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