1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1988, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 14df8bae1dSRodney W. Grimes * must display the following acknowledgement: 15df8bae1dSRodney W. Grimes * This product includes software developed by the University of 16df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 17df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 19df8bae1dSRodney W. Grimes * without specific prior written permission. 20df8bae1dSRodney W. Grimes * 21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31df8bae1dSRodney W. Grimes * SUCH DAMAGE. 32df8bae1dSRodney W. Grimes * 33df8bae1dSRodney W. Grimes * @(#)if_ether.c 8.1 (Berkeley) 6/10/93 34885f1aa4SPoul-Henning Kamp * $Id: if_ether.c,v 1.21 1995/12/02 19:37:48 bde Exp $ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37df8bae1dSRodney W. Grimes /* 38df8bae1dSRodney W. Grimes * Ethernet address resolution protocol. 39df8bae1dSRodney W. Grimes * TODO: 40df8bae1dSRodney W. Grimes * add "inuse/lock" bit (or ref. count) along with valid bit 41df8bae1dSRodney W. Grimes */ 42df8bae1dSRodney W. Grimes 43df8bae1dSRodney W. Grimes #include <sys/param.h> 44df8bae1dSRodney W. Grimes #include <sys/kernel.h> 45885f1aa4SPoul-Henning Kamp #include <sys/sysctl.h> 4666800f57SGarrett Wollman #include <sys/queue.h> 47885f1aa4SPoul-Henning Kamp #include <sys/systm.h> 48885f1aa4SPoul-Henning Kamp #include <sys/mbuf.h> 49885f1aa4SPoul-Henning Kamp #include <sys/malloc.h> 50885f1aa4SPoul-Henning Kamp #include <sys/syslog.h> 51df8bae1dSRodney W. Grimes 52df8bae1dSRodney W. Grimes #include <net/if.h> 53df8bae1dSRodney W. Grimes #include <net/if_dl.h> 54df8bae1dSRodney W. Grimes #include <net/route.h> 55748e0b0aSGarrett Wollman #include <net/netisr.h> 56df8bae1dSRodney W. Grimes 57df8bae1dSRodney W. Grimes #include <netinet/in.h> 58df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 59df8bae1dSRodney W. Grimes #include <netinet/if_ether.h> 60df8bae1dSRodney W. Grimes 61df8bae1dSRodney W. Grimes #define SIN(s) ((struct sockaddr_in *)s) 62df8bae1dSRodney W. Grimes #define SDL(s) ((struct sockaddr_dl *)s) 63df8bae1dSRodney W. Grimes 64885f1aa4SPoul-Henning Kamp SYSCTL_NODE(_net, OID_AUTO, arp, CTLFLAG_RW, 0, ""); 65df8bae1dSRodney W. Grimes 66df8bae1dSRodney W. Grimes /* timer values */ 67885f1aa4SPoul-Henning Kamp static int arpt_prune = (5*60*1); 68885f1aa4SPoul-Henning Kamp /* walk list every 5 minutes */ 69885f1aa4SPoul-Henning Kamp SYSCTL_INT(_net_arp, OID_AUTO, t_prune, CTLFLAG_RW, &arpt_prune, 0, ""); 70885f1aa4SPoul-Henning Kamp 71885f1aa4SPoul-Henning Kamp static int arpt_keep = (20*60); 72885f1aa4SPoul-Henning Kamp /* once resolved, good for 20 more minutes */ 73885f1aa4SPoul-Henning Kamp SYSCTL_INT(_net_arp, OID_AUTO, t_keep, CTLFLAG_RW, &arpt_keep, 0, ""); 74885f1aa4SPoul-Henning Kamp 75885f1aa4SPoul-Henning Kamp static int arpt_down = 20; 76885f1aa4SPoul-Henning Kamp /* once declared down, don't send for 20 secs */ 77885f1aa4SPoul-Henning Kamp SYSCTL_INT(_net_arp, OID_AUTO, t_down, CTLFLAG_RW, &arpt_down, 0, ""); 78885f1aa4SPoul-Henning Kamp 79df8bae1dSRodney W. Grimes #define rt_expire rt_rmx.rmx_expire 80df8bae1dSRodney W. Grimes 8166800f57SGarrett Wollman struct llinfo_arp { 8266800f57SGarrett Wollman LIST_ENTRY(llinfo_arp) la_le; 8366800f57SGarrett Wollman struct rtentry *la_rt; 8466800f57SGarrett Wollman struct mbuf *la_hold; /* last packet until resolved/timeout */ 8566800f57SGarrett Wollman long la_asked; /* last time we QUERIED for this addr */ 8666800f57SGarrett Wollman #define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */ 8766800f57SGarrett Wollman }; 8866800f57SGarrett Wollman 89885f1aa4SPoul-Henning Kamp static LIST_HEAD(, llinfo_arp) llinfo_arp; 90df8bae1dSRodney W. Grimes 91df8bae1dSRodney W. Grimes struct ifqueue arpintrq = {0, 0, 0, 50}; 92df8bae1dSRodney W. Grimes int arp_inuse, arp_allocated, arp_intimer; 93df8bae1dSRodney W. Grimes 94885f1aa4SPoul-Henning Kamp static int arp_maxtries = 5; 95885f1aa4SPoul-Henning Kamp SYSCTL_INT(_net_arp, OID_AUTO, maxtries, CTLFLAG_RW, &arp_maxtries, 0, ""); 96885f1aa4SPoul-Henning Kamp 97885f1aa4SPoul-Henning Kamp static int useloopback = 1; 98885f1aa4SPoul-Henning Kamp /* use loopback interface for local traffic */ 99885f1aa4SPoul-Henning Kamp SYSCTL_INT(_net_arp, OID_AUTO, useloopback, CTLFLAG_RW, &useloopback, 0, ""); 100885f1aa4SPoul-Henning Kamp 101885f1aa4SPoul-Henning Kamp static int arp_proxyall = 0; 102885f1aa4SPoul-Henning Kamp SYSCTL_INT(_net_arp, OID_AUTO, proxyall, CTLFLAG_RW, &arp_proxyall, 0, ""); 103885f1aa4SPoul-Henning Kamp 104885f1aa4SPoul-Henning Kamp static void arprequest __P((struct arpcom *, u_long *, u_long *, u_char *)); 105885f1aa4SPoul-Henning Kamp static void arpintr __P((void)); 106885f1aa4SPoul-Henning Kamp static void arptfree __P((struct llinfo_arp *)); 107885f1aa4SPoul-Henning Kamp static void arptimer __P((void *)); 108885f1aa4SPoul-Henning Kamp static void arpwhohas __P((struct arpcom *ac, struct in_addr *addr)); 109885f1aa4SPoul-Henning Kamp static struct llinfo_arp 110885f1aa4SPoul-Henning Kamp *arplookup __P((u_long, int, int)); 111885f1aa4SPoul-Henning Kamp static void in_arpinput __P((struct mbuf *)); 11228e82295SGarrett Wollman 113df8bae1dSRodney W. Grimes /* 114df8bae1dSRodney W. Grimes * Timeout routine. Age arp_tab entries periodically. 115df8bae1dSRodney W. Grimes */ 116df8bae1dSRodney W. Grimes /* ARGSUSED */ 117df8bae1dSRodney W. Grimes static void 118df8bae1dSRodney W. Grimes arptimer(ignored_arg) 119df8bae1dSRodney W. Grimes void *ignored_arg; 120df8bae1dSRodney W. Grimes { 121df8bae1dSRodney W. Grimes int s = splnet(); 12266800f57SGarrett Wollman register struct llinfo_arp *la = llinfo_arp.lh_first; 12366800f57SGarrett Wollman struct llinfo_arp *ola; 124df8bae1dSRodney W. Grimes 125df8bae1dSRodney W. Grimes timeout(arptimer, (caddr_t)0, arpt_prune * hz); 12666800f57SGarrett Wollman while ((ola = la) != 0) { 127df8bae1dSRodney W. Grimes register struct rtentry *rt = la->la_rt; 12866800f57SGarrett Wollman la = la->la_le.le_next; 129df8bae1dSRodney W. Grimes if (rt->rt_expire && rt->rt_expire <= time.tv_sec) 13066800f57SGarrett Wollman arptfree(ola); /* timer has expired, clear */ 131df8bae1dSRodney W. Grimes } 132df8bae1dSRodney W. Grimes splx(s); 133df8bae1dSRodney W. Grimes } 134df8bae1dSRodney W. Grimes 135df8bae1dSRodney W. Grimes /* 136df8bae1dSRodney W. Grimes * Parallel to llc_rtrequest. 137df8bae1dSRodney W. Grimes */ 138b2774d00SGarrett Wollman static void 139df8bae1dSRodney W. Grimes arp_rtrequest(req, rt, sa) 140df8bae1dSRodney W. Grimes int req; 141df8bae1dSRodney W. Grimes register struct rtentry *rt; 142df8bae1dSRodney W. Grimes struct sockaddr *sa; 143df8bae1dSRodney W. Grimes { 144df8bae1dSRodney W. Grimes register struct sockaddr *gate = rt->rt_gateway; 145df8bae1dSRodney W. Grimes register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo; 146df8bae1dSRodney W. Grimes static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 147885f1aa4SPoul-Henning Kamp static int arpinit_done; 148df8bae1dSRodney W. Grimes 149df8bae1dSRodney W. Grimes if (!arpinit_done) { 150df8bae1dSRodney W. Grimes arpinit_done = 1; 15166800f57SGarrett Wollman LIST_INIT(&llinfo_arp); 152df8bae1dSRodney W. Grimes timeout(arptimer, (caddr_t)0, hz); 153df8bae1dSRodney W. Grimes } 154df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_GATEWAY) 155df8bae1dSRodney W. Grimes return; 156df8bae1dSRodney W. Grimes switch (req) { 157df8bae1dSRodney W. Grimes 158df8bae1dSRodney W. Grimes case RTM_ADD: 159df8bae1dSRodney W. Grimes /* 160df8bae1dSRodney W. Grimes * XXX: If this is a manually added route to interface 161df8bae1dSRodney W. Grimes * such as older version of routed or gated might provide, 162df8bae1dSRodney W. Grimes * restore cloning bit. 163df8bae1dSRodney W. Grimes */ 164df8bae1dSRodney W. Grimes if ((rt->rt_flags & RTF_HOST) == 0 && 165df8bae1dSRodney W. Grimes SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 166df8bae1dSRodney W. Grimes rt->rt_flags |= RTF_CLONING; 167df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_CLONING) { 168df8bae1dSRodney W. Grimes /* 169df8bae1dSRodney W. Grimes * Case 1: This route should come from a route to iface. 170df8bae1dSRodney W. Grimes */ 171df8bae1dSRodney W. Grimes rt_setgate(rt, rt_key(rt), 172df8bae1dSRodney W. Grimes (struct sockaddr *)&null_sdl); 173df8bae1dSRodney W. Grimes gate = rt->rt_gateway; 174df8bae1dSRodney W. Grimes SDL(gate)->sdl_type = rt->rt_ifp->if_type; 175df8bae1dSRodney W. Grimes SDL(gate)->sdl_index = rt->rt_ifp->if_index; 176df8bae1dSRodney W. Grimes rt->rt_expire = time.tv_sec; 177df8bae1dSRodney W. Grimes break; 178df8bae1dSRodney W. Grimes } 179df8bae1dSRodney W. Grimes /* Announce a new entry if requested. */ 180df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_ANNOUNCE) 181df8bae1dSRodney W. Grimes arprequest((struct arpcom *)rt->rt_ifp, 182df8bae1dSRodney W. Grimes &SIN(rt_key(rt))->sin_addr.s_addr, 183df8bae1dSRodney W. Grimes &SIN(rt_key(rt))->sin_addr.s_addr, 184df8bae1dSRodney W. Grimes (u_char *)LLADDR(SDL(gate))); 185df8bae1dSRodney W. Grimes /*FALLTHROUGH*/ 186df8bae1dSRodney W. Grimes case RTM_RESOLVE: 187df8bae1dSRodney W. Grimes if (gate->sa_family != AF_LINK || 188df8bae1dSRodney W. Grimes gate->sa_len < sizeof(null_sdl)) { 18938aa9fc3SDavid Greenman log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n"); 190df8bae1dSRodney W. Grimes break; 191df8bae1dSRodney W. Grimes } 192df8bae1dSRodney W. Grimes SDL(gate)->sdl_type = rt->rt_ifp->if_type; 193df8bae1dSRodney W. Grimes SDL(gate)->sdl_index = rt->rt_ifp->if_index; 194df8bae1dSRodney W. Grimes if (la != 0) 195df8bae1dSRodney W. Grimes break; /* This happens on a route change */ 196df8bae1dSRodney W. Grimes /* 197df8bae1dSRodney W. Grimes * Case 2: This route may come from cloning, or a manual route 198df8bae1dSRodney W. Grimes * add with a LL address. 199df8bae1dSRodney W. Grimes */ 200df8bae1dSRodney W. Grimes R_Malloc(la, struct llinfo_arp *, sizeof(*la)); 201df8bae1dSRodney W. Grimes rt->rt_llinfo = (caddr_t)la; 202df8bae1dSRodney W. Grimes if (la == 0) { 203df8bae1dSRodney W. Grimes log(LOG_DEBUG, "arp_rtrequest: malloc failed\n"); 204df8bae1dSRodney W. Grimes break; 205df8bae1dSRodney W. Grimes } 206df8bae1dSRodney W. Grimes arp_inuse++, arp_allocated++; 207df8bae1dSRodney W. Grimes Bzero(la, sizeof(*la)); 208df8bae1dSRodney W. Grimes la->la_rt = rt; 209df8bae1dSRodney W. Grimes rt->rt_flags |= RTF_LLINFO; 21066800f57SGarrett Wollman LIST_INSERT_HEAD(&llinfo_arp, la, la_le); 211df8bae1dSRodney W. Grimes if (SIN(rt_key(rt))->sin_addr.s_addr == 212df8bae1dSRodney W. Grimes (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) { 213df8bae1dSRodney W. Grimes /* 214df8bae1dSRodney W. Grimes * This test used to be 215df8bae1dSRodney W. Grimes * if (loif.if_flags & IFF_UP) 216df8bae1dSRodney W. Grimes * It allowed local traffic to be forced 217df8bae1dSRodney W. Grimes * through the hardware by configuring the loopback down. 218df8bae1dSRodney W. Grimes * However, it causes problems during network configuration 219df8bae1dSRodney W. Grimes * for boards that can't receive packets they send. 220df8bae1dSRodney W. Grimes * It is now necessary to clear "useloopback" and remove 221df8bae1dSRodney W. Grimes * the route to force traffic out to the hardware. 222df8bae1dSRodney W. Grimes */ 223df8bae1dSRodney W. Grimes rt->rt_expire = 0; 224df8bae1dSRodney W. Grimes Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr, 225df8bae1dSRodney W. Grimes LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6); 226df8bae1dSRodney W. Grimes if (useloopback) 227f5fea3ddSPaul Traina rt->rt_ifp = loif; 228df8bae1dSRodney W. Grimes 229df8bae1dSRodney W. Grimes } 230df8bae1dSRodney W. Grimes break; 231df8bae1dSRodney W. Grimes 232df8bae1dSRodney W. Grimes case RTM_DELETE: 233df8bae1dSRodney W. Grimes if (la == 0) 234df8bae1dSRodney W. Grimes break; 235df8bae1dSRodney W. Grimes arp_inuse--; 23666800f57SGarrett Wollman LIST_REMOVE(la, la_le); 237df8bae1dSRodney W. Grimes rt->rt_llinfo = 0; 238df8bae1dSRodney W. Grimes rt->rt_flags &= ~RTF_LLINFO; 239df8bae1dSRodney W. Grimes if (la->la_hold) 240df8bae1dSRodney W. Grimes m_freem(la->la_hold); 241df8bae1dSRodney W. Grimes Free((caddr_t)la); 242df8bae1dSRodney W. Grimes } 243df8bae1dSRodney W. Grimes } 244df8bae1dSRodney W. Grimes /* 245df8bae1dSRodney W. Grimes * Broadcast an ARP packet, asking who has addr on interface ac. 246df8bae1dSRodney W. Grimes */ 247885f1aa4SPoul-Henning Kamp static void 248df8bae1dSRodney W. Grimes arpwhohas(ac, addr) 249885f1aa4SPoul-Henning Kamp struct arpcom *ac; 250885f1aa4SPoul-Henning Kamp struct in_addr *addr; 251df8bae1dSRodney W. Grimes { 252df8bae1dSRodney W. Grimes arprequest(ac, &ac->ac_ipaddr.s_addr, &addr->s_addr, ac->ac_enaddr); 253df8bae1dSRodney W. Grimes } 254df8bae1dSRodney W. Grimes 255df8bae1dSRodney W. Grimes /* 256df8bae1dSRodney W. Grimes * Broadcast an ARP request. Caller specifies: 257df8bae1dSRodney W. Grimes * - arp header source ip address 258df8bae1dSRodney W. Grimes * - arp header target ip address 259df8bae1dSRodney W. Grimes * - arp header source ethernet address 260df8bae1dSRodney W. Grimes */ 261df8bae1dSRodney W. Grimes static void 262df8bae1dSRodney W. Grimes arprequest(ac, sip, tip, enaddr) 263df8bae1dSRodney W. Grimes register struct arpcom *ac; 264df8bae1dSRodney W. Grimes register u_long *sip, *tip; 265df8bae1dSRodney W. Grimes register u_char *enaddr; 266df8bae1dSRodney W. Grimes { 267df8bae1dSRodney W. Grimes register struct mbuf *m; 268df8bae1dSRodney W. Grimes register struct ether_header *eh; 269df8bae1dSRodney W. Grimes register struct ether_arp *ea; 270df8bae1dSRodney W. Grimes struct sockaddr sa; 271df8bae1dSRodney W. Grimes 272df8bae1dSRodney W. Grimes if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 273df8bae1dSRodney W. Grimes return; 274df8bae1dSRodney W. Grimes m->m_len = sizeof(*ea); 275df8bae1dSRodney W. Grimes m->m_pkthdr.len = sizeof(*ea); 276df8bae1dSRodney W. Grimes MH_ALIGN(m, sizeof(*ea)); 277df8bae1dSRodney W. Grimes ea = mtod(m, struct ether_arp *); 278df8bae1dSRodney W. Grimes eh = (struct ether_header *)sa.sa_data; 279df8bae1dSRodney W. Grimes bzero((caddr_t)ea, sizeof (*ea)); 28094a5d9b6SDavid Greenman (void)memcpy(eh->ether_dhost, etherbroadcastaddr, sizeof(eh->ether_dhost)); 281df8bae1dSRodney W. Grimes eh->ether_type = ETHERTYPE_ARP; /* if_output will swap */ 282df8bae1dSRodney W. Grimes ea->arp_hrd = htons(ARPHRD_ETHER); 283df8bae1dSRodney W. Grimes ea->arp_pro = htons(ETHERTYPE_IP); 284df8bae1dSRodney W. Grimes ea->arp_hln = sizeof(ea->arp_sha); /* hardware address length */ 285df8bae1dSRodney W. Grimes ea->arp_pln = sizeof(ea->arp_spa); /* protocol address length */ 286df8bae1dSRodney W. Grimes ea->arp_op = htons(ARPOP_REQUEST); 28794a5d9b6SDavid Greenman (void)memcpy(ea->arp_sha, enaddr, sizeof(ea->arp_sha)); 28894a5d9b6SDavid Greenman (void)memcpy(ea->arp_spa, sip, sizeof(ea->arp_spa)); 28994a5d9b6SDavid Greenman (void)memcpy(ea->arp_tpa, tip, sizeof(ea->arp_tpa)); 290df8bae1dSRodney W. Grimes sa.sa_family = AF_UNSPEC; 291df8bae1dSRodney W. Grimes sa.sa_len = sizeof(sa); 292df8bae1dSRodney W. Grimes (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0); 293df8bae1dSRodney W. Grimes } 294df8bae1dSRodney W. Grimes 295df8bae1dSRodney W. Grimes /* 296df8bae1dSRodney W. Grimes * Resolve an IP address into an ethernet address. If success, 297df8bae1dSRodney W. Grimes * desten is filled in. If there is no entry in arptab, 298df8bae1dSRodney W. Grimes * set one up and broadcast a request for the IP address. 299df8bae1dSRodney W. Grimes * Hold onto this mbuf and resend it once the address 300df8bae1dSRodney W. Grimes * is finally resolved. A return value of 1 indicates 301df8bae1dSRodney W. Grimes * that desten has been filled in and the packet should be sent 302df8bae1dSRodney W. Grimes * normally; a 0 return indicates that the packet has been 303df8bae1dSRodney W. Grimes * taken over here, either now or for later transmission. 304df8bae1dSRodney W. Grimes */ 305df8bae1dSRodney W. Grimes int 3065df72964SGarrett Wollman arpresolve(ac, rt, m, dst, desten, rt0) 307df8bae1dSRodney W. Grimes register struct arpcom *ac; 308df8bae1dSRodney W. Grimes register struct rtentry *rt; 309df8bae1dSRodney W. Grimes struct mbuf *m; 310df8bae1dSRodney W. Grimes register struct sockaddr *dst; 311df8bae1dSRodney W. Grimes register u_char *desten; 3125df72964SGarrett Wollman struct rtentry *rt0; 313df8bae1dSRodney W. Grimes { 314df8bae1dSRodney W. Grimes register struct llinfo_arp *la; 315df8bae1dSRodney W. Grimes struct sockaddr_dl *sdl; 316df8bae1dSRodney W. Grimes 317df8bae1dSRodney W. Grimes if (m->m_flags & M_BCAST) { /* broadcast */ 31894a5d9b6SDavid Greenman (void)memcpy(desten, etherbroadcastaddr, sizeof(etherbroadcastaddr)); 319df8bae1dSRodney W. Grimes return (1); 320df8bae1dSRodney W. Grimes } 321df8bae1dSRodney W. Grimes if (m->m_flags & M_MCAST) { /* multicast */ 322df8bae1dSRodney W. Grimes ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 323df8bae1dSRodney W. Grimes return(1); 324df8bae1dSRodney W. Grimes } 325df8bae1dSRodney W. Grimes if (rt) 326df8bae1dSRodney W. Grimes la = (struct llinfo_arp *)rt->rt_llinfo; 327df8bae1dSRodney W. Grimes else { 328623ae52eSPoul-Henning Kamp la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0); 329623ae52eSPoul-Henning Kamp if (la) 330df8bae1dSRodney W. Grimes rt = la->la_rt; 331df8bae1dSRodney W. Grimes } 332df8bae1dSRodney W. Grimes if (la == 0 || rt == 0) { 33338aa9fc3SDavid Greenman log(LOG_DEBUG, "arpresolve: can't allocate llinfo\n"); 334df8bae1dSRodney W. Grimes m_freem(m); 335df8bae1dSRodney W. Grimes return (0); 336df8bae1dSRodney W. Grimes } 337df8bae1dSRodney W. Grimes sdl = SDL(rt->rt_gateway); 338df8bae1dSRodney W. Grimes /* 339df8bae1dSRodney W. Grimes * Check the address family and length is valid, the address 340df8bae1dSRodney W. Grimes * is resolved; otherwise, try to resolve. 341df8bae1dSRodney W. Grimes */ 342df8bae1dSRodney W. Grimes if ((rt->rt_expire == 0 || rt->rt_expire > time.tv_sec) && 343df8bae1dSRodney W. Grimes sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { 34494a5d9b6SDavid Greenman (void)memcpy(desten, LLADDR(sdl), sdl->sdl_alen); 345df8bae1dSRodney W. Grimes return 1; 346df8bae1dSRodney W. Grimes } 347df8bae1dSRodney W. Grimes /* 348df8bae1dSRodney W. Grimes * There is an arptab entry, but no ethernet address 349df8bae1dSRodney W. Grimes * response yet. Replace the held mbuf with this 350df8bae1dSRodney W. Grimes * latest one. 351df8bae1dSRodney W. Grimes */ 352df8bae1dSRodney W. Grimes if (la->la_hold) 353df8bae1dSRodney W. Grimes m_freem(la->la_hold); 354df8bae1dSRodney W. Grimes la->la_hold = m; 355df8bae1dSRodney W. Grimes if (rt->rt_expire) { 356df8bae1dSRodney W. Grimes rt->rt_flags &= ~RTF_REJECT; 357df8bae1dSRodney W. Grimes if (la->la_asked == 0 || rt->rt_expire != time.tv_sec) { 358df8bae1dSRodney W. Grimes rt->rt_expire = time.tv_sec; 359df8bae1dSRodney W. Grimes if (la->la_asked++ < arp_maxtries) 360df8bae1dSRodney W. Grimes arpwhohas(ac, &(SIN(dst)->sin_addr)); 361df8bae1dSRodney W. Grimes else { 362df8bae1dSRodney W. Grimes rt->rt_flags |= RTF_REJECT; 363df8bae1dSRodney W. Grimes rt->rt_expire += arpt_down; 364df8bae1dSRodney W. Grimes la->la_asked = 0; 365df8bae1dSRodney W. Grimes } 366df8bae1dSRodney W. Grimes 367df8bae1dSRodney W. Grimes } 368df8bae1dSRodney W. Grimes } 369df8bae1dSRodney W. Grimes return (0); 370df8bae1dSRodney W. Grimes } 371df8bae1dSRodney W. Grimes 372df8bae1dSRodney W. Grimes /* 373df8bae1dSRodney W. Grimes * Common length and type checks are done here, 374df8bae1dSRodney W. Grimes * then the protocol-specific routine is called. 375df8bae1dSRodney W. Grimes */ 376885f1aa4SPoul-Henning Kamp static void 377748e0b0aSGarrett Wollman arpintr(void) 378df8bae1dSRodney W. Grimes { 379df8bae1dSRodney W. Grimes register struct mbuf *m; 380df8bae1dSRodney W. Grimes register struct arphdr *ar; 381df8bae1dSRodney W. Grimes int s; 382df8bae1dSRodney W. Grimes 383df8bae1dSRodney W. Grimes while (arpintrq.ifq_head) { 384df8bae1dSRodney W. Grimes s = splimp(); 385df8bae1dSRodney W. Grimes IF_DEQUEUE(&arpintrq, m); 386df8bae1dSRodney W. Grimes splx(s); 387df8bae1dSRodney W. Grimes if (m == 0 || (m->m_flags & M_PKTHDR) == 0) 388df8bae1dSRodney W. Grimes panic("arpintr"); 389df8bae1dSRodney W. Grimes if (m->m_len >= sizeof(struct arphdr) && 390df8bae1dSRodney W. Grimes (ar = mtod(m, struct arphdr *)) && 391df8bae1dSRodney W. Grimes ntohs(ar->ar_hrd) == ARPHRD_ETHER && 392df8bae1dSRodney W. Grimes m->m_len >= 393df8bae1dSRodney W. Grimes sizeof(struct arphdr) + 2 * ar->ar_hln + 2 * ar->ar_pln) 394df8bae1dSRodney W. Grimes 395df8bae1dSRodney W. Grimes switch (ntohs(ar->ar_pro)) { 396df8bae1dSRodney W. Grimes 397df8bae1dSRodney W. Grimes case ETHERTYPE_IP: 398df8bae1dSRodney W. Grimes in_arpinput(m); 399df8bae1dSRodney W. Grimes continue; 400df8bae1dSRodney W. Grimes } 401df8bae1dSRodney W. Grimes m_freem(m); 402df8bae1dSRodney W. Grimes } 403df8bae1dSRodney W. Grimes } 404df8bae1dSRodney W. Grimes 405748e0b0aSGarrett Wollman NETISR_SET(NETISR_ARP, arpintr); 406748e0b0aSGarrett Wollman 407df8bae1dSRodney W. Grimes /* 408df8bae1dSRodney W. Grimes * ARP for Internet protocols on 10 Mb/s Ethernet. 409df8bae1dSRodney W. Grimes * Algorithm is that given in RFC 826. 410df8bae1dSRodney W. Grimes * In addition, a sanity check is performed on the sender 411df8bae1dSRodney W. Grimes * protocol address, to catch impersonators. 412df8bae1dSRodney W. Grimes * We no longer handle negotiations for use of trailer protocol: 413df8bae1dSRodney W. Grimes * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 414df8bae1dSRodney W. Grimes * along with IP replies if we wanted trailers sent to us, 415df8bae1dSRodney W. Grimes * and also sent them in response to IP replies. 416df8bae1dSRodney W. Grimes * This allowed either end to announce the desire to receive 417df8bae1dSRodney W. Grimes * trailer packets. 418df8bae1dSRodney W. Grimes * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 419df8bae1dSRodney W. Grimes * but formerly didn't normally send requests. 420df8bae1dSRodney W. Grimes */ 421df8bae1dSRodney W. Grimes static void 422df8bae1dSRodney W. Grimes in_arpinput(m) 423df8bae1dSRodney W. Grimes struct mbuf *m; 424df8bae1dSRodney W. Grimes { 425df8bae1dSRodney W. Grimes register struct ether_arp *ea; 426df8bae1dSRodney W. Grimes register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif; 427df8bae1dSRodney W. Grimes struct ether_header *eh; 428df8bae1dSRodney W. Grimes register struct llinfo_arp *la = 0; 429df8bae1dSRodney W. Grimes register struct rtentry *rt; 430df8bae1dSRodney W. Grimes struct in_ifaddr *ia, *maybe_ia = 0; 431df8bae1dSRodney W. Grimes struct sockaddr_dl *sdl; 432df8bae1dSRodney W. Grimes struct sockaddr sa; 433df8bae1dSRodney W. Grimes struct in_addr isaddr, itaddr, myaddr; 434df8bae1dSRodney W. Grimes int op; 435df8bae1dSRodney W. Grimes 436df8bae1dSRodney W. Grimes ea = mtod(m, struct ether_arp *); 437df8bae1dSRodney W. Grimes op = ntohs(ea->arp_op); 43894a5d9b6SDavid Greenman (void)memcpy(&isaddr, ea->arp_spa, sizeof (isaddr)); 43994a5d9b6SDavid Greenman (void)memcpy(&itaddr, ea->arp_tpa, sizeof (itaddr)); 440df8bae1dSRodney W. Grimes for (ia = in_ifaddr; ia; ia = ia->ia_next) 441df8bae1dSRodney W. Grimes if (ia->ia_ifp == &ac->ac_if) { 442df8bae1dSRodney W. Grimes maybe_ia = ia; 443df8bae1dSRodney W. Grimes if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) || 444df8bae1dSRodney W. Grimes (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)) 445df8bae1dSRodney W. Grimes break; 446df8bae1dSRodney W. Grimes } 447885f1aa4SPoul-Henning Kamp if (maybe_ia == 0) { 448885f1aa4SPoul-Henning Kamp m_freem(m); 449885f1aa4SPoul-Henning Kamp return; 450885f1aa4SPoul-Henning Kamp } 451df8bae1dSRodney W. Grimes myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr; 452df8bae1dSRodney W. Grimes if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr, 453885f1aa4SPoul-Henning Kamp sizeof (ea->arp_sha))) { 454885f1aa4SPoul-Henning Kamp m_freem(m); /* it's from me, ignore it. */ 455885f1aa4SPoul-Henning Kamp return; 456885f1aa4SPoul-Henning Kamp } 457df8bae1dSRodney W. Grimes if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr, 458df8bae1dSRodney W. Grimes sizeof (ea->arp_sha))) { 459df8bae1dSRodney W. Grimes log(LOG_ERR, 460ac234f93SGarrett Wollman "arp: ether address is broadcast for IP address %s!\n", 461ef0cdf33SGarrett Wollman inet_ntoa(isaddr)); 462885f1aa4SPoul-Henning Kamp m_freem(m); 463885f1aa4SPoul-Henning Kamp return; 464df8bae1dSRodney W. Grimes } 465df8bae1dSRodney W. Grimes if (isaddr.s_addr == myaddr.s_addr) { 466df8bae1dSRodney W. Grimes log(LOG_ERR, 467ac234f93SGarrett Wollman "duplicate IP address %s! sent from ethernet address: %s\n", 468ef0cdf33SGarrett Wollman inet_ntoa(isaddr), ether_sprintf(ea->arp_sha)); 469df8bae1dSRodney W. Grimes itaddr = myaddr; 470df8bae1dSRodney W. Grimes goto reply; 471df8bae1dSRodney W. Grimes } 472df8bae1dSRodney W. Grimes la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0); 473df8bae1dSRodney W. Grimes if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) { 474df8bae1dSRodney W. Grimes if (sdl->sdl_alen && 475df8bae1dSRodney W. Grimes bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen)) 476ac234f93SGarrett Wollman log(LOG_INFO, "arp info overwritten for %s by %s\n", 477ef0cdf33SGarrett Wollman inet_ntoa(isaddr), ether_sprintf(ea->arp_sha)); 47894a5d9b6SDavid Greenman (void)memcpy(LLADDR(sdl), ea->arp_sha, sizeof(ea->arp_sha)); 47994a5d9b6SDavid Greenman sdl->sdl_alen = sizeof(ea->arp_sha); 480df8bae1dSRodney W. Grimes if (rt->rt_expire) 481df8bae1dSRodney W. Grimes rt->rt_expire = time.tv_sec + arpt_keep; 482df8bae1dSRodney W. Grimes rt->rt_flags &= ~RTF_REJECT; 483df8bae1dSRodney W. Grimes la->la_asked = 0; 484df8bae1dSRodney W. Grimes if (la->la_hold) { 485df8bae1dSRodney W. Grimes (*ac->ac_if.if_output)(&ac->ac_if, la->la_hold, 486df8bae1dSRodney W. Grimes rt_key(rt), rt); 487df8bae1dSRodney W. Grimes la->la_hold = 0; 488df8bae1dSRodney W. Grimes } 489df8bae1dSRodney W. Grimes } 490df8bae1dSRodney W. Grimes reply: 491df8bae1dSRodney W. Grimes if (op != ARPOP_REQUEST) { 492df8bae1dSRodney W. Grimes m_freem(m); 493df8bae1dSRodney W. Grimes return; 494df8bae1dSRodney W. Grimes } 495df8bae1dSRodney W. Grimes if (itaddr.s_addr == myaddr.s_addr) { 496df8bae1dSRodney W. Grimes /* I am the target */ 49794a5d9b6SDavid Greenman (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha)); 49894a5d9b6SDavid Greenman (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha)); 499df8bae1dSRodney W. Grimes } else { 500df8bae1dSRodney W. Grimes la = arplookup(itaddr.s_addr, 0, SIN_PROXY); 50128e82295SGarrett Wollman if (la == NULL) { 50228e82295SGarrett Wollman struct sockaddr_in sin; 50328e82295SGarrett Wollman 504885f1aa4SPoul-Henning Kamp if (!arp_proxyall) { 505885f1aa4SPoul-Henning Kamp m_freem(m); 506885f1aa4SPoul-Henning Kamp return; 507885f1aa4SPoul-Henning Kamp } 50828e82295SGarrett Wollman 50928e82295SGarrett Wollman bzero(&sin, sizeof sin); 51028e82295SGarrett Wollman sin.sin_family = AF_INET; 51128e82295SGarrett Wollman sin.sin_len = sizeof sin; 51228e82295SGarrett Wollman sin.sin_addr = itaddr; 51328e82295SGarrett Wollman 51431246bc2SGarrett Wollman rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 515885f1aa4SPoul-Henning Kamp if (!rt) { 516885f1aa4SPoul-Henning Kamp m_freem(m); 517885f1aa4SPoul-Henning Kamp return; 518885f1aa4SPoul-Henning Kamp } 51928e82295SGarrett Wollman /* 52028e82295SGarrett Wollman * Don't send proxies for nodes on the same interface 52128e82295SGarrett Wollman * as this one came out of, or we'll get into a fight 52228e82295SGarrett Wollman * over who claims what Ether address. 52328e82295SGarrett Wollman */ 52428e82295SGarrett Wollman if (rt->rt_ifp == &ac->ac_if) { 52528e82295SGarrett Wollman rtfree(rt); 526885f1aa4SPoul-Henning Kamp m_freem(m); 527885f1aa4SPoul-Henning Kamp return; 52828e82295SGarrett Wollman } 52994a5d9b6SDavid Greenman (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha)); 53094a5d9b6SDavid Greenman (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha)); 53128e82295SGarrett Wollman rtfree(rt); 532ac234f93SGarrett Wollman #ifdef DEBUG_PROXY 533ac234f93SGarrett Wollman printf("arp: proxying for %s\n", 534ef0cdf33SGarrett Wollman inet_ntoa(itaddr)); 535ac234f93SGarrett Wollman #endif 53628e82295SGarrett Wollman } else { 537df8bae1dSRodney W. Grimes rt = la->la_rt; 53894a5d9b6SDavid Greenman (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha)); 539df8bae1dSRodney W. Grimes sdl = SDL(rt->rt_gateway); 54094a5d9b6SDavid Greenman (void)memcpy(ea->arp_sha, LLADDR(sdl), sizeof(ea->arp_sha)); 54128e82295SGarrett Wollman } 542df8bae1dSRodney W. Grimes } 543df8bae1dSRodney W. Grimes 54494a5d9b6SDavid Greenman (void)memcpy(ea->arp_tpa, ea->arp_spa, sizeof(ea->arp_spa)); 54594a5d9b6SDavid Greenman (void)memcpy(ea->arp_spa, &itaddr, sizeof(ea->arp_spa)); 546df8bae1dSRodney W. Grimes ea->arp_op = htons(ARPOP_REPLY); 547df8bae1dSRodney W. Grimes ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 548df8bae1dSRodney W. Grimes eh = (struct ether_header *)sa.sa_data; 54994a5d9b6SDavid Greenman (void)memcpy(eh->ether_dhost, ea->arp_tha, sizeof(eh->ether_dhost)); 550df8bae1dSRodney W. Grimes eh->ether_type = ETHERTYPE_ARP; 551df8bae1dSRodney W. Grimes sa.sa_family = AF_UNSPEC; 552df8bae1dSRodney W. Grimes sa.sa_len = sizeof(sa); 553df8bae1dSRodney W. Grimes (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0); 554df8bae1dSRodney W. Grimes return; 555df8bae1dSRodney W. Grimes } 556df8bae1dSRodney W. Grimes 557df8bae1dSRodney W. Grimes /* 558df8bae1dSRodney W. Grimes * Free an arp entry. 559df8bae1dSRodney W. Grimes */ 560df8bae1dSRodney W. Grimes static void 561df8bae1dSRodney W. Grimes arptfree(la) 562df8bae1dSRodney W. Grimes register struct llinfo_arp *la; 563df8bae1dSRodney W. Grimes { 564df8bae1dSRodney W. Grimes register struct rtentry *rt = la->la_rt; 565df8bae1dSRodney W. Grimes register struct sockaddr_dl *sdl; 566df8bae1dSRodney W. Grimes if (rt == 0) 567df8bae1dSRodney W. Grimes panic("arptfree"); 568df8bae1dSRodney W. Grimes if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) && 569df8bae1dSRodney W. Grimes sdl->sdl_family == AF_LINK) { 570df8bae1dSRodney W. Grimes sdl->sdl_alen = 0; 571df8bae1dSRodney W. Grimes la->la_asked = 0; 572df8bae1dSRodney W. Grimes rt->rt_flags &= ~RTF_REJECT; 573df8bae1dSRodney W. Grimes return; 574df8bae1dSRodney W. Grimes } 575df8bae1dSRodney W. Grimes rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt), 576df8bae1dSRodney W. Grimes 0, (struct rtentry **)0); 577df8bae1dSRodney W. Grimes } 578df8bae1dSRodney W. Grimes /* 579df8bae1dSRodney W. Grimes * Lookup or enter a new address in arptab. 580df8bae1dSRodney W. Grimes */ 581df8bae1dSRodney W. Grimes static struct llinfo_arp * 582df8bae1dSRodney W. Grimes arplookup(addr, create, proxy) 583df8bae1dSRodney W. Grimes u_long addr; 584df8bae1dSRodney W. Grimes int create, proxy; 585df8bae1dSRodney W. Grimes { 586df8bae1dSRodney W. Grimes register struct rtentry *rt; 587df8bae1dSRodney W. Grimes static struct sockaddr_inarp sin = {sizeof(sin), AF_INET }; 588ac234f93SGarrett Wollman const char *why = 0; 589df8bae1dSRodney W. Grimes 590df8bae1dSRodney W. Grimes sin.sin_addr.s_addr = addr; 591df8bae1dSRodney W. Grimes sin.sin_other = proxy ? SIN_PROXY : 0; 59231246bc2SGarrett Wollman rt = rtalloc1((struct sockaddr *)&sin, create, 0UL); 593df8bae1dSRodney W. Grimes if (rt == 0) 594df8bae1dSRodney W. Grimes return (0); 595df8bae1dSRodney W. Grimes rt->rt_refcnt--; 596ac234f93SGarrett Wollman 597ac234f93SGarrett Wollman if (rt->rt_flags & RTF_GATEWAY) 598ac234f93SGarrett Wollman why = "host is not on local network"; 599ac234f93SGarrett Wollman else if ((rt->rt_flags & RTF_LLINFO) == 0) 600ac234f93SGarrett Wollman why = "could not allocate llinfo"; 601ac234f93SGarrett Wollman else if (rt->rt_gateway->sa_family != AF_LINK) 602ac234f93SGarrett Wollman why = "gateway route is not ours"; 603ac234f93SGarrett Wollman 604ac234f93SGarrett Wollman if (why && create) { 605ac234f93SGarrett Wollman log(LOG_DEBUG, "arplookup %s failed: %s\n", 606ef0cdf33SGarrett Wollman inet_ntoa(sin.sin_addr), why); 607ac234f93SGarrett Wollman return 0; 608ac234f93SGarrett Wollman } else if (why) { 609ac234f93SGarrett Wollman return 0; 610df8bae1dSRodney W. Grimes } 611df8bae1dSRodney W. Grimes return ((struct llinfo_arp *)rt->rt_llinfo); 612df8bae1dSRodney W. Grimes } 613df8bae1dSRodney W. Grimes 614dd2e4102SGarrett Wollman void 615dd2e4102SGarrett Wollman arp_ifinit(ac, ifa) 616dd2e4102SGarrett Wollman struct arpcom *ac; 617dd2e4102SGarrett Wollman struct ifaddr *ifa; 618dd2e4102SGarrett Wollman { 619dd2e4102SGarrett Wollman ac->ac_ipaddr = IA_SIN(ifa)->sin_addr; 620dd2e4102SGarrett Wollman arpwhohas(ac, &ac->ac_ipaddr); 621dd2e4102SGarrett Wollman ifa->ifa_rtrequest = arp_rtrequest; 622dd2e4102SGarrett Wollman ifa->ifa_flags |= RTF_CLONING; 623dd2e4102SGarrett Wollman } 624