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 71fda82fc2SJulian Elischer #include <net/iso88025.h> 72fda82fc2SJulian Elischer 73df8bae1dSRodney W. Grimes #define SIN(s) ((struct sockaddr_in *)s) 74df8bae1dSRodney W. Grimes #define SDL(s) ((struct sockaddr_dl *)s) 75df8bae1dSRodney W. Grimes 76ce02431fSDoug Rabson SYSCTL_DECL(_net_link_ether); 77602d513cSGarrett Wollman SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, ""); 78df8bae1dSRodney W. Grimes 79df8bae1dSRodney W. Grimes /* timer values */ 80602d513cSGarrett Wollman static int arpt_prune = (5*60*1); /* walk list every 5 minutes */ 81602d513cSGarrett Wollman static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ 82602d513cSGarrett Wollman static int arpt_down = 20; /* once declared down, don't send for 20 sec */ 83885f1aa4SPoul-Henning Kamp 84602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW, 85602d513cSGarrett Wollman &arpt_prune, 0, ""); 86602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW, 87602d513cSGarrett Wollman &arpt_keep, 0, ""); 88602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW, 89602d513cSGarrett Wollman &arpt_down, 0, ""); 90885f1aa4SPoul-Henning Kamp 91df8bae1dSRodney W. Grimes #define rt_expire rt_rmx.rmx_expire 92df8bae1dSRodney W. Grimes 9366800f57SGarrett Wollman struct llinfo_arp { 94e3975643SJake Burkholder LIST_ENTRY(llinfo_arp) la_le; 9566800f57SGarrett Wollman struct rtentry *la_rt; 9666800f57SGarrett Wollman struct mbuf *la_hold; /* last packet until resolved/timeout */ 9766800f57SGarrett Wollman long la_asked; /* last time we QUERIED for this addr */ 9866800f57SGarrett Wollman #define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */ 9966800f57SGarrett Wollman }; 10066800f57SGarrett Wollman 101e3975643SJake Burkholder static LIST_HEAD(, llinfo_arp) llinfo_arp; 102df8bae1dSRodney W. Grimes 103df5e1987SJonathan Lemon struct ifqueue arpintrq; 104f708ef1bSPoul-Henning Kamp static int arp_inuse, arp_allocated; 105df8bae1dSRodney W. Grimes 106885f1aa4SPoul-Henning Kamp static int arp_maxtries = 5; 107602d513cSGarrett Wollman static int useloopback = 1; /* use loopback interface for local traffic */ 108885f1aa4SPoul-Henning Kamp static int arp_proxyall = 0; 109602d513cSGarrett Wollman 110602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW, 111602d513cSGarrett Wollman &arp_maxtries, 0, ""); 112602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW, 113602d513cSGarrett Wollman &useloopback, 0, ""); 114602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW, 115602d513cSGarrett Wollman &arp_proxyall, 0, ""); 116885f1aa4SPoul-Henning Kamp 117df5e1987SJonathan Lemon static void arp_init __P((void)); 118081d3b93SBruce Evans static void arp_rtrequest __P((int, struct rtentry *, struct sockaddr *)); 119ed7509acSJulian Elischer static void arprequest __P((struct arpcom *, 120ed7509acSJulian Elischer struct in_addr *, struct in_addr *, u_char *)); 121885f1aa4SPoul-Henning Kamp static void arpintr __P((void)); 122885f1aa4SPoul-Henning Kamp static void arptfree __P((struct llinfo_arp *)); 123885f1aa4SPoul-Henning Kamp static void arptimer __P((void *)); 124885f1aa4SPoul-Henning Kamp static struct llinfo_arp 125885f1aa4SPoul-Henning Kamp *arplookup __P((u_long, int, int)); 1261d5e9e22SEivind Eklund #ifdef INET 127885f1aa4SPoul-Henning Kamp static void in_arpinput __P((struct mbuf *)); 1281d5e9e22SEivind Eklund #endif 12928e82295SGarrett Wollman 130df8bae1dSRodney W. Grimes /* 131df8bae1dSRodney W. Grimes * Timeout routine. Age arp_tab entries periodically. 132df8bae1dSRodney W. Grimes */ 133df8bae1dSRodney W. Grimes /* ARGSUSED */ 134df8bae1dSRodney W. Grimes static void 135df8bae1dSRodney W. Grimes arptimer(ignored_arg) 136df8bae1dSRodney W. Grimes void *ignored_arg; 137df8bae1dSRodney W. Grimes { 138df8bae1dSRodney W. Grimes int s = splnet(); 139fc2ffbe6SPoul-Henning Kamp register struct llinfo_arp *la = LIST_FIRST(&llinfo_arp); 14066800f57SGarrett Wollman struct llinfo_arp *ola; 141df8bae1dSRodney W. Grimes 142df8bae1dSRodney W. Grimes timeout(arptimer, (caddr_t)0, arpt_prune * hz); 14366800f57SGarrett Wollman while ((ola = la) != 0) { 144df8bae1dSRodney W. Grimes register struct rtentry *rt = la->la_rt; 145fc2ffbe6SPoul-Henning Kamp la = LIST_NEXT(la, la_le); 146227ee8a1SPoul-Henning Kamp if (rt->rt_expire && rt->rt_expire <= time_second) 14766800f57SGarrett Wollman arptfree(ola); /* timer has expired, clear */ 148df8bae1dSRodney W. Grimes } 149df8bae1dSRodney W. Grimes splx(s); 150df8bae1dSRodney W. Grimes } 151df8bae1dSRodney W. Grimes 152df8bae1dSRodney W. Grimes /* 153df8bae1dSRodney W. Grimes * Parallel to llc_rtrequest. 154df8bae1dSRodney W. Grimes */ 155b2774d00SGarrett Wollman static void 156df8bae1dSRodney W. Grimes arp_rtrequest(req, rt, sa) 157df8bae1dSRodney W. Grimes int req; 158df8bae1dSRodney W. Grimes register struct rtentry *rt; 159df8bae1dSRodney W. Grimes struct sockaddr *sa; 160df8bae1dSRodney W. Grimes { 161df8bae1dSRodney W. Grimes register struct sockaddr *gate = rt->rt_gateway; 162df8bae1dSRodney W. Grimes register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo; 163df8bae1dSRodney W. Grimes static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 164885f1aa4SPoul-Henning Kamp static int arpinit_done; 165df8bae1dSRodney W. Grimes 166df8bae1dSRodney W. Grimes if (!arpinit_done) { 167df8bae1dSRodney W. Grimes arpinit_done = 1; 16866800f57SGarrett Wollman LIST_INIT(&llinfo_arp); 169df8bae1dSRodney W. Grimes timeout(arptimer, (caddr_t)0, hz); 170242c5536SPeter Wemm register_netisr(NETISR_ARP, arpintr); 171df8bae1dSRodney W. Grimes } 172df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_GATEWAY) 173df8bae1dSRodney W. Grimes return; 174df8bae1dSRodney W. Grimes switch (req) { 175df8bae1dSRodney W. Grimes 176df8bae1dSRodney W. Grimes case RTM_ADD: 177df8bae1dSRodney W. Grimes /* 178df8bae1dSRodney W. Grimes * XXX: If this is a manually added route to interface 179df8bae1dSRodney W. Grimes * such as older version of routed or gated might provide, 180df8bae1dSRodney W. Grimes * restore cloning bit. 181df8bae1dSRodney W. Grimes */ 182df8bae1dSRodney W. Grimes if ((rt->rt_flags & RTF_HOST) == 0 && 183df8bae1dSRodney W. Grimes SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 184df8bae1dSRodney W. Grimes rt->rt_flags |= RTF_CLONING; 185df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_CLONING) { 186df8bae1dSRodney W. Grimes /* 187df8bae1dSRodney W. Grimes * Case 1: This route should come from a route to iface. 188df8bae1dSRodney W. Grimes */ 189df8bae1dSRodney W. Grimes rt_setgate(rt, rt_key(rt), 190df8bae1dSRodney W. Grimes (struct sockaddr *)&null_sdl); 191df8bae1dSRodney W. Grimes gate = rt->rt_gateway; 192df8bae1dSRodney W. Grimes SDL(gate)->sdl_type = rt->rt_ifp->if_type; 193df8bae1dSRodney W. Grimes SDL(gate)->sdl_index = rt->rt_ifp->if_index; 194227ee8a1SPoul-Henning Kamp rt->rt_expire = time_second; 195df8bae1dSRodney W. Grimes break; 196df8bae1dSRodney W. Grimes } 197df8bae1dSRodney W. Grimes /* Announce a new entry if requested. */ 198df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_ANNOUNCE) 199df8bae1dSRodney W. Grimes arprequest((struct arpcom *)rt->rt_ifp, 200ed7509acSJulian Elischer &SIN(rt_key(rt))->sin_addr, 201ed7509acSJulian Elischer &SIN(rt_key(rt))->sin_addr, 202df8bae1dSRodney W. Grimes (u_char *)LLADDR(SDL(gate))); 203df8bae1dSRodney W. Grimes /*FALLTHROUGH*/ 204df8bae1dSRodney W. Grimes case RTM_RESOLVE: 205df8bae1dSRodney W. Grimes if (gate->sa_family != AF_LINK || 206df8bae1dSRodney W. Grimes gate->sa_len < sizeof(null_sdl)) { 20738aa9fc3SDavid Greenman log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n"); 208df8bae1dSRodney W. Grimes break; 209df8bae1dSRodney W. Grimes } 210df8bae1dSRodney W. Grimes SDL(gate)->sdl_type = rt->rt_ifp->if_type; 211df8bae1dSRodney W. Grimes SDL(gate)->sdl_index = rt->rt_ifp->if_index; 212df8bae1dSRodney W. Grimes if (la != 0) 213df8bae1dSRodney W. Grimes break; /* This happens on a route change */ 214df8bae1dSRodney W. Grimes /* 215df8bae1dSRodney W. Grimes * Case 2: This route may come from cloning, or a manual route 216df8bae1dSRodney W. Grimes * add with a LL address. 217df8bae1dSRodney W. Grimes */ 218df8bae1dSRodney W. Grimes R_Malloc(la, struct llinfo_arp *, sizeof(*la)); 219df8bae1dSRodney W. Grimes rt->rt_llinfo = (caddr_t)la; 220df8bae1dSRodney W. Grimes if (la == 0) { 221df8bae1dSRodney W. Grimes log(LOG_DEBUG, "arp_rtrequest: malloc failed\n"); 222df8bae1dSRodney W. Grimes break; 223df8bae1dSRodney W. Grimes } 224df8bae1dSRodney W. Grimes arp_inuse++, arp_allocated++; 225df8bae1dSRodney W. Grimes Bzero(la, sizeof(*la)); 226df8bae1dSRodney W. Grimes la->la_rt = rt; 227df8bae1dSRodney W. Grimes rt->rt_flags |= RTF_LLINFO; 22866800f57SGarrett Wollman LIST_INSERT_HEAD(&llinfo_arp, la, la_le); 229cbb0b46aSGarrett Wollman 2301d5e9e22SEivind Eklund #ifdef INET 231cbb0b46aSGarrett Wollman /* 232cbb0b46aSGarrett Wollman * This keeps the multicast addresses from showing up 233cbb0b46aSGarrett Wollman * in `arp -a' listings as unresolved. It's not actually 234cbb0b46aSGarrett Wollman * functional. Then the same for broadcast. 235cbb0b46aSGarrett Wollman */ 236cbb0b46aSGarrett Wollman if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) { 237cbb0b46aSGarrett Wollman ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr, 238cbb0b46aSGarrett Wollman LLADDR(SDL(gate))); 239cbb0b46aSGarrett Wollman SDL(gate)->sdl_alen = 6; 24051a109a1SPeter Wemm rt->rt_expire = 0; 241cbb0b46aSGarrett Wollman } 242cbb0b46aSGarrett Wollman if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) { 243cbb0b46aSGarrett Wollman memcpy(LLADDR(SDL(gate)), etherbroadcastaddr, 6); 244cbb0b46aSGarrett Wollman SDL(gate)->sdl_alen = 6; 24551a109a1SPeter Wemm rt->rt_expire = 0; 246cbb0b46aSGarrett Wollman } 2471d5e9e22SEivind Eklund #endif 248cbb0b46aSGarrett Wollman 249df8bae1dSRodney W. Grimes if (SIN(rt_key(rt))->sin_addr.s_addr == 250df8bae1dSRodney W. Grimes (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) { 251df8bae1dSRodney W. Grimes /* 252df8bae1dSRodney W. Grimes * This test used to be 253df8bae1dSRodney W. Grimes * if (loif.if_flags & IFF_UP) 254df8bae1dSRodney W. Grimes * It allowed local traffic to be forced 255df8bae1dSRodney W. Grimes * through the hardware by configuring the loopback down. 256df8bae1dSRodney W. Grimes * However, it causes problems during network configuration 257df8bae1dSRodney W. Grimes * for boards that can't receive packets they send. 258df8bae1dSRodney W. Grimes * It is now necessary to clear "useloopback" and remove 259df8bae1dSRodney W. Grimes * the route to force traffic out to the hardware. 260df8bae1dSRodney W. Grimes */ 261df8bae1dSRodney W. Grimes rt->rt_expire = 0; 262df8bae1dSRodney W. Grimes Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr, 263df8bae1dSRodney W. Grimes LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6); 264df8bae1dSRodney W. Grimes if (useloopback) 265f5fea3ddSPaul Traina rt->rt_ifp = loif; 266df8bae1dSRodney W. Grimes 267df8bae1dSRodney W. Grimes } 268df8bae1dSRodney W. Grimes break; 269df8bae1dSRodney W. Grimes 270df8bae1dSRodney W. Grimes case RTM_DELETE: 271df8bae1dSRodney W. Grimes if (la == 0) 272df8bae1dSRodney W. Grimes break; 273df8bae1dSRodney W. Grimes arp_inuse--; 27466800f57SGarrett Wollman LIST_REMOVE(la, la_le); 275df8bae1dSRodney W. Grimes rt->rt_llinfo = 0; 276df8bae1dSRodney W. Grimes rt->rt_flags &= ~RTF_LLINFO; 277df8bae1dSRodney W. Grimes if (la->la_hold) 278df8bae1dSRodney W. Grimes m_freem(la->la_hold); 279df8bae1dSRodney W. Grimes Free((caddr_t)la); 280df8bae1dSRodney W. Grimes } 281df8bae1dSRodney W. Grimes } 282df8bae1dSRodney W. Grimes 283df8bae1dSRodney W. Grimes /* 284df8bae1dSRodney W. Grimes * Broadcast an ARP request. Caller specifies: 285df8bae1dSRodney W. Grimes * - arp header source ip address 286df8bae1dSRodney W. Grimes * - arp header target ip address 287df8bae1dSRodney W. Grimes * - arp header source ethernet address 288df8bae1dSRodney W. Grimes */ 289df8bae1dSRodney W. Grimes static void 290df8bae1dSRodney W. Grimes arprequest(ac, sip, tip, enaddr) 291df8bae1dSRodney W. Grimes register struct arpcom *ac; 292ed7509acSJulian Elischer register struct in_addr *sip, *tip; 293df8bae1dSRodney W. Grimes register u_char *enaddr; 294df8bae1dSRodney W. Grimes { 295df8bae1dSRodney W. Grimes register struct mbuf *m; 296df8bae1dSRodney W. Grimes register struct ether_header *eh; 297df8bae1dSRodney W. Grimes register struct ether_arp *ea; 298df8bae1dSRodney W. Grimes struct sockaddr sa; 299b149dd6cSLarry Lile static u_char llcx[] = { 0x82, 0x40, LLC_SNAP_LSAP, LLC_SNAP_LSAP, 300b149dd6cSLarry Lile LLC_UI, 0x00, 0x00, 0x00, 0x08, 0x06 }; 301df8bae1dSRodney W. Grimes 302df8bae1dSRodney W. Grimes if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 303df8bae1dSRodney W. Grimes return; 304fda82fc2SJulian Elischer m->m_pkthdr.rcvif = (struct ifnet *)0; 305fda82fc2SJulian Elischer switch (ac->ac_if.if_type) { 306fda82fc2SJulian Elischer case IFT_ISO88025: 307b149dd6cSLarry Lile m->m_len = sizeof(*ea) + sizeof(llcx); 308b149dd6cSLarry Lile m->m_pkthdr.len = sizeof(*ea) + sizeof(llcx); 309b149dd6cSLarry Lile MH_ALIGN(m, sizeof(*ea) + sizeof(llcx)); 310b149dd6cSLarry Lile (void)memcpy(mtod(m, caddr_t), llcx, sizeof(llcx)); 311fda82fc2SJulian Elischer (void)memcpy(sa.sa_data, etherbroadcastaddr, 6); 312fda82fc2SJulian Elischer (void)memcpy(sa.sa_data + 6, enaddr, 6); 313b149dd6cSLarry Lile sa.sa_data[6] |= TR_RII; 314b149dd6cSLarry Lile sa.sa_data[12] = TR_AC; 315b149dd6cSLarry Lile sa.sa_data[13] = TR_LLC_FRAME; 316b149dd6cSLarry Lile ea = (struct ether_arp *)(mtod(m, char *) + sizeof(llcx)); 317fda82fc2SJulian Elischer bzero((caddr_t)ea, sizeof (*ea)); 318722012ccSJulian Elischer ea->arp_hrd = htons(ARPHRD_IEEE802); 319fda82fc2SJulian Elischer break; 320f9083fdbSLarry Lile case IFT_FDDI: 321f9083fdbSLarry Lile case IFT_ETHER: 322f9083fdbSLarry Lile /* 323f9083fdbSLarry Lile * This may not be correct for types not explicitly 324f9083fdbSLarry Lile * listed, but this is our best guess 325f9083fdbSLarry Lile */ 326fda82fc2SJulian Elischer default: 327f9083fdbSLarry Lile m->m_len = sizeof(*ea); 328f9083fdbSLarry Lile m->m_pkthdr.len = sizeof(*ea); 329f9083fdbSLarry Lile MH_ALIGN(m, sizeof(*ea)); 330f9083fdbSLarry Lile ea = mtod(m, struct ether_arp *); 331f9083fdbSLarry Lile eh = (struct ether_header *)sa.sa_data; 332f9083fdbSLarry Lile bzero((caddr_t)ea, sizeof (*ea)); 333f9083fdbSLarry Lile /* if_output will not swap */ 334f9083fdbSLarry Lile eh->ether_type = htons(ETHERTYPE_ARP); 335f9083fdbSLarry Lile (void)memcpy(eh->ether_dhost, etherbroadcastaddr, 336f9083fdbSLarry Lile sizeof(eh->ether_dhost)); 337f9083fdbSLarry Lile ea->arp_hrd = htons(ARPHRD_ETHER); 338f9083fdbSLarry Lile break; 339fda82fc2SJulian Elischer } 340df8bae1dSRodney W. Grimes ea->arp_pro = htons(ETHERTYPE_IP); 341df8bae1dSRodney W. Grimes ea->arp_hln = sizeof(ea->arp_sha); /* hardware address length */ 342df8bae1dSRodney W. Grimes ea->arp_pln = sizeof(ea->arp_spa); /* protocol address length */ 343df8bae1dSRodney W. Grimes ea->arp_op = htons(ARPOP_REQUEST); 34494a5d9b6SDavid Greenman (void)memcpy(ea->arp_sha, enaddr, sizeof(ea->arp_sha)); 34594a5d9b6SDavid Greenman (void)memcpy(ea->arp_spa, sip, sizeof(ea->arp_spa)); 34694a5d9b6SDavid Greenman (void)memcpy(ea->arp_tpa, tip, sizeof(ea->arp_tpa)); 347df8bae1dSRodney W. Grimes sa.sa_family = AF_UNSPEC; 348df8bae1dSRodney W. Grimes sa.sa_len = sizeof(sa); 349df8bae1dSRodney W. Grimes (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0); 350df8bae1dSRodney W. Grimes } 351df8bae1dSRodney W. Grimes 352df8bae1dSRodney W. Grimes /* 353df8bae1dSRodney W. Grimes * Resolve an IP address into an ethernet address. If success, 354df8bae1dSRodney W. Grimes * desten is filled in. If there is no entry in arptab, 355df8bae1dSRodney W. Grimes * set one up and broadcast a request for the IP address. 356df8bae1dSRodney W. Grimes * Hold onto this mbuf and resend it once the address 357df8bae1dSRodney W. Grimes * is finally resolved. A return value of 1 indicates 358df8bae1dSRodney W. Grimes * that desten has been filled in and the packet should be sent 359df8bae1dSRodney W. Grimes * normally; a 0 return indicates that the packet has been 360df8bae1dSRodney W. Grimes * taken over here, either now or for later transmission. 361df8bae1dSRodney W. Grimes */ 362df8bae1dSRodney W. Grimes int 3635df72964SGarrett Wollman arpresolve(ac, rt, m, dst, desten, rt0) 364df8bae1dSRodney W. Grimes register struct arpcom *ac; 365df8bae1dSRodney W. Grimes register struct rtentry *rt; 366df8bae1dSRodney W. Grimes struct mbuf *m; 367df8bae1dSRodney W. Grimes register struct sockaddr *dst; 368df8bae1dSRodney W. Grimes register u_char *desten; 3695df72964SGarrett Wollman struct rtentry *rt0; 370df8bae1dSRodney W. Grimes { 37108aadfbbSJonathan Lemon struct llinfo_arp *la = 0; 372df8bae1dSRodney W. Grimes struct sockaddr_dl *sdl; 373df8bae1dSRodney W. Grimes 374df8bae1dSRodney W. Grimes if (m->m_flags & M_BCAST) { /* broadcast */ 37594a5d9b6SDavid Greenman (void)memcpy(desten, etherbroadcastaddr, sizeof(etherbroadcastaddr)); 376df8bae1dSRodney W. Grimes return (1); 377df8bae1dSRodney W. Grimes } 378df8bae1dSRodney W. Grimes if (m->m_flags & M_MCAST) { /* multicast */ 379df8bae1dSRodney W. Grimes ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 380df8bae1dSRodney W. Grimes return(1); 381df8bae1dSRodney W. Grimes } 382df8bae1dSRodney W. Grimes if (rt) 383df8bae1dSRodney W. Grimes la = (struct llinfo_arp *)rt->rt_llinfo; 384e7bc5f27SBill Fenner if (la == 0) { 385623ae52eSPoul-Henning Kamp la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0); 386623ae52eSPoul-Henning Kamp if (la) 387df8bae1dSRodney W. Grimes rt = la->la_rt; 388df8bae1dSRodney W. Grimes } 389df8bae1dSRodney W. Grimes if (la == 0 || rt == 0) { 390245086a0SPoul-Henning Kamp log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n", 391245086a0SPoul-Henning Kamp inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "", 392245086a0SPoul-Henning Kamp rt ? "rt" : ""); 393df8bae1dSRodney W. Grimes m_freem(m); 394df8bae1dSRodney W. Grimes return (0); 395df8bae1dSRodney W. Grimes } 396df8bae1dSRodney W. Grimes sdl = SDL(rt->rt_gateway); 397df8bae1dSRodney W. Grimes /* 398df8bae1dSRodney W. Grimes * Check the address family and length is valid, the address 399df8bae1dSRodney W. Grimes * is resolved; otherwise, try to resolve. 400df8bae1dSRodney W. Grimes */ 401227ee8a1SPoul-Henning Kamp if ((rt->rt_expire == 0 || rt->rt_expire > time_second) && 402df8bae1dSRodney W. Grimes sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { 4030453d3cbSBruce Evans bcopy(LLADDR(sdl), desten, sdl->sdl_alen); 404df8bae1dSRodney W. Grimes return 1; 405df8bae1dSRodney W. Grimes } 406df8bae1dSRodney W. Grimes /* 40708aadfbbSJonathan Lemon * If ARP is disabled on this interface, stop. 40808aadfbbSJonathan Lemon * XXX 40908aadfbbSJonathan Lemon * Probably should not allocate empty llinfo struct if we are 41008aadfbbSJonathan Lemon * not going to be sending out an arp request. 41108aadfbbSJonathan Lemon */ 41208aadfbbSJonathan Lemon if (ac->ac_if.if_flags & IFF_NOARP) 41308aadfbbSJonathan Lemon return (0); 41408aadfbbSJonathan Lemon /* 415df8bae1dSRodney W. Grimes * There is an arptab entry, but no ethernet address 416df8bae1dSRodney W. Grimes * response yet. Replace the held mbuf with this 417df8bae1dSRodney W. Grimes * latest one. 418df8bae1dSRodney W. Grimes */ 419df8bae1dSRodney W. Grimes if (la->la_hold) 420df8bae1dSRodney W. Grimes m_freem(la->la_hold); 421df8bae1dSRodney W. Grimes la->la_hold = m; 422df8bae1dSRodney W. Grimes if (rt->rt_expire) { 423df8bae1dSRodney W. Grimes rt->rt_flags &= ~RTF_REJECT; 424227ee8a1SPoul-Henning Kamp if (la->la_asked == 0 || rt->rt_expire != time_second) { 425227ee8a1SPoul-Henning Kamp rt->rt_expire = time_second; 426df8bae1dSRodney W. Grimes if (la->la_asked++ < arp_maxtries) 427203f0c07SBill Fenner arprequest(ac, 428ed7509acSJulian Elischer &SIN(rt->rt_ifa->ifa_addr)->sin_addr, 429ed7509acSJulian Elischer &SIN(dst)->sin_addr, ac->ac_enaddr); 430df8bae1dSRodney W. Grimes else { 431df8bae1dSRodney W. Grimes rt->rt_flags |= RTF_REJECT; 432df8bae1dSRodney W. Grimes rt->rt_expire += arpt_down; 433df8bae1dSRodney W. Grimes la->la_asked = 0; 434df8bae1dSRodney W. Grimes } 435df8bae1dSRodney W. Grimes 436df8bae1dSRodney W. Grimes } 437df8bae1dSRodney W. Grimes } 438df8bae1dSRodney W. Grimes return (0); 439df8bae1dSRodney W. Grimes } 440df8bae1dSRodney W. Grimes 441df8bae1dSRodney W. Grimes /* 442df8bae1dSRodney W. Grimes * Common length and type checks are done here, 443df8bae1dSRodney W. Grimes * then the protocol-specific routine is called. 444df8bae1dSRodney W. Grimes */ 445885f1aa4SPoul-Henning Kamp static void 446c5a1016bSBruce Evans arpintr() 447df8bae1dSRodney W. Grimes { 448732f6a43SWes Peters register struct mbuf *m; 449df8bae1dSRodney W. Grimes register struct arphdr *ar; 450732f6a43SWes Peters int s; 451df8bae1dSRodney W. Grimes 452df8bae1dSRodney W. Grimes while (arpintrq.ifq_head) { 453df8bae1dSRodney W. Grimes s = splimp(); 454df8bae1dSRodney W. Grimes IF_DEQUEUE(&arpintrq, m); 455df8bae1dSRodney W. Grimes splx(s); 456df8bae1dSRodney W. Grimes if (m == 0 || (m->m_flags & M_PKTHDR) == 0) 457df8bae1dSRodney W. Grimes panic("arpintr"); 45876ec7b2fSRobert Watson 45976ec7b2fSRobert Watson if (m->m_len < sizeof(struct arphdr) && 46084365e2bSMatthew Dillon ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) { 461e44d6283SJoerg Wunsch log(LOG_ERR, "arp: runt packet -- m_pullup failed\n"); 46276ec7b2fSRobert Watson continue; 46376ec7b2fSRobert Watson } 46476ec7b2fSRobert Watson ar = mtod(m, struct arphdr *); 46576ec7b2fSRobert Watson 46676ec7b2fSRobert Watson if (ntohs(ar->ar_hrd) != ARPHRD_ETHER 46776ec7b2fSRobert Watson && ntohs(ar->ar_hrd) != ARPHRD_IEEE802) { 46876ec7b2fSRobert Watson log(LOG_ERR, 469e44d6283SJoerg Wunsch "arp: unknown hardware address format (0x%2D)\n", 47076ec7b2fSRobert Watson (unsigned char *)&ar->ar_hrd, ""); 47176ec7b2fSRobert Watson m_freem(m); 47276ec7b2fSRobert Watson continue; 47376ec7b2fSRobert Watson } 47476ec7b2fSRobert Watson 475732f6a43SWes Peters if (m->m_pkthdr.len < sizeof(struct arphdr) + 2 * ar->ar_hln 47676ec7b2fSRobert Watson + 2 * ar->ar_pln) { 477f72d9d83SJoerg Wunsch log(LOG_ERR, "arp: runt packet\n"); 47876ec7b2fSRobert Watson m_freem(m); 47976ec7b2fSRobert Watson continue; 48076ec7b2fSRobert Watson } 481df8bae1dSRodney W. Grimes 482df8bae1dSRodney W. Grimes switch (ntohs(ar->ar_pro)) { 4831d5e9e22SEivind Eklund #ifdef INET 484df8bae1dSRodney W. Grimes case ETHERTYPE_IP: 485df8bae1dSRodney W. Grimes in_arpinput(m); 486df8bae1dSRodney W. Grimes continue; 4871d5e9e22SEivind Eklund #endif 488df8bae1dSRodney W. Grimes } 489df8bae1dSRodney W. Grimes m_freem(m); 490df8bae1dSRodney W. Grimes } 491df8bae1dSRodney W. Grimes } 492df8bae1dSRodney W. Grimes 4931d5e9e22SEivind Eklund #ifdef INET 494df8bae1dSRodney W. Grimes /* 495df8bae1dSRodney W. Grimes * ARP for Internet protocols on 10 Mb/s Ethernet. 496df8bae1dSRodney W. Grimes * Algorithm is that given in RFC 826. 497df8bae1dSRodney W. Grimes * In addition, a sanity check is performed on the sender 498df8bae1dSRodney W. Grimes * protocol address, to catch impersonators. 499df8bae1dSRodney W. Grimes * We no longer handle negotiations for use of trailer protocol: 500df8bae1dSRodney W. Grimes * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 501df8bae1dSRodney W. Grimes * along with IP replies if we wanted trailers sent to us, 502df8bae1dSRodney W. Grimes * and also sent them in response to IP replies. 503df8bae1dSRodney W. Grimes * This allowed either end to announce the desire to receive 504df8bae1dSRodney W. Grimes * trailer packets. 505df8bae1dSRodney W. Grimes * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 506df8bae1dSRodney W. Grimes * but formerly didn't normally send requests. 507df8bae1dSRodney W. Grimes */ 5083269187dSAlfred Perlstein static int log_arp_wrong_iface = 1; 509e3d123d6SAlfred Perlstein static int log_arp_movements = 1; 5103269187dSAlfred Perlstein 5113269187dSAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW, 5123269187dSAlfred Perlstein &log_arp_wrong_iface, 0, 5133269187dSAlfred Perlstein "log arp packets arriving on the wrong interface"); 514e3d123d6SAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW, 515e3d123d6SAlfred Perlstein &log_arp_movements, 0, 51675ce3221SAlfred Perlstein "log arp replies from MACs different than the one in the cache"); 517e3d123d6SAlfred Perlstein 5183269187dSAlfred Perlstein 519df8bae1dSRodney W. Grimes static void 520df8bae1dSRodney W. Grimes in_arpinput(m) 521df8bae1dSRodney W. Grimes struct mbuf *m; 522df8bae1dSRodney W. Grimes { 523df8bae1dSRodney W. Grimes register struct ether_arp *ea; 524df8bae1dSRodney W. Grimes register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif; 525df8bae1dSRodney W. Grimes struct ether_header *eh; 526fda82fc2SJulian Elischer struct iso88025_header *th = (struct iso88025_header *)0; 527df8bae1dSRodney W. Grimes register struct llinfo_arp *la = 0; 528df8bae1dSRodney W. Grimes register struct rtentry *rt; 529df8bae1dSRodney W. Grimes struct in_ifaddr *ia, *maybe_ia = 0; 530df8bae1dSRodney W. Grimes struct sockaddr_dl *sdl; 531df8bae1dSRodney W. Grimes struct sockaddr sa; 532df8bae1dSRodney W. Grimes struct in_addr isaddr, itaddr, myaddr; 533b149dd6cSLarry Lile int op, rif_len; 534df8bae1dSRodney W. Grimes 5354cbc8ad1SYaroslav Tykhiy if (m->m_len < sizeof(struct ether_arp) && 5364cbc8ad1SYaroslav Tykhiy (m = m_pullup(m, sizeof(struct ether_arp))) == NULL) { 5374cbc8ad1SYaroslav Tykhiy log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n"); 5384cbc8ad1SYaroslav Tykhiy return; 5394cbc8ad1SYaroslav Tykhiy } 5404cbc8ad1SYaroslav Tykhiy 541df8bae1dSRodney W. Grimes ea = mtod(m, struct ether_arp *); 542df8bae1dSRodney W. Grimes op = ntohs(ea->arp_op); 54394a5d9b6SDavid Greenman (void)memcpy(&isaddr, ea->arp_spa, sizeof (isaddr)); 54494a5d9b6SDavid Greenman (void)memcpy(&itaddr, ea->arp_tpa, sizeof (itaddr)); 54541d2ba5eSJulian Elischer TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { 546b715f178SLuigi Rizzo /* 547b715f178SLuigi Rizzo * For a bridge, we want to check the address irrespective 548b715f178SLuigi Rizzo * of the receive interface. (This will change slightly 549b715f178SLuigi Rizzo * when we have clusters of interfaces). 550b715f178SLuigi Rizzo */ 55141d2ba5eSJulian Elischer #ifdef BRIDGE 55241d2ba5eSJulian Elischer #define BRIDGE_TEST (do_bridge) 553b715f178SLuigi Rizzo #else 55441d2ba5eSJulian Elischer #define BRIDGE_TEST (0) /* cc will optimise the test away */ 555b715f178SLuigi Rizzo #endif 55641d2ba5eSJulian Elischer if ((BRIDGE_TEST) || (ia->ia_ifp == &ac->ac_if)) { 557df8bae1dSRodney W. Grimes maybe_ia = ia; 558df8bae1dSRodney W. Grimes if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) || 55941d2ba5eSJulian Elischer (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)) { 560df8bae1dSRodney W. Grimes break; 561df8bae1dSRodney W. Grimes } 562c8f8e9c1SJulian Elischer } 56341d2ba5eSJulian Elischer } 564885f1aa4SPoul-Henning Kamp if (maybe_ia == 0) { 565885f1aa4SPoul-Henning Kamp m_freem(m); 566885f1aa4SPoul-Henning Kamp return; 567885f1aa4SPoul-Henning Kamp } 568df8bae1dSRodney W. Grimes myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr; 569df8bae1dSRodney W. Grimes if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr, 570885f1aa4SPoul-Henning Kamp sizeof (ea->arp_sha))) { 571885f1aa4SPoul-Henning Kamp m_freem(m); /* it's from me, ignore it. */ 572885f1aa4SPoul-Henning Kamp return; 573885f1aa4SPoul-Henning Kamp } 574df8bae1dSRodney W. Grimes if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr, 575df8bae1dSRodney W. Grimes sizeof (ea->arp_sha))) { 576df8bae1dSRodney W. Grimes log(LOG_ERR, 577ac234f93SGarrett Wollman "arp: ether address is broadcast for IP address %s!\n", 578ef0cdf33SGarrett Wollman inet_ntoa(isaddr)); 579885f1aa4SPoul-Henning Kamp m_freem(m); 580885f1aa4SPoul-Henning Kamp return; 581df8bae1dSRodney W. Grimes } 582df8bae1dSRodney W. Grimes if (isaddr.s_addr == myaddr.s_addr) { 583df8bae1dSRodney W. Grimes log(LOG_ERR, 584254de4eaSBill Fenner "arp: %6D is using my IP address %s!\n", 585254de4eaSBill Fenner ea->arp_sha, ":", inet_ntoa(isaddr)); 586df8bae1dSRodney W. Grimes itaddr = myaddr; 587df8bae1dSRodney W. Grimes goto reply; 588df8bae1dSRodney W. Grimes } 589df8bae1dSRodney W. Grimes la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0); 590df8bae1dSRodney W. Grimes if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) { 5917e1cd0d2SLuigi Rizzo /* the following is not an error when doing bridging */ 5927e1cd0d2SLuigi Rizzo if (!BRIDGE_TEST && rt->rt_ifp != &ac->ac_if) { 5933269187dSAlfred Perlstein if (log_arp_wrong_iface) 594dd9b6ddeSBill Fenner log(LOG_ERR, "arp: %s is on %s%d but got reply from %6D on %s%d\n", 595dd9b6ddeSBill Fenner inet_ntoa(isaddr), 596dd9b6ddeSBill Fenner rt->rt_ifp->if_name, rt->rt_ifp->if_unit, 597dd9b6ddeSBill Fenner ea->arp_sha, ":", 598dd9b6ddeSBill Fenner ac->ac_if.if_name, ac->ac_if.if_unit); 599dd9b6ddeSBill Fenner goto reply; 600dd9b6ddeSBill Fenner } 601df8bae1dSRodney W. Grimes if (sdl->sdl_alen && 602dfd5dee1SPeter Wemm bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen)) { 603e3d123d6SAlfred Perlstein if (rt->rt_expire) { 604e3d123d6SAlfred Perlstein if (log_arp_movements) 605dd9b6ddeSBill Fenner log(LOG_INFO, "arp: %s moved from %6D to %6D on %s%d\n", 606254de4eaSBill Fenner inet_ntoa(isaddr), (u_char *)LLADDR(sdl), ":", 607dd9b6ddeSBill Fenner ea->arp_sha, ":", 608dd9b6ddeSBill Fenner ac->ac_if.if_name, ac->ac_if.if_unit); 609e3d123d6SAlfred Perlstein } else { 610dd9b6ddeSBill Fenner log(LOG_ERR, 6110b757633SSheldon Hearn "arp: %6D attempts to modify permanent entry for %s on %s%d\n", 612dd9b6ddeSBill Fenner ea->arp_sha, ":", inet_ntoa(isaddr), 613dd9b6ddeSBill Fenner ac->ac_if.if_name, ac->ac_if.if_unit); 614dd9b6ddeSBill Fenner goto reply; 615dd9b6ddeSBill Fenner } 616dfd5dee1SPeter Wemm } 61794a5d9b6SDavid Greenman (void)memcpy(LLADDR(sdl), ea->arp_sha, sizeof(ea->arp_sha)); 61894a5d9b6SDavid Greenman sdl->sdl_alen = sizeof(ea->arp_sha); 619b149dd6cSLarry Lile sdl->sdl_rcf = (u_short)0; 620243c4c6dSEivind Eklund /* 621243c4c6dSEivind Eklund * If we receive an arp from a token-ring station over 622243c4c6dSEivind Eklund * a token-ring nic then try to save the source 623243c4c6dSEivind Eklund * routing info. 624243c4c6dSEivind Eklund */ 625243c4c6dSEivind Eklund if (ac->ac_if.if_type == IFT_ISO88025) { 626fda82fc2SJulian Elischer th = (struct iso88025_header *)m->m_pkthdr.header; 627b149dd6cSLarry Lile rif_len = TR_RCF_RIFLEN(th->rcf); 628b149dd6cSLarry Lile if ((th->iso88025_shost[0] & TR_RII) && 629b149dd6cSLarry Lile (rif_len > 2)) { 630b149dd6cSLarry Lile sdl->sdl_rcf = th->rcf; 631b149dd6cSLarry Lile sdl->sdl_rcf ^= htons(TR_RCF_DIR); 632b149dd6cSLarry Lile memcpy(sdl->sdl_route, th->rd, rif_len - 2); 633b149dd6cSLarry Lile sdl->sdl_rcf &= ~htons(TR_RCF_BCST_MASK); 634f9083fdbSLarry Lile /* 635f9083fdbSLarry Lile * Set up source routing information for 636f9083fdbSLarry Lile * reply packet (XXX) 637f9083fdbSLarry Lile */ 638b149dd6cSLarry Lile m->m_data -= rif_len; 639b149dd6cSLarry Lile m->m_len += rif_len; 640b149dd6cSLarry Lile m->m_pkthdr.len += rif_len; 641fda82fc2SJulian Elischer } else { 642b149dd6cSLarry Lile th->iso88025_shost[0] &= ~TR_RII; 643fcf11853SLarry Lile } 644fda82fc2SJulian Elischer m->m_data -= 8; 645fda82fc2SJulian Elischer m->m_len += 8; 646fcf11853SLarry Lile m->m_pkthdr.len += 8; 647fda82fc2SJulian Elischer th->rcf = sdl->sdl_rcf; 648243c4c6dSEivind Eklund } else { 649b149dd6cSLarry Lile sdl->sdl_rcf = (u_short)0; 650fda82fc2SJulian Elischer } 651df8bae1dSRodney W. Grimes if (rt->rt_expire) 652227ee8a1SPoul-Henning Kamp rt->rt_expire = time_second + arpt_keep; 653df8bae1dSRodney W. Grimes rt->rt_flags &= ~RTF_REJECT; 654df8bae1dSRodney W. Grimes la->la_asked = 0; 655df8bae1dSRodney W. Grimes if (la->la_hold) { 656df8bae1dSRodney W. Grimes (*ac->ac_if.if_output)(&ac->ac_if, la->la_hold, 657df8bae1dSRodney W. Grimes rt_key(rt), rt); 658df8bae1dSRodney W. Grimes la->la_hold = 0; 659df8bae1dSRodney W. Grimes } 660df8bae1dSRodney W. Grimes } 661df8bae1dSRodney W. Grimes reply: 662df8bae1dSRodney W. Grimes if (op != ARPOP_REQUEST) { 663df8bae1dSRodney W. Grimes m_freem(m); 664df8bae1dSRodney W. Grimes return; 665df8bae1dSRodney W. Grimes } 666df8bae1dSRodney W. Grimes if (itaddr.s_addr == myaddr.s_addr) { 667df8bae1dSRodney W. Grimes /* I am the target */ 66894a5d9b6SDavid Greenman (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha)); 66994a5d9b6SDavid Greenman (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha)); 670df8bae1dSRodney W. Grimes } else { 671df8bae1dSRodney W. Grimes la = arplookup(itaddr.s_addr, 0, SIN_PROXY); 67228e82295SGarrett Wollman if (la == NULL) { 67328e82295SGarrett Wollman struct sockaddr_in sin; 67428e82295SGarrett Wollman 675885f1aa4SPoul-Henning Kamp if (!arp_proxyall) { 676885f1aa4SPoul-Henning Kamp m_freem(m); 677885f1aa4SPoul-Henning Kamp return; 678885f1aa4SPoul-Henning Kamp } 67928e82295SGarrett Wollman 68028e82295SGarrett Wollman bzero(&sin, sizeof sin); 68128e82295SGarrett Wollman sin.sin_family = AF_INET; 68228e82295SGarrett Wollman sin.sin_len = sizeof sin; 68328e82295SGarrett Wollman sin.sin_addr = itaddr; 68428e82295SGarrett Wollman 68531246bc2SGarrett Wollman rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 686885f1aa4SPoul-Henning Kamp if (!rt) { 687885f1aa4SPoul-Henning Kamp m_freem(m); 688885f1aa4SPoul-Henning Kamp return; 689885f1aa4SPoul-Henning Kamp } 69028e82295SGarrett Wollman /* 69128e82295SGarrett Wollman * Don't send proxies for nodes on the same interface 69228e82295SGarrett Wollman * as this one came out of, or we'll get into a fight 69328e82295SGarrett Wollman * over who claims what Ether address. 69428e82295SGarrett Wollman */ 69528e82295SGarrett Wollman if (rt->rt_ifp == &ac->ac_if) { 69628e82295SGarrett Wollman rtfree(rt); 697885f1aa4SPoul-Henning Kamp m_freem(m); 698885f1aa4SPoul-Henning Kamp return; 69928e82295SGarrett Wollman } 70094a5d9b6SDavid Greenman (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha)); 70194a5d9b6SDavid Greenman (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha)); 70228e82295SGarrett Wollman rtfree(rt); 703cc728227SDavid Malone 704cc728227SDavid Malone /* 705cc728227SDavid Malone * Also check that the node which sent the ARP packet 706cc728227SDavid Malone * is on the the interface we expect it to be on. This 707cc728227SDavid Malone * avoids ARP chaos if an interface is connected to the 708cc728227SDavid Malone * wrong network. 709cc728227SDavid Malone */ 710cc728227SDavid Malone sin.sin_addr = isaddr; 711cc728227SDavid Malone 712cc728227SDavid Malone rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 713cc728227SDavid Malone if (!rt) { 714cc728227SDavid Malone m_freem(m); 715cc728227SDavid Malone return; 716cc728227SDavid Malone } 717cc728227SDavid Malone if (rt->rt_ifp != &ac->ac_if) { 718cc728227SDavid Malone log(LOG_INFO, "arp_proxy: ignoring request" 719cc728227SDavid Malone " from %s via %s%d, expecting %s%d\n", 720cc728227SDavid Malone inet_ntoa(isaddr), ac->ac_if.if_name, 721cc728227SDavid Malone ac->ac_if.if_unit, rt->rt_ifp->if_name, 722cc728227SDavid Malone rt->rt_ifp->if_unit); 723cc728227SDavid Malone rtfree(rt); 724cc728227SDavid Malone m_freem(m); 725cc728227SDavid Malone return; 726cc728227SDavid Malone } 727cc728227SDavid Malone rtfree(rt); 728cc728227SDavid Malone 729ac234f93SGarrett Wollman #ifdef DEBUG_PROXY 730ac234f93SGarrett Wollman printf("arp: proxying for %s\n", 731ef0cdf33SGarrett Wollman inet_ntoa(itaddr)); 732ac234f93SGarrett Wollman #endif 73328e82295SGarrett Wollman } else { 734df8bae1dSRodney W. Grimes rt = la->la_rt; 73594a5d9b6SDavid Greenman (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha)); 736df8bae1dSRodney W. Grimes sdl = SDL(rt->rt_gateway); 73794a5d9b6SDavid Greenman (void)memcpy(ea->arp_sha, LLADDR(sdl), sizeof(ea->arp_sha)); 73828e82295SGarrett Wollman } 739df8bae1dSRodney W. Grimes } 740df8bae1dSRodney W. Grimes 74194a5d9b6SDavid Greenman (void)memcpy(ea->arp_tpa, ea->arp_spa, sizeof(ea->arp_spa)); 74294a5d9b6SDavid Greenman (void)memcpy(ea->arp_spa, &itaddr, sizeof(ea->arp_spa)); 743df8bae1dSRodney W. Grimes ea->arp_op = htons(ARPOP_REPLY); 744df8bae1dSRodney W. Grimes ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 745243c4c6dSEivind Eklund switch (ac->ac_if.if_type) { 746243c4c6dSEivind Eklund case IFT_ISO88025: 747fda82fc2SJulian Elischer /* Re-arrange the source/dest address */ 748f9083fdbSLarry Lile memcpy(th->iso88025_dhost, th->iso88025_shost, 749f9083fdbSLarry Lile sizeof(th->iso88025_dhost)); 750f9083fdbSLarry Lile memcpy(th->iso88025_shost, ac->ac_enaddr, 751f9083fdbSLarry Lile sizeof(th->iso88025_shost)); 752fda82fc2SJulian Elischer /* Set the source routing bit if neccesary */ 753b149dd6cSLarry Lile if (th->iso88025_dhost[0] & TR_RII) { 754b149dd6cSLarry Lile th->iso88025_dhost[0] &= ~TR_RII; 755b149dd6cSLarry Lile if (TR_RCF_RIFLEN(th->rcf) > 2) 756b149dd6cSLarry Lile th->iso88025_shost[0] |= TR_RII; 757fda82fc2SJulian Elischer } 758fda82fc2SJulian Elischer /* Copy the addresses, ac and fc into sa_data */ 759f9083fdbSLarry Lile memcpy(sa.sa_data, th->iso88025_dhost, 760f9083fdbSLarry Lile sizeof(th->iso88025_dhost) * 2); 761b149dd6cSLarry Lile sa.sa_data[(sizeof(th->iso88025_dhost) * 2)] = TR_AC; 762b149dd6cSLarry Lile sa.sa_data[(sizeof(th->iso88025_dhost) * 2) + 1] = TR_LLC_FRAME; 763fda82fc2SJulian Elischer break; 764243c4c6dSEivind Eklund case IFT_ETHER: 765f9083fdbSLarry Lile case IFT_FDDI: 766f9083fdbSLarry Lile /* 767f9083fdbSLarry Lile * May not be correct for types not explictly 768f9083fdbSLarry Lile * listed, but it is our best guess. 769f9083fdbSLarry Lile */ 770f9083fdbSLarry Lile default: 771df8bae1dSRodney W. Grimes eh = (struct ether_header *)sa.sa_data; 772f9083fdbSLarry Lile (void)memcpy(eh->ether_dhost, ea->arp_tha, 773f9083fdbSLarry Lile sizeof(eh->ether_dhost)); 77434bed8b0SDavid Greenman eh->ether_type = htons(ETHERTYPE_ARP); 775fda82fc2SJulian Elischer break; 776fda82fc2SJulian Elischer } 777df8bae1dSRodney W. Grimes sa.sa_family = AF_UNSPEC; 778df8bae1dSRodney W. Grimes sa.sa_len = sizeof(sa); 779df8bae1dSRodney W. Grimes (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0); 780df8bae1dSRodney W. Grimes return; 781df8bae1dSRodney W. Grimes } 7821d5e9e22SEivind Eklund #endif 783df8bae1dSRodney W. Grimes 784df8bae1dSRodney W. Grimes /* 785df8bae1dSRodney W. Grimes * Free an arp entry. 786df8bae1dSRodney W. Grimes */ 787df8bae1dSRodney W. Grimes static void 788df8bae1dSRodney W. Grimes arptfree(la) 789df8bae1dSRodney W. Grimes register struct llinfo_arp *la; 790df8bae1dSRodney W. Grimes { 791df8bae1dSRodney W. Grimes register struct rtentry *rt = la->la_rt; 792df8bae1dSRodney W. Grimes register struct sockaddr_dl *sdl; 793df8bae1dSRodney W. Grimes if (rt == 0) 794df8bae1dSRodney W. Grimes panic("arptfree"); 795df8bae1dSRodney W. Grimes if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) && 796df8bae1dSRodney W. Grimes sdl->sdl_family == AF_LINK) { 797df8bae1dSRodney W. Grimes sdl->sdl_alen = 0; 798df8bae1dSRodney W. Grimes la->la_asked = 0; 799df8bae1dSRodney W. Grimes rt->rt_flags &= ~RTF_REJECT; 800df8bae1dSRodney W. Grimes return; 801df8bae1dSRodney W. Grimes } 802df8bae1dSRodney W. Grimes rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt), 803df8bae1dSRodney W. Grimes 0, (struct rtentry **)0); 804df8bae1dSRodney W. Grimes } 805df8bae1dSRodney W. Grimes /* 806df8bae1dSRodney W. Grimes * Lookup or enter a new address in arptab. 807df8bae1dSRodney W. Grimes */ 808df8bae1dSRodney W. Grimes static struct llinfo_arp * 809df8bae1dSRodney W. Grimes arplookup(addr, create, proxy) 810df8bae1dSRodney W. Grimes u_long addr; 811df8bae1dSRodney W. Grimes int create, proxy; 812df8bae1dSRodney W. Grimes { 813df8bae1dSRodney W. Grimes register struct rtentry *rt; 814df8bae1dSRodney W. Grimes static struct sockaddr_inarp sin = {sizeof(sin), AF_INET }; 815ac234f93SGarrett Wollman const char *why = 0; 816df8bae1dSRodney W. Grimes 817df8bae1dSRodney W. Grimes sin.sin_addr.s_addr = addr; 818df8bae1dSRodney W. Grimes sin.sin_other = proxy ? SIN_PROXY : 0; 81931246bc2SGarrett Wollman rt = rtalloc1((struct sockaddr *)&sin, create, 0UL); 820df8bae1dSRodney W. Grimes if (rt == 0) 821df8bae1dSRodney W. Grimes return (0); 822df8bae1dSRodney W. Grimes rt->rt_refcnt--; 823ac234f93SGarrett Wollman 824ac234f93SGarrett Wollman if (rt->rt_flags & RTF_GATEWAY) 825ac234f93SGarrett Wollman why = "host is not on local network"; 826ac234f93SGarrett Wollman else if ((rt->rt_flags & RTF_LLINFO) == 0) 827ac234f93SGarrett Wollman why = "could not allocate llinfo"; 828ac234f93SGarrett Wollman else if (rt->rt_gateway->sa_family != AF_LINK) 829ac234f93SGarrett Wollman why = "gateway route is not ours"; 830ac234f93SGarrett Wollman 831ac234f93SGarrett Wollman if (why && create) { 832ac234f93SGarrett Wollman log(LOG_DEBUG, "arplookup %s failed: %s\n", 833ef0cdf33SGarrett Wollman inet_ntoa(sin.sin_addr), why); 834ac234f93SGarrett Wollman return 0; 835ac234f93SGarrett Wollman } else if (why) { 836ac234f93SGarrett Wollman return 0; 837df8bae1dSRodney W. Grimes } 838df8bae1dSRodney W. Grimes return ((struct llinfo_arp *)rt->rt_llinfo); 839df8bae1dSRodney W. Grimes } 840df8bae1dSRodney W. Grimes 841dd2e4102SGarrett Wollman void 842dd2e4102SGarrett Wollman arp_ifinit(ac, ifa) 843dd2e4102SGarrett Wollman struct arpcom *ac; 844dd2e4102SGarrett Wollman struct ifaddr *ifa; 845dd2e4102SGarrett Wollman { 846dd570d4dSTor Egge if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 847ed7509acSJulian Elischer arprequest(ac, &IA_SIN(ifa)->sin_addr, 848ed7509acSJulian Elischer &IA_SIN(ifa)->sin_addr, ac->ac_enaddr); 849dd2e4102SGarrett Wollman ifa->ifa_rtrequest = arp_rtrequest; 850dd2e4102SGarrett Wollman ifa->ifa_flags |= RTF_CLONING; 851dd2e4102SGarrett Wollman } 852df5e1987SJonathan Lemon 853df5e1987SJonathan Lemon static void 854df5e1987SJonathan Lemon arp_init(void) 855df5e1987SJonathan Lemon { 856df5e1987SJonathan Lemon 857df5e1987SJonathan Lemon arpintrq.ifq_maxlen = 50; 858df5e1987SJonathan Lemon mtx_init(&arpintrq.ifq_mtx, "arp_inq", MTX_DEF); 859df5e1987SJonathan Lemon } 860df5e1987SJonathan Lemon 861df5e1987SJonathan Lemon SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0); 862