2 * Copyright (c) 2004, 2005 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of The DragonFly Project nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific, prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1982, 1986, 1988, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * @(#)if_ether.c 8.1 (Berkeley) 6/10/93
66 * $FreeBSD: src/sys/netinet/if_ether.c,v 1.64.2.23 2003/04/11 07:23:15 fjoe Exp $
67 * $DragonFly: src/sys/netinet/if_ether.c,v 1.48 2008/05/31 08:29:05 sephe Exp $
71 * Ethernet address resolution protocol.
73 * add "inuse/lock" bit (or ref. count) along with valid bit
79 #include <sys/param.h>
80 #include <sys/kernel.h>
81 #include <sys/queue.h>
82 #include <sys/sysctl.h>
83 #include <sys/systm.h>
85 #include <sys/malloc.h>
86 #include <sys/socket.h>
87 #include <sys/syslog.h>
90 #include <net/if_dl.h>
91 #include <net/if_types.h>
92 #include <net/route.h>
93 #include <net/netisr.h>
94 #include <net/if_llc.h>
96 #include <netinet/in.h>
97 #include <netinet/in_var.h>
98 #include <netinet/if_ether.h>
100 #include <sys/thread2.h>
101 #include <sys/msgport2.h>
102 #include <net/netmsg2.h>
106 #include <netinet/ip_carp.h>
109 #define SIN(s) ((struct sockaddr_in *)s)
110 #define SDL(s) ((struct sockaddr_dl *)s)
112 SYSCTL_DECL(_net_link_ether
);
113 SYSCTL_NODE(_net_link_ether
, PF_INET
, inet
, CTLFLAG_RW
, 0, "");
116 static int arpt_prune
= (5*60*1); /* walk list every 5 minutes */
117 static int arpt_keep
= (20*60); /* once resolved, good for 20 more minutes */
118 static int arpt_down
= 20; /* once declared down, don't send for 20 sec */
120 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, prune_intvl
, CTLFLAG_RW
,
122 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, max_age
, CTLFLAG_RW
,
124 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, host_down_time
, CTLFLAG_RW
,
127 #define rt_expire rt_rmx.rmx_expire
130 LIST_ENTRY(llinfo_arp
) la_le
;
131 struct rtentry
*la_rt
;
132 struct mbuf
*la_hold
; /* last packet until resolved/timeout */
133 u_short la_preempt
; /* countdown for pre-expiry arps */
134 u_short la_asked
; /* #times we QUERIED following expiration */
137 static LIST_HEAD(, llinfo_arp
) llinfo_arp_list
[MAXCPU
];
139 static int arp_maxtries
= 5;
140 static int useloopback
= 1; /* use loopback interface for local traffic */
141 static int arp_proxyall
= 0;
143 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, maxtries
, CTLFLAG_RW
,
144 &arp_maxtries
, 0, "");
145 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, useloopback
, CTLFLAG_RW
,
146 &useloopback
, 0, "");
147 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, proxyall
, CTLFLAG_RW
,
148 &arp_proxyall
, 0, "");
150 void arprequest_acces(struct ifnet
*ifp
, struct in_addr
*sip
, struct in_addr
*tip
, u_char
*enaddr
);
151 static void arp_rtrequest (int, struct rtentry
*, struct rt_addrinfo
*);
152 static void arprequest (struct ifnet
*,
153 struct in_addr
*, struct in_addr
*, u_char
*);
154 static void arpintr(struct netmsg
*);
155 static void arptfree (struct llinfo_arp
*);
156 static void arptimer (void *);
157 static struct llinfo_arp
158 *arplookup (in_addr_t addr
, boolean_t create
, boolean_t proxy
);
160 static void in_arpinput (struct mbuf
*);
163 static struct callout arptimer_ch
[MAXCPU
];
166 * Timeout routine. Age arp_tab entries periodically.
170 arptimer(void *ignored_arg
)
172 struct llinfo_arp
*la
, *nla
;
175 LIST_FOREACH_MUTABLE(la
, &llinfo_arp_list
[mycpuid
], la_le
, nla
) {
176 if (la
->la_rt
->rt_expire
&& la
->la_rt
->rt_expire
<= time_second
)
179 callout_reset(&arptimer_ch
[mycpuid
], arpt_prune
* hz
, arptimer
, NULL
);
184 * Parallel to llc_rtrequest.
187 arp_rtrequest(int req
, struct rtentry
*rt
, struct rt_addrinfo
*info
)
189 struct sockaddr
*gate
= rt
->rt_gateway
;
190 struct llinfo_arp
*la
= rt
->rt_llinfo
;
192 struct sockaddr_dl null_sdl
= { sizeof null_sdl
, AF_LINK
};
193 static boolean_t arpinit_done
[MAXCPU
];
195 if (!arpinit_done
[mycpuid
]) {
196 arpinit_done
[mycpuid
] = TRUE
;
197 callout_init(&arptimer_ch
[mycpuid
]);
198 callout_reset(&arptimer_ch
[mycpuid
], hz
, arptimer
, NULL
);
200 if (rt
->rt_flags
& RTF_GATEWAY
)
206 * XXX: If this is a manually added route to interface
207 * such as older version of routed or gated might provide,
208 * restore cloning bit.
210 if (!(rt
->rt_flags
& RTF_HOST
) &&
211 SIN(rt_mask(rt
))->sin_addr
.s_addr
!= 0xffffffff)
212 rt
->rt_flags
|= RTF_CLONING
;
213 if (rt
->rt_flags
& RTF_CLONING
) {
215 * Case 1: This route should come from a route to iface.
217 rt_setgate(rt
, rt_key(rt
),
218 (struct sockaddr
*)&null_sdl
);
219 gate
= rt
->rt_gateway
;
220 SDL(gate
)->sdl_type
= rt
->rt_ifp
->if_type
;
221 SDL(gate
)->sdl_index
= rt
->rt_ifp
->if_index
;
222 rt
->rt_expire
= time_second
;
225 /* Announce a new entry if requested. */
226 if (rt
->rt_flags
& RTF_ANNOUNCE
) {
227 arprequest(rt
->rt_ifp
,
228 &SIN(rt_key(rt
))->sin_addr
,
229 &SIN(rt_key(rt
))->sin_addr
,
234 if (gate
->sa_family
!= AF_LINK
||
235 gate
->sa_len
< sizeof(struct sockaddr_dl
)) {
236 log(LOG_DEBUG
, "arp_rtrequest: bad gateway value\n");
239 SDL(gate
)->sdl_type
= rt
->rt_ifp
->if_type
;
240 SDL(gate
)->sdl_index
= rt
->rt_ifp
->if_index
;
242 break; /* This happens on a route change */
244 * Case 2: This route may come from cloning, or a manual route
245 * add with a LL address.
247 R_Malloc(la
, struct llinfo_arp
*, sizeof *la
);
250 log(LOG_DEBUG
, "arp_rtrequest: malloc failed\n");
253 bzero(la
, sizeof *la
);
255 rt
->rt_flags
|= RTF_LLINFO
;
256 LIST_INSERT_HEAD(&llinfo_arp_list
[mycpuid
], la
, la_le
);
260 * This keeps the multicast addresses from showing up
261 * in `arp -a' listings as unresolved. It's not actually
262 * functional. Then the same for broadcast.
264 if (IN_MULTICAST(ntohl(SIN(rt_key(rt
))->sin_addr
.s_addr
))) {
265 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt
))->sin_addr
,
267 SDL(gate
)->sdl_alen
= 6;
270 if (in_broadcast(SIN(rt_key(rt
))->sin_addr
, rt
->rt_ifp
)) {
271 memcpy(LLADDR(SDL(gate
)), rt
->rt_ifp
->if_broadcastaddr
,
272 rt
->rt_ifp
->if_addrlen
);
273 SDL(gate
)->sdl_alen
= rt
->rt_ifp
->if_addrlen
;
278 if (SIN(rt_key(rt
))->sin_addr
.s_addr
==
279 (IA_SIN(rt
->rt_ifa
))->sin_addr
.s_addr
) {
281 * This test used to be
282 * if (loif.if_flags & IFF_UP)
283 * It allowed local traffic to be forced
284 * through the hardware by configuring the
285 * loopback down. However, it causes problems
286 * during network configuration for boards
287 * that can't receive packets they send. It
288 * is now necessary to clear "useloopback" and
289 * remove the route to force traffic out to
293 bcopy(IF_LLADDR(rt
->rt_ifp
), LLADDR(SDL(gate
)),
294 SDL(gate
)->sdl_alen
= rt
->rt_ifp
->if_addrlen
);
303 LIST_REMOVE(la
, la_le
);
304 rt
->rt_llinfo
= NULL
;
305 rt
->rt_flags
&= ~RTF_LLINFO
;
306 if (la
->la_hold
!= NULL
)
307 m_freem(la
->la_hold
);
313 * Broadcast an ARP request. Caller specifies:
314 * - arp header source ip address
315 * - arp header target ip address
316 * - arp header source ethernet address
319 arprequest(struct ifnet
*ifp
, struct in_addr
*sip
, struct in_addr
*tip
,
323 struct ether_header
*eh
;
328 if ((m
= m_gethdr(MB_DONTWAIT
, MT_DATA
)) == NULL
)
330 m
->m_pkthdr
.rcvif
= (struct ifnet
*)NULL
;
332 switch (ifp
->if_type
) {
335 * This may not be correct for types not explicitly
336 * listed, but this is our best guess
339 ar_hrd
= htons(ARPHRD_ETHER
);
341 m
->m_len
= arphdr_len2(ifp
->if_addrlen
, sizeof(struct in_addr
));
342 m
->m_pkthdr
.len
= m
->m_len
;
343 MH_ALIGN(m
, m
->m_len
);
345 eh
= (struct ether_header
*)sa
.sa_data
;
346 /* if_output() will not swap */
347 eh
->ether_type
= htons(ETHERTYPE_ARP
);
348 memcpy(eh
->ether_dhost
, ifp
->if_broadcastaddr
, ifp
->if_addrlen
);
350 ah
= mtod(m
, struct arphdr
*);
355 ah
->ar_pro
= htons(ETHERTYPE_IP
);
356 ah
->ar_hln
= ifp
->if_addrlen
; /* hardware address length */
357 ah
->ar_pln
= sizeof(struct in_addr
); /* protocol address length */
358 ah
->ar_op
= htons(ARPOP_REQUEST
);
359 memcpy(ar_sha(ah
), enaddr
, ah
->ar_hln
);
360 memset(ar_tha(ah
), 0, ah
->ar_hln
);
361 memcpy(ar_spa(ah
), sip
, ah
->ar_pln
);
362 memcpy(ar_tpa(ah
), tip
, ah
->ar_pln
);
364 sa
.sa_family
= AF_UNSPEC
;
365 sa
.sa_len
= sizeof sa
;
366 ifp
->if_output(ifp
, m
, &sa
, (struct rtentry
*)NULL
);
370 * Resolve an IP address into an ethernet address. If success,
371 * desten is filled in. If there is no entry in arptab,
372 * set one up and broadcast a request for the IP address.
373 * Hold onto this mbuf and resend it once the address
374 * is finally resolved. A return value of 1 indicates
375 * that desten has been filled in and the packet should be sent
376 * normally; a 0 return indicates that the packet has been
377 * taken over here, either now or for later transmission.
384 struct sockaddr
*dst
,
388 struct llinfo_arp
*la
= NULL
;
389 struct sockaddr_dl
*sdl
;
391 if (m
->m_flags
& M_BCAST
) { /* broadcast */
392 memcpy(desten
, ifp
->if_broadcastaddr
, ifp
->if_addrlen
);
395 if (m
->m_flags
& M_MCAST
) {/* multicast */
396 ETHER_MAP_IP_MULTICAST(&SIN(dst
)->sin_addr
, desten
);
400 if (rt_llroute(dst
, rt0
, &rt
) != 0) {
407 la
= arplookup(SIN(dst
)->sin_addr
.s_addr
, TRUE
, FALSE
);
411 if (la
== NULL
|| rt
== NULL
) {
412 log(LOG_DEBUG
, "arpresolve: can't allocate llinfo for %s%s%s\n",
413 inet_ntoa(SIN(dst
)->sin_addr
), la
? "la" : " ",
418 sdl
= SDL(rt
->rt_gateway
);
420 * Check the address family and length is valid, the address
421 * is resolved; otherwise, try to resolve.
423 if ((rt
->rt_expire
== 0 || rt
->rt_expire
> time_second
) &&
424 sdl
->sdl_family
== AF_LINK
&& sdl
->sdl_alen
!= 0) {
426 * If entry has an expiry time and it is approaching,
427 * see if we need to send an ARP request within this
428 * arpt_down interval.
430 if ((rt
->rt_expire
!= 0) &&
431 (time_second
+ la
->la_preempt
> rt
->rt_expire
)) {
433 &SIN(rt
->rt_ifa
->ifa_addr
)->sin_addr
,
439 bcopy(LLADDR(sdl
), desten
, sdl
->sdl_alen
);
443 * If ARP is disabled on this interface, stop.
445 * Probably should not allocate empty llinfo struct if we are
446 * not going to be sending out an arp request.
448 if (ifp
->if_flags
& IFF_NOARP
) {
453 * There is an arptab entry, but no ethernet address
454 * response yet. Replace the held mbuf with this
457 if (la
->la_hold
!= NULL
)
458 m_freem(la
->la_hold
);
460 if (rt
->rt_expire
|| ((rt
->rt_flags
& RTF_STATIC
) && !sdl
->sdl_alen
)) {
461 rt
->rt_flags
&= ~RTF_REJECT
;
462 if (la
->la_asked
== 0 || rt
->rt_expire
!= time_second
) {
463 rt
->rt_expire
= time_second
;
464 if (la
->la_asked
++ < arp_maxtries
) {
466 &SIN(rt
->rt_ifa
->ifa_addr
)->sin_addr
,
470 rt
->rt_flags
|= RTF_REJECT
;
471 rt
->rt_expire
+= arpt_down
;
473 la
->la_preempt
= arp_maxtries
;
482 * Common length and type checks are done here,
483 * then the protocol-specific routine is called.
486 arpintr(struct netmsg
*msg
)
488 struct mbuf
*m
= ((struct netmsg_packet
*)msg
)->nm_packet
;
492 if (m
->m_len
< sizeof(struct arphdr
) &&
493 (m
= m_pullup(m
, sizeof(struct arphdr
))) == NULL
) {
494 log(LOG_ERR
, "arp: runt packet -- m_pullup failed\n");
497 ar
= mtod(m
, struct arphdr
*);
499 ar_hrd
= ntohs(ar
->ar_hrd
);
500 if (ar_hrd
!= ARPHRD_ETHER
&& ar_hrd
!= ARPHRD_IEEE802
) {
501 log(LOG_ERR
, "arp: unknown hardware address format (0x%2D)\n",
502 (unsigned char *)&ar
->ar_hrd
, "");
507 if (m
->m_pkthdr
.len
< arphdr_len(ar
)) {
508 if ((m
= m_pullup(m
, arphdr_len(ar
))) == NULL
) {
509 log(LOG_ERR
, "arp: runt packet\n");
512 ar
= mtod(m
, struct arphdr
*);
515 switch (ntohs(ar
->ar_pro
)) {
523 /* msg was embedded in the mbuf, do not reply! */
528 * ARP for Internet protocols on 10 Mb/s Ethernet.
529 * Algorithm is that given in RFC 826.
530 * In addition, a sanity check is performed on the sender
531 * protocol address, to catch impersonators.
532 * We no longer handle negotiations for use of trailer protocol:
533 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
534 * along with IP replies if we wanted trailers sent to us,
535 * and also sent them in response to IP replies.
536 * This allowed either end to announce the desire to receive
538 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
539 * but formerly didn't normally send requests.
541 static int log_arp_wrong_iface
= 1;
542 SYSCTL_INT(_net_link_ether_inet
, OID_AUTO
, log_arp_wrong_iface
, CTLFLAG_RW
,
543 &log_arp_wrong_iface
, 0,
544 "log arp packets arriving on the wrong interface");
547 arp_update_oncpu(struct mbuf
*m
, in_addr_t saddr
, boolean_t create
,
550 struct arphdr
*ah
= mtod(m
, struct arphdr
*);
551 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
552 struct llinfo_arp
*la
;
553 struct sockaddr_dl
*sdl
;
557 la
= arplookup(saddr
, create
, FALSE
);
558 if (la
&& (rt
= la
->la_rt
) && (sdl
= SDL(rt
->rt_gateway
))) {
559 struct in_addr isaddr
= { saddr
};
561 /* the following is not an error when doing bridging */
562 if (rt
->rt_ifp
!= ifp
) {
563 if (dologging
&& log_arp_wrong_iface
&& cpu
== 0) {
566 "but got reply from %*D on %s\n",
568 rt
->rt_ifp
->if_xname
,
569 ifp
->if_addrlen
, (u_char
*)ar_sha(ah
), ":",
575 bcmp(ar_sha(ah
), LLADDR(sdl
), sdl
->sdl_alen
)) {
576 if (rt
->rt_expire
!= 0) {
577 if (dologging
&& cpu
== 0) {
579 "arp: %s moved from %*D to %*D on %s\n",
581 ifp
->if_addrlen
, (u_char
*)LLADDR(sdl
),
582 ":", ifp
->if_addrlen
,
583 (u_char
*)ar_sha(ah
), ":",
587 if (dologging
&& cpu
== 0) {
589 "arp: %*D attempts to modify permanent entry for %s on %s\n",
590 ifp
->if_addrlen
, (u_char
*)ar_sha(ah
), ":",
591 inet_ntoa(isaddr
), ifp
->if_xname
);
597 * sanity check for the address length.
598 * XXX this does not work for protocols with variable address
601 if (dologging
&& sdl
->sdl_alen
&& sdl
->sdl_alen
!= ah
->ar_hln
&&
605 "arp from %*D: new addr len %d, was %d",
606 ifp
->if_addrlen
, (u_char
*) ar_sha(ah
), ":",
607 ah
->ar_hln
, sdl
->sdl_alen
);
609 if (ifp
->if_addrlen
!= ah
->ar_hln
) {
610 if (dologging
&& cpu
== 0) {
612 "arp from %*D: addr len: new %d, i/f %d (ignored)",
613 ifp
->if_addrlen
, (u_char
*) ar_sha(ah
), ":",
614 ah
->ar_hln
, ifp
->if_addrlen
);
618 memcpy(LLADDR(sdl
), ar_sha(ah
), sdl
->sdl_alen
= ah
->ar_hln
);
619 if (rt
->rt_expire
!= 0)
620 rt
->rt_expire
= time_second
+ arpt_keep
;
621 rt
->rt_flags
&= ~RTF_REJECT
;
623 la
->la_preempt
= arp_maxtries
;
626 * This particular cpu might have been holding an mbuf
627 * pending ARP resolution. If so, transmit the mbuf now.
629 if (la
->la_hold
!= NULL
) {
630 m_adj(la
->la_hold
, sizeof(struct ether_header
));
631 ifp
->if_output(ifp
, la
->la_hold
, rt_key(rt
), rt
);
639 struct netmsg_arp_update
{
640 struct netmsg netmsg
;
646 static void arp_update_msghandler(struct netmsg
*netmsg
);
651 * Called from arpintr() - this routine is run from a single cpu.
654 in_arpinput(struct mbuf
*m
)
657 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
658 struct ether_header
*eh
;
660 struct ifaddr_container
*ifac
;
661 struct in_ifaddr
*ia
;
663 struct in_addr isaddr
, itaddr
, myaddr
;
665 struct netmsg_arp_update msg
;
667 u_int8_t
*enaddr
= NULL
;
671 req_len
= arphdr_len2(ifp
->if_addrlen
, sizeof(struct in_addr
));
672 if (m
->m_len
< req_len
&& (m
= m_pullup(m
, req_len
)) == NULL
) {
673 log(LOG_ERR
, "in_arp: runt packet -- m_pullup failed\n");
677 ah
= mtod(m
, struct arphdr
*);
678 op
= ntohs(ah
->ar_op
);
679 memcpy(&isaddr
, ar_spa(ah
), sizeof isaddr
);
680 memcpy(&itaddr
, ar_tpa(ah
), sizeof itaddr
);
682 * Check both target and sender IP addresses:
684 * If we receive the packet on the interface owning the address,
685 * then accept the address.
687 * For a bridge, we accept the address if the receive interface and
688 * the interface owning the address are on the same bridge.
689 * (This will change slightly when we have clusters of interfaces).
691 LIST_FOREACH(ia
, INADDR_HASH(itaddr
.s_addr
), ia_hash
) {
692 /* Skip all ia's which don't match */
693 if (itaddr
.s_addr
!= ia
->ia_addr
.sin_addr
.s_addr
)
696 if (ia
->ia_ifp
== ifp
)
699 if (ifp
->if_bridge
&& ia
->ia_ifp
&&
700 ifp
->if_bridge
== ia
->ia_ifp
->if_bridge
)
705 * If the interface does not match, but the recieving interface
706 * is part of carp, we call carp_iamatch to see if this is a
707 * request for the virtual host ip.
708 * XXX: This is really ugly!
710 if (ifp
->if_carp
!= NULL
&&
711 carp_iamatch(ifp
->if_carp
, ia
, &isaddr
, &enaddr
) &&
712 itaddr
.s_addr
== ia
->ia_addr
.sin_addr
.s_addr
)
716 LIST_FOREACH(ia
, INADDR_HASH(isaddr
.s_addr
), ia_hash
) {
717 /* Skip all ia's which don't match */
718 if (isaddr
.s_addr
!= ia
->ia_addr
.sin_addr
.s_addr
)
721 if (ia
->ia_ifp
== ifp
)
724 if (ifp
->if_bridge
&& ia
->ia_ifp
&&
725 ifp
->if_bridge
== ia
->ia_ifp
->if_bridge
)
729 * No match, use the first inet address on the receive interface
730 * as a dummy address for the rest of the function.
732 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
733 struct ifaddr
*ifa
= ifac
->ifa
;
735 if (ifa
->ifa_addr
&& ifa
->ifa_addr
->sa_family
== AF_INET
) {
741 * If we got here, we didn't find any suitable interface,
742 * so drop the packet.
749 enaddr
= (u_int8_t
*)IF_LLADDR(ifp
);
750 myaddr
= ia
->ia_addr
.sin_addr
;
751 if (!bcmp(ar_sha(ah
), enaddr
, ifp
->if_addrlen
)) {
752 m_freem(m
); /* it's from me, ignore it. */
755 if (!bcmp(ar_sha(ah
), ifp
->if_broadcastaddr
, ifp
->if_addrlen
)) {
757 "arp: link address is broadcast for IP address %s!\n",
762 if (isaddr
.s_addr
== myaddr
.s_addr
&& myaddr
.s_addr
!= 0) {
764 "arp: %*D is using my IP address %s!\n",
765 ifp
->if_addrlen
, (u_char
*)ar_sha(ah
), ":",
771 netmsg_init(&msg
.netmsg
, &curthread
->td_msgport
, 0,
772 arp_update_msghandler
);
774 msg
.saddr
= isaddr
.s_addr
;
775 msg
.create
= (itaddr
.s_addr
== myaddr
.s_addr
);
776 lwkt_domsg(rtable_portfn(0), &msg
.netmsg
.nm_lmsg
, 0);
778 arp_update_oncpu(m
, isaddr
.s_addr
, (itaddr
.s_addr
== myaddr
.s_addr
),
781 if (op
!= ARPOP_REQUEST
) {
785 if (itaddr
.s_addr
== myaddr
.s_addr
) {
786 /* I am the target */
787 memcpy(ar_tha(ah
), ar_sha(ah
), ah
->ar_hln
);
788 memcpy(ar_sha(ah
), enaddr
, ah
->ar_hln
);
790 struct llinfo_arp
*la
;
792 la
= arplookup(itaddr
.s_addr
, FALSE
, SIN_PROXY
);
794 struct sockaddr_in sin
;
801 bzero(&sin
, sizeof sin
);
802 sin
.sin_family
= AF_INET
;
803 sin
.sin_len
= sizeof sin
;
804 sin
.sin_addr
= itaddr
;
806 rt
= rtpurelookup((struct sockaddr
*)&sin
);
813 * Don't send proxies for nodes on the same interface
814 * as this one came out of, or we'll get into a fight
815 * over who claims what Ether address.
817 if (rt
->rt_ifp
== ifp
) {
821 memcpy(ar_tha(ah
), ar_sha(ah
), ah
->ar_hln
);
822 memcpy(ar_sha(ah
), enaddr
, ah
->ar_hln
);
824 kprintf("arp: proxying for %s\n", inet_ntoa(itaddr
));
827 struct sockaddr_dl
*sdl
;
830 memcpy(ar_tha(ah
), ar_sha(ah
), ah
->ar_hln
);
831 sdl
= SDL(rt
->rt_gateway
);
832 memcpy(ar_sha(ah
), LLADDR(sdl
), ah
->ar_hln
);
836 memcpy(ar_tpa(ah
), ar_spa(ah
), ah
->ar_pln
);
837 memcpy(ar_spa(ah
), &itaddr
, ah
->ar_pln
);
838 ah
->ar_op
= htons(ARPOP_REPLY
);
839 ah
->ar_pro
= htons(ETHERTYPE_IP
); /* let's be sure! */
840 switch (ifp
->if_type
) {
843 * May not be correct for types not explictly
844 * listed, but it is our best guess.
847 eh
= (struct ether_header
*)sa
.sa_data
;
848 memcpy(eh
->ether_dhost
, ar_tha(ah
), sizeof eh
->ether_dhost
);
849 eh
->ether_type
= htons(ETHERTYPE_ARP
);
852 sa
.sa_family
= AF_UNSPEC
;
853 sa
.sa_len
= sizeof sa
;
854 ifp
->if_output(ifp
, m
, &sa
, (struct rtentry
*)0);
862 arp_update_msghandler(struct netmsg
*netmsg
)
864 struct netmsg_arp_update
*msg
= (struct netmsg_arp_update
*)netmsg
;
867 arp_update_oncpu(msg
->m
, msg
->saddr
, msg
->create
, FALSE
);
869 nextcpu
= mycpuid
+ 1;
870 if (nextcpu
< ncpus
) {
871 lwkt_forwardmsg(rtable_portfn(nextcpu
), &msg
->netmsg
.nm_lmsg
);
873 lwkt_replymsg(&msg
->netmsg
.nm_lmsg
, 0);
882 * Free an arp entry. If the arp entry is actively referenced or represents
883 * a static entry we only clear it back to an unresolved state, otherwise
884 * we destroy the entry entirely.
886 * Note that static entries are created when route add ... -interface is used
887 * to create an interface route to a (direct) destination.
890 arptfree(struct llinfo_arp
*la
)
892 struct rtentry
*rt
= la
->la_rt
;
893 struct sockaddr_dl
*sdl
;
897 sdl
= SDL(rt
->rt_gateway
);
899 ((rt
->rt_refcnt
> 0 && sdl
->sdl_family
== AF_LINK
) ||
900 (rt
->rt_flags
& RTF_STATIC
))) {
902 la
->la_preempt
= la
->la_asked
= 0;
903 rt
->rt_flags
&= ~RTF_REJECT
;
906 rtrequest(RTM_DELETE
, rt_key(rt
), NULL
, rt_mask(rt
), 0, NULL
);
910 * Lookup or enter a new address in arptab.
912 static struct llinfo_arp
*
913 arplookup(in_addr_t addr
, boolean_t create
, boolean_t proxy
)
916 struct sockaddr_inarp sin
= { sizeof sin
, AF_INET
};
917 const char *why
= NULL
;
919 sin
.sin_addr
.s_addr
= addr
;
920 sin
.sin_other
= proxy
? SIN_PROXY
: 0;
922 rt
= rtlookup((struct sockaddr
*)&sin
);
924 rt
= rtpurelookup((struct sockaddr
*)&sin
);
929 if (rt
->rt_flags
& RTF_GATEWAY
)
930 why
= "host is not on local network";
931 else if (!(rt
->rt_flags
& RTF_LLINFO
))
932 why
= "could not allocate llinfo";
933 else if (rt
->rt_gateway
->sa_family
!= AF_LINK
)
934 why
= "gateway route is not ours";
938 log(LOG_DEBUG
, "arplookup %s failed: %s\n",
939 inet_ntoa(sin
.sin_addr
), why
);
941 if (rt
->rt_refcnt
<= 0 && (rt
->rt_flags
& RTF_WASCLONED
)) {
942 /* No references to this route. Purge it. */
943 rtrequest(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
944 rt_mask(rt
), rt
->rt_flags
, NULL
);
948 return (rt
->rt_llinfo
);
952 arp_ifinit(struct ifnet
*ifp
, struct ifaddr
*ifa
)
954 ASSERT_SERIALIZED(ifp
->if_serializer
);
956 if (IA_SIN(ifa
)->sin_addr
.s_addr
!= INADDR_ANY
) {
957 lwkt_serialize_exit(ifp
->if_serializer
);
958 arprequest(ifp
, &IA_SIN(ifa
)->sin_addr
, &IA_SIN(ifa
)->sin_addr
,
960 lwkt_serialize_enter(ifp
->if_serializer
);
962 ifa
->ifa_rtrequest
= arp_rtrequest
;
963 ifa
->ifa_flags
|= RTF_CLONING
;
967 arp_ifinit2(struct ifnet
*ifp
, struct ifaddr
*ifa
, u_char
*enaddr
)
969 ASSERT_NOT_SERIALIZED(ifp
->if_serializer
);
971 if (IA_SIN(ifa
)->sin_addr
.s_addr
!= INADDR_ANY
)
972 arprequest(ifp
, &IA_SIN(ifa
)->sin_addr
, &IA_SIN(ifa
)->sin_addr
,
974 ifa
->ifa_rtrequest
= arp_rtrequest
;
975 ifa
->ifa_flags
|= RTF_CLONING
;
983 for (cpu
= 0; cpu
< ncpus2
; cpu
++)
984 LIST_INIT(&llinfo_arp_list
[cpu
]);
985 netisr_register(NETISR_ARP
, cpu0_portfn
, arpintr
);
988 SYSINIT(arp
, SI_SUB_PROTO_DOMAIN
, SI_ORDER_ANY
, arp_init
, 0);