2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)if_ether.c 8.1 (Berkeley) 6/10/93
33 * Ethernet address resolution protocol.
35 * add "inuse/lock" bit (or ref. count) along with valid bit
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
46 #include <sys/kernel.h>
47 #include <sys/queue.h>
48 #include <sys/sysctl.h>
49 #include <sys/systm.h>
51 #include <sys/malloc.h>
53 #include <sys/socket.h>
54 #include <sys/syslog.h>
57 #include <net/if_dl.h>
58 #include <net/if_types.h>
59 #include <net/route.h>
60 #include <net/netisr.h>
61 #include <net/if_llc.h>
62 #include <net/ethernet.h>
64 #include <netinet/in.h>
65 #include <netinet/in_var.h>
66 #include <netinet/if_ether.h>
68 #include <net/if_arc.h>
69 #include <net/iso88025.h>
72 #include <netinet/ip_carp.h>
75 #include <security/mac/mac_framework.h>
77 #define SIN(s) ((struct sockaddr_in *)s)
78 #define SDL(s) ((struct sockaddr_dl *)s)
80 SYSCTL_DECL(_net_link_ether);
81 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
84 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
86 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
87 &arpt_keep, 0, "ARP entry lifetime in seconds");
89 #define rt_expire rt_rmx.rmx_expire
92 struct callout la_timer;
93 struct rtentry *la_rt;
94 struct mbuf *la_hold; /* last packet until resolved/timeout */
95 u_short la_preempt; /* countdown for pre-expiry arps */
96 u_short la_asked; /* # requests sent */
99 static struct ifqueue arpintrq;
100 static int arp_allocated;
102 static int arp_maxtries = 5;
103 static int useloopback = 1; /* use loopback interface for local traffic */
104 static int arp_proxyall = 0;
106 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
107 &arp_maxtries, 0, "ARP resolution attempts before returning error");
108 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
109 &useloopback, 0, "Use the loopback interface for local traffic");
110 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
111 &arp_proxyall, 0, "Enable proxy ARP for all suitable requests");
113 static void arp_init(void);
114 static void arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
115 static void arprequest(struct ifnet *,
116 struct in_addr *, struct in_addr *, u_char *);
117 static void arpintr(struct mbuf *);
118 static void arptimer(void *);
119 static struct rtentry
120 *arplookup(u_long, int, int, int);
122 static void in_arpinput(struct mbuf *);
131 struct rtentry *rt = (struct rtentry *)arg;
135 * The lock is needed to close a theoretical race
136 * between spontaneous expiry and intentional removal.
137 * We still got an extra reference on rtentry, so can
138 * safely pass pointers to its contents.
142 in_rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL,
147 * Parallel to llc_rtrequest.
150 arp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
152 struct sockaddr *gate;
153 struct llinfo_arp *la;
154 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
155 struct in_ifaddr *ia;
160 if (rt->rt_flags & RTF_GATEWAY)
162 gate = rt->rt_gateway;
163 la = (struct llinfo_arp *)rt->rt_llinfo;
168 * XXX: If this is a manually added route to interface
169 * such as older version of routed or gated might provide,
170 * restore cloning bit.
172 if ((rt->rt_flags & RTF_HOST) == 0 &&
173 rt_mask(rt) != NULL &&
174 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
175 rt->rt_flags |= RTF_CLONING;
176 if (rt->rt_flags & RTF_CLONING) {
178 * Case 1: This route should come from a route to iface.
180 rt_setgate(rt, rt_key(rt),
181 (struct sockaddr *)&null_sdl);
182 gate = rt->rt_gateway;
183 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
184 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
185 rt->rt_expire = time_uptime;
188 /* Announce a new entry if requested. */
189 if (rt->rt_flags & RTF_ANNOUNCE)
190 arprequest(rt->rt_ifp,
191 &SIN(rt_key(rt))->sin_addr,
192 &SIN(rt_key(rt))->sin_addr,
193 (u_char *)LLADDR(SDL(gate)));
196 if (gate->sa_family != AF_LINK ||
197 gate->sa_len < sizeof(null_sdl)) {
198 log(LOG_DEBUG, "%s: bad gateway %s%s\n", __func__,
199 inet_ntoa(SIN(rt_key(rt))->sin_addr),
200 (gate->sa_family != AF_LINK) ?
204 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
205 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
207 break; /* This happens on a route change */
209 * Case 2: This route may come from cloning, or a manual route
210 * add with a LL address.
212 R_Zalloc(la, struct llinfo_arp *, sizeof(*la));
213 rt->rt_llinfo = (caddr_t)la;
215 log(LOG_DEBUG, "%s: malloc failed\n", __func__);
220 * We are storing a route entry outside of radix tree. So,
221 * it can be found and accessed by other means than radix
222 * lookup. The routing code assumes that any rtentry detached
223 * from radix can be destroyed safely. To prevent this, we
224 * add an additional reference.
228 rt->rt_flags |= RTF_LLINFO;
229 callout_init_mtx(&la->la_timer, &rt->rt_mtx,
230 CALLOUT_RETURNUNLOCKED);
234 * This keeps the multicast addresses from showing up
235 * in `arp -a' listings as unresolved. It's not actually
236 * functional. Then the same for broadcast.
238 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) &&
239 rt->rt_ifp->if_type != IFT_ARCNET) {
240 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
242 SDL(gate)->sdl_alen = 6;
245 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
246 memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
247 rt->rt_ifp->if_addrlen);
248 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
253 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
254 if (ia->ia_ifp == rt->rt_ifp &&
255 SIN(rt_key(rt))->sin_addr.s_addr ==
256 (IA_SIN(ia))->sin_addr.s_addr)
261 * This test used to be
262 * if (loif.if_flags & IFF_UP)
263 * It allowed local traffic to be forced
264 * through the hardware by configuring the loopback down.
265 * However, it causes problems during network configuration
266 * for boards that can't receive packets they send.
267 * It is now necessary to clear "useloopback" and remove
268 * the route to force traffic out to the hardware.
271 bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
272 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
275 rt->rt_rmx.rmx_mtu = loif->if_mtu;
279 * make sure to set rt->rt_ifa to the interface
280 * address we are using, otherwise we will have trouble
281 * with source address selection.
284 if (ifa != rt->rt_ifa) {
293 if (la == NULL) /* XXX: at least CARP does this. */
295 callout_stop(&la->la_timer);
296 rt->rt_llinfo = NULL;
297 rt->rt_flags &= ~RTF_LLINFO;
300 m_freem(la->la_hold);
306 * Broadcast an ARP request. Caller specifies:
307 * - arp header source ip address
308 * - arp header target ip address
309 * - arp header source ethernet address
312 arprequest(struct ifnet *ifp, struct in_addr *sip, struct in_addr *tip,
319 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
321 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
322 2*ifp->if_data.ifi_addrlen;
323 m->m_pkthdr.len = m->m_len;
324 MH_ALIGN(m, m->m_len);
325 ah = mtod(m, struct arphdr *);
326 bzero((caddr_t)ah, m->m_len);
328 mac_create_mbuf_linklayer(ifp, m);
330 ah->ar_pro = htons(ETHERTYPE_IP);
331 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
332 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
333 ah->ar_op = htons(ARPOP_REQUEST);
334 bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
335 bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
336 bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
337 sa.sa_family = AF_ARP;
339 m->m_flags |= M_BCAST;
340 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
346 * Resolve an IP address into an ethernet address.
348 * ifp is the interface we use
349 * dst is the next hop,
350 * rt0 is the route to the final destination (possibly useless)
352 * desten is where we want the address.
354 * On success, desten is filled in and the function returns 0;
355 * If the packet must be held pending resolution, we return EWOULDBLOCK
356 * On other errors, we return the corresponding error code.
359 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
360 struct sockaddr *dst, u_char *desten)
362 struct llinfo_arp *la = NULL;
363 struct rtentry *rt = NULL;
364 struct sockaddr_dl *sdl;
370 if (m->m_flags & M_BCAST) { /* broadcast */
371 (void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
374 if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */
375 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
378 fibnum = M_GETFIB(m);
382 /* Look for a cached arp (ll) entry. */
383 error = rt_check(&rt, &rt0, dst);
388 la = (struct llinfo_arp *)rt->rt_llinfo;
394 * If we had no mbuf and no route, then hope the caller
395 * has a fib in mind because we are running out of ideas.
396 * I think this should not happen in current code.
397 * (kmacy would know).
400 fibnum = curthread->td_proc->p_fibnum; /* last gasp */
404 * We enter this block if rt0 was NULL,
405 * or if rt found by rt_check() didn't have llinfo.
406 * We should get a cloned route from the local interface,
407 * so it should have an ll entry.
408 * It may be incomplete but that's ok.
410 rt = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0, fibnum);
413 "arpresolve: can't allocate route for %s\n",
414 inet_ntoa(SIN(dst)->sin_addr));
416 return (EINVAL); /* XXX */
418 la = (struct llinfo_arp *)rt->rt_llinfo;
422 "arpresolve: can't allocate llinfo for %s\n",
423 inet_ntoa(SIN(dst)->sin_addr));
425 return (EINVAL); /* XXX */
428 sdl = SDL(rt->rt_gateway);
430 * Check the address family and length is valid, the address
431 * is resolved; otherwise, try to resolve.
433 if ((rt->rt_expire == 0 || rt->rt_expire > time_uptime) &&
434 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
436 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
439 * If entry has an expiry time and it is approaching,
440 * send an ARP request.
442 if ((rt->rt_expire != 0) &&
443 (time_uptime + la->la_preempt > rt->rt_expire)) {
445 SIN(rt->rt_ifa->ifa_addr)->sin_addr;
449 arprequest(ifp, &sin, &SIN(dst)->sin_addr,
458 * If ARP is disabled or static on this interface, stop.
460 * Probably should not allocate empty llinfo struct if we are
461 * not going to be sending out an arp request.
463 if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
469 * There is an arptab entry, but no ethernet address
470 * response yet. Replace the held mbuf with this
475 m_freem(la->la_hold);
479 KASSERT(rt->rt_expire > 0, ("sending ARP request for static entry"));
482 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
483 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
484 * if we have already sent arp_maxtries ARP requests. Retransmit the
485 * ARP request, but not faster than one request per second.
487 if (la->la_asked < arp_maxtries)
488 error = EWOULDBLOCK; /* First request. */
490 error = (rt == rt0) ? EHOSTDOWN : EHOSTUNREACH;
492 if (la->la_asked == 0 || rt->rt_expire != time_uptime) {
494 SIN(rt->rt_ifa->ifa_addr)->sin_addr;
496 rt->rt_expire = time_uptime;
497 callout_reset(&la->la_timer, hz, arptimer, rt);
501 arprequest(ifp, &sin, &SIN(dst)->sin_addr,
510 * Common length and type checks are done here,
511 * then the protocol-specific routine is called.
514 arpintr(struct mbuf *m)
518 if (m->m_len < sizeof(struct arphdr) &&
519 ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
520 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
523 ar = mtod(m, struct arphdr *);
525 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER &&
526 ntohs(ar->ar_hrd) != ARPHRD_IEEE802 &&
527 ntohs(ar->ar_hrd) != ARPHRD_ARCNET &&
528 ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) {
529 log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
530 (unsigned char *)&ar->ar_hrd, "");
535 if (m->m_len < arphdr_len(ar)) {
536 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
537 log(LOG_ERR, "arp: runt packet\n");
541 ar = mtod(m, struct arphdr *);
544 switch (ntohs(ar->ar_pro)) {
556 * ARP for Internet protocols on 10 Mb/s Ethernet.
557 * Algorithm is that given in RFC 826.
558 * In addition, a sanity check is performed on the sender
559 * protocol address, to catch impersonators.
560 * We no longer handle negotiations for use of trailer protocol:
561 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
562 * along with IP replies if we wanted trailers sent to us,
563 * and also sent them in response to IP replies.
564 * This allowed either end to announce the desire to receive
566 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
567 * but formerly didn't normally send requests.
569 static int log_arp_wrong_iface = 1;
570 static int log_arp_movements = 1;
571 static int log_arp_permanent_modify = 1;
573 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
574 &log_arp_wrong_iface, 0,
575 "log arp packets arriving on the wrong interface");
576 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
577 &log_arp_movements, 0,
578 "log arp replies from MACs different than the one in the cache");
579 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
580 &log_arp_permanent_modify, 0,
581 "log arp replies from MACs different than the one in the permanent arp entry");
585 in_arpinput(struct mbuf *m)
588 struct ifnet *ifp = m->m_pkthdr.rcvif;
589 struct llinfo_arp *la;
592 struct in_ifaddr *ia;
593 struct sockaddr_dl *sdl;
595 struct in_addr isaddr, itaddr, myaddr;
597 u_int8_t *enaddr = NULL;
600 int bridged = 0, is_bridge = 0;
610 if (ifp->if_type == IFT_BRIDGE)
613 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
614 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
615 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
619 ah = mtod(m, struct arphdr *);
620 op = ntohs(ah->ar_op);
621 (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
622 (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
625 * For a bridge, we want to check the address irrespective
626 * of the receive interface. (This will change slightly
627 * when we have clusters of interfaces).
628 * If the interface does not match, but the recieving interface
629 * is part of carp, we call carp_iamatch to see if this is a
630 * request for the virtual host ip.
631 * XXX: This is really ugly!
633 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
634 if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
635 (ia->ia_ifp == ifp)) &&
636 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
639 if (ifp->if_carp != NULL &&
640 carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) &&
641 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
647 LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
648 if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
649 (ia->ia_ifp == ifp)) &&
650 isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
653 #define BDG_MEMBER_MATCHES_ARP(addr, ifp, ia) \
654 (ia->ia_ifp->if_bridge == ifp->if_softc && \
655 !bcmp(IF_LLADDR(ia->ia_ifp), IF_LLADDR(ifp), ifp->if_addrlen) && \
656 addr == ia->ia_addr.sin_addr.s_addr)
658 * Check the case when bridge shares its MAC address with
659 * some of its children, so packets are claimed by bridge
660 * itself (bridge_input() does it first), but they are really
661 * meant to be destined to the bridge member.
664 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
665 if (BDG_MEMBER_MATCHES_ARP(itaddr.s_addr, ifp, ia)) {
671 #undef BDG_MEMBER_MATCHES_ARP
674 * No match, use the first inet address on the receive interface
675 * as a dummy address for the rest of the function.
677 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
678 if (ifa->ifa_addr->sa_family == AF_INET) {
683 * If bridging, fall back to using any inet address.
685 if (!bridged || (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL)
689 enaddr = (u_int8_t *)IF_LLADDR(ifp);
690 myaddr = ia->ia_addr.sin_addr;
691 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
692 goto drop; /* it's from me, ignore it. */
693 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
695 "arp: link address is broadcast for IP address %s!\n",
700 * Warn if another host is using the same IP address, but only if the
701 * IP address isn't 0.0.0.0, which is used for DHCP only, in which
702 * case we suppress the warning to avoid false positive complaints of
703 * potential misconfiguration.
705 if (!bridged && isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
707 "arp: %*D is using my IP address %s on %s!\n",
708 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
709 inet_ntoa(isaddr), ifp->if_xname);
713 if (ifp->if_flags & IFF_STATICARP)
716 * We look for any FIBs that has this address to find
718 * For sanity checks that are FIB independent we abort the loop.
720 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
721 rt = arplookup(isaddr.s_addr,
722 itaddr.s_addr == myaddr.s_addr, 0, fibnum);
726 sdl = SDL(rt->rt_gateway);
728 struct sockaddr_in sin;
730 sin.sin_len = sizeof(struct sockaddr_in);
731 sin.sin_family = AF_INET;
732 sin.sin_addr.s_addr = isaddr.s_addr;
733 EVENTHANDLER_INVOKE(route_arp_update_event, rt,
734 ar_sha(ah), (struct sockaddr *)&sin);
736 la = (struct llinfo_arp *)rt->rt_llinfo;
743 /* The following is not an error when doing bridging. */
744 if (!bridged && rt->rt_ifp != ifp
746 && (ifp->if_type != IFT_CARP || !carp_match)
749 if (log_arp_wrong_iface)
750 log(LOG_ERR, "arp: %s is on %s "
751 "but got reply from %*D "
754 rt->rt_ifp->if_xname,
756 (u_char *)ar_sha(ah), ":",
762 bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
764 if (log_arp_movements)
766 "arp: %s moved from %*D to %*D "
770 (u_char *)LLADDR(sdl), ":",
772 (u_char *)ar_sha(ah), ":",
776 if (log_arp_permanent_modify)
778 "arp: %*D attempts to "
779 "modify permanent entry "
782 (u_char *)ar_sha(ah), ":",
789 * sanity check for the address length.
790 * XXX this does not work for protocols
791 * with variable address length. -is
794 sdl->sdl_alen != ah->ar_hln) {
796 "arp from %*D: new addr len %d, was %d",
797 ifp->if_addrlen, (u_char *) ar_sha(ah),
798 ":", ah->ar_hln, sdl->sdl_alen);
800 if (ifp->if_addrlen != ah->ar_hln) {
802 "arp from %*D: addr len: "
803 "new %d, i/f %d (ignored)",
804 ifp->if_addrlen, (u_char *) ar_sha(ah),
805 ":", ah->ar_hln, ifp->if_addrlen);
813 /* Copy in the information received. */
814 (void)memcpy(LLADDR(sdl), ar_sha(ah),
815 sdl->sdl_alen = ah->ar_hln);
817 * If we receive an arp from a token-ring station over
818 * a token-ring nic then try to save the source routing info.
819 * XXXMRT Only minimal Token Ring support for MRT.
820 * Only do this on the first pass as if modifies the mbuf.
822 if (ifp->if_type == IFT_ISO88025) {
823 struct iso88025_header *th = NULL;
824 struct iso88025_sockaddr_dl_data *trld;
826 /* force the fib loop to end after this pass */
827 fibnum = rt_numfibs - 1;
829 th = (struct iso88025_header *)m->m_pkthdr.header;
830 trld = SDL_ISO88025(sdl);
831 rif_len = TR_RCF_RIFLEN(th->rcf);
832 if ((th->iso88025_shost[0] & TR_RII) &&
834 trld->trld_rcf = th->rcf;
835 trld->trld_rcf ^= htons(TR_RCF_DIR);
836 memcpy(trld->trld_route, th->rd, rif_len - 2);
837 trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
839 * Set up source routing information for
842 m->m_data -= rif_len;
844 m->m_pkthdr.len += rif_len;
846 th->iso88025_shost[0] &= ~TR_RII;
851 m->m_pkthdr.len += 8;
852 th->rcf = trld->trld_rcf;
856 rt->rt_expire = time_uptime + arpt_keep;
857 callout_reset(&la->la_timer, hz * arpt_keep,
861 la->la_preempt = arp_maxtries;
866 (*ifp->if_output)(ifp, hold, rt_key(rt), rt);
867 } /* end of FIB loop */
871 * Decide if we have to respond to something.
873 if (op != ARPOP_REQUEST)
875 if (itaddr.s_addr == myaddr.s_addr) {
876 /* Shortcut.. the receiving interface is the target. */
877 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
878 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
880 /* It's not asking for our address. But it still may
881 * be something we should answer.
884 * We assume that link level info is independent of
885 * the table used and so we use whichever we can and don't
886 * have a better option.
888 /* Have we been asked to proxy for the target. */
889 rt = arplookup(itaddr.s_addr, 0, SIN_PROXY, goodfib);
891 /* Nope, only intersted now if proxying everything. */
892 struct sockaddr_in sin;
897 bzero(&sin, sizeof sin);
898 sin.sin_family = AF_INET;
899 sin.sin_len = sizeof sin;
900 sin.sin_addr = itaddr;
902 /* XXX MRT use table 0 for arp reply */
903 rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0);
907 * Don't send proxies for nodes on the same interface
908 * as this one came out of, or we'll get into a fight
909 * over who claims what Ether address.
911 if (rt->rt_ifp == ifp) {
915 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
916 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
920 * Also check that the node which sent the ARP packet
921 * is on the the interface we expect it to be on. This
922 * avoids ARP chaos if an interface is connected to the
925 sin.sin_addr = isaddr;
927 /* XXX MRT use table 0 for arp checks */
928 rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0);
931 if (rt->rt_ifp != ifp) {
932 log(LOG_INFO, "arp_proxy: ignoring request"
933 " from %s via %s, expecting %s\n",
934 inet_ntoa(isaddr), ifp->if_xname,
935 rt->rt_ifp->if_xname);
942 printf("arp: proxying for %s\n",
947 * Return proxied ARP replies only on the interface
948 * or bridge cluster where this network resides.
949 * Otherwise we may conflict with the host we are
952 if (rt->rt_ifp != ifp &&
953 (rt->rt_ifp->if_bridge != ifp->if_bridge ||
954 ifp->if_bridge == NULL)) {
958 sdl = SDL(rt->rt_gateway);
959 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
960 (void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
965 if (itaddr.s_addr == myaddr.s_addr &&
966 IN_LINKLOCAL(ntohl(itaddr.s_addr))) {
967 /* RFC 3927 link-local IPv4; always reply by broadcast. */
968 #ifdef DEBUG_LINKLOCAL
969 printf("arp: sending reply for link-local addr %s\n",
972 m->m_flags |= M_BCAST;
973 m->m_flags &= ~M_MCAST;
975 /* default behaviour; never reply by broadcast. */
976 m->m_flags &= ~(M_BCAST|M_MCAST);
978 (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
979 (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
980 ah->ar_op = htons(ARPOP_REPLY);
981 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
982 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
983 m->m_pkthdr.len = m->m_len;
984 sa.sa_family = AF_ARP;
986 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
995 * Lookup or enter a new address in arptab.
997 static struct rtentry *
998 arplookup(u_long addr, int create, int proxy, int fibnum)
1001 struct sockaddr_inarp sin;
1002 const char *why = 0;
1004 bzero(&sin, sizeof(sin));
1005 sin.sin_len = sizeof(sin);
1006 sin.sin_family = AF_INET;
1007 sin.sin_addr.s_addr = addr;
1009 sin.sin_other = SIN_PROXY;
1010 rt = in_rtalloc1((struct sockaddr *)&sin, create, 0UL, fibnum);
1014 if (rt->rt_flags & RTF_GATEWAY)
1015 why = "host is not on local network";
1016 else if ((rt->rt_flags & RTF_LLINFO) == 0)
1017 why = "could not allocate llinfo";
1018 else if (rt->rt_gateway->sa_family != AF_LINK)
1019 why = "gateway route is not ours";
1022 #define ISDYNCLONE(_rt) \
1023 (((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED)
1025 log(LOG_DEBUG, "arplookup %s failed: %s\n",
1026 inet_ntoa(sin.sin_addr), why);
1028 * If there are no references to this Layer 2 route,
1029 * and it is a cloned route, and not static, and
1030 * arplookup() is creating the route, then purge
1031 * it from the routing table as it is probably bogus.
1033 if (rt->rt_refcnt == 1 && ISDYNCLONE(rt))
1045 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1047 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
1048 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
1049 &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
1050 ifa->ifa_rtrequest = arp_rtrequest;
1051 ifa->ifa_flags |= RTF_CLONING;
1055 arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr)
1057 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
1058 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
1059 &IA_SIN(ifa)->sin_addr, enaddr);
1060 ifa->ifa_rtrequest = arp_rtrequest;
1061 ifa->ifa_flags |= RTF_CLONING;
1068 arpintrq.ifq_maxlen = 50;
1069 mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF);
1070 netisr_register(NETISR_ARP, arpintr, &arpintrq, NETISR_MPSAFE);
1072 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);