1 /* 2 * Copyright (c) 1982, 1986, 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 16 * 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 27 * SUCH DAMAGE. 28 * 29 * @(#)if_ether.c 8.1 (Berkeley) 6/10/93 30 * $FreeBSD$ 31 */ 32 33 /* 34 * Ethernet address resolution protocol. 35 * TODO: 36 * add "inuse/lock" bit (or ref. count) along with valid bit 37 */ 38 39 #include "opt_inet.h" 40 #include "opt_bdg.h" 41 #include "opt_mac.h" 42 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> 48 #include <sys/mac.h> 49 #include <sys/mbuf.h> 50 #include <sys/malloc.h> 51 #include <sys/socket.h> 52 #include <sys/syslog.h> 53 54 #include <net/if.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 #ifdef BRIDGE 61 #include <net/ethernet.h> 62 #include <net/bridge.h> 63 #endif 64 65 #include <netinet/in.h> 66 #include <netinet/in_var.h> 67 #include <netinet/if_ether.h> 68 69 #include <net/if_arc.h> 70 #include <net/iso88025.h> 71 72 #define SIN(s) ((struct sockaddr_in *)s) 73 #define SDL(s) ((struct sockaddr_dl *)s) 74 75 SYSCTL_DECL(_net_link_ether); 76 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, ""); 77 78 /* timer values */ 79 static int arpt_prune = (5*60*1); /* walk list every 5 minutes */ 80 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ 81 static int arpt_down = 20; /* once declared down, don't send for 20 sec */ 82 83 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW, 84 &arpt_prune, 0, ""); 85 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW, 86 &arpt_keep, 0, ""); 87 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW, 88 &arpt_down, 0, ""); 89 90 #define rt_expire rt_rmx.rmx_expire 91 92 struct llinfo_arp { 93 LIST_ENTRY(llinfo_arp) la_le; 94 struct rtentry *la_rt; 95 struct mbuf *la_hold; /* last packet until resolved/timeout */ 96 u_short la_preempt; /* countdown for pre-expiry arps */ 97 u_short la_asked; /* #times we QUERIED following expiration */ 98 #define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */ 99 }; 100 101 static LIST_HEAD(, llinfo_arp) llinfo_arp; 102 103 static struct ifqueue arpintrq; 104 static int arp_allocated; 105 106 static int arp_maxtries = 5; 107 static int useloopback = 1; /* use loopback interface for local traffic */ 108 static int arp_proxyall = 0; 109 static struct callout arp_callout; 110 111 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW, 112 &arp_maxtries, 0, ""); 113 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW, 114 &useloopback, 0, ""); 115 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW, 116 &arp_proxyall, 0, ""); 117 118 static void arp_init(void); 119 static void arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *); 120 static void arprequest(struct ifnet *, 121 struct in_addr *, struct in_addr *, u_char *); 122 static void arpintr(struct mbuf *); 123 static void arptfree(struct llinfo_arp *); 124 static void arptimer(void *); 125 static struct llinfo_arp 126 *arplookup(u_long, int, int); 127 #ifdef INET 128 static void in_arpinput(struct mbuf *); 129 #endif 130 131 /* 132 * Timeout routine. Age arp_tab entries periodically. 133 */ 134 /* ARGSUSED */ 135 static void 136 arptimer(ignored_arg) 137 void *ignored_arg; 138 { 139 struct llinfo_arp *la, *ola; 140 141 RADIX_NODE_HEAD_LOCK(rt_tables[AF_INET]); 142 la = LIST_FIRST(&llinfo_arp); 143 while (la != NULL) { 144 struct rtentry *rt = la->la_rt; 145 ola = la; 146 la = LIST_NEXT(la, la_le); 147 if (rt->rt_expire && rt->rt_expire <= time_second) 148 arptfree(ola); /* timer has expired, clear */ 149 } 150 RADIX_NODE_HEAD_UNLOCK(rt_tables[AF_INET]); 151 152 callout_reset(&arp_callout, arpt_prune * hz, arptimer, NULL); 153 } 154 155 /* 156 * Parallel to llc_rtrequest. 157 */ 158 static void 159 arp_rtrequest(req, rt, info) 160 int req; 161 struct rtentry *rt; 162 struct rt_addrinfo *info; 163 { 164 struct sockaddr *gate; 165 struct llinfo_arp *la; 166 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 167 168 RT_LOCK_ASSERT(rt); 169 170 if (rt->rt_flags & RTF_GATEWAY) 171 return; 172 gate = rt->rt_gateway; 173 la = (struct llinfo_arp *)rt->rt_llinfo; 174 switch (req) { 175 176 case RTM_ADD: 177 /* 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. 181 */ 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) { 186 /* 187 * Case 1: This route should come from a route to iface. 188 */ 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; 195 break; 196 } 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))); 203 /*FALLTHROUGH*/ 204 case RTM_RESOLVE: 205 if (gate->sa_family != AF_LINK || 206 gate->sa_len < sizeof(null_sdl)) { 207 log(LOG_DEBUG, "%s: bad gateway %s%s\n", __func__, 208 inet_ntoa(SIN(rt_key(rt))->sin_addr), 209 (gate->sa_family != AF_LINK) ? 210 " (!AF_LINK)": ""); 211 break; 212 } 213 SDL(gate)->sdl_type = rt->rt_ifp->if_type; 214 SDL(gate)->sdl_index = rt->rt_ifp->if_index; 215 if (la != 0) 216 break; /* This happens on a route change */ 217 /* 218 * Case 2: This route may come from cloning, or a manual route 219 * add with a LL address. 220 */ 221 R_Zalloc(la, struct llinfo_arp *, sizeof(*la)); 222 rt->rt_llinfo = (caddr_t)la; 223 if (la == 0) { 224 log(LOG_DEBUG, "%s: malloc failed\n", __func__); 225 break; 226 } 227 arp_allocated++; 228 la->la_rt = rt; 229 rt->rt_flags |= RTF_LLINFO; 230 RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]); 231 LIST_INSERT_HEAD(&llinfo_arp, la, la_le); 232 233 #ifdef INET 234 /* 235 * This keeps the multicast addresses from showing up 236 * in `arp -a' listings as unresolved. It's not actually 237 * functional. Then the same for broadcast. 238 */ 239 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) && 240 rt->rt_ifp->if_type != IFT_ARCNET) { 241 ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr, 242 LLADDR(SDL(gate))); 243 SDL(gate)->sdl_alen = 6; 244 rt->rt_expire = 0; 245 } 246 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) { 247 memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr, 248 rt->rt_ifp->if_addrlen); 249 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen; 250 rt->rt_expire = 0; 251 } 252 #endif 253 254 if (SIN(rt_key(rt))->sin_addr.s_addr == 255 (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) { 256 /* 257 * This test used to be 258 * if (loif.if_flags & IFF_UP) 259 * It allowed local traffic to be forced 260 * through the hardware by configuring the loopback down. 261 * However, it causes problems during network configuration 262 * for boards that can't receive packets they send. 263 * It is now necessary to clear "useloopback" and remove 264 * the route to force traffic out to the hardware. 265 */ 266 rt->rt_expire = 0; 267 bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)), 268 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen); 269 if (useloopback) 270 rt->rt_ifp = loif; 271 272 } 273 break; 274 275 case RTM_DELETE: 276 if (la == 0) 277 break; 278 RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]); 279 LIST_REMOVE(la, la_le); 280 rt->rt_llinfo = 0; 281 rt->rt_flags &= ~RTF_LLINFO; 282 if (la->la_hold) 283 m_freem(la->la_hold); 284 Free((caddr_t)la); 285 } 286 } 287 288 /* 289 * Broadcast an ARP request. Caller specifies: 290 * - arp header source ip address 291 * - arp header target ip address 292 * - arp header source ethernet address 293 */ 294 static void 295 arprequest(ifp, sip, tip, enaddr) 296 struct ifnet *ifp; 297 struct in_addr *sip, *tip; 298 u_char *enaddr; 299 { 300 struct mbuf *m; 301 struct arphdr *ah; 302 struct sockaddr sa; 303 304 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 305 return; 306 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) + 307 2*ifp->if_data.ifi_addrlen; 308 m->m_pkthdr.len = m->m_len; 309 MH_ALIGN(m, m->m_len); 310 ah = mtod(m, struct arphdr *); 311 bzero((caddr_t)ah, m->m_len); 312 #ifdef MAC 313 mac_create_mbuf_linklayer(ifp, m); 314 #endif 315 ah->ar_pro = htons(ETHERTYPE_IP); 316 ah->ar_hln = ifp->if_addrlen; /* hardware address length */ 317 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ 318 ah->ar_op = htons(ARPOP_REQUEST); 319 bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln); 320 bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln); 321 bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln); 322 sa.sa_family = AF_ARP; 323 sa.sa_len = 2; 324 m->m_flags |= M_BCAST; 325 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 326 327 return; 328 } 329 330 /* 331 * Resolve an IP address into an ethernet address. 332 * On input: 333 * ifp is the interface we use 334 * dst is the next hop, 335 * rt0 is the route to the final destination (possibly useless) 336 * m is the mbuf 337 * desten is where we want the address. 338 * 339 * On success, desten is filled in and the function returns 0; 340 * If the packet must be held pending resolution, we return EWOULDBLOCK 341 * On other errors, we return the corresponding error code. 342 */ 343 int 344 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m, 345 struct sockaddr *dst, u_char *desten) 346 { 347 struct llinfo_arp *la = 0; 348 struct sockaddr_dl *sdl; 349 int error; 350 struct rtentry *rt; 351 352 error = rt_check(&rt, &rt0, dst); 353 if (error) { 354 m_freem(m); 355 return error; 356 } 357 358 if (m->m_flags & M_BCAST) { /* broadcast */ 359 (void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen); 360 return (0); 361 } 362 if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */ 363 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 364 return (0); 365 } 366 if (rt) 367 la = (struct llinfo_arp *)rt->rt_llinfo; 368 if (la == 0) { 369 la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0); 370 if (la) 371 rt = la->la_rt; 372 } 373 if (la == 0 || rt == 0) { 374 log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n", 375 inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "", 376 rt ? "rt" : ""); 377 m_freem(m); 378 return (EINVAL); /* XXX */ 379 } 380 sdl = SDL(rt->rt_gateway); 381 /* 382 * Check the address family and length is valid, the address 383 * is resolved; otherwise, try to resolve. 384 */ 385 if ((rt->rt_expire == 0 || rt->rt_expire > time_second) && 386 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { 387 /* 388 * If entry has an expiry time and it is approaching, 389 * see if we need to send an ARP request within this 390 * arpt_down interval. 391 */ 392 if ((rt->rt_expire != 0) && 393 (time_second + la->la_preempt > rt->rt_expire)) { 394 arprequest(ifp, 395 &SIN(rt->rt_ifa->ifa_addr)->sin_addr, 396 &SIN(dst)->sin_addr, 397 IF_LLADDR(ifp)); 398 la->la_preempt--; 399 } 400 401 bcopy(LLADDR(sdl), desten, sdl->sdl_alen); 402 return (0); 403 } 404 /* 405 * If ARP is disabled or static on this interface, stop. 406 * XXX 407 * Probably should not allocate empty llinfo struct if we are 408 * not going to be sending out an arp request. 409 */ 410 if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) { 411 m_freem(m); 412 return (EINVAL); 413 } 414 /* 415 * There is an arptab entry, but no ethernet address 416 * response yet. Replace the held mbuf with this 417 * latest one. 418 */ 419 if (la->la_hold) 420 m_freem(la->la_hold); 421 la->la_hold = m; 422 if (rt->rt_expire) { 423 RT_LOCK(rt); 424 rt->rt_flags &= ~RTF_REJECT; 425 if (la->la_asked == 0 || rt->rt_expire != time_second) { 426 rt->rt_expire = time_second; 427 if (la->la_asked++ < arp_maxtries) { 428 arprequest(ifp, 429 &SIN(rt->rt_ifa->ifa_addr)->sin_addr, 430 &SIN(dst)->sin_addr, 431 IF_LLADDR(ifp)); 432 } else { 433 rt->rt_flags |= RTF_REJECT; 434 rt->rt_expire += arpt_down; 435 la->la_asked = 0; 436 la->la_preempt = arp_maxtries; 437 } 438 439 } 440 RT_UNLOCK(rt); 441 } 442 return (EWOULDBLOCK); 443 } 444 445 /* 446 * Common length and type checks are done here, 447 * then the protocol-specific routine is called. 448 */ 449 static void 450 arpintr(struct mbuf *m) 451 { 452 struct arphdr *ar; 453 454 if (m->m_len < sizeof(struct arphdr) && 455 ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) { 456 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n"); 457 return; 458 } 459 ar = mtod(m, struct arphdr *); 460 461 if (ntohs(ar->ar_hrd) != ARPHRD_ETHER && 462 ntohs(ar->ar_hrd) != ARPHRD_IEEE802 && 463 ntohs(ar->ar_hrd) != ARPHRD_ARCNET) { 464 log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n", 465 (unsigned char *)&ar->ar_hrd, ""); 466 m_freem(m); 467 return; 468 } 469 470 if (m->m_len < arphdr_len(ar)) { 471 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) { 472 log(LOG_ERR, "arp: runt packet\n"); 473 m_freem(m); 474 return; 475 } 476 ar = mtod(m, struct arphdr *); 477 } 478 479 switch (ntohs(ar->ar_pro)) { 480 #ifdef INET 481 case ETHERTYPE_IP: 482 in_arpinput(m); 483 return; 484 #endif 485 } 486 m_freem(m); 487 } 488 489 #ifdef INET 490 /* 491 * ARP for Internet protocols on 10 Mb/s Ethernet. 492 * Algorithm is that given in RFC 826. 493 * In addition, a sanity check is performed on the sender 494 * protocol address, to catch impersonators. 495 * We no longer handle negotiations for use of trailer protocol: 496 * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 497 * along with IP replies if we wanted trailers sent to us, 498 * and also sent them in response to IP replies. 499 * This allowed either end to announce the desire to receive 500 * trailer packets. 501 * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 502 * but formerly didn't normally send requests. 503 */ 504 static int log_arp_wrong_iface = 1; 505 static int log_arp_movements = 1; 506 507 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW, 508 &log_arp_wrong_iface, 0, 509 "log arp packets arriving on the wrong interface"); 510 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW, 511 &log_arp_movements, 0, 512 "log arp replies from MACs different than the one in the cache"); 513 514 515 static void 516 in_arpinput(m) 517 struct mbuf *m; 518 { 519 struct arphdr *ah; 520 struct ifnet *ifp = m->m_pkthdr.rcvif; 521 struct iso88025_header *th = (struct iso88025_header *)0; 522 struct iso88025_sockaddr_dl_data *trld; 523 struct llinfo_arp *la = 0; 524 struct rtentry *rt; 525 struct ifaddr *ifa; 526 struct in_ifaddr *ia; 527 struct sockaddr_dl *sdl; 528 struct sockaddr sa; 529 struct in_addr isaddr, itaddr, myaddr; 530 int op, rif_len; 531 int req_len; 532 533 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 534 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) { 535 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n"); 536 return; 537 } 538 539 ah = mtod(m, struct arphdr *); 540 op = ntohs(ah->ar_op); 541 (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr)); 542 (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr)); 543 #ifdef BRIDGE 544 #define BRIDGE_TEST (do_bridge) 545 #else 546 #define BRIDGE_TEST (0) /* cc will optimise the test away */ 547 #endif 548 /* 549 * For a bridge, we want to check the address irrespective 550 * of the receive interface. (This will change slightly 551 * when we have clusters of interfaces). 552 */ 553 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) 554 if ((BRIDGE_TEST || (ia->ia_ifp == ifp)) && 555 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) 556 goto match; 557 LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash) 558 if ((BRIDGE_TEST || (ia->ia_ifp == ifp)) && 559 isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) 560 goto match; 561 /* 562 * No match, use the first inet address on the receive interface 563 * as a dummy address for the rest of the function. 564 */ 565 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 566 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) { 567 ia = ifatoia(ifa); 568 goto match; 569 } 570 /* 571 * If bridging, fall back to using any inet address. 572 */ 573 if (!BRIDGE_TEST || (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL) 574 goto drop; 575 match: 576 myaddr = ia->ia_addr.sin_addr; 577 if (!bcmp(ar_sha(ah), IF_LLADDR(ifp), ifp->if_addrlen)) 578 goto drop; /* it's from me, ignore it. */ 579 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) { 580 log(LOG_ERR, 581 "arp: link address is broadcast for IP address %s!\n", 582 inet_ntoa(isaddr)); 583 goto drop; 584 } 585 if (isaddr.s_addr == myaddr.s_addr) { 586 log(LOG_ERR, 587 "arp: %*D is using my IP address %s!\n", 588 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 589 inet_ntoa(isaddr)); 590 itaddr = myaddr; 591 goto reply; 592 } 593 if (ifp->if_flags & IFF_STATICARP) 594 goto reply; 595 la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0); 596 if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) { 597 /* the following is not an error when doing bridging */ 598 if (!BRIDGE_TEST && rt->rt_ifp != ifp) { 599 if (log_arp_wrong_iface) 600 log(LOG_ERR, "arp: %s is on %s but got reply from %*D on %s\n", 601 inet_ntoa(isaddr), 602 rt->rt_ifp->if_xname, 603 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 604 ifp->if_xname); 605 goto reply; 606 } 607 if (sdl->sdl_alen && 608 bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) { 609 if (rt->rt_expire) { 610 if (log_arp_movements) 611 log(LOG_INFO, "arp: %s moved from %*D to %*D on %s\n", 612 inet_ntoa(isaddr), 613 ifp->if_addrlen, (u_char *)LLADDR(sdl), ":", 614 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 615 ifp->if_xname); 616 } else { 617 log(LOG_ERR, 618 "arp: %*D attempts to modify permanent entry for %s on %s\n", 619 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 620 inet_ntoa(isaddr), ifp->if_xname); 621 goto reply; 622 } 623 } 624 /* 625 * sanity check for the address length. 626 * XXX this does not work for protocols with variable address 627 * length. -is 628 */ 629 if (sdl->sdl_alen && 630 sdl->sdl_alen != ah->ar_hln) { 631 log(LOG_WARNING, 632 "arp from %*D: new addr len %d, was %d", 633 ifp->if_addrlen, (u_char *) ar_sha(ah), ":", 634 ah->ar_hln, sdl->sdl_alen); 635 } 636 if (ifp->if_addrlen != ah->ar_hln) { 637 log(LOG_WARNING, 638 "arp from %*D: addr len: new %d, i/f %d (ignored)", 639 ifp->if_addrlen, (u_char *) ar_sha(ah), ":", 640 ah->ar_hln, ifp->if_addrlen); 641 goto reply; 642 } 643 (void)memcpy(LLADDR(sdl), ar_sha(ah), 644 sdl->sdl_alen = ah->ar_hln); 645 /* 646 * If we receive an arp from a token-ring station over 647 * a token-ring nic then try to save the source 648 * routing info. 649 */ 650 if (ifp->if_type == IFT_ISO88025) { 651 th = (struct iso88025_header *)m->m_pkthdr.header; 652 trld = SDL_ISO88025(sdl); 653 rif_len = TR_RCF_RIFLEN(th->rcf); 654 if ((th->iso88025_shost[0] & TR_RII) && 655 (rif_len > 2)) { 656 trld->trld_rcf = th->rcf; 657 trld->trld_rcf ^= htons(TR_RCF_DIR); 658 memcpy(trld->trld_route, th->rd, rif_len - 2); 659 trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK); 660 /* 661 * Set up source routing information for 662 * reply packet (XXX) 663 */ 664 m->m_data -= rif_len; 665 m->m_len += rif_len; 666 m->m_pkthdr.len += rif_len; 667 } else { 668 th->iso88025_shost[0] &= ~TR_RII; 669 trld->trld_rcf = 0; 670 } 671 m->m_data -= 8; 672 m->m_len += 8; 673 m->m_pkthdr.len += 8; 674 th->rcf = trld->trld_rcf; 675 } 676 RT_LOCK(rt); 677 if (rt->rt_expire) 678 rt->rt_expire = time_second + arpt_keep; 679 rt->rt_flags &= ~RTF_REJECT; 680 RT_UNLOCK(rt); 681 la->la_asked = 0; 682 la->la_preempt = arp_maxtries; 683 if (la->la_hold) { 684 (*ifp->if_output)(ifp, la->la_hold, rt_key(rt), rt); 685 la->la_hold = 0; 686 } 687 } 688 reply: 689 if (op != ARPOP_REQUEST) 690 goto drop; 691 if (itaddr.s_addr == myaddr.s_addr) { 692 /* I am the target */ 693 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 694 (void)memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln); 695 } else { 696 la = arplookup(itaddr.s_addr, 0, SIN_PROXY); 697 if (la == NULL) { 698 struct sockaddr_in sin; 699 700 if (!arp_proxyall) 701 goto drop; 702 703 bzero(&sin, sizeof sin); 704 sin.sin_family = AF_INET; 705 sin.sin_len = sizeof sin; 706 sin.sin_addr = itaddr; 707 708 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 709 if (!rt) 710 goto drop; 711 /* 712 * Don't send proxies for nodes on the same interface 713 * as this one came out of, or we'll get into a fight 714 * over who claims what Ether address. 715 */ 716 if (rt->rt_ifp == ifp) { 717 rtfree(rt); 718 goto drop; 719 } 720 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 721 (void)memcpy(ar_sha(ah), IF_LLADDR(ifp), ah->ar_hln); 722 rtfree(rt); 723 724 /* 725 * Also check that the node which sent the ARP packet 726 * is on the the interface we expect it to be on. This 727 * avoids ARP chaos if an interface is connected to the 728 * wrong network. 729 */ 730 sin.sin_addr = isaddr; 731 732 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 733 if (!rt) 734 goto drop; 735 if (rt->rt_ifp != ifp) { 736 log(LOG_INFO, "arp_proxy: ignoring request" 737 " from %s via %s, expecting %s\n", 738 inet_ntoa(isaddr), ifp->if_xname, 739 rt->rt_ifp->if_xname); 740 rtfree(rt); 741 goto drop; 742 } 743 rtfree(rt); 744 745 #ifdef DEBUG_PROXY 746 printf("arp: proxying for %s\n", 747 inet_ntoa(itaddr)); 748 #endif 749 } else { 750 rt = la->la_rt; 751 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 752 sdl = SDL(rt->rt_gateway); 753 (void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln); 754 } 755 } 756 757 (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln); 758 (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln); 759 ah->ar_op = htons(ARPOP_REPLY); 760 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 761 m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */ 762 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln); 763 m->m_pkthdr.len = m->m_len; 764 sa.sa_family = AF_ARP; 765 sa.sa_len = 2; 766 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 767 return; 768 769 drop: 770 m_freem(m); 771 } 772 #endif 773 774 /* 775 * Free an arp entry. 776 */ 777 static void 778 arptfree(la) 779 struct llinfo_arp *la; 780 { 781 struct rtentry *rt = la->la_rt; 782 struct sockaddr_dl *sdl; 783 784 if (rt == 0) 785 panic("arptfree"); 786 if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) && 787 sdl->sdl_family == AF_LINK) { 788 sdl->sdl_alen = 0; 789 la->la_preempt = la->la_asked = 0; 790 RT_LOCK(rt); /* XXX needed or move higher? */ 791 rt->rt_flags &= ~RTF_REJECT; 792 RT_UNLOCK(rt); 793 return; 794 } 795 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt), 796 0, (struct rtentry **)0); 797 } 798 /* 799 * Lookup or enter a new address in arptab. 800 */ 801 static struct llinfo_arp * 802 arplookup(addr, create, proxy) 803 u_long addr; 804 int create, proxy; 805 { 806 struct rtentry *rt; 807 struct sockaddr_inarp sin; 808 const char *why = 0; 809 810 bzero(&sin, sizeof(sin)); 811 sin.sin_len = sizeof(sin); 812 sin.sin_family = AF_INET; 813 sin.sin_addr.s_addr = addr; 814 if (proxy) 815 sin.sin_other = SIN_PROXY; 816 rt = rtalloc1((struct sockaddr *)&sin, create, 0UL); 817 if (rt == 0) 818 return (0); 819 820 if (rt->rt_flags & RTF_GATEWAY) 821 why = "host is not on local network"; 822 else if ((rt->rt_flags & RTF_LLINFO) == 0) 823 why = "could not allocate llinfo"; 824 else if (rt->rt_gateway->sa_family != AF_LINK) 825 why = "gateway route is not ours"; 826 827 if (why) { 828 #define ISDYNCLONE(_rt) \ 829 (((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED) 830 if (create) 831 log(LOG_DEBUG, "arplookup %s failed: %s\n", 832 inet_ntoa(sin.sin_addr), why); 833 /* 834 * If there are no references to this Layer 2 route, 835 * and it is a cloned route, and not static, and 836 * arplookup() is creating the route, then purge 837 * it from the routing table as it is probably bogus. 838 */ 839 if (rt->rt_refcnt == 1 && ISDYNCLONE(rt)) 840 rtexpunge(rt); 841 RTFREE_LOCKED(rt); 842 return (0); 843 #undef ISDYNCLONE 844 } else { 845 RT_REMREF(rt); 846 RT_UNLOCK(rt); 847 return ((struct llinfo_arp *)rt->rt_llinfo); 848 } 849 } 850 851 void 852 arp_ifinit(ifp, ifa) 853 struct ifnet *ifp; 854 struct ifaddr *ifa; 855 { 856 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 857 arprequest(ifp, &IA_SIN(ifa)->sin_addr, 858 &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp)); 859 ifa->ifa_rtrequest = arp_rtrequest; 860 ifa->ifa_flags |= RTF_CLONING; 861 } 862 863 static void 864 arp_init(void) 865 { 866 867 arpintrq.ifq_maxlen = 50; 868 mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF); 869 LIST_INIT(&llinfo_arp); 870 callout_init(&arp_callout, CALLOUT_MPSAFE); 871 netisr_register(NETISR_ARP, arpintr, &arpintrq, NETISR_MPSAFE); 872 callout_reset(&arp_callout, hz, arptimer, NULL); 873 } 874 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0); 875