xref: /openbsd-src/sys/netinet/if_ether.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: if_ether.c,v 1.79 2008/12/24 08:26:27 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);
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, 0);
204 			gate = rt->rt_gateway;
205 			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
206 			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
207 			/*
208 			 * Give this route an expiration time, even though
209 			 * it's a "permanent" route, so that routes cloned
210 			 * from it do not need their expiration time set.
211 			 */
212 			rt->rt_expire = time_second;
213 			/*
214 			 * linklayers with particular link MTU limitation.
215 			 */
216 			switch (rt->rt_ifp->if_type) {
217 			case IFT_FDDI:
218 				if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 &&
219 				    (rt->rt_rmx.rmx_mtu > FDDIIPMTU ||
220 				     (rt->rt_rmx.rmx_mtu == 0 &&
221 				      rt->rt_ifp->if_mtu > FDDIIPMTU)))
222 					rt->rt_rmx.rmx_mtu = FDDIIPMTU;
223 				break;
224 			}
225 			break;
226 		}
227 		/* Announce a new entry if requested. */
228 		if (rt->rt_flags & RTF_ANNOUNCE)
229 			arprequest(rt->rt_ifp,
230 			    &SIN(rt_key(rt))->sin_addr.s_addr,
231 			    &SIN(rt_key(rt))->sin_addr.s_addr,
232 			    (u_char *)LLADDR(SDL(gate)));
233 		/*FALLTHROUGH*/
234 	case RTM_RESOLVE:
235 		if (gate->sa_family != AF_LINK ||
236 		    gate->sa_len < sizeof(null_sdl)) {
237 			log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
238 			break;
239 		}
240 		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
241 		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
242 		if (la != 0)
243 			break; /* This happens on a route change */
244 		/*
245 		 * Case 2:  This route may come from cloning, or a manual route
246 		 * add with a LL address.
247 		 */
248 		R_Malloc(la, struct llinfo_arp *, sizeof(*la));
249 		rt->rt_llinfo = (caddr_t)la;
250 		if (la == 0) {
251 			log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
252 			break;
253 		}
254 		arp_inuse++, arp_allocated++;
255 		Bzero(la, sizeof(*la));
256 		la->la_rt = rt;
257 		rt->rt_flags |= RTF_LLINFO;
258 		LIST_INSERT_HEAD(&llinfo_arp, la, la_list);
259 
260 		TAILQ_FOREACH(ia, &in_ifaddr, ia_list) {
261 			if (ia->ia_ifp == rt->rt_ifp &&
262 			    SIN(rt_key(rt))->sin_addr.s_addr ==
263 			    (IA_SIN(ia))->sin_addr.s_addr)
264 				break;
265 		}
266 		if (ia) {
267 			/*
268 			 * This test used to be
269 			 *	if (lo0ifp->if_flags & IFF_UP)
270 			 * It allowed local traffic to be forced through
271 			 * the hardware by configuring the loopback down.
272 			 * However, it causes problems during network
273 			 * configuration for boards that can't receive
274 			 * packets they send.  It is now necessary to clear
275 			 * "useloopback" and remove the route to force
276 			 * traffic out to the hardware.
277 			 *
278 			 * In 4.4BSD, the above "if" statement checked
279 			 * rt->rt_ifa against rt_key(rt).  It was changed
280 			 * to the current form so that we can provide a
281 			 * better support for multiple IPv4 addresses on a
282 			 * interface.
283 			 */
284 			rt->rt_expire = 0;
285 			Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr,
286 			    LLADDR(SDL(gate)),
287 			    SDL(gate)->sdl_alen = ETHER_ADDR_LEN);
288 			if (useloopback)
289 				rt->rt_ifp = lo0ifp;
290 			/*
291 			 * make sure to set rt->rt_ifa to the interface
292 			 * address we are using, otherwise we will have trouble
293 			 * with source address selection.
294 			 */
295 			ifa = &ia->ia_ifa;
296 			if (ifa != rt->rt_ifa) {
297 				IFAFREE(rt->rt_ifa);
298 				ifa->ifa_refcnt++;
299 				rt->rt_ifa = ifa;
300 			}
301 		}
302 		break;
303 
304 	case RTM_DELETE:
305 		if (la == 0)
306 			break;
307 		arp_inuse--;
308 		LIST_REMOVE(la, la_list);
309 		rt->rt_llinfo = 0;
310 		rt->rt_flags &= ~RTF_LLINFO;
311 		while ((m = la->la_hold_head) != NULL) {
312 			la->la_hold_head = la->la_hold_head->m_nextpkt;
313 			la_hold_total--;
314 			m_freem(m);
315 		}
316 		Free((caddr_t)la);
317 	}
318 }
319 
320 /*
321  * Broadcast an ARP request. Caller specifies:
322  *	- arp header source ip address
323  *	- arp header target ip address
324  *	- arp header source ethernet address
325  */
326 void
327 arprequest(ifp, sip, tip, enaddr)
328 	struct ifnet *ifp;
329 	u_int32_t *sip, *tip;
330 	u_int8_t *enaddr;
331 {
332 	struct mbuf *m;
333 	struct ether_header *eh;
334 	struct ether_arp *ea;
335 	struct sockaddr sa;
336 
337 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
338 		return;
339 	m->m_len = sizeof(*ea);
340 	m->m_pkthdr.len = sizeof(*ea);
341 	MH_ALIGN(m, sizeof(*ea));
342 	ea = mtod(m, struct ether_arp *);
343 	eh = (struct ether_header *)sa.sa_data;
344 	bzero((caddr_t)ea, sizeof (*ea));
345 	bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
346 	    sizeof(eh->ether_dhost));
347 	eh->ether_type = htons(ETHERTYPE_ARP);	/* if_output will not swap */
348 	ea->arp_hrd = htons(ARPHRD_ETHER);
349 	ea->arp_pro = htons(ETHERTYPE_IP);
350 	ea->arp_hln = sizeof(ea->arp_sha);	/* hardware address length */
351 	ea->arp_pln = sizeof(ea->arp_spa);	/* protocol address length */
352 	ea->arp_op = htons(ARPOP_REQUEST);
353 	bcopy((caddr_t)enaddr, (caddr_t)eh->ether_shost,
354 	      sizeof(eh->ether_shost));
355 	bcopy((caddr_t)enaddr, (caddr_t)ea->arp_sha, sizeof(ea->arp_sha));
356 	bcopy((caddr_t)sip, (caddr_t)ea->arp_spa, sizeof(ea->arp_spa));
357 	bcopy((caddr_t)tip, (caddr_t)ea->arp_tpa, sizeof(ea->arp_tpa));
358 	sa.sa_family = pseudo_AF_HDRCMPLT;
359 	sa.sa_len = sizeof(sa);
360 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
361 }
362 
363 /*
364  * Resolve an IP address into an ethernet address.  If success,
365  * desten is filled in.  If there is no entry in arptab,
366  * set one up and broadcast a request for the IP address.
367  * Hold onto this mbuf and resend it once the address
368  * is finally resolved.  A return value of 1 indicates
369  * that desten has been filled in and the packet should be sent
370  * normally; a 0 return indicates that the packet has been
371  * taken over here, either now or for later transmission.
372  */
373 int
374 arpresolve(ac, rt, m, dst, desten)
375 	struct arpcom *ac;
376 	struct rtentry *rt;
377 	struct mbuf *m;
378 	struct sockaddr *dst;
379 	u_char *desten;
380 {
381 	struct llinfo_arp *la;
382 	struct sockaddr_dl *sdl;
383 	struct mbuf *mh;
384 
385 	if (m->m_flags & M_BCAST) {	/* broadcast */
386 		bcopy((caddr_t)etherbroadcastaddr, (caddr_t)desten,
387 		    sizeof(etherbroadcastaddr));
388 		return (1);
389 	}
390 	if (m->m_flags & M_MCAST) {	/* multicast */
391 		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
392 		return (1);
393 	}
394 	if (rt) {
395 		la = (struct llinfo_arp *)rt->rt_llinfo;
396 		if (la == NULL)
397 			log(LOG_DEBUG, "arpresolve: %s: route without link "
398 			    "local address\n", inet_ntoa(SIN(dst)->sin_addr));
399 	} else {
400 		if ((la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0)) != NULL)
401 			rt = la->la_rt;
402 		else
403 			log(LOG_DEBUG,
404 			    "arpresolve: %s: can't allocate llinfo\n",
405 			    inet_ntoa(SIN(dst)->sin_addr));
406 	}
407 	if (la == 0 || rt == 0) {
408 		m_freem(m);
409 		return (0);
410 	}
411 	sdl = SDL(rt->rt_gateway);
412 	/*
413 	 * Check the address family and length is valid, the address
414 	 * is resolved; otherwise, try to resolve.
415 	 */
416 	if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
417 	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
418 		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
419 		return 1;
420 	}
421 	if (((struct ifnet *)ac)->if_flags & IFF_NOARP) {
422 		m_freem(m);
423 		return 0;
424 	}
425 
426 	/*
427 	 * There is an arptab entry, but no ethernet address
428 	 * response yet. Insert mbuf in hold queue if below limit
429 	 * if above the limit free the queue without queuing the new packet.
430 	 */
431 	if (la_hold_total < MAX_HOLD_TOTAL && la_hold_total < nmbclust / 64) {
432 		if (la->la_hold_count >= MAX_HOLD_QUEUE) {
433 			mh = la->la_hold_head;
434 			la->la_hold_head = la->la_hold_head->m_nextpkt;
435 			if (mh == la->la_hold_tail)
436 				la->la_hold_tail = NULL;
437 			la->la_hold_count--;
438 			la_hold_total--;
439 			m_freem(mh);
440 		}
441 		if (la->la_hold_tail == NULL)
442 			la->la_hold_head = m;
443 		else
444 			la->la_hold_tail->m_nextpkt = m;
445 		la->la_hold_tail = m;
446 		la->la_hold_count++;
447 		la_hold_total++;
448 	} else {
449 		while ((mh = la->la_hold_head) != NULL) {
450 			la->la_hold_head =
451 			    la->la_hold_head->m_nextpkt;
452 			la_hold_total--;
453 			m_freem(mh);
454 		}
455 		la->la_hold_tail = NULL;
456 		la->la_hold_count = 0;
457 		m_freem(m);
458 	}
459 
460 	/*
461 	 * Re-send the ARP request when appropriate.
462 	 */
463 #ifdef	DIAGNOSTIC
464 	if (rt->rt_expire == 0) {
465 		/* This should never happen. (Should it? -gwr) */
466 		printf("arpresolve: unresolved and rt_expire == 0\n");
467 		/* Set expiration time to now (expired). */
468 		rt->rt_expire = time_second;
469 	}
470 #endif
471 	if (rt->rt_expire) {
472 		rt->rt_flags &= ~RTF_REJECT;
473 		if (la->la_asked == 0 || rt->rt_expire != time_second) {
474 			rt->rt_expire = time_second;
475 			if (la->la_asked++ < arp_maxtries)
476 				arprequest(&ac->ac_if,
477 				    &(SIN(rt->rt_ifa->ifa_addr)->sin_addr.s_addr),
478 				    &(SIN(dst)->sin_addr.s_addr),
479 #if NCARP > 0
480 				    (rt->rt_ifp->if_type == IFT_CARP) ?
481 					((struct arpcom *) rt->rt_ifp->if_softc
482 					)->ac_enaddr :
483 #endif
484 				    ac->ac_enaddr);
485 			else {
486 				rt->rt_flags |= RTF_REJECT;
487 				rt->rt_expire += arpt_down;
488 				la->la_asked = 0;
489 				while ((mh = la->la_hold_head) != NULL) {
490 					la->la_hold_head =
491 					    la->la_hold_head->m_nextpkt;
492 					la_hold_total--;
493 					m_freem(mh);
494 				}
495 				la->la_hold_tail = NULL;
496 				la->la_hold_count = 0;
497 			}
498 		}
499 	}
500 	return (0);
501 }
502 
503 /*
504  * Common length and type checks are done here,
505  * then the protocol-specific routine is called.
506  */
507 void
508 arpintr()
509 {
510 	struct mbuf *m;
511 	struct arphdr *ar;
512 	int s, len;
513 
514 	for (;;) {
515 		s = splnet();
516 		IF_DEQUEUE(&arpintrq, m);
517 		splx(s);
518 		if (m == NULL)
519 			break;
520 #ifdef DIAGNOSTIC
521 		if ((m->m_flags & M_PKTHDR) == 0)
522 			panic("arpintr");
523 #endif
524 
525 		len = sizeof(struct arphdr);
526 		if (m->m_len < len && (m = m_pullup(m, len)) == NULL)
527 			continue;
528 
529 		ar = mtod(m, struct arphdr *);
530 		if (ntohs(ar->ar_hrd) != ARPHRD_ETHER) {
531 			m_freem(m);
532 			continue;
533 		}
534 
535 		len += 2 * (ar->ar_hln + ar->ar_pln);
536 		if (m->m_len < len && (m = m_pullup(m, len)) == NULL)
537 			continue;
538 
539 		switch (ntohs(ar->ar_pro)) {
540 		case ETHERTYPE_IP:
541 		case ETHERTYPE_IPTRAILERS:
542 			in_arpinput(m);
543 			continue;
544 		}
545 		m_freem(m);
546 	}
547 }
548 
549 /*
550  * ARP for Internet protocols on Ethernet.
551  * Algorithm is that given in RFC 826.
552  * In addition, a sanity check is performed on the sender
553  * protocol address, to catch impersonators.
554  * We no longer handle negotiations for use of trailer protocol:
555  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
556  * along with IP replies if we wanted trailers sent to us,
557  * and also sent them in response to IP replies.
558  * This allowed either end to announce the desire to receive
559  * trailer packets.
560  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
561  * but formerly didn't normally send requests.
562  */
563 void
564 in_arpinput(m)
565 	struct mbuf *m;
566 {
567 	struct ether_arp *ea;
568 	struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif;
569 	struct ether_header *eh;
570 	struct llinfo_arp *la = 0;
571 	struct rtentry *rt;
572 	struct in_ifaddr *ia;
573 #if NBRIDGE > 0
574 	struct in_ifaddr *bridge_ia = NULL;
575 #endif
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 	TAILQ_FOREACH(ia, &in_ifaddr, ia_list) {
602 		if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
603 			continue;
604 
605 #if NCARP > 0
606 		if (ia->ia_ifp->if_type == IFT_CARP &&
607 		    ((ia->ia_ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
608 		    (IFF_UP|IFF_RUNNING))) {
609 			if (ia->ia_ifp == m->m_pkthdr.rcvif &&
610 			    (op == ARPOP_REPLY  ||
611 			    carp_iamatch(ia, ea->arp_sha,
612 			    &enaddr, &ether_shost)))
613 				break;
614 		} else
615 #endif
616 			if (ia->ia_ifp == m->m_pkthdr.rcvif)
617 				break;
618 #if NBRIDGE > 0
619 		/*
620 		 * If the interface we received the packet on
621 		 * is part of a bridge, check to see if we need
622 		 * to "bridge" the packet to ourselves at this
623 		 * layer.  Note we still prefer a perfect match,
624 		 * but allow this weaker match if necessary.
625 		 */
626 		if (m->m_pkthdr.rcvif->if_bridge != NULL) {
627 			if (m->m_pkthdr.rcvif->if_bridge ==
628 			    ia->ia_ifp->if_bridge)
629 				bridge_ia = ia;
630 #if NCARP > 0
631 			else if (ia->ia_ifp->if_carpdev != NULL &&
632 			    m->m_pkthdr.rcvif->if_bridge ==
633 			    ia->ia_ifp->if_carpdev->if_bridge &&
634 			    carp_iamatch(ia, ea->arp_sha,
635 			    &enaddr, &ether_shost))
636 				bridge_ia = ia;
637 #endif
638 		}
639 #endif
640 	}
641 
642 #if NBRIDGE > 0
643 	if (ia == NULL && bridge_ia != NULL) {
644 		ia = bridge_ia;
645 		ac = (struct arpcom *)bridge_ia->ia_ifp;
646 	}
647 #endif
648 
649 	if (ia == NULL) {
650 		TAILQ_FOREACH(ia, &in_ifaddr, ia_list) {
651 			if (isaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
652 				continue;
653 			if (ia->ia_ifp == m->m_pkthdr.rcvif)
654 				break;
655 		}
656 	}
657 
658 	if (ia == NULL && m->m_pkthdr.rcvif->if_type != IFT_CARP) {
659 		struct ifaddr *ifa;
660 
661 		TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrlist, ifa_list) {
662 			if (ifa->ifa_addr->sa_family == AF_INET)
663 				break;
664 		}
665 		if (ifa)
666 			ia = (struct in_ifaddr *)ifa;
667 	}
668 
669 	if (ia == NULL)
670 		goto out;
671 
672 	if (!enaddr)
673 		enaddr = ac->ac_enaddr;
674 	myaddr = ia->ia_addr.sin_addr;
675 
676 	if (!bcmp((caddr_t)ea->arp_sha, enaddr, sizeof (ea->arp_sha)))
677 		goto out;	/* it's from me, ignore it. */
678 	if (ETHER_IS_MULTICAST (&ea->arp_sha[0]))
679 		if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr,
680 		    sizeof (ea->arp_sha))) {
681 			log(LOG_ERR, "arp: ether address is broadcast for "
682 			    "IP address %s!\n", inet_ntoa(isaddr));
683 			goto out;
684 		}
685 	if (myaddr.s_addr && isaddr.s_addr == myaddr.s_addr) {
686 		log(LOG_ERR,
687 		   "duplicate IP address %s sent from ethernet address %s\n",
688 		   inet_ntoa(isaddr), ether_sprintf(ea->arp_sha));
689 		itaddr = myaddr;
690 		goto reply;
691 	}
692 	la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
693 	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
694 		if (sdl->sdl_alen) {
695 		    if (bcmp(ea->arp_sha, LLADDR(sdl), sdl->sdl_alen)) {
696 			if (rt->rt_flags & RTF_PERMANENT_ARP) {
697 				log(LOG_WARNING,
698 				   "arp: attempt to overwrite permanent "
699 				   "entry for %s by %s on %s\n",
700 				   inet_ntoa(isaddr),
701 				   ether_sprintf(ea->arp_sha),
702 				   ac->ac_if.if_xname);
703 				goto out;
704 			} else if (rt->rt_ifp != &ac->ac_if) {
705 				log(LOG_WARNING,
706 				   "arp: attempt to overwrite entry for %s "
707 				   "on %s by %s on %s\n",
708 				   inet_ntoa(isaddr), rt->rt_ifp->if_xname,
709 				   ether_sprintf(ea->arp_sha),
710 				   ac->ac_if.if_xname);
711 				goto out;
712 			} else {
713 				log(LOG_INFO,
714 				   "arp info overwritten for %s by %s on %s\n",
715 				   inet_ntoa(isaddr),
716 				   ether_sprintf(ea->arp_sha),
717 				   ac->ac_if.if_xname);
718 				rt->rt_expire = 1; /* no longer static */
719 			}
720 		    }
721 		} else if (rt->rt_ifp != &ac->ac_if && !(ac->ac_if.if_bridge &&
722 		    (rt->rt_ifp->if_bridge == ac->ac_if.if_bridge)) &&
723 		    !(rt->rt_ifp->if_type == IFT_CARP &&
724 		    rt->rt_ifp->if_carpdev == &ac->ac_if) &&
725 		    !(ac->ac_if.if_type == IFT_CARP &&
726 		    ac->ac_if.if_carpdev == rt->rt_ifp)) {
727 		    log(LOG_WARNING,
728 			"arp: attempt to add entry for %s "
729 			"on %s by %s on %s\n",
730 			inet_ntoa(isaddr), rt->rt_ifp->if_xname,
731 			ether_sprintf(ea->arp_sha),
732 			ac->ac_if.if_xname);
733 		    goto out;
734 		}
735 		bcopy(ea->arp_sha, LLADDR(sdl),
736 		    sdl->sdl_alen = sizeof(ea->arp_sha));
737 		if (rt->rt_expire)
738 			rt->rt_expire = time_second + arpt_keep;
739 		rt->rt_flags &= ~RTF_REJECT;
740 		la->la_asked = 0;
741 		while ((mh = la->la_hold_head) != NULL) {
742 			if ((la->la_hold_head = mh->m_nextpkt) == NULL)
743 				la->la_hold_tail = NULL;
744 			la->la_hold_count--;
745 			la_hold_total--;
746 			mt = la->la_hold_tail;
747 
748 			(*ac->ac_if.if_output)(&ac->ac_if, mh, rt_key(rt), rt);
749 
750 			if (la->la_hold_tail == mh) {
751 				/* mbuf is back in queue. Discard. */
752 				la->la_hold_tail = mt;
753 				if (la->la_hold_tail)
754 					la->la_hold_tail->m_nextpkt = NULL;
755 				else
756 					la->la_hold_head = NULL;
757 				la->la_hold_count--;
758 				la_hold_total--;
759 				m_freem(mh);
760 			}
761 		}
762 	}
763 reply:
764 	if (op != ARPOP_REQUEST) {
765 	out:
766 		m_freem(m);
767 		return;
768 	}
769 	if (itaddr.s_addr == myaddr.s_addr) {
770 		/* I am the target */
771 		bcopy(ea->arp_sha, ea->arp_tha, sizeof(ea->arp_sha));
772 		bcopy(enaddr, ea->arp_sha, sizeof(ea->arp_sha));
773 	} else {
774 		la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
775 		if (la == 0)
776 			goto out;
777 		rt = la->la_rt;
778 		if (rt->rt_ifp->if_type == IFT_CARP &&
779 		    m->m_pkthdr.rcvif->if_type != IFT_CARP)
780 			goto out;
781 		bcopy(ea->arp_sha, ea->arp_tha, sizeof(ea->arp_sha));
782 		sdl = SDL(rt->rt_gateway);
783 		bcopy(LLADDR(sdl), ea->arp_sha, sizeof(ea->arp_sha));
784 	}
785 
786 	bcopy(ea->arp_spa, ea->arp_tpa, sizeof(ea->arp_spa));
787 	bcopy(&itaddr, ea->arp_spa, sizeof(ea->arp_spa));
788 	ea->arp_op = htons(ARPOP_REPLY);
789 	ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */
790 	eh = (struct ether_header *)sa.sa_data;
791 	bcopy(ea->arp_tha, eh->ether_dhost, sizeof(eh->ether_dhost));
792 #if NCARP > 0
793 	if (ether_shost)
794 		enaddr = ether_shost;
795 #endif
796 	bcopy(enaddr, eh->ether_shost, sizeof(eh->ether_shost));
797 
798 	eh->ether_type = htons(ETHERTYPE_ARP);
799 	sa.sa_family = pseudo_AF_HDRCMPLT;
800 	sa.sa_len = sizeof(sa);
801 	(*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
802 	return;
803 }
804 
805 /*
806  * Free an arp entry.
807  */
808 void
809 arptfree(la)
810 	struct llinfo_arp *la;
811 {
812 	struct rtentry *rt = la->la_rt;
813 	struct sockaddr_dl *sdl;
814 	struct rt_addrinfo info;
815 
816 	if (rt == 0)
817 		panic("arptfree");
818 	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
819 	    sdl->sdl_family == AF_LINK) {
820 		sdl->sdl_alen = 0;
821 		la->la_asked = 0;
822 		rt->rt_flags &= ~RTF_REJECT;
823 		return;
824 	}
825 	bzero(&info, sizeof(info));
826 	info.rti_info[RTAX_DST] = rt_key(rt);
827 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
828 
829 	rtrequest1(RTM_DELETE, &info, rt->rt_priority, NULL, 0);
830 }
831 
832 /*
833  * Lookup or enter a new address in arptab.
834  */
835 struct llinfo_arp *
836 arplookup(addr, create, proxy)
837 	u_int32_t addr;
838 	int create, proxy;
839 {
840 	struct rtentry *rt;
841 	static struct sockaddr_inarp sin;
842 
843 	sin.sin_len = sizeof(sin);
844 	sin.sin_family = AF_INET;
845 	sin.sin_addr.s_addr = addr;
846 	sin.sin_other = proxy ? SIN_PROXY : 0;
847 	rt = rtalloc1(sintosa(&sin), create, 0);
848 	if (rt == 0)
849 		return (0);
850 	rt->rt_refcnt--;
851 	if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
852 	    rt->rt_gateway->sa_family != AF_LINK) {
853 		if (create) {
854 			if (rt->rt_refcnt <= 0 &&
855 			    (rt->rt_flags & RTF_CLONED) != 0) {
856 				struct rt_addrinfo info;
857 
858 				bzero(&info, sizeof(info));
859 				info.rti_info[RTAX_DST] = rt_key(rt);
860 				info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
861 				info.rti_info[RTAX_NETMASK] = rt_mask(rt);
862 
863 				rtrequest1(RTM_DELETE, &info, rt->rt_priority,
864 				    NULL, 0);
865 			}
866 		}
867 		return (0);
868 	}
869 	return ((struct llinfo_arp *)rt->rt_llinfo);
870 }
871 
872 int
873 arpioctl(cmd, data)
874 	u_long cmd;
875 	caddr_t data;
876 {
877 
878 	return (EOPNOTSUPP);
879 }
880 
881 void
882 arp_ifinit(ac, ifa)
883 	struct arpcom *ac;
884 	struct ifaddr *ifa;
885 {
886 
887 	/* Warn the user if another station has this IP address. */
888 	arprequest(&ac->ac_if,
889 	    &(IA_SIN(ifa)->sin_addr.s_addr),
890 	    &(IA_SIN(ifa)->sin_addr.s_addr),
891 	    ac->ac_enaddr);
892 	ifa->ifa_rtrequest = arp_rtrequest;
893 	ifa->ifa_flags |= RTF_CLONING;
894 }
895 
896 /*
897  * Called from Ethernet interrupt handlers
898  * when ether packet type ETHERTYPE_REVARP
899  * is received.  Common length and type checks are done here,
900  * then the protocol-specific routine is called.
901  */
902 void
903 revarpinput(m)
904 	struct mbuf *m;
905 {
906 	struct arphdr *ar;
907 
908 	if (m->m_len < sizeof(struct arphdr))
909 		goto out;
910 	ar = mtod(m, struct arphdr *);
911 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
912 		goto out;
913 	if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
914 		goto out;
915 	switch (ntohs(ar->ar_pro)) {
916 
917 	case ETHERTYPE_IP:
918 	case ETHERTYPE_IPTRAILERS:
919 		in_revarpinput(m);
920 		return;
921 
922 	default:
923 		break;
924 	}
925 out:
926 	m_freem(m);
927 }
928 
929 /*
930  * RARP for Internet protocols on Ethernet.
931  * Algorithm is that given in RFC 903.
932  * We are only using for bootstrap purposes to get an ip address for one of
933  * our interfaces.  Thus we support no user-interface.
934  *
935  * Since the contents of the RARP reply are specific to the interface that
936  * sent the request, this code must ensure that they are properly associated.
937  *
938  * Note: also supports ARP via RARP packets, per the RFC.
939  */
940 void
941 in_revarpinput(m)
942 	struct mbuf *m;
943 {
944 	struct ifnet *ifp;
945 	struct ether_arp *ar;
946 	int op;
947 
948 	ar = mtod(m, struct ether_arp *);
949 	op = ntohs(ar->arp_op);
950 	switch (op) {
951 	case ARPOP_REQUEST:
952 	case ARPOP_REPLY:	/* per RFC */
953 		in_arpinput(m);
954 		return;
955 	case ARPOP_REVREPLY:
956 		break;
957 	case ARPOP_REVREQUEST:	/* handled by rarpd(8) */
958 	default:
959 		goto out;
960 	}
961 	if (!revarp_in_progress)
962 		goto out;
963 	ifp = m->m_pkthdr.rcvif;
964 	if (ifp != myip_ifp) /* !same interface */
965 		goto out;
966 	if (myip_initialized)
967 		goto wake;
968 	if (bcmp(ar->arp_tha, ((struct arpcom *)ifp)->ac_enaddr,
969 	    sizeof(ar->arp_tha)))
970 		goto out;
971 	bcopy((caddr_t)ar->arp_spa, (caddr_t)&srv_ip, sizeof(srv_ip));
972 	bcopy((caddr_t)ar->arp_tpa, (caddr_t)&myip, sizeof(myip));
973 	myip_initialized = 1;
974 wake:	/* Do wakeup every time in case it was missed. */
975 	wakeup((caddr_t)&myip);
976 
977 out:
978 	m_freem(m);
979 }
980 
981 /*
982  * Send a RARP request for the ip address of the specified interface.
983  * The request should be RFC 903-compliant.
984  */
985 void
986 revarprequest(ifp)
987 	struct ifnet *ifp;
988 {
989 	struct sockaddr sa;
990 	struct mbuf *m;
991 	struct ether_header *eh;
992 	struct ether_arp *ea;
993 	struct arpcom *ac = (struct arpcom *)ifp;
994 
995 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
996 		return;
997 	m->m_len = sizeof(*ea);
998 	m->m_pkthdr.len = sizeof(*ea);
999 	MH_ALIGN(m, sizeof(*ea));
1000 	ea = mtod(m, struct ether_arp *);
1001 	eh = (struct ether_header *)sa.sa_data;
1002 	bzero((caddr_t)ea, sizeof(*ea));
1003 	bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
1004 	    sizeof(eh->ether_dhost));
1005 	eh->ether_type = htons(ETHERTYPE_REVARP);
1006 	ea->arp_hrd = htons(ARPHRD_ETHER);
1007 	ea->arp_pro = htons(ETHERTYPE_IP);
1008 	ea->arp_hln = sizeof(ea->arp_sha);	/* hardware address length */
1009 	ea->arp_pln = sizeof(ea->arp_spa);	/* protocol address length */
1010 	ea->arp_op = htons(ARPOP_REVREQUEST);
1011 	bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost,
1012 	   sizeof(ea->arp_tha));
1013 	bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha,
1014 	   sizeof(ea->arp_sha));
1015 	bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_tha,
1016 	   sizeof(ea->arp_tha));
1017 	sa.sa_family = pseudo_AF_HDRCMPLT;
1018 	sa.sa_len = sizeof(sa);
1019 	ifp->if_output(ifp, m, &sa, (struct rtentry *)0);
1020 }
1021 
1022 /*
1023  * RARP for the ip address of the specified interface, but also
1024  * save the ip address of the server that sent the answer.
1025  * Timeout if no response is received.
1026  */
1027 int
1028 revarpwhoarewe(ifp, serv_in, clnt_in)
1029 	struct ifnet *ifp;
1030 	struct in_addr *serv_in;
1031 	struct in_addr *clnt_in;
1032 {
1033 	int result, count = 20;
1034 
1035 	if (myip_initialized)
1036 		return EIO;
1037 
1038 	myip_ifp = ifp;
1039 	revarp_in_progress = 1;
1040 	while (count--) {
1041 		revarprequest(ifp);
1042 		result = tsleep((caddr_t)&myip, PSOCK, "revarp", hz/2);
1043 		if (result != EWOULDBLOCK)
1044 			break;
1045 	}
1046 	revarp_in_progress = 0;
1047 	if (!myip_initialized)
1048 		return ENETUNREACH;
1049 
1050 	bcopy((caddr_t)&srv_ip, serv_in, sizeof(*serv_in));
1051 	bcopy((caddr_t)&myip, clnt_in, sizeof(*clnt_in));
1052 	return 0;
1053 }
1054 
1055 /* For compatibility: only saves interface address. */
1056 int
1057 revarpwhoami(in, ifp)
1058 	struct in_addr *in;
1059 	struct ifnet *ifp;
1060 {
1061 	struct in_addr server;
1062 	return (revarpwhoarewe(ifp, &server, in));
1063 }
1064 
1065 
1066 #ifdef DDB
1067 
1068 #include <machine/db_machdep.h>
1069 #include <ddb/db_interface.h>
1070 #include <ddb/db_output.h>
1071 
1072 void
1073 db_print_sa(sa)
1074 	struct sockaddr *sa;
1075 {
1076 	int len;
1077 	u_char *p;
1078 
1079 	if (sa == 0) {
1080 		db_printf("[NULL]");
1081 		return;
1082 	}
1083 
1084 	p = (u_char *)sa;
1085 	len = sa->sa_len;
1086 	db_printf("[");
1087 	while (len > 0) {
1088 		db_printf("%d", *p);
1089 		p++;
1090 		len--;
1091 		if (len)
1092 			db_printf(",");
1093 	}
1094 	db_printf("]\n");
1095 }
1096 
1097 void
1098 db_print_ifa(ifa)
1099 	struct ifaddr *ifa;
1100 {
1101 	if (ifa == 0)
1102 		return;
1103 	db_printf("  ifa_addr=");
1104 	db_print_sa(ifa->ifa_addr);
1105 	db_printf("  ifa_dsta=");
1106 	db_print_sa(ifa->ifa_dstaddr);
1107 	db_printf("  ifa_mask=");
1108 	db_print_sa(ifa->ifa_netmask);
1109 	db_printf("  flags=0x%x, refcnt=%d, metric=%d\n",
1110 	    ifa->ifa_flags, ifa->ifa_refcnt, ifa->ifa_metric);
1111 }
1112 
1113 void
1114 db_print_llinfo(li)
1115 	caddr_t li;
1116 {
1117 	struct llinfo_arp *la;
1118 
1119 	if (li == 0)
1120 		return;
1121 	la = (struct llinfo_arp *)li;
1122 	db_printf("  la_rt=%p la_hold_head=%p, la_asked=0x%lx\n",
1123 	    la->la_rt, la->la_hold_head, la->la_asked);
1124 }
1125 
1126 /*
1127  * Function to pass to rn_walktree().
1128  * Return non-zero error to abort walk.
1129  */
1130 int
1131 db_show_radix_node(rn, w)
1132 	struct radix_node *rn;
1133 	void *w;
1134 {
1135 	struct rtentry *rt = (struct rtentry *)rn;
1136 
1137 	db_printf("rtentry=%p", rt);
1138 
1139 	db_printf(" flags=0x%x refcnt=%d use=%ld expire=%ld\n",
1140 	    rt->rt_flags, rt->rt_refcnt, rt->rt_use, rt->rt_expire);
1141 
1142 	db_printf(" key="); db_print_sa(rt_key(rt));
1143 	db_printf(" mask="); db_print_sa(rt_mask(rt));
1144 	db_printf(" gw="); db_print_sa(rt->rt_gateway);
1145 
1146 	db_printf(" ifp=%p ", rt->rt_ifp);
1147 	if (rt->rt_ifp)
1148 		db_printf("(%s)", rt->rt_ifp->if_xname);
1149 	else
1150 		db_printf("(NULL)");
1151 
1152 	db_printf(" ifa=%p\n", rt->rt_ifa);
1153 	db_print_ifa(rt->rt_ifa);
1154 
1155 	db_printf(" genmask="); db_print_sa(rt->rt_genmask);
1156 
1157 	db_printf(" gwroute=%p llinfo=%p\n", rt->rt_gwroute, rt->rt_llinfo);
1158 	db_print_llinfo(rt->rt_llinfo);
1159 	return (0);
1160 }
1161 
1162 /*
1163  * Function to print all the route trees.
1164  * Use this from ddb:  "call db_show_arptab"
1165  */
1166 int
1167 db_show_arptab()
1168 {
1169 	struct radix_node_head *rnh;
1170 	rnh = rt_gettable(AF_INET, 0);
1171 	db_printf("Route tree for AF_INET\n");
1172 	if (rnh == NULL) {
1173 		db_printf(" (not initialized)\n");
1174 		return (0);
1175 	}
1176 	rn_walktree(rnh, db_show_radix_node, NULL);
1177 	return (0);
1178 }
1179 #endif
1180 #endif /* INET */
1181