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