]> The Tcpdump Group git mirrors - tcpdump/blob - print-chdlc.c
don't include pcap.h needlessly
[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 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <tcpdump-stdinc.h>
27
28 #include <stdio.h>
29
30 #include "interface.h"
31 #include "addrtoname.h"
32 #include "ethertype.h"
33 #include "extract.h"
34 #include "ppp.h"
35 #include "chdlc.h"
36
37 static void chdlc_slarp_print(const u_char *, u_int);
38
39 static const struct tok chdlc_cast_values[] = {
40 { CHDLC_UNICAST, "unicast" },
41 { CHDLC_BCAST, "bcast" },
42 { 0, NULL}
43 };
44
45
46 /* Standard CHDLC printer */
47 u_int
48 chdlc_if_print(const struct pcap_pkthdr *h, register const u_char *p)
49 {
50 register u_int length = h->len;
51 register u_int caplen = h->caplen;
52
53 if (caplen < CHDLC_HDRLEN) {
54 printf("[|chdlc]");
55 return (caplen);
56 }
57 return (chdlc_print(p,length));
58 }
59
60 u_int
61 chdlc_print(register const u_char *p, u_int length) {
62 u_int proto;
63
64 proto = EXTRACT_16BITS(&p[2]);
65 if (eflag) {
66 printf("%s, ethertype %s (0x%04x), length %u: ",
67 tok2str(chdlc_cast_values, "0x%02x", p[0]),
68 tok2str(ethertype_values, "Unknown", proto),
69 proto,
70 length);
71 }
72
73 length -= CHDLC_HDRLEN;
74 p += CHDLC_HDRLEN;
75
76 switch (proto) {
77 case ETHERTYPE_IP:
78 ip_print(gndo, p, length);
79 break;
80 #ifdef INET6
81 case ETHERTYPE_IPV6:
82 ip6_print(gndo, p, length);
83 break;
84 #endif
85 case CHDLC_TYPE_SLARP:
86 chdlc_slarp_print(p, length);
87 break;
88 #if 0
89 case CHDLC_TYPE_CDP:
90 chdlc_cdp_print(p, length);
91 break;
92 #endif
93 case ETHERTYPE_MPLS:
94 case ETHERTYPE_MPLS_MULTI:
95 mpls_print(p, length);
96 break;
97 case ETHERTYPE_ISO:
98 /* is the fudge byte set ? lets verify by spotting ISO headers */
99 if (*(p+1) == 0x81 ||
100 *(p+1) == 0x82 ||
101 *(p+1) == 0x83)
102 isoclns_print(p+1, length-1, length-1);
103 else
104 isoclns_print(p, length, length);
105 break;
106 default:
107 if (!eflag)
108 printf("unknown CHDLC protocol (0x%04x)", proto);
109 break;
110 }
111
112 return (CHDLC_HDRLEN);
113 }
114
115 /*
116 * The fixed-length portion of a SLARP packet.
117 */
118 struct cisco_slarp {
119 u_int8_t code[4];
120 #define SLARP_REQUEST 0
121 #define SLARP_REPLY 1
122 #define SLARP_KEEPALIVE 2
123 union {
124 struct {
125 u_int8_t addr[4];
126 u_int8_t mask[4];
127 } addr;
128 struct {
129 u_int8_t myseq[4];
130 u_int8_t yourseq[4];
131 u_int8_t rel[2];
132 } keep;
133 } un;
134 };
135
136 #define SLARP_MIN_LEN 14
137 #define SLARP_MAX_LEN 18
138
139 static void
140 chdlc_slarp_print(const u_char *cp, u_int length)
141 {
142 const struct cisco_slarp *slarp;
143 u_int sec,min,hrs,days;
144
145 printf("SLARP (length: %u), ",length);
146 if (length < SLARP_MIN_LEN)
147 goto trunc;
148
149 slarp = (const struct cisco_slarp *)cp;
150 TCHECK2(*slarp, SLARP_MIN_LEN);
151 switch (EXTRACT_32BITS(&slarp->code)) {
152 case SLARP_REQUEST:
153 printf("request");
154 /*
155 * At least according to William "Chops" Westfield's
156 * message in
157 *
158 * http://www.nethelp.no/net/cisco-hdlc.txt
159 *
160 * the address and mask aren't used in requests -
161 * they're just zero.
162 */
163 break;
164 case SLARP_REPLY:
165 printf("reply %s/%s",
166 ipaddr_string(&slarp->un.addr.addr),
167 ipaddr_string(&slarp->un.addr.mask));
168 break;
169 case SLARP_KEEPALIVE:
170 printf("keepalive: mineseen=0x%08x, yourseen=0x%08x, reliability=0x%04x",
171 EXTRACT_32BITS(&slarp->un.keep.myseq),
172 EXTRACT_32BITS(&slarp->un.keep.yourseq),
173 EXTRACT_16BITS(&slarp->un.keep.rel));
174
175 if (length >= SLARP_MAX_LEN) { /* uptime-stamp is optional */
176 cp += SLARP_MIN_LEN;
177 if (!TTEST2(*cp, 4))
178 goto trunc;
179 sec = EXTRACT_32BITS(cp) / 1000;
180 min = sec / 60; sec -= min * 60;
181 hrs = min / 60; min -= hrs * 60;
182 days = hrs / 24; hrs -= days * 24;
183 printf(", link uptime=%ud%uh%um%us",days,hrs,min,sec);
184 }
185 break;
186 default:
187 printf("0x%02x unknown", EXTRACT_32BITS(&slarp->code));
188 if (vflag <= 1)
189 print_unknown_data(gndo,cp+4,"\n\t",length-4);
190 break;
191 }
192
193 if (SLARP_MAX_LEN < length && vflag)
194 printf(", (trailing junk: %d bytes)", length - SLARP_MAX_LEN);
195 if (vflag > 1)
196 print_unknown_data(gndo,cp+4,"\n\t",length-4);
197 return;
198
199 trunc:
200 printf("[|slarp]");
201 }
202
203
204 /*
205 * Local Variables:
206 * c-style: whitesmith
207 * c-basic-offset: 8
208 * End:
209 */