]> The Tcpdump Group git mirrors - tcpdump/blob - print-chdlc.c
more verbose printing for unknown SLARP options, misc. cleanups
[tcpdump] / print-chdlc.c
1 /*
2 * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996, 1997
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-chdlc.c,v 1.20 2002-10-06 16:35:45 hannes 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 <pcap.h>
34 #include <stdio.h>
35
36 #include "interface.h"
37 #include "addrtoname.h"
38 #include "ethertype.h"
39 #include "extract.h"
40 #include "ppp.h"
41 #include "chdlc.h"
42
43 static void chdlc_slarp_print(const u_char *, u_int);
44
45 /* Standard CHDLC printer */
46 void
47 chdlc_if_print(u_char *user _U_, const struct pcap_pkthdr *h,
48 register const u_char *p)
49 {
50 register u_int length = h->len;
51 register u_int caplen = h->caplen;
52
53 ++infodelay;
54 ts_print(&h->ts);
55
56 /*
57 * Some printers want to get back at the link level addresses,
58 * and/or check that they're not walking off the end of the packet.
59 * Rather than pass them all the way down, we set these globals.
60 */
61 packetp = p;
62 snapend = p + caplen;
63
64 chdlc_print(p, length, caplen);
65
66 putchar('\n');
67 --infodelay;
68 if (infoprint)
69 info(0);
70 }
71
72 void
73 chdlc_print(register const u_char *p, u_int length, u_int caplen)
74 {
75 const struct ip *ip;
76 u_int proto;
77
78 if (caplen < CHDLC_HDRLEN) {
79 printf("[|chdlc]");
80 return;
81 }
82
83 proto = EXTRACT_16BITS(&p[2]);
84 if (eflag) {
85 switch (p[0]) {
86 case CHDLC_UNICAST:
87 printf("unicast ");
88 break;
89 case CHDLC_BCAST:
90 printf("bcast ");
91 break;
92 default:
93 printf("0x%02x ", p[0]);
94 break;
95 }
96 printf("%d %04x: ", length, proto);
97 }
98
99 length -= CHDLC_HDRLEN;
100 ip = (const struct ip *)(p + CHDLC_HDRLEN);
101 switch (proto) {
102 case ETHERTYPE_IP:
103 ip_print((const u_char *)ip, length);
104 break;
105 #ifdef INET6
106 case ETHERTYPE_IPV6:
107 ip6_print((const u_char *)ip, length);
108 break;
109 #endif
110 case CHDLC_TYPE_SLARP:
111 chdlc_slarp_print((const u_char *)ip, length);
112 break;
113 #if 0
114 case CHDLC_TYPE_CDP:
115 chdlc_cdp_print((const u_char *)ip, length);
116 break;
117 #endif
118 case ETHERTYPE_ISO:
119 /* is the fudge byte set ? if yes lets skip a byte */
120 if (*(p+CHDLC_HDRLEN) == 0)
121 isoclns_print(p+CHDLC_HDRLEN+1, length-1, length-1, NULL, NULL);
122 else
123 isoclns_print(p+CHDLC_HDRLEN, length, length, NULL, NULL);
124 break;
125 default:
126 printf("unknown CHDLC protocol (0x%04x)", proto);
127 break;
128 }
129 if (xflag)
130 default_print((const u_char *)ip, caplen - CHDLC_HDRLEN);
131 }
132
133 struct cisco_slarp {
134 u_int32_t code;
135 #define SLARP_REQUEST 0
136 #define SLARP_REPLY 1
137 #define SLARP_KEEPALIVE 2
138 union {
139 struct {
140 struct in_addr addr;
141 struct in_addr mask;
142 u_int16_t unused[3];
143 } addr;
144 struct {
145 u_int32_t myseq;
146 u_int32_t yourseq;
147 u_int16_t rel;
148 u_int16_t t1;
149 u_int16_t t2;
150 } keep;
151 } un;
152 };
153
154 #define SLARP_LEN 18
155
156 static void
157 chdlc_slarp_print(const u_char *cp, u_int length)
158 {
159 const struct cisco_slarp *slarp;
160
161 if (length < SLARP_LEN) {
162 printf("[|slarp]");
163 return;
164 }
165
166 slarp = (const struct cisco_slarp *)cp;
167 printf("SLARP (length: %u), ",length);
168 switch (ntohl(slarp->code)) {
169 case SLARP_REQUEST:
170 printf("request");
171 /* ok we do not know it - but lets at least dump it */
172 print_unknown_data(cp+4,"\n\t",length-4);
173 break;
174 case SLARP_REPLY:
175 printf("reply %s/%s",
176 ipaddr_string(&slarp->un.addr.addr),
177 ipaddr_string(&slarp->un.addr.mask));
178 break;
179 case SLARP_KEEPALIVE:
180 printf("keepalive: mineseen=0x%08x yourseen=0x%08x ",
181 (u_int32_t)ntohl(slarp->un.keep.myseq),
182 (u_int32_t)ntohl(slarp->un.keep.yourseq));
183 printf("reliability=0x%04x t1=%d.%d",
184 ntohs(slarp->un.keep.rel), ntohs(slarp->un.keep.t1),
185 ntohs(slarp->un.keep.t2));
186 break;
187 default:
188 printf("0x%02x unknown", (u_int32_t)ntohl(slarp->code));
189 if (vflag <= 1)
190 print_unknown_data(cp+4,"\n\t",length-4);
191 break;
192 }
193
194 if (SLARP_LEN < length && vflag)
195 printf(", (trailing junk: %d bytes)", length - SLARP_LEN);
196 if (vflag > 1)
197 print_unknown_data(cp+4,"\n\t",length-4);
198 }