1 /* $NetBSD: arp.c,v 1.18 1997/07/07 15:52:49 drochner Exp $ */ 2 3 /* 4 * Copyright (c) 1992 Regents of the University of California. 5 * All rights reserved. 6 * 7 * This software was developed by the Computer Systems Engineering group 8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 9 * contributed to Berkeley. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#) Header: arp.c,v 1.5 93/07/15 05:52:26 leres Exp (LBL) 36 * $DragonFly: src/lib/libstand/arp.c,v 1.4 2005/12/11 02:27:26 swildner Exp $ 37 */ 38 39 #include <sys/param.h> 40 #include <sys/socket.h> 41 #include <net/if.h> 42 #include <netinet/in.h> 43 #include <netinet/if_ether.h> 44 45 #include <netinet/in_systm.h> 46 47 #include <string.h> 48 49 #include "stand.h" 50 #include "net.h" 51 52 /* Cache stuff */ 53 #define ARP_NUM 8 /* need at most 3 arp entries */ 54 55 struct arp_list { 56 struct in_addr addr; 57 u_char ea[6]; 58 } arp_list[ARP_NUM] = { 59 /* XXX - net order `INADDR_BROADCAST' must be a constant */ 60 { {0xffffffff}, BA } 61 }; 62 int arp_num = 1; 63 64 /* Local forwards */ 65 static ssize_t arpsend(struct iodesc *, void *, size_t); 66 static ssize_t arprecv(struct iodesc *, void *, size_t, time_t); 67 68 /* Broadcast an ARP packet, asking who has addr on interface d */ 69 u_char * 70 arpwhohas(struct iodesc *d, struct in_addr addr) 71 { 72 int i; 73 struct ether_arp *ah; 74 struct arp_list *al; 75 struct { 76 struct ether_header eh; 77 struct { 78 struct ether_arp arp; 79 u_char pad[18]; /* 60 - sizeof(...) */ 80 } data; 81 } wbuf; 82 struct { 83 struct ether_header eh; 84 struct { 85 struct ether_arp arp; 86 u_char pad[24]; /* extra space */ 87 } data; 88 } rbuf; 89 90 /* Try for cached answer first */ 91 for (i = 0, al = arp_list; i < arp_num; ++i, ++al) 92 if (addr.s_addr == al->addr.s_addr) 93 return (al->ea); 94 95 /* Don't overflow cache */ 96 if (arp_num > ARP_NUM - 1) { 97 arp_num = 1; /* recycle */ 98 printf("arpwhohas: overflowed arp_list!\n"); 99 } 100 101 #ifdef ARP_DEBUG 102 if (debug) 103 printf("arpwhohas: send request for %s\n", inet_ntoa(addr)); 104 #endif 105 106 bzero((char*)&wbuf.data, sizeof(wbuf.data)); 107 ah = &wbuf.data.arp; 108 ah->arp_hrd = htons(ARPHRD_ETHER); 109 ah->arp_pro = htons(ETHERTYPE_IP); 110 ah->arp_hln = sizeof(ah->arp_sha); /* hardware address length */ 111 ah->arp_pln = sizeof(ah->arp_spa); /* protocol address length */ 112 ah->arp_op = htons(ARPOP_REQUEST); 113 MACPY(d->myea, ah->arp_sha); 114 bcopy(&d->myip, ah->arp_spa, sizeof(ah->arp_spa)); 115 /* Leave zeros in arp_tha */ 116 bcopy(&addr, ah->arp_tpa, sizeof(ah->arp_tpa)); 117 118 /* Store ip address in cache (incomplete entry). */ 119 al->addr = addr; 120 121 i = sendrecv(d, 122 arpsend, &wbuf.data, sizeof(wbuf.data), 123 arprecv, &rbuf.data, sizeof(rbuf.data)); 124 if (i == -1) { 125 panic("arp: no response for %s\n", 126 inet_ntoa(addr)); 127 } 128 129 /* Store ethernet address in cache */ 130 ah = &rbuf.data.arp; 131 #ifdef ARP_DEBUG 132 if (debug) { 133 printf("arp: response from %s\n", 134 ether_sprintf(rbuf.eh.ether_shost)); 135 printf("arp: cacheing %s --> %s\n", 136 inet_ntoa(addr), ether_sprintf(ah->arp_sha)); 137 } 138 #endif 139 MACPY(ah->arp_sha, al->ea); 140 ++arp_num; 141 142 return (al->ea); 143 } 144 145 static ssize_t 146 arpsend(struct iodesc *d, void *pkt, size_t len) 147 { 148 149 #ifdef ARP_DEBUG 150 if (debug) 151 printf("arpsend: called\n"); 152 #endif 153 154 return (sendether(d, pkt, len, bcea, ETHERTYPE_ARP)); 155 } 156 157 /* 158 * Returns 0 if this is the packet we're waiting for 159 * else -1 (and errno == 0) 160 */ 161 static ssize_t 162 arprecv(struct iodesc *d, void *pkt, size_t len, time_t tleft) 163 { 164 ssize_t n; 165 struct ether_arp *ah; 166 u_int16_t etype; /* host order */ 167 168 #ifdef ARP_DEBUG 169 if (debug) 170 printf("arprecv: "); 171 #endif 172 173 n = readether(d, pkt, len, tleft, &etype); 174 errno = 0; /* XXX */ 175 if (n == -1 || n < sizeof(struct ether_arp)) { 176 #ifdef ARP_DEBUG 177 if (debug) 178 printf("bad len=%d\n", n); 179 #endif 180 return (-1); 181 } 182 183 if (etype != ETHERTYPE_ARP) { 184 #ifdef ARP_DEBUG 185 if (debug) 186 printf("not arp type=%d\n", etype); 187 #endif 188 return (-1); 189 } 190 191 /* Ethernet address now checked in readether() */ 192 193 ah = (struct ether_arp *)pkt; 194 if (ah->arp_hrd != htons(ARPHRD_ETHER) || 195 ah->arp_pro != htons(ETHERTYPE_IP) || 196 ah->arp_hln != sizeof(ah->arp_sha) || 197 ah->arp_pln != sizeof(ah->arp_spa) ) 198 { 199 #ifdef ARP_DEBUG 200 if (debug) 201 printf("bad hrd/pro/hln/pln\n"); 202 #endif 203 return (-1); 204 } 205 206 if (ah->arp_op == htons(ARPOP_REQUEST)) { 207 #ifdef ARP_DEBUG 208 if (debug) 209 printf("is request\n"); 210 #endif 211 arp_reply(d, ah); 212 return (-1); 213 } 214 215 if (ah->arp_op != htons(ARPOP_REPLY)) { 216 #ifdef ARP_DEBUG 217 if (debug) 218 printf("not ARP reply\n"); 219 #endif 220 return (-1); 221 } 222 223 /* Is the reply from the source we want? */ 224 if (bcmp(&arp_list[arp_num].addr, 225 ah->arp_spa, sizeof(ah->arp_spa))) 226 { 227 #ifdef ARP_DEBUG 228 if (debug) 229 printf("unwanted address\n"); 230 #endif 231 return (-1); 232 } 233 /* We don't care who the reply was sent to. */ 234 235 /* We have our answer. */ 236 #ifdef ARP_DEBUG 237 if (debug) 238 printf("got it\n"); 239 #endif 240 return (n); 241 } 242 243 /* 244 * Convert an ARP request into a reply and send it. 245 * Notes: Re-uses buffer. Pad to length = 46. 246 * 247 * Parameters: 248 * pkt: the request 249 */ 250 void 251 arp_reply(struct iodesc *d, void *pkt) 252 { 253 struct ether_arp *arp = pkt; 254 255 if (arp->arp_hrd != htons(ARPHRD_ETHER) || 256 arp->arp_pro != htons(ETHERTYPE_IP) || 257 arp->arp_hln != sizeof(arp->arp_sha) || 258 arp->arp_pln != sizeof(arp->arp_spa) ) 259 { 260 #ifdef ARP_DEBUG 261 if (debug) 262 printf("arp_reply: bad hrd/pro/hln/pln\n"); 263 #endif 264 return; 265 } 266 267 if (arp->arp_op != htons(ARPOP_REQUEST)) { 268 #ifdef ARP_DEBUG 269 if (debug) 270 printf("arp_reply: not request!\n"); 271 #endif 272 return; 273 } 274 275 /* If we are not the target, ignore the request. */ 276 if (bcmp(arp->arp_tpa, &d->myip, sizeof(arp->arp_tpa))) 277 return; 278 279 #ifdef ARP_DEBUG 280 if (debug) { 281 printf("arp_reply: to %s\n", ether_sprintf(arp->arp_sha)); 282 } 283 #endif 284 285 arp->arp_op = htons(ARPOP_REPLY); 286 /* source becomes target */ 287 bcopy(arp->arp_sha, arp->arp_tha, sizeof(arp->arp_tha)); 288 bcopy(arp->arp_spa, arp->arp_tpa, sizeof(arp->arp_tpa)); 289 /* here becomes source */ 290 bcopy(d->myea, arp->arp_sha, sizeof(arp->arp_sha)); 291 bcopy(&d->myip, arp->arp_spa, sizeof(arp->arp_spa)); 292 293 /* 294 * No need to get fancy here. If the send fails, the 295 * requestor will just ask again. 296 */ 297 (void) sendether(d, pkt, sizeof(*arp) + 18, 298 arp->arp_tha, ETHERTYPE_ARP); 299 } 300