xref: /freebsd/sys/netinet/if_ether.c (revision 8be96e101f2691b80ff9562b72f874da82e735aa)
1 /*-
2  * Copyright (c) 1982, 1986, 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)if_ether.c	8.1 (Berkeley) 6/10/93
30  */
31 
32 /*
33  * Ethernet address resolution protocol.
34  * TODO:
35  *	add "inuse/lock" bit (or ref. count) along with valid bit
36  */
37 
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 #include "opt_inet.h"
42 
43 #include <sys/param.h>
44 #include <sys/kernel.h>
45 #include <sys/queue.h>
46 #include <sys/sysctl.h>
47 #include <sys/systm.h>
48 #include <sys/mbuf.h>
49 #include <sys/malloc.h>
50 #include <sys/proc.h>
51 #include <sys/socket.h>
52 #include <sys/syslog.h>
53 
54 #include <net/if.h>
55 #include <net/if_dl.h>
56 #include <net/if_types.h>
57 #include <net/netisr.h>
58 #include <net/if_llc.h>
59 #include <net/ethernet.h>
60 #include <net/route.h>
61 #include <net/vnet.h>
62 
63 #include <netinet/in.h>
64 #include <netinet/in_var.h>
65 #include <net/if_llatbl.h>
66 #include <netinet/if_ether.h>
67 
68 #include <net/if_arc.h>
69 #include <net/iso88025.h>
70 
71 #include <security/mac/mac_framework.h>
72 
73 #define SIN(s) ((struct sockaddr_in *)s)
74 #define SDL(s) ((struct sockaddr_dl *)s)
75 
76 SYSCTL_DECL(_net_link_ether);
77 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
78 SYSCTL_NODE(_net_link_ether, PF_ARP, arp, CTLFLAG_RW, 0, "");
79 
80 /* timer values */
81 static VNET_DEFINE(int, arpt_keep) = (20*60);	/* once resolved, good for 20
82 						 * minutes */
83 static VNET_DEFINE(int, arp_maxtries) = 5;
84 VNET_DEFINE(int, useloopback) = 1;	/* use loopback interface for
85 					 * local traffic */
86 static VNET_DEFINE(int, arp_proxyall) = 0;
87 static VNET_DEFINE(int, arpt_down) = 20;      /* keep incomplete entries for
88 					       * 20 seconds */
89 static VNET_DEFINE(struct arpstat, arpstat);  /* ARP statistics, see if_arp.h */
90 
91 #define	V_arpt_keep		VNET(arpt_keep)
92 #define	V_arpt_down		VNET(arpt_down)
93 #define	V_arp_maxtries		VNET(arp_maxtries)
94 #define	V_arp_proxyall		VNET(arp_proxyall)
95 #define	V_arpstat		VNET(arpstat)
96 
97 SYSCTL_VNET_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
98 	&VNET_NAME(arpt_keep), 0,
99 	"ARP entry lifetime in seconds");
100 SYSCTL_VNET_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
101 	&VNET_NAME(arp_maxtries), 0,
102 	"ARP resolution attempts before returning error");
103 SYSCTL_VNET_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
104 	&VNET_NAME(useloopback), 0,
105 	"Use the loopback interface for local traffic");
106 SYSCTL_VNET_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
107 	&VNET_NAME(arp_proxyall), 0,
108 	"Enable proxy ARP for all suitable requests");
109 SYSCTL_VNET_STRUCT(_net_link_ether_arp, OID_AUTO, stats, CTLFLAG_RW,
110 	&VNET_NAME(arpstat), arpstat,
111 	"ARP statistics (struct arpstat, net/if_arp.h)");
112 
113 static void	arp_init(void);
114 void		arprequest(struct ifnet *,
115 			struct in_addr *, struct in_addr *, u_char *);
116 static void	arpintr(struct mbuf *);
117 static void	arptimer(void *);
118 #ifdef INET
119 static void	in_arpinput(struct mbuf *);
120 #endif
121 #if defined(INET) || defined(INET6)
122 int		(*carp_iamatch_p)(struct ifnet *, struct in_ifaddr *, struct in_addr *,
123 		    u_int8_t **);
124 #endif
125 
126 static const struct netisr_handler arp_nh = {
127 	.nh_name = "arp",
128 	.nh_handler = arpintr,
129 	.nh_proto = NETISR_ARP,
130 	.nh_policy = NETISR_POLICY_SOURCE,
131 };
132 
133 #ifdef AF_INET
134 void arp_ifscrub(struct ifnet *ifp, uint32_t addr);
135 
136 /*
137  * called by in_ifscrub to remove entry from the table when
138  * the interface goes away
139  */
140 void
141 arp_ifscrub(struct ifnet *ifp, uint32_t addr)
142 {
143 	struct sockaddr_in addr4;
144 
145 	bzero((void *)&addr4, sizeof(addr4));
146 	addr4.sin_len    = sizeof(addr4);
147 	addr4.sin_family = AF_INET;
148 	addr4.sin_addr.s_addr = addr;
149 	CURVNET_SET(ifp->if_vnet);
150 	IF_AFDATA_LOCK(ifp);
151 	lla_lookup(LLTABLE(ifp), (LLE_DELETE | LLE_IFADDR),
152 	    (struct sockaddr *)&addr4);
153 	IF_AFDATA_UNLOCK(ifp);
154 	CURVNET_RESTORE();
155 }
156 #endif
157 
158 /*
159  * Timeout routine.  Age arp_tab entries periodically.
160  */
161 static void
162 arptimer(void *arg)
163 {
164 	struct ifnet *ifp;
165 	struct llentry   *lle;
166 
167 	KASSERT(arg != NULL, ("%s: arg NULL", __func__));
168 	lle = (struct llentry *)arg;
169 	ifp = lle->lle_tbl->llt_ifp;
170 	CURVNET_SET(ifp->if_vnet);
171 	IF_AFDATA_LOCK(ifp);
172 	LLE_WLOCK(lle);
173 	if (lle->la_flags & LLE_STATIC)
174 		LLE_WUNLOCK(lle);
175 	else {
176 		if (!callout_pending(&lle->la_timer) &&
177 		    callout_active(&lle->la_timer)) {
178 			callout_stop(&lle->la_timer);
179 			LLE_REMREF(lle);
180 			(void) llentry_free(lle);
181 			ARPSTAT_INC(timeouts);
182 		}
183 #ifdef DIAGNOSTIC
184 		else {
185 			struct sockaddr *l3addr = L3_ADDR(lle);
186 			log(LOG_INFO,
187 			    "arptimer issue: %p, IPv4 address: \"%s\"\n", lle,
188 			    inet_ntoa(
189 			        ((const struct sockaddr_in *)l3addr)->sin_addr));
190 		}
191 #endif
192 	}
193 	IF_AFDATA_UNLOCK(ifp);
194 	CURVNET_RESTORE();
195 }
196 
197 /*
198  * Broadcast an ARP request. Caller specifies:
199  *	- arp header source ip address
200  *	- arp header target ip address
201  *	- arp header source ethernet address
202  */
203 void
204 arprequest(struct ifnet *ifp, struct in_addr *sip, struct in_addr  *tip,
205     u_char *enaddr)
206 {
207 	struct mbuf *m;
208 	struct arphdr *ah;
209 	struct sockaddr sa;
210 
211 	if (sip == NULL) {
212 		/* XXX don't believe this can happen (or explain why) */
213 		/*
214 		 * The caller did not supply a source address, try to find
215 		 * a compatible one among those assigned to this interface.
216 		 */
217 		struct ifaddr *ifa;
218 
219 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
220 			if (!ifa->ifa_addr ||
221 			    ifa->ifa_addr->sa_family != AF_INET)
222 				continue;
223 			sip = &SIN(ifa->ifa_addr)->sin_addr;
224 			if (0 == ((sip->s_addr ^ tip->s_addr) &
225 			    SIN(ifa->ifa_netmask)->sin_addr.s_addr) )
226 				break;  /* found it. */
227 		}
228 		if (sip == NULL) {
229 			printf("%s: cannot find matching address\n", __func__);
230 			return;
231 		}
232 	}
233 
234 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
235 		return;
236 	m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
237 		2*ifp->if_data.ifi_addrlen;
238 	m->m_pkthdr.len = m->m_len;
239 	MH_ALIGN(m, m->m_len);
240 	ah = mtod(m, struct arphdr *);
241 	bzero((caddr_t)ah, m->m_len);
242 #ifdef MAC
243 	mac_netinet_arp_send(ifp, m);
244 #endif
245 	ah->ar_pro = htons(ETHERTYPE_IP);
246 	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
247 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
248 	ah->ar_op = htons(ARPOP_REQUEST);
249 	bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
250 	bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
251 	bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
252 	sa.sa_family = AF_ARP;
253 	sa.sa_len = 2;
254 	m->m_flags |= M_BCAST;
255 	(*ifp->if_output)(ifp, m, &sa, NULL);
256 	ARPSTAT_INC(txrequests);
257 }
258 
259 /*
260  * Resolve an IP address into an ethernet address.
261  * On input:
262  *    ifp is the interface we use
263  *    rt0 is the route to the final destination (possibly useless)
264  *    m is the mbuf. May be NULL if we don't have a packet.
265  *    dst is the next hop,
266  *    desten is where we want the address.
267  *
268  * On success, desten is filled in and the function returns 0;
269  * If the packet must be held pending resolution, we return EWOULDBLOCK
270  * On other errors, we return the corresponding error code.
271  * Note that m_freem() handles NULL.
272  */
273 int
274 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
275 	struct sockaddr *dst, u_char *desten, struct llentry **lle)
276 {
277 	struct llentry *la = 0;
278 	u_int flags = 0;
279 	int error, renew;
280 
281 	*lle = NULL;
282 	if (m != NULL) {
283 		if (m->m_flags & M_BCAST) {
284 			/* broadcast */
285 			(void)memcpy(desten,
286 			    ifp->if_broadcastaddr, ifp->if_addrlen);
287 			return (0);
288 		}
289 		if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {
290 			/* multicast */
291 			ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
292 			return (0);
293 		}
294 	}
295 	/* XXXXX
296 	 */
297 retry:
298 	IF_AFDATA_RLOCK(ifp);
299 	la = lla_lookup(LLTABLE(ifp), flags, dst);
300 	IF_AFDATA_RUNLOCK(ifp);
301 	if ((la == NULL) && ((flags & LLE_EXCLUSIVE) == 0)
302 	    && ((ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) == 0)) {
303 		flags |= (LLE_CREATE | LLE_EXCLUSIVE);
304 		IF_AFDATA_WLOCK(ifp);
305 		la = lla_lookup(LLTABLE(ifp), flags, dst);
306 		IF_AFDATA_WUNLOCK(ifp);
307 	}
308 	if (la == NULL) {
309 		if (flags & LLE_CREATE)
310 			log(LOG_DEBUG,
311 			    "arpresolve: can't allocate llinfo for %s\n",
312 			    inet_ntoa(SIN(dst)->sin_addr));
313 		m_freem(m);
314 		return (EINVAL);
315 	}
316 
317 	if ((la->la_flags & LLE_VALID) &&
318 	    ((la->la_flags & LLE_STATIC) || la->la_expire > time_second)) {
319 		bcopy(&la->ll_addr, desten, ifp->if_addrlen);
320 		/*
321 		 * If entry has an expiry time and it is approaching,
322 		 * see if we need to send an ARP request within this
323 		 * arpt_down interval.
324 		 */
325 		if (!(la->la_flags & LLE_STATIC) &&
326 		    time_second + la->la_preempt > la->la_expire) {
327 			arprequest(ifp, NULL,
328 			    &SIN(dst)->sin_addr, IF_LLADDR(ifp));
329 
330 			la->la_preempt--;
331 		}
332 
333 		*lle = la;
334 		error = 0;
335 		goto done;
336 	}
337 
338 	if (la->la_flags & LLE_STATIC) {   /* should not happen! */
339 		log(LOG_DEBUG, "arpresolve: ouch, empty static llinfo for %s\n",
340 		    inet_ntoa(SIN(dst)->sin_addr));
341 		m_freem(m);
342 		error = EINVAL;
343 		goto done;
344 	}
345 
346 	renew = (la->la_asked == 0 || la->la_expire != time_second);
347 	if ((renew || m != NULL) && (flags & LLE_EXCLUSIVE) == 0) {
348 		flags |= LLE_EXCLUSIVE;
349 		LLE_RUNLOCK(la);
350 		goto retry;
351 	}
352 	/*
353 	 * There is an arptab entry, but no ethernet address
354 	 * response yet.  Replace the held mbuf with this
355 	 * latest one.
356 	 */
357 	if (m != NULL) {
358 		if (la->la_hold != NULL) {
359 			m_freem(la->la_hold);
360 			ARPSTAT_INC(dropped);
361 		}
362 		la->la_hold = m;
363 		if (renew == 0 && (flags & LLE_EXCLUSIVE)) {
364 			flags &= ~LLE_EXCLUSIVE;
365 			LLE_DOWNGRADE(la);
366 		}
367 
368 	}
369 	/*
370 	 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
371 	 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
372 	 * if we have already sent arp_maxtries ARP requests. Retransmit the
373 	 * ARP request, but not faster than one request per second.
374 	 */
375 	if (la->la_asked < V_arp_maxtries)
376 		error = EWOULDBLOCK;	/* First request. */
377 	else
378 		error = rt0 != NULL && (rt0->rt_flags & RTF_GATEWAY) ?
379 		    EHOSTUNREACH : EHOSTDOWN;
380 
381 	if (renew) {
382 		int canceled;
383 
384 		LLE_ADDREF(la);
385 		la->la_expire = time_second + V_arpt_down;
386 		canceled = callout_reset(&la->la_timer, hz * V_arpt_down,
387 		    arptimer, la);
388 		if (canceled)
389 			LLE_REMREF(la);
390 		la->la_asked++;
391 		LLE_WUNLOCK(la);
392 		arprequest(ifp, NULL, &SIN(dst)->sin_addr,
393 		    IF_LLADDR(ifp));
394 		return (error);
395 	}
396 done:
397 	if (flags & LLE_EXCLUSIVE)
398 		LLE_WUNLOCK(la);
399 	else
400 		LLE_RUNLOCK(la);
401 	return (error);
402 }
403 
404 /*
405  * Common length and type checks are done here,
406  * then the protocol-specific routine is called.
407  */
408 static void
409 arpintr(struct mbuf *m)
410 {
411 	struct arphdr *ar;
412 
413 	if (m->m_len < sizeof(struct arphdr) &&
414 	    ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
415 		log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
416 		return;
417 	}
418 	ar = mtod(m, struct arphdr *);
419 
420 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER &&
421 	    ntohs(ar->ar_hrd) != ARPHRD_IEEE802 &&
422 	    ntohs(ar->ar_hrd) != ARPHRD_ARCNET &&
423 	    ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) {
424 		log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
425 		    (unsigned char *)&ar->ar_hrd, "");
426 		m_freem(m);
427 		return;
428 	}
429 
430 	if (m->m_len < arphdr_len(ar)) {
431 		if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
432 			log(LOG_ERR, "arp: runt packet\n");
433 			m_freem(m);
434 			return;
435 		}
436 		ar = mtod(m, struct arphdr *);
437 	}
438 
439 	ARPSTAT_INC(received);
440 	switch (ntohs(ar->ar_pro)) {
441 #ifdef INET
442 	case ETHERTYPE_IP:
443 		in_arpinput(m);
444 		return;
445 #endif
446 	}
447 	m_freem(m);
448 }
449 
450 #ifdef INET
451 /*
452  * ARP for Internet protocols on 10 Mb/s Ethernet.
453  * Algorithm is that given in RFC 826.
454  * In addition, a sanity check is performed on the sender
455  * protocol address, to catch impersonators.
456  * We no longer handle negotiations for use of trailer protocol:
457  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
458  * along with IP replies if we wanted trailers sent to us,
459  * and also sent them in response to IP replies.
460  * This allowed either end to announce the desire to receive
461  * trailer packets.
462  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
463  * but formerly didn't normally send requests.
464  */
465 static int log_arp_wrong_iface = 1;
466 static int log_arp_movements = 1;
467 static int log_arp_permanent_modify = 1;
468 
469 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
470 	&log_arp_wrong_iface, 0,
471 	"log arp packets arriving on the wrong interface");
472 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
473         &log_arp_movements, 0,
474         "log arp replies from MACs different than the one in the cache");
475 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
476         &log_arp_permanent_modify, 0,
477         "log arp replies from MACs different than the one in the permanent arp entry");
478 
479 
480 static void
481 in_arpinput(struct mbuf *m)
482 {
483 	struct arphdr *ah;
484 	struct ifnet *ifp = m->m_pkthdr.rcvif;
485 	struct llentry *la = NULL;
486 	struct rtentry *rt;
487 	struct ifaddr *ifa;
488 	struct in_ifaddr *ia;
489 	struct mbuf *hold;
490 	struct sockaddr sa;
491 	struct in_addr isaddr, itaddr, myaddr;
492 	u_int8_t *enaddr = NULL;
493 	int op, flags;
494 	int req_len;
495 	int bridged = 0, is_bridge = 0;
496 	int carp_match = 0;
497 	struct sockaddr_in sin;
498 	sin.sin_len = sizeof(struct sockaddr_in);
499 	sin.sin_family = AF_INET;
500 	sin.sin_addr.s_addr = 0;
501 
502 	if (ifp->if_bridge)
503 		bridged = 1;
504 	if (ifp->if_type == IFT_BRIDGE)
505 		is_bridge = 1;
506 
507 	req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
508 	if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
509 		log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
510 		return;
511 	}
512 
513 	ah = mtod(m, struct arphdr *);
514 	op = ntohs(ah->ar_op);
515 	(void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
516 	(void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
517 
518 	if (op == ARPOP_REPLY)
519 		ARPSTAT_INC(rxreplies);
520 
521 	/*
522 	 * For a bridge, we want to check the address irrespective
523 	 * of the receive interface. (This will change slightly
524 	 * when we have clusters of interfaces).
525 	 * If the interface does not match, but the recieving interface
526 	 * is part of carp, we call carp_iamatch to see if this is a
527 	 * request for the virtual host ip.
528 	 * XXX: This is really ugly!
529 	 */
530 	IN_IFADDR_RLOCK();
531 	LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
532 		if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
533 		    ia->ia_ifp == ifp) &&
534 		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
535 			ifa_ref(&ia->ia_ifa);
536 			IN_IFADDR_RUNLOCK();
537 			goto match;
538 		}
539 		if (ifp->if_carp != NULL &&
540 		    (*carp_iamatch_p)(ifp, ia, &isaddr, &enaddr) &&
541 		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
542 			carp_match = 1;
543 			ifa_ref(&ia->ia_ifa);
544 			IN_IFADDR_RUNLOCK();
545 			goto match;
546 		}
547 	}
548 	LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
549 		if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
550 		    ia->ia_ifp == ifp) &&
551 		    isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
552 			ifa_ref(&ia->ia_ifa);
553 			IN_IFADDR_RUNLOCK();
554 			goto match;
555 		}
556 
557 #define BDG_MEMBER_MATCHES_ARP(addr, ifp, ia)				\
558   (ia->ia_ifp->if_bridge == ifp->if_softc &&				\
559   !bcmp(IF_LLADDR(ia->ia_ifp), IF_LLADDR(ifp), ifp->if_addrlen) &&	\
560   addr == ia->ia_addr.sin_addr.s_addr)
561 	/*
562 	 * Check the case when bridge shares its MAC address with
563 	 * some of its children, so packets are claimed by bridge
564 	 * itself (bridge_input() does it first), but they are really
565 	 * meant to be destined to the bridge member.
566 	 */
567 	if (is_bridge) {
568 		LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
569 			if (BDG_MEMBER_MATCHES_ARP(itaddr.s_addr, ifp, ia)) {
570 				ifa_ref(&ia->ia_ifa);
571 				ifp = ia->ia_ifp;
572 				IN_IFADDR_RUNLOCK();
573 				goto match;
574 			}
575 		}
576 	}
577 #undef BDG_MEMBER_MATCHES_ARP
578 	IN_IFADDR_RUNLOCK();
579 
580 	/*
581 	 * No match, use the first inet address on the receive interface
582 	 * as a dummy address for the rest of the function.
583 	 */
584 	IF_ADDR_LOCK(ifp);
585 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
586 		if (ifa->ifa_addr->sa_family == AF_INET) {
587 			ia = ifatoia(ifa);
588 			ifa_ref(ifa);
589 			IF_ADDR_UNLOCK(ifp);
590 			goto match;
591 		}
592 	IF_ADDR_UNLOCK(ifp);
593 
594 	/*
595 	 * If bridging, fall back to using any inet address.
596 	 */
597 	IN_IFADDR_RLOCK();
598 	if (!bridged || (ia = TAILQ_FIRST(&V_in_ifaddrhead)) == NULL) {
599 		IN_IFADDR_RUNLOCK();
600 		goto drop;
601 	}
602 	ifa_ref(&ia->ia_ifa);
603 	IN_IFADDR_RUNLOCK();
604 match:
605 	if (!enaddr)
606 		enaddr = (u_int8_t *)IF_LLADDR(ifp);
607 	myaddr = ia->ia_addr.sin_addr;
608 	ifa_free(&ia->ia_ifa);
609 	if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
610 		goto drop;	/* it's from me, ignore it. */
611 	if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
612 		log(LOG_ERR,
613 		    "arp: link address is broadcast for IP address %s!\n",
614 		    inet_ntoa(isaddr));
615 		goto drop;
616 	}
617 	/*
618 	 * Warn if another host is using the same IP address, but only if the
619 	 * IP address isn't 0.0.0.0, which is used for DHCP only, in which
620 	 * case we suppress the warning to avoid false positive complaints of
621 	 * potential misconfiguration.
622 	 */
623 	if (!bridged && isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
624 		log(LOG_ERR,
625 		   "arp: %*D is using my IP address %s on %s!\n",
626 		   ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
627 		   inet_ntoa(isaddr), ifp->if_xname);
628 		itaddr = myaddr;
629 		ARPSTAT_INC(dupips);
630 		goto reply;
631 	}
632 	if (ifp->if_flags & IFF_STATICARP)
633 		goto reply;
634 
635 	bzero(&sin, sizeof(sin));
636 	sin.sin_len = sizeof(struct sockaddr_in);
637 	sin.sin_family = AF_INET;
638 	sin.sin_addr = isaddr;
639 	flags = (itaddr.s_addr == myaddr.s_addr) ? LLE_CREATE : 0;
640 	flags |= LLE_EXCLUSIVE;
641 	IF_AFDATA_LOCK(ifp);
642 	la = lla_lookup(LLTABLE(ifp), flags, (struct sockaddr *)&sin);
643 	IF_AFDATA_UNLOCK(ifp);
644 	if (la != NULL) {
645 		/* the following is not an error when doing bridging */
646 		if (!bridged && la->lle_tbl->llt_ifp != ifp && !carp_match) {
647 			if (log_arp_wrong_iface)
648 				log(LOG_ERR, "arp: %s is on %s "
649 				    "but got reply from %*D on %s\n",
650 				    inet_ntoa(isaddr),
651 				    la->lle_tbl->llt_ifp->if_xname,
652 				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
653 				    ifp->if_xname);
654 			LLE_WUNLOCK(la);
655 			goto reply;
656 		}
657 		if ((la->la_flags & LLE_VALID) &&
658 		    bcmp(ar_sha(ah), &la->ll_addr, ifp->if_addrlen)) {
659 			if (la->la_flags & LLE_STATIC) {
660 				LLE_WUNLOCK(la);
661 				log(LOG_ERR,
662 				    "arp: %*D attempts to modify permanent "
663 				    "entry for %s on %s\n",
664 				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
665 				    inet_ntoa(isaddr), ifp->if_xname);
666 				goto reply;
667 			}
668 			if (log_arp_movements) {
669 			        log(LOG_INFO, "arp: %s moved from %*D "
670 				    "to %*D on %s\n",
671 				    inet_ntoa(isaddr),
672 				    ifp->if_addrlen,
673 				    (u_char *)&la->ll_addr, ":",
674 				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
675 				    ifp->if_xname);
676 			}
677 		}
678 
679 		if (ifp->if_addrlen != ah->ar_hln) {
680 			LLE_WUNLOCK(la);
681 			log(LOG_WARNING,
682 			    "arp from %*D: addr len: new %d, i/f %d (ignored)",
683 			    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
684 			    ah->ar_hln, ifp->if_addrlen);
685 			goto reply;
686 		}
687 		(void)memcpy(&la->ll_addr, ar_sha(ah), ifp->if_addrlen);
688 		la->la_flags |= LLE_VALID;
689 
690 		EVENTHANDLER_INVOKE(arp_update_event, la);
691 
692 		if (!(la->la_flags & LLE_STATIC)) {
693 			int canceled;
694 
695 			LLE_ADDREF(la);
696 			la->la_expire = time_second + V_arpt_keep;
697 			canceled = callout_reset(&la->la_timer,
698 			    hz * V_arpt_keep, arptimer, la);
699 			if (canceled)
700 				LLE_REMREF(la);
701 		}
702 		la->la_asked = 0;
703 		la->la_preempt = V_arp_maxtries;
704 		hold = la->la_hold;
705 		if (hold != NULL) {
706 			la->la_hold = NULL;
707 			memcpy(&sa, L3_ADDR(la), sizeof(sa));
708 		}
709 		LLE_WUNLOCK(la);
710 		if (hold != NULL)
711 			(*ifp->if_output)(ifp, hold, &sa, NULL);
712 	}
713 reply:
714 	if (op != ARPOP_REQUEST)
715 		goto drop;
716 	ARPSTAT_INC(rxrequests);
717 
718 	if (itaddr.s_addr == myaddr.s_addr) {
719 		/* Shortcut.. the receiving interface is the target. */
720 		(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
721 		(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
722 	} else {
723 		struct llentry *lle = NULL;
724 
725 		sin.sin_addr = itaddr;
726 		IF_AFDATA_LOCK(ifp);
727 		lle = lla_lookup(LLTABLE(ifp), 0, (struct sockaddr *)&sin);
728 		IF_AFDATA_UNLOCK(ifp);
729 
730 		if ((lle != NULL) && (lle->la_flags & LLE_PUB)) {
731 			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
732 			(void)memcpy(ar_sha(ah), &lle->ll_addr, ah->ar_hln);
733 			LLE_RUNLOCK(lle);
734 		} else {
735 
736 			if (lle != NULL)
737 				LLE_RUNLOCK(lle);
738 
739 			if (!V_arp_proxyall)
740 				goto drop;
741 
742 			sin.sin_addr = itaddr;
743 			/* XXX MRT use table 0 for arp reply  */
744 			rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0);
745 			if (!rt)
746 				goto drop;
747 
748 			/*
749 			 * Don't send proxies for nodes on the same interface
750 			 * as this one came out of, or we'll get into a fight
751 			 * over who claims what Ether address.
752 			 */
753 			if (!rt->rt_ifp || rt->rt_ifp == ifp) {
754 				RTFREE_LOCKED(rt);
755 				goto drop;
756 			}
757 			RTFREE_LOCKED(rt);
758 
759 			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
760 			(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
761 
762 			/*
763 			 * Also check that the node which sent the ARP packet
764 			 * is on the the interface we expect it to be on. This
765 			 * avoids ARP chaos if an interface is connected to the
766 			 * wrong network.
767 			 */
768 			sin.sin_addr = isaddr;
769 
770 			/* XXX MRT use table 0 for arp checks */
771 			rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0);
772 			if (!rt)
773 				goto drop;
774 			if (rt->rt_ifp != ifp) {
775 				log(LOG_INFO, "arp_proxy: ignoring request"
776 				    " from %s via %s, expecting %s\n",
777 				    inet_ntoa(isaddr), ifp->if_xname,
778 				    rt->rt_ifp->if_xname);
779 				RTFREE_LOCKED(rt);
780 				goto drop;
781 			}
782 			RTFREE_LOCKED(rt);
783 
784 #ifdef DEBUG_PROXY
785 			printf("arp: proxying for %s\n",
786 			       inet_ntoa(itaddr));
787 #endif
788 		}
789 	}
790 
791 	if (itaddr.s_addr == myaddr.s_addr &&
792 	    IN_LINKLOCAL(ntohl(itaddr.s_addr))) {
793 		/* RFC 3927 link-local IPv4; always reply by broadcast. */
794 #ifdef DEBUG_LINKLOCAL
795 		printf("arp: sending reply for link-local addr %s\n",
796 		    inet_ntoa(itaddr));
797 #endif
798 		m->m_flags |= M_BCAST;
799 		m->m_flags &= ~M_MCAST;
800 	} else {
801 		/* default behaviour; never reply by broadcast. */
802 		m->m_flags &= ~(M_BCAST|M_MCAST);
803 	}
804 	(void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
805 	(void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
806 	ah->ar_op = htons(ARPOP_REPLY);
807 	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
808 	m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
809 	m->m_pkthdr.len = m->m_len;
810 	sa.sa_family = AF_ARP;
811 	sa.sa_len = 2;
812 	(*ifp->if_output)(ifp, m, &sa, NULL);
813 	ARPSTAT_INC(txreplies);
814 	return;
815 
816 drop:
817 	m_freem(m);
818 }
819 #endif
820 
821 void
822 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
823 {
824 	struct llentry *lle;
825 
826 	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) {
827 		arprequest(ifp, &IA_SIN(ifa)->sin_addr,
828 				&IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
829 		/*
830 		 * interface address is considered static entry
831 		 * because the output of the arp utility shows
832 		 * that L2 entry as permanent
833 		 */
834 		IF_AFDATA_LOCK(ifp);
835 		lle = lla_lookup(LLTABLE(ifp), (LLE_CREATE | LLE_IFADDR | LLE_STATIC),
836 				 (struct sockaddr *)IA_SIN(ifa));
837 		IF_AFDATA_UNLOCK(ifp);
838 		if (lle == NULL)
839 			log(LOG_INFO, "arp_ifinit: cannot create arp "
840 			    "entry for interface address\n");
841 		else
842 			LLE_RUNLOCK(lle);
843 	}
844 	ifa->ifa_rtrequest = NULL;
845 }
846 
847 void
848 arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr)
849 {
850 	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
851 		arprequest(ifp, &IA_SIN(ifa)->sin_addr,
852 				&IA_SIN(ifa)->sin_addr, enaddr);
853 	ifa->ifa_rtrequest = NULL;
854 }
855 
856 static void
857 arp_init(void)
858 {
859 
860 	netisr_register(&arp_nh);
861 }
862 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);
863