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