if_ether.c revision 156409
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 * $FreeBSD: head/sys/netinet/if_ether.c 156409 2006-03-07 21:40:44Z thompsa $
31 */
32
33/*
34 * Ethernet address resolution protocol.
35 * TODO:
36 *	add "inuse/lock" bit (or ref. count) along with valid bit
37 */
38
39#include "opt_inet.h"
40#include "opt_mac.h"
41#include "opt_carp.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/mac.h>
49#include <sys/mbuf.h>
50#include <sys/malloc.h>
51#include <sys/socket.h>
52#include <sys/syslog.h>
53
54#include <net/if.h>
55#include <net/if_dl.h>
56#include <net/if_types.h>
57#include <net/route.h>
58#include <net/netisr.h>
59#include <net/if_llc.h>
60#include <net/ethernet.h>
61
62#include <netinet/in.h>
63#include <netinet/in_var.h>
64#include <netinet/if_ether.h>
65
66#include <net/if_arc.h>
67#include <net/iso88025.h>
68
69#ifdef DEV_CARP
70#include <netinet/ip_carp.h>
71#endif
72
73#define SIN(s) ((struct sockaddr_in *)s)
74#define SDL(s) ((struct sockaddr_dl *)s)
75
76SYSCTL_DECL(_net_link_ether);
77SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
78
79/* timer values */
80static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
81static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
82
83SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
84	   &arpt_prune, 0, "ARP table prune interval in seconds");
85SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
86	   &arpt_keep, 0, "ARP entry lifetime in seconds");
87
88#define	rt_expire rt_rmx.rmx_expire
89
90struct llinfo_arp {
91	LIST_ENTRY(llinfo_arp) la_le;
92	struct	rtentry *la_rt;
93	struct	mbuf *la_hold;	/* last packet until resolved/timeout */
94	u_short	la_preempt;	/* countdown for pre-expiry arps */
95	u_short	la_asked;	/* # requests sent */
96};
97
98static	LIST_HEAD(, llinfo_arp) llinfo_arp;
99
100static struct	ifqueue arpintrq;
101static int	arp_allocated;
102
103static int	arp_maxtries = 5;
104static int	useloopback = 1; /* use loopback interface for local traffic */
105static int	arp_proxyall = 0;
106static struct callout arp_callout;
107
108SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
109	   &arp_maxtries, 0, "ARP resolution attempts before returning error");
110SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
111	   &useloopback, 0, "Use the loopback interface for local traffic");
112SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
113	   &arp_proxyall, 0, "Enable proxy ARP for all suitable requests");
114
115static void	arp_init(void);
116static void	arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
117static void	arprequest(struct ifnet *,
118			struct in_addr *, struct in_addr *, u_char *);
119static void	arpintr(struct mbuf *);
120static void	arptimer(void *);
121static struct rtentry
122		*arplookup(u_long, int, int);
123#ifdef INET
124static void	in_arpinput(struct mbuf *);
125#endif
126
127/*
128 * Timeout routine.  Age arp_tab entries periodically.
129 */
130/* ARGSUSED */
131static void
132arptimer(void * __unused unused)
133{
134	struct llinfo_arp *la, *ola;
135
136	RADIX_NODE_HEAD_LOCK(rt_tables[AF_INET]);
137	LIST_FOREACH_SAFE(la, &llinfo_arp, la_le, ola) {
138		struct rtentry *rt = la->la_rt;
139
140		RT_LOCK(rt);
141		if (rt->rt_expire && rt->rt_expire <= time_uptime) {
142			struct sockaddr_dl *sdl = SDL(rt->rt_gateway);
143
144			KASSERT(sdl->sdl_family == AF_LINK, ("sdl_family %d",
145			    sdl->sdl_family));
146			if (rt->rt_refcnt > 1) {
147				sdl->sdl_alen = 0;
148				la->la_preempt = la->la_asked = 0;
149				RT_UNLOCK(rt);
150				continue;
151			}
152			RT_UNLOCK(rt);
153			/*
154			 * XXX: LIST_REMOVE() is deep inside rtrequest().
155			 */
156			rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0,
157			    NULL);
158			continue;
159		}
160		RT_UNLOCK(rt);
161	}
162	RADIX_NODE_HEAD_UNLOCK(rt_tables[AF_INET]);
163
164	callout_reset(&arp_callout, arpt_prune * hz, arptimer, NULL);
165}
166
167/*
168 * Parallel to llc_rtrequest.
169 */
170static void
171arp_rtrequest(req, rt, info)
172	int req;
173	struct rtentry *rt;
174	struct rt_addrinfo *info;
175{
176	struct sockaddr *gate;
177	struct llinfo_arp *la;
178	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
179	struct in_ifaddr *ia;
180	struct ifaddr *ifa;
181
182	RT_LOCK_ASSERT(rt);
183
184	if (rt->rt_flags & RTF_GATEWAY)
185		return;
186	gate = rt->rt_gateway;
187	la = (struct llinfo_arp *)rt->rt_llinfo;
188	switch (req) {
189
190	case RTM_ADD:
191		/*
192		 * XXX: If this is a manually added route to interface
193		 * such as older version of routed or gated might provide,
194		 * restore cloning bit.
195		 */
196		if ((rt->rt_flags & RTF_HOST) == 0 &&
197		    rt_mask(rt) != NULL &&
198		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
199			rt->rt_flags |= RTF_CLONING;
200		if (rt->rt_flags & RTF_CLONING) {
201			/*
202			 * Case 1: This route should come from a route to iface.
203			 */
204			rt_setgate(rt, rt_key(rt),
205					(struct sockaddr *)&null_sdl);
206			gate = rt->rt_gateway;
207			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
208			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
209			rt->rt_expire = time_uptime;
210			break;
211		}
212		/* Announce a new entry if requested. */
213		if (rt->rt_flags & RTF_ANNOUNCE)
214			arprequest(rt->rt_ifp,
215			    &SIN(rt_key(rt))->sin_addr,
216			    &SIN(rt_key(rt))->sin_addr,
217			    (u_char *)LLADDR(SDL(gate)));
218		/*FALLTHROUGH*/
219	case RTM_RESOLVE:
220		if (gate->sa_family != AF_LINK ||
221		    gate->sa_len < sizeof(null_sdl)) {
222			log(LOG_DEBUG, "%s: bad gateway %s%s\n", __func__,
223			    inet_ntoa(SIN(rt_key(rt))->sin_addr),
224			    (gate->sa_family != AF_LINK) ?
225			    " (!AF_LINK)": "");
226			break;
227		}
228		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
229		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
230		if (la != 0)
231			break; /* This happens on a route change */
232		/*
233		 * Case 2:  This route may come from cloning, or a manual route
234		 * add with a LL address.
235		 */
236		R_Zalloc(la, struct llinfo_arp *, sizeof(*la));
237		rt->rt_llinfo = (caddr_t)la;
238		if (la == 0) {
239			log(LOG_DEBUG, "%s: malloc failed\n", __func__);
240			break;
241		}
242		arp_allocated++;
243		/*
244		 * We are storing a route entry outside of radix tree. So,
245		 * it can be found and accessed by other means than radix
246		 * lookup. The routing code assumes that any rtentry detached
247		 * from radix can be destroyed safely. To prevent this, we
248		 * add an additional reference.
249		 */
250		RT_ADDREF(rt);
251		la->la_rt = rt;
252		rt->rt_flags |= RTF_LLINFO;
253		RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]);
254		LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
255
256#ifdef INET
257		/*
258		 * This keeps the multicast addresses from showing up
259		 * in `arp -a' listings as unresolved.  It's not actually
260		 * functional.  Then the same for broadcast.
261		 */
262		if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) &&
263		    rt->rt_ifp->if_type != IFT_ARCNET) {
264			ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
265					       LLADDR(SDL(gate)));
266			SDL(gate)->sdl_alen = 6;
267			rt->rt_expire = 0;
268		}
269		if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
270			memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
271			       rt->rt_ifp->if_addrlen);
272			SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
273			rt->rt_expire = 0;
274		}
275#endif
276
277		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
278			if (ia->ia_ifp == rt->rt_ifp &&
279			    SIN(rt_key(rt))->sin_addr.s_addr ==
280			    (IA_SIN(ia))->sin_addr.s_addr)
281				break;
282		}
283		if (ia) {
284		    /*
285		     * This test used to be
286		     *	if (loif.if_flags & IFF_UP)
287		     * It allowed local traffic to be forced
288		     * through the hardware by configuring the loopback down.
289		     * However, it causes problems during network configuration
290		     * for boards that can't receive packets they send.
291		     * It is now necessary to clear "useloopback" and remove
292		     * the route to force traffic out to the hardware.
293		     */
294			rt->rt_expire = 0;
295			bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
296			      SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
297			if (useloopback)
298				rt->rt_ifp = loif;
299
300		    /*
301		     * make sure to set rt->rt_ifa to the interface
302		     * address we are using, otherwise we will have trouble
303		     * with source address selection.
304		     */
305			ifa = &ia->ia_ifa;
306			if (ifa != rt->rt_ifa) {
307				IFAFREE(rt->rt_ifa);
308				IFAREF(ifa);
309				rt->rt_ifa = ifa;
310			}
311		}
312		break;
313
314	case RTM_DELETE:
315		if (la == 0)
316			break;
317		RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]);
318		LIST_REMOVE(la, la_le);
319		RT_REMREF(rt);
320		rt->rt_llinfo = 0;
321		rt->rt_flags &= ~RTF_LLINFO;
322		if (la->la_hold)
323			m_freem(la->la_hold);
324		Free((caddr_t)la);
325	}
326}
327
328/*
329 * Broadcast an ARP request. Caller specifies:
330 *	- arp header source ip address
331 *	- arp header target ip address
332 *	- arp header source ethernet address
333 */
334static void
335arprequest(ifp, sip, tip, enaddr)
336	struct ifnet *ifp;
337	struct in_addr *sip, *tip;
338	u_char *enaddr;
339{
340	struct mbuf *m;
341	struct arphdr *ah;
342	struct sockaddr sa;
343
344	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
345		return;
346	m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
347		2*ifp->if_data.ifi_addrlen;
348	m->m_pkthdr.len = m->m_len;
349	MH_ALIGN(m, m->m_len);
350	ah = mtod(m, struct arphdr *);
351	bzero((caddr_t)ah, m->m_len);
352#ifdef MAC
353	mac_create_mbuf_linklayer(ifp, m);
354#endif
355	ah->ar_pro = htons(ETHERTYPE_IP);
356	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
357	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
358	ah->ar_op = htons(ARPOP_REQUEST);
359	bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
360	bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
361	bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
362	sa.sa_family = AF_ARP;
363	sa.sa_len = 2;
364	m->m_flags |= M_BCAST;
365	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
366
367	return;
368}
369
370/*
371 * Resolve an IP address into an ethernet address.
372 * On input:
373 *    ifp is the interface we use
374 *    dst is the next hop,
375 *    rt0 is the route to the final destination (possibly useless)
376 *    m is the mbuf
377 *    desten is where we want the address.
378 *
379 * On success, desten is filled in and the function returns 0;
380 * If the packet must be held pending resolution, we return EWOULDBLOCK
381 * On other errors, we return the corresponding error code.
382 */
383int
384arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
385	struct sockaddr *dst, u_char *desten)
386{
387	struct llinfo_arp *la = NULL;
388	struct rtentry *rt = NULL;
389	struct sockaddr_dl *sdl;
390	int error;
391
392	if (m->m_flags & M_BCAST) {	/* broadcast */
393		(void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
394		return (0);
395	}
396	if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */
397		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
398		return (0);
399	}
400
401	if (rt0 != NULL) {
402		error = rt_check(&rt, &rt0, dst);
403		if (error) {
404			m_freem(m);
405			return error;
406		}
407		la = (struct llinfo_arp *)rt->rt_llinfo;
408		if (la == NULL)
409			RT_UNLOCK(rt);
410	}
411	if (la == NULL) {
412		/*
413		 * We enter this block in case if rt0 was NULL,
414		 * or if rt found by rt_check() didn't have llinfo.
415		 */
416		rt = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
417		if (rt == NULL) {
418			log(LOG_DEBUG,
419			    "arpresolve: can't allocate route for %s\n",
420			    inet_ntoa(SIN(dst)->sin_addr));
421			m_freem(m);
422			return (EINVAL); /* XXX */
423		}
424		la = (struct llinfo_arp *)rt->rt_llinfo;
425		if (la == NULL) {
426			RT_UNLOCK(rt);
427			log(LOG_DEBUG,
428			    "arpresolve: can't allocate llinfo for %s\n",
429			    inet_ntoa(SIN(dst)->sin_addr));
430			m_freem(m);
431			return (EINVAL); /* XXX */
432		}
433	}
434	sdl = SDL(rt->rt_gateway);
435	/*
436	 * Check the address family and length is valid, the address
437	 * is resolved; otherwise, try to resolve.
438	 */
439	if ((rt->rt_expire == 0 || rt->rt_expire > time_uptime) &&
440	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
441
442		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
443
444		/*
445		 * If entry has an expiry time and it is approaching,
446		 * send an ARP request.
447		 */
448		if ((rt->rt_expire != 0) &&
449		    (time_uptime + la->la_preempt > rt->rt_expire)) {
450			struct in_addr sin =
451			    SIN(rt->rt_ifa->ifa_addr)->sin_addr;
452
453			la->la_preempt--;
454			RT_UNLOCK(rt);
455			arprequest(ifp, &sin, &SIN(dst)->sin_addr,
456			    IF_LLADDR(ifp));
457			return (0);
458		}
459
460		RT_UNLOCK(rt);
461		return (0);
462	}
463	/*
464	 * If ARP is disabled or static on this interface, stop.
465	 * XXX
466	 * Probably should not allocate empty llinfo struct if we are
467	 * not going to be sending out an arp request.
468	 */
469	if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
470		RT_UNLOCK(rt);
471		m_freem(m);
472		return (EINVAL);
473	}
474	/*
475	 * There is an arptab entry, but no ethernet address
476	 * response yet.  Replace the held mbuf with this
477	 * latest one.
478	 */
479	if (la->la_hold)
480		m_freem(la->la_hold);
481	la->la_hold = m;
482
483	KASSERT(rt->rt_expire > 0, ("sending ARP request for static entry"));
484
485	/*
486	 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
487	 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
488	 * if we have already sent arp_maxtries ARP requests. Retransmit the
489	 * ARP request, but not faster than one request per second.
490	 */
491	if (la->la_asked < arp_maxtries)
492		error = EWOULDBLOCK;	/* First request. */
493	else
494		error = (rt == rt0) ? EHOSTDOWN : EHOSTUNREACH;
495
496	if (la->la_asked++ == 0 || rt->rt_expire != time_uptime) {
497		struct in_addr sin =
498		    SIN(rt->rt_ifa->ifa_addr)->sin_addr;
499
500		rt->rt_expire = time_uptime;
501		RT_UNLOCK(rt);
502
503		arprequest(ifp, &sin, &SIN(dst)->sin_addr,
504		    IF_LLADDR(ifp));
505	} else
506		RT_UNLOCK(rt);
507
508	return (error);
509}
510
511/*
512 * Common length and type checks are done here,
513 * then the protocol-specific routine is called.
514 */
515static void
516arpintr(struct mbuf *m)
517{
518	struct arphdr *ar;
519
520	if (m->m_len < sizeof(struct arphdr) &&
521	    ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
522		log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
523		return;
524	}
525	ar = mtod(m, struct arphdr *);
526
527	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER &&
528	    ntohs(ar->ar_hrd) != ARPHRD_IEEE802 &&
529	    ntohs(ar->ar_hrd) != ARPHRD_ARCNET &&
530	    ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) {
531		log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
532		    (unsigned char *)&ar->ar_hrd, "");
533		m_freem(m);
534		return;
535	}
536
537	if (m->m_len < arphdr_len(ar)) {
538		if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
539			log(LOG_ERR, "arp: runt packet\n");
540			m_freem(m);
541			return;
542		}
543		ar = mtod(m, struct arphdr *);
544	}
545
546	switch (ntohs(ar->ar_pro)) {
547#ifdef INET
548	case ETHERTYPE_IP:
549		in_arpinput(m);
550		return;
551#endif
552	}
553	m_freem(m);
554}
555
556#ifdef INET
557/*
558 * ARP for Internet protocols on 10 Mb/s Ethernet.
559 * Algorithm is that given in RFC 826.
560 * In addition, a sanity check is performed on the sender
561 * protocol address, to catch impersonators.
562 * We no longer handle negotiations for use of trailer protocol:
563 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
564 * along with IP replies if we wanted trailers sent to us,
565 * and also sent them in response to IP replies.
566 * This allowed either end to announce the desire to receive
567 * trailer packets.
568 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
569 * but formerly didn't normally send requests.
570 */
571static int log_arp_wrong_iface = 1;
572static int log_arp_movements = 1;
573static int log_arp_permanent_modify = 1;
574
575SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
576	&log_arp_wrong_iface, 0,
577	"log arp packets arriving on the wrong interface");
578SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
579        &log_arp_movements, 0,
580        "log arp replies from MACs different than the one in the cache");
581SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
582        &log_arp_permanent_modify, 0,
583        "log arp replies from MACs different than the one in the permanent arp entry");
584
585
586static void
587in_arpinput(m)
588	struct mbuf *m;
589{
590	struct arphdr *ah;
591	struct ifnet *ifp = m->m_pkthdr.rcvif;
592	struct iso88025_header *th = (struct iso88025_header *)0;
593	struct iso88025_sockaddr_dl_data *trld;
594	struct llinfo_arp *la;
595	struct rtentry *rt;
596	struct ifaddr *ifa;
597	struct in_ifaddr *ia;
598	struct sockaddr_dl *sdl;
599	struct sockaddr sa;
600	struct in_addr isaddr, itaddr, myaddr;
601	struct mbuf *hold;
602	u_int8_t *enaddr = NULL;
603	int op, rif_len;
604	int req_len;
605	int bridged = 0;
606#ifdef DEV_CARP
607	int carp_match = 0;
608#endif
609
610	if (ifp->if_bridge)
611		bridged = 1;
612
613	req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
614	if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
615		log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
616		return;
617	}
618
619	ah = mtod(m, struct arphdr *);
620	op = ntohs(ah->ar_op);
621	(void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
622	(void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
623
624	/*
625	 * For a bridge, we want to check the address irrespective
626	 * of the receive interface. (This will change slightly
627	 * when we have clusters of interfaces).
628	 * If the interface does not match, but the recieving interface
629	 * is part of carp, we call carp_iamatch to see if this is a
630	 * request for the virtual host ip.
631	 * XXX: This is really ugly!
632	 */
633	LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
634		if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
635		    (ia->ia_ifp == ifp)) &&
636		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
637			goto match;
638#ifdef DEV_CARP
639		if (ifp->if_carp != NULL &&
640		    carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) &&
641		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
642			carp_match = 1;
643			goto match;
644		}
645#endif
646	}
647	LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
648		if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
649		    (ia->ia_ifp == ifp)) &&
650		    isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
651			goto match;
652	/*
653	 * No match, use the first inet address on the receive interface
654	 * as a dummy address for the rest of the function.
655	 */
656	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
657		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
658			ia = ifatoia(ifa);
659			goto match;
660		}
661	/*
662	 * If bridging, fall back to using any inet address.
663	 */
664	if (!bridged || (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL)
665		goto drop;
666match:
667	if (!enaddr)
668		enaddr = (u_int8_t *)IF_LLADDR(ifp);
669	myaddr = ia->ia_addr.sin_addr;
670	if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
671		goto drop;	/* it's from me, ignore it. */
672	if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
673		log(LOG_ERR,
674		    "arp: link address is broadcast for IP address %s!\n",
675		    inet_ntoa(isaddr));
676		goto drop;
677	}
678	/*
679	 * Warn if another host is using the same IP address, but only if the
680	 * IP address isn't 0.0.0.0, which is used for DHCP only, in which
681	 * case we suppress the warning to avoid false positive complaints of
682	 * potential misconfiguration.
683	 */
684	if (!bridged && isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
685		log(LOG_ERR,
686		   "arp: %*D is using my IP address %s!\n",
687		   ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
688		   inet_ntoa(isaddr));
689		itaddr = myaddr;
690		goto reply;
691	}
692	if (ifp->if_flags & IFF_STATICARP)
693		goto reply;
694	rt = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
695	if (rt != NULL) {
696		la = (struct llinfo_arp *)rt->rt_llinfo;
697		if (la == NULL) {
698			RT_UNLOCK(rt);
699			goto reply;
700		}
701	} else
702		goto reply;
703
704	/* The following is not an error when doing bridging. */
705	if (!bridged && rt->rt_ifp != ifp
706#ifdef DEV_CARP
707	    && (ifp->if_type != IFT_CARP || !carp_match)
708#endif
709							) {
710		if (log_arp_wrong_iface)
711			log(LOG_ERR, "arp: %s is on %s but got reply from %*D on %s\n",
712			    inet_ntoa(isaddr),
713			    rt->rt_ifp->if_xname,
714			    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
715			    ifp->if_xname);
716		RT_UNLOCK(rt);
717		goto reply;
718	}
719	sdl = SDL(rt->rt_gateway);
720	if (sdl->sdl_alen &&
721	    bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
722		if (rt->rt_expire) {
723		    if (log_arp_movements)
724		        log(LOG_INFO, "arp: %s moved from %*D to %*D on %s\n",
725			    inet_ntoa(isaddr),
726			    ifp->if_addrlen, (u_char *)LLADDR(sdl), ":",
727			    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
728			    ifp->if_xname);
729		} else {
730			RT_UNLOCK(rt);
731			if (log_arp_permanent_modify)
732				log(LOG_ERR, "arp: %*D attempts to modify "
733				    "permanent entry for %s on %s\n",
734				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
735				    inet_ntoa(isaddr), ifp->if_xname);
736			goto reply;
737		}
738	}
739	/*
740	 * sanity check for the address length.
741	 * XXX this does not work for protocols with variable address
742	 * length. -is
743	 */
744	if (sdl->sdl_alen &&
745	    sdl->sdl_alen != ah->ar_hln) {
746		log(LOG_WARNING,
747		    "arp from %*D: new addr len %d, was %d",
748		    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
749		    ah->ar_hln, sdl->sdl_alen);
750	}
751	if (ifp->if_addrlen != ah->ar_hln) {
752		log(LOG_WARNING,
753		    "arp from %*D: addr len: new %d, i/f %d (ignored)",
754		    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
755		    ah->ar_hln, ifp->if_addrlen);
756		RT_UNLOCK(rt);
757		goto reply;
758	}
759	(void)memcpy(LLADDR(sdl), ar_sha(ah),
760	    sdl->sdl_alen = ah->ar_hln);
761	/*
762	 * If we receive an arp from a token-ring station over
763	 * a token-ring nic then try to save the source
764	 * routing info.
765	 */
766	if (ifp->if_type == IFT_ISO88025) {
767		th = (struct iso88025_header *)m->m_pkthdr.header;
768		trld = SDL_ISO88025(sdl);
769		rif_len = TR_RCF_RIFLEN(th->rcf);
770		if ((th->iso88025_shost[0] & TR_RII) &&
771		    (rif_len > 2)) {
772			trld->trld_rcf = th->rcf;
773			trld->trld_rcf ^= htons(TR_RCF_DIR);
774			memcpy(trld->trld_route, th->rd, rif_len - 2);
775			trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
776			/*
777			 * Set up source routing information for
778			 * reply packet (XXX)
779			 */
780			m->m_data -= rif_len;
781			m->m_len  += rif_len;
782			m->m_pkthdr.len += rif_len;
783		} else {
784			th->iso88025_shost[0] &= ~TR_RII;
785			trld->trld_rcf = 0;
786		}
787		m->m_data -= 8;
788		m->m_len  += 8;
789		m->m_pkthdr.len += 8;
790		th->rcf = trld->trld_rcf;
791	}
792	if (rt->rt_expire)
793		rt->rt_expire = time_uptime + arpt_keep;
794	la->la_asked = 0;
795	la->la_preempt = arp_maxtries;
796	hold = la->la_hold;
797	la->la_hold = NULL;
798	RT_UNLOCK(rt);
799	if (hold != NULL)
800		(*ifp->if_output)(ifp, hold, rt_key(rt), rt);
801
802reply:
803	if (op != ARPOP_REQUEST)
804		goto drop;
805	if (itaddr.s_addr == myaddr.s_addr) {
806		/* I am the target */
807		(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
808		(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
809	} else {
810		rt = arplookup(itaddr.s_addr, 0, SIN_PROXY);
811		if (rt == NULL) {
812			struct sockaddr_in sin;
813
814			if (!arp_proxyall)
815				goto drop;
816
817			bzero(&sin, sizeof sin);
818			sin.sin_family = AF_INET;
819			sin.sin_len = sizeof sin;
820			sin.sin_addr = itaddr;
821
822			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
823			if (!rt)
824				goto drop;
825			/*
826			 * Don't send proxies for nodes on the same interface
827			 * as this one came out of, or we'll get into a fight
828			 * over who claims what Ether address.
829			 */
830			if (rt->rt_ifp == ifp) {
831				rtfree(rt);
832				goto drop;
833			}
834			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
835			(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
836			rtfree(rt);
837
838			/*
839			 * Also check that the node which sent the ARP packet
840			 * is on the the interface we expect it to be on. This
841			 * avoids ARP chaos if an interface is connected to the
842			 * wrong network.
843			 */
844			sin.sin_addr = isaddr;
845
846			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
847			if (!rt)
848				goto drop;
849			if (rt->rt_ifp != ifp) {
850				log(LOG_INFO, "arp_proxy: ignoring request"
851				    " from %s via %s, expecting %s\n",
852				    inet_ntoa(isaddr), ifp->if_xname,
853				    rt->rt_ifp->if_xname);
854				rtfree(rt);
855				goto drop;
856			}
857			rtfree(rt);
858
859#ifdef DEBUG_PROXY
860			printf("arp: proxying for %s\n",
861			       inet_ntoa(itaddr));
862#endif
863		} else {
864			/*
865			 * Return proxied ARP replies only on the interface
866			 * where this network resides. Otherwise we may
867			 * conflict with the host we are proxying for.
868			 */
869			if (rt->rt_ifp != ifp) {
870				RT_UNLOCK(rt);
871				goto drop;
872			}
873			sdl = SDL(rt->rt_gateway);
874			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
875			(void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
876			RT_UNLOCK(rt);
877		}
878	}
879
880	(void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
881	(void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
882	ah->ar_op = htons(ARPOP_REPLY);
883	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
884	m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
885	m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
886	m->m_pkthdr.len = m->m_len;
887	sa.sa_family = AF_ARP;
888	sa.sa_len = 2;
889	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
890	return;
891
892drop:
893	m_freem(m);
894}
895#endif
896
897/*
898 * Lookup or enter a new address in arptab.
899 */
900static struct rtentry *
901arplookup(addr, create, proxy)
902	u_long addr;
903	int create, proxy;
904{
905	struct rtentry *rt;
906	struct sockaddr_inarp sin;
907	const char *why = 0;
908
909	bzero(&sin, sizeof(sin));
910	sin.sin_len = sizeof(sin);
911	sin.sin_family = AF_INET;
912	sin.sin_addr.s_addr = addr;
913	if (proxy)
914		sin.sin_other = SIN_PROXY;
915	rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
916	if (rt == 0)
917		return (0);
918
919	if (rt->rt_flags & RTF_GATEWAY)
920		why = "host is not on local network";
921	else if ((rt->rt_flags & RTF_LLINFO) == 0)
922		why = "could not allocate llinfo";
923	else if (rt->rt_gateway->sa_family != AF_LINK)
924		why = "gateway route is not ours";
925
926	if (why) {
927#define	ISDYNCLONE(_rt) \
928	(((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED)
929		if (create)
930			log(LOG_DEBUG, "arplookup %s failed: %s\n",
931			    inet_ntoa(sin.sin_addr), why);
932		/*
933		 * If there are no references to this Layer 2 route,
934		 * and it is a cloned route, and not static, and
935		 * arplookup() is creating the route, then purge
936		 * it from the routing table as it is probably bogus.
937		 */
938		if (rt->rt_refcnt == 1 && ISDYNCLONE(rt))
939			rtexpunge(rt);
940		RTFREE_LOCKED(rt);
941		return (0);
942#undef ISDYNCLONE
943	} else {
944		RT_REMREF(rt);
945		return (rt);
946	}
947}
948
949void
950arp_ifinit(ifp, ifa)
951	struct ifnet *ifp;
952	struct ifaddr *ifa;
953{
954	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
955		arprequest(ifp, &IA_SIN(ifa)->sin_addr,
956				&IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
957	ifa->ifa_rtrequest = arp_rtrequest;
958	ifa->ifa_flags |= RTF_CLONING;
959}
960
961void
962arp_ifinit2(ifp, ifa, enaddr)
963	struct ifnet *ifp;
964	struct ifaddr *ifa;
965	u_char *enaddr;
966{
967	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
968		arprequest(ifp, &IA_SIN(ifa)->sin_addr,
969				&IA_SIN(ifa)->sin_addr, enaddr);
970	ifa->ifa_rtrequest = arp_rtrequest;
971	ifa->ifa_flags |= RTF_CLONING;
972}
973
974static void
975arp_init(void)
976{
977
978	arpintrq.ifq_maxlen = 50;
979	mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF);
980	LIST_INIT(&llinfo_arp);
981	callout_init(&arp_callout, CALLOUT_MPSAFE);
982	netisr_register(NETISR_ARP, arpintr, &arpintrq, NETISR_MPSAFE);
983	callout_reset(&arp_callout, hz, arptimer, NULL);
984}
985SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);
986