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