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