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