xref: /dflybsd-src/sys/netinet/if_ether.c (revision ea47786f593951711c20c91893bc1346804cd893)
1 /*
2  * Copyright (c) 2004, 2005 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Jeffrey M. Hsu.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of The DragonFly Project nor the names of its
16  *    contributors may be used to endorse or promote products derived
17  *    from this software without specific, prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1982, 1986, 1988, 1993
35  *	The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. All advertising materials mentioning features or use of this software
46  *    must display the following acknowledgement:
47  *	This product includes software developed by the University of
48  *	California, Berkeley and its contributors.
49  * 4. Neither the name of the University nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  *
65  *	@(#)if_ether.c	8.1 (Berkeley) 6/10/93
66  * $FreeBSD: src/sys/netinet/if_ether.c,v 1.64.2.23 2003/04/11 07:23:15 fjoe Exp $
67  * $DragonFly: src/sys/netinet/if_ether.c,v 1.59 2008/11/22 11:03:35 sephe Exp $
68  */
69 
70 /*
71  * Ethernet address resolution protocol.
72  * TODO:
73  *	add "inuse/lock" bit (or ref. count) along with valid bit
74  */
75 
76 #include "opt_inet.h"
77 #include "opt_carp.h"
78 
79 #include <sys/param.h>
80 #include <sys/kernel.h>
81 #include <sys/queue.h>
82 #include <sys/sysctl.h>
83 #include <sys/systm.h>
84 #include <sys/mbuf.h>
85 #include <sys/malloc.h>
86 #include <sys/socket.h>
87 #include <sys/syslog.h>
88 #include <sys/lock.h>
89 
90 #include <net/if.h>
91 #include <net/if_dl.h>
92 #include <net/if_types.h>
93 #include <net/route.h>
94 #include <net/netisr.h>
95 #include <net/if_llc.h>
96 
97 #include <netinet/in.h>
98 #include <netinet/in_var.h>
99 #include <netinet/if_ether.h>
100 
101 #include <sys/thread2.h>
102 #include <sys/msgport2.h>
103 #include <net/netmsg2.h>
104 #include <sys/mplock2.h>
105 
106 #ifdef CARP
107 #include <netinet/ip_carp.h>
108 #endif
109 
110 #define SIN(s) ((struct sockaddr_in *)s)
111 #define SDL(s) ((struct sockaddr_dl *)s)
112 
113 SYSCTL_DECL(_net_link_ether);
114 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
115 
116 /* timer values */
117 static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
118 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
119 static int arpt_down = 20;	/* once declared down, don't send for 20 sec */
120 
121 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
122 	   &arpt_prune, 0, "");
123 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
124 	   &arpt_keep, 0, "");
125 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
126 	   &arpt_down, 0, "");
127 
128 #define	rt_expire	rt_rmx.rmx_expire
129 
130 struct llinfo_arp {
131 	LIST_ENTRY(llinfo_arp) la_le;
132 	struct	rtentry *la_rt;
133 	struct	mbuf *la_hold;	/* last packet until resolved/timeout */
134 	struct	lwkt_port *la_msgport; /* last packet's msgport */
135 	u_short	la_preempt;	/* countdown for pre-expiry arps */
136 	u_short	la_asked;	/* #times we QUERIED following expiration */
137 };
138 
139 static	LIST_HEAD(, llinfo_arp) llinfo_arp_list[MAXCPU];
140 
141 static int	arp_maxtries = 5;
142 static int	useloopback = 1; /* use loopback interface for local traffic */
143 static int	arp_proxyall = 0;
144 static int	arp_refresh = 60; /* refresh arp cache ~60 (not impl yet) */
145 static int	arp_restricted_match = 0;
146 
147 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
148 	   &arp_maxtries, 0, "ARP resolution attempts before returning error");
149 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
150 	   &useloopback, 0, "Use the loopback interface for local traffic");
151 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
152 	   &arp_proxyall, 0, "Enable proxy ARP for all suitable requests");
153 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, restricted_match, CTLFLAG_RW,
154 	   &arp_restricted_match, 0, "Only match against the sender");
155 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, refresh, CTLFLAG_RW,
156 	   &arp_refresh, 0, "Preemptively refresh the ARP");
157 
158 static void	arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
159 static void	arprequest(struct ifnet *, const struct in_addr *,
160 			   const struct in_addr *, const u_char *);
161 static void	arprequest_async(struct ifnet *, const struct in_addr *,
162 				 const struct in_addr *, const u_char *);
163 static void	arpintr(netmsg_t msg);
164 static void	arptfree(struct llinfo_arp *);
165 static void	arptimer(void *);
166 static struct llinfo_arp *
167 		arplookup(in_addr_t, boolean_t, boolean_t, boolean_t);
168 #ifdef INET
169 static void	in_arpinput(struct mbuf *);
170 #endif
171 
172 static struct callout	arptimer_ch[MAXCPU];
173 
174 /*
175  * Timeout routine.  Age arp_tab entries periodically.
176  */
177 /* ARGSUSED */
178 static void
179 arptimer(void *ignored_arg)
180 {
181 	struct llinfo_arp *la, *nla;
182 
183 	crit_enter();
184 	LIST_FOREACH_MUTABLE(la, &llinfo_arp_list[mycpuid], la_le, nla) {
185 		if (la->la_rt->rt_expire && la->la_rt->rt_expire <= time_second)
186 			arptfree(la);
187 	}
188 	callout_reset(&arptimer_ch[mycpuid], arpt_prune * hz, arptimer, NULL);
189 	crit_exit();
190 }
191 
192 /*
193  * Parallel to llc_rtrequest.
194  */
195 static void
196 arp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
197 {
198 	struct sockaddr *gate = rt->rt_gateway;
199 	struct llinfo_arp *la = rt->rt_llinfo;
200 
201 	struct sockaddr_dl null_sdl = { sizeof null_sdl, AF_LINK };
202 	static boolean_t arpinit_done[MAXCPU];
203 
204 	if (!arpinit_done[mycpuid]) {
205 		arpinit_done[mycpuid] = TRUE;
206 		callout_init(&arptimer_ch[mycpuid]);
207 		callout_reset(&arptimer_ch[mycpuid], hz, arptimer, NULL);
208 	}
209 	if (rt->rt_flags & RTF_GATEWAY)
210 		return;
211 
212 	switch (req) {
213 	case RTM_ADD:
214 		/*
215 		 * XXX: If this is a manually added route to interface
216 		 * such as older version of routed or gated might provide,
217 		 * restore cloning bit.
218 		 */
219 		if (!(rt->rt_flags & RTF_HOST) &&
220 		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
221 			rt->rt_flags |= RTF_CLONING;
222 		if (rt->rt_flags & RTF_CLONING) {
223 			/*
224 			 * Case 1: This route should come from a route to iface.
225 			 */
226 			rt_setgate(rt, rt_key(rt),
227 				   (struct sockaddr *)&null_sdl,
228 				   RTL_DONTREPORT);
229 			gate = rt->rt_gateway;
230 			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
231 			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
232 			rt->rt_expire = time_second;
233 			break;
234 		}
235 		/* Announce a new entry if requested. */
236 		if (rt->rt_flags & RTF_ANNOUNCE) {
237 			arprequest_async(rt->rt_ifp,
238 			    &SIN(rt_key(rt))->sin_addr,
239 			    &SIN(rt_key(rt))->sin_addr,
240 			    LLADDR(SDL(gate)));
241 		}
242 		/*FALLTHROUGH*/
243 	case RTM_RESOLVE:
244 		if (gate->sa_family != AF_LINK ||
245 		    gate->sa_len < sizeof(struct sockaddr_dl)) {
246 			log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
247 			break;
248 		}
249 		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
250 		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
251 		if (la != NULL)
252 			break; /* This happens on a route change */
253 		/*
254 		 * Case 2:  This route may come from cloning, or a manual route
255 		 * add with a LL address.
256 		 */
257 		R_Malloc(la, struct llinfo_arp *, sizeof *la);
258 		rt->rt_llinfo = la;
259 		if (la == NULL) {
260 			log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
261 			break;
262 		}
263 		bzero(la, sizeof *la);
264 		la->la_rt = rt;
265 		rt->rt_flags |= RTF_LLINFO;
266 		LIST_INSERT_HEAD(&llinfo_arp_list[mycpuid], la, la_le);
267 
268 #ifdef INET
269 		/*
270 		 * This keeps the multicast addresses from showing up
271 		 * in `arp -a' listings as unresolved.  It's not actually
272 		 * functional.  Then the same for broadcast.
273 		 */
274 		if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
275 			ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
276 					       LLADDR(SDL(gate)));
277 			SDL(gate)->sdl_alen = 6;
278 			rt->rt_expire = 0;
279 		}
280 		if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
281 			memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
282 			       rt->rt_ifp->if_addrlen);
283 			SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
284 			rt->rt_expire = 0;
285 		}
286 #endif
287 
288 		if (SIN(rt_key(rt))->sin_addr.s_addr ==
289 		    (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
290 			/*
291 			 * This test used to be
292 			 *	if (loif.if_flags & IFF_UP)
293 			 * It allowed local traffic to be forced
294 			 * through the hardware by configuring the
295 			 * loopback down.  However, it causes problems
296 			 * during network configuration for boards
297 			 * that can't receive packets they send.  It
298 			 * is now necessary to clear "useloopback" and
299 			 * remove the route to force traffic out to
300 			 * the hardware.
301 			 */
302 			rt->rt_expire = 0;
303 			bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
304 			      SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
305 			if (useloopback)
306 				rt->rt_ifp = loif;
307 		}
308 		break;
309 
310 	case RTM_DELETE:
311 		if (la == NULL)
312 			break;
313 		LIST_REMOVE(la, la_le);
314 		rt->rt_llinfo = NULL;
315 		rt->rt_flags &= ~RTF_LLINFO;
316 		if (la->la_hold != NULL)
317 			m_freem(la->la_hold);
318 		Free(la);
319 		break;
320 	}
321 }
322 
323 static struct mbuf *
324 arpreq_alloc(struct ifnet *ifp, const struct in_addr *sip,
325 	     const struct in_addr *tip, const u_char *enaddr)
326 {
327 	struct mbuf *m;
328 	struct arphdr *ah;
329 	u_short ar_hrd;
330 
331 	if ((m = m_gethdr(MB_DONTWAIT, MT_DATA)) == NULL)
332 		return NULL;
333 	m->m_pkthdr.rcvif = NULL;
334 
335 	switch (ifp->if_type) {
336 	case IFT_ETHER:
337 		/*
338 		 * This may not be correct for types not explicitly
339 		 * listed, but this is our best guess
340 		 */
341 	default:
342 		ar_hrd = htons(ARPHRD_ETHER);
343 
344 		m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
345 		m->m_pkthdr.len = m->m_len;
346 		MH_ALIGN(m, m->m_len);
347 
348 		ah = mtod(m, struct arphdr *);
349 		break;
350 	}
351 
352 	ah->ar_hrd = ar_hrd;
353 	ah->ar_pro = htons(ETHERTYPE_IP);
354 	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
355 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
356 	ah->ar_op = htons(ARPOP_REQUEST);
357 	memcpy(ar_sha(ah), enaddr, ah->ar_hln);
358 	memset(ar_tha(ah), 0, ah->ar_hln);
359 	memcpy(ar_spa(ah), sip, ah->ar_pln);
360 	memcpy(ar_tpa(ah), tip, ah->ar_pln);
361 
362 	return m;
363 }
364 
365 static void
366 arpreq_send(struct ifnet *ifp, struct mbuf *m)
367 {
368 	struct sockaddr sa;
369 	struct ether_header *eh;
370 
371 	switch (ifp->if_type) {
372 	case IFT_ETHER:
373 		/*
374 		 * This may not be correct for types not explicitly
375 		 * listed, but this is our best guess
376 		 */
377 	default:
378 		eh = (struct ether_header *)sa.sa_data;
379 		/* if_output() will not swap */
380 		eh->ether_type = htons(ETHERTYPE_ARP);
381 		memcpy(eh->ether_dhost, ifp->if_broadcastaddr, ifp->if_addrlen);
382 		break;
383 	}
384 
385 	sa.sa_family = AF_UNSPEC;
386 	sa.sa_len = sizeof(sa);
387 	ifp->if_output(ifp, m, &sa, NULL);
388 }
389 
390 static void
391 arpreq_send_handler(netmsg_t msg)
392 {
393 	struct mbuf *m = msg->packet.nm_packet;
394 	struct ifnet *ifp = msg->lmsg.u.ms_resultp;
395 
396 	arpreq_send(ifp, m);
397 	/* nmsg was embedded in the mbuf, do not reply! */
398 }
399 
400 /*
401  * Broadcast an ARP request. Caller specifies:
402  *	- arp header source ip address
403  *	- arp header target ip address
404  *	- arp header source ethernet address
405  *
406  * NOTE: Caller MUST NOT hold ifp's serializer
407  */
408 static void
409 arprequest(struct ifnet *ifp, const struct in_addr *sip,
410 	   const struct in_addr *tip, const u_char *enaddr)
411 {
412 	struct mbuf *m;
413 
414 	if (enaddr == NULL) {
415 		if (ifp->if_bridge) {
416 			enaddr = IF_LLADDR(ether_bridge_interface(ifp));
417 		} else {
418 			enaddr = IF_LLADDR(ifp);
419 		}
420 	}
421 
422 	m = arpreq_alloc(ifp, sip, tip, enaddr);
423 	if (m == NULL)
424 		return;
425 	arpreq_send(ifp, m);
426 }
427 
428 /*
429  * Same as arprequest(), except:
430  * - Caller is allowed to hold ifp's serializer
431  * - Network output is done in protocol thead
432  */
433 static void
434 arprequest_async(struct ifnet *ifp, const struct in_addr *sip,
435 		 const struct in_addr *tip, const u_char *enaddr)
436 {
437 	struct mbuf *m;
438 	struct netmsg_packet *pmsg;
439 
440 	if (enaddr == NULL) {
441 		if (ifp->if_bridge) {
442 			enaddr = IF_LLADDR(ether_bridge_interface(ifp));
443 		} else {
444 			enaddr = IF_LLADDR(ifp);
445 		}
446 	}
447 	m = arpreq_alloc(ifp, sip, tip, enaddr);
448 	if (m == NULL)
449 		return;
450 
451 	pmsg = &m->m_hdr.mh_netmsg;
452 	netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
453 		    0, arpreq_send_handler);
454 	pmsg->nm_packet = m;
455 	pmsg->base.lmsg.u.ms_resultp = ifp;
456 
457 	lwkt_sendmsg(cpu_portfn(mycpuid), &pmsg->base.lmsg);
458 }
459 
460 /*
461  * Resolve an IP address into an ethernet address.  If success,
462  * desten is filled in.  If there is no entry in arptab,
463  * set one up and broadcast a request for the IP address.
464  * Hold onto this mbuf and resend it once the address
465  * is finally resolved.  A return value of 1 indicates
466  * that desten has been filled in and the packet should be sent
467  * normally; a 0 return indicates that the packet has been
468  * taken over here, either now or for later transmission.
469  */
470 int
471 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
472 	   struct sockaddr *dst, u_char *desten)
473 {
474 	struct rtentry *rt;
475 	struct llinfo_arp *la = NULL;
476 	struct sockaddr_dl *sdl;
477 
478 	if (m->m_flags & M_BCAST) {	/* broadcast */
479 		memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
480 		return (1);
481 	}
482 	if (m->m_flags & M_MCAST) {/* multicast */
483 		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
484 		return (1);
485 	}
486 	if (rt0 != NULL) {
487 		if (rt_llroute(dst, rt0, &rt) != 0) {
488 			m_freem(m);
489 			return 0;
490 		}
491 		la = rt->rt_llinfo;
492 	}
493 	if (la == NULL) {
494 		la = arplookup(SIN(dst)->sin_addr.s_addr,
495 			       TRUE, RTL_REPORTMSG, FALSE);
496 		if (la != NULL)
497 			rt = la->la_rt;
498 	}
499 	if (la == NULL || rt == NULL) {
500 		log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
501 		    inet_ntoa(SIN(dst)->sin_addr), la ? "la" : " ",
502 		    rt ? "rt" : "");
503 		m_freem(m);
504 		return (0);
505 	}
506 	sdl = SDL(rt->rt_gateway);
507 	/*
508 	 * Check the address family and length is valid, the address
509 	 * is resolved; otherwise, try to resolve.
510 	 */
511 	if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
512 	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
513 		/*
514 		 * If entry has an expiry time and it is approaching,
515 		 * see if we need to send an ARP request within this
516 		 * arpt_down interval.
517 		 */
518 		if ((rt->rt_expire != 0) &&
519 		    (time_second + la->la_preempt > rt->rt_expire)) {
520 			arprequest(ifp,
521 				   &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
522 				   &SIN(dst)->sin_addr,
523 				   NULL);
524 			la->la_preempt--;
525 		}
526 
527 		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
528 		return 1;
529 	}
530 	/*
531 	 * If ARP is disabled or static on this interface, stop.
532 	 * XXX
533 	 * Probably should not allocate empty llinfo struct if we are
534 	 * not going to be sending out an arp request.
535 	 */
536 	if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
537 		m_freem(m);
538 		return (0);
539 	}
540 	/*
541 	 * There is an arptab entry, but no ethernet address
542 	 * response yet.  Replace the held mbuf with this
543 	 * latest one.
544 	 */
545 	if (la->la_hold != NULL)
546 		m_freem(la->la_hold);
547 	la->la_hold = m;
548 	la->la_msgport = cur_netport();
549 	if (rt->rt_expire || ((rt->rt_flags & RTF_STATIC) && !sdl->sdl_alen)) {
550 		rt->rt_flags &= ~RTF_REJECT;
551 		if (la->la_asked == 0 || rt->rt_expire != time_second) {
552 			rt->rt_expire = time_second;
553 			if (la->la_asked++ < arp_maxtries) {
554 				arprequest(ifp,
555 					   &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
556 					   &SIN(dst)->sin_addr,
557 					   NULL);
558 			} else {
559 				rt->rt_flags |= RTF_REJECT;
560 				rt->rt_expire += arpt_down;
561 				la->la_asked = 0;
562 				la->la_preempt = arp_maxtries;
563 			}
564 		}
565 	}
566 	return (0);
567 }
568 
569 /*
570  * Common length and type checks are done here,
571  * then the protocol-specific routine is called.
572  */
573 static void
574 arpintr(netmsg_t msg)
575 {
576 	struct mbuf *m = msg->packet.nm_packet;
577 	struct arphdr *ar;
578 	u_short ar_hrd;
579 
580 	if (m->m_len < sizeof(struct arphdr) &&
581 	    (m = m_pullup(m, sizeof(struct arphdr))) == NULL) {
582 		log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
583 		return;
584 	}
585 	ar = mtod(m, struct arphdr *);
586 
587 	ar_hrd = ntohs(ar->ar_hrd);
588 	if (ar_hrd != ARPHRD_ETHER && ar_hrd != ARPHRD_IEEE802) {
589 		log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
590 		    (unsigned char *)&ar->ar_hrd, "");
591 		m_freem(m);
592 		return;
593 	}
594 
595 	if (m->m_pkthdr.len < arphdr_len(ar)) {
596 		if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
597 			log(LOG_ERR, "arp: runt packet\n");
598 			return;
599 		}
600 		ar = mtod(m, struct arphdr *);
601 	}
602 
603 	switch (ntohs(ar->ar_pro)) {
604 #ifdef INET
605 	case ETHERTYPE_IP:
606 		in_arpinput(m);
607 		return;
608 #endif
609 	}
610 	m_freem(m);
611 	/* msg was embedded in the mbuf, do not reply! */
612 }
613 
614 #ifdef INET
615 /*
616  * ARP for Internet protocols on 10 Mb/s Ethernet.
617  * Algorithm is that given in RFC 826.
618  * In addition, a sanity check is performed on the sender
619  * protocol address, to catch impersonators.
620  * We no longer handle negotiations for use of trailer protocol:
621  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
622  * along with IP replies if we wanted trailers sent to us,
623  * and also sent them in response to IP replies.
624  * This allowed either end to announce the desire to receive
625  * trailer packets.
626  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
627  * but formerly didn't normally send requests.
628  */
629 
630 static int	log_arp_wrong_iface = 1;
631 static int	log_arp_movements = 1;
632 static int	log_arp_permanent_modify = 1;
633 
634 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
635 	   &log_arp_wrong_iface, 0,
636 	   "Log arp packets arriving on the wrong interface");
637 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
638 	   &log_arp_movements, 0,
639 	   "Log arp replies from MACs different than the one in the cache");
640 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
641 	   &log_arp_permanent_modify, 0,
642 	   "Log arp replies from MACs different than the one "
643 	   "in the permanent arp entry");
644 
645 
646 static void
647 arp_hold_output(netmsg_t msg)
648 {
649 	struct mbuf *m = msg->packet.nm_packet;
650 	struct rtentry *rt;
651 	struct ifnet *ifp;
652 
653 	rt = msg->lmsg.u.ms_resultp;
654 	ifp = m->m_pkthdr.rcvif;
655 	m->m_pkthdr.rcvif = NULL;
656 
657 	ifp->if_output(ifp, m, rt_key(rt), rt);
658 
659 	/* Drop the reference count bumped by the sender */
660 	RTFREE(rt);
661 
662 	/* nmsg was embedded in the mbuf, do not reply! */
663 }
664 
665 static void
666 arp_update_oncpu(struct mbuf *m, in_addr_t saddr, boolean_t create,
667 		 boolean_t generate_report, boolean_t dologging)
668 {
669 	struct arphdr *ah = mtod(m, struct arphdr *);
670 	struct ifnet *ifp = m->m_pkthdr.rcvif;
671 	struct llinfo_arp *la;
672 	struct sockaddr_dl *sdl;
673 	struct rtentry *rt;
674 
675 	la = arplookup(saddr, create, generate_report, FALSE);
676 	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
677 		struct in_addr isaddr = { saddr };
678 
679 		/*
680 		 * Normally arps coming in on the wrong interface are ignored,
681 		 * but if we are bridging and the two interfaces belong to
682 		 * the same bridge, or one is a member of the bridge which
683 		 * is the other, then it isn't an error.
684 		 */
685 		if (rt->rt_ifp != ifp) {
686 			/*
687 			 * (1) ifp and rt_ifp both members of same bridge
688 			 * (2) rt_ifp member of bridge ifp
689 			 * (3) ifp member of bridge rt_ifp
690 			 *
691 			 * Always replace rt_ifp with the bridge ifc.
692 			 */
693 			struct ifnet *nifp;
694 
695 			if (ifp->if_bridge &&
696 			    rt->rt_ifp->if_bridge == ifp->if_bridge) {
697 				nifp = ether_bridge_interface(ifp);
698 			} else if (rt->rt_ifp->if_bridge &&
699 				   ether_bridge_interface(rt->rt_ifp) == ifp) {
700 				nifp = ifp;
701 			} else if (ifp->if_bridge &&
702 				   ether_bridge_interface(ifp) == rt->rt_ifp) {
703 				nifp = rt->rt_ifp;
704 			} else {
705 				nifp = NULL;
706 			}
707 
708 			if ((log_arp_wrong_iface == 1 && nifp == NULL) ||
709 			    log_arp_wrong_iface == 2) {
710 				log(LOG_ERR,
711 				    "arp: %s is on %s "
712 				    "but got reply from %*D on %s\n",
713 				    inet_ntoa(isaddr),
714 				    rt->rt_ifp->if_xname,
715 				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
716 				    ifp->if_xname);
717 			}
718 			if (nifp == NULL)
719 				return;
720 
721 			/*
722 			 * nifp is our man!  Replace rt_ifp and adjust
723 			 * the sdl.
724 			 */
725 			ifp = rt->rt_ifp = nifp;
726 			sdl->sdl_type = ifp->if_type;
727 			sdl->sdl_index = ifp->if_index;
728 		}
729 		if (sdl->sdl_alen &&
730 		    bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
731 			if (rt->rt_expire != 0) {
732 				if (dologging && log_arp_movements) {
733 			    		log(LOG_INFO,
734 			    		"arp: %s moved from %*D to %*D on %s\n",
735 			    		inet_ntoa(isaddr),
736 			    		ifp->if_addrlen, (u_char *)LLADDR(sdl),
737 			    		":", ifp->if_addrlen,
738 			    		(u_char *)ar_sha(ah), ":",
739 			    		ifp->if_xname);
740 				}
741 			} else {
742 				if (dologging && log_arp_permanent_modify) {
743 					log(LOG_ERR,
744 					"arp: %*D attempts to modify "
745 					"permanent entry for %s on %s\n",
746 					ifp->if_addrlen, (u_char *)ar_sha(ah),
747 					":", inet_ntoa(isaddr), ifp->if_xname);
748 				}
749 				return;
750 			}
751 		}
752 		/*
753 		 * sanity check for the address length.
754 		 * XXX this does not work for protocols with variable address
755 		 * length. -is
756 		 */
757 		if (dologging && sdl->sdl_alen && sdl->sdl_alen != ah->ar_hln) {
758 			log(LOG_WARNING,
759 			    "arp from %*D: new addr len %d, was %d",
760 			    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
761 			    ah->ar_hln, sdl->sdl_alen);
762 		}
763 		if (ifp->if_addrlen != ah->ar_hln) {
764 			if (dologging) {
765 				log(LOG_WARNING,
766 				"arp from %*D: addr len: new %d, i/f %d "
767 				"(ignored)",
768 				ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
769 				ah->ar_hln, ifp->if_addrlen);
770 			}
771 			return;
772 		}
773 		memcpy(LLADDR(sdl), ar_sha(ah), sdl->sdl_alen = ah->ar_hln);
774 		if (rt->rt_expire != 0) {
775 			rt->rt_expire = time_second + arpt_keep;
776 		}
777 		rt->rt_flags &= ~RTF_REJECT;
778 		la->la_asked = 0;
779 		la->la_preempt = arp_maxtries;
780 
781 		/*
782 		 * This particular cpu might have been holding an mbuf
783 		 * pending ARP resolution.  If so, transmit the mbuf now.
784 		 */
785 		if (la->la_hold != NULL) {
786 			struct mbuf *m = la->la_hold;
787 			struct lwkt_port *port = la->la_msgport;
788 			struct netmsg_packet *pmsg;
789 
790 			la->la_hold = NULL;
791 			la->la_msgport = NULL;
792 
793 			m_adj(m, sizeof(struct ether_header));
794 
795 			/*
796 			 * Make sure that this rtentry will not be freed
797 			 * before the packet is processed on the target
798 			 * msgport.  The reference count will be dropped
799 			 * in the handler associated with this packet.
800 			 */
801 			rt->rt_refcnt++;
802 
803 			pmsg = &m->m_hdr.mh_netmsg;
804 			netmsg_init(&pmsg->base, NULL,
805 				    &netisr_apanic_rport,
806 				    MSGF_PRIORITY, arp_hold_output);
807 			pmsg->nm_packet = m;
808 
809 			/* Record necessary information */
810 			m->m_pkthdr.rcvif = ifp;
811 			pmsg->base.lmsg.u.ms_resultp = rt;
812 
813 			lwkt_sendmsg(port, &pmsg->base.lmsg);
814 		}
815 	}
816 }
817 
818 #ifdef SMP
819 
820 struct netmsg_arp_update {
821 	struct netmsg_base base;
822 	struct mbuf	*m;
823 	in_addr_t	saddr;
824 	boolean_t	create;
825 };
826 
827 static void arp_update_msghandler(netmsg_t msg);
828 
829 #endif
830 
831 /*
832  * Called from arpintr() - this routine is run from a single cpu.
833  */
834 static void
835 in_arpinput(struct mbuf *m)
836 {
837 	struct arphdr *ah;
838 	struct ifnet *ifp = m->m_pkthdr.rcvif;
839 	struct ether_header *eh;
840 	struct rtentry *rt;
841 	struct ifaddr_container *ifac;
842 	struct in_ifaddr_container *iac;
843 	struct in_ifaddr *ia = NULL;
844 	struct sockaddr sa;
845 	struct in_addr isaddr, itaddr, myaddr;
846 #ifdef SMP
847 	struct netmsg_arp_update msg;
848 #endif
849 	uint8_t *enaddr = NULL;
850 	int op;
851 	int req_len;
852 
853 	req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
854 	if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
855 		log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
856 		return;
857 	}
858 
859 	ah = mtod(m, struct arphdr *);
860 	op = ntohs(ah->ar_op);
861 	memcpy(&isaddr, ar_spa(ah), sizeof isaddr);
862 	memcpy(&itaddr, ar_tpa(ah), sizeof itaddr);
863 
864 	myaddr.s_addr = INADDR_ANY;
865 #ifdef CARP
866 	if (ifp->if_carp != NULL) {
867 		get_mplock();
868 		if (ifp->if_carp != NULL &&
869 		    carp_iamatch(ifp->if_carp, &itaddr, &isaddr, &enaddr)) {
870 			rel_mplock();
871 			myaddr = itaddr;
872 			goto match;
873 		}
874 		rel_mplock();
875 	}
876 #endif
877 
878 	/*
879 	 * Check both target and sender IP addresses:
880 	 *
881 	 * If we receive the packet on the interface owning the address,
882 	 * then accept the address.
883 	 *
884 	 * For a bridge, we accept the address if the receive interface and
885 	 * the interface owning the address are on the same bridge, and
886 	 * use the bridge MAC as the is-at response.  The bridge will be
887 	 * responsible for handling the packet.
888 	 *
889 	 * (1) Check target IP against our local IPs
890 	 */
891 	LIST_FOREACH(iac, INADDR_HASH(itaddr.s_addr), ia_hash) {
892 		ia = iac->ia;
893 
894 		/* Skip all ia's which don't match */
895 		if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
896 			continue;
897 #ifdef CARP
898 		if (ia->ia_ifp->if_type == IFT_CARP)
899 			continue;
900 #endif
901 		if (ifp->if_bridge && ia->ia_ifp &&
902 		    ifp->if_bridge == ia->ia_ifp->if_bridge) {
903 			ifp = ether_bridge_interface(ifp);
904 			goto match;
905 		}
906 		if (ia->ia_ifp && ia->ia_ifp->if_bridge &&
907 		    ether_bridge_interface(ia->ia_ifp) == ifp) {
908 			goto match;
909 		}
910 		if (ifp->if_bridge && ether_bridge_interface(ifp) ==
911 		    ia->ia_ifp) {
912 			goto match;
913 		}
914 		if (ia->ia_ifp == ifp)
915 			goto match;
916 
917 	}
918 
919 	/*
920 	 * (2) Check sender IP against our local IPs
921 	 */
922 	LIST_FOREACH(iac, INADDR_HASH(isaddr.s_addr), ia_hash) {
923 		ia = iac->ia;
924 
925 		/* Skip all ia's which don't match */
926 		if (isaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
927 			continue;
928 #ifdef CARP
929 		if (ia->ia_ifp->if_type == IFT_CARP)
930 			continue;
931 #endif
932 		if (ifp->if_bridge && ia->ia_ifp &&
933 		    ifp->if_bridge == ia->ia_ifp->if_bridge) {
934 			ifp = ether_bridge_interface(ifp);
935 			goto match;
936 		}
937 		if (ia->ia_ifp && ia->ia_ifp->if_bridge &&
938 		    ether_bridge_interface(ia->ia_ifp) == ifp) {
939 			goto match;
940 		}
941 		if (ifp->if_bridge && ether_bridge_interface(ifp) ==
942 		    ia->ia_ifp) {
943 			goto match;
944 		}
945 
946 		if (ia->ia_ifp == ifp)
947 			goto match;
948 	}
949 
950 	/*
951 	 * No match, use the first inet address on the receive interface
952 	 * as a dummy address for the rest of the function.
953 	 */
954 	TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
955 		struct ifaddr *ifa = ifac->ifa;
956 
957 		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
958 			ia = ifatoia(ifa);
959 			goto match;
960 		}
961 	}
962 
963 	/*
964 	 * If we got here, we didn't find any suitable interface,
965 	 * so drop the packet.
966 	 */
967 	m_freem(m);
968 	return;
969 
970 match:
971 	if (!enaddr)
972 		enaddr = (uint8_t *)IF_LLADDR(ifp);
973 	if (myaddr.s_addr == INADDR_ANY)
974 		myaddr = ia->ia_addr.sin_addr;
975 	if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) {
976 		m_freem(m);	/* it's from me, ignore it. */
977 		return;
978 	}
979 	if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
980 		log(LOG_ERR,
981 		    "arp: link address is broadcast for IP address %s!\n",
982 		    inet_ntoa(isaddr));
983 		m_freem(m);
984 		return;
985 	}
986 	if (isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
987 		log(LOG_ERR,
988 		   "arp: %*D is using my IP address %s!\n",
989 		   ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
990 		   inet_ntoa(isaddr));
991 		itaddr = myaddr;
992 		goto reply;
993 	}
994 	if (ifp->if_flags & IFF_STATICARP)
995 		goto reply;
996 
997 	/*
998 	 * When arp_restricted_match is true and the ARP response is not
999 	 * specifically targetted to me, ignore it.  Otherwise the entry
1000 	 * timeout may be updated for an old MAC.
1001 	 */
1002 	if (arp_restricted_match && itaddr.s_addr != myaddr.s_addr) {
1003 		m_freem(m);
1004 		return;
1005 	}
1006 
1007 #ifdef SMP
1008 	netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1009 		    0, arp_update_msghandler);
1010 	msg.m = m;
1011 	msg.saddr = isaddr.s_addr;
1012 	msg.create = (itaddr.s_addr == myaddr.s_addr);
1013 	lwkt_domsg(rtable_portfn(0), &msg.base.lmsg, 0);
1014 #else
1015 	arp_update_oncpu(m, isaddr.s_addr, (itaddr.s_addr == myaddr.s_addr),
1016 			 RTL_REPORTMSG, TRUE);
1017 #endif
1018 reply:
1019 	if (op != ARPOP_REQUEST) {
1020 		m_freem(m);
1021 		return;
1022 	}
1023 	if (itaddr.s_addr == myaddr.s_addr) {
1024 		/* I am the target */
1025 		memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
1026 		memcpy(ar_sha(ah), enaddr, ah->ar_hln);
1027 	} else {
1028 		struct llinfo_arp *la;
1029 
1030 		la = arplookup(itaddr.s_addr, FALSE, RTL_DONTREPORT, SIN_PROXY);
1031 		if (la == NULL) {
1032 			struct sockaddr_in sin;
1033 
1034 			if (!arp_proxyall) {
1035 				m_freem(m);
1036 				return;
1037 			}
1038 
1039 			bzero(&sin, sizeof sin);
1040 			sin.sin_family = AF_INET;
1041 			sin.sin_len = sizeof sin;
1042 			sin.sin_addr = itaddr;
1043 
1044 			rt = rtpurelookup((struct sockaddr *)&sin);
1045 			if (rt == NULL) {
1046 				m_freem(m);
1047 				return;
1048 			}
1049 			--rt->rt_refcnt;
1050 			/*
1051 			 * Don't send proxies for nodes on the same interface
1052 			 * as this one came out of, or we'll get into a fight
1053 			 * over who claims what Ether address.
1054 			 */
1055 			if (rt->rt_ifp == ifp) {
1056 				m_freem(m);
1057 				return;
1058 			}
1059 			memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
1060 			memcpy(ar_sha(ah), enaddr, ah->ar_hln);
1061 #ifdef DEBUG_PROXY
1062 			kprintf("arp: proxying for %s\n", inet_ntoa(itaddr));
1063 #endif
1064 		} else {
1065 			struct sockaddr_dl *sdl;
1066 
1067 			rt = la->la_rt;
1068 			memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
1069 			sdl = SDL(rt->rt_gateway);
1070 			memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
1071 		}
1072 	}
1073 
1074 	memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
1075 	memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
1076 	ah->ar_op = htons(ARPOP_REPLY);
1077 	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
1078 	switch (ifp->if_type) {
1079 	case IFT_ETHER:
1080 		/*
1081 		 * May not be correct for types not explictly
1082 		 * listed, but it is our best guess.
1083 		 */
1084 	default:
1085 		eh = (struct ether_header *)sa.sa_data;
1086 		memcpy(eh->ether_dhost, ar_tha(ah), sizeof eh->ether_dhost);
1087 		eh->ether_type = htons(ETHERTYPE_ARP);
1088 		break;
1089 	}
1090 	sa.sa_family = AF_UNSPEC;
1091 	sa.sa_len = sizeof sa;
1092 	ifp->if_output(ifp, m, &sa, NULL);
1093 }
1094 
1095 #ifdef SMP
1096 
1097 static void
1098 arp_update_msghandler(netmsg_t msg)
1099 {
1100 	struct netmsg_arp_update *rmsg = (struct netmsg_arp_update *)msg;
1101 	int nextcpu;
1102 
1103 	/*
1104 	 * This message handler will be called on all of the CPUs,
1105 	 * however, we only need to generate rtmsg on CPU0.
1106 	 */
1107 	arp_update_oncpu(rmsg->m, rmsg->saddr, rmsg->create,
1108 			 mycpuid == 0 ? RTL_REPORTMSG : RTL_DONTREPORT,
1109 			 mycpuid == 0);
1110 
1111 	nextcpu = mycpuid + 1;
1112 	if (nextcpu < ncpus)
1113 		lwkt_forwardmsg(rtable_portfn(nextcpu), &rmsg->base.lmsg);
1114 	else
1115 		lwkt_replymsg(&rmsg->base.lmsg, 0);
1116 }
1117 
1118 #endif	/* SMP */
1119 
1120 #endif	/* INET */
1121 
1122 /*
1123  * Free an arp entry.  If the arp entry is actively referenced or represents
1124  * a static entry we only clear it back to an unresolved state, otherwise
1125  * we destroy the entry entirely.
1126  *
1127  * Note that static entries are created when route add ... -interface is used
1128  * to create an interface route to a (direct) destination.
1129  */
1130 static void
1131 arptfree(struct llinfo_arp *la)
1132 {
1133 	struct rtentry *rt = la->la_rt;
1134 	struct sockaddr_dl *sdl;
1135 
1136 	if (rt == NULL)
1137 		panic("arptfree");
1138 	sdl = SDL(rt->rt_gateway);
1139 	if (sdl != NULL &&
1140 	    ((rt->rt_refcnt > 0 && sdl->sdl_family == AF_LINK) ||
1141 	     (rt->rt_flags & RTF_STATIC))) {
1142 		sdl->sdl_alen = 0;
1143 		la->la_preempt = la->la_asked = 0;
1144 		rt->rt_flags &= ~RTF_REJECT;
1145 		return;
1146 	}
1147 	rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
1148 }
1149 
1150 /*
1151  * Lookup or enter a new address in arptab.
1152  */
1153 static struct llinfo_arp *
1154 arplookup(in_addr_t addr, boolean_t create, boolean_t generate_report,
1155 	  boolean_t proxy)
1156 {
1157 	struct rtentry *rt;
1158 	struct sockaddr_inarp sin = { sizeof sin, AF_INET };
1159 	const char *why = NULL;
1160 
1161 	sin.sin_addr.s_addr = addr;
1162 	sin.sin_other = proxy ? SIN_PROXY : 0;
1163 	if (create) {
1164 		rt = _rtlookup((struct sockaddr *)&sin,
1165 			       generate_report, RTL_DOCLONE);
1166 	} else {
1167 		rt = rtpurelookup((struct sockaddr *)&sin);
1168 	}
1169 	if (rt == NULL)
1170 		return (NULL);
1171 	rt->rt_refcnt--;
1172 
1173 	if (rt->rt_flags & RTF_GATEWAY)
1174 		why = "host is not on local network";
1175 	else if (!(rt->rt_flags & RTF_LLINFO))
1176 		why = "could not allocate llinfo";
1177 	else if (rt->rt_gateway->sa_family != AF_LINK)
1178 		why = "gateway route is not ours";
1179 
1180 	if (why) {
1181 		if (create) {
1182 			log(LOG_DEBUG, "arplookup %s failed: %s\n",
1183 			    inet_ntoa(sin.sin_addr), why);
1184 		}
1185 		if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_WASCLONED)) {
1186 			/* No references to this route.  Purge it. */
1187 			rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1188 				  rt_mask(rt), rt->rt_flags, NULL);
1189 		}
1190 		return (NULL);
1191 	}
1192 	return (rt->rt_llinfo);
1193 }
1194 
1195 void
1196 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1197 {
1198 	if (IA_SIN(ifa)->sin_addr.s_addr != INADDR_ANY) {
1199 		arprequest_async(ifp, &IA_SIN(ifa)->sin_addr,
1200 				 &IA_SIN(ifa)->sin_addr, NULL);
1201 	}
1202 	ifa->ifa_rtrequest = arp_rtrequest;
1203 	ifa->ifa_flags |= RTF_CLONING;
1204 }
1205 
1206 void
1207 arp_iainit(struct ifnet *ifp, const struct in_addr *addr, const u_char *enaddr)
1208 {
1209 	if (addr->s_addr != INADDR_ANY)
1210 		arprequest_async(ifp, addr, addr, enaddr);
1211 }
1212 
1213 static void
1214 arp_init(void)
1215 {
1216 	int cpu;
1217 
1218 	for (cpu = 0; cpu < ncpus2; cpu++)
1219 		LIST_INIT(&llinfo_arp_list[cpu]);
1220 
1221 	netisr_register(NETISR_ARP, arpintr, NULL);
1222 }
1223 
1224 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);
1225