xref: /freebsd/sys/netinet/ip_carp.c (revision 137818006de5bb0be0b6562a0d601977f85c6867)
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>
7040e04359SKristof Provost #include <netinet/ip_carp_nl.h>
71a9771948SGleb Smirnoff #include <netinet/ip.h>
72a0ae8f04SBjoern A. Zeeb #include <machine/in_cksum.h>
73a0ae8f04SBjoern A. Zeeb #endif
74a0ae8f04SBjoern A. Zeeb #ifdef INET
75a9771948SGleb Smirnoff #include <netinet/ip_var.h>
76a9771948SGleb Smirnoff #include <netinet/if_ether.h>
77a9771948SGleb Smirnoff #endif
78a9771948SGleb Smirnoff 
79a9771948SGleb Smirnoff #ifdef INET6
80a9771948SGleb Smirnoff #include <netinet/icmp6.h>
81a9771948SGleb Smirnoff #include <netinet/ip6.h>
8208b68b0eSGleb Smirnoff #include <netinet6/in6_var.h>
83a9771948SGleb Smirnoff #include <netinet6/ip6_var.h>
8429da8af6SHajimu UMEMOTO #include <netinet6/scope6_var.h>
85a9771948SGleb Smirnoff #include <netinet6/nd6.h>
86a9771948SGleb Smirnoff #endif
87a9771948SGleb Smirnoff 
8840e04359SKristof Provost #include <netlink/netlink.h>
8940e04359SKristof Provost #include <netlink/netlink_ctl.h>
9040e04359SKristof Provost #include <netlink/netlink_generic.h>
9140e04359SKristof Provost #include <netlink/netlink_message_parser.h>
9240e04359SKristof Provost 
93a9771948SGleb Smirnoff #include <crypto/sha1.h>
94a9771948SGleb Smirnoff 
9508b68b0eSGleb Smirnoff static MALLOC_DEFINE(M_CARP, "CARP", "CARP addresses");
96a9771948SGleb Smirnoff 
97a9771948SGleb Smirnoff struct carp_softc {
9808b68b0eSGleb Smirnoff 	struct ifnet		*sc_carpdev;	/* Pointer to parent ifnet. */
9908b68b0eSGleb Smirnoff 	struct ifaddr		**sc_ifas;	/* Our ifaddrs. */
10008b68b0eSGleb Smirnoff 	struct sockaddr_dl	sc_addr;	/* Our link level address. */
10108b68b0eSGleb Smirnoff 	struct callout		sc_ad_tmo;	/* Advertising timeout. */
102a0ae8f04SBjoern A. Zeeb #ifdef INET
10308b68b0eSGleb Smirnoff 	struct callout		sc_md_tmo;	/* Master down timeout. */
104a0ae8f04SBjoern A. Zeeb #endif
105a9771948SGleb Smirnoff #ifdef INET6
10608b68b0eSGleb Smirnoff 	struct callout 		sc_md6_tmo;	/* XXX: Master down timeout. */
10708b68b0eSGleb Smirnoff #endif
10808b68b0eSGleb Smirnoff 	struct mtx		sc_mtx;
109a9771948SGleb Smirnoff 
11008b68b0eSGleb Smirnoff 	int			sc_vhid;
11108b68b0eSGleb Smirnoff 	int			sc_advskew;
11208b68b0eSGleb Smirnoff 	int			sc_advbase;
113*13781800SKristof Provost 	struct in_addr		sc_carpaddr;
114*13781800SKristof Provost 	struct in6_addr		sc_carpaddr6;
11508b68b0eSGleb Smirnoff 
11608b68b0eSGleb Smirnoff 	int			sc_naddrs;
11708b68b0eSGleb Smirnoff 	int			sc_naddrs6;
11808b68b0eSGleb Smirnoff 	int			sc_ifasiz;
119a9771948SGleb Smirnoff 	enum { INIT = 0, BACKUP, MASTER }	sc_state;
120a9771948SGleb Smirnoff 	int			sc_suppress;
121a9771948SGleb Smirnoff 	int			sc_sendad_errors;
122a9771948SGleb Smirnoff #define	CARP_SENDAD_MAX_ERRORS	3
123a9771948SGleb Smirnoff 	int			sc_sendad_success;
124a9771948SGleb Smirnoff #define	CARP_SENDAD_MIN_SUCCESS 3
125a9771948SGleb Smirnoff 
126a9771948SGleb Smirnoff 	int			sc_init_counter;
12708b68b0eSGleb Smirnoff 	uint64_t		sc_counter;
128a9771948SGleb Smirnoff 
129a9771948SGleb Smirnoff 	/* authentication */
130a9771948SGleb Smirnoff #define	CARP_HMAC_PAD	64
131a9771948SGleb Smirnoff 	unsigned char sc_key[CARP_KEY_LEN];
132a9771948SGleb Smirnoff 	unsigned char sc_pad[CARP_HMAC_PAD];
133a9771948SGleb Smirnoff 	SHA1_CTX sc_sha1;
134a9771948SGleb Smirnoff 
13508b68b0eSGleb Smirnoff 	TAILQ_ENTRY(carp_softc)	sc_list;	/* On the carp_if list. */
13608b68b0eSGleb Smirnoff 	LIST_ENTRY(carp_softc)	sc_next;	/* On the global list. */
137a9771948SGleb Smirnoff };
13808b68b0eSGleb Smirnoff 
13908b68b0eSGleb Smirnoff struct carp_if {
14008b68b0eSGleb Smirnoff #ifdef INET
14108b68b0eSGleb Smirnoff 	int	cif_naddrs;
14208b68b0eSGleb Smirnoff #endif
14308b68b0eSGleb Smirnoff #ifdef INET6
14408b68b0eSGleb Smirnoff 	int	cif_naddrs6;
14508b68b0eSGleb Smirnoff #endif
14608b68b0eSGleb Smirnoff 	TAILQ_HEAD(, carp_softc) cif_vrs;
14708b68b0eSGleb Smirnoff #ifdef INET
14808b68b0eSGleb Smirnoff 	struct ip_moptions 	 cif_imo;
14908b68b0eSGleb Smirnoff #endif
15008b68b0eSGleb Smirnoff #ifdef INET6
15108b68b0eSGleb Smirnoff 	struct ip6_moptions 	 cif_im6o;
15208b68b0eSGleb Smirnoff #endif
15308b68b0eSGleb Smirnoff 	struct ifnet	*cif_ifp;
15408b68b0eSGleb Smirnoff 	struct mtx	cif_mtx;
1550cc726f2SGleb Smirnoff 	uint32_t	cif_flags;
1560cc726f2SGleb Smirnoff #define	CIF_PROMISC	0x00000001
15708b68b0eSGleb Smirnoff };
15808b68b0eSGleb Smirnoff 
159*13781800SKristof Provost /* Kernel equivalent of struct carpreq, but with more fields for new features.
160*13781800SKristof Provost  * */
161*13781800SKristof Provost struct carpkreq {
162*13781800SKristof Provost 	int		carpr_count;
163*13781800SKristof Provost 	int		carpr_vhid;
164*13781800SKristof Provost 	int		carpr_state;
165*13781800SKristof Provost 	int		carpr_advskew;
166*13781800SKristof Provost 	int		carpr_advbase;
167*13781800SKristof Provost 	unsigned char	carpr_key[CARP_KEY_LEN];
168*13781800SKristof Provost 	/* Everything above this is identical to carpreq */
169*13781800SKristof Provost 	struct in_addr	carpr_addr;
170*13781800SKristof Provost 	struct in6_addr	carpr_addr6;
171*13781800SKristof Provost };
172*13781800SKristof Provost 
17308b68b0eSGleb Smirnoff /*
17408b68b0eSGleb Smirnoff  * Brief design of carp(4).
17508b68b0eSGleb Smirnoff  *
17608b68b0eSGleb Smirnoff  * Any carp-capable ifnet may have a list of carp softcs hanging off
17708b68b0eSGleb Smirnoff  * its ifp->if_carp pointer. Each softc represents one unique virtual
17808b68b0eSGleb Smirnoff  * host id, or vhid. The softc has a back pointer to the ifnet. All
17908b68b0eSGleb Smirnoff  * softcs are joined in a global list, which has quite limited use.
18008b68b0eSGleb Smirnoff  *
18108b68b0eSGleb Smirnoff  * Any interface address that takes part in CARP negotiation has a
18208b68b0eSGleb Smirnoff  * pointer to the softc of its vhid, ifa->ifa_carp. That could be either
18308b68b0eSGleb Smirnoff  * AF_INET or AF_INET6 address.
18408b68b0eSGleb Smirnoff  *
18508b68b0eSGleb Smirnoff  * Although, one can get the softc's backpointer to ifnet and traverse
18608b68b0eSGleb Smirnoff  * through its ifp->if_addrhead queue to find all interface addresses
18708b68b0eSGleb Smirnoff  * involved in CARP, we keep a growable array of ifaddr pointers. This
18808b68b0eSGleb Smirnoff  * allows us to avoid grabbing the IF_ADDR_LOCK() in many traversals that
18908b68b0eSGleb Smirnoff  * do calls into the network stack, thus avoiding LORs.
19008b68b0eSGleb Smirnoff  *
19108b68b0eSGleb Smirnoff  * Locking:
19208b68b0eSGleb Smirnoff  *
19308b68b0eSGleb Smirnoff  * Each softc has a lock sc_mtx. It is used to synchronise carp_input_c(),
19408b68b0eSGleb Smirnoff  * callout-driven events and ioctl()s.
19508b68b0eSGleb Smirnoff  *
19681098a01SAlexander Motin  * To traverse the list of softcs on an ifnet we use CIF_LOCK() or carp_sx.
19781098a01SAlexander Motin  * To traverse the global list we use the mutex carp_mtx.
19808b68b0eSGleb Smirnoff  *
19908b68b0eSGleb Smirnoff  * Known issues with locking:
20008b68b0eSGleb Smirnoff  *
20108b68b0eSGleb Smirnoff  * - Sending ad, we put the pointer to the softc in an mtag, and no reference
20208b68b0eSGleb Smirnoff  *   counting is done on the softc.
20308b68b0eSGleb Smirnoff  * - On module unload we may race (?) with packet processing thread
20408b68b0eSGleb Smirnoff  *   dereferencing our function pointers.
20508b68b0eSGleb Smirnoff  */
206a9771948SGleb Smirnoff 
207c5c392e7SMikolaj Golub /* Accept incoming CARP packets. */
2085f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_allow) = 1;
209c5c392e7SMikolaj Golub #define	V_carp_allow	VNET(carp_allow)
210c5c392e7SMikolaj Golub 
2110d3d234cSKristof Provost /* Set DSCP in outgoing CARP packets. */
2125f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_dscp) = 56;
2130d3d234cSKristof Provost #define	V_carp_dscp	VNET(carp_dscp)
2140d3d234cSKristof Provost 
215c5c392e7SMikolaj Golub /* Preempt slower nodes. */
2165f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_preempt) = 0;
217c5c392e7SMikolaj Golub #define	V_carp_preempt	VNET(carp_preempt)
218c5c392e7SMikolaj Golub 
219c5c392e7SMikolaj Golub /* Log level. */
2205f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_log) = 1;
221c5c392e7SMikolaj Golub #define	V_carp_log	VNET(carp_log)
222c5c392e7SMikolaj Golub 
223c5c392e7SMikolaj Golub /* Global advskew demotion. */
2245f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_demotion) = 0;
225c5c392e7SMikolaj Golub #define	V_carp_demotion	VNET(carp_demotion)
226c5c392e7SMikolaj Golub 
227c5c392e7SMikolaj Golub /* Send error demotion factor. */
2285f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_senderr_adj) = CARP_MAXSKEW;
229c5c392e7SMikolaj Golub #define	V_carp_senderr_adj	VNET(carp_senderr_adj)
230c5c392e7SMikolaj Golub 
231c5c392e7SMikolaj Golub /* Iface down demotion factor. */
2325f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_ifdown_adj) = CARP_MAXSKEW;
233c5c392e7SMikolaj Golub #define	V_carp_ifdown_adj	VNET(carp_ifdown_adj)
234c5c392e7SMikolaj Golub 
235167a3440SAlexander Motin static int carp_allow_sysctl(SYSCTL_HANDLER_ARGS);
2360d3d234cSKristof Provost static int carp_dscp_sysctl(SYSCTL_HANDLER_ARGS);
2377951008bSGleb Smirnoff static int carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS);
238a9771948SGleb Smirnoff 
23910b49b23SPawel Biernacki SYSCTL_NODE(_net_inet, IPPROTO_CARP, carp, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
24010b49b23SPawel Biernacki     "CARP");
241167a3440SAlexander Motin SYSCTL_PROC(_net_inet_carp, OID_AUTO, allow,
242ee49c5d3SBoris Lytochkin     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
243ee49c5d3SBoris Lytochkin     &VNET_NAME(carp_allow), 0, carp_allow_sysctl, "I",
244167a3440SAlexander Motin     "Accept incoming CARP packets");
2450d3d234cSKristof Provost SYSCTL_PROC(_net_inet_carp, OID_AUTO, dscp,
24610b49b23SPawel Biernacki     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
24710b49b23SPawel Biernacki     0, 0, carp_dscp_sysctl, "I",
2480d3d234cSKristof Provost     "DSCP value for carp packets");
2496df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, preempt, CTLFLAG_VNET | CTLFLAG_RW,
250c5c392e7SMikolaj Golub     &VNET_NAME(carp_preempt), 0, "High-priority backup preemption mode");
2516df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, log, CTLFLAG_VNET | CTLFLAG_RW,
252c5c392e7SMikolaj Golub     &VNET_NAME(carp_log), 0, "CARP log level");
2536df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_carp, OID_AUTO, demotion,
25410b49b23SPawel Biernacki     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
2557951008bSGleb Smirnoff     0, 0, carp_demote_adj_sysctl, "I",
2567951008bSGleb Smirnoff     "Adjust demotion factor (skew of advskew)");
2576df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, senderr_demotion_factor,
2586df8a710SGleb Smirnoff     CTLFLAG_VNET | CTLFLAG_RW,
259c5c392e7SMikolaj Golub     &VNET_NAME(carp_senderr_adj), 0, "Send error demotion factor adjustment");
2606df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, ifdown_demotion_factor,
2616df8a710SGleb Smirnoff     CTLFLAG_VNET | CTLFLAG_RW,
262c5c392e7SMikolaj Golub     &VNET_NAME(carp_ifdown_adj), 0,
263c5c392e7SMikolaj Golub     "Interface down demotion factor adjustment");
264f08535f8SGleb Smirnoff 
265c5c392e7SMikolaj Golub VNET_PCPUSTAT_DEFINE(struct carpstats, carpstats);
266c5c392e7SMikolaj Golub VNET_PCPUSTAT_SYSINIT(carpstats);
267c5c392e7SMikolaj Golub VNET_PCPUSTAT_SYSUNINIT(carpstats);
268c5c392e7SMikolaj Golub 
26969edf037SAndrey V. Elsukov #define	CARPSTATS_ADD(name, val)	\
270c5c392e7SMikolaj Golub     counter_u64_add(VNET(carpstats)[offsetof(struct carpstats, name) / \
27169edf037SAndrey V. Elsukov 	sizeof(uint64_t)], (val))
27269edf037SAndrey V. Elsukov #define	CARPSTATS_INC(name)		CARPSTATS_ADD(name, 1)
27369edf037SAndrey V. Elsukov 
274c5c392e7SMikolaj Golub SYSCTL_VNET_PCPUSTAT(_net_inet_carp, OID_AUTO, stats, struct carpstats,
275c5c392e7SMikolaj Golub     carpstats, "CARP statistics (struct carpstats, netinet/ip_carp.h)");
276a9771948SGleb Smirnoff 
27708b68b0eSGleb Smirnoff #define	CARP_LOCK_INIT(sc)	mtx_init(&(sc)->sc_mtx, "carp_softc",   \
278a9771948SGleb Smirnoff 	NULL, MTX_DEF)
27908b68b0eSGleb Smirnoff #define	CARP_LOCK_DESTROY(sc)	mtx_destroy(&(sc)->sc_mtx)
28008b68b0eSGleb Smirnoff #define	CARP_LOCK_ASSERT(sc)	mtx_assert(&(sc)->sc_mtx, MA_OWNED)
28108b68b0eSGleb Smirnoff #define	CARP_LOCK(sc)		mtx_lock(&(sc)->sc_mtx)
28208b68b0eSGleb Smirnoff #define	CARP_UNLOCK(sc)		mtx_unlock(&(sc)->sc_mtx)
28308b68b0eSGleb Smirnoff #define	CIF_LOCK_INIT(cif)	mtx_init(&(cif)->cif_mtx, "carp_if",   \
28408b68b0eSGleb Smirnoff 	NULL, MTX_DEF)
28508b68b0eSGleb Smirnoff #define	CIF_LOCK_DESTROY(cif)	mtx_destroy(&(cif)->cif_mtx)
28608b68b0eSGleb Smirnoff #define	CIF_LOCK_ASSERT(cif)	mtx_assert(&(cif)->cif_mtx, MA_OWNED)
28708b68b0eSGleb Smirnoff #define	CIF_LOCK(cif)		mtx_lock(&(cif)->cif_mtx)
28808b68b0eSGleb Smirnoff #define	CIF_UNLOCK(cif)		mtx_unlock(&(cif)->cif_mtx)
289a9a2c40cSGleb Smirnoff #define	CIF_FREE(cif)	do {				\
2909c2cd1aaSGleb Smirnoff 		CIF_LOCK(cif);				\
291a9a2c40cSGleb Smirnoff 		if (TAILQ_EMPTY(&(cif)->cif_vrs))	\
292a9a2c40cSGleb Smirnoff 			carp_free_if(cif);		\
293a9a2c40cSGleb Smirnoff 		else					\
294a9a2c40cSGleb Smirnoff 			CIF_UNLOCK(cif);		\
295a9a2c40cSGleb Smirnoff } while (0)
296d220759bSGleb Smirnoff 
297947b7cf3SGleb Smirnoff #define	CARP_LOG(...)	do {				\
298c5c392e7SMikolaj Golub 	if (V_carp_log > 0)				\
29908b68b0eSGleb Smirnoff 		log(LOG_INFO, "carp: " __VA_ARGS__);	\
300947b7cf3SGleb Smirnoff } while (0)
3011e9e6572SGleb Smirnoff 
302947b7cf3SGleb Smirnoff #define	CARP_DEBUG(...)	do {				\
303c5c392e7SMikolaj Golub 	if (V_carp_log > 1)				\
3041e9e6572SGleb Smirnoff 		log(LOG_DEBUG, __VA_ARGS__);		\
305947b7cf3SGleb Smirnoff } while (0)
306a9771948SGleb Smirnoff 
30708b68b0eSGleb Smirnoff #define	IFNET_FOREACH_IFA(ifp, ifa)					\
308d7c5a620SMatt Macy 	CK_STAILQ_FOREACH((ifa), &(ifp)->if_addrhead, ifa_link)	\
30908b68b0eSGleb Smirnoff 		if ((ifa)->ifa_carp != NULL)
31008b68b0eSGleb Smirnoff 
31108b68b0eSGleb Smirnoff #define	CARP_FOREACH_IFA(sc, ifa)					\
31208b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);						\
31308b68b0eSGleb Smirnoff 	for (int _i = 0;						\
31408b68b0eSGleb Smirnoff 		_i < (sc)->sc_naddrs + (sc)->sc_naddrs6 &&		\
31508b68b0eSGleb Smirnoff 		((ifa) = sc->sc_ifas[_i]) != NULL;			\
31608b68b0eSGleb Smirnoff 		++_i)
31708b68b0eSGleb Smirnoff 
31808b68b0eSGleb Smirnoff #define	IFNET_FOREACH_CARP(ifp, sc)					\
31981098a01SAlexander Motin 	KASSERT(mtx_owned(&ifp->if_carp->cif_mtx) ||			\
32081098a01SAlexander Motin 	    sx_xlocked(&carp_sx), ("cif_vrs not locked"));		\
32108b68b0eSGleb Smirnoff 	TAILQ_FOREACH((sc), &(ifp)->if_carp->cif_vrs, sc_list)
32208b68b0eSGleb Smirnoff 
323f08535f8SGleb Smirnoff #define	DEMOTE_ADVSKEW(sc)					\
324c5c392e7SMikolaj Golub     (((sc)->sc_advskew + V_carp_demotion > CARP_MAXSKEW) ?	\
3251019354bSMarius Halden     CARP_MAXSKEW :						\
3261019354bSMarius Halden         (((sc)->sc_advskew + V_carp_demotion < 0) ?		\
3271019354bSMarius Halden         0 : ((sc)->sc_advskew + V_carp_demotion)))
328f08535f8SGleb Smirnoff 
329*13781800SKristof Provost static void	carp_input_c(struct mbuf *, struct carp_header *, sa_family_t, int);
33008b68b0eSGleb Smirnoff static struct carp_softc
33108b68b0eSGleb Smirnoff 		*carp_alloc(struct ifnet *);
33208b68b0eSGleb Smirnoff static void	carp_destroy(struct carp_softc *);
33308b68b0eSGleb Smirnoff static struct carp_if
33408b68b0eSGleb Smirnoff 		*carp_alloc_if(struct ifnet *);
33508b68b0eSGleb Smirnoff static void	carp_free_if(struct carp_if *);
336d01641e2SWill Andrews static void	carp_set_state(struct carp_softc *, int, const char* reason);
33708b68b0eSGleb Smirnoff static void	carp_sc_state(struct carp_softc *);
33808b68b0eSGleb Smirnoff static void	carp_setrun(struct carp_softc *, sa_family_t);
3395c1f0f6dSGleb Smirnoff static void	carp_master_down(void *);
340d01641e2SWill Andrews static void	carp_master_down_locked(struct carp_softc *,
341d01641e2SWill Andrews     		    const char* reason);
34208b68b0eSGleb Smirnoff static void	carp_send_ad(void *);
34308b68b0eSGleb Smirnoff static void	carp_send_ad_locked(struct carp_softc *);
34408b68b0eSGleb Smirnoff static void	carp_addroute(struct carp_softc *);
3452512b096SGleb Smirnoff static void	carp_ifa_addroute(struct ifaddr *);
34608b68b0eSGleb Smirnoff static void	carp_delroute(struct carp_softc *);
3472512b096SGleb Smirnoff static void	carp_ifa_delroute(struct ifaddr *);
348f08535f8SGleb Smirnoff static void	carp_send_ad_all(void *, int);
349f08535f8SGleb Smirnoff static void	carp_demote_adj(int, char *);
350a9771948SGleb Smirnoff 
35108b68b0eSGleb Smirnoff static LIST_HEAD(, carp_softc) carp_list;
352d92d54d5SGleb Smirnoff static struct mtx carp_mtx;
35393d4534cSGleb Smirnoff static struct sx carp_sx;
354f08535f8SGleb Smirnoff static struct task carp_sendall_task =
355f08535f8SGleb Smirnoff     TASK_INITIALIZER(0, carp_send_ad_all, NULL);
356a9771948SGleb Smirnoff 
35740e04359SKristof Provost static int
35840e04359SKristof Provost carp_is_supported_if(if_t ifp)
35940e04359SKristof Provost {
36040e04359SKristof Provost 	if (ifp == NULL)
36140e04359SKristof Provost 		return (ENXIO);
36240e04359SKristof Provost 
36340e04359SKristof Provost 	switch (ifp->if_type) {
36440e04359SKristof Provost 	case IFT_ETHER:
36540e04359SKristof Provost 	case IFT_L2VLAN:
36640e04359SKristof Provost 	case IFT_BRIDGE:
36740e04359SKristof Provost 		break;
36840e04359SKristof Provost 	default:
36940e04359SKristof Provost 		return (EOPNOTSUPP);
37040e04359SKristof Provost 	}
37140e04359SKristof Provost 
37240e04359SKristof Provost 	return (0);
37340e04359SKristof Provost }
37440e04359SKristof Provost 
3755c1f0f6dSGleb Smirnoff static void
376a9771948SGleb Smirnoff carp_hmac_prepare(struct carp_softc *sc)
377a9771948SGleb Smirnoff {
37808b68b0eSGleb Smirnoff 	uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
37908b68b0eSGleb Smirnoff 	uint8_t vhid = sc->sc_vhid & 0xff;
380a9771948SGleb Smirnoff 	struct ifaddr *ifa;
3811ead26d4SMax Laier 	int i, found;
3821ead26d4SMax Laier #ifdef INET
3831ead26d4SMax Laier 	struct in_addr last, cur, in;
3841ead26d4SMax Laier #endif
385a9771948SGleb Smirnoff #ifdef INET6
3861ead26d4SMax Laier 	struct in6_addr last6, cur6, in6;
387a9771948SGleb Smirnoff #endif
388a9771948SGleb Smirnoff 
38908b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
390d220759bSGleb Smirnoff 
39108b68b0eSGleb Smirnoff 	/* Compute ipad from key. */
392a9771948SGleb Smirnoff 	bzero(sc->sc_pad, sizeof(sc->sc_pad));
393a9771948SGleb Smirnoff 	bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
394a9771948SGleb Smirnoff 	for (i = 0; i < sizeof(sc->sc_pad); i++)
395a9771948SGleb Smirnoff 		sc->sc_pad[i] ^= 0x36;
396a9771948SGleb Smirnoff 
39708b68b0eSGleb Smirnoff 	/* Precompute first part of inner hash. */
398a9771948SGleb Smirnoff 	SHA1Init(&sc->sc_sha1);
399a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
400a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
401a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
402a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
403a9771948SGleb Smirnoff #ifdef INET
4041ead26d4SMax Laier 	cur.s_addr = 0;
4051ead26d4SMax Laier 	do {
4061ead26d4SMax Laier 		found = 0;
4071ead26d4SMax Laier 		last = cur;
4081ead26d4SMax Laier 		cur.s_addr = 0xffffffff;
40908b68b0eSGleb Smirnoff 		CARP_FOREACH_IFA(sc, ifa) {
4101ead26d4SMax Laier 			in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr;
4111ead26d4SMax Laier 			if (ifa->ifa_addr->sa_family == AF_INET &&
4121ead26d4SMax Laier 			    ntohl(in.s_addr) > ntohl(last.s_addr) &&
4131ead26d4SMax Laier 			    ntohl(in.s_addr) < ntohl(cur.s_addr)) {
4141ead26d4SMax Laier 				cur.s_addr = in.s_addr;
4151ead26d4SMax Laier 				found++;
416a9771948SGleb Smirnoff 			}
4171ead26d4SMax Laier 		}
4181ead26d4SMax Laier 		if (found)
4191ead26d4SMax Laier 			SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur));
4201ead26d4SMax Laier 	} while (found);
421a9771948SGleb Smirnoff #endif /* INET */
422a9771948SGleb Smirnoff #ifdef INET6
4231ead26d4SMax Laier 	memset(&cur6, 0, sizeof(cur6));
4241ead26d4SMax Laier 	do {
4251ead26d4SMax Laier 		found = 0;
4261ead26d4SMax Laier 		last6 = cur6;
4271ead26d4SMax Laier 		memset(&cur6, 0xff, sizeof(cur6));
42808b68b0eSGleb Smirnoff 		CARP_FOREACH_IFA(sc, ifa) {
429a9771948SGleb Smirnoff 			in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
4301ead26d4SMax Laier 			if (IN6_IS_SCOPE_EMBED(&in6))
4311ead26d4SMax Laier 				in6.s6_addr16[1] = 0;
4321ead26d4SMax Laier 			if (ifa->ifa_addr->sa_family == AF_INET6 &&
4331ead26d4SMax Laier 			    memcmp(&in6, &last6, sizeof(in6)) > 0 &&
4341ead26d4SMax Laier 			    memcmp(&in6, &cur6, sizeof(in6)) < 0) {
4351ead26d4SMax Laier 				cur6 = in6;
4361ead26d4SMax Laier 				found++;
437a9771948SGleb Smirnoff 			}
438a9771948SGleb Smirnoff 		}
4391ead26d4SMax Laier 		if (found)
4401ead26d4SMax Laier 			SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6));
4411ead26d4SMax Laier 	} while (found);
442a9771948SGleb Smirnoff #endif /* INET6 */
443a9771948SGleb Smirnoff 
444a9771948SGleb Smirnoff 	/* convert ipad to opad */
445a9771948SGleb Smirnoff 	for (i = 0; i < sizeof(sc->sc_pad); i++)
446a9771948SGleb Smirnoff 		sc->sc_pad[i] ^= 0x36 ^ 0x5c;
447a9771948SGleb Smirnoff }
448a9771948SGleb Smirnoff 
4495c1f0f6dSGleb Smirnoff static void
45008b68b0eSGleb Smirnoff carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
451a9771948SGleb Smirnoff     unsigned char md[20])
452a9771948SGleb Smirnoff {
453a9771948SGleb Smirnoff 	SHA1_CTX sha1ctx;
454a9771948SGleb Smirnoff 
45508b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
45608b68b0eSGleb Smirnoff 
457a9771948SGleb Smirnoff 	/* fetch first half of inner hash */
458a9771948SGleb Smirnoff 	bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
459a9771948SGleb Smirnoff 
460a9771948SGleb Smirnoff 	SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
461a9771948SGleb Smirnoff 	SHA1Final(md, &sha1ctx);
462a9771948SGleb Smirnoff 
463a9771948SGleb Smirnoff 	/* outer hash */
464a9771948SGleb Smirnoff 	SHA1Init(&sha1ctx);
465a9771948SGleb Smirnoff 	SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
466a9771948SGleb Smirnoff 	SHA1Update(&sha1ctx, md, 20);
467a9771948SGleb Smirnoff 	SHA1Final(md, &sha1ctx);
468a9771948SGleb Smirnoff }
469a9771948SGleb Smirnoff 
4705c1f0f6dSGleb Smirnoff static int
47108b68b0eSGleb Smirnoff carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
472a9771948SGleb Smirnoff     unsigned char md[20])
473a9771948SGleb Smirnoff {
474a9771948SGleb Smirnoff 	unsigned char md2[20];
475a9771948SGleb Smirnoff 
47608b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
477d220759bSGleb Smirnoff 
478a9771948SGleb Smirnoff 	carp_hmac_generate(sc, counter, md2);
479a9771948SGleb Smirnoff 
480a9771948SGleb Smirnoff 	return (bcmp(md, md2, sizeof(md2)));
481a9771948SGleb Smirnoff }
482a9771948SGleb Smirnoff 
483a9771948SGleb Smirnoff /*
484a9771948SGleb Smirnoff  * process input packet.
485a9771948SGleb Smirnoff  * we have rearranged checks order compared to the rfc,
486a9771948SGleb Smirnoff  * but it seems more efficient this way or not possible otherwise.
487a9771948SGleb Smirnoff  */
488a0ae8f04SBjoern A. Zeeb #ifdef INET
48978b1fc05SGleb Smirnoff static int
4908f5a8818SKevin Lo carp_input(struct mbuf **mp, int *offp, int proto)
491a9771948SGleb Smirnoff {
4928f5a8818SKevin Lo 	struct mbuf *m = *mp;
493a9771948SGleb Smirnoff 	struct ip *ip = mtod(m, struct ip *);
494a9771948SGleb Smirnoff 	struct carp_header *ch;
495a9771948SGleb Smirnoff 	int iplen, len;
496a9771948SGleb Smirnoff 
4978f5a8818SKevin Lo 	iplen = *offp;
4988f5a8818SKevin Lo 	*mp = NULL;
4998f5a8818SKevin Lo 
5006bf65bcfSRobert Watson 	CARPSTATS_INC(carps_ipackets);
501a9771948SGleb Smirnoff 
502c5c392e7SMikolaj Golub 	if (!V_carp_allow) {
503a9771948SGleb Smirnoff 		m_freem(m);
5048f5a8818SKevin Lo 		return (IPPROTO_DONE);
505a9771948SGleb Smirnoff 	}
506a9771948SGleb Smirnoff 
507a9771948SGleb Smirnoff 	iplen = ip->ip_hl << 2;
508a9771948SGleb Smirnoff 
509a9771948SGleb Smirnoff 	if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
5106bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badlen);
51108b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: received len %zd < sizeof(struct carp_header) "
51208b68b0eSGleb Smirnoff 		    "on %s\n", __func__, m->m_len - sizeof(struct ip),
51316e324fcSXin LI 		    m->m_pkthdr.rcvif->if_xname);
514a9771948SGleb Smirnoff 		m_freem(m);
5158f5a8818SKevin Lo 		return (IPPROTO_DONE);
516a9771948SGleb Smirnoff 	}
517a9771948SGleb Smirnoff 
518a9771948SGleb Smirnoff 	if (iplen + sizeof(*ch) < m->m_len) {
519a9771948SGleb Smirnoff 		if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
5206bf65bcfSRobert Watson 			CARPSTATS_INC(carps_hdrops);
52108b68b0eSGleb Smirnoff 			CARP_DEBUG("%s: pullup failed\n", __func__);
5228f5a8818SKevin Lo 			return (IPPROTO_DONE);
523a9771948SGleb Smirnoff 		}
524a9771948SGleb Smirnoff 		ip = mtod(m, struct ip *);
525a9771948SGleb Smirnoff 	}
526a9771948SGleb Smirnoff 	ch = (struct carp_header *)((char *)ip + iplen);
527a9771948SGleb Smirnoff 
528a9771948SGleb Smirnoff 	/*
529a9771948SGleb Smirnoff 	 * verify that the received packet length is
530a9771948SGleb Smirnoff 	 * equal to the CARP header
531a9771948SGleb Smirnoff 	 */
532a9771948SGleb Smirnoff 	len = iplen + sizeof(*ch);
533a9771948SGleb Smirnoff 	if (len > m->m_pkthdr.len) {
5346bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badlen);
53508b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: packet too short %d on %s\n", __func__,
5361e9e6572SGleb Smirnoff 		    m->m_pkthdr.len,
5371e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
538a9771948SGleb Smirnoff 		m_freem(m);
5398f5a8818SKevin Lo 		return (IPPROTO_DONE);
540a9771948SGleb Smirnoff 	}
541a9771948SGleb Smirnoff 
542a9771948SGleb Smirnoff 	if ((m = m_pullup(m, len)) == NULL) {
5436bf65bcfSRobert Watson 		CARPSTATS_INC(carps_hdrops);
5448f5a8818SKevin Lo 		return (IPPROTO_DONE);
545a9771948SGleb Smirnoff 	}
546a9771948SGleb Smirnoff 	ip = mtod(m, struct ip *);
547a9771948SGleb Smirnoff 	ch = (struct carp_header *)((char *)ip + iplen);
548a9771948SGleb Smirnoff 
549a9771948SGleb Smirnoff 	/* verify the CARP checksum */
550a9771948SGleb Smirnoff 	m->m_data += iplen;
551c4d06976SGleb Smirnoff 	if (in_cksum(m, len - iplen)) {
5526bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badsum);
55308b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: checksum failed on %s\n", __func__,
5541e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
555a9771948SGleb Smirnoff 		m_freem(m);
5568f5a8818SKevin Lo 		return (IPPROTO_DONE);
557a9771948SGleb Smirnoff 	}
558a9771948SGleb Smirnoff 	m->m_data -= iplen;
559a9771948SGleb Smirnoff 
560*13781800SKristof Provost 	carp_input_c(m, ch, AF_INET, ip->ip_ttl);
5618f5a8818SKevin Lo 	return (IPPROTO_DONE);
562a9771948SGleb Smirnoff }
563a0ae8f04SBjoern A. Zeeb #endif
564a9771948SGleb Smirnoff 
565a9771948SGleb Smirnoff #ifdef INET6
56678b1fc05SGleb Smirnoff static int
567a9771948SGleb Smirnoff carp6_input(struct mbuf **mp, int *offp, int proto)
568a9771948SGleb Smirnoff {
569a9771948SGleb Smirnoff 	struct mbuf *m = *mp;
570a9771948SGleb Smirnoff 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
571a9771948SGleb Smirnoff 	struct carp_header *ch;
572a9771948SGleb Smirnoff 	u_int len;
573a9771948SGleb Smirnoff 
5746bf65bcfSRobert Watson 	CARPSTATS_INC(carps_ipackets6);
575a9771948SGleb Smirnoff 
576c5c392e7SMikolaj Golub 	if (!V_carp_allow) {
577a9771948SGleb Smirnoff 		m_freem(m);
578a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
579a9771948SGleb Smirnoff 	}
580a9771948SGleb Smirnoff 
581a9771948SGleb Smirnoff 	/* check if received on a valid carp interface */
582a9771948SGleb Smirnoff 	if (m->m_pkthdr.rcvif->if_carp == NULL) {
5836bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badif);
58408b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: packet received on non-carp interface: %s\n",
58508b68b0eSGleb Smirnoff 		    __func__, m->m_pkthdr.rcvif->if_xname);
586a9771948SGleb Smirnoff 		m_freem(m);
587a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
588a9771948SGleb Smirnoff 	}
589a9771948SGleb Smirnoff 
590a9771948SGleb Smirnoff 	/* verify that we have a complete carp packet */
591a4adf6ccSBjoern A. Zeeb 	if (m->m_len < *offp + sizeof(*ch)) {
592a9771948SGleb Smirnoff 		len = m->m_len;
59363abacc2SBjoern A. Zeeb 		m = m_pullup(m, *offp + sizeof(*ch));
59463abacc2SBjoern A. Zeeb 		if (m == NULL) {
5956bf65bcfSRobert Watson 			CARPSTATS_INC(carps_badlen);
59608b68b0eSGleb Smirnoff 			CARP_DEBUG("%s: packet size %u too small\n", __func__, len);
597a9771948SGleb Smirnoff 			return (IPPROTO_DONE);
598a9771948SGleb Smirnoff 		}
599*13781800SKristof Provost 		ip6 = mtod(m, struct ip6_hdr *);
600a4adf6ccSBjoern A. Zeeb 	}
601dad68fc3SBjoern A. Zeeb 	ch = (struct carp_header *)(mtod(m, char *) + *offp);
602a9771948SGleb Smirnoff 
603a9771948SGleb Smirnoff 	/* verify the CARP checksum */
604a9771948SGleb Smirnoff 	m->m_data += *offp;
605c4d06976SGleb Smirnoff 	if (in_cksum(m, sizeof(*ch))) {
6066bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badsum);
60708b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: checksum failed, on %s\n", __func__,
6081e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
609a9771948SGleb Smirnoff 		m_freem(m);
610a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
611a9771948SGleb Smirnoff 	}
612a9771948SGleb Smirnoff 	m->m_data -= *offp;
613a9771948SGleb Smirnoff 
614*13781800SKristof Provost 	carp_input_c(m, ch, AF_INET6, ip6->ip6_hlim);
615a9771948SGleb Smirnoff 	return (IPPROTO_DONE);
616a9771948SGleb Smirnoff }
617a9771948SGleb Smirnoff #endif /* INET6 */
618a9771948SGleb Smirnoff 
6198151740cSJosh Paetzel /*
6208151740cSJosh Paetzel  * This routine should not be necessary at all, but some switches
6218151740cSJosh Paetzel  * (VMWare ESX vswitches) can echo our own packets back at us,
6228151740cSJosh Paetzel  * and we must ignore them or they will cause us to drop out of
6238151740cSJosh Paetzel  * MASTER mode.
6248151740cSJosh Paetzel  *
6258151740cSJosh Paetzel  * We cannot catch all cases of network loops.  Instead, what we
6268151740cSJosh Paetzel  * do here is catch any packet that arrives with a carp header
6278151740cSJosh Paetzel  * with a VHID of 0, that comes from an address that is our own.
6288151740cSJosh Paetzel  * These packets are by definition "from us" (even if they are from
6298151740cSJosh Paetzel  * a misconfigured host that is pretending to be us).
6308151740cSJosh Paetzel  *
6318151740cSJosh Paetzel  * The VHID test is outside this mini-function.
6328151740cSJosh Paetzel  */
6338151740cSJosh Paetzel static int
6348151740cSJosh Paetzel carp_source_is_self(struct mbuf *m, struct ifaddr *ifa, sa_family_t af)
6358151740cSJosh Paetzel {
636cfff8d3dSEnji Cooper #ifdef INET
6378151740cSJosh Paetzel 	struct ip *ip4;
6388151740cSJosh Paetzel 	struct in_addr in4;
639cfff8d3dSEnji Cooper #endif
640cfff8d3dSEnji Cooper #ifdef INET6
6418151740cSJosh Paetzel 	struct ip6_hdr *ip6;
6428151740cSJosh Paetzel 	struct in6_addr in6;
643cfff8d3dSEnji Cooper #endif
6448151740cSJosh Paetzel 
6458151740cSJosh Paetzel 	switch (af) {
646cfff8d3dSEnji Cooper #ifdef INET
6478151740cSJosh Paetzel 	case AF_INET:
6488151740cSJosh Paetzel 		ip4 = mtod(m, struct ip *);
6498151740cSJosh Paetzel 		in4 = ifatoia(ifa)->ia_addr.sin_addr;
6508151740cSJosh Paetzel 		return (in4.s_addr == ip4->ip_src.s_addr);
651cfff8d3dSEnji Cooper #endif
652cfff8d3dSEnji Cooper #ifdef INET6
6538151740cSJosh Paetzel 	case AF_INET6:
6548151740cSJosh Paetzel 		ip6 = mtod(m, struct ip6_hdr *);
6558151740cSJosh Paetzel 		in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
6568151740cSJosh Paetzel 		return (memcmp(&in6, &ip6->ip6_src, sizeof(in6)) == 0);
657cfff8d3dSEnji Cooper #endif
658cfff8d3dSEnji Cooper 	default:
6598151740cSJosh Paetzel 		break;
6608151740cSJosh Paetzel 	}
6618151740cSJosh Paetzel 	return (0);
6628151740cSJosh Paetzel }
6638151740cSJosh Paetzel 
6645c1f0f6dSGleb Smirnoff static void
665*13781800SKristof Provost carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af, int ttl)
666a9771948SGleb Smirnoff {
667a9771948SGleb Smirnoff 	struct ifnet *ifp = m->m_pkthdr.rcvif;
6688151740cSJosh Paetzel 	struct ifaddr *ifa, *match;
6691e9e6572SGleb Smirnoff 	struct carp_softc *sc;
67008b68b0eSGleb Smirnoff 	uint64_t tmp_counter;
671a9771948SGleb Smirnoff 	struct timeval sc_tv, ch_tv;
6728151740cSJosh Paetzel 	int error;
673*13781800SKristof Provost 	bool multicast = false;
674a9771948SGleb Smirnoff 
675b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
676b8a6e03fSGleb Smirnoff 
6778151740cSJosh Paetzel 	/*
6788151740cSJosh Paetzel 	 * Verify that the VHID is valid on the receiving interface.
6798151740cSJosh Paetzel 	 *
6808151740cSJosh Paetzel 	 * There should be just one match.  If there are none
6818151740cSJosh Paetzel 	 * the VHID is not valid and we drop the packet.  If
6828151740cSJosh Paetzel 	 * there are multiple VHID matches, take just the first
6838151740cSJosh Paetzel 	 * one, for compatibility with previous code.  While we're
6848151740cSJosh Paetzel 	 * scanning, check for obvious loops in the network topology
6858151740cSJosh Paetzel 	 * (these should never happen, and as noted above, we may
6868151740cSJosh Paetzel 	 * miss real loops; this is just a double-check).
6878151740cSJosh Paetzel 	 */
6888151740cSJosh Paetzel 	error = 0;
6898151740cSJosh Paetzel 	match = NULL;
6908151740cSJosh Paetzel 	IFNET_FOREACH_IFA(ifp, ifa) {
6918151740cSJosh Paetzel 		if (match == NULL && ifa->ifa_carp != NULL &&
6928151740cSJosh Paetzel 		    ifa->ifa_addr->sa_family == af &&
6938151740cSJosh Paetzel 		    ifa->ifa_carp->sc_vhid == ch->carp_vhid)
6948151740cSJosh Paetzel 			match = ifa;
6958151740cSJosh Paetzel 		if (ch->carp_vhid == 0 && carp_source_is_self(m, ifa, af))
6968151740cSJosh Paetzel 			error = ELOOP;
69708b68b0eSGleb Smirnoff 	}
6988151740cSJosh Paetzel 	ifa = error ? NULL : match;
6998151740cSJosh Paetzel 	if (ifa != NULL)
7008151740cSJosh Paetzel 		ifa_ref(ifa);
701d220759bSGleb Smirnoff 
70208b68b0eSGleb Smirnoff 	if (ifa == NULL) {
7038151740cSJosh Paetzel 		if (error == ELOOP) {
7048151740cSJosh Paetzel 			CARP_DEBUG("dropping looped packet on interface %s\n",
7058151740cSJosh Paetzel 			    ifp->if_xname);
7068151740cSJosh Paetzel 			CARPSTATS_INC(carps_badif);	/* ??? */
7078151740cSJosh Paetzel 		} else {
7086bf65bcfSRobert Watson 			CARPSTATS_INC(carps_badvhid);
7098151740cSJosh Paetzel 		}
710a9771948SGleb Smirnoff 		m_freem(m);
711a9771948SGleb Smirnoff 		return;
712a9771948SGleb Smirnoff 	}
713a9771948SGleb Smirnoff 
714a9771948SGleb Smirnoff 	/* verify the CARP version. */
715a9771948SGleb Smirnoff 	if (ch->carp_version != CARP_VERSION) {
7166bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badver);
71708b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: invalid version %d\n", ifp->if_xname,
7181e9e6572SGleb Smirnoff 		    ch->carp_version);
71908b68b0eSGleb Smirnoff 		ifa_free(ifa);
720a9771948SGleb Smirnoff 		m_freem(m);
721a9771948SGleb Smirnoff 		return;
722a9771948SGleb Smirnoff 	}
723a9771948SGleb Smirnoff 
72408b68b0eSGleb Smirnoff 	sc = ifa->ifa_carp;
72508b68b0eSGleb Smirnoff 	CARP_LOCK(sc);
726*13781800SKristof Provost 	if (ifa->ifa_addr->sa_family == AF_INET) {
727*13781800SKristof Provost 		multicast = IN_MULTICAST(sc->sc_carpaddr.s_addr);
728*13781800SKristof Provost 	} else {
729*13781800SKristof Provost 		multicast = IN6_IS_ADDR_MULTICAST(&sc->sc_carpaddr6);
730*13781800SKristof Provost 	}
73108b68b0eSGleb Smirnoff 	ifa_free(ifa);
73208b68b0eSGleb Smirnoff 
733*13781800SKristof Provost 	/* verify that the IP TTL is 255, but only if we're not in unicast mode. */
734*13781800SKristof Provost 	if (multicast && ttl != CARP_DFLTTL) {
735*13781800SKristof Provost 		CARPSTATS_INC(carps_badttl);
736*13781800SKristof Provost 		CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
737*13781800SKristof Provost 		    ttl, m->m_pkthdr.rcvif->if_xname);
738*13781800SKristof Provost 		goto out;
739*13781800SKristof Provost 	}
740*13781800SKristof Provost 
741a9771948SGleb Smirnoff 	if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
7426bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badauth);
74308b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: incorrect hash for VHID %u@%s\n", __func__,
74408b68b0eSGleb Smirnoff 		    sc->sc_vhid, ifp->if_xname);
74508b68b0eSGleb Smirnoff 		goto out;
746a9771948SGleb Smirnoff 	}
747a9771948SGleb Smirnoff 
748a9771948SGleb Smirnoff 	tmp_counter = ntohl(ch->carp_counter[0]);
749a9771948SGleb Smirnoff 	tmp_counter = tmp_counter<<32;
750a9771948SGleb Smirnoff 	tmp_counter += ntohl(ch->carp_counter[1]);
751a9771948SGleb Smirnoff 
752a9771948SGleb Smirnoff 	/* XXX Replay protection goes here */
753a9771948SGleb Smirnoff 
754a9771948SGleb Smirnoff 	sc->sc_init_counter = 0;
755a9771948SGleb Smirnoff 	sc->sc_counter = tmp_counter;
756a9771948SGleb Smirnoff 
757a9771948SGleb Smirnoff 	sc_tv.tv_sec = sc->sc_advbase;
758f08535f8SGleb Smirnoff 	sc_tv.tv_usec = DEMOTE_ADVSKEW(sc) * 1000000 / 256;
759a9771948SGleb Smirnoff 	ch_tv.tv_sec = ch->carp_advbase;
760a9771948SGleb Smirnoff 	ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
761a9771948SGleb Smirnoff 
762a9771948SGleb Smirnoff 	switch (sc->sc_state) {
763a9771948SGleb Smirnoff 	case INIT:
764a9771948SGleb Smirnoff 		break;
765a9771948SGleb Smirnoff 	case MASTER:
766a9771948SGleb Smirnoff 		/*
767a9771948SGleb Smirnoff 		 * If we receive an advertisement from a master who's going to
768a9771948SGleb Smirnoff 		 * be more frequent than us, go into BACKUP state.
769a9771948SGleb Smirnoff 		 */
770a9771948SGleb Smirnoff 		if (timevalcmp(&sc_tv, &ch_tv, >) ||
771a9771948SGleb Smirnoff 		    timevalcmp(&sc_tv, &ch_tv, ==)) {
772a9771948SGleb Smirnoff 			callout_stop(&sc->sc_ad_tmo);
773d01641e2SWill Andrews 			carp_set_state(sc, BACKUP,
774d01641e2SWill Andrews 			    "more frequent advertisement received");
775a9771948SGleb Smirnoff 			carp_setrun(sc, 0);
77608b68b0eSGleb Smirnoff 			carp_delroute(sc);
777a9771948SGleb Smirnoff 		}
778a9771948SGleb Smirnoff 		break;
779a9771948SGleb Smirnoff 	case BACKUP:
780a9771948SGleb Smirnoff 		/*
781a9771948SGleb Smirnoff 		 * If we're pre-empting masters who advertise slower than us,
782a9771948SGleb Smirnoff 		 * and this one claims to be slower, treat him as down.
783a9771948SGleb Smirnoff 		 */
784c5c392e7SMikolaj Golub 		if (V_carp_preempt && timevalcmp(&sc_tv, &ch_tv, <)) {
785d01641e2SWill Andrews 			carp_master_down_locked(sc,
786d01641e2SWill Andrews 			    "preempting a slower master");
787a9771948SGleb Smirnoff 			break;
788a9771948SGleb Smirnoff 		}
789a9771948SGleb Smirnoff 
790a9771948SGleb Smirnoff 		/*
791a9771948SGleb Smirnoff 		 *  If the master is going to advertise at such a low frequency
792a9771948SGleb Smirnoff 		 *  that he's guaranteed to time out, we'd might as well just
793a9771948SGleb Smirnoff 		 *  treat him as timed out now.
794a9771948SGleb Smirnoff 		 */
795a9771948SGleb Smirnoff 		sc_tv.tv_sec = sc->sc_advbase * 3;
796a9771948SGleb Smirnoff 		if (timevalcmp(&sc_tv, &ch_tv, <)) {
797d01641e2SWill Andrews 			carp_master_down_locked(sc, "master will time out");
798a9771948SGleb Smirnoff 			break;
799a9771948SGleb Smirnoff 		}
800a9771948SGleb Smirnoff 
801a9771948SGleb Smirnoff 		/*
802a9771948SGleb Smirnoff 		 * Otherwise, we reset the counter and wait for the next
803a9771948SGleb Smirnoff 		 * advertisement.
804a9771948SGleb Smirnoff 		 */
805a9771948SGleb Smirnoff 		carp_setrun(sc, af);
806a9771948SGleb Smirnoff 		break;
807a9771948SGleb Smirnoff 	}
808a9771948SGleb Smirnoff 
80908b68b0eSGleb Smirnoff out:
81008b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
811a9771948SGleb Smirnoff 	m_freem(m);
812a9771948SGleb Smirnoff }
813a9771948SGleb Smirnoff 
8145c1f0f6dSGleb Smirnoff static int
815a9771948SGleb Smirnoff carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
816a9771948SGleb Smirnoff {
817a9771948SGleb Smirnoff 	struct m_tag *mtag;
818a9771948SGleb Smirnoff 
819a9771948SGleb Smirnoff 	if (sc->sc_init_counter) {
820a9771948SGleb Smirnoff 		/* this could also be seconds since unix epoch */
821a9771948SGleb Smirnoff 		sc->sc_counter = arc4random();
822a9771948SGleb Smirnoff 		sc->sc_counter = sc->sc_counter << 32;
823a9771948SGleb Smirnoff 		sc->sc_counter += arc4random();
824a9771948SGleb Smirnoff 	} else
825a9771948SGleb Smirnoff 		sc->sc_counter++;
826a9771948SGleb Smirnoff 
827a9771948SGleb Smirnoff 	ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
828a9771948SGleb Smirnoff 	ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
829a9771948SGleb Smirnoff 
830a9771948SGleb Smirnoff 	carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
831a9771948SGleb Smirnoff 
832a9771948SGleb Smirnoff 	/* Tag packet for carp_output */
83308b68b0eSGleb Smirnoff 	if ((mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct carp_softc *),
83408b68b0eSGleb Smirnoff 	    M_NOWAIT)) == NULL) {
835a9771948SGleb Smirnoff 		m_freem(m);
83608b68b0eSGleb Smirnoff 		CARPSTATS_INC(carps_onomem);
837a9771948SGleb Smirnoff 		return (ENOMEM);
838a9771948SGleb Smirnoff 	}
839eaf151c4SGleb Smirnoff 	bcopy(&sc, mtag + 1, sizeof(sc));
840a9771948SGleb Smirnoff 	m_tag_prepend(m, mtag);
841a9771948SGleb Smirnoff 
842a9771948SGleb Smirnoff 	return (0);
843a9771948SGleb Smirnoff }
844a9771948SGleb Smirnoff 
845f08535f8SGleb Smirnoff /*
846f08535f8SGleb Smirnoff  * To avoid LORs and possible recursions this function shouldn't
847f08535f8SGleb Smirnoff  * be called directly, but scheduled via taskqueue.
848f08535f8SGleb Smirnoff  */
8495c1f0f6dSGleb Smirnoff static void
850f08535f8SGleb Smirnoff carp_send_ad_all(void *ctx __unused, int pending __unused)
851a9771948SGleb Smirnoff {
852d92d54d5SGleb Smirnoff 	struct carp_softc *sc;
8531d126e9bSKristof Provost 	struct epoch_tracker et;
854a9771948SGleb Smirnoff 
8551d126e9bSKristof Provost 	NET_EPOCH_ENTER(et);
856d92d54d5SGleb Smirnoff 	mtx_lock(&carp_mtx);
85708b68b0eSGleb Smirnoff 	LIST_FOREACH(sc, &carp_list, sc_next)
858f08535f8SGleb Smirnoff 		if (sc->sc_state == MASTER) {
85908b68b0eSGleb Smirnoff 			CARP_LOCK(sc);
860afdbac98SGleb Smirnoff 			CURVNET_SET(sc->sc_carpdev->if_vnet);
861d220759bSGleb Smirnoff 			carp_send_ad_locked(sc);
862afdbac98SGleb Smirnoff 			CURVNET_RESTORE();
86308b68b0eSGleb Smirnoff 			CARP_UNLOCK(sc);
864a9771948SGleb Smirnoff 		}
865d92d54d5SGleb Smirnoff 	mtx_unlock(&carp_mtx);
8661d126e9bSKristof Provost 	NET_EPOCH_EXIT(et);
867a9771948SGleb Smirnoff }
868a9771948SGleb Smirnoff 
869afdbac98SGleb Smirnoff /* Send a periodic advertisement, executed in callout context. */
8705c1f0f6dSGleb Smirnoff static void
871a9771948SGleb Smirnoff carp_send_ad(void *v)
872a9771948SGleb Smirnoff {
873d220759bSGleb Smirnoff 	struct carp_softc *sc = v;
8741d126e9bSKristof Provost 	struct epoch_tracker et;
875d220759bSGleb Smirnoff 
8761d126e9bSKristof Provost 	NET_EPOCH_ENTER(et);
87708b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
878afdbac98SGleb Smirnoff 	CURVNET_SET(sc->sc_carpdev->if_vnet);
879d220759bSGleb Smirnoff 	carp_send_ad_locked(sc);
880afdbac98SGleb Smirnoff 	CURVNET_RESTORE();
88108b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
8821d126e9bSKristof Provost 	NET_EPOCH_EXIT(et);
883d220759bSGleb Smirnoff }
884d220759bSGleb Smirnoff 
885d220759bSGleb Smirnoff static void
8864a2dd8d4SGleb Smirnoff carp_send_ad_error(struct carp_softc *sc, int error)
8874a2dd8d4SGleb Smirnoff {
8884a2dd8d4SGleb Smirnoff 
8899a8cf950SGleb Smirnoff 	/*
8909a8cf950SGleb Smirnoff 	 * We track errors and successfull sends with this logic:
8919a8cf950SGleb Smirnoff 	 * - Any error resets success counter to 0.
8929a8cf950SGleb Smirnoff 	 * - MAX_ERRORS triggers demotion.
8939a8cf950SGleb Smirnoff 	 * - MIN_SUCCESS successes resets error counter to 0.
8949a8cf950SGleb Smirnoff 	 * - MIN_SUCCESS reverts demotion, if it was triggered before.
8959a8cf950SGleb Smirnoff 	 */
8964a2dd8d4SGleb Smirnoff 	if (error) {
8974a2dd8d4SGleb Smirnoff 		if (sc->sc_sendad_errors < INT_MAX)
8984a2dd8d4SGleb Smirnoff 			sc->sc_sendad_errors++;
8994a2dd8d4SGleb Smirnoff 		if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
9004a2dd8d4SGleb Smirnoff 			static const char fmt[] = "send error %d on %s";
9014a2dd8d4SGleb Smirnoff 			char msg[sizeof(fmt) + IFNAMSIZ];
9024a2dd8d4SGleb Smirnoff 
9034a2dd8d4SGleb Smirnoff 			sprintf(msg, fmt, error, sc->sc_carpdev->if_xname);
9044a2dd8d4SGleb Smirnoff 			carp_demote_adj(V_carp_senderr_adj, msg);
9054a2dd8d4SGleb Smirnoff 		}
9064a2dd8d4SGleb Smirnoff 		sc->sc_sendad_success = 0;
9079a8cf950SGleb Smirnoff 	} else if (sc->sc_sendad_errors > 0) {
9089a8cf950SGleb Smirnoff 		if (++sc->sc_sendad_success >= CARP_SENDAD_MIN_SUCCESS) {
9099a8cf950SGleb Smirnoff 			if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
9104a2dd8d4SGleb Smirnoff 				static const char fmt[] = "send ok on %s";
9114a2dd8d4SGleb Smirnoff 				char msg[sizeof(fmt) + IFNAMSIZ];
9124a2dd8d4SGleb Smirnoff 
9134a2dd8d4SGleb Smirnoff 				sprintf(msg, fmt, sc->sc_carpdev->if_xname);
9144a2dd8d4SGleb Smirnoff 				carp_demote_adj(-V_carp_senderr_adj, msg);
9159a8cf950SGleb Smirnoff 			}
9164a2dd8d4SGleb Smirnoff 			sc->sc_sendad_errors = 0;
9179a8cf950SGleb Smirnoff 		}
9184a2dd8d4SGleb Smirnoff 	}
9194a2dd8d4SGleb Smirnoff }
9204a2dd8d4SGleb Smirnoff 
9218151740cSJosh Paetzel /*
9228151740cSJosh Paetzel  * Pick the best ifaddr on the given ifp for sending CARP
9238151740cSJosh Paetzel  * advertisements.
9248151740cSJosh Paetzel  *
9258151740cSJosh Paetzel  * "Best" here is defined by ifa_preferred().  This function is much
9268151740cSJosh Paetzel  * much like ifaof_ifpforaddr() except that we just use ifa_preferred().
9278151740cSJosh Paetzel  *
9288151740cSJosh Paetzel  * (This could be simplified to return the actual address, except that
9298151740cSJosh Paetzel  * it has a different format in AF_INET and AF_INET6.)
9308151740cSJosh Paetzel  */
9318151740cSJosh Paetzel static struct ifaddr *
9328151740cSJosh Paetzel carp_best_ifa(int af, struct ifnet *ifp)
9338151740cSJosh Paetzel {
9348151740cSJosh Paetzel 	struct ifaddr *ifa, *best;
9358151740cSJosh Paetzel 
936b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
937b8a6e03fSGleb Smirnoff 
9388151740cSJosh Paetzel 	if (af >= AF_MAX)
9398151740cSJosh Paetzel 		return (NULL);
9408151740cSJosh Paetzel 	best = NULL;
941d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
9428151740cSJosh Paetzel 		if (ifa->ifa_addr->sa_family == af &&
9438151740cSJosh Paetzel 		    (best == NULL || ifa_preferred(best, ifa)))
9448151740cSJosh Paetzel 			best = ifa;
9458151740cSJosh Paetzel 	}
9468151740cSJosh Paetzel 	if (best != NULL)
9478151740cSJosh Paetzel 		ifa_ref(best);
9488151740cSJosh Paetzel 	return (best);
9498151740cSJosh Paetzel }
9508151740cSJosh Paetzel 
9514a2dd8d4SGleb Smirnoff static void
952d220759bSGleb Smirnoff carp_send_ad_locked(struct carp_softc *sc)
953d220759bSGleb Smirnoff {
954a9771948SGleb Smirnoff 	struct carp_header ch;
955a9771948SGleb Smirnoff 	struct timeval tv;
95608b68b0eSGleb Smirnoff 	struct ifaddr *ifa;
957a9771948SGleb Smirnoff 	struct carp_header *ch_ptr;
958a9771948SGleb Smirnoff 	struct mbuf *m;
95908b68b0eSGleb Smirnoff 	int len, advskew;
960a9771948SGleb Smirnoff 
9611d126e9bSKristof Provost 	NET_EPOCH_ASSERT();
96208b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
963d220759bSGleb Smirnoff 
964f08535f8SGleb Smirnoff 	advskew = DEMOTE_ADVSKEW(sc);
96508b68b0eSGleb Smirnoff 	tv.tv_sec = sc->sc_advbase;
966a9771948SGleb Smirnoff 	tv.tv_usec = advskew * 1000000 / 256;
967a9771948SGleb Smirnoff 
968a9771948SGleb Smirnoff 	ch.carp_version = CARP_VERSION;
969a9771948SGleb Smirnoff 	ch.carp_type = CARP_ADVERTISEMENT;
970a9771948SGleb Smirnoff 	ch.carp_vhid = sc->sc_vhid;
97108b68b0eSGleb Smirnoff 	ch.carp_advbase = sc->sc_advbase;
972a9771948SGleb Smirnoff 	ch.carp_advskew = advskew;
973a9771948SGleb Smirnoff 	ch.carp_authlen = 7;	/* XXX DEFINE */
974a9771948SGleb Smirnoff 	ch.carp_pad1 = 0;	/* must be zero */
975a9771948SGleb Smirnoff 	ch.carp_cksum = 0;
976a9771948SGleb Smirnoff 
97708b68b0eSGleb Smirnoff 	/* XXXGL: OpenBSD picks first ifaddr with needed family. */
97808b68b0eSGleb Smirnoff 
979a9771948SGleb Smirnoff #ifdef INET
98008b68b0eSGleb Smirnoff 	if (sc->sc_naddrs) {
981a9771948SGleb Smirnoff 		struct ip *ip;
982a9771948SGleb Smirnoff 
983dc4ad05eSGleb Smirnoff 		m = m_gethdr(M_NOWAIT, MT_DATA);
984a9771948SGleb Smirnoff 		if (m == NULL) {
9856bf65bcfSRobert Watson 			CARPSTATS_INC(carps_onomem);
986891122d1SGleb Smirnoff 			goto resched;
987a9771948SGleb Smirnoff 		}
988a9771948SGleb Smirnoff 		len = sizeof(*ip) + sizeof(ch);
989a9771948SGleb Smirnoff 		m->m_pkthdr.len = len;
990a9771948SGleb Smirnoff 		m->m_pkthdr.rcvif = NULL;
991a9771948SGleb Smirnoff 		m->m_len = len;
992ed6a66caSRobert Watson 		M_ALIGN(m, m->m_len);
993*13781800SKristof Provost 		if (IN_MULTICAST(sc->sc_carpaddr.s_addr))
994a9771948SGleb Smirnoff 			m->m_flags |= M_MCAST;
995a9771948SGleb Smirnoff 		ip = mtod(m, struct ip *);
996a9771948SGleb Smirnoff 		ip->ip_v = IPVERSION;
997a9771948SGleb Smirnoff 		ip->ip_hl = sizeof(*ip) >> 2;
9980d3d234cSKristof Provost 		ip->ip_tos = V_carp_dscp << IPTOS_DSCP_OFFSET;
9998f134647SGleb Smirnoff 		ip->ip_len = htons(len);
10008f134647SGleb Smirnoff 		ip->ip_off = htons(IP_DF);
1001a9771948SGleb Smirnoff 		ip->ip_ttl = CARP_DFLTTL;
1002a9771948SGleb Smirnoff 		ip->ip_p = IPPROTO_CARP;
1003a9771948SGleb Smirnoff 		ip->ip_sum = 0;
10046d947416SGleb Smirnoff 		ip_fillid(ip);
100508b68b0eSGleb Smirnoff 
10068151740cSJosh Paetzel 		ifa = carp_best_ifa(AF_INET, sc->sc_carpdev);
100708b68b0eSGleb Smirnoff 		if (ifa != NULL) {
100808b68b0eSGleb Smirnoff 			ip->ip_src.s_addr =
100908b68b0eSGleb Smirnoff 			    ifatoia(ifa)->ia_addr.sin_addr.s_addr;
101008b68b0eSGleb Smirnoff 			ifa_free(ifa);
101108b68b0eSGleb Smirnoff 		} else
101208b68b0eSGleb Smirnoff 			ip->ip_src.s_addr = 0;
1013*13781800SKristof Provost 		ip->ip_dst = sc->sc_carpaddr;
1014a9771948SGleb Smirnoff 
1015a9771948SGleb Smirnoff 		ch_ptr = (struct carp_header *)(&ip[1]);
1016a9771948SGleb Smirnoff 		bcopy(&ch, ch_ptr, sizeof(ch));
1017a9771948SGleb Smirnoff 		if (carp_prepare_ad(m, sc, ch_ptr))
1018891122d1SGleb Smirnoff 			goto resched;
1019a9771948SGleb Smirnoff 
1020a9771948SGleb Smirnoff 		m->m_data += sizeof(*ip);
1021c4d06976SGleb Smirnoff 		ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip));
1022a9771948SGleb Smirnoff 		m->m_data -= sizeof(*ip);
1023a9771948SGleb Smirnoff 
10246bf65bcfSRobert Watson 		CARPSTATS_INC(carps_opackets);
1025a9771948SGleb Smirnoff 
10264a2dd8d4SGleb Smirnoff 		carp_send_ad_error(sc, ip_output(m, NULL, NULL, IP_RAWOUTPUT,
10274a2dd8d4SGleb Smirnoff 		    &sc->sc_carpdev->if_carp->cif_imo, NULL));
1028a9771948SGleb Smirnoff 	}
1029a9771948SGleb Smirnoff #endif /* INET */
1030a9771948SGleb Smirnoff #ifdef INET6
103108b68b0eSGleb Smirnoff 	if (sc->sc_naddrs6) {
1032a9771948SGleb Smirnoff 		struct ip6_hdr *ip6;
1033a9771948SGleb Smirnoff 
1034dc4ad05eSGleb Smirnoff 		m = m_gethdr(M_NOWAIT, MT_DATA);
1035a9771948SGleb Smirnoff 		if (m == NULL) {
10366bf65bcfSRobert Watson 			CARPSTATS_INC(carps_onomem);
1037891122d1SGleb Smirnoff 			goto resched;
1038a9771948SGleb Smirnoff 		}
1039a9771948SGleb Smirnoff 		len = sizeof(*ip6) + sizeof(ch);
1040a9771948SGleb Smirnoff 		m->m_pkthdr.len = len;
1041a9771948SGleb Smirnoff 		m->m_pkthdr.rcvif = NULL;
1042a9771948SGleb Smirnoff 		m->m_len = len;
1043ed6a66caSRobert Watson 		M_ALIGN(m, m->m_len);
1044a9771948SGleb Smirnoff 		ip6 = mtod(m, struct ip6_hdr *);
1045a9771948SGleb Smirnoff 		bzero(ip6, sizeof(*ip6));
1046a9771948SGleb Smirnoff 		ip6->ip6_vfc |= IPV6_VERSION;
10470d3d234cSKristof Provost 		/* Traffic class isn't defined in ip6 struct instead
10480d3d234cSKristof Provost 		 * it gets offset into flowid field */
10490d3d234cSKristof Provost 		ip6->ip6_flow |= htonl(V_carp_dscp << (IPV6_FLOWLABEL_LEN +
10500d3d234cSKristof Provost 		    IPTOS_DSCP_OFFSET));
1051a9771948SGleb Smirnoff 		ip6->ip6_hlim = CARP_DFLTTL;
1052a9771948SGleb Smirnoff 		ip6->ip6_nxt = IPPROTO_CARP;
1053a9771948SGleb Smirnoff 
105408b68b0eSGleb Smirnoff 		/* set the source address */
10558151740cSJosh Paetzel 		ifa = carp_best_ifa(AF_INET6, sc->sc_carpdev);
105608b68b0eSGleb Smirnoff 		if (ifa != NULL) {
105708b68b0eSGleb Smirnoff 			bcopy(IFA_IN6(ifa), &ip6->ip6_src,
105808b68b0eSGleb Smirnoff 			    sizeof(struct in6_addr));
105908b68b0eSGleb Smirnoff 			ifa_free(ifa);
106008b68b0eSGleb Smirnoff 		} else
106108b68b0eSGleb Smirnoff 			/* This should never happen with IPv6. */
106208b68b0eSGleb Smirnoff 			bzero(&ip6->ip6_src, sizeof(struct in6_addr));
106308b68b0eSGleb Smirnoff 
106408b68b0eSGleb Smirnoff 		/* Set the multicast destination. */
1065*13781800SKristof Provost 		memcpy(&ip6->ip6_dst, &sc->sc_carpaddr6, sizeof(ip6->ip6_dst));
1066*13781800SKristof Provost 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
10677002145dSBjoern A. Zeeb 			if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
10687002145dSBjoern A. Zeeb 				m_freem(m);
1069e81ab876SGleb Smirnoff 				CARP_DEBUG("%s: in6_setscope failed\n", __func__);
1070891122d1SGleb Smirnoff 				goto resched;
10717002145dSBjoern A. Zeeb 			}
1072*13781800SKristof Provost 		}
1073a9771948SGleb Smirnoff 
1074a9771948SGleb Smirnoff 		ch_ptr = (struct carp_header *)(&ip6[1]);
1075a9771948SGleb Smirnoff 		bcopy(&ch, ch_ptr, sizeof(ch));
1076a9771948SGleb Smirnoff 		if (carp_prepare_ad(m, sc, ch_ptr))
1077891122d1SGleb Smirnoff 			goto resched;
1078a9771948SGleb Smirnoff 
1079a9771948SGleb Smirnoff 		m->m_data += sizeof(*ip6);
1080c4d06976SGleb Smirnoff 		ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip6));
1081a9771948SGleb Smirnoff 		m->m_data -= sizeof(*ip6);
1082a9771948SGleb Smirnoff 
10836bf65bcfSRobert Watson 		CARPSTATS_INC(carps_opackets6);
1084a9771948SGleb Smirnoff 
10854a2dd8d4SGleb Smirnoff 		carp_send_ad_error(sc, ip6_output(m, NULL, NULL, 0,
10864a2dd8d4SGleb Smirnoff 		    &sc->sc_carpdev->if_carp->cif_im6o, NULL, NULL));
1087a9771948SGleb Smirnoff 	}
1088a9771948SGleb Smirnoff #endif /* INET6 */
1089a9771948SGleb Smirnoff 
1090891122d1SGleb Smirnoff resched:
109108b68b0eSGleb Smirnoff 	callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), carp_send_ad, sc);
109208b68b0eSGleb Smirnoff }
1093a9771948SGleb Smirnoff 
109408b68b0eSGleb Smirnoff static void
109508b68b0eSGleb Smirnoff carp_addroute(struct carp_softc *sc)
109608b68b0eSGleb Smirnoff {
109708b68b0eSGleb Smirnoff 	struct ifaddr *ifa;
109808b68b0eSGleb Smirnoff 
109908b68b0eSGleb Smirnoff 	CARP_FOREACH_IFA(sc, ifa)
11002512b096SGleb Smirnoff 		carp_ifa_addroute(ifa);
11012512b096SGleb Smirnoff }
11022512b096SGleb Smirnoff 
11032512b096SGleb Smirnoff static void
11042512b096SGleb Smirnoff carp_ifa_addroute(struct ifaddr *ifa)
11052512b096SGleb Smirnoff {
11062512b096SGleb Smirnoff 
110708b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
110808b68b0eSGleb Smirnoff #ifdef INET
110908b68b0eSGleb Smirnoff 	case AF_INET:
1110130aebbaSAlexander V. Chernikov 		in_addprefix(ifatoia(ifa));
111108b68b0eSGleb Smirnoff 		ifa_add_loopback_route(ifa,
111208b68b0eSGleb Smirnoff 		    (struct sockaddr *)&ifatoia(ifa)->ia_addr);
111308b68b0eSGleb Smirnoff 		break;
111408b68b0eSGleb Smirnoff #endif
111508b68b0eSGleb Smirnoff #ifdef INET6
111608b68b0eSGleb Smirnoff 	case AF_INET6:
111708b68b0eSGleb Smirnoff 		ifa_add_loopback_route(ifa,
111808b68b0eSGleb Smirnoff 		    (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
1119f6b84910SAlexander V. Chernikov 		nd6_add_ifa_lle(ifatoia6(ifa));
112008b68b0eSGleb Smirnoff 		break;
112108b68b0eSGleb Smirnoff #endif
112208b68b0eSGleb Smirnoff 	}
112308b68b0eSGleb Smirnoff }
112408b68b0eSGleb Smirnoff 
112508b68b0eSGleb Smirnoff static void
112608b68b0eSGleb Smirnoff carp_delroute(struct carp_softc *sc)
112708b68b0eSGleb Smirnoff {
112808b68b0eSGleb Smirnoff 	struct ifaddr *ifa;
112908b68b0eSGleb Smirnoff 
113008b68b0eSGleb Smirnoff 	CARP_FOREACH_IFA(sc, ifa)
11312512b096SGleb Smirnoff 		carp_ifa_delroute(ifa);
11322512b096SGleb Smirnoff }
11332512b096SGleb Smirnoff 
11342512b096SGleb Smirnoff static void
11352512b096SGleb Smirnoff carp_ifa_delroute(struct ifaddr *ifa)
11362512b096SGleb Smirnoff {
11372512b096SGleb Smirnoff 
113808b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
113908b68b0eSGleb Smirnoff #ifdef INET
114008b68b0eSGleb Smirnoff 	case AF_INET:
114108b68b0eSGleb Smirnoff 		ifa_del_loopback_route(ifa,
114208b68b0eSGleb Smirnoff 		    (struct sockaddr *)&ifatoia(ifa)->ia_addr);
114308b68b0eSGleb Smirnoff 		in_scrubprefix(ifatoia(ifa), LLE_STATIC);
114408b68b0eSGleb Smirnoff 		break;
114508b68b0eSGleb Smirnoff #endif
114608b68b0eSGleb Smirnoff #ifdef INET6
114708b68b0eSGleb Smirnoff 	case AF_INET6:
114808b68b0eSGleb Smirnoff 		ifa_del_loopback_route(ifa,
114908b68b0eSGleb Smirnoff 		    (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
11503e7a2321SAlexander V. Chernikov 		nd6_rem_ifa_lle(ifatoia6(ifa), 1);
115108b68b0eSGleb Smirnoff 		break;
115208b68b0eSGleb Smirnoff #endif
115308b68b0eSGleb Smirnoff 	}
1154a9771948SGleb Smirnoff }
1155a9771948SGleb Smirnoff 
115624421c1cSGleb Smirnoff int
115724421c1cSGleb Smirnoff carp_master(struct ifaddr *ifa)
115824421c1cSGleb Smirnoff {
115924421c1cSGleb Smirnoff 	struct carp_softc *sc = ifa->ifa_carp;
116024421c1cSGleb Smirnoff 
116124421c1cSGleb Smirnoff 	return (sc->sc_state == MASTER);
116224421c1cSGleb Smirnoff }
116324421c1cSGleb Smirnoff 
1164a0ae8f04SBjoern A. Zeeb #ifdef INET
1165a9771948SGleb Smirnoff /*
1166a9771948SGleb Smirnoff  * Broadcast a gratuitous ARP request containing
1167a9771948SGleb Smirnoff  * the virtual router MAC address for each IP address
1168a9771948SGleb Smirnoff  * associated with the virtual router.
1169a9771948SGleb Smirnoff  */
11705c1f0f6dSGleb Smirnoff static void
1171a9771948SGleb Smirnoff carp_send_arp(struct carp_softc *sc)
1172a9771948SGleb Smirnoff {
1173a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1174d6e82913SSteven Hartland 	struct in_addr addr;
1175a9771948SGleb Smirnoff 
11761d126e9bSKristof Provost 	NET_EPOCH_ASSERT();
11771d126e9bSKristof Provost 
11781c302b58SAlexander V. Chernikov 	CARP_FOREACH_IFA(sc, ifa) {
11791c302b58SAlexander V. Chernikov 		if (ifa->ifa_addr->sa_family != AF_INET)
11801c302b58SAlexander V. Chernikov 			continue;
1181d6e82913SSteven Hartland 		addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
1182d6e82913SSteven Hartland 		arp_announce_ifaddr(sc->sc_carpdev, addr, LLADDR(&sc->sc_addr));
11831c302b58SAlexander V. Chernikov 	}
1184a9771948SGleb Smirnoff }
118508b68b0eSGleb Smirnoff 
118608b68b0eSGleb Smirnoff int
118708b68b0eSGleb Smirnoff carp_iamatch(struct ifaddr *ifa, uint8_t **enaddr)
118808b68b0eSGleb Smirnoff {
118908b68b0eSGleb Smirnoff 	struct carp_softc *sc = ifa->ifa_carp;
119008b68b0eSGleb Smirnoff 
119108b68b0eSGleb Smirnoff 	if (sc->sc_state == MASTER) {
119208b68b0eSGleb Smirnoff 		*enaddr = LLADDR(&sc->sc_addr);
119308b68b0eSGleb Smirnoff 		return (1);
119408b68b0eSGleb Smirnoff 	}
119508b68b0eSGleb Smirnoff 
119608b68b0eSGleb Smirnoff 	return (0);
1197a9771948SGleb Smirnoff }
1198a0ae8f04SBjoern A. Zeeb #endif
1199a9771948SGleb Smirnoff 
1200a9771948SGleb Smirnoff #ifdef INET6
12015c1f0f6dSGleb Smirnoff static void
1202a9771948SGleb Smirnoff carp_send_na(struct carp_softc *sc)
1203a9771948SGleb Smirnoff {
1204d6e82913SSteven Hartland 	static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1205a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1206d6e82913SSteven Hartland 	struct in6_addr *in6;
1207a9771948SGleb Smirnoff 
120808b68b0eSGleb Smirnoff 	CARP_FOREACH_IFA(sc, ifa) {
1209d6e82913SSteven Hartland 		if (ifa->ifa_addr->sa_family != AF_INET6)
1210a9771948SGleb Smirnoff 			continue;
1211a9771948SGleb Smirnoff 
1212d6e82913SSteven Hartland 		in6 = IFA_IN6(ifa);
1213d6e82913SSteven Hartland 		nd6_na_output(sc->sc_carpdev, &mcast, in6,
1214d6e82913SSteven Hartland 		    ND_NA_FLAG_OVERRIDE, 1, NULL);
1215d6e82913SSteven Hartland 		DELAY(1000);	/* XXX */
1216a9771948SGleb Smirnoff 	}
1217a9771948SGleb Smirnoff }
1218a9771948SGleb Smirnoff 
12198253dcabSBjoern A. Zeeb /*
12208253dcabSBjoern A. Zeeb  * Returns ifa in case it's a carp address and it is MASTER, or if the address
12218253dcabSBjoern A. Zeeb  * matches and is not a carp address.  Returns NULL otherwise.
12228253dcabSBjoern A. Zeeb  */
1223a4e53905SMax Laier struct ifaddr *
122454bfbd51SWill Andrews carp_iamatch6(struct ifnet *ifp, struct in6_addr *taddr)
1225a9771948SGleb Smirnoff {
1226a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1227a9771948SGleb Smirnoff 
1228b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
1229b8a6e03fSGleb Smirnoff 
12308253dcabSBjoern A. Zeeb 	ifa = NULL;
1231d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
12328253dcabSBjoern A. Zeeb 		if (ifa->ifa_addr->sa_family != AF_INET6)
12338253dcabSBjoern A. Zeeb 			continue;
12348253dcabSBjoern A. Zeeb 		if (!IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa)))
12358253dcabSBjoern A. Zeeb 			continue;
12368253dcabSBjoern A. Zeeb 		if (ifa->ifa_carp && ifa->ifa_carp->sc_state != MASTER)
12378253dcabSBjoern A. Zeeb 			ifa = NULL;
12388253dcabSBjoern A. Zeeb 		else
12398c0fec80SRobert Watson 			ifa_ref(ifa);
12408253dcabSBjoern A. Zeeb 		break;
1241a9771948SGleb Smirnoff 	}
1242a9771948SGleb Smirnoff 
12438253dcabSBjoern A. Zeeb 	return (ifa);
1244a9771948SGleb Smirnoff }
1245a9771948SGleb Smirnoff 
1246dad68fc3SBjoern A. Zeeb char *
124754bfbd51SWill Andrews carp_macmatch6(struct ifnet *ifp, struct mbuf *m, const struct in6_addr *taddr)
1248a9771948SGleb Smirnoff {
1249a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1250a9771948SGleb Smirnoff 
1251b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
1252b8a6e03fSGleb Smirnoff 
125308b68b0eSGleb Smirnoff 	IFNET_FOREACH_IFA(ifp, ifa)
125408b68b0eSGleb Smirnoff 		if (ifa->ifa_addr->sa_family == AF_INET6 &&
125508b68b0eSGleb Smirnoff 		    IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) {
125608b68b0eSGleb Smirnoff 			struct carp_softc *sc = ifa->ifa_carp;
125708b68b0eSGleb Smirnoff 			struct m_tag *mtag;
125808b68b0eSGleb Smirnoff 
1259a9771948SGleb Smirnoff 			mtag = m_tag_get(PACKET_TAG_CARP,
1260a856ddc6SGleb Smirnoff 			    sizeof(struct carp_softc *), M_NOWAIT);
126108b68b0eSGleb Smirnoff 			if (mtag == NULL)
126208b68b0eSGleb Smirnoff 				/* Better a bit than nothing. */
126308b68b0eSGleb Smirnoff 				return (LLADDR(&sc->sc_addr));
126408b68b0eSGleb Smirnoff 
1265eaf151c4SGleb Smirnoff 			bcopy(&sc, mtag + 1, sizeof(sc));
1266a9771948SGleb Smirnoff 			m_tag_prepend(m, mtag);
1267a9771948SGleb Smirnoff 
126808b68b0eSGleb Smirnoff 			return (LLADDR(&sc->sc_addr));
1269a9771948SGleb Smirnoff 		}
1270a9771948SGleb Smirnoff 
1271a9771948SGleb Smirnoff 	return (NULL);
1272a9771948SGleb Smirnoff }
127308b68b0eSGleb Smirnoff #endif /* INET6 */
1274a9771948SGleb Smirnoff 
127508b68b0eSGleb Smirnoff int
127654bfbd51SWill Andrews carp_forus(struct ifnet *ifp, u_char *dhost)
1277a9771948SGleb Smirnoff {
127808b68b0eSGleb Smirnoff 	struct carp_softc *sc;
127908b68b0eSGleb Smirnoff 	uint8_t *ena = dhost;
1280a9771948SGleb Smirnoff 
1281a9771948SGleb Smirnoff 	if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
128208b68b0eSGleb Smirnoff 		return (0);
1283a9771948SGleb Smirnoff 
128408b68b0eSGleb Smirnoff 	CIF_LOCK(ifp->if_carp);
128508b68b0eSGleb Smirnoff 	IFNET_FOREACH_CARP(ifp, sc) {
12868c3fbf3cSAlexander Motin 		/*
12878c3fbf3cSAlexander Motin 		 * CARP_LOCK() is not here, since would protect nothing, but
12888c3fbf3cSAlexander Motin 		 * cause deadlock with if_bridge, calling this under its lock.
12898c3fbf3cSAlexander Motin 		 */
129008b68b0eSGleb Smirnoff 		if (sc->sc_state == MASTER && !bcmp(dhost, LLADDR(&sc->sc_addr),
129108b68b0eSGleb Smirnoff 		    ETHER_ADDR_LEN)) {
129208b68b0eSGleb Smirnoff 			CIF_UNLOCK(ifp->if_carp);
129308b68b0eSGleb Smirnoff 			return (1);
1294a9771948SGleb Smirnoff 		}
129508b68b0eSGleb Smirnoff 	}
129608b68b0eSGleb Smirnoff 	CIF_UNLOCK(ifp->if_carp);
1297a9771948SGleb Smirnoff 
129808b68b0eSGleb Smirnoff 	return (0);
1299a9771948SGleb Smirnoff }
1300a9771948SGleb Smirnoff 
1301afdbac98SGleb Smirnoff /* Master down timeout event, executed in callout context. */
13025c1f0f6dSGleb Smirnoff static void
1303a9771948SGleb Smirnoff carp_master_down(void *v)
1304a9771948SGleb Smirnoff {
1305a9771948SGleb Smirnoff 	struct carp_softc *sc = v;
13061d126e9bSKristof Provost 	struct epoch_tracker et;
1307a9771948SGleb Smirnoff 
13081d126e9bSKristof Provost 	NET_EPOCH_ENTER(et);
130908b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
131008b68b0eSGleb Smirnoff 
1311afdbac98SGleb Smirnoff 	CURVNET_SET(sc->sc_carpdev->if_vnet);
131208b68b0eSGleb Smirnoff 	if (sc->sc_state == BACKUP) {
1313d01641e2SWill Andrews 		carp_master_down_locked(sc, "master timed out");
131408b68b0eSGleb Smirnoff 	}
1315afdbac98SGleb Smirnoff 	CURVNET_RESTORE();
131608b68b0eSGleb Smirnoff 
131708b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
13181d126e9bSKristof Provost 	NET_EPOCH_EXIT(et);
1319d220759bSGleb Smirnoff }
1320d220759bSGleb Smirnoff 
1321d220759bSGleb Smirnoff static void
1322d01641e2SWill Andrews carp_master_down_locked(struct carp_softc *sc, const char *reason)
1323d220759bSGleb Smirnoff {
132408b68b0eSGleb Smirnoff 
13251d126e9bSKristof Provost 	NET_EPOCH_ASSERT();
132608b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
1327d220759bSGleb Smirnoff 
1328a9771948SGleb Smirnoff 	switch (sc->sc_state) {
1329a9771948SGleb Smirnoff 	case BACKUP:
1330d01641e2SWill Andrews 		carp_set_state(sc, MASTER, reason);
1331d220759bSGleb Smirnoff 		carp_send_ad_locked(sc);
1332a0ae8f04SBjoern A. Zeeb #ifdef INET
1333a9771948SGleb Smirnoff 		carp_send_arp(sc);
1334a0ae8f04SBjoern A. Zeeb #endif
1335a9771948SGleb Smirnoff #ifdef INET6
1336a9771948SGleb Smirnoff 		carp_send_na(sc);
133708b68b0eSGleb Smirnoff #endif
1338a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
133908b68b0eSGleb Smirnoff 		carp_addroute(sc);
134008b68b0eSGleb Smirnoff 		break;
134108b68b0eSGleb Smirnoff 	case INIT:
134208b68b0eSGleb Smirnoff 	case MASTER:
134308b68b0eSGleb Smirnoff #ifdef INVARIANTS
134408b68b0eSGleb Smirnoff 		panic("carp: VHID %u@%s: master_down event in %s state\n",
134508b68b0eSGleb Smirnoff 		    sc->sc_vhid,
134608b68b0eSGleb Smirnoff 		    sc->sc_carpdev->if_xname,
134708b68b0eSGleb Smirnoff 		    sc->sc_state ? "MASTER" : "INIT");
134808b68b0eSGleb Smirnoff #endif
1349a9771948SGleb Smirnoff 		break;
1350a9771948SGleb Smirnoff 	}
1351a9771948SGleb Smirnoff }
1352a9771948SGleb Smirnoff 
1353a9771948SGleb Smirnoff /*
1354a9771948SGleb Smirnoff  * When in backup state, af indicates whether to reset the master down timer
1355a9771948SGleb Smirnoff  * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1356a9771948SGleb Smirnoff  */
13575c1f0f6dSGleb Smirnoff static void
1358a9771948SGleb Smirnoff carp_setrun(struct carp_softc *sc, sa_family_t af)
1359a9771948SGleb Smirnoff {
1360a9771948SGleb Smirnoff 	struct timeval tv;
1361a9771948SGleb Smirnoff 
136208b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
1363d220759bSGleb Smirnoff 
136408b68b0eSGleb Smirnoff 	if ((sc->sc_carpdev->if_flags & IFF_UP) == 0 ||
136508b68b0eSGleb Smirnoff 	    sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1366167a3440SAlexander Motin 	    (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) ||
1367167a3440SAlexander Motin 	    !V_carp_allow)
1368a9771948SGleb Smirnoff 		return;
1369a9771948SGleb Smirnoff 
1370a9771948SGleb Smirnoff 	switch (sc->sc_state) {
1371a9771948SGleb Smirnoff 	case INIT:
1372d01641e2SWill Andrews 		carp_set_state(sc, BACKUP, "initialization complete");
1373a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
1374a9771948SGleb Smirnoff 		break;
1375a9771948SGleb Smirnoff 	case BACKUP:
1376a9771948SGleb Smirnoff 		callout_stop(&sc->sc_ad_tmo);
1377a9771948SGleb Smirnoff 		tv.tv_sec = 3 * sc->sc_advbase;
1378a9771948SGleb Smirnoff 		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1379a9771948SGleb Smirnoff 		switch (af) {
1380a9771948SGleb Smirnoff #ifdef INET
1381a9771948SGleb Smirnoff 		case AF_INET:
1382a9771948SGleb Smirnoff 			callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1383a9771948SGleb Smirnoff 			    carp_master_down, sc);
1384a9771948SGleb Smirnoff 			break;
138508b68b0eSGleb Smirnoff #endif
1386a9771948SGleb Smirnoff #ifdef INET6
1387a9771948SGleb Smirnoff 		case AF_INET6:
1388a9771948SGleb Smirnoff 			callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1389a9771948SGleb Smirnoff 			    carp_master_down, sc);
1390a9771948SGleb Smirnoff 			break;
139108b68b0eSGleb Smirnoff #endif
1392a9771948SGleb Smirnoff 		default:
139308b68b0eSGleb Smirnoff #ifdef INET
1394a9771948SGleb Smirnoff 			if (sc->sc_naddrs)
1395a9771948SGleb Smirnoff 				callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1396a9771948SGleb Smirnoff 				    carp_master_down, sc);
139708b68b0eSGleb Smirnoff #endif
139808b68b0eSGleb Smirnoff #ifdef INET6
1399a9771948SGleb Smirnoff 			if (sc->sc_naddrs6)
1400a9771948SGleb Smirnoff 				callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1401a9771948SGleb Smirnoff 				    carp_master_down, sc);
140208b68b0eSGleb Smirnoff #endif
1403a9771948SGleb Smirnoff 			break;
1404a9771948SGleb Smirnoff 		}
1405a9771948SGleb Smirnoff 		break;
1406a9771948SGleb Smirnoff 	case MASTER:
1407a9771948SGleb Smirnoff 		tv.tv_sec = sc->sc_advbase;
1408a9771948SGleb Smirnoff 		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1409a9771948SGleb Smirnoff 		callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1410a9771948SGleb Smirnoff 		    carp_send_ad, sc);
1411a9771948SGleb Smirnoff 		break;
1412a9771948SGleb Smirnoff 	}
1413a9771948SGleb Smirnoff }
1414a9771948SGleb Smirnoff 
141508b68b0eSGleb Smirnoff /*
141608b68b0eSGleb Smirnoff  * Setup multicast structures.
141708b68b0eSGleb Smirnoff  */
14185c1f0f6dSGleb Smirnoff static int
1419a9a2c40cSGleb Smirnoff carp_multicast_setup(struct carp_if *cif, sa_family_t sa)
1420a9771948SGleb Smirnoff {
1421a9a2c40cSGleb Smirnoff 	struct ifnet *ifp = cif->cif_ifp;
142208b68b0eSGleb Smirnoff 	int error = 0;
142308b68b0eSGleb Smirnoff 
142408b68b0eSGleb Smirnoff 	switch (sa) {
142508b68b0eSGleb Smirnoff #ifdef INET
142608b68b0eSGleb Smirnoff 	case AF_INET:
142708b68b0eSGleb Smirnoff 	    {
142808b68b0eSGleb Smirnoff 		struct ip_moptions *imo = &cif->cif_imo;
142959854ecfSHans Petter Selasky 		struct in_mfilter *imf;
1430a9771948SGleb Smirnoff 		struct in_addr addr;
1431a9771948SGleb Smirnoff 
143259854ecfSHans Petter Selasky 		if (ip_mfilter_first(&imo->imo_head) != NULL)
1433a9771948SGleb Smirnoff 			return (0);
1434a9771948SGleb Smirnoff 
143559854ecfSHans Petter Selasky 		imf = ip_mfilter_alloc(M_WAITOK, 0, 0);
143659854ecfSHans Petter Selasky 		ip_mfilter_init(&imo->imo_head);
143708b68b0eSGleb Smirnoff 		imo->imo_multicast_vif = -1;
1438a9771948SGleb Smirnoff 
1439a9771948SGleb Smirnoff 		addr.s_addr = htonl(INADDR_CARP_GROUP);
144008b68b0eSGleb Smirnoff 		if ((error = in_joingroup(ifp, &addr, NULL,
144159854ecfSHans Petter Selasky 		    &imf->imf_inm)) != 0) {
144259854ecfSHans Petter Selasky 			ip_mfilter_free(imf);
144308b68b0eSGleb Smirnoff 			break;
14442d9cfabaSRobert Watson 		}
144559854ecfSHans Petter Selasky 
144659854ecfSHans Petter Selasky 		ip_mfilter_insert(&imo->imo_head, imf);
1447a9771948SGleb Smirnoff 		imo->imo_multicast_ifp = ifp;
1448a9771948SGleb Smirnoff 		imo->imo_multicast_ttl = CARP_DFLTTL;
1449a9771948SGleb Smirnoff 		imo->imo_multicast_loop = 0;
1450a9771948SGleb Smirnoff 		break;
1451a9771948SGleb Smirnoff 	   }
145208b68b0eSGleb Smirnoff #endif
145308b68b0eSGleb Smirnoff #ifdef INET6
145408b68b0eSGleb Smirnoff 	case AF_INET6:
145508b68b0eSGleb Smirnoff 	    {
145608b68b0eSGleb Smirnoff 		struct ip6_moptions *im6o = &cif->cif_im6o;
145759854ecfSHans Petter Selasky 		struct in6_mfilter *im6f[2];
145808b68b0eSGleb Smirnoff 		struct in6_addr in6;
145933cde130SBruce M Simpson 
146059854ecfSHans Petter Selasky 		if (ip6_mfilter_first(&im6o->im6o_head))
146108b68b0eSGleb Smirnoff 			return (0);
146208b68b0eSGleb Smirnoff 
146359854ecfSHans Petter Selasky 		im6f[0] = ip6_mfilter_alloc(M_WAITOK, 0, 0);
146459854ecfSHans Petter Selasky 		im6f[1] = ip6_mfilter_alloc(M_WAITOK, 0, 0);
146559854ecfSHans Petter Selasky 
146659854ecfSHans Petter Selasky 		ip6_mfilter_init(&im6o->im6o_head);
146708b68b0eSGleb Smirnoff 		im6o->im6o_multicast_hlim = CARP_DFLTTL;
1468a9771948SGleb Smirnoff 		im6o->im6o_multicast_ifp = ifp;
1469a9771948SGleb Smirnoff 
147008b68b0eSGleb Smirnoff 		/* Join IPv6 CARP multicast group. */
1471a1f7e5f8SHajimu UMEMOTO 		bzero(&in6, sizeof(in6));
1472a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr16[0] = htons(0xff02);
1473a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr8[15] = 0x12;
147408b68b0eSGleb Smirnoff 		if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
147559854ecfSHans Petter Selasky 			ip6_mfilter_free(im6f[0]);
147659854ecfSHans Petter Selasky 			ip6_mfilter_free(im6f[1]);
147708b68b0eSGleb Smirnoff 			break;
147808b68b0eSGleb Smirnoff 		}
147959854ecfSHans Petter Selasky 		if ((error = in6_joingroup(ifp, &in6, NULL, &im6f[0]->im6f_in6m, 0)) != 0) {
148059854ecfSHans Petter Selasky 			ip6_mfilter_free(im6f[0]);
148159854ecfSHans Petter Selasky 			ip6_mfilter_free(im6f[1]);
148208b68b0eSGleb Smirnoff 			break;
148308b68b0eSGleb Smirnoff 		}
1484a9771948SGleb Smirnoff 
148508b68b0eSGleb Smirnoff 		/* Join solicited multicast address. */
1486a1f7e5f8SHajimu UMEMOTO 		bzero(&in6, sizeof(in6));
1487a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr16[0] = htons(0xff02);
1488a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr32[1] = 0;
1489a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr32[2] = htonl(1);
149008b68b0eSGleb Smirnoff 		in6.s6_addr32[3] = 0;
1491a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr8[12] = 0xff;
149259854ecfSHans Petter Selasky 
149308b68b0eSGleb Smirnoff 		if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
149459854ecfSHans Petter Selasky 			ip6_mfilter_free(im6f[0]);
149559854ecfSHans Petter Selasky 			ip6_mfilter_free(im6f[1]);
149608b68b0eSGleb Smirnoff 			break;
149708b68b0eSGleb Smirnoff 		}
149859854ecfSHans Petter Selasky 
149959854ecfSHans Petter Selasky 		if ((error = in6_joingroup(ifp, &in6, NULL, &im6f[1]->im6f_in6m, 0)) != 0) {
150059854ecfSHans Petter Selasky 			in6_leavegroup(im6f[0]->im6f_in6m, NULL);
150159854ecfSHans Petter Selasky 			ip6_mfilter_free(im6f[0]);
150259854ecfSHans Petter Selasky 			ip6_mfilter_free(im6f[1]);
150308b68b0eSGleb Smirnoff 			break;
150408b68b0eSGleb Smirnoff 		}
150559854ecfSHans Petter Selasky 		ip6_mfilter_insert(&im6o->im6o_head, im6f[0]);
150659854ecfSHans Petter Selasky 		ip6_mfilter_insert(&im6o->im6o_head, im6f[1]);
1507a9771948SGleb Smirnoff 		break;
1508a9771948SGleb Smirnoff 	    }
1509a9771948SGleb Smirnoff #endif
151008b68b0eSGleb Smirnoff 	}
151108b68b0eSGleb Smirnoff 
151208b68b0eSGleb Smirnoff 	return (error);
1513a9771948SGleb Smirnoff }
1514a9771948SGleb Smirnoff 
1515a9771948SGleb Smirnoff /*
151608b68b0eSGleb Smirnoff  * Free multicast structures.
1517a9771948SGleb Smirnoff  */
15185c1f0f6dSGleb Smirnoff static void
1519a9a2c40cSGleb Smirnoff carp_multicast_cleanup(struct carp_if *cif, sa_family_t sa)
1520a9771948SGleb Smirnoff {
152159854ecfSHans Petter Selasky #ifdef INET
152259854ecfSHans Petter Selasky 	struct ip_moptions *imo = &cif->cif_imo;
152359854ecfSHans Petter Selasky 	struct in_mfilter *imf;
152459854ecfSHans Petter Selasky #endif
152559854ecfSHans Petter Selasky #ifdef INET6
152659854ecfSHans Petter Selasky 	struct ip6_moptions *im6o = &cif->cif_im6o;
152759854ecfSHans Petter Selasky 	struct in6_mfilter *im6f;
152859854ecfSHans Petter Selasky #endif
15299c2cd1aaSGleb Smirnoff 	sx_assert(&carp_sx, SA_XLOCKED);
15309c2cd1aaSGleb Smirnoff 
153108b68b0eSGleb Smirnoff 	switch (sa) {
153208b68b0eSGleb Smirnoff #ifdef INET
153308b68b0eSGleb Smirnoff 	case AF_INET:
153459854ecfSHans Petter Selasky 		if (cif->cif_naddrs != 0)
153559854ecfSHans Petter Selasky 			break;
153608b68b0eSGleb Smirnoff 
153759854ecfSHans Petter Selasky 		while ((imf = ip_mfilter_first(&imo->imo_head)) != NULL) {
153859854ecfSHans Petter Selasky 			ip_mfilter_remove(&imo->imo_head, imf);
153959854ecfSHans Petter Selasky 			in_leavegroup(imf->imf_inm, NULL);
154059854ecfSHans Petter Selasky 			ip_mfilter_free(imf);
154108b68b0eSGleb Smirnoff 		}
154208b68b0eSGleb Smirnoff 		break;
1543a9771948SGleb Smirnoff #endif
154408b68b0eSGleb Smirnoff #ifdef INET6
154508b68b0eSGleb Smirnoff 	case AF_INET6:
154659854ecfSHans Petter Selasky 		if (cif->cif_naddrs6 != 0)
154759854ecfSHans Petter Selasky 			break;
154808b68b0eSGleb Smirnoff 
154959854ecfSHans Petter Selasky 		while ((im6f = ip6_mfilter_first(&im6o->im6o_head)) != NULL) {
155059854ecfSHans Petter Selasky 			ip6_mfilter_remove(&im6o->im6o_head, im6f);
155159854ecfSHans Petter Selasky 			in6_leavegroup(im6f->im6f_in6m, NULL);
155259854ecfSHans Petter Selasky 			ip6_mfilter_free(im6f);
155308b68b0eSGleb Smirnoff 		}
155408b68b0eSGleb Smirnoff 		break;
155508b68b0eSGleb Smirnoff #endif
155608b68b0eSGleb Smirnoff 	}
1557a9771948SGleb Smirnoff }
1558a9771948SGleb Smirnoff 
1559a9771948SGleb Smirnoff int
156047e8d432SGleb Smirnoff carp_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa)
1561a9771948SGleb Smirnoff {
1562a9771948SGleb Smirnoff 	struct m_tag *mtag;
1563a9771948SGleb Smirnoff 	struct carp_softc *sc;
1564a9771948SGleb Smirnoff 
1565a9771948SGleb Smirnoff 	if (!sa)
1566a9771948SGleb Smirnoff 		return (0);
1567a9771948SGleb Smirnoff 
1568a9771948SGleb Smirnoff 	switch (sa->sa_family) {
1569a9771948SGleb Smirnoff #ifdef INET
1570a9771948SGleb Smirnoff 	case AF_INET:
1571a9771948SGleb Smirnoff 		break;
157208b68b0eSGleb Smirnoff #endif
1573a9771948SGleb Smirnoff #ifdef INET6
1574a9771948SGleb Smirnoff 	case AF_INET6:
1575a9771948SGleb Smirnoff 		break;
157608b68b0eSGleb Smirnoff #endif
1577a9771948SGleb Smirnoff 	default:
1578a9771948SGleb Smirnoff 		return (0);
1579a9771948SGleb Smirnoff 	}
1580a9771948SGleb Smirnoff 
1581a9771948SGleb Smirnoff 	mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
1582a9771948SGleb Smirnoff 	if (mtag == NULL)
1583a9771948SGleb Smirnoff 		return (0);
1584a9771948SGleb Smirnoff 
1585eaf151c4SGleb Smirnoff 	bcopy(mtag + 1, &sc, sizeof(sc));
1586a9771948SGleb Smirnoff 
1587*13781800SKristof Provost 	switch (sa->sa_family) {
1588*13781800SKristof Provost 	case AF_INET:
1589*13781800SKristof Provost 		if (! IN_MULTICAST(sc->sc_carpaddr.s_addr))
1590*13781800SKristof Provost 			return (0);
1591*13781800SKristof Provost 		break;
1592*13781800SKristof Provost 	case AF_INET6:
1593*13781800SKristof Provost 		if (! IN6_IS_ADDR_MULTICAST(&sc->sc_carpaddr6))
1594*13781800SKristof Provost 			return (0);
1595*13781800SKristof Provost 		break;
1596*13781800SKristof Provost 	default:
1597*13781800SKristof Provost 		panic("Unknown af");
1598*13781800SKristof Provost 	}
1599*13781800SKristof Provost 
160008b68b0eSGleb Smirnoff 	/* Set the source MAC address to the Virtual Router MAC Address. */
1601a9771948SGleb Smirnoff 	switch (ifp->if_type) {
1602630481bbSYaroslav Tykhiy 	case IFT_ETHER:
16039ca1fe0dSGleb Smirnoff 	case IFT_BRIDGE:
1604630481bbSYaroslav Tykhiy 	case IFT_L2VLAN: {
1605a9771948SGleb Smirnoff 			struct ether_header *eh;
1606a9771948SGleb Smirnoff 
1607a9771948SGleb Smirnoff 			eh = mtod(m, struct ether_header *);
1608a9771948SGleb Smirnoff 			eh->ether_shost[0] = 0;
1609a9771948SGleb Smirnoff 			eh->ether_shost[1] = 0;
1610a9771948SGleb Smirnoff 			eh->ether_shost[2] = 0x5e;
1611a9771948SGleb Smirnoff 			eh->ether_shost[3] = 0;
1612a9771948SGleb Smirnoff 			eh->ether_shost[4] = 1;
1613a9771948SGleb Smirnoff 			eh->ether_shost[5] = sc->sc_vhid;
1614a9771948SGleb Smirnoff 		}
1615a9771948SGleb Smirnoff 		break;
1616a9771948SGleb Smirnoff 	default:
161708b68b0eSGleb Smirnoff 		printf("%s: carp is not supported for the %d interface type\n",
161808b68b0eSGleb Smirnoff 		    ifp->if_xname, ifp->if_type);
1619a9771948SGleb Smirnoff 		return (EOPNOTSUPP);
1620a9771948SGleb Smirnoff 	}
1621a9771948SGleb Smirnoff 
1622a9771948SGleb Smirnoff 	return (0);
1623a9771948SGleb Smirnoff }
1624a9771948SGleb Smirnoff 
162508b68b0eSGleb Smirnoff static struct carp_softc*
162608b68b0eSGleb Smirnoff carp_alloc(struct ifnet *ifp)
1627a9771948SGleb Smirnoff {
162808b68b0eSGleb Smirnoff 	struct carp_softc *sc;
162908b68b0eSGleb Smirnoff 	struct carp_if *cif;
1630d220759bSGleb Smirnoff 
163181098a01SAlexander Motin 	sx_assert(&carp_sx, SA_XLOCKED);
163281098a01SAlexander Motin 
16330cc726f2SGleb Smirnoff 	if ((cif = ifp->if_carp) == NULL)
163408b68b0eSGleb Smirnoff 		cif = carp_alloc_if(ifp);
1635a9771948SGleb Smirnoff 
163608b68b0eSGleb Smirnoff 	sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
163708b68b0eSGleb Smirnoff 
163808b68b0eSGleb Smirnoff 	sc->sc_advbase = CARP_DFLTINTV;
163908b68b0eSGleb Smirnoff 	sc->sc_vhid = -1;	/* required setting */
164008b68b0eSGleb Smirnoff 	sc->sc_init_counter = 1;
164108b68b0eSGleb Smirnoff 	sc->sc_state = INIT;
164208b68b0eSGleb Smirnoff 
164308b68b0eSGleb Smirnoff 	sc->sc_ifasiz = sizeof(struct ifaddr *);
164408b68b0eSGleb Smirnoff 	sc->sc_ifas = malloc(sc->sc_ifasiz, M_CARP, M_WAITOK|M_ZERO);
164508b68b0eSGleb Smirnoff 	sc->sc_carpdev = ifp;
164608b68b0eSGleb Smirnoff 
1647*13781800SKristof Provost 	sc->sc_carpaddr.s_addr = htonl(INADDR_CARP_GROUP);
1648*13781800SKristof Provost 	sc->sc_carpaddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
1649*13781800SKristof Provost 	sc->sc_carpaddr6.s6_addr8[15] = 0x12;
1650*13781800SKristof Provost 
165108b68b0eSGleb Smirnoff 	CARP_LOCK_INIT(sc);
165208b68b0eSGleb Smirnoff #ifdef INET
165308b68b0eSGleb Smirnoff 	callout_init_mtx(&sc->sc_md_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
165408b68b0eSGleb Smirnoff #endif
165508b68b0eSGleb Smirnoff #ifdef INET6
165608b68b0eSGleb Smirnoff 	callout_init_mtx(&sc->sc_md6_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
165708b68b0eSGleb Smirnoff #endif
165808b68b0eSGleb Smirnoff 	callout_init_mtx(&sc->sc_ad_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
165908b68b0eSGleb Smirnoff 
166008b68b0eSGleb Smirnoff 	CIF_LOCK(cif);
166108b68b0eSGleb Smirnoff 	TAILQ_INSERT_TAIL(&cif->cif_vrs, sc, sc_list);
166208b68b0eSGleb Smirnoff 	CIF_UNLOCK(cif);
166308b68b0eSGleb Smirnoff 
166408b68b0eSGleb Smirnoff 	mtx_lock(&carp_mtx);
166508b68b0eSGleb Smirnoff 	LIST_INSERT_HEAD(&carp_list, sc, sc_next);
166608b68b0eSGleb Smirnoff 	mtx_unlock(&carp_mtx);
166708b68b0eSGleb Smirnoff 
166808b68b0eSGleb Smirnoff 	return (sc);
166908b68b0eSGleb Smirnoff }
167008b68b0eSGleb Smirnoff 
16719c2cd1aaSGleb Smirnoff static void
167208b68b0eSGleb Smirnoff carp_grow_ifas(struct carp_softc *sc)
167308b68b0eSGleb Smirnoff {
167408b68b0eSGleb Smirnoff 	struct ifaddr **new;
167508b68b0eSGleb Smirnoff 
16769c2cd1aaSGleb Smirnoff 	new = malloc(sc->sc_ifasiz * 2, M_CARP, M_WAITOK | M_ZERO);
16779c2cd1aaSGleb Smirnoff 	CARP_LOCK(sc);
167808b68b0eSGleb Smirnoff 	bcopy(sc->sc_ifas, new, sc->sc_ifasiz);
167908b68b0eSGleb Smirnoff 	free(sc->sc_ifas, M_CARP);
168008b68b0eSGleb Smirnoff 	sc->sc_ifas = new;
168108b68b0eSGleb Smirnoff 	sc->sc_ifasiz *= 2;
16829c2cd1aaSGleb Smirnoff 	CARP_UNLOCK(sc);
168308b68b0eSGleb Smirnoff }
168408b68b0eSGleb Smirnoff 
168508b68b0eSGleb Smirnoff static void
168608b68b0eSGleb Smirnoff carp_destroy(struct carp_softc *sc)
168708b68b0eSGleb Smirnoff {
168808b68b0eSGleb Smirnoff 	struct ifnet *ifp = sc->sc_carpdev;
168908b68b0eSGleb Smirnoff 	struct carp_if *cif = ifp->if_carp;
169008b68b0eSGleb Smirnoff 
16919c2cd1aaSGleb Smirnoff 	sx_assert(&carp_sx, SA_XLOCKED);
1692a9a2c40cSGleb Smirnoff 
16939c2cd1aaSGleb Smirnoff 	if (sc->sc_suppress)
16949c2cd1aaSGleb Smirnoff 		carp_demote_adj(-V_carp_ifdown_adj, "vhid removed");
16959c2cd1aaSGleb Smirnoff 	CARP_UNLOCK(sc);
16969c2cd1aaSGleb Smirnoff 
16979c2cd1aaSGleb Smirnoff 	CIF_LOCK(cif);
169808b68b0eSGleb Smirnoff 	TAILQ_REMOVE(&cif->cif_vrs, sc, sc_list);
16999c2cd1aaSGleb Smirnoff 	CIF_UNLOCK(cif);
170008b68b0eSGleb Smirnoff 
170108b68b0eSGleb Smirnoff 	mtx_lock(&carp_mtx);
170208b68b0eSGleb Smirnoff 	LIST_REMOVE(sc, sc_next);
170308b68b0eSGleb Smirnoff 	mtx_unlock(&carp_mtx);
170408b68b0eSGleb Smirnoff 
170508b68b0eSGleb Smirnoff 	callout_drain(&sc->sc_ad_tmo);
170608b68b0eSGleb Smirnoff #ifdef INET
170708b68b0eSGleb Smirnoff 	callout_drain(&sc->sc_md_tmo);
170808b68b0eSGleb Smirnoff #endif
170908b68b0eSGleb Smirnoff #ifdef INET6
171008b68b0eSGleb Smirnoff 	callout_drain(&sc->sc_md6_tmo);
171108b68b0eSGleb Smirnoff #endif
171208b68b0eSGleb Smirnoff 	CARP_LOCK_DESTROY(sc);
171308b68b0eSGleb Smirnoff 
171408b68b0eSGleb Smirnoff 	free(sc->sc_ifas, M_CARP);
171508b68b0eSGleb Smirnoff 	free(sc, M_CARP);
171608b68b0eSGleb Smirnoff }
171708b68b0eSGleb Smirnoff 
171808b68b0eSGleb Smirnoff static struct carp_if*
171908b68b0eSGleb Smirnoff carp_alloc_if(struct ifnet *ifp)
1720a9771948SGleb Smirnoff {
172154bfbd51SWill Andrews 	struct carp_if *cif;
17220cc726f2SGleb Smirnoff 	int error;
1723d220759bSGleb Smirnoff 
172408b68b0eSGleb Smirnoff 	cif = malloc(sizeof(*cif), M_CARP, M_WAITOK|M_ZERO);
172508b68b0eSGleb Smirnoff 
17260cc726f2SGleb Smirnoff 	if ((error = ifpromisc(ifp, 1)) != 0)
17270cc726f2SGleb Smirnoff 		printf("%s: ifpromisc(%s) failed: %d\n",
17280cc726f2SGleb Smirnoff 		    __func__, ifp->if_xname, error);
17290cc726f2SGleb Smirnoff 	else
17300cc726f2SGleb Smirnoff 		cif->cif_flags |= CIF_PROMISC;
173108b68b0eSGleb Smirnoff 
173208b68b0eSGleb Smirnoff 	CIF_LOCK_INIT(cif);
173308b68b0eSGleb Smirnoff 	cif->cif_ifp = ifp;
173408b68b0eSGleb Smirnoff 	TAILQ_INIT(&cif->cif_vrs);
173508b68b0eSGleb Smirnoff 
1736137f91e8SJohn Baldwin 	IF_ADDR_WLOCK(ifp);
173708b68b0eSGleb Smirnoff 	ifp->if_carp = cif;
173808b68b0eSGleb Smirnoff 	if_ref(ifp);
1739137f91e8SJohn Baldwin 	IF_ADDR_WUNLOCK(ifp);
174008b68b0eSGleb Smirnoff 
174108b68b0eSGleb Smirnoff 	return (cif);
1742d220759bSGleb Smirnoff }
1743d220759bSGleb Smirnoff 
1744d220759bSGleb Smirnoff static void
174508b68b0eSGleb Smirnoff carp_free_if(struct carp_if *cif)
174608b68b0eSGleb Smirnoff {
174708b68b0eSGleb Smirnoff 	struct ifnet *ifp = cif->cif_ifp;
174808b68b0eSGleb Smirnoff 
174908b68b0eSGleb Smirnoff 	CIF_LOCK_ASSERT(cif);
175008b68b0eSGleb Smirnoff 	KASSERT(TAILQ_EMPTY(&cif->cif_vrs), ("%s: softc list not empty",
175108b68b0eSGleb Smirnoff 	    __func__));
175208b68b0eSGleb Smirnoff 
1753137f91e8SJohn Baldwin 	IF_ADDR_WLOCK(ifp);
175408b68b0eSGleb Smirnoff 	ifp->if_carp = NULL;
1755137f91e8SJohn Baldwin 	IF_ADDR_WUNLOCK(ifp);
175608b68b0eSGleb Smirnoff 
175708b68b0eSGleb Smirnoff 	CIF_LOCK_DESTROY(cif);
175808b68b0eSGleb Smirnoff 
17590cc726f2SGleb Smirnoff 	if (cif->cif_flags & CIF_PROMISC)
176008b68b0eSGleb Smirnoff 		ifpromisc(ifp, 0);
17611f6addd9SMikolaj Golub 	if_rele(ifp);
176208b68b0eSGleb Smirnoff 
176308b68b0eSGleb Smirnoff 	free(cif, M_CARP);
176408b68b0eSGleb Smirnoff }
176508b68b0eSGleb Smirnoff 
176640e04359SKristof Provost static bool
176740e04359SKristof Provost carp_carprcp(void *arg, struct carp_softc *sc, int priv)
176808b68b0eSGleb Smirnoff {
176940e04359SKristof Provost 	struct carpreq *carpr = arg;
177008b68b0eSGleb Smirnoff 
177108b68b0eSGleb Smirnoff 	CARP_LOCK(sc);
177208b68b0eSGleb Smirnoff 	carpr->carpr_state = sc->sc_state;
177308b68b0eSGleb Smirnoff 	carpr->carpr_vhid = sc->sc_vhid;
177408b68b0eSGleb Smirnoff 	carpr->carpr_advbase = sc->sc_advbase;
177508b68b0eSGleb Smirnoff 	carpr->carpr_advskew = sc->sc_advskew;
177608b68b0eSGleb Smirnoff 	if (priv)
177708b68b0eSGleb Smirnoff 		bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key));
177808b68b0eSGleb Smirnoff 	else
177908b68b0eSGleb Smirnoff 		bzero(carpr->carpr_key, sizeof(carpr->carpr_key));
178008b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
178140e04359SKristof Provost 
178240e04359SKristof Provost 	return (true);
178308b68b0eSGleb Smirnoff }
178408b68b0eSGleb Smirnoff 
178540e04359SKristof Provost static int
1786*13781800SKristof Provost carp_ioctl_set(if_t ifp, struct carpkreq *carpr)
178708b68b0eSGleb Smirnoff {
178849cad3daSZhenlei Huang 	struct epoch_tracker et;
178908b68b0eSGleb Smirnoff 	struct carp_softc *sc = NULL;
179040e04359SKristof Provost 	int error = 0;
179108b68b0eSGleb Smirnoff 
179208b68b0eSGleb Smirnoff 
179340e04359SKristof Provost 	if (carpr->carpr_vhid <= 0 || carpr->carpr_vhid > CARP_MAXVHID ||
179440e04359SKristof Provost 	    carpr->carpr_advbase < 0 || carpr->carpr_advskew < 0) {
179540e04359SKristof Provost 		return (EINVAL);
179608b68b0eSGleb Smirnoff 	}
179708b68b0eSGleb Smirnoff 
179808b68b0eSGleb Smirnoff 	if (ifp->if_carp) {
179908b68b0eSGleb Smirnoff 		IFNET_FOREACH_CARP(ifp, sc)
180040e04359SKristof Provost 			if (sc->sc_vhid == carpr->carpr_vhid)
180108b68b0eSGleb Smirnoff 				break;
180208b68b0eSGleb Smirnoff 	}
180308b68b0eSGleb Smirnoff 	if (sc == NULL) {
180408b68b0eSGleb Smirnoff 		sc = carp_alloc(ifp);
180508b68b0eSGleb Smirnoff 		CARP_LOCK(sc);
180640e04359SKristof Provost 		sc->sc_vhid = carpr->carpr_vhid;
180708b68b0eSGleb Smirnoff 		LLADDR(&sc->sc_addr)[0] = 0;
180808b68b0eSGleb Smirnoff 		LLADDR(&sc->sc_addr)[1] = 0;
180908b68b0eSGleb Smirnoff 		LLADDR(&sc->sc_addr)[2] = 0x5e;
181008b68b0eSGleb Smirnoff 		LLADDR(&sc->sc_addr)[3] = 0;
181108b68b0eSGleb Smirnoff 		LLADDR(&sc->sc_addr)[4] = 1;
181208b68b0eSGleb Smirnoff 		LLADDR(&sc->sc_addr)[5] = sc->sc_vhid;
181308b68b0eSGleb Smirnoff 	} else
181408b68b0eSGleb Smirnoff 		CARP_LOCK(sc);
181540e04359SKristof Provost 	if (carpr->carpr_advbase > 0) {
181640e04359SKristof Provost 		if (carpr->carpr_advbase > 255 ||
181740e04359SKristof Provost 		    carpr->carpr_advbase < CARP_DFLTINTV) {
181808b68b0eSGleb Smirnoff 			error = EINVAL;
181940e04359SKristof Provost 			goto out;
182008b68b0eSGleb Smirnoff 		}
182140e04359SKristof Provost 		sc->sc_advbase = carpr->carpr_advbase;
182208b68b0eSGleb Smirnoff 	}
182340e04359SKristof Provost 	if (carpr->carpr_advskew >= 255) {
182408b68b0eSGleb Smirnoff 		error = EINVAL;
182540e04359SKristof Provost 		goto out;
182608b68b0eSGleb Smirnoff 	}
182740e04359SKristof Provost 	sc->sc_advskew = carpr->carpr_advskew;
1828*13781800SKristof Provost 	if (carpr->carpr_addr.s_addr != INADDR_ANY)
1829*13781800SKristof Provost 		sc->sc_carpaddr = carpr->carpr_addr;
1830*13781800SKristof Provost 	if (! IN6_IS_ADDR_UNSPECIFIED(&carpr->carpr_addr6)) {
1831*13781800SKristof Provost 		memcpy(&sc->sc_carpaddr6, &carpr->carpr_addr6,
1832*13781800SKristof Provost 		    sizeof(sc->sc_carpaddr6));
1833*13781800SKristof Provost 	}
183440e04359SKristof Provost 	if (carpr->carpr_key[0] != '\0') {
183540e04359SKristof Provost 		bcopy(carpr->carpr_key, sc->sc_key, sizeof(sc->sc_key));
183608b68b0eSGleb Smirnoff 		carp_hmac_prepare(sc);
183708b68b0eSGleb Smirnoff 	}
183808b68b0eSGleb Smirnoff 	if (sc->sc_state != INIT &&
183940e04359SKristof Provost 	    carpr->carpr_state != sc->sc_state) {
184040e04359SKristof Provost 		switch (carpr->carpr_state) {
184108b68b0eSGleb Smirnoff 		case BACKUP:
184208b68b0eSGleb Smirnoff 			callout_stop(&sc->sc_ad_tmo);
1843369a6708SWill Andrews 			carp_set_state(sc, BACKUP,
1844369a6708SWill Andrews 			    "user requested via ifconfig");
184508b68b0eSGleb Smirnoff 			carp_setrun(sc, 0);
184608b68b0eSGleb Smirnoff 			carp_delroute(sc);
184708b68b0eSGleb Smirnoff 			break;
184808b68b0eSGleb Smirnoff 		case MASTER:
184949cad3daSZhenlei Huang 			NET_EPOCH_ENTER(et);
1850369a6708SWill Andrews 			carp_master_down_locked(sc,
1851369a6708SWill Andrews 			    "user requested via ifconfig");
185249cad3daSZhenlei Huang 			NET_EPOCH_EXIT(et);
185308b68b0eSGleb Smirnoff 			break;
185408b68b0eSGleb Smirnoff 		default:
185508b68b0eSGleb Smirnoff 			break;
185608b68b0eSGleb Smirnoff 		}
185708b68b0eSGleb Smirnoff 	}
185808b68b0eSGleb Smirnoff 
185940e04359SKristof Provost out:
186040e04359SKristof Provost 	CARP_UNLOCK(sc);
186140e04359SKristof Provost 
186240e04359SKristof Provost 	return (error);
186340e04359SKristof Provost }
186440e04359SKristof Provost 
186540e04359SKristof Provost static int
186640e04359SKristof Provost carp_ioctl_get(if_t ifp, struct ucred *cred, struct carpreq *carpr,
186740e04359SKristof Provost     bool (*outfn)(void *, struct carp_softc *, int), void *arg)
186808b68b0eSGleb Smirnoff {
186908b68b0eSGleb Smirnoff 	int priveleged;
187040e04359SKristof Provost 	struct carp_softc *sc;
187108b68b0eSGleb Smirnoff 
187240e04359SKristof Provost 	if (carpr->carpr_vhid < 0 || carpr->carpr_vhid > CARP_MAXVHID)
187340e04359SKristof Provost 		return (EINVAL);
187440e04359SKristof Provost 	if (carpr->carpr_count < 1)
187540e04359SKristof Provost 		return (EMSGSIZE);
187640e04359SKristof Provost 	if (ifp->if_carp == NULL)
187740e04359SKristof Provost 		return (ENOENT);
187808b68b0eSGleb Smirnoff 
187940e04359SKristof Provost 	priveleged = (priv_check_cred(cred, PRIV_NETINET_CARP) == 0);
188040e04359SKristof Provost 	if (carpr->carpr_vhid != 0) {
188108b68b0eSGleb Smirnoff 		IFNET_FOREACH_CARP(ifp, sc)
188240e04359SKristof Provost 			if (sc->sc_vhid == carpr->carpr_vhid)
188308b68b0eSGleb Smirnoff 				break;
188440e04359SKristof Provost 		if (sc == NULL)
188540e04359SKristof Provost 			return (ENOENT);
188640e04359SKristof Provost 
188740e04359SKristof Provost 		if (! outfn(arg, sc, priveleged))
188840e04359SKristof Provost 			return (ENOMEM);
188940e04359SKristof Provost 		carpr->carpr_count = 1;
189008b68b0eSGleb Smirnoff 	} else  {
189140e04359SKristof Provost 		int count;
189208b68b0eSGleb Smirnoff 
189308b68b0eSGleb Smirnoff 		count = 0;
189408b68b0eSGleb Smirnoff 		IFNET_FOREACH_CARP(ifp, sc)
189508b68b0eSGleb Smirnoff 			count++;
189608b68b0eSGleb Smirnoff 
189740e04359SKristof Provost 		if (count > carpr->carpr_count)
189840e04359SKristof Provost 			return (EMSGSIZE);
189940e04359SKristof Provost 
190040e04359SKristof Provost 		IFNET_FOREACH_CARP(ifp, sc) {
190140e04359SKristof Provost 			if (! outfn(arg, sc, priveleged))
190240e04359SKristof Provost 				return (ENOMEM);
190340e04359SKristof Provost 			carpr->carpr_count = count;
190440e04359SKristof Provost 		}
190508b68b0eSGleb Smirnoff 	}
190608b68b0eSGleb Smirnoff 
190740e04359SKristof Provost 	return (0);
190840e04359SKristof Provost }
190940e04359SKristof Provost 
191040e04359SKristof Provost int
191140e04359SKristof Provost carp_ioctl(struct ifreq *ifr, u_long cmd, struct thread *td)
191240e04359SKristof Provost {
191340e04359SKristof Provost 	struct carpreq carpr;
1914*13781800SKristof Provost 	struct carpkreq carprk = { };
191540e04359SKristof Provost 	struct ifnet *ifp;
191640e04359SKristof Provost 	int error = 0;
191740e04359SKristof Provost 
191840e04359SKristof Provost 	if ((error = copyin(ifr_data_get_ptr(ifr), &carpr, sizeof carpr)))
191940e04359SKristof Provost 		return (error);
192040e04359SKristof Provost 
192140e04359SKristof Provost 	ifp = ifunit_ref(ifr->ifr_name);
192240e04359SKristof Provost 	if ((error = carp_is_supported_if(ifp)) != 0)
192340e04359SKristof Provost 		goto out;
192440e04359SKristof Provost 
192540e04359SKristof Provost 	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
192640e04359SKristof Provost 		error = EADDRNOTAVAIL;
192740e04359SKristof Provost 		goto out;
192840e04359SKristof Provost 	}
192940e04359SKristof Provost 
193040e04359SKristof Provost 	sx_xlock(&carp_sx);
193140e04359SKristof Provost 	switch (cmd) {
193240e04359SKristof Provost 	case SIOCSVH:
193340e04359SKristof Provost 		if ((error = priv_check(td, PRIV_NETINET_CARP)))
193440e04359SKristof Provost 			break;
193540e04359SKristof Provost 
1936*13781800SKristof Provost 		memcpy(&carprk, &carpr, sizeof(carpr));
1937*13781800SKristof Provost 		error = carp_ioctl_set(ifp, &carprk);
193840e04359SKristof Provost 		break;
193940e04359SKristof Provost 
194040e04359SKristof Provost 	case SIOCGVH:
194140e04359SKristof Provost 		error = carp_ioctl_get(ifp, td->td_ucred, &carpr,
194240e04359SKristof Provost 		    carp_carprcp, &carpr);
194340e04359SKristof Provost 		if (error == 0) {
1944541d96aaSBrooks Davis 			error = copyout(&carpr,
194540e04359SKristof Provost 			    (char *)ifr_data_get_ptr(ifr),
194640e04359SKristof Provost 			    carpr.carpr_count * sizeof(carpr));
194708b68b0eSGleb Smirnoff 		}
194808b68b0eSGleb Smirnoff 		break;
194908b68b0eSGleb Smirnoff 	default:
195008b68b0eSGleb Smirnoff 		error = EINVAL;
195108b68b0eSGleb Smirnoff 	}
195293d4534cSGleb Smirnoff 	sx_xunlock(&carp_sx);
195308b68b0eSGleb Smirnoff 
195408b68b0eSGleb Smirnoff out:
195540e04359SKristof Provost 	if (ifp != NULL)
195608b68b0eSGleb Smirnoff 		if_rele(ifp);
195708b68b0eSGleb Smirnoff 
195808b68b0eSGleb Smirnoff 	return (error);
195908b68b0eSGleb Smirnoff }
196008b68b0eSGleb Smirnoff 
196108b68b0eSGleb Smirnoff static int
196208b68b0eSGleb Smirnoff carp_get_vhid(struct ifaddr *ifa)
196308b68b0eSGleb Smirnoff {
196408b68b0eSGleb Smirnoff 
196508b68b0eSGleb Smirnoff 	if (ifa == NULL || ifa->ifa_carp == NULL)
196608b68b0eSGleb Smirnoff 		return (0);
196708b68b0eSGleb Smirnoff 
196808b68b0eSGleb Smirnoff 	return (ifa->ifa_carp->sc_vhid);
196908b68b0eSGleb Smirnoff }
197008b68b0eSGleb Smirnoff 
197108b68b0eSGleb Smirnoff int
197208b68b0eSGleb Smirnoff carp_attach(struct ifaddr *ifa, int vhid)
197308b68b0eSGleb Smirnoff {
197408b68b0eSGleb Smirnoff 	struct ifnet *ifp = ifa->ifa_ifp;
1975a9a2c40cSGleb Smirnoff 	struct carp_if *cif = ifp->if_carp;
197608b68b0eSGleb Smirnoff 	struct carp_softc *sc;
197708b68b0eSGleb Smirnoff 	int index, error;
197808b68b0eSGleb Smirnoff 
19799c2cd1aaSGleb Smirnoff 	KASSERT(ifa->ifa_carp == NULL, ("%s: ifa %p attached", __func__, ifa));
198008b68b0eSGleb Smirnoff 
198108b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
198208b68b0eSGleb Smirnoff #ifdef INET
198308b68b0eSGleb Smirnoff 	case AF_INET:
198408b68b0eSGleb Smirnoff #endif
198508b68b0eSGleb Smirnoff #ifdef INET6
198608b68b0eSGleb Smirnoff 	case AF_INET6:
198708b68b0eSGleb Smirnoff #endif
198808b68b0eSGleb Smirnoff 		break;
198908b68b0eSGleb Smirnoff 	default:
199008b68b0eSGleb Smirnoff 		return (EPROTOTYPE);
199108b68b0eSGleb Smirnoff 	}
199208b68b0eSGleb Smirnoff 
19939c2cd1aaSGleb Smirnoff 	sx_xlock(&carp_sx);
19949c2cd1aaSGleb Smirnoff 	if (ifp->if_carp == NULL) {
19959c2cd1aaSGleb Smirnoff 		sx_xunlock(&carp_sx);
19969c2cd1aaSGleb Smirnoff 		return (ENOPROTOOPT);
19979c2cd1aaSGleb Smirnoff 	}
19989c2cd1aaSGleb Smirnoff 
199908b68b0eSGleb Smirnoff 	IFNET_FOREACH_CARP(ifp, sc)
200008b68b0eSGleb Smirnoff 		if (sc->sc_vhid == vhid)
200108b68b0eSGleb Smirnoff 			break;
2002a9a2c40cSGleb Smirnoff 	if (sc == NULL) {
20039c2cd1aaSGleb Smirnoff 		sx_xunlock(&carp_sx);
200408b68b0eSGleb Smirnoff 		return (ENOENT);
2005a9a2c40cSGleb Smirnoff 	}
200608b68b0eSGleb Smirnoff 
2007a9a2c40cSGleb Smirnoff 	error = carp_multicast_setup(cif, ifa->ifa_addr->sa_family);
2008a9a2c40cSGleb Smirnoff 	if (error) {
2009a9a2c40cSGleb Smirnoff 		CIF_FREE(cif);
20109c2cd1aaSGleb Smirnoff 		sx_xunlock(&carp_sx);
201108b68b0eSGleb Smirnoff 		return (error);
2012a9a2c40cSGleb Smirnoff 	}
201308b68b0eSGleb Smirnoff 
201408b68b0eSGleb Smirnoff 	index = sc->sc_naddrs + sc->sc_naddrs6 + 1;
201508b68b0eSGleb Smirnoff 	if (index > sc->sc_ifasiz / sizeof(struct ifaddr *))
20169c2cd1aaSGleb Smirnoff 		carp_grow_ifas(sc);
201708b68b0eSGleb Smirnoff 
201808b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
201908b68b0eSGleb Smirnoff #ifdef INET
202008b68b0eSGleb Smirnoff 	case AF_INET:
2021a9a2c40cSGleb Smirnoff 		cif->cif_naddrs++;
202208b68b0eSGleb Smirnoff 		sc->sc_naddrs++;
202308b68b0eSGleb Smirnoff 		break;
202408b68b0eSGleb Smirnoff #endif
202508b68b0eSGleb Smirnoff #ifdef INET6
202608b68b0eSGleb Smirnoff 	case AF_INET6:
2027a9a2c40cSGleb Smirnoff 		cif->cif_naddrs6++;
202808b68b0eSGleb Smirnoff 		sc->sc_naddrs6++;
202908b68b0eSGleb Smirnoff 		break;
203008b68b0eSGleb Smirnoff #endif
203108b68b0eSGleb Smirnoff 	}
203208b68b0eSGleb Smirnoff 
203308b68b0eSGleb Smirnoff 	ifa_ref(ifa);
20349c2cd1aaSGleb Smirnoff 
20359c2cd1aaSGleb Smirnoff 	CARP_LOCK(sc);
203608b68b0eSGleb Smirnoff 	sc->sc_ifas[index - 1] = ifa;
203708b68b0eSGleb Smirnoff 	ifa->ifa_carp = sc;
203808b68b0eSGleb Smirnoff 	carp_hmac_prepare(sc);
203908b68b0eSGleb Smirnoff 	carp_sc_state(sc);
204008b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
20419c2cd1aaSGleb Smirnoff 
20429c2cd1aaSGleb Smirnoff 	sx_xunlock(&carp_sx);
204308b68b0eSGleb Smirnoff 
204408b68b0eSGleb Smirnoff 	return (0);
204508b68b0eSGleb Smirnoff }
204608b68b0eSGleb Smirnoff 
204708b68b0eSGleb Smirnoff void
2048338e227aSLuiz Otavio O Souza carp_detach(struct ifaddr *ifa, bool keep_cif)
204908b68b0eSGleb Smirnoff {
2050a9a2c40cSGleb Smirnoff 	struct ifnet *ifp = ifa->ifa_ifp;
2051a9a2c40cSGleb Smirnoff 	struct carp_if *cif = ifp->if_carp;
205208b68b0eSGleb Smirnoff 	struct carp_softc *sc = ifa->ifa_carp;
205308b68b0eSGleb Smirnoff 	int i, index;
205408b68b0eSGleb Smirnoff 
205508b68b0eSGleb Smirnoff 	KASSERT(sc != NULL, ("%s: %p not attached", __func__, ifa));
205608b68b0eSGleb Smirnoff 
20579c2cd1aaSGleb Smirnoff 	sx_xlock(&carp_sx);
205808b68b0eSGleb Smirnoff 
20599c2cd1aaSGleb Smirnoff 	CARP_LOCK(sc);
206008b68b0eSGleb Smirnoff 	/* Shift array. */
206108b68b0eSGleb Smirnoff 	index = sc->sc_naddrs + sc->sc_naddrs6;
206208b68b0eSGleb Smirnoff 	for (i = 0; i < index; i++)
206308b68b0eSGleb Smirnoff 		if (sc->sc_ifas[i] == ifa)
206408b68b0eSGleb Smirnoff 			break;
206508b68b0eSGleb Smirnoff 	KASSERT(i < index, ("%s: %p no backref", __func__, ifa));
206608b68b0eSGleb Smirnoff 	for (; i < index - 1; i++)
206708b68b0eSGleb Smirnoff 		sc->sc_ifas[i] = sc->sc_ifas[i+1];
206808b68b0eSGleb Smirnoff 	sc->sc_ifas[index - 1] = NULL;
206908b68b0eSGleb Smirnoff 
207008b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
207108b68b0eSGleb Smirnoff #ifdef INET
207208b68b0eSGleb Smirnoff 	case AF_INET:
2073a9a2c40cSGleb Smirnoff 		cif->cif_naddrs--;
207408b68b0eSGleb Smirnoff 		sc->sc_naddrs--;
207508b68b0eSGleb Smirnoff 		break;
207608b68b0eSGleb Smirnoff #endif
207708b68b0eSGleb Smirnoff #ifdef INET6
207808b68b0eSGleb Smirnoff 	case AF_INET6:
2079a9a2c40cSGleb Smirnoff 		cif->cif_naddrs6--;
208008b68b0eSGleb Smirnoff 		sc->sc_naddrs6--;
208108b68b0eSGleb Smirnoff 		break;
208208b68b0eSGleb Smirnoff #endif
208308b68b0eSGleb Smirnoff 	}
208408b68b0eSGleb Smirnoff 
20852512b096SGleb Smirnoff 	carp_ifa_delroute(ifa);
2086a9a2c40cSGleb Smirnoff 	carp_multicast_cleanup(cif, ifa->ifa_addr->sa_family);
208708b68b0eSGleb Smirnoff 
208808b68b0eSGleb Smirnoff 	ifa->ifa_carp = NULL;
208908b68b0eSGleb Smirnoff 	ifa_free(ifa);
209008b68b0eSGleb Smirnoff 
209108b68b0eSGleb Smirnoff 	carp_hmac_prepare(sc);
209208b68b0eSGleb Smirnoff 	carp_sc_state(sc);
209308b68b0eSGleb Smirnoff 
2094338e227aSLuiz Otavio O Souza 	if (!keep_cif && sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0)
209508b68b0eSGleb Smirnoff 		carp_destroy(sc);
20969c2cd1aaSGleb Smirnoff 	else
209708b68b0eSGleb Smirnoff 		CARP_UNLOCK(sc);
20989c2cd1aaSGleb Smirnoff 
2099338e227aSLuiz Otavio O Souza 	if (!keep_cif)
21009c2cd1aaSGleb Smirnoff 		CIF_FREE(cif);
21019c2cd1aaSGleb Smirnoff 
21029c2cd1aaSGleb Smirnoff 	sx_xunlock(&carp_sx);
210308b68b0eSGleb Smirnoff }
210408b68b0eSGleb Smirnoff 
210508b68b0eSGleb Smirnoff static void
2106d01641e2SWill Andrews carp_set_state(struct carp_softc *sc, int state, const char *reason)
210708b68b0eSGleb Smirnoff {
210808b68b0eSGleb Smirnoff 
210908b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
211008b68b0eSGleb Smirnoff 
211108b68b0eSGleb Smirnoff 	if (sc->sc_state != state) {
211208b68b0eSGleb Smirnoff 		const char *carp_states[] = { CARP_STATES };
211308b68b0eSGleb Smirnoff 		char subsys[IFNAMSIZ+5];
211408b68b0eSGleb Smirnoff 
211508b68b0eSGleb Smirnoff 		snprintf(subsys, IFNAMSIZ+5, "%u@%s", sc->sc_vhid,
211608b68b0eSGleb Smirnoff 		    sc->sc_carpdev->if_xname);
2117d01641e2SWill Andrews 
2118d01641e2SWill Andrews 		CARP_LOG("%s: %s -> %s (%s)\n", subsys,
2119d01641e2SWill Andrews 		    carp_states[sc->sc_state], carp_states[state], reason);
2120d01641e2SWill Andrews 
2121d01641e2SWill Andrews 		sc->sc_state = state;
2122d01641e2SWill Andrews 
212308b68b0eSGleb Smirnoff 		devctl_notify("CARP", subsys, carp_states[state], NULL);
212408b68b0eSGleb Smirnoff 	}
212508b68b0eSGleb Smirnoff }
212608b68b0eSGleb Smirnoff 
212708b68b0eSGleb Smirnoff static void
212808b68b0eSGleb Smirnoff carp_linkstate(struct ifnet *ifp)
2129d220759bSGleb Smirnoff {
2130d220759bSGleb Smirnoff 	struct carp_softc *sc;
2131d220759bSGleb Smirnoff 
213208b68b0eSGleb Smirnoff 	CIF_LOCK(ifp->if_carp);
213308b68b0eSGleb Smirnoff 	IFNET_FOREACH_CARP(ifp, sc) {
213408b68b0eSGleb Smirnoff 		CARP_LOCK(sc);
213508b68b0eSGleb Smirnoff 		carp_sc_state(sc);
213608b68b0eSGleb Smirnoff 		CARP_UNLOCK(sc);
213708b68b0eSGleb Smirnoff 	}
213808b68b0eSGleb Smirnoff 	CIF_UNLOCK(ifp->if_carp);
21394cb39345SGleb Smirnoff }
21404cb39345SGleb Smirnoff 
21414cb39345SGleb Smirnoff static void
214208b68b0eSGleb Smirnoff carp_sc_state(struct carp_softc *sc)
21434cb39345SGleb Smirnoff {
214408b68b0eSGleb Smirnoff 
214508b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
21464cb39345SGleb Smirnoff 
2147e8c34a71SGleb Smirnoff 	if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
2148167a3440SAlexander Motin 	    !(sc->sc_carpdev->if_flags & IFF_UP) ||
2149167a3440SAlexander Motin 	    !V_carp_allow) {
2150a9771948SGleb Smirnoff 		callout_stop(&sc->sc_ad_tmo);
215108b68b0eSGleb Smirnoff #ifdef INET
2152a9771948SGleb Smirnoff 		callout_stop(&sc->sc_md_tmo);
215308b68b0eSGleb Smirnoff #endif
215408b68b0eSGleb Smirnoff #ifdef INET6
2155a9771948SGleb Smirnoff 		callout_stop(&sc->sc_md6_tmo);
215608b68b0eSGleb Smirnoff #endif
2157bb269f3aSWill Andrews 		carp_set_state(sc, INIT, "hardware interface down");
2158a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
2159f08535f8SGleb Smirnoff 		if (!sc->sc_suppress)
2160c5c392e7SMikolaj Golub 			carp_demote_adj(V_carp_ifdown_adj, "interface down");
2161a9771948SGleb Smirnoff 		sc->sc_suppress = 1;
2162a9771948SGleb Smirnoff 	} else {
2163bb269f3aSWill Andrews 		carp_set_state(sc, INIT, "hardware interface up");
2164a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
2165a9771948SGleb Smirnoff 		if (sc->sc_suppress)
2166c5c392e7SMikolaj Golub 			carp_demote_adj(-V_carp_ifdown_adj, "interface up");
2167a9771948SGleb Smirnoff 		sc->sc_suppress = 0;
2168a9771948SGleb Smirnoff 	}
2169a9771948SGleb Smirnoff }
2170a9771948SGleb Smirnoff 
2171f08535f8SGleb Smirnoff static void
2172f08535f8SGleb Smirnoff carp_demote_adj(int adj, char *reason)
2173f08535f8SGleb Smirnoff {
2174c5c392e7SMikolaj Golub 	atomic_add_int(&V_carp_demotion, adj);
2175c5c392e7SMikolaj Golub 	CARP_LOG("demoted by %d to %d (%s)\n", adj, V_carp_demotion, reason);
2176f08535f8SGleb Smirnoff 	taskqueue_enqueue(taskqueue_swi, &carp_sendall_task);
2177f08535f8SGleb Smirnoff }
217808b68b0eSGleb Smirnoff 
21797951008bSGleb Smirnoff static int
2180167a3440SAlexander Motin carp_allow_sysctl(SYSCTL_HANDLER_ARGS)
2181167a3440SAlexander Motin {
2182167a3440SAlexander Motin 	int new, error;
2183167a3440SAlexander Motin 	struct carp_softc *sc;
2184167a3440SAlexander Motin 
2185167a3440SAlexander Motin 	new = V_carp_allow;
2186167a3440SAlexander Motin 	error = sysctl_handle_int(oidp, &new, 0, req);
2187167a3440SAlexander Motin 	if (error || !req->newptr)
2188167a3440SAlexander Motin 		return (error);
2189167a3440SAlexander Motin 
2190167a3440SAlexander Motin 	if (V_carp_allow != new) {
2191167a3440SAlexander Motin 		V_carp_allow = new;
2192167a3440SAlexander Motin 
2193167a3440SAlexander Motin 		mtx_lock(&carp_mtx);
2194167a3440SAlexander Motin 		LIST_FOREACH(sc, &carp_list, sc_next) {
2195167a3440SAlexander Motin 			CARP_LOCK(sc);
2196167a3440SAlexander Motin 			if (curvnet == sc->sc_carpdev->if_vnet)
2197167a3440SAlexander Motin 				carp_sc_state(sc);
2198167a3440SAlexander Motin 			CARP_UNLOCK(sc);
2199167a3440SAlexander Motin 		}
2200167a3440SAlexander Motin 		mtx_unlock(&carp_mtx);
2201167a3440SAlexander Motin 	}
2202167a3440SAlexander Motin 
2203167a3440SAlexander Motin 	return (0);
2204167a3440SAlexander Motin }
2205167a3440SAlexander Motin 
2206167a3440SAlexander Motin static int
22070d3d234cSKristof Provost carp_dscp_sysctl(SYSCTL_HANDLER_ARGS)
22080d3d234cSKristof Provost {
22090d3d234cSKristof Provost 	int new, error;
22100d3d234cSKristof Provost 
22110d3d234cSKristof Provost 	new = V_carp_dscp;
22120d3d234cSKristof Provost 	error = sysctl_handle_int(oidp, &new, 0, req);
22130d3d234cSKristof Provost 	if (error || !req->newptr)
22140d3d234cSKristof Provost 		return (error);
22150d3d234cSKristof Provost 
22160d3d234cSKristof Provost 	if (new < 0 || new > 63)
22170d3d234cSKristof Provost 		return (EINVAL);
22180d3d234cSKristof Provost 
22190d3d234cSKristof Provost 	V_carp_dscp = new;
22200d3d234cSKristof Provost 
22210d3d234cSKristof Provost 	return (0);
22220d3d234cSKristof Provost }
22230d3d234cSKristof Provost 
22240d3d234cSKristof Provost static int
22257951008bSGleb Smirnoff carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS)
22267951008bSGleb Smirnoff {
22277951008bSGleb Smirnoff 	int new, error;
22287951008bSGleb Smirnoff 
2229c5c392e7SMikolaj Golub 	new = V_carp_demotion;
22307951008bSGleb Smirnoff 	error = sysctl_handle_int(oidp, &new, 0, req);
22317951008bSGleb Smirnoff 	if (error || !req->newptr)
22327951008bSGleb Smirnoff 		return (error);
22337951008bSGleb Smirnoff 
22347951008bSGleb Smirnoff 	carp_demote_adj(new, "sysctl");
22357951008bSGleb Smirnoff 
22367951008bSGleb Smirnoff 	return (0);
22377951008bSGleb Smirnoff }
22387951008bSGleb Smirnoff 
223940e04359SKristof Provost static int
224040e04359SKristof Provost nlattr_get_carp_key(struct nlattr *nla, struct nl_pstate *npt, const void *arg, void *target)
224140e04359SKristof Provost {
224240e04359SKristof Provost 	if (__predict_false(NLA_DATA_LEN(nla) > CARP_KEY_LEN))
224340e04359SKristof Provost 		return (EINVAL);
224440e04359SKristof Provost 
224540e04359SKristof Provost 	memcpy(target, NLA_DATA_CONST(nla), NLA_DATA_LEN(nla));
224640e04359SKristof Provost 	return (0);
224740e04359SKristof Provost }
224840e04359SKristof Provost 
224940e04359SKristof Provost struct carp_nl_send_args {
225040e04359SKristof Provost 	struct nlmsghdr *hdr;
225140e04359SKristof Provost 	struct nl_pstate *npt;
225240e04359SKristof Provost };
225340e04359SKristof Provost 
225440e04359SKristof Provost static bool
225540e04359SKristof Provost carp_nl_send(void *arg, struct carp_softc *sc, int priv)
225640e04359SKristof Provost {
225740e04359SKristof Provost 	struct carp_nl_send_args *nlsa = arg;
225840e04359SKristof Provost 	struct nlmsghdr *hdr = nlsa->hdr;
225940e04359SKristof Provost 	struct nl_pstate *npt = nlsa->npt;
226040e04359SKristof Provost 	struct nl_writer *nw = npt->nw;
226140e04359SKristof Provost 	struct genlmsghdr *ghdr_new;
226240e04359SKristof Provost 
226340e04359SKristof Provost 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr))) {
226440e04359SKristof Provost 		nlmsg_abort(nw);
226540e04359SKristof Provost 		return (false);
226640e04359SKristof Provost 	}
226740e04359SKristof Provost 
226840e04359SKristof Provost 	ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
226940e04359SKristof Provost 	if (ghdr_new == NULL) {
227040e04359SKristof Provost 		nlmsg_abort(nw);
227140e04359SKristof Provost 		return (false);
227240e04359SKristof Provost 	}
227340e04359SKristof Provost 
227440e04359SKristof Provost 	ghdr_new->cmd = CARP_NL_CMD_GET;
227540e04359SKristof Provost 	ghdr_new->version = 0;
227640e04359SKristof Provost 	ghdr_new->reserved = 0;
227740e04359SKristof Provost 
227840e04359SKristof Provost 	CARP_LOCK(sc);
227940e04359SKristof Provost 
228040e04359SKristof Provost 	nlattr_add_u32(nw, CARP_NL_VHID, sc->sc_vhid);
228140e04359SKristof Provost 	nlattr_add_u32(nw, CARP_NL_STATE, sc->sc_state);
228240e04359SKristof Provost 	nlattr_add_s32(nw, CARP_NL_ADVBASE, sc->sc_advbase);
228340e04359SKristof Provost 	nlattr_add_s32(nw, CARP_NL_ADVSKEW, sc->sc_advskew);
2284*13781800SKristof Provost 	nlattr_add_in_addr(nw, CARP_NL_ADDR, &sc->sc_carpaddr);
2285*13781800SKristof Provost 	nlattr_add_in6_addr(nw, CARP_NL_ADDR6, &sc->sc_carpaddr6);
228640e04359SKristof Provost 
228740e04359SKristof Provost 	if (priv)
228840e04359SKristof Provost 		nlattr_add(nw, CARP_NL_KEY, sizeof(sc->sc_key), sc->sc_key);
228940e04359SKristof Provost 
229040e04359SKristof Provost 	CARP_UNLOCK(sc);
229140e04359SKristof Provost 
229240e04359SKristof Provost 	if (! nlmsg_end(nw)) {
229340e04359SKristof Provost 		nlmsg_abort(nw);
229440e04359SKristof Provost 		return (false);
229540e04359SKristof Provost 	}
229640e04359SKristof Provost 
229740e04359SKristof Provost 	return (true);
229840e04359SKristof Provost }
229940e04359SKristof Provost 
230040e04359SKristof Provost struct nl_carp_parsed {
230140e04359SKristof Provost 	unsigned int	ifindex;
230240e04359SKristof Provost 	uint32_t	state;
230340e04359SKristof Provost 	uint32_t	vhid;
230440e04359SKristof Provost 	int32_t		advbase;
230540e04359SKristof Provost 	int32_t		advskew;
230640e04359SKristof Provost 	char		key[CARP_KEY_LEN];
2307*13781800SKristof Provost 	struct in_addr	addr;
2308*13781800SKristof Provost 	struct in6_addr	addr6;
230940e04359SKristof Provost };
231040e04359SKristof Provost 
231140e04359SKristof Provost #define	_IN(_field)	offsetof(struct genlmsghdr, _field)
231240e04359SKristof Provost #define	_OUT(_field)	offsetof(struct nl_carp_parsed, _field)
231340e04359SKristof Provost 
231440e04359SKristof Provost static const struct nlattr_parser nla_p_set[] = {
231540e04359SKristof Provost 	{ .type = CARP_NL_VHID, .off = _OUT(vhid), .cb = nlattr_get_uint32 },
231640e04359SKristof Provost 	{ .type = CARP_NL_STATE, .off = _OUT(state), .cb = nlattr_get_uint32 },
231740e04359SKristof Provost 	{ .type = CARP_NL_ADVBASE, .off = _OUT(advbase), .cb = nlattr_get_uint32 },
231840e04359SKristof Provost 	{ .type = CARP_NL_ADVSKEW, .off = _OUT(advskew), .cb = nlattr_get_uint32 },
231940e04359SKristof Provost 	{ .type = CARP_NL_KEY, .off = _OUT(key), .cb = nlattr_get_carp_key },
232040e04359SKristof Provost 	{ .type = CARP_NL_IFINDEX, .off = _OUT(ifindex), .cb = nlattr_get_uint32 },
2321*13781800SKristof Provost 	{ .type = CARP_NL_ADDR, .off = _OUT(addr), .cb = nlattr_get_in_addr },
2322*13781800SKristof Provost 	{ .type = CARP_NL_ADDR6, .off = _OUT(addr6), .cb = nlattr_get_in6_addr },
232340e04359SKristof Provost };
232440e04359SKristof Provost static const struct nlfield_parser nlf_p_set[] = {
232540e04359SKristof Provost };
232640e04359SKristof Provost NL_DECLARE_PARSER(carp_parser, struct genlmsghdr, nlf_p_set, nla_p_set);
232740e04359SKristof Provost #undef _IN
232840e04359SKristof Provost #undef _OUT
232940e04359SKristof Provost 
233040e04359SKristof Provost 
233140e04359SKristof Provost static int
233240e04359SKristof Provost carp_nl_get(struct nlmsghdr *hdr, struct nl_pstate *npt)
233340e04359SKristof Provost {
233440e04359SKristof Provost 	struct nl_carp_parsed attrs = { };
233540e04359SKristof Provost 	struct carp_nl_send_args args;
233640e04359SKristof Provost 	struct carpreq carpr = { };
233740e04359SKristof Provost 	struct epoch_tracker et;
233840e04359SKristof Provost 	if_t ifp;
233940e04359SKristof Provost 	int error;
234040e04359SKristof Provost 
234140e04359SKristof Provost 	error = nl_parse_nlmsg(hdr, &carp_parser, npt, &attrs);
234240e04359SKristof Provost 	if (error != 0)
234340e04359SKristof Provost 		return (error);
234440e04359SKristof Provost 
234540e04359SKristof Provost 	NET_EPOCH_ENTER(et);
234640e04359SKristof Provost 	ifp = ifnet_byindex_ref(attrs.ifindex);
234740e04359SKristof Provost 	NET_EPOCH_EXIT(et);
234840e04359SKristof Provost 
234940e04359SKristof Provost 	if ((error = carp_is_supported_if(ifp)) != 0)
235040e04359SKristof Provost 		goto out;
235140e04359SKristof Provost 
235240e04359SKristof Provost 	hdr->nlmsg_flags |= NLM_F_MULTI;
235340e04359SKristof Provost 	args.hdr = hdr;
235440e04359SKristof Provost 	args.npt = npt;
235540e04359SKristof Provost 
235640e04359SKristof Provost 	carpr.carpr_vhid = attrs.vhid;
235740e04359SKristof Provost 	carpr.carpr_count = CARP_MAXVHID;
235840e04359SKristof Provost 
235940e04359SKristof Provost 	sx_xlock(&carp_sx);
236040e04359SKristof Provost 	error = carp_ioctl_get(ifp, nlp_get_cred(npt->nlp), &carpr,
236140e04359SKristof Provost 	    carp_nl_send, &args);
236240e04359SKristof Provost 	sx_xunlock(&carp_sx);
236340e04359SKristof Provost 
236440e04359SKristof Provost 	if (! nlmsg_end_dump(npt->nw, error, hdr))
236540e04359SKristof Provost 		error = ENOMEM;
236640e04359SKristof Provost 
236740e04359SKristof Provost out:
236840e04359SKristof Provost 	if (ifp != NULL)
236940e04359SKristof Provost 		if_rele(ifp);
237040e04359SKristof Provost 
237140e04359SKristof Provost 	return (error);
237240e04359SKristof Provost }
237340e04359SKristof Provost 
237440e04359SKristof Provost static int
237540e04359SKristof Provost carp_nl_set(struct nlmsghdr *hdr, struct nl_pstate *npt)
237640e04359SKristof Provost {
237740e04359SKristof Provost 	struct nl_carp_parsed attrs = { };
2378*13781800SKristof Provost 	struct carpkreq carpr;
237940e04359SKristof Provost 	struct epoch_tracker et;
238040e04359SKristof Provost 	if_t ifp;
238140e04359SKristof Provost 	int error;
238240e04359SKristof Provost 
238340e04359SKristof Provost 	error = nl_parse_nlmsg(hdr, &carp_parser, npt, &attrs);
238440e04359SKristof Provost 	if (error != 0)
238540e04359SKristof Provost 		return (error);
238640e04359SKristof Provost 
238740e04359SKristof Provost 	if (attrs.vhid <= 0 || attrs.vhid > CARP_MAXVHID)
238840e04359SKristof Provost 		return (EINVAL);
238940e04359SKristof Provost 	if (attrs.state > CARP_MAXSTATE)
239040e04359SKristof Provost 		return (EINVAL);
239140e04359SKristof Provost 	if (attrs.advbase < 0 || attrs.advskew < 0)
239240e04359SKristof Provost 		return (EINVAL);
239340e04359SKristof Provost 	if (attrs.advbase > 255)
239440e04359SKristof Provost 		return (EINVAL);
239540e04359SKristof Provost 	if (attrs.advskew >= 255)
239640e04359SKristof Provost 		return (EINVAL);
239740e04359SKristof Provost 
239840e04359SKristof Provost 	NET_EPOCH_ENTER(et);
239940e04359SKristof Provost 	ifp = ifnet_byindex_ref(attrs.ifindex);
240040e04359SKristof Provost 	NET_EPOCH_EXIT(et);
240140e04359SKristof Provost 
240240e04359SKristof Provost 	if ((error = carp_is_supported_if(ifp)) != 0)
240340e04359SKristof Provost 		goto out;
240440e04359SKristof Provost 
240540e04359SKristof Provost 	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
240640e04359SKristof Provost 		error = EADDRNOTAVAIL;
240740e04359SKristof Provost 		goto out;
240840e04359SKristof Provost 	}
240940e04359SKristof Provost 
241040e04359SKristof Provost 	carpr.carpr_count = 1;
241140e04359SKristof Provost 	carpr.carpr_vhid = attrs.vhid;
241240e04359SKristof Provost 	carpr.carpr_state = attrs.state;
241340e04359SKristof Provost 	carpr.carpr_advbase = attrs.advbase;
241440e04359SKristof Provost 	carpr.carpr_advskew = attrs.advskew;
2415*13781800SKristof Provost 	carpr.carpr_addr = attrs.addr;
2416*13781800SKristof Provost 	carpr.carpr_addr6 = attrs.addr6;
2417*13781800SKristof Provost 
241840e04359SKristof Provost 	memcpy(&carpr.carpr_key, &attrs.key, sizeof(attrs.key));
241940e04359SKristof Provost 
242040e04359SKristof Provost 	sx_xlock(&carp_sx);
242140e04359SKristof Provost 	error = carp_ioctl_set(ifp, &carpr);
242240e04359SKristof Provost 	sx_xunlock(&carp_sx);
242340e04359SKristof Provost 
242440e04359SKristof Provost out:
242540e04359SKristof Provost 	if (ifp != NULL)
242640e04359SKristof Provost 		if_rele(ifp);
242740e04359SKristof Provost 
242840e04359SKristof Provost 	return (error);
242940e04359SKristof Provost }
243040e04359SKristof Provost 
243140e04359SKristof Provost static const struct nlhdr_parser *all_parsers[] = {
243240e04359SKristof Provost 	&carp_parser
243340e04359SKristof Provost };
243440e04359SKristof Provost 
243540e04359SKristof Provost static const struct genl_cmd carp_cmds[] = {
243640e04359SKristof Provost 	{
243740e04359SKristof Provost 		.cmd_num = CARP_NL_CMD_GET,
243840e04359SKristof Provost 		.cmd_name = "SIOCGVH",
243940e04359SKristof Provost 		.cmd_cb = carp_nl_get,
244040e04359SKristof Provost 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_DUMP |
244140e04359SKristof Provost 		    GENL_CMD_CAP_HASPOL,
244240e04359SKristof Provost 	},
244340e04359SKristof Provost 	{
244440e04359SKristof Provost 		.cmd_num = CARP_NL_CMD_SET,
244540e04359SKristof Provost 		.cmd_name = "SIOCSVH",
244640e04359SKristof Provost 		.cmd_cb = carp_nl_set,
244740e04359SKristof Provost 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_HASPOL,
244840e04359SKristof Provost 		.cmd_priv = PRIV_NETINET_CARP,
244940e04359SKristof Provost 	},
245040e04359SKristof Provost };
245140e04359SKristof Provost 
245240e04359SKristof Provost static void
245340e04359SKristof Provost carp_nl_register(void)
245440e04359SKristof Provost {
245540e04359SKristof Provost 	bool ret __diagused;
245640e04359SKristof Provost 	int family_id __diagused;
245740e04359SKristof Provost 
245840e04359SKristof Provost 	NL_VERIFY_PARSERS(all_parsers);
245940e04359SKristof Provost 	family_id = genl_register_family(CARP_NL_FAMILY_NAME, 0, 2,
246040e04359SKristof Provost 	    CARP_NL_CMD_MAX);
246140e04359SKristof Provost 	MPASS(family_id != 0);
246240e04359SKristof Provost 
246340e04359SKristof Provost 	ret = genl_register_cmds(CARP_NL_FAMILY_NAME, carp_cmds,
246440e04359SKristof Provost 	    NL_ARRAY_LEN(carp_cmds));
246540e04359SKristof Provost 	MPASS(ret);
246640e04359SKristof Provost }
246740e04359SKristof Provost 
246840e04359SKristof Provost static void
246940e04359SKristof Provost carp_nl_unregister(void)
247040e04359SKristof Provost {
247140e04359SKristof Provost 	genl_unregister_family(CARP_NL_FAMILY_NAME);
247240e04359SKristof Provost }
247340e04359SKristof Provost 
247454bfbd51SWill Andrews static void
247554bfbd51SWill Andrews carp_mod_cleanup(void)
2476a9771948SGleb Smirnoff {
247754bfbd51SWill Andrews 
247840e04359SKristof Provost 	carp_nl_unregister();
247940e04359SKristof Provost 
248054bfbd51SWill Andrews #ifdef INET
248115249f73SWill Andrews 	(void)ipproto_unregister(IPPROTO_CARP);
248254bfbd51SWill Andrews 	carp_iamatch_p = NULL;
248354bfbd51SWill Andrews #endif
248454bfbd51SWill Andrews #ifdef INET6
248515249f73SWill Andrews 	(void)ip6proto_unregister(IPPROTO_CARP);
248654bfbd51SWill Andrews 	carp_iamatch6_p = NULL;
248754bfbd51SWill Andrews 	carp_macmatch6_p = NULL;
248854bfbd51SWill Andrews #endif
248908b68b0eSGleb Smirnoff 	carp_ioctl_p = NULL;
249008b68b0eSGleb Smirnoff 	carp_attach_p = NULL;
249108b68b0eSGleb Smirnoff 	carp_detach_p = NULL;
249208b68b0eSGleb Smirnoff 	carp_get_vhid_p = NULL;
249354bfbd51SWill Andrews 	carp_linkstate_p = NULL;
249454bfbd51SWill Andrews 	carp_forus_p = NULL;
249554bfbd51SWill Andrews 	carp_output_p = NULL;
2496f08535f8SGleb Smirnoff 	carp_demote_adj_p = NULL;
249724421c1cSGleb Smirnoff 	carp_master_p = NULL;
2498f08535f8SGleb Smirnoff 	mtx_unlock(&carp_mtx);
2499f08535f8SGleb Smirnoff 	taskqueue_drain(taskqueue_swi, &carp_sendall_task);
250054bfbd51SWill Andrews 	mtx_destroy(&carp_mtx);
250193d4534cSGleb Smirnoff 	sx_destroy(&carp_sx);
250254bfbd51SWill Andrews }
250354bfbd51SWill Andrews 
2504ee49c5d3SBoris Lytochkin static void
2505ee49c5d3SBoris Lytochkin ipcarp_sysinit(void)
2506ee49c5d3SBoris Lytochkin {
2507ee49c5d3SBoris Lytochkin 
2508ee49c5d3SBoris Lytochkin 	/* Load allow as tunable so to postpone carp start after module load */
2509ee49c5d3SBoris Lytochkin 	TUNABLE_INT_FETCH("net.inet.carp.allow", &V_carp_allow);
2510ee49c5d3SBoris Lytochkin }
2511ee49c5d3SBoris Lytochkin VNET_SYSINIT(ip_carp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, ipcarp_sysinit, NULL);
2512ee49c5d3SBoris Lytochkin 
251354bfbd51SWill Andrews static int
251454bfbd51SWill Andrews carp_mod_load(void)
251554bfbd51SWill Andrews {
251615249f73SWill Andrews 	int err;
251754bfbd51SWill Andrews 
2518d92d54d5SGleb Smirnoff 	mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
251993d4534cSGleb Smirnoff 	sx_init(&carp_sx, "carp_sx");
252008b68b0eSGleb Smirnoff 	LIST_INIT(&carp_list);
252108b68b0eSGleb Smirnoff 	carp_get_vhid_p = carp_get_vhid;
252254bfbd51SWill Andrews 	carp_forus_p = carp_forus;
252354bfbd51SWill Andrews 	carp_output_p = carp_output;
252408b68b0eSGleb Smirnoff 	carp_linkstate_p = carp_linkstate;
252508b68b0eSGleb Smirnoff 	carp_ioctl_p = carp_ioctl;
252608b68b0eSGleb Smirnoff 	carp_attach_p = carp_attach;
252708b68b0eSGleb Smirnoff 	carp_detach_p = carp_detach;
2528f08535f8SGleb Smirnoff 	carp_demote_adj_p = carp_demote_adj;
252924421c1cSGleb Smirnoff 	carp_master_p = carp_master;
253054bfbd51SWill Andrews #ifdef INET6
253154bfbd51SWill Andrews 	carp_iamatch6_p = carp_iamatch6;
253254bfbd51SWill Andrews 	carp_macmatch6_p = carp_macmatch6;
253378b1fc05SGleb Smirnoff 	err = ip6proto_register(IPPROTO_CARP, carp6_input, NULL);
253415249f73SWill Andrews 	if (err) {
253515249f73SWill Andrews 		printf("carp: error %d registering with INET6\n", err);
253615249f73SWill Andrews 		carp_mod_cleanup();
25376baf7a24SGleb Smirnoff 		return (err);
253815249f73SWill Andrews 	}
253954bfbd51SWill Andrews #endif
254054bfbd51SWill Andrews #ifdef INET
254154bfbd51SWill Andrews 	carp_iamatch_p = carp_iamatch;
254278b1fc05SGleb Smirnoff 	err = ipproto_register(IPPROTO_CARP, carp_input, NULL);
254315249f73SWill Andrews 	if (err) {
254415249f73SWill Andrews 		printf("carp: error %d registering with INET\n", err);
254515249f73SWill Andrews 		carp_mod_cleanup();
25466baf7a24SGleb Smirnoff 		return (err);
254715249f73SWill Andrews 	}
254854bfbd51SWill Andrews #endif
254940e04359SKristof Provost 
255040e04359SKristof Provost 	carp_nl_register();
255140e04359SKristof Provost 
255208b68b0eSGleb Smirnoff 	return (0);
255354bfbd51SWill Andrews }
2554a9771948SGleb Smirnoff 
255554bfbd51SWill Andrews static int
255654bfbd51SWill Andrews carp_modevent(module_t mod, int type, void *data)
255754bfbd51SWill Andrews {
255854bfbd51SWill Andrews 	switch (type) {
255954bfbd51SWill Andrews 	case MOD_LOAD:
256054bfbd51SWill Andrews 		return carp_mod_load();
256154bfbd51SWill Andrews 		/* NOTREACHED */
2562a9771948SGleb Smirnoff 	case MOD_UNLOAD:
256308b68b0eSGleb Smirnoff 		mtx_lock(&carp_mtx);
256408b68b0eSGleb Smirnoff 		if (LIST_EMPTY(&carp_list))
256554bfbd51SWill Andrews 			carp_mod_cleanup();
256608b68b0eSGleb Smirnoff 		else {
256708b68b0eSGleb Smirnoff 			mtx_unlock(&carp_mtx);
256854bfbd51SWill Andrews 			return (EBUSY);
256908b68b0eSGleb Smirnoff 		}
2570a9771948SGleb Smirnoff 		break;
2571a9771948SGleb Smirnoff 
2572a9771948SGleb Smirnoff 	default:
25730fa08018SGleb Smirnoff 		return (EINVAL);
2574a9771948SGleb Smirnoff 	}
2575a9771948SGleb Smirnoff 
25760fa08018SGleb Smirnoff 	return (0);
2577a9771948SGleb Smirnoff }
2578a9771948SGleb Smirnoff 
2579a9771948SGleb Smirnoff static moduledata_t carp_mod = {
2580a9771948SGleb Smirnoff 	"carp",
2581a9771948SGleb Smirnoff 	carp_modevent,
25829823d527SKevin Lo 	0
2583a9771948SGleb Smirnoff };
2584a9771948SGleb Smirnoff 
2585e24fa11dSWill Andrews DECLARE_MODULE(carp, carp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
2586