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