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