]> The Tcpdump Group git mirrors - tcpdump/blob - print-fr.c
Don't cast away constness.
[tcpdump] / print-fr.c
1 /*
2 * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 */
21
22 /* \summary: Frame Relay printer */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <netdissect-stdinc.h>
29
30 #include <stdio.h>
31 #include <string.h>
32
33 #include "netdissect.h"
34 #include "addrtoname.h"
35 #include "ethertype.h"
36 #include "llc.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 1 on success,
98 * 0 on invalid address, -1 on truncated packet
99 * save the flags dep. on address length
100 */
101 static int parse_q922_addr(netdissect_options *ndo,
102 const u_char *p, u_int *dlci,
103 u_int *addr_len, uint8_t *flags, u_int length)
104 {
105 if (!ND_TTEST(p[0]) || length < 1)
106 return -1;
107 if ((p[0] & FR_EA_BIT))
108 return 0;
109
110 if (!ND_TTEST(p[1]) || length < 2)
111 return -1;
112 *addr_len = 2;
113 *dlci = ((p[0] & 0xFC) << 2) | ((p[1] & 0xF0) >> 4);
114
115 flags[0] = p[0] & 0x02; /* populate the first flag fields */
116 flags[1] = p[1] & 0x0c;
117 flags[2] = 0; /* clear the rest of the flags */
118 flags[3] = 0;
119
120 if (p[1] & FR_EA_BIT)
121 return 1; /* 2-byte Q.922 address */
122
123 p += 2;
124 length -= 2;
125 if (!ND_TTEST(p[0]) || length < 1)
126 return -1;
127 (*addr_len)++; /* 3- or 4-byte Q.922 address */
128 if ((p[0] & FR_EA_BIT) == 0) {
129 *dlci = (*dlci << 7) | (p[0] >> 1);
130 (*addr_len)++; /* 4-byte Q.922 address */
131 p++;
132 length--;
133 }
134
135 if (!ND_TTEST(p[0]) || length < 1)
136 return -1;
137 if ((p[0] & FR_EA_BIT) == 0)
138 return 0; /* more than 4 bytes of Q.922 address? */
139
140 flags[3] = p[0] & 0x02;
141
142 *dlci = (*dlci << 6) | (p[0] >> 2);
143
144 return 1;
145 }
146
147 char *
148 q922_string(netdissect_options *ndo, const u_char *p, u_int length)
149 {
150
151 static u_int dlci, addr_len;
152 static uint8_t flags[4];
153 static char buffer[sizeof("DLCI xxxxxxxxxx")];
154 memset(buffer, 0, sizeof(buffer));
155
156 if (parse_q922_addr(ndo, p, &dlci, &addr_len, flags, length) == 1){
157 snprintf(buffer, sizeof(buffer), "DLCI %u", dlci);
158 }
159
160 return buffer;
161 }
162
163
164 /* Frame Relay packet structure, with flags and CRC removed
165
166 +---------------------------+
167 | Q.922 Address* |
168 +-- --+
169 | |
170 +---------------------------+
171 | Control (UI = 0x03) |
172 +---------------------------+
173 | Optional Pad (0x00) |
174 +---------------------------+
175 | NLPID |
176 +---------------------------+
177 | . |
178 | . |
179 | . |
180 | Data |
181 | . |
182 | . |
183 +---------------------------+
184
185 * Q.922 addresses, as presently defined, are two octets and
186 contain a 10-bit DLCI. In some networks Q.922 addresses
187 may optionally be increased to three or four octets.
188 */
189
190 static void
191 fr_hdr_print(netdissect_options *ndo,
192 int length, u_int addr_len, u_int dlci, uint8_t *flags, uint16_t nlpid)
193 {
194 if (ndo->ndo_qflag) {
195 ND_PRINT((ndo, "Q.922, DLCI %u, length %u: ",
196 dlci,
197 length));
198 } else {
199 if (nlpid <= 0xff) /* if its smaller than 256 then its a NLPID */
200 ND_PRINT((ndo, "Q.922, hdr-len %u, DLCI %u, Flags [%s], NLPID %s (0x%02x), length %u: ",
201 addr_len,
202 dlci,
203 bittok2str(fr_header_flag_values, "none", EXTRACT_32BITS(flags)),
204 tok2str(nlpid_values,"unknown", nlpid),
205 nlpid,
206 length));
207 else /* must be an ethertype */
208 ND_PRINT((ndo, "Q.922, hdr-len %u, DLCI %u, Flags [%s], cisco-ethertype %s (0x%04x), length %u: ",
209 addr_len,
210 dlci,
211 bittok2str(fr_header_flag_values, "none", EXTRACT_32BITS(flags)),
212 tok2str(ethertype_values, "unknown", nlpid),
213 nlpid,
214 length));
215 }
216 }
217
218 u_int
219 fr_if_print(netdissect_options *ndo,
220 const struct pcap_pkthdr *h, register const u_char *p)
221 {
222 register u_int length = h->len;
223 register u_int caplen = h->caplen;
224
225 ND_TCHECK2(*p, 4); /* minimum frame header length */
226
227 if ((length = fr_print(ndo, p, length)) == 0)
228 return (0);
229 else
230 return length;
231 trunc:
232 ND_PRINT((ndo, "[|fr]"));
233 return caplen;
234 }
235
236 u_int
237 fr_print(netdissect_options *ndo,
238 register const u_char *p, u_int length)
239 {
240 int ret;
241 uint16_t extracted_ethertype;
242 u_int dlci;
243 u_int addr_len;
244 uint16_t nlpid;
245 u_int hdr_len;
246 uint8_t flags[4];
247
248 ret = parse_q922_addr(ndo, p, &dlci, &addr_len, flags, length);
249 if (ret == -1)
250 goto trunc;
251 if (ret == 0) {
252 ND_PRINT((ndo, "Q.922, invalid address"));
253 return 0;
254 }
255
256 ND_TCHECK(p[addr_len]);
257 if (length < addr_len + 1)
258 goto trunc;
259
260 if (p[addr_len] != LLC_UI && dlci != 0) {
261 /*
262 * Let's figure out if we have Cisco-style encapsulation,
263 * with an Ethernet type (Cisco HDLC type?) following the
264 * address.
265 */
266 if (!ND_TTEST2(p[addr_len], 2) || length < addr_len + 2) {
267 /* no Ethertype */
268 ND_PRINT((ndo, "UI %02x! ", p[addr_len]));
269 } else {
270 extracted_ethertype = EXTRACT_16BITS(p+addr_len);
271
272 if (ndo->ndo_eflag)
273 fr_hdr_print(ndo, length, addr_len, dlci,
274 flags, extracted_ethertype);
275
276 if (ethertype_print(ndo, extracted_ethertype,
277 p+addr_len+ETHERTYPE_LEN,
278 length-addr_len-ETHERTYPE_LEN,
279 length-addr_len-ETHERTYPE_LEN) == 0)
280 /* ether_type not known, probably it wasn't one */
281 ND_PRINT((ndo, "UI %02x! ", p[addr_len]));
282 else
283 return addr_len + 2;
284 }
285 }
286
287 ND_TCHECK(p[addr_len+1]);
288 if (length < addr_len + 2)
289 goto trunc;
290
291 if (p[addr_len + 1] == 0) {
292 /*
293 * Assume a pad byte after the control (UI) byte.
294 * A pad byte should only be used with 3-byte Q.922.
295 */
296 if (addr_len != 3)
297 ND_PRINT((ndo, "Pad! "));
298 hdr_len = addr_len + 1 /* UI */ + 1 /* pad */ + 1 /* NLPID */;
299 } else {
300 /*
301 * Not a pad byte.
302 * A pad byte should be used with 3-byte Q.922.
303 */
304 if (addr_len == 3)
305 ND_PRINT((ndo, "No pad! "));
306 hdr_len = addr_len + 1 /* UI */ + 1 /* NLPID */;
307 }
308
309 ND_TCHECK(p[hdr_len - 1]);
310 if (length < hdr_len)
311 goto trunc;
312 nlpid = p[hdr_len - 1];
313
314 if (ndo->ndo_eflag)
315 fr_hdr_print(ndo, length, addr_len, dlci, flags, nlpid);
316 p += hdr_len;
317 length -= hdr_len;
318
319 switch (nlpid) {
320 case NLPID_IP:
321 ip_print(ndo, p, length);
322 break;
323
324 case NLPID_IP6:
325 ip6_print(ndo, p, length);
326 break;
327
328 case NLPID_CLNP:
329 case NLPID_ESIS:
330 case NLPID_ISIS:
331 isoclns_print(ndo, p - 1, length + 1, length + 1); /* OSI printers need the NLPID field */
332 break;
333
334 case NLPID_SNAP:
335 if (snap_print(ndo, p, length, length, NULL, NULL, 0) == 0) {
336 /* ether_type not known, print raw packet */
337 if (!ndo->ndo_eflag)
338 fr_hdr_print(ndo, length + hdr_len, hdr_len,
339 dlci, flags, nlpid);
340 if (!ndo->ndo_suppress_default_print)
341 ND_DEFAULTPRINT(p - hdr_len, length + hdr_len);
342 }
343 break;
344
345 case NLPID_Q933:
346 q933_print(ndo, p, length);
347 break;
348
349 case NLPID_MFR:
350 frf15_print(ndo, p, length);
351 break;
352
353 case NLPID_PPP:
354 ppp_print(ndo, p, length);
355 break;
356
357 default:
358 if (!ndo->ndo_eflag)
359 fr_hdr_print(ndo, length + hdr_len, addr_len,
360 dlci, flags, nlpid);
361 if (!ndo->ndo_xflag)
362 ND_DEFAULTPRINT(p, length);
363 }
364
365 return hdr_len;
366
367 trunc:
368 ND_PRINT((ndo, "[|fr]"));
369 return 0;
370
371 }
372
373 u_int
374 mfr_if_print(netdissect_options *ndo,
375 const struct pcap_pkthdr *h, register const u_char *p)
376 {
377 register u_int length = h->len;
378 register u_int caplen = h->caplen;
379
380 ND_TCHECK2(*p, 2); /* minimum frame header length */
381
382 if ((length = mfr_print(ndo, p, length)) == 0)
383 return (0);
384 else
385 return length;
386 trunc:
387 ND_PRINT((ndo, "[|mfr]"));
388 return caplen;
389 }
390
391
392 #define MFR_CTRL_MSG_ADD_LINK 1
393 #define MFR_CTRL_MSG_ADD_LINK_ACK 2
394 #define MFR_CTRL_MSG_ADD_LINK_REJ 3
395 #define MFR_CTRL_MSG_HELLO 4
396 #define MFR_CTRL_MSG_HELLO_ACK 5
397 #define MFR_CTRL_MSG_REMOVE_LINK 6
398 #define MFR_CTRL_MSG_REMOVE_LINK_ACK 7
399
400 static const struct tok mfr_ctrl_msg_values[] = {
401 { MFR_CTRL_MSG_ADD_LINK, "Add Link" },
402 { MFR_CTRL_MSG_ADD_LINK_ACK, "Add Link ACK" },
403 { MFR_CTRL_MSG_ADD_LINK_REJ, "Add Link Reject" },
404 { MFR_CTRL_MSG_HELLO, "Hello" },
405 { MFR_CTRL_MSG_HELLO_ACK, "Hello ACK" },
406 { MFR_CTRL_MSG_REMOVE_LINK, "Remove Link" },
407 { MFR_CTRL_MSG_REMOVE_LINK_ACK, "Remove Link ACK" },
408 { 0, NULL }
409 };
410
411 #define MFR_CTRL_IE_BUNDLE_ID 1
412 #define MFR_CTRL_IE_LINK_ID 2
413 #define MFR_CTRL_IE_MAGIC_NUM 3
414 #define MFR_CTRL_IE_TIMESTAMP 5
415 #define MFR_CTRL_IE_VENDOR_EXT 6
416 #define MFR_CTRL_IE_CAUSE 7
417
418 static const struct tok mfr_ctrl_ie_values[] = {
419 { MFR_CTRL_IE_BUNDLE_ID, "Bundle ID"},
420 { MFR_CTRL_IE_LINK_ID, "Link ID"},
421 { MFR_CTRL_IE_MAGIC_NUM, "Magic Number"},
422 { MFR_CTRL_IE_TIMESTAMP, "Timestamp"},
423 { MFR_CTRL_IE_VENDOR_EXT, "Vendor Extension"},
424 { MFR_CTRL_IE_CAUSE, "Cause"},
425 { 0, NULL }
426 };
427
428 #define MFR_ID_STRING_MAXLEN 50
429
430 struct ie_tlv_header_t {
431 uint8_t ie_type;
432 uint8_t ie_len;
433 };
434
435 u_int
436 mfr_print(netdissect_options *ndo,
437 register const u_char *p, u_int length)
438 {
439 u_int tlen,idx,hdr_len = 0;
440 uint16_t sequence_num;
441 uint8_t ie_type,ie_len;
442 const uint8_t *tptr;
443
444
445 /*
446 * FRF.16 Link Integrity Control Frame
447 *
448 * 7 6 5 4 3 2 1 0
449 * +----+----+----+----+----+----+----+----+
450 * | B | E | C=1| 0 0 0 0 | EA |
451 * +----+----+----+----+----+----+----+----+
452 * | 0 0 0 0 0 0 0 0 |
453 * +----+----+----+----+----+----+----+----+
454 * | message type |
455 * +----+----+----+----+----+----+----+----+
456 */
457
458 ND_TCHECK2(*p, 4); /* minimum frame header length */
459
460 if ((p[0] & MFR_BEC_MASK) == MFR_CTRL_FRAME && p[1] == 0) {
461 ND_PRINT((ndo, "FRF.16 Control, Flags [%s], %s, length %u",
462 bittok2str(frf_flag_values,"none",(p[0] & MFR_BEC_MASK)),
463 tok2str(mfr_ctrl_msg_values,"Unknown Message (0x%02x)",p[2]),
464 length));
465 tptr = p + 3;
466 tlen = length -3;
467 hdr_len = 3;
468
469 if (!ndo->ndo_vflag)
470 return hdr_len;
471
472 while (tlen>sizeof(struct ie_tlv_header_t)) {
473 ND_TCHECK2(*tptr, sizeof(struct ie_tlv_header_t));
474 ie_type=tptr[0];
475 ie_len=tptr[1];
476
477 ND_PRINT((ndo, "\n\tIE %s (%u), length %u: ",
478 tok2str(mfr_ctrl_ie_values,"Unknown",ie_type),
479 ie_type,
480 ie_len));
481
482 /* infinite loop check */
483 if (ie_type == 0 || ie_len <= sizeof(struct ie_tlv_header_t))
484 return hdr_len;
485
486 ND_TCHECK2(*tptr, ie_len);
487 tptr+=sizeof(struct ie_tlv_header_t);
488 /* tlv len includes header */
489 ie_len-=sizeof(struct ie_tlv_header_t);
490 tlen-=sizeof(struct ie_tlv_header_t);
491
492 switch (ie_type) {
493
494 case MFR_CTRL_IE_MAGIC_NUM:
495 ND_PRINT((ndo, "0x%08x", EXTRACT_32BITS(tptr)));
496 break;
497
498 case MFR_CTRL_IE_BUNDLE_ID: /* same message format */
499 case MFR_CTRL_IE_LINK_ID:
500 for (idx = 0; idx < ie_len && idx < MFR_ID_STRING_MAXLEN; idx++) {
501 if (*(tptr+idx) != 0) /* don't print null termination */
502 safeputchar(ndo, *(tptr + idx));
503 else
504 break;
505 }
506 break;
507
508 case MFR_CTRL_IE_TIMESTAMP:
509 if (ie_len == sizeof(struct timeval)) {
510 ts_print(ndo, (const struct timeval *)tptr);
511 break;
512 }
513 /* fall through and hexdump if no unix timestamp */
514
515 /*
516 * FIXME those are the defined IEs that lack a decoder
517 * you are welcome to contribute code ;-)
518 */
519
520 case MFR_CTRL_IE_VENDOR_EXT:
521 case MFR_CTRL_IE_CAUSE:
522
523 default:
524 if (ndo->ndo_vflag <= 1)
525 print_unknown_data(ndo, tptr, "\n\t ", ie_len);
526 break;
527 }
528
529 /* do we want to see a hexdump of the IE ? */
530 if (ndo->ndo_vflag > 1 )
531 print_unknown_data(ndo, tptr, "\n\t ", ie_len);
532
533 tlen-=ie_len;
534 tptr+=ie_len;
535 }
536 return hdr_len;
537 }
538 /*
539 * FRF.16 Fragmentation Frame
540 *
541 * 7 6 5 4 3 2 1 0
542 * +----+----+----+----+----+----+----+----+
543 * | B | E | C=0|seq. (high 4 bits) | EA |
544 * +----+----+----+----+----+----+----+----+
545 * | sequence (low 8 bits) |
546 * +----+----+----+----+----+----+----+----+
547 * | DLCI (6 bits) | CR | EA |
548 * +----+----+----+----+----+----+----+----+
549 * | DLCI (4 bits) |FECN|BECN| DE | EA |
550 * +----+----+----+----+----+----+----+----+
551 */
552
553 sequence_num = (p[0]&0x1e)<<7 | p[1];
554 /* whole packet or first fragment ? */
555 if ((p[0] & MFR_BEC_MASK) == MFR_FRAG_FRAME ||
556 (p[0] & MFR_BEC_MASK) == MFR_B_BIT) {
557 ND_PRINT((ndo, "FRF.16 Frag, seq %u, Flags [%s], ",
558 sequence_num,
559 bittok2str(frf_flag_values,"none",(p[0] & MFR_BEC_MASK))));
560 hdr_len = 2;
561 fr_print(ndo, p+hdr_len,length-hdr_len);
562 return hdr_len;
563 }
564
565 /* must be a middle or the last fragment */
566 ND_PRINT((ndo, "FRF.16 Frag, seq %u, Flags [%s]",
567 sequence_num,
568 bittok2str(frf_flag_values,"none",(p[0] & MFR_BEC_MASK))));
569 print_unknown_data(ndo, p, "\n\t", length);
570
571 return hdr_len;
572
573 trunc:
574 ND_PRINT((ndo, "[|mfr]"));
575 return length;
576 }
577
578 /* an NLPID of 0xb1 indicates a 2-byte
579 * FRF.15 header
580 *
581 * 7 6 5 4 3 2 1 0
582 * +----+----+----+----+----+----+----+----+
583 * ~ Q.922 header ~
584 * +----+----+----+----+----+----+----+----+
585 * | NLPID (8 bits) | NLPID=0xb1
586 * +----+----+----+----+----+----+----+----+
587 * | B | E | C |seq. (high 4 bits) | R |
588 * +----+----+----+----+----+----+----+----+
589 * | sequence (low 8 bits) |
590 * +----+----+----+----+----+----+----+----+
591 */
592
593 #define FR_FRF15_FRAGTYPE 0x01
594
595 static void
596 frf15_print(netdissect_options *ndo,
597 const u_char *p, u_int length)
598 {
599 uint16_t sequence_num, flags;
600
601 if (length < 2)
602 goto trunc;
603 ND_TCHECK2(*p, 2);
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 return;
623
624 trunc:
625 ND_PRINT((ndo, "[|frf.15]"));
626 }
627
628 /*
629 * Q.933 decoding portion for framerelay specific.
630 */
631
632 /* Q.933 packet format
633 Format of Other Protocols
634 using Q.933 NLPID
635 +-------------------------------+
636 | Q.922 Address |
637 +---------------+---------------+
638 |Control 0x03 | NLPID 0x08 |
639 +---------------+---------------+
640 | L2 Protocol ID |
641 | octet 1 | octet 2 |
642 +-------------------------------+
643 | L3 Protocol ID |
644 | octet 2 | octet 2 |
645 +-------------------------------+
646 | Protocol Data |
647 +-------------------------------+
648 | FCS |
649 +-------------------------------+
650 */
651
652 /* L2 (Octet 1)- Call Reference Usually is 0x0 */
653
654 /*
655 * L2 (Octet 2)- Message Types definition 1 byte long.
656 */
657 /* Call Establish */
658 #define MSG_TYPE_ESC_TO_NATIONAL 0x00
659 #define MSG_TYPE_ALERT 0x01
660 #define MSG_TYPE_CALL_PROCEEDING 0x02
661 #define MSG_TYPE_CONNECT 0x07
662 #define MSG_TYPE_CONNECT_ACK 0x0F
663 #define MSG_TYPE_PROGRESS 0x03
664 #define MSG_TYPE_SETUP 0x05
665 /* Call Clear */
666 #define MSG_TYPE_DISCONNECT 0x45
667 #define MSG_TYPE_RELEASE 0x4D
668 #define MSG_TYPE_RELEASE_COMPLETE 0x5A
669 #define MSG_TYPE_RESTART 0x46
670 #define MSG_TYPE_RESTART_ACK 0x4E
671 /* Status */
672 #define MSG_TYPE_STATUS 0x7D
673 #define MSG_TYPE_STATUS_ENQ 0x75
674
675 static const struct tok fr_q933_msg_values[] = {
676 { MSG_TYPE_ESC_TO_NATIONAL, "ESC to National" },
677 { MSG_TYPE_ALERT, "Alert" },
678 { MSG_TYPE_CALL_PROCEEDING, "Call proceeding" },
679 { MSG_TYPE_CONNECT, "Connect" },
680 { MSG_TYPE_CONNECT_ACK, "Connect ACK" },
681 { MSG_TYPE_PROGRESS, "Progress" },
682 { MSG_TYPE_SETUP, "Setup" },
683 { MSG_TYPE_DISCONNECT, "Disconnect" },
684 { MSG_TYPE_RELEASE, "Release" },
685 { MSG_TYPE_RELEASE_COMPLETE, "Release Complete" },
686 { MSG_TYPE_RESTART, "Restart" },
687 { MSG_TYPE_RESTART_ACK, "Restart ACK" },
688 { MSG_TYPE_STATUS, "Status Reply" },
689 { MSG_TYPE_STATUS_ENQ, "Status Enquiry" },
690 { 0, NULL }
691 };
692
693 #define IE_IS_SINGLE_OCTET(iecode) ((iecode) & 0x80)
694 #define IE_IS_SHIFT(iecode) (((iecode) & 0xF0) == 0x90)
695 #define IE_SHIFT_IS_NON_LOCKING(iecode) ((iecode) & 0x08)
696 #define IE_SHIFT_IS_LOCKING(iecode) (!(IE_SHIFT_IS_NON_LOCKING(iecode)))
697 #define IE_SHIFT_CODESET(iecode) ((iecode) & 0x07)
698
699 #define FR_LMI_ANSI_REPORT_TYPE_IE 0x01
700 #define FR_LMI_ANSI_LINK_VERIFY_IE_91 0x19 /* details? */
701 #define FR_LMI_ANSI_LINK_VERIFY_IE 0x03
702 #define FR_LMI_ANSI_PVC_STATUS_IE 0x07
703
704 #define FR_LMI_CCITT_REPORT_TYPE_IE 0x51
705 #define FR_LMI_CCITT_LINK_VERIFY_IE 0x53
706 #define FR_LMI_CCITT_PVC_STATUS_IE 0x57
707
708 static const struct tok fr_q933_ie_values_codeset_0_5[] = {
709 { FR_LMI_ANSI_REPORT_TYPE_IE, "ANSI Report Type" },
710 { FR_LMI_ANSI_LINK_VERIFY_IE_91, "ANSI Link Verify" },
711 { FR_LMI_ANSI_LINK_VERIFY_IE, "ANSI Link Verify" },
712 { FR_LMI_ANSI_PVC_STATUS_IE, "ANSI PVC Status" },
713 { FR_LMI_CCITT_REPORT_TYPE_IE, "CCITT Report Type" },
714 { FR_LMI_CCITT_LINK_VERIFY_IE, "CCITT Link Verify" },
715 { FR_LMI_CCITT_PVC_STATUS_IE, "CCITT PVC Status" },
716 { 0, NULL }
717 };
718
719 #define FR_LMI_REPORT_TYPE_IE_FULL_STATUS 0
720 #define FR_LMI_REPORT_TYPE_IE_LINK_VERIFY 1
721 #define FR_LMI_REPORT_TYPE_IE_ASYNC_PVC 2
722
723 static const struct tok fr_lmi_report_type_ie_values[] = {
724 { FR_LMI_REPORT_TYPE_IE_FULL_STATUS, "Full Status" },
725 { FR_LMI_REPORT_TYPE_IE_LINK_VERIFY, "Link verify" },
726 { FR_LMI_REPORT_TYPE_IE_ASYNC_PVC, "Async PVC Status" },
727 { 0, NULL }
728 };
729
730 /* array of 16 codesets - currently we only support codepage 0 and 5 */
731 static const struct tok *fr_q933_ie_codesets[] = {
732 fr_q933_ie_values_codeset_0_5,
733 NULL,
734 NULL,
735 NULL,
736 NULL,
737 fr_q933_ie_values_codeset_0_5,
738 NULL,
739 NULL,
740 NULL,
741 NULL,
742 NULL,
743 NULL,
744 NULL,
745 NULL,
746 NULL,
747 NULL
748 };
749
750 static int fr_q933_print_ie_codeset_0_5(netdissect_options *ndo, u_int iecode,
751 u_int ielength, const u_char *p);
752
753 typedef int (*codeset_pr_func_t)(netdissect_options *, u_int iecode,
754 u_int ielength, const u_char *p);
755
756 /* array of 16 codesets - currently we only support codepage 0 and 5 */
757 static const codeset_pr_func_t fr_q933_print_ie_codeset[] = {
758 fr_q933_print_ie_codeset_0_5,
759 NULL,
760 NULL,
761 NULL,
762 NULL,
763 fr_q933_print_ie_codeset_0_5,
764 NULL,
765 NULL,
766 NULL,
767 NULL,
768 NULL,
769 NULL,
770 NULL,
771 NULL,
772 NULL,
773 NULL
774 };
775
776 /*
777 * ITU-T Q.933.
778 *
779 * p points to octet 2, the octet containing the length of the
780 * call reference value, so p[n] is octet n+2 ("octet X" is as
781 * used in Q.931/Q.933).
782 *
783 * XXX - actually used both for Q.931 and Q.933.
784 */
785 void
786 q933_print(netdissect_options *ndo,
787 const u_char *p, u_int length)
788 {
789 u_int olen;
790 u_int call_ref_length, i;
791 uint8_t call_ref[15]; /* maximum length - length field is 4 bits */
792 u_int msgtype;
793 u_int iecode;
794 u_int ielength;
795 u_int codeset = 0;
796 u_int is_ansi = 0;
797 u_int ie_is_known;
798 u_int non_locking_shift;
799 u_int unshift_codeset;
800
801 ND_PRINT((ndo, "%s", ndo->ndo_eflag ? "" : "Q.933"));
802
803 if (length == 0 || !ND_TTEST(*p)) {
804 if (!ndo->ndo_eflag)
805 ND_PRINT((ndo, ", "));
806 ND_PRINT((ndo, "length %u", length));
807 goto trunc;
808 }
809
810 /*
811 * Get the length of the call reference value.
812 */
813 olen = length; /* preserve the original length for display */
814 call_ref_length = (*p) & 0x0f;
815 p++;
816 length--;
817
818 /*
819 * Get the call reference value.
820 */
821 for (i = 0; i < call_ref_length; i++) {
822 if (length == 0 || !ND_TTEST(*p)) {
823 if (!ndo->ndo_eflag)
824 ND_PRINT((ndo, ", "));
825 ND_PRINT((ndo, "length %u", olen));
826 goto trunc;
827 }
828 call_ref[i] = *p;
829 p++;
830 length--;
831 }
832
833 /*
834 * Get the message type.
835 */
836 if (length == 0 || !ND_TTEST(*p)) {
837 if (!ndo->ndo_eflag)
838 ND_PRINT((ndo, ", "));
839 ND_PRINT((ndo, "length %u", olen));
840 goto trunc;
841 }
842 msgtype = *p;
843 p++;
844 length--;
845
846 /*
847 * Peek ahead to see if we start with a shift.
848 */
849 non_locking_shift = 0;
850 unshift_codeset = codeset;
851 if (length != 0) {
852 if (!ND_TTEST(*p)) {
853 if (!ndo->ndo_eflag)
854 ND_PRINT((ndo, ", "));
855 ND_PRINT((ndo, "length %u", olen));
856 goto trunc;
857 }
858 iecode = *p;
859 if (IE_IS_SHIFT(iecode)) {
860 /*
861 * It's a shift. Skip over it.
862 */
863 p++;
864 length--;
865
866 /*
867 * Get the codeset.
868 */
869 codeset = IE_SHIFT_CODESET(iecode);
870
871 /*
872 * If it's a locking shift to codeset 5,
873 * mark this as ANSI. (XXX - 5 is actually
874 * for national variants in general, not
875 * the US variant in particular, but maybe
876 * this is more American exceptionalism. :-))
877 */
878 if (IE_SHIFT_IS_LOCKING(iecode)) {
879 /*
880 * It's a locking shift.
881 */
882 if (codeset == 5) {
883 /*
884 * It's a locking shift to
885 * codeset 5, so this is
886 * T1.617 Annex D.
887 */
888 is_ansi = 1;
889 }
890 } else {
891 /*
892 * It's a non-locking shift.
893 * Remember the current codeset, so we
894 * can revert to it after the next IE.
895 */
896 non_locking_shift = 1;
897 unshift_codeset = 0;
898 }
899 }
900 }
901
902 /* printing out header part */
903 if (!ndo->ndo_eflag)
904 ND_PRINT((ndo, ", "));
905 ND_PRINT((ndo, "%s, codeset %u", is_ansi ? "ANSI" : "CCITT", codeset));
906
907 if (call_ref_length != 0) {
908 if (call_ref_length > 1 || p[0] != 0) {
909 /*
910 * Not a dummy call reference.
911 */
912 ND_PRINT((ndo, ", Call Ref: 0x"));
913 for (i = 0; i < call_ref_length; i++)
914 ND_PRINT((ndo, "%02x", call_ref[i]));
915 }
916 }
917 if (ndo->ndo_vflag) {
918 ND_PRINT((ndo, ", %s (0x%02x), length %u",
919 tok2str(fr_q933_msg_values,
920 "unknown message", msgtype),
921 msgtype,
922 olen));
923 } else {
924 ND_PRINT((ndo, ", %s",
925 tok2str(fr_q933_msg_values,
926 "unknown message 0x%02x", msgtype)));
927 }
928
929 /* Loop through the rest of the IEs */
930 while (length != 0) {
931 /*
932 * What's the state of any non-locking shifts?
933 */
934 if (non_locking_shift == 1) {
935 /*
936 * There's a non-locking shift in effect for
937 * this IE. Count it, so we reset the codeset
938 * before the next IE.
939 */
940 non_locking_shift = 2;
941 } else if (non_locking_shift == 2) {
942 /*
943 * Unshift.
944 */
945 codeset = unshift_codeset;
946 non_locking_shift = 0;
947 }
948
949 /*
950 * Get the first octet of the IE.
951 */
952 if (!ND_TTEST(*p)) {
953 if (!ndo->ndo_vflag) {
954 ND_PRINT((ndo, ", length %u", olen));
955 }
956 goto trunc;
957 }
958 iecode = *p;
959 p++;
960 length--;
961
962 /* Single-octet IE? */
963 if (IE_IS_SINGLE_OCTET(iecode)) {
964 /*
965 * Yes. Is it a shift?
966 */
967 if (IE_IS_SHIFT(iecode)) {
968 /*
969 * Yes. Is it locking?
970 */
971 if (IE_SHIFT_IS_LOCKING(iecode)) {
972 /*
973 * Yes.
974 */
975 non_locking_shift = 0;
976 } else {
977 /*
978 * No. Remember the current
979 * codeset, so we can revert
980 * to it after the next IE.
981 */
982 non_locking_shift = 1;
983 unshift_codeset = codeset;
984 }
985
986 /*
987 * Get the codeset.
988 */
989 codeset = IE_SHIFT_CODESET(iecode);
990 }
991 } else {
992 /*
993 * No. Get the IE length.
994 */
995 if (length == 0 || !ND_TTEST(*p)) {
996 if (!ndo->ndo_vflag) {
997 ND_PRINT((ndo, ", length %u", olen));
998 }
999 goto trunc;
1000 }
1001 ielength = *p;
1002 p++;
1003 length--;
1004
1005 /* lets do the full IE parsing only in verbose mode
1006 * however some IEs (DLCI Status, Link Verify)
1007 * are also interesting in non-verbose mode */
1008 if (ndo->ndo_vflag) {
1009 ND_PRINT((ndo, "\n\t%s IE (0x%02x), length %u: ",
1010 tok2str(fr_q933_ie_codesets[codeset],
1011 "unknown", iecode),
1012 iecode,
1013 ielength));
1014 }
1015
1016 /* sanity checks */
1017 if (iecode == 0 || ielength == 0) {
1018 return;
1019 }
1020 if (length < ielength || !ND_TTEST2(*p, ielength)) {
1021 if (!ndo->ndo_vflag) {
1022 ND_PRINT((ndo, ", length %u", olen));
1023 }
1024 goto trunc;
1025 }
1026
1027 ie_is_known = 0;
1028 if (fr_q933_print_ie_codeset[codeset] != NULL) {
1029 ie_is_known = fr_q933_print_ie_codeset[codeset](ndo, iecode, ielength, p);
1030 }
1031
1032 if (ie_is_known) {
1033 /*
1034 * Known IE; do we want to see a hexdump
1035 * of it?
1036 */
1037 if (ndo->ndo_vflag > 1) {
1038 /* Yes. */
1039 print_unknown_data(ndo, p, "\n\t ", ielength);
1040 }
1041 } else {
1042 /*
1043 * Unknown IE; if we're printing verbosely,
1044 * print its content in hex.
1045 */
1046 if (ndo->ndo_vflag >= 1) {
1047 print_unknown_data(ndo, p, "\n\t", ielength);
1048 }
1049 }
1050
1051 length -= ielength;
1052 p += ielength;
1053 }
1054 }
1055 if (!ndo->ndo_vflag) {
1056 ND_PRINT((ndo, ", length %u", olen));
1057 }
1058 return;
1059
1060 trunc:
1061 ND_PRINT((ndo, "[|q.933]"));
1062 }
1063
1064 static int
1065 fr_q933_print_ie_codeset_0_5(netdissect_options *ndo, u_int iecode,
1066 u_int ielength, const u_char *p)
1067 {
1068 u_int dlci;
1069
1070 switch (iecode) {
1071
1072 case FR_LMI_ANSI_REPORT_TYPE_IE: /* fall through */
1073 case FR_LMI_CCITT_REPORT_TYPE_IE:
1074 if (ielength < 1) {
1075 if (!ndo->ndo_vflag) {
1076 ND_PRINT((ndo, ", "));
1077 }
1078 ND_PRINT((ndo, "Invalid REPORT TYPE IE"));
1079 return 1;
1080 }
1081 if (ndo->ndo_vflag) {
1082 ND_PRINT((ndo, "%s (%u)",
1083 tok2str(fr_lmi_report_type_ie_values,"unknown",p[0]),
1084 p[0]));
1085 }
1086 return 1;
1087
1088 case FR_LMI_ANSI_LINK_VERIFY_IE: /* fall through */
1089 case FR_LMI_CCITT_LINK_VERIFY_IE:
1090 case FR_LMI_ANSI_LINK_VERIFY_IE_91:
1091 if (!ndo->ndo_vflag) {
1092 ND_PRINT((ndo, ", "));
1093 }
1094 if (ielength < 2) {
1095 ND_PRINT((ndo, "Invalid LINK VERIFY IE"));
1096 return 1;
1097 }
1098 ND_PRINT((ndo, "TX Seq: %3d, RX Seq: %3d", p[0], p[1]));
1099 return 1;
1100
1101 case FR_LMI_ANSI_PVC_STATUS_IE: /* fall through */
1102 case FR_LMI_CCITT_PVC_STATUS_IE:
1103 if (!ndo->ndo_vflag) {
1104 ND_PRINT((ndo, ", "));
1105 }
1106 /* now parse the DLCI information element. */
1107 if ((ielength < 3) ||
1108 (p[0] & 0x80) ||
1109 ((ielength == 3) && !(p[1] & 0x80)) ||
1110 ((ielength == 4) && ((p[1] & 0x80) || !(p[2] & 0x80))) ||
1111 ((ielength == 5) && ((p[1] & 0x80) || (p[2] & 0x80) ||
1112 !(p[3] & 0x80))) ||
1113 (ielength > 5) ||
1114 !(p[ielength - 1] & 0x80)) {
1115 ND_PRINT((ndo, "Invalid DLCI in PVC STATUS IE"));
1116 return 1;
1117 }
1118
1119 dlci = ((p[0] & 0x3F) << 4) | ((p[1] & 0x78) >> 3);
1120 if (ielength == 4) {
1121 dlci = (dlci << 6) | ((p[2] & 0x7E) >> 1);
1122 }
1123 else if (ielength == 5) {
1124 dlci = (dlci << 13) | (p[2] & 0x7F) | ((p[3] & 0x7E) >> 1);
1125 }
1126
1127 ND_PRINT((ndo, "DLCI %u: status %s%s", dlci,
1128 p[ielength - 1] & 0x8 ? "New, " : "",
1129 p[ielength - 1] & 0x2 ? "Active" : "Inactive"));
1130 return 1;
1131 }
1132
1133 return 0;
1134 }
1135 /*
1136 * Local Variables:
1137 * c-style: whitesmith
1138 * c-basic-offset: 8
1139 * End:
1140 */