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