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