xref: /freebsd/sys/netinet/if_ether.c (revision 74948aa6f34cf410d032164c77d84d3ed57a9c66)
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"
4019527d3eSRobert Watson #include "opt_mac.h"
41a9771948SGleb Smirnoff #include "opt_carp.h"
421d5e9e22SEivind Eklund 
43df8bae1dSRodney W. Grimes #include <sys/param.h>
44df8bae1dSRodney W. Grimes #include <sys/kernel.h>
45ce02431fSDoug Rabson #include <sys/queue.h>
46885f1aa4SPoul-Henning Kamp #include <sys/sysctl.h>
47885f1aa4SPoul-Henning Kamp #include <sys/systm.h>
4819527d3eSRobert Watson #include <sys/mac.h>
49885f1aa4SPoul-Henning Kamp #include <sys/mbuf.h>
50885f1aa4SPoul-Henning Kamp #include <sys/malloc.h>
514458ac71SBruce Evans #include <sys/socket.h>
52885f1aa4SPoul-Henning Kamp #include <sys/syslog.h>
53df8bae1dSRodney W. Grimes 
54df8bae1dSRodney W. Grimes #include <net/if.h>
55df8bae1dSRodney W. Grimes #include <net/if_dl.h>
56722012ccSJulian Elischer #include <net/if_types.h>
57df8bae1dSRodney W. Grimes #include <net/route.h>
58748e0b0aSGarrett Wollman #include <net/netisr.h>
59b149dd6cSLarry Lile #include <net/if_llc.h>
60c8f8e9c1SJulian Elischer #include <net/ethernet.h>
61df8bae1dSRodney W. Grimes 
62df8bae1dSRodney W. Grimes #include <netinet/in.h>
63df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
64df8bae1dSRodney W. Grimes #include <netinet/if_ether.h>
65df8bae1dSRodney W. Grimes 
66322dcb8dSMax Khon #include <net/if_arc.h>
67fda82fc2SJulian Elischer #include <net/iso88025.h>
68fda82fc2SJulian Elischer 
69a9771948SGleb Smirnoff #ifdef DEV_CARP
70a9771948SGleb Smirnoff #include <netinet/ip_carp.h>
71a9771948SGleb Smirnoff #endif
72a9771948SGleb Smirnoff 
73df8bae1dSRodney W. Grimes #define SIN(s) ((struct sockaddr_in *)s)
74df8bae1dSRodney W. Grimes #define SDL(s) ((struct sockaddr_dl *)s)
75df8bae1dSRodney W. Grimes 
76ce02431fSDoug Rabson SYSCTL_DECL(_net_link_ether);
77602d513cSGarrett Wollman SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
78df8bae1dSRodney W. Grimes 
79df8bae1dSRodney W. Grimes /* timer values */
80602d513cSGarrett Wollman static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
81602d513cSGarrett Wollman static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
82885f1aa4SPoul-Henning Kamp 
83602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
84bd2b686fSEd Maste 	   &arpt_prune, 0, "ARP table prune interval in seconds");
85602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
86bd2b686fSEd Maste 	   &arpt_keep, 0, "ARP entry lifetime in seconds");
87885f1aa4SPoul-Henning Kamp 
88df8bae1dSRodney W. Grimes #define	rt_expire rt_rmx.rmx_expire
89df8bae1dSRodney W. Grimes 
9066800f57SGarrett Wollman struct llinfo_arp {
91e3975643SJake Burkholder 	LIST_ENTRY(llinfo_arp) la_le;
9266800f57SGarrett Wollman 	struct	rtentry *la_rt;
9366800f57SGarrett Wollman 	struct	mbuf *la_hold;	/* last packet until resolved/timeout */
94022695f8SOrion Hodson 	u_short	la_preempt;	/* countdown for pre-expiry arps */
95e1ff74c5SGleb Smirnoff 	u_short	la_asked;	/* # requests sent */
9666800f57SGarrett Wollman };
9766800f57SGarrett Wollman 
98e3975643SJake Burkholder static	LIST_HEAD(, llinfo_arp) llinfo_arp;
99df8bae1dSRodney W. Grimes 
1001cafed39SJonathan Lemon static struct	ifqueue arpintrq;
101d1dd20beSSam Leffler static int	arp_allocated;
102df8bae1dSRodney W. Grimes 
103885f1aa4SPoul-Henning Kamp static int	arp_maxtries = 5;
104602d513cSGarrett Wollman static int	useloopback = 1; /* use loopback interface for local traffic */
105885f1aa4SPoul-Henning Kamp static int	arp_proxyall = 0;
106d1dd20beSSam Leffler static struct callout arp_callout;
107602d513cSGarrett Wollman 
108602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
109bd2b686fSEd Maste 	   &arp_maxtries, 0, "ARP resolution attempts before returning error");
110602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
111bd2b686fSEd Maste 	   &useloopback, 0, "Use the loopback interface for local traffic");
112602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
113bd2b686fSEd Maste 	   &arp_proxyall, 0, "Enable proxy ARP for all suitable requests");
114885f1aa4SPoul-Henning Kamp 
1154d77a549SAlfred Perlstein static void	arp_init(void);
1164d77a549SAlfred Perlstein static void	arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
1174d77a549SAlfred Perlstein static void	arprequest(struct ifnet *,
1184d77a549SAlfred Perlstein 			struct in_addr *, struct in_addr *, u_char *);
1191cafed39SJonathan Lemon static void	arpintr(struct mbuf *);
1204d77a549SAlfred Perlstein static void	arptimer(void *);
1211ed7bf1eSGleb Smirnoff static struct rtentry
1224d77a549SAlfred Perlstein 		*arplookup(u_long, int, int);
1231d5e9e22SEivind Eklund #ifdef INET
1244d77a549SAlfred Perlstein static void	in_arpinput(struct mbuf *);
1251d5e9e22SEivind Eklund #endif
12628e82295SGarrett Wollman 
127df8bae1dSRodney W. Grimes /*
128df8bae1dSRodney W. Grimes  * Timeout routine.  Age arp_tab entries periodically.
129df8bae1dSRodney W. Grimes  */
130df8bae1dSRodney W. Grimes /* ARGSUSED */
131df8bae1dSRodney W. Grimes static void
1321ed7bf1eSGleb Smirnoff arptimer(void * __unused unused)
133df8bae1dSRodney W. Grimes {
134c996428cSJeffrey Hsu 	struct llinfo_arp *la, *ola;
135df8bae1dSRodney W. Grimes 
13693f79889SJeffrey Hsu 	RADIX_NODE_HEAD_LOCK(rt_tables[AF_INET]);
1371ed7bf1eSGleb Smirnoff 	LIST_FOREACH_SAFE(la, &llinfo_arp, la_le, ola) {
138c996428cSJeffrey Hsu 		struct rtentry *rt = la->la_rt;
1391ed7bf1eSGleb Smirnoff 
1401ed7bf1eSGleb Smirnoff 		RT_LOCK(rt);
141fe53256dSAndre Oppermann 		if (rt->rt_expire && rt->rt_expire <= time_uptime) {
1421ed7bf1eSGleb Smirnoff 			struct sockaddr_dl *sdl = SDL(rt->rt_gateway);
1431ed7bf1eSGleb Smirnoff 
1441ed7bf1eSGleb Smirnoff 			KASSERT(sdl->sdl_family == AF_LINK, ("sdl_family %d",
1451ed7bf1eSGleb Smirnoff 			    sdl->sdl_family));
1461ed7bf1eSGleb Smirnoff 			if (rt->rt_refcnt > 1) {
1471ed7bf1eSGleb Smirnoff 				sdl->sdl_alen = 0;
1481ed7bf1eSGleb Smirnoff 				la->la_preempt = la->la_asked = 0;
1491ed7bf1eSGleb Smirnoff 				RT_UNLOCK(rt);
1501ed7bf1eSGleb Smirnoff 				continue;
1511ed7bf1eSGleb Smirnoff 			}
1521ed7bf1eSGleb Smirnoff 			RT_UNLOCK(rt);
1531ed7bf1eSGleb Smirnoff 			/*
1541ed7bf1eSGleb Smirnoff 			 * XXX: LIST_REMOVE() is deep inside rtrequest().
1551ed7bf1eSGleb Smirnoff 			 */
1561ed7bf1eSGleb Smirnoff 			rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0,
1571ed7bf1eSGleb Smirnoff 			    NULL);
1581ed7bf1eSGleb Smirnoff 			continue;
1591ed7bf1eSGleb Smirnoff 		}
1601ed7bf1eSGleb Smirnoff 		RT_UNLOCK(rt);
161df8bae1dSRodney W. Grimes 	}
16293f79889SJeffrey Hsu 	RADIX_NODE_HEAD_UNLOCK(rt_tables[AF_INET]);
163d1dd20beSSam Leffler 
164d1dd20beSSam Leffler 	callout_reset(&arp_callout, arpt_prune * hz, arptimer, NULL);
165df8bae1dSRodney W. Grimes }
166df8bae1dSRodney W. Grimes 
167df8bae1dSRodney W. Grimes /*
168df8bae1dSRodney W. Grimes  * Parallel to llc_rtrequest.
169df8bae1dSRodney W. Grimes  */
170b2774d00SGarrett Wollman static void
1718071913dSRuslan Ermilov arp_rtrequest(req, rt, info)
172df8bae1dSRodney W. Grimes 	int req;
173e952fa39SMatthew N. Dodd 	struct rtentry *rt;
1748071913dSRuslan Ermilov 	struct rt_addrinfo *info;
175df8bae1dSRodney W. Grimes {
176e952fa39SMatthew N. Dodd 	struct sockaddr *gate;
177e952fa39SMatthew N. Dodd 	struct llinfo_arp *la;
178df8bae1dSRodney W. Grimes 	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
179067a8babSMax Laier 	struct in_ifaddr *ia;
180067a8babSMax Laier 	struct ifaddr *ifa;
181df8bae1dSRodney W. Grimes 
182d1dd20beSSam Leffler 	RT_LOCK_ASSERT(rt);
183d1dd20beSSam Leffler 
184df8bae1dSRodney W. Grimes 	if (rt->rt_flags & RTF_GATEWAY)
185df8bae1dSRodney W. Grimes 		return;
186d1dd20beSSam Leffler 	gate = rt->rt_gateway;
187d1dd20beSSam Leffler 	la = (struct llinfo_arp *)rt->rt_llinfo;
188df8bae1dSRodney W. Grimes 	switch (req) {
189df8bae1dSRodney W. Grimes 
190df8bae1dSRodney W. Grimes 	case RTM_ADD:
191df8bae1dSRodney W. Grimes 		/*
192df8bae1dSRodney W. Grimes 		 * XXX: If this is a manually added route to interface
193df8bae1dSRodney W. Grimes 		 * such as older version of routed or gated might provide,
194df8bae1dSRodney W. Grimes 		 * restore cloning bit.
195df8bae1dSRodney W. Grimes 		 */
196df8bae1dSRodney W. Grimes 		if ((rt->rt_flags & RTF_HOST) == 0 &&
197d6fa5d28SBruce M Simpson 		    rt_mask(rt) != NULL &&
198df8bae1dSRodney W. Grimes 		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
199df8bae1dSRodney W. Grimes 			rt->rt_flags |= RTF_CLONING;
200df8bae1dSRodney W. Grimes 		if (rt->rt_flags & RTF_CLONING) {
201df8bae1dSRodney W. Grimes 			/*
202df8bae1dSRodney W. Grimes 			 * Case 1: This route should come from a route to iface.
203df8bae1dSRodney W. Grimes 			 */
204df8bae1dSRodney W. Grimes 			rt_setgate(rt, rt_key(rt),
205df8bae1dSRodney W. Grimes 					(struct sockaddr *)&null_sdl);
206df8bae1dSRodney W. Grimes 			gate = rt->rt_gateway;
207df8bae1dSRodney W. Grimes 			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
208df8bae1dSRodney W. Grimes 			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
209fe53256dSAndre Oppermann 			rt->rt_expire = time_uptime;
210df8bae1dSRodney W. Grimes 			break;
211df8bae1dSRodney W. Grimes 		}
212df8bae1dSRodney W. Grimes 		/* Announce a new entry if requested. */
213df8bae1dSRodney W. Grimes 		if (rt->rt_flags & RTF_ANNOUNCE)
214322dcb8dSMax Khon 			arprequest(rt->rt_ifp,
215ed7509acSJulian Elischer 			    &SIN(rt_key(rt))->sin_addr,
216ed7509acSJulian Elischer 			    &SIN(rt_key(rt))->sin_addr,
217df8bae1dSRodney W. Grimes 			    (u_char *)LLADDR(SDL(gate)));
218df8bae1dSRodney W. Grimes 		/*FALLTHROUGH*/
219df8bae1dSRodney W. Grimes 	case RTM_RESOLVE:
220df8bae1dSRodney W. Grimes 		if (gate->sa_family != AF_LINK ||
221df8bae1dSRodney W. Grimes 		    gate->sa_len < sizeof(null_sdl)) {
222d1dd20beSSam Leffler 			log(LOG_DEBUG, "%s: bad gateway %s%s\n", __func__,
223beb2ced8SBruce M Simpson 			    inet_ntoa(SIN(rt_key(rt))->sin_addr),
224beb2ced8SBruce M Simpson 			    (gate->sa_family != AF_LINK) ?
225c3b52d64SBruce M Simpson 			    " (!AF_LINK)": "");
226df8bae1dSRodney W. Grimes 			break;
227df8bae1dSRodney W. Grimes 		}
228df8bae1dSRodney W. Grimes 		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
229df8bae1dSRodney W. Grimes 		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
230df8bae1dSRodney W. Grimes 		if (la != 0)
231df8bae1dSRodney W. Grimes 			break; /* This happens on a route change */
232df8bae1dSRodney W. Grimes 		/*
233df8bae1dSRodney W. Grimes 		 * Case 2:  This route may come from cloning, or a manual route
234df8bae1dSRodney W. Grimes 		 * add with a LL address.
235df8bae1dSRodney W. Grimes 		 */
236d1dd20beSSam Leffler 		R_Zalloc(la, struct llinfo_arp *, sizeof(*la));
237df8bae1dSRodney W. Grimes 		rt->rt_llinfo = (caddr_t)la;
238df8bae1dSRodney W. Grimes 		if (la == 0) {
239d1dd20beSSam Leffler 			log(LOG_DEBUG, "%s: malloc failed\n", __func__);
240df8bae1dSRodney W. Grimes 			break;
241df8bae1dSRodney W. Grimes 		}
242d1dd20beSSam Leffler 		arp_allocated++;
2431ed7bf1eSGleb Smirnoff 		/*
2441ed7bf1eSGleb Smirnoff 		 * We are storing a route entry outside of radix tree. So,
2451ed7bf1eSGleb Smirnoff 		 * it can be found and accessed by other means than radix
2461ed7bf1eSGleb Smirnoff 		 * lookup. The routing code assumes that any rtentry detached
2471ed7bf1eSGleb Smirnoff 		 * from radix can be destroyed safely. To prevent this, we
2481ed7bf1eSGleb Smirnoff 		 * add an additional reference.
2491ed7bf1eSGleb Smirnoff 		 */
2501ed7bf1eSGleb Smirnoff 		RT_ADDREF(rt);
251df8bae1dSRodney W. Grimes 		la->la_rt = rt;
252df8bae1dSRodney W. Grimes 		rt->rt_flags |= RTF_LLINFO;
25393f79889SJeffrey Hsu 		RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]);
25466800f57SGarrett Wollman 		LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
255cbb0b46aSGarrett Wollman 
2561d5e9e22SEivind Eklund #ifdef INET
257cbb0b46aSGarrett Wollman 		/*
258cbb0b46aSGarrett Wollman 		 * This keeps the multicast addresses from showing up
259cbb0b46aSGarrett Wollman 		 * in `arp -a' listings as unresolved.  It's not actually
260cbb0b46aSGarrett Wollman 		 * functional.  Then the same for broadcast.
261cbb0b46aSGarrett Wollman 		 */
262c448c89cSJonathan Lemon 		if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) &&
263c448c89cSJonathan Lemon 		    rt->rt_ifp->if_type != IFT_ARCNET) {
264cbb0b46aSGarrett Wollman 			ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
265cbb0b46aSGarrett Wollman 					       LLADDR(SDL(gate)));
266cbb0b46aSGarrett Wollman 			SDL(gate)->sdl_alen = 6;
26751a109a1SPeter Wemm 			rt->rt_expire = 0;
268cbb0b46aSGarrett Wollman 		}
269cbb0b46aSGarrett Wollman 		if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
270322dcb8dSMax Khon 			memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
271322dcb8dSMax Khon 			       rt->rt_ifp->if_addrlen);
272322dcb8dSMax Khon 			SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
27351a109a1SPeter Wemm 			rt->rt_expire = 0;
274cbb0b46aSGarrett Wollman 		}
2751d5e9e22SEivind Eklund #endif
276cbb0b46aSGarrett Wollman 
277067a8babSMax Laier 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
278067a8babSMax Laier 			if (ia->ia_ifp == rt->rt_ifp &&
279067a8babSMax Laier 			    SIN(rt_key(rt))->sin_addr.s_addr ==
280067a8babSMax Laier 			    (IA_SIN(ia))->sin_addr.s_addr)
281067a8babSMax Laier 				break;
282067a8babSMax Laier 		}
283067a8babSMax Laier 		if (ia) {
284df8bae1dSRodney W. Grimes 		    /*
285df8bae1dSRodney W. Grimes 		     * This test used to be
286df8bae1dSRodney W. Grimes 		     *	if (loif.if_flags & IFF_UP)
287df8bae1dSRodney W. Grimes 		     * It allowed local traffic to be forced
288df8bae1dSRodney W. Grimes 		     * through the hardware by configuring the loopback down.
289df8bae1dSRodney W. Grimes 		     * However, it causes problems during network configuration
290df8bae1dSRodney W. Grimes 		     * for boards that can't receive packets they send.
291df8bae1dSRodney W. Grimes 		     * It is now necessary to clear "useloopback" and remove
292df8bae1dSRodney W. Grimes 		     * the route to force traffic out to the hardware.
293df8bae1dSRodney W. Grimes 		     */
294df8bae1dSRodney W. Grimes 			rt->rt_expire = 0;
295ac912b2dSLuigi Rizzo 			bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
296322dcb8dSMax Khon 			      SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
297df8bae1dSRodney W. Grimes 			if (useloopback)
298f5fea3ddSPaul Traina 				rt->rt_ifp = loif;
299df8bae1dSRodney W. Grimes 
300067a8babSMax Laier 		    /*
301067a8babSMax Laier 		     * make sure to set rt->rt_ifa to the interface
302067a8babSMax Laier 		     * address we are using, otherwise we will have trouble
303067a8babSMax Laier 		     * with source address selection.
304067a8babSMax Laier 		     */
305067a8babSMax Laier 			ifa = &ia->ia_ifa;
306067a8babSMax Laier 			if (ifa != rt->rt_ifa) {
307067a8babSMax Laier 				IFAFREE(rt->rt_ifa);
308067a8babSMax Laier 				IFAREF(ifa);
309067a8babSMax Laier 				rt->rt_ifa = ifa;
310067a8babSMax Laier 			}
311df8bae1dSRodney W. Grimes 		}
312df8bae1dSRodney W. Grimes 		break;
313df8bae1dSRodney W. Grimes 
314df8bae1dSRodney W. Grimes 	case RTM_DELETE:
315df8bae1dSRodney W. Grimes 		if (la == 0)
316df8bae1dSRodney W. Grimes 			break;
31793f79889SJeffrey Hsu 		RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]);
31866800f57SGarrett Wollman 		LIST_REMOVE(la, la_le);
3191ed7bf1eSGleb Smirnoff 		RT_REMREF(rt);
320df8bae1dSRodney W. Grimes 		rt->rt_llinfo = 0;
321df8bae1dSRodney W. Grimes 		rt->rt_flags &= ~RTF_LLINFO;
322df8bae1dSRodney W. Grimes 		if (la->la_hold)
323df8bae1dSRodney W. Grimes 			m_freem(la->la_hold);
324df8bae1dSRodney W. Grimes 		Free((caddr_t)la);
325df8bae1dSRodney W. Grimes 	}
326df8bae1dSRodney W. Grimes }
327df8bae1dSRodney W. Grimes 
328df8bae1dSRodney W. Grimes /*
329df8bae1dSRodney W. Grimes  * Broadcast an ARP request. Caller specifies:
330df8bae1dSRodney W. Grimes  *	- arp header source ip address
331df8bae1dSRodney W. Grimes  *	- arp header target ip address
332df8bae1dSRodney W. Grimes  *	- arp header source ethernet address
333df8bae1dSRodney W. Grimes  */
334df8bae1dSRodney W. Grimes static void
335322dcb8dSMax Khon arprequest(ifp, sip, tip, enaddr)
336e952fa39SMatthew N. Dodd 	struct ifnet *ifp;
337e952fa39SMatthew N. Dodd 	struct in_addr *sip, *tip;
338e952fa39SMatthew N. Dodd 	u_char *enaddr;
339df8bae1dSRodney W. Grimes {
340e952fa39SMatthew N. Dodd 	struct mbuf *m;
341e952fa39SMatthew N. Dodd 	struct arphdr *ah;
342df8bae1dSRodney W. Grimes 	struct sockaddr sa;
343df8bae1dSRodney W. Grimes 
344a163d034SWarner Losh 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
345df8bae1dSRodney W. Grimes 		return;
34664bf80ceSMatthew N. Dodd 	m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
34764bf80ceSMatthew N. Dodd 		2*ifp->if_data.ifi_addrlen;
34864bf80ceSMatthew N. Dodd 	m->m_pkthdr.len = m->m_len;
34964bf80ceSMatthew N. Dodd 	MH_ALIGN(m, m->m_len);
35064bf80ceSMatthew N. Dodd 	ah = mtod(m, struct arphdr *);
35164bf80ceSMatthew N. Dodd 	bzero((caddr_t)ah, m->m_len);
35219527d3eSRobert Watson #ifdef MAC
35319527d3eSRobert Watson 	mac_create_mbuf_linklayer(ifp, m);
35419527d3eSRobert Watson #endif
355322dcb8dSMax Khon 	ah->ar_pro = htons(ETHERTYPE_IP);
356322dcb8dSMax Khon 	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
357322dcb8dSMax Khon 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
358322dcb8dSMax Khon 	ah->ar_op = htons(ARPOP_REQUEST);
35964bf80ceSMatthew N. Dodd 	bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
36064bf80ceSMatthew N. Dodd 	bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
36164bf80ceSMatthew N. Dodd 	bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
36264bf80ceSMatthew N. Dodd 	sa.sa_family = AF_ARP;
36364bf80ceSMatthew N. Dodd 	sa.sa_len = 2;
36464bf80ceSMatthew N. Dodd 	m->m_flags |= M_BCAST;
365322dcb8dSMax Khon 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
36664bf80ceSMatthew N. Dodd 
36764bf80ceSMatthew N. Dodd 	return;
368df8bae1dSRodney W. Grimes }
369df8bae1dSRodney W. Grimes 
370df8bae1dSRodney W. Grimes /*
371cd46a114SLuigi Rizzo  * Resolve an IP address into an ethernet address.
372cd46a114SLuigi Rizzo  * On input:
373cd46a114SLuigi Rizzo  *    ifp is the interface we use
374cd46a114SLuigi Rizzo  *    dst is the next hop,
375cd46a114SLuigi Rizzo  *    rt0 is the route to the final destination (possibly useless)
376cd46a114SLuigi Rizzo  *    m is the mbuf
377cd46a114SLuigi Rizzo  *    desten is where we want the address.
378cd46a114SLuigi Rizzo  *
379cd46a114SLuigi Rizzo  * On success, desten is filled in and the function returns 0;
380cd46a114SLuigi Rizzo  * If the packet must be held pending resolution, we return EWOULDBLOCK
381cd46a114SLuigi Rizzo  * On other errors, we return the corresponding error code.
382df8bae1dSRodney W. Grimes  */
383df8bae1dSRodney W. Grimes int
384cd46a114SLuigi Rizzo arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
385f7c5baa1SLuigi Rizzo 	struct sockaddr *dst, u_char *desten)
386df8bae1dSRodney W. Grimes {
3871ed7bf1eSGleb Smirnoff 	struct llinfo_arp *la = NULL;
3881ed7bf1eSGleb Smirnoff 	struct rtentry *rt = NULL;
389df8bae1dSRodney W. Grimes 	struct sockaddr_dl *sdl;
390cd46a114SLuigi Rizzo 	int error;
391df8bae1dSRodney W. Grimes 
392df8bae1dSRodney W. Grimes 	if (m->m_flags & M_BCAST) {	/* broadcast */
393322dcb8dSMax Khon 		(void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
394cd46a114SLuigi Rizzo 		return (0);
395df8bae1dSRodney W. Grimes 	}
396322dcb8dSMax Khon 	if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */
397df8bae1dSRodney W. Grimes 		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
398cd46a114SLuigi Rizzo 		return (0);
399df8bae1dSRodney W. Grimes 	}
4001ed7bf1eSGleb Smirnoff 
4011ed7bf1eSGleb Smirnoff 	if (rt0 != NULL) {
4021ed7bf1eSGleb Smirnoff 		error = rt_check(&rt, &rt0, dst);
4031ed7bf1eSGleb Smirnoff 		if (error) {
4041ed7bf1eSGleb Smirnoff 			m_freem(m);
4051ed7bf1eSGleb Smirnoff 			return error;
406df8bae1dSRodney W. Grimes 		}
4071ed7bf1eSGleb Smirnoff 		la = (struct llinfo_arp *)rt->rt_llinfo;
4081ed7bf1eSGleb Smirnoff 		if (la == NULL)
4091ed7bf1eSGleb Smirnoff 			RT_UNLOCK(rt);
4101ed7bf1eSGleb Smirnoff 	}
4111ed7bf1eSGleb Smirnoff 	if (la == NULL) {
4121ed7bf1eSGleb Smirnoff 		/*
4131ed7bf1eSGleb Smirnoff 		 * We enter this block in case if rt0 was NULL,
4141ed7bf1eSGleb Smirnoff 		 * or if rt found by rt_check() didn't have llinfo.
4151ed7bf1eSGleb Smirnoff 		 */
4161ed7bf1eSGleb Smirnoff 		rt = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
4171ed7bf1eSGleb Smirnoff 		if (rt == NULL) {
4181ed7bf1eSGleb Smirnoff 			log(LOG_DEBUG,
4191ed7bf1eSGleb Smirnoff 			    "arpresolve: can't allocate route for %s\n",
4201ed7bf1eSGleb Smirnoff 			    inet_ntoa(SIN(dst)->sin_addr));
421df8bae1dSRodney W. Grimes 			m_freem(m);
422cd46a114SLuigi Rizzo 			return (EINVAL); /* XXX */
423df8bae1dSRodney W. Grimes 		}
4241ed7bf1eSGleb Smirnoff 		la = (struct llinfo_arp *)rt->rt_llinfo;
4251ed7bf1eSGleb Smirnoff 		if (la == NULL) {
4261ed7bf1eSGleb Smirnoff 			RT_UNLOCK(rt);
4271ed7bf1eSGleb Smirnoff 			log(LOG_DEBUG,
4281ed7bf1eSGleb Smirnoff 			    "arpresolve: can't allocate llinfo for %s\n",
4291ed7bf1eSGleb Smirnoff 			    inet_ntoa(SIN(dst)->sin_addr));
4301ed7bf1eSGleb Smirnoff 			m_freem(m);
4311ed7bf1eSGleb Smirnoff 			return (EINVAL); /* XXX */
4321ed7bf1eSGleb Smirnoff 		}
4331ed7bf1eSGleb Smirnoff 	}
434df8bae1dSRodney W. Grimes 	sdl = SDL(rt->rt_gateway);
435df8bae1dSRodney W. Grimes 	/*
436df8bae1dSRodney W. Grimes 	 * Check the address family and length is valid, the address
437df8bae1dSRodney W. Grimes 	 * is resolved; otherwise, try to resolve.
438df8bae1dSRodney W. Grimes 	 */
439fe53256dSAndre Oppermann 	if ((rt->rt_expire == 0 || rt->rt_expire > time_uptime) &&
440df8bae1dSRodney W. Grimes 	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
441a20e2538SGleb Smirnoff 
442a20e2538SGleb Smirnoff 		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
443a20e2538SGleb Smirnoff 
444f0f3379eSOrion Hodson 		/*
445f0f3379eSOrion Hodson 		 * If entry has an expiry time and it is approaching,
446e1ff74c5SGleb Smirnoff 		 * send an ARP request.
447f0f3379eSOrion Hodson 		 */
448f0f3379eSOrion Hodson 		if ((rt->rt_expire != 0) &&
449fe53256dSAndre Oppermann 		    (time_uptime + la->la_preempt > rt->rt_expire)) {
450a20e2538SGleb Smirnoff 			struct in_addr sin =
451a20e2538SGleb Smirnoff 			    SIN(rt->rt_ifa->ifa_addr)->sin_addr;
452a20e2538SGleb Smirnoff 
453022695f8SOrion Hodson 			la->la_preempt--;
454a20e2538SGleb Smirnoff 			RT_UNLOCK(rt);
455a20e2538SGleb Smirnoff 			arprequest(ifp, &sin, &SIN(dst)->sin_addr,
456a20e2538SGleb Smirnoff 			    IF_LLADDR(ifp));
457a20e2538SGleb Smirnoff 			return (0);
458f0f3379eSOrion Hodson 		}
459f0f3379eSOrion Hodson 
4601ed7bf1eSGleb Smirnoff 		RT_UNLOCK(rt);
461cd46a114SLuigi Rizzo 		return (0);
462df8bae1dSRodney W. Grimes 	}
463df8bae1dSRodney W. Grimes 	/*
464deb62e28SRuslan Ermilov 	 * If ARP is disabled or static on this interface, stop.
46508aadfbbSJonathan Lemon 	 * XXX
46608aadfbbSJonathan Lemon 	 * Probably should not allocate empty llinfo struct if we are
46708aadfbbSJonathan Lemon 	 * not going to be sending out an arp request.
46808aadfbbSJonathan Lemon 	 */
469deb62e28SRuslan Ermilov 	if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
4701ed7bf1eSGleb Smirnoff 		RT_UNLOCK(rt);
47147891de1SRuslan Ermilov 		m_freem(m);
472cd46a114SLuigi Rizzo 		return (EINVAL);
47347891de1SRuslan Ermilov 	}
47408aadfbbSJonathan Lemon 	/*
475df8bae1dSRodney W. Grimes 	 * There is an arptab entry, but no ethernet address
476df8bae1dSRodney W. Grimes 	 * response yet.  Replace the held mbuf with this
477df8bae1dSRodney W. Grimes 	 * latest one.
478df8bae1dSRodney W. Grimes 	 */
479df8bae1dSRodney W. Grimes 	if (la->la_hold)
480df8bae1dSRodney W. Grimes 		m_freem(la->la_hold);
481df8bae1dSRodney W. Grimes 	la->la_hold = m;
482e1ff74c5SGleb Smirnoff 
483e1ff74c5SGleb Smirnoff 	KASSERT(rt->rt_expire > 0, ("sending ARP request for static entry"));
484e1ff74c5SGleb Smirnoff 
485e1ff74c5SGleb Smirnoff 	/*
486e1ff74c5SGleb Smirnoff 	 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
487e1ff74c5SGleb Smirnoff 	 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
488e1ff74c5SGleb Smirnoff 	 * if we have already sent arp_maxtries ARP requests. Retransmit the
489e1ff74c5SGleb Smirnoff 	 * ARP request, but not faster than one request per second.
490e1ff74c5SGleb Smirnoff 	 */
491e1ff74c5SGleb Smirnoff 	if (la->la_asked < arp_maxtries)
492e1ff74c5SGleb Smirnoff 		error = EWOULDBLOCK;	/* First request. */
493e1ff74c5SGleb Smirnoff 	else
494e1ff74c5SGleb Smirnoff 		error = (rt == rt0) ? EHOSTDOWN : EHOSTUNREACH;
495e1ff74c5SGleb Smirnoff 
496e1ff74c5SGleb Smirnoff 	if (la->la_asked++ == 0 || rt->rt_expire != time_uptime) {
497a20e2538SGleb Smirnoff 		struct in_addr sin =
498a20e2538SGleb Smirnoff 		    SIN(rt->rt_ifa->ifa_addr)->sin_addr;
499a20e2538SGleb Smirnoff 
500e1ff74c5SGleb Smirnoff 		rt->rt_expire = time_uptime;
501a20e2538SGleb Smirnoff 		RT_UNLOCK(rt);
502e1ff74c5SGleb Smirnoff 
503a20e2538SGleb Smirnoff 		arprequest(ifp, &sin, &SIN(dst)->sin_addr,
504322dcb8dSMax Khon 		    IF_LLADDR(ifp));
505e1ff74c5SGleb Smirnoff 	} else
5061ed7bf1eSGleb Smirnoff 		RT_UNLOCK(rt);
507e1ff74c5SGleb Smirnoff 
508e1ff74c5SGleb Smirnoff 	return (error);
509df8bae1dSRodney W. Grimes }
510df8bae1dSRodney W. Grimes 
511df8bae1dSRodney W. Grimes /*
512df8bae1dSRodney W. Grimes  * Common length and type checks are done here,
513df8bae1dSRodney W. Grimes  * then the protocol-specific routine is called.
514df8bae1dSRodney W. Grimes  */
515885f1aa4SPoul-Henning Kamp static void
5161cafed39SJonathan Lemon arpintr(struct mbuf *m)
517df8bae1dSRodney W. Grimes {
5181cafed39SJonathan Lemon 	struct arphdr *ar;
519df8bae1dSRodney W. Grimes 
52076ec7b2fSRobert Watson 	if (m->m_len < sizeof(struct arphdr) &&
52184365e2bSMatthew Dillon 	    ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
522e44d6283SJoerg Wunsch 		log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
5231cafed39SJonathan Lemon 		return;
52476ec7b2fSRobert Watson 	}
52576ec7b2fSRobert Watson 	ar = mtod(m, struct arphdr *);
52676ec7b2fSRobert Watson 
5271cafed39SJonathan Lemon 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER &&
5281cafed39SJonathan Lemon 	    ntohs(ar->ar_hrd) != ARPHRD_IEEE802 &&
529b8b33234SDoug Rabson 	    ntohs(ar->ar_hrd) != ARPHRD_ARCNET &&
530b8b33234SDoug Rabson 	    ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) {
5311cafed39SJonathan Lemon 		log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
53276ec7b2fSRobert Watson 		    (unsigned char *)&ar->ar_hrd, "");
53376ec7b2fSRobert Watson 		m_freem(m);
5341cafed39SJonathan Lemon 		return;
53576ec7b2fSRobert Watson 	}
53676ec7b2fSRobert Watson 
53731175791SRuslan Ermilov 	if (m->m_len < arphdr_len(ar)) {
53878e2d2bdSRuslan Ermilov 		if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
539f72d9d83SJoerg Wunsch 			log(LOG_ERR, "arp: runt packet\n");
54076ec7b2fSRobert Watson 			m_freem(m);
5411cafed39SJonathan Lemon 			return;
54276ec7b2fSRobert Watson 		}
54378e2d2bdSRuslan Ermilov 		ar = mtod(m, struct arphdr *);
54478e2d2bdSRuslan Ermilov 	}
545df8bae1dSRodney W. Grimes 
546df8bae1dSRodney W. Grimes 	switch (ntohs(ar->ar_pro)) {
5471d5e9e22SEivind Eklund #ifdef INET
548df8bae1dSRodney W. Grimes 	case ETHERTYPE_IP:
549df8bae1dSRodney W. Grimes 		in_arpinput(m);
5501cafed39SJonathan Lemon 		return;
5511d5e9e22SEivind Eklund #endif
552df8bae1dSRodney W. Grimes 	}
553df8bae1dSRodney W. Grimes 	m_freem(m);
554df8bae1dSRodney W. Grimes }
555df8bae1dSRodney W. Grimes 
5561d5e9e22SEivind Eklund #ifdef INET
557df8bae1dSRodney W. Grimes /*
558df8bae1dSRodney W. Grimes  * ARP for Internet protocols on 10 Mb/s Ethernet.
559df8bae1dSRodney W. Grimes  * Algorithm is that given in RFC 826.
560df8bae1dSRodney W. Grimes  * In addition, a sanity check is performed on the sender
561df8bae1dSRodney W. Grimes  * protocol address, to catch impersonators.
562df8bae1dSRodney W. Grimes  * We no longer handle negotiations for use of trailer protocol:
563df8bae1dSRodney W. Grimes  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
564df8bae1dSRodney W. Grimes  * along with IP replies if we wanted trailers sent to us,
565df8bae1dSRodney W. Grimes  * and also sent them in response to IP replies.
566df8bae1dSRodney W. Grimes  * This allowed either end to announce the desire to receive
567df8bae1dSRodney W. Grimes  * trailer packets.
568df8bae1dSRodney W. Grimes  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
569df8bae1dSRodney W. Grimes  * but formerly didn't normally send requests.
570df8bae1dSRodney W. Grimes  */
5713269187dSAlfred Perlstein static int log_arp_wrong_iface = 1;
572e3d123d6SAlfred Perlstein static int log_arp_movements = 1;
57339393906SGleb Smirnoff static int log_arp_permanent_modify = 1;
5743269187dSAlfred Perlstein 
5753269187dSAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
5763269187dSAlfred Perlstein 	&log_arp_wrong_iface, 0,
5773269187dSAlfred Perlstein 	"log arp packets arriving on the wrong interface");
578e3d123d6SAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
579e3d123d6SAlfred Perlstein         &log_arp_movements, 0,
58075ce3221SAlfred Perlstein         "log arp replies from MACs different than the one in the cache");
58139393906SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
58239393906SGleb Smirnoff         &log_arp_permanent_modify, 0,
58339393906SGleb Smirnoff         "log arp replies from MACs different than the one in the permanent arp entry");
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 
61074948aa6SAndrew Thompson 	if (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 	 */
682f69453caSAndrew Thompson 	if (!bridged && 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 {
72839393906SGleb Smirnoff 			RT_UNLOCK(rt);
72939393906SGleb Smirnoff 			if (log_arp_permanent_modify)
73039393906SGleb Smirnoff 				log(LOG_ERR, "arp: %*D attempts to modify "
73139393906SGleb Smirnoff 				    "permanent entry for %s on %s\n",
732322dcb8dSMax Khon 				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
7339bf40edeSBrooks Davis 				    inet_ntoa(isaddr), ifp->if_xname);
734dd9b6ddeSBill Fenner 			goto reply;
735dd9b6ddeSBill Fenner 		}
736dfd5dee1SPeter Wemm 	}
737322dcb8dSMax Khon 	/*
738322dcb8dSMax Khon 	 * sanity check for the address length.
739322dcb8dSMax Khon 	 * XXX this does not work for protocols with variable address
740322dcb8dSMax Khon 	 * length. -is
741322dcb8dSMax Khon 	 */
742322dcb8dSMax Khon 	if (sdl->sdl_alen &&
743322dcb8dSMax Khon 	    sdl->sdl_alen != ah->ar_hln) {
744322dcb8dSMax Khon 		log(LOG_WARNING,
745322dcb8dSMax Khon 		    "arp from %*D: new addr len %d, was %d",
746322dcb8dSMax Khon 		    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
747322dcb8dSMax Khon 		    ah->ar_hln, sdl->sdl_alen);
748322dcb8dSMax Khon 	}
749322dcb8dSMax Khon 	if (ifp->if_addrlen != ah->ar_hln) {
750322dcb8dSMax Khon 		log(LOG_WARNING,
751322dcb8dSMax Khon 		    "arp from %*D: addr len: new %d, i/f %d (ignored)",
752322dcb8dSMax Khon 		    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
753322dcb8dSMax Khon 		    ah->ar_hln, ifp->if_addrlen);
7541ed7bf1eSGleb Smirnoff 		RT_UNLOCK(rt);
755322dcb8dSMax Khon 		goto reply;
756322dcb8dSMax Khon 	}
757322dcb8dSMax Khon 	(void)memcpy(LLADDR(sdl), ar_sha(ah),
758322dcb8dSMax Khon 	    sdl->sdl_alen = ah->ar_hln);
759243c4c6dSEivind Eklund 	/*
760243c4c6dSEivind Eklund 	 * If we receive an arp from a token-ring station over
761243c4c6dSEivind Eklund 	 * a token-ring nic then try to save the source
762243c4c6dSEivind Eklund 	 * routing info.
763243c4c6dSEivind Eklund 	 */
764322dcb8dSMax Khon 	if (ifp->if_type == IFT_ISO88025) {
765fda82fc2SJulian Elischer 		th = (struct iso88025_header *)m->m_pkthdr.header;
76642fdfc12SKelly Yancey 		trld = SDL_ISO88025(sdl);
767b149dd6cSLarry Lile 		rif_len = TR_RCF_RIFLEN(th->rcf);
768b149dd6cSLarry Lile 		if ((th->iso88025_shost[0] & TR_RII) &&
769b149dd6cSLarry Lile 		    (rif_len > 2)) {
77042fdfc12SKelly Yancey 			trld->trld_rcf = th->rcf;
77142fdfc12SKelly Yancey 			trld->trld_rcf ^= htons(TR_RCF_DIR);
77242fdfc12SKelly Yancey 			memcpy(trld->trld_route, th->rd, rif_len - 2);
77342fdfc12SKelly Yancey 			trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
774f9083fdbSLarry Lile 			/*
775f9083fdbSLarry Lile 			 * Set up source routing information for
776f9083fdbSLarry Lile 			 * reply packet (XXX)
777f9083fdbSLarry Lile 			 */
778b149dd6cSLarry Lile 			m->m_data -= rif_len;
779b149dd6cSLarry Lile 			m->m_len  += rif_len;
780b149dd6cSLarry Lile 			m->m_pkthdr.len += rif_len;
781fda82fc2SJulian Elischer 		} else {
782b149dd6cSLarry Lile 			th->iso88025_shost[0] &= ~TR_RII;
783c3a2190cSKelly Yancey 			trld->trld_rcf = 0;
784fcf11853SLarry Lile 		}
785fda82fc2SJulian Elischer 		m->m_data -= 8;
786fda82fc2SJulian Elischer 		m->m_len  += 8;
787fcf11853SLarry Lile 		m->m_pkthdr.len += 8;
78842fdfc12SKelly Yancey 		th->rcf = trld->trld_rcf;
789fda82fc2SJulian Elischer 	}
790df8bae1dSRodney W. Grimes 	if (rt->rt_expire)
791fe53256dSAndre Oppermann 		rt->rt_expire = time_uptime + arpt_keep;
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