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" 451d5e9e22SEivind Eklund 46df8bae1dSRodney W. Grimes #include <sys/param.h> 47df8bae1dSRodney W. Grimes #include <sys/kernel.h> 48ce02431fSDoug Rabson #include <sys/queue.h> 49885f1aa4SPoul-Henning Kamp #include <sys/sysctl.h> 50885f1aa4SPoul-Henning Kamp #include <sys/systm.h> 51885f1aa4SPoul-Henning Kamp #include <sys/mbuf.h> 52885f1aa4SPoul-Henning Kamp #include <sys/malloc.h> 534458ac71SBruce Evans #include <sys/socket.h> 54885f1aa4SPoul-Henning Kamp #include <sys/syslog.h> 55df8bae1dSRodney W. Grimes 56df8bae1dSRodney W. Grimes #include <net/if.h> 57df8bae1dSRodney W. Grimes #include <net/if_dl.h> 58722012ccSJulian Elischer #include <net/if_types.h> 59df8bae1dSRodney W. Grimes #include <net/route.h> 60748e0b0aSGarrett Wollman #include <net/netisr.h> 61b149dd6cSLarry Lile #include <net/if_llc.h> 62c8f8e9c1SJulian Elischer #ifdef BRIDGE 63c8f8e9c1SJulian Elischer #include <net/ethernet.h> 64c8f8e9c1SJulian Elischer #include <net/bridge.h> 65c8f8e9c1SJulian Elischer #endif 66df8bae1dSRodney W. Grimes 67df8bae1dSRodney W. Grimes #include <netinet/in.h> 68df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 69df8bae1dSRodney W. Grimes #include <netinet/if_ether.h> 70df8bae1dSRodney W. Grimes 71322dcb8dSMax Khon #include <net/if_arc.h> 72fda82fc2SJulian Elischer #include <net/iso88025.h> 73fda82fc2SJulian Elischer 74df8bae1dSRodney W. Grimes #define SIN(s) ((struct sockaddr_in *)s) 75df8bae1dSRodney W. Grimes #define SDL(s) ((struct sockaddr_dl *)s) 76df8bae1dSRodney W. Grimes 77ce02431fSDoug Rabson SYSCTL_DECL(_net_link_ether); 78602d513cSGarrett Wollman SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, ""); 79df8bae1dSRodney W. Grimes 80df8bae1dSRodney W. Grimes /* timer values */ 81602d513cSGarrett Wollman static int arpt_prune = (5*60*1); /* walk list every 5 minutes */ 82602d513cSGarrett Wollman static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ 83602d513cSGarrett Wollman static int arpt_down = 20; /* once declared down, don't send for 20 sec */ 84885f1aa4SPoul-Henning Kamp 85602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW, 86602d513cSGarrett Wollman &arpt_prune, 0, ""); 87602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW, 88602d513cSGarrett Wollman &arpt_keep, 0, ""); 89602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW, 90602d513cSGarrett Wollman &arpt_down, 0, ""); 91885f1aa4SPoul-Henning Kamp 92df8bae1dSRodney W. Grimes #define rt_expire rt_rmx.rmx_expire 93df8bae1dSRodney W. Grimes 9466800f57SGarrett Wollman struct llinfo_arp { 95e3975643SJake Burkholder LIST_ENTRY(llinfo_arp) la_le; 9666800f57SGarrett Wollman struct rtentry *la_rt; 9766800f57SGarrett Wollman struct mbuf *la_hold; /* last packet until resolved/timeout */ 9866800f57SGarrett Wollman long la_asked; /* last time we QUERIED for this addr */ 9966800f57SGarrett Wollman #define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */ 10066800f57SGarrett Wollman }; 10166800f57SGarrett Wollman 102e3975643SJake Burkholder static LIST_HEAD(, llinfo_arp) llinfo_arp; 103df8bae1dSRodney W. Grimes 104df5e1987SJonathan Lemon struct ifqueue arpintrq; 105f708ef1bSPoul-Henning Kamp static int arp_inuse, arp_allocated; 106df8bae1dSRodney W. Grimes 107885f1aa4SPoul-Henning Kamp static int arp_maxtries = 5; 108602d513cSGarrett Wollman static int useloopback = 1; /* use loopback interface for local traffic */ 109885f1aa4SPoul-Henning Kamp static int arp_proxyall = 0; 110602d513cSGarrett Wollman 111602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW, 112602d513cSGarrett Wollman &arp_maxtries, 0, ""); 113602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW, 114602d513cSGarrett Wollman &useloopback, 0, ""); 115602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW, 116602d513cSGarrett Wollman &arp_proxyall, 0, ""); 117885f1aa4SPoul-Henning Kamp 118df5e1987SJonathan Lemon static void arp_init __P((void)); 1198071913dSRuslan Ermilov static void arp_rtrequest __P((int, struct rtentry *, struct rt_addrinfo *)); 120322dcb8dSMax Khon static void arprequest __P((struct ifnet *, 121ed7509acSJulian Elischer struct in_addr *, struct in_addr *, u_char *)); 122885f1aa4SPoul-Henning Kamp static void arpintr __P((void)); 123885f1aa4SPoul-Henning Kamp static void arptfree __P((struct llinfo_arp *)); 124885f1aa4SPoul-Henning Kamp static void arptimer __P((void *)); 125885f1aa4SPoul-Henning Kamp static struct llinfo_arp 126885f1aa4SPoul-Henning Kamp *arplookup __P((u_long, int, int)); 1271d5e9e22SEivind Eklund #ifdef INET 128885f1aa4SPoul-Henning Kamp static void in_arpinput __P((struct mbuf *)); 1291d5e9e22SEivind Eklund #endif 13028e82295SGarrett Wollman 131df8bae1dSRodney W. Grimes /* 132df8bae1dSRodney W. Grimes * Timeout routine. Age arp_tab entries periodically. 133df8bae1dSRodney W. Grimes */ 134df8bae1dSRodney W. Grimes /* ARGSUSED */ 135df8bae1dSRodney W. Grimes static void 136df8bae1dSRodney W. Grimes arptimer(ignored_arg) 137df8bae1dSRodney W. Grimes void *ignored_arg; 138df8bae1dSRodney W. Grimes { 139df8bae1dSRodney W. Grimes int s = splnet(); 140fc2ffbe6SPoul-Henning Kamp register struct llinfo_arp *la = LIST_FIRST(&llinfo_arp); 14166800f57SGarrett Wollman struct llinfo_arp *ola; 142df8bae1dSRodney W. Grimes 143df8bae1dSRodney W. Grimes timeout(arptimer, (caddr_t)0, arpt_prune * hz); 14466800f57SGarrett Wollman while ((ola = la) != 0) { 145df8bae1dSRodney W. Grimes register struct rtentry *rt = la->la_rt; 146fc2ffbe6SPoul-Henning Kamp la = LIST_NEXT(la, la_le); 147227ee8a1SPoul-Henning Kamp if (rt->rt_expire && rt->rt_expire <= time_second) 14866800f57SGarrett Wollman arptfree(ola); /* timer has expired, clear */ 149df8bae1dSRodney W. Grimes } 150df8bae1dSRodney W. Grimes splx(s); 151df8bae1dSRodney W. Grimes } 152df8bae1dSRodney W. Grimes 153df8bae1dSRodney W. Grimes /* 154df8bae1dSRodney W. Grimes * Parallel to llc_rtrequest. 155df8bae1dSRodney W. Grimes */ 156b2774d00SGarrett Wollman static void 1578071913dSRuslan Ermilov arp_rtrequest(req, rt, info) 158df8bae1dSRodney W. Grimes int req; 159df8bae1dSRodney W. Grimes register struct rtentry *rt; 1608071913dSRuslan Ermilov struct rt_addrinfo *info; 161df8bae1dSRodney W. Grimes { 162df8bae1dSRodney W. Grimes register struct sockaddr *gate = rt->rt_gateway; 163df8bae1dSRodney W. Grimes register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo; 164df8bae1dSRodney W. Grimes static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 165885f1aa4SPoul-Henning Kamp static int arpinit_done; 166df8bae1dSRodney W. Grimes 167df8bae1dSRodney W. Grimes if (!arpinit_done) { 168df8bae1dSRodney W. Grimes arpinit_done = 1; 16966800f57SGarrett Wollman LIST_INIT(&llinfo_arp); 170df8bae1dSRodney W. Grimes timeout(arptimer, (caddr_t)0, hz); 171242c5536SPeter Wemm register_netisr(NETISR_ARP, arpintr); 172df8bae1dSRodney W. Grimes } 173df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_GATEWAY) 174df8bae1dSRodney W. Grimes return; 175df8bae1dSRodney W. Grimes switch (req) { 176df8bae1dSRodney W. Grimes 177df8bae1dSRodney W. Grimes case RTM_ADD: 178df8bae1dSRodney W. Grimes /* 179df8bae1dSRodney W. Grimes * XXX: If this is a manually added route to interface 180df8bae1dSRodney W. Grimes * such as older version of routed or gated might provide, 181df8bae1dSRodney W. Grimes * restore cloning bit. 182df8bae1dSRodney W. Grimes */ 183df8bae1dSRodney W. Grimes if ((rt->rt_flags & RTF_HOST) == 0 && 184df8bae1dSRodney W. Grimes SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 185df8bae1dSRodney W. Grimes rt->rt_flags |= RTF_CLONING; 186df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_CLONING) { 187df8bae1dSRodney W. Grimes /* 188df8bae1dSRodney W. Grimes * Case 1: This route should come from a route to iface. 189df8bae1dSRodney W. Grimes */ 190df8bae1dSRodney W. Grimes rt_setgate(rt, rt_key(rt), 191df8bae1dSRodney W. Grimes (struct sockaddr *)&null_sdl); 192df8bae1dSRodney W. Grimes gate = rt->rt_gateway; 193df8bae1dSRodney W. Grimes SDL(gate)->sdl_type = rt->rt_ifp->if_type; 194df8bae1dSRodney W. Grimes SDL(gate)->sdl_index = rt->rt_ifp->if_index; 195227ee8a1SPoul-Henning Kamp rt->rt_expire = time_second; 196df8bae1dSRodney W. Grimes break; 197df8bae1dSRodney W. Grimes } 198df8bae1dSRodney W. Grimes /* Announce a new entry if requested. */ 199df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_ANNOUNCE) 200322dcb8dSMax Khon arprequest(rt->rt_ifp, 201ed7509acSJulian Elischer &SIN(rt_key(rt))->sin_addr, 202ed7509acSJulian Elischer &SIN(rt_key(rt))->sin_addr, 203df8bae1dSRodney W. Grimes (u_char *)LLADDR(SDL(gate))); 204df8bae1dSRodney W. Grimes /*FALLTHROUGH*/ 205df8bae1dSRodney W. Grimes case RTM_RESOLVE: 206df8bae1dSRodney W. Grimes if (gate->sa_family != AF_LINK || 207df8bae1dSRodney W. Grimes gate->sa_len < sizeof(null_sdl)) { 20838aa9fc3SDavid Greenman log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n"); 209df8bae1dSRodney W. Grimes break; 210df8bae1dSRodney W. Grimes } 211df8bae1dSRodney W. Grimes SDL(gate)->sdl_type = rt->rt_ifp->if_type; 212df8bae1dSRodney W. Grimes SDL(gate)->sdl_index = rt->rt_ifp->if_index; 213df8bae1dSRodney W. Grimes if (la != 0) 214df8bae1dSRodney W. Grimes break; /* This happens on a route change */ 215df8bae1dSRodney W. Grimes /* 216df8bae1dSRodney W. Grimes * Case 2: This route may come from cloning, or a manual route 217df8bae1dSRodney W. Grimes * add with a LL address. 218df8bae1dSRodney W. Grimes */ 219df8bae1dSRodney W. Grimes R_Malloc(la, struct llinfo_arp *, sizeof(*la)); 220df8bae1dSRodney W. Grimes rt->rt_llinfo = (caddr_t)la; 221df8bae1dSRodney W. Grimes if (la == 0) { 222df8bae1dSRodney W. Grimes log(LOG_DEBUG, "arp_rtrequest: malloc failed\n"); 223df8bae1dSRodney W. Grimes break; 224df8bae1dSRodney W. Grimes } 225df8bae1dSRodney W. Grimes arp_inuse++, arp_allocated++; 226df8bae1dSRodney W. Grimes Bzero(la, sizeof(*la)); 227df8bae1dSRodney W. Grimes la->la_rt = rt; 228df8bae1dSRodney W. Grimes rt->rt_flags |= RTF_LLINFO; 22966800f57SGarrett Wollman LIST_INSERT_HEAD(&llinfo_arp, la, la_le); 230cbb0b46aSGarrett Wollman 2311d5e9e22SEivind Eklund #ifdef INET 232cbb0b46aSGarrett Wollman /* 233cbb0b46aSGarrett Wollman * This keeps the multicast addresses from showing up 234cbb0b46aSGarrett Wollman * in `arp -a' listings as unresolved. It's not actually 235cbb0b46aSGarrett Wollman * functional. Then the same for broadcast. 236cbb0b46aSGarrett Wollman */ 237322dcb8dSMax Khon if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) 238322dcb8dSMax Khon && rt->rt_ifp->if_type != IFT_ARCNET) { 239cbb0b46aSGarrett Wollman ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr, 240cbb0b46aSGarrett Wollman LLADDR(SDL(gate))); 241cbb0b46aSGarrett Wollman SDL(gate)->sdl_alen = 6; 24251a109a1SPeter Wemm rt->rt_expire = 0; 243cbb0b46aSGarrett Wollman } 244cbb0b46aSGarrett Wollman if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) { 245322dcb8dSMax Khon memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr, 246322dcb8dSMax Khon rt->rt_ifp->if_addrlen); 247322dcb8dSMax Khon SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen; 24851a109a1SPeter Wemm rt->rt_expire = 0; 249cbb0b46aSGarrett Wollman } 2501d5e9e22SEivind Eklund #endif 251cbb0b46aSGarrett Wollman 252df8bae1dSRodney W. Grimes if (SIN(rt_key(rt))->sin_addr.s_addr == 253df8bae1dSRodney W. Grimes (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) { 254df8bae1dSRodney W. Grimes /* 255df8bae1dSRodney W. Grimes * This test used to be 256df8bae1dSRodney W. Grimes * if (loif.if_flags & IFF_UP) 257df8bae1dSRodney W. Grimes * It allowed local traffic to be forced 258df8bae1dSRodney W. Grimes * through the hardware by configuring the loopback down. 259df8bae1dSRodney W. Grimes * However, it causes problems during network configuration 260df8bae1dSRodney W. Grimes * for boards that can't receive packets they send. 261df8bae1dSRodney W. Grimes * It is now necessary to clear "useloopback" and remove 262df8bae1dSRodney W. Grimes * the route to force traffic out to the hardware. 263df8bae1dSRodney W. Grimes */ 264df8bae1dSRodney W. Grimes rt->rt_expire = 0; 265322dcb8dSMax Khon Bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)), 266322dcb8dSMax Khon SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen); 267df8bae1dSRodney W. Grimes if (useloopback) 268f5fea3ddSPaul Traina rt->rt_ifp = loif; 269df8bae1dSRodney W. Grimes 270df8bae1dSRodney W. Grimes } 271df8bae1dSRodney W. Grimes break; 272df8bae1dSRodney W. Grimes 273df8bae1dSRodney W. Grimes case RTM_DELETE: 274df8bae1dSRodney W. Grimes if (la == 0) 275df8bae1dSRodney W. Grimes break; 276df8bae1dSRodney W. Grimes arp_inuse--; 27766800f57SGarrett Wollman LIST_REMOVE(la, la_le); 278df8bae1dSRodney W. Grimes rt->rt_llinfo = 0; 279df8bae1dSRodney W. Grimes rt->rt_flags &= ~RTF_LLINFO; 280df8bae1dSRodney W. Grimes if (la->la_hold) 281df8bae1dSRodney W. Grimes m_freem(la->la_hold); 282df8bae1dSRodney W. Grimes Free((caddr_t)la); 283df8bae1dSRodney W. Grimes } 284df8bae1dSRodney W. Grimes } 285df8bae1dSRodney W. Grimes 286df8bae1dSRodney W. Grimes /* 287df8bae1dSRodney W. Grimes * Broadcast an ARP request. Caller specifies: 288df8bae1dSRodney W. Grimes * - arp header source ip address 289df8bae1dSRodney W. Grimes * - arp header target ip address 290df8bae1dSRodney W. Grimes * - arp header source ethernet address 291df8bae1dSRodney W. Grimes */ 292df8bae1dSRodney W. Grimes static void 293322dcb8dSMax Khon arprequest(ifp, sip, tip, enaddr) 294322dcb8dSMax Khon register struct ifnet *ifp; 295ed7509acSJulian Elischer register struct in_addr *sip, *tip; 296df8bae1dSRodney W. Grimes register u_char *enaddr; 297df8bae1dSRodney W. Grimes { 298df8bae1dSRodney W. Grimes register struct mbuf *m; 299df8bae1dSRodney W. Grimes register struct ether_header *eh; 300322dcb8dSMax Khon register struct arc_header *arh; 301322dcb8dSMax Khon register struct arphdr *ah; 302df8bae1dSRodney W. Grimes struct sockaddr sa; 303b149dd6cSLarry Lile static u_char llcx[] = { 0x82, 0x40, LLC_SNAP_LSAP, LLC_SNAP_LSAP, 304b149dd6cSLarry Lile LLC_UI, 0x00, 0x00, 0x00, 0x08, 0x06 }; 305322dcb8dSMax Khon u_short ar_hrd; 306df8bae1dSRodney W. Grimes 307df8bae1dSRodney W. Grimes if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 308df8bae1dSRodney W. Grimes return; 309fda82fc2SJulian Elischer m->m_pkthdr.rcvif = (struct ifnet *)0; 310322dcb8dSMax Khon switch (ifp->if_type) { 311322dcb8dSMax Khon case IFT_ARCNET: 312322dcb8dSMax Khon ar_hrd = htons(ARPHRD_ARCNET); 313322dcb8dSMax Khon 314322dcb8dSMax Khon m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 315322dcb8dSMax Khon m->m_pkthdr.len = m->m_len; 316322dcb8dSMax Khon MH_ALIGN(m, m->m_len); 317322dcb8dSMax Khon 318322dcb8dSMax Khon arh = (struct arc_header *)sa.sa_data; 319322dcb8dSMax Khon arh->arc_dhost = *ifp->if_broadcastaddr; 320322dcb8dSMax Khon arh->arc_type = ARCTYPE_ARP; 321322dcb8dSMax Khon 322322dcb8dSMax Khon ah = mtod(m, struct arphdr *); 323322dcb8dSMax Khon break; 324322dcb8dSMax Khon 325fda82fc2SJulian Elischer case IFT_ISO88025: 326322dcb8dSMax Khon ar_hrd = htons(ARPHRD_IEEE802); 327322dcb8dSMax Khon 328322dcb8dSMax Khon m->m_len = sizeof(llcx) + 329322dcb8dSMax Khon arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 330322dcb8dSMax Khon m->m_pkthdr.len = m->m_len; 331322dcb8dSMax Khon MH_ALIGN(m, m->m_len); 332322dcb8dSMax Khon 333b149dd6cSLarry Lile (void)memcpy(mtod(m, caddr_t), llcx, sizeof(llcx)); 334322dcb8dSMax Khon (void)memcpy(sa.sa_data, ifp->if_broadcastaddr, 6); 335fda82fc2SJulian Elischer (void)memcpy(sa.sa_data + 6, enaddr, 6); 336b149dd6cSLarry Lile sa.sa_data[6] |= TR_RII; 337b149dd6cSLarry Lile sa.sa_data[12] = TR_AC; 338b149dd6cSLarry Lile sa.sa_data[13] = TR_LLC_FRAME; 339322dcb8dSMax Khon 340322dcb8dSMax Khon ah = (struct arphdr *)(mtod(m, char *) + sizeof(llcx)); 341fda82fc2SJulian Elischer break; 342f9083fdbSLarry Lile case IFT_FDDI: 343f9083fdbSLarry Lile case IFT_ETHER: 344f9083fdbSLarry Lile /* 345f9083fdbSLarry Lile * This may not be correct for types not explicitly 346f9083fdbSLarry Lile * listed, but this is our best guess 347f9083fdbSLarry Lile */ 348fda82fc2SJulian Elischer default: 349322dcb8dSMax Khon ar_hrd = htons(ARPHRD_ETHER); 350322dcb8dSMax Khon 351322dcb8dSMax Khon m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 352322dcb8dSMax Khon m->m_pkthdr.len = m->m_len; 353322dcb8dSMax Khon MH_ALIGN(m, m->m_len); 354322dcb8dSMax Khon 355f9083fdbSLarry Lile eh = (struct ether_header *)sa.sa_data; 356f9083fdbSLarry Lile /* if_output will not swap */ 357f9083fdbSLarry Lile eh->ether_type = htons(ETHERTYPE_ARP); 358322dcb8dSMax Khon (void)memcpy(eh->ether_dhost, ifp->if_broadcastaddr, 359f9083fdbSLarry Lile sizeof(eh->ether_dhost)); 360322dcb8dSMax Khon 361322dcb8dSMax Khon ah = mtod(m, struct arphdr *); 362f9083fdbSLarry Lile break; 363fda82fc2SJulian Elischer } 364322dcb8dSMax Khon 365322dcb8dSMax Khon ah->ar_hrd = ar_hrd; 366322dcb8dSMax Khon ah->ar_pro = htons(ETHERTYPE_IP); 367322dcb8dSMax Khon ah->ar_hln = ifp->if_addrlen; /* hardware address length */ 368322dcb8dSMax Khon ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ 369322dcb8dSMax Khon ah->ar_op = htons(ARPOP_REQUEST); 370322dcb8dSMax Khon (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 371322dcb8dSMax Khon (void)memcpy(ar_spa(ah), sip, ah->ar_pln); 372322dcb8dSMax Khon (void)memcpy(ar_tpa(ah), tip, ah->ar_pln); 373322dcb8dSMax Khon 374df8bae1dSRodney W. Grimes sa.sa_family = AF_UNSPEC; 375df8bae1dSRodney W. Grimes sa.sa_len = sizeof(sa); 376322dcb8dSMax Khon (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 377df8bae1dSRodney W. Grimes } 378df8bae1dSRodney W. Grimes 379df8bae1dSRodney W. Grimes /* 380df8bae1dSRodney W. Grimes * Resolve an IP address into an ethernet address. If success, 381df8bae1dSRodney W. Grimes * desten is filled in. If there is no entry in arptab, 382df8bae1dSRodney W. Grimes * set one up and broadcast a request for the IP address. 383df8bae1dSRodney W. Grimes * Hold onto this mbuf and resend it once the address 384df8bae1dSRodney W. Grimes * is finally resolved. A return value of 1 indicates 385df8bae1dSRodney W. Grimes * that desten has been filled in and the packet should be sent 386df8bae1dSRodney W. Grimes * normally; a 0 return indicates that the packet has been 387df8bae1dSRodney W. Grimes * taken over here, either now or for later transmission. 388df8bae1dSRodney W. Grimes */ 389df8bae1dSRodney W. Grimes int 390322dcb8dSMax Khon arpresolve(ifp, rt, m, dst, desten, rt0) 391322dcb8dSMax Khon register struct ifnet *ifp; 392df8bae1dSRodney W. Grimes register struct rtentry *rt; 393df8bae1dSRodney W. Grimes struct mbuf *m; 394df8bae1dSRodney W. Grimes register struct sockaddr *dst; 395df8bae1dSRodney W. Grimes register u_char *desten; 3965df72964SGarrett Wollman struct rtentry *rt0; 397df8bae1dSRodney W. Grimes { 39808aadfbbSJonathan Lemon struct llinfo_arp *la = 0; 399df8bae1dSRodney W. Grimes struct sockaddr_dl *sdl; 400df8bae1dSRodney W. Grimes 401df8bae1dSRodney W. Grimes if (m->m_flags & M_BCAST) { /* broadcast */ 402322dcb8dSMax Khon (void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen); 403df8bae1dSRodney W. Grimes return (1); 404df8bae1dSRodney W. Grimes } 405322dcb8dSMax Khon if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */ 406df8bae1dSRodney W. Grimes ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 407df8bae1dSRodney W. Grimes return(1); 408df8bae1dSRodney W. Grimes } 409df8bae1dSRodney W. Grimes if (rt) 410df8bae1dSRodney W. Grimes la = (struct llinfo_arp *)rt->rt_llinfo; 411e7bc5f27SBill Fenner if (la == 0) { 412623ae52eSPoul-Henning Kamp la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0); 413623ae52eSPoul-Henning Kamp if (la) 414df8bae1dSRodney W. Grimes rt = la->la_rt; 415df8bae1dSRodney W. Grimes } 416df8bae1dSRodney W. Grimes if (la == 0 || rt == 0) { 417245086a0SPoul-Henning Kamp log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n", 418245086a0SPoul-Henning Kamp inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "", 419245086a0SPoul-Henning Kamp rt ? "rt" : ""); 420df8bae1dSRodney W. Grimes m_freem(m); 421df8bae1dSRodney W. Grimes return (0); 422df8bae1dSRodney W. Grimes } 423df8bae1dSRodney W. Grimes sdl = SDL(rt->rt_gateway); 424df8bae1dSRodney W. Grimes /* 425df8bae1dSRodney W. Grimes * Check the address family and length is valid, the address 426df8bae1dSRodney W. Grimes * is resolved; otherwise, try to resolve. 427df8bae1dSRodney W. Grimes */ 428227ee8a1SPoul-Henning Kamp if ((rt->rt_expire == 0 || rt->rt_expire > time_second) && 429df8bae1dSRodney W. Grimes sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { 4300453d3cbSBruce Evans bcopy(LLADDR(sdl), desten, sdl->sdl_alen); 431df8bae1dSRodney W. Grimes return 1; 432df8bae1dSRodney W. Grimes } 433df8bae1dSRodney W. Grimes /* 43408aadfbbSJonathan Lemon * If ARP is disabled on this interface, stop. 43508aadfbbSJonathan Lemon * XXX 43608aadfbbSJonathan Lemon * Probably should not allocate empty llinfo struct if we are 43708aadfbbSJonathan Lemon * not going to be sending out an arp request. 43808aadfbbSJonathan Lemon */ 439322dcb8dSMax Khon if (ifp->if_flags & IFF_NOARP) 44008aadfbbSJonathan Lemon return (0); 44108aadfbbSJonathan Lemon /* 442df8bae1dSRodney W. Grimes * There is an arptab entry, but no ethernet address 443df8bae1dSRodney W. Grimes * response yet. Replace the held mbuf with this 444df8bae1dSRodney W. Grimes * latest one. 445df8bae1dSRodney W. Grimes */ 446df8bae1dSRodney W. Grimes if (la->la_hold) 447df8bae1dSRodney W. Grimes m_freem(la->la_hold); 448df8bae1dSRodney W. Grimes la->la_hold = m; 449df8bae1dSRodney W. Grimes if (rt->rt_expire) { 450df8bae1dSRodney W. Grimes rt->rt_flags &= ~RTF_REJECT; 451227ee8a1SPoul-Henning Kamp if (la->la_asked == 0 || rt->rt_expire != time_second) { 452227ee8a1SPoul-Henning Kamp rt->rt_expire = time_second; 453df8bae1dSRodney W. Grimes if (la->la_asked++ < arp_maxtries) 454322dcb8dSMax Khon arprequest(ifp, 455ed7509acSJulian Elischer &SIN(rt->rt_ifa->ifa_addr)->sin_addr, 456322dcb8dSMax Khon &SIN(dst)->sin_addr, 457322dcb8dSMax Khon IF_LLADDR(ifp)); 458df8bae1dSRodney W. Grimes else { 459df8bae1dSRodney W. Grimes rt->rt_flags |= RTF_REJECT; 460df8bae1dSRodney W. Grimes rt->rt_expire += arpt_down; 461df8bae1dSRodney W. Grimes la->la_asked = 0; 462df8bae1dSRodney W. Grimes } 463df8bae1dSRodney W. Grimes 464df8bae1dSRodney W. Grimes } 465df8bae1dSRodney W. Grimes } 466df8bae1dSRodney W. Grimes return (0); 467df8bae1dSRodney W. Grimes } 468df8bae1dSRodney W. Grimes 469df8bae1dSRodney W. Grimes /* 470df8bae1dSRodney W. Grimes * Common length and type checks are done here, 471df8bae1dSRodney W. Grimes * then the protocol-specific routine is called. 472df8bae1dSRodney W. Grimes */ 473885f1aa4SPoul-Henning Kamp static void 474c5a1016bSBruce Evans arpintr() 475df8bae1dSRodney W. Grimes { 476732f6a43SWes Peters register struct mbuf *m; 477df8bae1dSRodney W. Grimes register struct arphdr *ar; 478732f6a43SWes Peters int s; 479df8bae1dSRodney W. Grimes 480df8bae1dSRodney W. Grimes while (arpintrq.ifq_head) { 481df8bae1dSRodney W. Grimes s = splimp(); 482df8bae1dSRodney W. Grimes IF_DEQUEUE(&arpintrq, m); 483df8bae1dSRodney W. Grimes splx(s); 484df8bae1dSRodney W. Grimes if (m == 0 || (m->m_flags & M_PKTHDR) == 0) 485df8bae1dSRodney W. Grimes panic("arpintr"); 48676ec7b2fSRobert Watson 48776ec7b2fSRobert Watson if (m->m_len < sizeof(struct arphdr) && 48884365e2bSMatthew Dillon ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) { 489e44d6283SJoerg Wunsch log(LOG_ERR, "arp: runt packet -- m_pullup failed\n"); 49076ec7b2fSRobert Watson continue; 49176ec7b2fSRobert Watson } 49276ec7b2fSRobert Watson ar = mtod(m, struct arphdr *); 49376ec7b2fSRobert Watson 49476ec7b2fSRobert Watson if (ntohs(ar->ar_hrd) != ARPHRD_ETHER 495322dcb8dSMax Khon && ntohs(ar->ar_hrd) != ARPHRD_IEEE802 496322dcb8dSMax Khon && ntohs(ar->ar_hrd) != ARPHRD_ARCNET) { 49776ec7b2fSRobert Watson log(LOG_ERR, 498e44d6283SJoerg Wunsch "arp: unknown hardware address format (0x%2D)\n", 49976ec7b2fSRobert Watson (unsigned char *)&ar->ar_hrd, ""); 50076ec7b2fSRobert Watson m_freem(m); 50176ec7b2fSRobert Watson continue; 50276ec7b2fSRobert Watson } 50376ec7b2fSRobert Watson 504322dcb8dSMax Khon if (m->m_pkthdr.len < arphdr_len(ar) && 505322dcb8dSMax Khon (m = m_pullup(m, arphdr_len(ar))) == NULL) { 506f72d9d83SJoerg Wunsch log(LOG_ERR, "arp: runt packet\n"); 50776ec7b2fSRobert Watson m_freem(m); 50876ec7b2fSRobert Watson continue; 50976ec7b2fSRobert Watson } 510df8bae1dSRodney W. Grimes 511df8bae1dSRodney W. Grimes switch (ntohs(ar->ar_pro)) { 5121d5e9e22SEivind Eklund #ifdef INET 513df8bae1dSRodney W. Grimes case ETHERTYPE_IP: 514df8bae1dSRodney W. Grimes in_arpinput(m); 515df8bae1dSRodney W. Grimes continue; 5161d5e9e22SEivind Eklund #endif 517df8bae1dSRodney W. Grimes } 518df8bae1dSRodney W. Grimes m_freem(m); 519df8bae1dSRodney W. Grimes } 520df8bae1dSRodney W. Grimes } 521df8bae1dSRodney W. Grimes 5221d5e9e22SEivind Eklund #ifdef INET 523df8bae1dSRodney W. Grimes /* 524df8bae1dSRodney W. Grimes * ARP for Internet protocols on 10 Mb/s Ethernet. 525df8bae1dSRodney W. Grimes * Algorithm is that given in RFC 826. 526df8bae1dSRodney W. Grimes * In addition, a sanity check is performed on the sender 527df8bae1dSRodney W. Grimes * protocol address, to catch impersonators. 528df8bae1dSRodney W. Grimes * We no longer handle negotiations for use of trailer protocol: 529df8bae1dSRodney W. Grimes * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 530df8bae1dSRodney W. Grimes * along with IP replies if we wanted trailers sent to us, 531df8bae1dSRodney W. Grimes * and also sent them in response to IP replies. 532df8bae1dSRodney W. Grimes * This allowed either end to announce the desire to receive 533df8bae1dSRodney W. Grimes * trailer packets. 534df8bae1dSRodney W. Grimes * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 535df8bae1dSRodney W. Grimes * but formerly didn't normally send requests. 536df8bae1dSRodney W. Grimes */ 5373269187dSAlfred Perlstein static int log_arp_wrong_iface = 1; 538e3d123d6SAlfred Perlstein static int log_arp_movements = 1; 5393269187dSAlfred Perlstein 5403269187dSAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW, 5413269187dSAlfred Perlstein &log_arp_wrong_iface, 0, 5423269187dSAlfred Perlstein "log arp packets arriving on the wrong interface"); 543e3d123d6SAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW, 544e3d123d6SAlfred Perlstein &log_arp_movements, 0, 54575ce3221SAlfred Perlstein "log arp replies from MACs different than the one in the cache"); 546e3d123d6SAlfred Perlstein 5473269187dSAlfred Perlstein 548df8bae1dSRodney W. Grimes static void 549df8bae1dSRodney W. Grimes in_arpinput(m) 550df8bae1dSRodney W. Grimes struct mbuf *m; 551df8bae1dSRodney W. Grimes { 552322dcb8dSMax Khon register struct arphdr *ah; 553322dcb8dSMax Khon register struct ifnet *ifp = m->m_pkthdr.rcvif; 554df8bae1dSRodney W. Grimes struct ether_header *eh; 555322dcb8dSMax Khon struct arc_header *arh; 556fda82fc2SJulian Elischer struct iso88025_header *th = (struct iso88025_header *)0; 557df8bae1dSRodney W. Grimes register struct llinfo_arp *la = 0; 558df8bae1dSRodney W. Grimes register struct rtentry *rt; 559ca925d9cSJonathan Lemon struct ifaddr *ifa; 560ca925d9cSJonathan Lemon struct in_ifaddr *ia; 561df8bae1dSRodney W. Grimes struct sockaddr_dl *sdl; 562df8bae1dSRodney W. Grimes struct sockaddr sa; 563df8bae1dSRodney W. Grimes struct in_addr isaddr, itaddr, myaddr; 564b149dd6cSLarry Lile int op, rif_len; 565322dcb8dSMax Khon int req_len; 566df8bae1dSRodney W. Grimes 567322dcb8dSMax Khon req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 568322dcb8dSMax Khon if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) { 5694cbc8ad1SYaroslav Tykhiy log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n"); 5704cbc8ad1SYaroslav Tykhiy return; 5714cbc8ad1SYaroslav Tykhiy } 5724cbc8ad1SYaroslav Tykhiy 573322dcb8dSMax Khon ah = mtod(m, struct arphdr *); 574322dcb8dSMax Khon op = ntohs(ah->ar_op); 575322dcb8dSMax Khon (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr)); 576322dcb8dSMax Khon (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr)); 57741d2ba5eSJulian Elischer #ifdef BRIDGE 57841d2ba5eSJulian Elischer #define BRIDGE_TEST (do_bridge) 579b715f178SLuigi Rizzo #else 58041d2ba5eSJulian Elischer #define BRIDGE_TEST (0) /* cc will optimise the test away */ 581b715f178SLuigi Rizzo #endif 582ca925d9cSJonathan Lemon /* 583ca925d9cSJonathan Lemon * For a bridge, we want to check the address irrespective 584ca925d9cSJonathan Lemon * of the receive interface. (This will change slightly 585ca925d9cSJonathan Lemon * when we have clusters of interfaces). 586ca925d9cSJonathan Lemon */ 587ca925d9cSJonathan Lemon LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) 588322dcb8dSMax Khon if ((BRIDGE_TEST || (ia->ia_ifp == ifp)) && 589ca925d9cSJonathan Lemon itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) 590ca925d9cSJonathan Lemon goto match; 591ca925d9cSJonathan Lemon LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash) 592322dcb8dSMax Khon if ((BRIDGE_TEST || (ia->ia_ifp == ifp)) && 593ca925d9cSJonathan Lemon isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) 594ca925d9cSJonathan Lemon goto match; 595ca925d9cSJonathan Lemon /* 596d8b84d9eSJonathan Lemon * No match, use the first inet address on the receive interface 597ca925d9cSJonathan Lemon * as a dummy address for the rest of the function. 598ca925d9cSJonathan Lemon */ 599d8b84d9eSJonathan Lemon TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 600d8b84d9eSJonathan Lemon if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) 601d8b84d9eSJonathan Lemon break; 602ca925d9cSJonathan Lemon if (ifa == NULL) { 603885f1aa4SPoul-Henning Kamp m_freem(m); 604885f1aa4SPoul-Henning Kamp return; 605885f1aa4SPoul-Henning Kamp } 606ca925d9cSJonathan Lemon ia = ifatoia(ifa); 607ca925d9cSJonathan Lemon match: 608ca925d9cSJonathan Lemon myaddr = ia->ia_addr.sin_addr; 609322dcb8dSMax Khon if (!bcmp(ar_sha(ah), IF_LLADDR(ifp), ifp->if_addrlen)) { 610885f1aa4SPoul-Henning Kamp m_freem(m); /* it's from me, ignore it. */ 611885f1aa4SPoul-Henning Kamp return; 612885f1aa4SPoul-Henning Kamp } 613322dcb8dSMax Khon if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) { 614df8bae1dSRodney W. Grimes log(LOG_ERR, 615322dcb8dSMax Khon "arp: link address is broadcast for IP address %s!\n", 616ef0cdf33SGarrett Wollman inet_ntoa(isaddr)); 617885f1aa4SPoul-Henning Kamp m_freem(m); 618885f1aa4SPoul-Henning Kamp return; 619df8bae1dSRodney W. Grimes } 620df8bae1dSRodney W. Grimes if (isaddr.s_addr == myaddr.s_addr) { 621df8bae1dSRodney W. Grimes log(LOG_ERR, 622322dcb8dSMax Khon "arp: %*D is using my IP address %s!\n", 623322dcb8dSMax Khon ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 624322dcb8dSMax Khon inet_ntoa(isaddr)); 625df8bae1dSRodney W. Grimes itaddr = myaddr; 626df8bae1dSRodney W. Grimes goto reply; 627df8bae1dSRodney W. Grimes } 628df8bae1dSRodney W. Grimes la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0); 629df8bae1dSRodney W. Grimes if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) { 6307e1cd0d2SLuigi Rizzo /* the following is not an error when doing bridging */ 631322dcb8dSMax Khon if (!BRIDGE_TEST && rt->rt_ifp != ifp) { 6323269187dSAlfred Perlstein if (log_arp_wrong_iface) 633322dcb8dSMax Khon log(LOG_ERR, "arp: %s is on %s%d but got reply from %*D on %s%d\n", 634dd9b6ddeSBill Fenner inet_ntoa(isaddr), 635dd9b6ddeSBill Fenner rt->rt_ifp->if_name, rt->rt_ifp->if_unit, 636322dcb8dSMax Khon ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 637322dcb8dSMax Khon ifp->if_name, ifp->if_unit); 638dd9b6ddeSBill Fenner goto reply; 639dd9b6ddeSBill Fenner } 640df8bae1dSRodney W. Grimes if (sdl->sdl_alen && 641322dcb8dSMax Khon bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) { 642e3d123d6SAlfred Perlstein if (rt->rt_expire) { 643e3d123d6SAlfred Perlstein if (log_arp_movements) 644322dcb8dSMax Khon log(LOG_INFO, "arp: %s moved from %*D to %*D on %s%d\n", 645322dcb8dSMax Khon inet_ntoa(isaddr), 646322dcb8dSMax Khon ifp->if_addrlen, (u_char *)LLADDR(sdl), ":", 647322dcb8dSMax Khon ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 648322dcb8dSMax Khon ifp->if_name, ifp->if_unit); 649e3d123d6SAlfred Perlstein } else { 650dd9b6ddeSBill Fenner log(LOG_ERR, 651322dcb8dSMax Khon "arp: %*D attempts to modify permanent entry for %s on %s%d\n", 652322dcb8dSMax Khon ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 653322dcb8dSMax Khon inet_ntoa(isaddr), ifp->if_name, ifp->if_unit); 654dd9b6ddeSBill Fenner goto reply; 655dd9b6ddeSBill Fenner } 656dfd5dee1SPeter Wemm } 657322dcb8dSMax Khon /* 658322dcb8dSMax Khon * sanity check for the address length. 659322dcb8dSMax Khon * XXX this does not work for protocols with variable address 660322dcb8dSMax Khon * length. -is 661322dcb8dSMax Khon */ 662322dcb8dSMax Khon if (sdl->sdl_alen && 663322dcb8dSMax Khon sdl->sdl_alen != ah->ar_hln) { 664322dcb8dSMax Khon log(LOG_WARNING, 665322dcb8dSMax Khon "arp from %*D: new addr len %d, was %d", 666322dcb8dSMax Khon ifp->if_addrlen, (u_char *) ar_sha(ah), ":", 667322dcb8dSMax Khon ah->ar_hln, sdl->sdl_alen); 668322dcb8dSMax Khon } 669322dcb8dSMax Khon if (ifp->if_addrlen != ah->ar_hln) { 670322dcb8dSMax Khon log(LOG_WARNING, 671322dcb8dSMax Khon "arp from %*D: addr len: new %d, i/f %d (ignored)", 672322dcb8dSMax Khon ifp->if_addrlen, (u_char *) ar_sha(ah), ":", 673322dcb8dSMax Khon ah->ar_hln, ifp->if_addrlen); 674322dcb8dSMax Khon goto reply; 675322dcb8dSMax Khon } 676322dcb8dSMax Khon (void)memcpy(LLADDR(sdl), ar_sha(ah), 677322dcb8dSMax Khon sdl->sdl_alen = ah->ar_hln); 678b149dd6cSLarry Lile sdl->sdl_rcf = (u_short)0; 679243c4c6dSEivind Eklund /* 680243c4c6dSEivind Eklund * If we receive an arp from a token-ring station over 681243c4c6dSEivind Eklund * a token-ring nic then try to save the source 682243c4c6dSEivind Eklund * routing info. 683243c4c6dSEivind Eklund */ 684322dcb8dSMax Khon if (ifp->if_type == IFT_ISO88025) { 685fda82fc2SJulian Elischer th = (struct iso88025_header *)m->m_pkthdr.header; 686b149dd6cSLarry Lile rif_len = TR_RCF_RIFLEN(th->rcf); 687b149dd6cSLarry Lile if ((th->iso88025_shost[0] & TR_RII) && 688b149dd6cSLarry Lile (rif_len > 2)) { 689b149dd6cSLarry Lile sdl->sdl_rcf = th->rcf; 690b149dd6cSLarry Lile sdl->sdl_rcf ^= htons(TR_RCF_DIR); 691b149dd6cSLarry Lile memcpy(sdl->sdl_route, th->rd, rif_len - 2); 692b149dd6cSLarry Lile sdl->sdl_rcf &= ~htons(TR_RCF_BCST_MASK); 693f9083fdbSLarry Lile /* 694f9083fdbSLarry Lile * Set up source routing information for 695f9083fdbSLarry Lile * reply packet (XXX) 696f9083fdbSLarry Lile */ 697b149dd6cSLarry Lile m->m_data -= rif_len; 698b149dd6cSLarry Lile m->m_len += rif_len; 699b149dd6cSLarry Lile m->m_pkthdr.len += rif_len; 700fda82fc2SJulian Elischer } else { 701b149dd6cSLarry Lile th->iso88025_shost[0] &= ~TR_RII; 702fcf11853SLarry Lile } 703fda82fc2SJulian Elischer m->m_data -= 8; 704fda82fc2SJulian Elischer m->m_len += 8; 705fcf11853SLarry Lile m->m_pkthdr.len += 8; 706fda82fc2SJulian Elischer th->rcf = sdl->sdl_rcf; 707243c4c6dSEivind Eklund } else { 708b149dd6cSLarry Lile sdl->sdl_rcf = (u_short)0; 709fda82fc2SJulian Elischer } 710df8bae1dSRodney W. Grimes if (rt->rt_expire) 711227ee8a1SPoul-Henning Kamp rt->rt_expire = time_second + arpt_keep; 712df8bae1dSRodney W. Grimes rt->rt_flags &= ~RTF_REJECT; 713df8bae1dSRodney W. Grimes la->la_asked = 0; 714df8bae1dSRodney W. Grimes if (la->la_hold) { 715322dcb8dSMax Khon (*ifp->if_output)(ifp, la->la_hold, 716df8bae1dSRodney W. Grimes rt_key(rt), rt); 717df8bae1dSRodney W. Grimes la->la_hold = 0; 718df8bae1dSRodney W. Grimes } 719df8bae1dSRodney W. Grimes } 720df8bae1dSRodney W. Grimes reply: 721df8bae1dSRodney W. Grimes if (op != ARPOP_REQUEST) { 722df8bae1dSRodney W. Grimes m_freem(m); 723df8bae1dSRodney W. Grimes return; 724df8bae1dSRodney W. Grimes } 725df8bae1dSRodney W. Grimes if (itaddr.s_addr == myaddr.s_addr) { 726df8bae1dSRodney W. Grimes /* I am the target */ 727322dcb8dSMax Khon (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 728322dcb8dSMax Khon (void)memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln); 729df8bae1dSRodney W. Grimes } else { 730df8bae1dSRodney W. Grimes la = arplookup(itaddr.s_addr, 0, SIN_PROXY); 73128e82295SGarrett Wollman if (la == NULL) { 73228e82295SGarrett Wollman struct sockaddr_in sin; 73328e82295SGarrett Wollman 734885f1aa4SPoul-Henning Kamp if (!arp_proxyall) { 735885f1aa4SPoul-Henning Kamp m_freem(m); 736885f1aa4SPoul-Henning Kamp return; 737885f1aa4SPoul-Henning Kamp } 73828e82295SGarrett Wollman 73928e82295SGarrett Wollman bzero(&sin, sizeof sin); 74028e82295SGarrett Wollman sin.sin_family = AF_INET; 74128e82295SGarrett Wollman sin.sin_len = sizeof sin; 74228e82295SGarrett Wollman sin.sin_addr = itaddr; 74328e82295SGarrett Wollman 74431246bc2SGarrett Wollman rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 745885f1aa4SPoul-Henning Kamp if (!rt) { 746885f1aa4SPoul-Henning Kamp m_freem(m); 747885f1aa4SPoul-Henning Kamp return; 748885f1aa4SPoul-Henning Kamp } 74928e82295SGarrett Wollman /* 75028e82295SGarrett Wollman * Don't send proxies for nodes on the same interface 75128e82295SGarrett Wollman * as this one came out of, or we'll get into a fight 75228e82295SGarrett Wollman * over who claims what Ether address. 75328e82295SGarrett Wollman */ 754322dcb8dSMax Khon if (rt->rt_ifp == ifp) { 75528e82295SGarrett Wollman rtfree(rt); 756885f1aa4SPoul-Henning Kamp m_freem(m); 757885f1aa4SPoul-Henning Kamp return; 75828e82295SGarrett Wollman } 759322dcb8dSMax Khon (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 760322dcb8dSMax Khon (void)memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln); 76128e82295SGarrett Wollman rtfree(rt); 762cc728227SDavid Malone 763cc728227SDavid Malone /* 764cc728227SDavid Malone * Also check that the node which sent the ARP packet 765cc728227SDavid Malone * is on the the interface we expect it to be on. This 766cc728227SDavid Malone * avoids ARP chaos if an interface is connected to the 767cc728227SDavid Malone * wrong network. 768cc728227SDavid Malone */ 769cc728227SDavid Malone sin.sin_addr = isaddr; 770cc728227SDavid Malone 771cc728227SDavid Malone rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 772cc728227SDavid Malone if (!rt) { 773cc728227SDavid Malone m_freem(m); 774cc728227SDavid Malone return; 775cc728227SDavid Malone } 776322dcb8dSMax Khon if (rt->rt_ifp != ifp) { 777cc728227SDavid Malone log(LOG_INFO, "arp_proxy: ignoring request" 778cc728227SDavid Malone " from %s via %s%d, expecting %s%d\n", 779322dcb8dSMax Khon inet_ntoa(isaddr), ifp->if_name, 780322dcb8dSMax Khon ifp->if_unit, rt->rt_ifp->if_name, 781cc728227SDavid Malone rt->rt_ifp->if_unit); 782cc728227SDavid Malone rtfree(rt); 783cc728227SDavid Malone m_freem(m); 784cc728227SDavid Malone return; 785cc728227SDavid Malone } 786cc728227SDavid Malone rtfree(rt); 787cc728227SDavid Malone 788ac234f93SGarrett Wollman #ifdef DEBUG_PROXY 789ac234f93SGarrett Wollman printf("arp: proxying for %s\n", 790ef0cdf33SGarrett Wollman inet_ntoa(itaddr)); 791ac234f93SGarrett Wollman #endif 79228e82295SGarrett Wollman } else { 793df8bae1dSRodney W. Grimes rt = la->la_rt; 794322dcb8dSMax Khon (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 795df8bae1dSRodney W. Grimes sdl = SDL(rt->rt_gateway); 796322dcb8dSMax Khon (void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln); 79728e82295SGarrett Wollman } 798df8bae1dSRodney W. Grimes } 799df8bae1dSRodney W. Grimes 800322dcb8dSMax Khon (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln); 801322dcb8dSMax Khon (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln); 802322dcb8dSMax Khon ah->ar_op = htons(ARPOP_REPLY); 803322dcb8dSMax Khon ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 804322dcb8dSMax Khon switch (ifp->if_type) { 805322dcb8dSMax Khon case IFT_ARCNET: 806322dcb8dSMax Khon arh = (struct arc_header *)sa.sa_data; 807322dcb8dSMax Khon arh->arc_dhost = *ar_tha(ah); 808322dcb8dSMax Khon arh->arc_type = ARCTYPE_ARP; 809322dcb8dSMax Khon break; 810322dcb8dSMax Khon 811243c4c6dSEivind Eklund case IFT_ISO88025: 812fda82fc2SJulian Elischer /* Re-arrange the source/dest address */ 813f9083fdbSLarry Lile memcpy(th->iso88025_dhost, th->iso88025_shost, 814f9083fdbSLarry Lile sizeof(th->iso88025_dhost)); 815322dcb8dSMax Khon memcpy(th->iso88025_shost, IF_LLADDR(ifp), 816f9083fdbSLarry Lile sizeof(th->iso88025_shost)); 817fda82fc2SJulian Elischer /* Set the source routing bit if neccesary */ 818b149dd6cSLarry Lile if (th->iso88025_dhost[0] & TR_RII) { 819b149dd6cSLarry Lile th->iso88025_dhost[0] &= ~TR_RII; 820b149dd6cSLarry Lile if (TR_RCF_RIFLEN(th->rcf) > 2) 821b149dd6cSLarry Lile th->iso88025_shost[0] |= TR_RII; 822fda82fc2SJulian Elischer } 823fda82fc2SJulian Elischer /* Copy the addresses, ac and fc into sa_data */ 824f9083fdbSLarry Lile memcpy(sa.sa_data, th->iso88025_dhost, 825f9083fdbSLarry Lile sizeof(th->iso88025_dhost) * 2); 826b149dd6cSLarry Lile sa.sa_data[(sizeof(th->iso88025_dhost) * 2)] = TR_AC; 827b149dd6cSLarry Lile sa.sa_data[(sizeof(th->iso88025_dhost) * 2) + 1] = TR_LLC_FRAME; 828fda82fc2SJulian Elischer break; 829243c4c6dSEivind Eklund case IFT_ETHER: 830f9083fdbSLarry Lile case IFT_FDDI: 831f9083fdbSLarry Lile /* 832f9083fdbSLarry Lile * May not be correct for types not explictly 833f9083fdbSLarry Lile * listed, but it is our best guess. 834f9083fdbSLarry Lile */ 835f9083fdbSLarry Lile default: 836df8bae1dSRodney W. Grimes eh = (struct ether_header *)sa.sa_data; 837322dcb8dSMax Khon (void)memcpy(eh->ether_dhost, ar_tha(ah), 838f9083fdbSLarry Lile sizeof(eh->ether_dhost)); 83934bed8b0SDavid Greenman eh->ether_type = htons(ETHERTYPE_ARP); 840fda82fc2SJulian Elischer break; 841fda82fc2SJulian Elischer } 842df8bae1dSRodney W. Grimes sa.sa_family = AF_UNSPEC; 843df8bae1dSRodney W. Grimes sa.sa_len = sizeof(sa); 844322dcb8dSMax Khon (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 845df8bae1dSRodney W. Grimes return; 846df8bae1dSRodney W. Grimes } 8471d5e9e22SEivind Eklund #endif 848df8bae1dSRodney W. Grimes 849df8bae1dSRodney W. Grimes /* 850df8bae1dSRodney W. Grimes * Free an arp entry. 851df8bae1dSRodney W. Grimes */ 852df8bae1dSRodney W. Grimes static void 853df8bae1dSRodney W. Grimes arptfree(la) 854df8bae1dSRodney W. Grimes register struct llinfo_arp *la; 855df8bae1dSRodney W. Grimes { 856df8bae1dSRodney W. Grimes register struct rtentry *rt = la->la_rt; 857df8bae1dSRodney W. Grimes register struct sockaddr_dl *sdl; 858df8bae1dSRodney W. Grimes if (rt == 0) 859df8bae1dSRodney W. Grimes panic("arptfree"); 860df8bae1dSRodney W. Grimes if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) && 861df8bae1dSRodney W. Grimes sdl->sdl_family == AF_LINK) { 862df8bae1dSRodney W. Grimes sdl->sdl_alen = 0; 863df8bae1dSRodney W. Grimes la->la_asked = 0; 864df8bae1dSRodney W. Grimes rt->rt_flags &= ~RTF_REJECT; 865df8bae1dSRodney W. Grimes return; 866df8bae1dSRodney W. Grimes } 867df8bae1dSRodney W. Grimes rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt), 868df8bae1dSRodney W. Grimes 0, (struct rtentry **)0); 869df8bae1dSRodney W. Grimes } 870df8bae1dSRodney W. Grimes /* 871df8bae1dSRodney W. Grimes * Lookup or enter a new address in arptab. 872df8bae1dSRodney W. Grimes */ 873df8bae1dSRodney W. Grimes static struct llinfo_arp * 874df8bae1dSRodney W. Grimes arplookup(addr, create, proxy) 875df8bae1dSRodney W. Grimes u_long addr; 876df8bae1dSRodney W. Grimes int create, proxy; 877df8bae1dSRodney W. Grimes { 878df8bae1dSRodney W. Grimes register struct rtentry *rt; 879df8bae1dSRodney W. Grimes static struct sockaddr_inarp sin = {sizeof(sin), AF_INET }; 880ac234f93SGarrett Wollman const char *why = 0; 881df8bae1dSRodney W. Grimes 882df8bae1dSRodney W. Grimes sin.sin_addr.s_addr = addr; 883df8bae1dSRodney W. Grimes sin.sin_other = proxy ? SIN_PROXY : 0; 88431246bc2SGarrett Wollman rt = rtalloc1((struct sockaddr *)&sin, create, 0UL); 885df8bae1dSRodney W. Grimes if (rt == 0) 886df8bae1dSRodney W. Grimes return (0); 887df8bae1dSRodney W. Grimes rt->rt_refcnt--; 888ac234f93SGarrett Wollman 889ac234f93SGarrett Wollman if (rt->rt_flags & RTF_GATEWAY) 890ac234f93SGarrett Wollman why = "host is not on local network"; 891ac234f93SGarrett Wollman else if ((rt->rt_flags & RTF_LLINFO) == 0) 892ac234f93SGarrett Wollman why = "could not allocate llinfo"; 893ac234f93SGarrett Wollman else if (rt->rt_gateway->sa_family != AF_LINK) 894ac234f93SGarrett Wollman why = "gateway route is not ours"; 895ac234f93SGarrett Wollman 896ac234f93SGarrett Wollman if (why && create) { 897ac234f93SGarrett Wollman log(LOG_DEBUG, "arplookup %s failed: %s\n", 898ef0cdf33SGarrett Wollman inet_ntoa(sin.sin_addr), why); 899ac234f93SGarrett Wollman return 0; 900ac234f93SGarrett Wollman } else if (why) { 901ac234f93SGarrett Wollman return 0; 902df8bae1dSRodney W. Grimes } 903df8bae1dSRodney W. Grimes return ((struct llinfo_arp *)rt->rt_llinfo); 904df8bae1dSRodney W. Grimes } 905df8bae1dSRodney W. Grimes 906dd2e4102SGarrett Wollman void 907322dcb8dSMax Khon arp_ifinit(ifp, ifa) 908322dcb8dSMax Khon struct ifnet *ifp; 909dd2e4102SGarrett Wollman struct ifaddr *ifa; 910dd2e4102SGarrett Wollman { 911dd570d4dSTor Egge if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 912322dcb8dSMax Khon arprequest(ifp, &IA_SIN(ifa)->sin_addr, 913322dcb8dSMax Khon &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp)); 914dd2e4102SGarrett Wollman ifa->ifa_rtrequest = arp_rtrequest; 915dd2e4102SGarrett Wollman ifa->ifa_flags |= RTF_CLONING; 916dd2e4102SGarrett Wollman } 917df5e1987SJonathan Lemon 918df5e1987SJonathan Lemon static void 919df5e1987SJonathan Lemon arp_init(void) 920df5e1987SJonathan Lemon { 921df5e1987SJonathan Lemon 922df5e1987SJonathan Lemon arpintrq.ifq_maxlen = 50; 923df5e1987SJonathan Lemon mtx_init(&arpintrq.ifq_mtx, "arp_inq", MTX_DEF); 924df5e1987SJonathan Lemon } 925df5e1987SJonathan Lemon 926df5e1987SJonathan Lemon SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0); 927