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