print-arp.c revision 146778
12116Sjkh/* 22116Sjkh * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997 32116Sjkh * The Regents of the University of California. All rights reserved. 42116Sjkh * 52116Sjkh * Redistribution and use in source and binary forms, with or without 62116Sjkh * modification, are permitted provided that: (1) source code distributions 72116Sjkh * retain the above copyright notice and this paragraph in its entirety, (2) 82116Sjkh * distributions including binary code include the above copyright notice and 92116Sjkh * this paragraph in its entirety in the documentation or other materials 102116Sjkh * provided with the distribution, and (3) all advertising materials mentioning 118870Srgrimes * features or use of this software display the following acknowledgement: 122116Sjkh * ``This product includes software developed by the University of California, 132116Sjkh * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 142116Sjkh * the University nor the names of its contributors may be used to endorse 152116Sjkh * or promote products derived from this software without specific prior 16176207Sbde * written permission. 17176207Sbde * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 182116Sjkh * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 192116Sjkh * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 202116Sjkh * 212116Sjkh * $FreeBSD: head/contrib/tcpdump/print-arp.c 146778 2005-05-29 19:09:28Z sam $ 222116Sjkh */ 232116Sjkh 242116Sjkh#ifndef lint 2597413Salfredstatic const char rcsid[] _U_ = 2697413Salfred "@(#) $Header: /tcpdump/master/tcpdump/print-arp.c,v 1.64 2004/04/30 16:42:14 mcr Exp $ (LBL)"; 272116Sjkh#endif 282116Sjkh 292116Sjkh#ifdef HAVE_CONFIG_H 302116Sjkh#include "config.h" 312116Sjkh#endif 322116Sjkh 332116Sjkh#include <tcpdump-stdinc.h> 342116Sjkh 352116Sjkh#include <stdio.h> 362116Sjkh#include <string.h> 372116Sjkh 382116Sjkh#include "netdissect.h" 392116Sjkh#include "addrtoname.h" 402116Sjkh#include "ether.h" 412116Sjkh#include "ethertype.h" 42176207Sbde#include "extract.h" /* must come after interface.h */ 432116Sjkh 442116Sjkh/* 452116Sjkh * Address Resolution Protocol. 462116Sjkh * 472116Sjkh * See RFC 826 for protocol description. ARP packets are variable 482116Sjkh * in size; the arphdr structure defines the fixed-length portion. 492116Sjkh * Protocol type values are the same as those for 10 Mb/s Ethernet. 502116Sjkh * It is followed by the variable-sized fields ar_sha, arp_spa, 512116Sjkh * arp_tha and arp_tpa in that order, according to the lengths 522116Sjkh * specified. Field names used correspond to RFC 826. 532116Sjkh */ 542116Sjkhstruct arp_pkthdr { 552116Sjkh u_short ar_hrd; /* format of hardware address */ 562116Sjkh#define ARPHRD_ETHER 1 /* ethernet hardware format */ 572116Sjkh#define ARPHRD_IEEE802 6 /* token-ring hardware format */ 582116Sjkh#define ARPHRD_ARCNET 7 /* arcnet hardware format */ 592116Sjkh#define ARPHRD_FRELAY 15 /* frame relay hardware format */ 602116Sjkh#define ARPHRD_STRIP 23 /* Ricochet Starmode Radio hardware format */ 612116Sjkh#define ARPHRD_IEEE1394 24 /* IEEE 1394 (FireWire) hardware format */ 62176207Sbde u_short ar_pro; /* format of protocol address */ 632116Sjkh u_char ar_hln; /* length of hardware address */ 642116Sjkh u_char ar_pln; /* length of protocol address */ 652116Sjkh u_short ar_op; /* one of: */ 66#define ARPOP_REQUEST 1 /* request to resolve address */ 67#define ARPOP_REPLY 2 /* response to previous request */ 68#define ARPOP_REVREQUEST 3 /* request protocol address given hardware */ 69#define ARPOP_REVREPLY 4 /* response giving protocol address */ 70#define ARPOP_INVREQUEST 8 /* request to identify peer */ 71#define ARPOP_INVREPLY 9 /* response identifying peer */ 72/* 73 * The remaining fields are variable in size, 74 * according to the sizes above. 75 */ 76#ifdef COMMENT_ONLY 77 u_char ar_sha[]; /* sender hardware address */ 78 u_char ar_spa[]; /* sender protocol address */ 79 u_char ar_tha[]; /* target hardware address */ 80 u_char ar_tpa[]; /* target protocol address */ 81#endif 82#define ar_sha(ap) (((const u_char *)((ap)+1))+0) 83#define ar_spa(ap) (((const u_char *)((ap)+1))+ (ap)->ar_hln) 84#define ar_tha(ap) (((const u_char *)((ap)+1))+ (ap)->ar_hln+(ap)->ar_pln) 85#define ar_tpa(ap) (((const u_char *)((ap)+1))+2*(ap)->ar_hln+(ap)->ar_pln) 86}; 87 88#define ARP_HDRLEN 8 89 90#define HRD(ap) EXTRACT_16BITS(&(ap)->ar_hrd) 91#define HLN(ap) ((ap)->ar_hln) 92#define PLN(ap) ((ap)->ar_pln) 93#define OP(ap) EXTRACT_16BITS(&(ap)->ar_op) 94#define PRO(ap) EXTRACT_16BITS(&(ap)->ar_pro) 95#define SHA(ap) (ar_sha(ap)) 96#define SPA(ap) (ar_spa(ap)) 97#define THA(ap) (ar_tha(ap)) 98#define TPA(ap) (ar_tpa(ap)) 99 100/* 101 * ATM Address Resolution Protocol. 102 * 103 * See RFC 2225 for protocol description. ATMARP packets are similar 104 * to ARP packets, except that there are no length fields for the 105 * protocol address - instead, there are type/length fields for 106 * the ATM number and subaddress - and the hardware addresses consist 107 * of an ATM number and an ATM subaddress. 108 */ 109struct atmarp_pkthdr { 110 u_short aar_hrd; /* format of hardware address */ 111#define ARPHRD_ATM2225 19 /* ATM (RFC 2225) */ 112 u_short aar_pro; /* format of protocol address */ 113 u_char aar_shtl; /* length of source ATM number */ 114 u_char aar_sstl; /* length of source ATM subaddress */ 115#define ATMARP_IS_E164 0x40 /* bit in type/length for E.164 format */ 116#define ATMARP_LEN_MASK 0x3F /* length of {sub}address in type/length */ 117 u_short aar_op; /* same as regular ARP */ 118#define ATMARPOP_NAK 10 /* NAK */ 119 u_char aar_spln; /* length of source protocol address */ 120 u_char aar_thtl; /* length of target ATM number */ 121 u_char aar_tstl; /* length of target ATM subaddress */ 122 u_char aar_tpln; /* length of target protocol address */ 123/* 124 * The remaining fields are variable in size, 125 * according to the sizes above. 126 */ 127#ifdef COMMENT_ONLY 128 u_char aar_sha[]; /* source ATM number */ 129 u_char aar_ssa[]; /* source ATM subaddress */ 130 u_char aar_spa[]; /* sender protocol address */ 131 u_char aar_tha[]; /* target ATM number */ 132 u_char aar_tsa[]; /* target ATM subaddress */ 133 u_char aar_tpa[]; /* target protocol address */ 134#endif 135 136#define ATMHRD(ap) EXTRACT_16BITS(&(ap)->aar_hrd) 137#define ATMSHLN(ap) ((ap)->aar_shtl & ATMARP_LEN_MASK) 138#define ATMSSLN(ap) ((ap)->aar_sstl & ATMARP_LEN_MASK) 139#define ATMSPLN(ap) ((ap)->aar_spln) 140#define ATMOP(ap) EXTRACT_16BITS(&(ap)->aar_op) 141#define ATMPRO(ap) EXTRACT_16BITS(&(ap)->aar_pro) 142#define ATMTHLN(ap) ((ap)->aar_thtl & ATMARP_LEN_MASK) 143#define ATMTSLN(ap) ((ap)->aar_tstl & ATMARP_LEN_MASK) 144#define ATMTPLN(ap) ((ap)->aar_tpln) 145#define aar_sha(ap) ((const u_char *)((ap)+1)) 146#define aar_ssa(ap) (aar_sha(ap) + ATMSHLN(ap)) 147#define aar_spa(ap) (aar_ssa(ap) + ATMSSLN(ap)) 148#define aar_tha(ap) (aar_spa(ap) + ATMSPLN(ap)) 149#define aar_tsa(ap) (aar_tha(ap) + ATMTHLN(ap)) 150#define aar_tpa(ap) (aar_tsa(ap) + ATMTSLN(ap)) 151}; 152 153#define ATMSHA(ap) (aar_sha(ap)) 154#define ATMSSA(ap) (aar_ssa(ap)) 155#define ATMSPA(ap) (aar_spa(ap)) 156#define ATMTHA(ap) (aar_tha(ap)) 157#define ATMTSA(ap) (aar_tsa(ap)) 158#define ATMTPA(ap) (aar_tpa(ap)) 159 160static u_char ezero[6]; 161 162static void 163atmarp_addr_print(netdissect_options *ndo, 164 const u_char *ha, u_int ha_len, const u_char *srca, 165 u_int srca_len) 166{ 167 if (ha_len == 0) 168 ND_PRINT((ndo, "<No address>")); 169 else { 170 ND_PRINT((ndo, "%s", linkaddr_string(ha, ha_len))); 171 if (srca_len != 0) 172 ND_PRINT((ndo, ",%s", 173 linkaddr_string(srca, srca_len))); 174 } 175} 176 177static void 178atmarp_print(netdissect_options *ndo, 179 const u_char *bp, u_int length, u_int caplen) 180{ 181 const struct atmarp_pkthdr *ap; 182 u_short pro, hrd, op; 183 184 ap = (const struct atmarp_pkthdr *)bp; 185 ND_TCHECK(*ap); 186 187 hrd = ATMHRD(ap); 188 pro = ATMPRO(ap); 189 op = ATMOP(ap); 190 191 if (!ND_TTEST2(*aar_tpa(ap), ATMTPLN(ap))) { 192 ND_PRINT((ndo, "truncated-atmarp")); 193 ND_DEFAULTPRINT((const u_char *)ap, length); 194 return; 195 } 196 197 if ((pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL) || 198 ATMSPLN(ap) != 4 || ATMTPLN(ap) != 4) { 199 ND_PRINT((ndo, "atmarp-#%d for proto #%d (%d/%d) hardware #%d", 200 op, pro, ATMSPLN(ap), ATMTPLN(ap), hrd)); 201 return; 202 } 203 if (pro == ETHERTYPE_TRAIL) 204 ND_PRINT((ndo, "trailer-")); 205 switch (op) { 206 207 case ARPOP_REQUEST: 208 ND_PRINT((ndo, "arp who-has %s", ipaddr_string(ATMTPA(ap)))); 209 if (ATMTHLN(ap) != 0) { 210 ND_PRINT((ndo, " (")); 211 atmarp_addr_print(ndo, ATMTHA(ap), ATMTHLN(ap), 212 ATMTSA(ap), ATMTSLN(ap)); 213 ND_PRINT((ndo, ")")); 214 } 215 ND_PRINT((ndo, " tell %s", ipaddr_string(ATMSPA(ap)))); 216 break; 217 218 case ARPOP_REPLY: 219 ND_PRINT((ndo, "arp reply %s", ipaddr_string(ATMSPA(ap)))); 220 ND_PRINT((ndo, " is-at ")); 221 atmarp_addr_print(ndo, ATMSHA(ap), ATMSHLN(ap), ATMSSA(ap), 222 ATMSSLN(ap)); 223 break; 224 225 case ARPOP_INVREQUEST: 226 ND_PRINT((ndo, "invarp who-is ")); 227 atmarp_addr_print(ndo, ATMTHA(ap), ATMTHLN(ap), ATMTSA(ap), 228 ATMTSLN(ap)); 229 ND_PRINT((ndo, " tell ")); 230 atmarp_addr_print(ndo, ATMSHA(ap), ATMSHLN(ap), ATMSSA(ap), 231 ATMSSLN(ap)); 232 break; 233 234 case ARPOP_INVREPLY: 235 ND_PRINT((ndo, "invarp reply ")); 236 atmarp_addr_print(ndo, ATMSHA(ap), ATMSHLN(ap), ATMSSA(ap), 237 ATMSSLN(ap)); 238 ND_PRINT((ndo, " at %s", ipaddr_string(ATMSPA(ap)))); 239 break; 240 241 case ATMARPOP_NAK: 242 ND_PRINT((ndo, "nak reply for %s", 243 ipaddr_string(ATMSPA(ap)))); 244 break; 245 246 default: 247 ND_PRINT((ndo, "atmarp-#%d", op)); 248 ND_DEFAULTPRINT((const u_char *)ap, caplen); 249 return; 250 } 251 return; 252trunc: 253 ND_PRINT((ndo, "[|atmarp]")); 254} 255 256void 257arp_print(netdissect_options *ndo, 258 const u_char *bp, u_int length, u_int caplen) 259{ 260 const struct arp_pkthdr *ap; 261 u_short pro, hrd, op; 262 263 ap = (const struct arp_pkthdr *)bp; 264 ND_TCHECK(*ap); 265 hrd = HRD(ap); 266 if (hrd == ARPHRD_ATM2225) { 267 atmarp_print(ndo, bp, length, caplen); 268 return; 269 } 270 pro = PRO(ap); 271 op = OP(ap); 272 273 if (!ND_TTEST2(*ar_tpa(ap), PLN(ap))) { 274 ND_PRINT((ndo, "truncated-arp")); 275 ND_DEFAULTPRINT((const u_char *)ap, length); 276 return; 277 } 278 279 if ((pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL) || 280 PLN(ap) != 4 || HLN(ap) == 0) { 281 ND_PRINT((ndo, "arp-#%d for proto #%d (%d) hardware #%d (%d)", 282 op, pro, PLN(ap), hrd, HLN(ap))); 283 return; 284 } 285 if (pro == ETHERTYPE_TRAIL) 286 ND_PRINT((ndo, "trailer-")); 287 switch (op) { 288 289 case ARPOP_REQUEST: 290 ND_PRINT((ndo, "arp who-has %s", ipaddr_string(TPA(ap)))); 291 if (memcmp((const char *)ezero, (const char *)THA(ap), HLN(ap)) != 0) 292 ND_PRINT((ndo, " (%s)", 293 linkaddr_string(THA(ap), HLN(ap)))); 294 ND_PRINT((ndo, " tell %s", ipaddr_string(SPA(ap)))); 295 break; 296 297 case ARPOP_REPLY: 298 ND_PRINT((ndo, "arp reply %s", ipaddr_string(SPA(ap)))); 299 ND_PRINT((ndo, " is-at %s", linkaddr_string(SHA(ap), HLN(ap)))); 300 break; 301 302 case ARPOP_REVREQUEST: 303 ND_PRINT((ndo, "rarp who-is %s tell %s", 304 linkaddr_string(THA(ap), HLN(ap)), 305 linkaddr_string(SHA(ap), HLN(ap)))); 306 break; 307 308 case ARPOP_REVREPLY: 309 ND_PRINT((ndo, "rarp reply %s at %s", 310 linkaddr_string(THA(ap), HLN(ap)), 311 ipaddr_string(TPA(ap)))); 312 break; 313 314 case ARPOP_INVREQUEST: 315 ND_PRINT((ndo, "invarp who-is %s tell %s", 316 linkaddr_string(THA(ap), HLN(ap)), 317 linkaddr_string(SHA(ap), HLN(ap)))); 318 break; 319 320 case ARPOP_INVREPLY: 321 ND_PRINT((ndo,"invarp reply %s at %s", 322 linkaddr_string(THA(ap), HLN(ap)), 323 ipaddr_string(TPA(ap)))); 324 break; 325 326 default: 327 ND_PRINT((ndo, "arp-#%d", op)); 328 ND_DEFAULTPRINT((const u_char *)ap, caplen); 329 return; 330 } 331 if (hrd != ARPHRD_ETHER) 332 ND_PRINT((ndo, " hardware #%d", hrd)); 333 return; 334trunc: 335 ND_PRINT((ndo, "[|arp]")); 336} 337 338/* 339 * Local Variables: 340 * c-style: bsd 341 * End: 342 */ 343 344