]> The Tcpdump Group git mirrors - tcpdump/blob - print-mpcp.c
don't include addrtoname.h needlessly
[tcpdump] / print-mpcp.c
1 /*
2 * Copyright (c) 1998-2006 The TCPDUMP project
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that: (1) source code
6 * distributions retain the above copyright notice and this paragraph
7 * in its entirety, and (2) distributions including binary code include
8 * the above copyright notice and this paragraph in its entirety in
9 * the documentation or other materials provided with the distribution.
10 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
11 * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
12 * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
13 * FOR A PARTICULAR PURPOSE.
14 *
15 * support for the IEEE MPCP protocol as per 802.3ah
16 *
17 * Original code by Hannes Gredler (hannes@juniper.net)
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <tcpdump-stdinc.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "interface.h"
31 #include "extract.h"
32 #include "ether.h"
33
34 #define MPCP_TIMESTAMP_LEN 4
35 #define MPCP_TIMESTAMP_DURATION_LEN 2
36
37 struct mpcp_common_header_t {
38 u_int8_t opcode[2];
39 u_int8_t timestamp[MPCP_TIMESTAMP_LEN];
40 };
41
42 #define MPCP_OPCODE_PAUSE 0x0001
43 #define MPCP_OPCODE_GATE 0x0002
44 #define MPCP_OPCODE_REPORT 0x0003
45 #define MPCP_OPCODE_REG_REQ 0x0004
46 #define MPCP_OPCODE_REG 0x0005
47 #define MPCP_OPCODE_REG_ACK 0x0006
48
49 static const struct tok mpcp_opcode_values[] = {
50 { MPCP_OPCODE_PAUSE, "Pause" },
51 { MPCP_OPCODE_GATE, "Gate" },
52 { MPCP_OPCODE_REPORT, "Report" },
53 { MPCP_OPCODE_REG_REQ, "Register Request" },
54 { MPCP_OPCODE_REG, "Register" },
55 { MPCP_OPCODE_REG_ACK, "Register ACK" },
56 { 0, NULL}
57 };
58
59 #define MPCP_GRANT_NUMBER_LEN 1
60 #define MPCP_GRANT_NUMBER_MASK 0x7
61 static const struct tok mpcp_grant_flag_values[] = {
62 { 0x08, "Discovery" },
63 { 0x10, "Force Grant #1" },
64 { 0x20, "Force Grant #2" },
65 { 0x40, "Force Grant #3" },
66 { 0x80, "Force Grant #4" },
67 { 0, NULL}
68 };
69
70 struct mpcp_grant_t {
71 u_int8_t starttime[MPCP_TIMESTAMP_LEN];
72 u_int8_t duration[MPCP_TIMESTAMP_DURATION_LEN];
73 };
74
75 struct mpcp_reg_req_t {
76 u_int8_t flags;
77 u_int8_t pending_grants;
78 };
79
80
81 static const struct tok mpcp_reg_req_flag_values[] = {
82 { 1, "Register" },
83 { 3, "De-Register" },
84 { 0, NULL}
85 };
86
87 struct mpcp_reg_t {
88 u_int8_t assigned_port[2];
89 u_int8_t flags;
90 u_int8_t sync_time[MPCP_TIMESTAMP_DURATION_LEN];
91 u_int8_t echoed_pending_grants;
92 };
93
94 static const struct tok mpcp_reg_flag_values[] = {
95 { 1, "Re-Register" },
96 { 2, "De-Register" },
97 { 3, "ACK" },
98 { 4, "NACK" },
99 { 0, NULL}
100 };
101
102 #define MPCP_REPORT_QUEUESETS_LEN 1
103 #define MPCP_REPORT_REPORTBITMAP_LEN 1
104 static const struct tok mpcp_report_bitmap_values[] = {
105 { 0x01, "Q0" },
106 { 0x02, "Q1" },
107 { 0x04, "Q2" },
108 { 0x08, "Q3" },
109 { 0x10, "Q4" },
110 { 0x20, "Q5" },
111 { 0x40, "Q6" },
112 { 0x80, "Q7" },
113 { 0, NULL}
114 };
115
116 struct mpcp_reg_ack_t {
117 u_int8_t flags;
118 u_int8_t echoed_assigned_port[2];
119 u_int8_t echoed_sync_time[MPCP_TIMESTAMP_DURATION_LEN];
120 };
121
122 static const struct tok mpcp_reg_ack_flag_values[] = {
123 { 0, "NACK" },
124 { 1, "ACK" },
125 { 0, NULL}
126 };
127
128 void
129 mpcp_print(register const u_char *pptr, register u_int length) {
130
131 union {
132 const struct mpcp_common_header_t *common_header;
133 const struct mpcp_grant_t *grant;
134 const struct mpcp_reg_req_t *reg_req;
135 const struct mpcp_reg_t *reg;
136 const struct mpcp_reg_ack_t *reg_ack;
137 } mpcp;
138
139
140 const u_char *tptr;
141 u_int16_t opcode;
142 u_int8_t grant_numbers, grant;
143 u_int8_t queue_sets, queue_set, report_bitmap, report;
144
145 tptr=pptr;
146 mpcp.common_header = (const struct mpcp_common_header_t *)pptr;
147
148 if (!TTEST2(*tptr, sizeof(const struct mpcp_common_header_t)))
149 goto trunc;
150 opcode = EXTRACT_16BITS(mpcp.common_header->opcode);
151 printf("MPCP, Opcode %s", tok2str(mpcp_opcode_values, "Unknown (%u)", opcode));
152 if (opcode != MPCP_OPCODE_PAUSE) {
153 printf(", Timestamp %u ticks", EXTRACT_32BITS(mpcp.common_header->timestamp));
154 }
155 printf(", length %u", length);
156
157 if (!vflag)
158 return;
159
160 tptr += sizeof(const struct mpcp_common_header_t);
161
162 switch (opcode) {
163 case MPCP_OPCODE_PAUSE:
164 break;
165
166 case MPCP_OPCODE_GATE:
167 if (!TTEST2(*tptr, MPCP_GRANT_NUMBER_LEN))
168 goto trunc;
169 grant_numbers = *tptr & MPCP_GRANT_NUMBER_MASK;
170 printf("\n\tGrant Numbers %u, Flags [ %s ]",
171 grant_numbers,
172 bittok2str(mpcp_grant_flag_values,
173 "?",
174 *tptr &~ MPCP_GRANT_NUMBER_MASK));
175 tptr++;
176
177 for (grant = 1; grant <= grant_numbers; grant++) {
178 if (!TTEST2(*tptr, sizeof(const struct mpcp_grant_t)))
179 goto trunc;
180 mpcp.grant = (const struct mpcp_grant_t *)tptr;
181 printf("\n\tGrant #%u, Start-Time %u ticks, duration %u ticks",
182 grant,
183 EXTRACT_32BITS(mpcp.grant->starttime),
184 EXTRACT_16BITS(mpcp.grant->duration));
185 tptr += sizeof(const struct mpcp_grant_t);
186 }
187
188 if (!TTEST2(*tptr, MPCP_TIMESTAMP_DURATION_LEN))
189 goto trunc;
190 printf("\n\tSync-Time %u ticks", EXTRACT_16BITS(tptr));
191 break;
192
193
194 case MPCP_OPCODE_REPORT:
195 if (!TTEST2(*tptr, MPCP_REPORT_QUEUESETS_LEN))
196 goto trunc;
197 queue_sets = *tptr;
198 tptr+=MPCP_REPORT_QUEUESETS_LEN;
199 printf("\n\tTotal Queue-Sets %u", queue_sets);
200
201 for (queue_set = 1; queue_set < queue_sets; queue_set++) {
202 if (!TTEST2(*tptr, MPCP_REPORT_REPORTBITMAP_LEN))
203 goto trunc;
204 report_bitmap = *(tptr);
205 printf("\n\t Queue-Set #%u, Report-Bitmap [ %s ]",
206 queue_sets,
207 bittok2str(mpcp_report_bitmap_values, "Unknown", report_bitmap));
208 tptr++;
209
210 report=1;
211 while (report_bitmap != 0) {
212 if (report_bitmap & 1) {
213 if (!TTEST2(*tptr, MPCP_TIMESTAMP_DURATION_LEN))
214 goto trunc;
215 printf("\n\t Q%u Report, Duration %u ticks",
216 report,
217 EXTRACT_16BITS(tptr));
218 tptr+=MPCP_TIMESTAMP_DURATION_LEN;
219 }
220 report++;
221 report_bitmap = report_bitmap >> 1;
222 }
223 }
224 break;
225
226 case MPCP_OPCODE_REG_REQ:
227 if (!TTEST2(*tptr, sizeof(const struct mpcp_reg_req_t)))
228 goto trunc;
229 mpcp.reg_req = (const struct mpcp_reg_req_t *)tptr;
230 printf("\n\tFlags [ %s ], Pending-Grants %u",
231 bittok2str(mpcp_reg_req_flag_values, "Reserved", mpcp.reg_req->flags),
232 mpcp.reg_req->pending_grants);
233 break;
234
235 case MPCP_OPCODE_REG:
236 if (!TTEST2(*tptr, sizeof(const struct mpcp_reg_t)))
237 goto trunc;
238 mpcp.reg = (const struct mpcp_reg_t *)tptr;
239 printf("\n\tAssigned-Port %u, Flags [ %s ]" \
240 "\n\tSync-Time %u ticks, Echoed-Pending-Grants %u",
241 EXTRACT_16BITS(mpcp.reg->assigned_port),
242 bittok2str(mpcp_reg_flag_values, "Reserved", mpcp.reg->flags),
243 EXTRACT_16BITS(mpcp.reg->sync_time),
244 mpcp.reg->echoed_pending_grants);
245 break;
246
247 case MPCP_OPCODE_REG_ACK:
248 if (!TTEST2(*tptr, sizeof(const struct mpcp_reg_ack_t)))
249 goto trunc;
250 mpcp.reg_ack = (const struct mpcp_reg_ack_t *)tptr;
251 printf("\n\tEchoed-Assigned-Port %u, Flags [ %s ]" \
252 "\n\tEchoed-Sync-Time %u ticks",
253 EXTRACT_16BITS(mpcp.reg_ack->echoed_assigned_port),
254 bittok2str(mpcp_reg_ack_flag_values, "Reserved", mpcp.reg_ack->flags),
255 EXTRACT_16BITS(mpcp.reg_ack->echoed_sync_time));
256 break;
257
258 default:
259 /* unknown opcode - hexdump for now */
260 print_unknown_data(gndo,pptr, "\n\t", length);
261 break;
262 }
263
264 return;
265
266 trunc:
267 printf("\n\t[|MPCP]");
268 }
269 /*
270 * Local Variables:
271 * c-style: whitesmith
272 * c-basic-offset: 8
273 * End:
274 */