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 */ 31 32 /* 33 * Ethernet address resolution protocol. 34 * TODO: 35 * add "inuse/lock" bit (or ref. count) along with valid bit 36 */ 37 38 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 #include "opt_inet.h" 42 #include "opt_mac.h" 43 #include "opt_carp.h" 44 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> 50 #include <sys/mbuf.h> 51 #include <sys/malloc.h> 52 #include <sys/proc.h> 53 #include <sys/socket.h> 54 #include <sys/syslog.h> 55 #include <sys/vimage.h> 56 57 #include <net/if.h> 58 #include <net/if_dl.h> 59 #include <net/if_types.h> 60 #include <net/route.h> 61 #include <net/netisr.h> 62 #include <net/if_llc.h> 63 #include <net/ethernet.h> 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 #ifdef DEV_CARP 73 #include <netinet/ip_carp.h> 74 #endif 75 76 #include <security/mac/mac_framework.h> 77 78 #define SIN(s) ((struct sockaddr_in *)s) 79 #define SDL(s) ((struct sockaddr_dl *)s) 80 81 SYSCTL_DECL(_net_link_ether); 82 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, ""); 83 84 /* timer values */ 85 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ 86 87 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW, 88 &arpt_keep, 0, "ARP entry lifetime in seconds"); 89 90 #define rt_expire rt_rmx.rmx_expire 91 92 struct llinfo_arp { 93 struct callout la_timer; 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; /* # requests sent */ 98 }; 99 100 static struct ifqueue arpintrq; 101 static int arp_allocated; 102 103 static int arp_maxtries = 5; 104 static int useloopback = 1; /* use loopback interface for local traffic */ 105 static int arp_proxyall = 0; 106 107 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW, 108 &arp_maxtries, 0, "ARP resolution attempts before returning error"); 109 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW, 110 &useloopback, 0, "Use the loopback interface for local traffic"); 111 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW, 112 &arp_proxyall, 0, "Enable proxy ARP for all suitable requests"); 113 114 static void arp_init(void); 115 static void arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *); 116 static void arprequest(struct ifnet *, 117 struct in_addr *, struct in_addr *, u_char *); 118 static void arpintr(struct mbuf *); 119 static void arptimer(void *); 120 static struct rtentry 121 *arplookup(u_long, int, int, int); 122 #ifdef INET 123 static void in_arpinput(struct mbuf *); 124 #endif 125 126 /* 127 * Timeout routine. 128 */ 129 static void 130 arptimer(void *arg) 131 { 132 struct rtentry *rt = (struct rtentry *)arg; 133 134 RT_LOCK_ASSERT(rt); 135 /* 136 * The lock is needed to close a theoretical race 137 * between spontaneous expiry and intentional removal. 138 * We still got an extra reference on rtentry, so can 139 * safely pass pointers to its contents. 140 */ 141 RT_UNLOCK(rt); 142 143 in_rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL, 144 rt->rt_fibnum); 145 } 146 147 /* 148 * Parallel to llc_rtrequest. 149 */ 150 static void 151 arp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info) 152 { 153 struct sockaddr *gate; 154 struct llinfo_arp *la; 155 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 156 struct in_ifaddr *ia; 157 struct ifaddr *ifa; 158 159 RT_LOCK_ASSERT(rt); 160 161 if (rt->rt_flags & RTF_GATEWAY) 162 return; 163 gate = rt->rt_gateway; 164 la = (struct llinfo_arp *)rt->rt_llinfo; 165 switch (req) { 166 167 case RTM_ADD: 168 /* 169 * XXX: If this is a manually added route to interface 170 * such as older version of routed or gated might provide, 171 * restore cloning bit. 172 */ 173 if ((rt->rt_flags & RTF_HOST) == 0 && 174 rt_mask(rt) != NULL && 175 SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 176 rt->rt_flags |= RTF_CLONING; 177 if (rt->rt_flags & RTF_CLONING) { 178 /* 179 * Case 1: This route should come from a route to iface. 180 */ 181 rt_setgate(rt, rt_key(rt), 182 (struct sockaddr *)&null_sdl); 183 gate = rt->rt_gateway; 184 SDL(gate)->sdl_type = rt->rt_ifp->if_type; 185 SDL(gate)->sdl_index = rt->rt_ifp->if_index; 186 rt->rt_expire = time_uptime; 187 break; 188 } 189 /* Announce a new entry if requested. */ 190 if (rt->rt_flags & RTF_ANNOUNCE) 191 arprequest(rt->rt_ifp, 192 &SIN(rt_key(rt))->sin_addr, 193 &SIN(rt_key(rt))->sin_addr, 194 (u_char *)LLADDR(SDL(gate))); 195 /*FALLTHROUGH*/ 196 case RTM_RESOLVE: 197 if (gate->sa_family != AF_LINK || 198 gate->sa_len < sizeof(null_sdl)) { 199 log(LOG_DEBUG, "%s: bad gateway %s%s\n", __func__, 200 inet_ntoa(SIN(rt_key(rt))->sin_addr), 201 (gate->sa_family != AF_LINK) ? 202 " (!AF_LINK)": ""); 203 break; 204 } 205 SDL(gate)->sdl_type = rt->rt_ifp->if_type; 206 SDL(gate)->sdl_index = rt->rt_ifp->if_index; 207 if (la != 0) 208 break; /* This happens on a route change */ 209 /* 210 * Case 2: This route may come from cloning, or a manual route 211 * add with a LL address. 212 */ 213 R_Zalloc(la, struct llinfo_arp *, sizeof(*la)); 214 rt->rt_llinfo = (caddr_t)la; 215 if (la == 0) { 216 log(LOG_DEBUG, "%s: malloc failed\n", __func__); 217 break; 218 } 219 arp_allocated++; 220 /* 221 * We are storing a route entry outside of radix tree. So, 222 * it can be found and accessed by other means than radix 223 * lookup. The routing code assumes that any rtentry detached 224 * from radix can be destroyed safely. To prevent this, we 225 * add an additional reference. 226 */ 227 RT_ADDREF(rt); 228 la->la_rt = rt; 229 rt->rt_flags |= RTF_LLINFO; 230 callout_init_mtx(&la->la_timer, &rt->rt_mtx, 231 CALLOUT_RETURNUNLOCKED); 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 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 255 if (ia->ia_ifp == rt->rt_ifp && 256 SIN(rt_key(rt))->sin_addr.s_addr == 257 (IA_SIN(ia))->sin_addr.s_addr) 258 break; 259 } 260 if (ia) { 261 /* 262 * This test used to be 263 * if (loif.if_flags & IFF_UP) 264 * It allowed local traffic to be forced 265 * through the hardware by configuring the loopback down. 266 * However, it causes problems during network configuration 267 * for boards that can't receive packets they send. 268 * It is now necessary to clear "useloopback" and remove 269 * the route to force traffic out to the hardware. 270 */ 271 rt->rt_expire = 0; 272 bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)), 273 SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen); 274 if (V_useloopback) { 275 rt->rt_ifp = V_loif; 276 rt->rt_rmx.rmx_mtu = V_loif->if_mtu; 277 } 278 279 /* 280 * make sure to set rt->rt_ifa to the interface 281 * address we are using, otherwise we will have trouble 282 * with source address selection. 283 */ 284 ifa = &ia->ia_ifa; 285 if (ifa != rt->rt_ifa) { 286 IFAFREE(rt->rt_ifa); 287 IFAREF(ifa); 288 rt->rt_ifa = ifa; 289 } 290 } 291 break; 292 293 case RTM_DELETE: 294 if (la == NULL) /* XXX: at least CARP does this. */ 295 break; 296 callout_stop(&la->la_timer); 297 rt->rt_llinfo = NULL; 298 rt->rt_flags &= ~RTF_LLINFO; 299 RT_REMREF(rt); 300 if (la->la_hold) 301 m_freem(la->la_hold); 302 Free((caddr_t)la); 303 } 304 } 305 306 /* 307 * Broadcast an ARP request. Caller specifies: 308 * - arp header source ip address 309 * - arp header target ip address 310 * - arp header source ethernet address 311 */ 312 static void 313 arprequest(struct ifnet *ifp, struct in_addr *sip, struct in_addr *tip, 314 u_char *enaddr) 315 { 316 struct mbuf *m; 317 struct arphdr *ah; 318 struct sockaddr sa; 319 320 if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 321 return; 322 m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) + 323 2*ifp->if_data.ifi_addrlen; 324 m->m_pkthdr.len = m->m_len; 325 MH_ALIGN(m, m->m_len); 326 ah = mtod(m, struct arphdr *); 327 bzero((caddr_t)ah, m->m_len); 328 #ifdef MAC 329 mac_netinet_arp_send(ifp, m); 330 #endif 331 ah->ar_pro = htons(ETHERTYPE_IP); 332 ah->ar_hln = ifp->if_addrlen; /* hardware address length */ 333 ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ 334 ah->ar_op = htons(ARPOP_REQUEST); 335 bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln); 336 bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln); 337 bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln); 338 sa.sa_family = AF_ARP; 339 sa.sa_len = 2; 340 m->m_flags |= M_BCAST; 341 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 342 343 return; 344 } 345 346 /* 347 * Resolve an IP address into an ethernet address. 348 * On input: 349 * ifp is the interface we use 350 * rt0 is the route to the final destination (possibly useless) 351 * m is the mbuf. May be NULL if we don't have a packet. 352 * dst is the next hop, 353 * desten is where we want the address. 354 * 355 * On success, desten is filled in and the function returns 0; 356 * If the packet must be held pending resolution, we return EWOULDBLOCK 357 * On other errors, we return the corresponding error code. 358 * Note that m_freem() handles NULL. 359 */ 360 int 361 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m, 362 struct sockaddr *dst, u_char *desten) 363 { 364 struct llinfo_arp *la = NULL; 365 struct rtentry *rt = NULL; 366 struct sockaddr_dl *sdl; 367 int error; 368 int fibnum = -1; 369 370 if (m) { 371 if (m->m_flags & M_BCAST) { 372 /* broadcast */ 373 (void)memcpy(desten, 374 ifp->if_broadcastaddr, ifp->if_addrlen); 375 return (0); 376 } 377 if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) { 378 /* multicast */ 379 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 380 return (0); 381 } 382 fibnum = M_GETFIB(m); 383 } 384 385 if (rt0 != NULL) { 386 /* Look for a cached arp (ll) entry. */ 387 if (m == NULL) 388 fibnum = rt0->rt_fibnum; 389 error = rt_check(&rt, &rt0, dst); 390 if (error) { 391 m_freem(m); 392 return error; 393 } 394 la = (struct llinfo_arp *)rt->rt_llinfo; 395 if (la == NULL) 396 RT_UNLOCK(rt); 397 } 398 399 /* 400 * If we had no mbuf and no route, then hope the caller 401 * has a fib in mind because we are running out of ideas. 402 * I think this should not happen in current code. 403 * (kmacy would know). 404 */ 405 if (fibnum == -1) 406 fibnum = curthread->td_proc->p_fibnum; /* last gasp */ 407 408 if (la == NULL) { 409 /* 410 * We enter this block if rt0 was NULL, 411 * or if rt found by rt_check() didn't have llinfo. 412 * we should get a cloned route, which since it should 413 * come from the local interface should have a ll entry. 414 * It may be incomplete but that's ok. 415 */ 416 rt = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0, fibnum); 417 if (rt == NULL) { 418 log(LOG_DEBUG, 419 "arpresolve: can't allocate route for %s\n", 420 inet_ntoa(SIN(dst)->sin_addr)); 421 m_freem(m); 422 return (EINVAL); /* XXX */ 423 } 424 la = (struct llinfo_arp *)rt->rt_llinfo; 425 if (la == NULL) { 426 RT_UNLOCK(rt); 427 log(LOG_DEBUG, 428 "arpresolve: can't allocate llinfo for %s\n", 429 inet_ntoa(SIN(dst)->sin_addr)); 430 m_freem(m); 431 return (EINVAL); /* XXX */ 432 } 433 } 434 sdl = SDL(rt->rt_gateway); 435 /* 436 * Check the address family and length is valid, the address 437 * is resolved; otherwise, try to resolve. 438 */ 439 if ((rt->rt_expire == 0 || rt->rt_expire > time_uptime) && 440 sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { 441 442 bcopy(LLADDR(sdl), desten, sdl->sdl_alen); 443 444 /* 445 * If entry has an expiry time and it is approaching, 446 * send an ARP request. 447 */ 448 if ((rt->rt_expire != 0) && 449 (time_uptime + la->la_preempt > rt->rt_expire)) { 450 struct in_addr sin = 451 SIN(rt->rt_ifa->ifa_addr)->sin_addr; 452 453 la->la_preempt--; 454 RT_UNLOCK(rt); 455 arprequest(ifp, &sin, &SIN(dst)->sin_addr, 456 IF_LLADDR(ifp)); 457 return (0); 458 } 459 460 RT_UNLOCK(rt); 461 return (0); 462 } 463 /* 464 * If ARP is disabled or static on this interface, stop. 465 * XXX 466 * Probably should not allocate empty llinfo struct if we are 467 * not going to be sending out an arp request. 468 */ 469 if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) { 470 RT_UNLOCK(rt); 471 m_freem(m); 472 return (EINVAL); 473 } 474 /* 475 * There is an arptab entry, but no ethernet address 476 * response yet. Replace the held mbuf with this 477 * latest one. 478 */ 479 if (m) { 480 if (la->la_hold) 481 m_freem(la->la_hold); 482 la->la_hold = m; 483 } 484 KASSERT(rt->rt_expire > 0, ("sending ARP request for static entry")); 485 486 /* 487 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It 488 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH 489 * if we have already sent arp_maxtries ARP requests. Retransmit the 490 * ARP request, but not faster than one request per second. 491 */ 492 if (la->la_asked < V_arp_maxtries) 493 error = EWOULDBLOCK; /* First request. */ 494 else 495 error = (rt == rt0) ? EHOSTDOWN : EHOSTUNREACH; 496 497 if (la->la_asked == 0 || rt->rt_expire != time_uptime) { 498 struct in_addr sin = 499 SIN(rt->rt_ifa->ifa_addr)->sin_addr; 500 501 rt->rt_expire = time_uptime; 502 callout_reset(&la->la_timer, hz, arptimer, rt); 503 la->la_asked++; 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(struct mbuf *m) 591 { 592 struct arphdr *ah; 593 struct ifnet *ifp = m->m_pkthdr.rcvif; 594 struct llinfo_arp *la; 595 struct rtentry *rt; 596 struct ifaddr *ifa; 597 struct in_ifaddr *ia; 598 struct sockaddr_dl *sdl; 599 struct sockaddr sa; 600 struct in_addr isaddr, itaddr, myaddr; 601 struct mbuf *hold; 602 u_int8_t *enaddr = NULL; 603 int op, rif_len; 604 int req_len; 605 int bridged = 0, is_bridge = 0; 606 u_int fibnum; 607 u_int goodfib = 0; 608 int firstpass = 1; 609 #ifdef DEV_CARP 610 int carp_match = 0; 611 #endif 612 struct sockaddr_in sin; 613 sin.sin_len = sizeof(struct sockaddr_in); 614 sin.sin_family = AF_INET; 615 sin.sin_addr.s_addr = 0; 616 617 if (ifp->if_bridge) 618 bridged = 1; 619 if (ifp->if_type == IFT_BRIDGE) 620 is_bridge = 1; 621 622 req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 623 if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) { 624 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n"); 625 return; 626 } 627 628 ah = mtod(m, struct arphdr *); 629 op = ntohs(ah->ar_op); 630 (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr)); 631 (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr)); 632 633 /* 634 * For a bridge, we want to check the address irrespective 635 * of the receive interface. (This will change slightly 636 * when we have clusters of interfaces). 637 * If the interface does not match, but the recieving interface 638 * is part of carp, we call carp_iamatch to see if this is a 639 * request for the virtual host ip. 640 * XXX: This is really ugly! 641 */ 642 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) { 643 if (((bridged && ia->ia_ifp->if_bridge != NULL) || 644 (ia->ia_ifp == ifp)) && 645 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) 646 goto match; 647 #ifdef DEV_CARP 648 if (ifp->if_carp != NULL && 649 carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) && 650 itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) { 651 carp_match = 1; 652 goto match; 653 } 654 #endif 655 } 656 LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash) 657 if (((bridged && ia->ia_ifp->if_bridge != NULL) || 658 (ia->ia_ifp == ifp)) && 659 isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) 660 goto match; 661 662 #define BDG_MEMBER_MATCHES_ARP(addr, ifp, ia) \ 663 (ia->ia_ifp->if_bridge == ifp->if_softc && \ 664 !bcmp(IF_LLADDR(ia->ia_ifp), IF_LLADDR(ifp), ifp->if_addrlen) && \ 665 addr == ia->ia_addr.sin_addr.s_addr) 666 /* 667 * Check the case when bridge shares its MAC address with 668 * some of its children, so packets are claimed by bridge 669 * itself (bridge_input() does it first), but they are really 670 * meant to be destined to the bridge member. 671 */ 672 if (is_bridge) { 673 LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) { 674 if (BDG_MEMBER_MATCHES_ARP(itaddr.s_addr, ifp, ia)) { 675 ifp = ia->ia_ifp; 676 goto match; 677 } 678 } 679 } 680 #undef BDG_MEMBER_MATCHES_ARP 681 682 /* 683 * No match, use the first inet address on the receive interface 684 * as a dummy address for the rest of the function. 685 */ 686 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 687 if (ifa->ifa_addr->sa_family == AF_INET) { 688 ia = ifatoia(ifa); 689 goto match; 690 } 691 /* 692 * If bridging, fall back to using any inet address. 693 */ 694 if (!bridged || (ia = TAILQ_FIRST(&V_in_ifaddrhead)) == NULL) 695 goto drop; 696 match: 697 if (!enaddr) 698 enaddr = (u_int8_t *)IF_LLADDR(ifp); 699 myaddr = ia->ia_addr.sin_addr; 700 if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) 701 goto drop; /* it's from me, ignore it. */ 702 if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) { 703 log(LOG_ERR, 704 "arp: link address is broadcast for IP address %s!\n", 705 inet_ntoa(isaddr)); 706 goto drop; 707 } 708 /* 709 * Warn if another host is using the same IP address, but only if the 710 * IP address isn't 0.0.0.0, which is used for DHCP only, in which 711 * case we suppress the warning to avoid false positive complaints of 712 * potential misconfiguration. 713 */ 714 if (!bridged && isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) { 715 log(LOG_ERR, 716 "arp: %*D is using my IP address %s on %s!\n", 717 ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 718 inet_ntoa(isaddr), ifp->if_xname); 719 itaddr = myaddr; 720 goto reply; 721 } 722 if (ifp->if_flags & IFF_STATICARP) 723 goto reply; 724 /* 725 * We look for any FIB that has this address to find 726 * the interface etc. 727 * For sanity checks that are FIB independent we abort the loop. 728 */ 729 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) { 730 rt = arplookup(isaddr.s_addr, 731 itaddr.s_addr == myaddr.s_addr, 0, fibnum); 732 if (rt == NULL) 733 continue; 734 735 sdl = SDL(rt->rt_gateway); 736 /* Only call this once */ 737 if (firstpass) { 738 sin.sin_addr.s_addr = isaddr.s_addr; 739 EVENTHANDLER_INVOKE(route_arp_update_event, rt, 740 ar_sha(ah), (struct sockaddr *)&sin); 741 } 742 743 la = (struct llinfo_arp *)rt->rt_llinfo; 744 if (la == NULL) { 745 RT_UNLOCK(rt); 746 continue; 747 } 748 749 if (firstpass) { 750 /* The following is not an error when doing bridging. */ 751 if (!bridged && rt->rt_ifp != ifp 752 #ifdef DEV_CARP 753 && (ifp->if_type != IFT_CARP || !carp_match) 754 #endif 755 ) { 756 if (log_arp_wrong_iface) 757 log(LOG_ERR, "arp: %s is on %s " 758 "but got reply from %*D " 759 "on %s\n", 760 inet_ntoa(isaddr), 761 rt->rt_ifp->if_xname, 762 ifp->if_addrlen, 763 (u_char *)ar_sha(ah), ":", 764 ifp->if_xname); 765 RT_UNLOCK(rt); 766 break; 767 } 768 if (sdl->sdl_alen && 769 bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) { 770 if (rt->rt_expire) { 771 if (log_arp_movements) 772 log(LOG_INFO, 773 "arp: %s moved from %*D to %*D " 774 "on %s\n", 775 inet_ntoa(isaddr), 776 ifp->if_addrlen, 777 (u_char *)LLADDR(sdl), ":", 778 ifp->if_addrlen, 779 (u_char *)ar_sha(ah), ":", 780 ifp->if_xname); 781 } else { 782 RT_UNLOCK(rt); 783 if (log_arp_permanent_modify) 784 log(LOG_ERR, 785 "arp: %*D attempts to " 786 "modify permanent entry " 787 "for %s on %s\n", 788 ifp->if_addrlen, 789 (u_char *)ar_sha(ah), ":", 790 inet_ntoa(isaddr), 791 ifp->if_xname); 792 break; 793 } 794 } 795 /* 796 * sanity check for the address length. 797 * XXX this does not work for protocols 798 * with variable address length. -is 799 */ 800 if (sdl->sdl_alen && 801 sdl->sdl_alen != ah->ar_hln) { 802 log(LOG_WARNING, 803 "arp from %*D: new addr len %d, was %d", 804 ifp->if_addrlen, (u_char *) ar_sha(ah), 805 ":", ah->ar_hln, sdl->sdl_alen); 806 } 807 if (ifp->if_addrlen != ah->ar_hln) { 808 log(LOG_WARNING, 809 "arp from %*D: addr len: " 810 "new %d, i/f %d (ignored)", 811 ifp->if_addrlen, (u_char *) ar_sha(ah), 812 ":", ah->ar_hln, ifp->if_addrlen); 813 RT_UNLOCK(rt); 814 break; 815 } 816 firstpass = 0; 817 goodfib = fibnum; 818 } 819 820 /* Copy in the information received. */ 821 (void)memcpy(LLADDR(sdl), ar_sha(ah), 822 sdl->sdl_alen = ah->ar_hln); 823 /* 824 * If we receive an arp from a token-ring station over 825 * a token-ring nic then try to save the source routing info. 826 * XXXMRT Only minimal Token Ring support for MRT. 827 * Only do this on the first pass as if modifies the mbuf. 828 */ 829 if (ifp->if_type == IFT_ISO88025) { 830 struct iso88025_header *th = NULL; 831 struct iso88025_sockaddr_dl_data *trld; 832 833 /* force the fib loop to end after this pass */ 834 fibnum = rt_numfibs - 1; 835 836 th = (struct iso88025_header *)m->m_pkthdr.header; 837 trld = SDL_ISO88025(sdl); 838 rif_len = TR_RCF_RIFLEN(th->rcf); 839 if ((th->iso88025_shost[0] & TR_RII) && 840 (rif_len > 2)) { 841 trld->trld_rcf = th->rcf; 842 trld->trld_rcf ^= htons(TR_RCF_DIR); 843 memcpy(trld->trld_route, th->rd, rif_len - 2); 844 trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK); 845 /* 846 * Set up source routing information for 847 * reply packet (XXX) 848 */ 849 m->m_data -= rif_len; 850 m->m_len += rif_len; 851 m->m_pkthdr.len += rif_len; 852 } else { 853 th->iso88025_shost[0] &= ~TR_RII; 854 trld->trld_rcf = 0; 855 } 856 m->m_data -= 8; 857 m->m_len += 8; 858 m->m_pkthdr.len += 8; 859 th->rcf = trld->trld_rcf; 860 } 861 862 if (rt->rt_expire) { 863 rt->rt_expire = time_uptime + V_arpt_keep; 864 callout_reset(&la->la_timer, hz * V_arpt_keep, 865 arptimer, rt); 866 } 867 la->la_asked = 0; 868 la->la_preempt = V_arp_maxtries; 869 hold = la->la_hold; 870 la->la_hold = NULL; 871 RT_UNLOCK(rt); 872 if (hold != NULL) 873 (*ifp->if_output)(ifp, hold, rt_key(rt), rt); 874 } /* end of FIB loop */ 875 reply: 876 877 /* 878 * Decide if we have to respond to something. 879 */ 880 if (op != ARPOP_REQUEST) 881 goto drop; 882 if (itaddr.s_addr == myaddr.s_addr) { 883 /* Shortcut.. the receiving interface is the target. */ 884 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 885 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 886 } else { 887 /* It's not asking for our address. But it still may 888 * be something we should answer. 889 * 890 * XXX MRT 891 * We assume that link level info is independent of 892 * the table used and so we use whichever we can and don't 893 * have a better option. 894 */ 895 /* Have we been asked to proxy for the target. */ 896 rt = arplookup(itaddr.s_addr, 0, SIN_PROXY, goodfib); 897 if (rt == NULL) { 898 /* Nope, only intersted now if proxying everything. */ 899 struct sockaddr_in sin; 900 901 if (!V_arp_proxyall) 902 goto drop; 903 904 bzero(&sin, sizeof sin); 905 sin.sin_family = AF_INET; 906 sin.sin_len = sizeof sin; 907 sin.sin_addr = itaddr; 908 909 /* XXX MRT use table 0 for arp reply */ 910 rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0); 911 if (!rt) 912 goto drop; 913 /* 914 * Don't send proxies for nodes on the same interface 915 * as this one came out of, or we'll get into a fight 916 * over who claims what Ether address. 917 */ 918 if (rt->rt_ifp == ifp) { 919 rtfree(rt); 920 goto drop; 921 } 922 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 923 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 924 rtfree(rt); 925 926 /* 927 * Also check that the node which sent the ARP packet 928 * is on the the interface we expect it to be on. This 929 * avoids ARP chaos if an interface is connected to the 930 * wrong network. 931 */ 932 sin.sin_addr = isaddr; 933 934 /* XXX MRT use table 0 for arp checks */ 935 rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0); 936 if (!rt) 937 goto drop; 938 if (rt->rt_ifp != ifp) { 939 log(LOG_INFO, "arp_proxy: ignoring request" 940 " from %s via %s, expecting %s\n", 941 inet_ntoa(isaddr), ifp->if_xname, 942 rt->rt_ifp->if_xname); 943 rtfree(rt); 944 goto drop; 945 } 946 rtfree(rt); 947 948 #ifdef DEBUG_PROXY 949 printf("arp: proxying for %s\n", 950 inet_ntoa(itaddr)); 951 #endif 952 } else { 953 /* 954 * Return proxied ARP replies only on the interface 955 * or bridge cluster where this network resides. 956 * Otherwise we may conflict with the host we are 957 * proxying for. 958 */ 959 if (rt->rt_ifp != ifp && 960 (rt->rt_ifp->if_bridge != ifp->if_bridge || 961 ifp->if_bridge == NULL)) { 962 RT_UNLOCK(rt); 963 goto drop; 964 } 965 sdl = SDL(rt->rt_gateway); 966 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 967 (void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln); 968 RT_UNLOCK(rt); 969 } 970 } 971 972 if (itaddr.s_addr == myaddr.s_addr && 973 IN_LINKLOCAL(ntohl(itaddr.s_addr))) { 974 /* RFC 3927 link-local IPv4; always reply by broadcast. */ 975 #ifdef DEBUG_LINKLOCAL 976 printf("arp: sending reply for link-local addr %s\n", 977 inet_ntoa(itaddr)); 978 #endif 979 m->m_flags |= M_BCAST; 980 m->m_flags &= ~M_MCAST; 981 } else { 982 /* default behaviour; never reply by broadcast. */ 983 m->m_flags &= ~(M_BCAST|M_MCAST); 984 } 985 (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln); 986 (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln); 987 ah->ar_op = htons(ARPOP_REPLY); 988 ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 989 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln); 990 m->m_pkthdr.len = m->m_len; 991 sa.sa_family = AF_ARP; 992 sa.sa_len = 2; 993 (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 994 return; 995 996 drop: 997 m_freem(m); 998 } 999 #endif 1000 1001 /* 1002 * Lookup or enter a new address in arptab. 1003 */ 1004 static struct rtentry * 1005 arplookup(u_long addr, int create, int proxy, int fibnum) 1006 { 1007 struct rtentry *rt; 1008 struct sockaddr_inarp sin; 1009 const char *why = 0; 1010 1011 bzero(&sin, sizeof(sin)); 1012 sin.sin_len = sizeof(sin); 1013 sin.sin_family = AF_INET; 1014 sin.sin_addr.s_addr = addr; 1015 if (proxy) 1016 sin.sin_other = SIN_PROXY; 1017 rt = in_rtalloc1((struct sockaddr *)&sin, create, 0UL, fibnum); 1018 if (rt == 0) 1019 return (0); 1020 1021 if (rt->rt_flags & RTF_GATEWAY) 1022 why = "host is not on local network"; 1023 else if ((rt->rt_flags & RTF_LLINFO) == 0) 1024 why = "could not allocate llinfo"; 1025 else if (rt->rt_gateway->sa_family != AF_LINK) 1026 why = "gateway route is not ours"; 1027 1028 if (why) { 1029 #define ISDYNCLONE(_rt) \ 1030 (((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED) 1031 if (create) 1032 log(LOG_DEBUG, "arplookup %s failed: %s\n", 1033 inet_ntoa(sin.sin_addr), why); 1034 /* 1035 * If there are no references to this Layer 2 route, 1036 * and it is a cloned route, and not static, and 1037 * arplookup() is creating the route, then purge 1038 * it from the routing table as it is probably bogus. 1039 */ 1040 if (rt->rt_refcnt == 1 && ISDYNCLONE(rt)) 1041 rtexpunge(rt); 1042 RTFREE_LOCKED(rt); 1043 return (0); 1044 #undef ISDYNCLONE 1045 } else { 1046 RT_REMREF(rt); 1047 return (rt); 1048 } 1049 } 1050 1051 void 1052 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa) 1053 { 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, IF_LLADDR(ifp)); 1057 ifa->ifa_rtrequest = arp_rtrequest; 1058 ifa->ifa_flags |= RTF_CLONING; 1059 } 1060 1061 void 1062 arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr) 1063 { 1064 if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 1065 arprequest(ifp, &IA_SIN(ifa)->sin_addr, 1066 &IA_SIN(ifa)->sin_addr, enaddr); 1067 ifa->ifa_rtrequest = arp_rtrequest; 1068 ifa->ifa_flags |= RTF_CLONING; 1069 } 1070 1071 static void 1072 arp_init(void) 1073 { 1074 1075 arpintrq.ifq_maxlen = 50; 1076 mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF); 1077 netisr_register(NETISR_ARP, arpintr, &arpintrq, 0); 1078 } 1079 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0); 1080