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