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