Really fix indent.
[dragonfly.git] / sys / netinet / if_ether.c
blob3a56a23d7ce67a0a7f9c7977689467c9f64af252
1 /*
2 * Copyright (c) 2004, 2005 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Jeffrey M. Hsu.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
30 * SUCH DAMAGE.
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
39 * are met:
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
63 * SUCH DAMAGE.
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.59 2008/11/22 11:03:35 sephe Exp $
71 * Ethernet address resolution protocol.
72 * TODO:
73 * add "inuse/lock" bit (or ref. count) along with valid bit
76 #include "opt_inet.h"
77 #include "opt_carp.h"
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>
84 #include <sys/mbuf.h>
85 #include <sys/malloc.h>
86 #include <sys/socket.h>
87 #include <sys/syslog.h>
88 #include <sys/lock.h>
90 #include <net/if.h>
91 #include <net/if_dl.h>
92 #include <net/if_types.h>
93 #include <net/route.h>
94 #include <net/netisr.h>
95 #include <net/if_llc.h>
97 #include <netinet/in.h>
98 #include <netinet/in_var.h>
99 #include <netinet/if_ether.h>
101 #include <sys/thread2.h>
102 #include <sys/msgport2.h>
103 #include <net/netmsg2.h>
105 #ifdef CARP
106 #include <netinet/ip_carp.h>
107 #endif
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, "");
115 /* timer values */
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,
121 &arpt_prune, 0, "");
122 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
123 &arpt_keep, 0, "");
124 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
125 &arpt_down, 0, "");
127 #define rt_expire rt_rmx.rmx_expire
129 struct llinfo_arp {
130 LIST_ENTRY(llinfo_arp) la_le;
131 struct rtentry *la_rt;
132 struct mbuf *la_hold; /* last packet until resolved/timeout */
133 struct lwkt_port *la_msgport; /* last packet's msgport */
134 u_short la_preempt; /* countdown for pre-expiry arps */
135 u_short la_asked; /* #times we QUERIED following expiration */
138 static LIST_HEAD(, llinfo_arp) llinfo_arp_list[MAXCPU];
140 static int arp_maxtries = 5;
141 static int useloopback = 1; /* use loopback interface for local traffic */
142 static int arp_proxyall = 0;
144 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
145 &arp_maxtries, 0, "");
146 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
147 &useloopback, 0, "");
148 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
149 &arp_proxyall, 0, "");
151 static int arp_mpsafe = 0;
152 TUNABLE_INT("net.link.ether.inet.arp_mpsafe", &arp_mpsafe);
154 static void arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
155 static void arprequest(struct ifnet *, const struct in_addr *,
156 const struct in_addr *, const u_char *);
157 static void arprequest_async(struct ifnet *, const struct in_addr *,
158 const struct in_addr *, const u_char *);
159 static void arpintr(struct netmsg *);
160 static void arptfree(struct llinfo_arp *);
161 static void arptimer(void *);
162 static struct llinfo_arp *
163 arplookup(in_addr_t, boolean_t, boolean_t);
164 #ifdef INET
165 static void in_arpinput(struct mbuf *);
166 #endif
168 static struct callout arptimer_ch[MAXCPU];
171 * Timeout routine. Age arp_tab entries periodically.
173 /* ARGSUSED */
174 static void
175 arptimer(void *ignored_arg)
177 struct llinfo_arp *la, *nla;
179 crit_enter();
180 LIST_FOREACH_MUTABLE(la, &llinfo_arp_list[mycpuid], la_le, nla) {
181 if (la->la_rt->rt_expire && la->la_rt->rt_expire <= time_second)
182 arptfree(la);
184 callout_reset(&arptimer_ch[mycpuid], arpt_prune * hz, arptimer, NULL);
185 crit_exit();
189 * Parallel to llc_rtrequest.
191 static void
192 arp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
194 struct sockaddr *gate = rt->rt_gateway;
195 struct llinfo_arp *la = rt->rt_llinfo;
197 struct sockaddr_dl null_sdl = { sizeof null_sdl, AF_LINK };
198 static boolean_t arpinit_done[MAXCPU];
200 if (!arpinit_done[mycpuid]) {
201 arpinit_done[mycpuid] = TRUE;
202 callout_init(&arptimer_ch[mycpuid]);
203 callout_reset(&arptimer_ch[mycpuid], hz, arptimer, NULL);
205 if (rt->rt_flags & RTF_GATEWAY)
206 return;
208 switch (req) {
209 case RTM_ADD:
211 * XXX: If this is a manually added route to interface
212 * such as older version of routed or gated might provide,
213 * restore cloning bit.
215 if (!(rt->rt_flags & RTF_HOST) &&
216 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
217 rt->rt_flags |= RTF_CLONING;
218 if (rt->rt_flags & RTF_CLONING) {
220 * Case 1: This route should come from a route to iface.
222 rt_setgate(rt, rt_key(rt),
223 (struct sockaddr *)&null_sdl);
224 gate = rt->rt_gateway;
225 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
226 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
227 rt->rt_expire = time_second;
228 break;
230 /* Announce a new entry if requested. */
231 if (rt->rt_flags & RTF_ANNOUNCE) {
232 arprequest_async(rt->rt_ifp,
233 &SIN(rt_key(rt))->sin_addr,
234 &SIN(rt_key(rt))->sin_addr,
235 LLADDR(SDL(gate)));
237 /*FALLTHROUGH*/
238 case RTM_RESOLVE:
239 if (gate->sa_family != AF_LINK ||
240 gate->sa_len < sizeof(struct sockaddr_dl)) {
241 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
242 break;
244 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
245 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
246 if (la != NULL)
247 break; /* This happens on a route change */
249 * Case 2: This route may come from cloning, or a manual route
250 * add with a LL address.
252 R_Malloc(la, struct llinfo_arp *, sizeof *la);
253 rt->rt_llinfo = la;
254 if (la == NULL) {
255 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
256 break;
258 bzero(la, sizeof *la);
259 la->la_rt = rt;
260 rt->rt_flags |= RTF_LLINFO;
261 LIST_INSERT_HEAD(&llinfo_arp_list[mycpuid], la, la_le);
263 #ifdef INET
265 * This keeps the multicast addresses from showing up
266 * in `arp -a' listings as unresolved. It's not actually
267 * functional. Then the same for broadcast.
269 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
270 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
271 LLADDR(SDL(gate)));
272 SDL(gate)->sdl_alen = 6;
273 rt->rt_expire = 0;
275 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
276 memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
277 rt->rt_ifp->if_addrlen);
278 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
279 rt->rt_expire = 0;
281 #endif
283 if (SIN(rt_key(rt))->sin_addr.s_addr ==
284 (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
286 * This test used to be
287 * if (loif.if_flags & IFF_UP)
288 * It allowed local traffic to be forced
289 * through the hardware by configuring the
290 * loopback down. However, it causes problems
291 * during network configuration for boards
292 * that can't receive packets they send. It
293 * is now necessary to clear "useloopback" and
294 * remove the route to force traffic out to
295 * the hardware.
297 rt->rt_expire = 0;
298 bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
299 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
300 if (useloopback)
301 rt->rt_ifp = loif;
303 break;
305 case RTM_DELETE:
306 if (la == NULL)
307 break;
308 LIST_REMOVE(la, la_le);
309 rt->rt_llinfo = NULL;
310 rt->rt_flags &= ~RTF_LLINFO;
311 if (la->la_hold != NULL)
312 m_freem(la->la_hold);
313 Free(la);
314 break;
318 static struct mbuf *
319 arpreq_alloc(struct ifnet *ifp, const struct in_addr *sip,
320 const struct in_addr *tip, const u_char *enaddr)
322 struct mbuf *m;
323 struct arphdr *ah;
324 u_short ar_hrd;
326 if ((m = m_gethdr(MB_DONTWAIT, MT_DATA)) == NULL)
327 return NULL;
328 m->m_pkthdr.rcvif = NULL;
330 switch (ifp->if_type) {
331 case IFT_ETHER:
333 * This may not be correct for types not explicitly
334 * listed, but this is our best guess
336 default:
337 ar_hrd = htons(ARPHRD_ETHER);
339 m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
340 m->m_pkthdr.len = m->m_len;
341 MH_ALIGN(m, m->m_len);
343 ah = mtod(m, struct arphdr *);
344 break;
347 ah->ar_hrd = ar_hrd;
348 ah->ar_pro = htons(ETHERTYPE_IP);
349 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
350 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
351 ah->ar_op = htons(ARPOP_REQUEST);
352 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
353 memset(ar_tha(ah), 0, ah->ar_hln);
354 memcpy(ar_spa(ah), sip, ah->ar_pln);
355 memcpy(ar_tpa(ah), tip, ah->ar_pln);
357 return m;
360 static void
361 arpreq_send(struct ifnet *ifp, struct mbuf *m)
363 struct sockaddr sa;
364 struct ether_header *eh;
366 switch (ifp->if_type) {
367 case IFT_ETHER:
369 * This may not be correct for types not explicitly
370 * listed, but this is our best guess
372 default:
373 eh = (struct ether_header *)sa.sa_data;
374 /* if_output() will not swap */
375 eh->ether_type = htons(ETHERTYPE_ARP);
376 memcpy(eh->ether_dhost, ifp->if_broadcastaddr, ifp->if_addrlen);
377 break;
380 sa.sa_family = AF_UNSPEC;
381 sa.sa_len = sizeof(sa);
382 ifp->if_output(ifp, m, &sa, NULL);
385 static void
386 arpreq_send_handler(struct netmsg *nmsg)
388 struct mbuf *m = ((struct netmsg_packet *)nmsg)->nm_packet;
389 struct ifnet *ifp = nmsg->nm_lmsg.u.ms_resultp;
391 arpreq_send(ifp, m);
392 /* nmsg was embedded in the mbuf, do not reply! */
396 * Broadcast an ARP request. Caller specifies:
397 * - arp header source ip address
398 * - arp header target ip address
399 * - arp header source ethernet address
401 * NOTE: Caller MUST NOT hold ifp's serializer
403 static void
404 arprequest(struct ifnet *ifp, const struct in_addr *sip,
405 const struct in_addr *tip, const u_char *enaddr)
407 struct mbuf *m;
409 m = arpreq_alloc(ifp, sip, tip, enaddr);
410 if (m == NULL)
411 return;
412 arpreq_send(ifp, m);
416 * Same as arprequest(), except:
417 * - Caller is allowed to hold ifp's serializer
418 * - Network output is done in TDF_NETWORK kernel thread
420 static void
421 arprequest_async(struct ifnet *ifp, const struct in_addr *sip,
422 const struct in_addr *tip, const u_char *enaddr)
424 struct mbuf *m;
425 struct netmsg_packet *pmsg;
427 m = arpreq_alloc(ifp, sip, tip, enaddr);
428 if (m == NULL)
429 return;
431 pmsg = &m->m_hdr.mh_netmsg;
432 netmsg_init(&pmsg->nm_netmsg, &netisr_apanic_rport, 0,
433 arpreq_send_handler);
434 pmsg->nm_packet = m;
435 pmsg->nm_netmsg.nm_lmsg.u.ms_resultp = ifp;
437 lwkt_sendmsg(cpu_portfn(mycpuid), &pmsg->nm_netmsg.nm_lmsg);
441 * Resolve an IP address into an ethernet address. If success,
442 * desten is filled in. If there is no entry in arptab,
443 * set one up and broadcast a request for the IP address.
444 * Hold onto this mbuf and resend it once the address
445 * is finally resolved. A return value of 1 indicates
446 * that desten has been filled in and the packet should be sent
447 * normally; a 0 return indicates that the packet has been
448 * taken over here, either now or for later transmission.
451 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
452 struct sockaddr *dst, u_char *desten)
454 struct rtentry *rt;
455 struct llinfo_arp *la = NULL;
456 struct sockaddr_dl *sdl;
458 if (m->m_flags & M_BCAST) { /* broadcast */
459 memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
460 return (1);
462 if (m->m_flags & M_MCAST) {/* multicast */
463 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
464 return (1);
466 if (rt0 != NULL) {
467 if (rt_llroute(dst, rt0, &rt) != 0) {
468 m_freem(m);
469 return 0;
471 la = rt->rt_llinfo;
473 if (la == NULL) {
474 la = arplookup(SIN(dst)->sin_addr.s_addr, TRUE, FALSE);
475 if (la != NULL)
476 rt = la->la_rt;
478 if (la == NULL || rt == NULL) {
479 log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
480 inet_ntoa(SIN(dst)->sin_addr), la ? "la" : " ",
481 rt ? "rt" : "");
482 m_freem(m);
483 return (0);
485 sdl = SDL(rt->rt_gateway);
487 * Check the address family and length is valid, the address
488 * is resolved; otherwise, try to resolve.
490 if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
491 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
493 * If entry has an expiry time and it is approaching,
494 * see if we need to send an ARP request within this
495 * arpt_down interval.
497 if ((rt->rt_expire != 0) &&
498 (time_second + la->la_preempt > rt->rt_expire)) {
499 arprequest(ifp,
500 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
501 &SIN(dst)->sin_addr,
502 IF_LLADDR(ifp));
503 la->la_preempt--;
506 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
507 return 1;
510 * If ARP is disabled on this interface, stop.
511 * XXX
512 * Probably should not allocate empty llinfo struct if we are
513 * not going to be sending out an arp request.
515 if (ifp->if_flags & IFF_NOARP) {
516 m_freem(m);
517 return (0);
520 * There is an arptab entry, but no ethernet address
521 * response yet. Replace the held mbuf with this
522 * latest one.
524 if (la->la_hold != NULL)
525 m_freem(la->la_hold);
526 la->la_hold = m;
527 if (curthread->td_flags & TDF_NETWORK)
528 la->la_msgport = &curthread->td_msgport;
529 else
530 la->la_msgport = cpu_portfn(mycpuid);
531 if (rt->rt_expire || ((rt->rt_flags & RTF_STATIC) && !sdl->sdl_alen)) {
532 rt->rt_flags &= ~RTF_REJECT;
533 if (la->la_asked == 0 || rt->rt_expire != time_second) {
534 rt->rt_expire = time_second;
535 if (la->la_asked++ < arp_maxtries) {
536 arprequest(ifp,
537 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
538 &SIN(dst)->sin_addr,
539 IF_LLADDR(ifp));
540 } else {
541 rt->rt_flags |= RTF_REJECT;
542 rt->rt_expire += arpt_down;
543 la->la_asked = 0;
544 la->la_preempt = arp_maxtries;
548 return (0);
552 * Common length and type checks are done here,
553 * then the protocol-specific routine is called.
555 static void
556 arpintr(struct netmsg *msg)
558 struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
559 struct arphdr *ar;
560 u_short ar_hrd;
562 if (m->m_len < sizeof(struct arphdr) &&
563 (m = m_pullup(m, sizeof(struct arphdr))) == NULL) {
564 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
565 return;
567 ar = mtod(m, struct arphdr *);
569 ar_hrd = ntohs(ar->ar_hrd);
570 if (ar_hrd != ARPHRD_ETHER && ar_hrd != ARPHRD_IEEE802) {
571 log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
572 (unsigned char *)&ar->ar_hrd, "");
573 m_freem(m);
574 return;
577 if (m->m_pkthdr.len < arphdr_len(ar)) {
578 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
579 log(LOG_ERR, "arp: runt packet\n");
580 return;
582 ar = mtod(m, struct arphdr *);
585 switch (ntohs(ar->ar_pro)) {
586 #ifdef INET
587 case ETHERTYPE_IP:
588 in_arpinput(m);
589 return;
590 #endif
592 m_freem(m);
593 /* msg was embedded in the mbuf, do not reply! */
596 #ifdef INET
598 * ARP for Internet protocols on 10 Mb/s Ethernet.
599 * Algorithm is that given in RFC 826.
600 * In addition, a sanity check is performed on the sender
601 * protocol address, to catch impersonators.
602 * We no longer handle negotiations for use of trailer protocol:
603 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
604 * along with IP replies if we wanted trailers sent to us,
605 * and also sent them in response to IP replies.
606 * This allowed either end to announce the desire to receive
607 * trailer packets.
608 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
609 * but formerly didn't normally send requests.
611 static int log_arp_wrong_iface = 1;
612 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
613 &log_arp_wrong_iface, 0,
614 "log arp packets arriving on the wrong interface");
616 static void
617 arp_hold_output(struct netmsg *nmsg)
619 struct mbuf *m = ((struct netmsg_packet *)nmsg)->nm_packet;
620 struct rtentry *rt;
621 struct ifnet *ifp;
623 rt = nmsg->nm_lmsg.u.ms_resultp;
624 ifp = m->m_pkthdr.rcvif;
625 m->m_pkthdr.rcvif = NULL;
627 ifp->if_output(ifp, m, rt_key(rt), rt);
629 /* Drop the reference count bumped by the sender */
630 RTFREE(rt);
632 /* nmsg was embedded in the mbuf, do not reply! */
635 static void
636 arp_update_oncpu(struct mbuf *m, in_addr_t saddr, boolean_t create,
637 boolean_t dologging)
639 struct arphdr *ah = mtod(m, struct arphdr *);
640 struct ifnet *ifp = m->m_pkthdr.rcvif;
641 struct llinfo_arp *la;
642 struct sockaddr_dl *sdl;
643 struct rtentry *rt;
645 la = arplookup(saddr, create, FALSE);
646 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
647 struct in_addr isaddr = { saddr };
649 /* the following is not an error when doing bridging */
650 if (rt->rt_ifp != ifp) {
651 if (dologging && log_arp_wrong_iface) {
652 log(LOG_ERR,
653 "arp: %s is on %s "
654 "but got reply from %*D on %s\n",
655 inet_ntoa(isaddr),
656 rt->rt_ifp->if_xname,
657 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
658 ifp->if_xname);
660 return;
662 if (sdl->sdl_alen &&
663 bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
664 if (rt->rt_expire != 0) {
665 if (dologging) {
666 log(LOG_INFO,
667 "arp: %s moved from %*D to %*D on %s\n",
668 inet_ntoa(isaddr),
669 ifp->if_addrlen, (u_char *)LLADDR(sdl),
670 ":", ifp->if_addrlen,
671 (u_char *)ar_sha(ah), ":",
672 ifp->if_xname);
674 } else {
675 if (dologging) {
676 log(LOG_ERR,
677 "arp: %*D attempts to modify "
678 "permanent entry for %s on %s\n",
679 ifp->if_addrlen, (u_char *)ar_sha(ah),
680 ":", inet_ntoa(isaddr), ifp->if_xname);
682 return;
686 * sanity check for the address length.
687 * XXX this does not work for protocols with variable address
688 * length. -is
690 if (dologging && sdl->sdl_alen && sdl->sdl_alen != ah->ar_hln) {
691 log(LOG_WARNING,
692 "arp from %*D: new addr len %d, was %d",
693 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
694 ah->ar_hln, sdl->sdl_alen);
696 if (ifp->if_addrlen != ah->ar_hln) {
697 if (dologging) {
698 log(LOG_WARNING,
699 "arp from %*D: addr len: new %d, i/f %d "
700 "(ignored)",
701 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
702 ah->ar_hln, ifp->if_addrlen);
704 return;
706 memcpy(LLADDR(sdl), ar_sha(ah), sdl->sdl_alen = ah->ar_hln);
707 if (rt->rt_expire != 0)
708 rt->rt_expire = time_second + arpt_keep;
709 rt->rt_flags &= ~RTF_REJECT;
710 la->la_asked = 0;
711 la->la_preempt = arp_maxtries;
714 * This particular cpu might have been holding an mbuf
715 * pending ARP resolution. If so, transmit the mbuf now.
717 if (la->la_hold != NULL) {
718 struct mbuf *m = la->la_hold;
719 struct lwkt_port *port = la->la_msgport;
720 struct netmsg_packet *pmsg;
722 la->la_hold = NULL;
723 la->la_msgport = NULL;
725 m_adj(m, sizeof(struct ether_header));
728 * Make sure that this rtentry will not be freed
729 * before the packet is processed on the target
730 * msgport. The reference count will be dropped
731 * in the handler associated with this packet.
733 rt->rt_refcnt++;
735 pmsg = &m->m_hdr.mh_netmsg;
736 netmsg_init(&pmsg->nm_netmsg, &netisr_apanic_rport,
737 MSGF_MPSAFE, arp_hold_output);
738 pmsg->nm_packet = m;
740 /* Record necessary information */
741 m->m_pkthdr.rcvif = ifp;
742 pmsg->nm_netmsg.nm_lmsg.u.ms_resultp = rt;
744 lwkt_sendmsg(port, &pmsg->nm_netmsg.nm_lmsg);
749 #ifdef SMP
751 struct netmsg_arp_update {
752 struct netmsg netmsg;
753 struct mbuf *m;
754 in_addr_t saddr;
755 boolean_t create;
758 static void arp_update_msghandler(struct netmsg *);
760 #endif
763 * Called from arpintr() - this routine is run from a single cpu.
765 static void
766 in_arpinput(struct mbuf *m)
768 struct arphdr *ah;
769 struct ifnet *ifp = m->m_pkthdr.rcvif;
770 struct ether_header *eh;
771 struct rtentry *rt;
772 struct ifaddr_container *ifac;
773 struct in_ifaddr_container *iac;
774 struct in_ifaddr *ia;
775 struct sockaddr sa;
776 struct in_addr isaddr, itaddr, myaddr;
777 #ifdef SMP
778 struct netmsg_arp_update msg;
779 #endif
780 u_int8_t *enaddr = NULL;
781 int op;
782 int req_len;
784 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
785 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
786 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
787 return;
790 ah = mtod(m, struct arphdr *);
791 op = ntohs(ah->ar_op);
792 memcpy(&isaddr, ar_spa(ah), sizeof isaddr);
793 memcpy(&itaddr, ar_tpa(ah), sizeof itaddr);
795 * Check both target and sender IP addresses:
797 * If we receive the packet on the interface owning the address,
798 * then accept the address.
800 * For a bridge, we accept the address if the receive interface and
801 * the interface owning the address are on the same bridge.
802 * (This will change slightly when we have clusters of interfaces).
804 LIST_FOREACH(iac, INADDR_HASH(itaddr.s_addr), ia_hash) {
805 ia = iac->ia;
807 /* Skip all ia's which don't match */
808 if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
809 continue;
811 if (ia->ia_ifp == ifp)
812 goto match;
814 if (ifp->if_bridge && ia->ia_ifp &&
815 ifp->if_bridge == ia->ia_ifp->if_bridge)
816 goto match;
818 #ifdef CARP
820 * If the interface does not match, but the recieving interface
821 * is part of carp, we call carp_iamatch to see if this is a
822 * request for the virtual host ip.
823 * XXX: This is really ugly!
825 if (ifp->if_carp != NULL) {
826 get_mplock();
827 if (ifp->if_carp != NULL &&
828 carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr)) {
829 rel_mplock();
830 goto match;
832 rel_mplock();
834 #endif
836 LIST_FOREACH(iac, INADDR_HASH(isaddr.s_addr), ia_hash) {
837 ia = iac->ia;
839 /* Skip all ia's which don't match */
840 if (isaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
841 continue;
843 if (ia->ia_ifp == ifp)
844 goto match;
846 if (ifp->if_bridge && ia->ia_ifp &&
847 ifp->if_bridge == ia->ia_ifp->if_bridge)
848 goto match;
851 * No match, use the first inet address on the receive interface
852 * as a dummy address for the rest of the function.
854 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
855 struct ifaddr *ifa = ifac->ifa;
857 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
858 ia = ifatoia(ifa);
859 goto match;
863 * If we got here, we didn't find any suitable interface,
864 * so drop the packet.
866 m_freem(m);
867 return;
869 match:
870 if (!enaddr)
871 enaddr = (u_int8_t *)IF_LLADDR(ifp);
872 myaddr = ia->ia_addr.sin_addr;
873 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) {
874 m_freem(m); /* it's from me, ignore it. */
875 return;
877 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
878 log(LOG_ERR,
879 "arp: link address is broadcast for IP address %s!\n",
880 inet_ntoa(isaddr));
881 m_freem(m);
882 return;
884 if (isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
885 log(LOG_ERR,
886 "arp: %*D is using my IP address %s!\n",
887 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
888 inet_ntoa(isaddr));
889 itaddr = myaddr;
890 goto reply;
892 #ifdef SMP
893 netmsg_init(&msg.netmsg, &curthread->td_msgport, 0,
894 arp_update_msghandler);
895 msg.m = m;
896 msg.saddr = isaddr.s_addr;
897 msg.create = (itaddr.s_addr == myaddr.s_addr);
898 lwkt_domsg(rtable_portfn(0), &msg.netmsg.nm_lmsg, 0);
899 #else
900 arp_update_oncpu(m, isaddr.s_addr, (itaddr.s_addr == myaddr.s_addr),
901 TRUE);
902 #endif
903 reply:
904 if (op != ARPOP_REQUEST) {
905 m_freem(m);
906 return;
908 if (itaddr.s_addr == myaddr.s_addr) {
909 /* I am the target */
910 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
911 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
912 } else {
913 struct llinfo_arp *la;
915 la = arplookup(itaddr.s_addr, FALSE, SIN_PROXY);
916 if (la == NULL) {
917 struct sockaddr_in sin;
919 if (!arp_proxyall) {
920 m_freem(m);
921 return;
924 bzero(&sin, sizeof sin);
925 sin.sin_family = AF_INET;
926 sin.sin_len = sizeof sin;
927 sin.sin_addr = itaddr;
929 rt = rtpurelookup((struct sockaddr *)&sin);
930 if (rt == NULL) {
931 m_freem(m);
932 return;
934 --rt->rt_refcnt;
936 * Don't send proxies for nodes on the same interface
937 * as this one came out of, or we'll get into a fight
938 * over who claims what Ether address.
940 if (rt->rt_ifp == ifp) {
941 m_freem(m);
942 return;
944 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
945 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
946 #ifdef DEBUG_PROXY
947 kprintf("arp: proxying for %s\n", inet_ntoa(itaddr));
948 #endif
949 } else {
950 struct sockaddr_dl *sdl;
952 rt = la->la_rt;
953 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
954 sdl = SDL(rt->rt_gateway);
955 memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
959 memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
960 memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
961 ah->ar_op = htons(ARPOP_REPLY);
962 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
963 switch (ifp->if_type) {
964 case IFT_ETHER:
966 * May not be correct for types not explictly
967 * listed, but it is our best guess.
969 default:
970 eh = (struct ether_header *)sa.sa_data;
971 memcpy(eh->ether_dhost, ar_tha(ah), sizeof eh->ether_dhost);
972 eh->ether_type = htons(ETHERTYPE_ARP);
973 break;
975 sa.sa_family = AF_UNSPEC;
976 sa.sa_len = sizeof sa;
977 ifp->if_output(ifp, m, &sa, NULL);
980 #ifdef SMP
982 static void
983 arp_update_msghandler(struct netmsg *netmsg)
985 struct netmsg_arp_update *msg = (struct netmsg_arp_update *)netmsg;
986 int nextcpu;
988 arp_update_oncpu(msg->m, msg->saddr, msg->create, mycpuid == 0);
990 nextcpu = mycpuid + 1;
991 if (nextcpu < ncpus)
992 lwkt_forwardmsg(rtable_portfn(nextcpu), &msg->netmsg.nm_lmsg);
993 else
994 lwkt_replymsg(&msg->netmsg.nm_lmsg, 0);
997 #endif /* SMP */
999 #endif /* INET */
1002 * Free an arp entry. If the arp entry is actively referenced or represents
1003 * a static entry we only clear it back to an unresolved state, otherwise
1004 * we destroy the entry entirely.
1006 * Note that static entries are created when route add ... -interface is used
1007 * to create an interface route to a (direct) destination.
1009 static void
1010 arptfree(struct llinfo_arp *la)
1012 struct rtentry *rt = la->la_rt;
1013 struct sockaddr_dl *sdl;
1015 if (rt == NULL)
1016 panic("arptfree");
1017 sdl = SDL(rt->rt_gateway);
1018 if (sdl != NULL &&
1019 ((rt->rt_refcnt > 0 && sdl->sdl_family == AF_LINK) ||
1020 (rt->rt_flags & RTF_STATIC))) {
1021 sdl->sdl_alen = 0;
1022 la->la_preempt = la->la_asked = 0;
1023 rt->rt_flags &= ~RTF_REJECT;
1024 return;
1026 rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
1030 * Lookup or enter a new address in arptab.
1032 static struct llinfo_arp *
1033 arplookup(in_addr_t addr, boolean_t create, boolean_t proxy)
1035 struct rtentry *rt;
1036 struct sockaddr_inarp sin = { sizeof sin, AF_INET };
1037 const char *why = NULL;
1039 sin.sin_addr.s_addr = addr;
1040 sin.sin_other = proxy ? SIN_PROXY : 0;
1041 if (create)
1042 rt = rtlookup((struct sockaddr *)&sin);
1043 else
1044 rt = rtpurelookup((struct sockaddr *)&sin);
1045 if (rt == NULL)
1046 return (NULL);
1047 rt->rt_refcnt--;
1049 if (rt->rt_flags & RTF_GATEWAY)
1050 why = "host is not on local network";
1051 else if (!(rt->rt_flags & RTF_LLINFO))
1052 why = "could not allocate llinfo";
1053 else if (rt->rt_gateway->sa_family != AF_LINK)
1054 why = "gateway route is not ours";
1056 if (why) {
1057 if (create) {
1058 log(LOG_DEBUG, "arplookup %s failed: %s\n",
1059 inet_ntoa(sin.sin_addr), why);
1061 if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_WASCLONED)) {
1062 /* No references to this route. Purge it. */
1063 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1064 rt_mask(rt), rt->rt_flags, NULL);
1066 return (NULL);
1068 return (rt->rt_llinfo);
1071 void
1072 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1074 if (IA_SIN(ifa)->sin_addr.s_addr != INADDR_ANY) {
1075 arprequest_async(ifp, &IA_SIN(ifa)->sin_addr,
1076 &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
1078 ifa->ifa_rtrequest = arp_rtrequest;
1079 ifa->ifa_flags |= RTF_CLONING;
1082 void
1083 arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr)
1085 if (IA_SIN(ifa)->sin_addr.s_addr != INADDR_ANY) {
1086 arprequest_async(ifp, &IA_SIN(ifa)->sin_addr,
1087 &IA_SIN(ifa)->sin_addr, enaddr);
1089 ifa->ifa_rtrequest = arp_rtrequest;
1090 ifa->ifa_flags |= RTF_CLONING;
1093 static void
1094 arp_init(void)
1096 uint32_t flags;
1097 int cpu;
1099 for (cpu = 0; cpu < ncpus2; cpu++)
1100 LIST_INIT(&llinfo_arp_list[cpu]);
1102 if (arp_mpsafe) {
1103 flags = NETISR_FLAG_MPSAFE;
1104 kprintf("arp: MPSAFE\n");
1105 } else {
1106 flags = NETISR_FLAG_NOTMPSAFE;
1108 netisr_register(NETISR_ARP, cpu0_portfn, arpintr, flags);
1111 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);