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