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