]> The Tcpdump Group git mirrors - tcpdump/blob - print-mpcp.c
Use EXTRACT_nBITS even when just testing against zero.
[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 "addrtoname.h"
33 #include "ether.h"
34
35 #define MPCP_TIMESTAMP_LEN 4
36 #define MPCP_TIMESTAMP_DURATION_LEN 2
37
38 struct mpcp_common_header_t {
39 u_int8_t opcode[2];
40 u_int8_t timestamp[MPCP_TIMESTAMP_LEN];
41 };
42
43 #define MPCP_OPCODE_PAUSE 0x0001
44 #define MPCP_OPCODE_GATE 0x0002
45 #define MPCP_OPCODE_REPORT 0x0003
46 #define MPCP_OPCODE_REG_REQ 0x0004
47 #define MPCP_OPCODE_REG 0x0005
48 #define MPCP_OPCODE_REG_ACK 0x0006
49
50 static const struct tok mpcp_opcode_values[] = {
51 { MPCP_OPCODE_PAUSE, "Pause" },
52 { MPCP_OPCODE_GATE, "Gate" },
53 { MPCP_OPCODE_REPORT, "Report" },
54 { MPCP_OPCODE_REG_REQ, "Register Request" },
55 { MPCP_OPCODE_REG, "Register" },
56 { MPCP_OPCODE_REG_ACK, "Register ACK" },
57 { 0, NULL}
58 };
59
60 #define MPCP_GRANT_NUMBER_LEN 1
61 #define MPCP_GRANT_NUMBER_MASK 0x7
62 static const struct tok mpcp_grant_flag_values[] = {
63 { 0x08, "Discovery" },
64 { 0x10, "Force Grant #1" },
65 { 0x20, "Force Grant #2" },
66 { 0x40, "Force Grant #3" },
67 { 0x80, "Force Grant #4" },
68 { 0, NULL}
69 };
70
71 struct mpcp_grant_t {
72 u_int8_t starttime[MPCP_TIMESTAMP_LEN];
73 u_int8_t duration[MPCP_TIMESTAMP_DURATION_LEN];
74 };
75
76 struct mpcp_reg_req_t {
77 u_int8_t flags;
78 u_int8_t pending_grants;
79 };
80
81
82 static const struct tok mpcp_reg_req_flag_values[] = {
83 { 1, "Register" },
84 { 3, "De-Register" },
85 { 0, NULL}
86 };
87
88 struct mpcp_reg_t {
89 u_int8_t assigned_port[2];
90 u_int8_t flags;
91 u_int8_t sync_time[MPCP_TIMESTAMP_DURATION_LEN];
92 u_int8_t echoed_pending_grants;
93 };
94
95 static const struct tok mpcp_reg_flag_values[] = {
96 { 1, "Re-Register" },
97 { 2, "De-Register" },
98 { 3, "ACK" },
99 { 4, "NACK" },
100 { 0, NULL}
101 };
102
103 #define MPCP_REPORT_QUEUESETS_LEN 1
104 #define MPCP_REPORT_REPORTBITMAP_LEN 1
105 static const struct tok mpcp_report_bitmap_values[] = {
106 { 0x01, "Q0" },
107 { 0x02, "Q1" },
108 { 0x04, "Q2" },
109 { 0x08, "Q3" },
110 { 0x10, "Q4" },
111 { 0x20, "Q5" },
112 { 0x40, "Q6" },
113 { 0x80, "Q7" },
114 { 0, NULL}
115 };
116
117 struct mpcp_reg_ack_t {
118 u_int8_t flags;
119 u_int8_t echoed_assigned_port[2];
120 u_int8_t echoed_sync_time[MPCP_TIMESTAMP_DURATION_LEN];
121 };
122
123 static const struct tok mpcp_reg_ack_flag_values[] = {
124 { 0, "NACK" },
125 { 1, "ACK" },
126 { 0, NULL}
127 };
128
129 void
130 mpcp_print(register const u_char *pptr, register u_int length) {
131
132 union {
133 const struct mpcp_common_header_t *common_header;
134 const struct mpcp_grant_t *grant;
135 const struct mpcp_reg_req_t *reg_req;
136 const struct mpcp_reg_t *reg;
137 const struct mpcp_reg_ack_t *reg_ack;
138 } mpcp;
139
140
141 const u_char *tptr;
142 u_int16_t opcode;
143 u_int8_t grant_numbers, grant;
144 u_int8_t queue_sets, queue_set, report_bitmap, report;
145
146 tptr=pptr;
147 mpcp.common_header = (const struct mpcp_common_header_t *)pptr;
148
149 if (!TTEST2(*tptr, sizeof(const struct mpcp_common_header_t)))
150 goto trunc;
151 opcode = EXTRACT_16BITS(mpcp.common_header->opcode);
152 printf("MPCP, Opcode %s", tok2str(mpcp_opcode_values, "Unknown (%u)", opcode));
153 if (opcode != MPCP_OPCODE_PAUSE) {
154 printf(", Timestamp %u ticks", EXTRACT_32BITS(mpcp.common_header->timestamp));
155 }
156 printf(", length %u", length);
157
158 if (!vflag)
159 return;
160
161 tptr += sizeof(const struct mpcp_common_header_t);
162
163 switch (opcode) {
164 case MPCP_OPCODE_PAUSE:
165 break;
166
167 case MPCP_OPCODE_GATE:
168 if (!TTEST2(*tptr, MPCP_GRANT_NUMBER_LEN))
169 goto trunc;
170 grant_numbers = *tptr & MPCP_GRANT_NUMBER_MASK;
171 printf("\n\tGrant Numbers %u, Flags [ %s ]",
172 grant_numbers,
173 bittok2str(mpcp_grant_flag_values,
174 "?",
175 *tptr &~ MPCP_GRANT_NUMBER_MASK));
176 tptr++;
177
178 for (grant = 1; grant <= grant_numbers; grant++) {
179 if (!TTEST2(*tptr, sizeof(const struct mpcp_grant_t)))
180 goto trunc;
181 mpcp.grant = (const struct mpcp_grant_t *)tptr;
182 printf("\n\tGrant #%u, Start-Time %u ticks, duration %u ticks",
183 grant,
184 EXTRACT_32BITS(mpcp.grant->starttime),
185 EXTRACT_16BITS(mpcp.grant->duration));
186 tptr += sizeof(const struct mpcp_grant_t);
187 }
188
189 if (!TTEST2(*tptr, MPCP_TIMESTAMP_DURATION_LEN))
190 goto trunc;
191 printf("\n\tSync-Time %u ticks", EXTRACT_16BITS(tptr));
192 break;
193
194
195 case MPCP_OPCODE_REPORT:
196 if (!TTEST2(*tptr, MPCP_REPORT_QUEUESETS_LEN))
197 goto trunc;
198 queue_sets = *tptr;
199 tptr+=MPCP_REPORT_QUEUESETS_LEN;
200 printf("\n\tTotal Queue-Sets %u", queue_sets);
201
202 for (queue_set = 1; queue_set < queue_sets; queue_set++) {
203 if (!TTEST2(*tptr, MPCP_REPORT_REPORTBITMAP_LEN))
204 goto trunc;
205 report_bitmap = *(tptr);
206 printf("\n\t Queue-Set #%u, Report-Bitmap [ %s ]",
207 queue_sets,
208 bittok2str(mpcp_report_bitmap_values, "Unknown", report_bitmap));
209 tptr++;
210
211 report=1;
212 while (report_bitmap != 0) {
213 if (report_bitmap & 1) {
214 if (!TTEST2(*tptr, MPCP_TIMESTAMP_DURATION_LEN))
215 goto trunc;
216 printf("\n\t Q%u Report, Duration %u ticks",
217 report,
218 EXTRACT_16BITS(tptr));
219 tptr+=MPCP_TIMESTAMP_DURATION_LEN;
220 }
221 report++;
222 report_bitmap = report_bitmap >> 1;
223 }
224 }
225 break;
226
227 case MPCP_OPCODE_REG_REQ:
228 if (!TTEST2(*tptr, sizeof(const struct mpcp_reg_req_t)))
229 goto trunc;
230 mpcp.reg_req = (const struct mpcp_reg_req_t *)tptr;
231 printf("\n\tFlags [ %s ], Pending-Grants %u",
232 bittok2str(mpcp_reg_req_flag_values, "Reserved", mpcp.reg_req->flags),
233 mpcp.reg_req->pending_grants);
234 break;
235
236 case MPCP_OPCODE_REG:
237 if (!TTEST2(*tptr, sizeof(const struct mpcp_reg_t)))
238 goto trunc;
239 mpcp.reg = (const struct mpcp_reg_t *)tptr;
240 printf("\n\tAssigned-Port %u, Flags [ %s ]" \
241 "\n\tSync-Time %u ticks, Echoed-Pending-Grants %u",
242 EXTRACT_16BITS(mpcp.reg->assigned_port),
243 bittok2str(mpcp_reg_flag_values, "Reserved", mpcp.reg->flags),
244 EXTRACT_16BITS(mpcp.reg->sync_time),
245 mpcp.reg->echoed_pending_grants);
246 break;
247
248 case MPCP_OPCODE_REG_ACK:
249 if (!TTEST2(*tptr, sizeof(const struct mpcp_reg_ack_t)))
250 goto trunc;
251 mpcp.reg_ack = (const struct mpcp_reg_ack_t *)tptr;
252 printf("\n\tEchoed-Assigned-Port %u, Flags [ %s ]" \
253 "\n\tEchoed-Sync-Time %u ticks",
254 EXTRACT_16BITS(mpcp.reg_ack->echoed_assigned_port),
255 bittok2str(mpcp_reg_ack_flag_values, "Reserved", mpcp.reg_ack->flags),
256 EXTRACT_16BITS(mpcp.reg_ack->echoed_sync_time));
257 break;
258
259 default:
260 /* unknown opcode - hexdump for now */
261 print_unknown_data(gndo,pptr, "\n\t", length);
262 break;
263 }
264
265 return;
266
267 trunc:
268 printf("\n\t[|MPCP]");
269 }
270 /*
271 * Local Variables:
272 * c-style: whitesmith
273 * c-basic-offset: 8
274 * End:
275 */