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