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