]> The Tcpdump Group git mirrors - tcpdump/blob - print-fr.c
-add frame-relay header documentation
[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.25 2004-10-09 17:55:26 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 lmi_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
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 frist 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_int32_t orgcode;
195 register u_short et;
196 u_int dlci;
197 u_int sdlcore;
198 u_int addr_len;
199 u_int16_t nlpid;
200 u_int hdr_len;
201 u_int8_t flags[4];
202
203 if (caplen < 4) { /* minimum frame header length */
204 printf("[|fr]");
205 return caplen;
206 }
207
208 if (parse_q922_addr(p, &dlci, &sdlcore, &addr_len, flags)) {
209 printf("Q.922, invalid address");
210 return caplen;
211 }
212
213 hdr_len = fr_hdrlen(p, addr_len, caplen);
214
215 if (caplen < hdr_len) {
216 printf("[|fr]");
217 return caplen;
218 }
219
220 if (p[addr_len] != 0x03) {
221
222 /* lets figure out if we have cisco style encapsulation: */
223 extracted_ethertype = EXTRACT_16BITS(p+addr_len);
224
225 if (eflag)
226 fr_hdr_print(length, addr_len, dlci, flags, extracted_ethertype);
227
228 if (ether_encap_print(extracted_ethertype,
229 p+addr_len+ETHERTYPE_LEN,
230 length-addr_len-ETHERTYPE_LEN,
231 caplen-addr_len-ETHERTYPE_LEN,
232 &extracted_ethertype) == 0)
233 /* ether_type not known, probably it wasn't one */
234 printf("UI %02x! ", p[addr_len]);
235 else
236 return hdr_len;
237 }
238
239 if (!p[addr_len + 1]) { /* pad byte should be used with 3-byte Q.922 */
240 if (addr_len != 3)
241 printf("Pad! ");
242 } else if (addr_len == 3)
243 printf("No pad! ");
244
245 nlpid = p[hdr_len - 1];
246
247 if (eflag)
248 fr_hdr_print(length, addr_len, dlci, flags, nlpid);
249
250 p += hdr_len;
251 length -= hdr_len;
252 caplen -= hdr_len;
253
254 switch (nlpid) {
255 case NLPID_IP:
256 ip_print(p, length);
257 break;
258
259 #ifdef INET6
260 case NLPID_IP6:
261 ip6_print(p, length);
262 break;
263 #endif
264 case NLPID_CLNP:
265 case NLPID_ESIS:
266 case NLPID_ISIS:
267 isoclns_print(p, length, caplen);
268 break;
269
270 case NLPID_SNAP:
271 orgcode = EXTRACT_24BITS(p);
272 et = EXTRACT_16BITS(p + 3);
273 if (snap_print((const u_char *)(p + 5), length - 5,
274 caplen - 5, &extracted_ethertype, orgcode, et,
275 0) == 0) {
276 /* ether_type not known, print raw packet */
277 if (!eflag)
278 fr_hdr_print(length + hdr_len, hdr_len,
279 dlci, flags, nlpid);
280 if (extracted_ethertype) {
281 printf("(SNAP %s) ",
282 etherproto_string(htons(extracted_ethertype)));
283 }
284 if (!xflag && !qflag)
285 default_print(p - hdr_len, caplen + hdr_len);
286 }
287 break;
288
289 case NLPID_LMI:
290 lmi_print(p, length);
291 break;
292
293 default:
294 if (!eflag)
295 fr_hdr_print(length + hdr_len, addr_len,
296 dlci, flags, nlpid);
297 if (!xflag)
298 default_print(p, caplen);
299 }
300
301 return hdr_len;
302 }
303
304 /*
305 * Q.933 decoding portion for framerelay specific.
306 */
307
308 /* Q.933 packet format
309 Format of Other Protocols
310 using Q.933 NLPID
311 +-------------------------------+
312 | Q.922 Address |
313 +---------------+---------------+
314 |Control 0x03 | NLPID 0x08 |
315 +---------------+---------------+
316 | L2 Protocol ID |
317 | octet 1 | octet 2 |
318 +-------------------------------+
319 | L3 Protocol ID |
320 | octet 2 | octet 2 |
321 +-------------------------------+
322 | Protocol Data |
323 +-------------------------------+
324 | FCS |
325 +-------------------------------+
326 */
327
328 /* L2 (Octet 1)- Call Reference Usually is 0x0 */
329
330 /*
331 * L2 (Octet 2)- Message Types definition 1 byte long.
332 */
333 /* Call Establish */
334 #define MSG_TYPE_ESC_TO_NATIONAL 0x00
335 #define MSG_TYPE_ALERT 0x01
336 #define MSG_TYPE_CALL_PROCEEDING 0x02
337 #define MSG_TYPE_CONNECT 0x07
338 #define MSG_TYPE_CONNECT_ACK 0x0F
339 #define MSG_TYPE_PROGRESS 0x03
340 #define MSG_TYPE_SETUP 0x05
341 /* Call Clear */
342 #define MSG_TYPE_DISCONNECT 0x45
343 #define MSG_TYPE_RELEASE 0x4D
344 #define MSG_TYPE_RELEASE_COMPLETE 0x5A
345 #define MSG_TYPE_RESTART 0x46
346 #define MSG_TYPE_RESTART_ACK 0x4E
347 /* Status */
348 #define MSG_TYPE_STATUS 0x7D
349 #define MSG_TYPE_STATUS_ENQ 0x75
350
351 struct tok fr_lmi_msg_values[] = {
352 { MSG_TYPE_ESC_TO_NATIONAL, "ESC to National" },
353 { MSG_TYPE_ALERT, "Alert" },
354 { MSG_TYPE_CALL_PROCEEDING, "Call proceeding" },
355 { MSG_TYPE_CONNECT, "Connect" },
356 { MSG_TYPE_CONNECT_ACK, "Connect ACK" },
357 { MSG_TYPE_PROGRESS, "Progress" },
358 { MSG_TYPE_SETUP, "Setup" },
359 { MSG_TYPE_DISCONNECT, "Disconnect" },
360 { MSG_TYPE_RELEASE, "Release" },
361 { MSG_TYPE_RELEASE_COMPLETE, "Release Complete" },
362 { MSG_TYPE_RESTART, "Restart" },
363 { MSG_TYPE_RESTART_ACK, "Restart ACK" },
364 { MSG_TYPE_STATUS, "Status Reply" },
365 { MSG_TYPE_STATUS_ENQ, "Status Enquiry" },
366 { 0, NULL }
367 };
368
369 #define MSG_ANSI_LOCKING_SHIFT 0x95
370
371 #define FR_LMI_ANSI_REPORT_TYPE_IE 0x01
372 #define FR_LMI_ANSI_LINK_VERIFY_IE_91 0x19 /* details? */
373 #define FR_LMI_ANSI_LINK_VERIFY_IE 0x03
374 #define FR_LMI_ANSI_PVC_STATUS_IE 0x07
375
376 #define FR_LMI_CCITT_REPORT_TYPE_IE 0x51
377 #define FR_LMI_CCITT_LINK_VERIFY_IE 0x53
378 #define FR_LMI_CCITT_PVC_STATUS_IE 0x57
379
380 struct tok fr_lmi_ie_values[] = {
381 { FR_LMI_ANSI_REPORT_TYPE_IE, "ANSI Report Type" },
382 { FR_LMI_ANSI_LINK_VERIFY_IE_91, "ANSI Link Verify" },
383 { FR_LMI_ANSI_LINK_VERIFY_IE, "ANSI Link Verify" },
384 { FR_LMI_ANSI_PVC_STATUS_IE, "ANSI PVC Status" },
385 { FR_LMI_CCITT_REPORT_TYPE_IE, "CCITT Report Type" },
386 { FR_LMI_CCITT_LINK_VERIFY_IE, "CCITT Link Verify" },
387 { FR_LMI_CCITT_PVC_STATUS_IE, "CCITT PVC Status" },
388 { 0, NULL }
389 };
390
391 #define FR_LMI_REPORT_TYPE_IE_FULL_STATUS 0
392 #define FR_LMI_REPORT_TYPE_IE_LINK_VERIFY 1
393 #define FR_LMI_REPORT_TYPE_IE_ASYNC_PVC 2
394
395 struct tok fr_lmi_report_type_ie_values[] = {
396 { FR_LMI_REPORT_TYPE_IE_FULL_STATUS, "Full Status" },
397 { FR_LMI_REPORT_TYPE_IE_LINK_VERIFY, "Link verify" },
398 { FR_LMI_REPORT_TYPE_IE_ASYNC_PVC, "Async PVC Status" },
399 { 0, NULL }
400 };
401
402 struct common_ie_header {
403 u_int8_t ie_id;
404 u_int8_t ie_len;
405 };
406
407 static void
408 lmi_print(const u_char *p, u_int length)
409 {
410 const u_char *ptemp = p;
411 struct common_ie_header *ie_p;
412 int olen;
413 int is_ansi = 0;
414 u_int dlci;
415
416 if (length < 9) { /* shortest: Q.933a LINK VERIFY */
417 printf("[|lmi]");
418 return;
419 }
420
421 if (p[2] == MSG_ANSI_LOCKING_SHIFT)
422 is_ansi = 1;
423
424 if (!eflag)
425 printf("Q.933 LMI, ");
426
427 /* printing out header part */
428 printf(is_ansi ? "ANSI" : "CCITT ");
429
430 if (p[0])
431 printf(", Call Ref: 0x%02x", p[0]);
432
433 if (vflag)
434 printf(", %s (0x%02x), length %u",
435 tok2str(fr_lmi_msg_values,"unknown message",p[1]),
436 p[1],
437 length);
438 else
439 printf(", %s",
440 tok2str(fr_lmi_msg_values,"unknown message 0x%02x",p[1]));
441
442 olen = length; /* preserve the original length for non verbose mode */
443
444 if (length < (u_int)(2 - is_ansi)) {
445 printf("[|lmi]");
446 return;
447 }
448 length -= 2 - is_ansi;
449 ptemp += 2 + is_ansi;
450
451 /* Loop through the rest of IE */
452 while (length > 0) {
453 ie_p = (struct common_ie_header *)ptemp;
454 if (length < sizeof(struct common_ie_header) ||
455 length < sizeof(struct common_ie_header) + ie_p->ie_len) {
456 if (vflag) /* not bark if there is just a trailer */
457 printf("\n[|lmi]");
458 else
459 printf(", length %u",olen);
460 return;
461 }
462
463 /* lets do the full IE parsing only in verbose mode
464 * however some IEs (DLCI Status, Link Verify)
465 * are also intereststing in non-verbose mode */
466 if (vflag)
467 printf("\n\t%s IE (%u), length %u: ",
468 tok2str(fr_lmi_ie_values,"unknown",ie_p->ie_id),
469 ie_p->ie_id,
470 ie_p->ie_len);
471
472 switch (ie_p->ie_id) {
473
474 case FR_LMI_ANSI_REPORT_TYPE_IE: /* fall through */
475 case FR_LMI_CCITT_REPORT_TYPE_IE:
476 if (vflag)
477 printf("%s (%u)",
478 tok2str(fr_lmi_report_type_ie_values,"unknown",ptemp[2]),
479 ptemp[2]);
480 break;
481
482 case FR_LMI_ANSI_LINK_VERIFY_IE: /* fall through */
483 case FR_LMI_CCITT_LINK_VERIFY_IE:
484 case FR_LMI_ANSI_LINK_VERIFY_IE_91:
485 if (!vflag)
486 printf(", ");
487 printf("TX Seq: %3d, RX Seq: %3d", ptemp[2], ptemp[3]);
488 break;
489 case FR_LMI_ANSI_PVC_STATUS_IE: /* fall through */
490 case FR_LMI_CCITT_PVC_STATUS_IE:
491 if (!vflag)
492 printf(", ");
493 /* now parse the DLCI information element. */
494 if ((ie_p->ie_len < 3) ||
495 (ptemp[2] & 0x80) ||
496 ((ie_p->ie_len == 3) && !(ptemp[3] & 0x80)) ||
497 ((ie_p->ie_len == 4) && ((ptemp[3] & 0x80) || !(ptemp[4] & 0x80))) ||
498 ((ie_p->ie_len == 5) && ((ptemp[3] & 0x80) || (ptemp[4] & 0x80) ||
499 !(ptemp[5] & 0x80))) ||
500 (ie_p->ie_len > 5) ||
501 !(ptemp[ie_p->ie_len + 1] & 0x80))
502 printf("Invalid DLCI IE");
503
504 dlci = ((ptemp[2] & 0x3F) << 4) | ((ptemp[3] & 0x78) >> 3);
505 if (ie_p->ie_len == 4)
506 dlci = (dlci << 6) | ((ptemp[4] & 0x7E) >> 1);
507 else if (ie_p->ie_len == 5)
508 dlci = (dlci << 13) | (ptemp[4] & 0x7F) | ((ptemp[5] & 0x7E) >> 1);
509
510 printf("DLCI %u: status %s%s", dlci,
511 ptemp[ie_p->ie_len + 1] & 0x8 ? "New, " : "",
512 ptemp[ie_p->ie_len + 1] & 0x2 ? "Active" : "Inactive");
513 break;
514
515 default:
516 if (vflag <= 1)
517 print_unknown_data(ptemp+2,"\n\t",ie_p->ie_len);
518 break;
519 }
520
521 /* do we want to see a hexdump of the IE ? */
522 if (vflag> 1)
523 print_unknown_data(ptemp+2,"\n\t ",ie_p->ie_len);
524
525 length = length - ie_p->ie_len - 2;
526 ptemp = ptemp + ie_p->ie_len + 2;
527 }
528 if (!vflag)
529 printf(", length %u",olen);
530 }