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>
50 #include <sys/malloc.h>
51 #include <sys/socket.h>
52 #include <sys/syslog.h>
55 #include <net/if_dl.h>
56 #include <net/if_types.h>
57 #include <net/route.h>
58 #include <net/netisr.h>
59 #include <net/if_llc.h>
60 #include <net/ethernet.h>
62 #include <netinet/in.h>
63 #include <netinet/in_var.h>
64 #include <netinet/if_ether.h>
66 #include <net/if_arc.h>
67 #include <net/iso88025.h>
70 #include <netinet/ip_carp.h>
73 #define SIN(s) ((struct sockaddr_in *)s)
74 #define SDL(s) ((struct sockaddr_dl *)s)
76 SYSCTL_DECL(_net_link_ether);
77 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
80 static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
81 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
83 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
84 &arpt_prune, 0, "ARP table prune interval in seconds");
85 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
86 &arpt_keep, 0, "ARP entry lifetime in seconds");
88 #define rt_expire rt_rmx.rmx_expire
91 LIST_ENTRY(llinfo_arp) la_le;
92 struct rtentry *la_rt;
93 struct mbuf *la_hold; /* last packet until resolved/timeout */
94 u_short la_preempt; /* countdown for pre-expiry arps */
95 u_short la_asked; /* # requests sent */
98 static LIST_HEAD(, llinfo_arp) llinfo_arp;
100 static struct ifqueue arpintrq;
101 static int arp_allocated;
103 static int arp_maxtries = 5;
104 static int useloopback = 1; /* use loopback interface for local traffic */
105 static int arp_proxyall = 0;
106 static struct callout arp_callout;
108 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
109 &arp_maxtries, 0, "ARP resolution attempts before returning error");
110 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
111 &useloopback, 0, "Use the loopback interface for local traffic");
112 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
113 &arp_proxyall, 0, "Enable proxy ARP for all suitable requests");
115 static void arp_init(void);
116 static void arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
117 static void arprequest(struct ifnet *,
118 struct in_addr *, struct in_addr *, u_char *);
119 static void arpintr(struct mbuf *);
120 static void arptimer(void *);
121 static struct rtentry
122 *arplookup(u_long, int, int);
124 static void in_arpinput(struct mbuf *);
128 * Timeout routine. Age arp_tab entries periodically.
132 arptimer(void * __unused unused)
134 struct llinfo_arp *la, *ola;
136 RADIX_NODE_HEAD_LOCK(rt_tables[AF_INET]);
137 LIST_FOREACH_SAFE(la, &llinfo_arp, la_le, ola) {
138 struct rtentry *rt = la->la_rt;
141 if (rt->rt_expire && rt->rt_expire <= time_uptime) {
142 struct sockaddr_dl *sdl = SDL(rt->rt_gateway);
144 KASSERT(sdl->sdl_family == AF_LINK, ("sdl_family %d",
146 if (rt->rt_refcnt > 1) {
148 la->la_preempt = la->la_asked = 0;
154 * XXX: LIST_REMOVE() is deep inside rtrequest().
156 rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0,
162 RADIX_NODE_HEAD_UNLOCK(rt_tables[AF_INET]);
164 callout_reset(&arp_callout, arpt_prune * hz, arptimer, NULL);
168 * Parallel to llc_rtrequest.
171 arp_rtrequest(req, rt, info)
174 struct rt_addrinfo *info;
176 struct sockaddr *gate;
177 struct llinfo_arp *la;
178 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
179 struct in_ifaddr *ia;
184 if (rt->rt_flags & RTF_GATEWAY)
186 gate = rt->rt_gateway;
187 la = (struct llinfo_arp *)rt->rt_llinfo;
192 * XXX: If this is a manually added route to interface
193 * such as older version of routed or gated might provide,
194 * restore cloning bit.
196 if ((rt->rt_flags & RTF_HOST) == 0 &&
197 rt_mask(rt) != NULL &&
198 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
199 rt->rt_flags |= RTF_CLONING;
200 if (rt->rt_flags & RTF_CLONING) {
202 * Case 1: This route should come from a route to iface.
204 rt_setgate(rt, rt_key(rt),
205 (struct sockaddr *)&null_sdl);
206 gate = rt->rt_gateway;
207 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
208 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
209 rt->rt_expire = time_uptime;
212 /* Announce a new entry if requested. */
213 if (rt->rt_flags & RTF_ANNOUNCE)
214 arprequest(rt->rt_ifp,
215 &SIN(rt_key(rt))->sin_addr,
216 &SIN(rt_key(rt))->sin_addr,
217 (u_char *)LLADDR(SDL(gate)));
220 if (gate->sa_family != AF_LINK ||
221 gate->sa_len < sizeof(null_sdl)) {
222 log(LOG_DEBUG, "%s: bad gateway %s%s\n", __func__,
223 inet_ntoa(SIN(rt_key(rt))->sin_addr),
224 (gate->sa_family != AF_LINK) ?
228 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
229 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
231 break; /* This happens on a route change */
233 * Case 2: This route may come from cloning, or a manual route
234 * add with a LL address.
236 R_Zalloc(la, struct llinfo_arp *, sizeof(*la));
237 rt->rt_llinfo = (caddr_t)la;
239 log(LOG_DEBUG, "%s: malloc failed\n", __func__);
244 * We are storing a route entry outside of radix tree. So,
245 * it can be found and accessed by other means than radix
246 * lookup. The routing code assumes that any rtentry detached
247 * from radix can be destroyed safely. To prevent this, we
248 * add an additional reference.
252 rt->rt_flags |= RTF_LLINFO;
253 RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]);
254 LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
258 * This keeps the multicast addresses from showing up
259 * in `arp -a' listings as unresolved. It's not actually
260 * functional. Then the same for broadcast.
262 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) &&
263 rt->rt_ifp->if_type != IFT_ARCNET) {
264 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
266 SDL(gate)->sdl_alen = 6;
269 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
270 memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
271 rt->rt_ifp->if_addrlen);
272 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
277 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
278 if (ia->ia_ifp == rt->rt_ifp &&
279 SIN(rt_key(rt))->sin_addr.s_addr ==
280 (IA_SIN(ia))->sin_addr.s_addr)
285 * This test used to be
286 * if (loif.if_flags & IFF_UP)
287 * It allowed local traffic to be forced
288 * through the hardware by configuring the loopback down.
289 * However, it causes problems during network configuration
290 * for boards that can't receive packets they send.
291 * It is now necessary to clear "useloopback" and remove
292 * the route to force traffic out to the hardware.
295 bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
296 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
301 * make sure to set rt->rt_ifa to the interface
302 * address we are using, otherwise we will have trouble
303 * with source address selection.
306 if (ifa != rt->rt_ifa) {
317 RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]);
318 LIST_REMOVE(la, la_le);
321 rt->rt_flags &= ~RTF_LLINFO;
323 m_freem(la->la_hold);
329 * Broadcast an ARP request. Caller specifies:
330 * - arp header source ip address
331 * - arp header target ip address
332 * - arp header source ethernet address
335 arprequest(ifp, sip, tip, enaddr)
337 struct in_addr *sip, *tip;
344 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
346 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
347 2*ifp->if_data.ifi_addrlen;
348 m->m_pkthdr.len = m->m_len;
349 MH_ALIGN(m, m->m_len);
350 ah = mtod(m, struct arphdr *);
351 bzero((caddr_t)ah, m->m_len);
353 mac_create_mbuf_linklayer(ifp, m);
355 ah->ar_pro = htons(ETHERTYPE_IP);
356 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
357 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
358 ah->ar_op = htons(ARPOP_REQUEST);
359 bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
360 bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
361 bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
362 sa.sa_family = AF_ARP;
364 m->m_flags |= M_BCAST;
365 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
371 * Resolve an IP address into an ethernet address.
373 * ifp is the interface we use
374 * dst is the next hop,
375 * rt0 is the route to the final destination (possibly useless)
377 * desten is where we want the address.
379 * On success, desten is filled in and the function returns 0;
380 * If the packet must be held pending resolution, we return EWOULDBLOCK
381 * On other errors, we return the corresponding error code.
384 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
385 struct sockaddr *dst, u_char *desten)
387 struct llinfo_arp *la = NULL;
388 struct rtentry *rt = NULL;
389 struct sockaddr_dl *sdl;
392 if (m->m_flags & M_BCAST) { /* broadcast */
393 (void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
396 if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */
397 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
402 error = rt_check(&rt, &rt0, dst);
407 la = (struct llinfo_arp *)rt->rt_llinfo;
413 * We enter this block in case if rt0 was NULL,
414 * or if rt found by rt_check() didn't have llinfo.
416 rt = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
419 "arpresolve: can't allocate route for %s\n",
420 inet_ntoa(SIN(dst)->sin_addr));
422 return (EINVAL); /* XXX */
424 la = (struct llinfo_arp *)rt->rt_llinfo;
428 "arpresolve: can't allocate llinfo for %s\n",
429 inet_ntoa(SIN(dst)->sin_addr));
431 return (EINVAL); /* XXX */
434 sdl = SDL(rt->rt_gateway);
436 * Check the address family and length is valid, the address
437 * is resolved; otherwise, try to resolve.
439 if ((rt->rt_expire == 0 || rt->rt_expire > time_uptime) &&
440 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
442 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
445 * If entry has an expiry time and it is approaching,
446 * send an ARP request.
448 if ((rt->rt_expire != 0) &&
449 (time_uptime + la->la_preempt > rt->rt_expire)) {
451 SIN(rt->rt_ifa->ifa_addr)->sin_addr;
455 arprequest(ifp, &sin, &SIN(dst)->sin_addr,
464 * If ARP is disabled or static on this interface, stop.
466 * Probably should not allocate empty llinfo struct if we are
467 * not going to be sending out an arp request.
469 if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
475 * There is an arptab entry, but no ethernet address
476 * response yet. Replace the held mbuf with this
480 m_freem(la->la_hold);
483 KASSERT(rt->rt_expire > 0, ("sending ARP request for static entry"));
486 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
487 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
488 * if we have already sent arp_maxtries ARP requests. Retransmit the
489 * ARP request, but not faster than one request per second.
491 if (la->la_asked < arp_maxtries)
492 error = EWOULDBLOCK; /* First request. */
494 error = (rt == rt0) ? EHOSTDOWN : EHOSTUNREACH;
496 if (la->la_asked++ == 0 || rt->rt_expire != time_uptime) {
498 SIN(rt->rt_ifa->ifa_addr)->sin_addr;
500 rt->rt_expire = time_uptime;
503 arprequest(ifp, &sin, &SIN(dst)->sin_addr,
512 * Common length and type checks are done here,
513 * then the protocol-specific routine is called.
516 arpintr(struct mbuf *m)
520 if (m->m_len < sizeof(struct arphdr) &&
521 ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
522 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
525 ar = mtod(m, struct arphdr *);
527 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER &&
528 ntohs(ar->ar_hrd) != ARPHRD_IEEE802 &&
529 ntohs(ar->ar_hrd) != ARPHRD_ARCNET &&
530 ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) {
531 log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
532 (unsigned char *)&ar->ar_hrd, "");
537 if (m->m_len < arphdr_len(ar)) {
538 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
539 log(LOG_ERR, "arp: runt packet\n");
543 ar = mtod(m, struct arphdr *);
546 switch (ntohs(ar->ar_pro)) {
558 * ARP for Internet protocols on 10 Mb/s Ethernet.
559 * Algorithm is that given in RFC 826.
560 * In addition, a sanity check is performed on the sender
561 * protocol address, to catch impersonators.
562 * We no longer handle negotiations for use of trailer protocol:
563 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
564 * along with IP replies if we wanted trailers sent to us,
565 * and also sent them in response to IP replies.
566 * This allowed either end to announce the desire to receive
568 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
569 * but formerly didn't normally send requests.
571 static int log_arp_wrong_iface = 1;
572 static int log_arp_movements = 1;
573 static int log_arp_permanent_modify = 1;
575 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
576 &log_arp_wrong_iface, 0,
577 "log arp packets arriving on the wrong interface");
578 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
579 &log_arp_movements, 0,
580 "log arp replies from MACs different than the one in the cache");
581 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
582 &log_arp_permanent_modify, 0,
583 "log arp replies from MACs different than the one in the permanent arp entry");
591 struct ifnet *ifp = m->m_pkthdr.rcvif;
592 struct iso88025_header *th = (struct iso88025_header *)0;
593 struct iso88025_sockaddr_dl_data *trld;
594 struct llinfo_arp *la;
597 struct in_ifaddr *ia;
598 struct sockaddr_dl *sdl;
600 struct in_addr isaddr, itaddr, myaddr;
602 u_int8_t *enaddr = NULL;
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 * No match, use the first inet address on the receive interface
654 * as a dummy address for the rest of the function.
656 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
657 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
662 * If bridging, fall back to using any inet address.
664 if (!bridged || (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL)
668 enaddr = (u_int8_t *)IF_LLADDR(ifp);
669 myaddr = ia->ia_addr.sin_addr;
670 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
671 goto drop; /* it's from me, ignore it. */
672 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
674 "arp: link address is broadcast for IP address %s!\n",
679 * Warn if another host is using the same IP address, but only if the
680 * IP address isn't 0.0.0.0, which is used for DHCP only, in which
681 * case we suppress the warning to avoid false positive complaints of
682 * potential misconfiguration.
684 if (!bridged && isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
686 "arp: %*D is using my IP address %s!\n",
687 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
692 if (ifp->if_flags & IFF_STATICARP)
694 rt = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
696 la = (struct llinfo_arp *)rt->rt_llinfo;
704 /* The following is not an error when doing bridging. */
705 if (!bridged && rt->rt_ifp != ifp
707 && (ifp->if_type != IFT_CARP || !carp_match)
710 if (log_arp_wrong_iface)
711 log(LOG_ERR, "arp: %s is on %s but got reply from %*D on %s\n",
713 rt->rt_ifp->if_xname,
714 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
719 sdl = SDL(rt->rt_gateway);
721 bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
723 if (log_arp_movements)
724 log(LOG_INFO, "arp: %s moved from %*D to %*D on %s\n",
726 ifp->if_addrlen, (u_char *)LLADDR(sdl), ":",
727 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
731 if (log_arp_permanent_modify)
732 log(LOG_ERR, "arp: %*D attempts to modify "
733 "permanent entry for %s on %s\n",
734 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
735 inet_ntoa(isaddr), ifp->if_xname);
740 * sanity check for the address length.
741 * XXX this does not work for protocols with variable address
745 sdl->sdl_alen != ah->ar_hln) {
747 "arp from %*D: new addr len %d, was %d",
748 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
749 ah->ar_hln, sdl->sdl_alen);
751 if (ifp->if_addrlen != ah->ar_hln) {
753 "arp from %*D: addr len: new %d, i/f %d (ignored)",
754 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
755 ah->ar_hln, ifp->if_addrlen);
759 (void)memcpy(LLADDR(sdl), ar_sha(ah),
760 sdl->sdl_alen = ah->ar_hln);
762 * If we receive an arp from a token-ring station over
763 * a token-ring nic then try to save the source
766 if (ifp->if_type == IFT_ISO88025) {
767 th = (struct iso88025_header *)m->m_pkthdr.header;
768 trld = SDL_ISO88025(sdl);
769 rif_len = TR_RCF_RIFLEN(th->rcf);
770 if ((th->iso88025_shost[0] & TR_RII) &&
772 trld->trld_rcf = th->rcf;
773 trld->trld_rcf ^= htons(TR_RCF_DIR);
774 memcpy(trld->trld_route, th->rd, rif_len - 2);
775 trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
777 * Set up source routing information for
780 m->m_data -= rif_len;
782 m->m_pkthdr.len += rif_len;
784 th->iso88025_shost[0] &= ~TR_RII;
789 m->m_pkthdr.len += 8;
790 th->rcf = trld->trld_rcf;
793 rt->rt_expire = time_uptime + arpt_keep;
795 la->la_preempt = arp_maxtries;
800 (*ifp->if_output)(ifp, hold, rt_key(rt), rt);
803 if (op != ARPOP_REQUEST)
805 if (itaddr.s_addr == myaddr.s_addr) {
806 /* I am the target */
807 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
808 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
810 rt = arplookup(itaddr.s_addr, 0, SIN_PROXY);
812 struct sockaddr_in sin;
817 bzero(&sin, sizeof sin);
818 sin.sin_family = AF_INET;
819 sin.sin_len = sizeof sin;
820 sin.sin_addr = itaddr;
822 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
826 * Don't send proxies for nodes on the same interface
827 * as this one came out of, or we'll get into a fight
828 * over who claims what Ether address.
830 if (rt->rt_ifp == ifp) {
834 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
835 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
839 * Also check that the node which sent the ARP packet
840 * is on the the interface we expect it to be on. This
841 * avoids ARP chaos if an interface is connected to the
844 sin.sin_addr = isaddr;
846 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
849 if (rt->rt_ifp != ifp) {
850 log(LOG_INFO, "arp_proxy: ignoring request"
851 " from %s via %s, expecting %s\n",
852 inet_ntoa(isaddr), ifp->if_xname,
853 rt->rt_ifp->if_xname);
860 printf("arp: proxying for %s\n",
865 * Return proxied ARP replies only on the interface
866 * where this network resides. Otherwise we may
867 * conflict with the host we are proxying for.
869 if (rt->rt_ifp != ifp) {
873 sdl = SDL(rt->rt_gateway);
874 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
875 (void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
880 (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
881 (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
882 ah->ar_op = htons(ARPOP_REPLY);
883 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
884 m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
885 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
886 m->m_pkthdr.len = m->m_len;
887 sa.sa_family = AF_ARP;
889 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
898 * Lookup or enter a new address in arptab.
900 static struct rtentry *
901 arplookup(addr, create, proxy)
906 struct sockaddr_inarp sin;
909 bzero(&sin, sizeof(sin));
910 sin.sin_len = sizeof(sin);
911 sin.sin_family = AF_INET;
912 sin.sin_addr.s_addr = addr;
914 sin.sin_other = SIN_PROXY;
915 rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
919 if (rt->rt_flags & RTF_GATEWAY)
920 why = "host is not on local network";
921 else if ((rt->rt_flags & RTF_LLINFO) == 0)
922 why = "could not allocate llinfo";
923 else if (rt->rt_gateway->sa_family != AF_LINK)
924 why = "gateway route is not ours";
927 #define ISDYNCLONE(_rt) \
928 (((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED)
930 log(LOG_DEBUG, "arplookup %s failed: %s\n",
931 inet_ntoa(sin.sin_addr), why);
933 * If there are no references to this Layer 2 route,
934 * and it is a cloned route, and not static, and
935 * arplookup() is creating the route, then purge
936 * it from the routing table as it is probably bogus.
938 if (rt->rt_refcnt == 1 && ISDYNCLONE(rt))
954 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
955 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
956 &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
957 ifa->ifa_rtrequest = arp_rtrequest;
958 ifa->ifa_flags |= RTF_CLONING;
962 arp_ifinit2(ifp, ifa, enaddr)
967 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
968 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
969 &IA_SIN(ifa)->sin_addr, enaddr);
970 ifa->ifa_rtrequest = arp_rtrequest;
971 ifa->ifa_flags |= RTF_CLONING;
978 arpintrq.ifq_maxlen = 50;
979 mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF);
980 LIST_INIT(&llinfo_arp);
981 callout_init(&arp_callout, CALLOUT_MPSAFE);
982 netisr_register(NETISR_ARP, arpintr, &arpintrq, NETISR_MPSAFE);
983 callout_reset(&arp_callout, hz, arptimer, NULL);
985 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);