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 22 #include <sys/cdefs.h> 23 #ifndef lint 24 __RCSID("$NetBSD: print-arp.c,v 1.8 2017/02/05 04:05:05 spz Exp $"); 25 #endif 26 27 /* \summary: Address Resolution Protocol (ARP) printer */ 28 29 #ifdef HAVE_CONFIG_H 30 #include "config.h" 31 #endif 32 33 #include <netdissect-stdinc.h> 34 35 #include <string.h> 36 37 #include "netdissect.h" 38 #include "addrtoname.h" 39 #include "ether.h" 40 #include "ethertype.h" 41 #include "extract.h" 42 43 static const char tstr[] = "[|ARP]"; 44 45 /* 46 * Address Resolution Protocol. 47 * 48 * See RFC 826 for protocol description. ARP packets are variable 49 * in size; the arphdr structure defines the fixed-length portion. 50 * Protocol type values are the same as those for 10 Mb/s Ethernet. 51 * It is followed by the variable-sized fields ar_sha, arp_spa, 52 * arp_tha and arp_tpa in that order, according to the lengths 53 * specified. Field names used correspond to RFC 826. 54 */ 55 struct arp_pkthdr { 56 u_short ar_hrd; /* format of hardware address */ 57 #define ARPHRD_ETHER 1 /* ethernet hardware format */ 58 #define ARPHRD_IEEE802 6 /* token-ring hardware format */ 59 #define ARPHRD_ARCNET 7 /* arcnet hardware format */ 60 #define ARPHRD_FRELAY 15 /* frame relay hardware format */ 61 #define ARPHRD_ATM2225 19 /* ATM (RFC 2225) */ 62 #define ARPHRD_STRIP 23 /* Ricochet Starmode Radio hardware format */ 63 #define ARPHRD_IEEE1394 24 /* IEEE 1394 (FireWire) hardware format */ 64 u_short ar_pro; /* format of protocol address */ 65 u_char ar_hln; /* length of hardware address */ 66 u_char ar_pln; /* length of protocol address */ 67 u_short ar_op; /* one of: */ 68 #define ARPOP_REQUEST 1 /* request to resolve address */ 69 #define ARPOP_REPLY 2 /* response to previous request */ 70 #define ARPOP_REVREQUEST 3 /* request protocol address given hardware */ 71 #define ARPOP_REVREPLY 4 /* response giving protocol address */ 72 #define ARPOP_INVREQUEST 8 /* request to identify peer */ 73 #define ARPOP_INVREPLY 9 /* response identifying peer */ 74 #define ARPOP_NAK 10 /* NAK - only valif for ATM ARP */ 75 76 /* 77 * The remaining fields are variable in size, 78 * according to the sizes above. 79 */ 80 #ifdef COMMENT_ONLY 81 u_char ar_sha[]; /* sender hardware address */ 82 u_char ar_spa[]; /* sender protocol address */ 83 u_char ar_tha[]; /* target hardware address */ 84 u_char ar_tpa[]; /* target protocol address */ 85 #endif 86 #define ar_sha(ap) (((const u_char *)((ap)+1))+0) 87 #define ar_spa(ap) (((const u_char *)((ap)+1))+ (ap)->ar_hln) 88 #define ar_tha(ap) (((const u_char *)((ap)+1))+ (ap)->ar_hln+(ap)->ar_pln) 89 #define ar_tpa(ap) (((const u_char *)((ap)+1))+2*(ap)->ar_hln+(ap)->ar_pln) 90 }; 91 92 #define ARP_HDRLEN 8 93 94 #define HRD(ap) EXTRACT_16BITS(&(ap)->ar_hrd) 95 #define HRD_LEN(ap) ((ap)->ar_hln) 96 #define PROTO_LEN(ap) ((ap)->ar_pln) 97 #define OP(ap) EXTRACT_16BITS(&(ap)->ar_op) 98 #define PRO(ap) EXTRACT_16BITS(&(ap)->ar_pro) 99 #define SHA(ap) (ar_sha(ap)) 100 #define SPA(ap) (ar_spa(ap)) 101 #define THA(ap) (ar_tha(ap)) 102 #define TPA(ap) (ar_tpa(ap)) 103 104 105 static const struct tok arpop_values[] = { 106 { ARPOP_REQUEST, "Request" }, 107 { ARPOP_REPLY, "Reply" }, 108 { ARPOP_REVREQUEST, "Reverse Request" }, 109 { ARPOP_REVREPLY, "Reverse Reply" }, 110 { ARPOP_INVREQUEST, "Inverse Request" }, 111 { ARPOP_INVREPLY, "Inverse Reply" }, 112 { ARPOP_NAK, "NACK Reply" }, 113 { 0, NULL } 114 }; 115 116 static const struct tok arphrd_values[] = { 117 { ARPHRD_ETHER, "Ethernet" }, 118 { ARPHRD_IEEE802, "TokenRing" }, 119 { ARPHRD_ARCNET, "ArcNet" }, 120 { ARPHRD_FRELAY, "FrameRelay" }, 121 { ARPHRD_STRIP, "Strip" }, 122 { ARPHRD_IEEE1394, "IEEE 1394" }, 123 { ARPHRD_ATM2225, "ATM" }, 124 { 0, NULL } 125 }; 126 127 /* 128 * ATM Address Resolution Protocol. 129 * 130 * See RFC 2225 for protocol description. ATMARP packets are similar 131 * to ARP packets, except that there are no length fields for the 132 * protocol address - instead, there are type/length fields for 133 * the ATM number and subaddress - and the hardware addresses consist 134 * of an ATM number and an ATM subaddress. 135 */ 136 struct atmarp_pkthdr { 137 u_short aar_hrd; /* format of hardware address */ 138 u_short aar_pro; /* format of protocol address */ 139 u_char aar_shtl; /* length of source ATM number */ 140 u_char aar_sstl; /* length of source ATM subaddress */ 141 #define ATMARP_IS_E164 0x40 /* bit in type/length for E.164 format */ 142 #define ATMARP_LEN_MASK 0x3F /* length of {sub}address in type/length */ 143 u_short aar_op; /* same as regular ARP */ 144 u_char aar_spln; /* length of source protocol address */ 145 u_char aar_thtl; /* length of target ATM number */ 146 u_char aar_tstl; /* length of target ATM subaddress */ 147 u_char aar_tpln; /* length of target protocol address */ 148 /* 149 * The remaining fields are variable in size, 150 * according to the sizes above. 151 */ 152 #ifdef COMMENT_ONLY 153 u_char aar_sha[]; /* source ATM number */ 154 u_char aar_ssa[]; /* source ATM subaddress */ 155 u_char aar_spa[]; /* sender protocol address */ 156 u_char aar_tha[]; /* target ATM number */ 157 u_char aar_tsa[]; /* target ATM subaddress */ 158 u_char aar_tpa[]; /* target protocol address */ 159 #endif 160 161 #define ATMHRD(ap) EXTRACT_16BITS(&(ap)->aar_hrd) 162 #define ATMSHRD_LEN(ap) ((ap)->aar_shtl & ATMARP_LEN_MASK) 163 #define ATMSSLN(ap) ((ap)->aar_sstl & ATMARP_LEN_MASK) 164 #define ATMSPROTO_LEN(ap) ((ap)->aar_spln) 165 #define ATMOP(ap) EXTRACT_16BITS(&(ap)->aar_op) 166 #define ATMPRO(ap) EXTRACT_16BITS(&(ap)->aar_pro) 167 #define ATMTHRD_LEN(ap) ((ap)->aar_thtl & ATMARP_LEN_MASK) 168 #define ATMTSLN(ap) ((ap)->aar_tstl & ATMARP_LEN_MASK) 169 #define ATMTPROTO_LEN(ap) ((ap)->aar_tpln) 170 #define aar_sha(ap) ((const u_char *)((ap)+1)) 171 #define aar_ssa(ap) (aar_sha(ap) + ATMSHRD_LEN(ap)) 172 #define aar_spa(ap) (aar_ssa(ap) + ATMSSLN(ap)) 173 #define aar_tha(ap) (aar_spa(ap) + ATMSPROTO_LEN(ap)) 174 #define aar_tsa(ap) (aar_tha(ap) + ATMTHRD_LEN(ap)) 175 #define aar_tpa(ap) (aar_tsa(ap) + ATMTSLN(ap)) 176 }; 177 178 #define ATMSHA(ap) (aar_sha(ap)) 179 #define ATMSSA(ap) (aar_ssa(ap)) 180 #define ATMSPA(ap) (aar_spa(ap)) 181 #define ATMTHA(ap) (aar_tha(ap)) 182 #define ATMTSA(ap) (aar_tsa(ap)) 183 #define ATMTPA(ap) (aar_tpa(ap)) 184 185 static int 186 isnonzero(const u_char *a, size_t len) 187 { 188 while (len > 0) { 189 if (*a != 0) 190 return (1); 191 a++; 192 len--; 193 } 194 return (0); 195 } 196 197 static void 198 atmarp_addr_print(netdissect_options *ndo, 199 const u_char *ha, u_int ha_len, const u_char *srca, 200 u_int srca_len) 201 { 202 if (ha_len == 0) 203 ND_PRINT((ndo, "<No address>")); 204 else { 205 ND_PRINT((ndo, "%s", linkaddr_string(ndo, ha, LINKADDR_ATM, ha_len))); 206 if (srca_len != 0) 207 ND_PRINT((ndo, ",%s", 208 linkaddr_string(ndo, srca, LINKADDR_ATM, srca_len))); 209 } 210 } 211 212 static void 213 atmarp_print(netdissect_options *ndo, 214 const u_char *bp, u_int length, u_int caplen) 215 { 216 const struct atmarp_pkthdr *ap; 217 u_short pro, hrd, op; 218 219 ap = (const struct atmarp_pkthdr *)bp; 220 ND_TCHECK(*ap); 221 222 hrd = ATMHRD(ap); 223 pro = ATMPRO(ap); 224 op = ATMOP(ap); 225 226 if (!ND_TTEST2(*aar_tpa(ap), ATMTPROTO_LEN(ap))) { 227 ND_PRINT((ndo, "%s", tstr)); 228 ND_DEFAULTPRINT((const u_char *)ap, length); 229 return; 230 } 231 232 if (!ndo->ndo_eflag) { 233 ND_PRINT((ndo, "ARP, ")); 234 } 235 236 if ((pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL) || 237 ATMSPROTO_LEN(ap) != 4 || 238 ATMTPROTO_LEN(ap) != 4 || 239 ndo->ndo_vflag) { 240 ND_PRINT((ndo, "%s, %s (len %u/%u)", 241 tok2str(arphrd_values, "Unknown Hardware (%u)", hrd), 242 tok2str(ethertype_values, "Unknown Protocol (0x%04x)", pro), 243 ATMSPROTO_LEN(ap), 244 ATMTPROTO_LEN(ap))); 245 246 /* don't know know about the address formats */ 247 if (!ndo->ndo_vflag) { 248 goto out; 249 } 250 } 251 252 /* print operation */ 253 ND_PRINT((ndo, "%s%s ", 254 ndo->ndo_vflag ? ", " : "", 255 tok2str(arpop_values, "Unknown (%u)", op))); 256 257 switch (op) { 258 259 case ARPOP_REQUEST: 260 ND_PRINT((ndo, "who-has %s", ipaddr_string(ndo, ATMTPA(ap)))); 261 if (ATMTHRD_LEN(ap) != 0) { 262 ND_PRINT((ndo, " (")); 263 atmarp_addr_print(ndo, ATMTHA(ap), ATMTHRD_LEN(ap), 264 ATMTSA(ap), ATMTSLN(ap)); 265 ND_PRINT((ndo, ")")); 266 } 267 ND_PRINT((ndo, "tell %s", ipaddr_string(ndo, ATMSPA(ap)))); 268 break; 269 270 case ARPOP_REPLY: 271 ND_PRINT((ndo, "%s is-at ", ipaddr_string(ndo, ATMSPA(ap)))); 272 atmarp_addr_print(ndo, ATMSHA(ap), ATMSHRD_LEN(ap), ATMSSA(ap), 273 ATMSSLN(ap)); 274 break; 275 276 case ARPOP_INVREQUEST: 277 ND_PRINT((ndo, "who-is ")); 278 atmarp_addr_print(ndo, ATMTHA(ap), ATMTHRD_LEN(ap), ATMTSA(ap), 279 ATMTSLN(ap)); 280 ND_PRINT((ndo, " tell ")); 281 atmarp_addr_print(ndo, ATMSHA(ap), ATMSHRD_LEN(ap), ATMSSA(ap), 282 ATMSSLN(ap)); 283 break; 284 285 case ARPOP_INVREPLY: 286 atmarp_addr_print(ndo, ATMSHA(ap), ATMSHRD_LEN(ap), ATMSSA(ap), 287 ATMSSLN(ap)); 288 ND_PRINT((ndo, "at %s", ipaddr_string(ndo, ATMSPA(ap)))); 289 break; 290 291 case ARPOP_NAK: 292 ND_PRINT((ndo, "for %s", ipaddr_string(ndo, ATMSPA(ap)))); 293 break; 294 295 default: 296 ND_DEFAULTPRINT((const u_char *)ap, caplen); 297 return; 298 } 299 300 out: 301 ND_PRINT((ndo, ", length %u", length)); 302 return; 303 304 trunc: 305 ND_PRINT((ndo, "%s", tstr)); 306 } 307 308 void 309 arp_print(netdissect_options *ndo, 310 const u_char *bp, u_int length, u_int caplen) 311 { 312 const struct arp_pkthdr *ap; 313 u_short pro, hrd, op, linkaddr; 314 315 ap = (const struct arp_pkthdr *)bp; 316 ND_TCHECK(*ap); 317 318 hrd = HRD(ap); 319 pro = PRO(ap); 320 op = OP(ap); 321 322 323 /* if its ATM then call the ATM ARP printer 324 for Frame-relay ARP most of the fields 325 are similar to Ethernet so overload the Ethernet Printer 326 and set the linkaddr type for linkaddr_string(ndo, ) accordingly */ 327 328 switch(hrd) { 329 case ARPHRD_ATM2225: 330 atmarp_print(ndo, bp, length, caplen); 331 return; 332 case ARPHRD_FRELAY: 333 linkaddr = LINKADDR_FRELAY; 334 break; 335 default: 336 linkaddr = LINKADDR_ETHER; 337 break; 338 } 339 340 if (!ND_TTEST2(*ar_tpa(ap), PROTO_LEN(ap))) { 341 ND_PRINT((ndo, "%s", tstr)); 342 ND_DEFAULTPRINT((const u_char *)ap, length); 343 return; 344 } 345 346 if (!ndo->ndo_eflag) { 347 ND_PRINT((ndo, "ARP, ")); 348 } 349 350 /* print hardware type/len and proto type/len */ 351 if ((pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL) || 352 PROTO_LEN(ap) != 4 || 353 HRD_LEN(ap) == 0 || 354 ndo->ndo_vflag) { 355 ND_PRINT((ndo, "%s (len %u), %s (len %u)", 356 tok2str(arphrd_values, "Unknown Hardware (%u)", hrd), 357 HRD_LEN(ap), 358 tok2str(ethertype_values, "Unknown Protocol (0x%04x)", pro), 359 PROTO_LEN(ap))); 360 361 /* don't know know about the address formats */ 362 if (!ndo->ndo_vflag) { 363 goto out; 364 } 365 } 366 367 /* print operation */ 368 ND_PRINT((ndo, "%s%s ", 369 ndo->ndo_vflag ? ", " : "", 370 tok2str(arpop_values, "Unknown (%u)", op))); 371 372 switch (op) { 373 374 case ARPOP_REQUEST: 375 ND_PRINT((ndo, "who-has %s", ipaddr_string(ndo, TPA(ap)))); 376 if (isnonzero((const u_char *)THA(ap), HRD_LEN(ap))) 377 ND_PRINT((ndo, " (%s)", 378 linkaddr_string(ndo, THA(ap), linkaddr, HRD_LEN(ap)))); 379 ND_PRINT((ndo, " tell %s", ipaddr_string(ndo, SPA(ap)))); 380 break; 381 382 case ARPOP_REPLY: 383 ND_PRINT((ndo, "%s is-at %s", 384 ipaddr_string(ndo, SPA(ap)), 385 linkaddr_string(ndo, SHA(ap), linkaddr, HRD_LEN(ap)))); 386 break; 387 388 case ARPOP_REVREQUEST: 389 ND_PRINT((ndo, "who-is %s tell %s", 390 linkaddr_string(ndo, THA(ap), linkaddr, HRD_LEN(ap)), 391 linkaddr_string(ndo, SHA(ap), linkaddr, HRD_LEN(ap)))); 392 break; 393 394 case ARPOP_REVREPLY: 395 ND_PRINT((ndo, "%s at %s", 396 linkaddr_string(ndo, THA(ap), linkaddr, HRD_LEN(ap)), 397 ipaddr_string(ndo, TPA(ap)))); 398 break; 399 400 case ARPOP_INVREQUEST: 401 ND_PRINT((ndo, "who-is %s tell %s", 402 linkaddr_string(ndo, THA(ap), linkaddr, HRD_LEN(ap)), 403 linkaddr_string(ndo, SHA(ap), linkaddr, HRD_LEN(ap)))); 404 break; 405 406 case ARPOP_INVREPLY: 407 ND_PRINT((ndo,"%s at %s", 408 linkaddr_string(ndo, SHA(ap), linkaddr, HRD_LEN(ap)), 409 ipaddr_string(ndo, SPA(ap)))); 410 break; 411 412 default: 413 ND_DEFAULTPRINT((const u_char *)ap, caplen); 414 return; 415 } 416 417 out: 418 ND_PRINT((ndo, ", length %u", length)); 419 420 return; 421 trunc: 422 ND_PRINT((ndo, "%s", tstr)); 423 } 424 425 /* 426 * Local Variables: 427 * c-style: bsd 428 * End: 429 */ 430 431