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