if_ether.c revision 82893
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 82893 2001-09-03 21:53:15Z alfred $ 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" 4532350Seivind 461541Srgrimes#include <sys/param.h> 4712693Sphk#include <sys/kernel.h> 4844078Sdfr#include <sys/queue.h> 4912693Sphk#include <sys/sysctl.h> 501541Srgrimes#include <sys/systm.h> 5112693Sphk#include <sys/mbuf.h> 521541Srgrimes#include <sys/malloc.h> 5318892Sbde#include <sys/socket.h> 541541Srgrimes#include <sys/syslog.h> 551541Srgrimes 561541Srgrimes#include <net/if.h> 571541Srgrimes#include <net/if_dl.h> 5844165Sjulian#include <net/if_types.h> 591541Srgrimes#include <net/route.h> 608426Swollman#include <net/netisr.h> 6158313Slile#include <net/if_llc.h> 6271963Sjulian#ifdef BRIDGE 6371963Sjulian#include <net/ethernet.h> 6471963Sjulian#include <net/bridge.h> 6571963Sjulian#endif 661541Srgrimes 671541Srgrimes#include <netinet/in.h> 681541Srgrimes#include <netinet/in_var.h> 691541Srgrimes#include <netinet/if_ether.h> 701541Srgrimes 7144627Sjulian#include <net/iso88025.h> 7244627Sjulian 731541Srgrimes#define SIN(s) ((struct sockaddr_in *)s) 741541Srgrimes#define SDL(s) ((struct sockaddr_dl *)s) 751541Srgrimes 7644078SdfrSYSCTL_DECL(_net_link_ether); 7712942SwollmanSYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, ""); 781541Srgrimes 7912693Sphk/* timer values */ 8012942Swollmanstatic int arpt_prune = (5*60*1); /* walk list every 5 minutes */ 8112942Swollmanstatic int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ 8212942Swollmanstatic int arpt_down = 20; /* once declared down, don't send for 20 sec */ 831541Srgrimes 8412942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW, 8512942Swollman &arpt_prune, 0, ""); 8612942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW, 8712942Swollman &arpt_keep, 0, ""); 8812942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW, 8912942Swollman &arpt_down, 0, ""); 9012693Sphk 911541Srgrimes#define rt_expire rt_rmx.rmx_expire 921541Srgrimes 9311225Swollmanstruct llinfo_arp { 9460938Sjake LIST_ENTRY(llinfo_arp) la_le; 9511225Swollman struct rtentry *la_rt; 9611225Swollman struct mbuf *la_hold; /* last packet until resolved/timeout */ 9711225Swollman long la_asked; /* last time we QUERIED for this addr */ 9811225Swollman#define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */ 9911225Swollman}; 10012693Sphk 10160938Sjakestatic LIST_HEAD(, llinfo_arp) llinfo_arp; 10211225Swollman 10369152Sjlemonstruct ifqueue arpintrq; 10412820Sphkstatic int arp_inuse, arp_allocated; 1051541Srgrimes 10612693Sphkstatic int arp_maxtries = 5; 10712942Swollmanstatic int useloopback = 1; /* use loopback interface for local traffic */ 10812942Swollmanstatic int arp_proxyall = 0; 1093282Swollman 11012942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW, 11112942Swollman &arp_maxtries, 0, ""); 11212942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW, 11312942Swollman &useloopback, 0, ""); 11412942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW, 11512942Swollman &arp_proxyall, 0, ""); 11612693Sphk 11769152Sjlemonstatic void arp_init __P((void)); 11812877Sbdestatic void arp_rtrequest __P((int, struct rtentry *, struct sockaddr *)); 11936908Sjulianstatic void arprequest __P((struct arpcom *, 12036908Sjulian struct in_addr *, struct in_addr *, u_char *)); 12112693Sphkstatic void arpintr __P((void)); 12212693Sphkstatic void arptfree __P((struct llinfo_arp *)); 12312693Sphkstatic void arptimer __P((void *)); 12412693Sphkstatic struct llinfo_arp 12512693Sphk *arplookup __P((u_long, int, int)); 12632350Seivind#ifdef INET 12712693Sphkstatic void in_arpinput __P((struct mbuf *)); 12832350Seivind#endif 12912693Sphk 1301541Srgrimes/* 1311541Srgrimes * Timeout routine. Age arp_tab entries periodically. 1321541Srgrimes */ 1331541Srgrimes/* ARGSUSED */ 1341541Srgrimesstatic void 1351541Srgrimesarptimer(ignored_arg) 1361541Srgrimes void *ignored_arg; 1371541Srgrimes{ 1381541Srgrimes int s = splnet(); 13971999Sphk register struct llinfo_arp *la = LIST_FIRST(&llinfo_arp); 14011225Swollman struct llinfo_arp *ola; 1411541Srgrimes 1421541Srgrimes timeout(arptimer, (caddr_t)0, arpt_prune * hz); 14311225Swollman while ((ola = la) != 0) { 1441541Srgrimes register struct rtentry *rt = la->la_rt; 14571999Sphk la = LIST_NEXT(la, la_le); 14634961Sphk if (rt->rt_expire && rt->rt_expire <= time_second) 14711225Swollman arptfree(ola); /* timer has expired, clear */ 1481541Srgrimes } 1491541Srgrimes splx(s); 1501541Srgrimes} 1511541Srgrimes 1521541Srgrimes/* 1531541Srgrimes * Parallel to llc_rtrequest. 1541541Srgrimes */ 1555196Swollmanstatic void 1561541Srgrimesarp_rtrequest(req, rt, sa) 1571541Srgrimes int req; 1581541Srgrimes register struct rtentry *rt; 1591541Srgrimes struct sockaddr *sa; 1601541Srgrimes{ 1611541Srgrimes register struct sockaddr *gate = rt->rt_gateway; 1621541Srgrimes register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo; 1631541Srgrimes static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 16412693Sphk static int arpinit_done; 1651541Srgrimes 1661541Srgrimes if (!arpinit_done) { 1671541Srgrimes arpinit_done = 1; 16811225Swollman LIST_INIT(&llinfo_arp); 1691541Srgrimes timeout(arptimer, (caddr_t)0, hz); 17057178Speter register_netisr(NETISR_ARP, arpintr); 1711541Srgrimes } 1721541Srgrimes if (rt->rt_flags & RTF_GATEWAY) 1731541Srgrimes return; 1741541Srgrimes switch (req) { 1751541Srgrimes 1761541Srgrimes case RTM_ADD: 1771541Srgrimes /* 1781541Srgrimes * XXX: If this is a manually added route to interface 1791541Srgrimes * such as older version of routed or gated might provide, 1801541Srgrimes * restore cloning bit. 1811541Srgrimes */ 1821541Srgrimes if ((rt->rt_flags & RTF_HOST) == 0 && 1831541Srgrimes SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 1841541Srgrimes rt->rt_flags |= RTF_CLONING; 1851541Srgrimes if (rt->rt_flags & RTF_CLONING) { 1861541Srgrimes /* 1871541Srgrimes * Case 1: This route should come from a route to iface. 1881541Srgrimes */ 1891541Srgrimes rt_setgate(rt, rt_key(rt), 1901541Srgrimes (struct sockaddr *)&null_sdl); 1911541Srgrimes gate = rt->rt_gateway; 1921541Srgrimes SDL(gate)->sdl_type = rt->rt_ifp->if_type; 1931541Srgrimes SDL(gate)->sdl_index = rt->rt_ifp->if_index; 19434961Sphk rt->rt_expire = time_second; 1951541Srgrimes break; 1961541Srgrimes } 1971541Srgrimes /* Announce a new entry if requested. */ 1981541Srgrimes if (rt->rt_flags & RTF_ANNOUNCE) 1991541Srgrimes arprequest((struct arpcom *)rt->rt_ifp, 20036908Sjulian &SIN(rt_key(rt))->sin_addr, 20136908Sjulian &SIN(rt_key(rt))->sin_addr, 2021541Srgrimes (u_char *)LLADDR(SDL(gate))); 2031541Srgrimes /*FALLTHROUGH*/ 2041541Srgrimes case RTM_RESOLVE: 2051541Srgrimes if (gate->sa_family != AF_LINK || 2061541Srgrimes gate->sa_len < sizeof(null_sdl)) { 2076568Sdg log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n"); 2081541Srgrimes break; 2091541Srgrimes } 2101541Srgrimes SDL(gate)->sdl_type = rt->rt_ifp->if_type; 2111541Srgrimes SDL(gate)->sdl_index = rt->rt_ifp->if_index; 2121541Srgrimes if (la != 0) 2131541Srgrimes break; /* This happens on a route change */ 2141541Srgrimes /* 2151541Srgrimes * Case 2: This route may come from cloning, or a manual route 2161541Srgrimes * add with a LL address. 2171541Srgrimes */ 2181541Srgrimes R_Malloc(la, struct llinfo_arp *, sizeof(*la)); 2191541Srgrimes rt->rt_llinfo = (caddr_t)la; 2201541Srgrimes if (la == 0) { 2211541Srgrimes log(LOG_DEBUG, "arp_rtrequest: malloc failed\n"); 2221541Srgrimes break; 2231541Srgrimes } 2241541Srgrimes arp_inuse++, arp_allocated++; 2251541Srgrimes Bzero(la, sizeof(*la)); 2261541Srgrimes la->la_rt = rt; 2271541Srgrimes rt->rt_flags |= RTF_LLINFO; 22811225Swollman LIST_INSERT_HEAD(&llinfo_arp, la, la_le); 22913926Swollman 23032350Seivind#ifdef INET 23113926Swollman /* 23213926Swollman * This keeps the multicast addresses from showing up 23313926Swollman * in `arp -a' listings as unresolved. It's not actually 23413926Swollman * functional. Then the same for broadcast. 23513926Swollman */ 23613926Swollman if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) { 23713926Swollman ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr, 23813926Swollman LLADDR(SDL(gate))); 23913926Swollman SDL(gate)->sdl_alen = 6; 24016576Speter rt->rt_expire = 0; 24113926Swollman } 24213926Swollman if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) { 24313926Swollman memcpy(LLADDR(SDL(gate)), etherbroadcastaddr, 6); 24413926Swollman SDL(gate)->sdl_alen = 6; 24516576Speter rt->rt_expire = 0; 24613926Swollman } 24732350Seivind#endif 24813926Swollman 2491541Srgrimes if (SIN(rt_key(rt))->sin_addr.s_addr == 2501541Srgrimes (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) { 2511541Srgrimes /* 2521541Srgrimes * This test used to be 2531541Srgrimes * if (loif.if_flags & IFF_UP) 2541541Srgrimes * It allowed local traffic to be forced 2551541Srgrimes * through the hardware by configuring the loopback down. 2561541Srgrimes * However, it causes problems during network configuration 2571541Srgrimes * for boards that can't receive packets they send. 2581541Srgrimes * It is now necessary to clear "useloopback" and remove 2591541Srgrimes * the route to force traffic out to the hardware. 2601541Srgrimes */ 2611541Srgrimes rt->rt_expire = 0; 2621541Srgrimes Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr, 2631541Srgrimes LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6); 2641541Srgrimes if (useloopback) 2658090Spst rt->rt_ifp = loif; 2661541Srgrimes 2671541Srgrimes } 2681541Srgrimes break; 2691541Srgrimes 2701541Srgrimes case RTM_DELETE: 2711541Srgrimes if (la == 0) 2721541Srgrimes break; 2731541Srgrimes arp_inuse--; 27411225Swollman LIST_REMOVE(la, la_le); 2751541Srgrimes rt->rt_llinfo = 0; 2761541Srgrimes rt->rt_flags &= ~RTF_LLINFO; 2771541Srgrimes if (la->la_hold) 2781541Srgrimes m_freem(la->la_hold); 2791541Srgrimes Free((caddr_t)la); 2801541Srgrimes } 2811541Srgrimes} 2821541Srgrimes 2831541Srgrimes/* 2841541Srgrimes * Broadcast an ARP request. Caller specifies: 2851541Srgrimes * - arp header source ip address 2861541Srgrimes * - arp header target ip address 2871541Srgrimes * - arp header source ethernet address 2881541Srgrimes */ 2891541Srgrimesstatic void 2901541Srgrimesarprequest(ac, sip, tip, enaddr) 2911541Srgrimes register struct arpcom *ac; 29236908Sjulian register struct in_addr *sip, *tip; 2931541Srgrimes register u_char *enaddr; 2941541Srgrimes{ 2951541Srgrimes register struct mbuf *m; 2961541Srgrimes register struct ether_header *eh; 2971541Srgrimes register struct ether_arp *ea; 2981541Srgrimes struct sockaddr sa; 29958313Slile static u_char llcx[] = { 0x82, 0x40, LLC_SNAP_LSAP, LLC_SNAP_LSAP, 30058313Slile LLC_UI, 0x00, 0x00, 0x00, 0x08, 0x06 }; 3011541Srgrimes 3021541Srgrimes if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 3031541Srgrimes return; 30444456Swpaul m->m_pkthdr.rcvif = (struct ifnet *)0; 30544627Sjulian switch (ac->ac_if.if_type) { 30644627Sjulian case IFT_ISO88025: 30758313Slile m->m_len = sizeof(*ea) + sizeof(llcx); 30858313Slile m->m_pkthdr.len = sizeof(*ea) + sizeof(llcx); 30958313Slile MH_ALIGN(m, sizeof(*ea) + sizeof(llcx)); 31058313Slile (void)memcpy(mtod(m, caddr_t), llcx, sizeof(llcx)); 31144627Sjulian (void)memcpy(sa.sa_data, etherbroadcastaddr, 6); 31244627Sjulian (void)memcpy(sa.sa_data + 6, enaddr, 6); 31358313Slile sa.sa_data[6] |= TR_RII; 31458313Slile sa.sa_data[12] = TR_AC; 31558313Slile sa.sa_data[13] = TR_LLC_FRAME; 31658313Slile ea = (struct ether_arp *)(mtod(m, char *) + sizeof(llcx)); 31744627Sjulian bzero((caddr_t)ea, sizeof (*ea)); 31844627Sjulian ea->arp_hrd = htons(ARPHRD_IEEE802); 31944627Sjulian break; 32051320Slile case IFT_FDDI: 32151320Slile case IFT_ETHER: 32251320Slile /* 32351320Slile * This may not be correct for types not explicitly 32451320Slile * listed, but this is our best guess 32551320Slile */ 32644627Sjulian default: 32751320Slile m->m_len = sizeof(*ea); 32851320Slile m->m_pkthdr.len = sizeof(*ea); 32951320Slile MH_ALIGN(m, sizeof(*ea)); 33051320Slile ea = mtod(m, struct ether_arp *); 33151320Slile eh = (struct ether_header *)sa.sa_data; 33251320Slile bzero((caddr_t)ea, sizeof (*ea)); 33351320Slile /* if_output will not swap */ 33451320Slile eh->ether_type = htons(ETHERTYPE_ARP); 33551320Slile (void)memcpy(eh->ether_dhost, etherbroadcastaddr, 33651320Slile sizeof(eh->ether_dhost)); 33751320Slile ea->arp_hrd = htons(ARPHRD_ETHER); 33851320Slile break; 33944627Sjulian } 3401541Srgrimes ea->arp_pro = htons(ETHERTYPE_IP); 3411541Srgrimes ea->arp_hln = sizeof(ea->arp_sha); /* hardware address length */ 3421541Srgrimes ea->arp_pln = sizeof(ea->arp_spa); /* protocol address length */ 3431541Srgrimes ea->arp_op = htons(ARPOP_REQUEST); 3448384Sdg (void)memcpy(ea->arp_sha, enaddr, sizeof(ea->arp_sha)); 3458384Sdg (void)memcpy(ea->arp_spa, sip, sizeof(ea->arp_spa)); 3468384Sdg (void)memcpy(ea->arp_tpa, tip, sizeof(ea->arp_tpa)); 3471541Srgrimes sa.sa_family = AF_UNSPEC; 3481541Srgrimes sa.sa_len = sizeof(sa); 3491541Srgrimes (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0); 3501541Srgrimes} 3511541Srgrimes 3521541Srgrimes/* 3531541Srgrimes * Resolve an IP address into an ethernet address. If success, 3541541Srgrimes * desten is filled in. If there is no entry in arptab, 3551541Srgrimes * set one up and broadcast a request for the IP address. 3561541Srgrimes * Hold onto this mbuf and resend it once the address 3571541Srgrimes * is finally resolved. A return value of 1 indicates 3581541Srgrimes * that desten has been filled in and the packet should be sent 3591541Srgrimes * normally; a 0 return indicates that the packet has been 3601541Srgrimes * taken over here, either now or for later transmission. 3611541Srgrimes */ 3621541Srgrimesint 3633514Swollmanarpresolve(ac, rt, m, dst, desten, rt0) 3641541Srgrimes register struct arpcom *ac; 3651541Srgrimes register struct rtentry *rt; 3661541Srgrimes struct mbuf *m; 3671541Srgrimes register struct sockaddr *dst; 3681541Srgrimes register u_char *desten; 3693514Swollman struct rtentry *rt0; 3701541Srgrimes{ 37178295Sjlemon struct llinfo_arp *la = 0; 3721541Srgrimes struct sockaddr_dl *sdl; 3731541Srgrimes 3741541Srgrimes if (m->m_flags & M_BCAST) { /* broadcast */ 3758384Sdg (void)memcpy(desten, etherbroadcastaddr, sizeof(etherbroadcastaddr)); 3761541Srgrimes return (1); 3771541Srgrimes } 3781541Srgrimes if (m->m_flags & M_MCAST) { /* multicast */ 3791541Srgrimes ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 3801541Srgrimes return(1); 3811541Srgrimes } 3821541Srgrimes if (rt) 3831541Srgrimes la = (struct llinfo_arp *)rt->rt_llinfo; 38442775Sfenner if (la == 0) { 3853311Sphk la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0); 3863311Sphk if (la) 3871541Srgrimes rt = la->la_rt; 3881541Srgrimes } 3891541Srgrimes if (la == 0 || rt == 0) { 39036308Sphk log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n", 39136308Sphk inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "", 39236308Sphk rt ? "rt" : ""); 3931541Srgrimes m_freem(m); 3941541Srgrimes return (0); 3951541Srgrimes } 3961541Srgrimes sdl = SDL(rt->rt_gateway); 3971541Srgrimes /* 3981541Srgrimes * Check the address family and length is valid, the address 3991541Srgrimes * is resolved; otherwise, try to resolve. 4001541Srgrimes */ 40134961Sphk if ((rt->rt_expire == 0 || rt->rt_expire > time_second) && 4021541Srgrimes sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { 40316206Sbde bcopy(LLADDR(sdl), desten, sdl->sdl_alen); 4041541Srgrimes return 1; 4051541Srgrimes } 4061541Srgrimes /* 40778295Sjlemon * If ARP is disabled on this interface, stop. 40878295Sjlemon * XXX 40978295Sjlemon * Probably should not allocate empty llinfo struct if we are 41078295Sjlemon * not going to be sending out an arp request. 41178295Sjlemon */ 41278295Sjlemon if (ac->ac_if.if_flags & IFF_NOARP) 41378295Sjlemon return (0); 41478295Sjlemon /* 4151541Srgrimes * There is an arptab entry, but no ethernet address 4161541Srgrimes * response yet. Replace the held mbuf with this 4171541Srgrimes * latest one. 4181541Srgrimes */ 4191541Srgrimes if (la->la_hold) 4201541Srgrimes m_freem(la->la_hold); 4211541Srgrimes la->la_hold = m; 4221541Srgrimes if (rt->rt_expire) { 4231541Srgrimes rt->rt_flags &= ~RTF_REJECT; 42434961Sphk if (la->la_asked == 0 || rt->rt_expire != time_second) { 42534961Sphk rt->rt_expire = time_second; 4261541Srgrimes if (la->la_asked++ < arp_maxtries) 42714761Sfenner arprequest(ac, 42836908Sjulian &SIN(rt->rt_ifa->ifa_addr)->sin_addr, 42936908Sjulian &SIN(dst)->sin_addr, ac->ac_enaddr); 4301541Srgrimes else { 4311541Srgrimes rt->rt_flags |= RTF_REJECT; 4321541Srgrimes rt->rt_expire += arpt_down; 4331541Srgrimes la->la_asked = 0; 4341541Srgrimes } 4351541Srgrimes 4361541Srgrimes } 4371541Srgrimes } 4381541Srgrimes return (0); 4391541Srgrimes} 4401541Srgrimes 4411541Srgrimes/* 4421541Srgrimes * Common length and type checks are done here, 4431541Srgrimes * then the protocol-specific routine is called. 4441541Srgrimes */ 44512693Sphkstatic void 44631884Sbdearpintr() 4471541Srgrimes{ 44859143Swes register struct mbuf *m; 4491541Srgrimes register struct arphdr *ar; 45059143Swes int s; 4511541Srgrimes 4521541Srgrimes while (arpintrq.ifq_head) { 4531541Srgrimes s = splimp(); 4541541Srgrimes IF_DEQUEUE(&arpintrq, m); 4551541Srgrimes splx(s); 4561541Srgrimes if (m == 0 || (m->m_flags & M_PKTHDR) == 0) 4571541Srgrimes panic("arpintr"); 45857900Srwatson 45957900Srwatson if (m->m_len < sizeof(struct arphdr) && 46058499Sdillon ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) { 46158758Sjoerg log(LOG_ERR, "arp: runt packet -- m_pullup failed\n"); 46257900Srwatson continue; 46357900Srwatson } 46457900Srwatson ar = mtod(m, struct arphdr *); 4651541Srgrimes 46657900Srwatson if (ntohs(ar->ar_hrd) != ARPHRD_ETHER 46757900Srwatson && ntohs(ar->ar_hrd) != ARPHRD_IEEE802) { 46857900Srwatson log(LOG_ERR, 46958758Sjoerg "arp: unknown hardware address format (0x%2D)\n", 47057900Srwatson (unsigned char *)&ar->ar_hrd, ""); 47157900Srwatson m_freem(m); 47257900Srwatson continue; 47357900Srwatson } 4741541Srgrimes 47559143Swes if (m->m_pkthdr.len < sizeof(struct arphdr) + 2 * ar->ar_hln 47657900Srwatson + 2 * ar->ar_pln) { 47758770Sjoerg log(LOG_ERR, "arp: runt packet\n"); 47857900Srwatson m_freem(m); 47957900Srwatson continue; 48057900Srwatson } 48157900Srwatson 48257900Srwatson switch (ntohs(ar->ar_pro)) { 48332350Seivind#ifdef INET 48457900Srwatson case ETHERTYPE_IP: 48557900Srwatson in_arpinput(m); 48657900Srwatson continue; 48732350Seivind#endif 48857900Srwatson } 4891541Srgrimes m_freem(m); 4901541Srgrimes } 4911541Srgrimes} 4921541Srgrimes 49332350Seivind#ifdef INET 4941541Srgrimes/* 4951541Srgrimes * ARP for Internet protocols on 10 Mb/s Ethernet. 4961541Srgrimes * Algorithm is that given in RFC 826. 4971541Srgrimes * In addition, a sanity check is performed on the sender 4981541Srgrimes * protocol address, to catch impersonators. 4991541Srgrimes * We no longer handle negotiations for use of trailer protocol: 5001541Srgrimes * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 5011541Srgrimes * along with IP replies if we wanted trailers sent to us, 5021541Srgrimes * and also sent them in response to IP replies. 5031541Srgrimes * This allowed either end to announce the desire to receive 5041541Srgrimes * trailer packets. 5051541Srgrimes * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 5061541Srgrimes * but formerly didn't normally send requests. 5071541Srgrimes */ 50870699Salfredstatic int log_arp_wrong_iface = 1; 50982893Salfredstatic int log_arp_movements = 1; 51070699Salfred 51170699SalfredSYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW, 51270699Salfred &log_arp_wrong_iface, 0, 51370699Salfred "log arp packets arriving on the wrong interface"); 51482893SalfredSYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW, 51582893Salfred &log_arp_movements, 0, 51682893Salfred "log arp replies from MACs different the the one in the cache"); 51770699Salfred 51882893Salfred 5191541Srgrimesstatic void 5201541Srgrimesin_arpinput(m) 5211541Srgrimes struct mbuf *m; 5221541Srgrimes{ 5231541Srgrimes register struct ether_arp *ea; 5241541Srgrimes register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif; 5251541Srgrimes struct ether_header *eh; 52644627Sjulian struct iso88025_header *th = (struct iso88025_header *)0; 5271541Srgrimes register struct llinfo_arp *la = 0; 5281541Srgrimes register struct rtentry *rt; 5291541Srgrimes struct in_ifaddr *ia, *maybe_ia = 0; 5301541Srgrimes struct sockaddr_dl *sdl; 5311541Srgrimes struct sockaddr sa; 5321541Srgrimes struct in_addr isaddr, itaddr, myaddr; 53358313Slile int op, rif_len; 5341541Srgrimes 53574851Syar if (m->m_len < sizeof(struct ether_arp) && 53674851Syar (m = m_pullup(m, sizeof(struct ether_arp))) == NULL) { 53774851Syar log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n"); 53874851Syar return; 53974851Syar } 54074851Syar 5411541Srgrimes ea = mtod(m, struct ether_arp *); 5421541Srgrimes op = ntohs(ea->arp_op); 5438384Sdg (void)memcpy(&isaddr, ea->arp_spa, sizeof (isaddr)); 5448384Sdg (void)memcpy(&itaddr, ea->arp_tpa, sizeof (itaddr)); 54572056Sjulian TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { 54641793Sluigi /* 54741793Sluigi * For a bridge, we want to check the address irrespective 54841793Sluigi * of the receive interface. (This will change slightly 54941793Sluigi * when we have clusters of interfaces). 55041793Sluigi */ 55172056Sjulian#ifdef BRIDGE 55272056Sjulian#define BRIDGE_TEST (do_bridge) 55371963Sjulian#else 55472056Sjulian#define BRIDGE_TEST (0) /* cc will optimise the test away */ 55541793Sluigi#endif 55672056Sjulian if ((BRIDGE_TEST) || (ia->ia_ifp == &ac->ac_if)) { 55772056Sjulian maybe_ia = ia; 55872056Sjulian if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) || 55972056Sjulian (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)) { 56072056Sjulian break; 56171963Sjulian } 5621541Srgrimes } 56372056Sjulian } 56412693Sphk if (maybe_ia == 0) { 56512693Sphk m_freem(m); 56612693Sphk return; 56712693Sphk } 5681541Srgrimes myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr; 5691541Srgrimes if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr, 57012693Sphk sizeof (ea->arp_sha))) { 57112693Sphk m_freem(m); /* it's from me, ignore it. */ 57212693Sphk return; 57312693Sphk } 5741541Srgrimes if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr, 5751541Srgrimes sizeof (ea->arp_sha))) { 5761541Srgrimes log(LOG_ERR, 5774069Swollman "arp: ether address is broadcast for IP address %s!\n", 5787088Swollman inet_ntoa(isaddr)); 57912693Sphk m_freem(m); 58012693Sphk return; 5811541Srgrimes } 5821541Srgrimes if (isaddr.s_addr == myaddr.s_addr) { 5831541Srgrimes log(LOG_ERR, 58419794Sfenner "arp: %6D is using my IP address %s!\n", 58519794Sfenner ea->arp_sha, ":", inet_ntoa(isaddr)); 5861541Srgrimes itaddr = myaddr; 5871541Srgrimes goto reply; 5881541Srgrimes } 5891541Srgrimes la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0); 5901541Srgrimes if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) { 59172270Sluigi /* the following is not an error when doing bridging */ 59272270Sluigi if (!BRIDGE_TEST && rt->rt_ifp != &ac->ac_if) { 59372270Sluigi if (log_arp_wrong_iface) 59471963Sjulian log(LOG_ERR, "arp: %s is on %s%d but got reply from %6D on %s%d\n", 59571963Sjulian inet_ntoa(isaddr), 59671963Sjulian rt->rt_ifp->if_name, rt->rt_ifp->if_unit, 59771963Sjulian ea->arp_sha, ":", 59871963Sjulian ac->ac_if.if_name, ac->ac_if.if_unit); 59972270Sluigi goto reply; 60039389Sfenner } 6011541Srgrimes if (sdl->sdl_alen && 60246568Speter bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen)) { 60382893Salfred if (rt->rt_expire) { 60482893Salfred if (log_arp_movements) 60582893Salfred log(LOG_INFO, "arp: %s moved from %6D to %6D on %s%d\n", 60682893Salfred inet_ntoa(isaddr), (u_char *)LLADDR(sdl), ":", 60782893Salfred ea->arp_sha, ":", 60882893Salfred ac->ac_if.if_name, ac->ac_if.if_unit); 60982893Salfred } else { 61039389Sfenner log(LOG_ERR, 61152377Ssheldonh "arp: %6D attempts to modify permanent entry for %s on %s%d\n", 61239389Sfenner ea->arp_sha, ":", inet_ntoa(isaddr), 61339389Sfenner ac->ac_if.if_name, ac->ac_if.if_unit); 61439389Sfenner goto reply; 61539389Sfenner } 61646568Speter } 6178384Sdg (void)memcpy(LLADDR(sdl), ea->arp_sha, sizeof(ea->arp_sha)); 6188384Sdg sdl->sdl_alen = sizeof(ea->arp_sha); 61958313Slile sdl->sdl_rcf = (u_short)0; 62045705Seivind /* 62145705Seivind * If we receive an arp from a token-ring station over 62245705Seivind * a token-ring nic then try to save the source 62345705Seivind * routing info. 62445705Seivind */ 62545705Seivind if (ac->ac_if.if_type == IFT_ISO88025) { 62644627Sjulian th = (struct iso88025_header *)m->m_pkthdr.header; 62758313Slile rif_len = TR_RCF_RIFLEN(th->rcf); 62858313Slile if ((th->iso88025_shost[0] & TR_RII) && 62958313Slile (rif_len > 2)) { 63058313Slile sdl->sdl_rcf = th->rcf; 63158313Slile sdl->sdl_rcf ^= htons(TR_RCF_DIR); 63258313Slile memcpy(sdl->sdl_route, th->rd, rif_len - 2); 63358313Slile sdl->sdl_rcf &= ~htons(TR_RCF_BCST_MASK); 63451320Slile /* 63551320Slile * Set up source routing information for 63651320Slile * reply packet (XXX) 63751320Slile */ 63858313Slile m->m_data -= rif_len; 63958313Slile m->m_len += rif_len; 64058313Slile m->m_pkthdr.len += rif_len; 64144627Sjulian } else { 64258313Slile th->iso88025_shost[0] &= ~TR_RII; 64344627Sjulian } 64450512Slile m->m_data -= 8; 64550512Slile m->m_len += 8; 64650512Slile m->m_pkthdr.len += 8; 64744627Sjulian th->rcf = sdl->sdl_rcf; 64845705Seivind } else { 64958313Slile sdl->sdl_rcf = (u_short)0; 65044627Sjulian } 6511541Srgrimes if (rt->rt_expire) 65234961Sphk rt->rt_expire = time_second + arpt_keep; 6531541Srgrimes rt->rt_flags &= ~RTF_REJECT; 6541541Srgrimes la->la_asked = 0; 6551541Srgrimes if (la->la_hold) { 6561541Srgrimes (*ac->ac_if.if_output)(&ac->ac_if, la->la_hold, 6571541Srgrimes rt_key(rt), rt); 6581541Srgrimes la->la_hold = 0; 6591541Srgrimes } 6601541Srgrimes } 6611541Srgrimesreply: 6621541Srgrimes if (op != ARPOP_REQUEST) { 6631541Srgrimes m_freem(m); 6641541Srgrimes return; 6651541Srgrimes } 6661541Srgrimes if (itaddr.s_addr == myaddr.s_addr) { 6671541Srgrimes /* I am the target */ 6688384Sdg (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha)); 6698384Sdg (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha)); 6701541Srgrimes } else { 6711541Srgrimes la = arplookup(itaddr.s_addr, 0, SIN_PROXY); 6723282Swollman if (la == NULL) { 6733282Swollman struct sockaddr_in sin; 6743282Swollman 67512693Sphk if (!arp_proxyall) { 67612693Sphk m_freem(m); 67712693Sphk return; 67812693Sphk } 6793282Swollman 6803282Swollman bzero(&sin, sizeof sin); 6813282Swollman sin.sin_family = AF_INET; 6823282Swollman sin.sin_len = sizeof sin; 6833282Swollman sin.sin_addr = itaddr; 6843282Swollman 6855101Swollman rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 68612693Sphk if (!rt) { 68712693Sphk m_freem(m); 68812693Sphk return; 68912693Sphk } 6903282Swollman /* 6913282Swollman * Don't send proxies for nodes on the same interface 6923282Swollman * as this one came out of, or we'll get into a fight 6933282Swollman * over who claims what Ether address. 6943282Swollman */ 69512693Sphk if (rt->rt_ifp == &ac->ac_if) { 6963282Swollman rtfree(rt); 69712693Sphk m_freem(m); 69812693Sphk return; 6993282Swollman } 7008384Sdg (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha)); 7018384Sdg (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha)); 7023282Swollman rtfree(rt); 70363080Sdwmalone 70463080Sdwmalone /* 70563080Sdwmalone * Also check that the node which sent the ARP packet 70663080Sdwmalone * is on the the interface we expect it to be on. This 70763080Sdwmalone * avoids ARP chaos if an interface is connected to the 70863080Sdwmalone * wrong network. 70963080Sdwmalone */ 71063080Sdwmalone sin.sin_addr = isaddr; 71163080Sdwmalone 71263080Sdwmalone rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 71363080Sdwmalone if (!rt) { 71463080Sdwmalone m_freem(m); 71563080Sdwmalone return; 71663080Sdwmalone } 71763080Sdwmalone if (rt->rt_ifp != &ac->ac_if) { 71863080Sdwmalone log(LOG_INFO, "arp_proxy: ignoring request" 71963080Sdwmalone " from %s via %s%d, expecting %s%d\n", 72063080Sdwmalone inet_ntoa(isaddr), ac->ac_if.if_name, 72163080Sdwmalone ac->ac_if.if_unit, rt->rt_ifp->if_name, 72263080Sdwmalone rt->rt_ifp->if_unit); 72363080Sdwmalone rtfree(rt); 72463080Sdwmalone m_freem(m); 72563080Sdwmalone return; 72663080Sdwmalone } 72763080Sdwmalone rtfree(rt); 72863080Sdwmalone 7294069Swollman#ifdef DEBUG_PROXY 7308876Srgrimes printf("arp: proxying for %s\n", 7317088Swollman inet_ntoa(itaddr)); 7324069Swollman#endif 7333282Swollman } else { 7343282Swollman rt = la->la_rt; 7358384Sdg (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha)); 7363282Swollman sdl = SDL(rt->rt_gateway); 7378384Sdg (void)memcpy(ea->arp_sha, LLADDR(sdl), sizeof(ea->arp_sha)); 7383282Swollman } 7391541Srgrimes } 7401541Srgrimes 7418384Sdg (void)memcpy(ea->arp_tpa, ea->arp_spa, sizeof(ea->arp_spa)); 7428384Sdg (void)memcpy(ea->arp_spa, &itaddr, sizeof(ea->arp_spa)); 7431541Srgrimes ea->arp_op = htons(ARPOP_REPLY); 7441541Srgrimes ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 74545705Seivind switch (ac->ac_if.if_type) { 74645705Seivind case IFT_ISO88025: 74744627Sjulian /* Re-arrange the source/dest address */ 74851320Slile memcpy(th->iso88025_dhost, th->iso88025_shost, 74951320Slile sizeof(th->iso88025_dhost)); 75051320Slile memcpy(th->iso88025_shost, ac->ac_enaddr, 75151320Slile sizeof(th->iso88025_shost)); 75244627Sjulian /* Set the source routing bit if neccesary */ 75358313Slile if (th->iso88025_dhost[0] & TR_RII) { 75458313Slile th->iso88025_dhost[0] &= ~TR_RII; 75558313Slile if (TR_RCF_RIFLEN(th->rcf) > 2) 75658313Slile th->iso88025_shost[0] |= TR_RII; 75744627Sjulian } 75844627Sjulian /* Copy the addresses, ac and fc into sa_data */ 75951320Slile memcpy(sa.sa_data, th->iso88025_dhost, 76051320Slile sizeof(th->iso88025_dhost) * 2); 76158313Slile sa.sa_data[(sizeof(th->iso88025_dhost) * 2)] = TR_AC; 76258313Slile sa.sa_data[(sizeof(th->iso88025_dhost) * 2) + 1] = TR_LLC_FRAME; 76344627Sjulian break; 76445705Seivind case IFT_ETHER: 76551320Slile case IFT_FDDI: 76651320Slile /* 76751320Slile * May not be correct for types not explictly 76851320Slile * listed, but it is our best guess. 76951320Slile */ 77051320Slile default: 77144627Sjulian eh = (struct ether_header *)sa.sa_data; 77251320Slile (void)memcpy(eh->ether_dhost, ea->arp_tha, 77351320Slile sizeof(eh->ether_dhost)); 77444627Sjulian eh->ether_type = htons(ETHERTYPE_ARP); 77544627Sjulian break; 77644627Sjulian } 7771541Srgrimes sa.sa_family = AF_UNSPEC; 7781541Srgrimes sa.sa_len = sizeof(sa); 7791541Srgrimes (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0); 7801541Srgrimes return; 7811541Srgrimes} 78232350Seivind#endif 7831541Srgrimes 7841541Srgrimes/* 7851541Srgrimes * Free an arp entry. 7861541Srgrimes */ 7871541Srgrimesstatic void 7881541Srgrimesarptfree(la) 7891541Srgrimes register struct llinfo_arp *la; 7901541Srgrimes{ 7911541Srgrimes register struct rtentry *rt = la->la_rt; 7921541Srgrimes register struct sockaddr_dl *sdl; 7931541Srgrimes if (rt == 0) 7941541Srgrimes panic("arptfree"); 7951541Srgrimes if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) && 7961541Srgrimes sdl->sdl_family == AF_LINK) { 7971541Srgrimes sdl->sdl_alen = 0; 7981541Srgrimes la->la_asked = 0; 7991541Srgrimes rt->rt_flags &= ~RTF_REJECT; 8001541Srgrimes return; 8011541Srgrimes } 8021541Srgrimes rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt), 8031541Srgrimes 0, (struct rtentry **)0); 8041541Srgrimes} 8051541Srgrimes/* 8061541Srgrimes * Lookup or enter a new address in arptab. 8071541Srgrimes */ 8081541Srgrimesstatic struct llinfo_arp * 8091541Srgrimesarplookup(addr, create, proxy) 8101541Srgrimes u_long addr; 8111541Srgrimes int create, proxy; 8121541Srgrimes{ 8131541Srgrimes register struct rtentry *rt; 8141541Srgrimes static struct sockaddr_inarp sin = {sizeof(sin), AF_INET }; 8154069Swollman const char *why = 0; 8161541Srgrimes 8171541Srgrimes sin.sin_addr.s_addr = addr; 8181541Srgrimes sin.sin_other = proxy ? SIN_PROXY : 0; 8195101Swollman rt = rtalloc1((struct sockaddr *)&sin, create, 0UL); 8201541Srgrimes if (rt == 0) 8211541Srgrimes return (0); 8221541Srgrimes rt->rt_refcnt--; 8234069Swollman 82412693Sphk if (rt->rt_flags & RTF_GATEWAY) 8254069Swollman why = "host is not on local network"; 82612693Sphk else if ((rt->rt_flags & RTF_LLINFO) == 0) 8274069Swollman why = "could not allocate llinfo"; 82812693Sphk else if (rt->rt_gateway->sa_family != AF_LINK) 8294069Swollman why = "gateway route is not ours"; 8304069Swollman 83112693Sphk if (why && create) { 8324069Swollman log(LOG_DEBUG, "arplookup %s failed: %s\n", 8337088Swollman inet_ntoa(sin.sin_addr), why); 8344069Swollman return 0; 83512693Sphk } else if (why) { 8364069Swollman return 0; 8371541Srgrimes } 8381541Srgrimes return ((struct llinfo_arp *)rt->rt_llinfo); 8391541Srgrimes} 8401541Srgrimes 8415195Swollmanvoid 8425195Swollmanarp_ifinit(ac, ifa) 8435195Swollman struct arpcom *ac; 8445195Swollman struct ifaddr *ifa; 8455195Swollman{ 84625822Stegge if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 84736908Sjulian arprequest(ac, &IA_SIN(ifa)->sin_addr, 84836908Sjulian &IA_SIN(ifa)->sin_addr, ac->ac_enaddr); 8495195Swollman ifa->ifa_rtrequest = arp_rtrequest; 8505195Swollman ifa->ifa_flags |= RTF_CLONING; 8515195Swollman} 85269152Sjlemon 85369152Sjlemonstatic void 85469152Sjlemonarp_init(void) 85569152Sjlemon{ 85669152Sjlemon 85769152Sjlemon arpintrq.ifq_maxlen = 50; 85869152Sjlemon mtx_init(&arpintrq.ifq_mtx, "arp_inq", MTX_DEF); 85969152Sjlemon} 86069152Sjlemon 86169152SjlemonSYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0); 862