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