108b68b0eSGleb Smirnoff /*- 2fe267a55SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3fe267a55SPedro F. Giffuni * 408b68b0eSGleb Smirnoff * Copyright (c) 2002 Michael Shalayeff. 508b68b0eSGleb Smirnoff * Copyright (c) 2003 Ryan McBride. 608b68b0eSGleb Smirnoff * Copyright (c) 2011 Gleb Smirnoff <glebius@FreeBSD.org> 708b68b0eSGleb Smirnoff * All rights reserved. 8a9771948SGleb Smirnoff * 9a9771948SGleb Smirnoff * Redistribution and use in source and binary forms, with or without 10a9771948SGleb Smirnoff * modification, are permitted provided that the following conditions 11a9771948SGleb Smirnoff * are met: 12a9771948SGleb Smirnoff * 1. Redistributions of source code must retain the above copyright 13a9771948SGleb Smirnoff * notice, this list of conditions and the following disclaimer. 14a9771948SGleb Smirnoff * 2. Redistributions in binary form must reproduce the above copyright 15a9771948SGleb Smirnoff * notice, this list of conditions and the following disclaimer in the 16a9771948SGleb Smirnoff * documentation and/or other materials provided with the distribution. 17a9771948SGleb Smirnoff * 18a9771948SGleb Smirnoff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19a9771948SGleb Smirnoff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20a9771948SGleb Smirnoff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21a9771948SGleb Smirnoff * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT, 22a9771948SGleb Smirnoff * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23a9771948SGleb Smirnoff * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24a9771948SGleb Smirnoff * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25a9771948SGleb Smirnoff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26a9771948SGleb Smirnoff * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 27a9771948SGleb Smirnoff * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28a9771948SGleb Smirnoff * THE POSSIBILITY OF SUCH DAMAGE. 29a9771948SGleb Smirnoff */ 30a9771948SGleb Smirnoff 314b421e2dSMike Silbersack #include <sys/cdefs.h> 324b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 334b421e2dSMike Silbersack 34a9771948SGleb Smirnoff #include "opt_bpf.h" 35a9771948SGleb Smirnoff #include "opt_inet.h" 36a9771948SGleb Smirnoff #include "opt_inet6.h" 37a9771948SGleb Smirnoff 38a9771948SGleb Smirnoff #include <sys/param.h> 39a9771948SGleb Smirnoff #include <sys/systm.h> 4008b68b0eSGleb Smirnoff #include <sys/bus.h> 4108b68b0eSGleb Smirnoff #include <sys/jail.h> 42a9771948SGleb Smirnoff #include <sys/kernel.h> 43a9771948SGleb Smirnoff #include <sys/limits.h> 44a9771948SGleb Smirnoff #include <sys/malloc.h> 45a9771948SGleb Smirnoff #include <sys/mbuf.h> 46a9771948SGleb Smirnoff #include <sys/module.h> 47acd3428bSRobert Watson #include <sys/priv.h> 48a9771948SGleb Smirnoff #include <sys/proc.h> 4954bfbd51SWill Andrews #include <sys/protosw.h> 5008b68b0eSGleb Smirnoff #include <sys/socket.h> 5108b68b0eSGleb Smirnoff #include <sys/sockio.h> 52a9771948SGleb Smirnoff #include <sys/sysctl.h> 53a9771948SGleb Smirnoff #include <sys/syslog.h> 54f08535f8SGleb Smirnoff #include <sys/taskqueue.h> 5569edf037SAndrey V. Elsukov #include <sys/counter.h> 56a9771948SGleb Smirnoff 57a9771948SGleb Smirnoff #include <net/ethernet.h> 58a9771948SGleb Smirnoff #include <net/if.h> 5976039bc8SGleb Smirnoff #include <net/if_var.h> 60433aaf04SRuslan Ermilov #include <net/if_dl.h> 6108b68b0eSGleb Smirnoff #include <net/if_llatbl.h> 62a9771948SGleb Smirnoff #include <net/if_types.h> 63a9771948SGleb Smirnoff #include <net/route.h> 64530c0060SRobert Watson #include <net/vnet.h> 65a9771948SGleb Smirnoff 66a0ae8f04SBjoern A. Zeeb #if defined(INET) || defined(INET6) 67a9771948SGleb Smirnoff #include <netinet/in.h> 68a9771948SGleb Smirnoff #include <netinet/in_var.h> 69a0ae8f04SBjoern A. Zeeb #include <netinet/ip_carp.h> 70a9771948SGleb Smirnoff #include <netinet/ip.h> 71a0ae8f04SBjoern A. Zeeb #include <machine/in_cksum.h> 72a0ae8f04SBjoern A. Zeeb #endif 73a0ae8f04SBjoern A. Zeeb #ifdef INET 74a9771948SGleb Smirnoff #include <netinet/ip_var.h> 75a9771948SGleb Smirnoff #include <netinet/if_ether.h> 76a9771948SGleb Smirnoff #endif 77a9771948SGleb Smirnoff 78a9771948SGleb Smirnoff #ifdef INET6 79a9771948SGleb Smirnoff #include <netinet/icmp6.h> 80a9771948SGleb Smirnoff #include <netinet/ip6.h> 8108b68b0eSGleb Smirnoff #include <netinet6/in6_var.h> 82a9771948SGleb Smirnoff #include <netinet6/ip6_var.h> 8329da8af6SHajimu UMEMOTO #include <netinet6/scope6_var.h> 84a9771948SGleb Smirnoff #include <netinet6/nd6.h> 85a9771948SGleb Smirnoff #endif 86a9771948SGleb Smirnoff 87a9771948SGleb Smirnoff #include <crypto/sha1.h> 88a9771948SGleb Smirnoff 8908b68b0eSGleb Smirnoff static MALLOC_DEFINE(M_CARP, "CARP", "CARP addresses"); 90a9771948SGleb Smirnoff 91a9771948SGleb Smirnoff struct carp_softc { 9208b68b0eSGleb Smirnoff struct ifnet *sc_carpdev; /* Pointer to parent ifnet. */ 9308b68b0eSGleb Smirnoff struct ifaddr **sc_ifas; /* Our ifaddrs. */ 9408b68b0eSGleb Smirnoff struct sockaddr_dl sc_addr; /* Our link level address. */ 9508b68b0eSGleb Smirnoff struct callout sc_ad_tmo; /* Advertising timeout. */ 96a0ae8f04SBjoern A. Zeeb #ifdef INET 9708b68b0eSGleb Smirnoff struct callout sc_md_tmo; /* Master down timeout. */ 98a0ae8f04SBjoern A. Zeeb #endif 99a9771948SGleb Smirnoff #ifdef INET6 10008b68b0eSGleb Smirnoff struct callout sc_md6_tmo; /* XXX: Master down timeout. */ 10108b68b0eSGleb Smirnoff #endif 10208b68b0eSGleb Smirnoff struct mtx sc_mtx; 103a9771948SGleb Smirnoff 10408b68b0eSGleb Smirnoff int sc_vhid; 10508b68b0eSGleb Smirnoff int sc_advskew; 10608b68b0eSGleb Smirnoff int sc_advbase; 10708b68b0eSGleb Smirnoff 10808b68b0eSGleb Smirnoff int sc_naddrs; 10908b68b0eSGleb Smirnoff int sc_naddrs6; 11008b68b0eSGleb Smirnoff int sc_ifasiz; 111a9771948SGleb Smirnoff enum { INIT = 0, BACKUP, MASTER } sc_state; 112a9771948SGleb Smirnoff int sc_suppress; 113a9771948SGleb Smirnoff int sc_sendad_errors; 114a9771948SGleb Smirnoff #define CARP_SENDAD_MAX_ERRORS 3 115a9771948SGleb Smirnoff int sc_sendad_success; 116a9771948SGleb Smirnoff #define CARP_SENDAD_MIN_SUCCESS 3 117a9771948SGleb Smirnoff 118a9771948SGleb Smirnoff int sc_init_counter; 11908b68b0eSGleb Smirnoff uint64_t sc_counter; 120a9771948SGleb Smirnoff 121a9771948SGleb Smirnoff /* authentication */ 122a9771948SGleb Smirnoff #define CARP_HMAC_PAD 64 123a9771948SGleb Smirnoff unsigned char sc_key[CARP_KEY_LEN]; 124a9771948SGleb Smirnoff unsigned char sc_pad[CARP_HMAC_PAD]; 125a9771948SGleb Smirnoff SHA1_CTX sc_sha1; 126a9771948SGleb Smirnoff 12708b68b0eSGleb Smirnoff TAILQ_ENTRY(carp_softc) sc_list; /* On the carp_if list. */ 12808b68b0eSGleb Smirnoff LIST_ENTRY(carp_softc) sc_next; /* On the global list. */ 129a9771948SGleb Smirnoff }; 13008b68b0eSGleb Smirnoff 13108b68b0eSGleb Smirnoff struct carp_if { 13208b68b0eSGleb Smirnoff #ifdef INET 13308b68b0eSGleb Smirnoff int cif_naddrs; 13408b68b0eSGleb Smirnoff #endif 13508b68b0eSGleb Smirnoff #ifdef INET6 13608b68b0eSGleb Smirnoff int cif_naddrs6; 13708b68b0eSGleb Smirnoff #endif 13808b68b0eSGleb Smirnoff TAILQ_HEAD(, carp_softc) cif_vrs; 13908b68b0eSGleb Smirnoff #ifdef INET 14008b68b0eSGleb Smirnoff struct ip_moptions cif_imo; 14108b68b0eSGleb Smirnoff #endif 14208b68b0eSGleb Smirnoff #ifdef INET6 14308b68b0eSGleb Smirnoff struct ip6_moptions cif_im6o; 14408b68b0eSGleb Smirnoff #endif 14508b68b0eSGleb Smirnoff struct ifnet *cif_ifp; 14608b68b0eSGleb Smirnoff struct mtx cif_mtx; 1470cc726f2SGleb Smirnoff uint32_t cif_flags; 1480cc726f2SGleb Smirnoff #define CIF_PROMISC 0x00000001 14908b68b0eSGleb Smirnoff }; 15008b68b0eSGleb Smirnoff 15108b68b0eSGleb Smirnoff #define CARP_INET 0 15208b68b0eSGleb Smirnoff #define CARP_INET6 1 15308b68b0eSGleb Smirnoff static int proto_reg[] = {-1, -1}; 15408b68b0eSGleb Smirnoff 15508b68b0eSGleb Smirnoff /* 15608b68b0eSGleb Smirnoff * Brief design of carp(4). 15708b68b0eSGleb Smirnoff * 15808b68b0eSGleb Smirnoff * Any carp-capable ifnet may have a list of carp softcs hanging off 15908b68b0eSGleb Smirnoff * its ifp->if_carp pointer. Each softc represents one unique virtual 16008b68b0eSGleb Smirnoff * host id, or vhid. The softc has a back pointer to the ifnet. All 16108b68b0eSGleb Smirnoff * softcs are joined in a global list, which has quite limited use. 16208b68b0eSGleb Smirnoff * 16308b68b0eSGleb Smirnoff * Any interface address that takes part in CARP negotiation has a 16408b68b0eSGleb Smirnoff * pointer to the softc of its vhid, ifa->ifa_carp. That could be either 16508b68b0eSGleb Smirnoff * AF_INET or AF_INET6 address. 16608b68b0eSGleb Smirnoff * 16708b68b0eSGleb Smirnoff * Although, one can get the softc's backpointer to ifnet and traverse 16808b68b0eSGleb Smirnoff * through its ifp->if_addrhead queue to find all interface addresses 16908b68b0eSGleb Smirnoff * involved in CARP, we keep a growable array of ifaddr pointers. This 17008b68b0eSGleb Smirnoff * allows us to avoid grabbing the IF_ADDR_LOCK() in many traversals that 17108b68b0eSGleb Smirnoff * do calls into the network stack, thus avoiding LORs. 17208b68b0eSGleb Smirnoff * 17308b68b0eSGleb Smirnoff * Locking: 17408b68b0eSGleb Smirnoff * 17508b68b0eSGleb Smirnoff * Each softc has a lock sc_mtx. It is used to synchronise carp_input_c(), 17608b68b0eSGleb Smirnoff * callout-driven events and ioctl()s. 17708b68b0eSGleb Smirnoff * 17881098a01SAlexander Motin * To traverse the list of softcs on an ifnet we use CIF_LOCK() or carp_sx. 17981098a01SAlexander Motin * To traverse the global list we use the mutex carp_mtx. 18008b68b0eSGleb Smirnoff * 18108b68b0eSGleb Smirnoff * Known issues with locking: 18208b68b0eSGleb Smirnoff * 18308b68b0eSGleb Smirnoff * - Sending ad, we put the pointer to the softc in an mtag, and no reference 18408b68b0eSGleb Smirnoff * counting is done on the softc. 18508b68b0eSGleb Smirnoff * - On module unload we may race (?) with packet processing thread 18608b68b0eSGleb Smirnoff * dereferencing our function pointers. 18708b68b0eSGleb Smirnoff */ 188a9771948SGleb Smirnoff 189c5c392e7SMikolaj Golub /* Accept incoming CARP packets. */ 1905f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_allow) = 1; 191c5c392e7SMikolaj Golub #define V_carp_allow VNET(carp_allow) 192c5c392e7SMikolaj Golub 1930d3d234cSKristof Provost /* Set DSCP in outgoing CARP packets. */ 1945f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_dscp) = 56; 1950d3d234cSKristof Provost #define V_carp_dscp VNET(carp_dscp) 1960d3d234cSKristof Provost 197c5c392e7SMikolaj Golub /* Preempt slower nodes. */ 1985f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_preempt) = 0; 199c5c392e7SMikolaj Golub #define V_carp_preempt VNET(carp_preempt) 200c5c392e7SMikolaj Golub 201c5c392e7SMikolaj Golub /* Log level. */ 2025f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_log) = 1; 203c5c392e7SMikolaj Golub #define V_carp_log VNET(carp_log) 204c5c392e7SMikolaj Golub 205c5c392e7SMikolaj Golub /* Global advskew demotion. */ 2065f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_demotion) = 0; 207c5c392e7SMikolaj Golub #define V_carp_demotion VNET(carp_demotion) 208c5c392e7SMikolaj Golub 209c5c392e7SMikolaj Golub /* Send error demotion factor. */ 2105f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_senderr_adj) = CARP_MAXSKEW; 211c5c392e7SMikolaj Golub #define V_carp_senderr_adj VNET(carp_senderr_adj) 212c5c392e7SMikolaj Golub 213c5c392e7SMikolaj Golub /* Iface down demotion factor. */ 2145f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_ifdown_adj) = CARP_MAXSKEW; 215c5c392e7SMikolaj Golub #define V_carp_ifdown_adj VNET(carp_ifdown_adj) 216c5c392e7SMikolaj Golub 217167a3440SAlexander Motin static int carp_allow_sysctl(SYSCTL_HANDLER_ARGS); 2180d3d234cSKristof Provost static int carp_dscp_sysctl(SYSCTL_HANDLER_ARGS); 2197951008bSGleb Smirnoff static int carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS); 220a9771948SGleb Smirnoff 221f08535f8SGleb Smirnoff SYSCTL_NODE(_net_inet, IPPROTO_CARP, carp, CTLFLAG_RW, 0, "CARP"); 222167a3440SAlexander Motin SYSCTL_PROC(_net_inet_carp, OID_AUTO, allow, 223167a3440SAlexander Motin CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, 0, 0, carp_allow_sysctl, "I", 224167a3440SAlexander Motin "Accept incoming CARP packets"); 2250d3d234cSKristof Provost SYSCTL_PROC(_net_inet_carp, OID_AUTO, dscp, 2260d3d234cSKristof Provost CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, 0, 0, carp_dscp_sysctl, "I", 2270d3d234cSKristof Provost "DSCP value for carp packets"); 2286df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, preempt, CTLFLAG_VNET | CTLFLAG_RW, 229c5c392e7SMikolaj Golub &VNET_NAME(carp_preempt), 0, "High-priority backup preemption mode"); 2306df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, log, CTLFLAG_VNET | CTLFLAG_RW, 231c5c392e7SMikolaj Golub &VNET_NAME(carp_log), 0, "CARP log level"); 2326df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_carp, OID_AUTO, demotion, 2336df8a710SGleb Smirnoff CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, 2347951008bSGleb Smirnoff 0, 0, carp_demote_adj_sysctl, "I", 2357951008bSGleb Smirnoff "Adjust demotion factor (skew of advskew)"); 2366df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, senderr_demotion_factor, 2376df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, 238c5c392e7SMikolaj Golub &VNET_NAME(carp_senderr_adj), 0, "Send error demotion factor adjustment"); 2396df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, ifdown_demotion_factor, 2406df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, 241c5c392e7SMikolaj Golub &VNET_NAME(carp_ifdown_adj), 0, 242c5c392e7SMikolaj Golub "Interface down demotion factor adjustment"); 243f08535f8SGleb Smirnoff 244c5c392e7SMikolaj Golub VNET_PCPUSTAT_DEFINE(struct carpstats, carpstats); 245c5c392e7SMikolaj Golub VNET_PCPUSTAT_SYSINIT(carpstats); 246c5c392e7SMikolaj Golub VNET_PCPUSTAT_SYSUNINIT(carpstats); 247c5c392e7SMikolaj Golub 24869edf037SAndrey V. Elsukov #define CARPSTATS_ADD(name, val) \ 249c5c392e7SMikolaj Golub counter_u64_add(VNET(carpstats)[offsetof(struct carpstats, name) / \ 25069edf037SAndrey V. Elsukov sizeof(uint64_t)], (val)) 25169edf037SAndrey V. Elsukov #define CARPSTATS_INC(name) CARPSTATS_ADD(name, 1) 25269edf037SAndrey V. Elsukov 253c5c392e7SMikolaj Golub SYSCTL_VNET_PCPUSTAT(_net_inet_carp, OID_AUTO, stats, struct carpstats, 254c5c392e7SMikolaj Golub carpstats, "CARP statistics (struct carpstats, netinet/ip_carp.h)"); 255a9771948SGleb Smirnoff 25608b68b0eSGleb Smirnoff #define CARP_LOCK_INIT(sc) mtx_init(&(sc)->sc_mtx, "carp_softc", \ 257a9771948SGleb Smirnoff NULL, MTX_DEF) 25808b68b0eSGleb Smirnoff #define CARP_LOCK_DESTROY(sc) mtx_destroy(&(sc)->sc_mtx) 25908b68b0eSGleb Smirnoff #define CARP_LOCK_ASSERT(sc) mtx_assert(&(sc)->sc_mtx, MA_OWNED) 26008b68b0eSGleb Smirnoff #define CARP_LOCK(sc) mtx_lock(&(sc)->sc_mtx) 26108b68b0eSGleb Smirnoff #define CARP_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx) 26208b68b0eSGleb Smirnoff #define CIF_LOCK_INIT(cif) mtx_init(&(cif)->cif_mtx, "carp_if", \ 26308b68b0eSGleb Smirnoff NULL, MTX_DEF) 26408b68b0eSGleb Smirnoff #define CIF_LOCK_DESTROY(cif) mtx_destroy(&(cif)->cif_mtx) 26508b68b0eSGleb Smirnoff #define CIF_LOCK_ASSERT(cif) mtx_assert(&(cif)->cif_mtx, MA_OWNED) 26608b68b0eSGleb Smirnoff #define CIF_LOCK(cif) mtx_lock(&(cif)->cif_mtx) 26708b68b0eSGleb Smirnoff #define CIF_UNLOCK(cif) mtx_unlock(&(cif)->cif_mtx) 268a9a2c40cSGleb Smirnoff #define CIF_FREE(cif) do { \ 2699c2cd1aaSGleb Smirnoff CIF_LOCK(cif); \ 270a9a2c40cSGleb Smirnoff if (TAILQ_EMPTY(&(cif)->cif_vrs)) \ 271a9a2c40cSGleb Smirnoff carp_free_if(cif); \ 272a9a2c40cSGleb Smirnoff else \ 273a9a2c40cSGleb Smirnoff CIF_UNLOCK(cif); \ 274a9a2c40cSGleb Smirnoff } while (0) 275d220759bSGleb Smirnoff 276947b7cf3SGleb Smirnoff #define CARP_LOG(...) do { \ 277c5c392e7SMikolaj Golub if (V_carp_log > 0) \ 27808b68b0eSGleb Smirnoff log(LOG_INFO, "carp: " __VA_ARGS__); \ 279947b7cf3SGleb Smirnoff } while (0) 2801e9e6572SGleb Smirnoff 281947b7cf3SGleb Smirnoff #define CARP_DEBUG(...) do { \ 282c5c392e7SMikolaj Golub if (V_carp_log > 1) \ 2831e9e6572SGleb Smirnoff log(LOG_DEBUG, __VA_ARGS__); \ 284947b7cf3SGleb Smirnoff } while (0) 285a9771948SGleb Smirnoff 28608b68b0eSGleb Smirnoff #define IFNET_FOREACH_IFA(ifp, ifa) \ 287d7c5a620SMatt Macy CK_STAILQ_FOREACH((ifa), &(ifp)->if_addrhead, ifa_link) \ 28808b68b0eSGleb Smirnoff if ((ifa)->ifa_carp != NULL) 28908b68b0eSGleb Smirnoff 29008b68b0eSGleb Smirnoff #define CARP_FOREACH_IFA(sc, ifa) \ 29108b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); \ 29208b68b0eSGleb Smirnoff for (int _i = 0; \ 29308b68b0eSGleb Smirnoff _i < (sc)->sc_naddrs + (sc)->sc_naddrs6 && \ 29408b68b0eSGleb Smirnoff ((ifa) = sc->sc_ifas[_i]) != NULL; \ 29508b68b0eSGleb Smirnoff ++_i) 29608b68b0eSGleb Smirnoff 29708b68b0eSGleb Smirnoff #define IFNET_FOREACH_CARP(ifp, sc) \ 29881098a01SAlexander Motin KASSERT(mtx_owned(&ifp->if_carp->cif_mtx) || \ 29981098a01SAlexander Motin sx_xlocked(&carp_sx), ("cif_vrs not locked")); \ 30008b68b0eSGleb Smirnoff TAILQ_FOREACH((sc), &(ifp)->if_carp->cif_vrs, sc_list) 30108b68b0eSGleb Smirnoff 302f08535f8SGleb Smirnoff #define DEMOTE_ADVSKEW(sc) \ 303c5c392e7SMikolaj Golub (((sc)->sc_advskew + V_carp_demotion > CARP_MAXSKEW) ? \ 304c5c392e7SMikolaj Golub CARP_MAXSKEW : ((sc)->sc_advskew + V_carp_demotion)) 305f08535f8SGleb Smirnoff 3065c1f0f6dSGleb Smirnoff static void carp_input_c(struct mbuf *, struct carp_header *, sa_family_t); 30708b68b0eSGleb Smirnoff static struct carp_softc 30808b68b0eSGleb Smirnoff *carp_alloc(struct ifnet *); 30908b68b0eSGleb Smirnoff static void carp_destroy(struct carp_softc *); 31008b68b0eSGleb Smirnoff static struct carp_if 31108b68b0eSGleb Smirnoff *carp_alloc_if(struct ifnet *); 31208b68b0eSGleb Smirnoff static void carp_free_if(struct carp_if *); 313d01641e2SWill Andrews static void carp_set_state(struct carp_softc *, int, const char* reason); 31408b68b0eSGleb Smirnoff static void carp_sc_state(struct carp_softc *); 31508b68b0eSGleb Smirnoff static void carp_setrun(struct carp_softc *, sa_family_t); 3165c1f0f6dSGleb Smirnoff static void carp_master_down(void *); 317d01641e2SWill Andrews static void carp_master_down_locked(struct carp_softc *, 318d01641e2SWill Andrews const char* reason); 31908b68b0eSGleb Smirnoff static void carp_send_ad(void *); 32008b68b0eSGleb Smirnoff static void carp_send_ad_locked(struct carp_softc *); 32108b68b0eSGleb Smirnoff static void carp_addroute(struct carp_softc *); 3222512b096SGleb Smirnoff static void carp_ifa_addroute(struct ifaddr *); 32308b68b0eSGleb Smirnoff static void carp_delroute(struct carp_softc *); 3242512b096SGleb Smirnoff static void carp_ifa_delroute(struct ifaddr *); 325f08535f8SGleb Smirnoff static void carp_send_ad_all(void *, int); 326f08535f8SGleb Smirnoff static void carp_demote_adj(int, char *); 327a9771948SGleb Smirnoff 32808b68b0eSGleb Smirnoff static LIST_HEAD(, carp_softc) carp_list; 329d92d54d5SGleb Smirnoff static struct mtx carp_mtx; 33093d4534cSGleb Smirnoff static struct sx carp_sx; 331f08535f8SGleb Smirnoff static struct task carp_sendall_task = 332f08535f8SGleb Smirnoff TASK_INITIALIZER(0, carp_send_ad_all, NULL); 333a9771948SGleb Smirnoff 3345c1f0f6dSGleb Smirnoff static void 335a9771948SGleb Smirnoff carp_hmac_prepare(struct carp_softc *sc) 336a9771948SGleb Smirnoff { 33708b68b0eSGleb Smirnoff uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT; 33808b68b0eSGleb Smirnoff uint8_t vhid = sc->sc_vhid & 0xff; 339a9771948SGleb Smirnoff struct ifaddr *ifa; 3401ead26d4SMax Laier int i, found; 3411ead26d4SMax Laier #ifdef INET 3421ead26d4SMax Laier struct in_addr last, cur, in; 3431ead26d4SMax Laier #endif 344a9771948SGleb Smirnoff #ifdef INET6 3451ead26d4SMax Laier struct in6_addr last6, cur6, in6; 346a9771948SGleb Smirnoff #endif 347a9771948SGleb Smirnoff 34808b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); 349d220759bSGleb Smirnoff 35008b68b0eSGleb Smirnoff /* Compute ipad from key. */ 351a9771948SGleb Smirnoff bzero(sc->sc_pad, sizeof(sc->sc_pad)); 352a9771948SGleb Smirnoff bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key)); 353a9771948SGleb Smirnoff for (i = 0; i < sizeof(sc->sc_pad); i++) 354a9771948SGleb Smirnoff sc->sc_pad[i] ^= 0x36; 355a9771948SGleb Smirnoff 35608b68b0eSGleb Smirnoff /* Precompute first part of inner hash. */ 357a9771948SGleb Smirnoff SHA1Init(&sc->sc_sha1); 358a9771948SGleb Smirnoff SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad)); 359a9771948SGleb Smirnoff SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version)); 360a9771948SGleb Smirnoff SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type)); 361a9771948SGleb Smirnoff SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid)); 362a9771948SGleb Smirnoff #ifdef INET 3631ead26d4SMax Laier cur.s_addr = 0; 3641ead26d4SMax Laier do { 3651ead26d4SMax Laier found = 0; 3661ead26d4SMax Laier last = cur; 3671ead26d4SMax Laier cur.s_addr = 0xffffffff; 36808b68b0eSGleb Smirnoff CARP_FOREACH_IFA(sc, ifa) { 3691ead26d4SMax Laier in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr; 3701ead26d4SMax Laier if (ifa->ifa_addr->sa_family == AF_INET && 3711ead26d4SMax Laier ntohl(in.s_addr) > ntohl(last.s_addr) && 3721ead26d4SMax Laier ntohl(in.s_addr) < ntohl(cur.s_addr)) { 3731ead26d4SMax Laier cur.s_addr = in.s_addr; 3741ead26d4SMax Laier found++; 375a9771948SGleb Smirnoff } 3761ead26d4SMax Laier } 3771ead26d4SMax Laier if (found) 3781ead26d4SMax Laier SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur)); 3791ead26d4SMax Laier } while (found); 380a9771948SGleb Smirnoff #endif /* INET */ 381a9771948SGleb Smirnoff #ifdef INET6 3821ead26d4SMax Laier memset(&cur6, 0, sizeof(cur6)); 3831ead26d4SMax Laier do { 3841ead26d4SMax Laier found = 0; 3851ead26d4SMax Laier last6 = cur6; 3861ead26d4SMax Laier memset(&cur6, 0xff, sizeof(cur6)); 38708b68b0eSGleb Smirnoff CARP_FOREACH_IFA(sc, ifa) { 388a9771948SGleb Smirnoff in6 = ifatoia6(ifa)->ia_addr.sin6_addr; 3891ead26d4SMax Laier if (IN6_IS_SCOPE_EMBED(&in6)) 3901ead26d4SMax Laier in6.s6_addr16[1] = 0; 3911ead26d4SMax Laier if (ifa->ifa_addr->sa_family == AF_INET6 && 3921ead26d4SMax Laier memcmp(&in6, &last6, sizeof(in6)) > 0 && 3931ead26d4SMax Laier memcmp(&in6, &cur6, sizeof(in6)) < 0) { 3941ead26d4SMax Laier cur6 = in6; 3951ead26d4SMax Laier found++; 396a9771948SGleb Smirnoff } 397a9771948SGleb Smirnoff } 3981ead26d4SMax Laier if (found) 3991ead26d4SMax Laier SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6)); 4001ead26d4SMax Laier } while (found); 401a9771948SGleb Smirnoff #endif /* INET6 */ 402a9771948SGleb Smirnoff 403a9771948SGleb Smirnoff /* convert ipad to opad */ 404a9771948SGleb Smirnoff for (i = 0; i < sizeof(sc->sc_pad); i++) 405a9771948SGleb Smirnoff sc->sc_pad[i] ^= 0x36 ^ 0x5c; 406a9771948SGleb Smirnoff } 407a9771948SGleb Smirnoff 4085c1f0f6dSGleb Smirnoff static void 40908b68b0eSGleb Smirnoff carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2], 410a9771948SGleb Smirnoff unsigned char md[20]) 411a9771948SGleb Smirnoff { 412a9771948SGleb Smirnoff SHA1_CTX sha1ctx; 413a9771948SGleb Smirnoff 41408b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); 41508b68b0eSGleb Smirnoff 416a9771948SGleb Smirnoff /* fetch first half of inner hash */ 417a9771948SGleb Smirnoff bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx)); 418a9771948SGleb Smirnoff 419a9771948SGleb Smirnoff SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter)); 420a9771948SGleb Smirnoff SHA1Final(md, &sha1ctx); 421a9771948SGleb Smirnoff 422a9771948SGleb Smirnoff /* outer hash */ 423a9771948SGleb Smirnoff SHA1Init(&sha1ctx); 424a9771948SGleb Smirnoff SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad)); 425a9771948SGleb Smirnoff SHA1Update(&sha1ctx, md, 20); 426a9771948SGleb Smirnoff SHA1Final(md, &sha1ctx); 427a9771948SGleb Smirnoff } 428a9771948SGleb Smirnoff 4295c1f0f6dSGleb Smirnoff static int 43008b68b0eSGleb Smirnoff carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2], 431a9771948SGleb Smirnoff unsigned char md[20]) 432a9771948SGleb Smirnoff { 433a9771948SGleb Smirnoff unsigned char md2[20]; 434a9771948SGleb Smirnoff 43508b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); 436d220759bSGleb Smirnoff 437a9771948SGleb Smirnoff carp_hmac_generate(sc, counter, md2); 438a9771948SGleb Smirnoff 439a9771948SGleb Smirnoff return (bcmp(md, md2, sizeof(md2))); 440a9771948SGleb Smirnoff } 441a9771948SGleb Smirnoff 442a9771948SGleb Smirnoff /* 443a9771948SGleb Smirnoff * process input packet. 444a9771948SGleb Smirnoff * we have rearranged checks order compared to the rfc, 445a9771948SGleb Smirnoff * but it seems more efficient this way or not possible otherwise. 446a9771948SGleb Smirnoff */ 447a0ae8f04SBjoern A. Zeeb #ifdef INET 4488f5a8818SKevin Lo int 4498f5a8818SKevin Lo carp_input(struct mbuf **mp, int *offp, int proto) 450a9771948SGleb Smirnoff { 4518f5a8818SKevin Lo struct mbuf *m = *mp; 452a9771948SGleb Smirnoff struct ip *ip = mtod(m, struct ip *); 453a9771948SGleb Smirnoff struct carp_header *ch; 454a9771948SGleb Smirnoff int iplen, len; 455a9771948SGleb Smirnoff 4568f5a8818SKevin Lo iplen = *offp; 4578f5a8818SKevin Lo *mp = NULL; 4588f5a8818SKevin Lo 4596bf65bcfSRobert Watson CARPSTATS_INC(carps_ipackets); 460a9771948SGleb Smirnoff 461c5c392e7SMikolaj Golub if (!V_carp_allow) { 462a9771948SGleb Smirnoff m_freem(m); 4638f5a8818SKevin Lo return (IPPROTO_DONE); 464a9771948SGleb Smirnoff } 465a9771948SGleb Smirnoff 466a9771948SGleb Smirnoff /* verify that the IP TTL is 255. */ 467a9771948SGleb Smirnoff if (ip->ip_ttl != CARP_DFLTTL) { 4686bf65bcfSRobert Watson CARPSTATS_INC(carps_badttl); 46908b68b0eSGleb Smirnoff CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__, 4701e9e6572SGleb Smirnoff ip->ip_ttl, 4711e9e6572SGleb Smirnoff m->m_pkthdr.rcvif->if_xname); 472a9771948SGleb Smirnoff m_freem(m); 4738f5a8818SKevin Lo return (IPPROTO_DONE); 474a9771948SGleb Smirnoff } 475a9771948SGleb Smirnoff 476a9771948SGleb Smirnoff iplen = ip->ip_hl << 2; 477a9771948SGleb Smirnoff 478a9771948SGleb Smirnoff if (m->m_pkthdr.len < iplen + sizeof(*ch)) { 4796bf65bcfSRobert Watson CARPSTATS_INC(carps_badlen); 48008b68b0eSGleb Smirnoff CARP_DEBUG("%s: received len %zd < sizeof(struct carp_header) " 48108b68b0eSGleb Smirnoff "on %s\n", __func__, m->m_len - sizeof(struct ip), 48216e324fcSXin LI m->m_pkthdr.rcvif->if_xname); 483a9771948SGleb Smirnoff m_freem(m); 4848f5a8818SKevin Lo return (IPPROTO_DONE); 485a9771948SGleb Smirnoff } 486a9771948SGleb Smirnoff 487a9771948SGleb Smirnoff if (iplen + sizeof(*ch) < m->m_len) { 488a9771948SGleb Smirnoff if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) { 4896bf65bcfSRobert Watson CARPSTATS_INC(carps_hdrops); 49008b68b0eSGleb Smirnoff CARP_DEBUG("%s: pullup failed\n", __func__); 4918f5a8818SKevin Lo return (IPPROTO_DONE); 492a9771948SGleb Smirnoff } 493a9771948SGleb Smirnoff ip = mtod(m, struct ip *); 494a9771948SGleb Smirnoff } 495a9771948SGleb Smirnoff ch = (struct carp_header *)((char *)ip + iplen); 496a9771948SGleb Smirnoff 497a9771948SGleb Smirnoff /* 498a9771948SGleb Smirnoff * verify that the received packet length is 499a9771948SGleb Smirnoff * equal to the CARP header 500a9771948SGleb Smirnoff */ 501a9771948SGleb Smirnoff len = iplen + sizeof(*ch); 502a9771948SGleb Smirnoff if (len > m->m_pkthdr.len) { 5036bf65bcfSRobert Watson CARPSTATS_INC(carps_badlen); 50408b68b0eSGleb Smirnoff CARP_DEBUG("%s: packet too short %d on %s\n", __func__, 5051e9e6572SGleb Smirnoff m->m_pkthdr.len, 5061e9e6572SGleb Smirnoff m->m_pkthdr.rcvif->if_xname); 507a9771948SGleb Smirnoff m_freem(m); 5088f5a8818SKevin Lo return (IPPROTO_DONE); 509a9771948SGleb Smirnoff } 510a9771948SGleb Smirnoff 511a9771948SGleb Smirnoff if ((m = m_pullup(m, len)) == NULL) { 5126bf65bcfSRobert Watson CARPSTATS_INC(carps_hdrops); 5138f5a8818SKevin Lo return (IPPROTO_DONE); 514a9771948SGleb Smirnoff } 515a9771948SGleb Smirnoff ip = mtod(m, struct ip *); 516a9771948SGleb Smirnoff ch = (struct carp_header *)((char *)ip + iplen); 517a9771948SGleb Smirnoff 518a9771948SGleb Smirnoff /* verify the CARP checksum */ 519a9771948SGleb Smirnoff m->m_data += iplen; 520c4d06976SGleb Smirnoff if (in_cksum(m, len - iplen)) { 5216bf65bcfSRobert Watson CARPSTATS_INC(carps_badsum); 52208b68b0eSGleb Smirnoff CARP_DEBUG("%s: checksum failed on %s\n", __func__, 5231e9e6572SGleb Smirnoff m->m_pkthdr.rcvif->if_xname); 524a9771948SGleb Smirnoff m_freem(m); 5258f5a8818SKevin Lo return (IPPROTO_DONE); 526a9771948SGleb Smirnoff } 527a9771948SGleb Smirnoff m->m_data -= iplen; 528a9771948SGleb Smirnoff 5291e9e6572SGleb Smirnoff carp_input_c(m, ch, AF_INET); 5308f5a8818SKevin Lo return (IPPROTO_DONE); 531a9771948SGleb Smirnoff } 532a0ae8f04SBjoern A. Zeeb #endif 533a9771948SGleb Smirnoff 534a9771948SGleb Smirnoff #ifdef INET6 535a9771948SGleb Smirnoff int 536a9771948SGleb Smirnoff carp6_input(struct mbuf **mp, int *offp, int proto) 537a9771948SGleb Smirnoff { 538a9771948SGleb Smirnoff struct mbuf *m = *mp; 539a9771948SGleb Smirnoff struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 540a9771948SGleb Smirnoff struct carp_header *ch; 541a9771948SGleb Smirnoff u_int len; 542a9771948SGleb Smirnoff 5436bf65bcfSRobert Watson CARPSTATS_INC(carps_ipackets6); 544a9771948SGleb Smirnoff 545c5c392e7SMikolaj Golub if (!V_carp_allow) { 546a9771948SGleb Smirnoff m_freem(m); 547a9771948SGleb Smirnoff return (IPPROTO_DONE); 548a9771948SGleb Smirnoff } 549a9771948SGleb Smirnoff 550a9771948SGleb Smirnoff /* check if received on a valid carp interface */ 551a9771948SGleb Smirnoff if (m->m_pkthdr.rcvif->if_carp == NULL) { 5526bf65bcfSRobert Watson CARPSTATS_INC(carps_badif); 55308b68b0eSGleb Smirnoff CARP_DEBUG("%s: packet received on non-carp interface: %s\n", 55408b68b0eSGleb Smirnoff __func__, m->m_pkthdr.rcvif->if_xname); 555a9771948SGleb Smirnoff m_freem(m); 556a9771948SGleb Smirnoff return (IPPROTO_DONE); 557a9771948SGleb Smirnoff } 558a9771948SGleb Smirnoff 559a9771948SGleb Smirnoff /* verify that the IP TTL is 255 */ 560a9771948SGleb Smirnoff if (ip6->ip6_hlim != CARP_DFLTTL) { 5616bf65bcfSRobert Watson CARPSTATS_INC(carps_badttl); 56208b68b0eSGleb Smirnoff CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__, 56308b68b0eSGleb Smirnoff ip6->ip6_hlim, m->m_pkthdr.rcvif->if_xname); 564a9771948SGleb Smirnoff m_freem(m); 565a9771948SGleb Smirnoff return (IPPROTO_DONE); 566a9771948SGleb Smirnoff } 567a9771948SGleb Smirnoff 568a9771948SGleb Smirnoff /* verify that we have a complete carp packet */ 569a4adf6ccSBjoern A. Zeeb if (m->m_len < *offp + sizeof(*ch)) { 570a9771948SGleb Smirnoff len = m->m_len; 57163abacc2SBjoern A. Zeeb m = m_pullup(m, *offp + sizeof(*ch)); 57263abacc2SBjoern A. Zeeb if (m == NULL) { 5736bf65bcfSRobert Watson CARPSTATS_INC(carps_badlen); 57408b68b0eSGleb Smirnoff CARP_DEBUG("%s: packet size %u too small\n", __func__, len); 575a9771948SGleb Smirnoff return (IPPROTO_DONE); 576a9771948SGleb Smirnoff } 577a4adf6ccSBjoern A. Zeeb } 578dad68fc3SBjoern A. Zeeb ch = (struct carp_header *)(mtod(m, char *) + *offp); 579a9771948SGleb Smirnoff 580a9771948SGleb Smirnoff 581a9771948SGleb Smirnoff /* verify the CARP checksum */ 582a9771948SGleb Smirnoff m->m_data += *offp; 583c4d06976SGleb Smirnoff if (in_cksum(m, sizeof(*ch))) { 5846bf65bcfSRobert Watson CARPSTATS_INC(carps_badsum); 58508b68b0eSGleb Smirnoff CARP_DEBUG("%s: checksum failed, on %s\n", __func__, 5861e9e6572SGleb Smirnoff m->m_pkthdr.rcvif->if_xname); 587a9771948SGleb Smirnoff m_freem(m); 588a9771948SGleb Smirnoff return (IPPROTO_DONE); 589a9771948SGleb Smirnoff } 590a9771948SGleb Smirnoff m->m_data -= *offp; 591a9771948SGleb Smirnoff 5921e9e6572SGleb Smirnoff carp_input_c(m, ch, AF_INET6); 593a9771948SGleb Smirnoff return (IPPROTO_DONE); 594a9771948SGleb Smirnoff } 595a9771948SGleb Smirnoff #endif /* INET6 */ 596a9771948SGleb Smirnoff 5978151740cSJosh Paetzel /* 5988151740cSJosh Paetzel * This routine should not be necessary at all, but some switches 5998151740cSJosh Paetzel * (VMWare ESX vswitches) can echo our own packets back at us, 6008151740cSJosh Paetzel * and we must ignore them or they will cause us to drop out of 6018151740cSJosh Paetzel * MASTER mode. 6028151740cSJosh Paetzel * 6038151740cSJosh Paetzel * We cannot catch all cases of network loops. Instead, what we 6048151740cSJosh Paetzel * do here is catch any packet that arrives with a carp header 6058151740cSJosh Paetzel * with a VHID of 0, that comes from an address that is our own. 6068151740cSJosh Paetzel * These packets are by definition "from us" (even if they are from 6078151740cSJosh Paetzel * a misconfigured host that is pretending to be us). 6088151740cSJosh Paetzel * 6098151740cSJosh Paetzel * The VHID test is outside this mini-function. 6108151740cSJosh Paetzel */ 6118151740cSJosh Paetzel static int 6128151740cSJosh Paetzel carp_source_is_self(struct mbuf *m, struct ifaddr *ifa, sa_family_t af) 6138151740cSJosh Paetzel { 614cfff8d3dSEnji Cooper #ifdef INET 6158151740cSJosh Paetzel struct ip *ip4; 6168151740cSJosh Paetzel struct in_addr in4; 617cfff8d3dSEnji Cooper #endif 618cfff8d3dSEnji Cooper #ifdef INET6 6198151740cSJosh Paetzel struct ip6_hdr *ip6; 6208151740cSJosh Paetzel struct in6_addr in6; 621cfff8d3dSEnji Cooper #endif 6228151740cSJosh Paetzel 6238151740cSJosh Paetzel switch (af) { 624cfff8d3dSEnji Cooper #ifdef INET 6258151740cSJosh Paetzel case AF_INET: 6268151740cSJosh Paetzel ip4 = mtod(m, struct ip *); 6278151740cSJosh Paetzel in4 = ifatoia(ifa)->ia_addr.sin_addr; 6288151740cSJosh Paetzel return (in4.s_addr == ip4->ip_src.s_addr); 629cfff8d3dSEnji Cooper #endif 630cfff8d3dSEnji Cooper #ifdef INET6 6318151740cSJosh Paetzel case AF_INET6: 6328151740cSJosh Paetzel ip6 = mtod(m, struct ip6_hdr *); 6338151740cSJosh Paetzel in6 = ifatoia6(ifa)->ia_addr.sin6_addr; 6348151740cSJosh Paetzel return (memcmp(&in6, &ip6->ip6_src, sizeof(in6)) == 0); 635cfff8d3dSEnji Cooper #endif 636cfff8d3dSEnji Cooper default: 6378151740cSJosh Paetzel break; 6388151740cSJosh Paetzel } 6398151740cSJosh Paetzel return (0); 6408151740cSJosh Paetzel } 6418151740cSJosh Paetzel 6425c1f0f6dSGleb Smirnoff static void 6431e9e6572SGleb Smirnoff carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af) 644a9771948SGleb Smirnoff { 645a9771948SGleb Smirnoff struct ifnet *ifp = m->m_pkthdr.rcvif; 6468151740cSJosh Paetzel struct ifaddr *ifa, *match; 6471e9e6572SGleb Smirnoff struct carp_softc *sc; 64808b68b0eSGleb Smirnoff uint64_t tmp_counter; 649a9771948SGleb Smirnoff struct timeval sc_tv, ch_tv; 6508151740cSJosh Paetzel int error; 651a9771948SGleb Smirnoff 652b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 653b8a6e03fSGleb Smirnoff 6548151740cSJosh Paetzel /* 6558151740cSJosh Paetzel * Verify that the VHID is valid on the receiving interface. 6568151740cSJosh Paetzel * 6578151740cSJosh Paetzel * There should be just one match. If there are none 6588151740cSJosh Paetzel * the VHID is not valid and we drop the packet. If 6598151740cSJosh Paetzel * there are multiple VHID matches, take just the first 6608151740cSJosh Paetzel * one, for compatibility with previous code. While we're 6618151740cSJosh Paetzel * scanning, check for obvious loops in the network topology 6628151740cSJosh Paetzel * (these should never happen, and as noted above, we may 6638151740cSJosh Paetzel * miss real loops; this is just a double-check). 6648151740cSJosh Paetzel */ 6658151740cSJosh Paetzel error = 0; 6668151740cSJosh Paetzel match = NULL; 6678151740cSJosh Paetzel IFNET_FOREACH_IFA(ifp, ifa) { 6688151740cSJosh Paetzel if (match == NULL && ifa->ifa_carp != NULL && 6698151740cSJosh Paetzel ifa->ifa_addr->sa_family == af && 6708151740cSJosh Paetzel ifa->ifa_carp->sc_vhid == ch->carp_vhid) 6718151740cSJosh Paetzel match = ifa; 6728151740cSJosh Paetzel if (ch->carp_vhid == 0 && carp_source_is_self(m, ifa, af)) 6738151740cSJosh Paetzel error = ELOOP; 67408b68b0eSGleb Smirnoff } 6758151740cSJosh Paetzel ifa = error ? NULL : match; 6768151740cSJosh Paetzel if (ifa != NULL) 6778151740cSJosh Paetzel ifa_ref(ifa); 678d220759bSGleb Smirnoff 67908b68b0eSGleb Smirnoff if (ifa == NULL) { 6808151740cSJosh Paetzel if (error == ELOOP) { 6818151740cSJosh Paetzel CARP_DEBUG("dropping looped packet on interface %s\n", 6828151740cSJosh Paetzel ifp->if_xname); 6838151740cSJosh Paetzel CARPSTATS_INC(carps_badif); /* ??? */ 6848151740cSJosh Paetzel } else { 6856bf65bcfSRobert Watson CARPSTATS_INC(carps_badvhid); 6868151740cSJosh Paetzel } 687a9771948SGleb Smirnoff m_freem(m); 688a9771948SGleb Smirnoff return; 689a9771948SGleb Smirnoff } 690a9771948SGleb Smirnoff 691a9771948SGleb Smirnoff /* verify the CARP version. */ 692a9771948SGleb Smirnoff if (ch->carp_version != CARP_VERSION) { 6936bf65bcfSRobert Watson CARPSTATS_INC(carps_badver); 69408b68b0eSGleb Smirnoff CARP_DEBUG("%s: invalid version %d\n", ifp->if_xname, 6951e9e6572SGleb Smirnoff ch->carp_version); 69608b68b0eSGleb Smirnoff ifa_free(ifa); 697a9771948SGleb Smirnoff m_freem(m); 698a9771948SGleb Smirnoff return; 699a9771948SGleb Smirnoff } 700a9771948SGleb Smirnoff 70108b68b0eSGleb Smirnoff sc = ifa->ifa_carp; 70208b68b0eSGleb Smirnoff CARP_LOCK(sc); 70308b68b0eSGleb Smirnoff ifa_free(ifa); 70408b68b0eSGleb Smirnoff 705a9771948SGleb Smirnoff if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) { 7066bf65bcfSRobert Watson CARPSTATS_INC(carps_badauth); 70708b68b0eSGleb Smirnoff CARP_DEBUG("%s: incorrect hash for VHID %u@%s\n", __func__, 70808b68b0eSGleb Smirnoff sc->sc_vhid, ifp->if_xname); 70908b68b0eSGleb Smirnoff goto out; 710a9771948SGleb Smirnoff } 711a9771948SGleb Smirnoff 712a9771948SGleb Smirnoff tmp_counter = ntohl(ch->carp_counter[0]); 713a9771948SGleb Smirnoff tmp_counter = tmp_counter<<32; 714a9771948SGleb Smirnoff tmp_counter += ntohl(ch->carp_counter[1]); 715a9771948SGleb Smirnoff 716a9771948SGleb Smirnoff /* XXX Replay protection goes here */ 717a9771948SGleb Smirnoff 718a9771948SGleb Smirnoff sc->sc_init_counter = 0; 719a9771948SGleb Smirnoff sc->sc_counter = tmp_counter; 720a9771948SGleb Smirnoff 721a9771948SGleb Smirnoff sc_tv.tv_sec = sc->sc_advbase; 722f08535f8SGleb Smirnoff sc_tv.tv_usec = DEMOTE_ADVSKEW(sc) * 1000000 / 256; 723a9771948SGleb Smirnoff ch_tv.tv_sec = ch->carp_advbase; 724a9771948SGleb Smirnoff ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256; 725a9771948SGleb Smirnoff 726a9771948SGleb Smirnoff switch (sc->sc_state) { 727a9771948SGleb Smirnoff case INIT: 728a9771948SGleb Smirnoff break; 729a9771948SGleb Smirnoff case MASTER: 730a9771948SGleb Smirnoff /* 731a9771948SGleb Smirnoff * If we receive an advertisement from a master who's going to 732a9771948SGleb Smirnoff * be more frequent than us, go into BACKUP state. 733a9771948SGleb Smirnoff */ 734a9771948SGleb Smirnoff if (timevalcmp(&sc_tv, &ch_tv, >) || 735a9771948SGleb Smirnoff timevalcmp(&sc_tv, &ch_tv, ==)) { 736a9771948SGleb Smirnoff callout_stop(&sc->sc_ad_tmo); 737d01641e2SWill Andrews carp_set_state(sc, BACKUP, 738d01641e2SWill Andrews "more frequent advertisement received"); 739a9771948SGleb Smirnoff carp_setrun(sc, 0); 74008b68b0eSGleb Smirnoff carp_delroute(sc); 741a9771948SGleb Smirnoff } 742a9771948SGleb Smirnoff break; 743a9771948SGleb Smirnoff case BACKUP: 744a9771948SGleb Smirnoff /* 745a9771948SGleb Smirnoff * If we're pre-empting masters who advertise slower than us, 746a9771948SGleb Smirnoff * and this one claims to be slower, treat him as down. 747a9771948SGleb Smirnoff */ 748c5c392e7SMikolaj Golub if (V_carp_preempt && timevalcmp(&sc_tv, &ch_tv, <)) { 749d01641e2SWill Andrews carp_master_down_locked(sc, 750d01641e2SWill Andrews "preempting a slower master"); 751a9771948SGleb Smirnoff break; 752a9771948SGleb Smirnoff } 753a9771948SGleb Smirnoff 754a9771948SGleb Smirnoff /* 755a9771948SGleb Smirnoff * If the master is going to advertise at such a low frequency 756a9771948SGleb Smirnoff * that he's guaranteed to time out, we'd might as well just 757a9771948SGleb Smirnoff * treat him as timed out now. 758a9771948SGleb Smirnoff */ 759a9771948SGleb Smirnoff sc_tv.tv_sec = sc->sc_advbase * 3; 760a9771948SGleb Smirnoff if (timevalcmp(&sc_tv, &ch_tv, <)) { 761d01641e2SWill Andrews carp_master_down_locked(sc, "master will time out"); 762a9771948SGleb Smirnoff break; 763a9771948SGleb Smirnoff } 764a9771948SGleb Smirnoff 765a9771948SGleb Smirnoff /* 766a9771948SGleb Smirnoff * Otherwise, we reset the counter and wait for the next 767a9771948SGleb Smirnoff * advertisement. 768a9771948SGleb Smirnoff */ 769a9771948SGleb Smirnoff carp_setrun(sc, af); 770a9771948SGleb Smirnoff break; 771a9771948SGleb Smirnoff } 772a9771948SGleb Smirnoff 77308b68b0eSGleb Smirnoff out: 77408b68b0eSGleb Smirnoff CARP_UNLOCK(sc); 775a9771948SGleb Smirnoff m_freem(m); 776a9771948SGleb Smirnoff } 777a9771948SGleb Smirnoff 7785c1f0f6dSGleb Smirnoff static int 779a9771948SGleb Smirnoff carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch) 780a9771948SGleb Smirnoff { 781a9771948SGleb Smirnoff struct m_tag *mtag; 782a9771948SGleb Smirnoff 783a9771948SGleb Smirnoff if (sc->sc_init_counter) { 784a9771948SGleb Smirnoff /* this could also be seconds since unix epoch */ 785a9771948SGleb Smirnoff sc->sc_counter = arc4random(); 786a9771948SGleb Smirnoff sc->sc_counter = sc->sc_counter << 32; 787a9771948SGleb Smirnoff sc->sc_counter += arc4random(); 788a9771948SGleb Smirnoff } else 789a9771948SGleb Smirnoff sc->sc_counter++; 790a9771948SGleb Smirnoff 791a9771948SGleb Smirnoff ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff); 792a9771948SGleb Smirnoff ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff); 793a9771948SGleb Smirnoff 794a9771948SGleb Smirnoff carp_hmac_generate(sc, ch->carp_counter, ch->carp_md); 795a9771948SGleb Smirnoff 796a9771948SGleb Smirnoff /* Tag packet for carp_output */ 79708b68b0eSGleb Smirnoff if ((mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct carp_softc *), 79808b68b0eSGleb Smirnoff M_NOWAIT)) == NULL) { 799a9771948SGleb Smirnoff m_freem(m); 80008b68b0eSGleb Smirnoff CARPSTATS_INC(carps_onomem); 801a9771948SGleb Smirnoff return (ENOMEM); 802a9771948SGleb Smirnoff } 803eaf151c4SGleb Smirnoff bcopy(&sc, mtag + 1, sizeof(sc)); 804a9771948SGleb Smirnoff m_tag_prepend(m, mtag); 805a9771948SGleb Smirnoff 806a9771948SGleb Smirnoff return (0); 807a9771948SGleb Smirnoff } 808a9771948SGleb Smirnoff 809f08535f8SGleb Smirnoff /* 810f08535f8SGleb Smirnoff * To avoid LORs and possible recursions this function shouldn't 811f08535f8SGleb Smirnoff * be called directly, but scheduled via taskqueue. 812f08535f8SGleb Smirnoff */ 8135c1f0f6dSGleb Smirnoff static void 814f08535f8SGleb Smirnoff carp_send_ad_all(void *ctx __unused, int pending __unused) 815a9771948SGleb Smirnoff { 816d92d54d5SGleb Smirnoff struct carp_softc *sc; 817a9771948SGleb Smirnoff 818d92d54d5SGleb Smirnoff mtx_lock(&carp_mtx); 81908b68b0eSGleb Smirnoff LIST_FOREACH(sc, &carp_list, sc_next) 820f08535f8SGleb Smirnoff if (sc->sc_state == MASTER) { 82108b68b0eSGleb Smirnoff CARP_LOCK(sc); 822afdbac98SGleb Smirnoff CURVNET_SET(sc->sc_carpdev->if_vnet); 823d220759bSGleb Smirnoff carp_send_ad_locked(sc); 824afdbac98SGleb Smirnoff CURVNET_RESTORE(); 82508b68b0eSGleb Smirnoff CARP_UNLOCK(sc); 826a9771948SGleb Smirnoff } 827d92d54d5SGleb Smirnoff mtx_unlock(&carp_mtx); 828a9771948SGleb Smirnoff } 829a9771948SGleb Smirnoff 830afdbac98SGleb Smirnoff /* Send a periodic advertisement, executed in callout context. */ 8315c1f0f6dSGleb Smirnoff static void 832a9771948SGleb Smirnoff carp_send_ad(void *v) 833a9771948SGleb Smirnoff { 834d220759bSGleb Smirnoff struct carp_softc *sc = v; 835d220759bSGleb Smirnoff 83608b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); 837afdbac98SGleb Smirnoff CURVNET_SET(sc->sc_carpdev->if_vnet); 838d220759bSGleb Smirnoff carp_send_ad_locked(sc); 839afdbac98SGleb Smirnoff CURVNET_RESTORE(); 84008b68b0eSGleb Smirnoff CARP_UNLOCK(sc); 841d220759bSGleb Smirnoff } 842d220759bSGleb Smirnoff 843d220759bSGleb Smirnoff static void 8444a2dd8d4SGleb Smirnoff carp_send_ad_error(struct carp_softc *sc, int error) 8454a2dd8d4SGleb Smirnoff { 8464a2dd8d4SGleb Smirnoff 8474a2dd8d4SGleb Smirnoff if (error) { 8484a2dd8d4SGleb Smirnoff if (sc->sc_sendad_errors < INT_MAX) 8494a2dd8d4SGleb Smirnoff sc->sc_sendad_errors++; 8504a2dd8d4SGleb Smirnoff if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) { 8514a2dd8d4SGleb Smirnoff static const char fmt[] = "send error %d on %s"; 8524a2dd8d4SGleb Smirnoff char msg[sizeof(fmt) + IFNAMSIZ]; 8534a2dd8d4SGleb Smirnoff 8544a2dd8d4SGleb Smirnoff sprintf(msg, fmt, error, sc->sc_carpdev->if_xname); 8554a2dd8d4SGleb Smirnoff carp_demote_adj(V_carp_senderr_adj, msg); 8564a2dd8d4SGleb Smirnoff } 8574a2dd8d4SGleb Smirnoff sc->sc_sendad_success = 0; 8584a2dd8d4SGleb Smirnoff } else { 8594a2dd8d4SGleb Smirnoff if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS && 8604a2dd8d4SGleb Smirnoff ++sc->sc_sendad_success >= CARP_SENDAD_MIN_SUCCESS) { 8614a2dd8d4SGleb Smirnoff static const char fmt[] = "send ok on %s"; 8624a2dd8d4SGleb Smirnoff char msg[sizeof(fmt) + IFNAMSIZ]; 8634a2dd8d4SGleb Smirnoff 8644a2dd8d4SGleb Smirnoff sprintf(msg, fmt, sc->sc_carpdev->if_xname); 8654a2dd8d4SGleb Smirnoff carp_demote_adj(-V_carp_senderr_adj, msg); 8664a2dd8d4SGleb Smirnoff sc->sc_sendad_errors = 0; 8674a2dd8d4SGleb Smirnoff } else 8684a2dd8d4SGleb Smirnoff sc->sc_sendad_errors = 0; 8694a2dd8d4SGleb Smirnoff } 8704a2dd8d4SGleb Smirnoff } 8714a2dd8d4SGleb Smirnoff 8728151740cSJosh Paetzel /* 8738151740cSJosh Paetzel * Pick the best ifaddr on the given ifp for sending CARP 8748151740cSJosh Paetzel * advertisements. 8758151740cSJosh Paetzel * 8768151740cSJosh Paetzel * "Best" here is defined by ifa_preferred(). This function is much 8778151740cSJosh Paetzel * much like ifaof_ifpforaddr() except that we just use ifa_preferred(). 8788151740cSJosh Paetzel * 8798151740cSJosh Paetzel * (This could be simplified to return the actual address, except that 8808151740cSJosh Paetzel * it has a different format in AF_INET and AF_INET6.) 8818151740cSJosh Paetzel */ 8828151740cSJosh Paetzel static struct ifaddr * 8838151740cSJosh Paetzel carp_best_ifa(int af, struct ifnet *ifp) 8848151740cSJosh Paetzel { 8858151740cSJosh Paetzel struct ifaddr *ifa, *best; 8868151740cSJosh Paetzel 887b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 888b8a6e03fSGleb Smirnoff 8898151740cSJosh Paetzel if (af >= AF_MAX) 8908151740cSJosh Paetzel return (NULL); 8918151740cSJosh Paetzel best = NULL; 892d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 8938151740cSJosh Paetzel if (ifa->ifa_addr->sa_family == af && 8948151740cSJosh Paetzel (best == NULL || ifa_preferred(best, ifa))) 8958151740cSJosh Paetzel best = ifa; 8968151740cSJosh Paetzel } 8978151740cSJosh Paetzel if (best != NULL) 8988151740cSJosh Paetzel ifa_ref(best); 8998151740cSJosh Paetzel return (best); 9008151740cSJosh Paetzel } 9018151740cSJosh Paetzel 9024a2dd8d4SGleb Smirnoff static void 903d220759bSGleb Smirnoff carp_send_ad_locked(struct carp_softc *sc) 904d220759bSGleb Smirnoff { 905a9771948SGleb Smirnoff struct carp_header ch; 906a9771948SGleb Smirnoff struct timeval tv; 90708b68b0eSGleb Smirnoff struct ifaddr *ifa; 908a9771948SGleb Smirnoff struct carp_header *ch_ptr; 909a9771948SGleb Smirnoff struct mbuf *m; 91008b68b0eSGleb Smirnoff int len, advskew; 911a9771948SGleb Smirnoff 91208b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); 913d220759bSGleb Smirnoff 914f08535f8SGleb Smirnoff advskew = DEMOTE_ADVSKEW(sc); 91508b68b0eSGleb Smirnoff tv.tv_sec = sc->sc_advbase; 916a9771948SGleb Smirnoff tv.tv_usec = advskew * 1000000 / 256; 917a9771948SGleb Smirnoff 918a9771948SGleb Smirnoff ch.carp_version = CARP_VERSION; 919a9771948SGleb Smirnoff ch.carp_type = CARP_ADVERTISEMENT; 920a9771948SGleb Smirnoff ch.carp_vhid = sc->sc_vhid; 92108b68b0eSGleb Smirnoff ch.carp_advbase = sc->sc_advbase; 922a9771948SGleb Smirnoff ch.carp_advskew = advskew; 923a9771948SGleb Smirnoff ch.carp_authlen = 7; /* XXX DEFINE */ 924a9771948SGleb Smirnoff ch.carp_pad1 = 0; /* must be zero */ 925a9771948SGleb Smirnoff ch.carp_cksum = 0; 926a9771948SGleb Smirnoff 92708b68b0eSGleb Smirnoff /* XXXGL: OpenBSD picks first ifaddr with needed family. */ 92808b68b0eSGleb Smirnoff 929a9771948SGleb Smirnoff #ifdef INET 93008b68b0eSGleb Smirnoff if (sc->sc_naddrs) { 931a9771948SGleb Smirnoff struct ip *ip; 932a9771948SGleb Smirnoff 933dc4ad05eSGleb Smirnoff m = m_gethdr(M_NOWAIT, MT_DATA); 934a9771948SGleb Smirnoff if (m == NULL) { 9356bf65bcfSRobert Watson CARPSTATS_INC(carps_onomem); 936891122d1SGleb Smirnoff goto resched; 937a9771948SGleb Smirnoff } 938a9771948SGleb Smirnoff len = sizeof(*ip) + sizeof(ch); 939a9771948SGleb Smirnoff m->m_pkthdr.len = len; 940a9771948SGleb Smirnoff m->m_pkthdr.rcvif = NULL; 941a9771948SGleb Smirnoff m->m_len = len; 942ed6a66caSRobert Watson M_ALIGN(m, m->m_len); 943a9771948SGleb Smirnoff m->m_flags |= M_MCAST; 944a9771948SGleb Smirnoff ip = mtod(m, struct ip *); 945a9771948SGleb Smirnoff ip->ip_v = IPVERSION; 946a9771948SGleb Smirnoff ip->ip_hl = sizeof(*ip) >> 2; 9470d3d234cSKristof Provost ip->ip_tos = V_carp_dscp << IPTOS_DSCP_OFFSET; 9488f134647SGleb Smirnoff ip->ip_len = htons(len); 9498f134647SGleb Smirnoff ip->ip_off = htons(IP_DF); 950a9771948SGleb Smirnoff ip->ip_ttl = CARP_DFLTTL; 951a9771948SGleb Smirnoff ip->ip_p = IPPROTO_CARP; 952a9771948SGleb Smirnoff ip->ip_sum = 0; 9536d947416SGleb Smirnoff ip_fillid(ip); 95408b68b0eSGleb Smirnoff 9558151740cSJosh Paetzel ifa = carp_best_ifa(AF_INET, sc->sc_carpdev); 95608b68b0eSGleb Smirnoff if (ifa != NULL) { 95708b68b0eSGleb Smirnoff ip->ip_src.s_addr = 95808b68b0eSGleb Smirnoff ifatoia(ifa)->ia_addr.sin_addr.s_addr; 95908b68b0eSGleb Smirnoff ifa_free(ifa); 96008b68b0eSGleb Smirnoff } else 96108b68b0eSGleb Smirnoff ip->ip_src.s_addr = 0; 962a9771948SGleb Smirnoff ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP); 963a9771948SGleb Smirnoff 964a9771948SGleb Smirnoff ch_ptr = (struct carp_header *)(&ip[1]); 965a9771948SGleb Smirnoff bcopy(&ch, ch_ptr, sizeof(ch)); 966a9771948SGleb Smirnoff if (carp_prepare_ad(m, sc, ch_ptr)) 967891122d1SGleb Smirnoff goto resched; 968a9771948SGleb Smirnoff 969a9771948SGleb Smirnoff m->m_data += sizeof(*ip); 970c4d06976SGleb Smirnoff ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip)); 971a9771948SGleb Smirnoff m->m_data -= sizeof(*ip); 972a9771948SGleb Smirnoff 9736bf65bcfSRobert Watson CARPSTATS_INC(carps_opackets); 974a9771948SGleb Smirnoff 9754a2dd8d4SGleb Smirnoff carp_send_ad_error(sc, ip_output(m, NULL, NULL, IP_RAWOUTPUT, 9764a2dd8d4SGleb Smirnoff &sc->sc_carpdev->if_carp->cif_imo, NULL)); 977a9771948SGleb Smirnoff } 978a9771948SGleb Smirnoff #endif /* INET */ 979a9771948SGleb Smirnoff #ifdef INET6 98008b68b0eSGleb Smirnoff if (sc->sc_naddrs6) { 981a9771948SGleb Smirnoff struct ip6_hdr *ip6; 982a9771948SGleb Smirnoff 983dc4ad05eSGleb Smirnoff m = m_gethdr(M_NOWAIT, MT_DATA); 984a9771948SGleb Smirnoff if (m == NULL) { 9856bf65bcfSRobert Watson CARPSTATS_INC(carps_onomem); 986891122d1SGleb Smirnoff goto resched; 987a9771948SGleb Smirnoff } 988a9771948SGleb Smirnoff len = sizeof(*ip6) + sizeof(ch); 989a9771948SGleb Smirnoff m->m_pkthdr.len = len; 990a9771948SGleb Smirnoff m->m_pkthdr.rcvif = NULL; 991a9771948SGleb Smirnoff m->m_len = len; 992ed6a66caSRobert Watson M_ALIGN(m, m->m_len); 993a9771948SGleb Smirnoff m->m_flags |= M_MCAST; 994a9771948SGleb Smirnoff ip6 = mtod(m, struct ip6_hdr *); 995a9771948SGleb Smirnoff bzero(ip6, sizeof(*ip6)); 996a9771948SGleb Smirnoff ip6->ip6_vfc |= IPV6_VERSION; 9970d3d234cSKristof Provost /* Traffic class isn't defined in ip6 struct instead 9980d3d234cSKristof Provost * it gets offset into flowid field */ 9990d3d234cSKristof Provost ip6->ip6_flow |= htonl(V_carp_dscp << (IPV6_FLOWLABEL_LEN + 10000d3d234cSKristof Provost IPTOS_DSCP_OFFSET)); 1001a9771948SGleb Smirnoff ip6->ip6_hlim = CARP_DFLTTL; 1002a9771948SGleb Smirnoff ip6->ip6_nxt = IPPROTO_CARP; 1003a9771948SGleb Smirnoff 100408b68b0eSGleb Smirnoff /* set the source address */ 10058151740cSJosh Paetzel ifa = carp_best_ifa(AF_INET6, sc->sc_carpdev); 100608b68b0eSGleb Smirnoff if (ifa != NULL) { 100708b68b0eSGleb Smirnoff bcopy(IFA_IN6(ifa), &ip6->ip6_src, 100808b68b0eSGleb Smirnoff sizeof(struct in6_addr)); 100908b68b0eSGleb Smirnoff ifa_free(ifa); 101008b68b0eSGleb Smirnoff } else 101108b68b0eSGleb Smirnoff /* This should never happen with IPv6. */ 101208b68b0eSGleb Smirnoff bzero(&ip6->ip6_src, sizeof(struct in6_addr)); 101308b68b0eSGleb Smirnoff 101408b68b0eSGleb Smirnoff /* Set the multicast destination. */ 10157002145dSBjoern A. Zeeb ip6->ip6_dst.s6_addr16[0] = htons(0xff02); 1016a9771948SGleb Smirnoff ip6->ip6_dst.s6_addr8[15] = 0x12; 10177002145dSBjoern A. Zeeb if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) { 10187002145dSBjoern A. Zeeb m_freem(m); 1019e81ab876SGleb Smirnoff CARP_DEBUG("%s: in6_setscope failed\n", __func__); 1020891122d1SGleb Smirnoff goto resched; 10217002145dSBjoern A. Zeeb } 1022a9771948SGleb Smirnoff 1023a9771948SGleb Smirnoff ch_ptr = (struct carp_header *)(&ip6[1]); 1024a9771948SGleb Smirnoff bcopy(&ch, ch_ptr, sizeof(ch)); 1025a9771948SGleb Smirnoff if (carp_prepare_ad(m, sc, ch_ptr)) 1026891122d1SGleb Smirnoff goto resched; 1027a9771948SGleb Smirnoff 1028a9771948SGleb Smirnoff m->m_data += sizeof(*ip6); 1029c4d06976SGleb Smirnoff ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip6)); 1030a9771948SGleb Smirnoff m->m_data -= sizeof(*ip6); 1031a9771948SGleb Smirnoff 10326bf65bcfSRobert Watson CARPSTATS_INC(carps_opackets6); 1033a9771948SGleb Smirnoff 10344a2dd8d4SGleb Smirnoff carp_send_ad_error(sc, ip6_output(m, NULL, NULL, 0, 10354a2dd8d4SGleb Smirnoff &sc->sc_carpdev->if_carp->cif_im6o, NULL, NULL)); 1036a9771948SGleb Smirnoff } 1037a9771948SGleb Smirnoff #endif /* INET6 */ 1038a9771948SGleb Smirnoff 1039891122d1SGleb Smirnoff resched: 104008b68b0eSGleb Smirnoff callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), carp_send_ad, sc); 104108b68b0eSGleb Smirnoff } 1042a9771948SGleb Smirnoff 104308b68b0eSGleb Smirnoff static void 104408b68b0eSGleb Smirnoff carp_addroute(struct carp_softc *sc) 104508b68b0eSGleb Smirnoff { 104608b68b0eSGleb Smirnoff struct ifaddr *ifa; 104708b68b0eSGleb Smirnoff 104808b68b0eSGleb Smirnoff CARP_FOREACH_IFA(sc, ifa) 10492512b096SGleb Smirnoff carp_ifa_addroute(ifa); 10502512b096SGleb Smirnoff } 10512512b096SGleb Smirnoff 10522512b096SGleb Smirnoff static void 10532512b096SGleb Smirnoff carp_ifa_addroute(struct ifaddr *ifa) 10542512b096SGleb Smirnoff { 10552512b096SGleb Smirnoff 105608b68b0eSGleb Smirnoff switch (ifa->ifa_addr->sa_family) { 105708b68b0eSGleb Smirnoff #ifdef INET 105808b68b0eSGleb Smirnoff case AF_INET: 105908b68b0eSGleb Smirnoff in_addprefix(ifatoia(ifa), RTF_UP); 106008b68b0eSGleb Smirnoff ifa_add_loopback_route(ifa, 106108b68b0eSGleb Smirnoff (struct sockaddr *)&ifatoia(ifa)->ia_addr); 106208b68b0eSGleb Smirnoff break; 106308b68b0eSGleb Smirnoff #endif 106408b68b0eSGleb Smirnoff #ifdef INET6 106508b68b0eSGleb Smirnoff case AF_INET6: 106608b68b0eSGleb Smirnoff ifa_add_loopback_route(ifa, 106708b68b0eSGleb Smirnoff (struct sockaddr *)&ifatoia6(ifa)->ia_addr); 1068f6b84910SAlexander V. Chernikov nd6_add_ifa_lle(ifatoia6(ifa)); 106908b68b0eSGleb Smirnoff break; 107008b68b0eSGleb Smirnoff #endif 107108b68b0eSGleb Smirnoff } 107208b68b0eSGleb Smirnoff } 107308b68b0eSGleb Smirnoff 107408b68b0eSGleb Smirnoff static void 107508b68b0eSGleb Smirnoff carp_delroute(struct carp_softc *sc) 107608b68b0eSGleb Smirnoff { 107708b68b0eSGleb Smirnoff struct ifaddr *ifa; 107808b68b0eSGleb Smirnoff 107908b68b0eSGleb Smirnoff CARP_FOREACH_IFA(sc, ifa) 10802512b096SGleb Smirnoff carp_ifa_delroute(ifa); 10812512b096SGleb Smirnoff } 10822512b096SGleb Smirnoff 10832512b096SGleb Smirnoff static void 10842512b096SGleb Smirnoff carp_ifa_delroute(struct ifaddr *ifa) 10852512b096SGleb Smirnoff { 10862512b096SGleb Smirnoff 108708b68b0eSGleb Smirnoff switch (ifa->ifa_addr->sa_family) { 108808b68b0eSGleb Smirnoff #ifdef INET 108908b68b0eSGleb Smirnoff case AF_INET: 109008b68b0eSGleb Smirnoff ifa_del_loopback_route(ifa, 109108b68b0eSGleb Smirnoff (struct sockaddr *)&ifatoia(ifa)->ia_addr); 109208b68b0eSGleb Smirnoff in_scrubprefix(ifatoia(ifa), LLE_STATIC); 109308b68b0eSGleb Smirnoff break; 109408b68b0eSGleb Smirnoff #endif 109508b68b0eSGleb Smirnoff #ifdef INET6 109608b68b0eSGleb Smirnoff case AF_INET6: 109708b68b0eSGleb Smirnoff ifa_del_loopback_route(ifa, 109808b68b0eSGleb Smirnoff (struct sockaddr *)&ifatoia6(ifa)->ia_addr); 10993e7a2321SAlexander V. Chernikov nd6_rem_ifa_lle(ifatoia6(ifa), 1); 110008b68b0eSGleb Smirnoff break; 110108b68b0eSGleb Smirnoff #endif 110208b68b0eSGleb Smirnoff } 1103a9771948SGleb Smirnoff } 1104a9771948SGleb Smirnoff 110524421c1cSGleb Smirnoff int 110624421c1cSGleb Smirnoff carp_master(struct ifaddr *ifa) 110724421c1cSGleb Smirnoff { 110824421c1cSGleb Smirnoff struct carp_softc *sc = ifa->ifa_carp; 110924421c1cSGleb Smirnoff 111024421c1cSGleb Smirnoff return (sc->sc_state == MASTER); 111124421c1cSGleb Smirnoff } 111224421c1cSGleb Smirnoff 1113a0ae8f04SBjoern A. Zeeb #ifdef INET 1114a9771948SGleb Smirnoff /* 1115a9771948SGleb Smirnoff * Broadcast a gratuitous ARP request containing 1116a9771948SGleb Smirnoff * the virtual router MAC address for each IP address 1117a9771948SGleb Smirnoff * associated with the virtual router. 1118a9771948SGleb Smirnoff */ 11195c1f0f6dSGleb Smirnoff static void 1120a9771948SGleb Smirnoff carp_send_arp(struct carp_softc *sc) 1121a9771948SGleb Smirnoff { 1122a9771948SGleb Smirnoff struct ifaddr *ifa; 1123d6e82913SSteven Hartland struct in_addr addr; 1124a9771948SGleb Smirnoff 11251c302b58SAlexander V. Chernikov CARP_FOREACH_IFA(sc, ifa) { 11261c302b58SAlexander V. Chernikov if (ifa->ifa_addr->sa_family != AF_INET) 11271c302b58SAlexander V. Chernikov continue; 1128d6e82913SSteven Hartland addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr; 1129d6e82913SSteven Hartland arp_announce_ifaddr(sc->sc_carpdev, addr, LLADDR(&sc->sc_addr)); 11301c302b58SAlexander V. Chernikov } 1131a9771948SGleb Smirnoff } 113208b68b0eSGleb Smirnoff 113308b68b0eSGleb Smirnoff int 113408b68b0eSGleb Smirnoff carp_iamatch(struct ifaddr *ifa, uint8_t **enaddr) 113508b68b0eSGleb Smirnoff { 113608b68b0eSGleb Smirnoff struct carp_softc *sc = ifa->ifa_carp; 113708b68b0eSGleb Smirnoff 113808b68b0eSGleb Smirnoff if (sc->sc_state == MASTER) { 113908b68b0eSGleb Smirnoff *enaddr = LLADDR(&sc->sc_addr); 114008b68b0eSGleb Smirnoff return (1); 114108b68b0eSGleb Smirnoff } 114208b68b0eSGleb Smirnoff 114308b68b0eSGleb Smirnoff return (0); 1144a9771948SGleb Smirnoff } 1145a0ae8f04SBjoern A. Zeeb #endif 1146a9771948SGleb Smirnoff 1147a9771948SGleb Smirnoff #ifdef INET6 11485c1f0f6dSGleb Smirnoff static void 1149a9771948SGleb Smirnoff carp_send_na(struct carp_softc *sc) 1150a9771948SGleb Smirnoff { 1151d6e82913SSteven Hartland static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT; 1152a9771948SGleb Smirnoff struct ifaddr *ifa; 1153d6e82913SSteven Hartland struct in6_addr *in6; 1154a9771948SGleb Smirnoff 115508b68b0eSGleb Smirnoff CARP_FOREACH_IFA(sc, ifa) { 1156d6e82913SSteven Hartland if (ifa->ifa_addr->sa_family != AF_INET6) 1157a9771948SGleb Smirnoff continue; 1158a9771948SGleb Smirnoff 1159d6e82913SSteven Hartland in6 = IFA_IN6(ifa); 1160d6e82913SSteven Hartland nd6_na_output(sc->sc_carpdev, &mcast, in6, 1161d6e82913SSteven Hartland ND_NA_FLAG_OVERRIDE, 1, NULL); 1162d6e82913SSteven Hartland DELAY(1000); /* XXX */ 1163a9771948SGleb Smirnoff } 1164a9771948SGleb Smirnoff } 1165a9771948SGleb Smirnoff 11668253dcabSBjoern A. Zeeb /* 11678253dcabSBjoern A. Zeeb * Returns ifa in case it's a carp address and it is MASTER, or if the address 11688253dcabSBjoern A. Zeeb * matches and is not a carp address. Returns NULL otherwise. 11698253dcabSBjoern A. Zeeb */ 1170a4e53905SMax Laier struct ifaddr * 117154bfbd51SWill Andrews carp_iamatch6(struct ifnet *ifp, struct in6_addr *taddr) 1172a9771948SGleb Smirnoff { 1173a9771948SGleb Smirnoff struct ifaddr *ifa; 1174a9771948SGleb Smirnoff 1175b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 1176b8a6e03fSGleb Smirnoff 11778253dcabSBjoern A. Zeeb ifa = NULL; 1178d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 11798253dcabSBjoern A. Zeeb if (ifa->ifa_addr->sa_family != AF_INET6) 11808253dcabSBjoern A. Zeeb continue; 11818253dcabSBjoern A. Zeeb if (!IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) 11828253dcabSBjoern A. Zeeb continue; 11838253dcabSBjoern A. Zeeb if (ifa->ifa_carp && ifa->ifa_carp->sc_state != MASTER) 11848253dcabSBjoern A. Zeeb ifa = NULL; 11858253dcabSBjoern A. Zeeb else 11868c0fec80SRobert Watson ifa_ref(ifa); 11878253dcabSBjoern A. Zeeb break; 1188a9771948SGleb Smirnoff } 1189a9771948SGleb Smirnoff 11908253dcabSBjoern A. Zeeb return (ifa); 1191a9771948SGleb Smirnoff } 1192a9771948SGleb Smirnoff 1193dad68fc3SBjoern A. Zeeb char * 119454bfbd51SWill Andrews carp_macmatch6(struct ifnet *ifp, struct mbuf *m, const struct in6_addr *taddr) 1195a9771948SGleb Smirnoff { 1196a9771948SGleb Smirnoff struct ifaddr *ifa; 1197a9771948SGleb Smirnoff 1198b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 1199b8a6e03fSGleb Smirnoff 120008b68b0eSGleb Smirnoff IFNET_FOREACH_IFA(ifp, ifa) 120108b68b0eSGleb Smirnoff if (ifa->ifa_addr->sa_family == AF_INET6 && 120208b68b0eSGleb Smirnoff IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) { 120308b68b0eSGleb Smirnoff struct carp_softc *sc = ifa->ifa_carp; 120408b68b0eSGleb Smirnoff struct m_tag *mtag; 120508b68b0eSGleb Smirnoff 1206a9771948SGleb Smirnoff mtag = m_tag_get(PACKET_TAG_CARP, 1207a856ddc6SGleb Smirnoff sizeof(struct carp_softc *), M_NOWAIT); 120808b68b0eSGleb Smirnoff if (mtag == NULL) 120908b68b0eSGleb Smirnoff /* Better a bit than nothing. */ 121008b68b0eSGleb Smirnoff return (LLADDR(&sc->sc_addr)); 121108b68b0eSGleb Smirnoff 1212eaf151c4SGleb Smirnoff bcopy(&sc, mtag + 1, sizeof(sc)); 1213a9771948SGleb Smirnoff m_tag_prepend(m, mtag); 1214a9771948SGleb Smirnoff 121508b68b0eSGleb Smirnoff return (LLADDR(&sc->sc_addr)); 1216a9771948SGleb Smirnoff } 1217a9771948SGleb Smirnoff 1218a9771948SGleb Smirnoff return (NULL); 1219a9771948SGleb Smirnoff } 122008b68b0eSGleb Smirnoff #endif /* INET6 */ 1221a9771948SGleb Smirnoff 122208b68b0eSGleb Smirnoff int 122354bfbd51SWill Andrews carp_forus(struct ifnet *ifp, u_char *dhost) 1224a9771948SGleb Smirnoff { 122508b68b0eSGleb Smirnoff struct carp_softc *sc; 122608b68b0eSGleb Smirnoff uint8_t *ena = dhost; 1227a9771948SGleb Smirnoff 1228a9771948SGleb Smirnoff if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1) 122908b68b0eSGleb Smirnoff return (0); 1230a9771948SGleb Smirnoff 123108b68b0eSGleb Smirnoff CIF_LOCK(ifp->if_carp); 123208b68b0eSGleb Smirnoff IFNET_FOREACH_CARP(ifp, sc) { 1233*8c3fbf3cSAlexander Motin /* 1234*8c3fbf3cSAlexander Motin * CARP_LOCK() is not here, since would protect nothing, but 1235*8c3fbf3cSAlexander Motin * cause deadlock with if_bridge, calling this under its lock. 1236*8c3fbf3cSAlexander Motin */ 123708b68b0eSGleb Smirnoff if (sc->sc_state == MASTER && !bcmp(dhost, LLADDR(&sc->sc_addr), 123808b68b0eSGleb Smirnoff ETHER_ADDR_LEN)) { 123908b68b0eSGleb Smirnoff CIF_UNLOCK(ifp->if_carp); 124008b68b0eSGleb Smirnoff return (1); 1241a9771948SGleb Smirnoff } 124208b68b0eSGleb Smirnoff } 124308b68b0eSGleb Smirnoff CIF_UNLOCK(ifp->if_carp); 1244a9771948SGleb Smirnoff 124508b68b0eSGleb Smirnoff return (0); 1246a9771948SGleb Smirnoff } 1247a9771948SGleb Smirnoff 1248afdbac98SGleb Smirnoff /* Master down timeout event, executed in callout context. */ 12495c1f0f6dSGleb Smirnoff static void 1250a9771948SGleb Smirnoff carp_master_down(void *v) 1251a9771948SGleb Smirnoff { 1252a9771948SGleb Smirnoff struct carp_softc *sc = v; 1253a9771948SGleb Smirnoff 125408b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); 125508b68b0eSGleb Smirnoff 1256afdbac98SGleb Smirnoff CURVNET_SET(sc->sc_carpdev->if_vnet); 125708b68b0eSGleb Smirnoff if (sc->sc_state == BACKUP) { 1258d01641e2SWill Andrews carp_master_down_locked(sc, "master timed out"); 125908b68b0eSGleb Smirnoff } 1260afdbac98SGleb Smirnoff CURVNET_RESTORE(); 126108b68b0eSGleb Smirnoff 126208b68b0eSGleb Smirnoff CARP_UNLOCK(sc); 1263d220759bSGleb Smirnoff } 1264d220759bSGleb Smirnoff 1265d220759bSGleb Smirnoff static void 1266d01641e2SWill Andrews carp_master_down_locked(struct carp_softc *sc, const char *reason) 1267d220759bSGleb Smirnoff { 126808b68b0eSGleb Smirnoff 126908b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); 1270d220759bSGleb Smirnoff 1271a9771948SGleb Smirnoff switch (sc->sc_state) { 1272a9771948SGleb Smirnoff case BACKUP: 1273d01641e2SWill Andrews carp_set_state(sc, MASTER, reason); 1274d220759bSGleb Smirnoff carp_send_ad_locked(sc); 1275a0ae8f04SBjoern A. Zeeb #ifdef INET 1276a9771948SGleb Smirnoff carp_send_arp(sc); 1277a0ae8f04SBjoern A. Zeeb #endif 1278a9771948SGleb Smirnoff #ifdef INET6 1279a9771948SGleb Smirnoff carp_send_na(sc); 128008b68b0eSGleb Smirnoff #endif 1281a9771948SGleb Smirnoff carp_setrun(sc, 0); 128208b68b0eSGleb Smirnoff carp_addroute(sc); 128308b68b0eSGleb Smirnoff break; 128408b68b0eSGleb Smirnoff case INIT: 128508b68b0eSGleb Smirnoff case MASTER: 128608b68b0eSGleb Smirnoff #ifdef INVARIANTS 128708b68b0eSGleb Smirnoff panic("carp: VHID %u@%s: master_down event in %s state\n", 128808b68b0eSGleb Smirnoff sc->sc_vhid, 128908b68b0eSGleb Smirnoff sc->sc_carpdev->if_xname, 129008b68b0eSGleb Smirnoff sc->sc_state ? "MASTER" : "INIT"); 129108b68b0eSGleb Smirnoff #endif 1292a9771948SGleb Smirnoff break; 1293a9771948SGleb Smirnoff } 1294a9771948SGleb Smirnoff } 1295a9771948SGleb Smirnoff 1296a9771948SGleb Smirnoff /* 1297a9771948SGleb Smirnoff * When in backup state, af indicates whether to reset the master down timer 1298a9771948SGleb Smirnoff * for v4 or v6. If it's set to zero, reset the ones which are already pending. 1299a9771948SGleb Smirnoff */ 13005c1f0f6dSGleb Smirnoff static void 1301a9771948SGleb Smirnoff carp_setrun(struct carp_softc *sc, sa_family_t af) 1302a9771948SGleb Smirnoff { 1303a9771948SGleb Smirnoff struct timeval tv; 1304a9771948SGleb Smirnoff 130508b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); 1306d220759bSGleb Smirnoff 130708b68b0eSGleb Smirnoff if ((sc->sc_carpdev->if_flags & IFF_UP) == 0 || 130808b68b0eSGleb Smirnoff sc->sc_carpdev->if_link_state != LINK_STATE_UP || 1309167a3440SAlexander Motin (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) || 1310167a3440SAlexander Motin !V_carp_allow) 1311a9771948SGleb Smirnoff return; 1312a9771948SGleb Smirnoff 1313a9771948SGleb Smirnoff switch (sc->sc_state) { 1314a9771948SGleb Smirnoff case INIT: 1315d01641e2SWill Andrews carp_set_state(sc, BACKUP, "initialization complete"); 1316a9771948SGleb Smirnoff carp_setrun(sc, 0); 1317a9771948SGleb Smirnoff break; 1318a9771948SGleb Smirnoff case BACKUP: 1319a9771948SGleb Smirnoff callout_stop(&sc->sc_ad_tmo); 1320a9771948SGleb Smirnoff tv.tv_sec = 3 * sc->sc_advbase; 1321a9771948SGleb Smirnoff tv.tv_usec = sc->sc_advskew * 1000000 / 256; 1322a9771948SGleb Smirnoff switch (af) { 1323a9771948SGleb Smirnoff #ifdef INET 1324a9771948SGleb Smirnoff case AF_INET: 1325a9771948SGleb Smirnoff callout_reset(&sc->sc_md_tmo, tvtohz(&tv), 1326a9771948SGleb Smirnoff carp_master_down, sc); 1327a9771948SGleb Smirnoff break; 132808b68b0eSGleb Smirnoff #endif 1329a9771948SGleb Smirnoff #ifdef INET6 1330a9771948SGleb Smirnoff case AF_INET6: 1331a9771948SGleb Smirnoff callout_reset(&sc->sc_md6_tmo, tvtohz(&tv), 1332a9771948SGleb Smirnoff carp_master_down, sc); 1333a9771948SGleb Smirnoff break; 133408b68b0eSGleb Smirnoff #endif 1335a9771948SGleb Smirnoff default: 133608b68b0eSGleb Smirnoff #ifdef INET 1337a9771948SGleb Smirnoff if (sc->sc_naddrs) 1338a9771948SGleb Smirnoff callout_reset(&sc->sc_md_tmo, tvtohz(&tv), 1339a9771948SGleb Smirnoff carp_master_down, sc); 134008b68b0eSGleb Smirnoff #endif 134108b68b0eSGleb Smirnoff #ifdef INET6 1342a9771948SGleb Smirnoff if (sc->sc_naddrs6) 1343a9771948SGleb Smirnoff callout_reset(&sc->sc_md6_tmo, tvtohz(&tv), 1344a9771948SGleb Smirnoff carp_master_down, sc); 134508b68b0eSGleb Smirnoff #endif 1346a9771948SGleb Smirnoff break; 1347a9771948SGleb Smirnoff } 1348a9771948SGleb Smirnoff break; 1349a9771948SGleb Smirnoff case MASTER: 1350a9771948SGleb Smirnoff tv.tv_sec = sc->sc_advbase; 1351a9771948SGleb Smirnoff tv.tv_usec = sc->sc_advskew * 1000000 / 256; 1352a9771948SGleb Smirnoff callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), 1353a9771948SGleb Smirnoff carp_send_ad, sc); 1354a9771948SGleb Smirnoff break; 1355a9771948SGleb Smirnoff } 1356a9771948SGleb Smirnoff } 1357a9771948SGleb Smirnoff 135808b68b0eSGleb Smirnoff /* 135908b68b0eSGleb Smirnoff * Setup multicast structures. 136008b68b0eSGleb Smirnoff */ 13615c1f0f6dSGleb Smirnoff static int 1362a9a2c40cSGleb Smirnoff carp_multicast_setup(struct carp_if *cif, sa_family_t sa) 1363a9771948SGleb Smirnoff { 1364a9a2c40cSGleb Smirnoff struct ifnet *ifp = cif->cif_ifp; 136508b68b0eSGleb Smirnoff int error = 0; 136608b68b0eSGleb Smirnoff 136708b68b0eSGleb Smirnoff switch (sa) { 136808b68b0eSGleb Smirnoff #ifdef INET 136908b68b0eSGleb Smirnoff case AF_INET: 137008b68b0eSGleb Smirnoff { 137108b68b0eSGleb Smirnoff struct ip_moptions *imo = &cif->cif_imo; 137259854ecfSHans Petter Selasky struct in_mfilter *imf; 1373a9771948SGleb Smirnoff struct in_addr addr; 1374a9771948SGleb Smirnoff 137559854ecfSHans Petter Selasky if (ip_mfilter_first(&imo->imo_head) != NULL) 1376a9771948SGleb Smirnoff return (0); 1377a9771948SGleb Smirnoff 137859854ecfSHans Petter Selasky imf = ip_mfilter_alloc(M_WAITOK, 0, 0); 137959854ecfSHans Petter Selasky ip_mfilter_init(&imo->imo_head); 138008b68b0eSGleb Smirnoff imo->imo_multicast_vif = -1; 1381a9771948SGleb Smirnoff 1382a9771948SGleb Smirnoff addr.s_addr = htonl(INADDR_CARP_GROUP); 138308b68b0eSGleb Smirnoff if ((error = in_joingroup(ifp, &addr, NULL, 138459854ecfSHans Petter Selasky &imf->imf_inm)) != 0) { 138559854ecfSHans Petter Selasky ip_mfilter_free(imf); 138608b68b0eSGleb Smirnoff break; 13872d9cfabaSRobert Watson } 138859854ecfSHans Petter Selasky 138959854ecfSHans Petter Selasky ip_mfilter_insert(&imo->imo_head, imf); 1390a9771948SGleb Smirnoff imo->imo_multicast_ifp = ifp; 1391a9771948SGleb Smirnoff imo->imo_multicast_ttl = CARP_DFLTTL; 1392a9771948SGleb Smirnoff imo->imo_multicast_loop = 0; 1393a9771948SGleb Smirnoff break; 1394a9771948SGleb Smirnoff } 139508b68b0eSGleb Smirnoff #endif 139608b68b0eSGleb Smirnoff #ifdef INET6 139708b68b0eSGleb Smirnoff case AF_INET6: 139808b68b0eSGleb Smirnoff { 139908b68b0eSGleb Smirnoff struct ip6_moptions *im6o = &cif->cif_im6o; 140059854ecfSHans Petter Selasky struct in6_mfilter *im6f[2]; 140108b68b0eSGleb Smirnoff struct in6_addr in6; 140233cde130SBruce M Simpson 140359854ecfSHans Petter Selasky if (ip6_mfilter_first(&im6o->im6o_head)) 140408b68b0eSGleb Smirnoff return (0); 140508b68b0eSGleb Smirnoff 140659854ecfSHans Petter Selasky im6f[0] = ip6_mfilter_alloc(M_WAITOK, 0, 0); 140759854ecfSHans Petter Selasky im6f[1] = ip6_mfilter_alloc(M_WAITOK, 0, 0); 140859854ecfSHans Petter Selasky 140959854ecfSHans Petter Selasky ip6_mfilter_init(&im6o->im6o_head); 141008b68b0eSGleb Smirnoff im6o->im6o_multicast_hlim = CARP_DFLTTL; 1411a9771948SGleb Smirnoff im6o->im6o_multicast_ifp = ifp; 1412a9771948SGleb Smirnoff 141308b68b0eSGleb Smirnoff /* Join IPv6 CARP multicast group. */ 1414a1f7e5f8SHajimu UMEMOTO bzero(&in6, sizeof(in6)); 1415a1f7e5f8SHajimu UMEMOTO in6.s6_addr16[0] = htons(0xff02); 1416a1f7e5f8SHajimu UMEMOTO in6.s6_addr8[15] = 0x12; 141708b68b0eSGleb Smirnoff if ((error = in6_setscope(&in6, ifp, NULL)) != 0) { 141859854ecfSHans Petter Selasky ip6_mfilter_free(im6f[0]); 141959854ecfSHans Petter Selasky ip6_mfilter_free(im6f[1]); 142008b68b0eSGleb Smirnoff break; 142108b68b0eSGleb Smirnoff } 142259854ecfSHans Petter Selasky if ((error = in6_joingroup(ifp, &in6, NULL, &im6f[0]->im6f_in6m, 0)) != 0) { 142359854ecfSHans Petter Selasky ip6_mfilter_free(im6f[0]); 142459854ecfSHans Petter Selasky ip6_mfilter_free(im6f[1]); 142508b68b0eSGleb Smirnoff break; 142608b68b0eSGleb Smirnoff } 1427a9771948SGleb Smirnoff 142808b68b0eSGleb Smirnoff /* Join solicited multicast address. */ 1429a1f7e5f8SHajimu UMEMOTO bzero(&in6, sizeof(in6)); 1430a1f7e5f8SHajimu UMEMOTO in6.s6_addr16[0] = htons(0xff02); 1431a1f7e5f8SHajimu UMEMOTO in6.s6_addr32[1] = 0; 1432a1f7e5f8SHajimu UMEMOTO in6.s6_addr32[2] = htonl(1); 143308b68b0eSGleb Smirnoff in6.s6_addr32[3] = 0; 1434a1f7e5f8SHajimu UMEMOTO in6.s6_addr8[12] = 0xff; 143559854ecfSHans Petter Selasky 143608b68b0eSGleb Smirnoff if ((error = in6_setscope(&in6, ifp, NULL)) != 0) { 143759854ecfSHans Petter Selasky ip6_mfilter_free(im6f[0]); 143859854ecfSHans Petter Selasky ip6_mfilter_free(im6f[1]); 143908b68b0eSGleb Smirnoff break; 144008b68b0eSGleb Smirnoff } 144159854ecfSHans Petter Selasky 144259854ecfSHans Petter Selasky if ((error = in6_joingroup(ifp, &in6, NULL, &im6f[1]->im6f_in6m, 0)) != 0) { 144359854ecfSHans Petter Selasky in6_leavegroup(im6f[0]->im6f_in6m, NULL); 144459854ecfSHans Petter Selasky ip6_mfilter_free(im6f[0]); 144559854ecfSHans Petter Selasky ip6_mfilter_free(im6f[1]); 144608b68b0eSGleb Smirnoff break; 144708b68b0eSGleb Smirnoff } 144859854ecfSHans Petter Selasky ip6_mfilter_insert(&im6o->im6o_head, im6f[0]); 144959854ecfSHans Petter Selasky ip6_mfilter_insert(&im6o->im6o_head, im6f[1]); 1450a9771948SGleb Smirnoff break; 1451a9771948SGleb Smirnoff } 1452a9771948SGleb Smirnoff #endif 145308b68b0eSGleb Smirnoff } 145408b68b0eSGleb Smirnoff 145508b68b0eSGleb Smirnoff return (error); 1456a9771948SGleb Smirnoff } 1457a9771948SGleb Smirnoff 1458a9771948SGleb Smirnoff /* 145908b68b0eSGleb Smirnoff * Free multicast structures. 1460a9771948SGleb Smirnoff */ 14615c1f0f6dSGleb Smirnoff static void 1462a9a2c40cSGleb Smirnoff carp_multicast_cleanup(struct carp_if *cif, sa_family_t sa) 1463a9771948SGleb Smirnoff { 146459854ecfSHans Petter Selasky #ifdef INET 146559854ecfSHans Petter Selasky struct ip_moptions *imo = &cif->cif_imo; 146659854ecfSHans Petter Selasky struct in_mfilter *imf; 146759854ecfSHans Petter Selasky #endif 146859854ecfSHans Petter Selasky #ifdef INET6 146959854ecfSHans Petter Selasky struct ip6_moptions *im6o = &cif->cif_im6o; 147059854ecfSHans Petter Selasky struct in6_mfilter *im6f; 147159854ecfSHans Petter Selasky #endif 14729c2cd1aaSGleb Smirnoff sx_assert(&carp_sx, SA_XLOCKED); 14739c2cd1aaSGleb Smirnoff 147408b68b0eSGleb Smirnoff switch (sa) { 147508b68b0eSGleb Smirnoff #ifdef INET 147608b68b0eSGleb Smirnoff case AF_INET: 147759854ecfSHans Petter Selasky if (cif->cif_naddrs != 0) 147859854ecfSHans Petter Selasky break; 147908b68b0eSGleb Smirnoff 148059854ecfSHans Petter Selasky while ((imf = ip_mfilter_first(&imo->imo_head)) != NULL) { 148159854ecfSHans Petter Selasky ip_mfilter_remove(&imo->imo_head, imf); 148259854ecfSHans Petter Selasky in_leavegroup(imf->imf_inm, NULL); 148359854ecfSHans Petter Selasky ip_mfilter_free(imf); 148408b68b0eSGleb Smirnoff } 148508b68b0eSGleb Smirnoff break; 1486a9771948SGleb Smirnoff #endif 148708b68b0eSGleb Smirnoff #ifdef INET6 148808b68b0eSGleb Smirnoff case AF_INET6: 148959854ecfSHans Petter Selasky if (cif->cif_naddrs6 != 0) 149059854ecfSHans Petter Selasky break; 149108b68b0eSGleb Smirnoff 149259854ecfSHans Petter Selasky while ((im6f = ip6_mfilter_first(&im6o->im6o_head)) != NULL) { 149359854ecfSHans Petter Selasky ip6_mfilter_remove(&im6o->im6o_head, im6f); 149459854ecfSHans Petter Selasky in6_leavegroup(im6f->im6f_in6m, NULL); 149559854ecfSHans Petter Selasky ip6_mfilter_free(im6f); 149608b68b0eSGleb Smirnoff } 149708b68b0eSGleb Smirnoff break; 149808b68b0eSGleb Smirnoff #endif 149908b68b0eSGleb Smirnoff } 1500a9771948SGleb Smirnoff } 1501a9771948SGleb Smirnoff 1502a9771948SGleb Smirnoff int 150347e8d432SGleb Smirnoff carp_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa) 1504a9771948SGleb Smirnoff { 1505a9771948SGleb Smirnoff struct m_tag *mtag; 1506a9771948SGleb Smirnoff struct carp_softc *sc; 1507a9771948SGleb Smirnoff 1508a9771948SGleb Smirnoff if (!sa) 1509a9771948SGleb Smirnoff return (0); 1510a9771948SGleb Smirnoff 1511a9771948SGleb Smirnoff switch (sa->sa_family) { 1512a9771948SGleb Smirnoff #ifdef INET 1513a9771948SGleb Smirnoff case AF_INET: 1514a9771948SGleb Smirnoff break; 151508b68b0eSGleb Smirnoff #endif 1516a9771948SGleb Smirnoff #ifdef INET6 1517a9771948SGleb Smirnoff case AF_INET6: 1518a9771948SGleb Smirnoff break; 151908b68b0eSGleb Smirnoff #endif 1520a9771948SGleb Smirnoff default: 1521a9771948SGleb Smirnoff return (0); 1522a9771948SGleb Smirnoff } 1523a9771948SGleb Smirnoff 1524a9771948SGleb Smirnoff mtag = m_tag_find(m, PACKET_TAG_CARP, NULL); 1525a9771948SGleb Smirnoff if (mtag == NULL) 1526a9771948SGleb Smirnoff return (0); 1527a9771948SGleb Smirnoff 1528eaf151c4SGleb Smirnoff bcopy(mtag + 1, &sc, sizeof(sc)); 1529a9771948SGleb Smirnoff 153008b68b0eSGleb Smirnoff /* Set the source MAC address to the Virtual Router MAC Address. */ 1531a9771948SGleb Smirnoff switch (ifp->if_type) { 1532630481bbSYaroslav Tykhiy case IFT_ETHER: 15339ca1fe0dSGleb Smirnoff case IFT_BRIDGE: 1534630481bbSYaroslav Tykhiy case IFT_L2VLAN: { 1535a9771948SGleb Smirnoff struct ether_header *eh; 1536a9771948SGleb Smirnoff 1537a9771948SGleb Smirnoff eh = mtod(m, struct ether_header *); 1538a9771948SGleb Smirnoff eh->ether_shost[0] = 0; 1539a9771948SGleb Smirnoff eh->ether_shost[1] = 0; 1540a9771948SGleb Smirnoff eh->ether_shost[2] = 0x5e; 1541a9771948SGleb Smirnoff eh->ether_shost[3] = 0; 1542a9771948SGleb Smirnoff eh->ether_shost[4] = 1; 1543a9771948SGleb Smirnoff eh->ether_shost[5] = sc->sc_vhid; 1544a9771948SGleb Smirnoff } 1545a9771948SGleb Smirnoff break; 1546a9771948SGleb Smirnoff default: 154708b68b0eSGleb Smirnoff printf("%s: carp is not supported for the %d interface type\n", 154808b68b0eSGleb Smirnoff ifp->if_xname, ifp->if_type); 1549a9771948SGleb Smirnoff return (EOPNOTSUPP); 1550a9771948SGleb Smirnoff } 1551a9771948SGleb Smirnoff 1552a9771948SGleb Smirnoff return (0); 1553a9771948SGleb Smirnoff } 1554a9771948SGleb Smirnoff 155508b68b0eSGleb Smirnoff static struct carp_softc* 155608b68b0eSGleb Smirnoff carp_alloc(struct ifnet *ifp) 1557a9771948SGleb Smirnoff { 155808b68b0eSGleb Smirnoff struct carp_softc *sc; 155908b68b0eSGleb Smirnoff struct carp_if *cif; 1560d220759bSGleb Smirnoff 156181098a01SAlexander Motin sx_assert(&carp_sx, SA_XLOCKED); 156281098a01SAlexander Motin 15630cc726f2SGleb Smirnoff if ((cif = ifp->if_carp) == NULL) 156408b68b0eSGleb Smirnoff cif = carp_alloc_if(ifp); 1565a9771948SGleb Smirnoff 156608b68b0eSGleb Smirnoff sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO); 156708b68b0eSGleb Smirnoff 156808b68b0eSGleb Smirnoff sc->sc_advbase = CARP_DFLTINTV; 156908b68b0eSGleb Smirnoff sc->sc_vhid = -1; /* required setting */ 157008b68b0eSGleb Smirnoff sc->sc_init_counter = 1; 157108b68b0eSGleb Smirnoff sc->sc_state = INIT; 157208b68b0eSGleb Smirnoff 157308b68b0eSGleb Smirnoff sc->sc_ifasiz = sizeof(struct ifaddr *); 157408b68b0eSGleb Smirnoff sc->sc_ifas = malloc(sc->sc_ifasiz, M_CARP, M_WAITOK|M_ZERO); 157508b68b0eSGleb Smirnoff sc->sc_carpdev = ifp; 157608b68b0eSGleb Smirnoff 157708b68b0eSGleb Smirnoff CARP_LOCK_INIT(sc); 157808b68b0eSGleb Smirnoff #ifdef INET 157908b68b0eSGleb Smirnoff callout_init_mtx(&sc->sc_md_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED); 158008b68b0eSGleb Smirnoff #endif 158108b68b0eSGleb Smirnoff #ifdef INET6 158208b68b0eSGleb Smirnoff callout_init_mtx(&sc->sc_md6_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED); 158308b68b0eSGleb Smirnoff #endif 158408b68b0eSGleb Smirnoff callout_init_mtx(&sc->sc_ad_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED); 158508b68b0eSGleb Smirnoff 158608b68b0eSGleb Smirnoff CIF_LOCK(cif); 158708b68b0eSGleb Smirnoff TAILQ_INSERT_TAIL(&cif->cif_vrs, sc, sc_list); 158808b68b0eSGleb Smirnoff CIF_UNLOCK(cif); 158908b68b0eSGleb Smirnoff 159008b68b0eSGleb Smirnoff mtx_lock(&carp_mtx); 159108b68b0eSGleb Smirnoff LIST_INSERT_HEAD(&carp_list, sc, sc_next); 159208b68b0eSGleb Smirnoff mtx_unlock(&carp_mtx); 159308b68b0eSGleb Smirnoff 159408b68b0eSGleb Smirnoff return (sc); 159508b68b0eSGleb Smirnoff } 159608b68b0eSGleb Smirnoff 15979c2cd1aaSGleb Smirnoff static void 159808b68b0eSGleb Smirnoff carp_grow_ifas(struct carp_softc *sc) 159908b68b0eSGleb Smirnoff { 160008b68b0eSGleb Smirnoff struct ifaddr **new; 160108b68b0eSGleb Smirnoff 16029c2cd1aaSGleb Smirnoff new = malloc(sc->sc_ifasiz * 2, M_CARP, M_WAITOK | M_ZERO); 16039c2cd1aaSGleb Smirnoff CARP_LOCK(sc); 160408b68b0eSGleb Smirnoff bcopy(sc->sc_ifas, new, sc->sc_ifasiz); 160508b68b0eSGleb Smirnoff free(sc->sc_ifas, M_CARP); 160608b68b0eSGleb Smirnoff sc->sc_ifas = new; 160708b68b0eSGleb Smirnoff sc->sc_ifasiz *= 2; 16089c2cd1aaSGleb Smirnoff CARP_UNLOCK(sc); 160908b68b0eSGleb Smirnoff } 161008b68b0eSGleb Smirnoff 161108b68b0eSGleb Smirnoff static void 161208b68b0eSGleb Smirnoff carp_destroy(struct carp_softc *sc) 161308b68b0eSGleb Smirnoff { 161408b68b0eSGleb Smirnoff struct ifnet *ifp = sc->sc_carpdev; 161508b68b0eSGleb Smirnoff struct carp_if *cif = ifp->if_carp; 161608b68b0eSGleb Smirnoff 16179c2cd1aaSGleb Smirnoff sx_assert(&carp_sx, SA_XLOCKED); 1618a9a2c40cSGleb Smirnoff 16199c2cd1aaSGleb Smirnoff if (sc->sc_suppress) 16209c2cd1aaSGleb Smirnoff carp_demote_adj(-V_carp_ifdown_adj, "vhid removed"); 16219c2cd1aaSGleb Smirnoff CARP_UNLOCK(sc); 16229c2cd1aaSGleb Smirnoff 16239c2cd1aaSGleb Smirnoff CIF_LOCK(cif); 162408b68b0eSGleb Smirnoff TAILQ_REMOVE(&cif->cif_vrs, sc, sc_list); 16259c2cd1aaSGleb Smirnoff CIF_UNLOCK(cif); 162608b68b0eSGleb Smirnoff 162708b68b0eSGleb Smirnoff mtx_lock(&carp_mtx); 162808b68b0eSGleb Smirnoff LIST_REMOVE(sc, sc_next); 162908b68b0eSGleb Smirnoff mtx_unlock(&carp_mtx); 163008b68b0eSGleb Smirnoff 163108b68b0eSGleb Smirnoff callout_drain(&sc->sc_ad_tmo); 163208b68b0eSGleb Smirnoff #ifdef INET 163308b68b0eSGleb Smirnoff callout_drain(&sc->sc_md_tmo); 163408b68b0eSGleb Smirnoff #endif 163508b68b0eSGleb Smirnoff #ifdef INET6 163608b68b0eSGleb Smirnoff callout_drain(&sc->sc_md6_tmo); 163708b68b0eSGleb Smirnoff #endif 163808b68b0eSGleb Smirnoff CARP_LOCK_DESTROY(sc); 163908b68b0eSGleb Smirnoff 164008b68b0eSGleb Smirnoff free(sc->sc_ifas, M_CARP); 164108b68b0eSGleb Smirnoff free(sc, M_CARP); 164208b68b0eSGleb Smirnoff } 164308b68b0eSGleb Smirnoff 164408b68b0eSGleb Smirnoff static struct carp_if* 164508b68b0eSGleb Smirnoff carp_alloc_if(struct ifnet *ifp) 1646a9771948SGleb Smirnoff { 164754bfbd51SWill Andrews struct carp_if *cif; 16480cc726f2SGleb Smirnoff int error; 1649d220759bSGleb Smirnoff 165008b68b0eSGleb Smirnoff cif = malloc(sizeof(*cif), M_CARP, M_WAITOK|M_ZERO); 165108b68b0eSGleb Smirnoff 16520cc726f2SGleb Smirnoff if ((error = ifpromisc(ifp, 1)) != 0) 16530cc726f2SGleb Smirnoff printf("%s: ifpromisc(%s) failed: %d\n", 16540cc726f2SGleb Smirnoff __func__, ifp->if_xname, error); 16550cc726f2SGleb Smirnoff else 16560cc726f2SGleb Smirnoff cif->cif_flags |= CIF_PROMISC; 165708b68b0eSGleb Smirnoff 165808b68b0eSGleb Smirnoff CIF_LOCK_INIT(cif); 165908b68b0eSGleb Smirnoff cif->cif_ifp = ifp; 166008b68b0eSGleb Smirnoff TAILQ_INIT(&cif->cif_vrs); 166108b68b0eSGleb Smirnoff 1662137f91e8SJohn Baldwin IF_ADDR_WLOCK(ifp); 166308b68b0eSGleb Smirnoff ifp->if_carp = cif; 166408b68b0eSGleb Smirnoff if_ref(ifp); 1665137f91e8SJohn Baldwin IF_ADDR_WUNLOCK(ifp); 166608b68b0eSGleb Smirnoff 166708b68b0eSGleb Smirnoff return (cif); 1668d220759bSGleb Smirnoff } 1669d220759bSGleb Smirnoff 1670d220759bSGleb Smirnoff static void 167108b68b0eSGleb Smirnoff carp_free_if(struct carp_if *cif) 167208b68b0eSGleb Smirnoff { 167308b68b0eSGleb Smirnoff struct ifnet *ifp = cif->cif_ifp; 167408b68b0eSGleb Smirnoff 167508b68b0eSGleb Smirnoff CIF_LOCK_ASSERT(cif); 167608b68b0eSGleb Smirnoff KASSERT(TAILQ_EMPTY(&cif->cif_vrs), ("%s: softc list not empty", 167708b68b0eSGleb Smirnoff __func__)); 167808b68b0eSGleb Smirnoff 1679137f91e8SJohn Baldwin IF_ADDR_WLOCK(ifp); 168008b68b0eSGleb Smirnoff ifp->if_carp = NULL; 1681137f91e8SJohn Baldwin IF_ADDR_WUNLOCK(ifp); 168208b68b0eSGleb Smirnoff 168308b68b0eSGleb Smirnoff CIF_LOCK_DESTROY(cif); 168408b68b0eSGleb Smirnoff 16850cc726f2SGleb Smirnoff if (cif->cif_flags & CIF_PROMISC) 168608b68b0eSGleb Smirnoff ifpromisc(ifp, 0); 16871f6addd9SMikolaj Golub if_rele(ifp); 168808b68b0eSGleb Smirnoff 168908b68b0eSGleb Smirnoff free(cif, M_CARP); 169008b68b0eSGleb Smirnoff } 169108b68b0eSGleb Smirnoff 169208b68b0eSGleb Smirnoff static void 169308b68b0eSGleb Smirnoff carp_carprcp(struct carpreq *carpr, struct carp_softc *sc, int priv) 169408b68b0eSGleb Smirnoff { 169508b68b0eSGleb Smirnoff 169608b68b0eSGleb Smirnoff CARP_LOCK(sc); 169708b68b0eSGleb Smirnoff carpr->carpr_state = sc->sc_state; 169808b68b0eSGleb Smirnoff carpr->carpr_vhid = sc->sc_vhid; 169908b68b0eSGleb Smirnoff carpr->carpr_advbase = sc->sc_advbase; 170008b68b0eSGleb Smirnoff carpr->carpr_advskew = sc->sc_advskew; 170108b68b0eSGleb Smirnoff if (priv) 170208b68b0eSGleb Smirnoff bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key)); 170308b68b0eSGleb Smirnoff else 170408b68b0eSGleb Smirnoff bzero(carpr->carpr_key, sizeof(carpr->carpr_key)); 170508b68b0eSGleb Smirnoff CARP_UNLOCK(sc); 170608b68b0eSGleb Smirnoff } 170708b68b0eSGleb Smirnoff 170808b68b0eSGleb Smirnoff int 170908b68b0eSGleb Smirnoff carp_ioctl(struct ifreq *ifr, u_long cmd, struct thread *td) 171008b68b0eSGleb Smirnoff { 171108b68b0eSGleb Smirnoff struct carpreq carpr; 171208b68b0eSGleb Smirnoff struct ifnet *ifp; 171308b68b0eSGleb Smirnoff struct carp_softc *sc = NULL; 171408b68b0eSGleb Smirnoff int error = 0, locked = 0; 171508b68b0eSGleb Smirnoff 1716541d96aaSBrooks Davis if ((error = copyin(ifr_data_get_ptr(ifr), &carpr, sizeof carpr))) 171708b68b0eSGleb Smirnoff return (error); 171808b68b0eSGleb Smirnoff 171908b68b0eSGleb Smirnoff ifp = ifunit_ref(ifr->ifr_name); 172008b68b0eSGleb Smirnoff if (ifp == NULL) 172108b68b0eSGleb Smirnoff return (ENXIO); 172208b68b0eSGleb Smirnoff 172308b68b0eSGleb Smirnoff switch (ifp->if_type) { 172408b68b0eSGleb Smirnoff case IFT_ETHER: 172508b68b0eSGleb Smirnoff case IFT_L2VLAN: 17269ca1fe0dSGleb Smirnoff case IFT_BRIDGE: 172708b68b0eSGleb Smirnoff break; 172808b68b0eSGleb Smirnoff default: 172908b68b0eSGleb Smirnoff error = EOPNOTSUPP; 173008b68b0eSGleb Smirnoff goto out; 173108b68b0eSGleb Smirnoff } 173208b68b0eSGleb Smirnoff 173308b68b0eSGleb Smirnoff if ((ifp->if_flags & IFF_MULTICAST) == 0) { 173408b68b0eSGleb Smirnoff error = EADDRNOTAVAIL; 173508b68b0eSGleb Smirnoff goto out; 173608b68b0eSGleb Smirnoff } 173708b68b0eSGleb Smirnoff 173893d4534cSGleb Smirnoff sx_xlock(&carp_sx); 173908b68b0eSGleb Smirnoff switch (cmd) { 174008b68b0eSGleb Smirnoff case SIOCSVH: 174108b68b0eSGleb Smirnoff if ((error = priv_check(td, PRIV_NETINET_CARP))) 174208b68b0eSGleb Smirnoff break; 174308b68b0eSGleb Smirnoff if (carpr.carpr_vhid <= 0 || carpr.carpr_vhid > CARP_MAXVHID || 174408b68b0eSGleb Smirnoff carpr.carpr_advbase < 0 || carpr.carpr_advskew < 0) { 174508b68b0eSGleb Smirnoff error = EINVAL; 174608b68b0eSGleb Smirnoff break; 174708b68b0eSGleb Smirnoff } 174808b68b0eSGleb Smirnoff 174908b68b0eSGleb Smirnoff if (ifp->if_carp) { 175008b68b0eSGleb Smirnoff IFNET_FOREACH_CARP(ifp, sc) 175108b68b0eSGleb Smirnoff if (sc->sc_vhid == carpr.carpr_vhid) 175208b68b0eSGleb Smirnoff break; 175308b68b0eSGleb Smirnoff } 175408b68b0eSGleb Smirnoff if (sc == NULL) { 175508b68b0eSGleb Smirnoff sc = carp_alloc(ifp); 175608b68b0eSGleb Smirnoff CARP_LOCK(sc); 175708b68b0eSGleb Smirnoff sc->sc_vhid = carpr.carpr_vhid; 175808b68b0eSGleb Smirnoff LLADDR(&sc->sc_addr)[0] = 0; 175908b68b0eSGleb Smirnoff LLADDR(&sc->sc_addr)[1] = 0; 176008b68b0eSGleb Smirnoff LLADDR(&sc->sc_addr)[2] = 0x5e; 176108b68b0eSGleb Smirnoff LLADDR(&sc->sc_addr)[3] = 0; 176208b68b0eSGleb Smirnoff LLADDR(&sc->sc_addr)[4] = 1; 176308b68b0eSGleb Smirnoff LLADDR(&sc->sc_addr)[5] = sc->sc_vhid; 176408b68b0eSGleb Smirnoff } else 176508b68b0eSGleb Smirnoff CARP_LOCK(sc); 176608b68b0eSGleb Smirnoff locked = 1; 176708b68b0eSGleb Smirnoff if (carpr.carpr_advbase > 0) { 176808b68b0eSGleb Smirnoff if (carpr.carpr_advbase > 255 || 176908b68b0eSGleb Smirnoff carpr.carpr_advbase < CARP_DFLTINTV) { 177008b68b0eSGleb Smirnoff error = EINVAL; 177108b68b0eSGleb Smirnoff break; 177208b68b0eSGleb Smirnoff } 177308b68b0eSGleb Smirnoff sc->sc_advbase = carpr.carpr_advbase; 177408b68b0eSGleb Smirnoff } 177508b68b0eSGleb Smirnoff if (carpr.carpr_advskew >= 255) { 177608b68b0eSGleb Smirnoff error = EINVAL; 177708b68b0eSGleb Smirnoff break; 177808b68b0eSGleb Smirnoff } 177908b68b0eSGleb Smirnoff sc->sc_advskew = carpr.carpr_advskew; 178008b68b0eSGleb Smirnoff if (carpr.carpr_key[0] != '\0') { 178108b68b0eSGleb Smirnoff bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key)); 178208b68b0eSGleb Smirnoff carp_hmac_prepare(sc); 178308b68b0eSGleb Smirnoff } 178408b68b0eSGleb Smirnoff if (sc->sc_state != INIT && 178508b68b0eSGleb Smirnoff carpr.carpr_state != sc->sc_state) { 178608b68b0eSGleb Smirnoff switch (carpr.carpr_state) { 178708b68b0eSGleb Smirnoff case BACKUP: 178808b68b0eSGleb Smirnoff callout_stop(&sc->sc_ad_tmo); 1789369a6708SWill Andrews carp_set_state(sc, BACKUP, 1790369a6708SWill Andrews "user requested via ifconfig"); 179108b68b0eSGleb Smirnoff carp_setrun(sc, 0); 179208b68b0eSGleb Smirnoff carp_delroute(sc); 179308b68b0eSGleb Smirnoff break; 179408b68b0eSGleb Smirnoff case MASTER: 1795369a6708SWill Andrews carp_master_down_locked(sc, 1796369a6708SWill Andrews "user requested via ifconfig"); 179708b68b0eSGleb Smirnoff break; 179808b68b0eSGleb Smirnoff default: 179908b68b0eSGleb Smirnoff break; 180008b68b0eSGleb Smirnoff } 180108b68b0eSGleb Smirnoff } 180208b68b0eSGleb Smirnoff break; 180308b68b0eSGleb Smirnoff 180408b68b0eSGleb Smirnoff case SIOCGVH: 180508b68b0eSGleb Smirnoff { 180608b68b0eSGleb Smirnoff int priveleged; 180708b68b0eSGleb Smirnoff 180808b68b0eSGleb Smirnoff if (carpr.carpr_vhid < 0 || carpr.carpr_vhid > CARP_MAXVHID) { 180908b68b0eSGleb Smirnoff error = EINVAL; 181008b68b0eSGleb Smirnoff break; 181108b68b0eSGleb Smirnoff } 181208b68b0eSGleb Smirnoff if (carpr.carpr_count < 1) { 181308b68b0eSGleb Smirnoff error = EMSGSIZE; 181408b68b0eSGleb Smirnoff break; 181508b68b0eSGleb Smirnoff } 181608b68b0eSGleb Smirnoff if (ifp->if_carp == NULL) { 181708b68b0eSGleb Smirnoff error = ENOENT; 181808b68b0eSGleb Smirnoff break; 181908b68b0eSGleb Smirnoff } 182008b68b0eSGleb Smirnoff 182108b68b0eSGleb Smirnoff priveleged = (priv_check(td, PRIV_NETINET_CARP) == 0); 182208b68b0eSGleb Smirnoff if (carpr.carpr_vhid != 0) { 182308b68b0eSGleb Smirnoff IFNET_FOREACH_CARP(ifp, sc) 182408b68b0eSGleb Smirnoff if (sc->sc_vhid == carpr.carpr_vhid) 182508b68b0eSGleb Smirnoff break; 182608b68b0eSGleb Smirnoff if (sc == NULL) { 182708b68b0eSGleb Smirnoff error = ENOENT; 182808b68b0eSGleb Smirnoff break; 182908b68b0eSGleb Smirnoff } 183008b68b0eSGleb Smirnoff carp_carprcp(&carpr, sc, priveleged); 1831541d96aaSBrooks Davis error = copyout(&carpr, ifr_data_get_ptr(ifr), 1832541d96aaSBrooks Davis sizeof(carpr)); 183308b68b0eSGleb Smirnoff } else { 183408b68b0eSGleb Smirnoff int i, count; 183508b68b0eSGleb Smirnoff 183608b68b0eSGleb Smirnoff count = 0; 183708b68b0eSGleb Smirnoff IFNET_FOREACH_CARP(ifp, sc) 183808b68b0eSGleb Smirnoff count++; 183908b68b0eSGleb Smirnoff 184008b68b0eSGleb Smirnoff if (count > carpr.carpr_count) { 184108b68b0eSGleb Smirnoff CIF_UNLOCK(ifp->if_carp); 184208b68b0eSGleb Smirnoff error = EMSGSIZE; 184308b68b0eSGleb Smirnoff break; 184408b68b0eSGleb Smirnoff } 184508b68b0eSGleb Smirnoff 184608b68b0eSGleb Smirnoff i = 0; 184708b68b0eSGleb Smirnoff IFNET_FOREACH_CARP(ifp, sc) { 184808b68b0eSGleb Smirnoff carp_carprcp(&carpr, sc, priveleged); 184908b68b0eSGleb Smirnoff carpr.carpr_count = count; 1850541d96aaSBrooks Davis error = copyout(&carpr, 1851dad68fc3SBjoern A. Zeeb (char *)ifr_data_get_ptr(ifr) + 185208b68b0eSGleb Smirnoff (i * sizeof(carpr)), sizeof(carpr)); 185308b68b0eSGleb Smirnoff if (error) { 185408b68b0eSGleb Smirnoff CIF_UNLOCK(ifp->if_carp); 185508b68b0eSGleb Smirnoff break; 185608b68b0eSGleb Smirnoff } 185708b68b0eSGleb Smirnoff i++; 185808b68b0eSGleb Smirnoff } 185908b68b0eSGleb Smirnoff } 186008b68b0eSGleb Smirnoff break; 186108b68b0eSGleb Smirnoff } 186208b68b0eSGleb Smirnoff default: 186308b68b0eSGleb Smirnoff error = EINVAL; 186408b68b0eSGleb Smirnoff } 186593d4534cSGleb Smirnoff sx_xunlock(&carp_sx); 186608b68b0eSGleb Smirnoff 186708b68b0eSGleb Smirnoff out: 186808b68b0eSGleb Smirnoff if (locked) 186908b68b0eSGleb Smirnoff CARP_UNLOCK(sc); 187008b68b0eSGleb Smirnoff if_rele(ifp); 187108b68b0eSGleb Smirnoff 187208b68b0eSGleb Smirnoff return (error); 187308b68b0eSGleb Smirnoff } 187408b68b0eSGleb Smirnoff 187508b68b0eSGleb Smirnoff static int 187608b68b0eSGleb Smirnoff carp_get_vhid(struct ifaddr *ifa) 187708b68b0eSGleb Smirnoff { 187808b68b0eSGleb Smirnoff 187908b68b0eSGleb Smirnoff if (ifa == NULL || ifa->ifa_carp == NULL) 188008b68b0eSGleb Smirnoff return (0); 188108b68b0eSGleb Smirnoff 188208b68b0eSGleb Smirnoff return (ifa->ifa_carp->sc_vhid); 188308b68b0eSGleb Smirnoff } 188408b68b0eSGleb Smirnoff 188508b68b0eSGleb Smirnoff int 188608b68b0eSGleb Smirnoff carp_attach(struct ifaddr *ifa, int vhid) 188708b68b0eSGleb Smirnoff { 188808b68b0eSGleb Smirnoff struct ifnet *ifp = ifa->ifa_ifp; 1889a9a2c40cSGleb Smirnoff struct carp_if *cif = ifp->if_carp; 189008b68b0eSGleb Smirnoff struct carp_softc *sc; 189108b68b0eSGleb Smirnoff int index, error; 189208b68b0eSGleb Smirnoff 18939c2cd1aaSGleb Smirnoff KASSERT(ifa->ifa_carp == NULL, ("%s: ifa %p attached", __func__, ifa)); 189408b68b0eSGleb Smirnoff 189508b68b0eSGleb Smirnoff switch (ifa->ifa_addr->sa_family) { 189608b68b0eSGleb Smirnoff #ifdef INET 189708b68b0eSGleb Smirnoff case AF_INET: 189808b68b0eSGleb Smirnoff #endif 189908b68b0eSGleb Smirnoff #ifdef INET6 190008b68b0eSGleb Smirnoff case AF_INET6: 190108b68b0eSGleb Smirnoff #endif 190208b68b0eSGleb Smirnoff break; 190308b68b0eSGleb Smirnoff default: 190408b68b0eSGleb Smirnoff return (EPROTOTYPE); 190508b68b0eSGleb Smirnoff } 190608b68b0eSGleb Smirnoff 19079c2cd1aaSGleb Smirnoff sx_xlock(&carp_sx); 19089c2cd1aaSGleb Smirnoff if (ifp->if_carp == NULL) { 19099c2cd1aaSGleb Smirnoff sx_xunlock(&carp_sx); 19109c2cd1aaSGleb Smirnoff return (ENOPROTOOPT); 19119c2cd1aaSGleb Smirnoff } 19129c2cd1aaSGleb Smirnoff 191308b68b0eSGleb Smirnoff IFNET_FOREACH_CARP(ifp, sc) 191408b68b0eSGleb Smirnoff if (sc->sc_vhid == vhid) 191508b68b0eSGleb Smirnoff break; 1916a9a2c40cSGleb Smirnoff if (sc == NULL) { 19179c2cd1aaSGleb Smirnoff sx_xunlock(&carp_sx); 191808b68b0eSGleb Smirnoff return (ENOENT); 1919a9a2c40cSGleb Smirnoff } 192008b68b0eSGleb Smirnoff 1921a9a2c40cSGleb Smirnoff error = carp_multicast_setup(cif, ifa->ifa_addr->sa_family); 1922a9a2c40cSGleb Smirnoff if (error) { 1923a9a2c40cSGleb Smirnoff CIF_FREE(cif); 19249c2cd1aaSGleb Smirnoff sx_xunlock(&carp_sx); 192508b68b0eSGleb Smirnoff return (error); 1926a9a2c40cSGleb Smirnoff } 192708b68b0eSGleb Smirnoff 192808b68b0eSGleb Smirnoff index = sc->sc_naddrs + sc->sc_naddrs6 + 1; 192908b68b0eSGleb Smirnoff if (index > sc->sc_ifasiz / sizeof(struct ifaddr *)) 19309c2cd1aaSGleb Smirnoff carp_grow_ifas(sc); 193108b68b0eSGleb Smirnoff 193208b68b0eSGleb Smirnoff switch (ifa->ifa_addr->sa_family) { 193308b68b0eSGleb Smirnoff #ifdef INET 193408b68b0eSGleb Smirnoff case AF_INET: 1935a9a2c40cSGleb Smirnoff cif->cif_naddrs++; 193608b68b0eSGleb Smirnoff sc->sc_naddrs++; 193708b68b0eSGleb Smirnoff break; 193808b68b0eSGleb Smirnoff #endif 193908b68b0eSGleb Smirnoff #ifdef INET6 194008b68b0eSGleb Smirnoff case AF_INET6: 1941a9a2c40cSGleb Smirnoff cif->cif_naddrs6++; 194208b68b0eSGleb Smirnoff sc->sc_naddrs6++; 194308b68b0eSGleb Smirnoff break; 194408b68b0eSGleb Smirnoff #endif 194508b68b0eSGleb Smirnoff } 194608b68b0eSGleb Smirnoff 194708b68b0eSGleb Smirnoff ifa_ref(ifa); 19489c2cd1aaSGleb Smirnoff 19499c2cd1aaSGleb Smirnoff CARP_LOCK(sc); 195008b68b0eSGleb Smirnoff sc->sc_ifas[index - 1] = ifa; 195108b68b0eSGleb Smirnoff ifa->ifa_carp = sc; 195208b68b0eSGleb Smirnoff carp_hmac_prepare(sc); 195308b68b0eSGleb Smirnoff carp_sc_state(sc); 195408b68b0eSGleb Smirnoff CARP_UNLOCK(sc); 19559c2cd1aaSGleb Smirnoff 19569c2cd1aaSGleb Smirnoff sx_xunlock(&carp_sx); 195708b68b0eSGleb Smirnoff 195808b68b0eSGleb Smirnoff return (0); 195908b68b0eSGleb Smirnoff } 196008b68b0eSGleb Smirnoff 196108b68b0eSGleb Smirnoff void 1962338e227aSLuiz Otavio O Souza carp_detach(struct ifaddr *ifa, bool keep_cif) 196308b68b0eSGleb Smirnoff { 1964a9a2c40cSGleb Smirnoff struct ifnet *ifp = ifa->ifa_ifp; 1965a9a2c40cSGleb Smirnoff struct carp_if *cif = ifp->if_carp; 196608b68b0eSGleb Smirnoff struct carp_softc *sc = ifa->ifa_carp; 196708b68b0eSGleb Smirnoff int i, index; 196808b68b0eSGleb Smirnoff 196908b68b0eSGleb Smirnoff KASSERT(sc != NULL, ("%s: %p not attached", __func__, ifa)); 197008b68b0eSGleb Smirnoff 19719c2cd1aaSGleb Smirnoff sx_xlock(&carp_sx); 197208b68b0eSGleb Smirnoff 19739c2cd1aaSGleb Smirnoff CARP_LOCK(sc); 197408b68b0eSGleb Smirnoff /* Shift array. */ 197508b68b0eSGleb Smirnoff index = sc->sc_naddrs + sc->sc_naddrs6; 197608b68b0eSGleb Smirnoff for (i = 0; i < index; i++) 197708b68b0eSGleb Smirnoff if (sc->sc_ifas[i] == ifa) 197808b68b0eSGleb Smirnoff break; 197908b68b0eSGleb Smirnoff KASSERT(i < index, ("%s: %p no backref", __func__, ifa)); 198008b68b0eSGleb Smirnoff for (; i < index - 1; i++) 198108b68b0eSGleb Smirnoff sc->sc_ifas[i] = sc->sc_ifas[i+1]; 198208b68b0eSGleb Smirnoff sc->sc_ifas[index - 1] = NULL; 198308b68b0eSGleb Smirnoff 198408b68b0eSGleb Smirnoff switch (ifa->ifa_addr->sa_family) { 198508b68b0eSGleb Smirnoff #ifdef INET 198608b68b0eSGleb Smirnoff case AF_INET: 1987a9a2c40cSGleb Smirnoff cif->cif_naddrs--; 198808b68b0eSGleb Smirnoff sc->sc_naddrs--; 198908b68b0eSGleb Smirnoff break; 199008b68b0eSGleb Smirnoff #endif 199108b68b0eSGleb Smirnoff #ifdef INET6 199208b68b0eSGleb Smirnoff case AF_INET6: 1993a9a2c40cSGleb Smirnoff cif->cif_naddrs6--; 199408b68b0eSGleb Smirnoff sc->sc_naddrs6--; 199508b68b0eSGleb Smirnoff break; 199608b68b0eSGleb Smirnoff #endif 199708b68b0eSGleb Smirnoff } 199808b68b0eSGleb Smirnoff 19992512b096SGleb Smirnoff carp_ifa_delroute(ifa); 2000a9a2c40cSGleb Smirnoff carp_multicast_cleanup(cif, ifa->ifa_addr->sa_family); 200108b68b0eSGleb Smirnoff 200208b68b0eSGleb Smirnoff ifa->ifa_carp = NULL; 200308b68b0eSGleb Smirnoff ifa_free(ifa); 200408b68b0eSGleb Smirnoff 200508b68b0eSGleb Smirnoff carp_hmac_prepare(sc); 200608b68b0eSGleb Smirnoff carp_sc_state(sc); 200708b68b0eSGleb Smirnoff 2008338e227aSLuiz Otavio O Souza if (!keep_cif && sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) 200908b68b0eSGleb Smirnoff carp_destroy(sc); 20109c2cd1aaSGleb Smirnoff else 201108b68b0eSGleb Smirnoff CARP_UNLOCK(sc); 20129c2cd1aaSGleb Smirnoff 2013338e227aSLuiz Otavio O Souza if (!keep_cif) 20149c2cd1aaSGleb Smirnoff CIF_FREE(cif); 20159c2cd1aaSGleb Smirnoff 20169c2cd1aaSGleb Smirnoff sx_xunlock(&carp_sx); 201708b68b0eSGleb Smirnoff } 201808b68b0eSGleb Smirnoff 201908b68b0eSGleb Smirnoff static void 2020d01641e2SWill Andrews carp_set_state(struct carp_softc *sc, int state, const char *reason) 202108b68b0eSGleb Smirnoff { 202208b68b0eSGleb Smirnoff 202308b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); 202408b68b0eSGleb Smirnoff 202508b68b0eSGleb Smirnoff if (sc->sc_state != state) { 202608b68b0eSGleb Smirnoff const char *carp_states[] = { CARP_STATES }; 202708b68b0eSGleb Smirnoff char subsys[IFNAMSIZ+5]; 202808b68b0eSGleb Smirnoff 202908b68b0eSGleb Smirnoff snprintf(subsys, IFNAMSIZ+5, "%u@%s", sc->sc_vhid, 203008b68b0eSGleb Smirnoff sc->sc_carpdev->if_xname); 2031d01641e2SWill Andrews 2032d01641e2SWill Andrews CARP_LOG("%s: %s -> %s (%s)\n", subsys, 2033d01641e2SWill Andrews carp_states[sc->sc_state], carp_states[state], reason); 2034d01641e2SWill Andrews 2035d01641e2SWill Andrews sc->sc_state = state; 2036d01641e2SWill Andrews 203708b68b0eSGleb Smirnoff devctl_notify("CARP", subsys, carp_states[state], NULL); 203808b68b0eSGleb Smirnoff } 203908b68b0eSGleb Smirnoff } 204008b68b0eSGleb Smirnoff 204108b68b0eSGleb Smirnoff static void 204208b68b0eSGleb Smirnoff carp_linkstate(struct ifnet *ifp) 2043d220759bSGleb Smirnoff { 2044d220759bSGleb Smirnoff struct carp_softc *sc; 2045d220759bSGleb Smirnoff 204608b68b0eSGleb Smirnoff CIF_LOCK(ifp->if_carp); 204708b68b0eSGleb Smirnoff IFNET_FOREACH_CARP(ifp, sc) { 204808b68b0eSGleb Smirnoff CARP_LOCK(sc); 204908b68b0eSGleb Smirnoff carp_sc_state(sc); 205008b68b0eSGleb Smirnoff CARP_UNLOCK(sc); 205108b68b0eSGleb Smirnoff } 205208b68b0eSGleb Smirnoff CIF_UNLOCK(ifp->if_carp); 20534cb39345SGleb Smirnoff } 20544cb39345SGleb Smirnoff 20554cb39345SGleb Smirnoff static void 205608b68b0eSGleb Smirnoff carp_sc_state(struct carp_softc *sc) 20574cb39345SGleb Smirnoff { 205808b68b0eSGleb Smirnoff 205908b68b0eSGleb Smirnoff CARP_LOCK_ASSERT(sc); 20604cb39345SGleb Smirnoff 2061e8c34a71SGleb Smirnoff if (sc->sc_carpdev->if_link_state != LINK_STATE_UP || 2062167a3440SAlexander Motin !(sc->sc_carpdev->if_flags & IFF_UP) || 2063167a3440SAlexander Motin !V_carp_allow) { 2064a9771948SGleb Smirnoff callout_stop(&sc->sc_ad_tmo); 206508b68b0eSGleb Smirnoff #ifdef INET 2066a9771948SGleb Smirnoff callout_stop(&sc->sc_md_tmo); 206708b68b0eSGleb Smirnoff #endif 206808b68b0eSGleb Smirnoff #ifdef INET6 2069a9771948SGleb Smirnoff callout_stop(&sc->sc_md6_tmo); 207008b68b0eSGleb Smirnoff #endif 2071bb269f3aSWill Andrews carp_set_state(sc, INIT, "hardware interface down"); 2072a9771948SGleb Smirnoff carp_setrun(sc, 0); 2073f08535f8SGleb Smirnoff if (!sc->sc_suppress) 2074c5c392e7SMikolaj Golub carp_demote_adj(V_carp_ifdown_adj, "interface down"); 2075a9771948SGleb Smirnoff sc->sc_suppress = 1; 2076a9771948SGleb Smirnoff } else { 2077bb269f3aSWill Andrews carp_set_state(sc, INIT, "hardware interface up"); 2078a9771948SGleb Smirnoff carp_setrun(sc, 0); 2079a9771948SGleb Smirnoff if (sc->sc_suppress) 2080c5c392e7SMikolaj Golub carp_demote_adj(-V_carp_ifdown_adj, "interface up"); 2081a9771948SGleb Smirnoff sc->sc_suppress = 0; 2082a9771948SGleb Smirnoff } 2083a9771948SGleb Smirnoff } 2084a9771948SGleb Smirnoff 2085f08535f8SGleb Smirnoff static void 2086f08535f8SGleb Smirnoff carp_demote_adj(int adj, char *reason) 2087f08535f8SGleb Smirnoff { 2088c5c392e7SMikolaj Golub atomic_add_int(&V_carp_demotion, adj); 2089c5c392e7SMikolaj Golub CARP_LOG("demoted by %d to %d (%s)\n", adj, V_carp_demotion, reason); 2090f08535f8SGleb Smirnoff taskqueue_enqueue(taskqueue_swi, &carp_sendall_task); 2091f08535f8SGleb Smirnoff } 209208b68b0eSGleb Smirnoff 20937951008bSGleb Smirnoff static int 2094167a3440SAlexander Motin carp_allow_sysctl(SYSCTL_HANDLER_ARGS) 2095167a3440SAlexander Motin { 2096167a3440SAlexander Motin int new, error; 2097167a3440SAlexander Motin struct carp_softc *sc; 2098167a3440SAlexander Motin 2099167a3440SAlexander Motin new = V_carp_allow; 2100167a3440SAlexander Motin error = sysctl_handle_int(oidp, &new, 0, req); 2101167a3440SAlexander Motin if (error || !req->newptr) 2102167a3440SAlexander Motin return (error); 2103167a3440SAlexander Motin 2104167a3440SAlexander Motin if (V_carp_allow != new) { 2105167a3440SAlexander Motin V_carp_allow = new; 2106167a3440SAlexander Motin 2107167a3440SAlexander Motin mtx_lock(&carp_mtx); 2108167a3440SAlexander Motin LIST_FOREACH(sc, &carp_list, sc_next) { 2109167a3440SAlexander Motin CARP_LOCK(sc); 2110167a3440SAlexander Motin if (curvnet == sc->sc_carpdev->if_vnet) 2111167a3440SAlexander Motin carp_sc_state(sc); 2112167a3440SAlexander Motin CARP_UNLOCK(sc); 2113167a3440SAlexander Motin } 2114167a3440SAlexander Motin mtx_unlock(&carp_mtx); 2115167a3440SAlexander Motin } 2116167a3440SAlexander Motin 2117167a3440SAlexander Motin return (0); 2118167a3440SAlexander Motin } 2119167a3440SAlexander Motin 2120167a3440SAlexander Motin static int 21210d3d234cSKristof Provost carp_dscp_sysctl(SYSCTL_HANDLER_ARGS) 21220d3d234cSKristof Provost { 21230d3d234cSKristof Provost int new, error; 21240d3d234cSKristof Provost 21250d3d234cSKristof Provost new = V_carp_dscp; 21260d3d234cSKristof Provost error = sysctl_handle_int(oidp, &new, 0, req); 21270d3d234cSKristof Provost if (error || !req->newptr) 21280d3d234cSKristof Provost return (error); 21290d3d234cSKristof Provost 21300d3d234cSKristof Provost if (new < 0 || new > 63) 21310d3d234cSKristof Provost return (EINVAL); 21320d3d234cSKristof Provost 21330d3d234cSKristof Provost V_carp_dscp = new; 21340d3d234cSKristof Provost 21350d3d234cSKristof Provost return (0); 21360d3d234cSKristof Provost } 21370d3d234cSKristof Provost 21380d3d234cSKristof Provost static int 21397951008bSGleb Smirnoff carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS) 21407951008bSGleb Smirnoff { 21417951008bSGleb Smirnoff int new, error; 21427951008bSGleb Smirnoff 2143c5c392e7SMikolaj Golub new = V_carp_demotion; 21447951008bSGleb Smirnoff error = sysctl_handle_int(oidp, &new, 0, req); 21457951008bSGleb Smirnoff if (error || !req->newptr) 21467951008bSGleb Smirnoff return (error); 21477951008bSGleb Smirnoff 21487951008bSGleb Smirnoff carp_demote_adj(new, "sysctl"); 21497951008bSGleb Smirnoff 21507951008bSGleb Smirnoff return (0); 21517951008bSGleb Smirnoff } 21527951008bSGleb Smirnoff 215354bfbd51SWill Andrews #ifdef INET 215454bfbd51SWill Andrews extern struct domain inetdomain; 215554bfbd51SWill Andrews static struct protosw in_carp_protosw = { 215654bfbd51SWill Andrews .pr_type = SOCK_RAW, 215754bfbd51SWill Andrews .pr_domain = &inetdomain, 215854bfbd51SWill Andrews .pr_protocol = IPPROTO_CARP, 215954bfbd51SWill Andrews .pr_flags = PR_ATOMIC|PR_ADDR, 216054bfbd51SWill Andrews .pr_input = carp_input, 216173d76e77SKevin Lo .pr_output = rip_output, 216254bfbd51SWill Andrews .pr_ctloutput = rip_ctloutput, 216354bfbd51SWill Andrews .pr_usrreqs = &rip_usrreqs 216454bfbd51SWill Andrews }; 216554bfbd51SWill Andrews #endif 216654bfbd51SWill Andrews 216754bfbd51SWill Andrews #ifdef INET6 216854bfbd51SWill Andrews extern struct domain inet6domain; 21698f5a8818SKevin Lo static struct protosw in6_carp_protosw = { 217054bfbd51SWill Andrews .pr_type = SOCK_RAW, 217154bfbd51SWill Andrews .pr_domain = &inet6domain, 217254bfbd51SWill Andrews .pr_protocol = IPPROTO_CARP, 217354bfbd51SWill Andrews .pr_flags = PR_ATOMIC|PR_ADDR, 217454bfbd51SWill Andrews .pr_input = carp6_input, 217573d76e77SKevin Lo .pr_output = rip6_output, 217654bfbd51SWill Andrews .pr_ctloutput = rip6_ctloutput, 217754bfbd51SWill Andrews .pr_usrreqs = &rip6_usrreqs 217854bfbd51SWill Andrews }; 217954bfbd51SWill Andrews #endif 218054bfbd51SWill Andrews 218154bfbd51SWill Andrews static void 218254bfbd51SWill Andrews carp_mod_cleanup(void) 2183a9771948SGleb Smirnoff { 218454bfbd51SWill Andrews 218554bfbd51SWill Andrews #ifdef INET 218654bfbd51SWill Andrews if (proto_reg[CARP_INET] == 0) { 218715249f73SWill Andrews (void)ipproto_unregister(IPPROTO_CARP); 218854bfbd51SWill Andrews pf_proto_unregister(PF_INET, IPPROTO_CARP, SOCK_RAW); 218954bfbd51SWill Andrews proto_reg[CARP_INET] = -1; 219054bfbd51SWill Andrews } 219154bfbd51SWill Andrews carp_iamatch_p = NULL; 219254bfbd51SWill Andrews #endif 219354bfbd51SWill Andrews #ifdef INET6 219454bfbd51SWill Andrews if (proto_reg[CARP_INET6] == 0) { 219515249f73SWill Andrews (void)ip6proto_unregister(IPPROTO_CARP); 219654bfbd51SWill Andrews pf_proto_unregister(PF_INET6, IPPROTO_CARP, SOCK_RAW); 219754bfbd51SWill Andrews proto_reg[CARP_INET6] = -1; 219854bfbd51SWill Andrews } 219954bfbd51SWill Andrews carp_iamatch6_p = NULL; 220054bfbd51SWill Andrews carp_macmatch6_p = NULL; 220154bfbd51SWill Andrews #endif 220208b68b0eSGleb Smirnoff carp_ioctl_p = NULL; 220308b68b0eSGleb Smirnoff carp_attach_p = NULL; 220408b68b0eSGleb Smirnoff carp_detach_p = NULL; 220508b68b0eSGleb Smirnoff carp_get_vhid_p = NULL; 220654bfbd51SWill Andrews carp_linkstate_p = NULL; 220754bfbd51SWill Andrews carp_forus_p = NULL; 220854bfbd51SWill Andrews carp_output_p = NULL; 2209f08535f8SGleb Smirnoff carp_demote_adj_p = NULL; 221024421c1cSGleb Smirnoff carp_master_p = NULL; 2211f08535f8SGleb Smirnoff mtx_unlock(&carp_mtx); 2212f08535f8SGleb Smirnoff taskqueue_drain(taskqueue_swi, &carp_sendall_task); 221354bfbd51SWill Andrews mtx_destroy(&carp_mtx); 221493d4534cSGleb Smirnoff sx_destroy(&carp_sx); 221554bfbd51SWill Andrews } 221654bfbd51SWill Andrews 221754bfbd51SWill Andrews static int 221854bfbd51SWill Andrews carp_mod_load(void) 221954bfbd51SWill Andrews { 222015249f73SWill Andrews int err; 222154bfbd51SWill Andrews 2222d92d54d5SGleb Smirnoff mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF); 222393d4534cSGleb Smirnoff sx_init(&carp_sx, "carp_sx"); 222408b68b0eSGleb Smirnoff LIST_INIT(&carp_list); 222508b68b0eSGleb Smirnoff carp_get_vhid_p = carp_get_vhid; 222654bfbd51SWill Andrews carp_forus_p = carp_forus; 222754bfbd51SWill Andrews carp_output_p = carp_output; 222808b68b0eSGleb Smirnoff carp_linkstate_p = carp_linkstate; 222908b68b0eSGleb Smirnoff carp_ioctl_p = carp_ioctl; 223008b68b0eSGleb Smirnoff carp_attach_p = carp_attach; 223108b68b0eSGleb Smirnoff carp_detach_p = carp_detach; 2232f08535f8SGleb Smirnoff carp_demote_adj_p = carp_demote_adj; 223324421c1cSGleb Smirnoff carp_master_p = carp_master; 223454bfbd51SWill Andrews #ifdef INET6 223554bfbd51SWill Andrews carp_iamatch6_p = carp_iamatch6; 223654bfbd51SWill Andrews carp_macmatch6_p = carp_macmatch6; 223754bfbd51SWill Andrews proto_reg[CARP_INET6] = pf_proto_register(PF_INET6, 223854bfbd51SWill Andrews (struct protosw *)&in6_carp_protosw); 223908b68b0eSGleb Smirnoff if (proto_reg[CARP_INET6]) { 224054bfbd51SWill Andrews printf("carp: error %d attaching to PF_INET6\n", 224154bfbd51SWill Andrews proto_reg[CARP_INET6]); 224254bfbd51SWill Andrews carp_mod_cleanup(); 22436baf7a24SGleb Smirnoff return (proto_reg[CARP_INET6]); 224454bfbd51SWill Andrews } 224515249f73SWill Andrews err = ip6proto_register(IPPROTO_CARP); 224615249f73SWill Andrews if (err) { 224715249f73SWill Andrews printf("carp: error %d registering with INET6\n", err); 224815249f73SWill Andrews carp_mod_cleanup(); 22496baf7a24SGleb Smirnoff return (err); 225015249f73SWill Andrews } 225154bfbd51SWill Andrews #endif 225254bfbd51SWill Andrews #ifdef INET 225354bfbd51SWill Andrews carp_iamatch_p = carp_iamatch; 225454bfbd51SWill Andrews proto_reg[CARP_INET] = pf_proto_register(PF_INET, &in_carp_protosw); 225508b68b0eSGleb Smirnoff if (proto_reg[CARP_INET]) { 225654bfbd51SWill Andrews printf("carp: error %d attaching to PF_INET\n", 225754bfbd51SWill Andrews proto_reg[CARP_INET]); 225854bfbd51SWill Andrews carp_mod_cleanup(); 22596baf7a24SGleb Smirnoff return (proto_reg[CARP_INET]); 226054bfbd51SWill Andrews } 226115249f73SWill Andrews err = ipproto_register(IPPROTO_CARP); 226215249f73SWill Andrews if (err) { 226315249f73SWill Andrews printf("carp: error %d registering with INET\n", err); 226415249f73SWill Andrews carp_mod_cleanup(); 22656baf7a24SGleb Smirnoff return (err); 226615249f73SWill Andrews } 226754bfbd51SWill Andrews #endif 226808b68b0eSGleb Smirnoff return (0); 226954bfbd51SWill Andrews } 2270a9771948SGleb Smirnoff 227154bfbd51SWill Andrews static int 227254bfbd51SWill Andrews carp_modevent(module_t mod, int type, void *data) 227354bfbd51SWill Andrews { 227454bfbd51SWill Andrews switch (type) { 227554bfbd51SWill Andrews case MOD_LOAD: 227654bfbd51SWill Andrews return carp_mod_load(); 227754bfbd51SWill Andrews /* NOTREACHED */ 2278a9771948SGleb Smirnoff case MOD_UNLOAD: 227908b68b0eSGleb Smirnoff mtx_lock(&carp_mtx); 228008b68b0eSGleb Smirnoff if (LIST_EMPTY(&carp_list)) 228154bfbd51SWill Andrews carp_mod_cleanup(); 228208b68b0eSGleb Smirnoff else { 228308b68b0eSGleb Smirnoff mtx_unlock(&carp_mtx); 228454bfbd51SWill Andrews return (EBUSY); 228508b68b0eSGleb Smirnoff } 2286a9771948SGleb Smirnoff break; 2287a9771948SGleb Smirnoff 2288a9771948SGleb Smirnoff default: 22890fa08018SGleb Smirnoff return (EINVAL); 2290a9771948SGleb Smirnoff } 2291a9771948SGleb Smirnoff 22920fa08018SGleb Smirnoff return (0); 2293a9771948SGleb Smirnoff } 2294a9771948SGleb Smirnoff 2295a9771948SGleb Smirnoff static moduledata_t carp_mod = { 2296a9771948SGleb Smirnoff "carp", 2297a9771948SGleb Smirnoff carp_modevent, 22989823d527SKevin Lo 0 2299a9771948SGleb Smirnoff }; 2300a9771948SGleb Smirnoff 2301e24fa11dSWill Andrews DECLARE_MODULE(carp, carp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY); 2302