xref: /freebsd/sys/netinet/if_ether.c (revision a20e25385cdae216f6b28038b295fa15b454dcec)
1c398230bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  *	@(#)if_ether.c	8.1 (Berkeley) 6/10/93
30c3aac50fSPeter Wemm  * $FreeBSD$
31df8bae1dSRodney W. Grimes  */
32df8bae1dSRodney W. Grimes 
33df8bae1dSRodney W. Grimes /*
34df8bae1dSRodney W. Grimes  * Ethernet address resolution protocol.
35df8bae1dSRodney W. Grimes  * TODO:
36df8bae1dSRodney W. Grimes  *	add "inuse/lock" bit (or ref. count) along with valid bit
37df8bae1dSRodney W. Grimes  */
38df8bae1dSRodney W. Grimes 
391d5e9e22SEivind Eklund #include "opt_inet.h"
40b715f178SLuigi Rizzo #include "opt_bdg.h"
4119527d3eSRobert Watson #include "opt_mac.h"
42a9771948SGleb Smirnoff #include "opt_carp.h"
431d5e9e22SEivind Eklund 
44df8bae1dSRodney W. Grimes #include <sys/param.h>
45df8bae1dSRodney W. Grimes #include <sys/kernel.h>
46ce02431fSDoug Rabson #include <sys/queue.h>
47885f1aa4SPoul-Henning Kamp #include <sys/sysctl.h>
48885f1aa4SPoul-Henning Kamp #include <sys/systm.h>
4919527d3eSRobert Watson #include <sys/mac.h>
50885f1aa4SPoul-Henning Kamp #include <sys/mbuf.h>
51885f1aa4SPoul-Henning Kamp #include <sys/malloc.h>
524458ac71SBruce Evans #include <sys/socket.h>
53885f1aa4SPoul-Henning Kamp #include <sys/syslog.h>
54df8bae1dSRodney W. Grimes 
55df8bae1dSRodney W. Grimes #include <net/if.h>
56df8bae1dSRodney W. Grimes #include <net/if_dl.h>
57722012ccSJulian Elischer #include <net/if_types.h>
58df8bae1dSRodney W. Grimes #include <net/route.h>
59748e0b0aSGarrett Wollman #include <net/netisr.h>
60b149dd6cSLarry Lile #include <net/if_llc.h>
61c8f8e9c1SJulian Elischer #include <net/ethernet.h>
62c8f8e9c1SJulian Elischer #include <net/bridge.h>
63df8bae1dSRodney W. Grimes 
64df8bae1dSRodney W. Grimes #include <netinet/in.h>
65df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
66df8bae1dSRodney W. Grimes #include <netinet/if_ether.h>
67df8bae1dSRodney W. Grimes 
68322dcb8dSMax Khon #include <net/if_arc.h>
69fda82fc2SJulian Elischer #include <net/iso88025.h>
70fda82fc2SJulian Elischer 
71a9771948SGleb Smirnoff #ifdef DEV_CARP
72a9771948SGleb Smirnoff #include <netinet/ip_carp.h>
73a9771948SGleb Smirnoff #endif
74a9771948SGleb Smirnoff 
75df8bae1dSRodney W. Grimes #define SIN(s) ((struct sockaddr_in *)s)
76df8bae1dSRodney W. Grimes #define SDL(s) ((struct sockaddr_dl *)s)
77df8bae1dSRodney W. Grimes 
78ce02431fSDoug Rabson SYSCTL_DECL(_net_link_ether);
79602d513cSGarrett Wollman SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
80df8bae1dSRodney W. Grimes 
81df8bae1dSRodney W. Grimes /* timer values */
82602d513cSGarrett Wollman static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
83602d513cSGarrett Wollman static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
84602d513cSGarrett Wollman static int arpt_down = 20;	/* once declared down, don't send for 20 sec */
85885f1aa4SPoul-Henning Kamp 
86602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
87602d513cSGarrett Wollman 	   &arpt_prune, 0, "");
88602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
89602d513cSGarrett Wollman 	   &arpt_keep, 0, "");
90602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
91602d513cSGarrett Wollman 	   &arpt_down, 0, "");
92885f1aa4SPoul-Henning Kamp 
93df8bae1dSRodney W. Grimes #define	rt_expire rt_rmx.rmx_expire
94df8bae1dSRodney W. Grimes 
9566800f57SGarrett Wollman struct llinfo_arp {
96e3975643SJake Burkholder 	LIST_ENTRY(llinfo_arp) la_le;
9766800f57SGarrett Wollman 	struct	rtentry *la_rt;
9866800f57SGarrett Wollman 	struct	mbuf *la_hold;	/* last packet until resolved/timeout */
99022695f8SOrion Hodson 	u_short	la_preempt;	/* countdown for pre-expiry arps */
10073224fb0SOrion Hodson 	u_short	la_asked;	/* #times we QUERIED following expiration */
10166800f57SGarrett Wollman #define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */
10266800f57SGarrett Wollman };
10366800f57SGarrett Wollman 
104e3975643SJake Burkholder static	LIST_HEAD(, llinfo_arp) llinfo_arp;
105df8bae1dSRodney W. Grimes 
1061cafed39SJonathan Lemon static struct	ifqueue arpintrq;
107d1dd20beSSam Leffler static int	arp_allocated;
108df8bae1dSRodney W. Grimes 
109885f1aa4SPoul-Henning Kamp static int	arp_maxtries = 5;
110602d513cSGarrett Wollman static int	useloopback = 1; /* use loopback interface for local traffic */
111885f1aa4SPoul-Henning Kamp static int	arp_proxyall = 0;
112d1dd20beSSam Leffler static struct callout arp_callout;
113602d513cSGarrett Wollman 
114602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
115602d513cSGarrett Wollman 	   &arp_maxtries, 0, "");
116602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
117602d513cSGarrett Wollman 	   &useloopback, 0, "");
118602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
119602d513cSGarrett Wollman 	   &arp_proxyall, 0, "");
120885f1aa4SPoul-Henning Kamp 
1214d77a549SAlfred Perlstein static void	arp_init(void);
1224d77a549SAlfred Perlstein static void	arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
1234d77a549SAlfred Perlstein static void	arprequest(struct ifnet *,
1244d77a549SAlfred Perlstein 			struct in_addr *, struct in_addr *, u_char *);
1251cafed39SJonathan Lemon static void	arpintr(struct mbuf *);
1264d77a549SAlfred Perlstein static void	arptimer(void *);
1271ed7bf1eSGleb Smirnoff static struct rtentry
1284d77a549SAlfred Perlstein 		*arplookup(u_long, int, int);
1291d5e9e22SEivind Eklund #ifdef INET
1304d77a549SAlfred Perlstein static void	in_arpinput(struct mbuf *);
1311d5e9e22SEivind Eklund #endif
13228e82295SGarrett Wollman 
133df8bae1dSRodney W. Grimes /*
134df8bae1dSRodney W. Grimes  * Timeout routine.  Age arp_tab entries periodically.
135df8bae1dSRodney W. Grimes  */
136df8bae1dSRodney W. Grimes /* ARGSUSED */
137df8bae1dSRodney W. Grimes static void
1381ed7bf1eSGleb Smirnoff arptimer(void * __unused unused)
139df8bae1dSRodney W. Grimes {
140c996428cSJeffrey Hsu 	struct llinfo_arp *la, *ola;
141df8bae1dSRodney W. Grimes 
14293f79889SJeffrey Hsu 	RADIX_NODE_HEAD_LOCK(rt_tables[AF_INET]);
1431ed7bf1eSGleb Smirnoff 	LIST_FOREACH_SAFE(la, &llinfo_arp, la_le, ola) {
144c996428cSJeffrey Hsu 		struct rtentry *rt = la->la_rt;
1451ed7bf1eSGleb Smirnoff 
1461ed7bf1eSGleb Smirnoff 		RT_LOCK(rt);
1471ed7bf1eSGleb Smirnoff 		if (rt->rt_expire && rt->rt_expire <= time_second) {
1481ed7bf1eSGleb Smirnoff 			struct sockaddr_dl *sdl = SDL(rt->rt_gateway);
1491ed7bf1eSGleb Smirnoff 
1501ed7bf1eSGleb Smirnoff 			KASSERT(sdl->sdl_family == AF_LINK, ("sdl_family %d",
1511ed7bf1eSGleb Smirnoff 			    sdl->sdl_family));
1521ed7bf1eSGleb Smirnoff 			if (rt->rt_refcnt > 1) {
1531ed7bf1eSGleb Smirnoff 				sdl->sdl_alen = 0;
1541ed7bf1eSGleb Smirnoff 				la->la_preempt = la->la_asked = 0;
1551ed7bf1eSGleb Smirnoff 				rt->rt_flags &= ~RTF_REJECT;
1561ed7bf1eSGleb Smirnoff 				RT_UNLOCK(rt);
1571ed7bf1eSGleb Smirnoff 				continue;
1581ed7bf1eSGleb Smirnoff 			}
1591ed7bf1eSGleb Smirnoff 			RT_UNLOCK(rt);
1601ed7bf1eSGleb Smirnoff 			/*
1611ed7bf1eSGleb Smirnoff 			 * XXX: LIST_REMOVE() is deep inside rtrequest().
1621ed7bf1eSGleb Smirnoff 			 */
1631ed7bf1eSGleb Smirnoff 			rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0,
1641ed7bf1eSGleb Smirnoff 			    NULL);
1651ed7bf1eSGleb Smirnoff 			continue;
1661ed7bf1eSGleb Smirnoff 		}
1671ed7bf1eSGleb Smirnoff 		RT_UNLOCK(rt);
168df8bae1dSRodney W. Grimes 	}
16993f79889SJeffrey Hsu 	RADIX_NODE_HEAD_UNLOCK(rt_tables[AF_INET]);
170d1dd20beSSam Leffler 
171d1dd20beSSam Leffler 	callout_reset(&arp_callout, arpt_prune * hz, arptimer, NULL);
172df8bae1dSRodney W. Grimes }
173df8bae1dSRodney W. Grimes 
174df8bae1dSRodney W. Grimes /*
175df8bae1dSRodney W. Grimes  * Parallel to llc_rtrequest.
176df8bae1dSRodney W. Grimes  */
177b2774d00SGarrett Wollman static void
1788071913dSRuslan Ermilov arp_rtrequest(req, rt, info)
179df8bae1dSRodney W. Grimes 	int req;
180e952fa39SMatthew N. Dodd 	struct rtentry *rt;
1818071913dSRuslan Ermilov 	struct rt_addrinfo *info;
182df8bae1dSRodney W. Grimes {
183e952fa39SMatthew N. Dodd 	struct sockaddr *gate;
184e952fa39SMatthew N. Dodd 	struct llinfo_arp *la;
185df8bae1dSRodney W. Grimes 	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
186067a8babSMax Laier 	struct in_ifaddr *ia;
187067a8babSMax Laier 	struct ifaddr *ifa;
188df8bae1dSRodney W. Grimes 
189d1dd20beSSam Leffler 	RT_LOCK_ASSERT(rt);
190d1dd20beSSam Leffler 
191df8bae1dSRodney W. Grimes 	if (rt->rt_flags & RTF_GATEWAY)
192df8bae1dSRodney W. Grimes 		return;
193d1dd20beSSam Leffler 	gate = rt->rt_gateway;
194d1dd20beSSam Leffler 	la = (struct llinfo_arp *)rt->rt_llinfo;
195df8bae1dSRodney W. Grimes 	switch (req) {
196df8bae1dSRodney W. Grimes 
197df8bae1dSRodney W. Grimes 	case RTM_ADD:
198df8bae1dSRodney W. Grimes 		/*
199df8bae1dSRodney W. Grimes 		 * XXX: If this is a manually added route to interface
200df8bae1dSRodney W. Grimes 		 * such as older version of routed or gated might provide,
201df8bae1dSRodney W. Grimes 		 * restore cloning bit.
202df8bae1dSRodney W. Grimes 		 */
203df8bae1dSRodney W. Grimes 		if ((rt->rt_flags & RTF_HOST) == 0 &&
204d6fa5d28SBruce M Simpson 		    rt_mask(rt) != NULL &&
205df8bae1dSRodney W. Grimes 		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
206df8bae1dSRodney W. Grimes 			rt->rt_flags |= RTF_CLONING;
207df8bae1dSRodney W. Grimes 		if (rt->rt_flags & RTF_CLONING) {
208df8bae1dSRodney W. Grimes 			/*
209df8bae1dSRodney W. Grimes 			 * Case 1: This route should come from a route to iface.
210df8bae1dSRodney W. Grimes 			 */
211df8bae1dSRodney W. Grimes 			rt_setgate(rt, rt_key(rt),
212df8bae1dSRodney W. Grimes 					(struct sockaddr *)&null_sdl);
213df8bae1dSRodney W. Grimes 			gate = rt->rt_gateway;
214df8bae1dSRodney W. Grimes 			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
215df8bae1dSRodney W. Grimes 			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
216227ee8a1SPoul-Henning Kamp 			rt->rt_expire = time_second;
217df8bae1dSRodney W. Grimes 			break;
218df8bae1dSRodney W. Grimes 		}
219df8bae1dSRodney W. Grimes 		/* Announce a new entry if requested. */
220df8bae1dSRodney W. Grimes 		if (rt->rt_flags & RTF_ANNOUNCE)
221322dcb8dSMax Khon 			arprequest(rt->rt_ifp,
222ed7509acSJulian Elischer 			    &SIN(rt_key(rt))->sin_addr,
223ed7509acSJulian Elischer 			    &SIN(rt_key(rt))->sin_addr,
224df8bae1dSRodney W. Grimes 			    (u_char *)LLADDR(SDL(gate)));
225df8bae1dSRodney W. Grimes 		/*FALLTHROUGH*/
226df8bae1dSRodney W. Grimes 	case RTM_RESOLVE:
227df8bae1dSRodney W. Grimes 		if (gate->sa_family != AF_LINK ||
228df8bae1dSRodney W. Grimes 		    gate->sa_len < sizeof(null_sdl)) {
229d1dd20beSSam Leffler 			log(LOG_DEBUG, "%s: bad gateway %s%s\n", __func__,
230beb2ced8SBruce M Simpson 			    inet_ntoa(SIN(rt_key(rt))->sin_addr),
231beb2ced8SBruce M Simpson 			    (gate->sa_family != AF_LINK) ?
232c3b52d64SBruce M Simpson 			    " (!AF_LINK)": "");
233df8bae1dSRodney W. Grimes 			break;
234df8bae1dSRodney W. Grimes 		}
235df8bae1dSRodney W. Grimes 		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
236df8bae1dSRodney W. Grimes 		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
237df8bae1dSRodney W. Grimes 		if (la != 0)
238df8bae1dSRodney W. Grimes 			break; /* This happens on a route change */
239df8bae1dSRodney W. Grimes 		/*
240df8bae1dSRodney W. Grimes 		 * Case 2:  This route may come from cloning, or a manual route
241df8bae1dSRodney W. Grimes 		 * add with a LL address.
242df8bae1dSRodney W. Grimes 		 */
243d1dd20beSSam Leffler 		R_Zalloc(la, struct llinfo_arp *, sizeof(*la));
244df8bae1dSRodney W. Grimes 		rt->rt_llinfo = (caddr_t)la;
245df8bae1dSRodney W. Grimes 		if (la == 0) {
246d1dd20beSSam Leffler 			log(LOG_DEBUG, "%s: malloc failed\n", __func__);
247df8bae1dSRodney W. Grimes 			break;
248df8bae1dSRodney W. Grimes 		}
249d1dd20beSSam Leffler 		arp_allocated++;
2501ed7bf1eSGleb Smirnoff 		/*
2511ed7bf1eSGleb Smirnoff 		 * We are storing a route entry outside of radix tree. So,
2521ed7bf1eSGleb Smirnoff 		 * it can be found and accessed by other means than radix
2531ed7bf1eSGleb Smirnoff 		 * lookup. The routing code assumes that any rtentry detached
2541ed7bf1eSGleb Smirnoff 		 * from radix can be destroyed safely. To prevent this, we
2551ed7bf1eSGleb Smirnoff 		 * add an additional reference.
2561ed7bf1eSGleb Smirnoff 		 */
2571ed7bf1eSGleb Smirnoff 		RT_ADDREF(rt);
258df8bae1dSRodney W. Grimes 		la->la_rt = rt;
259df8bae1dSRodney W. Grimes 		rt->rt_flags |= RTF_LLINFO;
26093f79889SJeffrey Hsu 		RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]);
26166800f57SGarrett Wollman 		LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
262cbb0b46aSGarrett Wollman 
2631d5e9e22SEivind Eklund #ifdef INET
264cbb0b46aSGarrett Wollman 		/*
265cbb0b46aSGarrett Wollman 		 * This keeps the multicast addresses from showing up
266cbb0b46aSGarrett Wollman 		 * in `arp -a' listings as unresolved.  It's not actually
267cbb0b46aSGarrett Wollman 		 * functional.  Then the same for broadcast.
268cbb0b46aSGarrett Wollman 		 */
269c448c89cSJonathan Lemon 		if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) &&
270c448c89cSJonathan Lemon 		    rt->rt_ifp->if_type != IFT_ARCNET) {
271cbb0b46aSGarrett Wollman 			ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
272cbb0b46aSGarrett Wollman 					       LLADDR(SDL(gate)));
273cbb0b46aSGarrett Wollman 			SDL(gate)->sdl_alen = 6;
27451a109a1SPeter Wemm 			rt->rt_expire = 0;
275cbb0b46aSGarrett Wollman 		}
276cbb0b46aSGarrett Wollman 		if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
277322dcb8dSMax Khon 			memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
278322dcb8dSMax Khon 			       rt->rt_ifp->if_addrlen);
279322dcb8dSMax Khon 			SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
28051a109a1SPeter Wemm 			rt->rt_expire = 0;
281cbb0b46aSGarrett Wollman 		}
2821d5e9e22SEivind Eklund #endif
283cbb0b46aSGarrett Wollman 
284067a8babSMax Laier 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
285067a8babSMax Laier 			if (ia->ia_ifp == rt->rt_ifp &&
286067a8babSMax Laier 			    SIN(rt_key(rt))->sin_addr.s_addr ==
287067a8babSMax Laier 			    (IA_SIN(ia))->sin_addr.s_addr)
288067a8babSMax Laier 				break;
289067a8babSMax Laier 		}
290067a8babSMax Laier 		if (ia) {
291df8bae1dSRodney W. Grimes 		    /*
292df8bae1dSRodney W. Grimes 		     * This test used to be
293df8bae1dSRodney W. Grimes 		     *	if (loif.if_flags & IFF_UP)
294df8bae1dSRodney W. Grimes 		     * It allowed local traffic to be forced
295df8bae1dSRodney W. Grimes 		     * through the hardware by configuring the loopback down.
296df8bae1dSRodney W. Grimes 		     * However, it causes problems during network configuration
297df8bae1dSRodney W. Grimes 		     * for boards that can't receive packets they send.
298df8bae1dSRodney W. Grimes 		     * It is now necessary to clear "useloopback" and remove
299df8bae1dSRodney W. Grimes 		     * the route to force traffic out to the hardware.
300df8bae1dSRodney W. Grimes 		     */
301df8bae1dSRodney W. Grimes 			rt->rt_expire = 0;
302ac912b2dSLuigi Rizzo 			bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
303322dcb8dSMax Khon 			      SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
304df8bae1dSRodney W. Grimes 			if (useloopback)
305f5fea3ddSPaul Traina 				rt->rt_ifp = loif;
306df8bae1dSRodney W. Grimes 
307067a8babSMax Laier 		    /*
308067a8babSMax Laier 		     * make sure to set rt->rt_ifa to the interface
309067a8babSMax Laier 		     * address we are using, otherwise we will have trouble
310067a8babSMax Laier 		     * with source address selection.
311067a8babSMax Laier 		     */
312067a8babSMax Laier 			ifa = &ia->ia_ifa;
313067a8babSMax Laier 			if (ifa != rt->rt_ifa) {
314067a8babSMax Laier 				IFAFREE(rt->rt_ifa);
315067a8babSMax Laier 				IFAREF(ifa);
316067a8babSMax Laier 				rt->rt_ifa = ifa;
317067a8babSMax Laier 			}
318df8bae1dSRodney W. Grimes 		}
319df8bae1dSRodney W. Grimes 		break;
320df8bae1dSRodney W. Grimes 
321df8bae1dSRodney W. Grimes 	case RTM_DELETE:
322df8bae1dSRodney W. Grimes 		if (la == 0)
323df8bae1dSRodney W. Grimes 			break;
32493f79889SJeffrey Hsu 		RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]);
32566800f57SGarrett Wollman 		LIST_REMOVE(la, la_le);
3261ed7bf1eSGleb Smirnoff 		RT_REMREF(rt);
327df8bae1dSRodney W. Grimes 		rt->rt_llinfo = 0;
328df8bae1dSRodney W. Grimes 		rt->rt_flags &= ~RTF_LLINFO;
329df8bae1dSRodney W. Grimes 		if (la->la_hold)
330df8bae1dSRodney W. Grimes 			m_freem(la->la_hold);
331df8bae1dSRodney W. Grimes 		Free((caddr_t)la);
332df8bae1dSRodney W. Grimes 	}
333df8bae1dSRodney W. Grimes }
334df8bae1dSRodney W. Grimes 
335df8bae1dSRodney W. Grimes /*
336df8bae1dSRodney W. Grimes  * Broadcast an ARP request. Caller specifies:
337df8bae1dSRodney W. Grimes  *	- arp header source ip address
338df8bae1dSRodney W. Grimes  *	- arp header target ip address
339df8bae1dSRodney W. Grimes  *	- arp header source ethernet address
340df8bae1dSRodney W. Grimes  */
341df8bae1dSRodney W. Grimes static void
342322dcb8dSMax Khon arprequest(ifp, sip, tip, enaddr)
343e952fa39SMatthew N. Dodd 	struct ifnet *ifp;
344e952fa39SMatthew N. Dodd 	struct in_addr *sip, *tip;
345e952fa39SMatthew N. Dodd 	u_char *enaddr;
346df8bae1dSRodney W. Grimes {
347e952fa39SMatthew N. Dodd 	struct mbuf *m;
348e952fa39SMatthew N. Dodd 	struct arphdr *ah;
349df8bae1dSRodney W. Grimes 	struct sockaddr sa;
350df8bae1dSRodney W. Grimes 
351a163d034SWarner Losh 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
352df8bae1dSRodney W. Grimes 		return;
35364bf80ceSMatthew N. Dodd 	m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
35464bf80ceSMatthew N. Dodd 		2*ifp->if_data.ifi_addrlen;
35564bf80ceSMatthew N. Dodd 	m->m_pkthdr.len = m->m_len;
35664bf80ceSMatthew N. Dodd 	MH_ALIGN(m, m->m_len);
35764bf80ceSMatthew N. Dodd 	ah = mtod(m, struct arphdr *);
35864bf80ceSMatthew N. Dodd 	bzero((caddr_t)ah, m->m_len);
35919527d3eSRobert Watson #ifdef MAC
36019527d3eSRobert Watson 	mac_create_mbuf_linklayer(ifp, m);
36119527d3eSRobert Watson #endif
362322dcb8dSMax Khon 	ah->ar_pro = htons(ETHERTYPE_IP);
363322dcb8dSMax Khon 	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
364322dcb8dSMax Khon 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
365322dcb8dSMax Khon 	ah->ar_op = htons(ARPOP_REQUEST);
36664bf80ceSMatthew N. Dodd 	bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
36764bf80ceSMatthew N. Dodd 	bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
36864bf80ceSMatthew N. Dodd 	bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
36964bf80ceSMatthew N. Dodd 	sa.sa_family = AF_ARP;
37064bf80ceSMatthew N. Dodd 	sa.sa_len = 2;
37164bf80ceSMatthew N. Dodd 	m->m_flags |= M_BCAST;
372322dcb8dSMax Khon 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
37364bf80ceSMatthew N. Dodd 
37464bf80ceSMatthew N. Dodd 	return;
375df8bae1dSRodney W. Grimes }
376df8bae1dSRodney W. Grimes 
377df8bae1dSRodney W. Grimes /*
378cd46a114SLuigi Rizzo  * Resolve an IP address into an ethernet address.
379cd46a114SLuigi Rizzo  * On input:
380cd46a114SLuigi Rizzo  *    ifp is the interface we use
381cd46a114SLuigi Rizzo  *    dst is the next hop,
382cd46a114SLuigi Rizzo  *    rt0 is the route to the final destination (possibly useless)
383cd46a114SLuigi Rizzo  *    m is the mbuf
384cd46a114SLuigi Rizzo  *    desten is where we want the address.
385cd46a114SLuigi Rizzo  *
386cd46a114SLuigi Rizzo  * On success, desten is filled in and the function returns 0;
387cd46a114SLuigi Rizzo  * If the packet must be held pending resolution, we return EWOULDBLOCK
388cd46a114SLuigi Rizzo  * On other errors, we return the corresponding error code.
389df8bae1dSRodney W. Grimes  */
390df8bae1dSRodney W. Grimes int
391cd46a114SLuigi Rizzo arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
392f7c5baa1SLuigi Rizzo 	struct sockaddr *dst, u_char *desten)
393df8bae1dSRodney W. Grimes {
3941ed7bf1eSGleb Smirnoff 	struct llinfo_arp *la = NULL;
3951ed7bf1eSGleb Smirnoff 	struct rtentry *rt = NULL;
396df8bae1dSRodney W. Grimes 	struct sockaddr_dl *sdl;
397cd46a114SLuigi Rizzo 	int error;
398df8bae1dSRodney W. Grimes 
399df8bae1dSRodney W. Grimes 	if (m->m_flags & M_BCAST) {	/* broadcast */
400322dcb8dSMax Khon 		(void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
401cd46a114SLuigi Rizzo 		return (0);
402df8bae1dSRodney W. Grimes 	}
403322dcb8dSMax Khon 	if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */
404df8bae1dSRodney W. Grimes 		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
405cd46a114SLuigi Rizzo 		return (0);
406df8bae1dSRodney W. Grimes 	}
4071ed7bf1eSGleb Smirnoff 
4081ed7bf1eSGleb Smirnoff 	if (rt0 != NULL) {
4091ed7bf1eSGleb Smirnoff 		error = rt_check(&rt, &rt0, dst);
4101ed7bf1eSGleb Smirnoff 		if (error) {
4111ed7bf1eSGleb Smirnoff 			m_freem(m);
4121ed7bf1eSGleb Smirnoff 			return error;
413df8bae1dSRodney W. Grimes 		}
4141ed7bf1eSGleb Smirnoff 		la = (struct llinfo_arp *)rt->rt_llinfo;
4151ed7bf1eSGleb Smirnoff 		if (la == NULL)
4161ed7bf1eSGleb Smirnoff 			RT_UNLOCK(rt);
4171ed7bf1eSGleb Smirnoff 	}
4181ed7bf1eSGleb Smirnoff 	if (la == NULL) {
4191ed7bf1eSGleb Smirnoff 		/*
4201ed7bf1eSGleb Smirnoff 		 * We enter this block in case if rt0 was NULL,
4211ed7bf1eSGleb Smirnoff 		 * or if rt found by rt_check() didn't have llinfo.
4221ed7bf1eSGleb Smirnoff 		 */
4231ed7bf1eSGleb Smirnoff 		rt = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
4241ed7bf1eSGleb Smirnoff 		if (rt == NULL) {
4251ed7bf1eSGleb Smirnoff 			log(LOG_DEBUG,
4261ed7bf1eSGleb Smirnoff 			    "arpresolve: can't allocate route for %s\n",
4271ed7bf1eSGleb Smirnoff 			    inet_ntoa(SIN(dst)->sin_addr));
428df8bae1dSRodney W. Grimes 			m_freem(m);
429cd46a114SLuigi Rizzo 			return (EINVAL); /* XXX */
430df8bae1dSRodney W. Grimes 		}
4311ed7bf1eSGleb Smirnoff 		la = (struct llinfo_arp *)rt->rt_llinfo;
4321ed7bf1eSGleb Smirnoff 		if (la == NULL) {
4331ed7bf1eSGleb Smirnoff 			RT_UNLOCK(rt);
4341ed7bf1eSGleb Smirnoff 			log(LOG_DEBUG,
4351ed7bf1eSGleb Smirnoff 			    "arpresolve: can't allocate llinfo for %s\n",
4361ed7bf1eSGleb Smirnoff 			    inet_ntoa(SIN(dst)->sin_addr));
4371ed7bf1eSGleb Smirnoff 			m_freem(m);
4381ed7bf1eSGleb Smirnoff 			return (EINVAL); /* XXX */
4391ed7bf1eSGleb Smirnoff 		}
4401ed7bf1eSGleb Smirnoff 	}
441df8bae1dSRodney W. Grimes 	sdl = SDL(rt->rt_gateway);
442df8bae1dSRodney W. Grimes 	/*
443df8bae1dSRodney W. Grimes 	 * Check the address family and length is valid, the address
444df8bae1dSRodney W. Grimes 	 * is resolved; otherwise, try to resolve.
445df8bae1dSRodney W. Grimes 	 */
446227ee8a1SPoul-Henning Kamp 	if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
447df8bae1dSRodney W. Grimes 	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
448a20e2538SGleb Smirnoff 
449a20e2538SGleb Smirnoff 		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
450a20e2538SGleb Smirnoff 
451f0f3379eSOrion Hodson 		/*
452f0f3379eSOrion Hodson 		 * If entry has an expiry time and it is approaching,
453f0f3379eSOrion Hodson 		 * see if we need to send an ARP request within this
454f0f3379eSOrion Hodson 		 * arpt_down interval.
455f0f3379eSOrion Hodson 		 */
456f0f3379eSOrion Hodson 		if ((rt->rt_expire != 0) &&
457022695f8SOrion Hodson 		    (time_second + la->la_preempt > rt->rt_expire)) {
458a20e2538SGleb Smirnoff 			struct in_addr sin =
459a20e2538SGleb Smirnoff 			    SIN(rt->rt_ifa->ifa_addr)->sin_addr;
460a20e2538SGleb Smirnoff 
461022695f8SOrion Hodson 			la->la_preempt--;
462a20e2538SGleb Smirnoff 			RT_UNLOCK(rt);
463a20e2538SGleb Smirnoff 			arprequest(ifp, &sin, &SIN(dst)->sin_addr,
464a20e2538SGleb Smirnoff 			    IF_LLADDR(ifp));
465a20e2538SGleb Smirnoff 			return (0);
466f0f3379eSOrion Hodson 		}
467f0f3379eSOrion Hodson 
4681ed7bf1eSGleb Smirnoff 		RT_UNLOCK(rt);
469cd46a114SLuigi Rizzo 		return (0);
470df8bae1dSRodney W. Grimes 	}
471df8bae1dSRodney W. Grimes 	/*
472deb62e28SRuslan Ermilov 	 * If ARP is disabled or static on this interface, stop.
47308aadfbbSJonathan Lemon 	 * XXX
47408aadfbbSJonathan Lemon 	 * Probably should not allocate empty llinfo struct if we are
47508aadfbbSJonathan Lemon 	 * not going to be sending out an arp request.
47608aadfbbSJonathan Lemon 	 */
477deb62e28SRuslan Ermilov 	if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
4781ed7bf1eSGleb Smirnoff 		RT_UNLOCK(rt);
47947891de1SRuslan Ermilov 		m_freem(m);
480cd46a114SLuigi Rizzo 		return (EINVAL);
48147891de1SRuslan Ermilov 	}
48208aadfbbSJonathan Lemon 	/*
483df8bae1dSRodney W. Grimes 	 * There is an arptab entry, but no ethernet address
484df8bae1dSRodney W. Grimes 	 * response yet.  Replace the held mbuf with this
485df8bae1dSRodney W. Grimes 	 * latest one.
486df8bae1dSRodney W. Grimes 	 */
487df8bae1dSRodney W. Grimes 	if (la->la_hold)
488df8bae1dSRodney W. Grimes 		m_freem(la->la_hold);
489df8bae1dSRodney W. Grimes 	la->la_hold = m;
490df8bae1dSRodney W. Grimes 	if (rt->rt_expire) {
491df8bae1dSRodney W. Grimes 		rt->rt_flags &= ~RTF_REJECT;
492227ee8a1SPoul-Henning Kamp 		if (la->la_asked == 0 || rt->rt_expire != time_second) {
493227ee8a1SPoul-Henning Kamp 			rt->rt_expire = time_second;
49473224fb0SOrion Hodson 			if (la->la_asked++ < arp_maxtries) {
495a20e2538SGleb Smirnoff 				struct in_addr sin =
496a20e2538SGleb Smirnoff 				    SIN(rt->rt_ifa->ifa_addr)->sin_addr;
497a20e2538SGleb Smirnoff 
498a20e2538SGleb Smirnoff 				RT_UNLOCK(rt);
499a20e2538SGleb Smirnoff 				arprequest(ifp, &sin, &SIN(dst)->sin_addr,
500322dcb8dSMax Khon 				    IF_LLADDR(ifp));
501a20e2538SGleb Smirnoff 				return (EWOULDBLOCK);
50273224fb0SOrion Hodson 			} else {
503df8bae1dSRodney W. Grimes 				rt->rt_flags |= RTF_REJECT;
504df8bae1dSRodney W. Grimes 				rt->rt_expire += arpt_down;
505022695f8SOrion Hodson 				la->la_asked = 0;
506022695f8SOrion Hodson 				la->la_preempt = arp_maxtries;
507df8bae1dSRodney W. Grimes 			}
508df8bae1dSRodney W. Grimes 
509df8bae1dSRodney W. Grimes 		}
510df8bae1dSRodney W. Grimes 	}
5111ed7bf1eSGleb Smirnoff 	RT_UNLOCK(rt);
512cd46a114SLuigi Rizzo 	return (EWOULDBLOCK);
513df8bae1dSRodney W. Grimes }
514df8bae1dSRodney W. Grimes 
515df8bae1dSRodney W. Grimes /*
516df8bae1dSRodney W. Grimes  * Common length and type checks are done here,
517df8bae1dSRodney W. Grimes  * then the protocol-specific routine is called.
518df8bae1dSRodney W. Grimes  */
519885f1aa4SPoul-Henning Kamp static void
5201cafed39SJonathan Lemon arpintr(struct mbuf *m)
521df8bae1dSRodney W. Grimes {
5221cafed39SJonathan Lemon 	struct arphdr *ar;
523df8bae1dSRodney W. Grimes 
52476ec7b2fSRobert Watson 	if (m->m_len < sizeof(struct arphdr) &&
52584365e2bSMatthew Dillon 	    ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
526e44d6283SJoerg Wunsch 		log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
5271cafed39SJonathan Lemon 		return;
52876ec7b2fSRobert Watson 	}
52976ec7b2fSRobert Watson 	ar = mtod(m, struct arphdr *);
53076ec7b2fSRobert Watson 
5311cafed39SJonathan Lemon 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER &&
5321cafed39SJonathan Lemon 	    ntohs(ar->ar_hrd) != ARPHRD_IEEE802 &&
533b8b33234SDoug Rabson 	    ntohs(ar->ar_hrd) != ARPHRD_ARCNET &&
534b8b33234SDoug Rabson 	    ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) {
5351cafed39SJonathan Lemon 		log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
53676ec7b2fSRobert Watson 		    (unsigned char *)&ar->ar_hrd, "");
53776ec7b2fSRobert Watson 		m_freem(m);
5381cafed39SJonathan Lemon 		return;
53976ec7b2fSRobert Watson 	}
54076ec7b2fSRobert Watson 
54131175791SRuslan Ermilov 	if (m->m_len < arphdr_len(ar)) {
54278e2d2bdSRuslan Ermilov 		if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
543f72d9d83SJoerg Wunsch 			log(LOG_ERR, "arp: runt packet\n");
54476ec7b2fSRobert Watson 			m_freem(m);
5451cafed39SJonathan Lemon 			return;
54676ec7b2fSRobert Watson 		}
54778e2d2bdSRuslan Ermilov 		ar = mtod(m, struct arphdr *);
54878e2d2bdSRuslan Ermilov 	}
549df8bae1dSRodney W. Grimes 
550df8bae1dSRodney W. Grimes 	switch (ntohs(ar->ar_pro)) {
5511d5e9e22SEivind Eklund #ifdef INET
552df8bae1dSRodney W. Grimes 	case ETHERTYPE_IP:
553df8bae1dSRodney W. Grimes 		in_arpinput(m);
5541cafed39SJonathan Lemon 		return;
5551d5e9e22SEivind Eklund #endif
556df8bae1dSRodney W. Grimes 	}
557df8bae1dSRodney W. Grimes 	m_freem(m);
558df8bae1dSRodney W. Grimes }
559df8bae1dSRodney W. Grimes 
5601d5e9e22SEivind Eklund #ifdef INET
561df8bae1dSRodney W. Grimes /*
562df8bae1dSRodney W. Grimes  * ARP for Internet protocols on 10 Mb/s Ethernet.
563df8bae1dSRodney W. Grimes  * Algorithm is that given in RFC 826.
564df8bae1dSRodney W. Grimes  * In addition, a sanity check is performed on the sender
565df8bae1dSRodney W. Grimes  * protocol address, to catch impersonators.
566df8bae1dSRodney W. Grimes  * We no longer handle negotiations for use of trailer protocol:
567df8bae1dSRodney W. Grimes  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
568df8bae1dSRodney W. Grimes  * along with IP replies if we wanted trailers sent to us,
569df8bae1dSRodney W. Grimes  * and also sent them in response to IP replies.
570df8bae1dSRodney W. Grimes  * This allowed either end to announce the desire to receive
571df8bae1dSRodney W. Grimes  * trailer packets.
572df8bae1dSRodney W. Grimes  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
573df8bae1dSRodney W. Grimes  * but formerly didn't normally send requests.
574df8bae1dSRodney W. Grimes  */
5753269187dSAlfred Perlstein static int log_arp_wrong_iface = 1;
576e3d123d6SAlfred Perlstein static int log_arp_movements = 1;
5773269187dSAlfred Perlstein 
5783269187dSAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
5793269187dSAlfred Perlstein 	&log_arp_wrong_iface, 0,
5803269187dSAlfred Perlstein 	"log arp packets arriving on the wrong interface");
581e3d123d6SAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
582e3d123d6SAlfred Perlstein         &log_arp_movements, 0,
58375ce3221SAlfred Perlstein         "log arp replies from MACs different than the one in the cache");
584e3d123d6SAlfred Perlstein 
5853269187dSAlfred Perlstein 
586df8bae1dSRodney W. Grimes static void
587df8bae1dSRodney W. Grimes in_arpinput(m)
588df8bae1dSRodney W. Grimes 	struct mbuf *m;
589df8bae1dSRodney W. Grimes {
590e952fa39SMatthew N. Dodd 	struct arphdr *ah;
591e952fa39SMatthew N. Dodd 	struct ifnet *ifp = m->m_pkthdr.rcvif;
592fda82fc2SJulian Elischer 	struct iso88025_header *th = (struct iso88025_header *)0;
59342fdfc12SKelly Yancey 	struct iso88025_sockaddr_dl_data *trld;
5941ed7bf1eSGleb Smirnoff 	struct llinfo_arp *la;
595e952fa39SMatthew N. Dodd 	struct rtentry *rt;
596ca925d9cSJonathan Lemon 	struct ifaddr *ifa;
597ca925d9cSJonathan Lemon 	struct in_ifaddr *ia;
598df8bae1dSRodney W. Grimes 	struct sockaddr_dl *sdl;
599df8bae1dSRodney W. Grimes 	struct sockaddr sa;
600df8bae1dSRodney W. Grimes 	struct in_addr isaddr, itaddr, myaddr;
6011ed7bf1eSGleb Smirnoff 	struct mbuf *hold;
602a9771948SGleb Smirnoff 	u_int8_t *enaddr = NULL;
603b149dd6cSLarry Lile 	int op, rif_len;
604322dcb8dSMax Khon 	int req_len;
6058f867517SAndrew Thompson 	int bridged = 0;
606422a115aSGleb Smirnoff #ifdef DEV_CARP
6072ef4a436SGleb Smirnoff 	int carp_match = 0;
608422a115aSGleb Smirnoff #endif
609df8bae1dSRodney W. Grimes 
6108f867517SAndrew Thompson 	if (do_bridge || ifp->if_bridge)
6118f867517SAndrew Thompson 		bridged = 1;
6128f867517SAndrew Thompson 
613322dcb8dSMax Khon 	req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
614322dcb8dSMax Khon 	if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
6154cbc8ad1SYaroslav Tykhiy 		log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
6164cbc8ad1SYaroslav Tykhiy 		return;
6174cbc8ad1SYaroslav Tykhiy 	}
6184cbc8ad1SYaroslav Tykhiy 
619322dcb8dSMax Khon 	ah = mtod(m, struct arphdr *);
620322dcb8dSMax Khon 	op = ntohs(ah->ar_op);
621322dcb8dSMax Khon 	(void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
622322dcb8dSMax Khon 	(void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
62332439868SGleb Smirnoff 
624ca925d9cSJonathan Lemon 	/*
625ca925d9cSJonathan Lemon 	 * For a bridge, we want to check the address irrespective
626ca925d9cSJonathan Lemon 	 * of the receive interface. (This will change slightly
627ca925d9cSJonathan Lemon 	 * when we have clusters of interfaces).
628a9771948SGleb Smirnoff 	 * If the interface does not match, but the recieving interface
629a9771948SGleb Smirnoff 	 * is part of carp, we call carp_iamatch to see if this is a
630a9771948SGleb Smirnoff 	 * request for the virtual host ip.
631a9771948SGleb Smirnoff 	 * XXX: This is really ugly!
632ca925d9cSJonathan Lemon 	 */
6332ef4a436SGleb Smirnoff 	LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
6348f867517SAndrew Thompson 		if ((bridged || (ia->ia_ifp == ifp)) &&
6352ef4a436SGleb Smirnoff 		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
636ca925d9cSJonathan Lemon 			goto match;
6372ef4a436SGleb Smirnoff #ifdef DEV_CARP
6382ef4a436SGleb Smirnoff 		if (ifp->if_carp != NULL &&
6392ef4a436SGleb Smirnoff 		    carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) &&
6402ef4a436SGleb Smirnoff 		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
6412ef4a436SGleb Smirnoff 			carp_match = 1;
6422ef4a436SGleb Smirnoff 			goto match;
6432ef4a436SGleb Smirnoff 		}
6442ef4a436SGleb Smirnoff #endif
6452ef4a436SGleb Smirnoff 	}
646ca925d9cSJonathan Lemon 	LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
6478f867517SAndrew Thompson 		if ((bridged || (ia->ia_ifp == ifp)) &&
648ca925d9cSJonathan Lemon 		    isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
649ca925d9cSJonathan Lemon 			goto match;
650ca925d9cSJonathan Lemon 	/*
651d8b84d9eSJonathan Lemon 	 * No match, use the first inet address on the receive interface
652ca925d9cSJonathan Lemon 	 * as a dummy address for the rest of the function.
653ca925d9cSJonathan Lemon 	 */
654d8b84d9eSJonathan Lemon 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
655ec691a10SJonathan Lemon 		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
656ec691a10SJonathan Lemon 			ia = ifatoia(ifa);
657ec691a10SJonathan Lemon 			goto match;
658ec691a10SJonathan Lemon 		}
659ec691a10SJonathan Lemon 	/*
660ec691a10SJonathan Lemon 	 * If bridging, fall back to using any inet address.
661ec691a10SJonathan Lemon 	 */
6628f867517SAndrew Thompson 	if (!bridged || (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL)
663b2a8ac7cSLuigi Rizzo 		goto drop;
664ca925d9cSJonathan Lemon match:
665a9771948SGleb Smirnoff 	if (!enaddr)
666a9771948SGleb Smirnoff 		enaddr = (u_int8_t *)IF_LLADDR(ifp);
667ca925d9cSJonathan Lemon 	myaddr = ia->ia_addr.sin_addr;
668a9771948SGleb Smirnoff 	if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
669b2a8ac7cSLuigi Rizzo 		goto drop;	/* it's from me, ignore it. */
670322dcb8dSMax Khon 	if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
671df8bae1dSRodney W. Grimes 		log(LOG_ERR,
672322dcb8dSMax Khon 		    "arp: link address is broadcast for IP address %s!\n",
673ef0cdf33SGarrett Wollman 		    inet_ntoa(isaddr));
674b2a8ac7cSLuigi Rizzo 		goto drop;
675df8bae1dSRodney W. Grimes 	}
67600fcf9d1SRobert Watson 	/*
67700fcf9d1SRobert Watson 	 * Warn if another host is using the same IP address, but only if the
67800fcf9d1SRobert Watson 	 * IP address isn't 0.0.0.0, which is used for DHCP only, in which
67900fcf9d1SRobert Watson 	 * case we suppress the warning to avoid false positive complaints of
68000fcf9d1SRobert Watson 	 * potential misconfiguration.
68100fcf9d1SRobert Watson 	 */
68200fcf9d1SRobert Watson 	if (isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
683df8bae1dSRodney W. Grimes 		log(LOG_ERR,
684322dcb8dSMax Khon 		   "arp: %*D is using my IP address %s!\n",
685322dcb8dSMax Khon 		   ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
686322dcb8dSMax Khon 		   inet_ntoa(isaddr));
687df8bae1dSRodney W. Grimes 		itaddr = myaddr;
688df8bae1dSRodney W. Grimes 		goto reply;
689df8bae1dSRodney W. Grimes 	}
690deb62e28SRuslan Ermilov 	if (ifp->if_flags & IFF_STATICARP)
691deb62e28SRuslan Ermilov 		goto reply;
6921ed7bf1eSGleb Smirnoff 	rt = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
6931ed7bf1eSGleb Smirnoff 	if (rt != NULL) {
6941ed7bf1eSGleb Smirnoff 		la = (struct llinfo_arp *)rt->rt_llinfo;
6951ed7bf1eSGleb Smirnoff 		if (la == NULL) {
6961ed7bf1eSGleb Smirnoff 			RT_UNLOCK(rt);
6971ed7bf1eSGleb Smirnoff 			goto reply;
6981ed7bf1eSGleb Smirnoff 		}
6991ed7bf1eSGleb Smirnoff 	} else
7001ed7bf1eSGleb Smirnoff 		goto reply;
7011ed7bf1eSGleb Smirnoff 
7021ed7bf1eSGleb Smirnoff 	/* The following is not an error when doing bridging. */
7038f867517SAndrew Thompson 	if (!bridged && rt->rt_ifp != ifp
704422a115aSGleb Smirnoff #ifdef DEV_CARP
705422a115aSGleb Smirnoff 	    && (ifp->if_type != IFT_CARP || !carp_match)
706422a115aSGleb Smirnoff #endif
707422a115aSGleb Smirnoff 							) {
7083269187dSAlfred Perlstein 		if (log_arp_wrong_iface)
7099bf40edeSBrooks Davis 			log(LOG_ERR, "arp: %s is on %s but got reply from %*D on %s\n",
710dd9b6ddeSBill Fenner 			    inet_ntoa(isaddr),
7119bf40edeSBrooks Davis 			    rt->rt_ifp->if_xname,
712322dcb8dSMax Khon 			    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
7139bf40edeSBrooks Davis 			    ifp->if_xname);
7141ed7bf1eSGleb Smirnoff 		RT_UNLOCK(rt);
715dd9b6ddeSBill Fenner 		goto reply;
716dd9b6ddeSBill Fenner 	}
7171ed7bf1eSGleb Smirnoff 	sdl = SDL(rt->rt_gateway);
718df8bae1dSRodney W. Grimes 	if (sdl->sdl_alen &&
719322dcb8dSMax Khon 	    bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
720e3d123d6SAlfred Perlstein 		if (rt->rt_expire) {
721e3d123d6SAlfred Perlstein 		    if (log_arp_movements)
7229bf40edeSBrooks Davis 		        log(LOG_INFO, "arp: %s moved from %*D to %*D on %s\n",
723322dcb8dSMax Khon 			    inet_ntoa(isaddr),
724322dcb8dSMax Khon 			    ifp->if_addrlen, (u_char *)LLADDR(sdl), ":",
725322dcb8dSMax Khon 			    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
7269bf40edeSBrooks Davis 			    ifp->if_xname);
727e3d123d6SAlfred Perlstein 		} else {
728dd9b6ddeSBill Fenner 		    log(LOG_ERR,
7299bf40edeSBrooks Davis 			"arp: %*D attempts to modify permanent entry for %s on %s\n",
730322dcb8dSMax Khon 			ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
7319bf40edeSBrooks Davis 			inet_ntoa(isaddr), ifp->if_xname);
7321ed7bf1eSGleb Smirnoff 		    RT_UNLOCK(rt);
733dd9b6ddeSBill Fenner 		    goto reply;
734dd9b6ddeSBill Fenner 		}
735dfd5dee1SPeter Wemm 	}
736322dcb8dSMax Khon 	/*
737322dcb8dSMax Khon 	 * sanity check for the address length.
738322dcb8dSMax Khon 	 * XXX this does not work for protocols with variable address
739322dcb8dSMax Khon 	 * length. -is
740322dcb8dSMax Khon 	 */
741322dcb8dSMax Khon 	if (sdl->sdl_alen &&
742322dcb8dSMax Khon 	    sdl->sdl_alen != ah->ar_hln) {
743322dcb8dSMax Khon 		log(LOG_WARNING,
744322dcb8dSMax Khon 		    "arp from %*D: new addr len %d, was %d",
745322dcb8dSMax Khon 		    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
746322dcb8dSMax Khon 		    ah->ar_hln, sdl->sdl_alen);
747322dcb8dSMax Khon 	}
748322dcb8dSMax Khon 	if (ifp->if_addrlen != ah->ar_hln) {
749322dcb8dSMax Khon 		log(LOG_WARNING,
750322dcb8dSMax Khon 		    "arp from %*D: addr len: new %d, i/f %d (ignored)",
751322dcb8dSMax Khon 		    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
752322dcb8dSMax Khon 		    ah->ar_hln, ifp->if_addrlen);
7531ed7bf1eSGleb Smirnoff 		RT_UNLOCK(rt);
754322dcb8dSMax Khon 		goto reply;
755322dcb8dSMax Khon 	}
756322dcb8dSMax Khon 	(void)memcpy(LLADDR(sdl), ar_sha(ah),
757322dcb8dSMax Khon 	    sdl->sdl_alen = ah->ar_hln);
758243c4c6dSEivind Eklund 	/*
759243c4c6dSEivind Eklund 	 * If we receive an arp from a token-ring station over
760243c4c6dSEivind Eklund 	 * a token-ring nic then try to save the source
761243c4c6dSEivind Eklund 	 * routing info.
762243c4c6dSEivind Eklund 	 */
763322dcb8dSMax Khon 	if (ifp->if_type == IFT_ISO88025) {
764fda82fc2SJulian Elischer 		th = (struct iso88025_header *)m->m_pkthdr.header;
76542fdfc12SKelly Yancey 		trld = SDL_ISO88025(sdl);
766b149dd6cSLarry Lile 		rif_len = TR_RCF_RIFLEN(th->rcf);
767b149dd6cSLarry Lile 		if ((th->iso88025_shost[0] & TR_RII) &&
768b149dd6cSLarry Lile 		    (rif_len > 2)) {
76942fdfc12SKelly Yancey 			trld->trld_rcf = th->rcf;
77042fdfc12SKelly Yancey 			trld->trld_rcf ^= htons(TR_RCF_DIR);
77142fdfc12SKelly Yancey 			memcpy(trld->trld_route, th->rd, rif_len - 2);
77242fdfc12SKelly Yancey 			trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
773f9083fdbSLarry Lile 			/*
774f9083fdbSLarry Lile 			 * Set up source routing information for
775f9083fdbSLarry Lile 			 * reply packet (XXX)
776f9083fdbSLarry Lile 			 */
777b149dd6cSLarry Lile 			m->m_data -= rif_len;
778b149dd6cSLarry Lile 			m->m_len  += rif_len;
779b149dd6cSLarry Lile 			m->m_pkthdr.len += rif_len;
780fda82fc2SJulian Elischer 		} else {
781b149dd6cSLarry Lile 			th->iso88025_shost[0] &= ~TR_RII;
782c3a2190cSKelly Yancey 			trld->trld_rcf = 0;
783fcf11853SLarry Lile 		}
784fda82fc2SJulian Elischer 		m->m_data -= 8;
785fda82fc2SJulian Elischer 		m->m_len  += 8;
786fcf11853SLarry Lile 		m->m_pkthdr.len += 8;
78742fdfc12SKelly Yancey 		th->rcf = trld->trld_rcf;
788fda82fc2SJulian Elischer 	}
789df8bae1dSRodney W. Grimes 	if (rt->rt_expire)
790227ee8a1SPoul-Henning Kamp 		rt->rt_expire = time_second + arpt_keep;
791df8bae1dSRodney W. Grimes 	rt->rt_flags &= ~RTF_REJECT;
792022695f8SOrion Hodson 	la->la_asked = 0;
793022695f8SOrion Hodson 	la->la_preempt = arp_maxtries;
7941ed7bf1eSGleb Smirnoff 	hold = la->la_hold;
7951ed7bf1eSGleb Smirnoff 	la->la_hold = NULL;
7961ed7bf1eSGleb Smirnoff 	RT_UNLOCK(rt);
7971ed7bf1eSGleb Smirnoff 	if (hold != NULL)
7981ed7bf1eSGleb Smirnoff 		(*ifp->if_output)(ifp, hold, rt_key(rt), rt);
7991ed7bf1eSGleb Smirnoff 
800df8bae1dSRodney W. Grimes reply:
801b2a8ac7cSLuigi Rizzo 	if (op != ARPOP_REQUEST)
802b2a8ac7cSLuigi Rizzo 		goto drop;
803df8bae1dSRodney W. Grimes 	if (itaddr.s_addr == myaddr.s_addr) {
804df8bae1dSRodney W. Grimes 		/* I am the target */
805322dcb8dSMax Khon 		(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
806a9771948SGleb Smirnoff 		(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
807df8bae1dSRodney W. Grimes 	} else {
8081ed7bf1eSGleb Smirnoff 		rt = arplookup(itaddr.s_addr, 0, SIN_PROXY);
8091ed7bf1eSGleb Smirnoff 		if (rt == NULL) {
81028e82295SGarrett Wollman 			struct sockaddr_in sin;
81128e82295SGarrett Wollman 
812b2a8ac7cSLuigi Rizzo 			if (!arp_proxyall)
813b2a8ac7cSLuigi Rizzo 				goto drop;
81428e82295SGarrett Wollman 
81528e82295SGarrett Wollman 			bzero(&sin, sizeof sin);
81628e82295SGarrett Wollman 			sin.sin_family = AF_INET;
81728e82295SGarrett Wollman 			sin.sin_len = sizeof sin;
81828e82295SGarrett Wollman 			sin.sin_addr = itaddr;
81928e82295SGarrett Wollman 
82031246bc2SGarrett Wollman 			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
821b2a8ac7cSLuigi Rizzo 			if (!rt)
822b2a8ac7cSLuigi Rizzo 				goto drop;
82328e82295SGarrett Wollman 			/*
82428e82295SGarrett Wollman 			 * Don't send proxies for nodes on the same interface
82528e82295SGarrett Wollman 			 * as this one came out of, or we'll get into a fight
82628e82295SGarrett Wollman 			 * over who claims what Ether address.
82728e82295SGarrett Wollman 			 */
828322dcb8dSMax Khon 			if (rt->rt_ifp == ifp) {
82928e82295SGarrett Wollman 				rtfree(rt);
830b2a8ac7cSLuigi Rizzo 				goto drop;
83128e82295SGarrett Wollman 			}
832322dcb8dSMax Khon 			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
833a9771948SGleb Smirnoff 			(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
83428e82295SGarrett Wollman 			rtfree(rt);
835cc728227SDavid Malone 
836cc728227SDavid Malone 			/*
837cc728227SDavid Malone 			 * Also check that the node which sent the ARP packet
838cc728227SDavid Malone 			 * is on the the interface we expect it to be on. This
839cc728227SDavid Malone 			 * avoids ARP chaos if an interface is connected to the
840cc728227SDavid Malone 			 * wrong network.
841cc728227SDavid Malone 			 */
842cc728227SDavid Malone 			sin.sin_addr = isaddr;
843cc728227SDavid Malone 
844cc728227SDavid Malone 			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
845b2a8ac7cSLuigi Rizzo 			if (!rt)
846b2a8ac7cSLuigi Rizzo 				goto drop;
847322dcb8dSMax Khon 			if (rt->rt_ifp != ifp) {
848cc728227SDavid Malone 				log(LOG_INFO, "arp_proxy: ignoring request"
8499bf40edeSBrooks Davis 				    " from %s via %s, expecting %s\n",
8509bf40edeSBrooks Davis 				    inet_ntoa(isaddr), ifp->if_xname,
8519bf40edeSBrooks Davis 				    rt->rt_ifp->if_xname);
852cc728227SDavid Malone 				rtfree(rt);
853b2a8ac7cSLuigi Rizzo 				goto drop;
854cc728227SDavid Malone 			}
855cc728227SDavid Malone 			rtfree(rt);
856cc728227SDavid Malone 
857ac234f93SGarrett Wollman #ifdef DEBUG_PROXY
858ac234f93SGarrett Wollman 			printf("arp: proxying for %s\n",
859ef0cdf33SGarrett Wollman 			       inet_ntoa(itaddr));
860ac234f93SGarrett Wollman #endif
86128e82295SGarrett Wollman 		} else {
862510b360fSGleb Smirnoff 			/*
863510b360fSGleb Smirnoff 			 * Return proxied ARP replies only on the interface
864510b360fSGleb Smirnoff 			 * where this network resides. Otherwise we may
865510b360fSGleb Smirnoff 			 * conflict with the host we are proxying for.
866510b360fSGleb Smirnoff 			 */
867510b360fSGleb Smirnoff 			if (rt->rt_ifp != ifp) {
868510b360fSGleb Smirnoff 				RT_UNLOCK(rt);
869510b360fSGleb Smirnoff 				goto drop;
870510b360fSGleb Smirnoff 			}
871df8bae1dSRodney W. Grimes 			sdl = SDL(rt->rt_gateway);
8721ed7bf1eSGleb Smirnoff 			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
873322dcb8dSMax Khon 			(void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
8741ed7bf1eSGleb Smirnoff 			RT_UNLOCK(rt);
87528e82295SGarrett Wollman 		}
876df8bae1dSRodney W. Grimes 	}
877df8bae1dSRodney W. Grimes 
878322dcb8dSMax Khon 	(void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
879322dcb8dSMax Khon 	(void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
880322dcb8dSMax Khon 	ah->ar_op = htons(ARPOP_REPLY);
881322dcb8dSMax Khon 	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
88264bf80ceSMatthew N. Dodd 	m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
88364bf80ceSMatthew N. Dodd 	m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
88464bf80ceSMatthew N. Dodd 	m->m_pkthdr.len = m->m_len;
88564bf80ceSMatthew N. Dodd 	sa.sa_family = AF_ARP;
88664bf80ceSMatthew N. Dodd 	sa.sa_len = 2;
887322dcb8dSMax Khon 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
888df8bae1dSRodney W. Grimes 	return;
889b2a8ac7cSLuigi Rizzo 
890b2a8ac7cSLuigi Rizzo drop:
891b2a8ac7cSLuigi Rizzo 	m_freem(m);
892df8bae1dSRodney W. Grimes }
8931d5e9e22SEivind Eklund #endif
894df8bae1dSRodney W. Grimes 
895df8bae1dSRodney W. Grimes /*
896df8bae1dSRodney W. Grimes  * Lookup or enter a new address in arptab.
897df8bae1dSRodney W. Grimes  */
8981ed7bf1eSGleb Smirnoff static struct rtentry *
899df8bae1dSRodney W. Grimes arplookup(addr, create, proxy)
900df8bae1dSRodney W. Grimes 	u_long addr;
901df8bae1dSRodney W. Grimes 	int create, proxy;
902df8bae1dSRodney W. Grimes {
903e952fa39SMatthew N. Dodd 	struct rtentry *rt;
904d1dd20beSSam Leffler 	struct sockaddr_inarp sin;
905ac234f93SGarrett Wollman 	const char *why = 0;
906df8bae1dSRodney W. Grimes 
907d1dd20beSSam Leffler 	bzero(&sin, sizeof(sin));
908d1dd20beSSam Leffler 	sin.sin_len = sizeof(sin);
909d1dd20beSSam Leffler 	sin.sin_family = AF_INET;
910df8bae1dSRodney W. Grimes 	sin.sin_addr.s_addr = addr;
911d1dd20beSSam Leffler 	if (proxy)
912d1dd20beSSam Leffler 		sin.sin_other = SIN_PROXY;
91331246bc2SGarrett Wollman 	rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
914df8bae1dSRodney W. Grimes 	if (rt == 0)
915df8bae1dSRodney W. Grimes 		return (0);
916ac234f93SGarrett Wollman 
917ac234f93SGarrett Wollman 	if (rt->rt_flags & RTF_GATEWAY)
918ac234f93SGarrett Wollman 		why = "host is not on local network";
919ac234f93SGarrett Wollman 	else if ((rt->rt_flags & RTF_LLINFO) == 0)
920ac234f93SGarrett Wollman 		why = "could not allocate llinfo";
921ac234f93SGarrett Wollman 	else if (rt->rt_gateway->sa_family != AF_LINK)
922ac234f93SGarrett Wollman 		why = "gateway route is not ours";
923ac234f93SGarrett Wollman 
924fedf1d01SBruce M Simpson 	if (why) {
925d1dd20beSSam Leffler #define	ISDYNCLONE(_rt) \
926d1dd20beSSam Leffler 	(((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED)
927d1dd20beSSam Leffler 		if (create)
928ac234f93SGarrett Wollman 			log(LOG_DEBUG, "arplookup %s failed: %s\n",
929ef0cdf33SGarrett Wollman 			    inet_ntoa(sin.sin_addr), why);
930b75bead1SBruce M Simpson 		/*
931b75bead1SBruce M Simpson 		 * If there are no references to this Layer 2 route,
932b75bead1SBruce M Simpson 		 * and it is a cloned route, and not static, and
933b75bead1SBruce M Simpson 		 * arplookup() is creating the route, then purge
934b75bead1SBruce M Simpson 		 * it from the routing table as it is probably bogus.
935b75bead1SBruce M Simpson 		 */
9369c63e9dbSSam Leffler 		if (rt->rt_refcnt == 1 && ISDYNCLONE(rt))
9379c63e9dbSSam Leffler 			rtexpunge(rt);
9389c63e9dbSSam Leffler 		RTFREE_LOCKED(rt);
939fedf1d01SBruce M Simpson 		return (0);
940d1dd20beSSam Leffler #undef ISDYNCLONE
941d1dd20beSSam Leffler 	} else {
9427138d65cSSam Leffler 		RT_REMREF(rt);
9431ed7bf1eSGleb Smirnoff 		return (rt);
944df8bae1dSRodney W. Grimes 	}
945d1dd20beSSam Leffler }
946df8bae1dSRodney W. Grimes 
947dd2e4102SGarrett Wollman void
948322dcb8dSMax Khon arp_ifinit(ifp, ifa)
949322dcb8dSMax Khon 	struct ifnet *ifp;
950dd2e4102SGarrett Wollman 	struct ifaddr *ifa;
951dd2e4102SGarrett Wollman {
952dd570d4dSTor Egge 	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
953322dcb8dSMax Khon 		arprequest(ifp, &IA_SIN(ifa)->sin_addr,
954322dcb8dSMax Khon 				&IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
955dd2e4102SGarrett Wollman 	ifa->ifa_rtrequest = arp_rtrequest;
956dd2e4102SGarrett Wollman 	ifa->ifa_flags |= RTF_CLONING;
957dd2e4102SGarrett Wollman }
958df5e1987SJonathan Lemon 
959a9771948SGleb Smirnoff void
960a9771948SGleb Smirnoff arp_ifinit2(ifp, ifa, enaddr)
961a9771948SGleb Smirnoff 	struct ifnet *ifp;
962a9771948SGleb Smirnoff 	struct ifaddr *ifa;
963a9771948SGleb Smirnoff 	u_char *enaddr;
964a9771948SGleb Smirnoff {
965a9771948SGleb Smirnoff 	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
966a9771948SGleb Smirnoff 		arprequest(ifp, &IA_SIN(ifa)->sin_addr,
967a9771948SGleb Smirnoff 				&IA_SIN(ifa)->sin_addr, enaddr);
968a9771948SGleb Smirnoff 	ifa->ifa_rtrequest = arp_rtrequest;
969a9771948SGleb Smirnoff 	ifa->ifa_flags |= RTF_CLONING;
970a9771948SGleb Smirnoff }
971a9771948SGleb Smirnoff 
972df5e1987SJonathan Lemon static void
973df5e1987SJonathan Lemon arp_init(void)
974df5e1987SJonathan Lemon {
975df5e1987SJonathan Lemon 
976df5e1987SJonathan Lemon 	arpintrq.ifq_maxlen = 50;
9776008862bSJohn Baldwin 	mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF);
978532cf61bSPeter Wemm 	LIST_INIT(&llinfo_arp);
979d1dd20beSSam Leffler 	callout_init(&arp_callout, CALLOUT_MPSAFE);
9807902224cSSam Leffler 	netisr_register(NETISR_ARP, arpintr, &arpintrq, NETISR_MPSAFE);
981cfff63f1SLuigi Rizzo 	callout_reset(&arp_callout, hz, arptimer, NULL);
982df5e1987SJonathan Lemon }
983df5e1987SJonathan Lemon SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);
984