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