xref: /freebsd/sys/netinet/ip_carp.c (revision 1019354b54161c140d10a8203ff1e849c09c8010)
108b68b0eSGleb Smirnoff /*-
2fe267a55SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3fe267a55SPedro F. Giffuni  *
408b68b0eSGleb Smirnoff  * Copyright (c) 2002 Michael Shalayeff.
508b68b0eSGleb Smirnoff  * Copyright (c) 2003 Ryan McBride.
608b68b0eSGleb Smirnoff  * Copyright (c) 2011 Gleb Smirnoff <glebius@FreeBSD.org>
708b68b0eSGleb Smirnoff  * All rights reserved.
8a9771948SGleb Smirnoff  *
9a9771948SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
10a9771948SGleb Smirnoff  * modification, are permitted provided that the following conditions
11a9771948SGleb Smirnoff  * are met:
12a9771948SGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
13a9771948SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
14a9771948SGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
15a9771948SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
16a9771948SGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
17a9771948SGleb Smirnoff  *
18a9771948SGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19a9771948SGleb Smirnoff  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20a9771948SGleb Smirnoff  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21a9771948SGleb Smirnoff  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
22a9771948SGleb Smirnoff  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23a9771948SGleb Smirnoff  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24a9771948SGleb Smirnoff  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25a9771948SGleb Smirnoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26a9771948SGleb Smirnoff  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27a9771948SGleb Smirnoff  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28a9771948SGleb Smirnoff  * THE POSSIBILITY OF SUCH DAMAGE.
29a9771948SGleb Smirnoff  */
30a9771948SGleb Smirnoff 
314b421e2dSMike Silbersack #include <sys/cdefs.h>
324b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
334b421e2dSMike Silbersack 
34a9771948SGleb Smirnoff #include "opt_bpf.h"
35a9771948SGleb Smirnoff #include "opt_inet.h"
36a9771948SGleb Smirnoff #include "opt_inet6.h"
37a9771948SGleb Smirnoff 
38a9771948SGleb Smirnoff #include <sys/param.h>
39a9771948SGleb Smirnoff #include <sys/systm.h>
40773e541eSWarner Losh #include <sys/devctl.h>
4108b68b0eSGleb Smirnoff #include <sys/jail.h>
42a9771948SGleb Smirnoff #include <sys/kernel.h>
43a9771948SGleb Smirnoff #include <sys/limits.h>
44a9771948SGleb Smirnoff #include <sys/malloc.h>
45a9771948SGleb Smirnoff #include <sys/mbuf.h>
46a9771948SGleb Smirnoff #include <sys/module.h>
47acd3428bSRobert Watson #include <sys/priv.h>
48a9771948SGleb Smirnoff #include <sys/proc.h>
4954bfbd51SWill Andrews #include <sys/protosw.h>
5008b68b0eSGleb Smirnoff #include <sys/socket.h>
5108b68b0eSGleb Smirnoff #include <sys/sockio.h>
52a9771948SGleb Smirnoff #include <sys/sysctl.h>
53a9771948SGleb Smirnoff #include <sys/syslog.h>
54f08535f8SGleb Smirnoff #include <sys/taskqueue.h>
5569edf037SAndrey V. Elsukov #include <sys/counter.h>
56a9771948SGleb Smirnoff 
57a9771948SGleb Smirnoff #include <net/ethernet.h>
58a9771948SGleb Smirnoff #include <net/if.h>
5976039bc8SGleb Smirnoff #include <net/if_var.h>
60433aaf04SRuslan Ermilov #include <net/if_dl.h>
6108b68b0eSGleb Smirnoff #include <net/if_llatbl.h>
62a9771948SGleb Smirnoff #include <net/if_types.h>
63a9771948SGleb Smirnoff #include <net/route.h>
64530c0060SRobert Watson #include <net/vnet.h>
65a9771948SGleb Smirnoff 
66a0ae8f04SBjoern A. Zeeb #if defined(INET) || defined(INET6)
67a9771948SGleb Smirnoff #include <netinet/in.h>
68a9771948SGleb Smirnoff #include <netinet/in_var.h>
69a0ae8f04SBjoern A. Zeeb #include <netinet/ip_carp.h>
70a9771948SGleb Smirnoff #include <netinet/ip.h>
71a0ae8f04SBjoern A. Zeeb #include <machine/in_cksum.h>
72a0ae8f04SBjoern A. Zeeb #endif
73a0ae8f04SBjoern A. Zeeb #ifdef INET
74a9771948SGleb Smirnoff #include <netinet/ip_var.h>
75a9771948SGleb Smirnoff #include <netinet/if_ether.h>
76a9771948SGleb Smirnoff #endif
77a9771948SGleb Smirnoff 
78a9771948SGleb Smirnoff #ifdef INET6
79a9771948SGleb Smirnoff #include <netinet/icmp6.h>
80a9771948SGleb Smirnoff #include <netinet/ip6.h>
8108b68b0eSGleb Smirnoff #include <netinet6/in6_var.h>
82a9771948SGleb Smirnoff #include <netinet6/ip6_var.h>
8329da8af6SHajimu UMEMOTO #include <netinet6/scope6_var.h>
84a9771948SGleb Smirnoff #include <netinet6/nd6.h>
85a9771948SGleb Smirnoff #endif
86a9771948SGleb Smirnoff 
87a9771948SGleb Smirnoff #include <crypto/sha1.h>
88a9771948SGleb Smirnoff 
8908b68b0eSGleb Smirnoff static MALLOC_DEFINE(M_CARP, "CARP", "CARP addresses");
90a9771948SGleb Smirnoff 
91a9771948SGleb Smirnoff struct carp_softc {
9208b68b0eSGleb Smirnoff 	struct ifnet		*sc_carpdev;	/* Pointer to parent ifnet. */
9308b68b0eSGleb Smirnoff 	struct ifaddr		**sc_ifas;	/* Our ifaddrs. */
9408b68b0eSGleb Smirnoff 	struct sockaddr_dl	sc_addr;	/* Our link level address. */
9508b68b0eSGleb Smirnoff 	struct callout		sc_ad_tmo;	/* Advertising timeout. */
96a0ae8f04SBjoern A. Zeeb #ifdef INET
9708b68b0eSGleb Smirnoff 	struct callout		sc_md_tmo;	/* Master down timeout. */
98a0ae8f04SBjoern A. Zeeb #endif
99a9771948SGleb Smirnoff #ifdef INET6
10008b68b0eSGleb Smirnoff 	struct callout 		sc_md6_tmo;	/* XXX: Master down timeout. */
10108b68b0eSGleb Smirnoff #endif
10208b68b0eSGleb Smirnoff 	struct mtx		sc_mtx;
103a9771948SGleb Smirnoff 
10408b68b0eSGleb Smirnoff 	int			sc_vhid;
10508b68b0eSGleb Smirnoff 	int			sc_advskew;
10608b68b0eSGleb Smirnoff 	int			sc_advbase;
10708b68b0eSGleb Smirnoff 
10808b68b0eSGleb Smirnoff 	int			sc_naddrs;
10908b68b0eSGleb Smirnoff 	int			sc_naddrs6;
11008b68b0eSGleb Smirnoff 	int			sc_ifasiz;
111a9771948SGleb Smirnoff 	enum { INIT = 0, BACKUP, MASTER }	sc_state;
112a9771948SGleb Smirnoff 	int			sc_suppress;
113a9771948SGleb Smirnoff 	int			sc_sendad_errors;
114a9771948SGleb Smirnoff #define	CARP_SENDAD_MAX_ERRORS	3
115a9771948SGleb Smirnoff 	int			sc_sendad_success;
116a9771948SGleb Smirnoff #define	CARP_SENDAD_MIN_SUCCESS 3
117a9771948SGleb Smirnoff 
118a9771948SGleb Smirnoff 	int			sc_init_counter;
11908b68b0eSGleb Smirnoff 	uint64_t		sc_counter;
120a9771948SGleb Smirnoff 
121a9771948SGleb Smirnoff 	/* authentication */
122a9771948SGleb Smirnoff #define	CARP_HMAC_PAD	64
123a9771948SGleb Smirnoff 	unsigned char sc_key[CARP_KEY_LEN];
124a9771948SGleb Smirnoff 	unsigned char sc_pad[CARP_HMAC_PAD];
125a9771948SGleb Smirnoff 	SHA1_CTX sc_sha1;
126a9771948SGleb Smirnoff 
12708b68b0eSGleb Smirnoff 	TAILQ_ENTRY(carp_softc)	sc_list;	/* On the carp_if list. */
12808b68b0eSGleb Smirnoff 	LIST_ENTRY(carp_softc)	sc_next;	/* On the global list. */
129a9771948SGleb Smirnoff };
13008b68b0eSGleb Smirnoff 
13108b68b0eSGleb Smirnoff struct carp_if {
13208b68b0eSGleb Smirnoff #ifdef INET
13308b68b0eSGleb Smirnoff 	int	cif_naddrs;
13408b68b0eSGleb Smirnoff #endif
13508b68b0eSGleb Smirnoff #ifdef INET6
13608b68b0eSGleb Smirnoff 	int	cif_naddrs6;
13708b68b0eSGleb Smirnoff #endif
13808b68b0eSGleb Smirnoff 	TAILQ_HEAD(, carp_softc) cif_vrs;
13908b68b0eSGleb Smirnoff #ifdef INET
14008b68b0eSGleb Smirnoff 	struct ip_moptions 	 cif_imo;
14108b68b0eSGleb Smirnoff #endif
14208b68b0eSGleb Smirnoff #ifdef INET6
14308b68b0eSGleb Smirnoff 	struct ip6_moptions 	 cif_im6o;
14408b68b0eSGleb Smirnoff #endif
14508b68b0eSGleb Smirnoff 	struct ifnet	*cif_ifp;
14608b68b0eSGleb Smirnoff 	struct mtx	cif_mtx;
1470cc726f2SGleb Smirnoff 	uint32_t	cif_flags;
1480cc726f2SGleb Smirnoff #define	CIF_PROMISC	0x00000001
14908b68b0eSGleb Smirnoff };
15008b68b0eSGleb Smirnoff 
15108b68b0eSGleb Smirnoff #define	CARP_INET	0
15208b68b0eSGleb Smirnoff #define	CARP_INET6	1
15308b68b0eSGleb Smirnoff static int proto_reg[] = {-1, -1};
15408b68b0eSGleb Smirnoff 
15508b68b0eSGleb Smirnoff /*
15608b68b0eSGleb Smirnoff  * Brief design of carp(4).
15708b68b0eSGleb Smirnoff  *
15808b68b0eSGleb Smirnoff  * Any carp-capable ifnet may have a list of carp softcs hanging off
15908b68b0eSGleb Smirnoff  * its ifp->if_carp pointer. Each softc represents one unique virtual
16008b68b0eSGleb Smirnoff  * host id, or vhid. The softc has a back pointer to the ifnet. All
16108b68b0eSGleb Smirnoff  * softcs are joined in a global list, which has quite limited use.
16208b68b0eSGleb Smirnoff  *
16308b68b0eSGleb Smirnoff  * Any interface address that takes part in CARP negotiation has a
16408b68b0eSGleb Smirnoff  * pointer to the softc of its vhid, ifa->ifa_carp. That could be either
16508b68b0eSGleb Smirnoff  * AF_INET or AF_INET6 address.
16608b68b0eSGleb Smirnoff  *
16708b68b0eSGleb Smirnoff  * Although, one can get the softc's backpointer to ifnet and traverse
16808b68b0eSGleb Smirnoff  * through its ifp->if_addrhead queue to find all interface addresses
16908b68b0eSGleb Smirnoff  * involved in CARP, we keep a growable array of ifaddr pointers. This
17008b68b0eSGleb Smirnoff  * allows us to avoid grabbing the IF_ADDR_LOCK() in many traversals that
17108b68b0eSGleb Smirnoff  * do calls into the network stack, thus avoiding LORs.
17208b68b0eSGleb Smirnoff  *
17308b68b0eSGleb Smirnoff  * Locking:
17408b68b0eSGleb Smirnoff  *
17508b68b0eSGleb Smirnoff  * Each softc has a lock sc_mtx. It is used to synchronise carp_input_c(),
17608b68b0eSGleb Smirnoff  * callout-driven events and ioctl()s.
17708b68b0eSGleb Smirnoff  *
17881098a01SAlexander Motin  * To traverse the list of softcs on an ifnet we use CIF_LOCK() or carp_sx.
17981098a01SAlexander Motin  * To traverse the global list we use the mutex carp_mtx.
18008b68b0eSGleb Smirnoff  *
18108b68b0eSGleb Smirnoff  * Known issues with locking:
18208b68b0eSGleb Smirnoff  *
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 
189c5c392e7SMikolaj Golub /* Accept incoming CARP packets. */
1905f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_allow) = 1;
191c5c392e7SMikolaj Golub #define	V_carp_allow	VNET(carp_allow)
192c5c392e7SMikolaj Golub 
1930d3d234cSKristof Provost /* Set DSCP in outgoing CARP packets. */
1945f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_dscp) = 56;
1950d3d234cSKristof Provost #define	V_carp_dscp	VNET(carp_dscp)
1960d3d234cSKristof Provost 
197c5c392e7SMikolaj Golub /* Preempt slower nodes. */
1985f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_preempt) = 0;
199c5c392e7SMikolaj Golub #define	V_carp_preempt	VNET(carp_preempt)
200c5c392e7SMikolaj Golub 
201c5c392e7SMikolaj Golub /* Log level. */
2025f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_log) = 1;
203c5c392e7SMikolaj Golub #define	V_carp_log	VNET(carp_log)
204c5c392e7SMikolaj Golub 
205c5c392e7SMikolaj Golub /* Global advskew demotion. */
2065f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_demotion) = 0;
207c5c392e7SMikolaj Golub #define	V_carp_demotion	VNET(carp_demotion)
208c5c392e7SMikolaj Golub 
209c5c392e7SMikolaj Golub /* Send error demotion factor. */
2105f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_senderr_adj) = CARP_MAXSKEW;
211c5c392e7SMikolaj Golub #define	V_carp_senderr_adj	VNET(carp_senderr_adj)
212c5c392e7SMikolaj Golub 
213c5c392e7SMikolaj Golub /* Iface down demotion factor. */
2145f901c92SAndrew Turner VNET_DEFINE_STATIC(int, carp_ifdown_adj) = CARP_MAXSKEW;
215c5c392e7SMikolaj Golub #define	V_carp_ifdown_adj	VNET(carp_ifdown_adj)
216c5c392e7SMikolaj Golub 
217167a3440SAlexander Motin static int carp_allow_sysctl(SYSCTL_HANDLER_ARGS);
2180d3d234cSKristof Provost static int carp_dscp_sysctl(SYSCTL_HANDLER_ARGS);
2197951008bSGleb Smirnoff static int carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS);
220a9771948SGleb Smirnoff 
22110b49b23SPawel Biernacki SYSCTL_NODE(_net_inet, IPPROTO_CARP, carp, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
22210b49b23SPawel Biernacki     "CARP");
223167a3440SAlexander Motin SYSCTL_PROC(_net_inet_carp, OID_AUTO, allow,
22410b49b23SPawel Biernacki     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
22510b49b23SPawel Biernacki     0, 0, carp_allow_sysctl, "I",
226167a3440SAlexander Motin     "Accept incoming CARP packets");
2270d3d234cSKristof Provost SYSCTL_PROC(_net_inet_carp, OID_AUTO, dscp,
22810b49b23SPawel Biernacki     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
22910b49b23SPawel Biernacki     0, 0, carp_dscp_sysctl, "I",
2300d3d234cSKristof Provost     "DSCP value for carp packets");
2316df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, preempt, CTLFLAG_VNET | CTLFLAG_RW,
232c5c392e7SMikolaj Golub     &VNET_NAME(carp_preempt), 0, "High-priority backup preemption mode");
2336df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, log, CTLFLAG_VNET | CTLFLAG_RW,
234c5c392e7SMikolaj Golub     &VNET_NAME(carp_log), 0, "CARP log level");
2356df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_carp, OID_AUTO, demotion,
23610b49b23SPawel Biernacki     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
2377951008bSGleb Smirnoff     0, 0, carp_demote_adj_sysctl, "I",
2387951008bSGleb Smirnoff     "Adjust demotion factor (skew of advskew)");
2396df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, senderr_demotion_factor,
2406df8a710SGleb Smirnoff     CTLFLAG_VNET | CTLFLAG_RW,
241c5c392e7SMikolaj Golub     &VNET_NAME(carp_senderr_adj), 0, "Send error demotion factor adjustment");
2426df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, ifdown_demotion_factor,
2436df8a710SGleb Smirnoff     CTLFLAG_VNET | CTLFLAG_RW,
244c5c392e7SMikolaj Golub     &VNET_NAME(carp_ifdown_adj), 0,
245c5c392e7SMikolaj Golub     "Interface down demotion factor adjustment");
246f08535f8SGleb Smirnoff 
247c5c392e7SMikolaj Golub VNET_PCPUSTAT_DEFINE(struct carpstats, carpstats);
248c5c392e7SMikolaj Golub VNET_PCPUSTAT_SYSINIT(carpstats);
249c5c392e7SMikolaj Golub VNET_PCPUSTAT_SYSUNINIT(carpstats);
250c5c392e7SMikolaj Golub 
25169edf037SAndrey V. Elsukov #define	CARPSTATS_ADD(name, val)	\
252c5c392e7SMikolaj Golub     counter_u64_add(VNET(carpstats)[offsetof(struct carpstats, name) / \
25369edf037SAndrey V. Elsukov 	sizeof(uint64_t)], (val))
25469edf037SAndrey V. Elsukov #define	CARPSTATS_INC(name)		CARPSTATS_ADD(name, 1)
25569edf037SAndrey V. Elsukov 
256c5c392e7SMikolaj Golub SYSCTL_VNET_PCPUSTAT(_net_inet_carp, OID_AUTO, stats, struct carpstats,
257c5c392e7SMikolaj Golub     carpstats, "CARP statistics (struct carpstats, netinet/ip_carp.h)");
258a9771948SGleb Smirnoff 
25908b68b0eSGleb Smirnoff #define	CARP_LOCK_INIT(sc)	mtx_init(&(sc)->sc_mtx, "carp_softc",   \
260a9771948SGleb Smirnoff 	NULL, MTX_DEF)
26108b68b0eSGleb Smirnoff #define	CARP_LOCK_DESTROY(sc)	mtx_destroy(&(sc)->sc_mtx)
26208b68b0eSGleb Smirnoff #define	CARP_LOCK_ASSERT(sc)	mtx_assert(&(sc)->sc_mtx, MA_OWNED)
26308b68b0eSGleb Smirnoff #define	CARP_LOCK(sc)		mtx_lock(&(sc)->sc_mtx)
26408b68b0eSGleb Smirnoff #define	CARP_UNLOCK(sc)		mtx_unlock(&(sc)->sc_mtx)
26508b68b0eSGleb Smirnoff #define	CIF_LOCK_INIT(cif)	mtx_init(&(cif)->cif_mtx, "carp_if",   \
26608b68b0eSGleb Smirnoff 	NULL, MTX_DEF)
26708b68b0eSGleb Smirnoff #define	CIF_LOCK_DESTROY(cif)	mtx_destroy(&(cif)->cif_mtx)
26808b68b0eSGleb Smirnoff #define	CIF_LOCK_ASSERT(cif)	mtx_assert(&(cif)->cif_mtx, MA_OWNED)
26908b68b0eSGleb Smirnoff #define	CIF_LOCK(cif)		mtx_lock(&(cif)->cif_mtx)
27008b68b0eSGleb Smirnoff #define	CIF_UNLOCK(cif)		mtx_unlock(&(cif)->cif_mtx)
271a9a2c40cSGleb Smirnoff #define	CIF_FREE(cif)	do {				\
2729c2cd1aaSGleb Smirnoff 		CIF_LOCK(cif);				\
273a9a2c40cSGleb Smirnoff 		if (TAILQ_EMPTY(&(cif)->cif_vrs))	\
274a9a2c40cSGleb Smirnoff 			carp_free_if(cif);		\
275a9a2c40cSGleb Smirnoff 		else					\
276a9a2c40cSGleb Smirnoff 			CIF_UNLOCK(cif);		\
277a9a2c40cSGleb Smirnoff } while (0)
278d220759bSGleb Smirnoff 
279947b7cf3SGleb Smirnoff #define	CARP_LOG(...)	do {				\
280c5c392e7SMikolaj Golub 	if (V_carp_log > 0)				\
28108b68b0eSGleb Smirnoff 		log(LOG_INFO, "carp: " __VA_ARGS__);	\
282947b7cf3SGleb Smirnoff } while (0)
2831e9e6572SGleb Smirnoff 
284947b7cf3SGleb Smirnoff #define	CARP_DEBUG(...)	do {				\
285c5c392e7SMikolaj Golub 	if (V_carp_log > 1)				\
2861e9e6572SGleb Smirnoff 		log(LOG_DEBUG, __VA_ARGS__);		\
287947b7cf3SGleb Smirnoff } while (0)
288a9771948SGleb Smirnoff 
28908b68b0eSGleb Smirnoff #define	IFNET_FOREACH_IFA(ifp, ifa)					\
290d7c5a620SMatt Macy 	CK_STAILQ_FOREACH((ifa), &(ifp)->if_addrhead, ifa_link)	\
29108b68b0eSGleb Smirnoff 		if ((ifa)->ifa_carp != NULL)
29208b68b0eSGleb Smirnoff 
29308b68b0eSGleb Smirnoff #define	CARP_FOREACH_IFA(sc, ifa)					\
29408b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);						\
29508b68b0eSGleb Smirnoff 	for (int _i = 0;						\
29608b68b0eSGleb Smirnoff 		_i < (sc)->sc_naddrs + (sc)->sc_naddrs6 &&		\
29708b68b0eSGleb Smirnoff 		((ifa) = sc->sc_ifas[_i]) != NULL;			\
29808b68b0eSGleb Smirnoff 		++_i)
29908b68b0eSGleb Smirnoff 
30008b68b0eSGleb Smirnoff #define	IFNET_FOREACH_CARP(ifp, sc)					\
30181098a01SAlexander Motin 	KASSERT(mtx_owned(&ifp->if_carp->cif_mtx) ||			\
30281098a01SAlexander Motin 	    sx_xlocked(&carp_sx), ("cif_vrs not locked"));		\
30308b68b0eSGleb Smirnoff 	TAILQ_FOREACH((sc), &(ifp)->if_carp->cif_vrs, sc_list)
30408b68b0eSGleb Smirnoff 
305f08535f8SGleb Smirnoff #define	DEMOTE_ADVSKEW(sc)					\
306c5c392e7SMikolaj Golub     (((sc)->sc_advskew + V_carp_demotion > CARP_MAXSKEW) ?	\
307*1019354bSMarius Halden     CARP_MAXSKEW :						\
308*1019354bSMarius Halden         (((sc)->sc_advskew + V_carp_demotion < 0) ?		\
309*1019354bSMarius Halden         0 : ((sc)->sc_advskew + V_carp_demotion)))
310f08535f8SGleb Smirnoff 
3115c1f0f6dSGleb Smirnoff static void	carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
31208b68b0eSGleb Smirnoff static struct carp_softc
31308b68b0eSGleb Smirnoff 		*carp_alloc(struct ifnet *);
31408b68b0eSGleb Smirnoff static void	carp_destroy(struct carp_softc *);
31508b68b0eSGleb Smirnoff static struct carp_if
31608b68b0eSGleb Smirnoff 		*carp_alloc_if(struct ifnet *);
31708b68b0eSGleb Smirnoff static void	carp_free_if(struct carp_if *);
318d01641e2SWill Andrews static void	carp_set_state(struct carp_softc *, int, const char* reason);
31908b68b0eSGleb Smirnoff static void	carp_sc_state(struct carp_softc *);
32008b68b0eSGleb Smirnoff static void	carp_setrun(struct carp_softc *, sa_family_t);
3215c1f0f6dSGleb Smirnoff static void	carp_master_down(void *);
322d01641e2SWill Andrews static void	carp_master_down_locked(struct carp_softc *,
323d01641e2SWill Andrews     		    const char* reason);
32408b68b0eSGleb Smirnoff static void	carp_send_ad(void *);
32508b68b0eSGleb Smirnoff static void	carp_send_ad_locked(struct carp_softc *);
32608b68b0eSGleb Smirnoff static void	carp_addroute(struct carp_softc *);
3272512b096SGleb Smirnoff static void	carp_ifa_addroute(struct ifaddr *);
32808b68b0eSGleb Smirnoff static void	carp_delroute(struct carp_softc *);
3292512b096SGleb Smirnoff static void	carp_ifa_delroute(struct ifaddr *);
330f08535f8SGleb Smirnoff static void	carp_send_ad_all(void *, int);
331f08535f8SGleb Smirnoff static void	carp_demote_adj(int, char *);
332a9771948SGleb Smirnoff 
33308b68b0eSGleb Smirnoff static LIST_HEAD(, carp_softc) carp_list;
334d92d54d5SGleb Smirnoff static struct mtx carp_mtx;
33593d4534cSGleb Smirnoff static struct sx carp_sx;
336f08535f8SGleb Smirnoff static struct task carp_sendall_task =
337f08535f8SGleb Smirnoff     TASK_INITIALIZER(0, carp_send_ad_all, NULL);
338a9771948SGleb Smirnoff 
3395c1f0f6dSGleb Smirnoff static void
340a9771948SGleb Smirnoff carp_hmac_prepare(struct carp_softc *sc)
341a9771948SGleb Smirnoff {
34208b68b0eSGleb Smirnoff 	uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
34308b68b0eSGleb Smirnoff 	uint8_t vhid = sc->sc_vhid & 0xff;
344a9771948SGleb Smirnoff 	struct ifaddr *ifa;
3451ead26d4SMax Laier 	int i, found;
3461ead26d4SMax Laier #ifdef INET
3471ead26d4SMax Laier 	struct in_addr last, cur, in;
3481ead26d4SMax Laier #endif
349a9771948SGleb Smirnoff #ifdef INET6
3501ead26d4SMax Laier 	struct in6_addr last6, cur6, in6;
351a9771948SGleb Smirnoff #endif
352a9771948SGleb Smirnoff 
35308b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
354d220759bSGleb Smirnoff 
35508b68b0eSGleb Smirnoff 	/* Compute ipad from key. */
356a9771948SGleb Smirnoff 	bzero(sc->sc_pad, sizeof(sc->sc_pad));
357a9771948SGleb Smirnoff 	bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
358a9771948SGleb Smirnoff 	for (i = 0; i < sizeof(sc->sc_pad); i++)
359a9771948SGleb Smirnoff 		sc->sc_pad[i] ^= 0x36;
360a9771948SGleb Smirnoff 
36108b68b0eSGleb Smirnoff 	/* Precompute first part of inner hash. */
362a9771948SGleb Smirnoff 	SHA1Init(&sc->sc_sha1);
363a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
364a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
365a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
366a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
367a9771948SGleb Smirnoff #ifdef INET
3681ead26d4SMax Laier 	cur.s_addr = 0;
3691ead26d4SMax Laier 	do {
3701ead26d4SMax Laier 		found = 0;
3711ead26d4SMax Laier 		last = cur;
3721ead26d4SMax Laier 		cur.s_addr = 0xffffffff;
37308b68b0eSGleb Smirnoff 		CARP_FOREACH_IFA(sc, ifa) {
3741ead26d4SMax Laier 			in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr;
3751ead26d4SMax Laier 			if (ifa->ifa_addr->sa_family == AF_INET &&
3761ead26d4SMax Laier 			    ntohl(in.s_addr) > ntohl(last.s_addr) &&
3771ead26d4SMax Laier 			    ntohl(in.s_addr) < ntohl(cur.s_addr)) {
3781ead26d4SMax Laier 				cur.s_addr = in.s_addr;
3791ead26d4SMax Laier 				found++;
380a9771948SGleb Smirnoff 			}
3811ead26d4SMax Laier 		}
3821ead26d4SMax Laier 		if (found)
3831ead26d4SMax Laier 			SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur));
3841ead26d4SMax Laier 	} while (found);
385a9771948SGleb Smirnoff #endif /* INET */
386a9771948SGleb Smirnoff #ifdef INET6
3871ead26d4SMax Laier 	memset(&cur6, 0, sizeof(cur6));
3881ead26d4SMax Laier 	do {
3891ead26d4SMax Laier 		found = 0;
3901ead26d4SMax Laier 		last6 = cur6;
3911ead26d4SMax Laier 		memset(&cur6, 0xff, sizeof(cur6));
39208b68b0eSGleb Smirnoff 		CARP_FOREACH_IFA(sc, ifa) {
393a9771948SGleb Smirnoff 			in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
3941ead26d4SMax Laier 			if (IN6_IS_SCOPE_EMBED(&in6))
3951ead26d4SMax Laier 				in6.s6_addr16[1] = 0;
3961ead26d4SMax Laier 			if (ifa->ifa_addr->sa_family == AF_INET6 &&
3971ead26d4SMax Laier 			    memcmp(&in6, &last6, sizeof(in6)) > 0 &&
3981ead26d4SMax Laier 			    memcmp(&in6, &cur6, sizeof(in6)) < 0) {
3991ead26d4SMax Laier 				cur6 = in6;
4001ead26d4SMax Laier 				found++;
401a9771948SGleb Smirnoff 			}
402a9771948SGleb Smirnoff 		}
4031ead26d4SMax Laier 		if (found)
4041ead26d4SMax Laier 			SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6));
4051ead26d4SMax Laier 	} while (found);
406a9771948SGleb Smirnoff #endif /* INET6 */
407a9771948SGleb Smirnoff 
408a9771948SGleb Smirnoff 	/* convert ipad to opad */
409a9771948SGleb Smirnoff 	for (i = 0; i < sizeof(sc->sc_pad); i++)
410a9771948SGleb Smirnoff 		sc->sc_pad[i] ^= 0x36 ^ 0x5c;
411a9771948SGleb Smirnoff }
412a9771948SGleb Smirnoff 
4135c1f0f6dSGleb Smirnoff static void
41408b68b0eSGleb Smirnoff carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
415a9771948SGleb Smirnoff     unsigned char md[20])
416a9771948SGleb Smirnoff {
417a9771948SGleb Smirnoff 	SHA1_CTX sha1ctx;
418a9771948SGleb Smirnoff 
41908b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
42008b68b0eSGleb Smirnoff 
421a9771948SGleb Smirnoff 	/* fetch first half of inner hash */
422a9771948SGleb Smirnoff 	bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
423a9771948SGleb Smirnoff 
424a9771948SGleb Smirnoff 	SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
425a9771948SGleb Smirnoff 	SHA1Final(md, &sha1ctx);
426a9771948SGleb Smirnoff 
427a9771948SGleb Smirnoff 	/* outer hash */
428a9771948SGleb Smirnoff 	SHA1Init(&sha1ctx);
429a9771948SGleb Smirnoff 	SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
430a9771948SGleb Smirnoff 	SHA1Update(&sha1ctx, md, 20);
431a9771948SGleb Smirnoff 	SHA1Final(md, &sha1ctx);
432a9771948SGleb Smirnoff }
433a9771948SGleb Smirnoff 
4345c1f0f6dSGleb Smirnoff static int
43508b68b0eSGleb Smirnoff carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
436a9771948SGleb Smirnoff     unsigned char md[20])
437a9771948SGleb Smirnoff {
438a9771948SGleb Smirnoff 	unsigned char md2[20];
439a9771948SGleb Smirnoff 
44008b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
441d220759bSGleb Smirnoff 
442a9771948SGleb Smirnoff 	carp_hmac_generate(sc, counter, md2);
443a9771948SGleb Smirnoff 
444a9771948SGleb Smirnoff 	return (bcmp(md, md2, sizeof(md2)));
445a9771948SGleb Smirnoff }
446a9771948SGleb Smirnoff 
447a9771948SGleb Smirnoff /*
448a9771948SGleb Smirnoff  * process input packet.
449a9771948SGleb Smirnoff  * we have rearranged checks order compared to the rfc,
450a9771948SGleb Smirnoff  * but it seems more efficient this way or not possible otherwise.
451a9771948SGleb Smirnoff  */
452a0ae8f04SBjoern A. Zeeb #ifdef INET
4538f5a8818SKevin Lo int
4548f5a8818SKevin Lo carp_input(struct mbuf **mp, int *offp, int proto)
455a9771948SGleb Smirnoff {
4568f5a8818SKevin Lo 	struct mbuf *m = *mp;
457a9771948SGleb Smirnoff 	struct ip *ip = mtod(m, struct ip *);
458a9771948SGleb Smirnoff 	struct carp_header *ch;
459a9771948SGleb Smirnoff 	int iplen, len;
460a9771948SGleb Smirnoff 
4618f5a8818SKevin Lo 	iplen = *offp;
4628f5a8818SKevin Lo 	*mp = NULL;
4638f5a8818SKevin Lo 
4646bf65bcfSRobert Watson 	CARPSTATS_INC(carps_ipackets);
465a9771948SGleb Smirnoff 
466c5c392e7SMikolaj Golub 	if (!V_carp_allow) {
467a9771948SGleb Smirnoff 		m_freem(m);
4688f5a8818SKevin Lo 		return (IPPROTO_DONE);
469a9771948SGleb Smirnoff 	}
470a9771948SGleb Smirnoff 
471a9771948SGleb Smirnoff 	/* verify that the IP TTL is 255.  */
472a9771948SGleb Smirnoff 	if (ip->ip_ttl != CARP_DFLTTL) {
4736bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badttl);
47408b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
4751e9e6572SGleb Smirnoff 		    ip->ip_ttl,
4761e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
477a9771948SGleb Smirnoff 		m_freem(m);
4788f5a8818SKevin Lo 		return (IPPROTO_DONE);
479a9771948SGleb Smirnoff 	}
480a9771948SGleb Smirnoff 
481a9771948SGleb Smirnoff 	iplen = ip->ip_hl << 2;
482a9771948SGleb Smirnoff 
483a9771948SGleb Smirnoff 	if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
4846bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badlen);
48508b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: received len %zd < sizeof(struct carp_header) "
48608b68b0eSGleb Smirnoff 		    "on %s\n", __func__, m->m_len - sizeof(struct ip),
48716e324fcSXin LI 		    m->m_pkthdr.rcvif->if_xname);
488a9771948SGleb Smirnoff 		m_freem(m);
4898f5a8818SKevin Lo 		return (IPPROTO_DONE);
490a9771948SGleb Smirnoff 	}
491a9771948SGleb Smirnoff 
492a9771948SGleb Smirnoff 	if (iplen + sizeof(*ch) < m->m_len) {
493a9771948SGleb Smirnoff 		if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
4946bf65bcfSRobert Watson 			CARPSTATS_INC(carps_hdrops);
49508b68b0eSGleb Smirnoff 			CARP_DEBUG("%s: pullup failed\n", __func__);
4968f5a8818SKevin Lo 			return (IPPROTO_DONE);
497a9771948SGleb Smirnoff 		}
498a9771948SGleb Smirnoff 		ip = mtod(m, struct ip *);
499a9771948SGleb Smirnoff 	}
500a9771948SGleb Smirnoff 	ch = (struct carp_header *)((char *)ip + iplen);
501a9771948SGleb Smirnoff 
502a9771948SGleb Smirnoff 	/*
503a9771948SGleb Smirnoff 	 * verify that the received packet length is
504a9771948SGleb Smirnoff 	 * equal to the CARP header
505a9771948SGleb Smirnoff 	 */
506a9771948SGleb Smirnoff 	len = iplen + sizeof(*ch);
507a9771948SGleb Smirnoff 	if (len > m->m_pkthdr.len) {
5086bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badlen);
50908b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: packet too short %d on %s\n", __func__,
5101e9e6572SGleb Smirnoff 		    m->m_pkthdr.len,
5111e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
512a9771948SGleb Smirnoff 		m_freem(m);
5138f5a8818SKevin Lo 		return (IPPROTO_DONE);
514a9771948SGleb Smirnoff 	}
515a9771948SGleb Smirnoff 
516a9771948SGleb Smirnoff 	if ((m = m_pullup(m, len)) == NULL) {
5176bf65bcfSRobert Watson 		CARPSTATS_INC(carps_hdrops);
5188f5a8818SKevin Lo 		return (IPPROTO_DONE);
519a9771948SGleb Smirnoff 	}
520a9771948SGleb Smirnoff 	ip = mtod(m, struct ip *);
521a9771948SGleb Smirnoff 	ch = (struct carp_header *)((char *)ip + iplen);
522a9771948SGleb Smirnoff 
523a9771948SGleb Smirnoff 	/* verify the CARP checksum */
524a9771948SGleb Smirnoff 	m->m_data += iplen;
525c4d06976SGleb Smirnoff 	if (in_cksum(m, len - iplen)) {
5266bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badsum);
52708b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: checksum failed on %s\n", __func__,
5281e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
529a9771948SGleb Smirnoff 		m_freem(m);
5308f5a8818SKevin Lo 		return (IPPROTO_DONE);
531a9771948SGleb Smirnoff 	}
532a9771948SGleb Smirnoff 	m->m_data -= iplen;
533a9771948SGleb Smirnoff 
5341e9e6572SGleb Smirnoff 	carp_input_c(m, ch, AF_INET);
5358f5a8818SKevin Lo 	return (IPPROTO_DONE);
536a9771948SGleb Smirnoff }
537a0ae8f04SBjoern A. Zeeb #endif
538a9771948SGleb Smirnoff 
539a9771948SGleb Smirnoff #ifdef INET6
540a9771948SGleb Smirnoff int
541a9771948SGleb Smirnoff carp6_input(struct mbuf **mp, int *offp, int proto)
542a9771948SGleb Smirnoff {
543a9771948SGleb Smirnoff 	struct mbuf *m = *mp;
544a9771948SGleb Smirnoff 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
545a9771948SGleb Smirnoff 	struct carp_header *ch;
546a9771948SGleb Smirnoff 	u_int len;
547a9771948SGleb Smirnoff 
5486bf65bcfSRobert Watson 	CARPSTATS_INC(carps_ipackets6);
549a9771948SGleb Smirnoff 
550c5c392e7SMikolaj Golub 	if (!V_carp_allow) {
551a9771948SGleb Smirnoff 		m_freem(m);
552a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
553a9771948SGleb Smirnoff 	}
554a9771948SGleb Smirnoff 
555a9771948SGleb Smirnoff 	/* check if received on a valid carp interface */
556a9771948SGleb Smirnoff 	if (m->m_pkthdr.rcvif->if_carp == NULL) {
5576bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badif);
55808b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: packet received on non-carp interface: %s\n",
55908b68b0eSGleb Smirnoff 		    __func__, m->m_pkthdr.rcvif->if_xname);
560a9771948SGleb Smirnoff 		m_freem(m);
561a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
562a9771948SGleb Smirnoff 	}
563a9771948SGleb Smirnoff 
564a9771948SGleb Smirnoff 	/* verify that the IP TTL is 255 */
565a9771948SGleb Smirnoff 	if (ip6->ip6_hlim != CARP_DFLTTL) {
5666bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badttl);
56708b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
56808b68b0eSGleb Smirnoff 		    ip6->ip6_hlim, m->m_pkthdr.rcvif->if_xname);
569a9771948SGleb Smirnoff 		m_freem(m);
570a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
571a9771948SGleb Smirnoff 	}
572a9771948SGleb Smirnoff 
573a9771948SGleb Smirnoff 	/* verify that we have a complete carp packet */
574a4adf6ccSBjoern A. Zeeb 	if (m->m_len < *offp + sizeof(*ch)) {
575a9771948SGleb Smirnoff 		len = m->m_len;
57663abacc2SBjoern A. Zeeb 		m = m_pullup(m, *offp + sizeof(*ch));
57763abacc2SBjoern A. Zeeb 		if (m == NULL) {
5786bf65bcfSRobert Watson 			CARPSTATS_INC(carps_badlen);
57908b68b0eSGleb Smirnoff 			CARP_DEBUG("%s: packet size %u too small\n", __func__, len);
580a9771948SGleb Smirnoff 			return (IPPROTO_DONE);
581a9771948SGleb Smirnoff 		}
582a4adf6ccSBjoern A. Zeeb 	}
583dad68fc3SBjoern A. Zeeb 	ch = (struct carp_header *)(mtod(m, char *) + *offp);
584a9771948SGleb Smirnoff 
585a9771948SGleb Smirnoff 	/* verify the CARP checksum */
586a9771948SGleb Smirnoff 	m->m_data += *offp;
587c4d06976SGleb Smirnoff 	if (in_cksum(m, sizeof(*ch))) {
5886bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badsum);
58908b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: checksum failed, on %s\n", __func__,
5901e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
591a9771948SGleb Smirnoff 		m_freem(m);
592a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
593a9771948SGleb Smirnoff 	}
594a9771948SGleb Smirnoff 	m->m_data -= *offp;
595a9771948SGleb Smirnoff 
5961e9e6572SGleb Smirnoff 	carp_input_c(m, ch, AF_INET6);
597a9771948SGleb Smirnoff 	return (IPPROTO_DONE);
598a9771948SGleb Smirnoff }
599a9771948SGleb Smirnoff #endif /* INET6 */
600a9771948SGleb Smirnoff 
6018151740cSJosh Paetzel /*
6028151740cSJosh Paetzel  * This routine should not be necessary at all, but some switches
6038151740cSJosh Paetzel  * (VMWare ESX vswitches) can echo our own packets back at us,
6048151740cSJosh Paetzel  * and we must ignore them or they will cause us to drop out of
6058151740cSJosh Paetzel  * MASTER mode.
6068151740cSJosh Paetzel  *
6078151740cSJosh Paetzel  * We cannot catch all cases of network loops.  Instead, what we
6088151740cSJosh Paetzel  * do here is catch any packet that arrives with a carp header
6098151740cSJosh Paetzel  * with a VHID of 0, that comes from an address that is our own.
6108151740cSJosh Paetzel  * These packets are by definition "from us" (even if they are from
6118151740cSJosh Paetzel  * a misconfigured host that is pretending to be us).
6128151740cSJosh Paetzel  *
6138151740cSJosh Paetzel  * The VHID test is outside this mini-function.
6148151740cSJosh Paetzel  */
6158151740cSJosh Paetzel static int
6168151740cSJosh Paetzel carp_source_is_self(struct mbuf *m, struct ifaddr *ifa, sa_family_t af)
6178151740cSJosh Paetzel {
618cfff8d3dSEnji Cooper #ifdef INET
6198151740cSJosh Paetzel 	struct ip *ip4;
6208151740cSJosh Paetzel 	struct in_addr in4;
621cfff8d3dSEnji Cooper #endif
622cfff8d3dSEnji Cooper #ifdef INET6
6238151740cSJosh Paetzel 	struct ip6_hdr *ip6;
6248151740cSJosh Paetzel 	struct in6_addr in6;
625cfff8d3dSEnji Cooper #endif
6268151740cSJosh Paetzel 
6278151740cSJosh Paetzel 	switch (af) {
628cfff8d3dSEnji Cooper #ifdef INET
6298151740cSJosh Paetzel 	case AF_INET:
6308151740cSJosh Paetzel 		ip4 = mtod(m, struct ip *);
6318151740cSJosh Paetzel 		in4 = ifatoia(ifa)->ia_addr.sin_addr;
6328151740cSJosh Paetzel 		return (in4.s_addr == ip4->ip_src.s_addr);
633cfff8d3dSEnji Cooper #endif
634cfff8d3dSEnji Cooper #ifdef INET6
6358151740cSJosh Paetzel 	case AF_INET6:
6368151740cSJosh Paetzel 		ip6 = mtod(m, struct ip6_hdr *);
6378151740cSJosh Paetzel 		in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
6388151740cSJosh Paetzel 		return (memcmp(&in6, &ip6->ip6_src, sizeof(in6)) == 0);
639cfff8d3dSEnji Cooper #endif
640cfff8d3dSEnji Cooper 	default:
6418151740cSJosh Paetzel 		break;
6428151740cSJosh Paetzel 	}
6438151740cSJosh Paetzel 	return (0);
6448151740cSJosh Paetzel }
6458151740cSJosh Paetzel 
6465c1f0f6dSGleb Smirnoff static void
6471e9e6572SGleb Smirnoff carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
648a9771948SGleb Smirnoff {
649a9771948SGleb Smirnoff 	struct ifnet *ifp = m->m_pkthdr.rcvif;
6508151740cSJosh Paetzel 	struct ifaddr *ifa, *match;
6511e9e6572SGleb Smirnoff 	struct carp_softc *sc;
65208b68b0eSGleb Smirnoff 	uint64_t tmp_counter;
653a9771948SGleb Smirnoff 	struct timeval sc_tv, ch_tv;
6548151740cSJosh Paetzel 	int error;
655a9771948SGleb Smirnoff 
656b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
657b8a6e03fSGleb Smirnoff 
6588151740cSJosh Paetzel 	/*
6598151740cSJosh Paetzel 	 * Verify that the VHID is valid on the receiving interface.
6608151740cSJosh Paetzel 	 *
6618151740cSJosh Paetzel 	 * There should be just one match.  If there are none
6628151740cSJosh Paetzel 	 * the VHID is not valid and we drop the packet.  If
6638151740cSJosh Paetzel 	 * there are multiple VHID matches, take just the first
6648151740cSJosh Paetzel 	 * one, for compatibility with previous code.  While we're
6658151740cSJosh Paetzel 	 * scanning, check for obvious loops in the network topology
6668151740cSJosh Paetzel 	 * (these should never happen, and as noted above, we may
6678151740cSJosh Paetzel 	 * miss real loops; this is just a double-check).
6688151740cSJosh Paetzel 	 */
6698151740cSJosh Paetzel 	error = 0;
6708151740cSJosh Paetzel 	match = NULL;
6718151740cSJosh Paetzel 	IFNET_FOREACH_IFA(ifp, ifa) {
6728151740cSJosh Paetzel 		if (match == NULL && ifa->ifa_carp != NULL &&
6738151740cSJosh Paetzel 		    ifa->ifa_addr->sa_family == af &&
6748151740cSJosh Paetzel 		    ifa->ifa_carp->sc_vhid == ch->carp_vhid)
6758151740cSJosh Paetzel 			match = ifa;
6768151740cSJosh Paetzel 		if (ch->carp_vhid == 0 && carp_source_is_self(m, ifa, af))
6778151740cSJosh Paetzel 			error = ELOOP;
67808b68b0eSGleb Smirnoff 	}
6798151740cSJosh Paetzel 	ifa = error ? NULL : match;
6808151740cSJosh Paetzel 	if (ifa != NULL)
6818151740cSJosh Paetzel 		ifa_ref(ifa);
682d220759bSGleb Smirnoff 
68308b68b0eSGleb Smirnoff 	if (ifa == NULL) {
6848151740cSJosh Paetzel 		if (error == ELOOP) {
6858151740cSJosh Paetzel 			CARP_DEBUG("dropping looped packet on interface %s\n",
6868151740cSJosh Paetzel 			    ifp->if_xname);
6878151740cSJosh Paetzel 			CARPSTATS_INC(carps_badif);	/* ??? */
6888151740cSJosh Paetzel 		} else {
6896bf65bcfSRobert Watson 			CARPSTATS_INC(carps_badvhid);
6908151740cSJosh Paetzel 		}
691a9771948SGleb Smirnoff 		m_freem(m);
692a9771948SGleb Smirnoff 		return;
693a9771948SGleb Smirnoff 	}
694a9771948SGleb Smirnoff 
695a9771948SGleb Smirnoff 	/* verify the CARP version. */
696a9771948SGleb Smirnoff 	if (ch->carp_version != CARP_VERSION) {
6976bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badver);
69808b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: invalid version %d\n", ifp->if_xname,
6991e9e6572SGleb Smirnoff 		    ch->carp_version);
70008b68b0eSGleb Smirnoff 		ifa_free(ifa);
701a9771948SGleb Smirnoff 		m_freem(m);
702a9771948SGleb Smirnoff 		return;
703a9771948SGleb Smirnoff 	}
704a9771948SGleb Smirnoff 
70508b68b0eSGleb Smirnoff 	sc = ifa->ifa_carp;
70608b68b0eSGleb Smirnoff 	CARP_LOCK(sc);
70708b68b0eSGleb Smirnoff 	ifa_free(ifa);
70808b68b0eSGleb Smirnoff 
709a9771948SGleb Smirnoff 	if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
7106bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badauth);
71108b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: incorrect hash for VHID %u@%s\n", __func__,
71208b68b0eSGleb Smirnoff 		    sc->sc_vhid, ifp->if_xname);
71308b68b0eSGleb Smirnoff 		goto out;
714a9771948SGleb Smirnoff 	}
715a9771948SGleb Smirnoff 
716a9771948SGleb Smirnoff 	tmp_counter = ntohl(ch->carp_counter[0]);
717a9771948SGleb Smirnoff 	tmp_counter = tmp_counter<<32;
718a9771948SGleb Smirnoff 	tmp_counter += ntohl(ch->carp_counter[1]);
719a9771948SGleb Smirnoff 
720a9771948SGleb Smirnoff 	/* XXX Replay protection goes here */
721a9771948SGleb Smirnoff 
722a9771948SGleb Smirnoff 	sc->sc_init_counter = 0;
723a9771948SGleb Smirnoff 	sc->sc_counter = tmp_counter;
724a9771948SGleb Smirnoff 
725a9771948SGleb Smirnoff 	sc_tv.tv_sec = sc->sc_advbase;
726f08535f8SGleb Smirnoff 	sc_tv.tv_usec = DEMOTE_ADVSKEW(sc) * 1000000 / 256;
727a9771948SGleb Smirnoff 	ch_tv.tv_sec = ch->carp_advbase;
728a9771948SGleb Smirnoff 	ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
729a9771948SGleb Smirnoff 
730a9771948SGleb Smirnoff 	switch (sc->sc_state) {
731a9771948SGleb Smirnoff 	case INIT:
732a9771948SGleb Smirnoff 		break;
733a9771948SGleb Smirnoff 	case MASTER:
734a9771948SGleb Smirnoff 		/*
735a9771948SGleb Smirnoff 		 * If we receive an advertisement from a master who's going to
736a9771948SGleb Smirnoff 		 * be more frequent than us, go into BACKUP state.
737a9771948SGleb Smirnoff 		 */
738a9771948SGleb Smirnoff 		if (timevalcmp(&sc_tv, &ch_tv, >) ||
739a9771948SGleb Smirnoff 		    timevalcmp(&sc_tv, &ch_tv, ==)) {
740a9771948SGleb Smirnoff 			callout_stop(&sc->sc_ad_tmo);
741d01641e2SWill Andrews 			carp_set_state(sc, BACKUP,
742d01641e2SWill Andrews 			    "more frequent advertisement received");
743a9771948SGleb Smirnoff 			carp_setrun(sc, 0);
74408b68b0eSGleb Smirnoff 			carp_delroute(sc);
745a9771948SGleb Smirnoff 		}
746a9771948SGleb Smirnoff 		break;
747a9771948SGleb Smirnoff 	case BACKUP:
748a9771948SGleb Smirnoff 		/*
749a9771948SGleb Smirnoff 		 * If we're pre-empting masters who advertise slower than us,
750a9771948SGleb Smirnoff 		 * and this one claims to be slower, treat him as down.
751a9771948SGleb Smirnoff 		 */
752c5c392e7SMikolaj Golub 		if (V_carp_preempt && timevalcmp(&sc_tv, &ch_tv, <)) {
753d01641e2SWill Andrews 			carp_master_down_locked(sc,
754d01641e2SWill Andrews 			    "preempting a slower master");
755a9771948SGleb Smirnoff 			break;
756a9771948SGleb Smirnoff 		}
757a9771948SGleb Smirnoff 
758a9771948SGleb Smirnoff 		/*
759a9771948SGleb Smirnoff 		 *  If the master is going to advertise at such a low frequency
760a9771948SGleb Smirnoff 		 *  that he's guaranteed to time out, we'd might as well just
761a9771948SGleb Smirnoff 		 *  treat him as timed out now.
762a9771948SGleb Smirnoff 		 */
763a9771948SGleb Smirnoff 		sc_tv.tv_sec = sc->sc_advbase * 3;
764a9771948SGleb Smirnoff 		if (timevalcmp(&sc_tv, &ch_tv, <)) {
765d01641e2SWill Andrews 			carp_master_down_locked(sc, "master will time out");
766a9771948SGleb Smirnoff 			break;
767a9771948SGleb Smirnoff 		}
768a9771948SGleb Smirnoff 
769a9771948SGleb Smirnoff 		/*
770a9771948SGleb Smirnoff 		 * Otherwise, we reset the counter and wait for the next
771a9771948SGleb Smirnoff 		 * advertisement.
772a9771948SGleb Smirnoff 		 */
773a9771948SGleb Smirnoff 		carp_setrun(sc, af);
774a9771948SGleb Smirnoff 		break;
775a9771948SGleb Smirnoff 	}
776a9771948SGleb Smirnoff 
77708b68b0eSGleb Smirnoff out:
77808b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
779a9771948SGleb Smirnoff 	m_freem(m);
780a9771948SGleb Smirnoff }
781a9771948SGleb Smirnoff 
7825c1f0f6dSGleb Smirnoff static int
783a9771948SGleb Smirnoff carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
784a9771948SGleb Smirnoff {
785a9771948SGleb Smirnoff 	struct m_tag *mtag;
786a9771948SGleb Smirnoff 
787a9771948SGleb Smirnoff 	if (sc->sc_init_counter) {
788a9771948SGleb Smirnoff 		/* this could also be seconds since unix epoch */
789a9771948SGleb Smirnoff 		sc->sc_counter = arc4random();
790a9771948SGleb Smirnoff 		sc->sc_counter = sc->sc_counter << 32;
791a9771948SGleb Smirnoff 		sc->sc_counter += arc4random();
792a9771948SGleb Smirnoff 	} else
793a9771948SGleb Smirnoff 		sc->sc_counter++;
794a9771948SGleb Smirnoff 
795a9771948SGleb Smirnoff 	ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
796a9771948SGleb Smirnoff 	ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
797a9771948SGleb Smirnoff 
798a9771948SGleb Smirnoff 	carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
799a9771948SGleb Smirnoff 
800a9771948SGleb Smirnoff 	/* Tag packet for carp_output */
80108b68b0eSGleb Smirnoff 	if ((mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct carp_softc *),
80208b68b0eSGleb Smirnoff 	    M_NOWAIT)) == NULL) {
803a9771948SGleb Smirnoff 		m_freem(m);
80408b68b0eSGleb Smirnoff 		CARPSTATS_INC(carps_onomem);
805a9771948SGleb Smirnoff 		return (ENOMEM);
806a9771948SGleb Smirnoff 	}
807eaf151c4SGleb Smirnoff 	bcopy(&sc, mtag + 1, sizeof(sc));
808a9771948SGleb Smirnoff 	m_tag_prepend(m, mtag);
809a9771948SGleb Smirnoff 
810a9771948SGleb Smirnoff 	return (0);
811a9771948SGleb Smirnoff }
812a9771948SGleb Smirnoff 
813f08535f8SGleb Smirnoff /*
814f08535f8SGleb Smirnoff  * To avoid LORs and possible recursions this function shouldn't
815f08535f8SGleb Smirnoff  * be called directly, but scheduled via taskqueue.
816f08535f8SGleb Smirnoff  */
8175c1f0f6dSGleb Smirnoff static void
818f08535f8SGleb Smirnoff carp_send_ad_all(void *ctx __unused, int pending __unused)
819a9771948SGleb Smirnoff {
820d92d54d5SGleb Smirnoff 	struct carp_softc *sc;
8211d126e9bSKristof Provost 	struct epoch_tracker et;
822a9771948SGleb Smirnoff 
8231d126e9bSKristof Provost 	NET_EPOCH_ENTER(et);
824d92d54d5SGleb Smirnoff 	mtx_lock(&carp_mtx);
82508b68b0eSGleb Smirnoff 	LIST_FOREACH(sc, &carp_list, sc_next)
826f08535f8SGleb Smirnoff 		if (sc->sc_state == MASTER) {
82708b68b0eSGleb Smirnoff 			CARP_LOCK(sc);
828afdbac98SGleb Smirnoff 			CURVNET_SET(sc->sc_carpdev->if_vnet);
829d220759bSGleb Smirnoff 			carp_send_ad_locked(sc);
830afdbac98SGleb Smirnoff 			CURVNET_RESTORE();
83108b68b0eSGleb Smirnoff 			CARP_UNLOCK(sc);
832a9771948SGleb Smirnoff 		}
833d92d54d5SGleb Smirnoff 	mtx_unlock(&carp_mtx);
8341d126e9bSKristof Provost 	NET_EPOCH_EXIT(et);
835a9771948SGleb Smirnoff }
836a9771948SGleb Smirnoff 
837afdbac98SGleb Smirnoff /* Send a periodic advertisement, executed in callout context. */
8385c1f0f6dSGleb Smirnoff static void
839a9771948SGleb Smirnoff carp_send_ad(void *v)
840a9771948SGleb Smirnoff {
841d220759bSGleb Smirnoff 	struct carp_softc *sc = v;
8421d126e9bSKristof Provost 	struct epoch_tracker et;
843d220759bSGleb Smirnoff 
8441d126e9bSKristof Provost 	NET_EPOCH_ENTER(et);
84508b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
846afdbac98SGleb Smirnoff 	CURVNET_SET(sc->sc_carpdev->if_vnet);
847d220759bSGleb Smirnoff 	carp_send_ad_locked(sc);
848afdbac98SGleb Smirnoff 	CURVNET_RESTORE();
84908b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
8501d126e9bSKristof Provost 	NET_EPOCH_EXIT(et);
851d220759bSGleb Smirnoff }
852d220759bSGleb Smirnoff 
853d220759bSGleb Smirnoff static void
8544a2dd8d4SGleb Smirnoff carp_send_ad_error(struct carp_softc *sc, int error)
8554a2dd8d4SGleb Smirnoff {
8564a2dd8d4SGleb Smirnoff 
8574a2dd8d4SGleb Smirnoff 	if (error) {
8584a2dd8d4SGleb Smirnoff 		if (sc->sc_sendad_errors < INT_MAX)
8594a2dd8d4SGleb Smirnoff 			sc->sc_sendad_errors++;
8604a2dd8d4SGleb Smirnoff 		if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
8614a2dd8d4SGleb Smirnoff 			static const char fmt[] = "send error %d on %s";
8624a2dd8d4SGleb Smirnoff 			char msg[sizeof(fmt) + IFNAMSIZ];
8634a2dd8d4SGleb Smirnoff 
8644a2dd8d4SGleb Smirnoff 			sprintf(msg, fmt, error, sc->sc_carpdev->if_xname);
8654a2dd8d4SGleb Smirnoff 			carp_demote_adj(V_carp_senderr_adj, msg);
8664a2dd8d4SGleb Smirnoff 		}
8674a2dd8d4SGleb Smirnoff 		sc->sc_sendad_success = 0;
8684a2dd8d4SGleb Smirnoff 	} else {
8694a2dd8d4SGleb Smirnoff 		if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS &&
8704a2dd8d4SGleb Smirnoff 		    ++sc->sc_sendad_success >= CARP_SENDAD_MIN_SUCCESS) {
8714a2dd8d4SGleb Smirnoff 			static const char fmt[] = "send ok on %s";
8724a2dd8d4SGleb Smirnoff 			char msg[sizeof(fmt) + IFNAMSIZ];
8734a2dd8d4SGleb Smirnoff 
8744a2dd8d4SGleb Smirnoff 			sprintf(msg, fmt, sc->sc_carpdev->if_xname);
8754a2dd8d4SGleb Smirnoff 			carp_demote_adj(-V_carp_senderr_adj, msg);
8764a2dd8d4SGleb Smirnoff 			sc->sc_sendad_errors = 0;
8774a2dd8d4SGleb Smirnoff 		} else
8784a2dd8d4SGleb Smirnoff 			sc->sc_sendad_errors = 0;
8794a2dd8d4SGleb Smirnoff 	}
8804a2dd8d4SGleb Smirnoff }
8814a2dd8d4SGleb Smirnoff 
8828151740cSJosh Paetzel /*
8838151740cSJosh Paetzel  * Pick the best ifaddr on the given ifp for sending CARP
8848151740cSJosh Paetzel  * advertisements.
8858151740cSJosh Paetzel  *
8868151740cSJosh Paetzel  * "Best" here is defined by ifa_preferred().  This function is much
8878151740cSJosh Paetzel  * much like ifaof_ifpforaddr() except that we just use ifa_preferred().
8888151740cSJosh Paetzel  *
8898151740cSJosh Paetzel  * (This could be simplified to return the actual address, except that
8908151740cSJosh Paetzel  * it has a different format in AF_INET and AF_INET6.)
8918151740cSJosh Paetzel  */
8928151740cSJosh Paetzel static struct ifaddr *
8938151740cSJosh Paetzel carp_best_ifa(int af, struct ifnet *ifp)
8948151740cSJosh Paetzel {
8958151740cSJosh Paetzel 	struct ifaddr *ifa, *best;
8968151740cSJosh Paetzel 
897b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
898b8a6e03fSGleb Smirnoff 
8998151740cSJosh Paetzel 	if (af >= AF_MAX)
9008151740cSJosh Paetzel 		return (NULL);
9018151740cSJosh Paetzel 	best = NULL;
902d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
9038151740cSJosh Paetzel 		if (ifa->ifa_addr->sa_family == af &&
9048151740cSJosh Paetzel 		    (best == NULL || ifa_preferred(best, ifa)))
9058151740cSJosh Paetzel 			best = ifa;
9068151740cSJosh Paetzel 	}
9078151740cSJosh Paetzel 	if (best != NULL)
9088151740cSJosh Paetzel 		ifa_ref(best);
9098151740cSJosh Paetzel 	return (best);
9108151740cSJosh Paetzel }
9118151740cSJosh Paetzel 
9124a2dd8d4SGleb Smirnoff static void
913d220759bSGleb Smirnoff carp_send_ad_locked(struct carp_softc *sc)
914d220759bSGleb Smirnoff {
915a9771948SGleb Smirnoff 	struct carp_header ch;
916a9771948SGleb Smirnoff 	struct timeval tv;
91708b68b0eSGleb Smirnoff 	struct ifaddr *ifa;
918a9771948SGleb Smirnoff 	struct carp_header *ch_ptr;
919a9771948SGleb Smirnoff 	struct mbuf *m;
92008b68b0eSGleb Smirnoff 	int len, advskew;
921a9771948SGleb Smirnoff 
9221d126e9bSKristof Provost 	NET_EPOCH_ASSERT();
92308b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
924d220759bSGleb Smirnoff 
925f08535f8SGleb Smirnoff 	advskew = DEMOTE_ADVSKEW(sc);
92608b68b0eSGleb Smirnoff 	tv.tv_sec = sc->sc_advbase;
927a9771948SGleb Smirnoff 	tv.tv_usec = advskew * 1000000 / 256;
928a9771948SGleb Smirnoff 
929a9771948SGleb Smirnoff 	ch.carp_version = CARP_VERSION;
930a9771948SGleb Smirnoff 	ch.carp_type = CARP_ADVERTISEMENT;
931a9771948SGleb Smirnoff 	ch.carp_vhid = sc->sc_vhid;
93208b68b0eSGleb Smirnoff 	ch.carp_advbase = sc->sc_advbase;
933a9771948SGleb Smirnoff 	ch.carp_advskew = advskew;
934a9771948SGleb Smirnoff 	ch.carp_authlen = 7;	/* XXX DEFINE */
935a9771948SGleb Smirnoff 	ch.carp_pad1 = 0;	/* must be zero */
936a9771948SGleb Smirnoff 	ch.carp_cksum = 0;
937a9771948SGleb Smirnoff 
93808b68b0eSGleb Smirnoff 	/* XXXGL: OpenBSD picks first ifaddr with needed family. */
93908b68b0eSGleb Smirnoff 
940a9771948SGleb Smirnoff #ifdef INET
94108b68b0eSGleb Smirnoff 	if (sc->sc_naddrs) {
942a9771948SGleb Smirnoff 		struct ip *ip;
943a9771948SGleb Smirnoff 
944dc4ad05eSGleb Smirnoff 		m = m_gethdr(M_NOWAIT, MT_DATA);
945a9771948SGleb Smirnoff 		if (m == NULL) {
9466bf65bcfSRobert Watson 			CARPSTATS_INC(carps_onomem);
947891122d1SGleb Smirnoff 			goto resched;
948a9771948SGleb Smirnoff 		}
949a9771948SGleb Smirnoff 		len = sizeof(*ip) + sizeof(ch);
950a9771948SGleb Smirnoff 		m->m_pkthdr.len = len;
951a9771948SGleb Smirnoff 		m->m_pkthdr.rcvif = NULL;
952a9771948SGleb Smirnoff 		m->m_len = len;
953ed6a66caSRobert Watson 		M_ALIGN(m, m->m_len);
954a9771948SGleb Smirnoff 		m->m_flags |= M_MCAST;
955a9771948SGleb Smirnoff 		ip = mtod(m, struct ip *);
956a9771948SGleb Smirnoff 		ip->ip_v = IPVERSION;
957a9771948SGleb Smirnoff 		ip->ip_hl = sizeof(*ip) >> 2;
9580d3d234cSKristof Provost 		ip->ip_tos = V_carp_dscp << IPTOS_DSCP_OFFSET;
9598f134647SGleb Smirnoff 		ip->ip_len = htons(len);
9608f134647SGleb Smirnoff 		ip->ip_off = htons(IP_DF);
961a9771948SGleb Smirnoff 		ip->ip_ttl = CARP_DFLTTL;
962a9771948SGleb Smirnoff 		ip->ip_p = IPPROTO_CARP;
963a9771948SGleb Smirnoff 		ip->ip_sum = 0;
9646d947416SGleb Smirnoff 		ip_fillid(ip);
96508b68b0eSGleb Smirnoff 
9668151740cSJosh Paetzel 		ifa = carp_best_ifa(AF_INET, sc->sc_carpdev);
96708b68b0eSGleb Smirnoff 		if (ifa != NULL) {
96808b68b0eSGleb Smirnoff 			ip->ip_src.s_addr =
96908b68b0eSGleb Smirnoff 			    ifatoia(ifa)->ia_addr.sin_addr.s_addr;
97008b68b0eSGleb Smirnoff 			ifa_free(ifa);
97108b68b0eSGleb Smirnoff 		} else
97208b68b0eSGleb Smirnoff 			ip->ip_src.s_addr = 0;
973a9771948SGleb Smirnoff 		ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
974a9771948SGleb Smirnoff 
975a9771948SGleb Smirnoff 		ch_ptr = (struct carp_header *)(&ip[1]);
976a9771948SGleb Smirnoff 		bcopy(&ch, ch_ptr, sizeof(ch));
977a9771948SGleb Smirnoff 		if (carp_prepare_ad(m, sc, ch_ptr))
978891122d1SGleb Smirnoff 			goto resched;
979a9771948SGleb Smirnoff 
980a9771948SGleb Smirnoff 		m->m_data += sizeof(*ip);
981c4d06976SGleb Smirnoff 		ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip));
982a9771948SGleb Smirnoff 		m->m_data -= sizeof(*ip);
983a9771948SGleb Smirnoff 
9846bf65bcfSRobert Watson 		CARPSTATS_INC(carps_opackets);
985a9771948SGleb Smirnoff 
9864a2dd8d4SGleb Smirnoff 		carp_send_ad_error(sc, ip_output(m, NULL, NULL, IP_RAWOUTPUT,
9874a2dd8d4SGleb Smirnoff 		    &sc->sc_carpdev->if_carp->cif_imo, NULL));
988a9771948SGleb Smirnoff 	}
989a9771948SGleb Smirnoff #endif /* INET */
990a9771948SGleb Smirnoff #ifdef INET6
99108b68b0eSGleb Smirnoff 	if (sc->sc_naddrs6) {
992a9771948SGleb Smirnoff 		struct ip6_hdr *ip6;
993a9771948SGleb Smirnoff 
994dc4ad05eSGleb Smirnoff 		m = m_gethdr(M_NOWAIT, MT_DATA);
995a9771948SGleb Smirnoff 		if (m == NULL) {
9966bf65bcfSRobert Watson 			CARPSTATS_INC(carps_onomem);
997891122d1SGleb Smirnoff 			goto resched;
998a9771948SGleb Smirnoff 		}
999a9771948SGleb Smirnoff 		len = sizeof(*ip6) + sizeof(ch);
1000a9771948SGleb Smirnoff 		m->m_pkthdr.len = len;
1001a9771948SGleb Smirnoff 		m->m_pkthdr.rcvif = NULL;
1002a9771948SGleb Smirnoff 		m->m_len = len;
1003ed6a66caSRobert Watson 		M_ALIGN(m, m->m_len);
1004a9771948SGleb Smirnoff 		m->m_flags |= M_MCAST;
1005a9771948SGleb Smirnoff 		ip6 = mtod(m, struct ip6_hdr *);
1006a9771948SGleb Smirnoff 		bzero(ip6, sizeof(*ip6));
1007a9771948SGleb Smirnoff 		ip6->ip6_vfc |= IPV6_VERSION;
10080d3d234cSKristof Provost 		/* Traffic class isn't defined in ip6 struct instead
10090d3d234cSKristof Provost 		 * it gets offset into flowid field */
10100d3d234cSKristof Provost 		ip6->ip6_flow |= htonl(V_carp_dscp << (IPV6_FLOWLABEL_LEN +
10110d3d234cSKristof Provost 		    IPTOS_DSCP_OFFSET));
1012a9771948SGleb Smirnoff 		ip6->ip6_hlim = CARP_DFLTTL;
1013a9771948SGleb Smirnoff 		ip6->ip6_nxt = IPPROTO_CARP;
1014a9771948SGleb Smirnoff 
101508b68b0eSGleb Smirnoff 		/* set the source address */
10168151740cSJosh Paetzel 		ifa = carp_best_ifa(AF_INET6, sc->sc_carpdev);
101708b68b0eSGleb Smirnoff 		if (ifa != NULL) {
101808b68b0eSGleb Smirnoff 			bcopy(IFA_IN6(ifa), &ip6->ip6_src,
101908b68b0eSGleb Smirnoff 			    sizeof(struct in6_addr));
102008b68b0eSGleb Smirnoff 			ifa_free(ifa);
102108b68b0eSGleb Smirnoff 		} else
102208b68b0eSGleb Smirnoff 			/* This should never happen with IPv6. */
102308b68b0eSGleb Smirnoff 			bzero(&ip6->ip6_src, sizeof(struct in6_addr));
102408b68b0eSGleb Smirnoff 
102508b68b0eSGleb Smirnoff 		/* Set the multicast destination. */
10267002145dSBjoern A. Zeeb 		ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
1027a9771948SGleb Smirnoff 		ip6->ip6_dst.s6_addr8[15] = 0x12;
10287002145dSBjoern A. Zeeb 		if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
10297002145dSBjoern A. Zeeb 			m_freem(m);
1030e81ab876SGleb Smirnoff 			CARP_DEBUG("%s: in6_setscope failed\n", __func__);
1031891122d1SGleb Smirnoff 			goto resched;
10327002145dSBjoern A. Zeeb 		}
1033a9771948SGleb Smirnoff 
1034a9771948SGleb Smirnoff 		ch_ptr = (struct carp_header *)(&ip6[1]);
1035a9771948SGleb Smirnoff 		bcopy(&ch, ch_ptr, sizeof(ch));
1036a9771948SGleb Smirnoff 		if (carp_prepare_ad(m, sc, ch_ptr))
1037891122d1SGleb Smirnoff 			goto resched;
1038a9771948SGleb Smirnoff 
1039a9771948SGleb Smirnoff 		m->m_data += sizeof(*ip6);
1040c4d06976SGleb Smirnoff 		ch_ptr->carp_cksum = in_cksum(m, len - sizeof(*ip6));
1041a9771948SGleb Smirnoff 		m->m_data -= sizeof(*ip6);
1042a9771948SGleb Smirnoff 
10436bf65bcfSRobert Watson 		CARPSTATS_INC(carps_opackets6);
1044a9771948SGleb Smirnoff 
10454a2dd8d4SGleb Smirnoff 		carp_send_ad_error(sc, ip6_output(m, NULL, NULL, 0,
10464a2dd8d4SGleb Smirnoff 		    &sc->sc_carpdev->if_carp->cif_im6o, NULL, NULL));
1047a9771948SGleb Smirnoff 	}
1048a9771948SGleb Smirnoff #endif /* INET6 */
1049a9771948SGleb Smirnoff 
1050891122d1SGleb Smirnoff resched:
105108b68b0eSGleb Smirnoff 	callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), carp_send_ad, sc);
105208b68b0eSGleb Smirnoff }
1053a9771948SGleb Smirnoff 
105408b68b0eSGleb Smirnoff static void
105508b68b0eSGleb Smirnoff carp_addroute(struct carp_softc *sc)
105608b68b0eSGleb Smirnoff {
105708b68b0eSGleb Smirnoff 	struct ifaddr *ifa;
105808b68b0eSGleb Smirnoff 
105908b68b0eSGleb Smirnoff 	CARP_FOREACH_IFA(sc, ifa)
10602512b096SGleb Smirnoff 		carp_ifa_addroute(ifa);
10612512b096SGleb Smirnoff }
10622512b096SGleb Smirnoff 
10632512b096SGleb Smirnoff static void
10642512b096SGleb Smirnoff carp_ifa_addroute(struct ifaddr *ifa)
10652512b096SGleb Smirnoff {
10662512b096SGleb Smirnoff 
106708b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
106808b68b0eSGleb Smirnoff #ifdef INET
106908b68b0eSGleb Smirnoff 	case AF_INET:
1070130aebbaSAlexander V. Chernikov 		in_addprefix(ifatoia(ifa));
107108b68b0eSGleb Smirnoff 		ifa_add_loopback_route(ifa,
107208b68b0eSGleb Smirnoff 		    (struct sockaddr *)&ifatoia(ifa)->ia_addr);
107308b68b0eSGleb Smirnoff 		break;
107408b68b0eSGleb Smirnoff #endif
107508b68b0eSGleb Smirnoff #ifdef INET6
107608b68b0eSGleb Smirnoff 	case AF_INET6:
107708b68b0eSGleb Smirnoff 		ifa_add_loopback_route(ifa,
107808b68b0eSGleb Smirnoff 		    (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
1079f6b84910SAlexander V. Chernikov 		nd6_add_ifa_lle(ifatoia6(ifa));
108008b68b0eSGleb Smirnoff 		break;
108108b68b0eSGleb Smirnoff #endif
108208b68b0eSGleb Smirnoff 	}
108308b68b0eSGleb Smirnoff }
108408b68b0eSGleb Smirnoff 
108508b68b0eSGleb Smirnoff static void
108608b68b0eSGleb Smirnoff carp_delroute(struct carp_softc *sc)
108708b68b0eSGleb Smirnoff {
108808b68b0eSGleb Smirnoff 	struct ifaddr *ifa;
108908b68b0eSGleb Smirnoff 
109008b68b0eSGleb Smirnoff 	CARP_FOREACH_IFA(sc, ifa)
10912512b096SGleb Smirnoff 		carp_ifa_delroute(ifa);
10922512b096SGleb Smirnoff }
10932512b096SGleb Smirnoff 
10942512b096SGleb Smirnoff static void
10952512b096SGleb Smirnoff carp_ifa_delroute(struct ifaddr *ifa)
10962512b096SGleb Smirnoff {
10972512b096SGleb Smirnoff 
109808b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
109908b68b0eSGleb Smirnoff #ifdef INET
110008b68b0eSGleb Smirnoff 	case AF_INET:
110108b68b0eSGleb Smirnoff 		ifa_del_loopback_route(ifa,
110208b68b0eSGleb Smirnoff 		    (struct sockaddr *)&ifatoia(ifa)->ia_addr);
110308b68b0eSGleb Smirnoff 		in_scrubprefix(ifatoia(ifa), LLE_STATIC);
110408b68b0eSGleb Smirnoff 		break;
110508b68b0eSGleb Smirnoff #endif
110608b68b0eSGleb Smirnoff #ifdef INET6
110708b68b0eSGleb Smirnoff 	case AF_INET6:
110808b68b0eSGleb Smirnoff 		ifa_del_loopback_route(ifa,
110908b68b0eSGleb Smirnoff 		    (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
11103e7a2321SAlexander V. Chernikov 		nd6_rem_ifa_lle(ifatoia6(ifa), 1);
111108b68b0eSGleb Smirnoff 		break;
111208b68b0eSGleb Smirnoff #endif
111308b68b0eSGleb Smirnoff 	}
1114a9771948SGleb Smirnoff }
1115a9771948SGleb Smirnoff 
111624421c1cSGleb Smirnoff int
111724421c1cSGleb Smirnoff carp_master(struct ifaddr *ifa)
111824421c1cSGleb Smirnoff {
111924421c1cSGleb Smirnoff 	struct carp_softc *sc = ifa->ifa_carp;
112024421c1cSGleb Smirnoff 
112124421c1cSGleb Smirnoff 	return (sc->sc_state == MASTER);
112224421c1cSGleb Smirnoff }
112324421c1cSGleb Smirnoff 
1124a0ae8f04SBjoern A. Zeeb #ifdef INET
1125a9771948SGleb Smirnoff /*
1126a9771948SGleb Smirnoff  * Broadcast a gratuitous ARP request containing
1127a9771948SGleb Smirnoff  * the virtual router MAC address for each IP address
1128a9771948SGleb Smirnoff  * associated with the virtual router.
1129a9771948SGleb Smirnoff  */
11305c1f0f6dSGleb Smirnoff static void
1131a9771948SGleb Smirnoff carp_send_arp(struct carp_softc *sc)
1132a9771948SGleb Smirnoff {
1133a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1134d6e82913SSteven Hartland 	struct in_addr addr;
1135a9771948SGleb Smirnoff 
11361d126e9bSKristof Provost 	NET_EPOCH_ASSERT();
11371d126e9bSKristof Provost 
11381c302b58SAlexander V. Chernikov 	CARP_FOREACH_IFA(sc, ifa) {
11391c302b58SAlexander V. Chernikov 		if (ifa->ifa_addr->sa_family != AF_INET)
11401c302b58SAlexander V. Chernikov 			continue;
1141d6e82913SSteven Hartland 		addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
1142d6e82913SSteven Hartland 		arp_announce_ifaddr(sc->sc_carpdev, addr, LLADDR(&sc->sc_addr));
11431c302b58SAlexander V. Chernikov 	}
1144a9771948SGleb Smirnoff }
114508b68b0eSGleb Smirnoff 
114608b68b0eSGleb Smirnoff int
114708b68b0eSGleb Smirnoff carp_iamatch(struct ifaddr *ifa, uint8_t **enaddr)
114808b68b0eSGleb Smirnoff {
114908b68b0eSGleb Smirnoff 	struct carp_softc *sc = ifa->ifa_carp;
115008b68b0eSGleb Smirnoff 
115108b68b0eSGleb Smirnoff 	if (sc->sc_state == MASTER) {
115208b68b0eSGleb Smirnoff 		*enaddr = LLADDR(&sc->sc_addr);
115308b68b0eSGleb Smirnoff 		return (1);
115408b68b0eSGleb Smirnoff 	}
115508b68b0eSGleb Smirnoff 
115608b68b0eSGleb Smirnoff 	return (0);
1157a9771948SGleb Smirnoff }
1158a0ae8f04SBjoern A. Zeeb #endif
1159a9771948SGleb Smirnoff 
1160a9771948SGleb Smirnoff #ifdef INET6
11615c1f0f6dSGleb Smirnoff static void
1162a9771948SGleb Smirnoff carp_send_na(struct carp_softc *sc)
1163a9771948SGleb Smirnoff {
1164d6e82913SSteven Hartland 	static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1165a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1166d6e82913SSteven Hartland 	struct in6_addr *in6;
1167a9771948SGleb Smirnoff 
116808b68b0eSGleb Smirnoff 	CARP_FOREACH_IFA(sc, ifa) {
1169d6e82913SSteven Hartland 		if (ifa->ifa_addr->sa_family != AF_INET6)
1170a9771948SGleb Smirnoff 			continue;
1171a9771948SGleb Smirnoff 
1172d6e82913SSteven Hartland 		in6 = IFA_IN6(ifa);
1173d6e82913SSteven Hartland 		nd6_na_output(sc->sc_carpdev, &mcast, in6,
1174d6e82913SSteven Hartland 		    ND_NA_FLAG_OVERRIDE, 1, NULL);
1175d6e82913SSteven Hartland 		DELAY(1000);	/* XXX */
1176a9771948SGleb Smirnoff 	}
1177a9771948SGleb Smirnoff }
1178a9771948SGleb Smirnoff 
11798253dcabSBjoern A. Zeeb /*
11808253dcabSBjoern A. Zeeb  * Returns ifa in case it's a carp address and it is MASTER, or if the address
11818253dcabSBjoern A. Zeeb  * matches and is not a carp address.  Returns NULL otherwise.
11828253dcabSBjoern A. Zeeb  */
1183a4e53905SMax Laier struct ifaddr *
118454bfbd51SWill Andrews carp_iamatch6(struct ifnet *ifp, struct in6_addr *taddr)
1185a9771948SGleb Smirnoff {
1186a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1187a9771948SGleb Smirnoff 
1188b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
1189b8a6e03fSGleb Smirnoff 
11908253dcabSBjoern A. Zeeb 	ifa = NULL;
1191d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
11928253dcabSBjoern A. Zeeb 		if (ifa->ifa_addr->sa_family != AF_INET6)
11938253dcabSBjoern A. Zeeb 			continue;
11948253dcabSBjoern A. Zeeb 		if (!IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa)))
11958253dcabSBjoern A. Zeeb 			continue;
11968253dcabSBjoern A. Zeeb 		if (ifa->ifa_carp && ifa->ifa_carp->sc_state != MASTER)
11978253dcabSBjoern A. Zeeb 			ifa = NULL;
11988253dcabSBjoern A. Zeeb 		else
11998c0fec80SRobert Watson 			ifa_ref(ifa);
12008253dcabSBjoern A. Zeeb 		break;
1201a9771948SGleb Smirnoff 	}
1202a9771948SGleb Smirnoff 
12038253dcabSBjoern A. Zeeb 	return (ifa);
1204a9771948SGleb Smirnoff }
1205a9771948SGleb Smirnoff 
1206dad68fc3SBjoern A. Zeeb char *
120754bfbd51SWill Andrews carp_macmatch6(struct ifnet *ifp, struct mbuf *m, const struct in6_addr *taddr)
1208a9771948SGleb Smirnoff {
1209a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1210a9771948SGleb Smirnoff 
1211b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
1212b8a6e03fSGleb Smirnoff 
121308b68b0eSGleb Smirnoff 	IFNET_FOREACH_IFA(ifp, ifa)
121408b68b0eSGleb Smirnoff 		if (ifa->ifa_addr->sa_family == AF_INET6 &&
121508b68b0eSGleb Smirnoff 		    IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) {
121608b68b0eSGleb Smirnoff 			struct carp_softc *sc = ifa->ifa_carp;
121708b68b0eSGleb Smirnoff 			struct m_tag *mtag;
121808b68b0eSGleb Smirnoff 
1219a9771948SGleb Smirnoff 			mtag = m_tag_get(PACKET_TAG_CARP,
1220a856ddc6SGleb Smirnoff 			    sizeof(struct carp_softc *), M_NOWAIT);
122108b68b0eSGleb Smirnoff 			if (mtag == NULL)
122208b68b0eSGleb Smirnoff 				/* Better a bit than nothing. */
122308b68b0eSGleb Smirnoff 				return (LLADDR(&sc->sc_addr));
122408b68b0eSGleb Smirnoff 
1225eaf151c4SGleb Smirnoff 			bcopy(&sc, mtag + 1, sizeof(sc));
1226a9771948SGleb Smirnoff 			m_tag_prepend(m, mtag);
1227a9771948SGleb Smirnoff 
122808b68b0eSGleb Smirnoff 			return (LLADDR(&sc->sc_addr));
1229a9771948SGleb Smirnoff 		}
1230a9771948SGleb Smirnoff 
1231a9771948SGleb Smirnoff 	return (NULL);
1232a9771948SGleb Smirnoff }
123308b68b0eSGleb Smirnoff #endif /* INET6 */
1234a9771948SGleb Smirnoff 
123508b68b0eSGleb Smirnoff int
123654bfbd51SWill Andrews carp_forus(struct ifnet *ifp, u_char *dhost)
1237a9771948SGleb Smirnoff {
123808b68b0eSGleb Smirnoff 	struct carp_softc *sc;
123908b68b0eSGleb Smirnoff 	uint8_t *ena = dhost;
1240a9771948SGleb Smirnoff 
1241a9771948SGleb Smirnoff 	if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
124208b68b0eSGleb Smirnoff 		return (0);
1243a9771948SGleb Smirnoff 
124408b68b0eSGleb Smirnoff 	CIF_LOCK(ifp->if_carp);
124508b68b0eSGleb Smirnoff 	IFNET_FOREACH_CARP(ifp, sc) {
12468c3fbf3cSAlexander Motin 		/*
12478c3fbf3cSAlexander Motin 		 * CARP_LOCK() is not here, since would protect nothing, but
12488c3fbf3cSAlexander Motin 		 * cause deadlock with if_bridge, calling this under its lock.
12498c3fbf3cSAlexander Motin 		 */
125008b68b0eSGleb Smirnoff 		if (sc->sc_state == MASTER && !bcmp(dhost, LLADDR(&sc->sc_addr),
125108b68b0eSGleb Smirnoff 		    ETHER_ADDR_LEN)) {
125208b68b0eSGleb Smirnoff 			CIF_UNLOCK(ifp->if_carp);
125308b68b0eSGleb Smirnoff 			return (1);
1254a9771948SGleb Smirnoff 		}
125508b68b0eSGleb Smirnoff 	}
125608b68b0eSGleb Smirnoff 	CIF_UNLOCK(ifp->if_carp);
1257a9771948SGleb Smirnoff 
125808b68b0eSGleb Smirnoff 	return (0);
1259a9771948SGleb Smirnoff }
1260a9771948SGleb Smirnoff 
1261afdbac98SGleb Smirnoff /* Master down timeout event, executed in callout context. */
12625c1f0f6dSGleb Smirnoff static void
1263a9771948SGleb Smirnoff carp_master_down(void *v)
1264a9771948SGleb Smirnoff {
1265a9771948SGleb Smirnoff 	struct carp_softc *sc = v;
12661d126e9bSKristof Provost 	struct epoch_tracker et;
1267a9771948SGleb Smirnoff 
12681d126e9bSKristof Provost 	NET_EPOCH_ENTER(et);
126908b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
127008b68b0eSGleb Smirnoff 
1271afdbac98SGleb Smirnoff 	CURVNET_SET(sc->sc_carpdev->if_vnet);
127208b68b0eSGleb Smirnoff 	if (sc->sc_state == BACKUP) {
1273d01641e2SWill Andrews 		carp_master_down_locked(sc, "master timed out");
127408b68b0eSGleb Smirnoff 	}
1275afdbac98SGleb Smirnoff 	CURVNET_RESTORE();
127608b68b0eSGleb Smirnoff 
127708b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
12781d126e9bSKristof Provost 	NET_EPOCH_EXIT(et);
1279d220759bSGleb Smirnoff }
1280d220759bSGleb Smirnoff 
1281d220759bSGleb Smirnoff static void
1282d01641e2SWill Andrews carp_master_down_locked(struct carp_softc *sc, const char *reason)
1283d220759bSGleb Smirnoff {
128408b68b0eSGleb Smirnoff 
12851d126e9bSKristof Provost 	NET_EPOCH_ASSERT();
128608b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
1287d220759bSGleb Smirnoff 
1288a9771948SGleb Smirnoff 	switch (sc->sc_state) {
1289a9771948SGleb Smirnoff 	case BACKUP:
1290d01641e2SWill Andrews 		carp_set_state(sc, MASTER, reason);
1291d220759bSGleb Smirnoff 		carp_send_ad_locked(sc);
1292a0ae8f04SBjoern A. Zeeb #ifdef INET
1293a9771948SGleb Smirnoff 		carp_send_arp(sc);
1294a0ae8f04SBjoern A. Zeeb #endif
1295a9771948SGleb Smirnoff #ifdef INET6
1296a9771948SGleb Smirnoff 		carp_send_na(sc);
129708b68b0eSGleb Smirnoff #endif
1298a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
129908b68b0eSGleb Smirnoff 		carp_addroute(sc);
130008b68b0eSGleb Smirnoff 		break;
130108b68b0eSGleb Smirnoff 	case INIT:
130208b68b0eSGleb Smirnoff 	case MASTER:
130308b68b0eSGleb Smirnoff #ifdef INVARIANTS
130408b68b0eSGleb Smirnoff 		panic("carp: VHID %u@%s: master_down event in %s state\n",
130508b68b0eSGleb Smirnoff 		    sc->sc_vhid,
130608b68b0eSGleb Smirnoff 		    sc->sc_carpdev->if_xname,
130708b68b0eSGleb Smirnoff 		    sc->sc_state ? "MASTER" : "INIT");
130808b68b0eSGleb Smirnoff #endif
1309a9771948SGleb Smirnoff 		break;
1310a9771948SGleb Smirnoff 	}
1311a9771948SGleb Smirnoff }
1312a9771948SGleb Smirnoff 
1313a9771948SGleb Smirnoff /*
1314a9771948SGleb Smirnoff  * When in backup state, af indicates whether to reset the master down timer
1315a9771948SGleb Smirnoff  * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1316a9771948SGleb Smirnoff  */
13175c1f0f6dSGleb Smirnoff static void
1318a9771948SGleb Smirnoff carp_setrun(struct carp_softc *sc, sa_family_t af)
1319a9771948SGleb Smirnoff {
1320a9771948SGleb Smirnoff 	struct timeval tv;
1321a9771948SGleb Smirnoff 
132208b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
1323d220759bSGleb Smirnoff 
132408b68b0eSGleb Smirnoff 	if ((sc->sc_carpdev->if_flags & IFF_UP) == 0 ||
132508b68b0eSGleb Smirnoff 	    sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1326167a3440SAlexander Motin 	    (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) ||
1327167a3440SAlexander Motin 	    !V_carp_allow)
1328a9771948SGleb Smirnoff 		return;
1329a9771948SGleb Smirnoff 
1330a9771948SGleb Smirnoff 	switch (sc->sc_state) {
1331a9771948SGleb Smirnoff 	case INIT:
1332d01641e2SWill Andrews 		carp_set_state(sc, BACKUP, "initialization complete");
1333a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
1334a9771948SGleb Smirnoff 		break;
1335a9771948SGleb Smirnoff 	case BACKUP:
1336a9771948SGleb Smirnoff 		callout_stop(&sc->sc_ad_tmo);
1337a9771948SGleb Smirnoff 		tv.tv_sec = 3 * sc->sc_advbase;
1338a9771948SGleb Smirnoff 		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1339a9771948SGleb Smirnoff 		switch (af) {
1340a9771948SGleb Smirnoff #ifdef INET
1341a9771948SGleb Smirnoff 		case AF_INET:
1342a9771948SGleb Smirnoff 			callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1343a9771948SGleb Smirnoff 			    carp_master_down, sc);
1344a9771948SGleb Smirnoff 			break;
134508b68b0eSGleb Smirnoff #endif
1346a9771948SGleb Smirnoff #ifdef INET6
1347a9771948SGleb Smirnoff 		case AF_INET6:
1348a9771948SGleb Smirnoff 			callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1349a9771948SGleb Smirnoff 			    carp_master_down, sc);
1350a9771948SGleb Smirnoff 			break;
135108b68b0eSGleb Smirnoff #endif
1352a9771948SGleb Smirnoff 		default:
135308b68b0eSGleb Smirnoff #ifdef INET
1354a9771948SGleb Smirnoff 			if (sc->sc_naddrs)
1355a9771948SGleb Smirnoff 				callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1356a9771948SGleb Smirnoff 				    carp_master_down, sc);
135708b68b0eSGleb Smirnoff #endif
135808b68b0eSGleb Smirnoff #ifdef INET6
1359a9771948SGleb Smirnoff 			if (sc->sc_naddrs6)
1360a9771948SGleb Smirnoff 				callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1361a9771948SGleb Smirnoff 				    carp_master_down, sc);
136208b68b0eSGleb Smirnoff #endif
1363a9771948SGleb Smirnoff 			break;
1364a9771948SGleb Smirnoff 		}
1365a9771948SGleb Smirnoff 		break;
1366a9771948SGleb Smirnoff 	case MASTER:
1367a9771948SGleb Smirnoff 		tv.tv_sec = sc->sc_advbase;
1368a9771948SGleb Smirnoff 		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1369a9771948SGleb Smirnoff 		callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1370a9771948SGleb Smirnoff 		    carp_send_ad, sc);
1371a9771948SGleb Smirnoff 		break;
1372a9771948SGleb Smirnoff 	}
1373a9771948SGleb Smirnoff }
1374a9771948SGleb Smirnoff 
137508b68b0eSGleb Smirnoff /*
137608b68b0eSGleb Smirnoff  * Setup multicast structures.
137708b68b0eSGleb Smirnoff  */
13785c1f0f6dSGleb Smirnoff static int
1379a9a2c40cSGleb Smirnoff carp_multicast_setup(struct carp_if *cif, sa_family_t sa)
1380a9771948SGleb Smirnoff {
1381a9a2c40cSGleb Smirnoff 	struct ifnet *ifp = cif->cif_ifp;
138208b68b0eSGleb Smirnoff 	int error = 0;
138308b68b0eSGleb Smirnoff 
138408b68b0eSGleb Smirnoff 	switch (sa) {
138508b68b0eSGleb Smirnoff #ifdef INET
138608b68b0eSGleb Smirnoff 	case AF_INET:
138708b68b0eSGleb Smirnoff 	    {
138808b68b0eSGleb Smirnoff 		struct ip_moptions *imo = &cif->cif_imo;
138959854ecfSHans Petter Selasky 		struct in_mfilter *imf;
1390a9771948SGleb Smirnoff 		struct in_addr addr;
1391a9771948SGleb Smirnoff 
139259854ecfSHans Petter Selasky 		if (ip_mfilter_first(&imo->imo_head) != NULL)
1393a9771948SGleb Smirnoff 			return (0);
1394a9771948SGleb Smirnoff 
139559854ecfSHans Petter Selasky 		imf = ip_mfilter_alloc(M_WAITOK, 0, 0);
139659854ecfSHans Petter Selasky 		ip_mfilter_init(&imo->imo_head);
139708b68b0eSGleb Smirnoff 		imo->imo_multicast_vif = -1;
1398a9771948SGleb Smirnoff 
1399a9771948SGleb Smirnoff 		addr.s_addr = htonl(INADDR_CARP_GROUP);
140008b68b0eSGleb Smirnoff 		if ((error = in_joingroup(ifp, &addr, NULL,
140159854ecfSHans Petter Selasky 		    &imf->imf_inm)) != 0) {
140259854ecfSHans Petter Selasky 			ip_mfilter_free(imf);
140308b68b0eSGleb Smirnoff 			break;
14042d9cfabaSRobert Watson 		}
140559854ecfSHans Petter Selasky 
140659854ecfSHans Petter Selasky 		ip_mfilter_insert(&imo->imo_head, imf);
1407a9771948SGleb Smirnoff 		imo->imo_multicast_ifp = ifp;
1408a9771948SGleb Smirnoff 		imo->imo_multicast_ttl = CARP_DFLTTL;
1409a9771948SGleb Smirnoff 		imo->imo_multicast_loop = 0;
1410a9771948SGleb Smirnoff 		break;
1411a9771948SGleb Smirnoff 	   }
141208b68b0eSGleb Smirnoff #endif
141308b68b0eSGleb Smirnoff #ifdef INET6
141408b68b0eSGleb Smirnoff 	case AF_INET6:
141508b68b0eSGleb Smirnoff 	    {
141608b68b0eSGleb Smirnoff 		struct ip6_moptions *im6o = &cif->cif_im6o;
141759854ecfSHans Petter Selasky 		struct in6_mfilter *im6f[2];
141808b68b0eSGleb Smirnoff 		struct in6_addr in6;
141933cde130SBruce M Simpson 
142059854ecfSHans Petter Selasky 		if (ip6_mfilter_first(&im6o->im6o_head))
142108b68b0eSGleb Smirnoff 			return (0);
142208b68b0eSGleb Smirnoff 
142359854ecfSHans Petter Selasky 		im6f[0] = ip6_mfilter_alloc(M_WAITOK, 0, 0);
142459854ecfSHans Petter Selasky 		im6f[1] = ip6_mfilter_alloc(M_WAITOK, 0, 0);
142559854ecfSHans Petter Selasky 
142659854ecfSHans Petter Selasky 		ip6_mfilter_init(&im6o->im6o_head);
142708b68b0eSGleb Smirnoff 		im6o->im6o_multicast_hlim = CARP_DFLTTL;
1428a9771948SGleb Smirnoff 		im6o->im6o_multicast_ifp = ifp;
1429a9771948SGleb Smirnoff 
143008b68b0eSGleb Smirnoff 		/* Join IPv6 CARP multicast group. */
1431a1f7e5f8SHajimu UMEMOTO 		bzero(&in6, sizeof(in6));
1432a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr16[0] = htons(0xff02);
1433a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr8[15] = 0x12;
143408b68b0eSGleb Smirnoff 		if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
143559854ecfSHans Petter Selasky 			ip6_mfilter_free(im6f[0]);
143659854ecfSHans Petter Selasky 			ip6_mfilter_free(im6f[1]);
143708b68b0eSGleb Smirnoff 			break;
143808b68b0eSGleb Smirnoff 		}
143959854ecfSHans Petter Selasky 		if ((error = in6_joingroup(ifp, &in6, NULL, &im6f[0]->im6f_in6m, 0)) != 0) {
144059854ecfSHans Petter Selasky 			ip6_mfilter_free(im6f[0]);
144159854ecfSHans Petter Selasky 			ip6_mfilter_free(im6f[1]);
144208b68b0eSGleb Smirnoff 			break;
144308b68b0eSGleb Smirnoff 		}
1444a9771948SGleb Smirnoff 
144508b68b0eSGleb Smirnoff 		/* Join solicited multicast address. */
1446a1f7e5f8SHajimu UMEMOTO 		bzero(&in6, sizeof(in6));
1447a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr16[0] = htons(0xff02);
1448a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr32[1] = 0;
1449a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr32[2] = htonl(1);
145008b68b0eSGleb Smirnoff 		in6.s6_addr32[3] = 0;
1451a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr8[12] = 0xff;
145259854ecfSHans Petter Selasky 
145308b68b0eSGleb Smirnoff 		if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
145459854ecfSHans Petter Selasky 			ip6_mfilter_free(im6f[0]);
145559854ecfSHans Petter Selasky 			ip6_mfilter_free(im6f[1]);
145608b68b0eSGleb Smirnoff 			break;
145708b68b0eSGleb Smirnoff 		}
145859854ecfSHans Petter Selasky 
145959854ecfSHans Petter Selasky 		if ((error = in6_joingroup(ifp, &in6, NULL, &im6f[1]->im6f_in6m, 0)) != 0) {
146059854ecfSHans Petter Selasky 			in6_leavegroup(im6f[0]->im6f_in6m, NULL);
146159854ecfSHans Petter Selasky 			ip6_mfilter_free(im6f[0]);
146259854ecfSHans Petter Selasky 			ip6_mfilter_free(im6f[1]);
146308b68b0eSGleb Smirnoff 			break;
146408b68b0eSGleb Smirnoff 		}
146559854ecfSHans Petter Selasky 		ip6_mfilter_insert(&im6o->im6o_head, im6f[0]);
146659854ecfSHans Petter Selasky 		ip6_mfilter_insert(&im6o->im6o_head, im6f[1]);
1467a9771948SGleb Smirnoff 		break;
1468a9771948SGleb Smirnoff 	    }
1469a9771948SGleb Smirnoff #endif
147008b68b0eSGleb Smirnoff 	}
147108b68b0eSGleb Smirnoff 
147208b68b0eSGleb Smirnoff 	return (error);
1473a9771948SGleb Smirnoff }
1474a9771948SGleb Smirnoff 
1475a9771948SGleb Smirnoff /*
147608b68b0eSGleb Smirnoff  * Free multicast structures.
1477a9771948SGleb Smirnoff  */
14785c1f0f6dSGleb Smirnoff static void
1479a9a2c40cSGleb Smirnoff carp_multicast_cleanup(struct carp_if *cif, sa_family_t sa)
1480a9771948SGleb Smirnoff {
148159854ecfSHans Petter Selasky #ifdef INET
148259854ecfSHans Petter Selasky 	struct ip_moptions *imo = &cif->cif_imo;
148359854ecfSHans Petter Selasky 	struct in_mfilter *imf;
148459854ecfSHans Petter Selasky #endif
148559854ecfSHans Petter Selasky #ifdef INET6
148659854ecfSHans Petter Selasky 	struct ip6_moptions *im6o = &cif->cif_im6o;
148759854ecfSHans Petter Selasky 	struct in6_mfilter *im6f;
148859854ecfSHans Petter Selasky #endif
14899c2cd1aaSGleb Smirnoff 	sx_assert(&carp_sx, SA_XLOCKED);
14909c2cd1aaSGleb Smirnoff 
149108b68b0eSGleb Smirnoff 	switch (sa) {
149208b68b0eSGleb Smirnoff #ifdef INET
149308b68b0eSGleb Smirnoff 	case AF_INET:
149459854ecfSHans Petter Selasky 		if (cif->cif_naddrs != 0)
149559854ecfSHans Petter Selasky 			break;
149608b68b0eSGleb Smirnoff 
149759854ecfSHans Petter Selasky 		while ((imf = ip_mfilter_first(&imo->imo_head)) != NULL) {
149859854ecfSHans Petter Selasky 			ip_mfilter_remove(&imo->imo_head, imf);
149959854ecfSHans Petter Selasky 			in_leavegroup(imf->imf_inm, NULL);
150059854ecfSHans Petter Selasky 			ip_mfilter_free(imf);
150108b68b0eSGleb Smirnoff 		}
150208b68b0eSGleb Smirnoff 		break;
1503a9771948SGleb Smirnoff #endif
150408b68b0eSGleb Smirnoff #ifdef INET6
150508b68b0eSGleb Smirnoff 	case AF_INET6:
150659854ecfSHans Petter Selasky 		if (cif->cif_naddrs6 != 0)
150759854ecfSHans Petter Selasky 			break;
150808b68b0eSGleb Smirnoff 
150959854ecfSHans Petter Selasky 		while ((im6f = ip6_mfilter_first(&im6o->im6o_head)) != NULL) {
151059854ecfSHans Petter Selasky 			ip6_mfilter_remove(&im6o->im6o_head, im6f);
151159854ecfSHans Petter Selasky 			in6_leavegroup(im6f->im6f_in6m, NULL);
151259854ecfSHans Petter Selasky 			ip6_mfilter_free(im6f);
151308b68b0eSGleb Smirnoff 		}
151408b68b0eSGleb Smirnoff 		break;
151508b68b0eSGleb Smirnoff #endif
151608b68b0eSGleb Smirnoff 	}
1517a9771948SGleb Smirnoff }
1518a9771948SGleb Smirnoff 
1519a9771948SGleb Smirnoff int
152047e8d432SGleb Smirnoff carp_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa)
1521a9771948SGleb Smirnoff {
1522a9771948SGleb Smirnoff 	struct m_tag *mtag;
1523a9771948SGleb Smirnoff 	struct carp_softc *sc;
1524a9771948SGleb Smirnoff 
1525a9771948SGleb Smirnoff 	if (!sa)
1526a9771948SGleb Smirnoff 		return (0);
1527a9771948SGleb Smirnoff 
1528a9771948SGleb Smirnoff 	switch (sa->sa_family) {
1529a9771948SGleb Smirnoff #ifdef INET
1530a9771948SGleb Smirnoff 	case AF_INET:
1531a9771948SGleb Smirnoff 		break;
153208b68b0eSGleb Smirnoff #endif
1533a9771948SGleb Smirnoff #ifdef INET6
1534a9771948SGleb Smirnoff 	case AF_INET6:
1535a9771948SGleb Smirnoff 		break;
153608b68b0eSGleb Smirnoff #endif
1537a9771948SGleb Smirnoff 	default:
1538a9771948SGleb Smirnoff 		return (0);
1539a9771948SGleb Smirnoff 	}
1540a9771948SGleb Smirnoff 
1541a9771948SGleb Smirnoff 	mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
1542a9771948SGleb Smirnoff 	if (mtag == NULL)
1543a9771948SGleb Smirnoff 		return (0);
1544a9771948SGleb Smirnoff 
1545eaf151c4SGleb Smirnoff 	bcopy(mtag + 1, &sc, sizeof(sc));
1546a9771948SGleb Smirnoff 
154708b68b0eSGleb Smirnoff 	/* Set the source MAC address to the Virtual Router MAC Address. */
1548a9771948SGleb Smirnoff 	switch (ifp->if_type) {
1549630481bbSYaroslav Tykhiy 	case IFT_ETHER:
15509ca1fe0dSGleb Smirnoff 	case IFT_BRIDGE:
1551630481bbSYaroslav Tykhiy 	case IFT_L2VLAN: {
1552a9771948SGleb Smirnoff 			struct ether_header *eh;
1553a9771948SGleb Smirnoff 
1554a9771948SGleb Smirnoff 			eh = mtod(m, struct ether_header *);
1555a9771948SGleb Smirnoff 			eh->ether_shost[0] = 0;
1556a9771948SGleb Smirnoff 			eh->ether_shost[1] = 0;
1557a9771948SGleb Smirnoff 			eh->ether_shost[2] = 0x5e;
1558a9771948SGleb Smirnoff 			eh->ether_shost[3] = 0;
1559a9771948SGleb Smirnoff 			eh->ether_shost[4] = 1;
1560a9771948SGleb Smirnoff 			eh->ether_shost[5] = sc->sc_vhid;
1561a9771948SGleb Smirnoff 		}
1562a9771948SGleb Smirnoff 		break;
1563a9771948SGleb Smirnoff 	default:
156408b68b0eSGleb Smirnoff 		printf("%s: carp is not supported for the %d interface type\n",
156508b68b0eSGleb Smirnoff 		    ifp->if_xname, ifp->if_type);
1566a9771948SGleb Smirnoff 		return (EOPNOTSUPP);
1567a9771948SGleb Smirnoff 	}
1568a9771948SGleb Smirnoff 
1569a9771948SGleb Smirnoff 	return (0);
1570a9771948SGleb Smirnoff }
1571a9771948SGleb Smirnoff 
157208b68b0eSGleb Smirnoff static struct carp_softc*
157308b68b0eSGleb Smirnoff carp_alloc(struct ifnet *ifp)
1574a9771948SGleb Smirnoff {
157508b68b0eSGleb Smirnoff 	struct carp_softc *sc;
157608b68b0eSGleb Smirnoff 	struct carp_if *cif;
1577d220759bSGleb Smirnoff 
157881098a01SAlexander Motin 	sx_assert(&carp_sx, SA_XLOCKED);
157981098a01SAlexander Motin 
15800cc726f2SGleb Smirnoff 	if ((cif = ifp->if_carp) == NULL)
158108b68b0eSGleb Smirnoff 		cif = carp_alloc_if(ifp);
1582a9771948SGleb Smirnoff 
158308b68b0eSGleb Smirnoff 	sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
158408b68b0eSGleb Smirnoff 
158508b68b0eSGleb Smirnoff 	sc->sc_advbase = CARP_DFLTINTV;
158608b68b0eSGleb Smirnoff 	sc->sc_vhid = -1;	/* required setting */
158708b68b0eSGleb Smirnoff 	sc->sc_init_counter = 1;
158808b68b0eSGleb Smirnoff 	sc->sc_state = INIT;
158908b68b0eSGleb Smirnoff 
159008b68b0eSGleb Smirnoff 	sc->sc_ifasiz = sizeof(struct ifaddr *);
159108b68b0eSGleb Smirnoff 	sc->sc_ifas = malloc(sc->sc_ifasiz, M_CARP, M_WAITOK|M_ZERO);
159208b68b0eSGleb Smirnoff 	sc->sc_carpdev = ifp;
159308b68b0eSGleb Smirnoff 
159408b68b0eSGleb Smirnoff 	CARP_LOCK_INIT(sc);
159508b68b0eSGleb Smirnoff #ifdef INET
159608b68b0eSGleb Smirnoff 	callout_init_mtx(&sc->sc_md_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
159708b68b0eSGleb Smirnoff #endif
159808b68b0eSGleb Smirnoff #ifdef INET6
159908b68b0eSGleb Smirnoff 	callout_init_mtx(&sc->sc_md6_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
160008b68b0eSGleb Smirnoff #endif
160108b68b0eSGleb Smirnoff 	callout_init_mtx(&sc->sc_ad_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
160208b68b0eSGleb Smirnoff 
160308b68b0eSGleb Smirnoff 	CIF_LOCK(cif);
160408b68b0eSGleb Smirnoff 	TAILQ_INSERT_TAIL(&cif->cif_vrs, sc, sc_list);
160508b68b0eSGleb Smirnoff 	CIF_UNLOCK(cif);
160608b68b0eSGleb Smirnoff 
160708b68b0eSGleb Smirnoff 	mtx_lock(&carp_mtx);
160808b68b0eSGleb Smirnoff 	LIST_INSERT_HEAD(&carp_list, sc, sc_next);
160908b68b0eSGleb Smirnoff 	mtx_unlock(&carp_mtx);
161008b68b0eSGleb Smirnoff 
161108b68b0eSGleb Smirnoff 	return (sc);
161208b68b0eSGleb Smirnoff }
161308b68b0eSGleb Smirnoff 
16149c2cd1aaSGleb Smirnoff static void
161508b68b0eSGleb Smirnoff carp_grow_ifas(struct carp_softc *sc)
161608b68b0eSGleb Smirnoff {
161708b68b0eSGleb Smirnoff 	struct ifaddr **new;
161808b68b0eSGleb Smirnoff 
16199c2cd1aaSGleb Smirnoff 	new = malloc(sc->sc_ifasiz * 2, M_CARP, M_WAITOK | M_ZERO);
16209c2cd1aaSGleb Smirnoff 	CARP_LOCK(sc);
162108b68b0eSGleb Smirnoff 	bcopy(sc->sc_ifas, new, sc->sc_ifasiz);
162208b68b0eSGleb Smirnoff 	free(sc->sc_ifas, M_CARP);
162308b68b0eSGleb Smirnoff 	sc->sc_ifas = new;
162408b68b0eSGleb Smirnoff 	sc->sc_ifasiz *= 2;
16259c2cd1aaSGleb Smirnoff 	CARP_UNLOCK(sc);
162608b68b0eSGleb Smirnoff }
162708b68b0eSGleb Smirnoff 
162808b68b0eSGleb Smirnoff static void
162908b68b0eSGleb Smirnoff carp_destroy(struct carp_softc *sc)
163008b68b0eSGleb Smirnoff {
163108b68b0eSGleb Smirnoff 	struct ifnet *ifp = sc->sc_carpdev;
163208b68b0eSGleb Smirnoff 	struct carp_if *cif = ifp->if_carp;
163308b68b0eSGleb Smirnoff 
16349c2cd1aaSGleb Smirnoff 	sx_assert(&carp_sx, SA_XLOCKED);
1635a9a2c40cSGleb Smirnoff 
16369c2cd1aaSGleb Smirnoff 	if (sc->sc_suppress)
16379c2cd1aaSGleb Smirnoff 		carp_demote_adj(-V_carp_ifdown_adj, "vhid removed");
16389c2cd1aaSGleb Smirnoff 	CARP_UNLOCK(sc);
16399c2cd1aaSGleb Smirnoff 
16409c2cd1aaSGleb Smirnoff 	CIF_LOCK(cif);
164108b68b0eSGleb Smirnoff 	TAILQ_REMOVE(&cif->cif_vrs, sc, sc_list);
16429c2cd1aaSGleb Smirnoff 	CIF_UNLOCK(cif);
164308b68b0eSGleb Smirnoff 
164408b68b0eSGleb Smirnoff 	mtx_lock(&carp_mtx);
164508b68b0eSGleb Smirnoff 	LIST_REMOVE(sc, sc_next);
164608b68b0eSGleb Smirnoff 	mtx_unlock(&carp_mtx);
164708b68b0eSGleb Smirnoff 
164808b68b0eSGleb Smirnoff 	callout_drain(&sc->sc_ad_tmo);
164908b68b0eSGleb Smirnoff #ifdef INET
165008b68b0eSGleb Smirnoff 	callout_drain(&sc->sc_md_tmo);
165108b68b0eSGleb Smirnoff #endif
165208b68b0eSGleb Smirnoff #ifdef INET6
165308b68b0eSGleb Smirnoff 	callout_drain(&sc->sc_md6_tmo);
165408b68b0eSGleb Smirnoff #endif
165508b68b0eSGleb Smirnoff 	CARP_LOCK_DESTROY(sc);
165608b68b0eSGleb Smirnoff 
165708b68b0eSGleb Smirnoff 	free(sc->sc_ifas, M_CARP);
165808b68b0eSGleb Smirnoff 	free(sc, M_CARP);
165908b68b0eSGleb Smirnoff }
166008b68b0eSGleb Smirnoff 
166108b68b0eSGleb Smirnoff static struct carp_if*
166208b68b0eSGleb Smirnoff carp_alloc_if(struct ifnet *ifp)
1663a9771948SGleb Smirnoff {
166454bfbd51SWill Andrews 	struct carp_if *cif;
16650cc726f2SGleb Smirnoff 	int error;
1666d220759bSGleb Smirnoff 
166708b68b0eSGleb Smirnoff 	cif = malloc(sizeof(*cif), M_CARP, M_WAITOK|M_ZERO);
166808b68b0eSGleb Smirnoff 
16690cc726f2SGleb Smirnoff 	if ((error = ifpromisc(ifp, 1)) != 0)
16700cc726f2SGleb Smirnoff 		printf("%s: ifpromisc(%s) failed: %d\n",
16710cc726f2SGleb Smirnoff 		    __func__, ifp->if_xname, error);
16720cc726f2SGleb Smirnoff 	else
16730cc726f2SGleb Smirnoff 		cif->cif_flags |= CIF_PROMISC;
167408b68b0eSGleb Smirnoff 
167508b68b0eSGleb Smirnoff 	CIF_LOCK_INIT(cif);
167608b68b0eSGleb Smirnoff 	cif->cif_ifp = ifp;
167708b68b0eSGleb Smirnoff 	TAILQ_INIT(&cif->cif_vrs);
167808b68b0eSGleb Smirnoff 
1679137f91e8SJohn Baldwin 	IF_ADDR_WLOCK(ifp);
168008b68b0eSGleb Smirnoff 	ifp->if_carp = cif;
168108b68b0eSGleb Smirnoff 	if_ref(ifp);
1682137f91e8SJohn Baldwin 	IF_ADDR_WUNLOCK(ifp);
168308b68b0eSGleb Smirnoff 
168408b68b0eSGleb Smirnoff 	return (cif);
1685d220759bSGleb Smirnoff }
1686d220759bSGleb Smirnoff 
1687d220759bSGleb Smirnoff static void
168808b68b0eSGleb Smirnoff carp_free_if(struct carp_if *cif)
168908b68b0eSGleb Smirnoff {
169008b68b0eSGleb Smirnoff 	struct ifnet *ifp = cif->cif_ifp;
169108b68b0eSGleb Smirnoff 
169208b68b0eSGleb Smirnoff 	CIF_LOCK_ASSERT(cif);
169308b68b0eSGleb Smirnoff 	KASSERT(TAILQ_EMPTY(&cif->cif_vrs), ("%s: softc list not empty",
169408b68b0eSGleb Smirnoff 	    __func__));
169508b68b0eSGleb Smirnoff 
1696137f91e8SJohn Baldwin 	IF_ADDR_WLOCK(ifp);
169708b68b0eSGleb Smirnoff 	ifp->if_carp = NULL;
1698137f91e8SJohn Baldwin 	IF_ADDR_WUNLOCK(ifp);
169908b68b0eSGleb Smirnoff 
170008b68b0eSGleb Smirnoff 	CIF_LOCK_DESTROY(cif);
170108b68b0eSGleb Smirnoff 
17020cc726f2SGleb Smirnoff 	if (cif->cif_flags & CIF_PROMISC)
170308b68b0eSGleb Smirnoff 		ifpromisc(ifp, 0);
17041f6addd9SMikolaj Golub 	if_rele(ifp);
170508b68b0eSGleb Smirnoff 
170608b68b0eSGleb Smirnoff 	free(cif, M_CARP);
170708b68b0eSGleb Smirnoff }
170808b68b0eSGleb Smirnoff 
170908b68b0eSGleb Smirnoff static void
171008b68b0eSGleb Smirnoff carp_carprcp(struct carpreq *carpr, struct carp_softc *sc, int priv)
171108b68b0eSGleb Smirnoff {
171208b68b0eSGleb Smirnoff 
171308b68b0eSGleb Smirnoff 	CARP_LOCK(sc);
171408b68b0eSGleb Smirnoff 	carpr->carpr_state = sc->sc_state;
171508b68b0eSGleb Smirnoff 	carpr->carpr_vhid = sc->sc_vhid;
171608b68b0eSGleb Smirnoff 	carpr->carpr_advbase = sc->sc_advbase;
171708b68b0eSGleb Smirnoff 	carpr->carpr_advskew = sc->sc_advskew;
171808b68b0eSGleb Smirnoff 	if (priv)
171908b68b0eSGleb Smirnoff 		bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key));
172008b68b0eSGleb Smirnoff 	else
172108b68b0eSGleb Smirnoff 		bzero(carpr->carpr_key, sizeof(carpr->carpr_key));
172208b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
172308b68b0eSGleb Smirnoff }
172408b68b0eSGleb Smirnoff 
172508b68b0eSGleb Smirnoff int
172608b68b0eSGleb Smirnoff carp_ioctl(struct ifreq *ifr, u_long cmd, struct thread *td)
172708b68b0eSGleb Smirnoff {
172808b68b0eSGleb Smirnoff 	struct carpreq carpr;
172908b68b0eSGleb Smirnoff 	struct ifnet *ifp;
173008b68b0eSGleb Smirnoff 	struct carp_softc *sc = NULL;
173108b68b0eSGleb Smirnoff 	int error = 0, locked = 0;
173208b68b0eSGleb Smirnoff 
1733541d96aaSBrooks Davis 	if ((error = copyin(ifr_data_get_ptr(ifr), &carpr, sizeof carpr)))
173408b68b0eSGleb Smirnoff 		return (error);
173508b68b0eSGleb Smirnoff 
173608b68b0eSGleb Smirnoff 	ifp = ifunit_ref(ifr->ifr_name);
173708b68b0eSGleb Smirnoff 	if (ifp == NULL)
173808b68b0eSGleb Smirnoff 		return (ENXIO);
173908b68b0eSGleb Smirnoff 
174008b68b0eSGleb Smirnoff 	switch (ifp->if_type) {
174108b68b0eSGleb Smirnoff 	case IFT_ETHER:
174208b68b0eSGleb Smirnoff 	case IFT_L2VLAN:
17439ca1fe0dSGleb Smirnoff 	case IFT_BRIDGE:
174408b68b0eSGleb Smirnoff 		break;
174508b68b0eSGleb Smirnoff 	default:
174608b68b0eSGleb Smirnoff 		error = EOPNOTSUPP;
174708b68b0eSGleb Smirnoff 		goto out;
174808b68b0eSGleb Smirnoff 	}
174908b68b0eSGleb Smirnoff 
175008b68b0eSGleb Smirnoff 	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
175108b68b0eSGleb Smirnoff 		error = EADDRNOTAVAIL;
175208b68b0eSGleb Smirnoff 		goto out;
175308b68b0eSGleb Smirnoff 	}
175408b68b0eSGleb Smirnoff 
175593d4534cSGleb Smirnoff 	sx_xlock(&carp_sx);
175608b68b0eSGleb Smirnoff 	switch (cmd) {
175708b68b0eSGleb Smirnoff 	case SIOCSVH:
175808b68b0eSGleb Smirnoff 		if ((error = priv_check(td, PRIV_NETINET_CARP)))
175908b68b0eSGleb Smirnoff 			break;
176008b68b0eSGleb Smirnoff 		if (carpr.carpr_vhid <= 0 || carpr.carpr_vhid > CARP_MAXVHID ||
176108b68b0eSGleb Smirnoff 		    carpr.carpr_advbase < 0 || carpr.carpr_advskew < 0) {
176208b68b0eSGleb Smirnoff 			error = EINVAL;
176308b68b0eSGleb Smirnoff 			break;
176408b68b0eSGleb Smirnoff 		}
176508b68b0eSGleb Smirnoff 
176608b68b0eSGleb Smirnoff 		if (ifp->if_carp) {
176708b68b0eSGleb Smirnoff 			IFNET_FOREACH_CARP(ifp, sc)
176808b68b0eSGleb Smirnoff 				if (sc->sc_vhid == carpr.carpr_vhid)
176908b68b0eSGleb Smirnoff 					break;
177008b68b0eSGleb Smirnoff 		}
177108b68b0eSGleb Smirnoff 		if (sc == NULL) {
177208b68b0eSGleb Smirnoff 			sc = carp_alloc(ifp);
177308b68b0eSGleb Smirnoff 			CARP_LOCK(sc);
177408b68b0eSGleb Smirnoff 			sc->sc_vhid = carpr.carpr_vhid;
177508b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[0] = 0;
177608b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[1] = 0;
177708b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[2] = 0x5e;
177808b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[3] = 0;
177908b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[4] = 1;
178008b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[5] = sc->sc_vhid;
178108b68b0eSGleb Smirnoff 		} else
178208b68b0eSGleb Smirnoff 			CARP_LOCK(sc);
178308b68b0eSGleb Smirnoff 		locked = 1;
178408b68b0eSGleb Smirnoff 		if (carpr.carpr_advbase > 0) {
178508b68b0eSGleb Smirnoff 			if (carpr.carpr_advbase > 255 ||
178608b68b0eSGleb Smirnoff 			    carpr.carpr_advbase < CARP_DFLTINTV) {
178708b68b0eSGleb Smirnoff 				error = EINVAL;
178808b68b0eSGleb Smirnoff 				break;
178908b68b0eSGleb Smirnoff 			}
179008b68b0eSGleb Smirnoff 			sc->sc_advbase = carpr.carpr_advbase;
179108b68b0eSGleb Smirnoff 		}
179208b68b0eSGleb Smirnoff 		if (carpr.carpr_advskew >= 255) {
179308b68b0eSGleb Smirnoff 			error = EINVAL;
179408b68b0eSGleb Smirnoff 			break;
179508b68b0eSGleb Smirnoff 		}
179608b68b0eSGleb Smirnoff 		sc->sc_advskew = carpr.carpr_advskew;
179708b68b0eSGleb Smirnoff 		if (carpr.carpr_key[0] != '\0') {
179808b68b0eSGleb Smirnoff 			bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
179908b68b0eSGleb Smirnoff 			carp_hmac_prepare(sc);
180008b68b0eSGleb Smirnoff 		}
180108b68b0eSGleb Smirnoff 		if (sc->sc_state != INIT &&
180208b68b0eSGleb Smirnoff 		    carpr.carpr_state != sc->sc_state) {
180308b68b0eSGleb Smirnoff 			switch (carpr.carpr_state) {
180408b68b0eSGleb Smirnoff 			case BACKUP:
180508b68b0eSGleb Smirnoff 				callout_stop(&sc->sc_ad_tmo);
1806369a6708SWill Andrews 				carp_set_state(sc, BACKUP,
1807369a6708SWill Andrews 				    "user requested via ifconfig");
180808b68b0eSGleb Smirnoff 				carp_setrun(sc, 0);
180908b68b0eSGleb Smirnoff 				carp_delroute(sc);
181008b68b0eSGleb Smirnoff 				break;
181108b68b0eSGleb Smirnoff 			case MASTER:
1812369a6708SWill Andrews 				carp_master_down_locked(sc,
1813369a6708SWill Andrews 				    "user requested via ifconfig");
181408b68b0eSGleb Smirnoff 				break;
181508b68b0eSGleb Smirnoff 			default:
181608b68b0eSGleb Smirnoff 				break;
181708b68b0eSGleb Smirnoff 			}
181808b68b0eSGleb Smirnoff 		}
181908b68b0eSGleb Smirnoff 		break;
182008b68b0eSGleb Smirnoff 
182108b68b0eSGleb Smirnoff 	case SIOCGVH:
182208b68b0eSGleb Smirnoff 	    {
182308b68b0eSGleb Smirnoff 		int priveleged;
182408b68b0eSGleb Smirnoff 
182508b68b0eSGleb Smirnoff 		if (carpr.carpr_vhid < 0 || carpr.carpr_vhid > CARP_MAXVHID) {
182608b68b0eSGleb Smirnoff 			error = EINVAL;
182708b68b0eSGleb Smirnoff 			break;
182808b68b0eSGleb Smirnoff 		}
182908b68b0eSGleb Smirnoff 		if (carpr.carpr_count < 1) {
183008b68b0eSGleb Smirnoff 			error = EMSGSIZE;
183108b68b0eSGleb Smirnoff 			break;
183208b68b0eSGleb Smirnoff 		}
183308b68b0eSGleb Smirnoff 		if (ifp->if_carp == NULL) {
183408b68b0eSGleb Smirnoff 			error = ENOENT;
183508b68b0eSGleb Smirnoff 			break;
183608b68b0eSGleb Smirnoff 		}
183708b68b0eSGleb Smirnoff 
183808b68b0eSGleb Smirnoff 		priveleged = (priv_check(td, PRIV_NETINET_CARP) == 0);
183908b68b0eSGleb Smirnoff 		if (carpr.carpr_vhid != 0) {
184008b68b0eSGleb Smirnoff 			IFNET_FOREACH_CARP(ifp, sc)
184108b68b0eSGleb Smirnoff 				if (sc->sc_vhid == carpr.carpr_vhid)
184208b68b0eSGleb Smirnoff 					break;
184308b68b0eSGleb Smirnoff 			if (sc == NULL) {
184408b68b0eSGleb Smirnoff 				error = ENOENT;
184508b68b0eSGleb Smirnoff 				break;
184608b68b0eSGleb Smirnoff 			}
184708b68b0eSGleb Smirnoff 			carp_carprcp(&carpr, sc, priveleged);
1848541d96aaSBrooks Davis 			error = copyout(&carpr, ifr_data_get_ptr(ifr),
1849541d96aaSBrooks Davis 			    sizeof(carpr));
185008b68b0eSGleb Smirnoff 		} else  {
185108b68b0eSGleb Smirnoff 			int i, count;
185208b68b0eSGleb Smirnoff 
185308b68b0eSGleb Smirnoff 			count = 0;
185408b68b0eSGleb Smirnoff 			IFNET_FOREACH_CARP(ifp, sc)
185508b68b0eSGleb Smirnoff 				count++;
185608b68b0eSGleb Smirnoff 
185708b68b0eSGleb Smirnoff 			if (count > carpr.carpr_count) {
185808b68b0eSGleb Smirnoff 				CIF_UNLOCK(ifp->if_carp);
185908b68b0eSGleb Smirnoff 				error = EMSGSIZE;
186008b68b0eSGleb Smirnoff 				break;
186108b68b0eSGleb Smirnoff 			}
186208b68b0eSGleb Smirnoff 
186308b68b0eSGleb Smirnoff 			i = 0;
186408b68b0eSGleb Smirnoff 			IFNET_FOREACH_CARP(ifp, sc) {
186508b68b0eSGleb Smirnoff 				carp_carprcp(&carpr, sc, priveleged);
186608b68b0eSGleb Smirnoff 				carpr.carpr_count = count;
1867541d96aaSBrooks Davis 				error = copyout(&carpr,
1868dad68fc3SBjoern A. Zeeb 				    (char *)ifr_data_get_ptr(ifr) +
186908b68b0eSGleb Smirnoff 				    (i * sizeof(carpr)), sizeof(carpr));
187008b68b0eSGleb Smirnoff 				if (error) {
187108b68b0eSGleb Smirnoff 					CIF_UNLOCK(ifp->if_carp);
187208b68b0eSGleb Smirnoff 					break;
187308b68b0eSGleb Smirnoff 				}
187408b68b0eSGleb Smirnoff 				i++;
187508b68b0eSGleb Smirnoff 			}
187608b68b0eSGleb Smirnoff 		}
187708b68b0eSGleb Smirnoff 		break;
187808b68b0eSGleb Smirnoff 	    }
187908b68b0eSGleb Smirnoff 	default:
188008b68b0eSGleb Smirnoff 		error = EINVAL;
188108b68b0eSGleb Smirnoff 	}
188293d4534cSGleb Smirnoff 	sx_xunlock(&carp_sx);
188308b68b0eSGleb Smirnoff 
188408b68b0eSGleb Smirnoff out:
188508b68b0eSGleb Smirnoff 	if (locked)
188608b68b0eSGleb Smirnoff 		CARP_UNLOCK(sc);
188708b68b0eSGleb Smirnoff 	if_rele(ifp);
188808b68b0eSGleb Smirnoff 
188908b68b0eSGleb Smirnoff 	return (error);
189008b68b0eSGleb Smirnoff }
189108b68b0eSGleb Smirnoff 
189208b68b0eSGleb Smirnoff static int
189308b68b0eSGleb Smirnoff carp_get_vhid(struct ifaddr *ifa)
189408b68b0eSGleb Smirnoff {
189508b68b0eSGleb Smirnoff 
189608b68b0eSGleb Smirnoff 	if (ifa == NULL || ifa->ifa_carp == NULL)
189708b68b0eSGleb Smirnoff 		return (0);
189808b68b0eSGleb Smirnoff 
189908b68b0eSGleb Smirnoff 	return (ifa->ifa_carp->sc_vhid);
190008b68b0eSGleb Smirnoff }
190108b68b0eSGleb Smirnoff 
190208b68b0eSGleb Smirnoff int
190308b68b0eSGleb Smirnoff carp_attach(struct ifaddr *ifa, int vhid)
190408b68b0eSGleb Smirnoff {
190508b68b0eSGleb Smirnoff 	struct ifnet *ifp = ifa->ifa_ifp;
1906a9a2c40cSGleb Smirnoff 	struct carp_if *cif = ifp->if_carp;
190708b68b0eSGleb Smirnoff 	struct carp_softc *sc;
190808b68b0eSGleb Smirnoff 	int index, error;
190908b68b0eSGleb Smirnoff 
19109c2cd1aaSGleb Smirnoff 	KASSERT(ifa->ifa_carp == NULL, ("%s: ifa %p attached", __func__, ifa));
191108b68b0eSGleb Smirnoff 
191208b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
191308b68b0eSGleb Smirnoff #ifdef INET
191408b68b0eSGleb Smirnoff 	case AF_INET:
191508b68b0eSGleb Smirnoff #endif
191608b68b0eSGleb Smirnoff #ifdef INET6
191708b68b0eSGleb Smirnoff 	case AF_INET6:
191808b68b0eSGleb Smirnoff #endif
191908b68b0eSGleb Smirnoff 		break;
192008b68b0eSGleb Smirnoff 	default:
192108b68b0eSGleb Smirnoff 		return (EPROTOTYPE);
192208b68b0eSGleb Smirnoff 	}
192308b68b0eSGleb Smirnoff 
19249c2cd1aaSGleb Smirnoff 	sx_xlock(&carp_sx);
19259c2cd1aaSGleb Smirnoff 	if (ifp->if_carp == NULL) {
19269c2cd1aaSGleb Smirnoff 		sx_xunlock(&carp_sx);
19279c2cd1aaSGleb Smirnoff 		return (ENOPROTOOPT);
19289c2cd1aaSGleb Smirnoff 	}
19299c2cd1aaSGleb Smirnoff 
193008b68b0eSGleb Smirnoff 	IFNET_FOREACH_CARP(ifp, sc)
193108b68b0eSGleb Smirnoff 		if (sc->sc_vhid == vhid)
193208b68b0eSGleb Smirnoff 			break;
1933a9a2c40cSGleb Smirnoff 	if (sc == NULL) {
19349c2cd1aaSGleb Smirnoff 		sx_xunlock(&carp_sx);
193508b68b0eSGleb Smirnoff 		return (ENOENT);
1936a9a2c40cSGleb Smirnoff 	}
193708b68b0eSGleb Smirnoff 
1938a9a2c40cSGleb Smirnoff 	error = carp_multicast_setup(cif, ifa->ifa_addr->sa_family);
1939a9a2c40cSGleb Smirnoff 	if (error) {
1940a9a2c40cSGleb Smirnoff 		CIF_FREE(cif);
19419c2cd1aaSGleb Smirnoff 		sx_xunlock(&carp_sx);
194208b68b0eSGleb Smirnoff 		return (error);
1943a9a2c40cSGleb Smirnoff 	}
194408b68b0eSGleb Smirnoff 
194508b68b0eSGleb Smirnoff 	index = sc->sc_naddrs + sc->sc_naddrs6 + 1;
194608b68b0eSGleb Smirnoff 	if (index > sc->sc_ifasiz / sizeof(struct ifaddr *))
19479c2cd1aaSGleb Smirnoff 		carp_grow_ifas(sc);
194808b68b0eSGleb Smirnoff 
194908b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
195008b68b0eSGleb Smirnoff #ifdef INET
195108b68b0eSGleb Smirnoff 	case AF_INET:
1952a9a2c40cSGleb Smirnoff 		cif->cif_naddrs++;
195308b68b0eSGleb Smirnoff 		sc->sc_naddrs++;
195408b68b0eSGleb Smirnoff 		break;
195508b68b0eSGleb Smirnoff #endif
195608b68b0eSGleb Smirnoff #ifdef INET6
195708b68b0eSGleb Smirnoff 	case AF_INET6:
1958a9a2c40cSGleb Smirnoff 		cif->cif_naddrs6++;
195908b68b0eSGleb Smirnoff 		sc->sc_naddrs6++;
196008b68b0eSGleb Smirnoff 		break;
196108b68b0eSGleb Smirnoff #endif
196208b68b0eSGleb Smirnoff 	}
196308b68b0eSGleb Smirnoff 
196408b68b0eSGleb Smirnoff 	ifa_ref(ifa);
19659c2cd1aaSGleb Smirnoff 
19669c2cd1aaSGleb Smirnoff 	CARP_LOCK(sc);
196708b68b0eSGleb Smirnoff 	sc->sc_ifas[index - 1] = ifa;
196808b68b0eSGleb Smirnoff 	ifa->ifa_carp = sc;
196908b68b0eSGleb Smirnoff 	carp_hmac_prepare(sc);
197008b68b0eSGleb Smirnoff 	carp_sc_state(sc);
197108b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
19729c2cd1aaSGleb Smirnoff 
19739c2cd1aaSGleb Smirnoff 	sx_xunlock(&carp_sx);
197408b68b0eSGleb Smirnoff 
197508b68b0eSGleb Smirnoff 	return (0);
197608b68b0eSGleb Smirnoff }
197708b68b0eSGleb Smirnoff 
197808b68b0eSGleb Smirnoff void
1979338e227aSLuiz Otavio O Souza carp_detach(struct ifaddr *ifa, bool keep_cif)
198008b68b0eSGleb Smirnoff {
1981a9a2c40cSGleb Smirnoff 	struct ifnet *ifp = ifa->ifa_ifp;
1982a9a2c40cSGleb Smirnoff 	struct carp_if *cif = ifp->if_carp;
198308b68b0eSGleb Smirnoff 	struct carp_softc *sc = ifa->ifa_carp;
198408b68b0eSGleb Smirnoff 	int i, index;
198508b68b0eSGleb Smirnoff 
198608b68b0eSGleb Smirnoff 	KASSERT(sc != NULL, ("%s: %p not attached", __func__, ifa));
198708b68b0eSGleb Smirnoff 
19889c2cd1aaSGleb Smirnoff 	sx_xlock(&carp_sx);
198908b68b0eSGleb Smirnoff 
19909c2cd1aaSGleb Smirnoff 	CARP_LOCK(sc);
199108b68b0eSGleb Smirnoff 	/* Shift array. */
199208b68b0eSGleb Smirnoff 	index = sc->sc_naddrs + sc->sc_naddrs6;
199308b68b0eSGleb Smirnoff 	for (i = 0; i < index; i++)
199408b68b0eSGleb Smirnoff 		if (sc->sc_ifas[i] == ifa)
199508b68b0eSGleb Smirnoff 			break;
199608b68b0eSGleb Smirnoff 	KASSERT(i < index, ("%s: %p no backref", __func__, ifa));
199708b68b0eSGleb Smirnoff 	for (; i < index - 1; i++)
199808b68b0eSGleb Smirnoff 		sc->sc_ifas[i] = sc->sc_ifas[i+1];
199908b68b0eSGleb Smirnoff 	sc->sc_ifas[index - 1] = NULL;
200008b68b0eSGleb Smirnoff 
200108b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
200208b68b0eSGleb Smirnoff #ifdef INET
200308b68b0eSGleb Smirnoff 	case AF_INET:
2004a9a2c40cSGleb Smirnoff 		cif->cif_naddrs--;
200508b68b0eSGleb Smirnoff 		sc->sc_naddrs--;
200608b68b0eSGleb Smirnoff 		break;
200708b68b0eSGleb Smirnoff #endif
200808b68b0eSGleb Smirnoff #ifdef INET6
200908b68b0eSGleb Smirnoff 	case AF_INET6:
2010a9a2c40cSGleb Smirnoff 		cif->cif_naddrs6--;
201108b68b0eSGleb Smirnoff 		sc->sc_naddrs6--;
201208b68b0eSGleb Smirnoff 		break;
201308b68b0eSGleb Smirnoff #endif
201408b68b0eSGleb Smirnoff 	}
201508b68b0eSGleb Smirnoff 
20162512b096SGleb Smirnoff 	carp_ifa_delroute(ifa);
2017a9a2c40cSGleb Smirnoff 	carp_multicast_cleanup(cif, ifa->ifa_addr->sa_family);
201808b68b0eSGleb Smirnoff 
201908b68b0eSGleb Smirnoff 	ifa->ifa_carp = NULL;
202008b68b0eSGleb Smirnoff 	ifa_free(ifa);
202108b68b0eSGleb Smirnoff 
202208b68b0eSGleb Smirnoff 	carp_hmac_prepare(sc);
202308b68b0eSGleb Smirnoff 	carp_sc_state(sc);
202408b68b0eSGleb Smirnoff 
2025338e227aSLuiz Otavio O Souza 	if (!keep_cif && sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0)
202608b68b0eSGleb Smirnoff 		carp_destroy(sc);
20279c2cd1aaSGleb Smirnoff 	else
202808b68b0eSGleb Smirnoff 		CARP_UNLOCK(sc);
20299c2cd1aaSGleb Smirnoff 
2030338e227aSLuiz Otavio O Souza 	if (!keep_cif)
20319c2cd1aaSGleb Smirnoff 		CIF_FREE(cif);
20329c2cd1aaSGleb Smirnoff 
20339c2cd1aaSGleb Smirnoff 	sx_xunlock(&carp_sx);
203408b68b0eSGleb Smirnoff }
203508b68b0eSGleb Smirnoff 
203608b68b0eSGleb Smirnoff static void
2037d01641e2SWill Andrews carp_set_state(struct carp_softc *sc, int state, const char *reason)
203808b68b0eSGleb Smirnoff {
203908b68b0eSGleb Smirnoff 
204008b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
204108b68b0eSGleb Smirnoff 
204208b68b0eSGleb Smirnoff 	if (sc->sc_state != state) {
204308b68b0eSGleb Smirnoff 		const char *carp_states[] = { CARP_STATES };
204408b68b0eSGleb Smirnoff 		char subsys[IFNAMSIZ+5];
204508b68b0eSGleb Smirnoff 
204608b68b0eSGleb Smirnoff 		snprintf(subsys, IFNAMSIZ+5, "%u@%s", sc->sc_vhid,
204708b68b0eSGleb Smirnoff 		    sc->sc_carpdev->if_xname);
2048d01641e2SWill Andrews 
2049d01641e2SWill Andrews 		CARP_LOG("%s: %s -> %s (%s)\n", subsys,
2050d01641e2SWill Andrews 		    carp_states[sc->sc_state], carp_states[state], reason);
2051d01641e2SWill Andrews 
2052d01641e2SWill Andrews 		sc->sc_state = state;
2053d01641e2SWill Andrews 
205408b68b0eSGleb Smirnoff 		devctl_notify("CARP", subsys, carp_states[state], NULL);
205508b68b0eSGleb Smirnoff 	}
205608b68b0eSGleb Smirnoff }
205708b68b0eSGleb Smirnoff 
205808b68b0eSGleb Smirnoff static void
205908b68b0eSGleb Smirnoff carp_linkstate(struct ifnet *ifp)
2060d220759bSGleb Smirnoff {
2061d220759bSGleb Smirnoff 	struct carp_softc *sc;
2062d220759bSGleb Smirnoff 
206308b68b0eSGleb Smirnoff 	CIF_LOCK(ifp->if_carp);
206408b68b0eSGleb Smirnoff 	IFNET_FOREACH_CARP(ifp, sc) {
206508b68b0eSGleb Smirnoff 		CARP_LOCK(sc);
206608b68b0eSGleb Smirnoff 		carp_sc_state(sc);
206708b68b0eSGleb Smirnoff 		CARP_UNLOCK(sc);
206808b68b0eSGleb Smirnoff 	}
206908b68b0eSGleb Smirnoff 	CIF_UNLOCK(ifp->if_carp);
20704cb39345SGleb Smirnoff }
20714cb39345SGleb Smirnoff 
20724cb39345SGleb Smirnoff static void
207308b68b0eSGleb Smirnoff carp_sc_state(struct carp_softc *sc)
20744cb39345SGleb Smirnoff {
207508b68b0eSGleb Smirnoff 
207608b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
20774cb39345SGleb Smirnoff 
2078e8c34a71SGleb Smirnoff 	if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
2079167a3440SAlexander Motin 	    !(sc->sc_carpdev->if_flags & IFF_UP) ||
2080167a3440SAlexander Motin 	    !V_carp_allow) {
2081a9771948SGleb Smirnoff 		callout_stop(&sc->sc_ad_tmo);
208208b68b0eSGleb Smirnoff #ifdef INET
2083a9771948SGleb Smirnoff 		callout_stop(&sc->sc_md_tmo);
208408b68b0eSGleb Smirnoff #endif
208508b68b0eSGleb Smirnoff #ifdef INET6
2086a9771948SGleb Smirnoff 		callout_stop(&sc->sc_md6_tmo);
208708b68b0eSGleb Smirnoff #endif
2088bb269f3aSWill Andrews 		carp_set_state(sc, INIT, "hardware interface down");
2089a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
2090f08535f8SGleb Smirnoff 		if (!sc->sc_suppress)
2091c5c392e7SMikolaj Golub 			carp_demote_adj(V_carp_ifdown_adj, "interface down");
2092a9771948SGleb Smirnoff 		sc->sc_suppress = 1;
2093a9771948SGleb Smirnoff 	} else {
2094bb269f3aSWill Andrews 		carp_set_state(sc, INIT, "hardware interface up");
2095a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
2096a9771948SGleb Smirnoff 		if (sc->sc_suppress)
2097c5c392e7SMikolaj Golub 			carp_demote_adj(-V_carp_ifdown_adj, "interface up");
2098a9771948SGleb Smirnoff 		sc->sc_suppress = 0;
2099a9771948SGleb Smirnoff 	}
2100a9771948SGleb Smirnoff }
2101a9771948SGleb Smirnoff 
2102f08535f8SGleb Smirnoff static void
2103f08535f8SGleb Smirnoff carp_demote_adj(int adj, char *reason)
2104f08535f8SGleb Smirnoff {
2105c5c392e7SMikolaj Golub 	atomic_add_int(&V_carp_demotion, adj);
2106c5c392e7SMikolaj Golub 	CARP_LOG("demoted by %d to %d (%s)\n", adj, V_carp_demotion, reason);
2107f08535f8SGleb Smirnoff 	taskqueue_enqueue(taskqueue_swi, &carp_sendall_task);
2108f08535f8SGleb Smirnoff }
210908b68b0eSGleb Smirnoff 
21107951008bSGleb Smirnoff static int
2111167a3440SAlexander Motin carp_allow_sysctl(SYSCTL_HANDLER_ARGS)
2112167a3440SAlexander Motin {
2113167a3440SAlexander Motin 	int new, error;
2114167a3440SAlexander Motin 	struct carp_softc *sc;
2115167a3440SAlexander Motin 
2116167a3440SAlexander Motin 	new = V_carp_allow;
2117167a3440SAlexander Motin 	error = sysctl_handle_int(oidp, &new, 0, req);
2118167a3440SAlexander Motin 	if (error || !req->newptr)
2119167a3440SAlexander Motin 		return (error);
2120167a3440SAlexander Motin 
2121167a3440SAlexander Motin 	if (V_carp_allow != new) {
2122167a3440SAlexander Motin 		V_carp_allow = new;
2123167a3440SAlexander Motin 
2124167a3440SAlexander Motin 		mtx_lock(&carp_mtx);
2125167a3440SAlexander Motin 		LIST_FOREACH(sc, &carp_list, sc_next) {
2126167a3440SAlexander Motin 			CARP_LOCK(sc);
2127167a3440SAlexander Motin 			if (curvnet == sc->sc_carpdev->if_vnet)
2128167a3440SAlexander Motin 				carp_sc_state(sc);
2129167a3440SAlexander Motin 			CARP_UNLOCK(sc);
2130167a3440SAlexander Motin 		}
2131167a3440SAlexander Motin 		mtx_unlock(&carp_mtx);
2132167a3440SAlexander Motin 	}
2133167a3440SAlexander Motin 
2134167a3440SAlexander Motin 	return (0);
2135167a3440SAlexander Motin }
2136167a3440SAlexander Motin 
2137167a3440SAlexander Motin static int
21380d3d234cSKristof Provost carp_dscp_sysctl(SYSCTL_HANDLER_ARGS)
21390d3d234cSKristof Provost {
21400d3d234cSKristof Provost 	int new, error;
21410d3d234cSKristof Provost 
21420d3d234cSKristof Provost 	new = V_carp_dscp;
21430d3d234cSKristof Provost 	error = sysctl_handle_int(oidp, &new, 0, req);
21440d3d234cSKristof Provost 	if (error || !req->newptr)
21450d3d234cSKristof Provost 		return (error);
21460d3d234cSKristof Provost 
21470d3d234cSKristof Provost 	if (new < 0 || new > 63)
21480d3d234cSKristof Provost 		return (EINVAL);
21490d3d234cSKristof Provost 
21500d3d234cSKristof Provost 	V_carp_dscp = new;
21510d3d234cSKristof Provost 
21520d3d234cSKristof Provost 	return (0);
21530d3d234cSKristof Provost }
21540d3d234cSKristof Provost 
21550d3d234cSKristof Provost static int
21567951008bSGleb Smirnoff carp_demote_adj_sysctl(SYSCTL_HANDLER_ARGS)
21577951008bSGleb Smirnoff {
21587951008bSGleb Smirnoff 	int new, error;
21597951008bSGleb Smirnoff 
2160c5c392e7SMikolaj Golub 	new = V_carp_demotion;
21617951008bSGleb Smirnoff 	error = sysctl_handle_int(oidp, &new, 0, req);
21627951008bSGleb Smirnoff 	if (error || !req->newptr)
21637951008bSGleb Smirnoff 		return (error);
21647951008bSGleb Smirnoff 
21657951008bSGleb Smirnoff 	carp_demote_adj(new, "sysctl");
21667951008bSGleb Smirnoff 
21677951008bSGleb Smirnoff 	return (0);
21687951008bSGleb Smirnoff }
21697951008bSGleb Smirnoff 
217054bfbd51SWill Andrews #ifdef INET
217154bfbd51SWill Andrews extern  struct domain inetdomain;
217254bfbd51SWill Andrews static struct protosw in_carp_protosw = {
217354bfbd51SWill Andrews 	.pr_type =		SOCK_RAW,
217454bfbd51SWill Andrews 	.pr_domain =		&inetdomain,
217554bfbd51SWill Andrews 	.pr_protocol =		IPPROTO_CARP,
217654bfbd51SWill Andrews 	.pr_flags =		PR_ATOMIC|PR_ADDR,
217754bfbd51SWill Andrews 	.pr_input =		carp_input,
217873d76e77SKevin Lo 	.pr_output =		rip_output,
217954bfbd51SWill Andrews 	.pr_ctloutput =		rip_ctloutput,
218054bfbd51SWill Andrews 	.pr_usrreqs =		&rip_usrreqs
218154bfbd51SWill Andrews };
218254bfbd51SWill Andrews #endif
218354bfbd51SWill Andrews 
218454bfbd51SWill Andrews #ifdef INET6
218554bfbd51SWill Andrews extern	struct domain inet6domain;
21868f5a8818SKevin Lo static struct protosw in6_carp_protosw = {
218754bfbd51SWill Andrews 	.pr_type =		SOCK_RAW,
218854bfbd51SWill Andrews 	.pr_domain =		&inet6domain,
218954bfbd51SWill Andrews 	.pr_protocol =		IPPROTO_CARP,
219054bfbd51SWill Andrews 	.pr_flags =		PR_ATOMIC|PR_ADDR,
219154bfbd51SWill Andrews 	.pr_input =		carp6_input,
219273d76e77SKevin Lo 	.pr_output =		rip6_output,
219354bfbd51SWill Andrews 	.pr_ctloutput =		rip6_ctloutput,
219454bfbd51SWill Andrews 	.pr_usrreqs =		&rip6_usrreqs
219554bfbd51SWill Andrews };
219654bfbd51SWill Andrews #endif
219754bfbd51SWill Andrews 
219854bfbd51SWill Andrews static void
219954bfbd51SWill Andrews carp_mod_cleanup(void)
2200a9771948SGleb Smirnoff {
220154bfbd51SWill Andrews 
220254bfbd51SWill Andrews #ifdef INET
220354bfbd51SWill Andrews 	if (proto_reg[CARP_INET] == 0) {
220415249f73SWill Andrews 		(void)ipproto_unregister(IPPROTO_CARP);
220554bfbd51SWill Andrews 		pf_proto_unregister(PF_INET, IPPROTO_CARP, SOCK_RAW);
220654bfbd51SWill Andrews 		proto_reg[CARP_INET] = -1;
220754bfbd51SWill Andrews 	}
220854bfbd51SWill Andrews 	carp_iamatch_p = NULL;
220954bfbd51SWill Andrews #endif
221054bfbd51SWill Andrews #ifdef INET6
221154bfbd51SWill Andrews 	if (proto_reg[CARP_INET6] == 0) {
221215249f73SWill Andrews 		(void)ip6proto_unregister(IPPROTO_CARP);
221354bfbd51SWill Andrews 		pf_proto_unregister(PF_INET6, IPPROTO_CARP, SOCK_RAW);
221454bfbd51SWill Andrews 		proto_reg[CARP_INET6] = -1;
221554bfbd51SWill Andrews 	}
221654bfbd51SWill Andrews 	carp_iamatch6_p = NULL;
221754bfbd51SWill Andrews 	carp_macmatch6_p = NULL;
221854bfbd51SWill Andrews #endif
221908b68b0eSGleb Smirnoff 	carp_ioctl_p = NULL;
222008b68b0eSGleb Smirnoff 	carp_attach_p = NULL;
222108b68b0eSGleb Smirnoff 	carp_detach_p = NULL;
222208b68b0eSGleb Smirnoff 	carp_get_vhid_p = NULL;
222354bfbd51SWill Andrews 	carp_linkstate_p = NULL;
222454bfbd51SWill Andrews 	carp_forus_p = NULL;
222554bfbd51SWill Andrews 	carp_output_p = NULL;
2226f08535f8SGleb Smirnoff 	carp_demote_adj_p = NULL;
222724421c1cSGleb Smirnoff 	carp_master_p = NULL;
2228f08535f8SGleb Smirnoff 	mtx_unlock(&carp_mtx);
2229f08535f8SGleb Smirnoff 	taskqueue_drain(taskqueue_swi, &carp_sendall_task);
223054bfbd51SWill Andrews 	mtx_destroy(&carp_mtx);
223193d4534cSGleb Smirnoff 	sx_destroy(&carp_sx);
223254bfbd51SWill Andrews }
223354bfbd51SWill Andrews 
223454bfbd51SWill Andrews static int
223554bfbd51SWill Andrews carp_mod_load(void)
223654bfbd51SWill Andrews {
223715249f73SWill Andrews 	int err;
223854bfbd51SWill Andrews 
2239d92d54d5SGleb Smirnoff 	mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
224093d4534cSGleb Smirnoff 	sx_init(&carp_sx, "carp_sx");
224108b68b0eSGleb Smirnoff 	LIST_INIT(&carp_list);
224208b68b0eSGleb Smirnoff 	carp_get_vhid_p = carp_get_vhid;
224354bfbd51SWill Andrews 	carp_forus_p = carp_forus;
224454bfbd51SWill Andrews 	carp_output_p = carp_output;
224508b68b0eSGleb Smirnoff 	carp_linkstate_p = carp_linkstate;
224608b68b0eSGleb Smirnoff 	carp_ioctl_p = carp_ioctl;
224708b68b0eSGleb Smirnoff 	carp_attach_p = carp_attach;
224808b68b0eSGleb Smirnoff 	carp_detach_p = carp_detach;
2249f08535f8SGleb Smirnoff 	carp_demote_adj_p = carp_demote_adj;
225024421c1cSGleb Smirnoff 	carp_master_p = carp_master;
225154bfbd51SWill Andrews #ifdef INET6
225254bfbd51SWill Andrews 	carp_iamatch6_p = carp_iamatch6;
225354bfbd51SWill Andrews 	carp_macmatch6_p = carp_macmatch6;
225454bfbd51SWill Andrews 	proto_reg[CARP_INET6] = pf_proto_register(PF_INET6,
225554bfbd51SWill Andrews 	    (struct protosw *)&in6_carp_protosw);
225608b68b0eSGleb Smirnoff 	if (proto_reg[CARP_INET6]) {
225754bfbd51SWill Andrews 		printf("carp: error %d attaching to PF_INET6\n",
225854bfbd51SWill Andrews 		    proto_reg[CARP_INET6]);
225954bfbd51SWill Andrews 		carp_mod_cleanup();
22606baf7a24SGleb Smirnoff 		return (proto_reg[CARP_INET6]);
226154bfbd51SWill Andrews 	}
226215249f73SWill Andrews 	err = ip6proto_register(IPPROTO_CARP);
226315249f73SWill Andrews 	if (err) {
226415249f73SWill Andrews 		printf("carp: error %d registering with INET6\n", err);
226515249f73SWill Andrews 		carp_mod_cleanup();
22666baf7a24SGleb Smirnoff 		return (err);
226715249f73SWill Andrews 	}
226854bfbd51SWill Andrews #endif
226954bfbd51SWill Andrews #ifdef INET
227054bfbd51SWill Andrews 	carp_iamatch_p = carp_iamatch;
227154bfbd51SWill Andrews 	proto_reg[CARP_INET] = pf_proto_register(PF_INET, &in_carp_protosw);
227208b68b0eSGleb Smirnoff 	if (proto_reg[CARP_INET]) {
227354bfbd51SWill Andrews 		printf("carp: error %d attaching to PF_INET\n",
227454bfbd51SWill Andrews 		    proto_reg[CARP_INET]);
227554bfbd51SWill Andrews 		carp_mod_cleanup();
22766baf7a24SGleb Smirnoff 		return (proto_reg[CARP_INET]);
227754bfbd51SWill Andrews 	}
227815249f73SWill Andrews 	err = ipproto_register(IPPROTO_CARP);
227915249f73SWill Andrews 	if (err) {
228015249f73SWill Andrews 		printf("carp: error %d registering with INET\n", err);
228115249f73SWill Andrews 		carp_mod_cleanup();
22826baf7a24SGleb Smirnoff 		return (err);
228315249f73SWill Andrews 	}
228454bfbd51SWill Andrews #endif
228508b68b0eSGleb Smirnoff 	return (0);
228654bfbd51SWill Andrews }
2287a9771948SGleb Smirnoff 
228854bfbd51SWill Andrews static int
228954bfbd51SWill Andrews carp_modevent(module_t mod, int type, void *data)
229054bfbd51SWill Andrews {
229154bfbd51SWill Andrews 	switch (type) {
229254bfbd51SWill Andrews 	case MOD_LOAD:
229354bfbd51SWill Andrews 		return carp_mod_load();
229454bfbd51SWill Andrews 		/* NOTREACHED */
2295a9771948SGleb Smirnoff 	case MOD_UNLOAD:
229608b68b0eSGleb Smirnoff 		mtx_lock(&carp_mtx);
229708b68b0eSGleb Smirnoff 		if (LIST_EMPTY(&carp_list))
229854bfbd51SWill Andrews 			carp_mod_cleanup();
229908b68b0eSGleb Smirnoff 		else {
230008b68b0eSGleb Smirnoff 			mtx_unlock(&carp_mtx);
230154bfbd51SWill Andrews 			return (EBUSY);
230208b68b0eSGleb Smirnoff 		}
2303a9771948SGleb Smirnoff 		break;
2304a9771948SGleb Smirnoff 
2305a9771948SGleb Smirnoff 	default:
23060fa08018SGleb Smirnoff 		return (EINVAL);
2307a9771948SGleb Smirnoff 	}
2308a9771948SGleb Smirnoff 
23090fa08018SGleb Smirnoff 	return (0);
2310a9771948SGleb Smirnoff }
2311a9771948SGleb Smirnoff 
2312a9771948SGleb Smirnoff static moduledata_t carp_mod = {
2313a9771948SGleb Smirnoff 	"carp",
2314a9771948SGleb Smirnoff 	carp_modevent,
23159823d527SKevin Lo 	0
2316a9771948SGleb Smirnoff };
2317a9771948SGleb Smirnoff 
2318e24fa11dSWill Andrews DECLARE_MODULE(carp, carp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
2319