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