if_ether.c revision 57900
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 57900 2000-03-11 00:24:29Z rwatson $ 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> 611541Srgrimes 621541Srgrimes#include <netinet/in.h> 631541Srgrimes#include <netinet/in_var.h> 641541Srgrimes#include <netinet/if_ether.h> 651541Srgrimes 6644627Sjulian#include <net/iso88025.h> 6744627Sjulian 681541Srgrimes#define SIN(s) ((struct sockaddr_in *)s) 691541Srgrimes#define SDL(s) ((struct sockaddr_dl *)s) 701541Srgrimes 7144078SdfrSYSCTL_DECL(_net_link_ether); 7212942SwollmanSYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, ""); 731541Srgrimes 7412693Sphk/* timer values */ 7512942Swollmanstatic int arpt_prune = (5*60*1); /* walk list every 5 minutes */ 7612942Swollmanstatic int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ 7712942Swollmanstatic int arpt_down = 20; /* once declared down, don't send for 20 sec */ 781541Srgrimes 7912942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW, 8012942Swollman &arpt_prune, 0, ""); 8112942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW, 8212942Swollman &arpt_keep, 0, ""); 8312942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW, 8412942Swollman &arpt_down, 0, ""); 8512693Sphk 861541Srgrimes#define rt_expire rt_rmx.rmx_expire 871541Srgrimes 8811225Swollmanstruct llinfo_arp { 8911225Swollman LIST_ENTRY(llinfo_arp) la_le; 9011225Swollman struct rtentry *la_rt; 9111225Swollman struct mbuf *la_hold; /* last packet until resolved/timeout */ 9211225Swollman long la_asked; /* last time we QUERIED for this addr */ 9311225Swollman#define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */ 9411225Swollman}; 9512693Sphk 9611225Swollmanstatic LIST_HEAD(, llinfo_arp) llinfo_arp; 9711225Swollman 981541Srgrimesstruct ifqueue arpintrq = {0, 0, 0, 50}; 9912820Sphkstatic int arp_inuse, arp_allocated; 1001541Srgrimes 10112693Sphkstatic int arp_maxtries = 5; 10212942Swollmanstatic int useloopback = 1; /* use loopback interface for local traffic */ 10312942Swollmanstatic int arp_proxyall = 0; 1043282Swollman 10512942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW, 10612942Swollman &arp_maxtries, 0, ""); 10712942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW, 10812942Swollman &useloopback, 0, ""); 10912942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW, 11012942Swollman &arp_proxyall, 0, ""); 11112693Sphk 11212877Sbdestatic void arp_rtrequest __P((int, struct rtentry *, struct sockaddr *)); 11336908Sjulianstatic void arprequest __P((struct arpcom *, 11436908Sjulian struct in_addr *, struct in_addr *, u_char *)); 11512693Sphkstatic void arpintr __P((void)); 11612693Sphkstatic void arptfree __P((struct llinfo_arp *)); 11712693Sphkstatic void arptimer __P((void *)); 11812693Sphkstatic struct llinfo_arp 11912693Sphk *arplookup __P((u_long, int, int)); 12032350Seivind#ifdef INET 12112693Sphkstatic void in_arpinput __P((struct mbuf *)); 12232350Seivind#endif 12312693Sphk 1241541Srgrimes/* 1251541Srgrimes * Timeout routine. Age arp_tab entries periodically. 1261541Srgrimes */ 1271541Srgrimes/* ARGSUSED */ 1281541Srgrimesstatic void 1291541Srgrimesarptimer(ignored_arg) 1301541Srgrimes void *ignored_arg; 1311541Srgrimes{ 1321541Srgrimes int s = splnet(); 13311225Swollman register struct llinfo_arp *la = llinfo_arp.lh_first; 13411225Swollman struct llinfo_arp *ola; 1351541Srgrimes 1361541Srgrimes timeout(arptimer, (caddr_t)0, arpt_prune * hz); 13711225Swollman while ((ola = la) != 0) { 1381541Srgrimes register struct rtentry *rt = la->la_rt; 13911225Swollman la = la->la_le.le_next; 14034961Sphk if (rt->rt_expire && rt->rt_expire <= time_second) 14111225Swollman arptfree(ola); /* timer has expired, clear */ 1421541Srgrimes } 1431541Srgrimes splx(s); 1441541Srgrimes} 1451541Srgrimes 1461541Srgrimes/* 1471541Srgrimes * Parallel to llc_rtrequest. 1481541Srgrimes */ 1495196Swollmanstatic void 1501541Srgrimesarp_rtrequest(req, rt, sa) 1511541Srgrimes int req; 1521541Srgrimes register struct rtentry *rt; 1531541Srgrimes struct sockaddr *sa; 1541541Srgrimes{ 1551541Srgrimes register struct sockaddr *gate = rt->rt_gateway; 1561541Srgrimes register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo; 1571541Srgrimes static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 15812693Sphk static int arpinit_done; 1591541Srgrimes 1601541Srgrimes if (!arpinit_done) { 1611541Srgrimes arpinit_done = 1; 16211225Swollman LIST_INIT(&llinfo_arp); 1631541Srgrimes timeout(arptimer, (caddr_t)0, hz); 16457178Speter register_netisr(NETISR_ARP, arpintr); 1651541Srgrimes } 1661541Srgrimes if (rt->rt_flags & RTF_GATEWAY) 1671541Srgrimes return; 1681541Srgrimes switch (req) { 1691541Srgrimes 1701541Srgrimes case RTM_ADD: 1711541Srgrimes /* 1721541Srgrimes * XXX: If this is a manually added route to interface 1731541Srgrimes * such as older version of routed or gated might provide, 1741541Srgrimes * restore cloning bit. 1751541Srgrimes */ 1761541Srgrimes if ((rt->rt_flags & RTF_HOST) == 0 && 1771541Srgrimes SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 1781541Srgrimes rt->rt_flags |= RTF_CLONING; 1791541Srgrimes if (rt->rt_flags & RTF_CLONING) { 1801541Srgrimes /* 1811541Srgrimes * Case 1: This route should come from a route to iface. 1821541Srgrimes */ 1831541Srgrimes rt_setgate(rt, rt_key(rt), 1841541Srgrimes (struct sockaddr *)&null_sdl); 1851541Srgrimes gate = rt->rt_gateway; 1861541Srgrimes SDL(gate)->sdl_type = rt->rt_ifp->if_type; 1871541Srgrimes SDL(gate)->sdl_index = rt->rt_ifp->if_index; 18834961Sphk rt->rt_expire = time_second; 1891541Srgrimes break; 1901541Srgrimes } 1911541Srgrimes /* Announce a new entry if requested. */ 1921541Srgrimes if (rt->rt_flags & RTF_ANNOUNCE) 1931541Srgrimes arprequest((struct arpcom *)rt->rt_ifp, 19436908Sjulian &SIN(rt_key(rt))->sin_addr, 19536908Sjulian &SIN(rt_key(rt))->sin_addr, 1961541Srgrimes (u_char *)LLADDR(SDL(gate))); 1971541Srgrimes /*FALLTHROUGH*/ 1981541Srgrimes case RTM_RESOLVE: 1991541Srgrimes if (gate->sa_family != AF_LINK || 2001541Srgrimes gate->sa_len < sizeof(null_sdl)) { 2016568Sdg log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n"); 2021541Srgrimes break; 2031541Srgrimes } 2041541Srgrimes SDL(gate)->sdl_type = rt->rt_ifp->if_type; 2051541Srgrimes SDL(gate)->sdl_index = rt->rt_ifp->if_index; 2061541Srgrimes if (la != 0) 2071541Srgrimes break; /* This happens on a route change */ 2081541Srgrimes /* 2091541Srgrimes * Case 2: This route may come from cloning, or a manual route 2101541Srgrimes * add with a LL address. 2111541Srgrimes */ 2121541Srgrimes R_Malloc(la, struct llinfo_arp *, sizeof(*la)); 2131541Srgrimes rt->rt_llinfo = (caddr_t)la; 2141541Srgrimes if (la == 0) { 2151541Srgrimes log(LOG_DEBUG, "arp_rtrequest: malloc failed\n"); 2161541Srgrimes break; 2171541Srgrimes } 2181541Srgrimes arp_inuse++, arp_allocated++; 2191541Srgrimes Bzero(la, sizeof(*la)); 2201541Srgrimes la->la_rt = rt; 2211541Srgrimes rt->rt_flags |= RTF_LLINFO; 22211225Swollman LIST_INSERT_HEAD(&llinfo_arp, la, la_le); 22313926Swollman 22432350Seivind#ifdef INET 22513926Swollman /* 22613926Swollman * This keeps the multicast addresses from showing up 22713926Swollman * in `arp -a' listings as unresolved. It's not actually 22813926Swollman * functional. Then the same for broadcast. 22913926Swollman */ 23013926Swollman if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) { 23113926Swollman ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr, 23213926Swollman LLADDR(SDL(gate))); 23313926Swollman SDL(gate)->sdl_alen = 6; 23416576Speter rt->rt_expire = 0; 23513926Swollman } 23613926Swollman if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) { 23713926Swollman memcpy(LLADDR(SDL(gate)), etherbroadcastaddr, 6); 23813926Swollman SDL(gate)->sdl_alen = 6; 23916576Speter rt->rt_expire = 0; 24013926Swollman } 24132350Seivind#endif 24213926Swollman 2431541Srgrimes if (SIN(rt_key(rt))->sin_addr.s_addr == 2441541Srgrimes (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) { 2451541Srgrimes /* 2461541Srgrimes * This test used to be 2471541Srgrimes * if (loif.if_flags & IFF_UP) 2481541Srgrimes * It allowed local traffic to be forced 2491541Srgrimes * through the hardware by configuring the loopback down. 2501541Srgrimes * However, it causes problems during network configuration 2511541Srgrimes * for boards that can't receive packets they send. 2521541Srgrimes * It is now necessary to clear "useloopback" and remove 2531541Srgrimes * the route to force traffic out to the hardware. 2541541Srgrimes */ 2551541Srgrimes rt->rt_expire = 0; 2561541Srgrimes Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr, 2571541Srgrimes LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6); 2581541Srgrimes if (useloopback) 2598090Spst rt->rt_ifp = loif; 2601541Srgrimes 2611541Srgrimes } 2621541Srgrimes break; 2631541Srgrimes 2641541Srgrimes case RTM_DELETE: 2651541Srgrimes if (la == 0) 2661541Srgrimes break; 2671541Srgrimes arp_inuse--; 26811225Swollman LIST_REMOVE(la, la_le); 2691541Srgrimes rt->rt_llinfo = 0; 2701541Srgrimes rt->rt_flags &= ~RTF_LLINFO; 2711541Srgrimes if (la->la_hold) 2721541Srgrimes m_freem(la->la_hold); 2731541Srgrimes Free((caddr_t)la); 2741541Srgrimes } 2751541Srgrimes} 2761541Srgrimes 2771541Srgrimes/* 2781541Srgrimes * Broadcast an ARP request. Caller specifies: 2791541Srgrimes * - arp header source ip address 2801541Srgrimes * - arp header target ip address 2811541Srgrimes * - arp header source ethernet address 2821541Srgrimes */ 2831541Srgrimesstatic void 2841541Srgrimesarprequest(ac, sip, tip, enaddr) 2851541Srgrimes register struct arpcom *ac; 28636908Sjulian register struct in_addr *sip, *tip; 2871541Srgrimes register u_char *enaddr; 2881541Srgrimes{ 2891541Srgrimes register struct mbuf *m; 2901541Srgrimes register struct ether_header *eh; 2911541Srgrimes register struct ether_arp *ea; 2921541Srgrimes struct sockaddr sa; 2931541Srgrimes 2941541Srgrimes if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 2951541Srgrimes return; 29644456Swpaul m->m_pkthdr.rcvif = (struct ifnet *)0; 29744627Sjulian switch (ac->ac_if.if_type) { 29844627Sjulian case IFT_ISO88025: 29944627Sjulian m->m_len = sizeof(*ea) + 10; 30044627Sjulian m->m_pkthdr.len = sizeof(*ea) + 10; 30144627Sjulian MH_ALIGN(m, sizeof(*ea) + 10); 30251320Slile (void)memcpy(mtod(m, caddr_t), 30351320Slile "\x82\x40\xaa\xaa\x03\x00\x00\x00\x08\x06", 10); 30444627Sjulian (void)memcpy(sa.sa_data, etherbroadcastaddr, 6); 30544627Sjulian (void)memcpy(sa.sa_data + 6, enaddr, 6); 30644627Sjulian sa.sa_data[6] |= 0x80; 30744627Sjulian sa.sa_data[12] = 0x10; 30844627Sjulian sa.sa_data[13] = 0x40; 30944627Sjulian ea = (struct ether_arp *)(mtod(m, char *) + 10); 31044627Sjulian bzero((caddr_t)ea, sizeof (*ea)); 31144627Sjulian ea->arp_hrd = htons(ARPHRD_IEEE802); 31244627Sjulian break; 31351320Slile case IFT_FDDI: 31451320Slile case IFT_ETHER: 31551320Slile /* 31651320Slile * This may not be correct for types not explicitly 31751320Slile * listed, but this is our best guess 31851320Slile */ 31944627Sjulian default: 32051320Slile m->m_len = sizeof(*ea); 32151320Slile m->m_pkthdr.len = sizeof(*ea); 32251320Slile MH_ALIGN(m, sizeof(*ea)); 32351320Slile ea = mtod(m, struct ether_arp *); 32451320Slile eh = (struct ether_header *)sa.sa_data; 32551320Slile bzero((caddr_t)ea, sizeof (*ea)); 32651320Slile /* if_output will not swap */ 32751320Slile eh->ether_type = htons(ETHERTYPE_ARP); 32851320Slile (void)memcpy(eh->ether_dhost, etherbroadcastaddr, 32951320Slile sizeof(eh->ether_dhost)); 33051320Slile ea->arp_hrd = htons(ARPHRD_ETHER); 33151320Slile break; 33244627Sjulian } 3331541Srgrimes ea->arp_pro = htons(ETHERTYPE_IP); 3341541Srgrimes ea->arp_hln = sizeof(ea->arp_sha); /* hardware address length */ 3351541Srgrimes ea->arp_pln = sizeof(ea->arp_spa); /* protocol address length */ 3361541Srgrimes ea->arp_op = htons(ARPOP_REQUEST); 3378384Sdg (void)memcpy(ea->arp_sha, enaddr, sizeof(ea->arp_sha)); 3388384Sdg (void)memcpy(ea->arp_spa, sip, sizeof(ea->arp_spa)); 3398384Sdg (void)memcpy(ea->arp_tpa, tip, sizeof(ea->arp_tpa)); 3401541Srgrimes sa.sa_family = AF_UNSPEC; 3411541Srgrimes sa.sa_len = sizeof(sa); 3421541Srgrimes (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0); 3431541Srgrimes} 3441541Srgrimes 3451541Srgrimes/* 3461541Srgrimes * Resolve an IP address into an ethernet address. If success, 3471541Srgrimes * desten is filled in. If there is no entry in arptab, 3481541Srgrimes * set one up and broadcast a request for the IP address. 3491541Srgrimes * Hold onto this mbuf and resend it once the address 3501541Srgrimes * is finally resolved. A return value of 1 indicates 3511541Srgrimes * that desten has been filled in and the packet should be sent 3521541Srgrimes * normally; a 0 return indicates that the packet has been 3531541Srgrimes * taken over here, either now or for later transmission. 3541541Srgrimes */ 3551541Srgrimesint 3563514Swollmanarpresolve(ac, rt, m, dst, desten, rt0) 3571541Srgrimes register struct arpcom *ac; 3581541Srgrimes register struct rtentry *rt; 3591541Srgrimes struct mbuf *m; 3601541Srgrimes register struct sockaddr *dst; 3611541Srgrimes register u_char *desten; 3623514Swollman struct rtentry *rt0; 3631541Srgrimes{ 36442866Sfenner register struct llinfo_arp *la = 0; 3651541Srgrimes struct sockaddr_dl *sdl; 3661541Srgrimes 3671541Srgrimes if (m->m_flags & M_BCAST) { /* broadcast */ 3688384Sdg (void)memcpy(desten, etherbroadcastaddr, sizeof(etherbroadcastaddr)); 3691541Srgrimes return (1); 3701541Srgrimes } 3711541Srgrimes if (m->m_flags & M_MCAST) { /* multicast */ 3721541Srgrimes ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 3731541Srgrimes return(1); 3741541Srgrimes } 3751541Srgrimes if (rt) 3761541Srgrimes la = (struct llinfo_arp *)rt->rt_llinfo; 37742775Sfenner if (la == 0) { 3783311Sphk la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0); 3793311Sphk if (la) 3801541Srgrimes rt = la->la_rt; 3811541Srgrimes } 3821541Srgrimes if (la == 0 || rt == 0) { 38336308Sphk log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n", 38436308Sphk inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "", 38536308Sphk rt ? "rt" : ""); 3861541Srgrimes m_freem(m); 3871541Srgrimes return (0); 3881541Srgrimes } 3891541Srgrimes sdl = SDL(rt->rt_gateway); 3901541Srgrimes /* 3911541Srgrimes * Check the address family and length is valid, the address 3921541Srgrimes * is resolved; otherwise, try to resolve. 3931541Srgrimes */ 39434961Sphk if ((rt->rt_expire == 0 || rt->rt_expire > time_second) && 3951541Srgrimes sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { 39616206Sbde bcopy(LLADDR(sdl), desten, sdl->sdl_alen); 3971541Srgrimes return 1; 3981541Srgrimes } 3991541Srgrimes /* 4001541Srgrimes * There is an arptab entry, but no ethernet address 4011541Srgrimes * response yet. Replace the held mbuf with this 4021541Srgrimes * latest one. 4031541Srgrimes */ 4041541Srgrimes if (la->la_hold) 4051541Srgrimes m_freem(la->la_hold); 4061541Srgrimes la->la_hold = m; 4071541Srgrimes if (rt->rt_expire) { 4081541Srgrimes rt->rt_flags &= ~RTF_REJECT; 40934961Sphk if (la->la_asked == 0 || rt->rt_expire != time_second) { 41034961Sphk rt->rt_expire = time_second; 4111541Srgrimes if (la->la_asked++ < arp_maxtries) 41214761Sfenner arprequest(ac, 41336908Sjulian &SIN(rt->rt_ifa->ifa_addr)->sin_addr, 41436908Sjulian &SIN(dst)->sin_addr, ac->ac_enaddr); 4151541Srgrimes else { 4161541Srgrimes rt->rt_flags |= RTF_REJECT; 4171541Srgrimes rt->rt_expire += arpt_down; 4181541Srgrimes la->la_asked = 0; 4191541Srgrimes } 4201541Srgrimes 4211541Srgrimes } 4221541Srgrimes } 4231541Srgrimes return (0); 4241541Srgrimes} 4251541Srgrimes 4261541Srgrimes/* 4271541Srgrimes * Common length and type checks are done here, 4281541Srgrimes * then the protocol-specific routine is called. 4291541Srgrimes */ 43012693Sphkstatic void 43131884Sbdearpintr() 4321541Srgrimes{ 43357900Srwatson register struct mbuf *m, *m0; 4341541Srgrimes register struct arphdr *ar; 43557900Srwatson int s, ml; 4361541Srgrimes 4371541Srgrimes while (arpintrq.ifq_head) { 4381541Srgrimes s = splimp(); 4391541Srgrimes IF_DEQUEUE(&arpintrq, m); 4401541Srgrimes splx(s); 4411541Srgrimes if (m == 0 || (m->m_flags & M_PKTHDR) == 0) 4421541Srgrimes panic("arpintr"); 44357900Srwatson 44457900Srwatson if (m->m_len < sizeof(struct arphdr) && 44557900Srwatson (m = m_pullup(m, sizeof(struct arphdr)) == NULL)) { 44657900Srwatson log(LOG_ERR, "arp: runt packet -- m_pullup failed."); 44757900Srwatson continue; 44857900Srwatson } 44957900Srwatson ar = mtod(m, struct arphdr *); 4501541Srgrimes 45157900Srwatson if (ntohs(ar->ar_hrd) != ARPHRD_ETHER 45257900Srwatson && ntohs(ar->ar_hrd) != ARPHRD_IEEE802) { 45357900Srwatson log(LOG_ERR, 45457900Srwatson "arp: unknown hardware address format (%2D)", 45557900Srwatson (unsigned char *)&ar->ar_hrd, ""); 45657900Srwatson m_freem(m); 45757900Srwatson continue; 45857900Srwatson } 4591541Srgrimes 46057900Srwatson m0 = m; 46157900Srwatson ml = 0; 46257900Srwatson while (m0 != NULL) { 46357900Srwatson ml += m0->m_len; /* wanna implement m_size?? */ 46457900Srwatson m0 = m0->m_next; 46557900Srwatson } 46657900Srwatson 46757900Srwatson if (ml < sizeof(struct arphdr) + 2 * ar->ar_hln 46857900Srwatson + 2 * ar->ar_pln) { 46957900Srwatson log(LOG_ERR, "arp: runt packet."); 47057900Srwatson m_freem(m); 47157900Srwatson continue; 47257900Srwatson } 47357900Srwatson 47457900Srwatson switch (ntohs(ar->ar_pro)) { 47532350Seivind#ifdef INET 47657900Srwatson case ETHERTYPE_IP: 47757900Srwatson in_arpinput(m); 47857900Srwatson continue; 47932350Seivind#endif 48057900Srwatson } 4811541Srgrimes m_freem(m); 4821541Srgrimes } 4831541Srgrimes} 4841541Srgrimes 48532350Seivind#ifdef INET 4861541Srgrimes/* 4871541Srgrimes * ARP for Internet protocols on 10 Mb/s Ethernet. 4881541Srgrimes * Algorithm is that given in RFC 826. 4891541Srgrimes * In addition, a sanity check is performed on the sender 4901541Srgrimes * protocol address, to catch impersonators. 4911541Srgrimes * We no longer handle negotiations for use of trailer protocol: 4921541Srgrimes * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 4931541Srgrimes * along with IP replies if we wanted trailers sent to us, 4941541Srgrimes * and also sent them in response to IP replies. 4951541Srgrimes * This allowed either end to announce the desire to receive 4961541Srgrimes * trailer packets. 4971541Srgrimes * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 4981541Srgrimes * but formerly didn't normally send requests. 4991541Srgrimes */ 5001541Srgrimesstatic void 5011541Srgrimesin_arpinput(m) 5021541Srgrimes struct mbuf *m; 5031541Srgrimes{ 5041541Srgrimes register struct ether_arp *ea; 5051541Srgrimes register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif; 5061541Srgrimes struct ether_header *eh; 50744627Sjulian struct iso88025_header *th = (struct iso88025_header *)0; 5081541Srgrimes register struct llinfo_arp *la = 0; 5091541Srgrimes register struct rtentry *rt; 5101541Srgrimes struct in_ifaddr *ia, *maybe_ia = 0; 5111541Srgrimes struct sockaddr_dl *sdl; 5121541Srgrimes struct sockaddr sa; 5131541Srgrimes struct in_addr isaddr, itaddr, myaddr; 5141541Srgrimes int op; 5151541Srgrimes 5161541Srgrimes ea = mtod(m, struct ether_arp *); 5171541Srgrimes op = ntohs(ea->arp_op); 5188384Sdg (void)memcpy(&isaddr, ea->arp_spa, sizeof (isaddr)); 5198384Sdg (void)memcpy(&itaddr, ea->arp_tpa, sizeof (itaddr)); 52020525Sbde for (ia = in_ifaddrhead.tqh_first; ia; ia = ia->ia_link.tqe_next) 52141793Sluigi#ifdef BRIDGE 52241793Sluigi /* 52341793Sluigi * For a bridge, we want to check the address irrespective 52441793Sluigi * of the receive interface. (This will change slightly 52541793Sluigi * when we have clusters of interfaces). 52641793Sluigi */ 52741793Sluigi { 52841793Sluigi#else 5291541Srgrimes if (ia->ia_ifp == &ac->ac_if) { 53041793Sluigi#endif 5311541Srgrimes maybe_ia = ia; 5321541Srgrimes if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) || 5331541Srgrimes (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)) 5341541Srgrimes break; 5351541Srgrimes } 53612693Sphk if (maybe_ia == 0) { 53712693Sphk m_freem(m); 53812693Sphk return; 53912693Sphk } 5401541Srgrimes myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr; 5411541Srgrimes if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr, 54212693Sphk sizeof (ea->arp_sha))) { 54312693Sphk m_freem(m); /* it's from me, ignore it. */ 54412693Sphk return; 54512693Sphk } 5461541Srgrimes if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr, 5471541Srgrimes sizeof (ea->arp_sha))) { 5481541Srgrimes log(LOG_ERR, 5494069Swollman "arp: ether address is broadcast for IP address %s!\n", 5507088Swollman inet_ntoa(isaddr)); 55112693Sphk m_freem(m); 55212693Sphk return; 5531541Srgrimes } 5541541Srgrimes if (isaddr.s_addr == myaddr.s_addr) { 5551541Srgrimes log(LOG_ERR, 55619794Sfenner "arp: %6D is using my IP address %s!\n", 55719794Sfenner ea->arp_sha, ":", inet_ntoa(isaddr)); 5581541Srgrimes itaddr = myaddr; 5591541Srgrimes goto reply; 5601541Srgrimes } 5611541Srgrimes la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0); 5621541Srgrimes if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) { 56342486Sluigi#ifndef BRIDGE /* the following is not an error when doing bridging */ 56439389Sfenner if (rt->rt_ifp != &ac->ac_if) { 56539389Sfenner log(LOG_ERR, "arp: %s is on %s%d but got reply from %6D on %s%d\n", 56639389Sfenner inet_ntoa(isaddr), 56739389Sfenner rt->rt_ifp->if_name, rt->rt_ifp->if_unit, 56839389Sfenner ea->arp_sha, ":", 56939389Sfenner ac->ac_if.if_name, ac->ac_if.if_unit); 57039389Sfenner goto reply; 57139389Sfenner } 57242486Sluigi#endif 5731541Srgrimes if (sdl->sdl_alen && 57446568Speter bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen)) { 57539389Sfenner if (rt->rt_expire) 57639389Sfenner log(LOG_INFO, "arp: %s moved from %6D to %6D on %s%d\n", 57739389Sfenner inet_ntoa(isaddr), (u_char *)LLADDR(sdl), ":", 57839389Sfenner ea->arp_sha, ":", 57939389Sfenner ac->ac_if.if_name, ac->ac_if.if_unit); 58039389Sfenner else { 58139389Sfenner log(LOG_ERR, 58252377Ssheldonh "arp: %6D attempts to modify permanent entry for %s on %s%d\n", 58339389Sfenner ea->arp_sha, ":", inet_ntoa(isaddr), 58439389Sfenner ac->ac_if.if_name, ac->ac_if.if_unit); 58539389Sfenner goto reply; 58639389Sfenner } 58746568Speter } 5888384Sdg (void)memcpy(LLADDR(sdl), ea->arp_sha, sizeof(ea->arp_sha)); 5898384Sdg sdl->sdl_alen = sizeof(ea->arp_sha); 59044627Sjulian sdl->sdl_rcf = NULL; 59145705Seivind /* 59245705Seivind * If we receive an arp from a token-ring station over 59345705Seivind * a token-ring nic then try to save the source 59445705Seivind * routing info. 59545705Seivind */ 59645705Seivind if (ac->ac_if.if_type == IFT_ISO88025) { 59744627Sjulian th = (struct iso88025_header *)m->m_pkthdr.header; 59851320Slile if ((th->iso88025_shost[0] & 0x80) && 59950512Slile ((th->rcf & 0x001f) > 2)) { 60051320Slile sdl->sdl_rcf = (th->rcf & 0x8000) ? 60151320Slile (th->rcf & 0x7fff) : 60250512Slile (th->rcf | 0x8000); 60351320Slile memcpy(sdl->sdl_route, th->rseg, 60451320Slile (th->rcf & 0x001f) - 2); 60550512Slile sdl->sdl_rcf = sdl->sdl_rcf & 0xff1f; 60651320Slile /* 60751320Slile * Set up source routing information for 60851320Slile * reply packet (XXX) 60951320Slile */ 61051320Slile m->m_data -= (th->rcf & 0x001f); 61151320Slile m->m_len += (th->rcf & 0x001f); 61250512Slile m->m_pkthdr.len += (th->rcf & 0x001f); 61344627Sjulian } else { 61444627Sjulian th->iso88025_shost[0] &= 0x7f; 61544627Sjulian } 61650512Slile m->m_data -= 8; 61750512Slile m->m_len += 8; 61850512Slile m->m_pkthdr.len += 8; 61944627Sjulian th->rcf = sdl->sdl_rcf; 62045705Seivind } else { 62145705Seivind sdl->sdl_rcf = NULL; 62244627Sjulian } 6231541Srgrimes if (rt->rt_expire) 62434961Sphk rt->rt_expire = time_second + arpt_keep; 6251541Srgrimes rt->rt_flags &= ~RTF_REJECT; 6261541Srgrimes la->la_asked = 0; 6271541Srgrimes if (la->la_hold) { 6281541Srgrimes (*ac->ac_if.if_output)(&ac->ac_if, la->la_hold, 6291541Srgrimes rt_key(rt), rt); 6301541Srgrimes la->la_hold = 0; 6311541Srgrimes } 6321541Srgrimes } 6331541Srgrimesreply: 6341541Srgrimes if (op != ARPOP_REQUEST) { 6351541Srgrimes m_freem(m); 6361541Srgrimes return; 6371541Srgrimes } 6381541Srgrimes if (itaddr.s_addr == myaddr.s_addr) { 6391541Srgrimes /* I am the target */ 6408384Sdg (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha)); 6418384Sdg (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha)); 6421541Srgrimes } else { 6431541Srgrimes la = arplookup(itaddr.s_addr, 0, SIN_PROXY); 6443282Swollman if (la == NULL) { 6453282Swollman struct sockaddr_in sin; 6463282Swollman 64712693Sphk if (!arp_proxyall) { 64812693Sphk m_freem(m); 64912693Sphk return; 65012693Sphk } 6513282Swollman 6523282Swollman bzero(&sin, sizeof sin); 6533282Swollman sin.sin_family = AF_INET; 6543282Swollman sin.sin_len = sizeof sin; 6553282Swollman sin.sin_addr = itaddr; 6563282Swollman 6575101Swollman rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 65812693Sphk if (!rt) { 65912693Sphk m_freem(m); 66012693Sphk return; 66112693Sphk } 6623282Swollman /* 6633282Swollman * Don't send proxies for nodes on the same interface 6643282Swollman * as this one came out of, or we'll get into a fight 6653282Swollman * over who claims what Ether address. 6663282Swollman */ 66712693Sphk if (rt->rt_ifp == &ac->ac_if) { 6683282Swollman rtfree(rt); 66912693Sphk m_freem(m); 67012693Sphk return; 6713282Swollman } 6728384Sdg (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha)); 6738384Sdg (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha)); 6743282Swollman rtfree(rt); 6754069Swollman#ifdef DEBUG_PROXY 6768876Srgrimes printf("arp: proxying for %s\n", 6777088Swollman inet_ntoa(itaddr)); 6784069Swollman#endif 6793282Swollman } else { 6803282Swollman rt = la->la_rt; 6818384Sdg (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha)); 6823282Swollman sdl = SDL(rt->rt_gateway); 6838384Sdg (void)memcpy(ea->arp_sha, LLADDR(sdl), sizeof(ea->arp_sha)); 6843282Swollman } 6851541Srgrimes } 6861541Srgrimes 6878384Sdg (void)memcpy(ea->arp_tpa, ea->arp_spa, sizeof(ea->arp_spa)); 6888384Sdg (void)memcpy(ea->arp_spa, &itaddr, sizeof(ea->arp_spa)); 6891541Srgrimes ea->arp_op = htons(ARPOP_REPLY); 6901541Srgrimes ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 69145705Seivind switch (ac->ac_if.if_type) { 69245705Seivind case IFT_ISO88025: 69344627Sjulian /* Re-arrange the source/dest address */ 69451320Slile memcpy(th->iso88025_dhost, th->iso88025_shost, 69551320Slile sizeof(th->iso88025_dhost)); 69651320Slile memcpy(th->iso88025_shost, ac->ac_enaddr, 69751320Slile sizeof(th->iso88025_shost)); 69844627Sjulian /* Set the source routing bit if neccesary */ 69944627Sjulian if (th->iso88025_dhost[0] & 0x80) { 70044627Sjulian th->iso88025_dhost[0] &= 0x7f; 70150512Slile if ((th->rcf & 0x001f) - 2) 70244627Sjulian th->iso88025_shost[0] |= 0x80; 70344627Sjulian } 70444627Sjulian /* Copy the addresses, ac and fc into sa_data */ 70551320Slile memcpy(sa.sa_data, th->iso88025_dhost, 70651320Slile sizeof(th->iso88025_dhost) * 2); 70744627Sjulian sa.sa_data[(sizeof(th->iso88025_dhost) * 2)] = 0x10; 70844627Sjulian sa.sa_data[(sizeof(th->iso88025_dhost) * 2) + 1] = 0x40; 70944627Sjulian break; 71045705Seivind case IFT_ETHER: 71151320Slile case IFT_FDDI: 71251320Slile /* 71351320Slile * May not be correct for types not explictly 71451320Slile * listed, but it is our best guess. 71551320Slile */ 71651320Slile default: 71744627Sjulian eh = (struct ether_header *)sa.sa_data; 71851320Slile (void)memcpy(eh->ether_dhost, ea->arp_tha, 71951320Slile sizeof(eh->ether_dhost)); 72044627Sjulian eh->ether_type = htons(ETHERTYPE_ARP); 72144627Sjulian break; 72244627Sjulian } 7231541Srgrimes sa.sa_family = AF_UNSPEC; 7241541Srgrimes sa.sa_len = sizeof(sa); 7251541Srgrimes (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0); 7261541Srgrimes return; 7271541Srgrimes} 72832350Seivind#endif 7291541Srgrimes 7301541Srgrimes/* 7311541Srgrimes * Free an arp entry. 7321541Srgrimes */ 7331541Srgrimesstatic void 7341541Srgrimesarptfree(la) 7351541Srgrimes register struct llinfo_arp *la; 7361541Srgrimes{ 7371541Srgrimes register struct rtentry *rt = la->la_rt; 7381541Srgrimes register struct sockaddr_dl *sdl; 7391541Srgrimes if (rt == 0) 7401541Srgrimes panic("arptfree"); 7411541Srgrimes if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) && 7421541Srgrimes sdl->sdl_family == AF_LINK) { 7431541Srgrimes sdl->sdl_alen = 0; 7441541Srgrimes la->la_asked = 0; 7451541Srgrimes rt->rt_flags &= ~RTF_REJECT; 7461541Srgrimes return; 7471541Srgrimes } 7481541Srgrimes rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt), 7491541Srgrimes 0, (struct rtentry **)0); 7501541Srgrimes} 7511541Srgrimes/* 7521541Srgrimes * Lookup or enter a new address in arptab. 7531541Srgrimes */ 7541541Srgrimesstatic struct llinfo_arp * 7551541Srgrimesarplookup(addr, create, proxy) 7561541Srgrimes u_long addr; 7571541Srgrimes int create, proxy; 7581541Srgrimes{ 7591541Srgrimes register struct rtentry *rt; 7601541Srgrimes static struct sockaddr_inarp sin = {sizeof(sin), AF_INET }; 7614069Swollman const char *why = 0; 7621541Srgrimes 7631541Srgrimes sin.sin_addr.s_addr = addr; 7641541Srgrimes sin.sin_other = proxy ? SIN_PROXY : 0; 7655101Swollman rt = rtalloc1((struct sockaddr *)&sin, create, 0UL); 7661541Srgrimes if (rt == 0) 7671541Srgrimes return (0); 7681541Srgrimes rt->rt_refcnt--; 7694069Swollman 77012693Sphk if (rt->rt_flags & RTF_GATEWAY) 7714069Swollman why = "host is not on local network"; 77212693Sphk else if ((rt->rt_flags & RTF_LLINFO) == 0) 7734069Swollman why = "could not allocate llinfo"; 77412693Sphk else if (rt->rt_gateway->sa_family != AF_LINK) 7754069Swollman why = "gateway route is not ours"; 7764069Swollman 77712693Sphk if (why && create) { 7784069Swollman log(LOG_DEBUG, "arplookup %s failed: %s\n", 7797088Swollman inet_ntoa(sin.sin_addr), why); 7804069Swollman return 0; 78112693Sphk } else if (why) { 7824069Swollman return 0; 7831541Srgrimes } 7841541Srgrimes return ((struct llinfo_arp *)rt->rt_llinfo); 7851541Srgrimes} 7861541Srgrimes 7875195Swollmanvoid 7885195Swollmanarp_ifinit(ac, ifa) 7895195Swollman struct arpcom *ac; 7905195Swollman struct ifaddr *ifa; 7915195Swollman{ 79225822Stegge if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 79336908Sjulian arprequest(ac, &IA_SIN(ifa)->sin_addr, 79436908Sjulian &IA_SIN(ifa)->sin_addr, ac->ac_enaddr); 7955195Swollman ifa->ifa_rtrequest = arp_rtrequest; 7965195Swollman ifa->ifa_flags |= RTF_CLONING; 7975195Swollman} 798