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