]> The Tcpdump Group git mirrors - tcpdump/blob - print-fr.c
47eb3ea7d738c94097ba0ceaad828ef56c4b6eb2
[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 #ifndef lint
23 static const char rcsid[] =
24 "@(#)$Header: /tcpdump/master/tcpdump/print-fr.c,v 1.2 2002-07-11 08:27:03 guy Exp $ (LBL)";
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <sys/param.h>
32 #include <sys/time.h>
33 #include <sys/socket.h>
34
35 struct mbuf;
36 struct rtentry;
37
38 #include <netinet/in.h>
39
40 #include <stdio.h>
41 #include <string.h>
42 #include <pcap.h>
43
44 #include "interface.h"
45 #include "addrtoname.h"
46 #include "ethertype.h"
47 #include "extract.h"
48
49 static void q933_print(const u_char *, int);
50
51 #define FR_CR_BIT 0x02
52 #define FR_EA_BIT(p) ((p)&0x01)
53 #define FR_FECN 0x08
54 #define FR_BECN 0x04
55 #define FR_DE 0x02
56 #define FR_DLCI(b0, b1) ((((b0)&0xFC)<<2)+(((b1)&0xF0)>>4))
57
58 /* find out how many bytes are there in a frame */
59 static int
60 fr_addr_len(const u_char *p)
61 {
62 int i=0;
63
64 while (!FR_EA_BIT(p[i]) && i++ && !FR_EA_BIT(p[i+1])) i++;
65 return (i+1);
66 }
67
68 /* the following is for framerelay */
69 #define NLPID_LEN 1 /* NLPID is one byte long */
70 #define NLPID_Q933 0x08
71 #define NLPID_LMI 0x09
72 #define NLPID_SNAP 0x80
73 #define NLPID_CLNP 0x81
74 #define NLPID_ESIS 0x82
75 #define NLPID_ISIS 0x83
76 #define NLPID_CONS 0x84
77 #define NLPID_IDRP 0x85
78 #define NLPID_X25_ESIS 0x8a
79 #define NLPID_IP 0xcc
80
81
82 static const char *fr_nlpids[256];
83
84 static void
85 init_fr_nlpids(void)
86 {
87 int i;
88 static int fr_nlpid_flag = 0;
89
90 if (!fr_nlpid_flag) {
91 for (i=0; i < 256; i++)
92 fr_nlpids[i] = NULL;
93 fr_nlpids[NLPID_Q933] = "Q.933";
94 fr_nlpids[NLPID_LMI] = "LMI";
95 fr_nlpids[NLPID_SNAP] = "SNAP";
96 fr_nlpids[NLPID_CLNP] = "CLNP";
97 fr_nlpids[NLPID_ESIS] = "ESIS";
98 fr_nlpids[NLPID_ISIS] = "ISIS";
99 fr_nlpids[NLPID_CONS] = "CONS";
100 fr_nlpids[NLPID_IDRP] = "IDRP";
101 fr_nlpids[NLPID_X25_ESIS] = "X25_ESIS";
102 fr_nlpids[NLPID_IP] = "IP";
103 }
104 fr_nlpid_flag = 1;
105 }
106
107 /* Framerelay packet structure */
108
109 /*
110 +---------------------------+
111 | flag (7E hexadecimal) |
112 +---------------------------+
113 | Q.922 Address* |
114 +-- --+
115 | |
116 +---------------------------+
117 | Control (UI = 0x03) |
118 +---------------------------+
119 | Optional Pad (0x00) |
120 +---------------------------+
121 | NLPID |
122 +---------------------------+
123 | . |
124 | . |
125 | . |
126 | Data |
127 | . |
128 | . |
129 +---------------------------+
130 | Frame Check Sequence |
131 +-- . --+
132 | (two octets) |
133 +---------------------------+
134 | flag (7E hexadecimal) |
135 +---------------------------+
136
137 * Q.922 addresses, as presently defined, are two octets and
138 contain a 10-bit DLCI. In some networks Q.922 addresses
139 may optionally be increased to three or four octets.
140
141 */
142
143 #define FR_PROTOCOL(p) fr_protocol((p))
144
145 static int
146 fr_hdrlen(const u_char *p)
147 {
148 int hlen;
149 hlen = fr_addr_len(p)+1; /* addr_len + 0x03 + padding */
150 if (p[hlen])
151 return hlen;
152 else
153 return hlen+1;
154 }
155
156 #define LAYER2_LEN(p) (fr_hdrlen((p))+NLPID_LEN)
157
158 static int
159 fr_protocol(const u_char *p)
160 {
161 int hlen;
162
163 hlen = fr_addr_len(p) + 1;
164 if (p[hlen]) /* check for padding */
165 return p[hlen];
166 else
167 return p[hlen+1];
168 }
169
170 static char *
171 fr_flags(const u_char *p)
172 {
173 static char flags[1+1+4+1+4+1+2+1];
174
175 if (p[0] & FR_CR_BIT)
176 strcpy(flags, "C");
177 else
178 strcpy(flags, "R");
179 if (p[1] & FR_FECN)
180 strcat(flags, ",FECN");
181 if (p[1] & FR_BECN)
182 strcat(flags, ",BECN");
183 if (p[1] & FR_DE)
184 strcat(flags, ",DE");
185 return flags;
186 }
187
188 static const char *
189 fr_protostring(u_int8_t proto)
190 {
191 static char buf[5+1+2+1];
192
193 if (nflag || fr_nlpids[proto] == NULL) {
194 sprintf(buf, "proto-%02x", proto);
195 return buf;
196 }
197 return fr_nlpids[proto];
198 }
199
200 static void
201 fr_hdr_print(const u_char *p, int length)
202 {
203 u_int8_t proto;
204
205 proto = FR_PROTOCOL(p);
206
207 init_fr_nlpids();
208 /* this is kinda kludge since it assumed that DLCI is two bytes. */
209 if (qflag)
210 (void)printf("DLCI-%u-%s %d: ",
211 FR_DLCI(p[0], p[1]), fr_flags(p), length);
212 else
213 (void)printf("DLCI-%u-%s %s %d: ",
214 FR_DLCI(p[0], p[1]), fr_flags(p),
215 fr_protostring(proto), length);
216 }
217
218 void
219 fr_if_print(u_char *user, const struct pcap_pkthdr *h,
220 register const u_char *p)
221 {
222 register u_int length = h->len;
223 register u_int caplen = h->caplen;
224 u_char protocol;
225 int layer2_len;
226 u_short extracted_ethertype;
227 u_int32_t orgcode;
228 register u_short et;
229
230 ts_print(&h->ts);
231
232 if (caplen < fr_hdrlen(p)) {
233 printf("[|fr]");
234 goto out;
235 }
236
237 /*
238 * Some printers want to get back at the link level addresses,
239 * and/or check that they're not walking off the end of the packet.
240 * Rather than pass them all the way down, we set these globals.
241 */
242 packetp = p;
243 snapend = p + caplen;
244
245 if (eflag)
246 fr_hdr_print(p, length);
247
248 protocol = FR_PROTOCOL(p);
249 layer2_len = LAYER2_LEN(p);
250 p += layer2_len;
251 length -= layer2_len;
252 caplen -= layer2_len;
253
254 switch (protocol) {
255
256 case NLPID_IP:
257 case ETHERTYPE_IP:
258 ip_print(p, length);
259 break;
260
261 case NLPID_CLNP:
262 case NLPID_ESIS:
263 case NLPID_ISIS:
264 isoclns_print(p, length, caplen, NULL, NULL);
265 break;
266
267 case NLPID_SNAP:
268 orgcode = EXTRACT_24BITS(p);
269 et = EXTRACT_16BITS(p + 3);
270 if (snap_print((const u_char *)(p + 5), length - 5,
271 caplen - 5, NULL, NULL,
272 &extracted_ethertype, orgcode, et, 0) == 0) {
273 /* ether_type not known, print raw packet */
274 if (!eflag)
275 fr_hdr_print(p - layer2_len, length + layer2_len);
276 if (extracted_ethertype) {
277 printf("(SNAP %s) ",
278 etherproto_string(htons(extracted_ethertype)));
279 }
280 if (!xflag && !qflag)
281 default_print(p - layer2_len, caplen + layer2_len);
282 }
283 break;
284
285 case NLPID_Q933:
286 q933_print(p, length);
287 break;
288
289 default:
290 if (!eflag)
291 fr_hdr_print(p - layer2_len, length + layer2_len);
292 if (!xflag)
293 default_print(p, caplen);
294 }
295
296 if (xflag)
297 default_print(p, caplen);
298 out:
299 putchar('\n');
300 }
301
302 /*
303 * Q.933 decoding portion for framerelay specific.
304 */
305
306 /* Q.933 packet format
307 Format of Other Protocols
308 using Q.933 NLPID
309 +-------------------------------+
310 | Q.922 Address |
311 +---------------+---------------+
312 |Control 0x03 | NLPID 0x08 |
313 +---------------+---------------+
314 | L2 Protocol ID |
315 | octet 1 | octet 2 |
316 +-------------------------------+
317 | L3 Protocol ID |
318 | octet 2 | octet 2 |
319 +-------------------------------+
320 | Protocol Data |
321 +-------------------------------+
322 | FCS |
323 +-------------------------------+
324 */
325
326 /* L2 (Octet 1)- Call Reference Usually is 0x0 */
327
328 /*
329 * L2 (Octet 2)- Message Types definition 1 byte long.
330 */
331 /* Call Establish */
332 #define MSG_TYPE_ESC_TO_NATIONAL 0x00
333 #define MSG_TYPE_ALERT 0x01
334 #define MSG_TYPE_CALL_PROCEEDING 0x02
335 #define MSG_TYPE_CONNECT 0x07
336 #define MSG_TYPE_CONNECT_ACK 0x0F
337 #define MSG_TYPE_PROGRESS 0x03
338 #define MSG_TYPE_SETUP 0x05
339 /* Call Clear */
340 #define MSG_TYPE_DISCONNECT 0x45
341 #define MSG_TYPE_RELEASE 0x4D
342 #define MSG_TYPE_RELEASE_COMPLETE 0x5A
343 #define MSG_TYPE_RESTART 0x46
344 #define MSG_TYPE_RESTART_ACK 0x4E
345 /* Status */
346 #define MSG_TYPE_STATUS 0x7D
347 #define MSG_TYPE_STATUS_ENQ 0x75
348
349 #define ONE_BYTE_IE_MASK 0xF0
350
351 /* See L2 protocol ID picture above */
352 struct q933_header {
353 u_char call_ref; /* usually is 0 for framerelay PVC */
354 u_char msg_type;
355 };
356
357 #define REPORT_TYPE_IE 0x01
358 #define LINK_VERIFY_IE_91 0x19
359 #define LINK_VERIFY_IE_94 0x03
360 #define PVC_STATUS_IE 0x07
361
362 #define MAX_IE_SIZE
363
364 struct common_ie_header {
365 u_char ie_id;
366 u_char ie_len;
367 };
368
369 #define FULL_STATUS 0
370 #define LINK_VERIFY 1
371 #define ASYNC_PVC 2
372
373 static void
374 q933_print(const u_char *p, int length)
375 {
376 struct q933_header *header = (struct q933_header *)(p+1);
377 const u_char *ptemp = p;
378 int ie_len;
379 char *decode_str, temp_str[255];
380 struct common_ie_header *ie_p;
381
382 /* printing out header part */
383 printf("Call Ref: %02x, MSG Type: %02x",
384 header->call_ref, header->msg_type);
385 switch(header->msg_type) {
386 case MSG_TYPE_STATUS:
387 decode_str = "STATUS REPLY";
388 break;
389 case MSG_TYPE_STATUS_ENQ:
390 decode_str = "STATUS ENQUIRY";
391 break;
392 default:
393 decode_str = "UNKNOWN MSG Type";
394 }
395 printf(" %s\n", decode_str);
396
397 length = length - 3;
398 ptemp = ptemp + 3;
399
400 /* Loop through the rest of IE */
401 while( length > 0 ) {
402 if (ptemp[0] & ONE_BYTE_IE_MASK) {
403 ie_len = 1;
404 printf("\t\tOne byte IE: %02x, Content %02x\n",
405 (*ptemp & 0x70)>>4, (*ptemp & 0x0F));
406 length--;
407 ptemp++;
408 }
409 else { /* Multi-byte IE */
410 ie_p = (struct common_ie_header *)ptemp;
411 switch (ie_p->ie_id) {
412 case REPORT_TYPE_IE:
413 switch(ptemp[2]) {
414 case FULL_STATUS:
415 decode_str = "FULL STATUS";
416 break;
417 case LINK_VERIFY:
418 decode_str = "LINK VERIFY";
419 break;
420 case ASYNC_PVC:
421 decode_str = "Async PVC Status";
422 break;
423 default:
424 decode_str = "Reserved Value";
425 }
426 break;
427 case LINK_VERIFY_IE_91:
428 case LINK_VERIFY_IE_94:
429 snprintf(temp_str, sizeof (temp_str), "TX Seq: %3d, RX Seq: %3d",
430 ptemp[2], ptemp[3]);
431 decode_str = temp_str;
432 break;
433 case PVC_STATUS_IE:
434 snprintf(temp_str, sizeof (temp_str), "DLCI %d: status %s %s",
435 ((ptemp[2]&0x3f)<<4)+ ((ptemp[3]&0x78)>>3),
436 ptemp[4] & 0x8 ?"new,":" ",
437 ptemp[4] & 0x2 ?"Active":"Inactive");
438 break;
439 default:
440 decode_str = "Non-decoded Value";
441 }
442 printf("\t\tIE: %02X Len: %d, %s\n",
443 ie_p->ie_id, ie_p->ie_len, decode_str);
444 length = length - ie_p->ie_len - 2;
445 ptemp = ptemp + ie_p->ie_len + 2;
446 }
447 }
448 }