]> The Tcpdump Group git mirrors - tcpdump/blob - print-fr.c
remove null_print() and make use of tok2str() instead; proto names taken from etherty...
[tcpdump] / print-fr.c
1 /*
2 * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 */
21
22 #ifndef lint
23 static const char rcsid[] _U_ =
24 "@(#)$Header: /tcpdump/master/tcpdump/print-fr.c,v 1.35 2005-04-26 07:26:34 guy Exp $ (LBL)";
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <tcpdump-stdinc.h>
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <pcap.h>
36
37 #include "addrtoname.h"
38 #include "interface.h"
39 #include "ethertype.h"
40 #include "nlpid.h"
41 #include "extract.h"
42 #include "oui.h"
43
44 static void frf15_print(const u_char *, u_int);
45
46 /*
47 * the frame relay header has a variable length
48 *
49 * the EA bit determines if there is another byte
50 * in the header
51 *
52 * minimum header length is 2 bytes
53 * maximum header length is 4 bytes
54 *
55 * 7 6 5 4 3 2 1 0
56 * +----+----+----+----+----+----+----+----+
57 * | DLCI (6 bits) | CR | EA |
58 * +----+----+----+----+----+----+----+----+
59 * | DLCI (4 bits) |FECN|BECN| DE | EA |
60 * +----+----+----+----+----+----+----+----+
61 * | DLCI (7 bits) | EA |
62 * +----+----+----+----+----+----+----+----+
63 * | DLCI (6 bits) |SDLC| EA |
64 * +----+----+----+----+----+----+----+----+
65 */
66
67 #define FR_EA_BIT 0x01
68
69 #define FR_CR_BIT 0x02000000
70 #define FR_DE_BIT 0x00020000
71 #define FR_BECN_BIT 0x00040000
72 #define FR_FECN_BIT 0x00080000
73 #define FR_SDLC_BIT 0x00000002
74
75
76 struct tok fr_header_flag_values[] = {
77 { FR_CR_BIT, "C!" },
78 { FR_DE_BIT, "DE" },
79 { FR_BECN_BIT, "BECN" },
80 { FR_FECN_BIT, "FECN" },
81 { FR_SDLC_BIT, "sdlcore" },
82 { 0, NULL }
83 };
84
85
86 /* Finds out Q.922 address length, DLCI and flags. Returns 0 on success
87 * save the flags dep. on address length
88 */
89 static int parse_q922_addr(const u_char *p, u_int *dlci, u_int *sdlcore,
90 u_int *addr_len, u_int8_t *flags)
91 {
92 if ((p[0] & FR_EA_BIT))
93 return -1;
94
95 *addr_len = 2;
96 *dlci = ((p[0] & 0xFC) << 2) | ((p[1] & 0xF0) >> 4);
97
98 flags[0] = p[0] & 0x02; /* populate the first flag fields */
99 flags[1] = p[1] & 0x0c;
100
101 if (p[1] & FR_EA_BIT)
102 return 0; /* 2-byte Q.922 address */
103
104 p += 2;
105 (*addr_len)++; /* 3- or 4-byte Q.922 address */
106 if ((p[0] & FR_EA_BIT) == 0) {
107 *dlci = (*dlci << 7) | (p[0] >> 1);
108 (*addr_len)++; /* 4-byte Q.922 address */
109 p++;
110 }
111
112 if ((p[0] & FR_EA_BIT) == 0)
113 return -1; /* more than 4 bytes of Q.922 address? */
114
115 flags[3] = p[0] & 0x02;
116
117 if (p[0] & 0x02)
118 *sdlcore = p[0] >> 2;
119 else
120 *dlci = (*dlci << 6) | (p[0] >> 2);
121
122 return 0;
123 }
124
125 /* Frame Relay packet structure, with flags and CRC removed
126
127 +---------------------------+
128 | Q.922 Address* |
129 +-- --+
130 | |
131 +---------------------------+
132 | Control (UI = 0x03) |
133 +---------------------------+
134 | Optional Pad (0x00) |
135 +---------------------------+
136 | NLPID |
137 +---------------------------+
138 | . |
139 | . |
140 | . |
141 | Data |
142 | . |
143 | . |
144 +---------------------------+
145
146 * Q.922 addresses, as presently defined, are two octets and
147 contain a 10-bit DLCI. In some networks Q.922 addresses
148 may optionally be increased to three or four octets.
149 */
150
151 static u_int
152 fr_hdrlen(const u_char *p, u_int addr_len, u_int caplen)
153 {
154 if ((caplen > addr_len + 1 /* UI */ + 1 /* pad */) &&
155 !p[addr_len + 1] /* pad exist */)
156 return addr_len + 1 /* UI */ + 1 /* pad */ + 1 /* NLPID */;
157 else
158 return addr_len + 1 /* UI */ + 1 /* NLPID */;
159 }
160
161 static void
162 fr_hdr_print(int length, u_int addr_len, u_int dlci, u_int8_t *flags, u_int16_t nlpid)
163 {
164 if (qflag) {
165 (void)printf("Q.922, DLCI %u, length %u: ",
166 dlci,
167 length);
168 } else {
169 if (nlpid <= 0xff) /* if its smaller than 256 then its a NLPID */
170 (void)printf("Q.922, hdr-len %u, DLCI %u, Flags [%s], NLPID %s (0x%02x), length %u: ",
171 addr_len,
172 dlci,
173 bittok2str(fr_header_flag_values, "none", EXTRACT_32BITS(flags)),
174 tok2str(nlpid_values,"unknown", nlpid),
175 nlpid,
176 length);
177 else /* must be an ethertype */
178 (void)printf("Q.922, hdr-len %u, DLCI %u, Flags [%s], cisco-ethertype %s (0x%04x), length %u: ",
179 addr_len,
180 dlci,
181 bittok2str(fr_header_flag_values, "none", EXTRACT_32BITS(flags)),
182 tok2str(ethertype_values, "unknown", nlpid),
183 nlpid,
184 length);
185 }
186 }
187
188 u_int
189 fr_if_print(const struct pcap_pkthdr *h, register const u_char *p)
190 {
191 register u_int length = h->len;
192 register u_int caplen = h->caplen;
193 u_int16_t extracted_ethertype;
194 u_int dlci;
195 u_int sdlcore;
196 u_int addr_len;
197 u_int16_t nlpid;
198 u_int hdr_len;
199 u_int8_t flags[4];
200
201 if (caplen < 4) { /* minimum frame header length */
202 printf("[|fr]");
203 return caplen;
204 }
205
206 if (parse_q922_addr(p, &dlci, &sdlcore, &addr_len, flags)) {
207 printf("Q.922, invalid address");
208 return caplen;
209 }
210
211 hdr_len = fr_hdrlen(p, addr_len, caplen);
212
213 if (caplen < hdr_len) {
214 printf("[|fr]");
215 return caplen;
216 }
217
218 if (p[addr_len] != 0x03 && dlci != 0) {
219
220 /* lets figure out if we have cisco style encapsulation: */
221 extracted_ethertype = EXTRACT_16BITS(p+addr_len);
222
223 if (eflag)
224 fr_hdr_print(length, addr_len, dlci, flags, extracted_ethertype);
225
226 if (ether_encap_print(extracted_ethertype,
227 p+addr_len+ETHERTYPE_LEN,
228 length-addr_len-ETHERTYPE_LEN,
229 caplen-addr_len-ETHERTYPE_LEN,
230 &extracted_ethertype) == 0)
231 /* ether_type not known, probably it wasn't one */
232 printf("UI %02x! ", p[addr_len]);
233 else
234 return hdr_len;
235 }
236
237 if (!p[addr_len + 1]) { /* pad byte should be used with 3-byte Q.922 */
238 if (addr_len != 3)
239 printf("Pad! ");
240 } else if (addr_len == 3)
241 printf("No pad! ");
242
243 nlpid = p[hdr_len - 1];
244
245 if (eflag)
246 fr_hdr_print(length, addr_len, dlci, flags, nlpid);
247
248 p += hdr_len;
249 length -= hdr_len;
250 caplen -= hdr_len;
251
252 switch (nlpid) {
253 case NLPID_IP:
254 ip_print(gndo, p, length);
255 break;
256
257 #ifdef INET6
258 case NLPID_IP6:
259 ip6_print(p, length);
260 break;
261 #endif
262 case NLPID_CLNP:
263 case NLPID_ESIS:
264 case NLPID_ISIS:
265 isoclns_print(p-1, length+1, caplen+1); /* OSI printers need the NLPID field */
266 break;
267
268 case NLPID_SNAP:
269 if (snap_print(p, length, caplen, &extracted_ethertype, 0) == 0) {
270 /* ether_type not known, print raw packet */
271 if (!eflag)
272 fr_hdr_print(length + hdr_len, hdr_len,
273 dlci, flags, nlpid);
274 if (!xflag && !qflag)
275 default_print(p - hdr_len, caplen + hdr_len);
276 }
277 break;
278
279 case NLPID_Q933:
280 q933_print(p, length);
281 break;
282
283 case NLPID_MFR:
284 frf15_print(p, length);
285 break;
286
287 default:
288 if (!eflag)
289 fr_hdr_print(length + hdr_len, addr_len,
290 dlci, flags, nlpid);
291 if (!xflag)
292 default_print(p, caplen);
293 }
294
295 return hdr_len;
296 }
297
298 /* an NLPID of 0xb1 indicates a 2-byte
299 * FRF.15 header
300 *
301 * 7 6 5 4 3 2 1 0
302 * +----+----+----+----+----+----+----+----+
303 * ~ Q.922 header ~
304 * +----+----+----+----+----+----+----+----+
305 * | NLPID (8 bits) | NLPID=0xb1
306 * +----+----+----+----+----+----+----+----+
307 * | B | E | C |seq. (high 4 bits) | R |
308 * +----+----+----+----+----+----+----+----+
309 * | sequence (low 8 bits) |
310 * +----+----+----+----+----+----+----+----+
311 */
312
313 struct tok frf15_flag_values[] = {
314 { 0x80, "Begin" },
315 { 0x40, "End" },
316 { 0x20, "Control" },
317 { 0, NULL }
318 };
319
320 #define FR_FRF15_FRAGTYPE 0x01
321
322 static void
323 frf15_print (const u_char *p, u_int length) {
324
325 u_int16_t sequence_num, flags;
326
327 flags = p[0]&0xe0;
328 sequence_num = (p[0]&0x1e)<<7 | p[1];
329
330 printf("FRF.15, seq 0x%03x, Flags [%s],%s Fragmentation, length %u",
331 sequence_num,
332 bittok2str(frf15_flag_values,"none",flags),
333 flags&FR_FRF15_FRAGTYPE ? "Interface" : "End-to-End",
334 length);
335
336 /* TODO:
337 * depending on all permutations of the B, E and C bit
338 * dig as deep as we can - e.g. on the first (B) fragment
339 * there is enough payload to print the IP header
340 * on non (B) fragments it depends if the fragmentation
341 * model is end-to-end or interface based wether we want to print
342 * another Q.922 header
343 */
344
345 }
346
347 /*
348 * Q.933 decoding portion for framerelay specific.
349 */
350
351 /* Q.933 packet format
352 Format of Other Protocols
353 using Q.933 NLPID
354 +-------------------------------+
355 | Q.922 Address |
356 +---------------+---------------+
357 |Control 0x03 | NLPID 0x08 |
358 +---------------+---------------+
359 | L2 Protocol ID |
360 | octet 1 | octet 2 |
361 +-------------------------------+
362 | L3 Protocol ID |
363 | octet 2 | octet 2 |
364 +-------------------------------+
365 | Protocol Data |
366 +-------------------------------+
367 | FCS |
368 +-------------------------------+
369 */
370
371 /* L2 (Octet 1)- Call Reference Usually is 0x0 */
372
373 /*
374 * L2 (Octet 2)- Message Types definition 1 byte long.
375 */
376 /* Call Establish */
377 #define MSG_TYPE_ESC_TO_NATIONAL 0x00
378 #define MSG_TYPE_ALERT 0x01
379 #define MSG_TYPE_CALL_PROCEEDING 0x02
380 #define MSG_TYPE_CONNECT 0x07
381 #define MSG_TYPE_CONNECT_ACK 0x0F
382 #define MSG_TYPE_PROGRESS 0x03
383 #define MSG_TYPE_SETUP 0x05
384 /* Call Clear */
385 #define MSG_TYPE_DISCONNECT 0x45
386 #define MSG_TYPE_RELEASE 0x4D
387 #define MSG_TYPE_RELEASE_COMPLETE 0x5A
388 #define MSG_TYPE_RESTART 0x46
389 #define MSG_TYPE_RESTART_ACK 0x4E
390 /* Status */
391 #define MSG_TYPE_STATUS 0x7D
392 #define MSG_TYPE_STATUS_ENQ 0x75
393
394 struct tok fr_q933_msg_values[] = {
395 { MSG_TYPE_ESC_TO_NATIONAL, "ESC to National" },
396 { MSG_TYPE_ALERT, "Alert" },
397 { MSG_TYPE_CALL_PROCEEDING, "Call proceeding" },
398 { MSG_TYPE_CONNECT, "Connect" },
399 { MSG_TYPE_CONNECT_ACK, "Connect ACK" },
400 { MSG_TYPE_PROGRESS, "Progress" },
401 { MSG_TYPE_SETUP, "Setup" },
402 { MSG_TYPE_DISCONNECT, "Disconnect" },
403 { MSG_TYPE_RELEASE, "Release" },
404 { MSG_TYPE_RELEASE_COMPLETE, "Release Complete" },
405 { MSG_TYPE_RESTART, "Restart" },
406 { MSG_TYPE_RESTART_ACK, "Restart ACK" },
407 { MSG_TYPE_STATUS, "Status Reply" },
408 { MSG_TYPE_STATUS_ENQ, "Status Enquiry" },
409 { 0, NULL }
410 };
411
412 #define MSG_ANSI_LOCKING_SHIFT 0x95
413
414 #define FR_LMI_ANSI_REPORT_TYPE_IE 0x01
415 #define FR_LMI_ANSI_LINK_VERIFY_IE_91 0x19 /* details? */
416 #define FR_LMI_ANSI_LINK_VERIFY_IE 0x03
417 #define FR_LMI_ANSI_PVC_STATUS_IE 0x07
418
419 #define FR_LMI_CCITT_REPORT_TYPE_IE 0x51
420 #define FR_LMI_CCITT_LINK_VERIFY_IE 0x53
421 #define FR_LMI_CCITT_PVC_STATUS_IE 0x57
422
423 struct tok fr_q933_ie_values_codeset5[] = {
424 { FR_LMI_ANSI_REPORT_TYPE_IE, "ANSI Report Type" },
425 { FR_LMI_ANSI_LINK_VERIFY_IE_91, "ANSI Link Verify" },
426 { FR_LMI_ANSI_LINK_VERIFY_IE, "ANSI Link Verify" },
427 { FR_LMI_ANSI_PVC_STATUS_IE, "ANSI PVC Status" },
428 { FR_LMI_CCITT_REPORT_TYPE_IE, "CCITT Report Type" },
429 { FR_LMI_CCITT_LINK_VERIFY_IE, "CCITT Link Verify" },
430 { FR_LMI_CCITT_PVC_STATUS_IE, "CCITT PVC Status" },
431 { 0, NULL }
432 };
433
434 #define FR_LMI_REPORT_TYPE_IE_FULL_STATUS 0
435 #define FR_LMI_REPORT_TYPE_IE_LINK_VERIFY 1
436 #define FR_LMI_REPORT_TYPE_IE_ASYNC_PVC 2
437
438 struct tok fr_lmi_report_type_ie_values[] = {
439 { FR_LMI_REPORT_TYPE_IE_FULL_STATUS, "Full Status" },
440 { FR_LMI_REPORT_TYPE_IE_LINK_VERIFY, "Link verify" },
441 { FR_LMI_REPORT_TYPE_IE_ASYNC_PVC, "Async PVC Status" },
442 { 0, NULL }
443 };
444
445 /* array of 16 codepages - currently we only support codepage 5 */
446 static struct tok *fr_q933_ie_codesets[] = {
447 NULL,
448 NULL,
449 NULL,
450 NULL,
451 NULL,
452 fr_q933_ie_values_codeset5,
453 NULL,
454 NULL,
455 NULL,
456 NULL,
457 NULL,
458 NULL,
459 NULL,
460 NULL,
461 NULL,
462 NULL
463 };
464
465
466 struct common_ie_header {
467 u_int8_t ie_id;
468 u_int8_t ie_len;
469 };
470
471 static int fr_q933_print_ie_codeset5(const struct common_ie_header *ie_p,
472 const u_char *p);
473
474 typedef int (*codeset_pr_func_t)(const struct common_ie_header *ie_p,
475 const u_char *p);
476
477 /* array of 16 codepages - currently we only support codepage 5 */
478 static codeset_pr_func_t fr_q933_print_ie_codeset[] = {
479 NULL,
480 NULL,
481 NULL,
482 NULL,
483 NULL,
484 fr_q933_print_ie_codeset5,
485 NULL,
486 NULL,
487 NULL,
488 NULL,
489 NULL,
490 NULL,
491 NULL,
492 NULL,
493 NULL,
494 NULL
495 };
496
497 void
498 q933_print(const u_char *p, u_int length)
499 {
500 const u_char *ptemp = p;
501 struct common_ie_header *ie_p;
502 int olen;
503 int is_ansi = 0;
504 u_int codeset;
505
506 if (length < 9) { /* shortest: Q.933a LINK VERIFY */
507 printf("[|q.933]");
508 return;
509 }
510
511 codeset = p[2]&0x0f; /* extract the codeset */
512
513 if (p[2] == MSG_ANSI_LOCKING_SHIFT)
514 is_ansi = 1;
515
516 printf("%s", eflag ? "" : "Q.933, ");
517
518 /* printing out header part */
519 printf(is_ansi ? "ANSI" : "CCITT");
520
521 if (p[0])
522 printf(", Call Ref: 0x%02x", p[0]);
523
524 if (vflag)
525 printf(", %s (0x%02x), length %u",
526 tok2str(fr_q933_msg_values,"unknown message",p[1]),
527 p[1],
528 length);
529 else
530 printf(", %s",
531 tok2str(fr_q933_msg_values,"unknown message 0x%02x",p[1]));
532
533 olen = length; /* preserve the original length for non verbose mode */
534
535 if (length < (u_int)(2 - is_ansi)) {
536 printf("[|q.933]");
537 return;
538 }
539 length -= 2 - is_ansi;
540 ptemp += 2 + is_ansi;
541
542 /* Loop through the rest of IE */
543 while (length > sizeof(struct common_ie_header)) {
544 ie_p = (struct common_ie_header *)ptemp;
545 if (length < sizeof(struct common_ie_header) ||
546 length < sizeof(struct common_ie_header) + ie_p->ie_len) {
547 if (vflag) /* not bark if there is just a trailer */
548 printf("\n[|q.933]");
549 else
550 printf(", length %u",olen);
551 return;
552 }
553
554 /* lets do the full IE parsing only in verbose mode
555 * however some IEs (DLCI Status, Link Verify)
556 * are also intereststing in non-verbose mode */
557 if (vflag)
558 printf("\n\t%s IE (%u), length %u: ",
559 tok2str(fr_q933_ie_codesets[codeset],"unknown",ie_p->ie_id),
560 ie_p->ie_id,
561 ie_p->ie_len);
562
563 if (!fr_q933_print_ie_codeset[codeset] ||
564 (*fr_q933_print_ie_codeset[codeset])(ie_p, ptemp)) {
565 if (vflag <= 1)
566 print_unknown_data(ptemp+2,"\n\t",ie_p->ie_len);
567 }
568
569 /* do we want to see a hexdump of the IE ? */
570 if (vflag> 1)
571 print_unknown_data(ptemp+2,"\n\t ",ie_p->ie_len);
572
573 length = length - ie_p->ie_len - 2;
574 ptemp = ptemp + ie_p->ie_len + 2;
575 }
576 if (!vflag)
577 printf(", length %u",olen);
578 }
579
580 static int
581 fr_q933_print_ie_codeset5(const struct common_ie_header *ie_p, const u_char *p)
582 {
583 u_int dlci;
584
585 switch (ie_p->ie_id) {
586
587 case FR_LMI_ANSI_REPORT_TYPE_IE: /* fall through */
588 case FR_LMI_CCITT_REPORT_TYPE_IE:
589 if (vflag)
590 printf("%s (%u)",
591 tok2str(fr_lmi_report_type_ie_values,"unknown",p[2]),
592 p[2]);
593 return 1;
594
595 case FR_LMI_ANSI_LINK_VERIFY_IE: /* fall through */
596 case FR_LMI_CCITT_LINK_VERIFY_IE:
597 case FR_LMI_ANSI_LINK_VERIFY_IE_91:
598 if (!vflag)
599 printf(", ");
600 printf("TX Seq: %3d, RX Seq: %3d", p[2], p[3]);
601 return 1;
602
603 case FR_LMI_ANSI_PVC_STATUS_IE: /* fall through */
604 case FR_LMI_CCITT_PVC_STATUS_IE:
605 if (!vflag)
606 printf(", ");
607 /* now parse the DLCI information element. */
608 if ((ie_p->ie_len < 3) ||
609 (p[2] & 0x80) ||
610 ((ie_p->ie_len == 3) && !(p[3] & 0x80)) ||
611 ((ie_p->ie_len == 4) && ((p[3] & 0x80) || !(p[4] & 0x80))) ||
612 ((ie_p->ie_len == 5) && ((p[3] & 0x80) || (p[4] & 0x80) ||
613 !(p[5] & 0x80))) ||
614 (ie_p->ie_len > 5) ||
615 !(p[ie_p->ie_len + 1] & 0x80))
616 printf("Invalid DLCI IE");
617
618 dlci = ((p[2] & 0x3F) << 4) | ((p[3] & 0x78) >> 3);
619 if (ie_p->ie_len == 4)
620 dlci = (dlci << 6) | ((p[4] & 0x7E) >> 1);
621 else if (ie_p->ie_len == 5)
622 dlci = (dlci << 13) | (p[4] & 0x7F) | ((p[5] & 0x7E) >> 1);
623
624 printf("DLCI %u: status %s%s", dlci,
625 p[ie_p->ie_len + 1] & 0x8 ? "New, " : "",
626 p[ie_p->ie_len + 1] & 0x2 ? "Active" : "Inactive");
627 return 1;
628 }
629
630 return 0;
631 }