141c99275SPeter Avalos /*
241c99275SPeter Avalos * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
341c99275SPeter Avalos * The Regents of the University of California. All rights reserved.
441c99275SPeter Avalos *
541c99275SPeter Avalos * Redistribution and use in source and binary forms, with or without
641c99275SPeter Avalos * modification, are permitted provided that: (1) source code distributions
741c99275SPeter Avalos * retain the above copyright notice and this paragraph in its entirety, (2)
841c99275SPeter Avalos * distributions including binary code include the above copyright notice and
941c99275SPeter Avalos * this paragraph in its entirety in the documentation or other materials
1041c99275SPeter Avalos * provided with the distribution, and (3) all advertising materials mentioning
1141c99275SPeter Avalos * features or use of this software display the following acknowledgement:
1241c99275SPeter Avalos * ``This product includes software developed by the University of California,
1341c99275SPeter Avalos * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1441c99275SPeter Avalos * the University nor the names of its contributors may be used to endorse
1541c99275SPeter Avalos * or promote products derived from this software without specific prior
1641c99275SPeter Avalos * written permission.
1741c99275SPeter Avalos * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1841c99275SPeter Avalos * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1941c99275SPeter Avalos * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2041c99275SPeter Avalos *
2141c99275SPeter Avalos * From: NetBSD: print-arcnet.c,v 1.2 2000/04/24 13:02:28 itojun Exp
2241c99275SPeter Avalos */
23411677aeSAaron LI
24411677aeSAaron LI /* \summary: Attached Resource Computer NETwork (ARCNET) printer */
2541c99275SPeter Avalos
2641c99275SPeter Avalos #ifdef HAVE_CONFIG_H
27*ed775ee7SAntonio Huete Jimenez #include <config.h>
2841c99275SPeter Avalos #endif
2941c99275SPeter Avalos
30*ed775ee7SAntonio Huete Jimenez #include "netdissect-stdinc.h"
3141c99275SPeter Avalos
32411677aeSAaron LI #include "netdissect.h"
3327bfbee1SPeter Avalos #include "extract.h"
3441c99275SPeter Avalos
35411677aeSAaron LI /*
36411677aeSAaron LI * from: NetBSD: if_arc.h,v 1.13 1999/11/19 20:41:19 thorpej Exp
37411677aeSAaron LI */
38411677aeSAaron LI
39411677aeSAaron LI /*
40411677aeSAaron LI * Structure of a 2.5MB/s Arcnet header on the BSDs,
41411677aeSAaron LI * as given to interface code.
42411677aeSAaron LI */
43411677aeSAaron LI struct arc_header {
44*ed775ee7SAntonio Huete Jimenez nd_uint8_t arc_shost;
45*ed775ee7SAntonio Huete Jimenez nd_uint8_t arc_dhost;
46*ed775ee7SAntonio Huete Jimenez nd_uint8_t arc_type;
47411677aeSAaron LI /*
48411677aeSAaron LI * only present for newstyle encoding with LL fragmentation.
49411677aeSAaron LI * Don't use sizeof(anything), use ARC_HDR{,NEW}LEN instead.
50411677aeSAaron LI */
51*ed775ee7SAntonio Huete Jimenez nd_uint8_t arc_flag;
52*ed775ee7SAntonio Huete Jimenez nd_uint16_t arc_seqid;
53411677aeSAaron LI
54411677aeSAaron LI /*
55411677aeSAaron LI * only present in exception packets (arc_flag == 0xff)
56411677aeSAaron LI */
57*ed775ee7SAntonio Huete Jimenez nd_uint8_t arc_type2; /* same as arc_type */
58*ed775ee7SAntonio Huete Jimenez nd_uint8_t arc_flag2; /* real flag value */
59*ed775ee7SAntonio Huete Jimenez nd_uint16_t arc_seqid2; /* real seqid value */
60411677aeSAaron LI };
61411677aeSAaron LI
62411677aeSAaron LI #define ARC_HDRLEN 3
63411677aeSAaron LI #define ARC_HDRNEWLEN 6
64411677aeSAaron LI #define ARC_HDRNEWLEN_EXC 10
65411677aeSAaron LI
66411677aeSAaron LI /* RFC 1051 */
67411677aeSAaron LI #define ARCTYPE_IP_OLD 240 /* IP protocol */
68411677aeSAaron LI #define ARCTYPE_ARP_OLD 241 /* address resolution protocol */
69411677aeSAaron LI
70411677aeSAaron LI /* RFC 1201 */
71411677aeSAaron LI #define ARCTYPE_IP 212 /* IP protocol */
72411677aeSAaron LI #define ARCTYPE_ARP 213 /* address resolution protocol */
73411677aeSAaron LI #define ARCTYPE_REVARP 214 /* reverse addr resolution protocol */
74411677aeSAaron LI
75411677aeSAaron LI #define ARCTYPE_ATALK 221 /* Appletalk */
76411677aeSAaron LI #define ARCTYPE_BANIAN 247 /* Banyan Vines */
77411677aeSAaron LI #define ARCTYPE_IPX 250 /* Novell IPX */
78411677aeSAaron LI
79411677aeSAaron LI #define ARCTYPE_INET6 0xc4 /* IPng */
80411677aeSAaron LI #define ARCTYPE_DIAGNOSE 0x80 /* as per ANSI/ATA 878.1 */
81411677aeSAaron LI
82411677aeSAaron LI /*
83411677aeSAaron LI * Structure of a 2.5MB/s Arcnet header on Linux. Linux has
84411677aeSAaron LI * an extra "offset" field when given to interface code, and
85411677aeSAaron LI * never presents packets that look like exception frames.
86411677aeSAaron LI */
87411677aeSAaron LI struct arc_linux_header {
88*ed775ee7SAntonio Huete Jimenez nd_uint8_t arc_shost;
89*ed775ee7SAntonio Huete Jimenez nd_uint8_t arc_dhost;
90*ed775ee7SAntonio Huete Jimenez nd_uint16_t arc_offset;
91*ed775ee7SAntonio Huete Jimenez nd_uint8_t arc_type;
92411677aeSAaron LI /*
93411677aeSAaron LI * only present for newstyle encoding with LL fragmentation.
94411677aeSAaron LI * Don't use sizeof(anything), use ARC_LINUX_HDR{,NEW}LEN
95411677aeSAaron LI * instead.
96411677aeSAaron LI */
97*ed775ee7SAntonio Huete Jimenez nd_uint8_t arc_flag;
98*ed775ee7SAntonio Huete Jimenez nd_uint16_t arc_seqid;
99411677aeSAaron LI };
100411677aeSAaron LI
101411677aeSAaron LI #define ARC_LINUX_HDRLEN 5
102411677aeSAaron LI #define ARC_LINUX_HDRNEWLEN 8
103411677aeSAaron LI
104411677aeSAaron LI static int arcnet_encap_print(netdissect_options *, u_char arctype, const u_char *p,
10541c99275SPeter Avalos u_int length, u_int caplen);
10641c99275SPeter Avalos
107411677aeSAaron LI static const struct tok arctypemap[] = {
10841c99275SPeter Avalos { ARCTYPE_IP_OLD, "oldip" },
10941c99275SPeter Avalos { ARCTYPE_ARP_OLD, "oldarp" },
11041c99275SPeter Avalos { ARCTYPE_IP, "ip" },
11141c99275SPeter Avalos { ARCTYPE_ARP, "arp" },
11241c99275SPeter Avalos { ARCTYPE_REVARP, "rarp" },
11341c99275SPeter Avalos { ARCTYPE_ATALK, "atalk" },
11441c99275SPeter Avalos { ARCTYPE_BANIAN, "banyan" },
11541c99275SPeter Avalos { ARCTYPE_IPX, "ipx" },
11641c99275SPeter Avalos { ARCTYPE_INET6, "ipv6" },
11741c99275SPeter Avalos { ARCTYPE_DIAGNOSE, "diag" },
118*ed775ee7SAntonio Huete Jimenez { 0, NULL }
11941c99275SPeter Avalos };
12041c99275SPeter Avalos
121*ed775ee7SAntonio Huete Jimenez static void
arcnet_print(netdissect_options * ndo,const u_char * bp,u_int length,int phds,u_int flag,u_int seqid)122411677aeSAaron LI arcnet_print(netdissect_options *ndo, const u_char *bp, u_int length, int phds,
123*ed775ee7SAntonio Huete Jimenez u_int flag, u_int seqid)
12441c99275SPeter Avalos {
12541c99275SPeter Avalos const struct arc_header *ap;
12641c99275SPeter Avalos const char *arctypename;
12741c99275SPeter Avalos
128*ed775ee7SAntonio Huete Jimenez ndo->ndo_protocol = "arcnet";
12941c99275SPeter Avalos ap = (const struct arc_header *)bp;
13041c99275SPeter Avalos
131411677aeSAaron LI if (ndo->ndo_qflag) {
132*ed775ee7SAntonio Huete Jimenez ND_PRINT("%02x %02x %u: ",
133*ed775ee7SAntonio Huete Jimenez GET_U_1(ap->arc_shost),
134*ed775ee7SAntonio Huete Jimenez GET_U_1(ap->arc_dhost),
135*ed775ee7SAntonio Huete Jimenez length);
13641c99275SPeter Avalos return;
13741c99275SPeter Avalos }
13841c99275SPeter Avalos
139*ed775ee7SAntonio Huete Jimenez arctypename = tok2str(arctypemap, "%02x", GET_U_1(ap->arc_type));
14041c99275SPeter Avalos
14141c99275SPeter Avalos if (!phds) {
142*ed775ee7SAntonio Huete Jimenez ND_PRINT("%02x %02x %s %u: ",
143*ed775ee7SAntonio Huete Jimenez GET_U_1(ap->arc_shost),
144*ed775ee7SAntonio Huete Jimenez GET_U_1(ap->arc_dhost),
145*ed775ee7SAntonio Huete Jimenez arctypename,
146*ed775ee7SAntonio Huete Jimenez length);
14741c99275SPeter Avalos return;
14841c99275SPeter Avalos }
14941c99275SPeter Avalos
15041c99275SPeter Avalos if (flag == 0) {
151*ed775ee7SAntonio Huete Jimenez ND_PRINT("%02x %02x %s seqid %04x %u: ",
152*ed775ee7SAntonio Huete Jimenez GET_U_1(ap->arc_shost),
153*ed775ee7SAntonio Huete Jimenez GET_U_1(ap->arc_dhost),
154*ed775ee7SAntonio Huete Jimenez arctypename, seqid,
155*ed775ee7SAntonio Huete Jimenez length);
15641c99275SPeter Avalos return;
15741c99275SPeter Avalos }
15841c99275SPeter Avalos
15941c99275SPeter Avalos if (flag & 1)
160*ed775ee7SAntonio Huete Jimenez ND_PRINT("%02x %02x %s seqid %04x "
161*ed775ee7SAntonio Huete Jimenez "(first of %u fragments) %u: ",
162*ed775ee7SAntonio Huete Jimenez GET_U_1(ap->arc_shost),
163*ed775ee7SAntonio Huete Jimenez GET_U_1(ap->arc_dhost),
164*ed775ee7SAntonio Huete Jimenez arctypename, seqid,
165*ed775ee7SAntonio Huete Jimenez (flag + 3) / 2, length);
16641c99275SPeter Avalos else
167*ed775ee7SAntonio Huete Jimenez ND_PRINT("%02x %02x %s seqid %04x "
168*ed775ee7SAntonio Huete Jimenez "(fragment %u) %u: ",
169*ed775ee7SAntonio Huete Jimenez GET_U_1(ap->arc_shost),
170*ed775ee7SAntonio Huete Jimenez GET_U_1(ap->arc_dhost),
171*ed775ee7SAntonio Huete Jimenez arctypename, seqid,
172*ed775ee7SAntonio Huete Jimenez flag/2 + 1, length);
17341c99275SPeter Avalos }
17441c99275SPeter Avalos
17541c99275SPeter Avalos /*
17641c99275SPeter Avalos * This is the top level routine of the printer. 'p' points
17741c99275SPeter Avalos * to the ARCNET header of the packet, 'h->ts' is the timestamp,
17841c99275SPeter Avalos * 'h->len' is the length of the packet off the wire, and 'h->caplen'
17941c99275SPeter Avalos * is the number of bytes actually captured.
18041c99275SPeter Avalos */
181*ed775ee7SAntonio Huete Jimenez void
arcnet_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,const u_char * p)182411677aeSAaron LI arcnet_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
18341c99275SPeter Avalos {
18441c99275SPeter Avalos u_int caplen = h->caplen;
18541c99275SPeter Avalos u_int length = h->len;
18641c99275SPeter Avalos const struct arc_header *ap;
18741c99275SPeter Avalos
188*ed775ee7SAntonio Huete Jimenez int phds;
189*ed775ee7SAntonio Huete Jimenez u_int flag = 0, archdrlen = 0;
19041c99275SPeter Avalos u_int seqid = 0;
19141c99275SPeter Avalos u_char arc_type;
19241c99275SPeter Avalos
193*ed775ee7SAntonio Huete Jimenez ndo->ndo_protocol = "arcnet";
194*ed775ee7SAntonio Huete Jimenez if (caplen < ARC_HDRLEN) {
195*ed775ee7SAntonio Huete Jimenez ndo->ndo_ll_hdr_len += caplen;
196*ed775ee7SAntonio Huete Jimenez nd_trunc_longjmp(ndo);
19741c99275SPeter Avalos }
19841c99275SPeter Avalos
19941c99275SPeter Avalos ap = (const struct arc_header *)p;
200*ed775ee7SAntonio Huete Jimenez arc_type = GET_U_1(ap->arc_type);
20141c99275SPeter Avalos
20241c99275SPeter Avalos switch (arc_type) {
20341c99275SPeter Avalos default:
20441c99275SPeter Avalos phds = 1;
20541c99275SPeter Avalos break;
20641c99275SPeter Avalos case ARCTYPE_IP_OLD:
20741c99275SPeter Avalos case ARCTYPE_ARP_OLD:
20841c99275SPeter Avalos case ARCTYPE_DIAGNOSE:
20941c99275SPeter Avalos phds = 0;
21041c99275SPeter Avalos archdrlen = ARC_HDRLEN;
21141c99275SPeter Avalos break;
21241c99275SPeter Avalos }
21341c99275SPeter Avalos
21441c99275SPeter Avalos if (phds) {
215*ed775ee7SAntonio Huete Jimenez if (caplen < ARC_HDRNEWLEN) {
216411677aeSAaron LI arcnet_print(ndo, p, length, 0, 0, 0);
217*ed775ee7SAntonio Huete Jimenez ND_PRINT(" phds");
218*ed775ee7SAntonio Huete Jimenez ndo->ndo_ll_hdr_len += caplen;
219*ed775ee7SAntonio Huete Jimenez nd_trunc_longjmp(ndo);
22041c99275SPeter Avalos }
22141c99275SPeter Avalos
222*ed775ee7SAntonio Huete Jimenez flag = GET_U_1(ap->arc_flag);
223*ed775ee7SAntonio Huete Jimenez if (flag == 0xff) {
224*ed775ee7SAntonio Huete Jimenez if (caplen < ARC_HDRNEWLEN_EXC) {
225411677aeSAaron LI arcnet_print(ndo, p, length, 0, 0, 0);
226*ed775ee7SAntonio Huete Jimenez ND_PRINT(" phds extended");
227*ed775ee7SAntonio Huete Jimenez ndo->ndo_ll_hdr_len += caplen;
228*ed775ee7SAntonio Huete Jimenez nd_trunc_longjmp(ndo);
22941c99275SPeter Avalos }
230*ed775ee7SAntonio Huete Jimenez flag = GET_U_1(ap->arc_flag2);
231*ed775ee7SAntonio Huete Jimenez seqid = GET_BE_U_2(ap->arc_seqid2);
23241c99275SPeter Avalos archdrlen = ARC_HDRNEWLEN_EXC;
23341c99275SPeter Avalos } else {
234*ed775ee7SAntonio Huete Jimenez seqid = GET_BE_U_2(ap->arc_seqid);
23541c99275SPeter Avalos archdrlen = ARC_HDRNEWLEN;
23641c99275SPeter Avalos }
23741c99275SPeter Avalos }
23841c99275SPeter Avalos
23941c99275SPeter Avalos
240411677aeSAaron LI if (ndo->ndo_eflag)
241411677aeSAaron LI arcnet_print(ndo, p, length, phds, flag, seqid);
24241c99275SPeter Avalos
24341c99275SPeter Avalos /*
24441c99275SPeter Avalos * Go past the ARCNET header.
24541c99275SPeter Avalos */
24641c99275SPeter Avalos length -= archdrlen;
24741c99275SPeter Avalos caplen -= archdrlen;
24841c99275SPeter Avalos p += archdrlen;
24941c99275SPeter Avalos
25041c99275SPeter Avalos if (phds && flag && (flag & 1) == 0) {
25141c99275SPeter Avalos /*
25241c99275SPeter Avalos * This is a middle fragment.
25341c99275SPeter Avalos */
254*ed775ee7SAntonio Huete Jimenez ndo->ndo_ll_hdr_len += archdrlen;
255*ed775ee7SAntonio Huete Jimenez return;
25641c99275SPeter Avalos }
25741c99275SPeter Avalos
258411677aeSAaron LI if (!arcnet_encap_print(ndo, arc_type, p, length, caplen))
259411677aeSAaron LI ND_DEFAULTPRINT(p, caplen);
26041c99275SPeter Avalos
261*ed775ee7SAntonio Huete Jimenez ndo->ndo_ll_hdr_len += archdrlen;
26241c99275SPeter Avalos }
26341c99275SPeter Avalos
26441c99275SPeter Avalos /*
26541c99275SPeter Avalos * This is the top level routine of the printer. 'p' points
26641c99275SPeter Avalos * to the ARCNET header of the packet, 'h->ts' is the timestamp,
26741c99275SPeter Avalos * 'h->len' is the length of the packet off the wire, and 'h->caplen'
26841c99275SPeter Avalos * is the number of bytes actually captured. It is quite similar
26941c99275SPeter Avalos * to the non-Linux style printer except that Linux doesn't ever
27041c99275SPeter Avalos * supply packets that look like exception frames, it always supplies
27141c99275SPeter Avalos * reassembled packets rather than raw frames, and headers have an
27241c99275SPeter Avalos * extra "offset" field between the src/dest and packet type.
27341c99275SPeter Avalos */
274*ed775ee7SAntonio Huete Jimenez void
arcnet_linux_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,const u_char * p)275411677aeSAaron LI arcnet_linux_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
27641c99275SPeter Avalos {
27741c99275SPeter Avalos u_int caplen = h->caplen;
27841c99275SPeter Avalos u_int length = h->len;
27941c99275SPeter Avalos const struct arc_linux_header *ap;
28041c99275SPeter Avalos
28141c99275SPeter Avalos int archdrlen = 0;
28241c99275SPeter Avalos u_char arc_type;
28341c99275SPeter Avalos
284*ed775ee7SAntonio Huete Jimenez ndo->ndo_protocol = "arcnet_linux";
285*ed775ee7SAntonio Huete Jimenez if (caplen < ARC_LINUX_HDRLEN) {
286*ed775ee7SAntonio Huete Jimenez ndo->ndo_ll_hdr_len += caplen;
287*ed775ee7SAntonio Huete Jimenez nd_trunc_longjmp(ndo);
28841c99275SPeter Avalos }
28941c99275SPeter Avalos
29041c99275SPeter Avalos ap = (const struct arc_linux_header *)p;
291*ed775ee7SAntonio Huete Jimenez arc_type = GET_U_1(ap->arc_type);
29241c99275SPeter Avalos
29341c99275SPeter Avalos switch (arc_type) {
29441c99275SPeter Avalos default:
29541c99275SPeter Avalos archdrlen = ARC_LINUX_HDRNEWLEN;
296*ed775ee7SAntonio Huete Jimenez if (caplen < ARC_LINUX_HDRNEWLEN) {
297*ed775ee7SAntonio Huete Jimenez ndo->ndo_ll_hdr_len += caplen;
298*ed775ee7SAntonio Huete Jimenez nd_trunc_longjmp(ndo);
29941c99275SPeter Avalos }
30041c99275SPeter Avalos break;
30141c99275SPeter Avalos case ARCTYPE_IP_OLD:
30241c99275SPeter Avalos case ARCTYPE_ARP_OLD:
30341c99275SPeter Avalos case ARCTYPE_DIAGNOSE:
30441c99275SPeter Avalos archdrlen = ARC_LINUX_HDRLEN;
30541c99275SPeter Avalos break;
30641c99275SPeter Avalos }
30741c99275SPeter Avalos
308411677aeSAaron LI if (ndo->ndo_eflag)
309411677aeSAaron LI arcnet_print(ndo, p, length, 0, 0, 0);
31041c99275SPeter Avalos
31141c99275SPeter Avalos /*
31241c99275SPeter Avalos * Go past the ARCNET header.
31341c99275SPeter Avalos */
31441c99275SPeter Avalos length -= archdrlen;
31541c99275SPeter Avalos caplen -= archdrlen;
31641c99275SPeter Avalos p += archdrlen;
31741c99275SPeter Avalos
318411677aeSAaron LI if (!arcnet_encap_print(ndo, arc_type, p, length, caplen))
319411677aeSAaron LI ND_DEFAULTPRINT(p, caplen);
32041c99275SPeter Avalos
321*ed775ee7SAntonio Huete Jimenez ndo->ndo_ll_hdr_len += archdrlen;
32241c99275SPeter Avalos }
32341c99275SPeter Avalos
32441c99275SPeter Avalos /*
32541c99275SPeter Avalos * Prints the packet encapsulated in an ARCnet data field,
32641c99275SPeter Avalos * given the ARCnet system code.
32741c99275SPeter Avalos *
32841c99275SPeter Avalos * Returns non-zero if it can do so, zero if the system code is unknown.
32941c99275SPeter Avalos */
33041c99275SPeter Avalos
33141c99275SPeter Avalos
33241c99275SPeter Avalos static int
arcnet_encap_print(netdissect_options * ndo,u_char arctype,const u_char * p,u_int length,u_int caplen)333411677aeSAaron LI arcnet_encap_print(netdissect_options *ndo, u_char arctype, const u_char *p,
33441c99275SPeter Avalos u_int length, u_int caplen)
33541c99275SPeter Avalos {
33641c99275SPeter Avalos switch (arctype) {
33741c99275SPeter Avalos
33841c99275SPeter Avalos case ARCTYPE_IP_OLD:
33941c99275SPeter Avalos case ARCTYPE_IP:
340411677aeSAaron LI ip_print(ndo, p, length);
34141c99275SPeter Avalos return (1);
34241c99275SPeter Avalos
34341c99275SPeter Avalos case ARCTYPE_INET6:
344411677aeSAaron LI ip6_print(ndo, p, length);
34541c99275SPeter Avalos return (1);
34641c99275SPeter Avalos
34741c99275SPeter Avalos case ARCTYPE_ARP_OLD:
34841c99275SPeter Avalos case ARCTYPE_ARP:
34941c99275SPeter Avalos case ARCTYPE_REVARP:
350411677aeSAaron LI arp_print(ndo, p, length, caplen);
35141c99275SPeter Avalos return (1);
35241c99275SPeter Avalos
35341c99275SPeter Avalos case ARCTYPE_ATALK: /* XXX was this ever used? */
354411677aeSAaron LI if (ndo->ndo_vflag)
355*ed775ee7SAntonio Huete Jimenez ND_PRINT("et1 ");
356411677aeSAaron LI atalk_print(ndo, p, length);
35741c99275SPeter Avalos return (1);
35841c99275SPeter Avalos
35941c99275SPeter Avalos case ARCTYPE_IPX:
360411677aeSAaron LI ipx_print(ndo, p, length);
36141c99275SPeter Avalos return (1);
36241c99275SPeter Avalos
36341c99275SPeter Avalos default:
36441c99275SPeter Avalos return (0);
36541c99275SPeter Avalos }
36641c99275SPeter Avalos }
367