108b68b0eSGleb Smirnoff /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3fe267a55SPedro F. Giffuni * 408b68b0eSGleb Smirnoff * Copyright (c) 2002 Michael Shalayeff. 508b68b0eSGleb Smirnoff * Copyright (c) 2003 Ryan McBride. 608b68b0eSGleb Smirnoff * Copyright (c) 2011 Gleb Smirnoff <glebius@FreeBSD.org> 708b68b0eSGleb Smirnoff * All rights reserved. 8a9771948SGleb Smirnoff * 9a9771948SGleb Smirnoff * Redistribution and use in source and binary forms, with or without 10a9771948SGleb Smirnoff * modification, are permitted provided that the following conditions 11a9771948SGleb Smirnoff * are met: 12a9771948SGleb Smirnoff * 1. Redistributions of source code must retain the above copyright 13a9771948SGleb Smirnoff * notice, this list of conditions and the following disclaimer. 14a9771948SGleb Smirnoff * 2. Redistributions in binary form must reproduce the above copyright 15a9771948SGleb Smirnoff * notice, this list of conditions and the following disclaimer in the 16a9771948SGleb Smirnoff * documentation and/or other materials provided with the distribution. 17a9771948SGleb Smirnoff * 18a9771948SGleb Smirnoff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19a9771948SGleb Smirnoff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20a9771948SGleb Smirnoff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21a9771948SGleb Smirnoff * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT, 22a9771948SGleb Smirnoff * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23a9771948SGleb Smirnoff * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24a9771948SGleb Smirnoff * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25a9771948SGleb Smirnoff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26a9771948SGleb Smirnoff * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 27a9771948SGleb Smirnoff * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28a9771948SGleb Smirnoff * THE POSSIBILITY OF SUCH DAMAGE. 29a9771948SGleb Smirnoff */ 30a9771948SGleb Smirnoff 314b421e2dSMike Silbersack #include <sys/cdefs.h> 32a9771948SGleb Smirnoff #include "opt_bpf.h" 33a9771948SGleb Smirnoff #include "opt_inet.h" 34a9771948SGleb Smirnoff #include "opt_inet6.h" 35a9771948SGleb Smirnoff 36a9771948SGleb Smirnoff #include <sys/param.h> 37a9771948SGleb Smirnoff #include <sys/systm.h> 38773e541eSWarner Losh #include <sys/devctl.h> 3908b68b0eSGleb Smirnoff #include <sys/jail.h> 4037115154SKristof Provost #include <sys/kassert.h> 41a9771948SGleb Smirnoff #include <sys/kernel.h> 42a9771948SGleb Smirnoff #include <sys/limits.h> 43a9771948SGleb Smirnoff #include <sys/malloc.h> 44a9771948SGleb Smirnoff #include <sys/mbuf.h> 45a9771948SGleb Smirnoff #include <sys/module.h> 46acd3428bSRobert Watson #include <sys/priv.h> 47a9771948SGleb Smirnoff #include <sys/proc.h> 4808b68b0eSGleb Smirnoff #include <sys/socket.h> 4908b68b0eSGleb Smirnoff #include <sys/sockio.h> 50a9771948SGleb Smirnoff #include <sys/sysctl.h> 51a9771948SGleb Smirnoff #include <sys/syslog.h> 52f08535f8SGleb Smirnoff #include <sys/taskqueue.h> 5369edf037SAndrey V. Elsukov #include <sys/counter.h> 54a9771948SGleb Smirnoff 55a9771948SGleb Smirnoff #include <net/ethernet.h> 56a9771948SGleb Smirnoff #include <net/if.h> 5776039bc8SGleb Smirnoff #include <net/if_var.h> 58433aaf04SRuslan Ermilov #include <net/if_dl.h> 5908b68b0eSGleb Smirnoff #include <net/if_llatbl.h> 603d0d5b21SJustin Hibbits #include <net/if_private.h> 61a9771948SGleb Smirnoff #include <net/if_types.h> 62a9771948SGleb Smirnoff #include <net/route.h> 63530c0060SRobert Watson #include <net/vnet.h> 64a9771948SGleb Smirnoff 65a0ae8f04SBjoern A. Zeeb #if defined(INET) || defined(INET6) 66a9771948SGleb Smirnoff #include <netinet/in.h> 67a9771948SGleb Smirnoff #include <netinet/in_var.h> 68a0ae8f04SBjoern A. Zeeb #include <netinet/ip_carp.h> 6940e04359SKristof Provost #include <netinet/ip_carp_nl.h> 70a9771948SGleb Smirnoff #include <netinet/ip.h> 71a0ae8f04SBjoern A. Zeeb #include <machine/in_cksum.h> 72a0ae8f04SBjoern A. Zeeb #endif 73a0ae8f04SBjoern A. Zeeb #ifdef INET 74a9771948SGleb Smirnoff #include <netinet/ip_var.h> 75a9771948SGleb Smirnoff #include <netinet/if_ether.h> 76a9771948SGleb Smirnoff #endif 77a9771948SGleb Smirnoff 78a9771948SGleb Smirnoff #ifdef INET6 79a9771948SGleb Smirnoff #include <netinet/icmp6.h> 80a9771948SGleb Smirnoff #include <netinet/ip6.h> 8108b68b0eSGleb Smirnoff #include <netinet6/in6_var.h> 82a9771948SGleb Smirnoff #include <netinet6/ip6_var.h> 8329da8af6SHajimu UMEMOTO #include <netinet6/scope6_var.h> 84a9771948SGleb Smirnoff #include <netinet6/nd6.h> 85a9771948SGleb Smirnoff #endif 86a9771948SGleb Smirnoff 8740e04359SKristof Provost #include <netlink/netlink.h> 8840e04359SKristof Provost #include <netlink/netlink_ctl.h> 8940e04359SKristof Provost #include <netlink/netlink_generic.h> 9040e04359SKristof Provost #include <netlink/netlink_message_parser.h> 9140e04359SKristof Provost 92a9771948SGleb Smirnoff #include <crypto/sha1.h> 93a9771948SGleb Smirnoff 9408b68b0eSGleb Smirnoff static MALLOC_DEFINE(M_CARP, "CARP", "CARP addresses"); 95a9771948SGleb Smirnoff 96a9771948SGleb Smirnoff struct carp_softc { 9708b68b0eSGleb Smirnoff struct ifnet *sc_carpdev; /* Pointer to parent ifnet. */ 9808b68b0eSGleb Smirnoff struct ifaddr **sc_ifas; /* Our ifaddrs. */ 9937115154SKristof Provost carp_version_t sc_version; /* carp or VRRPv3 */ 100a254d687SGleb Smirnoff uint8_t sc_addr[ETHER_ADDR_LEN]; /* Our link level address. */ 10108b68b0eSGleb Smirnoff struct callout sc_ad_tmo; /* Advertising timeout. */ 102a0ae8f04SBjoern A. Zeeb #ifdef INET 10308b68b0eSGleb Smirnoff struct callout sc_md_tmo; /* Master down timeout. */ 104a0ae8f04SBjoern A. Zeeb #endif 105a9771948SGleb Smirnoff #ifdef INET6 10608b68b0eSGleb Smirnoff struct callout sc_md6_tmo; /* XXX: Master down timeout. */ 10708b68b0eSGleb Smirnoff #endif 10808b68b0eSGleb Smirnoff struct mtx sc_mtx; 109a9771948SGleb Smirnoff 11008b68b0eSGleb Smirnoff int sc_vhid; 111a254d687SGleb Smirnoff union { 112a254d687SGleb Smirnoff struct { /* sc_version == CARP_VERSION_CARP */ 11308b68b0eSGleb Smirnoff int sc_advskew; 11408b68b0eSGleb Smirnoff int sc_advbase; 11513781800SKristof Provost struct in_addr sc_carpaddr; 11613781800SKristof Provost struct in6_addr sc_carpaddr6; 117a254d687SGleb Smirnoff uint64_t sc_counter; 118a254d687SGleb Smirnoff bool sc_init_counter; 119a254d687SGleb Smirnoff #define CARP_HMAC_PAD 64 120a254d687SGleb Smirnoff unsigned char sc_key[CARP_KEY_LEN]; 121a254d687SGleb Smirnoff unsigned char sc_pad[CARP_HMAC_PAD]; 122a254d687SGleb Smirnoff SHA1_CTX sc_sha1; 12337115154SKristof Provost }; 124a254d687SGleb Smirnoff struct { /* sc_version == CARP_VERSION_VRRPv3 */ 12537115154SKristof Provost uint8_t sc_vrrp_prio; 12637115154SKristof Provost uint16_t sc_vrrp_adv_inter; 12737115154SKristof Provost uint16_t sc_vrrp_master_inter; 12837115154SKristof Provost }; 129a254d687SGleb Smirnoff }; 13008b68b0eSGleb Smirnoff int sc_naddrs; 13108b68b0eSGleb Smirnoff int sc_naddrs6; 13208b68b0eSGleb Smirnoff int sc_ifasiz; 133a9771948SGleb Smirnoff enum { INIT = 0, BACKUP, MASTER } sc_state; 134a9771948SGleb Smirnoff int sc_suppress; 135a9771948SGleb Smirnoff int sc_sendad_errors; 136a9771948SGleb Smirnoff #define CARP_SENDAD_MAX_ERRORS 3 137a9771948SGleb Smirnoff int sc_sendad_success; 138a9771948SGleb Smirnoff #define CARP_SENDAD_MIN_SUCCESS 3 139a9771948SGleb Smirnoff 14008b68b0eSGleb Smirnoff TAILQ_ENTRY(carp_softc) sc_list; /* On the carp_if list. */ 14108b68b0eSGleb Smirnoff LIST_ENTRY(carp_softc) sc_next; /* On the global list. */ 142a9771948SGleb Smirnoff }; 14308b68b0eSGleb Smirnoff 14408b68b0eSGleb Smirnoff struct carp_if { 14508b68b0eSGleb Smirnoff #ifdef INET 14608b68b0eSGleb Smirnoff int cif_naddrs; 14708b68b0eSGleb Smirnoff #endif 14808b68b0eSGleb Smirnoff #ifdef INET6 14908b68b0eSGleb Smirnoff int cif_naddrs6; 15008b68b0eSGleb Smirnoff #endif 15108b68b0eSGleb Smirnoff TAILQ_HEAD(, carp_softc) cif_vrs; 15208b68b0eSGleb Smirnoff #ifdef INET 15308b68b0eSGleb Smirnoff struct ip_moptions cif_imo; 15408b68b0eSGleb Smirnoff #endif 15508b68b0eSGleb Smirnoff #ifdef INET6 15608b68b0eSGleb Smirnoff struct ip6_moptions cif_im6o; 15708b68b0eSGleb Smirnoff #endif 15808b68b0eSGleb Smirnoff struct ifnet *cif_ifp; 15908b68b0eSGleb Smirnoff struct mtx cif_mtx; 1600cc726f2SGleb Smirnoff uint32_t cif_flags; 1610cc726f2SGleb Smirnoff #define CIF_PROMISC 0x00000001 16208b68b0eSGleb Smirnoff }; 16308b68b0eSGleb Smirnoff 16413781800SKristof Provost /* Kernel equivalent of struct carpreq, but with more fields for new features. 16513781800SKristof Provost * */ 16613781800SKristof Provost struct carpkreq { 16713781800SKristof Provost int carpr_count; 16813781800SKristof Provost int carpr_vhid; 16913781800SKristof Provost int carpr_state; 17013781800SKristof Provost int carpr_advskew; 17113781800SKristof Provost int carpr_advbase; 17213781800SKristof Provost unsigned char carpr_key[CARP_KEY_LEN]; 17313781800SKristof Provost /* Everything above this is identical to carpreq */ 17413781800SKristof Provost struct in_addr carpr_addr; 17513781800SKristof Provost struct in6_addr carpr_addr6; 17637115154SKristof Provost carp_version_t carpr_version; 17737115154SKristof Provost uint8_t carpr_vrrp_priority; 17837115154SKristof Provost uint16_t carpr_vrrp_adv_inter; 17913781800SKristof Provost }; 18013781800SKristof Provost 18108b68b0eSGleb Smirnoff /* 18208b68b0eSGleb Smirnoff * Brief design of carp(4). 18308b68b0eSGleb Smirnoff * 18408b68b0eSGleb Smirnoff * Any carp-capable ifnet may have a list of carp softcs hanging off 18508b68b0eSGleb Smirnoff * its ifp->if_carp pointer. Each softc represents one unique virtual 18608b68b0eSGleb Smirnoff * host id, or vhid. The softc has a back pointer to the ifnet. All 18708b68b0eSGleb Smirnoff * softcs are joined in a global list, which has quite limited use. 18808b68b0eSGleb Smirnoff * 18908b68b0eSGleb Smirnoff * Any interface address that takes part in CARP negotiation has a 19008b68b0eSGleb Smirnoff * pointer to the softc of its vhid, ifa->ifa_carp. That could be either 19108b68b0eSGleb Smirnoff * AF_INET or AF_INET6 address. 19208b68b0eSGleb Smirnoff * 19308b68b0eSGleb Smirnoff * Although, one can get the softc's backpointer to ifnet and traverse 19408b68b0eSGleb Smirnoff * through its ifp->if_addrhead queue to find all interface addresses 19508b68b0eSGleb Smirnoff * involved in CARP, we keep a growable array of ifaddr pointers. This 19608b68b0eSGleb Smirnoff * allows us to avoid grabbing the IF_ADDR_LOCK() in many traversals that 19708b68b0eSGleb Smirnoff * do calls into the network stack, thus avoiding LORs. 19808b68b0eSGleb Smirnoff * 19908b68b0eSGleb Smirnoff * Locking: 20008b68b0eSGleb Smirnoff * 20108b68b0eSGleb Smirnoff * Each softc has a lock sc_mtx. It is used to synchronise carp_input_c(), 20208b68b0eSGleb Smirnoff * callout-driven events and ioctl()s. 20308b68b0eSGleb Smirnoff * 20481098a01SAlexander Motin * To traverse the list of softcs on an ifnet we use CIF_LOCK() or carp_sx. 20581098a01SAlexander Motin * To traverse the global list we use the mutex carp_mtx. 20608b68b0eSGleb Smirnoff * 20708b68b0eSGleb Smirnoff * Known issues with locking: 20808b68b0eSGleb Smirnoff * 20908b68b0eSGleb Smirnoff * - Sending ad, we put the pointer to the softc in an mtag, and no reference 21008b68b0eSGleb Smirnoff * counting is done on the softc. 21108b68b0eSGleb Smirnoff * - On module unload we may race (?) with packet processing thread 21208b68b0eSGleb Smirnoff * dereferencing our function pointers. 21308b68b0eSGleb Smirnoff */ 214a9771948SGleb Smirnoff 215c5c392e7SMikolaj Golub /* Accept incoming CARP packets. */ 2165f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_allow) = 1; 217c5c392e7SMikolaj Golub #define V_carp_allow VNET(carp_allow) 218c5c392e7SMikolaj Golub 2190d3d234cSKristof Provost /* Set DSCP in outgoing CARP packets. */ 2205f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_dscp) = 56; 2210d3d234cSKristof Provost #define V_carp_dscp VNET(carp_dscp) 2220d3d234cSKristof Provost 223c5c392e7SMikolaj Golub /* Preempt slower nodes. */ 2245f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_preempt) = 0; 225c5c392e7SMikolaj Golub #define V_carp_preempt VNET(carp_preempt) 226c5c392e7SMikolaj Golub 227c5c392e7SMikolaj Golub /* Log level. */ 2285f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_log) = 1; 229c5c392e7SMikolaj Golub #define V_carp_log VNET(carp_log) 230c5c392e7SMikolaj Golub 231c5c392e7SMikolaj Golub /* Global advskew demotion. */ 2325f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_demotion) = 0; 233c5c392e7SMikolaj Golub #define V_carp_demotion VNET(carp_demotion) 234c5c392e7SMikolaj Golub 235c5c392e7SMikolaj Golub /* Send error demotion factor. */ 2365f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_senderr_adj) = CARP_MAXSKEW; 237c5c392e7SMikolaj Golub #define V_carp_senderr_adj VNET(carp_senderr_adj) 238c5c392e7SMikolaj Golub 239c5c392e7SMikolaj Golub /* Iface down demotion factor. */ 2405f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_ifdown_adj) = CARP_MAXSKEW; 241c5c392e7SMikolaj Golub #define V_carp_ifdown_adj VNET(carp_ifdown_adj) 242c5c392e7SMikolaj Golub 243167a3440SAlexander Motin static int carp_allow_sysctl(SYSCTL_HANDLER_ARGS); 2440d3d234cSKristof Provost static int carp_dscp_sysctl(SYSCTL_HANDLER_ARGS); 2457951008bSGleb Smirnoff static int carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS); 246a9771948SGleb Smirnoff 24710b49b23SPawel Biernacki SYSCTL_NODE(_net_inet, IPPROTO_CARP, carp, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 24810b49b23SPawel Biernacki "CARP"); 249167a3440SAlexander Motin SYSCTL_PROC(_net_inet_carp, OID_AUTO, allow, 250242fa308SZhenlei Huang CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE, 251ee49c5d3SBoris Lytochkin &VNET_NAME(carp_allow), 0, carp_allow_sysctl, "I", 252167a3440SAlexander Motin "Accept incoming CARP packets"); 2530d3d234cSKristof Provost SYSCTL_PROC(_net_inet_carp, OID_AUTO, dscp, 25410b49b23SPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 25510b49b23SPawel Biernacki 0, 0, carp_dscp_sysctl, "I", 2560d3d234cSKristof Provost "DSCP value for carp packets"); 2576df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, preempt, CTLFLAG_VNET | CTLFLAG_RW, 258c5c392e7SMikolaj Golub &VNET_NAME(carp_preempt), 0, "High-priority backup preemption mode"); 2596df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, log, CTLFLAG_VNET | CTLFLAG_RW, 260c5c392e7SMikolaj Golub &VNET_NAME(carp_log), 0, "CARP log level"); 2616df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_carp, OID_AUTO, demotion, 26210b49b23SPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 2637951008bSGleb Smirnoff 0, 0, carp_demote_adj_sysctl, "I", 2647951008bSGleb Smirnoff "Adjust demotion factor (skew of advskew)"); 2656df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, senderr_demotion_factor, 2666df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, 267c5c392e7SMikolaj Golub &VNET_NAME(carp_senderr_adj), 0, "Send error demotion factor adjustment"); 2686df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, ifdown_demotion_factor, 2696df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, 270c5c392e7SMikolaj Golub &VNET_NAME(carp_ifdown_adj), 0, 271c5c392e7SMikolaj Golub "Interface down demotion factor adjustment"); 272f08535f8SGleb Smirnoff 273c5c392e7SMikolaj Golub VNET_PCPUSTAT_DEFINE(struct carpstats, carpstats); 274c5c392e7SMikolaj Golub VNET_PCPUSTAT_SYSINIT(carpstats); 275c5c392e7SMikolaj Golub VNET_PCPUSTAT_SYSUNINIT(carpstats); 276c5c392e7SMikolaj Golub 27769edf037SAndrey V. Elsukov #define CARPSTATS_ADD(name, val) \ 278c5c392e7SMikolaj Golub counter_u64_add(VNET(carpstats)[offsetof(struct carpstats, name) / \ 27969edf037SAndrey V. Elsukov sizeof(uint64_t)], (val)) 28069edf037SAndrey V. Elsukov #define CARPSTATS_INC(name) CARPSTATS_ADD(name, 1) 28169edf037SAndrey V. Elsukov 282c5c392e7SMikolaj Golub SYSCTL_VNET_PCPUSTAT(_net_inet_carp, OID_AUTO, stats, struct carpstats, 283c5c392e7SMikolaj Golub carpstats, "CARP statistics (struct carpstats, netinet/ip_carp.h)"); 284a9771948SGleb Smirnoff 28508b68b0eSGleb Smirnoff #define CARP_LOCK_INIT(sc) mtx_init(&(sc)->sc_mtx, "carp_softc", \ 286a9771948SGleb Smirnoff NULL, MTX_DEF) 28708b68b0eSGleb Smirnoff #define CARP_LOCK_DESTROY(sc) mtx_destroy(&(sc)->sc_mtx) 28808b68b0eSGleb Smirnoff #define CARP_LOCK_ASSERT(sc) mtx_assert(&(sc)->sc_mtx, MA_OWNED) 28908b68b0eSGleb Smirnoff #define CARP_LOCK(sc) mtx_lock(&(sc)->sc_mtx) 29008b68b0eSGleb Smirnoff #define CARP_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx) 29108b68b0eSGleb Smirnoff #define CIF_LOCK_INIT(cif) mtx_init(&(cif)->cif_mtx, "carp_if", \ 29208b68b0eSGleb Smirnoff NULL, MTX_DEF) 29308b68b0eSGleb Smirnoff #define CIF_LOCK_DESTROY(cif) mtx_destroy(&(cif)->cif_mtx) 29408b68b0eSGleb Smirnoff #define CIF_LOCK_ASSERT(cif) mtx_assert(&(cif)->cif_mtx, MA_OWNED) 29508b68b0eSGleb Smirnoff #define CIF_LOCK(cif) mtx_lock(&(cif)->cif_mtx) 29608b68b0eSGleb Smirnoff #define CIF_UNLOCK(cif) mtx_unlock(&(cif)->cif_mtx) 297a9a2c40cSGleb Smirnoff #define CIF_FREE(cif) do { \ 2989c2cd1aaSGleb Smirnoff CIF_LOCK(cif); \ 299a9a2c40cSGleb Smirnoff if (TAILQ_EMPTY(&(cif)->cif_vrs)) \ 300a9a2c40cSGleb Smirnoff carp_free_if(cif); \ 301a9a2c40cSGleb Smirnoff else \ 302a9a2c40cSGleb Smirnoff CIF_UNLOCK(cif); \ 303a9a2c40cSGleb Smirnoff } while (0) 304d220759bSGleb Smirnoff 305947b7cf3SGleb Smirnoff #define CARP_LOG(...) do { \ 306c5c392e7SMikolaj Golub if (V_carp_log > 0) \ 30708b68b0eSGleb Smirnoff log(LOG_INFO, "carp: " __VA_ARGS__); \ 308947b7cf3SGleb Smirnoff } while (0) 3091e9e6572SGleb Smirnoff 310947b7cf3SGleb Smirnoff #define CARP_DEBUG(...) do { \ 311c5c392e7SMikolaj Golub if (V_carp_log > 1) \ 3121e9e6572SGleb Smirnoff log(LOG_DEBUG, __VA_ARGS__); \ 313947b7cf3SGleb Smirnoff } while (0) 314a9771948SGleb Smirnoff 31508b68b0eSGleb Smirnoff #define IFNET_FOREACH_IFA(ifp, ifa) \ 316d7c5a620SMatt Macy CK_STAILQ_FOREACH((ifa), &(ifp)->if_addrhead, ifa_link) \ 31708b68b0eSGleb Smirnoff if ((ifa)->ifa_carp != NULL) 31808b68b0eSGleb Smirnoff 31908b68b0eSGleb Smirnoff #define CARP_FOREACH_IFA(sc, ifa) \ 32008b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); \ 32108b68b0eSGleb Smirnoff for (int _i = 0; \ 32208b68b0eSGleb Smirnoff _i < (sc)->sc_naddrs + (sc)->sc_naddrs6 && \ 32308b68b0eSGleb Smirnoff ((ifa) = sc->sc_ifas[_i]) != NULL; \ 32408b68b0eSGleb Smirnoff ++_i) 32508b68b0eSGleb Smirnoff 32608b68b0eSGleb Smirnoff #define IFNET_FOREACH_CARP(ifp, sc) \ 32781098a01SAlexander Motin KASSERT(mtx_owned(&ifp->if_carp->cif_mtx) || \ 32881098a01SAlexander Motin sx_xlocked(&carp_sx), ("cif_vrs not locked")); \ 32908b68b0eSGleb Smirnoff TAILQ_FOREACH((sc), &(ifp)->if_carp->cif_vrs, sc_list) 33008b68b0eSGleb Smirnoff 331f08535f8SGleb Smirnoff #define DEMOTE_ADVSKEW(sc) \ 332c5c392e7SMikolaj Golub (((sc)->sc_advskew + V_carp_demotion > CARP_MAXSKEW) ? \ 3331019354bSMarius Halden CARP_MAXSKEW : \ 3341019354bSMarius Halden (((sc)->sc_advskew + V_carp_demotion < 0) ? \ 3351019354bSMarius Halden 0 : ((sc)->sc_advskew + V_carp_demotion))) 336f08535f8SGleb Smirnoff 33713781800SKristof Provost static void carp_input_c(struct mbuf *, struct carp_header *, sa_family_t, int); 33837115154SKristof Provost static void vrrp_input_c(struct mbuf *, int, sa_family_t, int, int, uint16_t); 33908b68b0eSGleb Smirnoff static struct carp_softc 340a254d687SGleb Smirnoff *carp_alloc(struct ifnet *, carp_version_t, int); 34108b68b0eSGleb Smirnoff static void carp_destroy(struct carp_softc *); 34208b68b0eSGleb Smirnoff static struct carp_if 34308b68b0eSGleb Smirnoff *carp_alloc_if(struct ifnet *); 34408b68b0eSGleb Smirnoff static void carp_free_if(struct carp_if *); 345d01641e2SWill Andrews static void carp_set_state(struct carp_softc *, int, const char* reason); 34608b68b0eSGleb Smirnoff static void carp_sc_state(struct carp_softc *); 34708b68b0eSGleb Smirnoff static void carp_setrun(struct carp_softc *, sa_family_t); 3485c1f0f6dSGleb Smirnoff static void carp_master_down(void *); 349d01641e2SWill Andrews static void carp_master_down_locked(struct carp_softc *, 350d01641e2SWill Andrews const char* reason); 35108b68b0eSGleb Smirnoff static void carp_send_ad_locked(struct carp_softc *); 35237115154SKristof Provost static void vrrp_send_ad_locked(struct carp_softc *); 35308b68b0eSGleb Smirnoff static void carp_addroute(struct carp_softc *); 3542512b096SGleb Smirnoff static void carp_ifa_addroute(struct ifaddr *); 35508b68b0eSGleb Smirnoff static void carp_delroute(struct carp_softc *); 3562512b096SGleb Smirnoff static void carp_ifa_delroute(struct ifaddr *); 357f08535f8SGleb Smirnoff static void carp_send_ad_all(void *, int); 358f08535f8SGleb Smirnoff static void carp_demote_adj(int, char *); 359a9771948SGleb Smirnoff 36008b68b0eSGleb Smirnoff static LIST_HEAD(, carp_softc) carp_list; 361d92d54d5SGleb Smirnoff static struct mtx carp_mtx; 36293d4534cSGleb Smirnoff static struct sx carp_sx; 363f08535f8SGleb Smirnoff static struct task carp_sendall_task = 364f08535f8SGleb Smirnoff TASK_INITIALIZER(0, carp_send_ad_all, NULL); 365a9771948SGleb Smirnoff 36640e04359SKristof Provost static int 36740e04359SKristof Provost carp_is_supported_if(if_t ifp) 36840e04359SKristof Provost { 36940e04359SKristof Provost if (ifp == NULL) 37040e04359SKristof Provost return (ENXIO); 37140e04359SKristof Provost 37240e04359SKristof Provost switch (ifp->if_type) { 37340e04359SKristof Provost case IFT_ETHER: 37440e04359SKristof Provost case IFT_L2VLAN: 37540e04359SKristof Provost case IFT_BRIDGE: 37640e04359SKristof Provost break; 37740e04359SKristof Provost default: 37840e04359SKristof Provost return (EOPNOTSUPP); 37940e04359SKristof Provost } 38040e04359SKristof Provost 38140e04359SKristof Provost return (0); 38240e04359SKristof Provost } 38340e04359SKristof Provost 3845c1f0f6dSGleb Smirnoff static void 385a9771948SGleb Smirnoff carp_hmac_prepare(struct carp_softc *sc) 386a9771948SGleb Smirnoff { 38737115154SKristof Provost uint8_t version = CARP_VERSION_CARP, type = CARP_ADVERTISEMENT; 38808b68b0eSGleb Smirnoff uint8_t vhid = sc->sc_vhid & 0xff; 389a9771948SGleb Smirnoff struct ifaddr *ifa; 3901ead26d4SMax Laier int i, found; 3911ead26d4SMax Laier #ifdef INET 3921ead26d4SMax Laier struct in_addr last, cur, in; 3931ead26d4SMax Laier #endif 394a9771948SGleb Smirnoff #ifdef INET6 3951ead26d4SMax Laier struct in6_addr last6, cur6, in6; 396a9771948SGleb Smirnoff #endif 397a9771948SGleb Smirnoff 39808b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); 399a254d687SGleb Smirnoff MPASS(sc->sc_version == CARP_VERSION_CARP); 400d220759bSGleb Smirnoff 40108b68b0eSGleb Smirnoff /* Compute ipad from key. */ 402a9771948SGleb Smirnoff bzero(sc->sc_pad, sizeof(sc->sc_pad)); 403a9771948SGleb Smirnoff bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key)); 404a9771948SGleb Smirnoff for (i = 0; i < sizeof(sc->sc_pad); i++) 405a9771948SGleb Smirnoff sc->sc_pad[i] ^= 0x36; 406a9771948SGleb Smirnoff 40708b68b0eSGleb Smirnoff /* Precompute first part of inner hash. */ 408a9771948SGleb Smirnoff SHA1Init(&sc->sc_sha1); 409a9771948SGleb Smirnoff SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad)); 410a9771948SGleb Smirnoff SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version)); 411a9771948SGleb Smirnoff SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type)); 412a9771948SGleb Smirnoff SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid)); 413a9771948SGleb Smirnoff #ifdef INET 4141ead26d4SMax Laier cur.s_addr = 0; 4151ead26d4SMax Laier do { 4161ead26d4SMax Laier found = 0; 4171ead26d4SMax Laier last = cur; 4181ead26d4SMax Laier cur.s_addr = 0xffffffff; 41908b68b0eSGleb Smirnoff CARP_FOREACH_IFA(sc, ifa) { 4201ead26d4SMax Laier in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr; 4211ead26d4SMax Laier if (ifa->ifa_addr->sa_family == AF_INET && 4221ead26d4SMax Laier ntohl(in.s_addr) > ntohl(last.s_addr) && 4231ead26d4SMax Laier ntohl(in.s_addr) < ntohl(cur.s_addr)) { 4241ead26d4SMax Laier cur.s_addr = in.s_addr; 4251ead26d4SMax Laier found++; 426a9771948SGleb Smirnoff } 4271ead26d4SMax Laier } 4281ead26d4SMax Laier if (found) 4291ead26d4SMax Laier SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur)); 4301ead26d4SMax Laier } while (found); 431a9771948SGleb Smirnoff #endif /* INET */ 432a9771948SGleb Smirnoff #ifdef INET6 4331ead26d4SMax Laier memset(&cur6, 0, sizeof(cur6)); 4341ead26d4SMax Laier do { 4351ead26d4SMax Laier found = 0; 4361ead26d4SMax Laier last6 = cur6; 4371ead26d4SMax Laier memset(&cur6, 0xff, sizeof(cur6)); 43808b68b0eSGleb Smirnoff CARP_FOREACH_IFA(sc, ifa) { 439a9771948SGleb Smirnoff in6 = ifatoia6(ifa)->ia_addr.sin6_addr; 4401ead26d4SMax Laier if (IN6_IS_SCOPE_EMBED(&in6)) 4411ead26d4SMax Laier in6.s6_addr16[1] = 0; 4421ead26d4SMax Laier if (ifa->ifa_addr->sa_family == AF_INET6 && 4431ead26d4SMax Laier memcmp(&in6, &last6, sizeof(in6)) > 0 && 4441ead26d4SMax Laier memcmp(&in6, &cur6, sizeof(in6)) < 0) { 4451ead26d4SMax Laier cur6 = in6; 4461ead26d4SMax Laier found++; 447a9771948SGleb Smirnoff } 448a9771948SGleb Smirnoff } 4491ead26d4SMax Laier if (found) 4501ead26d4SMax Laier SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6)); 4511ead26d4SMax Laier } while (found); 452a9771948SGleb Smirnoff #endif /* INET6 */ 453a9771948SGleb Smirnoff 454a9771948SGleb Smirnoff /* convert ipad to opad */ 455a9771948SGleb Smirnoff for (i = 0; i < sizeof(sc->sc_pad); i++) 456a9771948SGleb Smirnoff sc->sc_pad[i] ^= 0x36 ^ 0x5c; 457a9771948SGleb Smirnoff } 458a9771948SGleb Smirnoff 4595c1f0f6dSGleb Smirnoff static void 46008b68b0eSGleb Smirnoff carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2], 461a9771948SGleb Smirnoff unsigned char md[20]) 462a9771948SGleb Smirnoff { 463a9771948SGleb Smirnoff SHA1_CTX sha1ctx; 464a9771948SGleb Smirnoff 46508b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); 46608b68b0eSGleb Smirnoff 467a9771948SGleb Smirnoff /* fetch first half of inner hash */ 468a9771948SGleb Smirnoff bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx)); 469a9771948SGleb Smirnoff 470a9771948SGleb Smirnoff SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter)); 471a9771948SGleb Smirnoff SHA1Final(md, &sha1ctx); 472a9771948SGleb Smirnoff 473a9771948SGleb Smirnoff /* outer hash */ 474a9771948SGleb Smirnoff SHA1Init(&sha1ctx); 475a9771948SGleb Smirnoff SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad)); 476a9771948SGleb Smirnoff SHA1Update(&sha1ctx, md, 20); 477a9771948SGleb Smirnoff SHA1Final(md, &sha1ctx); 478a9771948SGleb Smirnoff } 479a9771948SGleb Smirnoff 4805c1f0f6dSGleb Smirnoff static int 48108b68b0eSGleb Smirnoff carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2], 482a9771948SGleb Smirnoff unsigned char md[20]) 483a9771948SGleb Smirnoff { 484a9771948SGleb Smirnoff unsigned char md2[20]; 485a9771948SGleb Smirnoff 48608b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); 487d220759bSGleb Smirnoff 488a9771948SGleb Smirnoff carp_hmac_generate(sc, counter, md2); 489a9771948SGleb Smirnoff 490a9771948SGleb Smirnoff return (bcmp(md, md2, sizeof(md2))); 491a9771948SGleb Smirnoff } 492a9771948SGleb Smirnoff 49337115154SKristof Provost static int 49437115154SKristof Provost vrrp_checksum_verify(struct mbuf *m, int off, int len, uint16_t phdrcksum) 49537115154SKristof Provost { 49637115154SKristof Provost uint16_t cksum; 49737115154SKristof Provost 49837115154SKristof Provost /* 49937115154SKristof Provost * Note that VRRPv3 checksums are different from CARP checksums. 50037115154SKristof Provost * Carp just calculates the checksum over the packet. 50137115154SKristof Provost * VRRPv3 includes the pseudo-header checksum as well. 50237115154SKristof Provost */ 50337115154SKristof Provost cksum = in_cksum_skip(m, off + len, off); 50437115154SKristof Provost cksum -= phdrcksum; 50537115154SKristof Provost 50637115154SKristof Provost return (cksum); 50737115154SKristof Provost } 50837115154SKristof Provost 509a9771948SGleb Smirnoff /* 510a9771948SGleb Smirnoff * process input packet. 511a9771948SGleb Smirnoff * we have rearranged checks order compared to the rfc, 512a9771948SGleb Smirnoff * but it seems more efficient this way or not possible otherwise. 513a9771948SGleb Smirnoff */ 514a0ae8f04SBjoern A. Zeeb #ifdef INET 51578b1fc05SGleb Smirnoff static int 5168f5a8818SKevin Lo carp_input(struct mbuf **mp, int *offp, int proto) 517a9771948SGleb Smirnoff { 5188f5a8818SKevin Lo struct mbuf *m = *mp; 5197d508464SMark Johnston struct ip *ip; 52037115154SKristof Provost struct vrrpv3_header *vh; 52137115154SKristof Provost int iplen; 52237115154SKristof Provost int minlen; 52337115154SKristof Provost int totlen; 524a9771948SGleb Smirnoff 5258f5a8818SKevin Lo iplen = *offp; 5268f5a8818SKevin Lo *mp = NULL; 5278f5a8818SKevin Lo 5286bf65bcfSRobert Watson CARPSTATS_INC(carps_ipackets); 529a9771948SGleb Smirnoff 530c5c392e7SMikolaj Golub if (!V_carp_allow) { 531a9771948SGleb Smirnoff m_freem(m); 5328f5a8818SKevin Lo return (IPPROTO_DONE); 533a9771948SGleb Smirnoff } 534a9771948SGleb Smirnoff 53537115154SKristof Provost /* Ensure we have enough header to figure out the version. */ 53637115154SKristof Provost if (m->m_pkthdr.len < iplen + sizeof(*vh)) { 5376bf65bcfSRobert Watson CARPSTATS_INC(carps_badlen); 53837115154SKristof Provost CARP_DEBUG("%s: received len %zd < sizeof(struct vrrpv3_header) " 53908b68b0eSGleb Smirnoff "on %s\n", __func__, m->m_len - sizeof(struct ip), 540511a6d5eSKristof Provost if_name(m->m_pkthdr.rcvif)); 541a9771948SGleb Smirnoff m_freem(m); 5428f5a8818SKevin Lo return (IPPROTO_DONE); 543a9771948SGleb Smirnoff } 544a9771948SGleb Smirnoff 5457d508464SMark Johnston if (m->m_len < iplen + sizeof(*vh)) { 54637115154SKristof Provost if ((m = m_pullup(m, iplen + sizeof(*vh))) == NULL) { 5476bf65bcfSRobert Watson CARPSTATS_INC(carps_hdrops); 54837115154SKristof Provost CARP_DEBUG("%s():%d: pullup failed\n", __func__, __LINE__); 5498f5a8818SKevin Lo return (IPPROTO_DONE); 550a9771948SGleb Smirnoff } 551a9771948SGleb Smirnoff } 5527d508464SMark Johnston ip = mtod(m, struct ip *); 5537d508464SMark Johnston totlen = ntohs(ip->ip_len); 55437115154SKristof Provost vh = (struct vrrpv3_header *)((char *)ip + iplen); 555a9771948SGleb Smirnoff 55637115154SKristof Provost switch (vh->vrrp_version) { 55737115154SKristof Provost case CARP_VERSION_CARP: 55837115154SKristof Provost minlen = sizeof(struct carp_header); 55937115154SKristof Provost break; 56037115154SKristof Provost case CARP_VERSION_VRRPv3: 56137115154SKristof Provost minlen = sizeof(struct vrrpv3_header); 56237115154SKristof Provost break; 56337115154SKristof Provost default: 56437115154SKristof Provost CARPSTATS_INC(carps_badver); 56537115154SKristof Provost CARP_DEBUG("%s: unsupported version %d on %s\n", __func__, 56637115154SKristof Provost vh->vrrp_version, if_name(m->m_pkthdr.rcvif)); 56737115154SKristof Provost m_freem(m); 56837115154SKristof Provost return (IPPROTO_DONE); 56937115154SKristof Provost } 57037115154SKristof Provost 57137115154SKristof Provost /* And now check the length again but with the real minimal length. */ 57237115154SKristof Provost if (m->m_pkthdr.len < iplen + minlen) { 5736bf65bcfSRobert Watson CARPSTATS_INC(carps_badlen); 57437115154SKristof Provost CARP_DEBUG("%s: received len %zd < %d " 57537115154SKristof Provost "on %s\n", __func__, m->m_len - sizeof(struct ip), 57637115154SKristof Provost iplen + minlen, 577511a6d5eSKristof Provost if_name(m->m_pkthdr.rcvif)); 578a9771948SGleb Smirnoff m_freem(m); 5798f5a8818SKevin Lo return (IPPROTO_DONE); 580a9771948SGleb Smirnoff } 581a9771948SGleb Smirnoff 5827d508464SMark Johnston if (m->m_len < iplen + minlen) { 58337115154SKristof Provost if ((m = m_pullup(m, iplen + minlen)) == NULL) { 5846bf65bcfSRobert Watson CARPSTATS_INC(carps_hdrops); 58537115154SKristof Provost CARP_DEBUG("%s():%d: pullup failed\n", __func__, __LINE__); 5868f5a8818SKevin Lo return (IPPROTO_DONE); 587a9771948SGleb Smirnoff } 588a9771948SGleb Smirnoff ip = mtod(m, struct ip *); 58937115154SKristof Provost vh = (struct vrrpv3_header *)((char *)ip + iplen); 59037115154SKristof Provost } 591a9771948SGleb Smirnoff 592a254d687SGleb Smirnoff switch (vh->vrrp_version) { 593a254d687SGleb Smirnoff case CARP_VERSION_CARP: { 594a254d687SGleb Smirnoff struct carp_header *ch; 59537115154SKristof Provost 596a9771948SGleb Smirnoff /* verify the CARP checksum */ 5977d508464SMark Johnston if (in_cksum_skip(m, totlen, iplen)) { 5986bf65bcfSRobert Watson CARPSTATS_INC(carps_badsum); 59908b68b0eSGleb Smirnoff CARP_DEBUG("%s: checksum failed on %s\n", __func__, 600511a6d5eSKristof Provost if_name(m->m_pkthdr.rcvif)); 601a9771948SGleb Smirnoff m_freem(m); 602a254d687SGleb Smirnoff break; 603a9771948SGleb Smirnoff } 604a254d687SGleb Smirnoff ch = (struct carp_header *)((char *)ip + iplen); 60513781800SKristof Provost carp_input_c(m, ch, AF_INET, ip->ip_ttl); 60637115154SKristof Provost break; 60737115154SKristof Provost } 608a254d687SGleb Smirnoff case CARP_VERSION_VRRPv3: { 609a254d687SGleb Smirnoff uint16_t phdrcksum; 610a254d687SGleb Smirnoff 611a254d687SGleb Smirnoff phdrcksum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 612a254d687SGleb Smirnoff htonl((u_short)(totlen - iplen) + ip->ip_p)); 613a254d687SGleb Smirnoff vrrp_input_c(m, iplen, AF_INET, ip->ip_ttl, totlen - iplen, 614a254d687SGleb Smirnoff phdrcksum); 61537115154SKristof Provost break; 616a254d687SGleb Smirnoff } 61737115154SKristof Provost default: 61837115154SKristof Provost KASSERT(false, ("Unsupported version %d", vh->vrrp_version)); 61937115154SKristof Provost } 62037115154SKristof Provost 6218f5a8818SKevin Lo return (IPPROTO_DONE); 622a9771948SGleb Smirnoff } 623a0ae8f04SBjoern A. Zeeb #endif 624a9771948SGleb Smirnoff 625a9771948SGleb Smirnoff #ifdef INET6 62678b1fc05SGleb Smirnoff static int 627a9771948SGleb Smirnoff carp6_input(struct mbuf **mp, int *offp, int proto) 628a9771948SGleb Smirnoff { 629a9771948SGleb Smirnoff struct mbuf *m = *mp; 630a9771948SGleb Smirnoff struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 63137115154SKristof Provost struct vrrpv3_header *vh; 63237115154SKristof Provost u_int len, minlen; 633a9771948SGleb Smirnoff 6346bf65bcfSRobert Watson CARPSTATS_INC(carps_ipackets6); 635a9771948SGleb Smirnoff 636c5c392e7SMikolaj Golub if (!V_carp_allow) { 637a9771948SGleb Smirnoff m_freem(m); 638a9771948SGleb Smirnoff return (IPPROTO_DONE); 639a9771948SGleb Smirnoff } 640a9771948SGleb Smirnoff 641a9771948SGleb Smirnoff /* check if received on a valid carp interface */ 642a9771948SGleb Smirnoff if (m->m_pkthdr.rcvif->if_carp == NULL) { 6436bf65bcfSRobert Watson CARPSTATS_INC(carps_badif); 64408b68b0eSGleb Smirnoff CARP_DEBUG("%s: packet received on non-carp interface: %s\n", 645511a6d5eSKristof Provost __func__, if_name(m->m_pkthdr.rcvif)); 646a9771948SGleb Smirnoff m_freem(m); 647a9771948SGleb Smirnoff return (IPPROTO_DONE); 648a9771948SGleb Smirnoff } 649a9771948SGleb Smirnoff 65037115154SKristof Provost if (m->m_len < *offp + sizeof(*vh)) { 651a9771948SGleb Smirnoff len = m->m_len; 65237115154SKristof Provost m = m_pullup(m, *offp + sizeof(*vh)); 65363abacc2SBjoern A. Zeeb if (m == NULL) { 6546bf65bcfSRobert Watson CARPSTATS_INC(carps_badlen); 65508b68b0eSGleb Smirnoff CARP_DEBUG("%s: packet size %u too small\n", __func__, len); 656a9771948SGleb Smirnoff return (IPPROTO_DONE); 657a9771948SGleb Smirnoff } 65813781800SKristof Provost ip6 = mtod(m, struct ip6_hdr *); 659a4adf6ccSBjoern A. Zeeb } 66037115154SKristof Provost vh = (struct vrrpv3_header *)(mtod(m, char *) + *offp); 66137115154SKristof Provost 66237115154SKristof Provost switch (vh->vrrp_version) { 66337115154SKristof Provost case CARP_VERSION_CARP: 66437115154SKristof Provost minlen = sizeof(struct carp_header); 66537115154SKristof Provost break; 66637115154SKristof Provost case CARP_VERSION_VRRPv3: 66737115154SKristof Provost minlen = sizeof(struct vrrpv3_header); 66837115154SKristof Provost break; 66937115154SKristof Provost default: 67037115154SKristof Provost CARPSTATS_INC(carps_badver); 67137115154SKristof Provost CARP_DEBUG("%s: unsupported version %d on %s\n", __func__, 67237115154SKristof Provost vh->vrrp_version, if_name(m->m_pkthdr.rcvif)); 67337115154SKristof Provost m_freem(m); 67437115154SKristof Provost return (IPPROTO_DONE); 67537115154SKristof Provost } 67637115154SKristof Provost 67737115154SKristof Provost /* And now check the length again but with the real minimal length. */ 67837115154SKristof Provost if (m->m_pkthdr.len < sizeof(*ip6) + minlen) { 67937115154SKristof Provost CARPSTATS_INC(carps_badlen); 68037115154SKristof Provost CARP_DEBUG("%s: received len %zd < %zd " 68137115154SKristof Provost "on %s\n", __func__, m->m_len - sizeof(struct ip), 68237115154SKristof Provost sizeof(*ip6) + minlen, 68337115154SKristof Provost if_name(m->m_pkthdr.rcvif)); 68437115154SKristof Provost m_freem(m); 68537115154SKristof Provost return (IPPROTO_DONE); 68637115154SKristof Provost } 68737115154SKristof Provost 6887d508464SMark Johnston if (m->m_len < sizeof(*ip6) + minlen) { 68937115154SKristof Provost if ((m = m_pullup(m, sizeof(*ip6) + minlen)) == NULL) { 69037115154SKristof Provost CARPSTATS_INC(carps_hdrops); 69137115154SKristof Provost CARP_DEBUG("%s():%d: pullup failed\n", __func__, __LINE__); 69237115154SKristof Provost return (IPPROTO_DONE); 69337115154SKristof Provost } 69437115154SKristof Provost ip6 = mtod(m, struct ip6_hdr *); 69537115154SKristof Provost vh = (struct vrrpv3_header *)mtodo(m, sizeof(*ip6)); 69637115154SKristof Provost } 69737115154SKristof Provost 698a254d687SGleb Smirnoff switch (vh->vrrp_version) { 699a254d687SGleb Smirnoff case CARP_VERSION_CARP: { 700a254d687SGleb Smirnoff struct carp_header *ch; 701a9771948SGleb Smirnoff 702a9771948SGleb Smirnoff /* verify the CARP checksum */ 7037d508464SMark Johnston if (in_cksum_skip(m, *offp + sizeof(struct carp_header), 7047d508464SMark Johnston *offp)) { 7056bf65bcfSRobert Watson CARPSTATS_INC(carps_badsum); 70608b68b0eSGleb Smirnoff CARP_DEBUG("%s: checksum failed, on %s\n", __func__, 707511a6d5eSKristof Provost if_name(m->m_pkthdr.rcvif)); 708a9771948SGleb Smirnoff m_freem(m); 709a254d687SGleb Smirnoff break; 710a9771948SGleb Smirnoff } 711a254d687SGleb Smirnoff ch = (struct carp_header *)((char *)ip6 + sizeof(*ip6)); 71213781800SKristof Provost carp_input_c(m, ch, AF_INET6, ip6->ip6_hlim); 71337115154SKristof Provost break; 71437115154SKristof Provost } 715a254d687SGleb Smirnoff case CARP_VERSION_VRRPv3: { 716a254d687SGleb Smirnoff uint16_t phdrcksum; 717a254d687SGleb Smirnoff 718a254d687SGleb Smirnoff phdrcksum = in6_cksum_pseudo(ip6, ntohs(ip6->ip6_plen), 719a254d687SGleb Smirnoff ip6->ip6_nxt, 0); 720a254d687SGleb Smirnoff vrrp_input_c(m, sizeof(*ip6), AF_INET6, ip6->ip6_hlim, 721a254d687SGleb Smirnoff ntohs(ip6->ip6_plen), phdrcksum); 72237115154SKristof Provost break; 723a254d687SGleb Smirnoff } 72437115154SKristof Provost default: 72537115154SKristof Provost KASSERT(false, ("Unsupported version %d", vh->vrrp_version)); 72637115154SKristof Provost } 727a9771948SGleb Smirnoff return (IPPROTO_DONE); 728a9771948SGleb Smirnoff } 729a9771948SGleb Smirnoff #endif /* INET6 */ 730a9771948SGleb Smirnoff 7318151740cSJosh Paetzel /* 7328151740cSJosh Paetzel * This routine should not be necessary at all, but some switches 7338151740cSJosh Paetzel * (VMWare ESX vswitches) can echo our own packets back at us, 7348151740cSJosh Paetzel * and we must ignore them or they will cause us to drop out of 7358151740cSJosh Paetzel * MASTER mode. 7368151740cSJosh Paetzel * 7378151740cSJosh Paetzel * We cannot catch all cases of network loops. Instead, what we 7388151740cSJosh Paetzel * do here is catch any packet that arrives with a carp header 7398151740cSJosh Paetzel * with a VHID of 0, that comes from an address that is our own. 7408151740cSJosh Paetzel * These packets are by definition "from us" (even if they are from 7418151740cSJosh Paetzel * a misconfigured host that is pretending to be us). 7428151740cSJosh Paetzel * 7438151740cSJosh Paetzel * The VHID test is outside this mini-function. 7448151740cSJosh Paetzel */ 7458151740cSJosh Paetzel static int 74637115154SKristof Provost carp_source_is_self(const struct mbuf *m, struct ifaddr *ifa, sa_family_t af) 7478151740cSJosh Paetzel { 748cfff8d3dSEnji Cooper #ifdef INET 7498151740cSJosh Paetzel struct ip *ip4; 7508151740cSJosh Paetzel struct in_addr in4; 751cfff8d3dSEnji Cooper #endif 752cfff8d3dSEnji Cooper #ifdef INET6 7538151740cSJosh Paetzel struct ip6_hdr *ip6; 7548151740cSJosh Paetzel struct in6_addr in6; 755cfff8d3dSEnji Cooper #endif 7568151740cSJosh Paetzel 7578151740cSJosh Paetzel switch (af) { 758cfff8d3dSEnji Cooper #ifdef INET 7598151740cSJosh Paetzel case AF_INET: 7608151740cSJosh Paetzel ip4 = mtod(m, struct ip *); 7618151740cSJosh Paetzel in4 = ifatoia(ifa)->ia_addr.sin_addr; 7628151740cSJosh Paetzel return (in4.s_addr == ip4->ip_src.s_addr); 763cfff8d3dSEnji Cooper #endif 764cfff8d3dSEnji Cooper #ifdef INET6 7658151740cSJosh Paetzel case AF_INET6: 7668151740cSJosh Paetzel ip6 = mtod(m, struct ip6_hdr *); 7678151740cSJosh Paetzel in6 = ifatoia6(ifa)->ia_addr.sin6_addr; 7688151740cSJosh Paetzel return (memcmp(&in6, &ip6->ip6_src, sizeof(in6)) == 0); 769cfff8d3dSEnji Cooper #endif 770cfff8d3dSEnji Cooper default: 7718151740cSJosh Paetzel break; 7728151740cSJosh Paetzel } 7738151740cSJosh Paetzel return (0); 7748151740cSJosh Paetzel } 7758151740cSJosh Paetzel 77637115154SKristof Provost static struct ifaddr * 77737115154SKristof Provost carp_find_ifa(const struct mbuf *m, sa_family_t af, uint8_t vhid) 778a9771948SGleb Smirnoff { 779a9771948SGleb Smirnoff struct ifnet *ifp = m->m_pkthdr.rcvif; 7808151740cSJosh Paetzel struct ifaddr *ifa, *match; 7818151740cSJosh Paetzel int error; 782a9771948SGleb Smirnoff 783b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 784b8a6e03fSGleb Smirnoff 7858151740cSJosh Paetzel /* 7868151740cSJosh Paetzel * Verify that the VHID is valid on the receiving interface. 7878151740cSJosh Paetzel * 7888151740cSJosh Paetzel * There should be just one match. If there are none 7898151740cSJosh Paetzel * the VHID is not valid and we drop the packet. If 7908151740cSJosh Paetzel * there are multiple VHID matches, take just the first 7918151740cSJosh Paetzel * one, for compatibility with previous code. While we're 7928151740cSJosh Paetzel * scanning, check for obvious loops in the network topology 7938151740cSJosh Paetzel * (these should never happen, and as noted above, we may 7948151740cSJosh Paetzel * miss real loops; this is just a double-check). 7958151740cSJosh Paetzel */ 7968151740cSJosh Paetzel error = 0; 7978151740cSJosh Paetzel match = NULL; 7988151740cSJosh Paetzel IFNET_FOREACH_IFA(ifp, ifa) { 7998151740cSJosh Paetzel if (match == NULL && ifa->ifa_carp != NULL && 8008151740cSJosh Paetzel ifa->ifa_addr->sa_family == af && 80137115154SKristof Provost ifa->ifa_carp->sc_vhid == vhid) 8028151740cSJosh Paetzel match = ifa; 80337115154SKristof Provost if (vhid == 0 && carp_source_is_self(m, ifa, af)) 8048151740cSJosh Paetzel error = ELOOP; 80508b68b0eSGleb Smirnoff } 8068151740cSJosh Paetzel ifa = error ? NULL : match; 8078151740cSJosh Paetzel if (ifa != NULL) 8088151740cSJosh Paetzel ifa_ref(ifa); 809d220759bSGleb Smirnoff 81008b68b0eSGleb Smirnoff if (ifa == NULL) { 8118151740cSJosh Paetzel if (error == ELOOP) { 8128151740cSJosh Paetzel CARP_DEBUG("dropping looped packet on interface %s\n", 813511a6d5eSKristof Provost if_name(ifp)); 8148151740cSJosh Paetzel CARPSTATS_INC(carps_badif); /* ??? */ 8158151740cSJosh Paetzel } else { 8166bf65bcfSRobert Watson CARPSTATS_INC(carps_badvhid); 8178151740cSJosh Paetzel } 81837115154SKristof Provost } 81937115154SKristof Provost 82037115154SKristof Provost return (ifa); 82137115154SKristof Provost } 82237115154SKristof Provost 82337115154SKristof Provost static void 82437115154SKristof Provost carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af, int ttl) 82537115154SKristof Provost { 82637115154SKristof Provost struct ifnet *ifp = m->m_pkthdr.rcvif; 82737115154SKristof Provost struct ifaddr *ifa; 82837115154SKristof Provost struct carp_softc *sc; 82937115154SKristof Provost uint64_t tmp_counter; 83037115154SKristof Provost struct timeval sc_tv, ch_tv; 83137115154SKristof Provost bool multicast = false; 83237115154SKristof Provost 83337115154SKristof Provost NET_EPOCH_ASSERT(); 834cda57d95SGleb Smirnoff MPASS(ch->carp_version == CARP_VERSION_CARP); 83537115154SKristof Provost 83637115154SKristof Provost ifa = carp_find_ifa(m, af, ch->carp_vhid); 83737115154SKristof Provost if (ifa == NULL) { 838a9771948SGleb Smirnoff m_freem(m); 839a9771948SGleb Smirnoff return; 840a9771948SGleb Smirnoff } 841a9771948SGleb Smirnoff 84237115154SKristof Provost sc = ifa->ifa_carp; 84337115154SKristof Provost CARP_LOCK(sc); 84437115154SKristof Provost 845a9771948SGleb Smirnoff /* verify the CARP version. */ 846cda57d95SGleb Smirnoff if (sc->sc_version != CARP_VERSION_CARP) { 84737115154SKristof Provost CARP_UNLOCK(sc); 84837115154SKristof Provost 8496bf65bcfSRobert Watson CARPSTATS_INC(carps_badver); 850511a6d5eSKristof Provost CARP_DEBUG("%s: invalid version %d\n", if_name(ifp), 8511e9e6572SGleb Smirnoff ch->carp_version); 85208b68b0eSGleb Smirnoff ifa_free(ifa); 853a9771948SGleb Smirnoff m_freem(m); 854a9771948SGleb Smirnoff return; 855a9771948SGleb Smirnoff } 856a9771948SGleb Smirnoff 85713781800SKristof Provost if (ifa->ifa_addr->sa_family == AF_INET) { 85813781800SKristof Provost multicast = IN_MULTICAST(sc->sc_carpaddr.s_addr); 85913781800SKristof Provost } else { 86013781800SKristof Provost multicast = IN6_IS_ADDR_MULTICAST(&sc->sc_carpaddr6); 86113781800SKristof Provost } 86208b68b0eSGleb Smirnoff ifa_free(ifa); 86308b68b0eSGleb Smirnoff 86413781800SKristof Provost /* verify that the IP TTL is 255, but only if we're not in unicast mode. */ 86513781800SKristof Provost if (multicast && ttl != CARP_DFLTTL) { 86613781800SKristof Provost CARPSTATS_INC(carps_badttl); 86713781800SKristof Provost CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__, 868511a6d5eSKristof Provost ttl, if_name(m->m_pkthdr.rcvif)); 86913781800SKristof Provost goto out; 87013781800SKristof Provost } 87113781800SKristof Provost 872a9771948SGleb Smirnoff if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) { 8736bf65bcfSRobert Watson CARPSTATS_INC(carps_badauth); 87408b68b0eSGleb Smirnoff CARP_DEBUG("%s: incorrect hash for VHID %u@%s\n", __func__, 875511a6d5eSKristof Provost sc->sc_vhid, if_name(ifp)); 87608b68b0eSGleb Smirnoff goto out; 877a9771948SGleb Smirnoff } 878a9771948SGleb Smirnoff 879a9771948SGleb Smirnoff tmp_counter = ntohl(ch->carp_counter[0]); 880a9771948SGleb Smirnoff tmp_counter = tmp_counter<<32; 881a9771948SGleb Smirnoff tmp_counter += ntohl(ch->carp_counter[1]); 882a9771948SGleb Smirnoff 883a9771948SGleb Smirnoff /* XXX Replay protection goes here */ 884a9771948SGleb Smirnoff 885a254d687SGleb Smirnoff sc->sc_init_counter = false; 886a9771948SGleb Smirnoff sc->sc_counter = tmp_counter; 887a9771948SGleb Smirnoff 888a9771948SGleb Smirnoff sc_tv.tv_sec = sc->sc_advbase; 889f08535f8SGleb Smirnoff sc_tv.tv_usec = DEMOTE_ADVSKEW(sc) * 1000000 / 256; 890a9771948SGleb Smirnoff ch_tv.tv_sec = ch->carp_advbase; 891a9771948SGleb Smirnoff ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256; 892a9771948SGleb Smirnoff 893a9771948SGleb Smirnoff switch (sc->sc_state) { 894a9771948SGleb Smirnoff case INIT: 895a9771948SGleb Smirnoff break; 896a9771948SGleb Smirnoff case MASTER: 897a9771948SGleb Smirnoff /* 898a9771948SGleb Smirnoff * If we receive an advertisement from a master who's going to 899a9771948SGleb Smirnoff * be more frequent than us, go into BACKUP state. 900a9771948SGleb Smirnoff */ 901a9771948SGleb Smirnoff if (timevalcmp(&sc_tv, &ch_tv, >) || 902a9771948SGleb Smirnoff timevalcmp(&sc_tv, &ch_tv, ==)) { 903a9771948SGleb Smirnoff callout_stop(&sc->sc_ad_tmo); 904d01641e2SWill Andrews carp_set_state(sc, BACKUP, 905d01641e2SWill Andrews "more frequent advertisement received"); 906a9771948SGleb Smirnoff carp_setrun(sc, 0); 90708b68b0eSGleb Smirnoff carp_delroute(sc); 908a9771948SGleb Smirnoff } 909a9771948SGleb Smirnoff break; 910a9771948SGleb Smirnoff case BACKUP: 911a9771948SGleb Smirnoff /* 912a9771948SGleb Smirnoff * If we're pre-empting masters who advertise slower than us, 913a9771948SGleb Smirnoff * and this one claims to be slower, treat him as down. 914a9771948SGleb Smirnoff */ 915c5c392e7SMikolaj Golub if (V_carp_preempt && timevalcmp(&sc_tv, &ch_tv, <)) { 916d01641e2SWill Andrews carp_master_down_locked(sc, 917d01641e2SWill Andrews "preempting a slower master"); 918a9771948SGleb Smirnoff break; 919a9771948SGleb Smirnoff } 920a9771948SGleb Smirnoff 921a9771948SGleb Smirnoff /* 922a9771948SGleb Smirnoff * If the master is going to advertise at such a low frequency 923a9771948SGleb Smirnoff * that he's guaranteed to time out, we'd might as well just 924a9771948SGleb Smirnoff * treat him as timed out now. 925a9771948SGleb Smirnoff */ 926a9771948SGleb Smirnoff sc_tv.tv_sec = sc->sc_advbase * 3; 927a9771948SGleb Smirnoff if (timevalcmp(&sc_tv, &ch_tv, <)) { 928d01641e2SWill Andrews carp_master_down_locked(sc, "master will time out"); 929a9771948SGleb Smirnoff break; 930a9771948SGleb Smirnoff } 931a9771948SGleb Smirnoff 932a9771948SGleb Smirnoff /* 933a9771948SGleb Smirnoff * Otherwise, we reset the counter and wait for the next 934a9771948SGleb Smirnoff * advertisement. 935a9771948SGleb Smirnoff */ 936a9771948SGleb Smirnoff carp_setrun(sc, af); 937a9771948SGleb Smirnoff break; 938a9771948SGleb Smirnoff } 939a9771948SGleb Smirnoff 94008b68b0eSGleb Smirnoff out: 94108b68b0eSGleb Smirnoff CARP_UNLOCK(sc); 942a9771948SGleb Smirnoff m_freem(m); 943a9771948SGleb Smirnoff } 944a9771948SGleb Smirnoff 94537115154SKristof Provost static void 94637115154SKristof Provost vrrp_input_c(struct mbuf *m, int off, sa_family_t af, int ttl, 94737115154SKristof Provost int len, uint16_t phdrcksum) 94837115154SKristof Provost { 94937115154SKristof Provost struct vrrpv3_header *vh = mtodo(m, off); 95037115154SKristof Provost struct ifnet *ifp = m->m_pkthdr.rcvif; 95137115154SKristof Provost struct ifaddr *ifa; 95237115154SKristof Provost struct carp_softc *sc; 95337115154SKristof Provost 95437115154SKristof Provost NET_EPOCH_ASSERT(); 955cda57d95SGleb Smirnoff MPASS(vh->vrrp_version == CARP_VERSION_VRRPv3); 95637115154SKristof Provost 95737115154SKristof Provost ifa = carp_find_ifa(m, af, vh->vrrp_vrtid); 95837115154SKristof Provost if (ifa == NULL) { 95937115154SKristof Provost m_freem(m); 96037115154SKristof Provost return; 96137115154SKristof Provost } 96237115154SKristof Provost 96337115154SKristof Provost sc = ifa->ifa_carp; 96437115154SKristof Provost CARP_LOCK(sc); 96537115154SKristof Provost 96637115154SKristof Provost ifa_free(ifa); 96737115154SKristof Provost 96837115154SKristof Provost /* verify the CARP version. */ 969cda57d95SGleb Smirnoff if (sc->sc_version != CARP_VERSION_VRRPv3) { 97037115154SKristof Provost CARP_UNLOCK(sc); 97137115154SKristof Provost 97237115154SKristof Provost CARPSTATS_INC(carps_badver); 97337115154SKristof Provost CARP_DEBUG("%s: invalid version %d\n", if_name(ifp), 97437115154SKristof Provost vh->vrrp_version); 97537115154SKristof Provost m_freem(m); 97637115154SKristof Provost return; 97737115154SKristof Provost } 97837115154SKristof Provost 97937115154SKristof Provost /* verify that the IP TTL is 255. */ 98037115154SKristof Provost if (ttl != CARP_DFLTTL) { 98137115154SKristof Provost CARPSTATS_INC(carps_badttl); 98237115154SKristof Provost CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__, 98337115154SKristof Provost ttl, if_name(m->m_pkthdr.rcvif)); 98437115154SKristof Provost goto out; 98537115154SKristof Provost } 98637115154SKristof Provost 98737115154SKristof Provost if (vrrp_checksum_verify(m, off, len, phdrcksum)) { 98837115154SKristof Provost CARPSTATS_INC(carps_badsum); 98937115154SKristof Provost CARP_DEBUG("%s: incorrect checksum for VRID %u@%s\n", __func__, 99037115154SKristof Provost sc->sc_vhid, if_name(ifp)); 99137115154SKristof Provost goto out; 99237115154SKristof Provost } 99337115154SKristof Provost 99437115154SKristof Provost /* RFC9568, 7.1 Receiving VRRP packets. */ 99537115154SKristof Provost if (sc->sc_vrrp_prio == 255) { 99637115154SKristof Provost CARP_DEBUG("%s: our priority is 255. Ignore peer announcement.\n", 99737115154SKristof Provost __func__); 99837115154SKristof Provost goto out; 99937115154SKristof Provost } 100037115154SKristof Provost 100137115154SKristof Provost /* XXX TODO Check IP address payload. */ 100237115154SKristof Provost 100337115154SKristof Provost sc->sc_vrrp_master_inter = ntohs(vh->vrrp_max_adver_int); 100437115154SKristof Provost 100537115154SKristof Provost switch (sc->sc_state) { 100637115154SKristof Provost case INIT: 100737115154SKristof Provost break; 100837115154SKristof Provost case MASTER: 100937115154SKristof Provost /* 101037115154SKristof Provost * If we receive an advertisement from a master who's going to 101137115154SKristof Provost * be more frequent than us, go into BACKUP state. 101237115154SKristof Provost * Same if the peer has a higher priority than us. 101337115154SKristof Provost */ 101437115154SKristof Provost if (ntohs(vh->vrrp_max_adver_int) < sc->sc_vrrp_adv_inter || 101537115154SKristof Provost vh->vrrp_priority > sc->sc_vrrp_prio) { 101637115154SKristof Provost callout_stop(&sc->sc_ad_tmo); 101737115154SKristof Provost carp_set_state(sc, BACKUP, 101837115154SKristof Provost "more frequent advertisement received"); 101937115154SKristof Provost carp_setrun(sc, 0); 102037115154SKristof Provost carp_delroute(sc); 102137115154SKristof Provost } 102237115154SKristof Provost break; 102337115154SKristof Provost case BACKUP: 102437115154SKristof Provost /* 102537115154SKristof Provost * If we're pre-empting masters who advertise slower than us, 102637115154SKristof Provost * and this one claims to be slower, treat him as down. 102737115154SKristof Provost */ 102837115154SKristof Provost if (V_carp_preempt && (ntohs(vh->vrrp_max_adver_int) > sc->sc_vrrp_adv_inter 102937115154SKristof Provost || vh->vrrp_priority < sc->sc_vrrp_prio)) { 103037115154SKristof Provost carp_master_down_locked(sc, 103137115154SKristof Provost "preempting a slower master"); 103237115154SKristof Provost break; 103337115154SKristof Provost } 103437115154SKristof Provost 103537115154SKristof Provost /* 103637115154SKristof Provost * Otherwise, we reset the counter and wait for the next 103737115154SKristof Provost * advertisement. 103837115154SKristof Provost */ 103937115154SKristof Provost carp_setrun(sc, af); 104037115154SKristof Provost break; 104137115154SKristof Provost } 104237115154SKristof Provost 104337115154SKristof Provost out: 104437115154SKristof Provost CARP_UNLOCK(sc); 104537115154SKristof Provost m_freem(m); 104637115154SKristof Provost } 104737115154SKristof Provost 104837115154SKristof Provost static int 104937115154SKristof Provost carp_tag(struct carp_softc *sc, struct mbuf *m) 105037115154SKristof Provost { 105137115154SKristof Provost struct m_tag *mtag; 105237115154SKristof Provost 105337115154SKristof Provost /* Tag packet for carp_output */ 1054601438fbSGleb Smirnoff if ((mtag = m_tag_get(PACKET_TAG_CARP, sizeof(sc->sc_vhid), 105537115154SKristof Provost M_NOWAIT)) == NULL) { 105637115154SKristof Provost m_freem(m); 105737115154SKristof Provost CARPSTATS_INC(carps_onomem); 105837115154SKristof Provost return (ENOMEM); 105937115154SKristof Provost } 1060601438fbSGleb Smirnoff bcopy(&sc->sc_vhid, mtag + 1, sizeof(sc->sc_vhid)); 106137115154SKristof Provost m_tag_prepend(m, mtag); 106237115154SKristof Provost 106337115154SKristof Provost return (0); 106437115154SKristof Provost } 106537115154SKristof Provost 1066601438fbSGleb Smirnoff static void 1067a9771948SGleb Smirnoff carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch) 1068a9771948SGleb Smirnoff { 1069a9771948SGleb Smirnoff 1070601438fbSGleb Smirnoff MPASS(sc->sc_version == CARP_VERSION_CARP); 1071601438fbSGleb Smirnoff 1072a9771948SGleb Smirnoff if (sc->sc_init_counter) { 1073a9771948SGleb Smirnoff /* this could also be seconds since unix epoch */ 1074a9771948SGleb Smirnoff sc->sc_counter = arc4random(); 1075a9771948SGleb Smirnoff sc->sc_counter = sc->sc_counter << 32; 1076a9771948SGleb Smirnoff sc->sc_counter += arc4random(); 1077a9771948SGleb Smirnoff } else 1078a9771948SGleb Smirnoff sc->sc_counter++; 1079a9771948SGleb Smirnoff 1080a9771948SGleb Smirnoff ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff); 1081a9771948SGleb Smirnoff ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff); 1082a9771948SGleb Smirnoff 1083a9771948SGleb Smirnoff carp_hmac_generate(sc, ch->carp_counter, ch->carp_md); 1084a9771948SGleb Smirnoff } 1085a9771948SGleb Smirnoff 10865ee92cbdSGleb Smirnoff static inline void 10875ee92cbdSGleb Smirnoff send_ad_locked(struct carp_softc *sc) 10885ee92cbdSGleb Smirnoff { 10895ee92cbdSGleb Smirnoff switch (sc->sc_version) { 10905ee92cbdSGleb Smirnoff case CARP_VERSION_CARP: 10915ee92cbdSGleb Smirnoff carp_send_ad_locked(sc); 10925ee92cbdSGleb Smirnoff break; 10935ee92cbdSGleb Smirnoff case CARP_VERSION_VRRPv3: 10945ee92cbdSGleb Smirnoff vrrp_send_ad_locked(sc); 10955ee92cbdSGleb Smirnoff break; 10965ee92cbdSGleb Smirnoff } 10975ee92cbdSGleb Smirnoff } 10985ee92cbdSGleb Smirnoff 1099f08535f8SGleb Smirnoff /* 1100f08535f8SGleb Smirnoff * To avoid LORs and possible recursions this function shouldn't 1101f08535f8SGleb Smirnoff * be called directly, but scheduled via taskqueue. 1102f08535f8SGleb Smirnoff */ 11035c1f0f6dSGleb Smirnoff static void 1104f08535f8SGleb Smirnoff carp_send_ad_all(void *ctx __unused, int pending __unused) 1105a9771948SGleb Smirnoff { 1106d92d54d5SGleb Smirnoff struct carp_softc *sc; 11071d126e9bSKristof Provost struct epoch_tracker et; 1108a9771948SGleb Smirnoff 11091d126e9bSKristof Provost NET_EPOCH_ENTER(et); 1110d92d54d5SGleb Smirnoff mtx_lock(&carp_mtx); 111108b68b0eSGleb Smirnoff LIST_FOREACH(sc, &carp_list, sc_next) 1112f08535f8SGleb Smirnoff if (sc->sc_state == MASTER) { 111308b68b0eSGleb Smirnoff CARP_LOCK(sc); 1114afdbac98SGleb Smirnoff CURVNET_SET(sc->sc_carpdev->if_vnet); 11155ee92cbdSGleb Smirnoff send_ad_locked(sc); 1116afdbac98SGleb Smirnoff CURVNET_RESTORE(); 111708b68b0eSGleb Smirnoff CARP_UNLOCK(sc); 1118a9771948SGleb Smirnoff } 1119d92d54d5SGleb Smirnoff mtx_unlock(&carp_mtx); 11201d126e9bSKristof Provost NET_EPOCH_EXIT(et); 1121a9771948SGleb Smirnoff } 1122a9771948SGleb Smirnoff 1123afdbac98SGleb Smirnoff /* Send a periodic advertisement, executed in callout context. */ 11245c1f0f6dSGleb Smirnoff static void 11255ee92cbdSGleb Smirnoff carp_callout(void *v) 1126a9771948SGleb Smirnoff { 1127d220759bSGleb Smirnoff struct carp_softc *sc = v; 11281d126e9bSKristof Provost struct epoch_tracker et; 1129d220759bSGleb Smirnoff 11301d126e9bSKristof Provost NET_EPOCH_ENTER(et); 113108b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); 1132afdbac98SGleb Smirnoff CURVNET_SET(sc->sc_carpdev->if_vnet); 11335ee92cbdSGleb Smirnoff send_ad_locked(sc); 1134afdbac98SGleb Smirnoff CURVNET_RESTORE(); 113508b68b0eSGleb Smirnoff CARP_UNLOCK(sc); 11361d126e9bSKristof Provost NET_EPOCH_EXIT(et); 1137d220759bSGleb Smirnoff } 1138d220759bSGleb Smirnoff 1139d220759bSGleb Smirnoff static void 11404a2dd8d4SGleb Smirnoff carp_send_ad_error(struct carp_softc *sc, int error) 11414a2dd8d4SGleb Smirnoff { 11424a2dd8d4SGleb Smirnoff 11439a8cf950SGleb Smirnoff /* 11446bce41a3SGordon Bergling * We track errors and successful sends with this logic: 11459a8cf950SGleb Smirnoff * - Any error resets success counter to 0. 11469a8cf950SGleb Smirnoff * - MAX_ERRORS triggers demotion. 11479a8cf950SGleb Smirnoff * - MIN_SUCCESS successes resets error counter to 0. 11489a8cf950SGleb Smirnoff * - MIN_SUCCESS reverts demotion, if it was triggered before. 11499a8cf950SGleb Smirnoff */ 11504a2dd8d4SGleb Smirnoff if (error) { 11514a2dd8d4SGleb Smirnoff if (sc->sc_sendad_errors < INT_MAX) 11524a2dd8d4SGleb Smirnoff sc->sc_sendad_errors++; 11534a2dd8d4SGleb Smirnoff if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) { 11544a2dd8d4SGleb Smirnoff static const char fmt[] = "send error %d on %s"; 11554a2dd8d4SGleb Smirnoff char msg[sizeof(fmt) + IFNAMSIZ]; 11564a2dd8d4SGleb Smirnoff 1157511a6d5eSKristof Provost sprintf(msg, fmt, error, if_name(sc->sc_carpdev)); 11584a2dd8d4SGleb Smirnoff carp_demote_adj(V_carp_senderr_adj, msg); 11594a2dd8d4SGleb Smirnoff } 11604a2dd8d4SGleb Smirnoff sc->sc_sendad_success = 0; 11619a8cf950SGleb Smirnoff } else if (sc->sc_sendad_errors > 0) { 11629a8cf950SGleb Smirnoff if (++sc->sc_sendad_success >= CARP_SENDAD_MIN_SUCCESS) { 11639a8cf950SGleb Smirnoff if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) { 11644a2dd8d4SGleb Smirnoff static const char fmt[] = "send ok on %s"; 11654a2dd8d4SGleb Smirnoff char msg[sizeof(fmt) + IFNAMSIZ]; 11664a2dd8d4SGleb Smirnoff 1167511a6d5eSKristof Provost sprintf(msg, fmt, if_name(sc->sc_carpdev)); 11684a2dd8d4SGleb Smirnoff carp_demote_adj(-V_carp_senderr_adj, msg); 11699a8cf950SGleb Smirnoff } 11704a2dd8d4SGleb Smirnoff sc->sc_sendad_errors = 0; 11719a8cf950SGleb Smirnoff } 11724a2dd8d4SGleb Smirnoff } 11734a2dd8d4SGleb Smirnoff } 11744a2dd8d4SGleb Smirnoff 11758151740cSJosh Paetzel /* 11768151740cSJosh Paetzel * Pick the best ifaddr on the given ifp for sending CARP 11778151740cSJosh Paetzel * advertisements. 11788151740cSJosh Paetzel * 11798151740cSJosh Paetzel * "Best" here is defined by ifa_preferred(). This function is much 11808151740cSJosh Paetzel * much like ifaof_ifpforaddr() except that we just use ifa_preferred(). 11818151740cSJosh Paetzel * 11828151740cSJosh Paetzel * (This could be simplified to return the actual address, except that 11838151740cSJosh Paetzel * it has a different format in AF_INET and AF_INET6.) 11848151740cSJosh Paetzel */ 11858151740cSJosh Paetzel static struct ifaddr * 11868151740cSJosh Paetzel carp_best_ifa(int af, struct ifnet *ifp) 11878151740cSJosh Paetzel { 11888151740cSJosh Paetzel struct ifaddr *ifa, *best; 11898151740cSJosh Paetzel 1190b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 1191b8a6e03fSGleb Smirnoff 11928151740cSJosh Paetzel if (af >= AF_MAX) 11938151740cSJosh Paetzel return (NULL); 11948151740cSJosh Paetzel best = NULL; 1195d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 11968151740cSJosh Paetzel if (ifa->ifa_addr->sa_family == af && 11978151740cSJosh Paetzel (best == NULL || ifa_preferred(best, ifa))) 11988151740cSJosh Paetzel best = ifa; 11998151740cSJosh Paetzel } 12008151740cSJosh Paetzel if (best != NULL) 12018151740cSJosh Paetzel ifa_ref(best); 12028151740cSJosh Paetzel return (best); 12038151740cSJosh Paetzel } 12048151740cSJosh Paetzel 12054a2dd8d4SGleb Smirnoff static void 1206d220759bSGleb Smirnoff carp_send_ad_locked(struct carp_softc *sc) 1207d220759bSGleb Smirnoff { 1208a9771948SGleb Smirnoff struct carp_header ch; 1209a9771948SGleb Smirnoff struct timeval tv; 121008b68b0eSGleb Smirnoff struct ifaddr *ifa; 1211a9771948SGleb Smirnoff struct carp_header *ch_ptr; 1212a9771948SGleb Smirnoff struct mbuf *m; 121308b68b0eSGleb Smirnoff int len, advskew; 1214a9771948SGleb Smirnoff 12151d126e9bSKristof Provost NET_EPOCH_ASSERT(); 121608b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); 12175ee92cbdSGleb Smirnoff MPASS(sc->sc_version == CARP_VERSION_CARP); 121837115154SKristof Provost 1219f08535f8SGleb Smirnoff advskew = DEMOTE_ADVSKEW(sc); 122008b68b0eSGleb Smirnoff tv.tv_sec = sc->sc_advbase; 1221a9771948SGleb Smirnoff tv.tv_usec = advskew * 1000000 / 256; 1222a9771948SGleb Smirnoff 122337115154SKristof Provost ch.carp_version = CARP_VERSION_CARP; 1224a9771948SGleb Smirnoff ch.carp_type = CARP_ADVERTISEMENT; 1225a9771948SGleb Smirnoff ch.carp_vhid = sc->sc_vhid; 122608b68b0eSGleb Smirnoff ch.carp_advbase = sc->sc_advbase; 1227a9771948SGleb Smirnoff ch.carp_advskew = advskew; 1228a9771948SGleb Smirnoff ch.carp_authlen = 7; /* XXX DEFINE */ 1229a9771948SGleb Smirnoff ch.carp_pad1 = 0; /* must be zero */ 1230a9771948SGleb Smirnoff ch.carp_cksum = 0; 1231a9771948SGleb Smirnoff 123208b68b0eSGleb Smirnoff /* XXXGL: OpenBSD picks first ifaddr with needed family. */ 123308b68b0eSGleb Smirnoff 1234a9771948SGleb Smirnoff #ifdef INET 123508b68b0eSGleb Smirnoff if (sc->sc_naddrs) { 1236a9771948SGleb Smirnoff struct ip *ip; 1237a9771948SGleb Smirnoff 1238dc4ad05eSGleb Smirnoff m = m_gethdr(M_NOWAIT, MT_DATA); 1239a9771948SGleb Smirnoff if (m == NULL) { 12406bf65bcfSRobert Watson CARPSTATS_INC(carps_onomem); 1241891122d1SGleb Smirnoff goto resched; 1242a9771948SGleb Smirnoff } 1243a9771948SGleb Smirnoff len = sizeof(*ip) + sizeof(ch); 1244a9771948SGleb Smirnoff m->m_pkthdr.len = len; 1245a9771948SGleb Smirnoff m->m_pkthdr.rcvif = NULL; 1246a9771948SGleb Smirnoff m->m_len = len; 1247ed6a66caSRobert Watson M_ALIGN(m, m->m_len); 124813781800SKristof Provost if (IN_MULTICAST(sc->sc_carpaddr.s_addr)) 1249a9771948SGleb Smirnoff m->m_flags |= M_MCAST; 1250a9771948SGleb Smirnoff ip = mtod(m, struct ip *); 1251a9771948SGleb Smirnoff ip->ip_v = IPVERSION; 1252a9771948SGleb Smirnoff ip->ip_hl = sizeof(*ip) >> 2; 12530d3d234cSKristof Provost ip->ip_tos = V_carp_dscp << IPTOS_DSCP_OFFSET; 12548f134647SGleb Smirnoff ip->ip_len = htons(len); 12558f134647SGleb Smirnoff ip->ip_off = htons(IP_DF); 1256a9771948SGleb Smirnoff ip->ip_ttl = CARP_DFLTTL; 1257a9771948SGleb Smirnoff ip->ip_p = IPPROTO_CARP; 1258a9771948SGleb Smirnoff ip->ip_sum = 0; 12596d947416SGleb Smirnoff ip_fillid(ip); 126008b68b0eSGleb Smirnoff 12618151740cSJosh Paetzel ifa = carp_best_ifa(AF_INET, sc->sc_carpdev); 126208b68b0eSGleb Smirnoff if (ifa != NULL) { 126308b68b0eSGleb Smirnoff ip->ip_src.s_addr = 126408b68b0eSGleb Smirnoff ifatoia(ifa)->ia_addr.sin_addr.s_addr; 126508b68b0eSGleb Smirnoff ifa_free(ifa); 126608b68b0eSGleb Smirnoff } else 126708b68b0eSGleb Smirnoff ip->ip_src.s_addr = 0; 126813781800SKristof Provost ip->ip_dst = sc->sc_carpaddr; 1269a9771948SGleb Smirnoff 1270a9771948SGleb Smirnoff ch_ptr = (struct carp_header *)(&ip[1]); 1271a9771948SGleb Smirnoff bcopy(&ch, ch_ptr, sizeof(ch)); 1272601438fbSGleb Smirnoff carp_prepare_ad(m, sc, ch_ptr); 1273601438fbSGleb Smirnoff if (IN_MULTICAST(ntohl(sc->sc_carpaddr.s_addr)) && 1274601438fbSGleb Smirnoff carp_tag(sc, m) != 0) 1275891122d1SGleb Smirnoff goto resched; 1276a9771948SGleb Smirnoff 1277a9771948SGleb Smirnoff m->m_data += sizeof(*ip); 1278c4d06976SGleb Smirnoff ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip)); 1279a9771948SGleb Smirnoff m->m_data -= sizeof(*ip); 1280a9771948SGleb Smirnoff 12816bf65bcfSRobert Watson CARPSTATS_INC(carps_opackets); 1282a9771948SGleb Smirnoff 12834a2dd8d4SGleb Smirnoff carp_send_ad_error(sc, ip_output(m, NULL, NULL, IP_RAWOUTPUT, 12844a2dd8d4SGleb Smirnoff &sc->sc_carpdev->if_carp->cif_imo, NULL)); 1285a9771948SGleb Smirnoff } 1286a9771948SGleb Smirnoff #endif /* INET */ 1287a9771948SGleb Smirnoff #ifdef INET6 128808b68b0eSGleb Smirnoff if (sc->sc_naddrs6) { 1289a9771948SGleb Smirnoff struct ip6_hdr *ip6; 1290a9771948SGleb Smirnoff 1291dc4ad05eSGleb Smirnoff m = m_gethdr(M_NOWAIT, MT_DATA); 1292a9771948SGleb Smirnoff if (m == NULL) { 12936bf65bcfSRobert Watson CARPSTATS_INC(carps_onomem); 1294891122d1SGleb Smirnoff goto resched; 1295a9771948SGleb Smirnoff } 1296a9771948SGleb Smirnoff len = sizeof(*ip6) + sizeof(ch); 1297a9771948SGleb Smirnoff m->m_pkthdr.len = len; 1298a9771948SGleb Smirnoff m->m_pkthdr.rcvif = NULL; 1299a9771948SGleb Smirnoff m->m_len = len; 1300ed6a66caSRobert Watson M_ALIGN(m, m->m_len); 1301a9771948SGleb Smirnoff ip6 = mtod(m, struct ip6_hdr *); 1302a9771948SGleb Smirnoff bzero(ip6, sizeof(*ip6)); 1303a9771948SGleb Smirnoff ip6->ip6_vfc |= IPV6_VERSION; 13040d3d234cSKristof Provost /* Traffic class isn't defined in ip6 struct instead 13050d3d234cSKristof Provost * it gets offset into flowid field */ 13060d3d234cSKristof Provost ip6->ip6_flow |= htonl(V_carp_dscp << (IPV6_FLOWLABEL_LEN + 13070d3d234cSKristof Provost IPTOS_DSCP_OFFSET)); 1308a9771948SGleb Smirnoff ip6->ip6_hlim = CARP_DFLTTL; 1309a9771948SGleb Smirnoff ip6->ip6_nxt = IPPROTO_CARP; 1310a9771948SGleb Smirnoff 131108b68b0eSGleb Smirnoff /* set the source address */ 13128151740cSJosh Paetzel ifa = carp_best_ifa(AF_INET6, sc->sc_carpdev); 131308b68b0eSGleb Smirnoff if (ifa != NULL) { 131408b68b0eSGleb Smirnoff bcopy(IFA_IN6(ifa), &ip6->ip6_src, 131508b68b0eSGleb Smirnoff sizeof(struct in6_addr)); 131608b68b0eSGleb Smirnoff ifa_free(ifa); 131708b68b0eSGleb Smirnoff } else 131808b68b0eSGleb Smirnoff /* This should never happen with IPv6. */ 131908b68b0eSGleb Smirnoff bzero(&ip6->ip6_src, sizeof(struct in6_addr)); 132008b68b0eSGleb Smirnoff 132108b68b0eSGleb Smirnoff /* Set the multicast destination. */ 132213781800SKristof Provost memcpy(&ip6->ip6_dst, &sc->sc_carpaddr6, sizeof(ip6->ip6_dst)); 1323c2c28c0fSKristof Provost if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1324c2c28c0fSKristof Provost IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) { 13257002145dSBjoern A. Zeeb if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) { 13267002145dSBjoern A. Zeeb m_freem(m); 1327e81ab876SGleb Smirnoff CARP_DEBUG("%s: in6_setscope failed\n", __func__); 1328891122d1SGleb Smirnoff goto resched; 13297002145dSBjoern A. Zeeb } 133013781800SKristof Provost } 1331a9771948SGleb Smirnoff 1332a9771948SGleb Smirnoff ch_ptr = (struct carp_header *)(&ip6[1]); 1333a9771948SGleb Smirnoff bcopy(&ch, ch_ptr, sizeof(ch)); 1334601438fbSGleb Smirnoff carp_prepare_ad(m, sc, ch_ptr); 1335601438fbSGleb Smirnoff if (IN6_IS_ADDR_MULTICAST(&sc->sc_carpaddr6) && 1336601438fbSGleb Smirnoff carp_tag(sc, m) != 0) 1337891122d1SGleb Smirnoff goto resched; 1338a9771948SGleb Smirnoff 1339a9771948SGleb Smirnoff m->m_data += sizeof(*ip6); 1340c4d06976SGleb Smirnoff ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip6)); 1341a9771948SGleb Smirnoff m->m_data -= sizeof(*ip6); 1342a9771948SGleb Smirnoff 13436bf65bcfSRobert Watson CARPSTATS_INC(carps_opackets6); 1344a9771948SGleb Smirnoff 13454a2dd8d4SGleb Smirnoff carp_send_ad_error(sc, ip6_output(m, NULL, NULL, 0, 13464a2dd8d4SGleb Smirnoff &sc->sc_carpdev->if_carp->cif_im6o, NULL, NULL)); 1347a9771948SGleb Smirnoff } 1348a9771948SGleb Smirnoff #endif /* INET6 */ 1349a9771948SGleb Smirnoff 1350891122d1SGleb Smirnoff resched: 13515ee92cbdSGleb Smirnoff callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), carp_callout, sc); 135208b68b0eSGleb Smirnoff } 1353a9771948SGleb Smirnoff 135408b68b0eSGleb Smirnoff static void 135537115154SKristof Provost vrrp_send_ad_locked(struct carp_softc *sc) 135637115154SKristof Provost { 135737115154SKristof Provost struct vrrpv3_header *vh_ptr; 135837115154SKristof Provost struct ifaddr *ifa; 135937115154SKristof Provost struct mbuf *m; 136037115154SKristof Provost int len; 136137115154SKristof Provost struct vrrpv3_header vh = { 136237115154SKristof Provost .vrrp_version = CARP_VERSION_VRRPv3, 136337115154SKristof Provost .vrrp_type = VRRP_TYPE_ADVERTISEMENT, 136437115154SKristof Provost .vrrp_vrtid = sc->sc_vhid, 136537115154SKristof Provost .vrrp_priority = sc->sc_vrrp_prio, 136637115154SKristof Provost .vrrp_count_addr = 0, 136737115154SKristof Provost .vrrp_max_adver_int = htons(sc->sc_vrrp_adv_inter), 136837115154SKristof Provost .vrrp_checksum = 0, 136937115154SKristof Provost }; 137037115154SKristof Provost 137137115154SKristof Provost NET_EPOCH_ASSERT(); 137237115154SKristof Provost CARP_LOCK_ASSERT(sc); 137337115154SKristof Provost MPASS(sc->sc_version == CARP_VERSION_VRRPv3); 137437115154SKristof Provost 137537115154SKristof Provost #ifdef INET 137637115154SKristof Provost if (sc->sc_naddrs) { 137737115154SKristof Provost struct ip *ip; 137837115154SKristof Provost 137937115154SKristof Provost m = m_gethdr(M_NOWAIT, MT_DATA); 138037115154SKristof Provost if (m == NULL) { 138137115154SKristof Provost CARPSTATS_INC(carps_onomem); 138237115154SKristof Provost goto resched; 138337115154SKristof Provost } 138437115154SKristof Provost len = sizeof(*ip) + sizeof(vh); 138537115154SKristof Provost m->m_pkthdr.len = len; 138637115154SKristof Provost m->m_pkthdr.rcvif = NULL; 138737115154SKristof Provost m->m_len = len; 138837115154SKristof Provost M_ALIGN(m, m->m_len); 138937115154SKristof Provost m->m_flags |= M_MCAST; 139037115154SKristof Provost ip = mtod(m, struct ip *); 139137115154SKristof Provost ip->ip_v = IPVERSION; 139237115154SKristof Provost ip->ip_hl = sizeof(*ip) >> 2; 139337115154SKristof Provost ip->ip_tos = V_carp_dscp << IPTOS_DSCP_OFFSET; 139437115154SKristof Provost ip->ip_off = htons(IP_DF); 139537115154SKristof Provost ip->ip_ttl = CARP_DFLTTL; 139637115154SKristof Provost ip->ip_p = IPPROTO_CARP; 139737115154SKristof Provost ip->ip_sum = 0; 139837115154SKristof Provost ip_fillid(ip); 139937115154SKristof Provost 140037115154SKristof Provost ifa = carp_best_ifa(AF_INET, sc->sc_carpdev); 140137115154SKristof Provost if (ifa != NULL) { 140237115154SKristof Provost ip->ip_src.s_addr = 140337115154SKristof Provost ifatoia(ifa)->ia_addr.sin_addr.s_addr; 140437115154SKristof Provost ifa_free(ifa); 140537115154SKristof Provost } else 140637115154SKristof Provost ip->ip_src.s_addr = 0; 140737115154SKristof Provost ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP); 140837115154SKristof Provost 140937115154SKristof Provost /* Include the IP addresses in the announcement. */ 141037115154SKristof Provost for (int i = 0; i < (sc->sc_naddrs + sc->sc_naddrs6); i++) { 141137115154SKristof Provost struct sockaddr_in *in; 141237115154SKristof Provost 141337115154SKristof Provost MPASS(sc->sc_ifas[i] != NULL); 141437115154SKristof Provost if (sc->sc_ifas[i]->ifa_addr->sa_family != AF_INET) 141537115154SKristof Provost continue; 141637115154SKristof Provost 141737115154SKristof Provost in = (struct sockaddr_in *)sc->sc_ifas[i]->ifa_addr; 141837115154SKristof Provost 141937115154SKristof Provost if (m_append(m, sizeof(in->sin_addr), 142037115154SKristof Provost (caddr_t)&in->sin_addr) != 1) { 142137115154SKristof Provost m_freem(m); 142237115154SKristof Provost goto resched; 142337115154SKristof Provost } 142437115154SKristof Provost 142537115154SKristof Provost vh.vrrp_count_addr++; 142637115154SKristof Provost len += sizeof(in->sin_addr); 142737115154SKristof Provost } 142837115154SKristof Provost ip->ip_len = htons(len); 142937115154SKristof Provost 143037115154SKristof Provost vh_ptr = (struct vrrpv3_header *)mtodo(m, sizeof(*ip)); 143137115154SKristof Provost bcopy(&vh, vh_ptr, sizeof(vh)); 143237115154SKristof Provost 143337115154SKristof Provost vh_ptr->vrrp_checksum = in_pseudo(ip->ip_src.s_addr, 143437115154SKristof Provost ip->ip_dst.s_addr, 143537115154SKristof Provost htonl((uint16_t)(len - sizeof(*ip)) + ip->ip_p)); 143637115154SKristof Provost vh_ptr->vrrp_checksum = in_cksum_skip(m, len, sizeof(*ip)); 143737115154SKristof Provost 143837115154SKristof Provost if (carp_tag(sc, m)) 143937115154SKristof Provost goto resched; 144037115154SKristof Provost 144137115154SKristof Provost CARPSTATS_INC(carps_opackets); 144237115154SKristof Provost 144337115154SKristof Provost carp_send_ad_error(sc, ip_output(m, NULL, NULL, IP_RAWOUTPUT, 144437115154SKristof Provost &sc->sc_carpdev->if_carp->cif_imo, NULL)); 144537115154SKristof Provost } 144637115154SKristof Provost #endif 144737115154SKristof Provost #ifdef INET6 144837115154SKristof Provost if (sc->sc_naddrs6) { 144937115154SKristof Provost struct ip6_hdr *ip6; 145037115154SKristof Provost 145137115154SKristof Provost m = m_gethdr(M_NOWAIT, MT_DATA); 145237115154SKristof Provost if (m == NULL) { 145337115154SKristof Provost CARPSTATS_INC(carps_onomem); 145437115154SKristof Provost goto resched; 145537115154SKristof Provost } 145637115154SKristof Provost len = sizeof(*ip6) + sizeof(vh); 145737115154SKristof Provost m->m_pkthdr.len = len; 145837115154SKristof Provost m->m_pkthdr.rcvif = NULL; 145937115154SKristof Provost m->m_len = len; 146037115154SKristof Provost M_ALIGN(m, m->m_len); 146137115154SKristof Provost m->m_flags |= M_MCAST; 146237115154SKristof Provost ip6 = mtod(m, struct ip6_hdr *); 146337115154SKristof Provost bzero(ip6, sizeof(*ip6)); 146437115154SKristof Provost ip6->ip6_vfc |= IPV6_VERSION; 146537115154SKristof Provost /* Traffic class isn't defined in ip6 struct instead 146637115154SKristof Provost * it gets offset into flowid field */ 146737115154SKristof Provost ip6->ip6_flow |= htonl(V_carp_dscp << (IPV6_FLOWLABEL_LEN + 146837115154SKristof Provost IPTOS_DSCP_OFFSET)); 146937115154SKristof Provost ip6->ip6_hlim = CARP_DFLTTL; 147037115154SKristof Provost ip6->ip6_nxt = IPPROTO_CARP; 147137115154SKristof Provost 147237115154SKristof Provost /* set the source address */ 147337115154SKristof Provost ifa = carp_best_ifa(AF_INET6, sc->sc_carpdev); 147437115154SKristof Provost if (ifa != NULL) { 147537115154SKristof Provost bcopy(IFA_IN6(ifa), &ip6->ip6_src, 147637115154SKristof Provost sizeof(struct in6_addr)); 147737115154SKristof Provost ifa_free(ifa); 147837115154SKristof Provost } else 147937115154SKristof Provost /* This should never happen with IPv6. */ 148037115154SKristof Provost bzero(&ip6->ip6_src, sizeof(struct in6_addr)); 148137115154SKristof Provost 148237115154SKristof Provost /* Set the multicast destination. */ 148337115154SKristof Provost bzero(&ip6->ip6_dst, sizeof(ip6->ip6_dst)); 148437115154SKristof Provost ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL; 148537115154SKristof Provost ip6->ip6_dst.s6_addr8[15] = 0x12; 148637115154SKristof Provost 148737115154SKristof Provost /* Include the IP addresses in the announcement. */ 148837115154SKristof Provost len = sizeof(vh); 148937115154SKristof Provost for (int i = 0; i < (sc->sc_naddrs + sc->sc_naddrs6); i++) { 149037115154SKristof Provost struct sockaddr_in6 *in6; 149137115154SKristof Provost 149237115154SKristof Provost MPASS(sc->sc_ifas[i] != NULL); 149337115154SKristof Provost if (sc->sc_ifas[i]->ifa_addr->sa_family != AF_INET6) 149437115154SKristof Provost continue; 149537115154SKristof Provost 149637115154SKristof Provost in6 = (struct sockaddr_in6 *)sc->sc_ifas[i]->ifa_addr; 149737115154SKristof Provost 149837115154SKristof Provost if (m_append(m, sizeof(in6->sin6_addr), 149937115154SKristof Provost (char *)&in6->sin6_addr) != 1) { 150037115154SKristof Provost m_freem(m); 150137115154SKristof Provost goto resched; 150237115154SKristof Provost } 150337115154SKristof Provost 150437115154SKristof Provost vh.vrrp_count_addr++; 150537115154SKristof Provost len += sizeof(in6->sin6_addr); 150637115154SKristof Provost } 150737115154SKristof Provost ip6->ip6_plen = htonl(len); 150837115154SKristof Provost 150937115154SKristof Provost vh_ptr = (struct vrrpv3_header *)mtodo(m, sizeof(*ip6)); 151037115154SKristof Provost bcopy(&vh, vh_ptr, sizeof(vh)); 151137115154SKristof Provost 151237115154SKristof Provost vh_ptr->vrrp_checksum = in6_cksum_pseudo(ip6, len, ip6->ip6_nxt, 0); 151337115154SKristof Provost vh_ptr->vrrp_checksum = in_cksum_skip(m, len + sizeof(*ip6), sizeof(*ip6)); 151437115154SKristof Provost 151537115154SKristof Provost if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) { 151637115154SKristof Provost m_freem(m); 151737115154SKristof Provost CARP_DEBUG("%s: in6_setscope failed\n", __func__); 151837115154SKristof Provost goto resched; 151937115154SKristof Provost } 152037115154SKristof Provost 152137115154SKristof Provost if (carp_tag(sc, m)) 152237115154SKristof Provost goto resched; 152337115154SKristof Provost CARPSTATS_INC(carps_opackets6); 152437115154SKristof Provost 152537115154SKristof Provost carp_send_ad_error(sc, ip6_output(m, NULL, NULL, 0, 152637115154SKristof Provost &sc->sc_carpdev->if_carp->cif_im6o, NULL, NULL)); 152737115154SKristof Provost } 152837115154SKristof Provost #endif 152937115154SKristof Provost 153037115154SKristof Provost resched: 153137115154SKristof Provost callout_reset(&sc->sc_ad_tmo, sc->sc_vrrp_adv_inter * hz / 100, 15325ee92cbdSGleb Smirnoff carp_callout, sc); 153337115154SKristof Provost } 153437115154SKristof Provost 153537115154SKristof Provost static void 153608b68b0eSGleb Smirnoff carp_addroute(struct carp_softc *sc) 153708b68b0eSGleb Smirnoff { 153808b68b0eSGleb Smirnoff struct ifaddr *ifa; 153908b68b0eSGleb Smirnoff 154008b68b0eSGleb Smirnoff CARP_FOREACH_IFA(sc, ifa) 15412512b096SGleb Smirnoff carp_ifa_addroute(ifa); 15422512b096SGleb Smirnoff } 15432512b096SGleb Smirnoff 15442512b096SGleb Smirnoff static void 15452512b096SGleb Smirnoff carp_ifa_addroute(struct ifaddr *ifa) 15462512b096SGleb Smirnoff { 15472512b096SGleb Smirnoff 154808b68b0eSGleb Smirnoff switch (ifa->ifa_addr->sa_family) { 154908b68b0eSGleb Smirnoff #ifdef INET 155008b68b0eSGleb Smirnoff case AF_INET: 1551130aebbaSAlexander V. Chernikov in_addprefix(ifatoia(ifa)); 155208b68b0eSGleb Smirnoff ifa_add_loopback_route(ifa, 155308b68b0eSGleb Smirnoff (struct sockaddr *)&ifatoia(ifa)->ia_addr); 155408b68b0eSGleb Smirnoff break; 155508b68b0eSGleb Smirnoff #endif 155608b68b0eSGleb Smirnoff #ifdef INET6 155708b68b0eSGleb Smirnoff case AF_INET6: 155808b68b0eSGleb Smirnoff ifa_add_loopback_route(ifa, 155908b68b0eSGleb Smirnoff (struct sockaddr *)&ifatoia6(ifa)->ia_addr); 1560f6b84910SAlexander V. Chernikov nd6_add_ifa_lle(ifatoia6(ifa)); 156108b68b0eSGleb Smirnoff break; 156208b68b0eSGleb Smirnoff #endif 156308b68b0eSGleb Smirnoff } 156408b68b0eSGleb Smirnoff } 156508b68b0eSGleb Smirnoff 156608b68b0eSGleb Smirnoff static void 156708b68b0eSGleb Smirnoff carp_delroute(struct carp_softc *sc) 156808b68b0eSGleb Smirnoff { 156908b68b0eSGleb Smirnoff struct ifaddr *ifa; 157008b68b0eSGleb Smirnoff 157108b68b0eSGleb Smirnoff CARP_FOREACH_IFA(sc, ifa) 15722512b096SGleb Smirnoff carp_ifa_delroute(ifa); 15732512b096SGleb Smirnoff } 15742512b096SGleb Smirnoff 15752512b096SGleb Smirnoff static void 15762512b096SGleb Smirnoff carp_ifa_delroute(struct ifaddr *ifa) 15772512b096SGleb Smirnoff { 15782512b096SGleb Smirnoff 157908b68b0eSGleb Smirnoff switch (ifa->ifa_addr->sa_family) { 158008b68b0eSGleb Smirnoff #ifdef INET 158108b68b0eSGleb Smirnoff case AF_INET: 158208b68b0eSGleb Smirnoff ifa_del_loopback_route(ifa, 158308b68b0eSGleb Smirnoff (struct sockaddr *)&ifatoia(ifa)->ia_addr); 158408b68b0eSGleb Smirnoff in_scrubprefix(ifatoia(ifa), LLE_STATIC); 158508b68b0eSGleb Smirnoff break; 158608b68b0eSGleb Smirnoff #endif 158708b68b0eSGleb Smirnoff #ifdef INET6 158808b68b0eSGleb Smirnoff case AF_INET6: 158908b68b0eSGleb Smirnoff ifa_del_loopback_route(ifa, 159008b68b0eSGleb Smirnoff (struct sockaddr *)&ifatoia6(ifa)->ia_addr); 15913e7a2321SAlexander V. Chernikov nd6_rem_ifa_lle(ifatoia6(ifa), 1); 159208b68b0eSGleb Smirnoff break; 159308b68b0eSGleb Smirnoff #endif 159408b68b0eSGleb Smirnoff } 1595a9771948SGleb Smirnoff } 1596a9771948SGleb Smirnoff 159724421c1cSGleb Smirnoff int 159824421c1cSGleb Smirnoff carp_master(struct ifaddr *ifa) 159924421c1cSGleb Smirnoff { 160024421c1cSGleb Smirnoff struct carp_softc *sc = ifa->ifa_carp; 160124421c1cSGleb Smirnoff 160224421c1cSGleb Smirnoff return (sc->sc_state == MASTER); 160324421c1cSGleb Smirnoff } 160424421c1cSGleb Smirnoff 1605a0ae8f04SBjoern A. Zeeb #ifdef INET 1606a9771948SGleb Smirnoff /* 1607a9771948SGleb Smirnoff * Broadcast a gratuitous ARP request containing 1608a9771948SGleb Smirnoff * the virtual router MAC address for each IP address 1609a9771948SGleb Smirnoff * associated with the virtual router. 1610a9771948SGleb Smirnoff */ 16115c1f0f6dSGleb Smirnoff static void 1612a9771948SGleb Smirnoff carp_send_arp(struct carp_softc *sc) 1613a9771948SGleb Smirnoff { 1614a9771948SGleb Smirnoff struct ifaddr *ifa; 1615d6e82913SSteven Hartland struct in_addr addr; 1616a9771948SGleb Smirnoff 16171d126e9bSKristof Provost NET_EPOCH_ASSERT(); 16181d126e9bSKristof Provost 16191c302b58SAlexander V. Chernikov CARP_FOREACH_IFA(sc, ifa) { 16201c302b58SAlexander V. Chernikov if (ifa->ifa_addr->sa_family != AF_INET) 16211c302b58SAlexander V. Chernikov continue; 1622d6e82913SSteven Hartland addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr; 1623a254d687SGleb Smirnoff arp_announce_ifaddr(sc->sc_carpdev, addr, sc->sc_addr); 16241c302b58SAlexander V. Chernikov } 1625a9771948SGleb Smirnoff } 162608b68b0eSGleb Smirnoff 162708b68b0eSGleb Smirnoff int 162808b68b0eSGleb Smirnoff carp_iamatch(struct ifaddr *ifa, uint8_t **enaddr) 162908b68b0eSGleb Smirnoff { 163008b68b0eSGleb Smirnoff struct carp_softc *sc = ifa->ifa_carp; 163108b68b0eSGleb Smirnoff 163208b68b0eSGleb Smirnoff if (sc->sc_state == MASTER) { 1633a254d687SGleb Smirnoff *enaddr = sc->sc_addr; 163408b68b0eSGleb Smirnoff return (1); 163508b68b0eSGleb Smirnoff } 163608b68b0eSGleb Smirnoff 163708b68b0eSGleb Smirnoff return (0); 1638a9771948SGleb Smirnoff } 1639a0ae8f04SBjoern A. Zeeb #endif 1640a9771948SGleb Smirnoff 1641a9771948SGleb Smirnoff #ifdef INET6 16425c1f0f6dSGleb Smirnoff static void 1643a9771948SGleb Smirnoff carp_send_na(struct carp_softc *sc) 1644a9771948SGleb Smirnoff { 1645d6e82913SSteven Hartland static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT; 1646a9771948SGleb Smirnoff struct ifaddr *ifa; 1647d6e82913SSteven Hartland struct in6_addr *in6; 1648a9771948SGleb Smirnoff 164908b68b0eSGleb Smirnoff CARP_FOREACH_IFA(sc, ifa) { 1650d6e82913SSteven Hartland if (ifa->ifa_addr->sa_family != AF_INET6) 1651a9771948SGleb Smirnoff continue; 1652a9771948SGleb Smirnoff 1653d6e82913SSteven Hartland in6 = IFA_IN6(ifa); 1654d6e82913SSteven Hartland nd6_na_output(sc->sc_carpdev, &mcast, in6, 1655d6e82913SSteven Hartland ND_NA_FLAG_OVERRIDE, 1, NULL); 1656d6e82913SSteven Hartland DELAY(1000); /* XXX */ 1657a9771948SGleb Smirnoff } 1658a9771948SGleb Smirnoff } 1659a9771948SGleb Smirnoff 16608253dcabSBjoern A. Zeeb /* 16618253dcabSBjoern A. Zeeb * Returns ifa in case it's a carp address and it is MASTER, or if the address 16628253dcabSBjoern A. Zeeb * matches and is not a carp address. Returns NULL otherwise. 16638253dcabSBjoern A. Zeeb */ 1664a4e53905SMax Laier struct ifaddr * 166554bfbd51SWill Andrews carp_iamatch6(struct ifnet *ifp, struct in6_addr *taddr) 1666a9771948SGleb Smirnoff { 1667a9771948SGleb Smirnoff struct ifaddr *ifa; 1668a9771948SGleb Smirnoff 1669b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 1670b8a6e03fSGleb Smirnoff 16718253dcabSBjoern A. Zeeb ifa = NULL; 1672d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 16738253dcabSBjoern A. Zeeb if (ifa->ifa_addr->sa_family != AF_INET6) 16748253dcabSBjoern A. Zeeb continue; 16758253dcabSBjoern A. Zeeb if (!IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) 16768253dcabSBjoern A. Zeeb continue; 16778253dcabSBjoern A. Zeeb if (ifa->ifa_carp && ifa->ifa_carp->sc_state != MASTER) 16788253dcabSBjoern A. Zeeb ifa = NULL; 16798253dcabSBjoern A. Zeeb else 16808c0fec80SRobert Watson ifa_ref(ifa); 16818253dcabSBjoern A. Zeeb break; 1682a9771948SGleb Smirnoff } 1683a9771948SGleb Smirnoff 16848253dcabSBjoern A. Zeeb return (ifa); 1685a9771948SGleb Smirnoff } 1686a9771948SGleb Smirnoff 1687dad68fc3SBjoern A. Zeeb char * 168854bfbd51SWill Andrews carp_macmatch6(struct ifnet *ifp, struct mbuf *m, const struct in6_addr *taddr) 1689a9771948SGleb Smirnoff { 1690a9771948SGleb Smirnoff struct ifaddr *ifa; 1691a9771948SGleb Smirnoff 1692b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 1693b8a6e03fSGleb Smirnoff 169408b68b0eSGleb Smirnoff IFNET_FOREACH_IFA(ifp, ifa) 169508b68b0eSGleb Smirnoff if (ifa->ifa_addr->sa_family == AF_INET6 && 169608b68b0eSGleb Smirnoff IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) { 169708b68b0eSGleb Smirnoff struct carp_softc *sc = ifa->ifa_carp; 169808b68b0eSGleb Smirnoff struct m_tag *mtag; 169908b68b0eSGleb Smirnoff 1700a9771948SGleb Smirnoff mtag = m_tag_get(PACKET_TAG_CARP, 1701a856ddc6SGleb Smirnoff sizeof(struct carp_softc *), M_NOWAIT); 170208b68b0eSGleb Smirnoff if (mtag == NULL) 170308b68b0eSGleb Smirnoff /* Better a bit than nothing. */ 1704a254d687SGleb Smirnoff return (sc->sc_addr); 170508b68b0eSGleb Smirnoff 1706eaf151c4SGleb Smirnoff bcopy(&sc, mtag + 1, sizeof(sc)); 1707a9771948SGleb Smirnoff m_tag_prepend(m, mtag); 1708a9771948SGleb Smirnoff 1709a254d687SGleb Smirnoff return (sc->sc_addr); 1710a9771948SGleb Smirnoff } 1711a9771948SGleb Smirnoff 1712a9771948SGleb Smirnoff return (NULL); 1713a9771948SGleb Smirnoff } 171408b68b0eSGleb Smirnoff #endif /* INET6 */ 1715a9771948SGleb Smirnoff 171608b68b0eSGleb Smirnoff int 171754bfbd51SWill Andrews carp_forus(struct ifnet *ifp, u_char *dhost) 1718a9771948SGleb Smirnoff { 171908b68b0eSGleb Smirnoff struct carp_softc *sc; 172008b68b0eSGleb Smirnoff uint8_t *ena = dhost; 1721a9771948SGleb Smirnoff 1722a9771948SGleb Smirnoff if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1) 172308b68b0eSGleb Smirnoff return (0); 1724a9771948SGleb Smirnoff 172508b68b0eSGleb Smirnoff CIF_LOCK(ifp->if_carp); 172608b68b0eSGleb Smirnoff IFNET_FOREACH_CARP(ifp, sc) { 17278c3fbf3cSAlexander Motin /* 17288c3fbf3cSAlexander Motin * CARP_LOCK() is not here, since would protect nothing, but 17298c3fbf3cSAlexander Motin * cause deadlock with if_bridge, calling this under its lock. 17308c3fbf3cSAlexander Motin */ 1731a254d687SGleb Smirnoff if (sc->sc_state == MASTER && !bcmp(dhost, sc->sc_addr, 173208b68b0eSGleb Smirnoff ETHER_ADDR_LEN)) { 173308b68b0eSGleb Smirnoff CIF_UNLOCK(ifp->if_carp); 173408b68b0eSGleb Smirnoff return (1); 1735a9771948SGleb Smirnoff } 173608b68b0eSGleb Smirnoff } 173708b68b0eSGleb Smirnoff CIF_UNLOCK(ifp->if_carp); 1738a9771948SGleb Smirnoff 173908b68b0eSGleb Smirnoff return (0); 1740a9771948SGleb Smirnoff } 1741a9771948SGleb Smirnoff 1742afdbac98SGleb Smirnoff /* Master down timeout event, executed in callout context. */ 17435c1f0f6dSGleb Smirnoff static void 1744a9771948SGleb Smirnoff carp_master_down(void *v) 1745a9771948SGleb Smirnoff { 1746a9771948SGleb Smirnoff struct carp_softc *sc = v; 17471d126e9bSKristof Provost struct epoch_tracker et; 1748a9771948SGleb Smirnoff 17491d126e9bSKristof Provost NET_EPOCH_ENTER(et); 175008b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); 175108b68b0eSGleb Smirnoff 1752afdbac98SGleb Smirnoff CURVNET_SET(sc->sc_carpdev->if_vnet); 175308b68b0eSGleb Smirnoff if (sc->sc_state == BACKUP) { 1754d01641e2SWill Andrews carp_master_down_locked(sc, "master timed out"); 175508b68b0eSGleb Smirnoff } 1756afdbac98SGleb Smirnoff CURVNET_RESTORE(); 175708b68b0eSGleb Smirnoff 175808b68b0eSGleb Smirnoff CARP_UNLOCK(sc); 17591d126e9bSKristof Provost NET_EPOCH_EXIT(et); 1760d220759bSGleb Smirnoff } 1761d220759bSGleb Smirnoff 1762d220759bSGleb Smirnoff static void 1763d01641e2SWill Andrews carp_master_down_locked(struct carp_softc *sc, const char *reason) 1764d220759bSGleb Smirnoff { 176508b68b0eSGleb Smirnoff 17661d126e9bSKristof Provost NET_EPOCH_ASSERT(); 176708b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); 1768d220759bSGleb Smirnoff 1769a9771948SGleb Smirnoff switch (sc->sc_state) { 1770a9771948SGleb Smirnoff case BACKUP: 1771d01641e2SWill Andrews carp_set_state(sc, MASTER, reason); 17725ee92cbdSGleb Smirnoff send_ad_locked(sc); 1773a0ae8f04SBjoern A. Zeeb #ifdef INET 1774a9771948SGleb Smirnoff carp_send_arp(sc); 1775a0ae8f04SBjoern A. Zeeb #endif 1776a9771948SGleb Smirnoff #ifdef INET6 1777a9771948SGleb Smirnoff carp_send_na(sc); 177808b68b0eSGleb Smirnoff #endif 1779a9771948SGleb Smirnoff carp_setrun(sc, 0); 178008b68b0eSGleb Smirnoff carp_addroute(sc); 178108b68b0eSGleb Smirnoff break; 178208b68b0eSGleb Smirnoff case INIT: 178308b68b0eSGleb Smirnoff case MASTER: 178408b68b0eSGleb Smirnoff #ifdef INVARIANTS 178508b68b0eSGleb Smirnoff panic("carp: VHID %u@%s: master_down event in %s state\n", 178608b68b0eSGleb Smirnoff sc->sc_vhid, 1787511a6d5eSKristof Provost if_name(sc->sc_carpdev), 178808b68b0eSGleb Smirnoff sc->sc_state ? "MASTER" : "INIT"); 178908b68b0eSGleb Smirnoff #endif 1790a9771948SGleb Smirnoff break; 1791a9771948SGleb Smirnoff } 1792a9771948SGleb Smirnoff } 1793a9771948SGleb Smirnoff 1794a9771948SGleb Smirnoff /* 1795a9771948SGleb Smirnoff * When in backup state, af indicates whether to reset the master down timer 1796a9771948SGleb Smirnoff * for v4 or v6. If it's set to zero, reset the ones which are already pending. 1797a9771948SGleb Smirnoff */ 17985c1f0f6dSGleb Smirnoff static void 1799a9771948SGleb Smirnoff carp_setrun(struct carp_softc *sc, sa_family_t af) 1800a9771948SGleb Smirnoff { 1801a9771948SGleb Smirnoff struct timeval tv; 180237115154SKristof Provost int timeout; 1803a9771948SGleb Smirnoff 180408b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); 1805d220759bSGleb Smirnoff 180608b68b0eSGleb Smirnoff if ((sc->sc_carpdev->if_flags & IFF_UP) == 0 || 180708b68b0eSGleb Smirnoff sc->sc_carpdev->if_link_state != LINK_STATE_UP || 1808167a3440SAlexander Motin (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) || 1809167a3440SAlexander Motin !V_carp_allow) 1810a9771948SGleb Smirnoff return; 1811a9771948SGleb Smirnoff 1812a9771948SGleb Smirnoff switch (sc->sc_state) { 1813a9771948SGleb Smirnoff case INIT: 1814d01641e2SWill Andrews carp_set_state(sc, BACKUP, "initialization complete"); 1815a9771948SGleb Smirnoff carp_setrun(sc, 0); 1816a9771948SGleb Smirnoff break; 1817a9771948SGleb Smirnoff case BACKUP: 1818a9771948SGleb Smirnoff callout_stop(&sc->sc_ad_tmo); 181937115154SKristof Provost 1820a254d687SGleb Smirnoff switch (sc->sc_version) { 1821a254d687SGleb Smirnoff case CARP_VERSION_CARP: 1822a9771948SGleb Smirnoff tv.tv_sec = 3 * sc->sc_advbase; 1823a9771948SGleb Smirnoff tv.tv_usec = sc->sc_advskew * 1000000 / 256; 182437115154SKristof Provost timeout = tvtohz(&tv); 1825a254d687SGleb Smirnoff break; 1826a254d687SGleb Smirnoff case CARP_VERSION_VRRPv3: 182737115154SKristof Provost /* skew time */ 1828a254d687SGleb Smirnoff timeout = (256 - sc->sc_vrrp_prio) * 1829a254d687SGleb Smirnoff sc->sc_vrrp_master_inter / 256; 183037115154SKristof Provost timeout += (3 * sc->sc_vrrp_master_inter); 183137115154SKristof Provost timeout *= hz; 1832a254d687SGleb Smirnoff timeout /= 100; /* master interval is in centiseconds */ 1833a254d687SGleb Smirnoff break; 183437115154SKristof Provost } 1835a9771948SGleb Smirnoff switch (af) { 1836a9771948SGleb Smirnoff #ifdef INET 1837a9771948SGleb Smirnoff case AF_INET: 183837115154SKristof Provost callout_reset(&sc->sc_md_tmo, timeout, 1839a9771948SGleb Smirnoff carp_master_down, sc); 1840a9771948SGleb Smirnoff break; 184108b68b0eSGleb Smirnoff #endif 1842a9771948SGleb Smirnoff #ifdef INET6 1843a9771948SGleb Smirnoff case AF_INET6: 184437115154SKristof Provost callout_reset(&sc->sc_md6_tmo, timeout, 1845a9771948SGleb Smirnoff carp_master_down, sc); 1846a9771948SGleb Smirnoff break; 184708b68b0eSGleb Smirnoff #endif 1848a9771948SGleb Smirnoff default: 184908b68b0eSGleb Smirnoff #ifdef INET 1850a9771948SGleb Smirnoff if (sc->sc_naddrs) 185137115154SKristof Provost callout_reset(&sc->sc_md_tmo, timeout, 1852a9771948SGleb Smirnoff carp_master_down, sc); 185308b68b0eSGleb Smirnoff #endif 185408b68b0eSGleb Smirnoff #ifdef INET6 1855a9771948SGleb Smirnoff if (sc->sc_naddrs6) 185637115154SKristof Provost callout_reset(&sc->sc_md6_tmo, timeout, 1857a9771948SGleb Smirnoff carp_master_down, sc); 185808b68b0eSGleb Smirnoff #endif 1859a9771948SGleb Smirnoff break; 1860a9771948SGleb Smirnoff } 1861a9771948SGleb Smirnoff break; 1862a9771948SGleb Smirnoff case MASTER: 1863a254d687SGleb Smirnoff switch (sc->sc_version) { 1864a254d687SGleb Smirnoff case CARP_VERSION_CARP: 1865a9771948SGleb Smirnoff tv.tv_sec = sc->sc_advbase; 1866a9771948SGleb Smirnoff tv.tv_usec = sc->sc_advskew * 1000000 / 256; 1867a9771948SGleb Smirnoff callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), 18685ee92cbdSGleb Smirnoff carp_callout, sc); 1869a254d687SGleb Smirnoff break; 1870a254d687SGleb Smirnoff case CARP_VERSION_VRRPv3: 187137115154SKristof Provost callout_reset(&sc->sc_ad_tmo, 187237115154SKristof Provost sc->sc_vrrp_adv_inter * hz / 100, 18735ee92cbdSGleb Smirnoff carp_callout, sc); 1874a254d687SGleb Smirnoff break; 187537115154SKristof Provost } 1876a9771948SGleb Smirnoff break; 1877a9771948SGleb Smirnoff } 1878a9771948SGleb Smirnoff } 1879a9771948SGleb Smirnoff 188008b68b0eSGleb Smirnoff /* 188108b68b0eSGleb Smirnoff * Setup multicast structures. 188208b68b0eSGleb Smirnoff */ 18835c1f0f6dSGleb Smirnoff static int 1884a9a2c40cSGleb Smirnoff carp_multicast_setup(struct carp_if *cif, sa_family_t sa) 1885a9771948SGleb Smirnoff { 1886a9a2c40cSGleb Smirnoff struct ifnet *ifp = cif->cif_ifp; 188708b68b0eSGleb Smirnoff int error = 0; 188808b68b0eSGleb Smirnoff 188908b68b0eSGleb Smirnoff switch (sa) { 189008b68b0eSGleb Smirnoff #ifdef INET 189108b68b0eSGleb Smirnoff case AF_INET: 189208b68b0eSGleb Smirnoff { 189308b68b0eSGleb Smirnoff struct ip_moptions *imo = &cif->cif_imo; 189459854ecfSHans Petter Selasky struct in_mfilter *imf; 1895a9771948SGleb Smirnoff struct in_addr addr; 1896a9771948SGleb Smirnoff 189759854ecfSHans Petter Selasky if (ip_mfilter_first(&imo->imo_head) != NULL) 1898a9771948SGleb Smirnoff return (0); 1899a9771948SGleb Smirnoff 190059854ecfSHans Petter Selasky imf = ip_mfilter_alloc(M_WAITOK, 0, 0); 190159854ecfSHans Petter Selasky ip_mfilter_init(&imo->imo_head); 190208b68b0eSGleb Smirnoff imo->imo_multicast_vif = -1; 1903a9771948SGleb Smirnoff 1904a9771948SGleb Smirnoff addr.s_addr = htonl(INADDR_CARP_GROUP); 190508b68b0eSGleb Smirnoff if ((error = in_joingroup(ifp, &addr, NULL, 190659854ecfSHans Petter Selasky &imf->imf_inm)) != 0) { 190759854ecfSHans Petter Selasky ip_mfilter_free(imf); 190808b68b0eSGleb Smirnoff break; 19092d9cfabaSRobert Watson } 191059854ecfSHans Petter Selasky 191159854ecfSHans Petter Selasky ip_mfilter_insert(&imo->imo_head, imf); 1912a9771948SGleb Smirnoff imo->imo_multicast_ifp = ifp; 1913a9771948SGleb Smirnoff imo->imo_multicast_ttl = CARP_DFLTTL; 1914a9771948SGleb Smirnoff imo->imo_multicast_loop = 0; 1915a9771948SGleb Smirnoff break; 1916a9771948SGleb Smirnoff } 191708b68b0eSGleb Smirnoff #endif 191808b68b0eSGleb Smirnoff #ifdef INET6 191908b68b0eSGleb Smirnoff case AF_INET6: 192008b68b0eSGleb Smirnoff { 192108b68b0eSGleb Smirnoff struct ip6_moptions *im6o = &cif->cif_im6o; 192259854ecfSHans Petter Selasky struct in6_mfilter *im6f[2]; 192308b68b0eSGleb Smirnoff struct in6_addr in6; 192433cde130SBruce M Simpson 192559854ecfSHans Petter Selasky if (ip6_mfilter_first(&im6o->im6o_head)) 192608b68b0eSGleb Smirnoff return (0); 192708b68b0eSGleb Smirnoff 192859854ecfSHans Petter Selasky im6f[0] = ip6_mfilter_alloc(M_WAITOK, 0, 0); 192959854ecfSHans Petter Selasky im6f[1] = ip6_mfilter_alloc(M_WAITOK, 0, 0); 193059854ecfSHans Petter Selasky 193159854ecfSHans Petter Selasky ip6_mfilter_init(&im6o->im6o_head); 193208b68b0eSGleb Smirnoff im6o->im6o_multicast_hlim = CARP_DFLTTL; 1933a9771948SGleb Smirnoff im6o->im6o_multicast_ifp = ifp; 1934a9771948SGleb Smirnoff 193508b68b0eSGleb Smirnoff /* Join IPv6 CARP multicast group. */ 1936a1f7e5f8SHajimu UMEMOTO bzero(&in6, sizeof(in6)); 1937a1f7e5f8SHajimu UMEMOTO in6.s6_addr16[0] = htons(0xff02); 1938a1f7e5f8SHajimu UMEMOTO in6.s6_addr8[15] = 0x12; 193908b68b0eSGleb Smirnoff if ((error = in6_setscope(&in6, ifp, NULL)) != 0) { 194059854ecfSHans Petter Selasky ip6_mfilter_free(im6f[0]); 194159854ecfSHans Petter Selasky ip6_mfilter_free(im6f[1]); 194208b68b0eSGleb Smirnoff break; 194308b68b0eSGleb Smirnoff } 194459854ecfSHans Petter Selasky if ((error = in6_joingroup(ifp, &in6, NULL, &im6f[0]->im6f_in6m, 0)) != 0) { 194559854ecfSHans Petter Selasky ip6_mfilter_free(im6f[0]); 194659854ecfSHans Petter Selasky ip6_mfilter_free(im6f[1]); 194708b68b0eSGleb Smirnoff break; 194808b68b0eSGleb Smirnoff } 1949a9771948SGleb Smirnoff 195008b68b0eSGleb Smirnoff /* Join solicited multicast address. */ 1951a1f7e5f8SHajimu UMEMOTO bzero(&in6, sizeof(in6)); 1952a1f7e5f8SHajimu UMEMOTO in6.s6_addr16[0] = htons(0xff02); 1953a1f7e5f8SHajimu UMEMOTO in6.s6_addr32[1] = 0; 1954a1f7e5f8SHajimu UMEMOTO in6.s6_addr32[2] = htonl(1); 195508b68b0eSGleb Smirnoff in6.s6_addr32[3] = 0; 1956a1f7e5f8SHajimu UMEMOTO in6.s6_addr8[12] = 0xff; 195759854ecfSHans Petter Selasky 195808b68b0eSGleb Smirnoff if ((error = in6_setscope(&in6, ifp, NULL)) != 0) { 195959854ecfSHans Petter Selasky ip6_mfilter_free(im6f[0]); 196059854ecfSHans Petter Selasky ip6_mfilter_free(im6f[1]); 196108b68b0eSGleb Smirnoff break; 196208b68b0eSGleb Smirnoff } 196359854ecfSHans Petter Selasky 196459854ecfSHans Petter Selasky if ((error = in6_joingroup(ifp, &in6, NULL, &im6f[1]->im6f_in6m, 0)) != 0) { 196559854ecfSHans Petter Selasky in6_leavegroup(im6f[0]->im6f_in6m, NULL); 196659854ecfSHans Petter Selasky ip6_mfilter_free(im6f[0]); 196759854ecfSHans Petter Selasky ip6_mfilter_free(im6f[1]); 196808b68b0eSGleb Smirnoff break; 196908b68b0eSGleb Smirnoff } 197059854ecfSHans Petter Selasky ip6_mfilter_insert(&im6o->im6o_head, im6f[0]); 197159854ecfSHans Petter Selasky ip6_mfilter_insert(&im6o->im6o_head, im6f[1]); 1972a9771948SGleb Smirnoff break; 1973a9771948SGleb Smirnoff } 1974a9771948SGleb Smirnoff #endif 197508b68b0eSGleb Smirnoff } 197608b68b0eSGleb Smirnoff 197708b68b0eSGleb Smirnoff return (error); 1978a9771948SGleb Smirnoff } 1979a9771948SGleb Smirnoff 1980a9771948SGleb Smirnoff /* 198108b68b0eSGleb Smirnoff * Free multicast structures. 1982a9771948SGleb Smirnoff */ 19835c1f0f6dSGleb Smirnoff static void 1984a9a2c40cSGleb Smirnoff carp_multicast_cleanup(struct carp_if *cif, sa_family_t sa) 1985a9771948SGleb Smirnoff { 198659854ecfSHans Petter Selasky #ifdef INET 198759854ecfSHans Petter Selasky struct ip_moptions *imo = &cif->cif_imo; 198859854ecfSHans Petter Selasky struct in_mfilter *imf; 198959854ecfSHans Petter Selasky #endif 199059854ecfSHans Petter Selasky #ifdef INET6 199159854ecfSHans Petter Selasky struct ip6_moptions *im6o = &cif->cif_im6o; 199259854ecfSHans Petter Selasky struct in6_mfilter *im6f; 199359854ecfSHans Petter Selasky #endif 19949c2cd1aaSGleb Smirnoff sx_assert(&carp_sx, SA_XLOCKED); 19959c2cd1aaSGleb Smirnoff 199608b68b0eSGleb Smirnoff switch (sa) { 199708b68b0eSGleb Smirnoff #ifdef INET 199808b68b0eSGleb Smirnoff case AF_INET: 199959854ecfSHans Petter Selasky if (cif->cif_naddrs != 0) 200059854ecfSHans Petter Selasky break; 200108b68b0eSGleb Smirnoff 200259854ecfSHans Petter Selasky while ((imf = ip_mfilter_first(&imo->imo_head)) != NULL) { 200359854ecfSHans Petter Selasky ip_mfilter_remove(&imo->imo_head, imf); 200459854ecfSHans Petter Selasky in_leavegroup(imf->imf_inm, NULL); 200559854ecfSHans Petter Selasky ip_mfilter_free(imf); 200608b68b0eSGleb Smirnoff } 200708b68b0eSGleb Smirnoff break; 2008a9771948SGleb Smirnoff #endif 200908b68b0eSGleb Smirnoff #ifdef INET6 201008b68b0eSGleb Smirnoff case AF_INET6: 201159854ecfSHans Petter Selasky if (cif->cif_naddrs6 != 0) 201259854ecfSHans Petter Selasky break; 201308b68b0eSGleb Smirnoff 201459854ecfSHans Petter Selasky while ((im6f = ip6_mfilter_first(&im6o->im6o_head)) != NULL) { 201559854ecfSHans Petter Selasky ip6_mfilter_remove(&im6o->im6o_head, im6f); 201659854ecfSHans Petter Selasky in6_leavegroup(im6f->im6f_in6m, NULL); 201759854ecfSHans Petter Selasky ip6_mfilter_free(im6f); 201808b68b0eSGleb Smirnoff } 201908b68b0eSGleb Smirnoff break; 202008b68b0eSGleb Smirnoff #endif 202108b68b0eSGleb Smirnoff } 2022a9771948SGleb Smirnoff } 2023a9771948SGleb Smirnoff 2024a9771948SGleb Smirnoff int 202547e8d432SGleb Smirnoff carp_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa) 2026a9771948SGleb Smirnoff { 2027a9771948SGleb Smirnoff struct m_tag *mtag; 2028601438fbSGleb Smirnoff int vhid; 2029a9771948SGleb Smirnoff 2030a9771948SGleb Smirnoff if (!sa) 2031a9771948SGleb Smirnoff return (0); 2032a9771948SGleb Smirnoff 2033a9771948SGleb Smirnoff switch (sa->sa_family) { 2034a9771948SGleb Smirnoff #ifdef INET 2035a9771948SGleb Smirnoff case AF_INET: 2036a9771948SGleb Smirnoff break; 203708b68b0eSGleb Smirnoff #endif 2038a9771948SGleb Smirnoff #ifdef INET6 2039a9771948SGleb Smirnoff case AF_INET6: 2040a9771948SGleb Smirnoff break; 204108b68b0eSGleb Smirnoff #endif 2042a9771948SGleb Smirnoff default: 2043a9771948SGleb Smirnoff return (0); 2044a9771948SGleb Smirnoff } 2045a9771948SGleb Smirnoff 2046a9771948SGleb Smirnoff mtag = m_tag_find(m, PACKET_TAG_CARP, NULL); 2047a9771948SGleb Smirnoff if (mtag == NULL) 2048a9771948SGleb Smirnoff return (0); 2049a9771948SGleb Smirnoff 2050601438fbSGleb Smirnoff bcopy(mtag + 1, &vhid, sizeof(vhid)); 205113781800SKristof Provost 205208b68b0eSGleb Smirnoff /* Set the source MAC address to the Virtual Router MAC Address. */ 2053a9771948SGleb Smirnoff switch (ifp->if_type) { 2054630481bbSYaroslav Tykhiy case IFT_ETHER: 20559ca1fe0dSGleb Smirnoff case IFT_BRIDGE: 2056630481bbSYaroslav Tykhiy case IFT_L2VLAN: { 2057a9771948SGleb Smirnoff struct ether_header *eh; 2058a9771948SGleb Smirnoff 2059a9771948SGleb Smirnoff eh = mtod(m, struct ether_header *); 2060a9771948SGleb Smirnoff eh->ether_shost[0] = 0; 2061a9771948SGleb Smirnoff eh->ether_shost[1] = 0; 2062a9771948SGleb Smirnoff eh->ether_shost[2] = 0x5e; 2063a9771948SGleb Smirnoff eh->ether_shost[3] = 0; 2064a9771948SGleb Smirnoff eh->ether_shost[4] = 1; 2065601438fbSGleb Smirnoff eh->ether_shost[5] = vhid; 2066a9771948SGleb Smirnoff } 2067a9771948SGleb Smirnoff break; 2068a9771948SGleb Smirnoff default: 206908b68b0eSGleb Smirnoff printf("%s: carp is not supported for the %d interface type\n", 2070511a6d5eSKristof Provost if_name(ifp), ifp->if_type); 2071a9771948SGleb Smirnoff return (EOPNOTSUPP); 2072a9771948SGleb Smirnoff } 2073a9771948SGleb Smirnoff 2074a9771948SGleb Smirnoff return (0); 2075a9771948SGleb Smirnoff } 2076a9771948SGleb Smirnoff 207708b68b0eSGleb Smirnoff static struct carp_softc* 2078a254d687SGleb Smirnoff carp_alloc(struct ifnet *ifp, carp_version_t version, int vhid) 2079a9771948SGleb Smirnoff { 208008b68b0eSGleb Smirnoff struct carp_softc *sc; 208108b68b0eSGleb Smirnoff struct carp_if *cif; 2082d220759bSGleb Smirnoff 208381098a01SAlexander Motin sx_assert(&carp_sx, SA_XLOCKED); 208481098a01SAlexander Motin 20850cc726f2SGleb Smirnoff if ((cif = ifp->if_carp) == NULL) 208608b68b0eSGleb Smirnoff cif = carp_alloc_if(ifp); 2087a9771948SGleb Smirnoff 2088a254d687SGleb Smirnoff sc = malloc(sizeof(*sc), M_CARP, M_WAITOK); 2089a254d687SGleb Smirnoff *sc = (struct carp_softc ){ 2090a254d687SGleb Smirnoff .sc_vhid = vhid, 2091a254d687SGleb Smirnoff .sc_version = version, 2092a254d687SGleb Smirnoff .sc_state = INIT, 2093a254d687SGleb Smirnoff .sc_carpdev = ifp, 2094a254d687SGleb Smirnoff .sc_ifasiz = sizeof(struct ifaddr *), 2095a254d687SGleb Smirnoff .sc_addr = { 0, 0, 0x5e, 0, 1, vhid }, 2096a254d687SGleb Smirnoff }; 209708b68b0eSGleb Smirnoff sc->sc_ifas = malloc(sc->sc_ifasiz, M_CARP, M_WAITOK|M_ZERO); 209808b68b0eSGleb Smirnoff 2099a254d687SGleb Smirnoff switch (version) { 2100a254d687SGleb Smirnoff case CARP_VERSION_CARP: 2101a254d687SGleb Smirnoff sc->sc_advbase = CARP_DFLTINTV; 2102a254d687SGleb Smirnoff sc->sc_init_counter = true; 210313781800SKristof Provost sc->sc_carpaddr.s_addr = htonl(INADDR_CARP_GROUP); 210413781800SKristof Provost sc->sc_carpaddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL; 210513781800SKristof Provost sc->sc_carpaddr6.s6_addr8[15] = 0x12; 2106a254d687SGleb Smirnoff break; 2107a254d687SGleb Smirnoff case CARP_VERSION_VRRPv3: 210837115154SKristof Provost sc->sc_vrrp_adv_inter = 100; 210937115154SKristof Provost sc->sc_vrrp_master_inter = sc->sc_vrrp_adv_inter; 211037115154SKristof Provost sc->sc_vrrp_prio = 100; 2111a254d687SGleb Smirnoff break; 2112a254d687SGleb Smirnoff } 211337115154SKristof Provost 211408b68b0eSGleb Smirnoff CARP_LOCK_INIT(sc); 211508b68b0eSGleb Smirnoff #ifdef INET 211608b68b0eSGleb Smirnoff callout_init_mtx(&sc->sc_md_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED); 211708b68b0eSGleb Smirnoff #endif 211808b68b0eSGleb Smirnoff #ifdef INET6 211908b68b0eSGleb Smirnoff callout_init_mtx(&sc->sc_md6_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED); 212008b68b0eSGleb Smirnoff #endif 212108b68b0eSGleb Smirnoff callout_init_mtx(&sc->sc_ad_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED); 212208b68b0eSGleb Smirnoff 212308b68b0eSGleb Smirnoff CIF_LOCK(cif); 212408b68b0eSGleb Smirnoff TAILQ_INSERT_TAIL(&cif->cif_vrs, sc, sc_list); 212508b68b0eSGleb Smirnoff CIF_UNLOCK(cif); 212608b68b0eSGleb Smirnoff 212708b68b0eSGleb Smirnoff mtx_lock(&carp_mtx); 212808b68b0eSGleb Smirnoff LIST_INSERT_HEAD(&carp_list, sc, sc_next); 212908b68b0eSGleb Smirnoff mtx_unlock(&carp_mtx); 213008b68b0eSGleb Smirnoff 213108b68b0eSGleb Smirnoff return (sc); 213208b68b0eSGleb Smirnoff } 213308b68b0eSGleb Smirnoff 21349c2cd1aaSGleb Smirnoff static void 213508b68b0eSGleb Smirnoff carp_grow_ifas(struct carp_softc *sc) 213608b68b0eSGleb Smirnoff { 213708b68b0eSGleb Smirnoff struct ifaddr **new; 213808b68b0eSGleb Smirnoff 21399c2cd1aaSGleb Smirnoff new = malloc(sc->sc_ifasiz * 2, M_CARP, M_WAITOK | M_ZERO); 21409c2cd1aaSGleb Smirnoff CARP_LOCK(sc); 214108b68b0eSGleb Smirnoff bcopy(sc->sc_ifas, new, sc->sc_ifasiz); 214208b68b0eSGleb Smirnoff free(sc->sc_ifas, M_CARP); 214308b68b0eSGleb Smirnoff sc->sc_ifas = new; 214408b68b0eSGleb Smirnoff sc->sc_ifasiz *= 2; 21459c2cd1aaSGleb Smirnoff CARP_UNLOCK(sc); 214608b68b0eSGleb Smirnoff } 214708b68b0eSGleb Smirnoff 214808b68b0eSGleb Smirnoff static void 214908b68b0eSGleb Smirnoff carp_destroy(struct carp_softc *sc) 215008b68b0eSGleb Smirnoff { 215108b68b0eSGleb Smirnoff struct ifnet *ifp = sc->sc_carpdev; 215208b68b0eSGleb Smirnoff struct carp_if *cif = ifp->if_carp; 215308b68b0eSGleb Smirnoff 21549c2cd1aaSGleb Smirnoff sx_assert(&carp_sx, SA_XLOCKED); 2155a9a2c40cSGleb Smirnoff 21569c2cd1aaSGleb Smirnoff if (sc->sc_suppress) 21579c2cd1aaSGleb Smirnoff carp_demote_adj(-V_carp_ifdown_adj, "vhid removed"); 21589c2cd1aaSGleb Smirnoff CARP_UNLOCK(sc); 21599c2cd1aaSGleb Smirnoff 21609c2cd1aaSGleb Smirnoff CIF_LOCK(cif); 216108b68b0eSGleb Smirnoff TAILQ_REMOVE(&cif->cif_vrs, sc, sc_list); 21629c2cd1aaSGleb Smirnoff CIF_UNLOCK(cif); 216308b68b0eSGleb Smirnoff 216408b68b0eSGleb Smirnoff mtx_lock(&carp_mtx); 216508b68b0eSGleb Smirnoff LIST_REMOVE(sc, sc_next); 216608b68b0eSGleb Smirnoff mtx_unlock(&carp_mtx); 216708b68b0eSGleb Smirnoff 216808b68b0eSGleb Smirnoff callout_drain(&sc->sc_ad_tmo); 216908b68b0eSGleb Smirnoff #ifdef INET 217008b68b0eSGleb Smirnoff callout_drain(&sc->sc_md_tmo); 217108b68b0eSGleb Smirnoff #endif 217208b68b0eSGleb Smirnoff #ifdef INET6 217308b68b0eSGleb Smirnoff callout_drain(&sc->sc_md6_tmo); 217408b68b0eSGleb Smirnoff #endif 217508b68b0eSGleb Smirnoff CARP_LOCK_DESTROY(sc); 217608b68b0eSGleb Smirnoff 217708b68b0eSGleb Smirnoff free(sc->sc_ifas, M_CARP); 217808b68b0eSGleb Smirnoff free(sc, M_CARP); 217908b68b0eSGleb Smirnoff } 218008b68b0eSGleb Smirnoff 218108b68b0eSGleb Smirnoff static struct carp_if* 218208b68b0eSGleb Smirnoff carp_alloc_if(struct ifnet *ifp) 2183a9771948SGleb Smirnoff { 218454bfbd51SWill Andrews struct carp_if *cif; 21850cc726f2SGleb Smirnoff int error; 2186d220759bSGleb Smirnoff 218708b68b0eSGleb Smirnoff cif = malloc(sizeof(*cif), M_CARP, M_WAITOK|M_ZERO); 218808b68b0eSGleb Smirnoff 21890cc726f2SGleb Smirnoff if ((error = ifpromisc(ifp, 1)) != 0) 21900cc726f2SGleb Smirnoff printf("%s: ifpromisc(%s) failed: %d\n", 2191511a6d5eSKristof Provost __func__, if_name(ifp), error); 21920cc726f2SGleb Smirnoff else 21930cc726f2SGleb Smirnoff cif->cif_flags |= CIF_PROMISC; 219408b68b0eSGleb Smirnoff 219508b68b0eSGleb Smirnoff CIF_LOCK_INIT(cif); 219608b68b0eSGleb Smirnoff cif->cif_ifp = ifp; 219708b68b0eSGleb Smirnoff TAILQ_INIT(&cif->cif_vrs); 219808b68b0eSGleb Smirnoff 2199137f91e8SJohn Baldwin IF_ADDR_WLOCK(ifp); 220008b68b0eSGleb Smirnoff ifp->if_carp = cif; 220108b68b0eSGleb Smirnoff if_ref(ifp); 2202137f91e8SJohn Baldwin IF_ADDR_WUNLOCK(ifp); 220308b68b0eSGleb Smirnoff 220408b68b0eSGleb Smirnoff return (cif); 2205d220759bSGleb Smirnoff } 2206d220759bSGleb Smirnoff 2207d220759bSGleb Smirnoff static void 220808b68b0eSGleb Smirnoff carp_free_if(struct carp_if *cif) 220908b68b0eSGleb Smirnoff { 221008b68b0eSGleb Smirnoff struct ifnet *ifp = cif->cif_ifp; 221108b68b0eSGleb Smirnoff 221208b68b0eSGleb Smirnoff CIF_LOCK_ASSERT(cif); 221308b68b0eSGleb Smirnoff KASSERT(TAILQ_EMPTY(&cif->cif_vrs), ("%s: softc list not empty", 221408b68b0eSGleb Smirnoff __func__)); 221508b68b0eSGleb Smirnoff 2216137f91e8SJohn Baldwin IF_ADDR_WLOCK(ifp); 221708b68b0eSGleb Smirnoff ifp->if_carp = NULL; 2218137f91e8SJohn Baldwin IF_ADDR_WUNLOCK(ifp); 221908b68b0eSGleb Smirnoff 222008b68b0eSGleb Smirnoff CIF_LOCK_DESTROY(cif); 222108b68b0eSGleb Smirnoff 22220cc726f2SGleb Smirnoff if (cif->cif_flags & CIF_PROMISC) 222308b68b0eSGleb Smirnoff ifpromisc(ifp, 0); 22241f6addd9SMikolaj Golub if_rele(ifp); 222508b68b0eSGleb Smirnoff 222608b68b0eSGleb Smirnoff free(cif, M_CARP); 222708b68b0eSGleb Smirnoff } 222808b68b0eSGleb Smirnoff 222940e04359SKristof Provost static bool 223040e04359SKristof Provost carp_carprcp(void *arg, struct carp_softc *sc, int priv) 223108b68b0eSGleb Smirnoff { 223240e04359SKristof Provost struct carpreq *carpr = arg; 223308b68b0eSGleb Smirnoff 223408b68b0eSGleb Smirnoff CARP_LOCK(sc); 223508b68b0eSGleb Smirnoff carpr->carpr_state = sc->sc_state; 223608b68b0eSGleb Smirnoff carpr->carpr_vhid = sc->sc_vhid; 2237a254d687SGleb Smirnoff switch (sc->sc_version) { 2238a254d687SGleb Smirnoff case CARP_VERSION_CARP: 223908b68b0eSGleb Smirnoff carpr->carpr_advbase = sc->sc_advbase; 224008b68b0eSGleb Smirnoff carpr->carpr_advskew = sc->sc_advskew; 224108b68b0eSGleb Smirnoff if (priv) 2242a254d687SGleb Smirnoff bcopy(sc->sc_key, carpr->carpr_key, 2243a254d687SGleb Smirnoff sizeof(carpr->carpr_key)); 224408b68b0eSGleb Smirnoff else 224508b68b0eSGleb Smirnoff bzero(carpr->carpr_key, sizeof(carpr->carpr_key)); 2246a254d687SGleb Smirnoff break; 2247a254d687SGleb Smirnoff case CARP_VERSION_VRRPv3: 2248a254d687SGleb Smirnoff break; 2249a254d687SGleb Smirnoff } 225008b68b0eSGleb Smirnoff CARP_UNLOCK(sc); 225140e04359SKristof Provost 225240e04359SKristof Provost return (true); 225308b68b0eSGleb Smirnoff } 225408b68b0eSGleb Smirnoff 225540e04359SKristof Provost static int 225613781800SKristof Provost carp_ioctl_set(if_t ifp, struct carpkreq *carpr) 225708b68b0eSGleb Smirnoff { 225849cad3daSZhenlei Huang struct epoch_tracker et; 225908b68b0eSGleb Smirnoff struct carp_softc *sc = NULL; 226040e04359SKristof Provost int error = 0; 226108b68b0eSGleb Smirnoff 2262a254d687SGleb Smirnoff if (carpr->carpr_vhid <= 0 || carpr->carpr_vhid > CARP_MAXVHID) 2263a254d687SGleb Smirnoff return (EINVAL); 2264a254d687SGleb Smirnoff 2265a254d687SGleb Smirnoff switch (carpr->carpr_version) { 2266a254d687SGleb Smirnoff case CARP_VERSION_CARP: 2267a254d687SGleb Smirnoff if (carpr->carpr_advbase != 0 && (carpr->carpr_advbase > 255 || 2268a254d687SGleb Smirnoff carpr->carpr_advbase < CARP_DFLTINTV)) 2269a254d687SGleb Smirnoff return (EINVAL); 2270a254d687SGleb Smirnoff if (carpr->carpr_advskew < 0 || carpr->carpr_advskew >= 255) 2271a254d687SGleb Smirnoff return (EINVAL); 2272a254d687SGleb Smirnoff break; 2273a254d687SGleb Smirnoff case CARP_VERSION_VRRPv3: 2274a254d687SGleb Smirnoff /* XXXGL: shouldn't we check anything? */ 2275a254d687SGleb Smirnoff break; 2276a254d687SGleb Smirnoff default: 227740e04359SKristof Provost return (EINVAL); 227808b68b0eSGleb Smirnoff } 227908b68b0eSGleb Smirnoff 228008b68b0eSGleb Smirnoff if (ifp->if_carp) { 228108b68b0eSGleb Smirnoff IFNET_FOREACH_CARP(ifp, sc) 228240e04359SKristof Provost if (sc->sc_vhid == carpr->carpr_vhid) 228308b68b0eSGleb Smirnoff break; 228408b68b0eSGleb Smirnoff } 2285a254d687SGleb Smirnoff 2286a254d687SGleb Smirnoff if (sc == NULL) 2287a254d687SGleb Smirnoff sc = carp_alloc(ifp, carpr->carpr_version, carpr->carpr_vhid); 2288a254d687SGleb Smirnoff else if (sc->sc_version != carpr->carpr_version) 2289a254d687SGleb Smirnoff return (EINVAL); 2290a254d687SGleb Smirnoff 229108b68b0eSGleb Smirnoff CARP_LOCK(sc); 2292a254d687SGleb Smirnoff switch (sc->sc_version) { 2293a254d687SGleb Smirnoff case CARP_VERSION_CARP: 2294a254d687SGleb Smirnoff if (carpr->carpr_advbase != 0) 229540e04359SKristof Provost sc->sc_advbase = carpr->carpr_advbase; 229640e04359SKristof Provost sc->sc_advskew = carpr->carpr_advskew; 229713781800SKristof Provost if (carpr->carpr_addr.s_addr != INADDR_ANY) 229813781800SKristof Provost sc->sc_carpaddr = carpr->carpr_addr; 229913781800SKristof Provost if (!IN6_IS_ADDR_UNSPECIFIED(&carpr->carpr_addr6)) { 230013781800SKristof Provost memcpy(&sc->sc_carpaddr6, &carpr->carpr_addr6, 230113781800SKristof Provost sizeof(sc->sc_carpaddr6)); 230213781800SKristof Provost } 230340e04359SKristof Provost if (carpr->carpr_key[0] != '\0') { 230440e04359SKristof Provost bcopy(carpr->carpr_key, sc->sc_key, sizeof(sc->sc_key)); 230508b68b0eSGleb Smirnoff carp_hmac_prepare(sc); 230608b68b0eSGleb Smirnoff } 2307a254d687SGleb Smirnoff break; 2308a254d687SGleb Smirnoff case CARP_VERSION_VRRPv3: 230937115154SKristof Provost if (carpr->carpr_vrrp_priority != 0) 231037115154SKristof Provost sc->sc_vrrp_prio = carpr->carpr_vrrp_priority; 231137115154SKristof Provost if (carpr->carpr_vrrp_adv_inter) 231237115154SKristof Provost sc->sc_vrrp_adv_inter = carpr->carpr_vrrp_adv_inter; 2313a254d687SGleb Smirnoff break; 2314a254d687SGleb Smirnoff } 231537115154SKristof Provost 231608b68b0eSGleb Smirnoff if (sc->sc_state != INIT && 231740e04359SKristof Provost carpr->carpr_state != sc->sc_state) { 231840e04359SKristof Provost switch (carpr->carpr_state) { 231908b68b0eSGleb Smirnoff case BACKUP: 232008b68b0eSGleb Smirnoff callout_stop(&sc->sc_ad_tmo); 2321369a6708SWill Andrews carp_set_state(sc, BACKUP, 2322369a6708SWill Andrews "user requested via ifconfig"); 232308b68b0eSGleb Smirnoff carp_setrun(sc, 0); 232408b68b0eSGleb Smirnoff carp_delroute(sc); 232508b68b0eSGleb Smirnoff break; 232608b68b0eSGleb Smirnoff case MASTER: 232749cad3daSZhenlei Huang NET_EPOCH_ENTER(et); 2328369a6708SWill Andrews carp_master_down_locked(sc, 2329369a6708SWill Andrews "user requested via ifconfig"); 233049cad3daSZhenlei Huang NET_EPOCH_EXIT(et); 233108b68b0eSGleb Smirnoff break; 233208b68b0eSGleb Smirnoff default: 233308b68b0eSGleb Smirnoff break; 233408b68b0eSGleb Smirnoff } 233508b68b0eSGleb Smirnoff } 233640e04359SKristof Provost CARP_UNLOCK(sc); 233740e04359SKristof Provost 233840e04359SKristof Provost return (error); 233940e04359SKristof Provost } 234040e04359SKristof Provost 234140e04359SKristof Provost static int 234240e04359SKristof Provost carp_ioctl_get(if_t ifp, struct ucred *cred, struct carpreq *carpr, 234340e04359SKristof Provost bool (*outfn)(void *, struct carp_softc *, int), void *arg) 234408b68b0eSGleb Smirnoff { 234508b68b0eSGleb Smirnoff int priveleged; 234640e04359SKristof Provost struct carp_softc *sc; 234708b68b0eSGleb Smirnoff 234840e04359SKristof Provost if (carpr->carpr_vhid < 0 || carpr->carpr_vhid > CARP_MAXVHID) 234940e04359SKristof Provost return (EINVAL); 235040e04359SKristof Provost if (carpr->carpr_count < 1) 235140e04359SKristof Provost return (EMSGSIZE); 235240e04359SKristof Provost if (ifp->if_carp == NULL) 235340e04359SKristof Provost return (ENOENT); 235408b68b0eSGleb Smirnoff 235540e04359SKristof Provost priveleged = (priv_check_cred(cred, PRIV_NETINET_CARP) == 0); 235640e04359SKristof Provost if (carpr->carpr_vhid != 0) { 235708b68b0eSGleb Smirnoff IFNET_FOREACH_CARP(ifp, sc) 235840e04359SKristof Provost if (sc->sc_vhid == carpr->carpr_vhid) 235908b68b0eSGleb Smirnoff break; 236040e04359SKristof Provost if (sc == NULL) 236140e04359SKristof Provost return (ENOENT); 236240e04359SKristof Provost 236340e04359SKristof Provost if (! outfn(arg, sc, priveleged)) 236440e04359SKristof Provost return (ENOMEM); 236540e04359SKristof Provost carpr->carpr_count = 1; 236608b68b0eSGleb Smirnoff } else { 236740e04359SKristof Provost int count; 236808b68b0eSGleb Smirnoff 236908b68b0eSGleb Smirnoff count = 0; 237008b68b0eSGleb Smirnoff IFNET_FOREACH_CARP(ifp, sc) 237108b68b0eSGleb Smirnoff count++; 237208b68b0eSGleb Smirnoff 237340e04359SKristof Provost if (count > carpr->carpr_count) 237440e04359SKristof Provost return (EMSGSIZE); 237540e04359SKristof Provost 237640e04359SKristof Provost IFNET_FOREACH_CARP(ifp, sc) { 237740e04359SKristof Provost if (! outfn(arg, sc, priveleged)) 237840e04359SKristof Provost return (ENOMEM); 237940e04359SKristof Provost carpr->carpr_count = count; 238040e04359SKristof Provost } 238108b68b0eSGleb Smirnoff } 238208b68b0eSGleb Smirnoff 238340e04359SKristof Provost return (0); 238440e04359SKristof Provost } 238540e04359SKristof Provost 238640e04359SKristof Provost int 238740e04359SKristof Provost carp_ioctl(struct ifreq *ifr, u_long cmd, struct thread *td) 238840e04359SKristof Provost { 238940e04359SKristof Provost struct carpreq carpr; 2390a254d687SGleb Smirnoff struct carpkreq carprk = { 2391a254d687SGleb Smirnoff .carpr_version = CARP_VERSION_CARP, 2392a254d687SGleb Smirnoff }; 239340e04359SKristof Provost struct ifnet *ifp; 239440e04359SKristof Provost int error = 0; 239540e04359SKristof Provost 239640e04359SKristof Provost if ((error = copyin(ifr_data_get_ptr(ifr), &carpr, sizeof carpr))) 239740e04359SKristof Provost return (error); 239840e04359SKristof Provost 239940e04359SKristof Provost ifp = ifunit_ref(ifr->ifr_name); 240040e04359SKristof Provost if ((error = carp_is_supported_if(ifp)) != 0) 240140e04359SKristof Provost goto out; 240240e04359SKristof Provost 240340e04359SKristof Provost if ((ifp->if_flags & IFF_MULTICAST) == 0) { 240440e04359SKristof Provost error = EADDRNOTAVAIL; 240540e04359SKristof Provost goto out; 240640e04359SKristof Provost } 240740e04359SKristof Provost 240840e04359SKristof Provost sx_xlock(&carp_sx); 240940e04359SKristof Provost switch (cmd) { 241040e04359SKristof Provost case SIOCSVH: 241140e04359SKristof Provost if ((error = priv_check(td, PRIV_NETINET_CARP))) 241240e04359SKristof Provost break; 241340e04359SKristof Provost 241413781800SKristof Provost memcpy(&carprk, &carpr, sizeof(carpr)); 241513781800SKristof Provost error = carp_ioctl_set(ifp, &carprk); 241640e04359SKristof Provost break; 241740e04359SKristof Provost 241840e04359SKristof Provost case SIOCGVH: 241940e04359SKristof Provost error = carp_ioctl_get(ifp, td->td_ucred, &carpr, 242040e04359SKristof Provost carp_carprcp, &carpr); 242140e04359SKristof Provost if (error == 0) { 2422541d96aaSBrooks Davis error = copyout(&carpr, 242340e04359SKristof Provost (char *)ifr_data_get_ptr(ifr), 242440e04359SKristof Provost carpr.carpr_count * sizeof(carpr)); 242508b68b0eSGleb Smirnoff } 242608b68b0eSGleb Smirnoff break; 242708b68b0eSGleb Smirnoff default: 242808b68b0eSGleb Smirnoff error = EINVAL; 242908b68b0eSGleb Smirnoff } 243093d4534cSGleb Smirnoff sx_xunlock(&carp_sx); 243108b68b0eSGleb Smirnoff 243208b68b0eSGleb Smirnoff out: 243340e04359SKristof Provost if (ifp != NULL) 243408b68b0eSGleb Smirnoff if_rele(ifp); 243508b68b0eSGleb Smirnoff 243608b68b0eSGleb Smirnoff return (error); 243708b68b0eSGleb Smirnoff } 243808b68b0eSGleb Smirnoff 243908b68b0eSGleb Smirnoff static int 244008b68b0eSGleb Smirnoff carp_get_vhid(struct ifaddr *ifa) 244108b68b0eSGleb Smirnoff { 244208b68b0eSGleb Smirnoff 244308b68b0eSGleb Smirnoff if (ifa == NULL || ifa->ifa_carp == NULL) 244408b68b0eSGleb Smirnoff return (0); 244508b68b0eSGleb Smirnoff 244608b68b0eSGleb Smirnoff return (ifa->ifa_carp->sc_vhid); 244708b68b0eSGleb Smirnoff } 244808b68b0eSGleb Smirnoff 244908b68b0eSGleb Smirnoff int 245008b68b0eSGleb Smirnoff carp_attach(struct ifaddr *ifa, int vhid) 245108b68b0eSGleb Smirnoff { 245208b68b0eSGleb Smirnoff struct ifnet *ifp = ifa->ifa_ifp; 2453a9a2c40cSGleb Smirnoff struct carp_if *cif = ifp->if_carp; 245408b68b0eSGleb Smirnoff struct carp_softc *sc; 245508b68b0eSGleb Smirnoff int index, error; 245608b68b0eSGleb Smirnoff 24579c2cd1aaSGleb Smirnoff KASSERT(ifa->ifa_carp == NULL, ("%s: ifa %p attached", __func__, ifa)); 245808b68b0eSGleb Smirnoff 245908b68b0eSGleb Smirnoff switch (ifa->ifa_addr->sa_family) { 246008b68b0eSGleb Smirnoff #ifdef INET 246108b68b0eSGleb Smirnoff case AF_INET: 246208b68b0eSGleb Smirnoff #endif 246308b68b0eSGleb Smirnoff #ifdef INET6 246408b68b0eSGleb Smirnoff case AF_INET6: 246508b68b0eSGleb Smirnoff #endif 246608b68b0eSGleb Smirnoff break; 246708b68b0eSGleb Smirnoff default: 246808b68b0eSGleb Smirnoff return (EPROTOTYPE); 246908b68b0eSGleb Smirnoff } 247008b68b0eSGleb Smirnoff 24719c2cd1aaSGleb Smirnoff sx_xlock(&carp_sx); 24729c2cd1aaSGleb Smirnoff if (ifp->if_carp == NULL) { 24739c2cd1aaSGleb Smirnoff sx_xunlock(&carp_sx); 24749c2cd1aaSGleb Smirnoff return (ENOPROTOOPT); 24759c2cd1aaSGleb Smirnoff } 24769c2cd1aaSGleb Smirnoff 247708b68b0eSGleb Smirnoff IFNET_FOREACH_CARP(ifp, sc) 247808b68b0eSGleb Smirnoff if (sc->sc_vhid == vhid) 247908b68b0eSGleb Smirnoff break; 2480a9a2c40cSGleb Smirnoff if (sc == NULL) { 24819c2cd1aaSGleb Smirnoff sx_xunlock(&carp_sx); 248208b68b0eSGleb Smirnoff return (ENOENT); 2483a9a2c40cSGleb Smirnoff } 248408b68b0eSGleb Smirnoff 2485a9a2c40cSGleb Smirnoff error = carp_multicast_setup(cif, ifa->ifa_addr->sa_family); 2486a9a2c40cSGleb Smirnoff if (error) { 2487a9a2c40cSGleb Smirnoff CIF_FREE(cif); 24889c2cd1aaSGleb Smirnoff sx_xunlock(&carp_sx); 248908b68b0eSGleb Smirnoff return (error); 2490a9a2c40cSGleb Smirnoff } 249108b68b0eSGleb Smirnoff 249208b68b0eSGleb Smirnoff index = sc->sc_naddrs + sc->sc_naddrs6 + 1; 249308b68b0eSGleb Smirnoff if (index > sc->sc_ifasiz / sizeof(struct ifaddr *)) 24949c2cd1aaSGleb Smirnoff carp_grow_ifas(sc); 249508b68b0eSGleb Smirnoff 249608b68b0eSGleb Smirnoff switch (ifa->ifa_addr->sa_family) { 249708b68b0eSGleb Smirnoff #ifdef INET 249808b68b0eSGleb Smirnoff case AF_INET: 2499a9a2c40cSGleb Smirnoff cif->cif_naddrs++; 250008b68b0eSGleb Smirnoff sc->sc_naddrs++; 250108b68b0eSGleb Smirnoff break; 250208b68b0eSGleb Smirnoff #endif 250308b68b0eSGleb Smirnoff #ifdef INET6 250408b68b0eSGleb Smirnoff case AF_INET6: 2505a9a2c40cSGleb Smirnoff cif->cif_naddrs6++; 250608b68b0eSGleb Smirnoff sc->sc_naddrs6++; 250708b68b0eSGleb Smirnoff break; 250808b68b0eSGleb Smirnoff #endif 250908b68b0eSGleb Smirnoff } 251008b68b0eSGleb Smirnoff 251108b68b0eSGleb Smirnoff ifa_ref(ifa); 25129c2cd1aaSGleb Smirnoff 25139c2cd1aaSGleb Smirnoff CARP_LOCK(sc); 251408b68b0eSGleb Smirnoff sc->sc_ifas[index - 1] = ifa; 251508b68b0eSGleb Smirnoff ifa->ifa_carp = sc; 2516a254d687SGleb Smirnoff if (sc->sc_version == CARP_VERSION_CARP) 251708b68b0eSGleb Smirnoff carp_hmac_prepare(sc); 251808b68b0eSGleb Smirnoff carp_sc_state(sc); 251908b68b0eSGleb Smirnoff CARP_UNLOCK(sc); 25209c2cd1aaSGleb Smirnoff 25219c2cd1aaSGleb Smirnoff sx_xunlock(&carp_sx); 252208b68b0eSGleb Smirnoff 252308b68b0eSGleb Smirnoff return (0); 252408b68b0eSGleb Smirnoff } 252508b68b0eSGleb Smirnoff 252608b68b0eSGleb Smirnoff void 2527338e227aSLuiz Otavio O Souza carp_detach(struct ifaddr *ifa, bool keep_cif) 252808b68b0eSGleb Smirnoff { 2529a9a2c40cSGleb Smirnoff struct ifnet *ifp = ifa->ifa_ifp; 2530a9a2c40cSGleb Smirnoff struct carp_if *cif = ifp->if_carp; 253108b68b0eSGleb Smirnoff struct carp_softc *sc = ifa->ifa_carp; 253208b68b0eSGleb Smirnoff int i, index; 253308b68b0eSGleb Smirnoff 253408b68b0eSGleb Smirnoff KASSERT(sc != NULL, ("%s: %p not attached", __func__, ifa)); 253508b68b0eSGleb Smirnoff 25369c2cd1aaSGleb Smirnoff sx_xlock(&carp_sx); 253708b68b0eSGleb Smirnoff 25389c2cd1aaSGleb Smirnoff CARP_LOCK(sc); 253908b68b0eSGleb Smirnoff /* Shift array. */ 254008b68b0eSGleb Smirnoff index = sc->sc_naddrs + sc->sc_naddrs6; 254108b68b0eSGleb Smirnoff for (i = 0; i < index; i++) 254208b68b0eSGleb Smirnoff if (sc->sc_ifas[i] == ifa) 254308b68b0eSGleb Smirnoff break; 254408b68b0eSGleb Smirnoff KASSERT(i < index, ("%s: %p no backref", __func__, ifa)); 254508b68b0eSGleb Smirnoff for (; i < index - 1; i++) 254608b68b0eSGleb Smirnoff sc->sc_ifas[i] = sc->sc_ifas[i+1]; 254708b68b0eSGleb Smirnoff sc->sc_ifas[index - 1] = NULL; 254808b68b0eSGleb Smirnoff 254908b68b0eSGleb Smirnoff switch (ifa->ifa_addr->sa_family) { 255008b68b0eSGleb Smirnoff #ifdef INET 255108b68b0eSGleb Smirnoff case AF_INET: 2552a9a2c40cSGleb Smirnoff cif->cif_naddrs--; 255308b68b0eSGleb Smirnoff sc->sc_naddrs--; 255408b68b0eSGleb Smirnoff break; 255508b68b0eSGleb Smirnoff #endif 255608b68b0eSGleb Smirnoff #ifdef INET6 255708b68b0eSGleb Smirnoff case AF_INET6: 2558a9a2c40cSGleb Smirnoff cif->cif_naddrs6--; 255908b68b0eSGleb Smirnoff sc->sc_naddrs6--; 256008b68b0eSGleb Smirnoff break; 256108b68b0eSGleb Smirnoff #endif 256208b68b0eSGleb Smirnoff } 256308b68b0eSGleb Smirnoff 25642512b096SGleb Smirnoff carp_ifa_delroute(ifa); 2565a9a2c40cSGleb Smirnoff carp_multicast_cleanup(cif, ifa->ifa_addr->sa_family); 256608b68b0eSGleb Smirnoff 256708b68b0eSGleb Smirnoff ifa->ifa_carp = NULL; 256808b68b0eSGleb Smirnoff ifa_free(ifa); 256908b68b0eSGleb Smirnoff 2570a254d687SGleb Smirnoff if (sc->sc_version == CARP_VERSION_CARP) 257108b68b0eSGleb Smirnoff carp_hmac_prepare(sc); 257208b68b0eSGleb Smirnoff carp_sc_state(sc); 257308b68b0eSGleb Smirnoff 2574338e227aSLuiz Otavio O Souza if (!keep_cif && sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) 257508b68b0eSGleb Smirnoff carp_destroy(sc); 25769c2cd1aaSGleb Smirnoff else 257708b68b0eSGleb Smirnoff CARP_UNLOCK(sc); 25789c2cd1aaSGleb Smirnoff 2579338e227aSLuiz Otavio O Souza if (!keep_cif) 25809c2cd1aaSGleb Smirnoff CIF_FREE(cif); 25819c2cd1aaSGleb Smirnoff 25829c2cd1aaSGleb Smirnoff sx_xunlock(&carp_sx); 258308b68b0eSGleb Smirnoff } 258408b68b0eSGleb Smirnoff 258508b68b0eSGleb Smirnoff static void 2586d01641e2SWill Andrews carp_set_state(struct carp_softc *sc, int state, const char *reason) 258708b68b0eSGleb Smirnoff { 258808b68b0eSGleb Smirnoff 258908b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); 259008b68b0eSGleb Smirnoff 259108b68b0eSGleb Smirnoff if (sc->sc_state != state) { 259208b68b0eSGleb Smirnoff const char *carp_states[] = { CARP_STATES }; 259308b68b0eSGleb Smirnoff char subsys[IFNAMSIZ+5]; 259408b68b0eSGleb Smirnoff 259508b68b0eSGleb Smirnoff snprintf(subsys, IFNAMSIZ+5, "%u@%s", sc->sc_vhid, 2596511a6d5eSKristof Provost if_name(sc->sc_carpdev)); 2597d01641e2SWill Andrews 2598d01641e2SWill Andrews CARP_LOG("%s: %s -> %s (%s)\n", subsys, 2599d01641e2SWill Andrews carp_states[sc->sc_state], carp_states[state], reason); 2600d01641e2SWill Andrews 2601d01641e2SWill Andrews sc->sc_state = state; 2602d01641e2SWill Andrews 260308b68b0eSGleb Smirnoff devctl_notify("CARP", subsys, carp_states[state], NULL); 260408b68b0eSGleb Smirnoff } 260508b68b0eSGleb Smirnoff } 260608b68b0eSGleb Smirnoff 260708b68b0eSGleb Smirnoff static void 260808b68b0eSGleb Smirnoff carp_linkstate(struct ifnet *ifp) 2609d220759bSGleb Smirnoff { 2610d220759bSGleb Smirnoff struct carp_softc *sc; 2611d220759bSGleb Smirnoff 261208b68b0eSGleb Smirnoff CIF_LOCK(ifp->if_carp); 261308b68b0eSGleb Smirnoff IFNET_FOREACH_CARP(ifp, sc) { 261408b68b0eSGleb Smirnoff CARP_LOCK(sc); 261508b68b0eSGleb Smirnoff carp_sc_state(sc); 261608b68b0eSGleb Smirnoff CARP_UNLOCK(sc); 261708b68b0eSGleb Smirnoff } 261808b68b0eSGleb Smirnoff CIF_UNLOCK(ifp->if_carp); 26194cb39345SGleb Smirnoff } 26204cb39345SGleb Smirnoff 26214cb39345SGleb Smirnoff static void 262208b68b0eSGleb Smirnoff carp_sc_state(struct carp_softc *sc) 26234cb39345SGleb Smirnoff { 262408b68b0eSGleb Smirnoff 262508b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); 26264cb39345SGleb Smirnoff 2627e8c34a71SGleb Smirnoff if (sc->sc_carpdev->if_link_state != LINK_STATE_UP || 2628167a3440SAlexander Motin !(sc->sc_carpdev->if_flags & IFF_UP) || 2629167a3440SAlexander Motin !V_carp_allow) { 2630a9771948SGleb Smirnoff callout_stop(&sc->sc_ad_tmo); 263108b68b0eSGleb Smirnoff #ifdef INET 2632a9771948SGleb Smirnoff callout_stop(&sc->sc_md_tmo); 263308b68b0eSGleb Smirnoff #endif 263408b68b0eSGleb Smirnoff #ifdef INET6 2635a9771948SGleb Smirnoff callout_stop(&sc->sc_md6_tmo); 263608b68b0eSGleb Smirnoff #endif 2637bb269f3aSWill Andrews carp_set_state(sc, INIT, "hardware interface down"); 2638a9771948SGleb Smirnoff carp_setrun(sc, 0); 2639600bf006SAndrey V. Elsukov carp_delroute(sc); 2640f08535f8SGleb Smirnoff if (!sc->sc_suppress) 2641c5c392e7SMikolaj Golub carp_demote_adj(V_carp_ifdown_adj, "interface down"); 2642a9771948SGleb Smirnoff sc->sc_suppress = 1; 2643a9771948SGleb Smirnoff } else { 2644bb269f3aSWill Andrews carp_set_state(sc, INIT, "hardware interface up"); 2645a9771948SGleb Smirnoff carp_setrun(sc, 0); 2646a9771948SGleb Smirnoff if (sc->sc_suppress) 2647c5c392e7SMikolaj Golub carp_demote_adj(-V_carp_ifdown_adj, "interface up"); 2648a9771948SGleb Smirnoff sc->sc_suppress = 0; 2649a9771948SGleb Smirnoff } 2650a9771948SGleb Smirnoff } 2651a9771948SGleb Smirnoff 2652f08535f8SGleb Smirnoff static void 2653f08535f8SGleb Smirnoff carp_demote_adj(int adj, char *reason) 2654f08535f8SGleb Smirnoff { 2655c5c392e7SMikolaj Golub atomic_add_int(&V_carp_demotion, adj); 2656c5c392e7SMikolaj Golub CARP_LOG("demoted by %d to %d (%s)\n", adj, V_carp_demotion, reason); 2657f08535f8SGleb Smirnoff taskqueue_enqueue(taskqueue_swi, &carp_sendall_task); 2658f08535f8SGleb Smirnoff } 265908b68b0eSGleb Smirnoff 26607951008bSGleb Smirnoff static int 2661167a3440SAlexander Motin carp_allow_sysctl(SYSCTL_HANDLER_ARGS) 2662167a3440SAlexander Motin { 2663167a3440SAlexander Motin int new, error; 2664167a3440SAlexander Motin struct carp_softc *sc; 2665167a3440SAlexander Motin 2666167a3440SAlexander Motin new = V_carp_allow; 2667167a3440SAlexander Motin error = sysctl_handle_int(oidp, &new, 0, req); 2668167a3440SAlexander Motin if (error || !req->newptr) 2669167a3440SAlexander Motin return (error); 2670167a3440SAlexander Motin 2671167a3440SAlexander Motin if (V_carp_allow != new) { 2672167a3440SAlexander Motin V_carp_allow = new; 2673167a3440SAlexander Motin 2674167a3440SAlexander Motin mtx_lock(&carp_mtx); 2675167a3440SAlexander Motin LIST_FOREACH(sc, &carp_list, sc_next) { 2676167a3440SAlexander Motin CARP_LOCK(sc); 2677167a3440SAlexander Motin if (curvnet == sc->sc_carpdev->if_vnet) 2678167a3440SAlexander Motin carp_sc_state(sc); 2679167a3440SAlexander Motin CARP_UNLOCK(sc); 2680167a3440SAlexander Motin } 2681167a3440SAlexander Motin mtx_unlock(&carp_mtx); 2682167a3440SAlexander Motin } 2683167a3440SAlexander Motin 2684167a3440SAlexander Motin return (0); 2685167a3440SAlexander Motin } 2686167a3440SAlexander Motin 2687167a3440SAlexander Motin static int 26880d3d234cSKristof Provost carp_dscp_sysctl(SYSCTL_HANDLER_ARGS) 26890d3d234cSKristof Provost { 26900d3d234cSKristof Provost int new, error; 26910d3d234cSKristof Provost 26920d3d234cSKristof Provost new = V_carp_dscp; 26930d3d234cSKristof Provost error = sysctl_handle_int(oidp, &new, 0, req); 26940d3d234cSKristof Provost if (error || !req->newptr) 26950d3d234cSKristof Provost return (error); 26960d3d234cSKristof Provost 26970d3d234cSKristof Provost if (new < 0 || new > 63) 26980d3d234cSKristof Provost return (EINVAL); 26990d3d234cSKristof Provost 27000d3d234cSKristof Provost V_carp_dscp = new; 27010d3d234cSKristof Provost 27020d3d234cSKristof Provost return (0); 27030d3d234cSKristof Provost } 27040d3d234cSKristof Provost 27050d3d234cSKristof Provost static int 27067951008bSGleb Smirnoff carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS) 27077951008bSGleb Smirnoff { 27087951008bSGleb Smirnoff int new, error; 27097951008bSGleb Smirnoff 2710c5c392e7SMikolaj Golub new = V_carp_demotion; 27117951008bSGleb Smirnoff error = sysctl_handle_int(oidp, &new, 0, req); 27127951008bSGleb Smirnoff if (error || !req->newptr) 27137951008bSGleb Smirnoff return (error); 27147951008bSGleb Smirnoff 27157951008bSGleb Smirnoff carp_demote_adj(new, "sysctl"); 27167951008bSGleb Smirnoff 27177951008bSGleb Smirnoff return (0); 27187951008bSGleb Smirnoff } 27197951008bSGleb Smirnoff 272040e04359SKristof Provost static int 272140e04359SKristof Provost nlattr_get_carp_key(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target) 272240e04359SKristof Provost { 272340e04359SKristof Provost if (__predict_false(NLA_DATA_LEN(nla) > CARP_KEY_LEN)) 272440e04359SKristof Provost return (EINVAL); 272540e04359SKristof Provost 272640e04359SKristof Provost memcpy(target, NLA_DATA_CONST(nla), NLA_DATA_LEN(nla)); 272740e04359SKristof Provost return (0); 272840e04359SKristof Provost } 272940e04359SKristof Provost 273040e04359SKristof Provost struct carp_nl_send_args { 273140e04359SKristof Provost struct nlmsghdr *hdr; 273240e04359SKristof Provost struct nl_pstate *npt; 273340e04359SKristof Provost }; 273440e04359SKristof Provost 273540e04359SKristof Provost static bool 273640e04359SKristof Provost carp_nl_send(void *arg, struct carp_softc *sc, int priv) 273740e04359SKristof Provost { 273840e04359SKristof Provost struct carp_nl_send_args *nlsa = arg; 273940e04359SKristof Provost struct nlmsghdr *hdr = nlsa->hdr; 274040e04359SKristof Provost struct nl_pstate *npt = nlsa->npt; 274140e04359SKristof Provost struct nl_writer *nw = npt->nw; 274240e04359SKristof Provost struct genlmsghdr *ghdr_new; 274340e04359SKristof Provost 274440e04359SKristof Provost if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr))) { 274540e04359SKristof Provost nlmsg_abort(nw); 274640e04359SKristof Provost return (false); 274740e04359SKristof Provost } 274840e04359SKristof Provost 274940e04359SKristof Provost ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr); 275040e04359SKristof Provost if (ghdr_new == NULL) { 275140e04359SKristof Provost nlmsg_abort(nw); 275240e04359SKristof Provost return (false); 275340e04359SKristof Provost } 275440e04359SKristof Provost 275540e04359SKristof Provost ghdr_new->cmd = CARP_NL_CMD_GET; 275640e04359SKristof Provost ghdr_new->version = 0; 275740e04359SKristof Provost ghdr_new->reserved = 0; 275840e04359SKristof Provost 275940e04359SKristof Provost CARP_LOCK(sc); 276040e04359SKristof Provost 276140e04359SKristof Provost nlattr_add_u32(nw, CARP_NL_VHID, sc->sc_vhid); 276240e04359SKristof Provost nlattr_add_u32(nw, CARP_NL_STATE, sc->sc_state); 2763a254d687SGleb Smirnoff nlattr_add_u8(nw, CARP_NL_VERSION, sc->sc_version); 2764a254d687SGleb Smirnoff switch (sc->sc_version) { 2765a254d687SGleb Smirnoff case CARP_VERSION_CARP: 276640e04359SKristof Provost nlattr_add_s32(nw, CARP_NL_ADVBASE, sc->sc_advbase); 276740e04359SKristof Provost nlattr_add_s32(nw, CARP_NL_ADVSKEW, sc->sc_advskew); 276813781800SKristof Provost nlattr_add_in_addr(nw, CARP_NL_ADDR, &sc->sc_carpaddr); 276913781800SKristof Provost nlattr_add_in6_addr(nw, CARP_NL_ADDR6, &sc->sc_carpaddr6); 277040e04359SKristof Provost if (priv) 2771a254d687SGleb Smirnoff nlattr_add(nw, CARP_NL_KEY, sizeof(sc->sc_key), 2772a254d687SGleb Smirnoff sc->sc_key); 2773a254d687SGleb Smirnoff break; 2774a254d687SGleb Smirnoff case CARP_VERSION_VRRPv3: 2775a254d687SGleb Smirnoff nlattr_add_u8(nw, CARP_NL_VRRP_PRIORITY, sc->sc_vrrp_prio); 2776a254d687SGleb Smirnoff nlattr_add_u16(nw, CARP_NL_VRRP_ADV_INTER, 2777a254d687SGleb Smirnoff sc->sc_vrrp_adv_inter); 2778a254d687SGleb Smirnoff break; 2779a254d687SGleb Smirnoff } 278040e04359SKristof Provost 278140e04359SKristof Provost CARP_UNLOCK(sc); 278240e04359SKristof Provost 278340e04359SKristof Provost if (! nlmsg_end(nw)) { 278440e04359SKristof Provost nlmsg_abort(nw); 278540e04359SKristof Provost return (false); 278640e04359SKristof Provost } 278740e04359SKristof Provost 278840e04359SKristof Provost return (true); 278940e04359SKristof Provost } 279040e04359SKristof Provost 279140e04359SKristof Provost struct nl_carp_parsed { 279240e04359SKristof Provost unsigned int ifindex; 279328921c4fSKristof Provost char *ifname; 279440e04359SKristof Provost uint32_t state; 279540e04359SKristof Provost uint32_t vhid; 279640e04359SKristof Provost int32_t advbase; 279740e04359SKristof Provost int32_t advskew; 279840e04359SKristof Provost char key[CARP_KEY_LEN]; 279913781800SKristof Provost struct in_addr addr; 280013781800SKristof Provost struct in6_addr addr6; 280137115154SKristof Provost carp_version_t version; 280237115154SKristof Provost uint8_t vrrp_prio; 280337115154SKristof Provost uint16_t vrrp_adv_inter; 280440e04359SKristof Provost }; 280540e04359SKristof Provost 280640e04359SKristof Provost #define _OUT(_field) offsetof(struct nl_carp_parsed, _field) 280740e04359SKristof Provost static const struct nlattr_parser nla_p_set[] = { 280840e04359SKristof Provost { .type = CARP_NL_VHID, .off = _OUT(vhid), .cb = nlattr_get_uint32 }, 280940e04359SKristof Provost { .type = CARP_NL_STATE, .off = _OUT(state), .cb = nlattr_get_uint32 }, 281040e04359SKristof Provost { .type = CARP_NL_ADVBASE, .off = _OUT(advbase), .cb = nlattr_get_uint32 }, 281140e04359SKristof Provost { .type = CARP_NL_ADVSKEW, .off = _OUT(advskew), .cb = nlattr_get_uint32 }, 281240e04359SKristof Provost { .type = CARP_NL_KEY, .off = _OUT(key), .cb = nlattr_get_carp_key }, 281340e04359SKristof Provost { .type = CARP_NL_IFINDEX, .off = _OUT(ifindex), .cb = nlattr_get_uint32 }, 281413781800SKristof Provost { .type = CARP_NL_ADDR, .off = _OUT(addr), .cb = nlattr_get_in_addr }, 281513781800SKristof Provost { .type = CARP_NL_ADDR6, .off = _OUT(addr6), .cb = nlattr_get_in6_addr }, 281628921c4fSKristof Provost { .type = CARP_NL_IFNAME, .off = _OUT(ifname), .cb = nlattr_get_string }, 281737115154SKristof Provost { .type = CARP_NL_VERSION, .off = _OUT(version), .cb = nlattr_get_uint8 }, 281837115154SKristof Provost { .type = CARP_NL_VRRP_PRIORITY, .off = _OUT(vrrp_prio), .cb = nlattr_get_uint8 }, 281937115154SKristof Provost { .type = CARP_NL_VRRP_ADV_INTER, .off = _OUT(vrrp_adv_inter), .cb = nlattr_get_uint16 }, 282040e04359SKristof Provost }; 2821*e9255dafSGleb Smirnoff NL_DECLARE_PARSER(carp_parser, struct genlmsghdr, nlf_p_empty, nla_p_set); 282240e04359SKristof Provost #undef _OUT 282340e04359SKristof Provost 282440e04359SKristof Provost 282540e04359SKristof Provost static int 282640e04359SKristof Provost carp_nl_get(struct nlmsghdr *hdr, struct nl_pstate *npt) 282740e04359SKristof Provost { 282840e04359SKristof Provost struct nl_carp_parsed attrs = { }; 282940e04359SKristof Provost struct carp_nl_send_args args; 283040e04359SKristof Provost struct carpreq carpr = { }; 283140e04359SKristof Provost struct epoch_tracker et; 283228921c4fSKristof Provost if_t ifp = NULL; 283340e04359SKristof Provost int error; 283440e04359SKristof Provost 283540e04359SKristof Provost error = nl_parse_nlmsg(hdr, &carp_parser, npt, &attrs); 283640e04359SKristof Provost if (error != 0) 283740e04359SKristof Provost return (error); 283840e04359SKristof Provost 283940e04359SKristof Provost NET_EPOCH_ENTER(et); 284028921c4fSKristof Provost if (attrs.ifname != NULL) 284128921c4fSKristof Provost ifp = ifunit_ref(attrs.ifname); 284228921c4fSKristof Provost else if (attrs.ifindex != 0) 284340e04359SKristof Provost ifp = ifnet_byindex_ref(attrs.ifindex); 284440e04359SKristof Provost NET_EPOCH_EXIT(et); 284540e04359SKristof Provost 284640e04359SKristof Provost if ((error = carp_is_supported_if(ifp)) != 0) 284740e04359SKristof Provost goto out; 284840e04359SKristof Provost 284940e04359SKristof Provost hdr->nlmsg_flags |= NLM_F_MULTI; 285040e04359SKristof Provost args.hdr = hdr; 285140e04359SKristof Provost args.npt = npt; 285240e04359SKristof Provost 285340e04359SKristof Provost carpr.carpr_vhid = attrs.vhid; 285440e04359SKristof Provost carpr.carpr_count = CARP_MAXVHID; 285540e04359SKristof Provost 285640e04359SKristof Provost sx_xlock(&carp_sx); 285740e04359SKristof Provost error = carp_ioctl_get(ifp, nlp_get_cred(npt->nlp), &carpr, 285840e04359SKristof Provost carp_nl_send, &args); 285940e04359SKristof Provost sx_xunlock(&carp_sx); 286040e04359SKristof Provost 286140e04359SKristof Provost if (! nlmsg_end_dump(npt->nw, error, hdr)) 286240e04359SKristof Provost error = ENOMEM; 286340e04359SKristof Provost 286440e04359SKristof Provost out: 286540e04359SKristof Provost if (ifp != NULL) 286640e04359SKristof Provost if_rele(ifp); 286740e04359SKristof Provost 286840e04359SKristof Provost return (error); 286940e04359SKristof Provost } 287040e04359SKristof Provost 287140e04359SKristof Provost static int 287240e04359SKristof Provost carp_nl_set(struct nlmsghdr *hdr, struct nl_pstate *npt) 287340e04359SKristof Provost { 287440e04359SKristof Provost struct nl_carp_parsed attrs = { }; 287513781800SKristof Provost struct carpkreq carpr; 287640e04359SKristof Provost struct epoch_tracker et; 287728921c4fSKristof Provost if_t ifp = NULL; 287840e04359SKristof Provost int error; 287940e04359SKristof Provost 288040e04359SKristof Provost error = nl_parse_nlmsg(hdr, &carp_parser, npt, &attrs); 288140e04359SKristof Provost if (error != 0) 288240e04359SKristof Provost return (error); 288340e04359SKristof Provost 288440e04359SKristof Provost if (attrs.vhid <= 0 || attrs.vhid > CARP_MAXVHID) 288540e04359SKristof Provost return (EINVAL); 288640e04359SKristof Provost if (attrs.state > CARP_MAXSTATE) 288740e04359SKristof Provost return (EINVAL); 2888a254d687SGleb Smirnoff if (attrs.version == 0) /* compat with pre-VRRPv3 */ 2889a254d687SGleb Smirnoff attrs.version = CARP_VERSION_CARP; 2890a254d687SGleb Smirnoff switch (attrs.version) { 2891a254d687SGleb Smirnoff case CARP_VERSION_CARP: 289240e04359SKristof Provost if (attrs.advbase < 0 || attrs.advskew < 0) 289340e04359SKristof Provost return (EINVAL); 289440e04359SKristof Provost if (attrs.advbase > 255) 289540e04359SKristof Provost return (EINVAL); 289640e04359SKristof Provost if (attrs.advskew >= 255) 289740e04359SKristof Provost return (EINVAL); 2898a254d687SGleb Smirnoff break; 2899a254d687SGleb Smirnoff case CARP_VERSION_VRRPv3: 290037115154SKristof Provost if (attrs.vrrp_adv_inter > VRRP_MAX_INTERVAL) 290137115154SKristof Provost return (EINVAL); 2902a254d687SGleb Smirnoff break; 2903a254d687SGleb Smirnoff default: 2904a254d687SGleb Smirnoff return (EINVAL); 2905a254d687SGleb Smirnoff } 290640e04359SKristof Provost 290740e04359SKristof Provost NET_EPOCH_ENTER(et); 290828921c4fSKristof Provost if (attrs.ifname != NULL) 290928921c4fSKristof Provost ifp = ifunit_ref(attrs.ifname); 291028921c4fSKristof Provost else if (attrs.ifindex != 0) 291140e04359SKristof Provost ifp = ifnet_byindex_ref(attrs.ifindex); 291240e04359SKristof Provost NET_EPOCH_EXIT(et); 291340e04359SKristof Provost 291440e04359SKristof Provost if ((error = carp_is_supported_if(ifp)) != 0) 291540e04359SKristof Provost goto out; 291640e04359SKristof Provost 291740e04359SKristof Provost if ((ifp->if_flags & IFF_MULTICAST) == 0) { 291840e04359SKristof Provost error = EADDRNOTAVAIL; 291940e04359SKristof Provost goto out; 292040e04359SKristof Provost } 292140e04359SKristof Provost 292240e04359SKristof Provost carpr.carpr_count = 1; 292340e04359SKristof Provost carpr.carpr_vhid = attrs.vhid; 292440e04359SKristof Provost carpr.carpr_state = attrs.state; 2925a254d687SGleb Smirnoff carpr.carpr_version = attrs.version; 2926a254d687SGleb Smirnoff switch (attrs.version) { 2927a254d687SGleb Smirnoff case CARP_VERSION_CARP: 292840e04359SKristof Provost carpr.carpr_advbase = attrs.advbase; 292940e04359SKristof Provost carpr.carpr_advskew = attrs.advskew; 293013781800SKristof Provost carpr.carpr_addr = attrs.addr; 293113781800SKristof Provost carpr.carpr_addr6 = attrs.addr6; 2932a254d687SGleb Smirnoff memcpy(&carpr.carpr_key, &attrs.key, sizeof(attrs.key)); 2933a254d687SGleb Smirnoff break; 2934a254d687SGleb Smirnoff case CARP_VERSION_VRRPv3: 293537115154SKristof Provost carpr.carpr_vrrp_priority = attrs.vrrp_prio; 293637115154SKristof Provost carpr.carpr_vrrp_adv_inter = attrs.vrrp_adv_inter; 2937a254d687SGleb Smirnoff break; 2938a254d687SGleb Smirnoff } 293940e04359SKristof Provost 294040e04359SKristof Provost sx_xlock(&carp_sx); 294140e04359SKristof Provost error = carp_ioctl_set(ifp, &carpr); 294240e04359SKristof Provost sx_xunlock(&carp_sx); 294340e04359SKristof Provost 294440e04359SKristof Provost out: 294540e04359SKristof Provost if (ifp != NULL) 294640e04359SKristof Provost if_rele(ifp); 294740e04359SKristof Provost 294840e04359SKristof Provost return (error); 294940e04359SKristof Provost } 295040e04359SKristof Provost 295140e04359SKristof Provost static const struct nlhdr_parser *all_parsers[] = { 295240e04359SKristof Provost &carp_parser 295340e04359SKristof Provost }; 295440e04359SKristof Provost 295540e04359SKristof Provost static const struct genl_cmd carp_cmds[] = { 295640e04359SKristof Provost { 295740e04359SKristof Provost .cmd_num = CARP_NL_CMD_GET, 295840e04359SKristof Provost .cmd_name = "SIOCGVH", 295940e04359SKristof Provost .cmd_cb = carp_nl_get, 296040e04359SKristof Provost .cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_DUMP | 296140e04359SKristof Provost GENL_CMD_CAP_HASPOL, 296240e04359SKristof Provost }, 296340e04359SKristof Provost { 296440e04359SKristof Provost .cmd_num = CARP_NL_CMD_SET, 296540e04359SKristof Provost .cmd_name = "SIOCSVH", 296640e04359SKristof Provost .cmd_cb = carp_nl_set, 296740e04359SKristof Provost .cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_HASPOL, 296840e04359SKristof Provost .cmd_priv = PRIV_NETINET_CARP, 296940e04359SKristof Provost }, 297040e04359SKristof Provost }; 297140e04359SKristof Provost 297240e04359SKristof Provost static void 297340e04359SKristof Provost carp_nl_register(void) 297440e04359SKristof Provost { 297540e04359SKristof Provost bool ret __diagused; 297640e04359SKristof Provost int family_id __diagused; 297740e04359SKristof Provost 297840e04359SKristof Provost NL_VERIFY_PARSERS(all_parsers); 297940e04359SKristof Provost family_id = genl_register_family(CARP_NL_FAMILY_NAME, 0, 2, 298040e04359SKristof Provost CARP_NL_CMD_MAX); 298140e04359SKristof Provost MPASS(family_id != 0); 298240e04359SKristof Provost 298340e04359SKristof Provost ret = genl_register_cmds(CARP_NL_FAMILY_NAME, carp_cmds, 298429f61502SGleb Smirnoff nitems(carp_cmds)); 298540e04359SKristof Provost MPASS(ret); 298640e04359SKristof Provost } 298740e04359SKristof Provost 298840e04359SKristof Provost static void 298940e04359SKristof Provost carp_nl_unregister(void) 299040e04359SKristof Provost { 299140e04359SKristof Provost genl_unregister_family(CARP_NL_FAMILY_NAME); 299240e04359SKristof Provost } 299340e04359SKristof Provost 299454bfbd51SWill Andrews static void 299554bfbd51SWill Andrews carp_mod_cleanup(void) 2996a9771948SGleb Smirnoff { 299754bfbd51SWill Andrews 299840e04359SKristof Provost carp_nl_unregister(); 299940e04359SKristof Provost 300054bfbd51SWill Andrews #ifdef INET 300115249f73SWill Andrews (void)ipproto_unregister(IPPROTO_CARP); 300254bfbd51SWill Andrews carp_iamatch_p = NULL; 300354bfbd51SWill Andrews #endif 300454bfbd51SWill Andrews #ifdef INET6 300515249f73SWill Andrews (void)ip6proto_unregister(IPPROTO_CARP); 300654bfbd51SWill Andrews carp_iamatch6_p = NULL; 300754bfbd51SWill Andrews carp_macmatch6_p = NULL; 300854bfbd51SWill Andrews #endif 300908b68b0eSGleb Smirnoff carp_ioctl_p = NULL; 301008b68b0eSGleb Smirnoff carp_attach_p = NULL; 301108b68b0eSGleb Smirnoff carp_detach_p = NULL; 301208b68b0eSGleb Smirnoff carp_get_vhid_p = NULL; 301354bfbd51SWill Andrews carp_linkstate_p = NULL; 301454bfbd51SWill Andrews carp_forus_p = NULL; 301554bfbd51SWill Andrews carp_output_p = NULL; 3016f08535f8SGleb Smirnoff carp_demote_adj_p = NULL; 301724421c1cSGleb Smirnoff carp_master_p = NULL; 3018f08535f8SGleb Smirnoff mtx_unlock(&carp_mtx); 3019f08535f8SGleb Smirnoff taskqueue_drain(taskqueue_swi, &carp_sendall_task); 302054bfbd51SWill Andrews mtx_destroy(&carp_mtx); 302193d4534cSGleb Smirnoff sx_destroy(&carp_sx); 302254bfbd51SWill Andrews } 302354bfbd51SWill Andrews 3024ee49c5d3SBoris Lytochkin static void 3025ee49c5d3SBoris Lytochkin ipcarp_sysinit(void) 3026ee49c5d3SBoris Lytochkin { 3027ee49c5d3SBoris Lytochkin 3028ee49c5d3SBoris Lytochkin /* Load allow as tunable so to postpone carp start after module load */ 3029ee49c5d3SBoris Lytochkin TUNABLE_INT_FETCH("net.inet.carp.allow", &V_carp_allow); 3030ee49c5d3SBoris Lytochkin } 3031ee49c5d3SBoris Lytochkin VNET_SYSINIT(ip_carp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, ipcarp_sysinit, NULL); 3032ee49c5d3SBoris Lytochkin 303354bfbd51SWill Andrews static int 303454bfbd51SWill Andrews carp_mod_load(void) 303554bfbd51SWill Andrews { 303615249f73SWill Andrews int err; 303754bfbd51SWill Andrews 3038d92d54d5SGleb Smirnoff mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF); 303993d4534cSGleb Smirnoff sx_init(&carp_sx, "carp_sx"); 304008b68b0eSGleb Smirnoff LIST_INIT(&carp_list); 304108b68b0eSGleb Smirnoff carp_get_vhid_p = carp_get_vhid; 304254bfbd51SWill Andrews carp_forus_p = carp_forus; 304354bfbd51SWill Andrews carp_output_p = carp_output; 304408b68b0eSGleb Smirnoff carp_linkstate_p = carp_linkstate; 304508b68b0eSGleb Smirnoff carp_ioctl_p = carp_ioctl; 304608b68b0eSGleb Smirnoff carp_attach_p = carp_attach; 304708b68b0eSGleb Smirnoff carp_detach_p = carp_detach; 3048f08535f8SGleb Smirnoff carp_demote_adj_p = carp_demote_adj; 304924421c1cSGleb Smirnoff carp_master_p = carp_master; 305054bfbd51SWill Andrews #ifdef INET6 305154bfbd51SWill Andrews carp_iamatch6_p = carp_iamatch6; 305254bfbd51SWill Andrews carp_macmatch6_p = carp_macmatch6; 305378b1fc05SGleb Smirnoff err = ip6proto_register(IPPROTO_CARP, carp6_input, NULL); 305415249f73SWill Andrews if (err) { 305515249f73SWill Andrews printf("carp: error %d registering with INET6\n", err); 305615249f73SWill Andrews carp_mod_cleanup(); 30576baf7a24SGleb Smirnoff return (err); 305815249f73SWill Andrews } 305954bfbd51SWill Andrews #endif 306054bfbd51SWill Andrews #ifdef INET 306154bfbd51SWill Andrews carp_iamatch_p = carp_iamatch; 306278b1fc05SGleb Smirnoff err = ipproto_register(IPPROTO_CARP, carp_input, NULL); 306315249f73SWill Andrews if (err) { 306415249f73SWill Andrews printf("carp: error %d registering with INET\n", err); 306515249f73SWill Andrews carp_mod_cleanup(); 30666baf7a24SGleb Smirnoff return (err); 306715249f73SWill Andrews } 306854bfbd51SWill Andrews #endif 306940e04359SKristof Provost 307040e04359SKristof Provost carp_nl_register(); 307140e04359SKristof Provost 307208b68b0eSGleb Smirnoff return (0); 307354bfbd51SWill Andrews } 3074a9771948SGleb Smirnoff 307554bfbd51SWill Andrews static int 307654bfbd51SWill Andrews carp_modevent(module_t mod, int type, void *data) 307754bfbd51SWill Andrews { 307854bfbd51SWill Andrews switch (type) { 307954bfbd51SWill Andrews case MOD_LOAD: 308054bfbd51SWill Andrews return carp_mod_load(); 308154bfbd51SWill Andrews /* NOTREACHED */ 3082a9771948SGleb Smirnoff case MOD_UNLOAD: 308308b68b0eSGleb Smirnoff mtx_lock(&carp_mtx); 308408b68b0eSGleb Smirnoff if (LIST_EMPTY(&carp_list)) 308554bfbd51SWill Andrews carp_mod_cleanup(); 308608b68b0eSGleb Smirnoff else { 308708b68b0eSGleb Smirnoff mtx_unlock(&carp_mtx); 308854bfbd51SWill Andrews return (EBUSY); 308908b68b0eSGleb Smirnoff } 3090a9771948SGleb Smirnoff break; 3091a9771948SGleb Smirnoff 3092a9771948SGleb Smirnoff default: 30930fa08018SGleb Smirnoff return (EINVAL); 3094a9771948SGleb Smirnoff } 3095a9771948SGleb Smirnoff 30960fa08018SGleb Smirnoff return (0); 3097a9771948SGleb Smirnoff } 3098a9771948SGleb Smirnoff 3099a9771948SGleb Smirnoff static moduledata_t carp_mod = { 3100a9771948SGleb Smirnoff "carp", 3101a9771948SGleb Smirnoff carp_modevent, 31029823d527SKevin Lo 0 3103a9771948SGleb Smirnoff }; 3104a9771948SGleb Smirnoff 3105e24fa11dSWill Andrews DECLARE_MODULE(carp, carp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY); 3106