if_arp.c revision 1.110 1 /* $NetBSD: if_arp.c,v 1.110 2006/05/18 09:05:51 liamjfoy 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.110 2006/05/18 09:05:51 liamjfoy 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 ah = mtod(m, struct arphdr *);
846 op = ntohs(ah->ar_op);
847
848 /*
849 * Fix up ah->ar_hrd if necessary, before using ar_tha() or
850 * ar_tpa().
851 */
852 switch (ifp->if_type) {
853 case IFT_IEEE1394:
854 if (ntohs(ah->ar_hrd) == ARPHRD_IEEE1394)
855 ;
856 else {
857 /* XXX this is to make sure we compute ar_tha right */
858 /* XXX check ar_hrd more strictly? */
859 ah->ar_hrd = htons(ARPHRD_IEEE1394);
860 }
861 break;
862 default:
863 /* XXX check ar_hrd? */
864 break;
865 }
866
867 bcopy((caddr_t)ar_spa(ah), (caddr_t)&isaddr, sizeof (isaddr));
868 bcopy((caddr_t)ar_tpa(ah), (caddr_t)&itaddr, sizeof (itaddr));
869
870 if (m->m_flags & (M_BCAST|M_MCAST))
871 arpstat.as_rcvmcast++;
872
873 /*
874 * If the target IP address is zero, ignore the packet.
875 * This prevents the code below from tring to answer
876 * when we are using IP address zero (booting).
877 */
878 if (in_nullhost(itaddr)) {
879 arpstat.as_rcvzerotpa++;
880 goto out;
881 }
882
883 /*
884 * If the source IP address is zero, this is most likely a
885 * confused host trying to use IP address zero. (Windoze?)
886 * XXX: Should we bother trying to reply to these?
887 */
888 if (in_nullhost(isaddr)) {
889 arpstat.as_rcvzerospa++;
890 goto out;
891 }
892
893 /*
894 * Search for a matching interface address
895 * or any address on the interface to use
896 * as a dummy address in the rest of this function
897 */
898
899 INADDR_TO_IA(itaddr, ia);
900 while (ia != NULL) {
901 #if NCARP > 0
902 if (ia->ia_ifp->if_type == IFT_CARP &&
903 ((ia->ia_ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
904 (IFF_UP|IFF_RUNNING))) {
905 index++;
906 if (ia->ia_ifp == m->m_pkthdr.rcvif &&
907 carp_iamatch(ia, ar_sha(ah),
908 &count, index)) {
909 break;
910 }
911 } else
912 #endif
913 if (ia->ia_ifp == m->m_pkthdr.rcvif)
914 break;
915 #if NBRIDGE > 0
916 /*
917 * If the interface we received the packet on
918 * is part of a bridge, check to see if we need
919 * to "bridge" the packet to ourselves at this
920 * layer. Note we still prefer a perfect match,
921 * but allow this weaker match if necessary.
922 */
923 if (m->m_pkthdr.rcvif->if_bridge != NULL &&
924 m->m_pkthdr.rcvif->if_bridge == ia->ia_ifp->if_bridge)
925 bridge_ia = ia;
926 #endif /* NBRIDGE > 0 */
927
928 NEXT_IA_WITH_SAME_ADDR(ia);
929 }
930
931 #if NBRIDGE > 0
932 if (ia == NULL && bridge_ia != NULL) {
933 ia = bridge_ia;
934 ifp = bridge_ia->ia_ifp;
935 }
936 #endif
937
938 if (ia == NULL) {
939 INADDR_TO_IA(isaddr, ia);
940 while ((ia != NULL) && ia->ia_ifp != m->m_pkthdr.rcvif)
941 NEXT_IA_WITH_SAME_ADDR(ia);
942
943 if (ia == NULL) {
944 IFP_TO_IA(ifp, ia);
945 if (ia == NULL) {
946 arpstat.as_rcvnoint++;
947 goto out;
948 }
949 }
950 }
951
952 myaddr = ia->ia_addr.sin_addr;
953
954 /* XXX checks for bridge case? */
955 if (!bcmp((caddr_t)ar_sha(ah), LLADDR(ifp->if_sadl),
956 ifp->if_addrlen)) {
957 arpstat.as_rcvlocalsha++;
958 goto out; /* it's from me, ignore it. */
959 }
960
961 /* XXX checks for bridge case? */
962 if (!memcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
963 arpstat.as_rcvbcastsha++;
964 log(LOG_ERR,
965 "%s: arp: link address is broadcast for IP address %s!\n",
966 ifp->if_xname, in_fmtaddr(isaddr));
967 goto out;
968 }
969
970 if (in_hosteq(isaddr, myaddr)) {
971 arpstat.as_rcvlocalspa++;
972 log(LOG_ERR,
973 "duplicate IP address %s sent from link address %s\n",
974 in_fmtaddr(isaddr), lla_snprintf(ar_sha(ah), ah->ar_hln));
975 itaddr = myaddr;
976 goto reply;
977 }
978 la = arplookup(m, &isaddr, in_hosteq(itaddr, myaddr), 0);
979 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
980 if (sdl->sdl_alen &&
981 bcmp((caddr_t)ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
982 if (rt->rt_flags & RTF_STATIC) {
983 arpstat.as_rcvoverperm++;
984 log(LOG_INFO,
985 "%s tried to overwrite permanent arp info"
986 " for %s\n",
987 lla_snprintf(ar_sha(ah), ah->ar_hln),
988 in_fmtaddr(isaddr));
989 goto out;
990 } else if (rt->rt_ifp != ifp) {
991 arpstat.as_rcvoverint++;
992 log(LOG_INFO,
993 "%s on %s tried to overwrite "
994 "arp info for %s on %s\n",
995 lla_snprintf(ar_sha(ah), ah->ar_hln),
996 ifp->if_xname, in_fmtaddr(isaddr),
997 rt->rt_ifp->if_xname);
998 goto out;
999 } else {
1000 arpstat.as_rcvover++;
1001 log(LOG_INFO,
1002 "arp info overwritten for %s by %s\n",
1003 in_fmtaddr(isaddr),
1004 lla_snprintf(ar_sha(ah), ah->ar_hln));
1005 }
1006 }
1007 /*
1008 * sanity check for the address length.
1009 * XXX this does not work for protocols with variable address
1010 * length. -is
1011 */
1012 if (sdl->sdl_alen &&
1013 sdl->sdl_alen != ah->ar_hln) {
1014 arpstat.as_rcvlenchg++;
1015 log(LOG_WARNING,
1016 "arp from %s: new addr len %d, was %d",
1017 in_fmtaddr(isaddr), ah->ar_hln, sdl->sdl_alen);
1018 }
1019 if (ifp->if_addrlen != ah->ar_hln) {
1020 arpstat.as_rcvbadlen++;
1021 log(LOG_WARNING,
1022 "arp from %s: addr len: new %d, i/f %d (ignored)",
1023 in_fmtaddr(isaddr), ah->ar_hln,
1024 ifp->if_addrlen);
1025 goto reply;
1026 }
1027 #if NTOKEN > 0
1028 /*
1029 * XXX uses m_data and assumes the complete answer including
1030 * XXX token-ring headers is in the same buf
1031 */
1032 if (ifp->if_type == IFT_ISO88025) {
1033 struct token_header *trh;
1034
1035 trh = (struct token_header *)M_TRHSTART(m);
1036 if (trh->token_shost[0] & TOKEN_RI_PRESENT) {
1037 struct token_rif *rif;
1038 size_t riflen;
1039
1040 rif = TOKEN_RIF(trh);
1041 riflen = (ntohs(rif->tr_rcf) &
1042 TOKEN_RCF_LEN_MASK) >> 8;
1043
1044 if (riflen > 2 &&
1045 riflen < sizeof(struct token_rif) &&
1046 (riflen & 1) == 0) {
1047 rif->tr_rcf ^= htons(TOKEN_RCF_DIRECTION);
1048 rif->tr_rcf &= htons(~TOKEN_RCF_BROADCAST_MASK);
1049 bcopy(rif, TOKEN_RIF(la), riflen);
1050 }
1051 }
1052 }
1053 #endif /* NTOKEN > 0 */
1054 bcopy((caddr_t)ar_sha(ah), LLADDR(sdl),
1055 sdl->sdl_alen = ah->ar_hln);
1056 if (rt->rt_expire)
1057 rt->rt_expire = time.tv_sec + arpt_keep;
1058 rt->rt_flags &= ~RTF_REJECT;
1059 la->la_asked = 0;
1060
1061 s = splnet();
1062 mold = la->la_hold;
1063 la->la_hold = 0;
1064 splx(s);
1065
1066 if (mold) {
1067 arpstat.as_dfrsent++;
1068 (*ifp->if_output)(ifp, mold, rt_key(rt), rt);
1069 }
1070 }
1071 reply:
1072 if (op != ARPOP_REQUEST) {
1073 if (op == ARPOP_REPLY)
1074 arpstat.as_rcvreply++;
1075 out:
1076 m_freem(m);
1077 return;
1078 }
1079 arpstat.as_rcvrequest++;
1080 if (in_hosteq(itaddr, myaddr)) {
1081 /* I am the target */
1082 tha = ar_tha(ah);
1083 if (tha)
1084 bcopy((caddr_t)ar_sha(ah), tha, ah->ar_hln);
1085 bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln);
1086 } else {
1087 la = arplookup(m, &itaddr, 0, SIN_PROXY);
1088 if (la == 0)
1089 goto out;
1090 rt = la->la_rt;
1091 if (rt->rt_ifp->if_type == IFT_CARP &&
1092 m->m_pkthdr.rcvif->if_type != IFT_CARP)
1093 goto out;
1094 tha = ar_tha(ah);
1095 if (tha)
1096 bcopy((caddr_t)ar_sha(ah), tha, ah->ar_hln);
1097 sdl = SDL(rt->rt_gateway);
1098 bcopy(LLADDR(sdl), (caddr_t)ar_sha(ah), ah->ar_hln);
1099 }
1100
1101 bcopy((caddr_t)ar_spa(ah), (caddr_t)ar_tpa(ah), ah->ar_pln);
1102 bcopy((caddr_t)&itaddr, (caddr_t)ar_spa(ah), ah->ar_pln);
1103 ah->ar_op = htons(ARPOP_REPLY);
1104 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
1105 switch (ifp->if_type) {
1106 case IFT_IEEE1394:
1107 /*
1108 * ieee1394 arp reply is broadcast
1109 */
1110 m->m_flags &= ~M_MCAST;
1111 m->m_flags |= M_BCAST;
1112 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + ah->ar_hln;
1113 break;
1114
1115 default:
1116 m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
1117 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
1118 break;
1119 }
1120 m->m_pkthdr.len = m->m_len;
1121 sa.sa_family = AF_ARP;
1122 sa.sa_len = 2;
1123 arpstat.as_sndtotal++;
1124 arpstat.as_sndreply++;
1125 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
1126 return;
1127 }
1128
1129 /*
1130 * Free an arp entry.
1131 */
1132 static void arptfree(struct llinfo_arp *la)
1133 {
1134 struct rtentry *rt = la->la_rt;
1135 struct sockaddr_dl *sdl;
1136
1137 ARP_LOCK_CHECK();
1138
1139 if (rt == 0)
1140 panic("arptfree");
1141 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
1142 sdl->sdl_family == AF_LINK) {
1143 sdl->sdl_alen = 0;
1144 la->la_asked = 0;
1145 rt->rt_flags &= ~RTF_REJECT;
1146 return;
1147 }
1148 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
1149 0, (struct rtentry **)0);
1150 }
1151
1152 /*
1153 * Lookup or enter a new address in arptab.
1154 */
1155 static struct llinfo_arp *
1156 arplookup(struct mbuf *m, struct in_addr *addr, int create, int proxy)
1157 {
1158 struct arphdr *ah;
1159 struct ifnet *ifp = m->m_pkthdr.rcvif;
1160 struct rtentry *rt;
1161 static struct sockaddr_inarp sin;
1162 const char *why = 0;
1163
1164 ah = mtod(m, struct arphdr *);
1165 sin.sin_len = sizeof(sin);
1166 sin.sin_family = AF_INET;
1167 sin.sin_addr = *addr;
1168 sin.sin_other = proxy ? SIN_PROXY : 0;
1169 rt = rtalloc1(sintosa(&sin), create);
1170 if (rt == 0)
1171 return (0);
1172 rt->rt_refcnt--;
1173
1174 if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) == RTF_LLINFO &&
1175 rt->rt_gateway->sa_family == AF_LINK)
1176 return ((struct llinfo_arp *)rt->rt_llinfo);
1177
1178
1179
1180 if (create) {
1181 if (rt->rt_flags & RTF_GATEWAY)
1182 why = "host is not on local network";
1183 else if ((rt->rt_flags & RTF_LLINFO) == 0) {
1184 arpstat.as_allocfail++;
1185 why = "could not allocate llinfo";
1186 } else
1187 why = "gateway route is not ours";
1188 log(LOG_DEBUG, "arplookup: unable to enter address"
1189 " for %s@%s on %s (%s)\n",
1190 in_fmtaddr(*addr), lla_snprintf(ar_sha(ah), ah->ar_hln),
1191 (ifp) ? ifp->if_xname : 0, why);
1192 if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_CLONED) != 0) {
1193 rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt),
1194 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
1195 }
1196 }
1197 return (0);
1198 }
1199
1200 int
1201 arpioctl(u_long cmd, caddr_t data)
1202 {
1203
1204 return (EOPNOTSUPP);
1205 }
1206
1207 void
1208 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1209 {
1210 struct in_addr *ip;
1211
1212 /*
1213 * Warn the user if another station has this IP address,
1214 * but only if the interface IP address is not zero.
1215 */
1216 ip = &IA_SIN(ifa)->sin_addr;
1217 if (!in_nullhost(*ip))
1218 arprequest(ifp, ip, ip, LLADDR(ifp->if_sadl));
1219
1220 ifa->ifa_rtrequest = arp_rtrequest;
1221 ifa->ifa_flags |= RTF_CLONING;
1222 }
1223
1224 /*
1225 * Called from 10 Mb/s Ethernet interrupt handlers
1226 * when ether packet type ETHERTYPE_REVARP
1227 * is received. Common length and type checks are done here,
1228 * then the protocol-specific routine is called.
1229 */
1230 void
1231 revarpinput(struct mbuf *m)
1232 {
1233 struct arphdr *ar;
1234
1235 if (m->m_len < sizeof(struct arphdr))
1236 goto out;
1237 ar = mtod(m, struct arphdr *);
1238 #if 0 /* XXX I don't think we need this... and it will prevent other LL */
1239 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
1240 goto out;
1241 #endif
1242 if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
1243 goto out;
1244 switch (ntohs(ar->ar_pro)) {
1245 case ETHERTYPE_IP:
1246 case ETHERTYPE_IPTRAILERS:
1247 in_revarpinput(m);
1248 return;
1249
1250 default:
1251 break;
1252 }
1253 out:
1254 m_freem(m);
1255 }
1256
1257 /*
1258 * RARP for Internet protocols on 10 Mb/s Ethernet.
1259 * Algorithm is that given in RFC 903.
1260 * We are only using for bootstrap purposes to get an ip address for one of
1261 * our interfaces. Thus we support no user-interface.
1262 *
1263 * Since the contents of the RARP reply are specific to the interface that
1264 * sent the request, this code must ensure that they are properly associated.
1265 *
1266 * Note: also supports ARP via RARP packets, per the RFC.
1267 */
1268 void
1269 in_revarpinput(struct mbuf *m)
1270 {
1271 struct ifnet *ifp;
1272 struct arphdr *ah;
1273 caddr_t tha;
1274 int op;
1275
1276 ah = mtod(m, struct arphdr *);
1277 op = ntohs(ah->ar_op);
1278
1279 switch (m->m_pkthdr.rcvif->if_type) {
1280 case IFT_IEEE1394:
1281 /* ARP without target hardware address is not supported */
1282 goto out;
1283 default:
1284 break;
1285 }
1286
1287 switch (op) {
1288 case ARPOP_REQUEST:
1289 case ARPOP_REPLY: /* per RFC */
1290 in_arpinput(m);
1291 return;
1292 case ARPOP_REVREPLY:
1293 break;
1294 case ARPOP_REVREQUEST: /* handled by rarpd(8) */
1295 default:
1296 goto out;
1297 }
1298 if (!revarp_in_progress)
1299 goto out;
1300 ifp = m->m_pkthdr.rcvif;
1301 if (ifp != myip_ifp) /* !same interface */
1302 goto out;
1303 if (myip_initialized)
1304 goto wake;
1305 tha = ar_tha(ah);
1306 KASSERT(tha);
1307 if (bcmp(tha, LLADDR(ifp->if_sadl), ifp->if_sadl->sdl_alen))
1308 goto out;
1309 bcopy((caddr_t)ar_spa(ah), (caddr_t)&srv_ip, sizeof(srv_ip));
1310 bcopy((caddr_t)ar_tpa(ah), (caddr_t)&myip, sizeof(myip));
1311 myip_initialized = 1;
1312 wake: /* Do wakeup every time in case it was missed. */
1313 wakeup((caddr_t)&myip);
1314
1315 out:
1316 m_freem(m);
1317 }
1318
1319 /*
1320 * Send a RARP request for the ip address of the specified interface.
1321 * The request should be RFC 903-compliant.
1322 */
1323 void
1324 revarprequest(struct ifnet *ifp)
1325 {
1326 struct sockaddr sa;
1327 struct mbuf *m;
1328 struct arphdr *ah;
1329 caddr_t tha;
1330
1331 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
1332 return;
1333 MCLAIM(m, &arpdomain.dom_mowner);
1334 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
1335 2*ifp->if_addrlen;
1336 m->m_pkthdr.len = m->m_len;
1337 MH_ALIGN(m, m->m_len);
1338 ah = mtod(m, struct arphdr *);
1339 bzero((caddr_t)ah, m->m_len);
1340 ah->ar_pro = htons(ETHERTYPE_IP);
1341 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
1342 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
1343 ah->ar_op = htons(ARPOP_REVREQUEST);
1344
1345 bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln);
1346 tha = ar_tha(ah);
1347 KASSERT(tha);
1348 bcopy(LLADDR(ifp->if_sadl), tha, ah->ar_hln);
1349
1350 sa.sa_family = AF_ARP;
1351 sa.sa_len = 2;
1352 m->m_flags |= M_BCAST;
1353 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
1354
1355 }
1356
1357 /*
1358 * RARP for the ip address of the specified interface, but also
1359 * save the ip address of the server that sent the answer.
1360 * Timeout if no response is received.
1361 */
1362 int
1363 revarpwhoarewe(struct ifnet *ifp, struct in_addr *serv_in,
1364 struct in_addr *clnt_in)
1365 {
1366 int result, count = 20;
1367
1368 myip_initialized = 0;
1369 myip_ifp = ifp;
1370
1371 revarp_in_progress = 1;
1372 while (count--) {
1373 revarprequest(ifp);
1374 result = tsleep((caddr_t)&myip, PSOCK, "revarp", hz/2);
1375 if (result != EWOULDBLOCK)
1376 break;
1377 }
1378 revarp_in_progress = 0;
1379
1380 if (!myip_initialized)
1381 return ENETUNREACH;
1382
1383 bcopy((caddr_t)&srv_ip, serv_in, sizeof(*serv_in));
1384 bcopy((caddr_t)&myip, clnt_in, sizeof(*clnt_in));
1385 return 0;
1386 }
1387
1388
1389
1391 #ifdef DDB
1392
1393 #include <machine/db_machdep.h>
1394 #include <ddb/db_interface.h>
1395 #include <ddb/db_output.h>
1396
1397 static void
1398 db_print_sa(const struct sockaddr *sa)
1399 {
1400 int len;
1401 const u_char *p;
1402
1403 if (sa == 0) {
1404 db_printf("[NULL]");
1405 return;
1406 }
1407
1408 p = (const u_char *)sa;
1409 len = sa->sa_len;
1410 db_printf("[");
1411 while (len > 0) {
1412 db_printf("%d", *p);
1413 p++; len--;
1414 if (len) db_printf(",");
1415 }
1416 db_printf("]\n");
1417 }
1418
1419 static void
1420 db_print_ifa(struct ifaddr *ifa)
1421 {
1422 if (ifa == 0)
1423 return;
1424 db_printf(" ifa_addr=");
1425 db_print_sa(ifa->ifa_addr);
1426 db_printf(" ifa_dsta=");
1427 db_print_sa(ifa->ifa_dstaddr);
1428 db_printf(" ifa_mask=");
1429 db_print_sa(ifa->ifa_netmask);
1430 db_printf(" flags=0x%x,refcnt=%d,metric=%d\n",
1431 ifa->ifa_flags,
1432 ifa->ifa_refcnt,
1433 ifa->ifa_metric);
1434 }
1435
1436 static void
1437 db_print_llinfo(caddr_t li)
1438 {
1439 struct llinfo_arp *la;
1440
1441 if (li == 0)
1442 return;
1443 la = (struct llinfo_arp *)li;
1444 db_printf(" la_rt=%p la_hold=%p, la_asked=0x%lx\n",
1445 la->la_rt, la->la_hold, la->la_asked);
1446 }
1447
1448 /*
1449 * Function to pass to rn_walktree().
1450 * Return non-zero error to abort walk.
1451 */
1452 static int
1453 db_show_radix_node(struct radix_node *rn, void *w)
1454 {
1455 struct rtentry *rt = (struct rtentry *)rn;
1456
1457 db_printf("rtentry=%p", rt);
1458
1459 db_printf(" flags=0x%x refcnt=%d use=%ld expire=%ld\n",
1460 rt->rt_flags, rt->rt_refcnt,
1461 rt->rt_use, rt->rt_expire);
1462
1463 db_printf(" key="); db_print_sa(rt_key(rt));
1464 db_printf(" mask="); db_print_sa(rt_mask(rt));
1465 db_printf(" gw="); db_print_sa(rt->rt_gateway);
1466
1467 db_printf(" ifp=%p ", rt->rt_ifp);
1468 if (rt->rt_ifp)
1469 db_printf("(%s)", rt->rt_ifp->if_xname);
1470 else
1471 db_printf("(NULL)");
1472
1473 db_printf(" ifa=%p\n", rt->rt_ifa);
1474 db_print_ifa(rt->rt_ifa);
1475
1476 db_printf(" genmask="); db_print_sa(rt->rt_genmask);
1477
1478 db_printf(" gwroute=%p llinfo=%p\n",
1479 rt->rt_gwroute, rt->rt_llinfo);
1480 db_print_llinfo(rt->rt_llinfo);
1481
1482 return (0);
1483 }
1484
1485 /*
1486 * Function to print all the route trees.
1487 * Use this from ddb: "show arptab"
1488 */
1489 void
1490 db_show_arptab(db_expr_t addr, int have_addr, db_expr_t count, const char *modif)
1491 {
1492 struct radix_node_head *rnh;
1493 rnh = rt_tables[AF_INET];
1494 db_printf("Route tree for AF_INET\n");
1495 if (rnh == NULL) {
1496 db_printf(" (not initialized)\n");
1497 return;
1498 }
1499 rn_walktree(rnh, db_show_radix_node, NULL);
1500 return;
1501 }
1502 #endif
1503
1504 SYSCTL_SETUP(sysctl_net_inet_arp_setup, "sysctl net.inet.arp subtree setup")
1505 {
1506 const struct sysctlnode *node;
1507
1508 sysctl_createv(clog, 0, NULL, NULL,
1509 CTLFLAG_PERMANENT,
1510 CTLTYPE_NODE, "net", NULL,
1511 NULL, 0, NULL, 0,
1512 CTL_NET, CTL_EOL);
1513 sysctl_createv(clog, 0, NULL, NULL,
1514 CTLFLAG_PERMANENT,
1515 CTLTYPE_NODE, "inet", NULL,
1516 NULL, 0, NULL, 0,
1517 CTL_NET, PF_INET, CTL_EOL);
1518 sysctl_createv(clog, 0, NULL, &node,
1519 CTLFLAG_PERMANENT,
1520 CTLTYPE_NODE, "arp",
1521 SYSCTL_DESCR("Address Resolution Protocol"),
1522 NULL, 0, NULL, 0,
1523 CTL_NET, PF_INET, CTL_CREATE, CTL_EOL);
1524
1525 sysctl_createv(clog, 0, NULL, NULL,
1526 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1527 CTLTYPE_INT, "prune",
1528 SYSCTL_DESCR("ARP cache pruning interval"),
1529 NULL, 0, &arpt_prune, 0,
1530 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
1531
1532 sysctl_createv(clog, 0, NULL, NULL,
1533 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1534 CTLTYPE_INT, "keep",
1535 SYSCTL_DESCR("Valid ARP entry lifetime"),
1536 NULL, 0, &arpt_keep, 0,
1537 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
1538
1539 sysctl_createv(clog, 0, NULL, NULL,
1540 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1541 CTLTYPE_INT, "down",
1542 SYSCTL_DESCR("Failed ARP entry lifetime"),
1543 NULL, 0, &arpt_down, 0,
1544 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
1545
1546 sysctl_createv(clog, 0, NULL, NULL,
1547 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1548 CTLTYPE_INT, "refresh",
1549 SYSCTL_DESCR("ARP entry refresh interval"),
1550 NULL, 0, &arpt_refresh, 0,
1551 CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
1552 }
1553
1554 #endif /* INET */
1555