if_ether.c revision 123765
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 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *	@(#)if_ether.c	8.1 (Berkeley) 6/10/93
34 * $FreeBSD: head/sys/netinet/if_ether.c 123765 2003-12-23 13:33:23Z ru $
35 */
36
37/*
38 * Ethernet address resolution protocol.
39 * TODO:
40 *	add "inuse/lock" bit (or ref. count) along with valid bit
41 */
42
43#include "opt_inet.h"
44#include "opt_bdg.h"
45#include "opt_mac.h"
46
47#include <sys/param.h>
48#include <sys/kernel.h>
49#include <sys/queue.h>
50#include <sys/sysctl.h>
51#include <sys/systm.h>
52#include <sys/mac.h>
53#include <sys/mbuf.h>
54#include <sys/malloc.h>
55#include <sys/socket.h>
56#include <sys/syslog.h>
57
58#include <net/if.h>
59#include <net/if_dl.h>
60#include <net/if_types.h>
61#include <net/route.h>
62#include <net/netisr.h>
63#include <net/if_llc.h>
64#ifdef BRIDGE
65#include <net/ethernet.h>
66#include <net/bridge.h>
67#endif
68
69#include <netinet/in.h>
70#include <netinet/in_var.h>
71#include <netinet/if_ether.h>
72
73#include <net/if_arc.h>
74#include <net/iso88025.h>
75
76#define SIN(s) ((struct sockaddr_in *)s)
77#define SDL(s) ((struct sockaddr_dl *)s)
78
79SYSCTL_DECL(_net_link_ether);
80SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
81
82/* timer values */
83static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
84static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
85static int arpt_down = 20;	/* once declared down, don't send for 20 sec */
86
87SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
88	   &arpt_prune, 0, "");
89SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
90	   &arpt_keep, 0, "");
91SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
92	   &arpt_down, 0, "");
93
94#define	rt_expire rt_rmx.rmx_expire
95
96struct llinfo_arp {
97	LIST_ENTRY(llinfo_arp) la_le;
98	struct	rtentry *la_rt;
99	struct	mbuf *la_hold;	/* last packet until resolved/timeout */
100	u_short	la_preempt;	/* countdown for pre-expiry arps */
101	u_short	la_asked;	/* #times we QUERIED following expiration */
102#define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */
103};
104
105static	LIST_HEAD(, llinfo_arp) llinfo_arp;
106
107static struct	ifqueue arpintrq;
108static int	arp_allocated;
109static int	arpinit_done;
110
111static int	arp_maxtries = 5;
112static int	useloopback = 1; /* use loopback interface for local traffic */
113static int	arp_proxyall = 0;
114static struct callout arp_callout;
115
116SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
117	   &arp_maxtries, 0, "");
118SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
119	   &useloopback, 0, "");
120SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
121	   &arp_proxyall, 0, "");
122
123static void	arp_init(void);
124static void	arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
125static void	arprequest(struct ifnet *,
126			struct in_addr *, struct in_addr *, u_char *);
127static void	arpintr(struct mbuf *);
128static void	arptfree(struct llinfo_arp *);
129static void	arptimer(void *);
130static struct llinfo_arp
131		*arplookup(u_long, int, int);
132#ifdef INET
133static void	in_arpinput(struct mbuf *);
134#endif
135
136/*
137 * Timeout routine.  Age arp_tab entries periodically.
138 */
139/* ARGSUSED */
140static void
141arptimer(ignored_arg)
142	void *ignored_arg;
143{
144	struct llinfo_arp *la, *ola;
145
146	RADIX_NODE_HEAD_LOCK(rt_tables[AF_INET]);
147	la = LIST_FIRST(&llinfo_arp);
148	while (la != NULL) {
149		struct rtentry *rt = la->la_rt;
150		ola = la;
151		la = LIST_NEXT(la, la_le);
152		if (rt->rt_expire && rt->rt_expire <= time_second)
153			arptfree(ola);		/* timer has expired, clear */
154	}
155	RADIX_NODE_HEAD_UNLOCK(rt_tables[AF_INET]);
156
157	callout_reset(&arp_callout, arpt_prune * hz, arptimer, NULL);
158}
159
160/*
161 * Parallel to llc_rtrequest.
162 */
163static void
164arp_rtrequest(req, rt, info)
165	int req;
166	register struct rtentry *rt;
167	struct rt_addrinfo *info;
168{
169	register struct sockaddr *gate;
170	register struct llinfo_arp *la;
171	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
172
173	RT_LOCK_ASSERT(rt);
174
175	if (!arpinit_done) {
176		arpinit_done = 1;
177		callout_reset(&arp_callout, hz, arptimer, NULL);
178	}
179	if (rt->rt_flags & RTF_GATEWAY)
180		return;
181	gate = rt->rt_gateway;
182	la = (struct llinfo_arp *)rt->rt_llinfo;
183	switch (req) {
184
185	case RTM_ADD:
186		/*
187		 * XXX: If this is a manually added route to interface
188		 * such as older version of routed or gated might provide,
189		 * restore cloning bit.
190		 */
191		if ((rt->rt_flags & RTF_HOST) == 0 &&
192		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
193			rt->rt_flags |= RTF_CLONING;
194		if (rt->rt_flags & RTF_CLONING) {
195			/*
196			 * Case 1: This route should come from a route to iface.
197			 */
198			rt_setgate(rt, rt_key(rt),
199					(struct sockaddr *)&null_sdl);
200			gate = rt->rt_gateway;
201			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
202			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
203			rt->rt_expire = time_second;
204			break;
205		}
206		/* Announce a new entry if requested. */
207		if (rt->rt_flags & RTF_ANNOUNCE)
208			arprequest(rt->rt_ifp,
209			    &SIN(rt_key(rt))->sin_addr,
210			    &SIN(rt_key(rt))->sin_addr,
211			    (u_char *)LLADDR(SDL(gate)));
212		/*FALLTHROUGH*/
213	case RTM_RESOLVE:
214		if (gate->sa_family != AF_LINK ||
215		    gate->sa_len < sizeof(null_sdl)) {
216			log(LOG_DEBUG, "%s: bad gateway %s%s\n", __func__,
217			    inet_ntoa(SIN(rt_key(rt))->sin_addr),
218			    (gate->sa_family != AF_LINK) ?
219			    " (!AF_LINK)": "");
220			break;
221		}
222		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
223		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
224		if (la != 0)
225			break; /* This happens on a route change */
226		/*
227		 * Case 2:  This route may come from cloning, or a manual route
228		 * add with a LL address.
229		 */
230		R_Zalloc(la, struct llinfo_arp *, sizeof(*la));
231		rt->rt_llinfo = (caddr_t)la;
232		if (la == 0) {
233			log(LOG_DEBUG, "%s: malloc failed\n", __func__);
234			break;
235		}
236		arp_allocated++;
237		la->la_rt = rt;
238		rt->rt_flags |= RTF_LLINFO;
239		RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]);
240		LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
241
242#ifdef INET
243		/*
244		 * This keeps the multicast addresses from showing up
245		 * in `arp -a' listings as unresolved.  It's not actually
246		 * functional.  Then the same for broadcast.
247		 */
248		if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) &&
249		    rt->rt_ifp->if_type != IFT_ARCNET) {
250			ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
251					       LLADDR(SDL(gate)));
252			SDL(gate)->sdl_alen = 6;
253			rt->rt_expire = 0;
254		}
255		if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
256			memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
257			       rt->rt_ifp->if_addrlen);
258			SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
259			rt->rt_expire = 0;
260		}
261#endif
262
263		if (SIN(rt_key(rt))->sin_addr.s_addr ==
264		    (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
265		    /*
266		     * This test used to be
267		     *	if (loif.if_flags & IFF_UP)
268		     * It allowed local traffic to be forced
269		     * through the hardware by configuring the loopback down.
270		     * However, it causes problems during network configuration
271		     * for boards that can't receive packets they send.
272		     * It is now necessary to clear "useloopback" and remove
273		     * the route to force traffic out to the hardware.
274		     */
275			rt->rt_expire = 0;
276			Bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
277			      SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
278			if (useloopback)
279				rt->rt_ifp = loif;
280
281		}
282		break;
283
284	case RTM_DELETE:
285		if (la == 0)
286			break;
287		RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]);
288		LIST_REMOVE(la, la_le);
289		rt->rt_llinfo = 0;
290		rt->rt_flags &= ~RTF_LLINFO;
291		if (la->la_hold)
292			m_freem(la->la_hold);
293		Free((caddr_t)la);
294	}
295}
296
297/*
298 * Broadcast an ARP request. Caller specifies:
299 *	- arp header source ip address
300 *	- arp header target ip address
301 *	- arp header source ethernet address
302 */
303static void
304arprequest(ifp, sip, tip, enaddr)
305	register struct ifnet *ifp;
306	register struct in_addr *sip, *tip;
307	register u_char *enaddr;
308{
309	register struct mbuf *m;
310	register struct ether_header *eh;
311	register struct arc_header *arh;
312	register struct arphdr *ah;
313	struct sockaddr sa;
314	static u_char	llcx[] = { 0x82, 0x40, LLC_SNAP_LSAP, LLC_SNAP_LSAP,
315				   LLC_UI, 0x00, 0x00, 0x00, 0x08, 0x06 };
316	u_short ar_hrd;
317
318	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
319		return;
320	m->m_pkthdr.rcvif = (struct ifnet *)0;
321#ifdef MAC
322	mac_create_mbuf_linklayer(ifp, m);
323#endif
324	switch (ifp->if_type) {
325	case IFT_ARCNET:
326		ar_hrd = htons(ARPHRD_ARCNET);
327
328		m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
329		m->m_pkthdr.len = m->m_len;
330		MH_ALIGN(m, m->m_len);
331
332		arh = (struct arc_header *)sa.sa_data;
333		arh->arc_dhost = *ifp->if_broadcastaddr;
334		arh->arc_type = ARCTYPE_ARP;
335
336		ah = mtod(m, struct arphdr *);
337		break;
338
339	case IFT_ISO88025:
340		ar_hrd = htons(ARPHRD_IEEE802);
341
342		m->m_len = sizeof(llcx) +
343		    arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
344		m->m_pkthdr.len = m->m_len;
345		MH_ALIGN(m, m->m_len);
346
347		(void)memcpy(mtod(m, caddr_t), llcx, sizeof(llcx));
348		(void)memcpy(sa.sa_data, ifp->if_broadcastaddr, 6);
349		(void)memcpy(sa.sa_data + 6, enaddr, 6);
350		sa.sa_data[6] |= TR_RII;
351		sa.sa_data[12] = TR_AC;
352		sa.sa_data[13] = TR_LLC_FRAME;
353
354		ah = (struct arphdr *)(mtod(m, char *) + sizeof(llcx));
355		break;
356	case IFT_FDDI:
357	case IFT_ETHER:
358		/*
359		 * This may not be correct for types not explicitly
360		 * listed, but this is our best guess
361		 */
362	default:
363		ar_hrd = htons(ARPHRD_ETHER);
364
365		m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
366		m->m_pkthdr.len = m->m_len;
367		MH_ALIGN(m, m->m_len);
368
369		eh = (struct ether_header *)sa.sa_data;
370		/* if_output will not swap */
371		eh->ether_type = htons(ETHERTYPE_ARP);
372		(void)memcpy(eh->ether_dhost, ifp->if_broadcastaddr,
373		    sizeof(eh->ether_dhost));
374
375		ah = mtod(m, struct arphdr *);
376		break;
377	}
378
379	ah->ar_hrd = ar_hrd;
380	ah->ar_pro = htons(ETHERTYPE_IP);
381	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
382	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
383	ah->ar_op = htons(ARPOP_REQUEST);
384	(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
385	memset(ar_tha(ah), 0, ah->ar_hln);
386	(void)memcpy(ar_spa(ah), sip, ah->ar_pln);
387	(void)memcpy(ar_tpa(ah), tip, ah->ar_pln);
388
389	sa.sa_family = AF_UNSPEC;
390	sa.sa_len = sizeof(sa);
391	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
392}
393
394/*
395 * Resolve an IP address into an ethernet address.  If success,
396 * desten is filled in.  If there is no entry in arptab,
397 * set one up and broadcast a request for the IP address.
398 * Hold onto this mbuf and resend it once the address
399 * is finally resolved.  A return value of 1 indicates
400 * that desten has been filled in and the packet should be sent
401 * normally; a 0 return indicates that the packet has been
402 * taken over here, either now or for later transmission.
403 */
404int
405arpresolve(ifp, rt, m, dst, desten, rt0)
406	register struct ifnet *ifp;
407	register struct rtentry *rt;
408	struct mbuf *m;
409	register struct sockaddr *dst;
410	register u_char *desten;
411	struct rtentry *rt0;
412{
413	struct llinfo_arp *la = 0;
414	struct sockaddr_dl *sdl;
415
416	if (m->m_flags & M_BCAST) {	/* broadcast */
417		(void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
418		return (1);
419	}
420	if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */
421		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
422		return(1);
423	}
424	if (rt)
425		la = (struct llinfo_arp *)rt->rt_llinfo;
426	if (la == 0) {
427		la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
428		if (la)
429			rt = la->la_rt;
430	}
431	if (la == 0 || rt == 0) {
432		log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
433			inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "",
434				rt ? "rt" : "");
435		m_freem(m);
436		return (0);
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_second) &&
444	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
445		/*
446		 * If entry has an expiry time and it is approaching,
447		 * see if we need to send an ARP request within this
448		 * arpt_down interval.
449		 */
450		if ((rt->rt_expire != 0) &&
451		    (time_second + la->la_preempt > rt->rt_expire)) {
452			arprequest(ifp,
453				   &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
454				   &SIN(dst)->sin_addr,
455				   IF_LLADDR(ifp));
456			la->la_preempt--;
457		}
458
459		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
460		return 1;
461	}
462	/*
463	 * If ARP is disabled or static on this interface, stop.
464	 * XXX
465	 * Probably should not allocate empty llinfo struct if we are
466	 * not going to be sending out an arp request.
467	 */
468	if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
469		m_freem(m);
470		return (0);
471	}
472	/*
473	 * There is an arptab entry, but no ethernet address
474	 * response yet.  Replace the held mbuf with this
475	 * latest one.
476	 */
477	if (la->la_hold)
478		m_freem(la->la_hold);
479	la->la_hold = m;
480	if (rt->rt_expire) {
481		RT_LOCK(rt);
482		rt->rt_flags &= ~RTF_REJECT;
483		if (la->la_asked == 0 || rt->rt_expire != time_second) {
484			rt->rt_expire = time_second;
485			if (la->la_asked++ < arp_maxtries) {
486				arprequest(ifp,
487					   &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
488					   &SIN(dst)->sin_addr,
489					   IF_LLADDR(ifp));
490			} else {
491				rt->rt_flags |= RTF_REJECT;
492				rt->rt_expire += arpt_down;
493				la->la_asked = 0;
494				la->la_preempt = arp_maxtries;
495			}
496
497		}
498		RT_UNLOCK(rt);
499	}
500	return (0);
501}
502
503/*
504 * Common length and type checks are done here,
505 * then the protocol-specific routine is called.
506 */
507static void
508arpintr(struct mbuf *m)
509{
510	struct arphdr *ar;
511
512	if (!arpinit_done) {
513		/* NB: this race should not matter */
514		arpinit_done = 1;
515		callout_reset(&arp_callout, hz, arptimer, NULL);
516	}
517	if (m->m_len < sizeof(struct arphdr) &&
518	    ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
519		log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
520		return;
521	}
522	ar = mtod(m, struct arphdr *);
523
524	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER &&
525	    ntohs(ar->ar_hrd) != ARPHRD_IEEE802 &&
526	    ntohs(ar->ar_hrd) != ARPHRD_ARCNET) {
527		log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
528		    (unsigned char *)&ar->ar_hrd, "");
529		m_freem(m);
530		return;
531	}
532
533	if (m->m_pkthdr.len < arphdr_len(ar)) {
534		if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
535			log(LOG_ERR, "arp: runt packet\n");
536			m_freem(m);
537			return;
538		}
539		ar = mtod(m, struct arphdr *);
540	}
541
542	switch (ntohs(ar->ar_pro)) {
543#ifdef INET
544	case ETHERTYPE_IP:
545		in_arpinput(m);
546		return;
547#endif
548	}
549	m_freem(m);
550}
551
552#ifdef INET
553/*
554 * ARP for Internet protocols on 10 Mb/s Ethernet.
555 * Algorithm is that given in RFC 826.
556 * In addition, a sanity check is performed on the sender
557 * protocol address, to catch impersonators.
558 * We no longer handle negotiations for use of trailer protocol:
559 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
560 * along with IP replies if we wanted trailers sent to us,
561 * and also sent them in response to IP replies.
562 * This allowed either end to announce the desire to receive
563 * trailer packets.
564 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
565 * but formerly didn't normally send requests.
566 */
567static int log_arp_wrong_iface = 1;
568static int log_arp_movements = 1;
569
570SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
571	&log_arp_wrong_iface, 0,
572	"log arp packets arriving on the wrong interface");
573SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
574        &log_arp_movements, 0,
575        "log arp replies from MACs different than the one in the cache");
576
577
578static void
579in_arpinput(m)
580	struct mbuf *m;
581{
582	register struct arphdr *ah;
583	register struct ifnet *ifp = m->m_pkthdr.rcvif;
584	struct ether_header *eh;
585	struct arc_header *arh;
586	struct iso88025_header *th = (struct iso88025_header *)0;
587	struct iso88025_sockaddr_dl_data *trld;
588	register struct llinfo_arp *la = 0;
589	register struct rtentry *rt;
590	struct ifaddr *ifa;
591	struct in_ifaddr *ia;
592	struct sockaddr_dl *sdl;
593	struct sockaddr sa;
594	struct in_addr isaddr, itaddr, myaddr;
595	int op, rif_len;
596	int req_len;
597
598	req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
599	if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
600		log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
601		return;
602	}
603
604	ah = mtod(m, struct arphdr *);
605	op = ntohs(ah->ar_op);
606	(void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
607	(void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
608#ifdef BRIDGE
609#define BRIDGE_TEST (do_bridge)
610#else
611#define BRIDGE_TEST (0) /* cc will optimise the test away */
612#endif
613	/*
614	 * For a bridge, we want to check the address irrespective
615	 * of the receive interface. (This will change slightly
616	 * when we have clusters of interfaces).
617	 */
618	LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash)
619		if ((BRIDGE_TEST || (ia->ia_ifp == ifp)) &&
620		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
621			goto match;
622	LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
623		if ((BRIDGE_TEST || (ia->ia_ifp == ifp)) &&
624		    isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
625			goto match;
626	/*
627	 * No match, use the first inet address on the receive interface
628	 * as a dummy address for the rest of the function.
629	 */
630	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
631		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
632			ia = ifatoia(ifa);
633			goto match;
634		}
635	/*
636	 * If bridging, fall back to using any inet address.
637	 */
638	if (!BRIDGE_TEST ||
639	    (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL) {
640		m_freem(m);
641		return;
642	}
643match:
644	myaddr = ia->ia_addr.sin_addr;
645	if (!bcmp(ar_sha(ah), IF_LLADDR(ifp), ifp->if_addrlen)) {
646		m_freem(m);	/* it's from me, ignore it. */
647		return;
648	}
649	if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
650		log(LOG_ERR,
651		    "arp: link address is broadcast for IP address %s!\n",
652		    inet_ntoa(isaddr));
653		m_freem(m);
654		return;
655	}
656	if (isaddr.s_addr == myaddr.s_addr) {
657		log(LOG_ERR,
658		   "arp: %*D is using my IP address %s!\n",
659		   ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
660		   inet_ntoa(isaddr));
661		itaddr = myaddr;
662		goto reply;
663	}
664	if (ifp->if_flags & IFF_STATICARP)
665		goto reply;
666	la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
667	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
668		/* the following is not an error when doing bridging */
669		if (!BRIDGE_TEST && rt->rt_ifp != ifp) {
670			if (log_arp_wrong_iface)
671				log(LOG_ERR, "arp: %s is on %s but got reply from %*D on %s\n",
672				    inet_ntoa(isaddr),
673				    rt->rt_ifp->if_xname,
674				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
675				    ifp->if_xname);
676			goto reply;
677		}
678		if (sdl->sdl_alen &&
679		    bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
680			if (rt->rt_expire) {
681			    if (log_arp_movements)
682			        log(LOG_INFO, "arp: %s moved from %*D to %*D on %s\n",
683				    inet_ntoa(isaddr),
684				    ifp->if_addrlen, (u_char *)LLADDR(sdl), ":",
685				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
686				    ifp->if_xname);
687			} else {
688			    log(LOG_ERR,
689				"arp: %*D attempts to modify permanent entry for %s on %s\n",
690				ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
691				inet_ntoa(isaddr), ifp->if_xname);
692			    goto reply;
693			}
694		}
695		/*
696		 * sanity check for the address length.
697		 * XXX this does not work for protocols with variable address
698		 * length. -is
699		 */
700		if (sdl->sdl_alen &&
701		    sdl->sdl_alen != ah->ar_hln) {
702			log(LOG_WARNING,
703			    "arp from %*D: new addr len %d, was %d",
704			    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
705			    ah->ar_hln, sdl->sdl_alen);
706		}
707		if (ifp->if_addrlen != ah->ar_hln) {
708			log(LOG_WARNING,
709			    "arp from %*D: addr len: new %d, i/f %d (ignored)",
710			    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
711			    ah->ar_hln, ifp->if_addrlen);
712			goto reply;
713		}
714		(void)memcpy(LLADDR(sdl), ar_sha(ah),
715		    sdl->sdl_alen = ah->ar_hln);
716		/*
717		 * If we receive an arp from a token-ring station over
718		 * a token-ring nic then try to save the source
719		 * routing info.
720		 */
721		if (ifp->if_type == IFT_ISO88025) {
722			th = (struct iso88025_header *)m->m_pkthdr.header;
723			trld = SDL_ISO88025(sdl);
724			rif_len = TR_RCF_RIFLEN(th->rcf);
725			if ((th->iso88025_shost[0] & TR_RII) &&
726			    (rif_len > 2)) {
727				trld->trld_rcf = th->rcf;
728				trld->trld_rcf ^= htons(TR_RCF_DIR);
729				memcpy(trld->trld_route, th->rd, rif_len - 2);
730				trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
731				/*
732				 * Set up source routing information for
733				 * reply packet (XXX)
734				 */
735				m->m_data -= rif_len;
736				m->m_len  += rif_len;
737				m->m_pkthdr.len += rif_len;
738			} else {
739				th->iso88025_shost[0] &= ~TR_RII;
740				trld->trld_rcf = 0;
741			}
742			m->m_data -= 8;
743			m->m_len  += 8;
744			m->m_pkthdr.len += 8;
745			th->rcf = trld->trld_rcf;
746		}
747		RT_LOCK(rt);
748		if (rt->rt_expire)
749			rt->rt_expire = time_second + arpt_keep;
750		rt->rt_flags &= ~RTF_REJECT;
751		RT_UNLOCK(rt);
752		la->la_asked = 0;
753		la->la_preempt = arp_maxtries;
754		if (la->la_hold) {
755			(*ifp->if_output)(ifp, la->la_hold,
756				rt_key(rt), rt);
757			la->la_hold = 0;
758		}
759	}
760reply:
761	if (op != ARPOP_REQUEST) {
762		m_freem(m);
763		return;
764	}
765	if (itaddr.s_addr == myaddr.s_addr) {
766		/* I am the target */
767		(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
768		(void)memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln);
769	} else {
770		la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
771		if (la == NULL) {
772			struct sockaddr_in sin;
773
774			if (!arp_proxyall) {
775				m_freem(m);
776				return;
777			}
778
779			bzero(&sin, sizeof sin);
780			sin.sin_family = AF_INET;
781			sin.sin_len = sizeof sin;
782			sin.sin_addr = itaddr;
783
784			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
785			if (!rt) {
786				m_freem(m);
787				return;
788			}
789			/*
790			 * Don't send proxies for nodes on the same interface
791			 * as this one came out of, or we'll get into a fight
792			 * over who claims what Ether address.
793			 */
794			if (rt->rt_ifp == ifp) {
795				rtfree(rt);
796				m_freem(m);
797				return;
798			}
799			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
800			(void)memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln);
801			rtfree(rt);
802
803			/*
804			 * Also check that the node which sent the ARP packet
805			 * is on the the interface we expect it to be on. This
806			 * avoids ARP chaos if an interface is connected to the
807			 * wrong network.
808			 */
809			sin.sin_addr = isaddr;
810
811			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
812			if (!rt) {
813				m_freem(m);
814				return;
815			}
816			if (rt->rt_ifp != ifp) {
817				log(LOG_INFO, "arp_proxy: ignoring request"
818				    " from %s via %s, expecting %s\n",
819				    inet_ntoa(isaddr), ifp->if_xname,
820				    rt->rt_ifp->if_xname);
821				rtfree(rt);
822				m_freem(m);
823				return;
824			}
825			rtfree(rt);
826
827#ifdef DEBUG_PROXY
828			printf("arp: proxying for %s\n",
829			       inet_ntoa(itaddr));
830#endif
831		} else {
832			rt = la->la_rt;
833			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
834			sdl = SDL(rt->rt_gateway);
835			(void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
836		}
837	}
838
839	(void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
840	(void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
841	ah->ar_op = htons(ARPOP_REPLY);
842	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
843	switch (ifp->if_type) {
844	case IFT_ARCNET:
845		arh = (struct arc_header *)sa.sa_data;
846		arh->arc_dhost = *ar_tha(ah);
847		arh->arc_type = ARCTYPE_ARP;
848		break;
849
850	case IFT_ISO88025:
851		/* Re-arrange the source/dest address */
852		memcpy(th->iso88025_dhost, th->iso88025_shost,
853		    sizeof(th->iso88025_dhost));
854		memcpy(th->iso88025_shost, IF_LLADDR(ifp),
855		    sizeof(th->iso88025_shost));
856		/* Set the source routing bit if neccesary */
857		if (th->iso88025_dhost[0] & TR_RII) {
858			th->iso88025_dhost[0] &= ~TR_RII;
859			if (TR_RCF_RIFLEN(th->rcf) > 2)
860				th->iso88025_shost[0] |= TR_RII;
861		}
862		/* Copy the addresses, ac and fc into sa_data */
863		memcpy(sa.sa_data, th->iso88025_dhost,
864		    sizeof(th->iso88025_dhost) * 2);
865		sa.sa_data[(sizeof(th->iso88025_dhost) * 2)] = TR_AC;
866		sa.sa_data[(sizeof(th->iso88025_dhost) * 2) + 1] = TR_LLC_FRAME;
867		break;
868	case IFT_ETHER:
869	case IFT_FDDI:
870	/*
871	 * May not be correct for types not explictly
872	 * listed, but it is our best guess.
873	 */
874	default:
875		eh = (struct ether_header *)sa.sa_data;
876		(void)memcpy(eh->ether_dhost, ar_tha(ah),
877		    sizeof(eh->ether_dhost));
878		eh->ether_type = htons(ETHERTYPE_ARP);
879		break;
880	}
881	sa.sa_family = AF_UNSPEC;
882	sa.sa_len = sizeof(sa);
883	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
884	return;
885}
886#endif
887
888/*
889 * Free an arp entry.
890 */
891static void
892arptfree(la)
893	register struct llinfo_arp *la;
894{
895	register struct rtentry *rt = la->la_rt;
896	register struct sockaddr_dl *sdl;
897
898	if (rt == 0)
899		panic("arptfree");
900	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
901	    sdl->sdl_family == AF_LINK) {
902		sdl->sdl_alen = 0;
903		la->la_preempt = la->la_asked = 0;
904		RT_LOCK(rt);		/* XXX needed or move higher? */
905		rt->rt_flags &= ~RTF_REJECT;
906		RT_UNLOCK(rt);
907		return;
908	}
909	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
910			0, (struct rtentry **)0);
911}
912/*
913 * Lookup or enter a new address in arptab.
914 */
915static struct llinfo_arp *
916arplookup(addr, create, proxy)
917	u_long addr;
918	int create, proxy;
919{
920	register struct rtentry *rt;
921	struct sockaddr_inarp sin;
922	const char *why = 0;
923
924	bzero(&sin, sizeof(sin));
925	sin.sin_len = sizeof(sin);
926	sin.sin_family = AF_INET;
927	sin.sin_addr.s_addr = addr;
928	if (proxy)
929		sin.sin_other = SIN_PROXY;
930	rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
931	if (rt == 0)
932		return (0);
933
934	if (rt->rt_flags & RTF_GATEWAY)
935		why = "host is not on local network";
936	else if ((rt->rt_flags & RTF_LLINFO) == 0)
937		why = "could not allocate llinfo";
938	else if (rt->rt_gateway->sa_family != AF_LINK)
939		why = "gateway route is not ours";
940
941	if (why) {
942#define	ISDYNCLONE(_rt) \
943	(((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED)
944		if (create)
945			log(LOG_DEBUG, "arplookup %s failed: %s\n",
946			    inet_ntoa(sin.sin_addr), why);
947		/*
948		 * If there are no references to this Layer 2 route,
949		 * and it is a cloned route, and not static, and
950		 * arplookup() is creating the route, then purge
951		 * it from the routing table as it is probably bogus.
952		 */
953		if (rt->rt_refcnt == 1 && ISDYNCLONE(rt))
954			rtexpunge(rt);
955		RTFREE_LOCKED(rt);
956		return (0);
957#undef ISDYNCLONE
958	} else {
959		RT_REMREF(rt);
960		RT_UNLOCK(rt);
961		return ((struct llinfo_arp *)rt->rt_llinfo);
962	}
963}
964
965void
966arp_ifinit(ifp, ifa)
967	struct ifnet *ifp;
968	struct ifaddr *ifa;
969{
970	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
971		arprequest(ifp, &IA_SIN(ifa)->sin_addr,
972				&IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
973	ifa->ifa_rtrequest = arp_rtrequest;
974	ifa->ifa_flags |= RTF_CLONING;
975}
976
977static void
978arp_init(void)
979{
980
981	arpintrq.ifq_maxlen = 50;
982	mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF);
983	LIST_INIT(&llinfo_arp);
984	callout_init(&arp_callout, CALLOUT_MPSAFE);
985	netisr_register(NETISR_ARP, arpintr, &arpintrq, NETISR_MPSAFE);
986}
987SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);
988