xref: /freebsd/sys/netinet/if_ether.c (revision 23f282aa31e9b6fceacd449020e936e98d6f2298)
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$
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 
72 SYSCTL_DECL(_net_link_ether);
73 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
74 
75 /* timer values */
76 static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
77 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
78 static int arpt_down = 20;	/* once declared down, don't send for 20 sec */
79 
80 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
81 	   &arpt_prune, 0, "");
82 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
83 	   &arpt_keep, 0, "");
84 SYSCTL_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 
89 struct 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 
97 static	LIST_HEAD(, llinfo_arp) llinfo_arp;
98 
99 struct	ifqueue arpintrq = {0, 0, 0, 50};
100 static int	arp_inuse, arp_allocated;
101 
102 static int	arp_maxtries = 5;
103 static int	useloopback = 1; /* use loopback interface for local traffic */
104 static int	arp_proxyall = 0;
105 
106 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
107 	   &arp_maxtries, 0, "");
108 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
109 	   &useloopback, 0, "");
110 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
111 	   &arp_proxyall, 0, "");
112 
113 static void	arp_rtrequest __P((int, struct rtentry *, struct sockaddr *));
114 static void	arprequest __P((struct arpcom *,
115 			struct in_addr *, struct in_addr *, u_char *));
116 static void	arpintr __P((void));
117 static void	arptfree __P((struct llinfo_arp *));
118 static void	arptimer __P((void *));
119 static struct llinfo_arp
120 		*arplookup __P((u_long, int, int));
121 #ifdef INET
122 static void	in_arpinput __P((struct mbuf *));
123 #endif
124 
125 /*
126  * Timeout routine.  Age arp_tab entries periodically.
127  */
128 /* ARGSUSED */
129 static void
130 arptimer(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  */
150 static void
151 arp_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  */
284 static void
285 arprequest(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  */
357 int
358 arpresolve(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  */
432 static void
433 arpintr()
434 {
435 	register struct mbuf *m;
436 	register struct arphdr *ar;
437 	int s;
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\n");
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 (0x%2D)\n",
457 			    (unsigned char *)&ar->ar_hrd, "");
458 			m_freem(m);
459 			continue;
460 		}
461 
462 		if (m->m_pkthdr.len < sizeof(struct arphdr) + 2 * ar->ar_hln
463 		    + 2 * ar->ar_pln) {
464 			log(LOG_ERR, "arp: runt packet\n");
465 			m_freem(m);
466 			continue;
467 		}
468 
469 		switch (ntohs(ar->ar_pro)) {
470 #ifdef INET
471 			case ETHERTYPE_IP:
472 				in_arpinput(m);
473 				continue;
474 #endif
475 		}
476 		m_freem(m);
477 	}
478 }
479 
480 #ifdef INET
481 /*
482  * ARP for Internet protocols on 10 Mb/s Ethernet.
483  * Algorithm is that given in RFC 826.
484  * In addition, a sanity check is performed on the sender
485  * protocol address, to catch impersonators.
486  * We no longer handle negotiations for use of trailer protocol:
487  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
488  * along with IP replies if we wanted trailers sent to us,
489  * and also sent them in response to IP replies.
490  * This allowed either end to announce the desire to receive
491  * trailer packets.
492  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
493  * but formerly didn't normally send requests.
494  */
495 static void
496 in_arpinput(m)
497 	struct mbuf *m;
498 {
499 	register struct ether_arp *ea;
500 	register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif;
501 	struct ether_header *eh;
502 	struct iso88025_header *th = (struct iso88025_header *)0;
503 	register struct llinfo_arp *la = 0;
504 	register struct rtentry *rt;
505 	struct in_ifaddr *ia, *maybe_ia = 0;
506 	struct sockaddr_dl *sdl;
507 	struct sockaddr sa;
508 	struct in_addr isaddr, itaddr, myaddr;
509 	int op, rif_len;
510 
511 	ea = mtod(m, struct ether_arp *);
512 	op = ntohs(ea->arp_op);
513 	(void)memcpy(&isaddr, ea->arp_spa, sizeof (isaddr));
514 	(void)memcpy(&itaddr, ea->arp_tpa, sizeof (itaddr));
515 	for (ia = in_ifaddrhead.tqh_first; ia; ia = ia->ia_link.tqe_next)
516 #ifdef BRIDGE
517 		/*
518 		 * For a bridge, we want to check the address irrespective
519 		 * of the receive interface. (This will change slightly
520 		 * when we have clusters of interfaces).
521 		 */
522 		{
523 #else
524 		if (ia->ia_ifp == &ac->ac_if) {
525 #endif
526 			maybe_ia = ia;
527 			if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) ||
528 			     (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr))
529 				break;
530 		}
531 	if (maybe_ia == 0) {
532 		m_freem(m);
533 		return;
534 	}
535 	myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr;
536 	if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr,
537 	    sizeof (ea->arp_sha))) {
538 		m_freem(m);	/* it's from me, ignore it. */
539 		return;
540 	}
541 	if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr,
542 	    sizeof (ea->arp_sha))) {
543 		log(LOG_ERR,
544 		    "arp: ether address is broadcast for IP address %s!\n",
545 		    inet_ntoa(isaddr));
546 		m_freem(m);
547 		return;
548 	}
549 	if (isaddr.s_addr == myaddr.s_addr) {
550 		log(LOG_ERR,
551 		   "arp: %6D is using my IP address %s!\n",
552 		   ea->arp_sha, ":", inet_ntoa(isaddr));
553 		itaddr = myaddr;
554 		goto reply;
555 	}
556 	la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
557 	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
558 #ifndef BRIDGE /* the following is not an error when doing bridging */
559 		if (rt->rt_ifp != &ac->ac_if) {
560 			log(LOG_ERR, "arp: %s is on %s%d but got reply from %6D on %s%d\n",
561 			    inet_ntoa(isaddr),
562 			    rt->rt_ifp->if_name, rt->rt_ifp->if_unit,
563 			    ea->arp_sha, ":",
564 			    ac->ac_if.if_name, ac->ac_if.if_unit);
565 			goto reply;
566 		}
567 #endif
568 		if (sdl->sdl_alen &&
569 		    bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen)) {
570 			if (rt->rt_expire)
571 			    log(LOG_INFO, "arp: %s moved from %6D to %6D on %s%d\n",
572 				inet_ntoa(isaddr), (u_char *)LLADDR(sdl), ":",
573 				ea->arp_sha, ":",
574 				ac->ac_if.if_name, ac->ac_if.if_unit);
575 			else {
576 			    log(LOG_ERR,
577 				"arp: %6D attempts to modify permanent entry for %s on %s%d\n",
578 				ea->arp_sha, ":", inet_ntoa(isaddr),
579 				ac->ac_if.if_name, ac->ac_if.if_unit);
580 			    goto reply;
581 			}
582 		}
583 		(void)memcpy(LLADDR(sdl), ea->arp_sha, sizeof(ea->arp_sha));
584 		sdl->sdl_alen = sizeof(ea->arp_sha);
585                 sdl->sdl_rcf = (u_short)0;
586 		/*
587 		 * If we receive an arp from a token-ring station over
588 		 * a token-ring nic then try to save the source
589 		 * routing info.
590 		 */
591 		if (ac->ac_if.if_type == IFT_ISO88025) {
592 			th = (struct iso88025_header *)m->m_pkthdr.header;
593 			rif_len = TR_RCF_RIFLEN(th->rcf);
594 			if ((th->iso88025_shost[0] & TR_RII) &&
595 			    (rif_len > 2)) {
596 				sdl->sdl_rcf = th->rcf;
597 				sdl->sdl_rcf ^= htons(TR_RCF_DIR);
598 				memcpy(sdl->sdl_route, th->rd, rif_len - 2);
599 				sdl->sdl_rcf &= ~htons(TR_RCF_BCST_MASK);
600 				/*
601 				 * Set up source routing information for
602 				 * reply packet (XXX)
603 				 */
604 				m->m_data -= rif_len;
605 				m->m_len  += rif_len;
606 				m->m_pkthdr.len += rif_len;
607 			} else {
608 				th->iso88025_shost[0] &= ~TR_RII;
609 			}
610 			m->m_data -= 8;
611 			m->m_len  += 8;
612 			m->m_pkthdr.len += 8;
613 			th->rcf = sdl->sdl_rcf;
614 		} else {
615 			sdl->sdl_rcf = (u_short)0;
616 		}
617 		if (rt->rt_expire)
618 			rt->rt_expire = time_second + arpt_keep;
619 		rt->rt_flags &= ~RTF_REJECT;
620 		la->la_asked = 0;
621 		if (la->la_hold) {
622 			(*ac->ac_if.if_output)(&ac->ac_if, la->la_hold,
623 				rt_key(rt), rt);
624 			la->la_hold = 0;
625 		}
626 	}
627 reply:
628 	if (op != ARPOP_REQUEST) {
629 		m_freem(m);
630 		return;
631 	}
632 	if (itaddr.s_addr == myaddr.s_addr) {
633 		/* I am the target */
634 		(void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
635 		(void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
636 	} else {
637 		la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
638 		if (la == NULL) {
639 			struct sockaddr_in sin;
640 
641 			if (!arp_proxyall) {
642 				m_freem(m);
643 				return;
644 			}
645 
646 			bzero(&sin, sizeof sin);
647 			sin.sin_family = AF_INET;
648 			sin.sin_len = sizeof sin;
649 			sin.sin_addr = itaddr;
650 
651 			rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
652 			if (!rt) {
653 				m_freem(m);
654 				return;
655 			}
656 			/*
657 			 * Don't send proxies for nodes on the same interface
658 			 * as this one came out of, or we'll get into a fight
659 			 * over who claims what Ether address.
660 			 */
661 			if (rt->rt_ifp == &ac->ac_if) {
662 				rtfree(rt);
663 				m_freem(m);
664 				return;
665 			}
666 			(void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
667 			(void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
668 			rtfree(rt);
669 #ifdef DEBUG_PROXY
670 			printf("arp: proxying for %s\n",
671 			       inet_ntoa(itaddr));
672 #endif
673 		} else {
674 			rt = la->la_rt;
675 			(void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
676 			sdl = SDL(rt->rt_gateway);
677 			(void)memcpy(ea->arp_sha, LLADDR(sdl), sizeof(ea->arp_sha));
678 		}
679 	}
680 
681 	(void)memcpy(ea->arp_tpa, ea->arp_spa, sizeof(ea->arp_spa));
682 	(void)memcpy(ea->arp_spa, &itaddr, sizeof(ea->arp_spa));
683 	ea->arp_op = htons(ARPOP_REPLY);
684 	ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */
685 	switch (ac->ac_if.if_type) {
686 	case IFT_ISO88025:
687 		/* Re-arrange the source/dest address */
688 		memcpy(th->iso88025_dhost, th->iso88025_shost,
689 		    sizeof(th->iso88025_dhost));
690 		memcpy(th->iso88025_shost, ac->ac_enaddr,
691 		    sizeof(th->iso88025_shost));
692 		/* Set the source routing bit if neccesary */
693 		if (th->iso88025_dhost[0] & TR_RII) {
694 			th->iso88025_dhost[0] &= ~TR_RII;
695 			if (TR_RCF_RIFLEN(th->rcf) > 2)
696 				th->iso88025_shost[0] |= TR_RII;
697 		}
698 		/* Copy the addresses, ac and fc into sa_data */
699 		memcpy(sa.sa_data, th->iso88025_dhost,
700 		    sizeof(th->iso88025_dhost) * 2);
701 		sa.sa_data[(sizeof(th->iso88025_dhost) * 2)] = TR_AC;
702 		sa.sa_data[(sizeof(th->iso88025_dhost) * 2) + 1] = TR_LLC_FRAME;
703 		break;
704 	case IFT_ETHER:
705 	case IFT_FDDI:
706 	/*
707 	 * May not be correct for types not explictly
708 	 * listed, but it is our best guess.
709 	 */
710 	default:
711 		eh = (struct ether_header *)sa.sa_data;
712 		(void)memcpy(eh->ether_dhost, ea->arp_tha,
713 		    sizeof(eh->ether_dhost));
714 		eh->ether_type = htons(ETHERTYPE_ARP);
715 		break;
716 	}
717 	sa.sa_family = AF_UNSPEC;
718 	sa.sa_len = sizeof(sa);
719 	(*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
720 	return;
721 }
722 #endif
723 
724 /*
725  * Free an arp entry.
726  */
727 static void
728 arptfree(la)
729 	register struct llinfo_arp *la;
730 {
731 	register struct rtentry *rt = la->la_rt;
732 	register struct sockaddr_dl *sdl;
733 	if (rt == 0)
734 		panic("arptfree");
735 	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
736 	    sdl->sdl_family == AF_LINK) {
737 		sdl->sdl_alen = 0;
738 		la->la_asked = 0;
739 		rt->rt_flags &= ~RTF_REJECT;
740 		return;
741 	}
742 	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
743 			0, (struct rtentry **)0);
744 }
745 /*
746  * Lookup or enter a new address in arptab.
747  */
748 static struct llinfo_arp *
749 arplookup(addr, create, proxy)
750 	u_long addr;
751 	int create, proxy;
752 {
753 	register struct rtentry *rt;
754 	static struct sockaddr_inarp sin = {sizeof(sin), AF_INET };
755 	const char *why = 0;
756 
757 	sin.sin_addr.s_addr = addr;
758 	sin.sin_other = proxy ? SIN_PROXY : 0;
759 	rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
760 	if (rt == 0)
761 		return (0);
762 	rt->rt_refcnt--;
763 
764 	if (rt->rt_flags & RTF_GATEWAY)
765 		why = "host is not on local network";
766 	else if ((rt->rt_flags & RTF_LLINFO) == 0)
767 		why = "could not allocate llinfo";
768 	else if (rt->rt_gateway->sa_family != AF_LINK)
769 		why = "gateway route is not ours";
770 
771 	if (why && create) {
772 		log(LOG_DEBUG, "arplookup %s failed: %s\n",
773 		    inet_ntoa(sin.sin_addr), why);
774 		return 0;
775 	} else if (why) {
776 		return 0;
777 	}
778 	return ((struct llinfo_arp *)rt->rt_llinfo);
779 }
780 
781 void
782 arp_ifinit(ac, ifa)
783 	struct arpcom *ac;
784 	struct ifaddr *ifa;
785 {
786 	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
787 		arprequest(ac, &IA_SIN(ifa)->sin_addr,
788 			       &IA_SIN(ifa)->sin_addr, ac->ac_enaddr);
789 	ifa->ifa_rtrequest = arp_rtrequest;
790 	ifa->ifa_flags |= RTF_CLONING;
791 }
792