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