Upgrade to tcpdump-4.0.0.
[dragonfly.git] / contrib / tcpdump / print-fr.c
... / ...
CommitLineData
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
23static const char rcsid[] _U_ =
24 "@(#)$Header: /tcpdump/master/tcpdump/print-fr.c,v 1.51 2006-06-23 22:20:32 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#include "oui.h"
43
44static void frf15_print(const u_char *, u_int);
45
46/*
47 * the frame relay header has a variable length
48 *
49 * the EA bit determines if there is another byte
50 * in the header
51 *
52 * minimum header length is 2 bytes
53 * maximum header length is 4 bytes
54 *
55 * 7 6 5 4 3 2 1 0
56 * +----+----+----+----+----+----+----+----+
57 * | DLCI (6 bits) | CR | EA |
58 * +----+----+----+----+----+----+----+----+
59 * | DLCI (4 bits) |FECN|BECN| DE | EA |
60 * +----+----+----+----+----+----+----+----+
61 * | DLCI (7 bits) | EA |
62 * +----+----+----+----+----+----+----+----+
63 * | DLCI (6 bits) |SDLC| EA |
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
76struct 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/* FRF.15 / FRF.16 */
86#define MFR_B_BIT 0x80
87#define MFR_E_BIT 0x40
88#define MFR_C_BIT 0x20
89#define MFR_BEC_MASK (MFR_B_BIT | MFR_E_BIT | MFR_C_BIT)
90#define MFR_CTRL_FRAME (MFR_B_BIT | MFR_E_BIT | MFR_C_BIT)
91#define MFR_FRAG_FRAME (MFR_B_BIT | MFR_E_BIT )
92
93struct tok frf_flag_values[] = {
94 { MFR_B_BIT, "Begin" },
95 { MFR_E_BIT, "End" },
96 { MFR_C_BIT, "Control" },
97 { 0, NULL }
98};
99
100/* Finds out Q.922 address length, DLCI and flags. Returns 0 on success
101 * save the flags dep. on address length
102 */
103static int parse_q922_addr(const u_char *p, u_int *dlci,
104 u_int *addr_len, u_int8_t *flags)
105{
106 if ((p[0] & FR_EA_BIT))
107 return -1;
108
109 *addr_len = 2;
110 *dlci = ((p[0] & 0xFC) << 2) | ((p[1] & 0xF0) >> 4);
111
112 flags[0] = p[0] & 0x02; /* populate the first flag fields */
113 flags[1] = p[1] & 0x0c;
114 flags[2] = 0; /* clear the rest of the flags */
115 flags[3] = 0;
116
117 if (p[1] & FR_EA_BIT)
118 return 0; /* 2-byte Q.922 address */
119
120 p += 2;
121 (*addr_len)++; /* 3- or 4-byte Q.922 address */
122 if ((p[0] & FR_EA_BIT) == 0) {
123 *dlci = (*dlci << 7) | (p[0] >> 1);
124 (*addr_len)++; /* 4-byte Q.922 address */
125 p++;
126 }
127
128 if ((p[0] & FR_EA_BIT) == 0)
129 return -1; /* more than 4 bytes of Q.922 address? */
130
131 flags[3] = p[0] & 0x02;
132
133 *dlci = (*dlci << 6) | (p[0] >> 2);
134
135 return 0;
136}
137
138char *q922_string(const u_char *p) {
139
140 static u_int dlci, addr_len;
141 static u_int8_t flags[4];
142 static char buffer[sizeof("DLCI xxxxxxxxxx")];
143 memset(buffer, 0, sizeof(buffer));
144
145 if (parse_q922_addr(p, &dlci, &addr_len, flags) == 0){
146 snprintf(buffer, sizeof(buffer), "DLCI %u", dlci);
147 }
148
149 return buffer;
150}
151
152
153/* Frame Relay packet structure, with flags and CRC removed
154
155 +---------------------------+
156 | Q.922 Address* |
157 +-- --+
158 | |
159 +---------------------------+
160 | Control (UI = 0x03) |
161 +---------------------------+
162 | Optional Pad (0x00) |
163 +---------------------------+
164 | NLPID |
165 +---------------------------+
166 | . |
167 | . |
168 | . |
169 | Data |
170 | . |
171 | . |
172 +---------------------------+
173
174 * Q.922 addresses, as presently defined, are two octets and
175 contain a 10-bit DLCI. In some networks Q.922 addresses
176 may optionally be increased to three or four octets.
177*/
178
179static u_int
180fr_hdrlen(const u_char *p, u_int addr_len)
181{
182 if (!p[addr_len + 1] /* pad exist */)
183 return addr_len + 1 /* UI */ + 1 /* pad */ + 1 /* NLPID */;
184 else
185 return addr_len + 1 /* UI */ + 1 /* NLPID */;
186}
187
188static void
189fr_hdr_print(int length, u_int addr_len, u_int dlci, u_int8_t *flags, u_int16_t nlpid)
190{
191 if (qflag) {
192 (void)printf("Q.922, DLCI %u, length %u: ",
193 dlci,
194 length);
195 } else {
196 if (nlpid <= 0xff) /* if its smaller than 256 then its a NLPID */
197 (void)printf("Q.922, hdr-len %u, DLCI %u, Flags [%s], NLPID %s (0x%02x), length %u: ",
198 addr_len,
199 dlci,
200 bittok2str(fr_header_flag_values, "none", EXTRACT_32BITS(flags)),
201 tok2str(nlpid_values,"unknown", nlpid),
202 nlpid,
203 length);
204 else /* must be an ethertype */
205 (void)printf("Q.922, hdr-len %u, DLCI %u, Flags [%s], cisco-ethertype %s (0x%04x), length %u: ",
206 addr_len,
207 dlci,
208 bittok2str(fr_header_flag_values, "none", EXTRACT_32BITS(flags)),
209 tok2str(ethertype_values, "unknown", nlpid),
210 nlpid,
211 length);
212 }
213}
214
215u_int
216fr_if_print(const struct pcap_pkthdr *h, register const u_char *p)
217{
218 register u_int length = h->len;
219 register u_int caplen = h->caplen;
220
221 TCHECK2(*p, 4); /* minimum frame header length */
222
223 if ((length = fr_print(p, length)) == 0)
224 return (0);
225 else
226 return length;
227 trunc:
228 printf("[|fr]");
229 return caplen;
230}
231
232u_int
233fr_print(register const u_char *p, u_int length)
234{
235 u_int16_t extracted_ethertype;
236 u_int dlci;
237 u_int addr_len;
238 u_int16_t nlpid;
239 u_int hdr_len;
240 u_int8_t flags[4];
241
242 if (parse_q922_addr(p, &dlci, &addr_len, flags)) {
243 printf("Q.922, invalid address");
244 return 0;
245 }
246
247 TCHECK2(*p,addr_len+1+1);
248 hdr_len = fr_hdrlen(p, addr_len);
249 TCHECK2(*p,hdr_len);
250
251 if (p[addr_len] != 0x03 && dlci != 0) {
252
253 /* lets figure out if we have cisco style encapsulation: */
254 extracted_ethertype = EXTRACT_16BITS(p+addr_len);
255
256 if (eflag)
257 fr_hdr_print(length, addr_len, dlci, flags, extracted_ethertype);
258
259 if (ether_encap_print(extracted_ethertype,
260 p+addr_len+ETHERTYPE_LEN,
261 length-addr_len-ETHERTYPE_LEN,
262 length-addr_len-ETHERTYPE_LEN,
263 &extracted_ethertype) == 0)
264 /* ether_type not known, probably it wasn't one */
265 printf("UI %02x! ", p[addr_len]);
266 else
267 return hdr_len;
268 }
269
270 if (!p[addr_len + 1]) { /* pad byte should be used with 3-byte Q.922 */
271 if (addr_len != 3)
272 printf("Pad! ");
273 } else if (addr_len == 3)
274 printf("No pad! ");
275
276 nlpid = p[hdr_len - 1];
277
278 if (eflag)
279 fr_hdr_print(length, addr_len, dlci, flags, nlpid);
280 p += hdr_len;
281 length -= hdr_len;
282
283 switch (nlpid) {
284 case NLPID_IP:
285 ip_print(gndo, p, length);
286 break;
287
288#ifdef INET6
289 case NLPID_IP6:
290 ip6_print(p, length);
291 break;
292#endif
293 case NLPID_CLNP:
294 case NLPID_ESIS:
295 case NLPID_ISIS:
296 isoclns_print(p-1, length+1, length+1); /* OSI printers need the NLPID field */
297 break;
298
299 case NLPID_SNAP:
300 if (snap_print(p, length, length, &extracted_ethertype, 0) == 0) {
301 /* ether_type not known, print raw packet */
302 if (!eflag)
303 fr_hdr_print(length + hdr_len, hdr_len,
304 dlci, flags, nlpid);
305 if (!suppress_default_print)
306 default_print(p - hdr_len, length + hdr_len);
307 }
308 break;
309
310 case NLPID_Q933:
311 q933_print(p, length);
312 break;
313
314 case NLPID_MFR:
315 frf15_print(p, length);
316 break;
317
318 case NLPID_PPP:
319 ppp_print(p, length);
320 break;
321
322 default:
323 if (!eflag)
324 fr_hdr_print(length + hdr_len, addr_len,
325 dlci, flags, nlpid);
326 if (!xflag)
327 default_print(p, length);
328 }
329
330 return hdr_len;
331
332 trunc:
333 printf("[|fr]");
334 return 0;
335
336}
337
338u_int
339mfr_if_print(const struct pcap_pkthdr *h, register const u_char *p)
340{
341 register u_int length = h->len;
342 register u_int caplen = h->caplen;
343
344 TCHECK2(*p, 2); /* minimum frame header length */
345
346 if ((length = mfr_print(p, length)) == 0)
347 return (0);
348 else
349 return length;
350 trunc:
351 printf("[|mfr]");
352 return caplen;
353}
354
355
356#define MFR_CTRL_MSG_ADD_LINK 1
357#define MFR_CTRL_MSG_ADD_LINK_ACK 2
358#define MFR_CTRL_MSG_ADD_LINK_REJ 3
359#define MFR_CTRL_MSG_HELLO 4
360#define MFR_CTRL_MSG_HELLO_ACK 5
361#define MFR_CTRL_MSG_REMOVE_LINK 6
362#define MFR_CTRL_MSG_REMOVE_LINK_ACK 7
363
364struct tok mfr_ctrl_msg_values[] = {
365 { MFR_CTRL_MSG_ADD_LINK, "Add Link" },
366 { MFR_CTRL_MSG_ADD_LINK_ACK, "Add Link ACK" },
367 { MFR_CTRL_MSG_ADD_LINK_REJ, "Add Link Reject" },
368 { MFR_CTRL_MSG_HELLO, "Hello" },
369 { MFR_CTRL_MSG_HELLO_ACK, "Hello ACK" },
370 { MFR_CTRL_MSG_REMOVE_LINK, "Remove Link" },
371 { MFR_CTRL_MSG_REMOVE_LINK_ACK, "Remove Link ACK" },
372 { 0, NULL }
373};
374
375#define MFR_CTRL_IE_BUNDLE_ID 1
376#define MFR_CTRL_IE_LINK_ID 2
377#define MFR_CTRL_IE_MAGIC_NUM 3
378#define MFR_CTRL_IE_TIMESTAMP 5
379#define MFR_CTRL_IE_VENDOR_EXT 6
380#define MFR_CTRL_IE_CAUSE 7
381
382struct tok mfr_ctrl_ie_values[] = {
383 { MFR_CTRL_IE_BUNDLE_ID, "Bundle ID"},
384 { MFR_CTRL_IE_LINK_ID, "Link ID"},
385 { MFR_CTRL_IE_MAGIC_NUM, "Magic Number"},
386 { MFR_CTRL_IE_TIMESTAMP, "Timestamp"},
387 { MFR_CTRL_IE_VENDOR_EXT, "Vendor Extension"},
388 { MFR_CTRL_IE_CAUSE, "Cause"},
389 { 0, NULL }
390};
391
392#define MFR_ID_STRING_MAXLEN 50
393
394struct ie_tlv_header_t {
395 u_int8_t ie_type;
396 u_int8_t ie_len;
397};
398
399u_int
400mfr_print(register const u_char *p, u_int length)
401{
402 u_int tlen,idx,hdr_len = 0;
403 u_int16_t sequence_num;
404 u_int8_t ie_type,ie_len;
405 const u_int8_t *tptr;
406
407
408/*
409 * FRF.16 Link Integrity Control Frame
410 *
411 * 7 6 5 4 3 2 1 0
412 * +----+----+----+----+----+----+----+----+
413 * | B | E | C=1| 0 0 0 0 | EA |
414 * +----+----+----+----+----+----+----+----+
415 * | 0 0 0 0 0 0 0 0 |
416 * +----+----+----+----+----+----+----+----+
417 * | message type |
418 * +----+----+----+----+----+----+----+----+
419 */
420
421 TCHECK2(*p, 4); /* minimum frame header length */
422
423 if ((p[0] & MFR_BEC_MASK) == MFR_CTRL_FRAME && p[1] == 0) {
424 printf("FRF.16 Control, Flags [%s], %s, length %u",
425 bittok2str(frf_flag_values,"none",(p[0] & MFR_BEC_MASK)),
426 tok2str(mfr_ctrl_msg_values,"Unknown Message (0x%02x)",p[2]),
427 length);
428 tptr = p + 3;
429 tlen = length -3;
430 hdr_len = 3;
431
432 if (!vflag)
433 return hdr_len;
434
435 while (tlen>sizeof(struct ie_tlv_header_t)) {
436 TCHECK2(*tptr, sizeof(struct ie_tlv_header_t));
437 ie_type=tptr[0];
438 ie_len=tptr[1];
439
440 printf("\n\tIE %s (%u), length %u: ",
441 tok2str(mfr_ctrl_ie_values,"Unknown",ie_type),
442 ie_type,
443 ie_len);
444
445 /* infinite loop check */
446 if (ie_type == 0 || ie_len <= sizeof(struct ie_tlv_header_t))
447 return hdr_len;
448
449 TCHECK2(*tptr,ie_len);
450 tptr+=sizeof(struct ie_tlv_header_t);
451 /* tlv len includes header */
452 ie_len-=sizeof(struct ie_tlv_header_t);
453 tlen-=sizeof(struct ie_tlv_header_t);
454
455 switch (ie_type) {
456
457 case MFR_CTRL_IE_MAGIC_NUM:
458 printf("0x%08x",EXTRACT_32BITS(tptr));
459 break;
460
461 case MFR_CTRL_IE_BUNDLE_ID: /* same message format */
462 case MFR_CTRL_IE_LINK_ID:
463 for (idx = 0; idx < ie_len && idx < MFR_ID_STRING_MAXLEN; idx++) {
464 if (*(tptr+idx) != 0) /* don't print null termination */
465 safeputchar(*(tptr+idx));
466 else
467 break;
468 }
469 break;
470
471 case MFR_CTRL_IE_TIMESTAMP:
472 if (ie_len == sizeof(struct timeval)) {
473 ts_print((const struct timeval *)tptr);
474 break;
475 }
476 /* fall through and hexdump if no unix timestamp */
477
478 /*
479 * FIXME those are the defined IEs that lack a decoder
480 * you are welcome to contribute code ;-)
481 */
482
483 case MFR_CTRL_IE_VENDOR_EXT:
484 case MFR_CTRL_IE_CAUSE:
485
486 default:
487 if (vflag <= 1)
488 print_unknown_data(tptr,"\n\t ",ie_len);
489 break;
490 }
491
492 /* do we want to see a hexdump of the IE ? */
493 if (vflag > 1 )
494 print_unknown_data(tptr,"\n\t ",ie_len);
495
496 tlen-=ie_len;
497 tptr+=ie_len;
498 }
499 return hdr_len;
500 }
501/*
502 * FRF.16 Fragmentation Frame
503 *
504 * 7 6 5 4 3 2 1 0
505 * +----+----+----+----+----+----+----+----+
506 * | B | E | C=0|seq. (high 4 bits) | EA |
507 * +----+----+----+----+----+----+----+----+
508 * | sequence (low 8 bits) |
509 * +----+----+----+----+----+----+----+----+
510 * | DLCI (6 bits) | CR | EA |
511 * +----+----+----+----+----+----+----+----+
512 * | DLCI (4 bits) |FECN|BECN| DE | EA |
513 * +----+----+----+----+----+----+----+----+
514 */
515
516 sequence_num = (p[0]&0x1e)<<7 | p[1];
517 /* whole packet or first fragment ? */
518 if ((p[0] & MFR_BEC_MASK) == MFR_FRAG_FRAME ||
519 (p[0] & MFR_BEC_MASK) == MFR_B_BIT) {
520 printf("FRF.16 Frag, seq %u, Flags [%s], ",
521 sequence_num,
522 bittok2str(frf_flag_values,"none",(p[0] & MFR_BEC_MASK)));
523 hdr_len = 2;
524 fr_print(p+hdr_len,length-hdr_len);
525 return hdr_len;
526 }
527
528 /* must be a middle or the last fragment */
529 printf("FRF.16 Frag, seq %u, Flags [%s]",
530 sequence_num,
531 bittok2str(frf_flag_values,"none",(p[0] & MFR_BEC_MASK)));
532 print_unknown_data(p,"\n\t",length);
533
534 return hdr_len;
535
536 trunc:
537 printf("[|mfr]");
538 return length;
539}
540
541/* an NLPID of 0xb1 indicates a 2-byte
542 * FRF.15 header
543 *
544 * 7 6 5 4 3 2 1 0
545 * +----+----+----+----+----+----+----+----+
546 * ~ Q.922 header ~
547 * +----+----+----+----+----+----+----+----+
548 * | NLPID (8 bits) | NLPID=0xb1
549 * +----+----+----+----+----+----+----+----+
550 * | B | E | C |seq. (high 4 bits) | R |
551 * +----+----+----+----+----+----+----+----+
552 * | sequence (low 8 bits) |
553 * +----+----+----+----+----+----+----+----+
554 */
555
556#define FR_FRF15_FRAGTYPE 0x01
557
558static void
559frf15_print (const u_char *p, u_int length) {
560
561 u_int16_t sequence_num, flags;
562
563 flags = p[0]&MFR_BEC_MASK;
564 sequence_num = (p[0]&0x1e)<<7 | p[1];
565
566 printf("FRF.15, seq 0x%03x, Flags [%s],%s Fragmentation, length %u",
567 sequence_num,
568 bittok2str(frf_flag_values,"none",flags),
569 p[0]&FR_FRF15_FRAGTYPE ? "Interface" : "End-to-End",
570 length);
571
572/* TODO:
573 * depending on all permutations of the B, E and C bit
574 * dig as deep as we can - e.g. on the first (B) fragment
575 * there is enough payload to print the IP header
576 * on non (B) fragments it depends if the fragmentation
577 * model is end-to-end or interface based wether we want to print
578 * another Q.922 header
579 */
580
581}
582
583/*
584 * Q.933 decoding portion for framerelay specific.
585 */
586
587/* Q.933 packet format
588 Format of Other Protocols
589 using Q.933 NLPID
590 +-------------------------------+
591 | Q.922 Address |
592 +---------------+---------------+
593 |Control 0x03 | NLPID 0x08 |
594 +---------------+---------------+
595 | L2 Protocol ID |
596 | octet 1 | octet 2 |
597 +-------------------------------+
598 | L3 Protocol ID |
599 | octet 2 | octet 2 |
600 +-------------------------------+
601 | Protocol Data |
602 +-------------------------------+
603 | FCS |
604 +-------------------------------+
605 */
606
607/* L2 (Octet 1)- Call Reference Usually is 0x0 */
608
609/*
610 * L2 (Octet 2)- Message Types definition 1 byte long.
611 */
612/* Call Establish */
613#define MSG_TYPE_ESC_TO_NATIONAL 0x00
614#define MSG_TYPE_ALERT 0x01
615#define MSG_TYPE_CALL_PROCEEDING 0x02
616#define MSG_TYPE_CONNECT 0x07
617#define MSG_TYPE_CONNECT_ACK 0x0F
618#define MSG_TYPE_PROGRESS 0x03
619#define MSG_TYPE_SETUP 0x05
620/* Call Clear */
621#define MSG_TYPE_DISCONNECT 0x45
622#define MSG_TYPE_RELEASE 0x4D
623#define MSG_TYPE_RELEASE_COMPLETE 0x5A
624#define MSG_TYPE_RESTART 0x46
625#define MSG_TYPE_RESTART_ACK 0x4E
626/* Status */
627#define MSG_TYPE_STATUS 0x7D
628#define MSG_TYPE_STATUS_ENQ 0x75
629
630struct tok fr_q933_msg_values[] = {
631 { MSG_TYPE_ESC_TO_NATIONAL, "ESC to National" },
632 { MSG_TYPE_ALERT, "Alert" },
633 { MSG_TYPE_CALL_PROCEEDING, "Call proceeding" },
634 { MSG_TYPE_CONNECT, "Connect" },
635 { MSG_TYPE_CONNECT_ACK, "Connect ACK" },
636 { MSG_TYPE_PROGRESS, "Progress" },
637 { MSG_TYPE_SETUP, "Setup" },
638 { MSG_TYPE_DISCONNECT, "Disconnect" },
639 { MSG_TYPE_RELEASE, "Release" },
640 { MSG_TYPE_RELEASE_COMPLETE, "Release Complete" },
641 { MSG_TYPE_RESTART, "Restart" },
642 { MSG_TYPE_RESTART_ACK, "Restart ACK" },
643 { MSG_TYPE_STATUS, "Status Reply" },
644 { MSG_TYPE_STATUS_ENQ, "Status Enquiry" },
645 { 0, NULL }
646};
647
648#define MSG_ANSI_LOCKING_SHIFT 0x95
649
650#define FR_LMI_ANSI_REPORT_TYPE_IE 0x01
651#define FR_LMI_ANSI_LINK_VERIFY_IE_91 0x19 /* details? */
652#define FR_LMI_ANSI_LINK_VERIFY_IE 0x03
653#define FR_LMI_ANSI_PVC_STATUS_IE 0x07
654
655#define FR_LMI_CCITT_REPORT_TYPE_IE 0x51
656#define FR_LMI_CCITT_LINK_VERIFY_IE 0x53
657#define FR_LMI_CCITT_PVC_STATUS_IE 0x57
658
659struct tok fr_q933_ie_values_codeset5[] = {
660 { FR_LMI_ANSI_REPORT_TYPE_IE, "ANSI Report Type" },
661 { FR_LMI_ANSI_LINK_VERIFY_IE_91, "ANSI Link Verify" },
662 { FR_LMI_ANSI_LINK_VERIFY_IE, "ANSI Link Verify" },
663 { FR_LMI_ANSI_PVC_STATUS_IE, "ANSI PVC Status" },
664 { FR_LMI_CCITT_REPORT_TYPE_IE, "CCITT Report Type" },
665 { FR_LMI_CCITT_LINK_VERIFY_IE, "CCITT Link Verify" },
666 { FR_LMI_CCITT_PVC_STATUS_IE, "CCITT PVC Status" },
667 { 0, NULL }
668};
669
670#define FR_LMI_REPORT_TYPE_IE_FULL_STATUS 0
671#define FR_LMI_REPORT_TYPE_IE_LINK_VERIFY 1
672#define FR_LMI_REPORT_TYPE_IE_ASYNC_PVC 2
673
674struct tok fr_lmi_report_type_ie_values[] = {
675 { FR_LMI_REPORT_TYPE_IE_FULL_STATUS, "Full Status" },
676 { FR_LMI_REPORT_TYPE_IE_LINK_VERIFY, "Link verify" },
677 { FR_LMI_REPORT_TYPE_IE_ASYNC_PVC, "Async PVC Status" },
678 { 0, NULL }
679};
680
681/* array of 16 codepages - currently we only support codepage 1,5 */
682static struct tok *fr_q933_ie_codesets[] = {
683 NULL,
684 fr_q933_ie_values_codeset5,
685 NULL,
686 NULL,
687 NULL,
688 fr_q933_ie_values_codeset5,
689 NULL,
690 NULL,
691 NULL,
692 NULL,
693 NULL,
694 NULL,
695 NULL,
696 NULL,
697 NULL,
698 NULL
699};
700
701static int fr_q933_print_ie_codeset5(const struct ie_tlv_header_t *ie_p,
702 const u_char *p);
703
704typedef int (*codeset_pr_func_t)(const struct ie_tlv_header_t *ie_p,
705 const u_char *p);
706
707/* array of 16 codepages - currently we only support codepage 1,5 */
708static codeset_pr_func_t fr_q933_print_ie_codeset[] = {
709 NULL,
710 fr_q933_print_ie_codeset5,
711 NULL,
712 NULL,
713 NULL,
714 fr_q933_print_ie_codeset5,
715 NULL,
716 NULL,
717 NULL,
718 NULL,
719 NULL,
720 NULL,
721 NULL,
722 NULL,
723 NULL,
724 NULL
725};
726
727void
728q933_print(const u_char *p, u_int length)
729{
730 const u_char *ptemp = p;
731 struct ie_tlv_header_t *ie_p;
732 int olen;
733 int is_ansi = 0;
734 u_int codeset;
735 u_int ie_is_known = 0;
736
737 if (length < 9) { /* shortest: Q.933a LINK VERIFY */
738 printf("[|q.933]");
739 return;
740 }
741
742 codeset = p[2]&0x0f; /* extract the codeset */
743
744 if (p[2] == MSG_ANSI_LOCKING_SHIFT) {
745 is_ansi = 1;
746 }
747
748 printf("%s", eflag ? "" : "Q.933, ");
749
750 /* printing out header part */
751 printf("%s, codeset %u", is_ansi ? "ANSI" : "CCITT", codeset);
752
753 if (p[0]) {
754 printf(", Call Ref: 0x%02x", p[0]);
755 }
756 if (vflag) {
757 printf(", %s (0x%02x), length %u",
758 tok2str(fr_q933_msg_values,
759 "unknown message", p[1]),
760 p[1],
761 length);
762 } else {
763 printf(", %s",
764 tok2str(fr_q933_msg_values,
765 "unknown message 0x%02x", p[1]));
766 }
767
768 olen = length; /* preserve the original length for non verbose mode */
769
770 if (length < (u_int)(2 - is_ansi)) {
771 printf("[|q.933]");
772 return;
773 }
774 length -= 2 + is_ansi;
775 ptemp += 2 + is_ansi;
776
777 /* Loop through the rest of IE */
778 while (length > sizeof(struct ie_tlv_header_t)) {
779 ie_p = (struct ie_tlv_header_t *)ptemp;
780 if (length < sizeof(struct ie_tlv_header_t) ||
781 length < sizeof(struct ie_tlv_header_t) + ie_p->ie_len) {
782 if (vflag) { /* not bark if there is just a trailer */
783 printf("\n[|q.933]");
784 } else {
785 printf(", length %u",olen);
786 }
787 return;
788 }
789
790 /* lets do the full IE parsing only in verbose mode
791 * however some IEs (DLCI Status, Link Verify)
792 * are also interestting in non-verbose mode */
793 if (vflag) {
794 printf("\n\t%s IE (0x%02x), length %u: ",
795 tok2str(fr_q933_ie_codesets[codeset],
796 "unknown", ie_p->ie_type),
797 ie_p->ie_type,
798 ie_p->ie_len);
799 }
800
801 /* sanity check */
802 if (ie_p->ie_type == 0 || ie_p->ie_len == 0) {
803 return;
804 }
805
806 if (fr_q933_print_ie_codeset[codeset] != NULL) {
807 ie_is_known = fr_q933_print_ie_codeset[codeset](ie_p, ptemp);
808 }
809
810 if (vflag >= 1 && !ie_is_known) {
811 print_unknown_data(ptemp+2,"\n\t",ie_p->ie_len);
812 }
813
814 /* do we want to see a hexdump of the IE ? */
815 if (vflag> 1 && ie_is_known) {
816 print_unknown_data(ptemp+2,"\n\t ",ie_p->ie_len);
817 }
818
819 length = length - ie_p->ie_len - 2;
820 ptemp = ptemp + ie_p->ie_len + 2;
821 }
822 if (!vflag) {
823 printf(", length %u",olen);
824 }
825}
826
827static int
828fr_q933_print_ie_codeset5(const struct ie_tlv_header_t *ie_p, const u_char *p)
829{
830 u_int dlci;
831
832 switch (ie_p->ie_type) {
833
834 case FR_LMI_ANSI_REPORT_TYPE_IE: /* fall through */
835 case FR_LMI_CCITT_REPORT_TYPE_IE:
836 if (vflag) {
837 printf("%s (%u)",
838 tok2str(fr_lmi_report_type_ie_values,"unknown",p[2]),
839 p[2]);
840 }
841 return 1;
842
843 case FR_LMI_ANSI_LINK_VERIFY_IE: /* fall through */
844 case FR_LMI_CCITT_LINK_VERIFY_IE:
845 case FR_LMI_ANSI_LINK_VERIFY_IE_91:
846 if (!vflag) {
847 printf(", ");
848 }
849 printf("TX Seq: %3d, RX Seq: %3d", p[2], p[3]);
850 return 1;
851
852 case FR_LMI_ANSI_PVC_STATUS_IE: /* fall through */
853 case FR_LMI_CCITT_PVC_STATUS_IE:
854 if (!vflag) {
855 printf(", ");
856 }
857 /* now parse the DLCI information element. */
858 if ((ie_p->ie_len < 3) ||
859 (p[2] & 0x80) ||
860 ((ie_p->ie_len == 3) && !(p[3] & 0x80)) ||
861 ((ie_p->ie_len == 4) && ((p[3] & 0x80) || !(p[4] & 0x80))) ||
862 ((ie_p->ie_len == 5) && ((p[3] & 0x80) || (p[4] & 0x80) ||
863 !(p[5] & 0x80))) ||
864 (ie_p->ie_len > 5) ||
865 !(p[ie_p->ie_len + 1] & 0x80)) {
866 printf("Invalid DLCI IE");
867 }
868
869 dlci = ((p[2] & 0x3F) << 4) | ((p[3] & 0x78) >> 3);
870 if (ie_p->ie_len == 4) {
871 dlci = (dlci << 6) | ((p[4] & 0x7E) >> 1);
872 }
873 else if (ie_p->ie_len == 5) {
874 dlci = (dlci << 13) | (p[4] & 0x7F) | ((p[5] & 0x7E) >> 1);
875 }
876
877 printf("DLCI %u: status %s%s", dlci,
878 p[ie_p->ie_len + 1] & 0x8 ? "New, " : "",
879 p[ie_p->ie_len + 1] & 0x2 ? "Active" : "Inactive");
880 return 1;
881 }
882
883 return 0;
884}