]> The Tcpdump Group git mirrors - tcpdump/blob - print-arcnet.c
SUNRPC: Remove an unused structure
[tcpdump] / print-arcnet.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 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 * From: NetBSD: print-arcnet.c,v 1.2 2000/04/24 13:02:28 itojun Exp
22 */
23
24 /* \summary: Attached Resource Computer NETwork (ARCNET) printer */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <netdissect-stdinc.h>
31
32 #include "netdissect.h"
33 #include "extract.h"
34
35 /*
36 * from: NetBSD: if_arc.h,v 1.13 1999/11/19 20:41:19 thorpej Exp
37 */
38
39 /*
40 * Structure of a 2.5MB/s Arcnet header on the BSDs,
41 * as given to interface code.
42 */
43 struct arc_header {
44 nd_uint8_t arc_shost;
45 nd_uint8_t arc_dhost;
46 nd_uint8_t arc_type;
47 /*
48 * only present for newstyle encoding with LL fragmentation.
49 * Don't use sizeof(anything), use ARC_HDR{,NEW}LEN instead.
50 */
51 nd_uint8_t arc_flag;
52 nd_uint16_t arc_seqid;
53
54 /*
55 * only present in exception packets (arc_flag == 0xff)
56 */
57 nd_uint8_t arc_type2; /* same as arc_type */
58 nd_uint8_t arc_flag2; /* real flag value */
59 nd_uint16_t arc_seqid2; /* real seqid value */
60 };
61
62 #define ARC_HDRLEN 3
63 #define ARC_HDRNEWLEN 6
64 #define ARC_HDRNEWLEN_EXC 10
65
66 /* RFC 1051 */
67 #define ARCTYPE_IP_OLD 240 /* IP protocol */
68 #define ARCTYPE_ARP_OLD 241 /* address resolution protocol */
69
70 /* RFC 1201 */
71 #define ARCTYPE_IP 212 /* IP protocol */
72 #define ARCTYPE_ARP 213 /* address resolution protocol */
73 #define ARCTYPE_REVARP 214 /* reverse addr resolution protocol */
74
75 #define ARCTYPE_ATALK 221 /* Appletalk */
76 #define ARCTYPE_BANIAN 247 /* Banyan Vines */
77 #define ARCTYPE_IPX 250 /* Novell IPX */
78
79 #define ARCTYPE_INET6 0xc4 /* IPng */
80 #define ARCTYPE_DIAGNOSE 0x80 /* as per ANSI/ATA 878.1 */
81
82 /*
83 * Structure of a 2.5MB/s Arcnet header on Linux. Linux has
84 * an extra "offset" field when given to interface code, and
85 * never presents packets that look like exception frames.
86 */
87 struct arc_linux_header {
88 nd_uint8_t arc_shost;
89 nd_uint8_t arc_dhost;
90 nd_uint16_t arc_offset;
91 nd_uint8_t arc_type;
92 /*
93 * only present for newstyle encoding with LL fragmentation.
94 * Don't use sizeof(anything), use ARC_LINUX_HDR{,NEW}LEN
95 * instead.
96 */
97 nd_uint8_t arc_flag;
98 nd_uint16_t arc_seqid;
99 };
100
101 #define ARC_LINUX_HDRLEN 5
102 #define ARC_LINUX_HDRNEWLEN 8
103
104 static int arcnet_encap_print(netdissect_options *, u_char arctype, const u_char *p,
105 u_int length, u_int caplen);
106
107 static const struct tok arctypemap[] = {
108 { ARCTYPE_IP_OLD, "oldip" },
109 { ARCTYPE_ARP_OLD, "oldarp" },
110 { ARCTYPE_IP, "ip" },
111 { ARCTYPE_ARP, "arp" },
112 { ARCTYPE_REVARP, "rarp" },
113 { ARCTYPE_ATALK, "atalk" },
114 { ARCTYPE_BANIAN, "banyan" },
115 { ARCTYPE_IPX, "ipx" },
116 { ARCTYPE_INET6, "ipv6" },
117 { ARCTYPE_DIAGNOSE, "diag" },
118 { 0, 0 }
119 };
120
121 static inline void
122 arcnet_print(netdissect_options *ndo, const u_char *bp, u_int length, int phds,
123 int flag, u_int seqid)
124 {
125 const struct arc_header *ap;
126 const char *arctypename;
127
128
129 ap = (const struct arc_header *)bp;
130
131
132 if (ndo->ndo_qflag) {
133 ND_PRINT("%02x %02x %u: ",
134 EXTRACT_U_1(ap->arc_shost),
135 EXTRACT_U_1(ap->arc_dhost),
136 length);
137 return;
138 }
139
140 arctypename = tok2str(arctypemap, "%02x", EXTRACT_U_1(ap->arc_type));
141
142 if (!phds) {
143 ND_PRINT("%02x %02x %s %d: ",
144 EXTRACT_U_1(ap->arc_shost),
145 EXTRACT_U_1(ap->arc_dhost),
146 arctypename,
147 length);
148 return;
149 }
150
151 if (flag == 0) {
152 ND_PRINT("%02x %02x %s seqid %04x %d: ",
153 EXTRACT_U_1(ap->arc_shost),
154 EXTRACT_U_1(ap->arc_dhost),
155 arctypename, seqid,
156 length);
157 return;
158 }
159
160 if (flag & 1)
161 ND_PRINT("%02x %02x %s seqid %04x "
162 "(first of %d fragments) %d: ",
163 EXTRACT_U_1(ap->arc_shost),
164 EXTRACT_U_1(ap->arc_dhost),
165 arctypename, seqid,
166 (flag + 3) / 2, length);
167 else
168 ND_PRINT("%02x %02x %s seqid %04x "
169 "(fragment %d) %d: ",
170 EXTRACT_U_1(ap->arc_shost),
171 EXTRACT_U_1(ap->arc_dhost),
172 arctypename, seqid,
173 flag/2 + 1, length);
174 }
175
176 /*
177 * This is the top level routine of the printer. 'p' points
178 * to the ARCNET header of the packet, 'h->ts' is the timestamp,
179 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
180 * is the number of bytes actually captured.
181 */
182 u_int
183 arcnet_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
184 {
185 u_int caplen = h->caplen;
186 u_int length = h->len;
187 const struct arc_header *ap;
188
189 int phds, flag = 0, archdrlen = 0;
190 u_int seqid = 0;
191 u_char arc_type;
192
193 if (caplen < ARC_HDRLEN || length < ARC_HDRLEN) {
194 ND_PRINT("[|arcnet]");
195 return (caplen);
196 }
197
198 ap = (const struct arc_header *)p;
199 arc_type = EXTRACT_U_1(ap->arc_type);
200
201 switch (arc_type) {
202 default:
203 phds = 1;
204 break;
205 case ARCTYPE_IP_OLD:
206 case ARCTYPE_ARP_OLD:
207 case ARCTYPE_DIAGNOSE:
208 phds = 0;
209 archdrlen = ARC_HDRLEN;
210 break;
211 }
212
213 if (phds) {
214 if (caplen < ARC_HDRNEWLEN || length < ARC_HDRNEWLEN) {
215 arcnet_print(ndo, p, length, 0, 0, 0);
216 ND_PRINT("[|phds]");
217 return (caplen);
218 }
219
220 flag = EXTRACT_U_1(ap->arc_flag);
221 if (flag == 0xff) {
222 if (caplen < ARC_HDRNEWLEN_EXC || length < ARC_HDRNEWLEN_EXC) {
223 arcnet_print(ndo, p, length, 0, 0, 0);
224 ND_PRINT("[|phds extended]");
225 return (caplen);
226 }
227 flag = EXTRACT_U_1(ap->arc_flag2);
228 seqid = EXTRACT_BE_U_2(ap->arc_seqid2);
229 archdrlen = ARC_HDRNEWLEN_EXC;
230 } else {
231 seqid = EXTRACT_BE_U_2(ap->arc_seqid);
232 archdrlen = ARC_HDRNEWLEN;
233 }
234 }
235
236
237 if (ndo->ndo_eflag)
238 arcnet_print(ndo, p, length, phds, flag, seqid);
239
240 /*
241 * Go past the ARCNET header.
242 */
243 length -= archdrlen;
244 caplen -= archdrlen;
245 p += archdrlen;
246
247 if (phds && flag && (flag & 1) == 0) {
248 /*
249 * This is a middle fragment.
250 */
251 return (archdrlen);
252 }
253
254 if (!arcnet_encap_print(ndo, arc_type, p, length, caplen))
255 ND_DEFAULTPRINT(p, caplen);
256
257 return (archdrlen);
258 }
259
260 /*
261 * This is the top level routine of the printer. 'p' points
262 * to the ARCNET header of the packet, 'h->ts' is the timestamp,
263 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
264 * is the number of bytes actually captured. It is quite similar
265 * to the non-Linux style printer except that Linux doesn't ever
266 * supply packets that look like exception frames, it always supplies
267 * reassembled packets rather than raw frames, and headers have an
268 * extra "offset" field between the src/dest and packet type.
269 */
270 u_int
271 arcnet_linux_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
272 {
273 u_int caplen = h->caplen;
274 u_int length = h->len;
275 const struct arc_linux_header *ap;
276
277 int archdrlen = 0;
278 u_char arc_type;
279
280 if (caplen < ARC_LINUX_HDRLEN || length < ARC_LINUX_HDRLEN) {
281 ND_PRINT("[|arcnet]");
282 return (caplen);
283 }
284
285 ap = (const struct arc_linux_header *)p;
286 arc_type = EXTRACT_U_1(ap->arc_type);
287
288 switch (arc_type) {
289 default:
290 archdrlen = ARC_LINUX_HDRNEWLEN;
291 if (caplen < ARC_LINUX_HDRNEWLEN || length < ARC_LINUX_HDRNEWLEN) {
292 ND_PRINT("[|arcnet]");
293 return (caplen);
294 }
295 break;
296 case ARCTYPE_IP_OLD:
297 case ARCTYPE_ARP_OLD:
298 case ARCTYPE_DIAGNOSE:
299 archdrlen = ARC_LINUX_HDRLEN;
300 break;
301 }
302
303 if (ndo->ndo_eflag)
304 arcnet_print(ndo, p, length, 0, 0, 0);
305
306 /*
307 * Go past the ARCNET header.
308 */
309 length -= archdrlen;
310 caplen -= archdrlen;
311 p += archdrlen;
312
313 if (!arcnet_encap_print(ndo, arc_type, p, length, caplen))
314 ND_DEFAULTPRINT(p, caplen);
315
316 return (archdrlen);
317 }
318
319 /*
320 * Prints the packet encapsulated in an ARCnet data field,
321 * given the ARCnet system code.
322 *
323 * Returns non-zero if it can do so, zero if the system code is unknown.
324 */
325
326
327 static int
328 arcnet_encap_print(netdissect_options *ndo, u_char arctype, const u_char *p,
329 u_int length, u_int caplen)
330 {
331 switch (arctype) {
332
333 case ARCTYPE_IP_OLD:
334 case ARCTYPE_IP:
335 ip_print(ndo, p, length);
336 return (1);
337
338 case ARCTYPE_INET6:
339 ip6_print(ndo, p, length);
340 return (1);
341
342 case ARCTYPE_ARP_OLD:
343 case ARCTYPE_ARP:
344 case ARCTYPE_REVARP:
345 arp_print(ndo, p, length, caplen);
346 return (1);
347
348 case ARCTYPE_ATALK: /* XXX was this ever used? */
349 if (ndo->ndo_vflag)
350 ND_PRINT("et1 ");
351 atalk_print(ndo, p, length);
352 return (1);
353
354 case ARCTYPE_IPX:
355 ipx_print(ndo, p, length);
356 return (1);
357
358 default:
359 return (0);
360 }
361 }
362
363 /*
364 * Local Variables:
365 * c-style: bsd
366 * End:
367 */
368