if_ether.c revision 148883
1114407Snyan/*-
2114407Snyan * Copyright (c) 1982, 1986, 1988, 1993
3114407Snyan *	The Regents of the University of California.  All rights reserved.
4114407Snyan *
5114407Snyan * Redistribution and use in source and binary forms, with or without
6114407Snyan * modification, are permitted provided that the following conditions
7114407Snyan * are met:
8114407Snyan * 1. Redistributions of source code must retain the above copyright
9114407Snyan *    notice, this list of conditions and the following disclaimer.
10114407Snyan * 2. Redistributions in binary form must reproduce the above copyright
11114407Snyan *    notice, this list of conditions and the following disclaimer in the
12114407Snyan *    documentation and/or other materials provided with the distribution.
13114407Snyan * 4. Neither the name of the University nor the names of its contributors
14114407Snyan *    may be used to endorse or promote products derived from this software
15114407Snyan *    without specific prior written permission.
16114407Snyan *
17114407Snyan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18114407Snyan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19114407Snyan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20114407Snyan * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21114407Snyan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22114407Snyan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23114407Snyan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24114407Snyan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25114407Snyan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26114407Snyan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27119880Sobrien * SUCH DAMAGE.
28119880Sobrien *
29119880Sobrien *	@(#)if_ether.c	8.1 (Berkeley) 6/10/93
30114407Snyan * $FreeBSD: head/sys/netinet/if_ether.c 148883 2005-08-09 08:39:56Z glebius $
31114407Snyan */
32114407Snyan
33114407Snyan/*
34114407Snyan * Ethernet address resolution protocol.
35114407Snyan * TODO:
36114407Snyan *	add "inuse/lock" bit (or ref. count) along with valid bit
37114407Snyan */
38114407Snyan
39114407Snyan#include "opt_inet.h"
40114407Snyan#include "opt_bdg.h"
41114407Snyan#include "opt_mac.h"
42114407Snyan#include "opt_carp.h"
43114407Snyan
44114407Snyan#include <sys/param.h>
45114407Snyan#include <sys/kernel.h>
46114407Snyan#include <sys/queue.h>
47114407Snyan#include <sys/sysctl.h>
48114407Snyan#include <sys/systm.h>
49114407Snyan#include <sys/mac.h>
50114407Snyan#include <sys/mbuf.h>
51114407Snyan#include <sys/malloc.h>
52114407Snyan#include <sys/socket.h>
53114407Snyan#include <sys/syslog.h>
54114407Snyan
55114407Snyan#include <net/if.h>
56114407Snyan#include <net/if_dl.h>
57114407Snyan#include <net/if_types.h>
58114407Snyan#include <net/route.h>
59114407Snyan#include <net/netisr.h>
60114407Snyan#include <net/if_llc.h>
61114407Snyan#include <net/ethernet.h>
62114407Snyan#include <net/bridge.h>
63114407Snyan
64114407Snyan#include <netinet/in.h>
65114407Snyan#include <netinet/in_var.h>
66114407Snyan#include <netinet/if_ether.h>
67114407Snyan
68114407Snyan#include <net/if_arc.h>
69114407Snyan#include <net/iso88025.h>
70114407Snyan
71114407Snyan#ifdef DEV_CARP
72114407Snyan#include <netinet/ip_carp.h>
73114407Snyan#endif
74114407Snyan
75114407Snyan#define SIN(s) ((struct sockaddr_in *)s)
76114407Snyan#define SDL(s) ((struct sockaddr_dl *)s)
77114407Snyan
78114407SnyanSYSCTL_DECL(_net_link_ether);
79114407SnyanSYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
80114407Snyan
81114407Snyan/* timer values */
82114407Snyanstatic int arpt_prune = (5*60*1); /* walk list every 5 minutes */
83114407Snyanstatic int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
84114407Snyanstatic int arpt_down = 20;	/* once declared down, don't send for 20 sec */
85114407Snyan
86114407SnyanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
87114407Snyan	   &arpt_prune, 0, "");
88114407SnyanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
89114407Snyan	   &arpt_keep, 0, "");
90114407SnyanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
91114407Snyan	   &arpt_down, 0, "");
92114407Snyan
93114407Snyan#define	rt_expire rt_rmx.rmx_expire
94114407Snyan
95114407Snyanstruct llinfo_arp {
96114407Snyan	LIST_ENTRY(llinfo_arp) la_le;
97114407Snyan	struct	rtentry *la_rt;
98114407Snyan	struct	mbuf *la_hold;	/* last packet until resolved/timeout */
99114407Snyan	u_short	la_preempt;	/* countdown for pre-expiry arps */
100114407Snyan	u_short	la_asked;	/* #times we QUERIED following expiration */
101114407Snyan#define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */
102114407Snyan};
103114407Snyan
104114407Snyanstatic	LIST_HEAD(, llinfo_arp) llinfo_arp;
105114407Snyan
106114407Snyanstatic struct	ifqueue arpintrq;
107114407Snyanstatic int	arp_allocated;
108114407Snyan
109114407Snyanstatic int	arp_maxtries = 5;
110114407Snyanstatic int	useloopback = 1; /* use loopback interface for local traffic */
111114407Snyanstatic int	arp_proxyall = 0;
112114407Snyanstatic struct callout arp_callout;
113114407Snyan
114114407SnyanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
115114407Snyan	   &arp_maxtries, 0, "");
116114407SnyanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
117114407Snyan	   &useloopback, 0, "");
118114407SnyanSYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
119	   &arp_proxyall, 0, "");
120
121static void	arp_init(void);
122static void	arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
123static void	arprequest(struct ifnet *,
124			struct in_addr *, struct in_addr *, u_char *);
125static void	arpintr(struct mbuf *);
126static void	arptfree(struct llinfo_arp *);
127static void	arptimer(void *);
128static struct llinfo_arp
129		*arplookup(u_long, int, int);
130#ifdef INET
131static void	in_arpinput(struct mbuf *);
132#endif
133
134/*
135 * Timeout routine.  Age arp_tab entries periodically.
136 */
137/* ARGSUSED */
138static void
139arptimer(ignored_arg)
140	void *ignored_arg;
141{
142	struct llinfo_arp *la, *ola;
143
144	RADIX_NODE_HEAD_LOCK(rt_tables[AF_INET]);
145	la = LIST_FIRST(&llinfo_arp);
146	while (la != NULL) {
147		struct rtentry *rt = la->la_rt;
148		ola = la;
149		la = LIST_NEXT(la, la_le);
150		if (rt->rt_expire && rt->rt_expire <= time_second)
151			arptfree(ola);		/* timer has expired, clear */
152	}
153	RADIX_NODE_HEAD_UNLOCK(rt_tables[AF_INET]);
154
155	callout_reset(&arp_callout, arpt_prune * hz, arptimer, NULL);
156}
157
158/*
159 * Parallel to llc_rtrequest.
160 */
161static void
162arp_rtrequest(req, rt, info)
163	int req;
164	struct rtentry *rt;
165	struct rt_addrinfo *info;
166{
167	struct sockaddr *gate;
168	struct llinfo_arp *la;
169	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
170	struct in_ifaddr *ia;
171	struct ifaddr *ifa;
172
173	RT_LOCK_ASSERT(rt);
174
175	if (rt->rt_flags & RTF_GATEWAY)
176		return;
177	gate = rt->rt_gateway;
178	la = (struct llinfo_arp *)rt->rt_llinfo;
179	switch (req) {
180
181	case RTM_ADD:
182		/*
183		 * XXX: If this is a manually added route to interface
184		 * such as older version of routed or gated might provide,
185		 * restore cloning bit.
186		 */
187		if ((rt->rt_flags & RTF_HOST) == 0 &&
188		    rt_mask(rt) != NULL &&
189		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
190			rt->rt_flags |= RTF_CLONING;
191		if (rt->rt_flags & RTF_CLONING) {
192			/*
193			 * Case 1: This route should come from a route to iface.
194			 */
195			rt_setgate(rt, rt_key(rt),
196					(struct sockaddr *)&null_sdl);
197			gate = rt->rt_gateway;
198			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
199			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
200			rt->rt_expire = time_second;
201			break;
202		}
203		/* Announce a new entry if requested. */
204		if (rt->rt_flags & RTF_ANNOUNCE)
205			arprequest(rt->rt_ifp,
206			    &SIN(rt_key(rt))->sin_addr,
207			    &SIN(rt_key(rt))->sin_addr,
208			    (u_char *)LLADDR(SDL(gate)));
209		/*FALLTHROUGH*/
210	case RTM_RESOLVE:
211		if (gate->sa_family != AF_LINK ||
212		    gate->sa_len < sizeof(null_sdl)) {
213			log(LOG_DEBUG, "%s: bad gateway %s%s\n", __func__,
214			    inet_ntoa(SIN(rt_key(rt))->sin_addr),
215			    (gate->sa_family != AF_LINK) ?
216			    " (!AF_LINK)": "");
217			break;
218		}
219		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
220		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
221		if (la != 0)
222			break; /* This happens on a route change */
223		/*
224		 * Case 2:  This route may come from cloning, or a manual route
225		 * add with a LL address.
226		 */
227		R_Zalloc(la, struct llinfo_arp *, sizeof(*la));
228		rt->rt_llinfo = (caddr_t)la;
229		if (la == 0) {
230			log(LOG_DEBUG, "%s: malloc failed\n", __func__);
231			break;
232		}
233		arp_allocated++;
234		la->la_rt = rt;
235		rt->rt_flags |= RTF_LLINFO;
236		RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]);
237		LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
238
239#ifdef INET
240		/*
241		 * This keeps the multicast addresses from showing up
242		 * in `arp -a' listings as unresolved.  It's not actually
243		 * functional.  Then the same for broadcast.
244		 */
245		if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) &&
246		    rt->rt_ifp->if_type != IFT_ARCNET) {
247			ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
248					       LLADDR(SDL(gate)));
249			SDL(gate)->sdl_alen = 6;
250			rt->rt_expire = 0;
251		}
252		if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
253			memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
254			       rt->rt_ifp->if_addrlen);
255			SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
256			rt->rt_expire = 0;
257		}
258#endif
259
260		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
261			if (ia->ia_ifp == rt->rt_ifp &&
262			    SIN(rt_key(rt))->sin_addr.s_addr ==
263			    (IA_SIN(ia))->sin_addr.s_addr)
264				break;
265		}
266		if (ia) {
267		    /*
268		     * This test used to be
269		     *	if (loif.if_flags & IFF_UP)
270		     * It allowed local traffic to be forced
271		     * through the hardware by configuring the loopback down.
272		     * However, it causes problems during network configuration
273		     * for boards that can't receive packets they send.
274		     * It is now necessary to clear "useloopback" and remove
275		     * the route to force traffic out to the hardware.
276		     */
277			rt->rt_expire = 0;
278			bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
279			      SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
280			if (useloopback)
281				rt->rt_ifp = loif;
282
283		    /*
284		     * make sure to set rt->rt_ifa to the interface
285		     * address we are using, otherwise we will have trouble
286		     * with source address selection.
287		     */
288			ifa = &ia->ia_ifa;
289			if (ifa != rt->rt_ifa) {
290				IFAFREE(rt->rt_ifa);
291				IFAREF(ifa);
292				rt->rt_ifa = ifa;
293			}
294		}
295		break;
296
297	case RTM_DELETE:
298		if (la == 0)
299			break;
300		RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]);
301		LIST_REMOVE(la, la_le);
302		rt->rt_llinfo = 0;
303		rt->rt_flags &= ~RTF_LLINFO;
304		if (la->la_hold)
305			m_freem(la->la_hold);
306		Free((caddr_t)la);
307	}
308}
309
310/*
311 * Broadcast an ARP request. Caller specifies:
312 *	- arp header source ip address
313 *	- arp header target ip address
314 *	- arp header source ethernet address
315 */
316static void
317arprequest(ifp, sip, tip, enaddr)
318	struct ifnet *ifp;
319	struct in_addr *sip, *tip;
320	u_char *enaddr;
321{
322	struct mbuf *m;
323	struct arphdr *ah;
324	struct sockaddr sa;
325
326	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
327		return;
328	m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
329		2*ifp->if_data.ifi_addrlen;
330	m->m_pkthdr.len = m->m_len;
331	MH_ALIGN(m, m->m_len);
332	ah = mtod(m, struct arphdr *);
333	bzero((caddr_t)ah, m->m_len);
334#ifdef MAC
335	mac_create_mbuf_linklayer(ifp, m);
336#endif
337	ah->ar_pro = htons(ETHERTYPE_IP);
338	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
339	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
340	ah->ar_op = htons(ARPOP_REQUEST);
341	bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
342	bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
343	bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
344	sa.sa_family = AF_ARP;
345	sa.sa_len = 2;
346	m->m_flags |= M_BCAST;
347	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
348
349	return;
350}
351
352/*
353 * Resolve an IP address into an ethernet address.
354 * On input:
355 *    ifp is the interface we use
356 *    dst is the next hop,
357 *    rt0 is the route to the final destination (possibly useless)
358 *    m is the mbuf
359 *    desten is where we want the address.
360 *
361 * On success, desten is filled in and the function returns 0;
362 * If the packet must be held pending resolution, we return EWOULDBLOCK
363 * On other errors, we return the corresponding error code.
364 */
365int
366arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
367	struct sockaddr *dst, u_char *desten)
368{
369	struct llinfo_arp *la = 0;
370	struct sockaddr_dl *sdl;
371	int error;
372	struct rtentry *rt;
373
374	error = rt_check(&rt, &rt0, dst);
375	if (error) {
376		m_freem(m);
377		return error;
378	}
379	RT_UNLOCK(rt);
380
381	if (m->m_flags & M_BCAST) {	/* broadcast */
382		(void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
383		return (0);
384	}
385	if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */
386		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
387		return (0);
388	}
389	if (rt)
390		la = (struct llinfo_arp *)rt->rt_llinfo;
391	if (la == 0) {
392		la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
393		if (la)
394			rt = la->la_rt;
395	}
396	if (la == 0 || rt == 0) {
397		log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
398			inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "",
399				rt ? "rt" : "");
400		m_freem(m);
401		return (EINVAL); /* XXX */
402	}
403	sdl = SDL(rt->rt_gateway);
404	/*
405	 * Check the address family and length is valid, the address
406	 * is resolved; otherwise, try to resolve.
407	 */
408	if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
409	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
410		/*
411		 * If entry has an expiry time and it is approaching,
412		 * see if we need to send an ARP request within this
413		 * arpt_down interval.
414		 */
415		if ((rt->rt_expire != 0) &&
416		    (time_second + la->la_preempt > rt->rt_expire)) {
417			arprequest(ifp,
418				   &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
419				   &SIN(dst)->sin_addr,
420				   IF_LLADDR(ifp));
421			la->la_preempt--;
422		}
423
424		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
425		return (0);
426	}
427	/*
428	 * If ARP is disabled or static on this interface, stop.
429	 * XXX
430	 * Probably should not allocate empty llinfo struct if we are
431	 * not going to be sending out an arp request.
432	 */
433	if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
434		m_freem(m);
435		return (EINVAL);
436	}
437	/*
438	 * There is an arptab entry, but no ethernet address
439	 * response yet.  Replace the held mbuf with this
440	 * latest one.
441	 */
442	if (la->la_hold)
443		m_freem(la->la_hold);
444	la->la_hold = m;
445	if (rt->rt_expire) {
446		RT_LOCK(rt);
447		rt->rt_flags &= ~RTF_REJECT;
448		if (la->la_asked == 0 || rt->rt_expire != time_second) {
449			rt->rt_expire = time_second;
450			if (la->la_asked++ < arp_maxtries) {
451				arprequest(ifp,
452					   &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
453					   &SIN(dst)->sin_addr,
454					   IF_LLADDR(ifp));
455			} else {
456				rt->rt_flags |= RTF_REJECT;
457				rt->rt_expire += arpt_down;
458				la->la_asked = 0;
459				la->la_preempt = arp_maxtries;
460			}
461
462		}
463		RT_UNLOCK(rt);
464	}
465	return (EWOULDBLOCK);
466}
467
468/*
469 * Common length and type checks are done here,
470 * then the protocol-specific routine is called.
471 */
472static void
473arpintr(struct mbuf *m)
474{
475	struct arphdr *ar;
476
477	if (m->m_len < sizeof(struct arphdr) &&
478	    ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
479		log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
480		return;
481	}
482	ar = mtod(m, struct arphdr *);
483
484	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER &&
485	    ntohs(ar->ar_hrd) != ARPHRD_IEEE802 &&
486	    ntohs(ar->ar_hrd) != ARPHRD_ARCNET &&
487	    ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) {
488		log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
489		    (unsigned char *)&ar->ar_hrd, "");
490		m_freem(m);
491		return;
492	}
493
494	if (m->m_len < arphdr_len(ar)) {
495		if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
496			log(LOG_ERR, "arp: runt packet\n");
497			m_freem(m);
498			return;
499		}
500		ar = mtod(m, struct arphdr *);
501	}
502
503	switch (ntohs(ar->ar_pro)) {
504#ifdef INET
505	case ETHERTYPE_IP:
506		in_arpinput(m);
507		return;
508#endif
509	}
510	m_freem(m);
511}
512
513#ifdef INET
514/*
515 * ARP for Internet protocols on 10 Mb/s Ethernet.
516 * Algorithm is that given in RFC 826.
517 * In addition, a sanity check is performed on the sender
518 * protocol address, to catch impersonators.
519 * We no longer handle negotiations for use of trailer protocol:
520 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
521 * along with IP replies if we wanted trailers sent to us,
522 * and also sent them in response to IP replies.
523 * This allowed either end to announce the desire to receive
524 * trailer packets.
525 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
526 * but formerly didn't normally send requests.
527 */
528static int log_arp_wrong_iface = 1;
529static int log_arp_movements = 1;
530
531SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
532	&log_arp_wrong_iface, 0,
533	"log arp packets arriving on the wrong interface");
534SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
535        &log_arp_movements, 0,
536        "log arp replies from MACs different than the one in the cache");
537
538
539static void
540in_arpinput(m)
541	struct mbuf *m;
542{
543	struct arphdr *ah;
544	struct ifnet *ifp = m->m_pkthdr.rcvif;
545	struct iso88025_header *th = (struct iso88025_header *)0;
546	struct iso88025_sockaddr_dl_data *trld;
547	struct llinfo_arp *la = 0;
548	struct rtentry *rt;
549	struct ifaddr *ifa;
550	struct in_ifaddr *ia;
551	struct sockaddr_dl *sdl;
552	struct sockaddr sa;
553	struct in_addr isaddr, itaddr, myaddr;
554	u_int8_t *enaddr = NULL;
555	int op, rif_len;
556	int req_len;
557	int bridged = 0;
558#ifdef DEV_CARP
559	int carp_match = 0;
560#endif
561
562	if (do_bridge || ifp->if_bridge)
563		bridged = 1;
564
565	req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
566	if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
567		log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
568		return;
569	}
570
571	ah = mtod(m, struct arphdr *);
572	op = ntohs(ah->ar_op);
573	(void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
574	(void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
575
576	/*
577	 * For a bridge, we want to check the address irrespective
578	 * of the receive interface. (This will change slightly
579	 * when we have clusters of interfaces).
580	 * If the interface does not match, but the recieving interface
581	 * is part of carp, we call carp_iamatch to see if this is a
582	 * request for the virtual host ip.
583	 * XXX: This is really ugly!
584	 */
585	LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
586		if ((bridged || (ia->ia_ifp == ifp)) &&
587		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
588			goto match;
589#ifdef DEV_CARP
590		if (ifp->if_carp != NULL &&
591		    carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) &&
592		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
593			carp_match = 1;
594			goto match;
595		}
596#endif
597	}
598	LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
599		if ((bridged || (ia->ia_ifp == ifp)) &&
600		    isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
601			goto match;
602	/*
603	 * No match, use the first inet address on the receive interface
604	 * as a dummy address for the rest of the function.
605	 */
606	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
607		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
608			ia = ifatoia(ifa);
609			goto match;
610		}
611	/*
612	 * If bridging, fall back to using any inet address.
613	 */
614	if (!bridged || (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL)
615		goto drop;
616match:
617	if (!enaddr)
618		enaddr = (u_int8_t *)IF_LLADDR(ifp);
619	myaddr = ia->ia_addr.sin_addr;
620	if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
621		goto drop;	/* it's from me, ignore it. */
622	if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
623		log(LOG_ERR,
624		    "arp: link address is broadcast for IP address %s!\n",
625		    inet_ntoa(isaddr));
626		goto drop;
627	}
628	/*
629	 * Warn if another host is using the same IP address, but only if the
630	 * IP address isn't 0.0.0.0, which is used for DHCP only, in which
631	 * case we suppress the warning to avoid false positive complaints of
632	 * potential misconfiguration.
633	 */
634	if (isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
635		log(LOG_ERR,
636		   "arp: %*D is using my IP address %s!\n",
637		   ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
638		   inet_ntoa(isaddr));
639		itaddr = myaddr;
640		goto reply;
641	}
642	if (ifp->if_flags & IFF_STATICARP)
643		goto reply;
644	la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
645	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
646		/* the following is not an error when doing bridging */
647		if (!bridged && rt->rt_ifp != ifp
648#ifdef DEV_CARP
649		    && (ifp->if_type != IFT_CARP || !carp_match)
650#endif
651								) {
652			if (log_arp_wrong_iface)
653				log(LOG_ERR, "arp: %s is on %s but got reply from %*D on %s\n",
654				    inet_ntoa(isaddr),
655				    rt->rt_ifp->if_xname,
656				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
657				    ifp->if_xname);
658			goto reply;
659		}
660		if (sdl->sdl_alen &&
661		    bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
662			if (rt->rt_expire) {
663			    if (log_arp_movements)
664			        log(LOG_INFO, "arp: %s moved from %*D to %*D on %s\n",
665				    inet_ntoa(isaddr),
666				    ifp->if_addrlen, (u_char *)LLADDR(sdl), ":",
667				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
668				    ifp->if_xname);
669			} else {
670			    log(LOG_ERR,
671				"arp: %*D attempts to modify permanent entry for %s on %s\n",
672				ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
673				inet_ntoa(isaddr), ifp->if_xname);
674			    goto reply;
675			}
676		}
677		/*
678		 * sanity check for the address length.
679		 * XXX this does not work for protocols with variable address
680		 * length. -is
681		 */
682		if (sdl->sdl_alen &&
683		    sdl->sdl_alen != ah->ar_hln) {
684			log(LOG_WARNING,
685			    "arp from %*D: new addr len %d, was %d",
686			    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
687			    ah->ar_hln, sdl->sdl_alen);
688		}
689		if (ifp->if_addrlen != ah->ar_hln) {
690			log(LOG_WARNING,
691			    "arp from %*D: addr len: new %d, i/f %d (ignored)",
692			    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
693			    ah->ar_hln, ifp->if_addrlen);
694			goto reply;
695		}
696		(void)memcpy(LLADDR(sdl), ar_sha(ah),
697		    sdl->sdl_alen = ah->ar_hln);
698		/*
699		 * If we receive an arp from a token-ring station over
700		 * a token-ring nic then try to save the source
701		 * routing info.
702		 */
703		if (ifp->if_type == IFT_ISO88025) {
704			th = (struct iso88025_header *)m->m_pkthdr.header;
705			trld = SDL_ISO88025(sdl);
706			rif_len = TR_RCF_RIFLEN(th->rcf);
707			if ((th->iso88025_shost[0] & TR_RII) &&
708			    (rif_len > 2)) {
709				trld->trld_rcf = th->rcf;
710				trld->trld_rcf ^= htons(TR_RCF_DIR);
711				memcpy(trld->trld_route, th->rd, rif_len - 2);
712				trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
713				/*
714				 * Set up source routing information for
715				 * reply packet (XXX)
716				 */
717				m->m_data -= rif_len;
718				m->m_len  += rif_len;
719				m->m_pkthdr.len += rif_len;
720			} else {
721				th->iso88025_shost[0] &= ~TR_RII;
722				trld->trld_rcf = 0;
723			}
724			m->m_data -= 8;
725			m->m_len  += 8;
726			m->m_pkthdr.len += 8;
727			th->rcf = trld->trld_rcf;
728		}
729		RT_LOCK(rt);
730		if (rt->rt_expire)
731			rt->rt_expire = time_second + arpt_keep;
732		rt->rt_flags &= ~RTF_REJECT;
733		RT_UNLOCK(rt);
734		la->la_asked = 0;
735		la->la_preempt = arp_maxtries;
736		if (la->la_hold) {
737			(*ifp->if_output)(ifp, la->la_hold, rt_key(rt), rt);
738			la->la_hold = 0;
739		}
740	}
741reply:
742	if (op != ARPOP_REQUEST)
743		goto drop;
744	if (itaddr.s_addr == myaddr.s_addr) {
745		/* I am the target */
746		(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
747		(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
748	} else {
749		la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
750		if (la == NULL) {
751			struct sockaddr_in sin;
752
753			if (!arp_proxyall)
754				goto drop;
755
756			bzero(&sin, sizeof sin);
757			sin.sin_family = AF_INET;
758			sin.sin_len = sizeof sin;
759			sin.sin_addr = itaddr;
760
761			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
762			if (!rt)
763				goto drop;
764			/*
765			 * Don't send proxies for nodes on the same interface
766			 * as this one came out of, or we'll get into a fight
767			 * over who claims what Ether address.
768			 */
769			if (rt->rt_ifp == ifp) {
770				rtfree(rt);
771				goto drop;
772			}
773			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
774			(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
775			rtfree(rt);
776
777			/*
778			 * Also check that the node which sent the ARP packet
779			 * is on the the interface we expect it to be on. This
780			 * avoids ARP chaos if an interface is connected to the
781			 * wrong network.
782			 */
783			sin.sin_addr = isaddr;
784
785			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
786			if (!rt)
787				goto drop;
788			if (rt->rt_ifp != ifp) {
789				log(LOG_INFO, "arp_proxy: ignoring request"
790				    " from %s via %s, expecting %s\n",
791				    inet_ntoa(isaddr), ifp->if_xname,
792				    rt->rt_ifp->if_xname);
793				rtfree(rt);
794				goto drop;
795			}
796			rtfree(rt);
797
798#ifdef DEBUG_PROXY
799			printf("arp: proxying for %s\n",
800			       inet_ntoa(itaddr));
801#endif
802		} else {
803			rt = la->la_rt;
804			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
805			sdl = SDL(rt->rt_gateway);
806			(void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
807		}
808	}
809
810	(void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
811	(void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
812	ah->ar_op = htons(ARPOP_REPLY);
813	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
814	m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
815	m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
816	m->m_pkthdr.len = m->m_len;
817	sa.sa_family = AF_ARP;
818	sa.sa_len = 2;
819	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
820	return;
821
822drop:
823	m_freem(m);
824}
825#endif
826
827/*
828 * Free an arp entry.
829 */
830static void
831arptfree(la)
832	struct llinfo_arp *la;
833{
834	struct rtentry *rt = la->la_rt;
835	struct sockaddr_dl *sdl;
836
837	if (rt == 0)
838		panic("arptfree");
839	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
840	    sdl->sdl_family == AF_LINK) {
841		sdl->sdl_alen = 0;
842		la->la_preempt = la->la_asked = 0;
843		RT_LOCK(rt);		/* XXX needed or move higher? */
844		rt->rt_flags &= ~RTF_REJECT;
845		RT_UNLOCK(rt);
846		return;
847	}
848	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
849			0, (struct rtentry **)0);
850}
851/*
852 * Lookup or enter a new address in arptab.
853 */
854static struct llinfo_arp *
855arplookup(addr, create, proxy)
856	u_long addr;
857	int create, proxy;
858{
859	struct rtentry *rt;
860	struct sockaddr_inarp sin;
861	const char *why = 0;
862
863	bzero(&sin, sizeof(sin));
864	sin.sin_len = sizeof(sin);
865	sin.sin_family = AF_INET;
866	sin.sin_addr.s_addr = addr;
867	if (proxy)
868		sin.sin_other = SIN_PROXY;
869	rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
870	if (rt == 0)
871		return (0);
872
873	if (rt->rt_flags & RTF_GATEWAY)
874		why = "host is not on local network";
875	else if ((rt->rt_flags & RTF_LLINFO) == 0)
876		why = "could not allocate llinfo";
877	else if (rt->rt_gateway->sa_family != AF_LINK)
878		why = "gateway route is not ours";
879
880	if (why) {
881#define	ISDYNCLONE(_rt) \
882	(((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED)
883		if (create)
884			log(LOG_DEBUG, "arplookup %s failed: %s\n",
885			    inet_ntoa(sin.sin_addr), why);
886		/*
887		 * If there are no references to this Layer 2 route,
888		 * and it is a cloned route, and not static, and
889		 * arplookup() is creating the route, then purge
890		 * it from the routing table as it is probably bogus.
891		 */
892		if (rt->rt_refcnt == 1 && ISDYNCLONE(rt))
893			rtexpunge(rt);
894		RTFREE_LOCKED(rt);
895		return (0);
896#undef ISDYNCLONE
897	} else {
898		RT_REMREF(rt);
899		RT_UNLOCK(rt);
900		return ((struct llinfo_arp *)rt->rt_llinfo);
901	}
902}
903
904void
905arp_ifinit(ifp, ifa)
906	struct ifnet *ifp;
907	struct ifaddr *ifa;
908{
909	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
910		arprequest(ifp, &IA_SIN(ifa)->sin_addr,
911				&IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
912	ifa->ifa_rtrequest = arp_rtrequest;
913	ifa->ifa_flags |= RTF_CLONING;
914}
915
916void
917arp_ifinit2(ifp, ifa, enaddr)
918	struct ifnet *ifp;
919	struct ifaddr *ifa;
920	u_char *enaddr;
921{
922	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
923		arprequest(ifp, &IA_SIN(ifa)->sin_addr,
924				&IA_SIN(ifa)->sin_addr, enaddr);
925	ifa->ifa_rtrequest = arp_rtrequest;
926	ifa->ifa_flags |= RTF_CLONING;
927}
928
929static void
930arp_init(void)
931{
932
933	arpintrq.ifq_maxlen = 50;
934	mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF);
935	LIST_INIT(&llinfo_arp);
936	callout_init(&arp_callout, CALLOUT_MPSAFE);
937	netisr_register(NETISR_ARP, arpintr, &arpintrq, NETISR_MPSAFE);
938	callout_reset(&arp_callout, hz, arptimer, NULL);
939}
940SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);
941