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