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 3119e43c16SAlexander V. Chernikov #include "opt_netlink.h" 3219e43c16SAlexander V. Chernikov 334b421e2dSMike Silbersack #include <sys/cdefs.h> 344b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 354b421e2dSMike Silbersack 36a9771948SGleb Smirnoff #include "opt_bpf.h" 37a9771948SGleb Smirnoff #include "opt_inet.h" 38a9771948SGleb Smirnoff #include "opt_inet6.h" 39a9771948SGleb Smirnoff 40a9771948SGleb Smirnoff #include <sys/param.h> 41a9771948SGleb Smirnoff #include <sys/systm.h> 42773e541eSWarner Losh #include <sys/devctl.h> 4308b68b0eSGleb Smirnoff #include <sys/jail.h> 44a9771948SGleb Smirnoff #include <sys/kernel.h> 45a9771948SGleb Smirnoff #include <sys/limits.h> 46a9771948SGleb Smirnoff #include <sys/malloc.h> 47a9771948SGleb Smirnoff #include <sys/mbuf.h> 48a9771948SGleb Smirnoff #include <sys/module.h> 49acd3428bSRobert Watson #include <sys/priv.h> 50a9771948SGleb Smirnoff #include <sys/proc.h> 5108b68b0eSGleb Smirnoff #include <sys/socket.h> 5208b68b0eSGleb Smirnoff #include <sys/sockio.h> 53a9771948SGleb Smirnoff #include <sys/sysctl.h> 54a9771948SGleb Smirnoff #include <sys/syslog.h> 55f08535f8SGleb Smirnoff #include <sys/taskqueue.h> 5669edf037SAndrey V. Elsukov #include <sys/counter.h> 57a9771948SGleb Smirnoff 58a9771948SGleb Smirnoff #include <net/ethernet.h> 59a9771948SGleb Smirnoff #include <net/if.h> 6076039bc8SGleb Smirnoff #include <net/if_var.h> 61433aaf04SRuslan Ermilov #include <net/if_dl.h> 6208b68b0eSGleb Smirnoff #include <net/if_llatbl.h> 633d0d5b21SJustin Hibbits #include <net/if_private.h> 64a9771948SGleb Smirnoff #include <net/if_types.h> 65a9771948SGleb Smirnoff #include <net/route.h> 66530c0060SRobert Watson #include <net/vnet.h> 67a9771948SGleb Smirnoff 68a0ae8f04SBjoern A. Zeeb #if defined(INET) || defined(INET6) 69a9771948SGleb Smirnoff #include <netinet/in.h> 70a9771948SGleb Smirnoff #include <netinet/in_var.h> 71a0ae8f04SBjoern A. Zeeb #include <netinet/ip_carp.h> 7240e04359SKristof Provost #include <netinet/ip_carp_nl.h> 73a9771948SGleb Smirnoff #include <netinet/ip.h> 74a0ae8f04SBjoern A. Zeeb #include <machine/in_cksum.h> 75a0ae8f04SBjoern A. Zeeb #endif 76a0ae8f04SBjoern A. Zeeb #ifdef INET 77a9771948SGleb Smirnoff #include <netinet/ip_var.h> 78a9771948SGleb Smirnoff #include <netinet/if_ether.h> 79a9771948SGleb Smirnoff #endif 80a9771948SGleb Smirnoff 81a9771948SGleb Smirnoff #ifdef INET6 82a9771948SGleb Smirnoff #include <netinet/icmp6.h> 83a9771948SGleb Smirnoff #include <netinet/ip6.h> 8408b68b0eSGleb Smirnoff #include <netinet6/in6_var.h> 85a9771948SGleb Smirnoff #include <netinet6/ip6_var.h> 8629da8af6SHajimu UMEMOTO #include <netinet6/scope6_var.h> 87a9771948SGleb Smirnoff #include <netinet6/nd6.h> 88a9771948SGleb Smirnoff #endif 89a9771948SGleb Smirnoff 9040e04359SKristof Provost #include <netlink/netlink.h> 9140e04359SKristof Provost #include <netlink/netlink_ctl.h> 9240e04359SKristof Provost #include <netlink/netlink_generic.h> 9340e04359SKristof Provost #include <netlink/netlink_message_parser.h> 9440e04359SKristof Provost 95a9771948SGleb Smirnoff #include <crypto/sha1.h> 96a9771948SGleb Smirnoff 9708b68b0eSGleb Smirnoff static MALLOC_DEFINE(M_CARP, "CARP", "CARP addresses"); 98a9771948SGleb Smirnoff 99a9771948SGleb Smirnoff struct carp_softc { 10008b68b0eSGleb Smirnoff struct ifnet *sc_carpdev; /* Pointer to parent ifnet. */ 10108b68b0eSGleb Smirnoff struct ifaddr **sc_ifas; /* Our ifaddrs. */ 10208b68b0eSGleb Smirnoff struct sockaddr_dl sc_addr; /* Our link level address. */ 10308b68b0eSGleb Smirnoff struct callout sc_ad_tmo; /* Advertising timeout. */ 104a0ae8f04SBjoern A. Zeeb #ifdef INET 10508b68b0eSGleb Smirnoff struct callout sc_md_tmo; /* Master down timeout. */ 106a0ae8f04SBjoern A. Zeeb #endif 107a9771948SGleb Smirnoff #ifdef INET6 10808b68b0eSGleb Smirnoff struct callout sc_md6_tmo; /* XXX: Master down timeout. */ 10908b68b0eSGleb Smirnoff #endif 11008b68b0eSGleb Smirnoff struct mtx sc_mtx; 111a9771948SGleb Smirnoff 11208b68b0eSGleb Smirnoff int sc_vhid; 11308b68b0eSGleb Smirnoff int sc_advskew; 11408b68b0eSGleb Smirnoff int sc_advbase; 11513781800SKristof Provost struct in_addr sc_carpaddr; 11613781800SKristof Provost struct in6_addr sc_carpaddr6; 11708b68b0eSGleb Smirnoff 11808b68b0eSGleb Smirnoff int sc_naddrs; 11908b68b0eSGleb Smirnoff int sc_naddrs6; 12008b68b0eSGleb Smirnoff int sc_ifasiz; 121a9771948SGleb Smirnoff enum { INIT = 0, BACKUP, MASTER } sc_state; 122a9771948SGleb Smirnoff int sc_suppress; 123a9771948SGleb Smirnoff int sc_sendad_errors; 124a9771948SGleb Smirnoff #define CARP_SENDAD_MAX_ERRORS 3 125a9771948SGleb Smirnoff int sc_sendad_success; 126a9771948SGleb Smirnoff #define CARP_SENDAD_MIN_SUCCESS 3 127a9771948SGleb Smirnoff 128a9771948SGleb Smirnoff int sc_init_counter; 12908b68b0eSGleb Smirnoff uint64_t sc_counter; 130a9771948SGleb Smirnoff 131a9771948SGleb Smirnoff /* authentication */ 132a9771948SGleb Smirnoff #define CARP_HMAC_PAD 64 133a9771948SGleb Smirnoff unsigned char sc_key[CARP_KEY_LEN]; 134a9771948SGleb Smirnoff unsigned char sc_pad[CARP_HMAC_PAD]; 135a9771948SGleb Smirnoff SHA1_CTX sc_sha1; 136a9771948SGleb Smirnoff 13708b68b0eSGleb Smirnoff TAILQ_ENTRY(carp_softc) sc_list; /* On the carp_if list. */ 13808b68b0eSGleb Smirnoff LIST_ENTRY(carp_softc) sc_next; /* On the global list. */ 139a9771948SGleb Smirnoff }; 14008b68b0eSGleb Smirnoff 14108b68b0eSGleb Smirnoff struct carp_if { 14208b68b0eSGleb Smirnoff #ifdef INET 14308b68b0eSGleb Smirnoff int cif_naddrs; 14408b68b0eSGleb Smirnoff #endif 14508b68b0eSGleb Smirnoff #ifdef INET6 14608b68b0eSGleb Smirnoff int cif_naddrs6; 14708b68b0eSGleb Smirnoff #endif 14808b68b0eSGleb Smirnoff TAILQ_HEAD(, carp_softc) cif_vrs; 14908b68b0eSGleb Smirnoff #ifdef INET 15008b68b0eSGleb Smirnoff struct ip_moptions cif_imo; 15108b68b0eSGleb Smirnoff #endif 15208b68b0eSGleb Smirnoff #ifdef INET6 15308b68b0eSGleb Smirnoff struct ip6_moptions cif_im6o; 15408b68b0eSGleb Smirnoff #endif 15508b68b0eSGleb Smirnoff struct ifnet *cif_ifp; 15608b68b0eSGleb Smirnoff struct mtx cif_mtx; 1570cc726f2SGleb Smirnoff uint32_t cif_flags; 1580cc726f2SGleb Smirnoff #define CIF_PROMISC 0x00000001 15908b68b0eSGleb Smirnoff }; 16008b68b0eSGleb Smirnoff 16113781800SKristof Provost /* Kernel equivalent of struct carpreq, but with more fields for new features. 16213781800SKristof Provost * */ 16313781800SKristof Provost struct carpkreq { 16413781800SKristof Provost int carpr_count; 16513781800SKristof Provost int carpr_vhid; 16613781800SKristof Provost int carpr_state; 16713781800SKristof Provost int carpr_advskew; 16813781800SKristof Provost int carpr_advbase; 16913781800SKristof Provost unsigned char carpr_key[CARP_KEY_LEN]; 17013781800SKristof Provost /* Everything above this is identical to carpreq */ 17113781800SKristof Provost struct in_addr carpr_addr; 17213781800SKristof Provost struct in6_addr carpr_addr6; 17313781800SKristof Provost }; 17413781800SKristof Provost 17508b68b0eSGleb Smirnoff /* 17608b68b0eSGleb Smirnoff * Brief design of carp(4). 17708b68b0eSGleb Smirnoff * 17808b68b0eSGleb Smirnoff * Any carp-capable ifnet may have a list of carp softcs hanging off 17908b68b0eSGleb Smirnoff * its ifp->if_carp pointer. Each softc represents one unique virtual 18008b68b0eSGleb Smirnoff * host id, or vhid. The softc has a back pointer to the ifnet. All 18108b68b0eSGleb Smirnoff * softcs are joined in a global list, which has quite limited use. 18208b68b0eSGleb Smirnoff * 18308b68b0eSGleb Smirnoff * Any interface address that takes part in CARP negotiation has a 18408b68b0eSGleb Smirnoff * pointer to the softc of its vhid, ifa->ifa_carp. That could be either 18508b68b0eSGleb Smirnoff * AF_INET or AF_INET6 address. 18608b68b0eSGleb Smirnoff * 18708b68b0eSGleb Smirnoff * Although, one can get the softc's backpointer to ifnet and traverse 18808b68b0eSGleb Smirnoff * through its ifp->if_addrhead queue to find all interface addresses 18908b68b0eSGleb Smirnoff * involved in CARP, we keep a growable array of ifaddr pointers. This 19008b68b0eSGleb Smirnoff * allows us to avoid grabbing the IF_ADDR_LOCK() in many traversals that 19108b68b0eSGleb Smirnoff * do calls into the network stack, thus avoiding LORs. 19208b68b0eSGleb Smirnoff * 19308b68b0eSGleb Smirnoff * Locking: 19408b68b0eSGleb Smirnoff * 19508b68b0eSGleb Smirnoff * Each softc has a lock sc_mtx. It is used to synchronise carp_input_c(), 19608b68b0eSGleb Smirnoff * callout-driven events and ioctl()s. 19708b68b0eSGleb Smirnoff * 19881098a01SAlexander Motin * To traverse the list of softcs on an ifnet we use CIF_LOCK() or carp_sx. 19981098a01SAlexander Motin * To traverse the global list we use the mutex carp_mtx. 20008b68b0eSGleb Smirnoff * 20108b68b0eSGleb Smirnoff * Known issues with locking: 20208b68b0eSGleb Smirnoff * 20308b68b0eSGleb Smirnoff * - Sending ad, we put the pointer to the softc in an mtag, and no reference 20408b68b0eSGleb Smirnoff * counting is done on the softc. 20508b68b0eSGleb Smirnoff * - On module unload we may race (?) with packet processing thread 20608b68b0eSGleb Smirnoff * dereferencing our function pointers. 20708b68b0eSGleb Smirnoff */ 208a9771948SGleb Smirnoff 209c5c392e7SMikolaj Golub /* Accept incoming CARP packets. */ 2105f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_allow) = 1; 211c5c392e7SMikolaj Golub #define V_carp_allow VNET(carp_allow) 212c5c392e7SMikolaj Golub 2130d3d234cSKristof Provost /* Set DSCP in outgoing CARP packets. */ 2145f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_dscp) = 56; 2150d3d234cSKristof Provost #define V_carp_dscp VNET(carp_dscp) 2160d3d234cSKristof Provost 217c5c392e7SMikolaj Golub /* Preempt slower nodes. */ 2185f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_preempt) = 0; 219c5c392e7SMikolaj Golub #define V_carp_preempt VNET(carp_preempt) 220c5c392e7SMikolaj Golub 221c5c392e7SMikolaj Golub /* Log level. */ 2225f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_log) = 1; 223c5c392e7SMikolaj Golub #define V_carp_log VNET(carp_log) 224c5c392e7SMikolaj Golub 225c5c392e7SMikolaj Golub /* Global advskew demotion. */ 2265f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_demotion) = 0; 227c5c392e7SMikolaj Golub #define V_carp_demotion VNET(carp_demotion) 228c5c392e7SMikolaj Golub 229c5c392e7SMikolaj Golub /* Send error demotion factor. */ 2305f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_senderr_adj) = CARP_MAXSKEW; 231c5c392e7SMikolaj Golub #define V_carp_senderr_adj VNET(carp_senderr_adj) 232c5c392e7SMikolaj Golub 233c5c392e7SMikolaj Golub /* Iface down demotion factor. */ 2345f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_ifdown_adj) = CARP_MAXSKEW; 235c5c392e7SMikolaj Golub #define V_carp_ifdown_adj VNET(carp_ifdown_adj) 236c5c392e7SMikolaj Golub 237167a3440SAlexander Motin static int carp_allow_sysctl(SYSCTL_HANDLER_ARGS); 2380d3d234cSKristof Provost static int carp_dscp_sysctl(SYSCTL_HANDLER_ARGS); 2397951008bSGleb Smirnoff static int carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS); 240a9771948SGleb Smirnoff 24110b49b23SPawel Biernacki SYSCTL_NODE(_net_inet, IPPROTO_CARP, carp, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 24210b49b23SPawel Biernacki "CARP"); 243167a3440SAlexander Motin SYSCTL_PROC(_net_inet_carp, OID_AUTO, allow, 244ee49c5d3SBoris Lytochkin CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 245ee49c5d3SBoris Lytochkin &VNET_NAME(carp_allow), 0, carp_allow_sysctl, "I", 246167a3440SAlexander Motin "Accept incoming CARP packets"); 2470d3d234cSKristof Provost SYSCTL_PROC(_net_inet_carp, OID_AUTO, dscp, 24810b49b23SPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 24910b49b23SPawel Biernacki 0, 0, carp_dscp_sysctl, "I", 2500d3d234cSKristof Provost "DSCP value for carp packets"); 2516df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, preempt, CTLFLAG_VNET | CTLFLAG_RW, 252c5c392e7SMikolaj Golub &VNET_NAME(carp_preempt), 0, "High-priority backup preemption mode"); 2536df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, log, CTLFLAG_VNET | CTLFLAG_RW, 254c5c392e7SMikolaj Golub &VNET_NAME(carp_log), 0, "CARP log level"); 2556df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_carp, OID_AUTO, demotion, 25610b49b23SPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 2577951008bSGleb Smirnoff 0, 0, carp_demote_adj_sysctl, "I", 2587951008bSGleb Smirnoff "Adjust demotion factor (skew of advskew)"); 2596df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, senderr_demotion_factor, 2606df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, 261c5c392e7SMikolaj Golub &VNET_NAME(carp_senderr_adj), 0, "Send error demotion factor adjustment"); 2626df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, ifdown_demotion_factor, 2636df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, 264c5c392e7SMikolaj Golub &VNET_NAME(carp_ifdown_adj), 0, 265c5c392e7SMikolaj Golub "Interface down demotion factor adjustment"); 266f08535f8SGleb Smirnoff 267c5c392e7SMikolaj Golub VNET_PCPUSTAT_DEFINE(struct carpstats, carpstats); 268c5c392e7SMikolaj Golub VNET_PCPUSTAT_SYSINIT(carpstats); 269c5c392e7SMikolaj Golub VNET_PCPUSTAT_SYSUNINIT(carpstats); 270c5c392e7SMikolaj Golub 27169edf037SAndrey V. Elsukov #define CARPSTATS_ADD(name, val) \ 272c5c392e7SMikolaj Golub counter_u64_add(VNET(carpstats)[offsetof(struct carpstats, name) / \ 27369edf037SAndrey V. Elsukov sizeof(uint64_t)], (val)) 27469edf037SAndrey V. Elsukov #define CARPSTATS_INC(name) CARPSTATS_ADD(name, 1) 27569edf037SAndrey V. Elsukov 276c5c392e7SMikolaj Golub SYSCTL_VNET_PCPUSTAT(_net_inet_carp, OID_AUTO, stats, struct carpstats, 277c5c392e7SMikolaj Golub carpstats, "CARP statistics (struct carpstats, netinet/ip_carp.h)"); 278a9771948SGleb Smirnoff 27908b68b0eSGleb Smirnoff #define CARP_LOCK_INIT(sc) mtx_init(&(sc)->sc_mtx, "carp_softc", \ 280a9771948SGleb Smirnoff NULL, MTX_DEF) 28108b68b0eSGleb Smirnoff #define CARP_LOCK_DESTROY(sc) mtx_destroy(&(sc)->sc_mtx) 28208b68b0eSGleb Smirnoff #define CARP_LOCK_ASSERT(sc) mtx_assert(&(sc)->sc_mtx, MA_OWNED) 28308b68b0eSGleb Smirnoff #define CARP_LOCK(sc) mtx_lock(&(sc)->sc_mtx) 28408b68b0eSGleb Smirnoff #define CARP_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx) 28508b68b0eSGleb Smirnoff #define CIF_LOCK_INIT(cif) mtx_init(&(cif)->cif_mtx, "carp_if", \ 28608b68b0eSGleb Smirnoff NULL, MTX_DEF) 28708b68b0eSGleb Smirnoff #define CIF_LOCK_DESTROY(cif) mtx_destroy(&(cif)->cif_mtx) 28808b68b0eSGleb Smirnoff #define CIF_LOCK_ASSERT(cif) mtx_assert(&(cif)->cif_mtx, MA_OWNED) 28908b68b0eSGleb Smirnoff #define CIF_LOCK(cif) mtx_lock(&(cif)->cif_mtx) 29008b68b0eSGleb Smirnoff #define CIF_UNLOCK(cif) mtx_unlock(&(cif)->cif_mtx) 291a9a2c40cSGleb Smirnoff #define CIF_FREE(cif) do { \ 2929c2cd1aaSGleb Smirnoff CIF_LOCK(cif); \ 293a9a2c40cSGleb Smirnoff if (TAILQ_EMPTY(&(cif)->cif_vrs)) \ 294a9a2c40cSGleb Smirnoff carp_free_if(cif); \ 295a9a2c40cSGleb Smirnoff else \ 296a9a2c40cSGleb Smirnoff CIF_UNLOCK(cif); \ 297a9a2c40cSGleb Smirnoff } while (0) 298d220759bSGleb Smirnoff 299947b7cf3SGleb Smirnoff #define CARP_LOG(...) do { \ 300c5c392e7SMikolaj Golub if (V_carp_log > 0) \ 30108b68b0eSGleb Smirnoff log(LOG_INFO, "carp: " __VA_ARGS__); \ 302947b7cf3SGleb Smirnoff } while (0) 3031e9e6572SGleb Smirnoff 304947b7cf3SGleb Smirnoff #define CARP_DEBUG(...) do { \ 305c5c392e7SMikolaj Golub if (V_carp_log > 1) \ 3061e9e6572SGleb Smirnoff log(LOG_DEBUG, __VA_ARGS__); \ 307947b7cf3SGleb Smirnoff } while (0) 308a9771948SGleb Smirnoff 30908b68b0eSGleb Smirnoff #define IFNET_FOREACH_IFA(ifp, ifa) \ 310d7c5a620SMatt Macy CK_STAILQ_FOREACH((ifa), &(ifp)->if_addrhead, ifa_link) \ 31108b68b0eSGleb Smirnoff if ((ifa)->ifa_carp != NULL) 31208b68b0eSGleb Smirnoff 31308b68b0eSGleb Smirnoff #define CARP_FOREACH_IFA(sc, ifa) \ 31408b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); \ 31508b68b0eSGleb Smirnoff for (int _i = 0; \ 31608b68b0eSGleb Smirnoff _i < (sc)->sc_naddrs + (sc)->sc_naddrs6 && \ 31708b68b0eSGleb Smirnoff ((ifa) = sc->sc_ifas[_i]) != NULL; \ 31808b68b0eSGleb Smirnoff ++_i) 31908b68b0eSGleb Smirnoff 32008b68b0eSGleb Smirnoff #define IFNET_FOREACH_CARP(ifp, sc) \ 32181098a01SAlexander Motin KASSERT(mtx_owned(&ifp->if_carp->cif_mtx) || \ 32281098a01SAlexander Motin sx_xlocked(&carp_sx), ("cif_vrs not locked")); \ 32308b68b0eSGleb Smirnoff TAILQ_FOREACH((sc), &(ifp)->if_carp->cif_vrs, sc_list) 32408b68b0eSGleb Smirnoff 325f08535f8SGleb Smirnoff #define DEMOTE_ADVSKEW(sc) \ 326c5c392e7SMikolaj Golub (((sc)->sc_advskew + V_carp_demotion > CARP_MAXSKEW) ? \ 3271019354bSMarius Halden CARP_MAXSKEW : \ 3281019354bSMarius Halden (((sc)->sc_advskew + V_carp_demotion < 0) ? \ 3291019354bSMarius Halden 0 : ((sc)->sc_advskew + V_carp_demotion))) 330f08535f8SGleb Smirnoff 33113781800SKristof Provost static void carp_input_c(struct mbuf *, struct carp_header *, sa_family_t, int); 33208b68b0eSGleb Smirnoff static struct carp_softc 33308b68b0eSGleb Smirnoff *carp_alloc(struct ifnet *); 33408b68b0eSGleb Smirnoff static void carp_destroy(struct carp_softc *); 33508b68b0eSGleb Smirnoff static struct carp_if 33608b68b0eSGleb Smirnoff *carp_alloc_if(struct ifnet *); 33708b68b0eSGleb Smirnoff static void carp_free_if(struct carp_if *); 338d01641e2SWill Andrews static void carp_set_state(struct carp_softc *, int, const char* reason); 33908b68b0eSGleb Smirnoff static void carp_sc_state(struct carp_softc *); 34008b68b0eSGleb Smirnoff static void carp_setrun(struct carp_softc *, sa_family_t); 3415c1f0f6dSGleb Smirnoff static void carp_master_down(void *); 342d01641e2SWill Andrews static void carp_master_down_locked(struct carp_softc *, 343d01641e2SWill Andrews const char* reason); 34408b68b0eSGleb Smirnoff static void carp_send_ad(void *); 34508b68b0eSGleb Smirnoff static void carp_send_ad_locked(struct carp_softc *); 34608b68b0eSGleb Smirnoff static void carp_addroute(struct carp_softc *); 3472512b096SGleb Smirnoff static void carp_ifa_addroute(struct ifaddr *); 34808b68b0eSGleb Smirnoff static void carp_delroute(struct carp_softc *); 3492512b096SGleb Smirnoff static void carp_ifa_delroute(struct ifaddr *); 350f08535f8SGleb Smirnoff static void carp_send_ad_all(void *, int); 351f08535f8SGleb Smirnoff static void carp_demote_adj(int, char *); 352a9771948SGleb Smirnoff 35308b68b0eSGleb Smirnoff static LIST_HEAD(, carp_softc) carp_list; 354d92d54d5SGleb Smirnoff static struct mtx carp_mtx; 35593d4534cSGleb Smirnoff static struct sx carp_sx; 356f08535f8SGleb Smirnoff static struct task carp_sendall_task = 357f08535f8SGleb Smirnoff TASK_INITIALIZER(0, carp_send_ad_all, NULL); 358a9771948SGleb Smirnoff 35940e04359SKristof Provost static int 36040e04359SKristof Provost carp_is_supported_if(if_t ifp) 36140e04359SKristof Provost { 36240e04359SKristof Provost if (ifp == NULL) 36340e04359SKristof Provost return (ENXIO); 36440e04359SKristof Provost 36540e04359SKristof Provost switch (ifp->if_type) { 36640e04359SKristof Provost case IFT_ETHER: 36740e04359SKristof Provost case IFT_L2VLAN: 36840e04359SKristof Provost case IFT_BRIDGE: 36940e04359SKristof Provost break; 37040e04359SKristof Provost default: 37140e04359SKristof Provost return (EOPNOTSUPP); 37240e04359SKristof Provost } 37340e04359SKristof Provost 37440e04359SKristof Provost return (0); 37540e04359SKristof Provost } 37640e04359SKristof Provost 3775c1f0f6dSGleb Smirnoff static void 378a9771948SGleb Smirnoff carp_hmac_prepare(struct carp_softc *sc) 379a9771948SGleb Smirnoff { 38008b68b0eSGleb Smirnoff uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT; 38108b68b0eSGleb Smirnoff uint8_t vhid = sc->sc_vhid & 0xff; 382a9771948SGleb Smirnoff struct ifaddr *ifa; 3831ead26d4SMax Laier int i, found; 3841ead26d4SMax Laier #ifdef INET 3851ead26d4SMax Laier struct in_addr last, cur, in; 3861ead26d4SMax Laier #endif 387a9771948SGleb Smirnoff #ifdef INET6 3881ead26d4SMax Laier struct in6_addr last6, cur6, in6; 389a9771948SGleb Smirnoff #endif 390a9771948SGleb Smirnoff 39108b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); 392d220759bSGleb Smirnoff 39308b68b0eSGleb Smirnoff /* Compute ipad from key. */ 394a9771948SGleb Smirnoff bzero(sc->sc_pad, sizeof(sc->sc_pad)); 395a9771948SGleb Smirnoff bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key)); 396a9771948SGleb Smirnoff for (i = 0; i < sizeof(sc->sc_pad); i++) 397a9771948SGleb Smirnoff sc->sc_pad[i] ^= 0x36; 398a9771948SGleb Smirnoff 39908b68b0eSGleb Smirnoff /* Precompute first part of inner hash. */ 400a9771948SGleb Smirnoff SHA1Init(&sc->sc_sha1); 401a9771948SGleb Smirnoff SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad)); 402a9771948SGleb Smirnoff SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version)); 403a9771948SGleb Smirnoff SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type)); 404a9771948SGleb Smirnoff SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid)); 405a9771948SGleb Smirnoff #ifdef INET 4061ead26d4SMax Laier cur.s_addr = 0; 4071ead26d4SMax Laier do { 4081ead26d4SMax Laier found = 0; 4091ead26d4SMax Laier last = cur; 4101ead26d4SMax Laier cur.s_addr = 0xffffffff; 41108b68b0eSGleb Smirnoff CARP_FOREACH_IFA(sc, ifa) { 4121ead26d4SMax Laier in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr; 4131ead26d4SMax Laier if (ifa->ifa_addr->sa_family == AF_INET && 4141ead26d4SMax Laier ntohl(in.s_addr) > ntohl(last.s_addr) && 4151ead26d4SMax Laier ntohl(in.s_addr) < ntohl(cur.s_addr)) { 4161ead26d4SMax Laier cur.s_addr = in.s_addr; 4171ead26d4SMax Laier found++; 418a9771948SGleb Smirnoff } 4191ead26d4SMax Laier } 4201ead26d4SMax Laier if (found) 4211ead26d4SMax Laier SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur)); 4221ead26d4SMax Laier } while (found); 423a9771948SGleb Smirnoff #endif /* INET */ 424a9771948SGleb Smirnoff #ifdef INET6 4251ead26d4SMax Laier memset(&cur6, 0, sizeof(cur6)); 4261ead26d4SMax Laier do { 4271ead26d4SMax Laier found = 0; 4281ead26d4SMax Laier last6 = cur6; 4291ead26d4SMax Laier memset(&cur6, 0xff, sizeof(cur6)); 43008b68b0eSGleb Smirnoff CARP_FOREACH_IFA(sc, ifa) { 431a9771948SGleb Smirnoff in6 = ifatoia6(ifa)->ia_addr.sin6_addr; 4321ead26d4SMax Laier if (IN6_IS_SCOPE_EMBED(&in6)) 4331ead26d4SMax Laier in6.s6_addr16[1] = 0; 4341ead26d4SMax Laier if (ifa->ifa_addr->sa_family == AF_INET6 && 4351ead26d4SMax Laier memcmp(&in6, &last6, sizeof(in6)) > 0 && 4361ead26d4SMax Laier memcmp(&in6, &cur6, sizeof(in6)) < 0) { 4371ead26d4SMax Laier cur6 = in6; 4381ead26d4SMax Laier found++; 439a9771948SGleb Smirnoff } 440a9771948SGleb Smirnoff } 4411ead26d4SMax Laier if (found) 4421ead26d4SMax Laier SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6)); 4431ead26d4SMax Laier } while (found); 444a9771948SGleb Smirnoff #endif /* INET6 */ 445a9771948SGleb Smirnoff 446a9771948SGleb Smirnoff /* convert ipad to opad */ 447a9771948SGleb Smirnoff for (i = 0; i < sizeof(sc->sc_pad); i++) 448a9771948SGleb Smirnoff sc->sc_pad[i] ^= 0x36 ^ 0x5c; 449a9771948SGleb Smirnoff } 450a9771948SGleb Smirnoff 4515c1f0f6dSGleb Smirnoff static void 45208b68b0eSGleb Smirnoff carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2], 453a9771948SGleb Smirnoff unsigned char md[20]) 454a9771948SGleb Smirnoff { 455a9771948SGleb Smirnoff SHA1_CTX sha1ctx; 456a9771948SGleb Smirnoff 45708b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); 45808b68b0eSGleb Smirnoff 459a9771948SGleb Smirnoff /* fetch first half of inner hash */ 460a9771948SGleb Smirnoff bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx)); 461a9771948SGleb Smirnoff 462a9771948SGleb Smirnoff SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter)); 463a9771948SGleb Smirnoff SHA1Final(md, &sha1ctx); 464a9771948SGleb Smirnoff 465a9771948SGleb Smirnoff /* outer hash */ 466a9771948SGleb Smirnoff SHA1Init(&sha1ctx); 467a9771948SGleb Smirnoff SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad)); 468a9771948SGleb Smirnoff SHA1Update(&sha1ctx, md, 20); 469a9771948SGleb Smirnoff SHA1Final(md, &sha1ctx); 470a9771948SGleb Smirnoff } 471a9771948SGleb Smirnoff 4725c1f0f6dSGleb Smirnoff static int 47308b68b0eSGleb Smirnoff carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2], 474a9771948SGleb Smirnoff unsigned char md[20]) 475a9771948SGleb Smirnoff { 476a9771948SGleb Smirnoff unsigned char md2[20]; 477a9771948SGleb Smirnoff 47808b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); 479d220759bSGleb Smirnoff 480a9771948SGleb Smirnoff carp_hmac_generate(sc, counter, md2); 481a9771948SGleb Smirnoff 482a9771948SGleb Smirnoff return (bcmp(md, md2, sizeof(md2))); 483a9771948SGleb Smirnoff } 484a9771948SGleb Smirnoff 485a9771948SGleb Smirnoff /* 486a9771948SGleb Smirnoff * process input packet. 487a9771948SGleb Smirnoff * we have rearranged checks order compared to the rfc, 488a9771948SGleb Smirnoff * but it seems more efficient this way or not possible otherwise. 489a9771948SGleb Smirnoff */ 490a0ae8f04SBjoern A. Zeeb #ifdef INET 49178b1fc05SGleb Smirnoff static int 4928f5a8818SKevin Lo carp_input(struct mbuf **mp, int *offp, int proto) 493a9771948SGleb Smirnoff { 4948f5a8818SKevin Lo struct mbuf *m = *mp; 495a9771948SGleb Smirnoff struct ip *ip = mtod(m, struct ip *); 496a9771948SGleb Smirnoff struct carp_header *ch; 497a9771948SGleb Smirnoff int iplen, len; 498a9771948SGleb Smirnoff 4998f5a8818SKevin Lo iplen = *offp; 5008f5a8818SKevin Lo *mp = NULL; 5018f5a8818SKevin Lo 5026bf65bcfSRobert Watson CARPSTATS_INC(carps_ipackets); 503a9771948SGleb Smirnoff 504c5c392e7SMikolaj Golub if (!V_carp_allow) { 505a9771948SGleb Smirnoff m_freem(m); 5068f5a8818SKevin Lo return (IPPROTO_DONE); 507a9771948SGleb Smirnoff } 508a9771948SGleb Smirnoff 509a9771948SGleb Smirnoff iplen = ip->ip_hl << 2; 510a9771948SGleb Smirnoff 511a9771948SGleb Smirnoff if (m->m_pkthdr.len < iplen + sizeof(*ch)) { 5126bf65bcfSRobert Watson CARPSTATS_INC(carps_badlen); 51308b68b0eSGleb Smirnoff CARP_DEBUG("%s: received len %zd < sizeof(struct carp_header) " 51408b68b0eSGleb Smirnoff "on %s\n", __func__, m->m_len - sizeof(struct ip), 515511a6d5eSKristof Provost if_name(m->m_pkthdr.rcvif)); 516a9771948SGleb Smirnoff m_freem(m); 5178f5a8818SKevin Lo return (IPPROTO_DONE); 518a9771948SGleb Smirnoff } 519a9771948SGleb Smirnoff 520a9771948SGleb Smirnoff if (iplen + sizeof(*ch) < m->m_len) { 521a9771948SGleb Smirnoff if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) { 5226bf65bcfSRobert Watson CARPSTATS_INC(carps_hdrops); 52308b68b0eSGleb Smirnoff CARP_DEBUG("%s: pullup failed\n", __func__); 5248f5a8818SKevin Lo return (IPPROTO_DONE); 525a9771948SGleb Smirnoff } 526a9771948SGleb Smirnoff ip = mtod(m, struct ip *); 527a9771948SGleb Smirnoff } 528a9771948SGleb Smirnoff ch = (struct carp_header *)((char *)ip + iplen); 529a9771948SGleb Smirnoff 530a9771948SGleb Smirnoff /* 531a9771948SGleb Smirnoff * verify that the received packet length is 532a9771948SGleb Smirnoff * equal to the CARP header 533a9771948SGleb Smirnoff */ 534a9771948SGleb Smirnoff len = iplen + sizeof(*ch); 535a9771948SGleb Smirnoff if (len > m->m_pkthdr.len) { 5366bf65bcfSRobert Watson CARPSTATS_INC(carps_badlen); 53708b68b0eSGleb Smirnoff CARP_DEBUG("%s: packet too short %d on %s\n", __func__, 5381e9e6572SGleb Smirnoff m->m_pkthdr.len, 539511a6d5eSKristof Provost if_name(m->m_pkthdr.rcvif)); 540a9771948SGleb Smirnoff m_freem(m); 5418f5a8818SKevin Lo return (IPPROTO_DONE); 542a9771948SGleb Smirnoff } 543a9771948SGleb Smirnoff 544a9771948SGleb Smirnoff if ((m = m_pullup(m, len)) == NULL) { 5456bf65bcfSRobert Watson CARPSTATS_INC(carps_hdrops); 5468f5a8818SKevin Lo return (IPPROTO_DONE); 547a9771948SGleb Smirnoff } 548a9771948SGleb Smirnoff ip = mtod(m, struct ip *); 549a9771948SGleb Smirnoff ch = (struct carp_header *)((char *)ip + iplen); 550a9771948SGleb Smirnoff 551a9771948SGleb Smirnoff /* verify the CARP checksum */ 552a9771948SGleb Smirnoff m->m_data += iplen; 553c4d06976SGleb Smirnoff if (in_cksum(m, len - iplen)) { 5546bf65bcfSRobert Watson CARPSTATS_INC(carps_badsum); 55508b68b0eSGleb Smirnoff CARP_DEBUG("%s: checksum failed on %s\n", __func__, 556511a6d5eSKristof Provost if_name(m->m_pkthdr.rcvif)); 557a9771948SGleb Smirnoff m_freem(m); 5588f5a8818SKevin Lo return (IPPROTO_DONE); 559a9771948SGleb Smirnoff } 560a9771948SGleb Smirnoff m->m_data -= iplen; 561a9771948SGleb Smirnoff 56213781800SKristof Provost carp_input_c(m, ch, AF_INET, ip->ip_ttl); 5638f5a8818SKevin Lo return (IPPROTO_DONE); 564a9771948SGleb Smirnoff } 565a0ae8f04SBjoern A. Zeeb #endif 566a9771948SGleb Smirnoff 567a9771948SGleb Smirnoff #ifdef INET6 56878b1fc05SGleb Smirnoff static int 569a9771948SGleb Smirnoff carp6_input(struct mbuf **mp, int *offp, int proto) 570a9771948SGleb Smirnoff { 571a9771948SGleb Smirnoff struct mbuf *m = *mp; 572a9771948SGleb Smirnoff struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 573a9771948SGleb Smirnoff struct carp_header *ch; 574a9771948SGleb Smirnoff u_int len; 575a9771948SGleb Smirnoff 5766bf65bcfSRobert Watson CARPSTATS_INC(carps_ipackets6); 577a9771948SGleb Smirnoff 578c5c392e7SMikolaj Golub if (!V_carp_allow) { 579a9771948SGleb Smirnoff m_freem(m); 580a9771948SGleb Smirnoff return (IPPROTO_DONE); 581a9771948SGleb Smirnoff } 582a9771948SGleb Smirnoff 583a9771948SGleb Smirnoff /* check if received on a valid carp interface */ 584a9771948SGleb Smirnoff if (m->m_pkthdr.rcvif->if_carp == NULL) { 5856bf65bcfSRobert Watson CARPSTATS_INC(carps_badif); 58608b68b0eSGleb Smirnoff CARP_DEBUG("%s: packet received on non-carp interface: %s\n", 587511a6d5eSKristof Provost __func__, if_name(m->m_pkthdr.rcvif)); 588a9771948SGleb Smirnoff m_freem(m); 589a9771948SGleb Smirnoff return (IPPROTO_DONE); 590a9771948SGleb Smirnoff } 591a9771948SGleb Smirnoff 592a9771948SGleb Smirnoff /* verify that we have a complete carp packet */ 593a4adf6ccSBjoern A. Zeeb if (m->m_len < *offp + sizeof(*ch)) { 594a9771948SGleb Smirnoff len = m->m_len; 59563abacc2SBjoern A. Zeeb m = m_pullup(m, *offp + sizeof(*ch)); 59663abacc2SBjoern A. Zeeb if (m == NULL) { 5976bf65bcfSRobert Watson CARPSTATS_INC(carps_badlen); 59808b68b0eSGleb Smirnoff CARP_DEBUG("%s: packet size %u too small\n", __func__, len); 599a9771948SGleb Smirnoff return (IPPROTO_DONE); 600a9771948SGleb Smirnoff } 60113781800SKristof Provost ip6 = mtod(m, struct ip6_hdr *); 602a4adf6ccSBjoern A. Zeeb } 603dad68fc3SBjoern A. Zeeb ch = (struct carp_header *)(mtod(m, char *) + *offp); 604a9771948SGleb Smirnoff 605a9771948SGleb Smirnoff /* verify the CARP checksum */ 606a9771948SGleb Smirnoff m->m_data += *offp; 607c4d06976SGleb Smirnoff if (in_cksum(m, sizeof(*ch))) { 6086bf65bcfSRobert Watson CARPSTATS_INC(carps_badsum); 60908b68b0eSGleb Smirnoff CARP_DEBUG("%s: checksum failed, on %s\n", __func__, 610511a6d5eSKristof Provost if_name(m->m_pkthdr.rcvif)); 611a9771948SGleb Smirnoff m_freem(m); 612a9771948SGleb Smirnoff return (IPPROTO_DONE); 613a9771948SGleb Smirnoff } 614a9771948SGleb Smirnoff m->m_data -= *offp; 615a9771948SGleb Smirnoff 61613781800SKristof Provost carp_input_c(m, ch, AF_INET6, ip6->ip6_hlim); 617a9771948SGleb Smirnoff return (IPPROTO_DONE); 618a9771948SGleb Smirnoff } 619a9771948SGleb Smirnoff #endif /* INET6 */ 620a9771948SGleb Smirnoff 6218151740cSJosh Paetzel /* 6228151740cSJosh Paetzel * This routine should not be necessary at all, but some switches 6238151740cSJosh Paetzel * (VMWare ESX vswitches) can echo our own packets back at us, 6248151740cSJosh Paetzel * and we must ignore them or they will cause us to drop out of 6258151740cSJosh Paetzel * MASTER mode. 6268151740cSJosh Paetzel * 6278151740cSJosh Paetzel * We cannot catch all cases of network loops. Instead, what we 6288151740cSJosh Paetzel * do here is catch any packet that arrives with a carp header 6298151740cSJosh Paetzel * with a VHID of 0, that comes from an address that is our own. 6308151740cSJosh Paetzel * These packets are by definition "from us" (even if they are from 6318151740cSJosh Paetzel * a misconfigured host that is pretending to be us). 6328151740cSJosh Paetzel * 6338151740cSJosh Paetzel * The VHID test is outside this mini-function. 6348151740cSJosh Paetzel */ 6358151740cSJosh Paetzel static int 6368151740cSJosh Paetzel carp_source_is_self(struct mbuf *m, struct ifaddr *ifa, sa_family_t af) 6378151740cSJosh Paetzel { 638cfff8d3dSEnji Cooper #ifdef INET 6398151740cSJosh Paetzel struct ip *ip4; 6408151740cSJosh Paetzel struct in_addr in4; 641cfff8d3dSEnji Cooper #endif 642cfff8d3dSEnji Cooper #ifdef INET6 6438151740cSJosh Paetzel struct ip6_hdr *ip6; 6448151740cSJosh Paetzel struct in6_addr in6; 645cfff8d3dSEnji Cooper #endif 6468151740cSJosh Paetzel 6478151740cSJosh Paetzel switch (af) { 648cfff8d3dSEnji Cooper #ifdef INET 6498151740cSJosh Paetzel case AF_INET: 6508151740cSJosh Paetzel ip4 = mtod(m, struct ip *); 6518151740cSJosh Paetzel in4 = ifatoia(ifa)->ia_addr.sin_addr; 6528151740cSJosh Paetzel return (in4.s_addr == ip4->ip_src.s_addr); 653cfff8d3dSEnji Cooper #endif 654cfff8d3dSEnji Cooper #ifdef INET6 6558151740cSJosh Paetzel case AF_INET6: 6568151740cSJosh Paetzel ip6 = mtod(m, struct ip6_hdr *); 6578151740cSJosh Paetzel in6 = ifatoia6(ifa)->ia_addr.sin6_addr; 6588151740cSJosh Paetzel return (memcmp(&in6, &ip6->ip6_src, sizeof(in6)) == 0); 659cfff8d3dSEnji Cooper #endif 660cfff8d3dSEnji Cooper default: 6618151740cSJosh Paetzel break; 6628151740cSJosh Paetzel } 6638151740cSJosh Paetzel return (0); 6648151740cSJosh Paetzel } 6658151740cSJosh Paetzel 6665c1f0f6dSGleb Smirnoff static void 66713781800SKristof Provost carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af, int ttl) 668a9771948SGleb Smirnoff { 669a9771948SGleb Smirnoff struct ifnet *ifp = m->m_pkthdr.rcvif; 6708151740cSJosh Paetzel struct ifaddr *ifa, *match; 6711e9e6572SGleb Smirnoff struct carp_softc *sc; 67208b68b0eSGleb Smirnoff uint64_t tmp_counter; 673a9771948SGleb Smirnoff struct timeval sc_tv, ch_tv; 6748151740cSJosh Paetzel int error; 67513781800SKristof Provost bool multicast = false; 676a9771948SGleb Smirnoff 677b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 678b8a6e03fSGleb Smirnoff 6798151740cSJosh Paetzel /* 6808151740cSJosh Paetzel * Verify that the VHID is valid on the receiving interface. 6818151740cSJosh Paetzel * 6828151740cSJosh Paetzel * There should be just one match. If there are none 6838151740cSJosh Paetzel * the VHID is not valid and we drop the packet. If 6848151740cSJosh Paetzel * there are multiple VHID matches, take just the first 6858151740cSJosh Paetzel * one, for compatibility with previous code. While we're 6868151740cSJosh Paetzel * scanning, check for obvious loops in the network topology 6878151740cSJosh Paetzel * (these should never happen, and as noted above, we may 6888151740cSJosh Paetzel * miss real loops; this is just a double-check). 6898151740cSJosh Paetzel */ 6908151740cSJosh Paetzel error = 0; 6918151740cSJosh Paetzel match = NULL; 6928151740cSJosh Paetzel IFNET_FOREACH_IFA(ifp, ifa) { 6938151740cSJosh Paetzel if (match == NULL && ifa->ifa_carp != NULL && 6948151740cSJosh Paetzel ifa->ifa_addr->sa_family == af && 6958151740cSJosh Paetzel ifa->ifa_carp->sc_vhid == ch->carp_vhid) 6968151740cSJosh Paetzel match = ifa; 6978151740cSJosh Paetzel if (ch->carp_vhid == 0 && carp_source_is_self(m, ifa, af)) 6988151740cSJosh Paetzel error = ELOOP; 69908b68b0eSGleb Smirnoff } 7008151740cSJosh Paetzel ifa = error ? NULL : match; 7018151740cSJosh Paetzel if (ifa != NULL) 7028151740cSJosh Paetzel ifa_ref(ifa); 703d220759bSGleb Smirnoff 70408b68b0eSGleb Smirnoff if (ifa == NULL) { 7058151740cSJosh Paetzel if (error == ELOOP) { 7068151740cSJosh Paetzel CARP_DEBUG("dropping looped packet on interface %s\n", 707511a6d5eSKristof Provost if_name(ifp)); 7088151740cSJosh Paetzel CARPSTATS_INC(carps_badif); /* ??? */ 7098151740cSJosh Paetzel } else { 7106bf65bcfSRobert Watson CARPSTATS_INC(carps_badvhid); 7118151740cSJosh Paetzel } 712a9771948SGleb Smirnoff m_freem(m); 713a9771948SGleb Smirnoff return; 714a9771948SGleb Smirnoff } 715a9771948SGleb Smirnoff 716a9771948SGleb Smirnoff /* verify the CARP version. */ 717a9771948SGleb Smirnoff if (ch->carp_version != CARP_VERSION) { 7186bf65bcfSRobert Watson CARPSTATS_INC(carps_badver); 719511a6d5eSKristof Provost CARP_DEBUG("%s: invalid version %d\n", if_name(ifp), 7201e9e6572SGleb Smirnoff ch->carp_version); 72108b68b0eSGleb Smirnoff ifa_free(ifa); 722a9771948SGleb Smirnoff m_freem(m); 723a9771948SGleb Smirnoff return; 724a9771948SGleb Smirnoff } 725a9771948SGleb Smirnoff 72608b68b0eSGleb Smirnoff sc = ifa->ifa_carp; 72708b68b0eSGleb Smirnoff CARP_LOCK(sc); 72813781800SKristof Provost if (ifa->ifa_addr->sa_family == AF_INET) { 72913781800SKristof Provost multicast = IN_MULTICAST(sc->sc_carpaddr.s_addr); 73013781800SKristof Provost } else { 73113781800SKristof Provost multicast = IN6_IS_ADDR_MULTICAST(&sc->sc_carpaddr6); 73213781800SKristof Provost } 73308b68b0eSGleb Smirnoff ifa_free(ifa); 73408b68b0eSGleb Smirnoff 73513781800SKristof Provost /* verify that the IP TTL is 255, but only if we're not in unicast mode. */ 73613781800SKristof Provost if (multicast && ttl != CARP_DFLTTL) { 73713781800SKristof Provost CARPSTATS_INC(carps_badttl); 73813781800SKristof Provost CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__, 739511a6d5eSKristof Provost ttl, if_name(m->m_pkthdr.rcvif)); 74013781800SKristof Provost goto out; 74113781800SKristof Provost } 74213781800SKristof Provost 743a9771948SGleb Smirnoff if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) { 7446bf65bcfSRobert Watson CARPSTATS_INC(carps_badauth); 74508b68b0eSGleb Smirnoff CARP_DEBUG("%s: incorrect hash for VHID %u@%s\n", __func__, 746511a6d5eSKristof Provost sc->sc_vhid, if_name(ifp)); 74708b68b0eSGleb Smirnoff goto out; 748a9771948SGleb Smirnoff } 749a9771948SGleb Smirnoff 750a9771948SGleb Smirnoff tmp_counter = ntohl(ch->carp_counter[0]); 751a9771948SGleb Smirnoff tmp_counter = tmp_counter<<32; 752a9771948SGleb Smirnoff tmp_counter += ntohl(ch->carp_counter[1]); 753a9771948SGleb Smirnoff 754a9771948SGleb Smirnoff /* XXX Replay protection goes here */ 755a9771948SGleb Smirnoff 756a9771948SGleb Smirnoff sc->sc_init_counter = 0; 757a9771948SGleb Smirnoff sc->sc_counter = tmp_counter; 758a9771948SGleb Smirnoff 759a9771948SGleb Smirnoff sc_tv.tv_sec = sc->sc_advbase; 760f08535f8SGleb Smirnoff sc_tv.tv_usec = DEMOTE_ADVSKEW(sc) * 1000000 / 256; 761a9771948SGleb Smirnoff ch_tv.tv_sec = ch->carp_advbase; 762a9771948SGleb Smirnoff ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256; 763a9771948SGleb Smirnoff 764a9771948SGleb Smirnoff switch (sc->sc_state) { 765a9771948SGleb Smirnoff case INIT: 766a9771948SGleb Smirnoff break; 767a9771948SGleb Smirnoff case MASTER: 768a9771948SGleb Smirnoff /* 769a9771948SGleb Smirnoff * If we receive an advertisement from a master who's going to 770a9771948SGleb Smirnoff * be more frequent than us, go into BACKUP state. 771a9771948SGleb Smirnoff */ 772a9771948SGleb Smirnoff if (timevalcmp(&sc_tv, &ch_tv, >) || 773a9771948SGleb Smirnoff timevalcmp(&sc_tv, &ch_tv, ==)) { 774a9771948SGleb Smirnoff callout_stop(&sc->sc_ad_tmo); 775d01641e2SWill Andrews carp_set_state(sc, BACKUP, 776d01641e2SWill Andrews "more frequent advertisement received"); 777a9771948SGleb Smirnoff carp_setrun(sc, 0); 77808b68b0eSGleb Smirnoff carp_delroute(sc); 779a9771948SGleb Smirnoff } 780a9771948SGleb Smirnoff break; 781a9771948SGleb Smirnoff case BACKUP: 782a9771948SGleb Smirnoff /* 783a9771948SGleb Smirnoff * If we're pre-empting masters who advertise slower than us, 784a9771948SGleb Smirnoff * and this one claims to be slower, treat him as down. 785a9771948SGleb Smirnoff */ 786c5c392e7SMikolaj Golub if (V_carp_preempt && timevalcmp(&sc_tv, &ch_tv, <)) { 787d01641e2SWill Andrews carp_master_down_locked(sc, 788d01641e2SWill Andrews "preempting a slower master"); 789a9771948SGleb Smirnoff break; 790a9771948SGleb Smirnoff } 791a9771948SGleb Smirnoff 792a9771948SGleb Smirnoff /* 793a9771948SGleb Smirnoff * If the master is going to advertise at such a low frequency 794a9771948SGleb Smirnoff * that he's guaranteed to time out, we'd might as well just 795a9771948SGleb Smirnoff * treat him as timed out now. 796a9771948SGleb Smirnoff */ 797a9771948SGleb Smirnoff sc_tv.tv_sec = sc->sc_advbase * 3; 798a9771948SGleb Smirnoff if (timevalcmp(&sc_tv, &ch_tv, <)) { 799d01641e2SWill Andrews carp_master_down_locked(sc, "master will time out"); 800a9771948SGleb Smirnoff break; 801a9771948SGleb Smirnoff } 802a9771948SGleb Smirnoff 803a9771948SGleb Smirnoff /* 804a9771948SGleb Smirnoff * Otherwise, we reset the counter and wait for the next 805a9771948SGleb Smirnoff * advertisement. 806a9771948SGleb Smirnoff */ 807a9771948SGleb Smirnoff carp_setrun(sc, af); 808a9771948SGleb Smirnoff break; 809a9771948SGleb Smirnoff } 810a9771948SGleb Smirnoff 81108b68b0eSGleb Smirnoff out: 81208b68b0eSGleb Smirnoff CARP_UNLOCK(sc); 813a9771948SGleb Smirnoff m_freem(m); 814a9771948SGleb Smirnoff } 815a9771948SGleb Smirnoff 8165c1f0f6dSGleb Smirnoff static int 817a9771948SGleb Smirnoff carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch) 818a9771948SGleb Smirnoff { 819a9771948SGleb Smirnoff struct m_tag *mtag; 820a9771948SGleb Smirnoff 821a9771948SGleb Smirnoff if (sc->sc_init_counter) { 822a9771948SGleb Smirnoff /* this could also be seconds since unix epoch */ 823a9771948SGleb Smirnoff sc->sc_counter = arc4random(); 824a9771948SGleb Smirnoff sc->sc_counter = sc->sc_counter << 32; 825a9771948SGleb Smirnoff sc->sc_counter += arc4random(); 826a9771948SGleb Smirnoff } else 827a9771948SGleb Smirnoff sc->sc_counter++; 828a9771948SGleb Smirnoff 829a9771948SGleb Smirnoff ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff); 830a9771948SGleb Smirnoff ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff); 831a9771948SGleb Smirnoff 832a9771948SGleb Smirnoff carp_hmac_generate(sc, ch->carp_counter, ch->carp_md); 833a9771948SGleb Smirnoff 834a9771948SGleb Smirnoff /* Tag packet for carp_output */ 83508b68b0eSGleb Smirnoff if ((mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct carp_softc *), 83608b68b0eSGleb Smirnoff M_NOWAIT)) == NULL) { 837a9771948SGleb Smirnoff m_freem(m); 83808b68b0eSGleb Smirnoff CARPSTATS_INC(carps_onomem); 839a9771948SGleb Smirnoff return (ENOMEM); 840a9771948SGleb Smirnoff } 841eaf151c4SGleb Smirnoff bcopy(&sc, mtag + 1, sizeof(sc)); 842a9771948SGleb Smirnoff m_tag_prepend(m, mtag); 843a9771948SGleb Smirnoff 844a9771948SGleb Smirnoff return (0); 845a9771948SGleb Smirnoff } 846a9771948SGleb Smirnoff 847f08535f8SGleb Smirnoff /* 848f08535f8SGleb Smirnoff * To avoid LORs and possible recursions this function shouldn't 849f08535f8SGleb Smirnoff * be called directly, but scheduled via taskqueue. 850f08535f8SGleb Smirnoff */ 8515c1f0f6dSGleb Smirnoff static void 852f08535f8SGleb Smirnoff carp_send_ad_all(void *ctx __unused, int pending __unused) 853a9771948SGleb Smirnoff { 854d92d54d5SGleb Smirnoff struct carp_softc *sc; 8551d126e9bSKristof Provost struct epoch_tracker et; 856a9771948SGleb Smirnoff 8571d126e9bSKristof Provost NET_EPOCH_ENTER(et); 858d92d54d5SGleb Smirnoff mtx_lock(&carp_mtx); 85908b68b0eSGleb Smirnoff LIST_FOREACH(sc, &carp_list, sc_next) 860f08535f8SGleb Smirnoff if (sc->sc_state == MASTER) { 86108b68b0eSGleb Smirnoff CARP_LOCK(sc); 862afdbac98SGleb Smirnoff CURVNET_SET(sc->sc_carpdev->if_vnet); 863d220759bSGleb Smirnoff carp_send_ad_locked(sc); 864afdbac98SGleb Smirnoff CURVNET_RESTORE(); 86508b68b0eSGleb Smirnoff CARP_UNLOCK(sc); 866a9771948SGleb Smirnoff } 867d92d54d5SGleb Smirnoff mtx_unlock(&carp_mtx); 8681d126e9bSKristof Provost NET_EPOCH_EXIT(et); 869a9771948SGleb Smirnoff } 870a9771948SGleb Smirnoff 871afdbac98SGleb Smirnoff /* Send a periodic advertisement, executed in callout context. */ 8725c1f0f6dSGleb Smirnoff static void 873a9771948SGleb Smirnoff carp_send_ad(void *v) 874a9771948SGleb Smirnoff { 875d220759bSGleb Smirnoff struct carp_softc *sc = v; 8761d126e9bSKristof Provost struct epoch_tracker et; 877d220759bSGleb Smirnoff 8781d126e9bSKristof Provost NET_EPOCH_ENTER(et); 87908b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); 880afdbac98SGleb Smirnoff CURVNET_SET(sc->sc_carpdev->if_vnet); 881d220759bSGleb Smirnoff carp_send_ad_locked(sc); 882afdbac98SGleb Smirnoff CURVNET_RESTORE(); 88308b68b0eSGleb Smirnoff CARP_UNLOCK(sc); 8841d126e9bSKristof Provost NET_EPOCH_EXIT(et); 885d220759bSGleb Smirnoff } 886d220759bSGleb Smirnoff 887d220759bSGleb Smirnoff static void 8884a2dd8d4SGleb Smirnoff carp_send_ad_error(struct carp_softc *sc, int error) 8894a2dd8d4SGleb Smirnoff { 8904a2dd8d4SGleb Smirnoff 8919a8cf950SGleb Smirnoff /* 8929a8cf950SGleb Smirnoff * We track errors and successfull sends with this logic: 8939a8cf950SGleb Smirnoff * - Any error resets success counter to 0. 8949a8cf950SGleb Smirnoff * - MAX_ERRORS triggers demotion. 8959a8cf950SGleb Smirnoff * - MIN_SUCCESS successes resets error counter to 0. 8969a8cf950SGleb Smirnoff * - MIN_SUCCESS reverts demotion, if it was triggered before. 8979a8cf950SGleb Smirnoff */ 8984a2dd8d4SGleb Smirnoff if (error) { 8994a2dd8d4SGleb Smirnoff if (sc->sc_sendad_errors < INT_MAX) 9004a2dd8d4SGleb Smirnoff sc->sc_sendad_errors++; 9014a2dd8d4SGleb Smirnoff if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) { 9024a2dd8d4SGleb Smirnoff static const char fmt[] = "send error %d on %s"; 9034a2dd8d4SGleb Smirnoff char msg[sizeof(fmt) + IFNAMSIZ]; 9044a2dd8d4SGleb Smirnoff 905511a6d5eSKristof Provost sprintf(msg, fmt, error, if_name(sc->sc_carpdev)); 9064a2dd8d4SGleb Smirnoff carp_demote_adj(V_carp_senderr_adj, msg); 9074a2dd8d4SGleb Smirnoff } 9084a2dd8d4SGleb Smirnoff sc->sc_sendad_success = 0; 9099a8cf950SGleb Smirnoff } else if (sc->sc_sendad_errors > 0) { 9109a8cf950SGleb Smirnoff if (++sc->sc_sendad_success >= CARP_SENDAD_MIN_SUCCESS) { 9119a8cf950SGleb Smirnoff if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) { 9124a2dd8d4SGleb Smirnoff static const char fmt[] = "send ok on %s"; 9134a2dd8d4SGleb Smirnoff char msg[sizeof(fmt) + IFNAMSIZ]; 9144a2dd8d4SGleb Smirnoff 915511a6d5eSKristof Provost sprintf(msg, fmt, if_name(sc->sc_carpdev)); 9164a2dd8d4SGleb Smirnoff carp_demote_adj(-V_carp_senderr_adj, msg); 9179a8cf950SGleb Smirnoff } 9184a2dd8d4SGleb Smirnoff sc->sc_sendad_errors = 0; 9199a8cf950SGleb Smirnoff } 9204a2dd8d4SGleb Smirnoff } 9214a2dd8d4SGleb Smirnoff } 9224a2dd8d4SGleb Smirnoff 9238151740cSJosh Paetzel /* 9248151740cSJosh Paetzel * Pick the best ifaddr on the given ifp for sending CARP 9258151740cSJosh Paetzel * advertisements. 9268151740cSJosh Paetzel * 9278151740cSJosh Paetzel * "Best" here is defined by ifa_preferred(). This function is much 9288151740cSJosh Paetzel * much like ifaof_ifpforaddr() except that we just use ifa_preferred(). 9298151740cSJosh Paetzel * 9308151740cSJosh Paetzel * (This could be simplified to return the actual address, except that 9318151740cSJosh Paetzel * it has a different format in AF_INET and AF_INET6.) 9328151740cSJosh Paetzel */ 9338151740cSJosh Paetzel static struct ifaddr * 9348151740cSJosh Paetzel carp_best_ifa(int af, struct ifnet *ifp) 9358151740cSJosh Paetzel { 9368151740cSJosh Paetzel struct ifaddr *ifa, *best; 9378151740cSJosh Paetzel 938b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 939b8a6e03fSGleb Smirnoff 9408151740cSJosh Paetzel if (af >= AF_MAX) 9418151740cSJosh Paetzel return (NULL); 9428151740cSJosh Paetzel best = NULL; 943d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 9448151740cSJosh Paetzel if (ifa->ifa_addr->sa_family == af && 9458151740cSJosh Paetzel (best == NULL || ifa_preferred(best, ifa))) 9468151740cSJosh Paetzel best = ifa; 9478151740cSJosh Paetzel } 9488151740cSJosh Paetzel if (best != NULL) 9498151740cSJosh Paetzel ifa_ref(best); 9508151740cSJosh Paetzel return (best); 9518151740cSJosh Paetzel } 9528151740cSJosh Paetzel 9534a2dd8d4SGleb Smirnoff static void 954d220759bSGleb Smirnoff carp_send_ad_locked(struct carp_softc *sc) 955d220759bSGleb Smirnoff { 956a9771948SGleb Smirnoff struct carp_header ch; 957a9771948SGleb Smirnoff struct timeval tv; 95808b68b0eSGleb Smirnoff struct ifaddr *ifa; 959a9771948SGleb Smirnoff struct carp_header *ch_ptr; 960a9771948SGleb Smirnoff struct mbuf *m; 96108b68b0eSGleb Smirnoff int len, advskew; 962a9771948SGleb Smirnoff 9631d126e9bSKristof Provost NET_EPOCH_ASSERT(); 96408b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); 965d220759bSGleb Smirnoff 966f08535f8SGleb Smirnoff advskew = DEMOTE_ADVSKEW(sc); 96708b68b0eSGleb Smirnoff tv.tv_sec = sc->sc_advbase; 968a9771948SGleb Smirnoff tv.tv_usec = advskew * 1000000 / 256; 969a9771948SGleb Smirnoff 970a9771948SGleb Smirnoff ch.carp_version = CARP_VERSION; 971a9771948SGleb Smirnoff ch.carp_type = CARP_ADVERTISEMENT; 972a9771948SGleb Smirnoff ch.carp_vhid = sc->sc_vhid; 97308b68b0eSGleb Smirnoff ch.carp_advbase = sc->sc_advbase; 974a9771948SGleb Smirnoff ch.carp_advskew = advskew; 975a9771948SGleb Smirnoff ch.carp_authlen = 7; /* XXX DEFINE */ 976a9771948SGleb Smirnoff ch.carp_pad1 = 0; /* must be zero */ 977a9771948SGleb Smirnoff ch.carp_cksum = 0; 978a9771948SGleb Smirnoff 97908b68b0eSGleb Smirnoff /* XXXGL: OpenBSD picks first ifaddr with needed family. */ 98008b68b0eSGleb Smirnoff 981a9771948SGleb Smirnoff #ifdef INET 98208b68b0eSGleb Smirnoff if (sc->sc_naddrs) { 983a9771948SGleb Smirnoff struct ip *ip; 984a9771948SGleb Smirnoff 985dc4ad05eSGleb Smirnoff m = m_gethdr(M_NOWAIT, MT_DATA); 986a9771948SGleb Smirnoff if (m == NULL) { 9876bf65bcfSRobert Watson CARPSTATS_INC(carps_onomem); 988891122d1SGleb Smirnoff goto resched; 989a9771948SGleb Smirnoff } 990a9771948SGleb Smirnoff len = sizeof(*ip) + sizeof(ch); 991a9771948SGleb Smirnoff m->m_pkthdr.len = len; 992a9771948SGleb Smirnoff m->m_pkthdr.rcvif = NULL; 993a9771948SGleb Smirnoff m->m_len = len; 994ed6a66caSRobert Watson M_ALIGN(m, m->m_len); 99513781800SKristof Provost if (IN_MULTICAST(sc->sc_carpaddr.s_addr)) 996a9771948SGleb Smirnoff m->m_flags |= M_MCAST; 997a9771948SGleb Smirnoff ip = mtod(m, struct ip *); 998a9771948SGleb Smirnoff ip->ip_v = IPVERSION; 999a9771948SGleb Smirnoff ip->ip_hl = sizeof(*ip) >> 2; 10000d3d234cSKristof Provost ip->ip_tos = V_carp_dscp << IPTOS_DSCP_OFFSET; 10018f134647SGleb Smirnoff ip->ip_len = htons(len); 10028f134647SGleb Smirnoff ip->ip_off = htons(IP_DF); 1003a9771948SGleb Smirnoff ip->ip_ttl = CARP_DFLTTL; 1004a9771948SGleb Smirnoff ip->ip_p = IPPROTO_CARP; 1005a9771948SGleb Smirnoff ip->ip_sum = 0; 10066d947416SGleb Smirnoff ip_fillid(ip); 100708b68b0eSGleb Smirnoff 10088151740cSJosh Paetzel ifa = carp_best_ifa(AF_INET, sc->sc_carpdev); 100908b68b0eSGleb Smirnoff if (ifa != NULL) { 101008b68b0eSGleb Smirnoff ip->ip_src.s_addr = 101108b68b0eSGleb Smirnoff ifatoia(ifa)->ia_addr.sin_addr.s_addr; 101208b68b0eSGleb Smirnoff ifa_free(ifa); 101308b68b0eSGleb Smirnoff } else 101408b68b0eSGleb Smirnoff ip->ip_src.s_addr = 0; 101513781800SKristof Provost ip->ip_dst = sc->sc_carpaddr; 1016a9771948SGleb Smirnoff 1017a9771948SGleb Smirnoff ch_ptr = (struct carp_header *)(&ip[1]); 1018a9771948SGleb Smirnoff bcopy(&ch, ch_ptr, sizeof(ch)); 1019a9771948SGleb Smirnoff if (carp_prepare_ad(m, sc, ch_ptr)) 1020891122d1SGleb Smirnoff goto resched; 1021a9771948SGleb Smirnoff 1022a9771948SGleb Smirnoff m->m_data += sizeof(*ip); 1023c4d06976SGleb Smirnoff ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip)); 1024a9771948SGleb Smirnoff m->m_data -= sizeof(*ip); 1025a9771948SGleb Smirnoff 10266bf65bcfSRobert Watson CARPSTATS_INC(carps_opackets); 1027a9771948SGleb Smirnoff 10284a2dd8d4SGleb Smirnoff carp_send_ad_error(sc, ip_output(m, NULL, NULL, IP_RAWOUTPUT, 10294a2dd8d4SGleb Smirnoff &sc->sc_carpdev->if_carp->cif_imo, NULL)); 1030a9771948SGleb Smirnoff } 1031a9771948SGleb Smirnoff #endif /* INET */ 1032a9771948SGleb Smirnoff #ifdef INET6 103308b68b0eSGleb Smirnoff if (sc->sc_naddrs6) { 1034a9771948SGleb Smirnoff struct ip6_hdr *ip6; 1035a9771948SGleb Smirnoff 1036dc4ad05eSGleb Smirnoff m = m_gethdr(M_NOWAIT, MT_DATA); 1037a9771948SGleb Smirnoff if (m == NULL) { 10386bf65bcfSRobert Watson CARPSTATS_INC(carps_onomem); 1039891122d1SGleb Smirnoff goto resched; 1040a9771948SGleb Smirnoff } 1041a9771948SGleb Smirnoff len = sizeof(*ip6) + sizeof(ch); 1042a9771948SGleb Smirnoff m->m_pkthdr.len = len; 1043a9771948SGleb Smirnoff m->m_pkthdr.rcvif = NULL; 1044a9771948SGleb Smirnoff m->m_len = len; 1045ed6a66caSRobert Watson M_ALIGN(m, m->m_len); 1046a9771948SGleb Smirnoff ip6 = mtod(m, struct ip6_hdr *); 1047a9771948SGleb Smirnoff bzero(ip6, sizeof(*ip6)); 1048a9771948SGleb Smirnoff ip6->ip6_vfc |= IPV6_VERSION; 10490d3d234cSKristof Provost /* Traffic class isn't defined in ip6 struct instead 10500d3d234cSKristof Provost * it gets offset into flowid field */ 10510d3d234cSKristof Provost ip6->ip6_flow |= htonl(V_carp_dscp << (IPV6_FLOWLABEL_LEN + 10520d3d234cSKristof Provost IPTOS_DSCP_OFFSET)); 1053a9771948SGleb Smirnoff ip6->ip6_hlim = CARP_DFLTTL; 1054a9771948SGleb Smirnoff ip6->ip6_nxt = IPPROTO_CARP; 1055a9771948SGleb Smirnoff 105608b68b0eSGleb Smirnoff /* set the source address */ 10578151740cSJosh Paetzel ifa = carp_best_ifa(AF_INET6, sc->sc_carpdev); 105808b68b0eSGleb Smirnoff if (ifa != NULL) { 105908b68b0eSGleb Smirnoff bcopy(IFA_IN6(ifa), &ip6->ip6_src, 106008b68b0eSGleb Smirnoff sizeof(struct in6_addr)); 106108b68b0eSGleb Smirnoff ifa_free(ifa); 106208b68b0eSGleb Smirnoff } else 106308b68b0eSGleb Smirnoff /* This should never happen with IPv6. */ 106408b68b0eSGleb Smirnoff bzero(&ip6->ip6_src, sizeof(struct in6_addr)); 106508b68b0eSGleb Smirnoff 106608b68b0eSGleb Smirnoff /* Set the multicast destination. */ 106713781800SKristof Provost memcpy(&ip6->ip6_dst, &sc->sc_carpaddr6, sizeof(ip6->ip6_dst)); 1068c2c28c0fSKristof Provost if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1069c2c28c0fSKristof Provost IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) { 10707002145dSBjoern A. Zeeb if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) { 10717002145dSBjoern A. Zeeb m_freem(m); 1072e81ab876SGleb Smirnoff CARP_DEBUG("%s: in6_setscope failed\n", __func__); 1073891122d1SGleb Smirnoff goto resched; 10747002145dSBjoern A. Zeeb } 107513781800SKristof Provost } 1076a9771948SGleb Smirnoff 1077a9771948SGleb Smirnoff ch_ptr = (struct carp_header *)(&ip6[1]); 1078a9771948SGleb Smirnoff bcopy(&ch, ch_ptr, sizeof(ch)); 1079a9771948SGleb Smirnoff if (carp_prepare_ad(m, sc, ch_ptr)) 1080891122d1SGleb Smirnoff goto resched; 1081a9771948SGleb Smirnoff 1082a9771948SGleb Smirnoff m->m_data += sizeof(*ip6); 1083c4d06976SGleb Smirnoff ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip6)); 1084a9771948SGleb Smirnoff m->m_data -= sizeof(*ip6); 1085a9771948SGleb Smirnoff 10866bf65bcfSRobert Watson CARPSTATS_INC(carps_opackets6); 1087a9771948SGleb Smirnoff 10884a2dd8d4SGleb Smirnoff carp_send_ad_error(sc, ip6_output(m, NULL, NULL, 0, 10894a2dd8d4SGleb Smirnoff &sc->sc_carpdev->if_carp->cif_im6o, NULL, NULL)); 1090a9771948SGleb Smirnoff } 1091a9771948SGleb Smirnoff #endif /* INET6 */ 1092a9771948SGleb Smirnoff 1093891122d1SGleb Smirnoff resched: 109408b68b0eSGleb Smirnoff callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), carp_send_ad, sc); 109508b68b0eSGleb Smirnoff } 1096a9771948SGleb Smirnoff 109708b68b0eSGleb Smirnoff static void 109808b68b0eSGleb Smirnoff carp_addroute(struct carp_softc *sc) 109908b68b0eSGleb Smirnoff { 110008b68b0eSGleb Smirnoff struct ifaddr *ifa; 110108b68b0eSGleb Smirnoff 110208b68b0eSGleb Smirnoff CARP_FOREACH_IFA(sc, ifa) 11032512b096SGleb Smirnoff carp_ifa_addroute(ifa); 11042512b096SGleb Smirnoff } 11052512b096SGleb Smirnoff 11062512b096SGleb Smirnoff static void 11072512b096SGleb Smirnoff carp_ifa_addroute(struct ifaddr *ifa) 11082512b096SGleb Smirnoff { 11092512b096SGleb Smirnoff 111008b68b0eSGleb Smirnoff switch (ifa->ifa_addr->sa_family) { 111108b68b0eSGleb Smirnoff #ifdef INET 111208b68b0eSGleb Smirnoff case AF_INET: 1113130aebbaSAlexander V. Chernikov in_addprefix(ifatoia(ifa)); 111408b68b0eSGleb Smirnoff ifa_add_loopback_route(ifa, 111508b68b0eSGleb Smirnoff (struct sockaddr *)&ifatoia(ifa)->ia_addr); 111608b68b0eSGleb Smirnoff break; 111708b68b0eSGleb Smirnoff #endif 111808b68b0eSGleb Smirnoff #ifdef INET6 111908b68b0eSGleb Smirnoff case AF_INET6: 112008b68b0eSGleb Smirnoff ifa_add_loopback_route(ifa, 112108b68b0eSGleb Smirnoff (struct sockaddr *)&ifatoia6(ifa)->ia_addr); 1122f6b84910SAlexander V. Chernikov nd6_add_ifa_lle(ifatoia6(ifa)); 112308b68b0eSGleb Smirnoff break; 112408b68b0eSGleb Smirnoff #endif 112508b68b0eSGleb Smirnoff } 112608b68b0eSGleb Smirnoff } 112708b68b0eSGleb Smirnoff 112808b68b0eSGleb Smirnoff static void 112908b68b0eSGleb Smirnoff carp_delroute(struct carp_softc *sc) 113008b68b0eSGleb Smirnoff { 113108b68b0eSGleb Smirnoff struct ifaddr *ifa; 113208b68b0eSGleb Smirnoff 113308b68b0eSGleb Smirnoff CARP_FOREACH_IFA(sc, ifa) 11342512b096SGleb Smirnoff carp_ifa_delroute(ifa); 11352512b096SGleb Smirnoff } 11362512b096SGleb Smirnoff 11372512b096SGleb Smirnoff static void 11382512b096SGleb Smirnoff carp_ifa_delroute(struct ifaddr *ifa) 11392512b096SGleb Smirnoff { 11402512b096SGleb Smirnoff 114108b68b0eSGleb Smirnoff switch (ifa->ifa_addr->sa_family) { 114208b68b0eSGleb Smirnoff #ifdef INET 114308b68b0eSGleb Smirnoff case AF_INET: 114408b68b0eSGleb Smirnoff ifa_del_loopback_route(ifa, 114508b68b0eSGleb Smirnoff (struct sockaddr *)&ifatoia(ifa)->ia_addr); 114608b68b0eSGleb Smirnoff in_scrubprefix(ifatoia(ifa), LLE_STATIC); 114708b68b0eSGleb Smirnoff break; 114808b68b0eSGleb Smirnoff #endif 114908b68b0eSGleb Smirnoff #ifdef INET6 115008b68b0eSGleb Smirnoff case AF_INET6: 115108b68b0eSGleb Smirnoff ifa_del_loopback_route(ifa, 115208b68b0eSGleb Smirnoff (struct sockaddr *)&ifatoia6(ifa)->ia_addr); 11533e7a2321SAlexander V. Chernikov nd6_rem_ifa_lle(ifatoia6(ifa), 1); 115408b68b0eSGleb Smirnoff break; 115508b68b0eSGleb Smirnoff #endif 115608b68b0eSGleb Smirnoff } 1157a9771948SGleb Smirnoff } 1158a9771948SGleb Smirnoff 115924421c1cSGleb Smirnoff int 116024421c1cSGleb Smirnoff carp_master(struct ifaddr *ifa) 116124421c1cSGleb Smirnoff { 116224421c1cSGleb Smirnoff struct carp_softc *sc = ifa->ifa_carp; 116324421c1cSGleb Smirnoff 116424421c1cSGleb Smirnoff return (sc->sc_state == MASTER); 116524421c1cSGleb Smirnoff } 116624421c1cSGleb Smirnoff 1167a0ae8f04SBjoern A. Zeeb #ifdef INET 1168a9771948SGleb Smirnoff /* 1169a9771948SGleb Smirnoff * Broadcast a gratuitous ARP request containing 1170a9771948SGleb Smirnoff * the virtual router MAC address for each IP address 1171a9771948SGleb Smirnoff * associated with the virtual router. 1172a9771948SGleb Smirnoff */ 11735c1f0f6dSGleb Smirnoff static void 1174a9771948SGleb Smirnoff carp_send_arp(struct carp_softc *sc) 1175a9771948SGleb Smirnoff { 1176a9771948SGleb Smirnoff struct ifaddr *ifa; 1177d6e82913SSteven Hartland struct in_addr addr; 1178a9771948SGleb Smirnoff 11791d126e9bSKristof Provost NET_EPOCH_ASSERT(); 11801d126e9bSKristof Provost 11811c302b58SAlexander V. Chernikov CARP_FOREACH_IFA(sc, ifa) { 11821c302b58SAlexander V. Chernikov if (ifa->ifa_addr->sa_family != AF_INET) 11831c302b58SAlexander V. Chernikov continue; 1184d6e82913SSteven Hartland addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr; 1185d6e82913SSteven Hartland arp_announce_ifaddr(sc->sc_carpdev, addr, LLADDR(&sc->sc_addr)); 11861c302b58SAlexander V. Chernikov } 1187a9771948SGleb Smirnoff } 118808b68b0eSGleb Smirnoff 118908b68b0eSGleb Smirnoff int 119008b68b0eSGleb Smirnoff carp_iamatch(struct ifaddr *ifa, uint8_t **enaddr) 119108b68b0eSGleb Smirnoff { 119208b68b0eSGleb Smirnoff struct carp_softc *sc = ifa->ifa_carp; 119308b68b0eSGleb Smirnoff 119408b68b0eSGleb Smirnoff if (sc->sc_state == MASTER) { 119508b68b0eSGleb Smirnoff *enaddr = LLADDR(&sc->sc_addr); 119608b68b0eSGleb Smirnoff return (1); 119708b68b0eSGleb Smirnoff } 119808b68b0eSGleb Smirnoff 119908b68b0eSGleb Smirnoff return (0); 1200a9771948SGleb Smirnoff } 1201a0ae8f04SBjoern A. Zeeb #endif 1202a9771948SGleb Smirnoff 1203a9771948SGleb Smirnoff #ifdef INET6 12045c1f0f6dSGleb Smirnoff static void 1205a9771948SGleb Smirnoff carp_send_na(struct carp_softc *sc) 1206a9771948SGleb Smirnoff { 1207d6e82913SSteven Hartland static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT; 1208a9771948SGleb Smirnoff struct ifaddr *ifa; 1209d6e82913SSteven Hartland struct in6_addr *in6; 1210a9771948SGleb Smirnoff 121108b68b0eSGleb Smirnoff CARP_FOREACH_IFA(sc, ifa) { 1212d6e82913SSteven Hartland if (ifa->ifa_addr->sa_family != AF_INET6) 1213a9771948SGleb Smirnoff continue; 1214a9771948SGleb Smirnoff 1215d6e82913SSteven Hartland in6 = IFA_IN6(ifa); 1216d6e82913SSteven Hartland nd6_na_output(sc->sc_carpdev, &mcast, in6, 1217d6e82913SSteven Hartland ND_NA_FLAG_OVERRIDE, 1, NULL); 1218d6e82913SSteven Hartland DELAY(1000); /* XXX */ 1219a9771948SGleb Smirnoff } 1220a9771948SGleb Smirnoff } 1221a9771948SGleb Smirnoff 12228253dcabSBjoern A. Zeeb /* 12238253dcabSBjoern A. Zeeb * Returns ifa in case it's a carp address and it is MASTER, or if the address 12248253dcabSBjoern A. Zeeb * matches and is not a carp address. Returns NULL otherwise. 12258253dcabSBjoern A. Zeeb */ 1226a4e53905SMax Laier struct ifaddr * 122754bfbd51SWill Andrews carp_iamatch6(struct ifnet *ifp, struct in6_addr *taddr) 1228a9771948SGleb Smirnoff { 1229a9771948SGleb Smirnoff struct ifaddr *ifa; 1230a9771948SGleb Smirnoff 1231b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 1232b8a6e03fSGleb Smirnoff 12338253dcabSBjoern A. Zeeb ifa = NULL; 1234d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 12358253dcabSBjoern A. Zeeb if (ifa->ifa_addr->sa_family != AF_INET6) 12368253dcabSBjoern A. Zeeb continue; 12378253dcabSBjoern A. Zeeb if (!IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) 12388253dcabSBjoern A. Zeeb continue; 12398253dcabSBjoern A. Zeeb if (ifa->ifa_carp && ifa->ifa_carp->sc_state != MASTER) 12408253dcabSBjoern A. Zeeb ifa = NULL; 12418253dcabSBjoern A. Zeeb else 12428c0fec80SRobert Watson ifa_ref(ifa); 12438253dcabSBjoern A. Zeeb break; 1244a9771948SGleb Smirnoff } 1245a9771948SGleb Smirnoff 12468253dcabSBjoern A. Zeeb return (ifa); 1247a9771948SGleb Smirnoff } 1248a9771948SGleb Smirnoff 1249dad68fc3SBjoern A. Zeeb char * 125054bfbd51SWill Andrews carp_macmatch6(struct ifnet *ifp, struct mbuf *m, const struct in6_addr *taddr) 1251a9771948SGleb Smirnoff { 1252a9771948SGleb Smirnoff struct ifaddr *ifa; 1253a9771948SGleb Smirnoff 1254b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 1255b8a6e03fSGleb Smirnoff 125608b68b0eSGleb Smirnoff IFNET_FOREACH_IFA(ifp, ifa) 125708b68b0eSGleb Smirnoff if (ifa->ifa_addr->sa_family == AF_INET6 && 125808b68b0eSGleb Smirnoff IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) { 125908b68b0eSGleb Smirnoff struct carp_softc *sc = ifa->ifa_carp; 126008b68b0eSGleb Smirnoff struct m_tag *mtag; 126108b68b0eSGleb Smirnoff 1262a9771948SGleb Smirnoff mtag = m_tag_get(PACKET_TAG_CARP, 1263a856ddc6SGleb Smirnoff sizeof(struct carp_softc *), M_NOWAIT); 126408b68b0eSGleb Smirnoff if (mtag == NULL) 126508b68b0eSGleb Smirnoff /* Better a bit than nothing. */ 126608b68b0eSGleb Smirnoff return (LLADDR(&sc->sc_addr)); 126708b68b0eSGleb Smirnoff 1268eaf151c4SGleb Smirnoff bcopy(&sc, mtag + 1, sizeof(sc)); 1269a9771948SGleb Smirnoff m_tag_prepend(m, mtag); 1270a9771948SGleb Smirnoff 127108b68b0eSGleb Smirnoff return (LLADDR(&sc->sc_addr)); 1272a9771948SGleb Smirnoff } 1273a9771948SGleb Smirnoff 1274a9771948SGleb Smirnoff return (NULL); 1275a9771948SGleb Smirnoff } 127608b68b0eSGleb Smirnoff #endif /* INET6 */ 1277a9771948SGleb Smirnoff 127808b68b0eSGleb Smirnoff int 127954bfbd51SWill Andrews carp_forus(struct ifnet *ifp, u_char *dhost) 1280a9771948SGleb Smirnoff { 128108b68b0eSGleb Smirnoff struct carp_softc *sc; 128208b68b0eSGleb Smirnoff uint8_t *ena = dhost; 1283a9771948SGleb Smirnoff 1284a9771948SGleb Smirnoff if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1) 128508b68b0eSGleb Smirnoff return (0); 1286a9771948SGleb Smirnoff 128708b68b0eSGleb Smirnoff CIF_LOCK(ifp->if_carp); 128808b68b0eSGleb Smirnoff IFNET_FOREACH_CARP(ifp, sc) { 12898c3fbf3cSAlexander Motin /* 12908c3fbf3cSAlexander Motin * CARP_LOCK() is not here, since would protect nothing, but 12918c3fbf3cSAlexander Motin * cause deadlock with if_bridge, calling this under its lock. 12928c3fbf3cSAlexander Motin */ 129308b68b0eSGleb Smirnoff if (sc->sc_state == MASTER && !bcmp(dhost, LLADDR(&sc->sc_addr), 129408b68b0eSGleb Smirnoff ETHER_ADDR_LEN)) { 129508b68b0eSGleb Smirnoff CIF_UNLOCK(ifp->if_carp); 129608b68b0eSGleb Smirnoff return (1); 1297a9771948SGleb Smirnoff } 129808b68b0eSGleb Smirnoff } 129908b68b0eSGleb Smirnoff CIF_UNLOCK(ifp->if_carp); 1300a9771948SGleb Smirnoff 130108b68b0eSGleb Smirnoff return (0); 1302a9771948SGleb Smirnoff } 1303a9771948SGleb Smirnoff 1304afdbac98SGleb Smirnoff /* Master down timeout event, executed in callout context. */ 13055c1f0f6dSGleb Smirnoff static void 1306a9771948SGleb Smirnoff carp_master_down(void *v) 1307a9771948SGleb Smirnoff { 1308a9771948SGleb Smirnoff struct carp_softc *sc = v; 13091d126e9bSKristof Provost struct epoch_tracker et; 1310a9771948SGleb Smirnoff 13111d126e9bSKristof Provost NET_EPOCH_ENTER(et); 131208b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); 131308b68b0eSGleb Smirnoff 1314afdbac98SGleb Smirnoff CURVNET_SET(sc->sc_carpdev->if_vnet); 131508b68b0eSGleb Smirnoff if (sc->sc_state == BACKUP) { 1316d01641e2SWill Andrews carp_master_down_locked(sc, "master timed out"); 131708b68b0eSGleb Smirnoff } 1318afdbac98SGleb Smirnoff CURVNET_RESTORE(); 131908b68b0eSGleb Smirnoff 132008b68b0eSGleb Smirnoff CARP_UNLOCK(sc); 13211d126e9bSKristof Provost NET_EPOCH_EXIT(et); 1322d220759bSGleb Smirnoff } 1323d220759bSGleb Smirnoff 1324d220759bSGleb Smirnoff static void 1325d01641e2SWill Andrews carp_master_down_locked(struct carp_softc *sc, const char *reason) 1326d220759bSGleb Smirnoff { 132708b68b0eSGleb Smirnoff 13281d126e9bSKristof Provost NET_EPOCH_ASSERT(); 132908b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); 1330d220759bSGleb Smirnoff 1331a9771948SGleb Smirnoff switch (sc->sc_state) { 1332a9771948SGleb Smirnoff case BACKUP: 1333d01641e2SWill Andrews carp_set_state(sc, MASTER, reason); 1334d220759bSGleb Smirnoff carp_send_ad_locked(sc); 1335a0ae8f04SBjoern A. Zeeb #ifdef INET 1336a9771948SGleb Smirnoff carp_send_arp(sc); 1337a0ae8f04SBjoern A. Zeeb #endif 1338a9771948SGleb Smirnoff #ifdef INET6 1339a9771948SGleb Smirnoff carp_send_na(sc); 134008b68b0eSGleb Smirnoff #endif 1341a9771948SGleb Smirnoff carp_setrun(sc, 0); 134208b68b0eSGleb Smirnoff carp_addroute(sc); 134308b68b0eSGleb Smirnoff break; 134408b68b0eSGleb Smirnoff case INIT: 134508b68b0eSGleb Smirnoff case MASTER: 134608b68b0eSGleb Smirnoff #ifdef INVARIANTS 134708b68b0eSGleb Smirnoff panic("carp: VHID %u@%s: master_down event in %s state\n", 134808b68b0eSGleb Smirnoff sc->sc_vhid, 1349511a6d5eSKristof Provost if_name(sc->sc_carpdev), 135008b68b0eSGleb Smirnoff sc->sc_state ? "MASTER" : "INIT"); 135108b68b0eSGleb Smirnoff #endif 1352a9771948SGleb Smirnoff break; 1353a9771948SGleb Smirnoff } 1354a9771948SGleb Smirnoff } 1355a9771948SGleb Smirnoff 1356a9771948SGleb Smirnoff /* 1357a9771948SGleb Smirnoff * When in backup state, af indicates whether to reset the master down timer 1358a9771948SGleb Smirnoff * for v4 or v6. If it's set to zero, reset the ones which are already pending. 1359a9771948SGleb Smirnoff */ 13605c1f0f6dSGleb Smirnoff static void 1361a9771948SGleb Smirnoff carp_setrun(struct carp_softc *sc, sa_family_t af) 1362a9771948SGleb Smirnoff { 1363a9771948SGleb Smirnoff struct timeval tv; 1364a9771948SGleb Smirnoff 136508b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); 1366d220759bSGleb Smirnoff 136708b68b0eSGleb Smirnoff if ((sc->sc_carpdev->if_flags & IFF_UP) == 0 || 136808b68b0eSGleb Smirnoff sc->sc_carpdev->if_link_state != LINK_STATE_UP || 1369167a3440SAlexander Motin (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) || 1370167a3440SAlexander Motin !V_carp_allow) 1371a9771948SGleb Smirnoff return; 1372a9771948SGleb Smirnoff 1373a9771948SGleb Smirnoff switch (sc->sc_state) { 1374a9771948SGleb Smirnoff case INIT: 1375d01641e2SWill Andrews carp_set_state(sc, BACKUP, "initialization complete"); 1376a9771948SGleb Smirnoff carp_setrun(sc, 0); 1377a9771948SGleb Smirnoff break; 1378a9771948SGleb Smirnoff case BACKUP: 1379a9771948SGleb Smirnoff callout_stop(&sc->sc_ad_tmo); 1380a9771948SGleb Smirnoff tv.tv_sec = 3 * sc->sc_advbase; 1381a9771948SGleb Smirnoff tv.tv_usec = sc->sc_advskew * 1000000 / 256; 1382a9771948SGleb Smirnoff switch (af) { 1383a9771948SGleb Smirnoff #ifdef INET 1384a9771948SGleb Smirnoff case AF_INET: 1385a9771948SGleb Smirnoff callout_reset(&sc->sc_md_tmo, tvtohz(&tv), 1386a9771948SGleb Smirnoff carp_master_down, sc); 1387a9771948SGleb Smirnoff break; 138808b68b0eSGleb Smirnoff #endif 1389a9771948SGleb Smirnoff #ifdef INET6 1390a9771948SGleb Smirnoff case AF_INET6: 1391a9771948SGleb Smirnoff callout_reset(&sc->sc_md6_tmo, tvtohz(&tv), 1392a9771948SGleb Smirnoff carp_master_down, sc); 1393a9771948SGleb Smirnoff break; 139408b68b0eSGleb Smirnoff #endif 1395a9771948SGleb Smirnoff default: 139608b68b0eSGleb Smirnoff #ifdef INET 1397a9771948SGleb Smirnoff if (sc->sc_naddrs) 1398a9771948SGleb Smirnoff callout_reset(&sc->sc_md_tmo, tvtohz(&tv), 1399a9771948SGleb Smirnoff carp_master_down, sc); 140008b68b0eSGleb Smirnoff #endif 140108b68b0eSGleb Smirnoff #ifdef INET6 1402a9771948SGleb Smirnoff if (sc->sc_naddrs6) 1403a9771948SGleb Smirnoff callout_reset(&sc->sc_md6_tmo, tvtohz(&tv), 1404a9771948SGleb Smirnoff carp_master_down, sc); 140508b68b0eSGleb Smirnoff #endif 1406a9771948SGleb Smirnoff break; 1407a9771948SGleb Smirnoff } 1408a9771948SGleb Smirnoff break; 1409a9771948SGleb Smirnoff case MASTER: 1410a9771948SGleb Smirnoff tv.tv_sec = sc->sc_advbase; 1411a9771948SGleb Smirnoff tv.tv_usec = sc->sc_advskew * 1000000 / 256; 1412a9771948SGleb Smirnoff callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), 1413a9771948SGleb Smirnoff carp_send_ad, sc); 1414a9771948SGleb Smirnoff break; 1415a9771948SGleb Smirnoff } 1416a9771948SGleb Smirnoff } 1417a9771948SGleb Smirnoff 141808b68b0eSGleb Smirnoff /* 141908b68b0eSGleb Smirnoff * Setup multicast structures. 142008b68b0eSGleb Smirnoff */ 14215c1f0f6dSGleb Smirnoff static int 1422a9a2c40cSGleb Smirnoff carp_multicast_setup(struct carp_if *cif, sa_family_t sa) 1423a9771948SGleb Smirnoff { 1424a9a2c40cSGleb Smirnoff struct ifnet *ifp = cif->cif_ifp; 142508b68b0eSGleb Smirnoff int error = 0; 142608b68b0eSGleb Smirnoff 142708b68b0eSGleb Smirnoff switch (sa) { 142808b68b0eSGleb Smirnoff #ifdef INET 142908b68b0eSGleb Smirnoff case AF_INET: 143008b68b0eSGleb Smirnoff { 143108b68b0eSGleb Smirnoff struct ip_moptions *imo = &cif->cif_imo; 143259854ecfSHans Petter Selasky struct in_mfilter *imf; 1433a9771948SGleb Smirnoff struct in_addr addr; 1434a9771948SGleb Smirnoff 143559854ecfSHans Petter Selasky if (ip_mfilter_first(&imo->imo_head) != NULL) 1436a9771948SGleb Smirnoff return (0); 1437a9771948SGleb Smirnoff 143859854ecfSHans Petter Selasky imf = ip_mfilter_alloc(M_WAITOK, 0, 0); 143959854ecfSHans Petter Selasky ip_mfilter_init(&imo->imo_head); 144008b68b0eSGleb Smirnoff imo->imo_multicast_vif = -1; 1441a9771948SGleb Smirnoff 1442a9771948SGleb Smirnoff addr.s_addr = htonl(INADDR_CARP_GROUP); 144308b68b0eSGleb Smirnoff if ((error = in_joingroup(ifp, &addr, NULL, 144459854ecfSHans Petter Selasky &imf->imf_inm)) != 0) { 144559854ecfSHans Petter Selasky ip_mfilter_free(imf); 144608b68b0eSGleb Smirnoff break; 14472d9cfabaSRobert Watson } 144859854ecfSHans Petter Selasky 144959854ecfSHans Petter Selasky ip_mfilter_insert(&imo->imo_head, imf); 1450a9771948SGleb Smirnoff imo->imo_multicast_ifp = ifp; 1451a9771948SGleb Smirnoff imo->imo_multicast_ttl = CARP_DFLTTL; 1452a9771948SGleb Smirnoff imo->imo_multicast_loop = 0; 1453a9771948SGleb Smirnoff break; 1454a9771948SGleb Smirnoff } 145508b68b0eSGleb Smirnoff #endif 145608b68b0eSGleb Smirnoff #ifdef INET6 145708b68b0eSGleb Smirnoff case AF_INET6: 145808b68b0eSGleb Smirnoff { 145908b68b0eSGleb Smirnoff struct ip6_moptions *im6o = &cif->cif_im6o; 146059854ecfSHans Petter Selasky struct in6_mfilter *im6f[2]; 146108b68b0eSGleb Smirnoff struct in6_addr in6; 146233cde130SBruce M Simpson 146359854ecfSHans Petter Selasky if (ip6_mfilter_first(&im6o->im6o_head)) 146408b68b0eSGleb Smirnoff return (0); 146508b68b0eSGleb Smirnoff 146659854ecfSHans Petter Selasky im6f[0] = ip6_mfilter_alloc(M_WAITOK, 0, 0); 146759854ecfSHans Petter Selasky im6f[1] = ip6_mfilter_alloc(M_WAITOK, 0, 0); 146859854ecfSHans Petter Selasky 146959854ecfSHans Petter Selasky ip6_mfilter_init(&im6o->im6o_head); 147008b68b0eSGleb Smirnoff im6o->im6o_multicast_hlim = CARP_DFLTTL; 1471a9771948SGleb Smirnoff im6o->im6o_multicast_ifp = ifp; 1472a9771948SGleb Smirnoff 147308b68b0eSGleb Smirnoff /* Join IPv6 CARP multicast group. */ 1474a1f7e5f8SHajimu UMEMOTO bzero(&in6, sizeof(in6)); 1475a1f7e5f8SHajimu UMEMOTO in6.s6_addr16[0] = htons(0xff02); 1476a1f7e5f8SHajimu UMEMOTO in6.s6_addr8[15] = 0x12; 147708b68b0eSGleb Smirnoff if ((error = in6_setscope(&in6, ifp, NULL)) != 0) { 147859854ecfSHans Petter Selasky ip6_mfilter_free(im6f[0]); 147959854ecfSHans Petter Selasky ip6_mfilter_free(im6f[1]); 148008b68b0eSGleb Smirnoff break; 148108b68b0eSGleb Smirnoff } 148259854ecfSHans Petter Selasky if ((error = in6_joingroup(ifp, &in6, NULL, &im6f[0]->im6f_in6m, 0)) != 0) { 148359854ecfSHans Petter Selasky ip6_mfilter_free(im6f[0]); 148459854ecfSHans Petter Selasky ip6_mfilter_free(im6f[1]); 148508b68b0eSGleb Smirnoff break; 148608b68b0eSGleb Smirnoff } 1487a9771948SGleb Smirnoff 148808b68b0eSGleb Smirnoff /* Join solicited multicast address. */ 1489a1f7e5f8SHajimu UMEMOTO bzero(&in6, sizeof(in6)); 1490a1f7e5f8SHajimu UMEMOTO in6.s6_addr16[0] = htons(0xff02); 1491a1f7e5f8SHajimu UMEMOTO in6.s6_addr32[1] = 0; 1492a1f7e5f8SHajimu UMEMOTO in6.s6_addr32[2] = htonl(1); 149308b68b0eSGleb Smirnoff in6.s6_addr32[3] = 0; 1494a1f7e5f8SHajimu UMEMOTO in6.s6_addr8[12] = 0xff; 149559854ecfSHans Petter Selasky 149608b68b0eSGleb Smirnoff if ((error = in6_setscope(&in6, ifp, NULL)) != 0) { 149759854ecfSHans Petter Selasky ip6_mfilter_free(im6f[0]); 149859854ecfSHans Petter Selasky ip6_mfilter_free(im6f[1]); 149908b68b0eSGleb Smirnoff break; 150008b68b0eSGleb Smirnoff } 150159854ecfSHans Petter Selasky 150259854ecfSHans Petter Selasky if ((error = in6_joingroup(ifp, &in6, NULL, &im6f[1]->im6f_in6m, 0)) != 0) { 150359854ecfSHans Petter Selasky in6_leavegroup(im6f[0]->im6f_in6m, NULL); 150459854ecfSHans Petter Selasky ip6_mfilter_free(im6f[0]); 150559854ecfSHans Petter Selasky ip6_mfilter_free(im6f[1]); 150608b68b0eSGleb Smirnoff break; 150708b68b0eSGleb Smirnoff } 150859854ecfSHans Petter Selasky ip6_mfilter_insert(&im6o->im6o_head, im6f[0]); 150959854ecfSHans Petter Selasky ip6_mfilter_insert(&im6o->im6o_head, im6f[1]); 1510a9771948SGleb Smirnoff break; 1511a9771948SGleb Smirnoff } 1512a9771948SGleb Smirnoff #endif 151308b68b0eSGleb Smirnoff } 151408b68b0eSGleb Smirnoff 151508b68b0eSGleb Smirnoff return (error); 1516a9771948SGleb Smirnoff } 1517a9771948SGleb Smirnoff 1518a9771948SGleb Smirnoff /* 151908b68b0eSGleb Smirnoff * Free multicast structures. 1520a9771948SGleb Smirnoff */ 15215c1f0f6dSGleb Smirnoff static void 1522a9a2c40cSGleb Smirnoff carp_multicast_cleanup(struct carp_if *cif, sa_family_t sa) 1523a9771948SGleb Smirnoff { 152459854ecfSHans Petter Selasky #ifdef INET 152559854ecfSHans Petter Selasky struct ip_moptions *imo = &cif->cif_imo; 152659854ecfSHans Petter Selasky struct in_mfilter *imf; 152759854ecfSHans Petter Selasky #endif 152859854ecfSHans Petter Selasky #ifdef INET6 152959854ecfSHans Petter Selasky struct ip6_moptions *im6o = &cif->cif_im6o; 153059854ecfSHans Petter Selasky struct in6_mfilter *im6f; 153159854ecfSHans Petter Selasky #endif 15329c2cd1aaSGleb Smirnoff sx_assert(&carp_sx, SA_XLOCKED); 15339c2cd1aaSGleb Smirnoff 153408b68b0eSGleb Smirnoff switch (sa) { 153508b68b0eSGleb Smirnoff #ifdef INET 153608b68b0eSGleb Smirnoff case AF_INET: 153759854ecfSHans Petter Selasky if (cif->cif_naddrs != 0) 153859854ecfSHans Petter Selasky break; 153908b68b0eSGleb Smirnoff 154059854ecfSHans Petter Selasky while ((imf = ip_mfilter_first(&imo->imo_head)) != NULL) { 154159854ecfSHans Petter Selasky ip_mfilter_remove(&imo->imo_head, imf); 154259854ecfSHans Petter Selasky in_leavegroup(imf->imf_inm, NULL); 154359854ecfSHans Petter Selasky ip_mfilter_free(imf); 154408b68b0eSGleb Smirnoff } 154508b68b0eSGleb Smirnoff break; 1546a9771948SGleb Smirnoff #endif 154708b68b0eSGleb Smirnoff #ifdef INET6 154808b68b0eSGleb Smirnoff case AF_INET6: 154959854ecfSHans Petter Selasky if (cif->cif_naddrs6 != 0) 155059854ecfSHans Petter Selasky break; 155108b68b0eSGleb Smirnoff 155259854ecfSHans Petter Selasky while ((im6f = ip6_mfilter_first(&im6o->im6o_head)) != NULL) { 155359854ecfSHans Petter Selasky ip6_mfilter_remove(&im6o->im6o_head, im6f); 155459854ecfSHans Petter Selasky in6_leavegroup(im6f->im6f_in6m, NULL); 155559854ecfSHans Petter Selasky ip6_mfilter_free(im6f); 155608b68b0eSGleb Smirnoff } 155708b68b0eSGleb Smirnoff break; 155808b68b0eSGleb Smirnoff #endif 155908b68b0eSGleb Smirnoff } 1560a9771948SGleb Smirnoff } 1561a9771948SGleb Smirnoff 1562a9771948SGleb Smirnoff int 156347e8d432SGleb Smirnoff carp_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa) 1564a9771948SGleb Smirnoff { 1565a9771948SGleb Smirnoff struct m_tag *mtag; 1566a9771948SGleb Smirnoff struct carp_softc *sc; 1567a9771948SGleb Smirnoff 1568a9771948SGleb Smirnoff if (!sa) 1569a9771948SGleb Smirnoff return (0); 1570a9771948SGleb Smirnoff 1571a9771948SGleb Smirnoff switch (sa->sa_family) { 1572a9771948SGleb Smirnoff #ifdef INET 1573a9771948SGleb Smirnoff case AF_INET: 1574a9771948SGleb Smirnoff break; 157508b68b0eSGleb Smirnoff #endif 1576a9771948SGleb Smirnoff #ifdef INET6 1577a9771948SGleb Smirnoff case AF_INET6: 1578a9771948SGleb Smirnoff break; 157908b68b0eSGleb Smirnoff #endif 1580a9771948SGleb Smirnoff default: 1581a9771948SGleb Smirnoff return (0); 1582a9771948SGleb Smirnoff } 1583a9771948SGleb Smirnoff 1584a9771948SGleb Smirnoff mtag = m_tag_find(m, PACKET_TAG_CARP, NULL); 1585a9771948SGleb Smirnoff if (mtag == NULL) 1586a9771948SGleb Smirnoff return (0); 1587a9771948SGleb Smirnoff 1588eaf151c4SGleb Smirnoff bcopy(mtag + 1, &sc, sizeof(sc)); 1589a9771948SGleb Smirnoff 159013781800SKristof Provost switch (sa->sa_family) { 159113781800SKristof Provost case AF_INET: 1592ccff2078SKristof Provost if (! IN_MULTICAST(ntohl(sc->sc_carpaddr.s_addr))) 159313781800SKristof Provost return (0); 159413781800SKristof Provost break; 159513781800SKristof Provost case AF_INET6: 159613781800SKristof Provost if (! IN6_IS_ADDR_MULTICAST(&sc->sc_carpaddr6)) 159713781800SKristof Provost return (0); 159813781800SKristof Provost break; 159913781800SKristof Provost default: 160013781800SKristof Provost panic("Unknown af"); 160113781800SKristof Provost } 160213781800SKristof Provost 160308b68b0eSGleb Smirnoff /* Set the source MAC address to the Virtual Router MAC Address. */ 1604a9771948SGleb Smirnoff switch (ifp->if_type) { 1605630481bbSYaroslav Tykhiy case IFT_ETHER: 16069ca1fe0dSGleb Smirnoff case IFT_BRIDGE: 1607630481bbSYaroslav Tykhiy case IFT_L2VLAN: { 1608a9771948SGleb Smirnoff struct ether_header *eh; 1609a9771948SGleb Smirnoff 1610a9771948SGleb Smirnoff eh = mtod(m, struct ether_header *); 1611a9771948SGleb Smirnoff eh->ether_shost[0] = 0; 1612a9771948SGleb Smirnoff eh->ether_shost[1] = 0; 1613a9771948SGleb Smirnoff eh->ether_shost[2] = 0x5e; 1614a9771948SGleb Smirnoff eh->ether_shost[3] = 0; 1615a9771948SGleb Smirnoff eh->ether_shost[4] = 1; 1616a9771948SGleb Smirnoff eh->ether_shost[5] = sc->sc_vhid; 1617a9771948SGleb Smirnoff } 1618a9771948SGleb Smirnoff break; 1619a9771948SGleb Smirnoff default: 162008b68b0eSGleb Smirnoff printf("%s: carp is not supported for the %d interface type\n", 1621511a6d5eSKristof Provost if_name(ifp), ifp->if_type); 1622a9771948SGleb Smirnoff return (EOPNOTSUPP); 1623a9771948SGleb Smirnoff } 1624a9771948SGleb Smirnoff 1625a9771948SGleb Smirnoff return (0); 1626a9771948SGleb Smirnoff } 1627a9771948SGleb Smirnoff 162808b68b0eSGleb Smirnoff static struct carp_softc* 162908b68b0eSGleb Smirnoff carp_alloc(struct ifnet *ifp) 1630a9771948SGleb Smirnoff { 163108b68b0eSGleb Smirnoff struct carp_softc *sc; 163208b68b0eSGleb Smirnoff struct carp_if *cif; 1633d220759bSGleb Smirnoff 163481098a01SAlexander Motin sx_assert(&carp_sx, SA_XLOCKED); 163581098a01SAlexander Motin 16360cc726f2SGleb Smirnoff if ((cif = ifp->if_carp) == NULL) 163708b68b0eSGleb Smirnoff cif = carp_alloc_if(ifp); 1638a9771948SGleb Smirnoff 163908b68b0eSGleb Smirnoff sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO); 164008b68b0eSGleb Smirnoff 164108b68b0eSGleb Smirnoff sc->sc_advbase = CARP_DFLTINTV; 164208b68b0eSGleb Smirnoff sc->sc_vhid = -1; /* required setting */ 164308b68b0eSGleb Smirnoff sc->sc_init_counter = 1; 164408b68b0eSGleb Smirnoff sc->sc_state = INIT; 164508b68b0eSGleb Smirnoff 164608b68b0eSGleb Smirnoff sc->sc_ifasiz = sizeof(struct ifaddr *); 164708b68b0eSGleb Smirnoff sc->sc_ifas = malloc(sc->sc_ifasiz, M_CARP, M_WAITOK|M_ZERO); 164808b68b0eSGleb Smirnoff sc->sc_carpdev = ifp; 164908b68b0eSGleb Smirnoff 165013781800SKristof Provost sc->sc_carpaddr.s_addr = htonl(INADDR_CARP_GROUP); 165113781800SKristof Provost sc->sc_carpaddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL; 165213781800SKristof Provost sc->sc_carpaddr6.s6_addr8[15] = 0x12; 165313781800SKristof Provost 165408b68b0eSGleb Smirnoff CARP_LOCK_INIT(sc); 165508b68b0eSGleb Smirnoff #ifdef INET 165608b68b0eSGleb Smirnoff callout_init_mtx(&sc->sc_md_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED); 165708b68b0eSGleb Smirnoff #endif 165808b68b0eSGleb Smirnoff #ifdef INET6 165908b68b0eSGleb Smirnoff callout_init_mtx(&sc->sc_md6_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED); 166008b68b0eSGleb Smirnoff #endif 166108b68b0eSGleb Smirnoff callout_init_mtx(&sc->sc_ad_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED); 166208b68b0eSGleb Smirnoff 166308b68b0eSGleb Smirnoff CIF_LOCK(cif); 166408b68b0eSGleb Smirnoff TAILQ_INSERT_TAIL(&cif->cif_vrs, sc, sc_list); 166508b68b0eSGleb Smirnoff CIF_UNLOCK(cif); 166608b68b0eSGleb Smirnoff 166708b68b0eSGleb Smirnoff mtx_lock(&carp_mtx); 166808b68b0eSGleb Smirnoff LIST_INSERT_HEAD(&carp_list, sc, sc_next); 166908b68b0eSGleb Smirnoff mtx_unlock(&carp_mtx); 167008b68b0eSGleb Smirnoff 167108b68b0eSGleb Smirnoff return (sc); 167208b68b0eSGleb Smirnoff } 167308b68b0eSGleb Smirnoff 16749c2cd1aaSGleb Smirnoff static void 167508b68b0eSGleb Smirnoff carp_grow_ifas(struct carp_softc *sc) 167608b68b0eSGleb Smirnoff { 167708b68b0eSGleb Smirnoff struct ifaddr **new; 167808b68b0eSGleb Smirnoff 16799c2cd1aaSGleb Smirnoff new = malloc(sc->sc_ifasiz * 2, M_CARP, M_WAITOK | M_ZERO); 16809c2cd1aaSGleb Smirnoff CARP_LOCK(sc); 168108b68b0eSGleb Smirnoff bcopy(sc->sc_ifas, new, sc->sc_ifasiz); 168208b68b0eSGleb Smirnoff free(sc->sc_ifas, M_CARP); 168308b68b0eSGleb Smirnoff sc->sc_ifas = new; 168408b68b0eSGleb Smirnoff sc->sc_ifasiz *= 2; 16859c2cd1aaSGleb Smirnoff CARP_UNLOCK(sc); 168608b68b0eSGleb Smirnoff } 168708b68b0eSGleb Smirnoff 168808b68b0eSGleb Smirnoff static void 168908b68b0eSGleb Smirnoff carp_destroy(struct carp_softc *sc) 169008b68b0eSGleb Smirnoff { 169108b68b0eSGleb Smirnoff struct ifnet *ifp = sc->sc_carpdev; 169208b68b0eSGleb Smirnoff struct carp_if *cif = ifp->if_carp; 169308b68b0eSGleb Smirnoff 16949c2cd1aaSGleb Smirnoff sx_assert(&carp_sx, SA_XLOCKED); 1695a9a2c40cSGleb Smirnoff 16969c2cd1aaSGleb Smirnoff if (sc->sc_suppress) 16979c2cd1aaSGleb Smirnoff carp_demote_adj(-V_carp_ifdown_adj, "vhid removed"); 16989c2cd1aaSGleb Smirnoff CARP_UNLOCK(sc); 16999c2cd1aaSGleb Smirnoff 17009c2cd1aaSGleb Smirnoff CIF_LOCK(cif); 170108b68b0eSGleb Smirnoff TAILQ_REMOVE(&cif->cif_vrs, sc, sc_list); 17029c2cd1aaSGleb Smirnoff CIF_UNLOCK(cif); 170308b68b0eSGleb Smirnoff 170408b68b0eSGleb Smirnoff mtx_lock(&carp_mtx); 170508b68b0eSGleb Smirnoff LIST_REMOVE(sc, sc_next); 170608b68b0eSGleb Smirnoff mtx_unlock(&carp_mtx); 170708b68b0eSGleb Smirnoff 170808b68b0eSGleb Smirnoff callout_drain(&sc->sc_ad_tmo); 170908b68b0eSGleb Smirnoff #ifdef INET 171008b68b0eSGleb Smirnoff callout_drain(&sc->sc_md_tmo); 171108b68b0eSGleb Smirnoff #endif 171208b68b0eSGleb Smirnoff #ifdef INET6 171308b68b0eSGleb Smirnoff callout_drain(&sc->sc_md6_tmo); 171408b68b0eSGleb Smirnoff #endif 171508b68b0eSGleb Smirnoff CARP_LOCK_DESTROY(sc); 171608b68b0eSGleb Smirnoff 171708b68b0eSGleb Smirnoff free(sc->sc_ifas, M_CARP); 171808b68b0eSGleb Smirnoff free(sc, M_CARP); 171908b68b0eSGleb Smirnoff } 172008b68b0eSGleb Smirnoff 172108b68b0eSGleb Smirnoff static struct carp_if* 172208b68b0eSGleb Smirnoff carp_alloc_if(struct ifnet *ifp) 1723a9771948SGleb Smirnoff { 172454bfbd51SWill Andrews struct carp_if *cif; 17250cc726f2SGleb Smirnoff int error; 1726d220759bSGleb Smirnoff 172708b68b0eSGleb Smirnoff cif = malloc(sizeof(*cif), M_CARP, M_WAITOK|M_ZERO); 172808b68b0eSGleb Smirnoff 17290cc726f2SGleb Smirnoff if ((error = ifpromisc(ifp, 1)) != 0) 17300cc726f2SGleb Smirnoff printf("%s: ifpromisc(%s) failed: %d\n", 1731511a6d5eSKristof Provost __func__, if_name(ifp), error); 17320cc726f2SGleb Smirnoff else 17330cc726f2SGleb Smirnoff cif->cif_flags |= CIF_PROMISC; 173408b68b0eSGleb Smirnoff 173508b68b0eSGleb Smirnoff CIF_LOCK_INIT(cif); 173608b68b0eSGleb Smirnoff cif->cif_ifp = ifp; 173708b68b0eSGleb Smirnoff TAILQ_INIT(&cif->cif_vrs); 173808b68b0eSGleb Smirnoff 1739137f91e8SJohn Baldwin IF_ADDR_WLOCK(ifp); 174008b68b0eSGleb Smirnoff ifp->if_carp = cif; 174108b68b0eSGleb Smirnoff if_ref(ifp); 1742137f91e8SJohn Baldwin IF_ADDR_WUNLOCK(ifp); 174308b68b0eSGleb Smirnoff 174408b68b0eSGleb Smirnoff return (cif); 1745d220759bSGleb Smirnoff } 1746d220759bSGleb Smirnoff 1747d220759bSGleb Smirnoff static void 174808b68b0eSGleb Smirnoff carp_free_if(struct carp_if *cif) 174908b68b0eSGleb Smirnoff { 175008b68b0eSGleb Smirnoff struct ifnet *ifp = cif->cif_ifp; 175108b68b0eSGleb Smirnoff 175208b68b0eSGleb Smirnoff CIF_LOCK_ASSERT(cif); 175308b68b0eSGleb Smirnoff KASSERT(TAILQ_EMPTY(&cif->cif_vrs), ("%s: softc list not empty", 175408b68b0eSGleb Smirnoff __func__)); 175508b68b0eSGleb Smirnoff 1756137f91e8SJohn Baldwin IF_ADDR_WLOCK(ifp); 175708b68b0eSGleb Smirnoff ifp->if_carp = NULL; 1758137f91e8SJohn Baldwin IF_ADDR_WUNLOCK(ifp); 175908b68b0eSGleb Smirnoff 176008b68b0eSGleb Smirnoff CIF_LOCK_DESTROY(cif); 176108b68b0eSGleb Smirnoff 17620cc726f2SGleb Smirnoff if (cif->cif_flags & CIF_PROMISC) 176308b68b0eSGleb Smirnoff ifpromisc(ifp, 0); 17641f6addd9SMikolaj Golub if_rele(ifp); 176508b68b0eSGleb Smirnoff 176608b68b0eSGleb Smirnoff free(cif, M_CARP); 176708b68b0eSGleb Smirnoff } 176808b68b0eSGleb Smirnoff 176940e04359SKristof Provost static bool 177040e04359SKristof Provost carp_carprcp(void *arg, struct carp_softc *sc, int priv) 177108b68b0eSGleb Smirnoff { 177240e04359SKristof Provost struct carpreq *carpr = arg; 177308b68b0eSGleb Smirnoff 177408b68b0eSGleb Smirnoff CARP_LOCK(sc); 177508b68b0eSGleb Smirnoff carpr->carpr_state = sc->sc_state; 177608b68b0eSGleb Smirnoff carpr->carpr_vhid = sc->sc_vhid; 177708b68b0eSGleb Smirnoff carpr->carpr_advbase = sc->sc_advbase; 177808b68b0eSGleb Smirnoff carpr->carpr_advskew = sc->sc_advskew; 177908b68b0eSGleb Smirnoff if (priv) 178008b68b0eSGleb Smirnoff bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key)); 178108b68b0eSGleb Smirnoff else 178208b68b0eSGleb Smirnoff bzero(carpr->carpr_key, sizeof(carpr->carpr_key)); 178308b68b0eSGleb Smirnoff CARP_UNLOCK(sc); 178440e04359SKristof Provost 178540e04359SKristof Provost return (true); 178608b68b0eSGleb Smirnoff } 178708b68b0eSGleb Smirnoff 178840e04359SKristof Provost static int 178913781800SKristof Provost carp_ioctl_set(if_t ifp, struct carpkreq *carpr) 179008b68b0eSGleb Smirnoff { 179149cad3daSZhenlei Huang struct epoch_tracker et; 179208b68b0eSGleb Smirnoff struct carp_softc *sc = NULL; 179340e04359SKristof Provost int error = 0; 179408b68b0eSGleb Smirnoff 179508b68b0eSGleb Smirnoff 179640e04359SKristof Provost if (carpr->carpr_vhid <= 0 || carpr->carpr_vhid > CARP_MAXVHID || 179740e04359SKristof Provost carpr->carpr_advbase < 0 || carpr->carpr_advskew < 0) { 179840e04359SKristof Provost return (EINVAL); 179908b68b0eSGleb Smirnoff } 180008b68b0eSGleb Smirnoff 180108b68b0eSGleb Smirnoff if (ifp->if_carp) { 180208b68b0eSGleb Smirnoff IFNET_FOREACH_CARP(ifp, sc) 180340e04359SKristof Provost if (sc->sc_vhid == carpr->carpr_vhid) 180408b68b0eSGleb Smirnoff break; 180508b68b0eSGleb Smirnoff } 180608b68b0eSGleb Smirnoff if (sc == NULL) { 180708b68b0eSGleb Smirnoff sc = carp_alloc(ifp); 180808b68b0eSGleb Smirnoff CARP_LOCK(sc); 180940e04359SKristof Provost sc->sc_vhid = carpr->carpr_vhid; 181008b68b0eSGleb Smirnoff LLADDR(&sc->sc_addr)[0] = 0; 181108b68b0eSGleb Smirnoff LLADDR(&sc->sc_addr)[1] = 0; 181208b68b0eSGleb Smirnoff LLADDR(&sc->sc_addr)[2] = 0x5e; 181308b68b0eSGleb Smirnoff LLADDR(&sc->sc_addr)[3] = 0; 181408b68b0eSGleb Smirnoff LLADDR(&sc->sc_addr)[4] = 1; 181508b68b0eSGleb Smirnoff LLADDR(&sc->sc_addr)[5] = sc->sc_vhid; 181608b68b0eSGleb Smirnoff } else 181708b68b0eSGleb Smirnoff CARP_LOCK(sc); 181840e04359SKristof Provost if (carpr->carpr_advbase > 0) { 181940e04359SKristof Provost if (carpr->carpr_advbase > 255 || 182040e04359SKristof Provost carpr->carpr_advbase < CARP_DFLTINTV) { 182108b68b0eSGleb Smirnoff error = EINVAL; 182240e04359SKristof Provost goto out; 182308b68b0eSGleb Smirnoff } 182440e04359SKristof Provost sc->sc_advbase = carpr->carpr_advbase; 182508b68b0eSGleb Smirnoff } 182640e04359SKristof Provost if (carpr->carpr_advskew >= 255) { 182708b68b0eSGleb Smirnoff error = EINVAL; 182840e04359SKristof Provost goto out; 182908b68b0eSGleb Smirnoff } 183040e04359SKristof Provost sc->sc_advskew = carpr->carpr_advskew; 183113781800SKristof Provost if (carpr->carpr_addr.s_addr != INADDR_ANY) 183213781800SKristof Provost sc->sc_carpaddr = carpr->carpr_addr; 183313781800SKristof Provost if (! IN6_IS_ADDR_UNSPECIFIED(&carpr->carpr_addr6)) { 183413781800SKristof Provost memcpy(&sc->sc_carpaddr6, &carpr->carpr_addr6, 183513781800SKristof Provost sizeof(sc->sc_carpaddr6)); 183613781800SKristof Provost } 183740e04359SKristof Provost if (carpr->carpr_key[0] != '\0') { 183840e04359SKristof Provost bcopy(carpr->carpr_key, sc->sc_key, sizeof(sc->sc_key)); 183908b68b0eSGleb Smirnoff carp_hmac_prepare(sc); 184008b68b0eSGleb Smirnoff } 184108b68b0eSGleb Smirnoff if (sc->sc_state != INIT && 184240e04359SKristof Provost carpr->carpr_state != sc->sc_state) { 184340e04359SKristof Provost switch (carpr->carpr_state) { 184408b68b0eSGleb Smirnoff case BACKUP: 184508b68b0eSGleb Smirnoff callout_stop(&sc->sc_ad_tmo); 1846369a6708SWill Andrews carp_set_state(sc, BACKUP, 1847369a6708SWill Andrews "user requested via ifconfig"); 184808b68b0eSGleb Smirnoff carp_setrun(sc, 0); 184908b68b0eSGleb Smirnoff carp_delroute(sc); 185008b68b0eSGleb Smirnoff break; 185108b68b0eSGleb Smirnoff case MASTER: 185249cad3daSZhenlei Huang NET_EPOCH_ENTER(et); 1853369a6708SWill Andrews carp_master_down_locked(sc, 1854369a6708SWill Andrews "user requested via ifconfig"); 185549cad3daSZhenlei Huang NET_EPOCH_EXIT(et); 185608b68b0eSGleb Smirnoff break; 185708b68b0eSGleb Smirnoff default: 185808b68b0eSGleb Smirnoff break; 185908b68b0eSGleb Smirnoff } 186008b68b0eSGleb Smirnoff } 186108b68b0eSGleb Smirnoff 186240e04359SKristof Provost out: 186340e04359SKristof Provost CARP_UNLOCK(sc); 186440e04359SKristof Provost 186540e04359SKristof Provost return (error); 186640e04359SKristof Provost } 186740e04359SKristof Provost 186840e04359SKristof Provost static int 186940e04359SKristof Provost carp_ioctl_get(if_t ifp, struct ucred *cred, struct carpreq *carpr, 187040e04359SKristof Provost bool (*outfn)(void *, struct carp_softc *, int), void *arg) 187108b68b0eSGleb Smirnoff { 187208b68b0eSGleb Smirnoff int priveleged; 187340e04359SKristof Provost struct carp_softc *sc; 187408b68b0eSGleb Smirnoff 187540e04359SKristof Provost if (carpr->carpr_vhid < 0 || carpr->carpr_vhid > CARP_MAXVHID) 187640e04359SKristof Provost return (EINVAL); 187740e04359SKristof Provost if (carpr->carpr_count < 1) 187840e04359SKristof Provost return (EMSGSIZE); 187940e04359SKristof Provost if (ifp->if_carp == NULL) 188040e04359SKristof Provost return (ENOENT); 188108b68b0eSGleb Smirnoff 188240e04359SKristof Provost priveleged = (priv_check_cred(cred, PRIV_NETINET_CARP) == 0); 188340e04359SKristof Provost if (carpr->carpr_vhid != 0) { 188408b68b0eSGleb Smirnoff IFNET_FOREACH_CARP(ifp, sc) 188540e04359SKristof Provost if (sc->sc_vhid == carpr->carpr_vhid) 188608b68b0eSGleb Smirnoff break; 188740e04359SKristof Provost if (sc == NULL) 188840e04359SKristof Provost return (ENOENT); 188940e04359SKristof Provost 189040e04359SKristof Provost if (! outfn(arg, sc, priveleged)) 189140e04359SKristof Provost return (ENOMEM); 189240e04359SKristof Provost carpr->carpr_count = 1; 189308b68b0eSGleb Smirnoff } else { 189440e04359SKristof Provost int count; 189508b68b0eSGleb Smirnoff 189608b68b0eSGleb Smirnoff count = 0; 189708b68b0eSGleb Smirnoff IFNET_FOREACH_CARP(ifp, sc) 189808b68b0eSGleb Smirnoff count++; 189908b68b0eSGleb Smirnoff 190040e04359SKristof Provost if (count > carpr->carpr_count) 190140e04359SKristof Provost return (EMSGSIZE); 190240e04359SKristof Provost 190340e04359SKristof Provost IFNET_FOREACH_CARP(ifp, sc) { 190440e04359SKristof Provost if (! outfn(arg, sc, priveleged)) 190540e04359SKristof Provost return (ENOMEM); 190640e04359SKristof Provost carpr->carpr_count = count; 190740e04359SKristof Provost } 190808b68b0eSGleb Smirnoff } 190908b68b0eSGleb Smirnoff 191040e04359SKristof Provost return (0); 191140e04359SKristof Provost } 191240e04359SKristof Provost 191340e04359SKristof Provost int 191440e04359SKristof Provost carp_ioctl(struct ifreq *ifr, u_long cmd, struct thread *td) 191540e04359SKristof Provost { 191640e04359SKristof Provost struct carpreq carpr; 191713781800SKristof Provost struct carpkreq carprk = { }; 191840e04359SKristof Provost struct ifnet *ifp; 191940e04359SKristof Provost int error = 0; 192040e04359SKristof Provost 192140e04359SKristof Provost if ((error = copyin(ifr_data_get_ptr(ifr), &carpr, sizeof carpr))) 192240e04359SKristof Provost return (error); 192340e04359SKristof Provost 192440e04359SKristof Provost ifp = ifunit_ref(ifr->ifr_name); 192540e04359SKristof Provost if ((error = carp_is_supported_if(ifp)) != 0) 192640e04359SKristof Provost goto out; 192740e04359SKristof Provost 192840e04359SKristof Provost if ((ifp->if_flags & IFF_MULTICAST) == 0) { 192940e04359SKristof Provost error = EADDRNOTAVAIL; 193040e04359SKristof Provost goto out; 193140e04359SKristof Provost } 193240e04359SKristof Provost 193340e04359SKristof Provost sx_xlock(&carp_sx); 193440e04359SKristof Provost switch (cmd) { 193540e04359SKristof Provost case SIOCSVH: 193640e04359SKristof Provost if ((error = priv_check(td, PRIV_NETINET_CARP))) 193740e04359SKristof Provost break; 193840e04359SKristof Provost 193913781800SKristof Provost memcpy(&carprk, &carpr, sizeof(carpr)); 194013781800SKristof Provost error = carp_ioctl_set(ifp, &carprk); 194140e04359SKristof Provost break; 194240e04359SKristof Provost 194340e04359SKristof Provost case SIOCGVH: 194440e04359SKristof Provost error = carp_ioctl_get(ifp, td->td_ucred, &carpr, 194540e04359SKristof Provost carp_carprcp, &carpr); 194640e04359SKristof Provost if (error == 0) { 1947541d96aaSBrooks Davis error = copyout(&carpr, 194840e04359SKristof Provost (char *)ifr_data_get_ptr(ifr), 194940e04359SKristof Provost carpr.carpr_count * sizeof(carpr)); 195008b68b0eSGleb Smirnoff } 195108b68b0eSGleb Smirnoff break; 195208b68b0eSGleb Smirnoff default: 195308b68b0eSGleb Smirnoff error = EINVAL; 195408b68b0eSGleb Smirnoff } 195593d4534cSGleb Smirnoff sx_xunlock(&carp_sx); 195608b68b0eSGleb Smirnoff 195708b68b0eSGleb Smirnoff out: 195840e04359SKristof Provost if (ifp != NULL) 195908b68b0eSGleb Smirnoff if_rele(ifp); 196008b68b0eSGleb Smirnoff 196108b68b0eSGleb Smirnoff return (error); 196208b68b0eSGleb Smirnoff } 196308b68b0eSGleb Smirnoff 196408b68b0eSGleb Smirnoff static int 196508b68b0eSGleb Smirnoff carp_get_vhid(struct ifaddr *ifa) 196608b68b0eSGleb Smirnoff { 196708b68b0eSGleb Smirnoff 196808b68b0eSGleb Smirnoff if (ifa == NULL || ifa->ifa_carp == NULL) 196908b68b0eSGleb Smirnoff return (0); 197008b68b0eSGleb Smirnoff 197108b68b0eSGleb Smirnoff return (ifa->ifa_carp->sc_vhid); 197208b68b0eSGleb Smirnoff } 197308b68b0eSGleb Smirnoff 197408b68b0eSGleb Smirnoff int 197508b68b0eSGleb Smirnoff carp_attach(struct ifaddr *ifa, int vhid) 197608b68b0eSGleb Smirnoff { 197708b68b0eSGleb Smirnoff struct ifnet *ifp = ifa->ifa_ifp; 1978a9a2c40cSGleb Smirnoff struct carp_if *cif = ifp->if_carp; 197908b68b0eSGleb Smirnoff struct carp_softc *sc; 198008b68b0eSGleb Smirnoff int index, error; 198108b68b0eSGleb Smirnoff 19829c2cd1aaSGleb Smirnoff KASSERT(ifa->ifa_carp == NULL, ("%s: ifa %p attached", __func__, ifa)); 198308b68b0eSGleb Smirnoff 198408b68b0eSGleb Smirnoff switch (ifa->ifa_addr->sa_family) { 198508b68b0eSGleb Smirnoff #ifdef INET 198608b68b0eSGleb Smirnoff case AF_INET: 198708b68b0eSGleb Smirnoff #endif 198808b68b0eSGleb Smirnoff #ifdef INET6 198908b68b0eSGleb Smirnoff case AF_INET6: 199008b68b0eSGleb Smirnoff #endif 199108b68b0eSGleb Smirnoff break; 199208b68b0eSGleb Smirnoff default: 199308b68b0eSGleb Smirnoff return (EPROTOTYPE); 199408b68b0eSGleb Smirnoff } 199508b68b0eSGleb Smirnoff 19969c2cd1aaSGleb Smirnoff sx_xlock(&carp_sx); 19979c2cd1aaSGleb Smirnoff if (ifp->if_carp == NULL) { 19989c2cd1aaSGleb Smirnoff sx_xunlock(&carp_sx); 19999c2cd1aaSGleb Smirnoff return (ENOPROTOOPT); 20009c2cd1aaSGleb Smirnoff } 20019c2cd1aaSGleb Smirnoff 200208b68b0eSGleb Smirnoff IFNET_FOREACH_CARP(ifp, sc) 200308b68b0eSGleb Smirnoff if (sc->sc_vhid == vhid) 200408b68b0eSGleb Smirnoff break; 2005a9a2c40cSGleb Smirnoff if (sc == NULL) { 20069c2cd1aaSGleb Smirnoff sx_xunlock(&carp_sx); 200708b68b0eSGleb Smirnoff return (ENOENT); 2008a9a2c40cSGleb Smirnoff } 200908b68b0eSGleb Smirnoff 2010a9a2c40cSGleb Smirnoff error = carp_multicast_setup(cif, ifa->ifa_addr->sa_family); 2011a9a2c40cSGleb Smirnoff if (error) { 2012a9a2c40cSGleb Smirnoff CIF_FREE(cif); 20139c2cd1aaSGleb Smirnoff sx_xunlock(&carp_sx); 201408b68b0eSGleb Smirnoff return (error); 2015a9a2c40cSGleb Smirnoff } 201608b68b0eSGleb Smirnoff 201708b68b0eSGleb Smirnoff index = sc->sc_naddrs + sc->sc_naddrs6 + 1; 201808b68b0eSGleb Smirnoff if (index > sc->sc_ifasiz / sizeof(struct ifaddr *)) 20199c2cd1aaSGleb Smirnoff carp_grow_ifas(sc); 202008b68b0eSGleb Smirnoff 202108b68b0eSGleb Smirnoff switch (ifa->ifa_addr->sa_family) { 202208b68b0eSGleb Smirnoff #ifdef INET 202308b68b0eSGleb Smirnoff case AF_INET: 2024a9a2c40cSGleb Smirnoff cif->cif_naddrs++; 202508b68b0eSGleb Smirnoff sc->sc_naddrs++; 202608b68b0eSGleb Smirnoff break; 202708b68b0eSGleb Smirnoff #endif 202808b68b0eSGleb Smirnoff #ifdef INET6 202908b68b0eSGleb Smirnoff case AF_INET6: 2030a9a2c40cSGleb Smirnoff cif->cif_naddrs6++; 203108b68b0eSGleb Smirnoff sc->sc_naddrs6++; 203208b68b0eSGleb Smirnoff break; 203308b68b0eSGleb Smirnoff #endif 203408b68b0eSGleb Smirnoff } 203508b68b0eSGleb Smirnoff 203608b68b0eSGleb Smirnoff ifa_ref(ifa); 20379c2cd1aaSGleb Smirnoff 20389c2cd1aaSGleb Smirnoff CARP_LOCK(sc); 203908b68b0eSGleb Smirnoff sc->sc_ifas[index - 1] = ifa; 204008b68b0eSGleb Smirnoff ifa->ifa_carp = sc; 204108b68b0eSGleb Smirnoff carp_hmac_prepare(sc); 204208b68b0eSGleb Smirnoff carp_sc_state(sc); 204308b68b0eSGleb Smirnoff CARP_UNLOCK(sc); 20449c2cd1aaSGleb Smirnoff 20459c2cd1aaSGleb Smirnoff sx_xunlock(&carp_sx); 204608b68b0eSGleb Smirnoff 204708b68b0eSGleb Smirnoff return (0); 204808b68b0eSGleb Smirnoff } 204908b68b0eSGleb Smirnoff 205008b68b0eSGleb Smirnoff void 2051338e227aSLuiz Otavio O Souza carp_detach(struct ifaddr *ifa, bool keep_cif) 205208b68b0eSGleb Smirnoff { 2053a9a2c40cSGleb Smirnoff struct ifnet *ifp = ifa->ifa_ifp; 2054a9a2c40cSGleb Smirnoff struct carp_if *cif = ifp->if_carp; 205508b68b0eSGleb Smirnoff struct carp_softc *sc = ifa->ifa_carp; 205608b68b0eSGleb Smirnoff int i, index; 205708b68b0eSGleb Smirnoff 205808b68b0eSGleb Smirnoff KASSERT(sc != NULL, ("%s: %p not attached", __func__, ifa)); 205908b68b0eSGleb Smirnoff 20609c2cd1aaSGleb Smirnoff sx_xlock(&carp_sx); 206108b68b0eSGleb Smirnoff 20629c2cd1aaSGleb Smirnoff CARP_LOCK(sc); 206308b68b0eSGleb Smirnoff /* Shift array. */ 206408b68b0eSGleb Smirnoff index = sc->sc_naddrs + sc->sc_naddrs6; 206508b68b0eSGleb Smirnoff for (i = 0; i < index; i++) 206608b68b0eSGleb Smirnoff if (sc->sc_ifas[i] == ifa) 206708b68b0eSGleb Smirnoff break; 206808b68b0eSGleb Smirnoff KASSERT(i < index, ("%s: %p no backref", __func__, ifa)); 206908b68b0eSGleb Smirnoff for (; i < index - 1; i++) 207008b68b0eSGleb Smirnoff sc->sc_ifas[i] = sc->sc_ifas[i+1]; 207108b68b0eSGleb Smirnoff sc->sc_ifas[index - 1] = NULL; 207208b68b0eSGleb Smirnoff 207308b68b0eSGleb Smirnoff switch (ifa->ifa_addr->sa_family) { 207408b68b0eSGleb Smirnoff #ifdef INET 207508b68b0eSGleb Smirnoff case AF_INET: 2076a9a2c40cSGleb Smirnoff cif->cif_naddrs--; 207708b68b0eSGleb Smirnoff sc->sc_naddrs--; 207808b68b0eSGleb Smirnoff break; 207908b68b0eSGleb Smirnoff #endif 208008b68b0eSGleb Smirnoff #ifdef INET6 208108b68b0eSGleb Smirnoff case AF_INET6: 2082a9a2c40cSGleb Smirnoff cif->cif_naddrs6--; 208308b68b0eSGleb Smirnoff sc->sc_naddrs6--; 208408b68b0eSGleb Smirnoff break; 208508b68b0eSGleb Smirnoff #endif 208608b68b0eSGleb Smirnoff } 208708b68b0eSGleb Smirnoff 20882512b096SGleb Smirnoff carp_ifa_delroute(ifa); 2089a9a2c40cSGleb Smirnoff carp_multicast_cleanup(cif, ifa->ifa_addr->sa_family); 209008b68b0eSGleb Smirnoff 209108b68b0eSGleb Smirnoff ifa->ifa_carp = NULL; 209208b68b0eSGleb Smirnoff ifa_free(ifa); 209308b68b0eSGleb Smirnoff 209408b68b0eSGleb Smirnoff carp_hmac_prepare(sc); 209508b68b0eSGleb Smirnoff carp_sc_state(sc); 209608b68b0eSGleb Smirnoff 2097338e227aSLuiz Otavio O Souza if (!keep_cif && sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) 209808b68b0eSGleb Smirnoff carp_destroy(sc); 20999c2cd1aaSGleb Smirnoff else 210008b68b0eSGleb Smirnoff CARP_UNLOCK(sc); 21019c2cd1aaSGleb Smirnoff 2102338e227aSLuiz Otavio O Souza if (!keep_cif) 21039c2cd1aaSGleb Smirnoff CIF_FREE(cif); 21049c2cd1aaSGleb Smirnoff 21059c2cd1aaSGleb Smirnoff sx_xunlock(&carp_sx); 210608b68b0eSGleb Smirnoff } 210708b68b0eSGleb Smirnoff 210808b68b0eSGleb Smirnoff static void 2109d01641e2SWill Andrews carp_set_state(struct carp_softc *sc, int state, const char *reason) 211008b68b0eSGleb Smirnoff { 211108b68b0eSGleb Smirnoff 211208b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); 211308b68b0eSGleb Smirnoff 211408b68b0eSGleb Smirnoff if (sc->sc_state != state) { 211508b68b0eSGleb Smirnoff const char *carp_states[] = { CARP_STATES }; 211608b68b0eSGleb Smirnoff char subsys[IFNAMSIZ+5]; 211708b68b0eSGleb Smirnoff 211808b68b0eSGleb Smirnoff snprintf(subsys, IFNAMSIZ+5, "%u@%s", sc->sc_vhid, 2119511a6d5eSKristof Provost if_name(sc->sc_carpdev)); 2120d01641e2SWill Andrews 2121d01641e2SWill Andrews CARP_LOG("%s: %s -> %s (%s)\n", subsys, 2122d01641e2SWill Andrews carp_states[sc->sc_state], carp_states[state], reason); 2123d01641e2SWill Andrews 2124d01641e2SWill Andrews sc->sc_state = state; 2125d01641e2SWill Andrews 212608b68b0eSGleb Smirnoff devctl_notify("CARP", subsys, carp_states[state], NULL); 212708b68b0eSGleb Smirnoff } 212808b68b0eSGleb Smirnoff } 212908b68b0eSGleb Smirnoff 213008b68b0eSGleb Smirnoff static void 213108b68b0eSGleb Smirnoff carp_linkstate(struct ifnet *ifp) 2132d220759bSGleb Smirnoff { 2133d220759bSGleb Smirnoff struct carp_softc *sc; 2134d220759bSGleb Smirnoff 213508b68b0eSGleb Smirnoff CIF_LOCK(ifp->if_carp); 213608b68b0eSGleb Smirnoff IFNET_FOREACH_CARP(ifp, sc) { 213708b68b0eSGleb Smirnoff CARP_LOCK(sc); 213808b68b0eSGleb Smirnoff carp_sc_state(sc); 213908b68b0eSGleb Smirnoff CARP_UNLOCK(sc); 214008b68b0eSGleb Smirnoff } 214108b68b0eSGleb Smirnoff CIF_UNLOCK(ifp->if_carp); 21424cb39345SGleb Smirnoff } 21434cb39345SGleb Smirnoff 21444cb39345SGleb Smirnoff static void 214508b68b0eSGleb Smirnoff carp_sc_state(struct carp_softc *sc) 21464cb39345SGleb Smirnoff { 214708b68b0eSGleb Smirnoff 214808b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); 21494cb39345SGleb Smirnoff 2150e8c34a71SGleb Smirnoff if (sc->sc_carpdev->if_link_state != LINK_STATE_UP || 2151167a3440SAlexander Motin !(sc->sc_carpdev->if_flags & IFF_UP) || 2152167a3440SAlexander Motin !V_carp_allow) { 2153a9771948SGleb Smirnoff callout_stop(&sc->sc_ad_tmo); 215408b68b0eSGleb Smirnoff #ifdef INET 2155a9771948SGleb Smirnoff callout_stop(&sc->sc_md_tmo); 215608b68b0eSGleb Smirnoff #endif 215708b68b0eSGleb Smirnoff #ifdef INET6 2158a9771948SGleb Smirnoff callout_stop(&sc->sc_md6_tmo); 215908b68b0eSGleb Smirnoff #endif 2160bb269f3aSWill Andrews carp_set_state(sc, INIT, "hardware interface down"); 2161a9771948SGleb Smirnoff carp_setrun(sc, 0); 2162*600bf006SAndrey V. Elsukov carp_delroute(sc); 2163f08535f8SGleb Smirnoff if (!sc->sc_suppress) 2164c5c392e7SMikolaj Golub carp_demote_adj(V_carp_ifdown_adj, "interface down"); 2165a9771948SGleb Smirnoff sc->sc_suppress = 1; 2166a9771948SGleb Smirnoff } else { 2167bb269f3aSWill Andrews carp_set_state(sc, INIT, "hardware interface up"); 2168a9771948SGleb Smirnoff carp_setrun(sc, 0); 2169a9771948SGleb Smirnoff if (sc->sc_suppress) 2170c5c392e7SMikolaj Golub carp_demote_adj(-V_carp_ifdown_adj, "interface up"); 2171a9771948SGleb Smirnoff sc->sc_suppress = 0; 2172a9771948SGleb Smirnoff } 2173a9771948SGleb Smirnoff } 2174a9771948SGleb Smirnoff 2175f08535f8SGleb Smirnoff static void 2176f08535f8SGleb Smirnoff carp_demote_adj(int adj, char *reason) 2177f08535f8SGleb Smirnoff { 2178c5c392e7SMikolaj Golub atomic_add_int(&V_carp_demotion, adj); 2179c5c392e7SMikolaj Golub CARP_LOG("demoted by %d to %d (%s)\n", adj, V_carp_demotion, reason); 2180f08535f8SGleb Smirnoff taskqueue_enqueue(taskqueue_swi, &carp_sendall_task); 2181f08535f8SGleb Smirnoff } 218208b68b0eSGleb Smirnoff 21837951008bSGleb Smirnoff static int 2184167a3440SAlexander Motin carp_allow_sysctl(SYSCTL_HANDLER_ARGS) 2185167a3440SAlexander Motin { 2186167a3440SAlexander Motin int new, error; 2187167a3440SAlexander Motin struct carp_softc *sc; 2188167a3440SAlexander Motin 2189167a3440SAlexander Motin new = V_carp_allow; 2190167a3440SAlexander Motin error = sysctl_handle_int(oidp, &new, 0, req); 2191167a3440SAlexander Motin if (error || !req->newptr) 2192167a3440SAlexander Motin return (error); 2193167a3440SAlexander Motin 2194167a3440SAlexander Motin if (V_carp_allow != new) { 2195167a3440SAlexander Motin V_carp_allow = new; 2196167a3440SAlexander Motin 2197167a3440SAlexander Motin mtx_lock(&carp_mtx); 2198167a3440SAlexander Motin LIST_FOREACH(sc, &carp_list, sc_next) { 2199167a3440SAlexander Motin CARP_LOCK(sc); 2200167a3440SAlexander Motin if (curvnet == sc->sc_carpdev->if_vnet) 2201167a3440SAlexander Motin carp_sc_state(sc); 2202167a3440SAlexander Motin CARP_UNLOCK(sc); 2203167a3440SAlexander Motin } 2204167a3440SAlexander Motin mtx_unlock(&carp_mtx); 2205167a3440SAlexander Motin } 2206167a3440SAlexander Motin 2207167a3440SAlexander Motin return (0); 2208167a3440SAlexander Motin } 2209167a3440SAlexander Motin 2210167a3440SAlexander Motin static int 22110d3d234cSKristof Provost carp_dscp_sysctl(SYSCTL_HANDLER_ARGS) 22120d3d234cSKristof Provost { 22130d3d234cSKristof Provost int new, error; 22140d3d234cSKristof Provost 22150d3d234cSKristof Provost new = V_carp_dscp; 22160d3d234cSKristof Provost error = sysctl_handle_int(oidp, &new, 0, req); 22170d3d234cSKristof Provost if (error || !req->newptr) 22180d3d234cSKristof Provost return (error); 22190d3d234cSKristof Provost 22200d3d234cSKristof Provost if (new < 0 || new > 63) 22210d3d234cSKristof Provost return (EINVAL); 22220d3d234cSKristof Provost 22230d3d234cSKristof Provost V_carp_dscp = new; 22240d3d234cSKristof Provost 22250d3d234cSKristof Provost return (0); 22260d3d234cSKristof Provost } 22270d3d234cSKristof Provost 22280d3d234cSKristof Provost static int 22297951008bSGleb Smirnoff carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS) 22307951008bSGleb Smirnoff { 22317951008bSGleb Smirnoff int new, error; 22327951008bSGleb Smirnoff 2233c5c392e7SMikolaj Golub new = V_carp_demotion; 22347951008bSGleb Smirnoff error = sysctl_handle_int(oidp, &new, 0, req); 22357951008bSGleb Smirnoff if (error || !req->newptr) 22367951008bSGleb Smirnoff return (error); 22377951008bSGleb Smirnoff 22387951008bSGleb Smirnoff carp_demote_adj(new, "sysctl"); 22397951008bSGleb Smirnoff 22407951008bSGleb Smirnoff return (0); 22417951008bSGleb Smirnoff } 22427951008bSGleb Smirnoff 224340e04359SKristof Provost static int 224440e04359SKristof Provost nlattr_get_carp_key(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target) 224540e04359SKristof Provost { 224640e04359SKristof Provost if (__predict_false(NLA_DATA_LEN(nla) > CARP_KEY_LEN)) 224740e04359SKristof Provost return (EINVAL); 224840e04359SKristof Provost 224940e04359SKristof Provost memcpy(target, NLA_DATA_CONST(nla), NLA_DATA_LEN(nla)); 225040e04359SKristof Provost return (0); 225140e04359SKristof Provost } 225240e04359SKristof Provost 225340e04359SKristof Provost struct carp_nl_send_args { 225440e04359SKristof Provost struct nlmsghdr *hdr; 225540e04359SKristof Provost struct nl_pstate *npt; 225640e04359SKristof Provost }; 225740e04359SKristof Provost 225840e04359SKristof Provost static bool 225940e04359SKristof Provost carp_nl_send(void *arg, struct carp_softc *sc, int priv) 226040e04359SKristof Provost { 226140e04359SKristof Provost struct carp_nl_send_args *nlsa = arg; 226240e04359SKristof Provost struct nlmsghdr *hdr = nlsa->hdr; 226340e04359SKristof Provost struct nl_pstate *npt = nlsa->npt; 226440e04359SKristof Provost struct nl_writer *nw = npt->nw; 226540e04359SKristof Provost struct genlmsghdr *ghdr_new; 226640e04359SKristof Provost 226740e04359SKristof Provost if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr))) { 226840e04359SKristof Provost nlmsg_abort(nw); 226940e04359SKristof Provost return (false); 227040e04359SKristof Provost } 227140e04359SKristof Provost 227240e04359SKristof Provost ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr); 227340e04359SKristof Provost if (ghdr_new == NULL) { 227440e04359SKristof Provost nlmsg_abort(nw); 227540e04359SKristof Provost return (false); 227640e04359SKristof Provost } 227740e04359SKristof Provost 227840e04359SKristof Provost ghdr_new->cmd = CARP_NL_CMD_GET; 227940e04359SKristof Provost ghdr_new->version = 0; 228040e04359SKristof Provost ghdr_new->reserved = 0; 228140e04359SKristof Provost 228240e04359SKristof Provost CARP_LOCK(sc); 228340e04359SKristof Provost 228440e04359SKristof Provost nlattr_add_u32(nw, CARP_NL_VHID, sc->sc_vhid); 228540e04359SKristof Provost nlattr_add_u32(nw, CARP_NL_STATE, sc->sc_state); 228640e04359SKristof Provost nlattr_add_s32(nw, CARP_NL_ADVBASE, sc->sc_advbase); 228740e04359SKristof Provost nlattr_add_s32(nw, CARP_NL_ADVSKEW, sc->sc_advskew); 228813781800SKristof Provost nlattr_add_in_addr(nw, CARP_NL_ADDR, &sc->sc_carpaddr); 228913781800SKristof Provost nlattr_add_in6_addr(nw, CARP_NL_ADDR6, &sc->sc_carpaddr6); 229040e04359SKristof Provost 229140e04359SKristof Provost if (priv) 229240e04359SKristof Provost nlattr_add(nw, CARP_NL_KEY, sizeof(sc->sc_key), sc->sc_key); 229340e04359SKristof Provost 229440e04359SKristof Provost CARP_UNLOCK(sc); 229540e04359SKristof Provost 229640e04359SKristof Provost if (! nlmsg_end(nw)) { 229740e04359SKristof Provost nlmsg_abort(nw); 229840e04359SKristof Provost return (false); 229940e04359SKristof Provost } 230040e04359SKristof Provost 230140e04359SKristof Provost return (true); 230240e04359SKristof Provost } 230340e04359SKristof Provost 230440e04359SKristof Provost struct nl_carp_parsed { 230540e04359SKristof Provost unsigned int ifindex; 230628921c4fSKristof Provost char *ifname; 230740e04359SKristof Provost uint32_t state; 230840e04359SKristof Provost uint32_t vhid; 230940e04359SKristof Provost int32_t advbase; 231040e04359SKristof Provost int32_t advskew; 231140e04359SKristof Provost char key[CARP_KEY_LEN]; 231213781800SKristof Provost struct in_addr addr; 231313781800SKristof Provost struct in6_addr addr6; 231440e04359SKristof Provost }; 231540e04359SKristof Provost 231640e04359SKristof Provost #define _IN(_field) offsetof(struct genlmsghdr, _field) 231740e04359SKristof Provost #define _OUT(_field) offsetof(struct nl_carp_parsed, _field) 231840e04359SKristof Provost 231940e04359SKristof Provost static const struct nlattr_parser nla_p_set[] = { 232040e04359SKristof Provost { .type = CARP_NL_VHID, .off = _OUT(vhid), .cb = nlattr_get_uint32 }, 232140e04359SKristof Provost { .type = CARP_NL_STATE, .off = _OUT(state), .cb = nlattr_get_uint32 }, 232240e04359SKristof Provost { .type = CARP_NL_ADVBASE, .off = _OUT(advbase), .cb = nlattr_get_uint32 }, 232340e04359SKristof Provost { .type = CARP_NL_ADVSKEW, .off = _OUT(advskew), .cb = nlattr_get_uint32 }, 232440e04359SKristof Provost { .type = CARP_NL_KEY, .off = _OUT(key), .cb = nlattr_get_carp_key }, 232540e04359SKristof Provost { .type = CARP_NL_IFINDEX, .off = _OUT(ifindex), .cb = nlattr_get_uint32 }, 232613781800SKristof Provost { .type = CARP_NL_ADDR, .off = _OUT(addr), .cb = nlattr_get_in_addr }, 232713781800SKristof Provost { .type = CARP_NL_ADDR6, .off = _OUT(addr6), .cb = nlattr_get_in6_addr }, 232828921c4fSKristof Provost { .type = CARP_NL_IFNAME, .off = _OUT(ifname), .cb = nlattr_get_string }, 232940e04359SKristof Provost }; 233040e04359SKristof Provost static const struct nlfield_parser nlf_p_set[] = { 233140e04359SKristof Provost }; 233240e04359SKristof Provost NL_DECLARE_PARSER(carp_parser, struct genlmsghdr, nlf_p_set, nla_p_set); 233340e04359SKristof Provost #undef _IN 233440e04359SKristof Provost #undef _OUT 233540e04359SKristof Provost 233640e04359SKristof Provost 233740e04359SKristof Provost static int 233840e04359SKristof Provost carp_nl_get(struct nlmsghdr *hdr, struct nl_pstate *npt) 233940e04359SKristof Provost { 234040e04359SKristof Provost struct nl_carp_parsed attrs = { }; 234140e04359SKristof Provost struct carp_nl_send_args args; 234240e04359SKristof Provost struct carpreq carpr = { }; 234340e04359SKristof Provost struct epoch_tracker et; 234428921c4fSKristof Provost if_t ifp = NULL; 234540e04359SKristof Provost int error; 234640e04359SKristof Provost 234740e04359SKristof Provost error = nl_parse_nlmsg(hdr, &carp_parser, npt, &attrs); 234840e04359SKristof Provost if (error != 0) 234940e04359SKristof Provost return (error); 235040e04359SKristof Provost 235140e04359SKristof Provost NET_EPOCH_ENTER(et); 235228921c4fSKristof Provost if (attrs.ifname != NULL) 235328921c4fSKristof Provost ifp = ifunit_ref(attrs.ifname); 235428921c4fSKristof Provost else if (attrs.ifindex != 0) 235540e04359SKristof Provost ifp = ifnet_byindex_ref(attrs.ifindex); 235640e04359SKristof Provost NET_EPOCH_EXIT(et); 235740e04359SKristof Provost 235840e04359SKristof Provost if ((error = carp_is_supported_if(ifp)) != 0) 235940e04359SKristof Provost goto out; 236040e04359SKristof Provost 236140e04359SKristof Provost hdr->nlmsg_flags |= NLM_F_MULTI; 236240e04359SKristof Provost args.hdr = hdr; 236340e04359SKristof Provost args.npt = npt; 236440e04359SKristof Provost 236540e04359SKristof Provost carpr.carpr_vhid = attrs.vhid; 236640e04359SKristof Provost carpr.carpr_count = CARP_MAXVHID; 236740e04359SKristof Provost 236840e04359SKristof Provost sx_xlock(&carp_sx); 236940e04359SKristof Provost error = carp_ioctl_get(ifp, nlp_get_cred(npt->nlp), &carpr, 237040e04359SKristof Provost carp_nl_send, &args); 237140e04359SKristof Provost sx_xunlock(&carp_sx); 237240e04359SKristof Provost 237340e04359SKristof Provost if (! nlmsg_end_dump(npt->nw, error, hdr)) 237440e04359SKristof Provost error = ENOMEM; 237540e04359SKristof Provost 237640e04359SKristof Provost out: 237740e04359SKristof Provost if (ifp != NULL) 237840e04359SKristof Provost if_rele(ifp); 237940e04359SKristof Provost 238040e04359SKristof Provost return (error); 238140e04359SKristof Provost } 238240e04359SKristof Provost 238340e04359SKristof Provost static int 238440e04359SKristof Provost carp_nl_set(struct nlmsghdr *hdr, struct nl_pstate *npt) 238540e04359SKristof Provost { 238640e04359SKristof Provost struct nl_carp_parsed attrs = { }; 238713781800SKristof Provost struct carpkreq carpr; 238840e04359SKristof Provost struct epoch_tracker et; 238928921c4fSKristof Provost if_t ifp = NULL; 239040e04359SKristof Provost int error; 239140e04359SKristof Provost 239240e04359SKristof Provost error = nl_parse_nlmsg(hdr, &carp_parser, npt, &attrs); 239340e04359SKristof Provost if (error != 0) 239440e04359SKristof Provost return (error); 239540e04359SKristof Provost 239640e04359SKristof Provost if (attrs.vhid <= 0 || attrs.vhid > CARP_MAXVHID) 239740e04359SKristof Provost return (EINVAL); 239840e04359SKristof Provost if (attrs.state > CARP_MAXSTATE) 239940e04359SKristof Provost return (EINVAL); 240040e04359SKristof Provost if (attrs.advbase < 0 || attrs.advskew < 0) 240140e04359SKristof Provost return (EINVAL); 240240e04359SKristof Provost if (attrs.advbase > 255) 240340e04359SKristof Provost return (EINVAL); 240440e04359SKristof Provost if (attrs.advskew >= 255) 240540e04359SKristof Provost return (EINVAL); 240640e04359SKristof Provost 240740e04359SKristof Provost NET_EPOCH_ENTER(et); 240828921c4fSKristof Provost if (attrs.ifname != NULL) 240928921c4fSKristof Provost ifp = ifunit_ref(attrs.ifname); 241028921c4fSKristof Provost else if (attrs.ifindex != 0) 241140e04359SKristof Provost ifp = ifnet_byindex_ref(attrs.ifindex); 241240e04359SKristof Provost NET_EPOCH_EXIT(et); 241340e04359SKristof Provost 241440e04359SKristof Provost if ((error = carp_is_supported_if(ifp)) != 0) 241540e04359SKristof Provost goto out; 241640e04359SKristof Provost 241740e04359SKristof Provost if ((ifp->if_flags & IFF_MULTICAST) == 0) { 241840e04359SKristof Provost error = EADDRNOTAVAIL; 241940e04359SKristof Provost goto out; 242040e04359SKristof Provost } 242140e04359SKristof Provost 242240e04359SKristof Provost carpr.carpr_count = 1; 242340e04359SKristof Provost carpr.carpr_vhid = attrs.vhid; 242440e04359SKristof Provost carpr.carpr_state = attrs.state; 242540e04359SKristof Provost carpr.carpr_advbase = attrs.advbase; 242640e04359SKristof Provost carpr.carpr_advskew = attrs.advskew; 242713781800SKristof Provost carpr.carpr_addr = attrs.addr; 242813781800SKristof Provost carpr.carpr_addr6 = attrs.addr6; 242913781800SKristof Provost 243040e04359SKristof Provost memcpy(&carpr.carpr_key, &attrs.key, sizeof(attrs.key)); 243140e04359SKristof Provost 243240e04359SKristof Provost sx_xlock(&carp_sx); 243340e04359SKristof Provost error = carp_ioctl_set(ifp, &carpr); 243440e04359SKristof Provost sx_xunlock(&carp_sx); 243540e04359SKristof Provost 243640e04359SKristof Provost out: 243740e04359SKristof Provost if (ifp != NULL) 243840e04359SKristof Provost if_rele(ifp); 243940e04359SKristof Provost 244040e04359SKristof Provost return (error); 244140e04359SKristof Provost } 244240e04359SKristof Provost 244340e04359SKristof Provost static const struct nlhdr_parser *all_parsers[] = { 244440e04359SKristof Provost &carp_parser 244540e04359SKristof Provost }; 244640e04359SKristof Provost 244740e04359SKristof Provost static const struct genl_cmd carp_cmds[] = { 244840e04359SKristof Provost { 244940e04359SKristof Provost .cmd_num = CARP_NL_CMD_GET, 245040e04359SKristof Provost .cmd_name = "SIOCGVH", 245140e04359SKristof Provost .cmd_cb = carp_nl_get, 245240e04359SKristof Provost .cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_DUMP | 245340e04359SKristof Provost GENL_CMD_CAP_HASPOL, 245440e04359SKristof Provost }, 245540e04359SKristof Provost { 245640e04359SKristof Provost .cmd_num = CARP_NL_CMD_SET, 245740e04359SKristof Provost .cmd_name = "SIOCSVH", 245840e04359SKristof Provost .cmd_cb = carp_nl_set, 245940e04359SKristof Provost .cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_HASPOL, 246040e04359SKristof Provost .cmd_priv = PRIV_NETINET_CARP, 246140e04359SKristof Provost }, 246240e04359SKristof Provost }; 246340e04359SKristof Provost 246440e04359SKristof Provost static void 246540e04359SKristof Provost carp_nl_register(void) 246640e04359SKristof Provost { 246740e04359SKristof Provost bool ret __diagused; 246840e04359SKristof Provost int family_id __diagused; 246940e04359SKristof Provost 247040e04359SKristof Provost NL_VERIFY_PARSERS(all_parsers); 247140e04359SKristof Provost family_id = genl_register_family(CARP_NL_FAMILY_NAME, 0, 2, 247240e04359SKristof Provost CARP_NL_CMD_MAX); 247340e04359SKristof Provost MPASS(family_id != 0); 247440e04359SKristof Provost 247540e04359SKristof Provost ret = genl_register_cmds(CARP_NL_FAMILY_NAME, carp_cmds, 247640e04359SKristof Provost NL_ARRAY_LEN(carp_cmds)); 247740e04359SKristof Provost MPASS(ret); 247840e04359SKristof Provost } 247940e04359SKristof Provost 248040e04359SKristof Provost static void 248140e04359SKristof Provost carp_nl_unregister(void) 248240e04359SKristof Provost { 248340e04359SKristof Provost genl_unregister_family(CARP_NL_FAMILY_NAME); 248440e04359SKristof Provost } 248540e04359SKristof Provost 248654bfbd51SWill Andrews static void 248754bfbd51SWill Andrews carp_mod_cleanup(void) 2488a9771948SGleb Smirnoff { 248954bfbd51SWill Andrews 249040e04359SKristof Provost carp_nl_unregister(); 249140e04359SKristof Provost 249254bfbd51SWill Andrews #ifdef INET 249315249f73SWill Andrews (void)ipproto_unregister(IPPROTO_CARP); 249454bfbd51SWill Andrews carp_iamatch_p = NULL; 249554bfbd51SWill Andrews #endif 249654bfbd51SWill Andrews #ifdef INET6 249715249f73SWill Andrews (void)ip6proto_unregister(IPPROTO_CARP); 249854bfbd51SWill Andrews carp_iamatch6_p = NULL; 249954bfbd51SWill Andrews carp_macmatch6_p = NULL; 250054bfbd51SWill Andrews #endif 250108b68b0eSGleb Smirnoff carp_ioctl_p = NULL; 250208b68b0eSGleb Smirnoff carp_attach_p = NULL; 250308b68b0eSGleb Smirnoff carp_detach_p = NULL; 250408b68b0eSGleb Smirnoff carp_get_vhid_p = NULL; 250554bfbd51SWill Andrews carp_linkstate_p = NULL; 250654bfbd51SWill Andrews carp_forus_p = NULL; 250754bfbd51SWill Andrews carp_output_p = NULL; 2508f08535f8SGleb Smirnoff carp_demote_adj_p = NULL; 250924421c1cSGleb Smirnoff carp_master_p = NULL; 2510f08535f8SGleb Smirnoff mtx_unlock(&carp_mtx); 2511f08535f8SGleb Smirnoff taskqueue_drain(taskqueue_swi, &carp_sendall_task); 251254bfbd51SWill Andrews mtx_destroy(&carp_mtx); 251393d4534cSGleb Smirnoff sx_destroy(&carp_sx); 251454bfbd51SWill Andrews } 251554bfbd51SWill Andrews 2516ee49c5d3SBoris Lytochkin static void 2517ee49c5d3SBoris Lytochkin ipcarp_sysinit(void) 2518ee49c5d3SBoris Lytochkin { 2519ee49c5d3SBoris Lytochkin 2520ee49c5d3SBoris Lytochkin /* Load allow as tunable so to postpone carp start after module load */ 2521ee49c5d3SBoris Lytochkin TUNABLE_INT_FETCH("net.inet.carp.allow", &V_carp_allow); 2522ee49c5d3SBoris Lytochkin } 2523ee49c5d3SBoris Lytochkin VNET_SYSINIT(ip_carp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, ipcarp_sysinit, NULL); 2524ee49c5d3SBoris Lytochkin 252554bfbd51SWill Andrews static int 252654bfbd51SWill Andrews carp_mod_load(void) 252754bfbd51SWill Andrews { 252815249f73SWill Andrews int err; 252954bfbd51SWill Andrews 2530d92d54d5SGleb Smirnoff mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF); 253193d4534cSGleb Smirnoff sx_init(&carp_sx, "carp_sx"); 253208b68b0eSGleb Smirnoff LIST_INIT(&carp_list); 253308b68b0eSGleb Smirnoff carp_get_vhid_p = carp_get_vhid; 253454bfbd51SWill Andrews carp_forus_p = carp_forus; 253554bfbd51SWill Andrews carp_output_p = carp_output; 253608b68b0eSGleb Smirnoff carp_linkstate_p = carp_linkstate; 253708b68b0eSGleb Smirnoff carp_ioctl_p = carp_ioctl; 253808b68b0eSGleb Smirnoff carp_attach_p = carp_attach; 253908b68b0eSGleb Smirnoff carp_detach_p = carp_detach; 2540f08535f8SGleb Smirnoff carp_demote_adj_p = carp_demote_adj; 254124421c1cSGleb Smirnoff carp_master_p = carp_master; 254254bfbd51SWill Andrews #ifdef INET6 254354bfbd51SWill Andrews carp_iamatch6_p = carp_iamatch6; 254454bfbd51SWill Andrews carp_macmatch6_p = carp_macmatch6; 254578b1fc05SGleb Smirnoff err = ip6proto_register(IPPROTO_CARP, carp6_input, NULL); 254615249f73SWill Andrews if (err) { 254715249f73SWill Andrews printf("carp: error %d registering with INET6\n", err); 254815249f73SWill Andrews carp_mod_cleanup(); 25496baf7a24SGleb Smirnoff return (err); 255015249f73SWill Andrews } 255154bfbd51SWill Andrews #endif 255254bfbd51SWill Andrews #ifdef INET 255354bfbd51SWill Andrews carp_iamatch_p = carp_iamatch; 255478b1fc05SGleb Smirnoff err = ipproto_register(IPPROTO_CARP, carp_input, NULL); 255515249f73SWill Andrews if (err) { 255615249f73SWill Andrews printf("carp: error %d registering with INET\n", err); 255715249f73SWill Andrews carp_mod_cleanup(); 25586baf7a24SGleb Smirnoff return (err); 255915249f73SWill Andrews } 256054bfbd51SWill Andrews #endif 256140e04359SKristof Provost 256240e04359SKristof Provost carp_nl_register(); 256340e04359SKristof Provost 256408b68b0eSGleb Smirnoff return (0); 256554bfbd51SWill Andrews } 2566a9771948SGleb Smirnoff 256754bfbd51SWill Andrews static int 256854bfbd51SWill Andrews carp_modevent(module_t mod, int type, void *data) 256954bfbd51SWill Andrews { 257054bfbd51SWill Andrews switch (type) { 257154bfbd51SWill Andrews case MOD_LOAD: 257254bfbd51SWill Andrews return carp_mod_load(); 257354bfbd51SWill Andrews /* NOTREACHED */ 2574a9771948SGleb Smirnoff case MOD_UNLOAD: 257508b68b0eSGleb Smirnoff mtx_lock(&carp_mtx); 257608b68b0eSGleb Smirnoff if (LIST_EMPTY(&carp_list)) 257754bfbd51SWill Andrews carp_mod_cleanup(); 257808b68b0eSGleb Smirnoff else { 257908b68b0eSGleb Smirnoff mtx_unlock(&carp_mtx); 258054bfbd51SWill Andrews return (EBUSY); 258108b68b0eSGleb Smirnoff } 2582a9771948SGleb Smirnoff break; 2583a9771948SGleb Smirnoff 2584a9771948SGleb Smirnoff default: 25850fa08018SGleb Smirnoff return (EINVAL); 2586a9771948SGleb Smirnoff } 2587a9771948SGleb Smirnoff 25880fa08018SGleb Smirnoff return (0); 2589a9771948SGleb Smirnoff } 2590a9771948SGleb Smirnoff 2591a9771948SGleb Smirnoff static moduledata_t carp_mod = { 2592a9771948SGleb Smirnoff "carp", 2593a9771948SGleb Smirnoff carp_modevent, 25949823d527SKevin Lo 0 2595a9771948SGleb Smirnoff }; 2596a9771948SGleb Smirnoff 2597e24fa11dSWill Andrews DECLARE_MODULE(carp, carp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY); 2598