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