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