if_ether.c revision 152188
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 152188 2005-11-08 12:05:57Z glebius $
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, "");
85SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
86	   &arpt_keep, 0, "");
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, "");
110SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
111	   &useloopback, 0, "");
112SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
113	   &arp_proxyall, 0, "");
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;
573
574SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
575	&log_arp_wrong_iface, 0,
576	"log arp packets arriving on the wrong interface");
577SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
578        &log_arp_movements, 0,
579        "log arp replies from MACs different than the one in the cache");
580
581
582static void
583in_arpinput(m)
584	struct mbuf *m;
585{
586	struct arphdr *ah;
587	struct ifnet *ifp = m->m_pkthdr.rcvif;
588	struct iso88025_header *th = (struct iso88025_header *)0;
589	struct iso88025_sockaddr_dl_data *trld;
590	struct llinfo_arp *la;
591	struct rtentry *rt;
592	struct ifaddr *ifa;
593	struct in_ifaddr *ia;
594	struct sockaddr_dl *sdl;
595	struct sockaddr sa;
596	struct in_addr isaddr, itaddr, myaddr;
597	struct mbuf *hold;
598	u_int8_t *enaddr = NULL;
599	int op, rif_len;
600	int req_len;
601	int bridged = 0;
602#ifdef DEV_CARP
603	int carp_match = 0;
604#endif
605
606	if (ifp->if_bridge)
607		bridged = 1;
608
609	req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
610	if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
611		log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
612		return;
613	}
614
615	ah = mtod(m, struct arphdr *);
616	op = ntohs(ah->ar_op);
617	(void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
618	(void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
619
620	/*
621	 * For a bridge, we want to check the address irrespective
622	 * of the receive interface. (This will change slightly
623	 * when we have clusters of interfaces).
624	 * If the interface does not match, but the recieving interface
625	 * is part of carp, we call carp_iamatch to see if this is a
626	 * request for the virtual host ip.
627	 * XXX: This is really ugly!
628	 */
629	LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
630		if ((bridged || (ia->ia_ifp == ifp)) &&
631		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
632			goto match;
633#ifdef DEV_CARP
634		if (ifp->if_carp != NULL &&
635		    carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) &&
636		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
637			carp_match = 1;
638			goto match;
639		}
640#endif
641	}
642	LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
643		if ((bridged || (ia->ia_ifp == ifp)) &&
644		    isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
645			goto match;
646	/*
647	 * No match, use the first inet address on the receive interface
648	 * as a dummy address for the rest of the function.
649	 */
650	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
651		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
652			ia = ifatoia(ifa);
653			goto match;
654		}
655	/*
656	 * If bridging, fall back to using any inet address.
657	 */
658	if (!bridged || (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL)
659		goto drop;
660match:
661	if (!enaddr)
662		enaddr = (u_int8_t *)IF_LLADDR(ifp);
663	myaddr = ia->ia_addr.sin_addr;
664	if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
665		goto drop;	/* it's from me, ignore it. */
666	if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
667		log(LOG_ERR,
668		    "arp: link address is broadcast for IP address %s!\n",
669		    inet_ntoa(isaddr));
670		goto drop;
671	}
672	/*
673	 * Warn if another host is using the same IP address, but only if the
674	 * IP address isn't 0.0.0.0, which is used for DHCP only, in which
675	 * case we suppress the warning to avoid false positive complaints of
676	 * potential misconfiguration.
677	 */
678	if (!bridged && isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
679		log(LOG_ERR,
680		   "arp: %*D is using my IP address %s!\n",
681		   ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
682		   inet_ntoa(isaddr));
683		itaddr = myaddr;
684		goto reply;
685	}
686	if (ifp->if_flags & IFF_STATICARP)
687		goto reply;
688	rt = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
689	if (rt != NULL) {
690		la = (struct llinfo_arp *)rt->rt_llinfo;
691		if (la == NULL) {
692			RT_UNLOCK(rt);
693			goto reply;
694		}
695	} else
696		goto reply;
697
698	/* The following is not an error when doing bridging. */
699	if (!bridged && rt->rt_ifp != ifp
700#ifdef DEV_CARP
701	    && (ifp->if_type != IFT_CARP || !carp_match)
702#endif
703							) {
704		if (log_arp_wrong_iface)
705			log(LOG_ERR, "arp: %s is on %s but got reply from %*D on %s\n",
706			    inet_ntoa(isaddr),
707			    rt->rt_ifp->if_xname,
708			    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
709			    ifp->if_xname);
710		RT_UNLOCK(rt);
711		goto reply;
712	}
713	sdl = SDL(rt->rt_gateway);
714	if (sdl->sdl_alen &&
715	    bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
716		if (rt->rt_expire) {
717		    if (log_arp_movements)
718		        log(LOG_INFO, "arp: %s moved from %*D to %*D on %s\n",
719			    inet_ntoa(isaddr),
720			    ifp->if_addrlen, (u_char *)LLADDR(sdl), ":",
721			    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
722			    ifp->if_xname);
723		} else {
724		    log(LOG_ERR,
725			"arp: %*D attempts to modify permanent entry for %s on %s\n",
726			ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
727			inet_ntoa(isaddr), ifp->if_xname);
728		    RT_UNLOCK(rt);
729		    goto reply;
730		}
731	}
732	/*
733	 * sanity check for the address length.
734	 * XXX this does not work for protocols with variable address
735	 * length. -is
736	 */
737	if (sdl->sdl_alen &&
738	    sdl->sdl_alen != ah->ar_hln) {
739		log(LOG_WARNING,
740		    "arp from %*D: new addr len %d, was %d",
741		    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
742		    ah->ar_hln, sdl->sdl_alen);
743	}
744	if (ifp->if_addrlen != ah->ar_hln) {
745		log(LOG_WARNING,
746		    "arp from %*D: addr len: new %d, i/f %d (ignored)",
747		    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
748		    ah->ar_hln, ifp->if_addrlen);
749		RT_UNLOCK(rt);
750		goto reply;
751	}
752	(void)memcpy(LLADDR(sdl), ar_sha(ah),
753	    sdl->sdl_alen = ah->ar_hln);
754	/*
755	 * If we receive an arp from a token-ring station over
756	 * a token-ring nic then try to save the source
757	 * routing info.
758	 */
759	if (ifp->if_type == IFT_ISO88025) {
760		th = (struct iso88025_header *)m->m_pkthdr.header;
761		trld = SDL_ISO88025(sdl);
762		rif_len = TR_RCF_RIFLEN(th->rcf);
763		if ((th->iso88025_shost[0] & TR_RII) &&
764		    (rif_len > 2)) {
765			trld->trld_rcf = th->rcf;
766			trld->trld_rcf ^= htons(TR_RCF_DIR);
767			memcpy(trld->trld_route, th->rd, rif_len - 2);
768			trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
769			/*
770			 * Set up source routing information for
771			 * reply packet (XXX)
772			 */
773			m->m_data -= rif_len;
774			m->m_len  += rif_len;
775			m->m_pkthdr.len += rif_len;
776		} else {
777			th->iso88025_shost[0] &= ~TR_RII;
778			trld->trld_rcf = 0;
779		}
780		m->m_data -= 8;
781		m->m_len  += 8;
782		m->m_pkthdr.len += 8;
783		th->rcf = trld->trld_rcf;
784	}
785	if (rt->rt_expire)
786		rt->rt_expire = time_uptime + arpt_keep;
787	la->la_asked = 0;
788	la->la_preempt = arp_maxtries;
789	hold = la->la_hold;
790	la->la_hold = NULL;
791	RT_UNLOCK(rt);
792	if (hold != NULL)
793		(*ifp->if_output)(ifp, hold, rt_key(rt), rt);
794
795reply:
796	if (op != ARPOP_REQUEST)
797		goto drop;
798	if (itaddr.s_addr == myaddr.s_addr) {
799		/* I am the target */
800		(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
801		(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
802	} else {
803		rt = arplookup(itaddr.s_addr, 0, SIN_PROXY);
804		if (rt == NULL) {
805			struct sockaddr_in sin;
806
807			if (!arp_proxyall)
808				goto drop;
809
810			bzero(&sin, sizeof sin);
811			sin.sin_family = AF_INET;
812			sin.sin_len = sizeof sin;
813			sin.sin_addr = itaddr;
814
815			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
816			if (!rt)
817				goto drop;
818			/*
819			 * Don't send proxies for nodes on the same interface
820			 * as this one came out of, or we'll get into a fight
821			 * over who claims what Ether address.
822			 */
823			if (rt->rt_ifp == ifp) {
824				rtfree(rt);
825				goto drop;
826			}
827			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
828			(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
829			rtfree(rt);
830
831			/*
832			 * Also check that the node which sent the ARP packet
833			 * is on the the interface we expect it to be on. This
834			 * avoids ARP chaos if an interface is connected to the
835			 * wrong network.
836			 */
837			sin.sin_addr = isaddr;
838
839			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
840			if (!rt)
841				goto drop;
842			if (rt->rt_ifp != ifp) {
843				log(LOG_INFO, "arp_proxy: ignoring request"
844				    " from %s via %s, expecting %s\n",
845				    inet_ntoa(isaddr), ifp->if_xname,
846				    rt->rt_ifp->if_xname);
847				rtfree(rt);
848				goto drop;
849			}
850			rtfree(rt);
851
852#ifdef DEBUG_PROXY
853			printf("arp: proxying for %s\n",
854			       inet_ntoa(itaddr));
855#endif
856		} else {
857			/*
858			 * Return proxied ARP replies only on the interface
859			 * where this network resides. Otherwise we may
860			 * conflict with the host we are proxying for.
861			 */
862			if (rt->rt_ifp != ifp) {
863				RT_UNLOCK(rt);
864				goto drop;
865			}
866			sdl = SDL(rt->rt_gateway);
867			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
868			(void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
869			RT_UNLOCK(rt);
870		}
871	}
872
873	(void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
874	(void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
875	ah->ar_op = htons(ARPOP_REPLY);
876	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
877	m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
878	m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
879	m->m_pkthdr.len = m->m_len;
880	sa.sa_family = AF_ARP;
881	sa.sa_len = 2;
882	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
883	return;
884
885drop:
886	m_freem(m);
887}
888#endif
889
890/*
891 * Lookup or enter a new address in arptab.
892 */
893static struct rtentry *
894arplookup(addr, create, proxy)
895	u_long addr;
896	int create, proxy;
897{
898	struct rtentry *rt;
899	struct sockaddr_inarp sin;
900	const char *why = 0;
901
902	bzero(&sin, sizeof(sin));
903	sin.sin_len = sizeof(sin);
904	sin.sin_family = AF_INET;
905	sin.sin_addr.s_addr = addr;
906	if (proxy)
907		sin.sin_other = SIN_PROXY;
908	rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
909	if (rt == 0)
910		return (0);
911
912	if (rt->rt_flags & RTF_GATEWAY)
913		why = "host is not on local network";
914	else if ((rt->rt_flags & RTF_LLINFO) == 0)
915		why = "could not allocate llinfo";
916	else if (rt->rt_gateway->sa_family != AF_LINK)
917		why = "gateway route is not ours";
918
919	if (why) {
920#define	ISDYNCLONE(_rt) \
921	(((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED)
922		if (create)
923			log(LOG_DEBUG, "arplookup %s failed: %s\n",
924			    inet_ntoa(sin.sin_addr), why);
925		/*
926		 * If there are no references to this Layer 2 route,
927		 * and it is a cloned route, and not static, and
928		 * arplookup() is creating the route, then purge
929		 * it from the routing table as it is probably bogus.
930		 */
931		if (rt->rt_refcnt == 1 && ISDYNCLONE(rt))
932			rtexpunge(rt);
933		RTFREE_LOCKED(rt);
934		return (0);
935#undef ISDYNCLONE
936	} else {
937		RT_REMREF(rt);
938		return (rt);
939	}
940}
941
942void
943arp_ifinit(ifp, ifa)
944	struct ifnet *ifp;
945	struct ifaddr *ifa;
946{
947	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
948		arprequest(ifp, &IA_SIN(ifa)->sin_addr,
949				&IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
950	ifa->ifa_rtrequest = arp_rtrequest;
951	ifa->ifa_flags |= RTF_CLONING;
952}
953
954void
955arp_ifinit2(ifp, ifa, enaddr)
956	struct ifnet *ifp;
957	struct ifaddr *ifa;
958	u_char *enaddr;
959{
960	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
961		arprequest(ifp, &IA_SIN(ifa)->sin_addr,
962				&IA_SIN(ifa)->sin_addr, enaddr);
963	ifa->ifa_rtrequest = arp_rtrequest;
964	ifa->ifa_flags |= RTF_CLONING;
965}
966
967static void
968arp_init(void)
969{
970
971	arpintrq.ifq_maxlen = 50;
972	mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF);
973	LIST_INIT(&llinfo_arp);
974	callout_init(&arp_callout, CALLOUT_MPSAFE);
975	netisr_register(NETISR_ARP, arpintr, &arpintrq, NETISR_MPSAFE);
976	callout_reset(&arp_callout, hz, arptimer, NULL);
977}
978SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);
979