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