Nuke ARCnet support.
[dragonfly.git] / sys / netinet / if_ether.c
blobedbf6491e7fdb93dcd434438eeeba1ebedb7c1fe
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.40 2007/08/27 13:15:14 hasso 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>
89 #include <net/if.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 <net/iso88025.h>
102 #include <sys/thread2.h>
103 #include <sys/msgport2.h>
104 #include <net/netmsg2.h>
107 #ifdef CARP
108 #include <netinet/ip_carp.h>
109 #endif
111 #define SIN(s) ((struct sockaddr_in *)s)
112 #define SDL(s) ((struct sockaddr_dl *)s)
114 SYSCTL_DECL(_net_link_ether);
115 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
117 /* timer values */
118 static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
119 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
120 static int arpt_down = 20; /* once declared down, don't send for 20 sec */
122 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
123 &arpt_prune, 0, "");
124 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
125 &arpt_keep, 0, "");
126 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
127 &arpt_down, 0, "");
129 #define rt_expire rt_rmx.rmx_expire
131 struct llinfo_arp {
132 LIST_ENTRY(llinfo_arp) la_le;
133 struct rtentry *la_rt;
134 struct mbuf *la_hold; /* last packet until resolved/timeout */
135 u_short la_preempt; /* countdown for pre-expiry arps */
136 u_short la_asked; /* #times we QUERIED following expiration */
139 static LIST_HEAD(, llinfo_arp) llinfo_arp_list[MAXCPU];
141 static int arp_maxtries = 5;
142 static int useloopback = 1; /* use loopback interface for local traffic */
143 static int arp_proxyall = 0;
145 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
146 &arp_maxtries, 0, "");
147 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
148 &useloopback, 0, "");
149 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
150 &arp_proxyall, 0, "");
152 void arprequest_acces(struct ifnet *ifp, struct in_addr *sip, struct in_addr *tip, u_char *enaddr);
153 static void arp_rtrequest (int, struct rtentry *, struct rt_addrinfo *);
154 static void arprequest (struct ifnet *,
155 struct in_addr *, struct in_addr *, u_char *);
156 static void arpintr(struct netmsg *);
157 static void arptfree (struct llinfo_arp *);
158 static void arptimer (void *);
159 static struct llinfo_arp
160 *arplookup (in_addr_t addr, boolean_t create, boolean_t proxy);
161 #ifdef INET
162 static void in_arpinput (struct mbuf *);
163 #endif
165 static struct callout arptimer_ch[MAXCPU];
168 * Timeout routine. Age arp_tab entries periodically.
170 /* ARGSUSED */
171 static void
172 arptimer(void *ignored_arg)
174 struct llinfo_arp *la, *nla;
176 crit_enter();
177 LIST_FOREACH_MUTABLE(la, &llinfo_arp_list[mycpuid], la_le, nla) {
178 if (la->la_rt->rt_expire && la->la_rt->rt_expire <= time_second)
179 arptfree(la);
181 callout_reset(&arptimer_ch[mycpuid], arpt_prune * hz, arptimer, NULL);
182 crit_exit();
186 * Parallel to llc_rtrequest.
188 static void
189 arp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
191 struct sockaddr *gate = rt->rt_gateway;
192 struct llinfo_arp *la = rt->rt_llinfo;
194 struct sockaddr_dl null_sdl = { sizeof null_sdl, AF_LINK };
195 static boolean_t arpinit_done[MAXCPU];
197 if (!arpinit_done[mycpuid]) {
198 arpinit_done[mycpuid] = TRUE;
199 callout_init(&arptimer_ch[mycpuid]);
200 callout_reset(&arptimer_ch[mycpuid], hz, arptimer, NULL);
202 if (rt->rt_flags & RTF_GATEWAY)
203 return;
205 switch (req) {
206 case RTM_ADD:
208 * XXX: If this is a manually added route to interface
209 * such as older version of routed or gated might provide,
210 * restore cloning bit.
212 if (!(rt->rt_flags & RTF_HOST) &&
213 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
214 rt->rt_flags |= RTF_CLONING;
215 if (rt->rt_flags & RTF_CLONING) {
217 * Case 1: This route should come from a route to iface.
219 rt_setgate(rt, rt_key(rt),
220 (struct sockaddr *)&null_sdl);
221 gate = rt->rt_gateway;
222 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
223 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
224 rt->rt_expire = time_second;
225 break;
227 /* Announce a new entry if requested. */
228 if (rt->rt_flags & RTF_ANNOUNCE)
229 arprequest(rt->rt_ifp,
230 &SIN(rt_key(rt))->sin_addr,
231 &SIN(rt_key(rt))->sin_addr,
232 LLADDR(SDL(gate)));
233 /*FALLTHROUGH*/
234 case RTM_RESOLVE:
235 if (gate->sa_family != AF_LINK ||
236 gate->sa_len < sizeof(struct sockaddr_dl)) {
237 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
238 break;
240 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
241 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
242 if (la != NULL)
243 break; /* This happens on a route change */
245 * Case 2: This route may come from cloning, or a manual route
246 * add with a LL address.
248 R_Malloc(la, struct llinfo_arp *, sizeof *la);
249 rt->rt_llinfo = la;
250 if (la == NULL) {
251 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
252 break;
254 bzero(la, sizeof *la);
255 la->la_rt = rt;
256 rt->rt_flags |= RTF_LLINFO;
257 LIST_INSERT_HEAD(&llinfo_arp_list[mycpuid], la, la_le);
259 #ifdef INET
261 * This keeps the multicast addresses from showing up
262 * in `arp -a' listings as unresolved. It's not actually
263 * functional. Then the same for broadcast.
265 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
266 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
267 LLADDR(SDL(gate)));
268 SDL(gate)->sdl_alen = 6;
269 rt->rt_expire = 0;
271 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
272 memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
273 rt->rt_ifp->if_addrlen);
274 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
275 rt->rt_expire = 0;
277 #endif
279 if (SIN(rt_key(rt))->sin_addr.s_addr ==
280 (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
282 * This test used to be
283 * if (loif.if_flags & IFF_UP)
284 * It allowed local traffic to be forced
285 * through the hardware by configuring the
286 * loopback down. However, it causes problems
287 * during network configuration for boards
288 * that can't receive packets they send. It
289 * is now necessary to clear "useloopback" and
290 * remove the route to force traffic out to
291 * the hardware.
293 rt->rt_expire = 0;
294 bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
295 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
296 if (useloopback)
297 rt->rt_ifp = loif;
299 break;
301 case RTM_DELETE:
302 if (la == NULL)
303 break;
304 LIST_REMOVE(la, la_le);
305 rt->rt_llinfo = NULL;
306 rt->rt_flags &= ~RTF_LLINFO;
307 if (la->la_hold != NULL)
308 m_freem(la->la_hold);
309 Free(la);
314 * Broadcast an ARP request. Caller specifies:
315 * - arp header source ip address
316 * - arp header target ip address
317 * - arp header source ethernet address
319 static void
320 arprequest(struct ifnet *ifp, struct in_addr *sip, struct in_addr *tip,
321 u_char *enaddr)
323 struct mbuf *m;
324 struct ether_header *eh;
325 struct arphdr *ah;
326 struct sockaddr sa;
327 static u_char llcx[] = { 0x82, 0x40, LLC_SNAP_LSAP, LLC_SNAP_LSAP,
328 LLC_UI, 0x00, 0x00, 0x00, 0x08, 0x06 };
329 u_short ar_hrd;
331 if ((m = m_gethdr(MB_DONTWAIT, MT_DATA)) == NULL)
332 return;
333 m->m_pkthdr.rcvif = (struct ifnet *)NULL;
335 switch (ifp->if_type) {
336 case IFT_ISO88025:
337 ar_hrd = htons(ARPHRD_IEEE802);
339 m->m_len = (sizeof llcx) +
340 arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
341 m->m_pkthdr.len = m->m_len;
342 MH_ALIGN(m, m->m_len);
344 memcpy(mtod(m, caddr_t), llcx, sizeof llcx);
345 memcpy(sa.sa_data, ifp->if_broadcastaddr, ifp->if_addrlen);
346 memcpy(sa.sa_data + 6, enaddr, 6);
347 sa.sa_data[6] |= TR_RII;
348 sa.sa_data[12] = TR_AC;
349 sa.sa_data[13] = TR_LLC_FRAME;
351 ah = (struct arphdr *)(mtod(m, char *) + sizeof llcx);
352 break;
353 case IFT_FDDI:
354 case IFT_ETHER:
356 * This may not be correct for types not explicitly
357 * listed, but this is our best guess
359 default:
360 ar_hrd = htons(ARPHRD_ETHER);
362 m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
363 m->m_pkthdr.len = m->m_len;
364 MH_ALIGN(m, m->m_len);
366 eh = (struct ether_header *)sa.sa_data;
367 /* if_output() will not swap */
368 eh->ether_type = htons(ETHERTYPE_ARP);
369 memcpy(eh->ether_dhost, ifp->if_broadcastaddr, ifp->if_addrlen);
371 ah = mtod(m, struct arphdr *);
372 break;
375 ah->ar_hrd = ar_hrd;
376 ah->ar_pro = htons(ETHERTYPE_IP);
377 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
378 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
379 ah->ar_op = htons(ARPOP_REQUEST);
380 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
381 memset(ar_tha(ah), 0, ah->ar_hln);
382 memcpy(ar_spa(ah), sip, ah->ar_pln);
383 memcpy(ar_tpa(ah), tip, ah->ar_pln);
385 sa.sa_family = AF_UNSPEC;
386 sa.sa_len = sizeof sa;
387 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)NULL);
391 * Resolve an IP address into an ethernet address. If success,
392 * desten is filled in. If there is no entry in arptab,
393 * set one up and broadcast a request for the IP address.
394 * Hold onto this mbuf and resend it once the address
395 * is finally resolved. A return value of 1 indicates
396 * that desten has been filled in and the packet should be sent
397 * normally; a 0 return indicates that the packet has been
398 * taken over here, either now or for later transmission.
401 arpresolve(
402 struct ifnet *ifp,
403 struct rtentry *rt0,
404 struct mbuf *m,
405 struct sockaddr *dst,
406 u_char *desten)
408 struct rtentry *rt;
409 struct llinfo_arp *la = NULL;
410 struct sockaddr_dl *sdl;
412 if (m->m_flags & M_BCAST) { /* broadcast */
413 memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
414 return (1);
416 if (m->m_flags & M_MCAST) {/* multicast */
417 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
418 return (1);
420 if (rt0 != NULL) {
421 if (rt_llroute(dst, rt0, &rt) != 0) {
422 m_freem(m);
423 return 0;
425 la = rt->rt_llinfo;
427 if (la == NULL) {
428 la = arplookup(SIN(dst)->sin_addr.s_addr, TRUE, FALSE);
429 if (la != NULL)
430 rt = la->la_rt;
432 if (la == NULL || rt == NULL) {
433 log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
434 inet_ntoa(SIN(dst)->sin_addr), la ? "la" : " ",
435 rt ? "rt" : "");
436 m_freem(m);
437 return (0);
439 sdl = SDL(rt->rt_gateway);
441 * Check the address family and length is valid, the address
442 * is resolved; otherwise, try to resolve.
444 if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
445 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
447 * If entry has an expiry time and it is approaching,
448 * see if we need to send an ARP request within this
449 * arpt_down interval.
451 if ((rt->rt_expire != 0) &&
452 (time_second + la->la_preempt > rt->rt_expire)) {
453 arprequest(ifp,
454 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
455 &SIN(dst)->sin_addr,
456 IF_LLADDR(ifp));
457 la->la_preempt--;
460 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
461 return 1;
464 * If ARP is disabled on this interface, stop.
465 * XXX
466 * Probably should not allocate empty llinfo struct if we are
467 * not going to be sending out an arp request.
469 if (ifp->if_flags & IFF_NOARP) {
470 m_freem(m);
471 return (0);
474 * There is an arptab entry, but no ethernet address
475 * response yet. Replace the held mbuf with this
476 * latest one.
478 if (la->la_hold != NULL)
479 m_freem(la->la_hold);
480 la->la_hold = m;
481 if (rt->rt_expire || ((rt->rt_flags & RTF_STATIC) && !sdl->sdl_alen)) {
482 rt->rt_flags &= ~RTF_REJECT;
483 if (la->la_asked == 0 || rt->rt_expire != time_second) {
484 rt->rt_expire = time_second;
485 if (la->la_asked++ < arp_maxtries) {
486 arprequest(ifp,
487 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
488 &SIN(dst)->sin_addr,
489 IF_LLADDR(ifp));
490 } else {
491 rt->rt_flags |= RTF_REJECT;
492 rt->rt_expire += arpt_down;
493 la->la_asked = 0;
494 la->la_preempt = arp_maxtries;
499 return (0);
503 * Common length and type checks are done here,
504 * then the protocol-specific routine is called.
506 static void
507 arpintr(struct netmsg *msg)
509 struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
510 struct arphdr *ar;
511 u_short ar_hrd;
513 if (m->m_len < sizeof(struct arphdr) &&
514 ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
515 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
516 goto out2;
518 ar = mtod(m, struct arphdr *);
520 ar_hrd = ntohs(ar->ar_hrd);
521 if (ar_hrd != ARPHRD_ETHER &&
522 ar_hrd != ARPHRD_IEEE802) {
523 log(LOG_ERR,
524 "arp: unknown hardware address format (0x%2D)\n",
525 (unsigned char *)&ar->ar_hrd, "");
526 goto out1;
529 if (m->m_pkthdr.len < arphdr_len(ar) &&
530 (m = m_pullup(m, arphdr_len(ar))) == NULL) {
531 log(LOG_ERR, "arp: runt packet\n");
532 goto out1;
535 switch (ntohs(ar->ar_pro)) {
536 #ifdef INET
537 case ETHERTYPE_IP:
538 in_arpinput(m);
539 goto out2;
540 #endif
542 out1:
543 m_freem(m);
544 out2:
546 /* msg was embedded in the mbuf, do not reply! */
549 #ifdef INET
551 * ARP for Internet protocols on 10 Mb/s Ethernet.
552 * Algorithm is that given in RFC 826.
553 * In addition, a sanity check is performed on the sender
554 * protocol address, to catch impersonators.
555 * We no longer handle negotiations for use of trailer protocol:
556 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
557 * along with IP replies if we wanted trailers sent to us,
558 * and also sent them in response to IP replies.
559 * This allowed either end to announce the desire to receive
560 * trailer packets.
561 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
562 * but formerly didn't normally send requests.
564 static int log_arp_wrong_iface = 1;
565 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
566 &log_arp_wrong_iface, 0,
567 "log arp packets arriving on the wrong interface");
569 static void
570 arp_update_oncpu(struct mbuf *m, in_addr_t saddr, boolean_t create,
571 boolean_t dologging)
573 struct arphdr *ah = mtod(m, struct arphdr *);
574 struct ifnet *ifp = m->m_pkthdr.rcvif;
575 struct llinfo_arp *la;
576 struct sockaddr_dl *sdl;
577 struct rtentry *rt;
578 int cpu = mycpuid;
580 la = arplookup(saddr, create, FALSE);
581 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
582 struct in_addr isaddr = { saddr };
584 /* the following is not an error when doing bridging */
585 if (rt->rt_ifp != ifp) {
586 if (dologging && log_arp_wrong_iface && cpu == 0) {
587 log(LOG_ERR,
588 "arp: %s is on %s "
589 "but got reply from %*D on %s\n",
590 inet_ntoa(isaddr),
591 rt->rt_ifp->if_xname,
592 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
593 ifp->if_xname);
595 return;
597 if (sdl->sdl_alen &&
598 bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
599 if (rt->rt_expire != 0) {
600 if (dologging && cpu == 0) {
601 log(LOG_INFO,
602 "arp: %s moved from %*D to %*D on %s\n",
603 inet_ntoa(isaddr),
604 ifp->if_addrlen, (u_char *)LLADDR(sdl),
605 ":", ifp->if_addrlen,
606 (u_char *)ar_sha(ah), ":",
607 ifp->if_xname);
609 } else {
610 if (dologging && cpu == 0) {
611 log(LOG_ERR,
612 "arp: %*D attempts to modify permanent entry for %s on %s\n",
613 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
614 inet_ntoa(isaddr), ifp->if_xname);
616 return;
620 * sanity check for the address length.
621 * XXX this does not work for protocols with variable address
622 * length. -is
624 if (dologging && sdl->sdl_alen && sdl->sdl_alen != ah->ar_hln &&
625 cpu == 0)
627 log(LOG_WARNING,
628 "arp from %*D: new addr len %d, was %d",
629 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
630 ah->ar_hln, sdl->sdl_alen);
632 if (ifp->if_addrlen != ah->ar_hln) {
633 if (dologging && cpu == 0) {
634 log(LOG_WARNING,
635 "arp from %*D: addr len: new %d, i/f %d (ignored)",
636 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
637 ah->ar_hln, ifp->if_addrlen);
639 return;
641 memcpy(LLADDR(sdl), ar_sha(ah), sdl->sdl_alen = ah->ar_hln);
643 * If we receive an arp from a token-ring station over
644 * a token-ring nic then try to save the source
645 * routing info.
647 if (ifp->if_type == IFT_ISO88025) {
648 struct iso88025_header *th =
649 (struct iso88025_header *)m->m_pkthdr.header;
650 struct iso88025_sockaddr_dl_data *trld =
651 SDL_ISO88025(sdl);
652 int rif_len;
654 rif_len = TR_RCF_RIFLEN(th->rcf);
655 if ((th->iso88025_shost[0] & TR_RII) &&
656 (rif_len > 2)) {
657 trld->trld_rcf = th->rcf;
658 trld->trld_rcf ^= htons(TR_RCF_DIR);
659 memcpy(trld->trld_route, th->rd, rif_len - 2);
660 trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
662 * Set up source routing information for
663 * reply packet (XXX)
665 m->m_data -= rif_len;
666 m->m_len += rif_len;
667 m->m_pkthdr.len += rif_len;
668 } else {
669 th->iso88025_shost[0] &= ~TR_RII;
670 trld->trld_rcf = 0;
672 m->m_data -= 8;
673 m->m_len += 8;
674 m->m_pkthdr.len += 8;
675 th->rcf = trld->trld_rcf;
677 if (rt->rt_expire != 0)
678 rt->rt_expire = time_second + arpt_keep;
679 rt->rt_flags &= ~RTF_REJECT;
680 la->la_asked = 0;
681 la->la_preempt = arp_maxtries;
684 * This particular cpu might have been holding an mbuf
685 * pending ARP resolution. If so, transmit the mbuf now.
687 if (la->la_hold != NULL) {
688 m_adj(la->la_hold, sizeof(struct ether_header));
689 lwkt_serialize_enter(ifp->if_serializer);
690 (*ifp->if_output)(ifp, la->la_hold, rt_key(rt), rt);
691 lwkt_serialize_exit(ifp->if_serializer);
692 la->la_hold = NULL;
697 #ifdef SMP
699 struct netmsg_arp_update {
700 struct netmsg netmsg;
701 struct mbuf *m;
702 in_addr_t saddr;
703 boolean_t create;
706 static void arp_update_msghandler(struct netmsg *netmsg);
708 #endif
711 * Called from arpintr() - this routine is run from a single cpu.
713 static void
714 in_arpinput(struct mbuf *m)
716 struct arphdr *ah;
717 struct ifnet *ifp = m->m_pkthdr.rcvif;
718 struct ether_header *eh;
719 struct iso88025_header *th = (struct iso88025_header *)NULL;
720 struct rtentry *rt;
721 struct ifaddr *ifa;
722 struct in_ifaddr *ia;
723 struct sockaddr sa;
724 struct in_addr isaddr, itaddr, myaddr;
725 #ifdef SMP
726 struct netmsg_arp_update msg;
727 #endif
728 u_int8_t *enaddr = NULL;
729 int op;
730 int req_len;
732 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
733 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
734 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
735 return;
738 ah = mtod(m, struct arphdr *);
739 op = ntohs(ah->ar_op);
740 memcpy(&isaddr, ar_spa(ah), sizeof isaddr);
741 memcpy(&itaddr, ar_tpa(ah), sizeof itaddr);
743 * Check both target and sender IP addresses:
745 * If we receive the packet on the interface owning the address,
746 * then accept the address.
748 * For a bridge, we accept the address if the receive interface and
749 * the interface owning the address are on the same bridge.
750 * (This will change slightly when we have clusters of interfaces).
752 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
753 /* Skip all ia's which don't match */
754 if (itaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
755 continue;
757 if (ia->ia_ifp == ifp)
758 goto match;
760 if (ifp->if_bridge && ia->ia_ifp &&
761 ifp->if_bridge == ia->ia_ifp->if_bridge)
762 goto match;
764 #ifdef CARP
766 * If the interface does not match, but the recieving interface
767 * is part of carp, we call carp_iamatch to see if this is a
768 * request for the virtual host ip.
769 * XXX: This is really ugly!
771 if (ifp->if_carp != NULL &&
772 carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) &&
773 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
774 goto match;
775 #endif
777 LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash) {
778 /* Skip all ia's which don't match */
779 if (isaddr.s_addr != ia->ia_addr.sin_addr.s_addr)
780 continue;
782 if (ia->ia_ifp == ifp)
783 goto match;
785 if (ifp->if_bridge && ia->ia_ifp &&
786 ifp->if_bridge == ia->ia_ifp->if_bridge)
787 goto match;
790 * No match, use the first inet address on the receive interface
791 * as a dummy address for the rest of the function.
793 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
794 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
795 ia = ifatoia(ifa);
796 goto match;
800 * If we got here, we didn't find any suitable interface,
801 * so drop the packet.
803 m_freem(m);
804 return;
806 match:
807 if (!enaddr)
808 enaddr = (u_int8_t *)IF_LLADDR(ifp);
809 myaddr = ia->ia_addr.sin_addr;
810 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) {
811 m_freem(m); /* it's from me, ignore it. */
812 return;
814 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
815 log(LOG_ERR,
816 "arp: link address is broadcast for IP address %s!\n",
817 inet_ntoa(isaddr));
818 m_freem(m);
819 return;
821 if (isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
822 log(LOG_ERR,
823 "arp: %*D is using my IP address %s!\n",
824 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
825 inet_ntoa(isaddr));
826 itaddr = myaddr;
827 goto reply;
829 #ifdef SMP
830 netmsg_init(&msg.netmsg, &curthread->td_msgport, 0,
831 arp_update_msghandler);
832 msg.m = m;
833 msg.saddr = isaddr.s_addr;
834 msg.create = (itaddr.s_addr == myaddr.s_addr);
835 lwkt_domsg(rtable_portfn(0), &msg.netmsg.nm_lmsg, 0);
836 #endif
837 arp_update_oncpu(m, isaddr.s_addr, (itaddr.s_addr == myaddr.s_addr),
838 TRUE);
839 reply:
840 if (op != ARPOP_REQUEST) {
841 m_freem(m);
842 return;
844 if (itaddr.s_addr == myaddr.s_addr) {
845 /* I am the target */
846 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
847 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
848 } else {
849 struct llinfo_arp *la;
851 la = arplookup(itaddr.s_addr, FALSE, SIN_PROXY);
852 if (la == NULL) {
853 struct sockaddr_in sin;
855 if (!arp_proxyall) {
856 m_freem(m);
857 return;
860 bzero(&sin, sizeof sin);
861 sin.sin_family = AF_INET;
862 sin.sin_len = sizeof sin;
863 sin.sin_addr = itaddr;
865 rt = rtpurelookup((struct sockaddr *)&sin);
866 if (rt == NULL) {
867 m_freem(m);
868 return;
870 --rt->rt_refcnt;
872 * Don't send proxies for nodes on the same interface
873 * as this one came out of, or we'll get into a fight
874 * over who claims what Ether address.
876 if (rt->rt_ifp == ifp) {
877 m_freem(m);
878 return;
880 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
881 memcpy(ar_sha(ah), enaddr, ah->ar_hln);
882 #ifdef DEBUG_PROXY
883 kprintf("arp: proxying for %s\n", inet_ntoa(itaddr));
884 #endif
885 } else {
886 struct sockaddr_dl *sdl;
888 rt = la->la_rt;
889 memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
890 sdl = SDL(rt->rt_gateway);
891 memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
895 memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
896 memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
897 ah->ar_op = htons(ARPOP_REPLY);
898 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
899 switch (ifp->if_type) {
900 case IFT_ISO88025:
901 /* Re-arrange the source/dest address */
902 memcpy(th->iso88025_dhost, th->iso88025_shost,
903 sizeof th->iso88025_dhost);
904 memcpy(th->iso88025_shost, IF_LLADDR(ifp),
905 sizeof th->iso88025_shost);
906 /* Set the source routing bit if neccesary */
907 if (th->iso88025_dhost[0] & TR_RII) {
908 th->iso88025_dhost[0] &= ~TR_RII;
909 if (TR_RCF_RIFLEN(th->rcf) > 2)
910 th->iso88025_shost[0] |= TR_RII;
912 /* Copy the addresses, ac and fc into sa_data */
913 memcpy(sa.sa_data, th->iso88025_dhost,
914 (sizeof th->iso88025_dhost) * 2);
915 sa.sa_data[(sizeof th->iso88025_dhost) * 2] = TR_AC;
916 sa.sa_data[(sizeof th->iso88025_dhost) * 2 + 1] = TR_LLC_FRAME;
917 break;
918 case IFT_ETHER:
919 case IFT_FDDI:
921 * May not be correct for types not explictly
922 * listed, but it is our best guess.
924 default:
925 eh = (struct ether_header *)sa.sa_data;
926 memcpy(eh->ether_dhost, ar_tha(ah), sizeof eh->ether_dhost);
927 eh->ether_type = htons(ETHERTYPE_ARP);
928 break;
930 sa.sa_family = AF_UNSPEC;
931 sa.sa_len = sizeof sa;
932 lwkt_serialize_enter(ifp->if_serializer);
933 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
934 lwkt_serialize_exit(ifp->if_serializer);
935 return;
938 #ifdef SMP
940 static
941 void
942 arp_update_msghandler(struct netmsg *netmsg)
944 struct netmsg_arp_update *msg = (struct netmsg_arp_update *)netmsg;
945 int nextcpu;
947 arp_update_oncpu(msg->m, msg->saddr, msg->create, FALSE);
949 nextcpu = mycpuid + 1;
950 if (nextcpu < ncpus) {
951 lwkt_forwardmsg(rtable_portfn(nextcpu), &msg->netmsg.nm_lmsg);
952 } else {
953 lwkt_replymsg(&msg->netmsg.nm_lmsg, 0);
957 #endif
959 #endif
962 * Free an arp entry. If the arp entry is actively referenced or represents
963 * a static entry we only clear it back to an unresolved state, otherwise
964 * we destroy the entry entirely.
966 * Note that static entries are created when route add ... -interface is used
967 * to create an interface route to a (direct) destination.
969 static void
970 arptfree(struct llinfo_arp *la)
972 struct rtentry *rt = la->la_rt;
973 struct sockaddr_dl *sdl;
975 if (rt == NULL)
976 panic("arptfree");
977 sdl = SDL(rt->rt_gateway);
978 if (sdl != NULL &&
979 ((rt->rt_refcnt > 0 && sdl->sdl_family == AF_LINK) ||
980 (rt->rt_flags & RTF_STATIC))) {
981 sdl->sdl_alen = 0;
982 la->la_preempt = la->la_asked = 0;
983 rt->rt_flags &= ~RTF_REJECT;
984 return;
986 rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
990 * Lookup or enter a new address in arptab.
992 static struct llinfo_arp *
993 arplookup(in_addr_t addr, boolean_t create, boolean_t proxy)
995 struct rtentry *rt;
996 struct sockaddr_inarp sin = { sizeof sin, AF_INET };
997 const char *why = NULL;
999 sin.sin_addr.s_addr = addr;
1000 sin.sin_other = proxy ? SIN_PROXY : 0;
1001 if (create)
1002 rt = rtlookup((struct sockaddr *)&sin);
1003 else
1004 rt = rtpurelookup((struct sockaddr *)&sin);
1005 if (rt == NULL)
1006 return (NULL);
1007 rt->rt_refcnt--;
1009 if (rt->rt_flags & RTF_GATEWAY)
1010 why = "host is not on local network";
1011 else if (!(rt->rt_flags & RTF_LLINFO))
1012 why = "could not allocate llinfo";
1013 else if (rt->rt_gateway->sa_family != AF_LINK)
1014 why = "gateway route is not ours";
1016 if (why) {
1017 if (create) {
1018 log(LOG_DEBUG, "arplookup %s failed: %s\n",
1019 inet_ntoa(sin.sin_addr), why);
1021 if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_WASCLONED)) {
1022 /* No references to this route. Purge it. */
1023 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1024 rt_mask(rt), rt->rt_flags, NULL);
1026 return (NULL);
1028 return (rt->rt_llinfo);
1031 void
1032 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1034 if (IA_SIN(ifa)->sin_addr.s_addr != INADDR_ANY)
1035 arprequest(ifp, &IA_SIN(ifa)->sin_addr, &IA_SIN(ifa)->sin_addr,
1036 IF_LLADDR(ifp));
1037 ifa->ifa_rtrequest = arp_rtrequest;
1038 ifa->ifa_flags |= RTF_CLONING;
1041 void
1042 arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr)
1044 if (IA_SIN(ifa)->sin_addr.s_addr != INADDR_ANY)
1045 arprequest(ifp, &IA_SIN(ifa)->sin_addr, &IA_SIN(ifa)->sin_addr,
1046 enaddr);
1047 ifa->ifa_rtrequest = arp_rtrequest;
1048 ifa->ifa_flags |= RTF_CLONING;
1051 static void
1052 arp_init(void)
1054 int cpu;
1056 for (cpu = 0; cpu < ncpus2; cpu++)
1057 LIST_INIT(&llinfo_arp_list[cpu]);
1058 netisr_register(NETISR_ARP, cpu0_portfn, arpintr);
1061 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);