1 /* $NetBSD: ip_carp.c,v 1.18 2007/10/19 12:16:46 ad Exp $ */ 2 /* $OpenBSD: ip_carp.c,v 1.113 2005/11/04 08:11:54 mcbride Exp $ */ 3 4 /* 5 * Copyright (c) 2002 Michael Shalayeff. All rights reserved. 6 * Copyright (c) 2003 Ryan McBride. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT, 21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 23 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 26 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 27 * THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 /* 31 * TODO: 32 * - iface reconfigure 33 * - support for hardware checksum calculations; 34 * 35 */ 36 37 #include <sys/param.h> 38 #include <sys/proc.h> 39 #include <sys/mbuf.h> 40 #include <sys/socket.h> 41 #include <sys/socketvar.h> 42 #include <sys/callout.h> 43 #include <sys/ioctl.h> 44 #include <sys/errno.h> 45 #include <sys/device.h> 46 #include <sys/time.h> 47 #include <sys/kernel.h> 48 #include <sys/kauth.h> 49 #include <sys/sysctl.h> 50 #include <sys/ucred.h> 51 #include <sys/syslog.h> 52 #include <sys/acct.h> 53 54 #include <sys/cpu.h> 55 56 #include <net/if.h> 57 #include <net/pfil.h> 58 #include <net/if_types.h> 59 #include <net/if_ether.h> 60 #include <net/route.h> 61 #include <net/netisr.h> 62 #include <netinet/if_inarp.h> 63 64 #include <machine/stdarg.h> 65 66 #if NFDDI > 0 67 #include <net/if_fddi.h> 68 #endif 69 #if NTOKEN > 0 70 #include <net/if_token.h> 71 #endif 72 73 #ifdef INET 74 #include <netinet/in.h> 75 #include <netinet/in_systm.h> 76 #include <netinet/in_var.h> 77 #include <netinet/ip.h> 78 #include <netinet/ip_var.h> 79 80 #include <net/if_dl.h> 81 #endif 82 83 #ifdef INET6 84 #include <netinet/icmp6.h> 85 #include <netinet/ip6.h> 86 #include <netinet6/ip6_var.h> 87 #include <netinet6/nd6.h> 88 #endif 89 90 #include "bpfilter.h" 91 #if NBPFILTER > 0 92 #include <net/bpf.h> 93 #endif 94 95 #include <sys/sha1.h> 96 97 #include <netinet/ip_carp.h> 98 99 struct carp_mc_entry { 100 LIST_ENTRY(carp_mc_entry) mc_entries; 101 union { 102 struct ether_multi *mcu_enm; 103 } mc_u; 104 struct sockaddr_storage mc_addr; 105 }; 106 #define mc_enm mc_u.mcu_enm 107 108 struct carp_softc { 109 struct ethercom sc_ac; 110 #define sc_if sc_ac.ec_if 111 #define sc_carpdev sc_ac.ec_if.if_carpdev 112 int ah_cookie; 113 int lh_cookie; 114 struct ip_moptions sc_imo; 115 #ifdef INET6 116 struct ip6_moptions sc_im6o; 117 #endif /* INET6 */ 118 TAILQ_ENTRY(carp_softc) sc_list; 119 120 enum { INIT = 0, BACKUP, MASTER } sc_state; 121 122 int sc_suppress; 123 int sc_bow_out; 124 125 int sc_sendad_errors; 126 #define CARP_SENDAD_MAX_ERRORS 3 127 int sc_sendad_success; 128 #define CARP_SENDAD_MIN_SUCCESS 3 129 130 int sc_vhid; 131 int sc_advskew; 132 int sc_naddrs; 133 int sc_naddrs6; 134 int sc_advbase; /* seconds */ 135 int sc_init_counter; 136 u_int64_t sc_counter; 137 138 /* authentication */ 139 #define CARP_HMAC_PAD 64 140 unsigned char sc_key[CARP_KEY_LEN]; 141 unsigned char sc_pad[CARP_HMAC_PAD]; 142 SHA1_CTX sc_sha1; 143 u_int32_t sc_hashkey[2]; 144 145 struct callout sc_ad_tmo; /* advertisement timeout */ 146 struct callout sc_md_tmo; /* master down timeout */ 147 struct callout sc_md6_tmo; /* master down timeout */ 148 149 LIST_HEAD(__carp_mchead, carp_mc_entry) carp_mc_listhead; 150 }; 151 152 int carp_suppress_preempt = 0; 153 int carp_opts[CARPCTL_MAXID] = { 0, 1, 0, 0, 0 }; /* XXX for now */ 154 struct carpstats carpstats; 155 156 struct carp_if { 157 TAILQ_HEAD(, carp_softc) vhif_vrs; 158 int vhif_nvrs; 159 160 struct ifnet *vhif_ifp; 161 }; 162 163 #define CARP_LOG(sc, s) \ 164 if (carp_opts[CARPCTL_LOG]) { \ 165 if (sc) \ 166 log(LOG_INFO, "%s: ", \ 167 (sc)->sc_if.if_xname); \ 168 else \ 169 log(LOG_INFO, "carp: "); \ 170 addlog s; \ 171 addlog("\n"); \ 172 } 173 174 void carp_hmac_prepare(struct carp_softc *); 175 void carp_hmac_generate(struct carp_softc *, u_int32_t *, 176 unsigned char *); 177 int carp_hmac_verify(struct carp_softc *, u_int32_t *, 178 unsigned char *); 179 void carp_setroute(struct carp_softc *, int); 180 void carp_proto_input_c(struct mbuf *, struct carp_header *, sa_family_t); 181 void carpattach(int); 182 void carpdetach(struct carp_softc *); 183 int carp_prepare_ad(struct mbuf *, struct carp_softc *, 184 struct carp_header *); 185 void carp_send_ad_all(void); 186 void carp_send_ad(void *); 187 void carp_send_arp(struct carp_softc *); 188 void carp_master_down(void *); 189 int carp_ioctl(struct ifnet *, u_long, void *); 190 void carp_start(struct ifnet *); 191 void carp_setrun(struct carp_softc *, sa_family_t); 192 void carp_set_state(struct carp_softc *, int); 193 int carp_addrcount(struct carp_if *, struct in_ifaddr *, int); 194 enum { CARP_COUNT_MASTER, CARP_COUNT_RUNNING }; 195 196 void carp_multicast_cleanup(struct carp_softc *); 197 int carp_set_ifp(struct carp_softc *, struct ifnet *); 198 void carp_set_enaddr(struct carp_softc *); 199 void carp_addr_updated(void *); 200 u_int32_t carp_hash(struct carp_softc *, u_char *); 201 int carp_set_addr(struct carp_softc *, struct sockaddr_in *); 202 int carp_join_multicast(struct carp_softc *); 203 #ifdef INET6 204 void carp_send_na(struct carp_softc *); 205 int carp_set_addr6(struct carp_softc *, struct sockaddr_in6 *); 206 int carp_join_multicast6(struct carp_softc *); 207 #endif 208 int carp_clone_create(struct if_clone *, int); 209 int carp_clone_destroy(struct ifnet *); 210 int carp_ether_addmulti(struct carp_softc *, struct ifreq *); 211 int carp_ether_delmulti(struct carp_softc *, struct ifreq *); 212 void carp_ether_purgemulti(struct carp_softc *); 213 214 struct if_clone carp_cloner = 215 IF_CLONE_INITIALIZER("carp", carp_clone_create, carp_clone_destroy); 216 217 static __inline u_int16_t 218 carp_cksum(struct mbuf *m, int len) 219 { 220 return (in_cksum(m, len)); 221 } 222 223 void 224 carp_hmac_prepare(struct carp_softc *sc) 225 { 226 u_int8_t carp_version = CARP_VERSION, type = CARP_ADVERTISEMENT; 227 u_int8_t vhid = sc->sc_vhid & 0xff; 228 SHA1_CTX sha1ctx; 229 u_int32_t kmd[5]; 230 struct ifaddr *ifa; 231 int i, found; 232 struct in_addr last, cur, in; 233 #ifdef INET6 234 struct in6_addr last6, cur6, in6; 235 #endif /* INET6 */ 236 237 /* compute ipad from key */ 238 bzero(sc->sc_pad, sizeof(sc->sc_pad)); 239 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key)); 240 for (i = 0; i < sizeof(sc->sc_pad); i++) 241 sc->sc_pad[i] ^= 0x36; 242 243 /* precompute first part of inner hash */ 244 SHA1Init(&sc->sc_sha1); 245 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad)); 246 SHA1Update(&sc->sc_sha1, (void *)&carp_version, sizeof(carp_version)); 247 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type)); 248 249 /* generate a key for the arpbalance hash, before the vhid is hashed */ 250 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx)); 251 SHA1Final((unsigned char *)kmd, &sha1ctx); 252 sc->sc_hashkey[0] = kmd[0] ^ kmd[1]; 253 sc->sc_hashkey[1] = kmd[2] ^ kmd[3]; 254 255 /* the rest of the precomputation */ 256 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid)); 257 258 /* Hash the addresses from smallest to largest, not interface order */ 259 #ifdef INET 260 cur.s_addr = 0; 261 do { 262 found = 0; 263 last = cur; 264 cur.s_addr = 0xffffffff; 265 TAILQ_FOREACH(ifa, &sc->sc_if.if_addrlist, ifa_list) { 266 in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr; 267 if (ifa->ifa_addr->sa_family == AF_INET && 268 ntohl(in.s_addr) > ntohl(last.s_addr) && 269 ntohl(in.s_addr) < ntohl(cur.s_addr)) { 270 cur.s_addr = in.s_addr; 271 found++; 272 } 273 } 274 if (found) 275 SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur)); 276 } while (found); 277 #endif /* INET */ 278 279 #ifdef INET6 280 memset(&cur6, 0x00, sizeof(cur6)); 281 do { 282 found = 0; 283 last6 = cur6; 284 memset(&cur6, 0xff, sizeof(cur6)); 285 TAILQ_FOREACH(ifa, &sc->sc_if.if_addrlist, ifa_list) { 286 in6 = ifatoia6(ifa)->ia_addr.sin6_addr; 287 if (IN6_IS_ADDR_LINKLOCAL(&in6)) 288 in6.s6_addr16[1] = 0; 289 if (ifa->ifa_addr->sa_family == AF_INET6 && 290 memcmp(&in6, &last6, sizeof(in6)) > 0 && 291 memcmp(&in6, &cur6, sizeof(in6)) < 0) { 292 cur6 = in6; 293 found++; 294 } 295 } 296 if (found) 297 SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6)); 298 } while (found); 299 #endif /* INET6 */ 300 301 /* convert ipad to opad */ 302 for (i = 0; i < sizeof(sc->sc_pad); i++) 303 sc->sc_pad[i] ^= 0x36 ^ 0x5c; 304 } 305 306 void 307 carp_hmac_generate(struct carp_softc *sc, u_int32_t counter[2], 308 unsigned char md[20]) 309 { 310 SHA1_CTX sha1ctx; 311 312 /* fetch first half of inner hash */ 313 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx)); 314 315 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter)); 316 SHA1Final(md, &sha1ctx); 317 318 /* outer hash */ 319 SHA1Init(&sha1ctx); 320 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad)); 321 SHA1Update(&sha1ctx, md, 20); 322 SHA1Final(md, &sha1ctx); 323 } 324 325 int 326 carp_hmac_verify(struct carp_softc *sc, u_int32_t counter[2], 327 unsigned char md[20]) 328 { 329 unsigned char md2[20]; 330 331 carp_hmac_generate(sc, counter, md2); 332 333 return (bcmp(md, md2, sizeof(md2))); 334 } 335 336 void 337 carp_setroute(struct carp_softc *sc, int cmd) 338 { 339 struct ifaddr *ifa; 340 int s; 341 342 s = splsoftnet(); 343 TAILQ_FOREACH(ifa, &sc->sc_if.if_addrlist, ifa_list) { 344 switch (ifa->ifa_addr->sa_family) { 345 case AF_INET: { 346 int count = 0; 347 struct rtentry *rt; 348 int hr_otherif, nr_ourif; 349 350 /* 351 * Avoid screwing with the routes if there are other 352 * carp interfaces which are master and have the same 353 * address. 354 */ 355 if (sc->sc_carpdev != NULL && 356 sc->sc_carpdev->if_carp != NULL) { 357 count = carp_addrcount( 358 (struct carp_if *)sc->sc_carpdev->if_carp, 359 ifatoia(ifa), CARP_COUNT_MASTER); 360 if ((cmd == RTM_ADD && count != 1) || 361 (cmd == RTM_DELETE && count != 0)) 362 continue; 363 } 364 365 /* Remove the existing host route, if any */ 366 rtrequest(RTM_DELETE, ifa->ifa_addr, 367 ifa->ifa_addr, ifa->ifa_netmask, 368 RTF_HOST, NULL); 369 370 rt = NULL; 371 (void)rtrequest(RTM_GET, ifa->ifa_addr, ifa->ifa_addr, 372 ifa->ifa_netmask, RTF_HOST, &rt); 373 hr_otherif = (rt && rt->rt_ifp != &sc->sc_if && 374 rt->rt_flags & (RTF_CLONING|RTF_CLONED)); 375 if (rt != NULL) { 376 RTFREE(rt); 377 rt = NULL; 378 } 379 380 /* Check for a network route on our interface */ 381 382 rt = NULL; 383 (void)rtrequest(RTM_GET, ifa->ifa_addr, ifa->ifa_addr, 384 ifa->ifa_netmask, 0, &rt); 385 nr_ourif = (rt && rt->rt_ifp == &sc->sc_if); 386 387 switch (cmd) { 388 case RTM_ADD: 389 if (hr_otherif) { 390 ifa->ifa_rtrequest = NULL; 391 ifa->ifa_flags &= ~RTF_CLONING; 392 393 rtrequest(RTM_ADD, ifa->ifa_addr, 394 ifa->ifa_addr, ifa->ifa_netmask, 395 RTF_UP | RTF_HOST, NULL); 396 } 397 if (!hr_otherif || nr_ourif || !rt) { 398 if (nr_ourif && !(rt->rt_flags & 399 RTF_CLONING)) 400 rtrequest(RTM_DELETE, 401 ifa->ifa_addr, 402 ifa->ifa_addr, 403 ifa->ifa_netmask, 0, NULL); 404 405 ifa->ifa_rtrequest = arp_rtrequest; 406 ifa->ifa_flags |= RTF_CLONING; 407 408 if (rtrequest(RTM_ADD, ifa->ifa_addr, 409 ifa->ifa_addr, ifa->ifa_netmask, 0, 410 NULL) == 0) 411 ifa->ifa_flags |= IFA_ROUTE; 412 } 413 break; 414 case RTM_DELETE: 415 break; 416 default: 417 break; 418 } 419 if (rt != NULL) { 420 RTFREE(rt); 421 rt = NULL; 422 } 423 break; 424 } 425 426 #ifdef INET6 427 case AF_INET6: 428 if (cmd == RTM_ADD) 429 in6_ifaddloop(ifa); 430 else 431 in6_ifremloop(ifa); 432 break; 433 #endif /* INET6 */ 434 default: 435 break; 436 } 437 } 438 splx(s); 439 } 440 441 /* 442 * process input packet. 443 * we have rearranged checks order compared to the rfc, 444 * but it seems more efficient this way or not possible otherwise. 445 */ 446 void 447 carp_proto_input(struct mbuf *m, ...) 448 { 449 struct ip *ip = mtod(m, struct ip *); 450 struct carp_softc *sc = NULL; 451 struct carp_header *ch; 452 int iplen, len, hlen; 453 va_list ap; 454 455 va_start(ap, m); 456 hlen = va_arg(ap, int); 457 va_end(ap); 458 459 carpstats.carps_ipackets++; 460 461 if (!carp_opts[CARPCTL_ALLOW]) { 462 m_freem(m); 463 return; 464 } 465 466 /* check if received on a valid carp interface */ 467 if (m->m_pkthdr.rcvif->if_type != IFT_CARP) { 468 carpstats.carps_badif++; 469 CARP_LOG(sc, ("packet received on non-carp interface: %s", 470 m->m_pkthdr.rcvif->if_xname)); 471 m_freem(m); 472 return; 473 } 474 475 /* verify that the IP TTL is 255. */ 476 if (ip->ip_ttl != CARP_DFLTTL) { 477 carpstats.carps_badttl++; 478 CARP_LOG(sc, ("received ttl %d != %d on %s", ip->ip_ttl, 479 CARP_DFLTTL, m->m_pkthdr.rcvif->if_xname)); 480 m_freem(m); 481 return; 482 } 483 484 /* 485 * verify that the received packet length is 486 * equal to the CARP header 487 */ 488 iplen = ip->ip_hl << 2; 489 len = iplen + sizeof(*ch); 490 if (len > m->m_pkthdr.len) { 491 carpstats.carps_badlen++; 492 CARP_LOG(sc, ("packet too short %d on %s", m->m_pkthdr.len, 493 m->m_pkthdr.rcvif->if_xname)); 494 m_freem(m); 495 return; 496 } 497 498 if ((m = m_pullup(m, len)) == NULL) { 499 carpstats.carps_hdrops++; 500 return; 501 } 502 ip = mtod(m, struct ip *); 503 ch = (struct carp_header *)((char *)ip + iplen); 504 /* verify the CARP checksum */ 505 m->m_data += iplen; 506 if (carp_cksum(m, len - iplen)) { 507 carpstats.carps_badsum++; 508 CARP_LOG(sc, ("checksum failed on %s", 509 m->m_pkthdr.rcvif->if_xname)); 510 m_freem(m); 511 return; 512 } 513 m->m_data -= iplen; 514 515 carp_proto_input_c(m, ch, AF_INET); 516 } 517 518 #ifdef INET6 519 int 520 carp6_proto_input(struct mbuf **mp, int *offp, int proto) 521 { 522 struct mbuf *m = *mp; 523 struct carp_softc *sc = NULL; 524 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 525 struct carp_header *ch; 526 u_int len; 527 528 carpstats.carps_ipackets6++; 529 530 if (!carp_opts[CARPCTL_ALLOW]) { 531 m_freem(m); 532 return (IPPROTO_DONE); 533 } 534 535 /* check if received on a valid carp interface */ 536 if (m->m_pkthdr.rcvif->if_type != IFT_CARP) { 537 carpstats.carps_badif++; 538 CARP_LOG(sc, ("packet received on non-carp interface: %s", 539 m->m_pkthdr.rcvif->if_xname)); 540 m_freem(m); 541 return (IPPROTO_DONE); 542 } 543 544 /* verify that the IP TTL is 255 */ 545 if (ip6->ip6_hlim != CARP_DFLTTL) { 546 carpstats.carps_badttl++; 547 CARP_LOG(sc, ("received ttl %d != %d on %s", ip6->ip6_hlim, 548 CARP_DFLTTL, m->m_pkthdr.rcvif->if_xname)); 549 m_freem(m); 550 return (IPPROTO_DONE); 551 } 552 553 /* verify that we have a complete carp packet */ 554 len = m->m_len; 555 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch)); 556 if (ch == NULL) { 557 carpstats.carps_badlen++; 558 CARP_LOG(sc, ("packet size %u too small", len)); 559 return (IPPROTO_DONE); 560 } 561 562 563 /* verify the CARP checksum */ 564 m->m_data += *offp; 565 if (carp_cksum(m, sizeof(*ch))) { 566 carpstats.carps_badsum++; 567 CARP_LOG(sc, ("checksum failed, on %s", 568 m->m_pkthdr.rcvif->if_xname)); 569 m_freem(m); 570 return (IPPROTO_DONE); 571 } 572 m->m_data -= *offp; 573 574 carp_proto_input_c(m, ch, AF_INET6); 575 return (IPPROTO_DONE); 576 } 577 #endif /* INET6 */ 578 579 void 580 carp_proto_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af) 581 { 582 struct carp_softc *sc; 583 u_int64_t tmp_counter; 584 struct timeval sc_tv, ch_tv; 585 586 TAILQ_FOREACH(sc, &((struct carp_if *) 587 m->m_pkthdr.rcvif->if_carpdev->if_carp)->vhif_vrs, sc_list) 588 if (sc->sc_vhid == ch->carp_vhid) 589 break; 590 591 if (!sc || (sc->sc_if.if_flags & (IFF_UP|IFF_RUNNING)) != 592 (IFF_UP|IFF_RUNNING)) { 593 carpstats.carps_badvhid++; 594 m_freem(m); 595 return; 596 } 597 598 /* 599 * Check if our own advertisement was duplicated 600 * from a non simplex interface. 601 * XXX If there is no address on our physical interface 602 * there is no way to distinguish our ads from the ones 603 * another carp host might have sent us. 604 */ 605 if ((sc->sc_carpdev->if_flags & IFF_SIMPLEX) == 0) { 606 struct sockaddr sa; 607 struct ifaddr *ifa; 608 609 bzero(&sa, sizeof(sa)); 610 sa.sa_family = af; 611 ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev); 612 613 if (ifa && af == AF_INET) { 614 struct ip *ip = mtod(m, struct ip *); 615 if (ip->ip_src.s_addr == 616 ifatoia(ifa)->ia_addr.sin_addr.s_addr) { 617 m_freem(m); 618 return; 619 } 620 } 621 #ifdef INET6 622 if (ifa && af == AF_INET6) { 623 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 624 struct in6_addr in6_src, in6_found; 625 626 in6_src = ip6->ip6_src; 627 in6_found = ifatoia6(ifa)->ia_addr.sin6_addr; 628 if (IN6_IS_ADDR_LINKLOCAL(&in6_src)) 629 in6_src.s6_addr16[1] = 0; 630 if (IN6_IS_ADDR_LINKLOCAL(&in6_found)) 631 in6_found.s6_addr16[1] = 0; 632 if (IN6_ARE_ADDR_EQUAL(&in6_src, &in6_found)) { 633 m_freem(m); 634 return; 635 } 636 } 637 #endif /* INET6 */ 638 } 639 640 microtime(&sc->sc_if.if_lastchange); 641 sc->sc_if.if_ipackets++; 642 sc->sc_if.if_ibytes += m->m_pkthdr.len; 643 644 /* verify the CARP version. */ 645 if (ch->carp_version != CARP_VERSION) { 646 carpstats.carps_badver++; 647 sc->sc_if.if_ierrors++; 648 CARP_LOG(sc, ("invalid version %d != %d", 649 ch->carp_version, CARP_VERSION)); 650 m_freem(m); 651 return; 652 } 653 654 /* verify the hash */ 655 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) { 656 carpstats.carps_badauth++; 657 sc->sc_if.if_ierrors++; 658 CARP_LOG(sc, ("incorrect hash")); 659 m_freem(m); 660 return; 661 } 662 663 tmp_counter = ntohl(ch->carp_counter[0]); 664 tmp_counter = tmp_counter<<32; 665 tmp_counter += ntohl(ch->carp_counter[1]); 666 667 /* XXX Replay protection goes here */ 668 669 sc->sc_init_counter = 0; 670 sc->sc_counter = tmp_counter; 671 672 673 sc_tv.tv_sec = sc->sc_advbase; 674 if (carp_suppress_preempt && sc->sc_advskew < 240) 675 sc_tv.tv_usec = 240 * 1000000 / 256; 676 else 677 sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256; 678 ch_tv.tv_sec = ch->carp_advbase; 679 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256; 680 681 switch (sc->sc_state) { 682 case INIT: 683 break; 684 case MASTER: 685 /* 686 * If we receive an advertisement from a backup who's going to 687 * be more frequent than us, go into BACKUP state. 688 */ 689 if (timercmp(&sc_tv, &ch_tv, >) || 690 timercmp(&sc_tv, &ch_tv, ==)) { 691 callout_stop(&sc->sc_ad_tmo); 692 carp_set_state(sc, BACKUP); 693 carp_setrun(sc, 0); 694 carp_setroute(sc, RTM_DELETE); 695 } 696 break; 697 case BACKUP: 698 /* 699 * If we're pre-empting masters who advertise slower than us, 700 * and this one claims to be slower, treat him as down. 701 */ 702 if (carp_opts[CARPCTL_PREEMPT] && timercmp(&sc_tv, &ch_tv, <)) { 703 carp_master_down(sc); 704 break; 705 } 706 707 /* 708 * If the master is going to advertise at such a low frequency 709 * that he's guaranteed to time out, we'd might as well just 710 * treat him as timed out now. 711 */ 712 sc_tv.tv_sec = sc->sc_advbase * 3; 713 if (timercmp(&sc_tv, &ch_tv, <)) { 714 carp_master_down(sc); 715 break; 716 } 717 718 /* 719 * Otherwise, we reset the counter and wait for the next 720 * advertisement. 721 */ 722 carp_setrun(sc, af); 723 break; 724 } 725 726 m_freem(m); 727 return; 728 } 729 730 /* 731 * Interface side of the CARP implementation. 732 */ 733 734 /* ARGSUSED */ 735 void 736 carpattach(int n) 737 { 738 if_clone_attach(&carp_cloner); 739 } 740 741 int 742 carp_clone_create(struct if_clone *ifc, int unit) 743 { 744 extern int ifqmaxlen; 745 struct carp_softc *sc; 746 struct ifnet *ifp; 747 748 sc = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT); 749 if (!sc) 750 return (ENOMEM); 751 bzero(sc, sizeof(*sc)); 752 753 sc->sc_suppress = 0; 754 sc->sc_advbase = CARP_DFLTINTV; 755 sc->sc_vhid = -1; /* required setting */ 756 sc->sc_advskew = 0; 757 sc->sc_init_counter = 1; 758 sc->sc_naddrs = sc->sc_naddrs6 = 0; 759 #ifdef INET6 760 sc->sc_im6o.im6o_multicast_hlim = CARP_DFLTTL; 761 #endif /* INET6 */ 762 763 callout_init(&sc->sc_ad_tmo, 0); 764 callout_init(&sc->sc_md_tmo, 0); 765 callout_init(&sc->sc_md6_tmo, 0); 766 767 callout_setfunc(&sc->sc_ad_tmo, carp_send_ad, sc); 768 callout_setfunc(&sc->sc_md_tmo, carp_master_down, sc); 769 callout_setfunc(&sc->sc_md6_tmo, carp_master_down, sc); 770 771 LIST_INIT(&sc->carp_mc_listhead); 772 ifp = &sc->sc_if; 773 ifp->if_softc = sc; 774 snprintf(ifp->if_xname, sizeof ifp->if_xname, "%s%d", ifc->ifc_name, 775 unit); 776 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 777 ifp->if_ioctl = carp_ioctl; 778 ifp->if_start = carp_start; 779 ifp->if_output = carp_output; 780 ifp->if_type = IFT_CARP; 781 ifp->if_addrlen = ETHER_ADDR_LEN; 782 ifp->if_hdrlen = ETHER_HDR_LEN; 783 ifp->if_mtu = ETHERMTU; 784 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 785 IFQ_SET_READY(&ifp->if_snd); 786 if_attach(ifp); 787 788 if_alloc_sadl(ifp); 789 ifp->if_broadcastaddr = etherbroadcastaddr; 790 carp_set_enaddr(sc); 791 LIST_INIT(&sc->sc_ac.ec_multiaddrs); 792 #if NBPFILTER > 0 793 bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN); 794 #endif 795 return (0); 796 } 797 798 int 799 carp_clone_destroy(struct ifnet *ifp) 800 { 801 struct carp_softc *sc = ifp->if_softc; 802 803 carpdetach(ifp->if_softc); 804 ether_ifdetach(ifp); 805 if_detach(ifp); 806 callout_destroy(&sc->sc_ad_tmo); 807 callout_destroy(&sc->sc_md_tmo); 808 callout_destroy(&sc->sc_md6_tmo); 809 free(ifp->if_softc, M_DEVBUF); 810 811 return (0); 812 } 813 814 void 815 carpdetach(struct carp_softc *sc) 816 { 817 struct carp_if *cif; 818 int s; 819 820 callout_stop(&sc->sc_ad_tmo); 821 callout_stop(&sc->sc_md_tmo); 822 callout_stop(&sc->sc_md6_tmo); 823 824 if (sc->sc_suppress) 825 carp_suppress_preempt--; 826 sc->sc_suppress = 0; 827 828 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) 829 carp_suppress_preempt--; 830 sc->sc_sendad_errors = 0; 831 832 carp_set_state(sc, INIT); 833 sc->sc_if.if_flags &= ~IFF_UP; 834 carp_setrun(sc, 0); 835 carp_multicast_cleanup(sc); 836 837 s = splnet(); 838 if (sc->sc_carpdev != NULL) { 839 /* XXX linkstatehook removal */ 840 cif = (struct carp_if *)sc->sc_carpdev->if_carp; 841 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list); 842 if (!--cif->vhif_nvrs) { 843 ifpromisc(sc->sc_carpdev, 0); 844 sc->sc_carpdev->if_carp = NULL; 845 FREE(cif, M_IFADDR); 846 } 847 } 848 sc->sc_carpdev = NULL; 849 splx(s); 850 } 851 852 /* Detach an interface from the carp. */ 853 void 854 carp_ifdetach(struct ifnet *ifp) 855 { 856 struct carp_softc *sc, *nextsc; 857 struct carp_if *cif = (struct carp_if *)ifp->if_carp; 858 859 for (sc = TAILQ_FIRST(&cif->vhif_vrs); sc; sc = nextsc) { 860 nextsc = TAILQ_NEXT(sc, sc_list); 861 carpdetach(sc); 862 } 863 } 864 865 int 866 carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, 867 struct carp_header *ch) 868 { 869 if (sc->sc_init_counter) { 870 /* this could also be seconds since unix epoch */ 871 sc->sc_counter = arc4random(); 872 sc->sc_counter = sc->sc_counter << 32; 873 sc->sc_counter += arc4random(); 874 } else 875 sc->sc_counter++; 876 877 ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff); 878 ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff); 879 880 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md); 881 882 return (0); 883 } 884 885 void 886 carp_send_ad_all(void) 887 { 888 struct ifnet *ifp; 889 struct carp_if *cif; 890 struct carp_softc *vh; 891 892 TAILQ_FOREACH(ifp, &ifnet, if_list) { 893 if (ifp->if_carp == NULL || ifp->if_type == IFT_CARP) 894 continue; 895 896 cif = (struct carp_if *)ifp->if_carp; 897 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) { 898 if ((vh->sc_if.if_flags & (IFF_UP|IFF_RUNNING)) == 899 (IFF_UP|IFF_RUNNING) && vh->sc_state == MASTER) 900 carp_send_ad(vh); 901 } 902 } 903 } 904 905 906 void 907 carp_send_ad(void *v) 908 { 909 struct carp_header ch; 910 struct timeval tv; 911 struct carp_softc *sc = v; 912 struct carp_header *ch_ptr; 913 struct mbuf *m; 914 int error, len, advbase, advskew, s; 915 struct ifaddr *ifa; 916 struct sockaddr sa; 917 918 s = splsoftnet(); 919 920 advbase = advskew = 0; /* Sssssh compiler */ 921 if (sc->sc_carpdev == NULL) { 922 sc->sc_if.if_oerrors++; 923 goto retry_later; 924 } 925 926 /* bow out if we've gone to backup (the carp interface is going down) */ 927 if (sc->sc_bow_out) { 928 sc->sc_bow_out = 0; 929 advbase = 255; 930 advskew = 255; 931 } else { 932 advbase = sc->sc_advbase; 933 if (!carp_suppress_preempt || sc->sc_advskew > 240) 934 advskew = sc->sc_advskew; 935 else 936 advskew = 240; 937 tv.tv_sec = advbase; 938 tv.tv_usec = advskew * 1000000 / 256; 939 } 940 941 ch.carp_version = CARP_VERSION; 942 ch.carp_type = CARP_ADVERTISEMENT; 943 ch.carp_vhid = sc->sc_vhid; 944 ch.carp_advbase = advbase; 945 ch.carp_advskew = advskew; 946 ch.carp_authlen = 7; /* XXX DEFINE */ 947 ch.carp_pad1 = 0; /* must be zero */ 948 ch.carp_cksum = 0; 949 950 951 #ifdef INET 952 if (sc->sc_naddrs) { 953 struct ip *ip; 954 955 MGETHDR(m, M_DONTWAIT, MT_HEADER); 956 if (m == NULL) { 957 sc->sc_if.if_oerrors++; 958 carpstats.carps_onomem++; 959 /* XXX maybe less ? */ 960 goto retry_later; 961 } 962 len = sizeof(*ip) + sizeof(ch); 963 m->m_pkthdr.len = len; 964 m->m_pkthdr.rcvif = NULL; 965 m->m_len = len; 966 MH_ALIGN(m, m->m_len); 967 m->m_flags |= M_MCAST; 968 ip = mtod(m, struct ip *); 969 ip->ip_v = IPVERSION; 970 ip->ip_hl = sizeof(*ip) >> 2; 971 ip->ip_tos = IPTOS_LOWDELAY; 972 ip->ip_len = htons(len); 973 ip->ip_id = htons(ip_randomid()); 974 ip->ip_off = htons(IP_DF); 975 ip->ip_ttl = CARP_DFLTTL; 976 ip->ip_p = IPPROTO_CARP; 977 ip->ip_sum = 0; 978 979 bzero(&sa, sizeof(sa)); 980 sa.sa_family = AF_INET; 981 ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev); 982 if (ifa == NULL) 983 ip->ip_src.s_addr = 0; 984 else 985 ip->ip_src.s_addr = 986 ifatoia(ifa)->ia_addr.sin_addr.s_addr; 987 ip->ip_dst.s_addr = INADDR_CARP_GROUP; 988 989 ch_ptr = (struct carp_header *)(&ip[1]); 990 bcopy(&ch, ch_ptr, sizeof(ch)); 991 if (carp_prepare_ad(m, sc, ch_ptr)) 992 goto retry_later; 993 994 m->m_data += sizeof(*ip); 995 ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip)); 996 m->m_data -= sizeof(*ip); 997 998 microtime(&sc->sc_if.if_lastchange); 999 sc->sc_if.if_opackets++; 1000 sc->sc_if.if_obytes += len; 1001 carpstats.carps_opackets++; 1002 1003 error = ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, 1004 NULL); 1005 if (error) { 1006 if (error == ENOBUFS) 1007 carpstats.carps_onomem++; 1008 else 1009 CARP_LOG(sc, ("ip_output failed: %d", error)); 1010 sc->sc_if.if_oerrors++; 1011 if (sc->sc_sendad_errors < INT_MAX) 1012 sc->sc_sendad_errors++; 1013 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) { 1014 carp_suppress_preempt++; 1015 if (carp_suppress_preempt == 1) 1016 carp_send_ad_all(); 1017 } 1018 sc->sc_sendad_success = 0; 1019 } else { 1020 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) { 1021 if (++sc->sc_sendad_success >= 1022 CARP_SENDAD_MIN_SUCCESS) { 1023 carp_suppress_preempt--; 1024 sc->sc_sendad_errors = 0; 1025 } 1026 } else 1027 sc->sc_sendad_errors = 0; 1028 } 1029 } 1030 #endif /* INET */ 1031 #ifdef INET6 1032 if (sc->sc_naddrs6) { 1033 struct ip6_hdr *ip6; 1034 1035 MGETHDR(m, M_DONTWAIT, MT_HEADER); 1036 if (m == NULL) { 1037 sc->sc_if.if_oerrors++; 1038 carpstats.carps_onomem++; 1039 /* XXX maybe less ? */ 1040 goto retry_later; 1041 } 1042 len = sizeof(*ip6) + sizeof(ch); 1043 m->m_pkthdr.len = len; 1044 m->m_pkthdr.rcvif = NULL; 1045 m->m_len = len; 1046 MH_ALIGN(m, m->m_len); 1047 m->m_flags |= M_MCAST; 1048 ip6 = mtod(m, struct ip6_hdr *); 1049 bzero(ip6, sizeof(*ip6)); 1050 ip6->ip6_vfc |= IPV6_VERSION; 1051 ip6->ip6_hlim = CARP_DFLTTL; 1052 ip6->ip6_nxt = IPPROTO_CARP; 1053 1054 /* set the source address */ 1055 bzero(&sa, sizeof(sa)); 1056 sa.sa_family = AF_INET6; 1057 ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev); 1058 if (ifa == NULL) /* This should never happen with IPv6 */ 1059 bzero(&ip6->ip6_src, sizeof(struct in6_addr)); 1060 else 1061 bcopy(ifatoia6(ifa)->ia_addr.sin6_addr.s6_addr, 1062 &ip6->ip6_src, sizeof(struct in6_addr)); 1063 /* set the multicast destination */ 1064 1065 ip6->ip6_dst.s6_addr8[0] = 0xff; 1066 ip6->ip6_dst.s6_addr8[1] = 0x02; 1067 ip6->ip6_dst.s6_addr8[15] = 0x12; 1068 1069 ch_ptr = (struct carp_header *)(&ip6[1]); 1070 bcopy(&ch, ch_ptr, sizeof(ch)); 1071 if (carp_prepare_ad(m, sc, ch_ptr)) 1072 goto retry_later; 1073 1074 m->m_data += sizeof(*ip6); 1075 ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip6)); 1076 m->m_data -= sizeof(*ip6); 1077 1078 microtime(&sc->sc_if.if_lastchange); 1079 sc->sc_if.if_opackets++; 1080 sc->sc_if.if_obytes += len; 1081 carpstats.carps_opackets6++; 1082 1083 error = ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL); 1084 if (error) { 1085 if (error == ENOBUFS) 1086 carpstats.carps_onomem++; 1087 else 1088 CARP_LOG(sc, ("ip6_output failed: %d", error)); 1089 sc->sc_if.if_oerrors++; 1090 if (sc->sc_sendad_errors < INT_MAX) 1091 sc->sc_sendad_errors++; 1092 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) { 1093 carp_suppress_preempt++; 1094 if (carp_suppress_preempt == 1) 1095 carp_send_ad_all(); 1096 } 1097 sc->sc_sendad_success = 0; 1098 } else { 1099 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) { 1100 if (++sc->sc_sendad_success >= 1101 CARP_SENDAD_MIN_SUCCESS) { 1102 carp_suppress_preempt--; 1103 sc->sc_sendad_errors = 0; 1104 } 1105 } else 1106 sc->sc_sendad_errors = 0; 1107 } 1108 } 1109 #endif /* INET6 */ 1110 1111 retry_later: 1112 splx(s); 1113 if (advbase != 255 || advskew != 255) 1114 callout_schedule(&sc->sc_ad_tmo, tvtohz(&tv)); 1115 } 1116 1117 /* 1118 * Broadcast a gratuitous ARP request containing 1119 * the virtual router MAC address for each IP address 1120 * associated with the virtual router. 1121 */ 1122 void 1123 carp_send_arp(struct carp_softc *sc) 1124 { 1125 struct ifaddr *ifa; 1126 struct in_addr *in; 1127 int s = splsoftnet(); 1128 1129 TAILQ_FOREACH(ifa, &sc->sc_if.if_addrlist, ifa_list) { 1130 1131 if (ifa->ifa_addr->sa_family != AF_INET) 1132 continue; 1133 1134 in = &ifatoia(ifa)->ia_addr.sin_addr; 1135 arprequest(sc->sc_carpdev, in, in, CLLADDR(sc->sc_if.if_sadl)); 1136 DELAY(1000); /* XXX */ 1137 } 1138 splx(s); 1139 } 1140 1141 #ifdef INET6 1142 void 1143 carp_send_na(struct carp_softc *sc) 1144 { 1145 struct ifaddr *ifa; 1146 struct in6_addr *in6; 1147 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT; 1148 int s = splsoftnet(); 1149 1150 TAILQ_FOREACH(ifa, &sc->sc_if.if_addrlist, ifa_list) { 1151 1152 if (ifa->ifa_addr->sa_family != AF_INET6) 1153 continue; 1154 1155 in6 = &ifatoia6(ifa)->ia_addr.sin6_addr; 1156 nd6_na_output(sc->sc_carpdev, &mcast, in6, 1157 ND_NA_FLAG_OVERRIDE, 1, NULL); 1158 DELAY(1000); /* XXX */ 1159 } 1160 splx(s); 1161 } 1162 #endif /* INET6 */ 1163 1164 /* 1165 * Based on bridge_hash() in if_bridge.c 1166 */ 1167 #define mix(a,b,c) \ 1168 do { \ 1169 a -= b; a -= c; a ^= (c >> 13); \ 1170 b -= c; b -= a; b ^= (a << 8); \ 1171 c -= a; c -= b; c ^= (b >> 13); \ 1172 a -= b; a -= c; a ^= (c >> 12); \ 1173 b -= c; b -= a; b ^= (a << 16); \ 1174 c -= a; c -= b; c ^= (b >> 5); \ 1175 a -= b; a -= c; a ^= (c >> 3); \ 1176 b -= c; b -= a; b ^= (a << 10); \ 1177 c -= a; c -= b; c ^= (b >> 15); \ 1178 } while (0) 1179 1180 u_int32_t 1181 carp_hash(struct carp_softc *sc, u_char *src) 1182 { 1183 u_int32_t a = 0x9e3779b9, b = sc->sc_hashkey[0], c = sc->sc_hashkey[1]; 1184 1185 c += sc->sc_key[3] << 24; 1186 c += sc->sc_key[2] << 16; 1187 c += sc->sc_key[1] << 8; 1188 c += sc->sc_key[0]; 1189 b += src[5] << 8; 1190 b += src[4]; 1191 a += src[3] << 24; 1192 a += src[2] << 16; 1193 a += src[1] << 8; 1194 a += src[0]; 1195 1196 mix(a, b, c); 1197 return (c); 1198 } 1199 1200 int 1201 carp_addrcount(struct carp_if *cif, struct in_ifaddr *ia, int type) 1202 { 1203 struct carp_softc *vh; 1204 struct ifaddr *ifa; 1205 int count = 0; 1206 1207 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) { 1208 if ((type == CARP_COUNT_RUNNING && 1209 (vh->sc_if.if_flags & (IFF_UP|IFF_RUNNING)) == 1210 (IFF_UP|IFF_RUNNING)) || 1211 (type == CARP_COUNT_MASTER && vh->sc_state == MASTER)) { 1212 TAILQ_FOREACH(ifa, &vh->sc_if.if_addrlist, ifa_list) { 1213 if (ifa->ifa_addr->sa_family == AF_INET && 1214 ia->ia_addr.sin_addr.s_addr == 1215 ifatoia(ifa)->ia_addr.sin_addr.s_addr) 1216 count++; 1217 } 1218 } 1219 } 1220 return (count); 1221 } 1222 1223 int 1224 carp_iamatch(struct in_ifaddr *ia, u_char *src, 1225 u_int32_t *count, u_int32_t index) 1226 { 1227 struct carp_softc *sc = ia->ia_ifp->if_softc; 1228 1229 if (carp_opts[CARPCTL_ARPBALANCE]) { 1230 /* 1231 * We use the source ip to decide which virtual host should 1232 * handle the request. If we're master of that virtual host, 1233 * then we respond, otherwise, just drop the arp packet on 1234 * the floor. 1235 */ 1236 1237 /* Count the elegible carp interfaces with this address */ 1238 if (*count == 0) 1239 *count = carp_addrcount( 1240 (struct carp_if *)ia->ia_ifp->if_carpdev->if_carp, 1241 ia, CARP_COUNT_RUNNING); 1242 1243 /* This should never happen, but... */ 1244 if (*count == 0) 1245 return (0); 1246 1247 if (carp_hash(sc, src) % *count == index - 1 && 1248 sc->sc_state == MASTER) { 1249 return (1); 1250 } 1251 } else { 1252 if (sc->sc_state == MASTER) 1253 return (1); 1254 } 1255 1256 return (0); 1257 } 1258 1259 #ifdef INET6 1260 struct ifaddr * 1261 carp_iamatch6(void *v, struct in6_addr *taddr) 1262 { 1263 struct carp_if *cif = v; 1264 struct carp_softc *vh; 1265 struct ifaddr *ifa; 1266 1267 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) { 1268 TAILQ_FOREACH(ifa, &vh->sc_if.if_addrlist, ifa_list) { 1269 if (IN6_ARE_ADDR_EQUAL(taddr, 1270 &ifatoia6(ifa)->ia_addr.sin6_addr) && 1271 ((vh->sc_if.if_flags & (IFF_UP|IFF_RUNNING)) == 1272 (IFF_UP|IFF_RUNNING)) && vh->sc_state == MASTER) 1273 return (ifa); 1274 } 1275 } 1276 1277 return (NULL); 1278 } 1279 #endif /* INET6 */ 1280 1281 struct ifnet * 1282 carp_ourether(void *v, struct ether_header *eh, u_char iftype, int src) 1283 { 1284 struct carp_if *cif = (struct carp_if *)v; 1285 struct carp_softc *vh; 1286 u_int8_t *ena; 1287 1288 if (src) 1289 ena = (u_int8_t *)&eh->ether_shost; 1290 else 1291 ena = (u_int8_t *)&eh->ether_dhost; 1292 1293 switch (iftype) { 1294 case IFT_ETHER: 1295 case IFT_FDDI: 1296 if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1) 1297 return (NULL); 1298 break; 1299 case IFT_ISO88025: 1300 if (ena[0] != 3 || ena[1] || ena[4] || ena[5]) 1301 return (NULL); 1302 break; 1303 default: 1304 return (NULL); 1305 break; 1306 } 1307 1308 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) 1309 if ((vh->sc_if.if_flags & (IFF_UP|IFF_RUNNING)) == 1310 (IFF_UP|IFF_RUNNING) && vh->sc_state == MASTER && 1311 !bcmp(ena, CLLADDR(vh->sc_if.if_sadl), 1312 ETHER_ADDR_LEN)) { 1313 return (&vh->sc_if); 1314 } 1315 1316 return (NULL); 1317 } 1318 1319 int 1320 carp_input(struct mbuf *m, u_int8_t *shost, u_int8_t *dhost, u_int16_t etype) 1321 { 1322 struct ether_header eh; 1323 struct carp_if *cif = (struct carp_if *)m->m_pkthdr.rcvif->if_carp; 1324 struct ifnet *ifp; 1325 1326 bcopy(shost, &eh.ether_shost, sizeof(eh.ether_shost)); 1327 bcopy(dhost, &eh.ether_dhost, sizeof(eh.ether_dhost)); 1328 eh.ether_type = etype; 1329 1330 if (m->m_flags & (M_BCAST|M_MCAST)) { 1331 struct carp_softc *vh; 1332 struct mbuf *m0; 1333 1334 /* 1335 * XXX Should really check the list of multicast addresses 1336 * for each CARP interface _before_ copying. 1337 */ 1338 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) { 1339 m0 = m_copym(m, 0, M_COPYALL, M_DONTWAIT); 1340 if (m0 == NULL) 1341 continue; 1342 m0->m_pkthdr.rcvif = &vh->sc_if; 1343 ether_input(&vh->sc_if, m0); 1344 } 1345 return (1); 1346 } 1347 1348 ifp = carp_ourether(cif, &eh, m->m_pkthdr.rcvif->if_type, 0); 1349 if (ifp == NULL) { 1350 return (1); 1351 } 1352 1353 m->m_pkthdr.rcvif = ifp; 1354 1355 #if NBPFILTER > 0 1356 if (ifp->if_bpf) 1357 bpf_mtap(ifp->if_bpf, m); 1358 #endif 1359 ifp->if_ipackets++; 1360 ether_input(ifp, m); 1361 return (0); 1362 } 1363 1364 void 1365 carp_master_down(void *v) 1366 { 1367 struct carp_softc *sc = v; 1368 1369 switch (sc->sc_state) { 1370 case INIT: 1371 printf("%s: master_down event in INIT state\n", 1372 sc->sc_if.if_xname); 1373 break; 1374 case MASTER: 1375 break; 1376 case BACKUP: 1377 carp_set_state(sc, MASTER); 1378 carp_send_ad(sc); 1379 carp_send_arp(sc); 1380 #ifdef INET6 1381 carp_send_na(sc); 1382 #endif /* INET6 */ 1383 carp_setrun(sc, 0); 1384 carp_setroute(sc, RTM_ADD); 1385 break; 1386 } 1387 } 1388 1389 /* 1390 * When in backup state, af indicates whether to reset the master down timer 1391 * for v4 or v6. If it's set to zero, reset the ones which are already pending. 1392 */ 1393 void 1394 carp_setrun(struct carp_softc *sc, sa_family_t af) 1395 { 1396 struct timeval tv; 1397 1398 if (sc->sc_carpdev == NULL) { 1399 sc->sc_if.if_flags &= ~IFF_RUNNING; 1400 carp_set_state(sc, INIT); 1401 return; 1402 } 1403 1404 if (sc->sc_if.if_flags & IFF_UP && sc->sc_vhid > 0 && 1405 (sc->sc_naddrs || sc->sc_naddrs6) && !sc->sc_suppress) { 1406 sc->sc_if.if_flags |= IFF_RUNNING; 1407 } else { 1408 sc->sc_if.if_flags &= ~IFF_RUNNING; 1409 carp_setroute(sc, RTM_DELETE); 1410 return; 1411 } 1412 1413 switch (sc->sc_state) { 1414 case INIT: 1415 carp_set_state(sc, BACKUP); 1416 carp_setroute(sc, RTM_DELETE); 1417 carp_setrun(sc, 0); 1418 break; 1419 case BACKUP: 1420 callout_stop(&sc->sc_ad_tmo); 1421 tv.tv_sec = 3 * sc->sc_advbase; 1422 tv.tv_usec = sc->sc_advskew * 1000000 / 256; 1423 switch (af) { 1424 #ifdef INET 1425 case AF_INET: 1426 callout_schedule(&sc->sc_md_tmo, tvtohz(&tv)); 1427 break; 1428 #endif /* INET */ 1429 #ifdef INET6 1430 case AF_INET6: 1431 callout_schedule(&sc->sc_md6_tmo, tvtohz(&tv)); 1432 break; 1433 #endif /* INET6 */ 1434 default: 1435 if (sc->sc_naddrs) 1436 callout_schedule(&sc->sc_md_tmo, tvtohz(&tv)); 1437 if (sc->sc_naddrs6) 1438 callout_schedule(&sc->sc_md6_tmo, tvtohz(&tv)); 1439 break; 1440 } 1441 break; 1442 case MASTER: 1443 tv.tv_sec = sc->sc_advbase; 1444 tv.tv_usec = sc->sc_advskew * 1000000 / 256; 1445 callout_schedule(&sc->sc_ad_tmo, tvtohz(&tv)); 1446 break; 1447 } 1448 } 1449 1450 void 1451 carp_multicast_cleanup(struct carp_softc *sc) 1452 { 1453 struct ip_moptions *imo = &sc->sc_imo; 1454 #ifdef INET6 1455 struct ip6_moptions *im6o = &sc->sc_im6o; 1456 #endif 1457 u_int16_t n = imo->imo_num_memberships; 1458 1459 /* Clean up our own multicast memberships */ 1460 while (n-- > 0) { 1461 if (imo->imo_membership[n] != NULL) { 1462 in_delmulti(imo->imo_membership[n]); 1463 imo->imo_membership[n] = NULL; 1464 } 1465 } 1466 imo->imo_num_memberships = 0; 1467 imo->imo_multicast_ifp = NULL; 1468 1469 #ifdef INET6 1470 while (!LIST_EMPTY(&im6o->im6o_memberships)) { 1471 struct in6_multi_mship *imm = 1472 LIST_FIRST(&im6o->im6o_memberships); 1473 1474 LIST_REMOVE(imm, i6mm_chain); 1475 in6_leavegroup(imm); 1476 } 1477 im6o->im6o_multicast_ifp = NULL; 1478 #endif 1479 1480 /* And any other multicast memberships */ 1481 carp_ether_purgemulti(sc); 1482 } 1483 1484 int 1485 carp_set_ifp(struct carp_softc *sc, struct ifnet *ifp) 1486 { 1487 struct carp_if *cif, *ncif = NULL; 1488 struct carp_softc *vr, *after = NULL; 1489 int myself = 0, error = 0; 1490 int s; 1491 1492 if (ifp == sc->sc_carpdev) 1493 return (0); 1494 1495 if (ifp != NULL) { 1496 if ((ifp->if_flags & IFF_MULTICAST) == 0) 1497 return (EADDRNOTAVAIL); 1498 1499 if (ifp->if_type == IFT_CARP) 1500 return (EINVAL); 1501 1502 if (ifp->if_carp == NULL) { 1503 MALLOC(ncif, struct carp_if *, sizeof(*cif), 1504 M_IFADDR, M_NOWAIT); 1505 if (ncif == NULL) 1506 return (ENOBUFS); 1507 if ((error = ifpromisc(ifp, 1))) { 1508 FREE(ncif, M_IFADDR); 1509 return (error); 1510 } 1511 1512 ncif->vhif_ifp = ifp; 1513 TAILQ_INIT(&ncif->vhif_vrs); 1514 } else { 1515 cif = (struct carp_if *)ifp->if_carp; 1516 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) 1517 if (vr != sc && vr->sc_vhid == sc->sc_vhid) 1518 return (EINVAL); 1519 } 1520 1521 /* detach from old interface */ 1522 if (sc->sc_carpdev != NULL) 1523 carpdetach(sc); 1524 1525 /* join multicast groups */ 1526 if (sc->sc_naddrs < 0 && 1527 (error = carp_join_multicast(sc)) != 0) { 1528 if (ncif != NULL) 1529 FREE(ncif, M_IFADDR); 1530 return (error); 1531 } 1532 1533 #ifdef INET6 1534 if (sc->sc_naddrs6 < 0 && 1535 (error = carp_join_multicast6(sc)) != 0) { 1536 if (ncif != NULL) 1537 FREE(ncif, M_IFADDR); 1538 carp_multicast_cleanup(sc); 1539 return (error); 1540 } 1541 #endif 1542 1543 /* attach carp interface to physical interface */ 1544 if (ncif != NULL) 1545 ifp->if_carp = (void *)ncif; 1546 sc->sc_carpdev = ifp; 1547 cif = (struct carp_if *)ifp->if_carp; 1548 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) { 1549 if (vr == sc) 1550 myself = 1; 1551 if (vr->sc_vhid < sc->sc_vhid) 1552 after = vr; 1553 } 1554 1555 if (!myself) { 1556 /* We're trying to keep things in order */ 1557 if (after == NULL) { 1558 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list); 1559 } else { 1560 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, 1561 sc, sc_list); 1562 } 1563 cif->vhif_nvrs++; 1564 } 1565 if (sc->sc_naddrs || sc->sc_naddrs6) 1566 sc->sc_if.if_flags |= IFF_UP; 1567 carp_set_enaddr(sc); 1568 s = splnet(); 1569 /* XXX linkstatehooks establish */ 1570 carp_carpdev_state(ifp); 1571 splx(s); 1572 } else { 1573 carpdetach(sc); 1574 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING); 1575 } 1576 return (0); 1577 } 1578 1579 void 1580 carp_set_enaddr(struct carp_softc *sc) 1581 { 1582 uint8_t enaddr[ETHER_ADDR_LEN]; 1583 if (sc->sc_carpdev && sc->sc_carpdev->if_type == IFT_ISO88025) { 1584 enaddr[0] = 3; 1585 enaddr[1] = 0; 1586 enaddr[2] = 0x40 >> (sc->sc_vhid - 1); 1587 enaddr[3] = 0x40000 >> (sc->sc_vhid - 1); 1588 enaddr[4] = 0; 1589 enaddr[5] = 0; 1590 } else { 1591 enaddr[0] = 0; 1592 enaddr[1] = 0; 1593 enaddr[2] = 0x5e; 1594 enaddr[3] = 0; 1595 enaddr[4] = 1; 1596 enaddr[5] = sc->sc_vhid; 1597 } 1598 (void)sockaddr_dl_setaddr(sc->sc_if.if_sadl, sc->sc_if.if_sadl->sdl_len, 1599 enaddr, sizeof(enaddr)); 1600 } 1601 1602 void 1603 carp_addr_updated(void *v) 1604 { 1605 struct carp_softc *sc = (struct carp_softc *) v; 1606 struct ifaddr *ifa; 1607 int new_naddrs = 0, new_naddrs6 = 0; 1608 1609 TAILQ_FOREACH(ifa, &sc->sc_if.if_addrlist, ifa_list) { 1610 if (ifa->ifa_addr->sa_family == AF_INET) 1611 new_naddrs++; 1612 else if (ifa->ifa_addr->sa_family == AF_INET6) 1613 new_naddrs6++; 1614 } 1615 1616 /* Handle a callback after SIOCDIFADDR */ 1617 if (new_naddrs < sc->sc_naddrs || new_naddrs6 < sc->sc_naddrs6) { 1618 struct in_addr mc_addr; 1619 struct in_multi *inm; 1620 1621 sc->sc_naddrs = new_naddrs; 1622 sc->sc_naddrs6 = new_naddrs6; 1623 1624 /* Re-establish multicast membership removed by in_control */ 1625 mc_addr.s_addr = INADDR_CARP_GROUP; 1626 IN_LOOKUP_MULTI(mc_addr, &sc->sc_if, inm); 1627 if (inm == NULL) { 1628 bzero(&sc->sc_imo, sizeof(sc->sc_imo)); 1629 1630 if (sc->sc_carpdev != NULL && sc->sc_naddrs > 0) 1631 carp_join_multicast(sc); 1632 } 1633 1634 if (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) { 1635 sc->sc_if.if_flags &= ~IFF_UP; 1636 carp_set_state(sc, INIT); 1637 } else 1638 carp_hmac_prepare(sc); 1639 } 1640 1641 carp_setrun(sc, 0); 1642 } 1643 1644 int 1645 carp_set_addr(struct carp_softc *sc, struct sockaddr_in *sin) 1646 { 1647 struct ifnet *ifp = sc->sc_carpdev; 1648 struct in_ifaddr *ia, *ia_if; 1649 int error = 0; 1650 1651 if (sin->sin_addr.s_addr == 0) { 1652 if (!(sc->sc_if.if_flags & IFF_UP)) 1653 carp_set_state(sc, INIT); 1654 if (sc->sc_naddrs) 1655 sc->sc_if.if_flags |= IFF_UP; 1656 carp_setrun(sc, 0); 1657 return (0); 1658 } 1659 1660 /* we have to do this by hand to ensure we don't match on ourselves */ 1661 ia_if = NULL; 1662 for (ia = TAILQ_FIRST(&in_ifaddrhead); ia; 1663 ia = TAILQ_NEXT(ia, ia_list)) { 1664 1665 /* and, yeah, we need a multicast-capable iface too */ 1666 if (ia->ia_ifp != &sc->sc_if && 1667 ia->ia_ifp->if_type != IFT_CARP && 1668 (ia->ia_ifp->if_flags & IFF_MULTICAST) && 1669 (sin->sin_addr.s_addr & ia->ia_subnetmask) == 1670 ia->ia_subnet) { 1671 if (!ia_if) 1672 ia_if = ia; 1673 } 1674 } 1675 1676 if (ia_if) { 1677 ia = ia_if; 1678 if (ifp) { 1679 if (ifp != ia->ia_ifp) 1680 return (EADDRNOTAVAIL); 1681 } else { 1682 ifp = ia->ia_ifp; 1683 } 1684 } 1685 1686 if ((error = carp_set_ifp(sc, ifp))) 1687 return (error); 1688 1689 if (sc->sc_carpdev == NULL) 1690 return (EADDRNOTAVAIL); 1691 1692 if (sc->sc_naddrs == 0 && (error = carp_join_multicast(sc)) != 0) 1693 return (error); 1694 1695 sc->sc_naddrs++; 1696 if (sc->sc_carpdev != NULL) 1697 sc->sc_if.if_flags |= IFF_UP; 1698 1699 carp_set_state(sc, INIT); 1700 carp_setrun(sc, 0); 1701 1702 /* 1703 * Hook if_addrhooks so that we get a callback after in_ifinit has run, 1704 * to correct any inappropriate routes that it inserted. 1705 */ 1706 if (sc->ah_cookie == 0) { 1707 /* XXX link address hook */ 1708 } 1709 1710 return (0); 1711 } 1712 1713 int 1714 carp_join_multicast(struct carp_softc *sc) 1715 { 1716 struct ip_moptions *imo = &sc->sc_imo, tmpimo; 1717 struct in_addr addr; 1718 1719 bzero(&tmpimo, sizeof(tmpimo)); 1720 addr.s_addr = INADDR_CARP_GROUP; 1721 if ((tmpimo.imo_membership[0] = 1722 in_addmulti(&addr, &sc->sc_if)) == NULL) { 1723 return (ENOBUFS); 1724 } 1725 1726 imo->imo_membership[0] = tmpimo.imo_membership[0]; 1727 imo->imo_num_memberships = 1; 1728 imo->imo_multicast_ifp = &sc->sc_if; 1729 imo->imo_multicast_ttl = CARP_DFLTTL; 1730 imo->imo_multicast_loop = 0; 1731 return (0); 1732 } 1733 1734 1735 #ifdef INET6 1736 int 1737 carp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6) 1738 { 1739 struct ifnet *ifp = sc->sc_carpdev; 1740 struct in6_ifaddr *ia, *ia_if; 1741 int error = 0; 1742 1743 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 1744 if (!(sc->sc_if.if_flags & IFF_UP)) 1745 carp_set_state(sc, INIT); 1746 if (sc->sc_naddrs6) 1747 sc->sc_if.if_flags |= IFF_UP; 1748 carp_setrun(sc, 0); 1749 return (0); 1750 } 1751 1752 /* we have to do this by hand to ensure we don't match on ourselves */ 1753 ia_if = NULL; 1754 for (ia = in6_ifaddr; ia; ia = ia->ia_next) { 1755 int i; 1756 1757 for (i = 0; i < 4; i++) { 1758 if ((sin6->sin6_addr.s6_addr32[i] & 1759 ia->ia_prefixmask.sin6_addr.s6_addr32[i]) != 1760 (ia->ia_addr.sin6_addr.s6_addr32[i] & 1761 ia->ia_prefixmask.sin6_addr.s6_addr32[i])) 1762 break; 1763 } 1764 /* and, yeah, we need a multicast-capable iface too */ 1765 if (ia->ia_ifp != &sc->sc_if && 1766 ia->ia_ifp->if_type != IFT_CARP && 1767 (ia->ia_ifp->if_flags & IFF_MULTICAST) && 1768 (i == 4)) { 1769 if (!ia_if) 1770 ia_if = ia; 1771 } 1772 } 1773 1774 if (ia_if) { 1775 ia = ia_if; 1776 if (sc->sc_carpdev) { 1777 if (sc->sc_carpdev != ia->ia_ifp) 1778 return (EADDRNOTAVAIL); 1779 } else { 1780 ifp = ia->ia_ifp; 1781 } 1782 } 1783 1784 if ((error = carp_set_ifp(sc, ifp))) 1785 return (error); 1786 1787 if (sc->sc_carpdev == NULL) 1788 return (EADDRNOTAVAIL); 1789 1790 if (sc->sc_naddrs6 == 0 && (error = carp_join_multicast6(sc)) != 0) 1791 return (error); 1792 1793 sc->sc_naddrs6++; 1794 if (sc->sc_carpdev != NULL) 1795 sc->sc_if.if_flags |= IFF_UP; 1796 carp_set_state(sc, INIT); 1797 carp_setrun(sc, 0); 1798 1799 return (0); 1800 } 1801 1802 int 1803 carp_join_multicast6(struct carp_softc *sc) 1804 { 1805 struct in6_multi_mship *imm, *imm2; 1806 struct ip6_moptions *im6o = &sc->sc_im6o; 1807 struct sockaddr_in6 addr6; 1808 int error; 1809 1810 /* Join IPv6 CARP multicast group */ 1811 bzero(&addr6, sizeof(addr6)); 1812 addr6.sin6_family = AF_INET6; 1813 addr6.sin6_len = sizeof(addr6); 1814 addr6.sin6_addr.s6_addr16[0] = htons(0xff02); 1815 addr6.sin6_addr.s6_addr16[1] = htons(sc->sc_if.if_index); 1816 addr6.sin6_addr.s6_addr8[15] = 0x12; 1817 if ((imm = in6_joingroup(&sc->sc_if, 1818 &addr6.sin6_addr, &error, 0)) == NULL) { 1819 return (error); 1820 } 1821 /* join solicited multicast address */ 1822 bzero(&addr6.sin6_addr, sizeof(addr6.sin6_addr)); 1823 addr6.sin6_addr.s6_addr16[0] = htons(0xff02); 1824 addr6.sin6_addr.s6_addr16[1] = htons(sc->sc_if.if_index); 1825 addr6.sin6_addr.s6_addr32[1] = 0; 1826 addr6.sin6_addr.s6_addr32[2] = htonl(1); 1827 addr6.sin6_addr.s6_addr32[3] = 0; 1828 addr6.sin6_addr.s6_addr8[12] = 0xff; 1829 if ((imm2 = in6_joingroup(&sc->sc_if, 1830 &addr6.sin6_addr, &error, 0)) == NULL) { 1831 in6_leavegroup(imm); 1832 return (error); 1833 } 1834 1835 /* apply v6 multicast membership */ 1836 im6o->im6o_multicast_ifp = &sc->sc_if; 1837 if (imm) 1838 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, 1839 i6mm_chain); 1840 if (imm2) 1841 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm2, 1842 i6mm_chain); 1843 1844 return (0); 1845 } 1846 1847 #endif /* INET6 */ 1848 1849 int 1850 carp_ioctl(struct ifnet *ifp, u_long cmd, void *addr) 1851 { 1852 struct lwp *l = curlwp; /* XXX */ 1853 struct carp_softc *sc = ifp->if_softc, *vr; 1854 struct carpreq carpr; 1855 struct ifaddr *ifa; 1856 struct ifreq *ifr; 1857 struct ifnet *cdev = NULL; 1858 int error = 0; 1859 1860 ifa = (struct ifaddr *)addr; 1861 ifr = (struct ifreq *)addr; 1862 1863 switch (cmd) { 1864 case SIOCSIFADDR: 1865 switch (ifa->ifa_addr->sa_family) { 1866 #ifdef INET 1867 case AF_INET: 1868 sc->sc_if.if_flags |= IFF_UP; 1869 bcopy(ifa->ifa_addr, ifa->ifa_dstaddr, 1870 sizeof(struct sockaddr)); 1871 error = carp_set_addr(sc, satosin(ifa->ifa_addr)); 1872 break; 1873 #endif /* INET */ 1874 #ifdef INET6 1875 case AF_INET6: 1876 sc->sc_if.if_flags|= IFF_UP; 1877 error = carp_set_addr6(sc, satosin6(ifa->ifa_addr)); 1878 break; 1879 #endif /* INET6 */ 1880 default: 1881 error = EAFNOSUPPORT; 1882 break; 1883 } 1884 break; 1885 1886 case SIOCSIFFLAGS: 1887 if (sc->sc_state != INIT && !(ifr->ifr_flags & IFF_UP)) { 1888 callout_stop(&sc->sc_ad_tmo); 1889 callout_stop(&sc->sc_md_tmo); 1890 callout_stop(&sc->sc_md6_tmo); 1891 if (sc->sc_state == MASTER) { 1892 /* we need the interface up to bow out */ 1893 sc->sc_if.if_flags |= IFF_UP; 1894 sc->sc_bow_out = 1; 1895 carp_send_ad(sc); 1896 } 1897 sc->sc_if.if_flags &= ~IFF_UP; 1898 carp_set_state(sc, INIT); 1899 carp_setrun(sc, 0); 1900 } else if (sc->sc_state == INIT && (ifr->ifr_flags & IFF_UP)) { 1901 sc->sc_if.if_flags |= IFF_UP; 1902 carp_setrun(sc, 0); 1903 } 1904 break; 1905 1906 case SIOCSVH: 1907 if (l == NULL) 1908 break; 1909 if ((error = kauth_authorize_network(l->l_cred, 1910 KAUTH_NETWORK_INTERFACE, 1911 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd, 1912 NULL)) != 0) 1913 break; 1914 if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr))) 1915 break; 1916 error = 1; 1917 if (carpr.carpr_carpdev[0] != '\0' && 1918 (cdev = ifunit(carpr.carpr_carpdev)) == NULL) 1919 return (EINVAL); 1920 if ((error = carp_set_ifp(sc, cdev))) 1921 return (error); 1922 if (sc->sc_state != INIT && carpr.carpr_state != sc->sc_state) { 1923 switch (carpr.carpr_state) { 1924 case BACKUP: 1925 callout_stop(&sc->sc_ad_tmo); 1926 carp_set_state(sc, BACKUP); 1927 carp_setrun(sc, 0); 1928 carp_setroute(sc, RTM_DELETE); 1929 break; 1930 case MASTER: 1931 carp_master_down(sc); 1932 break; 1933 default: 1934 break; 1935 } 1936 } 1937 if (carpr.carpr_vhid > 0) { 1938 if (carpr.carpr_vhid > 255) { 1939 error = EINVAL; 1940 break; 1941 } 1942 if (sc->sc_carpdev) { 1943 struct carp_if *cif; 1944 cif = (struct carp_if *)sc->sc_carpdev->if_carp; 1945 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) 1946 if (vr != sc && 1947 vr->sc_vhid == carpr.carpr_vhid) 1948 return (EINVAL); 1949 } 1950 sc->sc_vhid = carpr.carpr_vhid; 1951 carp_set_enaddr(sc); 1952 carp_set_state(sc, INIT); 1953 error--; 1954 } 1955 if (carpr.carpr_advbase > 0 || carpr.carpr_advskew > 0) { 1956 if (carpr.carpr_advskew > 254) { 1957 error = EINVAL; 1958 break; 1959 } 1960 if (carpr.carpr_advbase > 255) { 1961 error = EINVAL; 1962 break; 1963 } 1964 sc->sc_advbase = carpr.carpr_advbase; 1965 sc->sc_advskew = carpr.carpr_advskew; 1966 error--; 1967 } 1968 bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key)); 1969 if (error > 0) 1970 error = EINVAL; 1971 else { 1972 error = 0; 1973 carp_setrun(sc, 0); 1974 } 1975 break; 1976 1977 case SIOCGVH: 1978 bzero(&carpr, sizeof(carpr)); 1979 if (sc->sc_carpdev != NULL) 1980 strlcpy(carpr.carpr_carpdev, sc->sc_carpdev->if_xname, 1981 IFNAMSIZ); 1982 carpr.carpr_state = sc->sc_state; 1983 carpr.carpr_vhid = sc->sc_vhid; 1984 carpr.carpr_advbase = sc->sc_advbase; 1985 carpr.carpr_advskew = sc->sc_advskew; 1986 1987 if ((l == NULL) || (error = kauth_authorize_network(l->l_cred, 1988 KAUTH_NETWORK_INTERFACE, 1989 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd, 1990 NULL)) != 0) 1991 bcopy(sc->sc_key, carpr.carpr_key, 1992 sizeof(carpr.carpr_key)); 1993 error = copyout(&carpr, ifr->ifr_data, sizeof(carpr)); 1994 break; 1995 1996 case SIOCADDMULTI: 1997 error = carp_ether_addmulti(sc, ifr); 1998 break; 1999 2000 case SIOCDELMULTI: 2001 error = carp_ether_delmulti(sc, ifr); 2002 break; 2003 2004 default: 2005 error = EINVAL; 2006 } 2007 2008 carp_hmac_prepare(sc); 2009 return (error); 2010 } 2011 2012 2013 /* 2014 * Start output on carp interface. This function should never be called. 2015 */ 2016 void 2017 carp_start(struct ifnet *ifp) 2018 { 2019 #ifdef DEBUG 2020 printf("%s: start called\n", ifp->if_xname); 2021 #endif 2022 } 2023 2024 int 2025 carp_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa, 2026 struct rtentry *rt) 2027 { 2028 struct carp_softc *sc = ((struct carp_softc *)ifp->if_softc); 2029 2030 if (sc->sc_carpdev != NULL && sc->sc_state == MASTER) { 2031 return (sc->sc_carpdev->if_output(ifp, m, sa, rt)); 2032 } else { 2033 m_freem(m); 2034 return (ENETUNREACH); 2035 } 2036 } 2037 2038 void 2039 carp_set_state(struct carp_softc *sc, int state) 2040 { 2041 if (sc->sc_state == state) 2042 return; 2043 2044 sc->sc_state = state; 2045 switch (state) { 2046 case BACKUP: 2047 sc->sc_if.if_link_state = LINK_STATE_DOWN; 2048 break; 2049 case MASTER: 2050 sc->sc_if.if_link_state = LINK_STATE_UP; 2051 break; 2052 default: 2053 sc->sc_if.if_link_state = LINK_STATE_UNKNOWN; 2054 break; 2055 } 2056 rt_ifmsg(&sc->sc_if); 2057 } 2058 2059 void 2060 carp_carpdev_state(void *v) 2061 { 2062 struct carp_if *cif; 2063 struct carp_softc *sc; 2064 struct ifnet *ifp = v; 2065 2066 if (ifp->if_type == IFT_CARP) 2067 return; 2068 2069 cif = (struct carp_if *)ifp->if_carp; 2070 2071 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) { 2072 int suppressed = sc->sc_suppress; 2073 2074 if (sc->sc_carpdev->if_link_state == LINK_STATE_DOWN || 2075 !(sc->sc_carpdev->if_flags & IFF_UP)) { 2076 sc->sc_if.if_flags &= ~IFF_RUNNING; 2077 callout_stop(&sc->sc_ad_tmo); 2078 callout_stop(&sc->sc_md_tmo); 2079 callout_stop(&sc->sc_md6_tmo); 2080 carp_set_state(sc, INIT); 2081 sc->sc_suppress = 1; 2082 carp_setrun(sc, 0); 2083 if (!suppressed) { 2084 carp_suppress_preempt++; 2085 if (carp_suppress_preempt == 1) 2086 carp_send_ad_all(); 2087 } 2088 } else { 2089 carp_set_state(sc, INIT); 2090 sc->sc_suppress = 0; 2091 carp_setrun(sc, 0); 2092 if (suppressed) 2093 carp_suppress_preempt--; 2094 } 2095 } 2096 } 2097 2098 int 2099 carp_ether_addmulti(struct carp_softc *sc, struct ifreq *ifr) 2100 { 2101 const struct sockaddr *sa = ifreq_getaddr(SIOCADDMULTI, ifr); 2102 struct ifnet *ifp; 2103 struct carp_mc_entry *mc; 2104 u_int8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN]; 2105 int error; 2106 2107 ifp = sc->sc_carpdev; 2108 if (ifp == NULL) 2109 return (EINVAL); 2110 2111 error = ether_addmulti(sa, &sc->sc_ac); 2112 if (error != ENETRESET) 2113 return (error); 2114 2115 /* 2116 * This is new multicast address. We have to tell parent 2117 * about it. Also, remember this multicast address so that 2118 * we can delete them on unconfigure. 2119 */ 2120 MALLOC(mc, struct carp_mc_entry *, sizeof(struct carp_mc_entry), 2121 M_DEVBUF, M_NOWAIT); 2122 if (mc == NULL) { 2123 error = ENOMEM; 2124 goto alloc_failed; 2125 } 2126 2127 /* 2128 * As ether_addmulti() returns ENETRESET, following two 2129 * statement shouldn't fail. 2130 */ 2131 (void)ether_multiaddr(sa, addrlo, addrhi); 2132 ETHER_LOOKUP_MULTI(addrlo, addrhi, &sc->sc_ac, mc->mc_enm); 2133 memcpy(&mc->mc_addr, sa, sa->sa_len); 2134 LIST_INSERT_HEAD(&sc->carp_mc_listhead, mc, mc_entries); 2135 2136 error = (*ifp->if_ioctl)(ifp, SIOCADDMULTI, (void *)ifr); 2137 if (error != 0) 2138 goto ioctl_failed; 2139 2140 return (error); 2141 2142 ioctl_failed: 2143 LIST_REMOVE(mc, mc_entries); 2144 FREE(mc, M_DEVBUF); 2145 alloc_failed: 2146 (void)ether_delmulti(sa, &sc->sc_ac); 2147 2148 return (error); 2149 } 2150 2151 int 2152 carp_ether_delmulti(struct carp_softc *sc, struct ifreq *ifr) 2153 { 2154 const struct sockaddr *sa = ifreq_getaddr(SIOCDELMULTI, ifr); 2155 struct ifnet *ifp; 2156 struct ether_multi *enm; 2157 struct carp_mc_entry *mc; 2158 u_int8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN]; 2159 int error; 2160 2161 ifp = sc->sc_carpdev; 2162 if (ifp == NULL) 2163 return (EINVAL); 2164 2165 /* 2166 * Find a key to lookup carp_mc_entry. We have to do this 2167 * before calling ether_delmulti for obvious reason. 2168 */ 2169 if ((error = ether_multiaddr(sa, addrlo, addrhi)) != 0) 2170 return (error); 2171 ETHER_LOOKUP_MULTI(addrlo, addrhi, &sc->sc_ac, enm); 2172 if (enm == NULL) 2173 return (EINVAL); 2174 2175 LIST_FOREACH(mc, &sc->carp_mc_listhead, mc_entries) 2176 if (mc->mc_enm == enm) 2177 break; 2178 2179 /* We won't delete entries we didn't add */ 2180 if (mc == NULL) 2181 return (EINVAL); 2182 2183 error = ether_delmulti(sa, &sc->sc_ac); 2184 if (error != ENETRESET) 2185 return (error); 2186 2187 /* We no longer use this multicast address. Tell parent so. */ 2188 error = (*ifp->if_ioctl)(ifp, SIOCDELMULTI, (void *)ifr); 2189 if (error == 0) { 2190 /* And forget about this address. */ 2191 LIST_REMOVE(mc, mc_entries); 2192 FREE(mc, M_DEVBUF); 2193 } else 2194 (void)ether_addmulti(sa, &sc->sc_ac); 2195 return (error); 2196 } 2197 2198 /* 2199 * Delete any multicast address we have asked to add from parent 2200 * interface. Called when the carp is being unconfigured. 2201 */ 2202 void 2203 carp_ether_purgemulti(struct carp_softc *sc) 2204 { 2205 struct ifnet *ifp = sc->sc_carpdev; /* Parent. */ 2206 struct carp_mc_entry *mc; 2207 union { 2208 struct ifreq ifreq; 2209 struct { 2210 char ifr_name[IFNAMSIZ]; 2211 struct sockaddr_storage ifr_ss; 2212 } ifreq_storage; 2213 } u; 2214 struct ifreq *ifr = &u.ifreq; 2215 2216 if (ifp == NULL) 2217 return; 2218 2219 memcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ); 2220 while ((mc = LIST_FIRST(&sc->carp_mc_listhead)) != NULL) { 2221 memcpy(&ifr->ifr_addr, &mc->mc_addr, mc->mc_addr.ss_len); 2222 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, (void *)ifr); 2223 LIST_REMOVE(mc, mc_entries); 2224 FREE(mc, M_DEVBUF); 2225 } 2226 } 2227 2228 SYSCTL_SETUP(sysctl_net_inet_carp_setup, "sysctl net.inet.carp subtree setup") 2229 { 2230 2231 sysctl_createv(clog, 0, NULL, NULL, 2232 CTLFLAG_PERMANENT, 2233 CTLTYPE_NODE, "net", NULL, 2234 NULL, 0, NULL, 0, 2235 CTL_NET, CTL_EOL); 2236 sysctl_createv(clog, 0, NULL, NULL, 2237 CTLFLAG_PERMANENT, 2238 CTLTYPE_NODE, "inet", NULL, 2239 NULL, 0, NULL, 0, 2240 CTL_NET, PF_INET, CTL_EOL); 2241 sysctl_createv(clog, 0, NULL, NULL, 2242 CTLFLAG_PERMANENT, 2243 CTLTYPE_NODE, "carp", 2244 SYSCTL_DESCR("CARP related settings"), 2245 NULL, 0, NULL, 0, 2246 CTL_NET, PF_INET, IPPROTO_CARP, CTL_EOL); 2247 2248 sysctl_createv(clog, 0, NULL, NULL, 2249 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2250 CTLTYPE_INT, "preempt", 2251 SYSCTL_DESCR("Enable CARP Preempt"), 2252 NULL, 0, &carp_opts[CARPCTL_PREEMPT], 0, 2253 CTL_NET, PF_INET, IPPROTO_CARP, 2254 CTL_CREATE, CTL_EOL); 2255 sysctl_createv(clog, 0, NULL, NULL, 2256 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2257 CTLTYPE_INT, "arpbalance", 2258 SYSCTL_DESCR("Enable ARP balancing"), 2259 NULL, 0, &carp_opts[CARPCTL_ARPBALANCE], 0, 2260 CTL_NET, PF_INET, IPPROTO_CARP, 2261 CTL_CREATE, CTL_EOL); 2262 sysctl_createv(clog, 0, NULL, NULL, 2263 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2264 CTLTYPE_INT, "allow", 2265 SYSCTL_DESCR("Enable CARP"), 2266 NULL, 0, &carp_opts[CARPCTL_ALLOW], 0, 2267 CTL_NET, PF_INET, IPPROTO_CARP, 2268 CTL_CREATE, CTL_EOL); 2269 sysctl_createv(clog, 0, NULL, NULL, 2270 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2271 CTLTYPE_INT, "log", 2272 SYSCTL_DESCR("CARP logging"), 2273 NULL, 0, &carp_opts[CARPCTL_LOG], 0, 2274 CTL_NET, PF_INET, IPPROTO_CARP, 2275 CTL_CREATE, CTL_EOL); 2276 sysctl_createv(clog, 0, NULL, NULL, 2277 CTLFLAG_PERMANENT, 2278 CTLTYPE_STRUCT, "stats", 2279 SYSCTL_DESCR("CARP statistics"), 2280 NULL, 0, &carpstats, sizeof(carpstats), 2281 CTL_NET, PF_INET, IPPROTO_CARP, CARPCTL_STATS, 2282 CTL_EOL); 2283 } 2284