]> The Tcpdump Group git mirrors - tcpdump/blob - print-isoclns.c
Get rid of some includes of <net/route.h>, and empty declarations of
[tcpdump] / print-isoclns.c
1 /*
2 * Copyright (c) 1992, 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 * Original code by Matt Thomas, Digital Equipment Corporation
22 */
23
24 #ifndef lint
25 static const char rcsid[] =
26 "@(#) $Header: /tcpdump/master/tcpdump/print-isoclns.c,v 1.20 2000-10-06 04:23:12 guy Exp $ (LBL)";
27 #endif
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <sys/types.h>
34 #include <sys/time.h>
35 #include <sys/socket.h>
36
37 #include <netinet/in.h>
38
39 #include <stdio.h>
40
41 #include "interface.h"
42 #include "addrtoname.h"
43 #include "ethertype.h"
44
45 #define CLNS 129
46 #define ESIS 130
47 #define ISIS 131
48 #define NULLNS 0
49
50 static int osi_cksum(const u_char *, u_int, const u_char *, u_char *, u_char *);
51 static void esis_print(const u_char *, u_int);
52
53 void
54 isoclns_print(const u_char *p, u_int length, u_int caplen,
55 const u_char *esrc, const u_char *edst)
56 {
57 if (caplen < 1) {
58 printf("[|iso-clns] ");
59 if (!eflag)
60 printf("%s > %s",
61 etheraddr_string(esrc),
62 etheraddr_string(edst));
63 return;
64 }
65
66 switch (*p) {
67
68 case CLNS:
69 /* esis_print(&p, &length); */
70 printf("iso-clns");
71 if (!eflag)
72 (void)printf(" %s > %s",
73 etheraddr_string(esrc),
74 etheraddr_string(edst));
75 break;
76
77 case ESIS:
78 printf("iso-esis");
79 if (!eflag)
80 (void)printf(" %s > %s",
81 etheraddr_string(esrc),
82 etheraddr_string(edst));
83 esis_print(p, length);
84 return;
85
86 case ISIS:
87 printf("iso-isis");
88 if (!eflag)
89 (void)printf(" %s > %s",
90 etheraddr_string(esrc),
91 etheraddr_string(edst));
92 /* isis_print(&p, &length); */
93 (void)printf(" len=%d ", length);
94 if (caplen > 1)
95 default_print_unaligned(p, caplen);
96 break;
97
98 case NULLNS:
99 printf("iso-nullns");
100 if (!eflag)
101 (void)printf(" %s > %s",
102 etheraddr_string(esrc),
103 etheraddr_string(edst));
104 break;
105
106 default:
107 printf("iso-clns %02x", p[0]);
108 if (!eflag)
109 (void)printf(" %s > %s",
110 etheraddr_string(esrc),
111 etheraddr_string(edst));
112 (void)printf(" len=%d ", length);
113 if (caplen > 1)
114 default_print_unaligned(p, caplen);
115 break;
116 }
117 }
118
119 #define ESIS_REDIRECT 6
120 #define ESIS_ESH 2
121 #define ESIS_ISH 4
122
123 struct esis_hdr {
124 u_char version;
125 u_char reserved;
126 u_char type;
127 u_char tmo[2];
128 u_char cksum[2];
129 };
130
131 static void
132 esis_print(const u_char *p, u_int length)
133 {
134 const u_char *ep;
135 int li = p[1];
136 const struct esis_hdr *eh = (const struct esis_hdr *) &p[2];
137 u_char cksum[2];
138 u_char off[2];
139
140 if (length == 2) {
141 if (qflag)
142 printf(" bad pkt!");
143 else
144 printf(" no header at all!");
145 return;
146 }
147 ep = p + li;
148 if (li > length) {
149 if (qflag)
150 printf(" bad pkt!");
151 else
152 printf(" LI(%d) > PDU size (%d)!", li, length);
153 return;
154 }
155 if (li < sizeof(struct esis_hdr) + 2) {
156 if (qflag)
157 printf(" bad pkt!");
158 else {
159 printf(" too short for esis header %d:", li);
160 while (--length != 0)
161 printf("%02X", *p++);
162 }
163 return;
164 }
165 switch (eh->type & 0x1f) {
166
167 case ESIS_REDIRECT:
168 printf(" redirect");
169 break;
170
171 case ESIS_ESH:
172 printf(" esh");
173 break;
174
175 case ESIS_ISH:
176 printf(" ish");
177 break;
178
179 default:
180 printf(" type %d", eh->type & 0x1f);
181 break;
182 }
183 off[0] = eh->cksum[0];
184 off[1] = eh->cksum[1];
185 if (vflag && osi_cksum(p, li, eh->cksum, cksum, off)) {
186 printf(" bad cksum (got %02x%02x want %02x%02x)",
187 eh->cksum[1], eh->cksum[0], cksum[1], cksum[0]);
188 return;
189 }
190 if (eh->version != 1) {
191 printf(" unsupported version %d", eh->version);
192 return;
193 }
194 p += sizeof(*eh) + 2;
195 li -= sizeof(*eh) + 2; /* protoid * li */
196
197 switch (eh->type & 0x1f) {
198 case ESIS_REDIRECT: {
199 const u_char *dst, *snpa, *is;
200
201 dst = p; p += *p + 1;
202 if (p > snapend)
203 return;
204 printf(" %s", isonsap_string(dst));
205 snpa = p; p += *p + 1;
206 is = p; p += *p + 1;
207 if (p > snapend)
208 return;
209 if (p > ep) {
210 printf(" [bad li]");
211 return;
212 }
213 if (is[0] == 0)
214 printf(" > %s", etheraddr_string(&snpa[1]));
215 else
216 printf(" > %s", isonsap_string(is));
217 li = ep - p;
218 break;
219 }
220 #if 0
221 case ESIS_ESH:
222 printf(" esh");
223 break;
224 #endif
225 case ESIS_ISH: {
226 const u_char *is;
227
228 is = p; p += *p + 1;
229 if (p > ep) {
230 printf(" [bad li]");
231 return;
232 }
233 if (p > snapend)
234 return;
235 printf(" %s", isonsap_string(is));
236 li = ep - p;
237 break;
238 }
239
240 default:
241 (void)printf(" len=%d", length);
242 if (length && p < snapend) {
243 length = snapend - p;
244 default_print(p, length);
245 }
246 return;
247 }
248 if (vflag)
249 while (p < ep && li) {
250 int op, opli;
251 const u_char *q;
252
253 if (snapend - p < 2)
254 return;
255 if (li < 2) {
256 printf(" bad opts/li");
257 return;
258 }
259 op = *p++;
260 opli = *p++;
261 li -= 2;
262 if (opli > li) {
263 printf(" opt (%d) too long", op);
264 return;
265 }
266 li -= opli;
267 q = p;
268 p += opli;
269 if (snapend < p)
270 return;
271 if (op == 198 && opli == 2) {
272 printf(" tmo=%d", q[0] * 256 + q[1]);
273 continue;
274 }
275 printf (" %d:<", op);
276 while (--opli >= 0)
277 printf("%02x", *q++);
278 printf (">");
279 }
280 }
281
282 static int
283 osi_cksum(register const u_char *p, register u_int len,
284 const u_char *toff, u_char *cksum, u_char *off)
285 {
286 int x, y, f = (len - ((toff - p) + 1));
287 int32_t c0 = 0, c1 = 0;
288
289 if ((cksum[0] = off[0]) == 0 && (cksum[1] = off[1]) == 0)
290 return 0;
291
292 off[0] = off[1] = 0;
293 while ((int)--len >= 0) {
294 c0 += *p++;
295 c1 += c0;
296 c0 %= 255;
297 c1 %= 255;
298 }
299 x = (c0 * f - c1);
300 if (x < 0)
301 x = 255 - (-x % 255);
302 else
303 x %= 255;
304 y = -1 * (x + c0);
305 if (y < 0)
306 y = 255 - (-y % 255);
307 else
308 y %= 255;
309
310 off[0] = x;
311 off[1] = y;
312
313 return (off[0] != cksum[0] || off[1] != cksum[1]);
314 }