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_prune = (5*60*1); /* walk list every 5 minutes */
82 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
84 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
85 &arpt_prune, 0, "ARP table prune interval in seconds");
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 LIST_ENTRY(llinfo_arp) la_le;
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 LIST_HEAD(, llinfo_arp) llinfo_arp;
101 static struct ifqueue arpintrq;
102 static int arp_allocated;
104 static int arp_maxtries = 5;
105 static int useloopback = 1; /* use loopback interface for local traffic */
106 static int arp_proxyall = 0;
107 static struct callout arp_callout;
109 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
110 &arp_maxtries, 0, "ARP resolution attempts before returning error");
111 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
112 &useloopback, 0, "Use the loopback interface for local traffic");
113 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
114 &arp_proxyall, 0, "Enable proxy ARP for all suitable requests");
116 static void arp_init(void);
117 static void arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
118 static void arprequest(struct ifnet *,
119 struct in_addr *, struct in_addr *, u_char *);
120 static void arpintr(struct mbuf *);
121 static void arptimer(void *);
122 static struct rtentry
123 *arplookup(u_long, int, int);
125 static void in_arpinput(struct mbuf *);
129 * Timeout routine. Age arp_tab entries periodically.
133 arptimer(void * __unused unused)
135 struct llinfo_arp *la, *ola;
137 RADIX_NODE_HEAD_LOCK(rt_tables[AF_INET]);
138 LIST_FOREACH_SAFE(la, &llinfo_arp, la_le, ola) {
139 struct rtentry *rt = la->la_rt;
142 if (rt->rt_expire && rt->rt_expire <= time_uptime) {
143 struct sockaddr_dl *sdl = SDL(rt->rt_gateway);
145 KASSERT(sdl->sdl_family == AF_LINK, ("sdl_family %d",
147 if (rt->rt_refcnt > 1) {
149 la->la_preempt = la->la_asked = 0;
155 * XXX: LIST_REMOVE() is deep inside rtrequest().
157 rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0,
163 RADIX_NODE_HEAD_UNLOCK(rt_tables[AF_INET]);
165 callout_reset(&arp_callout, arpt_prune * hz, arptimer, NULL);
169 * Parallel to llc_rtrequest.
172 arp_rtrequest(req, rt, info)
175 struct rt_addrinfo *info;
177 struct sockaddr *gate;
178 struct llinfo_arp *la;
179 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
180 struct in_ifaddr *ia;
185 if (rt->rt_flags & RTF_GATEWAY)
187 gate = rt->rt_gateway;
188 la = (struct llinfo_arp *)rt->rt_llinfo;
193 * XXX: If this is a manually added route to interface
194 * such as older version of routed or gated might provide,
195 * restore cloning bit.
197 if ((rt->rt_flags & RTF_HOST) == 0 &&
198 rt_mask(rt) != NULL &&
199 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
200 rt->rt_flags |= RTF_CLONING;
201 if (rt->rt_flags & RTF_CLONING) {
203 * Case 1: This route should come from a route to iface.
205 rt_setgate(rt, rt_key(rt),
206 (struct sockaddr *)&null_sdl);
207 gate = rt->rt_gateway;
208 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
209 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
210 rt->rt_expire = time_uptime;
213 /* Announce a new entry if requested. */
214 if (rt->rt_flags & RTF_ANNOUNCE)
215 arprequest(rt->rt_ifp,
216 &SIN(rt_key(rt))->sin_addr,
217 &SIN(rt_key(rt))->sin_addr,
218 (u_char *)LLADDR(SDL(gate)));
221 if (gate->sa_family != AF_LINK ||
222 gate->sa_len < sizeof(null_sdl)) {
223 log(LOG_DEBUG, "%s: bad gateway %s%s\n", __func__,
224 inet_ntoa(SIN(rt_key(rt))->sin_addr),
225 (gate->sa_family != AF_LINK) ?
229 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
230 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
232 break; /* This happens on a route change */
234 * Case 2: This route may come from cloning, or a manual route
235 * add with a LL address.
237 R_Zalloc(la, struct llinfo_arp *, sizeof(*la));
238 rt->rt_llinfo = (caddr_t)la;
240 log(LOG_DEBUG, "%s: malloc failed\n", __func__);
245 * We are storing a route entry outside of radix tree. So,
246 * it can be found and accessed by other means than radix
247 * lookup. The routing code assumes that any rtentry detached
248 * from radix can be destroyed safely. To prevent this, we
249 * add an additional reference.
253 rt->rt_flags |= RTF_LLINFO;
254 RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]);
255 LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
259 * This keeps the multicast addresses from showing up
260 * in `arp -a' listings as unresolved. It's not actually
261 * functional. Then the same for broadcast.
263 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) &&
264 rt->rt_ifp->if_type != IFT_ARCNET) {
265 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
267 SDL(gate)->sdl_alen = 6;
270 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
271 memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
272 rt->rt_ifp->if_addrlen);
273 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
278 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
279 if (ia->ia_ifp == rt->rt_ifp &&
280 SIN(rt_key(rt))->sin_addr.s_addr ==
281 (IA_SIN(ia))->sin_addr.s_addr)
286 * This test used to be
287 * if (loif.if_flags & IFF_UP)
288 * It allowed local traffic to be forced
289 * through the hardware by configuring the loopback down.
290 * However, it causes problems during network configuration
291 * for boards that can't receive packets they send.
292 * It is now necessary to clear "useloopback" and remove
293 * the route to force traffic out to the hardware.
296 bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
297 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
300 rt->rt_rmx.rmx_mtu = loif->if_mtu;
304 * make sure to set rt->rt_ifa to the interface
305 * address we are using, otherwise we will have trouble
306 * with source address selection.
309 if (ifa != rt->rt_ifa) {
320 RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]);
321 LIST_REMOVE(la, la_le);
324 rt->rt_flags &= ~RTF_LLINFO;
326 m_freem(la->la_hold);
332 * Broadcast an ARP request. Caller specifies:
333 * - arp header source ip address
334 * - arp header target ip address
335 * - arp header source ethernet address
338 arprequest(ifp, sip, tip, enaddr)
340 struct in_addr *sip, *tip;
347 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
349 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
350 2*ifp->if_data.ifi_addrlen;
351 m->m_pkthdr.len = m->m_len;
352 MH_ALIGN(m, m->m_len);
353 ah = mtod(m, struct arphdr *);
354 bzero((caddr_t)ah, m->m_len);
356 mac_create_mbuf_linklayer(ifp, m);
358 ah->ar_pro = htons(ETHERTYPE_IP);
359 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
360 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
361 ah->ar_op = htons(ARPOP_REQUEST);
362 bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
363 bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
364 bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
365 sa.sa_family = AF_ARP;
367 m->m_flags |= M_BCAST;
368 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
374 * Resolve an IP address into an ethernet address.
376 * ifp is the interface we use
377 * dst is the next hop,
378 * rt0 is the route to the final destination (possibly useless)
380 * desten is where we want the address.
382 * On success, desten is filled in and the function returns 0;
383 * If the packet must be held pending resolution, we return EWOULDBLOCK
384 * On other errors, we return the corresponding error code.
387 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
388 struct sockaddr *dst, u_char *desten)
390 struct llinfo_arp *la = NULL;
391 struct rtentry *rt = NULL;
392 struct sockaddr_dl *sdl;
395 if (m->m_flags & M_BCAST) { /* broadcast */
396 (void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
399 if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */
400 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
405 error = rt_check(&rt, &rt0, dst);
410 la = (struct llinfo_arp *)rt->rt_llinfo;
416 * We enter this block in case if rt0 was NULL,
417 * or if rt found by rt_check() didn't have llinfo.
419 rt = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
422 "arpresolve: can't allocate route for %s\n",
423 inet_ntoa(SIN(dst)->sin_addr));
425 return (EINVAL); /* XXX */
427 la = (struct llinfo_arp *)rt->rt_llinfo;
431 "arpresolve: can't allocate llinfo for %s\n",
432 inet_ntoa(SIN(dst)->sin_addr));
434 return (EINVAL); /* XXX */
437 sdl = SDL(rt->rt_gateway);
439 * Check the address family and length is valid, the address
440 * is resolved; otherwise, try to resolve.
442 if ((rt->rt_expire == 0 || rt->rt_expire > time_uptime) &&
443 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
445 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
448 * If entry has an expiry time and it is approaching,
449 * send an ARP request.
451 if ((rt->rt_expire != 0) &&
452 (time_uptime + la->la_preempt > rt->rt_expire)) {
454 SIN(rt->rt_ifa->ifa_addr)->sin_addr;
458 arprequest(ifp, &sin, &SIN(dst)->sin_addr,
467 * If ARP is disabled or static on this interface, stop.
469 * Probably should not allocate empty llinfo struct if we are
470 * not going to be sending out an arp request.
472 if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
478 * There is an arptab entry, but no ethernet address
479 * response yet. Replace the held mbuf with this
483 m_freem(la->la_hold);
486 KASSERT(rt->rt_expire > 0, ("sending ARP request for static entry"));
489 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
490 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
491 * if we have already sent arp_maxtries ARP requests. Retransmit the
492 * ARP request, but not faster than one request per second.
494 if (la->la_asked < arp_maxtries)
495 error = EWOULDBLOCK; /* First request. */
497 error = (rt == rt0) ? EHOSTDOWN : EHOSTUNREACH;
499 if (la->la_asked++ == 0 || rt->rt_expire != time_uptime) {
501 SIN(rt->rt_ifa->ifa_addr)->sin_addr;
503 rt->rt_expire = time_uptime;
506 arprequest(ifp, &sin, &SIN(dst)->sin_addr,
515 * Common length and type checks are done here,
516 * then the protocol-specific routine is called.
519 arpintr(struct mbuf *m)
523 if (m->m_len < sizeof(struct arphdr) &&
524 ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
525 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
528 ar = mtod(m, struct arphdr *);
530 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER &&
531 ntohs(ar->ar_hrd) != ARPHRD_IEEE802 &&
532 ntohs(ar->ar_hrd) != ARPHRD_ARCNET &&
533 ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) {
534 log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
535 (unsigned char *)&ar->ar_hrd, "");
540 if (m->m_len < arphdr_len(ar)) {
541 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
542 log(LOG_ERR, "arp: runt packet\n");
546 ar = mtod(m, struct arphdr *);
549 switch (ntohs(ar->ar_pro)) {
561 * ARP for Internet protocols on 10 Mb/s Ethernet.
562 * Algorithm is that given in RFC 826.
563 * In addition, a sanity check is performed on the sender
564 * protocol address, to catch impersonators.
565 * We no longer handle negotiations for use of trailer protocol:
566 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
567 * along with IP replies if we wanted trailers sent to us,
568 * and also sent them in response to IP replies.
569 * This allowed either end to announce the desire to receive
571 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
572 * but formerly didn't normally send requests.
574 static int log_arp_wrong_iface = 1;
575 static int log_arp_movements = 1;
576 static int log_arp_permanent_modify = 1;
578 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
579 &log_arp_wrong_iface, 0,
580 "log arp packets arriving on the wrong interface");
581 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
582 &log_arp_movements, 0,
583 "log arp replies from MACs different than the one in the cache");
584 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
585 &log_arp_permanent_modify, 0,
586 "log arp replies from MACs different than the one in the permanent arp entry");
594 struct ifnet *ifp = m->m_pkthdr.rcvif;
595 struct llinfo_arp *la;
598 struct in_ifaddr *ia;
599 struct sockaddr_dl *sdl;
601 struct in_addr isaddr, itaddr, myaddr;
603 u_int8_t *enaddr = NULL;
614 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
615 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
616 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
620 ah = mtod(m, struct arphdr *);
621 op = ntohs(ah->ar_op);
622 (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
623 (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
626 * For a bridge, we want to check the address irrespective
627 * of the receive interface. (This will change slightly
628 * when we have clusters of interfaces).
629 * If the interface does not match, but the recieving interface
630 * is part of carp, we call carp_iamatch to see if this is a
631 * request for the virtual host ip.
632 * XXX: This is really ugly!
634 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
635 if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
636 (ia->ia_ifp == ifp)) &&
637 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
640 if (ifp->if_carp != NULL &&
641 carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) &&
642 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
648 LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
649 if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
650 (ia->ia_ifp == ifp)) &&
651 isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
654 * No match, use the first inet address on the receive interface
655 * as a dummy address for the rest of the function.
657 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
658 if (ifa->ifa_addr->sa_family == AF_INET) {
663 * If bridging, fall back to using any inet address.
665 if (!bridged || (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL)
669 enaddr = (u_int8_t *)IF_LLADDR(ifp);
670 myaddr = ia->ia_addr.sin_addr;
671 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
672 goto drop; /* it's from me, ignore it. */
673 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
675 "arp: link address is broadcast for IP address %s!\n",
680 * Warn if another host is using the same IP address, but only if the
681 * IP address isn't 0.0.0.0, which is used for DHCP only, in which
682 * case we suppress the warning to avoid false positive complaints of
683 * potential misconfiguration.
685 if (!bridged && isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
687 "arp: %*D is using my IP address %s!\n",
688 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
693 if (ifp->if_flags & IFF_STATICARP)
695 rt = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
697 la = (struct llinfo_arp *)rt->rt_llinfo;
705 /* The following is not an error when doing bridging. */
706 if (!bridged && rt->rt_ifp != ifp
708 && (ifp->if_type != IFT_CARP || !carp_match)
711 if (log_arp_wrong_iface)
712 log(LOG_ERR, "arp: %s is on %s but got reply from %*D on %s\n",
714 rt->rt_ifp->if_xname,
715 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
720 sdl = SDL(rt->rt_gateway);
722 bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
724 if (log_arp_movements)
725 log(LOG_INFO, "arp: %s moved from %*D to %*D on %s\n",
727 ifp->if_addrlen, (u_char *)LLADDR(sdl), ":",
728 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
732 if (log_arp_permanent_modify)
733 log(LOG_ERR, "arp: %*D attempts to modify "
734 "permanent entry for %s on %s\n",
735 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
736 inet_ntoa(isaddr), ifp->if_xname);
741 * sanity check for the address length.
742 * XXX this does not work for protocols with variable address
746 sdl->sdl_alen != ah->ar_hln) {
748 "arp from %*D: new addr len %d, was %d",
749 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
750 ah->ar_hln, sdl->sdl_alen);
752 if (ifp->if_addrlen != ah->ar_hln) {
754 "arp from %*D: addr len: new %d, i/f %d (ignored)",
755 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
756 ah->ar_hln, ifp->if_addrlen);
760 (void)memcpy(LLADDR(sdl), ar_sha(ah),
761 sdl->sdl_alen = ah->ar_hln);
763 * If we receive an arp from a token-ring station over
764 * a token-ring nic then try to save the source
767 if (ifp->if_type == IFT_ISO88025) {
768 struct iso88025_header *th = NULL;
769 struct iso88025_sockaddr_dl_data *trld;
771 th = (struct iso88025_header *)m->m_pkthdr.header;
772 trld = SDL_ISO88025(sdl);
773 rif_len = TR_RCF_RIFLEN(th->rcf);
774 if ((th->iso88025_shost[0] & TR_RII) &&
776 trld->trld_rcf = th->rcf;
777 trld->trld_rcf ^= htons(TR_RCF_DIR);
778 memcpy(trld->trld_route, th->rd, rif_len - 2);
779 trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
781 * Set up source routing information for
784 m->m_data -= rif_len;
786 m->m_pkthdr.len += rif_len;
788 th->iso88025_shost[0] &= ~TR_RII;
793 m->m_pkthdr.len += 8;
794 th->rcf = trld->trld_rcf;
797 rt->rt_expire = time_uptime + arpt_keep;
799 la->la_preempt = arp_maxtries;
804 (*ifp->if_output)(ifp, hold, rt_key(rt), rt);
807 if (op != ARPOP_REQUEST)
809 if (itaddr.s_addr == myaddr.s_addr) {
810 /* I am the target */
811 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
812 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
814 rt = arplookup(itaddr.s_addr, 0, SIN_PROXY);
816 struct sockaddr_in sin;
821 bzero(&sin, sizeof sin);
822 sin.sin_family = AF_INET;
823 sin.sin_len = sizeof sin;
824 sin.sin_addr = itaddr;
826 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
830 * Don't send proxies for nodes on the same interface
831 * as this one came out of, or we'll get into a fight
832 * over who claims what Ether address.
834 if (rt->rt_ifp == ifp) {
838 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
839 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
843 * Also check that the node which sent the ARP packet
844 * is on the the interface we expect it to be on. This
845 * avoids ARP chaos if an interface is connected to the
848 sin.sin_addr = isaddr;
850 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
853 if (rt->rt_ifp != ifp) {
854 log(LOG_INFO, "arp_proxy: ignoring request"
855 " from %s via %s, expecting %s\n",
856 inet_ntoa(isaddr), ifp->if_xname,
857 rt->rt_ifp->if_xname);
864 printf("arp: proxying for %s\n",
869 * Return proxied ARP replies only on the interface
870 * or bridge cluster where this network resides.
871 * Otherwise we may conflict with the host we are
874 if (rt->rt_ifp != ifp &&
875 (rt->rt_ifp->if_bridge != ifp->if_bridge ||
876 ifp->if_bridge == NULL)) {
880 sdl = SDL(rt->rt_gateway);
881 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
882 (void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
887 (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
888 (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
889 ah->ar_op = htons(ARPOP_REPLY);
890 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
891 m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
892 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
893 m->m_pkthdr.len = m->m_len;
894 sa.sa_family = AF_ARP;
896 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
905 * Lookup or enter a new address in arptab.
907 static struct rtentry *
908 arplookup(addr, create, proxy)
913 struct sockaddr_inarp sin;
916 bzero(&sin, sizeof(sin));
917 sin.sin_len = sizeof(sin);
918 sin.sin_family = AF_INET;
919 sin.sin_addr.s_addr = addr;
921 sin.sin_other = SIN_PROXY;
922 rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
926 if (rt->rt_flags & RTF_GATEWAY)
927 why = "host is not on local network";
928 else if ((rt->rt_flags & RTF_LLINFO) == 0)
929 why = "could not allocate llinfo";
930 else if (rt->rt_gateway->sa_family != AF_LINK)
931 why = "gateway route is not ours";
934 #define ISDYNCLONE(_rt) \
935 (((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED)
937 log(LOG_DEBUG, "arplookup %s failed: %s\n",
938 inet_ntoa(sin.sin_addr), why);
940 * If there are no references to this Layer 2 route,
941 * and it is a cloned route, and not static, and
942 * arplookup() is creating the route, then purge
943 * it from the routing table as it is probably bogus.
945 if (rt->rt_refcnt == 1 && ISDYNCLONE(rt))
961 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
962 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
963 &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
964 ifa->ifa_rtrequest = arp_rtrequest;
965 ifa->ifa_flags |= RTF_CLONING;
969 arp_ifinit2(ifp, ifa, enaddr)
974 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
975 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
976 &IA_SIN(ifa)->sin_addr, enaddr);
977 ifa->ifa_rtrequest = arp_rtrequest;
978 ifa->ifa_flags |= RTF_CLONING;
985 arpintrq.ifq_maxlen = 50;
986 mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF);
987 LIST_INIT(&llinfo_arp);
988 callout_init(&arp_callout, CALLOUT_MPSAFE);
989 netisr_register(NETISR_ARP, arpintr, &arpintrq, NETISR_MPSAFE);
990 callout_reset(&arp_callout, hz, arptimer, NULL);
992 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);