]> The Tcpdump Group git mirrors - tcpdump/blob - print-fr.c
From Neil Spring:
[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
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <tcpdump-stdinc.h>
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <pcap.h>
32
33 #include "addrtoname.h"
34 #include "interface.h"
35 #include "ethertype.h"
36 #include "extract.h"
37
38 #ifndef lint
39 static const char rcsid[] _U_ =
40 "@(#)$Header: /tcpdump/master/tcpdump/print-fr.c,v 1.18 2003-11-15 00:39:22 guy Exp $ (LBL)";
41 #endif
42 static void lmi_print(const u_char *, u_int);
43
44 #define NLPID_LMI 0x08 /* ANSI T1.617 Annex D or ITU-T Q.933 Annex A */
45 #define NLPID_CISCO_LMI 0x09 /* The original, aka Cisco, aka Gang of Four */
46 #define NLPID_SNAP 0x80
47 #define NLPID_CLNP 0x81
48 #define NLPID_ESIS 0x82
49 #define NLPID_ISIS 0x83
50 #define NLPID_CONS 0x84
51 #define NLPID_IDRP 0x85
52 #define NLPID_X25_ESIS 0x8a
53 #define NLPID_IPV6 0x8e
54 #define NLPID_IP 0xcc
55
56 #define FR_EA_BIT 0x01
57
58
59 /* Finds out Q.922 address length, DLCI and flags. Returns 0 on success */
60 static int parse_q922_addr(const u_char *p, u_int *dlci, u_int *addr_len,
61 char **flags_ptr)
62 {
63 static char flags[32];
64 size_t len;
65
66 if ((p[0] & FR_EA_BIT))
67 return -1;
68
69 *flags_ptr = flags;
70 *addr_len = 2;
71 *dlci = ((p[0] & 0xFC) << 2) | ((p[1] & 0xF0) >> 4);
72
73 strcpy(flags, (p[0] & 0x02) ? "C!, " : "");
74 if (p[1] & 0x08)
75 strcat(flags, "FECN, ");
76 if (p[1] & 0x04)
77 strcat(flags, "BECN, ");
78 if (p[1] & 0x02)
79 strcat(flags, "DE, ");
80
81 len = strlen(flags);
82 if (len > 1)
83 flags[len - 2] = '\x0'; /* delete trailing comma and space */
84
85 if (p[1] & FR_EA_BIT)
86 return 0; /* 2-byte Q.922 address */
87
88 p += 2;
89 (*addr_len)++; /* 3- or 4-byte Q.922 address */
90 if ((p[0] & FR_EA_BIT) == 0) {
91 *dlci = (*dlci << 7) | (p[0] >> 1);
92 (*addr_len)++; /* 4-byte Q.922 address */
93 p++;
94 }
95
96 if ((p[0] & FR_EA_BIT) == 0)
97 return -1; /* more than 4 bytes of Q.922 address? */
98
99 if (p[0] & 0x02) {
100 len = strlen(flags);
101 snprintf(flags + len, sizeof(flags) - len,
102 "%sdlcore %x", len ? ", " : "", p[0] >> 2);
103 } else
104 *dlci = (*dlci << 6) | (p[0] >> 2);
105
106 return 0;
107 }
108
109
110 static const char *fr_nlpids[256];
111
112 static void
113 init_fr_nlpids(void)
114 {
115 int i;
116 static int fr_nlpid_flag = 0;
117
118 if (!fr_nlpid_flag) {
119 for (i=0; i < 256; i++)
120 fr_nlpids[i] = NULL;
121 fr_nlpids[NLPID_LMI] = "LMI";
122 fr_nlpids[NLPID_CISCO_LMI] = "Cisco LMI";
123 fr_nlpids[NLPID_SNAP] = "SNAP";
124 fr_nlpids[NLPID_CLNP] = "CLNP";
125 fr_nlpids[NLPID_ESIS] = "ESIS";
126 fr_nlpids[NLPID_ISIS] = "ISIS";
127 fr_nlpids[NLPID_CONS] = "CONS";
128 fr_nlpids[NLPID_IDRP] = "IDRP";
129 fr_nlpids[NLPID_X25_ESIS] = "X25_ESIS";
130 fr_nlpids[NLPID_IP] = "IP";
131 }
132 fr_nlpid_flag = 1;
133 }
134
135 /* Frame Relay packet structure, with flags and CRC removed
136
137 +---------------------------+
138 | Q.922 Address* |
139 +-- --+
140 | |
141 +---------------------------+
142 | Control (UI = 0x03) |
143 +---------------------------+
144 | Optional Pad (0x00) |
145 +---------------------------+
146 | NLPID |
147 +---------------------------+
148 | . |
149 | . |
150 | . |
151 | Data |
152 | . |
153 | . |
154 +---------------------------+
155
156 * Q.922 addresses, as presently defined, are two octets and
157 contain a 10-bit DLCI. In some networks Q.922 addresses
158 may optionally be increased to three or four octets.
159 */
160
161 static u_int
162 fr_hdrlen(const u_char *p, u_int addr_len, u_int caplen)
163 {
164 if ((caplen > addr_len + 1 /* UI */ + 1 /* pad */) &&
165 !p[addr_len + 1] /* pad exist */)
166 return addr_len + 1 /* UI */ + 1 /* pad */ + 1 /* NLPID */;
167 else
168 return addr_len + 1 /* UI */ + 1 /* NLPID */;
169 }
170
171 static const char *
172 fr_protostring(u_int8_t proto)
173 {
174 static char buf[5+1+2+1];
175
176 init_fr_nlpids();
177
178 if (nflag || fr_nlpids[proto] == NULL) {
179 snprintf(buf, sizeof(buf), "proto %02x", proto);
180 return buf;
181 }
182 return fr_nlpids[proto];
183 }
184
185 static void
186 fr_hdr_print(int length, u_int dlci, char *flags, u_char nlpid)
187 {
188 if (qflag)
189 (void)printf("DLCI %u, %s%slength %d: ",
190 dlci, flags, *flags ? ", " : "", length);
191 else
192 (void)printf("DLCI %u, %s%s%s, length %d: ",
193 dlci, flags, *flags ? ", " : "",
194 fr_protostring(nlpid), length);
195 }
196
197 u_int
198 fr_if_print(const struct pcap_pkthdr *h, register const u_char *p)
199 {
200 register u_int length = h->len;
201 register u_int caplen = h->caplen;
202 u_short extracted_ethertype;
203 u_int32_t orgcode;
204 register u_short et;
205 int dlci, addr_len;
206 u_char nlpid;
207 u_int hdr_len;
208 char *flags;
209
210 if (caplen < 4) { /* minimum frame header length */
211 printf("[|fr]");
212 return caplen;
213 }
214
215 if (parse_q922_addr(p, &dlci, &addr_len, &flags)) {
216 printf("Invalid Q.922 address");
217 return caplen;
218 }
219
220 hdr_len = fr_hdrlen(p, addr_len, caplen);
221
222 if (caplen < hdr_len) {
223 printf("[|fr]");
224 return caplen;
225 }
226
227 if (p[addr_len] != 0x03)
228 printf("UI %02x! ", p[addr_len]);
229
230 if (!p[addr_len + 1]) { /* pad byte should be used with 3-byte Q.922 */
231 if (addr_len != 3)
232 printf("Pad! ");
233 } else if (addr_len == 3)
234 printf("No pad! ");
235
236 nlpid = p[hdr_len - 1];
237
238 p += hdr_len;
239 length -= hdr_len;
240 caplen -= hdr_len;
241
242 if (eflag)
243 fr_hdr_print(length, dlci, flags, nlpid);
244
245 switch (nlpid) {
246 case NLPID_IP:
247 ip_print(p, length);
248 break;
249
250 #ifdef INET6
251 case NLPID_IPV6:
252 ip6_print(p, length);
253 break;
254 #endif
255 case NLPID_CLNP:
256 case NLPID_ESIS:
257 case NLPID_ISIS:
258 isoclns_print(p, length, caplen);
259 break;
260
261 case NLPID_SNAP:
262 orgcode = EXTRACT_24BITS(p);
263 et = EXTRACT_16BITS(p + 3);
264 if (snap_print((const u_char *)(p + 5), length - 5,
265 caplen - 5, &extracted_ethertype, orgcode, et,
266 0) == 0) {
267 /* ether_type not known, print raw packet */
268 if (!eflag)
269 fr_hdr_print(length + hdr_len,
270 dlci, flags, nlpid);
271 if (extracted_ethertype) {
272 printf("(SNAP %s) ",
273 etherproto_string(htons(extracted_ethertype)));
274 }
275 if (!xflag && !qflag)
276 default_print(p - hdr_len, caplen + hdr_len);
277 }
278 break;
279
280 case NLPID_LMI:
281 lmi_print(p, length);
282 break;
283
284 default:
285 if (!eflag)
286 fr_hdr_print(length + hdr_len,
287 dlci, flags, nlpid);
288 if (!xflag)
289 default_print(p, caplen);
290 }
291
292 return hdr_len;
293 }
294
295 /*
296 * Q.933 decoding portion for framerelay specific.
297 */
298
299 /* Q.933 packet format
300 Format of Other Protocols
301 using Q.933 NLPID
302 +-------------------------------+
303 | Q.922 Address |
304 +---------------+---------------+
305 |Control 0x03 | NLPID 0x08 |
306 +---------------+---------------+
307 | L2 Protocol ID |
308 | octet 1 | octet 2 |
309 +-------------------------------+
310 | L3 Protocol ID |
311 | octet 2 | octet 2 |
312 +-------------------------------+
313 | Protocol Data |
314 +-------------------------------+
315 | FCS |
316 +-------------------------------+
317 */
318
319 /* L2 (Octet 1)- Call Reference Usually is 0x0 */
320
321 /*
322 * L2 (Octet 2)- Message Types definition 1 byte long.
323 */
324 /* Call Establish */
325 #define MSG_TYPE_ESC_TO_NATIONAL 0x00
326 #define MSG_TYPE_ALERT 0x01
327 #define MSG_TYPE_CALL_PROCEEDING 0x02
328 #define MSG_TYPE_CONNECT 0x07
329 #define MSG_TYPE_CONNECT_ACK 0x0F
330 #define MSG_TYPE_PROGRESS 0x03
331 #define MSG_TYPE_SETUP 0x05
332 /* Call Clear */
333 #define MSG_TYPE_DISCONNECT 0x45
334 #define MSG_TYPE_RELEASE 0x4D
335 #define MSG_TYPE_RELEASE_COMPLETE 0x5A
336 #define MSG_TYPE_RESTART 0x46
337 #define MSG_TYPE_RESTART_ACK 0x4E
338 /* Status */
339 #define MSG_TYPE_STATUS 0x7D
340 #define MSG_TYPE_STATUS_ENQ 0x75
341
342 #define MSG_ANSI_LOCKING_SHIFT 0x95
343 #define ONE_BYTE_IE_MASK 0xF0 /* details? */
344
345 #define ANSI_REPORT_TYPE_IE 0x01
346 #define ANSI_LINK_VERIFY_IE_91 0x19 /* details? */
347 #define ANSI_LINK_VERIFY_IE 0x03
348 #define ANSI_PVC_STATUS_IE 0x07
349
350 #define CCITT_REPORT_TYPE_IE 0x51
351 #define CCITT_LINK_VERIFY_IE 0x53
352 #define CCITT_PVC_STATUS_IE 0x57
353
354 struct common_ie_header {
355 u_int8_t ie_id;
356 u_int8_t ie_len;
357 };
358
359 #define FULL_STATUS 0
360 #define LINK_VERIFY 1
361 #define ASYNC_PVC 2
362
363
364 /* Parses DLCI information element. */
365 static const char * parse_dlci_ie(const u_char *p, u_int ie_len, char *buffer,
366 size_t buffer_len)
367 {
368 u_int dlci;
369
370 if ((ie_len < 3) ||
371 (p[0] & 0x80) ||
372 ((ie_len == 3) && !(p[1] & 0x80)) ||
373 ((ie_len == 4) && ((p[1] & 0x80) || !(p[2] & 0x80))) ||
374 ((ie_len == 5) && ((p[1] & 0x80) || (p[2] & 0x80) ||
375 !(p[3] & 0x80))) ||
376 (ie_len > 5) ||
377 !(p[ie_len - 1] & 0x80))
378 return "Invalid DLCI IE";
379
380 dlci = ((p[0] & 0x3F) << 4) | ((p[1] & 0x78) >> 3);
381 if (ie_len == 4)
382 dlci = (dlci << 6) | ((p[2] & 0x7E) >> 1);
383 else if (ie_len == 5)
384 dlci = (dlci << 13) | (p[2] & 0x7F) | ((p[3] & 0x7E) >> 1);
385
386 snprintf(buffer, buffer_len, "DLCI %d: status %s%s", dlci,
387 p[ie_len - 1] & 0x8 ? "New, " : "",
388 p[ie_len - 1] & 0x2 ? "Active" : "Inactive");
389
390 return buffer;
391 }
392
393
394 static void
395 lmi_print(const u_char *p, u_int length)
396 {
397 const u_char *ptemp = p;
398 const char *decode_str;
399 char temp_str[255];
400 struct common_ie_header *ie_p;
401 int is_ansi = 0;
402
403 if (length < 9) { /* shortest: Q.933a LINK VERIFY */
404 printf("[|lmi]");
405 return;
406 }
407
408 if (p[2] == MSG_ANSI_LOCKING_SHIFT)
409 is_ansi = 1;
410
411 /* printing out header part */
412 printf(is_ansi ? "ANSI" : "CCITT");
413 if (p[0])
414 printf(" Call Ref: %02x!", p[0]);
415
416 switch(p[1]) {
417
418 case MSG_TYPE_STATUS:
419 printf(" STATUS REPLY\n");
420 break;
421
422 case MSG_TYPE_STATUS_ENQ:
423 printf(" STATUS ENQUIRY\n");
424 break;
425
426 default:
427 printf(" UNKNOWN MSG Type %02x\n", p[1]);
428 break;
429 }
430
431 if (length < (u_int)(2 - is_ansi)) {
432 printf("[|lmi]");
433 return;
434 }
435 length -= 2 - is_ansi;
436 ptemp += 2 + is_ansi;
437
438 /* Loop through the rest of IE */
439 while (length > 0) {
440 ie_p = (struct common_ie_header *)ptemp;
441 if (length < sizeof(struct common_ie_header) ||
442 length < sizeof(struct common_ie_header) + ie_p->ie_len) {
443 printf("[|lmi]");
444 return;
445 }
446
447 if ((is_ansi && ie_p->ie_id == ANSI_REPORT_TYPE_IE) ||
448 (!is_ansi && ie_p->ie_id == CCITT_REPORT_TYPE_IE)) {
449 switch(ptemp[2]) {
450
451 case FULL_STATUS:
452 decode_str = "FULL STATUS";
453 break;
454
455 case LINK_VERIFY:
456 decode_str = "LINK VERIFY";
457 break;
458
459 case ASYNC_PVC:
460 decode_str = "Async PVC Status";
461 break;
462
463 default:
464 decode_str = "Reserved Value";
465 break;
466 }
467 } else if ((is_ansi && (ie_p->ie_id == ANSI_LINK_VERIFY_IE_91 ||
468 ie_p->ie_id == ANSI_LINK_VERIFY_IE)) ||
469 (!is_ansi && ie_p->ie_id == CCITT_LINK_VERIFY_IE)) {
470 snprintf(temp_str, sizeof(temp_str),
471 "TX Seq: %3d, RX Seq: %3d",
472 ptemp[2], ptemp[3]);
473 decode_str = temp_str;
474 } else if ((is_ansi && ie_p->ie_id == ANSI_PVC_STATUS_IE) ||
475 (!is_ansi && ie_p->ie_id == CCITT_PVC_STATUS_IE)) {
476 decode_str = parse_dlci_ie(ptemp + 2, ie_p->ie_len,
477 temp_str, sizeof(temp_str));
478 } else
479 decode_str = "Non-decoded Value";
480
481 printf("\t\tIE: %02X Len: %d, %s\n",
482 ie_p->ie_id, ie_p->ie_len, decode_str);
483 length = length - ie_p->ie_len - 2;
484 ptemp = ptemp + ie_p->ie_len + 2;
485 }
486 }