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; 108532cf61bSPeter Wemm static int arp_inuse, arp_allocated, arpinit_done; 109df8bae1dSRodney W. Grimes 110885f1aa4SPoul-Henning Kamp static int arp_maxtries = 5; 111602d513cSGarrett Wollman static int useloopback = 1; /* use loopback interface for local traffic */ 112885f1aa4SPoul-Henning Kamp static int arp_proxyall = 0; 113602d513cSGarrett Wollman 114602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW, 115602d513cSGarrett Wollman &arp_maxtries, 0, ""); 116602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW, 117602d513cSGarrett Wollman &useloopback, 0, ""); 118602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW, 119602d513cSGarrett Wollman &arp_proxyall, 0, ""); 120885f1aa4SPoul-Henning Kamp 1214d77a549SAlfred Perlstein static void arp_init(void); 1224d77a549SAlfred Perlstein static void arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *); 1234d77a549SAlfred Perlstein static void arprequest(struct ifnet *, 1244d77a549SAlfred Perlstein struct in_addr *, struct in_addr *, u_char *); 1251cafed39SJonathan Lemon static void arpintr(struct mbuf *); 1264d77a549SAlfred Perlstein static void arptfree(struct llinfo_arp *); 1274d77a549SAlfred Perlstein static void arptimer(void *); 128885f1aa4SPoul-Henning Kamp static struct llinfo_arp 1294d77a549SAlfred Perlstein *arplookup(u_long, int, int); 1301d5e9e22SEivind Eklund #ifdef INET 1314d77a549SAlfred Perlstein static void in_arpinput(struct mbuf *); 1321d5e9e22SEivind Eklund #endif 13328e82295SGarrett Wollman 134df8bae1dSRodney W. Grimes /* 135df8bae1dSRodney W. Grimes * Timeout routine. Age arp_tab entries periodically. 136df8bae1dSRodney W. Grimes */ 137df8bae1dSRodney W. Grimes /* ARGSUSED */ 138df8bae1dSRodney W. Grimes static void 139df8bae1dSRodney W. Grimes arptimer(ignored_arg) 140df8bae1dSRodney W. Grimes void *ignored_arg; 141df8bae1dSRodney W. Grimes { 142c996428cSJeffrey Hsu struct llinfo_arp *la, *ola; 143df8bae1dSRodney W. Grimes int s = splnet(); 144df8bae1dSRodney W. Grimes 14593f79889SJeffrey Hsu RADIX_NODE_HEAD_LOCK(rt_tables[AF_INET]); 146c996428cSJeffrey Hsu la = LIST_FIRST(&llinfo_arp); 147c996428cSJeffrey Hsu while (la != NULL) { 148c996428cSJeffrey Hsu struct rtentry *rt = la->la_rt; 149c996428cSJeffrey Hsu ola = la; 150fc2ffbe6SPoul-Henning Kamp la = LIST_NEXT(la, la_le); 151227ee8a1SPoul-Henning Kamp if (rt->rt_expire && rt->rt_expire <= time_second) 15266800f57SGarrett Wollman arptfree(ola); /* timer has expired, clear */ 153df8bae1dSRodney W. Grimes } 15493f79889SJeffrey Hsu RADIX_NODE_HEAD_UNLOCK(rt_tables[AF_INET]); 155df8bae1dSRodney W. Grimes splx(s); 15693f79889SJeffrey Hsu timeout(arptimer, NULL, arpt_prune * hz); 157df8bae1dSRodney W. Grimes } 158df8bae1dSRodney W. Grimes 159df8bae1dSRodney W. Grimes /* 160df8bae1dSRodney W. Grimes * Parallel to llc_rtrequest. 161df8bae1dSRodney W. Grimes */ 162b2774d00SGarrett Wollman static void 1638071913dSRuslan Ermilov arp_rtrequest(req, rt, info) 164df8bae1dSRodney W. Grimes int req; 165df8bae1dSRodney W. Grimes register struct rtentry *rt; 1668071913dSRuslan Ermilov struct rt_addrinfo *info; 167df8bae1dSRodney W. Grimes { 168df8bae1dSRodney W. Grimes register struct sockaddr *gate = rt->rt_gateway; 169df8bae1dSRodney W. Grimes register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo; 170df8bae1dSRodney W. Grimes static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 171df8bae1dSRodney W. Grimes 172df8bae1dSRodney W. Grimes if (!arpinit_done) { 173df8bae1dSRodney W. Grimes arpinit_done = 1; 174df8bae1dSRodney W. Grimes timeout(arptimer, (caddr_t)0, hz); 175df8bae1dSRodney W. Grimes } 176df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_GATEWAY) 177df8bae1dSRodney W. Grimes return; 178df8bae1dSRodney W. Grimes switch (req) { 179df8bae1dSRodney W. Grimes 180df8bae1dSRodney W. Grimes case RTM_ADD: 181df8bae1dSRodney W. Grimes /* 182df8bae1dSRodney W. Grimes * XXX: If this is a manually added route to interface 183df8bae1dSRodney W. Grimes * such as older version of routed or gated might provide, 184df8bae1dSRodney W. Grimes * restore cloning bit. 185df8bae1dSRodney W. Grimes */ 186df8bae1dSRodney W. Grimes if ((rt->rt_flags & RTF_HOST) == 0 && 187df8bae1dSRodney W. Grimes SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 188df8bae1dSRodney W. Grimes rt->rt_flags |= RTF_CLONING; 189df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_CLONING) { 190df8bae1dSRodney W. Grimes /* 191df8bae1dSRodney W. Grimes * Case 1: This route should come from a route to iface. 192df8bae1dSRodney W. Grimes */ 193df8bae1dSRodney W. Grimes rt_setgate(rt, rt_key(rt), 194df8bae1dSRodney W. Grimes (struct sockaddr *)&null_sdl); 195df8bae1dSRodney W. Grimes gate = rt->rt_gateway; 196df8bae1dSRodney W. Grimes SDL(gate)->sdl_type = rt->rt_ifp->if_type; 197df8bae1dSRodney W. Grimes SDL(gate)->sdl_index = rt->rt_ifp->if_index; 198227ee8a1SPoul-Henning Kamp rt->rt_expire = time_second; 199df8bae1dSRodney W. Grimes break; 200df8bae1dSRodney W. Grimes } 201df8bae1dSRodney W. Grimes /* Announce a new entry if requested. */ 202df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_ANNOUNCE) 203322dcb8dSMax Khon arprequest(rt->rt_ifp, 204ed7509acSJulian Elischer &SIN(rt_key(rt))->sin_addr, 205ed7509acSJulian Elischer &SIN(rt_key(rt))->sin_addr, 206df8bae1dSRodney W. Grimes (u_char *)LLADDR(SDL(gate))); 207df8bae1dSRodney W. Grimes /*FALLTHROUGH*/ 208df8bae1dSRodney W. Grimes case RTM_RESOLVE: 209df8bae1dSRodney W. Grimes if (gate->sa_family != AF_LINK || 210df8bae1dSRodney W. Grimes gate->sa_len < sizeof(null_sdl)) { 21138aa9fc3SDavid Greenman log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n"); 212df8bae1dSRodney W. Grimes break; 213df8bae1dSRodney W. Grimes } 214df8bae1dSRodney W. Grimes SDL(gate)->sdl_type = rt->rt_ifp->if_type; 215df8bae1dSRodney W. Grimes SDL(gate)->sdl_index = rt->rt_ifp->if_index; 216df8bae1dSRodney W. Grimes if (la != 0) 217df8bae1dSRodney W. Grimes break; /* This happens on a route change */ 218df8bae1dSRodney W. Grimes /* 219df8bae1dSRodney W. Grimes * Case 2: This route may come from cloning, or a manual route 220df8bae1dSRodney W. Grimes * add with a LL address. 221df8bae1dSRodney W. Grimes */ 222df8bae1dSRodney W. Grimes R_Malloc(la, struct llinfo_arp *, sizeof(*la)); 223df8bae1dSRodney W. Grimes rt->rt_llinfo = (caddr_t)la; 224df8bae1dSRodney W. Grimes if (la == 0) { 225df8bae1dSRodney W. Grimes log(LOG_DEBUG, "arp_rtrequest: malloc failed\n"); 226df8bae1dSRodney W. Grimes break; 227df8bae1dSRodney W. Grimes } 228df8bae1dSRodney W. Grimes arp_inuse++, arp_allocated++; 229df8bae1dSRodney W. Grimes Bzero(la, sizeof(*la)); 230df8bae1dSRodney W. Grimes la->la_rt = rt; 231df8bae1dSRodney W. Grimes rt->rt_flags |= RTF_LLINFO; 23293f79889SJeffrey Hsu RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]); 23366800f57SGarrett Wollman LIST_INSERT_HEAD(&llinfo_arp, la, la_le); 234cbb0b46aSGarrett Wollman 2351d5e9e22SEivind Eklund #ifdef INET 236cbb0b46aSGarrett Wollman /* 237cbb0b46aSGarrett Wollman * This keeps the multicast addresses from showing up 238cbb0b46aSGarrett Wollman * in `arp -a' listings as unresolved. It's not actually 239cbb0b46aSGarrett Wollman * functional. Then the same for broadcast. 240cbb0b46aSGarrett Wollman */ 241c448c89cSJonathan Lemon if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) && 242c448c89cSJonathan Lemon rt->rt_ifp->if_type != IFT_ARCNET) { 243cbb0b46aSGarrett Wollman ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr, 244cbb0b46aSGarrett Wollman LLADDR(SDL(gate))); 245cbb0b46aSGarrett Wollman SDL(gate)->sdl_alen = 6; 24651a109a1SPeter Wemm rt->rt_expire = 0; 247cbb0b46aSGarrett Wollman } 248cbb0b46aSGarrett Wollman if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) { 249322dcb8dSMax Khon memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr, 250322dcb8dSMax Khon rt->rt_ifp->if_addrlen); 251322dcb8dSMax Khon SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen; 25251a109a1SPeter Wemm rt->rt_expire = 0; 253cbb0b46aSGarrett Wollman } 2541d5e9e22SEivind Eklund #endif 255cbb0b46aSGarrett Wollman 256df8bae1dSRodney W. Grimes if (SIN(rt_key(rt))->sin_addr.s_addr == 257df8bae1dSRodney W. Grimes (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) { 258df8bae1dSRodney W. Grimes /* 259df8bae1dSRodney W. Grimes * This test used to be 260df8bae1dSRodney W. Grimes * if (loif.if_flags & IFF_UP) 261df8bae1dSRodney W. Grimes * It allowed local traffic to be forced 262df8bae1dSRodney W. Grimes * through the hardware by configuring the loopback down. 263df8bae1dSRodney W. Grimes * However, it causes problems during network configuration 264df8bae1dSRodney W. Grimes * for boards that can't receive packets they send. 265df8bae1dSRodney W. Grimes * It is now necessary to clear "useloopback" and remove 266df8bae1dSRodney W. Grimes * the route to force traffic out to the hardware. 267df8bae1dSRodney W. Grimes */ 268df8bae1dSRodney W. Grimes rt->rt_expire = 0; 269322dcb8dSMax Khon Bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)), 270322dcb8dSMax Khon SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen); 271df8bae1dSRodney W. Grimes if (useloopback) 272f5fea3ddSPaul Traina rt->rt_ifp = loif; 273df8bae1dSRodney W. Grimes 274df8bae1dSRodney W. Grimes } 275df8bae1dSRodney W. Grimes break; 276df8bae1dSRodney W. Grimes 277df8bae1dSRodney W. Grimes case RTM_DELETE: 278df8bae1dSRodney W. Grimes if (la == 0) 279df8bae1dSRodney W. Grimes break; 280df8bae1dSRodney W. Grimes arp_inuse--; 28193f79889SJeffrey Hsu RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]); 28266800f57SGarrett Wollman LIST_REMOVE(la, la_le); 283df8bae1dSRodney W. Grimes rt->rt_llinfo = 0; 284df8bae1dSRodney W. Grimes rt->rt_flags &= ~RTF_LLINFO; 285df8bae1dSRodney W. Grimes if (la->la_hold) 286df8bae1dSRodney W. Grimes m_freem(la->la_hold); 287df8bae1dSRodney W. Grimes Free((caddr_t)la); 288df8bae1dSRodney W. Grimes } 289df8bae1dSRodney W. Grimes } 290df8bae1dSRodney W. Grimes 291df8bae1dSRodney W. Grimes /* 292df8bae1dSRodney W. Grimes * Broadcast an ARP request. Caller specifies: 293df8bae1dSRodney W. Grimes * - arp header source ip address 294df8bae1dSRodney W. Grimes * - arp header target ip address 295df8bae1dSRodney W. Grimes * - arp header source ethernet address 296df8bae1dSRodney W. Grimes */ 297df8bae1dSRodney W. Grimes static void 298322dcb8dSMax Khon arprequest(ifp, sip, tip, enaddr) 299322dcb8dSMax Khon register struct ifnet *ifp; 300ed7509acSJulian Elischer register struct in_addr *sip, *tip; 301df8bae1dSRodney W. Grimes register u_char *enaddr; 302df8bae1dSRodney W. Grimes { 303df8bae1dSRodney W. Grimes register struct mbuf *m; 304df8bae1dSRodney W. Grimes register struct ether_header *eh; 305322dcb8dSMax Khon register struct arc_header *arh; 306322dcb8dSMax Khon register struct arphdr *ah; 307df8bae1dSRodney W. Grimes struct sockaddr sa; 308b149dd6cSLarry Lile static u_char llcx[] = { 0x82, 0x40, LLC_SNAP_LSAP, LLC_SNAP_LSAP, 309b149dd6cSLarry Lile LLC_UI, 0x00, 0x00, 0x00, 0x08, 0x06 }; 310322dcb8dSMax Khon u_short ar_hrd; 311df8bae1dSRodney W. Grimes 312a163d034SWarner Losh if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 313df8bae1dSRodney W. Grimes return; 314fda82fc2SJulian Elischer m->m_pkthdr.rcvif = (struct ifnet *)0; 31519527d3eSRobert Watson #ifdef MAC 31619527d3eSRobert Watson mac_create_mbuf_linklayer(ifp, m); 31719527d3eSRobert Watson #endif 318322dcb8dSMax Khon switch (ifp->if_type) { 319322dcb8dSMax Khon case IFT_ARCNET: 320322dcb8dSMax Khon ar_hrd = htons(ARPHRD_ARCNET); 321322dcb8dSMax Khon 322322dcb8dSMax Khon m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 323322dcb8dSMax Khon m->m_pkthdr.len = m->m_len; 324322dcb8dSMax Khon MH_ALIGN(m, m->m_len); 325322dcb8dSMax Khon 326322dcb8dSMax Khon arh = (struct arc_header *)sa.sa_data; 327322dcb8dSMax Khon arh->arc_dhost = *ifp->if_broadcastaddr; 328322dcb8dSMax Khon arh->arc_type = ARCTYPE_ARP; 329322dcb8dSMax Khon 330322dcb8dSMax Khon ah = mtod(m, struct arphdr *); 331322dcb8dSMax Khon break; 332322dcb8dSMax Khon 333fda82fc2SJulian Elischer case IFT_ISO88025: 334322dcb8dSMax Khon ar_hrd = htons(ARPHRD_IEEE802); 335322dcb8dSMax Khon 336322dcb8dSMax Khon m->m_len = sizeof(llcx) + 337322dcb8dSMax Khon arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 338322dcb8dSMax Khon m->m_pkthdr.len = m->m_len; 339322dcb8dSMax Khon MH_ALIGN(m, m->m_len); 340322dcb8dSMax Khon 341b149dd6cSLarry Lile (void)memcpy(mtod(m, caddr_t), llcx, sizeof(llcx)); 342322dcb8dSMax Khon (void)memcpy(sa.sa_data, ifp->if_broadcastaddr, 6); 343fda82fc2SJulian Elischer (void)memcpy(sa.sa_data + 6, enaddr, 6); 344b149dd6cSLarry Lile sa.sa_data[6] |= TR_RII; 345b149dd6cSLarry Lile sa.sa_data[12] = TR_AC; 346b149dd6cSLarry Lile sa.sa_data[13] = TR_LLC_FRAME; 347322dcb8dSMax Khon 348322dcb8dSMax Khon ah = (struct arphdr *)(mtod(m, char *) + sizeof(llcx)); 349fda82fc2SJulian Elischer break; 350f9083fdbSLarry Lile case IFT_FDDI: 351f9083fdbSLarry Lile case IFT_ETHER: 352f9083fdbSLarry Lile /* 353f9083fdbSLarry Lile * This may not be correct for types not explicitly 354f9083fdbSLarry Lile * listed, but this is our best guess 355f9083fdbSLarry Lile */ 356fda82fc2SJulian Elischer default: 357322dcb8dSMax Khon ar_hrd = htons(ARPHRD_ETHER); 358322dcb8dSMax Khon 359322dcb8dSMax Khon m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 360322dcb8dSMax Khon m->m_pkthdr.len = m->m_len; 361322dcb8dSMax Khon MH_ALIGN(m, m->m_len); 362322dcb8dSMax Khon 363f9083fdbSLarry Lile eh = (struct ether_header *)sa.sa_data; 364f9083fdbSLarry Lile /* if_output will not swap */ 365f9083fdbSLarry Lile eh->ether_type = htons(ETHERTYPE_ARP); 366322dcb8dSMax Khon (void)memcpy(eh->ether_dhost, ifp->if_broadcastaddr, 367f9083fdbSLarry Lile sizeof(eh->ether_dhost)); 368322dcb8dSMax Khon 369322dcb8dSMax Khon ah = mtod(m, struct arphdr *); 370f9083fdbSLarry Lile break; 371fda82fc2SJulian Elischer } 372322dcb8dSMax Khon 373322dcb8dSMax Khon ah->ar_hrd = ar_hrd; 374322dcb8dSMax Khon ah->ar_pro = htons(ETHERTYPE_IP); 375322dcb8dSMax Khon ah->ar_hln = ifp->if_addrlen; /* hardware address length */ 376322dcb8dSMax Khon ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ 377322dcb8dSMax Khon ah->ar_op = htons(ARPOP_REQUEST); 378322dcb8dSMax Khon (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 379a9a7a912SThomas Moestl memset(ar_tha(ah), 0, ah->ar_hln); 380322dcb8dSMax Khon (void)memcpy(ar_spa(ah), sip, ah->ar_pln); 381322dcb8dSMax Khon (void)memcpy(ar_tpa(ah), tip, ah->ar_pln); 382322dcb8dSMax Khon 383df8bae1dSRodney W. Grimes sa.sa_family = AF_UNSPEC; 384df8bae1dSRodney W. Grimes sa.sa_len = sizeof(sa); 385322dcb8dSMax Khon (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 386df8bae1dSRodney W. Grimes } 387df8bae1dSRodney W. Grimes 388df8bae1dSRodney W. Grimes /* 389df8bae1dSRodney W. Grimes * Resolve an IP address into an ethernet address. If success, 390df8bae1dSRodney W. Grimes * desten is filled in. If there is no entry in arptab, 391df8bae1dSRodney W. Grimes * set one up and broadcast a request for the IP address. 392df8bae1dSRodney W. Grimes * Hold onto this mbuf and resend it once the address 393df8bae1dSRodney W. Grimes * is finally resolved. A return value of 1 indicates 394df8bae1dSRodney W. Grimes * that desten has been filled in and the packet should be sent 395df8bae1dSRodney W. Grimes * normally; a 0 return indicates that the packet has been 396df8bae1dSRodney W. Grimes * taken over here, either now or for later transmission. 397df8bae1dSRodney W. Grimes */ 398df8bae1dSRodney W. Grimes int 399322dcb8dSMax Khon arpresolve(ifp, rt, m, dst, desten, rt0) 400322dcb8dSMax Khon register struct ifnet *ifp; 401df8bae1dSRodney W. Grimes register struct rtentry *rt; 402df8bae1dSRodney W. Grimes struct mbuf *m; 403df8bae1dSRodney W. Grimes register struct sockaddr *dst; 404df8bae1dSRodney W. Grimes register u_char *desten; 4055df72964SGarrett Wollman struct rtentry *rt0; 406df8bae1dSRodney W. Grimes { 40708aadfbbSJonathan Lemon struct llinfo_arp *la = 0; 408df8bae1dSRodney W. Grimes struct sockaddr_dl *sdl; 409df8bae1dSRodney W. Grimes 410df8bae1dSRodney W. Grimes if (m->m_flags & M_BCAST) { /* broadcast */ 411322dcb8dSMax Khon (void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen); 412df8bae1dSRodney W. Grimes return (1); 413df8bae1dSRodney W. Grimes } 414322dcb8dSMax Khon if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */ 415df8bae1dSRodney W. Grimes ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 416df8bae1dSRodney W. Grimes return(1); 417df8bae1dSRodney W. Grimes } 418df8bae1dSRodney W. Grimes if (rt) 419df8bae1dSRodney W. Grimes la = (struct llinfo_arp *)rt->rt_llinfo; 420e7bc5f27SBill Fenner if (la == 0) { 421623ae52eSPoul-Henning Kamp la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0); 422623ae52eSPoul-Henning Kamp if (la) 423df8bae1dSRodney W. Grimes rt = la->la_rt; 424df8bae1dSRodney W. Grimes } 425df8bae1dSRodney W. Grimes if (la == 0 || rt == 0) { 426245086a0SPoul-Henning Kamp log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n", 427245086a0SPoul-Henning Kamp inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "", 428245086a0SPoul-Henning Kamp rt ? "rt" : ""); 429df8bae1dSRodney W. Grimes m_freem(m); 430df8bae1dSRodney W. Grimes return (0); 431df8bae1dSRodney W. Grimes } 432df8bae1dSRodney W. Grimes sdl = SDL(rt->rt_gateway); 433df8bae1dSRodney W. Grimes /* 434df8bae1dSRodney W. Grimes * Check the address family and length is valid, the address 435df8bae1dSRodney W. Grimes * is resolved; otherwise, try to resolve. 436df8bae1dSRodney W. Grimes */ 437227ee8a1SPoul-Henning Kamp if ((rt->rt_expire == 0 || rt->rt_expire > time_second) && 438df8bae1dSRodney W. Grimes sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { 439f0f3379eSOrion Hodson /* 440f0f3379eSOrion Hodson * If entry has an expiry time and it is approaching, 441f0f3379eSOrion Hodson * see if we need to send an ARP request within this 442f0f3379eSOrion Hodson * arpt_down interval. 443f0f3379eSOrion Hodson */ 444f0f3379eSOrion Hodson if ((rt->rt_expire != 0) && 445022695f8SOrion Hodson (time_second + la->la_preempt > rt->rt_expire)) { 446f0f3379eSOrion Hodson arprequest(ifp, 447f0f3379eSOrion Hodson &SIN(rt->rt_ifa->ifa_addr)->sin_addr, 448f0f3379eSOrion Hodson &SIN(dst)->sin_addr, 449f0f3379eSOrion Hodson IF_LLADDR(ifp)); 450022695f8SOrion Hodson la->la_preempt--; 451f0f3379eSOrion Hodson } 452f0f3379eSOrion Hodson 4530453d3cbSBruce Evans bcopy(LLADDR(sdl), desten, sdl->sdl_alen); 454df8bae1dSRodney W. Grimes return 1; 455df8bae1dSRodney W. Grimes } 456df8bae1dSRodney W. Grimes /* 457deb62e28SRuslan Ermilov * If ARP is disabled or static on this interface, stop. 45808aadfbbSJonathan Lemon * XXX 45908aadfbbSJonathan Lemon * Probably should not allocate empty llinfo struct if we are 46008aadfbbSJonathan Lemon * not going to be sending out an arp request. 46108aadfbbSJonathan Lemon */ 462deb62e28SRuslan Ermilov if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) { 46347891de1SRuslan Ermilov m_freem(m); 46408aadfbbSJonathan Lemon return (0); 46547891de1SRuslan Ermilov } 46608aadfbbSJonathan Lemon /* 467df8bae1dSRodney W. Grimes * There is an arptab entry, but no ethernet address 468df8bae1dSRodney W. Grimes * response yet. Replace the held mbuf with this 469df8bae1dSRodney W. Grimes * latest one. 470df8bae1dSRodney W. Grimes */ 471df8bae1dSRodney W. Grimes if (la->la_hold) 472df8bae1dSRodney W. Grimes m_freem(la->la_hold); 473df8bae1dSRodney W. Grimes la->la_hold = m; 474df8bae1dSRodney W. Grimes if (rt->rt_expire) { 475df8bae1dSRodney W. Grimes rt->rt_flags &= ~RTF_REJECT; 476227ee8a1SPoul-Henning Kamp if (la->la_asked == 0 || rt->rt_expire != time_second) { 477227ee8a1SPoul-Henning Kamp rt->rt_expire = time_second; 47873224fb0SOrion Hodson if (la->la_asked++ < arp_maxtries) { 479322dcb8dSMax Khon arprequest(ifp, 480ed7509acSJulian Elischer &SIN(rt->rt_ifa->ifa_addr)->sin_addr, 481322dcb8dSMax Khon &SIN(dst)->sin_addr, 482322dcb8dSMax Khon IF_LLADDR(ifp)); 48373224fb0SOrion Hodson } else { 484df8bae1dSRodney W. Grimes rt->rt_flags |= RTF_REJECT; 485df8bae1dSRodney W. Grimes rt->rt_expire += arpt_down; 486022695f8SOrion Hodson la->la_asked = 0; 487022695f8SOrion Hodson la->la_preempt = arp_maxtries; 488df8bae1dSRodney W. Grimes } 489df8bae1dSRodney W. Grimes 490df8bae1dSRodney W. Grimes } 491df8bae1dSRodney W. Grimes } 492df8bae1dSRodney W. Grimes return (0); 493df8bae1dSRodney W. Grimes } 494df8bae1dSRodney W. Grimes 495df8bae1dSRodney W. Grimes /* 496df8bae1dSRodney W. Grimes * Common length and type checks are done here, 497df8bae1dSRodney W. Grimes * then the protocol-specific routine is called. 498df8bae1dSRodney W. Grimes */ 499885f1aa4SPoul-Henning Kamp static void 5001cafed39SJonathan Lemon arpintr(struct mbuf *m) 501df8bae1dSRodney W. Grimes { 5021cafed39SJonathan Lemon struct arphdr *ar; 503df8bae1dSRodney W. Grimes 504532cf61bSPeter Wemm if (!arpinit_done) { 505532cf61bSPeter Wemm arpinit_done = 1; 506532cf61bSPeter Wemm timeout(arptimer, (caddr_t)0, hz); 507532cf61bSPeter Wemm } 50876ec7b2fSRobert Watson if (m->m_len < sizeof(struct arphdr) && 50984365e2bSMatthew Dillon ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) { 510e44d6283SJoerg Wunsch log(LOG_ERR, "arp: runt packet -- m_pullup failed\n"); 5111cafed39SJonathan Lemon return; 51276ec7b2fSRobert Watson } 51376ec7b2fSRobert Watson ar = mtod(m, struct arphdr *); 51476ec7b2fSRobert Watson 5151cafed39SJonathan Lemon if (ntohs(ar->ar_hrd) != ARPHRD_ETHER && 5161cafed39SJonathan Lemon ntohs(ar->ar_hrd) != ARPHRD_IEEE802 && 5171cafed39SJonathan Lemon ntohs(ar->ar_hrd) != ARPHRD_ARCNET) { 5181cafed39SJonathan Lemon log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n", 51976ec7b2fSRobert Watson (unsigned char *)&ar->ar_hrd, ""); 52076ec7b2fSRobert Watson m_freem(m); 5211cafed39SJonathan Lemon return; 52276ec7b2fSRobert Watson } 52376ec7b2fSRobert Watson 524322dcb8dSMax Khon if (m->m_pkthdr.len < arphdr_len(ar) && 525322dcb8dSMax Khon (m = m_pullup(m, arphdr_len(ar))) == NULL) { 526f72d9d83SJoerg Wunsch log(LOG_ERR, "arp: runt packet\n"); 52776ec7b2fSRobert Watson m_freem(m); 5281cafed39SJonathan Lemon return; 52976ec7b2fSRobert Watson } 530df8bae1dSRodney W. Grimes 531df8bae1dSRodney W. Grimes switch (ntohs(ar->ar_pro)) { 5321d5e9e22SEivind Eklund #ifdef INET 533df8bae1dSRodney W. Grimes case ETHERTYPE_IP: 534df8bae1dSRodney W. Grimes in_arpinput(m); 5351cafed39SJonathan Lemon return; 5361d5e9e22SEivind Eklund #endif 537df8bae1dSRodney W. Grimes } 538df8bae1dSRodney W. Grimes m_freem(m); 539df8bae1dSRodney W. Grimes } 540df8bae1dSRodney W. Grimes 5411d5e9e22SEivind Eklund #ifdef INET 542df8bae1dSRodney W. Grimes /* 543df8bae1dSRodney W. Grimes * ARP for Internet protocols on 10 Mb/s Ethernet. 544df8bae1dSRodney W. Grimes * Algorithm is that given in RFC 826. 545df8bae1dSRodney W. Grimes * In addition, a sanity check is performed on the sender 546df8bae1dSRodney W. Grimes * protocol address, to catch impersonators. 547df8bae1dSRodney W. Grimes * We no longer handle negotiations for use of trailer protocol: 548df8bae1dSRodney W. Grimes * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 549df8bae1dSRodney W. Grimes * along with IP replies if we wanted trailers sent to us, 550df8bae1dSRodney W. Grimes * and also sent them in response to IP replies. 551df8bae1dSRodney W. Grimes * This allowed either end to announce the desire to receive 552df8bae1dSRodney W. Grimes * trailer packets. 553df8bae1dSRodney W. Grimes * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 554df8bae1dSRodney W. Grimes * but formerly didn't normally send requests. 555df8bae1dSRodney W. Grimes */ 5563269187dSAlfred Perlstein static int log_arp_wrong_iface = 1; 557e3d123d6SAlfred Perlstein static int log_arp_movements = 1; 5583269187dSAlfred Perlstein 5593269187dSAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW, 5603269187dSAlfred Perlstein &log_arp_wrong_iface, 0, 5613269187dSAlfred Perlstein "log arp packets arriving on the wrong interface"); 562e3d123d6SAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW, 563e3d123d6SAlfred Perlstein &log_arp_movements, 0, 56475ce3221SAlfred Perlstein "log arp replies from MACs different than the one in the cache"); 565e3d123d6SAlfred Perlstein 5663269187dSAlfred Perlstein 567df8bae1dSRodney W. Grimes static void 568df8bae1dSRodney W. Grimes in_arpinput(m) 569df8bae1dSRodney W. Grimes struct mbuf *m; 570df8bae1dSRodney W. Grimes { 571322dcb8dSMax Khon register struct arphdr *ah; 572322dcb8dSMax Khon register struct ifnet *ifp = m->m_pkthdr.rcvif; 573df8bae1dSRodney W. Grimes struct ether_header *eh; 574322dcb8dSMax Khon struct arc_header *arh; 575fda82fc2SJulian Elischer struct iso88025_header *th = (struct iso88025_header *)0; 57642fdfc12SKelly Yancey struct iso88025_sockaddr_dl_data *trld; 577df8bae1dSRodney W. Grimes register struct llinfo_arp *la = 0; 578df8bae1dSRodney W. Grimes register struct rtentry *rt; 579ca925d9cSJonathan Lemon struct ifaddr *ifa; 580ca925d9cSJonathan Lemon struct in_ifaddr *ia; 581df8bae1dSRodney W. Grimes struct sockaddr_dl *sdl; 582df8bae1dSRodney W. Grimes struct sockaddr sa; 583df8bae1dSRodney W. Grimes struct in_addr isaddr, itaddr, myaddr; 584b149dd6cSLarry Lile int op, rif_len; 585322dcb8dSMax Khon int req_len; 586df8bae1dSRodney W. Grimes 587322dcb8dSMax Khon req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 588322dcb8dSMax Khon if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) { 5894cbc8ad1SYaroslav Tykhiy log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n"); 5904cbc8ad1SYaroslav Tykhiy return; 5914cbc8ad1SYaroslav Tykhiy } 5924cbc8ad1SYaroslav Tykhiy 593322dcb8dSMax Khon ah = mtod(m, struct arphdr *); 594322dcb8dSMax Khon op = ntohs(ah->ar_op); 595322dcb8dSMax Khon (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr)); 596322dcb8dSMax Khon (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr)); 59741d2ba5eSJulian Elischer #ifdef BRIDGE 59841d2ba5eSJulian Elischer #define BRIDGE_TEST (do_bridge) 599b715f178SLuigi Rizzo #else 60041d2ba5eSJulian Elischer #define BRIDGE_TEST (0) /* cc will optimise the test away */ 601b715f178SLuigi Rizzo #endif 602ca925d9cSJonathan Lemon /* 603ca925d9cSJonathan Lemon * For a bridge, we want to check the address irrespective 604ca925d9cSJonathan Lemon * of the receive interface. (This will change slightly 605ca925d9cSJonathan Lemon * when we have clusters of interfaces). 606ca925d9cSJonathan Lemon */ 607ca925d9cSJonathan Lemon LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) 608322dcb8dSMax Khon if ((BRIDGE_TEST || (ia->ia_ifp == ifp)) && 609ca925d9cSJonathan Lemon itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) 610ca925d9cSJonathan Lemon goto match; 611ca925d9cSJonathan Lemon LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash) 612322dcb8dSMax Khon if ((BRIDGE_TEST || (ia->ia_ifp == ifp)) && 613ca925d9cSJonathan Lemon isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) 614ca925d9cSJonathan Lemon goto match; 615ca925d9cSJonathan Lemon /* 616d8b84d9eSJonathan Lemon * No match, use the first inet address on the receive interface 617ca925d9cSJonathan Lemon * as a dummy address for the rest of the function. 618ca925d9cSJonathan Lemon */ 619d8b84d9eSJonathan Lemon TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 620ec691a10SJonathan Lemon if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) { 621ec691a10SJonathan Lemon ia = ifatoia(ifa); 622ec691a10SJonathan Lemon goto match; 623ec691a10SJonathan Lemon } 624ec691a10SJonathan Lemon /* 625ec691a10SJonathan Lemon * If bridging, fall back to using any inet address. 626ec691a10SJonathan Lemon */ 627ec691a10SJonathan Lemon if (!BRIDGE_TEST || 628ec691a10SJonathan Lemon (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL) { 629885f1aa4SPoul-Henning Kamp m_freem(m); 630885f1aa4SPoul-Henning Kamp return; 631885f1aa4SPoul-Henning Kamp } 632ca925d9cSJonathan Lemon match: 633ca925d9cSJonathan Lemon myaddr = ia->ia_addr.sin_addr; 634322dcb8dSMax Khon if (!bcmp(ar_sha(ah), IF_LLADDR(ifp), ifp->if_addrlen)) { 635885f1aa4SPoul-Henning Kamp m_freem(m); /* it's from me, ignore it. */ 636885f1aa4SPoul-Henning Kamp return; 637885f1aa4SPoul-Henning Kamp } 638322dcb8dSMax Khon if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) { 639df8bae1dSRodney W. Grimes log(LOG_ERR, 640322dcb8dSMax Khon "arp: link address is broadcast for IP address %s!\n", 641ef0cdf33SGarrett Wollman inet_ntoa(isaddr)); 642885f1aa4SPoul-Henning Kamp m_freem(m); 643885f1aa4SPoul-Henning Kamp return; 644df8bae1dSRodney W. Grimes } 645df8bae1dSRodney W. Grimes if (isaddr.s_addr == myaddr.s_addr) { 646df8bae1dSRodney W. Grimes log(LOG_ERR, 647322dcb8dSMax Khon "arp: %*D is using my IP address %s!\n", 648322dcb8dSMax Khon ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 649322dcb8dSMax Khon inet_ntoa(isaddr)); 650df8bae1dSRodney W. Grimes itaddr = myaddr; 651df8bae1dSRodney W. Grimes goto reply; 652df8bae1dSRodney W. Grimes } 653deb62e28SRuslan Ermilov if (ifp->if_flags & IFF_STATICARP) 654deb62e28SRuslan Ermilov goto reply; 655df8bae1dSRodney W. Grimes la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0); 656df8bae1dSRodney W. Grimes if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) { 6577e1cd0d2SLuigi Rizzo /* the following is not an error when doing bridging */ 658322dcb8dSMax Khon if (!BRIDGE_TEST && rt->rt_ifp != ifp) { 6593269187dSAlfred Perlstein if (log_arp_wrong_iface) 660322dcb8dSMax Khon log(LOG_ERR, "arp: %s is on %s%d but got reply from %*D on %s%d\n", 661dd9b6ddeSBill Fenner inet_ntoa(isaddr), 662dd9b6ddeSBill Fenner rt->rt_ifp->if_name, rt->rt_ifp->if_unit, 663322dcb8dSMax Khon ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 664322dcb8dSMax Khon ifp->if_name, ifp->if_unit); 665dd9b6ddeSBill Fenner goto reply; 666dd9b6ddeSBill Fenner } 667df8bae1dSRodney W. Grimes if (sdl->sdl_alen && 668322dcb8dSMax Khon bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) { 669e3d123d6SAlfred Perlstein if (rt->rt_expire) { 670e3d123d6SAlfred Perlstein if (log_arp_movements) 671322dcb8dSMax Khon log(LOG_INFO, "arp: %s moved from %*D to %*D on %s%d\n", 672322dcb8dSMax Khon inet_ntoa(isaddr), 673322dcb8dSMax Khon ifp->if_addrlen, (u_char *)LLADDR(sdl), ":", 674322dcb8dSMax Khon ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 675322dcb8dSMax Khon ifp->if_name, ifp->if_unit); 676e3d123d6SAlfred Perlstein } else { 677dd9b6ddeSBill Fenner log(LOG_ERR, 678322dcb8dSMax Khon "arp: %*D attempts to modify permanent entry for %s on %s%d\n", 679322dcb8dSMax Khon ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 680322dcb8dSMax Khon inet_ntoa(isaddr), ifp->if_name, ifp->if_unit); 681dd9b6ddeSBill Fenner goto reply; 682dd9b6ddeSBill Fenner } 683dfd5dee1SPeter Wemm } 684322dcb8dSMax Khon /* 685322dcb8dSMax Khon * sanity check for the address length. 686322dcb8dSMax Khon * XXX this does not work for protocols with variable address 687322dcb8dSMax Khon * length. -is 688322dcb8dSMax Khon */ 689322dcb8dSMax Khon if (sdl->sdl_alen && 690322dcb8dSMax Khon sdl->sdl_alen != ah->ar_hln) { 691322dcb8dSMax Khon log(LOG_WARNING, 692322dcb8dSMax Khon "arp from %*D: new addr len %d, was %d", 693322dcb8dSMax Khon ifp->if_addrlen, (u_char *) ar_sha(ah), ":", 694322dcb8dSMax Khon ah->ar_hln, sdl->sdl_alen); 695322dcb8dSMax Khon } 696322dcb8dSMax Khon if (ifp->if_addrlen != ah->ar_hln) { 697322dcb8dSMax Khon log(LOG_WARNING, 698322dcb8dSMax Khon "arp from %*D: addr len: new %d, i/f %d (ignored)", 699322dcb8dSMax Khon ifp->if_addrlen, (u_char *) ar_sha(ah), ":", 700322dcb8dSMax Khon ah->ar_hln, ifp->if_addrlen); 701322dcb8dSMax Khon goto reply; 702322dcb8dSMax Khon } 703322dcb8dSMax Khon (void)memcpy(LLADDR(sdl), ar_sha(ah), 704322dcb8dSMax Khon sdl->sdl_alen = ah->ar_hln); 705243c4c6dSEivind Eklund /* 706243c4c6dSEivind Eklund * If we receive an arp from a token-ring station over 707243c4c6dSEivind Eklund * a token-ring nic then try to save the source 708243c4c6dSEivind Eklund * routing info. 709243c4c6dSEivind Eklund */ 710322dcb8dSMax Khon if (ifp->if_type == IFT_ISO88025) { 711fda82fc2SJulian Elischer th = (struct iso88025_header *)m->m_pkthdr.header; 71242fdfc12SKelly Yancey trld = SDL_ISO88025(sdl); 713b149dd6cSLarry Lile rif_len = TR_RCF_RIFLEN(th->rcf); 714b149dd6cSLarry Lile if ((th->iso88025_shost[0] & TR_RII) && 715b149dd6cSLarry Lile (rif_len > 2)) { 71642fdfc12SKelly Yancey trld->trld_rcf = th->rcf; 71742fdfc12SKelly Yancey trld->trld_rcf ^= htons(TR_RCF_DIR); 71842fdfc12SKelly Yancey memcpy(trld->trld_route, th->rd, rif_len - 2); 71942fdfc12SKelly Yancey trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK); 720f9083fdbSLarry Lile /* 721f9083fdbSLarry Lile * Set up source routing information for 722f9083fdbSLarry Lile * reply packet (XXX) 723f9083fdbSLarry Lile */ 724b149dd6cSLarry Lile m->m_data -= rif_len; 725b149dd6cSLarry Lile m->m_len += rif_len; 726b149dd6cSLarry Lile m->m_pkthdr.len += rif_len; 727fda82fc2SJulian Elischer } else { 728b149dd6cSLarry Lile th->iso88025_shost[0] &= ~TR_RII; 729c3a2190cSKelly Yancey trld->trld_rcf = 0; 730fcf11853SLarry Lile } 731fda82fc2SJulian Elischer m->m_data -= 8; 732fda82fc2SJulian Elischer m->m_len += 8; 733fcf11853SLarry Lile m->m_pkthdr.len += 8; 73442fdfc12SKelly Yancey th->rcf = trld->trld_rcf; 735fda82fc2SJulian Elischer } 736df8bae1dSRodney W. Grimes if (rt->rt_expire) 737227ee8a1SPoul-Henning Kamp rt->rt_expire = time_second + arpt_keep; 738df8bae1dSRodney W. Grimes rt->rt_flags &= ~RTF_REJECT; 739022695f8SOrion Hodson la->la_asked = 0; 740022695f8SOrion Hodson la->la_preempt = arp_maxtries; 741df8bae1dSRodney W. Grimes if (la->la_hold) { 742322dcb8dSMax Khon (*ifp->if_output)(ifp, la->la_hold, 743df8bae1dSRodney W. Grimes rt_key(rt), rt); 744df8bae1dSRodney W. Grimes la->la_hold = 0; 745df8bae1dSRodney W. Grimes } 746df8bae1dSRodney W. Grimes } 747df8bae1dSRodney W. Grimes reply: 748df8bae1dSRodney W. Grimes if (op != ARPOP_REQUEST) { 749df8bae1dSRodney W. Grimes m_freem(m); 750df8bae1dSRodney W. Grimes return; 751df8bae1dSRodney W. Grimes } 752df8bae1dSRodney W. Grimes if (itaddr.s_addr == myaddr.s_addr) { 753df8bae1dSRodney W. Grimes /* I am the target */ 754322dcb8dSMax Khon (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 755322dcb8dSMax Khon (void)memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln); 756df8bae1dSRodney W. Grimes } else { 757df8bae1dSRodney W. Grimes la = arplookup(itaddr.s_addr, 0, SIN_PROXY); 75828e82295SGarrett Wollman if (la == NULL) { 75928e82295SGarrett Wollman struct sockaddr_in sin; 76028e82295SGarrett Wollman 761885f1aa4SPoul-Henning Kamp if (!arp_proxyall) { 762885f1aa4SPoul-Henning Kamp m_freem(m); 763885f1aa4SPoul-Henning Kamp return; 764885f1aa4SPoul-Henning Kamp } 76528e82295SGarrett Wollman 76628e82295SGarrett Wollman bzero(&sin, sizeof sin); 76728e82295SGarrett Wollman sin.sin_family = AF_INET; 76828e82295SGarrett Wollman sin.sin_len = sizeof sin; 76928e82295SGarrett Wollman sin.sin_addr = itaddr; 77028e82295SGarrett Wollman 77131246bc2SGarrett Wollman rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 772885f1aa4SPoul-Henning Kamp if (!rt) { 773885f1aa4SPoul-Henning Kamp m_freem(m); 774885f1aa4SPoul-Henning Kamp return; 775885f1aa4SPoul-Henning Kamp } 77628e82295SGarrett Wollman /* 77728e82295SGarrett Wollman * Don't send proxies for nodes on the same interface 77828e82295SGarrett Wollman * as this one came out of, or we'll get into a fight 77928e82295SGarrett Wollman * over who claims what Ether address. 78028e82295SGarrett Wollman */ 781322dcb8dSMax Khon if (rt->rt_ifp == ifp) { 78228e82295SGarrett Wollman rtfree(rt); 783885f1aa4SPoul-Henning Kamp m_freem(m); 784885f1aa4SPoul-Henning Kamp return; 78528e82295SGarrett Wollman } 786322dcb8dSMax Khon (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 787322dcb8dSMax Khon (void)memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln); 78828e82295SGarrett Wollman rtfree(rt); 789cc728227SDavid Malone 790cc728227SDavid Malone /* 791cc728227SDavid Malone * Also check that the node which sent the ARP packet 792cc728227SDavid Malone * is on the the interface we expect it to be on. This 793cc728227SDavid Malone * avoids ARP chaos if an interface is connected to the 794cc728227SDavid Malone * wrong network. 795cc728227SDavid Malone */ 796cc728227SDavid Malone sin.sin_addr = isaddr; 797cc728227SDavid Malone 798cc728227SDavid Malone rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 799cc728227SDavid Malone if (!rt) { 800cc728227SDavid Malone m_freem(m); 801cc728227SDavid Malone return; 802cc728227SDavid Malone } 803322dcb8dSMax Khon if (rt->rt_ifp != ifp) { 804cc728227SDavid Malone log(LOG_INFO, "arp_proxy: ignoring request" 805cc728227SDavid Malone " from %s via %s%d, expecting %s%d\n", 806322dcb8dSMax Khon inet_ntoa(isaddr), ifp->if_name, 807322dcb8dSMax Khon ifp->if_unit, rt->rt_ifp->if_name, 808cc728227SDavid Malone rt->rt_ifp->if_unit); 809cc728227SDavid Malone rtfree(rt); 810cc728227SDavid Malone m_freem(m); 811cc728227SDavid Malone return; 812cc728227SDavid Malone } 813cc728227SDavid Malone rtfree(rt); 814cc728227SDavid Malone 815ac234f93SGarrett Wollman #ifdef DEBUG_PROXY 816ac234f93SGarrett Wollman printf("arp: proxying for %s\n", 817ef0cdf33SGarrett Wollman inet_ntoa(itaddr)); 818ac234f93SGarrett Wollman #endif 81928e82295SGarrett Wollman } else { 820df8bae1dSRodney W. Grimes rt = la->la_rt; 821322dcb8dSMax Khon (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 822df8bae1dSRodney W. Grimes sdl = SDL(rt->rt_gateway); 823322dcb8dSMax Khon (void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln); 82428e82295SGarrett Wollman } 825df8bae1dSRodney W. Grimes } 826df8bae1dSRodney W. Grimes 827322dcb8dSMax Khon (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln); 828322dcb8dSMax Khon (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln); 829322dcb8dSMax Khon ah->ar_op = htons(ARPOP_REPLY); 830322dcb8dSMax Khon ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 831322dcb8dSMax Khon switch (ifp->if_type) { 832322dcb8dSMax Khon case IFT_ARCNET: 833322dcb8dSMax Khon arh = (struct arc_header *)sa.sa_data; 834322dcb8dSMax Khon arh->arc_dhost = *ar_tha(ah); 835322dcb8dSMax Khon arh->arc_type = ARCTYPE_ARP; 836322dcb8dSMax Khon break; 837322dcb8dSMax Khon 838243c4c6dSEivind Eklund case IFT_ISO88025: 839fda82fc2SJulian Elischer /* Re-arrange the source/dest address */ 840f9083fdbSLarry Lile memcpy(th->iso88025_dhost, th->iso88025_shost, 841f9083fdbSLarry Lile sizeof(th->iso88025_dhost)); 842322dcb8dSMax Khon memcpy(th->iso88025_shost, IF_LLADDR(ifp), 843f9083fdbSLarry Lile sizeof(th->iso88025_shost)); 844fda82fc2SJulian Elischer /* Set the source routing bit if neccesary */ 845b149dd6cSLarry Lile if (th->iso88025_dhost[0] & TR_RII) { 846b149dd6cSLarry Lile th->iso88025_dhost[0] &= ~TR_RII; 847b149dd6cSLarry Lile if (TR_RCF_RIFLEN(th->rcf) > 2) 848b149dd6cSLarry Lile th->iso88025_shost[0] |= TR_RII; 849fda82fc2SJulian Elischer } 850fda82fc2SJulian Elischer /* Copy the addresses, ac and fc into sa_data */ 851f9083fdbSLarry Lile memcpy(sa.sa_data, th->iso88025_dhost, 852f9083fdbSLarry Lile sizeof(th->iso88025_dhost) * 2); 853b149dd6cSLarry Lile sa.sa_data[(sizeof(th->iso88025_dhost) * 2)] = TR_AC; 854b149dd6cSLarry Lile sa.sa_data[(sizeof(th->iso88025_dhost) * 2) + 1] = TR_LLC_FRAME; 855fda82fc2SJulian Elischer break; 856243c4c6dSEivind Eklund case IFT_ETHER: 857f9083fdbSLarry Lile case IFT_FDDI: 858f9083fdbSLarry Lile /* 859f9083fdbSLarry Lile * May not be correct for types not explictly 860f9083fdbSLarry Lile * listed, but it is our best guess. 861f9083fdbSLarry Lile */ 862f9083fdbSLarry Lile default: 863df8bae1dSRodney W. Grimes eh = (struct ether_header *)sa.sa_data; 864322dcb8dSMax Khon (void)memcpy(eh->ether_dhost, ar_tha(ah), 865f9083fdbSLarry Lile sizeof(eh->ether_dhost)); 86634bed8b0SDavid Greenman eh->ether_type = htons(ETHERTYPE_ARP); 867fda82fc2SJulian Elischer break; 868fda82fc2SJulian Elischer } 869df8bae1dSRodney W. Grimes sa.sa_family = AF_UNSPEC; 870df8bae1dSRodney W. Grimes sa.sa_len = sizeof(sa); 871322dcb8dSMax Khon (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 872df8bae1dSRodney W. Grimes return; 873df8bae1dSRodney W. Grimes } 8741d5e9e22SEivind Eklund #endif 875df8bae1dSRodney W. Grimes 876df8bae1dSRodney W. Grimes /* 877df8bae1dSRodney W. Grimes * Free an arp entry. 878df8bae1dSRodney W. Grimes */ 879df8bae1dSRodney W. Grimes static void 880df8bae1dSRodney W. Grimes arptfree(la) 881df8bae1dSRodney W. Grimes register struct llinfo_arp *la; 882df8bae1dSRodney W. Grimes { 883df8bae1dSRodney W. Grimes register struct rtentry *rt = la->la_rt; 884df8bae1dSRodney W. Grimes register struct sockaddr_dl *sdl; 885df8bae1dSRodney W. Grimes if (rt == 0) 886df8bae1dSRodney W. Grimes panic("arptfree"); 887df8bae1dSRodney W. Grimes if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) && 888df8bae1dSRodney W. Grimes sdl->sdl_family == AF_LINK) { 889df8bae1dSRodney W. Grimes sdl->sdl_alen = 0; 89073224fb0SOrion Hodson la->la_preempt = la->la_asked = 0; 891df8bae1dSRodney W. Grimes rt->rt_flags &= ~RTF_REJECT; 892df8bae1dSRodney W. Grimes return; 893df8bae1dSRodney W. Grimes } 894df8bae1dSRodney W. Grimes rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt), 895df8bae1dSRodney W. Grimes 0, (struct rtentry **)0); 896df8bae1dSRodney W. Grimes } 897df8bae1dSRodney W. Grimes /* 898df8bae1dSRodney W. Grimes * Lookup or enter a new address in arptab. 899df8bae1dSRodney W. Grimes */ 900df8bae1dSRodney W. Grimes static struct llinfo_arp * 901df8bae1dSRodney W. Grimes arplookup(addr, create, proxy) 902df8bae1dSRodney W. Grimes u_long addr; 903df8bae1dSRodney W. Grimes int create, proxy; 904df8bae1dSRodney W. Grimes { 905df8bae1dSRodney W. Grimes register struct rtentry *rt; 906df8bae1dSRodney W. Grimes static struct sockaddr_inarp sin = {sizeof(sin), AF_INET }; 907ac234f93SGarrett Wollman const char *why = 0; 908df8bae1dSRodney W. Grimes 909df8bae1dSRodney W. Grimes sin.sin_addr.s_addr = addr; 910df8bae1dSRodney W. Grimes sin.sin_other = proxy ? SIN_PROXY : 0; 91131246bc2SGarrett Wollman rt = rtalloc1((struct sockaddr *)&sin, create, 0UL); 912df8bae1dSRodney W. Grimes if (rt == 0) 913df8bae1dSRodney W. Grimes return (0); 914df8bae1dSRodney W. Grimes rt->rt_refcnt--; 915ac234f93SGarrett Wollman 916ac234f93SGarrett Wollman if (rt->rt_flags & RTF_GATEWAY) 917ac234f93SGarrett Wollman why = "host is not on local network"; 918ac234f93SGarrett Wollman else if ((rt->rt_flags & RTF_LLINFO) == 0) 919ac234f93SGarrett Wollman why = "could not allocate llinfo"; 920ac234f93SGarrett Wollman else if (rt->rt_gateway->sa_family != AF_LINK) 921ac234f93SGarrett Wollman why = "gateway route is not ours"; 922ac234f93SGarrett Wollman 923fedf1d01SBruce M Simpson if (why) { 924fedf1d01SBruce M Simpson if (create) 925ac234f93SGarrett Wollman log(LOG_DEBUG, "arplookup %s failed: %s\n", 926ef0cdf33SGarrett Wollman inet_ntoa(sin.sin_addr), why); 927fedf1d01SBruce M Simpson 928fedf1d01SBruce M Simpson /* If there are no references to this route, purge it */ 92985cc1994SBruce M Simpson if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_WASCLONED)) { 930fedf1d01SBruce M Simpson rtrequest(RTM_DELETE, 931fedf1d01SBruce M Simpson (struct sockaddr *)rt_key(rt), 932fedf1d01SBruce M Simpson rt->rt_gateway, rt_mask(rt), 933fedf1d01SBruce M Simpson rt->rt_flags, 0); 934fedf1d01SBruce M Simpson } 935fedf1d01SBruce M Simpson return (0); 936df8bae1dSRodney W. Grimes } 937df8bae1dSRodney W. Grimes return ((struct llinfo_arp *)rt->rt_llinfo); 938df8bae1dSRodney W. Grimes } 939df8bae1dSRodney W. Grimes 940dd2e4102SGarrett Wollman void 941322dcb8dSMax Khon arp_ifinit(ifp, ifa) 942322dcb8dSMax Khon struct ifnet *ifp; 943dd2e4102SGarrett Wollman struct ifaddr *ifa; 944dd2e4102SGarrett Wollman { 945dd570d4dSTor Egge if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 946322dcb8dSMax Khon arprequest(ifp, &IA_SIN(ifa)->sin_addr, 947322dcb8dSMax Khon &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp)); 948dd2e4102SGarrett Wollman ifa->ifa_rtrequest = arp_rtrequest; 949dd2e4102SGarrett Wollman ifa->ifa_flags |= RTF_CLONING; 950dd2e4102SGarrett Wollman } 951df5e1987SJonathan Lemon 952df5e1987SJonathan Lemon static void 953df5e1987SJonathan Lemon arp_init(void) 954df5e1987SJonathan Lemon { 955df5e1987SJonathan Lemon 956df5e1987SJonathan Lemon arpintrq.ifq_maxlen = 50; 9576008862bSJohn Baldwin mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF); 958532cf61bSPeter Wemm LIST_INIT(&llinfo_arp); 9591cafed39SJonathan Lemon netisr_register(NETISR_ARP, arpintr, &arpintrq); 960df5e1987SJonathan Lemon } 961df5e1987SJonathan Lemon 962df5e1987SJonathan Lemon SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0); 963