if_ether.c revision 120727
11541Srgrimes/* 21541Srgrimes * Copyright (c) 1982, 1986, 1988, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * 51541Srgrimes * Redistribution and use in source and binary forms, with or without 61541Srgrimes * modification, are permitted provided that the following conditions 71541Srgrimes * are met: 81541Srgrimes * 1. Redistributions of source code must retain the above copyright 91541Srgrimes * notice, this list of conditions and the following disclaimer. 101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111541Srgrimes * notice, this list of conditions and the following disclaimer in the 121541Srgrimes * documentation and/or other materials provided with the distribution. 131541Srgrimes * 3. All advertising materials mentioning features or use of this software 141541Srgrimes * must display the following acknowledgement: 151541Srgrimes * This product includes software developed by the University of 161541Srgrimes * California, Berkeley and its contributors. 171541Srgrimes * 4. Neither the name of the University nor the names of its contributors 181541Srgrimes * may be used to endorse or promote products derived from this software 191541Srgrimes * without specific prior written permission. 201541Srgrimes * 211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311541Srgrimes * SUCH DAMAGE. 321541Srgrimes * 331541Srgrimes * @(#)if_ether.c 8.1 (Berkeley) 6/10/93 3450477Speter * $FreeBSD: head/sys/netinet/if_ether.c 120727 2003-10-04 03:44:50Z sam $ 351541Srgrimes */ 361541Srgrimes 371541Srgrimes/* 381541Srgrimes * Ethernet address resolution protocol. 391541Srgrimes * TODO: 401541Srgrimes * add "inuse/lock" bit (or ref. count) along with valid bit 411541Srgrimes */ 421541Srgrimes 4332350Seivind#include "opt_inet.h" 4441793Sluigi#include "opt_bdg.h" 45101090Srwatson#include "opt_mac.h" 4632350Seivind 471541Srgrimes#include <sys/param.h> 4812693Sphk#include <sys/kernel.h> 4944078Sdfr#include <sys/queue.h> 5012693Sphk#include <sys/sysctl.h> 511541Srgrimes#include <sys/systm.h> 52101090Srwatson#include <sys/mac.h> 5312693Sphk#include <sys/mbuf.h> 541541Srgrimes#include <sys/malloc.h> 5518892Sbde#include <sys/socket.h> 561541Srgrimes#include <sys/syslog.h> 571541Srgrimes 581541Srgrimes#include <net/if.h> 591541Srgrimes#include <net/if_dl.h> 6044165Sjulian#include <net/if_types.h> 611541Srgrimes#include <net/route.h> 628426Swollman#include <net/netisr.h> 6358313Slile#include <net/if_llc.h> 6471963Sjulian#ifdef BRIDGE 6571963Sjulian#include <net/ethernet.h> 6671963Sjulian#include <net/bridge.h> 6771963Sjulian#endif 681541Srgrimes 691541Srgrimes#include <netinet/in.h> 701541Srgrimes#include <netinet/in_var.h> 711541Srgrimes#include <netinet/if_ether.h> 721541Srgrimes 7384931Sfjoe#include <net/if_arc.h> 7444627Sjulian#include <net/iso88025.h> 7544627Sjulian 761541Srgrimes#define SIN(s) ((struct sockaddr_in *)s) 771541Srgrimes#define SDL(s) ((struct sockaddr_dl *)s) 781541Srgrimes 7944078SdfrSYSCTL_DECL(_net_link_ether); 8012942SwollmanSYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, ""); 811541Srgrimes 8212693Sphk/* timer values */ 8312942Swollmanstatic int arpt_prune = (5*60*1); /* walk list every 5 minutes */ 8412942Swollmanstatic int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ 8512942Swollmanstatic int arpt_down = 20; /* once declared down, don't send for 20 sec */ 861541Srgrimes 8712942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW, 8812942Swollman &arpt_prune, 0, ""); 8912942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW, 9012942Swollman &arpt_keep, 0, ""); 9112942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW, 9212942Swollman &arpt_down, 0, ""); 9312693Sphk 941541Srgrimes#define rt_expire rt_rmx.rmx_expire 951541Srgrimes 9611225Swollmanstruct llinfo_arp { 9760938Sjake LIST_ENTRY(llinfo_arp) la_le; 9811225Swollman struct rtentry *la_rt; 99110308Sorion struct mbuf *la_hold; /* last packet until resolved/timeout */ 100110544Sorion u_short la_preempt; /* countdown for pre-expiry arps */ 101110308Sorion u_short la_asked; /* #times we QUERIED following expiration */ 10211225Swollman#define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */ 10311225Swollman}; 10412693Sphk 10560938Sjakestatic LIST_HEAD(, llinfo_arp) llinfo_arp; 10611225Swollman 107111888Sjlemonstatic struct ifqueue arpintrq; 108120727Ssamstatic int arp_allocated; 109120727Ssamstatic int arpinit_done; 1101541Srgrimes 11112693Sphkstatic int arp_maxtries = 5; 11212942Swollmanstatic int useloopback = 1; /* use loopback interface for local traffic */ 11312942Swollmanstatic int arp_proxyall = 0; 114120727Ssamstatic struct callout arp_callout; 1153282Swollman 11612942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW, 11712942Swollman &arp_maxtries, 0, ""); 11812942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW, 11912942Swollman &useloopback, 0, ""); 12012942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW, 12112942Swollman &arp_proxyall, 0, ""); 12212693Sphk 12392723Salfredstatic void arp_init(void); 12492723Salfredstatic void arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *); 12592723Salfredstatic void arprequest(struct ifnet *, 12692723Salfred struct in_addr *, struct in_addr *, u_char *); 127111888Sjlemonstatic void arpintr(struct mbuf *); 12892723Salfredstatic void arptfree(struct llinfo_arp *); 12992723Salfredstatic void arptimer(void *); 13012693Sphkstatic struct llinfo_arp 13192723Salfred *arplookup(u_long, int, int); 13232350Seivind#ifdef INET 13392723Salfredstatic void in_arpinput(struct mbuf *); 13432350Seivind#endif 13512693Sphk 1361541Srgrimes/* 1371541Srgrimes * Timeout routine. Age arp_tab entries periodically. 1381541Srgrimes */ 1391541Srgrimes/* ARGSUSED */ 1401541Srgrimesstatic void 1411541Srgrimesarptimer(ignored_arg) 1421541Srgrimes void *ignored_arg; 1431541Srgrimes{ 144109409Shsu struct llinfo_arp *la, *ola; 1451541Srgrimes 146109996Shsu RADIX_NODE_HEAD_LOCK(rt_tables[AF_INET]); 147109409Shsu la = LIST_FIRST(&llinfo_arp); 148109409Shsu while (la != NULL) { 149109409Shsu struct rtentry *rt = la->la_rt; 150109409Shsu ola = la; 15171999Sphk la = LIST_NEXT(la, la_le); 15234961Sphk if (rt->rt_expire && rt->rt_expire <= time_second) 153109409Shsu arptfree(ola); /* timer has expired, clear */ 1541541Srgrimes } 155109996Shsu RADIX_NODE_HEAD_UNLOCK(rt_tables[AF_INET]); 156120727Ssam 157120727Ssam callout_reset(&arp_callout, arpt_prune * hz, arptimer, NULL); 1581541Srgrimes} 1591541Srgrimes 1601541Srgrimes/* 1611541Srgrimes * Parallel to llc_rtrequest. 1621541Srgrimes */ 1635196Swollmanstatic void 16485074Sruarp_rtrequest(req, rt, info) 1651541Srgrimes int req; 1661541Srgrimes register struct rtentry *rt; 16785074Sru struct rt_addrinfo *info; 1681541Srgrimes{ 169120727Ssam register struct sockaddr *gate; 170120727Ssam register struct llinfo_arp *la; 1711541Srgrimes static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 1721541Srgrimes 173120727Ssam RT_LOCK_ASSERT(rt); 174120727Ssam 1751541Srgrimes if (!arpinit_done) { 1761541Srgrimes arpinit_done = 1; 177120727Ssam callout_reset(&arp_callout, hz, arptimer, NULL); 1781541Srgrimes } 1791541Srgrimes if (rt->rt_flags & RTF_GATEWAY) 1801541Srgrimes return; 181120727Ssam gate = rt->rt_gateway; 182120727Ssam la = (struct llinfo_arp *)rt->rt_llinfo; 1831541Srgrimes switch (req) { 1841541Srgrimes 1851541Srgrimes case RTM_ADD: 1861541Srgrimes /* 1871541Srgrimes * XXX: If this is a manually added route to interface 1881541Srgrimes * such as older version of routed or gated might provide, 1891541Srgrimes * restore cloning bit. 1901541Srgrimes */ 1911541Srgrimes if ((rt->rt_flags & RTF_HOST) == 0 && 1921541Srgrimes SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 1931541Srgrimes rt->rt_flags |= RTF_CLONING; 1941541Srgrimes if (rt->rt_flags & RTF_CLONING) { 1951541Srgrimes /* 1961541Srgrimes * Case 1: This route should come from a route to iface. 1971541Srgrimes */ 1981541Srgrimes rt_setgate(rt, rt_key(rt), 1991541Srgrimes (struct sockaddr *)&null_sdl); 2001541Srgrimes gate = rt->rt_gateway; 2011541Srgrimes SDL(gate)->sdl_type = rt->rt_ifp->if_type; 2021541Srgrimes SDL(gate)->sdl_index = rt->rt_ifp->if_index; 20334961Sphk rt->rt_expire = time_second; 2041541Srgrimes break; 2051541Srgrimes } 2061541Srgrimes /* Announce a new entry if requested. */ 2071541Srgrimes if (rt->rt_flags & RTF_ANNOUNCE) 20884931Sfjoe arprequest(rt->rt_ifp, 20936908Sjulian &SIN(rt_key(rt))->sin_addr, 21036908Sjulian &SIN(rt_key(rt))->sin_addr, 2111541Srgrimes (u_char *)LLADDR(SDL(gate))); 2121541Srgrimes /*FALLTHROUGH*/ 2131541Srgrimes case RTM_RESOLVE: 2141541Srgrimes if (gate->sa_family != AF_LINK || 2151541Srgrimes gate->sa_len < sizeof(null_sdl)) { 216120727Ssam log(LOG_DEBUG, "%s: bad gateway %s%s\n", __func__, 217120698Sbms inet_ntoa(SIN(rt_key(rt))->sin_addr), 218120698Sbms (gate->sa_family != AF_LINK) ? 219120699Sbms " (!AF_LINK)": ""); 2201541Srgrimes break; 2211541Srgrimes } 2221541Srgrimes SDL(gate)->sdl_type = rt->rt_ifp->if_type; 2231541Srgrimes SDL(gate)->sdl_index = rt->rt_ifp->if_index; 2241541Srgrimes if (la != 0) 2251541Srgrimes break; /* This happens on a route change */ 2261541Srgrimes /* 2271541Srgrimes * Case 2: This route may come from cloning, or a manual route 2281541Srgrimes * add with a LL address. 2291541Srgrimes */ 230120727Ssam R_Zalloc(la, struct llinfo_arp *, sizeof(*la)); 2311541Srgrimes rt->rt_llinfo = (caddr_t)la; 2321541Srgrimes if (la == 0) { 233120727Ssam log(LOG_DEBUG, "%s: malloc failed\n", __func__); 2341541Srgrimes break; 2351541Srgrimes } 236120727Ssam arp_allocated++; 2371541Srgrimes la->la_rt = rt; 2381541Srgrimes rt->rt_flags |= RTF_LLINFO; 239109996Shsu RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]); 24011225Swollman LIST_INSERT_HEAD(&llinfo_arp, la, la_le); 24113926Swollman 24232350Seivind#ifdef INET 24313926Swollman /* 24413926Swollman * This keeps the multicast addresses from showing up 24513926Swollman * in `arp -a' listings as unresolved. It's not actually 24613926Swollman * functional. Then the same for broadcast. 24713926Swollman */ 24887776Sjlemon if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) && 24987776Sjlemon rt->rt_ifp->if_type != IFT_ARCNET) { 25013926Swollman ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr, 25113926Swollman LLADDR(SDL(gate))); 25213926Swollman SDL(gate)->sdl_alen = 6; 25316576Speter rt->rt_expire = 0; 25413926Swollman } 25513926Swollman if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) { 25684931Sfjoe memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr, 25784931Sfjoe rt->rt_ifp->if_addrlen); 25884931Sfjoe SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen; 25916576Speter rt->rt_expire = 0; 26013926Swollman } 26132350Seivind#endif 26213926Swollman 2631541Srgrimes if (SIN(rt_key(rt))->sin_addr.s_addr == 2641541Srgrimes (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) { 2651541Srgrimes /* 2661541Srgrimes * This test used to be 2671541Srgrimes * if (loif.if_flags & IFF_UP) 2681541Srgrimes * It allowed local traffic to be forced 2691541Srgrimes * through the hardware by configuring the loopback down. 2701541Srgrimes * However, it causes problems during network configuration 2711541Srgrimes * for boards that can't receive packets they send. 2721541Srgrimes * It is now necessary to clear "useloopback" and remove 2731541Srgrimes * the route to force traffic out to the hardware. 2741541Srgrimes */ 2751541Srgrimes rt->rt_expire = 0; 27684931Sfjoe Bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)), 27784931Sfjoe SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen); 2781541Srgrimes if (useloopback) 2798090Spst rt->rt_ifp = loif; 2801541Srgrimes 2811541Srgrimes } 2821541Srgrimes break; 2831541Srgrimes 2841541Srgrimes case RTM_DELETE: 2851541Srgrimes if (la == 0) 2861541Srgrimes break; 287109996Shsu RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]); 28811225Swollman LIST_REMOVE(la, la_le); 2891541Srgrimes rt->rt_llinfo = 0; 2901541Srgrimes rt->rt_flags &= ~RTF_LLINFO; 2911541Srgrimes if (la->la_hold) 2921541Srgrimes m_freem(la->la_hold); 2931541Srgrimes Free((caddr_t)la); 2941541Srgrimes } 2951541Srgrimes} 2961541Srgrimes 2971541Srgrimes/* 2981541Srgrimes * Broadcast an ARP request. Caller specifies: 2991541Srgrimes * - arp header source ip address 3001541Srgrimes * - arp header target ip address 3011541Srgrimes * - arp header source ethernet address 3021541Srgrimes */ 3031541Srgrimesstatic void 30484931Sfjoearprequest(ifp, sip, tip, enaddr) 30584931Sfjoe register struct ifnet *ifp; 30636908Sjulian register struct in_addr *sip, *tip; 3071541Srgrimes register u_char *enaddr; 3081541Srgrimes{ 3091541Srgrimes register struct mbuf *m; 3101541Srgrimes register struct ether_header *eh; 31184931Sfjoe register struct arc_header *arh; 31284931Sfjoe register struct arphdr *ah; 3131541Srgrimes struct sockaddr sa; 31458313Slile static u_char llcx[] = { 0x82, 0x40, LLC_SNAP_LSAP, LLC_SNAP_LSAP, 31558313Slile LLC_UI, 0x00, 0x00, 0x00, 0x08, 0x06 }; 31684931Sfjoe u_short ar_hrd; 3171541Srgrimes 318111119Simp if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 3191541Srgrimes return; 32044456Swpaul m->m_pkthdr.rcvif = (struct ifnet *)0; 321101090Srwatson#ifdef MAC 322101090Srwatson mac_create_mbuf_linklayer(ifp, m); 323101090Srwatson#endif 32484931Sfjoe switch (ifp->if_type) { 32584931Sfjoe case IFT_ARCNET: 32684931Sfjoe ar_hrd = htons(ARPHRD_ARCNET); 32784931Sfjoe 32884931Sfjoe m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 32984931Sfjoe m->m_pkthdr.len = m->m_len; 33084931Sfjoe MH_ALIGN(m, m->m_len); 33184931Sfjoe 33284931Sfjoe arh = (struct arc_header *)sa.sa_data; 33384931Sfjoe arh->arc_dhost = *ifp->if_broadcastaddr; 33484931Sfjoe arh->arc_type = ARCTYPE_ARP; 33584931Sfjoe 33684931Sfjoe ah = mtod(m, struct arphdr *); 33784931Sfjoe break; 33884931Sfjoe 33944627Sjulian case IFT_ISO88025: 34084931Sfjoe ar_hrd = htons(ARPHRD_IEEE802); 34184931Sfjoe 34284931Sfjoe m->m_len = sizeof(llcx) + 34384931Sfjoe arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 34484931Sfjoe m->m_pkthdr.len = m->m_len; 34584931Sfjoe MH_ALIGN(m, m->m_len); 34684931Sfjoe 34758313Slile (void)memcpy(mtod(m, caddr_t), llcx, sizeof(llcx)); 34884931Sfjoe (void)memcpy(sa.sa_data, ifp->if_broadcastaddr, 6); 34944627Sjulian (void)memcpy(sa.sa_data + 6, enaddr, 6); 35058313Slile sa.sa_data[6] |= TR_RII; 35158313Slile sa.sa_data[12] = TR_AC; 35258313Slile sa.sa_data[13] = TR_LLC_FRAME; 35384931Sfjoe 35484931Sfjoe ah = (struct arphdr *)(mtod(m, char *) + sizeof(llcx)); 35544627Sjulian break; 35651320Slile case IFT_FDDI: 35751320Slile case IFT_ETHER: 35851320Slile /* 35951320Slile * This may not be correct for types not explicitly 36051320Slile * listed, but this is our best guess 36151320Slile */ 36244627Sjulian default: 36384931Sfjoe ar_hrd = htons(ARPHRD_ETHER); 36484931Sfjoe 36584931Sfjoe m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 36684931Sfjoe m->m_pkthdr.len = m->m_len; 36784931Sfjoe MH_ALIGN(m, m->m_len); 36884931Sfjoe 36951320Slile eh = (struct ether_header *)sa.sa_data; 37051320Slile /* if_output will not swap */ 37151320Slile eh->ether_type = htons(ETHERTYPE_ARP); 37284931Sfjoe (void)memcpy(eh->ether_dhost, ifp->if_broadcastaddr, 37351320Slile sizeof(eh->ether_dhost)); 37484931Sfjoe 37584931Sfjoe ah = mtod(m, struct arphdr *); 37651320Slile break; 37744627Sjulian } 37884931Sfjoe 37984931Sfjoe ah->ar_hrd = ar_hrd; 38084931Sfjoe ah->ar_pro = htons(ETHERTYPE_IP); 38184931Sfjoe ah->ar_hln = ifp->if_addrlen; /* hardware address length */ 38284931Sfjoe ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ 38384931Sfjoe ah->ar_op = htons(ARPOP_REQUEST); 38484931Sfjoe (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 385109035Stmm memset(ar_tha(ah), 0, ah->ar_hln); 38684931Sfjoe (void)memcpy(ar_spa(ah), sip, ah->ar_pln); 38784931Sfjoe (void)memcpy(ar_tpa(ah), tip, ah->ar_pln); 38884931Sfjoe 3891541Srgrimes sa.sa_family = AF_UNSPEC; 3901541Srgrimes sa.sa_len = sizeof(sa); 39184931Sfjoe (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 3921541Srgrimes} 3931541Srgrimes 3941541Srgrimes/* 3951541Srgrimes * Resolve an IP address into an ethernet address. If success, 3961541Srgrimes * desten is filled in. If there is no entry in arptab, 3971541Srgrimes * set one up and broadcast a request for the IP address. 3981541Srgrimes * Hold onto this mbuf and resend it once the address 3991541Srgrimes * is finally resolved. A return value of 1 indicates 4001541Srgrimes * that desten has been filled in and the packet should be sent 4011541Srgrimes * normally; a 0 return indicates that the packet has been 4021541Srgrimes * taken over here, either now or for later transmission. 4031541Srgrimes */ 4041541Srgrimesint 40584931Sfjoearpresolve(ifp, rt, m, dst, desten, rt0) 40684931Sfjoe register struct ifnet *ifp; 4071541Srgrimes register struct rtentry *rt; 4081541Srgrimes struct mbuf *m; 4091541Srgrimes register struct sockaddr *dst; 4101541Srgrimes register u_char *desten; 4113514Swollman struct rtentry *rt0; 4121541Srgrimes{ 41378295Sjlemon struct llinfo_arp *la = 0; 4141541Srgrimes struct sockaddr_dl *sdl; 4151541Srgrimes 4161541Srgrimes if (m->m_flags & M_BCAST) { /* broadcast */ 41784931Sfjoe (void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen); 4181541Srgrimes return (1); 4191541Srgrimes } 42084931Sfjoe if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */ 4211541Srgrimes ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 4221541Srgrimes return(1); 4231541Srgrimes } 4241541Srgrimes if (rt) 4251541Srgrimes la = (struct llinfo_arp *)rt->rt_llinfo; 42642775Sfenner if (la == 0) { 4273311Sphk la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0); 4283311Sphk if (la) 4291541Srgrimes rt = la->la_rt; 4301541Srgrimes } 4311541Srgrimes if (la == 0 || rt == 0) { 43236308Sphk log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n", 43336308Sphk inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "", 43436308Sphk rt ? "rt" : ""); 4351541Srgrimes m_freem(m); 4361541Srgrimes return (0); 4371541Srgrimes } 4381541Srgrimes sdl = SDL(rt->rt_gateway); 4391541Srgrimes /* 4401541Srgrimes * Check the address family and length is valid, the address 4411541Srgrimes * is resolved; otherwise, try to resolve. 4421541Srgrimes */ 44334961Sphk if ((rt->rt_expire == 0 || rt->rt_expire > time_second) && 4441541Srgrimes sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { 44592802Sorion /* 44692802Sorion * If entry has an expiry time and it is approaching, 44792802Sorion * see if we need to send an ARP request within this 44892802Sorion * arpt_down interval. 44992802Sorion */ 45092802Sorion if ((rt->rt_expire != 0) && 451110544Sorion (time_second + la->la_preempt > rt->rt_expire)) { 45292802Sorion arprequest(ifp, 45392802Sorion &SIN(rt->rt_ifa->ifa_addr)->sin_addr, 45492802Sorion &SIN(dst)->sin_addr, 45592802Sorion IF_LLADDR(ifp)); 456110544Sorion la->la_preempt--; 45792802Sorion } 45892802Sorion 45916206Sbde bcopy(LLADDR(sdl), desten, sdl->sdl_alen); 4601541Srgrimes return 1; 4611541Srgrimes } 4621541Srgrimes /* 463120626Sru * If ARP is disabled or static on this interface, stop. 46478295Sjlemon * XXX 46578295Sjlemon * Probably should not allocate empty llinfo struct if we are 46678295Sjlemon * not going to be sending out an arp request. 46778295Sjlemon */ 468120626Sru if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) { 46987410Sru m_freem(m); 47078295Sjlemon return (0); 47187410Sru } 47278295Sjlemon /* 4731541Srgrimes * There is an arptab entry, but no ethernet address 4741541Srgrimes * response yet. Replace the held mbuf with this 4751541Srgrimes * latest one. 4761541Srgrimes */ 4771541Srgrimes if (la->la_hold) 4781541Srgrimes m_freem(la->la_hold); 4791541Srgrimes la->la_hold = m; 4801541Srgrimes if (rt->rt_expire) { 481120727Ssam RT_LOCK(rt); 4821541Srgrimes rt->rt_flags &= ~RTF_REJECT; 48334961Sphk if (la->la_asked == 0 || rt->rt_expire != time_second) { 48434961Sphk rt->rt_expire = time_second; 485110308Sorion if (la->la_asked++ < arp_maxtries) { 486110308Sorion arprequest(ifp, 487110308Sorion &SIN(rt->rt_ifa->ifa_addr)->sin_addr, 488110308Sorion &SIN(dst)->sin_addr, 489110308Sorion IF_LLADDR(ifp)); 490110308Sorion } else { 4911541Srgrimes rt->rt_flags |= RTF_REJECT; 4921541Srgrimes rt->rt_expire += arpt_down; 493110544Sorion la->la_asked = 0; 494110544Sorion la->la_preempt = arp_maxtries; 4951541Srgrimes } 4961541Srgrimes 4971541Srgrimes } 498120727Ssam RT_UNLOCK(rt); 4991541Srgrimes } 5001541Srgrimes return (0); 5011541Srgrimes} 5021541Srgrimes 5031541Srgrimes/* 5041541Srgrimes * Common length and type checks are done here, 5051541Srgrimes * then the protocol-specific routine is called. 5061541Srgrimes */ 50712693Sphkstatic void 508111888Sjlemonarpintr(struct mbuf *m) 5091541Srgrimes{ 510111888Sjlemon struct arphdr *ar; 5111541Srgrimes 51298459Speter if (!arpinit_done) { 513120727Ssam /* NB: this race should not matter */ 51498459Speter arpinit_done = 1; 515120727Ssam callout_reset(&arp_callout, hz, arptimer, NULL); 51698459Speter } 517111888Sjlemon if (m->m_len < sizeof(struct arphdr) && 518111888Sjlemon ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) { 519111888Sjlemon log(LOG_ERR, "arp: runt packet -- m_pullup failed\n"); 520111888Sjlemon return; 521111888Sjlemon } 522111888Sjlemon ar = mtod(m, struct arphdr *); 5231541Srgrimes 524111888Sjlemon if (ntohs(ar->ar_hrd) != ARPHRD_ETHER && 525111888Sjlemon ntohs(ar->ar_hrd) != ARPHRD_IEEE802 && 526111888Sjlemon ntohs(ar->ar_hrd) != ARPHRD_ARCNET) { 527111888Sjlemon log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n", 528111888Sjlemon (unsigned char *)&ar->ar_hrd, ""); 529111888Sjlemon m_freem(m); 530111888Sjlemon return; 531111888Sjlemon } 5321541Srgrimes 533111888Sjlemon if (m->m_pkthdr.len < arphdr_len(ar) && 534111888Sjlemon (m = m_pullup(m, arphdr_len(ar))) == NULL) { 535111888Sjlemon log(LOG_ERR, "arp: runt packet\n"); 536111888Sjlemon m_freem(m); 537111888Sjlemon return; 538111888Sjlemon } 53957900Srwatson 540111888Sjlemon switch (ntohs(ar->ar_pro)) { 54132350Seivind#ifdef INET 542111888Sjlemon case ETHERTYPE_IP: 543111888Sjlemon in_arpinput(m); 544111888Sjlemon return; 54532350Seivind#endif 5461541Srgrimes } 547111888Sjlemon m_freem(m); 5481541Srgrimes} 5491541Srgrimes 55032350Seivind#ifdef INET 5511541Srgrimes/* 5521541Srgrimes * ARP for Internet protocols on 10 Mb/s Ethernet. 5531541Srgrimes * Algorithm is that given in RFC 826. 5541541Srgrimes * In addition, a sanity check is performed on the sender 5551541Srgrimes * protocol address, to catch impersonators. 5561541Srgrimes * We no longer handle negotiations for use of trailer protocol: 5571541Srgrimes * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 5581541Srgrimes * along with IP replies if we wanted trailers sent to us, 5591541Srgrimes * and also sent them in response to IP replies. 5601541Srgrimes * This allowed either end to announce the desire to receive 5611541Srgrimes * trailer packets. 5621541Srgrimes * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 5631541Srgrimes * but formerly didn't normally send requests. 5641541Srgrimes */ 56570699Salfredstatic int log_arp_wrong_iface = 1; 56682893Salfredstatic int log_arp_movements = 1; 56770699Salfred 56870699SalfredSYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW, 56970699Salfred &log_arp_wrong_iface, 0, 57070699Salfred "log arp packets arriving on the wrong interface"); 57182893SalfredSYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW, 57282893Salfred &log_arp_movements, 0, 57382966Salfred "log arp replies from MACs different than the one in the cache"); 57470699Salfred 57582893Salfred 5761541Srgrimesstatic void 5771541Srgrimesin_arpinput(m) 5781541Srgrimes struct mbuf *m; 5791541Srgrimes{ 58084931Sfjoe register struct arphdr *ah; 58184931Sfjoe register struct ifnet *ifp = m->m_pkthdr.rcvif; 5821541Srgrimes struct ether_header *eh; 58384931Sfjoe struct arc_header *arh; 58444627Sjulian struct iso88025_header *th = (struct iso88025_header *)0; 58596184Skbyanc struct iso88025_sockaddr_dl_data *trld; 5861541Srgrimes register struct llinfo_arp *la = 0; 5871541Srgrimes register struct rtentry *rt; 58884102Sjlemon struct ifaddr *ifa; 58984102Sjlemon struct in_ifaddr *ia; 5901541Srgrimes struct sockaddr_dl *sdl; 5911541Srgrimes struct sockaddr sa; 5921541Srgrimes struct in_addr isaddr, itaddr, myaddr; 59358313Slile int op, rif_len; 59484931Sfjoe int req_len; 5951541Srgrimes 59684931Sfjoe req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 59784931Sfjoe if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) { 59874851Syar log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n"); 59974851Syar return; 60074851Syar } 60174851Syar 60284931Sfjoe ah = mtod(m, struct arphdr *); 60384931Sfjoe op = ntohs(ah->ar_op); 60484931Sfjoe (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr)); 60584931Sfjoe (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr)); 60672056Sjulian#ifdef BRIDGE 60772056Sjulian#define BRIDGE_TEST (do_bridge) 60871963Sjulian#else 60972056Sjulian#define BRIDGE_TEST (0) /* cc will optimise the test away */ 61041793Sluigi#endif 61184102Sjlemon /* 61284102Sjlemon * For a bridge, we want to check the address irrespective 61384102Sjlemon * of the receive interface. (This will change slightly 61484102Sjlemon * when we have clusters of interfaces). 61584102Sjlemon */ 61684102Sjlemon LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) 61784931Sfjoe if ((BRIDGE_TEST || (ia->ia_ifp == ifp)) && 61884102Sjlemon itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) 61984102Sjlemon goto match; 62084102Sjlemon LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash) 62184931Sfjoe if ((BRIDGE_TEST || (ia->ia_ifp == ifp)) && 62284102Sjlemon isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) 62384102Sjlemon goto match; 62484102Sjlemon /* 62585223Sjlemon * No match, use the first inet address on the receive interface 62684102Sjlemon * as a dummy address for the rest of the function. 62784102Sjlemon */ 62885223Sjlemon TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 62985466Sjlemon if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) { 63085466Sjlemon ia = ifatoia(ifa); 63185466Sjlemon goto match; 63285466Sjlemon } 63385466Sjlemon /* 63485466Sjlemon * If bridging, fall back to using any inet address. 63585466Sjlemon */ 63685466Sjlemon if (!BRIDGE_TEST || 63785466Sjlemon (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL) { 63812693Sphk m_freem(m); 63912693Sphk return; 64012693Sphk } 64184102Sjlemonmatch: 64284102Sjlemon myaddr = ia->ia_addr.sin_addr; 64384931Sfjoe if (!bcmp(ar_sha(ah), IF_LLADDR(ifp), ifp->if_addrlen)) { 64412693Sphk m_freem(m); /* it's from me, ignore it. */ 64512693Sphk return; 64612693Sphk } 64784931Sfjoe if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) { 6481541Srgrimes log(LOG_ERR, 64984931Sfjoe "arp: link address is broadcast for IP address %s!\n", 6507088Swollman inet_ntoa(isaddr)); 65112693Sphk m_freem(m); 65212693Sphk return; 6531541Srgrimes } 6541541Srgrimes if (isaddr.s_addr == myaddr.s_addr) { 6551541Srgrimes log(LOG_ERR, 65684931Sfjoe "arp: %*D is using my IP address %s!\n", 65784931Sfjoe ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 65884931Sfjoe inet_ntoa(isaddr)); 6591541Srgrimes itaddr = myaddr; 6601541Srgrimes goto reply; 6611541Srgrimes } 662120626Sru if (ifp->if_flags & IFF_STATICARP) 663120626Sru goto reply; 6641541Srgrimes la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0); 6651541Srgrimes if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) { 66672270Sluigi /* the following is not an error when doing bridging */ 66784931Sfjoe if (!BRIDGE_TEST && rt->rt_ifp != ifp) { 66872270Sluigi if (log_arp_wrong_iface) 66984931Sfjoe log(LOG_ERR, "arp: %s is on %s%d but got reply from %*D on %s%d\n", 67071963Sjulian inet_ntoa(isaddr), 67171963Sjulian rt->rt_ifp->if_name, rt->rt_ifp->if_unit, 67284931Sfjoe ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 67384931Sfjoe ifp->if_name, ifp->if_unit); 67472270Sluigi goto reply; 67539389Sfenner } 6761541Srgrimes if (sdl->sdl_alen && 67784931Sfjoe bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) { 67882893Salfred if (rt->rt_expire) { 67982893Salfred if (log_arp_movements) 68084931Sfjoe log(LOG_INFO, "arp: %s moved from %*D to %*D on %s%d\n", 68184931Sfjoe inet_ntoa(isaddr), 68284931Sfjoe ifp->if_addrlen, (u_char *)LLADDR(sdl), ":", 68384931Sfjoe ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 68484931Sfjoe ifp->if_name, ifp->if_unit); 68582893Salfred } else { 68639389Sfenner log(LOG_ERR, 68784931Sfjoe "arp: %*D attempts to modify permanent entry for %s on %s%d\n", 68884931Sfjoe ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 68984931Sfjoe inet_ntoa(isaddr), ifp->if_name, ifp->if_unit); 69039389Sfenner goto reply; 69139389Sfenner } 69246568Speter } 69384931Sfjoe /* 69484931Sfjoe * sanity check for the address length. 69584931Sfjoe * XXX this does not work for protocols with variable address 69684931Sfjoe * length. -is 69784931Sfjoe */ 69884931Sfjoe if (sdl->sdl_alen && 69984931Sfjoe sdl->sdl_alen != ah->ar_hln) { 70084931Sfjoe log(LOG_WARNING, 70184931Sfjoe "arp from %*D: new addr len %d, was %d", 70284931Sfjoe ifp->if_addrlen, (u_char *) ar_sha(ah), ":", 70384931Sfjoe ah->ar_hln, sdl->sdl_alen); 70484931Sfjoe } 70584931Sfjoe if (ifp->if_addrlen != ah->ar_hln) { 70684931Sfjoe log(LOG_WARNING, 70784931Sfjoe "arp from %*D: addr len: new %d, i/f %d (ignored)", 70884931Sfjoe ifp->if_addrlen, (u_char *) ar_sha(ah), ":", 70984931Sfjoe ah->ar_hln, ifp->if_addrlen); 71084931Sfjoe goto reply; 71184931Sfjoe } 71284931Sfjoe (void)memcpy(LLADDR(sdl), ar_sha(ah), 71384931Sfjoe sdl->sdl_alen = ah->ar_hln); 71445705Seivind /* 71545705Seivind * If we receive an arp from a token-ring station over 71645705Seivind * a token-ring nic then try to save the source 71745705Seivind * routing info. 71845705Seivind */ 71984931Sfjoe if (ifp->if_type == IFT_ISO88025) { 72044627Sjulian th = (struct iso88025_header *)m->m_pkthdr.header; 72196184Skbyanc trld = SDL_ISO88025(sdl); 72258313Slile rif_len = TR_RCF_RIFLEN(th->rcf); 72358313Slile if ((th->iso88025_shost[0] & TR_RII) && 72458313Slile (rif_len > 2)) { 72596184Skbyanc trld->trld_rcf = th->rcf; 72696184Skbyanc trld->trld_rcf ^= htons(TR_RCF_DIR); 72796184Skbyanc memcpy(trld->trld_route, th->rd, rif_len - 2); 72896184Skbyanc trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK); 72951320Slile /* 73051320Slile * Set up source routing information for 73151320Slile * reply packet (XXX) 73251320Slile */ 73358313Slile m->m_data -= rif_len; 73458313Slile m->m_len += rif_len; 73558313Slile m->m_pkthdr.len += rif_len; 73644627Sjulian } else { 73758313Slile th->iso88025_shost[0] &= ~TR_RII; 73896624Skbyanc trld->trld_rcf = 0; 73944627Sjulian } 74050512Slile m->m_data -= 8; 74150512Slile m->m_len += 8; 74250512Slile m->m_pkthdr.len += 8; 74396184Skbyanc th->rcf = trld->trld_rcf; 74444627Sjulian } 745120727Ssam RT_LOCK(rt); 7461541Srgrimes if (rt->rt_expire) 74734961Sphk rt->rt_expire = time_second + arpt_keep; 7481541Srgrimes rt->rt_flags &= ~RTF_REJECT; 749120727Ssam RT_UNLOCK(rt); 750110544Sorion la->la_asked = 0; 751110544Sorion la->la_preempt = arp_maxtries; 7521541Srgrimes if (la->la_hold) { 75384931Sfjoe (*ifp->if_output)(ifp, la->la_hold, 7541541Srgrimes rt_key(rt), rt); 7551541Srgrimes la->la_hold = 0; 7561541Srgrimes } 7571541Srgrimes } 7581541Srgrimesreply: 7591541Srgrimes if (op != ARPOP_REQUEST) { 7601541Srgrimes m_freem(m); 7611541Srgrimes return; 7621541Srgrimes } 7631541Srgrimes if (itaddr.s_addr == myaddr.s_addr) { 7641541Srgrimes /* I am the target */ 76584931Sfjoe (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 76684931Sfjoe (void)memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln); 7671541Srgrimes } else { 7681541Srgrimes la = arplookup(itaddr.s_addr, 0, SIN_PROXY); 7693282Swollman if (la == NULL) { 7703282Swollman struct sockaddr_in sin; 7713282Swollman 77212693Sphk if (!arp_proxyall) { 77312693Sphk m_freem(m); 77412693Sphk return; 77512693Sphk } 7763282Swollman 7773282Swollman bzero(&sin, sizeof sin); 7783282Swollman sin.sin_family = AF_INET; 7793282Swollman sin.sin_len = sizeof sin; 7803282Swollman sin.sin_addr = itaddr; 7813282Swollman 7825101Swollman rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 78312693Sphk if (!rt) { 78412693Sphk m_freem(m); 78512693Sphk return; 78612693Sphk } 7873282Swollman /* 7883282Swollman * Don't send proxies for nodes on the same interface 7893282Swollman * as this one came out of, or we'll get into a fight 7903282Swollman * over who claims what Ether address. 7913282Swollman */ 79284931Sfjoe if (rt->rt_ifp == ifp) { 7933282Swollman rtfree(rt); 79412693Sphk m_freem(m); 79512693Sphk return; 7963282Swollman } 79784931Sfjoe (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 79884931Sfjoe (void)memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln); 7993282Swollman rtfree(rt); 80063080Sdwmalone 80163080Sdwmalone /* 80263080Sdwmalone * Also check that the node which sent the ARP packet 80363080Sdwmalone * is on the the interface we expect it to be on. This 80463080Sdwmalone * avoids ARP chaos if an interface is connected to the 80563080Sdwmalone * wrong network. 80663080Sdwmalone */ 80763080Sdwmalone sin.sin_addr = isaddr; 80863080Sdwmalone 80963080Sdwmalone rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 81063080Sdwmalone if (!rt) { 81163080Sdwmalone m_freem(m); 81263080Sdwmalone return; 81363080Sdwmalone } 81484931Sfjoe if (rt->rt_ifp != ifp) { 81563080Sdwmalone log(LOG_INFO, "arp_proxy: ignoring request" 81663080Sdwmalone " from %s via %s%d, expecting %s%d\n", 81784931Sfjoe inet_ntoa(isaddr), ifp->if_name, 81884931Sfjoe ifp->if_unit, rt->rt_ifp->if_name, 81963080Sdwmalone rt->rt_ifp->if_unit); 82063080Sdwmalone rtfree(rt); 82163080Sdwmalone m_freem(m); 82263080Sdwmalone return; 82363080Sdwmalone } 82463080Sdwmalone rtfree(rt); 82563080Sdwmalone 8264069Swollman#ifdef DEBUG_PROXY 8278876Srgrimes printf("arp: proxying for %s\n", 8287088Swollman inet_ntoa(itaddr)); 8294069Swollman#endif 8303282Swollman } else { 8313282Swollman rt = la->la_rt; 83284931Sfjoe (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 8333282Swollman sdl = SDL(rt->rt_gateway); 83484931Sfjoe (void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln); 8353282Swollman } 8361541Srgrimes } 8371541Srgrimes 83884931Sfjoe (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln); 83984931Sfjoe (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln); 84084931Sfjoe ah->ar_op = htons(ARPOP_REPLY); 84184931Sfjoe ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 84284931Sfjoe switch (ifp->if_type) { 84384931Sfjoe case IFT_ARCNET: 84484931Sfjoe arh = (struct arc_header *)sa.sa_data; 84584931Sfjoe arh->arc_dhost = *ar_tha(ah); 84684931Sfjoe arh->arc_type = ARCTYPE_ARP; 84784931Sfjoe break; 84884931Sfjoe 84945705Seivind case IFT_ISO88025: 85044627Sjulian /* Re-arrange the source/dest address */ 85151320Slile memcpy(th->iso88025_dhost, th->iso88025_shost, 85251320Slile sizeof(th->iso88025_dhost)); 85384931Sfjoe memcpy(th->iso88025_shost, IF_LLADDR(ifp), 85451320Slile sizeof(th->iso88025_shost)); 85544627Sjulian /* Set the source routing bit if neccesary */ 85658313Slile if (th->iso88025_dhost[0] & TR_RII) { 85758313Slile th->iso88025_dhost[0] &= ~TR_RII; 85858313Slile if (TR_RCF_RIFLEN(th->rcf) > 2) 85958313Slile th->iso88025_shost[0] |= TR_RII; 86044627Sjulian } 86144627Sjulian /* Copy the addresses, ac and fc into sa_data */ 86251320Slile memcpy(sa.sa_data, th->iso88025_dhost, 86351320Slile sizeof(th->iso88025_dhost) * 2); 86458313Slile sa.sa_data[(sizeof(th->iso88025_dhost) * 2)] = TR_AC; 86558313Slile sa.sa_data[(sizeof(th->iso88025_dhost) * 2) + 1] = TR_LLC_FRAME; 86644627Sjulian break; 86745705Seivind case IFT_ETHER: 86851320Slile case IFT_FDDI: 86951320Slile /* 87051320Slile * May not be correct for types not explictly 87151320Slile * listed, but it is our best guess. 87251320Slile */ 87351320Slile default: 87444627Sjulian eh = (struct ether_header *)sa.sa_data; 87584931Sfjoe (void)memcpy(eh->ether_dhost, ar_tha(ah), 87651320Slile sizeof(eh->ether_dhost)); 87744627Sjulian eh->ether_type = htons(ETHERTYPE_ARP); 87844627Sjulian break; 87944627Sjulian } 8801541Srgrimes sa.sa_family = AF_UNSPEC; 8811541Srgrimes sa.sa_len = sizeof(sa); 88284931Sfjoe (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 8831541Srgrimes return; 8841541Srgrimes} 88532350Seivind#endif 8861541Srgrimes 8871541Srgrimes/* 8881541Srgrimes * Free an arp entry. 8891541Srgrimes */ 8901541Srgrimesstatic void 8911541Srgrimesarptfree(la) 8921541Srgrimes register struct llinfo_arp *la; 8931541Srgrimes{ 8941541Srgrimes register struct rtentry *rt = la->la_rt; 8951541Srgrimes register struct sockaddr_dl *sdl; 896120727Ssam 8971541Srgrimes if (rt == 0) 8981541Srgrimes panic("arptfree"); 8991541Srgrimes if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) && 9001541Srgrimes sdl->sdl_family == AF_LINK) { 9011541Srgrimes sdl->sdl_alen = 0; 902110308Sorion la->la_preempt = la->la_asked = 0; 903120727Ssam RT_LOCK(rt); /* XXX needed or move higher? */ 9041541Srgrimes rt->rt_flags &= ~RTF_REJECT; 905120727Ssam RT_UNLOCK(rt); 9061541Srgrimes return; 9071541Srgrimes } 9081541Srgrimes rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt), 9091541Srgrimes 0, (struct rtentry **)0); 9101541Srgrimes} 9111541Srgrimes/* 9121541Srgrimes * Lookup or enter a new address in arptab. 9131541Srgrimes */ 9141541Srgrimesstatic struct llinfo_arp * 9151541Srgrimesarplookup(addr, create, proxy) 9161541Srgrimes u_long addr; 9171541Srgrimes int create, proxy; 9181541Srgrimes{ 9191541Srgrimes register struct rtentry *rt; 920120727Ssam struct sockaddr_inarp sin; 9214069Swollman const char *why = 0; 9221541Srgrimes 923120727Ssam bzero(&sin, sizeof(sin)); 924120727Ssam sin.sin_len = sizeof(sin); 925120727Ssam sin.sin_family = AF_INET; 9261541Srgrimes sin.sin_addr.s_addr = addr; 927120727Ssam if (proxy) 928120727Ssam sin.sin_other = SIN_PROXY; 9295101Swollman rt = rtalloc1((struct sockaddr *)&sin, create, 0UL); 9301541Srgrimes if (rt == 0) 9311541Srgrimes return (0); 9324069Swollman 93312693Sphk if (rt->rt_flags & RTF_GATEWAY) 9344069Swollman why = "host is not on local network"; 93512693Sphk else if ((rt->rt_flags & RTF_LLINFO) == 0) 9364069Swollman why = "could not allocate llinfo"; 93712693Sphk else if (rt->rt_gateway->sa_family != AF_LINK) 9384069Swollman why = "gateway route is not ours"; 9394069Swollman 940120383Sbms if (why) { 941120727Ssam#define ISDYNCLONE(_rt) \ 942120727Ssam (((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED) 943120727Ssam if (create) 944120383Sbms log(LOG_DEBUG, "arplookup %s failed: %s\n", 945120383Sbms inet_ntoa(sin.sin_addr), why); 946120727Ssam /* 947120727Ssam * If there are no references to this Layer 2 route, 948120727Ssam * and it is a cloned route, and not static, and 949120727Ssam * arplookup() is creating the route, then purge 950120727Ssam * it from the routing table as it is probably bogus. 951120727Ssam */ 952120727Ssam RT_UNLOCK(rt); 953120727Ssam if (rt->rt_refcnt == 1 && ISDYNCLONE(rt)) { 954120727Ssam rtrequest(RTM_DELETE, 955120727Ssam (struct sockaddr *)rt_key(rt), 956120727Ssam rt->rt_gateway, rt_mask(rt), 957120727Ssam rt->rt_flags, 0); 958120383Sbms } 959120727Ssam RTFREE(rt); 960120383Sbms return (0); 961120727Ssam#undef ISDYNCLONE 962120727Ssam } else { 963120727Ssam rt->rt_refcnt--; 964120727Ssam RT_UNLOCK(rt); 965120727Ssam return ((struct llinfo_arp *)rt->rt_llinfo); 9661541Srgrimes } 9671541Srgrimes} 9681541Srgrimes 9695195Swollmanvoid 97084931Sfjoearp_ifinit(ifp, ifa) 97184931Sfjoe struct ifnet *ifp; 9725195Swollman struct ifaddr *ifa; 9735195Swollman{ 97425822Stegge if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 97584931Sfjoe arprequest(ifp, &IA_SIN(ifa)->sin_addr, 97684931Sfjoe &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp)); 9775195Swollman ifa->ifa_rtrequest = arp_rtrequest; 9785195Swollman ifa->ifa_flags |= RTF_CLONING; 9795195Swollman} 98069152Sjlemon 98169152Sjlemonstatic void 98269152Sjlemonarp_init(void) 98369152Sjlemon{ 98469152Sjlemon 98569152Sjlemon arpintrq.ifq_maxlen = 50; 98693818Sjhb mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF); 98798459Speter LIST_INIT(&llinfo_arp); 988120727Ssam callout_init(&arp_callout, CALLOUT_MPSAFE); 989111888Sjlemon netisr_register(NETISR_ARP, arpintr, &arpintrq); 99069152Sjlemon} 99169152SjlemonSYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0); 992