xref: /netbsd-src/sys/netinet/if_arp.c (revision ace896fac114f559f7469472324fbe68bbe378e5)
1 /*	$NetBSD: if_arp.c,v 1.43 1998/01/05 10:31:44 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1988, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)if_ether.c	8.2 (Berkeley) 9/26/94
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 #ifdef INET
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/malloc.h>
49 #include <sys/mbuf.h>
50 #include <sys/socket.h>
51 #include <sys/time.h>
52 #include <sys/kernel.h>
53 #include <sys/errno.h>
54 #include <sys/ioctl.h>
55 #include <sys/syslog.h>
56 #include <sys/proc.h>
57 
58 #include <net/ethertypes.h>
59 #include <net/if.h>
60 #include <net/if_dl.h>
61 #include <net/route.h>
62 
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/in_var.h>
66 #include <netinet/ip.h>
67 #include <netinet/if_inarp.h>
68 
69 #include "loop.h"
70 
71 #define SIN(s) ((struct sockaddr_in *)s)
72 #define SDL(s) ((struct sockaddr_dl *)s)
73 #define SRP(s) ((struct sockaddr_inarp *)s)
74 
75 /*
76  * ARP trailer negotiation.  Trailer protocol is not IP specific,
77  * but ARP request/response use IP addresses.
78  */
79 #define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL
80 
81 /* timer values */
82 int	arpt_prune = (5*60*1);	/* walk list every 5 minutes */
83 int	arpt_keep = (20*60);	/* once resolved, good for 20 more minutes */
84 int	arpt_down = 20;		/* once declared down, don't send for 20 secs */
85 #define	rt_expire rt_rmx.rmx_expire
86 
87 static	void arprequest __P((struct ifnet *,
88 	    struct in_addr *, struct in_addr *, u_int8_t *));
89 static	void arptfree __P((struct llinfo_arp *));
90 static	void arptimer __P((void *));
91 static	struct llinfo_arp *arplookup __P((struct in_addr *, int, int));
92 static	void in_arpinput __P((struct mbuf *));
93 
94 extern	struct ifnet loif[NLOOP];
95 LIST_HEAD(, llinfo_arp) llinfo_arp;
96 struct	ifqueue arpintrq = {0, 0, 0, 50};
97 int	arp_inuse, arp_allocated, arp_intimer;
98 int	arp_maxtries = 5;
99 int	useloopback = 1;	/* use loopback interface for local traffic */
100 int	arpinit_done = 0;
101 
102 /* revarp state */
103 static struct	in_addr myip, srv_ip;
104 static int	myip_initialized = 0;
105 static int	revarp_in_progress = 0;
106 static struct	ifnet *myip_ifp = NULL;
107 
108 #ifdef DDB
109 static void db_print_sa __P((struct sockaddr *));
110 static void db_print_ifa __P((struct ifaddr *));
111 static void db_print_llinfo __P((caddr_t));
112 static int db_show_radix_node __P((struct radix_node *, void *));
113 #endif
114 
115 /*
116  * this should be elsewhere.
117  */
118 
119 static char *
120 lla_snprintf __P((u_int8_t *, int));
121 
122 static char *
123 lla_snprintf(adrp, len)
124 	u_int8_t *adrp;
125 	int len;
126 {
127 	static char buf[16*3];
128 	static const char hexdigits[] = {
129 	    '0','1','2','3','4','5','6','7',
130 	    '8','9','a','b','c','d','e','f'
131 	};
132 
133 	int i;
134 	char *p;
135 
136 	p = buf;
137 
138 	*p++ = hexdigits[(*adrp)>>4];
139 	*p++ = hexdigits[(*adrp++)&0xf];
140 
141 	for (i=1; i<len && i<16; i++) {
142 		*p++ = ':';
143 		*p++ = hexdigits[(*adrp)>>4];
144 		*p++ = hexdigits[(*adrp++)&0xf];
145 	}
146 
147 	*p = 0;
148 	return buf;
149 }
150 
151 /*
152  * Timeout routine.  Age arp_tab entries periodically.
153  */
154 /* ARGSUSED */
155 static void
156 arptimer(arg)
157 	void *arg;
158 {
159 	int s;
160 	register struct llinfo_arp *la, *nla;
161 
162 	s = splsoftnet();
163 	timeout(arptimer, NULL, arpt_prune * hz);
164 	for (la = llinfo_arp.lh_first; la != 0; la = nla) {
165 		register struct rtentry *rt = la->la_rt;
166 
167 		nla = la->la_list.le_next;
168 		if (rt->rt_expire && rt->rt_expire <= time.tv_sec)
169 			arptfree(la); /* timer has expired; clear */
170 	}
171 	splx(s);
172 }
173 
174 /*
175  * Parallel to llc_rtrequest.
176  */
177 void
178 arp_rtrequest(req, rt, sa)
179 	int req;
180 	register struct rtentry *rt;
181 	struct sockaddr *sa;
182 {
183 	register struct sockaddr *gate = rt->rt_gateway;
184 	register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
185 	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
186 
187 	if (!arpinit_done) {
188 		arpinit_done = 1;
189 		/*
190 		 * We generate expiration times from time.tv_sec
191 		 * so avoid accidently creating permanent routes.
192 		 */
193 		if (time.tv_sec == 0) {
194 			time.tv_sec++;
195 		}
196 		timeout(arptimer, (caddr_t)0, hz);
197 	}
198 	if (rt->rt_flags & RTF_GATEWAY)
199 		return;
200 	switch (req) {
201 
202 	case RTM_ADD:
203 		/*
204 		 * XXX: If this is a manually added route to interface
205 		 * such as older version of routed or gated might provide,
206 		 * restore cloning bit.
207 		 */
208 		if ((rt->rt_flags & RTF_HOST) == 0 &&
209 		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
210 			rt->rt_flags |= RTF_CLONING;
211 		if (rt->rt_flags & RTF_CLONING) {
212 			/*
213 			 * Case 1: This route should come from a route to iface.
214 			 */
215 			rt_setgate(rt, rt_key(rt),
216 					(struct sockaddr *)&null_sdl);
217 			gate = rt->rt_gateway;
218 			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
219 			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
220 			/*
221 			 * Give this route an expiration time, even though
222 			 * it's a "permanent" route, so that routes cloned
223 			 * from it do not need their expiration time set.
224 			 */
225 			rt->rt_expire = time.tv_sec;
226 			break;
227 		}
228 		/* Announce a new entry if requested. */
229 		if (rt->rt_flags & RTF_ANNOUNCE)
230 			arprequest(rt->rt_ifp,
231 			    &SIN(rt_key(rt))->sin_addr,
232 			    &SIN(rt_key(rt))->sin_addr,
233 			    (u_char *)LLADDR(SDL(gate)));
234 		/*FALLTHROUGH*/
235 	case RTM_RESOLVE:
236 		if (gate->sa_family != AF_LINK ||
237 		    gate->sa_len < sizeof(null_sdl)) {
238 			log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
239 			break;
240 		}
241 		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
242 		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
243 		if (la != 0)
244 			break; /* This happens on a route change */
245 		/*
246 		 * Case 2:  This route may come from cloning, or a manual route
247 		 * add with a LL address.
248 		 */
249 		R_Malloc(la, struct llinfo_arp *, sizeof(*la));
250 		rt->rt_llinfo = (caddr_t)la;
251 		if (la == 0) {
252 			log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
253 			break;
254 		}
255 		arp_inuse++, arp_allocated++;
256 		Bzero(la, sizeof(*la));
257 		la->la_rt = rt;
258 		rt->rt_flags |= RTF_LLINFO;
259 		LIST_INSERT_HEAD(&llinfo_arp, la, la_list);
260 		if (in_hosteq(SIN(rt_key(rt))->sin_addr,
261 		    (IA_SIN(rt->rt_ifa))->sin_addr)) {
262 			/*
263 			 * This test used to be
264 			 *	if (loif.if_flags & IFF_UP)
265 			 * It allowed local traffic to be forced through
266 			 * the hardware by configuring the loopback down.
267 			 * However, it causes problems during network
268 			 * configuration for boards that can't receive
269 			 * packets they send.  It is now necessary to clear
270 			 * "useloopback" and remove the route to force
271 			 * traffic out to the hardware.
272 			 */
273 			rt->rt_expire = 0;
274 			Bcopy(LLADDR(rt->rt_ifp->if_sadl),
275 			    LLADDR(SDL(gate)),
276 			    SDL(gate)->sdl_alen =
277 			    rt->rt_ifp->if_data.ifi_addrlen);
278 			if (useloopback)
279 				rt->rt_ifp = &loif[0];
280 		}
281 		break;
282 
283 	case RTM_DELETE:
284 		if (la == 0)
285 			break;
286 		arp_inuse--;
287 		LIST_REMOVE(la, la_list);
288 		rt->rt_llinfo = 0;
289 		rt->rt_flags &= ~RTF_LLINFO;
290 		if (la->la_hold)
291 			m_freem(la->la_hold);
292 		Free((caddr_t)la);
293 	}
294 }
295 
296 /*
297  * Broadcast an ARP request. Caller specifies:
298  *	- arp header source ip address
299  *	- arp header target ip address
300  *	- arp header source ethernet address
301  */
302 static void
303 arprequest(ifp, sip, tip, enaddr)
304 	register struct ifnet *ifp;
305 	register struct in_addr *sip, *tip;
306 	register u_int8_t *enaddr;
307 {
308 	register struct mbuf *m;
309 	struct arphdr *ah;
310 	struct sockaddr sa;
311 
312 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
313 		return;
314 	m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
315 	    2*ifp->if_data.ifi_addrlen;
316 	m->m_pkthdr.len = m->m_len;
317 	MH_ALIGN(m, m->m_len);
318 	ah = mtod(m, struct arphdr *);
319 	bzero((caddr_t)ah, m->m_len);
320 	ah->ar_pro = htons(ETHERTYPE_IP);
321 	ah->ar_hln = ifp->if_data.ifi_addrlen;	/* hardware address length */
322 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
323 	ah->ar_op = htons(ARPOP_REQUEST);
324 	bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
325 	bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
326 	bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
327 	sa.sa_family = AF_ARP;
328 	sa.sa_len = 2;
329 	m->m_flags |= M_BCAST;
330 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
331 }
332 
333 /*
334  * Resolve an IP address into an ethernet address.  If success,
335  * desten is filled in.  If there is no entry in arptab,
336  * set one up and broadcast a request for the IP address.
337  * Hold onto this mbuf and resend it once the address
338  * is finally resolved.  A return value of 1 indicates
339  * that desten has been filled in and the packet should be sent
340  * normally; a 0 return indicates that the packet has been
341  * taken over here, either now or for later transmission.
342  */
343 int
344 arpresolve(ifp, rt, m, dst, desten)
345 	register struct ifnet *ifp;
346 	register struct rtentry *rt;
347 	struct mbuf *m;
348 	register struct sockaddr *dst;
349 	register u_char *desten;
350 {
351 	register struct llinfo_arp *la;
352 	struct sockaddr_dl *sdl;
353 
354 	if (rt)
355 		la = (struct llinfo_arp *)rt->rt_llinfo;
356 	else {
357 		if ((la = arplookup(&SIN(dst)->sin_addr, 1, 0)) != NULL)
358 			rt = la->la_rt;
359 	}
360 	if (la == 0 || rt == 0) {
361 		log(LOG_DEBUG, "arpresolve: can't allocate llinfo\n");
362 		m_freem(m);
363 		return (0);
364 	}
365 	sdl = SDL(rt->rt_gateway);
366 	/*
367 	 * Check the address family and length is valid, the address
368 	 * is resolved; otherwise, try to resolve.
369 	 */
370 	if ((rt->rt_expire == 0 || rt->rt_expire > time.tv_sec) &&
371 	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
372 		bcopy(LLADDR(sdl), desten,
373 		    min(sdl->sdl_alen, ifp->if_data.ifi_addrlen));
374 		return 1;
375 	}
376 	/*
377 	 * There is an arptab entry, but no ethernet address
378 	 * response yet.  Replace the held mbuf with this
379 	 * latest one.
380 	 */
381 	if (la->la_hold)
382 		m_freem(la->la_hold);
383 	la->la_hold = m;
384 	/*
385 	 * Re-send the ARP request when appropriate.
386 	 */
387 #ifdef	DIAGNOSTIC
388 	if (rt->rt_expire == 0) {
389 		/* This should never happen. (Should it? -gwr) */
390 		printf("arpresolve: unresolved and rt_expire == 0\n");
391 		/* Set expiration time to now (expired). */
392 		rt->rt_expire = time.tv_sec;
393 	}
394 #endif
395 	if (rt->rt_expire) {
396 		rt->rt_flags &= ~RTF_REJECT;
397 		if (la->la_asked == 0 || rt->rt_expire != time.tv_sec) {
398 			rt->rt_expire = time.tv_sec;
399 			if (la->la_asked++ < arp_maxtries)
400 				arprequest(ifp,
401 				    &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
402 				    &SIN(dst)->sin_addr,
403 				    LLADDR(ifp->if_sadl));
404 			else {
405 				rt->rt_flags |= RTF_REJECT;
406 				rt->rt_expire += arpt_down;
407 				la->la_asked = 0;
408 			}
409 		}
410 	}
411 	return (0);
412 }
413 
414 /*
415  * Common length and type checks are done here,
416  * then the protocol-specific routine is called.
417  */
418 void
419 arpintr()
420 {
421 	register struct mbuf *m;
422 	register struct arphdr *ar;
423 	int s;
424 
425 	while (arpintrq.ifq_head) {
426 		s = splimp();
427 		IF_DEQUEUE(&arpintrq, m);
428 		splx(s);
429 		if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
430 			panic("arpintr");
431 
432 		if (m->m_len >= sizeof(struct arphdr) &&
433 		    (ar = mtod(m, struct arphdr *)) &&
434 		    /* XXX ntohs(ar->ar_hrd) == ARPHRD_ETHER && */
435 		    m->m_len >=
436 		      sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
437 			switch (ntohs(ar->ar_pro)) {
438 
439 			case ETHERTYPE_IP:
440 			case ETHERTYPE_IPTRAILERS:
441 				in_arpinput(m);
442 				continue;
443 			}
444 		m_freem(m);
445 	}
446 }
447 
448 /*
449  * ARP for Internet protocols on 10 Mb/s Ethernet.
450  * Algorithm is that given in RFC 826.
451  * In addition, a sanity check is performed on the sender
452  * protocol address, to catch impersonators.
453  * We no longer handle negotiations for use of trailer protocol:
454  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
455  * along with IP replies if we wanted trailers sent to us,
456  * and also sent them in response to IP replies.
457  * This allowed either end to announce the desire to receive
458  * trailer packets.
459  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
460  * but formerly didn't normally send requests.
461  */
462 static void
463 in_arpinput(m)
464 	struct mbuf *m;
465 {
466 	struct arphdr *ah;
467 	register struct ifnet *ifp = m->m_pkthdr.rcvif;
468 	register struct llinfo_arp *la = 0;
469 	register struct rtentry *rt;
470 	struct in_ifaddr *ia, *maybe_ia = 0;
471 	struct sockaddr_dl *sdl;
472 	struct sockaddr sa;
473 	struct in_addr isaddr, itaddr, myaddr;
474 	int op;
475 
476 	ah = mtod(m, struct arphdr *);
477 	op = ntohs(ah->ar_op);
478 	bcopy((caddr_t)ar_spa(ah), (caddr_t)&isaddr, sizeof (isaddr));
479 	bcopy((caddr_t)ar_tpa(ah), (caddr_t)&itaddr, sizeof (itaddr));
480 
481 	/*
482 	 * If the target IP address is zero, ignore the packet.
483 	 * This prevents the code below from tring to answer
484 	 * when we are using IP address zero (booting).
485 	 */
486 	if (in_nullhost(itaddr))
487 		goto out;
488 
489 	/*
490 	 * If the source IP address is zero, this is most likely a
491 	 * confused host trying to use IP address zero.  (Windoze?)
492 	 * XXX:  Should we bother trying to reply to these?
493 	 */
494 	if (in_nullhost(isaddr))
495 		goto out;
496 
497 	/* Search for a matching interface address. */
498 	for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next)
499 		if (ia->ia_ifp == ifp) {
500 			maybe_ia = ia;
501 			if (in_hosteq(itaddr, ia->ia_addr.sin_addr) ||
502 			    in_hosteq(isaddr, ia->ia_addr.sin_addr))
503 				break;
504 		}
505 	if (maybe_ia == 0)
506 		goto out;
507 
508 	myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr;
509 	if (!bcmp((caddr_t)ar_sha(ah), LLADDR(ifp->if_sadl),
510 	    ifp->if_data.ifi_addrlen))
511 		goto out;	/* it's from me, ignore it. */
512 
513 	if (!bcmp((caddr_t)ar_sha(ah), (caddr_t)ifp->if_broadcastaddr,
514 	    ifp->if_data.ifi_addrlen)) {
515 		log(LOG_ERR,
516 		    "%s: arp: link address is broadcast for IP address %x!\n",
517 		    ifp->if_xname, ntohl(isaddr.s_addr));
518 		goto out;
519 	}
520 
521 	if (in_hosteq(isaddr, myaddr)) {
522 		log(LOG_ERR,
523 		   "duplicate IP address %08x sent from link address %s\n",
524 		   ntohl(isaddr.s_addr), lla_snprintf(ar_sha(ah), ah->ar_hln));
525 		itaddr = myaddr;
526 		goto reply;
527 	}
528 	la = arplookup(&isaddr, in_hosteq(itaddr, myaddr), 0);
529 	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
530 		if (sdl->sdl_alen &&
531 		    bcmp((caddr_t)ar_sha(ah), LLADDR(sdl), sdl->sdl_alen))
532 			log(LOG_INFO, "arp info overwritten for %08x by %s\n",
533 			    ntohl(isaddr.s_addr),
534 			    lla_snprintf(ar_sha(ah), ah->ar_hln));
535 		/*
536 		 * sanity check for the address length.
537 		 * XXX this does not work for protocols with variable address
538 		 * length. -is
539 		 */
540 		if (sdl->sdl_alen &&
541 		    sdl->sdl_alen != ah->ar_hln) {
542 			log(LOG_WARNING,
543 			    "arp from %08x: new addr len %d, was %d",
544 			    ntohl(isaddr.s_addr), ah->ar_hln, sdl->sdl_alen);
545 		}
546 		if (ifp->if_data.ifi_addrlen != ah->ar_hln) {
547 			log(LOG_WARNING,
548 			    "arp from %08x: addr len: new %d, i/f %d (ignored)",
549 			    ntohl(isaddr.s_addr), ah->ar_hln,
550 			    ifp->if_data.ifi_addrlen);
551 			goto reply;
552 		}
553 		bcopy((caddr_t)ar_sha(ah), LLADDR(sdl),
554 		    sdl->sdl_alen = ah->ar_hln);
555 		if (rt->rt_expire)
556 			rt->rt_expire = time.tv_sec + arpt_keep;
557 		rt->rt_flags &= ~RTF_REJECT;
558 		la->la_asked = 0;
559 		if (la->la_hold) {
560 			(*ifp->if_output)(ifp, la->la_hold,
561 				rt_key(rt), rt);
562 			la->la_hold = 0;
563 		}
564 	}
565 reply:
566 	if (op != ARPOP_REQUEST) {
567 	out:
568 		m_freem(m);
569 		return;
570 	}
571 	if (in_hosteq(itaddr, myaddr)) {
572 		/* I am the target */
573 		bcopy((caddr_t)ar_sha(ah), (caddr_t)ar_tha(ah), ah->ar_hln);
574 		bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln);
575 	} else {
576 		la = arplookup(&itaddr, 0, SIN_PROXY);
577 		if (la == 0)
578 			goto out;
579 		rt = la->la_rt;
580 		bcopy((caddr_t)ar_sha(ah), (caddr_t)ar_tha(ah), ah->ar_hln);
581 		sdl = SDL(rt->rt_gateway);
582 		bcopy(LLADDR(sdl), (caddr_t)ar_sha(ah), ah->ar_hln);
583 	}
584 
585 	bcopy((caddr_t)ar_spa(ah), (caddr_t)ar_tpa(ah), ah->ar_pln);
586 	bcopy((caddr_t)&itaddr, (caddr_t)ar_spa(ah), ah->ar_pln);
587 	ah->ar_op = htons(ARPOP_REPLY);
588 	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
589 	m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
590 	sa.sa_family = AF_ARP;
591 	sa.sa_len = 2;
592 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
593 	return;
594 }
595 
596 /*
597  * Free an arp entry.
598  */
599 static void
600 arptfree(la)
601 	register struct llinfo_arp *la;
602 {
603 	register struct rtentry *rt = la->la_rt;
604 	register struct sockaddr_dl *sdl;
605 
606 	if (rt == 0)
607 		panic("arptfree");
608 	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
609 	    sdl->sdl_family == AF_LINK) {
610 		sdl->sdl_alen = 0;
611 		la->la_asked = 0;
612 		rt->rt_flags &= ~RTF_REJECT;
613 		return;
614 	}
615 	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
616 	    0, (struct rtentry **)0);
617 }
618 
619 /*
620  * Lookup or enter a new address in arptab.
621  */
622 static struct llinfo_arp *
623 arplookup(addr, create, proxy)
624 	struct in_addr *addr;
625 	int create, proxy;
626 {
627 	register struct rtentry *rt;
628 	static struct sockaddr_inarp sin;
629 
630 	sin.sin_len = sizeof(sin);
631 	sin.sin_family = AF_INET;
632 	sin.sin_addr = *addr;
633 	sin.sin_other = proxy ? SIN_PROXY : 0;
634 	rt = rtalloc1(sintosa(&sin), create);
635 	if (rt == 0)
636 		return (0);
637 	rt->rt_refcnt--;
638 	if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
639 	    rt->rt_gateway->sa_family != AF_LINK) {
640 		if (create)
641 			log(LOG_DEBUG, "arplookup: unable to enter address for %x\n",
642 			    ntohl(addr->s_addr));
643 		return (0);
644 	}
645 	return ((struct llinfo_arp *)rt->rt_llinfo);
646 }
647 
648 int
649 arpioctl(cmd, data)
650 	u_long cmd;
651 	caddr_t data;
652 {
653 
654 	return (EOPNOTSUPP);
655 }
656 
657 void
658 arp_ifinit(ifp, ifa)
659 	struct ifnet *ifp;
660 	struct ifaddr *ifa;
661 {
662 	struct in_addr *ip;
663 
664 	/*
665 	 * Warn the user if another station has this IP address,
666 	 * but only if the interface IP address is not zero.
667 	 */
668 	ip = &IA_SIN(ifa)->sin_addr;
669 	if (!in_nullhost(*ip))
670 		arprequest(ifp, ip, ip, LLADDR(ifp->if_sadl));
671 
672 	ifa->ifa_rtrequest = arp_rtrequest;
673 	ifa->ifa_flags |= RTF_CLONING;
674 }
675 
676 /*
677  * Called from 10 Mb/s Ethernet interrupt handlers
678  * when ether packet type ETHERTYPE_REVARP
679  * is received.  Common length and type checks are done here,
680  * then the protocol-specific routine is called.
681  */
682 void
683 revarpinput(m)
684 	struct mbuf *m;
685 {
686 	struct arphdr *ar;
687 
688 	if (m->m_len < sizeof(struct arphdr))
689 		goto out;
690 	ar = mtod(m, struct arphdr *);
691 #if 0 /* XXX I don't think we need this... and it will prevent other LL */
692 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
693 		goto out;
694 #endif
695 	if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
696 		goto out;
697 	switch (ntohs(ar->ar_pro)) {
698 
699 	case ETHERTYPE_IP:
700 	case ETHERTYPE_IPTRAILERS:
701 		in_revarpinput(m);
702 		return;
703 
704 	default:
705 		break;
706 	}
707 out:
708 	m_freem(m);
709 }
710 
711 /*
712  * RARP for Internet protocols on 10 Mb/s Ethernet.
713  * Algorithm is that given in RFC 903.
714  * We are only using for bootstrap purposes to get an ip address for one of
715  * our interfaces.  Thus we support no user-interface.
716  *
717  * Since the contents of the RARP reply are specific to the interface that
718  * sent the request, this code must ensure that they are properly associated.
719  *
720  * Note: also supports ARP via RARP packets, per the RFC.
721  */
722 void
723 in_revarpinput(m)
724 	struct mbuf *m;
725 {
726 	struct ifnet *ifp;
727 	struct arphdr *ah;
728 	int op;
729 
730 	ah = mtod(m, struct arphdr *);
731 	op = ntohs(ah->ar_op);
732 	switch (op) {
733 	case ARPOP_REQUEST:
734 	case ARPOP_REPLY:	/* per RFC */
735 		in_arpinput(m);
736 		return;
737 	case ARPOP_REVREPLY:
738 		break;
739 	case ARPOP_REVREQUEST:	/* handled by rarpd(8) */
740 	default:
741 		goto out;
742 	}
743 	if (!revarp_in_progress)
744 		goto out;
745 	ifp = m->m_pkthdr.rcvif;
746 	if (ifp != myip_ifp) /* !same interface */
747 		goto out;
748 	if (myip_initialized)
749 		goto wake;
750 	if (bcmp(ar_tha(ah), LLADDR(ifp->if_sadl), ifp->if_sadl->sdl_alen))
751 		goto out;
752 	bcopy((caddr_t)ar_spa(ah), (caddr_t)&srv_ip, sizeof(srv_ip));
753 	bcopy((caddr_t)ar_tpa(ah), (caddr_t)&myip, sizeof(myip));
754 	myip_initialized = 1;
755 wake:	/* Do wakeup every time in case it was missed. */
756 	wakeup((caddr_t)&myip);
757 
758 out:
759 	m_freem(m);
760 }
761 
762 /*
763  * Send a RARP request for the ip address of the specified interface.
764  * The request should be RFC 903-compliant.
765  */
766 void
767 revarprequest(ifp)
768 	struct ifnet *ifp;
769 {
770 	struct sockaddr sa;
771 	struct mbuf *m;
772 	struct arphdr *ah;
773 
774 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
775 		return;
776 	m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
777 	    2*ifp->if_data.ifi_addrlen;
778 	m->m_pkthdr.len = m->m_len;
779 	MH_ALIGN(m, m->m_len);
780 	ah = mtod(m, struct arphdr *);
781 	bzero((caddr_t)ah, m->m_len);
782 	ah->ar_pro = htons(ETHERTYPE_IP);
783 	ah->ar_hln = ifp->if_data.ifi_addrlen;	/* hardware address length */
784 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
785 	ah->ar_op = htons(ARPOP_REVREQUEST);
786 
787 	bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln);
788 	bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_tha(ah), ah->ar_hln);
789 
790 	sa.sa_family = AF_ARP;
791 	sa.sa_len = 2;
792 	m->m_flags |= M_BCAST;
793 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
794 
795 }
796 
797 /*
798  * RARP for the ip address of the specified interface, but also
799  * save the ip address of the server that sent the answer.
800  * Timeout if no response is received.
801  */
802 int
803 revarpwhoarewe(ifp, serv_in, clnt_in)
804 	struct ifnet *ifp;
805 	struct in_addr *serv_in;
806 	struct in_addr *clnt_in;
807 {
808 	int result, count = 20;
809 
810 	if (!myip_initialized) {
811 		myip_ifp = ifp;
812 		revarp_in_progress = 1;
813 		while (count--) {
814 			revarprequest(ifp);
815 			result = tsleep((caddr_t)&myip, PSOCK, "revarp", hz/2);
816 			if (result != EWOULDBLOCK)
817 				break;
818 		}
819 		revarp_in_progress = 0;
820 	}
821 	if (!myip_initialized)
822 		return ENETUNREACH;
823 
824 	bcopy((caddr_t)&srv_ip, serv_in, sizeof(*serv_in));
825 	bcopy((caddr_t)&myip, clnt_in, sizeof(*clnt_in));
826 	return 0;
827 }
828 
829 /* For compatibility: only saves interface address. */
830 int
831 revarpwhoami(in, ifp)
832 	struct in_addr *in;
833 	struct ifnet *ifp;
834 {
835 	struct in_addr server;
836 	return (revarpwhoarewe(ifp, &server, in));
837 }
838 
839 
840 #ifdef DDB
841 
842 #include <machine/db_machdep.h>
843 #include <ddb/db_interface.h>
844 #include <ddb/db_output.h>
845 static void
846 db_print_sa(sa)
847 	struct sockaddr *sa;
848 {
849 	int len;
850 	u_char *p;
851 
852 	if (sa == 0) {
853 		db_printf("[NULL]");
854 		return;
855 	}
856 
857 	p = (u_char*)sa;
858 	len = sa->sa_len;
859 	db_printf("[");
860 	while (len > 0) {
861 		db_printf("%d", *p);
862 		p++; len--;
863 		if (len) db_printf(",");
864 	}
865 	db_printf("]\n");
866 }
867 static void
868 db_print_ifa(ifa)
869 	struct ifaddr *ifa;
870 {
871 	if (ifa == 0)
872 		return;
873 	db_printf("  ifa_addr=");
874 	db_print_sa(ifa->ifa_addr);
875 	db_printf("  ifa_dsta=");
876 	db_print_sa(ifa->ifa_dstaddr);
877 	db_printf("  ifa_mask=");
878 	db_print_sa(ifa->ifa_netmask);
879 	db_printf("  flags=0x%x,refcnt=%d,metric=%d\n",
880 			  ifa->ifa_flags,
881 			  ifa->ifa_refcnt,
882 			  ifa->ifa_metric);
883 }
884 static void
885 db_print_llinfo(li)
886 	caddr_t li;
887 {
888 	struct llinfo_arp *la;
889 
890 	if (li == 0)
891 		return;
892 	la = (struct llinfo_arp *)li;
893 	db_printf("  la_rt=%p la_hold=%p, la_asked=0x%lx\n",
894 			  la->la_rt, la->la_hold, la->la_asked);
895 }
896 /*
897  * Function to pass to rn_walktree().
898  * Return non-zero error to abort walk.
899  */
900 static int
901 db_show_radix_node(rn, w)
902 	struct radix_node *rn;
903 	void *w;
904 {
905 	struct rtentry *rt = (struct rtentry *)rn;
906 
907 	db_printf("rtentry=%p", rt);
908 
909 	db_printf(" flags=0x%x refcnt=%d use=%ld expire=%ld\n",
910 			  rt->rt_flags, rt->rt_refcnt,
911 			  rt->rt_use, rt->rt_expire);
912 
913 	db_printf(" key="); db_print_sa(rt_key(rt));
914 	db_printf(" mask="); db_print_sa(rt_mask(rt));
915 	db_printf(" gw="); db_print_sa(rt->rt_gateway);
916 
917 	db_printf(" ifp=%p ", rt->rt_ifp);
918 	if (rt->rt_ifp)
919 		db_printf("(%s)", rt->rt_ifp->if_xname);
920 	else
921 		db_printf("(NULL)");
922 
923 	db_printf(" ifa=%p\n", rt->rt_ifa);
924 	db_print_ifa(rt->rt_ifa);
925 
926 	db_printf(" genmask="); db_print_sa(rt->rt_genmask);
927 
928 	db_printf(" gwroute=%p llinfo=%p\n",
929 			  rt->rt_gwroute, rt->rt_llinfo);
930 	db_print_llinfo(rt->rt_llinfo);
931 
932 	return (0);
933 }
934 /*
935  * Function to print all the route trees.
936  * Use this from ddb:  "call db_show_arptab"
937  */
938 int
939 db_show_arptab()
940 {
941 	struct radix_node_head *rnh;
942 	rnh = rt_tables[AF_INET];
943 	db_printf("Route tree for AF_INET\n");
944 	if (rnh == NULL) {
945 		db_printf(" (not initialized)\n");
946 		return (0);
947 	}
948 	rn_walktree(rnh, db_show_radix_node, NULL);
949 	return (0);
950 }
951 #endif
952 #endif /* INET */
953