if_arp.c revision 1.119 1 /* $NetBSD: if_arp.c,v 1.119 2007/02/17 22:34:10 dyoung Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Public Access Networks Corporation ("Panix"). It was developed under
9 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1982, 1986, 1988, 1993
42 * The Regents of the University of California. All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * SUCH DAMAGE.
67 *
68 * @(#)if_ether.c 8.2 (Berkeley) 9/26/94
69 */
70
71 /*
72 * Ethernet address resolution protocol.
73 * TODO:
74 * add "inuse/lock" bit (or ref. count) along with valid bit
75 */
76
77 #include <sys/cdefs.h>
78 __KERNEL_RCSID(0, "$NetBSD: if_arp.c,v 1.119 2007/02/17 22:34:10 dyoung Exp $");
79
80 #include "opt_ddb.h"
81 #include "opt_inet.h"
82
83 #ifdef INET
84
85 #include "bridge.h"
86
87 #include <sys/param.h>
88 #include <sys/systm.h>
89 #include <sys/callout.h>
90 #include <sys/malloc.h>
91 #include <sys/mbuf.h>
92 #include <sys/socket.h>
93 #include <sys/time.h>
94 #include <sys/timetc.h>
95 #include <sys/kernel.h>
96 #include <sys/errno.h>
97 #include <sys/ioctl.h>
98 #include <sys/syslog.h>
99 #include <sys/proc.h>
100 #include <sys/protosw.h>
101 #include <sys/domain.h>
102 #include <sys/sysctl.h>
103
104 #include <net/ethertypes.h>
105 #include <net/if.h>
106 #include <net/if_dl.h>
107 #include <net/if_token.h>
108 #include <net/if_types.h>
109 #include <net/if_ether.h>
110 #include <net/route.h>
111
112 #include <netinet/in.h>
113 #include <netinet/in_systm.h>
114 #include <netinet/in_var.h>
115 #include <netinet/ip.h>
116 #include <netinet/if_inarp.h>
117
118 #include "arc.h"
119 #if NARC > 0
120 #include <net/if_arc.h>
121 #endif
122 #include "fddi.h"
123 #if NFDDI > 0
124 #include <net/if_fddi.h>
125 #endif
126 #include "token.h"
127 #include "carp.h"
128 #if NCARP > 0
129 #include <netinet/ip_carp.h>
130 #endif
131
132 #define SIN(s) ((struct sockaddr_in *)s)
133 #define SDL(s) ((struct sockaddr_dl *)s)
134 #define SRP(s) ((struct sockaddr_inarp *)s)
135
136 /*
137 * ARP trailer negotiation. Trailer protocol is not IP specific,
138 * but ARP request/response use IP addresses.
139 */
140 #define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL
141
142 /* timer values */
143 int arpt_prune = (5*60*1); /* walk list every 5 minutes */
144 int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
145 int arpt_down = 20; /* once declared down, don't send for 20 secs */
146 int arpt_refresh = (5*60); /* time left before refreshing */
147 #define rt_expire rt_rmx.rmx_expire
148 #define rt_pksent rt_rmx.rmx_pksent
149
150 static void arptfree(struct llinfo_arp *);
151 static void arptimer(void *);
152 static struct llinfo_arp *arplookup(struct mbuf *, const struct in_addr *,
153 int, int);
154 static void in_arpinput(struct mbuf *);
155
156 LIST_HEAD(, llinfo_arp) llinfo_arp;
157 struct ifqueue arpintrq = {
158 .ifq_head = NULL,
159 .ifq_tail = NULL,
160 .ifq_len = 0,
161 .ifq_maxlen = 50,
162 .ifq_drops = 0,
163 };
164 int arp_inuse, arp_allocated, arp_intimer;
165 int arp_maxtries = 5;
166 int useloopback = 1; /* use loopback interface for local traffic */
167 int arpinit_done = 0;
168
169 struct arpstat arpstat;
170 struct callout arptimer_ch;
171
172
173 /* revarp state */
174 struct in_addr myip, srv_ip;
175 int myip_initialized = 0;
176 int revarp_in_progress = 0;
177 struct ifnet *myip_ifp = NULL;
178
179 #ifdef DDB
180 static void db_print_sa(const struct sockaddr *);
181 static void db_print_ifa(struct ifaddr *);
182 static void db_print_llinfo(caddr_t);
183 static int db_show_radix_node(struct radix_node *, void *);
184 #endif
185
186 /*
187 * this should be elsewhere.
188 */
189
190 static char *
191 lla_snprintf(u_int8_t *, int);
192
193 static char *
194 lla_snprintf(u_int8_t *adrp, int len)
195 {
196 #define NUMBUFS 3
197 static char buf[NUMBUFS][16*3];
198 static int bnum = 0;
199
200 int i;
201 char *p;
202
203 p = buf[bnum];
204
205 *p++ = hexdigits[(*adrp)>>4];
206 *p++ = hexdigits[(*adrp++)&0xf];
207
208 for (i=1; i<len && i<16; i++) {
209 *p++ = ':';
210 *p++ = hexdigits[(*adrp)>>4];
211 *p++ = hexdigits[(*adrp++)&0xf];
212 }
213
214 *p = 0;
215 p = buf[bnum];
216 bnum = (bnum + 1) % NUMBUFS;
217 return p;
218 }
219
220 DOMAIN_DEFINE(arpdomain); /* forward declare and add to link set */
221
222 const struct protosw arpsw[] = {
223 { 0, &arpdomain, 0, 0,
224 0, 0, 0, 0,
225 0,
226 0, 0, 0, arp_drain,
227 }
228 };
229
230
231 struct domain arpdomain = {
232 .dom_family = PF_ARP,
233 .dom_name = "arp",
234 .dom_protosw = arpsw,
235 .dom_protoswNPROTOSW = &arpsw[sizeof(arpsw)/sizeof(arpsw[0])],
236 };
237
238 /*
239 * ARP table locking.
240 *
241 * to prevent lossage vs. the arp_drain routine (which may be called at
242 * any time, including in a device driver context), we do two things:
243 *
244 * 1) manipulation of la->la_hold is done at splnet() (for all of
245 * about two instructions).
246 *
247 * 2) manipulation of the arp table's linked list is done under the
248 * protection of the ARP_LOCK; if arp_drain() or arptimer is called
249 * while the arp table is locked, we punt and try again later.
250 */
251
252 static int arp_locked;
253 static inline int arp_lock_try(int);
254 static inline void arp_unlock(void);
255
256 static inline int
257 arp_lock_try(int recurse)
258 {
259 int s;
260
261 /*
262 * Use splvm() -- we're blocking things that would cause
263 * mbuf allocation.
264 */
265 s = splvm();
266 if (!recurse && arp_locked) {
267 splx(s);
268 return (0);
269 }
270 arp_locked++;
271 splx(s);
272 return (1);
273 }
274
275 static inline void
276 arp_unlock(void)
277 {
278 int s;
279
280 s = splvm();
281 arp_locked--;
282 splx(s);
283 }
284
285 #ifdef DIAGNOSTIC
286 #define ARP_LOCK(recurse) \
287 do { \
288 if (arp_lock_try(recurse) == 0) { \
289 printf("%s:%d: arp already locked\n", __FILE__, __LINE__); \
290 panic("arp_lock"); \
291 } \
292 } while (/*CONSTCOND*/ 0)
293 #define ARP_LOCK_CHECK() \
294 do { \
295 if (arp_locked == 0) { \
296 printf("%s:%d: arp lock not held\n", __FILE__, __LINE__); \
297 panic("arp lock check"); \
298 } \
299 } while (/*CONSTCOND*/ 0)
300 #else
301 #define ARP_LOCK(x) (void) arp_lock_try(x)
302 #define ARP_LOCK_CHECK() /* nothing */
303 #endif
304
305 #define ARP_UNLOCK() arp_unlock()
306
307 /*
308 * ARP protocol drain routine. Called when memory is in short supply.
309 * Called at splvm();
310 */
311
312 void
313 arp_drain(void)
314 {
315 struct llinfo_arp *la, *nla;
316 int count = 0;
317 struct mbuf *mold;
318
319 if (arp_lock_try(0) == 0) {
320 printf("arp_drain: locked; punting\n");
321 return;
322 }
323
324 for (la = LIST_FIRST(&llinfo_arp); la != 0; la = nla) {
325 nla = LIST_NEXT(la, la_list);
326
327 mold = la->la_hold;
328 la->la_hold = 0;
329
330 if (mold) {
331 m_freem(mold);
332 count++;
333 }
334 }
335 ARP_UNLOCK();
336 arpstat.as_dfrdropped += count;
337 }
338
339
340 /*
341 * Timeout routine. Age arp_tab entries periodically.
342 */
343 /* ARGSUSED */
344 static void
345 arptimer(void *arg)
346 {
347 int s;
348 struct llinfo_arp *la, *nla;
349
350 s = splsoftnet();
351
352 if (arp_lock_try(0) == 0) {
353 /* get it later.. */
354 splx(s);
355 return;
356 }
357
358 callout_reset(&arptimer_ch, arpt_prune * hz, arptimer, NULL);
359 for (la = LIST_FIRST(&llinfo_arp); la != 0; la = nla) {
360 struct rtentry *rt = la->la_rt;
361
362 nla = LIST_NEXT(la, la_list);
363 if (rt->rt_expire == 0)
364 continue;
365 if ((rt->rt_expire - time_second) < arpt_refresh &&
366 rt->rt_pksent > (time_second - arpt_keep)) {
367 /*
368 * If the entry has been used during since last
369 * refresh, try to renew it before deleting.
370 */
371 arprequest(rt->rt_ifp,
372 &satocsin(rt->rt_ifa->ifa_addr)->sin_addr,
373 &satocsin(rt_key(rt))->sin_addr,
374 LLADDR(rt->rt_ifp->if_sadl));
375 } else if (rt->rt_expire <= time_second)
376 arptfree(la); /* timer has expired; clear */
377 }
378
379 ARP_UNLOCK();
380
381 splx(s);
382 }
383
384 /*
385 * Parallel to llc_rtrequest.
386 */
387 void
388 arp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
389 {
390 struct sockaddr *gate = rt->rt_gateway;
391 struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
392 static const struct sockaddr_dl null_sdl = {
393 .sdl_len = sizeof(null_sdl),
394 .sdl_family = AF_LINK,
395 };
396 size_t allocsize;
397 struct mbuf *mold;
398 int s;
399 struct in_ifaddr *ia;
400 struct ifaddr *ifa;
401
402 if (!arpinit_done) {
403 arpinit_done = 1;
404 /*
405 * We generate expiration times from time_second
406 * so avoid accidentally creating permanent routes.
407 */
408 if (time_second == 0) {
409 #ifdef __HAVE_TIMECOUNTER
410 struct timespec ts;
411 ts.tv_sec = 1;
412 ts.tv_nsec = 0;
413 tc_setclock(&ts);
414 #else /* !__HAVE_TIMECOUNTER */
415 time.tv_sec++;
416 #endif /* !__HAVE_TIMECOUNTER */
417 }
418 callout_init(&arptimer_ch);
419 callout_reset(&arptimer_ch, hz, arptimer, NULL);
420 }
421
422 if ((rt->rt_flags & RTF_GATEWAY) != 0) {
423 if (req != RTM_ADD)
424 return;
425
426 /*
427 * linklayers with particular link MTU limitation.
428 */
429 switch(rt->rt_ifp->if_type) {
430 #if NFDDI > 0
431 case IFT_FDDI:
432 if (rt->rt_ifp->if_mtu > FDDIIPMTU)
433 rt->rt_rmx.rmx_mtu = FDDIIPMTU;
434 break;
435 #endif
436 #if NARC > 0
437 case IFT_ARCNET:
438 {
439 int arcipifmtu;
440
441 if (rt->rt_ifp->if_flags & IFF_LINK0)
442 arcipifmtu = arc_ipmtu;
443 else
444 arcipifmtu = ARCMTU;
445 if (rt->rt_ifp->if_mtu > arcipifmtu)
446 rt->rt_rmx.rmx_mtu = arcipifmtu;
447 break;
448 }
449 #endif
450 }
451 return;
452 }
453
454 ARP_LOCK(1); /* we may already be locked here. */
455
456 switch (req) {
457
458 case RTM_ADD:
459 /*
460 * XXX: If this is a manually added route to interface
461 * such as older version of routed or gated might provide,
462 * restore cloning bit.
463 */
464 if ((rt->rt_flags & RTF_HOST) == 0 &&
465 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
466 rt->rt_flags |= RTF_CLONING;
467 if (rt->rt_flags & RTF_CLONING) {
468 /*
469 * Case 1: This route should come from a route to iface.
470 */
471 rt_setgate(rt, rt_key(rt),
472 (const struct sockaddr *)&null_sdl);
473 gate = rt->rt_gateway;
474 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
475 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
476 /*
477 * Give this route an expiration time, even though
478 * it's a "permanent" route, so that routes cloned
479 * from it do not need their expiration time set.
480 */
481 rt->rt_expire = time_second;
482 /*
483 * linklayers with particular link MTU limitation.
484 */
485 switch (rt->rt_ifp->if_type) {
486 #if NFDDI > 0
487 case IFT_FDDI:
488 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 &&
489 (rt->rt_rmx.rmx_mtu > FDDIIPMTU ||
490 (rt->rt_rmx.rmx_mtu == 0 &&
491 rt->rt_ifp->if_mtu > FDDIIPMTU)))
492 rt->rt_rmx.rmx_mtu = FDDIIPMTU;
493 break;
494 #endif
495 #if NARC > 0
496 case IFT_ARCNET:
497 {
498 int arcipifmtu;
499 if (rt->rt_ifp->if_flags & IFF_LINK0)
500 arcipifmtu = arc_ipmtu;
501 else
502 arcipifmtu = ARCMTU;
503
504 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 &&
505 (rt->rt_rmx.rmx_mtu > arcipifmtu ||
506 (rt->rt_rmx.rmx_mtu == 0 &&
507 rt->rt_ifp->if_mtu > arcipifmtu)))
508 rt->rt_rmx.rmx_mtu = arcipifmtu;
509 break;
510 }
511 #endif
512 }
513 break;
514 }
515 /* Announce a new entry if requested. */
516 if (rt->rt_flags & RTF_ANNOUNCE)
517 arprequest(rt->rt_ifp,
518 &satocsin(rt_key(rt))->sin_addr,
519 &satocsin(rt_key(rt))->sin_addr,
520 (u_char *)LLADDR(SDL(gate)));
521 /*FALLTHROUGH*/
522 case RTM_RESOLVE:
523 if (gate->sa_family != AF_LINK ||
524 gate->sa_len < sizeof(null_sdl)) {
525 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
526 break;
527 }
528 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
529 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
530 if (la != 0)
531 break; /* This happens on a route change */
532 /*
533 * Case 2: This route may come from cloning, or a manual route
534 * add with a LL address.
535 */
536 switch (SDL(gate)->sdl_type) {
537 #if NTOKEN > 0
538 case IFT_ISO88025:
539 allocsize = sizeof(*la) + sizeof(struct token_rif);
540 break;
541 #endif /* NTOKEN > 0 */
542 default:
543 allocsize = sizeof(*la);
544 }
545 R_Malloc(la, struct llinfo_arp *, allocsize);
546 rt->rt_llinfo = (caddr_t)la;
547 if (la == 0) {
548 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
549 break;
550 }
551 arp_inuse++, arp_allocated++;
552 Bzero(la, allocsize);
553 la->la_rt = rt;
554 rt->rt_flags |= RTF_LLINFO;
555 LIST_INSERT_HEAD(&llinfo_arp, la, la_list);
556
557 INADDR_TO_IA(satocsin(rt_key(rt))->sin_addr, ia);
558 while (ia && ia->ia_ifp != rt->rt_ifp)
559 NEXT_IA_WITH_SAME_ADDR(ia);
560 if (ia) {
561 /*
562 * This test used to be
563 * if (lo0ifp->if_flags & IFF_UP)
564 * It allowed local traffic to be forced through
565 * the hardware by configuring the loopback down.
566 * However, it causes problems during network
567 * configuration for boards that can't receive
568 * packets they send. It is now necessary to clear
569 * "useloopback" and remove the route to force
570 * traffic out to the hardware.
571 *
572 * In 4.4BSD, the above "if" statement checked
573 * rt->rt_ifa against rt_key(rt). It was changed
574 * to the current form so that we can provide a
575 * better support for multiple IPv4 addresses on a
576 * interface.
577 */
578 rt->rt_expire = 0;
579 Bcopy(LLADDR(rt->rt_ifp->if_sadl),
580 LLADDR(SDL(gate)),
581 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
582 if (useloopback)
583 rt->rt_ifp = lo0ifp;
584 /*
585 * make sure to set rt->rt_ifa to the interface
586 * address we are using, otherwise we will have trouble
587 * with source address selection.
588 */
589 ifa = &ia->ia_ifa;
590 if (ifa != rt->rt_ifa)
591 rt_replace_ifa(rt, ifa);
592 }
593 break;
594
595 case RTM_DELETE:
596 if (la == 0)
597 break;
598 arp_inuse--;
599 LIST_REMOVE(la, la_list);
600 rt->rt_llinfo = 0;
601 rt->rt_flags &= ~RTF_LLINFO;
602
603 s = splnet();
604 mold = la->la_hold;
605 la->la_hold = 0;
606 splx(s);
607
608 if (mold)
609 m_freem(mold);
610
611 Free((caddr_t)la);
612 }
613 ARP_UNLOCK();
614 }
615
616 /*
617 * Broadcast an ARP request. Caller specifies:
618 * - arp header source ip address
619 * - arp header target ip address
620 * - arp header source ethernet address
621 */
622 void
623 arprequest(struct ifnet *ifp,
624 const struct in_addr *sip, const struct in_addr *tip,
625 const u_int8_t *enaddr)
626 {
627 struct mbuf *m;
628 struct arphdr *ah;
629 struct sockaddr sa;
630
631 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
632 return;
633 MCLAIM(m, &arpdomain.dom_mowner);
634 switch (ifp->if_type) {
635 case IFT_IEEE1394:
636 m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
637 ifp->if_addrlen;
638 break;
639 default:
640 m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
641 2 * ifp->if_addrlen;
642 break;
643 }
644 m->m_pkthdr.len = m->m_len;
645 MH_ALIGN(m, m->m_len);
646 ah = mtod(m, struct arphdr *);
647 bzero((caddr_t)ah, m->m_len);
648 switch (ifp->if_type) {
649 case IFT_IEEE1394: /* RFC2734 */
650 /* fill it now for ar_tpa computation */
651 ah->ar_hrd = htons(ARPHRD_IEEE1394);
652 break;
653 default:
654 /* ifp->if_output will fill ar_hrd */
655 break;
656 }
657 ah->ar_pro = htons(ETHERTYPE_IP);
658 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
659 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
660 ah->ar_op = htons(ARPOP_REQUEST);
661 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
662 memcpy(ar_spa(ah), sip, ah->ar_pln);
663 memcpy(ar_tpa(ah), tip, ah->ar_pln);
664 sa.sa_family = AF_ARP;
665 sa.sa_len = 2;
666 m->m_flags |= M_BCAST;
667 arpstat.as_sndtotal++;
668 arpstat.as_sndrequest++;
669 (*ifp->if_output)(ifp, m, &sa, NULL);
670 }
671
672 /*
673 * Resolve an IP address into an ethernet address. If success,
674 * desten is filled in. If there is no entry in arptab,
675 * set one up and broadcast a request for the IP address.
676 * Hold onto this mbuf and resend it once the address
677 * is finally resolved. A return value of 1 indicates
678 * that desten has been filled in and the packet should be sent
679 * normally; a 0 return indicates that the packet has been
680 * taken over here, either now or for later transmission.
681 */
682 int
683 arpresolve(struct ifnet *ifp, struct rtentry *rt, struct mbuf *m,
684 const struct sockaddr *dst, u_char *desten)
685 {
686 struct llinfo_arp *la;
687 struct sockaddr_dl *sdl;
688 struct mbuf *mold;
689 int s;
690
691 if (rt)
692 la = (struct llinfo_arp *)rt->rt_llinfo;
693 else {
694 if ((la = arplookup(m, &satocsin(dst)->sin_addr, 1, 0)) != NULL)
695 rt = la->la_rt;
696 }
697 if (la == 0 || rt == 0) {
698 arpstat.as_allocfail++;
699 log(LOG_DEBUG,
700 "arpresolve: can't allocate llinfo on %s for %s\n",
701 ifp->if_xname, in_fmtaddr(satocsin(dst)->sin_addr));
702 m_freem(m);
703 return (0);
704 }
705 sdl = SDL(rt->rt_gateway);
706 /*
707 * Check the address family and length is valid, the address
708 * is resolved; otherwise, try to resolve.
709 */
710 if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
711 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
712 bcopy(LLADDR(sdl), desten,
713 min(sdl->sdl_alen, ifp->if_addrlen));
714 rt->rt_pksent = time_second; /* Time for last pkt sent */
715 return 1;
716 }
717 /*
718 * There is an arptab entry, but no ethernet address
719 * response yet. Replace the held mbuf with this
720 * latest one.
721 */
722
723 arpstat.as_dfrtotal++;
724 s = splnet();
725 mold = la->la_hold;
726 la->la_hold = m;
727 splx(s);
728
729 if (mold) {
730 arpstat.as_dfrdropped++;
731 m_freem(mold);
732 }
733
734 /*
735 * Re-send the ARP request when appropriate.
736 */
737 #ifdef DIAGNOSTIC
738 if (rt->rt_expire == 0) {
739 /* This should never happen. (Should it? -gwr) */
740 printf("arpresolve: unresolved and rt_expire == 0\n");
741 /* Set expiration time to now (expired). */
742 rt->rt_expire = time_second;
743 }
744 #endif
745 if (rt->rt_expire) {
746 rt->rt_flags &= ~RTF_REJECT;
747 if (la->la_asked == 0 || rt->rt_expire != time_second) {
748 rt->rt_expire = time_second;
749 if (la->la_asked++ < arp_maxtries)
750 arprequest(ifp,
751 &satocsin(rt->rt_ifa->ifa_addr)->sin_addr,
752 &satocsin(dst)->sin_addr,
753 #if NCARP > 0
754 (rt->rt_ifp->if_type == IFT_CARP) ?
755 LLADDR(rt->rt_ifp->if_sadl):
756 #endif
757 LLADDR(ifp->if_sadl));
758 else {
759 rt->rt_flags |= RTF_REJECT;
760 rt->rt_expire += arpt_down;
761 la->la_asked = 0;
762 }
763 }
764 }
765 return (0);
766 }
767
768 /*
769 * Common length and type checks are done here,
770 * then the protocol-specific routine is called.
771 */
772 void
773 arpintr(void)
774 {
775 struct mbuf *m;
776 struct arphdr *ar;
777 int s;
778 int arplen;
779
780 while (arpintrq.ifq_head) {
781 s = splnet();
782 IF_DEQUEUE(&arpintrq, m);
783 splx(s);
784 if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
785 panic("arpintr");
786
787 MCLAIM(m, &arpdomain.dom_mowner);
788 arpstat.as_rcvtotal++;
789
790 /*
791 * First, make sure we have at least struct arphdr.
792 */
793 if (m->m_len < sizeof(struct arphdr) ||
794 (ar = mtod(m, struct arphdr *)) == NULL)
795 goto badlen;
796
797 switch (m->m_pkthdr.rcvif->if_type) {
798 case IFT_IEEE1394:
799 arplen = sizeof(struct arphdr) +
800 ar->ar_hln + 2 * ar->ar_pln;
801 break;
802 default:
803 arplen = sizeof(struct arphdr) +
804 2 * ar->ar_hln + 2 * ar->ar_pln;
805 break;
806 }
807
808 if (/* XXX ntohs(ar->ar_hrd) == ARPHRD_ETHER && */
809 m->m_len >= arplen)
810 switch (ntohs(ar->ar_pro)) {
811 case ETHERTYPE_IP:
812 case ETHERTYPE_IPTRAILERS:
813 in_arpinput(m);
814 continue;
815 default:
816 arpstat.as_rcvbadproto++;
817 }
818 else {
819 badlen:
820 arpstat.as_rcvbadlen++;
821 }
822 m_freem(m);
823 }
824 }
825
826 /*
827 * ARP for Internet protocols on 10 Mb/s Ethernet.
828 * Algorithm is that given in RFC 826.
829 * In addition, a sanity check is performed on the sender
830 * protocol address, to catch impersonators.
831 * We no longer handle negotiations for use of trailer protocol:
832 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
833 * along with IP replies if we wanted trailers sent to us,
834 * and also sent them in response to IP replies.
835 * This allowed either end to announce the desire to receive
836 * trailer packets.
837 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
838 * but formerly didn't normally send requests.
839 */
840 static void
841 in_arpinput(struct mbuf *m)
842 {
843 struct arphdr *ah;
844 struct ifnet *ifp = m->m_pkthdr.rcvif;
845 struct llinfo_arp *la = 0;
846 struct rtentry *rt;
847 struct in_ifaddr *ia;
848 #if NBRIDGE > 0
849 struct in_ifaddr *bridge_ia = NULL;
850 #endif
851 #if NCARP > 0
852 u_int32_t count = 0, index = 0;
853 #endif
854 struct sockaddr_dl *sdl;
855 struct sockaddr sa;
856 struct in_addr isaddr, itaddr, myaddr;
857 int op;
858 struct mbuf *mold;
859 caddr_t tha;
860 int s;
861
862 if (__predict_false(m_makewritable(&m, 0, m->m_pkthdr.len, M_DONTWAIT)))
863 goto out;
864 ah = mtod(m, struct arphdr *);
865 op = ntohs(ah->ar_op);
866
867 /*
868 * Fix up ah->ar_hrd if necessary, before using ar_tha() or
869 * ar_tpa().
870 */
871 switch (ifp->if_type) {
872 case IFT_IEEE1394:
873 if (ntohs(ah->ar_hrd) == ARPHRD_IEEE1394)
874 ;
875 else {
876 /* XXX this is to make sure we compute ar_tha right */
877 /* XXX check ar_hrd more strictly? */
878 ah->ar_hrd = htons(ARPHRD_IEEE1394);
879 }
880 break;
881 default:
882 /* XXX check ar_hrd? */
883 break;
884 }
885
886 bcopy((caddr_t)ar_spa(ah), (caddr_t)&isaddr, sizeof (isaddr));
887 bcopy((caddr_t)ar_tpa(ah), (caddr_t)&itaddr, sizeof (itaddr));
888
889 if (m->m_flags & (M_BCAST|M_MCAST))
890 arpstat.as_rcvmcast++;
891
892 /*
893 * If the target IP address is zero, ignore the packet.
894 * This prevents the code below from tring to answer
895 * when we are using IP address zero (booting).
896 */
897 if (in_nullhost(itaddr)) {
898 arpstat.as_rcvzerotpa++;
899 goto out;
900 }
901
902 /*
903 * If the source IP address is zero, this is most likely a
904 * confused host trying to use IP address zero. (Windoze?)
905 * XXX: Should we bother trying to reply to these?
906 */
907 if (in_nullhost(isaddr)) {
908 arpstat.as_rcvzerospa++;
909 goto out;
910 }
911
912 /*
913 * Search for a matching interface address
914 * or any address on the interface to use
915 * as a dummy address in the rest of this function
916 */
917
918 INADDR_TO_IA(itaddr, ia);
919 while (ia != NULL) {
920 #if NCARP > 0
921 if (ia->ia_ifp->if_type == IFT_CARP &&
922 ((ia->ia_ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
923 (IFF_UP|IFF_RUNNING))) {
924 index++;
925 if (ia->ia_ifp == m->m_pkthdr.rcvif &&
926 carp_iamatch(ia, ar_sha(ah),
927 &count, index)) {
928 break;
929 }
930 } else
931 #endif
932 if (ia->ia_ifp == m->m_pkthdr.rcvif)
933 break;
934 #if NBRIDGE > 0
935 /*
936 * If the interface we received the packet on
937 * is part of a bridge, check to see if we need
938 * to "bridge" the packet to ourselves at this
939 * layer. Note we still prefer a perfect match,
940 * but allow this weaker match if necessary.
941 */
942 if (m->m_pkthdr.rcvif->if_bridge != NULL &&
943 m->m_pkthdr.rcvif->if_bridge == ia->ia_ifp->if_bridge)
944 bridge_ia = ia;
945 #endif /* NBRIDGE > 0 */
946
947 NEXT_IA_WITH_SAME_ADDR(ia);
948 }
949
950 #if NBRIDGE > 0
951 if (ia == NULL && bridge_ia != NULL) {
952 ia = bridge_ia;
953 ifp = bridge_ia->ia_ifp;
954 }
955 #endif
956
957 if (ia == NULL) {
958 INADDR_TO_IA(isaddr, ia);
959 while ((ia != NULL) && ia->ia_ifp != m->m_pkthdr.rcvif)
960 NEXT_IA_WITH_SAME_ADDR(ia);
961
962 if (ia == NULL) {
963 IFP_TO_IA(ifp, ia);
964 if (ia == NULL) {
965 arpstat.as_rcvnoint++;
966 goto out;
967 }
968 }
969 }
970
971 myaddr = ia->ia_addr.sin_addr;
972
973 /* XXX checks for bridge case? */
974 if (!bcmp((caddr_t)ar_sha(ah), LLADDR(ifp->if_sadl),
975 ifp->if_addrlen)) {
976 arpstat.as_rcvlocalsha++;
977 goto out; /* it's from me, ignore it. */
978 }
979
980 /* XXX checks for bridge case? */
981 if (!memcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
982 arpstat.as_rcvbcastsha++;
983 log(LOG_ERR,
984 "%s: arp: link address is broadcast for IP address %s!\n",
985 ifp->if_xname, in_fmtaddr(isaddr));
986 goto out;
987 }
988
989 if (in_hosteq(isaddr, myaddr)) {
990 arpstat.as_rcvlocalspa++;
991 log(LOG_ERR,
992 "duplicate IP address %s sent from link address %s\n",
993 in_fmtaddr(isaddr), lla_snprintf(ar_sha(ah), ah->ar_hln));
994 itaddr = myaddr;
995 goto reply;
996 }
997 la = arplookup(m, &isaddr, in_hosteq(itaddr, myaddr), 0);
998 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
999 if (sdl->sdl_alen &&
1000 bcmp((caddr_t)ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
1001 if (rt->rt_flags & RTF_STATIC) {
1002 arpstat.as_rcvoverperm++;
1003 log(LOG_INFO,
1004 "%s tried to overwrite permanent arp info"
1005 " for %s\n",
1006 lla_snprintf(ar_sha(ah), ah->ar_hln),
1007 in_fmtaddr(isaddr));
1008 goto out;
1009 } else if (rt->rt_ifp != ifp) {
1010 arpstat.as_rcvoverint++;
1011 log(LOG_INFO,
1012 "%s on %s tried to overwrite "
1013 "arp info for %s on %s\n",
1014 lla_snprintf(ar_sha(ah), ah->ar_hln),
1015 ifp->if_xname, in_fmtaddr(isaddr),
1016 rt->rt_ifp->if_xname);
1017 goto out;
1018 } else {
1019 arpstat.as_rcvover++;
1020 log(LOG_INFO,
1021 "arp info overwritten for %s by %s\n",
1022 in_fmtaddr(isaddr),
1023 lla_snprintf(ar_sha(ah), ah->ar_hln));
1024 }
1025 }
1026 /*
1027 * sanity check for the address length.
1028 * XXX this does not work for protocols with variable address
1029 * length. -is
1030 */
1031 if (sdl->sdl_alen &&
1032 sdl->sdl_alen != ah->ar_hln) {
1033 arpstat.as_rcvlenchg++;
1034 log(LOG_WARNING,
1035 "arp from %s: new addr len %d, was %d",
1036 in_fmtaddr(isaddr), ah->ar_hln, sdl->sdl_alen);
1037 }
1038 if (ifp->if_addrlen != ah->ar_hln) {
1039 arpstat.as_rcvbadlen++;
1040 log(LOG_WARNING,
1041 "arp from %s: addr len: new %d, i/f %d (ignored)",
1042 in_fmtaddr(isaddr), ah->ar_hln,
1043 ifp->if_addrlen);
1044 goto reply;
1045 }
1046 #if NTOKEN > 0
1047 /*
1048 * XXX uses m_data and assumes the complete answer including
1049 * XXX token-ring headers is in the same buf
1050 */
1051 if (ifp->if_type == IFT_ISO88025) {
1052 struct token_header *trh;
1053
1054 trh = (struct token_header *)M_TRHSTART(m);
1055 if (trh->token_shost[0] & TOKEN_RI_PRESENT) {
1056 struct token_rif *rif;
1057 size_t riflen;
1058
1059 rif = TOKEN_RIF(trh);
1060 riflen = (ntohs(rif->tr_rcf) &
1061 TOKEN_RCF_LEN_MASK) >> 8;
1062
1063 if (riflen > 2 &&
1064 riflen < sizeof(struct token_rif) &&
1065 (riflen & 1) == 0) {
1066 rif->tr_rcf ^= htons(TOKEN_RCF_DIRECTION);
1067 rif->tr_rcf &= htons(~TOKEN_RCF_BROADCAST_MASK);
1068 bcopy(rif, TOKEN_RIF(la), riflen);
1069 }
1070 }
1071 }
1072 #endif /* NTOKEN > 0 */
1073 bcopy((caddr_t)ar_sha(ah), LLADDR(sdl),
1074 sdl->sdl_alen = ah->ar_hln);
1075 if (rt->rt_expire)
1076 rt->rt_expire = time_second + arpt_keep;
1077 rt->rt_flags &= ~RTF_REJECT;
1078 la->la_asked = 0;
1079
1080 s = splnet();
1081 mold = la->la_hold;
1082 la->la_hold = 0;
1083 splx(s);
1084
1085 if (mold) {
1086 arpstat.as_dfrsent++;
1087 (*ifp->if_output)(ifp, mold, rt_key(rt), rt);
1088 }
1089 }
1090 reply:
1091 if (op != ARPOP_REQUEST) {
1092 if (op == ARPOP_REPLY)
1093 arpstat.as_rcvreply++;
1094 out:
1095 m_freem(m);
1096 return;
1097 }
1098 arpstat.as_rcvrequest++;
1099 if (in_hosteq(itaddr, myaddr)) {
1100 /* I am the target */
1101 tha = ar_tha(ah);
1102 if (tha)
1103 bcopy((caddr_t)ar_sha(ah), tha, ah->ar_hln);
1104 bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln);
1105 } else {
1106 la = arplookup(m, &itaddr, 0, SIN_PROXY);
1107 if (la == 0)
1108 goto out;
1109 rt = la->la_rt;
1110 if (rt->rt_ifp->if_type == IFT_CARP &&
1111 m->m_pkthdr.rcvif->if_type != IFT_CARP)
1112 goto out;
1113 tha = ar_tha(ah);
1114 if (tha)
1115 bcopy((caddr_t)ar_sha(ah), tha, ah->ar_hln);
1116 sdl = SDL(rt->rt_gateway);
1117 bcopy(LLADDR(sdl), (caddr_t)ar_sha(ah), ah->ar_hln);
1118 }
1119
1120 bcopy((caddr_t)ar_spa(ah), (caddr_t)ar_tpa(ah), ah->ar_pln);
1121 bcopy((caddr_t)&itaddr, (caddr_t)ar_spa(ah), ah->ar_pln);
1122 ah->ar_op = htons(ARPOP_REPLY);
1123 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
1124 switch (ifp->if_type) {
1125 case IFT_IEEE1394:
1126 /*
1127 * ieee1394 arp reply is broadcast
1128 */
1129 m->m_flags &= ~M_MCAST;
1130 m->m_flags |= M_BCAST;
1131 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + ah->ar_hln;
1132 break;
1133
1134 default:
1135 m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
1136 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
1137 break;
1138 }
1139 m->m_pkthdr.len = m->m_len;
1140 sa.sa_family = AF_ARP;
1141 sa.sa_len = 2;
1142 arpstat.as_sndtotal++;
1143 arpstat.as_sndreply++;
1144 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
1145 return;
1146 }
1147
1148 /*
1149 * Free an arp entry.
1150 */
1151 static void arptfree(struct llinfo_arp *la)
1152 {
1153 struct rtentry *rt = la->la_rt;
1154 struct sockaddr_dl *sdl;
1155
1156 ARP_LOCK_CHECK();
1157
1158 if (rt == 0)
1159 panic("arptfree");
1160 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
1161 sdl->sdl_family == AF_LINK) {
1162 sdl->sdl_alen = 0;
1163 la->la_asked = 0;
1164 rt->rt_flags &= ~RTF_REJECT;
1165 return;
1166 }
1167 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
1168 0, (struct rtentry **)0);
1169 }
1170
1171 /*
1172 * Lookup or enter a new address in arptab.
1173 */
1174 static struct llinfo_arp *
1175 arplookup(struct mbuf *m, const struct in_addr *addr, int create, int proxy)
1176 {
1177 struct arphdr *ah;
1178 struct ifnet *ifp = m->m_pkthdr.rcvif;
1179 struct rtentry *rt;
1180 static struct sockaddr_inarp sin;
1181 const char *why = 0;
1182
1183 ah = mtod(m, struct arphdr *);
1184 sin.sin_len = sizeof(sin);
1185 sin.sin_family = AF_INET;
1186 sin.sin_addr = *addr;
1187 sin.sin_other = proxy ? SIN_PROXY : 0;
1188 rt = rtalloc1(sintosa(&sin), create);
1189 if (rt == 0)
1190 return (0);
1191 rt->rt_refcnt--;
1192
1193 if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) == RTF_LLINFO &&
1194 rt->rt_gateway->sa_family == AF_LINK)
1195 return ((struct llinfo_arp *)rt->rt_llinfo);
1196
1197
1198
1199 if (create) {
1200 if (rt->rt_flags & RTF_GATEWAY)
1201 why = "host is not on local network";
1202 else if ((rt->rt_flags & RTF_LLINFO) == 0) {
1203 arpstat.as_allocfail++;
1204 why = "could not allocate llinfo";
1205 } else
1206 why = "gateway route is not ours";
1207 log(LOG_DEBUG, "arplookup: unable to enter address"
1208 " for %s@%s on %s (%s)\n",
1209 in_fmtaddr(*addr), lla_snprintf(ar_sha(ah), ah->ar_hln),
1210 (ifp) ? ifp->if_xname : 0, why);
1211 if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_CLONED) != 0) {
1212 rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt),
1213 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
1214 }
1215 }
1216 return (0);
1217 }
1218
1219 int
1220 arpioctl(u_long cmd, caddr_t data)
1221 {
1222
1223 return (EOPNOTSUPP);
1224 }
1225
1226 void
1227 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1228 {
1229 struct in_addr *ip;
1230
1231 /*
1232 * Warn the user if another station has this IP address,
1233 * but only if the interface IP address is not zero.
1234 */
1235 ip = &IA_SIN(ifa)->sin_addr;
1236 if (!in_nullhost(*ip))
1237 arprequest(ifp, ip, ip, LLADDR(ifp->if_sadl));
1238
1239 ifa->ifa_rtrequest = arp_rtrequest;
1240 ifa->ifa_flags |= RTF_CLONING;
1241 }
1242
1243 /*
1244 * Called from 10 Mb/s Ethernet interrupt handlers
1245 * when ether packet type ETHERTYPE_REVARP
1246 * is received. Common length and type checks are done here,
1247 * then the protocol-specific routine is called.
1248 */
1249 void
1250 revarpinput(struct mbuf *m)
1251 {
1252 struct arphdr *ar;
1253
1254 if (m->m_len < sizeof(struct arphdr))
1255 goto out;
1256 ar = mtod(m, struct arphdr *);
1257 #if 0 /* XXX I don't think we need this... and it will prevent other LL */
1258 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
1259 goto out;
1260 #endif
1261 if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
1262 goto out;
1263 switch (ntohs(ar->ar_pro)) {
1264 case ETHERTYPE_IP:
1265 case ETHERTYPE_IPTRAILERS:
1266 in_revarpinput(m);
1267 return;
1268
1269 default:
1270 break;
1271 }
1272 out:
1273 m_freem(m);
1274 }
1275
1276 /*
1277 * RARP for Internet protocols on 10 Mb/s Ethernet.
1278 * Algorithm is that given in RFC 903.
1279 * We are only using for bootstrap purposes to get an ip address for one of
1280 * our interfaces. Thus we support no user-interface.
1281 *
1282 * Since the contents of the RARP reply are specific to the interface that
1283 * sent the request, this code must ensure that they are properly associated.
1284 *
1285 * Note: also supports ARP via RARP packets, per the RFC.
1286 */
1287 void
1288 in_revarpinput(struct mbuf *m)
1289 {
1290 struct ifnet *ifp;
1291 struct arphdr *ah;
1292 caddr_t tha;
1293 int op;
1294
1295 ah = mtod(m, struct arphdr *);
1296 op = ntohs(ah->ar_op);
1297
1298 switch (m->m_pkthdr.rcvif->if_type) {
1299 case IFT_IEEE1394:
1300 /* ARP without target hardware address is not supported */
1301 goto out;
1302 default:
1303 break;
1304 }
1305
1306 switch (op) {
1307 case ARPOP_REQUEST:
1308 case ARPOP_REPLY: /* per RFC */
1309 in_arpinput(m);
1310 return;
1311 case ARPOP_REVREPLY:
1312 break;
1313 case ARPOP_REVREQUEST: /* handled by rarpd(8) */
1314 default:
1315 goto out;
1316 }
1317 if (!revarp_in_progress)
1318 goto out;
1319 ifp = m->m_pkthdr.rcvif;
1320 if (ifp != myip_ifp) /* !same interface */
1321 goto out;
1322 if (myip_initialized)
1323 goto wake;
1324 tha = ar_tha(ah);
1325 KASSERT(tha);
1326 if (bcmp(tha, LLADDR(ifp->if_sadl), ifp->if_sadl->sdl_alen))
1327 goto out;
1328 bcopy((caddr_t)ar_spa(ah), (caddr_t)&srv_ip, sizeof(srv_ip));
1329 bcopy((caddr_t)ar_tpa(ah), (caddr_t)&myip, sizeof(myip));
1330 myip_initialized = 1;
1331 wake: /* Do wakeup every time in case it was missed. */
1332 wakeup((caddr_t)&myip);
1333
1334 out:
1335 m_freem(m);
1336 }
1337
1338 /*
1339 * Send a RARP request for the ip address of the specified interface.
1340 * The request should be RFC 903-compliant.
1341 */
1342 void
1343 revarprequest(struct ifnet *ifp)
1344 {
1345 struct sockaddr sa;
1346 struct mbuf *m;
1347 struct arphdr *ah;
1348 caddr_t tha;
1349
1350 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
1351 return;
1352 MCLAIM(m, &arpdomain.dom_mowner);
1353 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
1354 2*ifp->if_addrlen;
1355 m->m_pkthdr.len = m->m_len;
1356 MH_ALIGN(m, m->m_len);
1357 ah = mtod(m, struct arphdr *);
1358 bzero((caddr_t)ah, m->m_len);
1359 ah->ar_pro = htons(ETHERTYPE_IP);
1360 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
1361 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
1362 ah->ar_op = htons(ARPOP_REVREQUEST);
1363
1364 bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln);
1365 tha = ar_tha(ah);
1366 KASSERT(tha);
1367 bcopy(LLADDR(ifp->if_sadl), tha, ah->ar_hln);
1368
1369 sa.sa_family = AF_ARP;
1370 sa.sa_len = 2;
1371 m->m_flags |= M_BCAST;
1372 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
1373
1374 }
1375
1376 /*
1377 * RARP for the ip address of the specified interface, but also
1378 * save the ip address of the server that sent the answer.
1379 * Timeout if no response is received.
1380 */
1381 int
1382 revarpwhoarewe(struct ifnet *ifp, struct in_addr *serv_in,
1383 struct in_addr *clnt_in)
1384 {
1385 int result, count = 20;
1386
1387 myip_initialized = 0;
1388 myip_ifp = ifp;
1389
1390 revarp_in_progress = 1;
1391 while (count--) {
1392 revarprequest(ifp);
1393 result = tsleep((caddr_t)&myip, PSOCK, "revarp", hz/2);
1394 if (result != EWOULDBLOCK)
1395 break;
1396 }
1397 revarp_in_progress = 0;
1398
1399 if (!myip_initialized)
1400 return ENETUNREACH;
1401
1402 bcopy((caddr_t)&srv_ip, serv_in, sizeof(*serv_in));
1403 bcopy((caddr_t)&myip, clnt_in, sizeof(*clnt_in));
1404 return 0;
1405 }
1406
1407
1408
1410 #ifdef DDB
1411
1412 #include <machine/db_machdep.h>
1413 #include <ddb/db_interface.h>
1414 #include <ddb/db_output.h>
1415
1416 static void
1417 db_print_sa(const struct sockaddr *sa)
1418 {
1419 int len;
1420 const u_char *p;
1421
1422 if (sa == 0) {
1423 db_printf("[NULL]");
1424 return;
1425 }
1426
1427 p = (const u_char *)sa;
1428 len = sa->sa_len;
1429 db_printf("[");
1430 while (len > 0) {
1431 db_printf("%d", *p);
1432 p++; len--;
1433 if (len) db_printf(",");
1434 }
1435 db_printf("]\n");
1436 }
1437
1438 static void
1439 db_print_ifa(struct ifaddr *ifa)
1440 {
1441 if (ifa == 0)
1442 return;
1443 db_printf(" ifa_addr=");
1444 db_print_sa(ifa->ifa_addr);
1445 db_printf(" ifa_dsta=");
1446 db_print_sa(ifa->ifa_dstaddr);
1447 db_printf(" ifa_mask=");
1448 db_print_sa(ifa->ifa_netmask);
1449 db_printf(" flags=0x%x,refcnt=%d,metric=%d\n",
1450 ifa->ifa_flags,
1451 ifa->ifa_refcnt,
1452 ifa->ifa_metric);
1453 }
1454
1455 static void
1456 db_print_llinfo(caddr_t li)
1457 {
1458 struct llinfo_arp *la;
1459
1460 if (li == 0)
1461 return;
1462 la = (struct llinfo_arp *)li;
1463 db_printf(" la_rt=%p la_hold=%p, la_asked=0x%lx\n",
1464 la->la_rt, la->la_hold, la->la_asked);
1465 }
1466
1467 /*
1468 * Function to pass to rn_walktree().
1469 * Return non-zero error to abort walk.
1470 */
1471 static int
1472 db_show_radix_node(struct radix_node *rn, void *w)
1473 {
1474 struct rtentry *rt = (struct rtentry *)rn;
1475
1476 db_printf("rtentry=%p", rt);
1477
1478 db_printf(" flags=0x%x refcnt=%d use=%ld expire=%ld\n",
1479 rt->rt_flags, rt->rt_refcnt,
1480 rt->rt_use, rt->rt_expire);
1481
1482 db_printf(" key="); db_print_sa(rt_key(rt));
1483 db_printf(" mask="); db_print_sa(rt_mask(rt));
1484 db_printf(" gw="); db_print_sa(rt->rt_gateway);
1485
1486 db_printf(" ifp=%p ", rt->rt_ifp);
1487 if (rt->rt_ifp)
1488 db_printf("(%s)", rt->rt_ifp->if_xname);
1489 else
1490 db_printf("(NULL)");
1491
1492 db_printf(" ifa=%p\n", rt->rt_ifa);
1493 db_print_ifa(rt->rt_ifa);
1494
1495 db_printf(" genmask="); db_print_sa(rt->rt_genmask);
1496
1497 db_printf(" gwroute=%p llinfo=%p\n",
1498 rt->rt_gwroute, rt->rt_llinfo);
1499 db_print_llinfo(rt->rt_llinfo);
1500
1501 return (0);
1502 }
1503
1504 /*
1505 * Function to print all the route trees.
1506 * Use this from ddb: "show arptab"
1507 */
1508 void
1509 db_show_arptab(db_expr_t addr, int have_addr,
1510 db_expr_t count, const char *modif)
1511 {
1512 struct radix_node_head *rnh;
1513 rnh = rt_tables[AF_INET];
1514 db_printf("Route tree for AF_INET\n");
1515 if (rnh == NULL) {
1516 db_printf(" (not initialized)\n");
1517 return;
1518 }
1519 rn_walktree(rnh, db_show_radix_node, NULL);
1520 return;
1521 }
1522 #endif
1523
1524 SYSCTL_SETUP(sysctl_net_inet_arp_setup, "sysctl net.inet.arp subtree setup")
1525 {
1526 const struct sysctlnode *node;
1527
1528 sysctl_createv(clog, 0, NULL, NULL,
1529 CTLFLAG_PERMANENT,
1530 CTLTYPE_NODE, "net", NULL,
1531 NULL, 0, NULL, 0,
1532 CTL_NET, CTL_EOL);
1533 sysctl_createv(clog, 0, NULL, NULL,
1534 CTLFLAG_PERMANENT,
1535 CTLTYPE_NODE, "inet", NULL,
1536 NULL, 0, NULL, 0,
1537 CTL_NET, PF_INET, CTL_EOL);
1538 sysctl_createv(clog, 0, NULL, &node,
1539 CTLFLAG_PERMANENT,
1540 CTLTYPE_NODE, "arp",
1541 SYSCTL_DESCR("Address Resolution Protocol"),
1542 NULL, 0, NULL, 0,
1543 CTL_NET, PF_INET, CTL_CREATE, CTL_EOL);
1544
1545 sysctl_createv(clog, 0, NULL, NULL,
1546 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1547 CTLTYPE_INT, "prune",
1548 SYSCTL_DESCR("ARP cache pruning interval"),
1549 NULL, 0, &arpt_prune, 0,
1550 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
1551
1552 sysctl_createv(clog, 0, NULL, NULL,
1553 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1554 CTLTYPE_INT, "keep",
1555 SYSCTL_DESCR("Valid ARP entry lifetime"),
1556 NULL, 0, &arpt_keep, 0,
1557 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
1558
1559 sysctl_createv(clog, 0, NULL, NULL,
1560 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1561 CTLTYPE_INT, "down",
1562 SYSCTL_DESCR("Failed ARP entry lifetime"),
1563 NULL, 0, &arpt_down, 0,
1564 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
1565
1566 sysctl_createv(clog, 0, NULL, NULL,
1567 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1568 CTLTYPE_INT, "refresh",
1569 SYSCTL_DESCR("ARP entry refresh interval"),
1570 NULL, 0, &arpt_refresh, 0,
1571 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
1572 }
1573
1574 #endif /* INET */
1575