if_ether.c revision 32350
1296781Sdes/*
2124208Sdes * Copyright (c) 1982, 1986, 1988, 1993
3124208Sdes *	The Regents of the University of California.  All rights reserved.
4255670Sdes *
5124208Sdes * Redistribution and use in source and binary forms, with or without
6255670Sdes * modification, are permitted provided that the following conditions
7124208Sdes * are met:
8255670Sdes * 1. Redistributions of source code must retain the above copyright
9295367Sdes *    notice, this list of conditions and the following disclaimer.
10124208Sdes * 2. Redistributions in binary form must reproduce the above copyright
11262566Sdes *    notice, this list of conditions and the following disclaimer in the
12262566Sdes *    documentation and/or other materials provided with the distribution.
13262566Sdes * 3. All advertising materials mentioning features or use of this software
14262566Sdes *    must display the following acknowledgement:
15295367Sdes *	This product includes software developed by the University of
16295367Sdes *	California, Berkeley and its contributors.
17295367Sdes * 4. Neither the name of the University nor the names of its contributors
18295367Sdes *    may be used to endorse or promote products derived from this software
19295367Sdes *    without specific prior written permission.
20295367Sdes *
21295367Sdes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22255670Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23295367Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24295367Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25124208Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26262566Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27124208Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28262566Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29124208Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30124208Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31124208Sdes * SUCH DAMAGE.
32124208Sdes *
33262566Sdes *	@(#)if_ether.c	8.1 (Berkeley) 6/10/93
34124208Sdes * $Id: if_ether.c,v 1.42 1997/12/20 00:07:11 bde Exp $
35262566Sdes */
36262566Sdes
37262566Sdes/*
38262566Sdes * Ethernet address resolution protocol.
39262566Sdes * TODO:
40262566Sdes *	add "inuse/lock" bit (or ref. count) along with valid bit
41262566Sdes */
42124208Sdes
43262566Sdes#include "opt_inet.h"
44262566Sdes
45262566Sdes#include <sys/param.h>
46262566Sdes#include <sys/kernel.h>
47262566Sdes#include <sys/sysctl.h>
48262566Sdes#include <sys/systm.h>
49255670Sdes#include <sys/mbuf.h>
50262566Sdes#include <sys/malloc.h>
51262566Sdes#include <sys/socket.h>
52295367Sdes#include <sys/syslog.h>
53262566Sdes
54262566Sdes#include <net/if.h>
55262566Sdes#include <net/if_dl.h>
56262566Sdes#include <net/route.h>
57262566Sdes#include <net/netisr.h>
58262566Sdes
59262566Sdes#include <netinet/in.h>
60295367Sdes#include <netinet/in_var.h>
61262566Sdes#include <netinet/if_ether.h>
62262566Sdes
63262566Sdes#define SIN(s) ((struct sockaddr_in *)s)
64262566Sdes#define SDL(s) ((struct sockaddr_dl *)s)
65262566Sdes
66262566SdesSYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
67295367Sdes
68262566Sdes/* timer values */
69262566Sdesstatic int arpt_prune = (5*60*1); /* walk list every 5 minutes */
70255670Sdesstatic int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
71255670Sdesstatic int arpt_down = 20;	/* once declared down, don't send for 20 sec */
72255670Sdes
73262566SdesSYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
74262566Sdes	   &arpt_prune, 0, "");
75255670SdesSYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
76255670Sdes	   &arpt_keep, 0, "");
77255670SdesSYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
78262566Sdes	   &arpt_down, 0, "");
79255670Sdes
80255670Sdes#define	rt_expire rt_rmx.rmx_expire
81255670Sdes
82255670Sdesstruct llinfo_arp {
83255670Sdes	LIST_ENTRY(llinfo_arp) la_le;
84255670Sdes	struct	rtentry *la_rt;
85255670Sdes	struct	mbuf *la_hold;		/* last packet until resolved/timeout */
86255670Sdes	long	la_asked;		/* last time we QUERIED for this addr */
87255670Sdes#define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */
88255670Sdes};
89255670Sdes
90255670Sdesstatic	LIST_HEAD(, llinfo_arp) llinfo_arp;
91255670Sdes
92255670Sdesstruct	ifqueue arpintrq = {0, 0, 0, 50};
93255670Sdesstatic int	arp_inuse, arp_allocated;
94255670Sdes
95255670Sdesstatic int	arp_maxtries = 5;
96255670Sdesstatic int	useloopback = 1; /* use loopback interface for local traffic */
97255670Sdesstatic int	arp_proxyall = 0;
98255670Sdes
99255670SdesSYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
100255670Sdes	   &arp_maxtries, 0, "");
101255670SdesSYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
102255670Sdes	   &useloopback, 0, "");
103295367SdesSYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
104295367Sdes	   &arp_proxyall, 0, "");
105295367Sdes
106295367Sdesstatic void	arp_rtrequest __P((int, struct rtentry *, struct sockaddr *));
107295367Sdesstatic void	arprequest __P((struct arpcom *, u_long *, u_long *, u_char *));
108295367Sdesstatic void	arpintr __P((void));
109295367Sdesstatic void	arptfree __P((struct llinfo_arp *));
110295367Sdesstatic void	arptimer __P((void *));
111295367Sdesstatic struct llinfo_arp
112295367Sdes		*arplookup __P((u_long, int, int));
113295367Sdes#ifdef INET
114295367Sdesstatic void	in_arpinput __P((struct mbuf *));
115295367Sdes#endif
116295367Sdes
117295367Sdes/*
118295367Sdes * Timeout routine.  Age arp_tab entries periodically.
119295367Sdes */
120295367Sdes/* ARGSUSED */
121295367Sdesstatic void
122255670Sdesarptimer(ignored_arg)
123255670Sdes	void *ignored_arg;
124295367Sdes{
125295367Sdes	int s = splnet();
126255670Sdes	register struct llinfo_arp *la = llinfo_arp.lh_first;
127255670Sdes	struct llinfo_arp *ola;
128255670Sdes
129255670Sdes	timeout(arptimer, (caddr_t)0, arpt_prune * hz);
130255670Sdes	while ((ola = la) != 0) {
131255670Sdes		register struct rtentry *rt = la->la_rt;
132255670Sdes		la = la->la_le.le_next;
133255670Sdes		if (rt->rt_expire && rt->rt_expire <= time.tv_sec)
134255670Sdes			arptfree(ola); /* timer has expired, clear */
135255670Sdes	}
136255670Sdes	splx(s);
137255670Sdes}
138255670Sdes
139255670Sdes/*
140296781Sdes * Parallel to llc_rtrequest.
141255670Sdes */
142255670Sdesstatic void
143255670Sdesarp_rtrequest(req, rt, sa)
144255670Sdes	int req;
145255670Sdes	register struct rtentry *rt;
146255670Sdes	struct sockaddr *sa;
147296781Sdes{
148296781Sdes	register struct sockaddr *gate = rt->rt_gateway;
149255670Sdes	register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
150255670Sdes	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
151255670Sdes	static int arpinit_done;
152255670Sdes
153255670Sdes	if (!arpinit_done) {
154255670Sdes		arpinit_done = 1;
155255670Sdes		LIST_INIT(&llinfo_arp);
156255670Sdes		timeout(arptimer, (caddr_t)0, hz);
157255670Sdes	}
158255670Sdes	if (rt->rt_flags & RTF_GATEWAY)
159255670Sdes		return;
160255670Sdes	switch (req) {
161255670Sdes
162255670Sdes	case RTM_ADD:
163255670Sdes		/*
164262566Sdes		 * XXX: If this is a manually added route to interface
165255670Sdes		 * such as older version of routed or gated might provide,
166255670Sdes		 * restore cloning bit.
167262566Sdes		 */
168255670Sdes		if ((rt->rt_flags & RTF_HOST) == 0 &&
169255670Sdes		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
170255670Sdes			rt->rt_flags |= RTF_CLONING;
171255670Sdes		if (rt->rt_flags & RTF_CLONING) {
172255670Sdes			/*
173			 * Case 1: This route should come from a route to iface.
174			 */
175			rt_setgate(rt, rt_key(rt),
176					(struct sockaddr *)&null_sdl);
177			gate = rt->rt_gateway;
178			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
179			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
180			rt->rt_expire = time.tv_sec;
181			break;
182		}
183		/* Announce a new entry if requested. */
184		if (rt->rt_flags & RTF_ANNOUNCE)
185			arprequest((struct arpcom *)rt->rt_ifp,
186			    &SIN(rt_key(rt))->sin_addr.s_addr,
187			    &SIN(rt_key(rt))->sin_addr.s_addr,
188			    (u_char *)LLADDR(SDL(gate)));
189		/*FALLTHROUGH*/
190	case RTM_RESOLVE:
191		if (gate->sa_family != AF_LINK ||
192		    gate->sa_len < sizeof(null_sdl)) {
193			log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
194			break;
195		}
196		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
197		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
198		if (la != 0)
199			break; /* This happens on a route change */
200		/*
201		 * Case 2:  This route may come from cloning, or a manual route
202		 * add with a LL address.
203		 */
204		R_Malloc(la, struct llinfo_arp *, sizeof(*la));
205		rt->rt_llinfo = (caddr_t)la;
206		if (la == 0) {
207			log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
208			break;
209		}
210		arp_inuse++, arp_allocated++;
211		Bzero(la, sizeof(*la));
212		la->la_rt = rt;
213		rt->rt_flags |= RTF_LLINFO;
214		LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
215
216#ifdef INET
217		/*
218		 * This keeps the multicast addresses from showing up
219		 * in `arp -a' listings as unresolved.  It's not actually
220		 * functional.  Then the same for broadcast.
221		 */
222		if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
223			ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
224					       LLADDR(SDL(gate)));
225			SDL(gate)->sdl_alen = 6;
226			rt->rt_expire = 0;
227		}
228		if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
229			memcpy(LLADDR(SDL(gate)), etherbroadcastaddr, 6);
230			SDL(gate)->sdl_alen = 6;
231			rt->rt_expire = 0;
232		}
233#endif
234
235		if (SIN(rt_key(rt))->sin_addr.s_addr ==
236		    (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
237		    /*
238		     * This test used to be
239		     *	if (loif.if_flags & IFF_UP)
240		     * It allowed local traffic to be forced
241		     * through the hardware by configuring the loopback down.
242		     * However, it causes problems during network configuration
243		     * for boards that can't receive packets they send.
244		     * It is now necessary to clear "useloopback" and remove
245		     * the route to force traffic out to the hardware.
246		     */
247			rt->rt_expire = 0;
248			Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr,
249				LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6);
250			if (useloopback)
251				rt->rt_ifp = loif;
252
253		}
254		break;
255
256	case RTM_DELETE:
257		if (la == 0)
258			break;
259		arp_inuse--;
260		LIST_REMOVE(la, la_le);
261		rt->rt_llinfo = 0;
262		rt->rt_flags &= ~RTF_LLINFO;
263		if (la->la_hold)
264			m_freem(la->la_hold);
265		Free((caddr_t)la);
266	}
267}
268
269/*
270 * Broadcast an ARP request. Caller specifies:
271 *	- arp header source ip address
272 *	- arp header target ip address
273 *	- arp header source ethernet address
274 */
275static void
276arprequest(ac, sip, tip, enaddr)
277	register struct arpcom *ac;
278	register u_long *sip, *tip;
279	register u_char *enaddr;
280{
281	register struct mbuf *m;
282	register struct ether_header *eh;
283	register struct ether_arp *ea;
284	struct sockaddr sa;
285
286	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
287		return;
288	m->m_len = sizeof(*ea);
289	m->m_pkthdr.len = sizeof(*ea);
290	MH_ALIGN(m, sizeof(*ea));
291	ea = mtod(m, struct ether_arp *);
292	eh = (struct ether_header *)sa.sa_data;
293	bzero((caddr_t)ea, sizeof (*ea));
294	(void)memcpy(eh->ether_dhost, etherbroadcastaddr, sizeof(eh->ether_dhost));
295	eh->ether_type = htons(ETHERTYPE_ARP);	/* if_output will not swap */
296	ea->arp_hrd = htons(ARPHRD_ETHER);
297	ea->arp_pro = htons(ETHERTYPE_IP);
298	ea->arp_hln = sizeof(ea->arp_sha);	/* hardware address length */
299	ea->arp_pln = sizeof(ea->arp_spa);	/* protocol address length */
300	ea->arp_op = htons(ARPOP_REQUEST);
301	(void)memcpy(ea->arp_sha, enaddr, sizeof(ea->arp_sha));
302	(void)memcpy(ea->arp_spa, sip, sizeof(ea->arp_spa));
303	(void)memcpy(ea->arp_tpa, tip, sizeof(ea->arp_tpa));
304	sa.sa_family = AF_UNSPEC;
305	sa.sa_len = sizeof(sa);
306	(*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
307}
308
309/*
310 * Resolve an IP address into an ethernet address.  If success,
311 * desten is filled in.  If there is no entry in arptab,
312 * set one up and broadcast a request for the IP address.
313 * Hold onto this mbuf and resend it once the address
314 * is finally resolved.  A return value of 1 indicates
315 * that desten has been filled in and the packet should be sent
316 * normally; a 0 return indicates that the packet has been
317 * taken over here, either now or for later transmission.
318 */
319int
320arpresolve(ac, rt, m, dst, desten, rt0)
321	register struct arpcom *ac;
322	register struct rtentry *rt;
323	struct mbuf *m;
324	register struct sockaddr *dst;
325	register u_char *desten;
326	struct rtentry *rt0;
327{
328	register struct llinfo_arp *la;
329	struct sockaddr_dl *sdl;
330
331	if (m->m_flags & M_BCAST) {	/* broadcast */
332		(void)memcpy(desten, etherbroadcastaddr, sizeof(etherbroadcastaddr));
333		return (1);
334	}
335	if (m->m_flags & M_MCAST) {	/* multicast */
336		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
337		return(1);
338	}
339	if (rt)
340		la = (struct llinfo_arp *)rt->rt_llinfo;
341	else {
342		la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
343		if (la)
344			rt = la->la_rt;
345	}
346	if (la == 0 || rt == 0) {
347		log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s\n",
348			inet_ntoa(SIN(dst)->sin_addr));
349		m_freem(m);
350		return (0);
351	}
352	sdl = SDL(rt->rt_gateway);
353	/*
354	 * Check the address family and length is valid, the address
355	 * is resolved; otherwise, try to resolve.
356	 */
357	if ((rt->rt_expire == 0 || rt->rt_expire > time.tv_sec) &&
358	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
359		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
360		return 1;
361	}
362	/*
363	 * There is an arptab entry, but no ethernet address
364	 * response yet.  Replace the held mbuf with this
365	 * latest one.
366	 */
367	if (la->la_hold)
368		m_freem(la->la_hold);
369	la->la_hold = m;
370	if (rt->rt_expire) {
371		rt->rt_flags &= ~RTF_REJECT;
372		if (la->la_asked == 0 || rt->rt_expire != time.tv_sec) {
373			rt->rt_expire = time.tv_sec;
374			if (la->la_asked++ < arp_maxtries)
375			    arprequest(ac,
376			        &(SIN(rt->rt_ifa->ifa_addr)->sin_addr.s_addr),
377				&(SIN(dst)->sin_addr.s_addr),
378				ac->ac_enaddr);
379			else {
380				rt->rt_flags |= RTF_REJECT;
381				rt->rt_expire += arpt_down;
382				la->la_asked = 0;
383			}
384
385		}
386	}
387	return (0);
388}
389
390/*
391 * Common length and type checks are done here,
392 * then the protocol-specific routine is called.
393 */
394static void
395arpintr()
396{
397	register struct mbuf *m;
398	register struct arphdr *ar;
399	int s;
400
401	while (arpintrq.ifq_head) {
402		s = splimp();
403		IF_DEQUEUE(&arpintrq, m);
404		splx(s);
405		if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
406			panic("arpintr");
407		if (m->m_len >= sizeof(struct arphdr) &&
408		    (ar = mtod(m, struct arphdr *)) &&
409		    ntohs(ar->ar_hrd) == ARPHRD_ETHER &&
410		    m->m_len >=
411		      sizeof(struct arphdr) + 2 * ar->ar_hln + 2 * ar->ar_pln)
412
413			    switch (ntohs(ar->ar_pro)) {
414
415#ifdef INET
416			    case ETHERTYPE_IP:
417				    in_arpinput(m);
418				    continue;
419#endif
420			    }
421		m_freem(m);
422	}
423}
424
425NETISR_SET(NETISR_ARP, arpintr);
426
427
428#ifdef INET
429/*
430 * ARP for Internet protocols on 10 Mb/s Ethernet.
431 * Algorithm is that given in RFC 826.
432 * In addition, a sanity check is performed on the sender
433 * protocol address, to catch impersonators.
434 * We no longer handle negotiations for use of trailer protocol:
435 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
436 * along with IP replies if we wanted trailers sent to us,
437 * and also sent them in response to IP replies.
438 * This allowed either end to announce the desire to receive
439 * trailer packets.
440 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
441 * but formerly didn't normally send requests.
442 */
443static void
444in_arpinput(m)
445	struct mbuf *m;
446{
447	register struct ether_arp *ea;
448	register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif;
449	struct ether_header *eh;
450	register struct llinfo_arp *la = 0;
451	register struct rtentry *rt;
452	struct in_ifaddr *ia, *maybe_ia = 0;
453	struct sockaddr_dl *sdl;
454	struct sockaddr sa;
455	struct in_addr isaddr, itaddr, myaddr;
456	int op;
457
458	ea = mtod(m, struct ether_arp *);
459	op = ntohs(ea->arp_op);
460	(void)memcpy(&isaddr, ea->arp_spa, sizeof (isaddr));
461	(void)memcpy(&itaddr, ea->arp_tpa, sizeof (itaddr));
462	for (ia = in_ifaddrhead.tqh_first; ia; ia = ia->ia_link.tqe_next)
463		if (ia->ia_ifp == &ac->ac_if) {
464			maybe_ia = ia;
465			if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) ||
466			     (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr))
467				break;
468		}
469	if (maybe_ia == 0) {
470		m_freem(m);
471		return;
472	}
473	myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr;
474	if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr,
475	    sizeof (ea->arp_sha))) {
476		m_freem(m);	/* it's from me, ignore it. */
477		return;
478	}
479	if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr,
480	    sizeof (ea->arp_sha))) {
481		log(LOG_ERR,
482		    "arp: ether address is broadcast for IP address %s!\n",
483		    inet_ntoa(isaddr));
484		m_freem(m);
485		return;
486	}
487	if (isaddr.s_addr == myaddr.s_addr) {
488		log(LOG_ERR,
489		   "arp: %6D is using my IP address %s!\n",
490		   ea->arp_sha, ":", inet_ntoa(isaddr));
491		itaddr = myaddr;
492		goto reply;
493	}
494	la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
495	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
496		if (sdl->sdl_alen &&
497		    bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen))
498			log(LOG_INFO, "arp: %s moved from %6D to %6D\n",
499			    inet_ntoa(isaddr), (u_char *)LLADDR(sdl), ":",
500			    ea->arp_sha, ":");
501		(void)memcpy(LLADDR(sdl), ea->arp_sha, sizeof(ea->arp_sha));
502		sdl->sdl_alen = sizeof(ea->arp_sha);
503		if (rt->rt_expire)
504			rt->rt_expire = time.tv_sec + arpt_keep;
505		rt->rt_flags &= ~RTF_REJECT;
506		la->la_asked = 0;
507		if (la->la_hold) {
508			(*ac->ac_if.if_output)(&ac->ac_if, la->la_hold,
509				rt_key(rt), rt);
510			la->la_hold = 0;
511		}
512	}
513reply:
514	if (op != ARPOP_REQUEST) {
515		m_freem(m);
516		return;
517	}
518	if (itaddr.s_addr == myaddr.s_addr) {
519		/* I am the target */
520		(void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
521		(void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
522	} else {
523		la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
524		if (la == NULL) {
525			struct sockaddr_in sin;
526
527			if (!arp_proxyall) {
528				m_freem(m);
529				return;
530			}
531
532			bzero(&sin, sizeof sin);
533			sin.sin_family = AF_INET;
534			sin.sin_len = sizeof sin;
535			sin.sin_addr = itaddr;
536
537			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
538			if (!rt) {
539				m_freem(m);
540				return;
541			}
542			/*
543			 * Don't send proxies for nodes on the same interface
544			 * as this one came out of, or we'll get into a fight
545			 * over who claims what Ether address.
546			 */
547			if (rt->rt_ifp == &ac->ac_if) {
548				rtfree(rt);
549				m_freem(m);
550				return;
551			}
552			(void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
553			(void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
554			rtfree(rt);
555#ifdef DEBUG_PROXY
556			printf("arp: proxying for %s\n",
557			       inet_ntoa(itaddr));
558#endif
559		} else {
560			rt = la->la_rt;
561			(void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
562			sdl = SDL(rt->rt_gateway);
563			(void)memcpy(ea->arp_sha, LLADDR(sdl), sizeof(ea->arp_sha));
564		}
565	}
566
567	(void)memcpy(ea->arp_tpa, ea->arp_spa, sizeof(ea->arp_spa));
568	(void)memcpy(ea->arp_spa, &itaddr, sizeof(ea->arp_spa));
569	ea->arp_op = htons(ARPOP_REPLY);
570	ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */
571	eh = (struct ether_header *)sa.sa_data;
572	(void)memcpy(eh->ether_dhost, ea->arp_tha, sizeof(eh->ether_dhost));
573	eh->ether_type = htons(ETHERTYPE_ARP);
574	sa.sa_family = AF_UNSPEC;
575	sa.sa_len = sizeof(sa);
576	(*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
577	return;
578}
579#endif
580
581/*
582 * Free an arp entry.
583 */
584static void
585arptfree(la)
586	register struct llinfo_arp *la;
587{
588	register struct rtentry *rt = la->la_rt;
589	register struct sockaddr_dl *sdl;
590	if (rt == 0)
591		panic("arptfree");
592	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
593	    sdl->sdl_family == AF_LINK) {
594		sdl->sdl_alen = 0;
595		la->la_asked = 0;
596		rt->rt_flags &= ~RTF_REJECT;
597		return;
598	}
599	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
600			0, (struct rtentry **)0);
601}
602/*
603 * Lookup or enter a new address in arptab.
604 */
605static struct llinfo_arp *
606arplookup(addr, create, proxy)
607	u_long addr;
608	int create, proxy;
609{
610	register struct rtentry *rt;
611	static struct sockaddr_inarp sin = {sizeof(sin), AF_INET };
612	const char *why = 0;
613
614	sin.sin_addr.s_addr = addr;
615	sin.sin_other = proxy ? SIN_PROXY : 0;
616	rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
617	if (rt == 0)
618		return (0);
619	rt->rt_refcnt--;
620
621	if (rt->rt_flags & RTF_GATEWAY)
622		why = "host is not on local network";
623	else if ((rt->rt_flags & RTF_LLINFO) == 0)
624		why = "could not allocate llinfo";
625	else if (rt->rt_gateway->sa_family != AF_LINK)
626		why = "gateway route is not ours";
627
628	if (why && create) {
629		log(LOG_DEBUG, "arplookup %s failed: %s\n",
630		    inet_ntoa(sin.sin_addr), why);
631		return 0;
632	} else if (why) {
633		return 0;
634	}
635	return ((struct llinfo_arp *)rt->rt_llinfo);
636}
637
638void
639arp_ifinit(ac, ifa)
640	struct arpcom *ac;
641	struct ifaddr *ifa;
642{
643	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
644		arprequest(ac, &(IA_SIN(ifa)->sin_addr.s_addr),
645			       &(IA_SIN(ifa)->sin_addr.s_addr), ac->ac_enaddr);
646	ifa->ifa_rtrequest = arp_rtrequest;
647	ifa->ifa_flags |= RTF_CLONING;
648}
649