2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)if_ether.c 8.1 (Berkeley) 6/10/93
33 * Ethernet address resolution protocol.
35 * add "inuse/lock" bit (or ref. count) along with valid bit
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
46 #include <sys/kernel.h>
47 #include <sys/queue.h>
48 #include <sys/sysctl.h>
49 #include <sys/systm.h>
51 #include <sys/malloc.h>
52 #include <sys/socket.h>
53 #include <sys/syslog.h>
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
58 #include <net/route.h>
59 #include <net/netisr.h>
60 #include <net/if_llc.h>
61 #include <net/ethernet.h>
63 #include <netinet/in.h>
64 #include <netinet/in_var.h>
65 #include <netinet/if_ether.h>
67 #include <net/if_arc.h>
68 #include <net/iso88025.h>
71 #include <netinet/ip_carp.h>
74 #include <security/mac/mac_framework.h>
76 #define SIN(s) ((struct sockaddr_in *)s)
77 #define SDL(s) ((struct sockaddr_dl *)s)
79 SYSCTL_DECL(_net_link_ether);
80 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
83 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
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 struct callout la_timer;
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 struct ifqueue arpintrq;
99 static int arp_allocated;
101 static int arp_maxtries = 5;
102 static int useloopback = 1; /* use loopback interface for local traffic */
103 static int arp_proxyall = 0;
105 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
106 &arp_maxtries, 0, "ARP resolution attempts before returning error");
107 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
108 &useloopback, 0, "Use the loopback interface for local traffic");
109 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
110 &arp_proxyall, 0, "Enable proxy ARP for all suitable requests");
112 static void arp_init(void);
113 static void arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
114 static void arprequest(struct ifnet *,
115 struct in_addr *, struct in_addr *, u_char *);
116 static void arpintr(struct mbuf *);
117 static void arptimer(void *);
118 static struct rtentry
119 *arplookup(u_long, int, int);
121 static void in_arpinput(struct mbuf *);
130 struct rtentry *rt = (struct rtentry *)arg;
134 * The lock is needed to close a theoretical race
135 * between spontaneous expiry and intentional removal.
136 * We still got an extra reference on rtentry, so can
137 * safely pass pointers to its contents.
141 rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
145 * Parallel to llc_rtrequest.
148 arp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
150 struct sockaddr *gate;
151 struct llinfo_arp *la;
152 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
153 struct in_ifaddr *ia;
158 if (rt->rt_flags & RTF_GATEWAY)
160 gate = rt->rt_gateway;
161 la = (struct llinfo_arp *)rt->rt_llinfo;
166 * XXX: If this is a manually added route to interface
167 * such as older version of routed or gated might provide,
168 * restore cloning bit.
170 if ((rt->rt_flags & RTF_HOST) == 0 &&
171 rt_mask(rt) != NULL &&
172 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
173 rt->rt_flags |= RTF_CLONING;
174 if (rt->rt_flags & RTF_CLONING) {
176 * Case 1: This route should come from a route to iface.
178 rt_setgate(rt, rt_key(rt),
179 (struct sockaddr *)&null_sdl);
180 gate = rt->rt_gateway;
181 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
182 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
183 rt->rt_expire = time_uptime;
186 /* Announce a new entry if requested. */
187 if (rt->rt_flags & RTF_ANNOUNCE)
188 arprequest(rt->rt_ifp,
189 &SIN(rt_key(rt))->sin_addr,
190 &SIN(rt_key(rt))->sin_addr,
191 (u_char *)LLADDR(SDL(gate)));
194 if (gate->sa_family != AF_LINK ||
195 gate->sa_len < sizeof(null_sdl)) {
196 log(LOG_DEBUG, "%s: bad gateway %s%s\n", __func__,
197 inet_ntoa(SIN(rt_key(rt))->sin_addr),
198 (gate->sa_family != AF_LINK) ?
202 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
203 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
205 break; /* This happens on a route change */
207 * Case 2: This route may come from cloning, or a manual route
208 * add with a LL address.
210 R_Zalloc(la, struct llinfo_arp *, sizeof(*la));
211 rt->rt_llinfo = (caddr_t)la;
213 log(LOG_DEBUG, "%s: malloc failed\n", __func__);
218 * We are storing a route entry outside of radix tree. So,
219 * it can be found and accessed by other means than radix
220 * lookup. The routing code assumes that any rtentry detached
221 * from radix can be destroyed safely. To prevent this, we
222 * add an additional reference.
226 rt->rt_flags |= RTF_LLINFO;
227 callout_init_mtx(&la->la_timer, &rt->rt_mtx,
228 CALLOUT_RETURNUNLOCKED);
232 * This keeps the multicast addresses from showing up
233 * in `arp -a' listings as unresolved. It's not actually
234 * functional. Then the same for broadcast.
236 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) &&
237 rt->rt_ifp->if_type != IFT_ARCNET) {
238 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
240 SDL(gate)->sdl_alen = 6;
243 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
244 memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
245 rt->rt_ifp->if_addrlen);
246 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
251 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
252 if (ia->ia_ifp == rt->rt_ifp &&
253 SIN(rt_key(rt))->sin_addr.s_addr ==
254 (IA_SIN(ia))->sin_addr.s_addr)
259 * This test used to be
260 * if (loif.if_flags & IFF_UP)
261 * It allowed local traffic to be forced
262 * through the hardware by configuring the loopback down.
263 * However, it causes problems during network configuration
264 * for boards that can't receive packets they send.
265 * It is now necessary to clear "useloopback" and remove
266 * the route to force traffic out to the hardware.
269 bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
270 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
273 rt->rt_rmx.rmx_mtu = loif->if_mtu;
277 * make sure to set rt->rt_ifa to the interface
278 * address we are using, otherwise we will have trouble
279 * with source address selection.
282 if (ifa != rt->rt_ifa) {
291 if (la == NULL) /* XXX: at least CARP does this. */
293 callout_stop(&la->la_timer);
294 rt->rt_llinfo = NULL;
295 rt->rt_flags &= ~RTF_LLINFO;
298 m_freem(la->la_hold);
304 * Broadcast an ARP request. Caller specifies:
305 * - arp header source ip address
306 * - arp header target ip address
307 * - arp header source ethernet address
310 arprequest(struct ifnet *ifp, struct in_addr *sip, struct in_addr *tip,
317 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
319 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
320 2*ifp->if_data.ifi_addrlen;
321 m->m_pkthdr.len = m->m_len;
322 MH_ALIGN(m, m->m_len);
323 ah = mtod(m, struct arphdr *);
324 bzero((caddr_t)ah, m->m_len);
326 mac_netinet_arp_send(ifp, m);
328 ah->ar_pro = htons(ETHERTYPE_IP);
329 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
330 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
331 ah->ar_op = htons(ARPOP_REQUEST);
332 bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
333 bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
334 bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
335 sa.sa_family = AF_ARP;
337 m->m_flags |= M_BCAST;
338 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
344 * Resolve an IP address into an ethernet address.
346 * ifp is the interface we use
347 * rt0 is the route to the final destination (possibly useless)
348 * m is the mbuf. May be NULL if we don't have a packet.
349 * dst is the next hop,
350 * desten is where we want the address.
352 * On success, desten is filled in and the function returns 0;
353 * If the packet must be held pending resolution, we return EWOULDBLOCK
354 * On other errors, we return the corresponding error code.
355 * Note that m_freem() handles NULL.
358 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
359 struct sockaddr *dst, u_char *desten)
361 struct llinfo_arp *la = NULL;
362 struct rtentry *rt = NULL;
363 struct sockaddr_dl *sdl;
367 if (m->m_flags & M_BCAST) {
370 ifp->if_broadcastaddr, ifp->if_addrlen);
373 if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {
375 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
381 error = rt_check(&rt, &rt0, dst);
386 la = (struct llinfo_arp *)rt->rt_llinfo;
392 * We enter this block in case if rt0 was NULL,
393 * or if rt found by rt_check() didn't have llinfo.
395 rt = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
398 "arpresolve: can't allocate route for %s\n",
399 inet_ntoa(SIN(dst)->sin_addr));
401 return (EINVAL); /* XXX */
403 la = (struct llinfo_arp *)rt->rt_llinfo;
407 "arpresolve: can't allocate llinfo for %s\n",
408 inet_ntoa(SIN(dst)->sin_addr));
410 return (EINVAL); /* XXX */
413 sdl = SDL(rt->rt_gateway);
415 * Check the address family and length is valid, the address
416 * is resolved; otherwise, try to resolve.
418 if ((rt->rt_expire == 0 || rt->rt_expire > time_uptime) &&
419 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
421 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
424 * If entry has an expiry time and it is approaching,
425 * send an ARP request.
427 if ((rt->rt_expire != 0) &&
428 (time_uptime + la->la_preempt > rt->rt_expire)) {
430 SIN(rt->rt_ifa->ifa_addr)->sin_addr;
434 arprequest(ifp, &sin, &SIN(dst)->sin_addr,
443 * If ARP is disabled or static on this interface, stop.
445 * Probably should not allocate empty llinfo struct if we are
446 * not going to be sending out an arp request.
448 if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
454 * There is an arptab entry, but no ethernet address
455 * response yet. Replace the held mbuf with this
460 m_freem(la->la_hold);
463 KASSERT(rt->rt_expire > 0, ("sending ARP request for static entry"));
466 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
467 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
468 * if we have already sent arp_maxtries ARP requests. Retransmit the
469 * ARP request, but not faster than one request per second.
471 if (la->la_asked < arp_maxtries)
472 error = EWOULDBLOCK; /* First request. */
474 error = (rt == rt0) ? EHOSTDOWN : EHOSTUNREACH;
476 if (la->la_asked == 0 || rt->rt_expire != time_uptime) {
478 SIN(rt->rt_ifa->ifa_addr)->sin_addr;
480 rt->rt_expire = time_uptime;
481 callout_reset(&la->la_timer, hz, arptimer, rt);
485 arprequest(ifp, &sin, &SIN(dst)->sin_addr,
494 * Common length and type checks are done here,
495 * then the protocol-specific routine is called.
498 arpintr(struct mbuf *m)
502 if (m->m_len < sizeof(struct arphdr) &&
503 ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
504 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
507 ar = mtod(m, struct arphdr *);
509 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER &&
510 ntohs(ar->ar_hrd) != ARPHRD_IEEE802 &&
511 ntohs(ar->ar_hrd) != ARPHRD_ARCNET &&
512 ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) {
513 log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
514 (unsigned char *)&ar->ar_hrd, "");
519 if (m->m_len < arphdr_len(ar)) {
520 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
521 log(LOG_ERR, "arp: runt packet\n");
525 ar = mtod(m, struct arphdr *);
528 switch (ntohs(ar->ar_pro)) {
540 * ARP for Internet protocols on 10 Mb/s Ethernet.
541 * Algorithm is that given in RFC 826.
542 * In addition, a sanity check is performed on the sender
543 * protocol address, to catch impersonators.
544 * We no longer handle negotiations for use of trailer protocol:
545 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
546 * along with IP replies if we wanted trailers sent to us,
547 * and also sent them in response to IP replies.
548 * This allowed either end to announce the desire to receive
550 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
551 * but formerly didn't normally send requests.
553 static int log_arp_wrong_iface = 1;
554 static int log_arp_movements = 1;
555 static int log_arp_permanent_modify = 1;
557 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
558 &log_arp_wrong_iface, 0,
559 "log arp packets arriving on the wrong interface");
560 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
561 &log_arp_movements, 0,
562 "log arp replies from MACs different than the one in the cache");
563 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
564 &log_arp_permanent_modify, 0,
565 "log arp replies from MACs different than the one in the permanent arp entry");
569 in_arpinput(struct mbuf *m)
572 struct ifnet *ifp = m->m_pkthdr.rcvif;
573 struct llinfo_arp *la;
576 struct in_ifaddr *ia;
577 struct sockaddr_dl *sdl;
579 struct in_addr isaddr, itaddr, myaddr;
581 u_int8_t *enaddr = NULL;
588 struct sockaddr_in sin;
589 sin.sin_len = sizeof(struct sockaddr_in);
590 sin.sin_family = AF_INET;
591 sin.sin_addr.s_addr = 0;
596 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
597 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
598 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
602 ah = mtod(m, struct arphdr *);
603 op = ntohs(ah->ar_op);
604 (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
605 (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
608 * For a bridge, we want to check the address irrespective
609 * of the receive interface. (This will change slightly
610 * when we have clusters of interfaces).
611 * If the interface does not match, but the recieving interface
612 * is part of carp, we call carp_iamatch to see if this is a
613 * request for the virtual host ip.
614 * XXX: This is really ugly!
616 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
617 if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
618 (ia->ia_ifp == ifp)) &&
619 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
622 if (ifp->if_carp != NULL &&
623 carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) &&
624 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
630 LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
631 if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
632 (ia->ia_ifp == ifp)) &&
633 isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
636 * No match, use the first inet address on the receive interface
637 * as a dummy address for the rest of the function.
639 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
640 if (ifa->ifa_addr->sa_family == AF_INET) {
645 * If bridging, fall back to using any inet address.
647 if (!bridged || (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL)
651 enaddr = (u_int8_t *)IF_LLADDR(ifp);
652 myaddr = ia->ia_addr.sin_addr;
653 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
654 goto drop; /* it's from me, ignore it. */
655 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
657 "arp: link address is broadcast for IP address %s!\n",
662 * Warn if another host is using the same IP address, but only if the
663 * IP address isn't 0.0.0.0, which is used for DHCP only, in which
664 * case we suppress the warning to avoid false positive complaints of
665 * potential misconfiguration.
667 if (!bridged && isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
669 "arp: %*D is using my IP address %s on %s!\n",
670 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
671 inet_ntoa(isaddr), ifp->if_xname);
675 if (ifp->if_flags & IFF_STATICARP)
677 rt = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
679 sin.sin_addr.s_addr = isaddr.s_addr;
680 EVENTHANDLER_INVOKE(route_arp_update_event, rt,
681 ar_sha(ah), (struct sockaddr *)&sin);
683 la = (struct llinfo_arp *)rt->rt_llinfo;
691 /* The following is not an error when doing bridging. */
692 if (!bridged && rt->rt_ifp != ifp
694 && (ifp->if_type != IFT_CARP || !carp_match)
697 if (log_arp_wrong_iface)
698 log(LOG_ERR, "arp: %s is on %s but got reply from %*D on %s\n",
700 rt->rt_ifp->if_xname,
701 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
706 sdl = SDL(rt->rt_gateway);
708 bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
710 if (log_arp_movements)
711 log(LOG_INFO, "arp: %s moved from %*D to %*D on %s\n",
713 ifp->if_addrlen, (u_char *)LLADDR(sdl), ":",
714 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
718 if (log_arp_permanent_modify)
719 log(LOG_ERR, "arp: %*D attempts to modify "
720 "permanent entry for %s on %s\n",
721 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
722 inet_ntoa(isaddr), ifp->if_xname);
727 * sanity check for the address length.
728 * XXX this does not work for protocols with variable address
732 sdl->sdl_alen != ah->ar_hln) {
734 "arp from %*D: new addr len %d, was %d",
735 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
736 ah->ar_hln, sdl->sdl_alen);
738 if (ifp->if_addrlen != ah->ar_hln) {
740 "arp from %*D: addr len: new %d, i/f %d (ignored)",
741 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
742 ah->ar_hln, ifp->if_addrlen);
746 (void)memcpy(LLADDR(sdl), ar_sha(ah),
747 sdl->sdl_alen = ah->ar_hln);
749 * If we receive an arp from a token-ring station over
750 * a token-ring nic then try to save the source
753 if (ifp->if_type == IFT_ISO88025) {
754 struct iso88025_header *th = NULL;
755 struct iso88025_sockaddr_dl_data *trld;
757 th = (struct iso88025_header *)m->m_pkthdr.header;
758 trld = SDL_ISO88025(sdl);
759 rif_len = TR_RCF_RIFLEN(th->rcf);
760 if ((th->iso88025_shost[0] & TR_RII) &&
762 trld->trld_rcf = th->rcf;
763 trld->trld_rcf ^= htons(TR_RCF_DIR);
764 memcpy(trld->trld_route, th->rd, rif_len - 2);
765 trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
767 * Set up source routing information for
770 m->m_data -= rif_len;
772 m->m_pkthdr.len += rif_len;
774 th->iso88025_shost[0] &= ~TR_RII;
779 m->m_pkthdr.len += 8;
780 th->rcf = trld->trld_rcf;
783 rt->rt_expire = time_uptime + arpt_keep;
784 callout_reset(&la->la_timer, hz * arpt_keep, arptimer, rt);
787 la->la_preempt = arp_maxtries;
792 (*ifp->if_output)(ifp, hold, rt_key(rt), rt);
795 if (op != ARPOP_REQUEST)
797 if (itaddr.s_addr == myaddr.s_addr) {
798 /* I am the target */
799 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
800 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
802 rt = arplookup(itaddr.s_addr, 0, SIN_PROXY);
804 struct sockaddr_in sin;
809 bzero(&sin, sizeof sin);
810 sin.sin_family = AF_INET;
811 sin.sin_len = sizeof sin;
812 sin.sin_addr = itaddr;
814 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
818 * Don't send proxies for nodes on the same interface
819 * as this one came out of, or we'll get into a fight
820 * over who claims what Ether address.
822 if (rt->rt_ifp == ifp) {
826 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
827 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
831 * Also check that the node which sent the ARP packet
832 * is on the the interface we expect it to be on. This
833 * avoids ARP chaos if an interface is connected to the
836 sin.sin_addr = isaddr;
838 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
841 if (rt->rt_ifp != ifp) {
842 log(LOG_INFO, "arp_proxy: ignoring request"
843 " from %s via %s, expecting %s\n",
844 inet_ntoa(isaddr), ifp->if_xname,
845 rt->rt_ifp->if_xname);
852 printf("arp: proxying for %s\n",
857 * Return proxied ARP replies only on the interface
858 * or bridge cluster where this network resides.
859 * Otherwise we may conflict with the host we are
862 if (rt->rt_ifp != ifp &&
863 (rt->rt_ifp->if_bridge != ifp->if_bridge ||
864 ifp->if_bridge == NULL)) {
868 sdl = SDL(rt->rt_gateway);
869 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
870 (void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
875 if (itaddr.s_addr == myaddr.s_addr &&
876 IN_LINKLOCAL(ntohl(itaddr.s_addr))) {
877 /* RFC 3927 link-local IPv4; always reply by broadcast. */
878 #ifdef DEBUG_LINKLOCAL
879 printf("arp: sending reply for link-local addr %s\n",
882 m->m_flags |= M_BCAST;
883 m->m_flags &= ~M_MCAST;
885 /* default behaviour; never reply by broadcast. */
886 m->m_flags &= ~(M_BCAST|M_MCAST);
888 (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
889 (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
890 ah->ar_op = htons(ARPOP_REPLY);
891 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
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(u_long addr, int create, int proxy)
911 struct sockaddr_inarp sin;
914 bzero(&sin, sizeof(sin));
915 sin.sin_len = sizeof(sin);
916 sin.sin_family = AF_INET;
917 sin.sin_addr.s_addr = addr;
919 sin.sin_other = SIN_PROXY;
920 rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
924 if (rt->rt_flags & RTF_GATEWAY)
925 why = "host is not on local network";
926 else if ((rt->rt_flags & RTF_LLINFO) == 0)
927 why = "could not allocate llinfo";
928 else if (rt->rt_gateway->sa_family != AF_LINK)
929 why = "gateway route is not ours";
932 #define ISDYNCLONE(_rt) \
933 (((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED)
935 log(LOG_DEBUG, "arplookup %s failed: %s\n",
936 inet_ntoa(sin.sin_addr), why);
938 * If there are no references to this Layer 2 route,
939 * and it is a cloned route, and not static, and
940 * arplookup() is creating the route, then purge
941 * it from the routing table as it is probably bogus.
943 if (rt->rt_refcnt == 1 && ISDYNCLONE(rt))
955 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
957 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
958 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
959 &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
960 ifa->ifa_rtrequest = arp_rtrequest;
961 ifa->ifa_flags |= RTF_CLONING;
965 arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *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 netisr_register(NETISR_ARP, arpintr, &arpintrq, NETISR_MPSAFE);
982 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);