xref: /freebsd/sys/netinet/ip_carp.c (revision 49cad3daf2a3abe8270f71ee5b15b8990a19529d)
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>
40773e541eSWarner Losh #include <sys/devctl.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>
4908b68b0eSGleb Smirnoff #include <sys/socket.h>
5008b68b0eSGleb Smirnoff #include <sys/sockio.h>
51a9771948SGleb Smirnoff #include <sys/sysctl.h>
52a9771948SGleb Smirnoff #include <sys/syslog.h>
53f08535f8SGleb Smirnoff #include <sys/taskqueue.h>
5469edf037SAndrey V. Elsukov #include <sys/counter.h>
55a9771948SGleb Smirnoff 
56a9771948SGleb Smirnoff #include <net/ethernet.h>
57a9771948SGleb Smirnoff #include <net/if.h>
5876039bc8SGleb Smirnoff #include <net/if_var.h>
59433aaf04SRuslan Ermilov #include <net/if_dl.h>
6008b68b0eSGleb Smirnoff #include <net/if_llatbl.h>
613d0d5b21SJustin Hibbits #include <net/if_private.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 /*
15208b68b0eSGleb Smirnoff  * Brief design of carp(4).
15308b68b0eSGleb Smirnoff  *
15408b68b0eSGleb Smirnoff  * Any carp-capable ifnet may have a list of carp softcs hanging off
15508b68b0eSGleb Smirnoff  * its ifp->if_carp pointer. Each softc represents one unique virtual
15608b68b0eSGleb Smirnoff  * host id, or vhid. The softc has a back pointer to the ifnet. All
15708b68b0eSGleb Smirnoff  * softcs are joined in a global list, which has quite limited use.
15808b68b0eSGleb Smirnoff  *
15908b68b0eSGleb Smirnoff  * Any interface address that takes part in CARP negotiation has a
16008b68b0eSGleb Smirnoff  * pointer to the softc of its vhid, ifa->ifa_carp. That could be either
16108b68b0eSGleb Smirnoff  * AF_INET or AF_INET6 address.
16208b68b0eSGleb Smirnoff  *
16308b68b0eSGleb Smirnoff  * Although, one can get the softc's backpointer to ifnet and traverse
16408b68b0eSGleb Smirnoff  * through its ifp->if_addrhead queue to find all interface addresses
16508b68b0eSGleb Smirnoff  * involved in CARP, we keep a growable array of ifaddr pointers. This
16608b68b0eSGleb Smirnoff  * allows us to avoid grabbing the IF_ADDR_LOCK() in many traversals that
16708b68b0eSGleb Smirnoff  * do calls into the network stack, thus avoiding LORs.
16808b68b0eSGleb Smirnoff  *
16908b68b0eSGleb Smirnoff  * Locking:
17008b68b0eSGleb Smirnoff  *
17108b68b0eSGleb Smirnoff  * Each softc has a lock sc_mtx. It is used to synchronise carp_input_c(),
17208b68b0eSGleb Smirnoff  * callout-driven events and ioctl()s.
17308b68b0eSGleb Smirnoff  *
17481098a01SAlexander Motin  * To traverse the list of softcs on an ifnet we use CIF_LOCK() or carp_sx.
17581098a01SAlexander Motin  * To traverse the global list we use the mutex carp_mtx.
17608b68b0eSGleb Smirnoff  *
17708b68b0eSGleb Smirnoff  * Known issues with locking:
17808b68b0eSGleb Smirnoff  *
17908b68b0eSGleb Smirnoff  * - Sending ad, we put the pointer to the softc in an mtag, and no reference
18008b68b0eSGleb Smirnoff  *   counting is done on the softc.
18108b68b0eSGleb Smirnoff  * - On module unload we may race (?) with packet processing thread
18208b68b0eSGleb Smirnoff  *   dereferencing our function pointers.
18308b68b0eSGleb Smirnoff  */
184a9771948SGleb Smirnoff 
185c5c392e7SMikolaj Golub /* Accept incoming CARP packets. */
1865f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_allow) = 1;
187c5c392e7SMikolaj Golub #define	V_carp_allow	VNET(carp_allow)
188c5c392e7SMikolaj Golub 
1890d3d234cSKristof Provost /* Set DSCP in outgoing CARP packets. */
1905f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_dscp) = 56;
1910d3d234cSKristof Provost #define	V_carp_dscp	VNET(carp_dscp)
1920d3d234cSKristof Provost 
193c5c392e7SMikolaj Golub /* Preempt slower nodes. */
1945f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_preempt) = 0;
195c5c392e7SMikolaj Golub #define	V_carp_preempt	VNET(carp_preempt)
196c5c392e7SMikolaj Golub 
197c5c392e7SMikolaj Golub /* Log level. */
1985f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_log) = 1;
199c5c392e7SMikolaj Golub #define	V_carp_log	VNET(carp_log)
200c5c392e7SMikolaj Golub 
201c5c392e7SMikolaj Golub /* Global advskew demotion. */
2025f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_demotion) = 0;
203c5c392e7SMikolaj Golub #define	V_carp_demotion	VNET(carp_demotion)
204c5c392e7SMikolaj Golub 
205c5c392e7SMikolaj Golub /* Send error demotion factor. */
2065f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_senderr_adj) = CARP_MAXSKEW;
207c5c392e7SMikolaj Golub #define	V_carp_senderr_adj	VNET(carp_senderr_adj)
208c5c392e7SMikolaj Golub 
209c5c392e7SMikolaj Golub /* Iface down demotion factor. */
2105f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_ifdown_adj) = CARP_MAXSKEW;
211c5c392e7SMikolaj Golub #define	V_carp_ifdown_adj	VNET(carp_ifdown_adj)
212c5c392e7SMikolaj Golub 
213167a3440SAlexander Motin static int carp_allow_sysctl(SYSCTL_HANDLER_ARGS);
2140d3d234cSKristof Provost static int carp_dscp_sysctl(SYSCTL_HANDLER_ARGS);
2157951008bSGleb Smirnoff static int carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS);
216a9771948SGleb Smirnoff 
21710b49b23SPawel Biernacki SYSCTL_NODE(_net_inet, IPPROTO_CARP, carp, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
21810b49b23SPawel Biernacki     "CARP");
219167a3440SAlexander Motin SYSCTL_PROC(_net_inet_carp, OID_AUTO, allow,
220ee49c5d3SBoris Lytochkin     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
221ee49c5d3SBoris Lytochkin     &VNET_NAME(carp_allow), 0, carp_allow_sysctl, "I",
222167a3440SAlexander Motin     "Accept incoming CARP packets");
2230d3d234cSKristof Provost SYSCTL_PROC(_net_inet_carp, OID_AUTO, dscp,
22410b49b23SPawel Biernacki     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
22510b49b23SPawel Biernacki     0, 0, carp_dscp_sysctl, "I",
2260d3d234cSKristof Provost     "DSCP value for carp packets");
2276df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, preempt, CTLFLAG_VNET | CTLFLAG_RW,
228c5c392e7SMikolaj Golub     &VNET_NAME(carp_preempt), 0, "High-priority backup preemption mode");
2296df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, log, CTLFLAG_VNET | CTLFLAG_RW,
230c5c392e7SMikolaj Golub     &VNET_NAME(carp_log), 0, "CARP log level");
2316df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_carp, OID_AUTO, demotion,
23210b49b23SPawel Biernacki     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
2337951008bSGleb Smirnoff     0, 0, carp_demote_adj_sysctl, "I",
2347951008bSGleb Smirnoff     "Adjust demotion factor (skew of advskew)");
2356df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, senderr_demotion_factor,
2366df8a710SGleb Smirnoff     CTLFLAG_VNET | CTLFLAG_RW,
237c5c392e7SMikolaj Golub     &VNET_NAME(carp_senderr_adj), 0, "Send error demotion factor adjustment");
2386df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, ifdown_demotion_factor,
2396df8a710SGleb Smirnoff     CTLFLAG_VNET | CTLFLAG_RW,
240c5c392e7SMikolaj Golub     &VNET_NAME(carp_ifdown_adj), 0,
241c5c392e7SMikolaj Golub     "Interface down demotion factor adjustment");
242f08535f8SGleb Smirnoff 
243c5c392e7SMikolaj Golub VNET_PCPUSTAT_DEFINE(struct carpstats, carpstats);
244c5c392e7SMikolaj Golub VNET_PCPUSTAT_SYSINIT(carpstats);
245c5c392e7SMikolaj Golub VNET_PCPUSTAT_SYSUNINIT(carpstats);
246c5c392e7SMikolaj Golub 
24769edf037SAndrey V. Elsukov #define	CARPSTATS_ADD(name, val)	\
248c5c392e7SMikolaj Golub     counter_u64_add(VNET(carpstats)[offsetof(struct carpstats, name) / \
24969edf037SAndrey V. Elsukov 	sizeof(uint64_t)], (val))
25069edf037SAndrey V. Elsukov #define	CARPSTATS_INC(name)		CARPSTATS_ADD(name, 1)
25169edf037SAndrey V. Elsukov 
252c5c392e7SMikolaj Golub SYSCTL_VNET_PCPUSTAT(_net_inet_carp, OID_AUTO, stats, struct carpstats,
253c5c392e7SMikolaj Golub     carpstats, "CARP statistics (struct carpstats, netinet/ip_carp.h)");
254a9771948SGleb Smirnoff 
25508b68b0eSGleb Smirnoff #define	CARP_LOCK_INIT(sc)	mtx_init(&(sc)->sc_mtx, "carp_softc",   \
256a9771948SGleb Smirnoff 	NULL, MTX_DEF)
25708b68b0eSGleb Smirnoff #define	CARP_LOCK_DESTROY(sc)	mtx_destroy(&(sc)->sc_mtx)
25808b68b0eSGleb Smirnoff #define	CARP_LOCK_ASSERT(sc)	mtx_assert(&(sc)->sc_mtx, MA_OWNED)
25908b68b0eSGleb Smirnoff #define	CARP_LOCK(sc)		mtx_lock(&(sc)->sc_mtx)
26008b68b0eSGleb Smirnoff #define	CARP_UNLOCK(sc)		mtx_unlock(&(sc)->sc_mtx)
26108b68b0eSGleb Smirnoff #define	CIF_LOCK_INIT(cif)	mtx_init(&(cif)->cif_mtx, "carp_if",   \
26208b68b0eSGleb Smirnoff 	NULL, MTX_DEF)
26308b68b0eSGleb Smirnoff #define	CIF_LOCK_DESTROY(cif)	mtx_destroy(&(cif)->cif_mtx)
26408b68b0eSGleb Smirnoff #define	CIF_LOCK_ASSERT(cif)	mtx_assert(&(cif)->cif_mtx, MA_OWNED)
26508b68b0eSGleb Smirnoff #define	CIF_LOCK(cif)		mtx_lock(&(cif)->cif_mtx)
26608b68b0eSGleb Smirnoff #define	CIF_UNLOCK(cif)		mtx_unlock(&(cif)->cif_mtx)
267a9a2c40cSGleb Smirnoff #define	CIF_FREE(cif)	do {				\
2689c2cd1aaSGleb Smirnoff 		CIF_LOCK(cif);				\
269a9a2c40cSGleb Smirnoff 		if (TAILQ_EMPTY(&(cif)->cif_vrs))	\
270a9a2c40cSGleb Smirnoff 			carp_free_if(cif);		\
271a9a2c40cSGleb Smirnoff 		else					\
272a9a2c40cSGleb Smirnoff 			CIF_UNLOCK(cif);		\
273a9a2c40cSGleb Smirnoff } while (0)
274d220759bSGleb Smirnoff 
275947b7cf3SGleb Smirnoff #define	CARP_LOG(...)	do {				\
276c5c392e7SMikolaj Golub 	if (V_carp_log > 0)				\
27708b68b0eSGleb Smirnoff 		log(LOG_INFO, "carp: " __VA_ARGS__);	\
278947b7cf3SGleb Smirnoff } while (0)
2791e9e6572SGleb Smirnoff 
280947b7cf3SGleb Smirnoff #define	CARP_DEBUG(...)	do {				\
281c5c392e7SMikolaj Golub 	if (V_carp_log > 1)				\
2821e9e6572SGleb Smirnoff 		log(LOG_DEBUG, __VA_ARGS__);		\
283947b7cf3SGleb Smirnoff } while (0)
284a9771948SGleb Smirnoff 
28508b68b0eSGleb Smirnoff #define	IFNET_FOREACH_IFA(ifp, ifa)					\
286d7c5a620SMatt Macy 	CK_STAILQ_FOREACH((ifa), &(ifp)->if_addrhead, ifa_link)	\
28708b68b0eSGleb Smirnoff 		if ((ifa)->ifa_carp != NULL)
28808b68b0eSGleb Smirnoff 
28908b68b0eSGleb Smirnoff #define	CARP_FOREACH_IFA(sc, ifa)					\
29008b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);						\
29108b68b0eSGleb Smirnoff 	for (int _i = 0;						\
29208b68b0eSGleb Smirnoff 		_i < (sc)->sc_naddrs + (sc)->sc_naddrs6 &&		\
29308b68b0eSGleb Smirnoff 		((ifa) = sc->sc_ifas[_i]) != NULL;			\
29408b68b0eSGleb Smirnoff 		++_i)
29508b68b0eSGleb Smirnoff 
29608b68b0eSGleb Smirnoff #define	IFNET_FOREACH_CARP(ifp, sc)					\
29781098a01SAlexander Motin 	KASSERT(mtx_owned(&ifp->if_carp->cif_mtx) ||			\
29881098a01SAlexander Motin 	    sx_xlocked(&carp_sx), ("cif_vrs not locked"));		\
29908b68b0eSGleb Smirnoff 	TAILQ_FOREACH((sc), &(ifp)->if_carp->cif_vrs, sc_list)
30008b68b0eSGleb Smirnoff 
301f08535f8SGleb Smirnoff #define	DEMOTE_ADVSKEW(sc)					\
302c5c392e7SMikolaj Golub     (((sc)->sc_advskew + V_carp_demotion > CARP_MAXSKEW) ?	\
3031019354bSMarius Halden     CARP_MAXSKEW :						\
3041019354bSMarius Halden         (((sc)->sc_advskew + V_carp_demotion < 0) ?		\
3051019354bSMarius Halden         0 : ((sc)->sc_advskew + V_carp_demotion)))
306f08535f8SGleb Smirnoff 
3075c1f0f6dSGleb Smirnoff static void	carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
30808b68b0eSGleb Smirnoff static struct carp_softc
30908b68b0eSGleb Smirnoff 		*carp_alloc(struct ifnet *);
31008b68b0eSGleb Smirnoff static void	carp_destroy(struct carp_softc *);
31108b68b0eSGleb Smirnoff static struct carp_if
31208b68b0eSGleb Smirnoff 		*carp_alloc_if(struct ifnet *);
31308b68b0eSGleb Smirnoff static void	carp_free_if(struct carp_if *);
314d01641e2SWill Andrews static void	carp_set_state(struct carp_softc *, int, const char* reason);
31508b68b0eSGleb Smirnoff static void	carp_sc_state(struct carp_softc *);
31608b68b0eSGleb Smirnoff static void	carp_setrun(struct carp_softc *, sa_family_t);
3175c1f0f6dSGleb Smirnoff static void	carp_master_down(void *);
318d01641e2SWill Andrews static void	carp_master_down_locked(struct carp_softc *,
319d01641e2SWill Andrews     		    const char* reason);
32008b68b0eSGleb Smirnoff static void	carp_send_ad(void *);
32108b68b0eSGleb Smirnoff static void	carp_send_ad_locked(struct carp_softc *);
32208b68b0eSGleb Smirnoff static void	carp_addroute(struct carp_softc *);
3232512b096SGleb Smirnoff static void	carp_ifa_addroute(struct ifaddr *);
32408b68b0eSGleb Smirnoff static void	carp_delroute(struct carp_softc *);
3252512b096SGleb Smirnoff static void	carp_ifa_delroute(struct ifaddr *);
326f08535f8SGleb Smirnoff static void	carp_send_ad_all(void *, int);
327f08535f8SGleb Smirnoff static void	carp_demote_adj(int, char *);
328a9771948SGleb Smirnoff 
32908b68b0eSGleb Smirnoff static LIST_HEAD(, carp_softc) carp_list;
330d92d54d5SGleb Smirnoff static struct mtx carp_mtx;
33193d4534cSGleb Smirnoff static struct sx carp_sx;
332f08535f8SGleb Smirnoff static struct task carp_sendall_task =
333f08535f8SGleb Smirnoff     TASK_INITIALIZER(0, carp_send_ad_all, NULL);
334a9771948SGleb Smirnoff 
3355c1f0f6dSGleb Smirnoff static void
336a9771948SGleb Smirnoff carp_hmac_prepare(struct carp_softc *sc)
337a9771948SGleb Smirnoff {
33808b68b0eSGleb Smirnoff 	uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
33908b68b0eSGleb Smirnoff 	uint8_t vhid = sc->sc_vhid & 0xff;
340a9771948SGleb Smirnoff 	struct ifaddr *ifa;
3411ead26d4SMax Laier 	int i, found;
3421ead26d4SMax Laier #ifdef INET
3431ead26d4SMax Laier 	struct in_addr last, cur, in;
3441ead26d4SMax Laier #endif
345a9771948SGleb Smirnoff #ifdef INET6
3461ead26d4SMax Laier 	struct in6_addr last6, cur6, in6;
347a9771948SGleb Smirnoff #endif
348a9771948SGleb Smirnoff 
34908b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
350d220759bSGleb Smirnoff 
35108b68b0eSGleb Smirnoff 	/* Compute ipad from key. */
352a9771948SGleb Smirnoff 	bzero(sc->sc_pad, sizeof(sc->sc_pad));
353a9771948SGleb Smirnoff 	bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
354a9771948SGleb Smirnoff 	for (i = 0; i < sizeof(sc->sc_pad); i++)
355a9771948SGleb Smirnoff 		sc->sc_pad[i] ^= 0x36;
356a9771948SGleb Smirnoff 
35708b68b0eSGleb Smirnoff 	/* Precompute first part of inner hash. */
358a9771948SGleb Smirnoff 	SHA1Init(&sc->sc_sha1);
359a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
360a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
361a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
362a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
363a9771948SGleb Smirnoff #ifdef INET
3641ead26d4SMax Laier 	cur.s_addr = 0;
3651ead26d4SMax Laier 	do {
3661ead26d4SMax Laier 		found = 0;
3671ead26d4SMax Laier 		last = cur;
3681ead26d4SMax Laier 		cur.s_addr = 0xffffffff;
36908b68b0eSGleb Smirnoff 		CARP_FOREACH_IFA(sc, ifa) {
3701ead26d4SMax Laier 			in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr;
3711ead26d4SMax Laier 			if (ifa->ifa_addr->sa_family == AF_INET &&
3721ead26d4SMax Laier 			    ntohl(in.s_addr) > ntohl(last.s_addr) &&
3731ead26d4SMax Laier 			    ntohl(in.s_addr) < ntohl(cur.s_addr)) {
3741ead26d4SMax Laier 				cur.s_addr = in.s_addr;
3751ead26d4SMax Laier 				found++;
376a9771948SGleb Smirnoff 			}
3771ead26d4SMax Laier 		}
3781ead26d4SMax Laier 		if (found)
3791ead26d4SMax Laier 			SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur));
3801ead26d4SMax Laier 	} while (found);
381a9771948SGleb Smirnoff #endif /* INET */
382a9771948SGleb Smirnoff #ifdef INET6
3831ead26d4SMax Laier 	memset(&cur6, 0, sizeof(cur6));
3841ead26d4SMax Laier 	do {
3851ead26d4SMax Laier 		found = 0;
3861ead26d4SMax Laier 		last6 = cur6;
3871ead26d4SMax Laier 		memset(&cur6, 0xff, sizeof(cur6));
38808b68b0eSGleb Smirnoff 		CARP_FOREACH_IFA(sc, ifa) {
389a9771948SGleb Smirnoff 			in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
3901ead26d4SMax Laier 			if (IN6_IS_SCOPE_EMBED(&in6))
3911ead26d4SMax Laier 				in6.s6_addr16[1] = 0;
3921ead26d4SMax Laier 			if (ifa->ifa_addr->sa_family == AF_INET6 &&
3931ead26d4SMax Laier 			    memcmp(&in6, &last6, sizeof(in6)) > 0 &&
3941ead26d4SMax Laier 			    memcmp(&in6, &cur6, sizeof(in6)) < 0) {
3951ead26d4SMax Laier 				cur6 = in6;
3961ead26d4SMax Laier 				found++;
397a9771948SGleb Smirnoff 			}
398a9771948SGleb Smirnoff 		}
3991ead26d4SMax Laier 		if (found)
4001ead26d4SMax Laier 			SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6));
4011ead26d4SMax Laier 	} while (found);
402a9771948SGleb Smirnoff #endif /* INET6 */
403a9771948SGleb Smirnoff 
404a9771948SGleb Smirnoff 	/* convert ipad to opad */
405a9771948SGleb Smirnoff 	for (i = 0; i < sizeof(sc->sc_pad); i++)
406a9771948SGleb Smirnoff 		sc->sc_pad[i] ^= 0x36 ^ 0x5c;
407a9771948SGleb Smirnoff }
408a9771948SGleb Smirnoff 
4095c1f0f6dSGleb Smirnoff static void
41008b68b0eSGleb Smirnoff carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
411a9771948SGleb Smirnoff     unsigned char md[20])
412a9771948SGleb Smirnoff {
413a9771948SGleb Smirnoff 	SHA1_CTX sha1ctx;
414a9771948SGleb Smirnoff 
41508b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
41608b68b0eSGleb Smirnoff 
417a9771948SGleb Smirnoff 	/* fetch first half of inner hash */
418a9771948SGleb Smirnoff 	bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
419a9771948SGleb Smirnoff 
420a9771948SGleb Smirnoff 	SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
421a9771948SGleb Smirnoff 	SHA1Final(md, &sha1ctx);
422a9771948SGleb Smirnoff 
423a9771948SGleb Smirnoff 	/* outer hash */
424a9771948SGleb Smirnoff 	SHA1Init(&sha1ctx);
425a9771948SGleb Smirnoff 	SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
426a9771948SGleb Smirnoff 	SHA1Update(&sha1ctx, md, 20);
427a9771948SGleb Smirnoff 	SHA1Final(md, &sha1ctx);
428a9771948SGleb Smirnoff }
429a9771948SGleb Smirnoff 
4305c1f0f6dSGleb Smirnoff static int
43108b68b0eSGleb Smirnoff carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
432a9771948SGleb Smirnoff     unsigned char md[20])
433a9771948SGleb Smirnoff {
434a9771948SGleb Smirnoff 	unsigned char md2[20];
435a9771948SGleb Smirnoff 
43608b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
437d220759bSGleb Smirnoff 
438a9771948SGleb Smirnoff 	carp_hmac_generate(sc, counter, md2);
439a9771948SGleb Smirnoff 
440a9771948SGleb Smirnoff 	return (bcmp(md, md2, sizeof(md2)));
441a9771948SGleb Smirnoff }
442a9771948SGleb Smirnoff 
443a9771948SGleb Smirnoff /*
444a9771948SGleb Smirnoff  * process input packet.
445a9771948SGleb Smirnoff  * we have rearranged checks order compared to the rfc,
446a9771948SGleb Smirnoff  * but it seems more efficient this way or not possible otherwise.
447a9771948SGleb Smirnoff  */
448a0ae8f04SBjoern A. Zeeb #ifdef INET
44978b1fc05SGleb Smirnoff static int
4508f5a8818SKevin Lo carp_input(struct mbuf **mp, int *offp, int proto)
451a9771948SGleb Smirnoff {
4528f5a8818SKevin Lo 	struct mbuf *m = *mp;
453a9771948SGleb Smirnoff 	struct ip *ip = mtod(m, struct ip *);
454a9771948SGleb Smirnoff 	struct carp_header *ch;
455a9771948SGleb Smirnoff 	int iplen, len;
456a9771948SGleb Smirnoff 
4578f5a8818SKevin Lo 	iplen = *offp;
4588f5a8818SKevin Lo 	*mp = NULL;
4598f5a8818SKevin Lo 
4606bf65bcfSRobert Watson 	CARPSTATS_INC(carps_ipackets);
461a9771948SGleb Smirnoff 
462c5c392e7SMikolaj Golub 	if (!V_carp_allow) {
463a9771948SGleb Smirnoff 		m_freem(m);
4648f5a8818SKevin Lo 		return (IPPROTO_DONE);
465a9771948SGleb Smirnoff 	}
466a9771948SGleb Smirnoff 
467a9771948SGleb Smirnoff 	/* verify that the IP TTL is 255.  */
468a9771948SGleb Smirnoff 	if (ip->ip_ttl != CARP_DFLTTL) {
4696bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badttl);
47008b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
4711e9e6572SGleb Smirnoff 		    ip->ip_ttl,
4721e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
473a9771948SGleb Smirnoff 		m_freem(m);
4748f5a8818SKevin Lo 		return (IPPROTO_DONE);
475a9771948SGleb Smirnoff 	}
476a9771948SGleb Smirnoff 
477a9771948SGleb Smirnoff 	iplen = ip->ip_hl << 2;
478a9771948SGleb Smirnoff 
479a9771948SGleb Smirnoff 	if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
4806bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badlen);
48108b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: received len %zd < sizeof(struct carp_header) "
48208b68b0eSGleb Smirnoff 		    "on %s\n", __func__, m->m_len - sizeof(struct ip),
48316e324fcSXin LI 		    m->m_pkthdr.rcvif->if_xname);
484a9771948SGleb Smirnoff 		m_freem(m);
4858f5a8818SKevin Lo 		return (IPPROTO_DONE);
486a9771948SGleb Smirnoff 	}
487a9771948SGleb Smirnoff 
488a9771948SGleb Smirnoff 	if (iplen + sizeof(*ch) < m->m_len) {
489a9771948SGleb Smirnoff 		if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
4906bf65bcfSRobert Watson 			CARPSTATS_INC(carps_hdrops);
49108b68b0eSGleb Smirnoff 			CARP_DEBUG("%s: pullup failed\n", __func__);
4928f5a8818SKevin Lo 			return (IPPROTO_DONE);
493a9771948SGleb Smirnoff 		}
494a9771948SGleb Smirnoff 		ip = mtod(m, struct ip *);
495a9771948SGleb Smirnoff 	}
496a9771948SGleb Smirnoff 	ch = (struct carp_header *)((char *)ip + iplen);
497a9771948SGleb Smirnoff 
498a9771948SGleb Smirnoff 	/*
499a9771948SGleb Smirnoff 	 * verify that the received packet length is
500a9771948SGleb Smirnoff 	 * equal to the CARP header
501a9771948SGleb Smirnoff 	 */
502a9771948SGleb Smirnoff 	len = iplen + sizeof(*ch);
503a9771948SGleb Smirnoff 	if (len > m->m_pkthdr.len) {
5046bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badlen);
50508b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: packet too short %d on %s\n", __func__,
5061e9e6572SGleb Smirnoff 		    m->m_pkthdr.len,
5071e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
508a9771948SGleb Smirnoff 		m_freem(m);
5098f5a8818SKevin Lo 		return (IPPROTO_DONE);
510a9771948SGleb Smirnoff 	}
511a9771948SGleb Smirnoff 
512a9771948SGleb Smirnoff 	if ((m = m_pullup(m, len)) == NULL) {
5136bf65bcfSRobert Watson 		CARPSTATS_INC(carps_hdrops);
5148f5a8818SKevin Lo 		return (IPPROTO_DONE);
515a9771948SGleb Smirnoff 	}
516a9771948SGleb Smirnoff 	ip = mtod(m, struct ip *);
517a9771948SGleb Smirnoff 	ch = (struct carp_header *)((char *)ip + iplen);
518a9771948SGleb Smirnoff 
519a9771948SGleb Smirnoff 	/* verify the CARP checksum */
520a9771948SGleb Smirnoff 	m->m_data += iplen;
521c4d06976SGleb Smirnoff 	if (in_cksum(m, len - iplen)) {
5226bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badsum);
52308b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: checksum failed on %s\n", __func__,
5241e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
525a9771948SGleb Smirnoff 		m_freem(m);
5268f5a8818SKevin Lo 		return (IPPROTO_DONE);
527a9771948SGleb Smirnoff 	}
528a9771948SGleb Smirnoff 	m->m_data -= iplen;
529a9771948SGleb Smirnoff 
5301e9e6572SGleb Smirnoff 	carp_input_c(m, ch, AF_INET);
5318f5a8818SKevin Lo 	return (IPPROTO_DONE);
532a9771948SGleb Smirnoff }
533a0ae8f04SBjoern A. Zeeb #endif
534a9771948SGleb Smirnoff 
535a9771948SGleb Smirnoff #ifdef INET6
53678b1fc05SGleb Smirnoff static int
537a9771948SGleb Smirnoff carp6_input(struct mbuf **mp, int *offp, int proto)
538a9771948SGleb Smirnoff {
539a9771948SGleb Smirnoff 	struct mbuf *m = *mp;
540a9771948SGleb Smirnoff 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
541a9771948SGleb Smirnoff 	struct carp_header *ch;
542a9771948SGleb Smirnoff 	u_int len;
543a9771948SGleb Smirnoff 
5446bf65bcfSRobert Watson 	CARPSTATS_INC(carps_ipackets6);
545a9771948SGleb Smirnoff 
546c5c392e7SMikolaj Golub 	if (!V_carp_allow) {
547a9771948SGleb Smirnoff 		m_freem(m);
548a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
549a9771948SGleb Smirnoff 	}
550a9771948SGleb Smirnoff 
551a9771948SGleb Smirnoff 	/* check if received on a valid carp interface */
552a9771948SGleb Smirnoff 	if (m->m_pkthdr.rcvif->if_carp == NULL) {
5536bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badif);
55408b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: packet received on non-carp interface: %s\n",
55508b68b0eSGleb Smirnoff 		    __func__, m->m_pkthdr.rcvif->if_xname);
556a9771948SGleb Smirnoff 		m_freem(m);
557a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
558a9771948SGleb Smirnoff 	}
559a9771948SGleb Smirnoff 
560a9771948SGleb Smirnoff 	/* verify that the IP TTL is 255 */
561a9771948SGleb Smirnoff 	if (ip6->ip6_hlim != CARP_DFLTTL) {
5626bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badttl);
56308b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
56408b68b0eSGleb Smirnoff 		    ip6->ip6_hlim, m->m_pkthdr.rcvif->if_xname);
565a9771948SGleb Smirnoff 		m_freem(m);
566a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
567a9771948SGleb Smirnoff 	}
568a9771948SGleb Smirnoff 
569a9771948SGleb Smirnoff 	/* verify that we have a complete carp packet */
570a4adf6ccSBjoern A. Zeeb 	if (m->m_len < *offp + sizeof(*ch)) {
571a9771948SGleb Smirnoff 		len = m->m_len;
57263abacc2SBjoern A. Zeeb 		m = m_pullup(m, *offp + sizeof(*ch));
57363abacc2SBjoern A. Zeeb 		if (m == NULL) {
5746bf65bcfSRobert Watson 			CARPSTATS_INC(carps_badlen);
57508b68b0eSGleb Smirnoff 			CARP_DEBUG("%s: packet size %u too small\n", __func__, len);
576a9771948SGleb Smirnoff 			return (IPPROTO_DONE);
577a9771948SGleb Smirnoff 		}
578a4adf6ccSBjoern A. Zeeb 	}
579dad68fc3SBjoern A. Zeeb 	ch = (struct carp_header *)(mtod(m, char *) + *offp);
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;
8171d126e9bSKristof Provost 	struct epoch_tracker et;
818a9771948SGleb Smirnoff 
8191d126e9bSKristof Provost 	NET_EPOCH_ENTER(et);
820d92d54d5SGleb Smirnoff 	mtx_lock(&carp_mtx);
82108b68b0eSGleb Smirnoff 	LIST_FOREACH(sc, &carp_list, sc_next)
822f08535f8SGleb Smirnoff 		if (sc->sc_state == MASTER) {
82308b68b0eSGleb Smirnoff 			CARP_LOCK(sc);
824afdbac98SGleb Smirnoff 			CURVNET_SET(sc->sc_carpdev->if_vnet);
825d220759bSGleb Smirnoff 			carp_send_ad_locked(sc);
826afdbac98SGleb Smirnoff 			CURVNET_RESTORE();
82708b68b0eSGleb Smirnoff 			CARP_UNLOCK(sc);
828a9771948SGleb Smirnoff 		}
829d92d54d5SGleb Smirnoff 	mtx_unlock(&carp_mtx);
8301d126e9bSKristof Provost 	NET_EPOCH_EXIT(et);
831a9771948SGleb Smirnoff }
832a9771948SGleb Smirnoff 
833afdbac98SGleb Smirnoff /* Send a periodic advertisement, executed in callout context. */
8345c1f0f6dSGleb Smirnoff static void
835a9771948SGleb Smirnoff carp_send_ad(void *v)
836a9771948SGleb Smirnoff {
837d220759bSGleb Smirnoff 	struct carp_softc *sc = v;
8381d126e9bSKristof Provost 	struct epoch_tracker et;
839d220759bSGleb Smirnoff 
8401d126e9bSKristof Provost 	NET_EPOCH_ENTER(et);
84108b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
842afdbac98SGleb Smirnoff 	CURVNET_SET(sc->sc_carpdev->if_vnet);
843d220759bSGleb Smirnoff 	carp_send_ad_locked(sc);
844afdbac98SGleb Smirnoff 	CURVNET_RESTORE();
84508b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
8461d126e9bSKristof Provost 	NET_EPOCH_EXIT(et);
847d220759bSGleb Smirnoff }
848d220759bSGleb Smirnoff 
849d220759bSGleb Smirnoff static void
8504a2dd8d4SGleb Smirnoff carp_send_ad_error(struct carp_softc *sc, int error)
8514a2dd8d4SGleb Smirnoff {
8524a2dd8d4SGleb Smirnoff 
8539a8cf950SGleb Smirnoff 	/*
8549a8cf950SGleb Smirnoff 	 * We track errors and successfull sends with this logic:
8559a8cf950SGleb Smirnoff 	 * - Any error resets success counter to 0.
8569a8cf950SGleb Smirnoff 	 * - MAX_ERRORS triggers demotion.
8579a8cf950SGleb Smirnoff 	 * - MIN_SUCCESS successes resets error counter to 0.
8589a8cf950SGleb Smirnoff 	 * - MIN_SUCCESS reverts demotion, if it was triggered before.
8599a8cf950SGleb Smirnoff 	 */
8604a2dd8d4SGleb Smirnoff 	if (error) {
8614a2dd8d4SGleb Smirnoff 		if (sc->sc_sendad_errors < INT_MAX)
8624a2dd8d4SGleb Smirnoff 			sc->sc_sendad_errors++;
8634a2dd8d4SGleb Smirnoff 		if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
8644a2dd8d4SGleb Smirnoff 			static const char fmt[] = "send error %d on %s";
8654a2dd8d4SGleb Smirnoff 			char msg[sizeof(fmt) + IFNAMSIZ];
8664a2dd8d4SGleb Smirnoff 
8674a2dd8d4SGleb Smirnoff 			sprintf(msg, fmt, error, sc->sc_carpdev->if_xname);
8684a2dd8d4SGleb Smirnoff 			carp_demote_adj(V_carp_senderr_adj, msg);
8694a2dd8d4SGleb Smirnoff 		}
8704a2dd8d4SGleb Smirnoff 		sc->sc_sendad_success = 0;
8719a8cf950SGleb Smirnoff 	} else if (sc->sc_sendad_errors > 0) {
8729a8cf950SGleb Smirnoff 		if (++sc->sc_sendad_success >= CARP_SENDAD_MIN_SUCCESS) {
8739a8cf950SGleb Smirnoff 			if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
8744a2dd8d4SGleb Smirnoff 				static const char fmt[] = "send ok on %s";
8754a2dd8d4SGleb Smirnoff 				char msg[sizeof(fmt) + IFNAMSIZ];
8764a2dd8d4SGleb Smirnoff 
8774a2dd8d4SGleb Smirnoff 				sprintf(msg, fmt, sc->sc_carpdev->if_xname);
8784a2dd8d4SGleb Smirnoff 				carp_demote_adj(-V_carp_senderr_adj, msg);
8799a8cf950SGleb Smirnoff 			}
8804a2dd8d4SGleb Smirnoff 			sc->sc_sendad_errors = 0;
8819a8cf950SGleb Smirnoff 		}
8824a2dd8d4SGleb Smirnoff 	}
8834a2dd8d4SGleb Smirnoff }
8844a2dd8d4SGleb Smirnoff 
8858151740cSJosh Paetzel /*
8868151740cSJosh Paetzel  * Pick the best ifaddr on the given ifp for sending CARP
8878151740cSJosh Paetzel  * advertisements.
8888151740cSJosh Paetzel  *
8898151740cSJosh Paetzel  * "Best" here is defined by ifa_preferred().  This function is much
8908151740cSJosh Paetzel  * much like ifaof_ifpforaddr() except that we just use ifa_preferred().
8918151740cSJosh Paetzel  *
8928151740cSJosh Paetzel  * (This could be simplified to return the actual address, except that
8938151740cSJosh Paetzel  * it has a different format in AF_INET and AF_INET6.)
8948151740cSJosh Paetzel  */
8958151740cSJosh Paetzel static struct ifaddr *
8968151740cSJosh Paetzel carp_best_ifa(int af, struct ifnet *ifp)
8978151740cSJosh Paetzel {
8988151740cSJosh Paetzel 	struct ifaddr *ifa, *best;
8998151740cSJosh Paetzel 
900b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
901b8a6e03fSGleb Smirnoff 
9028151740cSJosh Paetzel 	if (af >= AF_MAX)
9038151740cSJosh Paetzel 		return (NULL);
9048151740cSJosh Paetzel 	best = NULL;
905d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
9068151740cSJosh Paetzel 		if (ifa->ifa_addr->sa_family == af &&
9078151740cSJosh Paetzel 		    (best == NULL || ifa_preferred(best, ifa)))
9088151740cSJosh Paetzel 			best = ifa;
9098151740cSJosh Paetzel 	}
9108151740cSJosh Paetzel 	if (best != NULL)
9118151740cSJosh Paetzel 		ifa_ref(best);
9128151740cSJosh Paetzel 	return (best);
9138151740cSJosh Paetzel }
9148151740cSJosh Paetzel 
9154a2dd8d4SGleb Smirnoff static void
916d220759bSGleb Smirnoff carp_send_ad_locked(struct carp_softc *sc)
917d220759bSGleb Smirnoff {
918a9771948SGleb Smirnoff 	struct carp_header ch;
919a9771948SGleb Smirnoff 	struct timeval tv;
92008b68b0eSGleb Smirnoff 	struct ifaddr *ifa;
921a9771948SGleb Smirnoff 	struct carp_header *ch_ptr;
922a9771948SGleb Smirnoff 	struct mbuf *m;
92308b68b0eSGleb Smirnoff 	int len, advskew;
924a9771948SGleb Smirnoff 
9251d126e9bSKristof Provost 	NET_EPOCH_ASSERT();
92608b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
927d220759bSGleb Smirnoff 
928f08535f8SGleb Smirnoff 	advskew = DEMOTE_ADVSKEW(sc);
92908b68b0eSGleb Smirnoff 	tv.tv_sec = sc->sc_advbase;
930a9771948SGleb Smirnoff 	tv.tv_usec = advskew * 1000000 / 256;
931a9771948SGleb Smirnoff 
932a9771948SGleb Smirnoff 	ch.carp_version = CARP_VERSION;
933a9771948SGleb Smirnoff 	ch.carp_type = CARP_ADVERTISEMENT;
934a9771948SGleb Smirnoff 	ch.carp_vhid = sc->sc_vhid;
93508b68b0eSGleb Smirnoff 	ch.carp_advbase = sc->sc_advbase;
936a9771948SGleb Smirnoff 	ch.carp_advskew = advskew;
937a9771948SGleb Smirnoff 	ch.carp_authlen = 7;	/* XXX DEFINE */
938a9771948SGleb Smirnoff 	ch.carp_pad1 = 0;	/* must be zero */
939a9771948SGleb Smirnoff 	ch.carp_cksum = 0;
940a9771948SGleb Smirnoff 
94108b68b0eSGleb Smirnoff 	/* XXXGL: OpenBSD picks first ifaddr with needed family. */
94208b68b0eSGleb Smirnoff 
943a9771948SGleb Smirnoff #ifdef INET
94408b68b0eSGleb Smirnoff 	if (sc->sc_naddrs) {
945a9771948SGleb Smirnoff 		struct ip *ip;
946a9771948SGleb Smirnoff 
947dc4ad05eSGleb Smirnoff 		m = m_gethdr(M_NOWAIT, MT_DATA);
948a9771948SGleb Smirnoff 		if (m == NULL) {
9496bf65bcfSRobert Watson 			CARPSTATS_INC(carps_onomem);
950891122d1SGleb Smirnoff 			goto resched;
951a9771948SGleb Smirnoff 		}
952a9771948SGleb Smirnoff 		len = sizeof(*ip) + sizeof(ch);
953a9771948SGleb Smirnoff 		m->m_pkthdr.len = len;
954a9771948SGleb Smirnoff 		m->m_pkthdr.rcvif = NULL;
955a9771948SGleb Smirnoff 		m->m_len = len;
956ed6a66caSRobert Watson 		M_ALIGN(m, m->m_len);
957a9771948SGleb Smirnoff 		m->m_flags |= M_MCAST;
958a9771948SGleb Smirnoff 		ip = mtod(m, struct ip *);
959a9771948SGleb Smirnoff 		ip->ip_v = IPVERSION;
960a9771948SGleb Smirnoff 		ip->ip_hl = sizeof(*ip) >> 2;
9610d3d234cSKristof Provost 		ip->ip_tos = V_carp_dscp << IPTOS_DSCP_OFFSET;
9628f134647SGleb Smirnoff 		ip->ip_len = htons(len);
9638f134647SGleb Smirnoff 		ip->ip_off = htons(IP_DF);
964a9771948SGleb Smirnoff 		ip->ip_ttl = CARP_DFLTTL;
965a9771948SGleb Smirnoff 		ip->ip_p = IPPROTO_CARP;
966a9771948SGleb Smirnoff 		ip->ip_sum = 0;
9676d947416SGleb Smirnoff 		ip_fillid(ip);
96808b68b0eSGleb Smirnoff 
9698151740cSJosh Paetzel 		ifa = carp_best_ifa(AF_INET, sc->sc_carpdev);
97008b68b0eSGleb Smirnoff 		if (ifa != NULL) {
97108b68b0eSGleb Smirnoff 			ip->ip_src.s_addr =
97208b68b0eSGleb Smirnoff 			    ifatoia(ifa)->ia_addr.sin_addr.s_addr;
97308b68b0eSGleb Smirnoff 			ifa_free(ifa);
97408b68b0eSGleb Smirnoff 		} else
97508b68b0eSGleb Smirnoff 			ip->ip_src.s_addr = 0;
976a9771948SGleb Smirnoff 		ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
977a9771948SGleb Smirnoff 
978a9771948SGleb Smirnoff 		ch_ptr = (struct carp_header *)(&ip[1]);
979a9771948SGleb Smirnoff 		bcopy(&ch, ch_ptr, sizeof(ch));
980a9771948SGleb Smirnoff 		if (carp_prepare_ad(m, sc, ch_ptr))
981891122d1SGleb Smirnoff 			goto resched;
982a9771948SGleb Smirnoff 
983a9771948SGleb Smirnoff 		m->m_data += sizeof(*ip);
984c4d06976SGleb Smirnoff 		ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip));
985a9771948SGleb Smirnoff 		m->m_data -= sizeof(*ip);
986a9771948SGleb Smirnoff 
9876bf65bcfSRobert Watson 		CARPSTATS_INC(carps_opackets);
988a9771948SGleb Smirnoff 
9894a2dd8d4SGleb Smirnoff 		carp_send_ad_error(sc, ip_output(m, NULL, NULL, IP_RAWOUTPUT,
9904a2dd8d4SGleb Smirnoff 		    &sc->sc_carpdev->if_carp->cif_imo, NULL));
991a9771948SGleb Smirnoff 	}
992a9771948SGleb Smirnoff #endif /* INET */
993a9771948SGleb Smirnoff #ifdef INET6
99408b68b0eSGleb Smirnoff 	if (sc->sc_naddrs6) {
995a9771948SGleb Smirnoff 		struct ip6_hdr *ip6;
996a9771948SGleb Smirnoff 
997dc4ad05eSGleb Smirnoff 		m = m_gethdr(M_NOWAIT, MT_DATA);
998a9771948SGleb Smirnoff 		if (m == NULL) {
9996bf65bcfSRobert Watson 			CARPSTATS_INC(carps_onomem);
1000891122d1SGleb Smirnoff 			goto resched;
1001a9771948SGleb Smirnoff 		}
1002a9771948SGleb Smirnoff 		len = sizeof(*ip6) + sizeof(ch);
1003a9771948SGleb Smirnoff 		m->m_pkthdr.len = len;
1004a9771948SGleb Smirnoff 		m->m_pkthdr.rcvif = NULL;
1005a9771948SGleb Smirnoff 		m->m_len = len;
1006ed6a66caSRobert Watson 		M_ALIGN(m, m->m_len);
1007a9771948SGleb Smirnoff 		m->m_flags |= M_MCAST;
1008a9771948SGleb Smirnoff 		ip6 = mtod(m, struct ip6_hdr *);
1009a9771948SGleb Smirnoff 		bzero(ip6, sizeof(*ip6));
1010a9771948SGleb Smirnoff 		ip6->ip6_vfc |= IPV6_VERSION;
10110d3d234cSKristof Provost 		/* Traffic class isn't defined in ip6 struct instead
10120d3d234cSKristof Provost 		 * it gets offset into flowid field */
10130d3d234cSKristof Provost 		ip6->ip6_flow |= htonl(V_carp_dscp << (IPV6_FLOWLABEL_LEN +
10140d3d234cSKristof Provost 		    IPTOS_DSCP_OFFSET));
1015a9771948SGleb Smirnoff 		ip6->ip6_hlim = CARP_DFLTTL;
1016a9771948SGleb Smirnoff 		ip6->ip6_nxt = IPPROTO_CARP;
1017a9771948SGleb Smirnoff 
101808b68b0eSGleb Smirnoff 		/* set the source address */
10198151740cSJosh Paetzel 		ifa = carp_best_ifa(AF_INET6, sc->sc_carpdev);
102008b68b0eSGleb Smirnoff 		if (ifa != NULL) {
102108b68b0eSGleb Smirnoff 			bcopy(IFA_IN6(ifa), &ip6->ip6_src,
102208b68b0eSGleb Smirnoff 			    sizeof(struct in6_addr));
102308b68b0eSGleb Smirnoff 			ifa_free(ifa);
102408b68b0eSGleb Smirnoff 		} else
102508b68b0eSGleb Smirnoff 			/* This should never happen with IPv6. */
102608b68b0eSGleb Smirnoff 			bzero(&ip6->ip6_src, sizeof(struct in6_addr));
102708b68b0eSGleb Smirnoff 
102808b68b0eSGleb Smirnoff 		/* Set the multicast destination. */
10297002145dSBjoern A. Zeeb 		ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
1030a9771948SGleb Smirnoff 		ip6->ip6_dst.s6_addr8[15] = 0x12;
10317002145dSBjoern A. Zeeb 		if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
10327002145dSBjoern A. Zeeb 			m_freem(m);
1033e81ab876SGleb Smirnoff 			CARP_DEBUG("%s: in6_setscope failed\n", __func__);
1034891122d1SGleb Smirnoff 			goto resched;
10357002145dSBjoern A. Zeeb 		}
1036a9771948SGleb Smirnoff 
1037a9771948SGleb Smirnoff 		ch_ptr = (struct carp_header *)(&ip6[1]);
1038a9771948SGleb Smirnoff 		bcopy(&ch, ch_ptr, sizeof(ch));
1039a9771948SGleb Smirnoff 		if (carp_prepare_ad(m, sc, ch_ptr))
1040891122d1SGleb Smirnoff 			goto resched;
1041a9771948SGleb Smirnoff 
1042a9771948SGleb Smirnoff 		m->m_data += sizeof(*ip6);
1043c4d06976SGleb Smirnoff 		ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip6));
1044a9771948SGleb Smirnoff 		m->m_data -= sizeof(*ip6);
1045a9771948SGleb Smirnoff 
10466bf65bcfSRobert Watson 		CARPSTATS_INC(carps_opackets6);
1047a9771948SGleb Smirnoff 
10484a2dd8d4SGleb Smirnoff 		carp_send_ad_error(sc, ip6_output(m, NULL, NULL, 0,
10494a2dd8d4SGleb Smirnoff 		    &sc->sc_carpdev->if_carp->cif_im6o, NULL, NULL));
1050a9771948SGleb Smirnoff 	}
1051a9771948SGleb Smirnoff #endif /* INET6 */
1052a9771948SGleb Smirnoff 
1053891122d1SGleb Smirnoff resched:
105408b68b0eSGleb Smirnoff 	callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), carp_send_ad, sc);
105508b68b0eSGleb Smirnoff }
1056a9771948SGleb Smirnoff 
105708b68b0eSGleb Smirnoff static void
105808b68b0eSGleb Smirnoff carp_addroute(struct carp_softc *sc)
105908b68b0eSGleb Smirnoff {
106008b68b0eSGleb Smirnoff 	struct ifaddr *ifa;
106108b68b0eSGleb Smirnoff 
106208b68b0eSGleb Smirnoff 	CARP_FOREACH_IFA(sc, ifa)
10632512b096SGleb Smirnoff 		carp_ifa_addroute(ifa);
10642512b096SGleb Smirnoff }
10652512b096SGleb Smirnoff 
10662512b096SGleb Smirnoff static void
10672512b096SGleb Smirnoff carp_ifa_addroute(struct ifaddr *ifa)
10682512b096SGleb Smirnoff {
10692512b096SGleb Smirnoff 
107008b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
107108b68b0eSGleb Smirnoff #ifdef INET
107208b68b0eSGleb Smirnoff 	case AF_INET:
1073130aebbaSAlexander V. Chernikov 		in_addprefix(ifatoia(ifa));
107408b68b0eSGleb Smirnoff 		ifa_add_loopback_route(ifa,
107508b68b0eSGleb Smirnoff 		    (struct sockaddr *)&ifatoia(ifa)->ia_addr);
107608b68b0eSGleb Smirnoff 		break;
107708b68b0eSGleb Smirnoff #endif
107808b68b0eSGleb Smirnoff #ifdef INET6
107908b68b0eSGleb Smirnoff 	case AF_INET6:
108008b68b0eSGleb Smirnoff 		ifa_add_loopback_route(ifa,
108108b68b0eSGleb Smirnoff 		    (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
1082f6b84910SAlexander V. Chernikov 		nd6_add_ifa_lle(ifatoia6(ifa));
108308b68b0eSGleb Smirnoff 		break;
108408b68b0eSGleb Smirnoff #endif
108508b68b0eSGleb Smirnoff 	}
108608b68b0eSGleb Smirnoff }
108708b68b0eSGleb Smirnoff 
108808b68b0eSGleb Smirnoff static void
108908b68b0eSGleb Smirnoff carp_delroute(struct carp_softc *sc)
109008b68b0eSGleb Smirnoff {
109108b68b0eSGleb Smirnoff 	struct ifaddr *ifa;
109208b68b0eSGleb Smirnoff 
109308b68b0eSGleb Smirnoff 	CARP_FOREACH_IFA(sc, ifa)
10942512b096SGleb Smirnoff 		carp_ifa_delroute(ifa);
10952512b096SGleb Smirnoff }
10962512b096SGleb Smirnoff 
10972512b096SGleb Smirnoff static void
10982512b096SGleb Smirnoff carp_ifa_delroute(struct ifaddr *ifa)
10992512b096SGleb Smirnoff {
11002512b096SGleb Smirnoff 
110108b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
110208b68b0eSGleb Smirnoff #ifdef INET
110308b68b0eSGleb Smirnoff 	case AF_INET:
110408b68b0eSGleb Smirnoff 		ifa_del_loopback_route(ifa,
110508b68b0eSGleb Smirnoff 		    (struct sockaddr *)&ifatoia(ifa)->ia_addr);
110608b68b0eSGleb Smirnoff 		in_scrubprefix(ifatoia(ifa), LLE_STATIC);
110708b68b0eSGleb Smirnoff 		break;
110808b68b0eSGleb Smirnoff #endif
110908b68b0eSGleb Smirnoff #ifdef INET6
111008b68b0eSGleb Smirnoff 	case AF_INET6:
111108b68b0eSGleb Smirnoff 		ifa_del_loopback_route(ifa,
111208b68b0eSGleb Smirnoff 		    (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
11133e7a2321SAlexander V. Chernikov 		nd6_rem_ifa_lle(ifatoia6(ifa), 1);
111408b68b0eSGleb Smirnoff 		break;
111508b68b0eSGleb Smirnoff #endif
111608b68b0eSGleb Smirnoff 	}
1117a9771948SGleb Smirnoff }
1118a9771948SGleb Smirnoff 
111924421c1cSGleb Smirnoff int
112024421c1cSGleb Smirnoff carp_master(struct ifaddr *ifa)
112124421c1cSGleb Smirnoff {
112224421c1cSGleb Smirnoff 	struct carp_softc *sc = ifa->ifa_carp;
112324421c1cSGleb Smirnoff 
112424421c1cSGleb Smirnoff 	return (sc->sc_state == MASTER);
112524421c1cSGleb Smirnoff }
112624421c1cSGleb Smirnoff 
1127a0ae8f04SBjoern A. Zeeb #ifdef INET
1128a9771948SGleb Smirnoff /*
1129a9771948SGleb Smirnoff  * Broadcast a gratuitous ARP request containing
1130a9771948SGleb Smirnoff  * the virtual router MAC address for each IP address
1131a9771948SGleb Smirnoff  * associated with the virtual router.
1132a9771948SGleb Smirnoff  */
11335c1f0f6dSGleb Smirnoff static void
1134a9771948SGleb Smirnoff carp_send_arp(struct carp_softc *sc)
1135a9771948SGleb Smirnoff {
1136a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1137d6e82913SSteven Hartland 	struct in_addr addr;
1138a9771948SGleb Smirnoff 
11391d126e9bSKristof Provost 	NET_EPOCH_ASSERT();
11401d126e9bSKristof Provost 
11411c302b58SAlexander V. Chernikov 	CARP_FOREACH_IFA(sc, ifa) {
11421c302b58SAlexander V. Chernikov 		if (ifa->ifa_addr->sa_family != AF_INET)
11431c302b58SAlexander V. Chernikov 			continue;
1144d6e82913SSteven Hartland 		addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
1145d6e82913SSteven Hartland 		arp_announce_ifaddr(sc->sc_carpdev, addr, LLADDR(&sc->sc_addr));
11461c302b58SAlexander V. Chernikov 	}
1147a9771948SGleb Smirnoff }
114808b68b0eSGleb Smirnoff 
114908b68b0eSGleb Smirnoff int
115008b68b0eSGleb Smirnoff carp_iamatch(struct ifaddr *ifa, uint8_t **enaddr)
115108b68b0eSGleb Smirnoff {
115208b68b0eSGleb Smirnoff 	struct carp_softc *sc = ifa->ifa_carp;
115308b68b0eSGleb Smirnoff 
115408b68b0eSGleb Smirnoff 	if (sc->sc_state == MASTER) {
115508b68b0eSGleb Smirnoff 		*enaddr = LLADDR(&sc->sc_addr);
115608b68b0eSGleb Smirnoff 		return (1);
115708b68b0eSGleb Smirnoff 	}
115808b68b0eSGleb Smirnoff 
115908b68b0eSGleb Smirnoff 	return (0);
1160a9771948SGleb Smirnoff }
1161a0ae8f04SBjoern A. Zeeb #endif
1162a9771948SGleb Smirnoff 
1163a9771948SGleb Smirnoff #ifdef INET6
11645c1f0f6dSGleb Smirnoff static void
1165a9771948SGleb Smirnoff carp_send_na(struct carp_softc *sc)
1166a9771948SGleb Smirnoff {
1167d6e82913SSteven Hartland 	static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1168a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1169d6e82913SSteven Hartland 	struct in6_addr *in6;
1170a9771948SGleb Smirnoff 
117108b68b0eSGleb Smirnoff 	CARP_FOREACH_IFA(sc, ifa) {
1172d6e82913SSteven Hartland 		if (ifa->ifa_addr->sa_family != AF_INET6)
1173a9771948SGleb Smirnoff 			continue;
1174a9771948SGleb Smirnoff 
1175d6e82913SSteven Hartland 		in6 = IFA_IN6(ifa);
1176d6e82913SSteven Hartland 		nd6_na_output(sc->sc_carpdev, &mcast, in6,
1177d6e82913SSteven Hartland 		    ND_NA_FLAG_OVERRIDE, 1, NULL);
1178d6e82913SSteven Hartland 		DELAY(1000);	/* XXX */
1179a9771948SGleb Smirnoff 	}
1180a9771948SGleb Smirnoff }
1181a9771948SGleb Smirnoff 
11828253dcabSBjoern A. Zeeb /*
11838253dcabSBjoern A. Zeeb  * Returns ifa in case it's a carp address and it is MASTER, or if the address
11848253dcabSBjoern A. Zeeb  * matches and is not a carp address.  Returns NULL otherwise.
11858253dcabSBjoern A. Zeeb  */
1186a4e53905SMax Laier struct ifaddr *
118754bfbd51SWill Andrews carp_iamatch6(struct ifnet *ifp, struct in6_addr *taddr)
1188a9771948SGleb Smirnoff {
1189a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1190a9771948SGleb Smirnoff 
1191b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
1192b8a6e03fSGleb Smirnoff 
11938253dcabSBjoern A. Zeeb 	ifa = NULL;
1194d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
11958253dcabSBjoern A. Zeeb 		if (ifa->ifa_addr->sa_family != AF_INET6)
11968253dcabSBjoern A. Zeeb 			continue;
11978253dcabSBjoern A. Zeeb 		if (!IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa)))
11988253dcabSBjoern A. Zeeb 			continue;
11998253dcabSBjoern A. Zeeb 		if (ifa->ifa_carp && ifa->ifa_carp->sc_state != MASTER)
12008253dcabSBjoern A. Zeeb 			ifa = NULL;
12018253dcabSBjoern A. Zeeb 		else
12028c0fec80SRobert Watson 			ifa_ref(ifa);
12038253dcabSBjoern A. Zeeb 		break;
1204a9771948SGleb Smirnoff 	}
1205a9771948SGleb Smirnoff 
12068253dcabSBjoern A. Zeeb 	return (ifa);
1207a9771948SGleb Smirnoff }
1208a9771948SGleb Smirnoff 
1209dad68fc3SBjoern A. Zeeb char *
121054bfbd51SWill Andrews carp_macmatch6(struct ifnet *ifp, struct mbuf *m, const struct in6_addr *taddr)
1211a9771948SGleb Smirnoff {
1212a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1213a9771948SGleb Smirnoff 
1214b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
1215b8a6e03fSGleb Smirnoff 
121608b68b0eSGleb Smirnoff 	IFNET_FOREACH_IFA(ifp, ifa)
121708b68b0eSGleb Smirnoff 		if (ifa->ifa_addr->sa_family == AF_INET6 &&
121808b68b0eSGleb Smirnoff 		    IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) {
121908b68b0eSGleb Smirnoff 			struct carp_softc *sc = ifa->ifa_carp;
122008b68b0eSGleb Smirnoff 			struct m_tag *mtag;
122108b68b0eSGleb Smirnoff 
1222a9771948SGleb Smirnoff 			mtag = m_tag_get(PACKET_TAG_CARP,
1223a856ddc6SGleb Smirnoff 			    sizeof(struct carp_softc *), M_NOWAIT);
122408b68b0eSGleb Smirnoff 			if (mtag == NULL)
122508b68b0eSGleb Smirnoff 				/* Better a bit than nothing. */
122608b68b0eSGleb Smirnoff 				return (LLADDR(&sc->sc_addr));
122708b68b0eSGleb Smirnoff 
1228eaf151c4SGleb Smirnoff 			bcopy(&sc, mtag + 1, sizeof(sc));
1229a9771948SGleb Smirnoff 			m_tag_prepend(m, mtag);
1230a9771948SGleb Smirnoff 
123108b68b0eSGleb Smirnoff 			return (LLADDR(&sc->sc_addr));
1232a9771948SGleb Smirnoff 		}
1233a9771948SGleb Smirnoff 
1234a9771948SGleb Smirnoff 	return (NULL);
1235a9771948SGleb Smirnoff }
123608b68b0eSGleb Smirnoff #endif /* INET6 */
1237a9771948SGleb Smirnoff 
123808b68b0eSGleb Smirnoff int
123954bfbd51SWill Andrews carp_forus(struct ifnet *ifp, u_char *dhost)
1240a9771948SGleb Smirnoff {
124108b68b0eSGleb Smirnoff 	struct carp_softc *sc;
124208b68b0eSGleb Smirnoff 	uint8_t *ena = dhost;
1243a9771948SGleb Smirnoff 
1244a9771948SGleb Smirnoff 	if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
124508b68b0eSGleb Smirnoff 		return (0);
1246a9771948SGleb Smirnoff 
124708b68b0eSGleb Smirnoff 	CIF_LOCK(ifp->if_carp);
124808b68b0eSGleb Smirnoff 	IFNET_FOREACH_CARP(ifp, sc) {
12498c3fbf3cSAlexander Motin 		/*
12508c3fbf3cSAlexander Motin 		 * CARP_LOCK() is not here, since would protect nothing, but
12518c3fbf3cSAlexander Motin 		 * cause deadlock with if_bridge, calling this under its lock.
12528c3fbf3cSAlexander Motin 		 */
125308b68b0eSGleb Smirnoff 		if (sc->sc_state == MASTER && !bcmp(dhost, LLADDR(&sc->sc_addr),
125408b68b0eSGleb Smirnoff 		    ETHER_ADDR_LEN)) {
125508b68b0eSGleb Smirnoff 			CIF_UNLOCK(ifp->if_carp);
125608b68b0eSGleb Smirnoff 			return (1);
1257a9771948SGleb Smirnoff 		}
125808b68b0eSGleb Smirnoff 	}
125908b68b0eSGleb Smirnoff 	CIF_UNLOCK(ifp->if_carp);
1260a9771948SGleb Smirnoff 
126108b68b0eSGleb Smirnoff 	return (0);
1262a9771948SGleb Smirnoff }
1263a9771948SGleb Smirnoff 
1264afdbac98SGleb Smirnoff /* Master down timeout event, executed in callout context. */
12655c1f0f6dSGleb Smirnoff static void
1266a9771948SGleb Smirnoff carp_master_down(void *v)
1267a9771948SGleb Smirnoff {
1268a9771948SGleb Smirnoff 	struct carp_softc *sc = v;
12691d126e9bSKristof Provost 	struct epoch_tracker et;
1270a9771948SGleb Smirnoff 
12711d126e9bSKristof Provost 	NET_EPOCH_ENTER(et);
127208b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
127308b68b0eSGleb Smirnoff 
1274afdbac98SGleb Smirnoff 	CURVNET_SET(sc->sc_carpdev->if_vnet);
127508b68b0eSGleb Smirnoff 	if (sc->sc_state == BACKUP) {
1276d01641e2SWill Andrews 		carp_master_down_locked(sc, "master timed out");
127708b68b0eSGleb Smirnoff 	}
1278afdbac98SGleb Smirnoff 	CURVNET_RESTORE();
127908b68b0eSGleb Smirnoff 
128008b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
12811d126e9bSKristof Provost 	NET_EPOCH_EXIT(et);
1282d220759bSGleb Smirnoff }
1283d220759bSGleb Smirnoff 
1284d220759bSGleb Smirnoff static void
1285d01641e2SWill Andrews carp_master_down_locked(struct carp_softc *sc, const char *reason)
1286d220759bSGleb Smirnoff {
128708b68b0eSGleb Smirnoff 
12881d126e9bSKristof Provost 	NET_EPOCH_ASSERT();
128908b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
1290d220759bSGleb Smirnoff 
1291a9771948SGleb Smirnoff 	switch (sc->sc_state) {
1292a9771948SGleb Smirnoff 	case BACKUP:
1293d01641e2SWill Andrews 		carp_set_state(sc, MASTER, reason);
1294d220759bSGleb Smirnoff 		carp_send_ad_locked(sc);
1295a0ae8f04SBjoern A. Zeeb #ifdef INET
1296a9771948SGleb Smirnoff 		carp_send_arp(sc);
1297a0ae8f04SBjoern A. Zeeb #endif
1298a9771948SGleb Smirnoff #ifdef INET6
1299a9771948SGleb Smirnoff 		carp_send_na(sc);
130008b68b0eSGleb Smirnoff #endif
1301a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
130208b68b0eSGleb Smirnoff 		carp_addroute(sc);
130308b68b0eSGleb Smirnoff 		break;
130408b68b0eSGleb Smirnoff 	case INIT:
130508b68b0eSGleb Smirnoff 	case MASTER:
130608b68b0eSGleb Smirnoff #ifdef INVARIANTS
130708b68b0eSGleb Smirnoff 		panic("carp: VHID %u@%s: master_down event in %s state\n",
130808b68b0eSGleb Smirnoff 		    sc->sc_vhid,
130908b68b0eSGleb Smirnoff 		    sc->sc_carpdev->if_xname,
131008b68b0eSGleb Smirnoff 		    sc->sc_state ? "MASTER" : "INIT");
131108b68b0eSGleb Smirnoff #endif
1312a9771948SGleb Smirnoff 		break;
1313a9771948SGleb Smirnoff 	}
1314a9771948SGleb Smirnoff }
1315a9771948SGleb Smirnoff 
1316a9771948SGleb Smirnoff /*
1317a9771948SGleb Smirnoff  * When in backup state, af indicates whether to reset the master down timer
1318a9771948SGleb Smirnoff  * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1319a9771948SGleb Smirnoff  */
13205c1f0f6dSGleb Smirnoff static void
1321a9771948SGleb Smirnoff carp_setrun(struct carp_softc *sc, sa_family_t af)
1322a9771948SGleb Smirnoff {
1323a9771948SGleb Smirnoff 	struct timeval tv;
1324a9771948SGleb Smirnoff 
132508b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
1326d220759bSGleb Smirnoff 
132708b68b0eSGleb Smirnoff 	if ((sc->sc_carpdev->if_flags & IFF_UP) == 0 ||
132808b68b0eSGleb Smirnoff 	    sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1329167a3440SAlexander Motin 	    (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) ||
1330167a3440SAlexander Motin 	    !V_carp_allow)
1331a9771948SGleb Smirnoff 		return;
1332a9771948SGleb Smirnoff 
1333a9771948SGleb Smirnoff 	switch (sc->sc_state) {
1334a9771948SGleb Smirnoff 	case INIT:
1335d01641e2SWill Andrews 		carp_set_state(sc, BACKUP, "initialization complete");
1336a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
1337a9771948SGleb Smirnoff 		break;
1338a9771948SGleb Smirnoff 	case BACKUP:
1339a9771948SGleb Smirnoff 		callout_stop(&sc->sc_ad_tmo);
1340a9771948SGleb Smirnoff 		tv.tv_sec = 3 * sc->sc_advbase;
1341a9771948SGleb Smirnoff 		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1342a9771948SGleb Smirnoff 		switch (af) {
1343a9771948SGleb Smirnoff #ifdef INET
1344a9771948SGleb Smirnoff 		case AF_INET:
1345a9771948SGleb Smirnoff 			callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1346a9771948SGleb Smirnoff 			    carp_master_down, sc);
1347a9771948SGleb Smirnoff 			break;
134808b68b0eSGleb Smirnoff #endif
1349a9771948SGleb Smirnoff #ifdef INET6
1350a9771948SGleb Smirnoff 		case AF_INET6:
1351a9771948SGleb Smirnoff 			callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1352a9771948SGleb Smirnoff 			    carp_master_down, sc);
1353a9771948SGleb Smirnoff 			break;
135408b68b0eSGleb Smirnoff #endif
1355a9771948SGleb Smirnoff 		default:
135608b68b0eSGleb Smirnoff #ifdef INET
1357a9771948SGleb Smirnoff 			if (sc->sc_naddrs)
1358a9771948SGleb Smirnoff 				callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1359a9771948SGleb Smirnoff 				    carp_master_down, sc);
136008b68b0eSGleb Smirnoff #endif
136108b68b0eSGleb Smirnoff #ifdef INET6
1362a9771948SGleb Smirnoff 			if (sc->sc_naddrs6)
1363a9771948SGleb Smirnoff 				callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1364a9771948SGleb Smirnoff 				    carp_master_down, sc);
136508b68b0eSGleb Smirnoff #endif
1366a9771948SGleb Smirnoff 			break;
1367a9771948SGleb Smirnoff 		}
1368a9771948SGleb Smirnoff 		break;
1369a9771948SGleb Smirnoff 	case MASTER:
1370a9771948SGleb Smirnoff 		tv.tv_sec = sc->sc_advbase;
1371a9771948SGleb Smirnoff 		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1372a9771948SGleb Smirnoff 		callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1373a9771948SGleb Smirnoff 		    carp_send_ad, sc);
1374a9771948SGleb Smirnoff 		break;
1375a9771948SGleb Smirnoff 	}
1376a9771948SGleb Smirnoff }
1377a9771948SGleb Smirnoff 
137808b68b0eSGleb Smirnoff /*
137908b68b0eSGleb Smirnoff  * Setup multicast structures.
138008b68b0eSGleb Smirnoff  */
13815c1f0f6dSGleb Smirnoff static int
1382a9a2c40cSGleb Smirnoff carp_multicast_setup(struct carp_if *cif, sa_family_t sa)
1383a9771948SGleb Smirnoff {
1384a9a2c40cSGleb Smirnoff 	struct ifnet *ifp = cif->cif_ifp;
138508b68b0eSGleb Smirnoff 	int error = 0;
138608b68b0eSGleb Smirnoff 
138708b68b0eSGleb Smirnoff 	switch (sa) {
138808b68b0eSGleb Smirnoff #ifdef INET
138908b68b0eSGleb Smirnoff 	case AF_INET:
139008b68b0eSGleb Smirnoff 	    {
139108b68b0eSGleb Smirnoff 		struct ip_moptions *imo = &cif->cif_imo;
139259854ecfSHans Petter Selasky 		struct in_mfilter *imf;
1393a9771948SGleb Smirnoff 		struct in_addr addr;
1394a9771948SGleb Smirnoff 
139559854ecfSHans Petter Selasky 		if (ip_mfilter_first(&imo->imo_head) != NULL)
1396a9771948SGleb Smirnoff 			return (0);
1397a9771948SGleb Smirnoff 
139859854ecfSHans Petter Selasky 		imf = ip_mfilter_alloc(M_WAITOK, 0, 0);
139959854ecfSHans Petter Selasky 		ip_mfilter_init(&imo->imo_head);
140008b68b0eSGleb Smirnoff 		imo->imo_multicast_vif = -1;
1401a9771948SGleb Smirnoff 
1402a9771948SGleb Smirnoff 		addr.s_addr = htonl(INADDR_CARP_GROUP);
140308b68b0eSGleb Smirnoff 		if ((error = in_joingroup(ifp, &addr, NULL,
140459854ecfSHans Petter Selasky 		    &imf->imf_inm)) != 0) {
140559854ecfSHans Petter Selasky 			ip_mfilter_free(imf);
140608b68b0eSGleb Smirnoff 			break;
14072d9cfabaSRobert Watson 		}
140859854ecfSHans Petter Selasky 
140959854ecfSHans Petter Selasky 		ip_mfilter_insert(&imo->imo_head, imf);
1410a9771948SGleb Smirnoff 		imo->imo_multicast_ifp = ifp;
1411a9771948SGleb Smirnoff 		imo->imo_multicast_ttl = CARP_DFLTTL;
1412a9771948SGleb Smirnoff 		imo->imo_multicast_loop = 0;
1413a9771948SGleb Smirnoff 		break;
1414a9771948SGleb Smirnoff 	   }
141508b68b0eSGleb Smirnoff #endif
141608b68b0eSGleb Smirnoff #ifdef INET6
141708b68b0eSGleb Smirnoff 	case AF_INET6:
141808b68b0eSGleb Smirnoff 	    {
141908b68b0eSGleb Smirnoff 		struct ip6_moptions *im6o = &cif->cif_im6o;
142059854ecfSHans Petter Selasky 		struct in6_mfilter *im6f[2];
142108b68b0eSGleb Smirnoff 		struct in6_addr in6;
142233cde130SBruce M Simpson 
142359854ecfSHans Petter Selasky 		if (ip6_mfilter_first(&im6o->im6o_head))
142408b68b0eSGleb Smirnoff 			return (0);
142508b68b0eSGleb Smirnoff 
142659854ecfSHans Petter Selasky 		im6f[0] = ip6_mfilter_alloc(M_WAITOK, 0, 0);
142759854ecfSHans Petter Selasky 		im6f[1] = ip6_mfilter_alloc(M_WAITOK, 0, 0);
142859854ecfSHans Petter Selasky 
142959854ecfSHans Petter Selasky 		ip6_mfilter_init(&im6o->im6o_head);
143008b68b0eSGleb Smirnoff 		im6o->im6o_multicast_hlim = CARP_DFLTTL;
1431a9771948SGleb Smirnoff 		im6o->im6o_multicast_ifp = ifp;
1432a9771948SGleb Smirnoff 
143308b68b0eSGleb Smirnoff 		/* Join IPv6 CARP multicast group. */
1434a1f7e5f8SHajimu UMEMOTO 		bzero(&in6, sizeof(in6));
1435a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr16[0] = htons(0xff02);
1436a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr8[15] = 0x12;
143708b68b0eSGleb Smirnoff 		if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
143859854ecfSHans Petter Selasky 			ip6_mfilter_free(im6f[0]);
143959854ecfSHans Petter Selasky 			ip6_mfilter_free(im6f[1]);
144008b68b0eSGleb Smirnoff 			break;
144108b68b0eSGleb Smirnoff 		}
144259854ecfSHans Petter Selasky 		if ((error = in6_joingroup(ifp, &in6, NULL, &im6f[0]->im6f_in6m, 0)) != 0) {
144359854ecfSHans Petter Selasky 			ip6_mfilter_free(im6f[0]);
144459854ecfSHans Petter Selasky 			ip6_mfilter_free(im6f[1]);
144508b68b0eSGleb Smirnoff 			break;
144608b68b0eSGleb Smirnoff 		}
1447a9771948SGleb Smirnoff 
144808b68b0eSGleb Smirnoff 		/* Join solicited multicast address. */
1449a1f7e5f8SHajimu UMEMOTO 		bzero(&in6, sizeof(in6));
1450a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr16[0] = htons(0xff02);
1451a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr32[1] = 0;
1452a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr32[2] = htonl(1);
145308b68b0eSGleb Smirnoff 		in6.s6_addr32[3] = 0;
1454a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr8[12] = 0xff;
145559854ecfSHans Petter Selasky 
145608b68b0eSGleb Smirnoff 		if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
145759854ecfSHans Petter Selasky 			ip6_mfilter_free(im6f[0]);
145859854ecfSHans Petter Selasky 			ip6_mfilter_free(im6f[1]);
145908b68b0eSGleb Smirnoff 			break;
146008b68b0eSGleb Smirnoff 		}
146159854ecfSHans Petter Selasky 
146259854ecfSHans Petter Selasky 		if ((error = in6_joingroup(ifp, &in6, NULL, &im6f[1]->im6f_in6m, 0)) != 0) {
146359854ecfSHans Petter Selasky 			in6_leavegroup(im6f[0]->im6f_in6m, NULL);
146459854ecfSHans Petter Selasky 			ip6_mfilter_free(im6f[0]);
146559854ecfSHans Petter Selasky 			ip6_mfilter_free(im6f[1]);
146608b68b0eSGleb Smirnoff 			break;
146708b68b0eSGleb Smirnoff 		}
146859854ecfSHans Petter Selasky 		ip6_mfilter_insert(&im6o->im6o_head, im6f[0]);
146959854ecfSHans Petter Selasky 		ip6_mfilter_insert(&im6o->im6o_head, im6f[1]);
1470a9771948SGleb Smirnoff 		break;
1471a9771948SGleb Smirnoff 	    }
1472a9771948SGleb Smirnoff #endif
147308b68b0eSGleb Smirnoff 	}
147408b68b0eSGleb Smirnoff 
147508b68b0eSGleb Smirnoff 	return (error);
1476a9771948SGleb Smirnoff }
1477a9771948SGleb Smirnoff 
1478a9771948SGleb Smirnoff /*
147908b68b0eSGleb Smirnoff  * Free multicast structures.
1480a9771948SGleb Smirnoff  */
14815c1f0f6dSGleb Smirnoff static void
1482a9a2c40cSGleb Smirnoff carp_multicast_cleanup(struct carp_if *cif, sa_family_t sa)
1483a9771948SGleb Smirnoff {
148459854ecfSHans Petter Selasky #ifdef INET
148559854ecfSHans Petter Selasky 	struct ip_moptions *imo = &cif->cif_imo;
148659854ecfSHans Petter Selasky 	struct in_mfilter *imf;
148759854ecfSHans Petter Selasky #endif
148859854ecfSHans Petter Selasky #ifdef INET6
148959854ecfSHans Petter Selasky 	struct ip6_moptions *im6o = &cif->cif_im6o;
149059854ecfSHans Petter Selasky 	struct in6_mfilter *im6f;
149159854ecfSHans Petter Selasky #endif
14929c2cd1aaSGleb Smirnoff 	sx_assert(&carp_sx, SA_XLOCKED);
14939c2cd1aaSGleb Smirnoff 
149408b68b0eSGleb Smirnoff 	switch (sa) {
149508b68b0eSGleb Smirnoff #ifdef INET
149608b68b0eSGleb Smirnoff 	case AF_INET:
149759854ecfSHans Petter Selasky 		if (cif->cif_naddrs != 0)
149859854ecfSHans Petter Selasky 			break;
149908b68b0eSGleb Smirnoff 
150059854ecfSHans Petter Selasky 		while ((imf = ip_mfilter_first(&imo->imo_head)) != NULL) {
150159854ecfSHans Petter Selasky 			ip_mfilter_remove(&imo->imo_head, imf);
150259854ecfSHans Petter Selasky 			in_leavegroup(imf->imf_inm, NULL);
150359854ecfSHans Petter Selasky 			ip_mfilter_free(imf);
150408b68b0eSGleb Smirnoff 		}
150508b68b0eSGleb Smirnoff 		break;
1506a9771948SGleb Smirnoff #endif
150708b68b0eSGleb Smirnoff #ifdef INET6
150808b68b0eSGleb Smirnoff 	case AF_INET6:
150959854ecfSHans Petter Selasky 		if (cif->cif_naddrs6 != 0)
151059854ecfSHans Petter Selasky 			break;
151108b68b0eSGleb Smirnoff 
151259854ecfSHans Petter Selasky 		while ((im6f = ip6_mfilter_first(&im6o->im6o_head)) != NULL) {
151359854ecfSHans Petter Selasky 			ip6_mfilter_remove(&im6o->im6o_head, im6f);
151459854ecfSHans Petter Selasky 			in6_leavegroup(im6f->im6f_in6m, NULL);
151559854ecfSHans Petter Selasky 			ip6_mfilter_free(im6f);
151608b68b0eSGleb Smirnoff 		}
151708b68b0eSGleb Smirnoff 		break;
151808b68b0eSGleb Smirnoff #endif
151908b68b0eSGleb Smirnoff 	}
1520a9771948SGleb Smirnoff }
1521a9771948SGleb Smirnoff 
1522a9771948SGleb Smirnoff int
152347e8d432SGleb Smirnoff carp_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa)
1524a9771948SGleb Smirnoff {
1525a9771948SGleb Smirnoff 	struct m_tag *mtag;
1526a9771948SGleb Smirnoff 	struct carp_softc *sc;
1527a9771948SGleb Smirnoff 
1528a9771948SGleb Smirnoff 	if (!sa)
1529a9771948SGleb Smirnoff 		return (0);
1530a9771948SGleb Smirnoff 
1531a9771948SGleb Smirnoff 	switch (sa->sa_family) {
1532a9771948SGleb Smirnoff #ifdef INET
1533a9771948SGleb Smirnoff 	case AF_INET:
1534a9771948SGleb Smirnoff 		break;
153508b68b0eSGleb Smirnoff #endif
1536a9771948SGleb Smirnoff #ifdef INET6
1537a9771948SGleb Smirnoff 	case AF_INET6:
1538a9771948SGleb Smirnoff 		break;
153908b68b0eSGleb Smirnoff #endif
1540a9771948SGleb Smirnoff 	default:
1541a9771948SGleb Smirnoff 		return (0);
1542a9771948SGleb Smirnoff 	}
1543a9771948SGleb Smirnoff 
1544a9771948SGleb Smirnoff 	mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
1545a9771948SGleb Smirnoff 	if (mtag == NULL)
1546a9771948SGleb Smirnoff 		return (0);
1547a9771948SGleb Smirnoff 
1548eaf151c4SGleb Smirnoff 	bcopy(mtag + 1, &sc, sizeof(sc));
1549a9771948SGleb Smirnoff 
155008b68b0eSGleb Smirnoff 	/* Set the source MAC address to the Virtual Router MAC Address. */
1551a9771948SGleb Smirnoff 	switch (ifp->if_type) {
1552630481bbSYaroslav Tykhiy 	case IFT_ETHER:
15539ca1fe0dSGleb Smirnoff 	case IFT_BRIDGE:
1554630481bbSYaroslav Tykhiy 	case IFT_L2VLAN: {
1555a9771948SGleb Smirnoff 			struct ether_header *eh;
1556a9771948SGleb Smirnoff 
1557a9771948SGleb Smirnoff 			eh = mtod(m, struct ether_header *);
1558a9771948SGleb Smirnoff 			eh->ether_shost[0] = 0;
1559a9771948SGleb Smirnoff 			eh->ether_shost[1] = 0;
1560a9771948SGleb Smirnoff 			eh->ether_shost[2] = 0x5e;
1561a9771948SGleb Smirnoff 			eh->ether_shost[3] = 0;
1562a9771948SGleb Smirnoff 			eh->ether_shost[4] = 1;
1563a9771948SGleb Smirnoff 			eh->ether_shost[5] = sc->sc_vhid;
1564a9771948SGleb Smirnoff 		}
1565a9771948SGleb Smirnoff 		break;
1566a9771948SGleb Smirnoff 	default:
156708b68b0eSGleb Smirnoff 		printf("%s: carp is not supported for the %d interface type\n",
156808b68b0eSGleb Smirnoff 		    ifp->if_xname, ifp->if_type);
1569a9771948SGleb Smirnoff 		return (EOPNOTSUPP);
1570a9771948SGleb Smirnoff 	}
1571a9771948SGleb Smirnoff 
1572a9771948SGleb Smirnoff 	return (0);
1573a9771948SGleb Smirnoff }
1574a9771948SGleb Smirnoff 
157508b68b0eSGleb Smirnoff static struct carp_softc*
157608b68b0eSGleb Smirnoff carp_alloc(struct ifnet *ifp)
1577a9771948SGleb Smirnoff {
157808b68b0eSGleb Smirnoff 	struct carp_softc *sc;
157908b68b0eSGleb Smirnoff 	struct carp_if *cif;
1580d220759bSGleb Smirnoff 
158181098a01SAlexander Motin 	sx_assert(&carp_sx, SA_XLOCKED);
158281098a01SAlexander Motin 
15830cc726f2SGleb Smirnoff 	if ((cif = ifp->if_carp) == NULL)
158408b68b0eSGleb Smirnoff 		cif = carp_alloc_if(ifp);
1585a9771948SGleb Smirnoff 
158608b68b0eSGleb Smirnoff 	sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
158708b68b0eSGleb Smirnoff 
158808b68b0eSGleb Smirnoff 	sc->sc_advbase = CARP_DFLTINTV;
158908b68b0eSGleb Smirnoff 	sc->sc_vhid = -1;	/* required setting */
159008b68b0eSGleb Smirnoff 	sc->sc_init_counter = 1;
159108b68b0eSGleb Smirnoff 	sc->sc_state = INIT;
159208b68b0eSGleb Smirnoff 
159308b68b0eSGleb Smirnoff 	sc->sc_ifasiz = sizeof(struct ifaddr *);
159408b68b0eSGleb Smirnoff 	sc->sc_ifas = malloc(sc->sc_ifasiz, M_CARP, M_WAITOK|M_ZERO);
159508b68b0eSGleb Smirnoff 	sc->sc_carpdev = ifp;
159608b68b0eSGleb Smirnoff 
159708b68b0eSGleb Smirnoff 	CARP_LOCK_INIT(sc);
159808b68b0eSGleb Smirnoff #ifdef INET
159908b68b0eSGleb Smirnoff 	callout_init_mtx(&sc->sc_md_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
160008b68b0eSGleb Smirnoff #endif
160108b68b0eSGleb Smirnoff #ifdef INET6
160208b68b0eSGleb Smirnoff 	callout_init_mtx(&sc->sc_md6_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
160308b68b0eSGleb Smirnoff #endif
160408b68b0eSGleb Smirnoff 	callout_init_mtx(&sc->sc_ad_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
160508b68b0eSGleb Smirnoff 
160608b68b0eSGleb Smirnoff 	CIF_LOCK(cif);
160708b68b0eSGleb Smirnoff 	TAILQ_INSERT_TAIL(&cif->cif_vrs, sc, sc_list);
160808b68b0eSGleb Smirnoff 	CIF_UNLOCK(cif);
160908b68b0eSGleb Smirnoff 
161008b68b0eSGleb Smirnoff 	mtx_lock(&carp_mtx);
161108b68b0eSGleb Smirnoff 	LIST_INSERT_HEAD(&carp_list, sc, sc_next);
161208b68b0eSGleb Smirnoff 	mtx_unlock(&carp_mtx);
161308b68b0eSGleb Smirnoff 
161408b68b0eSGleb Smirnoff 	return (sc);
161508b68b0eSGleb Smirnoff }
161608b68b0eSGleb Smirnoff 
16179c2cd1aaSGleb Smirnoff static void
161808b68b0eSGleb Smirnoff carp_grow_ifas(struct carp_softc *sc)
161908b68b0eSGleb Smirnoff {
162008b68b0eSGleb Smirnoff 	struct ifaddr **new;
162108b68b0eSGleb Smirnoff 
16229c2cd1aaSGleb Smirnoff 	new = malloc(sc->sc_ifasiz * 2, M_CARP, M_WAITOK | M_ZERO);
16239c2cd1aaSGleb Smirnoff 	CARP_LOCK(sc);
162408b68b0eSGleb Smirnoff 	bcopy(sc->sc_ifas, new, sc->sc_ifasiz);
162508b68b0eSGleb Smirnoff 	free(sc->sc_ifas, M_CARP);
162608b68b0eSGleb Smirnoff 	sc->sc_ifas = new;
162708b68b0eSGleb Smirnoff 	sc->sc_ifasiz *= 2;
16289c2cd1aaSGleb Smirnoff 	CARP_UNLOCK(sc);
162908b68b0eSGleb Smirnoff }
163008b68b0eSGleb Smirnoff 
163108b68b0eSGleb Smirnoff static void
163208b68b0eSGleb Smirnoff carp_destroy(struct carp_softc *sc)
163308b68b0eSGleb Smirnoff {
163408b68b0eSGleb Smirnoff 	struct ifnet *ifp = sc->sc_carpdev;
163508b68b0eSGleb Smirnoff 	struct carp_if *cif = ifp->if_carp;
163608b68b0eSGleb Smirnoff 
16379c2cd1aaSGleb Smirnoff 	sx_assert(&carp_sx, SA_XLOCKED);
1638a9a2c40cSGleb Smirnoff 
16399c2cd1aaSGleb Smirnoff 	if (sc->sc_suppress)
16409c2cd1aaSGleb Smirnoff 		carp_demote_adj(-V_carp_ifdown_adj, "vhid removed");
16419c2cd1aaSGleb Smirnoff 	CARP_UNLOCK(sc);
16429c2cd1aaSGleb Smirnoff 
16439c2cd1aaSGleb Smirnoff 	CIF_LOCK(cif);
164408b68b0eSGleb Smirnoff 	TAILQ_REMOVE(&cif->cif_vrs, sc, sc_list);
16459c2cd1aaSGleb Smirnoff 	CIF_UNLOCK(cif);
164608b68b0eSGleb Smirnoff 
164708b68b0eSGleb Smirnoff 	mtx_lock(&carp_mtx);
164808b68b0eSGleb Smirnoff 	LIST_REMOVE(sc, sc_next);
164908b68b0eSGleb Smirnoff 	mtx_unlock(&carp_mtx);
165008b68b0eSGleb Smirnoff 
165108b68b0eSGleb Smirnoff 	callout_drain(&sc->sc_ad_tmo);
165208b68b0eSGleb Smirnoff #ifdef INET
165308b68b0eSGleb Smirnoff 	callout_drain(&sc->sc_md_tmo);
165408b68b0eSGleb Smirnoff #endif
165508b68b0eSGleb Smirnoff #ifdef INET6
165608b68b0eSGleb Smirnoff 	callout_drain(&sc->sc_md6_tmo);
165708b68b0eSGleb Smirnoff #endif
165808b68b0eSGleb Smirnoff 	CARP_LOCK_DESTROY(sc);
165908b68b0eSGleb Smirnoff 
166008b68b0eSGleb Smirnoff 	free(sc->sc_ifas, M_CARP);
166108b68b0eSGleb Smirnoff 	free(sc, M_CARP);
166208b68b0eSGleb Smirnoff }
166308b68b0eSGleb Smirnoff 
166408b68b0eSGleb Smirnoff static struct carp_if*
166508b68b0eSGleb Smirnoff carp_alloc_if(struct ifnet *ifp)
1666a9771948SGleb Smirnoff {
166754bfbd51SWill Andrews 	struct carp_if *cif;
16680cc726f2SGleb Smirnoff 	int error;
1669d220759bSGleb Smirnoff 
167008b68b0eSGleb Smirnoff 	cif = malloc(sizeof(*cif), M_CARP, M_WAITOK|M_ZERO);
167108b68b0eSGleb Smirnoff 
16720cc726f2SGleb Smirnoff 	if ((error = ifpromisc(ifp, 1)) != 0)
16730cc726f2SGleb Smirnoff 		printf("%s: ifpromisc(%s) failed: %d\n",
16740cc726f2SGleb Smirnoff 		    __func__, ifp->if_xname, error);
16750cc726f2SGleb Smirnoff 	else
16760cc726f2SGleb Smirnoff 		cif->cif_flags |= CIF_PROMISC;
167708b68b0eSGleb Smirnoff 
167808b68b0eSGleb Smirnoff 	CIF_LOCK_INIT(cif);
167908b68b0eSGleb Smirnoff 	cif->cif_ifp = ifp;
168008b68b0eSGleb Smirnoff 	TAILQ_INIT(&cif->cif_vrs);
168108b68b0eSGleb Smirnoff 
1682137f91e8SJohn Baldwin 	IF_ADDR_WLOCK(ifp);
168308b68b0eSGleb Smirnoff 	ifp->if_carp = cif;
168408b68b0eSGleb Smirnoff 	if_ref(ifp);
1685137f91e8SJohn Baldwin 	IF_ADDR_WUNLOCK(ifp);
168608b68b0eSGleb Smirnoff 
168708b68b0eSGleb Smirnoff 	return (cif);
1688d220759bSGleb Smirnoff }
1689d220759bSGleb Smirnoff 
1690d220759bSGleb Smirnoff static void
169108b68b0eSGleb Smirnoff carp_free_if(struct carp_if *cif)
169208b68b0eSGleb Smirnoff {
169308b68b0eSGleb Smirnoff 	struct ifnet *ifp = cif->cif_ifp;
169408b68b0eSGleb Smirnoff 
169508b68b0eSGleb Smirnoff 	CIF_LOCK_ASSERT(cif);
169608b68b0eSGleb Smirnoff 	KASSERT(TAILQ_EMPTY(&cif->cif_vrs), ("%s: softc list not empty",
169708b68b0eSGleb Smirnoff 	    __func__));
169808b68b0eSGleb Smirnoff 
1699137f91e8SJohn Baldwin 	IF_ADDR_WLOCK(ifp);
170008b68b0eSGleb Smirnoff 	ifp->if_carp = NULL;
1701137f91e8SJohn Baldwin 	IF_ADDR_WUNLOCK(ifp);
170208b68b0eSGleb Smirnoff 
170308b68b0eSGleb Smirnoff 	CIF_LOCK_DESTROY(cif);
170408b68b0eSGleb Smirnoff 
17050cc726f2SGleb Smirnoff 	if (cif->cif_flags & CIF_PROMISC)
170608b68b0eSGleb Smirnoff 		ifpromisc(ifp, 0);
17071f6addd9SMikolaj Golub 	if_rele(ifp);
170808b68b0eSGleb Smirnoff 
170908b68b0eSGleb Smirnoff 	free(cif, M_CARP);
171008b68b0eSGleb Smirnoff }
171108b68b0eSGleb Smirnoff 
171208b68b0eSGleb Smirnoff static void
171308b68b0eSGleb Smirnoff carp_carprcp(struct carpreq *carpr, struct carp_softc *sc, int priv)
171408b68b0eSGleb Smirnoff {
171508b68b0eSGleb Smirnoff 
171608b68b0eSGleb Smirnoff 	CARP_LOCK(sc);
171708b68b0eSGleb Smirnoff 	carpr->carpr_state = sc->sc_state;
171808b68b0eSGleb Smirnoff 	carpr->carpr_vhid = sc->sc_vhid;
171908b68b0eSGleb Smirnoff 	carpr->carpr_advbase = sc->sc_advbase;
172008b68b0eSGleb Smirnoff 	carpr->carpr_advskew = sc->sc_advskew;
172108b68b0eSGleb Smirnoff 	if (priv)
172208b68b0eSGleb Smirnoff 		bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key));
172308b68b0eSGleb Smirnoff 	else
172408b68b0eSGleb Smirnoff 		bzero(carpr->carpr_key, sizeof(carpr->carpr_key));
172508b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
172608b68b0eSGleb Smirnoff }
172708b68b0eSGleb Smirnoff 
172808b68b0eSGleb Smirnoff int
172908b68b0eSGleb Smirnoff carp_ioctl(struct ifreq *ifr, u_long cmd, struct thread *td)
173008b68b0eSGleb Smirnoff {
1731*49cad3daSZhenlei Huang 	struct epoch_tracker et;
173208b68b0eSGleb Smirnoff 	struct carpreq carpr;
173308b68b0eSGleb Smirnoff 	struct ifnet *ifp;
173408b68b0eSGleb Smirnoff 	struct carp_softc *sc = NULL;
173508b68b0eSGleb Smirnoff 	int error = 0, locked = 0;
173608b68b0eSGleb Smirnoff 
1737541d96aaSBrooks Davis 	if ((error = copyin(ifr_data_get_ptr(ifr), &carpr, sizeof carpr)))
173808b68b0eSGleb Smirnoff 		return (error);
173908b68b0eSGleb Smirnoff 
174008b68b0eSGleb Smirnoff 	ifp = ifunit_ref(ifr->ifr_name);
174108b68b0eSGleb Smirnoff 	if (ifp == NULL)
174208b68b0eSGleb Smirnoff 		return (ENXIO);
174308b68b0eSGleb Smirnoff 
174408b68b0eSGleb Smirnoff 	switch (ifp->if_type) {
174508b68b0eSGleb Smirnoff 	case IFT_ETHER:
174608b68b0eSGleb Smirnoff 	case IFT_L2VLAN:
17479ca1fe0dSGleb Smirnoff 	case IFT_BRIDGE:
174808b68b0eSGleb Smirnoff 		break;
174908b68b0eSGleb Smirnoff 	default:
175008b68b0eSGleb Smirnoff 		error = EOPNOTSUPP;
175108b68b0eSGleb Smirnoff 		goto out;
175208b68b0eSGleb Smirnoff 	}
175308b68b0eSGleb Smirnoff 
175408b68b0eSGleb Smirnoff 	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
175508b68b0eSGleb Smirnoff 		error = EADDRNOTAVAIL;
175608b68b0eSGleb Smirnoff 		goto out;
175708b68b0eSGleb Smirnoff 	}
175808b68b0eSGleb Smirnoff 
175993d4534cSGleb Smirnoff 	sx_xlock(&carp_sx);
176008b68b0eSGleb Smirnoff 	switch (cmd) {
176108b68b0eSGleb Smirnoff 	case SIOCSVH:
176208b68b0eSGleb Smirnoff 		if ((error = priv_check(td, PRIV_NETINET_CARP)))
176308b68b0eSGleb Smirnoff 			break;
176408b68b0eSGleb Smirnoff 		if (carpr.carpr_vhid <= 0 || carpr.carpr_vhid > CARP_MAXVHID ||
176508b68b0eSGleb Smirnoff 		    carpr.carpr_advbase < 0 || carpr.carpr_advskew < 0) {
176608b68b0eSGleb Smirnoff 			error = EINVAL;
176708b68b0eSGleb Smirnoff 			break;
176808b68b0eSGleb Smirnoff 		}
176908b68b0eSGleb Smirnoff 
177008b68b0eSGleb Smirnoff 		if (ifp->if_carp) {
177108b68b0eSGleb Smirnoff 			IFNET_FOREACH_CARP(ifp, sc)
177208b68b0eSGleb Smirnoff 				if (sc->sc_vhid == carpr.carpr_vhid)
177308b68b0eSGleb Smirnoff 					break;
177408b68b0eSGleb Smirnoff 		}
177508b68b0eSGleb Smirnoff 		if (sc == NULL) {
177608b68b0eSGleb Smirnoff 			sc = carp_alloc(ifp);
177708b68b0eSGleb Smirnoff 			CARP_LOCK(sc);
177808b68b0eSGleb Smirnoff 			sc->sc_vhid = carpr.carpr_vhid;
177908b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[0] = 0;
178008b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[1] = 0;
178108b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[2] = 0x5e;
178208b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[3] = 0;
178308b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[4] = 1;
178408b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[5] = sc->sc_vhid;
178508b68b0eSGleb Smirnoff 		} else
178608b68b0eSGleb Smirnoff 			CARP_LOCK(sc);
178708b68b0eSGleb Smirnoff 		locked = 1;
178808b68b0eSGleb Smirnoff 		if (carpr.carpr_advbase > 0) {
178908b68b0eSGleb Smirnoff 			if (carpr.carpr_advbase > 255 ||
179008b68b0eSGleb Smirnoff 			    carpr.carpr_advbase < CARP_DFLTINTV) {
179108b68b0eSGleb Smirnoff 				error = EINVAL;
179208b68b0eSGleb Smirnoff 				break;
179308b68b0eSGleb Smirnoff 			}
179408b68b0eSGleb Smirnoff 			sc->sc_advbase = carpr.carpr_advbase;
179508b68b0eSGleb Smirnoff 		}
179608b68b0eSGleb Smirnoff 		if (carpr.carpr_advskew >= 255) {
179708b68b0eSGleb Smirnoff 			error = EINVAL;
179808b68b0eSGleb Smirnoff 			break;
179908b68b0eSGleb Smirnoff 		}
180008b68b0eSGleb Smirnoff 		sc->sc_advskew = carpr.carpr_advskew;
180108b68b0eSGleb Smirnoff 		if (carpr.carpr_key[0] != '\0') {
180208b68b0eSGleb Smirnoff 			bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
180308b68b0eSGleb Smirnoff 			carp_hmac_prepare(sc);
180408b68b0eSGleb Smirnoff 		}
180508b68b0eSGleb Smirnoff 		if (sc->sc_state != INIT &&
180608b68b0eSGleb Smirnoff 		    carpr.carpr_state != sc->sc_state) {
180708b68b0eSGleb Smirnoff 			switch (carpr.carpr_state) {
180808b68b0eSGleb Smirnoff 			case BACKUP:
180908b68b0eSGleb Smirnoff 				callout_stop(&sc->sc_ad_tmo);
1810369a6708SWill Andrews 				carp_set_state(sc, BACKUP,
1811369a6708SWill Andrews 				    "user requested via ifconfig");
181208b68b0eSGleb Smirnoff 				carp_setrun(sc, 0);
181308b68b0eSGleb Smirnoff 				carp_delroute(sc);
181408b68b0eSGleb Smirnoff 				break;
181508b68b0eSGleb Smirnoff 			case MASTER:
1816*49cad3daSZhenlei Huang 				NET_EPOCH_ENTER(et);
1817369a6708SWill Andrews 				carp_master_down_locked(sc,
1818369a6708SWill Andrews 				    "user requested via ifconfig");
1819*49cad3daSZhenlei Huang 				NET_EPOCH_EXIT(et);
182008b68b0eSGleb Smirnoff 				break;
182108b68b0eSGleb Smirnoff 			default:
182208b68b0eSGleb Smirnoff 				break;
182308b68b0eSGleb Smirnoff 			}
182408b68b0eSGleb Smirnoff 		}
182508b68b0eSGleb Smirnoff 		break;
182608b68b0eSGleb Smirnoff 
182708b68b0eSGleb Smirnoff 	case SIOCGVH:
182808b68b0eSGleb Smirnoff 	    {
182908b68b0eSGleb Smirnoff 		int priveleged;
183008b68b0eSGleb Smirnoff 
183108b68b0eSGleb Smirnoff 		if (carpr.carpr_vhid < 0 || carpr.carpr_vhid > CARP_MAXVHID) {
183208b68b0eSGleb Smirnoff 			error = EINVAL;
183308b68b0eSGleb Smirnoff 			break;
183408b68b0eSGleb Smirnoff 		}
183508b68b0eSGleb Smirnoff 		if (carpr.carpr_count < 1) {
183608b68b0eSGleb Smirnoff 			error = EMSGSIZE;
183708b68b0eSGleb Smirnoff 			break;
183808b68b0eSGleb Smirnoff 		}
183908b68b0eSGleb Smirnoff 		if (ifp->if_carp == NULL) {
184008b68b0eSGleb Smirnoff 			error = ENOENT;
184108b68b0eSGleb Smirnoff 			break;
184208b68b0eSGleb Smirnoff 		}
184308b68b0eSGleb Smirnoff 
184408b68b0eSGleb Smirnoff 		priveleged = (priv_check(td, PRIV_NETINET_CARP) == 0);
184508b68b0eSGleb Smirnoff 		if (carpr.carpr_vhid != 0) {
184608b68b0eSGleb Smirnoff 			IFNET_FOREACH_CARP(ifp, sc)
184708b68b0eSGleb Smirnoff 				if (sc->sc_vhid == carpr.carpr_vhid)
184808b68b0eSGleb Smirnoff 					break;
184908b68b0eSGleb Smirnoff 			if (sc == NULL) {
185008b68b0eSGleb Smirnoff 				error = ENOENT;
185108b68b0eSGleb Smirnoff 				break;
185208b68b0eSGleb Smirnoff 			}
185308b68b0eSGleb Smirnoff 			carp_carprcp(&carpr, sc, priveleged);
1854541d96aaSBrooks Davis 			error = copyout(&carpr, ifr_data_get_ptr(ifr),
1855541d96aaSBrooks Davis 			    sizeof(carpr));
185608b68b0eSGleb Smirnoff 		} else  {
185708b68b0eSGleb Smirnoff 			int i, count;
185808b68b0eSGleb Smirnoff 
185908b68b0eSGleb Smirnoff 			count = 0;
186008b68b0eSGleb Smirnoff 			IFNET_FOREACH_CARP(ifp, sc)
186108b68b0eSGleb Smirnoff 				count++;
186208b68b0eSGleb Smirnoff 
186308b68b0eSGleb Smirnoff 			if (count > carpr.carpr_count) {
186408b68b0eSGleb Smirnoff 				CIF_UNLOCK(ifp->if_carp);
186508b68b0eSGleb Smirnoff 				error = EMSGSIZE;
186608b68b0eSGleb Smirnoff 				break;
186708b68b0eSGleb Smirnoff 			}
186808b68b0eSGleb Smirnoff 
186908b68b0eSGleb Smirnoff 			i = 0;
187008b68b0eSGleb Smirnoff 			IFNET_FOREACH_CARP(ifp, sc) {
187108b68b0eSGleb Smirnoff 				carp_carprcp(&carpr, sc, priveleged);
187208b68b0eSGleb Smirnoff 				carpr.carpr_count = count;
1873541d96aaSBrooks Davis 				error = copyout(&carpr,
1874dad68fc3SBjoern A. Zeeb 				    (char *)ifr_data_get_ptr(ifr) +
187508b68b0eSGleb Smirnoff 				    (i * sizeof(carpr)), sizeof(carpr));
187608b68b0eSGleb Smirnoff 				if (error) {
187708b68b0eSGleb Smirnoff 					CIF_UNLOCK(ifp->if_carp);
187808b68b0eSGleb Smirnoff 					break;
187908b68b0eSGleb Smirnoff 				}
188008b68b0eSGleb Smirnoff 				i++;
188108b68b0eSGleb Smirnoff 			}
188208b68b0eSGleb Smirnoff 		}
188308b68b0eSGleb Smirnoff 		break;
188408b68b0eSGleb Smirnoff 	    }
188508b68b0eSGleb Smirnoff 	default:
188608b68b0eSGleb Smirnoff 		error = EINVAL;
188708b68b0eSGleb Smirnoff 	}
188893d4534cSGleb Smirnoff 	sx_xunlock(&carp_sx);
188908b68b0eSGleb Smirnoff 
189008b68b0eSGleb Smirnoff out:
189108b68b0eSGleb Smirnoff 	if (locked)
189208b68b0eSGleb Smirnoff 		CARP_UNLOCK(sc);
189308b68b0eSGleb Smirnoff 	if_rele(ifp);
189408b68b0eSGleb Smirnoff 
189508b68b0eSGleb Smirnoff 	return (error);
189608b68b0eSGleb Smirnoff }
189708b68b0eSGleb Smirnoff 
189808b68b0eSGleb Smirnoff static int
189908b68b0eSGleb Smirnoff carp_get_vhid(struct ifaddr *ifa)
190008b68b0eSGleb Smirnoff {
190108b68b0eSGleb Smirnoff 
190208b68b0eSGleb Smirnoff 	if (ifa == NULL || ifa->ifa_carp == NULL)
190308b68b0eSGleb Smirnoff 		return (0);
190408b68b0eSGleb Smirnoff 
190508b68b0eSGleb Smirnoff 	return (ifa->ifa_carp->sc_vhid);
190608b68b0eSGleb Smirnoff }
190708b68b0eSGleb Smirnoff 
190808b68b0eSGleb Smirnoff int
190908b68b0eSGleb Smirnoff carp_attach(struct ifaddr *ifa, int vhid)
191008b68b0eSGleb Smirnoff {
191108b68b0eSGleb Smirnoff 	struct ifnet *ifp = ifa->ifa_ifp;
1912a9a2c40cSGleb Smirnoff 	struct carp_if *cif = ifp->if_carp;
191308b68b0eSGleb Smirnoff 	struct carp_softc *sc;
191408b68b0eSGleb Smirnoff 	int index, error;
191508b68b0eSGleb Smirnoff 
19169c2cd1aaSGleb Smirnoff 	KASSERT(ifa->ifa_carp == NULL, ("%s: ifa %p attached", __func__, ifa));
191708b68b0eSGleb Smirnoff 
191808b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
191908b68b0eSGleb Smirnoff #ifdef INET
192008b68b0eSGleb Smirnoff 	case AF_INET:
192108b68b0eSGleb Smirnoff #endif
192208b68b0eSGleb Smirnoff #ifdef INET6
192308b68b0eSGleb Smirnoff 	case AF_INET6:
192408b68b0eSGleb Smirnoff #endif
192508b68b0eSGleb Smirnoff 		break;
192608b68b0eSGleb Smirnoff 	default:
192708b68b0eSGleb Smirnoff 		return (EPROTOTYPE);
192808b68b0eSGleb Smirnoff 	}
192908b68b0eSGleb Smirnoff 
19309c2cd1aaSGleb Smirnoff 	sx_xlock(&carp_sx);
19319c2cd1aaSGleb Smirnoff 	if (ifp->if_carp == NULL) {
19329c2cd1aaSGleb Smirnoff 		sx_xunlock(&carp_sx);
19339c2cd1aaSGleb Smirnoff 		return (ENOPROTOOPT);
19349c2cd1aaSGleb Smirnoff 	}
19359c2cd1aaSGleb Smirnoff 
193608b68b0eSGleb Smirnoff 	IFNET_FOREACH_CARP(ifp, sc)
193708b68b0eSGleb Smirnoff 		if (sc->sc_vhid == vhid)
193808b68b0eSGleb Smirnoff 			break;
1939a9a2c40cSGleb Smirnoff 	if (sc == NULL) {
19409c2cd1aaSGleb Smirnoff 		sx_xunlock(&carp_sx);
194108b68b0eSGleb Smirnoff 		return (ENOENT);
1942a9a2c40cSGleb Smirnoff 	}
194308b68b0eSGleb Smirnoff 
1944a9a2c40cSGleb Smirnoff 	error = carp_multicast_setup(cif, ifa->ifa_addr->sa_family);
1945a9a2c40cSGleb Smirnoff 	if (error) {
1946a9a2c40cSGleb Smirnoff 		CIF_FREE(cif);
19479c2cd1aaSGleb Smirnoff 		sx_xunlock(&carp_sx);
194808b68b0eSGleb Smirnoff 		return (error);
1949a9a2c40cSGleb Smirnoff 	}
195008b68b0eSGleb Smirnoff 
195108b68b0eSGleb Smirnoff 	index = sc->sc_naddrs + sc->sc_naddrs6 + 1;
195208b68b0eSGleb Smirnoff 	if (index > sc->sc_ifasiz / sizeof(struct ifaddr *))
19539c2cd1aaSGleb Smirnoff 		carp_grow_ifas(sc);
195408b68b0eSGleb Smirnoff 
195508b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
195608b68b0eSGleb Smirnoff #ifdef INET
195708b68b0eSGleb Smirnoff 	case AF_INET:
1958a9a2c40cSGleb Smirnoff 		cif->cif_naddrs++;
195908b68b0eSGleb Smirnoff 		sc->sc_naddrs++;
196008b68b0eSGleb Smirnoff 		break;
196108b68b0eSGleb Smirnoff #endif
196208b68b0eSGleb Smirnoff #ifdef INET6
196308b68b0eSGleb Smirnoff 	case AF_INET6:
1964a9a2c40cSGleb Smirnoff 		cif->cif_naddrs6++;
196508b68b0eSGleb Smirnoff 		sc->sc_naddrs6++;
196608b68b0eSGleb Smirnoff 		break;
196708b68b0eSGleb Smirnoff #endif
196808b68b0eSGleb Smirnoff 	}
196908b68b0eSGleb Smirnoff 
197008b68b0eSGleb Smirnoff 	ifa_ref(ifa);
19719c2cd1aaSGleb Smirnoff 
19729c2cd1aaSGleb Smirnoff 	CARP_LOCK(sc);
197308b68b0eSGleb Smirnoff 	sc->sc_ifas[index - 1] = ifa;
197408b68b0eSGleb Smirnoff 	ifa->ifa_carp = sc;
197508b68b0eSGleb Smirnoff 	carp_hmac_prepare(sc);
197608b68b0eSGleb Smirnoff 	carp_sc_state(sc);
197708b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
19789c2cd1aaSGleb Smirnoff 
19799c2cd1aaSGleb Smirnoff 	sx_xunlock(&carp_sx);
198008b68b0eSGleb Smirnoff 
198108b68b0eSGleb Smirnoff 	return (0);
198208b68b0eSGleb Smirnoff }
198308b68b0eSGleb Smirnoff 
198408b68b0eSGleb Smirnoff void
1985338e227aSLuiz Otavio O Souza carp_detach(struct ifaddr *ifa, bool keep_cif)
198608b68b0eSGleb Smirnoff {
1987a9a2c40cSGleb Smirnoff 	struct ifnet *ifp = ifa->ifa_ifp;
1988a9a2c40cSGleb Smirnoff 	struct carp_if *cif = ifp->if_carp;
198908b68b0eSGleb Smirnoff 	struct carp_softc *sc = ifa->ifa_carp;
199008b68b0eSGleb Smirnoff 	int i, index;
199108b68b0eSGleb Smirnoff 
199208b68b0eSGleb Smirnoff 	KASSERT(sc != NULL, ("%s: %p not attached", __func__, ifa));
199308b68b0eSGleb Smirnoff 
19949c2cd1aaSGleb Smirnoff 	sx_xlock(&carp_sx);
199508b68b0eSGleb Smirnoff 
19969c2cd1aaSGleb Smirnoff 	CARP_LOCK(sc);
199708b68b0eSGleb Smirnoff 	/* Shift array. */
199808b68b0eSGleb Smirnoff 	index = sc->sc_naddrs + sc->sc_naddrs6;
199908b68b0eSGleb Smirnoff 	for (i = 0; i < index; i++)
200008b68b0eSGleb Smirnoff 		if (sc->sc_ifas[i] == ifa)
200108b68b0eSGleb Smirnoff 			break;
200208b68b0eSGleb Smirnoff 	KASSERT(i < index, ("%s: %p no backref", __func__, ifa));
200308b68b0eSGleb Smirnoff 	for (; i < index - 1; i++)
200408b68b0eSGleb Smirnoff 		sc->sc_ifas[i] = sc->sc_ifas[i+1];
200508b68b0eSGleb Smirnoff 	sc->sc_ifas[index - 1] = NULL;
200608b68b0eSGleb Smirnoff 
200708b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
200808b68b0eSGleb Smirnoff #ifdef INET
200908b68b0eSGleb Smirnoff 	case AF_INET:
2010a9a2c40cSGleb Smirnoff 		cif->cif_naddrs--;
201108b68b0eSGleb Smirnoff 		sc->sc_naddrs--;
201208b68b0eSGleb Smirnoff 		break;
201308b68b0eSGleb Smirnoff #endif
201408b68b0eSGleb Smirnoff #ifdef INET6
201508b68b0eSGleb Smirnoff 	case AF_INET6:
2016a9a2c40cSGleb Smirnoff 		cif->cif_naddrs6--;
201708b68b0eSGleb Smirnoff 		sc->sc_naddrs6--;
201808b68b0eSGleb Smirnoff 		break;
201908b68b0eSGleb Smirnoff #endif
202008b68b0eSGleb Smirnoff 	}
202108b68b0eSGleb Smirnoff 
20222512b096SGleb Smirnoff 	carp_ifa_delroute(ifa);
2023a9a2c40cSGleb Smirnoff 	carp_multicast_cleanup(cif, ifa->ifa_addr->sa_family);
202408b68b0eSGleb Smirnoff 
202508b68b0eSGleb Smirnoff 	ifa->ifa_carp = NULL;
202608b68b0eSGleb Smirnoff 	ifa_free(ifa);
202708b68b0eSGleb Smirnoff 
202808b68b0eSGleb Smirnoff 	carp_hmac_prepare(sc);
202908b68b0eSGleb Smirnoff 	carp_sc_state(sc);
203008b68b0eSGleb Smirnoff 
2031338e227aSLuiz Otavio O Souza 	if (!keep_cif && sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0)
203208b68b0eSGleb Smirnoff 		carp_destroy(sc);
20339c2cd1aaSGleb Smirnoff 	else
203408b68b0eSGleb Smirnoff 		CARP_UNLOCK(sc);
20359c2cd1aaSGleb Smirnoff 
2036338e227aSLuiz Otavio O Souza 	if (!keep_cif)
20379c2cd1aaSGleb Smirnoff 		CIF_FREE(cif);
20389c2cd1aaSGleb Smirnoff 
20399c2cd1aaSGleb Smirnoff 	sx_xunlock(&carp_sx);
204008b68b0eSGleb Smirnoff }
204108b68b0eSGleb Smirnoff 
204208b68b0eSGleb Smirnoff static void
2043d01641e2SWill Andrews carp_set_state(struct carp_softc *sc, int state, const char *reason)
204408b68b0eSGleb Smirnoff {
204508b68b0eSGleb Smirnoff 
204608b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
204708b68b0eSGleb Smirnoff 
204808b68b0eSGleb Smirnoff 	if (sc->sc_state != state) {
204908b68b0eSGleb Smirnoff 		const char *carp_states[] = { CARP_STATES };
205008b68b0eSGleb Smirnoff 		char subsys[IFNAMSIZ+5];
205108b68b0eSGleb Smirnoff 
205208b68b0eSGleb Smirnoff 		snprintf(subsys, IFNAMSIZ+5, "%u@%s", sc->sc_vhid,
205308b68b0eSGleb Smirnoff 		    sc->sc_carpdev->if_xname);
2054d01641e2SWill Andrews 
2055d01641e2SWill Andrews 		CARP_LOG("%s: %s -> %s (%s)\n", subsys,
2056d01641e2SWill Andrews 		    carp_states[sc->sc_state], carp_states[state], reason);
2057d01641e2SWill Andrews 
2058d01641e2SWill Andrews 		sc->sc_state = state;
2059d01641e2SWill Andrews 
206008b68b0eSGleb Smirnoff 		devctl_notify("CARP", subsys, carp_states[state], NULL);
206108b68b0eSGleb Smirnoff 	}
206208b68b0eSGleb Smirnoff }
206308b68b0eSGleb Smirnoff 
206408b68b0eSGleb Smirnoff static void
206508b68b0eSGleb Smirnoff carp_linkstate(struct ifnet *ifp)
2066d220759bSGleb Smirnoff {
2067d220759bSGleb Smirnoff 	struct carp_softc *sc;
2068d220759bSGleb Smirnoff 
206908b68b0eSGleb Smirnoff 	CIF_LOCK(ifp->if_carp);
207008b68b0eSGleb Smirnoff 	IFNET_FOREACH_CARP(ifp, sc) {
207108b68b0eSGleb Smirnoff 		CARP_LOCK(sc);
207208b68b0eSGleb Smirnoff 		carp_sc_state(sc);
207308b68b0eSGleb Smirnoff 		CARP_UNLOCK(sc);
207408b68b0eSGleb Smirnoff 	}
207508b68b0eSGleb Smirnoff 	CIF_UNLOCK(ifp->if_carp);
20764cb39345SGleb Smirnoff }
20774cb39345SGleb Smirnoff 
20784cb39345SGleb Smirnoff static void
207908b68b0eSGleb Smirnoff carp_sc_state(struct carp_softc *sc)
20804cb39345SGleb Smirnoff {
208108b68b0eSGleb Smirnoff 
208208b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
20834cb39345SGleb Smirnoff 
2084e8c34a71SGleb Smirnoff 	if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
2085167a3440SAlexander Motin 	    !(sc->sc_carpdev->if_flags & IFF_UP) ||
2086167a3440SAlexander Motin 	    !V_carp_allow) {
2087a9771948SGleb Smirnoff 		callout_stop(&sc->sc_ad_tmo);
208808b68b0eSGleb Smirnoff #ifdef INET
2089a9771948SGleb Smirnoff 		callout_stop(&sc->sc_md_tmo);
209008b68b0eSGleb Smirnoff #endif
209108b68b0eSGleb Smirnoff #ifdef INET6
2092a9771948SGleb Smirnoff 		callout_stop(&sc->sc_md6_tmo);
209308b68b0eSGleb Smirnoff #endif
2094bb269f3aSWill Andrews 		carp_set_state(sc, INIT, "hardware interface down");
2095a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
2096f08535f8SGleb Smirnoff 		if (!sc->sc_suppress)
2097c5c392e7SMikolaj Golub 			carp_demote_adj(V_carp_ifdown_adj, "interface down");
2098a9771948SGleb Smirnoff 		sc->sc_suppress = 1;
2099a9771948SGleb Smirnoff 	} else {
2100bb269f3aSWill Andrews 		carp_set_state(sc, INIT, "hardware interface up");
2101a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
2102a9771948SGleb Smirnoff 		if (sc->sc_suppress)
2103c5c392e7SMikolaj Golub 			carp_demote_adj(-V_carp_ifdown_adj, "interface up");
2104a9771948SGleb Smirnoff 		sc->sc_suppress = 0;
2105a9771948SGleb Smirnoff 	}
2106a9771948SGleb Smirnoff }
2107a9771948SGleb Smirnoff 
2108f08535f8SGleb Smirnoff static void
2109f08535f8SGleb Smirnoff carp_demote_adj(int adj, char *reason)
2110f08535f8SGleb Smirnoff {
2111c5c392e7SMikolaj Golub 	atomic_add_int(&V_carp_demotion, adj);
2112c5c392e7SMikolaj Golub 	CARP_LOG("demoted by %d to %d (%s)\n", adj, V_carp_demotion, reason);
2113f08535f8SGleb Smirnoff 	taskqueue_enqueue(taskqueue_swi, &carp_sendall_task);
2114f08535f8SGleb Smirnoff }
211508b68b0eSGleb Smirnoff 
21167951008bSGleb Smirnoff static int
2117167a3440SAlexander Motin carp_allow_sysctl(SYSCTL_HANDLER_ARGS)
2118167a3440SAlexander Motin {
2119167a3440SAlexander Motin 	int new, error;
2120167a3440SAlexander Motin 	struct carp_softc *sc;
2121167a3440SAlexander Motin 
2122167a3440SAlexander Motin 	new = V_carp_allow;
2123167a3440SAlexander Motin 	error = sysctl_handle_int(oidp, &new, 0, req);
2124167a3440SAlexander Motin 	if (error || !req->newptr)
2125167a3440SAlexander Motin 		return (error);
2126167a3440SAlexander Motin 
2127167a3440SAlexander Motin 	if (V_carp_allow != new) {
2128167a3440SAlexander Motin 		V_carp_allow = new;
2129167a3440SAlexander Motin 
2130167a3440SAlexander Motin 		mtx_lock(&carp_mtx);
2131167a3440SAlexander Motin 		LIST_FOREACH(sc, &carp_list, sc_next) {
2132167a3440SAlexander Motin 			CARP_LOCK(sc);
2133167a3440SAlexander Motin 			if (curvnet == sc->sc_carpdev->if_vnet)
2134167a3440SAlexander Motin 				carp_sc_state(sc);
2135167a3440SAlexander Motin 			CARP_UNLOCK(sc);
2136167a3440SAlexander Motin 		}
2137167a3440SAlexander Motin 		mtx_unlock(&carp_mtx);
2138167a3440SAlexander Motin 	}
2139167a3440SAlexander Motin 
2140167a3440SAlexander Motin 	return (0);
2141167a3440SAlexander Motin }
2142167a3440SAlexander Motin 
2143167a3440SAlexander Motin static int
21440d3d234cSKristof Provost carp_dscp_sysctl(SYSCTL_HANDLER_ARGS)
21450d3d234cSKristof Provost {
21460d3d234cSKristof Provost 	int new, error;
21470d3d234cSKristof Provost 
21480d3d234cSKristof Provost 	new = V_carp_dscp;
21490d3d234cSKristof Provost 	error = sysctl_handle_int(oidp, &new, 0, req);
21500d3d234cSKristof Provost 	if (error || !req->newptr)
21510d3d234cSKristof Provost 		return (error);
21520d3d234cSKristof Provost 
21530d3d234cSKristof Provost 	if (new < 0 || new > 63)
21540d3d234cSKristof Provost 		return (EINVAL);
21550d3d234cSKristof Provost 
21560d3d234cSKristof Provost 	V_carp_dscp = new;
21570d3d234cSKristof Provost 
21580d3d234cSKristof Provost 	return (0);
21590d3d234cSKristof Provost }
21600d3d234cSKristof Provost 
21610d3d234cSKristof Provost static int
21627951008bSGleb Smirnoff carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS)
21637951008bSGleb Smirnoff {
21647951008bSGleb Smirnoff 	int new, error;
21657951008bSGleb Smirnoff 
2166c5c392e7SMikolaj Golub 	new = V_carp_demotion;
21677951008bSGleb Smirnoff 	error = sysctl_handle_int(oidp, &new, 0, req);
21687951008bSGleb Smirnoff 	if (error || !req->newptr)
21697951008bSGleb Smirnoff 		return (error);
21707951008bSGleb Smirnoff 
21717951008bSGleb Smirnoff 	carp_demote_adj(new, "sysctl");
21727951008bSGleb Smirnoff 
21737951008bSGleb Smirnoff 	return (0);
21747951008bSGleb Smirnoff }
21757951008bSGleb Smirnoff 
217654bfbd51SWill Andrews static void
217754bfbd51SWill Andrews carp_mod_cleanup(void)
2178a9771948SGleb Smirnoff {
217954bfbd51SWill Andrews 
218054bfbd51SWill Andrews #ifdef INET
218115249f73SWill Andrews 	(void)ipproto_unregister(IPPROTO_CARP);
218254bfbd51SWill Andrews 	carp_iamatch_p = NULL;
218354bfbd51SWill Andrews #endif
218454bfbd51SWill Andrews #ifdef INET6
218515249f73SWill Andrews 	(void)ip6proto_unregister(IPPROTO_CARP);
218654bfbd51SWill Andrews 	carp_iamatch6_p = NULL;
218754bfbd51SWill Andrews 	carp_macmatch6_p = NULL;
218854bfbd51SWill Andrews #endif
218908b68b0eSGleb Smirnoff 	carp_ioctl_p = NULL;
219008b68b0eSGleb Smirnoff 	carp_attach_p = NULL;
219108b68b0eSGleb Smirnoff 	carp_detach_p = NULL;
219208b68b0eSGleb Smirnoff 	carp_get_vhid_p = NULL;
219354bfbd51SWill Andrews 	carp_linkstate_p = NULL;
219454bfbd51SWill Andrews 	carp_forus_p = NULL;
219554bfbd51SWill Andrews 	carp_output_p = NULL;
2196f08535f8SGleb Smirnoff 	carp_demote_adj_p = NULL;
219724421c1cSGleb Smirnoff 	carp_master_p = NULL;
2198f08535f8SGleb Smirnoff 	mtx_unlock(&carp_mtx);
2199f08535f8SGleb Smirnoff 	taskqueue_drain(taskqueue_swi, &carp_sendall_task);
220054bfbd51SWill Andrews 	mtx_destroy(&carp_mtx);
220193d4534cSGleb Smirnoff 	sx_destroy(&carp_sx);
220254bfbd51SWill Andrews }
220354bfbd51SWill Andrews 
2204ee49c5d3SBoris Lytochkin static void
2205ee49c5d3SBoris Lytochkin ipcarp_sysinit(void)
2206ee49c5d3SBoris Lytochkin {
2207ee49c5d3SBoris Lytochkin 
2208ee49c5d3SBoris Lytochkin 	/* Load allow as tunable so to postpone carp start after module load */
2209ee49c5d3SBoris Lytochkin 	TUNABLE_INT_FETCH("net.inet.carp.allow", &V_carp_allow);
2210ee49c5d3SBoris Lytochkin }
2211ee49c5d3SBoris Lytochkin VNET_SYSINIT(ip_carp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, ipcarp_sysinit, NULL);
2212ee49c5d3SBoris Lytochkin 
221354bfbd51SWill Andrews static int
221454bfbd51SWill Andrews carp_mod_load(void)
221554bfbd51SWill Andrews {
221615249f73SWill Andrews 	int err;
221754bfbd51SWill Andrews 
2218d92d54d5SGleb Smirnoff 	mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
221993d4534cSGleb Smirnoff 	sx_init(&carp_sx, "carp_sx");
222008b68b0eSGleb Smirnoff 	LIST_INIT(&carp_list);
222108b68b0eSGleb Smirnoff 	carp_get_vhid_p = carp_get_vhid;
222254bfbd51SWill Andrews 	carp_forus_p = carp_forus;
222354bfbd51SWill Andrews 	carp_output_p = carp_output;
222408b68b0eSGleb Smirnoff 	carp_linkstate_p = carp_linkstate;
222508b68b0eSGleb Smirnoff 	carp_ioctl_p = carp_ioctl;
222608b68b0eSGleb Smirnoff 	carp_attach_p = carp_attach;
222708b68b0eSGleb Smirnoff 	carp_detach_p = carp_detach;
2228f08535f8SGleb Smirnoff 	carp_demote_adj_p = carp_demote_adj;
222924421c1cSGleb Smirnoff 	carp_master_p = carp_master;
223054bfbd51SWill Andrews #ifdef INET6
223154bfbd51SWill Andrews 	carp_iamatch6_p = carp_iamatch6;
223254bfbd51SWill Andrews 	carp_macmatch6_p = carp_macmatch6;
223378b1fc05SGleb Smirnoff 	err = ip6proto_register(IPPROTO_CARP, carp6_input, NULL);
223415249f73SWill Andrews 	if (err) {
223515249f73SWill Andrews 		printf("carp: error %d registering with INET6\n", err);
223615249f73SWill Andrews 		carp_mod_cleanup();
22376baf7a24SGleb Smirnoff 		return (err);
223815249f73SWill Andrews 	}
223954bfbd51SWill Andrews #endif
224054bfbd51SWill Andrews #ifdef INET
224154bfbd51SWill Andrews 	carp_iamatch_p = carp_iamatch;
224278b1fc05SGleb Smirnoff 	err = ipproto_register(IPPROTO_CARP, carp_input, NULL);
224315249f73SWill Andrews 	if (err) {
224415249f73SWill Andrews 		printf("carp: error %d registering with INET\n", err);
224515249f73SWill Andrews 		carp_mod_cleanup();
22466baf7a24SGleb Smirnoff 		return (err);
224715249f73SWill Andrews 	}
224854bfbd51SWill Andrews #endif
224908b68b0eSGleb Smirnoff 	return (0);
225054bfbd51SWill Andrews }
2251a9771948SGleb Smirnoff 
225254bfbd51SWill Andrews static int
225354bfbd51SWill Andrews carp_modevent(module_t mod, int type, void *data)
225454bfbd51SWill Andrews {
225554bfbd51SWill Andrews 	switch (type) {
225654bfbd51SWill Andrews 	case MOD_LOAD:
225754bfbd51SWill Andrews 		return carp_mod_load();
225854bfbd51SWill Andrews 		/* NOTREACHED */
2259a9771948SGleb Smirnoff 	case MOD_UNLOAD:
226008b68b0eSGleb Smirnoff 		mtx_lock(&carp_mtx);
226108b68b0eSGleb Smirnoff 		if (LIST_EMPTY(&carp_list))
226254bfbd51SWill Andrews 			carp_mod_cleanup();
226308b68b0eSGleb Smirnoff 		else {
226408b68b0eSGleb Smirnoff 			mtx_unlock(&carp_mtx);
226554bfbd51SWill Andrews 			return (EBUSY);
226608b68b0eSGleb Smirnoff 		}
2267a9771948SGleb Smirnoff 		break;
2268a9771948SGleb Smirnoff 
2269a9771948SGleb Smirnoff 	default:
22700fa08018SGleb Smirnoff 		return (EINVAL);
2271a9771948SGleb Smirnoff 	}
2272a9771948SGleb Smirnoff 
22730fa08018SGleb Smirnoff 	return (0);
2274a9771948SGleb Smirnoff }
2275a9771948SGleb Smirnoff 
2276a9771948SGleb Smirnoff static moduledata_t carp_mod = {
2277a9771948SGleb Smirnoff 	"carp",
2278a9771948SGleb Smirnoff 	carp_modevent,
22799823d527SKevin Lo 	0
2280a9771948SGleb Smirnoff };
2281a9771948SGleb Smirnoff 
2282e24fa11dSWill Andrews DECLARE_MODULE(carp, carp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
2283