1 /* 2 * Copyright (c) 2002 Michael Shalayeff. All rights reserved. 3 * Copyright (c) 2003 Ryan McBride. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT, 18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 20 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 23 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 24 * THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 /* 27 * $FreeBSD: src/sys/netinet/ip_carp.c,v 1.48 2007/02/02 09:39:09 glebius Exp $ 28 */ 29 30 #include "opt_carp.h" 31 #include "opt_inet.h" 32 #include "opt_inet6.h" 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #include <sys/in_cksum.h> 38 #include <sys/limits.h> 39 #include <sys/malloc.h> 40 #include <sys/mbuf.h> 41 #include <sys/msgport2.h> 42 #include <sys/time.h> 43 #include <sys/proc.h> 44 #include <sys/priv.h> 45 #include <sys/sockio.h> 46 #include <sys/socket.h> 47 #include <sys/sysctl.h> 48 #include <sys/syslog.h> 49 #include <sys/thread.h> 50 51 #include <machine/stdarg.h> 52 #include <crypto/sha1.h> 53 54 #include <net/bpf.h> 55 #include <net/ethernet.h> 56 #include <net/if.h> 57 #include <net/if_dl.h> 58 #include <net/if_types.h> 59 #include <net/route.h> 60 #include <net/if_clone.h> 61 #include <net/if_var.h> 62 #include <net/ifq_var.h> 63 #include <net/netmsg2.h> 64 #include <net/netisr2.h> 65 66 #ifdef INET 67 #include <netinet/in.h> 68 #include <netinet/in_var.h> 69 #include <netinet/in_systm.h> 70 #include <netinet/ip.h> 71 #include <netinet/ip_var.h> 72 #include <netinet/if_ether.h> 73 #endif 74 75 #ifdef INET6 76 #include <netinet/icmp6.h> 77 #include <netinet/ip6.h> 78 #include <netinet6/ip6_var.h> 79 #include <netinet6/scope6_var.h> 80 #include <netinet6/nd6.h> 81 #endif 82 83 #include <netinet/ip_carp.h> 84 85 /* 86 * Note about carp's MP safe approach: 87 * 88 * Brief: carp_softc (softc), carp_softc_container (scc) 89 * 90 * - All configuration operation, e.g. ioctl, add/delete inet addresses 91 * is serialized by netisr0; not by carp's serializer 92 * 93 * - Backing interface's if_carp and carp_softc's relationship: 94 * 95 * +---------+ 96 * if_carp -->| carp_if | 97 * +---------+ 98 * | 99 * | 100 * V +---------+ 101 * +-----+ | | 102 * | scc |-->| softc | 103 * +-----+ | | 104 * | +---------+ 105 * | 106 * V +---------+ 107 * +-----+ | | 108 * | scc |-->| softc | 109 * +-----+ | | 110 * +---------+ 111 * 112 * - if_carp creation, modification and deletion all happen in netisr0, 113 * as stated previously. Since if_carp is accessed by multiple netisrs, 114 * the modification to if_carp is conducted in the following way: 115 * 116 * Adding carp_softc: 117 * 118 * 1) Duplicate the old carp_if to new carp_if (ncif), and insert the 119 * to-be-added carp_softc to the new carp_if (ncif): 120 * 121 * if_carp ncif 122 * | | 123 * V V 124 * +---------+ +---------+ 125 * | carp_if | | carp_if | 126 * +---------+ +---------+ 127 * | | 128 * | | 129 * V +-------+ V 130 * +-----+ | | +-----+ 131 * | scc |---->| softc |<----| scc | 132 * +-----+ | | +-----+ 133 * | +-------+ | 134 * | | 135 * V +-------+ V 136 * +-----+ | | +-----+ 137 * | scc |---->| softc |<----| scc | 138 * +-----+ | | +-----+ 139 * +-------+ | 140 * | 141 * +-------+ V 142 * | | +-----+ 143 * | softc |<----| scc | 144 * | | +-----+ 145 * +-------+ 146 * 147 * 2) Switch save if_carp into ocif and switch if_carp to ncif: 148 * 149 * ocif if_carp 150 * | | 151 * V V 152 * +---------+ +---------+ 153 * | carp_if | | carp_if | 154 * +---------+ +---------+ 155 * | | 156 * | | 157 * V +-------+ V 158 * +-----+ | | +-----+ 159 * | scc |---->| softc |<----| scc | 160 * +-----+ | | +-----+ 161 * | +-------+ | 162 * | | 163 * V +-------+ V 164 * +-----+ | | +-----+ 165 * | scc |---->| softc |<----| scc | 166 * +-----+ | | +-----+ 167 * +-------+ | 168 * | 169 * +-------+ V 170 * | | +-----+ 171 * | softc |<----| scc | 172 * | | +-----+ 173 * +-------+ 174 * 175 * 3) Run netmsg_service_sync(), which will make sure that 176 * ocif is no longer accessed (all network operations 177 * are happened only in network threads). 178 * 4) Free ocif -- only carp_if and scc are freed. 179 * 180 * 181 * Removing carp_softc: 182 * 183 * 1) Duplicate the old carp_if to new carp_if (ncif); the to-be-deleted 184 * carp_softc will not be duplicated. 185 * 186 * if_carp ncif 187 * | | 188 * V V 189 * +---------+ +---------+ 190 * | carp_if | | carp_if | 191 * +---------+ +---------+ 192 * | | 193 * | | 194 * V +-------+ V 195 * +-----+ | | +-----+ 196 * | scc |---->| softc |<----| scc | 197 * +-----+ | | +-----+ 198 * | +-------+ | 199 * | | 200 * V +-------+ | 201 * +-----+ | | | 202 * | scc |---->| softc | | 203 * +-----+ | | | 204 * | +-------+ | 205 * | | 206 * V +-------+ V 207 * +-----+ | | +-----+ 208 * | scc |---->| softc |<----| scc | 209 * +-----+ | | +-----+ 210 * +-------+ 211 * 212 * 2) Switch save if_carp into ocif and switch if_carp to ncif: 213 * 214 * ocif if_carp 215 * | | 216 * V V 217 * +---------+ +---------+ 218 * | carp_if | | carp_if | 219 * +---------+ +---------+ 220 * | | 221 * | | 222 * V +-------+ V 223 * +-----+ | | +-----+ 224 * | scc |---->| softc |<----| scc | 225 * +-----+ | | +-----+ 226 * | +-------+ | 227 * | | 228 * V +-------+ | 229 * +-----+ | | | 230 * | scc |---->| softc | | 231 * +-----+ | | | 232 * | +-------+ | 233 * | | 234 * V +-------+ V 235 * +-----+ | | +-----+ 236 * | scc |---->| softc |<----| scc | 237 * +-----+ | | +-----+ 238 * +-------+ 239 * 240 * 3) Run netmsg_service_sync(), which will make sure that 241 * ocif is no longer accessed (all network operations 242 * are happened only in network threads). 243 * 4) Free ocif -- only carp_if and scc are freed. 244 * 245 * - if_carp accessing: 246 * The accessing code should cache the if_carp in a local temporary 247 * variable and accessing the temporary variable along the code path 248 * instead of accessing if_carp later on. 249 */ 250 251 #define CARP_IFNAME "carp" 252 #define CARP_IS_RUNNING(ifp) \ 253 (((ifp)->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING)) 254 255 struct carp_softc; 256 257 struct carp_vhaddr { 258 uint32_t vha_flags; /* CARP_VHAF_ */ 259 struct in_ifaddr *vha_ia; /* carp address */ 260 struct in_ifaddr *vha_iaback; /* backing address */ 261 TAILQ_ENTRY(carp_vhaddr) vha_link; 262 }; 263 TAILQ_HEAD(carp_vhaddr_list, carp_vhaddr); 264 265 struct netmsg_carp { 266 struct netmsg_base base; 267 struct ifnet *nc_carpdev; 268 struct carp_softc *nc_softc; 269 void *nc_data; 270 size_t nc_datalen; 271 }; 272 273 struct carp_softc { 274 struct arpcom arpcom; 275 struct ifnet *sc_carpdev; /* parent interface */ 276 struct carp_vhaddr_list sc_vha_list; /* virtual addr list */ 277 278 const struct in_ifaddr *sc_ia; /* primary iface address v4 */ 279 struct ip_moptions sc_imo; 280 281 #ifdef INET6 282 struct in6_ifaddr *sc_ia6; /* primary iface address v6 */ 283 struct ip6_moptions sc_im6o; 284 #endif /* INET6 */ 285 286 enum { INIT = 0, BACKUP, MASTER } 287 sc_state; 288 boolean_t sc_dead; 289 290 int sc_suppress; 291 292 int sc_sendad_errors; 293 #define CARP_SENDAD_MAX_ERRORS 3 294 int sc_sendad_success; 295 #define CARP_SENDAD_MIN_SUCCESS 3 296 297 int sc_vhid; 298 int sc_advskew; 299 int sc_naddrs; /* actually used IPv4 vha */ 300 int sc_naddrs6; 301 int sc_advbase; /* seconds */ 302 int sc_init_counter; 303 uint64_t sc_counter; 304 305 /* authentication */ 306 #define CARP_HMAC_PAD 64 307 unsigned char sc_key[CARP_KEY_LEN]; 308 unsigned char sc_pad[CARP_HMAC_PAD]; 309 SHA1_CTX sc_sha1; 310 311 struct callout sc_ad_tmo; /* advertisement timeout */ 312 struct netmsg_carp sc_ad_msg; /* adv timeout netmsg */ 313 struct callout sc_md_tmo; /* ip4 master down timeout */ 314 struct callout sc_md6_tmo; /* ip6 master down timeout */ 315 struct netmsg_carp sc_md_msg; /* master down timeout netmsg */ 316 317 LIST_ENTRY(carp_softc) sc_next; /* Interface clue */ 318 }; 319 320 #define sc_if arpcom.ac_if 321 322 struct carp_softc_container { 323 TAILQ_ENTRY(carp_softc_container) scc_link; 324 struct carp_softc *scc_softc; 325 }; 326 TAILQ_HEAD(carp_if, carp_softc_container); 327 328 SYSCTL_DECL(_net_inet_carp); 329 330 static int carp_opts[CARPCTL_MAXID] = { 0, 1, 0, 1, 0, 0 }; /* XXX for now */ 331 SYSCTL_INT(_net_inet_carp, CARPCTL_ALLOW, allow, CTLFLAG_RW, 332 &carp_opts[CARPCTL_ALLOW], 0, "Accept incoming CARP packets"); 333 SYSCTL_INT(_net_inet_carp, CARPCTL_PREEMPT, preempt, CTLFLAG_RW, 334 &carp_opts[CARPCTL_PREEMPT], 0, "high-priority backup preemption mode"); 335 SYSCTL_INT(_net_inet_carp, CARPCTL_LOG, log, CTLFLAG_RW, 336 &carp_opts[CARPCTL_LOG], 0, "log bad carp packets"); 337 SYSCTL_INT(_net_inet_carp, CARPCTL_ARPBALANCE, arpbalance, CTLFLAG_RW, 338 &carp_opts[CARPCTL_ARPBALANCE], 0, "balance arp responses"); 339 340 static int carp_suppress_preempt = 0; 341 SYSCTL_INT(_net_inet_carp, OID_AUTO, suppress_preempt, CTLFLAG_RD, 342 &carp_suppress_preempt, 0, "Preemption is suppressed"); 343 344 static struct carpstats carpstats; 345 SYSCTL_STRUCT(_net_inet_carp, CARPCTL_STATS, stats, CTLFLAG_RW, 346 &carpstats, carpstats, 347 "CARP statistics (struct carpstats, netinet/ip_carp.h)"); 348 349 #define CARP_LOG(...) do { \ 350 if (carp_opts[CARPCTL_LOG] > 0) \ 351 log(LOG_INFO, __VA_ARGS__); \ 352 } while (0) 353 354 #define CARP_DEBUG(...) do { \ 355 if (carp_opts[CARPCTL_LOG] > 1) \ 356 log(LOG_DEBUG, __VA_ARGS__); \ 357 } while (0) 358 359 static struct lwkt_token carp_listtok = LWKT_TOKEN_INITIALIZER(carp_list_token); 360 361 static void carp_hmac_prepare(struct carp_softc *); 362 static void carp_hmac_generate(struct carp_softc *, uint32_t *, 363 unsigned char *); 364 static int carp_hmac_verify(struct carp_softc *, uint32_t *, 365 unsigned char *); 366 static void carp_setroute(struct carp_softc *, int); 367 static void carp_proto_input_c(struct carp_softc *, struct mbuf *, 368 struct carp_header *, sa_family_t); 369 static int carp_clone_create(struct if_clone *, int, caddr_t); 370 static int carp_clone_destroy(struct ifnet *); 371 static void carp_detach(struct carp_softc *, boolean_t, boolean_t); 372 static void carp_prepare_ad(struct carp_softc *, struct carp_header *); 373 static void carp_send_ad_all(void); 374 static void carp_send_ad_timeout(void *); 375 static void carp_send_ad(struct carp_softc *); 376 static void carp_send_arp(struct carp_softc *); 377 static void carp_master_down_timeout(void *); 378 static void carp_master_down(struct carp_softc *); 379 static void carp_setrun(struct carp_softc *, sa_family_t); 380 static void carp_set_state(struct carp_softc *, int); 381 static struct ifnet *carp_forus(struct carp_if *, const uint8_t *); 382 383 static void carp_init(void *); 384 static int carp_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *); 385 static int carp_output(struct ifnet *, struct mbuf *, struct sockaddr *, 386 struct rtentry *); 387 static void carp_start(struct ifnet *, struct ifaltq_subque *); 388 389 static void carp_multicast_cleanup(struct carp_softc *); 390 static void carp_add_addr(struct carp_softc *, struct ifaddr *); 391 static void carp_del_addr(struct carp_softc *, struct ifaddr *); 392 static void carp_config_addr(struct carp_softc *, struct ifaddr *); 393 static void carp_link_addrs(struct carp_softc *, struct ifnet *, 394 struct ifaddr *); 395 static void carp_unlink_addrs(struct carp_softc *, struct ifnet *, 396 struct ifaddr *); 397 static void carp_update_addrs(struct carp_softc *, struct ifaddr *); 398 399 static int carp_config_vhaddr(struct carp_softc *, struct carp_vhaddr *, 400 struct in_ifaddr *); 401 static int carp_activate_vhaddr(struct carp_softc *, struct carp_vhaddr *, 402 struct ifnet *, struct in_ifaddr *, int); 403 static void carp_deactivate_vhaddr(struct carp_softc *, 404 struct carp_vhaddr *, boolean_t); 405 static int carp_addroute_vhaddr(struct carp_softc *, struct carp_vhaddr *); 406 static void carp_delroute_vhaddr(struct carp_softc *, struct carp_vhaddr *, 407 boolean_t); 408 409 #ifdef foo 410 static void carp_sc_state(struct carp_softc *); 411 #endif 412 #ifdef INET6 413 static void carp_send_na(struct carp_softc *); 414 #ifdef notyet 415 static int carp_set_addr6(struct carp_softc *, struct sockaddr_in6 *); 416 static int carp_del_addr6(struct carp_softc *, struct sockaddr_in6 *); 417 #endif 418 static void carp_multicast6_cleanup(struct carp_softc *); 419 #endif 420 static void carp_stop(struct carp_softc *, boolean_t); 421 static void carp_suspend(struct carp_softc *, boolean_t); 422 static void carp_ioctl_stop(struct carp_softc *); 423 static int carp_ioctl_setvh(struct carp_softc *, void *, struct ucred *); 424 static int carp_ioctl_getvh(struct carp_softc *, void *, struct ucred *); 425 static int carp_ioctl_getdevname(struct carp_softc *, struct ifdrv *); 426 static int carp_ioctl_getvhaddr(struct carp_softc *, struct ifdrv *); 427 428 static struct carp_if *carp_if_remove(struct carp_if *, struct carp_softc *); 429 static struct carp_if *carp_if_insert(struct carp_if *, struct carp_softc *); 430 static void carp_if_free(struct carp_if *); 431 432 static void carp_ifaddr(void *, struct ifnet *, enum ifaddr_event, 433 struct ifaddr *); 434 static void carp_ifdetach(void *, struct ifnet *); 435 436 static void carp_ifdetach_dispatch(netmsg_t); 437 static void carp_clone_destroy_dispatch(netmsg_t); 438 static void carp_init_dispatch(netmsg_t); 439 static void carp_ioctl_stop_dispatch(netmsg_t); 440 static void carp_ioctl_setvh_dispatch(netmsg_t); 441 static void carp_ioctl_getvh_dispatch(netmsg_t); 442 static void carp_ioctl_getdevname_dispatch(netmsg_t); 443 static void carp_ioctl_getvhaddr_dispatch(netmsg_t); 444 static void carp_send_ad_timeout_dispatch(netmsg_t); 445 static void carp_master_down_timeout_dispatch(netmsg_t); 446 447 static MALLOC_DEFINE(M_CARP, "CARP", "CARP interfaces"); 448 449 static LIST_HEAD(, carp_softc) carpif_list; 450 451 static struct if_clone carp_cloner = 452 IF_CLONE_INITIALIZER(CARP_IFNAME, carp_clone_create, carp_clone_destroy, 453 0, IF_MAXUNIT); 454 455 static uint8_t carp_etheraddr[ETHER_ADDR_LEN] = { 0, 0, 0x5e, 0, 1, 0 }; 456 457 static eventhandler_tag carp_ifdetach_event; 458 static eventhandler_tag carp_ifaddr_event; 459 460 static __inline void 461 carp_insert_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha_new) 462 { 463 struct carp_vhaddr *vha; 464 u_long new_addr, addr; 465 466 KKASSERT((vha_new->vha_flags & CARP_VHAF_ONLIST) == 0); 467 468 /* 469 * Virtual address list is sorted; smaller one first 470 */ 471 new_addr = ntohl(vha_new->vha_ia->ia_addr.sin_addr.s_addr); 472 473 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) { 474 addr = ntohl(vha->vha_ia->ia_addr.sin_addr.s_addr); 475 476 if (addr > new_addr) 477 break; 478 } 479 if (vha == NULL) 480 TAILQ_INSERT_TAIL(&sc->sc_vha_list, vha_new, vha_link); 481 else 482 TAILQ_INSERT_BEFORE(vha, vha_new, vha_link); 483 vha_new->vha_flags |= CARP_VHAF_ONLIST; 484 } 485 486 static __inline void 487 carp_remove_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha) 488 { 489 KKASSERT(vha->vha_flags & CARP_VHAF_ONLIST); 490 vha->vha_flags &= ~CARP_VHAF_ONLIST; 491 TAILQ_REMOVE(&sc->sc_vha_list, vha, vha_link); 492 } 493 494 static void 495 carp_hmac_prepare(struct carp_softc *sc) 496 { 497 uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT; 498 uint8_t vhid = sc->sc_vhid & 0xff; 499 int i; 500 #ifdef INET6 501 struct ifaddr_container *ifac; 502 struct in6_addr in6; 503 #endif 504 #ifdef INET 505 struct carp_vhaddr *vha; 506 #endif 507 508 /* XXX: possible race here */ 509 510 /* compute ipad from key */ 511 bzero(sc->sc_pad, sizeof(sc->sc_pad)); 512 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key)); 513 for (i = 0; i < sizeof(sc->sc_pad); i++) 514 sc->sc_pad[i] ^= 0x36; 515 516 /* precompute first part of inner hash */ 517 SHA1Init(&sc->sc_sha1); 518 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad)); 519 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version)); 520 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type)); 521 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid)); 522 #ifdef INET 523 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) { 524 SHA1Update(&sc->sc_sha1, 525 (const uint8_t *)&vha->vha_ia->ia_addr.sin_addr, 526 sizeof(struct in_addr)); 527 } 528 #endif /* INET */ 529 #ifdef INET6 530 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) { 531 struct ifaddr *ifa = ifac->ifa; 532 533 if (ifa->ifa_addr->sa_family == AF_INET6) { 534 in6 = ifatoia6(ifa)->ia_addr.sin6_addr; 535 in6_clearscope(&in6); 536 SHA1Update(&sc->sc_sha1, (void *)&in6, sizeof(in6)); 537 } 538 } 539 #endif /* INET6 */ 540 541 /* convert ipad to opad */ 542 for (i = 0; i < sizeof(sc->sc_pad); i++) 543 sc->sc_pad[i] ^= 0x36 ^ 0x5c; 544 } 545 546 static void 547 carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2], 548 unsigned char md[20]) 549 { 550 SHA1_CTX sha1ctx; 551 552 /* fetch first half of inner hash */ 553 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx)); 554 555 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter)); 556 SHA1Final(md, &sha1ctx); 557 558 /* outer hash */ 559 SHA1Init(&sha1ctx); 560 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad)); 561 SHA1Update(&sha1ctx, md, 20); 562 SHA1Final(md, &sha1ctx); 563 } 564 565 static int 566 carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2], 567 unsigned char md[20]) 568 { 569 unsigned char md2[20]; 570 571 carp_hmac_generate(sc, counter, md2); 572 return (bcmp(md, md2, sizeof(md2))); 573 } 574 575 static void 576 carp_setroute(struct carp_softc *sc, int cmd) 577 { 578 #ifdef INET6 579 struct ifaddr_container *ifac; 580 #endif 581 struct carp_vhaddr *vha; 582 583 KKASSERT(cmd == RTM_DELETE || cmd == RTM_ADD); 584 585 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) { 586 if (vha->vha_iaback == NULL) 587 continue; 588 if (cmd == RTM_DELETE) 589 carp_delroute_vhaddr(sc, vha, FALSE); 590 else 591 carp_addroute_vhaddr(sc, vha); 592 } 593 594 #ifdef INET6 595 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) { 596 struct ifaddr *ifa = ifac->ifa; 597 598 if (ifa->ifa_addr->sa_family == AF_INET6) { 599 if (cmd == RTM_ADD) 600 in6_ifaddloop(ifa); 601 else 602 in6_ifremloop(ifa); 603 } 604 } 605 #endif /* INET6 */ 606 } 607 608 static int 609 carp_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused) 610 { 611 struct carp_softc *sc; 612 struct ifnet *ifp; 613 614 sc = kmalloc(sizeof(*sc), M_CARP, M_WAITOK | M_ZERO); 615 ifp = &sc->sc_if; 616 617 sc->sc_suppress = 0; 618 sc->sc_advbase = CARP_DFLTINTV; 619 sc->sc_vhid = -1; /* required setting */ 620 sc->sc_advskew = 0; 621 sc->sc_init_counter = 1; 622 sc->sc_naddrs = 0; 623 sc->sc_naddrs6 = 0; 624 625 TAILQ_INIT(&sc->sc_vha_list); 626 627 #ifdef INET6 628 sc->sc_im6o.im6o_multicast_hlim = CARP_DFLTTL; 629 #endif 630 631 callout_init_mp(&sc->sc_ad_tmo); 632 netmsg_init(&sc->sc_ad_msg.base, NULL, &netisr_adone_rport, 633 MSGF_DROPABLE | MSGF_PRIORITY, carp_send_ad_timeout_dispatch); 634 sc->sc_ad_msg.nc_softc = sc; 635 636 callout_init_mp(&sc->sc_md_tmo); 637 callout_init_mp(&sc->sc_md6_tmo); 638 netmsg_init(&sc->sc_md_msg.base, NULL, &netisr_adone_rport, 639 MSGF_DROPABLE | MSGF_PRIORITY, carp_master_down_timeout_dispatch); 640 sc->sc_md_msg.nc_softc = sc; 641 642 if_initname(ifp, CARP_IFNAME, unit); 643 ifp->if_softc = sc; 644 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 645 ifp->if_init = carp_init; 646 ifp->if_ioctl = carp_ioctl; 647 ifp->if_start = carp_start; 648 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen); 649 ifq_set_ready(&ifp->if_snd); 650 651 ether_ifattach(ifp, carp_etheraddr, NULL); 652 653 ifp->if_type = IFT_CARP; 654 ifp->if_output = carp_output; 655 656 lwkt_gettoken(&carp_listtok); 657 LIST_INSERT_HEAD(&carpif_list, sc, sc_next); 658 lwkt_reltoken(&carp_listtok); 659 660 return (0); 661 } 662 663 static void 664 carp_clone_destroy_dispatch(netmsg_t msg) 665 { 666 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg; 667 struct carp_softc *sc = cmsg->nc_softc; 668 669 sc->sc_dead = TRUE; 670 carp_detach(sc, TRUE, FALSE); 671 672 callout_stop_sync(&sc->sc_ad_tmo); 673 callout_stop_sync(&sc->sc_md_tmo); 674 callout_stop_sync(&sc->sc_md6_tmo); 675 676 crit_enter(); 677 lwkt_dropmsg(&sc->sc_ad_msg.base.lmsg); 678 lwkt_dropmsg(&sc->sc_md_msg.base.lmsg); 679 crit_exit(); 680 681 lwkt_replymsg(&cmsg->base.lmsg, 0); 682 } 683 684 static int 685 carp_clone_destroy(struct ifnet *ifp) 686 { 687 struct carp_softc *sc = ifp->if_softc; 688 struct netmsg_carp cmsg; 689 690 bzero(&cmsg, sizeof(cmsg)); 691 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0, 692 carp_clone_destroy_dispatch); 693 cmsg.nc_softc = sc; 694 695 lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0); 696 697 lwkt_gettoken(&carp_listtok); 698 LIST_REMOVE(sc, sc_next); 699 lwkt_reltoken(&carp_listtok); 700 701 bpfdetach(ifp); 702 if_detach(ifp); 703 704 KASSERT(sc->sc_naddrs == 0, ("certain inet address is still active")); 705 kfree(sc, M_CARP); 706 707 return 0; 708 } 709 710 static struct carp_if * 711 carp_if_remove(struct carp_if *ocif, struct carp_softc *sc) 712 { 713 struct carp_softc_container *oscc, *scc; 714 struct carp_if *cif; 715 int count = 0; 716 #ifdef INVARIANTS 717 int found = 0; 718 #endif 719 720 TAILQ_FOREACH(oscc, ocif, scc_link) { 721 ++count; 722 #ifdef INVARIANTS 723 if (oscc->scc_softc == sc) 724 found = 1; 725 #endif 726 } 727 KASSERT(found, ("%s carp_softc is not on carp_if", __func__)); 728 729 if (count == 1) { 730 /* Last one is going to be unlinked */ 731 return NULL; 732 } 733 734 cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO); 735 TAILQ_INIT(cif); 736 737 TAILQ_FOREACH(oscc, ocif, scc_link) { 738 if (oscc->scc_softc == sc) 739 continue; 740 741 scc = kmalloc(sizeof(*scc), M_CARP, M_WAITOK | M_ZERO); 742 scc->scc_softc = oscc->scc_softc; 743 TAILQ_INSERT_TAIL(cif, scc, scc_link); 744 } 745 746 return cif; 747 } 748 749 static struct carp_if * 750 carp_if_insert(struct carp_if *ocif, struct carp_softc *sc) 751 { 752 struct carp_softc_container *oscc; 753 int onlist; 754 755 onlist = 0; 756 if (ocif != NULL) { 757 TAILQ_FOREACH(oscc, ocif, scc_link) { 758 if (oscc->scc_softc == sc) 759 onlist = 1; 760 } 761 } 762 763 #ifdef INVARIANTS 764 if (sc->sc_carpdev != NULL) { 765 KASSERT(onlist, ("%s is not on %s carp list", 766 sc->sc_if.if_xname, sc->sc_carpdev->if_xname)); 767 } else { 768 KASSERT(!onlist, ("%s is already on carp list", 769 sc->sc_if.if_xname)); 770 } 771 #endif 772 773 if (!onlist) { 774 struct carp_if *cif; 775 struct carp_softc_container *new_scc, *scc; 776 int inserted = 0; 777 778 cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO); 779 TAILQ_INIT(cif); 780 781 new_scc = kmalloc(sizeof(*new_scc), M_CARP, M_WAITOK | M_ZERO); 782 new_scc->scc_softc = sc; 783 784 if (ocif != NULL) { 785 TAILQ_FOREACH(oscc, ocif, scc_link) { 786 if (!inserted && 787 oscc->scc_softc->sc_vhid > sc->sc_vhid) { 788 TAILQ_INSERT_TAIL(cif, new_scc, 789 scc_link); 790 inserted = 1; 791 } 792 793 scc = kmalloc(sizeof(*scc), M_CARP, 794 M_WAITOK | M_ZERO); 795 scc->scc_softc = oscc->scc_softc; 796 TAILQ_INSERT_TAIL(cif, scc, scc_link); 797 } 798 } 799 if (!inserted) 800 TAILQ_INSERT_TAIL(cif, new_scc, scc_link); 801 802 return cif; 803 } else { 804 return ocif; 805 } 806 } 807 808 static void 809 carp_if_free(struct carp_if *cif) 810 { 811 struct carp_softc_container *scc; 812 813 while ((scc = TAILQ_FIRST(cif)) != NULL) { 814 TAILQ_REMOVE(cif, scc, scc_link); 815 kfree(scc, M_CARP); 816 } 817 kfree(cif, M_CARP); 818 } 819 820 static void 821 carp_detach(struct carp_softc *sc, boolean_t detach, boolean_t del_iaback) 822 { 823 carp_suspend(sc, detach); 824 825 carp_multicast_cleanup(sc); 826 #ifdef INET6 827 carp_multicast6_cleanup(sc); 828 #endif 829 830 if (!sc->sc_dead && detach) { 831 struct carp_vhaddr *vha; 832 833 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) 834 carp_deactivate_vhaddr(sc, vha, del_iaback); 835 KKASSERT(sc->sc_naddrs == 0); 836 } 837 838 if (sc->sc_carpdev != NULL) { 839 struct ifnet *ifp = sc->sc_carpdev; 840 struct carp_if *ocif = ifp->if_carp; 841 842 ifp->if_carp = carp_if_remove(ocif, sc); 843 KASSERT(ifp->if_carp != ocif, 844 ("%s carp_if_remove failed", __func__)); 845 846 sc->sc_carpdev = NULL; 847 sc->sc_ia = NULL; 848 849 /* 850 * Make sure that all protocol threads see the 851 * sc_carpdev and if_carp changes 852 */ 853 netmsg_service_sync(); 854 855 if (ifp->if_carp == NULL) { 856 /* 857 * No more carp interfaces using 858 * ifp as the backing interface, 859 * move it out of promiscous mode. 860 */ 861 ifpromisc(ifp, 0); 862 } 863 864 /* 865 * The old carp list could be safely free now, 866 * since no one can access it. 867 */ 868 carp_if_free(ocif); 869 } 870 } 871 872 static void 873 carp_ifdetach_dispatch(netmsg_t msg) 874 { 875 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg; 876 struct ifnet *ifp = cmsg->nc_carpdev; 877 878 while (ifp->if_carp) { 879 struct carp_softc_container *scc; 880 881 scc = TAILQ_FIRST((struct carp_if *)(ifp->if_carp)); 882 carp_detach(scc->scc_softc, TRUE, TRUE); 883 } 884 lwkt_replymsg(&cmsg->base.lmsg, 0); 885 } 886 887 /* Detach an interface from the carp. */ 888 static void 889 carp_ifdetach(void *arg __unused, struct ifnet *ifp) 890 { 891 struct netmsg_carp cmsg; 892 893 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp); 894 895 bzero(&cmsg, sizeof(cmsg)); 896 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0, 897 carp_ifdetach_dispatch); 898 cmsg.nc_carpdev = ifp; 899 900 lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0); 901 } 902 903 /* 904 * process input packet. 905 * we have rearranged checks order compared to the rfc, 906 * but it seems more efficient this way or not possible otherwise. 907 */ 908 int 909 carp_proto_input(struct mbuf **mp, int *offp, int proto) 910 { 911 struct mbuf *m = *mp; 912 struct ip *ip = mtod(m, struct ip *); 913 struct ifnet *ifp = m->m_pkthdr.rcvif; 914 struct carp_header *ch; 915 struct carp_softc *sc; 916 int len, iphlen; 917 918 iphlen = *offp; 919 *mp = NULL; 920 921 carpstats.carps_ipackets++; 922 923 if (!carp_opts[CARPCTL_ALLOW]) { 924 m_freem(m); 925 goto back; 926 } 927 928 /* Check if received on a valid carp interface */ 929 if (ifp->if_type != IFT_CARP) { 930 carpstats.carps_badif++; 931 CARP_LOG("carp_proto_input: packet received on non-carp " 932 "interface: %s\n", ifp->if_xname); 933 m_freem(m); 934 goto back; 935 } 936 937 if (!CARP_IS_RUNNING(ifp)) { 938 carpstats.carps_badif++; 939 CARP_LOG("carp_proto_input: packet received on stopped carp " 940 "interface: %s\n", ifp->if_xname); 941 m_freem(m); 942 goto back; 943 } 944 945 sc = ifp->if_softc; 946 if (sc->sc_carpdev == NULL) { 947 carpstats.carps_badif++; 948 CARP_LOG("carp_proto_input: packet received on defunc carp " 949 "interface: %s\n", ifp->if_xname); 950 m_freem(m); 951 goto back; 952 } 953 954 if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 955 carpstats.carps_badif++; 956 CARP_LOG("carp_proto_input: non-mcast packet on " 957 "interface: %s\n", ifp->if_xname); 958 m_freem(m); 959 goto back; 960 } 961 962 /* Verify that the IP TTL is CARP_DFLTTL. */ 963 if (ip->ip_ttl != CARP_DFLTTL) { 964 carpstats.carps_badttl++; 965 CARP_LOG("carp_proto_input: received ttl %d != %d on %s\n", 966 ip->ip_ttl, CARP_DFLTTL, ifp->if_xname); 967 m_freem(m); 968 goto back; 969 } 970 971 /* Minimal CARP packet size */ 972 len = iphlen + sizeof(*ch); 973 974 /* 975 * Verify that the received packet length is 976 * not less than the CARP header 977 */ 978 if (m->m_pkthdr.len < len) { 979 carpstats.carps_badlen++; 980 CARP_LOG("packet too short %d on %s\n", m->m_pkthdr.len, 981 ifp->if_xname); 982 m_freem(m); 983 goto back; 984 } 985 986 /* Make sure that CARP header is contiguous */ 987 if (len > m->m_len) { 988 m = m_pullup(m, len); 989 if (m == NULL) { 990 carpstats.carps_hdrops++; 991 CARP_LOG("carp_proto_input: m_pullup failed\n"); 992 goto back; 993 } 994 ip = mtod(m, struct ip *); 995 } 996 ch = (struct carp_header *)((uint8_t *)ip + iphlen); 997 998 /* Verify the CARP checksum */ 999 if (in_cksum_skip(m, len, iphlen)) { 1000 carpstats.carps_badsum++; 1001 CARP_LOG("carp_proto_input: checksum failed on %s\n", 1002 ifp->if_xname); 1003 m_freem(m); 1004 goto back; 1005 } 1006 carp_proto_input_c(sc, m, ch, AF_INET); 1007 back: 1008 return(IPPROTO_DONE); 1009 } 1010 1011 #ifdef INET6 1012 int 1013 carp6_proto_input(struct mbuf **mp, int *offp, int proto) 1014 { 1015 struct mbuf *m = *mp; 1016 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1017 struct ifnet *ifp = m->m_pkthdr.rcvif; 1018 struct carp_header *ch; 1019 struct carp_softc *sc; 1020 u_int len; 1021 1022 carpstats.carps_ipackets6++; 1023 1024 if (!carp_opts[CARPCTL_ALLOW]) { 1025 m_freem(m); 1026 goto back; 1027 } 1028 1029 /* check if received on a valid carp interface */ 1030 if (ifp->if_type != IFT_CARP) { 1031 carpstats.carps_badif++; 1032 CARP_LOG("carp6_proto_input: packet received on non-carp " 1033 "interface: %s\n", ifp->if_xname); 1034 m_freem(m); 1035 goto back; 1036 } 1037 1038 if (!CARP_IS_RUNNING(ifp)) { 1039 carpstats.carps_badif++; 1040 CARP_LOG("carp_proto_input: packet received on stopped carp " 1041 "interface: %s\n", ifp->if_xname); 1042 m_freem(m); 1043 goto back; 1044 } 1045 1046 sc = ifp->if_softc; 1047 if (sc->sc_carpdev == NULL) { 1048 carpstats.carps_badif++; 1049 CARP_LOG("carp6_proto_input: packet received on defunc-carp " 1050 "interface: %s\n", ifp->if_xname); 1051 m_freem(m); 1052 goto back; 1053 } 1054 1055 /* verify that the IP TTL is 255 */ 1056 if (ip6->ip6_hlim != CARP_DFLTTL) { 1057 carpstats.carps_badttl++; 1058 CARP_LOG("carp6_proto_input: received ttl %d != 255 on %s\n", 1059 ip6->ip6_hlim, ifp->if_xname); 1060 m_freem(m); 1061 goto back; 1062 } 1063 1064 /* verify that we have a complete carp packet */ 1065 len = m->m_len; 1066 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch)); 1067 if (ch == NULL) { 1068 carpstats.carps_badlen++; 1069 CARP_LOG("carp6_proto_input: packet size %u too small\n", len); 1070 goto back; 1071 } 1072 1073 /* verify the CARP checksum */ 1074 if (in_cksum_range(m, 0, *offp, sizeof(*ch))) { 1075 carpstats.carps_badsum++; 1076 CARP_LOG("carp6_proto_input: checksum failed, on %s\n", 1077 ifp->if_xname); 1078 m_freem(m); 1079 goto back; 1080 } 1081 1082 carp_proto_input_c(sc, m, ch, AF_INET6); 1083 back: 1084 return (IPPROTO_DONE); 1085 } 1086 #endif /* INET6 */ 1087 1088 static void 1089 carp_proto_input_c(struct carp_softc *sc, struct mbuf *m, 1090 struct carp_header *ch, sa_family_t af) 1091 { 1092 struct ifnet *cifp; 1093 uint64_t tmp_counter; 1094 struct timeval sc_tv, ch_tv; 1095 1096 if (sc->sc_vhid != ch->carp_vhid) { 1097 /* 1098 * CARP uses multicast, however, multicast packets 1099 * are tapped to all CARP interfaces on the physical 1100 * interface receiving the CARP packets, so we don't 1101 * update any stats here. 1102 */ 1103 m_freem(m); 1104 return; 1105 } 1106 cifp = &sc->sc_if; 1107 1108 /* verify the CARP version. */ 1109 if (ch->carp_version != CARP_VERSION) { 1110 carpstats.carps_badver++; 1111 CARP_LOG("%s; invalid version %d\n", cifp->if_xname, 1112 ch->carp_version); 1113 m_freem(m); 1114 return; 1115 } 1116 1117 /* verify the hash */ 1118 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) { 1119 carpstats.carps_badauth++; 1120 CARP_LOG("%s: incorrect hash\n", cifp->if_xname); 1121 m_freem(m); 1122 return; 1123 } 1124 1125 tmp_counter = ntohl(ch->carp_counter[0]); 1126 tmp_counter = tmp_counter<<32; 1127 tmp_counter += ntohl(ch->carp_counter[1]); 1128 1129 /* XXX Replay protection goes here */ 1130 1131 sc->sc_init_counter = 0; 1132 sc->sc_counter = tmp_counter; 1133 1134 sc_tv.tv_sec = sc->sc_advbase; 1135 if (carp_suppress_preempt && sc->sc_advskew < 240) 1136 sc_tv.tv_usec = 240 * 1000000 / 256; 1137 else 1138 sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256; 1139 ch_tv.tv_sec = ch->carp_advbase; 1140 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256; 1141 1142 switch (sc->sc_state) { 1143 case INIT: 1144 break; 1145 1146 case MASTER: 1147 /* 1148 * If we receive an advertisement from a master who's going to 1149 * be more frequent than us, go into BACKUP state. 1150 */ 1151 if (timevalcmp(&sc_tv, &ch_tv, >) || 1152 timevalcmp(&sc_tv, &ch_tv, ==)) { 1153 callout_stop(&sc->sc_ad_tmo); 1154 CARP_DEBUG("%s: MASTER -> BACKUP " 1155 "(more frequent advertisement received)\n", 1156 cifp->if_xname); 1157 carp_set_state(sc, BACKUP); 1158 carp_setrun(sc, 0); 1159 carp_setroute(sc, RTM_DELETE); 1160 } 1161 break; 1162 1163 case BACKUP: 1164 /* 1165 * If we're pre-empting masters who advertise slower than us, 1166 * and this one claims to be slower, treat him as down. 1167 */ 1168 if (carp_opts[CARPCTL_PREEMPT] && 1169 timevalcmp(&sc_tv, &ch_tv, <)) { 1170 CARP_DEBUG("%s: BACKUP -> MASTER " 1171 "(preempting a slower master)\n", cifp->if_xname); 1172 carp_master_down(sc); 1173 break; 1174 } 1175 1176 /* 1177 * If the master is going to advertise at such a low frequency 1178 * that he's guaranteed to time out, we'd might as well just 1179 * treat him as timed out now. 1180 */ 1181 sc_tv.tv_sec = sc->sc_advbase * 3; 1182 if (timevalcmp(&sc_tv, &ch_tv, <)) { 1183 CARP_DEBUG("%s: BACKUP -> MASTER (master timed out)\n", 1184 cifp->if_xname); 1185 carp_master_down(sc); 1186 break; 1187 } 1188 1189 /* 1190 * Otherwise, we reset the counter and wait for the next 1191 * advertisement. 1192 */ 1193 carp_setrun(sc, af); 1194 break; 1195 } 1196 m_freem(m); 1197 } 1198 1199 struct mbuf * 1200 carp_input(void *v, struct mbuf *m) 1201 { 1202 struct carp_if *cif = v; 1203 struct ether_header *eh; 1204 struct carp_softc_container *scc; 1205 struct ifnet *ifp; 1206 1207 eh = mtod(m, struct ether_header *); 1208 1209 ifp = carp_forus(cif, eh->ether_dhost); 1210 if (ifp != NULL) { 1211 ether_reinput_oncpu(ifp, m, REINPUT_RUNBPF); 1212 return NULL; 1213 } 1214 1215 if ((m->m_flags & (M_BCAST | M_MCAST)) == 0) 1216 return m; 1217 1218 /* 1219 * XXX Should really check the list of multicast addresses 1220 * for each CARP interface _before_ copying. 1221 */ 1222 TAILQ_FOREACH(scc, cif, scc_link) { 1223 struct carp_softc *sc = scc->scc_softc; 1224 struct mbuf *m0; 1225 1226 if ((sc->sc_if.if_flags & IFF_UP) == 0) 1227 continue; 1228 1229 m0 = m_dup(m, MB_DONTWAIT); 1230 if (m0 == NULL) 1231 continue; 1232 1233 ether_reinput_oncpu(&sc->sc_if, m0, REINPUT_RUNBPF); 1234 } 1235 return m; 1236 } 1237 1238 static void 1239 carp_prepare_ad(struct carp_softc *sc, struct carp_header *ch) 1240 { 1241 if (sc->sc_init_counter) { 1242 /* this could also be seconds since unix epoch */ 1243 sc->sc_counter = karc4random(); 1244 sc->sc_counter = sc->sc_counter << 32; 1245 sc->sc_counter += karc4random(); 1246 } else { 1247 sc->sc_counter++; 1248 } 1249 1250 ch->carp_counter[0] = htonl((sc->sc_counter >> 32) & 0xffffffff); 1251 ch->carp_counter[1] = htonl(sc->sc_counter & 0xffffffff); 1252 1253 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md); 1254 } 1255 1256 static void 1257 carp_send_ad_all(void) 1258 { 1259 struct carp_softc *sc; 1260 1261 LIST_FOREACH(sc, &carpif_list, sc_next) { 1262 if (sc->sc_carpdev == NULL) 1263 continue; 1264 1265 if (CARP_IS_RUNNING(&sc->sc_if) && sc->sc_state == MASTER) 1266 carp_send_ad(sc); 1267 } 1268 } 1269 1270 static void 1271 carp_send_ad_timeout(void *xsc) 1272 { 1273 struct carp_softc *sc = xsc; 1274 struct netmsg_carp *cmsg = &sc->sc_ad_msg; 1275 1276 KASSERT(mycpuid == 0, ("%s not on cpu0 but on cpu%d", 1277 __func__, mycpuid)); 1278 1279 crit_enter(); 1280 if (cmsg->base.lmsg.ms_flags & MSGF_DONE) 1281 lwkt_sendmsg(netisr_cpuport(0), &cmsg->base.lmsg); 1282 crit_exit(); 1283 } 1284 1285 static void 1286 carp_send_ad_timeout_dispatch(netmsg_t msg) 1287 { 1288 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg; 1289 struct carp_softc *sc = cmsg->nc_softc; 1290 1291 /* Reply ASAP */ 1292 crit_enter(); 1293 lwkt_replymsg(&cmsg->base.lmsg, 0); 1294 crit_exit(); 1295 1296 carp_send_ad(sc); 1297 } 1298 1299 static void 1300 carp_send_ad(struct carp_softc *sc) 1301 { 1302 struct ifnet *cifp = &sc->sc_if; 1303 struct carp_header ch; 1304 struct timeval tv; 1305 struct carp_header *ch_ptr; 1306 struct mbuf *m; 1307 int len, advbase, advskew; 1308 1309 if (!CARP_IS_RUNNING(cifp)) { 1310 /* Bow out */ 1311 advbase = 255; 1312 advskew = 255; 1313 } else { 1314 advbase = sc->sc_advbase; 1315 if (!carp_suppress_preempt || sc->sc_advskew > 240) 1316 advskew = sc->sc_advskew; 1317 else 1318 advskew = 240; 1319 tv.tv_sec = advbase; 1320 tv.tv_usec = advskew * 1000000 / 256; 1321 } 1322 1323 ch.carp_version = CARP_VERSION; 1324 ch.carp_type = CARP_ADVERTISEMENT; 1325 ch.carp_vhid = sc->sc_vhid; 1326 ch.carp_advbase = advbase; 1327 ch.carp_advskew = advskew; 1328 ch.carp_authlen = 7; /* XXX DEFINE */ 1329 ch.carp_pad1 = 0; /* must be zero */ 1330 ch.carp_cksum = 0; 1331 1332 #ifdef INET 1333 if (sc->sc_ia != NULL) { 1334 struct ip *ip; 1335 1336 MGETHDR(m, MB_DONTWAIT, MT_HEADER); 1337 if (m == NULL) { 1338 IFNET_STAT_INC(cifp, oerrors, 1); 1339 carpstats.carps_onomem++; 1340 /* XXX maybe less ? */ 1341 if (advbase != 255 || advskew != 255) 1342 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv), 1343 carp_send_ad_timeout, sc); 1344 return; 1345 } 1346 len = sizeof(*ip) + sizeof(ch); 1347 m->m_pkthdr.len = len; 1348 m->m_pkthdr.rcvif = NULL; 1349 m->m_len = len; 1350 MH_ALIGN(m, m->m_len); 1351 m->m_flags |= M_MCAST; 1352 ip = mtod(m, struct ip *); 1353 ip->ip_v = IPVERSION; 1354 ip->ip_hl = sizeof(*ip) >> 2; 1355 ip->ip_tos = IPTOS_LOWDELAY; 1356 ip->ip_len = len; 1357 ip->ip_id = ip_newid(); 1358 ip->ip_off = IP_DF; 1359 ip->ip_ttl = CARP_DFLTTL; 1360 ip->ip_p = IPPROTO_CARP; 1361 ip->ip_sum = 0; 1362 ip->ip_src = sc->sc_ia->ia_addr.sin_addr; 1363 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP); 1364 1365 ch_ptr = (struct carp_header *)(&ip[1]); 1366 bcopy(&ch, ch_ptr, sizeof(ch)); 1367 carp_prepare_ad(sc, ch_ptr); 1368 ch_ptr->carp_cksum = in_cksum_skip(m, len, sizeof(*ip)); 1369 1370 getmicrotime(&cifp->if_lastchange); 1371 IFNET_STAT_INC(cifp, opackets, 1); 1372 IFNET_STAT_INC(cifp, obytes, len); 1373 carpstats.carps_opackets++; 1374 1375 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) { 1376 IFNET_STAT_INC(cifp, oerrors, 1); 1377 if (sc->sc_sendad_errors < INT_MAX) 1378 sc->sc_sendad_errors++; 1379 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) { 1380 carp_suppress_preempt++; 1381 if (carp_suppress_preempt == 1) { 1382 carp_send_ad_all(); 1383 } 1384 } 1385 sc->sc_sendad_success = 0; 1386 } else { 1387 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) { 1388 if (++sc->sc_sendad_success >= 1389 CARP_SENDAD_MIN_SUCCESS) { 1390 carp_suppress_preempt--; 1391 sc->sc_sendad_errors = 0; 1392 } 1393 } else { 1394 sc->sc_sendad_errors = 0; 1395 } 1396 } 1397 } 1398 #endif /* INET */ 1399 #ifdef INET6 1400 if (sc->sc_ia6) { 1401 struct ip6_hdr *ip6; 1402 1403 MGETHDR(m, MB_DONTWAIT, MT_HEADER); 1404 if (m == NULL) { 1405 IFNET_STAT_INC(cifp, oerrors, 1); 1406 carpstats.carps_onomem++; 1407 /* XXX maybe less ? */ 1408 if (advbase != 255 || advskew != 255) 1409 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv), 1410 carp_send_ad_timeout, sc); 1411 return; 1412 } 1413 len = sizeof(*ip6) + sizeof(ch); 1414 m->m_pkthdr.len = len; 1415 m->m_pkthdr.rcvif = NULL; 1416 m->m_len = len; 1417 MH_ALIGN(m, m->m_len); 1418 m->m_flags |= M_MCAST; 1419 ip6 = mtod(m, struct ip6_hdr *); 1420 bzero(ip6, sizeof(*ip6)); 1421 ip6->ip6_vfc |= IPV6_VERSION; 1422 ip6->ip6_hlim = CARP_DFLTTL; 1423 ip6->ip6_nxt = IPPROTO_CARP; 1424 bcopy(&sc->sc_ia6->ia_addr.sin6_addr, &ip6->ip6_src, 1425 sizeof(struct in6_addr)); 1426 /* set the multicast destination */ 1427 1428 ip6->ip6_dst.s6_addr16[0] = htons(0xff02); 1429 ip6->ip6_dst.s6_addr8[15] = 0x12; 1430 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) { 1431 IFNET_STAT_INC(cifp, oerrors, 1); 1432 m_freem(m); 1433 CARP_LOG("%s: in6_setscope failed\n", __func__); 1434 return; 1435 } 1436 1437 ch_ptr = (struct carp_header *)(&ip6[1]); 1438 bcopy(&ch, ch_ptr, sizeof(ch)); 1439 carp_prepare_ad(sc, ch_ptr); 1440 ch_ptr->carp_cksum = in_cksum_skip(m, len, sizeof(*ip6)); 1441 1442 getmicrotime(&cifp->if_lastchange); 1443 IFNET_STAT_INC(cifp, opackets, 1); 1444 IFNET_STAT_INC(cifp, obytes, len); 1445 carpstats.carps_opackets6++; 1446 1447 if (ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL)) { 1448 IFNET_STAT_INC(cifp, oerrors, 1); 1449 if (sc->sc_sendad_errors < INT_MAX) 1450 sc->sc_sendad_errors++; 1451 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) { 1452 carp_suppress_preempt++; 1453 if (carp_suppress_preempt == 1) { 1454 carp_send_ad_all(); 1455 } 1456 } 1457 sc->sc_sendad_success = 0; 1458 } else { 1459 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) { 1460 if (++sc->sc_sendad_success >= 1461 CARP_SENDAD_MIN_SUCCESS) { 1462 carp_suppress_preempt--; 1463 sc->sc_sendad_errors = 0; 1464 } 1465 } else { 1466 sc->sc_sendad_errors = 0; 1467 } 1468 } 1469 } 1470 #endif /* INET6 */ 1471 1472 if (advbase != 255 || advskew != 255) 1473 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv), 1474 carp_send_ad_timeout, sc); 1475 } 1476 1477 /* 1478 * Broadcast a gratuitous ARP request containing 1479 * the virtual router MAC address for each IP address 1480 * associated with the virtual router. 1481 */ 1482 static void 1483 carp_send_arp(struct carp_softc *sc) 1484 { 1485 const struct carp_vhaddr *vha; 1486 1487 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) { 1488 if (vha->vha_iaback == NULL) 1489 continue; 1490 arp_gratuitous(&sc->sc_if, &vha->vha_ia->ia_ifa); 1491 } 1492 } 1493 1494 #ifdef INET6 1495 static void 1496 carp_send_na(struct carp_softc *sc) 1497 { 1498 struct ifaddr_container *ifac; 1499 struct in6_addr *in6; 1500 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT; 1501 1502 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) { 1503 struct ifaddr *ifa = ifac->ifa; 1504 1505 if (ifa->ifa_addr->sa_family != AF_INET6) 1506 continue; 1507 1508 in6 = &ifatoia6(ifa)->ia_addr.sin6_addr; 1509 nd6_na_output(sc->sc_carpdev, &mcast, in6, 1510 ND_NA_FLAG_OVERRIDE, 1, NULL); 1511 DELAY(1000); /* XXX */ 1512 } 1513 } 1514 #endif /* INET6 */ 1515 1516 static __inline const struct carp_vhaddr * 1517 carp_find_addr(const struct carp_softc *sc, const struct in_addr *addr) 1518 { 1519 struct carp_vhaddr *vha; 1520 1521 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) { 1522 if (vha->vha_iaback == NULL) 1523 continue; 1524 1525 if (vha->vha_ia->ia_addr.sin_addr.s_addr == addr->s_addr) 1526 return vha; 1527 } 1528 return NULL; 1529 } 1530 1531 #ifdef notyet 1532 static int 1533 carp_iamatch_balance(const struct carp_if *cif, const struct in_addr *itaddr, 1534 const struct in_addr *isaddr, uint8_t **enaddr) 1535 { 1536 const struct carp_softc *vh; 1537 int index, count = 0; 1538 1539 /* 1540 * XXX proof of concept implementation. 1541 * We use the source ip to decide which virtual host should 1542 * handle the request. If we're master of that virtual host, 1543 * then we respond, otherwise, just drop the arp packet on 1544 * the floor. 1545 */ 1546 1547 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) { 1548 if (!CARP_IS_RUNNING(&vh->sc_if)) 1549 continue; 1550 1551 if (carp_find_addr(vh, itaddr) != NULL) 1552 count++; 1553 } 1554 if (count == 0) 1555 return 0; 1556 1557 /* this should be a hash, like pf_hash() */ 1558 index = ntohl(isaddr->s_addr) % count; 1559 count = 0; 1560 1561 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) { 1562 if (!CARP_IS_RUNNING(&vh->sc_if)) 1563 continue; 1564 1565 if (carp_find_addr(vh, itaddr) == NULL) 1566 continue; 1567 1568 if (count == index) { 1569 if (vh->sc_state == MASTER) { 1570 *enaddr = IF_LLADDR(&vh->sc_if); 1571 return 1; 1572 } else { 1573 return 0; 1574 } 1575 } 1576 count++; 1577 } 1578 return 0; 1579 } 1580 #endif 1581 1582 int 1583 carp_iamatch(const struct in_ifaddr *ia) 1584 { 1585 const struct carp_softc *sc = ia->ia_ifp->if_softc; 1586 1587 KASSERT(&curthread->td_msgport == netisr_cpuport(0), 1588 ("not in netisr0")); 1589 1590 #ifdef notyet 1591 if (carp_opts[CARPCTL_ARPBALANCE]) 1592 return carp_iamatch_balance(cif, itaddr, isaddr, enaddr); 1593 #endif 1594 1595 if (!CARP_IS_RUNNING(&sc->sc_if) || sc->sc_state != MASTER) 1596 return 0; 1597 1598 return 1; 1599 } 1600 1601 #ifdef INET6 1602 struct ifaddr * 1603 carp_iamatch6(void *v, struct in6_addr *taddr) 1604 { 1605 #ifdef foo 1606 struct carp_if *cif = v; 1607 struct carp_softc *vh; 1608 1609 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) { 1610 struct ifaddr_container *ifac; 1611 1612 TAILQ_FOREACH(ifac, &vh->sc_if.if_addrheads[mycpuid], 1613 ifa_link) { 1614 struct ifaddr *ifa = ifac->ifa; 1615 1616 if (IN6_ARE_ADDR_EQUAL(taddr, 1617 &ifatoia6(ifa)->ia_addr.sin6_addr) && 1618 CARP_IS_RUNNING(&vh->sc_if) && 1619 vh->sc_state == MASTER) { 1620 return (ifa); 1621 } 1622 } 1623 } 1624 #endif 1625 return (NULL); 1626 } 1627 1628 void * 1629 carp_macmatch6(void *v, struct mbuf *m, const struct in6_addr *taddr) 1630 { 1631 #ifdef foo 1632 struct m_tag *mtag; 1633 struct carp_if *cif = v; 1634 struct carp_softc *sc; 1635 1636 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) { 1637 struct ifaddr_container *ifac; 1638 1639 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], 1640 ifa_link) { 1641 struct ifaddr *ifa = ifac->ifa; 1642 1643 if (IN6_ARE_ADDR_EQUAL(taddr, 1644 &ifatoia6(ifa)->ia_addr.sin6_addr) && 1645 CARP_IS_RUNNING(&sc->sc_if)) { 1646 struct ifnet *ifp = &sc->sc_if; 1647 1648 mtag = m_tag_get(PACKET_TAG_CARP, 1649 sizeof(struct ifnet *), MB_DONTWAIT); 1650 if (mtag == NULL) { 1651 /* better a bit than nothing */ 1652 return (IF_LLADDR(ifp)); 1653 } 1654 bcopy(&ifp, (caddr_t)(mtag + 1), 1655 sizeof(struct ifnet *)); 1656 m_tag_prepend(m, mtag); 1657 1658 return (IF_LLADDR(ifp)); 1659 } 1660 } 1661 } 1662 #endif 1663 return (NULL); 1664 } 1665 #endif 1666 1667 static struct ifnet * 1668 carp_forus(struct carp_if *cif, const uint8_t *dhost) 1669 { 1670 struct carp_softc_container *scc; 1671 1672 if (memcmp(dhost, carp_etheraddr, ETHER_ADDR_LEN - 1) != 0) 1673 return NULL; 1674 1675 TAILQ_FOREACH(scc, cif, scc_link) { 1676 struct carp_softc *sc = scc->scc_softc; 1677 struct ifnet *ifp = &sc->sc_if; 1678 1679 if (CARP_IS_RUNNING(ifp) && sc->sc_state == MASTER && 1680 !bcmp(dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN)) 1681 return ifp; 1682 } 1683 return NULL; 1684 } 1685 1686 static void 1687 carp_master_down_timeout(void *xsc) 1688 { 1689 struct carp_softc *sc = xsc; 1690 struct netmsg_carp *cmsg = &sc->sc_md_msg; 1691 1692 KASSERT(mycpuid == 0, ("%s not on cpu0 but on cpu%d", 1693 __func__, mycpuid)); 1694 1695 crit_enter(); 1696 if (cmsg->base.lmsg.ms_flags & MSGF_DONE) 1697 lwkt_sendmsg(netisr_cpuport(0), &cmsg->base.lmsg); 1698 crit_exit(); 1699 } 1700 1701 static void 1702 carp_master_down_timeout_dispatch(netmsg_t msg) 1703 { 1704 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg; 1705 struct carp_softc *sc = cmsg->nc_softc; 1706 1707 /* Reply ASAP */ 1708 crit_enter(); 1709 lwkt_replymsg(&cmsg->base.lmsg, 0); 1710 crit_exit(); 1711 1712 CARP_DEBUG("%s: BACKUP -> MASTER (master timed out)\n", 1713 sc->sc_if.if_xname); 1714 carp_master_down(sc); 1715 } 1716 1717 static void 1718 carp_master_down(struct carp_softc *sc) 1719 { 1720 switch (sc->sc_state) { 1721 case INIT: 1722 kprintf("%s: master_down event in INIT state\n", 1723 sc->sc_if.if_xname); 1724 break; 1725 1726 case MASTER: 1727 break; 1728 1729 case BACKUP: 1730 carp_set_state(sc, MASTER); 1731 carp_send_ad(sc); 1732 carp_send_arp(sc); 1733 #ifdef INET6 1734 carp_send_na(sc); 1735 #endif /* INET6 */ 1736 carp_setrun(sc, 0); 1737 carp_setroute(sc, RTM_ADD); 1738 break; 1739 } 1740 } 1741 1742 /* 1743 * When in backup state, af indicates whether to reset the master down timer 1744 * for v4 or v6. If it's set to zero, reset the ones which are already pending. 1745 */ 1746 static void 1747 carp_setrun(struct carp_softc *sc, sa_family_t af) 1748 { 1749 struct ifnet *cifp = &sc->sc_if; 1750 struct timeval tv; 1751 1752 if (sc->sc_carpdev == NULL) { 1753 carp_set_state(sc, INIT); 1754 return; 1755 } 1756 1757 if ((cifp->if_flags & IFF_RUNNING) && sc->sc_vhid > 0 && 1758 (sc->sc_naddrs || sc->sc_naddrs6)) { 1759 /* Nothing */ 1760 } else { 1761 carp_setroute(sc, RTM_DELETE); 1762 return; 1763 } 1764 1765 switch (sc->sc_state) { 1766 case INIT: 1767 if (carp_opts[CARPCTL_PREEMPT] && !carp_suppress_preempt) { 1768 carp_send_ad(sc); 1769 carp_send_arp(sc); 1770 #ifdef INET6 1771 carp_send_na(sc); 1772 #endif /* INET6 */ 1773 CARP_DEBUG("%s: INIT -> MASTER (preempting)\n", 1774 cifp->if_xname); 1775 carp_set_state(sc, MASTER); 1776 carp_setroute(sc, RTM_ADD); 1777 } else { 1778 CARP_DEBUG("%s: INIT -> BACKUP\n", cifp->if_xname); 1779 carp_set_state(sc, BACKUP); 1780 carp_setroute(sc, RTM_DELETE); 1781 carp_setrun(sc, 0); 1782 } 1783 break; 1784 1785 case BACKUP: 1786 callout_stop(&sc->sc_ad_tmo); 1787 tv.tv_sec = 3 * sc->sc_advbase; 1788 tv.tv_usec = sc->sc_advskew * 1000000 / 256; 1789 switch (af) { 1790 #ifdef INET 1791 case AF_INET: 1792 callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv), 1793 carp_master_down_timeout, sc); 1794 break; 1795 #endif /* INET */ 1796 #ifdef INET6 1797 case AF_INET6: 1798 callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv), 1799 carp_master_down_timeout, sc); 1800 break; 1801 #endif /* INET6 */ 1802 default: 1803 if (sc->sc_naddrs) 1804 callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv), 1805 carp_master_down_timeout, sc); 1806 if (sc->sc_naddrs6) 1807 callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv), 1808 carp_master_down_timeout, sc); 1809 break; 1810 } 1811 break; 1812 1813 case MASTER: 1814 tv.tv_sec = sc->sc_advbase; 1815 tv.tv_usec = sc->sc_advskew * 1000000 / 256; 1816 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv), 1817 carp_send_ad_timeout, sc); 1818 break; 1819 } 1820 } 1821 1822 static void 1823 carp_multicast_cleanup(struct carp_softc *sc) 1824 { 1825 struct ip_moptions *imo = &sc->sc_imo; 1826 1827 if (imo->imo_num_memberships == 0) 1828 return; 1829 KKASSERT(imo->imo_num_memberships == 1); 1830 1831 in_delmulti(imo->imo_membership[0]); 1832 imo->imo_membership[0] = NULL; 1833 imo->imo_num_memberships = 0; 1834 imo->imo_multicast_ifp = NULL; 1835 } 1836 1837 #ifdef INET6 1838 static void 1839 carp_multicast6_cleanup(struct carp_softc *sc) 1840 { 1841 struct ip6_moptions *im6o = &sc->sc_im6o; 1842 1843 while (!LIST_EMPTY(&im6o->im6o_memberships)) { 1844 struct in6_multi_mship *imm = 1845 LIST_FIRST(&im6o->im6o_memberships); 1846 1847 LIST_REMOVE(imm, i6mm_chain); 1848 in6_leavegroup(imm); 1849 } 1850 im6o->im6o_multicast_ifp = NULL; 1851 } 1852 #endif 1853 1854 static void 1855 carp_ioctl_getvhaddr_dispatch(netmsg_t msg) 1856 { 1857 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg; 1858 struct carp_softc *sc = cmsg->nc_softc; 1859 const struct carp_vhaddr *vha; 1860 struct ifcarpvhaddr *carpa, *carpa0; 1861 int count, len, error = 0; 1862 1863 count = 0; 1864 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) 1865 ++count; 1866 1867 if (cmsg->nc_datalen == 0) { 1868 cmsg->nc_datalen = count * sizeof(*carpa); 1869 goto back; 1870 } else if (count == 0 || cmsg->nc_datalen < sizeof(*carpa)) { 1871 cmsg->nc_datalen = 0; 1872 goto back; 1873 } 1874 len = min(cmsg->nc_datalen, sizeof(*carpa) * count); 1875 KKASSERT(len >= sizeof(*carpa)); 1876 1877 carpa0 = carpa = kmalloc(len, M_TEMP, M_WAITOK | M_NULLOK | M_ZERO); 1878 if (carpa == NULL) { 1879 error = ENOMEM; 1880 goto back; 1881 } 1882 1883 count = 0; 1884 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) { 1885 if (len < sizeof(*carpa)) 1886 break; 1887 1888 carpa->carpa_flags = vha->vha_flags; 1889 carpa->carpa_addr.sin_family = AF_INET; 1890 carpa->carpa_addr.sin_addr = vha->vha_ia->ia_addr.sin_addr; 1891 1892 carpa->carpa_baddr.sin_family = AF_INET; 1893 if (vha->vha_iaback == NULL) { 1894 carpa->carpa_baddr.sin_addr.s_addr = INADDR_ANY; 1895 } else { 1896 carpa->carpa_baddr.sin_addr = 1897 vha->vha_iaback->ia_addr.sin_addr; 1898 } 1899 1900 ++carpa; 1901 ++count; 1902 len -= sizeof(*carpa); 1903 } 1904 cmsg->nc_datalen = sizeof(*carpa) * count; 1905 KKASSERT(cmsg->nc_datalen > 0); 1906 1907 cmsg->nc_data = carpa0; 1908 1909 back: 1910 lwkt_replymsg(&cmsg->base.lmsg, error); 1911 } 1912 1913 static int 1914 carp_ioctl_getvhaddr(struct carp_softc *sc, struct ifdrv *ifd) 1915 { 1916 struct ifnet *ifp = &sc->arpcom.ac_if; 1917 struct netmsg_carp cmsg; 1918 int error; 1919 1920 ASSERT_IFNET_SERIALIZED_ALL(ifp); 1921 ifnet_deserialize_all(ifp); 1922 1923 bzero(&cmsg, sizeof(cmsg)); 1924 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0, 1925 carp_ioctl_getvhaddr_dispatch); 1926 cmsg.nc_softc = sc; 1927 cmsg.nc_datalen = ifd->ifd_len; 1928 1929 error = lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0); 1930 1931 if (!error) { 1932 if (cmsg.nc_data != NULL) { 1933 error = copyout(cmsg.nc_data, ifd->ifd_data, 1934 cmsg.nc_datalen); 1935 kfree(cmsg.nc_data, M_TEMP); 1936 } 1937 ifd->ifd_len = cmsg.nc_datalen; 1938 } else { 1939 KASSERT(cmsg.nc_data == NULL, 1940 ("%s temp vhaddr is alloc upon error", __func__)); 1941 } 1942 1943 ifnet_serialize_all(ifp); 1944 return error; 1945 } 1946 1947 static int 1948 carp_config_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha, 1949 struct in_ifaddr *ia_del) 1950 { 1951 struct ifnet *ifp; 1952 struct in_ifaddr *ia_if; 1953 struct in_ifaddr_container *iac; 1954 const struct sockaddr_in *sin; 1955 u_long iaddr; 1956 int own; 1957 1958 KKASSERT(vha->vha_ia != NULL); 1959 1960 sin = &vha->vha_ia->ia_addr; 1961 iaddr = ntohl(sin->sin_addr.s_addr); 1962 1963 ia_if = NULL; 1964 own = 0; 1965 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) { 1966 struct in_ifaddr *ia = iac->ia; 1967 1968 if (ia == ia_del) 1969 continue; 1970 1971 if (ia->ia_ifp->if_type == IFT_CARP) 1972 continue; 1973 1974 if ((ia->ia_ifp->if_flags & IFF_UP) == 0) 1975 continue; 1976 1977 /* and, yeah, we need a multicast-capable iface too */ 1978 if ((ia->ia_ifp->if_flags & IFF_MULTICAST) == 0) 1979 continue; 1980 1981 if ((iaddr & ia->ia_subnetmask) == ia->ia_subnet) { 1982 if (sin->sin_addr.s_addr == 1983 ia->ia_addr.sin_addr.s_addr) 1984 own = 1; 1985 if (ia_if == NULL) 1986 ia_if = ia; 1987 else if (sc->sc_carpdev != NULL && 1988 sc->sc_carpdev == ia->ia_ifp) 1989 ia_if = ia; 1990 } 1991 } 1992 1993 carp_deactivate_vhaddr(sc, vha, FALSE); 1994 if (!ia_if) 1995 return ENOENT; 1996 1997 ifp = ia_if->ia_ifp; 1998 1999 /* XXX Don't allow parent iface to be changed */ 2000 if (sc->sc_carpdev != NULL && sc->sc_carpdev != ifp) 2001 return EEXIST; 2002 2003 return carp_activate_vhaddr(sc, vha, ifp, ia_if, own); 2004 } 2005 2006 static void 2007 carp_add_addr(struct carp_softc *sc, struct ifaddr *carp_ifa) 2008 { 2009 struct carp_vhaddr *vha_new; 2010 struct in_ifaddr *carp_ia; 2011 #ifdef INVARIANTS 2012 struct carp_vhaddr *vha; 2013 #endif 2014 2015 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET); 2016 carp_ia = ifatoia(carp_ifa); 2017 2018 #ifdef INVARIANTS 2019 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) 2020 KKASSERT(vha->vha_ia != NULL && vha->vha_ia != carp_ia); 2021 #endif 2022 2023 vha_new = kmalloc(sizeof(*vha_new), M_CARP, M_WAITOK | M_ZERO); 2024 vha_new->vha_ia = carp_ia; 2025 carp_insert_vhaddr(sc, vha_new); 2026 2027 if (carp_config_vhaddr(sc, vha_new, NULL) != 0) { 2028 /* 2029 * If the above configuration fails, it may only mean 2030 * that the new address is problematic. However, the 2031 * carp(4) interface may already have several working 2032 * addresses. Since the expected behaviour of 2033 * SIOC[AS]IFADDR is to put the NIC into working state, 2034 * we try starting the state machine manually here with 2035 * the hope that the carp(4)'s previously working 2036 * addresses still could be brought up. 2037 */ 2038 carp_hmac_prepare(sc); 2039 carp_set_state(sc, INIT); 2040 carp_setrun(sc, 0); 2041 } 2042 } 2043 2044 static void 2045 carp_del_addr(struct carp_softc *sc, struct ifaddr *carp_ifa) 2046 { 2047 struct carp_vhaddr *vha; 2048 struct in_ifaddr *carp_ia; 2049 2050 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET); 2051 carp_ia = ifatoia(carp_ifa); 2052 2053 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) { 2054 KKASSERT(vha->vha_ia != NULL); 2055 if (vha->vha_ia == carp_ia) 2056 break; 2057 } 2058 KASSERT(vha != NULL, ("no corresponding vhaddr %p", carp_ifa)); 2059 2060 /* 2061 * Remove the vhaddr from the list before deactivating 2062 * the vhaddr, so that the HMAC could be correctly 2063 * updated in carp_deactivate_vhaddr() 2064 */ 2065 carp_remove_vhaddr(sc, vha); 2066 2067 carp_deactivate_vhaddr(sc, vha, FALSE); 2068 kfree(vha, M_CARP); 2069 } 2070 2071 static void 2072 carp_config_addr(struct carp_softc *sc, struct ifaddr *carp_ifa) 2073 { 2074 struct carp_vhaddr *vha; 2075 struct in_ifaddr *carp_ia; 2076 2077 KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET); 2078 carp_ia = ifatoia(carp_ifa); 2079 2080 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) { 2081 KKASSERT(vha->vha_ia != NULL); 2082 if (vha->vha_ia == carp_ia) 2083 break; 2084 } 2085 KASSERT(vha != NULL, ("no corresponding vhaddr %p", carp_ifa)); 2086 2087 /* Remove then reinsert, to keep the vhaddr list sorted */ 2088 carp_remove_vhaddr(sc, vha); 2089 carp_insert_vhaddr(sc, vha); 2090 2091 if (carp_config_vhaddr(sc, vha, NULL) != 0) { 2092 /* See the comment in carp_add_addr() */ 2093 carp_hmac_prepare(sc); 2094 carp_set_state(sc, INIT); 2095 carp_setrun(sc, 0); 2096 } 2097 } 2098 2099 #ifdef notyet 2100 2101 #ifdef INET6 2102 static int 2103 carp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6) 2104 { 2105 struct ifnet *ifp; 2106 struct carp_if *cif; 2107 struct in6_ifaddr *ia, *ia_if; 2108 struct ip6_moptions *im6o = &sc->sc_im6o; 2109 struct in6_multi_mship *imm; 2110 struct in6_addr in6; 2111 int own, error; 2112 2113 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 2114 carp_setrun(sc, 0); 2115 return (0); 2116 } 2117 2118 /* we have to do it by hands to check we won't match on us */ 2119 ia_if = NULL; own = 0; 2120 for (ia = in6_ifaddr; ia; ia = ia->ia_next) { 2121 int i; 2122 2123 for (i = 0; i < 4; i++) { 2124 if ((sin6->sin6_addr.s6_addr32[i] & 2125 ia->ia_prefixmask.sin6_addr.s6_addr32[i]) != 2126 (ia->ia_addr.sin6_addr.s6_addr32[i] & 2127 ia->ia_prefixmask.sin6_addr.s6_addr32[i])) 2128 break; 2129 } 2130 /* and, yeah, we need a multicast-capable iface too */ 2131 if (ia->ia_ifp != &sc->sc_if && 2132 (ia->ia_ifp->if_flags & IFF_MULTICAST) && 2133 (i == 4)) { 2134 if (!ia_if) 2135 ia_if = ia; 2136 if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, 2137 &ia->ia_addr.sin6_addr)) 2138 own++; 2139 } 2140 } 2141 2142 if (!ia_if) 2143 return (EADDRNOTAVAIL); 2144 ia = ia_if; 2145 ifp = ia->ia_ifp; 2146 2147 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 || 2148 (im6o->im6o_multicast_ifp && im6o->im6o_multicast_ifp != ifp)) 2149 return (EADDRNOTAVAIL); 2150 2151 if (!sc->sc_naddrs6) { 2152 im6o->im6o_multicast_ifp = ifp; 2153 2154 /* join CARP multicast address */ 2155 bzero(&in6, sizeof(in6)); 2156 in6.s6_addr16[0] = htons(0xff02); 2157 in6.s6_addr8[15] = 0x12; 2158 if (in6_setscope(&in6, ifp, NULL) != 0) 2159 goto cleanup; 2160 if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL) 2161 goto cleanup; 2162 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain); 2163 2164 /* join solicited multicast address */ 2165 bzero(&in6, sizeof(in6)); 2166 in6.s6_addr16[0] = htons(0xff02); 2167 in6.s6_addr32[1] = 0; 2168 in6.s6_addr32[2] = htonl(1); 2169 in6.s6_addr32[3] = sin6->sin6_addr.s6_addr32[3]; 2170 in6.s6_addr8[12] = 0xff; 2171 if (in6_setscope(&in6, ifp, NULL) != 0) 2172 goto cleanup; 2173 if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL) 2174 goto cleanup; 2175 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain); 2176 } 2177 2178 #ifdef foo 2179 if (!ifp->if_carp) { 2180 cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO); 2181 2182 if ((error = ifpromisc(ifp, 1))) { 2183 kfree(cif, M_CARP); 2184 goto cleanup; 2185 } 2186 2187 TAILQ_INIT(&cif->vhif_vrs); 2188 ifp->if_carp = cif; 2189 } else { 2190 struct carp_softc *vr; 2191 2192 cif = ifp->if_carp; 2193 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) { 2194 if (vr != sc && vr->sc_vhid == sc->sc_vhid) { 2195 error = EINVAL; 2196 goto cleanup; 2197 } 2198 } 2199 } 2200 #endif 2201 sc->sc_ia6 = ia; 2202 sc->sc_carpdev = ifp; 2203 2204 #ifdef foo 2205 { /* XXX prevent endless loop if already in queue */ 2206 struct carp_softc *vr, *after = NULL; 2207 int myself = 0; 2208 cif = ifp->if_carp; 2209 2210 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) { 2211 if (vr == sc) 2212 myself = 1; 2213 if (vr->sc_vhid < sc->sc_vhid) 2214 after = vr; 2215 } 2216 2217 if (!myself) { 2218 /* We're trying to keep things in order */ 2219 if (after == NULL) 2220 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list); 2221 else 2222 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list); 2223 } 2224 } 2225 #endif 2226 2227 sc->sc_naddrs6++; 2228 if (own) 2229 sc->sc_advskew = 0; 2230 carp_sc_state(sc); 2231 carp_setrun(sc, 0); 2232 2233 return (0); 2234 2235 cleanup: 2236 /* clean up multicast memberships */ 2237 if (!sc->sc_naddrs6) { 2238 while (!LIST_EMPTY(&im6o->im6o_memberships)) { 2239 imm = LIST_FIRST(&im6o->im6o_memberships); 2240 LIST_REMOVE(imm, i6mm_chain); 2241 in6_leavegroup(imm); 2242 } 2243 } 2244 return (error); 2245 } 2246 2247 static int 2248 carp_del_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6) 2249 { 2250 int error = 0; 2251 2252 if (!--sc->sc_naddrs6) { 2253 struct carp_if *cif = sc->sc_carpdev->if_carp; 2254 struct ip6_moptions *im6o = &sc->sc_im6o; 2255 2256 callout_stop(&sc->sc_ad_tmo); 2257 sc->sc_vhid = -1; 2258 while (!LIST_EMPTY(&im6o->im6o_memberships)) { 2259 struct in6_multi_mship *imm = 2260 LIST_FIRST(&im6o->im6o_memberships); 2261 2262 LIST_REMOVE(imm, i6mm_chain); 2263 in6_leavegroup(imm); 2264 } 2265 im6o->im6o_multicast_ifp = NULL; 2266 #ifdef foo 2267 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list); 2268 if (TAILQ_EMPTY(&cif->vhif_vrs)) { 2269 sc->sc_carpdev->if_carp = NULL; 2270 kfree(cif, M_IFADDR); 2271 } 2272 #endif 2273 } 2274 return (error); 2275 } 2276 #endif /* INET6 */ 2277 2278 #endif 2279 2280 static int 2281 carp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr, struct ucred *cr) 2282 { 2283 struct carp_softc *sc = ifp->if_softc; 2284 struct ifreq *ifr = (struct ifreq *)addr; 2285 struct ifdrv *ifd = (struct ifdrv *)addr; 2286 int error = 0; 2287 2288 ASSERT_IFNET_SERIALIZED_ALL(ifp); 2289 2290 switch (cmd) { 2291 case SIOCSIFFLAGS: 2292 if (ifp->if_flags & IFF_UP) { 2293 if ((ifp->if_flags & IFF_RUNNING) == 0) 2294 carp_init(sc); 2295 } else if (ifp->if_flags & IFF_RUNNING) { 2296 carp_ioctl_stop(sc); 2297 } 2298 break; 2299 2300 case SIOCSVH: 2301 error = carp_ioctl_setvh(sc, ifr->ifr_data, cr); 2302 break; 2303 2304 case SIOCGVH: 2305 error = carp_ioctl_getvh(sc, ifr->ifr_data, cr); 2306 break; 2307 2308 case SIOCGDRVSPEC: 2309 switch (ifd->ifd_cmd) { 2310 case CARPGDEVNAME: 2311 error = carp_ioctl_getdevname(sc, ifd); 2312 break; 2313 2314 case CARPGVHADDR: 2315 error = carp_ioctl_getvhaddr(sc, ifd); 2316 break; 2317 2318 default: 2319 error = EINVAL; 2320 break; 2321 } 2322 break; 2323 2324 default: 2325 error = ether_ioctl(ifp, cmd, addr); 2326 break; 2327 } 2328 2329 return error; 2330 } 2331 2332 static void 2333 carp_ioctl_stop_dispatch(netmsg_t msg) 2334 { 2335 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg; 2336 struct carp_softc *sc = cmsg->nc_softc; 2337 2338 carp_stop(sc, FALSE); 2339 lwkt_replymsg(&cmsg->base.lmsg, 0); 2340 } 2341 2342 static void 2343 carp_ioctl_stop(struct carp_softc *sc) 2344 { 2345 struct ifnet *ifp = &sc->arpcom.ac_if; 2346 struct netmsg_carp cmsg; 2347 2348 ASSERT_IFNET_SERIALIZED_ALL(ifp); 2349 2350 ifnet_deserialize_all(ifp); 2351 2352 bzero(&cmsg, sizeof(cmsg)); 2353 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0, 2354 carp_ioctl_stop_dispatch); 2355 cmsg.nc_softc = sc; 2356 2357 lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0); 2358 2359 ifnet_serialize_all(ifp); 2360 } 2361 2362 static void 2363 carp_ioctl_setvh_dispatch(netmsg_t msg) 2364 { 2365 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg; 2366 struct carp_softc *sc = cmsg->nc_softc; 2367 struct ifnet *ifp = &sc->arpcom.ac_if; 2368 const struct carpreq *carpr = cmsg->nc_data; 2369 int error; 2370 2371 error = 1; 2372 if ((ifp->if_flags & IFF_RUNNING) && 2373 sc->sc_state != INIT && carpr->carpr_state != sc->sc_state) { 2374 switch (carpr->carpr_state) { 2375 case BACKUP: 2376 callout_stop(&sc->sc_ad_tmo); 2377 carp_set_state(sc, BACKUP); 2378 carp_setrun(sc, 0); 2379 carp_setroute(sc, RTM_DELETE); 2380 break; 2381 2382 case MASTER: 2383 carp_master_down(sc); 2384 break; 2385 2386 default: 2387 break; 2388 } 2389 } 2390 if (carpr->carpr_vhid > 0) { 2391 if (carpr->carpr_vhid > 255) { 2392 error = EINVAL; 2393 goto back; 2394 } 2395 if (sc->sc_carpdev) { 2396 struct carp_if *cif = sc->sc_carpdev->if_carp; 2397 struct carp_softc_container *scc; 2398 2399 TAILQ_FOREACH(scc, cif, scc_link) { 2400 struct carp_softc *vr = scc->scc_softc; 2401 2402 if (vr != sc && 2403 vr->sc_vhid == carpr->carpr_vhid) { 2404 error = EEXIST; 2405 goto back; 2406 } 2407 } 2408 } 2409 sc->sc_vhid = carpr->carpr_vhid; 2410 2411 IF_LLADDR(ifp)[5] = sc->sc_vhid; 2412 bcopy(IF_LLADDR(ifp), sc->arpcom.ac_enaddr, 2413 ETHER_ADDR_LEN); 2414 2415 error--; 2416 } 2417 if (carpr->carpr_advbase > 0 || carpr->carpr_advskew > 0) { 2418 if (carpr->carpr_advskew >= 255) { 2419 error = EINVAL; 2420 goto back; 2421 } 2422 if (carpr->carpr_advbase > 255) { 2423 error = EINVAL; 2424 goto back; 2425 } 2426 sc->sc_advbase = carpr->carpr_advbase; 2427 sc->sc_advskew = carpr->carpr_advskew; 2428 error--; 2429 } 2430 bcopy(carpr->carpr_key, sc->sc_key, sizeof(sc->sc_key)); 2431 if (error > 0) { 2432 error = EINVAL; 2433 } else { 2434 error = 0; 2435 carp_setrun(sc, 0); 2436 } 2437 back: 2438 carp_hmac_prepare(sc); 2439 2440 lwkt_replymsg(&cmsg->base.lmsg, error); 2441 } 2442 2443 static int 2444 carp_ioctl_setvh(struct carp_softc *sc, void *udata, struct ucred *cr) 2445 { 2446 struct ifnet *ifp = &sc->arpcom.ac_if; 2447 struct netmsg_carp cmsg; 2448 struct carpreq carpr; 2449 int error; 2450 2451 ASSERT_IFNET_SERIALIZED_ALL(ifp); 2452 ifnet_deserialize_all(ifp); 2453 2454 error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY); 2455 if (error) 2456 goto back; 2457 2458 error = copyin(udata, &carpr, sizeof(carpr)); 2459 if (error) 2460 goto back; 2461 2462 bzero(&cmsg, sizeof(cmsg)); 2463 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0, 2464 carp_ioctl_setvh_dispatch); 2465 cmsg.nc_softc = sc; 2466 cmsg.nc_data = &carpr; 2467 2468 error = lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0); 2469 2470 back: 2471 ifnet_serialize_all(ifp); 2472 return error; 2473 } 2474 2475 static void 2476 carp_ioctl_getvh_dispatch(netmsg_t msg) 2477 { 2478 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg; 2479 struct carp_softc *sc = cmsg->nc_softc; 2480 struct carpreq *carpr = cmsg->nc_data; 2481 2482 carpr->carpr_state = sc->sc_state; 2483 carpr->carpr_vhid = sc->sc_vhid; 2484 carpr->carpr_advbase = sc->sc_advbase; 2485 carpr->carpr_advskew = sc->sc_advskew; 2486 bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key)); 2487 2488 lwkt_replymsg(&cmsg->base.lmsg, 0); 2489 } 2490 2491 static int 2492 carp_ioctl_getvh(struct carp_softc *sc, void *udata, struct ucred *cr) 2493 { 2494 struct ifnet *ifp = &sc->arpcom.ac_if; 2495 struct netmsg_carp cmsg; 2496 struct carpreq carpr; 2497 int error; 2498 2499 ASSERT_IFNET_SERIALIZED_ALL(ifp); 2500 ifnet_deserialize_all(ifp); 2501 2502 bzero(&cmsg, sizeof(cmsg)); 2503 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0, 2504 carp_ioctl_getvh_dispatch); 2505 cmsg.nc_softc = sc; 2506 cmsg.nc_data = &carpr; 2507 2508 lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0); 2509 2510 error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY); 2511 if (error) 2512 bzero(carpr.carpr_key, sizeof(carpr.carpr_key)); 2513 2514 error = copyout(&carpr, udata, sizeof(carpr)); 2515 2516 ifnet_serialize_all(ifp); 2517 return error; 2518 } 2519 2520 static void 2521 carp_ioctl_getdevname_dispatch(netmsg_t msg) 2522 { 2523 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg; 2524 struct carp_softc *sc = cmsg->nc_softc; 2525 char *devname = cmsg->nc_data; 2526 2527 bzero(devname, IFNAMSIZ); 2528 if (sc->sc_carpdev != NULL) 2529 strlcpy(devname, sc->sc_carpdev->if_xname, IFNAMSIZ); 2530 2531 lwkt_replymsg(&cmsg->base.lmsg, 0); 2532 } 2533 2534 static int 2535 carp_ioctl_getdevname(struct carp_softc *sc, struct ifdrv *ifd) 2536 { 2537 struct ifnet *ifp = &sc->arpcom.ac_if; 2538 struct netmsg_carp cmsg; 2539 char devname[IFNAMSIZ]; 2540 int error; 2541 2542 ASSERT_IFNET_SERIALIZED_ALL(ifp); 2543 2544 if (ifd->ifd_len != sizeof(devname)) 2545 return EINVAL; 2546 2547 ifnet_deserialize_all(ifp); 2548 2549 bzero(&cmsg, sizeof(cmsg)); 2550 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0, 2551 carp_ioctl_getdevname_dispatch); 2552 cmsg.nc_softc = sc; 2553 cmsg.nc_data = devname; 2554 2555 lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0); 2556 2557 error = copyout(devname, ifd->ifd_data, sizeof(devname)); 2558 2559 ifnet_serialize_all(ifp); 2560 return error; 2561 } 2562 2563 static void 2564 carp_init_dispatch(netmsg_t msg) 2565 { 2566 struct netmsg_carp *cmsg = (struct netmsg_carp *)msg; 2567 struct carp_softc *sc = cmsg->nc_softc; 2568 2569 sc->sc_if.if_flags |= IFF_RUNNING; 2570 carp_hmac_prepare(sc); 2571 carp_set_state(sc, INIT); 2572 carp_setrun(sc, 0); 2573 2574 lwkt_replymsg(&cmsg->base.lmsg, 0); 2575 } 2576 2577 static void 2578 carp_init(void *xsc) 2579 { 2580 struct carp_softc *sc = xsc; 2581 struct ifnet *ifp = &sc->arpcom.ac_if; 2582 struct netmsg_carp cmsg; 2583 2584 ASSERT_IFNET_SERIALIZED_ALL(ifp); 2585 2586 ifnet_deserialize_all(ifp); 2587 2588 bzero(&cmsg, sizeof(cmsg)); 2589 netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0, 2590 carp_init_dispatch); 2591 cmsg.nc_softc = sc; 2592 2593 lwkt_domsg(netisr_cpuport(0), &cmsg.base.lmsg, 0); 2594 2595 ifnet_serialize_all(ifp); 2596 } 2597 2598 static int 2599 carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, 2600 struct rtentry *rt) 2601 { 2602 struct carp_softc *sc = ifp->if_softc; 2603 struct ifnet *carpdev; 2604 int error = 0; 2605 2606 carpdev = sc->sc_carpdev; 2607 if (carpdev != NULL) { 2608 /* 2609 * NOTE: 2610 * CARP's ifp is passed to backing device's 2611 * if_output method. 2612 */ 2613 carpdev->if_output(ifp, m, dst, rt); 2614 } else { 2615 m_freem(m); 2616 error = ENETUNREACH; 2617 } 2618 return error; 2619 } 2620 2621 /* 2622 * Start output on carp interface. This function should never be called. 2623 */ 2624 static void 2625 carp_start(struct ifnet *ifp, struct ifaltq_subque *ifsq __unused) 2626 { 2627 panic("%s: start called", ifp->if_xname); 2628 } 2629 2630 static void 2631 carp_set_state(struct carp_softc *sc, int state) 2632 { 2633 struct ifnet *cifp = &sc->sc_if; 2634 2635 if (sc->sc_state == state) 2636 return; 2637 sc->sc_state = state; 2638 2639 switch (sc->sc_state) { 2640 case BACKUP: 2641 cifp->if_link_state = LINK_STATE_DOWN; 2642 break; 2643 2644 case MASTER: 2645 cifp->if_link_state = LINK_STATE_UP; 2646 break; 2647 2648 default: 2649 cifp->if_link_state = LINK_STATE_UNKNOWN; 2650 break; 2651 } 2652 rt_ifmsg(cifp); 2653 } 2654 2655 void 2656 carp_group_demote_adj(struct ifnet *ifp, int adj) 2657 { 2658 struct ifg_list *ifgl; 2659 int *dm; 2660 2661 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) { 2662 if (!strcmp(ifgl->ifgl_group->ifg_group, IFG_ALL)) 2663 continue; 2664 dm = &ifgl->ifgl_group->ifg_carp_demoted; 2665 2666 if (*dm + adj >= 0) 2667 *dm += adj; 2668 else 2669 *dm = 0; 2670 2671 if (adj > 0 && *dm == 1) 2672 carp_send_ad_all(); 2673 CARP_LOG("%s demoted group %s to %d", ifp->if_xname, 2674 ifgl->ifgl_group->ifg_group, *dm); 2675 } 2676 } 2677 2678 #ifdef foo 2679 void 2680 carp_carpdev_state(void *v) 2681 { 2682 struct carp_if *cif = v; 2683 struct carp_softc *sc; 2684 2685 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) 2686 carp_sc_state(sc); 2687 } 2688 2689 static void 2690 carp_sc_state(struct carp_softc *sc) 2691 { 2692 if (!(sc->sc_carpdev->if_flags & IFF_UP)) { 2693 callout_stop(&sc->sc_ad_tmo); 2694 callout_stop(&sc->sc_md_tmo); 2695 callout_stop(&sc->sc_md6_tmo); 2696 carp_set_state(sc, INIT); 2697 carp_setrun(sc, 0); 2698 if (!sc->sc_suppress) { 2699 carp_suppress_preempt++; 2700 if (carp_suppress_preempt == 1) 2701 carp_send_ad_all(); 2702 } 2703 sc->sc_suppress = 1; 2704 } else { 2705 carp_set_state(sc, INIT); 2706 carp_setrun(sc, 0); 2707 if (sc->sc_suppress) 2708 carp_suppress_preempt--; 2709 sc->sc_suppress = 0; 2710 } 2711 } 2712 #endif 2713 2714 static void 2715 carp_stop(struct carp_softc *sc, boolean_t detach) 2716 { 2717 sc->sc_if.if_flags &= ~IFF_RUNNING; 2718 2719 callout_stop(&sc->sc_ad_tmo); 2720 callout_stop(&sc->sc_md_tmo); 2721 callout_stop(&sc->sc_md6_tmo); 2722 2723 if (!detach && sc->sc_state == MASTER) 2724 carp_send_ad(sc); 2725 2726 if (sc->sc_suppress) 2727 carp_suppress_preempt--; 2728 sc->sc_suppress = 0; 2729 2730 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) 2731 carp_suppress_preempt--; 2732 sc->sc_sendad_errors = 0; 2733 sc->sc_sendad_success = 0; 2734 2735 carp_set_state(sc, INIT); 2736 carp_setrun(sc, 0); 2737 } 2738 2739 static void 2740 carp_suspend(struct carp_softc *sc, boolean_t detach) 2741 { 2742 struct ifnet *cifp = &sc->sc_if; 2743 2744 carp_stop(sc, detach); 2745 2746 /* Retain the running state, if we are not dead yet */ 2747 if (!sc->sc_dead && (cifp->if_flags & IFF_UP)) 2748 cifp->if_flags |= IFF_RUNNING; 2749 } 2750 2751 static int 2752 carp_activate_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha, 2753 struct ifnet *ifp, struct in_ifaddr *ia_if, int own) 2754 { 2755 struct ip_moptions *imo = &sc->sc_imo; 2756 struct carp_if *ocif = ifp->if_carp; 2757 int error; 2758 2759 KKASSERT(vha->vha_ia != NULL); 2760 2761 KASSERT(ia_if != NULL, ("NULL backing address")); 2762 KASSERT(vha->vha_iaback == NULL, ("%p is already activated", vha)); 2763 KASSERT((vha->vha_flags & CARP_VHAF_OWNER) == 0, 2764 ("inactive vhaddr %p is the address owner", vha)); 2765 2766 KASSERT(sc->sc_carpdev == NULL || sc->sc_carpdev == ifp, 2767 ("%s is already on %s", sc->sc_if.if_xname, 2768 sc->sc_carpdev->if_xname)); 2769 2770 if (ocif == NULL) { 2771 KASSERT(sc->sc_carpdev == NULL, 2772 ("%s is already on %s", sc->sc_if.if_xname, 2773 sc->sc_carpdev->if_xname)); 2774 2775 error = ifpromisc(ifp, 1); 2776 if (error) 2777 return error; 2778 } else { 2779 struct carp_softc_container *scc; 2780 2781 TAILQ_FOREACH(scc, ocif, scc_link) { 2782 struct carp_softc *vr = scc->scc_softc; 2783 2784 if (vr != sc && vr->sc_vhid == sc->sc_vhid) 2785 return EINVAL; 2786 } 2787 } 2788 2789 ifp->if_carp = carp_if_insert(ocif, sc); 2790 KASSERT(ifp->if_carp != NULL, ("%s carp_if_insert failed", __func__)); 2791 2792 sc->sc_ia = ia_if; 2793 sc->sc_carpdev = ifp; 2794 2795 /* 2796 * Make sure that all protocol threads see the sc_carpdev and 2797 * if_carp changes 2798 */ 2799 netmsg_service_sync(); 2800 2801 if (ocif != NULL && ifp->if_carp != ocif) { 2802 /* 2803 * The old carp list could be safely free now, 2804 * since no one can access it. 2805 */ 2806 carp_if_free(ocif); 2807 } 2808 2809 vha->vha_iaback = ia_if; 2810 sc->sc_naddrs++; 2811 2812 if (own) { 2813 vha->vha_flags |= CARP_VHAF_OWNER; 2814 2815 /* XXX save user configured advskew? */ 2816 sc->sc_advskew = 0; 2817 } 2818 2819 carp_addroute_vhaddr(sc, vha); 2820 2821 /* 2822 * Join the multicast group only after the backing interface 2823 * has been hooked with the CARP interface. 2824 */ 2825 KASSERT(imo->imo_multicast_ifp == NULL || 2826 imo->imo_multicast_ifp == &sc->sc_if, 2827 ("%s didn't leave mcast group on %s", 2828 sc->sc_if.if_xname, imo->imo_multicast_ifp->if_xname)); 2829 2830 if (imo->imo_num_memberships == 0) { 2831 struct in_addr addr; 2832 2833 addr.s_addr = htonl(INADDR_CARP_GROUP); 2834 imo->imo_membership[0] = in_addmulti(&addr, &sc->sc_if); 2835 if (imo->imo_membership[0] == NULL) { 2836 carp_deactivate_vhaddr(sc, vha, FALSE); 2837 return ENOBUFS; 2838 } 2839 2840 imo->imo_num_memberships++; 2841 imo->imo_multicast_ifp = &sc->sc_if; 2842 imo->imo_multicast_ttl = CARP_DFLTTL; 2843 imo->imo_multicast_loop = 0; 2844 } 2845 2846 carp_hmac_prepare(sc); 2847 carp_set_state(sc, INIT); 2848 carp_setrun(sc, 0); 2849 return 0; 2850 } 2851 2852 static void 2853 carp_deactivate_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha, 2854 boolean_t del_iaback) 2855 { 2856 KKASSERT(vha->vha_ia != NULL); 2857 2858 carp_hmac_prepare(sc); 2859 2860 if (vha->vha_iaback == NULL) { 2861 KASSERT((vha->vha_flags & CARP_VHAF_OWNER) == 0, 2862 ("inactive vhaddr %p is the address owner", vha)); 2863 return; 2864 } 2865 2866 vha->vha_flags &= ~CARP_VHAF_OWNER; 2867 carp_delroute_vhaddr(sc, vha, del_iaback); 2868 2869 KKASSERT(sc->sc_naddrs > 0); 2870 vha->vha_iaback = NULL; 2871 sc->sc_naddrs--; 2872 if (!sc->sc_naddrs) { 2873 if (sc->sc_naddrs6) { 2874 carp_multicast_cleanup(sc); 2875 sc->sc_ia = NULL; 2876 } else { 2877 carp_detach(sc, FALSE, del_iaback); 2878 } 2879 } 2880 } 2881 2882 static void 2883 carp_link_addrs(struct carp_softc *sc, struct ifnet *ifp, struct ifaddr *ifa_if) 2884 { 2885 struct carp_vhaddr *vha; 2886 struct in_ifaddr *ia_if; 2887 2888 KKASSERT(ifa_if->ifa_addr->sa_family == AF_INET); 2889 ia_if = ifatoia(ifa_if); 2890 2891 /* 2892 * Test each inactive vhaddr against the newly added address. 2893 * If the newly added address could be the backing address, 2894 * then activate the matching vhaddr. 2895 */ 2896 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) { 2897 const struct in_ifaddr *ia; 2898 u_long iaddr; 2899 int own; 2900 2901 if (vha->vha_iaback != NULL) 2902 continue; 2903 2904 ia = vha->vha_ia; 2905 iaddr = ntohl(ia->ia_addr.sin_addr.s_addr); 2906 2907 if ((iaddr & ia_if->ia_subnetmask) != ia_if->ia_subnet) 2908 continue; 2909 2910 own = 0; 2911 if (ia->ia_addr.sin_addr.s_addr == 2912 ia_if->ia_addr.sin_addr.s_addr) 2913 own = 1; 2914 2915 carp_activate_vhaddr(sc, vha, ifp, ia_if, own); 2916 } 2917 } 2918 2919 static void 2920 carp_unlink_addrs(struct carp_softc *sc, struct ifnet *ifp, 2921 struct ifaddr *ifa_if) 2922 { 2923 struct carp_vhaddr *vha; 2924 struct in_ifaddr *ia_if; 2925 2926 KKASSERT(ifa_if->ifa_addr->sa_family == AF_INET); 2927 ia_if = ifatoia(ifa_if); 2928 2929 /* 2930 * Ad src address is deleted; set it to NULL. 2931 * Following loop will try pick up a new ad src address 2932 * if one of the vhaddr could retain its backing address. 2933 */ 2934 if (sc->sc_ia == ia_if) 2935 sc->sc_ia = NULL; 2936 2937 /* 2938 * Test each active vhaddr against the deleted address. 2939 * If the deleted address is vhaddr address's backing 2940 * address, then deactivate the vhaddr. 2941 */ 2942 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) { 2943 if (vha->vha_iaback == NULL) 2944 continue; 2945 2946 if (vha->vha_iaback == ia_if) 2947 carp_deactivate_vhaddr(sc, vha, TRUE); 2948 else if (sc->sc_ia == NULL) 2949 sc->sc_ia = vha->vha_iaback; 2950 } 2951 } 2952 2953 static void 2954 carp_update_addrs(struct carp_softc *sc, struct ifaddr *ifa_del) 2955 { 2956 struct carp_vhaddr *vha; 2957 2958 KKASSERT(sc->sc_carpdev == NULL); 2959 2960 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) 2961 carp_config_vhaddr(sc, vha, ifatoia(ifa_del)); 2962 } 2963 2964 static void 2965 carp_ifaddr(void *arg __unused, struct ifnet *ifp, 2966 enum ifaddr_event event, struct ifaddr *ifa) 2967 { 2968 struct carp_softc *sc; 2969 2970 if (ifa->ifa_addr->sa_family != AF_INET) 2971 return; 2972 2973 KASSERT(&curthread->td_msgport == netisr_cpuport(0), 2974 ("not in netisr0")); 2975 2976 if (ifp->if_type == IFT_CARP) { 2977 /* 2978 * Address is changed on carp(4) interface 2979 */ 2980 switch (event) { 2981 case IFADDR_EVENT_ADD: 2982 carp_add_addr(ifp->if_softc, ifa); 2983 break; 2984 2985 case IFADDR_EVENT_CHANGE: 2986 carp_config_addr(ifp->if_softc, ifa); 2987 break; 2988 2989 case IFADDR_EVENT_DELETE: 2990 carp_del_addr(ifp->if_softc, ifa); 2991 break; 2992 } 2993 return; 2994 } 2995 2996 /* 2997 * Address is changed on non-carp(4) interface 2998 */ 2999 if ((ifp->if_flags & IFF_MULTICAST) == 0) 3000 return; 3001 3002 LIST_FOREACH(sc, &carpif_list, sc_next) { 3003 if (sc->sc_carpdev != NULL && sc->sc_carpdev != ifp) { 3004 /* Not the parent iface; skip */ 3005 continue; 3006 } 3007 3008 switch (event) { 3009 case IFADDR_EVENT_ADD: 3010 carp_link_addrs(sc, ifp, ifa); 3011 break; 3012 3013 case IFADDR_EVENT_DELETE: 3014 if (sc->sc_carpdev != NULL) { 3015 carp_unlink_addrs(sc, ifp, ifa); 3016 if (sc->sc_carpdev == NULL) { 3017 /* 3018 * We no longer have the parent 3019 * interface, however, certain 3020 * virtual addresses, which are 3021 * not used because they can't 3022 * match the previous parent 3023 * interface's addresses, may now 3024 * match different interface's 3025 * addresses. 3026 */ 3027 carp_update_addrs(sc, ifa); 3028 } 3029 } else { 3030 /* 3031 * The carp(4) interface didn't have a 3032 * parent iface, so it is not possible 3033 * that it will contain any address to 3034 * be unlinked. 3035 */ 3036 } 3037 break; 3038 3039 case IFADDR_EVENT_CHANGE: 3040 if (sc->sc_carpdev == NULL) { 3041 /* 3042 * The carp(4) interface didn't have a 3043 * parent iface, so it is not possible 3044 * that it will contain any address to 3045 * be updated. 3046 */ 3047 carp_link_addrs(sc, ifp, ifa); 3048 } else { 3049 /* 3050 * First try breaking tie with the old 3051 * address. Then see whether we could 3052 * link certain vhaddr to the new address. 3053 * If that fails, i.e. carpdev is NULL, 3054 * we try a global update. 3055 * 3056 * NOTE: The above order is critical. 3057 */ 3058 carp_unlink_addrs(sc, ifp, ifa); 3059 carp_link_addrs(sc, ifp, ifa); 3060 if (sc->sc_carpdev == NULL) { 3061 /* 3062 * See the comment in the above 3063 * IFADDR_EVENT_DELETE block. 3064 */ 3065 carp_update_addrs(sc, NULL); 3066 } 3067 } 3068 break; 3069 } 3070 } 3071 } 3072 3073 void 3074 carp_proto_ctlinput(netmsg_t msg) 3075 { 3076 int cmd = msg->ctlinput.nm_cmd; 3077 struct sockaddr *sa = msg->ctlinput.nm_arg; 3078 struct in_ifaddr_container *iac; 3079 3080 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) { 3081 struct in_ifaddr *ia = iac->ia; 3082 struct ifnet *ifp = ia->ia_ifp; 3083 3084 if (ifp->if_type == IFT_CARP) 3085 continue; 3086 3087 if (ia->ia_ifa.ifa_addr == sa) { 3088 if (cmd == PRC_IFDOWN) { 3089 carp_ifaddr(NULL, ifp, IFADDR_EVENT_DELETE, 3090 &ia->ia_ifa); 3091 } else if (cmd == PRC_IFUP) { 3092 carp_ifaddr(NULL, ifp, IFADDR_EVENT_ADD, 3093 &ia->ia_ifa); 3094 } 3095 break; 3096 } 3097 } 3098 3099 lwkt_replymsg(&msg->lmsg, 0); 3100 } 3101 3102 struct ifnet * 3103 carp_parent(struct ifnet *cifp) 3104 { 3105 struct carp_softc *sc; 3106 3107 KKASSERT(cifp->if_type == IFT_CARP); 3108 sc = cifp->if_softc; 3109 3110 return sc->sc_carpdev; 3111 } 3112 3113 #define rtinitflags(x) \ 3114 (((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) \ 3115 ? RTF_HOST : 0) 3116 3117 static int 3118 carp_addroute_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha) 3119 { 3120 struct in_ifaddr *ia, *iaback; 3121 int error; 3122 3123 if (sc->sc_state != MASTER) 3124 return 0; 3125 3126 ia = vha->vha_ia; 3127 KKASSERT(ia != NULL); 3128 3129 iaback = vha->vha_iaback; 3130 KKASSERT(iaback != NULL); 3131 3132 rtinit(&iaback->ia_ifa, RTM_DELETE, rtinitflags(iaback)); 3133 in_ifadown(&iaback->ia_ifa, 1); 3134 iaback->ia_flags &= ~IFA_ROUTE; 3135 3136 error = rtinit(&ia->ia_ifa, RTM_ADD, rtinitflags(ia) | RTF_UP); 3137 if (!error) 3138 ia->ia_flags |= IFA_ROUTE; 3139 return error; 3140 } 3141 3142 static void 3143 carp_delroute_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha, 3144 boolean_t del_iaback) 3145 { 3146 struct in_ifaddr *ia, *iaback; 3147 3148 ia = vha->vha_ia; 3149 KKASSERT(ia != NULL); 3150 3151 iaback = vha->vha_iaback; 3152 KKASSERT(iaback != NULL); 3153 3154 rtinit(&ia->ia_ifa, RTM_DELETE, rtinitflags(ia)); 3155 in_ifadown(&ia->ia_ifa, 1); 3156 ia->ia_flags &= ~IFA_ROUTE; 3157 3158 if (!del_iaback && (iaback->ia_ifp->if_flags & IFF_UP)) { 3159 int error; 3160 3161 error = rtinit(&iaback->ia_ifa, RTM_ADD, 3162 rtinitflags(iaback) | RTF_UP); 3163 if (!error) 3164 iaback->ia_flags |= IFA_ROUTE; 3165 } 3166 } 3167 3168 static int 3169 carp_modevent(module_t mod, int type, void *data) 3170 { 3171 switch (type) { 3172 case MOD_LOAD: 3173 LIST_INIT(&carpif_list); 3174 carp_ifdetach_event = 3175 EVENTHANDLER_REGISTER(ifnet_detach_event, carp_ifdetach, NULL, 3176 EVENTHANDLER_PRI_ANY); 3177 carp_ifaddr_event = 3178 EVENTHANDLER_REGISTER(ifaddr_event, carp_ifaddr, NULL, 3179 EVENTHANDLER_PRI_FIRST); 3180 if_clone_attach(&carp_cloner); 3181 break; 3182 3183 case MOD_UNLOAD: 3184 EVENTHANDLER_DEREGISTER(ifnet_detach_event, 3185 carp_ifdetach_event); 3186 EVENTHANDLER_DEREGISTER(ifaddr_event, 3187 carp_ifaddr_event); 3188 if_clone_detach(&carp_cloner); 3189 break; 3190 3191 default: 3192 return (EINVAL); 3193 } 3194 return (0); 3195 } 3196 3197 static moduledata_t carp_mod = { 3198 "carp", 3199 carp_modevent, 3200 0 3201 }; 3202 DECLARE_MODULE(carp, carp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 3203