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