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