]> The Tcpdump Group git mirrors - tcpdump/blob - print-fr.c
Revert partially the commit 21b1273
[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
40 static void frf15_print(netdissect_options *ndo, const u_char *, u_int);
41
42 /*
43 * the frame relay header has a variable length
44 *
45 * the EA bit determines if there is another byte
46 * in the header
47 *
48 * minimum header length is 2 bytes
49 * maximum header length is 4 bytes
50 *
51 * 7 6 5 4 3 2 1 0
52 * +----+----+----+----+----+----+----+----+
53 * | DLCI (6 bits) | CR | EA |
54 * +----+----+----+----+----+----+----+----+
55 * | DLCI (4 bits) |FECN|BECN| DE | EA |
56 * +----+----+----+----+----+----+----+----+
57 * | DLCI (7 bits) | EA |
58 * +----+----+----+----+----+----+----+----+
59 * | DLCI (6 bits) |SDLC| EA |
60 * +----+----+----+----+----+----+----+----+
61 */
62
63 #define FR_EA_BIT 0x01
64
65 #define FR_CR_BIT 0x02000000
66 #define FR_DE_BIT 0x00020000
67 #define FR_BECN_BIT 0x00040000
68 #define FR_FECN_BIT 0x00080000
69 #define FR_SDLC_BIT 0x00000002
70
71
72 static const struct tok fr_header_flag_values[] = {
73 { FR_CR_BIT, "C!" },
74 { FR_DE_BIT, "DE" },
75 { FR_BECN_BIT, "BECN" },
76 { FR_FECN_BIT, "FECN" },
77 { FR_SDLC_BIT, "sdlcore" },
78 { 0, NULL }
79 };
80
81 /* FRF.15 / FRF.16 */
82 #define MFR_B_BIT 0x80
83 #define MFR_E_BIT 0x40
84 #define MFR_C_BIT 0x20
85 #define MFR_BEC_MASK (MFR_B_BIT | MFR_E_BIT | MFR_C_BIT)
86 #define MFR_CTRL_FRAME (MFR_B_BIT | MFR_E_BIT | MFR_C_BIT)
87 #define MFR_FRAG_FRAME (MFR_B_BIT | MFR_E_BIT )
88
89 static const struct tok frf_flag_values[] = {
90 { MFR_B_BIT, "Begin" },
91 { MFR_E_BIT, "End" },
92 { MFR_C_BIT, "Control" },
93 { 0, NULL }
94 };
95
96 /* Finds out Q.922 address length, DLCI and flags. Returns 1 on success,
97 * 0 on invalid address, -1 on truncated packet
98 * save the flags dep. on address length
99 */
100 static int parse_q922_header(netdissect_options *ndo,
101 const u_char *p, u_int *dlci,
102 u_int *addr_len, uint32_t *flags, u_int length)
103 {
104 if (!ND_TTEST_1(p) || length < 1)
105 return -1;
106 if ((GET_U_1(p) & FR_EA_BIT))
107 return 0;
108
109 if (!ND_TTEST_1(p + 1) || length < 2)
110 return -1;
111 *addr_len = 2;
112 *dlci = ((GET_U_1(p) & 0xFC) << 2) | ((GET_U_1(p + 1) & 0xF0) >> 4);
113
114 *flags = ((GET_U_1(p) & 0x02) << 24) | /* CR flag */
115 ((GET_U_1(p + 1) & 0x0e) << 16); /* FECN,BECN,DE flags */
116
117 if (GET_U_1(p + 1) & FR_EA_BIT)
118 return 1; /* 2-byte Q.922 address */
119
120 p += 2;
121 length -= 2;
122 if (!ND_TTEST_1(p) || length < 1)
123 return -1;
124 (*addr_len)++; /* 3- or 4-byte Q.922 address */
125 if ((GET_U_1(p) & FR_EA_BIT) == 0) {
126 *dlci = (*dlci << 7) | (GET_U_1(p) >> 1);
127 (*addr_len)++; /* 4-byte Q.922 address */
128 p++;
129 length--;
130 }
131
132 if (!ND_TTEST_1(p) || length < 1)
133 return -1;
134 if ((GET_U_1(p) & FR_EA_BIT) == 0)
135 return 0; /* more than 4 bytes of Q.922 address? */
136
137 *flags = *flags | (GET_U_1(p) & 0x02); /* SDLC flag */
138
139 *dlci = (*dlci << 6) | (GET_U_1(p) >> 2);
140
141 return 1;
142 }
143
144 const char *
145 q922_string(netdissect_options *ndo, const u_char *p, u_int length)
146 {
147
148 static u_int dlci, addr_len;
149 static uint32_t flags;
150 static char buffer[sizeof("DLCI xxxxxxxxxx")];
151 memset(buffer, 0, sizeof(buffer));
152
153 if (parse_q922_header(ndo, p, &dlci, &addr_len, &flags, length) == 1){
154 snprintf(buffer, sizeof(buffer), "DLCI %u", dlci);
155 }
156
157 return buffer;
158 }
159
160
161 /* Frame Relay packet structure, with flags and CRC removed
162
163 +---------------------------+
164 | Q.922 Address* |
165 +-- --+
166 | |
167 +---------------------------+
168 | Control (UI = 0x03) |
169 +---------------------------+
170 | Optional Pad (0x00) |
171 +---------------------------+
172 | NLPID |
173 +---------------------------+
174 | . |
175 | . |
176 | . |
177 | Data |
178 | . |
179 | . |
180 +---------------------------+
181
182 * Q.922 addresses, as presently defined, are two octets and
183 contain a 10-bit DLCI. In some networks Q.922 addresses
184 may optionally be increased to three or four octets.
185 */
186
187 static void
188 fr_hdr_print(netdissect_options *ndo, int length, u_int addr_len,
189 u_int dlci, uint32_t flags, uint16_t nlpid)
190 {
191 if (ndo->ndo_qflag) {
192 ND_PRINT("Q.922, DLCI %u, length %u: ",
193 dlci,
194 length);
195 } else {
196 if (nlpid <= 0xff) /* if its smaller than 256 then its a NLPID */
197 ND_PRINT("Q.922, hdr-len %u, DLCI %u, Flags [%s], NLPID %s (0x%02x), length %u: ",
198 addr_len,
199 dlci,
200 bittok2str(fr_header_flag_values, "none", flags),
201 tok2str(nlpid_values,"unknown", nlpid),
202 nlpid,
203 length);
204 else /* must be an ethertype */
205 ND_PRINT("Q.922, hdr-len %u, DLCI %u, Flags [%s], cisco-ethertype %s (0x%04x), length %u: ",
206 addr_len,
207 dlci,
208 bittok2str(fr_header_flag_values, "none", flags),
209 tok2str(ethertype_values, "unknown", nlpid),
210 nlpid,
211 length);
212 }
213 }
214
215 /* Frame Relay */
216 void
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 ndo->ndo_protocol = "fr";
224 if (caplen < 4) { /* minimum frame header length */
225 nd_print_trunc(ndo);
226 ndo->ndo_ll_hdr_len += caplen;
227 return;
228 }
229
230 ndo->ndo_ll_hdr_len += fr_print(ndo, p, length);
231 }
232
233 u_int
234 fr_print(netdissect_options *ndo,
235 const u_char *p, u_int length)
236 {
237 int ret;
238 uint16_t extracted_ethertype;
239 u_int dlci;
240 u_int addr_len;
241 uint16_t nlpid;
242 u_int hdr_len;
243 uint32_t flags;
244
245 ndo->ndo_protocol = "fr";
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 (GET_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! ", GET_U_1(p + addr_len));
267 } else {
268 extracted_ethertype = GET_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 ND_BYTES_AVAILABLE_AFTER(p)-addr_len-ETHERTYPE_LEN,
278 NULL, NULL) == 0)
279 /* ether_type not known, probably it wasn't one */
280 ND_PRINT("UI %02x! ", GET_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 (GET_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 = GET_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, ND_BYTES_AVAILABLE_AFTER(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_trunc(ndo);
368 return 0;
369
370 }
371
372 /* Multi Link Frame Relay (FRF.16) */
373 void
374 mfr_if_print(netdissect_options *ndo,
375 const struct pcap_pkthdr *h, const u_char *p)
376 {
377 u_int length = h->len;
378 u_int caplen = h->caplen;
379
380 ndo->ndo_protocol = "mfr";
381 if (caplen < 2) { /* minimum frame header length */
382 nd_print_trunc(ndo);
383 ndo->ndo_ll_hdr_len += caplen;
384 return;
385 }
386
387 ndo->ndo_ll_hdr_len += mfr_print(ndo, p, length);
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 ndo->ndo_protocol = "mfr";
458
459 if (length < 4) { /* minimum frame header length */
460 ND_PRINT("[length %u < 4]", length);
461 nd_print_invalid(ndo);
462 return length;
463 }
464 ND_TCHECK_4(p);
465
466 if ((GET_U_1(p) & MFR_BEC_MASK) == MFR_CTRL_FRAME && GET_U_1(p + 1) == 0) {
467 ND_PRINT("FRF.16 Control, Flags [%s], %s, length %u",
468 bittok2str(frf_flag_values,"none",(GET_U_1(p) & MFR_BEC_MASK)),
469 tok2str(mfr_ctrl_msg_values,"Unknown Message (0x%02x)",GET_U_1(p + 2)),
470 length);
471 tptr = p + 3;
472 tlen = length -3;
473 hdr_len = 3;
474
475 if (!ndo->ndo_vflag)
476 return hdr_len;
477
478 while (tlen>sizeof(struct ie_tlv_header_t)) {
479 ND_TCHECK_LEN(tptr, sizeof(struct ie_tlv_header_t));
480 ie_type=GET_U_1(tptr);
481 ie_len=GET_U_1(tptr + 1);
482
483 ND_PRINT("\n\tIE %s (%u), length %u: ",
484 tok2str(mfr_ctrl_ie_values,"Unknown",ie_type),
485 ie_type,
486 ie_len);
487
488 /* infinite loop check */
489 if (ie_type == 0 || ie_len <= sizeof(struct ie_tlv_header_t))
490 return hdr_len;
491
492 ND_TCHECK_LEN(tptr, ie_len);
493 tptr+=sizeof(struct ie_tlv_header_t);
494 /* tlv len includes header */
495 ie_len-=sizeof(struct ie_tlv_header_t);
496 tlen-=sizeof(struct ie_tlv_header_t);
497
498 switch (ie_type) {
499
500 case MFR_CTRL_IE_MAGIC_NUM:
501 /* FRF.16.1 Section 3.4.3 Magic Number Information Element */
502 if (ie_len != 4) {
503 ND_PRINT("[IE data length %d != 4]", ie_len);
504 nd_print_invalid(ndo);
505 break;
506 }
507 ND_PRINT("0x%08x", GET_BE_U_4(tptr));
508 break;
509
510 case MFR_CTRL_IE_BUNDLE_ID: /* same message format */
511 case MFR_CTRL_IE_LINK_ID:
512 for (idx = 0; idx < ie_len && idx < MFR_ID_STRING_MAXLEN; idx++) {
513 if (GET_U_1(tptr + idx) != 0) /* don't print null termination */
514 fn_print_char(ndo, GET_U_1(tptr + idx));
515 else
516 break;
517 }
518 break;
519
520 case MFR_CTRL_IE_TIMESTAMP:
521 if (ie_len == sizeof(struct timeval)) {
522 ts_print(ndo, (const struct timeval *)tptr);
523 break;
524 }
525 /* fall through and hexdump if no unix timestamp */
526 ND_FALL_THROUGH;
527
528 /*
529 * FIXME those are the defined IEs that lack a decoder
530 * you are welcome to contribute code ;-)
531 */
532
533 case MFR_CTRL_IE_VENDOR_EXT:
534 case MFR_CTRL_IE_CAUSE:
535
536 default:
537 if (ndo->ndo_vflag <= 1)
538 print_unknown_data(ndo, tptr, "\n\t ", ie_len);
539 break;
540 }
541
542 /* do we want to see a hexdump of the IE ? */
543 if (ndo->ndo_vflag > 1 )
544 print_unknown_data(ndo, tptr, "\n\t ", ie_len);
545
546 tlen-=ie_len;
547 tptr+=ie_len;
548 }
549 return hdr_len;
550 }
551 /*
552 * FRF.16 Fragmentation Frame
553 *
554 * 7 6 5 4 3 2 1 0
555 * +----+----+----+----+----+----+----+----+
556 * | B | E | C=0|seq. (high 4 bits) | EA |
557 * +----+----+----+----+----+----+----+----+
558 * | sequence (low 8 bits) |
559 * +----+----+----+----+----+----+----+----+
560 * | DLCI (6 bits) | CR | EA |
561 * +----+----+----+----+----+----+----+----+
562 * | DLCI (4 bits) |FECN|BECN| DE | EA |
563 * +----+----+----+----+----+----+----+----+
564 */
565
566 sequence_num = (GET_U_1(p)&0x1e)<<7 | GET_U_1(p + 1);
567 /* whole packet or first fragment ? */
568 if ((GET_U_1(p) & MFR_BEC_MASK) == MFR_FRAG_FRAME ||
569 (GET_U_1(p) & MFR_BEC_MASK) == MFR_B_BIT) {
570 ND_PRINT("FRF.16 Frag, seq %u, Flags [%s], ",
571 sequence_num,
572 bittok2str(frf_flag_values,"none",(GET_U_1(p) & MFR_BEC_MASK)));
573 hdr_len = 2;
574 fr_print(ndo, p+hdr_len,length-hdr_len);
575 return hdr_len;
576 }
577
578 /* must be a middle or the last fragment */
579 ND_PRINT("FRF.16 Frag, seq %u, Flags [%s]",
580 sequence_num,
581 bittok2str(frf_flag_values,"none",(GET_U_1(p) & MFR_BEC_MASK)));
582 print_unknown_data(ndo, p, "\n\t", length);
583
584 return hdr_len;
585
586 trunc:
587 nd_print_trunc(ndo);
588 return length;
589 }
590
591 /* an NLPID of 0xb1 indicates a 2-byte
592 * FRF.15 header
593 *
594 * 7 6 5 4 3 2 1 0
595 * +----+----+----+----+----+----+----+----+
596 * ~ Q.922 header ~
597 * +----+----+----+----+----+----+----+----+
598 * | NLPID (8 bits) | NLPID=0xb1
599 * +----+----+----+----+----+----+----+----+
600 * | B | E | C |seq. (high 4 bits) | R |
601 * +----+----+----+----+----+----+----+----+
602 * | sequence (low 8 bits) |
603 * +----+----+----+----+----+----+----+----+
604 */
605
606 #define FR_FRF15_FRAGTYPE 0x01
607
608 static void
609 frf15_print(netdissect_options *ndo,
610 const u_char *p, u_int length)
611 {
612 uint16_t sequence_num, flags;
613
614 if (length < 2)
615 goto trunc;
616
617 flags = GET_U_1(p)&MFR_BEC_MASK;
618 sequence_num = (GET_U_1(p)&0x1e)<<7 | GET_U_1(p + 1);
619
620 ND_PRINT("FRF.15, seq 0x%03x, Flags [%s],%s Fragmentation, length %u",
621 sequence_num,
622 bittok2str(frf_flag_values,"none",flags),
623 GET_U_1(p)&FR_FRF15_FRAGTYPE ? "Interface" : "End-to-End",
624 length);
625
626 /* TODO:
627 * depending on all permutations of the B, E and C bit
628 * dig as deep as we can - e.g. on the first (B) fragment
629 * there is enough payload to print the IP header
630 * on non (B) fragments it depends if the fragmentation
631 * model is end-to-end or interface based whether we want to print
632 * another Q.922 header
633 */
634 return;
635
636 trunc:
637 nd_print_trunc(ndo);
638 }
639
640 /*
641 * Q.933 decoding portion for framerelay specific.
642 */
643
644 /* Q.933 packet format
645 Format of Other Protocols
646 using Q.933 NLPID
647 +-------------------------------+
648 | Q.922 Address |
649 +---------------+---------------+
650 |Control 0x03 | NLPID 0x08 |
651 +---------------+---------------+
652 | L2 Protocol ID |
653 | octet 1 | octet 2 |
654 +-------------------------------+
655 | L3 Protocol ID |
656 | octet 2 | octet 2 |
657 +-------------------------------+
658 | Protocol Data |
659 +-------------------------------+
660 | FCS |
661 +-------------------------------+
662 */
663
664 /* L2 (Octet 1)- Call Reference Usually is 0x0 */
665
666 /*
667 * L2 (Octet 2)- Message Types definition 1 byte long.
668 */
669 /* Call Establish */
670 #define MSG_TYPE_ESC_TO_NATIONAL 0x00
671 #define MSG_TYPE_ALERT 0x01
672 #define MSG_TYPE_CALL_PROCEEDING 0x02
673 #define MSG_TYPE_CONNECT 0x07
674 #define MSG_TYPE_CONNECT_ACK 0x0F
675 #define MSG_TYPE_PROGRESS 0x03
676 #define MSG_TYPE_SETUP 0x05
677 /* Call Clear */
678 #define MSG_TYPE_DISCONNECT 0x45
679 #define MSG_TYPE_RELEASE 0x4D
680 #define MSG_TYPE_RELEASE_COMPLETE 0x5A
681 #define MSG_TYPE_RESTART 0x46
682 #define MSG_TYPE_RESTART_ACK 0x4E
683 /* Status */
684 #define MSG_TYPE_STATUS 0x7D
685 #define MSG_TYPE_STATUS_ENQ 0x75
686
687 static const struct tok fr_q933_msg_values[] = {
688 { MSG_TYPE_ESC_TO_NATIONAL, "ESC to National" },
689 { MSG_TYPE_ALERT, "Alert" },
690 { MSG_TYPE_CALL_PROCEEDING, "Call proceeding" },
691 { MSG_TYPE_CONNECT, "Connect" },
692 { MSG_TYPE_CONNECT_ACK, "Connect ACK" },
693 { MSG_TYPE_PROGRESS, "Progress" },
694 { MSG_TYPE_SETUP, "Setup" },
695 { MSG_TYPE_DISCONNECT, "Disconnect" },
696 { MSG_TYPE_RELEASE, "Release" },
697 { MSG_TYPE_RELEASE_COMPLETE, "Release Complete" },
698 { MSG_TYPE_RESTART, "Restart" },
699 { MSG_TYPE_RESTART_ACK, "Restart ACK" },
700 { MSG_TYPE_STATUS, "Status Reply" },
701 { MSG_TYPE_STATUS_ENQ, "Status Enquiry" },
702 { 0, NULL }
703 };
704
705 #define IE_IS_SINGLE_OCTET(iecode) ((iecode) & 0x80)
706 #define IE_IS_SHIFT(iecode) (((iecode) & 0xF0) == 0x90)
707 #define IE_SHIFT_IS_NON_LOCKING(iecode) ((iecode) & 0x08)
708 #define IE_SHIFT_IS_LOCKING(iecode) (!(IE_SHIFT_IS_NON_LOCKING(iecode)))
709 #define IE_SHIFT_CODESET(iecode) ((iecode) & 0x07)
710
711 #define FR_LMI_ANSI_REPORT_TYPE_IE 0x01
712 #define FR_LMI_ANSI_LINK_VERIFY_IE_91 0x19 /* details? */
713 #define FR_LMI_ANSI_LINK_VERIFY_IE 0x03
714 #define FR_LMI_ANSI_PVC_STATUS_IE 0x07
715
716 #define FR_LMI_CCITT_REPORT_TYPE_IE 0x51
717 #define FR_LMI_CCITT_LINK_VERIFY_IE 0x53
718 #define FR_LMI_CCITT_PVC_STATUS_IE 0x57
719
720 static const struct tok fr_q933_ie_values_codeset_0_5[] = {
721 { FR_LMI_ANSI_REPORT_TYPE_IE, "ANSI Report Type" },
722 { FR_LMI_ANSI_LINK_VERIFY_IE_91, "ANSI Link Verify" },
723 { FR_LMI_ANSI_LINK_VERIFY_IE, "ANSI Link Verify" },
724 { FR_LMI_ANSI_PVC_STATUS_IE, "ANSI PVC Status" },
725 { FR_LMI_CCITT_REPORT_TYPE_IE, "CCITT Report Type" },
726 { FR_LMI_CCITT_LINK_VERIFY_IE, "CCITT Link Verify" },
727 { FR_LMI_CCITT_PVC_STATUS_IE, "CCITT PVC Status" },
728 { 0, NULL }
729 };
730
731 #define FR_LMI_REPORT_TYPE_IE_FULL_STATUS 0
732 #define FR_LMI_REPORT_TYPE_IE_LINK_VERIFY 1
733 #define FR_LMI_REPORT_TYPE_IE_ASYNC_PVC 2
734
735 static const struct tok fr_lmi_report_type_ie_values[] = {
736 { FR_LMI_REPORT_TYPE_IE_FULL_STATUS, "Full Status" },
737 { FR_LMI_REPORT_TYPE_IE_LINK_VERIFY, "Link verify" },
738 { FR_LMI_REPORT_TYPE_IE_ASYNC_PVC, "Async PVC Status" },
739 { 0, NULL }
740 };
741
742 /* array of 16 codesets - currently we only support codepage 0 and 5 */
743 static const struct tok *fr_q933_ie_codesets[] = {
744 fr_q933_ie_values_codeset_0_5,
745 NULL,
746 NULL,
747 NULL,
748 NULL,
749 fr_q933_ie_values_codeset_0_5,
750 NULL,
751 NULL,
752 NULL,
753 NULL,
754 NULL,
755 NULL,
756 NULL,
757 NULL,
758 NULL,
759 NULL
760 };
761
762 static int fr_q933_print_ie_codeset_0_5(netdissect_options *ndo, u_int iecode,
763 u_int ielength, const u_char *p);
764
765 typedef int (*codeset_pr_func_t)(netdissect_options *, u_int iecode,
766 u_int ielength, const u_char *p);
767
768 /* array of 16 codesets - currently we only support codepage 0 and 5 */
769 static const codeset_pr_func_t fr_q933_print_ie_codeset[] = {
770 fr_q933_print_ie_codeset_0_5,
771 NULL,
772 NULL,
773 NULL,
774 NULL,
775 fr_q933_print_ie_codeset_0_5,
776 NULL,
777 NULL,
778 NULL,
779 NULL,
780 NULL,
781 NULL,
782 NULL,
783 NULL,
784 NULL,
785 NULL
786 };
787
788 /*
789 * ITU-T Q.933.
790 *
791 * p points to octet 2, the octet containing the length of the
792 * call reference value, so p[n] is octet n+2 ("octet X" is as
793 * used in Q.931/Q.933).
794 *
795 * XXX - actually used both for Q.931 and Q.933.
796 */
797 void
798 q933_print(netdissect_options *ndo,
799 const u_char *p, u_int length)
800 {
801 u_int olen;
802 u_int call_ref_length, i;
803 uint8_t call_ref[15]; /* maximum length - length field is 4 bits */
804 u_int msgtype;
805 u_int iecode;
806 u_int ielength;
807 u_int codeset = 0;
808 u_int is_ansi = 0;
809 u_int ie_is_known;
810 u_int non_locking_shift;
811 u_int unshift_codeset;
812
813 ndo->ndo_protocol = "q.933";
814 ND_PRINT("%s", ndo->ndo_eflag ? "" : "Q.933");
815
816 if (length == 0 || !ND_TTEST_1(p)) {
817 if (!ndo->ndo_eflag)
818 ND_PRINT(", ");
819 ND_PRINT("length %u", length);
820 goto trunc;
821 }
822
823 /*
824 * Get the length of the call reference value.
825 */
826 olen = length; /* preserve the original length for display */
827 call_ref_length = GET_U_1(p) & 0x0f;
828 p++;
829 length--;
830
831 /*
832 * Get the call reference value.
833 */
834 for (i = 0; i < call_ref_length; i++) {
835 if (length == 0 || !ND_TTEST_1(p)) {
836 if (!ndo->ndo_eflag)
837 ND_PRINT(", ");
838 ND_PRINT("length %u", olen);
839 goto trunc;
840 }
841 call_ref[i] = GET_U_1(p);
842 p++;
843 length--;
844 }
845
846 /*
847 * Get the message type.
848 */
849 if (length == 0 || !ND_TTEST_1(p)) {
850 if (!ndo->ndo_eflag)
851 ND_PRINT(", ");
852 ND_PRINT("length %u", olen);
853 goto trunc;
854 }
855 msgtype = GET_U_1(p);
856 p++;
857 length--;
858
859 /*
860 * Peek ahead to see if we start with a shift.
861 */
862 non_locking_shift = 0;
863 unshift_codeset = codeset;
864 if (length != 0) {
865 if (!ND_TTEST_1(p)) {
866 if (!ndo->ndo_eflag)
867 ND_PRINT(", ");
868 ND_PRINT("length %u", olen);
869 goto trunc;
870 }
871 iecode = GET_U_1(p);
872 if (IE_IS_SHIFT(iecode)) {
873 /*
874 * It's a shift. Skip over it.
875 */
876 p++;
877 length--;
878
879 /*
880 * Get the codeset.
881 */
882 codeset = IE_SHIFT_CODESET(iecode);
883
884 /*
885 * If it's a locking shift to codeset 5,
886 * mark this as ANSI. (XXX - 5 is actually
887 * for national variants in general, not
888 * the US variant in particular, but maybe
889 * this is more American exceptionalism. :-))
890 */
891 if (IE_SHIFT_IS_LOCKING(iecode)) {
892 /*
893 * It's a locking shift.
894 */
895 if (codeset == 5) {
896 /*
897 * It's a locking shift to
898 * codeset 5, so this is
899 * T1.617 Annex D.
900 */
901 is_ansi = 1;
902 }
903 } else {
904 /*
905 * It's a non-locking shift.
906 * Remember the current codeset, so we
907 * can revert to it after the next IE.
908 */
909 non_locking_shift = 1;
910 unshift_codeset = 0;
911 }
912 }
913 }
914
915 /* printing out header part */
916 if (!ndo->ndo_eflag)
917 ND_PRINT(", ");
918 ND_PRINT("%s, codeset %u", is_ansi ? "ANSI" : "CCITT", codeset);
919
920 if (call_ref_length != 0) {
921 if (call_ref_length > 1 || GET_U_1(p) != 0) {
922 /*
923 * Not a dummy call reference.
924 */
925 ND_PRINT(", Call Ref: 0x");
926 for (i = 0; i < call_ref_length; i++)
927 ND_PRINT("%02x", call_ref[i]);
928 }
929 }
930 if (ndo->ndo_vflag) {
931 ND_PRINT(", %s (0x%02x), length %u",
932 tok2str(fr_q933_msg_values,
933 "unknown message", msgtype),
934 msgtype,
935 olen);
936 } else {
937 ND_PRINT(", %s",
938 tok2str(fr_q933_msg_values,
939 "unknown message 0x%02x", msgtype));
940 }
941
942 /* Loop through the rest of the IEs */
943 while (length != 0) {
944 /*
945 * What's the state of any non-locking shifts?
946 */
947 if (non_locking_shift == 1) {
948 /*
949 * There's a non-locking shift in effect for
950 * this IE. Count it, so we reset the codeset
951 * before the next IE.
952 */
953 non_locking_shift = 2;
954 } else if (non_locking_shift == 2) {
955 /*
956 * Unshift.
957 */
958 codeset = unshift_codeset;
959 non_locking_shift = 0;
960 }
961
962 /*
963 * Get the first octet of the IE.
964 */
965 if (!ND_TTEST_1(p)) {
966 if (!ndo->ndo_vflag) {
967 ND_PRINT(", length %u", olen);
968 }
969 goto trunc;
970 }
971 iecode = GET_U_1(p);
972 p++;
973 length--;
974
975 /* Single-octet IE? */
976 if (IE_IS_SINGLE_OCTET(iecode)) {
977 /*
978 * Yes. Is it a shift?
979 */
980 if (IE_IS_SHIFT(iecode)) {
981 /*
982 * Yes. Is it locking?
983 */
984 if (IE_SHIFT_IS_LOCKING(iecode)) {
985 /*
986 * Yes.
987 */
988 non_locking_shift = 0;
989 } else {
990 /*
991 * No. Remember the current
992 * codeset, so we can revert
993 * to it after the next IE.
994 */
995 non_locking_shift = 1;
996 unshift_codeset = codeset;
997 }
998
999 /*
1000 * Get the codeset.
1001 */
1002 codeset = IE_SHIFT_CODESET(iecode);
1003 }
1004 } else {
1005 /*
1006 * No. Get the IE length.
1007 */
1008 if (length == 0 || !ND_TTEST_1(p)) {
1009 if (!ndo->ndo_vflag) {
1010 ND_PRINT(", length %u", olen);
1011 }
1012 goto trunc;
1013 }
1014 ielength = GET_U_1(p);
1015 p++;
1016 length--;
1017
1018 /* lets do the full IE parsing only in verbose mode
1019 * however some IEs (DLCI Status, Link Verify)
1020 * are also interesting in non-verbose mode */
1021 if (ndo->ndo_vflag) {
1022 ND_PRINT("\n\t%s IE (0x%02x), length %u: ",
1023 tok2str(fr_q933_ie_codesets[codeset],
1024 "unknown", iecode),
1025 iecode,
1026 ielength);
1027 }
1028
1029 /* sanity checks */
1030 if (iecode == 0 || ielength == 0) {
1031 return;
1032 }
1033 if (length < ielength || !ND_TTEST_LEN(p, ielength)) {
1034 if (!ndo->ndo_vflag) {
1035 ND_PRINT(", length %u", olen);
1036 }
1037 goto trunc;
1038 }
1039
1040 ie_is_known = 0;
1041 if (fr_q933_print_ie_codeset[codeset] != NULL) {
1042 ie_is_known = fr_q933_print_ie_codeset[codeset](ndo, iecode, ielength, p);
1043 }
1044
1045 if (ie_is_known) {
1046 /*
1047 * Known IE; do we want to see a hexdump
1048 * of it?
1049 */
1050 if (ndo->ndo_vflag > 1) {
1051 /* Yes. */
1052 print_unknown_data(ndo, p, "\n\t ", ielength);
1053 }
1054 } else {
1055 /*
1056 * Unknown IE; if we're printing verbosely,
1057 * print its content in hex.
1058 */
1059 if (ndo->ndo_vflag >= 1) {
1060 print_unknown_data(ndo, p, "\n\t", ielength);
1061 }
1062 }
1063
1064 length -= ielength;
1065 p += ielength;
1066 }
1067 }
1068 if (!ndo->ndo_vflag) {
1069 ND_PRINT(", length %u", olen);
1070 }
1071 return;
1072
1073 trunc:
1074 nd_print_trunc(ndo);
1075 }
1076
1077 static int
1078 fr_q933_print_ie_codeset_0_5(netdissect_options *ndo, u_int iecode,
1079 u_int ielength, const u_char *p)
1080 {
1081 u_int dlci;
1082
1083 switch (iecode) {
1084
1085 case FR_LMI_ANSI_REPORT_TYPE_IE: /* fall through */
1086 case FR_LMI_CCITT_REPORT_TYPE_IE:
1087 if (ielength < 1) {
1088 if (!ndo->ndo_vflag) {
1089 ND_PRINT(", ");
1090 }
1091 ND_PRINT("Invalid REPORT TYPE IE");
1092 return 1;
1093 }
1094 if (ndo->ndo_vflag) {
1095 ND_PRINT("%s (%u)",
1096 tok2str(fr_lmi_report_type_ie_values,"unknown",GET_U_1(p)),
1097 GET_U_1(p));
1098 }
1099 return 1;
1100
1101 case FR_LMI_ANSI_LINK_VERIFY_IE: /* fall through */
1102 case FR_LMI_CCITT_LINK_VERIFY_IE:
1103 case FR_LMI_ANSI_LINK_VERIFY_IE_91:
1104 if (!ndo->ndo_vflag) {
1105 ND_PRINT(", ");
1106 }
1107 if (ielength < 2) {
1108 ND_PRINT("Invalid LINK VERIFY IE");
1109 return 1;
1110 }
1111 ND_PRINT("TX Seq: %3d, RX Seq: %3d", GET_U_1(p), GET_U_1(p + 1));
1112 return 1;
1113
1114 case FR_LMI_ANSI_PVC_STATUS_IE: /* fall through */
1115 case FR_LMI_CCITT_PVC_STATUS_IE:
1116 if (!ndo->ndo_vflag) {
1117 ND_PRINT(", ");
1118 }
1119 /* now parse the DLCI information element. */
1120 if ((ielength < 3) ||
1121 (GET_U_1(p) & 0x80) ||
1122 ((ielength == 3) && !(GET_U_1(p + 1) & 0x80)) ||
1123 ((ielength == 4) &&
1124 ((GET_U_1(p + 1) & 0x80) || !(GET_U_1(p + 2) & 0x80))) ||
1125 ((ielength == 5) &&
1126 ((GET_U_1(p + 1) & 0x80) || (GET_U_1(p + 2) & 0x80) ||
1127 !(GET_U_1(p + 3) & 0x80))) ||
1128 (ielength > 5) ||
1129 !(GET_U_1(p + ielength - 1) & 0x80)) {
1130 ND_PRINT("Invalid DLCI in PVC STATUS IE");
1131 return 1;
1132 }
1133
1134 dlci = ((GET_U_1(p) & 0x3F) << 4) | ((GET_U_1(p + 1) & 0x78) >> 3);
1135 if (ielength == 4) {
1136 dlci = (dlci << 6) | ((GET_U_1(p + 2) & 0x7E) >> 1);
1137 }
1138 else if (ielength == 5) {
1139 dlci = (dlci << 13) | (GET_U_1(p + 2) & 0x7F) | ((GET_U_1(p + 3) & 0x7E) >> 1);
1140 }
1141
1142 ND_PRINT("DLCI %u: status %s%s", dlci,
1143 GET_U_1(p + ielength - 1) & 0x8 ? "New, " : "",
1144 GET_U_1(p + ielength - 1) & 0x2 ? "Active" : "Inactive");
1145 return 1;
1146 }
1147
1148 return 0;
1149 }