xref: /netbsd-src/sys/netinet/if_arp.c (revision d9158b13b5dfe46201430699a3f7a235ecf28df3)
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  *	from: @(#)if_ether.c	8.1 (Berkeley) 6/10/93
34  *	$Id: if_arp.c,v 1.14 1994/06/04 03:15:09 gwr Exp $
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 <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/socket.h>
48 #include <sys/time.h>
49 #include <sys/kernel.h>
50 #include <sys/errno.h>
51 #include <sys/ioctl.h>
52 #include <sys/syslog.h>
53 
54 #include <net/if.h>
55 #include <net/if_dl.h>
56 #include <net/route.h>
57 
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/in_var.h>
61 #include <netinet/ip.h>
62 #include <netinet/if_ether.h>
63 
64 #define SIN(s) ((struct sockaddr_in *)s)
65 #define SDL(s) ((struct sockaddr_dl *)s)
66 #define SRP(s) ((struct sockaddr_inarp *)s)
67 
68 /*
69  * ARP trailer negotiation.  Trailer protocol is not IP specific,
70  * but ARP request/response use IP addresses.
71  */
72 #define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL
73 
74 /* timer values */
75 int	arpt_prune = (5*60*1);	/* walk list every 5 minutes */
76 int	arpt_keep = (20*60);	/* once resolved, good for 20 more minutes */
77 int	arpt_down = 20;		/* once declared down, don't send for 20 secs */
78 #define	rt_expire rt_rmx.rmx_expire
79 
80 static	void arprequest __P((struct arpcom *, u_long *, u_long *, u_char *));
81 static	void arptfree __P((struct llinfo_arp *));
82 static	void arptimer __P((void *));
83 static	struct llinfo_arp *arplookup __P((u_long, int, int));
84 static	void in_arpinput __P((struct mbuf *));
85 
86 extern	struct ifnet loif;
87 extern	struct timeval time;
88 struct	llinfo_arp llinfo_arp = {&llinfo_arp, &llinfo_arp};
89 struct	ifqueue arpintrq = {0, 0, 0, 50};
90 int	arp_inuse, arp_allocated, arp_intimer;
91 int	arp_maxtries = 5;
92 int	useloopback = 1;	/* use loopback interface for local traffic */
93 int	arpinit_done = 0;
94 
95 /* revarp state */
96 static struct	in_addr myip, srv_ip;
97 static int	myip_initialized = 0;
98 static int	revarp_in_progress = 0;
99 static struct	ifnet *myip_ifp = NULL;
100 
101 /*
102  * Timeout routine.  Age arp_tab entries periodically.
103  */
104 /* ARGSUSED */
105 static void
106 arptimer(arg)
107 	void *arg;
108 {
109 	int s = splnet();
110 	register struct llinfo_arp *la = llinfo_arp.la_next;
111 
112 	timeout(arptimer, NULL, arpt_prune * hz);
113 	while (la != &llinfo_arp) {
114 		register struct rtentry *rt = la->la_rt;
115 		la = la->la_next;
116 		if (rt->rt_expire && rt->rt_expire <= time.tv_sec)
117 			arptfree(la->la_prev); /* timer has expired; clear */
118 	}
119 	splx(s);
120 }
121 
122 /*
123  * Parallel to llc_rtrequest.
124  */
125 void
126 arp_rtrequest(req, rt, sa)
127 	int req;
128 	register struct rtentry *rt;
129 	struct sockaddr *sa;
130 {
131 	register struct sockaddr *gate = rt->rt_gateway;
132 	register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
133 	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
134 
135 	if (!arpinit_done) {
136 		arpinit_done = 1;
137 		/*
138 		 * We generate expiration times from time.tv_sec
139 		 * so avoid accidently creating permanent routes.
140 		 */
141 		if (time.tv_sec == 0) {
142 			time.tv_sec++;
143 		}
144 		timeout(arptimer, (caddr_t)0, hz);
145 	}
146 	if (rt->rt_flags & RTF_GATEWAY)
147 		return;
148 	switch (req) {
149 
150 	case RTM_ADD:
151 		/*
152 		 * XXX: If this is a manually added route to interface
153 		 * such as older version of routed or gated might provide,
154 		 * restore cloning bit.
155 		 */
156 		if ((rt->rt_flags & RTF_HOST) == 0 &&
157 		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
158 			rt->rt_flags |= RTF_CLONING;
159 		if (rt->rt_flags & RTF_CLONING) {
160 			/*
161 			 * Case 1: This route should come from a route to iface.
162 			 */
163 			rt_setgate(rt, rt_key(rt),
164 					(struct sockaddr *)&null_sdl);
165 			gate = rt->rt_gateway;
166 			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
167 			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
168 			/*
169 			 * Give this route an expiration time, even though
170 			 * it's a "permanent" route, so that routes cloned
171 			 * from it do not need their expiration time set.
172 			 */
173 			rt->rt_expire = time.tv_sec;
174 			break;
175 		}
176 		/* Announce a new entry if requested. */
177 		if (rt->rt_flags & RTF_ANNOUNCE)
178 			arprequest((struct arpcom *)rt->rt_ifp,
179 			    &SIN(rt_key(rt))->sin_addr.s_addr,
180 			    &SIN(rt_key(rt))->sin_addr.s_addr,
181 			    (u_char *)LLADDR(SDL(gate)));
182 		/*FALLTHROUGH*/
183 	case RTM_RESOLVE:
184 		if (gate->sa_family != AF_LINK ||
185 		    gate->sa_len < sizeof(null_sdl)) {
186 			log(LOG_DEBUG, "arp_rtrequest: bad gateway value");
187 			break;
188 		}
189 		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
190 		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
191 		if (la != 0)
192 			break; /* This happens on a route change */
193 		/*
194 		 * Case 2:  This route may come from cloning, or a manual route
195 		 * add with a LL address.
196 		 */
197 		R_Malloc(la, struct llinfo_arp *, sizeof(*la));
198 		rt->rt_llinfo = (caddr_t)la;
199 		if (la == 0) {
200 			log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
201 			break;
202 		}
203 		arp_inuse++, arp_allocated++;
204 		Bzero(la, sizeof(*la));
205 		la->la_rt = rt;
206 		rt->rt_flags |= RTF_LLINFO;
207 		insque(la, &llinfo_arp);
208 		if (SIN(rt_key(rt))->sin_addr.s_addr ==
209 		    (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
210 		    /*
211 		     * This test used to be
212 		     *	if (loif.if_flags & IFF_UP)
213 		     * It allowed local traffic to be forced
214 		     * through the hardware by configuring the loopback down.
215 		     * However, it causes problems during network configuration
216 		     * for boards that can't receive packets they send.
217 		     * It is now necessary to clear "useloopback" and remove
218 		     * the route to force traffic out to the hardware.
219 		     */
220 			rt->rt_expire = 0;
221 			Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr,
222 				LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6);
223 			if (useloopback)
224 				rt->rt_ifp = &loif;
225 
226 		}
227 		break;
228 
229 	case RTM_DELETE:
230 		if (la == 0)
231 			break;
232 		arp_inuse--;
233 		remque(la);
234 		rt->rt_llinfo = 0;
235 		rt->rt_flags &= ~RTF_LLINFO;
236 		if (la->la_hold)
237 			m_freem(la->la_hold);
238 		Free((caddr_t)la);
239 	}
240 }
241 
242 /*
243  * Broadcast an ARP packet, asking who has addr on interface ac.
244  */
245 void
246 arpwhohas(ac, addr)
247 	register struct arpcom *ac;
248 	register struct in_addr *addr;
249 {
250 	arprequest(ac, &ac->ac_ipaddr.s_addr, &addr->s_addr, ac->ac_enaddr);
251 }
252 
253 /*
254  * Broadcast an ARP request. Caller specifies:
255  *	- arp header source ip address
256  *	- arp header target ip address
257  *	- arp header source ethernet address
258  */
259 static void
260 arprequest(ac, sip, tip, enaddr)
261 	register struct arpcom *ac;
262 	register u_long *sip, *tip;
263 	register u_char *enaddr;
264 {
265 	register struct mbuf *m;
266 	register struct ether_header *eh;
267 	register struct ether_arp *ea;
268 	struct sockaddr sa;
269 
270 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
271 		return;
272 	m->m_len = sizeof(*ea);
273 	m->m_pkthdr.len = sizeof(*ea);
274 	MH_ALIGN(m, sizeof(*ea));
275 	ea = mtod(m, struct ether_arp *);
276 	eh = (struct ether_header *)sa.sa_data;
277 	bzero((caddr_t)ea, sizeof (*ea));
278 	bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
279 	    sizeof(eh->ether_dhost));
280 	eh->ether_type = htons(ETHERTYPE_ARP);
281 	ea->arp_hrd = htons(ARPHRD_ETHER);
282 	ea->arp_pro = htons(ETHERTYPE_IP);
283 	ea->arp_hln = sizeof(ea->arp_sha);	/* hardware address length */
284 	ea->arp_pln = sizeof(ea->arp_spa);	/* protocol address length */
285 	ea->arp_op = htons(ARPOP_REQUEST);
286 	bcopy((caddr_t)enaddr, (caddr_t)ea->arp_sha, sizeof(ea->arp_sha));
287 	bcopy((caddr_t)sip, (caddr_t)ea->arp_spa, sizeof(ea->arp_spa));
288 	bcopy((caddr_t)tip, (caddr_t)ea->arp_tpa, sizeof(ea->arp_tpa));
289 	sa.sa_family = AF_UNSPEC;
290 	sa.sa_len = sizeof(sa);
291 	(*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
292 }
293 
294 /*
295  * Resolve an IP address into an ethernet address.  If success,
296  * desten is filled in.  If there is no entry in arptab,
297  * set one up and broadcast a request for the IP address.
298  * Hold onto this mbuf and resend it once the address
299  * is finally resolved.  A return value of 1 indicates
300  * that desten has been filled in and the packet should be sent
301  * normally; a 0 return indicates that the packet has been
302  * taken over here, either now or for later transmission.
303  */
304 int
305 arpresolve(ac, rt, m, dst, desten)
306 	register struct arpcom *ac;
307 	register struct rtentry *rt;
308 	struct mbuf *m;
309 	register struct sockaddr *dst;
310 	register u_char *desten;
311 {
312 	register struct llinfo_arp *la;
313 	struct sockaddr_dl *sdl;
314 
315 	if (m->m_flags & M_BCAST) {	/* broadcast */
316 		bcopy((caddr_t)etherbroadcastaddr, (caddr_t)desten,
317 		    sizeof(etherbroadcastaddr));
318 		return (1);
319 	}
320 	if (m->m_flags & M_MCAST) {	/* multicast */
321 		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
322 		return(1);
323 	}
324 	if (rt)
325 		la = (struct llinfo_arp *)rt->rt_llinfo;
326 	else {
327 		if (la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0))
328 			rt = la->la_rt;
329 	}
330 	if (la == 0 || rt == 0) {
331 		log(LOG_DEBUG, "arpresolve: can't allocate llinfo");
332 		m_freem(m);
333 		return (0);
334 	}
335 	sdl = SDL(rt->rt_gateway);
336 	/*
337 	 * Check the address family and length is valid, the address
338 	 * is resolved; otherwise, try to resolve.
339 	 */
340 	if ((rt->rt_expire == 0 || rt->rt_expire > time.tv_sec) &&
341 	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
342 		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
343 		return 1;
344 	}
345 	/*
346 	 * There is an arptab entry, but no ethernet address
347 	 * response yet.  Replace the held mbuf with this
348 	 * latest one.
349 	 */
350 	if (la->la_hold)
351 		m_freem(la->la_hold);
352 	la->la_hold = m;
353 	/*
354 	 * Re-send the ARP request when appropriate.
355 	 */
356 #ifdef	DIAGNOSTIC
357 	if (rt->rt_expire == 0) {
358 		/* This should never happen. (Should it? -gwr) */
359 		printf("arpresolve: unresolved and rt_expire == 0\n");
360 		/* Set expiration time to now (expired). */
361 		rt->rt_expire = time.tv_sec;
362 	}
363 #endif
364 	if (rt->rt_expire) {
365 		rt->rt_flags &= ~RTF_REJECT;
366 		if (la->la_asked == 0 || rt->rt_expire != time.tv_sec) {
367 			rt->rt_expire = time.tv_sec;
368 			if (la->la_asked++ < arp_maxtries)
369 				arpwhohas(ac, &(SIN(dst)->sin_addr));
370 			else {
371 				rt->rt_flags |= RTF_REJECT;
372 				rt->rt_expire += arpt_down;
373 				la->la_asked = 0;
374 			}
375 		}
376 	}
377 	return (0);
378 }
379 
380 /*
381  * Common length and type checks are done here,
382  * then the protocol-specific routine is called.
383  */
384 void
385 arpintr()
386 {
387 	register struct mbuf *m;
388 	register struct arphdr *ar;
389 	int s;
390 
391 	while (arpintrq.ifq_head) {
392 		s = splimp();
393 		IF_DEQUEUE(&arpintrq, m);
394 		splx(s);
395 		if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
396 			panic("arpintr");
397 		if (m->m_len >= sizeof(struct arphdr) &&
398 		    (ar = mtod(m, struct arphdr *)) &&
399 		    ntohs(ar->ar_hrd) == ARPHRD_ETHER &&
400 		    m->m_len >=
401 		      sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
402 			switch (ntohs(ar->ar_pro)) {
403 
404 			case ETHERTYPE_IP:
405 			case ETHERTYPE_IPTRAILERS:
406 				in_arpinput(m);
407 				continue;
408 			}
409 		m_freem(m);
410 	}
411 }
412 
413 /*
414  * ARP for Internet protocols on 10 Mb/s Ethernet.
415  * Algorithm is that given in RFC 826.
416  * In addition, a sanity check is performed on the sender
417  * protocol address, to catch impersonators.
418  * We no longer handle negotiations for use of trailer protocol:
419  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
420  * along with IP replies if we wanted trailers sent to us,
421  * and also sent them in response to IP replies.
422  * This allowed either end to announce the desire to receive
423  * trailer packets.
424  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
425  * but formerly didn't normally send requests.
426  */
427 static void
428 in_arpinput(m)
429 	struct mbuf *m;
430 {
431 	register struct ether_arp *ea;
432 	register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif;
433 	struct ether_header *eh;
434 	register struct llinfo_arp *la = 0;
435 	register struct rtentry *rt;
436 	struct in_ifaddr *ia, *maybe_ia = 0;
437 	struct sockaddr_dl *sdl;
438 	struct sockaddr sa;
439 	struct in_addr isaddr, itaddr, myaddr;
440 	int op;
441 
442 	ea = mtod(m, struct ether_arp *);
443 	op = ntohs(ea->arp_op);
444 	bcopy((caddr_t)ea->arp_spa, (caddr_t)&isaddr, sizeof (isaddr));
445 	bcopy((caddr_t)ea->arp_tpa, (caddr_t)&itaddr, sizeof (itaddr));
446 	for (ia = in_ifaddr; ia; ia = ia->ia_next)
447 		if (ia->ia_ifp == &ac->ac_if) {
448 			maybe_ia = ia;
449 			if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) ||
450 			     (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr))
451 				break;
452 		}
453 	if (maybe_ia == 0)
454 		goto out;
455 	myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr;
456 	if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr,
457 	    sizeof (ea->arp_sha)))
458 		goto out;	/* it's from me, ignore it. */
459 	if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr,
460 	    sizeof (ea->arp_sha))) {
461 		log(LOG_ERR,
462 		    "arp: ether address is broadcast for IP address %x!\n",
463 		    ntohl(isaddr.s_addr));
464 		goto out;
465 	}
466 	if (isaddr.s_addr == myaddr.s_addr) {
467 		log(LOG_ERR,
468 		   "duplicate IP address %x!! sent from ethernet address: %s\n",
469 		   ntohl(isaddr.s_addr), ether_sprintf(ea->arp_sha));
470 		itaddr = myaddr;
471 		goto reply;
472 	}
473 	la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
474 	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
475 		if (sdl->sdl_alen &&
476 		    bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen))
477 			log(LOG_INFO, "arp info overwritten for %x by %s\n",
478 			    isaddr.s_addr, ether_sprintf(ea->arp_sha));
479 		bcopy((caddr_t)ea->arp_sha, LLADDR(sdl),
480 		    sdl->sdl_alen = sizeof(ea->arp_sha));
481 		if (rt->rt_expire)
482 			rt->rt_expire = time.tv_sec + arpt_keep;
483 		rt->rt_flags &= ~RTF_REJECT;
484 		la->la_asked = 0;
485 		if (la->la_hold) {
486 			(*ac->ac_if.if_output)(&ac->ac_if, la->la_hold,
487 				rt_key(rt), rt);
488 			la->la_hold = 0;
489 		}
490 	}
491 reply:
492 	if (op != ARPOP_REQUEST) {
493 	out:
494 		m_freem(m);
495 		return;
496 	}
497 	if (itaddr.s_addr == myaddr.s_addr) {
498 		/* I am the target */
499 		bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha,
500 		    sizeof(ea->arp_sha));
501 		bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha,
502 		    sizeof(ea->arp_sha));
503 	} else {
504 		la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
505 		if (la == 0)
506 			goto out;
507 		rt = la->la_rt;
508 		bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha,
509 		    sizeof(ea->arp_sha));
510 		sdl = SDL(rt->rt_gateway);
511 		bcopy(LLADDR(sdl), (caddr_t)ea->arp_sha, sizeof(ea->arp_sha));
512 	}
513 
514 	bcopy((caddr_t)ea->arp_spa, (caddr_t)ea->arp_tpa, sizeof(ea->arp_spa));
515 	bcopy((caddr_t)&itaddr, (caddr_t)ea->arp_spa, sizeof(ea->arp_spa));
516 	ea->arp_op = htons(ARPOP_REPLY);
517 	ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */
518 	eh = (struct ether_header *)sa.sa_data;
519 	bcopy((caddr_t)ea->arp_tha, (caddr_t)eh->ether_dhost,
520 	    sizeof(eh->ether_dhost));
521 	eh->ether_type = htons(ETHERTYPE_ARP);
522 	sa.sa_family = AF_UNSPEC;
523 	sa.sa_len = sizeof(sa);
524 	(*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
525 	return;
526 }
527 
528 /*
529  * Free an arp entry.
530  */
531 static void
532 arptfree(la)
533 	register struct llinfo_arp *la;
534 {
535 	register struct rtentry *rt = la->la_rt;
536 	register struct sockaddr_dl *sdl;
537 
538 	if (rt == 0)
539 		panic("arptfree");
540 	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
541 	    sdl->sdl_family == AF_LINK) {
542 		sdl->sdl_alen = 0;
543 		la->la_asked = 0;
544 		rt->rt_flags &= ~RTF_REJECT;
545 		return;
546 	}
547 	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
548 	    0, (struct rtentry **)0);
549 }
550 
551 /*
552  * Lookup or enter a new address in arptab.
553  */
554 static struct llinfo_arp *
555 arplookup(addr, create, proxy)
556 	u_long addr;
557 	int create, proxy;
558 {
559 	register struct rtentry *rt;
560 	static struct sockaddr_inarp sin;
561 
562 	sin.sin_len = sizeof(sin);
563 	sin.sin_family = AF_INET;
564 	sin.sin_addr.s_addr = addr;
565 	sin.sin_other = proxy ? SIN_PROXY : 0;
566 	rt = rtalloc1((struct sockaddr *)&sin, create);
567 	if (rt == 0)
568 		return (0);
569 	rt->rt_refcnt--;
570 	if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
571 	    rt->rt_gateway->sa_family != AF_LINK) {
572 		if (create)
573 			log(LOG_DEBUG, "arptnew failed on %x\n", ntohl(addr));
574 		return (0);
575 	}
576 	return ((struct llinfo_arp *)rt->rt_llinfo);
577 }
578 
579 int
580 arpioctl(cmd, data)
581 	int cmd;
582 	caddr_t data;
583 {
584 	return (EOPNOTSUPP);
585 }
586 
587 /*
588  * Called from 10 Mb/s Ethernet interrupt handlers
589  * when ether packet type ETHERTYPE_REVARP
590  * is received.  Common length and type checks are done here,
591  * then the protocol-specific routine is called.
592  */
593 void
594 revarpinput(m)
595 	struct mbuf *m;
596 {
597 	struct arphdr *ar;
598 	int op, s;
599 
600 	if (m->m_len < sizeof(struct arphdr))
601 		goto out;
602 	ar = mtod(m, struct arphdr *);
603 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
604 		goto out;
605 	if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
606 		goto out;
607 	switch (ntohs(ar->ar_pro)) {
608 
609 	case ETHERTYPE_IP:
610 	case ETHERTYPE_IPTRAILERS:
611 		in_revarpinput(m);
612 		return;
613 
614 	default:
615 		break;
616 	}
617 out:
618 	m_freem(m);
619 }
620 
621 /*
622  * RARP for Internet protocols on 10 Mb/s Ethernet.
623  * Algorithm is that given in RFC 903.
624  * We are only using for bootstrap purposes to get an ip address for one of
625  * our interfaces.  Thus we support no user-interface.
626  *
627  * Since the contents of the RARP reply are specific to the interface that
628  * sent the request, this code must ensure that they are properly associated.
629  *
630  * Note: also supports ARP via RARP packets, per the RFC.
631  */
632 in_revarpinput(m)
633 	struct mbuf *m;
634 {
635 	struct ifnet *ifp;
636 	struct ether_arp *ar;
637 	int op, s;
638 
639 	ar = mtod(m, struct ether_arp *);
640 	op = ntohs(ar->arp_op);
641 	switch (op) {
642 	case ARPOP_REQUEST:
643 	case ARPOP_REPLY:	/* per RFC */
644 		in_arpinput(m);
645 		return;
646 	case ARPOP_REVREPLY:
647 		break;
648 	case ARPOP_REVREQUEST:	/* handled by rarpd(8) */
649 	default:
650 		goto out;
651 	}
652 	if (!revarp_in_progress)
653 		goto out;
654 	ifp = m->m_pkthdr.rcvif;
655 	if (ifp != myip_ifp) /* !same interface */
656 		goto out;
657 	if (myip_initialized)
658 		goto wake;
659 	if (bcmp(ar->arp_tha, ((struct arpcom *)ifp)->ac_enaddr,
660 	    sizeof(ar->arp_tha)))
661 		goto out;
662 	bcopy((caddr_t)ar->arp_spa, (caddr_t)&srv_ip, sizeof(srv_ip));
663 	bcopy((caddr_t)ar->arp_tpa, (caddr_t)&myip, sizeof(myip));
664 	myip_initialized = 1;
665 wake:	/* Do wakeup every time in case it was missed. */
666 	wakeup((caddr_t)&myip);
667 
668 out:
669 	m_freem(m);
670 }
671 
672 /*
673  * Send a RARP request for the ip address of the specified interface.
674  * The request should be RFC 903-compliant.
675  */
676 void
677 revarprequest(ifp)
678 	struct ifnet *ifp;
679 {
680 	struct sockaddr sa;
681 	struct mbuf *m;
682 	struct ether_header *eh;
683 	struct ether_arp *ea;
684 	struct arpcom *ac = (struct arpcom *)ifp;
685 
686 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
687 		return;
688 	m->m_len = sizeof(*ea);
689 	m->m_pkthdr.len = sizeof(*ea);
690 	MH_ALIGN(m, sizeof(*ea));
691 	ea = mtod(m, struct ether_arp *);
692 	eh = (struct ether_header *)sa.sa_data;
693 	bzero((caddr_t)ea, sizeof(*ea));
694 	bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
695 	    sizeof(eh->ether_dhost));
696 	eh->ether_type = htons(ETHERTYPE_REVARP);
697 	ea->arp_hrd = htons(ARPHRD_ETHER);
698 	ea->arp_pro = htons(ETHERTYPE_IP);
699 	ea->arp_hln = sizeof(ea->arp_sha);	/* hardware address length */
700 	ea->arp_pln = sizeof(ea->arp_spa);	/* protocol address length */
701 	ea->arp_op = htons(ARPOP_REVREQUEST);
702 	bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha,
703 	   sizeof(ea->arp_sha));
704 	bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_tha,
705 	   sizeof(ea->arp_tha));
706 	sa.sa_family = AF_UNSPEC;
707 	sa.sa_len = sizeof(sa);
708 	ifp->if_output(ifp, m, &sa, (struct rtentry *)0);
709 }
710 
711 /*
712  * RARP for the ip address of the specified interface, but also
713  * save the ip address of the server that sent the answer.
714  * Timeout if no response is received.
715  */
716 int
717 revarpwhoarewe(ifp, serv_in, clnt_in)
718 	struct ifnet *ifp;
719 	struct in_addr *serv_in;
720 	struct in_addr *clnt_in;
721 {
722 	int result, count = 20;
723 
724 	if (myip_initialized)
725 		return EIO;
726 
727 	myip_ifp = ifp;
728 	revarp_in_progress = 1;
729 	while (count--) {
730 		revarprequest(ifp);
731 		result = tsleep((caddr_t)&myip, PSOCK, "revarp", hz/2);
732 		if (result != EWOULDBLOCK)
733 			break;
734 	}
735 	revarp_in_progress = 0;
736 	if (!myip_initialized)
737 		return ENETUNREACH;
738 
739 	bcopy((caddr_t)&srv_ip, serv_in, sizeof(*serv_in));
740 	bcopy((caddr_t)&myip, clnt_in, sizeof(*clnt_in));
741 	return 0;
742 }
743 
744 /* For compatibility: only saves interface address. */
745 int
746 revarpwhoami(in, ifp)
747 	struct in_addr *in;
748 	struct ifnet *ifp;
749 {
750 	struct in_addr server;
751 	return (revarpwhoarewe(ifp, &server, in));
752 }
753 
754 
755 #ifdef	DDB
756 static void
757 db_print_sa(sa)
758 	struct sockaddr *sa;
759 {
760 	int len;
761 	u_char *p;
762 
763 	if (sa == 0) {
764 		db_printf("[NULL]");
765 		return;
766 	}
767 
768 	p = (u_char*)sa;
769 	len = sa->sa_len;
770 	db_printf("[");
771 	while (len > 0) {
772 		db_printf("%d", *p);
773 		p++; len--;
774 		if (len) db_printf(",");
775 	}
776 	db_printf("]\n");
777 }
778 static void
779 db_print_ifa(ifa)
780 	struct ifaddr *ifa;
781 {
782 	if (ifa == 0)
783 		return;
784 	db_printf("  ifa_addr=");
785 	db_print_sa(ifa->ifa_addr);
786 	db_printf("  ifa_dsta=");
787 	db_print_sa(ifa->ifa_dstaddr);
788 	db_printf("  ifa_mask=");
789 	db_print_sa(ifa->ifa_netmask);
790 	db_printf("  flags=0x%x,refcnt=%d,metric=%d\n",
791 			  ifa->ifa_flags,
792 			  ifa->ifa_refcnt,
793 			  ifa->ifa_metric);
794 }
795 static void
796 db_print_llinfo(li)
797 	caddr_t li;
798 {
799 	struct llinfo_arp *la;
800 
801 	if (li == 0)
802 		return;
803 	la = (struct llinfo_arp *)li;
804 	db_printf("  la_rt=0x%x la_hold=0x%x, la_asked=0x%x\n",
805 			  la->la_rt, la->la_hold, la->la_asked);
806 }
807 /*
808  * Function to pass to rn_walktree().
809  * Return non-zero error to abort walk.
810  */
811 static int
812 db_show_radix_node(rn, w)
813 	struct radix_node *rn;
814 	void *w;
815 {
816 	struct rtentry *rt = (struct rtentry *)rn;
817 
818 	db_printf("rtentry=0x%x", rt);
819 
820 	db_printf(" flags=0x%x refcnt=%d use=%d expire=%d\n",
821 			  rt->rt_flags, rt->rt_refcnt,
822 			  rt->rt_use, rt->rt_expire);
823 
824 	db_printf(" key="); db_print_sa(rt_key(rt));
825 	db_printf(" mask="); db_print_sa(rt_mask(rt));
826 	db_printf(" gw="); db_print_sa(rt->rt_gateway);
827 
828 	db_printf(" ifp=0x%x ", rt->rt_ifp);
829 	if (rt->rt_ifp)
830 		db_printf("(%s%d)",
831 				  rt->rt_ifp->if_name,
832 				  rt->rt_ifp->if_unit);
833 	else
834 		db_printf("(NULL)");
835 
836 	db_printf(" ifa=0x%x\n", rt->rt_ifa);
837 	db_print_ifa(rt->rt_ifa);
838 
839 	db_printf(" genmask="); db_print_sa(rt->rt_genmask);
840 
841 	db_printf(" gwroute=0x%x llinfo=0x%x\n",
842 			  rt->rt_gwroute, rt->rt_llinfo);
843 	db_print_llinfo(rt->rt_llinfo);
844 
845 	return (0);
846 }
847 /*
848  * Function to print all the route trees.
849  * Use this from ddb:  "call db_show_arptab"
850  */
851 db_show_arptab()
852 {
853 	struct radix_node_head *rnh;
854 	rnh = rt_tables[AF_INET];
855 	db_printf("Route tree for AF_INET\n");
856 	if (rnh == NULL) {
857 		db_printf(" (not initialized)\n");
858 		return (0);
859 	}
860 	rn_walktree(rnh, db_show_radix_node, NULL);
861 	return (0);
862 }
863 #endif
864