if_ether.c revision 138615
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 * 4. Neither the name of the University nor the names of its contributors
141541Srgrimes *    may be used to endorse or promote products derived from this software
151541Srgrimes *    without specific prior written permission.
161541Srgrimes *
171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271541Srgrimes * SUCH DAMAGE.
281541Srgrimes *
291541Srgrimes *	@(#)if_ether.c	8.1 (Berkeley) 6/10/93
3050477Speter * $FreeBSD: head/sys/netinet/if_ether.c 138615 2004-12-09 00:12:41Z mlaier $
311541Srgrimes */
321541Srgrimes
331541Srgrimes/*
341541Srgrimes * Ethernet address resolution protocol.
351541Srgrimes * TODO:
361541Srgrimes *	add "inuse/lock" bit (or ref. count) along with valid bit
371541Srgrimes */
381541Srgrimes
3932350Seivind#include "opt_inet.h"
4041793Sluigi#include "opt_bdg.h"
41101090Srwatson#include "opt_mac.h"
4232350Seivind
431541Srgrimes#include <sys/param.h>
4412693Sphk#include <sys/kernel.h>
4544078Sdfr#include <sys/queue.h>
4612693Sphk#include <sys/sysctl.h>
471541Srgrimes#include <sys/systm.h>
48101090Srwatson#include <sys/mac.h>
4912693Sphk#include <sys/mbuf.h>
501541Srgrimes#include <sys/malloc.h>
5118892Sbde#include <sys/socket.h>
521541Srgrimes#include <sys/syslog.h>
531541Srgrimes
541541Srgrimes#include <net/if.h>
551541Srgrimes#include <net/if_dl.h>
5644165Sjulian#include <net/if_types.h>
571541Srgrimes#include <net/route.h>
588426Swollman#include <net/netisr.h>
5958313Slile#include <net/if_llc.h>
6071963Sjulian#include <net/ethernet.h>
6171963Sjulian#include <net/bridge.h>
621541Srgrimes
631541Srgrimes#include <netinet/in.h>
641541Srgrimes#include <netinet/in_var.h>
651541Srgrimes#include <netinet/if_ether.h>
661541Srgrimes
6784931Sfjoe#include <net/if_arc.h>
6844627Sjulian#include <net/iso88025.h>
6944627Sjulian
701541Srgrimes#define SIN(s) ((struct sockaddr_in *)s)
711541Srgrimes#define SDL(s) ((struct sockaddr_dl *)s)
721541Srgrimes
7344078SdfrSYSCTL_DECL(_net_link_ether);
7412942SwollmanSYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
751541Srgrimes
7612693Sphk/* timer values */
7712942Swollmanstatic int arpt_prune = (5*60*1); /* walk list every 5 minutes */
7812942Swollmanstatic int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
7912942Swollmanstatic int arpt_down = 20;	/* once declared down, don't send for 20 sec */
801541Srgrimes
8112942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
8212942Swollman	   &arpt_prune, 0, "");
8312942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
8412942Swollman	   &arpt_keep, 0, "");
8512942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
8612942Swollman	   &arpt_down, 0, "");
8712693Sphk
881541Srgrimes#define	rt_expire rt_rmx.rmx_expire
891541Srgrimes
9011225Swollmanstruct llinfo_arp {
9160938Sjake	LIST_ENTRY(llinfo_arp) la_le;
9211225Swollman	struct	rtentry *la_rt;
93110308Sorion	struct	mbuf *la_hold;	/* last packet until resolved/timeout */
94110544Sorion	u_short	la_preempt;	/* countdown for pre-expiry arps */
95110308Sorion	u_short	la_asked;	/* #times we QUERIED following expiration */
9611225Swollman#define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */
9711225Swollman};
9812693Sphk
9960938Sjakestatic	LIST_HEAD(, llinfo_arp) llinfo_arp;
10011225Swollman
101111888Sjlemonstatic struct	ifqueue arpintrq;
102120727Ssamstatic int	arp_allocated;
1031541Srgrimes
10412693Sphkstatic int	arp_maxtries = 5;
10512942Swollmanstatic int	useloopback = 1; /* use loopback interface for local traffic */
10612942Swollmanstatic int	arp_proxyall = 0;
107120727Ssamstatic struct callout arp_callout;
1083282Swollman
10912942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
11012942Swollman	   &arp_maxtries, 0, "");
11112942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
11212942Swollman	   &useloopback, 0, "");
11312942SwollmanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
11412942Swollman	   &arp_proxyall, 0, "");
11512693Sphk
11692723Salfredstatic void	arp_init(void);
11792723Salfredstatic void	arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
11892723Salfredstatic void	arprequest(struct ifnet *,
11992723Salfred			struct in_addr *, struct in_addr *, u_char *);
120111888Sjlemonstatic void	arpintr(struct mbuf *);
12192723Salfredstatic void	arptfree(struct llinfo_arp *);
12292723Salfredstatic void	arptimer(void *);
12312693Sphkstatic struct llinfo_arp
12492723Salfred		*arplookup(u_long, int, int);
12532350Seivind#ifdef INET
12692723Salfredstatic void	in_arpinput(struct mbuf *);
12732350Seivind#endif
12812693Sphk
1291541Srgrimes/*
1301541Srgrimes * Timeout routine.  Age arp_tab entries periodically.
1311541Srgrimes */
1321541Srgrimes/* ARGSUSED */
1331541Srgrimesstatic void
1341541Srgrimesarptimer(ignored_arg)
1351541Srgrimes	void *ignored_arg;
1361541Srgrimes{
137109409Shsu	struct llinfo_arp *la, *ola;
1381541Srgrimes
139109996Shsu	RADIX_NODE_HEAD_LOCK(rt_tables[AF_INET]);
140109409Shsu	la = LIST_FIRST(&llinfo_arp);
141109409Shsu	while (la != NULL) {
142109409Shsu		struct rtentry *rt = la->la_rt;
143109409Shsu		ola = la;
14471999Sphk		la = LIST_NEXT(la, la_le);
14534961Sphk		if (rt->rt_expire && rt->rt_expire <= time_second)
146109409Shsu			arptfree(ola);		/* timer has expired, clear */
1471541Srgrimes	}
148109996Shsu	RADIX_NODE_HEAD_UNLOCK(rt_tables[AF_INET]);
149120727Ssam
150120727Ssam	callout_reset(&arp_callout, arpt_prune * hz, arptimer, NULL);
1511541Srgrimes}
1521541Srgrimes
1531541Srgrimes/*
1541541Srgrimes * Parallel to llc_rtrequest.
1551541Srgrimes */
1565196Swollmanstatic void
15785074Sruarp_rtrequest(req, rt, info)
1581541Srgrimes	int req;
159126936Smdodd	struct rtentry *rt;
16085074Sru	struct rt_addrinfo *info;
1611541Srgrimes{
162126936Smdodd	struct sockaddr *gate;
163126936Smdodd	struct llinfo_arp *la;
1641541Srgrimes	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
165138615Smlaier	struct in_ifaddr *ia;
166138615Smlaier	struct ifaddr *ifa;
1671541Srgrimes
168120727Ssam	RT_LOCK_ASSERT(rt);
169120727Ssam
1701541Srgrimes	if (rt->rt_flags & RTF_GATEWAY)
1711541Srgrimes		return;
172120727Ssam	gate = rt->rt_gateway;
173120727Ssam	la = (struct llinfo_arp *)rt->rt_llinfo;
1741541Srgrimes	switch (req) {
1751541Srgrimes
1761541Srgrimes	case RTM_ADD:
1771541Srgrimes		/*
1781541Srgrimes		 * XXX: If this is a manually added route to interface
1791541Srgrimes		 * such as older version of routed or gated might provide,
1801541Srgrimes		 * restore cloning bit.
1811541Srgrimes		 */
1821541Srgrimes		if ((rt->rt_flags & RTF_HOST) == 0 &&
183136960Sbms		    rt_mask(rt) != NULL &&
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)) {
208120727Ssam			log(LOG_DEBUG, "%s: bad gateway %s%s\n", __func__,
209120698Sbms			    inet_ntoa(SIN(rt_key(rt))->sin_addr),
210120698Sbms			    (gate->sa_family != AF_LINK) ?
211120699Sbms			    " (!AF_LINK)": "");
2121541Srgrimes			break;
2131541Srgrimes		}
2141541Srgrimes		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
2151541Srgrimes		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
2161541Srgrimes		if (la != 0)
2171541Srgrimes			break; /* This happens on a route change */
2181541Srgrimes		/*
2191541Srgrimes		 * Case 2:  This route may come from cloning, or a manual route
2201541Srgrimes		 * add with a LL address.
2211541Srgrimes		 */
222120727Ssam		R_Zalloc(la, struct llinfo_arp *, sizeof(*la));
2231541Srgrimes		rt->rt_llinfo = (caddr_t)la;
2241541Srgrimes		if (la == 0) {
225120727Ssam			log(LOG_DEBUG, "%s: malloc failed\n", __func__);
2261541Srgrimes			break;
2271541Srgrimes		}
228120727Ssam		arp_allocated++;
2291541Srgrimes		la->la_rt = rt;
2301541Srgrimes		rt->rt_flags |= RTF_LLINFO;
231109996Shsu		RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]);
23211225Swollman		LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
23313926Swollman
23432350Seivind#ifdef INET
23513926Swollman		/*
23613926Swollman		 * This keeps the multicast addresses from showing up
23713926Swollman		 * in `arp -a' listings as unresolved.  It's not actually
23813926Swollman		 * functional.  Then the same for broadcast.
23913926Swollman		 */
24087776Sjlemon		if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) &&
24187776Sjlemon		    rt->rt_ifp->if_type != IFT_ARCNET) {
24213926Swollman			ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
24313926Swollman					       LLADDR(SDL(gate)));
24413926Swollman			SDL(gate)->sdl_alen = 6;
24516576Speter			rt->rt_expire = 0;
24613926Swollman		}
24713926Swollman		if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
24884931Sfjoe			memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
24984931Sfjoe			       rt->rt_ifp->if_addrlen);
25084931Sfjoe			SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
25116576Speter			rt->rt_expire = 0;
25213926Swollman		}
25332350Seivind#endif
25413926Swollman
255138615Smlaier		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
256138615Smlaier			if (ia->ia_ifp == rt->rt_ifp &&
257138615Smlaier			    SIN(rt_key(rt))->sin_addr.s_addr ==
258138615Smlaier			    (IA_SIN(ia))->sin_addr.s_addr)
259138615Smlaier				break;
260138615Smlaier		}
261138615Smlaier		if (ia) {
2621541Srgrimes		    /*
2631541Srgrimes		     * This test used to be
2641541Srgrimes		     *	if (loif.if_flags & IFF_UP)
2651541Srgrimes		     * It allowed local traffic to be forced
2661541Srgrimes		     * through the hardware by configuring the loopback down.
2671541Srgrimes		     * However, it causes problems during network configuration
2681541Srgrimes		     * for boards that can't receive packets they send.
2691541Srgrimes		     * It is now necessary to clear "useloopback" and remove
2701541Srgrimes		     * the route to force traffic out to the hardware.
2711541Srgrimes		     */
2721541Srgrimes			rt->rt_expire = 0;
273128398Sluigi			bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
27484931Sfjoe			      SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
2751541Srgrimes			if (useloopback)
2768090Spst				rt->rt_ifp = loif;
2771541Srgrimes
278138615Smlaier		    /*
279138615Smlaier		     * make sure to set rt->rt_ifa to the interface
280138615Smlaier		     * address we are using, otherwise we will have trouble
281138615Smlaier		     * with source address selection.
282138615Smlaier		     */
283138615Smlaier			ifa = &ia->ia_ifa;
284138615Smlaier			if (ifa != rt->rt_ifa) {
285138615Smlaier				IFAFREE(rt->rt_ifa);
286138615Smlaier				IFAREF(ifa);
287138615Smlaier				rt->rt_ifa = ifa;
288138615Smlaier			}
2891541Srgrimes		}
2901541Srgrimes		break;
2911541Srgrimes
2921541Srgrimes	case RTM_DELETE:
2931541Srgrimes		if (la == 0)
2941541Srgrimes			break;
295109996Shsu		RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]);
29611225Swollman		LIST_REMOVE(la, la_le);
2971541Srgrimes		rt->rt_llinfo = 0;
2981541Srgrimes		rt->rt_flags &= ~RTF_LLINFO;
2991541Srgrimes		if (la->la_hold)
3001541Srgrimes			m_freem(la->la_hold);
3011541Srgrimes		Free((caddr_t)la);
3021541Srgrimes	}
3031541Srgrimes}
3041541Srgrimes
3051541Srgrimes/*
3061541Srgrimes * Broadcast an ARP request. Caller specifies:
3071541Srgrimes *	- arp header source ip address
3081541Srgrimes *	- arp header target ip address
3091541Srgrimes *	- arp header source ethernet address
3101541Srgrimes */
3111541Srgrimesstatic void
31284931Sfjoearprequest(ifp, sip, tip, enaddr)
313126936Smdodd	struct ifnet *ifp;
314126936Smdodd	struct in_addr *sip, *tip;
315126936Smdodd	u_char *enaddr;
3161541Srgrimes{
317126936Smdodd	struct mbuf *m;
318126936Smdodd	struct arphdr *ah;
3191541Srgrimes	struct sockaddr sa;
3201541Srgrimes
321111119Simp	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
3221541Srgrimes		return;
323127261Smdodd	m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
324127261Smdodd		2*ifp->if_data.ifi_addrlen;
325127277Smdodd	m->m_pkthdr.len = m->m_len;
326127277Smdodd	MH_ALIGN(m, m->m_len);
327127277Smdodd	ah = mtod(m, struct arphdr *);
328127261Smdodd	bzero((caddr_t)ah, m->m_len);
329101090Srwatson#ifdef MAC
330101090Srwatson	mac_create_mbuf_linklayer(ifp, m);
331101090Srwatson#endif
33284931Sfjoe	ah->ar_pro = htons(ETHERTYPE_IP);
33384931Sfjoe	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
33484931Sfjoe	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
33584931Sfjoe	ah->ar_op = htons(ARPOP_REQUEST);
336127261Smdodd	bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
337127261Smdodd	bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
338127261Smdodd	bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
339127261Smdodd	sa.sa_family = AF_ARP;
340127261Smdodd	sa.sa_len = 2;
341127261Smdodd	m->m_flags |= M_BCAST;
342127261Smdodd	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
34384931Sfjoe
344127261Smdodd	return;
3451541Srgrimes}
3461541Srgrimes
3471541Srgrimes/*
348128636Sluigi * Resolve an IP address into an ethernet address.
349128636Sluigi * On input:
350128636Sluigi *    ifp is the interface we use
351128636Sluigi *    dst is the next hop,
352128636Sluigi *    rt0 is the route to the final destination (possibly useless)
353128636Sluigi *    m is the mbuf
354128636Sluigi *    desten is where we want the address.
355128636Sluigi *
356128636Sluigi * On success, desten is filled in and the function returns 0;
357128636Sluigi * If the packet must be held pending resolution, we return EWOULDBLOCK
358128636Sluigi * On other errors, we return the corresponding error code.
3591541Srgrimes */
3601541Srgrimesint
361128636Sluigiarpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
362127828Sluigi	struct sockaddr *dst, u_char *desten)
3631541Srgrimes{
36478295Sjlemon	struct llinfo_arp *la = 0;
3651541Srgrimes	struct sockaddr_dl *sdl;
366128636Sluigi	int error;
367128636Sluigi	struct rtentry *rt;
3681541Srgrimes
369128636Sluigi	error = rt_check(&rt, &rt0, dst);
370128636Sluigi	if (error) {
371128636Sluigi		m_freem(m);
372128636Sluigi		return error;
373128636Sluigi	}
374128636Sluigi
3751541Srgrimes	if (m->m_flags & M_BCAST) {	/* broadcast */
37684931Sfjoe		(void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
377128636Sluigi		return (0);
3781541Srgrimes	}
37984931Sfjoe	if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */
3801541Srgrimes		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
381128636Sluigi		return (0);
3821541Srgrimes	}
3831541Srgrimes	if (rt)
3841541Srgrimes		la = (struct llinfo_arp *)rt->rt_llinfo;
38542775Sfenner	if (la == 0) {
3863311Sphk		la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
3873311Sphk		if (la)
3881541Srgrimes			rt = la->la_rt;
3891541Srgrimes	}
3901541Srgrimes	if (la == 0 || rt == 0) {
39136308Sphk		log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
39236308Sphk			inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "",
39336308Sphk				rt ? "rt" : "");
3941541Srgrimes		m_freem(m);
395128636Sluigi		return (EINVAL); /* XXX */
3961541Srgrimes	}
3971541Srgrimes	sdl = SDL(rt->rt_gateway);
3981541Srgrimes	/*
3991541Srgrimes	 * Check the address family and length is valid, the address
4001541Srgrimes	 * is resolved; otherwise, try to resolve.
4011541Srgrimes	 */
40234961Sphk	if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
4031541Srgrimes	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
40492802Sorion		/*
40592802Sorion		 * If entry has an expiry time and it is approaching,
40692802Sorion		 * see if we need to send an ARP request within this
40792802Sorion		 * arpt_down interval.
40892802Sorion		 */
40992802Sorion		if ((rt->rt_expire != 0) &&
410110544Sorion		    (time_second + la->la_preempt > rt->rt_expire)) {
41192802Sorion			arprequest(ifp,
41292802Sorion				   &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
41392802Sorion				   &SIN(dst)->sin_addr,
41492802Sorion				   IF_LLADDR(ifp));
415110544Sorion			la->la_preempt--;
41692802Sorion		}
41792802Sorion
41816206Sbde		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
419128636Sluigi		return (0);
4201541Srgrimes	}
4211541Srgrimes	/*
422120626Sru	 * If ARP is disabled or static on this interface, stop.
42378295Sjlemon	 * XXX
42478295Sjlemon	 * Probably should not allocate empty llinfo struct if we are
42578295Sjlemon	 * not going to be sending out an arp request.
42678295Sjlemon	 */
427120626Sru	if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
42887410Sru		m_freem(m);
429128636Sluigi		return (EINVAL);
43087410Sru	}
43178295Sjlemon	/*
4321541Srgrimes	 * There is an arptab entry, but no ethernet address
4331541Srgrimes	 * response yet.  Replace the held mbuf with this
4341541Srgrimes	 * latest one.
4351541Srgrimes	 */
4361541Srgrimes	if (la->la_hold)
4371541Srgrimes		m_freem(la->la_hold);
4381541Srgrimes	la->la_hold = m;
4391541Srgrimes	if (rt->rt_expire) {
440120727Ssam		RT_LOCK(rt);
4411541Srgrimes		rt->rt_flags &= ~RTF_REJECT;
44234961Sphk		if (la->la_asked == 0 || rt->rt_expire != time_second) {
44334961Sphk			rt->rt_expire = time_second;
444110308Sorion			if (la->la_asked++ < arp_maxtries) {
445110308Sorion				arprequest(ifp,
446110308Sorion					   &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
447110308Sorion					   &SIN(dst)->sin_addr,
448110308Sorion					   IF_LLADDR(ifp));
449110308Sorion			} else {
4501541Srgrimes				rt->rt_flags |= RTF_REJECT;
4511541Srgrimes				rt->rt_expire += arpt_down;
452110544Sorion				la->la_asked = 0;
453110544Sorion				la->la_preempt = arp_maxtries;
4541541Srgrimes			}
4551541Srgrimes
4561541Srgrimes		}
457120727Ssam		RT_UNLOCK(rt);
4581541Srgrimes	}
459128636Sluigi	return (EWOULDBLOCK);
4601541Srgrimes}
4611541Srgrimes
4621541Srgrimes/*
4631541Srgrimes * Common length and type checks are done here,
4641541Srgrimes * then the protocol-specific routine is called.
4651541Srgrimes */
46612693Sphkstatic void
467111888Sjlemonarpintr(struct mbuf *m)
4681541Srgrimes{
469111888Sjlemon	struct arphdr *ar;
4701541Srgrimes
471111888Sjlemon	if (m->m_len < sizeof(struct arphdr) &&
472111888Sjlemon	    ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
473111888Sjlemon		log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
474111888Sjlemon		return;
475111888Sjlemon	}
476111888Sjlemon	ar = mtod(m, struct arphdr *);
4771541Srgrimes
478111888Sjlemon	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER &&
479111888Sjlemon	    ntohs(ar->ar_hrd) != ARPHRD_IEEE802 &&
480130407Sdfr	    ntohs(ar->ar_hrd) != ARPHRD_ARCNET &&
481130407Sdfr	    ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) {
482111888Sjlemon		log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
483111888Sjlemon		    (unsigned char *)&ar->ar_hrd, "");
484111888Sjlemon		m_freem(m);
485111888Sjlemon		return;
486111888Sjlemon	}
4871541Srgrimes
488123768Sru	if (m->m_len < arphdr_len(ar)) {
489123765Sru		if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
490123765Sru			log(LOG_ERR, "arp: runt packet\n");
491123765Sru			m_freem(m);
492123765Sru			return;
493123765Sru		}
494123765Sru		ar = mtod(m, struct arphdr *);
495111888Sjlemon	}
49657900Srwatson
497111888Sjlemon	switch (ntohs(ar->ar_pro)) {
49832350Seivind#ifdef INET
499111888Sjlemon	case ETHERTYPE_IP:
500111888Sjlemon		in_arpinput(m);
501111888Sjlemon		return;
50232350Seivind#endif
5031541Srgrimes	}
504111888Sjlemon	m_freem(m);
5051541Srgrimes}
5061541Srgrimes
50732350Seivind#ifdef INET
5081541Srgrimes/*
5091541Srgrimes * ARP for Internet protocols on 10 Mb/s Ethernet.
5101541Srgrimes * Algorithm is that given in RFC 826.
5111541Srgrimes * In addition, a sanity check is performed on the sender
5121541Srgrimes * protocol address, to catch impersonators.
5131541Srgrimes * We no longer handle negotiations for use of trailer protocol:
5141541Srgrimes * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
5151541Srgrimes * along with IP replies if we wanted trailers sent to us,
5161541Srgrimes * and also sent them in response to IP replies.
5171541Srgrimes * This allowed either end to announce the desire to receive
5181541Srgrimes * trailer packets.
5191541Srgrimes * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
5201541Srgrimes * but formerly didn't normally send requests.
5211541Srgrimes */
52270699Salfredstatic int log_arp_wrong_iface = 1;
52382893Salfredstatic int log_arp_movements = 1;
52470699Salfred
52570699SalfredSYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
52670699Salfred	&log_arp_wrong_iface, 0,
52770699Salfred	"log arp packets arriving on the wrong interface");
52882893SalfredSYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
52982893Salfred        &log_arp_movements, 0,
53082966Salfred        "log arp replies from MACs different than the one in the cache");
53170699Salfred
53282893Salfred
5331541Srgrimesstatic void
5341541Srgrimesin_arpinput(m)
5351541Srgrimes	struct mbuf *m;
5361541Srgrimes{
537126936Smdodd	struct arphdr *ah;
538126936Smdodd	struct ifnet *ifp = m->m_pkthdr.rcvif;
53944627Sjulian	struct iso88025_header *th = (struct iso88025_header *)0;
54096184Skbyanc	struct iso88025_sockaddr_dl_data *trld;
541126936Smdodd	struct llinfo_arp *la = 0;
542126936Smdodd	struct rtentry *rt;
54384102Sjlemon	struct ifaddr *ifa;
54484102Sjlemon	struct in_ifaddr *ia;
5451541Srgrimes	struct sockaddr_dl *sdl;
5461541Srgrimes	struct sockaddr sa;
5471541Srgrimes	struct in_addr isaddr, itaddr, myaddr;
54858313Slile	int op, rif_len;
54984931Sfjoe	int req_len;
5501541Srgrimes
55184931Sfjoe	req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
55284931Sfjoe	if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
55374851Syar		log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
55474851Syar		return;
55574851Syar	}
55674851Syar
55784931Sfjoe	ah = mtod(m, struct arphdr *);
55884931Sfjoe	op = ntohs(ah->ar_op);
55984931Sfjoe	(void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
56084931Sfjoe	(void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
561134991Sglebius
56284102Sjlemon	/*
56384102Sjlemon	 * For a bridge, we want to check the address irrespective
56484102Sjlemon	 * of the receive interface. (This will change slightly
56584102Sjlemon	 * when we have clusters of interfaces).
56684102Sjlemon	 */
56784102Sjlemon	LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash)
568134991Sglebius		if ((do_bridge || (ia->ia_ifp == ifp)) &&
56984102Sjlemon		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
57084102Sjlemon			goto match;
57184102Sjlemon	LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
572134991Sglebius		if ((do_bridge || (ia->ia_ifp == ifp)) &&
57384102Sjlemon		    isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
57484102Sjlemon			goto match;
57584102Sjlemon	/*
57685223Sjlemon	 * No match, use the first inet address on the receive interface
57784102Sjlemon	 * as a dummy address for the rest of the function.
57884102Sjlemon	 */
57985223Sjlemon	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
58085466Sjlemon		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
58185466Sjlemon			ia = ifatoia(ifa);
58285466Sjlemon			goto match;
58385466Sjlemon		}
58485466Sjlemon	/*
58585466Sjlemon	 * If bridging, fall back to using any inet address.
58685466Sjlemon	 */
587134991Sglebius	if (!do_bridge || (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL)
588128645Sluigi		goto drop;
58984102Sjlemonmatch:
59084102Sjlemon	myaddr = ia->ia_addr.sin_addr;
591128645Sluigi	if (!bcmp(ar_sha(ah), IF_LLADDR(ifp), ifp->if_addrlen))
592128645Sluigi		goto drop;	/* it's from me, ignore it. */
59384931Sfjoe	if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
5941541Srgrimes		log(LOG_ERR,
59584931Sfjoe		    "arp: link address is broadcast for IP address %s!\n",
5967088Swollman		    inet_ntoa(isaddr));
597128645Sluigi		goto drop;
5981541Srgrimes	}
599136441Srwatson	/*
600136441Srwatson	 * Warn if another host is using the same IP address, but only if the
601136441Srwatson	 * IP address isn't 0.0.0.0, which is used for DHCP only, in which
602136441Srwatson	 * case we suppress the warning to avoid false positive complaints of
603136441Srwatson	 * potential misconfiguration.
604136441Srwatson	 */
605136441Srwatson	if (isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
6061541Srgrimes		log(LOG_ERR,
60784931Sfjoe		   "arp: %*D is using my IP address %s!\n",
60884931Sfjoe		   ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
60984931Sfjoe		   inet_ntoa(isaddr));
6101541Srgrimes		itaddr = myaddr;
6111541Srgrimes		goto reply;
6121541Srgrimes	}
613120626Sru	if (ifp->if_flags & IFF_STATICARP)
614120626Sru		goto reply;
6151541Srgrimes	la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
6161541Srgrimes	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
61772270Sluigi		/* the following is not an error when doing bridging */
618134991Sglebius		if (!do_bridge && rt->rt_ifp != ifp) {
61972270Sluigi			if (log_arp_wrong_iface)
620121816Sbrooks				log(LOG_ERR, "arp: %s is on %s but got reply from %*D on %s\n",
62171963Sjulian				    inet_ntoa(isaddr),
622121816Sbrooks				    rt->rt_ifp->if_xname,
62384931Sfjoe				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
624121816Sbrooks				    ifp->if_xname);
62572270Sluigi			goto reply;
62639389Sfenner		}
6271541Srgrimes		if (sdl->sdl_alen &&
62884931Sfjoe		    bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
62982893Salfred			if (rt->rt_expire) {
63082893Salfred			    if (log_arp_movements)
631121816Sbrooks			        log(LOG_INFO, "arp: %s moved from %*D to %*D on %s\n",
63284931Sfjoe				    inet_ntoa(isaddr),
63384931Sfjoe				    ifp->if_addrlen, (u_char *)LLADDR(sdl), ":",
63484931Sfjoe				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
635121816Sbrooks				    ifp->if_xname);
63682893Salfred			} else {
63739389Sfenner			    log(LOG_ERR,
638121816Sbrooks				"arp: %*D attempts to modify permanent entry for %s on %s\n",
63984931Sfjoe				ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
640121816Sbrooks				inet_ntoa(isaddr), ifp->if_xname);
64139389Sfenner			    goto reply;
64239389Sfenner			}
64346568Speter		}
64484931Sfjoe		/*
64584931Sfjoe		 * sanity check for the address length.
64684931Sfjoe		 * XXX this does not work for protocols with variable address
64784931Sfjoe		 * length. -is
64884931Sfjoe		 */
64984931Sfjoe		if (sdl->sdl_alen &&
65084931Sfjoe		    sdl->sdl_alen != ah->ar_hln) {
65184931Sfjoe			log(LOG_WARNING,
65284931Sfjoe			    "arp from %*D: new addr len %d, was %d",
65384931Sfjoe			    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
65484931Sfjoe			    ah->ar_hln, sdl->sdl_alen);
65584931Sfjoe		}
65684931Sfjoe		if (ifp->if_addrlen != ah->ar_hln) {
65784931Sfjoe			log(LOG_WARNING,
65884931Sfjoe			    "arp from %*D: addr len: new %d, i/f %d (ignored)",
65984931Sfjoe			    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
66084931Sfjoe			    ah->ar_hln, ifp->if_addrlen);
66184931Sfjoe			goto reply;
66284931Sfjoe		}
66384931Sfjoe		(void)memcpy(LLADDR(sdl), ar_sha(ah),
66484931Sfjoe		    sdl->sdl_alen = ah->ar_hln);
66545705Seivind		/*
66645705Seivind		 * If we receive an arp from a token-ring station over
66745705Seivind		 * a token-ring nic then try to save the source
66845705Seivind		 * routing info.
66945705Seivind		 */
67084931Sfjoe		if (ifp->if_type == IFT_ISO88025) {
67144627Sjulian			th = (struct iso88025_header *)m->m_pkthdr.header;
67296184Skbyanc			trld = SDL_ISO88025(sdl);
67358313Slile			rif_len = TR_RCF_RIFLEN(th->rcf);
67458313Slile			if ((th->iso88025_shost[0] & TR_RII) &&
67558313Slile			    (rif_len > 2)) {
67696184Skbyanc				trld->trld_rcf = th->rcf;
67796184Skbyanc				trld->trld_rcf ^= htons(TR_RCF_DIR);
67896184Skbyanc				memcpy(trld->trld_route, th->rd, rif_len - 2);
67996184Skbyanc				trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
68051320Slile				/*
68151320Slile				 * Set up source routing information for
68251320Slile				 * reply packet (XXX)
68351320Slile				 */
68458313Slile				m->m_data -= rif_len;
68558313Slile				m->m_len  += rif_len;
68658313Slile				m->m_pkthdr.len += rif_len;
68744627Sjulian			} else {
68858313Slile				th->iso88025_shost[0] &= ~TR_RII;
68996624Skbyanc				trld->trld_rcf = 0;
69044627Sjulian			}
69150512Slile			m->m_data -= 8;
69250512Slile			m->m_len  += 8;
69350512Slile			m->m_pkthdr.len += 8;
69496184Skbyanc			th->rcf = trld->trld_rcf;
69544627Sjulian		}
696120727Ssam		RT_LOCK(rt);
6971541Srgrimes		if (rt->rt_expire)
69834961Sphk			rt->rt_expire = time_second + arpt_keep;
6991541Srgrimes		rt->rt_flags &= ~RTF_REJECT;
700120727Ssam		RT_UNLOCK(rt);
701110544Sorion		la->la_asked = 0;
702110544Sorion		la->la_preempt = arp_maxtries;
7031541Srgrimes		if (la->la_hold) {
704127277Smdodd			(*ifp->if_output)(ifp, la->la_hold, rt_key(rt), rt);
7051541Srgrimes			la->la_hold = 0;
7061541Srgrimes		}
7071541Srgrimes	}
7081541Srgrimesreply:
709128645Sluigi	if (op != ARPOP_REQUEST)
710128645Sluigi		goto drop;
7111541Srgrimes	if (itaddr.s_addr == myaddr.s_addr) {
7121541Srgrimes		/* I am the target */
71384931Sfjoe		(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
71484931Sfjoe		(void)memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln);
7151541Srgrimes	} else {
7161541Srgrimes		la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
7173282Swollman		if (la == NULL) {
7183282Swollman			struct sockaddr_in sin;
7193282Swollman
720128645Sluigi			if (!arp_proxyall)
721128645Sluigi				goto drop;
7223282Swollman
7233282Swollman			bzero(&sin, sizeof sin);
7243282Swollman			sin.sin_family = AF_INET;
7253282Swollman			sin.sin_len = sizeof sin;
7263282Swollman			sin.sin_addr = itaddr;
7273282Swollman
7285101Swollman			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
729128645Sluigi			if (!rt)
730128645Sluigi				goto drop;
7313282Swollman			/*
7323282Swollman			 * Don't send proxies for nodes on the same interface
7333282Swollman			 * as this one came out of, or we'll get into a fight
7343282Swollman			 * over who claims what Ether address.
7353282Swollman			 */
73684931Sfjoe			if (rt->rt_ifp == ifp) {
7373282Swollman				rtfree(rt);
738128645Sluigi				goto drop;
7393282Swollman			}
74084931Sfjoe			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
74184931Sfjoe			(void)memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln);
7423282Swollman			rtfree(rt);
74363080Sdwmalone
74463080Sdwmalone			/*
74563080Sdwmalone			 * Also check that the node which sent the ARP packet
74663080Sdwmalone			 * is on the the interface we expect it to be on. This
74763080Sdwmalone			 * avoids ARP chaos if an interface is connected to the
74863080Sdwmalone			 * wrong network.
74963080Sdwmalone			 */
75063080Sdwmalone			sin.sin_addr = isaddr;
75163080Sdwmalone
75263080Sdwmalone			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
753128645Sluigi			if (!rt)
754128645Sluigi				goto drop;
75584931Sfjoe			if (rt->rt_ifp != ifp) {
75663080Sdwmalone				log(LOG_INFO, "arp_proxy: ignoring request"
757121816Sbrooks				    " from %s via %s, expecting %s\n",
758121816Sbrooks				    inet_ntoa(isaddr), ifp->if_xname,
759121816Sbrooks				    rt->rt_ifp->if_xname);
76063080Sdwmalone				rtfree(rt);
761128645Sluigi				goto drop;
76263080Sdwmalone			}
76363080Sdwmalone			rtfree(rt);
76463080Sdwmalone
7654069Swollman#ifdef DEBUG_PROXY
7668876Srgrimes			printf("arp: proxying for %s\n",
7677088Swollman			       inet_ntoa(itaddr));
7684069Swollman#endif
7693282Swollman		} else {
7703282Swollman			rt = la->la_rt;
77184931Sfjoe			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
7723282Swollman			sdl = SDL(rt->rt_gateway);
77384931Sfjoe			(void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
7743282Swollman		}
7751541Srgrimes	}
7761541Srgrimes
77784931Sfjoe	(void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
77884931Sfjoe	(void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
77984931Sfjoe	ah->ar_op = htons(ARPOP_REPLY);
78084931Sfjoe	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
781127261Smdodd	m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
782127261Smdodd	m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
783127261Smdodd	m->m_pkthdr.len = m->m_len;
784127261Smdodd	sa.sa_family = AF_ARP;
785127261Smdodd	sa.sa_len = 2;
78684931Sfjoe	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
7871541Srgrimes	return;
788128645Sluigi
789128645Sluigidrop:
790128645Sluigi	m_freem(m);
7911541Srgrimes}
79232350Seivind#endif
7931541Srgrimes
7941541Srgrimes/*
7951541Srgrimes * Free an arp entry.
7961541Srgrimes */
7971541Srgrimesstatic void
7981541Srgrimesarptfree(la)
799126936Smdodd	struct llinfo_arp *la;
8001541Srgrimes{
801126936Smdodd	struct rtentry *rt = la->la_rt;
802126936Smdodd	struct sockaddr_dl *sdl;
803120727Ssam
8041541Srgrimes	if (rt == 0)
8051541Srgrimes		panic("arptfree");
8061541Srgrimes	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
8071541Srgrimes	    sdl->sdl_family == AF_LINK) {
8081541Srgrimes		sdl->sdl_alen = 0;
809110308Sorion		la->la_preempt = la->la_asked = 0;
810120727Ssam		RT_LOCK(rt);		/* XXX needed or move higher? */
8111541Srgrimes		rt->rt_flags &= ~RTF_REJECT;
812120727Ssam		RT_UNLOCK(rt);
8131541Srgrimes		return;
8141541Srgrimes	}
8151541Srgrimes	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
8161541Srgrimes			0, (struct rtentry **)0);
8171541Srgrimes}
8181541Srgrimes/*
8191541Srgrimes * Lookup or enter a new address in arptab.
8201541Srgrimes */
8211541Srgrimesstatic struct llinfo_arp *
8221541Srgrimesarplookup(addr, create, proxy)
8231541Srgrimes	u_long addr;
8241541Srgrimes	int create, proxy;
8251541Srgrimes{
826126936Smdodd	struct rtentry *rt;
827120727Ssam	struct sockaddr_inarp sin;
8284069Swollman	const char *why = 0;
8291541Srgrimes
830120727Ssam	bzero(&sin, sizeof(sin));
831120727Ssam	sin.sin_len = sizeof(sin);
832120727Ssam	sin.sin_family = AF_INET;
8331541Srgrimes	sin.sin_addr.s_addr = addr;
834120727Ssam	if (proxy)
835120727Ssam		sin.sin_other = SIN_PROXY;
8365101Swollman	rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
8371541Srgrimes	if (rt == 0)
8381541Srgrimes		return (0);
8394069Swollman
84012693Sphk	if (rt->rt_flags & RTF_GATEWAY)
8414069Swollman		why = "host is not on local network";
84212693Sphk	else if ((rt->rt_flags & RTF_LLINFO) == 0)
8434069Swollman		why = "could not allocate llinfo";
84412693Sphk	else if (rt->rt_gateway->sa_family != AF_LINK)
8454069Swollman		why = "gateway route is not ours";
8464069Swollman
847120383Sbms	if (why) {
848120727Ssam#define	ISDYNCLONE(_rt) \
849120727Ssam	(((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED)
850120727Ssam		if (create)
851120383Sbms			log(LOG_DEBUG, "arplookup %s failed: %s\n",
852120383Sbms			    inet_ntoa(sin.sin_addr), why);
853120727Ssam		/*
854120727Ssam		 * If there are no references to this Layer 2 route,
855120727Ssam		 * and it is a cloned route, and not static, and
856120727Ssam		 * arplookup() is creating the route, then purge
857120727Ssam		 * it from the routing table as it is probably bogus.
858120727Ssam		 */
859121770Ssam		if (rt->rt_refcnt == 1 && ISDYNCLONE(rt))
860121770Ssam			rtexpunge(rt);
861121770Ssam		RTFREE_LOCKED(rt);
862120383Sbms		return (0);
863120727Ssam#undef ISDYNCLONE
864120727Ssam	} else {
865122334Ssam		RT_REMREF(rt);
866120727Ssam		RT_UNLOCK(rt);
867120727Ssam		return ((struct llinfo_arp *)rt->rt_llinfo);
8681541Srgrimes	}
8691541Srgrimes}
8701541Srgrimes
8715195Swollmanvoid
87284931Sfjoearp_ifinit(ifp, ifa)
87384931Sfjoe	struct ifnet *ifp;
8745195Swollman	struct ifaddr *ifa;
8755195Swollman{
87625822Stegge	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
87784931Sfjoe		arprequest(ifp, &IA_SIN(ifa)->sin_addr,
87884931Sfjoe				&IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
8795195Swollman	ifa->ifa_rtrequest = arp_rtrequest;
8805195Swollman	ifa->ifa_flags |= RTF_CLONING;
8815195Swollman}
88269152Sjlemon
88369152Sjlemonstatic void
88469152Sjlemonarp_init(void)
88569152Sjlemon{
88669152Sjlemon
88769152Sjlemon	arpintrq.ifq_maxlen = 50;
88893818Sjhb	mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF);
88998459Speter	LIST_INIT(&llinfo_arp);
890120727Ssam	callout_init(&arp_callout, CALLOUT_MPSAFE);
891122320Ssam	netisr_register(NETISR_ARP, arpintr, &arpintrq, NETISR_MPSAFE);
892128641Sluigi	callout_reset(&arp_callout, hz, arptimer, NULL);
89369152Sjlemon}
89469152SjlemonSYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);
895