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