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