xref: /dragonfly/sys/netinet/if_ether.c (revision 66e72101)
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  */
68 
69 /*
70  * Ethernet address resolution protocol.
71  * TODO:
72  *	add "inuse/lock" bit (or ref. count) along with valid bit
73  */
74 
75 #include "opt_inet.h"
76 #include "opt_carp.h"
77 
78 #include <sys/param.h>
79 #include <sys/kernel.h>
80 #include <sys/queue.h>
81 #include <sys/sysctl.h>
82 #include <sys/systm.h>
83 #include <sys/mbuf.h>
84 #include <sys/malloc.h>
85 #include <sys/socket.h>
86 #include <sys/syslog.h>
87 #include <sys/lock.h>
88 
89 #include <net/if.h>
90 #include <net/if_dl.h>
91 #include <net/if_types.h>
92 #include <net/route.h>
93 #include <net/netisr.h>
94 #include <net/if_llc.h>
95 
96 #include <netinet/in.h>
97 #include <netinet/in_var.h>
98 #include <netinet/if_ether.h>
99 
100 #include <sys/thread2.h>
101 #include <sys/msgport2.h>
102 #include <net/netmsg2.h>
103 #include <net/netisr2.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 static void	in_arpreply(struct mbuf *m, in_addr_t, in_addr_t);
171 static void	arp_update_msghandler(netmsg_t);
172 static void	arp_reply_msghandler(netmsg_t);
173 #endif
174 
175 static struct callout	arptimer_ch[MAXCPU];
176 
177 /*
178  * Timeout routine.  Age arp_tab entries periodically.
179  */
180 /* ARGSUSED */
181 static void
182 arptimer(void *ignored_arg)
183 {
184 	struct llinfo_arp *la, *nla;
185 
186 	crit_enter();
187 	LIST_FOREACH_MUTABLE(la, &llinfo_arp_list[mycpuid], la_le, nla) {
188 		if (la->la_rt->rt_expire && la->la_rt->rt_expire <= time_second)
189 			arptfree(la);
190 	}
191 	callout_reset(&arptimer_ch[mycpuid], arpt_prune * hz, arptimer, NULL);
192 	crit_exit();
193 }
194 
195 /*
196  * Parallel to llc_rtrequest.
197  *
198  * Called after a route is successfully added to the tree to fix-up the
199  * route and initiate arp operations if required.
200  */
201 static void
202 arp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
203 {
204 	struct sockaddr *gate = rt->rt_gateway;
205 	struct llinfo_arp *la = rt->rt_llinfo;
206 
207 	struct sockaddr_dl null_sdl = { sizeof null_sdl, AF_LINK };
208 	static boolean_t arpinit_done[MAXCPU];
209 
210 	if (!arpinit_done[mycpuid]) {
211 		arpinit_done[mycpuid] = TRUE;
212 		callout_init(&arptimer_ch[mycpuid]);
213 		callout_reset(&arptimer_ch[mycpuid], hz, arptimer, NULL);
214 	}
215 	if (rt->rt_flags & RTF_GATEWAY)
216 		return;
217 
218 	switch (req) {
219 	case RTM_ADD:
220 		/*
221 		 * XXX: If this is a manually added route to interface
222 		 * such as older version of routed or gated might provide,
223 		 * restore cloning bit.
224 		 */
225 		if (!(rt->rt_flags & RTF_HOST) &&
226 		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
227 			rt->rt_flags |= RTF_CLONING;
228 		if (rt->rt_flags & RTF_CLONING) {
229 			/*
230 			 * Case 1: This route should come from a route to iface.
231 			 */
232 			rt_setgate(rt, rt_key(rt),
233 				   (struct sockaddr *)&null_sdl,
234 				   RTL_DONTREPORT);
235 			gate = rt->rt_gateway;
236 			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
237 			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
238 			rt->rt_expire = time_second;
239 			break;
240 		}
241 		/* Announce a new entry if requested. */
242 		if (rt->rt_flags & RTF_ANNOUNCE) {
243 			arprequest_async(rt->rt_ifp,
244 			    &SIN(rt_key(rt))->sin_addr,
245 			    &SIN(rt_key(rt))->sin_addr,
246 			    LLADDR(SDL(gate)));
247 		}
248 		/*FALLTHROUGH*/
249 	case RTM_RESOLVE:
250 		if (gate->sa_family != AF_LINK ||
251 		    gate->sa_len < sizeof(struct sockaddr_dl)) {
252 			log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
253 			break;
254 		}
255 		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
256 		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
257 		if (la != NULL)
258 			break; /* This happens on a route change */
259 		/*
260 		 * Case 2:  This route may come from cloning, or a manual route
261 		 * add with a LL address.
262 		 */
263 		R_Malloc(la, struct llinfo_arp *, sizeof *la);
264 		rt->rt_llinfo = la;
265 		if (la == NULL) {
266 			log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
267 			break;
268 		}
269 		bzero(la, sizeof *la);
270 		la->la_rt = rt;
271 		rt->rt_flags |= RTF_LLINFO;
272 		LIST_INSERT_HEAD(&llinfo_arp_list[mycpuid], la, la_le);
273 
274 #ifdef INET
275 		/*
276 		 * This keeps the multicast addresses from showing up
277 		 * in `arp -a' listings as unresolved.  It's not actually
278 		 * functional.  Then the same for broadcast.
279 		 */
280 		if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
281 			ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
282 					       LLADDR(SDL(gate)));
283 			SDL(gate)->sdl_alen = 6;
284 			rt->rt_expire = 0;
285 		}
286 		if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
287 			memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
288 			       rt->rt_ifp->if_addrlen);
289 			SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
290 			rt->rt_expire = 0;
291 		}
292 #endif
293 
294 		/*
295 		 * This fixes up the routing interface for local addresses.
296 		 * The route is adjusted to point at lo0 and the expiration
297 		 * timer is disabled.
298 		 *
299 		 * NOTE: This prevents locally targetted traffic from going
300 		 *	 out the hardware interface, which is inefficient
301 		 *	 and might not work if the hardware cannot listen
302 		 *	 to its own transmitted packets.   Setting
303 		 *	 net.link.ether.inet.useloopback to 0 will force
304 		 *	 packets for local addresses out the hardware (and
305 		 *	 it is expected to receive its own packet).
306 		 *
307 		 * XXX We should just be able to test RTF_LOCAL here instead
308 		 *     of having to compare IPs.
309 		 */
310 		if (SIN(rt_key(rt))->sin_addr.s_addr ==
311 		    (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
312 			rt->rt_expire = 0;
313 			bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
314 			      SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
315 			if (useloopback)
316 				rt->rt_ifp = loif;
317 		}
318 		break;
319 
320 	case RTM_DELETE:
321 		if (la == NULL)
322 			break;
323 		LIST_REMOVE(la, la_le);
324 		rt->rt_llinfo = NULL;
325 		rt->rt_flags &= ~RTF_LLINFO;
326 		if (la->la_hold != NULL)
327 			m_freem(la->la_hold);
328 		Free(la);
329 		break;
330 	}
331 }
332 
333 static struct mbuf *
334 arpreq_alloc(struct ifnet *ifp, const struct in_addr *sip,
335 	     const struct in_addr *tip, const u_char *enaddr)
336 {
337 	struct mbuf *m;
338 	struct arphdr *ah;
339 	u_short ar_hrd;
340 
341 	if ((m = m_gethdr(MB_DONTWAIT, MT_DATA)) == NULL)
342 		return NULL;
343 	m->m_pkthdr.rcvif = NULL;
344 
345 	switch (ifp->if_type) {
346 	case IFT_ETHER:
347 		/*
348 		 * This may not be correct for types not explicitly
349 		 * listed, but this is our best guess
350 		 */
351 	default:
352 		ar_hrd = htons(ARPHRD_ETHER);
353 
354 		m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
355 		m->m_pkthdr.len = m->m_len;
356 		MH_ALIGN(m, m->m_len);
357 
358 		ah = mtod(m, struct arphdr *);
359 		break;
360 	}
361 
362 	ah->ar_hrd = ar_hrd;
363 	ah->ar_pro = htons(ETHERTYPE_IP);
364 	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
365 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
366 	ah->ar_op = htons(ARPOP_REQUEST);
367 	memcpy(ar_sha(ah), enaddr, ah->ar_hln);
368 	memset(ar_tha(ah), 0, ah->ar_hln);
369 	memcpy(ar_spa(ah), sip, ah->ar_pln);
370 	memcpy(ar_tpa(ah), tip, ah->ar_pln);
371 
372 	return m;
373 }
374 
375 static void
376 arpreq_send(struct ifnet *ifp, struct mbuf *m)
377 {
378 	struct sockaddr sa;
379 	struct ether_header *eh;
380 
381 	switch (ifp->if_type) {
382 	case IFT_ETHER:
383 		/*
384 		 * This may not be correct for types not explicitly
385 		 * listed, but this is our best guess
386 		 */
387 	default:
388 		eh = (struct ether_header *)sa.sa_data;
389 		/* if_output() will not swap */
390 		eh->ether_type = htons(ETHERTYPE_ARP);
391 		memcpy(eh->ether_dhost, ifp->if_broadcastaddr, ifp->if_addrlen);
392 		break;
393 	}
394 
395 	sa.sa_family = AF_UNSPEC;
396 	sa.sa_len = sizeof(sa);
397 	ifp->if_output(ifp, m, &sa, NULL);
398 }
399 
400 static void
401 arpreq_send_handler(netmsg_t msg)
402 {
403 	struct mbuf *m = msg->packet.nm_packet;
404 	struct ifnet *ifp = msg->lmsg.u.ms_resultp;
405 
406 	arpreq_send(ifp, m);
407 	/* nmsg was embedded in the mbuf, do not reply! */
408 }
409 
410 /*
411  * Broadcast an ARP request. Caller specifies:
412  *	- arp header source ip address
413  *	- arp header target ip address
414  *	- arp header source ethernet address
415  *
416  * NOTE: Caller MUST NOT hold ifp's serializer
417  */
418 static void
419 arprequest(struct ifnet *ifp, const struct in_addr *sip,
420 	   const struct in_addr *tip, const u_char *enaddr)
421 {
422 	struct mbuf *m;
423 
424 	if (enaddr == NULL) {
425 		if (ifp->if_bridge) {
426 			enaddr = IF_LLADDR(ether_bridge_interface(ifp));
427 		} else {
428 			enaddr = IF_LLADDR(ifp);
429 		}
430 	}
431 
432 	m = arpreq_alloc(ifp, sip, tip, enaddr);
433 	if (m == NULL)
434 		return;
435 	arpreq_send(ifp, m);
436 }
437 
438 /*
439  * Same as arprequest(), except:
440  * - Caller is allowed to hold ifp's serializer
441  * - Network output is done in protocol thead
442  */
443 static void
444 arprequest_async(struct ifnet *ifp, const struct in_addr *sip,
445 		 const struct in_addr *tip, const u_char *enaddr)
446 {
447 	struct mbuf *m;
448 	struct netmsg_packet *pmsg;
449 
450 	if (enaddr == NULL) {
451 		if (ifp->if_bridge) {
452 			enaddr = IF_LLADDR(ether_bridge_interface(ifp));
453 		} else {
454 			enaddr = IF_LLADDR(ifp);
455 		}
456 	}
457 	m = arpreq_alloc(ifp, sip, tip, enaddr);
458 	if (m == NULL)
459 		return;
460 
461 	pmsg = &m->m_hdr.mh_netmsg;
462 	netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
463 		    0, arpreq_send_handler);
464 	pmsg->nm_packet = m;
465 	pmsg->base.lmsg.u.ms_resultp = ifp;
466 
467 	lwkt_sendmsg(netisr_cpuport(mycpuid), &pmsg->base.lmsg);
468 }
469 
470 /*
471  * Resolve an IP address into an ethernet address.  If success,
472  * desten is filled in.  If there is no entry in arptab,
473  * set one up and broadcast a request for the IP address.
474  * Hold onto this mbuf and resend it once the address
475  * is finally resolved.  A return value of 1 indicates
476  * that desten has been filled in and the packet should be sent
477  * normally; a 0 return indicates that the packet has been
478  * taken over here, either now or for later transmission.
479  */
480 int
481 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
482 	   struct sockaddr *dst, u_char *desten)
483 {
484 	struct rtentry *rt = NULL;
485 	struct llinfo_arp *la = NULL;
486 	struct sockaddr_dl *sdl;
487 
488 	if (m->m_flags & M_BCAST) {	/* broadcast */
489 		memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
490 		return (1);
491 	}
492 	if (m->m_flags & M_MCAST) {/* multicast */
493 		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
494 		return (1);
495 	}
496 	if (rt0 != NULL) {
497 		if (rt_llroute(dst, rt0, &rt) != 0) {
498 			m_freem(m);
499 			return 0;
500 		}
501 		la = rt->rt_llinfo;
502 	}
503 	if (la == NULL) {
504 		la = arplookup(SIN(dst)->sin_addr.s_addr,
505 			       TRUE, RTL_REPORTMSG, FALSE);
506 		if (la != NULL)
507 			rt = la->la_rt;
508 	}
509 	if (la == NULL || rt == NULL) {
510 		log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
511 		    inet_ntoa(SIN(dst)->sin_addr), la ? "la" : " ",
512 		    rt ? "rt" : "");
513 		m_freem(m);
514 		return (0);
515 	}
516 	sdl = SDL(rt->rt_gateway);
517 	/*
518 	 * Check the address family and length is valid, the address
519 	 * is resolved; otherwise, try to resolve.
520 	 */
521 	if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
522 	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
523 		/*
524 		 * If entry has an expiry time and it is approaching,
525 		 * see if we need to send an ARP request within this
526 		 * arpt_down interval.
527 		 */
528 		if ((rt->rt_expire != 0) &&
529 		    (time_second + la->la_preempt > rt->rt_expire)) {
530 			arprequest(ifp,
531 				   &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
532 				   &SIN(dst)->sin_addr,
533 				   NULL);
534 			la->la_preempt--;
535 		}
536 
537 		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
538 		return 1;
539 	}
540 	/*
541 	 * If ARP is disabled or static on this interface, stop.
542 	 * XXX
543 	 * Probably should not allocate empty llinfo struct if we are
544 	 * not going to be sending out an arp request.
545 	 */
546 	if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
547 		m_freem(m);
548 		return (0);
549 	}
550 	/*
551 	 * There is an arptab entry, but no ethernet address
552 	 * response yet.  Replace the held mbuf with this
553 	 * latest one.
554 	 */
555 	if (la->la_hold != NULL)
556 		m_freem(la->la_hold);
557 	la->la_hold = m;
558 	la->la_msgport = netisr_curport();
559 	if (rt->rt_expire || ((rt->rt_flags & RTF_STATIC) && !sdl->sdl_alen)) {
560 		rt->rt_flags &= ~RTF_REJECT;
561 		if (la->la_asked == 0 || rt->rt_expire != time_second) {
562 			rt->rt_expire = time_second;
563 			if (la->la_asked++ < arp_maxtries) {
564 				arprequest(ifp,
565 					   &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
566 					   &SIN(dst)->sin_addr,
567 					   NULL);
568 			} else {
569 				rt->rt_flags |= RTF_REJECT;
570 				rt->rt_expire += arpt_down;
571 				la->la_asked = 0;
572 				la->la_preempt = arp_maxtries;
573 			}
574 		}
575 	}
576 	return (0);
577 }
578 
579 /*
580  * Common length and type checks are done here,
581  * then the protocol-specific routine is called.
582  */
583 static void
584 arpintr(netmsg_t msg)
585 {
586 	struct mbuf *m = msg->packet.nm_packet;
587 	struct arphdr *ar;
588 	u_short ar_hrd;
589 	char hexstr[6];
590 
591 	if (m->m_len < sizeof(struct arphdr) &&
592 	    (m = m_pullup(m, sizeof(struct arphdr))) == NULL) {
593 		log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
594 		return;
595 	}
596 	ar = mtod(m, struct arphdr *);
597 
598 	ar_hrd = ntohs(ar->ar_hrd);
599 	if (ar_hrd != ARPHRD_ETHER && ar_hrd != ARPHRD_IEEE802) {
600 		hexncpy((unsigned char *)&ar->ar_hrd, 2, hexstr, 5, NULL);
601 		log(LOG_ERR, "arp: unknown hardware address format (0x%s)\n",
602 		    hexstr);
603 		m_freem(m);
604 		return;
605 	}
606 
607 	if (m->m_pkthdr.len < arphdr_len(ar)) {
608 		if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
609 			log(LOG_ERR, "arp: runt packet\n");
610 			return;
611 		}
612 		ar = mtod(m, struct arphdr *);
613 	}
614 
615 	switch (ntohs(ar->ar_pro)) {
616 #ifdef INET
617 	case ETHERTYPE_IP:
618 		in_arpinput(m);
619 		return;
620 #endif
621 	}
622 	m_freem(m);
623 	/* msg was embedded in the mbuf, do not reply! */
624 }
625 
626 #ifdef INET
627 /*
628  * ARP for Internet protocols on 10 Mb/s Ethernet.
629  * Algorithm is that given in RFC 826.
630  * In addition, a sanity check is performed on the sender
631  * protocol address, to catch impersonators.
632  * We no longer handle negotiations for use of trailer protocol:
633  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
634  * along with IP replies if we wanted trailers sent to us,
635  * and also sent them in response to IP replies.
636  * This allowed either end to announce the desire to receive
637  * trailer packets.
638  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
639  * but formerly didn't normally send requests.
640  */
641 
642 static int	log_arp_wrong_iface = 1;
643 static int	log_arp_movements = 1;
644 static int	log_arp_permanent_modify = 1;
645 
646 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
647 	   &log_arp_wrong_iface, 0,
648 	   "Log arp packets arriving on the wrong interface");
649 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
650 	   &log_arp_movements, 0,
651 	   "Log arp replies from MACs different than the one in the cache");
652 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
653 	   &log_arp_permanent_modify, 0,
654 	   "Log arp replies from MACs different than the one "
655 	   "in the permanent arp entry");
656 
657 
658 static void
659 arp_hold_output(netmsg_t msg)
660 {
661 	struct mbuf *m = msg->packet.nm_packet;
662 	struct rtentry *rt;
663 	struct ifnet *ifp;
664 
665 	rt = msg->lmsg.u.ms_resultp;
666 	ifp = m->m_pkthdr.rcvif;
667 	m->m_pkthdr.rcvif = NULL;
668 
669 	ifp->if_output(ifp, m, rt_key(rt), rt);
670 
671 	/* Drop the reference count bumped by the sender */
672 	RTFREE(rt);
673 
674 	/* nmsg was embedded in the mbuf, do not reply! */
675 }
676 
677 static void
678 arp_update_oncpu(struct mbuf *m, in_addr_t saddr, boolean_t create,
679 		 boolean_t generate_report, boolean_t dologging)
680 {
681 	struct arphdr *ah = mtod(m, struct arphdr *);
682 	struct ifnet *ifp = m->m_pkthdr.rcvif;
683 	struct llinfo_arp *la;
684 	struct sockaddr_dl *sdl;
685 	struct rtentry *rt;
686 	char hexstr[2][64];
687 
688 	la = arplookup(saddr, create, generate_report, FALSE);
689 	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
690 		struct in_addr isaddr = { saddr };
691 
692 		/*
693 		 * Normally arps coming in on the wrong interface are ignored,
694 		 * but if we are bridging and the two interfaces belong to
695 		 * the same bridge, or one is a member of the bridge which
696 		 * is the other, then it isn't an error.
697 		 */
698 		if (rt->rt_ifp != ifp) {
699 			/*
700 			 * (1) ifp and rt_ifp both members of same bridge
701 			 * (2) rt_ifp member of bridge ifp
702 			 * (3) ifp member of bridge rt_ifp
703 			 *
704 			 * Always replace rt_ifp with the bridge ifc.
705 			 */
706 			struct ifnet *nifp;
707 
708 			if (ifp->if_bridge &&
709 			    rt->rt_ifp->if_bridge == ifp->if_bridge) {
710 				nifp = ether_bridge_interface(ifp);
711 			} else if (rt->rt_ifp->if_bridge &&
712 				   ether_bridge_interface(rt->rt_ifp) == ifp) {
713 				nifp = ifp;
714 			} else if (ifp->if_bridge &&
715 				   ether_bridge_interface(ifp) == rt->rt_ifp) {
716 				nifp = rt->rt_ifp;
717 			} else {
718 				nifp = NULL;
719 			}
720 
721 			if ((log_arp_wrong_iface == 1 && nifp == NULL) ||
722 			    log_arp_wrong_iface == 2) {
723 				hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
724 				    hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":");
725 				log(LOG_ERR,
726 				    "arp: %s is on %s "
727 				    "but got reply from %s on %s\n",
728 				    inet_ntoa(isaddr),
729 				    rt->rt_ifp->if_xname, hexstr[0],
730 				    ifp->if_xname);
731 			}
732 			if (nifp == NULL)
733 				return;
734 
735 			/*
736 			 * nifp is our man!  Replace rt_ifp and adjust
737 			 * the sdl.
738 			 */
739 			ifp = rt->rt_ifp = nifp;
740 			sdl->sdl_type = ifp->if_type;
741 			sdl->sdl_index = ifp->if_index;
742 		}
743 		if (sdl->sdl_alen &&
744 		    bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
745 			if (rt->rt_expire != 0) {
746 				if (dologging && log_arp_movements) {
747 					hexncpy((u_char *)LLADDR(sdl), ifp->if_addrlen,
748 					    hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":");
749 					hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
750 					    hexstr[1], HEX_NCPYLEN(ifp->if_addrlen), ":");
751 			    		log(LOG_INFO,
752 					    "arp: %s moved from %s to %s on %s\n",
753 					    inet_ntoa(isaddr), hexstr[0], hexstr[1],
754 					    ifp->if_xname);
755 				}
756 			} else {
757 				if (dologging && log_arp_permanent_modify) {
758 					hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
759 					    hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":");
760 					log(LOG_ERR,
761 					"arp: %s attempts to modify "
762 					"permanent entry for %s on %s\n",
763 					hexstr[0], inet_ntoa(isaddr), ifp->if_xname);
764 				}
765 				return;
766 			}
767 		}
768 		/*
769 		 * sanity check for the address length.
770 		 * XXX this does not work for protocols with variable address
771 		 * length. -is
772 		 */
773 		if (dologging && sdl->sdl_alen && sdl->sdl_alen != ah->ar_hln) {
774 			hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
775 			    hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":");
776 			log(LOG_WARNING,
777 			    "arp from %s: new addr len %d, was %d",
778 			    hexstr[0], ah->ar_hln, sdl->sdl_alen);
779 		}
780 		if (ifp->if_addrlen != ah->ar_hln) {
781 			if (dologging) {
782 				hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
783 				    hexstr[0], HEX_NCPYLEN(ifp->if_addrlen), ":");
784 				log(LOG_WARNING,
785 				"arp from %s: addr len: new %d, i/f %d "
786 				"(ignored)", hexstr[0],
787 				ah->ar_hln, ifp->if_addrlen);
788 			}
789 			return;
790 		}
791 		memcpy(LLADDR(sdl), ar_sha(ah), sdl->sdl_alen = ah->ar_hln);
792 		if (rt->rt_expire != 0) {
793 			rt->rt_expire = time_second + arpt_keep;
794 		}
795 		rt->rt_flags &= ~RTF_REJECT;
796 		la->la_asked = 0;
797 		la->la_preempt = arp_maxtries;
798 
799 		/*
800 		 * This particular cpu might have been holding an mbuf
801 		 * pending ARP resolution.  If so, transmit the mbuf now.
802 		 */
803 		if (la->la_hold != NULL) {
804 			struct mbuf *m = la->la_hold;
805 			struct lwkt_port *port = la->la_msgport;
806 			struct netmsg_packet *pmsg;
807 
808 			la->la_hold = NULL;
809 			la->la_msgport = NULL;
810 
811 			m_adj(m, sizeof(struct ether_header));
812 
813 			/*
814 			 * Make sure that this rtentry will not be freed
815 			 * before the packet is processed on the target
816 			 * msgport.  The reference count will be dropped
817 			 * in the handler associated with this packet.
818 			 */
819 			rt->rt_refcnt++;
820 
821 			pmsg = &m->m_hdr.mh_netmsg;
822 			netmsg_init(&pmsg->base, NULL,
823 				    &netisr_apanic_rport,
824 				    MSGF_PRIORITY, arp_hold_output);
825 			pmsg->nm_packet = m;
826 
827 			/* Record necessary information */
828 			m->m_pkthdr.rcvif = ifp;
829 			pmsg->base.lmsg.u.ms_resultp = rt;
830 
831 			lwkt_sendmsg(port, &pmsg->base.lmsg);
832 		}
833 	}
834 }
835 
836 /*
837  * Called from arpintr() - this routine is run from a single cpu.
838  */
839 static void
840 in_arpinput(struct mbuf *m)
841 {
842 	struct arphdr *ah;
843 	struct ifnet *ifp = m->m_pkthdr.rcvif;
844 	struct ifaddr_container *ifac;
845 	struct in_ifaddr_container *iac;
846 	struct in_ifaddr *ia = NULL;
847 	struct in_addr isaddr, itaddr, myaddr;
848 	struct netmsg_inarp *msg;
849 	uint8_t *enaddr = NULL;
850 	int req_len;
851 	char hexstr[64];
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 	memcpy(&isaddr, ar_spa(ah), sizeof isaddr);
861 	memcpy(&itaddr, ar_tpa(ah), sizeof itaddr);
862 
863 	/*
864 	 * Check both target and sender IP addresses:
865 	 *
866 	 * If we receive the packet on the interface owning the address,
867 	 * then accept the address.
868 	 *
869 	 * For a bridge, we accept the address if the receive interface and
870 	 * the interface owning the address are on the same bridge, and
871 	 * use the bridge MAC as the is-at response.  The bridge will be
872 	 * responsible for handling the packet.
873 	 *
874 	 * (0) Check target IP against CARP IPs
875 	 */
876 #ifdef CARP
877 	LIST_FOREACH(iac, INADDR_HASH(itaddr.s_addr), ia_hash) {
878 		int is_match = 0, is_parent = 0;
879 
880 		ia = iac->ia;
881 
882 		/* Skip all ia's which don't match */
883 		if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
884 			continue;
885 
886 		if (ia->ia_ifp->if_type != IFT_CARP)
887 			continue;
888 
889 		if (carp_parent(ia->ia_ifp) == ifp)
890 			is_parent = 1;
891 		if (is_parent || ia->ia_ifp == ifp)
892 			is_match = carp_iamatch(ia);
893 
894 		if (is_match) {
895 			if (is_parent) {
896 				/*
897 				 * The parent interface will also receive
898 				 * the ethernet broadcast packets, e.g. ARP
899 				 * REQUEST, so if we could find a CARP
900 				 * interface of the parent that could match
901 				 * the target IP address, we then drop the
902 				 * packets, which is delieverd to us through
903 				 * the parent interface.
904 				 */
905 				m_freem(m);
906 				return;
907 			}
908 			goto match;
909 		}
910 	}
911 #endif	/* CARP */
912 
913 	/*
914 	 * (1) Check target IP against our local IPs
915 	 */
916 	LIST_FOREACH(iac, INADDR_HASH(itaddr.s_addr), ia_hash) {
917 		ia = iac->ia;
918 
919 		/* Skip all ia's which don't match */
920 		if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
921 			continue;
922 
923 #ifdef CARP
924 		/* CARP interfaces are checked in (0) */
925 		if (ia->ia_ifp->if_type == IFT_CARP)
926 			continue;
927 #endif
928 
929 		if (ifp->if_bridge && ia->ia_ifp &&
930 		    ifp->if_bridge == ia->ia_ifp->if_bridge) {
931 			ifp = ether_bridge_interface(ifp);
932 			goto match;
933 		}
934 		if (ia->ia_ifp && ia->ia_ifp->if_bridge &&
935 		    ether_bridge_interface(ia->ia_ifp) == ifp) {
936 			goto match;
937 		}
938 		if (ifp->if_bridge && ether_bridge_interface(ifp) ==
939 		    ia->ia_ifp) {
940 			goto match;
941 		}
942 		if (ia->ia_ifp == ifp) {
943 			goto match;
944 		}
945 	}
946 
947 	/*
948 	 * (2) Check sender IP against our local IPs
949 	 */
950 	LIST_FOREACH(iac, INADDR_HASH(isaddr.s_addr), ia_hash) {
951 		ia = iac->ia;
952 
953 		/* Skip all ia's which don't match */
954 		if (isaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
955 			continue;
956 
957 		if (ifp->if_bridge && ia->ia_ifp &&
958 		    ifp->if_bridge == ia->ia_ifp->if_bridge) {
959 			ifp = ether_bridge_interface(ifp);
960 			goto match;
961 		}
962 		if (ia->ia_ifp && ia->ia_ifp->if_bridge &&
963 		    ether_bridge_interface(ia->ia_ifp) == ifp) {
964 			goto match;
965 		}
966 		if (ifp->if_bridge && ether_bridge_interface(ifp) ==
967 		    ia->ia_ifp) {
968 			goto match;
969 		}
970 
971 		if (ia->ia_ifp == ifp)
972 			goto match;
973 	}
974 
975 	/*
976 	 * No match, use the first inet address on the receive interface
977 	 * as a dummy address for the rest of the function.
978 	 */
979 	TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
980 		struct ifaddr *ifa = ifac->ifa;
981 
982 		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
983 			ia = ifatoia(ifa);
984 			goto match;
985 		}
986 	}
987 
988 	/*
989 	 * If we got here, we didn't find any suitable interface,
990 	 * so drop the packet.
991 	 */
992 	m_freem(m);
993 	return;
994 
995 match:
996 	if (!enaddr)
997 		enaddr = (uint8_t *)IF_LLADDR(ifp);
998 	myaddr = ia->ia_addr.sin_addr;
999 	if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) {
1000 		m_freem(m);	/* it's from me, ignore it. */
1001 		return;
1002 	}
1003 	if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
1004 		log(LOG_ERR,
1005 		    "arp: link address is broadcast for IP address %s!\n",
1006 		    inet_ntoa(isaddr));
1007 		m_freem(m);
1008 		return;
1009 	}
1010 	if (isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
1011 		hexncpy((u_char *)ar_sha(ah), ifp->if_addrlen,
1012 		    hexstr, HEX_NCPYLEN(ifp->if_addrlen), ":");
1013 		log(LOG_ERR,
1014 		   "arp: %s is using my IP address %s!\n",
1015 		    hexstr, inet_ntoa(isaddr));
1016 		itaddr = myaddr;
1017 		goto reply;
1018 	}
1019 	if (ifp->if_flags & IFF_STATICARP)
1020 		goto reply;
1021 
1022 	/*
1023 	 * When arp_restricted_match is true and the ARP response is not
1024 	 * specifically targetted to me, ignore it.  Otherwise the entry
1025 	 * timeout may be updated for an old MAC.
1026 	 */
1027 	if (arp_restricted_match && itaddr.s_addr != myaddr.s_addr) {
1028 		m_freem(m);
1029 		return;
1030 	}
1031 
1032 	/*
1033 	 * Update all CPU's routing tables with this ARP packet
1034 	 */
1035 	msg = &m->m_hdr.mh_arpmsg;
1036 	netmsg_init(&msg->base, NULL, &netisr_apanic_rport,
1037 	    0, arp_update_msghandler);
1038 	msg->m = m;
1039 	msg->saddr = isaddr.s_addr;
1040 	msg->taddr = itaddr.s_addr;
1041 	msg->myaddr = myaddr.s_addr;
1042 	lwkt_sendmsg(rtable_portfn(0), &msg->base.lmsg);
1043 
1044 	/*
1045 	 * Just return here; after all CPUs's routing tables are
1046 	 * properly updated by this ARP packet, an ARP reply will
1047 	 * be generated if appropriate.
1048 	 */
1049 	return;
1050 reply:
1051 	in_arpreply(m, itaddr.s_addr, myaddr.s_addr);
1052 }
1053 
1054 static void
1055 arp_reply_msghandler(netmsg_t msg)
1056 {
1057 	struct netmsg_inarp *rmsg = (struct netmsg_inarp *)msg;
1058 
1059 	in_arpreply(rmsg->m, rmsg->taddr, rmsg->myaddr);
1060 	/* Don't reply this netmsg; netmsg_inarp is embedded in mbuf */
1061 }
1062 
1063 static void
1064 arp_update_msghandler(netmsg_t msg)
1065 {
1066 	struct netmsg_inarp *rmsg = (struct netmsg_inarp *)msg;
1067 	int nextcpu;
1068 
1069 	/*
1070 	 * This message handler will be called on all of the CPUs,
1071 	 * however, we only need to generate rtmsg on CPU0.
1072 	 */
1073 	arp_update_oncpu(rmsg->m, rmsg->saddr, rmsg->taddr == rmsg->myaddr,
1074 	    mycpuid == 0 ? RTL_REPORTMSG : RTL_DONTREPORT, mycpuid == 0);
1075 
1076 	nextcpu = mycpuid + 1;
1077 	if (nextcpu < ncpus) {
1078 		lwkt_forwardmsg(rtable_portfn(nextcpu), &rmsg->base.lmsg);
1079 	} else {
1080 		struct mbuf *m = rmsg->m;
1081 		in_addr_t saddr = rmsg->saddr;
1082 		in_addr_t taddr = rmsg->taddr;
1083 		in_addr_t myaddr = rmsg->myaddr;
1084 
1085 		/*
1086 		 * Dispatch this mbuf to netisr0 to perform ARP reply,
1087 		 * if appropriate.
1088 		 * NOTE: netmsg_inarp is embedded in this mbuf.
1089 		 */
1090 		netmsg_init(&rmsg->base, NULL, &netisr_apanic_rport,
1091 		    0, arp_reply_msghandler);
1092 		rmsg->m = m;
1093 		rmsg->saddr = saddr;
1094 		rmsg->taddr = taddr;
1095 		rmsg->myaddr = myaddr;
1096 		lwkt_sendmsg(netisr_cpuport(0), &rmsg->base.lmsg);
1097 	}
1098 }
1099 
1100 static void
1101 in_arpreply(struct mbuf *m, in_addr_t taddr, in_addr_t myaddr)
1102 {
1103 	struct ifnet *ifp = m->m_pkthdr.rcvif;
1104 	const uint8_t *enaddr;
1105 	struct arphdr *ah;
1106 	struct sockaddr sa;
1107 	struct ether_header *eh;
1108 
1109 	ah = mtod(m, struct arphdr *);
1110 	if (ntohs(ah->ar_op) != ARPOP_REQUEST) {
1111 		m_freem(m);
1112 		return;
1113 	}
1114 
1115 	enaddr = (const uint8_t *)IF_LLADDR(ifp);
1116 	if (taddr == myaddr) {
1117 		/* I am the target */
1118 		memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
1119 		memcpy(ar_sha(ah), enaddr, ah->ar_hln);
1120 	} else {
1121 		struct llinfo_arp *la;
1122 		struct rtentry *rt;
1123 
1124 		la = arplookup(taddr, FALSE, RTL_DONTREPORT, SIN_PROXY);
1125 		if (la == NULL) {
1126 			struct sockaddr_in sin;
1127 
1128 			if (!arp_proxyall) {
1129 				m_freem(m);
1130 				return;
1131 			}
1132 
1133 			bzero(&sin, sizeof sin);
1134 			sin.sin_family = AF_INET;
1135 			sin.sin_len = sizeof sin;
1136 			sin.sin_addr.s_addr = taddr;
1137 
1138 			rt = rtpurelookup((struct sockaddr *)&sin);
1139 			if (rt == NULL) {
1140 				m_freem(m);
1141 				return;
1142 			}
1143 			--rt->rt_refcnt;
1144 			/*
1145 			 * Don't send proxies for nodes on the same interface
1146 			 * as this one came out of, or we'll get into a fight
1147 			 * over who claims what Ether address.
1148 			 */
1149 			if (rt->rt_ifp == ifp) {
1150 				m_freem(m);
1151 				return;
1152 			}
1153 			memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
1154 			memcpy(ar_sha(ah), enaddr, ah->ar_hln);
1155 #ifdef DEBUG_PROXY
1156 			kprintf("arp: proxying for %s\n", inet_ntoa(itaddr));
1157 #endif
1158 		} else {
1159 			struct sockaddr_dl *sdl;
1160 
1161 			rt = la->la_rt;
1162 			memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
1163 			sdl = SDL(rt->rt_gateway);
1164 			memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
1165 		}
1166 	}
1167 
1168 	memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
1169 	memcpy(ar_spa(ah), &taddr, ah->ar_pln);
1170 	ah->ar_op = htons(ARPOP_REPLY);
1171 	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
1172 	switch (ifp->if_type) {
1173 	case IFT_ETHER:
1174 		/*
1175 		 * May not be correct for types not explictly
1176 		 * listed, but it is our best guess.
1177 		 */
1178 	default:
1179 		eh = (struct ether_header *)sa.sa_data;
1180 		memcpy(eh->ether_dhost, ar_tha(ah), sizeof eh->ether_dhost);
1181 		eh->ether_type = htons(ETHERTYPE_ARP);
1182 		break;
1183 	}
1184 	sa.sa_family = AF_UNSPEC;
1185 	sa.sa_len = sizeof sa;
1186 	ifp->if_output(ifp, m, &sa, NULL);
1187 }
1188 
1189 #endif	/* INET */
1190 
1191 /*
1192  * Free an arp entry.  If the arp entry is actively referenced or represents
1193  * a static entry we only clear it back to an unresolved state, otherwise
1194  * we destroy the entry entirely.
1195  *
1196  * Note that static entries are created when route add ... -interface is used
1197  * to create an interface route to a (direct) destination.
1198  */
1199 static void
1200 arptfree(struct llinfo_arp *la)
1201 {
1202 	struct rtentry *rt = la->la_rt;
1203 	struct sockaddr_dl *sdl;
1204 
1205 	if (rt == NULL)
1206 		panic("arptfree");
1207 	sdl = SDL(rt->rt_gateway);
1208 	if (sdl != NULL &&
1209 	    ((rt->rt_refcnt > 0 && sdl->sdl_family == AF_LINK) ||
1210 	     (rt->rt_flags & RTF_STATIC))) {
1211 		sdl->sdl_alen = 0;
1212 		la->la_preempt = la->la_asked = 0;
1213 		rt->rt_flags &= ~RTF_REJECT;
1214 		return;
1215 	}
1216 	rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
1217 }
1218 
1219 /*
1220  * Lookup or enter a new address in arptab.
1221  */
1222 static struct llinfo_arp *
1223 arplookup(in_addr_t addr, boolean_t create, boolean_t generate_report,
1224 	  boolean_t proxy)
1225 {
1226 	struct rtentry *rt;
1227 	struct sockaddr_inarp sin = { sizeof sin, AF_INET };
1228 	const char *why = NULL;
1229 
1230 	sin.sin_addr.s_addr = addr;
1231 	sin.sin_other = proxy ? SIN_PROXY : 0;
1232 	if (create) {
1233 		rt = _rtlookup((struct sockaddr *)&sin,
1234 			       generate_report, RTL_DOCLONE);
1235 	} else {
1236 		rt = rtpurelookup((struct sockaddr *)&sin);
1237 	}
1238 	if (rt == NULL)
1239 		return (NULL);
1240 	rt->rt_refcnt--;
1241 
1242 	if (rt->rt_flags & RTF_GATEWAY)
1243 		why = "host is not on local network";
1244 	else if (!(rt->rt_flags & RTF_LLINFO))
1245 		why = "could not allocate llinfo";
1246 	else if (rt->rt_gateway->sa_family != AF_LINK)
1247 		why = "gateway route is not ours";
1248 
1249 	if (why) {
1250 		if (create) {
1251 			log(LOG_DEBUG, "arplookup %s failed: %s\n",
1252 			    inet_ntoa(sin.sin_addr), why);
1253 		}
1254 		if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_WASCLONED)) {
1255 			/* No references to this route.  Purge it. */
1256 			rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1257 				  rt_mask(rt), rt->rt_flags, NULL);
1258 		}
1259 		return (NULL);
1260 	}
1261 	return (rt->rt_llinfo);
1262 }
1263 
1264 void
1265 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1266 {
1267 	ifa->ifa_rtrequest = arp_rtrequest;
1268 	ifa->ifa_flags |= RTF_CLONING;
1269 }
1270 
1271 void
1272 arp_gratuitous(struct ifnet *ifp, struct ifaddr *ifa)
1273 {
1274 	if (IA_SIN(ifa)->sin_addr.s_addr != INADDR_ANY) {
1275 		arprequest_async(ifp, &IA_SIN(ifa)->sin_addr,
1276 				 &IA_SIN(ifa)->sin_addr, NULL);
1277 	}
1278 }
1279 
1280 static void
1281 arp_ifaddr(void *arg __unused, struct ifnet *ifp,
1282     enum ifaddr_event event, struct ifaddr *ifa)
1283 {
1284 	if (ifa->ifa_rtrequest != arp_rtrequest) /* XXX need a generic way */
1285 		return;
1286 	if (ifa->ifa_addr->sa_family != AF_INET)
1287 		return;
1288 	if (event == IFADDR_EVENT_DELETE)
1289 		return;
1290 
1291 	/*
1292 	 * - CARP interfaces will take care of gratuitous ARP themselves.
1293 	 * - If we are the CARP interface's parent, don't send gratuitous
1294 	 *   ARP to avoid unnecessary confusion.
1295 	 */
1296 #ifdef CARP
1297 	if (ifp->if_type != IFT_CARP && ifp->if_carp == NULL)
1298 #endif
1299 	{
1300 		arp_gratuitous(ifp, ifa);
1301 	}
1302 }
1303 
1304 static void
1305 arp_init(void)
1306 {
1307 	int cpu;
1308 
1309 	for (cpu = 0; cpu < ncpus2; cpu++)
1310 		LIST_INIT(&llinfo_arp_list[cpu]);
1311 
1312 	netisr_register(NETISR_ARP, arpintr, NULL);
1313 
1314 	EVENTHANDLER_REGISTER(ifaddr_event, arp_ifaddr, NULL,
1315 	    EVENTHANDLER_PRI_LAST);
1316 }
1317 
1318 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);
1319