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