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