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