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