]> The Tcpdump Group git mirrors - tcpdump/blob - print-fr.c
d9ac455bc7f22c09f504a08be47f3ea432e5fa36
[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_header(netdissect_options *ndo,
102 const u_char *p, u_int *dlci,
103 u_int *addr_len, uint32_t *flags, u_int length)
104 {
105 if (!ND_TTEST_1(p) || length < 1)
106 return -1;
107 if ((EXTRACT_U_1(p) & FR_EA_BIT))
108 return 0;
109
110 if (!ND_TTEST_1(p + 1) || length < 2)
111 return -1;
112 *addr_len = 2;
113 *dlci = ((EXTRACT_U_1(p) & 0xFC) << 2) | ((EXTRACT_U_1(p + 1) & 0xF0) >> 4);
114
115 *flags = ((EXTRACT_U_1(p) & 0x02) << 24) | /* CR flag */
116 ((EXTRACT_U_1(p + 1) & 0x0e) << 16); /* FECN,BECN,DE flags */
117
118 if (EXTRACT_U_1(p + 1) & FR_EA_BIT)
119 return 1; /* 2-byte Q.922 address */
120
121 p += 2;
122 length -= 2;
123 if (!ND_TTEST_1(p) || length < 1)
124 return -1;
125 (*addr_len)++; /* 3- or 4-byte Q.922 address */
126 if ((EXTRACT_U_1(p) & FR_EA_BIT) == 0) {
127 *dlci = (*dlci << 7) | (EXTRACT_U_1(p) >> 1);
128 (*addr_len)++; /* 4-byte Q.922 address */
129 p++;
130 length--;
131 }
132
133 if (!ND_TTEST_1(p) || length < 1)
134 return -1;
135 if ((EXTRACT_U_1(p) & FR_EA_BIT) == 0)
136 return 0; /* more than 4 bytes of Q.922 address? */
137
138 *flags = *flags | (EXTRACT_U_1(p) & 0x02); /* SDLC flag */
139
140 *dlci = (*dlci << 6) | (EXTRACT_U_1(p) >> 2);
141
142 return 1;
143 }
144
145 char *
146 q922_string(netdissect_options *ndo, const u_char *p, u_int length)
147 {
148
149 static u_int dlci, addr_len;
150 static uint32_t flags;
151 static char buffer[sizeof("DLCI xxxxxxxxxx")];
152 memset(buffer, 0, sizeof(buffer));
153
154 if (parse_q922_header(ndo, p, &dlci, &addr_len, &flags, length) == 1){
155 snprintf(buffer, sizeof(buffer), "DLCI %u", dlci);
156 }
157
158 return buffer;
159 }
160
161
162 /* Frame Relay packet structure, with flags and CRC removed
163
164 +---------------------------+
165 | Q.922 Address* |
166 +-- --+
167 | |
168 +---------------------------+
169 | Control (UI = 0x03) |
170 +---------------------------+
171 | Optional Pad (0x00) |
172 +---------------------------+
173 | NLPID |
174 +---------------------------+
175 | . |
176 | . |
177 | . |
178 | Data |
179 | . |
180 | . |
181 +---------------------------+
182
183 * Q.922 addresses, as presently defined, are two octets and
184 contain a 10-bit DLCI. In some networks Q.922 addresses
185 may optionally be increased to three or four octets.
186 */
187
188 static void
189 fr_hdr_print(netdissect_options *ndo, int length, u_int addr_len,
190 u_int dlci, uint32_t flags, uint16_t nlpid)
191 {
192 if (ndo->ndo_qflag) {
193 ND_PRINT("Q.922, DLCI %u, length %u: ",
194 dlci,
195 length);
196 } else {
197 if (nlpid <= 0xff) /* if its smaller than 256 then its a NLPID */
198 ND_PRINT("Q.922, hdr-len %u, DLCI %u, Flags [%s], NLPID %s (0x%02x), length %u: ",
199 addr_len,
200 dlci,
201 bittok2str(fr_header_flag_values, "none", flags),
202 tok2str(nlpid_values,"unknown", nlpid),
203 nlpid,
204 length);
205 else /* must be an ethertype */
206 ND_PRINT("Q.922, hdr-len %u, DLCI %u, Flags [%s], cisco-ethertype %s (0x%04x), length %u: ",
207 addr_len,
208 dlci,
209 bittok2str(fr_header_flag_values, "none", flags),
210 tok2str(ethertype_values, "unknown", nlpid),
211 nlpid,
212 length);
213 }
214 }
215
216 u_int
217 fr_if_print(netdissect_options *ndo,
218 const struct pcap_pkthdr *h, const u_char *p)
219 {
220 u_int length = h->len;
221 u_int caplen = h->caplen;
222
223 ND_TCHECK_4(p); /* minimum frame header length */
224
225 if ((length = fr_print(ndo, p, length)) == 0)
226 return (0);
227 else
228 return length;
229 trunc:
230 ND_PRINT("[|fr]");
231 return caplen;
232 }
233
234 u_int
235 fr_print(netdissect_options *ndo,
236 const u_char *p, u_int length)
237 {
238 int ret;
239 uint16_t extracted_ethertype;
240 u_int dlci;
241 u_int addr_len;
242 uint16_t nlpid;
243 u_int hdr_len;
244 uint32_t flags;
245
246 ret = parse_q922_header(ndo, p, &dlci, &addr_len, &flags, length);
247 if (ret == -1)
248 goto trunc;
249 if (ret == 0) {
250 ND_PRINT("Q.922, invalid address");
251 return 0;
252 }
253
254 ND_TCHECK_1(p + addr_len);
255 if (length < addr_len + 1)
256 goto trunc;
257
258 if (EXTRACT_U_1(p + addr_len) != LLC_UI && dlci != 0) {
259 /*
260 * Let's figure out if we have Cisco-style encapsulation,
261 * with an Ethernet type (Cisco HDLC type?) following the
262 * address.
263 */
264 if (!ND_TTEST_2(p + addr_len) || length < addr_len + 2) {
265 /* no Ethertype */
266 ND_PRINT("UI %02x! ", EXTRACT_U_1(p + addr_len));
267 } else {
268 extracted_ethertype = EXTRACT_BE_U_2(p + addr_len);
269
270 if (ndo->ndo_eflag)
271 fr_hdr_print(ndo, length, addr_len, dlci,
272 flags, extracted_ethertype);
273
274 if (ethertype_print(ndo, extracted_ethertype,
275 p+addr_len+ETHERTYPE_LEN,
276 length-addr_len-ETHERTYPE_LEN,
277 ndo->ndo_snapend-p-addr_len-ETHERTYPE_LEN,
278 NULL, NULL) == 0)
279 /* ether_type not known, probably it wasn't one */
280 ND_PRINT("UI %02x! ", EXTRACT_U_1(p + addr_len));
281 else
282 return addr_len + 2;
283 }
284 }
285
286 ND_TCHECK_1(p + addr_len + 1);
287 if (length < addr_len + 2)
288 goto trunc;
289
290 if (EXTRACT_U_1(p + addr_len + 1) == 0) {
291 /*
292 * Assume a pad byte after the control (UI) byte.
293 * A pad byte should only be used with 3-byte Q.922.
294 */
295 if (addr_len != 3)
296 ND_PRINT("Pad! ");
297 hdr_len = addr_len + 1 /* UI */ + 1 /* pad */ + 1 /* NLPID */;
298 } else {
299 /*
300 * Not a pad byte.
301 * A pad byte should be used with 3-byte Q.922.
302 */
303 if (addr_len == 3)
304 ND_PRINT("No pad! ");
305 hdr_len = addr_len + 1 /* UI */ + 1 /* NLPID */;
306 }
307
308 ND_TCHECK_1(p + hdr_len - 1);
309 if (length < hdr_len)
310 goto trunc;
311 nlpid = EXTRACT_U_1(p + hdr_len - 1);
312
313 if (ndo->ndo_eflag)
314 fr_hdr_print(ndo, length, addr_len, dlci, flags, nlpid);
315 p += hdr_len;
316 length -= hdr_len;
317
318 switch (nlpid) {
319 case NLPID_IP:
320 ip_print(ndo, p, length);
321 break;
322
323 case NLPID_IP6:
324 ip6_print(ndo, p, length);
325 break;
326
327 case NLPID_CLNP:
328 case NLPID_ESIS:
329 case NLPID_ISIS:
330 isoclns_print(ndo, p - 1, length + 1); /* OSI printers need the NLPID field */
331 break;
332
333 case NLPID_SNAP:
334 if (snap_print(ndo, p, length, ndo->ndo_snapend - p, NULL, NULL, 0) == 0) {
335 /* ether_type not known, print raw packet */
336 if (!ndo->ndo_eflag)
337 fr_hdr_print(ndo, length + hdr_len, hdr_len,
338 dlci, flags, nlpid);
339 if (!ndo->ndo_suppress_default_print)
340 ND_DEFAULTPRINT(p - hdr_len, length + hdr_len);
341 }
342 break;
343
344 case NLPID_Q933:
345 q933_print(ndo, p, length);
346 break;
347
348 case NLPID_MFR:
349 frf15_print(ndo, p, length);
350 break;
351
352 case NLPID_PPP:
353 ppp_print(ndo, p, length);
354 break;
355
356 default:
357 if (!ndo->ndo_eflag)
358 fr_hdr_print(ndo, length + hdr_len, addr_len,
359 dlci, flags, nlpid);
360 if (!ndo->ndo_xflag)
361 ND_DEFAULTPRINT(p, length);
362 }
363
364 return hdr_len;
365
366 trunc:
367 ND_PRINT("[|fr]");
368 return 0;
369
370 }
371
372 u_int
373 mfr_if_print(netdissect_options *ndo,
374 const struct pcap_pkthdr *h, const u_char *p)
375 {
376 u_int length = h->len;
377 u_int caplen = h->caplen;
378
379 ND_TCHECK_2(p); /* minimum frame header length */
380
381 if ((length = mfr_print(ndo, p, length)) == 0)
382 return (0);
383 else
384 return length;
385 trunc:
386 ND_PRINT("[|mfr]");
387 return caplen;
388 }
389
390
391 #define MFR_CTRL_MSG_ADD_LINK 1
392 #define MFR_CTRL_MSG_ADD_LINK_ACK 2
393 #define MFR_CTRL_MSG_ADD_LINK_REJ 3
394 #define MFR_CTRL_MSG_HELLO 4
395 #define MFR_CTRL_MSG_HELLO_ACK 5
396 #define MFR_CTRL_MSG_REMOVE_LINK 6
397 #define MFR_CTRL_MSG_REMOVE_LINK_ACK 7
398
399 static const struct tok mfr_ctrl_msg_values[] = {
400 { MFR_CTRL_MSG_ADD_LINK, "Add Link" },
401 { MFR_CTRL_MSG_ADD_LINK_ACK, "Add Link ACK" },
402 { MFR_CTRL_MSG_ADD_LINK_REJ, "Add Link Reject" },
403 { MFR_CTRL_MSG_HELLO, "Hello" },
404 { MFR_CTRL_MSG_HELLO_ACK, "Hello ACK" },
405 { MFR_CTRL_MSG_REMOVE_LINK, "Remove Link" },
406 { MFR_CTRL_MSG_REMOVE_LINK_ACK, "Remove Link ACK" },
407 { 0, NULL }
408 };
409
410 #define MFR_CTRL_IE_BUNDLE_ID 1
411 #define MFR_CTRL_IE_LINK_ID 2
412 #define MFR_CTRL_IE_MAGIC_NUM 3
413 #define MFR_CTRL_IE_TIMESTAMP 5
414 #define MFR_CTRL_IE_VENDOR_EXT 6
415 #define MFR_CTRL_IE_CAUSE 7
416
417 static const struct tok mfr_ctrl_ie_values[] = {
418 { MFR_CTRL_IE_BUNDLE_ID, "Bundle ID"},
419 { MFR_CTRL_IE_LINK_ID, "Link ID"},
420 { MFR_CTRL_IE_MAGIC_NUM, "Magic Number"},
421 { MFR_CTRL_IE_TIMESTAMP, "Timestamp"},
422 { MFR_CTRL_IE_VENDOR_EXT, "Vendor Extension"},
423 { MFR_CTRL_IE_CAUSE, "Cause"},
424 { 0, NULL }
425 };
426
427 #define MFR_ID_STRING_MAXLEN 50
428
429 struct ie_tlv_header_t {
430 uint8_t ie_type;
431 uint8_t ie_len;
432 };
433
434 u_int
435 mfr_print(netdissect_options *ndo,
436 const u_char *p, u_int length)
437 {
438 u_int tlen,idx,hdr_len = 0;
439 uint16_t sequence_num;
440 uint8_t ie_type,ie_len;
441 const uint8_t *tptr;
442
443
444 /*
445 * FRF.16 Link Integrity Control Frame
446 *
447 * 7 6 5 4 3 2 1 0
448 * +----+----+----+----+----+----+----+----+
449 * | B | E | C=1| 0 0 0 0 | EA |
450 * +----+----+----+----+----+----+----+----+
451 * | 0 0 0 0 0 0 0 0 |
452 * +----+----+----+----+----+----+----+----+
453 * | message type |
454 * +----+----+----+----+----+----+----+----+
455 */
456
457 ND_TCHECK_4(p); /* minimum frame header length */
458
459 if ((EXTRACT_U_1(p) & MFR_BEC_MASK) == MFR_CTRL_FRAME && EXTRACT_U_1(p + 1) == 0) {
460 ND_PRINT("FRF.16 Control, Flags [%s], %s, length %u",
461 bittok2str(frf_flag_values,"none",(EXTRACT_U_1(p) & MFR_BEC_MASK)),
462 tok2str(mfr_ctrl_msg_values,"Unknown Message (0x%02x)",EXTRACT_U_1(p + 2)),
463 length);
464 tptr = p + 3;
465 tlen = length -3;
466 hdr_len = 3;
467
468 if (!ndo->ndo_vflag)
469 return hdr_len;
470
471 while (tlen>sizeof(struct ie_tlv_header_t)) {
472 ND_TCHECK_LEN(tptr, sizeof(struct ie_tlv_header_t));
473 ie_type=EXTRACT_U_1(tptr);
474 ie_len=EXTRACT_U_1(tptr + 1);
475
476 ND_PRINT("\n\tIE %s (%u), length %u: ",
477 tok2str(mfr_ctrl_ie_values,"Unknown",ie_type),
478 ie_type,
479 ie_len);
480
481 /* infinite loop check */
482 if (ie_type == 0 || ie_len <= sizeof(struct ie_tlv_header_t))
483 return hdr_len;
484
485 ND_TCHECK_LEN(tptr, ie_len);
486 tptr+=sizeof(struct ie_tlv_header_t);
487 /* tlv len includes header */
488 ie_len-=sizeof(struct ie_tlv_header_t);
489 tlen-=sizeof(struct ie_tlv_header_t);
490
491 switch (ie_type) {
492
493 case MFR_CTRL_IE_MAGIC_NUM:
494 ND_PRINT("0x%08x", EXTRACT_BE_U_4(tptr));
495 break;
496
497 case MFR_CTRL_IE_BUNDLE_ID: /* same message format */
498 case MFR_CTRL_IE_LINK_ID:
499 for (idx = 0; idx < ie_len && idx < MFR_ID_STRING_MAXLEN; idx++) {
500 if (EXTRACT_U_1(tptr + idx) != 0) /* don't print null termination */
501 safeputchar(ndo, EXTRACT_U_1(tptr + idx));
502 else
503 break;
504 }
505 break;
506
507 case MFR_CTRL_IE_TIMESTAMP:
508 if (ie_len == sizeof(struct timeval)) {
509 ts_print(ndo, (const struct timeval *)tptr);
510 break;
511 }
512 /* fall through and hexdump if no unix timestamp */
513 ND_FALL_THROUGH;
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 = (EXTRACT_U_1(p)&0x1e)<<7 | EXTRACT_U_1(p + 1);
554 /* whole packet or first fragment ? */
555 if ((EXTRACT_U_1(p) & MFR_BEC_MASK) == MFR_FRAG_FRAME ||
556 (EXTRACT_U_1(p) & MFR_BEC_MASK) == MFR_B_BIT) {
557 ND_PRINT("FRF.16 Frag, seq %u, Flags [%s], ",
558 sequence_num,
559 bittok2str(frf_flag_values,"none",(EXTRACT_U_1(p) & 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("FRF.16 Frag, seq %u, Flags [%s]",
567 sequence_num,
568 bittok2str(frf_flag_values,"none",(EXTRACT_U_1(p) & MFR_BEC_MASK)));
569 print_unknown_data(ndo, p, "\n\t", length);
570
571 return hdr_len;
572
573 trunc:
574 ND_PRINT("[|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_TCHECK_2(p);
604
605 flags = EXTRACT_U_1(p)&MFR_BEC_MASK;
606 sequence_num = (EXTRACT_U_1(p)&0x1e)<<7 | EXTRACT_U_1(p + 1);
607
608 ND_PRINT("FRF.15, seq 0x%03x, Flags [%s],%s Fragmentation, length %u",
609 sequence_num,
610 bittok2str(frf_flag_values,"none",flags),
611 EXTRACT_U_1(p)&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("[|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("%s", ndo->ndo_eflag ? "" : "Q.933");
802
803 if (length == 0 || !ND_TTEST_1(p)) {
804 if (!ndo->ndo_eflag)
805 ND_PRINT(", ");
806 ND_PRINT("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 = EXTRACT_U_1(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_1(p)) {
823 if (!ndo->ndo_eflag)
824 ND_PRINT(", ");
825 ND_PRINT("length %u", olen);
826 goto trunc;
827 }
828 call_ref[i] = EXTRACT_U_1(p);
829 p++;
830 length--;
831 }
832
833 /*
834 * Get the message type.
835 */
836 if (length == 0 || !ND_TTEST_1(p)) {
837 if (!ndo->ndo_eflag)
838 ND_PRINT(", ");
839 ND_PRINT("length %u", olen);
840 goto trunc;
841 }
842 msgtype = EXTRACT_U_1(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_1(p)) {
853 if (!ndo->ndo_eflag)
854 ND_PRINT(", ");
855 ND_PRINT("length %u", olen);
856 goto trunc;
857 }
858 iecode = EXTRACT_U_1(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(", ");
905 ND_PRINT("%s, codeset %u", is_ansi ? "ANSI" : "CCITT", codeset);
906
907 if (call_ref_length != 0) {
908 ND_TCHECK_1(p);
909 if (call_ref_length > 1 || EXTRACT_U_1(p) != 0) {
910 /*
911 * Not a dummy call reference.
912 */
913 ND_PRINT(", Call Ref: 0x");
914 for (i = 0; i < call_ref_length; i++)
915 ND_PRINT("%02x", call_ref[i]);
916 }
917 }
918 if (ndo->ndo_vflag) {
919 ND_PRINT(", %s (0x%02x), length %u",
920 tok2str(fr_q933_msg_values,
921 "unknown message", msgtype),
922 msgtype,
923 olen);
924 } else {
925 ND_PRINT(", %s",
926 tok2str(fr_q933_msg_values,
927 "unknown message 0x%02x", msgtype));
928 }
929
930 /* Loop through the rest of the IEs */
931 while (length != 0) {
932 /*
933 * What's the state of any non-locking shifts?
934 */
935 if (non_locking_shift == 1) {
936 /*
937 * There's a non-locking shift in effect for
938 * this IE. Count it, so we reset the codeset
939 * before the next IE.
940 */
941 non_locking_shift = 2;
942 } else if (non_locking_shift == 2) {
943 /*
944 * Unshift.
945 */
946 codeset = unshift_codeset;
947 non_locking_shift = 0;
948 }
949
950 /*
951 * Get the first octet of the IE.
952 */
953 if (!ND_TTEST_1(p)) {
954 if (!ndo->ndo_vflag) {
955 ND_PRINT(", length %u", olen);
956 }
957 goto trunc;
958 }
959 iecode = EXTRACT_U_1(p);
960 p++;
961 length--;
962
963 /* Single-octet IE? */
964 if (IE_IS_SINGLE_OCTET(iecode)) {
965 /*
966 * Yes. Is it a shift?
967 */
968 if (IE_IS_SHIFT(iecode)) {
969 /*
970 * Yes. Is it locking?
971 */
972 if (IE_SHIFT_IS_LOCKING(iecode)) {
973 /*
974 * Yes.
975 */
976 non_locking_shift = 0;
977 } else {
978 /*
979 * No. Remember the current
980 * codeset, so we can revert
981 * to it after the next IE.
982 */
983 non_locking_shift = 1;
984 unshift_codeset = codeset;
985 }
986
987 /*
988 * Get the codeset.
989 */
990 codeset = IE_SHIFT_CODESET(iecode);
991 }
992 } else {
993 /*
994 * No. Get the IE length.
995 */
996 if (length == 0 || !ND_TTEST_1(p)) {
997 if (!ndo->ndo_vflag) {
998 ND_PRINT(", length %u", olen);
999 }
1000 goto trunc;
1001 }
1002 ielength = EXTRACT_U_1(p);
1003 p++;
1004 length--;
1005
1006 /* lets do the full IE parsing only in verbose mode
1007 * however some IEs (DLCI Status, Link Verify)
1008 * are also interesting in non-verbose mode */
1009 if (ndo->ndo_vflag) {
1010 ND_PRINT("\n\t%s IE (0x%02x), length %u: ",
1011 tok2str(fr_q933_ie_codesets[codeset],
1012 "unknown", iecode),
1013 iecode,
1014 ielength);
1015 }
1016
1017 /* sanity checks */
1018 if (iecode == 0 || ielength == 0) {
1019 return;
1020 }
1021 if (length < ielength || !ND_TTEST_LEN(p, ielength)) {
1022 if (!ndo->ndo_vflag) {
1023 ND_PRINT(", length %u", olen);
1024 }
1025 goto trunc;
1026 }
1027
1028 ie_is_known = 0;
1029 if (fr_q933_print_ie_codeset[codeset] != NULL) {
1030 ie_is_known = fr_q933_print_ie_codeset[codeset](ndo, iecode, ielength, p);
1031 }
1032
1033 if (ie_is_known) {
1034 /*
1035 * Known IE; do we want to see a hexdump
1036 * of it?
1037 */
1038 if (ndo->ndo_vflag > 1) {
1039 /* Yes. */
1040 print_unknown_data(ndo, p, "\n\t ", ielength);
1041 }
1042 } else {
1043 /*
1044 * Unknown IE; if we're printing verbosely,
1045 * print its content in hex.
1046 */
1047 if (ndo->ndo_vflag >= 1) {
1048 print_unknown_data(ndo, p, "\n\t", ielength);
1049 }
1050 }
1051
1052 length -= ielength;
1053 p += ielength;
1054 }
1055 }
1056 if (!ndo->ndo_vflag) {
1057 ND_PRINT(", length %u", olen);
1058 }
1059 return;
1060
1061 trunc:
1062 ND_PRINT("[|q.933]");
1063 }
1064
1065 static int
1066 fr_q933_print_ie_codeset_0_5(netdissect_options *ndo, u_int iecode,
1067 u_int ielength, const u_char *p)
1068 {
1069 u_int dlci;
1070
1071 switch (iecode) {
1072
1073 case FR_LMI_ANSI_REPORT_TYPE_IE: /* fall through */
1074 case FR_LMI_CCITT_REPORT_TYPE_IE:
1075 if (ielength < 1) {
1076 if (!ndo->ndo_vflag) {
1077 ND_PRINT(", ");
1078 }
1079 ND_PRINT("Invalid REPORT TYPE IE");
1080 return 1;
1081 }
1082 if (ndo->ndo_vflag) {
1083 ND_PRINT("%s (%u)",
1084 tok2str(fr_lmi_report_type_ie_values,"unknown",EXTRACT_U_1(p)),
1085 EXTRACT_U_1(p));
1086 }
1087 return 1;
1088
1089 case FR_LMI_ANSI_LINK_VERIFY_IE: /* fall through */
1090 case FR_LMI_CCITT_LINK_VERIFY_IE:
1091 case FR_LMI_ANSI_LINK_VERIFY_IE_91:
1092 if (!ndo->ndo_vflag) {
1093 ND_PRINT(", ");
1094 }
1095 if (ielength < 2) {
1096 ND_PRINT("Invalid LINK VERIFY IE");
1097 return 1;
1098 }
1099 ND_PRINT("TX Seq: %3d, RX Seq: %3d", EXTRACT_U_1(p), EXTRACT_U_1(p + 1));
1100 return 1;
1101
1102 case FR_LMI_ANSI_PVC_STATUS_IE: /* fall through */
1103 case FR_LMI_CCITT_PVC_STATUS_IE:
1104 if (!ndo->ndo_vflag) {
1105 ND_PRINT(", ");
1106 }
1107 /* now parse the DLCI information element. */
1108 if ((ielength < 3) ||
1109 (EXTRACT_U_1(p) & 0x80) ||
1110 ((ielength == 3) && !(EXTRACT_U_1(p + 1) & 0x80)) ||
1111 ((ielength == 4) &&
1112 ((EXTRACT_U_1(p + 1) & 0x80) || !(EXTRACT_U_1(p + 2) & 0x80))) ||
1113 ((ielength == 5) &&
1114 ((EXTRACT_U_1(p + 1) & 0x80) || (EXTRACT_U_1(p + 2) & 0x80) ||
1115 !(EXTRACT_U_1(p + 3) & 0x80))) ||
1116 (ielength > 5) ||
1117 !(EXTRACT_U_1(p + ielength - 1) & 0x80)) {
1118 ND_PRINT("Invalid DLCI in PVC STATUS IE");
1119 return 1;
1120 }
1121
1122 dlci = ((EXTRACT_U_1(p) & 0x3F) << 4) | ((EXTRACT_U_1(p + 1) & 0x78) >> 3);
1123 if (ielength == 4) {
1124 dlci = (dlci << 6) | ((EXTRACT_U_1(p + 2) & 0x7E) >> 1);
1125 }
1126 else if (ielength == 5) {
1127 dlci = (dlci << 13) | (EXTRACT_U_1(p + 2) & 0x7F) | ((EXTRACT_U_1(p + 3) & 0x7E) >> 1);
1128 }
1129
1130 ND_PRINT("DLCI %u: status %s%s", dlci,
1131 EXTRACT_U_1(p + ielength - 1) & 0x8 ? "New, " : "",
1132 EXTRACT_U_1(p + ielength - 1) & 0x2 ? "Active" : "Inactive");
1133 return 1;
1134 }
1135
1136 return 0;
1137 }
1138 /*
1139 * Local Variables:
1140 * c-style: whitesmith
1141 * c-basic-offset: 8
1142 * End:
1143 */