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