xref: /freebsd/sys/netinet/ip_carp.c (revision 2512b096a2d5621bd4cd81211a0e8c013cc5cef1)
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 */
195a9771948SGleb Smirnoff 
196f08535f8SGleb Smirnoff SYSCTL_NODE(_net_inet, IPPROTO_CARP,	carp,	CTLFLAG_RW, 0,	"CARP");
197f08535f8SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, allow, CTLFLAG_RW, &carp_allow, 0,
198f08535f8SGleb Smirnoff     "Accept incoming CARP packets");
199f08535f8SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, preempt, CTLFLAG_RW, &carp_preempt, 0,
200f08535f8SGleb Smirnoff     "High-priority backup preemption mode");
201f08535f8SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, log, CTLFLAG_RW, &carp_log, 0,
202f08535f8SGleb Smirnoff     "CARP log level");
203f08535f8SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, demotion, CTLFLAG_RW, &carp_demotion, 0,
204f08535f8SGleb Smirnoff     "Demotion factor (skew of advskew)");
205f08535f8SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, senderr_demotion_factor, CTLFLAG_RW,
206f08535f8SGleb Smirnoff     &carp_senderr_adj, 0, "Send error demotion factor adjustment");
207f08535f8SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, ifdown_demotion_factor, CTLFLAG_RW,
208f08535f8SGleb Smirnoff     &carp_ifdown_adj, 0, "Interface down demotion factor adjustment");
209f08535f8SGleb Smirnoff 
210f08535f8SGleb Smirnoff static struct carpstats carpstats;
211f08535f8SGleb Smirnoff SYSCTL_STRUCT(_net_inet_carp, OID_AUTO, stats, CTLFLAG_RW, &carpstats,
212f08535f8SGleb Smirnoff     carpstats, "CARP statistics (struct carpstats, netinet/ip_carp.h)");
213a9771948SGleb Smirnoff 
21408b68b0eSGleb Smirnoff #define	CARP_LOCK_INIT(sc)	mtx_init(&(sc)->sc_mtx, "carp_softc",   \
215a9771948SGleb Smirnoff 	NULL, MTX_DEF)
21608b68b0eSGleb Smirnoff #define	CARP_LOCK_DESTROY(sc)	mtx_destroy(&(sc)->sc_mtx)
21708b68b0eSGleb Smirnoff #define	CARP_LOCK_ASSERT(sc)	mtx_assert(&(sc)->sc_mtx, MA_OWNED)
21808b68b0eSGleb Smirnoff #define	CARP_LOCK(sc)		mtx_lock(&(sc)->sc_mtx)
21908b68b0eSGleb Smirnoff #define	CARP_UNLOCK(sc)		mtx_unlock(&(sc)->sc_mtx)
22008b68b0eSGleb Smirnoff #define	CIF_LOCK_INIT(cif)	mtx_init(&(cif)->cif_mtx, "carp_if",   \
22108b68b0eSGleb Smirnoff 	NULL, MTX_DEF)
22208b68b0eSGleb Smirnoff #define	CIF_LOCK_DESTROY(cif)	mtx_destroy(&(cif)->cif_mtx)
22308b68b0eSGleb Smirnoff #define	CIF_LOCK_ASSERT(cif)	mtx_assert(&(cif)->cif_mtx, MA_OWNED)
22408b68b0eSGleb Smirnoff #define	CIF_LOCK(cif)		mtx_lock(&(cif)->cif_mtx)
22508b68b0eSGleb Smirnoff #define	CIF_UNLOCK(cif)		mtx_unlock(&(cif)->cif_mtx)
226d220759bSGleb Smirnoff 
227947b7cf3SGleb Smirnoff #define	CARP_LOG(...)	do {				\
228f08535f8SGleb Smirnoff 	if (carp_log > 0)				\
22908b68b0eSGleb Smirnoff 		log(LOG_INFO, "carp: " __VA_ARGS__);	\
230947b7cf3SGleb Smirnoff } while (0)
2311e9e6572SGleb Smirnoff 
232947b7cf3SGleb Smirnoff #define	CARP_DEBUG(...)	do {				\
233f08535f8SGleb Smirnoff 	if (carp_log > 1)				\
2341e9e6572SGleb Smirnoff 		log(LOG_DEBUG, __VA_ARGS__);		\
235947b7cf3SGleb Smirnoff } while (0)
236a9771948SGleb Smirnoff 
23708b68b0eSGleb Smirnoff #define	IFNET_FOREACH_IFA(ifp, ifa)					\
23808b68b0eSGleb Smirnoff 	IF_ADDR_LOCK_ASSERT(ifp);					\
23908b68b0eSGleb Smirnoff 	TAILQ_FOREACH((ifa), &(ifp)->if_addrhead, ifa_link)		\
24008b68b0eSGleb Smirnoff 		if ((ifa)->ifa_carp != NULL)
24108b68b0eSGleb Smirnoff 
24208b68b0eSGleb Smirnoff #define	CARP_FOREACH_IFA(sc, ifa)					\
24308b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);						\
24408b68b0eSGleb Smirnoff 	for (int _i = 0;						\
24508b68b0eSGleb Smirnoff 		_i < (sc)->sc_naddrs + (sc)->sc_naddrs6 &&		\
24608b68b0eSGleb Smirnoff 		((ifa) = sc->sc_ifas[_i]) != NULL;			\
24708b68b0eSGleb Smirnoff 		++_i)
24808b68b0eSGleb Smirnoff 
24908b68b0eSGleb Smirnoff #define	IFNET_FOREACH_CARP(ifp, sc)					\
25008b68b0eSGleb Smirnoff 	CIF_LOCK_ASSERT(ifp->if_carp);					\
25108b68b0eSGleb Smirnoff 	TAILQ_FOREACH((sc), &(ifp)->if_carp->cif_vrs, sc_list)
25208b68b0eSGleb Smirnoff 
253f08535f8SGleb Smirnoff #define	DEMOTE_ADVSKEW(sc)					\
254f08535f8SGleb Smirnoff     (((sc)->sc_advskew + carp_demotion > CARP_MAXSKEW) ?	\
255f08535f8SGleb Smirnoff     CARP_MAXSKEW : ((sc)->sc_advskew + carp_demotion))
256f08535f8SGleb Smirnoff 
2575c1f0f6dSGleb Smirnoff static void	carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
25808b68b0eSGleb Smirnoff static struct carp_softc
25908b68b0eSGleb Smirnoff 		*carp_alloc(struct ifnet *);
26008b68b0eSGleb Smirnoff static void	carp_destroy(struct carp_softc *);
26108b68b0eSGleb Smirnoff static struct carp_if
26208b68b0eSGleb Smirnoff 		*carp_alloc_if(struct ifnet *);
26308b68b0eSGleb Smirnoff static void	carp_free_if(struct carp_if *);
26408b68b0eSGleb Smirnoff static void	carp_set_state(struct carp_softc *, int);
26508b68b0eSGleb Smirnoff static void	carp_sc_state(struct carp_softc *);
26608b68b0eSGleb Smirnoff static void	carp_setrun(struct carp_softc *, sa_family_t);
2675c1f0f6dSGleb Smirnoff static void	carp_master_down(void *);
268d220759bSGleb Smirnoff static void	carp_master_down_locked(struct carp_softc *);
26908b68b0eSGleb Smirnoff static void	carp_send_ad(void *);
27008b68b0eSGleb Smirnoff static void	carp_send_ad_locked(struct carp_softc *);
27108b68b0eSGleb Smirnoff static void	carp_addroute(struct carp_softc *);
272*2512b096SGleb Smirnoff static void	carp_ifa_addroute(struct ifaddr *);
27308b68b0eSGleb Smirnoff static void	carp_delroute(struct carp_softc *);
274*2512b096SGleb Smirnoff static void	carp_ifa_delroute(struct ifaddr *);
275f08535f8SGleb Smirnoff static void	carp_send_ad_all(void *, int);
276f08535f8SGleb Smirnoff static void	carp_demote_adj(int, char *);
277a9771948SGleb Smirnoff 
27808b68b0eSGleb Smirnoff static LIST_HEAD(, carp_softc) carp_list;
279d92d54d5SGleb Smirnoff static struct mtx carp_mtx;
280f08535f8SGleb Smirnoff static struct task carp_sendall_task =
281f08535f8SGleb Smirnoff     TASK_INITIALIZER(0, carp_send_ad_all, NULL);
282a9771948SGleb Smirnoff 
28308b68b0eSGleb Smirnoff static __inline uint16_t
284a9771948SGleb Smirnoff carp_cksum(struct mbuf *m, int len)
285a9771948SGleb Smirnoff {
286a9771948SGleb Smirnoff 	return (in_cksum(m, len));
287a9771948SGleb Smirnoff }
288a9771948SGleb Smirnoff 
2895c1f0f6dSGleb Smirnoff static void
290a9771948SGleb Smirnoff carp_hmac_prepare(struct carp_softc *sc)
291a9771948SGleb Smirnoff {
29208b68b0eSGleb Smirnoff 	uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
29308b68b0eSGleb Smirnoff 	uint8_t vhid = sc->sc_vhid & 0xff;
294a9771948SGleb Smirnoff 	struct ifaddr *ifa;
2951ead26d4SMax Laier 	int i, found;
2961ead26d4SMax Laier #ifdef INET
2971ead26d4SMax Laier 	struct in_addr last, cur, in;
2981ead26d4SMax Laier #endif
299a9771948SGleb Smirnoff #ifdef INET6
3001ead26d4SMax Laier 	struct in6_addr last6, cur6, in6;
301a9771948SGleb Smirnoff #endif
302a9771948SGleb Smirnoff 
30308b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
304d220759bSGleb Smirnoff 
30508b68b0eSGleb Smirnoff 	/* Compute ipad from key. */
306a9771948SGleb Smirnoff 	bzero(sc->sc_pad, sizeof(sc->sc_pad));
307a9771948SGleb Smirnoff 	bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
308a9771948SGleb Smirnoff 	for (i = 0; i < sizeof(sc->sc_pad); i++)
309a9771948SGleb Smirnoff 		sc->sc_pad[i] ^= 0x36;
310a9771948SGleb Smirnoff 
31108b68b0eSGleb Smirnoff 	/* Precompute first part of inner hash. */
312a9771948SGleb Smirnoff 	SHA1Init(&sc->sc_sha1);
313a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
314a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
315a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
316a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
317a9771948SGleb Smirnoff #ifdef INET
3181ead26d4SMax Laier 	cur.s_addr = 0;
3191ead26d4SMax Laier 	do {
3201ead26d4SMax Laier 		found = 0;
3211ead26d4SMax Laier 		last = cur;
3221ead26d4SMax Laier 		cur.s_addr = 0xffffffff;
32308b68b0eSGleb Smirnoff 		CARP_FOREACH_IFA(sc, ifa) {
3241ead26d4SMax Laier 			in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr;
3251ead26d4SMax Laier 			if (ifa->ifa_addr->sa_family == AF_INET &&
3261ead26d4SMax Laier 			    ntohl(in.s_addr) > ntohl(last.s_addr) &&
3271ead26d4SMax Laier 			    ntohl(in.s_addr) < ntohl(cur.s_addr)) {
3281ead26d4SMax Laier 				cur.s_addr = in.s_addr;
3291ead26d4SMax Laier 				found++;
330a9771948SGleb Smirnoff 			}
3311ead26d4SMax Laier 		}
3321ead26d4SMax Laier 		if (found)
3331ead26d4SMax Laier 			SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur));
3341ead26d4SMax Laier 	} while (found);
335a9771948SGleb Smirnoff #endif /* INET */
336a9771948SGleb Smirnoff #ifdef INET6
3371ead26d4SMax Laier 	memset(&cur6, 0, sizeof(cur6));
3381ead26d4SMax Laier 	do {
3391ead26d4SMax Laier 		found = 0;
3401ead26d4SMax Laier 		last6 = cur6;
3411ead26d4SMax Laier 		memset(&cur6, 0xff, sizeof(cur6));
34208b68b0eSGleb Smirnoff 		CARP_FOREACH_IFA(sc, ifa) {
343a9771948SGleb Smirnoff 			in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
3441ead26d4SMax Laier 			if (IN6_IS_SCOPE_EMBED(&in6))
3451ead26d4SMax Laier 				in6.s6_addr16[1] = 0;
3461ead26d4SMax Laier 			if (ifa->ifa_addr->sa_family == AF_INET6 &&
3471ead26d4SMax Laier 			    memcmp(&in6, &last6, sizeof(in6)) > 0 &&
3481ead26d4SMax Laier 			    memcmp(&in6, &cur6, sizeof(in6)) < 0) {
3491ead26d4SMax Laier 				cur6 = in6;
3501ead26d4SMax Laier 				found++;
351a9771948SGleb Smirnoff 			}
352a9771948SGleb Smirnoff 		}
3531ead26d4SMax Laier 		if (found)
3541ead26d4SMax Laier 			SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6));
3551ead26d4SMax Laier 	} while (found);
356a9771948SGleb Smirnoff #endif /* INET6 */
357a9771948SGleb Smirnoff 
358a9771948SGleb Smirnoff 	/* convert ipad to opad */
359a9771948SGleb Smirnoff 	for (i = 0; i < sizeof(sc->sc_pad); i++)
360a9771948SGleb Smirnoff 		sc->sc_pad[i] ^= 0x36 ^ 0x5c;
361a9771948SGleb Smirnoff }
362a9771948SGleb Smirnoff 
3635c1f0f6dSGleb Smirnoff static void
36408b68b0eSGleb Smirnoff carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
365a9771948SGleb Smirnoff     unsigned char md[20])
366a9771948SGleb Smirnoff {
367a9771948SGleb Smirnoff 	SHA1_CTX sha1ctx;
368a9771948SGleb Smirnoff 
36908b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
37008b68b0eSGleb Smirnoff 
371a9771948SGleb Smirnoff 	/* fetch first half of inner hash */
372a9771948SGleb Smirnoff 	bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
373a9771948SGleb Smirnoff 
374a9771948SGleb Smirnoff 	SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
375a9771948SGleb Smirnoff 	SHA1Final(md, &sha1ctx);
376a9771948SGleb Smirnoff 
377a9771948SGleb Smirnoff 	/* outer hash */
378a9771948SGleb Smirnoff 	SHA1Init(&sha1ctx);
379a9771948SGleb Smirnoff 	SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
380a9771948SGleb Smirnoff 	SHA1Update(&sha1ctx, md, 20);
381a9771948SGleb Smirnoff 	SHA1Final(md, &sha1ctx);
382a9771948SGleb Smirnoff }
383a9771948SGleb Smirnoff 
3845c1f0f6dSGleb Smirnoff static int
38508b68b0eSGleb Smirnoff carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
386a9771948SGleb Smirnoff     unsigned char md[20])
387a9771948SGleb Smirnoff {
388a9771948SGleb Smirnoff 	unsigned char md2[20];
389a9771948SGleb Smirnoff 
39008b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
391d220759bSGleb Smirnoff 
392a9771948SGleb Smirnoff 	carp_hmac_generate(sc, counter, md2);
393a9771948SGleb Smirnoff 
394a9771948SGleb Smirnoff 	return (bcmp(md, md2, sizeof(md2)));
395a9771948SGleb Smirnoff }
396a9771948SGleb Smirnoff 
397a9771948SGleb Smirnoff /*
398a9771948SGleb Smirnoff  * process input packet.
399a9771948SGleb Smirnoff  * we have rearranged checks order compared to the rfc,
400a9771948SGleb Smirnoff  * but it seems more efficient this way or not possible otherwise.
401a9771948SGleb Smirnoff  */
402a0ae8f04SBjoern A. Zeeb #ifdef INET
403a9771948SGleb Smirnoff void
404a9771948SGleb Smirnoff carp_input(struct mbuf *m, int hlen)
405a9771948SGleb Smirnoff {
406a9771948SGleb Smirnoff 	struct ip *ip = mtod(m, struct ip *);
407a9771948SGleb Smirnoff 	struct carp_header *ch;
408a9771948SGleb Smirnoff 	int iplen, len;
409a9771948SGleb Smirnoff 
4106bf65bcfSRobert Watson 	CARPSTATS_INC(carps_ipackets);
411a9771948SGleb Smirnoff 
412f08535f8SGleb Smirnoff 	if (!carp_allow) {
413a9771948SGleb Smirnoff 		m_freem(m);
414a9771948SGleb Smirnoff 		return;
415a9771948SGleb Smirnoff 	}
416a9771948SGleb Smirnoff 
417a9771948SGleb Smirnoff 	/* verify that the IP TTL is 255.  */
418a9771948SGleb Smirnoff 	if (ip->ip_ttl != CARP_DFLTTL) {
4196bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badttl);
42008b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
4211e9e6572SGleb Smirnoff 		    ip->ip_ttl,
4221e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
423a9771948SGleb Smirnoff 		m_freem(m);
424a9771948SGleb Smirnoff 		return;
425a9771948SGleb Smirnoff 	}
426a9771948SGleb Smirnoff 
427a9771948SGleb Smirnoff 	iplen = ip->ip_hl << 2;
428a9771948SGleb Smirnoff 
429a9771948SGleb Smirnoff 	if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
4306bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badlen);
43108b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: received len %zd < sizeof(struct carp_header) "
43208b68b0eSGleb Smirnoff 		    "on %s\n", __func__, m->m_len - sizeof(struct ip),
43316e324fcSXin LI 		    m->m_pkthdr.rcvif->if_xname);
434a9771948SGleb Smirnoff 		m_freem(m);
435a9771948SGleb Smirnoff 		return;
436a9771948SGleb Smirnoff 	}
437a9771948SGleb Smirnoff 
438a9771948SGleb Smirnoff 	if (iplen + sizeof(*ch) < m->m_len) {
439a9771948SGleb Smirnoff 		if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
4406bf65bcfSRobert Watson 			CARPSTATS_INC(carps_hdrops);
44108b68b0eSGleb Smirnoff 			CARP_DEBUG("%s: pullup failed\n", __func__);
442a9771948SGleb Smirnoff 			return;
443a9771948SGleb Smirnoff 		}
444a9771948SGleb Smirnoff 		ip = mtod(m, struct ip *);
445a9771948SGleb Smirnoff 	}
446a9771948SGleb Smirnoff 	ch = (struct carp_header *)((char *)ip + iplen);
447a9771948SGleb Smirnoff 
448a9771948SGleb Smirnoff 	/*
449a9771948SGleb Smirnoff 	 * verify that the received packet length is
450a9771948SGleb Smirnoff 	 * equal to the CARP header
451a9771948SGleb Smirnoff 	 */
452a9771948SGleb Smirnoff 	len = iplen + sizeof(*ch);
453a9771948SGleb Smirnoff 	if (len > m->m_pkthdr.len) {
4546bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badlen);
45508b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: packet too short %d on %s\n", __func__,
4561e9e6572SGleb Smirnoff 		    m->m_pkthdr.len,
4571e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
458a9771948SGleb Smirnoff 		m_freem(m);
459a9771948SGleb Smirnoff 		return;
460a9771948SGleb Smirnoff 	}
461a9771948SGleb Smirnoff 
462a9771948SGleb Smirnoff 	if ((m = m_pullup(m, len)) == NULL) {
4636bf65bcfSRobert Watson 		CARPSTATS_INC(carps_hdrops);
464a9771948SGleb Smirnoff 		return;
465a9771948SGleb Smirnoff 	}
466a9771948SGleb Smirnoff 	ip = mtod(m, struct ip *);
467a9771948SGleb Smirnoff 	ch = (struct carp_header *)((char *)ip + iplen);
468a9771948SGleb Smirnoff 
469a9771948SGleb Smirnoff 	/* verify the CARP checksum */
470a9771948SGleb Smirnoff 	m->m_data += iplen;
471a9771948SGleb Smirnoff 	if (carp_cksum(m, len - iplen)) {
4726bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badsum);
47308b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: checksum failed on %s\n", __func__,
4741e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
475a9771948SGleb Smirnoff 		m_freem(m);
476a9771948SGleb Smirnoff 		return;
477a9771948SGleb Smirnoff 	}
478a9771948SGleb Smirnoff 	m->m_data -= iplen;
479a9771948SGleb Smirnoff 
4801e9e6572SGleb Smirnoff 	carp_input_c(m, ch, AF_INET);
481a9771948SGleb Smirnoff }
482a0ae8f04SBjoern A. Zeeb #endif
483a9771948SGleb Smirnoff 
484a9771948SGleb Smirnoff #ifdef INET6
485a9771948SGleb Smirnoff int
486a9771948SGleb Smirnoff carp6_input(struct mbuf **mp, int *offp, int proto)
487a9771948SGleb Smirnoff {
488a9771948SGleb Smirnoff 	struct mbuf *m = *mp;
489a9771948SGleb Smirnoff 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
490a9771948SGleb Smirnoff 	struct carp_header *ch;
491a9771948SGleb Smirnoff 	u_int len;
492a9771948SGleb Smirnoff 
4936bf65bcfSRobert Watson 	CARPSTATS_INC(carps_ipackets6);
494a9771948SGleb Smirnoff 
495f08535f8SGleb Smirnoff 	if (!carp_allow) {
496a9771948SGleb Smirnoff 		m_freem(m);
497a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
498a9771948SGleb Smirnoff 	}
499a9771948SGleb Smirnoff 
500a9771948SGleb Smirnoff 	/* check if received on a valid carp interface */
501a9771948SGleb Smirnoff 	if (m->m_pkthdr.rcvif->if_carp == NULL) {
5026bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badif);
50308b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: packet received on non-carp interface: %s\n",
50408b68b0eSGleb Smirnoff 		    __func__, m->m_pkthdr.rcvif->if_xname);
505a9771948SGleb Smirnoff 		m_freem(m);
506a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
507a9771948SGleb Smirnoff 	}
508a9771948SGleb Smirnoff 
509a9771948SGleb Smirnoff 	/* verify that the IP TTL is 255 */
510a9771948SGleb Smirnoff 	if (ip6->ip6_hlim != CARP_DFLTTL) {
5116bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badttl);
51208b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
51308b68b0eSGleb Smirnoff 		    ip6->ip6_hlim, m->m_pkthdr.rcvif->if_xname);
514a9771948SGleb Smirnoff 		m_freem(m);
515a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
516a9771948SGleb Smirnoff 	}
517a9771948SGleb Smirnoff 
518a9771948SGleb Smirnoff 	/* verify that we have a complete carp packet */
519a9771948SGleb Smirnoff 	len = m->m_len;
520a9771948SGleb Smirnoff 	IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
521a9771948SGleb Smirnoff 	if (ch == NULL) {
5226bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badlen);
52308b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: packet size %u too small\n", __func__, len);
524a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
525a9771948SGleb Smirnoff 	}
526a9771948SGleb Smirnoff 
527a9771948SGleb Smirnoff 
528a9771948SGleb Smirnoff 	/* verify the CARP checksum */
529a9771948SGleb Smirnoff 	m->m_data += *offp;
530a9771948SGleb Smirnoff 	if (carp_cksum(m, sizeof(*ch))) {
5316bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badsum);
53208b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: checksum failed, on %s\n", __func__,
5331e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
534a9771948SGleb Smirnoff 		m_freem(m);
535a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
536a9771948SGleb Smirnoff 	}
537a9771948SGleb Smirnoff 	m->m_data -= *offp;
538a9771948SGleb Smirnoff 
5391e9e6572SGleb Smirnoff 	carp_input_c(m, ch, AF_INET6);
540a9771948SGleb Smirnoff 	return (IPPROTO_DONE);
541a9771948SGleb Smirnoff }
542a9771948SGleb Smirnoff #endif /* INET6 */
543a9771948SGleb Smirnoff 
5445c1f0f6dSGleb Smirnoff static void
5451e9e6572SGleb Smirnoff carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
546a9771948SGleb Smirnoff {
547a9771948SGleb Smirnoff 	struct ifnet *ifp = m->m_pkthdr.rcvif;
54808b68b0eSGleb Smirnoff 	struct ifaddr *ifa;
5491e9e6572SGleb Smirnoff 	struct carp_softc *sc;
55008b68b0eSGleb Smirnoff 	uint64_t tmp_counter;
551a9771948SGleb Smirnoff 	struct timeval sc_tv, ch_tv;
552a9771948SGleb Smirnoff 
553a9771948SGleb Smirnoff 	/* verify that the VHID is valid on the receiving interface */
554137f91e8SJohn Baldwin 	IF_ADDR_RLOCK(ifp);
55508b68b0eSGleb Smirnoff 	IFNET_FOREACH_IFA(ifp, ifa)
55608b68b0eSGleb Smirnoff 		if (ifa->ifa_addr->sa_family == af &&
55708b68b0eSGleb Smirnoff 		    ifa->ifa_carp->sc_vhid == ch->carp_vhid) {
55808b68b0eSGleb Smirnoff 			ifa_ref(ifa);
559a9771948SGleb Smirnoff 			break;
56008b68b0eSGleb Smirnoff 		}
561137f91e8SJohn Baldwin 	IF_ADDR_RUNLOCK(ifp);
562d220759bSGleb Smirnoff 
56308b68b0eSGleb Smirnoff 	if (ifa == NULL) {
5646bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badvhid);
565a9771948SGleb Smirnoff 		m_freem(m);
566a9771948SGleb Smirnoff 		return;
567a9771948SGleb Smirnoff 	}
568a9771948SGleb Smirnoff 
569a9771948SGleb Smirnoff 	/* verify the CARP version. */
570a9771948SGleb Smirnoff 	if (ch->carp_version != CARP_VERSION) {
5716bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badver);
57208b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: invalid version %d\n", ifp->if_xname,
5731e9e6572SGleb Smirnoff 		    ch->carp_version);
57408b68b0eSGleb Smirnoff 		ifa_free(ifa);
575a9771948SGleb Smirnoff 		m_freem(m);
576a9771948SGleb Smirnoff 		return;
577a9771948SGleb Smirnoff 	}
578a9771948SGleb Smirnoff 
57908b68b0eSGleb Smirnoff 	sc = ifa->ifa_carp;
58008b68b0eSGleb Smirnoff 	CARP_LOCK(sc);
58108b68b0eSGleb Smirnoff 	ifa_free(ifa);
58208b68b0eSGleb Smirnoff 
583a9771948SGleb Smirnoff 	if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
5846bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badauth);
58508b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: incorrect hash for VHID %u@%s\n", __func__,
58608b68b0eSGleb Smirnoff 		    sc->sc_vhid, ifp->if_xname);
58708b68b0eSGleb Smirnoff 		goto out;
588a9771948SGleb Smirnoff 	}
589a9771948SGleb Smirnoff 
590a9771948SGleb Smirnoff 	tmp_counter = ntohl(ch->carp_counter[0]);
591a9771948SGleb Smirnoff 	tmp_counter = tmp_counter<<32;
592a9771948SGleb Smirnoff 	tmp_counter += ntohl(ch->carp_counter[1]);
593a9771948SGleb Smirnoff 
594a9771948SGleb Smirnoff 	/* XXX Replay protection goes here */
595a9771948SGleb Smirnoff 
596a9771948SGleb Smirnoff 	sc->sc_init_counter = 0;
597a9771948SGleb Smirnoff 	sc->sc_counter = tmp_counter;
598a9771948SGleb Smirnoff 
599a9771948SGleb Smirnoff 	sc_tv.tv_sec = sc->sc_advbase;
600f08535f8SGleb Smirnoff 	sc_tv.tv_usec = DEMOTE_ADVSKEW(sc) * 1000000 / 256;
601a9771948SGleb Smirnoff 	ch_tv.tv_sec = ch->carp_advbase;
602a9771948SGleb Smirnoff 	ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
603a9771948SGleb Smirnoff 
604a9771948SGleb Smirnoff 	switch (sc->sc_state) {
605a9771948SGleb Smirnoff 	case INIT:
606a9771948SGleb Smirnoff 		break;
607a9771948SGleb Smirnoff 	case MASTER:
608a9771948SGleb Smirnoff 		/*
609a9771948SGleb Smirnoff 		 * If we receive an advertisement from a master who's going to
610a9771948SGleb Smirnoff 		 * be more frequent than us, go into BACKUP state.
611a9771948SGleb Smirnoff 		 */
612a9771948SGleb Smirnoff 		if (timevalcmp(&sc_tv, &ch_tv, >) ||
613a9771948SGleb Smirnoff 		    timevalcmp(&sc_tv, &ch_tv, ==)) {
614a9771948SGleb Smirnoff 			callout_stop(&sc->sc_ad_tmo);
61508b68b0eSGleb Smirnoff 			CARP_LOG("VHID %u@%s: MASTER -> BACKUP "
61688bf82a6SGleb Smirnoff 			    "(more frequent advertisement received)\n",
61708b68b0eSGleb Smirnoff 			    sc->sc_vhid,
61808b68b0eSGleb Smirnoff 			    sc->sc_carpdev->if_xname);
619a9771948SGleb Smirnoff 			carp_set_state(sc, BACKUP);
620a9771948SGleb Smirnoff 			carp_setrun(sc, 0);
62108b68b0eSGleb Smirnoff 			carp_delroute(sc);
622a9771948SGleb Smirnoff 		}
623a9771948SGleb Smirnoff 		break;
624a9771948SGleb Smirnoff 	case BACKUP:
625a9771948SGleb Smirnoff 		/*
626a9771948SGleb Smirnoff 		 * If we're pre-empting masters who advertise slower than us,
627a9771948SGleb Smirnoff 		 * and this one claims to be slower, treat him as down.
628a9771948SGleb Smirnoff 		 */
629f08535f8SGleb Smirnoff 		if (carp_preempt && timevalcmp(&sc_tv, &ch_tv, <)) {
63008b68b0eSGleb Smirnoff 			CARP_LOG("VHID %u@%s: BACKUP -> MASTER "
63188bf82a6SGleb Smirnoff 			    "(preempting a slower master)\n",
63208b68b0eSGleb Smirnoff 			    sc->sc_vhid,
63308b68b0eSGleb Smirnoff 			    sc->sc_carpdev->if_xname);
634d220759bSGleb Smirnoff 			carp_master_down_locked(sc);
635a9771948SGleb Smirnoff 			break;
636a9771948SGleb Smirnoff 		}
637a9771948SGleb Smirnoff 
638a9771948SGleb Smirnoff 		/*
639a9771948SGleb Smirnoff 		 *  If the master is going to advertise at such a low frequency
640a9771948SGleb Smirnoff 		 *  that he's guaranteed to time out, we'd might as well just
641a9771948SGleb Smirnoff 		 *  treat him as timed out now.
642a9771948SGleb Smirnoff 		 */
643a9771948SGleb Smirnoff 		sc_tv.tv_sec = sc->sc_advbase * 3;
644a9771948SGleb Smirnoff 		if (timevalcmp(&sc_tv, &ch_tv, <)) {
64508b68b0eSGleb Smirnoff 			CARP_LOG("VHID %u@%s: BACKUP -> MASTER "
64688bf82a6SGleb Smirnoff 			    "(master timed out)\n",
64708b68b0eSGleb Smirnoff 			    sc->sc_vhid,
64808b68b0eSGleb Smirnoff 			    sc->sc_carpdev->if_xname);
649d220759bSGleb Smirnoff 			carp_master_down_locked(sc);
650a9771948SGleb Smirnoff 			break;
651a9771948SGleb Smirnoff 		}
652a9771948SGleb Smirnoff 
653a9771948SGleb Smirnoff 		/*
654a9771948SGleb Smirnoff 		 * Otherwise, we reset the counter and wait for the next
655a9771948SGleb Smirnoff 		 * advertisement.
656a9771948SGleb Smirnoff 		 */
657a9771948SGleb Smirnoff 		carp_setrun(sc, af);
658a9771948SGleb Smirnoff 		break;
659a9771948SGleb Smirnoff 	}
660a9771948SGleb Smirnoff 
66108b68b0eSGleb Smirnoff out:
66208b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
663a9771948SGleb Smirnoff 	m_freem(m);
664a9771948SGleb Smirnoff }
665a9771948SGleb Smirnoff 
6665c1f0f6dSGleb Smirnoff static int
667a9771948SGleb Smirnoff carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
668a9771948SGleb Smirnoff {
669a9771948SGleb Smirnoff 	struct m_tag *mtag;
670a9771948SGleb Smirnoff 
671a9771948SGleb Smirnoff 	if (sc->sc_init_counter) {
672a9771948SGleb Smirnoff 		/* this could also be seconds since unix epoch */
673a9771948SGleb Smirnoff 		sc->sc_counter = arc4random();
674a9771948SGleb Smirnoff 		sc->sc_counter = sc->sc_counter << 32;
675a9771948SGleb Smirnoff 		sc->sc_counter += arc4random();
676a9771948SGleb Smirnoff 	} else
677a9771948SGleb Smirnoff 		sc->sc_counter++;
678a9771948SGleb Smirnoff 
679a9771948SGleb Smirnoff 	ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
680a9771948SGleb Smirnoff 	ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
681a9771948SGleb Smirnoff 
682a9771948SGleb Smirnoff 	carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
683a9771948SGleb Smirnoff 
684a9771948SGleb Smirnoff 	/* Tag packet for carp_output */
68508b68b0eSGleb Smirnoff 	if ((mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct carp_softc *),
68608b68b0eSGleb Smirnoff 	    M_NOWAIT)) == NULL) {
687a9771948SGleb Smirnoff 		m_freem(m);
68808b68b0eSGleb Smirnoff 		CARPSTATS_INC(carps_onomem);
689a9771948SGleb Smirnoff 		return (ENOMEM);
690a9771948SGleb Smirnoff 	}
69108b68b0eSGleb Smirnoff 	bcopy(&sc, (caddr_t)(mtag + 1), sizeof(struct carp_softc *));
692a9771948SGleb Smirnoff 	m_tag_prepend(m, mtag);
693a9771948SGleb Smirnoff 
694a9771948SGleb Smirnoff 	return (0);
695a9771948SGleb Smirnoff }
696a9771948SGleb Smirnoff 
697f08535f8SGleb Smirnoff /*
698f08535f8SGleb Smirnoff  * To avoid LORs and possible recursions this function shouldn't
699f08535f8SGleb Smirnoff  * be called directly, but scheduled via taskqueue.
700f08535f8SGleb Smirnoff  */
7015c1f0f6dSGleb Smirnoff static void
702f08535f8SGleb Smirnoff carp_send_ad_all(void *ctx __unused, int pending __unused)
703a9771948SGleb Smirnoff {
704d92d54d5SGleb Smirnoff 	struct carp_softc *sc;
705a9771948SGleb Smirnoff 
706d92d54d5SGleb Smirnoff 	mtx_lock(&carp_mtx);
70708b68b0eSGleb Smirnoff 	LIST_FOREACH(sc, &carp_list, sc_next)
708f08535f8SGleb Smirnoff 		if (sc->sc_state == MASTER) {
70908b68b0eSGleb Smirnoff 			CARP_LOCK(sc);
710d220759bSGleb Smirnoff 			carp_send_ad_locked(sc);
71108b68b0eSGleb Smirnoff 			CARP_UNLOCK(sc);
712a9771948SGleb Smirnoff 		}
713d92d54d5SGleb Smirnoff 	mtx_unlock(&carp_mtx);
714a9771948SGleb Smirnoff }
715a9771948SGleb Smirnoff 
7165c1f0f6dSGleb Smirnoff static void
717a9771948SGleb Smirnoff carp_send_ad(void *v)
718a9771948SGleb Smirnoff {
719d220759bSGleb Smirnoff 	struct carp_softc *sc = v;
720d220759bSGleb Smirnoff 
72108b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
722d220759bSGleb Smirnoff 	carp_send_ad_locked(sc);
72308b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
724d220759bSGleb Smirnoff }
725d220759bSGleb Smirnoff 
726d220759bSGleb Smirnoff static void
727d220759bSGleb Smirnoff carp_send_ad_locked(struct carp_softc *sc)
728d220759bSGleb Smirnoff {
729a9771948SGleb Smirnoff 	struct carp_header ch;
730a9771948SGleb Smirnoff 	struct timeval tv;
73108b68b0eSGleb Smirnoff 	struct sockaddr sa;
73208b68b0eSGleb Smirnoff 	struct ifaddr *ifa;
733a9771948SGleb Smirnoff 	struct carp_header *ch_ptr;
734a9771948SGleb Smirnoff 	struct mbuf *m;
73508b68b0eSGleb Smirnoff 	int len, advskew;
736a9771948SGleb Smirnoff 
73708b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
738d220759bSGleb Smirnoff 
739f08535f8SGleb Smirnoff 	advskew = DEMOTE_ADVSKEW(sc);
74008b68b0eSGleb Smirnoff 	tv.tv_sec = sc->sc_advbase;
741a9771948SGleb Smirnoff 	tv.tv_usec = advskew * 1000000 / 256;
742a9771948SGleb Smirnoff 
743a9771948SGleb Smirnoff 	ch.carp_version = CARP_VERSION;
744a9771948SGleb Smirnoff 	ch.carp_type = CARP_ADVERTISEMENT;
745a9771948SGleb Smirnoff 	ch.carp_vhid = sc->sc_vhid;
74608b68b0eSGleb Smirnoff 	ch.carp_advbase = sc->sc_advbase;
747a9771948SGleb Smirnoff 	ch.carp_advskew = advskew;
748a9771948SGleb Smirnoff 	ch.carp_authlen = 7;	/* XXX DEFINE */
749a9771948SGleb Smirnoff 	ch.carp_pad1 = 0;	/* must be zero */
750a9771948SGleb Smirnoff 	ch.carp_cksum = 0;
751a9771948SGleb Smirnoff 
75208b68b0eSGleb Smirnoff 	/* XXXGL: OpenBSD picks first ifaddr with needed family. */
75308b68b0eSGleb Smirnoff 
754a9771948SGleb Smirnoff #ifdef INET
75508b68b0eSGleb Smirnoff 	if (sc->sc_naddrs) {
756a9771948SGleb Smirnoff 		struct ip *ip;
757a9771948SGleb Smirnoff 
75808b68b0eSGleb Smirnoff 		MGETHDR(m, M_NOWAIT, MT_HEADER);
759a9771948SGleb Smirnoff 		if (m == NULL) {
7606bf65bcfSRobert Watson 			CARPSTATS_INC(carps_onomem);
761a9771948SGleb Smirnoff 			/* XXX maybe less ? */
762a9771948SGleb Smirnoff 			callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
763a9771948SGleb Smirnoff 			    carp_send_ad, sc);
764a9771948SGleb Smirnoff 			return;
765a9771948SGleb Smirnoff 		}
766a9771948SGleb Smirnoff 		len = sizeof(*ip) + sizeof(ch);
767a9771948SGleb Smirnoff 		m->m_pkthdr.len = len;
768a9771948SGleb Smirnoff 		m->m_pkthdr.rcvif = NULL;
769a9771948SGleb Smirnoff 		m->m_len = len;
770a9771948SGleb Smirnoff 		MH_ALIGN(m, m->m_len);
771a9771948SGleb Smirnoff 		m->m_flags |= M_MCAST;
772a9771948SGleb Smirnoff 		ip = mtod(m, struct ip *);
773a9771948SGleb Smirnoff 		ip->ip_v = IPVERSION;
774a9771948SGleb Smirnoff 		ip->ip_hl = sizeof(*ip) >> 2;
775a9771948SGleb Smirnoff 		ip->ip_tos = IPTOS_LOWDELAY;
776a9771948SGleb Smirnoff 		ip->ip_len = len;
777a9771948SGleb Smirnoff 		ip->ip_id = ip_newid();
778a9771948SGleb Smirnoff 		ip->ip_off = IP_DF;
779a9771948SGleb Smirnoff 		ip->ip_ttl = CARP_DFLTTL;
780a9771948SGleb Smirnoff 		ip->ip_p = IPPROTO_CARP;
781a9771948SGleb Smirnoff 		ip->ip_sum = 0;
78208b68b0eSGleb Smirnoff 
78308b68b0eSGleb Smirnoff 		bzero(&sa, sizeof(sa));
78408b68b0eSGleb Smirnoff 		sa.sa_family = AF_INET;
78508b68b0eSGleb Smirnoff 		ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
78608b68b0eSGleb Smirnoff 		if (ifa != NULL) {
78708b68b0eSGleb Smirnoff 			ip->ip_src.s_addr =
78808b68b0eSGleb Smirnoff 			    ifatoia(ifa)->ia_addr.sin_addr.s_addr;
78908b68b0eSGleb Smirnoff 			ifa_free(ifa);
79008b68b0eSGleb Smirnoff 		} else
79108b68b0eSGleb Smirnoff 			ip->ip_src.s_addr = 0;
792a9771948SGleb Smirnoff 		ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
793a9771948SGleb Smirnoff 
794a9771948SGleb Smirnoff 		ch_ptr = (struct carp_header *)(&ip[1]);
795a9771948SGleb Smirnoff 		bcopy(&ch, ch_ptr, sizeof(ch));
796a9771948SGleb Smirnoff 		if (carp_prepare_ad(m, sc, ch_ptr))
797a9771948SGleb Smirnoff 			return;
798a9771948SGleb Smirnoff 
799a9771948SGleb Smirnoff 		m->m_data += sizeof(*ip);
800a9771948SGleb Smirnoff 		ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip));
801a9771948SGleb Smirnoff 		m->m_data -= sizeof(*ip);
802a9771948SGleb Smirnoff 
8036bf65bcfSRobert Watson 		CARPSTATS_INC(carps_opackets);
804a9771948SGleb Smirnoff 
80508b68b0eSGleb Smirnoff 		if (ip_output(m, NULL, NULL, IP_RAWOUTPUT,
80608b68b0eSGleb Smirnoff 		    &sc->sc_carpdev->if_carp->cif_imo, NULL)) {
807a9771948SGleb Smirnoff 			if (sc->sc_sendad_errors < INT_MAX)
808a9771948SGleb Smirnoff 				sc->sc_sendad_errors++;
809f08535f8SGleb Smirnoff 			if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS)
810f08535f8SGleb Smirnoff 				carp_demote_adj(carp_senderr_adj, "send error");
811a9771948SGleb Smirnoff 			sc->sc_sendad_success = 0;
812a9771948SGleb Smirnoff 		} else {
813a9771948SGleb Smirnoff 			if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
814a9771948SGleb Smirnoff 				if (++sc->sc_sendad_success >=
815a9771948SGleb Smirnoff 				    CARP_SENDAD_MIN_SUCCESS) {
816f08535f8SGleb Smirnoff 					carp_demote_adj(-carp_senderr_adj,
817f08535f8SGleb Smirnoff 					    "send ok");
818a9771948SGleb Smirnoff 					sc->sc_sendad_errors = 0;
819a9771948SGleb Smirnoff 				}
820a9771948SGleb Smirnoff 			} else
821a9771948SGleb Smirnoff 				sc->sc_sendad_errors = 0;
822a9771948SGleb Smirnoff 		}
823a9771948SGleb Smirnoff 	}
824a9771948SGleb Smirnoff #endif /* INET */
825a9771948SGleb Smirnoff #ifdef INET6
82608b68b0eSGleb Smirnoff 	if (sc->sc_naddrs6) {
827a9771948SGleb Smirnoff 		struct ip6_hdr *ip6;
828a9771948SGleb Smirnoff 
82908b68b0eSGleb Smirnoff 		MGETHDR(m, M_NOWAIT, MT_HEADER);
830a9771948SGleb Smirnoff 		if (m == NULL) {
8316bf65bcfSRobert Watson 			CARPSTATS_INC(carps_onomem);
832a9771948SGleb Smirnoff 			/* XXX maybe less ? */
833a9771948SGleb Smirnoff 			callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
834a9771948SGleb Smirnoff 			    carp_send_ad, sc);
835a9771948SGleb Smirnoff 			return;
836a9771948SGleb Smirnoff 		}
837a9771948SGleb Smirnoff 		len = sizeof(*ip6) + sizeof(ch);
838a9771948SGleb Smirnoff 		m->m_pkthdr.len = len;
839a9771948SGleb Smirnoff 		m->m_pkthdr.rcvif = NULL;
840a9771948SGleb Smirnoff 		m->m_len = len;
841a9771948SGleb Smirnoff 		MH_ALIGN(m, m->m_len);
842a9771948SGleb Smirnoff 		m->m_flags |= M_MCAST;
843a9771948SGleb Smirnoff 		ip6 = mtod(m, struct ip6_hdr *);
844a9771948SGleb Smirnoff 		bzero(ip6, sizeof(*ip6));
845a9771948SGleb Smirnoff 		ip6->ip6_vfc |= IPV6_VERSION;
846a9771948SGleb Smirnoff 		ip6->ip6_hlim = CARP_DFLTTL;
847a9771948SGleb Smirnoff 		ip6->ip6_nxt = IPPROTO_CARP;
84808b68b0eSGleb Smirnoff 		bzero(&sa, sizeof(sa));
849a9771948SGleb Smirnoff 
85008b68b0eSGleb Smirnoff 		/* set the source address */
85108b68b0eSGleb Smirnoff 		sa.sa_family = AF_INET6;
85208b68b0eSGleb Smirnoff 		ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
85308b68b0eSGleb Smirnoff 		if (ifa != NULL) {
85408b68b0eSGleb Smirnoff 			bcopy(IFA_IN6(ifa), &ip6->ip6_src,
85508b68b0eSGleb Smirnoff 			    sizeof(struct in6_addr));
85608b68b0eSGleb Smirnoff 			ifa_free(ifa);
85708b68b0eSGleb Smirnoff 		} else
85808b68b0eSGleb Smirnoff 			/* This should never happen with IPv6. */
85908b68b0eSGleb Smirnoff 			bzero(&ip6->ip6_src, sizeof(struct in6_addr));
86008b68b0eSGleb Smirnoff 
86108b68b0eSGleb Smirnoff 		/* Set the multicast destination. */
8627002145dSBjoern A. Zeeb 		ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
863a9771948SGleb Smirnoff 		ip6->ip6_dst.s6_addr8[15] = 0x12;
8647002145dSBjoern A. Zeeb 		if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
8657002145dSBjoern A. Zeeb 			m_freem(m);
866e81ab876SGleb Smirnoff 			CARP_DEBUG("%s: in6_setscope failed\n", __func__);
8677002145dSBjoern A. Zeeb 			return;
8687002145dSBjoern A. Zeeb 		}
869a9771948SGleb Smirnoff 
870a9771948SGleb Smirnoff 		ch_ptr = (struct carp_header *)(&ip6[1]);
871a9771948SGleb Smirnoff 		bcopy(&ch, ch_ptr, sizeof(ch));
872a9771948SGleb Smirnoff 		if (carp_prepare_ad(m, sc, ch_ptr))
873a9771948SGleb Smirnoff 			return;
874a9771948SGleb Smirnoff 
875a9771948SGleb Smirnoff 		m->m_data += sizeof(*ip6);
876a9771948SGleb Smirnoff 		ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip6));
877a9771948SGleb Smirnoff 		m->m_data -= sizeof(*ip6);
878a9771948SGleb Smirnoff 
8796bf65bcfSRobert Watson 		CARPSTATS_INC(carps_opackets6);
880a9771948SGleb Smirnoff 
88108b68b0eSGleb Smirnoff 		if (ip6_output(m, NULL, NULL, 0,
88208b68b0eSGleb Smirnoff 		    &sc->sc_carpdev->if_carp->cif_im6o, NULL, NULL)) {
883a9771948SGleb Smirnoff 			if (sc->sc_sendad_errors < INT_MAX)
884a9771948SGleb Smirnoff 				sc->sc_sendad_errors++;
885f08535f8SGleb Smirnoff 			if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS)
886f08535f8SGleb Smirnoff 				carp_demote_adj(carp_senderr_adj,
887f08535f8SGleb Smirnoff 				    "send6 error");
888a9771948SGleb Smirnoff 			sc->sc_sendad_success = 0;
889a9771948SGleb Smirnoff 		} else {
890a9771948SGleb Smirnoff 			if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
891a9771948SGleb Smirnoff 				if (++sc->sc_sendad_success >=
892a9771948SGleb Smirnoff 				    CARP_SENDAD_MIN_SUCCESS) {
893f08535f8SGleb Smirnoff 					carp_demote_adj(-carp_senderr_adj,
894f08535f8SGleb Smirnoff 					    "send6 ok");
895a9771948SGleb Smirnoff 					sc->sc_sendad_errors = 0;
896a9771948SGleb Smirnoff 				}
897a9771948SGleb Smirnoff 			} else
898a9771948SGleb Smirnoff 				sc->sc_sendad_errors = 0;
899a9771948SGleb Smirnoff 		}
900a9771948SGleb Smirnoff 	}
901a9771948SGleb Smirnoff #endif /* INET6 */
902a9771948SGleb Smirnoff 
90308b68b0eSGleb Smirnoff 	callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), carp_send_ad, sc);
90408b68b0eSGleb Smirnoff }
905a9771948SGleb Smirnoff 
90608b68b0eSGleb Smirnoff static void
90708b68b0eSGleb Smirnoff carp_addroute(struct carp_softc *sc)
90808b68b0eSGleb Smirnoff {
90908b68b0eSGleb Smirnoff 	struct ifaddr *ifa;
91008b68b0eSGleb Smirnoff 
91108b68b0eSGleb Smirnoff 	CARP_FOREACH_IFA(sc, ifa)
912*2512b096SGleb Smirnoff 		carp_ifa_addroute(ifa);
913*2512b096SGleb Smirnoff }
914*2512b096SGleb Smirnoff 
915*2512b096SGleb Smirnoff static void
916*2512b096SGleb Smirnoff carp_ifa_addroute(struct ifaddr *ifa)
917*2512b096SGleb Smirnoff {
918*2512b096SGleb Smirnoff 
91908b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
92008b68b0eSGleb Smirnoff #ifdef INET
92108b68b0eSGleb Smirnoff 	case AF_INET:
92208b68b0eSGleb Smirnoff 		in_addprefix(ifatoia(ifa), RTF_UP);
92308b68b0eSGleb Smirnoff 		ifa_add_loopback_route(ifa,
92408b68b0eSGleb Smirnoff 		    (struct sockaddr *)&ifatoia(ifa)->ia_addr);
92508b68b0eSGleb Smirnoff 		break;
92608b68b0eSGleb Smirnoff #endif
92708b68b0eSGleb Smirnoff #ifdef INET6
92808b68b0eSGleb Smirnoff 	case AF_INET6:
92908b68b0eSGleb Smirnoff 		ifa_add_loopback_route(ifa,
93008b68b0eSGleb Smirnoff 		    (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
93108b68b0eSGleb Smirnoff 		in6_ifaddloop(ifa);
93208b68b0eSGleb Smirnoff 		break;
93308b68b0eSGleb Smirnoff #endif
93408b68b0eSGleb Smirnoff 	}
93508b68b0eSGleb Smirnoff }
93608b68b0eSGleb Smirnoff 
93708b68b0eSGleb Smirnoff static void
93808b68b0eSGleb Smirnoff carp_delroute(struct carp_softc *sc)
93908b68b0eSGleb Smirnoff {
94008b68b0eSGleb Smirnoff 	struct ifaddr *ifa;
94108b68b0eSGleb Smirnoff 
94208b68b0eSGleb Smirnoff 	CARP_FOREACH_IFA(sc, ifa)
943*2512b096SGleb Smirnoff 		carp_ifa_delroute(ifa);
944*2512b096SGleb Smirnoff }
945*2512b096SGleb Smirnoff 
946*2512b096SGleb Smirnoff static void
947*2512b096SGleb Smirnoff carp_ifa_delroute(struct ifaddr *ifa)
948*2512b096SGleb Smirnoff {
949*2512b096SGleb Smirnoff 
95008b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
95108b68b0eSGleb Smirnoff #ifdef INET
95208b68b0eSGleb Smirnoff 	case AF_INET:
95308b68b0eSGleb Smirnoff 		ifa_del_loopback_route(ifa,
95408b68b0eSGleb Smirnoff 		    (struct sockaddr *)&ifatoia(ifa)->ia_addr);
95508b68b0eSGleb Smirnoff 		in_scrubprefix(ifatoia(ifa), LLE_STATIC);
95608b68b0eSGleb Smirnoff 		break;
95708b68b0eSGleb Smirnoff #endif
95808b68b0eSGleb Smirnoff #ifdef INET6
95908b68b0eSGleb Smirnoff 	case AF_INET6:
96008b68b0eSGleb Smirnoff 		ifa_del_loopback_route(ifa,
96108b68b0eSGleb Smirnoff 		    (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
96208b68b0eSGleb Smirnoff 		in6_ifremloop(ifa);
96308b68b0eSGleb Smirnoff 		break;
96408b68b0eSGleb Smirnoff #endif
96508b68b0eSGleb Smirnoff 	}
966a9771948SGleb Smirnoff }
967a9771948SGleb Smirnoff 
968a0ae8f04SBjoern A. Zeeb #ifdef INET
969a9771948SGleb Smirnoff /*
970a9771948SGleb Smirnoff  * Broadcast a gratuitous ARP request containing
971a9771948SGleb Smirnoff  * the virtual router MAC address for each IP address
972a9771948SGleb Smirnoff  * associated with the virtual router.
973a9771948SGleb Smirnoff  */
9745c1f0f6dSGleb Smirnoff static void
975a9771948SGleb Smirnoff carp_send_arp(struct carp_softc *sc)
976a9771948SGleb Smirnoff {
977a9771948SGleb Smirnoff 	struct ifaddr *ifa;
978a9771948SGleb Smirnoff 
97908b68b0eSGleb Smirnoff 	CARP_FOREACH_IFA(sc, ifa)
98008b68b0eSGleb Smirnoff 		if (ifa->ifa_addr->sa_family == AF_INET)
98108b68b0eSGleb Smirnoff 			arp_ifinit2(sc->sc_carpdev, ifa, LLADDR(&sc->sc_addr));
982a9771948SGleb Smirnoff }
98308b68b0eSGleb Smirnoff 
98408b68b0eSGleb Smirnoff int
98508b68b0eSGleb Smirnoff carp_iamatch(struct ifaddr *ifa, uint8_t **enaddr)
98608b68b0eSGleb Smirnoff {
98708b68b0eSGleb Smirnoff 	struct carp_softc *sc = ifa->ifa_carp;
98808b68b0eSGleb Smirnoff 
98908b68b0eSGleb Smirnoff 	if (sc->sc_state == MASTER) {
99008b68b0eSGleb Smirnoff 		*enaddr = LLADDR(&sc->sc_addr);
99108b68b0eSGleb Smirnoff 		return (1);
99208b68b0eSGleb Smirnoff 	}
99308b68b0eSGleb Smirnoff 
99408b68b0eSGleb Smirnoff 	return (0);
995a9771948SGleb Smirnoff }
996a0ae8f04SBjoern A. Zeeb #endif
997a9771948SGleb Smirnoff 
998a9771948SGleb Smirnoff #ifdef INET6
9995c1f0f6dSGleb Smirnoff static void
1000a9771948SGleb Smirnoff carp_send_na(struct carp_softc *sc)
1001a9771948SGleb Smirnoff {
100208b68b0eSGleb Smirnoff 	static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1003a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1004a9771948SGleb Smirnoff 	struct in6_addr *in6;
1005a9771948SGleb Smirnoff 
100608b68b0eSGleb Smirnoff 	CARP_FOREACH_IFA(sc, ifa) {
1007a9771948SGleb Smirnoff 		if (ifa->ifa_addr->sa_family != AF_INET6)
1008a9771948SGleb Smirnoff 			continue;
1009a9771948SGleb Smirnoff 
101008b68b0eSGleb Smirnoff 		in6 = IFA_IN6(ifa);
1011e8c34a71SGleb Smirnoff 		nd6_na_output(sc->sc_carpdev, &mcast, in6,
1012a9771948SGleb Smirnoff 		    ND_NA_FLAG_OVERRIDE, 1, NULL);
1013a9771948SGleb Smirnoff 		DELAY(1000);	/* XXX */
1014a9771948SGleb Smirnoff 	}
1015a9771948SGleb Smirnoff }
1016a9771948SGleb Smirnoff 
1017a4e53905SMax Laier struct ifaddr *
101854bfbd51SWill Andrews carp_iamatch6(struct ifnet *ifp, struct in6_addr *taddr)
1019a9771948SGleb Smirnoff {
1020a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1021a9771948SGleb Smirnoff 
1022137f91e8SJohn Baldwin 	IF_ADDR_RLOCK(ifp);
102308b68b0eSGleb Smirnoff 	IFNET_FOREACH_IFA(ifp, ifa)
102408b68b0eSGleb Smirnoff 		if (ifa->ifa_addr->sa_family == AF_INET6 &&
102508b68b0eSGleb Smirnoff 		    ifa->ifa_carp->sc_state == MASTER &&
102608b68b0eSGleb Smirnoff 		    IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) {
10278c0fec80SRobert Watson 			ifa_ref(ifa);
1028137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
1029a9771948SGleb Smirnoff 			return (ifa);
1030a9771948SGleb Smirnoff 		}
1031137f91e8SJohn Baldwin 	IF_ADDR_RUNLOCK(ifp);
1032a9771948SGleb Smirnoff 
1033a9771948SGleb Smirnoff 	return (NULL);
1034a9771948SGleb Smirnoff }
1035a9771948SGleb Smirnoff 
103654bfbd51SWill Andrews caddr_t
103754bfbd51SWill Andrews carp_macmatch6(struct ifnet *ifp, struct mbuf *m, const struct in6_addr *taddr)
1038a9771948SGleb Smirnoff {
1039a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1040a9771948SGleb Smirnoff 
1041137f91e8SJohn Baldwin 	IF_ADDR_RLOCK(ifp);
104208b68b0eSGleb Smirnoff 	IFNET_FOREACH_IFA(ifp, ifa)
104308b68b0eSGleb Smirnoff 		if (ifa->ifa_addr->sa_family == AF_INET6 &&
104408b68b0eSGleb Smirnoff 		    IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) {
104508b68b0eSGleb Smirnoff 			struct carp_softc *sc = ifa->ifa_carp;
104608b68b0eSGleb Smirnoff 			struct m_tag *mtag;
104708b68b0eSGleb Smirnoff 
1048137f91e8SJohn Baldwin 			IF_ADDR_RUNLOCK(ifp);
104908b68b0eSGleb Smirnoff 
1050a9771948SGleb Smirnoff 			mtag = m_tag_get(PACKET_TAG_CARP,
1051a9771948SGleb Smirnoff 			    sizeof(struct ifnet *), M_NOWAIT);
105208b68b0eSGleb Smirnoff 			if (mtag == NULL)
105308b68b0eSGleb Smirnoff 				/* Better a bit than nothing. */
105408b68b0eSGleb Smirnoff 				return (LLADDR(&sc->sc_addr));
105508b68b0eSGleb Smirnoff 
1056a9771948SGleb Smirnoff 			bcopy(&ifp, (caddr_t)(mtag + 1),
1057a9771948SGleb Smirnoff 			    sizeof(struct ifnet *));
1058a9771948SGleb Smirnoff 			m_tag_prepend(m, mtag);
1059a9771948SGleb Smirnoff 
106008b68b0eSGleb Smirnoff 			return (LLADDR(&sc->sc_addr));
1061a9771948SGleb Smirnoff 		}
1062137f91e8SJohn Baldwin 	IF_ADDR_RUNLOCK(ifp);
1063a9771948SGleb Smirnoff 
1064a9771948SGleb Smirnoff 	return (NULL);
1065a9771948SGleb Smirnoff }
106608b68b0eSGleb Smirnoff #endif /* INET6 */
1067a9771948SGleb Smirnoff 
106808b68b0eSGleb Smirnoff int
106954bfbd51SWill Andrews carp_forus(struct ifnet *ifp, u_char *dhost)
1070a9771948SGleb Smirnoff {
107108b68b0eSGleb Smirnoff 	struct carp_softc *sc;
107208b68b0eSGleb Smirnoff 	uint8_t *ena = dhost;
1073a9771948SGleb Smirnoff 
1074a9771948SGleb Smirnoff 	if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
107508b68b0eSGleb Smirnoff 		return (0);
1076a9771948SGleb Smirnoff 
107708b68b0eSGleb Smirnoff 	CIF_LOCK(ifp->if_carp);
107808b68b0eSGleb Smirnoff 	IFNET_FOREACH_CARP(ifp, sc) {
107908b68b0eSGleb Smirnoff 		CARP_LOCK(sc);
108008b68b0eSGleb Smirnoff 		if (sc->sc_state == MASTER && !bcmp(dhost, LLADDR(&sc->sc_addr),
108108b68b0eSGleb Smirnoff 		    ETHER_ADDR_LEN)) {
108208b68b0eSGleb Smirnoff 			CARP_UNLOCK(sc);
108308b68b0eSGleb Smirnoff 			CIF_UNLOCK(ifp->if_carp);
108408b68b0eSGleb Smirnoff 			return (1);
1085a9771948SGleb Smirnoff 		}
108608b68b0eSGleb Smirnoff 		CARP_UNLOCK(sc);
108708b68b0eSGleb Smirnoff 	}
108808b68b0eSGleb Smirnoff 	CIF_UNLOCK(ifp->if_carp);
1089a9771948SGleb Smirnoff 
109008b68b0eSGleb Smirnoff 	return (0);
1091a9771948SGleb Smirnoff }
1092a9771948SGleb Smirnoff 
10935c1f0f6dSGleb Smirnoff static void
1094a9771948SGleb Smirnoff carp_master_down(void *v)
1095a9771948SGleb Smirnoff {
1096a9771948SGleb Smirnoff 	struct carp_softc *sc = v;
1097a9771948SGleb Smirnoff 
109808b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
109908b68b0eSGleb Smirnoff 
110008b68b0eSGleb Smirnoff 	if (sc->sc_state == BACKUP) {
11011c435c73SGleb Smirnoff 		CARP_LOG("VHID %u@%s: BACKUP -> MASTER (master down)\n",
110208b68b0eSGleb Smirnoff 		    sc->sc_vhid,
110308b68b0eSGleb Smirnoff 		    sc->sc_carpdev->if_xname);
1104d220759bSGleb Smirnoff 		carp_master_down_locked(sc);
110508b68b0eSGleb Smirnoff 	}
110608b68b0eSGleb Smirnoff 
110708b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
1108d220759bSGleb Smirnoff }
1109d220759bSGleb Smirnoff 
1110d220759bSGleb Smirnoff static void
1111d220759bSGleb Smirnoff carp_master_down_locked(struct carp_softc *sc)
1112d220759bSGleb Smirnoff {
111308b68b0eSGleb Smirnoff 
111408b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
1115d220759bSGleb Smirnoff 
1116a9771948SGleb Smirnoff 	switch (sc->sc_state) {
1117a9771948SGleb Smirnoff 	case BACKUP:
1118a9771948SGleb Smirnoff 		carp_set_state(sc, MASTER);
1119d220759bSGleb Smirnoff 		carp_send_ad_locked(sc);
1120a0ae8f04SBjoern A. Zeeb #ifdef INET
1121a9771948SGleb Smirnoff 		carp_send_arp(sc);
1122a0ae8f04SBjoern A. Zeeb #endif
1123a9771948SGleb Smirnoff #ifdef INET6
1124a9771948SGleb Smirnoff 		carp_send_na(sc);
112508b68b0eSGleb Smirnoff #endif
1126a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
112708b68b0eSGleb Smirnoff 		carp_addroute(sc);
112808b68b0eSGleb Smirnoff 		break;
112908b68b0eSGleb Smirnoff 	case INIT:
113008b68b0eSGleb Smirnoff 	case MASTER:
113108b68b0eSGleb Smirnoff #ifdef INVARIANTS
113208b68b0eSGleb Smirnoff 		panic("carp: VHID %u@%s: master_down event in %s state\n",
113308b68b0eSGleb Smirnoff 		    sc->sc_vhid,
113408b68b0eSGleb Smirnoff 		    sc->sc_carpdev->if_xname,
113508b68b0eSGleb Smirnoff 		    sc->sc_state ? "MASTER" : "INIT");
113608b68b0eSGleb Smirnoff #endif
1137a9771948SGleb Smirnoff 		break;
1138a9771948SGleb Smirnoff 	}
1139a9771948SGleb Smirnoff }
1140a9771948SGleb Smirnoff 
1141a9771948SGleb Smirnoff /*
1142a9771948SGleb Smirnoff  * When in backup state, af indicates whether to reset the master down timer
1143a9771948SGleb Smirnoff  * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1144a9771948SGleb Smirnoff  */
11455c1f0f6dSGleb Smirnoff static void
1146a9771948SGleb Smirnoff carp_setrun(struct carp_softc *sc, sa_family_t af)
1147a9771948SGleb Smirnoff {
1148a9771948SGleb Smirnoff 	struct timeval tv;
1149a9771948SGleb Smirnoff 
115008b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
1151d220759bSGleb Smirnoff 
115208b68b0eSGleb Smirnoff 	if ((sc->sc_carpdev->if_flags & IFF_UP) == 0 ||
115308b68b0eSGleb Smirnoff 	    sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
115408b68b0eSGleb Smirnoff 	    (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0))
1155a9771948SGleb Smirnoff 		return;
1156a9771948SGleb Smirnoff 
1157a9771948SGleb Smirnoff 	switch (sc->sc_state) {
1158a9771948SGleb Smirnoff 	case INIT:
115908b68b0eSGleb Smirnoff 		CARP_LOG("VHID %u@%s: INIT -> BACKUP\n",
116008b68b0eSGleb Smirnoff 		    sc->sc_vhid,
116108b68b0eSGleb Smirnoff 		    sc->sc_carpdev->if_xname);
1162a9771948SGleb Smirnoff 		carp_set_state(sc, BACKUP);
1163a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
1164a9771948SGleb Smirnoff 		break;
1165a9771948SGleb Smirnoff 	case BACKUP:
1166a9771948SGleb Smirnoff 		callout_stop(&sc->sc_ad_tmo);
1167a9771948SGleb Smirnoff 		tv.tv_sec = 3 * sc->sc_advbase;
1168a9771948SGleb Smirnoff 		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1169a9771948SGleb Smirnoff 		switch (af) {
1170a9771948SGleb Smirnoff #ifdef INET
1171a9771948SGleb Smirnoff 		case AF_INET:
1172a9771948SGleb Smirnoff 			callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1173a9771948SGleb Smirnoff 			    carp_master_down, sc);
1174a9771948SGleb Smirnoff 			break;
117508b68b0eSGleb Smirnoff #endif
1176a9771948SGleb Smirnoff #ifdef INET6
1177a9771948SGleb Smirnoff 		case AF_INET6:
1178a9771948SGleb Smirnoff 			callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1179a9771948SGleb Smirnoff 			    carp_master_down, sc);
1180a9771948SGleb Smirnoff 			break;
118108b68b0eSGleb Smirnoff #endif
1182a9771948SGleb Smirnoff 		default:
118308b68b0eSGleb Smirnoff #ifdef INET
1184a9771948SGleb Smirnoff 			if (sc->sc_naddrs)
1185a9771948SGleb Smirnoff 				callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1186a9771948SGleb Smirnoff 				    carp_master_down, sc);
118708b68b0eSGleb Smirnoff #endif
118808b68b0eSGleb Smirnoff #ifdef INET6
1189a9771948SGleb Smirnoff 			if (sc->sc_naddrs6)
1190a9771948SGleb Smirnoff 				callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1191a9771948SGleb Smirnoff 				    carp_master_down, sc);
119208b68b0eSGleb Smirnoff #endif
1193a9771948SGleb Smirnoff 			break;
1194a9771948SGleb Smirnoff 		}
1195a9771948SGleb Smirnoff 		break;
1196a9771948SGleb Smirnoff 	case MASTER:
1197a9771948SGleb Smirnoff 		tv.tv_sec = sc->sc_advbase;
1198a9771948SGleb Smirnoff 		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1199a9771948SGleb Smirnoff 		callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1200a9771948SGleb Smirnoff 		    carp_send_ad, sc);
1201a9771948SGleb Smirnoff 		break;
1202a9771948SGleb Smirnoff 	}
1203a9771948SGleb Smirnoff }
1204a9771948SGleb Smirnoff 
120508b68b0eSGleb Smirnoff /*
120608b68b0eSGleb Smirnoff  * Setup multicast structures.
120708b68b0eSGleb Smirnoff  */
12085c1f0f6dSGleb Smirnoff static int
120908b68b0eSGleb Smirnoff carp_multicast_setup(struct carp_softc *sc, sa_family_t sa)
1210a9771948SGleb Smirnoff {
121108b68b0eSGleb Smirnoff 	struct ifnet *ifp = sc->sc_carpdev;
121208b68b0eSGleb Smirnoff 	struct carp_if *cif = ifp->if_carp;
121308b68b0eSGleb Smirnoff 	int error = 0;
121408b68b0eSGleb Smirnoff 
121508b68b0eSGleb Smirnoff 	switch (sa) {
121608b68b0eSGleb Smirnoff #ifdef INET
121708b68b0eSGleb Smirnoff 	case AF_INET:
121808b68b0eSGleb Smirnoff 	    {
121908b68b0eSGleb Smirnoff 		struct ip_moptions *imo = &cif->cif_imo;
1220a9771948SGleb Smirnoff 		struct in_addr addr;
1221a9771948SGleb Smirnoff 
122208b68b0eSGleb Smirnoff 		if (imo->imo_membership)
1223a9771948SGleb Smirnoff 			return (0);
1224a9771948SGleb Smirnoff 
122508b68b0eSGleb Smirnoff 		imo->imo_membership = (struct in_multi **)malloc(
122608b68b0eSGleb Smirnoff 		    (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_CARP,
122708b68b0eSGleb Smirnoff 		    M_WAITOK);
122808b68b0eSGleb Smirnoff 		imo->imo_mfilters = NULL;
122908b68b0eSGleb Smirnoff 		imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
123008b68b0eSGleb Smirnoff 		imo->imo_multicast_vif = -1;
1231a9771948SGleb Smirnoff 
1232a9771948SGleb Smirnoff 		addr.s_addr = htonl(INADDR_CARP_GROUP);
123308b68b0eSGleb Smirnoff 		if ((error = in_joingroup(ifp, &addr, NULL,
123408b68b0eSGleb Smirnoff 		    &imo->imo_membership[0])) != 0) {
123508b68b0eSGleb Smirnoff 			free(imo->imo_membership, M_CARP);
123608b68b0eSGleb Smirnoff 			break;
12372d9cfabaSRobert Watson 		}
1238a9771948SGleb Smirnoff 		imo->imo_num_memberships++;
1239a9771948SGleb Smirnoff 		imo->imo_multicast_ifp = ifp;
1240a9771948SGleb Smirnoff 		imo->imo_multicast_ttl = CARP_DFLTTL;
1241a9771948SGleb Smirnoff 		imo->imo_multicast_loop = 0;
1242a9771948SGleb Smirnoff 		break;
1243a9771948SGleb Smirnoff 	   }
124408b68b0eSGleb Smirnoff #endif
124508b68b0eSGleb Smirnoff #ifdef INET6
124608b68b0eSGleb Smirnoff 	case AF_INET6:
124708b68b0eSGleb Smirnoff 	    {
124808b68b0eSGleb Smirnoff 		struct ip6_moptions *im6o = &cif->cif_im6o;
124908b68b0eSGleb Smirnoff 		struct in6_addr in6;
125033cde130SBruce M Simpson 		struct in6_multi *in6m;
125133cde130SBruce M Simpson 
125208b68b0eSGleb Smirnoff 		if (im6o->im6o_membership)
125308b68b0eSGleb Smirnoff 			return (0);
125408b68b0eSGleb Smirnoff 
125508b68b0eSGleb Smirnoff 		im6o->im6o_membership = (struct in6_multi **)malloc(
125608b68b0eSGleb Smirnoff 		    (sizeof(struct in6_multi *) * IPV6_MIN_MEMBERSHIPS), M_CARP,
125708b68b0eSGleb Smirnoff 		    M_ZERO|M_WAITOK);
125808b68b0eSGleb Smirnoff 		im6o->im6o_mfilters = NULL;
125908b68b0eSGleb Smirnoff 		im6o->im6o_max_memberships = IPV6_MIN_MEMBERSHIPS;
126008b68b0eSGleb Smirnoff 		im6o->im6o_multicast_hlim = CARP_DFLTTL;
1261a9771948SGleb Smirnoff 		im6o->im6o_multicast_ifp = ifp;
1262a9771948SGleb Smirnoff 
126308b68b0eSGleb Smirnoff 		/* Join IPv6 CARP multicast group. */
1264a1f7e5f8SHajimu UMEMOTO 		bzero(&in6, sizeof(in6));
1265a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr16[0] = htons(0xff02);
1266a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr8[15] = 0x12;
126708b68b0eSGleb Smirnoff 		if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
126808b68b0eSGleb Smirnoff 			free(im6o->im6o_membership, M_CARP);
126908b68b0eSGleb Smirnoff 			break;
127008b68b0eSGleb Smirnoff 		}
127133cde130SBruce M Simpson 		in6m = NULL;
127208b68b0eSGleb Smirnoff 		if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
127308b68b0eSGleb Smirnoff 			free(im6o->im6o_membership, M_CARP);
127408b68b0eSGleb Smirnoff 			break;
127508b68b0eSGleb Smirnoff 		}
127633cde130SBruce M Simpson 		im6o->im6o_membership[0] = in6m;
127733cde130SBruce M Simpson 		im6o->im6o_num_memberships++;
1278a9771948SGleb Smirnoff 
127908b68b0eSGleb Smirnoff 		/* Join solicited multicast address. */
1280a1f7e5f8SHajimu UMEMOTO 		bzero(&in6, sizeof(in6));
1281a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr16[0] = htons(0xff02);
1282a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr32[1] = 0;
1283a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr32[2] = htonl(1);
128408b68b0eSGleb Smirnoff 		in6.s6_addr32[3] = 0;
1285a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr8[12] = 0xff;
128608b68b0eSGleb Smirnoff 		if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
128708b68b0eSGleb Smirnoff 			in6_mc_leave(im6o->im6o_membership[0], NULL);
128808b68b0eSGleb Smirnoff 			free(im6o->im6o_membership, M_CARP);
128908b68b0eSGleb Smirnoff 			break;
129008b68b0eSGleb Smirnoff 		}
129133cde130SBruce M Simpson 		in6m = NULL;
129208b68b0eSGleb Smirnoff 		if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
129308b68b0eSGleb Smirnoff 			in6_mc_leave(im6o->im6o_membership[0], NULL);
129408b68b0eSGleb Smirnoff 			free(im6o->im6o_membership, M_CARP);
129508b68b0eSGleb Smirnoff 			break;
129608b68b0eSGleb Smirnoff 		}
129733cde130SBruce M Simpson 		im6o->im6o_membership[1] = in6m;
129833cde130SBruce M Simpson 		im6o->im6o_num_memberships++;
1299a9771948SGleb Smirnoff 		break;
1300a9771948SGleb Smirnoff 	    }
1301a9771948SGleb Smirnoff #endif
130208b68b0eSGleb Smirnoff 	}
130308b68b0eSGleb Smirnoff 
130408b68b0eSGleb Smirnoff 	return (error);
1305a9771948SGleb Smirnoff }
1306a9771948SGleb Smirnoff 
1307a9771948SGleb Smirnoff /*
130808b68b0eSGleb Smirnoff  * Free multicast structures.
1309a9771948SGleb Smirnoff  */
13105c1f0f6dSGleb Smirnoff static void
131108b68b0eSGleb Smirnoff carp_multicast_cleanup(struct carp_softc *sc, sa_family_t sa)
1312a9771948SGleb Smirnoff {
131308b68b0eSGleb Smirnoff 	struct ifnet *ifp = sc->sc_carpdev;
131408b68b0eSGleb Smirnoff 	struct carp_if *cif = ifp->if_carp;
131508b68b0eSGleb Smirnoff 
131608b68b0eSGleb Smirnoff 	switch (sa) {
131708b68b0eSGleb Smirnoff #ifdef INET
131808b68b0eSGleb Smirnoff 	case AF_INET:
131908b68b0eSGleb Smirnoff 		if (sc->sc_naddrs == 0) {
132008b68b0eSGleb Smirnoff 			struct ip_moptions *imo = &cif->cif_imo;
132108b68b0eSGleb Smirnoff 
132208b68b0eSGleb Smirnoff 			in_leavegroup(imo->imo_membership[0], NULL);
132308b68b0eSGleb Smirnoff 			KASSERT(imo->imo_mfilters == NULL,
132408b68b0eSGleb Smirnoff 			    ("%s: imo_mfilters != NULL", __func__));
132508b68b0eSGleb Smirnoff 			free(imo->imo_membership, M_CARP);
132608b68b0eSGleb Smirnoff 			imo->imo_membership = NULL;
132708b68b0eSGleb Smirnoff 
132808b68b0eSGleb Smirnoff 		}
132908b68b0eSGleb Smirnoff 		break;
1330a9771948SGleb Smirnoff #endif
133108b68b0eSGleb Smirnoff #ifdef INET6
133208b68b0eSGleb Smirnoff 	case AF_INET6:
133308b68b0eSGleb Smirnoff 		if (sc->sc_naddrs6 == 0) {
133408b68b0eSGleb Smirnoff 			struct ip6_moptions *im6o = &cif->cif_im6o;
133508b68b0eSGleb Smirnoff 
133608b68b0eSGleb Smirnoff 			in6_mc_leave(im6o->im6o_membership[0], NULL);
133708b68b0eSGleb Smirnoff 			in6_mc_leave(im6o->im6o_membership[1], NULL);
133808b68b0eSGleb Smirnoff 			KASSERT(im6o->im6o_mfilters == NULL,
133908b68b0eSGleb Smirnoff 			    ("%s: im6o_mfilters != NULL", __func__));
134008b68b0eSGleb Smirnoff 			free(im6o->im6o_membership, M_CARP);
134108b68b0eSGleb Smirnoff 			im6o->im6o_membership = NULL;
134208b68b0eSGleb Smirnoff 		}
134308b68b0eSGleb Smirnoff 		break;
134408b68b0eSGleb Smirnoff #endif
134508b68b0eSGleb Smirnoff 	}
1346a9771948SGleb Smirnoff }
1347a9771948SGleb Smirnoff 
1348a9771948SGleb Smirnoff int
134908b68b0eSGleb Smirnoff carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa)
1350a9771948SGleb Smirnoff {
1351a9771948SGleb Smirnoff 	struct m_tag *mtag;
1352a9771948SGleb Smirnoff 	struct carp_softc *sc;
1353a9771948SGleb Smirnoff 
1354a9771948SGleb Smirnoff 	if (!sa)
1355a9771948SGleb Smirnoff 		return (0);
1356a9771948SGleb Smirnoff 
1357a9771948SGleb Smirnoff 	switch (sa->sa_family) {
1358a9771948SGleb Smirnoff #ifdef INET
1359a9771948SGleb Smirnoff 	case AF_INET:
1360a9771948SGleb Smirnoff 		break;
136108b68b0eSGleb Smirnoff #endif
1362a9771948SGleb Smirnoff #ifdef INET6
1363a9771948SGleb Smirnoff 	case AF_INET6:
1364a9771948SGleb Smirnoff 		break;
136508b68b0eSGleb Smirnoff #endif
1366a9771948SGleb Smirnoff 	default:
1367a9771948SGleb Smirnoff 		return (0);
1368a9771948SGleb Smirnoff 	}
1369a9771948SGleb Smirnoff 
1370a9771948SGleb Smirnoff 	mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
1371a9771948SGleb Smirnoff 	if (mtag == NULL)
1372a9771948SGleb Smirnoff 		return (0);
1373a9771948SGleb Smirnoff 
137408b68b0eSGleb Smirnoff 	bcopy(mtag + 1, &sc, sizeof(struct carp_softc *));
1375a9771948SGleb Smirnoff 
137608b68b0eSGleb Smirnoff 	/* Set the source MAC address to the Virtual Router MAC Address. */
1377a9771948SGleb Smirnoff 	switch (ifp->if_type) {
1378630481bbSYaroslav Tykhiy 	case IFT_ETHER:
1379630481bbSYaroslav Tykhiy 	case IFT_L2VLAN: {
1380a9771948SGleb Smirnoff 			struct ether_header *eh;
1381a9771948SGleb Smirnoff 
1382a9771948SGleb Smirnoff 			eh = mtod(m, struct ether_header *);
1383a9771948SGleb Smirnoff 			eh->ether_shost[0] = 0;
1384a9771948SGleb Smirnoff 			eh->ether_shost[1] = 0;
1385a9771948SGleb Smirnoff 			eh->ether_shost[2] = 0x5e;
1386a9771948SGleb Smirnoff 			eh->ether_shost[3] = 0;
1387a9771948SGleb Smirnoff 			eh->ether_shost[4] = 1;
1388a9771948SGleb Smirnoff 			eh->ether_shost[5] = sc->sc_vhid;
1389a9771948SGleb Smirnoff 		}
1390a9771948SGleb Smirnoff 		break;
1391a9771948SGleb Smirnoff 	case IFT_FDDI: {
1392a9771948SGleb Smirnoff 			struct fddi_header *fh;
1393a9771948SGleb Smirnoff 
1394a9771948SGleb Smirnoff 			fh = mtod(m, struct fddi_header *);
1395a9771948SGleb Smirnoff 			fh->fddi_shost[0] = 0;
1396a9771948SGleb Smirnoff 			fh->fddi_shost[1] = 0;
1397a9771948SGleb Smirnoff 			fh->fddi_shost[2] = 0x5e;
1398a9771948SGleb Smirnoff 			fh->fddi_shost[3] = 0;
1399a9771948SGleb Smirnoff 			fh->fddi_shost[4] = 1;
1400a9771948SGleb Smirnoff 			fh->fddi_shost[5] = sc->sc_vhid;
1401a9771948SGleb Smirnoff 		}
1402a9771948SGleb Smirnoff 		break;
1403a9771948SGleb Smirnoff 	case IFT_ISO88025: {
1404a9771948SGleb Smirnoff  			struct iso88025_header *th;
1405a9771948SGleb Smirnoff  			th = mtod(m, struct iso88025_header *);
1406a9771948SGleb Smirnoff 			th->iso88025_shost[0] = 3;
1407a9771948SGleb Smirnoff 			th->iso88025_shost[1] = 0;
1408a9771948SGleb Smirnoff 			th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1);
1409a9771948SGleb Smirnoff 			th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1);
1410a9771948SGleb Smirnoff 			th->iso88025_shost[4] = 0;
1411a9771948SGleb Smirnoff 			th->iso88025_shost[5] = 0;
1412a9771948SGleb Smirnoff 		}
1413a9771948SGleb Smirnoff 		break;
1414a9771948SGleb Smirnoff 	default:
141508b68b0eSGleb Smirnoff 		printf("%s: carp is not supported for the %d interface type\n",
141608b68b0eSGleb Smirnoff 		    ifp->if_xname, ifp->if_type);
1417a9771948SGleb Smirnoff 		return (EOPNOTSUPP);
1418a9771948SGleb Smirnoff 	}
1419a9771948SGleb Smirnoff 
1420a9771948SGleb Smirnoff 	return (0);
1421a9771948SGleb Smirnoff }
1422a9771948SGleb Smirnoff 
142308b68b0eSGleb Smirnoff static struct carp_softc*
142408b68b0eSGleb Smirnoff carp_alloc(struct ifnet *ifp)
1425a9771948SGleb Smirnoff {
142608b68b0eSGleb Smirnoff 	struct carp_softc *sc;
142708b68b0eSGleb Smirnoff 	struct carp_if *cif;
1428d220759bSGleb Smirnoff 
142908b68b0eSGleb Smirnoff 	if ((cif = ifp->if_carp) == NULL) {
143008b68b0eSGleb Smirnoff 		cif = carp_alloc_if(ifp);
143108b68b0eSGleb Smirnoff 		if (cif == NULL)
143208b68b0eSGleb Smirnoff 			return (NULL);
1433a9771948SGleb Smirnoff 	}
1434a9771948SGleb Smirnoff 
143508b68b0eSGleb Smirnoff 	sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
143608b68b0eSGleb Smirnoff 
143708b68b0eSGleb Smirnoff 	sc->sc_advbase = CARP_DFLTINTV;
143808b68b0eSGleb Smirnoff 	sc->sc_vhid = -1;	/* required setting */
143908b68b0eSGleb Smirnoff 	sc->sc_init_counter = 1;
144008b68b0eSGleb Smirnoff 	sc->sc_state = INIT;
144108b68b0eSGleb Smirnoff 
144208b68b0eSGleb Smirnoff 	sc->sc_ifasiz = sizeof(struct ifaddr *);
144308b68b0eSGleb Smirnoff 	sc->sc_ifas = malloc(sc->sc_ifasiz, M_CARP, M_WAITOK|M_ZERO);
144408b68b0eSGleb Smirnoff 	sc->sc_carpdev = ifp;
144508b68b0eSGleb Smirnoff 
144608b68b0eSGleb Smirnoff 	CARP_LOCK_INIT(sc);
144708b68b0eSGleb Smirnoff #ifdef INET
144808b68b0eSGleb Smirnoff 	callout_init_mtx(&sc->sc_md_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
144908b68b0eSGleb Smirnoff #endif
145008b68b0eSGleb Smirnoff #ifdef INET6
145108b68b0eSGleb Smirnoff 	callout_init_mtx(&sc->sc_md6_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
145208b68b0eSGleb Smirnoff #endif
145308b68b0eSGleb Smirnoff 	callout_init_mtx(&sc->sc_ad_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
145408b68b0eSGleb Smirnoff 
145508b68b0eSGleb Smirnoff 	CIF_LOCK(cif);
145608b68b0eSGleb Smirnoff 	TAILQ_INSERT_TAIL(&cif->cif_vrs, sc, sc_list);
145708b68b0eSGleb Smirnoff 	CIF_UNLOCK(cif);
145808b68b0eSGleb Smirnoff 
145908b68b0eSGleb Smirnoff 	mtx_lock(&carp_mtx);
146008b68b0eSGleb Smirnoff 	LIST_INSERT_HEAD(&carp_list, sc, sc_next);
146108b68b0eSGleb Smirnoff 	mtx_unlock(&carp_mtx);
146208b68b0eSGleb Smirnoff 
146308b68b0eSGleb Smirnoff 	return (sc);
146408b68b0eSGleb Smirnoff }
146508b68b0eSGleb Smirnoff 
146608b68b0eSGleb Smirnoff static int
146708b68b0eSGleb Smirnoff carp_grow_ifas(struct carp_softc *sc)
146808b68b0eSGleb Smirnoff {
146908b68b0eSGleb Smirnoff 	struct ifaddr **new;
147008b68b0eSGleb Smirnoff 
147108b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
147208b68b0eSGleb Smirnoff 
147308b68b0eSGleb Smirnoff 	new = malloc(sc->sc_ifasiz * 2, M_CARP, M_NOWAIT|M_ZERO);
147408b68b0eSGleb Smirnoff 	if (new == NULL)
147508b68b0eSGleb Smirnoff 		return (ENOMEM);
147608b68b0eSGleb Smirnoff 	bcopy(sc->sc_ifas, new, sc->sc_ifasiz);
147708b68b0eSGleb Smirnoff 	free(sc->sc_ifas, M_CARP);
147808b68b0eSGleb Smirnoff 	sc->sc_ifas = new;
147908b68b0eSGleb Smirnoff 	sc->sc_ifasiz *= 2;
148008b68b0eSGleb Smirnoff 
148108b68b0eSGleb Smirnoff 	return (0);
148208b68b0eSGleb Smirnoff }
148308b68b0eSGleb Smirnoff 
148408b68b0eSGleb Smirnoff static void
148508b68b0eSGleb Smirnoff carp_destroy(struct carp_softc *sc)
148608b68b0eSGleb Smirnoff {
148708b68b0eSGleb Smirnoff 	struct ifnet *ifp = sc->sc_carpdev;
148808b68b0eSGleb Smirnoff 	struct carp_if *cif = ifp->if_carp;
148908b68b0eSGleb Smirnoff 
149008b68b0eSGleb Smirnoff 	CIF_LOCK(cif);
149108b68b0eSGleb Smirnoff 	TAILQ_REMOVE(&cif->cif_vrs, sc, sc_list);
149208b68b0eSGleb Smirnoff 	if (TAILQ_EMPTY(&cif->cif_vrs))
149308b68b0eSGleb Smirnoff 		carp_free_if(cif);
149408b68b0eSGleb Smirnoff 	else
149508b68b0eSGleb Smirnoff 		CIF_UNLOCK(cif);
149608b68b0eSGleb Smirnoff 
149708b68b0eSGleb Smirnoff 	mtx_lock(&carp_mtx);
149808b68b0eSGleb Smirnoff 	LIST_REMOVE(sc, sc_next);
149908b68b0eSGleb Smirnoff 	mtx_unlock(&carp_mtx);
150008b68b0eSGleb Smirnoff 
150108b68b0eSGleb Smirnoff 	CARP_LOCK(sc);
1502f08535f8SGleb Smirnoff 	if (sc->sc_suppress)
1503f08535f8SGleb Smirnoff 		carp_demote_adj(-carp_ifdown_adj, "vhid removed");
150408b68b0eSGleb Smirnoff 	callout_drain(&sc->sc_ad_tmo);
150508b68b0eSGleb Smirnoff #ifdef INET
150608b68b0eSGleb Smirnoff 	callout_drain(&sc->sc_md_tmo);
150708b68b0eSGleb Smirnoff #endif
150808b68b0eSGleb Smirnoff #ifdef INET6
150908b68b0eSGleb Smirnoff 	callout_drain(&sc->sc_md6_tmo);
151008b68b0eSGleb Smirnoff #endif
151108b68b0eSGleb Smirnoff 	CARP_LOCK_DESTROY(sc);
151208b68b0eSGleb Smirnoff 
151308b68b0eSGleb Smirnoff 	free(sc->sc_ifas, M_CARP);
151408b68b0eSGleb Smirnoff 	free(sc, M_CARP);
151508b68b0eSGleb Smirnoff }
151608b68b0eSGleb Smirnoff 
151708b68b0eSGleb Smirnoff static struct carp_if*
151808b68b0eSGleb Smirnoff carp_alloc_if(struct ifnet *ifp)
1519a9771948SGleb Smirnoff {
152054bfbd51SWill Andrews 	struct carp_if *cif;
1521d220759bSGleb Smirnoff 
152208b68b0eSGleb Smirnoff 	cif = malloc(sizeof(*cif), M_CARP, M_WAITOK|M_ZERO);
152308b68b0eSGleb Smirnoff 
152408b68b0eSGleb Smirnoff 	if (ifpromisc(ifp, 1) != 0)
152508b68b0eSGleb Smirnoff 		goto cleanup;
152608b68b0eSGleb Smirnoff 
152708b68b0eSGleb Smirnoff 	CIF_LOCK_INIT(cif);
152808b68b0eSGleb Smirnoff 	cif->cif_ifp = ifp;
152908b68b0eSGleb Smirnoff 	TAILQ_INIT(&cif->cif_vrs);
153008b68b0eSGleb Smirnoff 
1531137f91e8SJohn Baldwin 	IF_ADDR_WLOCK(ifp);
153208b68b0eSGleb Smirnoff 	ifp->if_carp = cif;
153308b68b0eSGleb Smirnoff 	if_ref(ifp);
1534137f91e8SJohn Baldwin 	IF_ADDR_WUNLOCK(ifp);
153508b68b0eSGleb Smirnoff 
153608b68b0eSGleb Smirnoff 	return (cif);
153708b68b0eSGleb Smirnoff 
153808b68b0eSGleb Smirnoff cleanup:
153908b68b0eSGleb Smirnoff 	free(cif, M_CARP);
154008b68b0eSGleb Smirnoff 
154108b68b0eSGleb Smirnoff 	return (NULL);
1542d220759bSGleb Smirnoff }
1543d220759bSGleb Smirnoff 
1544d220759bSGleb Smirnoff static void
154508b68b0eSGleb Smirnoff carp_free_if(struct carp_if *cif)
154608b68b0eSGleb Smirnoff {
154708b68b0eSGleb Smirnoff 	struct ifnet *ifp = cif->cif_ifp;
154808b68b0eSGleb Smirnoff 
154908b68b0eSGleb Smirnoff 	CIF_LOCK_ASSERT(cif);
155008b68b0eSGleb Smirnoff 	KASSERT(TAILQ_EMPTY(&cif->cif_vrs), ("%s: softc list not empty",
155108b68b0eSGleb Smirnoff 	    __func__));
155208b68b0eSGleb Smirnoff 
1553137f91e8SJohn Baldwin 	IF_ADDR_WLOCK(ifp);
155408b68b0eSGleb Smirnoff 	ifp->if_carp = NULL;
155508b68b0eSGleb Smirnoff 	if_rele(ifp);
1556137f91e8SJohn Baldwin 	IF_ADDR_WUNLOCK(ifp);
155708b68b0eSGleb Smirnoff 
155808b68b0eSGleb Smirnoff 	CIF_LOCK_DESTROY(cif);
155908b68b0eSGleb Smirnoff 
156008b68b0eSGleb Smirnoff 	ifpromisc(ifp, 0);
156108b68b0eSGleb Smirnoff 
156208b68b0eSGleb Smirnoff 	free(cif, M_CARP);
156308b68b0eSGleb Smirnoff }
156408b68b0eSGleb Smirnoff 
156508b68b0eSGleb Smirnoff static void
156608b68b0eSGleb Smirnoff carp_carprcp(struct carpreq *carpr, struct carp_softc *sc, int priv)
156708b68b0eSGleb Smirnoff {
156808b68b0eSGleb Smirnoff 
156908b68b0eSGleb Smirnoff 	CARP_LOCK(sc);
157008b68b0eSGleb Smirnoff 	carpr->carpr_state = sc->sc_state;
157108b68b0eSGleb Smirnoff 	carpr->carpr_vhid = sc->sc_vhid;
157208b68b0eSGleb Smirnoff 	carpr->carpr_advbase = sc->sc_advbase;
157308b68b0eSGleb Smirnoff 	carpr->carpr_advskew = sc->sc_advskew;
157408b68b0eSGleb Smirnoff 	if (priv)
157508b68b0eSGleb Smirnoff 		bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key));
157608b68b0eSGleb Smirnoff 	else
157708b68b0eSGleb Smirnoff 		bzero(carpr->carpr_key, sizeof(carpr->carpr_key));
157808b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
157908b68b0eSGleb Smirnoff }
158008b68b0eSGleb Smirnoff 
158108b68b0eSGleb Smirnoff int
158208b68b0eSGleb Smirnoff carp_ioctl(struct ifreq *ifr, u_long cmd, struct thread *td)
158308b68b0eSGleb Smirnoff {
158408b68b0eSGleb Smirnoff 	struct carpreq carpr;
158508b68b0eSGleb Smirnoff 	struct ifnet *ifp;
158608b68b0eSGleb Smirnoff 	struct carp_softc *sc = NULL;
158708b68b0eSGleb Smirnoff 	int error = 0, locked = 0;
158808b68b0eSGleb Smirnoff 
158908b68b0eSGleb Smirnoff 	if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
159008b68b0eSGleb Smirnoff 		return (error);
159108b68b0eSGleb Smirnoff 
159208b68b0eSGleb Smirnoff 	ifp = ifunit_ref(ifr->ifr_name);
159308b68b0eSGleb Smirnoff 	if (ifp == NULL)
159408b68b0eSGleb Smirnoff 		return (ENXIO);
159508b68b0eSGleb Smirnoff 
159608b68b0eSGleb Smirnoff 	switch (ifp->if_type) {
159708b68b0eSGleb Smirnoff 	case IFT_ETHER:
159808b68b0eSGleb Smirnoff 	case IFT_L2VLAN:
159908b68b0eSGleb Smirnoff 	case IFT_FDDI:
160008b68b0eSGleb Smirnoff 	case IFT_ISO88025:
160108b68b0eSGleb Smirnoff 		break;
160208b68b0eSGleb Smirnoff 	default:
160308b68b0eSGleb Smirnoff 		error = EOPNOTSUPP;
160408b68b0eSGleb Smirnoff 		goto out;
160508b68b0eSGleb Smirnoff 	}
160608b68b0eSGleb Smirnoff 
160708b68b0eSGleb Smirnoff 	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
160808b68b0eSGleb Smirnoff 		error = EADDRNOTAVAIL;
160908b68b0eSGleb Smirnoff 		goto out;
161008b68b0eSGleb Smirnoff 	}
161108b68b0eSGleb Smirnoff 
161208b68b0eSGleb Smirnoff 	switch (cmd) {
161308b68b0eSGleb Smirnoff 	case SIOCSVH:
161408b68b0eSGleb Smirnoff 		if ((error = priv_check(td, PRIV_NETINET_CARP)))
161508b68b0eSGleb Smirnoff 			break;
161608b68b0eSGleb Smirnoff 		if (carpr.carpr_vhid <= 0 || carpr.carpr_vhid > CARP_MAXVHID ||
161708b68b0eSGleb Smirnoff 		    carpr.carpr_advbase < 0 || carpr.carpr_advskew < 0) {
161808b68b0eSGleb Smirnoff 			error = EINVAL;
161908b68b0eSGleb Smirnoff 			break;
162008b68b0eSGleb Smirnoff 		}
162108b68b0eSGleb Smirnoff 
162208b68b0eSGleb Smirnoff 		if (ifp->if_carp) {
162308b68b0eSGleb Smirnoff 			CIF_LOCK(ifp->if_carp);
162408b68b0eSGleb Smirnoff 			IFNET_FOREACH_CARP(ifp, sc)
162508b68b0eSGleb Smirnoff 				if (sc->sc_vhid == carpr.carpr_vhid)
162608b68b0eSGleb Smirnoff 					break;
162708b68b0eSGleb Smirnoff 			CIF_UNLOCK(ifp->if_carp);
162808b68b0eSGleb Smirnoff 		}
162908b68b0eSGleb Smirnoff 		if (sc == NULL) {
163008b68b0eSGleb Smirnoff 			sc = carp_alloc(ifp);
163108b68b0eSGleb Smirnoff 			if (sc == NULL) {
163208b68b0eSGleb Smirnoff 				error = EINVAL; /* XXX: ifpromisc failed */
163308b68b0eSGleb Smirnoff 				break;
163408b68b0eSGleb Smirnoff 			}
163508b68b0eSGleb Smirnoff 
163608b68b0eSGleb Smirnoff 			CARP_LOCK(sc);
163708b68b0eSGleb Smirnoff 			sc->sc_vhid = carpr.carpr_vhid;
163808b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[0] = 0;
163908b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[1] = 0;
164008b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[2] = 0x5e;
164108b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[3] = 0;
164208b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[4] = 1;
164308b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[5] = sc->sc_vhid;
164408b68b0eSGleb Smirnoff 		} else
164508b68b0eSGleb Smirnoff 			CARP_LOCK(sc);
164608b68b0eSGleb Smirnoff 		locked = 1;
164708b68b0eSGleb Smirnoff 		if (carpr.carpr_advbase > 0) {
164808b68b0eSGleb Smirnoff 			if (carpr.carpr_advbase > 255 ||
164908b68b0eSGleb Smirnoff 			    carpr.carpr_advbase < CARP_DFLTINTV) {
165008b68b0eSGleb Smirnoff 				error = EINVAL;
165108b68b0eSGleb Smirnoff 				break;
165208b68b0eSGleb Smirnoff 			}
165308b68b0eSGleb Smirnoff 			sc->sc_advbase = carpr.carpr_advbase;
165408b68b0eSGleb Smirnoff 		}
165508b68b0eSGleb Smirnoff 		if (carpr.carpr_advskew > 0) {
165608b68b0eSGleb Smirnoff 			if (carpr.carpr_advskew >= 255) {
165708b68b0eSGleb Smirnoff 				error = EINVAL;
165808b68b0eSGleb Smirnoff 				break;
165908b68b0eSGleb Smirnoff 			}
166008b68b0eSGleb Smirnoff 			sc->sc_advskew = carpr.carpr_advskew;
166108b68b0eSGleb Smirnoff 		}
166208b68b0eSGleb Smirnoff 		if (carpr.carpr_key[0] != '\0') {
166308b68b0eSGleb Smirnoff 			bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
166408b68b0eSGleb Smirnoff 			carp_hmac_prepare(sc);
166508b68b0eSGleb Smirnoff 		}
166608b68b0eSGleb Smirnoff 		if (sc->sc_state != INIT &&
166708b68b0eSGleb Smirnoff 		    carpr.carpr_state != sc->sc_state) {
166808b68b0eSGleb Smirnoff 			switch (carpr.carpr_state) {
166908b68b0eSGleb Smirnoff 			case BACKUP:
167008b68b0eSGleb Smirnoff 				callout_stop(&sc->sc_ad_tmo);
167108b68b0eSGleb Smirnoff 				carp_set_state(sc, BACKUP);
167208b68b0eSGleb Smirnoff 				carp_setrun(sc, 0);
167308b68b0eSGleb Smirnoff 				carp_delroute(sc);
167408b68b0eSGleb Smirnoff 				break;
167508b68b0eSGleb Smirnoff 			case MASTER:
167608b68b0eSGleb Smirnoff 				carp_master_down_locked(sc);
167708b68b0eSGleb Smirnoff 				break;
167808b68b0eSGleb Smirnoff 			default:
167908b68b0eSGleb Smirnoff 				break;
168008b68b0eSGleb Smirnoff 			}
168108b68b0eSGleb Smirnoff 		}
168208b68b0eSGleb Smirnoff 		break;
168308b68b0eSGleb Smirnoff 
168408b68b0eSGleb Smirnoff 	case SIOCGVH:
168508b68b0eSGleb Smirnoff 	    {
168608b68b0eSGleb Smirnoff 		int priveleged;
168708b68b0eSGleb Smirnoff 
168808b68b0eSGleb Smirnoff 		if (carpr.carpr_vhid < 0 || carpr.carpr_vhid > CARP_MAXVHID) {
168908b68b0eSGleb Smirnoff 			error = EINVAL;
169008b68b0eSGleb Smirnoff 			break;
169108b68b0eSGleb Smirnoff 		}
169208b68b0eSGleb Smirnoff 		if (carpr.carpr_count < 1) {
169308b68b0eSGleb Smirnoff 			error = EMSGSIZE;
169408b68b0eSGleb Smirnoff 			break;
169508b68b0eSGleb Smirnoff 		}
169608b68b0eSGleb Smirnoff 		if (ifp->if_carp == NULL) {
169708b68b0eSGleb Smirnoff 			error = ENOENT;
169808b68b0eSGleb Smirnoff 			break;
169908b68b0eSGleb Smirnoff 		}
170008b68b0eSGleb Smirnoff 
170108b68b0eSGleb Smirnoff 		priveleged = (priv_check(td, PRIV_NETINET_CARP) == 0);
170208b68b0eSGleb Smirnoff 		if (carpr.carpr_vhid != 0) {
170308b68b0eSGleb Smirnoff 			CIF_LOCK(ifp->if_carp);
170408b68b0eSGleb Smirnoff 			IFNET_FOREACH_CARP(ifp, sc)
170508b68b0eSGleb Smirnoff 				if (sc->sc_vhid == carpr.carpr_vhid)
170608b68b0eSGleb Smirnoff 					break;
170708b68b0eSGleb Smirnoff 			CIF_UNLOCK(ifp->if_carp);
170808b68b0eSGleb Smirnoff 			if (sc == NULL) {
170908b68b0eSGleb Smirnoff 				error = ENOENT;
171008b68b0eSGleb Smirnoff 				break;
171108b68b0eSGleb Smirnoff 			}
171208b68b0eSGleb Smirnoff 			carp_carprcp(&carpr, sc, priveleged);
171308b68b0eSGleb Smirnoff 			error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
171408b68b0eSGleb Smirnoff 		} else  {
171508b68b0eSGleb Smirnoff 			int i, count;
171608b68b0eSGleb Smirnoff 
171708b68b0eSGleb Smirnoff 			count = 0;
171808b68b0eSGleb Smirnoff 			CIF_LOCK(ifp->if_carp);
171908b68b0eSGleb Smirnoff 			IFNET_FOREACH_CARP(ifp, sc)
172008b68b0eSGleb Smirnoff 				count++;
172108b68b0eSGleb Smirnoff 
172208b68b0eSGleb Smirnoff 			if (count > carpr.carpr_count) {
172308b68b0eSGleb Smirnoff 				CIF_UNLOCK(ifp->if_carp);
172408b68b0eSGleb Smirnoff 				error = EMSGSIZE;
172508b68b0eSGleb Smirnoff 				break;
172608b68b0eSGleb Smirnoff 			}
172708b68b0eSGleb Smirnoff 
172808b68b0eSGleb Smirnoff 			i = 0;
172908b68b0eSGleb Smirnoff 			IFNET_FOREACH_CARP(ifp, sc) {
173008b68b0eSGleb Smirnoff 				carp_carprcp(&carpr, sc, priveleged);
173108b68b0eSGleb Smirnoff 				carpr.carpr_count = count;
173208b68b0eSGleb Smirnoff 				error = copyout(&carpr, ifr->ifr_data +
173308b68b0eSGleb Smirnoff 				    (i * sizeof(carpr)), sizeof(carpr));
173408b68b0eSGleb Smirnoff 				if (error) {
173508b68b0eSGleb Smirnoff 					CIF_UNLOCK(ifp->if_carp);
173608b68b0eSGleb Smirnoff 					break;
173708b68b0eSGleb Smirnoff 				}
173808b68b0eSGleb Smirnoff 				i++;
173908b68b0eSGleb Smirnoff 			}
174008b68b0eSGleb Smirnoff 			CIF_UNLOCK(ifp->if_carp);
174108b68b0eSGleb Smirnoff 		}
174208b68b0eSGleb Smirnoff 		break;
174308b68b0eSGleb Smirnoff 	    }
174408b68b0eSGleb Smirnoff 	default:
174508b68b0eSGleb Smirnoff 		error = EINVAL;
174608b68b0eSGleb Smirnoff 	}
174708b68b0eSGleb Smirnoff 
174808b68b0eSGleb Smirnoff out:
174908b68b0eSGleb Smirnoff 	if (locked)
175008b68b0eSGleb Smirnoff 		CARP_UNLOCK(sc);
175108b68b0eSGleb Smirnoff 	if_rele(ifp);
175208b68b0eSGleb Smirnoff 
175308b68b0eSGleb Smirnoff 	return (error);
175408b68b0eSGleb Smirnoff }
175508b68b0eSGleb Smirnoff 
175608b68b0eSGleb Smirnoff static int
175708b68b0eSGleb Smirnoff carp_get_vhid(struct ifaddr *ifa)
175808b68b0eSGleb Smirnoff {
175908b68b0eSGleb Smirnoff 
176008b68b0eSGleb Smirnoff 	if (ifa == NULL || ifa->ifa_carp == NULL)
176108b68b0eSGleb Smirnoff 		return (0);
176208b68b0eSGleb Smirnoff 
176308b68b0eSGleb Smirnoff 	return (ifa->ifa_carp->sc_vhid);
176408b68b0eSGleb Smirnoff }
176508b68b0eSGleb Smirnoff 
176608b68b0eSGleb Smirnoff int
176708b68b0eSGleb Smirnoff carp_attach(struct ifaddr *ifa, int vhid)
176808b68b0eSGleb Smirnoff {
176908b68b0eSGleb Smirnoff 	struct ifnet *ifp = ifa->ifa_ifp;
177008b68b0eSGleb Smirnoff 	struct carp_softc *sc;
177108b68b0eSGleb Smirnoff 	int index, error;
177208b68b0eSGleb Smirnoff 
177308b68b0eSGleb Smirnoff 	if (ifp->if_carp == NULL)
177408b68b0eSGleb Smirnoff 		return (ENOPROTOOPT);
177508b68b0eSGleb Smirnoff 
177608b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
177708b68b0eSGleb Smirnoff #ifdef INET
177808b68b0eSGleb Smirnoff 	case AF_INET:
177908b68b0eSGleb Smirnoff #endif
178008b68b0eSGleb Smirnoff #ifdef INET6
178108b68b0eSGleb Smirnoff 	case AF_INET6:
178208b68b0eSGleb Smirnoff #endif
178308b68b0eSGleb Smirnoff 		break;
178408b68b0eSGleb Smirnoff 	default:
178508b68b0eSGleb Smirnoff 		return (EPROTOTYPE);
178608b68b0eSGleb Smirnoff 	}
178708b68b0eSGleb Smirnoff 
178808b68b0eSGleb Smirnoff 	CIF_LOCK(ifp->if_carp);
178908b68b0eSGleb Smirnoff 	IFNET_FOREACH_CARP(ifp, sc)
179008b68b0eSGleb Smirnoff 		if (sc->sc_vhid == vhid)
179108b68b0eSGleb Smirnoff 			break;
179208b68b0eSGleb Smirnoff 	CIF_UNLOCK(ifp->if_carp);
179308b68b0eSGleb Smirnoff 	if (sc == NULL)
179408b68b0eSGleb Smirnoff 		return (ENOENT);
179508b68b0eSGleb Smirnoff 
179608b68b0eSGleb Smirnoff 	if (ifa->ifa_carp) {
179708b68b0eSGleb Smirnoff 		if (ifa->ifa_carp->sc_vhid != vhid)
179808b68b0eSGleb Smirnoff 			carp_detach(ifa);
179908b68b0eSGleb Smirnoff 		else
180008b68b0eSGleb Smirnoff 			return (0);
180108b68b0eSGleb Smirnoff 	}
180208b68b0eSGleb Smirnoff 
180308b68b0eSGleb Smirnoff 	error = carp_multicast_setup(sc, ifa->ifa_addr->sa_family);
180408b68b0eSGleb Smirnoff 	if (error)
180508b68b0eSGleb Smirnoff 		return (error);
180608b68b0eSGleb Smirnoff 
180708b68b0eSGleb Smirnoff 	CARP_LOCK(sc);
180808b68b0eSGleb Smirnoff 	index = sc->sc_naddrs + sc->sc_naddrs6 + 1;
180908b68b0eSGleb Smirnoff 	if (index > sc->sc_ifasiz / sizeof(struct ifaddr *))
181008b68b0eSGleb Smirnoff 		if ((error = carp_grow_ifas(sc)) != 0) {
181108b68b0eSGleb Smirnoff 			carp_multicast_cleanup(sc,
181208b68b0eSGleb Smirnoff 			    ifa->ifa_addr->sa_family);
181308b68b0eSGleb Smirnoff 			CARP_UNLOCK(sc);
181408b68b0eSGleb Smirnoff 			return (error);
181508b68b0eSGleb Smirnoff 		}
181608b68b0eSGleb Smirnoff 
181708b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
181808b68b0eSGleb Smirnoff #ifdef INET
181908b68b0eSGleb Smirnoff 	case AF_INET:
182008b68b0eSGleb Smirnoff 		sc->sc_naddrs++;
182108b68b0eSGleb Smirnoff 		break;
182208b68b0eSGleb Smirnoff #endif
182308b68b0eSGleb Smirnoff #ifdef INET6
182408b68b0eSGleb Smirnoff 	case AF_INET6:
182508b68b0eSGleb Smirnoff 		sc->sc_naddrs6++;
182608b68b0eSGleb Smirnoff 		break;
182708b68b0eSGleb Smirnoff #endif
182808b68b0eSGleb Smirnoff 	}
182908b68b0eSGleb Smirnoff 
183008b68b0eSGleb Smirnoff 	ifa_ref(ifa);
183108b68b0eSGleb Smirnoff 	sc->sc_ifas[index - 1] = ifa;
183208b68b0eSGleb Smirnoff 	ifa->ifa_carp = sc;
183308b68b0eSGleb Smirnoff 
183408b68b0eSGleb Smirnoff 	carp_hmac_prepare(sc);
183508b68b0eSGleb Smirnoff 	carp_sc_state(sc);
183608b68b0eSGleb Smirnoff 
183708b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
183808b68b0eSGleb Smirnoff 
183908b68b0eSGleb Smirnoff 	return (0);
184008b68b0eSGleb Smirnoff }
184108b68b0eSGleb Smirnoff 
184208b68b0eSGleb Smirnoff void
184308b68b0eSGleb Smirnoff carp_detach(struct ifaddr *ifa)
184408b68b0eSGleb Smirnoff {
184508b68b0eSGleb Smirnoff 	struct carp_softc *sc = ifa->ifa_carp;
184608b68b0eSGleb Smirnoff 	int i, index;
184708b68b0eSGleb Smirnoff 
184808b68b0eSGleb Smirnoff 	KASSERT(sc != NULL, ("%s: %p not attached", __func__, ifa));
184908b68b0eSGleb Smirnoff 
185008b68b0eSGleb Smirnoff 	CARP_LOCK(sc);
185108b68b0eSGleb Smirnoff 
185208b68b0eSGleb Smirnoff 	/* Shift array. */
185308b68b0eSGleb Smirnoff 	index = sc->sc_naddrs + sc->sc_naddrs6;
185408b68b0eSGleb Smirnoff 	for (i = 0; i < index; i++)
185508b68b0eSGleb Smirnoff 		if (sc->sc_ifas[i] == ifa)
185608b68b0eSGleb Smirnoff 			break;
185708b68b0eSGleb Smirnoff 	KASSERT(i < index, ("%s: %p no backref", __func__, ifa));
185808b68b0eSGleb Smirnoff 	for (; i < index - 1; i++)
185908b68b0eSGleb Smirnoff 		sc->sc_ifas[i] = sc->sc_ifas[i+1];
186008b68b0eSGleb Smirnoff 	sc->sc_ifas[index - 1] = NULL;
186108b68b0eSGleb Smirnoff 
186208b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
186308b68b0eSGleb Smirnoff #ifdef INET
186408b68b0eSGleb Smirnoff 	case AF_INET:
186508b68b0eSGleb Smirnoff 		sc->sc_naddrs--;
186608b68b0eSGleb Smirnoff 		break;
186708b68b0eSGleb Smirnoff #endif
186808b68b0eSGleb Smirnoff #ifdef INET6
186908b68b0eSGleb Smirnoff 	case AF_INET6:
187008b68b0eSGleb Smirnoff 		sc->sc_naddrs6--;
187108b68b0eSGleb Smirnoff 		break;
187208b68b0eSGleb Smirnoff #endif
187308b68b0eSGleb Smirnoff 	}
187408b68b0eSGleb Smirnoff 
1875*2512b096SGleb Smirnoff 	carp_ifa_delroute(ifa);
187608b68b0eSGleb Smirnoff 	carp_multicast_cleanup(sc, ifa->ifa_addr->sa_family);
187708b68b0eSGleb Smirnoff 
187808b68b0eSGleb Smirnoff 	ifa->ifa_carp = NULL;
187908b68b0eSGleb Smirnoff 	ifa_free(ifa);
188008b68b0eSGleb Smirnoff 
188108b68b0eSGleb Smirnoff 	carp_hmac_prepare(sc);
188208b68b0eSGleb Smirnoff 	carp_sc_state(sc);
188308b68b0eSGleb Smirnoff 
188408b68b0eSGleb Smirnoff 	if (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) {
188508b68b0eSGleb Smirnoff 		CARP_UNLOCK(sc);
188608b68b0eSGleb Smirnoff 		carp_destroy(sc);
188708b68b0eSGleb Smirnoff 	} else
188808b68b0eSGleb Smirnoff 		CARP_UNLOCK(sc);
188908b68b0eSGleb Smirnoff }
189008b68b0eSGleb Smirnoff 
189108b68b0eSGleb Smirnoff static void
189208b68b0eSGleb Smirnoff carp_set_state(struct carp_softc *sc, int state)
189308b68b0eSGleb Smirnoff {
189408b68b0eSGleb Smirnoff 
189508b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
189608b68b0eSGleb Smirnoff 
189708b68b0eSGleb Smirnoff 	if (sc->sc_state != state) {
189808b68b0eSGleb Smirnoff 		const char *carp_states[] = { CARP_STATES };
189908b68b0eSGleb Smirnoff 		char subsys[IFNAMSIZ+5];
190008b68b0eSGleb Smirnoff 
190108b68b0eSGleb Smirnoff 		sc->sc_state = state;
190208b68b0eSGleb Smirnoff 
190308b68b0eSGleb Smirnoff 		snprintf(subsys, IFNAMSIZ+5, "%u@%s", sc->sc_vhid,
190408b68b0eSGleb Smirnoff 		    sc->sc_carpdev->if_xname);
190508b68b0eSGleb Smirnoff 		devctl_notify("CARP", subsys, carp_states[state], NULL);
190608b68b0eSGleb Smirnoff 	}
190708b68b0eSGleb Smirnoff }
190808b68b0eSGleb Smirnoff 
190908b68b0eSGleb Smirnoff static void
191008b68b0eSGleb Smirnoff carp_linkstate(struct ifnet *ifp)
1911d220759bSGleb Smirnoff {
1912d220759bSGleb Smirnoff 	struct carp_softc *sc;
1913d220759bSGleb Smirnoff 
191408b68b0eSGleb Smirnoff 	CIF_LOCK(ifp->if_carp);
191508b68b0eSGleb Smirnoff 	IFNET_FOREACH_CARP(ifp, sc) {
191608b68b0eSGleb Smirnoff 		CARP_LOCK(sc);
191708b68b0eSGleb Smirnoff 		carp_sc_state(sc);
191808b68b0eSGleb Smirnoff 		CARP_UNLOCK(sc);
191908b68b0eSGleb Smirnoff 	}
192008b68b0eSGleb Smirnoff 	CIF_UNLOCK(ifp->if_carp);
19214cb39345SGleb Smirnoff }
19224cb39345SGleb Smirnoff 
19234cb39345SGleb Smirnoff static void
192408b68b0eSGleb Smirnoff carp_sc_state(struct carp_softc *sc)
19254cb39345SGleb Smirnoff {
192608b68b0eSGleb Smirnoff 
192708b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
19284cb39345SGleb Smirnoff 
1929e8c34a71SGleb Smirnoff 	if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1930e8c34a71SGleb Smirnoff 	    !(sc->sc_carpdev->if_flags & IFF_UP)) {
1931a9771948SGleb Smirnoff 		callout_stop(&sc->sc_ad_tmo);
193208b68b0eSGleb Smirnoff #ifdef INET
1933a9771948SGleb Smirnoff 		callout_stop(&sc->sc_md_tmo);
193408b68b0eSGleb Smirnoff #endif
193508b68b0eSGleb Smirnoff #ifdef INET6
1936a9771948SGleb Smirnoff 		callout_stop(&sc->sc_md6_tmo);
193708b68b0eSGleb Smirnoff #endif
1938a9771948SGleb Smirnoff 		carp_set_state(sc, INIT);
1939a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
1940f08535f8SGleb Smirnoff 		if (!sc->sc_suppress)
1941f08535f8SGleb Smirnoff 			carp_demote_adj(carp_ifdown_adj, "interface down");
1942a9771948SGleb Smirnoff 		sc->sc_suppress = 1;
1943a9771948SGleb Smirnoff 	} else {
1944a9771948SGleb Smirnoff 		carp_set_state(sc, INIT);
1945a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
1946a9771948SGleb Smirnoff 		if (sc->sc_suppress)
1947f08535f8SGleb Smirnoff 			carp_demote_adj(-carp_ifdown_adj, "interface up");
1948a9771948SGleb Smirnoff 		sc->sc_suppress = 0;
1949a9771948SGleb Smirnoff 	}
1950a9771948SGleb Smirnoff }
1951a9771948SGleb Smirnoff 
1952f08535f8SGleb Smirnoff static void
1953f08535f8SGleb Smirnoff carp_demote_adj(int adj, char *reason)
1954f08535f8SGleb Smirnoff {
1955f08535f8SGleb Smirnoff 	carp_demotion += adj;
1956f08535f8SGleb Smirnoff 	CARP_LOG("demoted by %d to %d (%s)\n", adj, carp_demotion, reason);
1957f08535f8SGleb Smirnoff 	taskqueue_enqueue(taskqueue_swi, &carp_sendall_task);
1958f08535f8SGleb Smirnoff }
195908b68b0eSGleb Smirnoff 
196054bfbd51SWill Andrews #ifdef INET
196154bfbd51SWill Andrews extern  struct domain inetdomain;
196254bfbd51SWill Andrews static struct protosw in_carp_protosw = {
196354bfbd51SWill Andrews 	.pr_type =		SOCK_RAW,
196454bfbd51SWill Andrews 	.pr_domain =		&inetdomain,
196554bfbd51SWill Andrews 	.pr_protocol =		IPPROTO_CARP,
196654bfbd51SWill Andrews 	.pr_flags =		PR_ATOMIC|PR_ADDR,
196754bfbd51SWill Andrews 	.pr_input =		carp_input,
196854bfbd51SWill Andrews 	.pr_output =		(pr_output_t *)rip_output,
196954bfbd51SWill Andrews 	.pr_ctloutput =		rip_ctloutput,
197054bfbd51SWill Andrews 	.pr_usrreqs =		&rip_usrreqs
197154bfbd51SWill Andrews };
197254bfbd51SWill Andrews #endif
197354bfbd51SWill Andrews 
197454bfbd51SWill Andrews #ifdef INET6
197554bfbd51SWill Andrews extern	struct domain inet6domain;
197654bfbd51SWill Andrews static struct ip6protosw in6_carp_protosw = {
197754bfbd51SWill Andrews 	.pr_type =		SOCK_RAW,
197854bfbd51SWill Andrews 	.pr_domain =		&inet6domain,
197954bfbd51SWill Andrews 	.pr_protocol =		IPPROTO_CARP,
198054bfbd51SWill Andrews 	.pr_flags =		PR_ATOMIC|PR_ADDR,
198154bfbd51SWill Andrews 	.pr_input =		carp6_input,
198254bfbd51SWill Andrews 	.pr_output =		rip6_output,
198354bfbd51SWill Andrews 	.pr_ctloutput =		rip6_ctloutput,
198454bfbd51SWill Andrews 	.pr_usrreqs =		&rip6_usrreqs
198554bfbd51SWill Andrews };
198654bfbd51SWill Andrews #endif
198754bfbd51SWill Andrews 
198854bfbd51SWill Andrews static void
198954bfbd51SWill Andrews carp_mod_cleanup(void)
1990a9771948SGleb Smirnoff {
199154bfbd51SWill Andrews 
199254bfbd51SWill Andrews #ifdef INET
199354bfbd51SWill Andrews 	if (proto_reg[CARP_INET] == 0) {
199415249f73SWill Andrews 		(void)ipproto_unregister(IPPROTO_CARP);
199554bfbd51SWill Andrews 		pf_proto_unregister(PF_INET, IPPROTO_CARP, SOCK_RAW);
199654bfbd51SWill Andrews 		proto_reg[CARP_INET] = -1;
199754bfbd51SWill Andrews 	}
199854bfbd51SWill Andrews 	carp_iamatch_p = NULL;
199954bfbd51SWill Andrews #endif
200054bfbd51SWill Andrews #ifdef INET6
200154bfbd51SWill Andrews 	if (proto_reg[CARP_INET6] == 0) {
200215249f73SWill Andrews 		(void)ip6proto_unregister(IPPROTO_CARP);
200354bfbd51SWill Andrews 		pf_proto_unregister(PF_INET6, IPPROTO_CARP, SOCK_RAW);
200454bfbd51SWill Andrews 		proto_reg[CARP_INET6] = -1;
200554bfbd51SWill Andrews 	}
200654bfbd51SWill Andrews 	carp_iamatch6_p = NULL;
200754bfbd51SWill Andrews 	carp_macmatch6_p = NULL;
200854bfbd51SWill Andrews #endif
200908b68b0eSGleb Smirnoff 	carp_ioctl_p = NULL;
201008b68b0eSGleb Smirnoff 	carp_attach_p = NULL;
201108b68b0eSGleb Smirnoff 	carp_detach_p = NULL;
201208b68b0eSGleb Smirnoff 	carp_get_vhid_p = NULL;
201354bfbd51SWill Andrews 	carp_linkstate_p = NULL;
201454bfbd51SWill Andrews 	carp_forus_p = NULL;
201554bfbd51SWill Andrews 	carp_output_p = NULL;
2016f08535f8SGleb Smirnoff 	carp_demote_adj_p = NULL;
2017f08535f8SGleb Smirnoff 	mtx_unlock(&carp_mtx);
2018f08535f8SGleb Smirnoff 	taskqueue_drain(taskqueue_swi, &carp_sendall_task);
201954bfbd51SWill Andrews 	mtx_destroy(&carp_mtx);
202054bfbd51SWill Andrews }
202154bfbd51SWill Andrews 
202254bfbd51SWill Andrews static int
202354bfbd51SWill Andrews carp_mod_load(void)
202454bfbd51SWill Andrews {
202515249f73SWill Andrews 	int err;
202654bfbd51SWill Andrews 
2027d92d54d5SGleb Smirnoff 	mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
202808b68b0eSGleb Smirnoff 	LIST_INIT(&carp_list);
202908b68b0eSGleb Smirnoff 	carp_get_vhid_p = carp_get_vhid;
203054bfbd51SWill Andrews 	carp_forus_p = carp_forus;
203154bfbd51SWill Andrews 	carp_output_p = carp_output;
203208b68b0eSGleb Smirnoff 	carp_linkstate_p = carp_linkstate;
203308b68b0eSGleb Smirnoff 	carp_ioctl_p = carp_ioctl;
203408b68b0eSGleb Smirnoff 	carp_attach_p = carp_attach;
203508b68b0eSGleb Smirnoff 	carp_detach_p = carp_detach;
2036f08535f8SGleb Smirnoff 	carp_demote_adj_p = carp_demote_adj;
203754bfbd51SWill Andrews #ifdef INET6
203854bfbd51SWill Andrews 	carp_iamatch6_p = carp_iamatch6;
203954bfbd51SWill Andrews 	carp_macmatch6_p = carp_macmatch6;
204054bfbd51SWill Andrews 	proto_reg[CARP_INET6] = pf_proto_register(PF_INET6,
204154bfbd51SWill Andrews 	    (struct protosw *)&in6_carp_protosw);
204208b68b0eSGleb Smirnoff 	if (proto_reg[CARP_INET6]) {
204354bfbd51SWill Andrews 		printf("carp: error %d attaching to PF_INET6\n",
204454bfbd51SWill Andrews 		    proto_reg[CARP_INET6]);
204554bfbd51SWill Andrews 		carp_mod_cleanup();
20466baf7a24SGleb Smirnoff 		return (proto_reg[CARP_INET6]);
204754bfbd51SWill Andrews 	}
204815249f73SWill Andrews 	err = ip6proto_register(IPPROTO_CARP);
204915249f73SWill Andrews 	if (err) {
205015249f73SWill Andrews 		printf("carp: error %d registering with INET6\n", err);
205115249f73SWill Andrews 		carp_mod_cleanup();
20526baf7a24SGleb Smirnoff 		return (err);
205315249f73SWill Andrews 	}
205454bfbd51SWill Andrews #endif
205554bfbd51SWill Andrews #ifdef INET
205654bfbd51SWill Andrews 	carp_iamatch_p = carp_iamatch;
205754bfbd51SWill Andrews 	proto_reg[CARP_INET] = pf_proto_register(PF_INET, &in_carp_protosw);
205808b68b0eSGleb Smirnoff 	if (proto_reg[CARP_INET]) {
205954bfbd51SWill Andrews 		printf("carp: error %d attaching to PF_INET\n",
206054bfbd51SWill Andrews 		    proto_reg[CARP_INET]);
206154bfbd51SWill Andrews 		carp_mod_cleanup();
20626baf7a24SGleb Smirnoff 		return (proto_reg[CARP_INET]);
206354bfbd51SWill Andrews 	}
206415249f73SWill Andrews 	err = ipproto_register(IPPROTO_CARP);
206515249f73SWill Andrews 	if (err) {
206615249f73SWill Andrews 		printf("carp: error %d registering with INET\n", err);
206715249f73SWill Andrews 		carp_mod_cleanup();
20686baf7a24SGleb Smirnoff 		return (err);
206915249f73SWill Andrews 	}
207054bfbd51SWill Andrews #endif
207108b68b0eSGleb Smirnoff 	return (0);
207254bfbd51SWill Andrews }
2073a9771948SGleb Smirnoff 
207454bfbd51SWill Andrews static int
207554bfbd51SWill Andrews carp_modevent(module_t mod, int type, void *data)
207654bfbd51SWill Andrews {
207754bfbd51SWill Andrews 	switch (type) {
207854bfbd51SWill Andrews 	case MOD_LOAD:
207954bfbd51SWill Andrews 		return carp_mod_load();
208054bfbd51SWill Andrews 		/* NOTREACHED */
2081a9771948SGleb Smirnoff 	case MOD_UNLOAD:
208208b68b0eSGleb Smirnoff 		mtx_lock(&carp_mtx);
208308b68b0eSGleb Smirnoff 		if (LIST_EMPTY(&carp_list))
208454bfbd51SWill Andrews 			carp_mod_cleanup();
208508b68b0eSGleb Smirnoff 		else {
208608b68b0eSGleb Smirnoff 			mtx_unlock(&carp_mtx);
208754bfbd51SWill Andrews 			return (EBUSY);
208808b68b0eSGleb Smirnoff 		}
2089a9771948SGleb Smirnoff 		break;
2090a9771948SGleb Smirnoff 
2091a9771948SGleb Smirnoff 	default:
20920fa08018SGleb Smirnoff 		return (EINVAL);
2093a9771948SGleb Smirnoff 	}
2094a9771948SGleb Smirnoff 
20950fa08018SGleb Smirnoff 	return (0);
2096a9771948SGleb Smirnoff }
2097a9771948SGleb Smirnoff 
2098a9771948SGleb Smirnoff static moduledata_t carp_mod = {
2099a9771948SGleb Smirnoff 	"carp",
2100a9771948SGleb Smirnoff 	carp_modevent,
2101a9771948SGleb Smirnoff 	0
2102a9771948SGleb Smirnoff };
2103a9771948SGleb Smirnoff 
2104e24fa11dSWill Andrews DECLARE_MODULE(carp, carp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
2105