1 /* 2 * Copyright (c) 1992 Regents of the University of California. 3 * All rights reserved. 4 * 5 * This software was developed by the Computer Systems Engineering group 6 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 7 * contributed to Berkeley. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Lawrence Berkeley Laboratory and its contributors. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * from @(#) Header: arp.c,v 1.5 93/07/15 05:52:26 leres Exp (LBL) 38 * $Id: arp.c,v 1.2 1994/08/04 19:39:34 brezak Exp $ 39 */ 40 41 #include <sys/types.h> 42 #include <sys/socket.h> 43 #include <net/if.h> 44 #include <netinet/in.h> 45 46 #include <netinet/if_ether.h> 47 #include <netinet/in_systm.h> 48 49 #include <string.h> 50 51 #include "stand.h" 52 #include "net.h" 53 54 /* Cache stuff */ 55 #define ARP_NUM 8 /* need at most 3 arp entries */ 56 57 static struct arp_list { 58 n_long addr; 59 u_char ea[6]; 60 } arp_list[ARP_NUM] = { 61 { INADDR_BROADCAST, BA } 62 }; 63 static int arp_num = 1; 64 65 /* Local forwards */ 66 static int arpsend __P((struct iodesc *, void *, int)); 67 static int arprecv __P((struct iodesc *, void *, int)); 68 69 /* Broadcast an ARP packet, asking who has addr on interface d */ 70 u_char * 71 arpwhohas(d, addr) 72 register struct iodesc *d; 73 n_long addr; 74 { 75 register int i; 76 register struct ether_arp *ah; 77 register struct arp_list *al; 78 struct { 79 u_char header[ETHER_SIZE]; 80 struct ether_arp warp; 81 } wbuf; 82 union { 83 u_char buffer[RECV_SIZE]; 84 struct { 85 u_char header[ETHER_SIZE]; 86 struct ether_arp rarp; 87 } ru; 88 } rbuf; 89 90 #ifdef ARP_DEBUG 91 if (debug) 92 printf("arpwhohas: called for %s\n", intoa(addr)); 93 #endif 94 /* Try for cached answer first */ 95 for (i = 0, al = arp_list; i < arp_num; ++i, ++al) 96 if (addr == al->addr) 97 return (al->ea); 98 99 /* Don't overflow cache */ 100 if (arp_num > ARP_NUM - 1) 101 panic("arpwhohas: overflowed arp_list!"); 102 103 #ifdef ARP_DEBUG 104 if (debug) 105 printf("arpwhohas: not cached\n"); 106 #endif 107 ah = &wbuf.warp; 108 bzero(ah, sizeof(*ah)); 109 110 ah->arp_hrd = htons(ARPHRD_ETHER); 111 ah->arp_pro = htons(ETHERTYPE_IP); 112 ah->arp_hln = sizeof(ah->arp_sha); /* hardware address length */ 113 ah->arp_pln = sizeof(ah->arp_spa); /* protocol address length */ 114 ah->arp_op = htons(ARPOP_REQUEST); 115 MACPY(d->myea, ah->arp_sha); 116 bcopy(&d->myip, ah->arp_spa, sizeof(ah->arp_spa)); 117 bcopy(&addr, ah->arp_tpa, sizeof(ah->arp_tpa)); 118 119 /* Store ip address in cache */ 120 al->addr = addr; 121 122 (void)sendrecv(d, arpsend, ah, sizeof(*ah), arprecv, 123 ((u_char *)&rbuf.ru.rarp) - ETHER_SIZE, 124 ETHER_SIZE + sizeof(rbuf.ru.rarp)); 125 126 /* Store ethernet address in cache */ 127 MACPY(rbuf.ru.rarp.arp_sha, al->ea); 128 ++arp_num; 129 #ifdef ARP_DEBUG 130 if (debug) 131 printf("arpwhohas: cacheing %s --> %s\n", intoa(addr), ether_sprintf(al->ea)); 132 #endif 133 134 return (al->ea); 135 } 136 137 static int 138 arpsend(d, pkt, len) 139 register struct iodesc *d; 140 register void *pkt; 141 register int len; 142 { 143 #ifdef ARP_DEBUG 144 if (debug) 145 printf("arpsend: called\n"); 146 #endif 147 return (sendether(d, pkt, len, bcea, ETHERTYPE_ARP)); 148 } 149 150 /* Returns 0 if this is the packet we're waiting for else -1 (and errno == 0) */ 151 static int 152 arprecv(d, pkt, len) 153 register struct iodesc *d; 154 register void *pkt; 155 register int len; 156 { 157 register struct ether_header *eh; 158 register struct ether_arp *ah; 159 160 #ifdef ARP_DEBUG 161 if (debug) 162 printf("arprecv: called\n"); 163 #endif 164 if (len < sizeof(*eh) + sizeof(*ah)) { 165 errno = 0; 166 return (-1); 167 } 168 169 eh = (struct ether_header *)pkt; 170 171 /* Must be to us */ 172 if (bcmp(d->myea, eh->ether_dhost, 6) != 0 && 173 bcmp(bcea, eh->ether_dhost, 6) != 0) { 174 #ifdef ARP_DEBUG 175 if (debug) 176 printf("arprecv: not ours %s\n", ether_sprintf(eh->ether_dhost)); 177 #endif 178 errno = 0; 179 return (-1); 180 } 181 182 if (ntohs(eh->ether_type) != ETHERTYPE_ARP) { 183 #ifdef ARP_DEBUG 184 if (debug) 185 printf("arprecv: not arp %d\n", ntohs(eh->ether_type)); 186 #endif 187 errno = 0; 188 return (-1); 189 } 190 191 ah = (struct ether_arp *)(eh + 1); 192 HTONS(ah->arp_hrd); 193 HTONS(ah->arp_pro); 194 HTONS(ah->arp_op); 195 if (ah->arp_hrd != ARPHRD_ETHER || ah->arp_pro != ETHERTYPE_IP || 196 ah->arp_hln != sizeof(ah->arp_sha) || 197 ah->arp_pln != sizeof(ah->arp_spa) || ah->arp_op != ARPOP_REPLY) { 198 #ifdef ARP_DEBUG 199 if (debug) 200 printf("arprecv: not valid reply\n"); 201 #endif 202 errno = 0; 203 return (-1); 204 } 205 if (bcmp(&arp_list[arp_num].addr, ah->arp_spa, sizeof(long)) != 0 || 206 bcmp(&d->myip, ah->arp_tpa, sizeof(d->myip)) != 0) { 207 #ifdef ARP_DEBUG 208 if (debug) 209 printf("arprecv: already cached??\n"); 210 #endif 211 errno = 0; 212 return (-1); 213 } 214 215 return (0); 216 } 217