]> The Tcpdump Group git mirrors - tcpdump/blob - print-fr.c
print frame-relay flags & DLCI # for cisco style encaps
[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.24 2004-10-07 16:16:02 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("DLCI %u, %s%scisco-ethertype %s (0x%04x), length %u: ",
186 dlci,
187 flags,
188 *flags ? ", " : "",
189 tok2str(ethertype_values, "unknown", extracted_ethertype),
190 extracted_ethertype,
191 length);
192
193 if (ether_encap_print(extracted_ethertype,
194 p+addr_len+ETHERTYPE_LEN,
195 length-(addr_len),
196 caplen-(addr_len),
197 &extracted_ethertype) == 0)
198 /* ether_type not known, probably it wasn't one */
199 printf("UI %02x! ", p[addr_len]);
200 else
201 return hdr_len;
202 }
203
204 if (!p[addr_len + 1]) { /* pad byte should be used with 3-byte Q.922 */
205 if (addr_len != 3)
206 printf("Pad! ");
207 } else if (addr_len == 3)
208 printf("No pad! ");
209
210 nlpid = p[hdr_len - 1];
211
212 p += hdr_len;
213 length -= hdr_len;
214 caplen -= hdr_len;
215
216 if (eflag)
217 fr_hdr_print(length, dlci, flags, nlpid);
218
219 switch (nlpid) {
220 case NLPID_IP:
221 ip_print(p, length);
222 break;
223
224 #ifdef INET6
225 case NLPID_IP6:
226 ip6_print(p, length);
227 break;
228 #endif
229 case NLPID_CLNP:
230 case NLPID_ESIS:
231 case NLPID_ISIS:
232 isoclns_print(p, length, caplen);
233 break;
234
235 case NLPID_SNAP:
236 orgcode = EXTRACT_24BITS(p);
237 et = EXTRACT_16BITS(p + 3);
238 if (snap_print((const u_char *)(p + 5), length - 5,
239 caplen - 5, &extracted_ethertype, orgcode, et,
240 0) == 0) {
241 /* ether_type not known, print raw packet */
242 if (!eflag)
243 fr_hdr_print(length + hdr_len,
244 dlci, flags, nlpid);
245 if (extracted_ethertype) {
246 printf("(SNAP %s) ",
247 etherproto_string(htons(extracted_ethertype)));
248 }
249 if (!xflag && !qflag)
250 default_print(p - hdr_len, caplen + hdr_len);
251 }
252 break;
253
254 case NLPID_LMI:
255 lmi_print(p, length);
256 break;
257
258 default:
259 if (!eflag)
260 fr_hdr_print(length + hdr_len,
261 dlci, flags, nlpid);
262 if (!xflag)
263 default_print(p, caplen);
264 }
265
266 return hdr_len;
267 }
268
269 /*
270 * Q.933 decoding portion for framerelay specific.
271 */
272
273 /* Q.933 packet format
274 Format of Other Protocols
275 using Q.933 NLPID
276 +-------------------------------+
277 | Q.922 Address |
278 +---------------+---------------+
279 |Control 0x03 | NLPID 0x08 |
280 +---------------+---------------+
281 | L2 Protocol ID |
282 | octet 1 | octet 2 |
283 +-------------------------------+
284 | L3 Protocol ID |
285 | octet 2 | octet 2 |
286 +-------------------------------+
287 | Protocol Data |
288 +-------------------------------+
289 | FCS |
290 +-------------------------------+
291 */
292
293 /* L2 (Octet 1)- Call Reference Usually is 0x0 */
294
295 /*
296 * L2 (Octet 2)- Message Types definition 1 byte long.
297 */
298 /* Call Establish */
299 #define MSG_TYPE_ESC_TO_NATIONAL 0x00
300 #define MSG_TYPE_ALERT 0x01
301 #define MSG_TYPE_CALL_PROCEEDING 0x02
302 #define MSG_TYPE_CONNECT 0x07
303 #define MSG_TYPE_CONNECT_ACK 0x0F
304 #define MSG_TYPE_PROGRESS 0x03
305 #define MSG_TYPE_SETUP 0x05
306 /* Call Clear */
307 #define MSG_TYPE_DISCONNECT 0x45
308 #define MSG_TYPE_RELEASE 0x4D
309 #define MSG_TYPE_RELEASE_COMPLETE 0x5A
310 #define MSG_TYPE_RESTART 0x46
311 #define MSG_TYPE_RESTART_ACK 0x4E
312 /* Status */
313 #define MSG_TYPE_STATUS 0x7D
314 #define MSG_TYPE_STATUS_ENQ 0x75
315
316 #define MSG_ANSI_LOCKING_SHIFT 0x95
317 #define ONE_BYTE_IE_MASK 0xF0 /* details? */
318
319 #define ANSI_REPORT_TYPE_IE 0x01
320 #define ANSI_LINK_VERIFY_IE_91 0x19 /* details? */
321 #define ANSI_LINK_VERIFY_IE 0x03
322 #define ANSI_PVC_STATUS_IE 0x07
323
324 #define CCITT_REPORT_TYPE_IE 0x51
325 #define CCITT_LINK_VERIFY_IE 0x53
326 #define CCITT_PVC_STATUS_IE 0x57
327
328 struct common_ie_header {
329 u_int8_t ie_id;
330 u_int8_t ie_len;
331 };
332
333 #define FULL_STATUS 0
334 #define LINK_VERIFY 1
335 #define ASYNC_PVC 2
336
337
338 /* Parses DLCI information element. */
339 static const char * parse_dlci_ie(const u_char *p, u_int ie_len, char *buffer,
340 size_t buffer_len)
341 {
342 u_int dlci;
343
344 if ((ie_len < 3) ||
345 (p[0] & 0x80) ||
346 ((ie_len == 3) && !(p[1] & 0x80)) ||
347 ((ie_len == 4) && ((p[1] & 0x80) || !(p[2] & 0x80))) ||
348 ((ie_len == 5) && ((p[1] & 0x80) || (p[2] & 0x80) ||
349 !(p[3] & 0x80))) ||
350 (ie_len > 5) ||
351 !(p[ie_len - 1] & 0x80))
352 return "Invalid DLCI IE";
353
354 dlci = ((p[0] & 0x3F) << 4) | ((p[1] & 0x78) >> 3);
355 if (ie_len == 4)
356 dlci = (dlci << 6) | ((p[2] & 0x7E) >> 1);
357 else if (ie_len == 5)
358 dlci = (dlci << 13) | (p[2] & 0x7F) | ((p[3] & 0x7E) >> 1);
359
360 snprintf(buffer, buffer_len, "DLCI %d: status %s%s", dlci,
361 p[ie_len - 1] & 0x8 ? "New, " : "",
362 p[ie_len - 1] & 0x2 ? "Active" : "Inactive");
363
364 return buffer;
365 }
366
367
368 static void
369 lmi_print(const u_char *p, u_int length)
370 {
371 const u_char *ptemp = p;
372 const char *decode_str;
373 char temp_str[255];
374 struct common_ie_header *ie_p;
375 int is_ansi = 0;
376
377 if (length < 9) { /* shortest: Q.933a LINK VERIFY */
378 printf("[|lmi]");
379 return;
380 }
381
382 if (p[2] == MSG_ANSI_LOCKING_SHIFT)
383 is_ansi = 1;
384
385 /* printing out header part */
386 printf(is_ansi ? "ANSI" : "CCITT");
387 if (p[0])
388 printf(" Call Ref: %02x!", p[0]);
389
390 switch(p[1]) {
391
392 case MSG_TYPE_STATUS:
393 printf(" STATUS REPLY\n");
394 break;
395
396 case MSG_TYPE_STATUS_ENQ:
397 printf(" STATUS ENQUIRY\n");
398 break;
399
400 default:
401 printf(" UNKNOWN MSG Type %02x\n", p[1]);
402 break;
403 }
404
405 if (length < (u_int)(2 - is_ansi)) {
406 printf("[|lmi]");
407 return;
408 }
409 length -= 2 - is_ansi;
410 ptemp += 2 + is_ansi;
411
412 /* Loop through the rest of IE */
413 while (length > 0) {
414 ie_p = (struct common_ie_header *)ptemp;
415 if (length < sizeof(struct common_ie_header) ||
416 length < sizeof(struct common_ie_header) + ie_p->ie_len) {
417 printf("[|lmi]");
418 return;
419 }
420
421 if ((is_ansi && ie_p->ie_id == ANSI_REPORT_TYPE_IE) ||
422 (!is_ansi && ie_p->ie_id == CCITT_REPORT_TYPE_IE)) {
423 switch(ptemp[2]) {
424
425 case FULL_STATUS:
426 decode_str = "FULL STATUS";
427 break;
428
429 case LINK_VERIFY:
430 decode_str = "LINK VERIFY";
431 break;
432
433 case ASYNC_PVC:
434 decode_str = "Async PVC Status";
435 break;
436
437 default:
438 decode_str = "Reserved Value";
439 break;
440 }
441 } else if ((is_ansi && (ie_p->ie_id == ANSI_LINK_VERIFY_IE_91 ||
442 ie_p->ie_id == ANSI_LINK_VERIFY_IE)) ||
443 (!is_ansi && ie_p->ie_id == CCITT_LINK_VERIFY_IE)) {
444 snprintf(temp_str, sizeof(temp_str),
445 "TX Seq: %3d, RX Seq: %3d",
446 ptemp[2], ptemp[3]);
447 decode_str = temp_str;
448 } else if ((is_ansi && ie_p->ie_id == ANSI_PVC_STATUS_IE) ||
449 (!is_ansi && ie_p->ie_id == CCITT_PVC_STATUS_IE)) {
450 decode_str = parse_dlci_ie(ptemp + 2, ie_p->ie_len,
451 temp_str, sizeof(temp_str));
452 } else
453 decode_str = "Non-decoded Value";
454
455 printf("\t\tIE: %02X Len: %d, %s\n",
456 ie_p->ie_id, ie_p->ie_len, decode_str);
457 length = length - ie_p->ie_len - 2;
458 ptemp = ptemp + ie_p->ie_len + 2;
459 }
460 }