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>
54 #include <sys/vimage.h>
57 #include <net/if_dl.h>
58 #include <net/if_types.h>
59 #include <net/route.h>
60 #include <net/netisr.h>
61 #include <net/if_llc.h>
62 #include <net/ethernet.h>
64 #include <netinet/in.h>
65 #include <netinet/in_var.h>
66 #include <netinet/if_ether.h>
68 #include <net/if_arc.h>
69 #include <net/iso88025.h>
72 #include <netinet/ip_carp.h>
75 #include <security/mac/mac_framework.h>
77 #define SIN(s) ((struct sockaddr_in *)s)
78 #define SDL(s) ((struct sockaddr_dl *)s)
80 SYSCTL_DECL(_net_link_ether);
81 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
84 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
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 struct callout la_timer;
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 struct ifqueue arpintrq;
100 static int arp_allocated;
102 static int arp_maxtries = 5;
103 static int useloopback = 1; /* use loopback interface for local traffic */
104 static int arp_proxyall = 0;
106 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
107 &arp_maxtries, 0, "ARP resolution attempts before returning error");
108 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
109 &useloopback, 0, "Use the loopback interface for local traffic");
110 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
111 &arp_proxyall, 0, "Enable proxy ARP for all suitable requests");
113 static void arp_init(void);
114 static void arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
115 static void arprequest(struct ifnet *,
116 struct in_addr *, struct in_addr *, u_char *);
117 static void arpintr(struct mbuf *);
118 static void arptimer(void *);
119 static struct rtentry
120 *arplookup(u_long, int, int, int);
122 static void in_arpinput(struct mbuf *);
131 struct rtentry *rt = (struct rtentry *)arg;
135 * The lock is needed to close a theoretical race
136 * between spontaneous expiry and intentional removal.
137 * We still got an extra reference on rtentry, so can
138 * safely pass pointers to its contents.
142 in_rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL,
147 * Parallel to llc_rtrequest.
150 arp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
152 struct sockaddr *gate;
153 struct llinfo_arp *la;
154 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
155 struct in_ifaddr *ia;
160 if (rt->rt_flags & RTF_GATEWAY)
162 gate = rt->rt_gateway;
163 la = (struct llinfo_arp *)rt->rt_llinfo;
168 * XXX: If this is a manually added route to interface
169 * such as older version of routed or gated might provide,
170 * restore cloning bit.
172 if ((rt->rt_flags & RTF_HOST) == 0 &&
173 rt_mask(rt) != NULL &&
174 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
175 rt->rt_flags |= RTF_CLONING;
176 if (rt->rt_flags & RTF_CLONING) {
178 * Case 1: This route should come from a route to iface.
180 rt_setgate(rt, rt_key(rt),
181 (struct sockaddr *)&null_sdl);
182 gate = rt->rt_gateway;
183 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
184 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
185 rt->rt_expire = time_uptime;
188 /* Announce a new entry if requested. */
189 if (rt->rt_flags & RTF_ANNOUNCE)
190 arprequest(rt->rt_ifp,
191 &SIN(rt_key(rt))->sin_addr,
192 &SIN(rt_key(rt))->sin_addr,
193 (u_char *)LLADDR(SDL(gate)));
196 if (gate->sa_family != AF_LINK ||
197 gate->sa_len < sizeof(null_sdl)) {
198 log(LOG_DEBUG, "%s: bad gateway %s%s\n", __func__,
199 inet_ntoa(SIN(rt_key(rt))->sin_addr),
200 (gate->sa_family != AF_LINK) ?
204 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
205 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
207 break; /* This happens on a route change */
209 * Case 2: This route may come from cloning, or a manual route
210 * add with a LL address.
212 R_Zalloc(la, struct llinfo_arp *, sizeof(*la));
213 rt->rt_llinfo = (caddr_t)la;
215 log(LOG_DEBUG, "%s: malloc failed\n", __func__);
220 * We are storing a route entry outside of radix tree. So,
221 * it can be found and accessed by other means than radix
222 * lookup. The routing code assumes that any rtentry detached
223 * from radix can be destroyed safely. To prevent this, we
224 * add an additional reference.
228 rt->rt_flags |= RTF_LLINFO;
229 callout_init_mtx(&la->la_timer, &rt->rt_mtx,
230 CALLOUT_RETURNUNLOCKED);
234 * This keeps the multicast addresses from showing up
235 * in `arp -a' listings as unresolved. It's not actually
236 * functional. Then the same for broadcast.
238 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) &&
239 rt->rt_ifp->if_type != IFT_ARCNET) {
240 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
242 SDL(gate)->sdl_alen = 6;
245 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
246 memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
247 rt->rt_ifp->if_addrlen);
248 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
253 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
254 if (ia->ia_ifp == rt->rt_ifp &&
255 SIN(rt_key(rt))->sin_addr.s_addr ==
256 (IA_SIN(ia))->sin_addr.s_addr)
261 * This test used to be
262 * if (loif.if_flags & IFF_UP)
263 * It allowed local traffic to be forced
264 * through the hardware by configuring the loopback down.
265 * However, it causes problems during network configuration
266 * for boards that can't receive packets they send.
267 * It is now necessary to clear "useloopback" and remove
268 * the route to force traffic out to the hardware.
271 bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
272 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
275 rt->rt_rmx.rmx_mtu = V_loif->if_mtu;
279 * make sure to set rt->rt_ifa to the interface
280 * address we are using, otherwise we will have trouble
281 * with source address selection.
284 if (ifa != rt->rt_ifa) {
293 if (la == NULL) /* XXX: at least CARP does this. */
295 callout_stop(&la->la_timer);
296 rt->rt_llinfo = NULL;
297 rt->rt_flags &= ~RTF_LLINFO;
300 m_freem(la->la_hold);
306 * Broadcast an ARP request. Caller specifies:
307 * - arp header source ip address
308 * - arp header target ip address
309 * - arp header source ethernet address
312 arprequest(struct ifnet *ifp, struct in_addr *sip, struct in_addr *tip,
319 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
321 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
322 2*ifp->if_data.ifi_addrlen;
323 m->m_pkthdr.len = m->m_len;
324 MH_ALIGN(m, m->m_len);
325 ah = mtod(m, struct arphdr *);
326 bzero((caddr_t)ah, m->m_len);
328 mac_netinet_arp_send(ifp, m);
330 ah->ar_pro = htons(ETHERTYPE_IP);
331 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
332 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
333 ah->ar_op = htons(ARPOP_REQUEST);
334 bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
335 bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
336 bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
337 sa.sa_family = AF_ARP;
339 m->m_flags |= M_BCAST;
340 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
346 * Resolve an IP address into an ethernet address.
348 * ifp is the interface we use
349 * rt0 is the route to the final destination (possibly useless)
350 * m is the mbuf. May be NULL if we don't have a packet.
351 * dst is the next hop,
352 * desten is where we want the address.
354 * On success, desten is filled in and the function returns 0;
355 * If the packet must be held pending resolution, we return EWOULDBLOCK
356 * On other errors, we return the corresponding error code.
357 * Note that m_freem() handles NULL.
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;
370 if (m->m_flags & M_BCAST) {
373 ifp->if_broadcastaddr, ifp->if_addrlen);
376 if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {
378 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
381 fibnum = M_GETFIB(m);
385 /* Look for a cached arp (ll) entry. */
387 fibnum = rt0->rt_fibnum;
388 error = in_rt_check(&rt, &rt0, dst, fibnum);
393 la = (struct llinfo_arp *)rt->rt_llinfo;
399 * We enter this block if rt0 was NULL,
400 * or if rt found by in_rt_check() didn't have llinfo.
401 * we should get a cloned route, which since it should
402 * come from the local interface should have a ll entry.
403 * if may be incoplete but that's ok.
404 * XXXMRT if we haven't found a fibnum is that OK?
406 rt = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0, fibnum);
409 "arpresolve: can't allocate route for %s\n",
410 inet_ntoa(SIN(dst)->sin_addr));
412 return (EINVAL); /* XXX */
414 la = (struct llinfo_arp *)rt->rt_llinfo;
418 "arpresolve: can't allocate llinfo for %s\n",
419 inet_ntoa(SIN(dst)->sin_addr));
421 return (EINVAL); /* XXX */
424 sdl = SDL(rt->rt_gateway);
426 * Check the address family and length is valid, the address
427 * is resolved; otherwise, try to resolve.
429 if ((rt->rt_expire == 0 || rt->rt_expire > time_uptime) &&
430 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
432 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
435 * If entry has an expiry time and it is approaching,
436 * send an ARP request.
438 if ((rt->rt_expire != 0) &&
439 (time_uptime + la->la_preempt > rt->rt_expire)) {
441 SIN(rt->rt_ifa->ifa_addr)->sin_addr;
445 arprequest(ifp, &sin, &SIN(dst)->sin_addr,
454 * If ARP is disabled or static on this interface, stop.
456 * Probably should not allocate empty llinfo struct if we are
457 * not going to be sending out an arp request.
459 if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
465 * There is an arptab entry, but no ethernet address
466 * response yet. Replace the held mbuf with this
471 m_freem(la->la_hold);
474 KASSERT(rt->rt_expire > 0, ("sending ARP request for static entry"));
477 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
478 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
479 * if we have already sent arp_maxtries ARP requests. Retransmit the
480 * ARP request, but not faster than one request per second.
482 if (la->la_asked < V_arp_maxtries)
483 error = EWOULDBLOCK; /* First request. */
485 error = (rt == rt0) ? EHOSTDOWN : EHOSTUNREACH;
487 if (la->la_asked == 0 || rt->rt_expire != time_uptime) {
489 SIN(rt->rt_ifa->ifa_addr)->sin_addr;
491 rt->rt_expire = time_uptime;
492 callout_reset(&la->la_timer, hz, arptimer, rt);
496 arprequest(ifp, &sin, &SIN(dst)->sin_addr,
505 * Common length and type checks are done here,
506 * then the protocol-specific routine is called.
509 arpintr(struct mbuf *m)
513 if (m->m_len < sizeof(struct arphdr) &&
514 ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
515 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
518 ar = mtod(m, struct arphdr *);
520 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER &&
521 ntohs(ar->ar_hrd) != ARPHRD_IEEE802 &&
522 ntohs(ar->ar_hrd) != ARPHRD_ARCNET &&
523 ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) {
524 log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
525 (unsigned char *)&ar->ar_hrd, "");
530 if (m->m_len < arphdr_len(ar)) {
531 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
532 log(LOG_ERR, "arp: runt packet\n");
536 ar = mtod(m, struct arphdr *);
539 switch (ntohs(ar->ar_pro)) {
551 * ARP for Internet protocols on 10 Mb/s Ethernet.
552 * Algorithm is that given in RFC 826.
553 * In addition, a sanity check is performed on the sender
554 * protocol address, to catch impersonators.
555 * We no longer handle negotiations for use of trailer protocol:
556 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
557 * along with IP replies if we wanted trailers sent to us,
558 * and also sent them in response to IP replies.
559 * This allowed either end to announce the desire to receive
561 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
562 * but formerly didn't normally send requests.
564 static int log_arp_wrong_iface = 1;
565 static int log_arp_movements = 1;
566 static int log_arp_permanent_modify = 1;
568 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
569 &log_arp_wrong_iface, 0,
570 "log arp packets arriving on the wrong interface");
571 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
572 &log_arp_movements, 0,
573 "log arp replies from MACs different than the one in the cache");
574 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
575 &log_arp_permanent_modify, 0,
576 "log arp replies from MACs different than the one in the permanent arp entry");
580 in_arpinput(struct mbuf *m)
583 struct ifnet *ifp = m->m_pkthdr.rcvif;
584 struct llinfo_arp *la;
587 struct in_ifaddr *ia;
588 struct sockaddr_dl *sdl;
590 struct in_addr isaddr, itaddr, myaddr;
592 u_int8_t *enaddr = NULL;
595 int bridged = 0, is_bridge = 0;
602 struct sockaddr_in sin;
603 sin.sin_len = sizeof(struct sockaddr_in);
604 sin.sin_family = AF_INET;
605 sin.sin_addr.s_addr = 0;
609 if (ifp->if_type == IFT_BRIDGE)
612 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
613 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
614 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
618 ah = mtod(m, struct arphdr *);
619 op = ntohs(ah->ar_op);
620 (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
621 (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
624 * For a bridge, we want to check the address irrespective
625 * of the receive interface. (This will change slightly
626 * when we have clusters of interfaces).
627 * If the interface does not match, but the recieving interface
628 * is part of carp, we call carp_iamatch to see if this is a
629 * request for the virtual host ip.
630 * XXX: This is really ugly!
632 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
633 if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
634 (ia->ia_ifp == ifp)) &&
635 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
638 if (ifp->if_carp != NULL &&
639 carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) &&
640 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
646 LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
647 if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
648 (ia->ia_ifp == ifp)) &&
649 isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
652 #define BDG_MEMBER_MATCHES_ARP(addr, ifp, ia) \
653 (ia->ia_ifp->if_bridge == ifp->if_softc && \
654 !bcmp(IF_LLADDR(ia->ia_ifp), IF_LLADDR(ifp), ifp->if_addrlen) && \
655 addr == ia->ia_addr.sin_addr.s_addr)
657 * Check the case when bridge shares its MAC address with
658 * some of its children, so packets are claimed by bridge
659 * itself (bridge_input() does it first), but they are really
660 * meant to be destined to the bridge member.
663 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
664 if (BDG_MEMBER_MATCHES_ARP(itaddr.s_addr, ifp, ia)) {
670 #undef BDG_MEMBER_MATCHES_ARP
673 * No match, use the first inet address on the receive interface
674 * as a dummy address for the rest of the function.
676 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
677 if (ifa->ifa_addr->sa_family == AF_INET) {
682 * If bridging, fall back to using any inet address.
684 if (!bridged || (ia = TAILQ_FIRST(&V_in_ifaddrhead)) == NULL)
688 enaddr = (u_int8_t *)IF_LLADDR(ifp);
689 myaddr = ia->ia_addr.sin_addr;
690 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
691 goto drop; /* it's from me, ignore it. */
692 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
694 "arp: link address is broadcast for IP address %s!\n",
699 * Warn if another host is using the same IP address, but only if the
700 * IP address isn't 0.0.0.0, which is used for DHCP only, in which
701 * case we suppress the warning to avoid false positive complaints of
702 * potential misconfiguration.
704 if (!bridged && isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
706 "arp: %*D is using my IP address %s on %s!\n",
707 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
708 inet_ntoa(isaddr), ifp->if_xname);
712 if (ifp->if_flags & IFF_STATICARP)
715 * We look for any FIBs that has this address to find
717 * For sanity checks that are FIB independent we abort the loop.
719 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
720 rt = arplookup(isaddr.s_addr,
721 itaddr.s_addr == myaddr.s_addr, 0, fibnum);
725 sdl = SDL(rt->rt_gateway);
726 /* Only call this once */
728 sin.sin_addr.s_addr = isaddr.s_addr;
729 EVENTHANDLER_INVOKE(route_arp_update_event, rt,
730 ar_sha(ah), (struct sockaddr *)&sin);
733 la = (struct llinfo_arp *)rt->rt_llinfo;
740 /* The following is not an error when doing bridging. */
741 if (!bridged && rt->rt_ifp != ifp
743 && (ifp->if_type != IFT_CARP || !carp_match)
746 if (log_arp_wrong_iface)
747 log(LOG_ERR, "arp: %s is on %s "
748 "but got reply from %*D "
751 rt->rt_ifp->if_xname,
753 (u_char *)ar_sha(ah), ":",
759 bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
761 if (log_arp_movements)
763 "arp: %s moved from %*D to %*D "
767 (u_char *)LLADDR(sdl), ":",
769 (u_char *)ar_sha(ah), ":",
773 if (log_arp_permanent_modify)
775 "arp: %*D attempts to "
776 "modify permanent entry "
779 (u_char *)ar_sha(ah), ":",
786 * sanity check for the address length.
787 * XXX this does not work for protocols
788 * with variable address length. -is
791 sdl->sdl_alen != ah->ar_hln) {
793 "arp from %*D: new addr len %d, was %d",
794 ifp->if_addrlen, (u_char *) ar_sha(ah),
795 ":", ah->ar_hln, sdl->sdl_alen);
797 if (ifp->if_addrlen != ah->ar_hln) {
799 "arp from %*D: addr len: "
800 "new %d, i/f %d (ignored)",
801 ifp->if_addrlen, (u_char *) ar_sha(ah),
802 ":", ah->ar_hln, ifp->if_addrlen);
810 /* Copy in the information received. */
811 (void)memcpy(LLADDR(sdl), ar_sha(ah),
812 sdl->sdl_alen = ah->ar_hln);
814 * If we receive an arp from a token-ring station over
815 * a token-ring nic then try to save the source routing info.
816 * XXXMRT Only minimal Token Ring support for MRT.
817 * Only do this on the first pass as if modifies the mbuf.
819 if (ifp->if_type == IFT_ISO88025) {
820 struct iso88025_header *th = NULL;
821 struct iso88025_sockaddr_dl_data *trld;
823 /* force the fib loop to end after this pass */
824 fibnum = rt_numfibs - 1;
826 th = (struct iso88025_header *)m->m_pkthdr.header;
827 trld = SDL_ISO88025(sdl);
828 rif_len = TR_RCF_RIFLEN(th->rcf);
829 if ((th->iso88025_shost[0] & TR_RII) &&
831 trld->trld_rcf = th->rcf;
832 trld->trld_rcf ^= htons(TR_RCF_DIR);
833 memcpy(trld->trld_route, th->rd, rif_len - 2);
834 trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
836 * Set up source routing information for
839 m->m_data -= rif_len;
841 m->m_pkthdr.len += rif_len;
843 th->iso88025_shost[0] &= ~TR_RII;
848 m->m_pkthdr.len += 8;
849 th->rcf = trld->trld_rcf;
853 rt->rt_expire = time_uptime + V_arpt_keep;
854 callout_reset(&la->la_timer, hz * V_arpt_keep,
858 la->la_preempt = V_arp_maxtries;
863 (*ifp->if_output)(ifp, hold, rt_key(rt), rt);
864 } /* end of FIB loop */
868 * Decide if we have to respond to something.
870 if (op != ARPOP_REQUEST)
872 if (itaddr.s_addr == myaddr.s_addr) {
873 /* Shortcut.. the receiving interface is the target. */
874 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
875 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
877 /* It's not asking for our address. But it still may
878 * be something we should answer.
881 * We assume that link level info is independent of
882 * the table used and so we use whichever we can and don't
883 * have a better option.
885 /* Have we been asked to proxy for the target. */
886 rt = arplookup(itaddr.s_addr, 0, SIN_PROXY, goodfib);
888 /* Nope, only intersted now if proxying everything. */
889 struct sockaddr_in sin;
894 bzero(&sin, sizeof sin);
895 sin.sin_family = AF_INET;
896 sin.sin_len = sizeof sin;
897 sin.sin_addr = itaddr;
899 /* XXX MRT use table 0 for arp reply */
900 rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0);
904 * Don't send proxies for nodes on the same interface
905 * as this one came out of, or we'll get into a fight
906 * over who claims what Ether address.
908 if (rt->rt_ifp == ifp) {
912 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
913 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
917 * Also check that the node which sent the ARP packet
918 * is on the the interface we expect it to be on. This
919 * avoids ARP chaos if an interface is connected to the
922 sin.sin_addr = isaddr;
924 /* XXX MRT use table 0 for arp checks */
925 rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0);
928 if (rt->rt_ifp != ifp) {
929 log(LOG_INFO, "arp_proxy: ignoring request"
930 " from %s via %s, expecting %s\n",
931 inet_ntoa(isaddr), ifp->if_xname,
932 rt->rt_ifp->if_xname);
939 printf("arp: proxying for %s\n",
944 * Return proxied ARP replies only on the interface
945 * or bridge cluster where this network resides.
946 * Otherwise we may conflict with the host we are
949 if (rt->rt_ifp != ifp &&
950 (rt->rt_ifp->if_bridge != ifp->if_bridge ||
951 ifp->if_bridge == NULL)) {
955 sdl = SDL(rt->rt_gateway);
956 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
957 (void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
962 if (itaddr.s_addr == myaddr.s_addr &&
963 IN_LINKLOCAL(ntohl(itaddr.s_addr))) {
964 /* RFC 3927 link-local IPv4; always reply by broadcast. */
965 #ifdef DEBUG_LINKLOCAL
966 printf("arp: sending reply for link-local addr %s\n",
969 m->m_flags |= M_BCAST;
970 m->m_flags &= ~M_MCAST;
972 /* default behaviour; never reply by broadcast. */
973 m->m_flags &= ~(M_BCAST|M_MCAST);
975 (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
976 (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
977 ah->ar_op = htons(ARPOP_REPLY);
978 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
979 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
980 m->m_pkthdr.len = m->m_len;
981 sa.sa_family = AF_ARP;
983 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
992 * Lookup or enter a new address in arptab.
994 static struct rtentry *
995 arplookup(u_long addr, int create, int proxy, int fibnum)
998 struct sockaddr_inarp sin;
1001 bzero(&sin, sizeof(sin));
1002 sin.sin_len = sizeof(sin);
1003 sin.sin_family = AF_INET;
1004 sin.sin_addr.s_addr = addr;
1006 sin.sin_other = SIN_PROXY;
1007 rt = in_rtalloc1((struct sockaddr *)&sin, create, 0UL, fibnum);
1011 if (rt->rt_flags & RTF_GATEWAY)
1012 why = "host is not on local network";
1013 else if ((rt->rt_flags & RTF_LLINFO) == 0)
1014 why = "could not allocate llinfo";
1015 else if (rt->rt_gateway->sa_family != AF_LINK)
1016 why = "gateway route is not ours";
1019 #define ISDYNCLONE(_rt) \
1020 (((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED)
1022 log(LOG_DEBUG, "arplookup %s failed: %s\n",
1023 inet_ntoa(sin.sin_addr), why);
1025 * If there are no references to this Layer 2 route,
1026 * and it is a cloned route, and not static, and
1027 * arplookup() is creating the route, then purge
1028 * it from the routing table as it is probably bogus.
1030 if (rt->rt_refcnt == 1 && ISDYNCLONE(rt))
1042 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1044 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
1045 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
1046 &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
1047 ifa->ifa_rtrequest = arp_rtrequest;
1048 ifa->ifa_flags |= RTF_CLONING;
1052 arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr)
1054 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
1055 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
1056 &IA_SIN(ifa)->sin_addr, enaddr);
1057 ifa->ifa_rtrequest = arp_rtrequest;
1058 ifa->ifa_flags |= RTF_CLONING;
1065 arpintrq.ifq_maxlen = 50;
1066 mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF);
1067 netisr_register(NETISR_ARP, arpintr, &arpintrq, 0);
1069 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);