if_ether.c revision 92723
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 92723 2002-03-19 21:25:46Z alfred $
351541Srgrimes */
361541Srgrimes
371541Srgrimes/*
381541Srgrimes * Ethernet address resolution protocol.
391541Srgrimes * TODO:
401541Srgrimes *	add "inuse/lock" bit (or ref. count) along with valid bit
411541Srgrimes */
421541Srgrimes
4332350Seivind#include "opt_inet.h"
4441793Sluigi#include "opt_bdg.h"
4532350Seivind
461541Srgrimes#include <sys/param.h>
4712693Sphk#include <sys/kernel.h>
4844078Sdfr#include <sys/queue.h>
4912693Sphk#include <sys/sysctl.h>
501541Srgrimes#include <sys/systm.h>
5112693Sphk#include <sys/mbuf.h>
521541Srgrimes#include <sys/malloc.h>
5318892Sbde#include <sys/socket.h>
541541Srgrimes#include <sys/syslog.h>
551541Srgrimes
561541Srgrimes#include <net/if.h>
571541Srgrimes#include <net/if_dl.h>
5844165Sjulian#include <net/if_types.h>
591541Srgrimes#include <net/route.h>
608426Swollman#include <net/netisr.h>
6158313Slile#include <net/if_llc.h>
6271963Sjulian#ifdef BRIDGE
6371963Sjulian#include <net/ethernet.h>
6471963Sjulian#include <net/bridge.h>
6571963Sjulian#endif
661541Srgrimes
671541Srgrimes#include <netinet/in.h>
681541Srgrimes#include <netinet/in_var.h>
691541Srgrimes#include <netinet/if_ether.h>
701541Srgrimes
7184931Sfjoe#include <net/if_arc.h>
7244627Sjulian#include <net/iso88025.h>
7344627Sjulian
741541Srgrimes#define SIN(s) ((struct sockaddr_in *)s)
751541Srgrimes#define SDL(s) ((struct sockaddr_dl *)s)
761541Srgrimes
7744078SdfrSYSCTL_DECL(_net_link_ether);
7812942SwollmanSYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
791541Srgrimes
8012693Sphk/* timer values */
8112942Swollmanstatic int arpt_prune = (5*60*1); /* walk list every 5 minutes */
8212942Swollmanstatic int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
8312942Swollmanstatic int arpt_down = 20;	/* once declared down, don't send for 20 sec */
841541Srgrimes
8512942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
8612942Swollman	   &arpt_prune, 0, "");
8712942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
8812942Swollman	   &arpt_keep, 0, "");
8912942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
9012942Swollman	   &arpt_down, 0, "");
9112693Sphk
921541Srgrimes#define	rt_expire rt_rmx.rmx_expire
931541Srgrimes
9411225Swollmanstruct llinfo_arp {
9560938Sjake	LIST_ENTRY(llinfo_arp) la_le;
9611225Swollman	struct	rtentry *la_rt;
9711225Swollman	struct	mbuf *la_hold;		/* last packet until resolved/timeout */
9811225Swollman	long	la_asked;		/* last time we QUERIED for this addr */
9911225Swollman#define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */
10011225Swollman};
10112693Sphk
10260938Sjakestatic	LIST_HEAD(, llinfo_arp) llinfo_arp;
10311225Swollman
10469152Sjlemonstruct	ifqueue arpintrq;
10512820Sphkstatic int	arp_inuse, arp_allocated;
1061541Srgrimes
10712693Sphkstatic int	arp_maxtries = 5;
10812942Swollmanstatic int	useloopback = 1; /* use loopback interface for local traffic */
10912942Swollmanstatic int	arp_proxyall = 0;
1103282Swollman
11112942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
11212942Swollman	   &arp_maxtries, 0, "");
11312942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
11412942Swollman	   &useloopback, 0, "");
11512942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
11612942Swollman	   &arp_proxyall, 0, "");
11712693Sphk
11892723Salfredstatic void	arp_init(void);
11992723Salfredstatic void	arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
12092723Salfredstatic void	arprequest(struct ifnet *,
12192723Salfred			struct in_addr *, struct in_addr *, u_char *);
12292723Salfredstatic void	arpintr(void);
12392723Salfredstatic void	arptfree(struct llinfo_arp *);
12492723Salfredstatic void	arptimer(void *);
12512693Sphkstatic struct llinfo_arp
12692723Salfred		*arplookup(u_long, int, int);
12732350Seivind#ifdef INET
12892723Salfredstatic void	in_arpinput(struct mbuf *);
12932350Seivind#endif
13012693Sphk
1311541Srgrimes/*
1321541Srgrimes * Timeout routine.  Age arp_tab entries periodically.
1331541Srgrimes */
1341541Srgrimes/* ARGSUSED */
1351541Srgrimesstatic void
1361541Srgrimesarptimer(ignored_arg)
1371541Srgrimes	void *ignored_arg;
1381541Srgrimes{
1391541Srgrimes	int s = splnet();
14071999Sphk	register struct llinfo_arp *la = LIST_FIRST(&llinfo_arp);
14111225Swollman	struct llinfo_arp *ola;
1421541Srgrimes
1431541Srgrimes	timeout(arptimer, (caddr_t)0, arpt_prune * hz);
14411225Swollman	while ((ola = la) != 0) {
1451541Srgrimes		register struct rtentry *rt = la->la_rt;
14671999Sphk		la = LIST_NEXT(la, la_le);
14734961Sphk		if (rt->rt_expire && rt->rt_expire <= time_second)
14811225Swollman			arptfree(ola); /* timer has expired, clear */
1491541Srgrimes	}
1501541Srgrimes	splx(s);
1511541Srgrimes}
1521541Srgrimes
1531541Srgrimes/*
1541541Srgrimes * Parallel to llc_rtrequest.
1551541Srgrimes */
1565196Swollmanstatic void
15785074Sruarp_rtrequest(req, rt, info)
1581541Srgrimes	int req;
1591541Srgrimes	register struct rtentry *rt;
16085074Sru	struct rt_addrinfo *info;
1611541Srgrimes{
1621541Srgrimes	register struct sockaddr *gate = rt->rt_gateway;
1631541Srgrimes	register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
1641541Srgrimes	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
16512693Sphk	static int arpinit_done;
1661541Srgrimes
1671541Srgrimes	if (!arpinit_done) {
1681541Srgrimes		arpinit_done = 1;
16911225Swollman		LIST_INIT(&llinfo_arp);
1701541Srgrimes		timeout(arptimer, (caddr_t)0, hz);
17157178Speter		register_netisr(NETISR_ARP, arpintr);
1721541Srgrimes	}
1731541Srgrimes	if (rt->rt_flags & RTF_GATEWAY)
1741541Srgrimes		return;
1751541Srgrimes	switch (req) {
1761541Srgrimes
1771541Srgrimes	case RTM_ADD:
1781541Srgrimes		/*
1791541Srgrimes		 * XXX: If this is a manually added route to interface
1801541Srgrimes		 * such as older version of routed or gated might provide,
1811541Srgrimes		 * restore cloning bit.
1821541Srgrimes		 */
1831541Srgrimes		if ((rt->rt_flags & RTF_HOST) == 0 &&
1841541Srgrimes		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1851541Srgrimes			rt->rt_flags |= RTF_CLONING;
1861541Srgrimes		if (rt->rt_flags & RTF_CLONING) {
1871541Srgrimes			/*
1881541Srgrimes			 * Case 1: This route should come from a route to iface.
1891541Srgrimes			 */
1901541Srgrimes			rt_setgate(rt, rt_key(rt),
1911541Srgrimes					(struct sockaddr *)&null_sdl);
1921541Srgrimes			gate = rt->rt_gateway;
1931541Srgrimes			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
1941541Srgrimes			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
19534961Sphk			rt->rt_expire = time_second;
1961541Srgrimes			break;
1971541Srgrimes		}
1981541Srgrimes		/* Announce a new entry if requested. */
1991541Srgrimes		if (rt->rt_flags & RTF_ANNOUNCE)
20084931Sfjoe			arprequest(rt->rt_ifp,
20136908Sjulian			    &SIN(rt_key(rt))->sin_addr,
20236908Sjulian			    &SIN(rt_key(rt))->sin_addr,
2031541Srgrimes			    (u_char *)LLADDR(SDL(gate)));
2041541Srgrimes		/*FALLTHROUGH*/
2051541Srgrimes	case RTM_RESOLVE:
2061541Srgrimes		if (gate->sa_family != AF_LINK ||
2071541Srgrimes		    gate->sa_len < sizeof(null_sdl)) {
2086568Sdg			log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
2091541Srgrimes			break;
2101541Srgrimes		}
2111541Srgrimes		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
2121541Srgrimes		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
2131541Srgrimes		if (la != 0)
2141541Srgrimes			break; /* This happens on a route change */
2151541Srgrimes		/*
2161541Srgrimes		 * Case 2:  This route may come from cloning, or a manual route
2171541Srgrimes		 * add with a LL address.
2181541Srgrimes		 */
2191541Srgrimes		R_Malloc(la, struct llinfo_arp *, sizeof(*la));
2201541Srgrimes		rt->rt_llinfo = (caddr_t)la;
2211541Srgrimes		if (la == 0) {
2221541Srgrimes			log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
2231541Srgrimes			break;
2241541Srgrimes		}
2251541Srgrimes		arp_inuse++, arp_allocated++;
2261541Srgrimes		Bzero(la, sizeof(*la));
2271541Srgrimes		la->la_rt = rt;
2281541Srgrimes		rt->rt_flags |= RTF_LLINFO;
22911225Swollman		LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
23013926Swollman
23132350Seivind#ifdef INET
23213926Swollman		/*
23313926Swollman		 * This keeps the multicast addresses from showing up
23413926Swollman		 * in `arp -a' listings as unresolved.  It's not actually
23513926Swollman		 * functional.  Then the same for broadcast.
23613926Swollman		 */
23787776Sjlemon		if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) &&
23887776Sjlemon		    rt->rt_ifp->if_type != IFT_ARCNET) {
23913926Swollman			ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
24013926Swollman					       LLADDR(SDL(gate)));
24113926Swollman			SDL(gate)->sdl_alen = 6;
24216576Speter			rt->rt_expire = 0;
24313926Swollman		}
24413926Swollman		if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
24584931Sfjoe			memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
24684931Sfjoe			       rt->rt_ifp->if_addrlen);
24784931Sfjoe			SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
24816576Speter			rt->rt_expire = 0;
24913926Swollman		}
25032350Seivind#endif
25113926Swollman
2521541Srgrimes		if (SIN(rt_key(rt))->sin_addr.s_addr ==
2531541Srgrimes		    (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
2541541Srgrimes		    /*
2551541Srgrimes		     * This test used to be
2561541Srgrimes		     *	if (loif.if_flags & IFF_UP)
2571541Srgrimes		     * It allowed local traffic to be forced
2581541Srgrimes		     * through the hardware by configuring the loopback down.
2591541Srgrimes		     * However, it causes problems during network configuration
2601541Srgrimes		     * for boards that can't receive packets they send.
2611541Srgrimes		     * It is now necessary to clear "useloopback" and remove
2621541Srgrimes		     * the route to force traffic out to the hardware.
2631541Srgrimes		     */
2641541Srgrimes			rt->rt_expire = 0;
26584931Sfjoe			Bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
26684931Sfjoe			      SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
2671541Srgrimes			if (useloopback)
2688090Spst				rt->rt_ifp = loif;
2691541Srgrimes
2701541Srgrimes		}
2711541Srgrimes		break;
2721541Srgrimes
2731541Srgrimes	case RTM_DELETE:
2741541Srgrimes		if (la == 0)
2751541Srgrimes			break;
2761541Srgrimes		arp_inuse--;
27711225Swollman		LIST_REMOVE(la, la_le);
2781541Srgrimes		rt->rt_llinfo = 0;
2791541Srgrimes		rt->rt_flags &= ~RTF_LLINFO;
2801541Srgrimes		if (la->la_hold)
2811541Srgrimes			m_freem(la->la_hold);
2821541Srgrimes		Free((caddr_t)la);
2831541Srgrimes	}
2841541Srgrimes}
2851541Srgrimes
2861541Srgrimes/*
2871541Srgrimes * Broadcast an ARP request. Caller specifies:
2881541Srgrimes *	- arp header source ip address
2891541Srgrimes *	- arp header target ip address
2901541Srgrimes *	- arp header source ethernet address
2911541Srgrimes */
2921541Srgrimesstatic void
29384931Sfjoearprequest(ifp, sip, tip, enaddr)
29484931Sfjoe	register struct ifnet *ifp;
29536908Sjulian	register struct in_addr *sip, *tip;
2961541Srgrimes	register u_char *enaddr;
2971541Srgrimes{
2981541Srgrimes	register struct mbuf *m;
2991541Srgrimes	register struct ether_header *eh;
30084931Sfjoe	register struct arc_header *arh;
30184931Sfjoe	register struct arphdr *ah;
3021541Srgrimes	struct sockaddr sa;
30358313Slile	static u_char	llcx[] = { 0x82, 0x40, LLC_SNAP_LSAP, LLC_SNAP_LSAP,
30458313Slile				   LLC_UI, 0x00, 0x00, 0x00, 0x08, 0x06 };
30584931Sfjoe	u_short ar_hrd;
3061541Srgrimes
3071541Srgrimes	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
3081541Srgrimes		return;
30944456Swpaul	m->m_pkthdr.rcvif = (struct ifnet *)0;
31084931Sfjoe	switch (ifp->if_type) {
31184931Sfjoe	case IFT_ARCNET:
31284931Sfjoe		ar_hrd = htons(ARPHRD_ARCNET);
31384931Sfjoe
31484931Sfjoe		m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
31584931Sfjoe		m->m_pkthdr.len = m->m_len;
31684931Sfjoe		MH_ALIGN(m, m->m_len);
31784931Sfjoe
31884931Sfjoe		arh = (struct arc_header *)sa.sa_data;
31984931Sfjoe		arh->arc_dhost = *ifp->if_broadcastaddr;
32084931Sfjoe		arh->arc_type = ARCTYPE_ARP;
32184931Sfjoe
32284931Sfjoe		ah = mtod(m, struct arphdr *);
32384931Sfjoe		break;
32484931Sfjoe
32544627Sjulian	case IFT_ISO88025:
32684931Sfjoe		ar_hrd = htons(ARPHRD_IEEE802);
32784931Sfjoe
32884931Sfjoe		m->m_len = sizeof(llcx) +
32984931Sfjoe		    arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
33084931Sfjoe		m->m_pkthdr.len = m->m_len;
33184931Sfjoe		MH_ALIGN(m, m->m_len);
33284931Sfjoe
33358313Slile		(void)memcpy(mtod(m, caddr_t), llcx, sizeof(llcx));
33484931Sfjoe		(void)memcpy(sa.sa_data, ifp->if_broadcastaddr, 6);
33544627Sjulian		(void)memcpy(sa.sa_data + 6, enaddr, 6);
33658313Slile		sa.sa_data[6] |= TR_RII;
33758313Slile		sa.sa_data[12] = TR_AC;
33858313Slile		sa.sa_data[13] = TR_LLC_FRAME;
33984931Sfjoe
34084931Sfjoe		ah = (struct arphdr *)(mtod(m, char *) + sizeof(llcx));
34144627Sjulian		break;
34251320Slile	case IFT_FDDI:
34351320Slile	case IFT_ETHER:
34451320Slile		/*
34551320Slile		 * This may not be correct for types not explicitly
34651320Slile		 * listed, but this is our best guess
34751320Slile		 */
34844627Sjulian	default:
34984931Sfjoe		ar_hrd = htons(ARPHRD_ETHER);
35084931Sfjoe
35184931Sfjoe		m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
35284931Sfjoe		m->m_pkthdr.len = m->m_len;
35384931Sfjoe		MH_ALIGN(m, m->m_len);
35484931Sfjoe
35551320Slile		eh = (struct ether_header *)sa.sa_data;
35651320Slile		/* if_output will not swap */
35751320Slile		eh->ether_type = htons(ETHERTYPE_ARP);
35884931Sfjoe		(void)memcpy(eh->ether_dhost, ifp->if_broadcastaddr,
35951320Slile		    sizeof(eh->ether_dhost));
36084931Sfjoe
36184931Sfjoe		ah = mtod(m, struct arphdr *);
36251320Slile		break;
36344627Sjulian	}
36484931Sfjoe
36584931Sfjoe	ah->ar_hrd = ar_hrd;
36684931Sfjoe	ah->ar_pro = htons(ETHERTYPE_IP);
36784931Sfjoe	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
36884931Sfjoe	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
36984931Sfjoe	ah->ar_op = htons(ARPOP_REQUEST);
37084931Sfjoe	(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
37184931Sfjoe	(void)memcpy(ar_spa(ah), sip, ah->ar_pln);
37284931Sfjoe	(void)memcpy(ar_tpa(ah), tip, ah->ar_pln);
37384931Sfjoe
3741541Srgrimes	sa.sa_family = AF_UNSPEC;
3751541Srgrimes	sa.sa_len = sizeof(sa);
37684931Sfjoe	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
3771541Srgrimes}
3781541Srgrimes
3791541Srgrimes/*
3801541Srgrimes * Resolve an IP address into an ethernet address.  If success,
3811541Srgrimes * desten is filled in.  If there is no entry in arptab,
3821541Srgrimes * set one up and broadcast a request for the IP address.
3831541Srgrimes * Hold onto this mbuf and resend it once the address
3841541Srgrimes * is finally resolved.  A return value of 1 indicates
3851541Srgrimes * that desten has been filled in and the packet should be sent
3861541Srgrimes * normally; a 0 return indicates that the packet has been
3871541Srgrimes * taken over here, either now or for later transmission.
3881541Srgrimes */
3891541Srgrimesint
39084931Sfjoearpresolve(ifp, rt, m, dst, desten, rt0)
39184931Sfjoe	register struct ifnet *ifp;
3921541Srgrimes	register struct rtentry *rt;
3931541Srgrimes	struct mbuf *m;
3941541Srgrimes	register struct sockaddr *dst;
3951541Srgrimes	register u_char *desten;
3963514Swollman	struct rtentry *rt0;
3971541Srgrimes{
39878295Sjlemon	struct llinfo_arp *la = 0;
3991541Srgrimes	struct sockaddr_dl *sdl;
4001541Srgrimes
4011541Srgrimes	if (m->m_flags & M_BCAST) {	/* broadcast */
40284931Sfjoe		(void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
4031541Srgrimes		return (1);
4041541Srgrimes	}
40584931Sfjoe	if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */
4061541Srgrimes		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
4071541Srgrimes		return(1);
4081541Srgrimes	}
4091541Srgrimes	if (rt)
4101541Srgrimes		la = (struct llinfo_arp *)rt->rt_llinfo;
41142775Sfenner	if (la == 0) {
4123311Sphk		la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
4133311Sphk		if (la)
4141541Srgrimes			rt = la->la_rt;
4151541Srgrimes	}
4161541Srgrimes	if (la == 0 || rt == 0) {
41736308Sphk		log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
41836308Sphk			inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "",
41936308Sphk				rt ? "rt" : "");
4201541Srgrimes		m_freem(m);
4211541Srgrimes		return (0);
4221541Srgrimes	}
4231541Srgrimes	sdl = SDL(rt->rt_gateway);
4241541Srgrimes	/*
4251541Srgrimes	 * Check the address family and length is valid, the address
4261541Srgrimes	 * is resolved; otherwise, try to resolve.
4271541Srgrimes	 */
42834961Sphk	if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
4291541Srgrimes	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
43016206Sbde		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
4311541Srgrimes		return 1;
4321541Srgrimes	}
4331541Srgrimes	/*
43478295Sjlemon	 * If ARP is disabled on this interface, stop.
43578295Sjlemon	 * XXX
43678295Sjlemon	 * Probably should not allocate empty llinfo struct if we are
43778295Sjlemon	 * not going to be sending out an arp request.
43878295Sjlemon	 */
43987410Sru	if (ifp->if_flags & IFF_NOARP) {
44087410Sru		m_freem(m);
44178295Sjlemon		return (0);
44287410Sru	}
44378295Sjlemon	/*
4441541Srgrimes	 * There is an arptab entry, but no ethernet address
4451541Srgrimes	 * response yet.  Replace the held mbuf with this
4461541Srgrimes	 * latest one.
4471541Srgrimes	 */
4481541Srgrimes	if (la->la_hold)
4491541Srgrimes		m_freem(la->la_hold);
4501541Srgrimes	la->la_hold = m;
4511541Srgrimes	if (rt->rt_expire) {
4521541Srgrimes		rt->rt_flags &= ~RTF_REJECT;
45334961Sphk		if (la->la_asked == 0 || rt->rt_expire != time_second) {
45434961Sphk			rt->rt_expire = time_second;
4551541Srgrimes			if (la->la_asked++ < arp_maxtries)
45684931Sfjoe			    arprequest(ifp,
45736908Sjulian			        &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
45884931Sfjoe				&SIN(dst)->sin_addr,
45984931Sfjoe				IF_LLADDR(ifp));
4601541Srgrimes			else {
4611541Srgrimes				rt->rt_flags |= RTF_REJECT;
4621541Srgrimes				rt->rt_expire += arpt_down;
4631541Srgrimes				la->la_asked = 0;
4641541Srgrimes			}
4651541Srgrimes
4661541Srgrimes		}
4671541Srgrimes	}
4681541Srgrimes	return (0);
4691541Srgrimes}
4701541Srgrimes
4711541Srgrimes/*
4721541Srgrimes * Common length and type checks are done here,
4731541Srgrimes * then the protocol-specific routine is called.
4741541Srgrimes */
47512693Sphkstatic void
47631884Sbdearpintr()
4771541Srgrimes{
47859143Swes	register struct mbuf *m;
4791541Srgrimes	register struct arphdr *ar;
48059143Swes	int s;
4811541Srgrimes
4821541Srgrimes	while (arpintrq.ifq_head) {
4831541Srgrimes		s = splimp();
4841541Srgrimes		IF_DEQUEUE(&arpintrq, m);
4851541Srgrimes		splx(s);
4861541Srgrimes		if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
4871541Srgrimes			panic("arpintr");
48857900Srwatson
48957900Srwatson                if (m->m_len < sizeof(struct arphdr) &&
49058499Sdillon                    ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
49158758Sjoerg			log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
49257900Srwatson			continue;
49357900Srwatson		}
49457900Srwatson		ar = mtod(m, struct arphdr *);
4951541Srgrimes
49657900Srwatson		if (ntohs(ar->ar_hrd) != ARPHRD_ETHER
49784931Sfjoe		    && ntohs(ar->ar_hrd) != ARPHRD_IEEE802
49884931Sfjoe		    && ntohs(ar->ar_hrd) != ARPHRD_ARCNET) {
49957900Srwatson			log(LOG_ERR,
50058758Sjoerg			    "arp: unknown hardware address format (0x%2D)\n",
50157900Srwatson			    (unsigned char *)&ar->ar_hrd, "");
50257900Srwatson			m_freem(m);
50357900Srwatson			continue;
50457900Srwatson		}
5051541Srgrimes
50684931Sfjoe		if (m->m_pkthdr.len < arphdr_len(ar) &&
50784931Sfjoe		    (m = m_pullup(m, arphdr_len(ar))) == NULL) {
50858770Sjoerg			log(LOG_ERR, "arp: runt packet\n");
50957900Srwatson			m_freem(m);
51057900Srwatson			continue;
51157900Srwatson		}
51257900Srwatson
51357900Srwatson		switch (ntohs(ar->ar_pro)) {
51432350Seivind#ifdef INET
51557900Srwatson			case ETHERTYPE_IP:
51657900Srwatson				in_arpinput(m);
51757900Srwatson				continue;
51832350Seivind#endif
51957900Srwatson		}
5201541Srgrimes		m_freem(m);
5211541Srgrimes	}
5221541Srgrimes}
5231541Srgrimes
52432350Seivind#ifdef INET
5251541Srgrimes/*
5261541Srgrimes * ARP for Internet protocols on 10 Mb/s Ethernet.
5271541Srgrimes * Algorithm is that given in RFC 826.
5281541Srgrimes * In addition, a sanity check is performed on the sender
5291541Srgrimes * protocol address, to catch impersonators.
5301541Srgrimes * We no longer handle negotiations for use of trailer protocol:
5311541Srgrimes * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
5321541Srgrimes * along with IP replies if we wanted trailers sent to us,
5331541Srgrimes * and also sent them in response to IP replies.
5341541Srgrimes * This allowed either end to announce the desire to receive
5351541Srgrimes * trailer packets.
5361541Srgrimes * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
5371541Srgrimes * but formerly didn't normally send requests.
5381541Srgrimes */
53970699Salfredstatic int log_arp_wrong_iface = 1;
54082893Salfredstatic int log_arp_movements = 1;
54170699Salfred
54270699SalfredSYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
54370699Salfred	&log_arp_wrong_iface, 0,
54470699Salfred	"log arp packets arriving on the wrong interface");
54582893SalfredSYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
54682893Salfred        &log_arp_movements, 0,
54782966Salfred        "log arp replies from MACs different than the one in the cache");
54870699Salfred
54982893Salfred
5501541Srgrimesstatic void
5511541Srgrimesin_arpinput(m)
5521541Srgrimes	struct mbuf *m;
5531541Srgrimes{
55484931Sfjoe	register struct arphdr *ah;
55584931Sfjoe	register struct ifnet *ifp = m->m_pkthdr.rcvif;
5561541Srgrimes	struct ether_header *eh;
55784931Sfjoe	struct arc_header *arh;
55844627Sjulian	struct iso88025_header *th = (struct iso88025_header *)0;
5591541Srgrimes	register struct llinfo_arp *la = 0;
5601541Srgrimes	register struct rtentry *rt;
56184102Sjlemon	struct ifaddr *ifa;
56284102Sjlemon	struct in_ifaddr *ia;
5631541Srgrimes	struct sockaddr_dl *sdl;
5641541Srgrimes	struct sockaddr sa;
5651541Srgrimes	struct in_addr isaddr, itaddr, myaddr;
56658313Slile	int op, rif_len;
56784931Sfjoe	int req_len;
5681541Srgrimes
56984931Sfjoe	req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
57084931Sfjoe	if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
57174851Syar		log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
57274851Syar		return;
57374851Syar	}
57474851Syar
57584931Sfjoe	ah = mtod(m, struct arphdr *);
57684931Sfjoe	op = ntohs(ah->ar_op);
57784931Sfjoe	(void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
57884931Sfjoe	(void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
57972056Sjulian#ifdef BRIDGE
58072056Sjulian#define BRIDGE_TEST (do_bridge)
58171963Sjulian#else
58272056Sjulian#define BRIDGE_TEST (0) /* cc will optimise the test away */
58341793Sluigi#endif
58484102Sjlemon	/*
58584102Sjlemon	 * For a bridge, we want to check the address irrespective
58684102Sjlemon	 * of the receive interface. (This will change slightly
58784102Sjlemon	 * when we have clusters of interfaces).
58884102Sjlemon	 */
58984102Sjlemon	LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash)
59084931Sfjoe		if ((BRIDGE_TEST || (ia->ia_ifp == ifp)) &&
59184102Sjlemon		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
59284102Sjlemon			goto match;
59384102Sjlemon	LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
59484931Sfjoe		if ((BRIDGE_TEST || (ia->ia_ifp == ifp)) &&
59584102Sjlemon		    isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
59684102Sjlemon			goto match;
59784102Sjlemon	/*
59885223Sjlemon	 * No match, use the first inet address on the receive interface
59984102Sjlemon	 * as a dummy address for the rest of the function.
60084102Sjlemon	 */
60185223Sjlemon	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
60285466Sjlemon		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
60385466Sjlemon			ia = ifatoia(ifa);
60485466Sjlemon			goto match;
60585466Sjlemon		}
60685466Sjlemon	/*
60785466Sjlemon	 * If bridging, fall back to using any inet address.
60885466Sjlemon	 */
60985466Sjlemon	if (!BRIDGE_TEST ||
61085466Sjlemon	    (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL) {
61112693Sphk		m_freem(m);
61212693Sphk		return;
61312693Sphk	}
61484102Sjlemonmatch:
61584102Sjlemon	myaddr = ia->ia_addr.sin_addr;
61684931Sfjoe	if (!bcmp(ar_sha(ah), IF_LLADDR(ifp), ifp->if_addrlen)) {
61712693Sphk		m_freem(m);	/* it's from me, ignore it. */
61812693Sphk		return;
61912693Sphk	}
62084931Sfjoe	if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
6211541Srgrimes		log(LOG_ERR,
62284931Sfjoe		    "arp: link address is broadcast for IP address %s!\n",
6237088Swollman		    inet_ntoa(isaddr));
62412693Sphk		m_freem(m);
62512693Sphk		return;
6261541Srgrimes	}
6271541Srgrimes	if (isaddr.s_addr == myaddr.s_addr) {
6281541Srgrimes		log(LOG_ERR,
62984931Sfjoe		   "arp: %*D is using my IP address %s!\n",
63084931Sfjoe		   ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
63184931Sfjoe		   inet_ntoa(isaddr));
6321541Srgrimes		itaddr = myaddr;
6331541Srgrimes		goto reply;
6341541Srgrimes	}
6351541Srgrimes	la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
6361541Srgrimes	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
63772270Sluigi		/* the following is not an error when doing bridging */
63884931Sfjoe		if (!BRIDGE_TEST && rt->rt_ifp != ifp) {
63972270Sluigi			if (log_arp_wrong_iface)
64084931Sfjoe				log(LOG_ERR, "arp: %s is on %s%d but got reply from %*D on %s%d\n",
64171963Sjulian				    inet_ntoa(isaddr),
64271963Sjulian				    rt->rt_ifp->if_name, rt->rt_ifp->if_unit,
64384931Sfjoe				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
64484931Sfjoe				    ifp->if_name, ifp->if_unit);
64572270Sluigi			goto reply;
64639389Sfenner		}
6471541Srgrimes		if (sdl->sdl_alen &&
64884931Sfjoe		    bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
64982893Salfred			if (rt->rt_expire) {
65082893Salfred			    if (log_arp_movements)
65184931Sfjoe			        log(LOG_INFO, "arp: %s moved from %*D to %*D on %s%d\n",
65284931Sfjoe				    inet_ntoa(isaddr),
65384931Sfjoe				    ifp->if_addrlen, (u_char *)LLADDR(sdl), ":",
65484931Sfjoe				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
65584931Sfjoe				    ifp->if_name, ifp->if_unit);
65682893Salfred			} else {
65739389Sfenner			    log(LOG_ERR,
65884931Sfjoe				"arp: %*D attempts to modify permanent entry for %s on %s%d\n",
65984931Sfjoe				ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
66084931Sfjoe				inet_ntoa(isaddr), ifp->if_name, ifp->if_unit);
66139389Sfenner			    goto reply;
66239389Sfenner			}
66346568Speter		}
66484931Sfjoe		/*
66584931Sfjoe		 * sanity check for the address length.
66684931Sfjoe		 * XXX this does not work for protocols with variable address
66784931Sfjoe		 * length. -is
66884931Sfjoe		 */
66984931Sfjoe		if (sdl->sdl_alen &&
67084931Sfjoe		    sdl->sdl_alen != ah->ar_hln) {
67184931Sfjoe			log(LOG_WARNING,
67284931Sfjoe			    "arp from %*D: new addr len %d, was %d",
67384931Sfjoe			    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
67484931Sfjoe			    ah->ar_hln, sdl->sdl_alen);
67584931Sfjoe		}
67684931Sfjoe		if (ifp->if_addrlen != ah->ar_hln) {
67784931Sfjoe			log(LOG_WARNING,
67884931Sfjoe			    "arp from %*D: addr len: new %d, i/f %d (ignored)",
67984931Sfjoe			    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
68084931Sfjoe			    ah->ar_hln, ifp->if_addrlen);
68184931Sfjoe			goto reply;
68284931Sfjoe		}
68384931Sfjoe		(void)memcpy(LLADDR(sdl), ar_sha(ah),
68484931Sfjoe		    sdl->sdl_alen = ah->ar_hln);
68558313Slile                sdl->sdl_rcf = (u_short)0;
68645705Seivind		/*
68745705Seivind		 * If we receive an arp from a token-ring station over
68845705Seivind		 * a token-ring nic then try to save the source
68945705Seivind		 * routing info.
69045705Seivind		 */
69184931Sfjoe		if (ifp->if_type == IFT_ISO88025) {
69244627Sjulian			th = (struct iso88025_header *)m->m_pkthdr.header;
69358313Slile			rif_len = TR_RCF_RIFLEN(th->rcf);
69458313Slile			if ((th->iso88025_shost[0] & TR_RII) &&
69558313Slile			    (rif_len > 2)) {
69658313Slile				sdl->sdl_rcf = th->rcf;
69758313Slile				sdl->sdl_rcf ^= htons(TR_RCF_DIR);
69858313Slile				memcpy(sdl->sdl_route, th->rd, rif_len - 2);
69958313Slile				sdl->sdl_rcf &= ~htons(TR_RCF_BCST_MASK);
70051320Slile				/*
70151320Slile				 * Set up source routing information for
70251320Slile				 * reply packet (XXX)
70351320Slile				 */
70458313Slile				m->m_data -= rif_len;
70558313Slile				m->m_len  += rif_len;
70658313Slile				m->m_pkthdr.len += rif_len;
70744627Sjulian			} else {
70858313Slile				th->iso88025_shost[0] &= ~TR_RII;
70944627Sjulian			}
71050512Slile			m->m_data -= 8;
71150512Slile			m->m_len  += 8;
71250512Slile			m->m_pkthdr.len += 8;
71344627Sjulian			th->rcf = sdl->sdl_rcf;
71445705Seivind		} else {
71558313Slile			sdl->sdl_rcf = (u_short)0;
71644627Sjulian		}
7171541Srgrimes		if (rt->rt_expire)
71834961Sphk			rt->rt_expire = time_second + arpt_keep;
7191541Srgrimes		rt->rt_flags &= ~RTF_REJECT;
7201541Srgrimes		la->la_asked = 0;
7211541Srgrimes		if (la->la_hold) {
72284931Sfjoe			(*ifp->if_output)(ifp, la->la_hold,
7231541Srgrimes				rt_key(rt), rt);
7241541Srgrimes			la->la_hold = 0;
7251541Srgrimes		}
7261541Srgrimes	}
7271541Srgrimesreply:
7281541Srgrimes	if (op != ARPOP_REQUEST) {
7291541Srgrimes		m_freem(m);
7301541Srgrimes		return;
7311541Srgrimes	}
7321541Srgrimes	if (itaddr.s_addr == myaddr.s_addr) {
7331541Srgrimes		/* I am the target */
73484931Sfjoe		(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
73584931Sfjoe		(void)memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln);
7361541Srgrimes	} else {
7371541Srgrimes		la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
7383282Swollman		if (la == NULL) {
7393282Swollman			struct sockaddr_in sin;
7403282Swollman
74112693Sphk			if (!arp_proxyall) {
74212693Sphk				m_freem(m);
74312693Sphk				return;
74412693Sphk			}
7453282Swollman
7463282Swollman			bzero(&sin, sizeof sin);
7473282Swollman			sin.sin_family = AF_INET;
7483282Swollman			sin.sin_len = sizeof sin;
7493282Swollman			sin.sin_addr = itaddr;
7503282Swollman
7515101Swollman			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
75212693Sphk			if (!rt) {
75312693Sphk				m_freem(m);
75412693Sphk				return;
75512693Sphk			}
7563282Swollman			/*
7573282Swollman			 * Don't send proxies for nodes on the same interface
7583282Swollman			 * as this one came out of, or we'll get into a fight
7593282Swollman			 * over who claims what Ether address.
7603282Swollman			 */
76184931Sfjoe			if (rt->rt_ifp == ifp) {
7623282Swollman				rtfree(rt);
76312693Sphk				m_freem(m);
76412693Sphk				return;
7653282Swollman			}
76684931Sfjoe			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
76784931Sfjoe			(void)memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln);
7683282Swollman			rtfree(rt);
76963080Sdwmalone
77063080Sdwmalone			/*
77163080Sdwmalone			 * Also check that the node which sent the ARP packet
77263080Sdwmalone			 * is on the the interface we expect it to be on. This
77363080Sdwmalone			 * avoids ARP chaos if an interface is connected to the
77463080Sdwmalone			 * wrong network.
77563080Sdwmalone			 */
77663080Sdwmalone			sin.sin_addr = isaddr;
77763080Sdwmalone
77863080Sdwmalone			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
77963080Sdwmalone			if (!rt) {
78063080Sdwmalone				m_freem(m);
78163080Sdwmalone				return;
78263080Sdwmalone			}
78384931Sfjoe			if (rt->rt_ifp != ifp) {
78463080Sdwmalone				log(LOG_INFO, "arp_proxy: ignoring request"
78563080Sdwmalone				    " from %s via %s%d, expecting %s%d\n",
78684931Sfjoe				    inet_ntoa(isaddr), ifp->if_name,
78784931Sfjoe				    ifp->if_unit, rt->rt_ifp->if_name,
78863080Sdwmalone				    rt->rt_ifp->if_unit);
78963080Sdwmalone				rtfree(rt);
79063080Sdwmalone				m_freem(m);
79163080Sdwmalone				return;
79263080Sdwmalone			}
79363080Sdwmalone			rtfree(rt);
79463080Sdwmalone
7954069Swollman#ifdef DEBUG_PROXY
7968876Srgrimes			printf("arp: proxying for %s\n",
7977088Swollman			       inet_ntoa(itaddr));
7984069Swollman#endif
7993282Swollman		} else {
8003282Swollman			rt = la->la_rt;
80184931Sfjoe			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
8023282Swollman			sdl = SDL(rt->rt_gateway);
80384931Sfjoe			(void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
8043282Swollman		}
8051541Srgrimes	}
8061541Srgrimes
80784931Sfjoe	(void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
80884931Sfjoe	(void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
80984931Sfjoe	ah->ar_op = htons(ARPOP_REPLY);
81084931Sfjoe	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
81184931Sfjoe	switch (ifp->if_type) {
81284931Sfjoe	case IFT_ARCNET:
81384931Sfjoe		arh = (struct arc_header *)sa.sa_data;
81484931Sfjoe		arh->arc_dhost = *ar_tha(ah);
81584931Sfjoe		arh->arc_type = ARCTYPE_ARP;
81684931Sfjoe		break;
81784931Sfjoe
81845705Seivind	case IFT_ISO88025:
81944627Sjulian		/* Re-arrange the source/dest address */
82051320Slile		memcpy(th->iso88025_dhost, th->iso88025_shost,
82151320Slile		    sizeof(th->iso88025_dhost));
82284931Sfjoe		memcpy(th->iso88025_shost, IF_LLADDR(ifp),
82351320Slile		    sizeof(th->iso88025_shost));
82444627Sjulian		/* Set the source routing bit if neccesary */
82558313Slile		if (th->iso88025_dhost[0] & TR_RII) {
82658313Slile			th->iso88025_dhost[0] &= ~TR_RII;
82758313Slile			if (TR_RCF_RIFLEN(th->rcf) > 2)
82858313Slile				th->iso88025_shost[0] |= TR_RII;
82944627Sjulian		}
83044627Sjulian		/* Copy the addresses, ac and fc into sa_data */
83151320Slile		memcpy(sa.sa_data, th->iso88025_dhost,
83251320Slile		    sizeof(th->iso88025_dhost) * 2);
83358313Slile		sa.sa_data[(sizeof(th->iso88025_dhost) * 2)] = TR_AC;
83458313Slile		sa.sa_data[(sizeof(th->iso88025_dhost) * 2) + 1] = TR_LLC_FRAME;
83544627Sjulian		break;
83645705Seivind	case IFT_ETHER:
83751320Slile	case IFT_FDDI:
83851320Slile	/*
83951320Slile	 * May not be correct for types not explictly
84051320Slile	 * listed, but it is our best guess.
84151320Slile	 */
84251320Slile	default:
84344627Sjulian		eh = (struct ether_header *)sa.sa_data;
84484931Sfjoe		(void)memcpy(eh->ether_dhost, ar_tha(ah),
84551320Slile		    sizeof(eh->ether_dhost));
84644627Sjulian		eh->ether_type = htons(ETHERTYPE_ARP);
84744627Sjulian		break;
84844627Sjulian	}
8491541Srgrimes	sa.sa_family = AF_UNSPEC;
8501541Srgrimes	sa.sa_len = sizeof(sa);
85184931Sfjoe	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
8521541Srgrimes	return;
8531541Srgrimes}
85432350Seivind#endif
8551541Srgrimes
8561541Srgrimes/*
8571541Srgrimes * Free an arp entry.
8581541Srgrimes */
8591541Srgrimesstatic void
8601541Srgrimesarptfree(la)
8611541Srgrimes	register struct llinfo_arp *la;
8621541Srgrimes{
8631541Srgrimes	register struct rtentry *rt = la->la_rt;
8641541Srgrimes	register struct sockaddr_dl *sdl;
8651541Srgrimes	if (rt == 0)
8661541Srgrimes		panic("arptfree");
8671541Srgrimes	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
8681541Srgrimes	    sdl->sdl_family == AF_LINK) {
8691541Srgrimes		sdl->sdl_alen = 0;
8701541Srgrimes		la->la_asked = 0;
8711541Srgrimes		rt->rt_flags &= ~RTF_REJECT;
8721541Srgrimes		return;
8731541Srgrimes	}
8741541Srgrimes	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
8751541Srgrimes			0, (struct rtentry **)0);
8761541Srgrimes}
8771541Srgrimes/*
8781541Srgrimes * Lookup or enter a new address in arptab.
8791541Srgrimes */
8801541Srgrimesstatic struct llinfo_arp *
8811541Srgrimesarplookup(addr, create, proxy)
8821541Srgrimes	u_long addr;
8831541Srgrimes	int create, proxy;
8841541Srgrimes{
8851541Srgrimes	register struct rtentry *rt;
8861541Srgrimes	static struct sockaddr_inarp sin = {sizeof(sin), AF_INET };
8874069Swollman	const char *why = 0;
8881541Srgrimes
8891541Srgrimes	sin.sin_addr.s_addr = addr;
8901541Srgrimes	sin.sin_other = proxy ? SIN_PROXY : 0;
8915101Swollman	rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
8921541Srgrimes	if (rt == 0)
8931541Srgrimes		return (0);
8941541Srgrimes	rt->rt_refcnt--;
8954069Swollman
89612693Sphk	if (rt->rt_flags & RTF_GATEWAY)
8974069Swollman		why = "host is not on local network";
89812693Sphk	else if ((rt->rt_flags & RTF_LLINFO) == 0)
8994069Swollman		why = "could not allocate llinfo";
90012693Sphk	else if (rt->rt_gateway->sa_family != AF_LINK)
9014069Swollman		why = "gateway route is not ours";
9024069Swollman
90312693Sphk	if (why && create) {
9044069Swollman		log(LOG_DEBUG, "arplookup %s failed: %s\n",
9057088Swollman		    inet_ntoa(sin.sin_addr), why);
9064069Swollman		return 0;
90712693Sphk	} else if (why) {
9084069Swollman		return 0;
9091541Srgrimes	}
9101541Srgrimes	return ((struct llinfo_arp *)rt->rt_llinfo);
9111541Srgrimes}
9121541Srgrimes
9135195Swollmanvoid
91484931Sfjoearp_ifinit(ifp, ifa)
91584931Sfjoe	struct ifnet *ifp;
9165195Swollman	struct ifaddr *ifa;
9175195Swollman{
91825822Stegge	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
91984931Sfjoe		arprequest(ifp, &IA_SIN(ifa)->sin_addr,
92084931Sfjoe				&IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
9215195Swollman	ifa->ifa_rtrequest = arp_rtrequest;
9225195Swollman	ifa->ifa_flags |= RTF_CLONING;
9235195Swollman}
92469152Sjlemon
92569152Sjlemonstatic void
92669152Sjlemonarp_init(void)
92769152Sjlemon{
92869152Sjlemon
92969152Sjlemon	arpintrq.ifq_maxlen = 50;
93069152Sjlemon	mtx_init(&arpintrq.ifq_mtx, "arp_inq", MTX_DEF);
93169152Sjlemon}
93269152Sjlemon
93369152SjlemonSYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);
934