xref: /freebsd/sys/netinet/ip_carp.c (revision c4d06976852e009141c95fa31bca765a85102c91)
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>
53a9771948SGleb Smirnoff 
54a9771948SGleb Smirnoff #include <net/ethernet.h>
55a9771948SGleb Smirnoff #include <net/fddi.h>
56a9771948SGleb Smirnoff #include <net/if.h>
57433aaf04SRuslan Ermilov #include <net/if_dl.h>
5808b68b0eSGleb Smirnoff #include <net/if_llatbl.h>
59a9771948SGleb Smirnoff #include <net/if_types.h>
6008b68b0eSGleb Smirnoff #include <net/iso88025.h>
61a9771948SGleb Smirnoff #include <net/route.h>
62530c0060SRobert Watson #include <net/vnet.h>
63a9771948SGleb Smirnoff 
64a0ae8f04SBjoern A. Zeeb #if defined(INET) || defined(INET6)
65a9771948SGleb Smirnoff #include <netinet/in.h>
66a9771948SGleb Smirnoff #include <netinet/in_var.h>
67a0ae8f04SBjoern A. Zeeb #include <netinet/ip_carp.h>
68a9771948SGleb Smirnoff #include <netinet/ip.h>
69a0ae8f04SBjoern A. Zeeb #include <machine/in_cksum.h>
70a0ae8f04SBjoern A. Zeeb #endif
71a0ae8f04SBjoern A. Zeeb #ifdef INET
72a9771948SGleb Smirnoff #include <netinet/ip_var.h>
73a9771948SGleb Smirnoff #include <netinet/if_ether.h>
74a9771948SGleb Smirnoff #endif
75a9771948SGleb Smirnoff 
76a9771948SGleb Smirnoff #ifdef INET6
77a9771948SGleb Smirnoff #include <netinet/icmp6.h>
78a9771948SGleb Smirnoff #include <netinet/ip6.h>
7954bfbd51SWill Andrews #include <netinet6/ip6protosw.h>
8008b68b0eSGleb Smirnoff #include <netinet6/in6_var.h>
81a9771948SGleb Smirnoff #include <netinet6/ip6_var.h>
8229da8af6SHajimu UMEMOTO #include <netinet6/scope6_var.h>
83a9771948SGleb Smirnoff #include <netinet6/nd6.h>
84a9771948SGleb Smirnoff #endif
85a9771948SGleb Smirnoff 
86a9771948SGleb Smirnoff #include <crypto/sha1.h>
87a9771948SGleb Smirnoff 
8808b68b0eSGleb Smirnoff static MALLOC_DEFINE(M_CARP, "CARP", "CARP addresses");
89a9771948SGleb Smirnoff 
90a9771948SGleb Smirnoff struct carp_softc {
9108b68b0eSGleb Smirnoff 	struct ifnet		*sc_carpdev;	/* Pointer to parent ifnet. */
9208b68b0eSGleb Smirnoff 	struct ifaddr		**sc_ifas;	/* Our ifaddrs. */
9308b68b0eSGleb Smirnoff 	struct sockaddr_dl	sc_addr;	/* Our link level address. */
9408b68b0eSGleb Smirnoff 	struct callout		sc_ad_tmo;	/* Advertising timeout. */
95a0ae8f04SBjoern A. Zeeb #ifdef INET
9608b68b0eSGleb Smirnoff 	struct callout		sc_md_tmo;	/* Master down timeout. */
97a0ae8f04SBjoern A. Zeeb #endif
98a9771948SGleb Smirnoff #ifdef INET6
9908b68b0eSGleb Smirnoff 	struct callout 		sc_md6_tmo;	/* XXX: Master down timeout. */
10008b68b0eSGleb Smirnoff #endif
10108b68b0eSGleb Smirnoff 	struct mtx		sc_mtx;
102a9771948SGleb Smirnoff 
10308b68b0eSGleb Smirnoff 	int			sc_vhid;
10408b68b0eSGleb Smirnoff 	int			sc_advskew;
10508b68b0eSGleb Smirnoff 	int			sc_advbase;
10608b68b0eSGleb Smirnoff 
10708b68b0eSGleb Smirnoff 	int			sc_naddrs;
10808b68b0eSGleb Smirnoff 	int			sc_naddrs6;
10908b68b0eSGleb Smirnoff 	int			sc_ifasiz;
110a9771948SGleb Smirnoff 	enum { INIT = 0, BACKUP, MASTER }	sc_state;
111a9771948SGleb Smirnoff 	int			sc_suppress;
112a9771948SGleb Smirnoff 	int			sc_sendad_errors;
113a9771948SGleb Smirnoff #define	CARP_SENDAD_MAX_ERRORS	3
114a9771948SGleb Smirnoff 	int			sc_sendad_success;
115a9771948SGleb Smirnoff #define	CARP_SENDAD_MIN_SUCCESS 3
116a9771948SGleb Smirnoff 
117a9771948SGleb Smirnoff 	int			sc_init_counter;
11808b68b0eSGleb Smirnoff 	uint64_t		sc_counter;
119a9771948SGleb Smirnoff 
120a9771948SGleb Smirnoff 	/* authentication */
121a9771948SGleb Smirnoff #define	CARP_HMAC_PAD	64
122a9771948SGleb Smirnoff 	unsigned char sc_key[CARP_KEY_LEN];
123a9771948SGleb Smirnoff 	unsigned char sc_pad[CARP_HMAC_PAD];
124a9771948SGleb Smirnoff 	SHA1_CTX sc_sha1;
125a9771948SGleb Smirnoff 
12608b68b0eSGleb Smirnoff 	TAILQ_ENTRY(carp_softc)	sc_list;	/* On the carp_if list. */
12708b68b0eSGleb Smirnoff 	LIST_ENTRY(carp_softc)	sc_next;	/* On the global list. */
128a9771948SGleb Smirnoff };
12908b68b0eSGleb Smirnoff 
13008b68b0eSGleb Smirnoff struct carp_if {
13108b68b0eSGleb Smirnoff #ifdef INET
13208b68b0eSGleb Smirnoff 	int	cif_naddrs;
13308b68b0eSGleb Smirnoff #endif
13408b68b0eSGleb Smirnoff #ifdef INET6
13508b68b0eSGleb Smirnoff 	int	cif_naddrs6;
13608b68b0eSGleb Smirnoff #endif
13708b68b0eSGleb Smirnoff 	TAILQ_HEAD(, carp_softc) cif_vrs;
13808b68b0eSGleb Smirnoff #ifdef INET
13908b68b0eSGleb Smirnoff 	struct ip_moptions 	 cif_imo;
14008b68b0eSGleb Smirnoff #endif
14108b68b0eSGleb Smirnoff #ifdef INET6
14208b68b0eSGleb Smirnoff 	struct ip6_moptions 	 cif_im6o;
14308b68b0eSGleb Smirnoff #endif
14408b68b0eSGleb Smirnoff 	struct ifnet	*cif_ifp;
14508b68b0eSGleb Smirnoff 	struct mtx	cif_mtx;
14608b68b0eSGleb Smirnoff };
14708b68b0eSGleb Smirnoff 
14808b68b0eSGleb Smirnoff #define	CARP_INET	0
14908b68b0eSGleb Smirnoff #define	CARP_INET6	1
15008b68b0eSGleb Smirnoff static int proto_reg[] = {-1, -1};
15108b68b0eSGleb Smirnoff 
15208b68b0eSGleb Smirnoff /*
15308b68b0eSGleb Smirnoff  * Brief design of carp(4).
15408b68b0eSGleb Smirnoff  *
15508b68b0eSGleb Smirnoff  * Any carp-capable ifnet may have a list of carp softcs hanging off
15608b68b0eSGleb Smirnoff  * its ifp->if_carp pointer. Each softc represents one unique virtual
15708b68b0eSGleb Smirnoff  * host id, or vhid. The softc has a back pointer to the ifnet. All
15808b68b0eSGleb Smirnoff  * softcs are joined in a global list, which has quite limited use.
15908b68b0eSGleb Smirnoff  *
16008b68b0eSGleb Smirnoff  * Any interface address that takes part in CARP negotiation has a
16108b68b0eSGleb Smirnoff  * pointer to the softc of its vhid, ifa->ifa_carp. That could be either
16208b68b0eSGleb Smirnoff  * AF_INET or AF_INET6 address.
16308b68b0eSGleb Smirnoff  *
16408b68b0eSGleb Smirnoff  * Although, one can get the softc's backpointer to ifnet and traverse
16508b68b0eSGleb Smirnoff  * through its ifp->if_addrhead queue to find all interface addresses
16608b68b0eSGleb Smirnoff  * involved in CARP, we keep a growable array of ifaddr pointers. This
16708b68b0eSGleb Smirnoff  * allows us to avoid grabbing the IF_ADDR_LOCK() in many traversals that
16808b68b0eSGleb Smirnoff  * do calls into the network stack, thus avoiding LORs.
16908b68b0eSGleb Smirnoff  *
17008b68b0eSGleb Smirnoff  * Locking:
17108b68b0eSGleb Smirnoff  *
17208b68b0eSGleb Smirnoff  * Each softc has a lock sc_mtx. It is used to synchronise carp_input_c(),
17308b68b0eSGleb Smirnoff  * callout-driven events and ioctl()s.
17408b68b0eSGleb Smirnoff  *
17508b68b0eSGleb Smirnoff  * To traverse the list of softcs on an ifnet we use CIF_LOCK(), to
17608b68b0eSGleb Smirnoff  * traverse the global list we use the mutex carp_mtx.
17708b68b0eSGleb Smirnoff  *
17808b68b0eSGleb Smirnoff  * Known issues with locking:
17908b68b0eSGleb Smirnoff  *
18008b68b0eSGleb Smirnoff  * - There is no protection for races between two ioctl() requests,
18108b68b0eSGleb Smirnoff  *   neither SIOCSVH, nor SIOCAIFADDR & SIOCAIFADDR_IN6. I think that all
18208b68b0eSGleb Smirnoff  *   interface ioctl()s should be serialized right in net/if.c.
18308b68b0eSGleb Smirnoff  * - Sending ad, we put the pointer to the softc in an mtag, and no reference
18408b68b0eSGleb Smirnoff  *   counting is done on the softc.
18508b68b0eSGleb Smirnoff  * - On module unload we may race (?) with packet processing thread
18608b68b0eSGleb Smirnoff  *   dereferencing our function pointers.
18708b68b0eSGleb Smirnoff  */
188a9771948SGleb Smirnoff 
189f08535f8SGleb Smirnoff static int carp_allow = 1;		/* Accept incoming CARP packets. */
190f08535f8SGleb Smirnoff static int carp_preempt = 0;		/* Preempt slower nodes. */
191f08535f8SGleb Smirnoff static int carp_log = 1;		/* Log level. */
192f08535f8SGleb Smirnoff static int carp_demotion = 0;		/* Global advskew demotion. */
193f08535f8SGleb Smirnoff static int carp_senderr_adj = CARP_MAXSKEW;	/* Send error demotion factor */
194f08535f8SGleb Smirnoff static int carp_ifdown_adj = CARP_MAXSKEW;	/* Iface down demotion factor */
1957951008bSGleb Smirnoff static int carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS);
196a9771948SGleb Smirnoff 
197f08535f8SGleb Smirnoff SYSCTL_NODE(_net_inet, IPPROTO_CARP,	carp,	CTLFLAG_RW, 0,	"CARP");
198f08535f8SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, allow, CTLFLAG_RW, &carp_allow, 0,
199f08535f8SGleb Smirnoff     "Accept incoming CARP packets");
200f08535f8SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, preempt, CTLFLAG_RW, &carp_preempt, 0,
201f08535f8SGleb Smirnoff     "High-priority backup preemption mode");
202f08535f8SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, log, CTLFLAG_RW, &carp_log, 0,
203f08535f8SGleb Smirnoff     "CARP log level");
2047951008bSGleb Smirnoff SYSCTL_PROC(_net_inet_carp, OID_AUTO, demotion, CTLTYPE_INT|CTLFLAG_RW,
2057951008bSGleb Smirnoff     0, 0, carp_demote_adj_sysctl, "I",
2067951008bSGleb Smirnoff     "Adjust demotion factor (skew of advskew)");
207f08535f8SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, senderr_demotion_factor, CTLFLAG_RW,
208f08535f8SGleb Smirnoff     &carp_senderr_adj, 0, "Send error demotion factor adjustment");
209f08535f8SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, ifdown_demotion_factor, CTLFLAG_RW,
210f08535f8SGleb Smirnoff     &carp_ifdown_adj, 0, "Interface down demotion factor adjustment");
211f08535f8SGleb Smirnoff 
212f08535f8SGleb Smirnoff static struct carpstats carpstats;
213f08535f8SGleb Smirnoff SYSCTL_STRUCT(_net_inet_carp, OID_AUTO, stats, CTLFLAG_RW, &carpstats,
214f08535f8SGleb Smirnoff     carpstats, "CARP statistics (struct carpstats, netinet/ip_carp.h)");
215a9771948SGleb Smirnoff 
21608b68b0eSGleb Smirnoff #define	CARP_LOCK_INIT(sc)	mtx_init(&(sc)->sc_mtx, "carp_softc",   \
217a9771948SGleb Smirnoff 	NULL, MTX_DEF)
21808b68b0eSGleb Smirnoff #define	CARP_LOCK_DESTROY(sc)	mtx_destroy(&(sc)->sc_mtx)
21908b68b0eSGleb Smirnoff #define	CARP_LOCK_ASSERT(sc)	mtx_assert(&(sc)->sc_mtx, MA_OWNED)
22008b68b0eSGleb Smirnoff #define	CARP_LOCK(sc)		mtx_lock(&(sc)->sc_mtx)
22108b68b0eSGleb Smirnoff #define	CARP_UNLOCK(sc)		mtx_unlock(&(sc)->sc_mtx)
22208b68b0eSGleb Smirnoff #define	CIF_LOCK_INIT(cif)	mtx_init(&(cif)->cif_mtx, "carp_if",   \
22308b68b0eSGleb Smirnoff 	NULL, MTX_DEF)
22408b68b0eSGleb Smirnoff #define	CIF_LOCK_DESTROY(cif)	mtx_destroy(&(cif)->cif_mtx)
22508b68b0eSGleb Smirnoff #define	CIF_LOCK_ASSERT(cif)	mtx_assert(&(cif)->cif_mtx, MA_OWNED)
22608b68b0eSGleb Smirnoff #define	CIF_LOCK(cif)		mtx_lock(&(cif)->cif_mtx)
22708b68b0eSGleb Smirnoff #define	CIF_UNLOCK(cif)		mtx_unlock(&(cif)->cif_mtx)
228a9a2c40cSGleb Smirnoff #define	CIF_FREE(cif)	do {				\
229a9a2c40cSGleb Smirnoff 		CIF_LOCK_ASSERT(cif);			\
230a9a2c40cSGleb Smirnoff 		if (TAILQ_EMPTY(&(cif)->cif_vrs))	\
231a9a2c40cSGleb Smirnoff 			carp_free_if(cif);		\
232a9a2c40cSGleb Smirnoff 		else					\
233a9a2c40cSGleb Smirnoff 			CIF_UNLOCK(cif);		\
234a9a2c40cSGleb Smirnoff } while (0)
235d220759bSGleb Smirnoff 
236947b7cf3SGleb Smirnoff #define	CARP_LOG(...)	do {				\
237f08535f8SGleb Smirnoff 	if (carp_log > 0)				\
23808b68b0eSGleb Smirnoff 		log(LOG_INFO, "carp: " __VA_ARGS__);	\
239947b7cf3SGleb Smirnoff } while (0)
2401e9e6572SGleb Smirnoff 
241947b7cf3SGleb Smirnoff #define	CARP_DEBUG(...)	do {				\
242f08535f8SGleb Smirnoff 	if (carp_log > 1)				\
2431e9e6572SGleb Smirnoff 		log(LOG_DEBUG, __VA_ARGS__);		\
244947b7cf3SGleb Smirnoff } while (0)
245a9771948SGleb Smirnoff 
24608b68b0eSGleb Smirnoff #define	IFNET_FOREACH_IFA(ifp, ifa)					\
24708b68b0eSGleb Smirnoff 	IF_ADDR_LOCK_ASSERT(ifp);					\
24808b68b0eSGleb Smirnoff 	TAILQ_FOREACH((ifa), &(ifp)->if_addrhead, ifa_link)		\
24908b68b0eSGleb Smirnoff 		if ((ifa)->ifa_carp != NULL)
25008b68b0eSGleb Smirnoff 
25108b68b0eSGleb Smirnoff #define	CARP_FOREACH_IFA(sc, ifa)					\
25208b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);						\
25308b68b0eSGleb Smirnoff 	for (int _i = 0;						\
25408b68b0eSGleb Smirnoff 		_i < (sc)->sc_naddrs + (sc)->sc_naddrs6 &&		\
25508b68b0eSGleb Smirnoff 		((ifa) = sc->sc_ifas[_i]) != NULL;			\
25608b68b0eSGleb Smirnoff 		++_i)
25708b68b0eSGleb Smirnoff 
25808b68b0eSGleb Smirnoff #define	IFNET_FOREACH_CARP(ifp, sc)					\
25908b68b0eSGleb Smirnoff 	CIF_LOCK_ASSERT(ifp->if_carp);					\
26008b68b0eSGleb Smirnoff 	TAILQ_FOREACH((sc), &(ifp)->if_carp->cif_vrs, sc_list)
26108b68b0eSGleb Smirnoff 
262f08535f8SGleb Smirnoff #define	DEMOTE_ADVSKEW(sc)					\
263f08535f8SGleb Smirnoff     (((sc)->sc_advskew + carp_demotion > CARP_MAXSKEW) ?	\
264f08535f8SGleb Smirnoff     CARP_MAXSKEW : ((sc)->sc_advskew + carp_demotion))
265f08535f8SGleb Smirnoff 
2665c1f0f6dSGleb Smirnoff static void	carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
26708b68b0eSGleb Smirnoff static struct carp_softc
26808b68b0eSGleb Smirnoff 		*carp_alloc(struct ifnet *);
269a9a2c40cSGleb Smirnoff static void	carp_detach_locked(struct ifaddr *);
27008b68b0eSGleb Smirnoff static void	carp_destroy(struct carp_softc *);
27108b68b0eSGleb Smirnoff static struct carp_if
27208b68b0eSGleb Smirnoff 		*carp_alloc_if(struct ifnet *);
27308b68b0eSGleb Smirnoff static void	carp_free_if(struct carp_if *);
27408b68b0eSGleb Smirnoff static void	carp_set_state(struct carp_softc *, int);
27508b68b0eSGleb Smirnoff static void	carp_sc_state(struct carp_softc *);
27608b68b0eSGleb Smirnoff static void	carp_setrun(struct carp_softc *, sa_family_t);
2775c1f0f6dSGleb Smirnoff static void	carp_master_down(void *);
278d220759bSGleb Smirnoff static void	carp_master_down_locked(struct carp_softc *);
27908b68b0eSGleb Smirnoff static void	carp_send_ad(void *);
28008b68b0eSGleb Smirnoff static void	carp_send_ad_locked(struct carp_softc *);
28108b68b0eSGleb Smirnoff static void	carp_addroute(struct carp_softc *);
2822512b096SGleb Smirnoff static void	carp_ifa_addroute(struct ifaddr *);
28308b68b0eSGleb Smirnoff static void	carp_delroute(struct carp_softc *);
2842512b096SGleb Smirnoff static void	carp_ifa_delroute(struct ifaddr *);
285f08535f8SGleb Smirnoff static void	carp_send_ad_all(void *, int);
286f08535f8SGleb Smirnoff static void	carp_demote_adj(int, char *);
287a9771948SGleb Smirnoff 
28808b68b0eSGleb Smirnoff static LIST_HEAD(, carp_softc) carp_list;
289d92d54d5SGleb Smirnoff static struct mtx carp_mtx;
290f08535f8SGleb Smirnoff static struct task carp_sendall_task =
291f08535f8SGleb Smirnoff     TASK_INITIALIZER(0, carp_send_ad_all, NULL);
292a9771948SGleb Smirnoff 
2935c1f0f6dSGleb Smirnoff static void
294a9771948SGleb Smirnoff carp_hmac_prepare(struct carp_softc *sc)
295a9771948SGleb Smirnoff {
29608b68b0eSGleb Smirnoff 	uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
29708b68b0eSGleb Smirnoff 	uint8_t vhid = sc->sc_vhid & 0xff;
298a9771948SGleb Smirnoff 	struct ifaddr *ifa;
2991ead26d4SMax Laier 	int i, found;
3001ead26d4SMax Laier #ifdef INET
3011ead26d4SMax Laier 	struct in_addr last, cur, in;
3021ead26d4SMax Laier #endif
303a9771948SGleb Smirnoff #ifdef INET6
3041ead26d4SMax Laier 	struct in6_addr last6, cur6, in6;
305a9771948SGleb Smirnoff #endif
306a9771948SGleb Smirnoff 
30708b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
308d220759bSGleb Smirnoff 
30908b68b0eSGleb Smirnoff 	/* Compute ipad from key. */
310a9771948SGleb Smirnoff 	bzero(sc->sc_pad, sizeof(sc->sc_pad));
311a9771948SGleb Smirnoff 	bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
312a9771948SGleb Smirnoff 	for (i = 0; i < sizeof(sc->sc_pad); i++)
313a9771948SGleb Smirnoff 		sc->sc_pad[i] ^= 0x36;
314a9771948SGleb Smirnoff 
31508b68b0eSGleb Smirnoff 	/* Precompute first part of inner hash. */
316a9771948SGleb Smirnoff 	SHA1Init(&sc->sc_sha1);
317a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
318a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
319a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
320a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
321a9771948SGleb Smirnoff #ifdef INET
3221ead26d4SMax Laier 	cur.s_addr = 0;
3231ead26d4SMax Laier 	do {
3241ead26d4SMax Laier 		found = 0;
3251ead26d4SMax Laier 		last = cur;
3261ead26d4SMax Laier 		cur.s_addr = 0xffffffff;
32708b68b0eSGleb Smirnoff 		CARP_FOREACH_IFA(sc, ifa) {
3281ead26d4SMax Laier 			in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr;
3291ead26d4SMax Laier 			if (ifa->ifa_addr->sa_family == AF_INET &&
3301ead26d4SMax Laier 			    ntohl(in.s_addr) > ntohl(last.s_addr) &&
3311ead26d4SMax Laier 			    ntohl(in.s_addr) < ntohl(cur.s_addr)) {
3321ead26d4SMax Laier 				cur.s_addr = in.s_addr;
3331ead26d4SMax Laier 				found++;
334a9771948SGleb Smirnoff 			}
3351ead26d4SMax Laier 		}
3361ead26d4SMax Laier 		if (found)
3371ead26d4SMax Laier 			SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur));
3381ead26d4SMax Laier 	} while (found);
339a9771948SGleb Smirnoff #endif /* INET */
340a9771948SGleb Smirnoff #ifdef INET6
3411ead26d4SMax Laier 	memset(&cur6, 0, sizeof(cur6));
3421ead26d4SMax Laier 	do {
3431ead26d4SMax Laier 		found = 0;
3441ead26d4SMax Laier 		last6 = cur6;
3451ead26d4SMax Laier 		memset(&cur6, 0xff, sizeof(cur6));
34608b68b0eSGleb Smirnoff 		CARP_FOREACH_IFA(sc, ifa) {
347a9771948SGleb Smirnoff 			in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
3481ead26d4SMax Laier 			if (IN6_IS_SCOPE_EMBED(&in6))
3491ead26d4SMax Laier 				in6.s6_addr16[1] = 0;
3501ead26d4SMax Laier 			if (ifa->ifa_addr->sa_family == AF_INET6 &&
3511ead26d4SMax Laier 			    memcmp(&in6, &last6, sizeof(in6)) > 0 &&
3521ead26d4SMax Laier 			    memcmp(&in6, &cur6, sizeof(in6)) < 0) {
3531ead26d4SMax Laier 				cur6 = in6;
3541ead26d4SMax Laier 				found++;
355a9771948SGleb Smirnoff 			}
356a9771948SGleb Smirnoff 		}
3571ead26d4SMax Laier 		if (found)
3581ead26d4SMax Laier 			SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6));
3591ead26d4SMax Laier 	} while (found);
360a9771948SGleb Smirnoff #endif /* INET6 */
361a9771948SGleb Smirnoff 
362a9771948SGleb Smirnoff 	/* convert ipad to opad */
363a9771948SGleb Smirnoff 	for (i = 0; i < sizeof(sc->sc_pad); i++)
364a9771948SGleb Smirnoff 		sc->sc_pad[i] ^= 0x36 ^ 0x5c;
365a9771948SGleb Smirnoff }
366a9771948SGleb Smirnoff 
3675c1f0f6dSGleb Smirnoff static void
36808b68b0eSGleb Smirnoff carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
369a9771948SGleb Smirnoff     unsigned char md[20])
370a9771948SGleb Smirnoff {
371a9771948SGleb Smirnoff 	SHA1_CTX sha1ctx;
372a9771948SGleb Smirnoff 
37308b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
37408b68b0eSGleb Smirnoff 
375a9771948SGleb Smirnoff 	/* fetch first half of inner hash */
376a9771948SGleb Smirnoff 	bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
377a9771948SGleb Smirnoff 
378a9771948SGleb Smirnoff 	SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
379a9771948SGleb Smirnoff 	SHA1Final(md, &sha1ctx);
380a9771948SGleb Smirnoff 
381a9771948SGleb Smirnoff 	/* outer hash */
382a9771948SGleb Smirnoff 	SHA1Init(&sha1ctx);
383a9771948SGleb Smirnoff 	SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
384a9771948SGleb Smirnoff 	SHA1Update(&sha1ctx, md, 20);
385a9771948SGleb Smirnoff 	SHA1Final(md, &sha1ctx);
386a9771948SGleb Smirnoff }
387a9771948SGleb Smirnoff 
3885c1f0f6dSGleb Smirnoff static int
38908b68b0eSGleb Smirnoff carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
390a9771948SGleb Smirnoff     unsigned char md[20])
391a9771948SGleb Smirnoff {
392a9771948SGleb Smirnoff 	unsigned char md2[20];
393a9771948SGleb Smirnoff 
39408b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
395d220759bSGleb Smirnoff 
396a9771948SGleb Smirnoff 	carp_hmac_generate(sc, counter, md2);
397a9771948SGleb Smirnoff 
398a9771948SGleb Smirnoff 	return (bcmp(md, md2, sizeof(md2)));
399a9771948SGleb Smirnoff }
400a9771948SGleb Smirnoff 
401a9771948SGleb Smirnoff /*
402a9771948SGleb Smirnoff  * process input packet.
403a9771948SGleb Smirnoff  * we have rearranged checks order compared to the rfc,
404a9771948SGleb Smirnoff  * but it seems more efficient this way or not possible otherwise.
405a9771948SGleb Smirnoff  */
406a0ae8f04SBjoern A. Zeeb #ifdef INET
407a9771948SGleb Smirnoff void
408a9771948SGleb Smirnoff carp_input(struct mbuf *m, int hlen)
409a9771948SGleb Smirnoff {
410a9771948SGleb Smirnoff 	struct ip *ip = mtod(m, struct ip *);
411a9771948SGleb Smirnoff 	struct carp_header *ch;
412a9771948SGleb Smirnoff 	int iplen, len;
413a9771948SGleb Smirnoff 
4146bf65bcfSRobert Watson 	CARPSTATS_INC(carps_ipackets);
415a9771948SGleb Smirnoff 
416f08535f8SGleb Smirnoff 	if (!carp_allow) {
417a9771948SGleb Smirnoff 		m_freem(m);
418a9771948SGleb Smirnoff 		return;
419a9771948SGleb Smirnoff 	}
420a9771948SGleb Smirnoff 
421a9771948SGleb Smirnoff 	/* verify that the IP TTL is 255.  */
422a9771948SGleb Smirnoff 	if (ip->ip_ttl != CARP_DFLTTL) {
4236bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badttl);
42408b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
4251e9e6572SGleb Smirnoff 		    ip->ip_ttl,
4261e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
427a9771948SGleb Smirnoff 		m_freem(m);
428a9771948SGleb Smirnoff 		return;
429a9771948SGleb Smirnoff 	}
430a9771948SGleb Smirnoff 
431a9771948SGleb Smirnoff 	iplen = ip->ip_hl << 2;
432a9771948SGleb Smirnoff 
433a9771948SGleb Smirnoff 	if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
4346bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badlen);
43508b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: received len %zd < sizeof(struct carp_header) "
43608b68b0eSGleb Smirnoff 		    "on %s\n", __func__, m->m_len - sizeof(struct ip),
43716e324fcSXin LI 		    m->m_pkthdr.rcvif->if_xname);
438a9771948SGleb Smirnoff 		m_freem(m);
439a9771948SGleb Smirnoff 		return;
440a9771948SGleb Smirnoff 	}
441a9771948SGleb Smirnoff 
442a9771948SGleb Smirnoff 	if (iplen + sizeof(*ch) < m->m_len) {
443a9771948SGleb Smirnoff 		if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
4446bf65bcfSRobert Watson 			CARPSTATS_INC(carps_hdrops);
44508b68b0eSGleb Smirnoff 			CARP_DEBUG("%s: pullup failed\n", __func__);
446a9771948SGleb Smirnoff 			return;
447a9771948SGleb Smirnoff 		}
448a9771948SGleb Smirnoff 		ip = mtod(m, struct ip *);
449a9771948SGleb Smirnoff 	}
450a9771948SGleb Smirnoff 	ch = (struct carp_header *)((char *)ip + iplen);
451a9771948SGleb Smirnoff 
452a9771948SGleb Smirnoff 	/*
453a9771948SGleb Smirnoff 	 * verify that the received packet length is
454a9771948SGleb Smirnoff 	 * equal to the CARP header
455a9771948SGleb Smirnoff 	 */
456a9771948SGleb Smirnoff 	len = iplen + sizeof(*ch);
457a9771948SGleb Smirnoff 	if (len > m->m_pkthdr.len) {
4586bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badlen);
45908b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: packet too short %d on %s\n", __func__,
4601e9e6572SGleb Smirnoff 		    m->m_pkthdr.len,
4611e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
462a9771948SGleb Smirnoff 		m_freem(m);
463a9771948SGleb Smirnoff 		return;
464a9771948SGleb Smirnoff 	}
465a9771948SGleb Smirnoff 
466a9771948SGleb Smirnoff 	if ((m = m_pullup(m, len)) == NULL) {
4676bf65bcfSRobert Watson 		CARPSTATS_INC(carps_hdrops);
468a9771948SGleb Smirnoff 		return;
469a9771948SGleb Smirnoff 	}
470a9771948SGleb Smirnoff 	ip = mtod(m, struct ip *);
471a9771948SGleb Smirnoff 	ch = (struct carp_header *)((char *)ip + iplen);
472a9771948SGleb Smirnoff 
473a9771948SGleb Smirnoff 	/* verify the CARP checksum */
474a9771948SGleb Smirnoff 	m->m_data += iplen;
475*c4d06976SGleb Smirnoff 	if (in_cksum(m, len - iplen)) {
4766bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badsum);
47708b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: checksum failed on %s\n", __func__,
4781e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
479a9771948SGleb Smirnoff 		m_freem(m);
480a9771948SGleb Smirnoff 		return;
481a9771948SGleb Smirnoff 	}
482a9771948SGleb Smirnoff 	m->m_data -= iplen;
483a9771948SGleb Smirnoff 
4841e9e6572SGleb Smirnoff 	carp_input_c(m, ch, AF_INET);
485a9771948SGleb Smirnoff }
486a0ae8f04SBjoern A. Zeeb #endif
487a9771948SGleb Smirnoff 
488a9771948SGleb Smirnoff #ifdef INET6
489a9771948SGleb Smirnoff int
490a9771948SGleb Smirnoff carp6_input(struct mbuf **mp, int *offp, int proto)
491a9771948SGleb Smirnoff {
492a9771948SGleb Smirnoff 	struct mbuf *m = *mp;
493a9771948SGleb Smirnoff 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
494a9771948SGleb Smirnoff 	struct carp_header *ch;
495a9771948SGleb Smirnoff 	u_int len;
496a9771948SGleb Smirnoff 
4976bf65bcfSRobert Watson 	CARPSTATS_INC(carps_ipackets6);
498a9771948SGleb Smirnoff 
499f08535f8SGleb Smirnoff 	if (!carp_allow) {
500a9771948SGleb Smirnoff 		m_freem(m);
501a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
502a9771948SGleb Smirnoff 	}
503a9771948SGleb Smirnoff 
504a9771948SGleb Smirnoff 	/* check if received on a valid carp interface */
505a9771948SGleb Smirnoff 	if (m->m_pkthdr.rcvif->if_carp == NULL) {
5066bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badif);
50708b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: packet received on non-carp interface: %s\n",
50808b68b0eSGleb Smirnoff 		    __func__, m->m_pkthdr.rcvif->if_xname);
509a9771948SGleb Smirnoff 		m_freem(m);
510a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
511a9771948SGleb Smirnoff 	}
512a9771948SGleb Smirnoff 
513a9771948SGleb Smirnoff 	/* verify that the IP TTL is 255 */
514a9771948SGleb Smirnoff 	if (ip6->ip6_hlim != CARP_DFLTTL) {
5156bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badttl);
51608b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
51708b68b0eSGleb Smirnoff 		    ip6->ip6_hlim, m->m_pkthdr.rcvif->if_xname);
518a9771948SGleb Smirnoff 		m_freem(m);
519a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
520a9771948SGleb Smirnoff 	}
521a9771948SGleb Smirnoff 
522a9771948SGleb Smirnoff 	/* verify that we have a complete carp packet */
523a9771948SGleb Smirnoff 	len = m->m_len;
524a9771948SGleb Smirnoff 	IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
525a9771948SGleb Smirnoff 	if (ch == NULL) {
5266bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badlen);
52708b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: packet size %u too small\n", __func__, len);
528a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
529a9771948SGleb Smirnoff 	}
530a9771948SGleb Smirnoff 
531a9771948SGleb Smirnoff 
532a9771948SGleb Smirnoff 	/* verify the CARP checksum */
533a9771948SGleb Smirnoff 	m->m_data += *offp;
534*c4d06976SGleb Smirnoff 	if (in_cksum(m, sizeof(*ch))) {
5356bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badsum);
53608b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: checksum failed, on %s\n", __func__,
5371e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
538a9771948SGleb Smirnoff 		m_freem(m);
539a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
540a9771948SGleb Smirnoff 	}
541a9771948SGleb Smirnoff 	m->m_data -= *offp;
542a9771948SGleb Smirnoff 
5431e9e6572SGleb Smirnoff 	carp_input_c(m, ch, AF_INET6);
544a9771948SGleb Smirnoff 	return (IPPROTO_DONE);
545a9771948SGleb Smirnoff }
546a9771948SGleb Smirnoff #endif /* INET6 */
547a9771948SGleb Smirnoff 
5485c1f0f6dSGleb Smirnoff static void
5491e9e6572SGleb Smirnoff carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
550a9771948SGleb Smirnoff {
551a9771948SGleb Smirnoff 	struct ifnet *ifp = m->m_pkthdr.rcvif;
55208b68b0eSGleb Smirnoff 	struct ifaddr *ifa;
5531e9e6572SGleb Smirnoff 	struct carp_softc *sc;
55408b68b0eSGleb Smirnoff 	uint64_t tmp_counter;
555a9771948SGleb Smirnoff 	struct timeval sc_tv, ch_tv;
556a9771948SGleb Smirnoff 
557a9771948SGleb Smirnoff 	/* verify that the VHID is valid on the receiving interface */
558137f91e8SJohn Baldwin 	IF_ADDR_RLOCK(ifp);
55908b68b0eSGleb Smirnoff 	IFNET_FOREACH_IFA(ifp, ifa)
56008b68b0eSGleb Smirnoff 		if (ifa->ifa_addr->sa_family == af &&
56108b68b0eSGleb Smirnoff 		    ifa->ifa_carp->sc_vhid == ch->carp_vhid) {
56208b68b0eSGleb Smirnoff 			ifa_ref(ifa);
563a9771948SGleb Smirnoff 			break;
56408b68b0eSGleb Smirnoff 		}
565137f91e8SJohn Baldwin 	IF_ADDR_RUNLOCK(ifp);
566d220759bSGleb Smirnoff 
56708b68b0eSGleb Smirnoff 	if (ifa == NULL) {
5686bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badvhid);
569a9771948SGleb Smirnoff 		m_freem(m);
570a9771948SGleb Smirnoff 		return;
571a9771948SGleb Smirnoff 	}
572a9771948SGleb Smirnoff 
573a9771948SGleb Smirnoff 	/* verify the CARP version. */
574a9771948SGleb Smirnoff 	if (ch->carp_version != CARP_VERSION) {
5756bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badver);
57608b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: invalid version %d\n", ifp->if_xname,
5771e9e6572SGleb Smirnoff 		    ch->carp_version);
57808b68b0eSGleb Smirnoff 		ifa_free(ifa);
579a9771948SGleb Smirnoff 		m_freem(m);
580a9771948SGleb Smirnoff 		return;
581a9771948SGleb Smirnoff 	}
582a9771948SGleb Smirnoff 
58308b68b0eSGleb Smirnoff 	sc = ifa->ifa_carp;
58408b68b0eSGleb Smirnoff 	CARP_LOCK(sc);
58508b68b0eSGleb Smirnoff 	ifa_free(ifa);
58608b68b0eSGleb Smirnoff 
587a9771948SGleb Smirnoff 	if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
5886bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badauth);
58908b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: incorrect hash for VHID %u@%s\n", __func__,
59008b68b0eSGleb Smirnoff 		    sc->sc_vhid, ifp->if_xname);
59108b68b0eSGleb Smirnoff 		goto out;
592a9771948SGleb Smirnoff 	}
593a9771948SGleb Smirnoff 
594a9771948SGleb Smirnoff 	tmp_counter = ntohl(ch->carp_counter[0]);
595a9771948SGleb Smirnoff 	tmp_counter = tmp_counter<<32;
596a9771948SGleb Smirnoff 	tmp_counter += ntohl(ch->carp_counter[1]);
597a9771948SGleb Smirnoff 
598a9771948SGleb Smirnoff 	/* XXX Replay protection goes here */
599a9771948SGleb Smirnoff 
600a9771948SGleb Smirnoff 	sc->sc_init_counter = 0;
601a9771948SGleb Smirnoff 	sc->sc_counter = tmp_counter;
602a9771948SGleb Smirnoff 
603a9771948SGleb Smirnoff 	sc_tv.tv_sec = sc->sc_advbase;
604f08535f8SGleb Smirnoff 	sc_tv.tv_usec = DEMOTE_ADVSKEW(sc) * 1000000 / 256;
605a9771948SGleb Smirnoff 	ch_tv.tv_sec = ch->carp_advbase;
606a9771948SGleb Smirnoff 	ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
607a9771948SGleb Smirnoff 
608a9771948SGleb Smirnoff 	switch (sc->sc_state) {
609a9771948SGleb Smirnoff 	case INIT:
610a9771948SGleb Smirnoff 		break;
611a9771948SGleb Smirnoff 	case MASTER:
612a9771948SGleb Smirnoff 		/*
613a9771948SGleb Smirnoff 		 * If we receive an advertisement from a master who's going to
614a9771948SGleb Smirnoff 		 * be more frequent than us, go into BACKUP state.
615a9771948SGleb Smirnoff 		 */
616a9771948SGleb Smirnoff 		if (timevalcmp(&sc_tv, &ch_tv, >) ||
617a9771948SGleb Smirnoff 		    timevalcmp(&sc_tv, &ch_tv, ==)) {
618a9771948SGleb Smirnoff 			callout_stop(&sc->sc_ad_tmo);
61908b68b0eSGleb Smirnoff 			CARP_LOG("VHID %u@%s: MASTER -> BACKUP "
62088bf82a6SGleb Smirnoff 			    "(more frequent advertisement received)\n",
62108b68b0eSGleb Smirnoff 			    sc->sc_vhid,
62208b68b0eSGleb Smirnoff 			    sc->sc_carpdev->if_xname);
623a9771948SGleb Smirnoff 			carp_set_state(sc, BACKUP);
624a9771948SGleb Smirnoff 			carp_setrun(sc, 0);
62508b68b0eSGleb Smirnoff 			carp_delroute(sc);
626a9771948SGleb Smirnoff 		}
627a9771948SGleb Smirnoff 		break;
628a9771948SGleb Smirnoff 	case BACKUP:
629a9771948SGleb Smirnoff 		/*
630a9771948SGleb Smirnoff 		 * If we're pre-empting masters who advertise slower than us,
631a9771948SGleb Smirnoff 		 * and this one claims to be slower, treat him as down.
632a9771948SGleb Smirnoff 		 */
633f08535f8SGleb Smirnoff 		if (carp_preempt && timevalcmp(&sc_tv, &ch_tv, <)) {
63408b68b0eSGleb Smirnoff 			CARP_LOG("VHID %u@%s: BACKUP -> MASTER "
63588bf82a6SGleb Smirnoff 			    "(preempting a slower master)\n",
63608b68b0eSGleb Smirnoff 			    sc->sc_vhid,
63708b68b0eSGleb Smirnoff 			    sc->sc_carpdev->if_xname);
638d220759bSGleb Smirnoff 			carp_master_down_locked(sc);
639a9771948SGleb Smirnoff 			break;
640a9771948SGleb Smirnoff 		}
641a9771948SGleb Smirnoff 
642a9771948SGleb Smirnoff 		/*
643a9771948SGleb Smirnoff 		 *  If the master is going to advertise at such a low frequency
644a9771948SGleb Smirnoff 		 *  that he's guaranteed to time out, we'd might as well just
645a9771948SGleb Smirnoff 		 *  treat him as timed out now.
646a9771948SGleb Smirnoff 		 */
647a9771948SGleb Smirnoff 		sc_tv.tv_sec = sc->sc_advbase * 3;
648a9771948SGleb Smirnoff 		if (timevalcmp(&sc_tv, &ch_tv, <)) {
64908b68b0eSGleb Smirnoff 			CARP_LOG("VHID %u@%s: BACKUP -> MASTER "
65088bf82a6SGleb Smirnoff 			    "(master timed out)\n",
65108b68b0eSGleb Smirnoff 			    sc->sc_vhid,
65208b68b0eSGleb Smirnoff 			    sc->sc_carpdev->if_xname);
653d220759bSGleb Smirnoff 			carp_master_down_locked(sc);
654a9771948SGleb Smirnoff 			break;
655a9771948SGleb Smirnoff 		}
656a9771948SGleb Smirnoff 
657a9771948SGleb Smirnoff 		/*
658a9771948SGleb Smirnoff 		 * Otherwise, we reset the counter and wait for the next
659a9771948SGleb Smirnoff 		 * advertisement.
660a9771948SGleb Smirnoff 		 */
661a9771948SGleb Smirnoff 		carp_setrun(sc, af);
662a9771948SGleb Smirnoff 		break;
663a9771948SGleb Smirnoff 	}
664a9771948SGleb Smirnoff 
66508b68b0eSGleb Smirnoff out:
66608b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
667a9771948SGleb Smirnoff 	m_freem(m);
668a9771948SGleb Smirnoff }
669a9771948SGleb Smirnoff 
6705c1f0f6dSGleb Smirnoff static int
671a9771948SGleb Smirnoff carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
672a9771948SGleb Smirnoff {
673a9771948SGleb Smirnoff 	struct m_tag *mtag;
674a9771948SGleb Smirnoff 
675a9771948SGleb Smirnoff 	if (sc->sc_init_counter) {
676a9771948SGleb Smirnoff 		/* this could also be seconds since unix epoch */
677a9771948SGleb Smirnoff 		sc->sc_counter = arc4random();
678a9771948SGleb Smirnoff 		sc->sc_counter = sc->sc_counter << 32;
679a9771948SGleb Smirnoff 		sc->sc_counter += arc4random();
680a9771948SGleb Smirnoff 	} else
681a9771948SGleb Smirnoff 		sc->sc_counter++;
682a9771948SGleb Smirnoff 
683a9771948SGleb Smirnoff 	ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
684a9771948SGleb Smirnoff 	ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
685a9771948SGleb Smirnoff 
686a9771948SGleb Smirnoff 	carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
687a9771948SGleb Smirnoff 
688a9771948SGleb Smirnoff 	/* Tag packet for carp_output */
68908b68b0eSGleb Smirnoff 	if ((mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct carp_softc *),
69008b68b0eSGleb Smirnoff 	    M_NOWAIT)) == NULL) {
691a9771948SGleb Smirnoff 		m_freem(m);
69208b68b0eSGleb Smirnoff 		CARPSTATS_INC(carps_onomem);
693a9771948SGleb Smirnoff 		return (ENOMEM);
694a9771948SGleb Smirnoff 	}
695eaf151c4SGleb Smirnoff 	bcopy(&sc, mtag + 1, sizeof(sc));
696a9771948SGleb Smirnoff 	m_tag_prepend(m, mtag);
697a9771948SGleb Smirnoff 
698a9771948SGleb Smirnoff 	return (0);
699a9771948SGleb Smirnoff }
700a9771948SGleb Smirnoff 
701f08535f8SGleb Smirnoff /*
702f08535f8SGleb Smirnoff  * To avoid LORs and possible recursions this function shouldn't
703f08535f8SGleb Smirnoff  * be called directly, but scheduled via taskqueue.
704f08535f8SGleb Smirnoff  */
7055c1f0f6dSGleb Smirnoff static void
706f08535f8SGleb Smirnoff carp_send_ad_all(void *ctx __unused, int pending __unused)
707a9771948SGleb Smirnoff {
708d92d54d5SGleb Smirnoff 	struct carp_softc *sc;
709a9771948SGleb Smirnoff 
710d92d54d5SGleb Smirnoff 	mtx_lock(&carp_mtx);
71108b68b0eSGleb Smirnoff 	LIST_FOREACH(sc, &carp_list, sc_next)
712f08535f8SGleb Smirnoff 		if (sc->sc_state == MASTER) {
71308b68b0eSGleb Smirnoff 			CARP_LOCK(sc);
714afdbac98SGleb Smirnoff 			CURVNET_SET(sc->sc_carpdev->if_vnet);
715d220759bSGleb Smirnoff 			carp_send_ad_locked(sc);
716afdbac98SGleb Smirnoff 			CURVNET_RESTORE();
71708b68b0eSGleb Smirnoff 			CARP_UNLOCK(sc);
718a9771948SGleb Smirnoff 		}
719d92d54d5SGleb Smirnoff 	mtx_unlock(&carp_mtx);
720a9771948SGleb Smirnoff }
721a9771948SGleb Smirnoff 
722afdbac98SGleb Smirnoff /* Send a periodic advertisement, executed in callout context. */
7235c1f0f6dSGleb Smirnoff static void
724a9771948SGleb Smirnoff carp_send_ad(void *v)
725a9771948SGleb Smirnoff {
726d220759bSGleb Smirnoff 	struct carp_softc *sc = v;
727d220759bSGleb Smirnoff 
72808b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
729afdbac98SGleb Smirnoff 	CURVNET_SET(sc->sc_carpdev->if_vnet);
730d220759bSGleb Smirnoff 	carp_send_ad_locked(sc);
731afdbac98SGleb Smirnoff 	CURVNET_RESTORE();
73208b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
733d220759bSGleb Smirnoff }
734d220759bSGleb Smirnoff 
735d220759bSGleb Smirnoff static void
736d220759bSGleb Smirnoff carp_send_ad_locked(struct carp_softc *sc)
737d220759bSGleb Smirnoff {
738a9771948SGleb Smirnoff 	struct carp_header ch;
739a9771948SGleb Smirnoff 	struct timeval tv;
74008b68b0eSGleb Smirnoff 	struct sockaddr sa;
74108b68b0eSGleb Smirnoff 	struct ifaddr *ifa;
742a9771948SGleb Smirnoff 	struct carp_header *ch_ptr;
743a9771948SGleb Smirnoff 	struct mbuf *m;
74408b68b0eSGleb Smirnoff 	int len, advskew;
745a9771948SGleb Smirnoff 
74608b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
747d220759bSGleb Smirnoff 
748f08535f8SGleb Smirnoff 	advskew = DEMOTE_ADVSKEW(sc);
74908b68b0eSGleb Smirnoff 	tv.tv_sec = sc->sc_advbase;
750a9771948SGleb Smirnoff 	tv.tv_usec = advskew * 1000000 / 256;
751a9771948SGleb Smirnoff 
752a9771948SGleb Smirnoff 	ch.carp_version = CARP_VERSION;
753a9771948SGleb Smirnoff 	ch.carp_type = CARP_ADVERTISEMENT;
754a9771948SGleb Smirnoff 	ch.carp_vhid = sc->sc_vhid;
75508b68b0eSGleb Smirnoff 	ch.carp_advbase = sc->sc_advbase;
756a9771948SGleb Smirnoff 	ch.carp_advskew = advskew;
757a9771948SGleb Smirnoff 	ch.carp_authlen = 7;	/* XXX DEFINE */
758a9771948SGleb Smirnoff 	ch.carp_pad1 = 0;	/* must be zero */
759a9771948SGleb Smirnoff 	ch.carp_cksum = 0;
760a9771948SGleb Smirnoff 
76108b68b0eSGleb Smirnoff 	/* XXXGL: OpenBSD picks first ifaddr with needed family. */
76208b68b0eSGleb Smirnoff 
763a9771948SGleb Smirnoff #ifdef INET
76408b68b0eSGleb Smirnoff 	if (sc->sc_naddrs) {
765a9771948SGleb Smirnoff 		struct ip *ip;
766a9771948SGleb Smirnoff 
76708b68b0eSGleb Smirnoff 		MGETHDR(m, M_NOWAIT, MT_HEADER);
768a9771948SGleb Smirnoff 		if (m == NULL) {
7696bf65bcfSRobert Watson 			CARPSTATS_INC(carps_onomem);
770891122d1SGleb Smirnoff 			goto resched;
771a9771948SGleb Smirnoff 		}
772a9771948SGleb Smirnoff 		len = sizeof(*ip) + sizeof(ch);
773a9771948SGleb Smirnoff 		m->m_pkthdr.len = len;
774a9771948SGleb Smirnoff 		m->m_pkthdr.rcvif = NULL;
775a9771948SGleb Smirnoff 		m->m_len = len;
776a9771948SGleb Smirnoff 		MH_ALIGN(m, m->m_len);
777a9771948SGleb Smirnoff 		m->m_flags |= M_MCAST;
778a9771948SGleb Smirnoff 		ip = mtod(m, struct ip *);
779a9771948SGleb Smirnoff 		ip->ip_v = IPVERSION;
780a9771948SGleb Smirnoff 		ip->ip_hl = sizeof(*ip) >> 2;
781a9771948SGleb Smirnoff 		ip->ip_tos = IPTOS_LOWDELAY;
7828f134647SGleb Smirnoff 		ip->ip_len = htons(len);
783a9771948SGleb Smirnoff 		ip->ip_id = ip_newid();
7848f134647SGleb Smirnoff 		ip->ip_off = htons(IP_DF);
785a9771948SGleb Smirnoff 		ip->ip_ttl = CARP_DFLTTL;
786a9771948SGleb Smirnoff 		ip->ip_p = IPPROTO_CARP;
787a9771948SGleb Smirnoff 		ip->ip_sum = 0;
78808b68b0eSGleb Smirnoff 
78908b68b0eSGleb Smirnoff 		bzero(&sa, sizeof(sa));
79008b68b0eSGleb Smirnoff 		sa.sa_family = AF_INET;
79108b68b0eSGleb Smirnoff 		ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
79208b68b0eSGleb Smirnoff 		if (ifa != NULL) {
79308b68b0eSGleb Smirnoff 			ip->ip_src.s_addr =
79408b68b0eSGleb Smirnoff 			    ifatoia(ifa)->ia_addr.sin_addr.s_addr;
79508b68b0eSGleb Smirnoff 			ifa_free(ifa);
79608b68b0eSGleb Smirnoff 		} else
79708b68b0eSGleb Smirnoff 			ip->ip_src.s_addr = 0;
798a9771948SGleb Smirnoff 		ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
799a9771948SGleb Smirnoff 
800a9771948SGleb Smirnoff 		ch_ptr = (struct carp_header *)(&ip[1]);
801a9771948SGleb Smirnoff 		bcopy(&ch, ch_ptr, sizeof(ch));
802a9771948SGleb Smirnoff 		if (carp_prepare_ad(m, sc, ch_ptr))
803891122d1SGleb Smirnoff 			goto resched;
804a9771948SGleb Smirnoff 
805a9771948SGleb Smirnoff 		m->m_data += sizeof(*ip);
806*c4d06976SGleb Smirnoff 		ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip));
807a9771948SGleb Smirnoff 		m->m_data -= sizeof(*ip);
808a9771948SGleb Smirnoff 
8096bf65bcfSRobert Watson 		CARPSTATS_INC(carps_opackets);
810a9771948SGleb Smirnoff 
81108b68b0eSGleb Smirnoff 		if (ip_output(m, NULL, NULL, IP_RAWOUTPUT,
81208b68b0eSGleb Smirnoff 		    &sc->sc_carpdev->if_carp->cif_imo, NULL)) {
813a9771948SGleb Smirnoff 			if (sc->sc_sendad_errors < INT_MAX)
814a9771948SGleb Smirnoff 				sc->sc_sendad_errors++;
815f08535f8SGleb Smirnoff 			if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS)
816f08535f8SGleb Smirnoff 				carp_demote_adj(carp_senderr_adj, "send error");
817a9771948SGleb Smirnoff 			sc->sc_sendad_success = 0;
818a9771948SGleb Smirnoff 		} else {
819a9771948SGleb Smirnoff 			if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
820a9771948SGleb Smirnoff 				if (++sc->sc_sendad_success >=
821a9771948SGleb Smirnoff 				    CARP_SENDAD_MIN_SUCCESS) {
822f08535f8SGleb Smirnoff 					carp_demote_adj(-carp_senderr_adj,
823f08535f8SGleb Smirnoff 					    "send ok");
824a9771948SGleb Smirnoff 					sc->sc_sendad_errors = 0;
825a9771948SGleb Smirnoff 				}
826a9771948SGleb Smirnoff 			} else
827a9771948SGleb Smirnoff 				sc->sc_sendad_errors = 0;
828a9771948SGleb Smirnoff 		}
829a9771948SGleb Smirnoff 	}
830a9771948SGleb Smirnoff #endif /* INET */
831a9771948SGleb Smirnoff #ifdef INET6
83208b68b0eSGleb Smirnoff 	if (sc->sc_naddrs6) {
833a9771948SGleb Smirnoff 		struct ip6_hdr *ip6;
834a9771948SGleb Smirnoff 
83508b68b0eSGleb Smirnoff 		MGETHDR(m, M_NOWAIT, MT_HEADER);
836a9771948SGleb Smirnoff 		if (m == NULL) {
8376bf65bcfSRobert Watson 			CARPSTATS_INC(carps_onomem);
838891122d1SGleb Smirnoff 			goto resched;
839a9771948SGleb Smirnoff 		}
840a9771948SGleb Smirnoff 		len = sizeof(*ip6) + sizeof(ch);
841a9771948SGleb Smirnoff 		m->m_pkthdr.len = len;
842a9771948SGleb Smirnoff 		m->m_pkthdr.rcvif = NULL;
843a9771948SGleb Smirnoff 		m->m_len = len;
844a9771948SGleb Smirnoff 		MH_ALIGN(m, m->m_len);
845a9771948SGleb Smirnoff 		m->m_flags |= M_MCAST;
846a9771948SGleb Smirnoff 		ip6 = mtod(m, struct ip6_hdr *);
847a9771948SGleb Smirnoff 		bzero(ip6, sizeof(*ip6));
848a9771948SGleb Smirnoff 		ip6->ip6_vfc |= IPV6_VERSION;
849a9771948SGleb Smirnoff 		ip6->ip6_hlim = CARP_DFLTTL;
850a9771948SGleb Smirnoff 		ip6->ip6_nxt = IPPROTO_CARP;
85108b68b0eSGleb Smirnoff 		bzero(&sa, sizeof(sa));
852a9771948SGleb Smirnoff 
85308b68b0eSGleb Smirnoff 		/* set the source address */
85408b68b0eSGleb Smirnoff 		sa.sa_family = AF_INET6;
85508b68b0eSGleb Smirnoff 		ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
85608b68b0eSGleb Smirnoff 		if (ifa != NULL) {
85708b68b0eSGleb Smirnoff 			bcopy(IFA_IN6(ifa), &ip6->ip6_src,
85808b68b0eSGleb Smirnoff 			    sizeof(struct in6_addr));
85908b68b0eSGleb Smirnoff 			ifa_free(ifa);
86008b68b0eSGleb Smirnoff 		} else
86108b68b0eSGleb Smirnoff 			/* This should never happen with IPv6. */
86208b68b0eSGleb Smirnoff 			bzero(&ip6->ip6_src, sizeof(struct in6_addr));
86308b68b0eSGleb Smirnoff 
86408b68b0eSGleb Smirnoff 		/* Set the multicast destination. */
8657002145dSBjoern A. Zeeb 		ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
866a9771948SGleb Smirnoff 		ip6->ip6_dst.s6_addr8[15] = 0x12;
8677002145dSBjoern A. Zeeb 		if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
8687002145dSBjoern A. Zeeb 			m_freem(m);
869e81ab876SGleb Smirnoff 			CARP_DEBUG("%s: in6_setscope failed\n", __func__);
870891122d1SGleb Smirnoff 			goto resched;
8717002145dSBjoern A. Zeeb 		}
872a9771948SGleb Smirnoff 
873a9771948SGleb Smirnoff 		ch_ptr = (struct carp_header *)(&ip6[1]);
874a9771948SGleb Smirnoff 		bcopy(&ch, ch_ptr, sizeof(ch));
875a9771948SGleb Smirnoff 		if (carp_prepare_ad(m, sc, ch_ptr))
876891122d1SGleb Smirnoff 			goto resched;
877a9771948SGleb Smirnoff 
878a9771948SGleb Smirnoff 		m->m_data += sizeof(*ip6);
879*c4d06976SGleb Smirnoff 		ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip6));
880a9771948SGleb Smirnoff 		m->m_data -= sizeof(*ip6);
881a9771948SGleb Smirnoff 
8826bf65bcfSRobert Watson 		CARPSTATS_INC(carps_opackets6);
883a9771948SGleb Smirnoff 
88408b68b0eSGleb Smirnoff 		if (ip6_output(m, NULL, NULL, 0,
88508b68b0eSGleb Smirnoff 		    &sc->sc_carpdev->if_carp->cif_im6o, NULL, NULL)) {
886a9771948SGleb Smirnoff 			if (sc->sc_sendad_errors < INT_MAX)
887a9771948SGleb Smirnoff 				sc->sc_sendad_errors++;
888f08535f8SGleb Smirnoff 			if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS)
889f08535f8SGleb Smirnoff 				carp_demote_adj(carp_senderr_adj,
890f08535f8SGleb Smirnoff 				    "send6 error");
891a9771948SGleb Smirnoff 			sc->sc_sendad_success = 0;
892a9771948SGleb Smirnoff 		} else {
893a9771948SGleb Smirnoff 			if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
894a9771948SGleb Smirnoff 				if (++sc->sc_sendad_success >=
895a9771948SGleb Smirnoff 				    CARP_SENDAD_MIN_SUCCESS) {
896f08535f8SGleb Smirnoff 					carp_demote_adj(-carp_senderr_adj,
897f08535f8SGleb Smirnoff 					    "send6 ok");
898a9771948SGleb Smirnoff 					sc->sc_sendad_errors = 0;
899a9771948SGleb Smirnoff 				}
900a9771948SGleb Smirnoff 			} else
901a9771948SGleb Smirnoff 				sc->sc_sendad_errors = 0;
902a9771948SGleb Smirnoff 		}
903a9771948SGleb Smirnoff 	}
904a9771948SGleb Smirnoff #endif /* INET6 */
905a9771948SGleb Smirnoff 
906891122d1SGleb Smirnoff resched:
90708b68b0eSGleb Smirnoff 	callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), carp_send_ad, sc);
90808b68b0eSGleb Smirnoff }
909a9771948SGleb Smirnoff 
91008b68b0eSGleb Smirnoff static void
91108b68b0eSGleb Smirnoff carp_addroute(struct carp_softc *sc)
91208b68b0eSGleb Smirnoff {
91308b68b0eSGleb Smirnoff 	struct ifaddr *ifa;
91408b68b0eSGleb Smirnoff 
91508b68b0eSGleb Smirnoff 	CARP_FOREACH_IFA(sc, ifa)
9162512b096SGleb Smirnoff 		carp_ifa_addroute(ifa);
9172512b096SGleb Smirnoff }
9182512b096SGleb Smirnoff 
9192512b096SGleb Smirnoff static void
9202512b096SGleb Smirnoff carp_ifa_addroute(struct ifaddr *ifa)
9212512b096SGleb Smirnoff {
9222512b096SGleb Smirnoff 
92308b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
92408b68b0eSGleb Smirnoff #ifdef INET
92508b68b0eSGleb Smirnoff 	case AF_INET:
92608b68b0eSGleb Smirnoff 		in_addprefix(ifatoia(ifa), RTF_UP);
92708b68b0eSGleb Smirnoff 		ifa_add_loopback_route(ifa,
92808b68b0eSGleb Smirnoff 		    (struct sockaddr *)&ifatoia(ifa)->ia_addr);
92908b68b0eSGleb Smirnoff 		break;
93008b68b0eSGleb Smirnoff #endif
93108b68b0eSGleb Smirnoff #ifdef INET6
93208b68b0eSGleb Smirnoff 	case AF_INET6:
93308b68b0eSGleb Smirnoff 		ifa_add_loopback_route(ifa,
93408b68b0eSGleb Smirnoff 		    (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
93508b68b0eSGleb Smirnoff 		in6_ifaddloop(ifa);
93608b68b0eSGleb Smirnoff 		break;
93708b68b0eSGleb Smirnoff #endif
93808b68b0eSGleb Smirnoff 	}
93908b68b0eSGleb Smirnoff }
94008b68b0eSGleb Smirnoff 
94108b68b0eSGleb Smirnoff static void
94208b68b0eSGleb Smirnoff carp_delroute(struct carp_softc *sc)
94308b68b0eSGleb Smirnoff {
94408b68b0eSGleb Smirnoff 	struct ifaddr *ifa;
94508b68b0eSGleb Smirnoff 
94608b68b0eSGleb Smirnoff 	CARP_FOREACH_IFA(sc, ifa)
9472512b096SGleb Smirnoff 		carp_ifa_delroute(ifa);
9482512b096SGleb Smirnoff }
9492512b096SGleb Smirnoff 
9502512b096SGleb Smirnoff static void
9512512b096SGleb Smirnoff carp_ifa_delroute(struct ifaddr *ifa)
9522512b096SGleb Smirnoff {
9532512b096SGleb Smirnoff 
95408b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
95508b68b0eSGleb Smirnoff #ifdef INET
95608b68b0eSGleb Smirnoff 	case AF_INET:
95708b68b0eSGleb Smirnoff 		ifa_del_loopback_route(ifa,
95808b68b0eSGleb Smirnoff 		    (struct sockaddr *)&ifatoia(ifa)->ia_addr);
95908b68b0eSGleb Smirnoff 		in_scrubprefix(ifatoia(ifa), LLE_STATIC);
96008b68b0eSGleb Smirnoff 		break;
96108b68b0eSGleb Smirnoff #endif
96208b68b0eSGleb Smirnoff #ifdef INET6
96308b68b0eSGleb Smirnoff 	case AF_INET6:
96408b68b0eSGleb Smirnoff 		ifa_del_loopback_route(ifa,
96508b68b0eSGleb Smirnoff 		    (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
96608b68b0eSGleb Smirnoff 		in6_ifremloop(ifa);
96708b68b0eSGleb Smirnoff 		break;
96808b68b0eSGleb Smirnoff #endif
96908b68b0eSGleb Smirnoff 	}
970a9771948SGleb Smirnoff }
971a9771948SGleb Smirnoff 
972a0ae8f04SBjoern A. Zeeb #ifdef INET
973a9771948SGleb Smirnoff /*
974a9771948SGleb Smirnoff  * Broadcast a gratuitous ARP request containing
975a9771948SGleb Smirnoff  * the virtual router MAC address for each IP address
976a9771948SGleb Smirnoff  * associated with the virtual router.
977a9771948SGleb Smirnoff  */
9785c1f0f6dSGleb Smirnoff static void
979a9771948SGleb Smirnoff carp_send_arp(struct carp_softc *sc)
980a9771948SGleb Smirnoff {
981a9771948SGleb Smirnoff 	struct ifaddr *ifa;
982a9771948SGleb Smirnoff 
98308b68b0eSGleb Smirnoff 	CARP_FOREACH_IFA(sc, ifa)
98408b68b0eSGleb Smirnoff 		if (ifa->ifa_addr->sa_family == AF_INET)
98508b68b0eSGleb Smirnoff 			arp_ifinit2(sc->sc_carpdev, ifa, LLADDR(&sc->sc_addr));
986a9771948SGleb Smirnoff }
98708b68b0eSGleb Smirnoff 
98808b68b0eSGleb Smirnoff int
98908b68b0eSGleb Smirnoff carp_iamatch(struct ifaddr *ifa, uint8_t **enaddr)
99008b68b0eSGleb Smirnoff {
99108b68b0eSGleb Smirnoff 	struct carp_softc *sc = ifa->ifa_carp;
99208b68b0eSGleb Smirnoff 
99308b68b0eSGleb Smirnoff 	if (sc->sc_state == MASTER) {
99408b68b0eSGleb Smirnoff 		*enaddr = LLADDR(&sc->sc_addr);
99508b68b0eSGleb Smirnoff 		return (1);
99608b68b0eSGleb Smirnoff 	}
99708b68b0eSGleb Smirnoff 
99808b68b0eSGleb Smirnoff 	return (0);
999a9771948SGleb Smirnoff }
1000a0ae8f04SBjoern A. Zeeb #endif
1001a9771948SGleb Smirnoff 
1002a9771948SGleb Smirnoff #ifdef INET6
10035c1f0f6dSGleb Smirnoff static void
1004a9771948SGleb Smirnoff carp_send_na(struct carp_softc *sc)
1005a9771948SGleb Smirnoff {
100608b68b0eSGleb Smirnoff 	static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1007a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1008a9771948SGleb Smirnoff 	struct in6_addr *in6;
1009a9771948SGleb Smirnoff 
101008b68b0eSGleb Smirnoff 	CARP_FOREACH_IFA(sc, ifa) {
1011a9771948SGleb Smirnoff 		if (ifa->ifa_addr->sa_family != AF_INET6)
1012a9771948SGleb Smirnoff 			continue;
1013a9771948SGleb Smirnoff 
101408b68b0eSGleb Smirnoff 		in6 = IFA_IN6(ifa);
1015e8c34a71SGleb Smirnoff 		nd6_na_output(sc->sc_carpdev, &mcast, in6,
1016a9771948SGleb Smirnoff 		    ND_NA_FLAG_OVERRIDE, 1, NULL);
1017a9771948SGleb Smirnoff 		DELAY(1000);	/* XXX */
1018a9771948SGleb Smirnoff 	}
1019a9771948SGleb Smirnoff }
1020a9771948SGleb Smirnoff 
10218253dcabSBjoern A. Zeeb /*
10228253dcabSBjoern A. Zeeb  * Returns ifa in case it's a carp address and it is MASTER, or if the address
10238253dcabSBjoern A. Zeeb  * matches and is not a carp address.  Returns NULL otherwise.
10248253dcabSBjoern A. Zeeb  */
1025a4e53905SMax Laier struct ifaddr *
102654bfbd51SWill Andrews carp_iamatch6(struct ifnet *ifp, struct in6_addr *taddr)
1027a9771948SGleb Smirnoff {
1028a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1029a9771948SGleb Smirnoff 
10308253dcabSBjoern A. Zeeb 	ifa = NULL;
1031137f91e8SJohn Baldwin 	IF_ADDR_RLOCK(ifp);
10328253dcabSBjoern A. Zeeb 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
10338253dcabSBjoern A. Zeeb 		if (ifa->ifa_addr->sa_family != AF_INET6)
10348253dcabSBjoern A. Zeeb 			continue;
10358253dcabSBjoern A. Zeeb 		if (!IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa)))
10368253dcabSBjoern A. Zeeb 			continue;
10378253dcabSBjoern A. Zeeb 		if (ifa->ifa_carp && ifa->ifa_carp->sc_state != MASTER)
10388253dcabSBjoern A. Zeeb 			ifa = NULL;
10398253dcabSBjoern A. Zeeb 		else
10408c0fec80SRobert Watson 			ifa_ref(ifa);
10418253dcabSBjoern A. Zeeb 		break;
1042a9771948SGleb Smirnoff 	}
1043137f91e8SJohn Baldwin 	IF_ADDR_RUNLOCK(ifp);
1044a9771948SGleb Smirnoff 
10458253dcabSBjoern A. Zeeb 	return (ifa);
1046a9771948SGleb Smirnoff }
1047a9771948SGleb Smirnoff 
104854bfbd51SWill Andrews caddr_t
104954bfbd51SWill Andrews carp_macmatch6(struct ifnet *ifp, struct mbuf *m, const struct in6_addr *taddr)
1050a9771948SGleb Smirnoff {
1051a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1052a9771948SGleb Smirnoff 
1053137f91e8SJohn Baldwin 	IF_ADDR_RLOCK(ifp);
105408b68b0eSGleb Smirnoff 	IFNET_FOREACH_IFA(ifp, ifa)
105508b68b0eSGleb Smirnoff 		if (ifa->ifa_addr->sa_family == AF_INET6 &&
105608b68b0eSGleb Smirnoff 		    IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) {
105708b68b0eSGleb Smirnoff 			struct carp_softc *sc = ifa->ifa_carp;
105808b68b0eSGleb Smirnoff 			struct m_tag *mtag;
105908b68b0eSGleb Smirnoff 
1060137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
106108b68b0eSGleb Smirnoff 
1062a9771948SGleb Smirnoff 			mtag = m_tag_get(PACKET_TAG_CARP,
1063a856ddc6SGleb Smirnoff 			    sizeof(struct carp_softc *), M_NOWAIT);
106408b68b0eSGleb Smirnoff 			if (mtag == NULL)
106508b68b0eSGleb Smirnoff 				/* Better a bit than nothing. */
106608b68b0eSGleb Smirnoff 				return (LLADDR(&sc->sc_addr));
106708b68b0eSGleb Smirnoff 
1068eaf151c4SGleb Smirnoff 			bcopy(&sc, mtag + 1, sizeof(sc));
1069a9771948SGleb Smirnoff 			m_tag_prepend(m, mtag);
1070a9771948SGleb Smirnoff 
107108b68b0eSGleb Smirnoff 			return (LLADDR(&sc->sc_addr));
1072a9771948SGleb Smirnoff 		}
1073137f91e8SJohn Baldwin 	IF_ADDR_RUNLOCK(ifp);
1074a9771948SGleb Smirnoff 
1075a9771948SGleb Smirnoff 	return (NULL);
1076a9771948SGleb Smirnoff }
107708b68b0eSGleb Smirnoff #endif /* INET6 */
1078a9771948SGleb Smirnoff 
107908b68b0eSGleb Smirnoff int
108054bfbd51SWill Andrews carp_forus(struct ifnet *ifp, u_char *dhost)
1081a9771948SGleb Smirnoff {
108208b68b0eSGleb Smirnoff 	struct carp_softc *sc;
108308b68b0eSGleb Smirnoff 	uint8_t *ena = dhost;
1084a9771948SGleb Smirnoff 
1085a9771948SGleb Smirnoff 	if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
108608b68b0eSGleb Smirnoff 		return (0);
1087a9771948SGleb Smirnoff 
108808b68b0eSGleb Smirnoff 	CIF_LOCK(ifp->if_carp);
108908b68b0eSGleb Smirnoff 	IFNET_FOREACH_CARP(ifp, sc) {
109008b68b0eSGleb Smirnoff 		CARP_LOCK(sc);
109108b68b0eSGleb Smirnoff 		if (sc->sc_state == MASTER && !bcmp(dhost, LLADDR(&sc->sc_addr),
109208b68b0eSGleb Smirnoff 		    ETHER_ADDR_LEN)) {
109308b68b0eSGleb Smirnoff 			CARP_UNLOCK(sc);
109408b68b0eSGleb Smirnoff 			CIF_UNLOCK(ifp->if_carp);
109508b68b0eSGleb Smirnoff 			return (1);
1096a9771948SGleb Smirnoff 		}
109708b68b0eSGleb Smirnoff 		CARP_UNLOCK(sc);
109808b68b0eSGleb Smirnoff 	}
109908b68b0eSGleb Smirnoff 	CIF_UNLOCK(ifp->if_carp);
1100a9771948SGleb Smirnoff 
110108b68b0eSGleb Smirnoff 	return (0);
1102a9771948SGleb Smirnoff }
1103a9771948SGleb Smirnoff 
1104afdbac98SGleb Smirnoff /* Master down timeout event, executed in callout context. */
11055c1f0f6dSGleb Smirnoff static void
1106a9771948SGleb Smirnoff carp_master_down(void *v)
1107a9771948SGleb Smirnoff {
1108a9771948SGleb Smirnoff 	struct carp_softc *sc = v;
1109a9771948SGleb Smirnoff 
111008b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
111108b68b0eSGleb Smirnoff 
1112afdbac98SGleb Smirnoff 	CURVNET_SET(sc->sc_carpdev->if_vnet);
111308b68b0eSGleb Smirnoff 	if (sc->sc_state == BACKUP) {
11141c435c73SGleb Smirnoff 		CARP_LOG("VHID %u@%s: BACKUP -> MASTER (master down)\n",
111508b68b0eSGleb Smirnoff 		    sc->sc_vhid,
111608b68b0eSGleb Smirnoff 		    sc->sc_carpdev->if_xname);
1117d220759bSGleb Smirnoff 		carp_master_down_locked(sc);
111808b68b0eSGleb Smirnoff 	}
1119afdbac98SGleb Smirnoff 	CURVNET_RESTORE();
112008b68b0eSGleb Smirnoff 
112108b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
1122d220759bSGleb Smirnoff }
1123d220759bSGleb Smirnoff 
1124d220759bSGleb Smirnoff static void
1125d220759bSGleb Smirnoff carp_master_down_locked(struct carp_softc *sc)
1126d220759bSGleb Smirnoff {
112708b68b0eSGleb Smirnoff 
112808b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
1129d220759bSGleb Smirnoff 
1130a9771948SGleb Smirnoff 	switch (sc->sc_state) {
1131a9771948SGleb Smirnoff 	case BACKUP:
1132a9771948SGleb Smirnoff 		carp_set_state(sc, MASTER);
1133d220759bSGleb Smirnoff 		carp_send_ad_locked(sc);
1134a0ae8f04SBjoern A. Zeeb #ifdef INET
1135a9771948SGleb Smirnoff 		carp_send_arp(sc);
1136a0ae8f04SBjoern A. Zeeb #endif
1137a9771948SGleb Smirnoff #ifdef INET6
1138a9771948SGleb Smirnoff 		carp_send_na(sc);
113908b68b0eSGleb Smirnoff #endif
1140a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
114108b68b0eSGleb Smirnoff 		carp_addroute(sc);
114208b68b0eSGleb Smirnoff 		break;
114308b68b0eSGleb Smirnoff 	case INIT:
114408b68b0eSGleb Smirnoff 	case MASTER:
114508b68b0eSGleb Smirnoff #ifdef INVARIANTS
114608b68b0eSGleb Smirnoff 		panic("carp: VHID %u@%s: master_down event in %s state\n",
114708b68b0eSGleb Smirnoff 		    sc->sc_vhid,
114808b68b0eSGleb Smirnoff 		    sc->sc_carpdev->if_xname,
114908b68b0eSGleb Smirnoff 		    sc->sc_state ? "MASTER" : "INIT");
115008b68b0eSGleb Smirnoff #endif
1151a9771948SGleb Smirnoff 		break;
1152a9771948SGleb Smirnoff 	}
1153a9771948SGleb Smirnoff }
1154a9771948SGleb Smirnoff 
1155a9771948SGleb Smirnoff /*
1156a9771948SGleb Smirnoff  * When in backup state, af indicates whether to reset the master down timer
1157a9771948SGleb Smirnoff  * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1158a9771948SGleb Smirnoff  */
11595c1f0f6dSGleb Smirnoff static void
1160a9771948SGleb Smirnoff carp_setrun(struct carp_softc *sc, sa_family_t af)
1161a9771948SGleb Smirnoff {
1162a9771948SGleb Smirnoff 	struct timeval tv;
1163a9771948SGleb Smirnoff 
116408b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
1165d220759bSGleb Smirnoff 
116608b68b0eSGleb Smirnoff 	if ((sc->sc_carpdev->if_flags & IFF_UP) == 0 ||
116708b68b0eSGleb Smirnoff 	    sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
116808b68b0eSGleb Smirnoff 	    (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0))
1169a9771948SGleb Smirnoff 		return;
1170a9771948SGleb Smirnoff 
1171a9771948SGleb Smirnoff 	switch (sc->sc_state) {
1172a9771948SGleb Smirnoff 	case INIT:
117308b68b0eSGleb Smirnoff 		CARP_LOG("VHID %u@%s: INIT -> BACKUP\n",
117408b68b0eSGleb Smirnoff 		    sc->sc_vhid,
117508b68b0eSGleb Smirnoff 		    sc->sc_carpdev->if_xname);
1176a9771948SGleb Smirnoff 		carp_set_state(sc, BACKUP);
1177a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
1178a9771948SGleb Smirnoff 		break;
1179a9771948SGleb Smirnoff 	case BACKUP:
1180a9771948SGleb Smirnoff 		callout_stop(&sc->sc_ad_tmo);
1181a9771948SGleb Smirnoff 		tv.tv_sec = 3 * sc->sc_advbase;
1182a9771948SGleb Smirnoff 		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1183a9771948SGleb Smirnoff 		switch (af) {
1184a9771948SGleb Smirnoff #ifdef INET
1185a9771948SGleb Smirnoff 		case AF_INET:
1186a9771948SGleb Smirnoff 			callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1187a9771948SGleb Smirnoff 			    carp_master_down, sc);
1188a9771948SGleb Smirnoff 			break;
118908b68b0eSGleb Smirnoff #endif
1190a9771948SGleb Smirnoff #ifdef INET6
1191a9771948SGleb Smirnoff 		case AF_INET6:
1192a9771948SGleb Smirnoff 			callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1193a9771948SGleb Smirnoff 			    carp_master_down, sc);
1194a9771948SGleb Smirnoff 			break;
119508b68b0eSGleb Smirnoff #endif
1196a9771948SGleb Smirnoff 		default:
119708b68b0eSGleb Smirnoff #ifdef INET
1198a9771948SGleb Smirnoff 			if (sc->sc_naddrs)
1199a9771948SGleb Smirnoff 				callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1200a9771948SGleb Smirnoff 				    carp_master_down, sc);
120108b68b0eSGleb Smirnoff #endif
120208b68b0eSGleb Smirnoff #ifdef INET6
1203a9771948SGleb Smirnoff 			if (sc->sc_naddrs6)
1204a9771948SGleb Smirnoff 				callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1205a9771948SGleb Smirnoff 				    carp_master_down, sc);
120608b68b0eSGleb Smirnoff #endif
1207a9771948SGleb Smirnoff 			break;
1208a9771948SGleb Smirnoff 		}
1209a9771948SGleb Smirnoff 		break;
1210a9771948SGleb Smirnoff 	case MASTER:
1211a9771948SGleb Smirnoff 		tv.tv_sec = sc->sc_advbase;
1212a9771948SGleb Smirnoff 		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1213a9771948SGleb Smirnoff 		callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1214a9771948SGleb Smirnoff 		    carp_send_ad, sc);
1215a9771948SGleb Smirnoff 		break;
1216a9771948SGleb Smirnoff 	}
1217a9771948SGleb Smirnoff }
1218a9771948SGleb Smirnoff 
121908b68b0eSGleb Smirnoff /*
122008b68b0eSGleb Smirnoff  * Setup multicast structures.
122108b68b0eSGleb Smirnoff  */
12225c1f0f6dSGleb Smirnoff static int
1223a9a2c40cSGleb Smirnoff carp_multicast_setup(struct carp_if *cif, sa_family_t sa)
1224a9771948SGleb Smirnoff {
1225a9a2c40cSGleb Smirnoff 	struct ifnet *ifp = cif->cif_ifp;
122608b68b0eSGleb Smirnoff 	int error = 0;
122708b68b0eSGleb Smirnoff 
1228a9a2c40cSGleb Smirnoff 	CIF_LOCK_ASSERT(cif);
1229a9a2c40cSGleb Smirnoff 
123008b68b0eSGleb Smirnoff 	switch (sa) {
123108b68b0eSGleb Smirnoff #ifdef INET
123208b68b0eSGleb Smirnoff 	case AF_INET:
123308b68b0eSGleb Smirnoff 	    {
123408b68b0eSGleb Smirnoff 		struct ip_moptions *imo = &cif->cif_imo;
1235a9771948SGleb Smirnoff 		struct in_addr addr;
1236a9771948SGleb Smirnoff 
123708b68b0eSGleb Smirnoff 		if (imo->imo_membership)
1238a9771948SGleb Smirnoff 			return (0);
1239a9771948SGleb Smirnoff 
124008b68b0eSGleb Smirnoff 		imo->imo_membership = (struct in_multi **)malloc(
124108b68b0eSGleb Smirnoff 		    (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_CARP,
1242a9a2c40cSGleb Smirnoff 		    M_NOWAIT);
1243a9a2c40cSGleb Smirnoff 		if (imo->imo_membership == NULL)
1244a9a2c40cSGleb Smirnoff 			return (ENOMEM);
124508b68b0eSGleb Smirnoff 		imo->imo_mfilters = NULL;
124608b68b0eSGleb Smirnoff 		imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
124708b68b0eSGleb Smirnoff 		imo->imo_multicast_vif = -1;
1248a9771948SGleb Smirnoff 
1249a9771948SGleb Smirnoff 		addr.s_addr = htonl(INADDR_CARP_GROUP);
125008b68b0eSGleb Smirnoff 		if ((error = in_joingroup(ifp, &addr, NULL,
125108b68b0eSGleb Smirnoff 		    &imo->imo_membership[0])) != 0) {
125208b68b0eSGleb Smirnoff 			free(imo->imo_membership, M_CARP);
125308b68b0eSGleb Smirnoff 			break;
12542d9cfabaSRobert Watson 		}
1255a9771948SGleb Smirnoff 		imo->imo_num_memberships++;
1256a9771948SGleb Smirnoff 		imo->imo_multicast_ifp = ifp;
1257a9771948SGleb Smirnoff 		imo->imo_multicast_ttl = CARP_DFLTTL;
1258a9771948SGleb Smirnoff 		imo->imo_multicast_loop = 0;
1259a9771948SGleb Smirnoff 		break;
1260a9771948SGleb Smirnoff 	   }
126108b68b0eSGleb Smirnoff #endif
126208b68b0eSGleb Smirnoff #ifdef INET6
126308b68b0eSGleb Smirnoff 	case AF_INET6:
126408b68b0eSGleb Smirnoff 	    {
126508b68b0eSGleb Smirnoff 		struct ip6_moptions *im6o = &cif->cif_im6o;
126608b68b0eSGleb Smirnoff 		struct in6_addr in6;
126733cde130SBruce M Simpson 		struct in6_multi *in6m;
126833cde130SBruce M Simpson 
126908b68b0eSGleb Smirnoff 		if (im6o->im6o_membership)
127008b68b0eSGleb Smirnoff 			return (0);
127108b68b0eSGleb Smirnoff 
127208b68b0eSGleb Smirnoff 		im6o->im6o_membership = (struct in6_multi **)malloc(
127308b68b0eSGleb Smirnoff 		    (sizeof(struct in6_multi *) * IPV6_MIN_MEMBERSHIPS), M_CARP,
1274a9a2c40cSGleb Smirnoff 		    M_ZERO | M_NOWAIT);
1275a9a2c40cSGleb Smirnoff 		if (im6o->im6o_membership == NULL)
1276a9a2c40cSGleb Smirnoff 			return (ENOMEM);
127708b68b0eSGleb Smirnoff 		im6o->im6o_mfilters = NULL;
127808b68b0eSGleb Smirnoff 		im6o->im6o_max_memberships = IPV6_MIN_MEMBERSHIPS;
127908b68b0eSGleb Smirnoff 		im6o->im6o_multicast_hlim = CARP_DFLTTL;
1280a9771948SGleb Smirnoff 		im6o->im6o_multicast_ifp = ifp;
1281a9771948SGleb Smirnoff 
128208b68b0eSGleb Smirnoff 		/* Join IPv6 CARP multicast group. */
1283a1f7e5f8SHajimu UMEMOTO 		bzero(&in6, sizeof(in6));
1284a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr16[0] = htons(0xff02);
1285a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr8[15] = 0x12;
128608b68b0eSGleb Smirnoff 		if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
128708b68b0eSGleb Smirnoff 			free(im6o->im6o_membership, M_CARP);
128808b68b0eSGleb Smirnoff 			break;
128908b68b0eSGleb Smirnoff 		}
129033cde130SBruce M Simpson 		in6m = NULL;
129108b68b0eSGleb Smirnoff 		if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
129208b68b0eSGleb Smirnoff 			free(im6o->im6o_membership, M_CARP);
129308b68b0eSGleb Smirnoff 			break;
129408b68b0eSGleb Smirnoff 		}
129533cde130SBruce M Simpson 		im6o->im6o_membership[0] = in6m;
129633cde130SBruce M Simpson 		im6o->im6o_num_memberships++;
1297a9771948SGleb Smirnoff 
129808b68b0eSGleb Smirnoff 		/* Join solicited multicast address. */
1299a1f7e5f8SHajimu UMEMOTO 		bzero(&in6, sizeof(in6));
1300a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr16[0] = htons(0xff02);
1301a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr32[1] = 0;
1302a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr32[2] = htonl(1);
130308b68b0eSGleb Smirnoff 		in6.s6_addr32[3] = 0;
1304a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr8[12] = 0xff;
130508b68b0eSGleb Smirnoff 		if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
130608b68b0eSGleb Smirnoff 			in6_mc_leave(im6o->im6o_membership[0], NULL);
130708b68b0eSGleb Smirnoff 			free(im6o->im6o_membership, M_CARP);
130808b68b0eSGleb Smirnoff 			break;
130908b68b0eSGleb Smirnoff 		}
131033cde130SBruce M Simpson 		in6m = NULL;
131108b68b0eSGleb Smirnoff 		if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
131208b68b0eSGleb Smirnoff 			in6_mc_leave(im6o->im6o_membership[0], NULL);
131308b68b0eSGleb Smirnoff 			free(im6o->im6o_membership, M_CARP);
131408b68b0eSGleb Smirnoff 			break;
131508b68b0eSGleb Smirnoff 		}
131633cde130SBruce M Simpson 		im6o->im6o_membership[1] = in6m;
131733cde130SBruce M Simpson 		im6o->im6o_num_memberships++;
1318a9771948SGleb Smirnoff 		break;
1319a9771948SGleb Smirnoff 	    }
1320a9771948SGleb Smirnoff #endif
132108b68b0eSGleb Smirnoff 	}
132208b68b0eSGleb Smirnoff 
132308b68b0eSGleb Smirnoff 	return (error);
1324a9771948SGleb Smirnoff }
1325a9771948SGleb Smirnoff 
1326a9771948SGleb Smirnoff /*
132708b68b0eSGleb Smirnoff  * Free multicast structures.
1328a9771948SGleb Smirnoff  */
13295c1f0f6dSGleb Smirnoff static void
1330a9a2c40cSGleb Smirnoff carp_multicast_cleanup(struct carp_if *cif, sa_family_t sa)
1331a9771948SGleb Smirnoff {
133208b68b0eSGleb Smirnoff 
1333a9a2c40cSGleb Smirnoff 	CIF_LOCK_ASSERT(cif);
133408b68b0eSGleb Smirnoff 	switch (sa) {
133508b68b0eSGleb Smirnoff #ifdef INET
133608b68b0eSGleb Smirnoff 	case AF_INET:
1337a9a2c40cSGleb Smirnoff 		if (cif->cif_naddrs == 0) {
133808b68b0eSGleb Smirnoff 			struct ip_moptions *imo = &cif->cif_imo;
133908b68b0eSGleb Smirnoff 
134008b68b0eSGleb Smirnoff 			in_leavegroup(imo->imo_membership[0], NULL);
134108b68b0eSGleb Smirnoff 			KASSERT(imo->imo_mfilters == NULL,
134208b68b0eSGleb Smirnoff 			    ("%s: imo_mfilters != NULL", __func__));
134308b68b0eSGleb Smirnoff 			free(imo->imo_membership, M_CARP);
134408b68b0eSGleb Smirnoff 			imo->imo_membership = NULL;
134508b68b0eSGleb Smirnoff 
134608b68b0eSGleb Smirnoff 		}
134708b68b0eSGleb Smirnoff 		break;
1348a9771948SGleb Smirnoff #endif
134908b68b0eSGleb Smirnoff #ifdef INET6
135008b68b0eSGleb Smirnoff 	case AF_INET6:
1351a9a2c40cSGleb Smirnoff 		if (cif->cif_naddrs6 == 0) {
135208b68b0eSGleb Smirnoff 			struct ip6_moptions *im6o = &cif->cif_im6o;
135308b68b0eSGleb Smirnoff 
135408b68b0eSGleb Smirnoff 			in6_mc_leave(im6o->im6o_membership[0], NULL);
135508b68b0eSGleb Smirnoff 			in6_mc_leave(im6o->im6o_membership[1], NULL);
135608b68b0eSGleb Smirnoff 			KASSERT(im6o->im6o_mfilters == NULL,
135708b68b0eSGleb Smirnoff 			    ("%s: im6o_mfilters != NULL", __func__));
135808b68b0eSGleb Smirnoff 			free(im6o->im6o_membership, M_CARP);
135908b68b0eSGleb Smirnoff 			im6o->im6o_membership = NULL;
136008b68b0eSGleb Smirnoff 		}
136108b68b0eSGleb Smirnoff 		break;
136208b68b0eSGleb Smirnoff #endif
136308b68b0eSGleb Smirnoff 	}
1364a9771948SGleb Smirnoff }
1365a9771948SGleb Smirnoff 
1366a9771948SGleb Smirnoff int
136708b68b0eSGleb Smirnoff carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa)
1368a9771948SGleb Smirnoff {
1369a9771948SGleb Smirnoff 	struct m_tag *mtag;
1370a9771948SGleb Smirnoff 	struct carp_softc *sc;
1371a9771948SGleb Smirnoff 
1372a9771948SGleb Smirnoff 	if (!sa)
1373a9771948SGleb Smirnoff 		return (0);
1374a9771948SGleb Smirnoff 
1375a9771948SGleb Smirnoff 	switch (sa->sa_family) {
1376a9771948SGleb Smirnoff #ifdef INET
1377a9771948SGleb Smirnoff 	case AF_INET:
1378a9771948SGleb Smirnoff 		break;
137908b68b0eSGleb Smirnoff #endif
1380a9771948SGleb Smirnoff #ifdef INET6
1381a9771948SGleb Smirnoff 	case AF_INET6:
1382a9771948SGleb Smirnoff 		break;
138308b68b0eSGleb Smirnoff #endif
1384a9771948SGleb Smirnoff 	default:
1385a9771948SGleb Smirnoff 		return (0);
1386a9771948SGleb Smirnoff 	}
1387a9771948SGleb Smirnoff 
1388a9771948SGleb Smirnoff 	mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
1389a9771948SGleb Smirnoff 	if (mtag == NULL)
1390a9771948SGleb Smirnoff 		return (0);
1391a9771948SGleb Smirnoff 
1392eaf151c4SGleb Smirnoff 	bcopy(mtag + 1, &sc, sizeof(sc));
1393a9771948SGleb Smirnoff 
139408b68b0eSGleb Smirnoff 	/* Set the source MAC address to the Virtual Router MAC Address. */
1395a9771948SGleb Smirnoff 	switch (ifp->if_type) {
1396630481bbSYaroslav Tykhiy 	case IFT_ETHER:
13979ca1fe0dSGleb Smirnoff 	case IFT_BRIDGE:
1398630481bbSYaroslav Tykhiy 	case IFT_L2VLAN: {
1399a9771948SGleb Smirnoff 			struct ether_header *eh;
1400a9771948SGleb Smirnoff 
1401a9771948SGleb Smirnoff 			eh = mtod(m, struct ether_header *);
1402a9771948SGleb Smirnoff 			eh->ether_shost[0] = 0;
1403a9771948SGleb Smirnoff 			eh->ether_shost[1] = 0;
1404a9771948SGleb Smirnoff 			eh->ether_shost[2] = 0x5e;
1405a9771948SGleb Smirnoff 			eh->ether_shost[3] = 0;
1406a9771948SGleb Smirnoff 			eh->ether_shost[4] = 1;
1407a9771948SGleb Smirnoff 			eh->ether_shost[5] = sc->sc_vhid;
1408a9771948SGleb Smirnoff 		}
1409a9771948SGleb Smirnoff 		break;
1410a9771948SGleb Smirnoff 	case IFT_FDDI: {
1411a9771948SGleb Smirnoff 			struct fddi_header *fh;
1412a9771948SGleb Smirnoff 
1413a9771948SGleb Smirnoff 			fh = mtod(m, struct fddi_header *);
1414a9771948SGleb Smirnoff 			fh->fddi_shost[0] = 0;
1415a9771948SGleb Smirnoff 			fh->fddi_shost[1] = 0;
1416a9771948SGleb Smirnoff 			fh->fddi_shost[2] = 0x5e;
1417a9771948SGleb Smirnoff 			fh->fddi_shost[3] = 0;
1418a9771948SGleb Smirnoff 			fh->fddi_shost[4] = 1;
1419a9771948SGleb Smirnoff 			fh->fddi_shost[5] = sc->sc_vhid;
1420a9771948SGleb Smirnoff 		}
1421a9771948SGleb Smirnoff 		break;
1422a9771948SGleb Smirnoff 	case IFT_ISO88025: {
1423a9771948SGleb Smirnoff  			struct iso88025_header *th;
1424a9771948SGleb Smirnoff  			th = mtod(m, struct iso88025_header *);
1425a9771948SGleb Smirnoff 			th->iso88025_shost[0] = 3;
1426a9771948SGleb Smirnoff 			th->iso88025_shost[1] = 0;
1427a9771948SGleb Smirnoff 			th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1);
1428a9771948SGleb Smirnoff 			th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1);
1429a9771948SGleb Smirnoff 			th->iso88025_shost[4] = 0;
1430a9771948SGleb Smirnoff 			th->iso88025_shost[5] = 0;
1431a9771948SGleb Smirnoff 		}
1432a9771948SGleb Smirnoff 		break;
1433a9771948SGleb Smirnoff 	default:
143408b68b0eSGleb Smirnoff 		printf("%s: carp is not supported for the %d interface type\n",
143508b68b0eSGleb Smirnoff 		    ifp->if_xname, ifp->if_type);
1436a9771948SGleb Smirnoff 		return (EOPNOTSUPP);
1437a9771948SGleb Smirnoff 	}
1438a9771948SGleb Smirnoff 
1439a9771948SGleb Smirnoff 	return (0);
1440a9771948SGleb Smirnoff }
1441a9771948SGleb Smirnoff 
144208b68b0eSGleb Smirnoff static struct carp_softc*
144308b68b0eSGleb Smirnoff carp_alloc(struct ifnet *ifp)
1444a9771948SGleb Smirnoff {
144508b68b0eSGleb Smirnoff 	struct carp_softc *sc;
144608b68b0eSGleb Smirnoff 	struct carp_if *cif;
1447d220759bSGleb Smirnoff 
144808b68b0eSGleb Smirnoff 	if ((cif = ifp->if_carp) == NULL) {
144908b68b0eSGleb Smirnoff 		cif = carp_alloc_if(ifp);
145008b68b0eSGleb Smirnoff 		if (cif == NULL)
145108b68b0eSGleb Smirnoff 			return (NULL);
1452a9771948SGleb Smirnoff 	}
1453a9771948SGleb Smirnoff 
145408b68b0eSGleb Smirnoff 	sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
145508b68b0eSGleb Smirnoff 
145608b68b0eSGleb Smirnoff 	sc->sc_advbase = CARP_DFLTINTV;
145708b68b0eSGleb Smirnoff 	sc->sc_vhid = -1;	/* required setting */
145808b68b0eSGleb Smirnoff 	sc->sc_init_counter = 1;
145908b68b0eSGleb Smirnoff 	sc->sc_state = INIT;
146008b68b0eSGleb Smirnoff 
146108b68b0eSGleb Smirnoff 	sc->sc_ifasiz = sizeof(struct ifaddr *);
146208b68b0eSGleb Smirnoff 	sc->sc_ifas = malloc(sc->sc_ifasiz, M_CARP, M_WAITOK|M_ZERO);
146308b68b0eSGleb Smirnoff 	sc->sc_carpdev = ifp;
146408b68b0eSGleb Smirnoff 
146508b68b0eSGleb Smirnoff 	CARP_LOCK_INIT(sc);
146608b68b0eSGleb Smirnoff #ifdef INET
146708b68b0eSGleb Smirnoff 	callout_init_mtx(&sc->sc_md_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
146808b68b0eSGleb Smirnoff #endif
146908b68b0eSGleb Smirnoff #ifdef INET6
147008b68b0eSGleb Smirnoff 	callout_init_mtx(&sc->sc_md6_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
147108b68b0eSGleb Smirnoff #endif
147208b68b0eSGleb Smirnoff 	callout_init_mtx(&sc->sc_ad_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
147308b68b0eSGleb Smirnoff 
147408b68b0eSGleb Smirnoff 	CIF_LOCK(cif);
147508b68b0eSGleb Smirnoff 	TAILQ_INSERT_TAIL(&cif->cif_vrs, sc, sc_list);
147608b68b0eSGleb Smirnoff 	CIF_UNLOCK(cif);
147708b68b0eSGleb Smirnoff 
147808b68b0eSGleb Smirnoff 	mtx_lock(&carp_mtx);
147908b68b0eSGleb Smirnoff 	LIST_INSERT_HEAD(&carp_list, sc, sc_next);
148008b68b0eSGleb Smirnoff 	mtx_unlock(&carp_mtx);
148108b68b0eSGleb Smirnoff 
148208b68b0eSGleb Smirnoff 	return (sc);
148308b68b0eSGleb Smirnoff }
148408b68b0eSGleb Smirnoff 
148508b68b0eSGleb Smirnoff static int
148608b68b0eSGleb Smirnoff carp_grow_ifas(struct carp_softc *sc)
148708b68b0eSGleb Smirnoff {
148808b68b0eSGleb Smirnoff 	struct ifaddr **new;
148908b68b0eSGleb Smirnoff 
149008b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
149108b68b0eSGleb Smirnoff 
149208b68b0eSGleb Smirnoff 	new = malloc(sc->sc_ifasiz * 2, M_CARP, M_NOWAIT|M_ZERO);
149308b68b0eSGleb Smirnoff 	if (new == NULL)
149408b68b0eSGleb Smirnoff 		return (ENOMEM);
149508b68b0eSGleb Smirnoff 	bcopy(sc->sc_ifas, new, sc->sc_ifasiz);
149608b68b0eSGleb Smirnoff 	free(sc->sc_ifas, M_CARP);
149708b68b0eSGleb Smirnoff 	sc->sc_ifas = new;
149808b68b0eSGleb Smirnoff 	sc->sc_ifasiz *= 2;
149908b68b0eSGleb Smirnoff 
150008b68b0eSGleb Smirnoff 	return (0);
150108b68b0eSGleb Smirnoff }
150208b68b0eSGleb Smirnoff 
150308b68b0eSGleb Smirnoff static void
150408b68b0eSGleb Smirnoff carp_destroy(struct carp_softc *sc)
150508b68b0eSGleb Smirnoff {
150608b68b0eSGleb Smirnoff 	struct ifnet *ifp = sc->sc_carpdev;
150708b68b0eSGleb Smirnoff 	struct carp_if *cif = ifp->if_carp;
150808b68b0eSGleb Smirnoff 
1509a9a2c40cSGleb Smirnoff 	CIF_LOCK_ASSERT(cif);
1510a9a2c40cSGleb Smirnoff 
151108b68b0eSGleb Smirnoff 	TAILQ_REMOVE(&cif->cif_vrs, sc, sc_list);
151208b68b0eSGleb Smirnoff 
151308b68b0eSGleb Smirnoff 	mtx_lock(&carp_mtx);
151408b68b0eSGleb Smirnoff 	LIST_REMOVE(sc, sc_next);
151508b68b0eSGleb Smirnoff 	mtx_unlock(&carp_mtx);
151608b68b0eSGleb Smirnoff 
151708b68b0eSGleb Smirnoff 	CARP_LOCK(sc);
1518f08535f8SGleb Smirnoff 	if (sc->sc_suppress)
1519f08535f8SGleb Smirnoff 		carp_demote_adj(-carp_ifdown_adj, "vhid removed");
152008b68b0eSGleb Smirnoff 	callout_drain(&sc->sc_ad_tmo);
152108b68b0eSGleb Smirnoff #ifdef INET
152208b68b0eSGleb Smirnoff 	callout_drain(&sc->sc_md_tmo);
152308b68b0eSGleb Smirnoff #endif
152408b68b0eSGleb Smirnoff #ifdef INET6
152508b68b0eSGleb Smirnoff 	callout_drain(&sc->sc_md6_tmo);
152608b68b0eSGleb Smirnoff #endif
152708b68b0eSGleb Smirnoff 	CARP_LOCK_DESTROY(sc);
152808b68b0eSGleb Smirnoff 
152908b68b0eSGleb Smirnoff 	free(sc->sc_ifas, M_CARP);
153008b68b0eSGleb Smirnoff 	free(sc, M_CARP);
153108b68b0eSGleb Smirnoff }
153208b68b0eSGleb Smirnoff 
153308b68b0eSGleb Smirnoff static struct carp_if*
153408b68b0eSGleb Smirnoff carp_alloc_if(struct ifnet *ifp)
1535a9771948SGleb Smirnoff {
153654bfbd51SWill Andrews 	struct carp_if *cif;
1537d220759bSGleb Smirnoff 
153808b68b0eSGleb Smirnoff 	cif = malloc(sizeof(*cif), M_CARP, M_WAITOK|M_ZERO);
153908b68b0eSGleb Smirnoff 
154008b68b0eSGleb Smirnoff 	if (ifpromisc(ifp, 1) != 0)
154108b68b0eSGleb Smirnoff 		goto cleanup;
154208b68b0eSGleb Smirnoff 
154308b68b0eSGleb Smirnoff 	CIF_LOCK_INIT(cif);
154408b68b0eSGleb Smirnoff 	cif->cif_ifp = ifp;
154508b68b0eSGleb Smirnoff 	TAILQ_INIT(&cif->cif_vrs);
154608b68b0eSGleb Smirnoff 
1547137f91e8SJohn Baldwin 	IF_ADDR_WLOCK(ifp);
154808b68b0eSGleb Smirnoff 	ifp->if_carp = cif;
154908b68b0eSGleb Smirnoff 	if_ref(ifp);
1550137f91e8SJohn Baldwin 	IF_ADDR_WUNLOCK(ifp);
155108b68b0eSGleb Smirnoff 
155208b68b0eSGleb Smirnoff 	return (cif);
155308b68b0eSGleb Smirnoff 
155408b68b0eSGleb Smirnoff cleanup:
155508b68b0eSGleb Smirnoff 	free(cif, M_CARP);
155608b68b0eSGleb Smirnoff 
155708b68b0eSGleb Smirnoff 	return (NULL);
1558d220759bSGleb Smirnoff }
1559d220759bSGleb Smirnoff 
1560d220759bSGleb Smirnoff static void
156108b68b0eSGleb Smirnoff carp_free_if(struct carp_if *cif)
156208b68b0eSGleb Smirnoff {
156308b68b0eSGleb Smirnoff 	struct ifnet *ifp = cif->cif_ifp;
156408b68b0eSGleb Smirnoff 
156508b68b0eSGleb Smirnoff 	CIF_LOCK_ASSERT(cif);
156608b68b0eSGleb Smirnoff 	KASSERT(TAILQ_EMPTY(&cif->cif_vrs), ("%s: softc list not empty",
156708b68b0eSGleb Smirnoff 	    __func__));
156808b68b0eSGleb Smirnoff 
1569137f91e8SJohn Baldwin 	IF_ADDR_WLOCK(ifp);
157008b68b0eSGleb Smirnoff 	ifp->if_carp = NULL;
157108b68b0eSGleb Smirnoff 	if_rele(ifp);
1572137f91e8SJohn Baldwin 	IF_ADDR_WUNLOCK(ifp);
157308b68b0eSGleb Smirnoff 
157408b68b0eSGleb Smirnoff 	CIF_LOCK_DESTROY(cif);
157508b68b0eSGleb Smirnoff 
157608b68b0eSGleb Smirnoff 	ifpromisc(ifp, 0);
157708b68b0eSGleb Smirnoff 
157808b68b0eSGleb Smirnoff 	free(cif, M_CARP);
157908b68b0eSGleb Smirnoff }
158008b68b0eSGleb Smirnoff 
158108b68b0eSGleb Smirnoff static void
158208b68b0eSGleb Smirnoff carp_carprcp(struct carpreq *carpr, struct carp_softc *sc, int priv)
158308b68b0eSGleb Smirnoff {
158408b68b0eSGleb Smirnoff 
158508b68b0eSGleb Smirnoff 	CARP_LOCK(sc);
158608b68b0eSGleb Smirnoff 	carpr->carpr_state = sc->sc_state;
158708b68b0eSGleb Smirnoff 	carpr->carpr_vhid = sc->sc_vhid;
158808b68b0eSGleb Smirnoff 	carpr->carpr_advbase = sc->sc_advbase;
158908b68b0eSGleb Smirnoff 	carpr->carpr_advskew = sc->sc_advskew;
159008b68b0eSGleb Smirnoff 	if (priv)
159108b68b0eSGleb Smirnoff 		bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key));
159208b68b0eSGleb Smirnoff 	else
159308b68b0eSGleb Smirnoff 		bzero(carpr->carpr_key, sizeof(carpr->carpr_key));
159408b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
159508b68b0eSGleb Smirnoff }
159608b68b0eSGleb Smirnoff 
159708b68b0eSGleb Smirnoff int
159808b68b0eSGleb Smirnoff carp_ioctl(struct ifreq *ifr, u_long cmd, struct thread *td)
159908b68b0eSGleb Smirnoff {
160008b68b0eSGleb Smirnoff 	struct carpreq carpr;
160108b68b0eSGleb Smirnoff 	struct ifnet *ifp;
160208b68b0eSGleb Smirnoff 	struct carp_softc *sc = NULL;
160308b68b0eSGleb Smirnoff 	int error = 0, locked = 0;
160408b68b0eSGleb Smirnoff 
160508b68b0eSGleb Smirnoff 	if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
160608b68b0eSGleb Smirnoff 		return (error);
160708b68b0eSGleb Smirnoff 
160808b68b0eSGleb Smirnoff 	ifp = ifunit_ref(ifr->ifr_name);
160908b68b0eSGleb Smirnoff 	if (ifp == NULL)
161008b68b0eSGleb Smirnoff 		return (ENXIO);
161108b68b0eSGleb Smirnoff 
161208b68b0eSGleb Smirnoff 	switch (ifp->if_type) {
161308b68b0eSGleb Smirnoff 	case IFT_ETHER:
161408b68b0eSGleb Smirnoff 	case IFT_L2VLAN:
16159ca1fe0dSGleb Smirnoff 	case IFT_BRIDGE:
161608b68b0eSGleb Smirnoff 	case IFT_FDDI:
161708b68b0eSGleb Smirnoff 	case IFT_ISO88025:
161808b68b0eSGleb Smirnoff 		break;
161908b68b0eSGleb Smirnoff 	default:
162008b68b0eSGleb Smirnoff 		error = EOPNOTSUPP;
162108b68b0eSGleb Smirnoff 		goto out;
162208b68b0eSGleb Smirnoff 	}
162308b68b0eSGleb Smirnoff 
162408b68b0eSGleb Smirnoff 	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
162508b68b0eSGleb Smirnoff 		error = EADDRNOTAVAIL;
162608b68b0eSGleb Smirnoff 		goto out;
162708b68b0eSGleb Smirnoff 	}
162808b68b0eSGleb Smirnoff 
162908b68b0eSGleb Smirnoff 	switch (cmd) {
163008b68b0eSGleb Smirnoff 	case SIOCSVH:
163108b68b0eSGleb Smirnoff 		if ((error = priv_check(td, PRIV_NETINET_CARP)))
163208b68b0eSGleb Smirnoff 			break;
163308b68b0eSGleb Smirnoff 		if (carpr.carpr_vhid <= 0 || carpr.carpr_vhid > CARP_MAXVHID ||
163408b68b0eSGleb Smirnoff 		    carpr.carpr_advbase < 0 || carpr.carpr_advskew < 0) {
163508b68b0eSGleb Smirnoff 			error = EINVAL;
163608b68b0eSGleb Smirnoff 			break;
163708b68b0eSGleb Smirnoff 		}
163808b68b0eSGleb Smirnoff 
163908b68b0eSGleb Smirnoff 		if (ifp->if_carp) {
164008b68b0eSGleb Smirnoff 			CIF_LOCK(ifp->if_carp);
164108b68b0eSGleb Smirnoff 			IFNET_FOREACH_CARP(ifp, sc)
164208b68b0eSGleb Smirnoff 				if (sc->sc_vhid == carpr.carpr_vhid)
164308b68b0eSGleb Smirnoff 					break;
164408b68b0eSGleb Smirnoff 			CIF_UNLOCK(ifp->if_carp);
164508b68b0eSGleb Smirnoff 		}
164608b68b0eSGleb Smirnoff 		if (sc == NULL) {
164708b68b0eSGleb Smirnoff 			sc = carp_alloc(ifp);
164808b68b0eSGleb Smirnoff 			if (sc == NULL) {
164908b68b0eSGleb Smirnoff 				error = EINVAL; /* XXX: ifpromisc failed */
165008b68b0eSGleb Smirnoff 				break;
165108b68b0eSGleb Smirnoff 			}
165208b68b0eSGleb Smirnoff 
165308b68b0eSGleb Smirnoff 			CARP_LOCK(sc);
165408b68b0eSGleb Smirnoff 			sc->sc_vhid = carpr.carpr_vhid;
165508b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[0] = 0;
165608b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[1] = 0;
165708b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[2] = 0x5e;
165808b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[3] = 0;
165908b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[4] = 1;
166008b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[5] = sc->sc_vhid;
166108b68b0eSGleb Smirnoff 		} else
166208b68b0eSGleb Smirnoff 			CARP_LOCK(sc);
166308b68b0eSGleb Smirnoff 		locked = 1;
166408b68b0eSGleb Smirnoff 		if (carpr.carpr_advbase > 0) {
166508b68b0eSGleb Smirnoff 			if (carpr.carpr_advbase > 255 ||
166608b68b0eSGleb Smirnoff 			    carpr.carpr_advbase < CARP_DFLTINTV) {
166708b68b0eSGleb Smirnoff 				error = EINVAL;
166808b68b0eSGleb Smirnoff 				break;
166908b68b0eSGleb Smirnoff 			}
167008b68b0eSGleb Smirnoff 			sc->sc_advbase = carpr.carpr_advbase;
167108b68b0eSGleb Smirnoff 		}
167208b68b0eSGleb Smirnoff 		if (carpr.carpr_advskew > 0) {
167308b68b0eSGleb Smirnoff 			if (carpr.carpr_advskew >= 255) {
167408b68b0eSGleb Smirnoff 				error = EINVAL;
167508b68b0eSGleb Smirnoff 				break;
167608b68b0eSGleb Smirnoff 			}
167708b68b0eSGleb Smirnoff 			sc->sc_advskew = carpr.carpr_advskew;
167808b68b0eSGleb Smirnoff 		}
167908b68b0eSGleb Smirnoff 		if (carpr.carpr_key[0] != '\0') {
168008b68b0eSGleb Smirnoff 			bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
168108b68b0eSGleb Smirnoff 			carp_hmac_prepare(sc);
168208b68b0eSGleb Smirnoff 		}
168308b68b0eSGleb Smirnoff 		if (sc->sc_state != INIT &&
168408b68b0eSGleb Smirnoff 		    carpr.carpr_state != sc->sc_state) {
168508b68b0eSGleb Smirnoff 			switch (carpr.carpr_state) {
168608b68b0eSGleb Smirnoff 			case BACKUP:
168708b68b0eSGleb Smirnoff 				callout_stop(&sc->sc_ad_tmo);
168808b68b0eSGleb Smirnoff 				carp_set_state(sc, BACKUP);
168908b68b0eSGleb Smirnoff 				carp_setrun(sc, 0);
169008b68b0eSGleb Smirnoff 				carp_delroute(sc);
169108b68b0eSGleb Smirnoff 				break;
169208b68b0eSGleb Smirnoff 			case MASTER:
169308b68b0eSGleb Smirnoff 				carp_master_down_locked(sc);
169408b68b0eSGleb Smirnoff 				break;
169508b68b0eSGleb Smirnoff 			default:
169608b68b0eSGleb Smirnoff 				break;
169708b68b0eSGleb Smirnoff 			}
169808b68b0eSGleb Smirnoff 		}
169908b68b0eSGleb Smirnoff 		break;
170008b68b0eSGleb Smirnoff 
170108b68b0eSGleb Smirnoff 	case SIOCGVH:
170208b68b0eSGleb Smirnoff 	    {
170308b68b0eSGleb Smirnoff 		int priveleged;
170408b68b0eSGleb Smirnoff 
170508b68b0eSGleb Smirnoff 		if (carpr.carpr_vhid < 0 || carpr.carpr_vhid > CARP_MAXVHID) {
170608b68b0eSGleb Smirnoff 			error = EINVAL;
170708b68b0eSGleb Smirnoff 			break;
170808b68b0eSGleb Smirnoff 		}
170908b68b0eSGleb Smirnoff 		if (carpr.carpr_count < 1) {
171008b68b0eSGleb Smirnoff 			error = EMSGSIZE;
171108b68b0eSGleb Smirnoff 			break;
171208b68b0eSGleb Smirnoff 		}
171308b68b0eSGleb Smirnoff 		if (ifp->if_carp == NULL) {
171408b68b0eSGleb Smirnoff 			error = ENOENT;
171508b68b0eSGleb Smirnoff 			break;
171608b68b0eSGleb Smirnoff 		}
171708b68b0eSGleb Smirnoff 
171808b68b0eSGleb Smirnoff 		priveleged = (priv_check(td, PRIV_NETINET_CARP) == 0);
171908b68b0eSGleb Smirnoff 		if (carpr.carpr_vhid != 0) {
172008b68b0eSGleb Smirnoff 			CIF_LOCK(ifp->if_carp);
172108b68b0eSGleb Smirnoff 			IFNET_FOREACH_CARP(ifp, sc)
172208b68b0eSGleb Smirnoff 				if (sc->sc_vhid == carpr.carpr_vhid)
172308b68b0eSGleb Smirnoff 					break;
172408b68b0eSGleb Smirnoff 			CIF_UNLOCK(ifp->if_carp);
172508b68b0eSGleb Smirnoff 			if (sc == NULL) {
172608b68b0eSGleb Smirnoff 				error = ENOENT;
172708b68b0eSGleb Smirnoff 				break;
172808b68b0eSGleb Smirnoff 			}
172908b68b0eSGleb Smirnoff 			carp_carprcp(&carpr, sc, priveleged);
173008b68b0eSGleb Smirnoff 			error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
173108b68b0eSGleb Smirnoff 		} else  {
173208b68b0eSGleb Smirnoff 			int i, count;
173308b68b0eSGleb Smirnoff 
173408b68b0eSGleb Smirnoff 			count = 0;
173508b68b0eSGleb Smirnoff 			CIF_LOCK(ifp->if_carp);
173608b68b0eSGleb Smirnoff 			IFNET_FOREACH_CARP(ifp, sc)
173708b68b0eSGleb Smirnoff 				count++;
173808b68b0eSGleb Smirnoff 
173908b68b0eSGleb Smirnoff 			if (count > carpr.carpr_count) {
174008b68b0eSGleb Smirnoff 				CIF_UNLOCK(ifp->if_carp);
174108b68b0eSGleb Smirnoff 				error = EMSGSIZE;
174208b68b0eSGleb Smirnoff 				break;
174308b68b0eSGleb Smirnoff 			}
174408b68b0eSGleb Smirnoff 
174508b68b0eSGleb Smirnoff 			i = 0;
174608b68b0eSGleb Smirnoff 			IFNET_FOREACH_CARP(ifp, sc) {
174708b68b0eSGleb Smirnoff 				carp_carprcp(&carpr, sc, priveleged);
174808b68b0eSGleb Smirnoff 				carpr.carpr_count = count;
174908b68b0eSGleb Smirnoff 				error = copyout(&carpr, ifr->ifr_data +
175008b68b0eSGleb Smirnoff 				    (i * sizeof(carpr)), sizeof(carpr));
175108b68b0eSGleb Smirnoff 				if (error) {
175208b68b0eSGleb Smirnoff 					CIF_UNLOCK(ifp->if_carp);
175308b68b0eSGleb Smirnoff 					break;
175408b68b0eSGleb Smirnoff 				}
175508b68b0eSGleb Smirnoff 				i++;
175608b68b0eSGleb Smirnoff 			}
175708b68b0eSGleb Smirnoff 			CIF_UNLOCK(ifp->if_carp);
175808b68b0eSGleb Smirnoff 		}
175908b68b0eSGleb Smirnoff 		break;
176008b68b0eSGleb Smirnoff 	    }
176108b68b0eSGleb Smirnoff 	default:
176208b68b0eSGleb Smirnoff 		error = EINVAL;
176308b68b0eSGleb Smirnoff 	}
176408b68b0eSGleb Smirnoff 
176508b68b0eSGleb Smirnoff out:
176608b68b0eSGleb Smirnoff 	if (locked)
176708b68b0eSGleb Smirnoff 		CARP_UNLOCK(sc);
176808b68b0eSGleb Smirnoff 	if_rele(ifp);
176908b68b0eSGleb Smirnoff 
177008b68b0eSGleb Smirnoff 	return (error);
177108b68b0eSGleb Smirnoff }
177208b68b0eSGleb Smirnoff 
177308b68b0eSGleb Smirnoff static int
177408b68b0eSGleb Smirnoff carp_get_vhid(struct ifaddr *ifa)
177508b68b0eSGleb Smirnoff {
177608b68b0eSGleb Smirnoff 
177708b68b0eSGleb Smirnoff 	if (ifa == NULL || ifa->ifa_carp == NULL)
177808b68b0eSGleb Smirnoff 		return (0);
177908b68b0eSGleb Smirnoff 
178008b68b0eSGleb Smirnoff 	return (ifa->ifa_carp->sc_vhid);
178108b68b0eSGleb Smirnoff }
178208b68b0eSGleb Smirnoff 
178308b68b0eSGleb Smirnoff int
178408b68b0eSGleb Smirnoff carp_attach(struct ifaddr *ifa, int vhid)
178508b68b0eSGleb Smirnoff {
178608b68b0eSGleb Smirnoff 	struct ifnet *ifp = ifa->ifa_ifp;
1787a9a2c40cSGleb Smirnoff 	struct carp_if *cif = ifp->if_carp;
178808b68b0eSGleb Smirnoff 	struct carp_softc *sc;
178908b68b0eSGleb Smirnoff 	int index, error;
179008b68b0eSGleb Smirnoff 
179108b68b0eSGleb Smirnoff 	if (ifp->if_carp == NULL)
179208b68b0eSGleb Smirnoff 		return (ENOPROTOOPT);
179308b68b0eSGleb Smirnoff 
179408b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
179508b68b0eSGleb Smirnoff #ifdef INET
179608b68b0eSGleb Smirnoff 	case AF_INET:
179708b68b0eSGleb Smirnoff #endif
179808b68b0eSGleb Smirnoff #ifdef INET6
179908b68b0eSGleb Smirnoff 	case AF_INET6:
180008b68b0eSGleb Smirnoff #endif
180108b68b0eSGleb Smirnoff 		break;
180208b68b0eSGleb Smirnoff 	default:
180308b68b0eSGleb Smirnoff 		return (EPROTOTYPE);
180408b68b0eSGleb Smirnoff 	}
180508b68b0eSGleb Smirnoff 
1806a9a2c40cSGleb Smirnoff 	CIF_LOCK(cif);
180708b68b0eSGleb Smirnoff 	IFNET_FOREACH_CARP(ifp, sc)
180808b68b0eSGleb Smirnoff 		if (sc->sc_vhid == vhid)
180908b68b0eSGleb Smirnoff 			break;
1810a9a2c40cSGleb Smirnoff 	if (sc == NULL) {
1811a9a2c40cSGleb Smirnoff 		CIF_UNLOCK(cif);
181208b68b0eSGleb Smirnoff 		return (ENOENT);
1813a9a2c40cSGleb Smirnoff 	}
181408b68b0eSGleb Smirnoff 
181508b68b0eSGleb Smirnoff 	if (ifa->ifa_carp) {
181608b68b0eSGleb Smirnoff 		if (ifa->ifa_carp->sc_vhid != vhid)
1817a9a2c40cSGleb Smirnoff 			carp_detach_locked(ifa);
1818a9a2c40cSGleb Smirnoff 		else {
1819a9a2c40cSGleb Smirnoff 			CIF_UNLOCK(cif);
182008b68b0eSGleb Smirnoff 			return (0);
182108b68b0eSGleb Smirnoff 		}
1822a9a2c40cSGleb Smirnoff 	}
182308b68b0eSGleb Smirnoff 
1824a9a2c40cSGleb Smirnoff 	error = carp_multicast_setup(cif, ifa->ifa_addr->sa_family);
1825a9a2c40cSGleb Smirnoff 	if (error) {
1826a9a2c40cSGleb Smirnoff 		CIF_FREE(cif);
182708b68b0eSGleb Smirnoff 		return (error);
1828a9a2c40cSGleb Smirnoff 	}
182908b68b0eSGleb Smirnoff 
183008b68b0eSGleb Smirnoff 	CARP_LOCK(sc);
183108b68b0eSGleb Smirnoff 	index = sc->sc_naddrs + sc->sc_naddrs6 + 1;
183208b68b0eSGleb Smirnoff 	if (index > sc->sc_ifasiz / sizeof(struct ifaddr *))
183308b68b0eSGleb Smirnoff 		if ((error = carp_grow_ifas(sc)) != 0) {
1834a9a2c40cSGleb Smirnoff 			carp_multicast_cleanup(cif,
183508b68b0eSGleb Smirnoff 			    ifa->ifa_addr->sa_family);
183608b68b0eSGleb Smirnoff 			CARP_UNLOCK(sc);
1837a9a2c40cSGleb Smirnoff 			CIF_FREE(cif);
183808b68b0eSGleb Smirnoff 			return (error);
183908b68b0eSGleb Smirnoff 		}
184008b68b0eSGleb Smirnoff 
184108b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
184208b68b0eSGleb Smirnoff #ifdef INET
184308b68b0eSGleb Smirnoff 	case AF_INET:
1844a9a2c40cSGleb Smirnoff 		cif->cif_naddrs++;
184508b68b0eSGleb Smirnoff 		sc->sc_naddrs++;
184608b68b0eSGleb Smirnoff 		break;
184708b68b0eSGleb Smirnoff #endif
184808b68b0eSGleb Smirnoff #ifdef INET6
184908b68b0eSGleb Smirnoff 	case AF_INET6:
1850a9a2c40cSGleb Smirnoff 		cif->cif_naddrs6++;
185108b68b0eSGleb Smirnoff 		sc->sc_naddrs6++;
185208b68b0eSGleb Smirnoff 		break;
185308b68b0eSGleb Smirnoff #endif
185408b68b0eSGleb Smirnoff 	}
185508b68b0eSGleb Smirnoff 
185608b68b0eSGleb Smirnoff 	ifa_ref(ifa);
185708b68b0eSGleb Smirnoff 	sc->sc_ifas[index - 1] = ifa;
185808b68b0eSGleb Smirnoff 	ifa->ifa_carp = sc;
185908b68b0eSGleb Smirnoff 
186008b68b0eSGleb Smirnoff 	carp_hmac_prepare(sc);
186108b68b0eSGleb Smirnoff 	carp_sc_state(sc);
186208b68b0eSGleb Smirnoff 
186308b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
1864a9a2c40cSGleb Smirnoff 	CIF_UNLOCK(cif);
186508b68b0eSGleb Smirnoff 
186608b68b0eSGleb Smirnoff 	return (0);
186708b68b0eSGleb Smirnoff }
186808b68b0eSGleb Smirnoff 
186908b68b0eSGleb Smirnoff void
187008b68b0eSGleb Smirnoff carp_detach(struct ifaddr *ifa)
187108b68b0eSGleb Smirnoff {
1872a9a2c40cSGleb Smirnoff 	struct ifnet *ifp = ifa->ifa_ifp;
1873a9a2c40cSGleb Smirnoff 	struct carp_if *cif = ifp->if_carp;
1874a9a2c40cSGleb Smirnoff 
1875a9a2c40cSGleb Smirnoff 	CIF_LOCK(cif);
1876a9a2c40cSGleb Smirnoff 	carp_detach_locked(ifa);
1877a9a2c40cSGleb Smirnoff 	CIF_FREE(cif);
1878a9a2c40cSGleb Smirnoff }
1879a9a2c40cSGleb Smirnoff 
1880a9a2c40cSGleb Smirnoff static void
1881a9a2c40cSGleb Smirnoff carp_detach_locked(struct ifaddr *ifa)
1882a9a2c40cSGleb Smirnoff {
1883a9a2c40cSGleb Smirnoff 	struct ifnet *ifp = ifa->ifa_ifp;
1884a9a2c40cSGleb Smirnoff 	struct carp_if *cif = ifp->if_carp;
188508b68b0eSGleb Smirnoff 	struct carp_softc *sc = ifa->ifa_carp;
188608b68b0eSGleb Smirnoff 	int i, index;
188708b68b0eSGleb Smirnoff 
188808b68b0eSGleb Smirnoff 	KASSERT(sc != NULL, ("%s: %p not attached", __func__, ifa));
188908b68b0eSGleb Smirnoff 
1890a9a2c40cSGleb Smirnoff 	CIF_LOCK_ASSERT(cif);
189108b68b0eSGleb Smirnoff 	CARP_LOCK(sc);
189208b68b0eSGleb Smirnoff 
189308b68b0eSGleb Smirnoff 	/* Shift array. */
189408b68b0eSGleb Smirnoff 	index = sc->sc_naddrs + sc->sc_naddrs6;
189508b68b0eSGleb Smirnoff 	for (i = 0; i < index; i++)
189608b68b0eSGleb Smirnoff 		if (sc->sc_ifas[i] == ifa)
189708b68b0eSGleb Smirnoff 			break;
189808b68b0eSGleb Smirnoff 	KASSERT(i < index, ("%s: %p no backref", __func__, ifa));
189908b68b0eSGleb Smirnoff 	for (; i < index - 1; i++)
190008b68b0eSGleb Smirnoff 		sc->sc_ifas[i] = sc->sc_ifas[i+1];
190108b68b0eSGleb Smirnoff 	sc->sc_ifas[index - 1] = NULL;
190208b68b0eSGleb Smirnoff 
190308b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
190408b68b0eSGleb Smirnoff #ifdef INET
190508b68b0eSGleb Smirnoff 	case AF_INET:
1906a9a2c40cSGleb Smirnoff 		cif->cif_naddrs--;
190708b68b0eSGleb Smirnoff 		sc->sc_naddrs--;
190808b68b0eSGleb Smirnoff 		break;
190908b68b0eSGleb Smirnoff #endif
191008b68b0eSGleb Smirnoff #ifdef INET6
191108b68b0eSGleb Smirnoff 	case AF_INET6:
1912a9a2c40cSGleb Smirnoff 		cif->cif_naddrs6--;
191308b68b0eSGleb Smirnoff 		sc->sc_naddrs6--;
191408b68b0eSGleb Smirnoff 		break;
191508b68b0eSGleb Smirnoff #endif
191608b68b0eSGleb Smirnoff 	}
191708b68b0eSGleb Smirnoff 
19182512b096SGleb Smirnoff 	carp_ifa_delroute(ifa);
1919a9a2c40cSGleb Smirnoff 	carp_multicast_cleanup(cif, ifa->ifa_addr->sa_family);
192008b68b0eSGleb Smirnoff 
192108b68b0eSGleb Smirnoff 	ifa->ifa_carp = NULL;
192208b68b0eSGleb Smirnoff 	ifa_free(ifa);
192308b68b0eSGleb Smirnoff 
192408b68b0eSGleb Smirnoff 	carp_hmac_prepare(sc);
192508b68b0eSGleb Smirnoff 	carp_sc_state(sc);
192608b68b0eSGleb Smirnoff 
192708b68b0eSGleb Smirnoff 	if (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) {
192808b68b0eSGleb Smirnoff 		CARP_UNLOCK(sc);
192908b68b0eSGleb Smirnoff 		carp_destroy(sc);
193008b68b0eSGleb Smirnoff 	} else
193108b68b0eSGleb Smirnoff 		CARP_UNLOCK(sc);
193208b68b0eSGleb Smirnoff }
193308b68b0eSGleb Smirnoff 
193408b68b0eSGleb Smirnoff static void
193508b68b0eSGleb Smirnoff carp_set_state(struct carp_softc *sc, int state)
193608b68b0eSGleb Smirnoff {
193708b68b0eSGleb Smirnoff 
193808b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
193908b68b0eSGleb Smirnoff 
194008b68b0eSGleb Smirnoff 	if (sc->sc_state != state) {
194108b68b0eSGleb Smirnoff 		const char *carp_states[] = { CARP_STATES };
194208b68b0eSGleb Smirnoff 		char subsys[IFNAMSIZ+5];
194308b68b0eSGleb Smirnoff 
194408b68b0eSGleb Smirnoff 		sc->sc_state = state;
194508b68b0eSGleb Smirnoff 
194608b68b0eSGleb Smirnoff 		snprintf(subsys, IFNAMSIZ+5, "%u@%s", sc->sc_vhid,
194708b68b0eSGleb Smirnoff 		    sc->sc_carpdev->if_xname);
194808b68b0eSGleb Smirnoff 		devctl_notify("CARP", subsys, carp_states[state], NULL);
194908b68b0eSGleb Smirnoff 	}
195008b68b0eSGleb Smirnoff }
195108b68b0eSGleb Smirnoff 
195208b68b0eSGleb Smirnoff static void
195308b68b0eSGleb Smirnoff carp_linkstate(struct ifnet *ifp)
1954d220759bSGleb Smirnoff {
1955d220759bSGleb Smirnoff 	struct carp_softc *sc;
1956d220759bSGleb Smirnoff 
195708b68b0eSGleb Smirnoff 	CIF_LOCK(ifp->if_carp);
195808b68b0eSGleb Smirnoff 	IFNET_FOREACH_CARP(ifp, sc) {
195908b68b0eSGleb Smirnoff 		CARP_LOCK(sc);
196008b68b0eSGleb Smirnoff 		carp_sc_state(sc);
196108b68b0eSGleb Smirnoff 		CARP_UNLOCK(sc);
196208b68b0eSGleb Smirnoff 	}
196308b68b0eSGleb Smirnoff 	CIF_UNLOCK(ifp->if_carp);
19644cb39345SGleb Smirnoff }
19654cb39345SGleb Smirnoff 
19664cb39345SGleb Smirnoff static void
196708b68b0eSGleb Smirnoff carp_sc_state(struct carp_softc *sc)
19684cb39345SGleb Smirnoff {
196908b68b0eSGleb Smirnoff 
197008b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
19714cb39345SGleb Smirnoff 
1972e8c34a71SGleb Smirnoff 	if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1973e8c34a71SGleb Smirnoff 	    !(sc->sc_carpdev->if_flags & IFF_UP)) {
1974a9771948SGleb Smirnoff 		callout_stop(&sc->sc_ad_tmo);
197508b68b0eSGleb Smirnoff #ifdef INET
1976a9771948SGleb Smirnoff 		callout_stop(&sc->sc_md_tmo);
197708b68b0eSGleb Smirnoff #endif
197808b68b0eSGleb Smirnoff #ifdef INET6
1979a9771948SGleb Smirnoff 		callout_stop(&sc->sc_md6_tmo);
198008b68b0eSGleb Smirnoff #endif
1981a9771948SGleb Smirnoff 		carp_set_state(sc, INIT);
1982a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
1983f08535f8SGleb Smirnoff 		if (!sc->sc_suppress)
1984f08535f8SGleb Smirnoff 			carp_demote_adj(carp_ifdown_adj, "interface down");
1985a9771948SGleb Smirnoff 		sc->sc_suppress = 1;
1986a9771948SGleb Smirnoff 	} else {
1987a9771948SGleb Smirnoff 		carp_set_state(sc, INIT);
1988a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
1989a9771948SGleb Smirnoff 		if (sc->sc_suppress)
1990f08535f8SGleb Smirnoff 			carp_demote_adj(-carp_ifdown_adj, "interface up");
1991a9771948SGleb Smirnoff 		sc->sc_suppress = 0;
1992a9771948SGleb Smirnoff 	}
1993a9771948SGleb Smirnoff }
1994a9771948SGleb Smirnoff 
1995f08535f8SGleb Smirnoff static void
1996f08535f8SGleb Smirnoff carp_demote_adj(int adj, char *reason)
1997f08535f8SGleb Smirnoff {
19987951008bSGleb Smirnoff 	atomic_add_int(&carp_demotion, adj);
1999f08535f8SGleb Smirnoff 	CARP_LOG("demoted by %d to %d (%s)\n", adj, carp_demotion, reason);
2000f08535f8SGleb Smirnoff 	taskqueue_enqueue(taskqueue_swi, &carp_sendall_task);
2001f08535f8SGleb Smirnoff }
200208b68b0eSGleb Smirnoff 
20037951008bSGleb Smirnoff static int
20047951008bSGleb Smirnoff carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS)
20057951008bSGleb Smirnoff {
20067951008bSGleb Smirnoff 	int new, error;
20077951008bSGleb Smirnoff 
20087951008bSGleb Smirnoff 	new = carp_demotion;
20097951008bSGleb Smirnoff 	error = sysctl_handle_int(oidp, &new, 0, req);
20107951008bSGleb Smirnoff 	if (error || !req->newptr)
20117951008bSGleb Smirnoff 		return (error);
20127951008bSGleb Smirnoff 
20137951008bSGleb Smirnoff 	carp_demote_adj(new, "sysctl");
20147951008bSGleb Smirnoff 
20157951008bSGleb Smirnoff 	return (0);
20167951008bSGleb Smirnoff }
20177951008bSGleb Smirnoff 
201854bfbd51SWill Andrews #ifdef INET
201954bfbd51SWill Andrews extern  struct domain inetdomain;
202054bfbd51SWill Andrews static struct protosw in_carp_protosw = {
202154bfbd51SWill Andrews 	.pr_type =		SOCK_RAW,
202254bfbd51SWill Andrews 	.pr_domain =		&inetdomain,
202354bfbd51SWill Andrews 	.pr_protocol =		IPPROTO_CARP,
202454bfbd51SWill Andrews 	.pr_flags =		PR_ATOMIC|PR_ADDR,
202554bfbd51SWill Andrews 	.pr_input =		carp_input,
202654bfbd51SWill Andrews 	.pr_output =		(pr_output_t *)rip_output,
202754bfbd51SWill Andrews 	.pr_ctloutput =		rip_ctloutput,
202854bfbd51SWill Andrews 	.pr_usrreqs =		&rip_usrreqs
202954bfbd51SWill Andrews };
203054bfbd51SWill Andrews #endif
203154bfbd51SWill Andrews 
203254bfbd51SWill Andrews #ifdef INET6
203354bfbd51SWill Andrews extern	struct domain inet6domain;
203454bfbd51SWill Andrews static struct ip6protosw in6_carp_protosw = {
203554bfbd51SWill Andrews 	.pr_type =		SOCK_RAW,
203654bfbd51SWill Andrews 	.pr_domain =		&inet6domain,
203754bfbd51SWill Andrews 	.pr_protocol =		IPPROTO_CARP,
203854bfbd51SWill Andrews 	.pr_flags =		PR_ATOMIC|PR_ADDR,
203954bfbd51SWill Andrews 	.pr_input =		carp6_input,
204054bfbd51SWill Andrews 	.pr_output =		rip6_output,
204154bfbd51SWill Andrews 	.pr_ctloutput =		rip6_ctloutput,
204254bfbd51SWill Andrews 	.pr_usrreqs =		&rip6_usrreqs
204354bfbd51SWill Andrews };
204454bfbd51SWill Andrews #endif
204554bfbd51SWill Andrews 
204654bfbd51SWill Andrews static void
204754bfbd51SWill Andrews carp_mod_cleanup(void)
2048a9771948SGleb Smirnoff {
204954bfbd51SWill Andrews 
205054bfbd51SWill Andrews #ifdef INET
205154bfbd51SWill Andrews 	if (proto_reg[CARP_INET] == 0) {
205215249f73SWill Andrews 		(void)ipproto_unregister(IPPROTO_CARP);
205354bfbd51SWill Andrews 		pf_proto_unregister(PF_INET, IPPROTO_CARP, SOCK_RAW);
205454bfbd51SWill Andrews 		proto_reg[CARP_INET] = -1;
205554bfbd51SWill Andrews 	}
205654bfbd51SWill Andrews 	carp_iamatch_p = NULL;
205754bfbd51SWill Andrews #endif
205854bfbd51SWill Andrews #ifdef INET6
205954bfbd51SWill Andrews 	if (proto_reg[CARP_INET6] == 0) {
206015249f73SWill Andrews 		(void)ip6proto_unregister(IPPROTO_CARP);
206154bfbd51SWill Andrews 		pf_proto_unregister(PF_INET6, IPPROTO_CARP, SOCK_RAW);
206254bfbd51SWill Andrews 		proto_reg[CARP_INET6] = -1;
206354bfbd51SWill Andrews 	}
206454bfbd51SWill Andrews 	carp_iamatch6_p = NULL;
206554bfbd51SWill Andrews 	carp_macmatch6_p = NULL;
206654bfbd51SWill Andrews #endif
206708b68b0eSGleb Smirnoff 	carp_ioctl_p = NULL;
206808b68b0eSGleb Smirnoff 	carp_attach_p = NULL;
206908b68b0eSGleb Smirnoff 	carp_detach_p = NULL;
207008b68b0eSGleb Smirnoff 	carp_get_vhid_p = NULL;
207154bfbd51SWill Andrews 	carp_linkstate_p = NULL;
207254bfbd51SWill Andrews 	carp_forus_p = NULL;
207354bfbd51SWill Andrews 	carp_output_p = NULL;
2074f08535f8SGleb Smirnoff 	carp_demote_adj_p = NULL;
2075f08535f8SGleb Smirnoff 	mtx_unlock(&carp_mtx);
2076f08535f8SGleb Smirnoff 	taskqueue_drain(taskqueue_swi, &carp_sendall_task);
207754bfbd51SWill Andrews 	mtx_destroy(&carp_mtx);
207854bfbd51SWill Andrews }
207954bfbd51SWill Andrews 
208054bfbd51SWill Andrews static int
208154bfbd51SWill Andrews carp_mod_load(void)
208254bfbd51SWill Andrews {
208315249f73SWill Andrews 	int err;
208454bfbd51SWill Andrews 
2085d92d54d5SGleb Smirnoff 	mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
208608b68b0eSGleb Smirnoff 	LIST_INIT(&carp_list);
208708b68b0eSGleb Smirnoff 	carp_get_vhid_p = carp_get_vhid;
208854bfbd51SWill Andrews 	carp_forus_p = carp_forus;
208954bfbd51SWill Andrews 	carp_output_p = carp_output;
209008b68b0eSGleb Smirnoff 	carp_linkstate_p = carp_linkstate;
209108b68b0eSGleb Smirnoff 	carp_ioctl_p = carp_ioctl;
209208b68b0eSGleb Smirnoff 	carp_attach_p = carp_attach;
209308b68b0eSGleb Smirnoff 	carp_detach_p = carp_detach;
2094f08535f8SGleb Smirnoff 	carp_demote_adj_p = carp_demote_adj;
209554bfbd51SWill Andrews #ifdef INET6
209654bfbd51SWill Andrews 	carp_iamatch6_p = carp_iamatch6;
209754bfbd51SWill Andrews 	carp_macmatch6_p = carp_macmatch6;
209854bfbd51SWill Andrews 	proto_reg[CARP_INET6] = pf_proto_register(PF_INET6,
209954bfbd51SWill Andrews 	    (struct protosw *)&in6_carp_protosw);
210008b68b0eSGleb Smirnoff 	if (proto_reg[CARP_INET6]) {
210154bfbd51SWill Andrews 		printf("carp: error %d attaching to PF_INET6\n",
210254bfbd51SWill Andrews 		    proto_reg[CARP_INET6]);
210354bfbd51SWill Andrews 		carp_mod_cleanup();
21046baf7a24SGleb Smirnoff 		return (proto_reg[CARP_INET6]);
210554bfbd51SWill Andrews 	}
210615249f73SWill Andrews 	err = ip6proto_register(IPPROTO_CARP);
210715249f73SWill Andrews 	if (err) {
210815249f73SWill Andrews 		printf("carp: error %d registering with INET6\n", err);
210915249f73SWill Andrews 		carp_mod_cleanup();
21106baf7a24SGleb Smirnoff 		return (err);
211115249f73SWill Andrews 	}
211254bfbd51SWill Andrews #endif
211354bfbd51SWill Andrews #ifdef INET
211454bfbd51SWill Andrews 	carp_iamatch_p = carp_iamatch;
211554bfbd51SWill Andrews 	proto_reg[CARP_INET] = pf_proto_register(PF_INET, &in_carp_protosw);
211608b68b0eSGleb Smirnoff 	if (proto_reg[CARP_INET]) {
211754bfbd51SWill Andrews 		printf("carp: error %d attaching to PF_INET\n",
211854bfbd51SWill Andrews 		    proto_reg[CARP_INET]);
211954bfbd51SWill Andrews 		carp_mod_cleanup();
21206baf7a24SGleb Smirnoff 		return (proto_reg[CARP_INET]);
212154bfbd51SWill Andrews 	}
212215249f73SWill Andrews 	err = ipproto_register(IPPROTO_CARP);
212315249f73SWill Andrews 	if (err) {
212415249f73SWill Andrews 		printf("carp: error %d registering with INET\n", err);
212515249f73SWill Andrews 		carp_mod_cleanup();
21266baf7a24SGleb Smirnoff 		return (err);
212715249f73SWill Andrews 	}
212854bfbd51SWill Andrews #endif
212908b68b0eSGleb Smirnoff 	return (0);
213054bfbd51SWill Andrews }
2131a9771948SGleb Smirnoff 
213254bfbd51SWill Andrews static int
213354bfbd51SWill Andrews carp_modevent(module_t mod, int type, void *data)
213454bfbd51SWill Andrews {
213554bfbd51SWill Andrews 	switch (type) {
213654bfbd51SWill Andrews 	case MOD_LOAD:
213754bfbd51SWill Andrews 		return carp_mod_load();
213854bfbd51SWill Andrews 		/* NOTREACHED */
2139a9771948SGleb Smirnoff 	case MOD_UNLOAD:
214008b68b0eSGleb Smirnoff 		mtx_lock(&carp_mtx);
214108b68b0eSGleb Smirnoff 		if (LIST_EMPTY(&carp_list))
214254bfbd51SWill Andrews 			carp_mod_cleanup();
214308b68b0eSGleb Smirnoff 		else {
214408b68b0eSGleb Smirnoff 			mtx_unlock(&carp_mtx);
214554bfbd51SWill Andrews 			return (EBUSY);
214608b68b0eSGleb Smirnoff 		}
2147a9771948SGleb Smirnoff 		break;
2148a9771948SGleb Smirnoff 
2149a9771948SGleb Smirnoff 	default:
21500fa08018SGleb Smirnoff 		return (EINVAL);
2151a9771948SGleb Smirnoff 	}
2152a9771948SGleb Smirnoff 
21530fa08018SGleb Smirnoff 	return (0);
2154a9771948SGleb Smirnoff }
2155a9771948SGleb Smirnoff 
2156a9771948SGleb Smirnoff static moduledata_t carp_mod = {
2157a9771948SGleb Smirnoff 	"carp",
2158a9771948SGleb Smirnoff 	carp_modevent,
21599823d527SKevin Lo 	0
2160a9771948SGleb Smirnoff };
2161a9771948SGleb Smirnoff 
2162e24fa11dSWill Andrews DECLARE_MODULE(carp, carp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
2163