Retire old subvers file
[dragonfly.git] / contrib / tcpdump-3.8.3 / 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 #ifndef lint
23 static const char rcsid[] _U_ =
24         "@(#)$Header: /tcpdump/master/tcpdump/print-fr.c,v 1.17.2.3 2003/12/15 03:37:45 guy Exp $ (LBL)";
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <tcpdump-stdinc.h>
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <pcap.h>
36
37 #include "addrtoname.h"
38 #include "interface.h"
39 #include "ethertype.h"
40 #include "extract.h"
41
42 static void lmi_print(const u_char *, u_int);
43
44 #define NLPID_LMI       0x08   /* ANSI T1.617 Annex D or ITU-T Q.933 Annex A */
45 #define NLPID_CISCO_LMI 0x09   /* The original, aka Cisco, aka Gang of Four */
46 #define NLPID_SNAP      0x80
47 #define NLPID_CLNP      0x81
48 #define NLPID_ESIS      0x82
49 #define NLPID_ISIS      0x83
50 #define NLPID_CONS      0x84
51 #define NLPID_IDRP      0x85
52 #define NLPID_X25_ESIS  0x8a
53 #define NLPID_IPV6      0x8e
54 #define NLPID_IP        0xcc
55
56 #define FR_EA_BIT       0x01
57
58
59 /* Finds out Q.922 address length, DLCI and flags. Returns 0 on success */
60 static int parse_q922_addr(const u_char *p, u_int *dlci, u_int *addr_len,
61                            char **flags_ptr)
62 {
63         static char flags[32];
64         size_t len;
65
66         if ((p[0] & FR_EA_BIT))
67                 return -1;
68
69         *flags_ptr = flags;
70         *addr_len = 2;
71         *dlci = ((p[0] & 0xFC) << 2) | ((p[1] & 0xF0) >> 4);
72
73         strcpy(flags, (p[0] & 0x02) ? "C!, " : "");
74         if (p[1] & 0x08)
75                 strcat(flags, "FECN, ");
76         if (p[1] & 0x04)
77                 strcat(flags, "BECN, ");
78         if (p[1] & 0x02)
79                 strcat(flags, "DE, ");
80
81         len = strlen(flags);
82         if (len > 1)
83                 flags[len - 2] = '\x0'; /* delete trailing comma and space */
84
85         if (p[1] & FR_EA_BIT)
86                 return 0;       /* 2-byte Q.922 address */
87
88         p += 2;
89         (*addr_len)++;          /* 3- or 4-byte Q.922 address */
90         if ((p[0] & FR_EA_BIT) == 0) {
91                 *dlci = (*dlci << 7) | (p[0] >> 1);
92                 (*addr_len)++;  /* 4-byte Q.922 address */
93                 p++;
94         }
95
96         if ((p[0] & FR_EA_BIT) == 0)
97                 return -1; /* more than 4 bytes of Q.922 address? */
98
99         if (p[0] & 0x02) {
100                 len = strlen(flags);
101                 snprintf(flags + len, sizeof(flags) - len,
102                          "%sdlcore %x", len ? ", " : "", p[0] >> 2);
103         } else
104                 *dlci = (*dlci << 6) | (p[0] >> 2);
105
106         return 0;
107 }
108
109
110 static const char *fr_nlpids[256];
111
112 static void
113 init_fr_nlpids(void)
114 {
115         int i;
116         static int fr_nlpid_flag = 0;
117
118         if (!fr_nlpid_flag) {
119                 for (i=0; i < 256; i++)
120                         fr_nlpids[i] = NULL;
121                 fr_nlpids[NLPID_LMI] = "LMI";
122                 fr_nlpids[NLPID_CISCO_LMI] = "Cisco LMI";
123                 fr_nlpids[NLPID_SNAP] = "SNAP";
124                 fr_nlpids[NLPID_CLNP] = "CLNP";
125                 fr_nlpids[NLPID_ESIS] = "ESIS";
126                 fr_nlpids[NLPID_ISIS] = "ISIS";
127                 fr_nlpids[NLPID_CONS] = "CONS";
128                 fr_nlpids[NLPID_IDRP] = "IDRP";
129                 fr_nlpids[NLPID_X25_ESIS] = "X25_ESIS";
130                 fr_nlpids[NLPID_IP] = "IP";
131         }
132         fr_nlpid_flag = 1;
133 }
134
135 /* Frame Relay packet structure, with flags and CRC removed
136
137                   +---------------------------+
138                   |       Q.922 Address*      |
139                   +--                       --+
140                   |                           |
141                   +---------------------------+
142                   | Control (UI = 0x03)       |
143                   +---------------------------+
144                   | Optional Pad      (0x00)  |
145                   +---------------------------+
146                   | NLPID                     |
147                   +---------------------------+
148                   |             .             |
149                   |             .             |
150                   |             .             |
151                   |           Data            |
152                   |             .             |
153                   |             .             |
154                   +---------------------------+
155
156            * Q.922 addresses, as presently defined, are two octets and
157              contain a 10-bit DLCI.  In some networks Q.922 addresses
158              may optionally be increased to three or four octets.
159 */
160
161 static u_int
162 fr_hdrlen(const u_char *p, u_int addr_len, u_int caplen)
163 {
164         if ((caplen > addr_len + 1 /* UI */ + 1 /* pad */) &&
165             !p[addr_len + 1] /* pad exist */)
166                 return addr_len + 1 /* UI */ + 1 /* pad */ + 1 /* NLPID */;
167         else 
168                 return addr_len + 1 /* UI */ + 1 /* NLPID */;
169 }
170
171 static const char *
172 fr_protostring(u_int8_t proto)
173 {
174         static char buf[5+1+2+1];
175
176         init_fr_nlpids();
177
178         if (nflag || fr_nlpids[proto] == NULL) {
179                 snprintf(buf, sizeof(buf), "proto %02x", proto);
180                 return buf;
181         }
182         return fr_nlpids[proto];
183 }
184
185 static void
186 fr_hdr_print(int length, u_int dlci, char *flags, u_char nlpid)
187 {
188         if (qflag)
189                 (void)printf("DLCI %u, %s%slength %d: ",
190                              dlci, flags, *flags ? ", " : "", length);
191         else
192                 (void)printf("DLCI %u, %s%s%s, length %d: ",
193                              dlci, flags, *flags ? ", " : "",
194                              fr_protostring(nlpid), length);
195 }
196
197 u_int
198 fr_if_print(const struct pcap_pkthdr *h, register const u_char *p)
199 {
200         register u_int length = h->len;
201         register u_int caplen = h->caplen;
202         u_short extracted_ethertype;
203         u_int32_t orgcode;
204         register u_short et;
205         u_int dlci;
206         int addr_len;
207         u_char nlpid;
208         u_int hdr_len;
209         char *flags;
210
211         if (caplen < 4) {       /* minimum frame header length */
212                 printf("[|fr]");
213                 return caplen;
214         }
215
216         if (parse_q922_addr(p, &dlci, &addr_len, &flags)) {
217                 printf("Invalid Q.922 address");
218                 return caplen;
219         }
220
221         hdr_len = fr_hdrlen(p, addr_len, caplen);
222
223         if (caplen < hdr_len) {
224                 printf("[|fr]");
225                 return caplen;
226         }
227
228         if (p[addr_len] != 0x03)
229                 printf("UI %02x! ", p[addr_len]);
230
231         if (!p[addr_len + 1]) { /* pad byte should be used with 3-byte Q.922 */
232                 if (addr_len != 3)
233                         printf("Pad! ");
234         } else if (addr_len == 3)
235                 printf("No pad! ");
236
237         nlpid = p[hdr_len - 1];
238
239         p += hdr_len;
240         length -= hdr_len;
241         caplen -= hdr_len;
242
243         if (eflag)
244                 fr_hdr_print(length, dlci, flags, nlpid);
245
246         switch (nlpid) {
247         case NLPID_IP:
248                 ip_print(p, length);
249                 break;
250
251 #ifdef INET6
252         case NLPID_IPV6:
253                 ip6_print(p, length);
254                 break;
255 #endif
256         case NLPID_CLNP:
257         case NLPID_ESIS:
258         case NLPID_ISIS:
259                 isoclns_print(p, length, caplen);
260                 break;
261
262         case NLPID_SNAP:
263                 orgcode = EXTRACT_24BITS(p);
264                 et = EXTRACT_16BITS(p + 3);
265                 if (snap_print((const u_char *)(p + 5), length - 5,
266                            caplen - 5, &extracted_ethertype, orgcode, et,
267                            0) == 0) {
268                         /* ether_type not known, print raw packet */
269                         if (!eflag)
270                                 fr_hdr_print(length + hdr_len,
271                                              dlci, flags, nlpid);
272                         if (extracted_ethertype) {
273                                 printf("(SNAP %s) ",
274                                etherproto_string(htons(extracted_ethertype)));
275                         }
276                         if (!xflag && !qflag)
277                                 default_print(p - hdr_len, caplen + hdr_len);
278                 }
279                 break;
280
281         case NLPID_LMI:
282                 lmi_print(p, length);
283                 break;
284
285         default:
286                 if (!eflag)
287                         fr_hdr_print(length + hdr_len,
288                                      dlci, flags, nlpid);
289                 if (!xflag)
290                         default_print(p, caplen);
291         }
292
293         return hdr_len;
294 }
295
296 /*
297  * Q.933 decoding portion for framerelay specific.
298  */
299
300 /* Q.933 packet format
301                       Format of Other Protocols   
302                           using Q.933 NLPID
303                   +-------------------------------+            
304                   |        Q.922 Address          | 
305                   +---------------+---------------+
306                   |Control  0x03  | NLPID   0x08  |        
307                   +---------------+---------------+        
308                   |          L2 Protocol ID       |
309                   | octet 1       |  octet 2      |
310                   +-------------------------------+
311                   |          L3 Protocol ID       |
312                   | octet 2       |  octet 2      |
313                   +-------------------------------+
314                   |         Protocol Data         |
315                   +-------------------------------+
316                   | FCS                           |
317                   +-------------------------------+
318  */
319
320 /* L2 (Octet 1)- Call Reference Usually is 0x0 */
321
322 /*
323  * L2 (Octet 2)- Message Types definition 1 byte long.
324  */
325 /* Call Establish */
326 #define MSG_TYPE_ESC_TO_NATIONAL  0x00
327 #define MSG_TYPE_ALERT            0x01
328 #define MSG_TYPE_CALL_PROCEEDING  0x02
329 #define MSG_TYPE_CONNECT          0x07
330 #define MSG_TYPE_CONNECT_ACK      0x0F
331 #define MSG_TYPE_PROGRESS         0x03
332 #define MSG_TYPE_SETUP            0x05
333 /* Call Clear */
334 #define MSG_TYPE_DISCONNECT       0x45
335 #define MSG_TYPE_RELEASE          0x4D
336 #define MSG_TYPE_RELEASE_COMPLETE 0x5A
337 #define MSG_TYPE_RESTART          0x46
338 #define MSG_TYPE_RESTART_ACK      0x4E
339 /* Status */
340 #define MSG_TYPE_STATUS           0x7D
341 #define MSG_TYPE_STATUS_ENQ       0x75
342
343 #define MSG_ANSI_LOCKING_SHIFT  0x95
344 #define ONE_BYTE_IE_MASK        0xF0 /* details? */
345
346 #define ANSI_REPORT_TYPE_IE     0x01
347 #define ANSI_LINK_VERIFY_IE_91  0x19 /* details? */
348 #define ANSI_LINK_VERIFY_IE     0x03
349 #define ANSI_PVC_STATUS_IE      0x07
350
351 #define CCITT_REPORT_TYPE_IE    0x51
352 #define CCITT_LINK_VERIFY_IE    0x53
353 #define CCITT_PVC_STATUS_IE     0x57
354
355 struct common_ie_header {
356     u_int8_t ie_id;
357     u_int8_t ie_len;
358 };
359
360 #define FULL_STATUS 0
361 #define LINK_VERIFY 1
362 #define ASYNC_PVC   2
363
364
365 /* Parses DLCI information element. */
366 static const char * parse_dlci_ie(const u_char *p, u_int ie_len, char *buffer,
367                             size_t buffer_len)
368 {
369         u_int dlci;
370
371         if ((ie_len < 3) ||
372             (p[0] & 0x80) ||
373             ((ie_len == 3) && !(p[1] & 0x80)) ||
374             ((ie_len == 4) && ((p[1] & 0x80) || !(p[2] & 0x80))) ||
375             ((ie_len == 5) && ((p[1] & 0x80) || (p[2] & 0x80) ||
376                                !(p[3] & 0x80))) ||
377             (ie_len > 5) ||
378             !(p[ie_len - 1] & 0x80))
379                 return "Invalid DLCI IE";
380
381         dlci = ((p[0] & 0x3F) << 4) | ((p[1] & 0x78) >> 3);
382         if (ie_len == 4)
383                 dlci = (dlci << 6) | ((p[2] & 0x7E) >> 1);
384         else if (ie_len == 5)
385                 dlci = (dlci << 13) | (p[2] & 0x7F) | ((p[3] & 0x7E) >> 1);
386
387         snprintf(buffer, buffer_len, "DLCI %d: status %s%s", dlci,
388                  p[ie_len - 1] & 0x8 ? "New, " : "",
389                  p[ie_len - 1] & 0x2 ? "Active" : "Inactive");
390
391         return buffer;
392 }
393
394
395 static void
396 lmi_print(const u_char *p, u_int length)
397 {
398         const u_char *ptemp = p;
399         const char *decode_str;
400         char temp_str[255];
401         struct common_ie_header *ie_p;
402         int is_ansi = 0;
403
404         if (length < 9) {       /* shortest: Q.933a LINK VERIFY */
405                 printf("[|lmi]");
406                 return;
407         }
408
409         if (p[2] == MSG_ANSI_LOCKING_SHIFT)
410                 is_ansi = 1;
411     
412         /* printing out header part */
413         printf(is_ansi ? "ANSI" : "CCITT");
414         if (p[0])
415                 printf(" Call Ref: %02x!", p[0]);
416
417         switch(p[1]) {
418
419         case MSG_TYPE_STATUS:
420                 printf(" STATUS REPLY\n");
421                 break;
422
423         case MSG_TYPE_STATUS_ENQ:
424                 printf(" STATUS ENQUIRY\n");
425                 break;
426
427         default:
428                 printf(" UNKNOWN MSG Type %02x\n", p[1]);
429                 break;
430         }
431
432         if (length < (u_int)(2 - is_ansi)) {
433                 printf("[|lmi]");
434                 return;
435         }
436         length -= 2 - is_ansi;
437         ptemp += 2 + is_ansi;
438         
439         /* Loop through the rest of IE */
440         while (length > 0) {
441                 ie_p = (struct common_ie_header *)ptemp;
442                 if (length < sizeof(struct common_ie_header) ||
443                     length < sizeof(struct common_ie_header) + ie_p->ie_len) {
444                         printf("[|lmi]");
445                         return;
446                 }
447
448                 if ((is_ansi && ie_p->ie_id == ANSI_REPORT_TYPE_IE) ||
449                     (!is_ansi && ie_p->ie_id == CCITT_REPORT_TYPE_IE)) {
450                         switch(ptemp[2]) {
451
452                         case FULL_STATUS:
453                                 decode_str = "FULL STATUS";
454                                 break;
455
456                         case LINK_VERIFY:
457                                 decode_str = "LINK VERIFY";
458                                 break;
459
460                         case ASYNC_PVC:
461                                 decode_str = "Async PVC Status";
462                                 break;
463
464                         default:
465                                 decode_str = "Reserved Value";
466                                 break;
467                         }
468                 } else if ((is_ansi && (ie_p->ie_id == ANSI_LINK_VERIFY_IE_91 ||
469                                         ie_p->ie_id == ANSI_LINK_VERIFY_IE)) ||
470                           (!is_ansi && ie_p->ie_id == CCITT_LINK_VERIFY_IE)) {
471                         snprintf(temp_str, sizeof(temp_str),
472                              "TX Seq: %3d, RX Seq: %3d",
473                              ptemp[2], ptemp[3]);
474                         decode_str = temp_str;
475                 } else if ((is_ansi && ie_p->ie_id == ANSI_PVC_STATUS_IE) ||
476                            (!is_ansi && ie_p->ie_id == CCITT_PVC_STATUS_IE)) {
477                         decode_str = parse_dlci_ie(ptemp + 2, ie_p->ie_len,
478                                                    temp_str, sizeof(temp_str));
479                 } else
480                         decode_str = "Non-decoded Value";                   
481
482                 printf("\t\tIE: %02X Len: %d, %s\n",
483                        ie_p->ie_id, ie_p->ie_len, decode_str);
484                 length = length - ie_p->ie_len - 2;
485                 ptemp = ptemp + ie_p->ie_len + 2;
486         }
487 }