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