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