if_ether.c revision 58313
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 58313 2000-03-19 21:34:39Z lile $ 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> 621541Srgrimes 631541Srgrimes#include <netinet/in.h> 641541Srgrimes#include <netinet/in_var.h> 651541Srgrimes#include <netinet/if_ether.h> 661541Srgrimes 6744627Sjulian#include <net/iso88025.h> 6844627Sjulian 691541Srgrimes#define SIN(s) ((struct sockaddr_in *)s) 701541Srgrimes#define SDL(s) ((struct sockaddr_dl *)s) 711541Srgrimes 7244078SdfrSYSCTL_DECL(_net_link_ether); 7312942SwollmanSYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, ""); 741541Srgrimes 7512693Sphk/* timer values */ 7612942Swollmanstatic int arpt_prune = (5*60*1); /* walk list every 5 minutes */ 7712942Swollmanstatic int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ 7812942Swollmanstatic int arpt_down = 20; /* once declared down, don't send for 20 sec */ 791541Srgrimes 8012942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW, 8112942Swollman &arpt_prune, 0, ""); 8212942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW, 8312942Swollman &arpt_keep, 0, ""); 8412942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW, 8512942Swollman &arpt_down, 0, ""); 8612693Sphk 871541Srgrimes#define rt_expire rt_rmx.rmx_expire 881541Srgrimes 8911225Swollmanstruct llinfo_arp { 9011225Swollman LIST_ENTRY(llinfo_arp) la_le; 9111225Swollman struct rtentry *la_rt; 9211225Swollman struct mbuf *la_hold; /* last packet until resolved/timeout */ 9311225Swollman long la_asked; /* last time we QUERIED for this addr */ 9411225Swollman#define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */ 9511225Swollman}; 9612693Sphk 9711225Swollmanstatic LIST_HEAD(, llinfo_arp) llinfo_arp; 9811225Swollman 991541Srgrimesstruct ifqueue arpintrq = {0, 0, 0, 50}; 10012820Sphkstatic int arp_inuse, arp_allocated; 1011541Srgrimes 10212693Sphkstatic int arp_maxtries = 5; 10312942Swollmanstatic int useloopback = 1; /* use loopback interface for local traffic */ 10412942Swollmanstatic int arp_proxyall = 0; 1053282Swollman 10612942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW, 10712942Swollman &arp_maxtries, 0, ""); 10812942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW, 10912942Swollman &useloopback, 0, ""); 11012942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW, 11112942Swollman &arp_proxyall, 0, ""); 11212693Sphk 11312877Sbdestatic void arp_rtrequest __P((int, struct rtentry *, struct sockaddr *)); 11436908Sjulianstatic void arprequest __P((struct arpcom *, 11536908Sjulian struct in_addr *, struct in_addr *, u_char *)); 11612693Sphkstatic void arpintr __P((void)); 11712693Sphkstatic void arptfree __P((struct llinfo_arp *)); 11812693Sphkstatic void arptimer __P((void *)); 11912693Sphkstatic struct llinfo_arp 12012693Sphk *arplookup __P((u_long, int, int)); 12132350Seivind#ifdef INET 12212693Sphkstatic void in_arpinput __P((struct mbuf *)); 12332350Seivind#endif 12412693Sphk 1251541Srgrimes/* 1261541Srgrimes * Timeout routine. Age arp_tab entries periodically. 1271541Srgrimes */ 1281541Srgrimes/* ARGSUSED */ 1291541Srgrimesstatic void 1301541Srgrimesarptimer(ignored_arg) 1311541Srgrimes void *ignored_arg; 1321541Srgrimes{ 1331541Srgrimes int s = splnet(); 13411225Swollman register struct llinfo_arp *la = llinfo_arp.lh_first; 13511225Swollman struct llinfo_arp *ola; 1361541Srgrimes 1371541Srgrimes timeout(arptimer, (caddr_t)0, arpt_prune * hz); 13811225Swollman while ((ola = la) != 0) { 1391541Srgrimes register struct rtentry *rt = la->la_rt; 14011225Swollman la = la->la_le.le_next; 14134961Sphk if (rt->rt_expire && rt->rt_expire <= time_second) 14211225Swollman arptfree(ola); /* timer has expired, clear */ 1431541Srgrimes } 1441541Srgrimes splx(s); 1451541Srgrimes} 1461541Srgrimes 1471541Srgrimes/* 1481541Srgrimes * Parallel to llc_rtrequest. 1491541Srgrimes */ 1505196Swollmanstatic void 1511541Srgrimesarp_rtrequest(req, rt, sa) 1521541Srgrimes int req; 1531541Srgrimes register struct rtentry *rt; 1541541Srgrimes struct sockaddr *sa; 1551541Srgrimes{ 1561541Srgrimes register struct sockaddr *gate = rt->rt_gateway; 1571541Srgrimes register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo; 1581541Srgrimes static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 15912693Sphk static int arpinit_done; 1601541Srgrimes 1611541Srgrimes if (!arpinit_done) { 1621541Srgrimes arpinit_done = 1; 16311225Swollman LIST_INIT(&llinfo_arp); 1641541Srgrimes timeout(arptimer, (caddr_t)0, hz); 16557178Speter register_netisr(NETISR_ARP, arpintr); 1661541Srgrimes } 1671541Srgrimes if (rt->rt_flags & RTF_GATEWAY) 1681541Srgrimes return; 1691541Srgrimes switch (req) { 1701541Srgrimes 1711541Srgrimes case RTM_ADD: 1721541Srgrimes /* 1731541Srgrimes * XXX: If this is a manually added route to interface 1741541Srgrimes * such as older version of routed or gated might provide, 1751541Srgrimes * restore cloning bit. 1761541Srgrimes */ 1771541Srgrimes if ((rt->rt_flags & RTF_HOST) == 0 && 1781541Srgrimes SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 1791541Srgrimes rt->rt_flags |= RTF_CLONING; 1801541Srgrimes if (rt->rt_flags & RTF_CLONING) { 1811541Srgrimes /* 1821541Srgrimes * Case 1: This route should come from a route to iface. 1831541Srgrimes */ 1841541Srgrimes rt_setgate(rt, rt_key(rt), 1851541Srgrimes (struct sockaddr *)&null_sdl); 1861541Srgrimes gate = rt->rt_gateway; 1871541Srgrimes SDL(gate)->sdl_type = rt->rt_ifp->if_type; 1881541Srgrimes SDL(gate)->sdl_index = rt->rt_ifp->if_index; 18934961Sphk rt->rt_expire = time_second; 1901541Srgrimes break; 1911541Srgrimes } 1921541Srgrimes /* Announce a new entry if requested. */ 1931541Srgrimes if (rt->rt_flags & RTF_ANNOUNCE) 1941541Srgrimes arprequest((struct arpcom *)rt->rt_ifp, 19536908Sjulian &SIN(rt_key(rt))->sin_addr, 19636908Sjulian &SIN(rt_key(rt))->sin_addr, 1971541Srgrimes (u_char *)LLADDR(SDL(gate))); 1981541Srgrimes /*FALLTHROUGH*/ 1991541Srgrimes case RTM_RESOLVE: 2001541Srgrimes if (gate->sa_family != AF_LINK || 2011541Srgrimes gate->sa_len < sizeof(null_sdl)) { 2026568Sdg log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n"); 2031541Srgrimes break; 2041541Srgrimes } 2051541Srgrimes SDL(gate)->sdl_type = rt->rt_ifp->if_type; 2061541Srgrimes SDL(gate)->sdl_index = rt->rt_ifp->if_index; 2071541Srgrimes if (la != 0) 2081541Srgrimes break; /* This happens on a route change */ 2091541Srgrimes /* 2101541Srgrimes * Case 2: This route may come from cloning, or a manual route 2111541Srgrimes * add with a LL address. 2121541Srgrimes */ 2131541Srgrimes R_Malloc(la, struct llinfo_arp *, sizeof(*la)); 2141541Srgrimes rt->rt_llinfo = (caddr_t)la; 2151541Srgrimes if (la == 0) { 2161541Srgrimes log(LOG_DEBUG, "arp_rtrequest: malloc failed\n"); 2171541Srgrimes break; 2181541Srgrimes } 2191541Srgrimes arp_inuse++, arp_allocated++; 2201541Srgrimes Bzero(la, sizeof(*la)); 2211541Srgrimes la->la_rt = rt; 2221541Srgrimes rt->rt_flags |= RTF_LLINFO; 22311225Swollman LIST_INSERT_HEAD(&llinfo_arp, la, la_le); 22413926Swollman 22532350Seivind#ifdef INET 22613926Swollman /* 22713926Swollman * This keeps the multicast addresses from showing up 22813926Swollman * in `arp -a' listings as unresolved. It's not actually 22913926Swollman * functional. Then the same for broadcast. 23013926Swollman */ 23113926Swollman if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) { 23213926Swollman ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr, 23313926Swollman LLADDR(SDL(gate))); 23413926Swollman SDL(gate)->sdl_alen = 6; 23516576Speter rt->rt_expire = 0; 23613926Swollman } 23713926Swollman if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) { 23813926Swollman memcpy(LLADDR(SDL(gate)), etherbroadcastaddr, 6); 23913926Swollman SDL(gate)->sdl_alen = 6; 24016576Speter rt->rt_expire = 0; 24113926Swollman } 24232350Seivind#endif 24313926Swollman 2441541Srgrimes if (SIN(rt_key(rt))->sin_addr.s_addr == 2451541Srgrimes (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) { 2461541Srgrimes /* 2471541Srgrimes * This test used to be 2481541Srgrimes * if (loif.if_flags & IFF_UP) 2491541Srgrimes * It allowed local traffic to be forced 2501541Srgrimes * through the hardware by configuring the loopback down. 2511541Srgrimes * However, it causes problems during network configuration 2521541Srgrimes * for boards that can't receive packets they send. 2531541Srgrimes * It is now necessary to clear "useloopback" and remove 2541541Srgrimes * the route to force traffic out to the hardware. 2551541Srgrimes */ 2561541Srgrimes rt->rt_expire = 0; 2571541Srgrimes Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr, 2581541Srgrimes LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6); 2591541Srgrimes if (useloopback) 2608090Spst rt->rt_ifp = loif; 2611541Srgrimes 2621541Srgrimes } 2631541Srgrimes break; 2641541Srgrimes 2651541Srgrimes case RTM_DELETE: 2661541Srgrimes if (la == 0) 2671541Srgrimes break; 2681541Srgrimes arp_inuse--; 26911225Swollman LIST_REMOVE(la, la_le); 2701541Srgrimes rt->rt_llinfo = 0; 2711541Srgrimes rt->rt_flags &= ~RTF_LLINFO; 2721541Srgrimes if (la->la_hold) 2731541Srgrimes m_freem(la->la_hold); 2741541Srgrimes Free((caddr_t)la); 2751541Srgrimes } 2761541Srgrimes} 2771541Srgrimes 2781541Srgrimes/* 2791541Srgrimes * Broadcast an ARP request. Caller specifies: 2801541Srgrimes * - arp header source ip address 2811541Srgrimes * - arp header target ip address 2821541Srgrimes * - arp header source ethernet address 2831541Srgrimes */ 2841541Srgrimesstatic void 2851541Srgrimesarprequest(ac, sip, tip, enaddr) 2861541Srgrimes register struct arpcom *ac; 28736908Sjulian register struct in_addr *sip, *tip; 2881541Srgrimes register u_char *enaddr; 2891541Srgrimes{ 2901541Srgrimes register struct mbuf *m; 2911541Srgrimes register struct ether_header *eh; 2921541Srgrimes register struct ether_arp *ea; 2931541Srgrimes struct sockaddr sa; 29458313Slile static u_char llcx[] = { 0x82, 0x40, LLC_SNAP_LSAP, LLC_SNAP_LSAP, 29558313Slile LLC_UI, 0x00, 0x00, 0x00, 0x08, 0x06 }; 2961541Srgrimes 2971541Srgrimes if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 2981541Srgrimes return; 29944456Swpaul m->m_pkthdr.rcvif = (struct ifnet *)0; 30044627Sjulian switch (ac->ac_if.if_type) { 30144627Sjulian case IFT_ISO88025: 30258313Slile m->m_len = sizeof(*ea) + sizeof(llcx); 30358313Slile m->m_pkthdr.len = sizeof(*ea) + sizeof(llcx); 30458313Slile MH_ALIGN(m, sizeof(*ea) + sizeof(llcx)); 30558313Slile (void)memcpy(mtod(m, caddr_t), llcx, sizeof(llcx)); 30644627Sjulian (void)memcpy(sa.sa_data, etherbroadcastaddr, 6); 30744627Sjulian (void)memcpy(sa.sa_data + 6, enaddr, 6); 30858313Slile sa.sa_data[6] |= TR_RII; 30958313Slile sa.sa_data[12] = TR_AC; 31058313Slile sa.sa_data[13] = TR_LLC_FRAME; 31158313Slile ea = (struct ether_arp *)(mtod(m, char *) + sizeof(llcx)); 31244627Sjulian bzero((caddr_t)ea, sizeof (*ea)); 31344627Sjulian ea->arp_hrd = htons(ARPHRD_IEEE802); 31444627Sjulian break; 31551320Slile case IFT_FDDI: 31651320Slile case IFT_ETHER: 31751320Slile /* 31851320Slile * This may not be correct for types not explicitly 31951320Slile * listed, but this is our best guess 32051320Slile */ 32144627Sjulian default: 32251320Slile m->m_len = sizeof(*ea); 32351320Slile m->m_pkthdr.len = sizeof(*ea); 32451320Slile MH_ALIGN(m, sizeof(*ea)); 32551320Slile ea = mtod(m, struct ether_arp *); 32651320Slile eh = (struct ether_header *)sa.sa_data; 32751320Slile bzero((caddr_t)ea, sizeof (*ea)); 32851320Slile /* if_output will not swap */ 32951320Slile eh->ether_type = htons(ETHERTYPE_ARP); 33051320Slile (void)memcpy(eh->ether_dhost, etherbroadcastaddr, 33151320Slile sizeof(eh->ether_dhost)); 33251320Slile ea->arp_hrd = htons(ARPHRD_ETHER); 33351320Slile break; 33444627Sjulian } 3351541Srgrimes ea->arp_pro = htons(ETHERTYPE_IP); 3361541Srgrimes ea->arp_hln = sizeof(ea->arp_sha); /* hardware address length */ 3371541Srgrimes ea->arp_pln = sizeof(ea->arp_spa); /* protocol address length */ 3381541Srgrimes ea->arp_op = htons(ARPOP_REQUEST); 3398384Sdg (void)memcpy(ea->arp_sha, enaddr, sizeof(ea->arp_sha)); 3408384Sdg (void)memcpy(ea->arp_spa, sip, sizeof(ea->arp_spa)); 3418384Sdg (void)memcpy(ea->arp_tpa, tip, sizeof(ea->arp_tpa)); 3421541Srgrimes sa.sa_family = AF_UNSPEC; 3431541Srgrimes sa.sa_len = sizeof(sa); 3441541Srgrimes (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0); 3451541Srgrimes} 3461541Srgrimes 3471541Srgrimes/* 3481541Srgrimes * Resolve an IP address into an ethernet address. If success, 3491541Srgrimes * desten is filled in. If there is no entry in arptab, 3501541Srgrimes * set one up and broadcast a request for the IP address. 3511541Srgrimes * Hold onto this mbuf and resend it once the address 3521541Srgrimes * is finally resolved. A return value of 1 indicates 3531541Srgrimes * that desten has been filled in and the packet should be sent 3541541Srgrimes * normally; a 0 return indicates that the packet has been 3551541Srgrimes * taken over here, either now or for later transmission. 3561541Srgrimes */ 3571541Srgrimesint 3583514Swollmanarpresolve(ac, rt, m, dst, desten, rt0) 3591541Srgrimes register struct arpcom *ac; 3601541Srgrimes register struct rtentry *rt; 3611541Srgrimes struct mbuf *m; 3621541Srgrimes register struct sockaddr *dst; 3631541Srgrimes register u_char *desten; 3643514Swollman struct rtentry *rt0; 3651541Srgrimes{ 36642866Sfenner register struct llinfo_arp *la = 0; 3671541Srgrimes struct sockaddr_dl *sdl; 3681541Srgrimes 3691541Srgrimes if (m->m_flags & M_BCAST) { /* broadcast */ 3708384Sdg (void)memcpy(desten, etherbroadcastaddr, sizeof(etherbroadcastaddr)); 3711541Srgrimes return (1); 3721541Srgrimes } 3731541Srgrimes if (m->m_flags & M_MCAST) { /* multicast */ 3741541Srgrimes ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 3751541Srgrimes return(1); 3761541Srgrimes } 3771541Srgrimes if (rt) 3781541Srgrimes la = (struct llinfo_arp *)rt->rt_llinfo; 37942775Sfenner if (la == 0) { 3803311Sphk la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0); 3813311Sphk if (la) 3821541Srgrimes rt = la->la_rt; 3831541Srgrimes } 3841541Srgrimes if (la == 0 || rt == 0) { 38536308Sphk log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n", 38636308Sphk inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "", 38736308Sphk rt ? "rt" : ""); 3881541Srgrimes m_freem(m); 3891541Srgrimes return (0); 3901541Srgrimes } 3911541Srgrimes sdl = SDL(rt->rt_gateway); 3921541Srgrimes /* 3931541Srgrimes * Check the address family and length is valid, the address 3941541Srgrimes * is resolved; otherwise, try to resolve. 3951541Srgrimes */ 39634961Sphk if ((rt->rt_expire == 0 || rt->rt_expire > time_second) && 3971541Srgrimes sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { 39816206Sbde bcopy(LLADDR(sdl), desten, sdl->sdl_alen); 3991541Srgrimes return 1; 4001541Srgrimes } 4011541Srgrimes /* 4021541Srgrimes * There is an arptab entry, but no ethernet address 4031541Srgrimes * response yet. Replace the held mbuf with this 4041541Srgrimes * latest one. 4051541Srgrimes */ 4061541Srgrimes if (la->la_hold) 4071541Srgrimes m_freem(la->la_hold); 4081541Srgrimes la->la_hold = m; 4091541Srgrimes if (rt->rt_expire) { 4101541Srgrimes rt->rt_flags &= ~RTF_REJECT; 41134961Sphk if (la->la_asked == 0 || rt->rt_expire != time_second) { 41234961Sphk rt->rt_expire = time_second; 4131541Srgrimes if (la->la_asked++ < arp_maxtries) 41414761Sfenner arprequest(ac, 41536908Sjulian &SIN(rt->rt_ifa->ifa_addr)->sin_addr, 41636908Sjulian &SIN(dst)->sin_addr, ac->ac_enaddr); 4171541Srgrimes else { 4181541Srgrimes rt->rt_flags |= RTF_REJECT; 4191541Srgrimes rt->rt_expire += arpt_down; 4201541Srgrimes la->la_asked = 0; 4211541Srgrimes } 4221541Srgrimes 4231541Srgrimes } 4241541Srgrimes } 4251541Srgrimes return (0); 4261541Srgrimes} 4271541Srgrimes 4281541Srgrimes/* 4291541Srgrimes * Common length and type checks are done here, 4301541Srgrimes * then the protocol-specific routine is called. 4311541Srgrimes */ 43212693Sphkstatic void 43331884Sbdearpintr() 4341541Srgrimes{ 43557900Srwatson register struct mbuf *m, *m0; 4361541Srgrimes register struct arphdr *ar; 43757900Srwatson int s, ml; 4381541Srgrimes 4391541Srgrimes while (arpintrq.ifq_head) { 4401541Srgrimes s = splimp(); 4411541Srgrimes IF_DEQUEUE(&arpintrq, m); 4421541Srgrimes splx(s); 4431541Srgrimes if (m == 0 || (m->m_flags & M_PKTHDR) == 0) 4441541Srgrimes panic("arpintr"); 44557900Srwatson 44657900Srwatson if (m->m_len < sizeof(struct arphdr) && 44757900Srwatson (m = m_pullup(m, sizeof(struct arphdr)) == NULL)) { 44857900Srwatson log(LOG_ERR, "arp: runt packet -- m_pullup failed."); 44957900Srwatson continue; 45057900Srwatson } 45157900Srwatson ar = mtod(m, struct arphdr *); 4521541Srgrimes 45357900Srwatson if (ntohs(ar->ar_hrd) != ARPHRD_ETHER 45457900Srwatson && ntohs(ar->ar_hrd) != ARPHRD_IEEE802) { 45557900Srwatson log(LOG_ERR, 45657900Srwatson "arp: unknown hardware address format (%2D)", 45757900Srwatson (unsigned char *)&ar->ar_hrd, ""); 45857900Srwatson m_freem(m); 45957900Srwatson continue; 46057900Srwatson } 4611541Srgrimes 46257900Srwatson m0 = m; 46357900Srwatson ml = 0; 46457900Srwatson while (m0 != NULL) { 46557900Srwatson ml += m0->m_len; /* wanna implement m_size?? */ 46657900Srwatson m0 = m0->m_next; 46757900Srwatson } 46857900Srwatson 46957900Srwatson if (ml < sizeof(struct arphdr) + 2 * ar->ar_hln 47057900Srwatson + 2 * ar->ar_pln) { 47157900Srwatson log(LOG_ERR, "arp: runt packet."); 47257900Srwatson m_freem(m); 47357900Srwatson continue; 47457900Srwatson } 47557900Srwatson 47657900Srwatson switch (ntohs(ar->ar_pro)) { 47732350Seivind#ifdef INET 47857900Srwatson case ETHERTYPE_IP: 47957900Srwatson in_arpinput(m); 48057900Srwatson continue; 48132350Seivind#endif 48257900Srwatson } 4831541Srgrimes m_freem(m); 4841541Srgrimes } 4851541Srgrimes} 4861541Srgrimes 48732350Seivind#ifdef INET 4881541Srgrimes/* 4891541Srgrimes * ARP for Internet protocols on 10 Mb/s Ethernet. 4901541Srgrimes * Algorithm is that given in RFC 826. 4911541Srgrimes * In addition, a sanity check is performed on the sender 4921541Srgrimes * protocol address, to catch impersonators. 4931541Srgrimes * We no longer handle negotiations for use of trailer protocol: 4941541Srgrimes * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 4951541Srgrimes * along with IP replies if we wanted trailers sent to us, 4961541Srgrimes * and also sent them in response to IP replies. 4971541Srgrimes * This allowed either end to announce the desire to receive 4981541Srgrimes * trailer packets. 4991541Srgrimes * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 5001541Srgrimes * but formerly didn't normally send requests. 5011541Srgrimes */ 5021541Srgrimesstatic void 5031541Srgrimesin_arpinput(m) 5041541Srgrimes struct mbuf *m; 5051541Srgrimes{ 5061541Srgrimes register struct ether_arp *ea; 5071541Srgrimes register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif; 5081541Srgrimes struct ether_header *eh; 50944627Sjulian struct iso88025_header *th = (struct iso88025_header *)0; 5101541Srgrimes register struct llinfo_arp *la = 0; 5111541Srgrimes register struct rtentry *rt; 5121541Srgrimes struct in_ifaddr *ia, *maybe_ia = 0; 5131541Srgrimes struct sockaddr_dl *sdl; 5141541Srgrimes struct sockaddr sa; 5151541Srgrimes struct in_addr isaddr, itaddr, myaddr; 51658313Slile int op, rif_len; 5171541Srgrimes 5181541Srgrimes ea = mtod(m, struct ether_arp *); 5191541Srgrimes op = ntohs(ea->arp_op); 5208384Sdg (void)memcpy(&isaddr, ea->arp_spa, sizeof (isaddr)); 5218384Sdg (void)memcpy(&itaddr, ea->arp_tpa, sizeof (itaddr)); 52220525Sbde for (ia = in_ifaddrhead.tqh_first; ia; ia = ia->ia_link.tqe_next) 52341793Sluigi#ifdef BRIDGE 52441793Sluigi /* 52541793Sluigi * For a bridge, we want to check the address irrespective 52641793Sluigi * of the receive interface. (This will change slightly 52741793Sluigi * when we have clusters of interfaces). 52841793Sluigi */ 52941793Sluigi { 53041793Sluigi#else 5311541Srgrimes if (ia->ia_ifp == &ac->ac_if) { 53241793Sluigi#endif 5331541Srgrimes maybe_ia = ia; 5341541Srgrimes if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) || 5351541Srgrimes (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)) 5361541Srgrimes break; 5371541Srgrimes } 53812693Sphk if (maybe_ia == 0) { 53912693Sphk m_freem(m); 54012693Sphk return; 54112693Sphk } 5421541Srgrimes myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr; 5431541Srgrimes if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr, 54412693Sphk sizeof (ea->arp_sha))) { 54512693Sphk m_freem(m); /* it's from me, ignore it. */ 54612693Sphk return; 54712693Sphk } 5481541Srgrimes if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr, 5491541Srgrimes sizeof (ea->arp_sha))) { 5501541Srgrimes log(LOG_ERR, 5514069Swollman "arp: ether address is broadcast for IP address %s!\n", 5527088Swollman inet_ntoa(isaddr)); 55312693Sphk m_freem(m); 55412693Sphk return; 5551541Srgrimes } 5561541Srgrimes if (isaddr.s_addr == myaddr.s_addr) { 5571541Srgrimes log(LOG_ERR, 55819794Sfenner "arp: %6D is using my IP address %s!\n", 55919794Sfenner ea->arp_sha, ":", inet_ntoa(isaddr)); 5601541Srgrimes itaddr = myaddr; 5611541Srgrimes goto reply; 5621541Srgrimes } 5631541Srgrimes la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0); 5641541Srgrimes if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) { 56542486Sluigi#ifndef BRIDGE /* the following is not an error when doing bridging */ 56639389Sfenner if (rt->rt_ifp != &ac->ac_if) { 56739389Sfenner log(LOG_ERR, "arp: %s is on %s%d but got reply from %6D on %s%d\n", 56839389Sfenner inet_ntoa(isaddr), 56939389Sfenner rt->rt_ifp->if_name, rt->rt_ifp->if_unit, 57039389Sfenner ea->arp_sha, ":", 57139389Sfenner ac->ac_if.if_name, ac->ac_if.if_unit); 57239389Sfenner goto reply; 57339389Sfenner } 57442486Sluigi#endif 5751541Srgrimes if (sdl->sdl_alen && 57646568Speter bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen)) { 57739389Sfenner if (rt->rt_expire) 57839389Sfenner log(LOG_INFO, "arp: %s moved from %6D to %6D on %s%d\n", 57939389Sfenner inet_ntoa(isaddr), (u_char *)LLADDR(sdl), ":", 58039389Sfenner ea->arp_sha, ":", 58139389Sfenner ac->ac_if.if_name, ac->ac_if.if_unit); 58239389Sfenner else { 58339389Sfenner log(LOG_ERR, 58452377Ssheldonh "arp: %6D attempts to modify permanent entry for %s on %s%d\n", 58539389Sfenner ea->arp_sha, ":", inet_ntoa(isaddr), 58639389Sfenner ac->ac_if.if_name, ac->ac_if.if_unit); 58739389Sfenner goto reply; 58839389Sfenner } 58946568Speter } 5908384Sdg (void)memcpy(LLADDR(sdl), ea->arp_sha, sizeof(ea->arp_sha)); 5918384Sdg sdl->sdl_alen = sizeof(ea->arp_sha); 59258313Slile sdl->sdl_rcf = (u_short)0; 59345705Seivind /* 59445705Seivind * If we receive an arp from a token-ring station over 59545705Seivind * a token-ring nic then try to save the source 59645705Seivind * routing info. 59745705Seivind */ 59845705Seivind if (ac->ac_if.if_type == IFT_ISO88025) { 59944627Sjulian th = (struct iso88025_header *)m->m_pkthdr.header; 60058313Slile rif_len = TR_RCF_RIFLEN(th->rcf); 60158313Slile if ((th->iso88025_shost[0] & TR_RII) && 60258313Slile (rif_len > 2)) { 60358313Slile sdl->sdl_rcf = th->rcf; 60458313Slile sdl->sdl_rcf ^= htons(TR_RCF_DIR); 60558313Slile memcpy(sdl->sdl_route, th->rd, rif_len - 2); 60658313Slile sdl->sdl_rcf &= ~htons(TR_RCF_BCST_MASK); 60751320Slile /* 60851320Slile * Set up source routing information for 60951320Slile * reply packet (XXX) 61051320Slile */ 61158313Slile m->m_data -= rif_len; 61258313Slile m->m_len += rif_len; 61358313Slile m->m_pkthdr.len += rif_len; 61444627Sjulian } else { 61558313Slile th->iso88025_shost[0] &= ~TR_RII; 61644627Sjulian } 61750512Slile m->m_data -= 8; 61850512Slile m->m_len += 8; 61950512Slile m->m_pkthdr.len += 8; 62044627Sjulian th->rcf = sdl->sdl_rcf; 62145705Seivind } else { 62258313Slile sdl->sdl_rcf = (u_short)0; 62344627Sjulian } 6241541Srgrimes if (rt->rt_expire) 62534961Sphk rt->rt_expire = time_second + arpt_keep; 6261541Srgrimes rt->rt_flags &= ~RTF_REJECT; 6271541Srgrimes la->la_asked = 0; 6281541Srgrimes if (la->la_hold) { 6291541Srgrimes (*ac->ac_if.if_output)(&ac->ac_if, la->la_hold, 6301541Srgrimes rt_key(rt), rt); 6311541Srgrimes la->la_hold = 0; 6321541Srgrimes } 6331541Srgrimes } 6341541Srgrimesreply: 6351541Srgrimes if (op != ARPOP_REQUEST) { 6361541Srgrimes m_freem(m); 6371541Srgrimes return; 6381541Srgrimes } 6391541Srgrimes if (itaddr.s_addr == myaddr.s_addr) { 6401541Srgrimes /* I am the target */ 6418384Sdg (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha)); 6428384Sdg (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha)); 6431541Srgrimes } else { 6441541Srgrimes la = arplookup(itaddr.s_addr, 0, SIN_PROXY); 6453282Swollman if (la == NULL) { 6463282Swollman struct sockaddr_in sin; 6473282Swollman 64812693Sphk if (!arp_proxyall) { 64912693Sphk m_freem(m); 65012693Sphk return; 65112693Sphk } 6523282Swollman 6533282Swollman bzero(&sin, sizeof sin); 6543282Swollman sin.sin_family = AF_INET; 6553282Swollman sin.sin_len = sizeof sin; 6563282Swollman sin.sin_addr = itaddr; 6573282Swollman 6585101Swollman rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 65912693Sphk if (!rt) { 66012693Sphk m_freem(m); 66112693Sphk return; 66212693Sphk } 6633282Swollman /* 6643282Swollman * Don't send proxies for nodes on the same interface 6653282Swollman * as this one came out of, or we'll get into a fight 6663282Swollman * over who claims what Ether address. 6673282Swollman */ 66812693Sphk if (rt->rt_ifp == &ac->ac_if) { 6693282Swollman rtfree(rt); 67012693Sphk m_freem(m); 67112693Sphk return; 6723282Swollman } 6738384Sdg (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha)); 6748384Sdg (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha)); 6753282Swollman rtfree(rt); 6764069Swollman#ifdef DEBUG_PROXY 6778876Srgrimes printf("arp: proxying for %s\n", 6787088Swollman inet_ntoa(itaddr)); 6794069Swollman#endif 6803282Swollman } else { 6813282Swollman rt = la->la_rt; 6828384Sdg (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha)); 6833282Swollman sdl = SDL(rt->rt_gateway); 6848384Sdg (void)memcpy(ea->arp_sha, LLADDR(sdl), sizeof(ea->arp_sha)); 6853282Swollman } 6861541Srgrimes } 6871541Srgrimes 6888384Sdg (void)memcpy(ea->arp_tpa, ea->arp_spa, sizeof(ea->arp_spa)); 6898384Sdg (void)memcpy(ea->arp_spa, &itaddr, sizeof(ea->arp_spa)); 6901541Srgrimes ea->arp_op = htons(ARPOP_REPLY); 6911541Srgrimes ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 69245705Seivind switch (ac->ac_if.if_type) { 69345705Seivind case IFT_ISO88025: 69444627Sjulian /* Re-arrange the source/dest address */ 69551320Slile memcpy(th->iso88025_dhost, th->iso88025_shost, 69651320Slile sizeof(th->iso88025_dhost)); 69751320Slile memcpy(th->iso88025_shost, ac->ac_enaddr, 69851320Slile sizeof(th->iso88025_shost)); 69944627Sjulian /* Set the source routing bit if neccesary */ 70058313Slile if (th->iso88025_dhost[0] & TR_RII) { 70158313Slile th->iso88025_dhost[0] &= ~TR_RII; 70258313Slile if (TR_RCF_RIFLEN(th->rcf) > 2) 70358313Slile th->iso88025_shost[0] |= TR_RII; 70444627Sjulian } 70544627Sjulian /* Copy the addresses, ac and fc into sa_data */ 70651320Slile memcpy(sa.sa_data, th->iso88025_dhost, 70751320Slile sizeof(th->iso88025_dhost) * 2); 70858313Slile sa.sa_data[(sizeof(th->iso88025_dhost) * 2)] = TR_AC; 70958313Slile sa.sa_data[(sizeof(th->iso88025_dhost) * 2) + 1] = TR_LLC_FRAME; 71044627Sjulian break; 71145705Seivind case IFT_ETHER: 71251320Slile case IFT_FDDI: 71351320Slile /* 71451320Slile * May not be correct for types not explictly 71551320Slile * listed, but it is our best guess. 71651320Slile */ 71751320Slile default: 71844627Sjulian eh = (struct ether_header *)sa.sa_data; 71951320Slile (void)memcpy(eh->ether_dhost, ea->arp_tha, 72051320Slile sizeof(eh->ether_dhost)); 72144627Sjulian eh->ether_type = htons(ETHERTYPE_ARP); 72244627Sjulian break; 72344627Sjulian } 7241541Srgrimes sa.sa_family = AF_UNSPEC; 7251541Srgrimes sa.sa_len = sizeof(sa); 7261541Srgrimes (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0); 7271541Srgrimes return; 7281541Srgrimes} 72932350Seivind#endif 7301541Srgrimes 7311541Srgrimes/* 7321541Srgrimes * Free an arp entry. 7331541Srgrimes */ 7341541Srgrimesstatic void 7351541Srgrimesarptfree(la) 7361541Srgrimes register struct llinfo_arp *la; 7371541Srgrimes{ 7381541Srgrimes register struct rtentry *rt = la->la_rt; 7391541Srgrimes register struct sockaddr_dl *sdl; 7401541Srgrimes if (rt == 0) 7411541Srgrimes panic("arptfree"); 7421541Srgrimes if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) && 7431541Srgrimes sdl->sdl_family == AF_LINK) { 7441541Srgrimes sdl->sdl_alen = 0; 7451541Srgrimes la->la_asked = 0; 7461541Srgrimes rt->rt_flags &= ~RTF_REJECT; 7471541Srgrimes return; 7481541Srgrimes } 7491541Srgrimes rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt), 7501541Srgrimes 0, (struct rtentry **)0); 7511541Srgrimes} 7521541Srgrimes/* 7531541Srgrimes * Lookup or enter a new address in arptab. 7541541Srgrimes */ 7551541Srgrimesstatic struct llinfo_arp * 7561541Srgrimesarplookup(addr, create, proxy) 7571541Srgrimes u_long addr; 7581541Srgrimes int create, proxy; 7591541Srgrimes{ 7601541Srgrimes register struct rtentry *rt; 7611541Srgrimes static struct sockaddr_inarp sin = {sizeof(sin), AF_INET }; 7624069Swollman const char *why = 0; 7631541Srgrimes 7641541Srgrimes sin.sin_addr.s_addr = addr; 7651541Srgrimes sin.sin_other = proxy ? SIN_PROXY : 0; 7665101Swollman rt = rtalloc1((struct sockaddr *)&sin, create, 0UL); 7671541Srgrimes if (rt == 0) 7681541Srgrimes return (0); 7691541Srgrimes rt->rt_refcnt--; 7704069Swollman 77112693Sphk if (rt->rt_flags & RTF_GATEWAY) 7724069Swollman why = "host is not on local network"; 77312693Sphk else if ((rt->rt_flags & RTF_LLINFO) == 0) 7744069Swollman why = "could not allocate llinfo"; 77512693Sphk else if (rt->rt_gateway->sa_family != AF_LINK) 7764069Swollman why = "gateway route is not ours"; 7774069Swollman 77812693Sphk if (why && create) { 7794069Swollman log(LOG_DEBUG, "arplookup %s failed: %s\n", 7807088Swollman inet_ntoa(sin.sin_addr), why); 7814069Swollman return 0; 78212693Sphk } else if (why) { 7834069Swollman return 0; 7841541Srgrimes } 7851541Srgrimes return ((struct llinfo_arp *)rt->rt_llinfo); 7861541Srgrimes} 7871541Srgrimes 7885195Swollmanvoid 7895195Swollmanarp_ifinit(ac, ifa) 7905195Swollman struct arpcom *ac; 7915195Swollman struct ifaddr *ifa; 7925195Swollman{ 79325822Stegge if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 79436908Sjulian arprequest(ac, &IA_SIN(ifa)->sin_addr, 79536908Sjulian &IA_SIN(ifa)->sin_addr, ac->ac_enaddr); 7965195Swollman ifa->ifa_rtrequest = arp_rtrequest; 7975195Swollman ifa->ifa_flags |= RTF_CLONING; 7985195Swollman} 799