]> The Tcpdump Group git mirrors - tcpdump/blob - print-fr.c
IPFC: Update the link-layer dissector to a void function
[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 u_int
216 fr_if_print(netdissect_options *ndo,
217 const struct pcap_pkthdr *h, const u_char *p)
218 {
219 u_int length = h->len;
220 u_int caplen = h->caplen;
221
222 ndo->ndo_protocol = "fr_if";
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_trunc(ndo);
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 ndo->ndo_protocol = "fr";
247 ret = parse_q922_header(ndo, p, &dlci, &addr_len, &flags, length);
248 if (ret == -1)
249 goto trunc;
250 if (ret == 0) {
251 ND_PRINT("Q.922, invalid address");
252 return 0;
253 }
254
255 ND_TCHECK_1(p + addr_len);
256 if (length < addr_len + 1)
257 goto trunc;
258
259 if (GET_U_1(p + addr_len) != LLC_UI && dlci != 0) {
260 /*
261 * Let's figure out if we have Cisco-style encapsulation,
262 * with an Ethernet type (Cisco HDLC type?) following the
263 * address.
264 */
265 if (!ND_TTEST_2(p + addr_len) || length < addr_len + 2) {
266 /* no Ethertype */
267 ND_PRINT("UI %02x! ", GET_U_1(p + addr_len));
268 } else {
269 extracted_ethertype = GET_BE_U_2(p + addr_len);
270
271 if (ndo->ndo_eflag)
272 fr_hdr_print(ndo, length, addr_len, dlci,
273 flags, extracted_ethertype);
274
275 if (ethertype_print(ndo, extracted_ethertype,
276 p+addr_len+ETHERTYPE_LEN,
277 length-addr_len-ETHERTYPE_LEN,
278 ND_BYTES_AVAILABLE_AFTER(p)-addr_len-ETHERTYPE_LEN,
279 NULL, NULL) == 0)
280 /* ether_type not known, probably it wasn't one */
281 ND_PRINT("UI %02x! ", GET_U_1(p + addr_len));
282 else
283 return addr_len + 2;
284 }
285 }
286
287 ND_TCHECK_1(p + addr_len + 1);
288 if (length < addr_len + 2)
289 goto trunc;
290
291 if (GET_U_1(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("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("No pad! ");
306 hdr_len = addr_len + 1 /* UI */ + 1 /* NLPID */;
307 }
308
309 ND_TCHECK_1(p + hdr_len - 1);
310 if (length < hdr_len)
311 goto trunc;
312 nlpid = GET_U_1(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); /* OSI printers need the NLPID field */
332 break;
333
334 case NLPID_SNAP:
335 if (snap_print(ndo, p, length, ND_BYTES_AVAILABLE_AFTER(p), 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_trunc(ndo);
369 return 0;
370
371 }
372
373 u_int
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_if";
381 ND_TCHECK_2(p); /* minimum frame header length */
382
383 if ((length = mfr_print(ndo, p, length)) == 0)
384 return (0);
385 else
386 return length;
387 trunc:
388 nd_print_trunc(ndo);
389 return caplen;
390 }
391
392
393 #define MFR_CTRL_MSG_ADD_LINK 1
394 #define MFR_CTRL_MSG_ADD_LINK_ACK 2
395 #define MFR_CTRL_MSG_ADD_LINK_REJ 3
396 #define MFR_CTRL_MSG_HELLO 4
397 #define MFR_CTRL_MSG_HELLO_ACK 5
398 #define MFR_CTRL_MSG_REMOVE_LINK 6
399 #define MFR_CTRL_MSG_REMOVE_LINK_ACK 7
400
401 static const struct tok mfr_ctrl_msg_values[] = {
402 { MFR_CTRL_MSG_ADD_LINK, "Add Link" },
403 { MFR_CTRL_MSG_ADD_LINK_ACK, "Add Link ACK" },
404 { MFR_CTRL_MSG_ADD_LINK_REJ, "Add Link Reject" },
405 { MFR_CTRL_MSG_HELLO, "Hello" },
406 { MFR_CTRL_MSG_HELLO_ACK, "Hello ACK" },
407 { MFR_CTRL_MSG_REMOVE_LINK, "Remove Link" },
408 { MFR_CTRL_MSG_REMOVE_LINK_ACK, "Remove Link ACK" },
409 { 0, NULL }
410 };
411
412 #define MFR_CTRL_IE_BUNDLE_ID 1
413 #define MFR_CTRL_IE_LINK_ID 2
414 #define MFR_CTRL_IE_MAGIC_NUM 3
415 #define MFR_CTRL_IE_TIMESTAMP 5
416 #define MFR_CTRL_IE_VENDOR_EXT 6
417 #define MFR_CTRL_IE_CAUSE 7
418
419 static const struct tok mfr_ctrl_ie_values[] = {
420 { MFR_CTRL_IE_BUNDLE_ID, "Bundle ID"},
421 { MFR_CTRL_IE_LINK_ID, "Link ID"},
422 { MFR_CTRL_IE_MAGIC_NUM, "Magic Number"},
423 { MFR_CTRL_IE_TIMESTAMP, "Timestamp"},
424 { MFR_CTRL_IE_VENDOR_EXT, "Vendor Extension"},
425 { MFR_CTRL_IE_CAUSE, "Cause"},
426 { 0, NULL }
427 };
428
429 #define MFR_ID_STRING_MAXLEN 50
430
431 struct ie_tlv_header_t {
432 uint8_t ie_type;
433 uint8_t ie_len;
434 };
435
436 u_int
437 mfr_print(netdissect_options *ndo,
438 const u_char *p, u_int length)
439 {
440 u_int tlen,idx,hdr_len = 0;
441 uint16_t sequence_num;
442 uint8_t ie_type,ie_len;
443 const uint8_t *tptr;
444
445
446 /*
447 * FRF.16 Link Integrity Control Frame
448 *
449 * 7 6 5 4 3 2 1 0
450 * +----+----+----+----+----+----+----+----+
451 * | B | E | C=1| 0 0 0 0 | EA |
452 * +----+----+----+----+----+----+----+----+
453 * | 0 0 0 0 0 0 0 0 |
454 * +----+----+----+----+----+----+----+----+
455 * | message type |
456 * +----+----+----+----+----+----+----+----+
457 */
458
459 ndo->ndo_protocol = "mfr";
460
461 if (length < 4) { /* minimum frame header length */
462 ND_PRINT("[length %u < 4]", length);
463 nd_print_invalid(ndo);
464 return length;
465 }
466 ND_TCHECK_4(p);
467
468 if ((GET_U_1(p) & MFR_BEC_MASK) == MFR_CTRL_FRAME && GET_U_1(p + 1) == 0) {
469 ND_PRINT("FRF.16 Control, Flags [%s], %s, length %u",
470 bittok2str(frf_flag_values,"none",(GET_U_1(p) & MFR_BEC_MASK)),
471 tok2str(mfr_ctrl_msg_values,"Unknown Message (0x%02x)",GET_U_1(p + 2)),
472 length);
473 tptr = p + 3;
474 tlen = length -3;
475 hdr_len = 3;
476
477 if (!ndo->ndo_vflag)
478 return hdr_len;
479
480 while (tlen>sizeof(struct ie_tlv_header_t)) {
481 ND_TCHECK_LEN(tptr, sizeof(struct ie_tlv_header_t));
482 ie_type=GET_U_1(tptr);
483 ie_len=GET_U_1(tptr + 1);
484
485 ND_PRINT("\n\tIE %s (%u), length %u: ",
486 tok2str(mfr_ctrl_ie_values,"Unknown",ie_type),
487 ie_type,
488 ie_len);
489
490 /* infinite loop check */
491 if (ie_type == 0 || ie_len <= sizeof(struct ie_tlv_header_t))
492 return hdr_len;
493
494 ND_TCHECK_LEN(tptr, ie_len);
495 tptr+=sizeof(struct ie_tlv_header_t);
496 /* tlv len includes header */
497 ie_len-=sizeof(struct ie_tlv_header_t);
498 tlen-=sizeof(struct ie_tlv_header_t);
499
500 switch (ie_type) {
501
502 case MFR_CTRL_IE_MAGIC_NUM:
503 /* FRF.16.1 Section 3.4.3 Magic Number Information Element */
504 if (ie_len != 4) {
505 ND_PRINT("[IE data length %d != 4]", ie_len);
506 nd_print_invalid(ndo);
507 break;
508 }
509 ND_PRINT("0x%08x", GET_BE_U_4(tptr));
510 break;
511
512 case MFR_CTRL_IE_BUNDLE_ID: /* same message format */
513 case MFR_CTRL_IE_LINK_ID:
514 for (idx = 0; idx < ie_len && idx < MFR_ID_STRING_MAXLEN; idx++) {
515 if (GET_U_1(tptr + idx) != 0) /* don't print null termination */
516 fn_print_char(ndo, GET_U_1(tptr + idx));
517 else
518 break;
519 }
520 break;
521
522 case MFR_CTRL_IE_TIMESTAMP:
523 if (ie_len == sizeof(struct timeval)) {
524 ts_print(ndo, (const struct timeval *)tptr);
525 break;
526 }
527 /* fall through and hexdump if no unix timestamp */
528 ND_FALL_THROUGH;
529
530 /*
531 * FIXME those are the defined IEs that lack a decoder
532 * you are welcome to contribute code ;-)
533 */
534
535 case MFR_CTRL_IE_VENDOR_EXT:
536 case MFR_CTRL_IE_CAUSE:
537
538 default:
539 if (ndo->ndo_vflag <= 1)
540 print_unknown_data(ndo, tptr, "\n\t ", ie_len);
541 break;
542 }
543
544 /* do we want to see a hexdump of the IE ? */
545 if (ndo->ndo_vflag > 1 )
546 print_unknown_data(ndo, tptr, "\n\t ", ie_len);
547
548 tlen-=ie_len;
549 tptr+=ie_len;
550 }
551 return hdr_len;
552 }
553 /*
554 * FRF.16 Fragmentation Frame
555 *
556 * 7 6 5 4 3 2 1 0
557 * +----+----+----+----+----+----+----+----+
558 * | B | E | C=0|seq. (high 4 bits) | EA |
559 * +----+----+----+----+----+----+----+----+
560 * | sequence (low 8 bits) |
561 * +----+----+----+----+----+----+----+----+
562 * | DLCI (6 bits) | CR | EA |
563 * +----+----+----+----+----+----+----+----+
564 * | DLCI (4 bits) |FECN|BECN| DE | EA |
565 * +----+----+----+----+----+----+----+----+
566 */
567
568 sequence_num = (GET_U_1(p)&0x1e)<<7 | GET_U_1(p + 1);
569 /* whole packet or first fragment ? */
570 if ((GET_U_1(p) & MFR_BEC_MASK) == MFR_FRAG_FRAME ||
571 (GET_U_1(p) & MFR_BEC_MASK) == MFR_B_BIT) {
572 ND_PRINT("FRF.16 Frag, seq %u, Flags [%s], ",
573 sequence_num,
574 bittok2str(frf_flag_values,"none",(GET_U_1(p) & MFR_BEC_MASK)));
575 hdr_len = 2;
576 fr_print(ndo, p+hdr_len,length-hdr_len);
577 return hdr_len;
578 }
579
580 /* must be a middle or the last fragment */
581 ND_PRINT("FRF.16 Frag, seq %u, Flags [%s]",
582 sequence_num,
583 bittok2str(frf_flag_values,"none",(GET_U_1(p) & MFR_BEC_MASK)));
584 print_unknown_data(ndo, p, "\n\t", length);
585
586 return hdr_len;
587
588 trunc:
589 nd_print_trunc(ndo);
590 return length;
591 }
592
593 /* an NLPID of 0xb1 indicates a 2-byte
594 * FRF.15 header
595 *
596 * 7 6 5 4 3 2 1 0
597 * +----+----+----+----+----+----+----+----+
598 * ~ Q.922 header ~
599 * +----+----+----+----+----+----+----+----+
600 * | NLPID (8 bits) | NLPID=0xb1
601 * +----+----+----+----+----+----+----+----+
602 * | B | E | C |seq. (high 4 bits) | R |
603 * +----+----+----+----+----+----+----+----+
604 * | sequence (low 8 bits) |
605 * +----+----+----+----+----+----+----+----+
606 */
607
608 #define FR_FRF15_FRAGTYPE 0x01
609
610 static void
611 frf15_print(netdissect_options *ndo,
612 const u_char *p, u_int length)
613 {
614 uint16_t sequence_num, flags;
615
616 if (length < 2)
617 goto trunc;
618 ND_TCHECK_2(p);
619
620 flags = GET_U_1(p)&MFR_BEC_MASK;
621 sequence_num = (GET_U_1(p)&0x1e)<<7 | GET_U_1(p + 1);
622
623 ND_PRINT("FRF.15, seq 0x%03x, Flags [%s],%s Fragmentation, length %u",
624 sequence_num,
625 bittok2str(frf_flag_values,"none",flags),
626 GET_U_1(p)&FR_FRF15_FRAGTYPE ? "Interface" : "End-to-End",
627 length);
628
629 /* TODO:
630 * depending on all permutations of the B, E and C bit
631 * dig as deep as we can - e.g. on the first (B) fragment
632 * there is enough payload to print the IP header
633 * on non (B) fragments it depends if the fragmentation
634 * model is end-to-end or interface based wether we want to print
635 * another Q.922 header
636 */
637 return;
638
639 trunc:
640 nd_print_trunc(ndo);
641 }
642
643 /*
644 * Q.933 decoding portion for framerelay specific.
645 */
646
647 /* Q.933 packet format
648 Format of Other Protocols
649 using Q.933 NLPID
650 +-------------------------------+
651 | Q.922 Address |
652 +---------------+---------------+
653 |Control 0x03 | NLPID 0x08 |
654 +---------------+---------------+
655 | L2 Protocol ID |
656 | octet 1 | octet 2 |
657 +-------------------------------+
658 | L3 Protocol ID |
659 | octet 2 | octet 2 |
660 +-------------------------------+
661 | Protocol Data |
662 +-------------------------------+
663 | FCS |
664 +-------------------------------+
665 */
666
667 /* L2 (Octet 1)- Call Reference Usually is 0x0 */
668
669 /*
670 * L2 (Octet 2)- Message Types definition 1 byte long.
671 */
672 /* Call Establish */
673 #define MSG_TYPE_ESC_TO_NATIONAL 0x00
674 #define MSG_TYPE_ALERT 0x01
675 #define MSG_TYPE_CALL_PROCEEDING 0x02
676 #define MSG_TYPE_CONNECT 0x07
677 #define MSG_TYPE_CONNECT_ACK 0x0F
678 #define MSG_TYPE_PROGRESS 0x03
679 #define MSG_TYPE_SETUP 0x05
680 /* Call Clear */
681 #define MSG_TYPE_DISCONNECT 0x45
682 #define MSG_TYPE_RELEASE 0x4D
683 #define MSG_TYPE_RELEASE_COMPLETE 0x5A
684 #define MSG_TYPE_RESTART 0x46
685 #define MSG_TYPE_RESTART_ACK 0x4E
686 /* Status */
687 #define MSG_TYPE_STATUS 0x7D
688 #define MSG_TYPE_STATUS_ENQ 0x75
689
690 static const struct tok fr_q933_msg_values[] = {
691 { MSG_TYPE_ESC_TO_NATIONAL, "ESC to National" },
692 { MSG_TYPE_ALERT, "Alert" },
693 { MSG_TYPE_CALL_PROCEEDING, "Call proceeding" },
694 { MSG_TYPE_CONNECT, "Connect" },
695 { MSG_TYPE_CONNECT_ACK, "Connect ACK" },
696 { MSG_TYPE_PROGRESS, "Progress" },
697 { MSG_TYPE_SETUP, "Setup" },
698 { MSG_TYPE_DISCONNECT, "Disconnect" },
699 { MSG_TYPE_RELEASE, "Release" },
700 { MSG_TYPE_RELEASE_COMPLETE, "Release Complete" },
701 { MSG_TYPE_RESTART, "Restart" },
702 { MSG_TYPE_RESTART_ACK, "Restart ACK" },
703 { MSG_TYPE_STATUS, "Status Reply" },
704 { MSG_TYPE_STATUS_ENQ, "Status Enquiry" },
705 { 0, NULL }
706 };
707
708 #define IE_IS_SINGLE_OCTET(iecode) ((iecode) & 0x80)
709 #define IE_IS_SHIFT(iecode) (((iecode) & 0xF0) == 0x90)
710 #define IE_SHIFT_IS_NON_LOCKING(iecode) ((iecode) & 0x08)
711 #define IE_SHIFT_IS_LOCKING(iecode) (!(IE_SHIFT_IS_NON_LOCKING(iecode)))
712 #define IE_SHIFT_CODESET(iecode) ((iecode) & 0x07)
713
714 #define FR_LMI_ANSI_REPORT_TYPE_IE 0x01
715 #define FR_LMI_ANSI_LINK_VERIFY_IE_91 0x19 /* details? */
716 #define FR_LMI_ANSI_LINK_VERIFY_IE 0x03
717 #define FR_LMI_ANSI_PVC_STATUS_IE 0x07
718
719 #define FR_LMI_CCITT_REPORT_TYPE_IE 0x51
720 #define FR_LMI_CCITT_LINK_VERIFY_IE 0x53
721 #define FR_LMI_CCITT_PVC_STATUS_IE 0x57
722
723 static const struct tok fr_q933_ie_values_codeset_0_5[] = {
724 { FR_LMI_ANSI_REPORT_TYPE_IE, "ANSI Report Type" },
725 { FR_LMI_ANSI_LINK_VERIFY_IE_91, "ANSI Link Verify" },
726 { FR_LMI_ANSI_LINK_VERIFY_IE, "ANSI Link Verify" },
727 { FR_LMI_ANSI_PVC_STATUS_IE, "ANSI PVC Status" },
728 { FR_LMI_CCITT_REPORT_TYPE_IE, "CCITT Report Type" },
729 { FR_LMI_CCITT_LINK_VERIFY_IE, "CCITT Link Verify" },
730 { FR_LMI_CCITT_PVC_STATUS_IE, "CCITT PVC Status" },
731 { 0, NULL }
732 };
733
734 #define FR_LMI_REPORT_TYPE_IE_FULL_STATUS 0
735 #define FR_LMI_REPORT_TYPE_IE_LINK_VERIFY 1
736 #define FR_LMI_REPORT_TYPE_IE_ASYNC_PVC 2
737
738 static const struct tok fr_lmi_report_type_ie_values[] = {
739 { FR_LMI_REPORT_TYPE_IE_FULL_STATUS, "Full Status" },
740 { FR_LMI_REPORT_TYPE_IE_LINK_VERIFY, "Link verify" },
741 { FR_LMI_REPORT_TYPE_IE_ASYNC_PVC, "Async PVC Status" },
742 { 0, NULL }
743 };
744
745 /* array of 16 codesets - currently we only support codepage 0 and 5 */
746 static const struct tok *fr_q933_ie_codesets[] = {
747 fr_q933_ie_values_codeset_0_5,
748 NULL,
749 NULL,
750 NULL,
751 NULL,
752 fr_q933_ie_values_codeset_0_5,
753 NULL,
754 NULL,
755 NULL,
756 NULL,
757 NULL,
758 NULL,
759 NULL,
760 NULL,
761 NULL,
762 NULL
763 };
764
765 static int fr_q933_print_ie_codeset_0_5(netdissect_options *ndo, u_int iecode,
766 u_int ielength, const u_char *p);
767
768 typedef int (*codeset_pr_func_t)(netdissect_options *, u_int iecode,
769 u_int ielength, const u_char *p);
770
771 /* array of 16 codesets - currently we only support codepage 0 and 5 */
772 static const codeset_pr_func_t fr_q933_print_ie_codeset[] = {
773 fr_q933_print_ie_codeset_0_5,
774 NULL,
775 NULL,
776 NULL,
777 NULL,
778 fr_q933_print_ie_codeset_0_5,
779 NULL,
780 NULL,
781 NULL,
782 NULL,
783 NULL,
784 NULL,
785 NULL,
786 NULL,
787 NULL,
788 NULL
789 };
790
791 /*
792 * ITU-T Q.933.
793 *
794 * p points to octet 2, the octet containing the length of the
795 * call reference value, so p[n] is octet n+2 ("octet X" is as
796 * used in Q.931/Q.933).
797 *
798 * XXX - actually used both for Q.931 and Q.933.
799 */
800 void
801 q933_print(netdissect_options *ndo,
802 const u_char *p, u_int length)
803 {
804 u_int olen;
805 u_int call_ref_length, i;
806 uint8_t call_ref[15]; /* maximum length - length field is 4 bits */
807 u_int msgtype;
808 u_int iecode;
809 u_int ielength;
810 u_int codeset = 0;
811 u_int is_ansi = 0;
812 u_int ie_is_known;
813 u_int non_locking_shift;
814 u_int unshift_codeset;
815
816 ndo->ndo_protocol = "q.933";
817 ND_PRINT("%s", ndo->ndo_eflag ? "" : "Q.933");
818
819 if (length == 0 || !ND_TTEST_1(p)) {
820 if (!ndo->ndo_eflag)
821 ND_PRINT(", ");
822 ND_PRINT("length %u", length);
823 goto trunc;
824 }
825
826 /*
827 * Get the length of the call reference value.
828 */
829 olen = length; /* preserve the original length for display */
830 call_ref_length = GET_U_1(p) & 0x0f;
831 p++;
832 length--;
833
834 /*
835 * Get the call reference value.
836 */
837 for (i = 0; i < call_ref_length; i++) {
838 if (length == 0 || !ND_TTEST_1(p)) {
839 if (!ndo->ndo_eflag)
840 ND_PRINT(", ");
841 ND_PRINT("length %u", olen);
842 goto trunc;
843 }
844 call_ref[i] = GET_U_1(p);
845 p++;
846 length--;
847 }
848
849 /*
850 * Get the message type.
851 */
852 if (length == 0 || !ND_TTEST_1(p)) {
853 if (!ndo->ndo_eflag)
854 ND_PRINT(", ");
855 ND_PRINT("length %u", olen);
856 goto trunc;
857 }
858 msgtype = GET_U_1(p);
859 p++;
860 length--;
861
862 /*
863 * Peek ahead to see if we start with a shift.
864 */
865 non_locking_shift = 0;
866 unshift_codeset = codeset;
867 if (length != 0) {
868 if (!ND_TTEST_1(p)) {
869 if (!ndo->ndo_eflag)
870 ND_PRINT(", ");
871 ND_PRINT("length %u", olen);
872 goto trunc;
873 }
874 iecode = GET_U_1(p);
875 if (IE_IS_SHIFT(iecode)) {
876 /*
877 * It's a shift. Skip over it.
878 */
879 p++;
880 length--;
881
882 /*
883 * Get the codeset.
884 */
885 codeset = IE_SHIFT_CODESET(iecode);
886
887 /*
888 * If it's a locking shift to codeset 5,
889 * mark this as ANSI. (XXX - 5 is actually
890 * for national variants in general, not
891 * the US variant in particular, but maybe
892 * this is more American exceptionalism. :-))
893 */
894 if (IE_SHIFT_IS_LOCKING(iecode)) {
895 /*
896 * It's a locking shift.
897 */
898 if (codeset == 5) {
899 /*
900 * It's a locking shift to
901 * codeset 5, so this is
902 * T1.617 Annex D.
903 */
904 is_ansi = 1;
905 }
906 } else {
907 /*
908 * It's a non-locking shift.
909 * Remember the current codeset, so we
910 * can revert to it after the next IE.
911 */
912 non_locking_shift = 1;
913 unshift_codeset = 0;
914 }
915 }
916 }
917
918 /* printing out header part */
919 if (!ndo->ndo_eflag)
920 ND_PRINT(", ");
921 ND_PRINT("%s, codeset %u", is_ansi ? "ANSI" : "CCITT", codeset);
922
923 if (call_ref_length != 0) {
924 ND_TCHECK_1(p);
925 if (call_ref_length > 1 || GET_U_1(p) != 0) {
926 /*
927 * Not a dummy call reference.
928 */
929 ND_PRINT(", Call Ref: 0x");
930 for (i = 0; i < call_ref_length; i++)
931 ND_PRINT("%02x", call_ref[i]);
932 }
933 }
934 if (ndo->ndo_vflag) {
935 ND_PRINT(", %s (0x%02x), length %u",
936 tok2str(fr_q933_msg_values,
937 "unknown message", msgtype),
938 msgtype,
939 olen);
940 } else {
941 ND_PRINT(", %s",
942 tok2str(fr_q933_msg_values,
943 "unknown message 0x%02x", msgtype));
944 }
945
946 /* Loop through the rest of the IEs */
947 while (length != 0) {
948 /*
949 * What's the state of any non-locking shifts?
950 */
951 if (non_locking_shift == 1) {
952 /*
953 * There's a non-locking shift in effect for
954 * this IE. Count it, so we reset the codeset
955 * before the next IE.
956 */
957 non_locking_shift = 2;
958 } else if (non_locking_shift == 2) {
959 /*
960 * Unshift.
961 */
962 codeset = unshift_codeset;
963 non_locking_shift = 0;
964 }
965
966 /*
967 * Get the first octet of the IE.
968 */
969 if (!ND_TTEST_1(p)) {
970 if (!ndo->ndo_vflag) {
971 ND_PRINT(", length %u", olen);
972 }
973 goto trunc;
974 }
975 iecode = GET_U_1(p);
976 p++;
977 length--;
978
979 /* Single-octet IE? */
980 if (IE_IS_SINGLE_OCTET(iecode)) {
981 /*
982 * Yes. Is it a shift?
983 */
984 if (IE_IS_SHIFT(iecode)) {
985 /*
986 * Yes. Is it locking?
987 */
988 if (IE_SHIFT_IS_LOCKING(iecode)) {
989 /*
990 * Yes.
991 */
992 non_locking_shift = 0;
993 } else {
994 /*
995 * No. Remember the current
996 * codeset, so we can revert
997 * to it after the next IE.
998 */
999 non_locking_shift = 1;
1000 unshift_codeset = codeset;
1001 }
1002
1003 /*
1004 * Get the codeset.
1005 */
1006 codeset = IE_SHIFT_CODESET(iecode);
1007 }
1008 } else {
1009 /*
1010 * No. Get the IE length.
1011 */
1012 if (length == 0 || !ND_TTEST_1(p)) {
1013 if (!ndo->ndo_vflag) {
1014 ND_PRINT(", length %u", olen);
1015 }
1016 goto trunc;
1017 }
1018 ielength = GET_U_1(p);
1019 p++;
1020 length--;
1021
1022 /* lets do the full IE parsing only in verbose mode
1023 * however some IEs (DLCI Status, Link Verify)
1024 * are also interesting in non-verbose mode */
1025 if (ndo->ndo_vflag) {
1026 ND_PRINT("\n\t%s IE (0x%02x), length %u: ",
1027 tok2str(fr_q933_ie_codesets[codeset],
1028 "unknown", iecode),
1029 iecode,
1030 ielength);
1031 }
1032
1033 /* sanity checks */
1034 if (iecode == 0 || ielength == 0) {
1035 return;
1036 }
1037 if (length < ielength || !ND_TTEST_LEN(p, ielength)) {
1038 if (!ndo->ndo_vflag) {
1039 ND_PRINT(", length %u", olen);
1040 }
1041 goto trunc;
1042 }
1043
1044 ie_is_known = 0;
1045 if (fr_q933_print_ie_codeset[codeset] != NULL) {
1046 ie_is_known = fr_q933_print_ie_codeset[codeset](ndo, iecode, ielength, p);
1047 }
1048
1049 if (ie_is_known) {
1050 /*
1051 * Known IE; do we want to see a hexdump
1052 * of it?
1053 */
1054 if (ndo->ndo_vflag > 1) {
1055 /* Yes. */
1056 print_unknown_data(ndo, p, "\n\t ", ielength);
1057 }
1058 } else {
1059 /*
1060 * Unknown IE; if we're printing verbosely,
1061 * print its content in hex.
1062 */
1063 if (ndo->ndo_vflag >= 1) {
1064 print_unknown_data(ndo, p, "\n\t", ielength);
1065 }
1066 }
1067
1068 length -= ielength;
1069 p += ielength;
1070 }
1071 }
1072 if (!ndo->ndo_vflag) {
1073 ND_PRINT(", length %u", olen);
1074 }
1075 return;
1076
1077 trunc:
1078 nd_print_trunc(ndo);
1079 }
1080
1081 static int
1082 fr_q933_print_ie_codeset_0_5(netdissect_options *ndo, u_int iecode,
1083 u_int ielength, const u_char *p)
1084 {
1085 u_int dlci;
1086
1087 switch (iecode) {
1088
1089 case FR_LMI_ANSI_REPORT_TYPE_IE: /* fall through */
1090 case FR_LMI_CCITT_REPORT_TYPE_IE:
1091 if (ielength < 1) {
1092 if (!ndo->ndo_vflag) {
1093 ND_PRINT(", ");
1094 }
1095 ND_PRINT("Invalid REPORT TYPE IE");
1096 return 1;
1097 }
1098 if (ndo->ndo_vflag) {
1099 ND_PRINT("%s (%u)",
1100 tok2str(fr_lmi_report_type_ie_values,"unknown",GET_U_1(p)),
1101 GET_U_1(p));
1102 }
1103 return 1;
1104
1105 case FR_LMI_ANSI_LINK_VERIFY_IE: /* fall through */
1106 case FR_LMI_CCITT_LINK_VERIFY_IE:
1107 case FR_LMI_ANSI_LINK_VERIFY_IE_91:
1108 if (!ndo->ndo_vflag) {
1109 ND_PRINT(", ");
1110 }
1111 if (ielength < 2) {
1112 ND_PRINT("Invalid LINK VERIFY IE");
1113 return 1;
1114 }
1115 ND_PRINT("TX Seq: %3d, RX Seq: %3d", GET_U_1(p), GET_U_1(p + 1));
1116 return 1;
1117
1118 case FR_LMI_ANSI_PVC_STATUS_IE: /* fall through */
1119 case FR_LMI_CCITT_PVC_STATUS_IE:
1120 if (!ndo->ndo_vflag) {
1121 ND_PRINT(", ");
1122 }
1123 /* now parse the DLCI information element. */
1124 if ((ielength < 3) ||
1125 (GET_U_1(p) & 0x80) ||
1126 ((ielength == 3) && !(GET_U_1(p + 1) & 0x80)) ||
1127 ((ielength == 4) &&
1128 ((GET_U_1(p + 1) & 0x80) || !(GET_U_1(p + 2) & 0x80))) ||
1129 ((ielength == 5) &&
1130 ((GET_U_1(p + 1) & 0x80) || (GET_U_1(p + 2) & 0x80) ||
1131 !(GET_U_1(p + 3) & 0x80))) ||
1132 (ielength > 5) ||
1133 !(GET_U_1(p + ielength - 1) & 0x80)) {
1134 ND_PRINT("Invalid DLCI in PVC STATUS IE");
1135 return 1;
1136 }
1137
1138 dlci = ((GET_U_1(p) & 0x3F) << 4) | ((GET_U_1(p + 1) & 0x78) >> 3);
1139 if (ielength == 4) {
1140 dlci = (dlci << 6) | ((GET_U_1(p + 2) & 0x7E) >> 1);
1141 }
1142 else if (ielength == 5) {
1143 dlci = (dlci << 13) | (GET_U_1(p + 2) & 0x7F) | ((GET_U_1(p + 3) & 0x7E) >> 1);
1144 }
1145
1146 ND_PRINT("DLCI %u: status %s%s", dlci,
1147 GET_U_1(p + ielength - 1) & 0x8 ? "New, " : "",
1148 GET_U_1(p + ielength - 1) & 0x2 ? "Active" : "Inactive");
1149 return 1;
1150 }
1151
1152 return 0;
1153 }