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 36 #include <sys/cdefs.h> 37 #include <sys/types.h> 38 #include <sys/socket.h> 39 #include <net/if.h> 40 #include <netinet/in.h> 41 #include <netinet/if_ether.h> 42 43 #include <netinet/in_systm.h> 44 45 #include <string.h> 46 47 #include "stand.h" 48 #include "net.h" 49 50 /* Cache stuff */ 51 #define ARP_NUM 8 /* need at most 3 arp entries */ 52 53 struct arp_list { 54 struct in_addr addr; 55 u_char ea[6]; 56 } arp_list[ARP_NUM] = { 57 /* XXX - net order `INADDR_BROADCAST' must be a constant */ 58 { {0xffffffff}, BA } 59 }; 60 int arp_num = 1; 61 62 /* Local forwards */ 63 static ssize_t arpsend(struct iodesc *, void *, size_t); 64 static ssize_t arprecv(struct iodesc *, void **, void **, time_t, void *); 65 66 /* Broadcast an ARP packet, asking who has addr on interface d */ 67 u_char * 68 arpwhohas(struct iodesc *d, struct in_addr addr) 69 { 70 int i; 71 struct ether_arp *ah; 72 struct arp_list *al; 73 void *pkt; 74 struct { 75 struct ether_header eh; 76 struct { 77 struct ether_arp arp; 78 u_char pad[18]; /* 60 - sizeof(...) */ 79 } data; 80 } wbuf; 81 82 /* Try for cached answer first */ 83 for (i = 0, al = arp_list; i < arp_num; ++i, ++al) 84 if (addr.s_addr == al->addr.s_addr) 85 return (al->ea); 86 87 /* Don't overflow cache */ 88 if (arp_num > ARP_NUM - 1) { 89 arp_num = 1; /* recycle */ 90 printf("arpwhohas: overflowed arp_list!\n"); 91 } 92 93 #ifdef ARP_DEBUG 94 if (debug) 95 printf("arpwhohas: send request for %s\n", inet_ntoa(addr)); 96 #endif 97 98 bzero((char*)&wbuf.data, sizeof(wbuf.data)); 99 ah = &wbuf.data.arp; 100 ah->arp_hrd = htons(ARPHRD_ETHER); 101 ah->arp_pro = htons(ETHERTYPE_IP); 102 ah->arp_hln = sizeof(ah->arp_sha); /* hardware address length */ 103 ah->arp_pln = sizeof(ah->arp_spa); /* protocol address length */ 104 ah->arp_op = htons(ARPOP_REQUEST); 105 MACPY(d->myea, ah->arp_sha); 106 bcopy(&d->myip, ah->arp_spa, sizeof(ah->arp_spa)); 107 /* Leave zeros in arp_tha */ 108 bcopy(&addr, ah->arp_tpa, sizeof(ah->arp_tpa)); 109 110 /* Store ip address in cache (incomplete entry). */ 111 al->addr = addr; 112 113 pkt = NULL; 114 ah = NULL; 115 i = sendrecv(d, 116 arpsend, &wbuf.data, sizeof(wbuf.data), 117 arprecv, &pkt, (void **)&ah, NULL); 118 if (i == -1) { 119 panic("arp: no response for %s", 120 inet_ntoa(addr)); 121 } 122 123 /* Store ethernet address in cache */ 124 #ifdef ARP_DEBUG 125 if (debug) { 126 struct ether_header *eh; 127 128 eh = (struct ether_header *)((uintptr_t)pkt + ETHER_ALIGN); 129 printf("arp: response from %s\n", 130 ether_sprintf(eh->ether_shost)); 131 printf("arp: cacheing %s --> %s\n", 132 inet_ntoa(addr), ether_sprintf(ah->arp_sha)); 133 } 134 #endif 135 MACPY(ah->arp_sha, al->ea); 136 ++arp_num; 137 138 free(pkt); 139 return (al->ea); 140 } 141 142 static ssize_t 143 arpsend(struct iodesc *d, void *pkt, size_t len) 144 { 145 146 #ifdef ARP_DEBUG 147 if (debug) 148 printf("arpsend: called\n"); 149 #endif 150 151 return (sendether(d, pkt, len, bcea, ETHERTYPE_ARP)); 152 } 153 154 /* 155 * Returns 0 if this is the packet we're waiting for 156 * else -1 (and errno == 0) 157 */ 158 static ssize_t 159 arprecv(struct iodesc *d, void **pkt, void **payload, time_t tleft, void *extra) 160 { 161 ssize_t n; 162 struct ether_arp *ah; 163 uint16_t etype; /* host order */ 164 void *ptr; 165 166 #ifdef ARP_DEBUG 167 if (debug) 168 printf("arprecv: "); 169 #endif 170 171 ptr = NULL; 172 n = readether(d, &ptr, (void **)&ah, tleft, &etype); 173 errno = 0; /* XXX */ 174 if (n == -1 || n < sizeof(struct ether_arp)) { 175 #ifdef ARP_DEBUG 176 if (debug) 177 printf("bad len=%zd\n", n); 178 #endif 179 free(ptr); 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 free(ptr); 189 return (-1); 190 } 191 192 /* Ethernet address now checked in readether() */ 193 if (ah->arp_hrd != htons(ARPHRD_ETHER) || 194 ah->arp_pro != htons(ETHERTYPE_IP) || 195 ah->arp_hln != sizeof(ah->arp_sha) || 196 ah->arp_pln != sizeof(ah->arp_spa) ) 197 { 198 #ifdef ARP_DEBUG 199 if (debug) 200 printf("bad hrd/pro/hln/pln\n"); 201 #endif 202 free(ptr); 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 free(ptr); 213 return (-1); 214 } 215 216 if (ah->arp_op != htons(ARPOP_REPLY)) { 217 #ifdef ARP_DEBUG 218 if (debug) 219 printf("not ARP reply\n"); 220 #endif 221 free(ptr); 222 return (-1); 223 } 224 225 /* Is the reply from the source we want? */ 226 if (bcmp(&arp_list[arp_num].addr, 227 ah->arp_spa, sizeof(ah->arp_spa))) 228 { 229 #ifdef ARP_DEBUG 230 if (debug) 231 printf("unwanted address\n"); 232 #endif 233 free(ptr); 234 return (-1); 235 } 236 /* We don't care who the reply was sent to. */ 237 238 /* We have our answer. */ 239 #ifdef ARP_DEBUG 240 if (debug) 241 printf("got it\n"); 242 #endif 243 *pkt = ptr; 244 *payload = ah; 245 return (n); 246 } 247 248 /* 249 * Convert an ARP request into a reply and send it. 250 * Notes: Re-uses buffer. Pad to length = 46. 251 */ 252 void 253 arp_reply(struct iodesc *d, void *pkt) 254 { 255 struct ether_arp *arp = pkt; 256 257 if (arp->arp_hrd != htons(ARPHRD_ETHER) || 258 arp->arp_pro != htons(ETHERTYPE_IP) || 259 arp->arp_hln != sizeof(arp->arp_sha) || 260 arp->arp_pln != sizeof(arp->arp_spa) ) 261 { 262 #ifdef ARP_DEBUG 263 if (debug) 264 printf("arp_reply: bad hrd/pro/hln/pln\n"); 265 #endif 266 return; 267 } 268 269 if (arp->arp_op != htons(ARPOP_REQUEST)) { 270 #ifdef ARP_DEBUG 271 if (debug) 272 printf("arp_reply: not request!\n"); 273 #endif 274 return; 275 } 276 277 /* If we are not the target, ignore the request. */ 278 if (bcmp(arp->arp_tpa, &d->myip, sizeof(arp->arp_tpa))) 279 return; 280 281 #ifdef ARP_DEBUG 282 if (debug) { 283 printf("arp_reply: to %s\n", ether_sprintf(arp->arp_sha)); 284 } 285 #endif 286 287 arp->arp_op = htons(ARPOP_REPLY); 288 /* source becomes target */ 289 bcopy(arp->arp_sha, arp->arp_tha, sizeof(arp->arp_tha)); 290 bcopy(arp->arp_spa, arp->arp_tpa, sizeof(arp->arp_tpa)); 291 /* here becomes source */ 292 bcopy(d->myea, arp->arp_sha, sizeof(arp->arp_sha)); 293 bcopy(&d->myip, arp->arp_spa, sizeof(arp->arp_spa)); 294 295 /* 296 * No need to get fancy here. If the send fails, the 297 * requestor will just ask again. 298 */ 299 (void) sendether(d, pkt, sizeof(*arp) + 18, 300 arp->arp_tha, ETHERTYPE_ARP); 301 } 302