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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)if_ether.c 8.1 (Berkeley) 6/10/93
34 * $FreeBSD: src/sys/netinet/if_ether.c,v 1.64.2.23 2003/04/11 07:23:15 fjoe Exp $
35 * $DragonFly: src/sys/netinet/if_ether.c,v 1.20 2004/12/14 18:46:08 hsu Exp $
39 * Ethernet address resolution protocol.
41 * add "inuse/lock" bit (or ref. count) along with valid bit
47 #include <sys/param.h>
48 #include <sys/kernel.h>
49 #include <sys/queue.h>
50 #include <sys/sysctl.h>
51 #include <sys/systm.h>
53 #include <sys/malloc.h>
54 #include <sys/socket.h>
55 #include <sys/syslog.h>
57 #include <sys/thread2.h>
58 #include <sys/msgport2.h>
61 #include <net/if_dl.h>
62 #include <net/if_types.h>
63 #include <net/route.h>
64 #include <net/netisr.h>
65 #include <net/if_llc.h>
67 #include <net/ethernet.h>
68 #include <net/bridge/bridge.h>
71 #include <netinet/in.h>
72 #include <netinet/in_var.h>
73 #include <netinet/if_ether.h>
75 #include <net/if_arc.h>
76 #include <net/iso88025.h>
78 #define SIN(s) ((struct sockaddr_in *)s)
79 #define SDL(s) ((struct sockaddr_dl *)s)
81 SYSCTL_DECL(_net_link_ether);
82 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
85 static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
86 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
87 static int arpt_down = 20; /* once declared down, don't send for 20 sec */
89 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
91 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW,
93 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
96 #define rt_expire rt_rmx.rmx_expire
99 LIST_ENTRY(llinfo_arp) la_le;
100 struct rtentry *la_rt;
101 struct mbuf *la_hold; /* last packet until resolved/timeout */
102 u_short la_preempt; /* countdown for pre-expiry arps */
103 u_short la_asked; /* #times we QUERIED following expiration */
106 static LIST_HEAD(, llinfo_arp) llinfo_arp;
108 static int arp_inuse, arp_allocated, arpinit_done;
110 static int arp_maxtries = 5;
111 static int useloopback = 1; /* use loopback interface for local traffic */
112 static int arp_proxyall = 0;
114 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
115 &arp_maxtries, 0, "");
116 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
117 &useloopback, 0, "");
118 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
119 &arp_proxyall, 0, "");
121 static void arp_rtrequest (int, struct rtentry *, struct rt_addrinfo *);
122 static void arprequest (struct ifnet *,
123 struct in_addr *, struct in_addr *, u_char *);
124 static int arpintr(struct netmsg *);
125 static void arptfree (struct llinfo_arp *);
126 static void arptimer (void *);
127 static struct llinfo_arp
128 *arplookup (u_long, int, int);
130 static void in_arpinput (struct mbuf *);
133 static struct callout arptimer_ch;
136 * Timeout routine. Age arp_tab entries periodically.
140 arptimer(ignored_arg)
144 struct llinfo_arp *la, *nla;
146 LIST_FOREACH_MUTABLE(la, &llinfo_arp, la_le, nla) {
147 if (la->la_rt->rt_expire && la->la_rt->rt_expire <= time_second)
148 arptfree(la); /* might remove la from llinfo_arp! */
150 callout_reset(&arptimer_ch, arpt_prune * hz, arptimer, NULL);
155 * Parallel to llc_rtrequest.
158 arp_rtrequest(req, rt, info)
161 struct rt_addrinfo *info;
163 struct sockaddr *gate = rt->rt_gateway;
164 struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
165 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
169 callout_init(&arptimer_ch);
170 callout_reset(&arptimer_ch, hz, arptimer, NULL);
172 if (rt->rt_flags & RTF_GATEWAY)
178 * XXX: If this is a manually added route to interface
179 * such as older version of routed or gated might provide,
180 * restore cloning bit.
182 if ((rt->rt_flags & RTF_HOST) == 0 &&
183 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
184 rt->rt_flags |= RTF_CLONING;
185 if (rt->rt_flags & RTF_CLONING) {
187 * Case 1: This route should come from a route to iface.
189 rt_setgate(rt, rt_key(rt),
190 (struct sockaddr *)&null_sdl);
191 gate = rt->rt_gateway;
192 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
193 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
194 rt->rt_expire = time_second;
197 /* Announce a new entry if requested. */
198 if (rt->rt_flags & RTF_ANNOUNCE)
199 arprequest(rt->rt_ifp,
200 &SIN(rt_key(rt))->sin_addr,
201 &SIN(rt_key(rt))->sin_addr,
202 (u_char *)LLADDR(SDL(gate)));
205 if (gate->sa_family != AF_LINK ||
206 gate->sa_len < sizeof(null_sdl)) {
207 log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
210 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
211 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
213 break; /* This happens on a route change */
215 * Case 2: This route may come from cloning, or a manual route
216 * add with a LL address.
218 R_Malloc(la, struct llinfo_arp *, sizeof(*la));
219 rt->rt_llinfo = (caddr_t)la;
221 log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
224 arp_inuse++, arp_allocated++;
225 bzero(la, sizeof(*la));
227 rt->rt_flags |= RTF_LLINFO;
228 LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
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 if (SIN(rt_key(rt))->sin_addr.s_addr ==
252 (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
254 * This test used to be
255 * if (loif.if_flags & IFF_UP)
256 * It allowed local traffic to be forced
257 * through the hardware by configuring the loopback down.
258 * However, it causes problems during network configuration
259 * for boards that can't receive packets they send.
260 * It is now necessary to clear "useloopback" and remove
261 * the route to force traffic out to the hardware.
264 bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
265 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
276 LIST_REMOVE(la, la_le);
278 rt->rt_flags &= ~RTF_LLINFO;
280 m_freem(la->la_hold);
286 * Broadcast an ARP request. Caller specifies:
287 * - arp header source ip address
288 * - arp header target ip address
289 * - arp header source ethernet address
292 arprequest(ifp, sip, tip, enaddr)
294 struct in_addr *sip, *tip;
298 struct ether_header *eh;
299 struct arc_header *arh;
302 static u_char llcx[] = { 0x82, 0x40, LLC_SNAP_LSAP, LLC_SNAP_LSAP,
303 LLC_UI, 0x00, 0x00, 0x00, 0x08, 0x06 };
306 if ((m = m_gethdr(MB_DONTWAIT, MT_DATA)) == NULL)
308 m->m_pkthdr.rcvif = (struct ifnet *)0;
309 switch (ifp->if_type) {
311 ar_hrd = htons(ARPHRD_ARCNET);
313 m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
314 m->m_pkthdr.len = m->m_len;
315 MH_ALIGN(m, m->m_len);
317 arh = (struct arc_header *)sa.sa_data;
318 arh->arc_dhost = ifp->if_broadcastaddr[0];
319 arh->arc_type = ARCTYPE_ARP;
321 ah = mtod(m, struct arphdr *);
325 ar_hrd = htons(ARPHRD_IEEE802);
327 m->m_len = sizeof(llcx) +
328 arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
329 m->m_pkthdr.len = m->m_len;
330 MH_ALIGN(m, m->m_len);
332 (void)memcpy(mtod(m, caddr_t), llcx, sizeof(llcx));
333 memcpy(sa.sa_data, ifp->if_broadcastaddr, ifp->if_addrlen);
334 (void)memcpy(sa.sa_data + 6, enaddr, 6);
335 sa.sa_data[6] |= TR_RII;
336 sa.sa_data[12] = TR_AC;
337 sa.sa_data[13] = TR_LLC_FRAME;
339 ah = (struct arphdr *)(mtod(m, char *) + sizeof(llcx));
344 * This may not be correct for types not explicitly
345 * listed, but this is our best guess
348 ar_hrd = htons(ARPHRD_ETHER);
350 m->m_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
351 m->m_pkthdr.len = m->m_len;
352 MH_ALIGN(m, m->m_len);
354 eh = (struct ether_header *)sa.sa_data;
355 /* if_output will not swap */
356 eh->ether_type = htons(ETHERTYPE_ARP);
357 memcpy(eh->ether_dhost, ifp->if_broadcastaddr, ifp->if_addrlen);
359 ah = mtod(m, struct arphdr *);
364 ah->ar_pro = htons(ETHERTYPE_IP);
365 ah->ar_hln = ifp->if_addrlen; /* hardware address length */
366 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */
367 ah->ar_op = htons(ARPOP_REQUEST);
368 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
369 memset(ar_tha(ah), 0, ah->ar_hln);
370 (void)memcpy(ar_spa(ah), sip, ah->ar_pln);
371 (void)memcpy(ar_tpa(ah), tip, ah->ar_pln);
373 sa.sa_family = AF_UNSPEC;
374 sa.sa_len = sizeof(sa);
375 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
379 * Resolve an IP address into an ethernet address. If success,
380 * desten is filled in. If there is no entry in arptab,
381 * set one up and broadcast a request for the IP address.
382 * Hold onto this mbuf and resend it once the address
383 * is finally resolved. A return value of 1 indicates
384 * that desten has been filled in and the packet should be sent
385 * normally; a 0 return indicates that the packet has been
386 * taken over here, either now or for later transmission.
389 arpresolve(ifp, rt, m, dst, desten, rt0)
393 struct sockaddr *dst;
397 struct llinfo_arp *la = 0;
398 struct sockaddr_dl *sdl;
400 if (m->m_flags & M_BCAST) { /* broadcast */
401 memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
404 if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */
405 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
409 la = (struct llinfo_arp *)rt->rt_llinfo;
411 la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
415 if (la == 0 || rt == 0) {
416 log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
417 inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "",
422 sdl = SDL(rt->rt_gateway);
424 * Check the address family and length is valid, the address
425 * is resolved; otherwise, try to resolve.
427 if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
428 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
430 * If entry has an expiry time and it is approaching,
431 * see if we need to send an ARP request within this
432 * arpt_down interval.
434 if ((rt->rt_expire != 0) &&
435 (time_second + la->la_preempt > rt->rt_expire)) {
437 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
443 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
447 * If ARP is disabled on this interface, stop.
449 * Probably should not allocate empty llinfo struct if we are
450 * not going to be sending out an arp request.
452 if (ifp->if_flags & IFF_NOARP) {
457 * There is an arptab entry, but no ethernet address
458 * response yet. Replace the held mbuf with this
462 m_freem(la->la_hold);
464 if (rt->rt_expire || ((rt->rt_flags & RTF_STATIC) && !sdl->sdl_alen)) {
465 rt->rt_flags &= ~RTF_REJECT;
466 if (la->la_asked == 0 || rt->rt_expire != time_second) {
467 rt->rt_expire = time_second;
468 if (la->la_asked++ < arp_maxtries) {
470 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
474 rt->rt_flags |= RTF_REJECT;
475 rt->rt_expire += arpt_down;
477 la->la_preempt = arp_maxtries;
486 * Common length and type checks are done here,
487 * then the protocol-specific routine is called.
490 arpintr(struct netmsg *msg)
492 struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
496 if (m->m_len < sizeof(struct arphdr) &&
497 ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
498 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
501 ar = mtod(m, struct arphdr *);
503 ar_hrd = ntohs(ar->ar_hrd);
504 if (ar_hrd != ARPHRD_ETHER &&
505 ar_hrd != ARPHRD_IEEE802 &&
506 ar_hrd != ARPHRD_ARCNET) {
508 "arp: unknown hardware address format (0x%2D)\n",
509 (unsigned char *)&ar->ar_hrd, "");
513 if (m->m_pkthdr.len < arphdr_len(ar) &&
514 (m = m_pullup(m, arphdr_len(ar))) == NULL) {
515 log(LOG_ERR, "arp: runt packet\n");
519 switch (ntohs(ar->ar_pro)) {
529 lwkt_replymsg(&msg->nm_lmsg, 0);
535 * ARP for Internet protocols on 10 Mb/s Ethernet.
536 * Algorithm is that given in RFC 826.
537 * In addition, a sanity check is performed on the sender
538 * protocol address, to catch impersonators.
539 * We no longer handle negotiations for use of trailer protocol:
540 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
541 * along with IP replies if we wanted trailers sent to us,
542 * and also sent them in response to IP replies.
543 * This allowed either end to announce the desire to receive
545 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
546 * but formerly didn't normally send requests.
548 static int log_arp_wrong_iface = 1;
550 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
551 &log_arp_wrong_iface, 0,
552 "log arp packets arriving on the wrong interface");
559 struct ifnet *ifp = m->m_pkthdr.rcvif;
560 struct ether_header *eh;
561 struct arc_header *arh;
562 struct iso88025_header *th = (struct iso88025_header *)0;
563 struct iso88025_sockaddr_dl_data *trld;
564 struct llinfo_arp *la = 0;
567 struct in_ifaddr *ia;
568 struct sockaddr_dl *sdl;
570 struct in_addr isaddr, itaddr, myaddr;
574 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
575 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
576 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
580 ah = mtod(m, struct arphdr *);
581 op = ntohs(ah->ar_op);
582 (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
583 (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
585 #define BRIDGE_TEST (do_bridge)
587 #define BRIDGE_TEST (0) /* cc will optimise the test away */
590 * For a bridge, we want to check the address irrespective
591 * of the receive interface. (This will change slightly
592 * when we have clusters of interfaces).
594 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash)
595 if ((BRIDGE_TEST || (ia->ia_ifp == ifp)) &&
596 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
598 LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
599 if ((BRIDGE_TEST || (ia->ia_ifp == ifp)) &&
600 isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
603 * No match, use the first inet address on the receive interface
604 * as a dummy address for the rest of the function.
606 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
607 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
612 * If bridging, fall back to using any inet address.
613 * This is probably incorrect, the right way being try to match
614 * addresses for interfaces in the same cluster, so if we
615 * get here we should always drop the packet.
618 (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL) {
623 myaddr = ia->ia_addr.sin_addr;
624 if (!bcmp(ar_sha(ah), IF_LLADDR(ifp), ifp->if_addrlen)) {
625 m_freem(m); /* it's from me, ignore it. */
628 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
630 "arp: link address is broadcast for IP address %s!\n",
635 if (isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
637 "arp: %*D is using my IP address %s!\n",
638 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
643 la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
644 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
645 /* the following is not an error when doing bridging */
646 if (!BRIDGE_TEST && rt->rt_ifp != ifp) {
647 if (log_arp_wrong_iface)
648 log(LOG_ERR, "arp: %s is on %s but got reply from %*D on %s\n",
650 rt->rt_ifp->if_xname,
651 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
656 bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
658 log(LOG_INFO, "arp: %s moved from %*D to %*D on %s\n",
660 ifp->if_addrlen, (u_char *)LLADDR(sdl), ":",
661 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
665 "arp: %*D attempts to modify permanent entry for %s on %s\n",
666 ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
667 inet_ntoa(isaddr), ifp->if_xname);
672 * sanity check for the address length.
673 * XXX this does not work for protocols with variable address
677 sdl->sdl_alen != ah->ar_hln) {
679 "arp from %*D: new addr len %d, was %d",
680 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
681 ah->ar_hln, sdl->sdl_alen);
683 if (ifp->if_addrlen != ah->ar_hln) {
685 "arp from %*D: addr len: new %d, i/f %d (ignored)",
686 ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
687 ah->ar_hln, ifp->if_addrlen);
690 (void)memcpy(LLADDR(sdl), ar_sha(ah),
691 sdl->sdl_alen = ah->ar_hln);
693 * If we receive an arp from a token-ring station over
694 * a token-ring nic then try to save the source
697 if (ifp->if_type == IFT_ISO88025) {
698 th = (struct iso88025_header *)m->m_pkthdr.header;
699 trld = SDL_ISO88025(sdl);
700 rif_len = TR_RCF_RIFLEN(th->rcf);
701 if ((th->iso88025_shost[0] & TR_RII) &&
703 trld->trld_rcf = th->rcf;
704 trld->trld_rcf ^= htons(TR_RCF_DIR);
705 memcpy(trld->trld_route, th->rd, rif_len - 2);
706 trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
708 * Set up source routing information for
711 m->m_data -= rif_len;
713 m->m_pkthdr.len += rif_len;
715 th->iso88025_shost[0] &= ~TR_RII;
720 m->m_pkthdr.len += 8;
721 th->rcf = trld->trld_rcf;
724 rt->rt_expire = time_second + arpt_keep;
725 rt->rt_flags &= ~RTF_REJECT;
727 la->la_preempt = arp_maxtries;
729 (*ifp->if_output)(ifp, la->la_hold,
735 if (op != ARPOP_REQUEST) {
739 if (itaddr.s_addr == myaddr.s_addr) {
740 /* I am the target */
741 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
742 (void)memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln);
744 la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
746 struct sockaddr_in sin;
753 bzero(&sin, sizeof sin);
754 sin.sin_family = AF_INET;
755 sin.sin_len = sizeof sin;
756 sin.sin_addr = itaddr;
758 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
764 * Don't send proxies for nodes on the same interface
765 * as this one came out of, or we'll get into a fight
766 * over who claims what Ether address.
768 if (rt->rt_ifp == ifp) {
773 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
774 (void)memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln);
777 printf("arp: proxying for %s\n",
782 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
783 sdl = SDL(rt->rt_gateway);
784 (void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
788 (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
789 (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
790 ah->ar_op = htons(ARPOP_REPLY);
791 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
792 switch (ifp->if_type) {
794 arh = (struct arc_header *)sa.sa_data;
795 arh->arc_dhost = *ar_tha(ah);
796 arh->arc_type = ARCTYPE_ARP;
800 /* Re-arrange the source/dest address */
801 memcpy(th->iso88025_dhost, th->iso88025_shost,
802 sizeof(th->iso88025_dhost));
803 memcpy(th->iso88025_shost, IF_LLADDR(ifp),
804 sizeof(th->iso88025_shost));
805 /* Set the source routing bit if neccesary */
806 if (th->iso88025_dhost[0] & TR_RII) {
807 th->iso88025_dhost[0] &= ~TR_RII;
808 if (TR_RCF_RIFLEN(th->rcf) > 2)
809 th->iso88025_shost[0] |= TR_RII;
811 /* Copy the addresses, ac and fc into sa_data */
812 memcpy(sa.sa_data, th->iso88025_dhost,
813 sizeof(th->iso88025_dhost) * 2);
814 sa.sa_data[(sizeof(th->iso88025_dhost) * 2)] = TR_AC;
815 sa.sa_data[(sizeof(th->iso88025_dhost) * 2) + 1] = TR_LLC_FRAME;
820 * May not be correct for types not explictly
821 * listed, but it is our best guess.
824 eh = (struct ether_header *)sa.sa_data;
825 (void)memcpy(eh->ether_dhost, ar_tha(ah),
826 sizeof(eh->ether_dhost));
827 eh->ether_type = htons(ETHERTYPE_ARP);
830 sa.sa_family = AF_UNSPEC;
831 sa.sa_len = sizeof(sa);
832 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
838 * Free an arp entry. If the arp entry is actively referenced or represents
839 * a static entry we only clear it back to an unresolved state, otherwise
840 * we destroy the entry entirely.
842 * Note that static entries are created when route add ... -interface is used
843 * to create an interface route to a (direct) destination.
847 struct llinfo_arp *la;
849 struct rtentry *rt = la->la_rt;
850 struct sockaddr_dl *sdl;
853 sdl = SDL(rt->rt_gateway);
854 if (sdl && ((rt->rt_refcnt > 0 && sdl->sdl_family == AF_LINK) ||
855 (rt->rt_flags & RTF_STATIC))) {
857 la->la_preempt = la->la_asked = 0;
858 rt->rt_flags &= ~RTF_REJECT;
861 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
862 0, (struct rtentry **)0);
865 * Lookup or enter a new address in arptab.
867 static struct llinfo_arp *
868 arplookup(addr, create, proxy)
873 static struct sockaddr_inarp sin = {sizeof(sin), AF_INET };
876 sin.sin_addr.s_addr = addr;
877 sin.sin_other = proxy ? SIN_PROXY : 0;
878 rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
883 if (rt->rt_flags & RTF_GATEWAY)
884 why = "host is not on local network";
885 else if ((rt->rt_flags & RTF_LLINFO) == 0)
886 why = "could not allocate llinfo";
887 else if (rt->rt_gateway->sa_family != AF_LINK)
888 why = "gateway route is not ours";
892 log(LOG_DEBUG, "arplookup %s failed: %s\n",
893 inet_ntoa(sin.sin_addr), why);
896 /* if there are no references to this route, purge it */
897 if (rt->rt_refcnt <= 0 &&
898 (rt->rt_flags & RTF_WASCLONED) == RTF_WASCLONED) {
899 rtrequest(RTM_DELETE,
900 (struct sockaddr *)rt_key(rt), rt->rt_gateway,
901 rt_mask(rt), rt->rt_flags, 0);
905 return ((struct llinfo_arp *)rt->rt_llinfo);
913 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
914 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
915 &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
916 ifa->ifa_rtrequest = arp_rtrequest;
917 ifa->ifa_flags |= RTF_CLONING;
923 LIST_INIT(&llinfo_arp);
924 netisr_register(NETISR_ARP, cpu0_portfn, arpintr);
927 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);