]> The Tcpdump Group git mirrors - tcpdump/blob - print-slow.c
fix an infinite loop while processing OAM info PDUs
[tcpdump] / print-slow.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 "slow protocols" LACP, MARKER as per 802.3ad
16 * OAM as per 802.3ah
17 *
18 * Original code by Hannes Gredler (hannes@juniper.net)
19 */
20
21 #ifndef lint
22 static const char rcsid[] _U_ =
23 "@(#) $Header: /tcpdump/master/tcpdump/print-slow.c,v 1.4 2006-05-16 21:57:26 hannes Exp $";
24 #endif
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <tcpdump-stdinc.h>
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "interface.h"
37 #include "extract.h"
38 #include "addrtoname.h"
39 #include "ether.h"
40 #include "oui.h"
41
42 struct slow_common_header_t {
43 u_int8_t proto_subtype;
44 u_int8_t version;
45 };
46
47 #define SLOW_PROTO_LACP 1
48 #define SLOW_PROTO_MARKER 2
49 #define SLOW_PROTO_OAM 3
50
51 #define LACP_VERSION 1
52 #define MARKER_VERSION 1
53
54 static const struct tok slow_proto_values[] = {
55 { SLOW_PROTO_LACP, "LACP" },
56 { SLOW_PROTO_MARKER, "MARKER" },
57 { SLOW_PROTO_OAM, "OAM" },
58 { 0, NULL}
59 };
60
61 static const struct tok slow_oam_flag_values[] = {
62 { 0x0001, "Link Fault" },
63 { 0x0002, "Dying Gasp" },
64 { 0x0004, "Critical Event" },
65 { 0x0008, "Local Evaluating" },
66 { 0x0010, "Local Stable" },
67 { 0x0020, "Remote Evaluating" },
68 { 0x0040, "Remote Stable" },
69 { 0, NULL}
70 };
71
72 #define SLOW_OAM_CODE_INFO 0x00
73 #define SLOW_OAM_CODE_EVENT_NOTIF 0x01
74 #define SLOW_OAM_CODE_VAR_REQUEST 0x02
75 #define SLOW_OAM_CODE_VAR_RESPONSE 0x03
76 #define SLOW_OAM_CODE_LOOPBACK_CTRL 0x04
77 #define SLOW_OAM_CODE_PRIVATE 0xfe
78
79 static const struct tok slow_oam_code_values[] = {
80 { SLOW_OAM_CODE_INFO, "Information" },
81 { SLOW_OAM_CODE_EVENT_NOTIF, "Event Notification" },
82 { SLOW_OAM_CODE_VAR_REQUEST, "Variable Request" },
83 { SLOW_OAM_CODE_VAR_RESPONSE, "Variable Response" },
84 { SLOW_OAM_CODE_LOOPBACK_CTRL, "Loopback Control" },
85 { SLOW_OAM_CODE_PRIVATE, "Vendor Private" },
86 { 0, NULL}
87 };
88
89 struct slow_oam_info_t {
90 u_int8_t info_type;
91 u_int8_t info_len;
92 u_int8_t oam_version;
93 u_int8_t revision[2];
94 u_int8_t state;
95 u_int8_t oam_config;
96 u_int8_t oam_pdu_config;
97 u_int8_t oui[3];
98 u_int8_t vendor_private[4];
99 };
100
101 #define SLOW_OAM_INFO_TYPE_END_OF_TLV 0x00
102 #define SLOW_OAM_INFO_TYPE_LOCAL 0x01
103 #define SLOW_OAM_INFO_TYPE_REMOTE 0x02
104 #define SLOW_OAM_INFO_TYPE_ORG_SPECIFIC 0xfe
105
106 static const struct tok slow_oam_info_type_values[] = {
107 { SLOW_OAM_INFO_TYPE_END_OF_TLV, "End of TLV marker" },
108 { SLOW_OAM_INFO_TYPE_LOCAL, "Local" },
109 { SLOW_OAM_INFO_TYPE_REMOTE, "Remote" },
110 { SLOW_OAM_INFO_TYPE_ORG_SPECIFIC, "Organization specific" },
111 { 0, NULL}
112 };
113
114 #define SLOW_OAM_INFO_TYPE_LOCAL_REMOTE_MINLEN 16
115
116 #define OAM_INFO_TYPE_PARSER_MASK 0x3
117 static const struct tok slow_oam_info_type_state_parser_values[] = {
118 { 0x00, "forwarding" },
119 { 0x01, "looping back" },
120 { 0x02, "discarding" },
121 { 0x03, "reserved" },
122 { 0, NULL}
123 };
124
125 #define OAM_INFO_TYPE_MUX_MASK 0x4
126 static const struct tok slow_oam_info_type_state_mux_values[] = {
127 { 0x00, "forwarding" },
128 { 0x04, "discarding" },
129 { 0, NULL}
130 };
131
132 static const struct tok slow_oam_info_type_oam_config_values[] = {
133 { 0x01, "Active" },
134 { 0x02, "Unidirectional" },
135 { 0x04, "Remote-Loopback" },
136 { 0x08, "Link-Events" },
137 { 0x10, "Variable-Retrieval" },
138 { 0, NULL}
139 };
140
141 /* 11 Bits */
142 #define OAM_INFO_TYPE_PDU_SIZE_MASK 0x7ff
143
144 struct slow_oam_eventnotification_t {
145 u_int8_t event_type;
146 u_int8_t event_length;
147 u_int8_t event_time_stamp[2];
148 u_int8_t window[8];
149 u_int8_t threshold[8];
150 u_int8_t errors[8];
151 u_int8_t errors_running_total[8];
152 u_int8_t event_running_total[4];
153 };
154
155 struct slow_oam_variablerequest_t {
156 u_int8_t branch;
157 u_int8_t leaf[2];
158 };
159
160 struct slow_oam_variableresponse_t {
161 u_int8_t branch;
162 u_int8_t leaf[2];
163 u_int8_t length;
164 };
165
166 static const struct tok slow_oam_loopbackctrl_cmd_values[] = {
167 { 0x01, "Enable OAM Remote Loopback" },
168 { 0x02, "Disable OAM Remote Loopback" },
169 { 0, NULL}
170 };
171
172 struct tlv_header_t {
173 u_int8_t type;
174 u_int8_t length;
175 };
176
177 #define LACP_TLV_TERMINATOR 0x00
178 #define LACP_TLV_ACTOR_INFO 0x01
179 #define LACP_TLV_PARTNER_INFO 0x02
180 #define LACP_TLV_COLLECTOR_INFO 0x03
181
182 #define MARKER_TLV_TERMINATOR 0x00
183 #define MARKER_TLV_MARKER_INFO 0x01
184
185 static const struct tok slow_tlv_values[] = {
186 { (SLOW_PROTO_LACP << 8) + LACP_TLV_TERMINATOR, "Terminator"},
187 { (SLOW_PROTO_LACP << 8) + LACP_TLV_ACTOR_INFO, "Actor Information"},
188 { (SLOW_PROTO_LACP << 8) + LACP_TLV_PARTNER_INFO, "Partner Information"},
189 { (SLOW_PROTO_LACP << 8) + LACP_TLV_COLLECTOR_INFO, "Collector Information"},
190
191 { (SLOW_PROTO_MARKER << 8) + MARKER_TLV_TERMINATOR, "Terminator"},
192 { (SLOW_PROTO_MARKER << 8) + MARKER_TLV_MARKER_INFO, "Marker Information"},
193 { 0, NULL}
194 };
195
196 struct lacp_tlv_actor_partner_info_t {
197 u_int8_t sys_pri[2];
198 u_int8_t sys[ETHER_ADDR_LEN];
199 u_int8_t key[2];
200 u_int8_t port_pri[2];
201 u_int8_t port[2];
202 u_int8_t state;
203 u_int8_t pad[3];
204 };
205
206 static const struct tok lacp_tlv_actor_partner_info_state_values[] = {
207 { 0x01, "Activity"},
208 { 0x02, "Timeout"},
209 { 0x04, "Aggregation"},
210 { 0x08, "Synchronization"},
211 { 0x10, "Collecting"},
212 { 0x20, "Distributing"},
213 { 0x40, "Default"},
214 { 0x80, "Expired"},
215 { 0, NULL}
216 };
217
218 struct lacp_tlv_collector_info_t {
219 u_int8_t max_delay[2];
220 u_int8_t pad[12];
221 };
222
223 struct marker_tlv_marker_info_t {
224 u_int8_t req_port[2];
225 u_int8_t req_sys[ETHER_ADDR_LEN];
226 u_int8_t req_trans_id[4];
227 u_int8_t pad[2];
228 };
229
230 struct lacp_marker_tlv_terminator_t {
231 u_int8_t pad[50];
232 };
233
234 void slow_marker_lacp_print(register const u_char *, register u_int);
235 void slow_oam_print(register const u_char *, register u_int);
236
237 const struct slow_common_header_t *slow_com_header;
238
239 void
240 slow_print(register const u_char *pptr, register u_int len) {
241
242 int print_version;
243
244 slow_com_header = (const struct slow_common_header_t *)pptr;
245 TCHECK(*slow_com_header);
246
247 /*
248 * Sanity checking of the header.
249 */
250 switch (slow_com_header->proto_subtype) {
251 case SLOW_PROTO_LACP:
252 if (slow_com_header->version != LACP_VERSION) {
253 printf("LACP version %u packet not supported",slow_com_header->version);
254 return;
255 }
256 print_version = 1;
257 break;
258
259 case SLOW_PROTO_MARKER:
260 if (slow_com_header->version != MARKER_VERSION) {
261 printf("MARKER version %u packet not supported",slow_com_header->version);
262 return;
263 }
264 print_version = 1;
265 break;
266
267 case SLOW_PROTO_OAM: /* fall through */
268 print_version = 0;
269 break;
270
271 default:
272 /* print basic information and exit */
273 print_version = -1;
274 break;
275 }
276
277 if (print_version) {
278 printf("%sv%u, length %u",
279 tok2str(slow_proto_values, "unknown (%u)",slow_com_header->proto_subtype),
280 slow_com_header->version,
281 len);
282 } else {
283 /* some slow protos don't have a version number in the header */
284 printf("%s, length %u",
285 tok2str(slow_proto_values, "unknown (%u)",slow_com_header->proto_subtype),
286 len);
287 }
288
289 /* unrecognized subtype */
290 if (print_version == -1) {
291 print_unknown_data(pptr, "\n\t", len);
292 return;
293 }
294
295 if (!vflag)
296 return;
297
298 switch (slow_com_header->proto_subtype) {
299 default: /* should not happen */
300 break;
301
302 case SLOW_PROTO_OAM:
303 /* skip proto_subtype */
304 slow_oam_print(pptr+1, len-1);
305 break;
306
307 case SLOW_PROTO_LACP: /* LACP and MARKER share the same semantics */
308 case SLOW_PROTO_MARKER:
309 /* skip slow_common_header */
310 len -= sizeof(const struct slow_common_header_t);
311 pptr += sizeof(const struct slow_common_header_t);
312 slow_marker_lacp_print(pptr, len);
313 break;
314 }
315 return;
316
317 trunc:
318 printf("\n\t\t packet exceeded snapshot");
319 }
320
321 void slow_marker_lacp_print(register const u_char *tptr, register u_int tlen) {
322
323 const struct tlv_header_t *tlv_header;
324 const u_char *tlv_tptr;
325 u_int tlv_len, tlv_tlen;
326
327 union {
328 const struct lacp_marker_tlv_terminator_t *lacp_marker_tlv_terminator;
329 const struct lacp_tlv_actor_partner_info_t *lacp_tlv_actor_partner_info;
330 const struct lacp_tlv_collector_info_t *lacp_tlv_collector_info;
331 const struct marker_tlv_marker_info_t *marker_tlv_marker_info;
332 } tlv_ptr;
333
334 while(tlen>0) {
335 /* did we capture enough for fully decoding the tlv header ? */
336 TCHECK2(*tptr, sizeof(struct tlv_header_t));
337 tlv_header = (const struct tlv_header_t *)tptr;
338 tlv_len = tlv_header->length;
339
340 printf("\n\t%s TLV (0x%02x), length %u",
341 tok2str(slow_tlv_values,
342 "Unknown",
343 (slow_com_header->proto_subtype << 8) + tlv_header->type),
344 tlv_header->type,
345 tlv_len);
346
347 if ((tlv_len < sizeof(struct tlv_header_t) ||
348 tlv_len > tlen) &&
349 tlv_header->type != LACP_TLV_TERMINATOR &&
350 tlv_header->type != MARKER_TLV_TERMINATOR) {
351 printf("\n\t-----trailing data-----");
352 print_unknown_data(tptr+sizeof(sizeof(struct tlv_header_t)),"\n\t ",tlen);
353 return;
354 }
355
356 tlv_tptr=tptr+sizeof(struct tlv_header_t);
357 tlv_tlen=tlv_len-sizeof(struct tlv_header_t);
358
359 /* did we capture enough for fully decoding the tlv ? */
360 TCHECK2(*tptr, tlv_len);
361
362 switch((slow_com_header->proto_subtype << 8) + tlv_header->type) {
363
364 /* those two TLVs have the same structure -> fall through */
365 case ((SLOW_PROTO_LACP << 8) + LACP_TLV_ACTOR_INFO):
366 case ((SLOW_PROTO_LACP << 8) + LACP_TLV_PARTNER_INFO):
367 tlv_ptr.lacp_tlv_actor_partner_info = (const struct lacp_tlv_actor_partner_info_t *)tlv_tptr;
368
369 printf("\n\t System %s, System Priority %u, Key %u" \
370 ", Port %u, Port Priority %u\n\t State Flags [%s]",
371 etheraddr_string(tlv_ptr.lacp_tlv_actor_partner_info->sys),
372 EXTRACT_16BITS(tlv_ptr.lacp_tlv_actor_partner_info->sys_pri),
373 EXTRACT_16BITS(tlv_ptr.lacp_tlv_actor_partner_info->key),
374 EXTRACT_16BITS(tlv_ptr.lacp_tlv_actor_partner_info->port),
375 EXTRACT_16BITS(tlv_ptr.lacp_tlv_actor_partner_info->port_pri),
376 bittok2str(lacp_tlv_actor_partner_info_state_values,
377 "none",
378 tlv_ptr.lacp_tlv_actor_partner_info->state));
379
380 break;
381
382 case ((SLOW_PROTO_LACP << 8) + LACP_TLV_COLLECTOR_INFO):
383 tlv_ptr.lacp_tlv_collector_info = (const struct lacp_tlv_collector_info_t *)tlv_tptr;
384
385 printf("\n\t Max Delay %u",
386 EXTRACT_16BITS(tlv_ptr.lacp_tlv_collector_info->max_delay));
387
388 break;
389
390 case ((SLOW_PROTO_MARKER << 8) + MARKER_TLV_MARKER_INFO):
391 tlv_ptr.marker_tlv_marker_info = (const struct marker_tlv_marker_info_t *)tlv_tptr;
392
393 printf("\n\t Request System %s, Request Port %u, Request Transaction ID 0x%08x",
394 etheraddr_string(tlv_ptr.marker_tlv_marker_info->req_sys),
395 EXTRACT_16BITS(tlv_ptr.marker_tlv_marker_info->req_port),
396 EXTRACT_32BITS(tlv_ptr.marker_tlv_marker_info->req_trans_id));
397
398 break;
399
400 /* those two TLVs have the same structure -> fall through */
401 case ((SLOW_PROTO_LACP << 8) + LACP_TLV_TERMINATOR):
402 case ((SLOW_PROTO_MARKER << 8) + LACP_TLV_TERMINATOR):
403 tlv_ptr.lacp_marker_tlv_terminator = (const struct lacp_marker_tlv_terminator_t *)tlv_tptr;
404 if (tlv_len == 0) {
405 tlv_len = sizeof(tlv_ptr.lacp_marker_tlv_terminator->pad) +
406 sizeof(struct tlv_header_t);
407 /* tell the user that we modified the length field */
408 if (vflag>1)
409 printf(" (=%u)",tlv_len);
410 /* we have messed around with the length field - now we need to check
411 * again if there are enough bytes on the wire for the hexdump */
412 TCHECK2(tlv_ptr.lacp_marker_tlv_terminator->pad[0],
413 sizeof(tlv_ptr.lacp_marker_tlv_terminator->pad));
414 }
415
416 break;
417
418 default:
419 if (vflag <= 1)
420 print_unknown_data(tlv_tptr,"\n\t ",tlv_tlen);
421 break;
422 }
423 /* do we want to see an additional hexdump ? */
424 if (vflag > 1) {
425 print_unknown_data(tptr+sizeof(sizeof(struct tlv_header_t)),"\n\t ",
426 tlv_len-sizeof(struct tlv_header_t));
427 }
428
429 tptr+=tlv_len;
430 tlen-=tlv_len;
431 }
432 return;
433 trunc:
434 printf("\n\t\t packet exceeded snapshot");
435 }
436
437 void slow_oam_print(register const u_char *tptr, register u_int tlen) {
438
439 struct slow_oam_common_header_t {
440 u_int8_t flags[2];
441 u_int8_t code;
442 };
443 const struct slow_oam_common_header_t *slow_oam_common_header;
444
445 union {
446 const struct slow_oam_info_t *slow_oam_info;
447 const struct slow_oam_eventnotification_t *slow_oam_eventnotification;
448 const struct slow_oam_variablerequest_t *slow_oam_variablerequest;
449 const struct slow_oam_variableresponse_t *slow_oam_variableresponse;
450 } tlv;
451
452 slow_oam_common_header = (struct slow_oam_common_header_t *)tptr;
453 tptr += sizeof(struct slow_oam_common_header_t);
454 tlen -= sizeof(struct slow_oam_common_header_t);
455
456 printf("\n\tCode %s OAM PDU, Flags [%s]",
457 tok2str(slow_oam_code_values, "Unknown (%u)", slow_oam_common_header->code),
458 bittok2str(slow_oam_flag_values,
459 "none",
460 EXTRACT_16BITS(&slow_oam_common_header->flags)));
461
462 switch (slow_oam_common_header->code) {
463 case SLOW_OAM_CODE_INFO:
464 while (tlen > 0) {
465 tlv.slow_oam_info = (const struct slow_oam_info_t *)tptr;
466 printf("\n\t %s Information Type (%u), Version %u, Rev %u, length %u",
467 tok2str(slow_oam_info_type_values, "Reserved", tlv.slow_oam_info->info_type),
468 tlv.slow_oam_info->info_type,
469 tlv.slow_oam_info->oam_version,
470 EXTRACT_16BITS(&tlv.slow_oam_info->revision),
471 tlv.slow_oam_info->info_len);
472
473 switch (tlv.slow_oam_info->info_type) {
474 case SLOW_OAM_INFO_TYPE_END_OF_TLV:
475
476 if (tlv.slow_oam_info->info_len != 0) {
477 printf("\n\t ERROR: illegal length - should be 0");
478 }
479 return;
480
481 case SLOW_OAM_INFO_TYPE_LOCAL: /* identical format - fall through */
482 case SLOW_OAM_INFO_TYPE_REMOTE:
483
484 if (tlv.slow_oam_info->info_len !=
485 SLOW_OAM_INFO_TYPE_LOCAL_REMOTE_MINLEN) {
486 printf("\n\t ERROR: illegal length - should be %u",
487 SLOW_OAM_INFO_TYPE_LOCAL_REMOTE_MINLEN);
488 return;
489 }
490
491 printf("\n\t State-MUX-Action %s, State-Parser-Action %s",
492 tok2str(slow_oam_info_type_state_parser_values, "Reserved",
493 tlv.slow_oam_info->state & OAM_INFO_TYPE_PARSER_MASK),
494 tok2str(slow_oam_info_type_state_mux_values, "Reserved",
495 tlv.slow_oam_info->state & OAM_INFO_TYPE_MUX_MASK));
496 printf("\n\t OAM-Config Flags [%s], OAM-PDU-Config max-PDU size %u",
497 bittok2str(slow_oam_info_type_oam_config_values, "none",
498 tlv.slow_oam_info->oam_config),
499 EXTRACT_16BITS(&tlv.slow_oam_info->oam_pdu_config) &
500 OAM_INFO_TYPE_PDU_SIZE_MASK);
501 printf("\n\t OUI %s (0x%06x), Vendor-Private 0x%08x",
502 tok2str(oui_values, "Unknown",
503 EXTRACT_24BITS(&tlv.slow_oam_info->oui)),
504 EXTRACT_24BITS(&tlv.slow_oam_info->oui),
505 EXTRACT_32BITS(&tlv.slow_oam_info->vendor_private));
506 break;
507
508 case SLOW_OAM_INFO_TYPE_ORG_SPECIFIC:
509 /* FIXME hexdump */
510 break;
511
512 default:
513 break;
514 }
515
516 /* infinite loop check */
517 if (!tlv.slow_oam_info->info_len) {
518 return;
519 }
520 tlen -= tlv.slow_oam_info->info_len;
521 tptr += tlv.slow_oam_info->info_len;
522 }
523 break;
524 /* FIXME no codes yet known - just hexdump for now */
525 case SLOW_OAM_CODE_EVENT_NOTIF:
526 case SLOW_OAM_CODE_VAR_REQUEST:
527 case SLOW_OAM_CODE_VAR_RESPONSE:
528 case SLOW_OAM_CODE_LOOPBACK_CTRL:
529 case SLOW_OAM_CODE_PRIVATE:
530 default:
531 if (vflag <= 1) {
532 print_unknown_data(tptr,"\n\t ", tlen);
533 }
534 break;
535 }
536 /* do we want to see an additional hexdump ? */
537 if (vflag > 1) {
538 print_unknown_data(tptr,"\n\t ", tlen);
539 }
540
541 return;
542 }