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