if_ether.c revision 8426
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 348426Swollman * $Id: if_ether.c,v 1.15 1995/05/09 13:35:44 davidg Exp $ 351541Srgrimes */ 361541Srgrimes 371541Srgrimes/* 381541Srgrimes * Ethernet address resolution protocol. 391541Srgrimes * TODO: 401541Srgrimes * add "inuse/lock" bit (or ref. count) along with valid bit 411541Srgrimes */ 421541Srgrimes 431541Srgrimes#include <sys/param.h> 441541Srgrimes#include <sys/systm.h> 451541Srgrimes#include <sys/malloc.h> 461541Srgrimes#include <sys/mbuf.h> 471541Srgrimes#include <sys/socket.h> 481541Srgrimes#include <sys/time.h> 491541Srgrimes#include <sys/kernel.h> 501541Srgrimes#include <sys/errno.h> 511541Srgrimes#include <sys/ioctl.h> 521541Srgrimes#include <sys/syslog.h> 531541Srgrimes 541541Srgrimes#include <net/if.h> 551541Srgrimes#include <net/if_dl.h> 561541Srgrimes#include <net/route.h> 578426Swollman#include <net/netisr.h> 581541Srgrimes 591541Srgrimes#include <netinet/in.h> 601541Srgrimes#include <netinet/in_systm.h> 611541Srgrimes#include <netinet/in_var.h> 621541Srgrimes#include <netinet/ip.h> 631541Srgrimes#include <netinet/if_ether.h> 641541Srgrimes 651541Srgrimes#define SIN(s) ((struct sockaddr_in *)s) 661541Srgrimes#define SDL(s) ((struct sockaddr_dl *)s) 671541Srgrimes#define SRP(s) ((struct sockaddr_inarp *)s) 681541Srgrimes 691541Srgrimes/* 701541Srgrimes * ARP trailer negotiation. Trailer protocol is not IP specific, 711541Srgrimes * but ARP request/response use IP addresses. 721541Srgrimes */ 731541Srgrimes#define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL 741541Srgrimes 751541Srgrimes 761541Srgrimes/* timer values */ 771541Srgrimesint arpt_prune = (5*60*1); /* walk list every 5 minutes */ 781541Srgrimesint arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ 791541Srgrimesint arpt_down = 20; /* once declared down, don't send for 20 secs */ 801541Srgrimes#define rt_expire rt_rmx.rmx_expire 811541Srgrimes 821541Srgrimesstatic void arprequest __P((struct arpcom *, u_long *, u_long *, u_char *)); 831541Srgrimesstatic void arptfree __P((struct llinfo_arp *)); 841541Srgrimesstatic void arptimer __P((void *)); 851541Srgrimesstatic struct llinfo_arp *arplookup __P((u_long, int, int)); 861541Srgrimesstatic void in_arpinput __P((struct mbuf *)); 871541Srgrimes 881541Srgrimesstruct llinfo_arp llinfo_arp = {&llinfo_arp, &llinfo_arp}; 891541Srgrimesstruct ifqueue arpintrq = {0, 0, 0, 50}; 901541Srgrimesint arp_inuse, arp_allocated, arp_intimer; 911541Srgrimesint arp_maxtries = 5; 921541Srgrimesint useloopback = 1; /* use loopback interface for local traffic */ 931541Srgrimesint arpinit_done = 0; 941541Srgrimes 953282Swollman#ifdef ARP_PROXYALL 963282Swollmanint arp_proxyall = 1; 973282Swollman#endif 983282Swollman 991541Srgrimes/* 1001541Srgrimes * Timeout routine. Age arp_tab entries periodically. 1011541Srgrimes */ 1021541Srgrimes/* ARGSUSED */ 1031541Srgrimesstatic void 1041541Srgrimesarptimer(ignored_arg) 1051541Srgrimes void *ignored_arg; 1061541Srgrimes{ 1071541Srgrimes int s = splnet(); 1081541Srgrimes register struct llinfo_arp *la = llinfo_arp.la_next; 1091541Srgrimes 1101541Srgrimes timeout(arptimer, (caddr_t)0, arpt_prune * hz); 1111541Srgrimes while (la != &llinfo_arp) { 1121541Srgrimes register struct rtentry *rt = la->la_rt; 1131541Srgrimes la = la->la_next; 1141541Srgrimes if (rt->rt_expire && rt->rt_expire <= time.tv_sec) 1151541Srgrimes arptfree(la->la_prev); /* timer has expired, clear */ 1161541Srgrimes } 1171541Srgrimes splx(s); 1181541Srgrimes} 1191541Srgrimes 1201541Srgrimes/* 1211541Srgrimes * Parallel to llc_rtrequest. 1221541Srgrimes */ 1235196Swollmanstatic void 1241541Srgrimesarp_rtrequest(req, rt, sa) 1251541Srgrimes int req; 1261541Srgrimes register struct rtentry *rt; 1271541Srgrimes struct sockaddr *sa; 1281541Srgrimes{ 1291541Srgrimes register struct sockaddr *gate = rt->rt_gateway; 1301541Srgrimes register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo; 1311541Srgrimes static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 1321541Srgrimes 1331541Srgrimes if (!arpinit_done) { 1341541Srgrimes arpinit_done = 1; 1351541Srgrimes timeout(arptimer, (caddr_t)0, hz); 1361541Srgrimes } 1371541Srgrimes if (rt->rt_flags & RTF_GATEWAY) 1381541Srgrimes return; 1391541Srgrimes switch (req) { 1401541Srgrimes 1411541Srgrimes case RTM_ADD: 1421541Srgrimes /* 1431541Srgrimes * XXX: If this is a manually added route to interface 1441541Srgrimes * such as older version of routed or gated might provide, 1451541Srgrimes * restore cloning bit. 1461541Srgrimes */ 1471541Srgrimes if ((rt->rt_flags & RTF_HOST) == 0 && 1481541Srgrimes SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 1491541Srgrimes rt->rt_flags |= RTF_CLONING; 1503514Swollman#if 0 1513514Swollman /* 1523514Swollman * Actually, all IP gateway routes should have the cloning 1533514Swollman * flag turned on. We can't do this yet because the expiration 1543514Swollman * stuff isn't working yet. 1553514Swollman */ 1563514Swollman if (rt->rt_flags & RTF_GATEWAY) { 1573514Swollman rt->rt_flags |= RTF_CLONING; 1583514Swollman } 1593514Swollman#endif 1601541Srgrimes if (rt->rt_flags & RTF_CLONING) { 1611541Srgrimes /* 1621541Srgrimes * Case 1: This route should come from a route to iface. 1631541Srgrimes */ 1641541Srgrimes rt_setgate(rt, rt_key(rt), 1651541Srgrimes (struct sockaddr *)&null_sdl); 1661541Srgrimes gate = rt->rt_gateway; 1671541Srgrimes SDL(gate)->sdl_type = rt->rt_ifp->if_type; 1681541Srgrimes SDL(gate)->sdl_index = rt->rt_ifp->if_index; 1691541Srgrimes rt->rt_expire = time.tv_sec; 1701541Srgrimes break; 1711541Srgrimes } 1721541Srgrimes /* Announce a new entry if requested. */ 1731541Srgrimes if (rt->rt_flags & RTF_ANNOUNCE) 1741541Srgrimes arprequest((struct arpcom *)rt->rt_ifp, 1751541Srgrimes &SIN(rt_key(rt))->sin_addr.s_addr, 1761541Srgrimes &SIN(rt_key(rt))->sin_addr.s_addr, 1771541Srgrimes (u_char *)LLADDR(SDL(gate))); 1781541Srgrimes /*FALLTHROUGH*/ 1791541Srgrimes case RTM_RESOLVE: 1801541Srgrimes if (gate->sa_family != AF_LINK || 1811541Srgrimes gate->sa_len < sizeof(null_sdl)) { 1826568Sdg log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n"); 1831541Srgrimes break; 1841541Srgrimes } 1851541Srgrimes SDL(gate)->sdl_type = rt->rt_ifp->if_type; 1861541Srgrimes SDL(gate)->sdl_index = rt->rt_ifp->if_index; 1871541Srgrimes if (la != 0) 1881541Srgrimes break; /* This happens on a route change */ 1891541Srgrimes /* 1901541Srgrimes * Case 2: This route may come from cloning, or a manual route 1911541Srgrimes * add with a LL address. 1921541Srgrimes */ 1931541Srgrimes R_Malloc(la, struct llinfo_arp *, sizeof(*la)); 1941541Srgrimes rt->rt_llinfo = (caddr_t)la; 1951541Srgrimes if (la == 0) { 1961541Srgrimes log(LOG_DEBUG, "arp_rtrequest: malloc failed\n"); 1971541Srgrimes break; 1981541Srgrimes } 1991541Srgrimes arp_inuse++, arp_allocated++; 2001541Srgrimes Bzero(la, sizeof(*la)); 2011541Srgrimes la->la_rt = rt; 2021541Srgrimes rt->rt_flags |= RTF_LLINFO; 2031541Srgrimes insque(la, &llinfo_arp); 2041541Srgrimes if (SIN(rt_key(rt))->sin_addr.s_addr == 2051541Srgrimes (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) { 2061541Srgrimes /* 2071541Srgrimes * This test used to be 2081541Srgrimes * if (loif.if_flags & IFF_UP) 2091541Srgrimes * It allowed local traffic to be forced 2101541Srgrimes * through the hardware by configuring the loopback down. 2111541Srgrimes * However, it causes problems during network configuration 2121541Srgrimes * for boards that can't receive packets they send. 2131541Srgrimes * It is now necessary to clear "useloopback" and remove 2141541Srgrimes * the route to force traffic out to the hardware. 2151541Srgrimes */ 2161541Srgrimes rt->rt_expire = 0; 2171541Srgrimes Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr, 2181541Srgrimes LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6); 2191541Srgrimes if (useloopback) 2208090Spst rt->rt_ifp = loif; 2211541Srgrimes 2221541Srgrimes } 2231541Srgrimes break; 2241541Srgrimes 2251541Srgrimes case RTM_DELETE: 2261541Srgrimes if (la == 0) 2271541Srgrimes break; 2281541Srgrimes arp_inuse--; 2291541Srgrimes remque(la); 2301541Srgrimes rt->rt_llinfo = 0; 2311541Srgrimes rt->rt_flags &= ~RTF_LLINFO; 2321541Srgrimes if (la->la_hold) 2331541Srgrimes m_freem(la->la_hold); 2341541Srgrimes Free((caddr_t)la); 2351541Srgrimes } 2361541Srgrimes} 2371541Srgrimes 2381541Srgrimes/* 2391541Srgrimes * Broadcast an ARP packet, asking who has addr on interface ac. 2401541Srgrimes */ 2411541Srgrimesvoid 2421541Srgrimesarpwhohas(ac, addr) 2431541Srgrimes register struct arpcom *ac; 2441541Srgrimes register struct in_addr *addr; 2451541Srgrimes{ 2461541Srgrimes arprequest(ac, &ac->ac_ipaddr.s_addr, &addr->s_addr, ac->ac_enaddr); 2471541Srgrimes} 2481541Srgrimes 2491541Srgrimes/* 2501541Srgrimes * Broadcast an ARP request. Caller specifies: 2511541Srgrimes * - arp header source ip address 2521541Srgrimes * - arp header target ip address 2531541Srgrimes * - arp header source ethernet address 2541541Srgrimes */ 2551541Srgrimesstatic void 2561541Srgrimesarprequest(ac, sip, tip, enaddr) 2571541Srgrimes register struct arpcom *ac; 2581541Srgrimes register u_long *sip, *tip; 2591541Srgrimes register u_char *enaddr; 2601541Srgrimes{ 2611541Srgrimes register struct mbuf *m; 2621541Srgrimes register struct ether_header *eh; 2631541Srgrimes register struct ether_arp *ea; 2641541Srgrimes struct sockaddr sa; 2651541Srgrimes 2661541Srgrimes if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 2671541Srgrimes return; 2681541Srgrimes m->m_len = sizeof(*ea); 2691541Srgrimes m->m_pkthdr.len = sizeof(*ea); 2701541Srgrimes MH_ALIGN(m, sizeof(*ea)); 2711541Srgrimes ea = mtod(m, struct ether_arp *); 2721541Srgrimes eh = (struct ether_header *)sa.sa_data; 2731541Srgrimes bzero((caddr_t)ea, sizeof (*ea)); 2748384Sdg (void)memcpy(eh->ether_dhost, etherbroadcastaddr, sizeof(eh->ether_dhost)); 2751541Srgrimes eh->ether_type = ETHERTYPE_ARP; /* if_output will swap */ 2761541Srgrimes ea->arp_hrd = htons(ARPHRD_ETHER); 2771541Srgrimes ea->arp_pro = htons(ETHERTYPE_IP); 2781541Srgrimes ea->arp_hln = sizeof(ea->arp_sha); /* hardware address length */ 2791541Srgrimes ea->arp_pln = sizeof(ea->arp_spa); /* protocol address length */ 2801541Srgrimes ea->arp_op = htons(ARPOP_REQUEST); 2818384Sdg (void)memcpy(ea->arp_sha, enaddr, sizeof(ea->arp_sha)); 2828384Sdg (void)memcpy(ea->arp_spa, sip, sizeof(ea->arp_spa)); 2838384Sdg (void)memcpy(ea->arp_tpa, tip, sizeof(ea->arp_tpa)); 2841541Srgrimes sa.sa_family = AF_UNSPEC; 2851541Srgrimes sa.sa_len = sizeof(sa); 2861541Srgrimes (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0); 2871541Srgrimes} 2881541Srgrimes 2891541Srgrimes/* 2901541Srgrimes * Resolve an IP address into an ethernet address. If success, 2911541Srgrimes * desten is filled in. If there is no entry in arptab, 2921541Srgrimes * set one up and broadcast a request for the IP address. 2931541Srgrimes * Hold onto this mbuf and resend it once the address 2941541Srgrimes * is finally resolved. A return value of 1 indicates 2951541Srgrimes * that desten has been filled in and the packet should be sent 2961541Srgrimes * normally; a 0 return indicates that the packet has been 2971541Srgrimes * taken over here, either now or for later transmission. 2981541Srgrimes */ 2991541Srgrimesint 3003514Swollmanarpresolve(ac, rt, m, dst, desten, rt0) 3011541Srgrimes register struct arpcom *ac; 3021541Srgrimes register struct rtentry *rt; 3031541Srgrimes struct mbuf *m; 3041541Srgrimes register struct sockaddr *dst; 3051541Srgrimes register u_char *desten; 3063514Swollman struct rtentry *rt0; 3071541Srgrimes{ 3081541Srgrimes register struct llinfo_arp *la; 3091541Srgrimes struct sockaddr_dl *sdl; 3101541Srgrimes 3111541Srgrimes if (m->m_flags & M_BCAST) { /* broadcast */ 3128384Sdg (void)memcpy(desten, etherbroadcastaddr, sizeof(etherbroadcastaddr)); 3131541Srgrimes return (1); 3141541Srgrimes } 3151541Srgrimes if (m->m_flags & M_MCAST) { /* multicast */ 3161541Srgrimes ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 3171541Srgrimes return(1); 3181541Srgrimes } 3191541Srgrimes if (rt) 3201541Srgrimes la = (struct llinfo_arp *)rt->rt_llinfo; 3211541Srgrimes else { 3223311Sphk la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0); 3233311Sphk if (la) 3241541Srgrimes rt = la->la_rt; 3251541Srgrimes } 3261541Srgrimes if (la == 0 || rt == 0) { 3276568Sdg log(LOG_DEBUG, "arpresolve: can't allocate llinfo\n"); 3281541Srgrimes m_freem(m); 3291541Srgrimes return (0); 3301541Srgrimes } 3311541Srgrimes sdl = SDL(rt->rt_gateway); 3321541Srgrimes /* 3331541Srgrimes * Check the address family and length is valid, the address 3341541Srgrimes * is resolved; otherwise, try to resolve. 3351541Srgrimes */ 3361541Srgrimes if ((rt->rt_expire == 0 || rt->rt_expire > time.tv_sec) && 3371541Srgrimes sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { 3388384Sdg (void)memcpy(desten, LLADDR(sdl), sdl->sdl_alen); 3391541Srgrimes return 1; 3401541Srgrimes } 3411541Srgrimes /* 3421541Srgrimes * There is an arptab entry, but no ethernet address 3431541Srgrimes * response yet. Replace the held mbuf with this 3441541Srgrimes * latest one. 3451541Srgrimes */ 3461541Srgrimes if (la->la_hold) 3471541Srgrimes m_freem(la->la_hold); 3481541Srgrimes la->la_hold = m; 3491541Srgrimes if (rt->rt_expire) { 3501541Srgrimes rt->rt_flags &= ~RTF_REJECT; 3511541Srgrimes if (la->la_asked == 0 || rt->rt_expire != time.tv_sec) { 3521541Srgrimes rt->rt_expire = time.tv_sec; 3531541Srgrimes if (la->la_asked++ < arp_maxtries) 3541541Srgrimes arpwhohas(ac, &(SIN(dst)->sin_addr)); 3551541Srgrimes else { 3561541Srgrimes rt->rt_flags |= RTF_REJECT; 3571541Srgrimes rt->rt_expire += arpt_down; 3581541Srgrimes la->la_asked = 0; 3591541Srgrimes } 3601541Srgrimes 3611541Srgrimes } 3621541Srgrimes } 3631541Srgrimes return (0); 3641541Srgrimes} 3651541Srgrimes 3661541Srgrimes/* 3671541Srgrimes * Common length and type checks are done here, 3681541Srgrimes * then the protocol-specific routine is called. 3691541Srgrimes */ 3701541Srgrimesvoid 3718426Swollmanarpintr(void) 3721541Srgrimes{ 3731541Srgrimes register struct mbuf *m; 3741541Srgrimes register struct arphdr *ar; 3751541Srgrimes int s; 3761541Srgrimes 3771541Srgrimes while (arpintrq.ifq_head) { 3781541Srgrimes s = splimp(); 3791541Srgrimes IF_DEQUEUE(&arpintrq, m); 3801541Srgrimes splx(s); 3811541Srgrimes if (m == 0 || (m->m_flags & M_PKTHDR) == 0) 3821541Srgrimes panic("arpintr"); 3831541Srgrimes if (m->m_len >= sizeof(struct arphdr) && 3841541Srgrimes (ar = mtod(m, struct arphdr *)) && 3851541Srgrimes ntohs(ar->ar_hrd) == ARPHRD_ETHER && 3861541Srgrimes m->m_len >= 3871541Srgrimes sizeof(struct arphdr) + 2 * ar->ar_hln + 2 * ar->ar_pln) 3881541Srgrimes 3891541Srgrimes switch (ntohs(ar->ar_pro)) { 3901541Srgrimes 3911541Srgrimes case ETHERTYPE_IP: 3921541Srgrimes case ETHERTYPE_IPTRAILERS: 3931541Srgrimes in_arpinput(m); 3941541Srgrimes continue; 3951541Srgrimes } 3961541Srgrimes m_freem(m); 3971541Srgrimes } 3981541Srgrimes} 3991541Srgrimes 4008426SwollmanNETISR_SET(NETISR_ARP, arpintr); 4018426Swollman 4021541Srgrimes/* 4031541Srgrimes * ARP for Internet protocols on 10 Mb/s Ethernet. 4041541Srgrimes * Algorithm is that given in RFC 826. 4051541Srgrimes * In addition, a sanity check is performed on the sender 4061541Srgrimes * protocol address, to catch impersonators. 4071541Srgrimes * We no longer handle negotiations for use of trailer protocol: 4081541Srgrimes * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 4091541Srgrimes * along with IP replies if we wanted trailers sent to us, 4101541Srgrimes * and also sent them in response to IP replies. 4111541Srgrimes * This allowed either end to announce the desire to receive 4121541Srgrimes * trailer packets. 4131541Srgrimes * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 4141541Srgrimes * but formerly didn't normally send requests. 4151541Srgrimes */ 4161541Srgrimesstatic void 4171541Srgrimesin_arpinput(m) 4181541Srgrimes struct mbuf *m; 4191541Srgrimes{ 4201541Srgrimes register struct ether_arp *ea; 4211541Srgrimes register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif; 4221541Srgrimes struct ether_header *eh; 4231541Srgrimes register struct llinfo_arp *la = 0; 4241541Srgrimes register struct rtentry *rt; 4251541Srgrimes struct in_ifaddr *ia, *maybe_ia = 0; 4261541Srgrimes struct sockaddr_dl *sdl; 4271541Srgrimes struct sockaddr sa; 4281541Srgrimes struct in_addr isaddr, itaddr, myaddr; 4291541Srgrimes int op; 4301541Srgrimes 4311541Srgrimes ea = mtod(m, struct ether_arp *); 4321541Srgrimes op = ntohs(ea->arp_op); 4338384Sdg (void)memcpy(&isaddr, ea->arp_spa, sizeof (isaddr)); 4348384Sdg (void)memcpy(&itaddr, ea->arp_tpa, sizeof (itaddr)); 4351541Srgrimes for (ia = in_ifaddr; ia; ia = ia->ia_next) 4361541Srgrimes if (ia->ia_ifp == &ac->ac_if) { 4371541Srgrimes maybe_ia = ia; 4381541Srgrimes if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) || 4391541Srgrimes (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)) 4401541Srgrimes break; 4411541Srgrimes } 4421541Srgrimes if (maybe_ia == 0) 4431541Srgrimes goto out; 4441541Srgrimes myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr; 4451541Srgrimes if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr, 4461541Srgrimes sizeof (ea->arp_sha))) 4471541Srgrimes goto out; /* it's from me, ignore it. */ 4481541Srgrimes if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr, 4491541Srgrimes sizeof (ea->arp_sha))) { 4501541Srgrimes log(LOG_ERR, 4514069Swollman "arp: ether address is broadcast for IP address %s!\n", 4527088Swollman inet_ntoa(isaddr)); 4531541Srgrimes goto out; 4541541Srgrimes } 4551541Srgrimes if (isaddr.s_addr == myaddr.s_addr) { 4561541Srgrimes log(LOG_ERR, 4574069Swollman "duplicate IP address %s! sent from ethernet address: %s\n", 4587088Swollman inet_ntoa(isaddr), ether_sprintf(ea->arp_sha)); 4591541Srgrimes itaddr = myaddr; 4601541Srgrimes goto reply; 4611541Srgrimes } 4621541Srgrimes la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0); 4631541Srgrimes if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) { 4641541Srgrimes if (sdl->sdl_alen && 4651541Srgrimes bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen)) 4664069Swollman log(LOG_INFO, "arp info overwritten for %s by %s\n", 4677088Swollman inet_ntoa(isaddr), ether_sprintf(ea->arp_sha)); 4688384Sdg (void)memcpy(LLADDR(sdl), ea->arp_sha, sizeof(ea->arp_sha)); 4698384Sdg sdl->sdl_alen = sizeof(ea->arp_sha); 4701541Srgrimes if (rt->rt_expire) 4711541Srgrimes rt->rt_expire = time.tv_sec + arpt_keep; 4721541Srgrimes rt->rt_flags &= ~RTF_REJECT; 4731541Srgrimes la->la_asked = 0; 4741541Srgrimes if (la->la_hold) { 4751541Srgrimes (*ac->ac_if.if_output)(&ac->ac_if, la->la_hold, 4761541Srgrimes rt_key(rt), rt); 4771541Srgrimes la->la_hold = 0; 4781541Srgrimes } 4791541Srgrimes } 4801541Srgrimesreply: 4811541Srgrimes if (op != ARPOP_REQUEST) { 4821541Srgrimes out: 4831541Srgrimes m_freem(m); 4841541Srgrimes return; 4851541Srgrimes } 4861541Srgrimes if (itaddr.s_addr == myaddr.s_addr) { 4871541Srgrimes /* I am the target */ 4888384Sdg (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha)); 4898384Sdg (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha)); 4901541Srgrimes } else { 4911541Srgrimes la = arplookup(itaddr.s_addr, 0, SIN_PROXY); 4923282Swollman if (la == NULL) { 4933282Swollman#ifdef ARP_PROXYALL 4943282Swollman struct sockaddr_in sin; 4953282Swollman 4963282Swollman if(!arp_proxyall) goto out; 4973282Swollman 4983282Swollman bzero(&sin, sizeof sin); 4993282Swollman sin.sin_family = AF_INET; 5003282Swollman sin.sin_len = sizeof sin; 5013282Swollman sin.sin_addr = itaddr; 5023282Swollman 5035101Swollman rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 5043282Swollman if( !rt ) 5053282Swollman goto out; 5063282Swollman /* 5073282Swollman * Don't send proxies for nodes on the same interface 5083282Swollman * as this one came out of, or we'll get into a fight 5093282Swollman * over who claims what Ether address. 5103282Swollman */ 5113282Swollman if(rt->rt_ifp == &ac->ac_if) { 5123282Swollman rtfree(rt); 5133282Swollman goto out; 5143282Swollman } 5158384Sdg (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha)); 5168384Sdg (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha)); 5173282Swollman rtfree(rt); 5184069Swollman#ifdef DEBUG_PROXY 5194069Swollman printf("arp: proxying for %s\n", 5207088Swollman inet_ntoa(itaddr)); 5214069Swollman#endif 5223282Swollman#else 5231541Srgrimes goto out; 5243282Swollman#endif 5253282Swollman } else { 5263282Swollman rt = la->la_rt; 5278384Sdg (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha)); 5283282Swollman sdl = SDL(rt->rt_gateway); 5298384Sdg (void)memcpy(ea->arp_sha, LLADDR(sdl), sizeof(ea->arp_sha)); 5303282Swollman } 5311541Srgrimes } 5321541Srgrimes 5338384Sdg (void)memcpy(ea->arp_tpa, ea->arp_spa, sizeof(ea->arp_spa)); 5348384Sdg (void)memcpy(ea->arp_spa, &itaddr, sizeof(ea->arp_spa)); 5351541Srgrimes ea->arp_op = htons(ARPOP_REPLY); 5361541Srgrimes ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 5371541Srgrimes eh = (struct ether_header *)sa.sa_data; 5388384Sdg (void)memcpy(eh->ether_dhost, ea->arp_tha, sizeof(eh->ether_dhost)); 5391541Srgrimes eh->ether_type = ETHERTYPE_ARP; 5401541Srgrimes sa.sa_family = AF_UNSPEC; 5411541Srgrimes sa.sa_len = sizeof(sa); 5421541Srgrimes (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0); 5431541Srgrimes return; 5441541Srgrimes} 5451541Srgrimes 5461541Srgrimes/* 5471541Srgrimes * Free an arp entry. 5481541Srgrimes */ 5491541Srgrimesstatic void 5501541Srgrimesarptfree(la) 5511541Srgrimes register struct llinfo_arp *la; 5521541Srgrimes{ 5531541Srgrimes register struct rtentry *rt = la->la_rt; 5541541Srgrimes register struct sockaddr_dl *sdl; 5551541Srgrimes if (rt == 0) 5561541Srgrimes panic("arptfree"); 5571541Srgrimes if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) && 5581541Srgrimes sdl->sdl_family == AF_LINK) { 5591541Srgrimes sdl->sdl_alen = 0; 5601541Srgrimes la->la_asked = 0; 5611541Srgrimes rt->rt_flags &= ~RTF_REJECT; 5621541Srgrimes return; 5631541Srgrimes } 5641541Srgrimes rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt), 5651541Srgrimes 0, (struct rtentry **)0); 5661541Srgrimes} 5671541Srgrimes/* 5681541Srgrimes * Lookup or enter a new address in arptab. 5691541Srgrimes */ 5701541Srgrimesstatic struct llinfo_arp * 5711541Srgrimesarplookup(addr, create, proxy) 5721541Srgrimes u_long addr; 5731541Srgrimes int create, proxy; 5741541Srgrimes{ 5751541Srgrimes register struct rtentry *rt; 5761541Srgrimes static struct sockaddr_inarp sin = {sizeof(sin), AF_INET }; 5774069Swollman const char *why = 0; 5781541Srgrimes 5791541Srgrimes sin.sin_addr.s_addr = addr; 5801541Srgrimes sin.sin_other = proxy ? SIN_PROXY : 0; 5815101Swollman rt = rtalloc1((struct sockaddr *)&sin, create, 0UL); 5821541Srgrimes if (rt == 0) 5831541Srgrimes return (0); 5841541Srgrimes rt->rt_refcnt--; 5854069Swollman 5864069Swollman if(rt->rt_flags & RTF_GATEWAY) 5874069Swollman why = "host is not on local network"; 5884069Swollman else if((rt->rt_flags & RTF_LLINFO) == 0) 5894069Swollman why = "could not allocate llinfo"; 5904069Swollman else if(rt->rt_gateway->sa_family != AF_LINK) 5914069Swollman why = "gateway route is not ours"; 5924069Swollman 5934069Swollman if(why && create) { 5944069Swollman log(LOG_DEBUG, "arplookup %s failed: %s\n", 5957088Swollman inet_ntoa(sin.sin_addr), why); 5964069Swollman return 0; 5974069Swollman } else if(why) { 5984069Swollman return 0; 5991541Srgrimes } 6001541Srgrimes return ((struct llinfo_arp *)rt->rt_llinfo); 6011541Srgrimes} 6021541Srgrimes 6031541Srgrimesint 6041541Srgrimesarpioctl(cmd, data) 6051541Srgrimes int cmd; 6061541Srgrimes caddr_t data; 6071541Srgrimes{ 6081541Srgrimes return (EOPNOTSUPP); 6091541Srgrimes} 6105195Swollman 6115195Swollmanvoid 6125195Swollmanarp_ifinit(ac, ifa) 6135195Swollman struct arpcom *ac; 6145195Swollman struct ifaddr *ifa; 6155195Swollman{ 6165195Swollman ac->ac_ipaddr = IA_SIN(ifa)->sin_addr; 6175195Swollman arpwhohas(ac, &ac->ac_ipaddr); 6185195Swollman ifa->ifa_rtrequest = arp_rtrequest; 6195195Swollman ifa->ifa_flags |= RTF_CLONING; 6205195Swollman} 621