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(req, rt, info)
149 struct rt_addrinfo *info;
151 struct sockaddr *gate;
152 struct llinfo_arp *la;
153 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
154 struct in_ifaddr *ia;
159 if (rt->rt_flags & RTF_GATEWAY)
161 gate = rt->rt_gateway;
162 la = (struct llinfo_arp *)rt->rt_llinfo;
167 * XXX: If this is a manually added route to interface
168 * such as older version of routed or gated might provide,
169 * restore cloning bit.
171 if ((rt->rt_flags & RTF_HOST) == 0 &&
172 rt_mask(rt) != NULL &&
173 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
174 rt->rt_flags |= RTF_CLONING;
175 if (rt->rt_flags & RTF_CLONING) {
177 * Case 1: This route should come from a route to iface.
179 rt_setgate(rt, rt_key(rt),
180 (struct sockaddr *)&null_sdl);
181 gate = rt->rt_gateway;
182 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
183 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
184 rt->rt_expire = time_uptime;
187 /* Announce a new entry if requested. */
188 if (rt->rt_flags & RTF_ANNOUNCE)
189 arprequest(rt->rt_ifp,
190 &SIN(rt_key(rt))->sin_addr,
191 &SIN(rt_key(rt))->sin_addr,
192 (u_char *)LLADDR(SDL(gate)));
195 if (gate->sa_family != AF_LINK ||
196 gate->sa_len < sizeof(null_sdl)) {
197 log(LOG_DEBUG, "%s: bad gateway %s%s\n", __func__,
198 inet_ntoa(SIN(rt_key(rt))->sin_addr),
199 (gate->sa_family != AF_LINK) ?
203 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
204 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
206 break; /* This happens on a route change */
208 * Case 2: This route may come from cloning, or a manual route
209 * add with a LL address.
211 R_Zalloc(la, struct llinfo_arp *, sizeof(*la));
212 rt->rt_llinfo = (caddr_t)la;
214 log(LOG_DEBUG, "%s: malloc failed\n", __func__);
219 * We are storing a route entry outside of radix tree. So,
220 * it can be found and accessed by other means than radix
221 * lookup. The routing code assumes that any rtentry detached
222 * from radix can be destroyed safely. To prevent this, we
223 * add an additional reference.
227 rt->rt_flags |= RTF_LLINFO;
228 callout_init_mtx(&la->la_timer, &rt->rt_mtx,
229 CALLOUT_RETURNUNLOCKED);
233 * This keeps the multicast addresses from showing up
234 * in `arp -a' listings as unresolved. It's not actually
235 * functional. Then the same for broadcast.
237 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) &&
238 rt->rt_ifp->if_type != IFT_ARCNET) {
239 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
241 SDL(gate)->sdl_alen = 6;
244 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
245 memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
246 rt->rt_ifp->if_addrlen);
247 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
252 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
253 if (ia->ia_ifp == rt->rt_ifp &&
254 SIN(rt_key(rt))->sin_addr.s_addr ==
255 (IA_SIN(ia))->sin_addr.s_addr)
260 * This test used to be
261 * if (loif.if_flags & IFF_UP)
262 * It allowed local traffic to be forced
263 * through the hardware by configuring the loopback down.
264 * However, it causes problems during network configuration
265 * for boards that can't receive packets they send.
266 * It is now necessary to clear "useloopback" and remove
267 * the route to force traffic out to the hardware.
270 bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
271 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
274 rt->rt_rmx.rmx_mtu = loif->if_mtu;
278 * make sure to set rt->rt_ifa to the interface
279 * address we are using, otherwise we will have trouble
280 * with source address selection.
283 if (ifa != rt->rt_ifa) {
292 if (la == NULL) /* XXX: at least CARP does this. */
294 callout_stop(&la->la_timer);
295 rt->rt_llinfo = NULL;
296 rt->rt_flags &= ~RTF_LLINFO;
299 m_freem(la->la_hold);
305 * Broadcast an ARP request. Caller specifies:
306 * - arp header source ip address
307 * - arp header target ip address
308 * - arp header source ethernet address
311 arprequest(ifp, sip, tip, enaddr)
313 struct in_addr *sip, *tip;
320 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
322 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
323 2*ifp->if_data.ifi_addrlen;
324 m->m_pkthdr.len = m->m_len;
325 MH_ALIGN(m, m->m_len);
326 ah = mtod(m, struct arphdr *);
327 bzero((caddr_t)ah, m->m_len);
329 mac_create_mbuf_linklayer(ifp, m);
331 ah->ar_pro = htons(ETHERTYPE_IP);
332 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
333 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
334 ah->ar_op = htons(ARPOP_REQUEST);
335 bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
336 bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
337 bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
338 sa.sa_family = AF_ARP;
340 m->m_flags |= M_BCAST;
341 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
347 * Resolve an IP address into an ethernet address.
349 * ifp is the interface we use
350 * dst is the next hop,
351 * rt0 is the route to the final destination (possibly useless)
353 * desten is where we want the address.
355 * On success, desten is filled in and the function returns 0;
356 * If the packet must be held pending resolution, we return EWOULDBLOCK
357 * On other errors, we return the corresponding error code.
360 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
361 struct sockaddr *dst, u_char *desten)
363 struct llinfo_arp *la = NULL;
364 struct rtentry *rt = NULL;
365 struct sockaddr_dl *sdl;
368 if (m->m_flags & M_BCAST) { /* broadcast */
369 (void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
372 if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */
373 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
378 error = rt_check(&rt, &rt0, dst);
383 la = (struct llinfo_arp *)rt->rt_llinfo;
389 * We enter this block in case if rt0 was NULL,
390 * or if rt found by rt_check() didn't have llinfo.
392 rt = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
395 "arpresolve: can't allocate route for %s\n",
396 inet_ntoa(SIN(dst)->sin_addr));
398 return (EINVAL); /* XXX */
400 la = (struct llinfo_arp *)rt->rt_llinfo;
404 "arpresolve: can't allocate llinfo for %s\n",
405 inet_ntoa(SIN(dst)->sin_addr));
407 return (EINVAL); /* XXX */
410 sdl = SDL(rt->rt_gateway);
412 * Check the address family and length is valid, the address
413 * is resolved; otherwise, try to resolve.
415 if ((rt->rt_expire == 0 || rt->rt_expire > time_uptime) &&
416 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
418 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
421 * If entry has an expiry time and it is approaching,
422 * send an ARP request.
424 if ((rt->rt_expire != 0) &&
425 (time_uptime + la->la_preempt > rt->rt_expire)) {
427 SIN(rt->rt_ifa->ifa_addr)->sin_addr;
431 arprequest(ifp, &sin, &SIN(dst)->sin_addr,
440 * If ARP is disabled or static on this interface, stop.
442 * Probably should not allocate empty llinfo struct if we are
443 * not going to be sending out an arp request.
445 if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
451 * There is an arptab entry, but no ethernet address
452 * response yet. Replace the held mbuf with this
456 m_freem(la->la_hold);
459 KASSERT(rt->rt_expire > 0, ("sending ARP request for static entry"));
462 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
463 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
464 * if we have already sent arp_maxtries ARP requests. Retransmit the
465 * ARP request, but not faster than one request per second.
467 if (la->la_asked < arp_maxtries)
468 error = EWOULDBLOCK; /* First request. */
470 error = (rt == rt0) ? EHOSTDOWN : EHOSTUNREACH;
472 if (la->la_asked == 0 || rt->rt_expire != time_uptime) {
474 SIN(rt->rt_ifa->ifa_addr)->sin_addr;
476 rt->rt_expire = time_uptime;
477 callout_reset(&la->la_timer, hz, arptimer, rt);
481 arprequest(ifp, &sin, &SIN(dst)->sin_addr,
490 * Common length and type checks are done here,
491 * then the protocol-specific routine is called.
494 arpintr(struct mbuf *m)
498 if (m->m_len < sizeof(struct arphdr) &&
499 ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
500 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
503 ar = mtod(m, struct arphdr *);
505 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER &&
506 ntohs(ar->ar_hrd) != ARPHRD_IEEE802 &&
507 ntohs(ar->ar_hrd) != ARPHRD_ARCNET &&
508 ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) {
509 log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
510 (unsigned char *)&ar->ar_hrd, "");
515 if (m->m_len < arphdr_len(ar)) {
516 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
517 log(LOG_ERR, "arp: runt packet\n");
521 ar = mtod(m, struct arphdr *);
524 switch (ntohs(ar->ar_pro)) {
536 * ARP for Internet protocols on 10 Mb/s Ethernet.
537 * Algorithm is that given in RFC 826.
538 * In addition, a sanity check is performed on the sender
539 * protocol address, to catch impersonators.
540 * We no longer handle negotiations for use of trailer protocol:
541 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
542 * along with IP replies if we wanted trailers sent to us,
543 * and also sent them in response to IP replies.
544 * This allowed either end to announce the desire to receive
546 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
547 * but formerly didn't normally send requests.
549 static int log_arp_wrong_iface = 1;
550 static int log_arp_movements = 1;
551 static int log_arp_permanent_modify = 1;
553 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
554 &log_arp_wrong_iface, 0,
555 "log arp packets arriving on the wrong interface");
556 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
557 &log_arp_movements, 0,
558 "log arp replies from MACs different than the one in the cache");
559 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
560 &log_arp_permanent_modify, 0,
561 "log arp replies from MACs different than the one in the permanent arp entry");
569 struct ifnet *ifp = m->m_pkthdr.rcvif;
570 struct llinfo_arp *la;
573 struct in_ifaddr *ia;
574 struct sockaddr_dl *sdl;
576 struct in_addr isaddr, itaddr, myaddr;
578 u_int8_t *enaddr = NULL;
589 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
590 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
591 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
595 ah = mtod(m, struct arphdr *);
596 op = ntohs(ah->ar_op);
597 (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
598 (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
601 * For a bridge, we want to check the address irrespective
602 * of the receive interface. (This will change slightly
603 * when we have clusters of interfaces).
604 * If the interface does not match, but the recieving interface
605 * is part of carp, we call carp_iamatch to see if this is a
606 * request for the virtual host ip.
607 * XXX: This is really ugly!
609 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
610 if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
611 (ia->ia_ifp == ifp)) &&
612 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
615 if (ifp->if_carp != NULL &&
616 carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) &&
617 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
623 LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
624 if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
625 (ia->ia_ifp == ifp)) &&
626 isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
629 * No match, use the first inet address on the receive interface
630 * as a dummy address for the rest of the function.
632 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
633 if (ifa->ifa_addr->sa_family == AF_INET) {
638 * If bridging, fall back to using any inet address.
640 if (!bridged || (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL)
644 enaddr = (u_int8_t *)IF_LLADDR(ifp);
645 myaddr = ia->ia_addr.sin_addr;
646 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
647 goto drop; /* it's from me, ignore it. */
648 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
650 "arp: link address is broadcast for IP address %s!\n",
655 * Warn if another host is using the same IP address, but only if the
656 * IP address isn't 0.0.0.0, which is used for DHCP only, in which
657 * case we suppress the warning to avoid false positive complaints of
658 * potential misconfiguration.
660 if (!bridged && isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
662 "arp: %*D is using my IP address %s!\n",
663 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
668 if (ifp->if_flags & IFF_STATICARP)
670 rt = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
672 la = (struct llinfo_arp *)rt->rt_llinfo;
680 /* The following is not an error when doing bridging. */
681 if (!bridged && rt->rt_ifp != ifp
683 && (ifp->if_type != IFT_CARP || !carp_match)
686 if (log_arp_wrong_iface)
687 log(LOG_ERR, "arp: %s is on %s but got reply from %*D on %s\n",
689 rt->rt_ifp->if_xname,
690 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
695 sdl = SDL(rt->rt_gateway);
697 bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
699 if (log_arp_movements)
700 log(LOG_INFO, "arp: %s moved from %*D to %*D on %s\n",
702 ifp->if_addrlen, (u_char *)LLADDR(sdl), ":",
703 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
707 if (log_arp_permanent_modify)
708 log(LOG_ERR, "arp: %*D attempts to modify "
709 "permanent entry for %s on %s\n",
710 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
711 inet_ntoa(isaddr), ifp->if_xname);
716 * sanity check for the address length.
717 * XXX this does not work for protocols with variable address
721 sdl->sdl_alen != ah->ar_hln) {
723 "arp from %*D: new addr len %d, was %d",
724 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
725 ah->ar_hln, sdl->sdl_alen);
727 if (ifp->if_addrlen != ah->ar_hln) {
729 "arp from %*D: addr len: new %d, i/f %d (ignored)",
730 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
731 ah->ar_hln, ifp->if_addrlen);
735 (void)memcpy(LLADDR(sdl), ar_sha(ah),
736 sdl->sdl_alen = ah->ar_hln);
738 * If we receive an arp from a token-ring station over
739 * a token-ring nic then try to save the source
742 if (ifp->if_type == IFT_ISO88025) {
743 struct iso88025_header *th = NULL;
744 struct iso88025_sockaddr_dl_data *trld;
746 th = (struct iso88025_header *)m->m_pkthdr.header;
747 trld = SDL_ISO88025(sdl);
748 rif_len = TR_RCF_RIFLEN(th->rcf);
749 if ((th->iso88025_shost[0] & TR_RII) &&
751 trld->trld_rcf = th->rcf;
752 trld->trld_rcf ^= htons(TR_RCF_DIR);
753 memcpy(trld->trld_route, th->rd, rif_len - 2);
754 trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
756 * Set up source routing information for
759 m->m_data -= rif_len;
761 m->m_pkthdr.len += rif_len;
763 th->iso88025_shost[0] &= ~TR_RII;
768 m->m_pkthdr.len += 8;
769 th->rcf = trld->trld_rcf;
772 rt->rt_expire = time_uptime + arpt_keep;
773 callout_reset(&la->la_timer, hz * arpt_keep, arptimer, rt);
776 la->la_preempt = arp_maxtries;
781 (*ifp->if_output)(ifp, hold, rt_key(rt), rt);
784 if (op != ARPOP_REQUEST)
786 if (itaddr.s_addr == myaddr.s_addr) {
787 /* I am the target */
788 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
789 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
791 rt = arplookup(itaddr.s_addr, 0, SIN_PROXY);
793 struct sockaddr_in sin;
798 bzero(&sin, sizeof sin);
799 sin.sin_family = AF_INET;
800 sin.sin_len = sizeof sin;
801 sin.sin_addr = itaddr;
803 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
807 * Don't send proxies for nodes on the same interface
808 * as this one came out of, or we'll get into a fight
809 * over who claims what Ether address.
811 if (rt->rt_ifp == ifp) {
815 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
816 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
820 * Also check that the node which sent the ARP packet
821 * is on the the interface we expect it to be on. This
822 * avoids ARP chaos if an interface is connected to the
825 sin.sin_addr = isaddr;
827 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
830 if (rt->rt_ifp != ifp) {
831 log(LOG_INFO, "arp_proxy: ignoring request"
832 " from %s via %s, expecting %s\n",
833 inet_ntoa(isaddr), ifp->if_xname,
834 rt->rt_ifp->if_xname);
841 printf("arp: proxying for %s\n",
846 * Return proxied ARP replies only on the interface
847 * or bridge cluster where this network resides.
848 * Otherwise we may conflict with the host we are
851 if (rt->rt_ifp != ifp &&
852 (rt->rt_ifp->if_bridge != ifp->if_bridge ||
853 ifp->if_bridge == NULL)) {
857 sdl = SDL(rt->rt_gateway);
858 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
859 (void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
864 if (itaddr.s_addr == myaddr.s_addr &&
865 IN_LINKLOCAL(ntohl(itaddr.s_addr))) {
866 /* RFC 3927 link-local IPv4; always reply by broadcast. */
867 #ifdef DEBUG_LINKLOCAL
868 printf("arp: sending reply for link-local addr %s\n",
871 m->m_flags |= M_BCAST;
872 m->m_flags &= ~M_MCAST;
874 /* default behaviour; never reply by broadcast. */
875 m->m_flags &= ~(M_BCAST|M_MCAST);
877 (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
878 (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
879 ah->ar_op = htons(ARPOP_REPLY);
880 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
881 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
882 m->m_pkthdr.len = m->m_len;
883 sa.sa_family = AF_ARP;
885 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
894 * Lookup or enter a new address in arptab.
896 static struct rtentry *
897 arplookup(addr, create, proxy)
902 struct sockaddr_inarp sin;
905 bzero(&sin, sizeof(sin));
906 sin.sin_len = sizeof(sin);
907 sin.sin_family = AF_INET;
908 sin.sin_addr.s_addr = addr;
910 sin.sin_other = SIN_PROXY;
911 rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
915 if (rt->rt_flags & RTF_GATEWAY)
916 why = "host is not on local network";
917 else if ((rt->rt_flags & RTF_LLINFO) == 0)
918 why = "could not allocate llinfo";
919 else if (rt->rt_gateway->sa_family != AF_LINK)
920 why = "gateway route is not ours";
923 #define ISDYNCLONE(_rt) \
924 (((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED)
926 log(LOG_DEBUG, "arplookup %s failed: %s\n",
927 inet_ntoa(sin.sin_addr), why);
929 * If there are no references to this Layer 2 route,
930 * and it is a cloned route, and not static, and
931 * arplookup() is creating the route, then purge
932 * it from the routing table as it is probably bogus.
934 if (rt->rt_refcnt == 1 && ISDYNCLONE(rt))
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, IF_LLADDR(ifp));
953 ifa->ifa_rtrequest = arp_rtrequest;
954 ifa->ifa_flags |= RTF_CLONING;
958 arp_ifinit2(ifp, ifa, enaddr)
963 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
964 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
965 &IA_SIN(ifa)->sin_addr, enaddr);
966 ifa->ifa_rtrequest = arp_rtrequest;
967 ifa->ifa_flags |= RTF_CLONING;
974 arpintrq.ifq_maxlen = 50;
975 mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF);
976 netisr_register(NETISR_ARP, arpintr, &arpintrq, NETISR_MPSAFE);
978 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);