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 34c3aac50fSPeter Wemm * $FreeBSD$ 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 431d5e9e22SEivind Eklund #include "opt_inet.h" 44b715f178SLuigi Rizzo #include "opt_bdg.h" 4519527d3eSRobert Watson #include "opt_mac.h" 461d5e9e22SEivind Eklund 47df8bae1dSRodney W. Grimes #include <sys/param.h> 48df8bae1dSRodney W. Grimes #include <sys/kernel.h> 49ce02431fSDoug Rabson #include <sys/queue.h> 50885f1aa4SPoul-Henning Kamp #include <sys/sysctl.h> 51885f1aa4SPoul-Henning Kamp #include <sys/systm.h> 5219527d3eSRobert Watson #include <sys/mac.h> 53885f1aa4SPoul-Henning Kamp #include <sys/mbuf.h> 54885f1aa4SPoul-Henning Kamp #include <sys/malloc.h> 554458ac71SBruce Evans #include <sys/socket.h> 56885f1aa4SPoul-Henning Kamp #include <sys/syslog.h> 57df8bae1dSRodney W. Grimes 58df8bae1dSRodney W. Grimes #include <net/if.h> 59df8bae1dSRodney W. Grimes #include <net/if_dl.h> 60722012ccSJulian Elischer #include <net/if_types.h> 61df8bae1dSRodney W. Grimes #include <net/route.h> 62748e0b0aSGarrett Wollman #include <net/netisr.h> 63b149dd6cSLarry Lile #include <net/if_llc.h> 64c8f8e9c1SJulian Elischer #ifdef BRIDGE 65c8f8e9c1SJulian Elischer #include <net/ethernet.h> 66c8f8e9c1SJulian Elischer #include <net/bridge.h> 67c8f8e9c1SJulian Elischer #endif 68df8bae1dSRodney W. Grimes 69df8bae1dSRodney W. Grimes #include <netinet/in.h> 70df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 71df8bae1dSRodney W. Grimes #include <netinet/if_ether.h> 72df8bae1dSRodney W. Grimes 73322dcb8dSMax Khon #include <net/if_arc.h> 74fda82fc2SJulian Elischer #include <net/iso88025.h> 75fda82fc2SJulian Elischer 76df8bae1dSRodney W. Grimes #define SIN(s) ((struct sockaddr_in *)s) 77df8bae1dSRodney W. Grimes #define SDL(s) ((struct sockaddr_dl *)s) 78df8bae1dSRodney W. Grimes 79ce02431fSDoug Rabson SYSCTL_DECL(_net_link_ether); 80602d513cSGarrett Wollman SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, ""); 81df8bae1dSRodney W. Grimes 82df8bae1dSRodney W. Grimes /* timer values */ 83602d513cSGarrett Wollman static int arpt_prune = (5*60*1); /* walk list every 5 minutes */ 84602d513cSGarrett Wollman static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ 85602d513cSGarrett Wollman static int arpt_down = 20; /* once declared down, don't send for 20 sec */ 86885f1aa4SPoul-Henning Kamp 87602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW, 88602d513cSGarrett Wollman &arpt_prune, 0, ""); 89602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW, 90602d513cSGarrett Wollman &arpt_keep, 0, ""); 91602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW, 92602d513cSGarrett Wollman &arpt_down, 0, ""); 93885f1aa4SPoul-Henning Kamp 94df8bae1dSRodney W. Grimes #define rt_expire rt_rmx.rmx_expire 95df8bae1dSRodney W. Grimes 9666800f57SGarrett Wollman struct llinfo_arp { 97e3975643SJake Burkholder LIST_ENTRY(llinfo_arp) la_le; 9866800f57SGarrett Wollman struct rtentry *la_rt; 9966800f57SGarrett Wollman struct mbuf *la_hold; /* last packet until resolved/timeout */ 100022695f8SOrion Hodson u_short la_preempt; /* countdown for pre-expiry arps */ 10173224fb0SOrion Hodson u_short la_asked; /* #times we QUERIED following expiration */ 10266800f57SGarrett Wollman #define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */ 10366800f57SGarrett Wollman }; 10466800f57SGarrett Wollman 105e3975643SJake Burkholder static LIST_HEAD(, llinfo_arp) llinfo_arp; 106df8bae1dSRodney W. Grimes 1071cafed39SJonathan Lemon static struct ifqueue arpintrq; 108d1dd20beSSam Leffler static int arp_allocated; 109d1dd20beSSam Leffler static int arpinit_done; 110df8bae1dSRodney W. Grimes 111885f1aa4SPoul-Henning Kamp static int arp_maxtries = 5; 112602d513cSGarrett Wollman static int useloopback = 1; /* use loopback interface for local traffic */ 113885f1aa4SPoul-Henning Kamp static int arp_proxyall = 0; 114d1dd20beSSam Leffler static struct callout arp_callout; 115602d513cSGarrett Wollman 116602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW, 117602d513cSGarrett Wollman &arp_maxtries, 0, ""); 118602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW, 119602d513cSGarrett Wollman &useloopback, 0, ""); 120602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW, 121602d513cSGarrett Wollman &arp_proxyall, 0, ""); 122885f1aa4SPoul-Henning Kamp 1234d77a549SAlfred Perlstein static void arp_init(void); 1244d77a549SAlfred Perlstein static void arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *); 1254d77a549SAlfred Perlstein static void arprequest(struct ifnet *, 1264d77a549SAlfred Perlstein struct in_addr *, struct in_addr *, u_char *); 1271cafed39SJonathan Lemon static void arpintr(struct mbuf *); 1284d77a549SAlfred Perlstein static void arptfree(struct llinfo_arp *); 1294d77a549SAlfred Perlstein static void arptimer(void *); 130885f1aa4SPoul-Henning Kamp static struct llinfo_arp 1314d77a549SAlfred Perlstein *arplookup(u_long, int, int); 1321d5e9e22SEivind Eklund #ifdef INET 1334d77a549SAlfred Perlstein static void in_arpinput(struct mbuf *); 1341d5e9e22SEivind Eklund #endif 13528e82295SGarrett Wollman 136df8bae1dSRodney W. Grimes /* 137df8bae1dSRodney W. Grimes * Timeout routine. Age arp_tab entries periodically. 138df8bae1dSRodney W. Grimes */ 139df8bae1dSRodney W. Grimes /* ARGSUSED */ 140df8bae1dSRodney W. Grimes static void 141df8bae1dSRodney W. Grimes arptimer(ignored_arg) 142df8bae1dSRodney W. Grimes void *ignored_arg; 143df8bae1dSRodney W. Grimes { 144c996428cSJeffrey Hsu struct llinfo_arp *la, *ola; 145df8bae1dSRodney W. Grimes 14693f79889SJeffrey Hsu RADIX_NODE_HEAD_LOCK(rt_tables[AF_INET]); 147c996428cSJeffrey Hsu la = LIST_FIRST(&llinfo_arp); 148c996428cSJeffrey Hsu while (la != NULL) { 149c996428cSJeffrey Hsu struct rtentry *rt = la->la_rt; 150c996428cSJeffrey Hsu ola = la; 151fc2ffbe6SPoul-Henning Kamp la = LIST_NEXT(la, la_le); 152227ee8a1SPoul-Henning Kamp if (rt->rt_expire && rt->rt_expire <= time_second) 15366800f57SGarrett Wollman arptfree(ola); /* timer has expired, clear */ 154df8bae1dSRodney W. Grimes } 15593f79889SJeffrey Hsu RADIX_NODE_HEAD_UNLOCK(rt_tables[AF_INET]); 156d1dd20beSSam Leffler 157d1dd20beSSam Leffler callout_reset(&arp_callout, arpt_prune * hz, arptimer, NULL); 158df8bae1dSRodney W. Grimes } 159df8bae1dSRodney W. Grimes 160df8bae1dSRodney W. Grimes /* 161df8bae1dSRodney W. Grimes * Parallel to llc_rtrequest. 162df8bae1dSRodney W. Grimes */ 163b2774d00SGarrett Wollman static void 1648071913dSRuslan Ermilov arp_rtrequest(req, rt, info) 165df8bae1dSRodney W. Grimes int req; 166e952fa39SMatthew N. Dodd struct rtentry *rt; 1678071913dSRuslan Ermilov struct rt_addrinfo *info; 168df8bae1dSRodney W. Grimes { 169e952fa39SMatthew N. Dodd struct sockaddr *gate; 170e952fa39SMatthew N. Dodd struct llinfo_arp *la; 171df8bae1dSRodney W. Grimes static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 172df8bae1dSRodney W. Grimes 173d1dd20beSSam Leffler RT_LOCK_ASSERT(rt); 174d1dd20beSSam Leffler 175df8bae1dSRodney W. Grimes if (!arpinit_done) { 176df8bae1dSRodney W. Grimes arpinit_done = 1; 177d1dd20beSSam Leffler callout_reset(&arp_callout, hz, arptimer, NULL); 178df8bae1dSRodney W. Grimes } 179df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_GATEWAY) 180df8bae1dSRodney W. Grimes return; 181d1dd20beSSam Leffler gate = rt->rt_gateway; 182d1dd20beSSam Leffler la = (struct llinfo_arp *)rt->rt_llinfo; 183df8bae1dSRodney W. Grimes switch (req) { 184df8bae1dSRodney W. Grimes 185df8bae1dSRodney W. Grimes case RTM_ADD: 186df8bae1dSRodney W. Grimes /* 187df8bae1dSRodney W. Grimes * XXX: If this is a manually added route to interface 188df8bae1dSRodney W. Grimes * such as older version of routed or gated might provide, 189df8bae1dSRodney W. Grimes * restore cloning bit. 190df8bae1dSRodney W. Grimes */ 191df8bae1dSRodney W. Grimes if ((rt->rt_flags & RTF_HOST) == 0 && 192df8bae1dSRodney W. Grimes SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 193df8bae1dSRodney W. Grimes rt->rt_flags |= RTF_CLONING; 194df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_CLONING) { 195df8bae1dSRodney W. Grimes /* 196df8bae1dSRodney W. Grimes * Case 1: This route should come from a route to iface. 197df8bae1dSRodney W. Grimes */ 198df8bae1dSRodney W. Grimes rt_setgate(rt, rt_key(rt), 199df8bae1dSRodney W. Grimes (struct sockaddr *)&null_sdl); 200df8bae1dSRodney W. Grimes gate = rt->rt_gateway; 201df8bae1dSRodney W. Grimes SDL(gate)->sdl_type = rt->rt_ifp->if_type; 202df8bae1dSRodney W. Grimes SDL(gate)->sdl_index = rt->rt_ifp->if_index; 203227ee8a1SPoul-Henning Kamp rt->rt_expire = time_second; 204df8bae1dSRodney W. Grimes break; 205df8bae1dSRodney W. Grimes } 206df8bae1dSRodney W. Grimes /* Announce a new entry if requested. */ 207df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_ANNOUNCE) 208322dcb8dSMax Khon arprequest(rt->rt_ifp, 209ed7509acSJulian Elischer &SIN(rt_key(rt))->sin_addr, 210ed7509acSJulian Elischer &SIN(rt_key(rt))->sin_addr, 211df8bae1dSRodney W. Grimes (u_char *)LLADDR(SDL(gate))); 212df8bae1dSRodney W. Grimes /*FALLTHROUGH*/ 213df8bae1dSRodney W. Grimes case RTM_RESOLVE: 214df8bae1dSRodney W. Grimes if (gate->sa_family != AF_LINK || 215df8bae1dSRodney W. Grimes gate->sa_len < sizeof(null_sdl)) { 216d1dd20beSSam Leffler log(LOG_DEBUG, "%s: bad gateway %s%s\n", __func__, 217beb2ced8SBruce M Simpson inet_ntoa(SIN(rt_key(rt))->sin_addr), 218beb2ced8SBruce M Simpson (gate->sa_family != AF_LINK) ? 219c3b52d64SBruce M Simpson " (!AF_LINK)": ""); 220df8bae1dSRodney W. Grimes break; 221df8bae1dSRodney W. Grimes } 222df8bae1dSRodney W. Grimes SDL(gate)->sdl_type = rt->rt_ifp->if_type; 223df8bae1dSRodney W. Grimes SDL(gate)->sdl_index = rt->rt_ifp->if_index; 224df8bae1dSRodney W. Grimes if (la != 0) 225df8bae1dSRodney W. Grimes break; /* This happens on a route change */ 226df8bae1dSRodney W. Grimes /* 227df8bae1dSRodney W. Grimes * Case 2: This route may come from cloning, or a manual route 228df8bae1dSRodney W. Grimes * add with a LL address. 229df8bae1dSRodney W. Grimes */ 230d1dd20beSSam Leffler R_Zalloc(la, struct llinfo_arp *, sizeof(*la)); 231df8bae1dSRodney W. Grimes rt->rt_llinfo = (caddr_t)la; 232df8bae1dSRodney W. Grimes if (la == 0) { 233d1dd20beSSam Leffler log(LOG_DEBUG, "%s: malloc failed\n", __func__); 234df8bae1dSRodney W. Grimes break; 235df8bae1dSRodney W. Grimes } 236d1dd20beSSam Leffler arp_allocated++; 237df8bae1dSRodney W. Grimes la->la_rt = rt; 238df8bae1dSRodney W. Grimes rt->rt_flags |= RTF_LLINFO; 23993f79889SJeffrey Hsu RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]); 24066800f57SGarrett Wollman LIST_INSERT_HEAD(&llinfo_arp, la, la_le); 241cbb0b46aSGarrett Wollman 2421d5e9e22SEivind Eklund #ifdef INET 243cbb0b46aSGarrett Wollman /* 244cbb0b46aSGarrett Wollman * This keeps the multicast addresses from showing up 245cbb0b46aSGarrett Wollman * in `arp -a' listings as unresolved. It's not actually 246cbb0b46aSGarrett Wollman * functional. Then the same for broadcast. 247cbb0b46aSGarrett Wollman */ 248c448c89cSJonathan Lemon if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) && 249c448c89cSJonathan Lemon rt->rt_ifp->if_type != IFT_ARCNET) { 250cbb0b46aSGarrett Wollman ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr, 251cbb0b46aSGarrett Wollman LLADDR(SDL(gate))); 252cbb0b46aSGarrett Wollman SDL(gate)->sdl_alen = 6; 25351a109a1SPeter Wemm rt->rt_expire = 0; 254cbb0b46aSGarrett Wollman } 255cbb0b46aSGarrett Wollman if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) { 256322dcb8dSMax Khon memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr, 257322dcb8dSMax Khon rt->rt_ifp->if_addrlen); 258322dcb8dSMax Khon SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen; 25951a109a1SPeter Wemm rt->rt_expire = 0; 260cbb0b46aSGarrett Wollman } 2611d5e9e22SEivind Eklund #endif 262cbb0b46aSGarrett Wollman 263df8bae1dSRodney W. Grimes if (SIN(rt_key(rt))->sin_addr.s_addr == 264df8bae1dSRodney W. Grimes (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) { 265df8bae1dSRodney W. Grimes /* 266df8bae1dSRodney W. Grimes * This test used to be 267df8bae1dSRodney W. Grimes * if (loif.if_flags & IFF_UP) 268df8bae1dSRodney W. Grimes * It allowed local traffic to be forced 269df8bae1dSRodney W. Grimes * through the hardware by configuring the loopback down. 270df8bae1dSRodney W. Grimes * However, it causes problems during network configuration 271df8bae1dSRodney W. Grimes * for boards that can't receive packets they send. 272df8bae1dSRodney W. Grimes * It is now necessary to clear "useloopback" and remove 273df8bae1dSRodney W. Grimes * the route to force traffic out to the hardware. 274df8bae1dSRodney W. Grimes */ 275df8bae1dSRodney W. Grimes rt->rt_expire = 0; 276322dcb8dSMax Khon Bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)), 277322dcb8dSMax Khon SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen); 278df8bae1dSRodney W. Grimes if (useloopback) 279f5fea3ddSPaul Traina rt->rt_ifp = loif; 280df8bae1dSRodney W. Grimes 281df8bae1dSRodney W. Grimes } 282df8bae1dSRodney W. Grimes break; 283df8bae1dSRodney W. Grimes 284df8bae1dSRodney W. Grimes case RTM_DELETE: 285df8bae1dSRodney W. Grimes if (la == 0) 286df8bae1dSRodney W. Grimes break; 28793f79889SJeffrey Hsu RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]); 28866800f57SGarrett Wollman LIST_REMOVE(la, la_le); 289df8bae1dSRodney W. Grimes rt->rt_llinfo = 0; 290df8bae1dSRodney W. Grimes rt->rt_flags &= ~RTF_LLINFO; 291df8bae1dSRodney W. Grimes if (la->la_hold) 292df8bae1dSRodney W. Grimes m_freem(la->la_hold); 293df8bae1dSRodney W. Grimes Free((caddr_t)la); 294df8bae1dSRodney W. Grimes } 295df8bae1dSRodney W. Grimes } 296df8bae1dSRodney W. Grimes 297df8bae1dSRodney W. Grimes /* 298df8bae1dSRodney W. Grimes * Broadcast an ARP request. Caller specifies: 299df8bae1dSRodney W. Grimes * - arp header source ip address 300df8bae1dSRodney W. Grimes * - arp header target ip address 301df8bae1dSRodney W. Grimes * - arp header source ethernet address 302df8bae1dSRodney W. Grimes */ 303df8bae1dSRodney W. Grimes static void 304322dcb8dSMax Khon arprequest(ifp, sip, tip, enaddr) 305e952fa39SMatthew N. Dodd struct ifnet *ifp; 306e952fa39SMatthew N. Dodd struct in_addr *sip, *tip; 307e952fa39SMatthew N. Dodd u_char *enaddr; 308df8bae1dSRodney W. Grimes { 309e952fa39SMatthew N. Dodd struct mbuf *m; 310e952fa39SMatthew N. Dodd struct arphdr *ah; 311df8bae1dSRodney W. Grimes struct sockaddr sa; 312df8bae1dSRodney W. Grimes 313a163d034SWarner Losh if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 314df8bae1dSRodney W. Grimes return; 31564bf80ceSMatthew N. Dodd m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) + 31664bf80ceSMatthew N. Dodd 2*ifp->if_data.ifi_addrlen; 31764bf80ceSMatthew N. Dodd m->m_pkthdr.len = m->m_len; 31864bf80ceSMatthew N. Dodd MH_ALIGN(m, m->m_len); 31964bf80ceSMatthew N. Dodd ah = mtod(m, struct arphdr *); 32064bf80ceSMatthew N. Dodd bzero((caddr_t)ah, m->m_len); 32119527d3eSRobert Watson #ifdef MAC 32219527d3eSRobert Watson mac_create_mbuf_linklayer(ifp, m); 32319527d3eSRobert Watson #endif 324322dcb8dSMax Khon ah->ar_pro = htons(ETHERTYPE_IP); 325322dcb8dSMax Khon ah->ar_hln = ifp->if_addrlen; /* hardware address length */ 326322dcb8dSMax Khon ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ 327322dcb8dSMax Khon ah->ar_op = htons(ARPOP_REQUEST); 32864bf80ceSMatthew N. Dodd bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln); 32964bf80ceSMatthew N. Dodd bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln); 33064bf80ceSMatthew N. Dodd bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln); 33164bf80ceSMatthew N. Dodd sa.sa_family = AF_ARP; 33264bf80ceSMatthew N. Dodd sa.sa_len = 2; 33364bf80ceSMatthew N. Dodd m->m_flags |= M_BCAST; 334322dcb8dSMax Khon (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 33564bf80ceSMatthew N. Dodd 33664bf80ceSMatthew N. Dodd return; 337df8bae1dSRodney W. Grimes } 338df8bae1dSRodney W. Grimes 339df8bae1dSRodney W. Grimes /* 340df8bae1dSRodney W. Grimes * Resolve an IP address into an ethernet address. If success, 341df8bae1dSRodney W. Grimes * desten is filled in. If there is no entry in arptab, 342df8bae1dSRodney W. Grimes * set one up and broadcast a request for the IP address. 343df8bae1dSRodney W. Grimes * Hold onto this mbuf and resend it once the address 344df8bae1dSRodney W. Grimes * is finally resolved. A return value of 1 indicates 345df8bae1dSRodney W. Grimes * that desten has been filled in and the packet should be sent 346df8bae1dSRodney W. Grimes * normally; a 0 return indicates that the packet has been 347df8bae1dSRodney W. Grimes * taken over here, either now or for later transmission. 348df8bae1dSRodney W. Grimes */ 349df8bae1dSRodney W. Grimes int 350322dcb8dSMax Khon arpresolve(ifp, rt, m, dst, desten, rt0) 351e952fa39SMatthew N. Dodd struct ifnet *ifp; 352e952fa39SMatthew N. Dodd struct rtentry *rt; 353df8bae1dSRodney W. Grimes struct mbuf *m; 354e952fa39SMatthew N. Dodd struct sockaddr *dst; 355e952fa39SMatthew N. Dodd u_char *desten; 3565df72964SGarrett Wollman struct rtentry *rt0; 357df8bae1dSRodney W. Grimes { 35808aadfbbSJonathan Lemon struct llinfo_arp *la = 0; 359df8bae1dSRodney W. Grimes struct sockaddr_dl *sdl; 360df8bae1dSRodney W. Grimes 361df8bae1dSRodney W. Grimes if (m->m_flags & M_BCAST) { /* broadcast */ 362322dcb8dSMax Khon (void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen); 363df8bae1dSRodney W. Grimes return (1); 364df8bae1dSRodney W. Grimes } 365322dcb8dSMax Khon if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */ 366df8bae1dSRodney W. Grimes ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 367df8bae1dSRodney W. Grimes return(1); 368df8bae1dSRodney W. Grimes } 369df8bae1dSRodney W. Grimes if (rt) 370df8bae1dSRodney W. Grimes la = (struct llinfo_arp *)rt->rt_llinfo; 371e7bc5f27SBill Fenner if (la == 0) { 372623ae52eSPoul-Henning Kamp la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0); 373623ae52eSPoul-Henning Kamp if (la) 374df8bae1dSRodney W. Grimes rt = la->la_rt; 375df8bae1dSRodney W. Grimes } 376df8bae1dSRodney W. Grimes if (la == 0 || rt == 0) { 377245086a0SPoul-Henning Kamp log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n", 378245086a0SPoul-Henning Kamp inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "", 379245086a0SPoul-Henning Kamp rt ? "rt" : ""); 380df8bae1dSRodney W. Grimes m_freem(m); 381df8bae1dSRodney W. Grimes return (0); 382df8bae1dSRodney W. Grimes } 383df8bae1dSRodney W. Grimes sdl = SDL(rt->rt_gateway); 384df8bae1dSRodney W. Grimes /* 385df8bae1dSRodney W. Grimes * Check the address family and length is valid, the address 386df8bae1dSRodney W. Grimes * is resolved; otherwise, try to resolve. 387df8bae1dSRodney W. Grimes */ 388227ee8a1SPoul-Henning Kamp if ((rt->rt_expire == 0 || rt->rt_expire > time_second) && 389df8bae1dSRodney W. Grimes sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { 390f0f3379eSOrion Hodson /* 391f0f3379eSOrion Hodson * If entry has an expiry time and it is approaching, 392f0f3379eSOrion Hodson * see if we need to send an ARP request within this 393f0f3379eSOrion Hodson * arpt_down interval. 394f0f3379eSOrion Hodson */ 395f0f3379eSOrion Hodson if ((rt->rt_expire != 0) && 396022695f8SOrion Hodson (time_second + la->la_preempt > rt->rt_expire)) { 397f0f3379eSOrion Hodson arprequest(ifp, 398f0f3379eSOrion Hodson &SIN(rt->rt_ifa->ifa_addr)->sin_addr, 399f0f3379eSOrion Hodson &SIN(dst)->sin_addr, 400f0f3379eSOrion Hodson IF_LLADDR(ifp)); 401022695f8SOrion Hodson la->la_preempt--; 402f0f3379eSOrion Hodson } 403f0f3379eSOrion Hodson 4040453d3cbSBruce Evans bcopy(LLADDR(sdl), desten, sdl->sdl_alen); 405df8bae1dSRodney W. Grimes return 1; 406df8bae1dSRodney W. Grimes } 407df8bae1dSRodney W. Grimes /* 408deb62e28SRuslan Ermilov * If ARP is disabled or static on this interface, stop. 40908aadfbbSJonathan Lemon * XXX 41008aadfbbSJonathan Lemon * Probably should not allocate empty llinfo struct if we are 41108aadfbbSJonathan Lemon * not going to be sending out an arp request. 41208aadfbbSJonathan Lemon */ 413deb62e28SRuslan Ermilov if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) { 41447891de1SRuslan Ermilov m_freem(m); 41508aadfbbSJonathan Lemon return (0); 41647891de1SRuslan Ermilov } 41708aadfbbSJonathan Lemon /* 418df8bae1dSRodney W. Grimes * There is an arptab entry, but no ethernet address 419df8bae1dSRodney W. Grimes * response yet. Replace the held mbuf with this 420df8bae1dSRodney W. Grimes * latest one. 421df8bae1dSRodney W. Grimes */ 422df8bae1dSRodney W. Grimes if (la->la_hold) 423df8bae1dSRodney W. Grimes m_freem(la->la_hold); 424df8bae1dSRodney W. Grimes la->la_hold = m; 425df8bae1dSRodney W. Grimes if (rt->rt_expire) { 426d1dd20beSSam Leffler RT_LOCK(rt); 427df8bae1dSRodney W. Grimes rt->rt_flags &= ~RTF_REJECT; 428227ee8a1SPoul-Henning Kamp if (la->la_asked == 0 || rt->rt_expire != time_second) { 429227ee8a1SPoul-Henning Kamp rt->rt_expire = time_second; 43073224fb0SOrion Hodson if (la->la_asked++ < arp_maxtries) { 431322dcb8dSMax Khon arprequest(ifp, 432ed7509acSJulian Elischer &SIN(rt->rt_ifa->ifa_addr)->sin_addr, 433322dcb8dSMax Khon &SIN(dst)->sin_addr, 434322dcb8dSMax Khon IF_LLADDR(ifp)); 43573224fb0SOrion Hodson } else { 436df8bae1dSRodney W. Grimes rt->rt_flags |= RTF_REJECT; 437df8bae1dSRodney W. Grimes rt->rt_expire += arpt_down; 438022695f8SOrion Hodson la->la_asked = 0; 439022695f8SOrion Hodson la->la_preempt = arp_maxtries; 440df8bae1dSRodney W. Grimes } 441df8bae1dSRodney W. Grimes 442df8bae1dSRodney W. Grimes } 443d1dd20beSSam Leffler RT_UNLOCK(rt); 444df8bae1dSRodney W. Grimes } 445df8bae1dSRodney W. Grimes return (0); 446df8bae1dSRodney W. Grimes } 447df8bae1dSRodney W. Grimes 448df8bae1dSRodney W. Grimes /* 449df8bae1dSRodney W. Grimes * Common length and type checks are done here, 450df8bae1dSRodney W. Grimes * then the protocol-specific routine is called. 451df8bae1dSRodney W. Grimes */ 452885f1aa4SPoul-Henning Kamp static void 4531cafed39SJonathan Lemon arpintr(struct mbuf *m) 454df8bae1dSRodney W. Grimes { 4551cafed39SJonathan Lemon struct arphdr *ar; 456df8bae1dSRodney W. Grimes 457532cf61bSPeter Wemm if (!arpinit_done) { 458d1dd20beSSam Leffler /* NB: this race should not matter */ 459532cf61bSPeter Wemm arpinit_done = 1; 460d1dd20beSSam Leffler callout_reset(&arp_callout, hz, arptimer, NULL); 461532cf61bSPeter Wemm } 46276ec7b2fSRobert Watson if (m->m_len < sizeof(struct arphdr) && 46384365e2bSMatthew Dillon ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) { 464e44d6283SJoerg Wunsch log(LOG_ERR, "arp: runt packet -- m_pullup failed\n"); 4651cafed39SJonathan Lemon return; 46676ec7b2fSRobert Watson } 46776ec7b2fSRobert Watson ar = mtod(m, struct arphdr *); 46876ec7b2fSRobert Watson 4691cafed39SJonathan Lemon if (ntohs(ar->ar_hrd) != ARPHRD_ETHER && 4701cafed39SJonathan Lemon ntohs(ar->ar_hrd) != ARPHRD_IEEE802 && 4711cafed39SJonathan Lemon ntohs(ar->ar_hrd) != ARPHRD_ARCNET) { 4721cafed39SJonathan Lemon log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n", 47376ec7b2fSRobert Watson (unsigned char *)&ar->ar_hrd, ""); 47476ec7b2fSRobert Watson m_freem(m); 4751cafed39SJonathan Lemon return; 47676ec7b2fSRobert Watson } 47776ec7b2fSRobert Watson 47831175791SRuslan Ermilov if (m->m_len < arphdr_len(ar)) { 47978e2d2bdSRuslan Ermilov if ((m = m_pullup(m, arphdr_len(ar))) == NULL) { 480f72d9d83SJoerg Wunsch log(LOG_ERR, "arp: runt packet\n"); 48176ec7b2fSRobert Watson m_freem(m); 4821cafed39SJonathan Lemon return; 48376ec7b2fSRobert Watson } 48478e2d2bdSRuslan Ermilov ar = mtod(m, struct arphdr *); 48578e2d2bdSRuslan Ermilov } 486df8bae1dSRodney W. Grimes 487df8bae1dSRodney W. Grimes switch (ntohs(ar->ar_pro)) { 4881d5e9e22SEivind Eklund #ifdef INET 489df8bae1dSRodney W. Grimes case ETHERTYPE_IP: 490df8bae1dSRodney W. Grimes in_arpinput(m); 4911cafed39SJonathan Lemon return; 4921d5e9e22SEivind Eklund #endif 493df8bae1dSRodney W. Grimes } 494df8bae1dSRodney W. Grimes m_freem(m); 495df8bae1dSRodney W. Grimes } 496df8bae1dSRodney W. Grimes 4971d5e9e22SEivind Eklund #ifdef INET 498df8bae1dSRodney W. Grimes /* 499df8bae1dSRodney W. Grimes * ARP for Internet protocols on 10 Mb/s Ethernet. 500df8bae1dSRodney W. Grimes * Algorithm is that given in RFC 826. 501df8bae1dSRodney W. Grimes * In addition, a sanity check is performed on the sender 502df8bae1dSRodney W. Grimes * protocol address, to catch impersonators. 503df8bae1dSRodney W. Grimes * We no longer handle negotiations for use of trailer protocol: 504df8bae1dSRodney W. Grimes * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 505df8bae1dSRodney W. Grimes * along with IP replies if we wanted trailers sent to us, 506df8bae1dSRodney W. Grimes * and also sent them in response to IP replies. 507df8bae1dSRodney W. Grimes * This allowed either end to announce the desire to receive 508df8bae1dSRodney W. Grimes * trailer packets. 509df8bae1dSRodney W. Grimes * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 510df8bae1dSRodney W. Grimes * but formerly didn't normally send requests. 511df8bae1dSRodney W. Grimes */ 5123269187dSAlfred Perlstein static int log_arp_wrong_iface = 1; 513e3d123d6SAlfred Perlstein static int log_arp_movements = 1; 5143269187dSAlfred Perlstein 5153269187dSAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW, 5163269187dSAlfred Perlstein &log_arp_wrong_iface, 0, 5173269187dSAlfred Perlstein "log arp packets arriving on the wrong interface"); 518e3d123d6SAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW, 519e3d123d6SAlfred Perlstein &log_arp_movements, 0, 52075ce3221SAlfred Perlstein "log arp replies from MACs different than the one in the cache"); 521e3d123d6SAlfred Perlstein 5223269187dSAlfred Perlstein 523df8bae1dSRodney W. Grimes static void 524df8bae1dSRodney W. Grimes in_arpinput(m) 525df8bae1dSRodney W. Grimes struct mbuf *m; 526df8bae1dSRodney W. Grimes { 527e952fa39SMatthew N. Dodd struct arphdr *ah; 528e952fa39SMatthew N. Dodd struct ifnet *ifp = m->m_pkthdr.rcvif; 529fda82fc2SJulian Elischer struct iso88025_header *th = (struct iso88025_header *)0; 53042fdfc12SKelly Yancey struct iso88025_sockaddr_dl_data *trld; 531e952fa39SMatthew N. Dodd struct llinfo_arp *la = 0; 532e952fa39SMatthew N. Dodd struct rtentry *rt; 533ca925d9cSJonathan Lemon struct ifaddr *ifa; 534ca925d9cSJonathan Lemon struct in_ifaddr *ia; 535df8bae1dSRodney W. Grimes struct sockaddr_dl *sdl; 536df8bae1dSRodney W. Grimes struct sockaddr sa; 537df8bae1dSRodney W. Grimes struct in_addr isaddr, itaddr, myaddr; 538b149dd6cSLarry Lile int op, rif_len; 539322dcb8dSMax Khon int req_len; 540df8bae1dSRodney W. Grimes 541322dcb8dSMax Khon req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 542322dcb8dSMax Khon if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) { 5434cbc8ad1SYaroslav Tykhiy log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n"); 5444cbc8ad1SYaroslav Tykhiy return; 5454cbc8ad1SYaroslav Tykhiy } 5464cbc8ad1SYaroslav Tykhiy 547322dcb8dSMax Khon ah = mtod(m, struct arphdr *); 548322dcb8dSMax Khon op = ntohs(ah->ar_op); 549322dcb8dSMax Khon (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr)); 550322dcb8dSMax Khon (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr)); 55141d2ba5eSJulian Elischer #ifdef BRIDGE 55241d2ba5eSJulian Elischer #define BRIDGE_TEST (do_bridge) 553b715f178SLuigi Rizzo #else 55441d2ba5eSJulian Elischer #define BRIDGE_TEST (0) /* cc will optimise the test away */ 555b715f178SLuigi Rizzo #endif 556ca925d9cSJonathan Lemon /* 557ca925d9cSJonathan Lemon * For a bridge, we want to check the address irrespective 558ca925d9cSJonathan Lemon * of the receive interface. (This will change slightly 559ca925d9cSJonathan Lemon * when we have clusters of interfaces). 560ca925d9cSJonathan Lemon */ 561ca925d9cSJonathan Lemon LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) 562322dcb8dSMax Khon if ((BRIDGE_TEST || (ia->ia_ifp == ifp)) && 563ca925d9cSJonathan Lemon itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) 564ca925d9cSJonathan Lemon goto match; 565ca925d9cSJonathan Lemon LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash) 566322dcb8dSMax Khon if ((BRIDGE_TEST || (ia->ia_ifp == ifp)) && 567ca925d9cSJonathan Lemon isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) 568ca925d9cSJonathan Lemon goto match; 569ca925d9cSJonathan Lemon /* 570d8b84d9eSJonathan Lemon * No match, use the first inet address on the receive interface 571ca925d9cSJonathan Lemon * as a dummy address for the rest of the function. 572ca925d9cSJonathan Lemon */ 573d8b84d9eSJonathan Lemon TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 574ec691a10SJonathan Lemon if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) { 575ec691a10SJonathan Lemon ia = ifatoia(ifa); 576ec691a10SJonathan Lemon goto match; 577ec691a10SJonathan Lemon } 578ec691a10SJonathan Lemon /* 579ec691a10SJonathan Lemon * If bridging, fall back to using any inet address. 580ec691a10SJonathan Lemon */ 581ec691a10SJonathan Lemon if (!BRIDGE_TEST || 582ec691a10SJonathan Lemon (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL) { 583885f1aa4SPoul-Henning Kamp m_freem(m); 584885f1aa4SPoul-Henning Kamp return; 585885f1aa4SPoul-Henning Kamp } 586ca925d9cSJonathan Lemon match: 587ca925d9cSJonathan Lemon myaddr = ia->ia_addr.sin_addr; 588322dcb8dSMax Khon if (!bcmp(ar_sha(ah), IF_LLADDR(ifp), ifp->if_addrlen)) { 589885f1aa4SPoul-Henning Kamp m_freem(m); /* it's from me, ignore it. */ 590885f1aa4SPoul-Henning Kamp return; 591885f1aa4SPoul-Henning Kamp } 592322dcb8dSMax Khon if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) { 593df8bae1dSRodney W. Grimes log(LOG_ERR, 594322dcb8dSMax Khon "arp: link address is broadcast for IP address %s!\n", 595ef0cdf33SGarrett Wollman inet_ntoa(isaddr)); 596885f1aa4SPoul-Henning Kamp m_freem(m); 597885f1aa4SPoul-Henning Kamp return; 598df8bae1dSRodney W. Grimes } 599df8bae1dSRodney W. Grimes if (isaddr.s_addr == myaddr.s_addr) { 600df8bae1dSRodney W. Grimes log(LOG_ERR, 601322dcb8dSMax Khon "arp: %*D is using my IP address %s!\n", 602322dcb8dSMax Khon ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 603322dcb8dSMax Khon inet_ntoa(isaddr)); 604df8bae1dSRodney W. Grimes itaddr = myaddr; 605df8bae1dSRodney W. Grimes goto reply; 606df8bae1dSRodney W. Grimes } 607deb62e28SRuslan Ermilov if (ifp->if_flags & IFF_STATICARP) 608deb62e28SRuslan Ermilov goto reply; 609df8bae1dSRodney W. Grimes la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0); 610df8bae1dSRodney W. Grimes if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) { 6117e1cd0d2SLuigi Rizzo /* the following is not an error when doing bridging */ 612322dcb8dSMax Khon if (!BRIDGE_TEST && rt->rt_ifp != ifp) { 6133269187dSAlfred Perlstein if (log_arp_wrong_iface) 6149bf40edeSBrooks Davis log(LOG_ERR, "arp: %s is on %s but got reply from %*D on %s\n", 615dd9b6ddeSBill Fenner inet_ntoa(isaddr), 6169bf40edeSBrooks Davis rt->rt_ifp->if_xname, 617322dcb8dSMax Khon ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 6189bf40edeSBrooks Davis ifp->if_xname); 619dd9b6ddeSBill Fenner goto reply; 620dd9b6ddeSBill Fenner } 621df8bae1dSRodney W. Grimes if (sdl->sdl_alen && 622322dcb8dSMax Khon bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) { 623e3d123d6SAlfred Perlstein if (rt->rt_expire) { 624e3d123d6SAlfred Perlstein if (log_arp_movements) 6259bf40edeSBrooks Davis log(LOG_INFO, "arp: %s moved from %*D to %*D on %s\n", 626322dcb8dSMax Khon inet_ntoa(isaddr), 627322dcb8dSMax Khon ifp->if_addrlen, (u_char *)LLADDR(sdl), ":", 628322dcb8dSMax Khon ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 6299bf40edeSBrooks Davis ifp->if_xname); 630e3d123d6SAlfred Perlstein } else { 631dd9b6ddeSBill Fenner log(LOG_ERR, 6329bf40edeSBrooks Davis "arp: %*D attempts to modify permanent entry for %s on %s\n", 633322dcb8dSMax Khon ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 6349bf40edeSBrooks Davis inet_ntoa(isaddr), ifp->if_xname); 635dd9b6ddeSBill Fenner goto reply; 636dd9b6ddeSBill Fenner } 637dfd5dee1SPeter Wemm } 638322dcb8dSMax Khon /* 639322dcb8dSMax Khon * sanity check for the address length. 640322dcb8dSMax Khon * XXX this does not work for protocols with variable address 641322dcb8dSMax Khon * length. -is 642322dcb8dSMax Khon */ 643322dcb8dSMax Khon if (sdl->sdl_alen && 644322dcb8dSMax Khon sdl->sdl_alen != ah->ar_hln) { 645322dcb8dSMax Khon log(LOG_WARNING, 646322dcb8dSMax Khon "arp from %*D: new addr len %d, was %d", 647322dcb8dSMax Khon ifp->if_addrlen, (u_char *) ar_sha(ah), ":", 648322dcb8dSMax Khon ah->ar_hln, sdl->sdl_alen); 649322dcb8dSMax Khon } 650322dcb8dSMax Khon if (ifp->if_addrlen != ah->ar_hln) { 651322dcb8dSMax Khon log(LOG_WARNING, 652322dcb8dSMax Khon "arp from %*D: addr len: new %d, i/f %d (ignored)", 653322dcb8dSMax Khon ifp->if_addrlen, (u_char *) ar_sha(ah), ":", 654322dcb8dSMax Khon ah->ar_hln, ifp->if_addrlen); 655322dcb8dSMax Khon goto reply; 656322dcb8dSMax Khon } 657322dcb8dSMax Khon (void)memcpy(LLADDR(sdl), ar_sha(ah), 658322dcb8dSMax Khon sdl->sdl_alen = ah->ar_hln); 659243c4c6dSEivind Eklund /* 660243c4c6dSEivind Eklund * If we receive an arp from a token-ring station over 661243c4c6dSEivind Eklund * a token-ring nic then try to save the source 662243c4c6dSEivind Eklund * routing info. 663243c4c6dSEivind Eklund */ 664322dcb8dSMax Khon if (ifp->if_type == IFT_ISO88025) { 665fda82fc2SJulian Elischer th = (struct iso88025_header *)m->m_pkthdr.header; 66642fdfc12SKelly Yancey trld = SDL_ISO88025(sdl); 667b149dd6cSLarry Lile rif_len = TR_RCF_RIFLEN(th->rcf); 668b149dd6cSLarry Lile if ((th->iso88025_shost[0] & TR_RII) && 669b149dd6cSLarry Lile (rif_len > 2)) { 67042fdfc12SKelly Yancey trld->trld_rcf = th->rcf; 67142fdfc12SKelly Yancey trld->trld_rcf ^= htons(TR_RCF_DIR); 67242fdfc12SKelly Yancey memcpy(trld->trld_route, th->rd, rif_len - 2); 67342fdfc12SKelly Yancey trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK); 674f9083fdbSLarry Lile /* 675f9083fdbSLarry Lile * Set up source routing information for 676f9083fdbSLarry Lile * reply packet (XXX) 677f9083fdbSLarry Lile */ 678b149dd6cSLarry Lile m->m_data -= rif_len; 679b149dd6cSLarry Lile m->m_len += rif_len; 680b149dd6cSLarry Lile m->m_pkthdr.len += rif_len; 681fda82fc2SJulian Elischer } else { 682b149dd6cSLarry Lile th->iso88025_shost[0] &= ~TR_RII; 683c3a2190cSKelly Yancey trld->trld_rcf = 0; 684fcf11853SLarry Lile } 685fda82fc2SJulian Elischer m->m_data -= 8; 686fda82fc2SJulian Elischer m->m_len += 8; 687fcf11853SLarry Lile m->m_pkthdr.len += 8; 68842fdfc12SKelly Yancey th->rcf = trld->trld_rcf; 689fda82fc2SJulian Elischer } 690d1dd20beSSam Leffler RT_LOCK(rt); 691df8bae1dSRodney W. Grimes if (rt->rt_expire) 692227ee8a1SPoul-Henning Kamp rt->rt_expire = time_second + arpt_keep; 693df8bae1dSRodney W. Grimes rt->rt_flags &= ~RTF_REJECT; 694d1dd20beSSam Leffler RT_UNLOCK(rt); 695022695f8SOrion Hodson la->la_asked = 0; 696022695f8SOrion Hodson la->la_preempt = arp_maxtries; 697df8bae1dSRodney W. Grimes if (la->la_hold) { 698322dcb8dSMax Khon (*ifp->if_output)(ifp, la->la_hold, 699df8bae1dSRodney W. Grimes rt_key(rt), rt); 700df8bae1dSRodney W. Grimes la->la_hold = 0; 701df8bae1dSRodney W. Grimes } 702df8bae1dSRodney W. Grimes } 703df8bae1dSRodney W. Grimes reply: 704df8bae1dSRodney W. Grimes if (op != ARPOP_REQUEST) { 705df8bae1dSRodney W. Grimes m_freem(m); 706df8bae1dSRodney W. Grimes return; 707df8bae1dSRodney W. Grimes } 708df8bae1dSRodney W. Grimes if (itaddr.s_addr == myaddr.s_addr) { 709df8bae1dSRodney W. Grimes /* I am the target */ 710322dcb8dSMax Khon (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 711322dcb8dSMax Khon (void)memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln); 712df8bae1dSRodney W. Grimes } else { 713df8bae1dSRodney W. Grimes la = arplookup(itaddr.s_addr, 0, SIN_PROXY); 71428e82295SGarrett Wollman if (la == NULL) { 71528e82295SGarrett Wollman struct sockaddr_in sin; 71628e82295SGarrett Wollman 717885f1aa4SPoul-Henning Kamp if (!arp_proxyall) { 718885f1aa4SPoul-Henning Kamp m_freem(m); 719885f1aa4SPoul-Henning Kamp return; 720885f1aa4SPoul-Henning Kamp } 72128e82295SGarrett Wollman 72228e82295SGarrett Wollman bzero(&sin, sizeof sin); 72328e82295SGarrett Wollman sin.sin_family = AF_INET; 72428e82295SGarrett Wollman sin.sin_len = sizeof sin; 72528e82295SGarrett Wollman sin.sin_addr = itaddr; 72628e82295SGarrett Wollman 72731246bc2SGarrett Wollman rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 728885f1aa4SPoul-Henning Kamp if (!rt) { 729885f1aa4SPoul-Henning Kamp m_freem(m); 730885f1aa4SPoul-Henning Kamp return; 731885f1aa4SPoul-Henning Kamp } 73228e82295SGarrett Wollman /* 73328e82295SGarrett Wollman * Don't send proxies for nodes on the same interface 73428e82295SGarrett Wollman * as this one came out of, or we'll get into a fight 73528e82295SGarrett Wollman * over who claims what Ether address. 73628e82295SGarrett Wollman */ 737322dcb8dSMax Khon if (rt->rt_ifp == ifp) { 73828e82295SGarrett Wollman rtfree(rt); 739885f1aa4SPoul-Henning Kamp m_freem(m); 740885f1aa4SPoul-Henning Kamp return; 74128e82295SGarrett Wollman } 742322dcb8dSMax Khon (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 743322dcb8dSMax Khon (void)memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln); 74428e82295SGarrett Wollman rtfree(rt); 745cc728227SDavid Malone 746cc728227SDavid Malone /* 747cc728227SDavid Malone * Also check that the node which sent the ARP packet 748cc728227SDavid Malone * is on the the interface we expect it to be on. This 749cc728227SDavid Malone * avoids ARP chaos if an interface is connected to the 750cc728227SDavid Malone * wrong network. 751cc728227SDavid Malone */ 752cc728227SDavid Malone sin.sin_addr = isaddr; 753cc728227SDavid Malone 754cc728227SDavid Malone rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 755cc728227SDavid Malone if (!rt) { 756cc728227SDavid Malone m_freem(m); 757cc728227SDavid Malone return; 758cc728227SDavid Malone } 759322dcb8dSMax Khon if (rt->rt_ifp != ifp) { 760cc728227SDavid Malone log(LOG_INFO, "arp_proxy: ignoring request" 7619bf40edeSBrooks Davis " from %s via %s, expecting %s\n", 7629bf40edeSBrooks Davis inet_ntoa(isaddr), ifp->if_xname, 7639bf40edeSBrooks Davis rt->rt_ifp->if_xname); 764cc728227SDavid Malone rtfree(rt); 765cc728227SDavid Malone m_freem(m); 766cc728227SDavid Malone return; 767cc728227SDavid Malone } 768cc728227SDavid Malone rtfree(rt); 769cc728227SDavid Malone 770ac234f93SGarrett Wollman #ifdef DEBUG_PROXY 771ac234f93SGarrett Wollman printf("arp: proxying for %s\n", 772ef0cdf33SGarrett Wollman inet_ntoa(itaddr)); 773ac234f93SGarrett Wollman #endif 77428e82295SGarrett Wollman } else { 775df8bae1dSRodney W. Grimes rt = la->la_rt; 776322dcb8dSMax Khon (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 777df8bae1dSRodney W. Grimes sdl = SDL(rt->rt_gateway); 778322dcb8dSMax Khon (void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln); 77928e82295SGarrett Wollman } 780df8bae1dSRodney W. Grimes } 781df8bae1dSRodney W. Grimes 782322dcb8dSMax Khon (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln); 783322dcb8dSMax Khon (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln); 784322dcb8dSMax Khon ah->ar_op = htons(ARPOP_REPLY); 785322dcb8dSMax Khon ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 78664bf80ceSMatthew N. Dodd m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */ 78764bf80ceSMatthew N. Dodd m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln); 78864bf80ceSMatthew N. Dodd m->m_pkthdr.len = m->m_len; 78964bf80ceSMatthew N. Dodd sa.sa_family = AF_ARP; 79064bf80ceSMatthew N. Dodd sa.sa_len = 2; 791322dcb8dSMax Khon (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 792df8bae1dSRodney W. Grimes return; 793df8bae1dSRodney W. Grimes } 7941d5e9e22SEivind Eklund #endif 795df8bae1dSRodney W. Grimes 796df8bae1dSRodney W. Grimes /* 797df8bae1dSRodney W. Grimes * Free an arp entry. 798df8bae1dSRodney W. Grimes */ 799df8bae1dSRodney W. Grimes static void 800df8bae1dSRodney W. Grimes arptfree(la) 801e952fa39SMatthew N. Dodd struct llinfo_arp *la; 802df8bae1dSRodney W. Grimes { 803e952fa39SMatthew N. Dodd struct rtentry *rt = la->la_rt; 804e952fa39SMatthew N. Dodd struct sockaddr_dl *sdl; 805d1dd20beSSam Leffler 806df8bae1dSRodney W. Grimes if (rt == 0) 807df8bae1dSRodney W. Grimes panic("arptfree"); 808df8bae1dSRodney W. Grimes if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) && 809df8bae1dSRodney W. Grimes sdl->sdl_family == AF_LINK) { 810df8bae1dSRodney W. Grimes sdl->sdl_alen = 0; 81173224fb0SOrion Hodson la->la_preempt = la->la_asked = 0; 812d1dd20beSSam Leffler RT_LOCK(rt); /* XXX needed or move higher? */ 813df8bae1dSRodney W. Grimes rt->rt_flags &= ~RTF_REJECT; 814d1dd20beSSam Leffler RT_UNLOCK(rt); 815df8bae1dSRodney W. Grimes return; 816df8bae1dSRodney W. Grimes } 817df8bae1dSRodney W. Grimes rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt), 818df8bae1dSRodney W. Grimes 0, (struct rtentry **)0); 819df8bae1dSRodney W. Grimes } 820df8bae1dSRodney W. Grimes /* 821df8bae1dSRodney W. Grimes * Lookup or enter a new address in arptab. 822df8bae1dSRodney W. Grimes */ 823df8bae1dSRodney W. Grimes static struct llinfo_arp * 824df8bae1dSRodney W. Grimes arplookup(addr, create, proxy) 825df8bae1dSRodney W. Grimes u_long addr; 826df8bae1dSRodney W. Grimes int create, proxy; 827df8bae1dSRodney W. Grimes { 828e952fa39SMatthew N. Dodd struct rtentry *rt; 829d1dd20beSSam Leffler struct sockaddr_inarp sin; 830ac234f93SGarrett Wollman const char *why = 0; 831df8bae1dSRodney W. Grimes 832d1dd20beSSam Leffler bzero(&sin, sizeof(sin)); 833d1dd20beSSam Leffler sin.sin_len = sizeof(sin); 834d1dd20beSSam Leffler sin.sin_family = AF_INET; 835df8bae1dSRodney W. Grimes sin.sin_addr.s_addr = addr; 836d1dd20beSSam Leffler if (proxy) 837d1dd20beSSam Leffler sin.sin_other = SIN_PROXY; 83831246bc2SGarrett Wollman rt = rtalloc1((struct sockaddr *)&sin, create, 0UL); 839df8bae1dSRodney W. Grimes if (rt == 0) 840df8bae1dSRodney W. Grimes return (0); 841ac234f93SGarrett Wollman 842ac234f93SGarrett Wollman if (rt->rt_flags & RTF_GATEWAY) 843ac234f93SGarrett Wollman why = "host is not on local network"; 844ac234f93SGarrett Wollman else if ((rt->rt_flags & RTF_LLINFO) == 0) 845ac234f93SGarrett Wollman why = "could not allocate llinfo"; 846ac234f93SGarrett Wollman else if (rt->rt_gateway->sa_family != AF_LINK) 847ac234f93SGarrett Wollman why = "gateway route is not ours"; 848ac234f93SGarrett Wollman 849fedf1d01SBruce M Simpson if (why) { 850d1dd20beSSam Leffler #define ISDYNCLONE(_rt) \ 851d1dd20beSSam Leffler (((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED) 852d1dd20beSSam Leffler if (create) 853ac234f93SGarrett Wollman log(LOG_DEBUG, "arplookup %s failed: %s\n", 854ef0cdf33SGarrett Wollman inet_ntoa(sin.sin_addr), why); 855b75bead1SBruce M Simpson /* 856b75bead1SBruce M Simpson * If there are no references to this Layer 2 route, 857b75bead1SBruce M Simpson * and it is a cloned route, and not static, and 858b75bead1SBruce M Simpson * arplookup() is creating the route, then purge 859b75bead1SBruce M Simpson * it from the routing table as it is probably bogus. 860b75bead1SBruce M Simpson */ 8619c63e9dbSSam Leffler if (rt->rt_refcnt == 1 && ISDYNCLONE(rt)) 8629c63e9dbSSam Leffler rtexpunge(rt); 8639c63e9dbSSam Leffler RTFREE_LOCKED(rt); 864fedf1d01SBruce M Simpson return (0); 865d1dd20beSSam Leffler #undef ISDYNCLONE 866d1dd20beSSam Leffler } else { 8677138d65cSSam Leffler RT_REMREF(rt); 868d1dd20beSSam Leffler RT_UNLOCK(rt); 869df8bae1dSRodney W. Grimes return ((struct llinfo_arp *)rt->rt_llinfo); 870df8bae1dSRodney W. Grimes } 871d1dd20beSSam Leffler } 872df8bae1dSRodney W. Grimes 873dd2e4102SGarrett Wollman void 874322dcb8dSMax Khon arp_ifinit(ifp, ifa) 875322dcb8dSMax Khon struct ifnet *ifp; 876dd2e4102SGarrett Wollman struct ifaddr *ifa; 877dd2e4102SGarrett Wollman { 878dd570d4dSTor Egge if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 879322dcb8dSMax Khon arprequest(ifp, &IA_SIN(ifa)->sin_addr, 880322dcb8dSMax Khon &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp)); 881dd2e4102SGarrett Wollman ifa->ifa_rtrequest = arp_rtrequest; 882dd2e4102SGarrett Wollman ifa->ifa_flags |= RTF_CLONING; 883dd2e4102SGarrett Wollman } 884df5e1987SJonathan Lemon 885df5e1987SJonathan Lemon static void 886df5e1987SJonathan Lemon arp_init(void) 887df5e1987SJonathan Lemon { 888df5e1987SJonathan Lemon 889df5e1987SJonathan Lemon arpintrq.ifq_maxlen = 50; 8906008862bSJohn Baldwin mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF); 891532cf61bSPeter Wemm LIST_INIT(&llinfo_arp); 892d1dd20beSSam Leffler callout_init(&arp_callout, CALLOUT_MPSAFE); 8937902224cSSam Leffler netisr_register(NETISR_ARP, arpintr, &arpintrq, NETISR_MPSAFE); 894df5e1987SJonathan Lemon } 895df5e1987SJonathan Lemon SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0); 896