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