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