xref: /openbsd-src/sys/netinet/if_ether.c (revision cd1eb269cafb12c415be1749cd4a4b5422710415)
1 /*	$OpenBSD: if_ether.c,v 1.86 2010/05/07 13:33:16 claudio 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. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)if_ether.c	8.1 (Berkeley) 6/10/93
33  */
34 
35 /*
36  * Ethernet address resolution protocol.
37  * TODO:
38  *	add "inuse/lock" bit (or ref. count) along with valid bit
39  */
40 
41 #ifdef INET
42 #include "carp.h"
43 
44 #include "bridge.h"
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/mbuf.h>
49 #include <sys/socket.h>
50 #include <sys/kernel.h>
51 #include <sys/syslog.h>
52 #include <sys/proc.h>
53 
54 #include <net/if.h>
55 #include <net/if_dl.h>
56 #include <net/route.h>
57 #include <net/if_fddi.h>
58 #include <net/if_types.h>
59 
60 #include <netinet/in.h>
61 #include <netinet/in_var.h>
62 #include <netinet/if_ether.h>
63 #if NCARP > 0
64 #include <netinet/ip_carp.h>
65 #endif
66 
67 #define SIN(s) ((struct sockaddr_in *)s)
68 #define SDL(s) ((struct sockaddr_dl *)s)
69 #define SRP(s) ((struct sockaddr_inarp *)s)
70 
71 /*
72  * ARP trailer negotiation.  Trailer protocol is not IP specific,
73  * but ARP request/response use IP addresses.
74  */
75 #define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL
76 
77 /* timer values */
78 int	arpt_prune = (5*60*1);	/* walk list every 5 minutes */
79 int	arpt_keep = (20*60);	/* once resolved, good for 20 more minutes */
80 int	arpt_down = 20;		/* once declared down, don't send for 20 secs */
81 #define	rt_expire rt_rmx.rmx_expire
82 
83 void arptfree(struct llinfo_arp *);
84 void arptimer(void *);
85 struct llinfo_arp *arplookup(u_int32_t, int, int, u_int);
86 void in_arpinput(struct mbuf *);
87 
88 LIST_HEAD(, 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;
94 int	la_hold_total;
95 
96 /* revarp state */
97 struct in_addr myip, srv_ip;
98 int myip_initialized;
99 int revarp_in_progress;
100 struct ifnet *myip_ifp;
101 
102 #ifdef DDB
103 #include <uvm/uvm_extern.h>
104 
105 void	db_print_sa(struct sockaddr *);
106 void	db_print_ifa(struct ifaddr *);
107 void	db_print_llinfo(caddr_t);
108 int	db_show_radix_node(struct radix_node *, void *);
109 #endif
110 
111 /*
112  * Timeout routine.  Age arp_tab entries periodically.
113  */
114 /* ARGSUSED */
115 void
116 arptimer(arg)
117 	void *arg;
118 {
119 	struct timeout *to = (struct timeout *)arg;
120 	int s;
121 	struct llinfo_arp *la, *nla;
122 
123 	s = splsoftnet();
124 	timeout_add_sec(to, arpt_prune);
125 	for (la = LIST_FIRST(&llinfo_arp); la != LIST_END(&llinfo_arp);
126 	    la = nla) {
127 		struct rtentry *rt = la->la_rt;
128 
129 		nla = LIST_NEXT(la, la_list);
130 		if (rt->rt_expire && rt->rt_expire <= time_second)
131 			arptfree(la); /* timer has expired; clear */
132 	}
133 	splx(s);
134 }
135 
136 /*
137  * Parallel to llc_rtrequest.
138  */
139 void
140 arp_rtrequest(req, rt, info)
141 	int req;
142 	struct rtentry *rt;
143 	struct rt_addrinfo *info;
144 {
145 	struct sockaddr *gate = rt->rt_gateway;
146 	struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
147 	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
148 	struct in_ifaddr *ia;
149 	struct ifaddr *ifa;
150 	struct mbuf *m;
151 
152 	if (!arpinit_done) {
153 		static struct timeout arptimer_to;
154 
155 		arpinit_done = 1;
156 		/*
157 		 * We generate expiration times from time.tv_sec
158 		 * so avoid accidently creating permanent routes.
159 		 */
160 		if (time_second == 0) {
161 			time_second++;
162 		}
163 
164 		timeout_set(&arptimer_to, arptimer, &arptimer_to);
165 		timeout_add_sec(&arptimer_to, 1);
166 	}
167 
168 	if (rt->rt_flags & RTF_GATEWAY) {
169 		if (req != RTM_ADD)
170 			return;
171 
172 		/*
173 		 * linklayers with particular link MTU limitation.  it is a bit
174 		 * awkward to have FDDI handling here, we should split ARP from
175 		 * netinet/if_ether.c like NetBSD does.
176 		 */
177 		switch (rt->rt_ifp->if_type) {
178 		case IFT_FDDI:
179 			if (rt->rt_ifp->if_mtu > FDDIIPMTU)
180 				rt->rt_rmx.rmx_mtu = FDDIIPMTU;
181 			break;
182 		}
183 
184 		return;
185 	}
186 
187 	switch (req) {
188 
189 	case RTM_ADD:
190 		/*
191 		 * XXX: If this is a manually added route to interface
192 		 * such as older version of routed or gated might provide,
193 		 * restore cloning bit.
194 		 */
195 		if ((rt->rt_flags & RTF_HOST) == 0 &&
196 		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
197 			rt->rt_flags |= RTF_CLONING;
198 		if (rt->rt_flags & RTF_CLONING) {
199 			/*
200 			 * Case 1: This route should come from a route to iface.
201 			 */
202 			rt_setgate(rt, rt_key(rt),
203 			    (struct sockaddr *)&null_sdl,
204 			    rt->rt_ifp->if_rdomain);
205 			gate = rt->rt_gateway;
206 			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
207 			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
208 			/*
209 			 * Give this route an expiration time, even though
210 			 * it's a "permanent" route, so that routes cloned
211 			 * from it do not need their expiration time set.
212 			 */
213 			rt->rt_expire = time_second;
214 			/*
215 			 * linklayers with particular link MTU limitation.
216 			 */
217 			switch (rt->rt_ifp->if_type) {
218 			case IFT_FDDI:
219 				if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 &&
220 				    (rt->rt_rmx.rmx_mtu > FDDIIPMTU ||
221 				     (rt->rt_rmx.rmx_mtu == 0 &&
222 				      rt->rt_ifp->if_mtu > FDDIIPMTU)))
223 					rt->rt_rmx.rmx_mtu = FDDIIPMTU;
224 				break;
225 			}
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.s_addr,
232 			    &SIN(rt_key(rt))->sin_addr.s_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 
261 		TAILQ_FOREACH(ia, &in_ifaddr, ia_list) {
262 			if (ia->ia_ifp == rt->rt_ifp &&
263 			    SIN(rt_key(rt))->sin_addr.s_addr ==
264 			    (IA_SIN(ia))->sin_addr.s_addr)
265 				break;
266 		}
267 		if (ia) {
268 			/*
269 			 * This test used to be
270 			 *	if (lo0ifp->if_flags & IFF_UP)
271 			 * It allowed local traffic to be forced through
272 			 * the hardware by configuring the loopback down.
273 			 * However, it causes problems during network
274 			 * configuration for boards that can't receive
275 			 * packets they send.  It is now necessary to clear
276 			 * "useloopback" and remove the route to force
277 			 * traffic out to the hardware.
278 			 *
279 			 * In 4.4BSD, the above "if" statement checked
280 			 * rt->rt_ifa against rt_key(rt).  It was changed
281 			 * to the current form so that we can provide a
282 			 * better support for multiple IPv4 addresses on a
283 			 * interface.
284 			 */
285 			rt->rt_expire = 0;
286 			Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr,
287 			    LLADDR(SDL(gate)),
288 			    SDL(gate)->sdl_alen = ETHER_ADDR_LEN);
289 			if (useloopback)
290 				rt->rt_ifp = lo0ifp;
291 			/*
292 			 * make sure to set rt->rt_ifa to the interface
293 			 * address we are using, otherwise we will have trouble
294 			 * with source address selection.
295 			 */
296 			ifa = &ia->ia_ifa;
297 			if (ifa != rt->rt_ifa) {
298 				IFAFREE(rt->rt_ifa);
299 				ifa->ifa_refcnt++;
300 				rt->rt_ifa = ifa;
301 			}
302 		}
303 		break;
304 
305 	case RTM_DELETE:
306 		if (la == 0)
307 			break;
308 		arp_inuse--;
309 		LIST_REMOVE(la, la_list);
310 		rt->rt_llinfo = 0;
311 		rt->rt_flags &= ~RTF_LLINFO;
312 		while ((m = la->la_hold_head) != NULL) {
313 			la->la_hold_head = la->la_hold_head->m_nextpkt;
314 			la_hold_total--;
315 			m_freem(m);
316 		}
317 		Free((caddr_t)la);
318 	}
319 }
320 
321 /*
322  * Broadcast an ARP request. Caller specifies:
323  *	- arp header source ip address
324  *	- arp header target ip address
325  *	- arp header source ethernet address
326  */
327 void
328 arprequest(ifp, sip, tip, enaddr)
329 	struct ifnet *ifp;
330 	u_int32_t *sip, *tip;
331 	u_int8_t *enaddr;
332 {
333 	struct mbuf *m;
334 	struct ether_header *eh;
335 	struct ether_arp *ea;
336 	struct sockaddr sa;
337 
338 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
339 		return;
340 	m->m_len = sizeof(*ea);
341 	m->m_pkthdr.len = sizeof(*ea);
342 	m->m_pkthdr.rdomain = ifp->if_rdomain;
343 	MH_ALIGN(m, sizeof(*ea));
344 	ea = mtod(m, struct ether_arp *);
345 	eh = (struct ether_header *)sa.sa_data;
346 	bzero((caddr_t)ea, sizeof (*ea));
347 	bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
348 	    sizeof(eh->ether_dhost));
349 	eh->ether_type = htons(ETHERTYPE_ARP);	/* if_output will not swap */
350 	ea->arp_hrd = htons(ARPHRD_ETHER);
351 	ea->arp_pro = htons(ETHERTYPE_IP);
352 	ea->arp_hln = sizeof(ea->arp_sha);	/* hardware address length */
353 	ea->arp_pln = sizeof(ea->arp_spa);	/* protocol address length */
354 	ea->arp_op = htons(ARPOP_REQUEST);
355 	bcopy((caddr_t)enaddr, (caddr_t)eh->ether_shost,
356 	      sizeof(eh->ether_shost));
357 	bcopy((caddr_t)enaddr, (caddr_t)ea->arp_sha, sizeof(ea->arp_sha));
358 	bcopy((caddr_t)sip, (caddr_t)ea->arp_spa, sizeof(ea->arp_spa));
359 	bcopy((caddr_t)tip, (caddr_t)ea->arp_tpa, sizeof(ea->arp_tpa));
360 	sa.sa_family = pseudo_AF_HDRCMPLT;
361 	sa.sa_len = sizeof(sa);
362 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
363 }
364 
365 /*
366  * Resolve an IP address into an ethernet address.  If success,
367  * desten is filled in.  If there is no entry in arptab,
368  * set one up and broadcast a request for the IP address.
369  * Hold onto this mbuf and resend it once the address
370  * is finally resolved.  A return value of 1 indicates
371  * that desten has been filled in and the packet should be sent
372  * normally; a 0 return indicates that the packet has been
373  * taken over here, either now or for later transmission.
374  */
375 int
376 arpresolve(ac, rt, m, dst, desten)
377 	struct arpcom *ac;
378 	struct rtentry *rt;
379 	struct mbuf *m;
380 	struct sockaddr *dst;
381 	u_char *desten;
382 {
383 	struct llinfo_arp *la;
384 	struct sockaddr_dl *sdl;
385 	struct mbuf *mh;
386 
387 	if (m->m_flags & M_BCAST) {	/* broadcast */
388 		bcopy((caddr_t)etherbroadcastaddr, (caddr_t)desten,
389 		    sizeof(etherbroadcastaddr));
390 		return (1);
391 	}
392 	if (m->m_flags & M_MCAST) {	/* multicast */
393 		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
394 		return (1);
395 	}
396 	if (rt) {
397 		la = (struct llinfo_arp *)rt->rt_llinfo;
398 		if (la == NULL)
399 			log(LOG_DEBUG, "arpresolve: %s: route without link "
400 			    "local address\n", inet_ntoa(SIN(dst)->sin_addr));
401 	} else {
402 		if ((la = arplookup(SIN(dst)->sin_addr.s_addr, RT_REPORT, 0,
403 		    ac->ac_if.if_rdomain)) != NULL)
404 			rt = la->la_rt;
405 		else
406 			log(LOG_DEBUG,
407 			    "arpresolve: %s: can't allocate llinfo\n",
408 			    inet_ntoa(SIN(dst)->sin_addr));
409 	}
410 	if (la == 0 || rt == 0) {
411 		m_freem(m);
412 		return (0);
413 	}
414 	sdl = SDL(rt->rt_gateway);
415 	/*
416 	 * Check the address family and length is valid, the address
417 	 * is resolved; otherwise, try to resolve.
418 	 */
419 	if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
420 	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
421 		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
422 		return 1;
423 	}
424 	if (((struct ifnet *)ac)->if_flags & IFF_NOARP) {
425 		m_freem(m);
426 		return 0;
427 	}
428 
429 	/*
430 	 * There is an arptab entry, but no ethernet address
431 	 * response yet. Insert mbuf in hold queue if below limit
432 	 * if above the limit free the queue without queuing the new packet.
433 	 */
434 	if (la_hold_total < MAX_HOLD_TOTAL && la_hold_total < nmbclust / 64) {
435 		if (la->la_hold_count >= MAX_HOLD_QUEUE) {
436 			mh = la->la_hold_head;
437 			la->la_hold_head = la->la_hold_head->m_nextpkt;
438 			if (mh == la->la_hold_tail)
439 				la->la_hold_tail = NULL;
440 			la->la_hold_count--;
441 			la_hold_total--;
442 			m_freem(mh);
443 		}
444 		if (la->la_hold_tail == NULL)
445 			la->la_hold_head = m;
446 		else
447 			la->la_hold_tail->m_nextpkt = m;
448 		la->la_hold_tail = m;
449 		la->la_hold_count++;
450 		la_hold_total++;
451 	} else {
452 		while ((mh = la->la_hold_head) != NULL) {
453 			la->la_hold_head =
454 			    la->la_hold_head->m_nextpkt;
455 			la_hold_total--;
456 			m_freem(mh);
457 		}
458 		la->la_hold_tail = NULL;
459 		la->la_hold_count = 0;
460 		m_freem(m);
461 	}
462 
463 	/*
464 	 * Re-send the ARP request when appropriate.
465 	 */
466 #ifdef	DIAGNOSTIC
467 	if (rt->rt_expire == 0) {
468 		/* This should never happen. (Should it? -gwr) */
469 		printf("arpresolve: unresolved and rt_expire == 0\n");
470 		/* Set expiration time to now (expired). */
471 		rt->rt_expire = time_second;
472 	}
473 #endif
474 	if (rt->rt_expire) {
475 		rt->rt_flags &= ~RTF_REJECT;
476 		if (la->la_asked == 0 || rt->rt_expire != time_second) {
477 			rt->rt_expire = time_second;
478 			if (la->la_asked++ < arp_maxtries)
479 				arprequest(&ac->ac_if,
480 				    &(SIN(rt->rt_ifa->ifa_addr)->sin_addr.s_addr),
481 				    &(SIN(dst)->sin_addr.s_addr),
482 #if NCARP > 0
483 				    (rt->rt_ifp->if_type == IFT_CARP) ?
484 					((struct arpcom *) rt->rt_ifp->if_softc
485 					)->ac_enaddr :
486 #endif
487 				    ac->ac_enaddr);
488 			else {
489 				rt->rt_flags |= RTF_REJECT;
490 				rt->rt_expire += arpt_down;
491 				la->la_asked = 0;
492 				while ((mh = la->la_hold_head) != NULL) {
493 					la->la_hold_head =
494 					    la->la_hold_head->m_nextpkt;
495 					la_hold_total--;
496 					m_freem(mh);
497 				}
498 				la->la_hold_tail = NULL;
499 				la->la_hold_count = 0;
500 			}
501 		}
502 	}
503 	return (0);
504 }
505 
506 /*
507  * Common length and type checks are done here,
508  * then the protocol-specific routine is called.
509  */
510 void
511 arpintr()
512 {
513 	struct mbuf *m;
514 	struct arphdr *ar;
515 	int s, len;
516 
517 	for (;;) {
518 		s = splnet();
519 		IF_DEQUEUE(&arpintrq, m);
520 		splx(s);
521 		if (m == NULL)
522 			break;
523 #ifdef DIAGNOSTIC
524 		if ((m->m_flags & M_PKTHDR) == 0)
525 			panic("arpintr");
526 #endif
527 
528 		len = sizeof(struct arphdr);
529 		if (m->m_len < len && (m = m_pullup(m, len)) == NULL)
530 			continue;
531 
532 		ar = mtod(m, struct arphdr *);
533 		if (ntohs(ar->ar_hrd) != ARPHRD_ETHER) {
534 			m_freem(m);
535 			continue;
536 		}
537 
538 		len += 2 * (ar->ar_hln + ar->ar_pln);
539 		if (m->m_len < len && (m = m_pullup(m, len)) == NULL)
540 			continue;
541 
542 		switch (ntohs(ar->ar_pro)) {
543 		case ETHERTYPE_IP:
544 		case ETHERTYPE_IPTRAILERS:
545 			in_arpinput(m);
546 			continue;
547 		}
548 		m_freem(m);
549 	}
550 }
551 
552 /*
553  * ARP for Internet protocols on Ethernet.
554  * Algorithm is that given in RFC 826.
555  * In addition, a sanity check is performed on the sender
556  * protocol address, to catch impersonators.
557  * We no longer handle negotiations for use of trailer protocol:
558  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
559  * along with IP replies if we wanted trailers sent to us,
560  * and also sent them in response to IP replies.
561  * This allowed either end to announce the desire to receive
562  * trailer packets.
563  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
564  * but formerly didn't normally send requests.
565  */
566 void
567 in_arpinput(m)
568 	struct mbuf *m;
569 {
570 	struct ether_arp *ea;
571 	struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif;
572 	struct ether_header *eh;
573 	struct llinfo_arp *la = 0;
574 	struct rtentry *rt;
575 	struct in_ifaddr *ia;
576 	struct sockaddr_dl *sdl;
577 	struct sockaddr sa;
578 	struct in_addr isaddr, itaddr, myaddr;
579 	struct mbuf *mh, *mt;
580 	u_int8_t *enaddr = NULL;
581 #if NCARP > 0
582 	u_int8_t *ether_shost = NULL;
583 #endif
584 	int op;
585 
586 	ea = mtod(m, struct ether_arp *);
587 	op = ntohs(ea->arp_op);
588 	if ((op != ARPOP_REQUEST) && (op != ARPOP_REPLY))
589 		goto out;
590 #if notyet
591 	if ((op == ARPOP_REPLY) && (m->m_flags & (M_BCAST|M_MCAST))) {
592 		log(LOG_ERR,
593 		    "arp: received reply to broadcast or multicast address\n");
594 		goto out;
595 	}
596 #endif
597 
598 	bcopy((caddr_t)ea->arp_tpa, (caddr_t)&itaddr, sizeof(itaddr));
599 	bcopy((caddr_t)ea->arp_spa, (caddr_t)&isaddr, sizeof(isaddr));
600 
601 	/* First try: check target against our addresses */
602 	TAILQ_FOREACH(ia, &in_ifaddr, ia_list) {
603 		if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
604 			continue;
605 
606 #if NCARP > 0
607 		if (ia->ia_ifp->if_type == IFT_CARP &&
608 		    ((ia->ia_ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
609 		    (IFF_UP|IFF_RUNNING))) {
610 			if (ia->ia_ifp == m->m_pkthdr.rcvif) {
611 				if (op == ARPOP_REPLY)
612 					break;
613 				if (carp_iamatch(ia, ea->arp_sha,
614 				    &enaddr, &ether_shost))
615 					break;
616 				else
617 					goto out;
618 			}
619 		} else
620 #endif
621 			if (ia->ia_ifp == m->m_pkthdr.rcvif)
622 				break;
623 	}
624 
625 	/* Second try: check source against our addresses */
626 	if (ia == NULL) {
627 		TAILQ_FOREACH(ia, &in_ifaddr, ia_list) {
628 			if (isaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
629 				continue;
630 			if (ia->ia_ifp == m->m_pkthdr.rcvif)
631 				break;
632 		}
633 	}
634 
635 	/* Third try: not one of our addresses, just find an usable ia */
636 	if (ia == NULL) {
637 		struct ifaddr *ifa;
638 
639 		TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrlist, ifa_list) {
640 			if (ifa->ifa_addr->sa_family == AF_INET)
641 				break;
642 		}
643 		if (ifa)
644 			ia = (struct in_ifaddr *)ifa;
645 	}
646 
647 	if (ia == NULL)
648 		goto out;
649 
650 	if (!enaddr)
651 		enaddr = ac->ac_enaddr;
652 	myaddr = ia->ia_addr.sin_addr;
653 
654 	if (!bcmp((caddr_t)ea->arp_sha, enaddr, sizeof (ea->arp_sha)))
655 		goto out;	/* it's from me, ignore it. */
656 	if (ETHER_IS_MULTICAST(&ea->arp_sha[0]))
657 		if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr,
658 		    sizeof (ea->arp_sha))) {
659 			log(LOG_ERR, "arp: ether address is broadcast for "
660 			    "IP address %s!\n", inet_ntoa(isaddr));
661 			goto out;
662 		}
663 	if (myaddr.s_addr && isaddr.s_addr == myaddr.s_addr) {
664 		log(LOG_ERR,
665 		   "duplicate IP address %s sent from ethernet address %s\n",
666 		   inet_ntoa(isaddr), ether_sprintf(ea->arp_sha));
667 		itaddr = myaddr;
668 		goto reply;
669 	}
670 	la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0,
671 	    rtable_l2(m->m_pkthdr.rdomain));
672 	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
673 		if (sdl->sdl_alen) {
674 		    if (bcmp(ea->arp_sha, LLADDR(sdl), sdl->sdl_alen)) {
675 			if (rt->rt_flags & RTF_PERMANENT_ARP) {
676 				log(LOG_WARNING,
677 				   "arp: attempt to overwrite permanent "
678 				   "entry for %s by %s on %s\n",
679 				   inet_ntoa(isaddr),
680 				   ether_sprintf(ea->arp_sha),
681 				   ac->ac_if.if_xname);
682 				goto out;
683 			} else if (rt->rt_ifp != &ac->ac_if) {
684 				if (ac->ac_if.if_type != IFT_CARP)
685 					log(LOG_WARNING,
686 					   "arp: attempt to overwrite entry for"
687 					   " %s on %s by %s on %s\n",
688 					   inet_ntoa(isaddr),
689 					   rt->rt_ifp->if_xname,
690 					   ether_sprintf(ea->arp_sha),
691 					   ac->ac_if.if_xname);
692 				goto out;
693 			} else {
694 				log(LOG_INFO,
695 				   "arp info overwritten for %s by %s on %s\n",
696 				   inet_ntoa(isaddr),
697 				   ether_sprintf(ea->arp_sha),
698 				   ac->ac_if.if_xname);
699 				rt->rt_expire = 1; /* no longer static */
700 			}
701 		    }
702 		} else if (rt->rt_ifp != &ac->ac_if && !(ac->ac_if.if_bridge &&
703 		    (rt->rt_ifp->if_bridge == ac->ac_if.if_bridge)) &&
704 		    !(rt->rt_ifp->if_type == IFT_CARP &&
705 		    rt->rt_ifp->if_carpdev == &ac->ac_if) &&
706 		    !(ac->ac_if.if_type == IFT_CARP &&
707 		    ac->ac_if.if_carpdev == rt->rt_ifp)) {
708 		    log(LOG_WARNING,
709 			"arp: attempt to add entry for %s "
710 			"on %s by %s on %s\n",
711 			inet_ntoa(isaddr), rt->rt_ifp->if_xname,
712 			ether_sprintf(ea->arp_sha),
713 			ac->ac_if.if_xname);
714 		    goto out;
715 		}
716 		bcopy(ea->arp_sha, LLADDR(sdl),
717 		    sdl->sdl_alen = sizeof(ea->arp_sha));
718 		if (rt->rt_expire)
719 			rt->rt_expire = time_second + arpt_keep;
720 		rt->rt_flags &= ~RTF_REJECT;
721 		la->la_asked = 0;
722 		while ((mh = la->la_hold_head) != NULL) {
723 			if ((la->la_hold_head = mh->m_nextpkt) == NULL)
724 				la->la_hold_tail = NULL;
725 			la->la_hold_count--;
726 			la_hold_total--;
727 			mt = la->la_hold_tail;
728 
729 			(*ac->ac_if.if_output)(&ac->ac_if, mh, rt_key(rt), rt);
730 
731 			if (la->la_hold_tail == mh) {
732 				/* mbuf is back in queue. Discard. */
733 				la->la_hold_tail = mt;
734 				if (la->la_hold_tail)
735 					la->la_hold_tail->m_nextpkt = NULL;
736 				else
737 					la->la_hold_head = NULL;
738 				la->la_hold_count--;
739 				la_hold_total--;
740 				m_freem(mh);
741 			}
742 		}
743 	}
744 reply:
745 	if (op != ARPOP_REQUEST) {
746 out:
747 		m_freem(m);
748 		return;
749 	}
750 	if (itaddr.s_addr == myaddr.s_addr) {
751 		/* I am the target */
752 		bcopy(ea->arp_sha, ea->arp_tha, sizeof(ea->arp_sha));
753 		bcopy(enaddr, ea->arp_sha, sizeof(ea->arp_sha));
754 	} else {
755 		la = arplookup(itaddr.s_addr, 0, SIN_PROXY,
756 		    rtable_l2(m->m_pkthdr.rdomain));
757 		if (la == 0)
758 			goto out;
759 		rt = la->la_rt;
760 		if (rt->rt_ifp->if_type == IFT_CARP &&
761 		    m->m_pkthdr.rcvif->if_type != IFT_CARP)
762 			goto out;
763 		bcopy(ea->arp_sha, ea->arp_tha, sizeof(ea->arp_sha));
764 		sdl = SDL(rt->rt_gateway);
765 		bcopy(LLADDR(sdl), ea->arp_sha, sizeof(ea->arp_sha));
766 	}
767 
768 	bcopy(ea->arp_spa, ea->arp_tpa, sizeof(ea->arp_spa));
769 	bcopy(&itaddr, ea->arp_spa, sizeof(ea->arp_spa));
770 	ea->arp_op = htons(ARPOP_REPLY);
771 	ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */
772 	eh = (struct ether_header *)sa.sa_data;
773 	bcopy(ea->arp_tha, eh->ether_dhost, sizeof(eh->ether_dhost));
774 #if NCARP > 0
775 	if (ether_shost)
776 		enaddr = ether_shost;
777 #endif
778 	bcopy(enaddr, eh->ether_shost, sizeof(eh->ether_shost));
779 
780 	eh->ether_type = htons(ETHERTYPE_ARP);
781 	sa.sa_family = pseudo_AF_HDRCMPLT;
782 	sa.sa_len = sizeof(sa);
783 	(*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
784 	return;
785 }
786 
787 /*
788  * Free an arp entry.
789  */
790 void
791 arptfree(la)
792 	struct llinfo_arp *la;
793 {
794 	struct rtentry *rt = la->la_rt;
795 	struct sockaddr_dl *sdl;
796 	struct rt_addrinfo info;
797 	u_int tid = 0;
798 
799 	if (rt == 0)
800 		panic("arptfree");
801 	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
802 	    sdl->sdl_family == AF_LINK) {
803 		sdl->sdl_alen = 0;
804 		la->la_asked = 0;
805 		rt->rt_flags &= ~RTF_REJECT;
806 		return;
807 	}
808 	bzero(&info, sizeof(info));
809 	info.rti_info[RTAX_DST] = rt_key(rt);
810 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
811 
812 	if (rt->rt_ifp)
813 		tid = rt->rt_ifp->if_rdomain;
814 
815 	rtrequest1(RTM_DELETE, &info, rt->rt_priority, NULL, tid);
816 }
817 
818 /*
819  * Lookup or enter a new address in arptab.
820  */
821 struct llinfo_arp *
822 arplookup(addr, create, proxy, tableid)
823 	u_int32_t addr;
824 	int create, proxy;
825 	u_int tableid;
826 {
827 	struct rtentry *rt;
828 	static struct sockaddr_inarp sin;
829 
830 	sin.sin_len = sizeof(sin);
831 	sin.sin_family = AF_INET;
832 	sin.sin_addr.s_addr = addr;
833 	sin.sin_other = proxy ? SIN_PROXY : 0;
834 	rt = rtalloc1(sintosa(&sin), create, tableid);
835 	if (rt == 0)
836 		return (0);
837 	rt->rt_refcnt--;
838 	if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
839 	    rt->rt_gateway->sa_family != AF_LINK) {
840 		if (create) {
841 			if (rt->rt_refcnt <= 0 &&
842 			    (rt->rt_flags & RTF_CLONED) != 0) {
843 				struct rt_addrinfo info;
844 
845 				bzero(&info, sizeof(info));
846 				info.rti_info[RTAX_DST] = rt_key(rt);
847 				info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
848 				info.rti_info[RTAX_NETMASK] = rt_mask(rt);
849 
850 				rtrequest1(RTM_DELETE, &info, rt->rt_priority,
851 				    NULL, tableid);
852 			}
853 		}
854 		return (0);
855 	}
856 	return ((struct llinfo_arp *)rt->rt_llinfo);
857 }
858 
859 void
860 arp_ifinit(ac, ifa)
861 	struct arpcom *ac;
862 	struct ifaddr *ifa;
863 {
864 
865 	/* Warn the user if another station has this IP address. */
866 	arprequest(&ac->ac_if,
867 	    &(IA_SIN(ifa)->sin_addr.s_addr),
868 	    &(IA_SIN(ifa)->sin_addr.s_addr),
869 	    ac->ac_enaddr);
870 	ifa->ifa_rtrequest = arp_rtrequest;
871 	ifa->ifa_flags |= RTF_CLONING;
872 }
873 
874 /*
875  * Called from Ethernet interrupt handlers
876  * when ether packet type ETHERTYPE_REVARP
877  * is received.  Common length and type checks are done here,
878  * then the protocol-specific routine is called.
879  */
880 void
881 revarpinput(m)
882 	struct mbuf *m;
883 {
884 	struct arphdr *ar;
885 
886 	if (m->m_len < sizeof(struct arphdr))
887 		goto out;
888 	ar = mtod(m, struct arphdr *);
889 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
890 		goto out;
891 	if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
892 		goto out;
893 	switch (ntohs(ar->ar_pro)) {
894 
895 	case ETHERTYPE_IP:
896 	case ETHERTYPE_IPTRAILERS:
897 		in_revarpinput(m);
898 		return;
899 
900 	default:
901 		break;
902 	}
903 out:
904 	m_freem(m);
905 }
906 
907 /*
908  * RARP for Internet protocols on Ethernet.
909  * Algorithm is that given in RFC 903.
910  * We are only using for bootstrap purposes to get an ip address for one of
911  * our interfaces.  Thus we support no user-interface.
912  *
913  * Since the contents of the RARP reply are specific to the interface that
914  * sent the request, this code must ensure that they are properly associated.
915  *
916  * Note: also supports ARP via RARP packets, per the RFC.
917  */
918 void
919 in_revarpinput(m)
920 	struct mbuf *m;
921 {
922 	struct ifnet *ifp;
923 	struct ether_arp *ar;
924 	int op;
925 
926 	ar = mtod(m, struct ether_arp *);
927 	op = ntohs(ar->arp_op);
928 	switch (op) {
929 	case ARPOP_REQUEST:
930 	case ARPOP_REPLY:	/* per RFC */
931 		in_arpinput(m);
932 		return;
933 	case ARPOP_REVREPLY:
934 		break;
935 	case ARPOP_REVREQUEST:	/* handled by rarpd(8) */
936 	default:
937 		goto out;
938 	}
939 	if (!revarp_in_progress)
940 		goto out;
941 	ifp = m->m_pkthdr.rcvif;
942 	if (ifp != myip_ifp) /* !same interface */
943 		goto out;
944 	if (myip_initialized)
945 		goto wake;
946 	if (bcmp(ar->arp_tha, ((struct arpcom *)ifp)->ac_enaddr,
947 	    sizeof(ar->arp_tha)))
948 		goto out;
949 	bcopy((caddr_t)ar->arp_spa, (caddr_t)&srv_ip, sizeof(srv_ip));
950 	bcopy((caddr_t)ar->arp_tpa, (caddr_t)&myip, sizeof(myip));
951 	myip_initialized = 1;
952 wake:	/* Do wakeup every time in case it was missed. */
953 	wakeup((caddr_t)&myip);
954 
955 out:
956 	m_freem(m);
957 }
958 
959 /*
960  * Send a RARP request for the ip address of the specified interface.
961  * The request should be RFC 903-compliant.
962  */
963 void
964 revarprequest(ifp)
965 	struct ifnet *ifp;
966 {
967 	struct sockaddr sa;
968 	struct mbuf *m;
969 	struct ether_header *eh;
970 	struct ether_arp *ea;
971 	struct arpcom *ac = (struct arpcom *)ifp;
972 
973 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
974 		return;
975 	m->m_len = sizeof(*ea);
976 	m->m_pkthdr.len = sizeof(*ea);
977 	MH_ALIGN(m, sizeof(*ea));
978 	ea = mtod(m, struct ether_arp *);
979 	eh = (struct ether_header *)sa.sa_data;
980 	bzero((caddr_t)ea, sizeof(*ea));
981 	bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
982 	    sizeof(eh->ether_dhost));
983 	eh->ether_type = htons(ETHERTYPE_REVARP);
984 	ea->arp_hrd = htons(ARPHRD_ETHER);
985 	ea->arp_pro = htons(ETHERTYPE_IP);
986 	ea->arp_hln = sizeof(ea->arp_sha);	/* hardware address length */
987 	ea->arp_pln = sizeof(ea->arp_spa);	/* protocol address length */
988 	ea->arp_op = htons(ARPOP_REVREQUEST);
989 	bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost,
990 	   sizeof(ea->arp_tha));
991 	bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha,
992 	   sizeof(ea->arp_sha));
993 	bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_tha,
994 	   sizeof(ea->arp_tha));
995 	sa.sa_family = pseudo_AF_HDRCMPLT;
996 	sa.sa_len = sizeof(sa);
997 	ifp->if_output(ifp, m, &sa, (struct rtentry *)0);
998 }
999 
1000 /*
1001  * RARP for the ip address of the specified interface, but also
1002  * save the ip address of the server that sent the answer.
1003  * Timeout if no response is received.
1004  */
1005 int
1006 revarpwhoarewe(ifp, serv_in, clnt_in)
1007 	struct ifnet *ifp;
1008 	struct in_addr *serv_in;
1009 	struct in_addr *clnt_in;
1010 {
1011 	int result, count = 20;
1012 
1013 	if (myip_initialized)
1014 		return EIO;
1015 
1016 	myip_ifp = ifp;
1017 	revarp_in_progress = 1;
1018 	while (count--) {
1019 		revarprequest(ifp);
1020 		result = tsleep((caddr_t)&myip, PSOCK, "revarp", hz/2);
1021 		if (result != EWOULDBLOCK)
1022 			break;
1023 	}
1024 	revarp_in_progress = 0;
1025 	if (!myip_initialized)
1026 		return ENETUNREACH;
1027 
1028 	bcopy((caddr_t)&srv_ip, serv_in, sizeof(*serv_in));
1029 	bcopy((caddr_t)&myip, clnt_in, sizeof(*clnt_in));
1030 	return 0;
1031 }
1032 
1033 /* For compatibility: only saves interface address. */
1034 int
1035 revarpwhoami(in, ifp)
1036 	struct in_addr *in;
1037 	struct ifnet *ifp;
1038 {
1039 	struct in_addr server;
1040 	return (revarpwhoarewe(ifp, &server, in));
1041 }
1042 
1043 
1044 #ifdef DDB
1045 
1046 #include <machine/db_machdep.h>
1047 #include <ddb/db_interface.h>
1048 #include <ddb/db_output.h>
1049 
1050 void
1051 db_print_sa(sa)
1052 	struct sockaddr *sa;
1053 {
1054 	int len;
1055 	u_char *p;
1056 
1057 	if (sa == 0) {
1058 		db_printf("[NULL]");
1059 		return;
1060 	}
1061 
1062 	p = (u_char *)sa;
1063 	len = sa->sa_len;
1064 	db_printf("[");
1065 	while (len > 0) {
1066 		db_printf("%d", *p);
1067 		p++;
1068 		len--;
1069 		if (len)
1070 			db_printf(",");
1071 	}
1072 	db_printf("]\n");
1073 }
1074 
1075 void
1076 db_print_ifa(ifa)
1077 	struct ifaddr *ifa;
1078 {
1079 	if (ifa == 0)
1080 		return;
1081 	db_printf("  ifa_addr=");
1082 	db_print_sa(ifa->ifa_addr);
1083 	db_printf("  ifa_dsta=");
1084 	db_print_sa(ifa->ifa_dstaddr);
1085 	db_printf("  ifa_mask=");
1086 	db_print_sa(ifa->ifa_netmask);
1087 	db_printf("  flags=0x%x, refcnt=%d, metric=%d\n",
1088 	    ifa->ifa_flags, ifa->ifa_refcnt, ifa->ifa_metric);
1089 }
1090 
1091 void
1092 db_print_llinfo(li)
1093 	caddr_t li;
1094 {
1095 	struct llinfo_arp *la;
1096 
1097 	if (li == 0)
1098 		return;
1099 	la = (struct llinfo_arp *)li;
1100 	db_printf("  la_rt=%p la_hold_head=%p, la_asked=0x%lx\n",
1101 	    la->la_rt, la->la_hold_head, la->la_asked);
1102 }
1103 
1104 /*
1105  * Function to pass to rn_walktree().
1106  * Return non-zero error to abort walk.
1107  */
1108 int
1109 db_show_radix_node(rn, w)
1110 	struct radix_node *rn;
1111 	void *w;
1112 {
1113 	struct rtentry *rt = (struct rtentry *)rn;
1114 
1115 	db_printf("rtentry=%p", rt);
1116 
1117 	db_printf(" flags=0x%x refcnt=%d use=%ld expire=%ld\n",
1118 	    rt->rt_flags, rt->rt_refcnt, rt->rt_use, rt->rt_expire);
1119 
1120 	db_printf(" key="); db_print_sa(rt_key(rt));
1121 	db_printf(" mask="); db_print_sa(rt_mask(rt));
1122 	db_printf(" gw="); db_print_sa(rt->rt_gateway);
1123 
1124 	db_printf(" ifp=%p ", rt->rt_ifp);
1125 	if (rt->rt_ifp)
1126 		db_printf("(%s)", rt->rt_ifp->if_xname);
1127 	else
1128 		db_printf("(NULL)");
1129 
1130 	db_printf(" ifa=%p\n", rt->rt_ifa);
1131 	db_print_ifa(rt->rt_ifa);
1132 
1133 	db_printf(" genmask="); db_print_sa(rt->rt_genmask);
1134 
1135 	db_printf(" gwroute=%p llinfo=%p\n", rt->rt_gwroute, rt->rt_llinfo);
1136 	db_print_llinfo(rt->rt_llinfo);
1137 	return (0);
1138 }
1139 
1140 /*
1141  * Function to print all the route trees.
1142  * Use this from ddb:  "call db_show_arptab"
1143  */
1144 int
1145 db_show_arptab()
1146 {
1147 	struct radix_node_head *rnh;
1148 	rnh = rt_gettable(AF_INET, 0);
1149 	db_printf("Route tree for AF_INET\n");
1150 	if (rnh == NULL) {
1151 		db_printf(" (not initialized)\n");
1152 		return (0);
1153 	}
1154 	rn_walktree(rnh, db_show_radix_node, NULL);
1155 	return (0);
1156 }
1157 #endif
1158 #endif /* INET */
1159