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