1 /* $NetBSD: ip_carp.c,v 1.51 2013/10/18 19:48:36 christos 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.51 2013/10/18 19:48:36 christos 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; 461 va_list ap; 462 463 va_start(ap, m); 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 = cprng_fast64(); 881 } else 882 sc->sc_counter++; 883 884 ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff); 885 ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff); 886 887 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md); 888 889 return (0); 890 } 891 892 void 893 carp_send_ad_all(void) 894 { 895 struct ifnet *ifp; 896 struct carp_if *cif; 897 struct carp_softc *vh; 898 899 TAILQ_FOREACH(ifp, &ifnet, if_list) { 900 if (ifp->if_carp == NULL || ifp->if_type == IFT_CARP) 901 continue; 902 903 cif = (struct carp_if *)ifp->if_carp; 904 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) { 905 if ((vh->sc_if.if_flags & (IFF_UP|IFF_RUNNING)) == 906 (IFF_UP|IFF_RUNNING) && vh->sc_state == MASTER) 907 carp_send_ad(vh); 908 } 909 } 910 } 911 912 913 void 914 carp_send_ad(void *v) 915 { 916 struct carp_header ch; 917 struct timeval tv; 918 struct carp_softc *sc = v; 919 struct carp_header *ch_ptr; 920 struct mbuf *m; 921 int error, len, advbase, advskew, s; 922 struct ifaddr *ifa; 923 struct sockaddr sa; 924 925 s = splsoftnet(); 926 927 advbase = advskew = 0; /* Sssssh compiler */ 928 if (sc->sc_carpdev == NULL) { 929 sc->sc_if.if_oerrors++; 930 goto retry_later; 931 } 932 933 /* bow out if we've gone to backup (the carp interface is going down) */ 934 if (sc->sc_bow_out) { 935 sc->sc_bow_out = 0; 936 advbase = 255; 937 advskew = 255; 938 } else { 939 advbase = sc->sc_advbase; 940 if (!carp_suppress_preempt || sc->sc_advskew > 240) 941 advskew = sc->sc_advskew; 942 else 943 advskew = 240; 944 tv.tv_sec = advbase; 945 tv.tv_usec = advskew * 1000000 / 256; 946 } 947 948 ch.carp_version = CARP_VERSION; 949 ch.carp_type = CARP_ADVERTISEMENT; 950 ch.carp_vhid = sc->sc_vhid; 951 ch.carp_advbase = advbase; 952 ch.carp_advskew = advskew; 953 ch.carp_authlen = 7; /* XXX DEFINE */ 954 ch.carp_pad1 = 0; /* must be zero */ 955 ch.carp_cksum = 0; 956 957 958 #ifdef INET 959 if (sc->sc_naddrs) { 960 struct ip *ip; 961 962 MGETHDR(m, M_DONTWAIT, MT_HEADER); 963 if (m == NULL) { 964 sc->sc_if.if_oerrors++; 965 CARP_STATINC(CARP_STAT_ONOMEM); 966 /* XXX maybe less ? */ 967 goto retry_later; 968 } 969 len = sizeof(*ip) + sizeof(ch); 970 m->m_pkthdr.len = len; 971 m->m_pkthdr.rcvif = NULL; 972 m->m_len = len; 973 MH_ALIGN(m, m->m_len); 974 m->m_flags |= M_MCAST; 975 ip = mtod(m, struct ip *); 976 ip->ip_v = IPVERSION; 977 ip->ip_hl = sizeof(*ip) >> 2; 978 ip->ip_tos = IPTOS_LOWDELAY; 979 ip->ip_len = htons(len); 980 ip->ip_id = 0; /* no need for id, we don't support fragments */ 981 ip->ip_off = htons(IP_DF); 982 ip->ip_ttl = CARP_DFLTTL; 983 ip->ip_p = IPPROTO_CARP; 984 ip->ip_sum = 0; 985 986 memset(&sa, 0, sizeof(sa)); 987 sa.sa_family = AF_INET; 988 ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev); 989 if (ifa == NULL) 990 ip->ip_src.s_addr = 0; 991 else 992 ip->ip_src.s_addr = 993 ifatoia(ifa)->ia_addr.sin_addr.s_addr; 994 ip->ip_dst.s_addr = INADDR_CARP_GROUP; 995 996 ch_ptr = (struct carp_header *)(&ip[1]); 997 memcpy(ch_ptr, &ch, sizeof(ch)); 998 if (carp_prepare_ad(m, sc, ch_ptr)) 999 goto retry_later; 1000 1001 m->m_data += sizeof(*ip); 1002 ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip)); 1003 m->m_data -= sizeof(*ip); 1004 1005 nanotime(&sc->sc_if.if_lastchange); 1006 sc->sc_if.if_opackets++; 1007 sc->sc_if.if_obytes += len; 1008 CARP_STATINC(CARP_STAT_OPACKETS); 1009 1010 error = ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, 1011 NULL); 1012 if (error) { 1013 if (error == ENOBUFS) 1014 CARP_STATINC(CARP_STAT_ONOMEM); 1015 else 1016 CARP_LOG(sc, ("ip_output failed: %d", error)); 1017 sc->sc_if.if_oerrors++; 1018 if (sc->sc_sendad_errors < INT_MAX) 1019 sc->sc_sendad_errors++; 1020 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) { 1021 carp_suppress_preempt++; 1022 if (carp_suppress_preempt == 1) 1023 carp_send_ad_all(); 1024 } 1025 sc->sc_sendad_success = 0; 1026 } else { 1027 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) { 1028 if (++sc->sc_sendad_success >= 1029 CARP_SENDAD_MIN_SUCCESS) { 1030 carp_suppress_preempt--; 1031 sc->sc_sendad_errors = 0; 1032 } 1033 } else 1034 sc->sc_sendad_errors = 0; 1035 } 1036 } 1037 #endif /* INET */ 1038 #ifdef INET6 1039 if (sc->sc_naddrs6) { 1040 struct ip6_hdr *ip6; 1041 1042 MGETHDR(m, M_DONTWAIT, MT_HEADER); 1043 if (m == NULL) { 1044 sc->sc_if.if_oerrors++; 1045 CARP_STATINC(CARP_STAT_ONOMEM); 1046 /* XXX maybe less ? */ 1047 goto retry_later; 1048 } 1049 len = sizeof(*ip6) + sizeof(ch); 1050 m->m_pkthdr.len = len; 1051 m->m_pkthdr.rcvif = NULL; 1052 m->m_len = len; 1053 MH_ALIGN(m, m->m_len); 1054 m->m_flags |= M_MCAST; 1055 ip6 = mtod(m, struct ip6_hdr *); 1056 memset(ip6, 0, sizeof(*ip6)); 1057 ip6->ip6_vfc |= IPV6_VERSION; 1058 ip6->ip6_hlim = CARP_DFLTTL; 1059 ip6->ip6_nxt = IPPROTO_CARP; 1060 1061 /* set the source address */ 1062 memset(&sa, 0, sizeof(sa)); 1063 sa.sa_family = AF_INET6; 1064 ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev); 1065 if (ifa == NULL) /* This should never happen with IPv6 */ 1066 memset(&ip6->ip6_src, 0, sizeof(struct in6_addr)); 1067 else 1068 bcopy(ifatoia6(ifa)->ia_addr.sin6_addr.s6_addr, 1069 &ip6->ip6_src, sizeof(struct in6_addr)); 1070 /* set the multicast destination */ 1071 1072 ip6->ip6_dst.s6_addr16[0] = htons(0xff02); 1073 ip6->ip6_dst.s6_addr8[15] = 0x12; 1074 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) { 1075 sc->sc_if.if_oerrors++; 1076 m_freem(m); 1077 CARP_LOG(sc, ("in6_setscope failed")); 1078 goto retry_later; 1079 } 1080 1081 ch_ptr = (struct carp_header *)(&ip6[1]); 1082 memcpy(ch_ptr, &ch, sizeof(ch)); 1083 if (carp_prepare_ad(m, sc, ch_ptr)) 1084 goto retry_later; 1085 1086 m->m_data += sizeof(*ip6); 1087 ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip6)); 1088 m->m_data -= sizeof(*ip6); 1089 1090 nanotime(&sc->sc_if.if_lastchange); 1091 sc->sc_if.if_opackets++; 1092 sc->sc_if.if_obytes += len; 1093 CARP_STATINC(CARP_STAT_OPACKETS6); 1094 1095 error = ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL); 1096 if (error) { 1097 if (error == ENOBUFS) 1098 CARP_STATINC(CARP_STAT_ONOMEM); 1099 else 1100 CARP_LOG(sc, ("ip6_output failed: %d", error)); 1101 sc->sc_if.if_oerrors++; 1102 if (sc->sc_sendad_errors < INT_MAX) 1103 sc->sc_sendad_errors++; 1104 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) { 1105 carp_suppress_preempt++; 1106 if (carp_suppress_preempt == 1) 1107 carp_send_ad_all(); 1108 } 1109 sc->sc_sendad_success = 0; 1110 } else { 1111 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) { 1112 if (++sc->sc_sendad_success >= 1113 CARP_SENDAD_MIN_SUCCESS) { 1114 carp_suppress_preempt--; 1115 sc->sc_sendad_errors = 0; 1116 } 1117 } else 1118 sc->sc_sendad_errors = 0; 1119 } 1120 } 1121 #endif /* INET6 */ 1122 1123 retry_later: 1124 splx(s); 1125 if (advbase != 255 || advskew != 255) 1126 callout_schedule(&sc->sc_ad_tmo, tvtohz(&tv)); 1127 } 1128 1129 /* 1130 * Broadcast a gratuitous ARP request containing 1131 * the virtual router MAC address for each IP address 1132 * associated with the virtual router. 1133 */ 1134 void 1135 carp_send_arp(struct carp_softc *sc) 1136 { 1137 struct ifaddr *ifa; 1138 struct in_addr *in; 1139 int s = splsoftnet(); 1140 1141 IFADDR_FOREACH(ifa, &sc->sc_if) { 1142 1143 if (ifa->ifa_addr->sa_family != AF_INET) 1144 continue; 1145 1146 in = &ifatoia(ifa)->ia_addr.sin_addr; 1147 arprequest(sc->sc_carpdev, in, in, CLLADDR(sc->sc_if.if_sadl)); 1148 } 1149 splx(s); 1150 } 1151 1152 #ifdef INET6 1153 void 1154 carp_send_na(struct carp_softc *sc) 1155 { 1156 struct ifaddr *ifa; 1157 struct in6_addr *in6; 1158 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT; 1159 int s = splsoftnet(); 1160 1161 IFADDR_FOREACH(ifa, &sc->sc_if) { 1162 1163 if (ifa->ifa_addr->sa_family != AF_INET6) 1164 continue; 1165 1166 in6 = &ifatoia6(ifa)->ia_addr.sin6_addr; 1167 nd6_na_output(sc->sc_carpdev, &mcast, in6, 1168 ND_NA_FLAG_OVERRIDE, 1, NULL); 1169 } 1170 splx(s); 1171 } 1172 #endif /* INET6 */ 1173 1174 /* 1175 * Based on bridge_hash() in if_bridge.c 1176 */ 1177 #define mix(a,b,c) \ 1178 do { \ 1179 a -= b; a -= c; a ^= (c >> 13); \ 1180 b -= c; b -= a; b ^= (a << 8); \ 1181 c -= a; c -= b; c ^= (b >> 13); \ 1182 a -= b; a -= c; a ^= (c >> 12); \ 1183 b -= c; b -= a; b ^= (a << 16); \ 1184 c -= a; c -= b; c ^= (b >> 5); \ 1185 a -= b; a -= c; a ^= (c >> 3); \ 1186 b -= c; b -= a; b ^= (a << 10); \ 1187 c -= a; c -= b; c ^= (b >> 15); \ 1188 } while (0) 1189 1190 u_int32_t 1191 carp_hash(struct carp_softc *sc, u_char *src) 1192 { 1193 u_int32_t a = 0x9e3779b9, b = sc->sc_hashkey[0], c = sc->sc_hashkey[1]; 1194 1195 c += sc->sc_key[3] << 24; 1196 c += sc->sc_key[2] << 16; 1197 c += sc->sc_key[1] << 8; 1198 c += sc->sc_key[0]; 1199 b += src[5] << 8; 1200 b += src[4]; 1201 a += src[3] << 24; 1202 a += src[2] << 16; 1203 a += src[1] << 8; 1204 a += src[0]; 1205 1206 mix(a, b, c); 1207 return (c); 1208 } 1209 1210 int 1211 carp_addrcount(struct carp_if *cif, struct in_ifaddr *ia, int type) 1212 { 1213 struct carp_softc *vh; 1214 struct ifaddr *ifa; 1215 int count = 0; 1216 1217 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) { 1218 if ((type == CARP_COUNT_RUNNING && 1219 (vh->sc_if.if_flags & (IFF_UP|IFF_RUNNING)) == 1220 (IFF_UP|IFF_RUNNING)) || 1221 (type == CARP_COUNT_MASTER && vh->sc_state == MASTER)) { 1222 IFADDR_FOREACH(ifa, &vh->sc_if) { 1223 if (ifa->ifa_addr->sa_family == AF_INET && 1224 ia->ia_addr.sin_addr.s_addr == 1225 ifatoia(ifa)->ia_addr.sin_addr.s_addr) 1226 count++; 1227 } 1228 } 1229 } 1230 return (count); 1231 } 1232 1233 int 1234 carp_iamatch(struct in_ifaddr *ia, u_char *src, 1235 u_int32_t *count, u_int32_t index) 1236 { 1237 struct carp_softc *sc = ia->ia_ifp->if_softc; 1238 1239 if (carp_opts[CARPCTL_ARPBALANCE]) { 1240 /* 1241 * We use the source ip to decide which virtual host should 1242 * handle the request. If we're master of that virtual host, 1243 * then we respond, otherwise, just drop the arp packet on 1244 * the floor. 1245 */ 1246 1247 /* Count the elegible carp interfaces with this address */ 1248 if (*count == 0) 1249 *count = carp_addrcount( 1250 (struct carp_if *)ia->ia_ifp->if_carpdev->if_carp, 1251 ia, CARP_COUNT_RUNNING); 1252 1253 /* This should never happen, but... */ 1254 if (*count == 0) 1255 return (0); 1256 1257 if (carp_hash(sc, src) % *count == index - 1 && 1258 sc->sc_state == MASTER) { 1259 return (1); 1260 } 1261 } else { 1262 if (sc->sc_state == MASTER) 1263 return (1); 1264 } 1265 1266 return (0); 1267 } 1268 1269 #ifdef INET6 1270 struct ifaddr * 1271 carp_iamatch6(void *v, struct in6_addr *taddr) 1272 { 1273 struct carp_if *cif = v; 1274 struct carp_softc *vh; 1275 struct ifaddr *ifa; 1276 1277 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) { 1278 IFADDR_FOREACH(ifa, &vh->sc_if) { 1279 if (IN6_ARE_ADDR_EQUAL(taddr, 1280 &ifatoia6(ifa)->ia_addr.sin6_addr) && 1281 ((vh->sc_if.if_flags & (IFF_UP|IFF_RUNNING)) == 1282 (IFF_UP|IFF_RUNNING)) && vh->sc_state == MASTER) 1283 return (ifa); 1284 } 1285 } 1286 1287 return (NULL); 1288 } 1289 #endif /* INET6 */ 1290 1291 struct ifnet * 1292 carp_ourether(void *v, struct ether_header *eh, u_char iftype, int src) 1293 { 1294 struct carp_if *cif = (struct carp_if *)v; 1295 struct carp_softc *vh; 1296 u_int8_t *ena; 1297 1298 if (src) 1299 ena = (u_int8_t *)&eh->ether_shost; 1300 else 1301 ena = (u_int8_t *)&eh->ether_dhost; 1302 1303 switch (iftype) { 1304 case IFT_ETHER: 1305 case IFT_FDDI: 1306 if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1) 1307 return (NULL); 1308 break; 1309 case IFT_ISO88025: 1310 if (ena[0] != 3 || ena[1] || ena[4] || ena[5]) 1311 return (NULL); 1312 break; 1313 default: 1314 return (NULL); 1315 break; 1316 } 1317 1318 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) 1319 if ((vh->sc_if.if_flags & (IFF_UP|IFF_RUNNING)) == 1320 (IFF_UP|IFF_RUNNING) && vh->sc_state == MASTER && 1321 !memcmp(ena, CLLADDR(vh->sc_if.if_sadl), 1322 ETHER_ADDR_LEN)) { 1323 return (&vh->sc_if); 1324 } 1325 1326 return (NULL); 1327 } 1328 1329 int 1330 carp_input(struct mbuf *m, u_int8_t *shost, u_int8_t *dhost, u_int16_t etype) 1331 { 1332 struct ether_header eh; 1333 struct carp_if *cif = (struct carp_if *)m->m_pkthdr.rcvif->if_carp; 1334 struct ifnet *ifp; 1335 1336 memcpy(&eh.ether_shost, shost, sizeof(eh.ether_shost)); 1337 memcpy(&eh.ether_dhost, dhost, sizeof(eh.ether_dhost)); 1338 eh.ether_type = etype; 1339 1340 if (m->m_flags & (M_BCAST|M_MCAST)) { 1341 struct carp_softc *vh; 1342 struct mbuf *m0; 1343 1344 /* 1345 * XXX Should really check the list of multicast addresses 1346 * for each CARP interface _before_ copying. 1347 */ 1348 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) { 1349 m0 = m_copym(m, 0, M_COPYALL, M_DONTWAIT); 1350 if (m0 == NULL) 1351 continue; 1352 m0->m_pkthdr.rcvif = &vh->sc_if; 1353 ether_input(&vh->sc_if, m0); 1354 } 1355 return (1); 1356 } 1357 1358 ifp = carp_ourether(cif, &eh, m->m_pkthdr.rcvif->if_type, 0); 1359 if (ifp == NULL) { 1360 return (1); 1361 } 1362 1363 m->m_pkthdr.rcvif = ifp; 1364 1365 bpf_mtap(ifp, m); 1366 ifp->if_ipackets++; 1367 ether_input(ifp, m); 1368 return (0); 1369 } 1370 1371 void 1372 carp_master_down(void *v) 1373 { 1374 struct carp_softc *sc = v; 1375 1376 switch (sc->sc_state) { 1377 case INIT: 1378 printf("%s: master_down event in INIT state\n", 1379 sc->sc_if.if_xname); 1380 break; 1381 case MASTER: 1382 break; 1383 case BACKUP: 1384 CARP_LOG(sc, ("INIT -> MASTER (preempting)")); 1385 carp_set_state(sc, MASTER); 1386 carp_send_ad(sc); 1387 carp_send_arp(sc); 1388 #ifdef INET6 1389 carp_send_na(sc); 1390 #endif /* INET6 */ 1391 carp_setrun(sc, 0); 1392 carp_setroute(sc, RTM_ADD); 1393 break; 1394 } 1395 } 1396 1397 /* 1398 * When in backup state, af indicates whether to reset the master down timer 1399 * for v4 or v6. If it's set to zero, reset the ones which are already pending. 1400 */ 1401 void 1402 carp_setrun(struct carp_softc *sc, sa_family_t af) 1403 { 1404 struct timeval tv; 1405 1406 if (sc->sc_carpdev == NULL) { 1407 sc->sc_if.if_flags &= ~IFF_RUNNING; 1408 carp_set_state(sc, INIT); 1409 return; 1410 } 1411 1412 if (sc->sc_if.if_flags & IFF_UP && sc->sc_vhid > 0 && 1413 (sc->sc_naddrs || sc->sc_naddrs6) && !sc->sc_suppress) { 1414 sc->sc_if.if_flags |= IFF_RUNNING; 1415 } else { 1416 sc->sc_if.if_flags &= ~IFF_RUNNING; 1417 carp_setroute(sc, RTM_DELETE); 1418 return; 1419 } 1420 1421 switch (sc->sc_state) { 1422 case INIT: 1423 carp_set_state(sc, BACKUP); 1424 carp_setroute(sc, RTM_DELETE); 1425 carp_setrun(sc, 0); 1426 break; 1427 case BACKUP: 1428 callout_stop(&sc->sc_ad_tmo); 1429 tv.tv_sec = 3 * sc->sc_advbase; 1430 tv.tv_usec = sc->sc_advskew * 1000000 / 256; 1431 switch (af) { 1432 #ifdef INET 1433 case AF_INET: 1434 callout_schedule(&sc->sc_md_tmo, tvtohz(&tv)); 1435 break; 1436 #endif /* INET */ 1437 #ifdef INET6 1438 case AF_INET6: 1439 callout_schedule(&sc->sc_md6_tmo, tvtohz(&tv)); 1440 break; 1441 #endif /* INET6 */ 1442 default: 1443 if (sc->sc_naddrs) 1444 callout_schedule(&sc->sc_md_tmo, tvtohz(&tv)); 1445 if (sc->sc_naddrs6) 1446 callout_schedule(&sc->sc_md6_tmo, tvtohz(&tv)); 1447 break; 1448 } 1449 break; 1450 case MASTER: 1451 tv.tv_sec = sc->sc_advbase; 1452 tv.tv_usec = sc->sc_advskew * 1000000 / 256; 1453 callout_schedule(&sc->sc_ad_tmo, tvtohz(&tv)); 1454 break; 1455 } 1456 } 1457 1458 void 1459 carp_multicast_cleanup(struct carp_softc *sc) 1460 { 1461 struct ip_moptions *imo = &sc->sc_imo; 1462 #ifdef INET6 1463 struct ip6_moptions *im6o = &sc->sc_im6o; 1464 #endif 1465 u_int16_t n = imo->imo_num_memberships; 1466 1467 /* Clean up our own multicast memberships */ 1468 while (n-- > 0) { 1469 if (imo->imo_membership[n] != NULL) { 1470 in_delmulti(imo->imo_membership[n]); 1471 imo->imo_membership[n] = NULL; 1472 } 1473 } 1474 imo->imo_num_memberships = 0; 1475 imo->imo_multicast_ifp = NULL; 1476 1477 #ifdef INET6 1478 while (!LIST_EMPTY(&im6o->im6o_memberships)) { 1479 struct in6_multi_mship *imm = 1480 LIST_FIRST(&im6o->im6o_memberships); 1481 1482 LIST_REMOVE(imm, i6mm_chain); 1483 in6_leavegroup(imm); 1484 } 1485 im6o->im6o_multicast_ifp = NULL; 1486 #endif 1487 1488 /* And any other multicast memberships */ 1489 carp_ether_purgemulti(sc); 1490 } 1491 1492 int 1493 carp_set_ifp(struct carp_softc *sc, struct ifnet *ifp) 1494 { 1495 struct carp_if *cif, *ncif = NULL; 1496 struct carp_softc *vr, *after = NULL; 1497 int myself = 0, error = 0; 1498 int s; 1499 1500 if (ifp == sc->sc_carpdev) 1501 return (0); 1502 1503 if (ifp != NULL) { 1504 if ((ifp->if_flags & IFF_MULTICAST) == 0) 1505 return (EADDRNOTAVAIL); 1506 1507 if (ifp->if_type == IFT_CARP) 1508 return (EINVAL); 1509 1510 if (ifp->if_carp == NULL) { 1511 ncif = malloc(sizeof(*cif), M_IFADDR, M_NOWAIT); 1512 if (ncif == NULL) 1513 return (ENOBUFS); 1514 if ((error = ifpromisc(ifp, 1))) { 1515 free(ncif, M_IFADDR); 1516 return (error); 1517 } 1518 1519 ncif->vhif_ifp = ifp; 1520 TAILQ_INIT(&ncif->vhif_vrs); 1521 } else { 1522 cif = (struct carp_if *)ifp->if_carp; 1523 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) 1524 if (vr != sc && vr->sc_vhid == sc->sc_vhid) 1525 return (EINVAL); 1526 } 1527 1528 /* detach from old interface */ 1529 if (sc->sc_carpdev != NULL) 1530 carpdetach(sc); 1531 1532 /* join multicast groups */ 1533 if (sc->sc_naddrs < 0 && 1534 (error = carp_join_multicast(sc)) != 0) { 1535 if (ncif != NULL) 1536 free(ncif, M_IFADDR); 1537 return (error); 1538 } 1539 1540 #ifdef INET6 1541 if (sc->sc_naddrs6 < 0 && 1542 (error = carp_join_multicast6(sc)) != 0) { 1543 if (ncif != NULL) 1544 free(ncif, M_IFADDR); 1545 carp_multicast_cleanup(sc); 1546 return (error); 1547 } 1548 #endif 1549 1550 /* attach carp interface to physical interface */ 1551 if (ncif != NULL) 1552 ifp->if_carp = (void *)ncif; 1553 sc->sc_carpdev = ifp; 1554 sc->sc_if.if_capabilities = ifp->if_capabilities & 1555 (IFCAP_TSOv4 | IFCAP_TSOv6 | 1556 IFCAP_CSUM_IPv4_Tx|IFCAP_CSUM_IPv4_Rx| 1557 IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_TCPv4_Rx| 1558 IFCAP_CSUM_UDPv4_Tx|IFCAP_CSUM_UDPv4_Rx| 1559 IFCAP_CSUM_TCPv6_Tx|IFCAP_CSUM_TCPv6_Rx| 1560 IFCAP_CSUM_UDPv6_Tx|IFCAP_CSUM_UDPv6_Rx); 1561 1562 cif = (struct carp_if *)ifp->if_carp; 1563 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) { 1564 if (vr == sc) 1565 myself = 1; 1566 if (vr->sc_vhid < sc->sc_vhid) 1567 after = vr; 1568 } 1569 1570 if (!myself) { 1571 /* We're trying to keep things in order */ 1572 if (after == NULL) { 1573 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list); 1574 } else { 1575 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, 1576 sc, sc_list); 1577 } 1578 cif->vhif_nvrs++; 1579 } 1580 if (sc->sc_naddrs || sc->sc_naddrs6) 1581 sc->sc_if.if_flags |= IFF_UP; 1582 carp_set_enaddr(sc); 1583 s = splnet(); 1584 /* XXX linkstatehooks establish */ 1585 carp_carpdev_state(ifp); 1586 splx(s); 1587 } else { 1588 carpdetach(sc); 1589 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING); 1590 } 1591 return (0); 1592 } 1593 1594 void 1595 carp_set_enaddr(struct carp_softc *sc) 1596 { 1597 uint8_t enaddr[ETHER_ADDR_LEN]; 1598 if (sc->sc_carpdev && sc->sc_carpdev->if_type == IFT_ISO88025) { 1599 enaddr[0] = 3; 1600 enaddr[1] = 0; 1601 enaddr[2] = 0x40 >> (sc->sc_vhid - 1); 1602 enaddr[3] = 0x40000 >> (sc->sc_vhid - 1); 1603 enaddr[4] = 0; 1604 enaddr[5] = 0; 1605 } else { 1606 enaddr[0] = 0; 1607 enaddr[1] = 0; 1608 enaddr[2] = 0x5e; 1609 enaddr[3] = 0; 1610 enaddr[4] = 1; 1611 enaddr[5] = sc->sc_vhid; 1612 } 1613 if_set_sadl(&sc->sc_if, enaddr, sizeof(enaddr), false); 1614 } 1615 1616 void 1617 carp_addr_updated(void *v) 1618 { 1619 struct carp_softc *sc = (struct carp_softc *) v; 1620 struct ifaddr *ifa; 1621 int new_naddrs = 0, new_naddrs6 = 0; 1622 1623 IFADDR_FOREACH(ifa, &sc->sc_if) { 1624 if (ifa->ifa_addr->sa_family == AF_INET) 1625 new_naddrs++; 1626 else if (ifa->ifa_addr->sa_family == AF_INET6) 1627 new_naddrs6++; 1628 } 1629 1630 /* Handle a callback after SIOCDIFADDR */ 1631 if (new_naddrs < sc->sc_naddrs || new_naddrs6 < sc->sc_naddrs6) { 1632 struct in_addr mc_addr; 1633 struct in_multi *inm; 1634 1635 sc->sc_naddrs = new_naddrs; 1636 sc->sc_naddrs6 = new_naddrs6; 1637 1638 /* Re-establish multicast membership removed by in_control */ 1639 mc_addr.s_addr = INADDR_CARP_GROUP; 1640 IN_LOOKUP_MULTI(mc_addr, &sc->sc_if, inm); 1641 if (inm == NULL) { 1642 memset(&sc->sc_imo, 0, sizeof(sc->sc_imo)); 1643 1644 if (sc->sc_carpdev != NULL && sc->sc_naddrs > 0) 1645 carp_join_multicast(sc); 1646 } 1647 1648 if (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) { 1649 sc->sc_if.if_flags &= ~IFF_UP; 1650 carp_set_state(sc, INIT); 1651 } else 1652 carp_hmac_prepare(sc); 1653 } 1654 1655 carp_setrun(sc, 0); 1656 } 1657 1658 int 1659 carp_set_addr(struct carp_softc *sc, struct sockaddr_in *sin) 1660 { 1661 struct ifnet *ifp = sc->sc_carpdev; 1662 struct in_ifaddr *ia, *ia_if; 1663 int error = 0; 1664 1665 if (sin->sin_addr.s_addr == 0) { 1666 if (!(sc->sc_if.if_flags & IFF_UP)) 1667 carp_set_state(sc, INIT); 1668 if (sc->sc_naddrs) 1669 sc->sc_if.if_flags |= IFF_UP; 1670 carp_setrun(sc, 0); 1671 return (0); 1672 } 1673 1674 /* we have to do this by hand to ensure we don't match on ourselves */ 1675 ia_if = NULL; 1676 for (ia = TAILQ_FIRST(&in_ifaddrhead); ia; 1677 ia = TAILQ_NEXT(ia, ia_list)) { 1678 1679 /* and, yeah, we need a multicast-capable iface too */ 1680 if (ia->ia_ifp != &sc->sc_if && 1681 ia->ia_ifp->if_type != IFT_CARP && 1682 (ia->ia_ifp->if_flags & IFF_MULTICAST) && 1683 (sin->sin_addr.s_addr & ia->ia_subnetmask) == 1684 ia->ia_subnet) { 1685 if (!ia_if) 1686 ia_if = ia; 1687 } 1688 } 1689 1690 if (ia_if) { 1691 ia = ia_if; 1692 if (ifp) { 1693 if (ifp != ia->ia_ifp) 1694 return (EADDRNOTAVAIL); 1695 } else { 1696 ifp = ia->ia_ifp; 1697 } 1698 } 1699 1700 if ((error = carp_set_ifp(sc, ifp))) 1701 return (error); 1702 1703 if (sc->sc_carpdev == NULL) 1704 return (EADDRNOTAVAIL); 1705 1706 if (sc->sc_naddrs == 0 && (error = carp_join_multicast(sc)) != 0) 1707 return (error); 1708 1709 sc->sc_naddrs++; 1710 if (sc->sc_carpdev != NULL) 1711 sc->sc_if.if_flags |= IFF_UP; 1712 1713 carp_set_state(sc, INIT); 1714 carp_setrun(sc, 0); 1715 1716 /* 1717 * Hook if_addrhooks so that we get a callback after in_ifinit has run, 1718 * to correct any inappropriate routes that it inserted. 1719 */ 1720 if (sc->ah_cookie == 0) { 1721 /* XXX link address hook */ 1722 } 1723 1724 return (0); 1725 } 1726 1727 int 1728 carp_join_multicast(struct carp_softc *sc) 1729 { 1730 struct ip_moptions *imo = &sc->sc_imo, tmpimo; 1731 struct in_addr addr; 1732 1733 memset(&tmpimo, 0, sizeof(tmpimo)); 1734 addr.s_addr = INADDR_CARP_GROUP; 1735 if ((tmpimo.imo_membership[0] = 1736 in_addmulti(&addr, &sc->sc_if)) == NULL) { 1737 return (ENOBUFS); 1738 } 1739 1740 imo->imo_membership[0] = tmpimo.imo_membership[0]; 1741 imo->imo_num_memberships = 1; 1742 imo->imo_multicast_ifp = &sc->sc_if; 1743 imo->imo_multicast_ttl = CARP_DFLTTL; 1744 imo->imo_multicast_loop = 0; 1745 return (0); 1746 } 1747 1748 1749 #ifdef INET6 1750 int 1751 carp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6) 1752 { 1753 struct ifnet *ifp = sc->sc_carpdev; 1754 struct in6_ifaddr *ia, *ia_if; 1755 int error = 0; 1756 1757 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 1758 if (!(sc->sc_if.if_flags & IFF_UP)) 1759 carp_set_state(sc, INIT); 1760 if (sc->sc_naddrs6) 1761 sc->sc_if.if_flags |= IFF_UP; 1762 carp_setrun(sc, 0); 1763 return (0); 1764 } 1765 1766 /* we have to do this by hand to ensure we don't match on ourselves */ 1767 ia_if = NULL; 1768 for (ia = in6_ifaddr; ia; ia = ia->ia_next) { 1769 int i; 1770 1771 for (i = 0; i < 4; i++) { 1772 if ((sin6->sin6_addr.s6_addr32[i] & 1773 ia->ia_prefixmask.sin6_addr.s6_addr32[i]) != 1774 (ia->ia_addr.sin6_addr.s6_addr32[i] & 1775 ia->ia_prefixmask.sin6_addr.s6_addr32[i])) 1776 break; 1777 } 1778 /* and, yeah, we need a multicast-capable iface too */ 1779 if (ia->ia_ifp != &sc->sc_if && 1780 ia->ia_ifp->if_type != IFT_CARP && 1781 (ia->ia_ifp->if_flags & IFF_MULTICAST) && 1782 (i == 4)) { 1783 if (!ia_if) 1784 ia_if = ia; 1785 } 1786 } 1787 1788 if (ia_if) { 1789 ia = ia_if; 1790 if (sc->sc_carpdev) { 1791 if (sc->sc_carpdev != ia->ia_ifp) 1792 return (EADDRNOTAVAIL); 1793 } else { 1794 ifp = ia->ia_ifp; 1795 } 1796 } 1797 1798 if ((error = carp_set_ifp(sc, ifp))) 1799 return (error); 1800 1801 if (sc->sc_carpdev == NULL) 1802 return (EADDRNOTAVAIL); 1803 1804 if (sc->sc_naddrs6 == 0 && (error = carp_join_multicast6(sc)) != 0) 1805 return (error); 1806 1807 sc->sc_naddrs6++; 1808 if (sc->sc_carpdev != NULL) 1809 sc->sc_if.if_flags |= IFF_UP; 1810 carp_set_state(sc, INIT); 1811 carp_setrun(sc, 0); 1812 1813 return (0); 1814 } 1815 1816 int 1817 carp_join_multicast6(struct carp_softc *sc) 1818 { 1819 struct in6_multi_mship *imm, *imm2; 1820 struct ip6_moptions *im6o = &sc->sc_im6o; 1821 struct sockaddr_in6 addr6; 1822 int error; 1823 1824 /* Join IPv6 CARP multicast group */ 1825 memset(&addr6, 0, sizeof(addr6)); 1826 addr6.sin6_family = AF_INET6; 1827 addr6.sin6_len = sizeof(addr6); 1828 addr6.sin6_addr.s6_addr16[0] = htons(0xff02); 1829 addr6.sin6_addr.s6_addr16[1] = htons(sc->sc_if.if_index); 1830 addr6.sin6_addr.s6_addr8[15] = 0x12; 1831 if ((imm = in6_joingroup(&sc->sc_if, 1832 &addr6.sin6_addr, &error, 0)) == NULL) { 1833 return (error); 1834 } 1835 /* join solicited multicast address */ 1836 memset(&addr6.sin6_addr, 0, sizeof(addr6.sin6_addr)); 1837 addr6.sin6_addr.s6_addr16[0] = htons(0xff02); 1838 addr6.sin6_addr.s6_addr16[1] = htons(sc->sc_if.if_index); 1839 addr6.sin6_addr.s6_addr32[1] = 0; 1840 addr6.sin6_addr.s6_addr32[2] = htonl(1); 1841 addr6.sin6_addr.s6_addr32[3] = 0; 1842 addr6.sin6_addr.s6_addr8[12] = 0xff; 1843 if ((imm2 = in6_joingroup(&sc->sc_if, 1844 &addr6.sin6_addr, &error, 0)) == NULL) { 1845 in6_leavegroup(imm); 1846 return (error); 1847 } 1848 1849 /* apply v6 multicast membership */ 1850 im6o->im6o_multicast_ifp = &sc->sc_if; 1851 if (imm) 1852 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, 1853 i6mm_chain); 1854 if (imm2) 1855 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm2, 1856 i6mm_chain); 1857 1858 return (0); 1859 } 1860 1861 #endif /* INET6 */ 1862 1863 int 1864 carp_ioctl(struct ifnet *ifp, u_long cmd, void *data) 1865 { 1866 struct lwp *l = curlwp; /* XXX */ 1867 struct carp_softc *sc = ifp->if_softc, *vr; 1868 struct carpreq carpr; 1869 struct ifaddr *ifa; 1870 struct ifreq *ifr; 1871 struct ifnet *cdev = NULL; 1872 int error = 0; 1873 1874 ifa = (struct ifaddr *)data; 1875 ifr = (struct ifreq *)data; 1876 1877 switch (cmd) { 1878 case SIOCINITIFADDR: 1879 switch (ifa->ifa_addr->sa_family) { 1880 #ifdef INET 1881 case AF_INET: 1882 sc->sc_if.if_flags |= IFF_UP; 1883 memcpy(ifa->ifa_dstaddr, ifa->ifa_addr, 1884 sizeof(struct sockaddr)); 1885 error = carp_set_addr(sc, satosin(ifa->ifa_addr)); 1886 break; 1887 #endif /* INET */ 1888 #ifdef INET6 1889 case AF_INET6: 1890 sc->sc_if.if_flags|= IFF_UP; 1891 error = carp_set_addr6(sc, satosin6(ifa->ifa_addr)); 1892 break; 1893 #endif /* INET6 */ 1894 default: 1895 error = EAFNOSUPPORT; 1896 break; 1897 } 1898 break; 1899 1900 case SIOCSIFFLAGS: 1901 if ((error = ifioctl_common(ifp, cmd, data)) != 0) 1902 break; 1903 if (sc->sc_state != INIT && !(ifr->ifr_flags & IFF_UP)) { 1904 callout_stop(&sc->sc_ad_tmo); 1905 callout_stop(&sc->sc_md_tmo); 1906 callout_stop(&sc->sc_md6_tmo); 1907 if (sc->sc_state == MASTER) { 1908 /* we need the interface up to bow out */ 1909 sc->sc_if.if_flags |= IFF_UP; 1910 sc->sc_bow_out = 1; 1911 carp_send_ad(sc); 1912 } 1913 sc->sc_if.if_flags &= ~IFF_UP; 1914 carp_set_state(sc, INIT); 1915 carp_setrun(sc, 0); 1916 } else if (sc->sc_state == INIT && (ifr->ifr_flags & IFF_UP)) { 1917 sc->sc_if.if_flags |= IFF_UP; 1918 carp_setrun(sc, 0); 1919 } 1920 break; 1921 1922 case SIOCSVH: 1923 if (l == NULL) 1924 break; 1925 if ((error = kauth_authorize_network(l->l_cred, 1926 KAUTH_NETWORK_INTERFACE, 1927 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd, 1928 NULL)) != 0) 1929 break; 1930 if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr))) 1931 break; 1932 error = 1; 1933 if (carpr.carpr_carpdev[0] != '\0' && 1934 (cdev = ifunit(carpr.carpr_carpdev)) == NULL) 1935 return (EINVAL); 1936 if ((error = carp_set_ifp(sc, cdev))) 1937 return (error); 1938 if (sc->sc_state != INIT && carpr.carpr_state != sc->sc_state) { 1939 switch (carpr.carpr_state) { 1940 case BACKUP: 1941 callout_stop(&sc->sc_ad_tmo); 1942 carp_set_state(sc, BACKUP); 1943 carp_setrun(sc, 0); 1944 carp_setroute(sc, RTM_DELETE); 1945 break; 1946 case MASTER: 1947 carp_master_down(sc); 1948 break; 1949 default: 1950 break; 1951 } 1952 } 1953 if (carpr.carpr_vhid > 0) { 1954 if (carpr.carpr_vhid > 255) { 1955 error = EINVAL; 1956 break; 1957 } 1958 if (sc->sc_carpdev) { 1959 struct carp_if *cif; 1960 cif = (struct carp_if *)sc->sc_carpdev->if_carp; 1961 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) 1962 if (vr != sc && 1963 vr->sc_vhid == carpr.carpr_vhid) 1964 return (EINVAL); 1965 } 1966 sc->sc_vhid = carpr.carpr_vhid; 1967 carp_set_enaddr(sc); 1968 carp_set_state(sc, INIT); 1969 error--; 1970 } 1971 if (carpr.carpr_advbase > 0 || carpr.carpr_advskew > 0) { 1972 if (carpr.carpr_advskew > 254) { 1973 error = EINVAL; 1974 break; 1975 } 1976 if (carpr.carpr_advbase > 255) { 1977 error = EINVAL; 1978 break; 1979 } 1980 sc->sc_advbase = carpr.carpr_advbase; 1981 sc->sc_advskew = carpr.carpr_advskew; 1982 error--; 1983 } 1984 memcpy(sc->sc_key, carpr.carpr_key, sizeof(sc->sc_key)); 1985 if (error > 0) 1986 error = EINVAL; 1987 else { 1988 error = 0; 1989 carp_setrun(sc, 0); 1990 } 1991 break; 1992 1993 case SIOCGVH: 1994 memset(&carpr, 0, sizeof(carpr)); 1995 if (sc->sc_carpdev != NULL) 1996 strlcpy(carpr.carpr_carpdev, sc->sc_carpdev->if_xname, 1997 IFNAMSIZ); 1998 carpr.carpr_state = sc->sc_state; 1999 carpr.carpr_vhid = sc->sc_vhid; 2000 carpr.carpr_advbase = sc->sc_advbase; 2001 carpr.carpr_advskew = sc->sc_advskew; 2002 2003 if ((l != NULL) && (error = kauth_authorize_network(l->l_cred, 2004 KAUTH_NETWORK_INTERFACE, 2005 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd, 2006 NULL)) == 0) 2007 memcpy(carpr.carpr_key, sc->sc_key, 2008 sizeof(carpr.carpr_key)); 2009 error = copyout(&carpr, ifr->ifr_data, sizeof(carpr)); 2010 break; 2011 2012 case SIOCADDMULTI: 2013 error = carp_ether_addmulti(sc, ifr); 2014 break; 2015 2016 case SIOCDELMULTI: 2017 error = carp_ether_delmulti(sc, ifr); 2018 break; 2019 2020 case SIOCSIFCAP: 2021 if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET) 2022 error = 0; 2023 break; 2024 2025 default: 2026 error = ether_ioctl(ifp, cmd, data); 2027 } 2028 2029 carp_hmac_prepare(sc); 2030 return (error); 2031 } 2032 2033 2034 /* 2035 * Start output on carp interface. This function should never be called. 2036 */ 2037 void 2038 carp_start(struct ifnet *ifp) 2039 { 2040 #ifdef DEBUG 2041 printf("%s: start called\n", ifp->if_xname); 2042 #endif 2043 } 2044 2045 int 2046 carp_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa, 2047 struct rtentry *rt) 2048 { 2049 struct carp_softc *sc = ((struct carp_softc *)ifp->if_softc); 2050 2051 if (sc->sc_carpdev != NULL && sc->sc_state == MASTER) { 2052 return (sc->sc_carpdev->if_output(ifp, m, sa, rt)); 2053 } else { 2054 m_freem(m); 2055 return (ENETUNREACH); 2056 } 2057 } 2058 2059 void 2060 carp_set_state(struct carp_softc *sc, int state) 2061 { 2062 static const char *carp_states[] = { CARP_STATES }; 2063 if (sc->sc_state == state) 2064 return; 2065 2066 CARP_LOG(sc, ("state transition from: %s -> to: %s", carp_states[sc->sc_state], carp_states[state])); 2067 2068 sc->sc_state = state; 2069 switch (state) { 2070 case BACKUP: 2071 sc->sc_if.if_link_state = LINK_STATE_DOWN; 2072 break; 2073 case MASTER: 2074 sc->sc_if.if_link_state = LINK_STATE_UP; 2075 break; 2076 default: 2077 sc->sc_if.if_link_state = LINK_STATE_UNKNOWN; 2078 break; 2079 } 2080 rt_ifmsg(&sc->sc_if); 2081 } 2082 2083 void 2084 carp_carpdev_state(void *v) 2085 { 2086 struct carp_if *cif; 2087 struct carp_softc *sc; 2088 struct ifnet *ifp = v; 2089 2090 if (ifp->if_type == IFT_CARP) 2091 return; 2092 2093 cif = (struct carp_if *)ifp->if_carp; 2094 2095 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) { 2096 int suppressed = sc->sc_suppress; 2097 2098 if (sc->sc_carpdev->if_link_state == LINK_STATE_DOWN || 2099 !(sc->sc_carpdev->if_flags & IFF_UP)) { 2100 sc->sc_if.if_flags &= ~IFF_RUNNING; 2101 callout_stop(&sc->sc_ad_tmo); 2102 callout_stop(&sc->sc_md_tmo); 2103 callout_stop(&sc->sc_md6_tmo); 2104 carp_set_state(sc, INIT); 2105 sc->sc_suppress = 1; 2106 carp_setrun(sc, 0); 2107 if (!suppressed) { 2108 carp_suppress_preempt++; 2109 if (carp_suppress_preempt == 1) 2110 carp_send_ad_all(); 2111 } 2112 } else { 2113 carp_set_state(sc, INIT); 2114 sc->sc_suppress = 0; 2115 carp_setrun(sc, 0); 2116 if (suppressed) 2117 carp_suppress_preempt--; 2118 } 2119 } 2120 } 2121 2122 int 2123 carp_ether_addmulti(struct carp_softc *sc, struct ifreq *ifr) 2124 { 2125 const struct sockaddr *sa = ifreq_getaddr(SIOCADDMULTI, ifr); 2126 struct ifnet *ifp; 2127 struct carp_mc_entry *mc; 2128 u_int8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN]; 2129 int error; 2130 2131 ifp = sc->sc_carpdev; 2132 if (ifp == NULL) 2133 return (EINVAL); 2134 2135 error = ether_addmulti(sa, &sc->sc_ac); 2136 if (error != ENETRESET) 2137 return (error); 2138 2139 /* 2140 * This is new multicast address. We have to tell parent 2141 * about it. Also, remember this multicast address so that 2142 * we can delete them on unconfigure. 2143 */ 2144 mc = malloc(sizeof(struct carp_mc_entry), M_DEVBUF, M_NOWAIT); 2145 if (mc == NULL) { 2146 error = ENOMEM; 2147 goto alloc_failed; 2148 } 2149 2150 /* 2151 * As ether_addmulti() returns ENETRESET, following two 2152 * statement shouldn't fail. 2153 */ 2154 (void)ether_multiaddr(sa, addrlo, addrhi); 2155 ETHER_LOOKUP_MULTI(addrlo, addrhi, &sc->sc_ac, mc->mc_enm); 2156 memcpy(&mc->mc_addr, sa, sa->sa_len); 2157 LIST_INSERT_HEAD(&sc->carp_mc_listhead, mc, mc_entries); 2158 2159 error = if_mcast_op(ifp, SIOCADDMULTI, sa); 2160 if (error != 0) 2161 goto ioctl_failed; 2162 2163 return (error); 2164 2165 ioctl_failed: 2166 LIST_REMOVE(mc, mc_entries); 2167 free(mc, M_DEVBUF); 2168 alloc_failed: 2169 (void)ether_delmulti(sa, &sc->sc_ac); 2170 2171 return (error); 2172 } 2173 2174 int 2175 carp_ether_delmulti(struct carp_softc *sc, struct ifreq *ifr) 2176 { 2177 const struct sockaddr *sa = ifreq_getaddr(SIOCDELMULTI, ifr); 2178 struct ifnet *ifp; 2179 struct ether_multi *enm; 2180 struct carp_mc_entry *mc; 2181 u_int8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN]; 2182 int error; 2183 2184 ifp = sc->sc_carpdev; 2185 if (ifp == NULL) 2186 return (EINVAL); 2187 2188 /* 2189 * Find a key to lookup carp_mc_entry. We have to do this 2190 * before calling ether_delmulti for obvious reason. 2191 */ 2192 if ((error = ether_multiaddr(sa, addrlo, addrhi)) != 0) 2193 return (error); 2194 ETHER_LOOKUP_MULTI(addrlo, addrhi, &sc->sc_ac, enm); 2195 if (enm == NULL) 2196 return (EINVAL); 2197 2198 LIST_FOREACH(mc, &sc->carp_mc_listhead, mc_entries) 2199 if (mc->mc_enm == enm) 2200 break; 2201 2202 /* We won't delete entries we didn't add */ 2203 if (mc == NULL) 2204 return (EINVAL); 2205 2206 error = ether_delmulti(sa, &sc->sc_ac); 2207 if (error != ENETRESET) 2208 return (error); 2209 2210 /* We no longer use this multicast address. Tell parent so. */ 2211 error = if_mcast_op(ifp, SIOCDELMULTI, sa); 2212 if (error == 0) { 2213 /* And forget about this address. */ 2214 LIST_REMOVE(mc, mc_entries); 2215 free(mc, M_DEVBUF); 2216 } else 2217 (void)ether_addmulti(sa, &sc->sc_ac); 2218 return (error); 2219 } 2220 2221 /* 2222 * Delete any multicast address we have asked to add from parent 2223 * interface. Called when the carp is being unconfigured. 2224 */ 2225 void 2226 carp_ether_purgemulti(struct carp_softc *sc) 2227 { 2228 struct ifnet *ifp = sc->sc_carpdev; /* Parent. */ 2229 struct carp_mc_entry *mc; 2230 2231 if (ifp == NULL) 2232 return; 2233 2234 while ((mc = LIST_FIRST(&sc->carp_mc_listhead)) != NULL) { 2235 (void)if_mcast_op(ifp, SIOCDELMULTI, sstosa(&mc->mc_addr)); 2236 LIST_REMOVE(mc, mc_entries); 2237 free(mc, M_DEVBUF); 2238 } 2239 } 2240 2241 static int 2242 sysctl_net_inet_carp_stats(SYSCTLFN_ARGS) 2243 { 2244 2245 return (NETSTAT_SYSCTL(carpstat_percpu, CARP_NSTATS)); 2246 } 2247 2248 void 2249 carp_init(void) 2250 { 2251 2252 sysctl_net_inet_carp_setup(NULL); 2253 } 2254 2255 static void 2256 sysctl_net_inet_carp_setup(struct sysctllog **clog) 2257 { 2258 2259 sysctl_createv(clog, 0, NULL, NULL, 2260 CTLFLAG_PERMANENT, 2261 CTLTYPE_NODE, "net", NULL, 2262 NULL, 0, NULL, 0, 2263 CTL_NET, CTL_EOL); 2264 sysctl_createv(clog, 0, NULL, NULL, 2265 CTLFLAG_PERMANENT, 2266 CTLTYPE_NODE, "inet", NULL, 2267 NULL, 0, NULL, 0, 2268 CTL_NET, PF_INET, CTL_EOL); 2269 sysctl_createv(clog, 0, NULL, NULL, 2270 CTLFLAG_PERMANENT, 2271 CTLTYPE_NODE, "carp", 2272 SYSCTL_DESCR("CARP related settings"), 2273 NULL, 0, NULL, 0, 2274 CTL_NET, PF_INET, IPPROTO_CARP, CTL_EOL); 2275 2276 sysctl_createv(clog, 0, NULL, NULL, 2277 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2278 CTLTYPE_INT, "preempt", 2279 SYSCTL_DESCR("Enable CARP Preempt"), 2280 NULL, 0, &carp_opts[CARPCTL_PREEMPT], 0, 2281 CTL_NET, PF_INET, IPPROTO_CARP, 2282 CTL_CREATE, CTL_EOL); 2283 sysctl_createv(clog, 0, NULL, NULL, 2284 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2285 CTLTYPE_INT, "arpbalance", 2286 SYSCTL_DESCR("Enable ARP balancing"), 2287 NULL, 0, &carp_opts[CARPCTL_ARPBALANCE], 0, 2288 CTL_NET, PF_INET, IPPROTO_CARP, 2289 CTL_CREATE, CTL_EOL); 2290 sysctl_createv(clog, 0, NULL, NULL, 2291 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2292 CTLTYPE_INT, "allow", 2293 SYSCTL_DESCR("Enable CARP"), 2294 NULL, 0, &carp_opts[CARPCTL_ALLOW], 0, 2295 CTL_NET, PF_INET, IPPROTO_CARP, 2296 CTL_CREATE, CTL_EOL); 2297 sysctl_createv(clog, 0, NULL, NULL, 2298 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2299 CTLTYPE_INT, "log", 2300 SYSCTL_DESCR("CARP logging"), 2301 NULL, 0, &carp_opts[CARPCTL_LOG], 0, 2302 CTL_NET, PF_INET, IPPROTO_CARP, 2303 CTL_CREATE, CTL_EOL); 2304 sysctl_createv(clog, 0, NULL, NULL, 2305 CTLFLAG_PERMANENT, 2306 CTLTYPE_STRUCT, "stats", 2307 SYSCTL_DESCR("CARP statistics"), 2308 sysctl_net_inet_carp_stats, 0, NULL, 0, 2309 CTL_NET, PF_INET, IPPROTO_CARP, CARPCTL_STATS, 2310 CTL_EOL); 2311 } 2312