xref: /openbsd-src/sys/netinet/if_ether.c (revision 43003dfe3ad45d1698bed8a37f2b0f5b14f20d4f)
1 /*	$OpenBSD: if_ether.c,v 1.80 2009/06/05 00:05:22 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, 1, 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 #if NBRIDGE > 0
577 	struct in_ifaddr *bridge_ia = NULL;
578 #endif
579 	struct sockaddr_dl *sdl;
580 	struct sockaddr sa;
581 	struct in_addr isaddr, itaddr, myaddr;
582 	struct mbuf *mh, *mt;
583 	u_int8_t *enaddr = NULL;
584 #if NCARP > 0
585 	u_int8_t *ether_shost = NULL;
586 #endif
587 	int op;
588 
589 	ea = mtod(m, struct ether_arp *);
590 	op = ntohs(ea->arp_op);
591 	if ((op != ARPOP_REQUEST) && (op != ARPOP_REPLY))
592 		goto out;
593 #if notyet
594 	if ((op == ARPOP_REPLY) && (m->m_flags & (M_BCAST|M_MCAST))) {
595 		log(LOG_ERR,
596 		    "arp: received reply to broadcast or multicast address\n");
597 		goto out;
598 	}
599 #endif
600 
601 	bcopy((caddr_t)ea->arp_tpa, (caddr_t)&itaddr, sizeof(itaddr));
602 	bcopy((caddr_t)ea->arp_spa, (caddr_t)&isaddr, sizeof(isaddr));
603 
604 	TAILQ_FOREACH(ia, &in_ifaddr, ia_list) {
605 		if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
606 			continue;
607 
608 #if NCARP > 0
609 		if (ia->ia_ifp->if_type == IFT_CARP &&
610 		    ((ia->ia_ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
611 		    (IFF_UP|IFF_RUNNING))) {
612 			if (ia->ia_ifp == m->m_pkthdr.rcvif &&
613 			    (op == ARPOP_REPLY  ||
614 			    carp_iamatch(ia, ea->arp_sha,
615 			    &enaddr, &ether_shost)))
616 				break;
617 		} else
618 #endif
619 			if (ia->ia_ifp == m->m_pkthdr.rcvif)
620 				break;
621 #if NBRIDGE > 0
622 		/*
623 		 * If the interface we received the packet on
624 		 * is part of a bridge, check to see if we need
625 		 * to "bridge" the packet to ourselves at this
626 		 * layer.  Note we still prefer a perfect match,
627 		 * but allow this weaker match if necessary.
628 		 */
629 		if (m->m_pkthdr.rcvif->if_bridge != NULL) {
630 			if (m->m_pkthdr.rcvif->if_bridge ==
631 			    ia->ia_ifp->if_bridge)
632 				bridge_ia = ia;
633 #if NCARP > 0
634 			else if (ia->ia_ifp->if_carpdev != NULL &&
635 			    m->m_pkthdr.rcvif->if_bridge ==
636 			    ia->ia_ifp->if_carpdev->if_bridge &&
637 			    carp_iamatch(ia, ea->arp_sha,
638 			    &enaddr, &ether_shost))
639 				bridge_ia = ia;
640 #endif
641 		}
642 #endif
643 	}
644 
645 #if NBRIDGE > 0
646 	if (ia == NULL && bridge_ia != NULL) {
647 		ia = bridge_ia;
648 		ac = (struct arpcom *)bridge_ia->ia_ifp;
649 	}
650 #endif
651 
652 	if (ia == NULL) {
653 		TAILQ_FOREACH(ia, &in_ifaddr, ia_list) {
654 			if (isaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
655 				continue;
656 			if (ia->ia_ifp == m->m_pkthdr.rcvif)
657 				break;
658 		}
659 	}
660 
661 	if (ia == NULL && m->m_pkthdr.rcvif->if_type != IFT_CARP) {
662 		struct ifaddr *ifa;
663 
664 		TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrlist, ifa_list) {
665 			if (ifa->ifa_addr->sa_family == AF_INET)
666 				break;
667 		}
668 		if (ifa)
669 			ia = (struct in_ifaddr *)ifa;
670 	}
671 
672 	if (ia == NULL)
673 		goto out;
674 
675 	if (!enaddr)
676 		enaddr = ac->ac_enaddr;
677 	myaddr = ia->ia_addr.sin_addr;
678 
679 	if (!bcmp((caddr_t)ea->arp_sha, enaddr, sizeof (ea->arp_sha)))
680 		goto out;	/* it's from me, ignore it. */
681 	if (ETHER_IS_MULTICAST (&ea->arp_sha[0]))
682 		if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr,
683 		    sizeof (ea->arp_sha))) {
684 			log(LOG_ERR, "arp: ether address is broadcast for "
685 			    "IP address %s!\n", inet_ntoa(isaddr));
686 			goto out;
687 		}
688 	if (myaddr.s_addr && isaddr.s_addr == myaddr.s_addr) {
689 		log(LOG_ERR,
690 		   "duplicate IP address %s sent from ethernet address %s\n",
691 		   inet_ntoa(isaddr), ether_sprintf(ea->arp_sha));
692 		itaddr = myaddr;
693 		goto reply;
694 	}
695 	la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0,
696 	    m->m_pkthdr.rdomain);
697 	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
698 		if (sdl->sdl_alen) {
699 		    if (bcmp(ea->arp_sha, LLADDR(sdl), sdl->sdl_alen)) {
700 			if (rt->rt_flags & RTF_PERMANENT_ARP) {
701 				log(LOG_WARNING,
702 				   "arp: attempt to overwrite permanent "
703 				   "entry for %s by %s on %s\n",
704 				   inet_ntoa(isaddr),
705 				   ether_sprintf(ea->arp_sha),
706 				   ac->ac_if.if_xname);
707 				goto out;
708 			} else if (rt->rt_ifp != &ac->ac_if) {
709 				log(LOG_WARNING,
710 				   "arp: attempt to overwrite entry for %s "
711 				   "on %s by %s on %s\n",
712 				   inet_ntoa(isaddr), rt->rt_ifp->if_xname,
713 				   ether_sprintf(ea->arp_sha),
714 				   ac->ac_if.if_xname);
715 				goto out;
716 			} else {
717 				log(LOG_INFO,
718 				   "arp info overwritten for %s by %s on %s\n",
719 				   inet_ntoa(isaddr),
720 				   ether_sprintf(ea->arp_sha),
721 				   ac->ac_if.if_xname);
722 				rt->rt_expire = 1; /* no longer static */
723 			}
724 		    }
725 		} else if (rt->rt_ifp != &ac->ac_if && !(ac->ac_if.if_bridge &&
726 		    (rt->rt_ifp->if_bridge == ac->ac_if.if_bridge)) &&
727 		    !(rt->rt_ifp->if_type == IFT_CARP &&
728 		    rt->rt_ifp->if_carpdev == &ac->ac_if) &&
729 		    !(ac->ac_if.if_type == IFT_CARP &&
730 		    ac->ac_if.if_carpdev == rt->rt_ifp)) {
731 		    log(LOG_WARNING,
732 			"arp: attempt to add entry for %s "
733 			"on %s by %s on %s\n",
734 			inet_ntoa(isaddr), rt->rt_ifp->if_xname,
735 			ether_sprintf(ea->arp_sha),
736 			ac->ac_if.if_xname);
737 		    goto out;
738 		}
739 		bcopy(ea->arp_sha, LLADDR(sdl),
740 		    sdl->sdl_alen = sizeof(ea->arp_sha));
741 		if (rt->rt_expire)
742 			rt->rt_expire = time_second + arpt_keep;
743 		rt->rt_flags &= ~RTF_REJECT;
744 		la->la_asked = 0;
745 		while ((mh = la->la_hold_head) != NULL) {
746 			if ((la->la_hold_head = mh->m_nextpkt) == NULL)
747 				la->la_hold_tail = NULL;
748 			la->la_hold_count--;
749 			la_hold_total--;
750 			mt = la->la_hold_tail;
751 
752 			(*ac->ac_if.if_output)(&ac->ac_if, mh, rt_key(rt), rt);
753 
754 			if (la->la_hold_tail == mh) {
755 				/* mbuf is back in queue. Discard. */
756 				la->la_hold_tail = mt;
757 				if (la->la_hold_tail)
758 					la->la_hold_tail->m_nextpkt = NULL;
759 				else
760 					la->la_hold_head = NULL;
761 				la->la_hold_count--;
762 				la_hold_total--;
763 				m_freem(mh);
764 			}
765 		}
766 	}
767 reply:
768 	if (op != ARPOP_REQUEST) {
769 	out:
770 		m_freem(m);
771 		return;
772 	}
773 	if (itaddr.s_addr == myaddr.s_addr) {
774 		/* I am the target */
775 		bcopy(ea->arp_sha, ea->arp_tha, sizeof(ea->arp_sha));
776 		bcopy(enaddr, ea->arp_sha, sizeof(ea->arp_sha));
777 	} else {
778 		la = arplookup(itaddr.s_addr, 0, SIN_PROXY,
779 		    m->m_pkthdr.rdomain);
780 		if (la == 0)
781 			goto out;
782 		rt = la->la_rt;
783 		if (rt->rt_ifp->if_type == IFT_CARP &&
784 		    m->m_pkthdr.rcvif->if_type != IFT_CARP)
785 			goto out;
786 		bcopy(ea->arp_sha, ea->arp_tha, sizeof(ea->arp_sha));
787 		sdl = SDL(rt->rt_gateway);
788 		bcopy(LLADDR(sdl), ea->arp_sha, sizeof(ea->arp_sha));
789 	}
790 
791 	bcopy(ea->arp_spa, ea->arp_tpa, sizeof(ea->arp_spa));
792 	bcopy(&itaddr, ea->arp_spa, sizeof(ea->arp_spa));
793 	ea->arp_op = htons(ARPOP_REPLY);
794 	ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */
795 	eh = (struct ether_header *)sa.sa_data;
796 	bcopy(ea->arp_tha, eh->ether_dhost, sizeof(eh->ether_dhost));
797 #if NCARP > 0
798 	if (ether_shost)
799 		enaddr = ether_shost;
800 #endif
801 	bcopy(enaddr, eh->ether_shost, sizeof(eh->ether_shost));
802 
803 	eh->ether_type = htons(ETHERTYPE_ARP);
804 	sa.sa_family = pseudo_AF_HDRCMPLT;
805 	sa.sa_len = sizeof(sa);
806 	(*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
807 	return;
808 }
809 
810 /*
811  * Free an arp entry.
812  */
813 void
814 arptfree(la)
815 	struct llinfo_arp *la;
816 {
817 	struct rtentry *rt = la->la_rt;
818 	struct sockaddr_dl *sdl;
819 	struct rt_addrinfo info;
820 	u_int tid = 0;
821 
822 	if (rt == 0)
823 		panic("arptfree");
824 	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
825 	    sdl->sdl_family == AF_LINK) {
826 		sdl->sdl_alen = 0;
827 		la->la_asked = 0;
828 		rt->rt_flags &= ~RTF_REJECT;
829 		return;
830 	}
831 	bzero(&info, sizeof(info));
832 	info.rti_info[RTAX_DST] = rt_key(rt);
833 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
834 
835 	if (rt->rt_ifp)
836 		tid = rt->rt_ifp->if_rdomain;
837 
838 	rtrequest1(RTM_DELETE, &info, rt->rt_priority, NULL, tid);
839 }
840 
841 /*
842  * Lookup or enter a new address in arptab.
843  */
844 struct llinfo_arp *
845 arplookup(addr, create, proxy, tableid)
846 	u_int32_t addr;
847 	int create, proxy;
848 	u_int tableid;
849 {
850 	struct rtentry *rt;
851 	static struct sockaddr_inarp sin;
852 
853 	sin.sin_len = sizeof(sin);
854 	sin.sin_family = AF_INET;
855 	sin.sin_addr.s_addr = addr;
856 	sin.sin_other = proxy ? SIN_PROXY : 0;
857 	rt = rtalloc1(sintosa(&sin), create, tableid);
858 	if (rt == 0)
859 		return (0);
860 	rt->rt_refcnt--;
861 	if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
862 	    rt->rt_gateway->sa_family != AF_LINK) {
863 		if (create) {
864 			if (rt->rt_refcnt <= 0 &&
865 			    (rt->rt_flags & RTF_CLONED) != 0) {
866 				struct rt_addrinfo info;
867 
868 				bzero(&info, sizeof(info));
869 				info.rti_info[RTAX_DST] = rt_key(rt);
870 				info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
871 				info.rti_info[RTAX_NETMASK] = rt_mask(rt);
872 
873 				rtrequest1(RTM_DELETE, &info, rt->rt_priority,
874 				    NULL, tableid);
875 			}
876 		}
877 		return (0);
878 	}
879 	return ((struct llinfo_arp *)rt->rt_llinfo);
880 }
881 
882 int
883 arpioctl(cmd, data)
884 	u_long cmd;
885 	caddr_t data;
886 {
887 
888 	return (EOPNOTSUPP);
889 }
890 
891 void
892 arp_ifinit(ac, ifa)
893 	struct arpcom *ac;
894 	struct ifaddr *ifa;
895 {
896 
897 	/* Warn the user if another station has this IP address. */
898 	arprequest(&ac->ac_if,
899 	    &(IA_SIN(ifa)->sin_addr.s_addr),
900 	    &(IA_SIN(ifa)->sin_addr.s_addr),
901 	    ac->ac_enaddr);
902 	ifa->ifa_rtrequest = arp_rtrequest;
903 	ifa->ifa_flags |= RTF_CLONING;
904 }
905 
906 /*
907  * Called from Ethernet interrupt handlers
908  * when ether packet type ETHERTYPE_REVARP
909  * is received.  Common length and type checks are done here,
910  * then the protocol-specific routine is called.
911  */
912 void
913 revarpinput(m)
914 	struct mbuf *m;
915 {
916 	struct arphdr *ar;
917 
918 	if (m->m_len < sizeof(struct arphdr))
919 		goto out;
920 	ar = mtod(m, struct arphdr *);
921 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
922 		goto out;
923 	if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
924 		goto out;
925 	switch (ntohs(ar->ar_pro)) {
926 
927 	case ETHERTYPE_IP:
928 	case ETHERTYPE_IPTRAILERS:
929 		in_revarpinput(m);
930 		return;
931 
932 	default:
933 		break;
934 	}
935 out:
936 	m_freem(m);
937 }
938 
939 /*
940  * RARP for Internet protocols on Ethernet.
941  * Algorithm is that given in RFC 903.
942  * We are only using for bootstrap purposes to get an ip address for one of
943  * our interfaces.  Thus we support no user-interface.
944  *
945  * Since the contents of the RARP reply are specific to the interface that
946  * sent the request, this code must ensure that they are properly associated.
947  *
948  * Note: also supports ARP via RARP packets, per the RFC.
949  */
950 void
951 in_revarpinput(m)
952 	struct mbuf *m;
953 {
954 	struct ifnet *ifp;
955 	struct ether_arp *ar;
956 	int op;
957 
958 	ar = mtod(m, struct ether_arp *);
959 	op = ntohs(ar->arp_op);
960 	switch (op) {
961 	case ARPOP_REQUEST:
962 	case ARPOP_REPLY:	/* per RFC */
963 		in_arpinput(m);
964 		return;
965 	case ARPOP_REVREPLY:
966 		break;
967 	case ARPOP_REVREQUEST:	/* handled by rarpd(8) */
968 	default:
969 		goto out;
970 	}
971 	if (!revarp_in_progress)
972 		goto out;
973 	ifp = m->m_pkthdr.rcvif;
974 	if (ifp != myip_ifp) /* !same interface */
975 		goto out;
976 	if (myip_initialized)
977 		goto wake;
978 	if (bcmp(ar->arp_tha, ((struct arpcom *)ifp)->ac_enaddr,
979 	    sizeof(ar->arp_tha)))
980 		goto out;
981 	bcopy((caddr_t)ar->arp_spa, (caddr_t)&srv_ip, sizeof(srv_ip));
982 	bcopy((caddr_t)ar->arp_tpa, (caddr_t)&myip, sizeof(myip));
983 	myip_initialized = 1;
984 wake:	/* Do wakeup every time in case it was missed. */
985 	wakeup((caddr_t)&myip);
986 
987 out:
988 	m_freem(m);
989 }
990 
991 /*
992  * Send a RARP request for the ip address of the specified interface.
993  * The request should be RFC 903-compliant.
994  */
995 void
996 revarprequest(ifp)
997 	struct ifnet *ifp;
998 {
999 	struct sockaddr sa;
1000 	struct mbuf *m;
1001 	struct ether_header *eh;
1002 	struct ether_arp *ea;
1003 	struct arpcom *ac = (struct arpcom *)ifp;
1004 
1005 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
1006 		return;
1007 	m->m_len = sizeof(*ea);
1008 	m->m_pkthdr.len = sizeof(*ea);
1009 	MH_ALIGN(m, sizeof(*ea));
1010 	ea = mtod(m, struct ether_arp *);
1011 	eh = (struct ether_header *)sa.sa_data;
1012 	bzero((caddr_t)ea, sizeof(*ea));
1013 	bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
1014 	    sizeof(eh->ether_dhost));
1015 	eh->ether_type = htons(ETHERTYPE_REVARP);
1016 	ea->arp_hrd = htons(ARPHRD_ETHER);
1017 	ea->arp_pro = htons(ETHERTYPE_IP);
1018 	ea->arp_hln = sizeof(ea->arp_sha);	/* hardware address length */
1019 	ea->arp_pln = sizeof(ea->arp_spa);	/* protocol address length */
1020 	ea->arp_op = htons(ARPOP_REVREQUEST);
1021 	bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost,
1022 	   sizeof(ea->arp_tha));
1023 	bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha,
1024 	   sizeof(ea->arp_sha));
1025 	bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_tha,
1026 	   sizeof(ea->arp_tha));
1027 	sa.sa_family = pseudo_AF_HDRCMPLT;
1028 	sa.sa_len = sizeof(sa);
1029 	ifp->if_output(ifp, m, &sa, (struct rtentry *)0);
1030 }
1031 
1032 /*
1033  * RARP for the ip address of the specified interface, but also
1034  * save the ip address of the server that sent the answer.
1035  * Timeout if no response is received.
1036  */
1037 int
1038 revarpwhoarewe(ifp, serv_in, clnt_in)
1039 	struct ifnet *ifp;
1040 	struct in_addr *serv_in;
1041 	struct in_addr *clnt_in;
1042 {
1043 	int result, count = 20;
1044 
1045 	if (myip_initialized)
1046 		return EIO;
1047 
1048 	myip_ifp = ifp;
1049 	revarp_in_progress = 1;
1050 	while (count--) {
1051 		revarprequest(ifp);
1052 		result = tsleep((caddr_t)&myip, PSOCK, "revarp", hz/2);
1053 		if (result != EWOULDBLOCK)
1054 			break;
1055 	}
1056 	revarp_in_progress = 0;
1057 	if (!myip_initialized)
1058 		return ENETUNREACH;
1059 
1060 	bcopy((caddr_t)&srv_ip, serv_in, sizeof(*serv_in));
1061 	bcopy((caddr_t)&myip, clnt_in, sizeof(*clnt_in));
1062 	return 0;
1063 }
1064 
1065 /* For compatibility: only saves interface address. */
1066 int
1067 revarpwhoami(in, ifp)
1068 	struct in_addr *in;
1069 	struct ifnet *ifp;
1070 {
1071 	struct in_addr server;
1072 	return (revarpwhoarewe(ifp, &server, in));
1073 }
1074 
1075 
1076 #ifdef DDB
1077 
1078 #include <machine/db_machdep.h>
1079 #include <ddb/db_interface.h>
1080 #include <ddb/db_output.h>
1081 
1082 void
1083 db_print_sa(sa)
1084 	struct sockaddr *sa;
1085 {
1086 	int len;
1087 	u_char *p;
1088 
1089 	if (sa == 0) {
1090 		db_printf("[NULL]");
1091 		return;
1092 	}
1093 
1094 	p = (u_char *)sa;
1095 	len = sa->sa_len;
1096 	db_printf("[");
1097 	while (len > 0) {
1098 		db_printf("%d", *p);
1099 		p++;
1100 		len--;
1101 		if (len)
1102 			db_printf(",");
1103 	}
1104 	db_printf("]\n");
1105 }
1106 
1107 void
1108 db_print_ifa(ifa)
1109 	struct ifaddr *ifa;
1110 {
1111 	if (ifa == 0)
1112 		return;
1113 	db_printf("  ifa_addr=");
1114 	db_print_sa(ifa->ifa_addr);
1115 	db_printf("  ifa_dsta=");
1116 	db_print_sa(ifa->ifa_dstaddr);
1117 	db_printf("  ifa_mask=");
1118 	db_print_sa(ifa->ifa_netmask);
1119 	db_printf("  flags=0x%x, refcnt=%d, metric=%d\n",
1120 	    ifa->ifa_flags, ifa->ifa_refcnt, ifa->ifa_metric);
1121 }
1122 
1123 void
1124 db_print_llinfo(li)
1125 	caddr_t li;
1126 {
1127 	struct llinfo_arp *la;
1128 
1129 	if (li == 0)
1130 		return;
1131 	la = (struct llinfo_arp *)li;
1132 	db_printf("  la_rt=%p la_hold_head=%p, la_asked=0x%lx\n",
1133 	    la->la_rt, la->la_hold_head, la->la_asked);
1134 }
1135 
1136 /*
1137  * Function to pass to rn_walktree().
1138  * Return non-zero error to abort walk.
1139  */
1140 int
1141 db_show_radix_node(rn, w)
1142 	struct radix_node *rn;
1143 	void *w;
1144 {
1145 	struct rtentry *rt = (struct rtentry *)rn;
1146 
1147 	db_printf("rtentry=%p", rt);
1148 
1149 	db_printf(" flags=0x%x refcnt=%d use=%ld expire=%ld\n",
1150 	    rt->rt_flags, rt->rt_refcnt, rt->rt_use, rt->rt_expire);
1151 
1152 	db_printf(" key="); db_print_sa(rt_key(rt));
1153 	db_printf(" mask="); db_print_sa(rt_mask(rt));
1154 	db_printf(" gw="); db_print_sa(rt->rt_gateway);
1155 
1156 	db_printf(" ifp=%p ", rt->rt_ifp);
1157 	if (rt->rt_ifp)
1158 		db_printf("(%s)", rt->rt_ifp->if_xname);
1159 	else
1160 		db_printf("(NULL)");
1161 
1162 	db_printf(" ifa=%p\n", rt->rt_ifa);
1163 	db_print_ifa(rt->rt_ifa);
1164 
1165 	db_printf(" genmask="); db_print_sa(rt->rt_genmask);
1166 
1167 	db_printf(" gwroute=%p llinfo=%p\n", rt->rt_gwroute, rt->rt_llinfo);
1168 	db_print_llinfo(rt->rt_llinfo);
1169 	return (0);
1170 }
1171 
1172 /*
1173  * Function to print all the route trees.
1174  * Use this from ddb:  "call db_show_arptab"
1175  */
1176 int
1177 db_show_arptab()
1178 {
1179 	struct radix_node_head *rnh;
1180 	rnh = rt_gettable(AF_INET, 0);
1181 	db_printf("Route tree for AF_INET\n");
1182 	if (rnh == NULL) {
1183 		db_printf(" (not initialized)\n");
1184 		return (0);
1185 	}
1186 	rn_walktree(rnh, db_show_radix_node, NULL);
1187 	return (0);
1188 }
1189 #endif
1190 #endif /* INET */
1191