if_ether.c revision 4069
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
344069Swollman * $Id: if_ether.c,v 1.6 1994/10/11 23:16:37 wollman 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>
571541Srgrimes
581541Srgrimes#include <netinet/in.h>
591541Srgrimes#include <netinet/in_systm.h>
601541Srgrimes#include <netinet/in_var.h>
611541Srgrimes#include <netinet/ip.h>
621541Srgrimes#include <netinet/if_ether.h>
631541Srgrimes
641541Srgrimes#define SIN(s) ((struct sockaddr_in *)s)
651541Srgrimes#define SDL(s) ((struct sockaddr_dl *)s)
661541Srgrimes#define SRP(s) ((struct sockaddr_inarp *)s)
671541Srgrimes
681541Srgrimes/*
691541Srgrimes * ARP trailer negotiation.  Trailer protocol is not IP specific,
701541Srgrimes * but ARP request/response use IP addresses.
711541Srgrimes */
721541Srgrimes#define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL
731541Srgrimes
741541Srgrimes
751541Srgrimes/* timer values */
761541Srgrimesint	arpt_prune = (5*60*1);	/* walk list every 5 minutes */
771541Srgrimesint	arpt_keep = (20*60);	/* once resolved, good for 20 more minutes */
781541Srgrimesint	arpt_down = 20;		/* once declared down, don't send for 20 secs */
791541Srgrimes#define	rt_expire rt_rmx.rmx_expire
801541Srgrimes
811541Srgrimesstatic	void arprequest __P((struct arpcom *, u_long *, u_long *, u_char *));
821541Srgrimesstatic	void arptfree __P((struct llinfo_arp *));
831541Srgrimesstatic	void arptimer __P((void *));
841541Srgrimesstatic	struct llinfo_arp *arplookup __P((u_long, int, int));
851541Srgrimesstatic	void in_arpinput __P((struct mbuf *));
861541Srgrimes
871541Srgrimesextern	struct ifnet loif;
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/*
1004069Swollman * Support: format an IP address.  There should be a standard kernel routine
1014069Swollman * to do this.
1024069Swollman */
1034069Swollmanstatic char *
1044069Swollmanarp_ntoa(struct in_addr *x)
1054069Swollman{
1064069Swollman	static char buf[4*sizeof "123"];
1074069Swollman	unsigned char *ucp = (unsigned char *)x;
1084069Swollman
1094069Swollman	sprintf(buf, "%d.%d.%d.%d",
1104069Swollman		ucp[0] & 0xff,
1114069Swollman		ucp[1] & 0xff,
1124069Swollman		ucp[2] & 0xff,
1134069Swollman		ucp[3] & 0xff);
1144069Swollman	return buf;
1154069Swollman}
1164069Swollman
1174069Swollman/*
1181541Srgrimes * Timeout routine.  Age arp_tab entries periodically.
1191541Srgrimes */
1201541Srgrimes/* ARGSUSED */
1211541Srgrimesstatic void
1221541Srgrimesarptimer(ignored_arg)
1231541Srgrimes	void *ignored_arg;
1241541Srgrimes{
1251541Srgrimes	int s = splnet();
1261541Srgrimes	register struct llinfo_arp *la = llinfo_arp.la_next;
1271541Srgrimes
1281541Srgrimes	timeout(arptimer, (caddr_t)0, arpt_prune * hz);
1291541Srgrimes	while (la != &llinfo_arp) {
1301541Srgrimes		register struct rtentry *rt = la->la_rt;
1311541Srgrimes		la = la->la_next;
1321541Srgrimes		if (rt->rt_expire && rt->rt_expire <= time.tv_sec)
1331541Srgrimes			arptfree(la->la_prev); /* timer has expired, clear */
1341541Srgrimes	}
1351541Srgrimes	splx(s);
1361541Srgrimes}
1371541Srgrimes
1381541Srgrimes/*
1391541Srgrimes * Parallel to llc_rtrequest.
1401541Srgrimes */
1411541Srgrimesvoid
1421541Srgrimesarp_rtrequest(req, rt, sa)
1431541Srgrimes	int req;
1441541Srgrimes	register struct rtentry *rt;
1451541Srgrimes	struct sockaddr *sa;
1461541Srgrimes{
1471541Srgrimes	register struct sockaddr *gate = rt->rt_gateway;
1481541Srgrimes	register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
1491541Srgrimes	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1501541Srgrimes
1511541Srgrimes	if (!arpinit_done) {
1521541Srgrimes		arpinit_done = 1;
1531541Srgrimes		timeout(arptimer, (caddr_t)0, hz);
1541541Srgrimes	}
1551541Srgrimes	if (rt->rt_flags & RTF_GATEWAY)
1561541Srgrimes		return;
1571541Srgrimes	switch (req) {
1581541Srgrimes
1591541Srgrimes	case RTM_ADD:
1601541Srgrimes		/*
1611541Srgrimes		 * XXX: If this is a manually added route to interface
1621541Srgrimes		 * such as older version of routed or gated might provide,
1631541Srgrimes		 * restore cloning bit.
1641541Srgrimes		 */
1651541Srgrimes		if ((rt->rt_flags & RTF_HOST) == 0 &&
1661541Srgrimes		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1671541Srgrimes			rt->rt_flags |= RTF_CLONING;
1683514Swollman#if 0
1693514Swollman		/*
1703514Swollman		 * Actually, all IP gateway routes should have the cloning
1713514Swollman		 * flag turned on.  We can't do this yet because the expiration
1723514Swollman		 * stuff isn't working yet.
1733514Swollman		 */
1743514Swollman		if (rt->rt_flags & RTF_GATEWAY) {
1753514Swollman			rt->rt_flags |= RTF_CLONING;
1763514Swollman		}
1773514Swollman#endif
1781541Srgrimes		if (rt->rt_flags & RTF_CLONING) {
1791541Srgrimes			/*
1801541Srgrimes			 * Case 1: This route should come from a route to iface.
1811541Srgrimes			 */
1821541Srgrimes			rt_setgate(rt, rt_key(rt),
1831541Srgrimes					(struct sockaddr *)&null_sdl);
1841541Srgrimes			gate = rt->rt_gateway;
1851541Srgrimes			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
1861541Srgrimes			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
1871541Srgrimes			rt->rt_expire = time.tv_sec;
1881541Srgrimes			break;
1891541Srgrimes		}
1901541Srgrimes		/* Announce a new entry if requested. */
1911541Srgrimes		if (rt->rt_flags & RTF_ANNOUNCE)
1921541Srgrimes			arprequest((struct arpcom *)rt->rt_ifp,
1931541Srgrimes			    &SIN(rt_key(rt))->sin_addr.s_addr,
1941541Srgrimes			    &SIN(rt_key(rt))->sin_addr.s_addr,
1951541Srgrimes			    (u_char *)LLADDR(SDL(gate)));
1961541Srgrimes		/*FALLTHROUGH*/
1971541Srgrimes	case RTM_RESOLVE:
1981541Srgrimes		if (gate->sa_family != AF_LINK ||
1991541Srgrimes		    gate->sa_len < sizeof(null_sdl)) {
2001541Srgrimes			log(LOG_DEBUG, "arp_rtrequest: bad gateway value");
2011541Srgrimes			break;
2021541Srgrimes		}
2031541Srgrimes		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
2041541Srgrimes		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
2051541Srgrimes		if (la != 0)
2061541Srgrimes			break; /* This happens on a route change */
2071541Srgrimes		/*
2081541Srgrimes		 * Case 2:  This route may come from cloning, or a manual route
2091541Srgrimes		 * add with a LL address.
2101541Srgrimes		 */
2111541Srgrimes		R_Malloc(la, struct llinfo_arp *, sizeof(*la));
2121541Srgrimes		rt->rt_llinfo = (caddr_t)la;
2131541Srgrimes		if (la == 0) {
2141541Srgrimes			log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
2151541Srgrimes			break;
2161541Srgrimes		}
2171541Srgrimes		arp_inuse++, arp_allocated++;
2181541Srgrimes		Bzero(la, sizeof(*la));
2191541Srgrimes		la->la_rt = rt;
2201541Srgrimes		rt->rt_flags |= RTF_LLINFO;
2211541Srgrimes		insque(la, &llinfo_arp);
2221541Srgrimes		if (SIN(rt_key(rt))->sin_addr.s_addr ==
2231541Srgrimes		    (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
2241541Srgrimes		    /*
2251541Srgrimes		     * This test used to be
2261541Srgrimes		     *	if (loif.if_flags & IFF_UP)
2271541Srgrimes		     * It allowed local traffic to be forced
2281541Srgrimes		     * through the hardware by configuring the loopback down.
2291541Srgrimes		     * However, it causes problems during network configuration
2301541Srgrimes		     * for boards that can't receive packets they send.
2311541Srgrimes		     * It is now necessary to clear "useloopback" and remove
2321541Srgrimes		     * the route to force traffic out to the hardware.
2331541Srgrimes		     */
2341541Srgrimes			rt->rt_expire = 0;
2351541Srgrimes			Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr,
2361541Srgrimes				LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6);
2371541Srgrimes			if (useloopback)
2381541Srgrimes				rt->rt_ifp = &loif;
2391541Srgrimes
2401541Srgrimes		}
2411541Srgrimes		break;
2421541Srgrimes
2431541Srgrimes	case RTM_DELETE:
2441541Srgrimes		if (la == 0)
2451541Srgrimes			break;
2461541Srgrimes		arp_inuse--;
2471541Srgrimes		remque(la);
2481541Srgrimes		rt->rt_llinfo = 0;
2491541Srgrimes		rt->rt_flags &= ~RTF_LLINFO;
2501541Srgrimes		if (la->la_hold)
2511541Srgrimes			m_freem(la->la_hold);
2521541Srgrimes		Free((caddr_t)la);
2531541Srgrimes	}
2541541Srgrimes}
2551541Srgrimes
2561541Srgrimes/*
2571541Srgrimes * Broadcast an ARP packet, asking who has addr on interface ac.
2581541Srgrimes */
2591541Srgrimesvoid
2601541Srgrimesarpwhohas(ac, addr)
2611541Srgrimes	register struct arpcom *ac;
2621541Srgrimes	register struct in_addr *addr;
2631541Srgrimes{
2641541Srgrimes	arprequest(ac, &ac->ac_ipaddr.s_addr, &addr->s_addr, ac->ac_enaddr);
2651541Srgrimes}
2661541Srgrimes
2671541Srgrimes/*
2681541Srgrimes * Broadcast an ARP request. Caller specifies:
2691541Srgrimes *	- arp header source ip address
2701541Srgrimes *	- arp header target ip address
2711541Srgrimes *	- arp header source ethernet address
2721541Srgrimes */
2731541Srgrimesstatic void
2741541Srgrimesarprequest(ac, sip, tip, enaddr)
2751541Srgrimes	register struct arpcom *ac;
2761541Srgrimes	register u_long *sip, *tip;
2771541Srgrimes	register u_char *enaddr;
2781541Srgrimes{
2791541Srgrimes	register struct mbuf *m;
2801541Srgrimes	register struct ether_header *eh;
2811541Srgrimes	register struct ether_arp *ea;
2821541Srgrimes	struct sockaddr sa;
2831541Srgrimes
2841541Srgrimes	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
2851541Srgrimes		return;
2861541Srgrimes	m->m_len = sizeof(*ea);
2871541Srgrimes	m->m_pkthdr.len = sizeof(*ea);
2881541Srgrimes	MH_ALIGN(m, sizeof(*ea));
2891541Srgrimes	ea = mtod(m, struct ether_arp *);
2901541Srgrimes	eh = (struct ether_header *)sa.sa_data;
2911541Srgrimes	bzero((caddr_t)ea, sizeof (*ea));
2921541Srgrimes	bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
2931541Srgrimes	    sizeof(eh->ether_dhost));
2941541Srgrimes	eh->ether_type = ETHERTYPE_ARP;		/* if_output will swap */
2951541Srgrimes	ea->arp_hrd = htons(ARPHRD_ETHER);
2961541Srgrimes	ea->arp_pro = htons(ETHERTYPE_IP);
2971541Srgrimes	ea->arp_hln = sizeof(ea->arp_sha);	/* hardware address length */
2981541Srgrimes	ea->arp_pln = sizeof(ea->arp_spa);	/* protocol address length */
2991541Srgrimes	ea->arp_op = htons(ARPOP_REQUEST);
3001541Srgrimes	bcopy((caddr_t)enaddr, (caddr_t)ea->arp_sha, sizeof(ea->arp_sha));
3011541Srgrimes	bcopy((caddr_t)sip, (caddr_t)ea->arp_spa, sizeof(ea->arp_spa));
3021541Srgrimes	bcopy((caddr_t)tip, (caddr_t)ea->arp_tpa, sizeof(ea->arp_tpa));
3031541Srgrimes	sa.sa_family = AF_UNSPEC;
3041541Srgrimes	sa.sa_len = sizeof(sa);
3051541Srgrimes	(*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
3061541Srgrimes}
3071541Srgrimes
3081541Srgrimes/*
3091541Srgrimes * Resolve an IP address into an ethernet address.  If success,
3101541Srgrimes * desten is filled in.  If there is no entry in arptab,
3111541Srgrimes * set one up and broadcast a request for the IP address.
3121541Srgrimes * Hold onto this mbuf and resend it once the address
3131541Srgrimes * is finally resolved.  A return value of 1 indicates
3141541Srgrimes * that desten has been filled in and the packet should be sent
3151541Srgrimes * normally; a 0 return indicates that the packet has been
3161541Srgrimes * taken over here, either now or for later transmission.
3171541Srgrimes */
3181541Srgrimesint
3193514Swollmanarpresolve(ac, rt, m, dst, desten, rt0)
3201541Srgrimes	register struct arpcom *ac;
3211541Srgrimes	register struct rtentry *rt;
3221541Srgrimes	struct mbuf *m;
3231541Srgrimes	register struct sockaddr *dst;
3241541Srgrimes	register u_char *desten;
3253514Swollman	struct rtentry *rt0;
3261541Srgrimes{
3271541Srgrimes	register struct llinfo_arp *la;
3281541Srgrimes	struct sockaddr_dl *sdl;
3291541Srgrimes
3301541Srgrimes	if (m->m_flags & M_BCAST) {	/* broadcast */
3311541Srgrimes		bcopy((caddr_t)etherbroadcastaddr, (caddr_t)desten,
3321541Srgrimes		    sizeof(etherbroadcastaddr));
3331541Srgrimes		return (1);
3341541Srgrimes	}
3351541Srgrimes	if (m->m_flags & M_MCAST) {	/* multicast */
3361541Srgrimes		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
3371541Srgrimes		return(1);
3381541Srgrimes	}
3391541Srgrimes	if (rt)
3401541Srgrimes		la = (struct llinfo_arp *)rt->rt_llinfo;
3411541Srgrimes	else {
3423311Sphk		la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
3433311Sphk		if (la)
3441541Srgrimes			rt = la->la_rt;
3451541Srgrimes	}
3461541Srgrimes	if (la == 0 || rt == 0) {
3471541Srgrimes		log(LOG_DEBUG, "arpresolve: can't allocate llinfo");
3481541Srgrimes		m_freem(m);
3491541Srgrimes		return (0);
3501541Srgrimes	}
3511541Srgrimes	sdl = SDL(rt->rt_gateway);
3521541Srgrimes	/*
3531541Srgrimes	 * Check the address family and length is valid, the address
3541541Srgrimes	 * is resolved; otherwise, try to resolve.
3551541Srgrimes	 */
3561541Srgrimes	if ((rt->rt_expire == 0 || rt->rt_expire > time.tv_sec) &&
3571541Srgrimes	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
3581541Srgrimes		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
3591541Srgrimes		return 1;
3601541Srgrimes	}
3611541Srgrimes	/*
3621541Srgrimes	 * There is an arptab entry, but no ethernet address
3631541Srgrimes	 * response yet.  Replace the held mbuf with this
3641541Srgrimes	 * latest one.
3651541Srgrimes	 */
3661541Srgrimes	if (la->la_hold)
3671541Srgrimes		m_freem(la->la_hold);
3681541Srgrimes	la->la_hold = m;
3691541Srgrimes	if (rt->rt_expire) {
3701541Srgrimes		rt->rt_flags &= ~RTF_REJECT;
3711541Srgrimes		if (la->la_asked == 0 || rt->rt_expire != time.tv_sec) {
3721541Srgrimes			rt->rt_expire = time.tv_sec;
3731541Srgrimes			if (la->la_asked++ < arp_maxtries)
3741541Srgrimes				arpwhohas(ac, &(SIN(dst)->sin_addr));
3751541Srgrimes			else {
3761541Srgrimes				rt->rt_flags |= RTF_REJECT;
3771541Srgrimes				rt->rt_expire += arpt_down;
3781541Srgrimes				la->la_asked = 0;
3791541Srgrimes			}
3801541Srgrimes
3811541Srgrimes		}
3821541Srgrimes	}
3831541Srgrimes	return (0);
3841541Srgrimes}
3851541Srgrimes
3861541Srgrimes/*
3871541Srgrimes * Common length and type checks are done here,
3881541Srgrimes * then the protocol-specific routine is called.
3891541Srgrimes */
3901541Srgrimesvoid
3911541Srgrimesarpintr()
3921541Srgrimes{
3931541Srgrimes	register struct mbuf *m;
3941541Srgrimes	register struct arphdr *ar;
3951541Srgrimes	int s;
3961541Srgrimes
3971541Srgrimes	while (arpintrq.ifq_head) {
3981541Srgrimes		s = splimp();
3991541Srgrimes		IF_DEQUEUE(&arpintrq, m);
4001541Srgrimes		splx(s);
4011541Srgrimes		if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
4021541Srgrimes			panic("arpintr");
4031541Srgrimes		if (m->m_len >= sizeof(struct arphdr) &&
4041541Srgrimes		    (ar = mtod(m, struct arphdr *)) &&
4051541Srgrimes		    ntohs(ar->ar_hrd) == ARPHRD_ETHER &&
4061541Srgrimes		    m->m_len >=
4071541Srgrimes		      sizeof(struct arphdr) + 2 * ar->ar_hln + 2 * ar->ar_pln)
4081541Srgrimes
4091541Srgrimes			    switch (ntohs(ar->ar_pro)) {
4101541Srgrimes
4111541Srgrimes			    case ETHERTYPE_IP:
4121541Srgrimes			    case ETHERTYPE_IPTRAILERS:
4131541Srgrimes				    in_arpinput(m);
4141541Srgrimes				    continue;
4151541Srgrimes			    }
4161541Srgrimes		m_freem(m);
4171541Srgrimes	}
4181541Srgrimes}
4191541Srgrimes
4201541Srgrimes/*
4211541Srgrimes * ARP for Internet protocols on 10 Mb/s Ethernet.
4221541Srgrimes * Algorithm is that given in RFC 826.
4231541Srgrimes * In addition, a sanity check is performed on the sender
4241541Srgrimes * protocol address, to catch impersonators.
4251541Srgrimes * We no longer handle negotiations for use of trailer protocol:
4261541Srgrimes * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
4271541Srgrimes * along with IP replies if we wanted trailers sent to us,
4281541Srgrimes * and also sent them in response to IP replies.
4291541Srgrimes * This allowed either end to announce the desire to receive
4301541Srgrimes * trailer packets.
4311541Srgrimes * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
4321541Srgrimes * but formerly didn't normally send requests.
4331541Srgrimes */
4341541Srgrimesstatic void
4351541Srgrimesin_arpinput(m)
4361541Srgrimes	struct mbuf *m;
4371541Srgrimes{
4381541Srgrimes	register struct ether_arp *ea;
4391541Srgrimes	register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif;
4401541Srgrimes	struct ether_header *eh;
4411541Srgrimes	register struct llinfo_arp *la = 0;
4421541Srgrimes	register struct rtentry *rt;
4431541Srgrimes	struct in_ifaddr *ia, *maybe_ia = 0;
4441541Srgrimes	struct sockaddr_dl *sdl;
4451541Srgrimes	struct sockaddr sa;
4461541Srgrimes	struct in_addr isaddr, itaddr, myaddr;
4471541Srgrimes	int op;
4481541Srgrimes
4491541Srgrimes	ea = mtod(m, struct ether_arp *);
4501541Srgrimes	op = ntohs(ea->arp_op);
4511541Srgrimes	bcopy((caddr_t)ea->arp_spa, (caddr_t)&isaddr, sizeof (isaddr));
4521541Srgrimes	bcopy((caddr_t)ea->arp_tpa, (caddr_t)&itaddr, sizeof (itaddr));
4531541Srgrimes	for (ia = in_ifaddr; ia; ia = ia->ia_next)
4541541Srgrimes		if (ia->ia_ifp == &ac->ac_if) {
4551541Srgrimes			maybe_ia = ia;
4561541Srgrimes			if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) ||
4571541Srgrimes			     (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr))
4581541Srgrimes				break;
4591541Srgrimes		}
4601541Srgrimes	if (maybe_ia == 0)
4611541Srgrimes		goto out;
4621541Srgrimes	myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr;
4631541Srgrimes	if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr,
4641541Srgrimes	    sizeof (ea->arp_sha)))
4651541Srgrimes		goto out;	/* it's from me, ignore it. */
4661541Srgrimes	if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr,
4671541Srgrimes	    sizeof (ea->arp_sha))) {
4681541Srgrimes		log(LOG_ERR,
4694069Swollman		    "arp: ether address is broadcast for IP address %s!\n",
4704069Swollman		    arp_ntoa(&isaddr));
4711541Srgrimes		goto out;
4721541Srgrimes	}
4731541Srgrimes	if (isaddr.s_addr == myaddr.s_addr) {
4741541Srgrimes		log(LOG_ERR,
4754069Swollman		   "duplicate IP address %s! sent from ethernet address: %s\n",
4764069Swollman		   arp_ntoa(&isaddr), ether_sprintf(ea->arp_sha));
4771541Srgrimes		itaddr = myaddr;
4781541Srgrimes		goto reply;
4791541Srgrimes	}
4801541Srgrimes	la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
4811541Srgrimes	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
4821541Srgrimes		if (sdl->sdl_alen &&
4831541Srgrimes		    bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen))
4844069Swollman			log(LOG_INFO, "arp info overwritten for %s by %s\n",
4854069Swollman			    arp_ntoa(&isaddr), ether_sprintf(ea->arp_sha));
4861541Srgrimes		bcopy((caddr_t)ea->arp_sha, LLADDR(sdl),
4871541Srgrimes			    sdl->sdl_alen = sizeof(ea->arp_sha));
4881541Srgrimes		if (rt->rt_expire)
4891541Srgrimes			rt->rt_expire = time.tv_sec + arpt_keep;
4901541Srgrimes		rt->rt_flags &= ~RTF_REJECT;
4911541Srgrimes		la->la_asked = 0;
4921541Srgrimes		if (la->la_hold) {
4931541Srgrimes			(*ac->ac_if.if_output)(&ac->ac_if, la->la_hold,
4941541Srgrimes				rt_key(rt), rt);
4951541Srgrimes			la->la_hold = 0;
4961541Srgrimes		}
4971541Srgrimes	}
4981541Srgrimesreply:
4991541Srgrimes	if (op != ARPOP_REQUEST) {
5001541Srgrimes	out:
5011541Srgrimes		m_freem(m);
5021541Srgrimes		return;
5031541Srgrimes	}
5041541Srgrimes	if (itaddr.s_addr == myaddr.s_addr) {
5051541Srgrimes		/* I am the target */
5061541Srgrimes		bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha,
5071541Srgrimes		    sizeof(ea->arp_sha));
5081541Srgrimes		bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha,
5091541Srgrimes		    sizeof(ea->arp_sha));
5101541Srgrimes	} else {
5111541Srgrimes		la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
5123282Swollman		if (la == NULL) {
5133282Swollman#ifdef ARP_PROXYALL
5143282Swollman			struct sockaddr_in sin;
5153282Swollman
5163282Swollman			if(!arp_proxyall) goto out;
5173282Swollman
5183282Swollman			bzero(&sin, sizeof sin);
5193282Swollman			sin.sin_family = AF_INET;
5203282Swollman			sin.sin_len = sizeof sin;
5213282Swollman			sin.sin_addr = itaddr;
5223282Swollman
5233282Swollman			rt = rtalloc1((struct sockaddr *)&sin, 0);
5243282Swollman			if( !rt )
5253282Swollman				goto out;
5263282Swollman			/*
5273282Swollman			 * Don't send proxies for nodes on the same interface
5283282Swollman			 * as this one came out of, or we'll get into a fight
5293282Swollman			 * over who claims what Ether address.
5303282Swollman			 */
5313282Swollman			if(rt->rt_ifp == &ac->ac_if) {
5323282Swollman				rtfree(rt);
5333282Swollman				goto out;
5343282Swollman			}
5353282Swollman			bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha,
5363282Swollman			      sizeof(ea->arp_sha));
5373282Swollman			bcopy(ac->ac_enaddr, (caddr_t)ea->arp_sha,
5383282Swollman			      sizeof(ea->arp_sha));
5393282Swollman			rtfree(rt);
5404069Swollman#ifdef DEBUG_PROXY
5414069Swollman			printf("arp: proxying for %s\n",
5424069Swollman			       arp_ntoa(&itaddr));
5434069Swollman#endif
5443282Swollman#else
5451541Srgrimes			goto out;
5463282Swollman#endif
5473282Swollman		} else {
5483282Swollman			rt = la->la_rt;
5493282Swollman			bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha,
5503282Swollman			      sizeof(ea->arp_sha));
5513282Swollman			sdl = SDL(rt->rt_gateway);
5523282Swollman			bcopy(LLADDR(sdl), (caddr_t)ea->arp_sha,
5533282Swollman			      sizeof(ea->arp_sha));
5543282Swollman		}
5551541Srgrimes	}
5561541Srgrimes
5571541Srgrimes	bcopy((caddr_t)ea->arp_spa, (caddr_t)ea->arp_tpa, sizeof(ea->arp_spa));
5581541Srgrimes	bcopy((caddr_t)&itaddr, (caddr_t)ea->arp_spa, sizeof(ea->arp_spa));
5591541Srgrimes	ea->arp_op = htons(ARPOP_REPLY);
5601541Srgrimes	ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */
5611541Srgrimes	eh = (struct ether_header *)sa.sa_data;
5621541Srgrimes	bcopy((caddr_t)ea->arp_tha, (caddr_t)eh->ether_dhost,
5631541Srgrimes	    sizeof(eh->ether_dhost));
5641541Srgrimes	eh->ether_type = ETHERTYPE_ARP;
5651541Srgrimes	sa.sa_family = AF_UNSPEC;
5661541Srgrimes	sa.sa_len = sizeof(sa);
5671541Srgrimes	(*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
5681541Srgrimes	return;
5691541Srgrimes}
5701541Srgrimes
5711541Srgrimes/*
5721541Srgrimes * Free an arp entry.
5731541Srgrimes */
5741541Srgrimesstatic void
5751541Srgrimesarptfree(la)
5761541Srgrimes	register struct llinfo_arp *la;
5771541Srgrimes{
5781541Srgrimes	register struct rtentry *rt = la->la_rt;
5791541Srgrimes	register struct sockaddr_dl *sdl;
5801541Srgrimes	if (rt == 0)
5811541Srgrimes		panic("arptfree");
5821541Srgrimes	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
5831541Srgrimes	    sdl->sdl_family == AF_LINK) {
5841541Srgrimes		sdl->sdl_alen = 0;
5851541Srgrimes		la->la_asked = 0;
5861541Srgrimes		rt->rt_flags &= ~RTF_REJECT;
5871541Srgrimes		return;
5881541Srgrimes	}
5891541Srgrimes	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
5901541Srgrimes			0, (struct rtentry **)0);
5911541Srgrimes}
5921541Srgrimes/*
5931541Srgrimes * Lookup or enter a new address in arptab.
5941541Srgrimes */
5951541Srgrimesstatic struct llinfo_arp *
5961541Srgrimesarplookup(addr, create, proxy)
5971541Srgrimes	u_long addr;
5981541Srgrimes	int create, proxy;
5991541Srgrimes{
6001541Srgrimes	register struct rtentry *rt;
6011541Srgrimes	static struct sockaddr_inarp sin = {sizeof(sin), AF_INET };
6024069Swollman	const char *why = 0;
6031541Srgrimes
6041541Srgrimes	sin.sin_addr.s_addr = addr;
6051541Srgrimes	sin.sin_other = proxy ? SIN_PROXY : 0;
6061541Srgrimes	rt = rtalloc1((struct sockaddr *)&sin, create);
6071541Srgrimes	if (rt == 0)
6081541Srgrimes		return (0);
6091541Srgrimes	rt->rt_refcnt--;
6104069Swollman
6114069Swollman	if(rt->rt_flags & RTF_GATEWAY)
6124069Swollman		why = "host is not on local network";
6134069Swollman	else if((rt->rt_flags & RTF_LLINFO) == 0)
6144069Swollman		why = "could not allocate llinfo";
6154069Swollman	else if(rt->rt_gateway->sa_family != AF_LINK)
6164069Swollman		why = "gateway route is not ours";
6174069Swollman
6184069Swollman	if(why && create) {
6194069Swollman		log(LOG_DEBUG, "arplookup %s failed: %s\n",
6204069Swollman		    arp_ntoa(&sin.sin_addr), why);
6214069Swollman		return 0;
6224069Swollman	} else if(why) {
6234069Swollman		return 0;
6241541Srgrimes	}
6251541Srgrimes	return ((struct llinfo_arp *)rt->rt_llinfo);
6261541Srgrimes}
6271541Srgrimes
6281541Srgrimesint
6291541Srgrimesarpioctl(cmd, data)
6301541Srgrimes	int cmd;
6311541Srgrimes	caddr_t data;
6321541Srgrimes{
6331541Srgrimes	return (EOPNOTSUPP);
6341541Srgrimes}
635