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 *
| 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: head/contrib/tcpdump/print-arp.c 75118 2001-04-03 07:50:46Z fenner $
| 21 * $FreeBSD: head/contrib/tcpdump/print-arp.c 98527 2002-06-21 00:49:02Z fenner $
|
22 */ 23 24#ifndef lint 25static const char rcsid[] =
| 22 */ 23 24#ifndef lint 25static const char rcsid[] =
|
26 "@(#) $Header: /tcpdump/master/tcpdump/print-arp.c,v 1.49 2000/10/10 05:05:07 guy Exp $ (LBL)";
| 26 "@(#) $Header: /tcpdump/master/tcpdump/print-arp.c,v 1.51 2001/09/17 21:57:54 fenner Exp $ (LBL)";
|
27#endif 28 29#ifdef HAVE_CONFIG_H 30#include "config.h" 31#endif 32 33#include <sys/param.h> 34#include <sys/time.h> 35 36#include <netinet/in.h> 37 38#include <stdio.h> 39#include <string.h> 40 41#include "interface.h" 42#include "addrtoname.h" 43#include "ether.h" 44#include "ethertype.h" 45#include "extract.h" /* must come after interface.h */ 46 47/* 48 * Address Resolution Protocol. 49 * 50 * See RFC 826 for protocol description. ARP packets are variable 51 * in size; the arphdr structure defines the fixed-length portion. 52 * Protocol type values are the same as those for 10 Mb/s Ethernet. 53 * It is followed by the variable-sized fields ar_sha, arp_spa, 54 * arp_tha and arp_tpa in that order, according to the lengths 55 * specified. Field names used correspond to RFC 826. 56 */ 57struct arphdr { 58 u_short ar_hrd; /* format of hardware address */ 59#define ARPHRD_ETHER 1 /* ethernet hardware format */ 60#define ARPHRD_IEEE802 6 /* token-ring hardware format */
| 27#endif 28 29#ifdef HAVE_CONFIG_H 30#include "config.h" 31#endif 32 33#include <sys/param.h> 34#include <sys/time.h> 35 36#include <netinet/in.h> 37 38#include <stdio.h> 39#include <string.h> 40 41#include "interface.h" 42#include "addrtoname.h" 43#include "ether.h" 44#include "ethertype.h" 45#include "extract.h" /* must come after interface.h */ 46 47/* 48 * Address Resolution Protocol. 49 * 50 * See RFC 826 for protocol description. ARP packets are variable 51 * in size; the arphdr structure defines the fixed-length portion. 52 * Protocol type values are the same as those for 10 Mb/s Ethernet. 53 * It is followed by the variable-sized fields ar_sha, arp_spa, 54 * arp_tha and arp_tpa in that order, according to the lengths 55 * specified. Field names used correspond to RFC 826. 56 */ 57struct arphdr { 58 u_short ar_hrd; /* format of hardware address */ 59#define ARPHRD_ETHER 1 /* ethernet hardware format */ 60#define ARPHRD_IEEE802 6 /* token-ring hardware format */
|
| 61#define ARPHRD_ARCNET 7 /* arcnet hardware format */
|
61#define ARPHRD_FRELAY 15 /* frame relay hardware format */
| 62#define ARPHRD_FRELAY 15 /* frame relay hardware format */
|
| 63#define ARPHRD_STRIP 23 /* Ricochet Starmode Radio hardware format */ 64#define ARPHRD_IEEE1394 24 /* IEEE 1394 (FireWire) hardware format */
|
62 u_short ar_pro; /* format of protocol address */ 63 u_char ar_hln; /* length of hardware address */ 64 u_char ar_pln; /* length of protocol address */ 65 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
| 65 u_short ar_pro; /* format of protocol address */ 66 u_char ar_hln; /* length of hardware address */ 67 u_char ar_pln; /* length of protocol address */ 68 u_short ar_op; /* one of: */ 69#define ARPOP_REQUEST 1 /* request to resolve address */ 70#define ARPOP_REPLY 2 /* response to previous request */ 71#define ARPOP_REVREQUEST 3 /* request protocol address given hardware */ 72#define ARPOP_REVREPLY 4 /* response giving protocol address */ 73#define ARPOP_INVREQUEST 8 /* request to identify peer */ 74#define ARPOP_INVREPLY 9 /* response identifying peer */ 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 caddr_t)((ap)+1))+0) 86#define ar_spa(ap) (((const caddr_t)((ap)+1))+ (ap)->ar_hln) 87#define ar_tha(ap) (((const caddr_t)((ap)+1))+ (ap)->ar_hln+(ap)->ar_pln) 88#define ar_tpa(ap) (((const caddr_t)((ap)+1))+2*(ap)->ar_hln+(ap)->ar_pln)
|
82}; 83 84#define ARP_HDRLEN 8 85
| 89}; 90 91#define ARP_HDRLEN 8 92
|
86/* 87 * Ethernet Address Resolution Protocol. 88 * 89 * See RFC 826 for protocol description. Structure below is adapted 90 * to resolving internet addresses. Field names used correspond to 91 * RFC 826. 92 */ 93struct ether_arp { 94 struct arphdr ea_hdr; /* fixed-size header */ 95 u_char arp_sha[6]; /* sender hardware address */ 96 u_char arp_spa[4]; /* sender protocol address */ 97 u_char arp_tha[6]; /* target hardware address */ 98 u_char arp_tpa[4]; /* target protocol address */ 99}; 100#define arp_hrd ea_hdr.ar_hrd 101#define arp_pro ea_hdr.ar_pro 102#define arp_hln ea_hdr.ar_hln 103#define arp_pln ea_hdr.ar_pln 104#define arp_op ea_hdr.ar_op
| 93#define HRD(ap) ((ap)->ar_hrd) 94#define HLN(ap) ((ap)->ar_hln) 95#define PLN(ap) ((ap)->ar_pln) 96#define OP(ap) ((ap)->ar_op) 97#define PRO(ap) ((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))
|
105
| 102
|
106#define ETHER_ARP_HDRLEN (ARP_HDRLEN + 6 + 4 + 6 + 4) 107 108#define SHA(ap) ((ap)->arp_sha) 109#define THA(ap) ((ap)->arp_tha) 110#define SPA(ap) ((ap)->arp_spa) 111#define TPA(ap) ((ap)->arp_tpa) 112 113/* Compatibility */ 114#ifndef REVARP_REQUEST 115#define REVARP_REQUEST 3 116#endif 117#ifndef REVARP_REPLY 118#define REVARP_REPLY 4 119#endif 120
| |
121static u_char ezero[6]; 122 123void
| 103static u_char ezero[6]; 104 105void
|
124arp_print(register const u_char *bp, u_int length, u_int caplen)
| 106arp_print(const u_char *bp, u_int length, u_int caplen)
|
125{
| 107{
|
126 register const struct ether_arp *ap; 127 register const struct ether_header *eh; 128 register u_short pro, hrd, op;
| 108 const struct arphdr *ap; 109 u_short pro, hrd, op;
|
129
| 110
|
130 ap = (struct ether_arp *)bp; 131 if ((u_char *)(ap + 1) > snapend) { 132 printf("[|arp]"); 133 return; 134 } 135 if (length < ETHER_ARP_HDRLEN) {
| 111 ap = (const struct arphdr *)bp; 112 TCHECK(*ap); 113 if ((const u_char *)(ar_tpa(ap) + PLN(ap)) > snapend) {
|
136 (void)printf("truncated-arp");
| 114 (void)printf("truncated-arp");
|
137 default_print((u_char *)ap, length);
| 115 default_print((const u_char *)ap, length);
|
138 return; 139 } 140
| 116 return; 117 } 118
|
141 pro = EXTRACT_16BITS(&ap->arp_pro); 142 hrd = EXTRACT_16BITS(&ap->arp_hrd); 143 op = EXTRACT_16BITS(&ap->arp_op);
| 119 pro = EXTRACT_16BITS(&PRO(ap)); 120 hrd = EXTRACT_16BITS(&HRD(ap)); 121 op = EXTRACT_16BITS(&OP(ap));
|
144
| 122
|
145 if ((pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL) 146 || ap->arp_hln != sizeof(SHA(ap)) 147 || ap->arp_pln != sizeof(SPA(ap))) {
| 123 if (pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL) {
|
148 (void)printf("arp-#%d for proto #%d (%d) hardware #%d (%d)",
| 124 (void)printf("arp-#%d for proto #%d (%d) hardware #%d (%d)",
|
149 op, pro, ap->arp_pln, 150 hrd, ap->arp_hln);
| 125 op, pro, PLN(ap), hrd, HLN(ap));
|
151 return; 152 } 153 if (pro == ETHERTYPE_TRAIL) 154 (void)printf("trailer-");
| 126 return; 127 } 128 if (pro == ETHERTYPE_TRAIL) 129 (void)printf("trailer-");
|
155 eh = (struct ether_header *)packetp;
| |
156 switch (op) { 157 158 case ARPOP_REQUEST: 159 (void)printf("arp who-has %s", ipaddr_string(TPA(ap)));
| 130 switch (op) { 131 132 case ARPOP_REQUEST: 133 (void)printf("arp who-has %s", ipaddr_string(TPA(ap)));
|
160 if (memcmp((char *)ezero, (char *)THA(ap), 6) != 0) 161 (void)printf(" (%s)", etheraddr_string(THA(ap)));
| 134 if (memcmp((const char *)ezero, (const char *)THA(ap), HLN(ap)) != 0) 135 (void)printf(" (%s)", 136 linkaddr_string(THA(ap), HLN(ap)));
|
162 (void)printf(" tell %s", ipaddr_string(SPA(ap)));
| 137 (void)printf(" tell %s", ipaddr_string(SPA(ap)));
|
163 if (memcmp((char *)ESRC(eh), (char *)SHA(ap), 6) != 0) 164 (void)printf(" (%s)", etheraddr_string(SHA(ap)));
| |
165 break; 166 167 case ARPOP_REPLY: 168 (void)printf("arp reply %s", ipaddr_string(SPA(ap)));
| 138 break; 139 140 case ARPOP_REPLY: 141 (void)printf("arp reply %s", ipaddr_string(SPA(ap)));
|
169 if (memcmp((char *)ESRC(eh), (char *)SHA(ap), 6) != 0) 170 (void)printf(" (%s)", etheraddr_string(SHA(ap))); 171 (void)printf(" is-at %s", etheraddr_string(SHA(ap))); 172 if (memcmp((char *)EDST(eh), (char *)THA(ap), 6) != 0) 173 (void)printf(" (%s)", etheraddr_string(THA(ap)));
| 142 (void)printf(" is-at %s", linkaddr_string(SHA(ap), HLN(ap)));
|
174 break; 175
| 143 break; 144
|
176 case REVARP_REQUEST:
| 145 case ARPOP_REVREQUEST:
|
177 (void)printf("rarp who-is %s tell %s",
| 146 (void)printf("rarp who-is %s tell %s",
|
178 etheraddr_string(THA(ap)), 179 etheraddr_string(SHA(ap)));
| 147 linkaddr_string(THA(ap), HLN(ap)), 148 linkaddr_string(SHA(ap), HLN(ap)));
|
180 break; 181
| 149 break; 150
|
182 case REVARP_REPLY:
| 151 case ARPOP_REVREPLY:
|
183 (void)printf("rarp reply %s at %s",
| 152 (void)printf("rarp reply %s at %s",
|
184 etheraddr_string(THA(ap)),
| 153 linkaddr_string(THA(ap), HLN(ap)),
|
185 ipaddr_string(TPA(ap))); 186 break; 187 188 default: 189 (void)printf("arp-#%d", op);
| 154 ipaddr_string(TPA(ap))); 155 break; 156 157 default: 158 (void)printf("arp-#%d", op);
|
190 default_print((u_char *)ap, caplen);
| 159 default_print((const u_char *)ap, caplen);
|
191 return; 192 } 193 if (hrd != ARPHRD_ETHER) 194 printf(" hardware #%d", hrd);
| 160 return; 161 } 162 if (hrd != ARPHRD_ETHER) 163 printf(" hardware #%d", hrd);
|
| 164 return; 165trunc: 166 (void)printf("[|arp]");
|
195}
| 167}
|