xref: /dflybsd-src/sys/netinet/if_ether.c (revision f7e25d559127833cbbb89b74fcf0cc036406459b)
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: src/sys/netinet/if_ether.c,v 1.64.2.23 2003/04/11 07:23:15 fjoe Exp $
35  * $DragonFly: src/sys/netinet/if_ether.c,v 1.21 2004/12/21 02:54:15 hsu Exp $
36  */
37 
38 /*
39  * Ethernet address resolution protocol.
40  * TODO:
41  *	add "inuse/lock" bit (or ref. count) along with valid bit
42  */
43 
44 #include "opt_inet.h"
45 #include "opt_bdg.h"
46 
47 #include <sys/param.h>
48 #include <sys/kernel.h>
49 #include <sys/queue.h>
50 #include <sys/sysctl.h>
51 #include <sys/systm.h>
52 #include <sys/mbuf.h>
53 #include <sys/malloc.h>
54 #include <sys/socket.h>
55 #include <sys/syslog.h>
56 
57 #include <sys/thread2.h>
58 #include <sys/msgport2.h>
59 
60 #include <net/if.h>
61 #include <net/if_dl.h>
62 #include <net/if_types.h>
63 #include <net/route.h>
64 #include <net/netisr.h>
65 #include <net/if_llc.h>
66 #ifdef BRIDGE
67 #include <net/ethernet.h>
68 #include <net/bridge/bridge.h>
69 #endif
70 
71 #include <netinet/in.h>
72 #include <netinet/in_var.h>
73 #include <netinet/if_ether.h>
74 
75 #include <net/if_arc.h>
76 #include <net/iso88025.h>
77 
78 #define SIN(s) ((struct sockaddr_in *)s)
79 #define SDL(s) ((struct sockaddr_dl *)s)
80 
81 SYSCTL_DECL(_net_link_ether);
82 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
83 
84 /* timer values */
85 static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
86 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
87 static int arpt_down = 20;	/* once declared down, don't send for 20 sec */
88 
89 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
90 	   &arpt_prune, 0, "");
91 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
92 	   &arpt_keep, 0, "");
93 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
94 	   &arpt_down, 0, "");
95 
96 #define	rt_expire	rt_rmx.rmx_expire
97 
98 struct llinfo_arp {
99 	LIST_ENTRY(llinfo_arp) la_le;
100 	struct	rtentry *la_rt;
101 	struct	mbuf *la_hold;	/* last packet until resolved/timeout */
102 	u_short	la_preempt;	/* countdown for pre-expiry arps */
103 	u_short	la_asked;	/* #times we QUERIED following expiration */
104 };
105 
106 static	LIST_HEAD(, llinfo_arp) llinfo_arp;
107 
108 static int	arp_maxtries = 5;
109 static int	useloopback = 1; /* use loopback interface for local traffic */
110 static int	arp_proxyall = 0;
111 
112 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
113 	   &arp_maxtries, 0, "");
114 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
115 	   &useloopback, 0, "");
116 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
117 	   &arp_proxyall, 0, "");
118 
119 static void	arp_rtrequest (int, struct rtentry *, struct rt_addrinfo *);
120 static void	arprequest (struct ifnet *,
121 			struct in_addr *, struct in_addr *, u_char *);
122 static int	arpintr(struct netmsg *);
123 static void	arptfree (struct llinfo_arp *);
124 static void	arptimer (void *);
125 static struct llinfo_arp
126 		*arplookup (in_addr_t addr, boolean_t create, boolean_t proxy);
127 #ifdef INET
128 static void	in_arpinput (struct mbuf *);
129 #endif
130 
131 static struct callout	arptimer_ch;
132 
133 /*
134  * Timeout routine.  Age arp_tab entries periodically.
135  */
136 /* ARGSUSED */
137 static void
138 arptimer(void *ignored_arg)
139 {
140 	int s = splnet();
141 	struct llinfo_arp *la, *nla;
142 
143 	LIST_FOREACH_MUTABLE(la, &llinfo_arp, la_le, nla) {
144 		if (la->la_rt->rt_expire && la->la_rt->rt_expire <= time_second)
145 			arptfree(la);	/* might remove la from llinfo_arp! */
146 	}
147 	callout_reset(&arptimer_ch, arpt_prune * hz, arptimer, NULL);
148 	splx(s);
149 }
150 
151 /*
152  * Parallel to llc_rtrequest.
153  */
154 static void
155 arp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
156 {
157 	struct sockaddr *gate = rt->rt_gateway;
158 	struct llinfo_arp *la = rt->rt_llinfo;
159 
160 	struct sockaddr_dl null_sdl = { sizeof(null_sdl), AF_LINK };
161 	static boolean_t arpinit_done;
162 	static int arp_inuse, arp_allocated;	/* for debugging only */
163 
164 	if (!arpinit_done) {
165 		arpinit_done = TRUE;
166 		callout_init(&arptimer_ch);
167 		callout_reset(&arptimer_ch, hz, arptimer, NULL);
168 	}
169 	if (rt->rt_flags & RTF_GATEWAY)
170 		return;
171 
172 	switch (req) {
173 	case RTM_ADD:
174 		/*
175 		 * XXX: If this is a manually added route to interface
176 		 * such as older version of routed or gated might provide,
177 		 * restore cloning bit.
178 		 */
179 		if (!(rt->rt_flags & RTF_HOST) &&
180 		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
181 			rt->rt_flags |= RTF_CLONING;
182 		if (rt->rt_flags & RTF_CLONING) {
183 			/*
184 			 * Case 1: This route should come from a route to iface.
185 			 */
186 			rt_setgate(rt, rt_key(rt),
187 			    (struct sockaddr *)&null_sdl);
188 			gate = rt->rt_gateway;
189 			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
190 			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
191 			rt->rt_expire = time_second;
192 			break;
193 		}
194 		/* Announce a new entry if requested. */
195 		if (rt->rt_flags & RTF_ANNOUNCE)
196 			arprequest(rt->rt_ifp,
197 			    &SIN(rt_key(rt))->sin_addr,
198 			    &SIN(rt_key(rt))->sin_addr,
199 			    LLADDR(SDL(gate)));
200 		/*FALLTHROUGH*/
201 	case RTM_RESOLVE:
202 		if (gate->sa_family != AF_LINK ||
203 		    gate->sa_len < sizeof(struct sockaddr_dl)) {
204 			log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
205 			break;
206 		}
207 		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
208 		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
209 		if (la != NULL)
210 			break; /* This happens on a route change */
211 		/*
212 		 * Case 2:  This route may come from cloning, or a manual route
213 		 * add with a LL address.
214 		 */
215 		R_Malloc(la, struct llinfo_arp *, sizeof(*la));
216 		rt->rt_llinfo = la;
217 		if (la == NULL) {
218 			log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
219 			break;
220 		}
221 		arp_inuse++, arp_allocated++;
222 		bzero(la, sizeof *la);
223 		la->la_rt = rt;
224 		rt->rt_flags |= RTF_LLINFO;
225 		LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
226 
227 #ifdef INET
228 		/*
229 		 * This keeps the multicast addresses from showing up
230 		 * in `arp -a' listings as unresolved.  It's not actually
231 		 * functional.  Then the same for broadcast.
232 		 */
233 		if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) &&
234 		    rt->rt_ifp->if_type != IFT_ARCNET) {
235 			ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
236 					       LLADDR(SDL(gate)));
237 			SDL(gate)->sdl_alen = 6;
238 			rt->rt_expire = 0;
239 		}
240 		if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
241 			memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
242 			       rt->rt_ifp->if_addrlen);
243 			SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
244 			rt->rt_expire = 0;
245 		}
246 #endif
247 
248 		if (SIN(rt_key(rt))->sin_addr.s_addr ==
249 		    (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
250 			/*
251 			 * This test used to be
252 			 *	if (loif.if_flags & IFF_UP)
253 			 * It allowed local traffic to be forced
254 			 * through the hardware by configuring the
255 			 * loopback down.  However, it causes problems
256 			 * during network configuration for boards
257 			 * that can't receive packets they send.  It
258 			 * is now necessary to clear "useloopback" and
259 			 * remove the route to force traffic out to
260 			 * the hardware.
261 			 */
262 			rt->rt_expire = 0;
263 			bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
264 			      SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
265 			if (useloopback)
266 				rt->rt_ifp = loif;
267 		}
268 		break;
269 
270 	case RTM_DELETE:
271 		if (la == NULL)
272 			break;
273 		arp_inuse--;
274 		LIST_REMOVE(la, la_le);
275 		rt->rt_llinfo = NULL;
276 		rt->rt_flags &= ~RTF_LLINFO;
277 		if (la->la_hold != NULL)
278 			m_freem(la->la_hold);
279 		Free(la);
280 	}
281 }
282 
283 /*
284  * Broadcast an ARP request. Caller specifies:
285  *	- arp header source ip address
286  *	- arp header target ip address
287  *	- arp header source ethernet address
288  */
289 static void
290 arprequest(struct ifnet *ifp, struct in_addr *sip, struct in_addr *tip,
291 	   u_char *enaddr)
292 {
293 	struct mbuf *m;
294 	struct ether_header *eh;
295 	struct arc_header *arh;
296 	struct arphdr *ah;
297 	struct sockaddr sa;
298 	static u_char llcx[] = { 0x82, 0x40, LLC_SNAP_LSAP, LLC_SNAP_LSAP,
299 				 LLC_UI, 0x00, 0x00, 0x00, 0x08, 0x06 };
300 	u_short ar_hrd;
301 
302 	if ((m = m_gethdr(MB_DONTWAIT, MT_DATA)) == NULL)
303 		return;
304 	m->m_pkthdr.rcvif = (struct ifnet *)NULL;
305 
306 	switch (ifp->if_type) {
307 	case IFT_ARCNET:
308 		ar_hrd = htons(ARPHRD_ARCNET);
309 
310 		m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
311 		m->m_pkthdr.len = m->m_len;
312 		MH_ALIGN(m, m->m_len);
313 
314 		arh = (struct arc_header *)sa.sa_data;
315 		arh->arc_dhost = ifp->if_broadcastaddr[0];
316 		arh->arc_type = ARCTYPE_ARP;
317 
318 		ah = mtod(m, struct arphdr *);
319 		break;
320 
321 	case IFT_ISO88025:
322 		ar_hrd = htons(ARPHRD_IEEE802);
323 
324 		m->m_len = sizeof(llcx) +
325 		    arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
326 		m->m_pkthdr.len = m->m_len;
327 		MH_ALIGN(m, m->m_len);
328 
329 		memcpy(mtod(m, caddr_t), llcx, sizeof(llcx));
330 		memcpy(sa.sa_data, ifp->if_broadcastaddr, ifp->if_addrlen);
331 		memcpy(sa.sa_data + 6, enaddr, 6);
332 		sa.sa_data[6] |= TR_RII;
333 		sa.sa_data[12] = TR_AC;
334 		sa.sa_data[13] = TR_LLC_FRAME;
335 
336 		ah = (struct arphdr *)(mtod(m, char *) + sizeof(llcx));
337 		break;
338 	case IFT_FDDI:
339 	case IFT_ETHER:
340 		/*
341 		 * This may not be correct for types not explicitly
342 		 * listed, but this is our best guess
343 		 */
344 	default:
345 		ar_hrd = htons(ARPHRD_ETHER);
346 
347 		m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
348 		m->m_pkthdr.len = m->m_len;
349 		MH_ALIGN(m, m->m_len);
350 
351 		eh = (struct ether_header *)sa.sa_data;
352 		/* if_output() will not swap */
353 		eh->ether_type = htons(ETHERTYPE_ARP);
354 		memcpy(eh->ether_dhost, ifp->if_broadcastaddr, ifp->if_addrlen);
355 
356 		ah = mtod(m, struct arphdr *);
357 		break;
358 	}
359 
360 	ah->ar_hrd = ar_hrd;
361 	ah->ar_pro = htons(ETHERTYPE_IP);
362 	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
363 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
364 	ah->ar_op = htons(ARPOP_REQUEST);
365 	memcpy(ar_sha(ah), enaddr, ah->ar_hln);
366 	memset(ar_tha(ah), 0, ah->ar_hln);
367 	memcpy(ar_spa(ah), sip, ah->ar_pln);
368 	memcpy(ar_tpa(ah), tip, ah->ar_pln);
369 
370 	sa.sa_family = AF_UNSPEC;
371 	sa.sa_len = sizeof(sa);
372 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)NULL);
373 }
374 
375 /*
376  * Resolve an IP address into an ethernet address.  If success,
377  * desten is filled in.  If there is no entry in arptab,
378  * set one up and broadcast a request for the IP address.
379  * Hold onto this mbuf and resend it once the address
380  * is finally resolved.  A return value of 1 indicates
381  * that desten has been filled in and the packet should be sent
382  * normally; a 0 return indicates that the packet has been
383  * taken over here, either now or for later transmission.
384  */
385 int
386 arpresolve(
387 	struct ifnet *ifp,
388 	struct rtentry *rt0,
389 	struct mbuf *m,
390 	struct sockaddr *dst,
391 	u_char *desten)
392 {
393 	struct rtentry *rt;
394 	struct llinfo_arp *la = NULL;
395 	struct sockaddr_dl *sdl;
396 
397 	if (m->m_flags & M_BCAST) {	/* broadcast */
398 		memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
399 		return (1);
400 	}
401 	if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */
402 		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
403 		return (1);
404 	}
405 	if (rt0 != NULL) {
406 		if (rt_llroute(dst, rt0, &rt) != 0) {
407 			m_freem(m);
408 			return 0;
409 		}
410 		la = rt->rt_llinfo;
411 	}
412 	if (la == NULL) {
413 		la = arplookup(SIN(dst)->sin_addr.s_addr, TRUE, FALSE);
414 		if (la != NULL)
415 			rt = la->la_rt;
416 	}
417 	if (la == NULL || rt == NULL) {
418 		log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
419 			inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "",
420 				rt ? "rt" : "");
421 		m_freem(m);
422 		return (0);
423 	}
424 	sdl = SDL(rt->rt_gateway);
425 	/*
426 	 * Check the address family and length is valid, the address
427 	 * is resolved; otherwise, try to resolve.
428 	 */
429 	if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
430 	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
431 		/*
432 		 * If entry has an expiry time and it is approaching,
433 		 * see if we need to send an ARP request within this
434 		 * arpt_down interval.
435 		 */
436 		if ((rt->rt_expire != 0) &&
437 		    (time_second + la->la_preempt > rt->rt_expire)) {
438 			arprequest(ifp,
439 				   &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
440 				   &SIN(dst)->sin_addr,
441 				   IF_LLADDR(ifp));
442 			la->la_preempt--;
443 		}
444 
445 		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
446 		return 1;
447 	}
448 	/*
449 	 * If ARP is disabled on this interface, stop.
450 	 * XXX
451 	 * Probably should not allocate empty llinfo struct if we are
452 	 * not going to be sending out an arp request.
453 	 */
454 	if (ifp->if_flags & IFF_NOARP) {
455 		m_freem(m);
456 		return (0);
457 	}
458 	/*
459 	 * There is an arptab entry, but no ethernet address
460 	 * response yet.  Replace the held mbuf with this
461 	 * latest one.
462 	 */
463 	if (la->la_hold != NULL)
464 		m_freem(la->la_hold);
465 	la->la_hold = m;
466 	if (rt->rt_expire || ((rt->rt_flags & RTF_STATIC) && !sdl->sdl_alen)) {
467 		rt->rt_flags &= ~RTF_REJECT;
468 		if (la->la_asked == 0 || rt->rt_expire != time_second) {
469 			rt->rt_expire = time_second;
470 			if (la->la_asked++ < arp_maxtries) {
471 				arprequest(ifp,
472 					   &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
473 					   &SIN(dst)->sin_addr,
474 					   IF_LLADDR(ifp));
475 			} else {
476 				rt->rt_flags |= RTF_REJECT;
477 				rt->rt_expire += arpt_down;
478 				la->la_asked = 0;
479 				la->la_preempt = arp_maxtries;
480 			}
481 
482 		}
483 	}
484 	return (0);
485 }
486 
487 /*
488  * Common length and type checks are done here,
489  * then the protocol-specific routine is called.
490  */
491 static int
492 arpintr(struct netmsg *msg)
493 {
494 	struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
495 	struct arphdr *ar;
496 	u_short ar_hrd;
497 
498 	if (m->m_len < sizeof(struct arphdr) &&
499 	    ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
500 		log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
501 		goto out2;
502 	}
503 	ar = mtod(m, struct arphdr *);
504 
505 	ar_hrd = ntohs(ar->ar_hrd);
506 	if (ar_hrd != ARPHRD_ETHER &&
507 	    ar_hrd != ARPHRD_IEEE802 &&
508 	    ar_hrd != ARPHRD_ARCNET) {
509 		log(LOG_ERR,
510 		    "arp: unknown hardware address format (0x%2D)\n",
511 		    (unsigned char *)&ar->ar_hrd, "");
512 		goto out1;
513 	}
514 
515 	if (m->m_pkthdr.len < arphdr_len(ar) &&
516 	    (m = m_pullup(m, arphdr_len(ar))) == NULL) {
517 		log(LOG_ERR, "arp: runt packet\n");
518 		goto out1;
519 	}
520 
521 	switch (ntohs(ar->ar_pro)) {
522 #ifdef INET
523 		case ETHERTYPE_IP:
524 			in_arpinput(m);
525 			goto out2;
526 #endif
527 	}
528 out1:
529 	m_freem(m);
530 out2:
531 	lwkt_replymsg(&msg->nm_lmsg, 0);
532 	return(EASYNC);
533 }
534 
535 #ifdef INET
536 /*
537  * ARP for Internet protocols on 10 Mb/s Ethernet.
538  * Algorithm is that given in RFC 826.
539  * In addition, a sanity check is performed on the sender
540  * protocol address, to catch impersonators.
541  * We no longer handle negotiations for use of trailer protocol:
542  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
543  * along with IP replies if we wanted trailers sent to us,
544  * and also sent them in response to IP replies.
545  * This allowed either end to announce the desire to receive
546  * trailer packets.
547  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
548  * but formerly didn't normally send requests.
549  */
550 static int log_arp_wrong_iface = 1;
551 
552 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
553 	&log_arp_wrong_iface, 0,
554 	"log arp packets arriving on the wrong interface");
555 
556 static void
557 in_arpinput(struct mbuf *m)
558 {
559 	struct arphdr *ah;
560 	struct ifnet *ifp = m->m_pkthdr.rcvif;
561 	struct ether_header *eh;
562 	struct arc_header *arh;
563 	struct iso88025_header *th = (struct iso88025_header *)0;
564 	struct iso88025_sockaddr_dl_data *trld;
565 	struct llinfo_arp *la = 0;
566 	struct rtentry *rt;
567 	struct ifaddr *ifa;
568 	struct in_ifaddr *ia;
569 	struct sockaddr_dl *sdl;
570 	struct sockaddr sa;
571 	struct in_addr isaddr, itaddr, myaddr;
572 	int op, rif_len;
573 	int req_len;
574 
575 	req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
576 	if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
577 		log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
578 		return;
579 	}
580 
581 	ah = mtod(m, struct arphdr *);
582 	op = ntohs(ah->ar_op);
583 	memcpy(&isaddr, ar_spa(ah), sizeof isaddr);
584 	memcpy(&itaddr, ar_tpa(ah), sizeof itaddr);
585 #ifdef BRIDGE
586 #define BRIDGE_TEST (do_bridge)
587 #else
588 #define BRIDGE_TEST (0) /* cc will optimise the test away */
589 #endif
590 	/*
591 	 * For a bridge, we want to check the address irrespective
592 	 * of the receive interface. (This will change slightly
593 	 * when we have clusters of interfaces).
594 	 */
595 	LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash)
596 		if ((BRIDGE_TEST || (ia->ia_ifp == ifp)) &&
597 		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
598 			goto match;
599 	LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
600 		if ((BRIDGE_TEST || (ia->ia_ifp == ifp)) &&
601 		    isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
602 			goto match;
603 	/*
604 	 * No match, use the first inet address on the receive interface
605 	 * as a dummy address for the rest of the function.
606 	 */
607 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
608 		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
609 			ia = ifatoia(ifa);
610 			goto match;
611 		}
612 	/*
613 	 * If bridging, fall back to using any inet address.
614 	 * This is probably incorrect, the right way being try to match
615 	 * addresses for interfaces in the same cluster, so if we
616 	 * get here we should always drop the packet.
617 	 */
618 	if (!BRIDGE_TEST ||
619 	    (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL) {
620 		m_freem(m);
621 		return;
622 	}
623 match:
624 	myaddr = ia->ia_addr.sin_addr;
625 	if (!bcmp(ar_sha(ah), IF_LLADDR(ifp), ifp->if_addrlen)) {
626 		m_freem(m);	/* it's from me, ignore it. */
627 		return;
628 	}
629 	if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
630 		log(LOG_ERR,
631 		    "arp: link address is broadcast for IP address %s!\n",
632 		    inet_ntoa(isaddr));
633 		m_freem(m);
634 		return;
635 	}
636 	if (isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
637 		log(LOG_ERR,
638 		   "arp: %*D is using my IP address %s!\n",
639 		   ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
640 		   inet_ntoa(isaddr));
641 		itaddr = myaddr;
642 		goto reply;
643 	}
644 	la = arplookup(isaddr.s_addr, (itaddr.s_addr == myaddr.s_addr), FALSE);
645 	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
646 		/* the following is not an error when doing bridging */
647 		if (!BRIDGE_TEST && rt->rt_ifp != ifp) {
648 		    if (log_arp_wrong_iface)
649 			log(LOG_ERR,
650 			    "arp: %s is on %s but got reply from %*D on %s\n",
651 			    inet_ntoa(isaddr),
652 			    rt->rt_ifp->if_xname,
653 			    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
654 			    ifp->if_xname);
655 		    goto reply;
656 		}
657 		if (sdl->sdl_alen &&
658 		    bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
659 			if (rt->rt_expire)
660 			    log(LOG_INFO,
661 				"arp: %s moved from %*D to %*D on %s\n",
662 				inet_ntoa(isaddr),
663 				ifp->if_addrlen, (u_char *)LLADDR(sdl), ":",
664 				ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
665 				ifp->if_xname);
666 			else {
667 			    log(LOG_ERR,
668 				"arp: %*D attempts to modify permanent entry for %s on %s\n",
669 				ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
670 				inet_ntoa(isaddr), ifp->if_xname);
671 			    goto reply;
672 			}
673 		}
674 		/*
675 		 * sanity check for the address length.
676 		 * XXX this does not work for protocols with variable address
677 		 * length. -is
678 		 */
679 		if (sdl->sdl_alen &&
680 		    sdl->sdl_alen != ah->ar_hln) {
681 			log(LOG_WARNING,
682 			    "arp from %*D: new addr len %d, was %d",
683 			    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
684 			    ah->ar_hln, sdl->sdl_alen);
685 		}
686 		if (ifp->if_addrlen != ah->ar_hln) {
687 			log(LOG_WARNING,
688 			    "arp from %*D: addr len: new %d, i/f %d (ignored)",
689 			    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
690 			    ah->ar_hln, ifp->if_addrlen);
691 			goto reply;
692 		}
693 		memcpy(LLADDR(sdl), ar_sha(ah), sdl->sdl_alen = ah->ar_hln);
694 		/*
695 		 * If we receive an arp from a token-ring station over
696 		 * a token-ring nic then try to save the source
697 		 * routing info.
698 		 */
699 		if (ifp->if_type == IFT_ISO88025) {
700 			th = (struct iso88025_header *)m->m_pkthdr.header;
701 			trld = SDL_ISO88025(sdl);
702 			rif_len = TR_RCF_RIFLEN(th->rcf);
703 			if ((th->iso88025_shost[0] & TR_RII) &&
704 			    (rif_len > 2)) {
705 				trld->trld_rcf = th->rcf;
706 				trld->trld_rcf ^= htons(TR_RCF_DIR);
707 				memcpy(trld->trld_route, th->rd, rif_len - 2);
708 				trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
709 				/*
710 				 * Set up source routing information for
711 				 * reply packet (XXX)
712 				 */
713 				m->m_data -= rif_len;
714 				m->m_len  += rif_len;
715 				m->m_pkthdr.len += rif_len;
716 			} else {
717 				th->iso88025_shost[0] &= ~TR_RII;
718 				trld->trld_rcf = 0;
719 			}
720 			m->m_data -= 8;
721 			m->m_len  += 8;
722 			m->m_pkthdr.len += 8;
723 			th->rcf = trld->trld_rcf;
724 		}
725 		if (rt->rt_expire)
726 			rt->rt_expire = time_second + arpt_keep;
727 		rt->rt_flags &= ~RTF_REJECT;
728 		la->la_asked = 0;
729 		la->la_preempt = arp_maxtries;
730 		if (la->la_hold != NULL) {
731 			(*ifp->if_output)(ifp, la->la_hold, rt_key(rt), rt);
732 			la->la_hold = NULL;
733 		}
734 	}
735 reply:
736 	if (op != ARPOP_REQUEST) {
737 		m_freem(m);
738 		return;
739 	}
740 	if (itaddr.s_addr == myaddr.s_addr) {
741 		/* I am the target */
742 		memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
743 		memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln);
744 	} else {
745 		la = arplookup(itaddr.s_addr, FALSE, SIN_PROXY);
746 		if (la == NULL) {
747 			struct sockaddr_in sin;
748 
749 			if (!arp_proxyall) {
750 				m_freem(m);
751 				return;
752 			}
753 
754 			bzero(&sin, sizeof sin);
755 			sin.sin_family = AF_INET;
756 			sin.sin_len = sizeof sin;
757 			sin.sin_addr = itaddr;
758 
759 			rt = rtlookup((struct sockaddr *)&sin, 0, 0UL);
760 			if (!rt) {
761 				m_freem(m);
762 				return;
763 			}
764 			/*
765 			 * Don't send proxies for nodes on the same interface
766 			 * as this one came out of, or we'll get into a fight
767 			 * over who claims what Ether address.
768 			 */
769 			if (rt->rt_ifp == ifp) {
770 				--rt->rt_refcnt;
771 				m_freem(m);
772 				return;
773 			}
774 			memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
775 			memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln);
776 			--rt->rt_refcnt;
777 #ifdef DEBUG_PROXY
778 			printf("arp: proxying for %s\n",
779 			       inet_ntoa(itaddr));
780 #endif
781 		} else {
782 			rt = la->la_rt;
783 			memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
784 			sdl = SDL(rt->rt_gateway);
785 			memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
786 		}
787 	}
788 
789 	memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
790 	memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
791 	ah->ar_op = htons(ARPOP_REPLY);
792 	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
793 	switch (ifp->if_type) {
794 	case IFT_ARCNET:
795 		arh = (struct arc_header *)sa.sa_data;
796 		arh->arc_dhost = *ar_tha(ah);
797 		arh->arc_type = ARCTYPE_ARP;
798 		break;
799 
800 	case IFT_ISO88025:
801 		/* Re-arrange the source/dest address */
802 		memcpy(th->iso88025_dhost, th->iso88025_shost,
803 		    sizeof(th->iso88025_dhost));
804 		memcpy(th->iso88025_shost, IF_LLADDR(ifp),
805 		    sizeof(th->iso88025_shost));
806 		/* Set the source routing bit if neccesary */
807 		if (th->iso88025_dhost[0] & TR_RII) {
808 			th->iso88025_dhost[0] &= ~TR_RII;
809 			if (TR_RCF_RIFLEN(th->rcf) > 2)
810 				th->iso88025_shost[0] |= TR_RII;
811 		}
812 		/* Copy the addresses, ac and fc into sa_data */
813 		memcpy(sa.sa_data, th->iso88025_dhost,
814 		    sizeof(th->iso88025_dhost) * 2);
815 		sa.sa_data[(sizeof(th->iso88025_dhost) * 2)] = TR_AC;
816 		sa.sa_data[(sizeof(th->iso88025_dhost) * 2) + 1] = TR_LLC_FRAME;
817 		break;
818 	case IFT_ETHER:
819 	case IFT_FDDI:
820 	/*
821 	 * May not be correct for types not explictly
822 	 * listed, but it is our best guess.
823 	 */
824 	default:
825 		eh = (struct ether_header *)sa.sa_data;
826 		memcpy(eh->ether_dhost, ar_tha(ah), sizeof(eh->ether_dhost));
827 		eh->ether_type = htons(ETHERTYPE_ARP);
828 		break;
829 	}
830 	sa.sa_family = AF_UNSPEC;
831 	sa.sa_len = sizeof(sa);
832 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
833 	return;
834 }
835 #endif
836 
837 /*
838  * Free an arp entry.  If the arp entry is actively referenced or represents
839  * a static entry we only clear it back to an unresolved state, otherwise
840  * we destroy the entry entirely.
841  *
842  * Note that static entries are created when route add ... -interface is used
843  * to create an interface route to a (direct) destination.
844  */
845 static void
846 arptfree(la)
847 	struct llinfo_arp *la;
848 {
849 	struct rtentry *rt = la->la_rt;
850 	struct sockaddr_dl *sdl;
851 
852 	if (rt == NULL)
853 		panic("arptfree");
854 	sdl = SDL(rt->rt_gateway);
855 	if (sdl && ((rt->rt_refcnt > 0 && sdl->sdl_family == AF_LINK) ||
856 	    (rt->rt_flags & RTF_STATIC))) {
857 		sdl->sdl_alen = 0;
858 		la->la_preempt = la->la_asked = 0;
859 		rt->rt_flags &= ~RTF_REJECT;
860 		return;
861 	}
862 	rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
863 }
864 /*
865  * Lookup or enter a new address in arptab.
866  */
867 static struct llinfo_arp *
868 arplookup(in_addr_t addr, boolean_t create, boolean_t proxy)
869 {
870 	struct rtentry *rt;
871 	static struct sockaddr_inarp sin = { sizeof(sin), AF_INET };
872 	const char *why = NULL;
873 
874 	sin.sin_addr.s_addr = addr;
875 	sin.sin_other = proxy ? SIN_PROXY : 0;
876 	rt = rtlookup((struct sockaddr *)&sin, create, 0UL);
877 	if (rt == NULL)
878 		return (NULL);
879 	rt->rt_refcnt--;
880 
881 	if (rt->rt_flags & RTF_GATEWAY)
882 		why = "host is not on local network";
883 	else if (!(rt->rt_flags & RTF_LLINFO))
884 		why = "could not allocate llinfo";
885 	else if (rt->rt_gateway->sa_family != AF_LINK)
886 		why = "gateway route is not ours";
887 
888 	if (why) {
889 		if (create) {
890 			log(LOG_DEBUG, "arplookup %s failed: %s\n",
891 			    inet_ntoa(sin.sin_addr), why);
892 		}
893 		if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_WASCLONED)) {
894 			/* No references to this route.  Purge it. */
895 			rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
896 			    rt_mask(rt), rt->rt_flags, NULL);
897 		}
898 		return (NULL);
899 	}
900 	return (rt->rt_llinfo);
901 }
902 
903 void
904 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
905 {
906 	if (IA_SIN(ifa)->sin_addr.s_addr != INADDR_ANY)
907 		arprequest(ifp, &IA_SIN(ifa)->sin_addr, &IA_SIN(ifa)->sin_addr,
908 			   IF_LLADDR(ifp));
909 	ifa->ifa_rtrequest = arp_rtrequest;
910 	ifa->ifa_flags |= RTF_CLONING;
911 }
912 
913 static void
914 arp_init(void)
915 {
916 	LIST_INIT(&llinfo_arp);
917 	netisr_register(NETISR_ARP, cpu0_portfn, arpintr);
918 }
919 
920 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);
921