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