xref: /freebsd/sys/netinet/if_ether.c (revision 79b015331632b33f22bc8d8fef136c2672f2241f)
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 
43 #include <sys/param.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.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/rmlock.h>
53 #include <sys/socket.h>
54 #include <sys/syslog.h>
55 
56 #include <net/if.h>
57 #include <net/if_var.h>
58 #include <net/if_dl.h>
59 #include <net/if_types.h>
60 #include <net/netisr.h>
61 #include <net/if_llc.h>
62 #include <net/ethernet.h>
63 #include <net/route.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 #ifdef INET
71 #include <netinet/ip_carp.h>
72 #endif
73 
74 #include <net/if_arc.h>
75 #include <net/iso88025.h>
76 
77 #include <security/mac/mac_framework.h>
78 
79 #define SIN(s) ((const struct sockaddr_in *)(s))
80 #define SDL(s) ((struct sockaddr_dl *)s)
81 
82 SYSCTL_DECL(_net_link_ether);
83 static SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
84 static SYSCTL_NODE(_net_link_ether, PF_ARP, arp, CTLFLAG_RW, 0, "");
85 
86 /* timer values */
87 static VNET_DEFINE(int, arpt_keep) = (20*60);	/* once resolved, good for 20
88 						 * minutes */
89 static VNET_DEFINE(int, arp_maxtries) = 5;
90 static VNET_DEFINE(int, arp_proxyall) = 0;
91 static VNET_DEFINE(int, arpt_down) = 20;	/* keep incomplete entries for
92 						 * 20 seconds */
93 VNET_PCPUSTAT_DEFINE(struct arpstat, arpstat);  /* ARP statistics, see if_arp.h */
94 VNET_PCPUSTAT_SYSINIT(arpstat);
95 
96 #ifdef VIMAGE
97 VNET_PCPUSTAT_SYSUNINIT(arpstat);
98 #endif /* VIMAGE */
99 
100 static VNET_DEFINE(int, arp_maxhold) = 1;
101 
102 #define	V_arpt_keep		VNET(arpt_keep)
103 #define	V_arpt_down		VNET(arpt_down)
104 #define	V_arp_maxtries		VNET(arp_maxtries)
105 #define	V_arp_proxyall		VNET(arp_proxyall)
106 #define	V_arp_maxhold		VNET(arp_maxhold)
107 
108 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_VNET | CTLFLAG_RW,
109 	&VNET_NAME(arpt_keep), 0,
110 	"ARP entry lifetime in seconds");
111 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_VNET | CTLFLAG_RW,
112 	&VNET_NAME(arp_maxtries), 0,
113 	"ARP resolution attempts before returning error");
114 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_VNET | CTLFLAG_RW,
115 	&VNET_NAME(arp_proxyall), 0,
116 	"Enable proxy ARP for all suitable requests");
117 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, wait, CTLFLAG_VNET | CTLFLAG_RW,
118 	&VNET_NAME(arpt_down), 0,
119 	"Incomplete ARP entry lifetime in seconds");
120 SYSCTL_VNET_PCPUSTAT(_net_link_ether_arp, OID_AUTO, stats, struct arpstat,
121     arpstat, "ARP statistics (struct arpstat, net/if_arp.h)");
122 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxhold, CTLFLAG_VNET | CTLFLAG_RW,
123 	&VNET_NAME(arp_maxhold), 0,
124 	"Number of packets to hold per ARP entry");
125 
126 static void	arp_init(void);
127 static void	arpintr(struct mbuf *);
128 static void	arptimer(void *);
129 #ifdef INET
130 static void	in_arpinput(struct mbuf *);
131 #endif
132 
133 static void arp_check_update_lle(struct arphdr *ah, struct in_addr isaddr,
134     struct ifnet *ifp, int bridged, struct llentry *la);
135 static void arp_update_lle(struct arphdr *ah, struct ifnet *ifp,
136     struct llentry *la);
137 static void arp_mark_lle_reachable(struct llentry *la);
138 
139 
140 static const struct netisr_handler arp_nh = {
141 	.nh_name = "arp",
142 	.nh_handler = arpintr,
143 	.nh_proto = NETISR_ARP,
144 	.nh_policy = NETISR_POLICY_SOURCE,
145 };
146 
147 #ifdef AF_INET
148 /*
149  * called by in_scrubprefix() to remove entry from the table when
150  * the interface goes away
151  */
152 void
153 arp_ifscrub(struct ifnet *ifp, uint32_t addr)
154 {
155 	struct sockaddr_in addr4;
156 
157 	bzero((void *)&addr4, sizeof(addr4));
158 	addr4.sin_len    = sizeof(addr4);
159 	addr4.sin_family = AF_INET;
160 	addr4.sin_addr.s_addr = addr;
161 	IF_AFDATA_WLOCK(ifp);
162 	lla_delete(LLTABLE(ifp), LLE_IFADDR, (struct sockaddr *)&addr4);
163 	IF_AFDATA_WUNLOCK(ifp);
164 }
165 #endif
166 
167 /*
168  * Timeout routine.  Age arp_tab entries periodically.
169  */
170 static void
171 arptimer(void *arg)
172 {
173 	struct llentry *lle = (struct llentry *)arg;
174 	struct ifnet *ifp;
175 
176 	if (lle->la_flags & LLE_STATIC) {
177 		return;
178 	}
179 	LLE_WLOCK(lle);
180 	if (callout_pending(&lle->lle_timer)) {
181 		/*
182 		 * Here we are a bit odd here in the treatment of
183 		 * active/pending. If the pending bit is set, it got
184 		 * rescheduled before I ran. The active
185 		 * bit we ignore, since if it was stopped
186 		 * in ll_tablefree() and was currently running
187 		 * it would have return 0 so the code would
188 		 * not have deleted it since the callout could
189 		 * not be stopped so we want to go through
190 		 * with the delete here now. If the callout
191 		 * was restarted, the pending bit will be back on and
192 		 * we just want to bail since the callout_reset would
193 		 * return 1 and our reference would have been removed
194 		 * by arpresolve() below.
195 		 */
196 		LLE_WUNLOCK(lle);
197  		return;
198  	}
199 	ifp = lle->lle_tbl->llt_ifp;
200 	CURVNET_SET(ifp->if_vnet);
201 
202 	if ((lle->la_flags & LLE_DELETED) == 0) {
203 		int evt;
204 
205 		if (lle->la_flags & LLE_VALID)
206 			evt = LLENTRY_EXPIRED;
207 		else
208 			evt = LLENTRY_TIMEDOUT;
209 		EVENTHANDLER_INVOKE(lle_event, lle, evt);
210 	}
211 
212 	callout_stop(&lle->lle_timer);
213 
214 	/* XXX: LOR avoidance. We still have ref on lle. */
215 	LLE_WUNLOCK(lle);
216 	IF_AFDATA_LOCK(ifp);
217 	LLE_WLOCK(lle);
218 
219 	/* Guard against race with other llentry_free(). */
220 	if (lle->la_flags & LLE_LINKED) {
221 		size_t pkts_dropped;
222 
223 		LLE_REMREF(lle);
224 		pkts_dropped = llentry_free(lle);
225 		ARPSTAT_ADD(dropped, pkts_dropped);
226 	} else
227 		LLE_FREE_LOCKED(lle);
228 
229 	IF_AFDATA_UNLOCK(ifp);
230 
231 	ARPSTAT_INC(timeouts);
232 
233 	CURVNET_RESTORE();
234 }
235 
236 /*
237  * Broadcast an ARP request. Caller specifies:
238  *	- arp header source ip address
239  *	- arp header target ip address
240  *	- arp header source ethernet address
241  */
242 void
243 arprequest(struct ifnet *ifp, const struct in_addr *sip,
244     const struct in_addr *tip, u_char *enaddr)
245 {
246 	struct mbuf *m;
247 	struct arphdr *ah;
248 	struct sockaddr sa;
249 	u_char *carpaddr = NULL;
250 
251 	if (sip == NULL) {
252 		/*
253 		 * The caller did not supply a source address, try to find
254 		 * a compatible one among those assigned to this interface.
255 		 */
256 		struct ifaddr *ifa;
257 
258 		IF_ADDR_RLOCK(ifp);
259 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
260 			if (ifa->ifa_addr->sa_family != AF_INET)
261 				continue;
262 
263 			if (ifa->ifa_carp) {
264 				if ((*carp_iamatch_p)(ifa, &carpaddr) == 0)
265 					continue;
266 				sip = &IA_SIN(ifa)->sin_addr;
267 			} else {
268 				carpaddr = NULL;
269 				sip = &IA_SIN(ifa)->sin_addr;
270 			}
271 
272 			if (0 == ((sip->s_addr ^ tip->s_addr) &
273 			    IA_MASKSIN(ifa)->sin_addr.s_addr))
274 				break;  /* found it. */
275 		}
276 		IF_ADDR_RUNLOCK(ifp);
277 		if (sip == NULL) {
278 			printf("%s: cannot find matching address\n", __func__);
279 			return;
280 		}
281 	}
282 	if (enaddr == NULL)
283 		enaddr = carpaddr ? carpaddr : (u_char *)IF_LLADDR(ifp);
284 
285 	if ((m = m_gethdr(M_NOWAIT, MT_DATA)) == NULL)
286 		return;
287 	m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
288 		2 * ifp->if_addrlen;
289 	m->m_pkthdr.len = m->m_len;
290 	M_ALIGN(m, m->m_len);
291 	ah = mtod(m, struct arphdr *);
292 	bzero((caddr_t)ah, m->m_len);
293 #ifdef MAC
294 	mac_netinet_arp_send(ifp, m);
295 #endif
296 	ah->ar_pro = htons(ETHERTYPE_IP);
297 	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
298 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
299 	ah->ar_op = htons(ARPOP_REQUEST);
300 	bcopy(enaddr, ar_sha(ah), ah->ar_hln);
301 	bcopy(sip, ar_spa(ah), ah->ar_pln);
302 	bcopy(tip, ar_tpa(ah), ah->ar_pln);
303 	sa.sa_family = AF_ARP;
304 	sa.sa_len = 2;
305 	m->m_flags |= M_BCAST;
306 	m_clrprotoflags(m);	/* Avoid confusing lower layers. */
307 	(*ifp->if_output)(ifp, m, &sa, NULL);
308 	ARPSTAT_INC(txrequests);
309 }
310 
311 /*
312  * Resolve an IP address into an ethernet address - heavy version.
313  * Used internally by arpresolve().
314  * We have already checked than  we can't use existing lle without
315  * modification so we have to acquire LLE_EXCLUSIVE lle lock.
316  *
317  * On success, desten and flags are filled in and the function returns 0;
318  * If the packet must be held pending resolution, we return EWOULDBLOCK
319  * On other errors, we return the corresponding error code.
320  * Note that m_freem() handles NULL.
321  */
322 static int
323 arpresolve_full(struct ifnet *ifp, int is_gw, int create, struct mbuf *m,
324 	const struct sockaddr *dst, u_char *desten, uint32_t *pflags)
325 {
326 	struct llentry *la = NULL;
327 	struct mbuf *curr = NULL;
328 	struct mbuf *next = NULL;
329 	int error, renew;
330 
331 	if (pflags != NULL)
332 		*pflags = 0;
333 
334 	if (create == 0) {
335 		IF_AFDATA_RLOCK(ifp);
336 		la = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
337 		IF_AFDATA_RUNLOCK(ifp);
338 	}
339 	if (la == NULL && (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) == 0) {
340 		create = 1;
341 		IF_AFDATA_WLOCK(ifp);
342 		la = lla_create(LLTABLE(ifp), 0, dst);
343 		IF_AFDATA_WUNLOCK(ifp);
344 	}
345 	if (la == NULL) {
346 		if (create != 0)
347 			log(LOG_DEBUG,
348 			    "arpresolve: can't allocate llinfo for %s on %s\n",
349 			    inet_ntoa(SIN(dst)->sin_addr), ifp->if_xname);
350 		m_freem(m);
351 		return (EINVAL);
352 	}
353 
354 	if ((la->la_flags & LLE_VALID) &&
355 	    ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) {
356 		bcopy(&la->ll_addr, desten, ifp->if_addrlen);
357 		renew = 0;
358 		/*
359 		 * If entry has an expiry time and it is approaching,
360 		 * see if we need to send an ARP request within this
361 		 * arpt_down interval.
362 		 */
363 		if (!(la->la_flags & LLE_STATIC) &&
364 		    time_uptime + la->la_preempt > la->la_expire) {
365 			renew = 1;
366 			la->la_preempt--;
367 		}
368 
369 		if (pflags != NULL)
370 			*pflags = la->la_flags;
371 
372 		LLE_WUNLOCK(la);
373 
374 		if (renew == 1)
375 			arprequest(ifp, NULL, &SIN(dst)->sin_addr, NULL);
376 
377 		return (0);
378 	}
379 
380 	renew = (la->la_asked == 0 || la->la_expire != time_uptime);
381 	/*
382 	 * There is an arptab entry, but no ethernet address
383 	 * response yet.  Add the mbuf to the list, dropping
384 	 * the oldest packet if we have exceeded the system
385 	 * setting.
386 	 */
387 	if (m != NULL) {
388 		if (la->la_numheld >= V_arp_maxhold) {
389 			if (la->la_hold != NULL) {
390 				next = la->la_hold->m_nextpkt;
391 				m_freem(la->la_hold);
392 				la->la_hold = next;
393 				la->la_numheld--;
394 				ARPSTAT_INC(dropped);
395 			}
396 		}
397 		if (la->la_hold != NULL) {
398 			curr = la->la_hold;
399 			while (curr->m_nextpkt != NULL)
400 				curr = curr->m_nextpkt;
401 			curr->m_nextpkt = m;
402 		} else
403 			la->la_hold = m;
404 		la->la_numheld++;
405 	}
406 	/*
407 	 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
408 	 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
409 	 * if we have already sent arp_maxtries ARP requests. Retransmit the
410 	 * ARP request, but not faster than one request per second.
411 	 */
412 	if (la->la_asked < V_arp_maxtries)
413 		error = EWOULDBLOCK;	/* First request. */
414 	else
415 		error = is_gw != 0 ? EHOSTUNREACH : EHOSTDOWN;
416 
417 	if (renew) {
418 		int canceled;
419 
420 		LLE_ADDREF(la);
421 		la->la_expire = time_uptime;
422 		canceled = callout_reset(&la->lle_timer, hz * V_arpt_down,
423 		    arptimer, la);
424 		if (canceled)
425 			LLE_REMREF(la);
426 		la->la_asked++;
427 		LLE_WUNLOCK(la);
428 		arprequest(ifp, NULL, &SIN(dst)->sin_addr, NULL);
429 		return (error);
430 	}
431 
432 	LLE_WUNLOCK(la);
433 	return (error);
434 }
435 
436 /*
437  * Resolve an IP address into an ethernet address.
438  * On input:
439  *    ifp is the interface we use
440  *    is_gw != 0 if @dst represents gateway to some destination
441  *    m is the mbuf. May be NULL if we don't have a packet.
442  *    dst is the next hop,
443  *    desten is the storage to put LL address.
444  *    flags returns lle entry flags.
445  *
446  * On success, desten and flags are filled in and the function returns 0;
447  * If the packet must be held pending resolution, we return EWOULDBLOCK
448  * On other errors, we return the corresponding error code.
449  * Note that m_freem() handles NULL.
450  */
451 int
452 arpresolve(struct ifnet *ifp, int is_gw, struct mbuf *m,
453 	const struct sockaddr *dst, u_char *desten, uint32_t *pflags)
454 {
455 	struct llentry *la = 0;
456 	int renew;
457 
458 	if (pflags != NULL)
459 		*pflags = 0;
460 
461 	if (m != NULL) {
462 		if (m->m_flags & M_BCAST) {
463 			/* broadcast */
464 			(void)memcpy(desten,
465 			    ifp->if_broadcastaddr, ifp->if_addrlen);
466 			return (0);
467 		}
468 		if (m->m_flags & M_MCAST) {
469 			/* multicast */
470 			ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
471 			return (0);
472 		}
473 	}
474 
475 	IF_AFDATA_RLOCK(ifp);
476 	la = lla_lookup(LLTABLE(ifp), 0, dst);
477 	IF_AFDATA_RUNLOCK(ifp);
478 
479 	if (la == NULL)
480 		return (arpresolve_full(ifp, is_gw, 1, m, dst, desten, pflags));
481 
482 	if ((la->la_flags & LLE_VALID) &&
483 	    ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) {
484 		bcopy(&la->ll_addr, desten, ifp->if_addrlen);
485 		renew = 0;
486 		/*
487 		 * If entry has an expiry time and it is approaching,
488 		 * see if we need to send an ARP request within this
489 		 * arpt_down interval.
490 		 */
491 		if (!(la->la_flags & LLE_STATIC) &&
492 		    time_uptime + la->la_preempt > la->la_expire) {
493 			renew = 1;
494 			la->la_preempt--;
495 		}
496 
497 		if (pflags != NULL)
498 			*pflags = la->la_flags;
499 
500 		LLE_RUNLOCK(la);
501 
502 		if (renew == 1)
503 			arprequest(ifp, NULL, &SIN(dst)->sin_addr, NULL);
504 
505 		return (0);
506 	}
507 	LLE_RUNLOCK(la);
508 
509 	return (arpresolve_full(ifp, is_gw, 0, m, dst, desten, pflags));
510 }
511 
512 /*
513  * Common length and type checks are done here,
514  * then the protocol-specific routine is called.
515  */
516 static void
517 arpintr(struct mbuf *m)
518 {
519 	struct arphdr *ar;
520 
521 	if (m->m_len < sizeof(struct arphdr) &&
522 	    ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
523 		log(LOG_NOTICE, "arp: runt packet -- m_pullup failed\n");
524 		return;
525 	}
526 	ar = mtod(m, struct arphdr *);
527 
528 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER &&
529 	    ntohs(ar->ar_hrd) != ARPHRD_IEEE802 &&
530 	    ntohs(ar->ar_hrd) != ARPHRD_ARCNET &&
531 	    ntohs(ar->ar_hrd) != ARPHRD_IEEE1394 &&
532 	    ntohs(ar->ar_hrd) != ARPHRD_INFINIBAND) {
533 		log(LOG_NOTICE, "arp: unknown hardware address format (0x%2D)"
534 		    " (from %*D to %*D)\n", (unsigned char *)&ar->ar_hrd, "",
535 		    ETHER_ADDR_LEN, (u_char *)ar_sha(ar), ":",
536 		    ETHER_ADDR_LEN, (u_char *)ar_tha(ar), ":");
537 		m_freem(m);
538 		return;
539 	}
540 
541 	if (m->m_len < arphdr_len(ar)) {
542 		if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
543 			log(LOG_NOTICE, "arp: runt packet\n");
544 			m_freem(m);
545 			return;
546 		}
547 		ar = mtod(m, struct arphdr *);
548 	}
549 
550 	ARPSTAT_INC(received);
551 	switch (ntohs(ar->ar_pro)) {
552 #ifdef INET
553 	case ETHERTYPE_IP:
554 		in_arpinput(m);
555 		return;
556 #endif
557 	}
558 	m_freem(m);
559 }
560 
561 #ifdef INET
562 /*
563  * ARP for Internet protocols on 10 Mb/s Ethernet.
564  * Algorithm is that given in RFC 826.
565  * In addition, a sanity check is performed on the sender
566  * protocol address, to catch impersonators.
567  * We no longer handle negotiations for use of trailer protocol:
568  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
569  * along with IP replies if we wanted trailers sent to us,
570  * and also sent them in response to IP replies.
571  * This allowed either end to announce the desire to receive
572  * trailer packets.
573  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
574  * but formerly didn't normally send requests.
575  */
576 static int log_arp_wrong_iface = 1;
577 static int log_arp_movements = 1;
578 static int log_arp_permanent_modify = 1;
579 static int allow_multicast = 0;
580 static struct timeval arp_lastlog;
581 static int arp_curpps;
582 static int arp_maxpps = 1;
583 
584 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
585 	&log_arp_wrong_iface, 0,
586 	"log arp packets arriving on the wrong interface");
587 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
588 	&log_arp_movements, 0,
589 	"log arp replies from MACs different than the one in the cache");
590 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
591 	&log_arp_permanent_modify, 0,
592 	"log arp replies from MACs different than the one in the permanent arp entry");
593 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, allow_multicast, CTLFLAG_RW,
594 	&allow_multicast, 0, "accept multicast addresses");
595 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_log_per_second,
596 	CTLFLAG_RW, &arp_maxpps, 0,
597 	"Maximum number of remotely triggered ARP messages that can be "
598 	"logged per second");
599 
600 #define	ARP_LOG(pri, ...)	do {					\
601 	if (ppsratecheck(&arp_lastlog, &arp_curpps, arp_maxpps))	\
602 		log((pri), "arp: " __VA_ARGS__);			\
603 } while (0)
604 
605 static void
606 in_arpinput(struct mbuf *m)
607 {
608 	struct rm_priotracker in_ifa_tracker;
609 	struct arphdr *ah;
610 	struct ifnet *ifp = m->m_pkthdr.rcvif;
611 	struct llentry *la = NULL;
612 	struct rtentry *rt;
613 	struct ifaddr *ifa;
614 	struct in_ifaddr *ia;
615 	struct sockaddr sa;
616 	struct in_addr isaddr, itaddr, myaddr;
617 	u_int8_t *enaddr = NULL;
618 	int op;
619 	int req_len;
620 	int bridged = 0, is_bridge = 0;
621 	int carped;
622 	struct sockaddr_in sin;
623 	sin.sin_len = sizeof(struct sockaddr_in);
624 	sin.sin_family = AF_INET;
625 	sin.sin_addr.s_addr = 0;
626 
627 	if (ifp->if_bridge)
628 		bridged = 1;
629 	if (ifp->if_type == IFT_BRIDGE)
630 		is_bridge = 1;
631 
632 	req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
633 	if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
634 		ARP_LOG(LOG_NOTICE, "runt packet -- m_pullup failed\n");
635 		return;
636 	}
637 
638 	ah = mtod(m, struct arphdr *);
639 	/*
640 	 * ARP is only for IPv4 so we can reject packets with
641 	 * a protocol length not equal to an IPv4 address.
642 	 */
643 	if (ah->ar_pln != sizeof(struct in_addr)) {
644 		ARP_LOG(LOG_NOTICE, "requested protocol length != %zu\n",
645 		    sizeof(struct in_addr));
646 		goto drop;
647 	}
648 
649 	if (allow_multicast == 0 && ETHER_IS_MULTICAST(ar_sha(ah))) {
650 		ARP_LOG(LOG_NOTICE, "%*D is multicast\n",
651 		    ifp->if_addrlen, (u_char *)ar_sha(ah), ":");
652 		goto drop;
653 	}
654 
655 	op = ntohs(ah->ar_op);
656 	(void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
657 	(void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
658 
659 	if (op == ARPOP_REPLY)
660 		ARPSTAT_INC(rxreplies);
661 
662 	/*
663 	 * For a bridge, we want to check the address irrespective
664 	 * of the receive interface. (This will change slightly
665 	 * when we have clusters of interfaces).
666 	 */
667 	IN_IFADDR_RLOCK(&in_ifa_tracker);
668 	LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
669 		if (((bridged && ia->ia_ifp->if_bridge == ifp->if_bridge) ||
670 		    ia->ia_ifp == ifp) &&
671 		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr &&
672 		    (ia->ia_ifa.ifa_carp == NULL ||
673 		    (*carp_iamatch_p)(&ia->ia_ifa, &enaddr))) {
674 			ifa_ref(&ia->ia_ifa);
675 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
676 			goto match;
677 		}
678 	}
679 	LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
680 		if (((bridged && ia->ia_ifp->if_bridge == ifp->if_bridge) ||
681 		    ia->ia_ifp == ifp) &&
682 		    isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
683 			ifa_ref(&ia->ia_ifa);
684 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
685 			goto match;
686 		}
687 
688 #define BDG_MEMBER_MATCHES_ARP(addr, ifp, ia)				\
689   (ia->ia_ifp->if_bridge == ifp->if_softc &&				\
690   !bcmp(IF_LLADDR(ia->ia_ifp), IF_LLADDR(ifp), ifp->if_addrlen) &&	\
691   addr == ia->ia_addr.sin_addr.s_addr)
692 	/*
693 	 * Check the case when bridge shares its MAC address with
694 	 * some of its children, so packets are claimed by bridge
695 	 * itself (bridge_input() does it first), but they are really
696 	 * meant to be destined to the bridge member.
697 	 */
698 	if (is_bridge) {
699 		LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
700 			if (BDG_MEMBER_MATCHES_ARP(itaddr.s_addr, ifp, ia)) {
701 				ifa_ref(&ia->ia_ifa);
702 				ifp = ia->ia_ifp;
703 				IN_IFADDR_RUNLOCK(&in_ifa_tracker);
704 				goto match;
705 			}
706 		}
707 	}
708 #undef BDG_MEMBER_MATCHES_ARP
709 	IN_IFADDR_RUNLOCK(&in_ifa_tracker);
710 
711 	/*
712 	 * No match, use the first inet address on the receive interface
713 	 * as a dummy address for the rest of the function.
714 	 */
715 	IF_ADDR_RLOCK(ifp);
716 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
717 		if (ifa->ifa_addr->sa_family == AF_INET &&
718 		    (ifa->ifa_carp == NULL ||
719 		    (*carp_iamatch_p)(ifa, &enaddr))) {
720 			ia = ifatoia(ifa);
721 			ifa_ref(ifa);
722 			IF_ADDR_RUNLOCK(ifp);
723 			goto match;
724 		}
725 	IF_ADDR_RUNLOCK(ifp);
726 
727 	/*
728 	 * If bridging, fall back to using any inet address.
729 	 */
730 	IN_IFADDR_RLOCK(&in_ifa_tracker);
731 	if (!bridged || (ia = TAILQ_FIRST(&V_in_ifaddrhead)) == NULL) {
732 		IN_IFADDR_RUNLOCK(&in_ifa_tracker);
733 		goto drop;
734 	}
735 	ifa_ref(&ia->ia_ifa);
736 	IN_IFADDR_RUNLOCK(&in_ifa_tracker);
737 match:
738 	if (!enaddr)
739 		enaddr = (u_int8_t *)IF_LLADDR(ifp);
740 	carped = (ia->ia_ifa.ifa_carp != NULL);
741 	myaddr = ia->ia_addr.sin_addr;
742 	ifa_free(&ia->ia_ifa);
743 	if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
744 		goto drop;	/* it's from me, ignore it. */
745 	if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
746 		ARP_LOG(LOG_NOTICE, "link address is broadcast for IP address "
747 		    "%s!\n", inet_ntoa(isaddr));
748 		goto drop;
749 	}
750 
751 	if (ifp->if_addrlen != ah->ar_hln) {
752 		ARP_LOG(LOG_WARNING, "from %*D: addr len: new %d, "
753 		    "i/f %d (ignored)\n", ifp->if_addrlen,
754 		    (u_char *) ar_sha(ah), ":", ah->ar_hln,
755 		    ifp->if_addrlen);
756 		goto drop;
757 	}
758 
759 	/*
760 	 * Warn if another host is using the same IP address, but only if the
761 	 * IP address isn't 0.0.0.0, which is used for DHCP only, in which
762 	 * case we suppress the warning to avoid false positive complaints of
763 	 * potential misconfiguration.
764 	 */
765 	if (!bridged && !carped && isaddr.s_addr == myaddr.s_addr &&
766 	    myaddr.s_addr != 0) {
767 		ARP_LOG(LOG_ERR, "%*D is using my IP address %s on %s!\n",
768 		   ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
769 		   inet_ntoa(isaddr), ifp->if_xname);
770 		itaddr = myaddr;
771 		ARPSTAT_INC(dupips);
772 		goto reply;
773 	}
774 	if (ifp->if_flags & IFF_STATICARP)
775 		goto reply;
776 
777 	bzero(&sin, sizeof(sin));
778 	sin.sin_len = sizeof(struct sockaddr_in);
779 	sin.sin_family = AF_INET;
780 	sin.sin_addr = isaddr;
781 	IF_AFDATA_RLOCK(ifp);
782 	la = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, (struct sockaddr *)&sin);
783 	IF_AFDATA_RUNLOCK(ifp);
784 	if (la != NULL)
785 		arp_check_update_lle(ah, isaddr, ifp, bridged, la);
786 	else if (itaddr.s_addr == myaddr.s_addr) {
787 		/*
788 		 * Reply to our address, but no lle exists yet.
789 		 * do we really have to create an entry?
790 		 */
791 		IF_AFDATA_WLOCK(ifp);
792 		la = lla_create(LLTABLE(ifp), 0, (struct sockaddr *)&sin);
793 		arp_update_lle(ah, ifp, la);
794 		IF_AFDATA_WUNLOCK(ifp);
795 		arp_mark_lle_reachable(la);
796 		LLE_WUNLOCK(la);
797 	}
798 reply:
799 	if (op != ARPOP_REQUEST)
800 		goto drop;
801 	ARPSTAT_INC(rxrequests);
802 
803 	if (itaddr.s_addr == myaddr.s_addr) {
804 		/* Shortcut.. the receiving interface is the target. */
805 		(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
806 		(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
807 	} else {
808 		struct llentry *lle = NULL;
809 
810 		sin.sin_addr = itaddr;
811 		IF_AFDATA_RLOCK(ifp);
812 		lle = lla_lookup(LLTABLE(ifp), 0, (struct sockaddr *)&sin);
813 		IF_AFDATA_RUNLOCK(ifp);
814 
815 		if ((lle != NULL) && (lle->la_flags & LLE_PUB)) {
816 			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
817 			(void)memcpy(ar_sha(ah), &lle->ll_addr, ah->ar_hln);
818 			LLE_RUNLOCK(lle);
819 		} else {
820 
821 			if (lle != NULL)
822 				LLE_RUNLOCK(lle);
823 
824 			if (!V_arp_proxyall)
825 				goto drop;
826 
827 			sin.sin_addr = itaddr;
828 			/* XXX MRT use table 0 for arp reply  */
829 			rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0);
830 			if (!rt)
831 				goto drop;
832 
833 			/*
834 			 * Don't send proxies for nodes on the same interface
835 			 * as this one came out of, or we'll get into a fight
836 			 * over who claims what Ether address.
837 			 */
838 			if (!rt->rt_ifp || rt->rt_ifp == ifp) {
839 				RTFREE_LOCKED(rt);
840 				goto drop;
841 			}
842 			RTFREE_LOCKED(rt);
843 
844 			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
845 			(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
846 
847 			/*
848 			 * Also check that the node which sent the ARP packet
849 			 * is on the interface we expect it to be on. This
850 			 * avoids ARP chaos if an interface is connected to the
851 			 * wrong network.
852 			 */
853 			sin.sin_addr = isaddr;
854 
855 			/* XXX MRT use table 0 for arp checks */
856 			rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0);
857 			if (!rt)
858 				goto drop;
859 			if (rt->rt_ifp != ifp) {
860 				ARP_LOG(LOG_INFO, "proxy: ignoring request"
861 				    " from %s via %s, expecting %s\n",
862 				    inet_ntoa(isaddr), ifp->if_xname,
863 				    rt->rt_ifp->if_xname);
864 				RTFREE_LOCKED(rt);
865 				goto drop;
866 			}
867 			RTFREE_LOCKED(rt);
868 
869 #ifdef DEBUG_PROXY
870 			printf("arp: proxying for %s\n", inet_ntoa(itaddr));
871 #endif
872 		}
873 	}
874 
875 	if (itaddr.s_addr == myaddr.s_addr &&
876 	    IN_LINKLOCAL(ntohl(itaddr.s_addr))) {
877 		/* RFC 3927 link-local IPv4; always reply by broadcast. */
878 #ifdef DEBUG_LINKLOCAL
879 		printf("arp: sending reply for link-local addr %s\n",
880 		    inet_ntoa(itaddr));
881 #endif
882 		m->m_flags |= M_BCAST;
883 		m->m_flags &= ~M_MCAST;
884 	} else {
885 		/* default behaviour; never reply by broadcast. */
886 		m->m_flags &= ~(M_BCAST|M_MCAST);
887 	}
888 	(void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
889 	(void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
890 	ah->ar_op = htons(ARPOP_REPLY);
891 	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
892 	m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
893 	m->m_pkthdr.len = m->m_len;
894 	m->m_pkthdr.rcvif = NULL;
895 	sa.sa_family = AF_ARP;
896 	sa.sa_len = 2;
897 	m_clrprotoflags(m);	/* Avoid confusing lower layers. */
898 	(*ifp->if_output)(ifp, m, &sa, NULL);
899 	ARPSTAT_INC(txreplies);
900 	return;
901 
902 drop:
903 	m_freem(m);
904 }
905 #endif
906 
907 /*
908  * Checks received arp data against existing @la.
909  * Updates lle state/performs notification if necessary.
910  */
911 static void
912 arp_check_update_lle(struct arphdr *ah, struct in_addr isaddr, struct ifnet *ifp,
913     int bridged, struct llentry *la)
914 {
915 	struct sockaddr sa;
916 	struct mbuf *m_hold, *m_hold_next;
917 
918 	LLE_WLOCK_ASSERT(la);
919 
920 	/* the following is not an error when doing bridging */
921 	if (!bridged && la->lle_tbl->llt_ifp != ifp) {
922 		if (log_arp_wrong_iface)
923 			ARP_LOG(LOG_WARNING, "%s is on %s "
924 			    "but got reply from %*D on %s\n",
925 			    inet_ntoa(isaddr),
926 			    la->lle_tbl->llt_ifp->if_xname,
927 			    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
928 			    ifp->if_xname);
929 		LLE_WUNLOCK(la);
930 		return;
931 	}
932 	if ((la->la_flags & LLE_VALID) &&
933 	    bcmp(ar_sha(ah), &la->ll_addr, ifp->if_addrlen)) {
934 		if (la->la_flags & LLE_STATIC) {
935 			LLE_WUNLOCK(la);
936 			if (log_arp_permanent_modify)
937 				ARP_LOG(LOG_ERR,
938 				    "%*D attempts to modify "
939 				    "permanent entry for %s on %s\n",
940 				    ifp->if_addrlen,
941 				    (u_char *)ar_sha(ah), ":",
942 				    inet_ntoa(isaddr), ifp->if_xname);
943 			return;
944 		}
945 		if (log_arp_movements) {
946 			ARP_LOG(LOG_INFO, "%s moved from %*D "
947 			    "to %*D on %s\n",
948 			    inet_ntoa(isaddr),
949 			    ifp->if_addrlen,
950 			    (u_char *)&la->ll_addr, ":",
951 			    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
952 			    ifp->if_xname);
953 		}
954 	}
955 
956 	/* Check if something has changed */
957 	if (memcmp(&la->ll_addr, ar_sha(ah), ifp->if_addrlen) != 0 ||
958 	    (la->la_flags & LLE_VALID) == 0) {
959 		/* Perform real LLE update */
960 		/* use afdata WLOCK to update fields */
961 		LLE_ADDREF(la);
962 		LLE_WUNLOCK(la);
963 		IF_AFDATA_WLOCK(ifp);
964 		LLE_WLOCK(la);
965 
966 		/*
967 		 * Since we droppped LLE lock, other thread might have deleted
968 		 * this lle. Check and return
969 		 */
970 		if ((la->la_flags & LLE_DELETED) != 0) {
971 			IF_AFDATA_WUNLOCK(ifp);
972 			LLE_FREE_LOCKED(la);
973 			return;
974 		}
975 
976 		/* Update data */
977 		arp_update_lle(ah, ifp, la);
978 
979 		IF_AFDATA_WUNLOCK(ifp);
980 		LLE_REMREF(la);
981 	}
982 
983 	arp_mark_lle_reachable(la);
984 
985 	/*
986 	 * The packets are all freed within the call to the output
987 	 * routine.
988 	 *
989 	 * NB: The lock MUST be released before the call to the
990 	 * output routine.
991 	 */
992 	if (la->la_hold != NULL) {
993 		m_hold = la->la_hold;
994 		la->la_hold = NULL;
995 		la->la_numheld = 0;
996 		lltable_fill_sa_entry(la, &sa);
997 		LLE_WUNLOCK(la);
998 		for (; m_hold != NULL; m_hold = m_hold_next) {
999 			m_hold_next = m_hold->m_nextpkt;
1000 			m_hold->m_nextpkt = NULL;
1001 			/* Avoid confusing lower layers. */
1002 			m_clrprotoflags(m_hold);
1003 			(*ifp->if_output)(ifp, m_hold, &sa, NULL);
1004 		}
1005 	} else
1006 		LLE_WUNLOCK(la);
1007 }
1008 
1009 /*
1010  * Updates @la fields used by fast path code.
1011  */
1012 static void
1013 arp_update_lle(struct arphdr *ah, struct ifnet *ifp, struct llentry *la)
1014 {
1015 
1016 	memcpy(&la->ll_addr, ar_sha(ah), ifp->if_addrlen);
1017 	la->la_flags |= LLE_VALID;
1018 }
1019 
1020 static void
1021 arp_mark_lle_reachable(struct llentry *la)
1022 {
1023 	int canceled;
1024 
1025 	LLE_WLOCK_ASSERT(la);
1026 
1027 	EVENTHANDLER_INVOKE(lle_event, la, LLENTRY_RESOLVED);
1028 
1029 	if (!(la->la_flags & LLE_STATIC)) {
1030 		LLE_ADDREF(la);
1031 		la->la_expire = time_uptime + V_arpt_keep;
1032 		canceled = callout_reset(&la->lle_timer,
1033 		    hz * V_arpt_keep, arptimer, la);
1034 		if (canceled)
1035 			LLE_REMREF(la);
1036 	}
1037 	la->la_asked = 0;
1038 	la->la_preempt = V_arp_maxtries;
1039 }
1040 
1041 void
1042 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1043 {
1044 	struct llentry *lle;
1045 
1046 	if (ifa->ifa_carp != NULL)
1047 		return;
1048 
1049 	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) {
1050 		arprequest(ifp, &IA_SIN(ifa)->sin_addr,
1051 				&IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
1052 		/*
1053 		 * interface address is considered static entry
1054 		 * because the output of the arp utility shows
1055 		 * that L2 entry as permanent
1056 		 */
1057 		IF_AFDATA_LOCK(ifp);
1058 		lle = lla_create(LLTABLE(ifp), LLE_IFADDR | LLE_STATIC,
1059 				 (struct sockaddr *)IA_SIN(ifa));
1060 		IF_AFDATA_UNLOCK(ifp);
1061 		if (lle == NULL)
1062 			log(LOG_INFO, "arp_ifinit: cannot create arp "
1063 			    "entry for interface address\n");
1064 		else
1065 			LLE_WUNLOCK(lle);
1066 	}
1067 	ifa->ifa_rtrequest = NULL;
1068 }
1069 
1070 void
1071 arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr)
1072 {
1073 	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
1074 		arprequest(ifp, &IA_SIN(ifa)->sin_addr,
1075 				&IA_SIN(ifa)->sin_addr, enaddr);
1076 	ifa->ifa_rtrequest = NULL;
1077 }
1078 
1079 static void
1080 arp_init(void)
1081 {
1082 
1083 	netisr_register(&arp_nh);
1084 }
1085 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);
1086