xref: /freebsd/sys/netinet/ip_carp.c (revision ed6a66ca6c2065bc1fed93e00805676a5d3b981e)
108b68b0eSGleb Smirnoff /*-
208b68b0eSGleb Smirnoff  * Copyright (c) 2002 Michael Shalayeff.
308b68b0eSGleb Smirnoff  * Copyright (c) 2003 Ryan McBride.
408b68b0eSGleb Smirnoff  * Copyright (c) 2011 Gleb Smirnoff <glebius@FreeBSD.org>
508b68b0eSGleb Smirnoff  * All rights reserved.
6a9771948SGleb Smirnoff  *
7a9771948SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
8a9771948SGleb Smirnoff  * modification, are permitted provided that the following conditions
9a9771948SGleb Smirnoff  * are met:
10a9771948SGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
11a9771948SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
12a9771948SGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
13a9771948SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
14a9771948SGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
15a9771948SGleb Smirnoff  *
16a9771948SGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17a9771948SGleb Smirnoff  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18a9771948SGleb Smirnoff  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19a9771948SGleb Smirnoff  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
20a9771948SGleb Smirnoff  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21a9771948SGleb Smirnoff  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22a9771948SGleb Smirnoff  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23a9771948SGleb Smirnoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24a9771948SGleb Smirnoff  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25a9771948SGleb Smirnoff  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26a9771948SGleb Smirnoff  * THE POSSIBILITY OF SUCH DAMAGE.
27a9771948SGleb Smirnoff  */
28a9771948SGleb Smirnoff 
294b421e2dSMike Silbersack #include <sys/cdefs.h>
304b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
314b421e2dSMike Silbersack 
32a9771948SGleb Smirnoff #include "opt_bpf.h"
33a9771948SGleb Smirnoff #include "opt_inet.h"
34a9771948SGleb Smirnoff #include "opt_inet6.h"
35a9771948SGleb Smirnoff 
36a9771948SGleb Smirnoff #include <sys/param.h>
37a9771948SGleb Smirnoff #include <sys/systm.h>
3808b68b0eSGleb Smirnoff #include <sys/bus.h>
3908b68b0eSGleb Smirnoff #include <sys/jail.h>
40a9771948SGleb Smirnoff #include <sys/kernel.h>
41a9771948SGleb Smirnoff #include <sys/limits.h>
42a9771948SGleb Smirnoff #include <sys/malloc.h>
43a9771948SGleb Smirnoff #include <sys/mbuf.h>
44a9771948SGleb Smirnoff #include <sys/module.h>
45acd3428bSRobert Watson #include <sys/priv.h>
46a9771948SGleb Smirnoff #include <sys/proc.h>
4754bfbd51SWill Andrews #include <sys/protosw.h>
4808b68b0eSGleb Smirnoff #include <sys/socket.h>
4908b68b0eSGleb Smirnoff #include <sys/sockio.h>
50a9771948SGleb Smirnoff #include <sys/sysctl.h>
51a9771948SGleb Smirnoff #include <sys/syslog.h>
52f08535f8SGleb Smirnoff #include <sys/taskqueue.h>
5369edf037SAndrey V. Elsukov #include <sys/counter.h>
54a9771948SGleb Smirnoff 
55a9771948SGleb Smirnoff #include <net/ethernet.h>
56a9771948SGleb Smirnoff #include <net/fddi.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>
61a9771948SGleb Smirnoff #include <net/if_types.h>
6208b68b0eSGleb Smirnoff #include <net/iso88025.h>
63a9771948SGleb Smirnoff #include <net/route.h>
64530c0060SRobert Watson #include <net/vnet.h>
65a9771948SGleb Smirnoff 
66a0ae8f04SBjoern A. Zeeb #if defined(INET) || defined(INET6)
67a9771948SGleb Smirnoff #include <netinet/in.h>
68a9771948SGleb Smirnoff #include <netinet/in_var.h>
69a0ae8f04SBjoern A. Zeeb #include <netinet/ip_carp.h>
70a9771948SGleb Smirnoff #include <netinet/ip.h>
71a0ae8f04SBjoern A. Zeeb #include <machine/in_cksum.h>
72a0ae8f04SBjoern A. Zeeb #endif
73a0ae8f04SBjoern A. Zeeb #ifdef INET
74a9771948SGleb Smirnoff #include <netinet/ip_var.h>
75a9771948SGleb Smirnoff #include <netinet/if_ether.h>
76a9771948SGleb Smirnoff #endif
77a9771948SGleb Smirnoff 
78a9771948SGleb Smirnoff #ifdef INET6
79a9771948SGleb Smirnoff #include <netinet/icmp6.h>
80a9771948SGleb Smirnoff #include <netinet/ip6.h>
8108b68b0eSGleb Smirnoff #include <netinet6/in6_var.h>
82a9771948SGleb Smirnoff #include <netinet6/ip6_var.h>
8329da8af6SHajimu UMEMOTO #include <netinet6/scope6_var.h>
84a9771948SGleb Smirnoff #include <netinet6/nd6.h>
85a9771948SGleb Smirnoff #endif
86a9771948SGleb Smirnoff 
87a9771948SGleb Smirnoff #include <crypto/sha1.h>
88a9771948SGleb Smirnoff 
8908b68b0eSGleb Smirnoff static MALLOC_DEFINE(M_CARP, "CARP", "CARP addresses");
90a9771948SGleb Smirnoff 
91a9771948SGleb Smirnoff struct carp_softc {
9208b68b0eSGleb Smirnoff 	struct ifnet		*sc_carpdev;	/* Pointer to parent ifnet. */
9308b68b0eSGleb Smirnoff 	struct ifaddr		**sc_ifas;	/* Our ifaddrs. */
9408b68b0eSGleb Smirnoff 	struct sockaddr_dl	sc_addr;	/* Our link level address. */
9508b68b0eSGleb Smirnoff 	struct callout		sc_ad_tmo;	/* Advertising timeout. */
96a0ae8f04SBjoern A. Zeeb #ifdef INET
9708b68b0eSGleb Smirnoff 	struct callout		sc_md_tmo;	/* Master down timeout. */
98a0ae8f04SBjoern A. Zeeb #endif
99a9771948SGleb Smirnoff #ifdef INET6
10008b68b0eSGleb Smirnoff 	struct callout 		sc_md6_tmo;	/* XXX: Master down timeout. */
10108b68b0eSGleb Smirnoff #endif
10208b68b0eSGleb Smirnoff 	struct mtx		sc_mtx;
103a9771948SGleb Smirnoff 
10408b68b0eSGleb Smirnoff 	int			sc_vhid;
10508b68b0eSGleb Smirnoff 	int			sc_advskew;
10608b68b0eSGleb Smirnoff 	int			sc_advbase;
10708b68b0eSGleb Smirnoff 
10808b68b0eSGleb Smirnoff 	int			sc_naddrs;
10908b68b0eSGleb Smirnoff 	int			sc_naddrs6;
11008b68b0eSGleb Smirnoff 	int			sc_ifasiz;
111a9771948SGleb Smirnoff 	enum { INIT = 0, BACKUP, MASTER }	sc_state;
112a9771948SGleb Smirnoff 	int			sc_suppress;
113a9771948SGleb Smirnoff 	int			sc_sendad_errors;
114a9771948SGleb Smirnoff #define	CARP_SENDAD_MAX_ERRORS	3
115a9771948SGleb Smirnoff 	int			sc_sendad_success;
116a9771948SGleb Smirnoff #define	CARP_SENDAD_MIN_SUCCESS 3
117a9771948SGleb Smirnoff 
118a9771948SGleb Smirnoff 	int			sc_init_counter;
11908b68b0eSGleb Smirnoff 	uint64_t		sc_counter;
120a9771948SGleb Smirnoff 
121a9771948SGleb Smirnoff 	/* authentication */
122a9771948SGleb Smirnoff #define	CARP_HMAC_PAD	64
123a9771948SGleb Smirnoff 	unsigned char sc_key[CARP_KEY_LEN];
124a9771948SGleb Smirnoff 	unsigned char sc_pad[CARP_HMAC_PAD];
125a9771948SGleb Smirnoff 	SHA1_CTX sc_sha1;
126a9771948SGleb Smirnoff 
12708b68b0eSGleb Smirnoff 	TAILQ_ENTRY(carp_softc)	sc_list;	/* On the carp_if list. */
12808b68b0eSGleb Smirnoff 	LIST_ENTRY(carp_softc)	sc_next;	/* On the global list. */
129a9771948SGleb Smirnoff };
13008b68b0eSGleb Smirnoff 
13108b68b0eSGleb Smirnoff struct carp_if {
13208b68b0eSGleb Smirnoff #ifdef INET
13308b68b0eSGleb Smirnoff 	int	cif_naddrs;
13408b68b0eSGleb Smirnoff #endif
13508b68b0eSGleb Smirnoff #ifdef INET6
13608b68b0eSGleb Smirnoff 	int	cif_naddrs6;
13708b68b0eSGleb Smirnoff #endif
13808b68b0eSGleb Smirnoff 	TAILQ_HEAD(, carp_softc) cif_vrs;
13908b68b0eSGleb Smirnoff #ifdef INET
14008b68b0eSGleb Smirnoff 	struct ip_moptions 	 cif_imo;
14108b68b0eSGleb Smirnoff #endif
14208b68b0eSGleb Smirnoff #ifdef INET6
14308b68b0eSGleb Smirnoff 	struct ip6_moptions 	 cif_im6o;
14408b68b0eSGleb Smirnoff #endif
14508b68b0eSGleb Smirnoff 	struct ifnet	*cif_ifp;
14608b68b0eSGleb Smirnoff 	struct mtx	cif_mtx;
1470cc726f2SGleb Smirnoff 	uint32_t	cif_flags;
1480cc726f2SGleb Smirnoff #define	CIF_PROMISC	0x00000001
14908b68b0eSGleb Smirnoff };
15008b68b0eSGleb Smirnoff 
15108b68b0eSGleb Smirnoff #define	CARP_INET	0
15208b68b0eSGleb Smirnoff #define	CARP_INET6	1
15308b68b0eSGleb Smirnoff static int proto_reg[] = {-1, -1};
15408b68b0eSGleb Smirnoff 
15508b68b0eSGleb Smirnoff /*
15608b68b0eSGleb Smirnoff  * Brief design of carp(4).
15708b68b0eSGleb Smirnoff  *
15808b68b0eSGleb Smirnoff  * Any carp-capable ifnet may have a list of carp softcs hanging off
15908b68b0eSGleb Smirnoff  * its ifp->if_carp pointer. Each softc represents one unique virtual
16008b68b0eSGleb Smirnoff  * host id, or vhid. The softc has a back pointer to the ifnet. All
16108b68b0eSGleb Smirnoff  * softcs are joined in a global list, which has quite limited use.
16208b68b0eSGleb Smirnoff  *
16308b68b0eSGleb Smirnoff  * Any interface address that takes part in CARP negotiation has a
16408b68b0eSGleb Smirnoff  * pointer to the softc of its vhid, ifa->ifa_carp. That could be either
16508b68b0eSGleb Smirnoff  * AF_INET or AF_INET6 address.
16608b68b0eSGleb Smirnoff  *
16708b68b0eSGleb Smirnoff  * Although, one can get the softc's backpointer to ifnet and traverse
16808b68b0eSGleb Smirnoff  * through its ifp->if_addrhead queue to find all interface addresses
16908b68b0eSGleb Smirnoff  * involved in CARP, we keep a growable array of ifaddr pointers. This
17008b68b0eSGleb Smirnoff  * allows us to avoid grabbing the IF_ADDR_LOCK() in many traversals that
17108b68b0eSGleb Smirnoff  * do calls into the network stack, thus avoiding LORs.
17208b68b0eSGleb Smirnoff  *
17308b68b0eSGleb Smirnoff  * Locking:
17408b68b0eSGleb Smirnoff  *
17508b68b0eSGleb Smirnoff  * Each softc has a lock sc_mtx. It is used to synchronise carp_input_c(),
17608b68b0eSGleb Smirnoff  * callout-driven events and ioctl()s.
17708b68b0eSGleb Smirnoff  *
17808b68b0eSGleb Smirnoff  * To traverse the list of softcs on an ifnet we use CIF_LOCK(), to
17908b68b0eSGleb Smirnoff  * traverse the global list we use the mutex carp_mtx.
18008b68b0eSGleb Smirnoff  *
18108b68b0eSGleb Smirnoff  * Known issues with locking:
18208b68b0eSGleb Smirnoff  *
18308b68b0eSGleb Smirnoff  * - There is no protection for races between two ioctl() requests,
18408b68b0eSGleb Smirnoff  *   neither SIOCSVH, nor SIOCAIFADDR & SIOCAIFADDR_IN6. I think that all
18508b68b0eSGleb Smirnoff  *   interface ioctl()s should be serialized right in net/if.c.
18608b68b0eSGleb Smirnoff  * - Sending ad, we put the pointer to the softc in an mtag, and no reference
18708b68b0eSGleb Smirnoff  *   counting is done on the softc.
18808b68b0eSGleb Smirnoff  * - On module unload we may race (?) with packet processing thread
18908b68b0eSGleb Smirnoff  *   dereferencing our function pointers.
19008b68b0eSGleb Smirnoff  */
191a9771948SGleb Smirnoff 
192c5c392e7SMikolaj Golub /* Accept incoming CARP packets. */
193c5c392e7SMikolaj Golub static VNET_DEFINE(int, carp_allow) = 1;
194c5c392e7SMikolaj Golub #define	V_carp_allow	VNET(carp_allow)
195c5c392e7SMikolaj Golub 
196c5c392e7SMikolaj Golub /* Preempt slower nodes. */
197c5c392e7SMikolaj Golub static VNET_DEFINE(int, carp_preempt) = 0;
198c5c392e7SMikolaj Golub #define	V_carp_preempt	VNET(carp_preempt)
199c5c392e7SMikolaj Golub 
200c5c392e7SMikolaj Golub /* Log level. */
201c5c392e7SMikolaj Golub static VNET_DEFINE(int, carp_log) = 1;
202c5c392e7SMikolaj Golub #define	V_carp_log	VNET(carp_log)
203c5c392e7SMikolaj Golub 
204c5c392e7SMikolaj Golub /* Global advskew demotion. */
205c5c392e7SMikolaj Golub static VNET_DEFINE(int, carp_demotion) = 0;
206c5c392e7SMikolaj Golub #define	V_carp_demotion	VNET(carp_demotion)
207c5c392e7SMikolaj Golub 
208c5c392e7SMikolaj Golub /* Send error demotion factor. */
209c5c392e7SMikolaj Golub static VNET_DEFINE(int, carp_senderr_adj) = CARP_MAXSKEW;
210c5c392e7SMikolaj Golub #define	V_carp_senderr_adj	VNET(carp_senderr_adj)
211c5c392e7SMikolaj Golub 
212c5c392e7SMikolaj Golub /* Iface down demotion factor. */
213c5c392e7SMikolaj Golub static VNET_DEFINE(int, carp_ifdown_adj) = CARP_MAXSKEW;
214c5c392e7SMikolaj Golub #define	V_carp_ifdown_adj	VNET(carp_ifdown_adj)
215c5c392e7SMikolaj Golub 
2167951008bSGleb Smirnoff static int carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS);
217a9771948SGleb Smirnoff 
218f08535f8SGleb Smirnoff SYSCTL_NODE(_net_inet, IPPROTO_CARP,	carp,	CTLFLAG_RW, 0,	"CARP");
2196df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, allow, CTLFLAG_VNET | CTLFLAG_RW,
220c5c392e7SMikolaj Golub     &VNET_NAME(carp_allow), 0, "Accept incoming CARP packets");
2216df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, preempt, CTLFLAG_VNET | CTLFLAG_RW,
222c5c392e7SMikolaj Golub     &VNET_NAME(carp_preempt), 0, "High-priority backup preemption mode");
2236df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, log, CTLFLAG_VNET | CTLFLAG_RW,
224c5c392e7SMikolaj Golub     &VNET_NAME(carp_log), 0, "CARP log level");
2256df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_carp, OID_AUTO, demotion,
2266df8a710SGleb Smirnoff     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
2277951008bSGleb Smirnoff     0, 0, carp_demote_adj_sysctl, "I",
2287951008bSGleb Smirnoff     "Adjust demotion factor (skew of advskew)");
2296df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, senderr_demotion_factor,
2306df8a710SGleb Smirnoff     CTLFLAG_VNET | CTLFLAG_RW,
231c5c392e7SMikolaj Golub     &VNET_NAME(carp_senderr_adj), 0, "Send error demotion factor adjustment");
2326df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, ifdown_demotion_factor,
2336df8a710SGleb Smirnoff     CTLFLAG_VNET | CTLFLAG_RW,
234c5c392e7SMikolaj Golub     &VNET_NAME(carp_ifdown_adj), 0,
235c5c392e7SMikolaj Golub     "Interface down demotion factor adjustment");
236f08535f8SGleb Smirnoff 
237c5c392e7SMikolaj Golub VNET_PCPUSTAT_DEFINE(struct carpstats, carpstats);
238c5c392e7SMikolaj Golub VNET_PCPUSTAT_SYSINIT(carpstats);
239c5c392e7SMikolaj Golub VNET_PCPUSTAT_SYSUNINIT(carpstats);
240c5c392e7SMikolaj Golub 
24169edf037SAndrey V. Elsukov #define	CARPSTATS_ADD(name, val)	\
242c5c392e7SMikolaj Golub     counter_u64_add(VNET(carpstats)[offsetof(struct carpstats, name) / \
24369edf037SAndrey V. Elsukov 	sizeof(uint64_t)], (val))
24469edf037SAndrey V. Elsukov #define	CARPSTATS_INC(name)		CARPSTATS_ADD(name, 1)
24569edf037SAndrey V. Elsukov 
246c5c392e7SMikolaj Golub SYSCTL_VNET_PCPUSTAT(_net_inet_carp, OID_AUTO, stats, struct carpstats,
247c5c392e7SMikolaj Golub     carpstats, "CARP statistics (struct carpstats, netinet/ip_carp.h)");
248a9771948SGleb Smirnoff 
24908b68b0eSGleb Smirnoff #define	CARP_LOCK_INIT(sc)	mtx_init(&(sc)->sc_mtx, "carp_softc",   \
250a9771948SGleb Smirnoff 	NULL, MTX_DEF)
25108b68b0eSGleb Smirnoff #define	CARP_LOCK_DESTROY(sc)	mtx_destroy(&(sc)->sc_mtx)
25208b68b0eSGleb Smirnoff #define	CARP_LOCK_ASSERT(sc)	mtx_assert(&(sc)->sc_mtx, MA_OWNED)
25308b68b0eSGleb Smirnoff #define	CARP_LOCK(sc)		mtx_lock(&(sc)->sc_mtx)
25408b68b0eSGleb Smirnoff #define	CARP_UNLOCK(sc)		mtx_unlock(&(sc)->sc_mtx)
25508b68b0eSGleb Smirnoff #define	CIF_LOCK_INIT(cif)	mtx_init(&(cif)->cif_mtx, "carp_if",   \
25608b68b0eSGleb Smirnoff 	NULL, MTX_DEF)
25708b68b0eSGleb Smirnoff #define	CIF_LOCK_DESTROY(cif)	mtx_destroy(&(cif)->cif_mtx)
25808b68b0eSGleb Smirnoff #define	CIF_LOCK_ASSERT(cif)	mtx_assert(&(cif)->cif_mtx, MA_OWNED)
25908b68b0eSGleb Smirnoff #define	CIF_LOCK(cif)		mtx_lock(&(cif)->cif_mtx)
26008b68b0eSGleb Smirnoff #define	CIF_UNLOCK(cif)		mtx_unlock(&(cif)->cif_mtx)
261a9a2c40cSGleb Smirnoff #define	CIF_FREE(cif)	do {				\
262a9a2c40cSGleb Smirnoff 		CIF_LOCK_ASSERT(cif);			\
263a9a2c40cSGleb Smirnoff 		if (TAILQ_EMPTY(&(cif)->cif_vrs))	\
264a9a2c40cSGleb Smirnoff 			carp_free_if(cif);		\
265a9a2c40cSGleb Smirnoff 		else					\
266a9a2c40cSGleb Smirnoff 			CIF_UNLOCK(cif);		\
267a9a2c40cSGleb Smirnoff } while (0)
268d220759bSGleb Smirnoff 
269947b7cf3SGleb Smirnoff #define	CARP_LOG(...)	do {				\
270c5c392e7SMikolaj Golub 	if (V_carp_log > 0)				\
27108b68b0eSGleb Smirnoff 		log(LOG_INFO, "carp: " __VA_ARGS__);	\
272947b7cf3SGleb Smirnoff } while (0)
2731e9e6572SGleb Smirnoff 
274947b7cf3SGleb Smirnoff #define	CARP_DEBUG(...)	do {				\
275c5c392e7SMikolaj Golub 	if (V_carp_log > 1)				\
2761e9e6572SGleb Smirnoff 		log(LOG_DEBUG, __VA_ARGS__);		\
277947b7cf3SGleb Smirnoff } while (0)
278a9771948SGleb Smirnoff 
27908b68b0eSGleb Smirnoff #define	IFNET_FOREACH_IFA(ifp, ifa)					\
28008b68b0eSGleb Smirnoff 	IF_ADDR_LOCK_ASSERT(ifp);					\
28108b68b0eSGleb Smirnoff 	TAILQ_FOREACH((ifa), &(ifp)->if_addrhead, ifa_link)		\
28208b68b0eSGleb Smirnoff 		if ((ifa)->ifa_carp != NULL)
28308b68b0eSGleb Smirnoff 
28408b68b0eSGleb Smirnoff #define	CARP_FOREACH_IFA(sc, ifa)					\
28508b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);						\
28608b68b0eSGleb Smirnoff 	for (int _i = 0;						\
28708b68b0eSGleb Smirnoff 		_i < (sc)->sc_naddrs + (sc)->sc_naddrs6 &&		\
28808b68b0eSGleb Smirnoff 		((ifa) = sc->sc_ifas[_i]) != NULL;			\
28908b68b0eSGleb Smirnoff 		++_i)
29008b68b0eSGleb Smirnoff 
29108b68b0eSGleb Smirnoff #define	IFNET_FOREACH_CARP(ifp, sc)					\
29208b68b0eSGleb Smirnoff 	CIF_LOCK_ASSERT(ifp->if_carp);					\
29308b68b0eSGleb Smirnoff 	TAILQ_FOREACH((sc), &(ifp)->if_carp->cif_vrs, sc_list)
29408b68b0eSGleb Smirnoff 
295f08535f8SGleb Smirnoff #define	DEMOTE_ADVSKEW(sc)					\
296c5c392e7SMikolaj Golub     (((sc)->sc_advskew + V_carp_demotion > CARP_MAXSKEW) ?	\
297c5c392e7SMikolaj Golub     CARP_MAXSKEW : ((sc)->sc_advskew + V_carp_demotion))
298f08535f8SGleb Smirnoff 
2995c1f0f6dSGleb Smirnoff static void	carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
30008b68b0eSGleb Smirnoff static struct carp_softc
30108b68b0eSGleb Smirnoff 		*carp_alloc(struct ifnet *);
302a9a2c40cSGleb Smirnoff static void	carp_detach_locked(struct ifaddr *);
30308b68b0eSGleb Smirnoff static void	carp_destroy(struct carp_softc *);
30408b68b0eSGleb Smirnoff static struct carp_if
30508b68b0eSGleb Smirnoff 		*carp_alloc_if(struct ifnet *);
30608b68b0eSGleb Smirnoff static void	carp_free_if(struct carp_if *);
30708b68b0eSGleb Smirnoff static void	carp_set_state(struct carp_softc *, int);
30808b68b0eSGleb Smirnoff static void	carp_sc_state(struct carp_softc *);
30908b68b0eSGleb Smirnoff static void	carp_setrun(struct carp_softc *, sa_family_t);
3105c1f0f6dSGleb Smirnoff static void	carp_master_down(void *);
311d220759bSGleb Smirnoff static void	carp_master_down_locked(struct carp_softc *);
31208b68b0eSGleb Smirnoff static void	carp_send_ad(void *);
31308b68b0eSGleb Smirnoff static void	carp_send_ad_locked(struct carp_softc *);
31408b68b0eSGleb Smirnoff static void	carp_addroute(struct carp_softc *);
3152512b096SGleb Smirnoff static void	carp_ifa_addroute(struct ifaddr *);
31608b68b0eSGleb Smirnoff static void	carp_delroute(struct carp_softc *);
3172512b096SGleb Smirnoff static void	carp_ifa_delroute(struct ifaddr *);
318f08535f8SGleb Smirnoff static void	carp_send_ad_all(void *, int);
319f08535f8SGleb Smirnoff static void	carp_demote_adj(int, char *);
320a9771948SGleb Smirnoff 
32108b68b0eSGleb Smirnoff static LIST_HEAD(, carp_softc) carp_list;
322d92d54d5SGleb Smirnoff static struct mtx carp_mtx;
323f08535f8SGleb Smirnoff static struct task carp_sendall_task =
324f08535f8SGleb Smirnoff     TASK_INITIALIZER(0, carp_send_ad_all, NULL);
325a9771948SGleb Smirnoff 
3265c1f0f6dSGleb Smirnoff static void
327a9771948SGleb Smirnoff carp_hmac_prepare(struct carp_softc *sc)
328a9771948SGleb Smirnoff {
32908b68b0eSGleb Smirnoff 	uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
33008b68b0eSGleb Smirnoff 	uint8_t vhid = sc->sc_vhid & 0xff;
331a9771948SGleb Smirnoff 	struct ifaddr *ifa;
3321ead26d4SMax Laier 	int i, found;
3331ead26d4SMax Laier #ifdef INET
3341ead26d4SMax Laier 	struct in_addr last, cur, in;
3351ead26d4SMax Laier #endif
336a9771948SGleb Smirnoff #ifdef INET6
3371ead26d4SMax Laier 	struct in6_addr last6, cur6, in6;
338a9771948SGleb Smirnoff #endif
339a9771948SGleb Smirnoff 
34008b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
341d220759bSGleb Smirnoff 
34208b68b0eSGleb Smirnoff 	/* Compute ipad from key. */
343a9771948SGleb Smirnoff 	bzero(sc->sc_pad, sizeof(sc->sc_pad));
344a9771948SGleb Smirnoff 	bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
345a9771948SGleb Smirnoff 	for (i = 0; i < sizeof(sc->sc_pad); i++)
346a9771948SGleb Smirnoff 		sc->sc_pad[i] ^= 0x36;
347a9771948SGleb Smirnoff 
34808b68b0eSGleb Smirnoff 	/* Precompute first part of inner hash. */
349a9771948SGleb Smirnoff 	SHA1Init(&sc->sc_sha1);
350a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
351a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
352a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
353a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
354a9771948SGleb Smirnoff #ifdef INET
3551ead26d4SMax Laier 	cur.s_addr = 0;
3561ead26d4SMax Laier 	do {
3571ead26d4SMax Laier 		found = 0;
3581ead26d4SMax Laier 		last = cur;
3591ead26d4SMax Laier 		cur.s_addr = 0xffffffff;
36008b68b0eSGleb Smirnoff 		CARP_FOREACH_IFA(sc, ifa) {
3611ead26d4SMax Laier 			in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr;
3621ead26d4SMax Laier 			if (ifa->ifa_addr->sa_family == AF_INET &&
3631ead26d4SMax Laier 			    ntohl(in.s_addr) > ntohl(last.s_addr) &&
3641ead26d4SMax Laier 			    ntohl(in.s_addr) < ntohl(cur.s_addr)) {
3651ead26d4SMax Laier 				cur.s_addr = in.s_addr;
3661ead26d4SMax Laier 				found++;
367a9771948SGleb Smirnoff 			}
3681ead26d4SMax Laier 		}
3691ead26d4SMax Laier 		if (found)
3701ead26d4SMax Laier 			SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur));
3711ead26d4SMax Laier 	} while (found);
372a9771948SGleb Smirnoff #endif /* INET */
373a9771948SGleb Smirnoff #ifdef INET6
3741ead26d4SMax Laier 	memset(&cur6, 0, sizeof(cur6));
3751ead26d4SMax Laier 	do {
3761ead26d4SMax Laier 		found = 0;
3771ead26d4SMax Laier 		last6 = cur6;
3781ead26d4SMax Laier 		memset(&cur6, 0xff, sizeof(cur6));
37908b68b0eSGleb Smirnoff 		CARP_FOREACH_IFA(sc, ifa) {
380a9771948SGleb Smirnoff 			in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
3811ead26d4SMax Laier 			if (IN6_IS_SCOPE_EMBED(&in6))
3821ead26d4SMax Laier 				in6.s6_addr16[1] = 0;
3831ead26d4SMax Laier 			if (ifa->ifa_addr->sa_family == AF_INET6 &&
3841ead26d4SMax Laier 			    memcmp(&in6, &last6, sizeof(in6)) > 0 &&
3851ead26d4SMax Laier 			    memcmp(&in6, &cur6, sizeof(in6)) < 0) {
3861ead26d4SMax Laier 				cur6 = in6;
3871ead26d4SMax Laier 				found++;
388a9771948SGleb Smirnoff 			}
389a9771948SGleb Smirnoff 		}
3901ead26d4SMax Laier 		if (found)
3911ead26d4SMax Laier 			SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6));
3921ead26d4SMax Laier 	} while (found);
393a9771948SGleb Smirnoff #endif /* INET6 */
394a9771948SGleb Smirnoff 
395a9771948SGleb Smirnoff 	/* convert ipad to opad */
396a9771948SGleb Smirnoff 	for (i = 0; i < sizeof(sc->sc_pad); i++)
397a9771948SGleb Smirnoff 		sc->sc_pad[i] ^= 0x36 ^ 0x5c;
398a9771948SGleb Smirnoff }
399a9771948SGleb Smirnoff 
4005c1f0f6dSGleb Smirnoff static void
40108b68b0eSGleb Smirnoff carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
402a9771948SGleb Smirnoff     unsigned char md[20])
403a9771948SGleb Smirnoff {
404a9771948SGleb Smirnoff 	SHA1_CTX sha1ctx;
405a9771948SGleb Smirnoff 
40608b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
40708b68b0eSGleb Smirnoff 
408a9771948SGleb Smirnoff 	/* fetch first half of inner hash */
409a9771948SGleb Smirnoff 	bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
410a9771948SGleb Smirnoff 
411a9771948SGleb Smirnoff 	SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
412a9771948SGleb Smirnoff 	SHA1Final(md, &sha1ctx);
413a9771948SGleb Smirnoff 
414a9771948SGleb Smirnoff 	/* outer hash */
415a9771948SGleb Smirnoff 	SHA1Init(&sha1ctx);
416a9771948SGleb Smirnoff 	SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
417a9771948SGleb Smirnoff 	SHA1Update(&sha1ctx, md, 20);
418a9771948SGleb Smirnoff 	SHA1Final(md, &sha1ctx);
419a9771948SGleb Smirnoff }
420a9771948SGleb Smirnoff 
4215c1f0f6dSGleb Smirnoff static int
42208b68b0eSGleb Smirnoff carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
423a9771948SGleb Smirnoff     unsigned char md[20])
424a9771948SGleb Smirnoff {
425a9771948SGleb Smirnoff 	unsigned char md2[20];
426a9771948SGleb Smirnoff 
42708b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
428d220759bSGleb Smirnoff 
429a9771948SGleb Smirnoff 	carp_hmac_generate(sc, counter, md2);
430a9771948SGleb Smirnoff 
431a9771948SGleb Smirnoff 	return (bcmp(md, md2, sizeof(md2)));
432a9771948SGleb Smirnoff }
433a9771948SGleb Smirnoff 
434a9771948SGleb Smirnoff /*
435a9771948SGleb Smirnoff  * process input packet.
436a9771948SGleb Smirnoff  * we have rearranged checks order compared to the rfc,
437a9771948SGleb Smirnoff  * but it seems more efficient this way or not possible otherwise.
438a9771948SGleb Smirnoff  */
439a0ae8f04SBjoern A. Zeeb #ifdef INET
4408f5a8818SKevin Lo int
4418f5a8818SKevin Lo carp_input(struct mbuf **mp, int *offp, int proto)
442a9771948SGleb Smirnoff {
4438f5a8818SKevin Lo 	struct mbuf *m = *mp;
444a9771948SGleb Smirnoff 	struct ip *ip = mtod(m, struct ip *);
445a9771948SGleb Smirnoff 	struct carp_header *ch;
446a9771948SGleb Smirnoff 	int iplen, len;
447a9771948SGleb Smirnoff 
4488f5a8818SKevin Lo 	iplen = *offp;
4498f5a8818SKevin Lo 	*mp = NULL;
4508f5a8818SKevin Lo 
4516bf65bcfSRobert Watson 	CARPSTATS_INC(carps_ipackets);
452a9771948SGleb Smirnoff 
453c5c392e7SMikolaj Golub 	if (!V_carp_allow) {
454a9771948SGleb Smirnoff 		m_freem(m);
4558f5a8818SKevin Lo 		return (IPPROTO_DONE);
456a9771948SGleb Smirnoff 	}
457a9771948SGleb Smirnoff 
458a9771948SGleb Smirnoff 	/* verify that the IP TTL is 255.  */
459a9771948SGleb Smirnoff 	if (ip->ip_ttl != CARP_DFLTTL) {
4606bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badttl);
46108b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
4621e9e6572SGleb Smirnoff 		    ip->ip_ttl,
4631e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
464a9771948SGleb Smirnoff 		m_freem(m);
4658f5a8818SKevin Lo 		return (IPPROTO_DONE);
466a9771948SGleb Smirnoff 	}
467a9771948SGleb Smirnoff 
468a9771948SGleb Smirnoff 	iplen = ip->ip_hl << 2;
469a9771948SGleb Smirnoff 
470a9771948SGleb Smirnoff 	if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
4716bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badlen);
47208b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: received len %zd < sizeof(struct carp_header) "
47308b68b0eSGleb Smirnoff 		    "on %s\n", __func__, m->m_len - sizeof(struct ip),
47416e324fcSXin LI 		    m->m_pkthdr.rcvif->if_xname);
475a9771948SGleb Smirnoff 		m_freem(m);
4768f5a8818SKevin Lo 		return (IPPROTO_DONE);
477a9771948SGleb Smirnoff 	}
478a9771948SGleb Smirnoff 
479a9771948SGleb Smirnoff 	if (iplen + sizeof(*ch) < m->m_len) {
480a9771948SGleb Smirnoff 		if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
4816bf65bcfSRobert Watson 			CARPSTATS_INC(carps_hdrops);
48208b68b0eSGleb Smirnoff 			CARP_DEBUG("%s: pullup failed\n", __func__);
4838f5a8818SKevin Lo 			return (IPPROTO_DONE);
484a9771948SGleb Smirnoff 		}
485a9771948SGleb Smirnoff 		ip = mtod(m, struct ip *);
486a9771948SGleb Smirnoff 	}
487a9771948SGleb Smirnoff 	ch = (struct carp_header *)((char *)ip + iplen);
488a9771948SGleb Smirnoff 
489a9771948SGleb Smirnoff 	/*
490a9771948SGleb Smirnoff 	 * verify that the received packet length is
491a9771948SGleb Smirnoff 	 * equal to the CARP header
492a9771948SGleb Smirnoff 	 */
493a9771948SGleb Smirnoff 	len = iplen + sizeof(*ch);
494a9771948SGleb Smirnoff 	if (len > m->m_pkthdr.len) {
4956bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badlen);
49608b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: packet too short %d on %s\n", __func__,
4971e9e6572SGleb Smirnoff 		    m->m_pkthdr.len,
4981e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
499a9771948SGleb Smirnoff 		m_freem(m);
5008f5a8818SKevin Lo 		return (IPPROTO_DONE);
501a9771948SGleb Smirnoff 	}
502a9771948SGleb Smirnoff 
503a9771948SGleb Smirnoff 	if ((m = m_pullup(m, len)) == NULL) {
5046bf65bcfSRobert Watson 		CARPSTATS_INC(carps_hdrops);
5058f5a8818SKevin Lo 		return (IPPROTO_DONE);
506a9771948SGleb Smirnoff 	}
507a9771948SGleb Smirnoff 	ip = mtod(m, struct ip *);
508a9771948SGleb Smirnoff 	ch = (struct carp_header *)((char *)ip + iplen);
509a9771948SGleb Smirnoff 
510a9771948SGleb Smirnoff 	/* verify the CARP checksum */
511a9771948SGleb Smirnoff 	m->m_data += iplen;
512c4d06976SGleb Smirnoff 	if (in_cksum(m, len - iplen)) {
5136bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badsum);
51408b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: checksum failed on %s\n", __func__,
5151e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
516a9771948SGleb Smirnoff 		m_freem(m);
5178f5a8818SKevin Lo 		return (IPPROTO_DONE);
518a9771948SGleb Smirnoff 	}
519a9771948SGleb Smirnoff 	m->m_data -= iplen;
520a9771948SGleb Smirnoff 
5211e9e6572SGleb Smirnoff 	carp_input_c(m, ch, AF_INET);
5228f5a8818SKevin Lo 	return (IPPROTO_DONE);
523a9771948SGleb Smirnoff }
524a0ae8f04SBjoern A. Zeeb #endif
525a9771948SGleb Smirnoff 
526a9771948SGleb Smirnoff #ifdef INET6
527a9771948SGleb Smirnoff int
528a9771948SGleb Smirnoff carp6_input(struct mbuf **mp, int *offp, int proto)
529a9771948SGleb Smirnoff {
530a9771948SGleb Smirnoff 	struct mbuf *m = *mp;
531a9771948SGleb Smirnoff 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
532a9771948SGleb Smirnoff 	struct carp_header *ch;
533a9771948SGleb Smirnoff 	u_int len;
534a9771948SGleb Smirnoff 
5356bf65bcfSRobert Watson 	CARPSTATS_INC(carps_ipackets6);
536a9771948SGleb Smirnoff 
537c5c392e7SMikolaj Golub 	if (!V_carp_allow) {
538a9771948SGleb Smirnoff 		m_freem(m);
539a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
540a9771948SGleb Smirnoff 	}
541a9771948SGleb Smirnoff 
542a9771948SGleb Smirnoff 	/* check if received on a valid carp interface */
543a9771948SGleb Smirnoff 	if (m->m_pkthdr.rcvif->if_carp == NULL) {
5446bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badif);
54508b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: packet received on non-carp interface: %s\n",
54608b68b0eSGleb Smirnoff 		    __func__, m->m_pkthdr.rcvif->if_xname);
547a9771948SGleb Smirnoff 		m_freem(m);
548a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
549a9771948SGleb Smirnoff 	}
550a9771948SGleb Smirnoff 
551a9771948SGleb Smirnoff 	/* verify that the IP TTL is 255 */
552a9771948SGleb Smirnoff 	if (ip6->ip6_hlim != CARP_DFLTTL) {
5536bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badttl);
55408b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
55508b68b0eSGleb Smirnoff 		    ip6->ip6_hlim, m->m_pkthdr.rcvif->if_xname);
556a9771948SGleb Smirnoff 		m_freem(m);
557a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
558a9771948SGleb Smirnoff 	}
559a9771948SGleb Smirnoff 
560a9771948SGleb Smirnoff 	/* verify that we have a complete carp packet */
561a9771948SGleb Smirnoff 	len = m->m_len;
562a9771948SGleb Smirnoff 	IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
563a9771948SGleb Smirnoff 	if (ch == NULL) {
5646bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badlen);
56508b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: packet size %u too small\n", __func__, len);
566a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
567a9771948SGleb Smirnoff 	}
568a9771948SGleb Smirnoff 
569a9771948SGleb Smirnoff 
570a9771948SGleb Smirnoff 	/* verify the CARP checksum */
571a9771948SGleb Smirnoff 	m->m_data += *offp;
572c4d06976SGleb Smirnoff 	if (in_cksum(m, sizeof(*ch))) {
5736bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badsum);
57408b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: checksum failed, on %s\n", __func__,
5751e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
576a9771948SGleb Smirnoff 		m_freem(m);
577a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
578a9771948SGleb Smirnoff 	}
579a9771948SGleb Smirnoff 	m->m_data -= *offp;
580a9771948SGleb Smirnoff 
5811e9e6572SGleb Smirnoff 	carp_input_c(m, ch, AF_INET6);
582a9771948SGleb Smirnoff 	return (IPPROTO_DONE);
583a9771948SGleb Smirnoff }
584a9771948SGleb Smirnoff #endif /* INET6 */
585a9771948SGleb Smirnoff 
5865c1f0f6dSGleb Smirnoff static void
5871e9e6572SGleb Smirnoff carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
588a9771948SGleb Smirnoff {
589a9771948SGleb Smirnoff 	struct ifnet *ifp = m->m_pkthdr.rcvif;
59008b68b0eSGleb Smirnoff 	struct ifaddr *ifa;
5911e9e6572SGleb Smirnoff 	struct carp_softc *sc;
59208b68b0eSGleb Smirnoff 	uint64_t tmp_counter;
593a9771948SGleb Smirnoff 	struct timeval sc_tv, ch_tv;
594a9771948SGleb Smirnoff 
595a9771948SGleb Smirnoff 	/* verify that the VHID is valid on the receiving interface */
596137f91e8SJohn Baldwin 	IF_ADDR_RLOCK(ifp);
59708b68b0eSGleb Smirnoff 	IFNET_FOREACH_IFA(ifp, ifa)
59808b68b0eSGleb Smirnoff 		if (ifa->ifa_addr->sa_family == af &&
59908b68b0eSGleb Smirnoff 		    ifa->ifa_carp->sc_vhid == ch->carp_vhid) {
60008b68b0eSGleb Smirnoff 			ifa_ref(ifa);
601a9771948SGleb Smirnoff 			break;
60208b68b0eSGleb Smirnoff 		}
603137f91e8SJohn Baldwin 	IF_ADDR_RUNLOCK(ifp);
604d220759bSGleb Smirnoff 
60508b68b0eSGleb Smirnoff 	if (ifa == NULL) {
6066bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badvhid);
607a9771948SGleb Smirnoff 		m_freem(m);
608a9771948SGleb Smirnoff 		return;
609a9771948SGleb Smirnoff 	}
610a9771948SGleb Smirnoff 
611a9771948SGleb Smirnoff 	/* verify the CARP version. */
612a9771948SGleb Smirnoff 	if (ch->carp_version != CARP_VERSION) {
6136bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badver);
61408b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: invalid version %d\n", ifp->if_xname,
6151e9e6572SGleb Smirnoff 		    ch->carp_version);
61608b68b0eSGleb Smirnoff 		ifa_free(ifa);
617a9771948SGleb Smirnoff 		m_freem(m);
618a9771948SGleb Smirnoff 		return;
619a9771948SGleb Smirnoff 	}
620a9771948SGleb Smirnoff 
62108b68b0eSGleb Smirnoff 	sc = ifa->ifa_carp;
62208b68b0eSGleb Smirnoff 	CARP_LOCK(sc);
62308b68b0eSGleb Smirnoff 	ifa_free(ifa);
62408b68b0eSGleb Smirnoff 
625a9771948SGleb Smirnoff 	if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
6266bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badauth);
62708b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: incorrect hash for VHID %u@%s\n", __func__,
62808b68b0eSGleb Smirnoff 		    sc->sc_vhid, ifp->if_xname);
62908b68b0eSGleb Smirnoff 		goto out;
630a9771948SGleb Smirnoff 	}
631a9771948SGleb Smirnoff 
632a9771948SGleb Smirnoff 	tmp_counter = ntohl(ch->carp_counter[0]);
633a9771948SGleb Smirnoff 	tmp_counter = tmp_counter<<32;
634a9771948SGleb Smirnoff 	tmp_counter += ntohl(ch->carp_counter[1]);
635a9771948SGleb Smirnoff 
636a9771948SGleb Smirnoff 	/* XXX Replay protection goes here */
637a9771948SGleb Smirnoff 
638a9771948SGleb Smirnoff 	sc->sc_init_counter = 0;
639a9771948SGleb Smirnoff 	sc->sc_counter = tmp_counter;
640a9771948SGleb Smirnoff 
641a9771948SGleb Smirnoff 	sc_tv.tv_sec = sc->sc_advbase;
642f08535f8SGleb Smirnoff 	sc_tv.tv_usec = DEMOTE_ADVSKEW(sc) * 1000000 / 256;
643a9771948SGleb Smirnoff 	ch_tv.tv_sec = ch->carp_advbase;
644a9771948SGleb Smirnoff 	ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
645a9771948SGleb Smirnoff 
646a9771948SGleb Smirnoff 	switch (sc->sc_state) {
647a9771948SGleb Smirnoff 	case INIT:
648a9771948SGleb Smirnoff 		break;
649a9771948SGleb Smirnoff 	case MASTER:
650a9771948SGleb Smirnoff 		/*
651a9771948SGleb Smirnoff 		 * If we receive an advertisement from a master who's going to
652a9771948SGleb Smirnoff 		 * be more frequent than us, go into BACKUP state.
653a9771948SGleb Smirnoff 		 */
654a9771948SGleb Smirnoff 		if (timevalcmp(&sc_tv, &ch_tv, >) ||
655a9771948SGleb Smirnoff 		    timevalcmp(&sc_tv, &ch_tv, ==)) {
656a9771948SGleb Smirnoff 			callout_stop(&sc->sc_ad_tmo);
65708b68b0eSGleb Smirnoff 			CARP_LOG("VHID %u@%s: MASTER -> BACKUP "
65888bf82a6SGleb Smirnoff 			    "(more frequent advertisement received)\n",
65908b68b0eSGleb Smirnoff 			    sc->sc_vhid,
66008b68b0eSGleb Smirnoff 			    sc->sc_carpdev->if_xname);
661a9771948SGleb Smirnoff 			carp_set_state(sc, BACKUP);
662a9771948SGleb Smirnoff 			carp_setrun(sc, 0);
66308b68b0eSGleb Smirnoff 			carp_delroute(sc);
664a9771948SGleb Smirnoff 		}
665a9771948SGleb Smirnoff 		break;
666a9771948SGleb Smirnoff 	case BACKUP:
667a9771948SGleb Smirnoff 		/*
668a9771948SGleb Smirnoff 		 * If we're pre-empting masters who advertise slower than us,
669a9771948SGleb Smirnoff 		 * and this one claims to be slower, treat him as down.
670a9771948SGleb Smirnoff 		 */
671c5c392e7SMikolaj Golub 		if (V_carp_preempt && timevalcmp(&sc_tv, &ch_tv, <)) {
67208b68b0eSGleb Smirnoff 			CARP_LOG("VHID %u@%s: BACKUP -> MASTER "
67388bf82a6SGleb Smirnoff 			    "(preempting a slower master)\n",
67408b68b0eSGleb Smirnoff 			    sc->sc_vhid,
67508b68b0eSGleb Smirnoff 			    sc->sc_carpdev->if_xname);
676d220759bSGleb Smirnoff 			carp_master_down_locked(sc);
677a9771948SGleb Smirnoff 			break;
678a9771948SGleb Smirnoff 		}
679a9771948SGleb Smirnoff 
680a9771948SGleb Smirnoff 		/*
681a9771948SGleb Smirnoff 		 *  If the master is going to advertise at such a low frequency
682a9771948SGleb Smirnoff 		 *  that he's guaranteed to time out, we'd might as well just
683a9771948SGleb Smirnoff 		 *  treat him as timed out now.
684a9771948SGleb Smirnoff 		 */
685a9771948SGleb Smirnoff 		sc_tv.tv_sec = sc->sc_advbase * 3;
686a9771948SGleb Smirnoff 		if (timevalcmp(&sc_tv, &ch_tv, <)) {
68708b68b0eSGleb Smirnoff 			CARP_LOG("VHID %u@%s: BACKUP -> MASTER "
68888bf82a6SGleb Smirnoff 			    "(master timed out)\n",
68908b68b0eSGleb Smirnoff 			    sc->sc_vhid,
69008b68b0eSGleb Smirnoff 			    sc->sc_carpdev->if_xname);
691d220759bSGleb Smirnoff 			carp_master_down_locked(sc);
692a9771948SGleb Smirnoff 			break;
693a9771948SGleb Smirnoff 		}
694a9771948SGleb Smirnoff 
695a9771948SGleb Smirnoff 		/*
696a9771948SGleb Smirnoff 		 * Otherwise, we reset the counter and wait for the next
697a9771948SGleb Smirnoff 		 * advertisement.
698a9771948SGleb Smirnoff 		 */
699a9771948SGleb Smirnoff 		carp_setrun(sc, af);
700a9771948SGleb Smirnoff 		break;
701a9771948SGleb Smirnoff 	}
702a9771948SGleb Smirnoff 
70308b68b0eSGleb Smirnoff out:
70408b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
705a9771948SGleb Smirnoff 	m_freem(m);
706a9771948SGleb Smirnoff }
707a9771948SGleb Smirnoff 
7085c1f0f6dSGleb Smirnoff static int
709a9771948SGleb Smirnoff carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
710a9771948SGleb Smirnoff {
711a9771948SGleb Smirnoff 	struct m_tag *mtag;
712a9771948SGleb Smirnoff 
713a9771948SGleb Smirnoff 	if (sc->sc_init_counter) {
714a9771948SGleb Smirnoff 		/* this could also be seconds since unix epoch */
715a9771948SGleb Smirnoff 		sc->sc_counter = arc4random();
716a9771948SGleb Smirnoff 		sc->sc_counter = sc->sc_counter << 32;
717a9771948SGleb Smirnoff 		sc->sc_counter += arc4random();
718a9771948SGleb Smirnoff 	} else
719a9771948SGleb Smirnoff 		sc->sc_counter++;
720a9771948SGleb Smirnoff 
721a9771948SGleb Smirnoff 	ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
722a9771948SGleb Smirnoff 	ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
723a9771948SGleb Smirnoff 
724a9771948SGleb Smirnoff 	carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
725a9771948SGleb Smirnoff 
726a9771948SGleb Smirnoff 	/* Tag packet for carp_output */
72708b68b0eSGleb Smirnoff 	if ((mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct carp_softc *),
72808b68b0eSGleb Smirnoff 	    M_NOWAIT)) == NULL) {
729a9771948SGleb Smirnoff 		m_freem(m);
73008b68b0eSGleb Smirnoff 		CARPSTATS_INC(carps_onomem);
731a9771948SGleb Smirnoff 		return (ENOMEM);
732a9771948SGleb Smirnoff 	}
733eaf151c4SGleb Smirnoff 	bcopy(&sc, mtag + 1, sizeof(sc));
734a9771948SGleb Smirnoff 	m_tag_prepend(m, mtag);
735a9771948SGleb Smirnoff 
736a9771948SGleb Smirnoff 	return (0);
737a9771948SGleb Smirnoff }
738a9771948SGleb Smirnoff 
739f08535f8SGleb Smirnoff /*
740f08535f8SGleb Smirnoff  * To avoid LORs and possible recursions this function shouldn't
741f08535f8SGleb Smirnoff  * be called directly, but scheduled via taskqueue.
742f08535f8SGleb Smirnoff  */
7435c1f0f6dSGleb Smirnoff static void
744f08535f8SGleb Smirnoff carp_send_ad_all(void *ctx __unused, int pending __unused)
745a9771948SGleb Smirnoff {
746d92d54d5SGleb Smirnoff 	struct carp_softc *sc;
747a9771948SGleb Smirnoff 
748d92d54d5SGleb Smirnoff 	mtx_lock(&carp_mtx);
74908b68b0eSGleb Smirnoff 	LIST_FOREACH(sc, &carp_list, sc_next)
750f08535f8SGleb Smirnoff 		if (sc->sc_state == MASTER) {
75108b68b0eSGleb Smirnoff 			CARP_LOCK(sc);
752afdbac98SGleb Smirnoff 			CURVNET_SET(sc->sc_carpdev->if_vnet);
753d220759bSGleb Smirnoff 			carp_send_ad_locked(sc);
754afdbac98SGleb Smirnoff 			CURVNET_RESTORE();
75508b68b0eSGleb Smirnoff 			CARP_UNLOCK(sc);
756a9771948SGleb Smirnoff 		}
757d92d54d5SGleb Smirnoff 	mtx_unlock(&carp_mtx);
758a9771948SGleb Smirnoff }
759a9771948SGleb Smirnoff 
760afdbac98SGleb Smirnoff /* Send a periodic advertisement, executed in callout context. */
7615c1f0f6dSGleb Smirnoff static void
762a9771948SGleb Smirnoff carp_send_ad(void *v)
763a9771948SGleb Smirnoff {
764d220759bSGleb Smirnoff 	struct carp_softc *sc = v;
765d220759bSGleb Smirnoff 
76608b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
767afdbac98SGleb Smirnoff 	CURVNET_SET(sc->sc_carpdev->if_vnet);
768d220759bSGleb Smirnoff 	carp_send_ad_locked(sc);
769afdbac98SGleb Smirnoff 	CURVNET_RESTORE();
77008b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
771d220759bSGleb Smirnoff }
772d220759bSGleb Smirnoff 
773d220759bSGleb Smirnoff static void
7744a2dd8d4SGleb Smirnoff carp_send_ad_error(struct carp_softc *sc, int error)
7754a2dd8d4SGleb Smirnoff {
7764a2dd8d4SGleb Smirnoff 
7774a2dd8d4SGleb Smirnoff 	if (error) {
7784a2dd8d4SGleb Smirnoff 		if (sc->sc_sendad_errors < INT_MAX)
7794a2dd8d4SGleb Smirnoff 			sc->sc_sendad_errors++;
7804a2dd8d4SGleb Smirnoff 		if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
7814a2dd8d4SGleb Smirnoff 			static const char fmt[] = "send error %d on %s";
7824a2dd8d4SGleb Smirnoff 			char msg[sizeof(fmt) + IFNAMSIZ];
7834a2dd8d4SGleb Smirnoff 
7844a2dd8d4SGleb Smirnoff 			sprintf(msg, fmt, error, sc->sc_carpdev->if_xname);
7854a2dd8d4SGleb Smirnoff 			carp_demote_adj(V_carp_senderr_adj, msg);
7864a2dd8d4SGleb Smirnoff 		}
7874a2dd8d4SGleb Smirnoff 		sc->sc_sendad_success = 0;
7884a2dd8d4SGleb Smirnoff 	} else {
7894a2dd8d4SGleb Smirnoff 		if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS &&
7904a2dd8d4SGleb Smirnoff 		    ++sc->sc_sendad_success >= CARP_SENDAD_MIN_SUCCESS) {
7914a2dd8d4SGleb Smirnoff 			static const char fmt[] = "send ok on %s";
7924a2dd8d4SGleb Smirnoff 			char msg[sizeof(fmt) + IFNAMSIZ];
7934a2dd8d4SGleb Smirnoff 
7944a2dd8d4SGleb Smirnoff 			sprintf(msg, fmt, sc->sc_carpdev->if_xname);
7954a2dd8d4SGleb Smirnoff 			carp_demote_adj(-V_carp_senderr_adj, msg);
7964a2dd8d4SGleb Smirnoff 			sc->sc_sendad_errors = 0;
7974a2dd8d4SGleb Smirnoff 		} else
7984a2dd8d4SGleb Smirnoff 			sc->sc_sendad_errors = 0;
7994a2dd8d4SGleb Smirnoff 	}
8004a2dd8d4SGleb Smirnoff }
8014a2dd8d4SGleb Smirnoff 
8024a2dd8d4SGleb Smirnoff static void
803d220759bSGleb Smirnoff carp_send_ad_locked(struct carp_softc *sc)
804d220759bSGleb Smirnoff {
805a9771948SGleb Smirnoff 	struct carp_header ch;
806a9771948SGleb Smirnoff 	struct timeval tv;
80708b68b0eSGleb Smirnoff 	struct sockaddr sa;
80808b68b0eSGleb Smirnoff 	struct ifaddr *ifa;
809a9771948SGleb Smirnoff 	struct carp_header *ch_ptr;
810a9771948SGleb Smirnoff 	struct mbuf *m;
81108b68b0eSGleb Smirnoff 	int len, advskew;
812a9771948SGleb Smirnoff 
81308b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
814d220759bSGleb Smirnoff 
815f08535f8SGleb Smirnoff 	advskew = DEMOTE_ADVSKEW(sc);
81608b68b0eSGleb Smirnoff 	tv.tv_sec = sc->sc_advbase;
817a9771948SGleb Smirnoff 	tv.tv_usec = advskew * 1000000 / 256;
818a9771948SGleb Smirnoff 
819a9771948SGleb Smirnoff 	ch.carp_version = CARP_VERSION;
820a9771948SGleb Smirnoff 	ch.carp_type = CARP_ADVERTISEMENT;
821a9771948SGleb Smirnoff 	ch.carp_vhid = sc->sc_vhid;
82208b68b0eSGleb Smirnoff 	ch.carp_advbase = sc->sc_advbase;
823a9771948SGleb Smirnoff 	ch.carp_advskew = advskew;
824a9771948SGleb Smirnoff 	ch.carp_authlen = 7;	/* XXX DEFINE */
825a9771948SGleb Smirnoff 	ch.carp_pad1 = 0;	/* must be zero */
826a9771948SGleb Smirnoff 	ch.carp_cksum = 0;
827a9771948SGleb Smirnoff 
82808b68b0eSGleb Smirnoff 	/* XXXGL: OpenBSD picks first ifaddr with needed family. */
82908b68b0eSGleb Smirnoff 
830a9771948SGleb Smirnoff #ifdef INET
83108b68b0eSGleb Smirnoff 	if (sc->sc_naddrs) {
832a9771948SGleb Smirnoff 		struct ip *ip;
833a9771948SGleb Smirnoff 
834dc4ad05eSGleb Smirnoff 		m = m_gethdr(M_NOWAIT, MT_DATA);
835a9771948SGleb Smirnoff 		if (m == NULL) {
8366bf65bcfSRobert Watson 			CARPSTATS_INC(carps_onomem);
837891122d1SGleb Smirnoff 			goto resched;
838a9771948SGleb Smirnoff 		}
839a9771948SGleb Smirnoff 		len = sizeof(*ip) + sizeof(ch);
840a9771948SGleb Smirnoff 		m->m_pkthdr.len = len;
841a9771948SGleb Smirnoff 		m->m_pkthdr.rcvif = NULL;
842a9771948SGleb Smirnoff 		m->m_len = len;
843*ed6a66caSRobert Watson 		M_ALIGN(m, m->m_len);
844a9771948SGleb Smirnoff 		m->m_flags |= M_MCAST;
845a9771948SGleb Smirnoff 		ip = mtod(m, struct ip *);
846a9771948SGleb Smirnoff 		ip->ip_v = IPVERSION;
847a9771948SGleb Smirnoff 		ip->ip_hl = sizeof(*ip) >> 2;
848a9771948SGleb Smirnoff 		ip->ip_tos = IPTOS_LOWDELAY;
8498f134647SGleb Smirnoff 		ip->ip_len = htons(len);
850a9771948SGleb Smirnoff 		ip->ip_id = ip_newid();
8518f134647SGleb Smirnoff 		ip->ip_off = htons(IP_DF);
852a9771948SGleb Smirnoff 		ip->ip_ttl = CARP_DFLTTL;
853a9771948SGleb Smirnoff 		ip->ip_p = IPPROTO_CARP;
854a9771948SGleb Smirnoff 		ip->ip_sum = 0;
85508b68b0eSGleb Smirnoff 
85608b68b0eSGleb Smirnoff 		bzero(&sa, sizeof(sa));
85708b68b0eSGleb Smirnoff 		sa.sa_family = AF_INET;
85808b68b0eSGleb Smirnoff 		ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
85908b68b0eSGleb Smirnoff 		if (ifa != NULL) {
86008b68b0eSGleb Smirnoff 			ip->ip_src.s_addr =
86108b68b0eSGleb Smirnoff 			    ifatoia(ifa)->ia_addr.sin_addr.s_addr;
86208b68b0eSGleb Smirnoff 			ifa_free(ifa);
86308b68b0eSGleb Smirnoff 		} else
86408b68b0eSGleb Smirnoff 			ip->ip_src.s_addr = 0;
865a9771948SGleb Smirnoff 		ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
866a9771948SGleb Smirnoff 
867a9771948SGleb Smirnoff 		ch_ptr = (struct carp_header *)(&ip[1]);
868a9771948SGleb Smirnoff 		bcopy(&ch, ch_ptr, sizeof(ch));
869a9771948SGleb Smirnoff 		if (carp_prepare_ad(m, sc, ch_ptr))
870891122d1SGleb Smirnoff 			goto resched;
871a9771948SGleb Smirnoff 
872a9771948SGleb Smirnoff 		m->m_data += sizeof(*ip);
873c4d06976SGleb Smirnoff 		ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip));
874a9771948SGleb Smirnoff 		m->m_data -= sizeof(*ip);
875a9771948SGleb Smirnoff 
8766bf65bcfSRobert Watson 		CARPSTATS_INC(carps_opackets);
877a9771948SGleb Smirnoff 
8784a2dd8d4SGleb Smirnoff 		carp_send_ad_error(sc, ip_output(m, NULL, NULL, IP_RAWOUTPUT,
8794a2dd8d4SGleb Smirnoff 		    &sc->sc_carpdev->if_carp->cif_imo, NULL));
880a9771948SGleb Smirnoff 	}
881a9771948SGleb Smirnoff #endif /* INET */
882a9771948SGleb Smirnoff #ifdef INET6
88308b68b0eSGleb Smirnoff 	if (sc->sc_naddrs6) {
884a9771948SGleb Smirnoff 		struct ip6_hdr *ip6;
885a9771948SGleb Smirnoff 
886dc4ad05eSGleb Smirnoff 		m = m_gethdr(M_NOWAIT, MT_DATA);
887a9771948SGleb Smirnoff 		if (m == NULL) {
8886bf65bcfSRobert Watson 			CARPSTATS_INC(carps_onomem);
889891122d1SGleb Smirnoff 			goto resched;
890a9771948SGleb Smirnoff 		}
891a9771948SGleb Smirnoff 		len = sizeof(*ip6) + sizeof(ch);
892a9771948SGleb Smirnoff 		m->m_pkthdr.len = len;
893a9771948SGleb Smirnoff 		m->m_pkthdr.rcvif = NULL;
894a9771948SGleb Smirnoff 		m->m_len = len;
895*ed6a66caSRobert Watson 		M_ALIGN(m, m->m_len);
896a9771948SGleb Smirnoff 		m->m_flags |= M_MCAST;
897a9771948SGleb Smirnoff 		ip6 = mtod(m, struct ip6_hdr *);
898a9771948SGleb Smirnoff 		bzero(ip6, sizeof(*ip6));
899a9771948SGleb Smirnoff 		ip6->ip6_vfc |= IPV6_VERSION;
900a9771948SGleb Smirnoff 		ip6->ip6_hlim = CARP_DFLTTL;
901a9771948SGleb Smirnoff 		ip6->ip6_nxt = IPPROTO_CARP;
90208b68b0eSGleb Smirnoff 		bzero(&sa, sizeof(sa));
903a9771948SGleb Smirnoff 
90408b68b0eSGleb Smirnoff 		/* set the source address */
90508b68b0eSGleb Smirnoff 		sa.sa_family = AF_INET6;
90608b68b0eSGleb Smirnoff 		ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
90708b68b0eSGleb Smirnoff 		if (ifa != NULL) {
90808b68b0eSGleb Smirnoff 			bcopy(IFA_IN6(ifa), &ip6->ip6_src,
90908b68b0eSGleb Smirnoff 			    sizeof(struct in6_addr));
91008b68b0eSGleb Smirnoff 			ifa_free(ifa);
91108b68b0eSGleb Smirnoff 		} else
91208b68b0eSGleb Smirnoff 			/* This should never happen with IPv6. */
91308b68b0eSGleb Smirnoff 			bzero(&ip6->ip6_src, sizeof(struct in6_addr));
91408b68b0eSGleb Smirnoff 
91508b68b0eSGleb Smirnoff 		/* Set the multicast destination. */
9167002145dSBjoern A. Zeeb 		ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
917a9771948SGleb Smirnoff 		ip6->ip6_dst.s6_addr8[15] = 0x12;
9187002145dSBjoern A. Zeeb 		if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
9197002145dSBjoern A. Zeeb 			m_freem(m);
920e81ab876SGleb Smirnoff 			CARP_DEBUG("%s: in6_setscope failed\n", __func__);
921891122d1SGleb Smirnoff 			goto resched;
9227002145dSBjoern A. Zeeb 		}
923a9771948SGleb Smirnoff 
924a9771948SGleb Smirnoff 		ch_ptr = (struct carp_header *)(&ip6[1]);
925a9771948SGleb Smirnoff 		bcopy(&ch, ch_ptr, sizeof(ch));
926a9771948SGleb Smirnoff 		if (carp_prepare_ad(m, sc, ch_ptr))
927891122d1SGleb Smirnoff 			goto resched;
928a9771948SGleb Smirnoff 
929a9771948SGleb Smirnoff 		m->m_data += sizeof(*ip6);
930c4d06976SGleb Smirnoff 		ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip6));
931a9771948SGleb Smirnoff 		m->m_data -= sizeof(*ip6);
932a9771948SGleb Smirnoff 
9336bf65bcfSRobert Watson 		CARPSTATS_INC(carps_opackets6);
934a9771948SGleb Smirnoff 
9354a2dd8d4SGleb Smirnoff 		carp_send_ad_error(sc, ip6_output(m, NULL, NULL, 0,
9364a2dd8d4SGleb Smirnoff 		    &sc->sc_carpdev->if_carp->cif_im6o, NULL, NULL));
937a9771948SGleb Smirnoff 	}
938a9771948SGleb Smirnoff #endif /* INET6 */
939a9771948SGleb Smirnoff 
940891122d1SGleb Smirnoff resched:
94108b68b0eSGleb Smirnoff 	callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), carp_send_ad, sc);
94208b68b0eSGleb Smirnoff }
943a9771948SGleb Smirnoff 
94408b68b0eSGleb Smirnoff static void
94508b68b0eSGleb Smirnoff carp_addroute(struct carp_softc *sc)
94608b68b0eSGleb Smirnoff {
94708b68b0eSGleb Smirnoff 	struct ifaddr *ifa;
94808b68b0eSGleb Smirnoff 
94908b68b0eSGleb Smirnoff 	CARP_FOREACH_IFA(sc, ifa)
9502512b096SGleb Smirnoff 		carp_ifa_addroute(ifa);
9512512b096SGleb Smirnoff }
9522512b096SGleb Smirnoff 
9532512b096SGleb Smirnoff static void
9542512b096SGleb Smirnoff carp_ifa_addroute(struct ifaddr *ifa)
9552512b096SGleb Smirnoff {
9562512b096SGleb Smirnoff 
95708b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
95808b68b0eSGleb Smirnoff #ifdef INET
95908b68b0eSGleb Smirnoff 	case AF_INET:
96008b68b0eSGleb Smirnoff 		in_addprefix(ifatoia(ifa), RTF_UP);
96108b68b0eSGleb Smirnoff 		ifa_add_loopback_route(ifa,
96208b68b0eSGleb Smirnoff 		    (struct sockaddr *)&ifatoia(ifa)->ia_addr);
96308b68b0eSGleb Smirnoff 		break;
96408b68b0eSGleb Smirnoff #endif
96508b68b0eSGleb Smirnoff #ifdef INET6
96608b68b0eSGleb Smirnoff 	case AF_INET6:
96708b68b0eSGleb Smirnoff 		ifa_add_loopback_route(ifa,
96808b68b0eSGleb Smirnoff 		    (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
969f6b84910SAlexander V. Chernikov 		nd6_add_ifa_lle(ifatoia6(ifa));
97008b68b0eSGleb Smirnoff 		break;
97108b68b0eSGleb Smirnoff #endif
97208b68b0eSGleb Smirnoff 	}
97308b68b0eSGleb Smirnoff }
97408b68b0eSGleb Smirnoff 
97508b68b0eSGleb Smirnoff static void
97608b68b0eSGleb Smirnoff carp_delroute(struct carp_softc *sc)
97708b68b0eSGleb Smirnoff {
97808b68b0eSGleb Smirnoff 	struct ifaddr *ifa;
97908b68b0eSGleb Smirnoff 
98008b68b0eSGleb Smirnoff 	CARP_FOREACH_IFA(sc, ifa)
9812512b096SGleb Smirnoff 		carp_ifa_delroute(ifa);
9822512b096SGleb Smirnoff }
9832512b096SGleb Smirnoff 
9842512b096SGleb Smirnoff static void
9852512b096SGleb Smirnoff carp_ifa_delroute(struct ifaddr *ifa)
9862512b096SGleb Smirnoff {
9872512b096SGleb Smirnoff 
98808b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
98908b68b0eSGleb Smirnoff #ifdef INET
99008b68b0eSGleb Smirnoff 	case AF_INET:
99108b68b0eSGleb Smirnoff 		ifa_del_loopback_route(ifa,
99208b68b0eSGleb Smirnoff 		    (struct sockaddr *)&ifatoia(ifa)->ia_addr);
99308b68b0eSGleb Smirnoff 		in_scrubprefix(ifatoia(ifa), LLE_STATIC);
99408b68b0eSGleb Smirnoff 		break;
99508b68b0eSGleb Smirnoff #endif
99608b68b0eSGleb Smirnoff #ifdef INET6
99708b68b0eSGleb Smirnoff 	case AF_INET6:
99808b68b0eSGleb Smirnoff 		ifa_del_loopback_route(ifa,
99908b68b0eSGleb Smirnoff 		    (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
1000f6b84910SAlexander V. Chernikov 		nd6_rem_ifa_lle(ifatoia6(ifa));
100108b68b0eSGleb Smirnoff 		break;
100208b68b0eSGleb Smirnoff #endif
100308b68b0eSGleb Smirnoff 	}
1004a9771948SGleb Smirnoff }
1005a9771948SGleb Smirnoff 
100624421c1cSGleb Smirnoff int
100724421c1cSGleb Smirnoff carp_master(struct ifaddr *ifa)
100824421c1cSGleb Smirnoff {
100924421c1cSGleb Smirnoff 	struct carp_softc *sc = ifa->ifa_carp;
101024421c1cSGleb Smirnoff 
101124421c1cSGleb Smirnoff 	return (sc->sc_state == MASTER);
101224421c1cSGleb Smirnoff }
101324421c1cSGleb Smirnoff 
1014a0ae8f04SBjoern A. Zeeb #ifdef INET
1015a9771948SGleb Smirnoff /*
1016a9771948SGleb Smirnoff  * Broadcast a gratuitous ARP request containing
1017a9771948SGleb Smirnoff  * the virtual router MAC address for each IP address
1018a9771948SGleb Smirnoff  * associated with the virtual router.
1019a9771948SGleb Smirnoff  */
10205c1f0f6dSGleb Smirnoff static void
1021a9771948SGleb Smirnoff carp_send_arp(struct carp_softc *sc)
1022a9771948SGleb Smirnoff {
1023a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1024a9771948SGleb Smirnoff 
102508b68b0eSGleb Smirnoff 	CARP_FOREACH_IFA(sc, ifa)
102608b68b0eSGleb Smirnoff 		if (ifa->ifa_addr->sa_family == AF_INET)
102708b68b0eSGleb Smirnoff 			arp_ifinit2(sc->sc_carpdev, ifa, LLADDR(&sc->sc_addr));
1028a9771948SGleb Smirnoff }
102908b68b0eSGleb Smirnoff 
103008b68b0eSGleb Smirnoff int
103108b68b0eSGleb Smirnoff carp_iamatch(struct ifaddr *ifa, uint8_t **enaddr)
103208b68b0eSGleb Smirnoff {
103308b68b0eSGleb Smirnoff 	struct carp_softc *sc = ifa->ifa_carp;
103408b68b0eSGleb Smirnoff 
103508b68b0eSGleb Smirnoff 	if (sc->sc_state == MASTER) {
103608b68b0eSGleb Smirnoff 		*enaddr = LLADDR(&sc->sc_addr);
103708b68b0eSGleb Smirnoff 		return (1);
103808b68b0eSGleb Smirnoff 	}
103908b68b0eSGleb Smirnoff 
104008b68b0eSGleb Smirnoff 	return (0);
1041a9771948SGleb Smirnoff }
1042a0ae8f04SBjoern A. Zeeb #endif
1043a9771948SGleb Smirnoff 
1044a9771948SGleb Smirnoff #ifdef INET6
10455c1f0f6dSGleb Smirnoff static void
1046a9771948SGleb Smirnoff carp_send_na(struct carp_softc *sc)
1047a9771948SGleb Smirnoff {
104808b68b0eSGleb Smirnoff 	static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1049a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1050a9771948SGleb Smirnoff 	struct in6_addr *in6;
1051a9771948SGleb Smirnoff 
105208b68b0eSGleb Smirnoff 	CARP_FOREACH_IFA(sc, ifa) {
1053a9771948SGleb Smirnoff 		if (ifa->ifa_addr->sa_family != AF_INET6)
1054a9771948SGleb Smirnoff 			continue;
1055a9771948SGleb Smirnoff 
105608b68b0eSGleb Smirnoff 		in6 = IFA_IN6(ifa);
1057e8c34a71SGleb Smirnoff 		nd6_na_output(sc->sc_carpdev, &mcast, in6,
1058a9771948SGleb Smirnoff 		    ND_NA_FLAG_OVERRIDE, 1, NULL);
1059a9771948SGleb Smirnoff 		DELAY(1000);	/* XXX */
1060a9771948SGleb Smirnoff 	}
1061a9771948SGleb Smirnoff }
1062a9771948SGleb Smirnoff 
10638253dcabSBjoern A. Zeeb /*
10648253dcabSBjoern A. Zeeb  * Returns ifa in case it's a carp address and it is MASTER, or if the address
10658253dcabSBjoern A. Zeeb  * matches and is not a carp address.  Returns NULL otherwise.
10668253dcabSBjoern A. Zeeb  */
1067a4e53905SMax Laier struct ifaddr *
106854bfbd51SWill Andrews carp_iamatch6(struct ifnet *ifp, struct in6_addr *taddr)
1069a9771948SGleb Smirnoff {
1070a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1071a9771948SGleb Smirnoff 
10728253dcabSBjoern A. Zeeb 	ifa = NULL;
1073137f91e8SJohn Baldwin 	IF_ADDR_RLOCK(ifp);
10748253dcabSBjoern A. Zeeb 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
10758253dcabSBjoern A. Zeeb 		if (ifa->ifa_addr->sa_family != AF_INET6)
10768253dcabSBjoern A. Zeeb 			continue;
10778253dcabSBjoern A. Zeeb 		if (!IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa)))
10788253dcabSBjoern A. Zeeb 			continue;
10798253dcabSBjoern A. Zeeb 		if (ifa->ifa_carp && ifa->ifa_carp->sc_state != MASTER)
10808253dcabSBjoern A. Zeeb 			ifa = NULL;
10818253dcabSBjoern A. Zeeb 		else
10828c0fec80SRobert Watson 			ifa_ref(ifa);
10838253dcabSBjoern A. Zeeb 		break;
1084a9771948SGleb Smirnoff 	}
1085137f91e8SJohn Baldwin 	IF_ADDR_RUNLOCK(ifp);
1086a9771948SGleb Smirnoff 
10878253dcabSBjoern A. Zeeb 	return (ifa);
1088a9771948SGleb Smirnoff }
1089a9771948SGleb Smirnoff 
109054bfbd51SWill Andrews caddr_t
109154bfbd51SWill Andrews carp_macmatch6(struct ifnet *ifp, struct mbuf *m, const struct in6_addr *taddr)
1092a9771948SGleb Smirnoff {
1093a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1094a9771948SGleb Smirnoff 
1095137f91e8SJohn Baldwin 	IF_ADDR_RLOCK(ifp);
109608b68b0eSGleb Smirnoff 	IFNET_FOREACH_IFA(ifp, ifa)
109708b68b0eSGleb Smirnoff 		if (ifa->ifa_addr->sa_family == AF_INET6 &&
109808b68b0eSGleb Smirnoff 		    IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) {
109908b68b0eSGleb Smirnoff 			struct carp_softc *sc = ifa->ifa_carp;
110008b68b0eSGleb Smirnoff 			struct m_tag *mtag;
110108b68b0eSGleb Smirnoff 
1102137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
110308b68b0eSGleb Smirnoff 
1104a9771948SGleb Smirnoff 			mtag = m_tag_get(PACKET_TAG_CARP,
1105a856ddc6SGleb Smirnoff 			    sizeof(struct carp_softc *), M_NOWAIT);
110608b68b0eSGleb Smirnoff 			if (mtag == NULL)
110708b68b0eSGleb Smirnoff 				/* Better a bit than nothing. */
110808b68b0eSGleb Smirnoff 				return (LLADDR(&sc->sc_addr));
110908b68b0eSGleb Smirnoff 
1110eaf151c4SGleb Smirnoff 			bcopy(&sc, mtag + 1, sizeof(sc));
1111a9771948SGleb Smirnoff 			m_tag_prepend(m, mtag);
1112a9771948SGleb Smirnoff 
111308b68b0eSGleb Smirnoff 			return (LLADDR(&sc->sc_addr));
1114a9771948SGleb Smirnoff 		}
1115137f91e8SJohn Baldwin 	IF_ADDR_RUNLOCK(ifp);
1116a9771948SGleb Smirnoff 
1117a9771948SGleb Smirnoff 	return (NULL);
1118a9771948SGleb Smirnoff }
111908b68b0eSGleb Smirnoff #endif /* INET6 */
1120a9771948SGleb Smirnoff 
112108b68b0eSGleb Smirnoff int
112254bfbd51SWill Andrews carp_forus(struct ifnet *ifp, u_char *dhost)
1123a9771948SGleb Smirnoff {
112408b68b0eSGleb Smirnoff 	struct carp_softc *sc;
112508b68b0eSGleb Smirnoff 	uint8_t *ena = dhost;
1126a9771948SGleb Smirnoff 
1127a9771948SGleb Smirnoff 	if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
112808b68b0eSGleb Smirnoff 		return (0);
1129a9771948SGleb Smirnoff 
113008b68b0eSGleb Smirnoff 	CIF_LOCK(ifp->if_carp);
113108b68b0eSGleb Smirnoff 	IFNET_FOREACH_CARP(ifp, sc) {
113208b68b0eSGleb Smirnoff 		CARP_LOCK(sc);
113308b68b0eSGleb Smirnoff 		if (sc->sc_state == MASTER && !bcmp(dhost, LLADDR(&sc->sc_addr),
113408b68b0eSGleb Smirnoff 		    ETHER_ADDR_LEN)) {
113508b68b0eSGleb Smirnoff 			CARP_UNLOCK(sc);
113608b68b0eSGleb Smirnoff 			CIF_UNLOCK(ifp->if_carp);
113708b68b0eSGleb Smirnoff 			return (1);
1138a9771948SGleb Smirnoff 		}
113908b68b0eSGleb Smirnoff 		CARP_UNLOCK(sc);
114008b68b0eSGleb Smirnoff 	}
114108b68b0eSGleb Smirnoff 	CIF_UNLOCK(ifp->if_carp);
1142a9771948SGleb Smirnoff 
114308b68b0eSGleb Smirnoff 	return (0);
1144a9771948SGleb Smirnoff }
1145a9771948SGleb Smirnoff 
1146afdbac98SGleb Smirnoff /* Master down timeout event, executed in callout context. */
11475c1f0f6dSGleb Smirnoff static void
1148a9771948SGleb Smirnoff carp_master_down(void *v)
1149a9771948SGleb Smirnoff {
1150a9771948SGleb Smirnoff 	struct carp_softc *sc = v;
1151a9771948SGleb Smirnoff 
115208b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
115308b68b0eSGleb Smirnoff 
1154afdbac98SGleb Smirnoff 	CURVNET_SET(sc->sc_carpdev->if_vnet);
115508b68b0eSGleb Smirnoff 	if (sc->sc_state == BACKUP) {
11561c435c73SGleb Smirnoff 		CARP_LOG("VHID %u@%s: BACKUP -> MASTER (master down)\n",
115708b68b0eSGleb Smirnoff 		    sc->sc_vhid,
115808b68b0eSGleb Smirnoff 		    sc->sc_carpdev->if_xname);
1159d220759bSGleb Smirnoff 		carp_master_down_locked(sc);
116008b68b0eSGleb Smirnoff 	}
1161afdbac98SGleb Smirnoff 	CURVNET_RESTORE();
116208b68b0eSGleb Smirnoff 
116308b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
1164d220759bSGleb Smirnoff }
1165d220759bSGleb Smirnoff 
1166d220759bSGleb Smirnoff static void
1167d220759bSGleb Smirnoff carp_master_down_locked(struct carp_softc *sc)
1168d220759bSGleb Smirnoff {
116908b68b0eSGleb Smirnoff 
117008b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
1171d220759bSGleb Smirnoff 
1172a9771948SGleb Smirnoff 	switch (sc->sc_state) {
1173a9771948SGleb Smirnoff 	case BACKUP:
1174a9771948SGleb Smirnoff 		carp_set_state(sc, MASTER);
1175d220759bSGleb Smirnoff 		carp_send_ad_locked(sc);
1176a0ae8f04SBjoern A. Zeeb #ifdef INET
1177a9771948SGleb Smirnoff 		carp_send_arp(sc);
1178a0ae8f04SBjoern A. Zeeb #endif
1179a9771948SGleb Smirnoff #ifdef INET6
1180a9771948SGleb Smirnoff 		carp_send_na(sc);
118108b68b0eSGleb Smirnoff #endif
1182a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
118308b68b0eSGleb Smirnoff 		carp_addroute(sc);
118408b68b0eSGleb Smirnoff 		break;
118508b68b0eSGleb Smirnoff 	case INIT:
118608b68b0eSGleb Smirnoff 	case MASTER:
118708b68b0eSGleb Smirnoff #ifdef INVARIANTS
118808b68b0eSGleb Smirnoff 		panic("carp: VHID %u@%s: master_down event in %s state\n",
118908b68b0eSGleb Smirnoff 		    sc->sc_vhid,
119008b68b0eSGleb Smirnoff 		    sc->sc_carpdev->if_xname,
119108b68b0eSGleb Smirnoff 		    sc->sc_state ? "MASTER" : "INIT");
119208b68b0eSGleb Smirnoff #endif
1193a9771948SGleb Smirnoff 		break;
1194a9771948SGleb Smirnoff 	}
1195a9771948SGleb Smirnoff }
1196a9771948SGleb Smirnoff 
1197a9771948SGleb Smirnoff /*
1198a9771948SGleb Smirnoff  * When in backup state, af indicates whether to reset the master down timer
1199a9771948SGleb Smirnoff  * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1200a9771948SGleb Smirnoff  */
12015c1f0f6dSGleb Smirnoff static void
1202a9771948SGleb Smirnoff carp_setrun(struct carp_softc *sc, sa_family_t af)
1203a9771948SGleb Smirnoff {
1204a9771948SGleb Smirnoff 	struct timeval tv;
1205a9771948SGleb Smirnoff 
120608b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
1207d220759bSGleb Smirnoff 
120808b68b0eSGleb Smirnoff 	if ((sc->sc_carpdev->if_flags & IFF_UP) == 0 ||
120908b68b0eSGleb Smirnoff 	    sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
121008b68b0eSGleb Smirnoff 	    (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0))
1211a9771948SGleb Smirnoff 		return;
1212a9771948SGleb Smirnoff 
1213a9771948SGleb Smirnoff 	switch (sc->sc_state) {
1214a9771948SGleb Smirnoff 	case INIT:
121508b68b0eSGleb Smirnoff 		CARP_LOG("VHID %u@%s: INIT -> BACKUP\n",
121608b68b0eSGleb Smirnoff 		    sc->sc_vhid,
121708b68b0eSGleb Smirnoff 		    sc->sc_carpdev->if_xname);
1218a9771948SGleb Smirnoff 		carp_set_state(sc, BACKUP);
1219a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
1220a9771948SGleb Smirnoff 		break;
1221a9771948SGleb Smirnoff 	case BACKUP:
1222a9771948SGleb Smirnoff 		callout_stop(&sc->sc_ad_tmo);
1223a9771948SGleb Smirnoff 		tv.tv_sec = 3 * sc->sc_advbase;
1224a9771948SGleb Smirnoff 		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1225a9771948SGleb Smirnoff 		switch (af) {
1226a9771948SGleb Smirnoff #ifdef INET
1227a9771948SGleb Smirnoff 		case AF_INET:
1228a9771948SGleb Smirnoff 			callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1229a9771948SGleb Smirnoff 			    carp_master_down, sc);
1230a9771948SGleb Smirnoff 			break;
123108b68b0eSGleb Smirnoff #endif
1232a9771948SGleb Smirnoff #ifdef INET6
1233a9771948SGleb Smirnoff 		case AF_INET6:
1234a9771948SGleb Smirnoff 			callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1235a9771948SGleb Smirnoff 			    carp_master_down, sc);
1236a9771948SGleb Smirnoff 			break;
123708b68b0eSGleb Smirnoff #endif
1238a9771948SGleb Smirnoff 		default:
123908b68b0eSGleb Smirnoff #ifdef INET
1240a9771948SGleb Smirnoff 			if (sc->sc_naddrs)
1241a9771948SGleb Smirnoff 				callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1242a9771948SGleb Smirnoff 				    carp_master_down, sc);
124308b68b0eSGleb Smirnoff #endif
124408b68b0eSGleb Smirnoff #ifdef INET6
1245a9771948SGleb Smirnoff 			if (sc->sc_naddrs6)
1246a9771948SGleb Smirnoff 				callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1247a9771948SGleb Smirnoff 				    carp_master_down, sc);
124808b68b0eSGleb Smirnoff #endif
1249a9771948SGleb Smirnoff 			break;
1250a9771948SGleb Smirnoff 		}
1251a9771948SGleb Smirnoff 		break;
1252a9771948SGleb Smirnoff 	case MASTER:
1253a9771948SGleb Smirnoff 		tv.tv_sec = sc->sc_advbase;
1254a9771948SGleb Smirnoff 		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1255a9771948SGleb Smirnoff 		callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1256a9771948SGleb Smirnoff 		    carp_send_ad, sc);
1257a9771948SGleb Smirnoff 		break;
1258a9771948SGleb Smirnoff 	}
1259a9771948SGleb Smirnoff }
1260a9771948SGleb Smirnoff 
126108b68b0eSGleb Smirnoff /*
126208b68b0eSGleb Smirnoff  * Setup multicast structures.
126308b68b0eSGleb Smirnoff  */
12645c1f0f6dSGleb Smirnoff static int
1265a9a2c40cSGleb Smirnoff carp_multicast_setup(struct carp_if *cif, sa_family_t sa)
1266a9771948SGleb Smirnoff {
1267a9a2c40cSGleb Smirnoff 	struct ifnet *ifp = cif->cif_ifp;
126808b68b0eSGleb Smirnoff 	int error = 0;
126908b68b0eSGleb Smirnoff 
1270a9a2c40cSGleb Smirnoff 	CIF_LOCK_ASSERT(cif);
1271a9a2c40cSGleb Smirnoff 
127208b68b0eSGleb Smirnoff 	switch (sa) {
127308b68b0eSGleb Smirnoff #ifdef INET
127408b68b0eSGleb Smirnoff 	case AF_INET:
127508b68b0eSGleb Smirnoff 	    {
127608b68b0eSGleb Smirnoff 		struct ip_moptions *imo = &cif->cif_imo;
1277a9771948SGleb Smirnoff 		struct in_addr addr;
1278a9771948SGleb Smirnoff 
127908b68b0eSGleb Smirnoff 		if (imo->imo_membership)
1280a9771948SGleb Smirnoff 			return (0);
1281a9771948SGleb Smirnoff 
128208b68b0eSGleb Smirnoff 		imo->imo_membership = (struct in_multi **)malloc(
128308b68b0eSGleb Smirnoff 		    (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_CARP,
1284a9a2c40cSGleb Smirnoff 		    M_NOWAIT);
1285a9a2c40cSGleb Smirnoff 		if (imo->imo_membership == NULL)
1286a9a2c40cSGleb Smirnoff 			return (ENOMEM);
128708b68b0eSGleb Smirnoff 		imo->imo_mfilters = NULL;
128808b68b0eSGleb Smirnoff 		imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
128908b68b0eSGleb Smirnoff 		imo->imo_multicast_vif = -1;
1290a9771948SGleb Smirnoff 
1291a9771948SGleb Smirnoff 		addr.s_addr = htonl(INADDR_CARP_GROUP);
129208b68b0eSGleb Smirnoff 		if ((error = in_joingroup(ifp, &addr, NULL,
129308b68b0eSGleb Smirnoff 		    &imo->imo_membership[0])) != 0) {
129408b68b0eSGleb Smirnoff 			free(imo->imo_membership, M_CARP);
129508b68b0eSGleb Smirnoff 			break;
12962d9cfabaSRobert Watson 		}
1297a9771948SGleb Smirnoff 		imo->imo_num_memberships++;
1298a9771948SGleb Smirnoff 		imo->imo_multicast_ifp = ifp;
1299a9771948SGleb Smirnoff 		imo->imo_multicast_ttl = CARP_DFLTTL;
1300a9771948SGleb Smirnoff 		imo->imo_multicast_loop = 0;
1301a9771948SGleb Smirnoff 		break;
1302a9771948SGleb Smirnoff 	   }
130308b68b0eSGleb Smirnoff #endif
130408b68b0eSGleb Smirnoff #ifdef INET6
130508b68b0eSGleb Smirnoff 	case AF_INET6:
130608b68b0eSGleb Smirnoff 	    {
130708b68b0eSGleb Smirnoff 		struct ip6_moptions *im6o = &cif->cif_im6o;
130808b68b0eSGleb Smirnoff 		struct in6_addr in6;
130933cde130SBruce M Simpson 		struct in6_multi *in6m;
131033cde130SBruce M Simpson 
131108b68b0eSGleb Smirnoff 		if (im6o->im6o_membership)
131208b68b0eSGleb Smirnoff 			return (0);
131308b68b0eSGleb Smirnoff 
131408b68b0eSGleb Smirnoff 		im6o->im6o_membership = (struct in6_multi **)malloc(
131508b68b0eSGleb Smirnoff 		    (sizeof(struct in6_multi *) * IPV6_MIN_MEMBERSHIPS), M_CARP,
1316a9a2c40cSGleb Smirnoff 		    M_ZERO | M_NOWAIT);
1317a9a2c40cSGleb Smirnoff 		if (im6o->im6o_membership == NULL)
1318a9a2c40cSGleb Smirnoff 			return (ENOMEM);
131908b68b0eSGleb Smirnoff 		im6o->im6o_mfilters = NULL;
132008b68b0eSGleb Smirnoff 		im6o->im6o_max_memberships = IPV6_MIN_MEMBERSHIPS;
132108b68b0eSGleb Smirnoff 		im6o->im6o_multicast_hlim = CARP_DFLTTL;
1322a9771948SGleb Smirnoff 		im6o->im6o_multicast_ifp = ifp;
1323a9771948SGleb Smirnoff 
132408b68b0eSGleb Smirnoff 		/* Join IPv6 CARP multicast group. */
1325a1f7e5f8SHajimu UMEMOTO 		bzero(&in6, sizeof(in6));
1326a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr16[0] = htons(0xff02);
1327a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr8[15] = 0x12;
132808b68b0eSGleb Smirnoff 		if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
132908b68b0eSGleb Smirnoff 			free(im6o->im6o_membership, M_CARP);
133008b68b0eSGleb Smirnoff 			break;
133108b68b0eSGleb Smirnoff 		}
133233cde130SBruce M Simpson 		in6m = NULL;
133308b68b0eSGleb Smirnoff 		if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
133408b68b0eSGleb Smirnoff 			free(im6o->im6o_membership, M_CARP);
133508b68b0eSGleb Smirnoff 			break;
133608b68b0eSGleb Smirnoff 		}
133733cde130SBruce M Simpson 		im6o->im6o_membership[0] = in6m;
133833cde130SBruce M Simpson 		im6o->im6o_num_memberships++;
1339a9771948SGleb Smirnoff 
134008b68b0eSGleb Smirnoff 		/* Join solicited multicast address. */
1341a1f7e5f8SHajimu UMEMOTO 		bzero(&in6, sizeof(in6));
1342a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr16[0] = htons(0xff02);
1343a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr32[1] = 0;
1344a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr32[2] = htonl(1);
134508b68b0eSGleb Smirnoff 		in6.s6_addr32[3] = 0;
1346a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr8[12] = 0xff;
134708b68b0eSGleb Smirnoff 		if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
134808b68b0eSGleb Smirnoff 			in6_mc_leave(im6o->im6o_membership[0], NULL);
134908b68b0eSGleb Smirnoff 			free(im6o->im6o_membership, M_CARP);
135008b68b0eSGleb Smirnoff 			break;
135108b68b0eSGleb Smirnoff 		}
135233cde130SBruce M Simpson 		in6m = NULL;
135308b68b0eSGleb Smirnoff 		if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
135408b68b0eSGleb Smirnoff 			in6_mc_leave(im6o->im6o_membership[0], NULL);
135508b68b0eSGleb Smirnoff 			free(im6o->im6o_membership, M_CARP);
135608b68b0eSGleb Smirnoff 			break;
135708b68b0eSGleb Smirnoff 		}
135833cde130SBruce M Simpson 		im6o->im6o_membership[1] = in6m;
135933cde130SBruce M Simpson 		im6o->im6o_num_memberships++;
1360a9771948SGleb Smirnoff 		break;
1361a9771948SGleb Smirnoff 	    }
1362a9771948SGleb Smirnoff #endif
136308b68b0eSGleb Smirnoff 	}
136408b68b0eSGleb Smirnoff 
136508b68b0eSGleb Smirnoff 	return (error);
1366a9771948SGleb Smirnoff }
1367a9771948SGleb Smirnoff 
1368a9771948SGleb Smirnoff /*
136908b68b0eSGleb Smirnoff  * Free multicast structures.
1370a9771948SGleb Smirnoff  */
13715c1f0f6dSGleb Smirnoff static void
1372a9a2c40cSGleb Smirnoff carp_multicast_cleanup(struct carp_if *cif, sa_family_t sa)
1373a9771948SGleb Smirnoff {
137408b68b0eSGleb Smirnoff 
1375a9a2c40cSGleb Smirnoff 	CIF_LOCK_ASSERT(cif);
137608b68b0eSGleb Smirnoff 	switch (sa) {
137708b68b0eSGleb Smirnoff #ifdef INET
137808b68b0eSGleb Smirnoff 	case AF_INET:
1379a9a2c40cSGleb Smirnoff 		if (cif->cif_naddrs == 0) {
138008b68b0eSGleb Smirnoff 			struct ip_moptions *imo = &cif->cif_imo;
138108b68b0eSGleb Smirnoff 
138208b68b0eSGleb Smirnoff 			in_leavegroup(imo->imo_membership[0], NULL);
138308b68b0eSGleb Smirnoff 			KASSERT(imo->imo_mfilters == NULL,
138408b68b0eSGleb Smirnoff 			    ("%s: imo_mfilters != NULL", __func__));
138508b68b0eSGleb Smirnoff 			free(imo->imo_membership, M_CARP);
138608b68b0eSGleb Smirnoff 			imo->imo_membership = NULL;
138708b68b0eSGleb Smirnoff 
138808b68b0eSGleb Smirnoff 		}
138908b68b0eSGleb Smirnoff 		break;
1390a9771948SGleb Smirnoff #endif
139108b68b0eSGleb Smirnoff #ifdef INET6
139208b68b0eSGleb Smirnoff 	case AF_INET6:
1393a9a2c40cSGleb Smirnoff 		if (cif->cif_naddrs6 == 0) {
139408b68b0eSGleb Smirnoff 			struct ip6_moptions *im6o = &cif->cif_im6o;
139508b68b0eSGleb Smirnoff 
139608b68b0eSGleb Smirnoff 			in6_mc_leave(im6o->im6o_membership[0], NULL);
139708b68b0eSGleb Smirnoff 			in6_mc_leave(im6o->im6o_membership[1], NULL);
139808b68b0eSGleb Smirnoff 			KASSERT(im6o->im6o_mfilters == NULL,
139908b68b0eSGleb Smirnoff 			    ("%s: im6o_mfilters != NULL", __func__));
140008b68b0eSGleb Smirnoff 			free(im6o->im6o_membership, M_CARP);
140108b68b0eSGleb Smirnoff 			im6o->im6o_membership = NULL;
140208b68b0eSGleb Smirnoff 		}
140308b68b0eSGleb Smirnoff 		break;
140408b68b0eSGleb Smirnoff #endif
140508b68b0eSGleb Smirnoff 	}
1406a9771948SGleb Smirnoff }
1407a9771948SGleb Smirnoff 
1408a9771948SGleb Smirnoff int
140947e8d432SGleb Smirnoff carp_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa)
1410a9771948SGleb Smirnoff {
1411a9771948SGleb Smirnoff 	struct m_tag *mtag;
1412a9771948SGleb Smirnoff 	struct carp_softc *sc;
1413a9771948SGleb Smirnoff 
1414a9771948SGleb Smirnoff 	if (!sa)
1415a9771948SGleb Smirnoff 		return (0);
1416a9771948SGleb Smirnoff 
1417a9771948SGleb Smirnoff 	switch (sa->sa_family) {
1418a9771948SGleb Smirnoff #ifdef INET
1419a9771948SGleb Smirnoff 	case AF_INET:
1420a9771948SGleb Smirnoff 		break;
142108b68b0eSGleb Smirnoff #endif
1422a9771948SGleb Smirnoff #ifdef INET6
1423a9771948SGleb Smirnoff 	case AF_INET6:
1424a9771948SGleb Smirnoff 		break;
142508b68b0eSGleb Smirnoff #endif
1426a9771948SGleb Smirnoff 	default:
1427a9771948SGleb Smirnoff 		return (0);
1428a9771948SGleb Smirnoff 	}
1429a9771948SGleb Smirnoff 
1430a9771948SGleb Smirnoff 	mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
1431a9771948SGleb Smirnoff 	if (mtag == NULL)
1432a9771948SGleb Smirnoff 		return (0);
1433a9771948SGleb Smirnoff 
1434eaf151c4SGleb Smirnoff 	bcopy(mtag + 1, &sc, sizeof(sc));
1435a9771948SGleb Smirnoff 
143608b68b0eSGleb Smirnoff 	/* Set the source MAC address to the Virtual Router MAC Address. */
1437a9771948SGleb Smirnoff 	switch (ifp->if_type) {
1438630481bbSYaroslav Tykhiy 	case IFT_ETHER:
14399ca1fe0dSGleb Smirnoff 	case IFT_BRIDGE:
1440630481bbSYaroslav Tykhiy 	case IFT_L2VLAN: {
1441a9771948SGleb Smirnoff 			struct ether_header *eh;
1442a9771948SGleb Smirnoff 
1443a9771948SGleb Smirnoff 			eh = mtod(m, struct ether_header *);
1444a9771948SGleb Smirnoff 			eh->ether_shost[0] = 0;
1445a9771948SGleb Smirnoff 			eh->ether_shost[1] = 0;
1446a9771948SGleb Smirnoff 			eh->ether_shost[2] = 0x5e;
1447a9771948SGleb Smirnoff 			eh->ether_shost[3] = 0;
1448a9771948SGleb Smirnoff 			eh->ether_shost[4] = 1;
1449a9771948SGleb Smirnoff 			eh->ether_shost[5] = sc->sc_vhid;
1450a9771948SGleb Smirnoff 		}
1451a9771948SGleb Smirnoff 		break;
1452a9771948SGleb Smirnoff 	case IFT_FDDI: {
1453a9771948SGleb Smirnoff 			struct fddi_header *fh;
1454a9771948SGleb Smirnoff 
1455a9771948SGleb Smirnoff 			fh = mtod(m, struct fddi_header *);
1456a9771948SGleb Smirnoff 			fh->fddi_shost[0] = 0;
1457a9771948SGleb Smirnoff 			fh->fddi_shost[1] = 0;
1458a9771948SGleb Smirnoff 			fh->fddi_shost[2] = 0x5e;
1459a9771948SGleb Smirnoff 			fh->fddi_shost[3] = 0;
1460a9771948SGleb Smirnoff 			fh->fddi_shost[4] = 1;
1461a9771948SGleb Smirnoff 			fh->fddi_shost[5] = sc->sc_vhid;
1462a9771948SGleb Smirnoff 		}
1463a9771948SGleb Smirnoff 		break;
1464a9771948SGleb Smirnoff 	case IFT_ISO88025: {
1465a9771948SGleb Smirnoff  			struct iso88025_header *th;
1466a9771948SGleb Smirnoff  			th = mtod(m, struct iso88025_header *);
1467a9771948SGleb Smirnoff 			th->iso88025_shost[0] = 3;
1468a9771948SGleb Smirnoff 			th->iso88025_shost[1] = 0;
1469a9771948SGleb Smirnoff 			th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1);
1470a9771948SGleb Smirnoff 			th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1);
1471a9771948SGleb Smirnoff 			th->iso88025_shost[4] = 0;
1472a9771948SGleb Smirnoff 			th->iso88025_shost[5] = 0;
1473a9771948SGleb Smirnoff 		}
1474a9771948SGleb Smirnoff 		break;
1475a9771948SGleb Smirnoff 	default:
147608b68b0eSGleb Smirnoff 		printf("%s: carp is not supported for the %d interface type\n",
147708b68b0eSGleb Smirnoff 		    ifp->if_xname, ifp->if_type);
1478a9771948SGleb Smirnoff 		return (EOPNOTSUPP);
1479a9771948SGleb Smirnoff 	}
1480a9771948SGleb Smirnoff 
1481a9771948SGleb Smirnoff 	return (0);
1482a9771948SGleb Smirnoff }
1483a9771948SGleb Smirnoff 
148408b68b0eSGleb Smirnoff static struct carp_softc*
148508b68b0eSGleb Smirnoff carp_alloc(struct ifnet *ifp)
1486a9771948SGleb Smirnoff {
148708b68b0eSGleb Smirnoff 	struct carp_softc *sc;
148808b68b0eSGleb Smirnoff 	struct carp_if *cif;
1489d220759bSGleb Smirnoff 
14900cc726f2SGleb Smirnoff 	if ((cif = ifp->if_carp) == NULL)
149108b68b0eSGleb Smirnoff 		cif = carp_alloc_if(ifp);
1492a9771948SGleb Smirnoff 
149308b68b0eSGleb Smirnoff 	sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
149408b68b0eSGleb Smirnoff 
149508b68b0eSGleb Smirnoff 	sc->sc_advbase = CARP_DFLTINTV;
149608b68b0eSGleb Smirnoff 	sc->sc_vhid = -1;	/* required setting */
149708b68b0eSGleb Smirnoff 	sc->sc_init_counter = 1;
149808b68b0eSGleb Smirnoff 	sc->sc_state = INIT;
149908b68b0eSGleb Smirnoff 
150008b68b0eSGleb Smirnoff 	sc->sc_ifasiz = sizeof(struct ifaddr *);
150108b68b0eSGleb Smirnoff 	sc->sc_ifas = malloc(sc->sc_ifasiz, M_CARP, M_WAITOK|M_ZERO);
150208b68b0eSGleb Smirnoff 	sc->sc_carpdev = ifp;
150308b68b0eSGleb Smirnoff 
150408b68b0eSGleb Smirnoff 	CARP_LOCK_INIT(sc);
150508b68b0eSGleb Smirnoff #ifdef INET
150608b68b0eSGleb Smirnoff 	callout_init_mtx(&sc->sc_md_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
150708b68b0eSGleb Smirnoff #endif
150808b68b0eSGleb Smirnoff #ifdef INET6
150908b68b0eSGleb Smirnoff 	callout_init_mtx(&sc->sc_md6_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
151008b68b0eSGleb Smirnoff #endif
151108b68b0eSGleb Smirnoff 	callout_init_mtx(&sc->sc_ad_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
151208b68b0eSGleb Smirnoff 
151308b68b0eSGleb Smirnoff 	CIF_LOCK(cif);
151408b68b0eSGleb Smirnoff 	TAILQ_INSERT_TAIL(&cif->cif_vrs, sc, sc_list);
151508b68b0eSGleb Smirnoff 	CIF_UNLOCK(cif);
151608b68b0eSGleb Smirnoff 
151708b68b0eSGleb Smirnoff 	mtx_lock(&carp_mtx);
151808b68b0eSGleb Smirnoff 	LIST_INSERT_HEAD(&carp_list, sc, sc_next);
151908b68b0eSGleb Smirnoff 	mtx_unlock(&carp_mtx);
152008b68b0eSGleb Smirnoff 
152108b68b0eSGleb Smirnoff 	return (sc);
152208b68b0eSGleb Smirnoff }
152308b68b0eSGleb Smirnoff 
152408b68b0eSGleb Smirnoff static int
152508b68b0eSGleb Smirnoff carp_grow_ifas(struct carp_softc *sc)
152608b68b0eSGleb Smirnoff {
152708b68b0eSGleb Smirnoff 	struct ifaddr **new;
152808b68b0eSGleb Smirnoff 
152908b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
153008b68b0eSGleb Smirnoff 
153108b68b0eSGleb Smirnoff 	new = malloc(sc->sc_ifasiz * 2, M_CARP, M_NOWAIT|M_ZERO);
153208b68b0eSGleb Smirnoff 	if (new == NULL)
153308b68b0eSGleb Smirnoff 		return (ENOMEM);
153408b68b0eSGleb Smirnoff 	bcopy(sc->sc_ifas, new, sc->sc_ifasiz);
153508b68b0eSGleb Smirnoff 	free(sc->sc_ifas, M_CARP);
153608b68b0eSGleb Smirnoff 	sc->sc_ifas = new;
153708b68b0eSGleb Smirnoff 	sc->sc_ifasiz *= 2;
153808b68b0eSGleb Smirnoff 
153908b68b0eSGleb Smirnoff 	return (0);
154008b68b0eSGleb Smirnoff }
154108b68b0eSGleb Smirnoff 
154208b68b0eSGleb Smirnoff static void
154308b68b0eSGleb Smirnoff carp_destroy(struct carp_softc *sc)
154408b68b0eSGleb Smirnoff {
154508b68b0eSGleb Smirnoff 	struct ifnet *ifp = sc->sc_carpdev;
154608b68b0eSGleb Smirnoff 	struct carp_if *cif = ifp->if_carp;
154708b68b0eSGleb Smirnoff 
1548a9a2c40cSGleb Smirnoff 	CIF_LOCK_ASSERT(cif);
1549a9a2c40cSGleb Smirnoff 
155008b68b0eSGleb Smirnoff 	TAILQ_REMOVE(&cif->cif_vrs, sc, sc_list);
155108b68b0eSGleb Smirnoff 
155208b68b0eSGleb Smirnoff 	mtx_lock(&carp_mtx);
155308b68b0eSGleb Smirnoff 	LIST_REMOVE(sc, sc_next);
155408b68b0eSGleb Smirnoff 	mtx_unlock(&carp_mtx);
155508b68b0eSGleb Smirnoff 
155608b68b0eSGleb Smirnoff 	CARP_LOCK(sc);
1557f08535f8SGleb Smirnoff 	if (sc->sc_suppress)
1558c5c392e7SMikolaj Golub 		carp_demote_adj(-V_carp_ifdown_adj, "vhid removed");
155908b68b0eSGleb Smirnoff 	callout_drain(&sc->sc_ad_tmo);
156008b68b0eSGleb Smirnoff #ifdef INET
156108b68b0eSGleb Smirnoff 	callout_drain(&sc->sc_md_tmo);
156208b68b0eSGleb Smirnoff #endif
156308b68b0eSGleb Smirnoff #ifdef INET6
156408b68b0eSGleb Smirnoff 	callout_drain(&sc->sc_md6_tmo);
156508b68b0eSGleb Smirnoff #endif
156608b68b0eSGleb Smirnoff 	CARP_LOCK_DESTROY(sc);
156708b68b0eSGleb Smirnoff 
156808b68b0eSGleb Smirnoff 	free(sc->sc_ifas, M_CARP);
156908b68b0eSGleb Smirnoff 	free(sc, M_CARP);
157008b68b0eSGleb Smirnoff }
157108b68b0eSGleb Smirnoff 
157208b68b0eSGleb Smirnoff static struct carp_if*
157308b68b0eSGleb Smirnoff carp_alloc_if(struct ifnet *ifp)
1574a9771948SGleb Smirnoff {
157554bfbd51SWill Andrews 	struct carp_if *cif;
15760cc726f2SGleb Smirnoff 	int error;
1577d220759bSGleb Smirnoff 
157808b68b0eSGleb Smirnoff 	cif = malloc(sizeof(*cif), M_CARP, M_WAITOK|M_ZERO);
157908b68b0eSGleb Smirnoff 
15800cc726f2SGleb Smirnoff 	if ((error = ifpromisc(ifp, 1)) != 0)
15810cc726f2SGleb Smirnoff 		printf("%s: ifpromisc(%s) failed: %d\n",
15820cc726f2SGleb Smirnoff 		    __func__, ifp->if_xname, error);
15830cc726f2SGleb Smirnoff 	else
15840cc726f2SGleb Smirnoff 		cif->cif_flags |= CIF_PROMISC;
158508b68b0eSGleb Smirnoff 
158608b68b0eSGleb Smirnoff 	CIF_LOCK_INIT(cif);
158708b68b0eSGleb Smirnoff 	cif->cif_ifp = ifp;
158808b68b0eSGleb Smirnoff 	TAILQ_INIT(&cif->cif_vrs);
158908b68b0eSGleb Smirnoff 
1590137f91e8SJohn Baldwin 	IF_ADDR_WLOCK(ifp);
159108b68b0eSGleb Smirnoff 	ifp->if_carp = cif;
159208b68b0eSGleb Smirnoff 	if_ref(ifp);
1593137f91e8SJohn Baldwin 	IF_ADDR_WUNLOCK(ifp);
159408b68b0eSGleb Smirnoff 
159508b68b0eSGleb Smirnoff 	return (cif);
1596d220759bSGleb Smirnoff }
1597d220759bSGleb Smirnoff 
1598d220759bSGleb Smirnoff static void
159908b68b0eSGleb Smirnoff carp_free_if(struct carp_if *cif)
160008b68b0eSGleb Smirnoff {
160108b68b0eSGleb Smirnoff 	struct ifnet *ifp = cif->cif_ifp;
160208b68b0eSGleb Smirnoff 
160308b68b0eSGleb Smirnoff 	CIF_LOCK_ASSERT(cif);
160408b68b0eSGleb Smirnoff 	KASSERT(TAILQ_EMPTY(&cif->cif_vrs), ("%s: softc list not empty",
160508b68b0eSGleb Smirnoff 	    __func__));
160608b68b0eSGleb Smirnoff 
1607137f91e8SJohn Baldwin 	IF_ADDR_WLOCK(ifp);
160808b68b0eSGleb Smirnoff 	ifp->if_carp = NULL;
1609137f91e8SJohn Baldwin 	IF_ADDR_WUNLOCK(ifp);
161008b68b0eSGleb Smirnoff 
161108b68b0eSGleb Smirnoff 	CIF_LOCK_DESTROY(cif);
161208b68b0eSGleb Smirnoff 
16130cc726f2SGleb Smirnoff 	if (cif->cif_flags & CIF_PROMISC)
161408b68b0eSGleb Smirnoff 		ifpromisc(ifp, 0);
16151f6addd9SMikolaj Golub 	if_rele(ifp);
161608b68b0eSGleb Smirnoff 
161708b68b0eSGleb Smirnoff 	free(cif, M_CARP);
161808b68b0eSGleb Smirnoff }
161908b68b0eSGleb Smirnoff 
162008b68b0eSGleb Smirnoff static void
162108b68b0eSGleb Smirnoff carp_carprcp(struct carpreq *carpr, struct carp_softc *sc, int priv)
162208b68b0eSGleb Smirnoff {
162308b68b0eSGleb Smirnoff 
162408b68b0eSGleb Smirnoff 	CARP_LOCK(sc);
162508b68b0eSGleb Smirnoff 	carpr->carpr_state = sc->sc_state;
162608b68b0eSGleb Smirnoff 	carpr->carpr_vhid = sc->sc_vhid;
162708b68b0eSGleb Smirnoff 	carpr->carpr_advbase = sc->sc_advbase;
162808b68b0eSGleb Smirnoff 	carpr->carpr_advskew = sc->sc_advskew;
162908b68b0eSGleb Smirnoff 	if (priv)
163008b68b0eSGleb Smirnoff 		bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key));
163108b68b0eSGleb Smirnoff 	else
163208b68b0eSGleb Smirnoff 		bzero(carpr->carpr_key, sizeof(carpr->carpr_key));
163308b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
163408b68b0eSGleb Smirnoff }
163508b68b0eSGleb Smirnoff 
163608b68b0eSGleb Smirnoff int
163708b68b0eSGleb Smirnoff carp_ioctl(struct ifreq *ifr, u_long cmd, struct thread *td)
163808b68b0eSGleb Smirnoff {
163908b68b0eSGleb Smirnoff 	struct carpreq carpr;
164008b68b0eSGleb Smirnoff 	struct ifnet *ifp;
164108b68b0eSGleb Smirnoff 	struct carp_softc *sc = NULL;
164208b68b0eSGleb Smirnoff 	int error = 0, locked = 0;
164308b68b0eSGleb Smirnoff 
164408b68b0eSGleb Smirnoff 	if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
164508b68b0eSGleb Smirnoff 		return (error);
164608b68b0eSGleb Smirnoff 
164708b68b0eSGleb Smirnoff 	ifp = ifunit_ref(ifr->ifr_name);
164808b68b0eSGleb Smirnoff 	if (ifp == NULL)
164908b68b0eSGleb Smirnoff 		return (ENXIO);
165008b68b0eSGleb Smirnoff 
165108b68b0eSGleb Smirnoff 	switch (ifp->if_type) {
165208b68b0eSGleb Smirnoff 	case IFT_ETHER:
165308b68b0eSGleb Smirnoff 	case IFT_L2VLAN:
16549ca1fe0dSGleb Smirnoff 	case IFT_BRIDGE:
165508b68b0eSGleb Smirnoff 	case IFT_FDDI:
165608b68b0eSGleb Smirnoff 	case IFT_ISO88025:
165708b68b0eSGleb Smirnoff 		break;
165808b68b0eSGleb Smirnoff 	default:
165908b68b0eSGleb Smirnoff 		error = EOPNOTSUPP;
166008b68b0eSGleb Smirnoff 		goto out;
166108b68b0eSGleb Smirnoff 	}
166208b68b0eSGleb Smirnoff 
166308b68b0eSGleb Smirnoff 	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
166408b68b0eSGleb Smirnoff 		error = EADDRNOTAVAIL;
166508b68b0eSGleb Smirnoff 		goto out;
166608b68b0eSGleb Smirnoff 	}
166708b68b0eSGleb Smirnoff 
166808b68b0eSGleb Smirnoff 	switch (cmd) {
166908b68b0eSGleb Smirnoff 	case SIOCSVH:
167008b68b0eSGleb Smirnoff 		if ((error = priv_check(td, PRIV_NETINET_CARP)))
167108b68b0eSGleb Smirnoff 			break;
167208b68b0eSGleb Smirnoff 		if (carpr.carpr_vhid <= 0 || carpr.carpr_vhid > CARP_MAXVHID ||
167308b68b0eSGleb Smirnoff 		    carpr.carpr_advbase < 0 || carpr.carpr_advskew < 0) {
167408b68b0eSGleb Smirnoff 			error = EINVAL;
167508b68b0eSGleb Smirnoff 			break;
167608b68b0eSGleb Smirnoff 		}
167708b68b0eSGleb Smirnoff 
167808b68b0eSGleb Smirnoff 		if (ifp->if_carp) {
167908b68b0eSGleb Smirnoff 			CIF_LOCK(ifp->if_carp);
168008b68b0eSGleb Smirnoff 			IFNET_FOREACH_CARP(ifp, sc)
168108b68b0eSGleb Smirnoff 				if (sc->sc_vhid == carpr.carpr_vhid)
168208b68b0eSGleb Smirnoff 					break;
168308b68b0eSGleb Smirnoff 			CIF_UNLOCK(ifp->if_carp);
168408b68b0eSGleb Smirnoff 		}
168508b68b0eSGleb Smirnoff 		if (sc == NULL) {
168608b68b0eSGleb Smirnoff 			sc = carp_alloc(ifp);
168708b68b0eSGleb Smirnoff 			CARP_LOCK(sc);
168808b68b0eSGleb Smirnoff 			sc->sc_vhid = carpr.carpr_vhid;
168908b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[0] = 0;
169008b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[1] = 0;
169108b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[2] = 0x5e;
169208b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[3] = 0;
169308b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[4] = 1;
169408b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[5] = sc->sc_vhid;
169508b68b0eSGleb Smirnoff 		} else
169608b68b0eSGleb Smirnoff 			CARP_LOCK(sc);
169708b68b0eSGleb Smirnoff 		locked = 1;
169808b68b0eSGleb Smirnoff 		if (carpr.carpr_advbase > 0) {
169908b68b0eSGleb Smirnoff 			if (carpr.carpr_advbase > 255 ||
170008b68b0eSGleb Smirnoff 			    carpr.carpr_advbase < CARP_DFLTINTV) {
170108b68b0eSGleb Smirnoff 				error = EINVAL;
170208b68b0eSGleb Smirnoff 				break;
170308b68b0eSGleb Smirnoff 			}
170408b68b0eSGleb Smirnoff 			sc->sc_advbase = carpr.carpr_advbase;
170508b68b0eSGleb Smirnoff 		}
170608b68b0eSGleb Smirnoff 		if (carpr.carpr_advskew > 0) {
170708b68b0eSGleb Smirnoff 			if (carpr.carpr_advskew >= 255) {
170808b68b0eSGleb Smirnoff 				error = EINVAL;
170908b68b0eSGleb Smirnoff 				break;
171008b68b0eSGleb Smirnoff 			}
171108b68b0eSGleb Smirnoff 			sc->sc_advskew = carpr.carpr_advskew;
171208b68b0eSGleb Smirnoff 		}
171308b68b0eSGleb Smirnoff 		if (carpr.carpr_key[0] != '\0') {
171408b68b0eSGleb Smirnoff 			bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
171508b68b0eSGleb Smirnoff 			carp_hmac_prepare(sc);
171608b68b0eSGleb Smirnoff 		}
171708b68b0eSGleb Smirnoff 		if (sc->sc_state != INIT &&
171808b68b0eSGleb Smirnoff 		    carpr.carpr_state != sc->sc_state) {
171908b68b0eSGleb Smirnoff 			switch (carpr.carpr_state) {
172008b68b0eSGleb Smirnoff 			case BACKUP:
172108b68b0eSGleb Smirnoff 				callout_stop(&sc->sc_ad_tmo);
172208b68b0eSGleb Smirnoff 				carp_set_state(sc, BACKUP);
172308b68b0eSGleb Smirnoff 				carp_setrun(sc, 0);
172408b68b0eSGleb Smirnoff 				carp_delroute(sc);
172508b68b0eSGleb Smirnoff 				break;
172608b68b0eSGleb Smirnoff 			case MASTER:
172708b68b0eSGleb Smirnoff 				carp_master_down_locked(sc);
172808b68b0eSGleb Smirnoff 				break;
172908b68b0eSGleb Smirnoff 			default:
173008b68b0eSGleb Smirnoff 				break;
173108b68b0eSGleb Smirnoff 			}
173208b68b0eSGleb Smirnoff 		}
173308b68b0eSGleb Smirnoff 		break;
173408b68b0eSGleb Smirnoff 
173508b68b0eSGleb Smirnoff 	case SIOCGVH:
173608b68b0eSGleb Smirnoff 	    {
173708b68b0eSGleb Smirnoff 		int priveleged;
173808b68b0eSGleb Smirnoff 
173908b68b0eSGleb Smirnoff 		if (carpr.carpr_vhid < 0 || carpr.carpr_vhid > CARP_MAXVHID) {
174008b68b0eSGleb Smirnoff 			error = EINVAL;
174108b68b0eSGleb Smirnoff 			break;
174208b68b0eSGleb Smirnoff 		}
174308b68b0eSGleb Smirnoff 		if (carpr.carpr_count < 1) {
174408b68b0eSGleb Smirnoff 			error = EMSGSIZE;
174508b68b0eSGleb Smirnoff 			break;
174608b68b0eSGleb Smirnoff 		}
174708b68b0eSGleb Smirnoff 		if (ifp->if_carp == NULL) {
174808b68b0eSGleb Smirnoff 			error = ENOENT;
174908b68b0eSGleb Smirnoff 			break;
175008b68b0eSGleb Smirnoff 		}
175108b68b0eSGleb Smirnoff 
175208b68b0eSGleb Smirnoff 		priveleged = (priv_check(td, PRIV_NETINET_CARP) == 0);
175308b68b0eSGleb Smirnoff 		if (carpr.carpr_vhid != 0) {
175408b68b0eSGleb Smirnoff 			CIF_LOCK(ifp->if_carp);
175508b68b0eSGleb Smirnoff 			IFNET_FOREACH_CARP(ifp, sc)
175608b68b0eSGleb Smirnoff 				if (sc->sc_vhid == carpr.carpr_vhid)
175708b68b0eSGleb Smirnoff 					break;
175808b68b0eSGleb Smirnoff 			CIF_UNLOCK(ifp->if_carp);
175908b68b0eSGleb Smirnoff 			if (sc == NULL) {
176008b68b0eSGleb Smirnoff 				error = ENOENT;
176108b68b0eSGleb Smirnoff 				break;
176208b68b0eSGleb Smirnoff 			}
176308b68b0eSGleb Smirnoff 			carp_carprcp(&carpr, sc, priveleged);
176408b68b0eSGleb Smirnoff 			error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
176508b68b0eSGleb Smirnoff 		} else  {
176608b68b0eSGleb Smirnoff 			int i, count;
176708b68b0eSGleb Smirnoff 
176808b68b0eSGleb Smirnoff 			count = 0;
176908b68b0eSGleb Smirnoff 			CIF_LOCK(ifp->if_carp);
177008b68b0eSGleb Smirnoff 			IFNET_FOREACH_CARP(ifp, sc)
177108b68b0eSGleb Smirnoff 				count++;
177208b68b0eSGleb Smirnoff 
177308b68b0eSGleb Smirnoff 			if (count > carpr.carpr_count) {
177408b68b0eSGleb Smirnoff 				CIF_UNLOCK(ifp->if_carp);
177508b68b0eSGleb Smirnoff 				error = EMSGSIZE;
177608b68b0eSGleb Smirnoff 				break;
177708b68b0eSGleb Smirnoff 			}
177808b68b0eSGleb Smirnoff 
177908b68b0eSGleb Smirnoff 			i = 0;
178008b68b0eSGleb Smirnoff 			IFNET_FOREACH_CARP(ifp, sc) {
178108b68b0eSGleb Smirnoff 				carp_carprcp(&carpr, sc, priveleged);
178208b68b0eSGleb Smirnoff 				carpr.carpr_count = count;
178308b68b0eSGleb Smirnoff 				error = copyout(&carpr, ifr->ifr_data +
178408b68b0eSGleb Smirnoff 				    (i * sizeof(carpr)), sizeof(carpr));
178508b68b0eSGleb Smirnoff 				if (error) {
178608b68b0eSGleb Smirnoff 					CIF_UNLOCK(ifp->if_carp);
178708b68b0eSGleb Smirnoff 					break;
178808b68b0eSGleb Smirnoff 				}
178908b68b0eSGleb Smirnoff 				i++;
179008b68b0eSGleb Smirnoff 			}
179108b68b0eSGleb Smirnoff 			CIF_UNLOCK(ifp->if_carp);
179208b68b0eSGleb Smirnoff 		}
179308b68b0eSGleb Smirnoff 		break;
179408b68b0eSGleb Smirnoff 	    }
179508b68b0eSGleb Smirnoff 	default:
179608b68b0eSGleb Smirnoff 		error = EINVAL;
179708b68b0eSGleb Smirnoff 	}
179808b68b0eSGleb Smirnoff 
179908b68b0eSGleb Smirnoff out:
180008b68b0eSGleb Smirnoff 	if (locked)
180108b68b0eSGleb Smirnoff 		CARP_UNLOCK(sc);
180208b68b0eSGleb Smirnoff 	if_rele(ifp);
180308b68b0eSGleb Smirnoff 
180408b68b0eSGleb Smirnoff 	return (error);
180508b68b0eSGleb Smirnoff }
180608b68b0eSGleb Smirnoff 
180708b68b0eSGleb Smirnoff static int
180808b68b0eSGleb Smirnoff carp_get_vhid(struct ifaddr *ifa)
180908b68b0eSGleb Smirnoff {
181008b68b0eSGleb Smirnoff 
181108b68b0eSGleb Smirnoff 	if (ifa == NULL || ifa->ifa_carp == NULL)
181208b68b0eSGleb Smirnoff 		return (0);
181308b68b0eSGleb Smirnoff 
181408b68b0eSGleb Smirnoff 	return (ifa->ifa_carp->sc_vhid);
181508b68b0eSGleb Smirnoff }
181608b68b0eSGleb Smirnoff 
181708b68b0eSGleb Smirnoff int
181808b68b0eSGleb Smirnoff carp_attach(struct ifaddr *ifa, int vhid)
181908b68b0eSGleb Smirnoff {
182008b68b0eSGleb Smirnoff 	struct ifnet *ifp = ifa->ifa_ifp;
1821a9a2c40cSGleb Smirnoff 	struct carp_if *cif = ifp->if_carp;
182208b68b0eSGleb Smirnoff 	struct carp_softc *sc;
182308b68b0eSGleb Smirnoff 	int index, error;
182408b68b0eSGleb Smirnoff 
182508b68b0eSGleb Smirnoff 	if (ifp->if_carp == NULL)
182608b68b0eSGleb Smirnoff 		return (ENOPROTOOPT);
182708b68b0eSGleb Smirnoff 
182808b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
182908b68b0eSGleb Smirnoff #ifdef INET
183008b68b0eSGleb Smirnoff 	case AF_INET:
183108b68b0eSGleb Smirnoff #endif
183208b68b0eSGleb Smirnoff #ifdef INET6
183308b68b0eSGleb Smirnoff 	case AF_INET6:
183408b68b0eSGleb Smirnoff #endif
183508b68b0eSGleb Smirnoff 		break;
183608b68b0eSGleb Smirnoff 	default:
183708b68b0eSGleb Smirnoff 		return (EPROTOTYPE);
183808b68b0eSGleb Smirnoff 	}
183908b68b0eSGleb Smirnoff 
1840a9a2c40cSGleb Smirnoff 	CIF_LOCK(cif);
184108b68b0eSGleb Smirnoff 	IFNET_FOREACH_CARP(ifp, sc)
184208b68b0eSGleb Smirnoff 		if (sc->sc_vhid == vhid)
184308b68b0eSGleb Smirnoff 			break;
1844a9a2c40cSGleb Smirnoff 	if (sc == NULL) {
1845a9a2c40cSGleb Smirnoff 		CIF_UNLOCK(cif);
184608b68b0eSGleb Smirnoff 		return (ENOENT);
1847a9a2c40cSGleb Smirnoff 	}
184808b68b0eSGleb Smirnoff 
184908b68b0eSGleb Smirnoff 	if (ifa->ifa_carp) {
185008b68b0eSGleb Smirnoff 		if (ifa->ifa_carp->sc_vhid != vhid)
1851a9a2c40cSGleb Smirnoff 			carp_detach_locked(ifa);
1852a9a2c40cSGleb Smirnoff 		else {
1853a9a2c40cSGleb Smirnoff 			CIF_UNLOCK(cif);
185408b68b0eSGleb Smirnoff 			return (0);
185508b68b0eSGleb Smirnoff 		}
1856a9a2c40cSGleb Smirnoff 	}
185708b68b0eSGleb Smirnoff 
1858a9a2c40cSGleb Smirnoff 	error = carp_multicast_setup(cif, ifa->ifa_addr->sa_family);
1859a9a2c40cSGleb Smirnoff 	if (error) {
1860a9a2c40cSGleb Smirnoff 		CIF_FREE(cif);
186108b68b0eSGleb Smirnoff 		return (error);
1862a9a2c40cSGleb Smirnoff 	}
186308b68b0eSGleb Smirnoff 
186408b68b0eSGleb Smirnoff 	CARP_LOCK(sc);
186508b68b0eSGleb Smirnoff 	index = sc->sc_naddrs + sc->sc_naddrs6 + 1;
186608b68b0eSGleb Smirnoff 	if (index > sc->sc_ifasiz / sizeof(struct ifaddr *))
186708b68b0eSGleb Smirnoff 		if ((error = carp_grow_ifas(sc)) != 0) {
1868a9a2c40cSGleb Smirnoff 			carp_multicast_cleanup(cif,
186908b68b0eSGleb Smirnoff 			    ifa->ifa_addr->sa_family);
187008b68b0eSGleb Smirnoff 			CARP_UNLOCK(sc);
1871a9a2c40cSGleb Smirnoff 			CIF_FREE(cif);
187208b68b0eSGleb Smirnoff 			return (error);
187308b68b0eSGleb Smirnoff 		}
187408b68b0eSGleb Smirnoff 
187508b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
187608b68b0eSGleb Smirnoff #ifdef INET
187708b68b0eSGleb Smirnoff 	case AF_INET:
1878a9a2c40cSGleb Smirnoff 		cif->cif_naddrs++;
187908b68b0eSGleb Smirnoff 		sc->sc_naddrs++;
188008b68b0eSGleb Smirnoff 		break;
188108b68b0eSGleb Smirnoff #endif
188208b68b0eSGleb Smirnoff #ifdef INET6
188308b68b0eSGleb Smirnoff 	case AF_INET6:
1884a9a2c40cSGleb Smirnoff 		cif->cif_naddrs6++;
188508b68b0eSGleb Smirnoff 		sc->sc_naddrs6++;
188608b68b0eSGleb Smirnoff 		break;
188708b68b0eSGleb Smirnoff #endif
188808b68b0eSGleb Smirnoff 	}
188908b68b0eSGleb Smirnoff 
189008b68b0eSGleb Smirnoff 	ifa_ref(ifa);
189108b68b0eSGleb Smirnoff 	sc->sc_ifas[index - 1] = ifa;
189208b68b0eSGleb Smirnoff 	ifa->ifa_carp = sc;
189308b68b0eSGleb Smirnoff 
189408b68b0eSGleb Smirnoff 	carp_hmac_prepare(sc);
189508b68b0eSGleb Smirnoff 	carp_sc_state(sc);
189608b68b0eSGleb Smirnoff 
189708b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
1898a9a2c40cSGleb Smirnoff 	CIF_UNLOCK(cif);
189908b68b0eSGleb Smirnoff 
190008b68b0eSGleb Smirnoff 	return (0);
190108b68b0eSGleb Smirnoff }
190208b68b0eSGleb Smirnoff 
190308b68b0eSGleb Smirnoff void
190408b68b0eSGleb Smirnoff carp_detach(struct ifaddr *ifa)
190508b68b0eSGleb Smirnoff {
1906a9a2c40cSGleb Smirnoff 	struct ifnet *ifp = ifa->ifa_ifp;
1907a9a2c40cSGleb Smirnoff 	struct carp_if *cif = ifp->if_carp;
1908a9a2c40cSGleb Smirnoff 
1909a9a2c40cSGleb Smirnoff 	CIF_LOCK(cif);
1910a9a2c40cSGleb Smirnoff 	carp_detach_locked(ifa);
1911a9a2c40cSGleb Smirnoff 	CIF_FREE(cif);
1912a9a2c40cSGleb Smirnoff }
1913a9a2c40cSGleb Smirnoff 
1914a9a2c40cSGleb Smirnoff static void
1915a9a2c40cSGleb Smirnoff carp_detach_locked(struct ifaddr *ifa)
1916a9a2c40cSGleb Smirnoff {
1917a9a2c40cSGleb Smirnoff 	struct ifnet *ifp = ifa->ifa_ifp;
1918a9a2c40cSGleb Smirnoff 	struct carp_if *cif = ifp->if_carp;
191908b68b0eSGleb Smirnoff 	struct carp_softc *sc = ifa->ifa_carp;
192008b68b0eSGleb Smirnoff 	int i, index;
192108b68b0eSGleb Smirnoff 
192208b68b0eSGleb Smirnoff 	KASSERT(sc != NULL, ("%s: %p not attached", __func__, ifa));
192308b68b0eSGleb Smirnoff 
1924a9a2c40cSGleb Smirnoff 	CIF_LOCK_ASSERT(cif);
192508b68b0eSGleb Smirnoff 	CARP_LOCK(sc);
192608b68b0eSGleb Smirnoff 
192708b68b0eSGleb Smirnoff 	/* Shift array. */
192808b68b0eSGleb Smirnoff 	index = sc->sc_naddrs + sc->sc_naddrs6;
192908b68b0eSGleb Smirnoff 	for (i = 0; i < index; i++)
193008b68b0eSGleb Smirnoff 		if (sc->sc_ifas[i] == ifa)
193108b68b0eSGleb Smirnoff 			break;
193208b68b0eSGleb Smirnoff 	KASSERT(i < index, ("%s: %p no backref", __func__, ifa));
193308b68b0eSGleb Smirnoff 	for (; i < index - 1; i++)
193408b68b0eSGleb Smirnoff 		sc->sc_ifas[i] = sc->sc_ifas[i+1];
193508b68b0eSGleb Smirnoff 	sc->sc_ifas[index - 1] = NULL;
193608b68b0eSGleb Smirnoff 
193708b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
193808b68b0eSGleb Smirnoff #ifdef INET
193908b68b0eSGleb Smirnoff 	case AF_INET:
1940a9a2c40cSGleb Smirnoff 		cif->cif_naddrs--;
194108b68b0eSGleb Smirnoff 		sc->sc_naddrs--;
194208b68b0eSGleb Smirnoff 		break;
194308b68b0eSGleb Smirnoff #endif
194408b68b0eSGleb Smirnoff #ifdef INET6
194508b68b0eSGleb Smirnoff 	case AF_INET6:
1946a9a2c40cSGleb Smirnoff 		cif->cif_naddrs6--;
194708b68b0eSGleb Smirnoff 		sc->sc_naddrs6--;
194808b68b0eSGleb Smirnoff 		break;
194908b68b0eSGleb Smirnoff #endif
195008b68b0eSGleb Smirnoff 	}
195108b68b0eSGleb Smirnoff 
19522512b096SGleb Smirnoff 	carp_ifa_delroute(ifa);
1953a9a2c40cSGleb Smirnoff 	carp_multicast_cleanup(cif, ifa->ifa_addr->sa_family);
195408b68b0eSGleb Smirnoff 
195508b68b0eSGleb Smirnoff 	ifa->ifa_carp = NULL;
195608b68b0eSGleb Smirnoff 	ifa_free(ifa);
195708b68b0eSGleb Smirnoff 
195808b68b0eSGleb Smirnoff 	carp_hmac_prepare(sc);
195908b68b0eSGleb Smirnoff 	carp_sc_state(sc);
196008b68b0eSGleb Smirnoff 
196108b68b0eSGleb Smirnoff 	if (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) {
196208b68b0eSGleb Smirnoff 		CARP_UNLOCK(sc);
196308b68b0eSGleb Smirnoff 		carp_destroy(sc);
196408b68b0eSGleb Smirnoff 	} else
196508b68b0eSGleb Smirnoff 		CARP_UNLOCK(sc);
196608b68b0eSGleb Smirnoff }
196708b68b0eSGleb Smirnoff 
196808b68b0eSGleb Smirnoff static void
196908b68b0eSGleb Smirnoff carp_set_state(struct carp_softc *sc, int state)
197008b68b0eSGleb Smirnoff {
197108b68b0eSGleb Smirnoff 
197208b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
197308b68b0eSGleb Smirnoff 
197408b68b0eSGleb Smirnoff 	if (sc->sc_state != state) {
197508b68b0eSGleb Smirnoff 		const char *carp_states[] = { CARP_STATES };
197608b68b0eSGleb Smirnoff 		char subsys[IFNAMSIZ+5];
197708b68b0eSGleb Smirnoff 
197808b68b0eSGleb Smirnoff 		sc->sc_state = state;
197908b68b0eSGleb Smirnoff 
198008b68b0eSGleb Smirnoff 		snprintf(subsys, IFNAMSIZ+5, "%u@%s", sc->sc_vhid,
198108b68b0eSGleb Smirnoff 		    sc->sc_carpdev->if_xname);
198208b68b0eSGleb Smirnoff 		devctl_notify("CARP", subsys, carp_states[state], NULL);
198308b68b0eSGleb Smirnoff 	}
198408b68b0eSGleb Smirnoff }
198508b68b0eSGleb Smirnoff 
198608b68b0eSGleb Smirnoff static void
198708b68b0eSGleb Smirnoff carp_linkstate(struct ifnet *ifp)
1988d220759bSGleb Smirnoff {
1989d220759bSGleb Smirnoff 	struct carp_softc *sc;
1990d220759bSGleb Smirnoff 
199108b68b0eSGleb Smirnoff 	CIF_LOCK(ifp->if_carp);
199208b68b0eSGleb Smirnoff 	IFNET_FOREACH_CARP(ifp, sc) {
199308b68b0eSGleb Smirnoff 		CARP_LOCK(sc);
199408b68b0eSGleb Smirnoff 		carp_sc_state(sc);
199508b68b0eSGleb Smirnoff 		CARP_UNLOCK(sc);
199608b68b0eSGleb Smirnoff 	}
199708b68b0eSGleb Smirnoff 	CIF_UNLOCK(ifp->if_carp);
19984cb39345SGleb Smirnoff }
19994cb39345SGleb Smirnoff 
20004cb39345SGleb Smirnoff static void
200108b68b0eSGleb Smirnoff carp_sc_state(struct carp_softc *sc)
20024cb39345SGleb Smirnoff {
200308b68b0eSGleb Smirnoff 
200408b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
20054cb39345SGleb Smirnoff 
2006e8c34a71SGleb Smirnoff 	if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
2007e8c34a71SGleb Smirnoff 	    !(sc->sc_carpdev->if_flags & IFF_UP)) {
2008a9771948SGleb Smirnoff 		callout_stop(&sc->sc_ad_tmo);
200908b68b0eSGleb Smirnoff #ifdef INET
2010a9771948SGleb Smirnoff 		callout_stop(&sc->sc_md_tmo);
201108b68b0eSGleb Smirnoff #endif
201208b68b0eSGleb Smirnoff #ifdef INET6
2013a9771948SGleb Smirnoff 		callout_stop(&sc->sc_md6_tmo);
201408b68b0eSGleb Smirnoff #endif
2015a9771948SGleb Smirnoff 		carp_set_state(sc, INIT);
2016a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
2017f08535f8SGleb Smirnoff 		if (!sc->sc_suppress)
2018c5c392e7SMikolaj Golub 			carp_demote_adj(V_carp_ifdown_adj, "interface down");
2019a9771948SGleb Smirnoff 		sc->sc_suppress = 1;
2020a9771948SGleb Smirnoff 	} else {
2021a9771948SGleb Smirnoff 		carp_set_state(sc, INIT);
2022a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
2023a9771948SGleb Smirnoff 		if (sc->sc_suppress)
2024c5c392e7SMikolaj Golub 			carp_demote_adj(-V_carp_ifdown_adj, "interface up");
2025a9771948SGleb Smirnoff 		sc->sc_suppress = 0;
2026a9771948SGleb Smirnoff 	}
2027a9771948SGleb Smirnoff }
2028a9771948SGleb Smirnoff 
2029f08535f8SGleb Smirnoff static void
2030f08535f8SGleb Smirnoff carp_demote_adj(int adj, char *reason)
2031f08535f8SGleb Smirnoff {
2032c5c392e7SMikolaj Golub 	atomic_add_int(&V_carp_demotion, adj);
2033c5c392e7SMikolaj Golub 	CARP_LOG("demoted by %d to %d (%s)\n", adj, V_carp_demotion, reason);
2034f08535f8SGleb Smirnoff 	taskqueue_enqueue(taskqueue_swi, &carp_sendall_task);
2035f08535f8SGleb Smirnoff }
203608b68b0eSGleb Smirnoff 
20377951008bSGleb Smirnoff static int
20387951008bSGleb Smirnoff carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS)
20397951008bSGleb Smirnoff {
20407951008bSGleb Smirnoff 	int new, error;
20417951008bSGleb Smirnoff 
2042c5c392e7SMikolaj Golub 	new = V_carp_demotion;
20437951008bSGleb Smirnoff 	error = sysctl_handle_int(oidp, &new, 0, req);
20447951008bSGleb Smirnoff 	if (error || !req->newptr)
20457951008bSGleb Smirnoff 		return (error);
20467951008bSGleb Smirnoff 
20477951008bSGleb Smirnoff 	carp_demote_adj(new, "sysctl");
20487951008bSGleb Smirnoff 
20497951008bSGleb Smirnoff 	return (0);
20507951008bSGleb Smirnoff }
20517951008bSGleb Smirnoff 
205254bfbd51SWill Andrews #ifdef INET
205354bfbd51SWill Andrews extern  struct domain inetdomain;
205454bfbd51SWill Andrews static struct protosw in_carp_protosw = {
205554bfbd51SWill Andrews 	.pr_type =		SOCK_RAW,
205654bfbd51SWill Andrews 	.pr_domain =		&inetdomain,
205754bfbd51SWill Andrews 	.pr_protocol =		IPPROTO_CARP,
205854bfbd51SWill Andrews 	.pr_flags =		PR_ATOMIC|PR_ADDR,
205954bfbd51SWill Andrews 	.pr_input =		carp_input,
206073d76e77SKevin Lo 	.pr_output =		rip_output,
206154bfbd51SWill Andrews 	.pr_ctloutput =		rip_ctloutput,
206254bfbd51SWill Andrews 	.pr_usrreqs =		&rip_usrreqs
206354bfbd51SWill Andrews };
206454bfbd51SWill Andrews #endif
206554bfbd51SWill Andrews 
206654bfbd51SWill Andrews #ifdef INET6
206754bfbd51SWill Andrews extern	struct domain inet6domain;
20688f5a8818SKevin Lo static struct protosw in6_carp_protosw = {
206954bfbd51SWill Andrews 	.pr_type =		SOCK_RAW,
207054bfbd51SWill Andrews 	.pr_domain =		&inet6domain,
207154bfbd51SWill Andrews 	.pr_protocol =		IPPROTO_CARP,
207254bfbd51SWill Andrews 	.pr_flags =		PR_ATOMIC|PR_ADDR,
207354bfbd51SWill Andrews 	.pr_input =		carp6_input,
207473d76e77SKevin Lo 	.pr_output =		rip6_output,
207554bfbd51SWill Andrews 	.pr_ctloutput =		rip6_ctloutput,
207654bfbd51SWill Andrews 	.pr_usrreqs =		&rip6_usrreqs
207754bfbd51SWill Andrews };
207854bfbd51SWill Andrews #endif
207954bfbd51SWill Andrews 
208054bfbd51SWill Andrews static void
208154bfbd51SWill Andrews carp_mod_cleanup(void)
2082a9771948SGleb Smirnoff {
208354bfbd51SWill Andrews 
208454bfbd51SWill Andrews #ifdef INET
208554bfbd51SWill Andrews 	if (proto_reg[CARP_INET] == 0) {
208615249f73SWill Andrews 		(void)ipproto_unregister(IPPROTO_CARP);
208754bfbd51SWill Andrews 		pf_proto_unregister(PF_INET, IPPROTO_CARP, SOCK_RAW);
208854bfbd51SWill Andrews 		proto_reg[CARP_INET] = -1;
208954bfbd51SWill Andrews 	}
209054bfbd51SWill Andrews 	carp_iamatch_p = NULL;
209154bfbd51SWill Andrews #endif
209254bfbd51SWill Andrews #ifdef INET6
209354bfbd51SWill Andrews 	if (proto_reg[CARP_INET6] == 0) {
209415249f73SWill Andrews 		(void)ip6proto_unregister(IPPROTO_CARP);
209554bfbd51SWill Andrews 		pf_proto_unregister(PF_INET6, IPPROTO_CARP, SOCK_RAW);
209654bfbd51SWill Andrews 		proto_reg[CARP_INET6] = -1;
209754bfbd51SWill Andrews 	}
209854bfbd51SWill Andrews 	carp_iamatch6_p = NULL;
209954bfbd51SWill Andrews 	carp_macmatch6_p = NULL;
210054bfbd51SWill Andrews #endif
210108b68b0eSGleb Smirnoff 	carp_ioctl_p = NULL;
210208b68b0eSGleb Smirnoff 	carp_attach_p = NULL;
210308b68b0eSGleb Smirnoff 	carp_detach_p = NULL;
210408b68b0eSGleb Smirnoff 	carp_get_vhid_p = NULL;
210554bfbd51SWill Andrews 	carp_linkstate_p = NULL;
210654bfbd51SWill Andrews 	carp_forus_p = NULL;
210754bfbd51SWill Andrews 	carp_output_p = NULL;
2108f08535f8SGleb Smirnoff 	carp_demote_adj_p = NULL;
210924421c1cSGleb Smirnoff 	carp_master_p = NULL;
2110f08535f8SGleb Smirnoff 	mtx_unlock(&carp_mtx);
2111f08535f8SGleb Smirnoff 	taskqueue_drain(taskqueue_swi, &carp_sendall_task);
211254bfbd51SWill Andrews 	mtx_destroy(&carp_mtx);
211354bfbd51SWill Andrews }
211454bfbd51SWill Andrews 
211554bfbd51SWill Andrews static int
211654bfbd51SWill Andrews carp_mod_load(void)
211754bfbd51SWill Andrews {
211815249f73SWill Andrews 	int err;
211954bfbd51SWill Andrews 
2120d92d54d5SGleb Smirnoff 	mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
212108b68b0eSGleb Smirnoff 	LIST_INIT(&carp_list);
212208b68b0eSGleb Smirnoff 	carp_get_vhid_p = carp_get_vhid;
212354bfbd51SWill Andrews 	carp_forus_p = carp_forus;
212454bfbd51SWill Andrews 	carp_output_p = carp_output;
212508b68b0eSGleb Smirnoff 	carp_linkstate_p = carp_linkstate;
212608b68b0eSGleb Smirnoff 	carp_ioctl_p = carp_ioctl;
212708b68b0eSGleb Smirnoff 	carp_attach_p = carp_attach;
212808b68b0eSGleb Smirnoff 	carp_detach_p = carp_detach;
2129f08535f8SGleb Smirnoff 	carp_demote_adj_p = carp_demote_adj;
213024421c1cSGleb Smirnoff 	carp_master_p = carp_master;
213154bfbd51SWill Andrews #ifdef INET6
213254bfbd51SWill Andrews 	carp_iamatch6_p = carp_iamatch6;
213354bfbd51SWill Andrews 	carp_macmatch6_p = carp_macmatch6;
213454bfbd51SWill Andrews 	proto_reg[CARP_INET6] = pf_proto_register(PF_INET6,
213554bfbd51SWill Andrews 	    (struct protosw *)&in6_carp_protosw);
213608b68b0eSGleb Smirnoff 	if (proto_reg[CARP_INET6]) {
213754bfbd51SWill Andrews 		printf("carp: error %d attaching to PF_INET6\n",
213854bfbd51SWill Andrews 		    proto_reg[CARP_INET6]);
213954bfbd51SWill Andrews 		carp_mod_cleanup();
21406baf7a24SGleb Smirnoff 		return (proto_reg[CARP_INET6]);
214154bfbd51SWill Andrews 	}
214215249f73SWill Andrews 	err = ip6proto_register(IPPROTO_CARP);
214315249f73SWill Andrews 	if (err) {
214415249f73SWill Andrews 		printf("carp: error %d registering with INET6\n", err);
214515249f73SWill Andrews 		carp_mod_cleanup();
21466baf7a24SGleb Smirnoff 		return (err);
214715249f73SWill Andrews 	}
214854bfbd51SWill Andrews #endif
214954bfbd51SWill Andrews #ifdef INET
215054bfbd51SWill Andrews 	carp_iamatch_p = carp_iamatch;
215154bfbd51SWill Andrews 	proto_reg[CARP_INET] = pf_proto_register(PF_INET, &in_carp_protosw);
215208b68b0eSGleb Smirnoff 	if (proto_reg[CARP_INET]) {
215354bfbd51SWill Andrews 		printf("carp: error %d attaching to PF_INET\n",
215454bfbd51SWill Andrews 		    proto_reg[CARP_INET]);
215554bfbd51SWill Andrews 		carp_mod_cleanup();
21566baf7a24SGleb Smirnoff 		return (proto_reg[CARP_INET]);
215754bfbd51SWill Andrews 	}
215815249f73SWill Andrews 	err = ipproto_register(IPPROTO_CARP);
215915249f73SWill Andrews 	if (err) {
216015249f73SWill Andrews 		printf("carp: error %d registering with INET\n", err);
216115249f73SWill Andrews 		carp_mod_cleanup();
21626baf7a24SGleb Smirnoff 		return (err);
216315249f73SWill Andrews 	}
216454bfbd51SWill Andrews #endif
216508b68b0eSGleb Smirnoff 	return (0);
216654bfbd51SWill Andrews }
2167a9771948SGleb Smirnoff 
216854bfbd51SWill Andrews static int
216954bfbd51SWill Andrews carp_modevent(module_t mod, int type, void *data)
217054bfbd51SWill Andrews {
217154bfbd51SWill Andrews 	switch (type) {
217254bfbd51SWill Andrews 	case MOD_LOAD:
217354bfbd51SWill Andrews 		return carp_mod_load();
217454bfbd51SWill Andrews 		/* NOTREACHED */
2175a9771948SGleb Smirnoff 	case MOD_UNLOAD:
217608b68b0eSGleb Smirnoff 		mtx_lock(&carp_mtx);
217708b68b0eSGleb Smirnoff 		if (LIST_EMPTY(&carp_list))
217854bfbd51SWill Andrews 			carp_mod_cleanup();
217908b68b0eSGleb Smirnoff 		else {
218008b68b0eSGleb Smirnoff 			mtx_unlock(&carp_mtx);
218154bfbd51SWill Andrews 			return (EBUSY);
218208b68b0eSGleb Smirnoff 		}
2183a9771948SGleb Smirnoff 		break;
2184a9771948SGleb Smirnoff 
2185a9771948SGleb Smirnoff 	default:
21860fa08018SGleb Smirnoff 		return (EINVAL);
2187a9771948SGleb Smirnoff 	}
2188a9771948SGleb Smirnoff 
21890fa08018SGleb Smirnoff 	return (0);
2190a9771948SGleb Smirnoff }
2191a9771948SGleb Smirnoff 
2192a9771948SGleb Smirnoff static moduledata_t carp_mod = {
2193a9771948SGleb Smirnoff 	"carp",
2194a9771948SGleb Smirnoff 	carp_modevent,
21959823d527SKevin Lo 	0
2196a9771948SGleb Smirnoff };
2197a9771948SGleb Smirnoff 
2198e24fa11dSWill Andrews DECLARE_MODULE(carp, carp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
2199