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