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
34 * Ethernet address resolution protocol.
36 * add "inuse/lock" bit (or ref. count) along with valid bit
43 #include <sys/param.h>
44 #include <sys/kernel.h>
45 #include <sys/queue.h>
46 #include <sys/sysctl.h>
47 #include <sys/systm.h>
49 #include <sys/malloc.h>
50 #include <sys/socket.h>
51 #include <sys/syslog.h>
54 #include <net/if_dl.h>
55 #include <net/if_types.h>
56 #include <net/route.h>
57 #include <net/netisr.h>
58 #include <net/if_llc.h>
59 #include <net/ethernet.h>
61 #include <netinet/in.h>
62 #include <netinet/in_var.h>
63 #include <netinet/if_ether.h>
65 #include <net/if_arc.h>
66 #include <net/iso88025.h>
69 #include <netinet/ip_carp.h>
72 #include <security/mac/mac_framework.h>
74 #define SIN(s) ((struct sockaddr_in *)s)
75 #define SDL(s) ((struct sockaddr_dl *)s)
77 SYSCTL_DECL(_net_link_ether);
78 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
81 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
83 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
84 &arpt_keep, 0, "ARP entry lifetime in seconds");
86 #define rt_expire rt_rmx.rmx_expire
89 struct callout la_timer;
90 struct rtentry *la_rt;
91 struct mbuf *la_hold; /* last packet until resolved/timeout */
92 u_short la_preempt; /* countdown for pre-expiry arps */
93 u_short la_asked; /* # requests sent */
96 static struct ifqueue arpintrq;
97 static int arp_allocated;
99 static int arp_maxtries = 5;
100 static int useloopback = 1; /* use loopback interface for local traffic */
101 static int arp_proxyall = 0;
103 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
104 &arp_maxtries, 0, "ARP resolution attempts before returning error");
105 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
106 &useloopback, 0, "Use the loopback interface for local traffic");
107 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
108 &arp_proxyall, 0, "Enable proxy ARP for all suitable requests");
110 static void arp_init(void);
111 static void arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
112 static void arprequest(struct ifnet *,
113 struct in_addr *, struct in_addr *, u_char *);
114 static void arpintr(struct mbuf *);
115 static void arptimer(void *);
116 static struct rtentry
117 *arplookup(u_long, int, int);
119 static void in_arpinput(struct mbuf *);
128 struct rtentry *rt = (struct rtentry *)arg;
132 * The lock is needed to close a theoretical race
133 * between spontaneous expiry and intentional removal.
134 * We still got an extra reference on rtentry, so can
135 * safely pass pointers to its contents.
139 rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
143 * Parallel to llc_rtrequest.
146 arp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
148 struct sockaddr *gate;
149 struct llinfo_arp *la;
150 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
151 struct in_ifaddr *ia;
156 if (rt->rt_flags & RTF_GATEWAY)
158 gate = rt->rt_gateway;
159 la = (struct llinfo_arp *)rt->rt_llinfo;
164 * XXX: If this is a manually added route to interface
165 * such as older version of routed or gated might provide,
166 * restore cloning bit.
168 if ((rt->rt_flags & RTF_HOST) == 0 &&
169 rt_mask(rt) != NULL &&
170 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
171 rt->rt_flags |= RTF_CLONING;
172 if (rt->rt_flags & RTF_CLONING) {
174 * Case 1: This route should come from a route to iface.
176 rt_setgate(rt, rt_key(rt),
177 (struct sockaddr *)&null_sdl);
178 gate = rt->rt_gateway;
179 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
180 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
181 rt->rt_expire = time_uptime;
184 /* Announce a new entry if requested. */
185 if (rt->rt_flags & RTF_ANNOUNCE)
186 arprequest(rt->rt_ifp,
187 &SIN(rt_key(rt))->sin_addr,
188 &SIN(rt_key(rt))->sin_addr,
189 (u_char *)LLADDR(SDL(gate)));
192 if (gate->sa_family != AF_LINK ||
193 gate->sa_len < sizeof(null_sdl)) {
194 log(LOG_DEBUG, "%s: bad gateway %s%s\n", __func__,
195 inet_ntoa(SIN(rt_key(rt))->sin_addr),
196 (gate->sa_family != AF_LINK) ?
200 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
201 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
203 break; /* This happens on a route change */
205 * Case 2: This route may come from cloning, or a manual route
206 * add with a LL address.
208 R_Zalloc(la, struct llinfo_arp *, sizeof(*la));
209 rt->rt_llinfo = (caddr_t)la;
211 log(LOG_DEBUG, "%s: malloc failed\n", __func__);
216 * We are storing a route entry outside of radix tree. So,
217 * it can be found and accessed by other means than radix
218 * lookup. The routing code assumes that any rtentry detached
219 * from radix can be destroyed safely. To prevent this, we
220 * add an additional reference.
224 rt->rt_flags |= RTF_LLINFO;
225 callout_init_mtx(&la->la_timer, &rt->rt_mtx,
226 CALLOUT_RETURNUNLOCKED);
230 * This keeps the multicast addresses from showing up
231 * in `arp -a' listings as unresolved. It's not actually
232 * functional. Then the same for broadcast.
234 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) &&
235 rt->rt_ifp->if_type != IFT_ARCNET) {
236 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
238 SDL(gate)->sdl_alen = 6;
241 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
242 memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
243 rt->rt_ifp->if_addrlen);
244 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
249 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
250 if (ia->ia_ifp == rt->rt_ifp &&
251 SIN(rt_key(rt))->sin_addr.s_addr ==
252 (IA_SIN(ia))->sin_addr.s_addr)
257 * This test used to be
258 * if (loif.if_flags & IFF_UP)
259 * It allowed local traffic to be forced
260 * through the hardware by configuring the loopback down.
261 * However, it causes problems during network configuration
262 * for boards that can't receive packets they send.
263 * It is now necessary to clear "useloopback" and remove
264 * the route to force traffic out to the hardware.
267 bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
268 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
271 rt->rt_rmx.rmx_mtu = loif->if_mtu;
275 * make sure to set rt->rt_ifa to the interface
276 * address we are using, otherwise we will have trouble
277 * with source address selection.
280 if (ifa != rt->rt_ifa) {
289 if (la == NULL) /* XXX: at least CARP does this. */
291 callout_stop(&la->la_timer);
292 rt->rt_llinfo = NULL;
293 rt->rt_flags &= ~RTF_LLINFO;
296 m_freem(la->la_hold);
302 * Broadcast an ARP request. Caller specifies:
303 * - arp header source ip address
304 * - arp header target ip address
305 * - arp header source ethernet address
308 arprequest(struct ifnet *ifp, struct in_addr *sip, struct in_addr *tip,
315 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
317 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
318 2*ifp->if_data.ifi_addrlen;
319 m->m_pkthdr.len = m->m_len;
320 MH_ALIGN(m, m->m_len);
321 ah = mtod(m, struct arphdr *);
322 bzero((caddr_t)ah, m->m_len);
324 mac_create_mbuf_linklayer(ifp, m);
326 ah->ar_pro = htons(ETHERTYPE_IP);
327 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
328 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
329 ah->ar_op = htons(ARPOP_REQUEST);
330 bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
331 bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
332 bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
333 sa.sa_family = AF_ARP;
335 m->m_flags |= M_BCAST;
336 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
342 * Resolve an IP address into an ethernet address.
344 * ifp is the interface we use
345 * dst is the next hop,
346 * rt0 is the route to the final destination (possibly useless)
348 * desten is where we want the address.
350 * On success, desten is filled in and the function returns 0;
351 * If the packet must be held pending resolution, we return EWOULDBLOCK
352 * On other errors, we return the corresponding error code.
355 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
356 struct sockaddr *dst, u_char *desten)
358 struct llinfo_arp *la = NULL;
359 struct rtentry *rt = NULL;
360 struct sockaddr_dl *sdl;
363 if (m->m_flags & M_BCAST) { /* broadcast */
364 (void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
367 if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */
368 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
373 error = rt_check(&rt, &rt0, dst);
378 la = (struct llinfo_arp *)rt->rt_llinfo;
384 * We enter this block in case if rt0 was NULL,
385 * or if rt found by rt_check() didn't have llinfo.
387 rt = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
390 "arpresolve: can't allocate route for %s\n",
391 inet_ntoa(SIN(dst)->sin_addr));
393 return (EINVAL); /* XXX */
395 la = (struct llinfo_arp *)rt->rt_llinfo;
399 "arpresolve: can't allocate llinfo for %s\n",
400 inet_ntoa(SIN(dst)->sin_addr));
402 return (EINVAL); /* XXX */
405 sdl = SDL(rt->rt_gateway);
407 * Check the address family and length is valid, the address
408 * is resolved; otherwise, try to resolve.
410 if ((rt->rt_expire == 0 || rt->rt_expire > time_uptime) &&
411 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
413 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
416 * If entry has an expiry time and it is approaching,
417 * send an ARP request.
419 if ((rt->rt_expire != 0) &&
420 (time_uptime + la->la_preempt > rt->rt_expire)) {
422 SIN(rt->rt_ifa->ifa_addr)->sin_addr;
426 arprequest(ifp, &sin, &SIN(dst)->sin_addr,
435 * If ARP is disabled or static on this interface, stop.
437 * Probably should not allocate empty llinfo struct if we are
438 * not going to be sending out an arp request.
440 if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
446 * There is an arptab entry, but no ethernet address
447 * response yet. Replace the held mbuf with this
451 m_freem(la->la_hold);
454 KASSERT(rt->rt_expire > 0, ("sending ARP request for static entry"));
457 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
458 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
459 * if we have already sent arp_maxtries ARP requests. Retransmit the
460 * ARP request, but not faster than one request per second.
462 if (la->la_asked < arp_maxtries)
463 error = EWOULDBLOCK; /* First request. */
465 error = (rt == rt0) ? EHOSTDOWN : EHOSTUNREACH;
467 if (la->la_asked == 0 || rt->rt_expire != time_uptime) {
469 SIN(rt->rt_ifa->ifa_addr)->sin_addr;
471 rt->rt_expire = time_uptime;
472 callout_reset(&la->la_timer, hz, arptimer, rt);
476 arprequest(ifp, &sin, &SIN(dst)->sin_addr,
485 * Common length and type checks are done here,
486 * then the protocol-specific routine is called.
489 arpintr(struct mbuf *m)
493 if (m->m_len < sizeof(struct arphdr) &&
494 ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
495 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
498 ar = mtod(m, struct arphdr *);
500 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER &&
501 ntohs(ar->ar_hrd) != ARPHRD_IEEE802 &&
502 ntohs(ar->ar_hrd) != ARPHRD_ARCNET &&
503 ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) {
504 log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
505 (unsigned char *)&ar->ar_hrd, "");
510 if (m->m_len < arphdr_len(ar)) {
511 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
512 log(LOG_ERR, "arp: runt packet\n");
516 ar = mtod(m, struct arphdr *);
519 switch (ntohs(ar->ar_pro)) {
531 * ARP for Internet protocols on 10 Mb/s Ethernet.
532 * Algorithm is that given in RFC 826.
533 * In addition, a sanity check is performed on the sender
534 * protocol address, to catch impersonators.
535 * We no longer handle negotiations for use of trailer protocol:
536 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
537 * along with IP replies if we wanted trailers sent to us,
538 * and also sent them in response to IP replies.
539 * This allowed either end to announce the desire to receive
541 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
542 * but formerly didn't normally send requests.
544 static int log_arp_wrong_iface = 1;
545 static int log_arp_movements = 1;
546 static int log_arp_permanent_modify = 1;
548 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
549 &log_arp_wrong_iface, 0,
550 "log arp packets arriving on the wrong interface");
551 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
552 &log_arp_movements, 0,
553 "log arp replies from MACs different than the one in the cache");
554 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
555 &log_arp_permanent_modify, 0,
556 "log arp replies from MACs different than the one in the permanent arp entry");
560 in_arpinput(struct mbuf *m)
563 struct ifnet *ifp = m->m_pkthdr.rcvif;
564 struct llinfo_arp *la;
567 struct in_ifaddr *ia;
568 struct sockaddr_dl *sdl;
570 struct in_addr isaddr, itaddr, myaddr;
572 u_int8_t *enaddr = NULL;
583 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
584 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
585 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
589 ah = mtod(m, struct arphdr *);
590 op = ntohs(ah->ar_op);
591 (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
592 (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
595 * For a bridge, we want to check the address irrespective
596 * of the receive interface. (This will change slightly
597 * when we have clusters of interfaces).
598 * If the interface does not match, but the recieving interface
599 * is part of carp, we call carp_iamatch to see if this is a
600 * request for the virtual host ip.
601 * XXX: This is really ugly!
603 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
604 if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
605 (ia->ia_ifp == ifp)) &&
606 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
609 if (ifp->if_carp != NULL &&
610 carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) &&
611 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
617 LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
618 if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
619 (ia->ia_ifp == ifp)) &&
620 isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
623 * No match, use the first inet address on the receive interface
624 * as a dummy address for the rest of the function.
626 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
627 if (ifa->ifa_addr->sa_family == AF_INET) {
632 * If bridging, fall back to using any inet address.
634 if (!bridged || (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL)
638 enaddr = (u_int8_t *)IF_LLADDR(ifp);
639 myaddr = ia->ia_addr.sin_addr;
640 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
641 goto drop; /* it's from me, ignore it. */
642 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
644 "arp: link address is broadcast for IP address %s!\n",
649 * Warn if another host is using the same IP address, but only if the
650 * IP address isn't 0.0.0.0, which is used for DHCP only, in which
651 * case we suppress the warning to avoid false positive complaints of
652 * potential misconfiguration.
654 if (!bridged && isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
656 "arp: %*D is using my IP address %s!\n",
657 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
662 if (ifp->if_flags & IFF_STATICARP)
664 rt = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
666 la = (struct llinfo_arp *)rt->rt_llinfo;
674 /* The following is not an error when doing bridging. */
675 if (!bridged && rt->rt_ifp != ifp
677 && (ifp->if_type != IFT_CARP || !carp_match)
680 if (log_arp_wrong_iface)
681 log(LOG_ERR, "arp: %s is on %s but got reply from %*D on %s\n",
683 rt->rt_ifp->if_xname,
684 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
689 sdl = SDL(rt->rt_gateway);
691 bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
693 if (log_arp_movements)
694 log(LOG_INFO, "arp: %s moved from %*D to %*D on %s\n",
696 ifp->if_addrlen, (u_char *)LLADDR(sdl), ":",
697 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
701 if (log_arp_permanent_modify)
702 log(LOG_ERR, "arp: %*D attempts to modify "
703 "permanent entry for %s on %s\n",
704 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
705 inet_ntoa(isaddr), ifp->if_xname);
710 * sanity check for the address length.
711 * XXX this does not work for protocols with variable address
715 sdl->sdl_alen != ah->ar_hln) {
717 "arp from %*D: new addr len %d, was %d",
718 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
719 ah->ar_hln, sdl->sdl_alen);
721 if (ifp->if_addrlen != ah->ar_hln) {
723 "arp from %*D: addr len: new %d, i/f %d (ignored)",
724 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
725 ah->ar_hln, ifp->if_addrlen);
729 (void)memcpy(LLADDR(sdl), ar_sha(ah),
730 sdl->sdl_alen = ah->ar_hln);
732 * If we receive an arp from a token-ring station over
733 * a token-ring nic then try to save the source
736 if (ifp->if_type == IFT_ISO88025) {
737 struct iso88025_header *th = NULL;
738 struct iso88025_sockaddr_dl_data *trld;
740 th = (struct iso88025_header *)m->m_pkthdr.header;
741 trld = SDL_ISO88025(sdl);
742 rif_len = TR_RCF_RIFLEN(th->rcf);
743 if ((th->iso88025_shost[0] & TR_RII) &&
745 trld->trld_rcf = th->rcf;
746 trld->trld_rcf ^= htons(TR_RCF_DIR);
747 memcpy(trld->trld_route, th->rd, rif_len - 2);
748 trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
750 * Set up source routing information for
753 m->m_data -= rif_len;
755 m->m_pkthdr.len += rif_len;
757 th->iso88025_shost[0] &= ~TR_RII;
762 m->m_pkthdr.len += 8;
763 th->rcf = trld->trld_rcf;
766 rt->rt_expire = time_uptime + arpt_keep;
767 callout_reset(&la->la_timer, hz * arpt_keep, arptimer, rt);
770 la->la_preempt = arp_maxtries;
775 (*ifp->if_output)(ifp, hold, rt_key(rt), rt);
778 if (op != ARPOP_REQUEST)
780 if (itaddr.s_addr == myaddr.s_addr) {
781 /* I am the target */
782 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
783 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
785 rt = arplookup(itaddr.s_addr, 0, SIN_PROXY);
787 struct sockaddr_in sin;
792 bzero(&sin, sizeof sin);
793 sin.sin_family = AF_INET;
794 sin.sin_len = sizeof sin;
795 sin.sin_addr = itaddr;
797 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
801 * Don't send proxies for nodes on the same interface
802 * as this one came out of, or we'll get into a fight
803 * over who claims what Ether address.
805 if (rt->rt_ifp == ifp) {
809 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
810 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
814 * Also check that the node which sent the ARP packet
815 * is on the the interface we expect it to be on. This
816 * avoids ARP chaos if an interface is connected to the
819 sin.sin_addr = isaddr;
821 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
824 if (rt->rt_ifp != ifp) {
825 log(LOG_INFO, "arp_proxy: ignoring request"
826 " from %s via %s, expecting %s\n",
827 inet_ntoa(isaddr), ifp->if_xname,
828 rt->rt_ifp->if_xname);
835 printf("arp: proxying for %s\n",
840 * Return proxied ARP replies only on the interface
841 * or bridge cluster where this network resides.
842 * Otherwise we may conflict with the host we are
845 if (rt->rt_ifp != ifp &&
846 (rt->rt_ifp->if_bridge != ifp->if_bridge ||
847 ifp->if_bridge == NULL)) {
851 sdl = SDL(rt->rt_gateway);
852 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
853 (void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
858 if (itaddr.s_addr == myaddr.s_addr &&
859 IN_LINKLOCAL(ntohl(itaddr.s_addr))) {
860 /* RFC 3927 link-local IPv4; always reply by broadcast. */
861 #ifdef DEBUG_LINKLOCAL
862 printf("arp: sending reply for link-local addr %s\n",
865 m->m_flags |= M_BCAST;
866 m->m_flags &= ~M_MCAST;
868 /* default behaviour; never reply by broadcast. */
869 m->m_flags &= ~(M_BCAST|M_MCAST);
871 (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
872 (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
873 ah->ar_op = htons(ARPOP_REPLY);
874 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
875 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
876 m->m_pkthdr.len = m->m_len;
877 sa.sa_family = AF_ARP;
879 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
888 * Lookup or enter a new address in arptab.
890 static struct rtentry *
891 arplookup(u_long addr, int create, int proxy)
894 struct sockaddr_inarp sin;
897 bzero(&sin, sizeof(sin));
898 sin.sin_len = sizeof(sin);
899 sin.sin_family = AF_INET;
900 sin.sin_addr.s_addr = addr;
902 sin.sin_other = SIN_PROXY;
903 rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
907 if (rt->rt_flags & RTF_GATEWAY)
908 why = "host is not on local network";
909 else if ((rt->rt_flags & RTF_LLINFO) == 0)
910 why = "could not allocate llinfo";
911 else if (rt->rt_gateway->sa_family != AF_LINK)
912 why = "gateway route is not ours";
915 #define ISDYNCLONE(_rt) \
916 (((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED)
918 log(LOG_DEBUG, "arplookup %s failed: %s\n",
919 inet_ntoa(sin.sin_addr), why);
921 * If there are no references to this Layer 2 route,
922 * and it is a cloned route, and not static, and
923 * arplookup() is creating the route, then purge
924 * it from the routing table as it is probably bogus.
926 if (rt->rt_refcnt == 1 && ISDYNCLONE(rt))
938 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
940 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
941 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
942 &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
943 ifa->ifa_rtrequest = arp_rtrequest;
944 ifa->ifa_flags |= RTF_CLONING;
948 arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr)
950 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
951 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
952 &IA_SIN(ifa)->sin_addr, enaddr);
953 ifa->ifa_rtrequest = arp_rtrequest;
954 ifa->ifa_flags |= RTF_CLONING;
961 arpintrq.ifq_maxlen = 50;
962 mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF);
963 netisr_register(NETISR_ARP, arpintr, &arpintrq, NETISR_MPSAFE);
965 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);