xref: /freebsd/sys/netinet/ip_carp.c (revision 08b68b0e4c6b132127919cfbaf7275c727ca7843)
1*08b68b0eSGleb Smirnoff /*-
2*08b68b0eSGleb Smirnoff  * Copyright (c) 2002 Michael Shalayeff.
3*08b68b0eSGleb Smirnoff  * Copyright (c) 2003 Ryan McBride.
4*08b68b0eSGleb Smirnoff  * Copyright (c) 2011 Gleb Smirnoff <glebius@FreeBSD.org>
5*08b68b0eSGleb Smirnoff  * All rights reserved.
6a9771948SGleb Smirnoff  *
7a9771948SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
8a9771948SGleb Smirnoff  * modification, are permitted provided that the following conditions
9a9771948SGleb Smirnoff  * are met:
10a9771948SGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
11a9771948SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
12a9771948SGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
13a9771948SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
14a9771948SGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
15a9771948SGleb Smirnoff  *
16a9771948SGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17a9771948SGleb Smirnoff  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18a9771948SGleb Smirnoff  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19a9771948SGleb Smirnoff  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
20a9771948SGleb Smirnoff  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21a9771948SGleb Smirnoff  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22a9771948SGleb Smirnoff  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23a9771948SGleb Smirnoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24a9771948SGleb Smirnoff  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25a9771948SGleb Smirnoff  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26a9771948SGleb Smirnoff  * THE POSSIBILITY OF SUCH DAMAGE.
27a9771948SGleb Smirnoff  */
28a9771948SGleb Smirnoff 
294b421e2dSMike Silbersack #include <sys/cdefs.h>
304b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
314b421e2dSMike Silbersack 
32a9771948SGleb Smirnoff #include "opt_bpf.h"
33a9771948SGleb Smirnoff #include "opt_inet.h"
34a9771948SGleb Smirnoff #include "opt_inet6.h"
35a9771948SGleb Smirnoff 
36a9771948SGleb Smirnoff #include <sys/param.h>
37a9771948SGleb Smirnoff #include <sys/systm.h>
38*08b68b0eSGleb Smirnoff #include <sys/bus.h>
39*08b68b0eSGleb Smirnoff #include <sys/jail.h>
40a9771948SGleb Smirnoff #include <sys/kernel.h>
41a9771948SGleb Smirnoff #include <sys/limits.h>
42a9771948SGleb Smirnoff #include <sys/malloc.h>
43a9771948SGleb Smirnoff #include <sys/mbuf.h>
44a9771948SGleb Smirnoff #include <sys/module.h>
45acd3428bSRobert Watson #include <sys/priv.h>
46a9771948SGleb Smirnoff #include <sys/proc.h>
4754bfbd51SWill Andrews #include <sys/protosw.h>
48*08b68b0eSGleb Smirnoff #include <sys/socket.h>
49*08b68b0eSGleb Smirnoff #include <sys/sockio.h>
50a9771948SGleb Smirnoff #include <sys/sysctl.h>
51a9771948SGleb Smirnoff #include <sys/syslog.h>
52a9771948SGleb Smirnoff 
53a9771948SGleb Smirnoff #include <net/ethernet.h>
54a9771948SGleb Smirnoff #include <net/fddi.h>
55a9771948SGleb Smirnoff #include <net/if.h>
56433aaf04SRuslan Ermilov #include <net/if_dl.h>
57*08b68b0eSGleb Smirnoff #include <net/if_llatbl.h>
58a9771948SGleb Smirnoff #include <net/if_types.h>
59*08b68b0eSGleb Smirnoff #include <net/iso88025.h>
60a9771948SGleb Smirnoff #include <net/route.h>
61530c0060SRobert Watson #include <net/vnet.h>
62a9771948SGleb Smirnoff 
63a0ae8f04SBjoern A. Zeeb #if defined(INET) || defined(INET6)
64a9771948SGleb Smirnoff #include <netinet/in.h>
65a9771948SGleb Smirnoff #include <netinet/in_var.h>
66a0ae8f04SBjoern A. Zeeb #include <netinet/ip_carp.h>
67a9771948SGleb Smirnoff #include <netinet/ip.h>
68a0ae8f04SBjoern A. Zeeb #include <machine/in_cksum.h>
69a0ae8f04SBjoern A. Zeeb #endif
70a0ae8f04SBjoern A. Zeeb #ifdef INET
71a9771948SGleb Smirnoff #include <netinet/ip_var.h>
72a9771948SGleb Smirnoff #include <netinet/if_ether.h>
73a9771948SGleb Smirnoff #endif
74a9771948SGleb Smirnoff 
75a9771948SGleb Smirnoff #ifdef INET6
76a9771948SGleb Smirnoff #include <netinet/icmp6.h>
77a9771948SGleb Smirnoff #include <netinet/ip6.h>
7854bfbd51SWill Andrews #include <netinet6/ip6protosw.h>
79*08b68b0eSGleb Smirnoff #include <netinet6/in6_var.h>
80a9771948SGleb Smirnoff #include <netinet6/ip6_var.h>
8129da8af6SHajimu UMEMOTO #include <netinet6/scope6_var.h>
82a9771948SGleb Smirnoff #include <netinet6/nd6.h>
83a9771948SGleb Smirnoff #endif
84a9771948SGleb Smirnoff 
85a9771948SGleb Smirnoff #include <crypto/sha1.h>
86a9771948SGleb Smirnoff 
87*08b68b0eSGleb Smirnoff static MALLOC_DEFINE(M_CARP, "CARP", "CARP addresses");
88a9771948SGleb Smirnoff 
89a9771948SGleb Smirnoff struct carp_softc {
90*08b68b0eSGleb Smirnoff 	struct ifnet		*sc_carpdev;	/* Pointer to parent ifnet. */
91*08b68b0eSGleb Smirnoff 	struct ifaddr		**sc_ifas;	/* Our ifaddrs. */
92*08b68b0eSGleb Smirnoff 	struct sockaddr_dl	sc_addr;	/* Our link level address. */
93*08b68b0eSGleb Smirnoff 	struct callout		sc_ad_tmo;	/* Advertising timeout. */
94a0ae8f04SBjoern A. Zeeb #ifdef INET
95*08b68b0eSGleb Smirnoff 	struct callout		sc_md_tmo;	/* Master down timeout. */
96a0ae8f04SBjoern A. Zeeb #endif
97a9771948SGleb Smirnoff #ifdef INET6
98*08b68b0eSGleb Smirnoff 	struct callout 		sc_md6_tmo;	/* XXX: Master down timeout. */
99*08b68b0eSGleb Smirnoff #endif
100*08b68b0eSGleb Smirnoff 	struct mtx		sc_mtx;
101a9771948SGleb Smirnoff 
102*08b68b0eSGleb Smirnoff 	int			sc_vhid;
103*08b68b0eSGleb Smirnoff 	int			sc_advskew;
104*08b68b0eSGleb Smirnoff 	int			sc_advbase;
105*08b68b0eSGleb Smirnoff 
106*08b68b0eSGleb Smirnoff 	int			sc_naddrs;
107*08b68b0eSGleb Smirnoff 	int			sc_naddrs6;
108*08b68b0eSGleb Smirnoff 	int			sc_ifasiz;
109a9771948SGleb Smirnoff 	enum { INIT = 0, BACKUP, MASTER }	sc_state;
110a9771948SGleb Smirnoff 	int			sc_suppress;
111a9771948SGleb Smirnoff 	int			sc_sendad_errors;
112a9771948SGleb Smirnoff #define	CARP_SENDAD_MAX_ERRORS	3
113a9771948SGleb Smirnoff 	int			sc_sendad_success;
114a9771948SGleb Smirnoff #define	CARP_SENDAD_MIN_SUCCESS 3
115a9771948SGleb Smirnoff 
116a9771948SGleb Smirnoff 	int			sc_init_counter;
117*08b68b0eSGleb Smirnoff 	uint64_t		sc_counter;
118a9771948SGleb Smirnoff 
119a9771948SGleb Smirnoff 	/* authentication */
120a9771948SGleb Smirnoff #define	CARP_HMAC_PAD	64
121a9771948SGleb Smirnoff 	unsigned char sc_key[CARP_KEY_LEN];
122a9771948SGleb Smirnoff 	unsigned char sc_pad[CARP_HMAC_PAD];
123a9771948SGleb Smirnoff 	SHA1_CTX sc_sha1;
124a9771948SGleb Smirnoff 
125*08b68b0eSGleb Smirnoff 	TAILQ_ENTRY(carp_softc)	sc_list;	/* On the carp_if list. */
126*08b68b0eSGleb Smirnoff 	LIST_ENTRY(carp_softc)	sc_next;	/* On the global list. */
127a9771948SGleb Smirnoff };
128*08b68b0eSGleb Smirnoff 
129*08b68b0eSGleb Smirnoff struct carp_if {
130*08b68b0eSGleb Smirnoff #ifdef INET
131*08b68b0eSGleb Smirnoff 	int	cif_naddrs;
132*08b68b0eSGleb Smirnoff #endif
133*08b68b0eSGleb Smirnoff #ifdef INET6
134*08b68b0eSGleb Smirnoff 	int	cif_naddrs6;
135*08b68b0eSGleb Smirnoff #endif
136*08b68b0eSGleb Smirnoff 	TAILQ_HEAD(, carp_softc) cif_vrs;
137*08b68b0eSGleb Smirnoff #ifdef INET
138*08b68b0eSGleb Smirnoff 	struct ip_moptions 	 cif_imo;
139*08b68b0eSGleb Smirnoff #endif
140*08b68b0eSGleb Smirnoff #ifdef INET6
141*08b68b0eSGleb Smirnoff 	struct ip6_moptions 	 cif_im6o;
142*08b68b0eSGleb Smirnoff #endif
143*08b68b0eSGleb Smirnoff 	struct ifnet	*cif_ifp;
144*08b68b0eSGleb Smirnoff 	struct mtx	cif_mtx;
145*08b68b0eSGleb Smirnoff };
146*08b68b0eSGleb Smirnoff 
147*08b68b0eSGleb Smirnoff #define	CARP_INET	0
148*08b68b0eSGleb Smirnoff #define	CARP_INET6	1
149*08b68b0eSGleb Smirnoff static int proto_reg[] = {-1, -1};
150*08b68b0eSGleb Smirnoff 
151*08b68b0eSGleb Smirnoff /*
152*08b68b0eSGleb Smirnoff  * Brief design of carp(4).
153*08b68b0eSGleb Smirnoff  *
154*08b68b0eSGleb Smirnoff  * Any carp-capable ifnet may have a list of carp softcs hanging off
155*08b68b0eSGleb Smirnoff  * its ifp->if_carp pointer. Each softc represents one unique virtual
156*08b68b0eSGleb Smirnoff  * host id, or vhid. The softc has a back pointer to the ifnet. All
157*08b68b0eSGleb Smirnoff  * softcs are joined in a global list, which has quite limited use.
158*08b68b0eSGleb Smirnoff  *
159*08b68b0eSGleb Smirnoff  * Any interface address that takes part in CARP negotiation has a
160*08b68b0eSGleb Smirnoff  * pointer to the softc of its vhid, ifa->ifa_carp. That could be either
161*08b68b0eSGleb Smirnoff  * AF_INET or AF_INET6 address.
162*08b68b0eSGleb Smirnoff  *
163*08b68b0eSGleb Smirnoff  * Although, one can get the softc's backpointer to ifnet and traverse
164*08b68b0eSGleb Smirnoff  * through its ifp->if_addrhead queue to find all interface addresses
165*08b68b0eSGleb Smirnoff  * involved in CARP, we keep a growable array of ifaddr pointers. This
166*08b68b0eSGleb Smirnoff  * allows us to avoid grabbing the IF_ADDR_LOCK() in many traversals that
167*08b68b0eSGleb Smirnoff  * do calls into the network stack, thus avoiding LORs.
168*08b68b0eSGleb Smirnoff  *
169*08b68b0eSGleb Smirnoff  * Locking:
170*08b68b0eSGleb Smirnoff  *
171*08b68b0eSGleb Smirnoff  * Each softc has a lock sc_mtx. It is used to synchronise carp_input_c(),
172*08b68b0eSGleb Smirnoff  * callout-driven events and ioctl()s.
173*08b68b0eSGleb Smirnoff  *
174*08b68b0eSGleb Smirnoff  * To traverse the list of softcs on an ifnet we use CIF_LOCK(), to
175*08b68b0eSGleb Smirnoff  * traverse the global list we use the mutex carp_mtx.
176*08b68b0eSGleb Smirnoff  *
177*08b68b0eSGleb Smirnoff  * Known issues with locking:
178*08b68b0eSGleb Smirnoff  *
179*08b68b0eSGleb Smirnoff  * - There is no protection for races between two ioctl() requests,
180*08b68b0eSGleb Smirnoff  *   neither SIOCSVH, nor SIOCAIFADDR & SIOCAIFADDR_IN6. I think that all
181*08b68b0eSGleb Smirnoff  *   interface ioctl()s should be serialized right in net/if.c.
182*08b68b0eSGleb Smirnoff  * - Sending ad, we put the pointer to the softc in an mtag, and no reference
183*08b68b0eSGleb Smirnoff  *   counting is done on the softc.
184*08b68b0eSGleb Smirnoff  * - On module unload we may race (?) with packet processing thread
185*08b68b0eSGleb Smirnoff  *   dereferencing our function pointers.
186*08b68b0eSGleb Smirnoff  */
187a9771948SGleb Smirnoff 
188a9771948SGleb Smirnoff int carp_suppress_preempt = 0;
189*08b68b0eSGleb Smirnoff int carp_opts[CARPCTL_MAXID] = { 0, 1, 0, 1, 0, };
19054bfbd51SWill Andrews SYSCTL_NODE(_net_inet, IPPROTO_CARP,	carp,	CTLFLAG_RW, 0,	"CARP");
191a9771948SGleb Smirnoff SYSCTL_INT(_net_inet_carp, CARPCTL_ALLOW, allow, CTLFLAG_RW,
192a9771948SGleb Smirnoff     &carp_opts[CARPCTL_ALLOW], 0, "Accept incoming CARP packets");
193a9771948SGleb Smirnoff SYSCTL_INT(_net_inet_carp, CARPCTL_PREEMPT, preempt, CTLFLAG_RW,
194a9771948SGleb Smirnoff     &carp_opts[CARPCTL_PREEMPT], 0, "high-priority backup preemption mode");
195a9771948SGleb Smirnoff SYSCTL_INT(_net_inet_carp, CARPCTL_LOG, log, CTLFLAG_RW,
196a9771948SGleb Smirnoff     &carp_opts[CARPCTL_LOG], 0, "log bad carp packets");
19732247f86SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, suppress_preempt, CTLFLAG_RD,
19832247f86SGleb Smirnoff     &carp_suppress_preempt, 0, "Preemption is suppressed");
199a9771948SGleb Smirnoff 
200a9771948SGleb Smirnoff struct carpstats carpstats;
201a9771948SGleb Smirnoff SYSCTL_STRUCT(_net_inet_carp, CARPCTL_STATS, stats, CTLFLAG_RW,
202a9771948SGleb Smirnoff     &carpstats, carpstats,
203a9771948SGleb Smirnoff     "CARP statistics (struct carpstats, netinet/ip_carp.h)");
204a9771948SGleb Smirnoff 
205*08b68b0eSGleb Smirnoff #define	CARP_LOCK_INIT(sc)	mtx_init(&(sc)->sc_mtx, "carp_softc",   \
206a9771948SGleb Smirnoff 	NULL, MTX_DEF)
207*08b68b0eSGleb Smirnoff #define	CARP_LOCK_DESTROY(sc)	mtx_destroy(&(sc)->sc_mtx)
208*08b68b0eSGleb Smirnoff #define	CARP_LOCK_ASSERT(sc)	mtx_assert(&(sc)->sc_mtx, MA_OWNED)
209*08b68b0eSGleb Smirnoff #define	CARP_LOCK(sc)		mtx_lock(&(sc)->sc_mtx)
210*08b68b0eSGleb Smirnoff #define	CARP_UNLOCK(sc)		mtx_unlock(&(sc)->sc_mtx)
211*08b68b0eSGleb Smirnoff #define	CIF_LOCK_INIT(cif)	mtx_init(&(cif)->cif_mtx, "carp_if",   \
212*08b68b0eSGleb Smirnoff 	NULL, MTX_DEF)
213*08b68b0eSGleb Smirnoff #define	CIF_LOCK_DESTROY(cif)	mtx_destroy(&(cif)->cif_mtx)
214*08b68b0eSGleb Smirnoff #define	CIF_LOCK_ASSERT(cif)	mtx_assert(&(cif)->cif_mtx, MA_OWNED)
215*08b68b0eSGleb Smirnoff #define	CIF_LOCK(cif)		mtx_lock(&(cif)->cif_mtx)
216*08b68b0eSGleb Smirnoff #define	CIF_UNLOCK(cif)		mtx_unlock(&(cif)->cif_mtx)
217d220759bSGleb Smirnoff 
218947b7cf3SGleb Smirnoff #define	CARP_LOG(...)	do {				\
2191e9e6572SGleb Smirnoff 	if (carp_opts[CARPCTL_LOG] > 0)			\
220*08b68b0eSGleb Smirnoff 		log(LOG_INFO, "carp: " __VA_ARGS__);	\
221947b7cf3SGleb Smirnoff } while (0)
2221e9e6572SGleb Smirnoff 
223947b7cf3SGleb Smirnoff #define	CARP_DEBUG(...)	do {				\
2241e9e6572SGleb Smirnoff 	if (carp_opts[CARPCTL_LOG] > 1)			\
2251e9e6572SGleb Smirnoff 		log(LOG_DEBUG, __VA_ARGS__);		\
226947b7cf3SGleb Smirnoff } while (0)
227a9771948SGleb Smirnoff 
228*08b68b0eSGleb Smirnoff #define	IFNET_FOREACH_IFA(ifp, ifa)					\
229*08b68b0eSGleb Smirnoff 	IF_ADDR_LOCK_ASSERT(ifp);					\
230*08b68b0eSGleb Smirnoff 	TAILQ_FOREACH((ifa), &(ifp)->if_addrhead, ifa_link)		\
231*08b68b0eSGleb Smirnoff 		if ((ifa)->ifa_carp != NULL)
232*08b68b0eSGleb Smirnoff 
233*08b68b0eSGleb Smirnoff #define	CARP_FOREACH_IFA(sc, ifa)					\
234*08b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);						\
235*08b68b0eSGleb Smirnoff 	for (int _i = 0;						\
236*08b68b0eSGleb Smirnoff 		_i < (sc)->sc_naddrs + (sc)->sc_naddrs6 &&		\
237*08b68b0eSGleb Smirnoff 		((ifa) = sc->sc_ifas[_i]) != NULL;			\
238*08b68b0eSGleb Smirnoff 		++_i)
239*08b68b0eSGleb Smirnoff 
240*08b68b0eSGleb Smirnoff #define	IFNET_FOREACH_CARP(ifp, sc)					\
241*08b68b0eSGleb Smirnoff 	CIF_LOCK_ASSERT(ifp->if_carp);					\
242*08b68b0eSGleb Smirnoff 	TAILQ_FOREACH((sc), &(ifp)->if_carp->cif_vrs, sc_list)
243*08b68b0eSGleb Smirnoff 
2445c1f0f6dSGleb Smirnoff static void	carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
245*08b68b0eSGleb Smirnoff static struct carp_softc
246*08b68b0eSGleb Smirnoff 		*carp_alloc(struct ifnet *);
247*08b68b0eSGleb Smirnoff static void	carp_destroy(struct carp_softc *);
248*08b68b0eSGleb Smirnoff static struct carp_if
249*08b68b0eSGleb Smirnoff 		*carp_alloc_if(struct ifnet *);
250*08b68b0eSGleb Smirnoff static void	carp_free_if(struct carp_if *);
251*08b68b0eSGleb Smirnoff static void	carp_set_state(struct carp_softc *, int);
252*08b68b0eSGleb Smirnoff static void	carp_sc_state(struct carp_softc *);
253*08b68b0eSGleb Smirnoff static void	carp_setrun(struct carp_softc *, sa_family_t);
2545c1f0f6dSGleb Smirnoff static void	carp_master_down(void *);
255d220759bSGleb Smirnoff static void	carp_master_down_locked(struct carp_softc *);
256*08b68b0eSGleb Smirnoff static void	carp_send_ad(void *);
257*08b68b0eSGleb Smirnoff static void	carp_send_ad_locked(struct carp_softc *);
258*08b68b0eSGleb Smirnoff static void	carp_addroute(struct carp_softc *);
259*08b68b0eSGleb Smirnoff static void	carp_delroute(struct carp_softc *);
260a9771948SGleb Smirnoff 
261*08b68b0eSGleb Smirnoff static LIST_HEAD(, carp_softc) carp_list;
262d92d54d5SGleb Smirnoff static struct mtx carp_mtx;
263a9771948SGleb Smirnoff 
264*08b68b0eSGleb Smirnoff static __inline uint16_t
265a9771948SGleb Smirnoff carp_cksum(struct mbuf *m, int len)
266a9771948SGleb Smirnoff {
267a9771948SGleb Smirnoff 	return (in_cksum(m, len));
268a9771948SGleb Smirnoff }
269a9771948SGleb Smirnoff 
2705c1f0f6dSGleb Smirnoff static void
271a9771948SGleb Smirnoff carp_hmac_prepare(struct carp_softc *sc)
272a9771948SGleb Smirnoff {
273*08b68b0eSGleb Smirnoff 	uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
274*08b68b0eSGleb Smirnoff 	uint8_t vhid = sc->sc_vhid & 0xff;
275a9771948SGleb Smirnoff 	struct ifaddr *ifa;
2761ead26d4SMax Laier 	int i, found;
2771ead26d4SMax Laier #ifdef INET
2781ead26d4SMax Laier 	struct in_addr last, cur, in;
2791ead26d4SMax Laier #endif
280a9771948SGleb Smirnoff #ifdef INET6
2811ead26d4SMax Laier 	struct in6_addr last6, cur6, in6;
282a9771948SGleb Smirnoff #endif
283a9771948SGleb Smirnoff 
284*08b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
285d220759bSGleb Smirnoff 
286*08b68b0eSGleb Smirnoff 	/* Compute ipad from key. */
287a9771948SGleb Smirnoff 	bzero(sc->sc_pad, sizeof(sc->sc_pad));
288a9771948SGleb Smirnoff 	bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
289a9771948SGleb Smirnoff 	for (i = 0; i < sizeof(sc->sc_pad); i++)
290a9771948SGleb Smirnoff 		sc->sc_pad[i] ^= 0x36;
291a9771948SGleb Smirnoff 
292*08b68b0eSGleb Smirnoff 	/* Precompute first part of inner hash. */
293a9771948SGleb Smirnoff 	SHA1Init(&sc->sc_sha1);
294a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
295a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
296a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
297a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
298a9771948SGleb Smirnoff #ifdef INET
2991ead26d4SMax Laier 	cur.s_addr = 0;
3001ead26d4SMax Laier 	do {
3011ead26d4SMax Laier 		found = 0;
3021ead26d4SMax Laier 		last = cur;
3031ead26d4SMax Laier 		cur.s_addr = 0xffffffff;
304*08b68b0eSGleb Smirnoff 		CARP_FOREACH_IFA(sc, ifa) {
3051ead26d4SMax Laier 			in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr;
3061ead26d4SMax Laier 			if (ifa->ifa_addr->sa_family == AF_INET &&
3071ead26d4SMax Laier 			    ntohl(in.s_addr) > ntohl(last.s_addr) &&
3081ead26d4SMax Laier 			    ntohl(in.s_addr) < ntohl(cur.s_addr)) {
3091ead26d4SMax Laier 				cur.s_addr = in.s_addr;
3101ead26d4SMax Laier 				found++;
311a9771948SGleb Smirnoff 			}
3121ead26d4SMax Laier 		}
3131ead26d4SMax Laier 		if (found)
3141ead26d4SMax Laier 			SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur));
3151ead26d4SMax Laier 	} while (found);
316a9771948SGleb Smirnoff #endif /* INET */
317a9771948SGleb Smirnoff #ifdef INET6
3181ead26d4SMax Laier 	memset(&cur6, 0, sizeof(cur6));
3191ead26d4SMax Laier 	do {
3201ead26d4SMax Laier 		found = 0;
3211ead26d4SMax Laier 		last6 = cur6;
3221ead26d4SMax Laier 		memset(&cur6, 0xff, sizeof(cur6));
323*08b68b0eSGleb Smirnoff 		CARP_FOREACH_IFA(sc, ifa) {
324a9771948SGleb Smirnoff 			in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
3251ead26d4SMax Laier 			if (IN6_IS_SCOPE_EMBED(&in6))
3261ead26d4SMax Laier 				in6.s6_addr16[1] = 0;
3271ead26d4SMax Laier 			if (ifa->ifa_addr->sa_family == AF_INET6 &&
3281ead26d4SMax Laier 			    memcmp(&in6, &last6, sizeof(in6)) > 0 &&
3291ead26d4SMax Laier 			    memcmp(&in6, &cur6, sizeof(in6)) < 0) {
3301ead26d4SMax Laier 				cur6 = in6;
3311ead26d4SMax Laier 				found++;
332a9771948SGleb Smirnoff 			}
333a9771948SGleb Smirnoff 		}
3341ead26d4SMax Laier 		if (found)
3351ead26d4SMax Laier 			SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6));
3361ead26d4SMax Laier 	} while (found);
337a9771948SGleb Smirnoff #endif /* INET6 */
338a9771948SGleb Smirnoff 
339a9771948SGleb Smirnoff 	/* convert ipad to opad */
340a9771948SGleb Smirnoff 	for (i = 0; i < sizeof(sc->sc_pad); i++)
341a9771948SGleb Smirnoff 		sc->sc_pad[i] ^= 0x36 ^ 0x5c;
342a9771948SGleb Smirnoff }
343a9771948SGleb Smirnoff 
3445c1f0f6dSGleb Smirnoff static void
345*08b68b0eSGleb Smirnoff carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
346a9771948SGleb Smirnoff     unsigned char md[20])
347a9771948SGleb Smirnoff {
348a9771948SGleb Smirnoff 	SHA1_CTX sha1ctx;
349a9771948SGleb Smirnoff 
350*08b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
351*08b68b0eSGleb Smirnoff 
352a9771948SGleb Smirnoff 	/* fetch first half of inner hash */
353a9771948SGleb Smirnoff 	bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
354a9771948SGleb Smirnoff 
355a9771948SGleb Smirnoff 	SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
356a9771948SGleb Smirnoff 	SHA1Final(md, &sha1ctx);
357a9771948SGleb Smirnoff 
358a9771948SGleb Smirnoff 	/* outer hash */
359a9771948SGleb Smirnoff 	SHA1Init(&sha1ctx);
360a9771948SGleb Smirnoff 	SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
361a9771948SGleb Smirnoff 	SHA1Update(&sha1ctx, md, 20);
362a9771948SGleb Smirnoff 	SHA1Final(md, &sha1ctx);
363a9771948SGleb Smirnoff }
364a9771948SGleb Smirnoff 
3655c1f0f6dSGleb Smirnoff static int
366*08b68b0eSGleb Smirnoff carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
367a9771948SGleb Smirnoff     unsigned char md[20])
368a9771948SGleb Smirnoff {
369a9771948SGleb Smirnoff 	unsigned char md2[20];
370a9771948SGleb Smirnoff 
371*08b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
372d220759bSGleb Smirnoff 
373a9771948SGleb Smirnoff 	carp_hmac_generate(sc, counter, md2);
374a9771948SGleb Smirnoff 
375a9771948SGleb Smirnoff 	return (bcmp(md, md2, sizeof(md2)));
376a9771948SGleb Smirnoff }
377a9771948SGleb Smirnoff 
378a9771948SGleb Smirnoff /*
379a9771948SGleb Smirnoff  * process input packet.
380a9771948SGleb Smirnoff  * we have rearranged checks order compared to the rfc,
381a9771948SGleb Smirnoff  * but it seems more efficient this way or not possible otherwise.
382a9771948SGleb Smirnoff  */
383a0ae8f04SBjoern A. Zeeb #ifdef INET
384a9771948SGleb Smirnoff void
385a9771948SGleb Smirnoff carp_input(struct mbuf *m, int hlen)
386a9771948SGleb Smirnoff {
387a9771948SGleb Smirnoff 	struct ip *ip = mtod(m, struct ip *);
388a9771948SGleb Smirnoff 	struct carp_header *ch;
389a9771948SGleb Smirnoff 	int iplen, len;
390a9771948SGleb Smirnoff 
3916bf65bcfSRobert Watson 	CARPSTATS_INC(carps_ipackets);
392a9771948SGleb Smirnoff 
393a9771948SGleb Smirnoff 	if (!carp_opts[CARPCTL_ALLOW]) {
394a9771948SGleb Smirnoff 		m_freem(m);
395a9771948SGleb Smirnoff 		return;
396a9771948SGleb Smirnoff 	}
397a9771948SGleb Smirnoff 
398a9771948SGleb Smirnoff 	/* verify that the IP TTL is 255.  */
399a9771948SGleb Smirnoff 	if (ip->ip_ttl != CARP_DFLTTL) {
4006bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badttl);
401*08b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
4021e9e6572SGleb Smirnoff 		    ip->ip_ttl,
4031e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
404a9771948SGleb Smirnoff 		m_freem(m);
405a9771948SGleb Smirnoff 		return;
406a9771948SGleb Smirnoff 	}
407a9771948SGleb Smirnoff 
408a9771948SGleb Smirnoff 	iplen = ip->ip_hl << 2;
409a9771948SGleb Smirnoff 
410a9771948SGleb Smirnoff 	if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
4116bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badlen);
412*08b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: received len %zd < sizeof(struct carp_header) "
413*08b68b0eSGleb Smirnoff 		    "on %s\n", __func__, m->m_len - sizeof(struct ip),
41416e324fcSXin LI 		    m->m_pkthdr.rcvif->if_xname);
415a9771948SGleb Smirnoff 		m_freem(m);
416a9771948SGleb Smirnoff 		return;
417a9771948SGleb Smirnoff 	}
418a9771948SGleb Smirnoff 
419a9771948SGleb Smirnoff 	if (iplen + sizeof(*ch) < m->m_len) {
420a9771948SGleb Smirnoff 		if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
4216bf65bcfSRobert Watson 			CARPSTATS_INC(carps_hdrops);
422*08b68b0eSGleb Smirnoff 			CARP_DEBUG("%s: pullup failed\n", __func__);
423a9771948SGleb Smirnoff 			return;
424a9771948SGleb Smirnoff 		}
425a9771948SGleb Smirnoff 		ip = mtod(m, struct ip *);
426a9771948SGleb Smirnoff 	}
427a9771948SGleb Smirnoff 	ch = (struct carp_header *)((char *)ip + iplen);
428a9771948SGleb Smirnoff 
429a9771948SGleb Smirnoff 	/*
430a9771948SGleb Smirnoff 	 * verify that the received packet length is
431a9771948SGleb Smirnoff 	 * equal to the CARP header
432a9771948SGleb Smirnoff 	 */
433a9771948SGleb Smirnoff 	len = iplen + sizeof(*ch);
434a9771948SGleb Smirnoff 	if (len > m->m_pkthdr.len) {
4356bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badlen);
436*08b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: packet too short %d on %s\n", __func__,
4371e9e6572SGleb Smirnoff 		    m->m_pkthdr.len,
4381e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
439a9771948SGleb Smirnoff 		m_freem(m);
440a9771948SGleb Smirnoff 		return;
441a9771948SGleb Smirnoff 	}
442a9771948SGleb Smirnoff 
443a9771948SGleb Smirnoff 	if ((m = m_pullup(m, len)) == NULL) {
4446bf65bcfSRobert Watson 		CARPSTATS_INC(carps_hdrops);
445a9771948SGleb Smirnoff 		return;
446a9771948SGleb Smirnoff 	}
447a9771948SGleb Smirnoff 	ip = mtod(m, struct ip *);
448a9771948SGleb Smirnoff 	ch = (struct carp_header *)((char *)ip + iplen);
449a9771948SGleb Smirnoff 
450a9771948SGleb Smirnoff 	/* verify the CARP checksum */
451a9771948SGleb Smirnoff 	m->m_data += iplen;
452a9771948SGleb Smirnoff 	if (carp_cksum(m, len - iplen)) {
4536bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badsum);
454*08b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: checksum failed on %s\n", __func__,
4551e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
456a9771948SGleb Smirnoff 		m_freem(m);
457a9771948SGleb Smirnoff 		return;
458a9771948SGleb Smirnoff 	}
459a9771948SGleb Smirnoff 	m->m_data -= iplen;
460a9771948SGleb Smirnoff 
4611e9e6572SGleb Smirnoff 	carp_input_c(m, ch, AF_INET);
462a9771948SGleb Smirnoff }
463a0ae8f04SBjoern A. Zeeb #endif
464a9771948SGleb Smirnoff 
465a9771948SGleb Smirnoff #ifdef INET6
466a9771948SGleb Smirnoff int
467a9771948SGleb Smirnoff carp6_input(struct mbuf **mp, int *offp, int proto)
468a9771948SGleb Smirnoff {
469a9771948SGleb Smirnoff 	struct mbuf *m = *mp;
470a9771948SGleb Smirnoff 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
471a9771948SGleb Smirnoff 	struct carp_header *ch;
472a9771948SGleb Smirnoff 	u_int len;
473a9771948SGleb Smirnoff 
4746bf65bcfSRobert Watson 	CARPSTATS_INC(carps_ipackets6);
475a9771948SGleb Smirnoff 
476a9771948SGleb Smirnoff 	if (!carp_opts[CARPCTL_ALLOW]) {
477a9771948SGleb Smirnoff 		m_freem(m);
478a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
479a9771948SGleb Smirnoff 	}
480a9771948SGleb Smirnoff 
481a9771948SGleb Smirnoff 	/* check if received on a valid carp interface */
482a9771948SGleb Smirnoff 	if (m->m_pkthdr.rcvif->if_carp == NULL) {
4836bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badif);
484*08b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: packet received on non-carp interface: %s\n",
485*08b68b0eSGleb Smirnoff 		    __func__, m->m_pkthdr.rcvif->if_xname);
486a9771948SGleb Smirnoff 		m_freem(m);
487a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
488a9771948SGleb Smirnoff 	}
489a9771948SGleb Smirnoff 
490a9771948SGleb Smirnoff 	/* verify that the IP TTL is 255 */
491a9771948SGleb Smirnoff 	if (ip6->ip6_hlim != CARP_DFLTTL) {
4926bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badttl);
493*08b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
494*08b68b0eSGleb Smirnoff 		    ip6->ip6_hlim, m->m_pkthdr.rcvif->if_xname);
495a9771948SGleb Smirnoff 		m_freem(m);
496a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
497a9771948SGleb Smirnoff 	}
498a9771948SGleb Smirnoff 
499a9771948SGleb Smirnoff 	/* verify that we have a complete carp packet */
500a9771948SGleb Smirnoff 	len = m->m_len;
501a9771948SGleb Smirnoff 	IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
502a9771948SGleb Smirnoff 	if (ch == NULL) {
5036bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badlen);
504*08b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: packet size %u too small\n", __func__, len);
505a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
506a9771948SGleb Smirnoff 	}
507a9771948SGleb Smirnoff 
508a9771948SGleb Smirnoff 
509a9771948SGleb Smirnoff 	/* verify the CARP checksum */
510a9771948SGleb Smirnoff 	m->m_data += *offp;
511a9771948SGleb Smirnoff 	if (carp_cksum(m, sizeof(*ch))) {
5126bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badsum);
513*08b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: checksum failed, on %s\n", __func__,
5141e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
515a9771948SGleb Smirnoff 		m_freem(m);
516a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
517a9771948SGleb Smirnoff 	}
518a9771948SGleb Smirnoff 	m->m_data -= *offp;
519a9771948SGleb Smirnoff 
5201e9e6572SGleb Smirnoff 	carp_input_c(m, ch, AF_INET6);
521a9771948SGleb Smirnoff 	return (IPPROTO_DONE);
522a9771948SGleb Smirnoff }
523a9771948SGleb Smirnoff #endif /* INET6 */
524a9771948SGleb Smirnoff 
5255c1f0f6dSGleb Smirnoff static void
5261e9e6572SGleb Smirnoff carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
527a9771948SGleb Smirnoff {
528a9771948SGleb Smirnoff 	struct ifnet *ifp = m->m_pkthdr.rcvif;
529*08b68b0eSGleb Smirnoff 	struct ifaddr *ifa;
5301e9e6572SGleb Smirnoff 	struct carp_softc *sc;
531*08b68b0eSGleb Smirnoff 	uint64_t tmp_counter;
532a9771948SGleb Smirnoff 	struct timeval sc_tv, ch_tv;
533a9771948SGleb Smirnoff 
534a9771948SGleb Smirnoff 	/* verify that the VHID is valid on the receiving interface */
535*08b68b0eSGleb Smirnoff 	IF_ADDR_LOCK(ifp);
536*08b68b0eSGleb Smirnoff 	IFNET_FOREACH_IFA(ifp, ifa)
537*08b68b0eSGleb Smirnoff 		if (ifa->ifa_addr->sa_family == af &&
538*08b68b0eSGleb Smirnoff 		    ifa->ifa_carp->sc_vhid == ch->carp_vhid) {
539*08b68b0eSGleb Smirnoff 			ifa_ref(ifa);
540a9771948SGleb Smirnoff 			break;
541*08b68b0eSGleb Smirnoff 		}
542*08b68b0eSGleb Smirnoff 	IF_ADDR_UNLOCK(ifp);
543d220759bSGleb Smirnoff 
544*08b68b0eSGleb Smirnoff 	if (ifa == NULL) {
5456bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badvhid);
546a9771948SGleb Smirnoff 		m_freem(m);
547a9771948SGleb Smirnoff 		return;
548a9771948SGleb Smirnoff 	}
549a9771948SGleb Smirnoff 
550a9771948SGleb Smirnoff 	/* verify the CARP version. */
551a9771948SGleb Smirnoff 	if (ch->carp_version != CARP_VERSION) {
5526bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badver);
553*08b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: invalid version %d\n", ifp->if_xname,
5541e9e6572SGleb Smirnoff 		    ch->carp_version);
555*08b68b0eSGleb Smirnoff 		ifa_free(ifa);
556a9771948SGleb Smirnoff 		m_freem(m);
557a9771948SGleb Smirnoff 		return;
558a9771948SGleb Smirnoff 	}
559a9771948SGleb Smirnoff 
560*08b68b0eSGleb Smirnoff 	sc = ifa->ifa_carp;
561*08b68b0eSGleb Smirnoff 	CARP_LOCK(sc);
562*08b68b0eSGleb Smirnoff 	ifa_free(ifa);
563*08b68b0eSGleb Smirnoff 
564a9771948SGleb Smirnoff 	if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
5656bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badauth);
566*08b68b0eSGleb Smirnoff 		CARP_DEBUG("%s: incorrect hash for VHID %u@%s\n", __func__,
567*08b68b0eSGleb Smirnoff 		    sc->sc_vhid, ifp->if_xname);
568*08b68b0eSGleb Smirnoff 		goto out;
569a9771948SGleb Smirnoff 	}
570a9771948SGleb Smirnoff 
571a9771948SGleb Smirnoff 	tmp_counter = ntohl(ch->carp_counter[0]);
572a9771948SGleb Smirnoff 	tmp_counter = tmp_counter<<32;
573a9771948SGleb Smirnoff 	tmp_counter += ntohl(ch->carp_counter[1]);
574a9771948SGleb Smirnoff 
575a9771948SGleb Smirnoff 	/* XXX Replay protection goes here */
576a9771948SGleb Smirnoff 
577a9771948SGleb Smirnoff 	sc->sc_init_counter = 0;
578a9771948SGleb Smirnoff 	sc->sc_counter = tmp_counter;
579a9771948SGleb Smirnoff 
580a9771948SGleb Smirnoff 	sc_tv.tv_sec = sc->sc_advbase;
581a9771948SGleb Smirnoff 	if (carp_suppress_preempt && sc->sc_advskew <  240)
582a9771948SGleb Smirnoff 		sc_tv.tv_usec = 240 * 1000000 / 256;
583a9771948SGleb Smirnoff 	else
584a9771948SGleb Smirnoff 		sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256;
585a9771948SGleb Smirnoff 	ch_tv.tv_sec = ch->carp_advbase;
586a9771948SGleb Smirnoff 	ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
587a9771948SGleb Smirnoff 
588a9771948SGleb Smirnoff 	switch (sc->sc_state) {
589a9771948SGleb Smirnoff 	case INIT:
590a9771948SGleb Smirnoff 		break;
591a9771948SGleb Smirnoff 	case MASTER:
592a9771948SGleb Smirnoff 		/*
593a9771948SGleb Smirnoff 		 * If we receive an advertisement from a master who's going to
594a9771948SGleb Smirnoff 		 * be more frequent than us, go into BACKUP state.
595a9771948SGleb Smirnoff 		 */
596a9771948SGleb Smirnoff 		if (timevalcmp(&sc_tv, &ch_tv, >) ||
597a9771948SGleb Smirnoff 		    timevalcmp(&sc_tv, &ch_tv, ==)) {
598a9771948SGleb Smirnoff 			callout_stop(&sc->sc_ad_tmo);
599*08b68b0eSGleb Smirnoff 			CARP_LOG("VHID %u@%s: MASTER -> BACKUP "
60088bf82a6SGleb Smirnoff 			    "(more frequent advertisement received)\n",
601*08b68b0eSGleb Smirnoff 			    sc->sc_vhid,
602*08b68b0eSGleb Smirnoff 			    sc->sc_carpdev->if_xname);
603a9771948SGleb Smirnoff 			carp_set_state(sc, BACKUP);
604a9771948SGleb Smirnoff 			carp_setrun(sc, 0);
605*08b68b0eSGleb Smirnoff 			carp_delroute(sc);
606a9771948SGleb Smirnoff 		}
607a9771948SGleb Smirnoff 		break;
608a9771948SGleb Smirnoff 	case BACKUP:
609a9771948SGleb Smirnoff 		/*
610a9771948SGleb Smirnoff 		 * If we're pre-empting masters who advertise slower than us,
611a9771948SGleb Smirnoff 		 * and this one claims to be slower, treat him as down.
612a9771948SGleb Smirnoff 		 */
613a9771948SGleb Smirnoff 		if (carp_opts[CARPCTL_PREEMPT] &&
614a9771948SGleb Smirnoff 		    timevalcmp(&sc_tv, &ch_tv, <)) {
615*08b68b0eSGleb Smirnoff 			CARP_LOG("VHID %u@%s: BACKUP -> MASTER "
61688bf82a6SGleb Smirnoff 			    "(preempting a slower master)\n",
617*08b68b0eSGleb Smirnoff 			    sc->sc_vhid,
618*08b68b0eSGleb Smirnoff 			    sc->sc_carpdev->if_xname);
619d220759bSGleb Smirnoff 			carp_master_down_locked(sc);
620a9771948SGleb Smirnoff 			break;
621a9771948SGleb Smirnoff 		}
622a9771948SGleb Smirnoff 
623a9771948SGleb Smirnoff 		/*
624a9771948SGleb Smirnoff 		 *  If the master is going to advertise at such a low frequency
625a9771948SGleb Smirnoff 		 *  that he's guaranteed to time out, we'd might as well just
626a9771948SGleb Smirnoff 		 *  treat him as timed out now.
627a9771948SGleb Smirnoff 		 */
628a9771948SGleb Smirnoff 		sc_tv.tv_sec = sc->sc_advbase * 3;
629a9771948SGleb Smirnoff 		if (timevalcmp(&sc_tv, &ch_tv, <)) {
630*08b68b0eSGleb Smirnoff 			CARP_LOG("VHID %u@%s: BACKUP -> MASTER "
63188bf82a6SGleb Smirnoff 			    "(master timed out)\n",
632*08b68b0eSGleb Smirnoff 			    sc->sc_vhid,
633*08b68b0eSGleb Smirnoff 			    sc->sc_carpdev->if_xname);
634d220759bSGleb Smirnoff 			carp_master_down_locked(sc);
635a9771948SGleb Smirnoff 			break;
636a9771948SGleb Smirnoff 		}
637a9771948SGleb Smirnoff 
638a9771948SGleb Smirnoff 		/*
639a9771948SGleb Smirnoff 		 * Otherwise, we reset the counter and wait for the next
640a9771948SGleb Smirnoff 		 * advertisement.
641a9771948SGleb Smirnoff 		 */
642a9771948SGleb Smirnoff 		carp_setrun(sc, af);
643a9771948SGleb Smirnoff 		break;
644a9771948SGleb Smirnoff 	}
645a9771948SGleb Smirnoff 
646*08b68b0eSGleb Smirnoff out:
647*08b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
648a9771948SGleb Smirnoff 	m_freem(m);
649a9771948SGleb Smirnoff }
650a9771948SGleb Smirnoff 
6515c1f0f6dSGleb Smirnoff static int
652a9771948SGleb Smirnoff carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
653a9771948SGleb Smirnoff {
654a9771948SGleb Smirnoff 	struct m_tag *mtag;
655a9771948SGleb Smirnoff 
656a9771948SGleb Smirnoff 	if (sc->sc_init_counter) {
657a9771948SGleb Smirnoff 		/* this could also be seconds since unix epoch */
658a9771948SGleb Smirnoff 		sc->sc_counter = arc4random();
659a9771948SGleb Smirnoff 		sc->sc_counter = sc->sc_counter << 32;
660a9771948SGleb Smirnoff 		sc->sc_counter += arc4random();
661a9771948SGleb Smirnoff 	} else
662a9771948SGleb Smirnoff 		sc->sc_counter++;
663a9771948SGleb Smirnoff 
664a9771948SGleb Smirnoff 	ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
665a9771948SGleb Smirnoff 	ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
666a9771948SGleb Smirnoff 
667a9771948SGleb Smirnoff 	carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
668a9771948SGleb Smirnoff 
669a9771948SGleb Smirnoff 	/* Tag packet for carp_output */
670*08b68b0eSGleb Smirnoff 	if ((mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct carp_softc *),
671*08b68b0eSGleb Smirnoff 	    M_NOWAIT)) == NULL) {
672a9771948SGleb Smirnoff 		m_freem(m);
673*08b68b0eSGleb Smirnoff 		CARPSTATS_INC(carps_onomem);
674a9771948SGleb Smirnoff 		return (ENOMEM);
675a9771948SGleb Smirnoff 	}
676*08b68b0eSGleb Smirnoff 	bcopy(&sc, (caddr_t)(mtag + 1), sizeof(struct carp_softc *));
677a9771948SGleb Smirnoff 	m_tag_prepend(m, mtag);
678a9771948SGleb Smirnoff 
679a9771948SGleb Smirnoff 	return (0);
680a9771948SGleb Smirnoff }
681a9771948SGleb Smirnoff 
6825c1f0f6dSGleb Smirnoff static void
683*08b68b0eSGleb Smirnoff carp_send_ad_all(struct carp_softc *badsc)
684a9771948SGleb Smirnoff {
685d92d54d5SGleb Smirnoff 	struct carp_softc *sc;
686a9771948SGleb Smirnoff 
687*08b68b0eSGleb Smirnoff 	/*
688*08b68b0eSGleb Smirnoff 	 * Avoid LOR and recursive call to carp_send_ad_locked().
689*08b68b0eSGleb Smirnoff 	 */
690*08b68b0eSGleb Smirnoff 	CARP_UNLOCK(badsc);
691*08b68b0eSGleb Smirnoff 
692d92d54d5SGleb Smirnoff 	mtx_lock(&carp_mtx);
693*08b68b0eSGleb Smirnoff 	LIST_FOREACH(sc, &carp_list, sc_next)
694*08b68b0eSGleb Smirnoff 		if (sc != badsc && sc->sc_state == MASTER) {
695*08b68b0eSGleb Smirnoff 			CARP_LOCK(sc);
696d220759bSGleb Smirnoff 			carp_send_ad_locked(sc);
697*08b68b0eSGleb Smirnoff 			CARP_UNLOCK(sc);
698a9771948SGleb Smirnoff 		}
699d92d54d5SGleb Smirnoff 	mtx_unlock(&carp_mtx);
700*08b68b0eSGleb Smirnoff 
701*08b68b0eSGleb Smirnoff 	CARP_LOCK(badsc);
702a9771948SGleb Smirnoff }
703a9771948SGleb Smirnoff 
7045c1f0f6dSGleb Smirnoff static void
705a9771948SGleb Smirnoff carp_send_ad(void *v)
706a9771948SGleb Smirnoff {
707d220759bSGleb Smirnoff 	struct carp_softc *sc = v;
708d220759bSGleb Smirnoff 
709*08b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
710d220759bSGleb Smirnoff 	carp_send_ad_locked(sc);
711*08b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
712d220759bSGleb Smirnoff }
713d220759bSGleb Smirnoff 
714d220759bSGleb Smirnoff static void
715d220759bSGleb Smirnoff carp_send_ad_locked(struct carp_softc *sc)
716d220759bSGleb Smirnoff {
717a9771948SGleb Smirnoff 	struct carp_header ch;
718a9771948SGleb Smirnoff 	struct timeval tv;
719*08b68b0eSGleb Smirnoff 	struct sockaddr sa;
720*08b68b0eSGleb Smirnoff 	struct ifaddr *ifa;
721a9771948SGleb Smirnoff 	struct carp_header *ch_ptr;
722a9771948SGleb Smirnoff 	struct mbuf *m;
723*08b68b0eSGleb Smirnoff 	int len, advskew;
724a9771948SGleb Smirnoff 
725*08b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
726d220759bSGleb Smirnoff 
727a9771948SGleb Smirnoff 	if (!carp_suppress_preempt || sc->sc_advskew > 240)
728a9771948SGleb Smirnoff 		advskew = sc->sc_advskew;
729a9771948SGleb Smirnoff 	else
730a9771948SGleb Smirnoff 		advskew = 240;
731*08b68b0eSGleb Smirnoff 	tv.tv_sec = sc->sc_advbase;
732a9771948SGleb Smirnoff 	tv.tv_usec = advskew * 1000000 / 256;
733a9771948SGleb Smirnoff 
734a9771948SGleb Smirnoff 	ch.carp_version = CARP_VERSION;
735a9771948SGleb Smirnoff 	ch.carp_type = CARP_ADVERTISEMENT;
736a9771948SGleb Smirnoff 	ch.carp_vhid = sc->sc_vhid;
737*08b68b0eSGleb Smirnoff 	ch.carp_advbase = sc->sc_advbase;
738a9771948SGleb Smirnoff 	ch.carp_advskew = advskew;
739a9771948SGleb Smirnoff 	ch.carp_authlen = 7;	/* XXX DEFINE */
740a9771948SGleb Smirnoff 	ch.carp_pad1 = 0;	/* must be zero */
741a9771948SGleb Smirnoff 	ch.carp_cksum = 0;
742a9771948SGleb Smirnoff 
743*08b68b0eSGleb Smirnoff 	/* XXXGL: OpenBSD picks first ifaddr with needed family. */
744*08b68b0eSGleb Smirnoff 
745a9771948SGleb Smirnoff #ifdef INET
746*08b68b0eSGleb Smirnoff 	if (sc->sc_naddrs) {
747a9771948SGleb Smirnoff 		struct ip *ip;
748a9771948SGleb Smirnoff 
749*08b68b0eSGleb Smirnoff 		MGETHDR(m, M_NOWAIT, MT_HEADER);
750a9771948SGleb Smirnoff 		if (m == NULL) {
7516bf65bcfSRobert Watson 			CARPSTATS_INC(carps_onomem);
752a9771948SGleb Smirnoff 			/* XXX maybe less ? */
753a9771948SGleb Smirnoff 			callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
754a9771948SGleb Smirnoff 			    carp_send_ad, sc);
755a9771948SGleb Smirnoff 			return;
756a9771948SGleb Smirnoff 		}
757a9771948SGleb Smirnoff 		len = sizeof(*ip) + sizeof(ch);
758a9771948SGleb Smirnoff 		m->m_pkthdr.len = len;
759a9771948SGleb Smirnoff 		m->m_pkthdr.rcvif = NULL;
760a9771948SGleb Smirnoff 		m->m_len = len;
761a9771948SGleb Smirnoff 		MH_ALIGN(m, m->m_len);
762a9771948SGleb Smirnoff 		m->m_flags |= M_MCAST;
763a9771948SGleb Smirnoff 		ip = mtod(m, struct ip *);
764a9771948SGleb Smirnoff 		ip->ip_v = IPVERSION;
765a9771948SGleb Smirnoff 		ip->ip_hl = sizeof(*ip) >> 2;
766a9771948SGleb Smirnoff 		ip->ip_tos = IPTOS_LOWDELAY;
767a9771948SGleb Smirnoff 		ip->ip_len = len;
768a9771948SGleb Smirnoff 		ip->ip_id = ip_newid();
769a9771948SGleb Smirnoff 		ip->ip_off = IP_DF;
770a9771948SGleb Smirnoff 		ip->ip_ttl = CARP_DFLTTL;
771a9771948SGleb Smirnoff 		ip->ip_p = IPPROTO_CARP;
772a9771948SGleb Smirnoff 		ip->ip_sum = 0;
773*08b68b0eSGleb Smirnoff 
774*08b68b0eSGleb Smirnoff 		bzero(&sa, sizeof(sa));
775*08b68b0eSGleb Smirnoff 		sa.sa_family = AF_INET;
776*08b68b0eSGleb Smirnoff 		ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
777*08b68b0eSGleb Smirnoff 		if (ifa != NULL) {
778*08b68b0eSGleb Smirnoff 			ip->ip_src.s_addr =
779*08b68b0eSGleb Smirnoff 			    ifatoia(ifa)->ia_addr.sin_addr.s_addr;
780*08b68b0eSGleb Smirnoff 			ifa_free(ifa);
781*08b68b0eSGleb Smirnoff 		} else
782*08b68b0eSGleb Smirnoff 			ip->ip_src.s_addr = 0;
783a9771948SGleb Smirnoff 		ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
784a9771948SGleb Smirnoff 
785a9771948SGleb Smirnoff 		ch_ptr = (struct carp_header *)(&ip[1]);
786a9771948SGleb Smirnoff 		bcopy(&ch, ch_ptr, sizeof(ch));
787a9771948SGleb Smirnoff 		if (carp_prepare_ad(m, sc, ch_ptr))
788a9771948SGleb Smirnoff 			return;
789a9771948SGleb Smirnoff 
790a9771948SGleb Smirnoff 		m->m_data += sizeof(*ip);
791a9771948SGleb Smirnoff 		ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip));
792a9771948SGleb Smirnoff 		m->m_data -= sizeof(*ip);
793a9771948SGleb Smirnoff 
7946bf65bcfSRobert Watson 		CARPSTATS_INC(carps_opackets);
795a9771948SGleb Smirnoff 
796*08b68b0eSGleb Smirnoff 		if (ip_output(m, NULL, NULL, IP_RAWOUTPUT,
797*08b68b0eSGleb Smirnoff 		    &sc->sc_carpdev->if_carp->cif_imo, NULL)) {
798a9771948SGleb Smirnoff 			if (sc->sc_sendad_errors < INT_MAX)
799a9771948SGleb Smirnoff 				sc->sc_sendad_errors++;
800a9771948SGleb Smirnoff 			if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
801a9771948SGleb Smirnoff 				carp_suppress_preempt++;
802*08b68b0eSGleb Smirnoff 				if (carp_suppress_preempt == 1)
803*08b68b0eSGleb Smirnoff 					carp_send_ad_all(sc);
804a9771948SGleb Smirnoff 			}
805a9771948SGleb Smirnoff 			sc->sc_sendad_success = 0;
806a9771948SGleb Smirnoff 		} else {
807a9771948SGleb Smirnoff 			if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
808a9771948SGleb Smirnoff 				if (++sc->sc_sendad_success >=
809a9771948SGleb Smirnoff 				    CARP_SENDAD_MIN_SUCCESS) {
810a9771948SGleb Smirnoff 					carp_suppress_preempt--;
811a9771948SGleb Smirnoff 					sc->sc_sendad_errors = 0;
812a9771948SGleb Smirnoff 				}
813a9771948SGleb Smirnoff 			} else
814a9771948SGleb Smirnoff 				sc->sc_sendad_errors = 0;
815a9771948SGleb Smirnoff 		}
816a9771948SGleb Smirnoff 	}
817a9771948SGleb Smirnoff #endif /* INET */
818a9771948SGleb Smirnoff #ifdef INET6
819*08b68b0eSGleb Smirnoff 	if (sc->sc_naddrs6) {
820a9771948SGleb Smirnoff 		struct ip6_hdr *ip6;
821a9771948SGleb Smirnoff 
822*08b68b0eSGleb Smirnoff 		MGETHDR(m, M_NOWAIT, MT_HEADER);
823a9771948SGleb Smirnoff 		if (m == NULL) {
8246bf65bcfSRobert Watson 			CARPSTATS_INC(carps_onomem);
825a9771948SGleb Smirnoff 			/* XXX maybe less ? */
826a9771948SGleb Smirnoff 			callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
827a9771948SGleb Smirnoff 			    carp_send_ad, sc);
828a9771948SGleb Smirnoff 			return;
829a9771948SGleb Smirnoff 		}
830a9771948SGleb Smirnoff 		len = sizeof(*ip6) + sizeof(ch);
831a9771948SGleb Smirnoff 		m->m_pkthdr.len = len;
832a9771948SGleb Smirnoff 		m->m_pkthdr.rcvif = NULL;
833a9771948SGleb Smirnoff 		m->m_len = len;
834a9771948SGleb Smirnoff 		MH_ALIGN(m, m->m_len);
835a9771948SGleb Smirnoff 		m->m_flags |= M_MCAST;
836a9771948SGleb Smirnoff 		ip6 = mtod(m, struct ip6_hdr *);
837a9771948SGleb Smirnoff 		bzero(ip6, sizeof(*ip6));
838a9771948SGleb Smirnoff 		ip6->ip6_vfc |= IPV6_VERSION;
839a9771948SGleb Smirnoff 		ip6->ip6_hlim = CARP_DFLTTL;
840a9771948SGleb Smirnoff 		ip6->ip6_nxt = IPPROTO_CARP;
841*08b68b0eSGleb Smirnoff 		bzero(&sa, sizeof(sa));
842a9771948SGleb Smirnoff 
843*08b68b0eSGleb Smirnoff 		/* set the source address */
844*08b68b0eSGleb Smirnoff 		sa.sa_family = AF_INET6;
845*08b68b0eSGleb Smirnoff 		ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
846*08b68b0eSGleb Smirnoff 		if (ifa != NULL) {
847*08b68b0eSGleb Smirnoff 			bcopy(IFA_IN6(ifa), &ip6->ip6_src,
848*08b68b0eSGleb Smirnoff 			    sizeof(struct in6_addr));
849*08b68b0eSGleb Smirnoff 			ifa_free(ifa);
850*08b68b0eSGleb Smirnoff 		} else
851*08b68b0eSGleb Smirnoff 			/* This should never happen with IPv6. */
852*08b68b0eSGleb Smirnoff 			bzero(&ip6->ip6_src, sizeof(struct in6_addr));
853*08b68b0eSGleb Smirnoff 
854*08b68b0eSGleb Smirnoff 		/* Set the multicast destination. */
8557002145dSBjoern A. Zeeb 		ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
856a9771948SGleb Smirnoff 		ip6->ip6_dst.s6_addr8[15] = 0x12;
8577002145dSBjoern A. Zeeb 		if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
8587002145dSBjoern A. Zeeb 			m_freem(m);
859e81ab876SGleb Smirnoff 			CARP_DEBUG("%s: in6_setscope failed\n", __func__);
8607002145dSBjoern A. Zeeb 			return;
8617002145dSBjoern A. Zeeb 		}
862a9771948SGleb Smirnoff 
863a9771948SGleb Smirnoff 		ch_ptr = (struct carp_header *)(&ip6[1]);
864a9771948SGleb Smirnoff 		bcopy(&ch, ch_ptr, sizeof(ch));
865a9771948SGleb Smirnoff 		if (carp_prepare_ad(m, sc, ch_ptr))
866a9771948SGleb Smirnoff 			return;
867a9771948SGleb Smirnoff 
868a9771948SGleb Smirnoff 		m->m_data += sizeof(*ip6);
869a9771948SGleb Smirnoff 		ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip6));
870a9771948SGleb Smirnoff 		m->m_data -= sizeof(*ip6);
871a9771948SGleb Smirnoff 
8726bf65bcfSRobert Watson 		CARPSTATS_INC(carps_opackets6);
873a9771948SGleb Smirnoff 
874*08b68b0eSGleb Smirnoff 		if (ip6_output(m, NULL, NULL, 0,
875*08b68b0eSGleb Smirnoff 		    &sc->sc_carpdev->if_carp->cif_im6o, NULL, NULL)) {
876a9771948SGleb Smirnoff 			if (sc->sc_sendad_errors < INT_MAX)
877a9771948SGleb Smirnoff 				sc->sc_sendad_errors++;
878a9771948SGleb Smirnoff 			if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
879a9771948SGleb Smirnoff 				carp_suppress_preempt++;
880*08b68b0eSGleb Smirnoff 				if (carp_suppress_preempt == 1)
881*08b68b0eSGleb Smirnoff 					carp_send_ad_all(sc);
882a9771948SGleb Smirnoff 			}
883a9771948SGleb Smirnoff 			sc->sc_sendad_success = 0;
884a9771948SGleb Smirnoff 		} else {
885a9771948SGleb Smirnoff 			if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
886a9771948SGleb Smirnoff 				if (++sc->sc_sendad_success >=
887a9771948SGleb Smirnoff 				    CARP_SENDAD_MIN_SUCCESS) {
888a9771948SGleb Smirnoff 					carp_suppress_preempt--;
889a9771948SGleb Smirnoff 					sc->sc_sendad_errors = 0;
890a9771948SGleb Smirnoff 				}
891a9771948SGleb Smirnoff 			} else
892a9771948SGleb Smirnoff 				sc->sc_sendad_errors = 0;
893a9771948SGleb Smirnoff 		}
894a9771948SGleb Smirnoff 	}
895a9771948SGleb Smirnoff #endif /* INET6 */
896a9771948SGleb Smirnoff 
897*08b68b0eSGleb Smirnoff 	callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), carp_send_ad, sc);
898*08b68b0eSGleb Smirnoff }
899a9771948SGleb Smirnoff 
900*08b68b0eSGleb Smirnoff static void
901*08b68b0eSGleb Smirnoff carp_addroute(struct carp_softc *sc)
902*08b68b0eSGleb Smirnoff {
903*08b68b0eSGleb Smirnoff 	struct ifaddr *ifa;
904*08b68b0eSGleb Smirnoff 
905*08b68b0eSGleb Smirnoff 	CARP_FOREACH_IFA(sc, ifa)
906*08b68b0eSGleb Smirnoff 		switch (ifa->ifa_addr->sa_family) {
907*08b68b0eSGleb Smirnoff #ifdef INET
908*08b68b0eSGleb Smirnoff 		case AF_INET:
909*08b68b0eSGleb Smirnoff 			in_addprefix(ifatoia(ifa), RTF_UP);
910*08b68b0eSGleb Smirnoff 			ifa_add_loopback_route(ifa,
911*08b68b0eSGleb Smirnoff 			    (struct sockaddr *)&ifatoia(ifa)->ia_addr);
912*08b68b0eSGleb Smirnoff 			break;
913*08b68b0eSGleb Smirnoff #endif
914*08b68b0eSGleb Smirnoff #ifdef INET6
915*08b68b0eSGleb Smirnoff 		case AF_INET6:
916*08b68b0eSGleb Smirnoff 			ifa_add_loopback_route(ifa,
917*08b68b0eSGleb Smirnoff 			    (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
918*08b68b0eSGleb Smirnoff 			in6_ifaddloop(ifa);
919*08b68b0eSGleb Smirnoff 			break;
920*08b68b0eSGleb Smirnoff #endif
921*08b68b0eSGleb Smirnoff 		}
922*08b68b0eSGleb Smirnoff }
923*08b68b0eSGleb Smirnoff 
924*08b68b0eSGleb Smirnoff static void
925*08b68b0eSGleb Smirnoff carp_delroute(struct carp_softc *sc)
926*08b68b0eSGleb Smirnoff {
927*08b68b0eSGleb Smirnoff 	struct ifaddr *ifa;
928*08b68b0eSGleb Smirnoff 
929*08b68b0eSGleb Smirnoff 	CARP_FOREACH_IFA(sc, ifa)
930*08b68b0eSGleb Smirnoff 		switch (ifa->ifa_addr->sa_family) {
931*08b68b0eSGleb Smirnoff #ifdef INET
932*08b68b0eSGleb Smirnoff 		case AF_INET:
933*08b68b0eSGleb Smirnoff 			ifa_del_loopback_route(ifa,
934*08b68b0eSGleb Smirnoff 			    (struct sockaddr *)&ifatoia(ifa)->ia_addr);
935*08b68b0eSGleb Smirnoff 			in_scrubprefix(ifatoia(ifa), LLE_STATIC);
936*08b68b0eSGleb Smirnoff 			break;
937*08b68b0eSGleb Smirnoff #endif
938*08b68b0eSGleb Smirnoff #ifdef INET6
939*08b68b0eSGleb Smirnoff 		case AF_INET6:
940*08b68b0eSGleb Smirnoff 			ifa_del_loopback_route(ifa,
941*08b68b0eSGleb Smirnoff 			    (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
942*08b68b0eSGleb Smirnoff 			in6_ifremloop(ifa);
943*08b68b0eSGleb Smirnoff 			break;
944*08b68b0eSGleb Smirnoff #endif
945*08b68b0eSGleb Smirnoff 		}
946a9771948SGleb Smirnoff }
947a9771948SGleb Smirnoff 
948a0ae8f04SBjoern A. Zeeb #ifdef INET
949a9771948SGleb Smirnoff /*
950a9771948SGleb Smirnoff  * Broadcast a gratuitous ARP request containing
951a9771948SGleb Smirnoff  * the virtual router MAC address for each IP address
952a9771948SGleb Smirnoff  * associated with the virtual router.
953a9771948SGleb Smirnoff  */
9545c1f0f6dSGleb Smirnoff static void
955a9771948SGleb Smirnoff carp_send_arp(struct carp_softc *sc)
956a9771948SGleb Smirnoff {
957a9771948SGleb Smirnoff 	struct ifaddr *ifa;
958a9771948SGleb Smirnoff 
959*08b68b0eSGleb Smirnoff 	CARP_FOREACH_IFA(sc, ifa)
960*08b68b0eSGleb Smirnoff 		if (ifa->ifa_addr->sa_family == AF_INET)
961*08b68b0eSGleb Smirnoff 			arp_ifinit2(sc->sc_carpdev, ifa, LLADDR(&sc->sc_addr));
962a9771948SGleb Smirnoff }
963*08b68b0eSGleb Smirnoff 
964*08b68b0eSGleb Smirnoff int
965*08b68b0eSGleb Smirnoff carp_iamatch(struct ifaddr *ifa, uint8_t **enaddr)
966*08b68b0eSGleb Smirnoff {
967*08b68b0eSGleb Smirnoff 	struct carp_softc *sc = ifa->ifa_carp;
968*08b68b0eSGleb Smirnoff 
969*08b68b0eSGleb Smirnoff 	if (sc->sc_state == MASTER) {
970*08b68b0eSGleb Smirnoff 		*enaddr = LLADDR(&sc->sc_addr);
971*08b68b0eSGleb Smirnoff 		return (1);
972*08b68b0eSGleb Smirnoff 	}
973*08b68b0eSGleb Smirnoff 
974*08b68b0eSGleb Smirnoff 	return (0);
975a9771948SGleb Smirnoff }
976a0ae8f04SBjoern A. Zeeb #endif
977a9771948SGleb Smirnoff 
978a9771948SGleb Smirnoff #ifdef INET6
9795c1f0f6dSGleb Smirnoff static void
980a9771948SGleb Smirnoff carp_send_na(struct carp_softc *sc)
981a9771948SGleb Smirnoff {
982*08b68b0eSGleb Smirnoff 	static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
983a9771948SGleb Smirnoff 	struct ifaddr *ifa;
984a9771948SGleb Smirnoff 	struct in6_addr *in6;
985a9771948SGleb Smirnoff 
986*08b68b0eSGleb Smirnoff 	CARP_FOREACH_IFA(sc, ifa) {
987a9771948SGleb Smirnoff 		if (ifa->ifa_addr->sa_family != AF_INET6)
988a9771948SGleb Smirnoff 			continue;
989a9771948SGleb Smirnoff 
990*08b68b0eSGleb Smirnoff 		in6 = IFA_IN6(ifa);
991e8c34a71SGleb Smirnoff 		nd6_na_output(sc->sc_carpdev, &mcast, in6,
992a9771948SGleb Smirnoff 		    ND_NA_FLAG_OVERRIDE, 1, NULL);
993a9771948SGleb Smirnoff 		DELAY(1000);	/* XXX */
994a9771948SGleb Smirnoff 	}
995a9771948SGleb Smirnoff }
996a9771948SGleb Smirnoff 
997a4e53905SMax Laier struct ifaddr *
99854bfbd51SWill Andrews carp_iamatch6(struct ifnet *ifp, struct in6_addr *taddr)
999a9771948SGleb Smirnoff {
1000a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1001a9771948SGleb Smirnoff 
1002*08b68b0eSGleb Smirnoff 	IF_ADDR_LOCK(ifp);
1003*08b68b0eSGleb Smirnoff 	IFNET_FOREACH_IFA(ifp, ifa)
1004*08b68b0eSGleb Smirnoff 		if (ifa->ifa_addr->sa_family == AF_INET6 &&
1005*08b68b0eSGleb Smirnoff 		    ifa->ifa_carp->sc_state == MASTER &&
1006*08b68b0eSGleb Smirnoff 		    IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) {
10078c0fec80SRobert Watson 			ifa_ref(ifa);
1008*08b68b0eSGleb Smirnoff 			IF_ADDR_UNLOCK(ifp);
1009a9771948SGleb Smirnoff 			return (ifa);
1010a9771948SGleb Smirnoff 		}
1011*08b68b0eSGleb Smirnoff 	IF_ADDR_UNLOCK(ifp);
1012a9771948SGleb Smirnoff 
1013a9771948SGleb Smirnoff 	return (NULL);
1014a9771948SGleb Smirnoff }
1015a9771948SGleb Smirnoff 
101654bfbd51SWill Andrews caddr_t
101754bfbd51SWill Andrews carp_macmatch6(struct ifnet *ifp, struct mbuf *m, const struct in6_addr *taddr)
1018a9771948SGleb Smirnoff {
1019a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1020a9771948SGleb Smirnoff 
1021*08b68b0eSGleb Smirnoff 	IF_ADDR_LOCK(ifp);
1022*08b68b0eSGleb Smirnoff 	IFNET_FOREACH_IFA(ifp, ifa)
1023*08b68b0eSGleb Smirnoff 		if (ifa->ifa_addr->sa_family == AF_INET6 &&
1024*08b68b0eSGleb Smirnoff 		    IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) {
1025*08b68b0eSGleb Smirnoff 			struct carp_softc *sc = ifa->ifa_carp;
1026*08b68b0eSGleb Smirnoff 			struct m_tag *mtag;
1027*08b68b0eSGleb Smirnoff 
1028*08b68b0eSGleb Smirnoff 			IF_ADDR_UNLOCK(ifp);
1029*08b68b0eSGleb Smirnoff 
1030a9771948SGleb Smirnoff 			mtag = m_tag_get(PACKET_TAG_CARP,
1031a9771948SGleb Smirnoff 			    sizeof(struct ifnet *), M_NOWAIT);
1032*08b68b0eSGleb Smirnoff 			if (mtag == NULL)
1033*08b68b0eSGleb Smirnoff 				/* Better a bit than nothing. */
1034*08b68b0eSGleb Smirnoff 				return (LLADDR(&sc->sc_addr));
1035*08b68b0eSGleb Smirnoff 
1036a9771948SGleb Smirnoff 			bcopy(&ifp, (caddr_t)(mtag + 1),
1037a9771948SGleb Smirnoff 			    sizeof(struct ifnet *));
1038a9771948SGleb Smirnoff 			m_tag_prepend(m, mtag);
1039a9771948SGleb Smirnoff 
1040*08b68b0eSGleb Smirnoff 			return (LLADDR(&sc->sc_addr));
1041a9771948SGleb Smirnoff 		}
1042*08b68b0eSGleb Smirnoff 	IF_ADDR_UNLOCK(ifp);
1043a9771948SGleb Smirnoff 
1044a9771948SGleb Smirnoff 	return (NULL);
1045a9771948SGleb Smirnoff }
1046*08b68b0eSGleb Smirnoff #endif /* INET6 */
1047a9771948SGleb Smirnoff 
1048*08b68b0eSGleb Smirnoff int
104954bfbd51SWill Andrews carp_forus(struct ifnet *ifp, u_char *dhost)
1050a9771948SGleb Smirnoff {
1051*08b68b0eSGleb Smirnoff 	struct carp_softc *sc;
1052*08b68b0eSGleb Smirnoff 	uint8_t *ena = dhost;
1053a9771948SGleb Smirnoff 
1054a9771948SGleb Smirnoff 	if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1055*08b68b0eSGleb Smirnoff 		return (0);
1056a9771948SGleb Smirnoff 
1057*08b68b0eSGleb Smirnoff 	CIF_LOCK(ifp->if_carp);
1058*08b68b0eSGleb Smirnoff 	IFNET_FOREACH_CARP(ifp, sc) {
1059*08b68b0eSGleb Smirnoff 		CARP_LOCK(sc);
1060*08b68b0eSGleb Smirnoff 		if (sc->sc_state == MASTER && !bcmp(dhost, LLADDR(&sc->sc_addr),
1061*08b68b0eSGleb Smirnoff 		    ETHER_ADDR_LEN)) {
1062*08b68b0eSGleb Smirnoff 			CARP_UNLOCK(sc);
1063*08b68b0eSGleb Smirnoff 			CIF_UNLOCK(ifp->if_carp);
1064*08b68b0eSGleb Smirnoff 			return (1);
1065a9771948SGleb Smirnoff 		}
1066*08b68b0eSGleb Smirnoff 		CARP_UNLOCK(sc);
1067*08b68b0eSGleb Smirnoff 	}
1068*08b68b0eSGleb Smirnoff 	CIF_UNLOCK(ifp->if_carp);
1069a9771948SGleb Smirnoff 
1070*08b68b0eSGleb Smirnoff 	return (0);
1071a9771948SGleb Smirnoff }
1072a9771948SGleb Smirnoff 
10735c1f0f6dSGleb Smirnoff static void
1074a9771948SGleb Smirnoff carp_master_down(void *v)
1075a9771948SGleb Smirnoff {
1076a9771948SGleb Smirnoff 	struct carp_softc *sc = v;
1077a9771948SGleb Smirnoff 
1078*08b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
1079*08b68b0eSGleb Smirnoff 
1080*08b68b0eSGleb Smirnoff 	if (sc->sc_state == BACKUP) {
1081*08b68b0eSGleb Smirnoff 		CARP_LOG("VHID %u@%s: BACKUP -> MASTER (preempting)\n",
1082*08b68b0eSGleb Smirnoff 		    sc->sc_vhid,
1083*08b68b0eSGleb Smirnoff 		    sc->sc_carpdev->if_xname);
1084d220759bSGleb Smirnoff 		carp_master_down_locked(sc);
1085*08b68b0eSGleb Smirnoff 	}
1086*08b68b0eSGleb Smirnoff 
1087*08b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
1088d220759bSGleb Smirnoff }
1089d220759bSGleb Smirnoff 
1090d220759bSGleb Smirnoff static void
1091d220759bSGleb Smirnoff carp_master_down_locked(struct carp_softc *sc)
1092d220759bSGleb Smirnoff {
1093*08b68b0eSGleb Smirnoff 
1094*08b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
1095d220759bSGleb Smirnoff 
1096a9771948SGleb Smirnoff 	switch (sc->sc_state) {
1097a9771948SGleb Smirnoff 	case BACKUP:
1098a9771948SGleb Smirnoff 		carp_set_state(sc, MASTER);
1099d220759bSGleb Smirnoff 		carp_send_ad_locked(sc);
1100a0ae8f04SBjoern A. Zeeb #ifdef INET
1101a9771948SGleb Smirnoff 		carp_send_arp(sc);
1102a0ae8f04SBjoern A. Zeeb #endif
1103a9771948SGleb Smirnoff #ifdef INET6
1104a9771948SGleb Smirnoff 		carp_send_na(sc);
1105*08b68b0eSGleb Smirnoff #endif
1106a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
1107*08b68b0eSGleb Smirnoff 		carp_addroute(sc);
1108*08b68b0eSGleb Smirnoff 		break;
1109*08b68b0eSGleb Smirnoff 	case INIT:
1110*08b68b0eSGleb Smirnoff 	case MASTER:
1111*08b68b0eSGleb Smirnoff #ifdef INVARIANTS
1112*08b68b0eSGleb Smirnoff 		panic("carp: VHID %u@%s: master_down event in %s state\n",
1113*08b68b0eSGleb Smirnoff 		    sc->sc_vhid,
1114*08b68b0eSGleb Smirnoff 		    sc->sc_carpdev->if_xname,
1115*08b68b0eSGleb Smirnoff 		    sc->sc_state ? "MASTER" : "INIT");
1116*08b68b0eSGleb Smirnoff #endif
1117a9771948SGleb Smirnoff 		break;
1118a9771948SGleb Smirnoff 	}
1119a9771948SGleb Smirnoff }
1120a9771948SGleb Smirnoff 
1121a9771948SGleb Smirnoff /*
1122a9771948SGleb Smirnoff  * When in backup state, af indicates whether to reset the master down timer
1123a9771948SGleb Smirnoff  * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1124a9771948SGleb Smirnoff  */
11255c1f0f6dSGleb Smirnoff static void
1126a9771948SGleb Smirnoff carp_setrun(struct carp_softc *sc, sa_family_t af)
1127a9771948SGleb Smirnoff {
1128a9771948SGleb Smirnoff 	struct timeval tv;
1129a9771948SGleb Smirnoff 
1130*08b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
1131d220759bSGleb Smirnoff 
1132*08b68b0eSGleb Smirnoff 	if ((sc->sc_carpdev->if_flags & IFF_UP) == 0 ||
1133*08b68b0eSGleb Smirnoff 	    sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1134*08b68b0eSGleb Smirnoff 	    (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0))
1135a9771948SGleb Smirnoff 		return;
1136a9771948SGleb Smirnoff 
1137a9771948SGleb Smirnoff 	switch (sc->sc_state) {
1138a9771948SGleb Smirnoff 	case INIT:
1139*08b68b0eSGleb Smirnoff 		CARP_LOG("VHID %u@%s: INIT -> BACKUP\n",
1140*08b68b0eSGleb Smirnoff 		    sc->sc_vhid,
1141*08b68b0eSGleb Smirnoff 		    sc->sc_carpdev->if_xname);
1142a9771948SGleb Smirnoff 		carp_set_state(sc, BACKUP);
1143a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
1144a9771948SGleb Smirnoff 		break;
1145a9771948SGleb Smirnoff 	case BACKUP:
1146a9771948SGleb Smirnoff 		callout_stop(&sc->sc_ad_tmo);
1147a9771948SGleb Smirnoff 		tv.tv_sec = 3 * sc->sc_advbase;
1148a9771948SGleb Smirnoff 		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1149a9771948SGleb Smirnoff 		switch (af) {
1150a9771948SGleb Smirnoff #ifdef INET
1151a9771948SGleb Smirnoff 		case AF_INET:
1152a9771948SGleb Smirnoff 			callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1153a9771948SGleb Smirnoff 			    carp_master_down, sc);
1154a9771948SGleb Smirnoff 			break;
1155*08b68b0eSGleb Smirnoff #endif
1156a9771948SGleb Smirnoff #ifdef INET6
1157a9771948SGleb Smirnoff 		case AF_INET6:
1158a9771948SGleb Smirnoff 			callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1159a9771948SGleb Smirnoff 			    carp_master_down, sc);
1160a9771948SGleb Smirnoff 			break;
1161*08b68b0eSGleb Smirnoff #endif
1162a9771948SGleb Smirnoff 		default:
1163*08b68b0eSGleb Smirnoff #ifdef INET
1164a9771948SGleb Smirnoff 			if (sc->sc_naddrs)
1165a9771948SGleb Smirnoff 				callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1166a9771948SGleb Smirnoff 				    carp_master_down, sc);
1167*08b68b0eSGleb Smirnoff #endif
1168*08b68b0eSGleb Smirnoff #ifdef INET6
1169a9771948SGleb Smirnoff 			if (sc->sc_naddrs6)
1170a9771948SGleb Smirnoff 				callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1171a9771948SGleb Smirnoff 				    carp_master_down, sc);
1172*08b68b0eSGleb Smirnoff #endif
1173a9771948SGleb Smirnoff 			break;
1174a9771948SGleb Smirnoff 		}
1175a9771948SGleb Smirnoff 		break;
1176a9771948SGleb Smirnoff 	case MASTER:
1177a9771948SGleb Smirnoff 		tv.tv_sec = sc->sc_advbase;
1178a9771948SGleb Smirnoff 		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1179a9771948SGleb Smirnoff 		callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1180a9771948SGleb Smirnoff 		    carp_send_ad, sc);
1181a9771948SGleb Smirnoff 		break;
1182a9771948SGleb Smirnoff 	}
1183a9771948SGleb Smirnoff }
1184a9771948SGleb Smirnoff 
1185*08b68b0eSGleb Smirnoff /*
1186*08b68b0eSGleb Smirnoff  * Setup multicast structures.
1187*08b68b0eSGleb Smirnoff  */
11885c1f0f6dSGleb Smirnoff static int
1189*08b68b0eSGleb Smirnoff carp_multicast_setup(struct carp_softc *sc, sa_family_t sa)
1190a9771948SGleb Smirnoff {
1191*08b68b0eSGleb Smirnoff 	struct ifnet *ifp = sc->sc_carpdev;
1192*08b68b0eSGleb Smirnoff 	struct carp_if *cif = ifp->if_carp;
1193*08b68b0eSGleb Smirnoff 	int error = 0;
1194*08b68b0eSGleb Smirnoff 
1195*08b68b0eSGleb Smirnoff 	switch (sa) {
1196*08b68b0eSGleb Smirnoff #ifdef INET
1197*08b68b0eSGleb Smirnoff 	case AF_INET:
1198*08b68b0eSGleb Smirnoff 	    {
1199*08b68b0eSGleb Smirnoff 		struct ip_moptions *imo = &cif->cif_imo;
1200a9771948SGleb Smirnoff 		struct in_addr addr;
1201a9771948SGleb Smirnoff 
1202*08b68b0eSGleb Smirnoff 		if (imo->imo_membership)
1203a9771948SGleb Smirnoff 			return (0);
1204a9771948SGleb Smirnoff 
1205*08b68b0eSGleb Smirnoff 		imo->imo_membership = (struct in_multi **)malloc(
1206*08b68b0eSGleb Smirnoff 		    (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_CARP,
1207*08b68b0eSGleb Smirnoff 		    M_WAITOK);
1208*08b68b0eSGleb Smirnoff 		imo->imo_mfilters = NULL;
1209*08b68b0eSGleb Smirnoff 		imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
1210*08b68b0eSGleb Smirnoff 		imo->imo_multicast_vif = -1;
1211a9771948SGleb Smirnoff 
1212a9771948SGleb Smirnoff 		addr.s_addr = htonl(INADDR_CARP_GROUP);
1213*08b68b0eSGleb Smirnoff 		if ((error = in_joingroup(ifp, &addr, NULL,
1214*08b68b0eSGleb Smirnoff 		    &imo->imo_membership[0])) != 0) {
1215*08b68b0eSGleb Smirnoff 			free(imo->imo_membership, M_CARP);
1216*08b68b0eSGleb Smirnoff 			break;
12172d9cfabaSRobert Watson 		}
1218a9771948SGleb Smirnoff 		imo->imo_num_memberships++;
1219a9771948SGleb Smirnoff 		imo->imo_multicast_ifp = ifp;
1220a9771948SGleb Smirnoff 		imo->imo_multicast_ttl = CARP_DFLTTL;
1221a9771948SGleb Smirnoff 		imo->imo_multicast_loop = 0;
1222a9771948SGleb Smirnoff 		break;
1223a9771948SGleb Smirnoff 	   }
1224*08b68b0eSGleb Smirnoff #endif
1225*08b68b0eSGleb Smirnoff #ifdef INET6
1226*08b68b0eSGleb Smirnoff 	case AF_INET6:
1227*08b68b0eSGleb Smirnoff 	    {
1228*08b68b0eSGleb Smirnoff 		struct ip6_moptions *im6o = &cif->cif_im6o;
1229*08b68b0eSGleb Smirnoff 		struct in6_addr in6;
123033cde130SBruce M Simpson 		struct in6_multi *in6m;
123133cde130SBruce M Simpson 
1232*08b68b0eSGleb Smirnoff 		if (im6o->im6o_membership)
1233*08b68b0eSGleb Smirnoff 			return (0);
1234*08b68b0eSGleb Smirnoff 
1235*08b68b0eSGleb Smirnoff 		im6o->im6o_membership = (struct in6_multi **)malloc(
1236*08b68b0eSGleb Smirnoff 		    (sizeof(struct in6_multi *) * IPV6_MIN_MEMBERSHIPS), M_CARP,
1237*08b68b0eSGleb Smirnoff 		    M_ZERO|M_WAITOK);
1238*08b68b0eSGleb Smirnoff 		im6o->im6o_mfilters = NULL;
1239*08b68b0eSGleb Smirnoff 		im6o->im6o_max_memberships = IPV6_MIN_MEMBERSHIPS;
1240*08b68b0eSGleb Smirnoff 		im6o->im6o_multicast_hlim = CARP_DFLTTL;
1241a9771948SGleb Smirnoff 		im6o->im6o_multicast_ifp = ifp;
1242a9771948SGleb Smirnoff 
1243*08b68b0eSGleb Smirnoff 		/* Join IPv6 CARP multicast group. */
1244a1f7e5f8SHajimu UMEMOTO 		bzero(&in6, sizeof(in6));
1245a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr16[0] = htons(0xff02);
1246a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr8[15] = 0x12;
1247*08b68b0eSGleb Smirnoff 		if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1248*08b68b0eSGleb Smirnoff 			free(im6o->im6o_membership, M_CARP);
1249*08b68b0eSGleb Smirnoff 			break;
1250*08b68b0eSGleb Smirnoff 		}
125133cde130SBruce M Simpson 		in6m = NULL;
1252*08b68b0eSGleb Smirnoff 		if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
1253*08b68b0eSGleb Smirnoff 			free(im6o->im6o_membership, M_CARP);
1254*08b68b0eSGleb Smirnoff 			break;
1255*08b68b0eSGleb Smirnoff 		}
125633cde130SBruce M Simpson 		im6o->im6o_membership[0] = in6m;
125733cde130SBruce M Simpson 		im6o->im6o_num_memberships++;
1258a9771948SGleb Smirnoff 
1259*08b68b0eSGleb Smirnoff 		/* Join solicited multicast address. */
1260a1f7e5f8SHajimu UMEMOTO 		bzero(&in6, sizeof(in6));
1261a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr16[0] = htons(0xff02);
1262a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr32[1] = 0;
1263a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr32[2] = htonl(1);
1264*08b68b0eSGleb Smirnoff 		in6.s6_addr32[3] = 0;
1265a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr8[12] = 0xff;
1266*08b68b0eSGleb Smirnoff 		if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1267*08b68b0eSGleb Smirnoff 			in6_mc_leave(im6o->im6o_membership[0], NULL);
1268*08b68b0eSGleb Smirnoff 			free(im6o->im6o_membership, M_CARP);
1269*08b68b0eSGleb Smirnoff 			break;
1270*08b68b0eSGleb Smirnoff 		}
127133cde130SBruce M Simpson 		in6m = NULL;
1272*08b68b0eSGleb Smirnoff 		if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
1273*08b68b0eSGleb Smirnoff 			in6_mc_leave(im6o->im6o_membership[0], NULL);
1274*08b68b0eSGleb Smirnoff 			free(im6o->im6o_membership, M_CARP);
1275*08b68b0eSGleb Smirnoff 			break;
1276*08b68b0eSGleb Smirnoff 		}
127733cde130SBruce M Simpson 		im6o->im6o_membership[1] = in6m;
127833cde130SBruce M Simpson 		im6o->im6o_num_memberships++;
1279a9771948SGleb Smirnoff 		break;
1280a9771948SGleb Smirnoff 	    }
1281a9771948SGleb Smirnoff #endif
1282*08b68b0eSGleb Smirnoff 	}
1283*08b68b0eSGleb Smirnoff 
1284*08b68b0eSGleb Smirnoff 	return (error);
1285a9771948SGleb Smirnoff }
1286a9771948SGleb Smirnoff 
1287a9771948SGleb Smirnoff /*
1288*08b68b0eSGleb Smirnoff  * Free multicast structures.
1289a9771948SGleb Smirnoff  */
12905c1f0f6dSGleb Smirnoff static void
1291*08b68b0eSGleb Smirnoff carp_multicast_cleanup(struct carp_softc *sc, sa_family_t sa)
1292a9771948SGleb Smirnoff {
1293*08b68b0eSGleb Smirnoff 	struct ifnet *ifp = sc->sc_carpdev;
1294*08b68b0eSGleb Smirnoff 	struct carp_if *cif = ifp->if_carp;
1295*08b68b0eSGleb Smirnoff 
1296*08b68b0eSGleb Smirnoff 	switch (sa) {
1297*08b68b0eSGleb Smirnoff #ifdef INET
1298*08b68b0eSGleb Smirnoff 	case AF_INET:
1299*08b68b0eSGleb Smirnoff 		if (sc->sc_naddrs == 0) {
1300*08b68b0eSGleb Smirnoff 			struct ip_moptions *imo = &cif->cif_imo;
1301*08b68b0eSGleb Smirnoff 
1302*08b68b0eSGleb Smirnoff 			in_leavegroup(imo->imo_membership[0], NULL);
1303*08b68b0eSGleb Smirnoff 			KASSERT(imo->imo_mfilters == NULL,
1304*08b68b0eSGleb Smirnoff 			    ("%s: imo_mfilters != NULL", __func__));
1305*08b68b0eSGleb Smirnoff 			free(imo->imo_membership, M_CARP);
1306*08b68b0eSGleb Smirnoff 			imo->imo_membership = NULL;
1307*08b68b0eSGleb Smirnoff 
1308*08b68b0eSGleb Smirnoff 		}
1309*08b68b0eSGleb Smirnoff 		break;
1310a9771948SGleb Smirnoff #endif
1311*08b68b0eSGleb Smirnoff #ifdef INET6
1312*08b68b0eSGleb Smirnoff 	case AF_INET6:
1313*08b68b0eSGleb Smirnoff 		if (sc->sc_naddrs6 == 0) {
1314*08b68b0eSGleb Smirnoff 			struct ip6_moptions *im6o = &cif->cif_im6o;
1315*08b68b0eSGleb Smirnoff 
1316*08b68b0eSGleb Smirnoff 			in6_mc_leave(im6o->im6o_membership[0], NULL);
1317*08b68b0eSGleb Smirnoff 			in6_mc_leave(im6o->im6o_membership[1], NULL);
1318*08b68b0eSGleb Smirnoff 			KASSERT(im6o->im6o_mfilters == NULL,
1319*08b68b0eSGleb Smirnoff 			    ("%s: im6o_mfilters != NULL", __func__));
1320*08b68b0eSGleb Smirnoff 			free(im6o->im6o_membership, M_CARP);
1321*08b68b0eSGleb Smirnoff 			im6o->im6o_membership = NULL;
1322*08b68b0eSGleb Smirnoff 		}
1323*08b68b0eSGleb Smirnoff 		break;
1324*08b68b0eSGleb Smirnoff #endif
1325*08b68b0eSGleb Smirnoff 	}
1326a9771948SGleb Smirnoff }
1327a9771948SGleb Smirnoff 
1328a9771948SGleb Smirnoff int
1329*08b68b0eSGleb Smirnoff carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa)
1330a9771948SGleb Smirnoff {
1331a9771948SGleb Smirnoff 	struct m_tag *mtag;
1332a9771948SGleb Smirnoff 	struct carp_softc *sc;
1333a9771948SGleb Smirnoff 
1334a9771948SGleb Smirnoff 	if (!sa)
1335a9771948SGleb Smirnoff 		return (0);
1336a9771948SGleb Smirnoff 
1337a9771948SGleb Smirnoff 	switch (sa->sa_family) {
1338a9771948SGleb Smirnoff #ifdef INET
1339a9771948SGleb Smirnoff 	case AF_INET:
1340a9771948SGleb Smirnoff 		break;
1341*08b68b0eSGleb Smirnoff #endif
1342a9771948SGleb Smirnoff #ifdef INET6
1343a9771948SGleb Smirnoff 	case AF_INET6:
1344a9771948SGleb Smirnoff 		break;
1345*08b68b0eSGleb Smirnoff #endif
1346a9771948SGleb Smirnoff 	default:
1347a9771948SGleb Smirnoff 		return (0);
1348a9771948SGleb Smirnoff 	}
1349a9771948SGleb Smirnoff 
1350a9771948SGleb Smirnoff 	mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
1351a9771948SGleb Smirnoff 	if (mtag == NULL)
1352a9771948SGleb Smirnoff 		return (0);
1353a9771948SGleb Smirnoff 
1354*08b68b0eSGleb Smirnoff 	bcopy(mtag + 1, &sc, sizeof(struct carp_softc *));
1355a9771948SGleb Smirnoff 
1356*08b68b0eSGleb Smirnoff 	/* Set the source MAC address to the Virtual Router MAC Address. */
1357a9771948SGleb Smirnoff 	switch (ifp->if_type) {
1358630481bbSYaroslav Tykhiy 	case IFT_ETHER:
1359630481bbSYaroslav Tykhiy 	case IFT_L2VLAN: {
1360a9771948SGleb Smirnoff 			struct ether_header *eh;
1361a9771948SGleb Smirnoff 
1362a9771948SGleb Smirnoff 			eh = mtod(m, struct ether_header *);
1363a9771948SGleb Smirnoff 			eh->ether_shost[0] = 0;
1364a9771948SGleb Smirnoff 			eh->ether_shost[1] = 0;
1365a9771948SGleb Smirnoff 			eh->ether_shost[2] = 0x5e;
1366a9771948SGleb Smirnoff 			eh->ether_shost[3] = 0;
1367a9771948SGleb Smirnoff 			eh->ether_shost[4] = 1;
1368a9771948SGleb Smirnoff 			eh->ether_shost[5] = sc->sc_vhid;
1369a9771948SGleb Smirnoff 		}
1370a9771948SGleb Smirnoff 		break;
1371a9771948SGleb Smirnoff 	case IFT_FDDI: {
1372a9771948SGleb Smirnoff 			struct fddi_header *fh;
1373a9771948SGleb Smirnoff 
1374a9771948SGleb Smirnoff 			fh = mtod(m, struct fddi_header *);
1375a9771948SGleb Smirnoff 			fh->fddi_shost[0] = 0;
1376a9771948SGleb Smirnoff 			fh->fddi_shost[1] = 0;
1377a9771948SGleb Smirnoff 			fh->fddi_shost[2] = 0x5e;
1378a9771948SGleb Smirnoff 			fh->fddi_shost[3] = 0;
1379a9771948SGleb Smirnoff 			fh->fddi_shost[4] = 1;
1380a9771948SGleb Smirnoff 			fh->fddi_shost[5] = sc->sc_vhid;
1381a9771948SGleb Smirnoff 		}
1382a9771948SGleb Smirnoff 		break;
1383a9771948SGleb Smirnoff 	case IFT_ISO88025: {
1384a9771948SGleb Smirnoff  			struct iso88025_header *th;
1385a9771948SGleb Smirnoff  			th = mtod(m, struct iso88025_header *);
1386a9771948SGleb Smirnoff 			th->iso88025_shost[0] = 3;
1387a9771948SGleb Smirnoff 			th->iso88025_shost[1] = 0;
1388a9771948SGleb Smirnoff 			th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1);
1389a9771948SGleb Smirnoff 			th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1);
1390a9771948SGleb Smirnoff 			th->iso88025_shost[4] = 0;
1391a9771948SGleb Smirnoff 			th->iso88025_shost[5] = 0;
1392a9771948SGleb Smirnoff 		}
1393a9771948SGleb Smirnoff 		break;
1394a9771948SGleb Smirnoff 	default:
1395*08b68b0eSGleb Smirnoff 		printf("%s: carp is not supported for the %d interface type\n",
1396*08b68b0eSGleb Smirnoff 		    ifp->if_xname, ifp->if_type);
1397a9771948SGleb Smirnoff 		return (EOPNOTSUPP);
1398a9771948SGleb Smirnoff 	}
1399a9771948SGleb Smirnoff 
1400a9771948SGleb Smirnoff 	return (0);
1401a9771948SGleb Smirnoff }
1402a9771948SGleb Smirnoff 
1403*08b68b0eSGleb Smirnoff static struct carp_softc*
1404*08b68b0eSGleb Smirnoff carp_alloc(struct ifnet *ifp)
1405a9771948SGleb Smirnoff {
1406*08b68b0eSGleb Smirnoff 	struct carp_softc *sc;
1407*08b68b0eSGleb Smirnoff 	struct carp_if *cif;
1408d220759bSGleb Smirnoff 
1409*08b68b0eSGleb Smirnoff 	if ((cif = ifp->if_carp) == NULL) {
1410*08b68b0eSGleb Smirnoff 		cif = carp_alloc_if(ifp);
1411*08b68b0eSGleb Smirnoff 		if (cif == NULL)
1412*08b68b0eSGleb Smirnoff 			return (NULL);
1413a9771948SGleb Smirnoff 	}
1414a9771948SGleb Smirnoff 
1415*08b68b0eSGleb Smirnoff 	sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
1416*08b68b0eSGleb Smirnoff 
1417*08b68b0eSGleb Smirnoff 	sc->sc_advbase = CARP_DFLTINTV;
1418*08b68b0eSGleb Smirnoff 	sc->sc_vhid = -1;	/* required setting */
1419*08b68b0eSGleb Smirnoff 	sc->sc_init_counter = 1;
1420*08b68b0eSGleb Smirnoff 	sc->sc_state = INIT;
1421*08b68b0eSGleb Smirnoff 
1422*08b68b0eSGleb Smirnoff 	sc->sc_ifasiz = sizeof(struct ifaddr *);
1423*08b68b0eSGleb Smirnoff 	sc->sc_ifas = malloc(sc->sc_ifasiz, M_CARP, M_WAITOK|M_ZERO);
1424*08b68b0eSGleb Smirnoff 	sc->sc_carpdev = ifp;
1425*08b68b0eSGleb Smirnoff 
1426*08b68b0eSGleb Smirnoff 	CARP_LOCK_INIT(sc);
1427*08b68b0eSGleb Smirnoff #ifdef INET
1428*08b68b0eSGleb Smirnoff 	callout_init_mtx(&sc->sc_md_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1429*08b68b0eSGleb Smirnoff #endif
1430*08b68b0eSGleb Smirnoff #ifdef INET6
1431*08b68b0eSGleb Smirnoff 	callout_init_mtx(&sc->sc_md6_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1432*08b68b0eSGleb Smirnoff #endif
1433*08b68b0eSGleb Smirnoff 	callout_init_mtx(&sc->sc_ad_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1434*08b68b0eSGleb Smirnoff 
1435*08b68b0eSGleb Smirnoff 	CIF_LOCK(cif);
1436*08b68b0eSGleb Smirnoff 	TAILQ_INSERT_TAIL(&cif->cif_vrs, sc, sc_list);
1437*08b68b0eSGleb Smirnoff 	CIF_UNLOCK(cif);
1438*08b68b0eSGleb Smirnoff 
1439*08b68b0eSGleb Smirnoff 	mtx_lock(&carp_mtx);
1440*08b68b0eSGleb Smirnoff 	LIST_INSERT_HEAD(&carp_list, sc, sc_next);
1441*08b68b0eSGleb Smirnoff 	mtx_unlock(&carp_mtx);
1442*08b68b0eSGleb Smirnoff 
1443*08b68b0eSGleb Smirnoff 	return (sc);
1444*08b68b0eSGleb Smirnoff }
1445*08b68b0eSGleb Smirnoff 
1446*08b68b0eSGleb Smirnoff static int
1447*08b68b0eSGleb Smirnoff carp_grow_ifas(struct carp_softc *sc)
1448*08b68b0eSGleb Smirnoff {
1449*08b68b0eSGleb Smirnoff 	struct ifaddr **new;
1450*08b68b0eSGleb Smirnoff 
1451*08b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
1452*08b68b0eSGleb Smirnoff 
1453*08b68b0eSGleb Smirnoff 	new = malloc(sc->sc_ifasiz * 2, M_CARP, M_NOWAIT|M_ZERO);
1454*08b68b0eSGleb Smirnoff 	if (new == NULL)
1455*08b68b0eSGleb Smirnoff 		return (ENOMEM);
1456*08b68b0eSGleb Smirnoff 	bcopy(sc->sc_ifas, new, sc->sc_ifasiz);
1457*08b68b0eSGleb Smirnoff 	free(sc->sc_ifas, M_CARP);
1458*08b68b0eSGleb Smirnoff 	sc->sc_ifas = new;
1459*08b68b0eSGleb Smirnoff 	sc->sc_ifasiz *= 2;
1460*08b68b0eSGleb Smirnoff 
1461*08b68b0eSGleb Smirnoff 	return (0);
1462*08b68b0eSGleb Smirnoff }
1463*08b68b0eSGleb Smirnoff 
1464*08b68b0eSGleb Smirnoff static void
1465*08b68b0eSGleb Smirnoff carp_destroy(struct carp_softc *sc)
1466*08b68b0eSGleb Smirnoff {
1467*08b68b0eSGleb Smirnoff 	struct ifnet *ifp = sc->sc_carpdev;
1468*08b68b0eSGleb Smirnoff 	struct carp_if *cif = ifp->if_carp;
1469*08b68b0eSGleb Smirnoff 
1470*08b68b0eSGleb Smirnoff 	CIF_LOCK(cif);
1471*08b68b0eSGleb Smirnoff 	TAILQ_REMOVE(&cif->cif_vrs, sc, sc_list);
1472*08b68b0eSGleb Smirnoff 	if (TAILQ_EMPTY(&cif->cif_vrs))
1473*08b68b0eSGleb Smirnoff 		carp_free_if(cif);
1474*08b68b0eSGleb Smirnoff 	else
1475*08b68b0eSGleb Smirnoff 		CIF_UNLOCK(cif);
1476*08b68b0eSGleb Smirnoff 
1477*08b68b0eSGleb Smirnoff 	mtx_lock(&carp_mtx);
1478*08b68b0eSGleb Smirnoff 	LIST_REMOVE(sc, sc_next);
1479*08b68b0eSGleb Smirnoff 	mtx_unlock(&carp_mtx);
1480*08b68b0eSGleb Smirnoff 
1481*08b68b0eSGleb Smirnoff 	CARP_LOCK(sc);
1482*08b68b0eSGleb Smirnoff 	callout_drain(&sc->sc_ad_tmo);
1483*08b68b0eSGleb Smirnoff #ifdef INET
1484*08b68b0eSGleb Smirnoff 	callout_drain(&sc->sc_md_tmo);
1485*08b68b0eSGleb Smirnoff #endif
1486*08b68b0eSGleb Smirnoff #ifdef INET6
1487*08b68b0eSGleb Smirnoff 	callout_drain(&sc->sc_md6_tmo);
1488*08b68b0eSGleb Smirnoff #endif
1489*08b68b0eSGleb Smirnoff 	CARP_LOCK_DESTROY(sc);
1490*08b68b0eSGleb Smirnoff 
1491*08b68b0eSGleb Smirnoff 	free(sc->sc_ifas, M_CARP);
1492*08b68b0eSGleb Smirnoff 	free(sc, M_CARP);
1493*08b68b0eSGleb Smirnoff }
1494*08b68b0eSGleb Smirnoff 
1495*08b68b0eSGleb Smirnoff static struct carp_if*
1496*08b68b0eSGleb Smirnoff carp_alloc_if(struct ifnet *ifp)
1497a9771948SGleb Smirnoff {
149854bfbd51SWill Andrews 	struct carp_if *cif;
1499d220759bSGleb Smirnoff 
1500*08b68b0eSGleb Smirnoff 	cif = malloc(sizeof(*cif), M_CARP, M_WAITOK|M_ZERO);
1501*08b68b0eSGleb Smirnoff 
1502*08b68b0eSGleb Smirnoff 	if (ifpromisc(ifp, 1) != 0)
1503*08b68b0eSGleb Smirnoff 		goto cleanup;
1504*08b68b0eSGleb Smirnoff 
1505*08b68b0eSGleb Smirnoff 	CIF_LOCK_INIT(cif);
1506*08b68b0eSGleb Smirnoff 	cif->cif_ifp = ifp;
1507*08b68b0eSGleb Smirnoff 	TAILQ_INIT(&cif->cif_vrs);
1508*08b68b0eSGleb Smirnoff 
1509*08b68b0eSGleb Smirnoff 	IF_ADDR_LOCK(ifp);
1510*08b68b0eSGleb Smirnoff 	ifp->if_carp = cif;
1511*08b68b0eSGleb Smirnoff 	if_ref(ifp);
1512*08b68b0eSGleb Smirnoff 	IF_ADDR_UNLOCK(ifp);
1513*08b68b0eSGleb Smirnoff 
1514*08b68b0eSGleb Smirnoff 	return (cif);
1515*08b68b0eSGleb Smirnoff 
1516*08b68b0eSGleb Smirnoff cleanup:
1517*08b68b0eSGleb Smirnoff 	free(cif, M_CARP);
1518*08b68b0eSGleb Smirnoff 
1519*08b68b0eSGleb Smirnoff 	return (NULL);
1520d220759bSGleb Smirnoff }
1521d220759bSGleb Smirnoff 
1522d220759bSGleb Smirnoff static void
1523*08b68b0eSGleb Smirnoff carp_free_if(struct carp_if *cif)
1524*08b68b0eSGleb Smirnoff {
1525*08b68b0eSGleb Smirnoff 	struct ifnet *ifp = cif->cif_ifp;
1526*08b68b0eSGleb Smirnoff 
1527*08b68b0eSGleb Smirnoff 	CIF_LOCK_ASSERT(cif);
1528*08b68b0eSGleb Smirnoff 	KASSERT(TAILQ_EMPTY(&cif->cif_vrs), ("%s: softc list not empty",
1529*08b68b0eSGleb Smirnoff 	    __func__));
1530*08b68b0eSGleb Smirnoff 
1531*08b68b0eSGleb Smirnoff 	IF_ADDR_LOCK(ifp);
1532*08b68b0eSGleb Smirnoff 	ifp->if_carp = NULL;
1533*08b68b0eSGleb Smirnoff 	if_rele(ifp);
1534*08b68b0eSGleb Smirnoff 	IF_ADDR_UNLOCK(ifp);
1535*08b68b0eSGleb Smirnoff 
1536*08b68b0eSGleb Smirnoff 	CIF_LOCK_DESTROY(cif);
1537*08b68b0eSGleb Smirnoff 
1538*08b68b0eSGleb Smirnoff 	ifpromisc(ifp, 0);
1539*08b68b0eSGleb Smirnoff 
1540*08b68b0eSGleb Smirnoff 	free(cif, M_CARP);
1541*08b68b0eSGleb Smirnoff }
1542*08b68b0eSGleb Smirnoff 
1543*08b68b0eSGleb Smirnoff static void
1544*08b68b0eSGleb Smirnoff carp_carprcp(struct carpreq *carpr, struct carp_softc *sc, int priv)
1545*08b68b0eSGleb Smirnoff {
1546*08b68b0eSGleb Smirnoff 
1547*08b68b0eSGleb Smirnoff 	CARP_LOCK(sc);
1548*08b68b0eSGleb Smirnoff 	carpr->carpr_state = sc->sc_state;
1549*08b68b0eSGleb Smirnoff 	carpr->carpr_vhid = sc->sc_vhid;
1550*08b68b0eSGleb Smirnoff 	carpr->carpr_advbase = sc->sc_advbase;
1551*08b68b0eSGleb Smirnoff 	carpr->carpr_advskew = sc->sc_advskew;
1552*08b68b0eSGleb Smirnoff 	if (priv)
1553*08b68b0eSGleb Smirnoff 		bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key));
1554*08b68b0eSGleb Smirnoff 	else
1555*08b68b0eSGleb Smirnoff 		bzero(carpr->carpr_key, sizeof(carpr->carpr_key));
1556*08b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
1557*08b68b0eSGleb Smirnoff }
1558*08b68b0eSGleb Smirnoff 
1559*08b68b0eSGleb Smirnoff int
1560*08b68b0eSGleb Smirnoff carp_ioctl(struct ifreq *ifr, u_long cmd, struct thread *td)
1561*08b68b0eSGleb Smirnoff {
1562*08b68b0eSGleb Smirnoff 	struct carpreq carpr;
1563*08b68b0eSGleb Smirnoff 	struct ifnet *ifp;
1564*08b68b0eSGleb Smirnoff 	struct carp_softc *sc = NULL;
1565*08b68b0eSGleb Smirnoff 	int error = 0, locked = 0;
1566*08b68b0eSGleb Smirnoff 
1567*08b68b0eSGleb Smirnoff 	if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
1568*08b68b0eSGleb Smirnoff 		return (error);
1569*08b68b0eSGleb Smirnoff 
1570*08b68b0eSGleb Smirnoff 	ifp = ifunit_ref(ifr->ifr_name);
1571*08b68b0eSGleb Smirnoff 	if (ifp == NULL)
1572*08b68b0eSGleb Smirnoff 		return (ENXIO);
1573*08b68b0eSGleb Smirnoff 
1574*08b68b0eSGleb Smirnoff 	switch (ifp->if_type) {
1575*08b68b0eSGleb Smirnoff 	case IFT_ETHER:
1576*08b68b0eSGleb Smirnoff 	case IFT_L2VLAN:
1577*08b68b0eSGleb Smirnoff 	case IFT_FDDI:
1578*08b68b0eSGleb Smirnoff 	case IFT_ISO88025:
1579*08b68b0eSGleb Smirnoff 		break;
1580*08b68b0eSGleb Smirnoff 	default:
1581*08b68b0eSGleb Smirnoff 		error = EOPNOTSUPP;
1582*08b68b0eSGleb Smirnoff 		goto out;
1583*08b68b0eSGleb Smirnoff 	}
1584*08b68b0eSGleb Smirnoff 
1585*08b68b0eSGleb Smirnoff 	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1586*08b68b0eSGleb Smirnoff 		error = EADDRNOTAVAIL;
1587*08b68b0eSGleb Smirnoff 		goto out;
1588*08b68b0eSGleb Smirnoff 	}
1589*08b68b0eSGleb Smirnoff 
1590*08b68b0eSGleb Smirnoff 	switch (cmd) {
1591*08b68b0eSGleb Smirnoff 	case SIOCSVH:
1592*08b68b0eSGleb Smirnoff 		if ((error = priv_check(td, PRIV_NETINET_CARP)))
1593*08b68b0eSGleb Smirnoff 			break;
1594*08b68b0eSGleb Smirnoff 		if (carpr.carpr_vhid <= 0 || carpr.carpr_vhid > CARP_MAXVHID ||
1595*08b68b0eSGleb Smirnoff 		    carpr.carpr_advbase < 0 || carpr.carpr_advskew < 0) {
1596*08b68b0eSGleb Smirnoff 			error = EINVAL;
1597*08b68b0eSGleb Smirnoff 			break;
1598*08b68b0eSGleb Smirnoff 		}
1599*08b68b0eSGleb Smirnoff 
1600*08b68b0eSGleb Smirnoff 		if (ifp->if_carp) {
1601*08b68b0eSGleb Smirnoff 			CIF_LOCK(ifp->if_carp);
1602*08b68b0eSGleb Smirnoff 			IFNET_FOREACH_CARP(ifp, sc)
1603*08b68b0eSGleb Smirnoff 				if (sc->sc_vhid == carpr.carpr_vhid)
1604*08b68b0eSGleb Smirnoff 					break;
1605*08b68b0eSGleb Smirnoff 			CIF_UNLOCK(ifp->if_carp);
1606*08b68b0eSGleb Smirnoff 		}
1607*08b68b0eSGleb Smirnoff 		if (sc == NULL) {
1608*08b68b0eSGleb Smirnoff 			sc = carp_alloc(ifp);
1609*08b68b0eSGleb Smirnoff 			if (sc == NULL) {
1610*08b68b0eSGleb Smirnoff 				error = EINVAL; /* XXX: ifpromisc failed */
1611*08b68b0eSGleb Smirnoff 				break;
1612*08b68b0eSGleb Smirnoff 			}
1613*08b68b0eSGleb Smirnoff 
1614*08b68b0eSGleb Smirnoff 			CARP_LOCK(sc);
1615*08b68b0eSGleb Smirnoff 			sc->sc_vhid = carpr.carpr_vhid;
1616*08b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[0] = 0;
1617*08b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[1] = 0;
1618*08b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[2] = 0x5e;
1619*08b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[3] = 0;
1620*08b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[4] = 1;
1621*08b68b0eSGleb Smirnoff 			LLADDR(&sc->sc_addr)[5] = sc->sc_vhid;
1622*08b68b0eSGleb Smirnoff 		} else
1623*08b68b0eSGleb Smirnoff 			CARP_LOCK(sc);
1624*08b68b0eSGleb Smirnoff 		locked = 1;
1625*08b68b0eSGleb Smirnoff 		if (carpr.carpr_advbase > 0) {
1626*08b68b0eSGleb Smirnoff 			if (carpr.carpr_advbase > 255 ||
1627*08b68b0eSGleb Smirnoff 			    carpr.carpr_advbase < CARP_DFLTINTV) {
1628*08b68b0eSGleb Smirnoff 				error = EINVAL;
1629*08b68b0eSGleb Smirnoff 				break;
1630*08b68b0eSGleb Smirnoff 			}
1631*08b68b0eSGleb Smirnoff 			sc->sc_advbase = carpr.carpr_advbase;
1632*08b68b0eSGleb Smirnoff 		}
1633*08b68b0eSGleb Smirnoff 		if (carpr.carpr_advskew > 0) {
1634*08b68b0eSGleb Smirnoff 			if (carpr.carpr_advskew >= 255) {
1635*08b68b0eSGleb Smirnoff 				error = EINVAL;
1636*08b68b0eSGleb Smirnoff 				break;
1637*08b68b0eSGleb Smirnoff 			}
1638*08b68b0eSGleb Smirnoff 			sc->sc_advskew = carpr.carpr_advskew;
1639*08b68b0eSGleb Smirnoff 		}
1640*08b68b0eSGleb Smirnoff 		if (carpr.carpr_key[0] != '\0') {
1641*08b68b0eSGleb Smirnoff 			bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1642*08b68b0eSGleb Smirnoff 			carp_hmac_prepare(sc);
1643*08b68b0eSGleb Smirnoff 		}
1644*08b68b0eSGleb Smirnoff 		if (sc->sc_state != INIT &&
1645*08b68b0eSGleb Smirnoff 		    carpr.carpr_state != sc->sc_state) {
1646*08b68b0eSGleb Smirnoff 			switch (carpr.carpr_state) {
1647*08b68b0eSGleb Smirnoff 			case BACKUP:
1648*08b68b0eSGleb Smirnoff 				callout_stop(&sc->sc_ad_tmo);
1649*08b68b0eSGleb Smirnoff 				carp_set_state(sc, BACKUP);
1650*08b68b0eSGleb Smirnoff 				carp_setrun(sc, 0);
1651*08b68b0eSGleb Smirnoff 				carp_delroute(sc);
1652*08b68b0eSGleb Smirnoff 				break;
1653*08b68b0eSGleb Smirnoff 			case MASTER:
1654*08b68b0eSGleb Smirnoff 				carp_master_down_locked(sc);
1655*08b68b0eSGleb Smirnoff 				break;
1656*08b68b0eSGleb Smirnoff 			default:
1657*08b68b0eSGleb Smirnoff 				break;
1658*08b68b0eSGleb Smirnoff 			}
1659*08b68b0eSGleb Smirnoff 		}
1660*08b68b0eSGleb Smirnoff 		break;
1661*08b68b0eSGleb Smirnoff 
1662*08b68b0eSGleb Smirnoff 	case SIOCGVH:
1663*08b68b0eSGleb Smirnoff 	    {
1664*08b68b0eSGleb Smirnoff 		int priveleged;
1665*08b68b0eSGleb Smirnoff 
1666*08b68b0eSGleb Smirnoff 		if (carpr.carpr_vhid < 0 || carpr.carpr_vhid > CARP_MAXVHID) {
1667*08b68b0eSGleb Smirnoff 			error = EINVAL;
1668*08b68b0eSGleb Smirnoff 			break;
1669*08b68b0eSGleb Smirnoff 		}
1670*08b68b0eSGleb Smirnoff 		if (carpr.carpr_count < 1) {
1671*08b68b0eSGleb Smirnoff 			error = EMSGSIZE;
1672*08b68b0eSGleb Smirnoff 			break;
1673*08b68b0eSGleb Smirnoff 		}
1674*08b68b0eSGleb Smirnoff 		if (ifp->if_carp == NULL) {
1675*08b68b0eSGleb Smirnoff 			error = ENOENT;
1676*08b68b0eSGleb Smirnoff 			break;
1677*08b68b0eSGleb Smirnoff 		}
1678*08b68b0eSGleb Smirnoff 
1679*08b68b0eSGleb Smirnoff 		priveleged = (priv_check(td, PRIV_NETINET_CARP) == 0);
1680*08b68b0eSGleb Smirnoff 		if (carpr.carpr_vhid != 0) {
1681*08b68b0eSGleb Smirnoff 			CIF_LOCK(ifp->if_carp);
1682*08b68b0eSGleb Smirnoff 			IFNET_FOREACH_CARP(ifp, sc)
1683*08b68b0eSGleb Smirnoff 				if (sc->sc_vhid == carpr.carpr_vhid)
1684*08b68b0eSGleb Smirnoff 					break;
1685*08b68b0eSGleb Smirnoff 			CIF_UNLOCK(ifp->if_carp);
1686*08b68b0eSGleb Smirnoff 			if (sc == NULL) {
1687*08b68b0eSGleb Smirnoff 				error = ENOENT;
1688*08b68b0eSGleb Smirnoff 				break;
1689*08b68b0eSGleb Smirnoff 			}
1690*08b68b0eSGleb Smirnoff 			carp_carprcp(&carpr, sc, priveleged);
1691*08b68b0eSGleb Smirnoff 			error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
1692*08b68b0eSGleb Smirnoff 		} else  {
1693*08b68b0eSGleb Smirnoff 			int i, count;
1694*08b68b0eSGleb Smirnoff 
1695*08b68b0eSGleb Smirnoff 			count = 0;
1696*08b68b0eSGleb Smirnoff 			CIF_LOCK(ifp->if_carp);
1697*08b68b0eSGleb Smirnoff 			IFNET_FOREACH_CARP(ifp, sc)
1698*08b68b0eSGleb Smirnoff 				count++;
1699*08b68b0eSGleb Smirnoff 
1700*08b68b0eSGleb Smirnoff 			if (count > carpr.carpr_count) {
1701*08b68b0eSGleb Smirnoff 				CIF_UNLOCK(ifp->if_carp);
1702*08b68b0eSGleb Smirnoff 				error = EMSGSIZE;
1703*08b68b0eSGleb Smirnoff 				break;
1704*08b68b0eSGleb Smirnoff 			}
1705*08b68b0eSGleb Smirnoff 
1706*08b68b0eSGleb Smirnoff 			i = 0;
1707*08b68b0eSGleb Smirnoff 			IFNET_FOREACH_CARP(ifp, sc) {
1708*08b68b0eSGleb Smirnoff 				carp_carprcp(&carpr, sc, priveleged);
1709*08b68b0eSGleb Smirnoff 				carpr.carpr_count = count;
1710*08b68b0eSGleb Smirnoff 				error = copyout(&carpr, ifr->ifr_data +
1711*08b68b0eSGleb Smirnoff 				    (i * sizeof(carpr)), sizeof(carpr));
1712*08b68b0eSGleb Smirnoff 				if (error) {
1713*08b68b0eSGleb Smirnoff 					CIF_UNLOCK(ifp->if_carp);
1714*08b68b0eSGleb Smirnoff 					break;
1715*08b68b0eSGleb Smirnoff 				}
1716*08b68b0eSGleb Smirnoff 				i++;
1717*08b68b0eSGleb Smirnoff 			}
1718*08b68b0eSGleb Smirnoff 			CIF_UNLOCK(ifp->if_carp);
1719*08b68b0eSGleb Smirnoff 		}
1720*08b68b0eSGleb Smirnoff 		break;
1721*08b68b0eSGleb Smirnoff 	    }
1722*08b68b0eSGleb Smirnoff 	default:
1723*08b68b0eSGleb Smirnoff 		error = EINVAL;
1724*08b68b0eSGleb Smirnoff 	}
1725*08b68b0eSGleb Smirnoff 
1726*08b68b0eSGleb Smirnoff out:
1727*08b68b0eSGleb Smirnoff 	if (locked)
1728*08b68b0eSGleb Smirnoff 		CARP_UNLOCK(sc);
1729*08b68b0eSGleb Smirnoff 	if_rele(ifp);
1730*08b68b0eSGleb Smirnoff 
1731*08b68b0eSGleb Smirnoff 	return (error);
1732*08b68b0eSGleb Smirnoff }
1733*08b68b0eSGleb Smirnoff 
1734*08b68b0eSGleb Smirnoff static int
1735*08b68b0eSGleb Smirnoff carp_get_vhid(struct ifaddr *ifa)
1736*08b68b0eSGleb Smirnoff {
1737*08b68b0eSGleb Smirnoff 
1738*08b68b0eSGleb Smirnoff 	if (ifa == NULL || ifa->ifa_carp == NULL)
1739*08b68b0eSGleb Smirnoff 		return (0);
1740*08b68b0eSGleb Smirnoff 
1741*08b68b0eSGleb Smirnoff 	return (ifa->ifa_carp->sc_vhid);
1742*08b68b0eSGleb Smirnoff }
1743*08b68b0eSGleb Smirnoff 
1744*08b68b0eSGleb Smirnoff int
1745*08b68b0eSGleb Smirnoff carp_attach(struct ifaddr *ifa, int vhid)
1746*08b68b0eSGleb Smirnoff {
1747*08b68b0eSGleb Smirnoff 	struct ifnet *ifp = ifa->ifa_ifp;
1748*08b68b0eSGleb Smirnoff 	struct carp_softc *sc;
1749*08b68b0eSGleb Smirnoff 	int index, error;
1750*08b68b0eSGleb Smirnoff 
1751*08b68b0eSGleb Smirnoff 	if (ifp->if_carp == NULL)
1752*08b68b0eSGleb Smirnoff 		return (ENOPROTOOPT);
1753*08b68b0eSGleb Smirnoff 
1754*08b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
1755*08b68b0eSGleb Smirnoff #ifdef INET
1756*08b68b0eSGleb Smirnoff 	case AF_INET:
1757*08b68b0eSGleb Smirnoff #endif
1758*08b68b0eSGleb Smirnoff #ifdef INET6
1759*08b68b0eSGleb Smirnoff 	case AF_INET6:
1760*08b68b0eSGleb Smirnoff #endif
1761*08b68b0eSGleb Smirnoff 		break;
1762*08b68b0eSGleb Smirnoff 	default:
1763*08b68b0eSGleb Smirnoff 		return (EPROTOTYPE);
1764*08b68b0eSGleb Smirnoff 	}
1765*08b68b0eSGleb Smirnoff 
1766*08b68b0eSGleb Smirnoff 	CIF_LOCK(ifp->if_carp);
1767*08b68b0eSGleb Smirnoff 	IFNET_FOREACH_CARP(ifp, sc)
1768*08b68b0eSGleb Smirnoff 		if (sc->sc_vhid == vhid)
1769*08b68b0eSGleb Smirnoff 			break;
1770*08b68b0eSGleb Smirnoff 	CIF_UNLOCK(ifp->if_carp);
1771*08b68b0eSGleb Smirnoff 	if (sc == NULL)
1772*08b68b0eSGleb Smirnoff 		return (ENOENT);
1773*08b68b0eSGleb Smirnoff 
1774*08b68b0eSGleb Smirnoff 	if (ifa->ifa_carp) {
1775*08b68b0eSGleb Smirnoff 		if (ifa->ifa_carp->sc_vhid != vhid)
1776*08b68b0eSGleb Smirnoff 			carp_detach(ifa);
1777*08b68b0eSGleb Smirnoff 		else
1778*08b68b0eSGleb Smirnoff 			return (0);
1779*08b68b0eSGleb Smirnoff 	}
1780*08b68b0eSGleb Smirnoff 
1781*08b68b0eSGleb Smirnoff 	error = carp_multicast_setup(sc, ifa->ifa_addr->sa_family);
1782*08b68b0eSGleb Smirnoff 	if (error)
1783*08b68b0eSGleb Smirnoff 		return (error);
1784*08b68b0eSGleb Smirnoff 
1785*08b68b0eSGleb Smirnoff 	CARP_LOCK(sc);
1786*08b68b0eSGleb Smirnoff 	index = sc->sc_naddrs + sc->sc_naddrs6 + 1;
1787*08b68b0eSGleb Smirnoff 	if (index > sc->sc_ifasiz / sizeof(struct ifaddr *))
1788*08b68b0eSGleb Smirnoff 		if ((error = carp_grow_ifas(sc)) != 0) {
1789*08b68b0eSGleb Smirnoff 			carp_multicast_cleanup(sc,
1790*08b68b0eSGleb Smirnoff 			    ifa->ifa_addr->sa_family);
1791*08b68b0eSGleb Smirnoff 			CARP_UNLOCK(sc);
1792*08b68b0eSGleb Smirnoff 			return (error);
1793*08b68b0eSGleb Smirnoff 		}
1794*08b68b0eSGleb Smirnoff 
1795*08b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
1796*08b68b0eSGleb Smirnoff #ifdef INET
1797*08b68b0eSGleb Smirnoff 	case AF_INET:
1798*08b68b0eSGleb Smirnoff 		sc->sc_naddrs++;
1799*08b68b0eSGleb Smirnoff 		break;
1800*08b68b0eSGleb Smirnoff #endif
1801*08b68b0eSGleb Smirnoff #ifdef INET6
1802*08b68b0eSGleb Smirnoff 	case AF_INET6:
1803*08b68b0eSGleb Smirnoff 		sc->sc_naddrs6++;
1804*08b68b0eSGleb Smirnoff 		break;
1805*08b68b0eSGleb Smirnoff #endif
1806*08b68b0eSGleb Smirnoff 	}
1807*08b68b0eSGleb Smirnoff 
1808*08b68b0eSGleb Smirnoff 	ifa_ref(ifa);
1809*08b68b0eSGleb Smirnoff 	sc->sc_ifas[index - 1] = ifa;
1810*08b68b0eSGleb Smirnoff 	ifa->ifa_carp = sc;
1811*08b68b0eSGleb Smirnoff 
1812*08b68b0eSGleb Smirnoff 	carp_hmac_prepare(sc);
1813*08b68b0eSGleb Smirnoff 	carp_sc_state(sc);
1814*08b68b0eSGleb Smirnoff 
1815*08b68b0eSGleb Smirnoff 	CARP_UNLOCK(sc);
1816*08b68b0eSGleb Smirnoff 
1817*08b68b0eSGleb Smirnoff 	return (0);
1818*08b68b0eSGleb Smirnoff }
1819*08b68b0eSGleb Smirnoff 
1820*08b68b0eSGleb Smirnoff void
1821*08b68b0eSGleb Smirnoff carp_detach(struct ifaddr *ifa)
1822*08b68b0eSGleb Smirnoff {
1823*08b68b0eSGleb Smirnoff 	struct carp_softc *sc = ifa->ifa_carp;
1824*08b68b0eSGleb Smirnoff 	int i, index;
1825*08b68b0eSGleb Smirnoff 
1826*08b68b0eSGleb Smirnoff 	KASSERT(sc != NULL, ("%s: %p not attached", __func__, ifa));
1827*08b68b0eSGleb Smirnoff 
1828*08b68b0eSGleb Smirnoff 	CARP_LOCK(sc);
1829*08b68b0eSGleb Smirnoff 
1830*08b68b0eSGleb Smirnoff 	/* Shift array. */
1831*08b68b0eSGleb Smirnoff 	index = sc->sc_naddrs + sc->sc_naddrs6;
1832*08b68b0eSGleb Smirnoff 	for (i = 0; i < index; i++)
1833*08b68b0eSGleb Smirnoff 		if (sc->sc_ifas[i] == ifa)
1834*08b68b0eSGleb Smirnoff 			break;
1835*08b68b0eSGleb Smirnoff 	KASSERT(i < index, ("%s: %p no backref", __func__, ifa));
1836*08b68b0eSGleb Smirnoff 	for (; i < index - 1; i++)
1837*08b68b0eSGleb Smirnoff 		sc->sc_ifas[i] = sc->sc_ifas[i+1];
1838*08b68b0eSGleb Smirnoff 	sc->sc_ifas[index - 1] = NULL;
1839*08b68b0eSGleb Smirnoff 
1840*08b68b0eSGleb Smirnoff 	switch (ifa->ifa_addr->sa_family) {
1841*08b68b0eSGleb Smirnoff #ifdef INET
1842*08b68b0eSGleb Smirnoff 	case AF_INET:
1843*08b68b0eSGleb Smirnoff 		sc->sc_naddrs--;
1844*08b68b0eSGleb Smirnoff 		break;
1845*08b68b0eSGleb Smirnoff #endif
1846*08b68b0eSGleb Smirnoff #ifdef INET6
1847*08b68b0eSGleb Smirnoff 	case AF_INET6:
1848*08b68b0eSGleb Smirnoff 		sc->sc_naddrs6--;
1849*08b68b0eSGleb Smirnoff 		break;
1850*08b68b0eSGleb Smirnoff #endif
1851*08b68b0eSGleb Smirnoff 	}
1852*08b68b0eSGleb Smirnoff 
1853*08b68b0eSGleb Smirnoff 	carp_multicast_cleanup(sc, ifa->ifa_addr->sa_family);
1854*08b68b0eSGleb Smirnoff 
1855*08b68b0eSGleb Smirnoff 	ifa->ifa_carp = NULL;
1856*08b68b0eSGleb Smirnoff 	ifa_free(ifa);
1857*08b68b0eSGleb Smirnoff 
1858*08b68b0eSGleb Smirnoff 	carp_hmac_prepare(sc);
1859*08b68b0eSGleb Smirnoff 	carp_sc_state(sc);
1860*08b68b0eSGleb Smirnoff 
1861*08b68b0eSGleb Smirnoff 	if (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) {
1862*08b68b0eSGleb Smirnoff 		CARP_UNLOCK(sc);
1863*08b68b0eSGleb Smirnoff 		carp_destroy(sc);
1864*08b68b0eSGleb Smirnoff 	} else
1865*08b68b0eSGleb Smirnoff 		CARP_UNLOCK(sc);
1866*08b68b0eSGleb Smirnoff }
1867*08b68b0eSGleb Smirnoff 
1868*08b68b0eSGleb Smirnoff static void
1869*08b68b0eSGleb Smirnoff carp_set_state(struct carp_softc *sc, int state)
1870*08b68b0eSGleb Smirnoff {
1871*08b68b0eSGleb Smirnoff 
1872*08b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
1873*08b68b0eSGleb Smirnoff 
1874*08b68b0eSGleb Smirnoff 	if (sc->sc_state != state) {
1875*08b68b0eSGleb Smirnoff 		const char *carp_states[] = { CARP_STATES };
1876*08b68b0eSGleb Smirnoff 		char subsys[IFNAMSIZ+5];
1877*08b68b0eSGleb Smirnoff 
1878*08b68b0eSGleb Smirnoff 		sc->sc_state = state;
1879*08b68b0eSGleb Smirnoff 
1880*08b68b0eSGleb Smirnoff 		snprintf(subsys, IFNAMSIZ+5, "%u@%s", sc->sc_vhid,
1881*08b68b0eSGleb Smirnoff 		    sc->sc_carpdev->if_xname);
1882*08b68b0eSGleb Smirnoff 		devctl_notify("CARP", subsys, carp_states[state], NULL);
1883*08b68b0eSGleb Smirnoff 	}
1884*08b68b0eSGleb Smirnoff }
1885*08b68b0eSGleb Smirnoff 
1886*08b68b0eSGleb Smirnoff static void
1887*08b68b0eSGleb Smirnoff carp_linkstate(struct ifnet *ifp)
1888d220759bSGleb Smirnoff {
1889d220759bSGleb Smirnoff 	struct carp_softc *sc;
1890d220759bSGleb Smirnoff 
1891*08b68b0eSGleb Smirnoff 	CIF_LOCK(ifp->if_carp);
1892*08b68b0eSGleb Smirnoff 	IFNET_FOREACH_CARP(ifp, sc) {
1893*08b68b0eSGleb Smirnoff 		CARP_LOCK(sc);
1894*08b68b0eSGleb Smirnoff 		carp_sc_state(sc);
1895*08b68b0eSGleb Smirnoff 		CARP_UNLOCK(sc);
1896*08b68b0eSGleb Smirnoff 	}
1897*08b68b0eSGleb Smirnoff 	CIF_UNLOCK(ifp->if_carp);
18984cb39345SGleb Smirnoff }
18994cb39345SGleb Smirnoff 
19004cb39345SGleb Smirnoff static void
1901*08b68b0eSGleb Smirnoff carp_sc_state(struct carp_softc *sc)
19024cb39345SGleb Smirnoff {
1903*08b68b0eSGleb Smirnoff 
1904*08b68b0eSGleb Smirnoff 	CARP_LOCK_ASSERT(sc);
19054cb39345SGleb Smirnoff 
1906e8c34a71SGleb Smirnoff 	if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1907e8c34a71SGleb Smirnoff 	    !(sc->sc_carpdev->if_flags & IFF_UP)) {
1908a9771948SGleb Smirnoff 		callout_stop(&sc->sc_ad_tmo);
1909*08b68b0eSGleb Smirnoff #ifdef INET
1910a9771948SGleb Smirnoff 		callout_stop(&sc->sc_md_tmo);
1911*08b68b0eSGleb Smirnoff #endif
1912*08b68b0eSGleb Smirnoff #ifdef INET6
1913a9771948SGleb Smirnoff 		callout_stop(&sc->sc_md6_tmo);
1914*08b68b0eSGleb Smirnoff #endif
1915a9771948SGleb Smirnoff 		carp_set_state(sc, INIT);
1916a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
1917a9771948SGleb Smirnoff 		if (!sc->sc_suppress) {
1918a9771948SGleb Smirnoff 			carp_suppress_preempt++;
1919*08b68b0eSGleb Smirnoff 			if (carp_suppress_preempt == 1)
1920*08b68b0eSGleb Smirnoff 				carp_send_ad_all(sc);
1921a9771948SGleb Smirnoff 		}
1922a9771948SGleb Smirnoff 		sc->sc_suppress = 1;
1923a9771948SGleb Smirnoff 	} else {
1924a9771948SGleb Smirnoff 		carp_set_state(sc, INIT);
1925a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
1926a9771948SGleb Smirnoff 		if (sc->sc_suppress)
1927a9771948SGleb Smirnoff 			carp_suppress_preempt--;
1928a9771948SGleb Smirnoff 		sc->sc_suppress = 0;
1929a9771948SGleb Smirnoff 	}
1930a9771948SGleb Smirnoff }
1931a9771948SGleb Smirnoff 
1932*08b68b0eSGleb Smirnoff 
193354bfbd51SWill Andrews #ifdef INET
193454bfbd51SWill Andrews extern  struct domain inetdomain;
193554bfbd51SWill Andrews static struct protosw in_carp_protosw = {
193654bfbd51SWill Andrews 	.pr_type =		SOCK_RAW,
193754bfbd51SWill Andrews 	.pr_domain =		&inetdomain,
193854bfbd51SWill Andrews 	.pr_protocol =		IPPROTO_CARP,
193954bfbd51SWill Andrews 	.pr_flags =		PR_ATOMIC|PR_ADDR,
194054bfbd51SWill Andrews 	.pr_input =		carp_input,
194154bfbd51SWill Andrews 	.pr_output =		(pr_output_t *)rip_output,
194254bfbd51SWill Andrews 	.pr_ctloutput =		rip_ctloutput,
194354bfbd51SWill Andrews 	.pr_usrreqs =		&rip_usrreqs
194454bfbd51SWill Andrews };
194554bfbd51SWill Andrews #endif
194654bfbd51SWill Andrews 
194754bfbd51SWill Andrews #ifdef INET6
194854bfbd51SWill Andrews extern	struct domain inet6domain;
194954bfbd51SWill Andrews static struct ip6protosw in6_carp_protosw = {
195054bfbd51SWill Andrews 	.pr_type =		SOCK_RAW,
195154bfbd51SWill Andrews 	.pr_domain =		&inet6domain,
195254bfbd51SWill Andrews 	.pr_protocol =		IPPROTO_CARP,
195354bfbd51SWill Andrews 	.pr_flags =		PR_ATOMIC|PR_ADDR,
195454bfbd51SWill Andrews 	.pr_input =		carp6_input,
195554bfbd51SWill Andrews 	.pr_output =		rip6_output,
195654bfbd51SWill Andrews 	.pr_ctloutput =		rip6_ctloutput,
195754bfbd51SWill Andrews 	.pr_usrreqs =		&rip6_usrreqs
195854bfbd51SWill Andrews };
195954bfbd51SWill Andrews #endif
196054bfbd51SWill Andrews 
196154bfbd51SWill Andrews static void
196254bfbd51SWill Andrews carp_mod_cleanup(void)
1963a9771948SGleb Smirnoff {
196454bfbd51SWill Andrews 
196554bfbd51SWill Andrews #ifdef INET
196654bfbd51SWill Andrews 	if (proto_reg[CARP_INET] == 0) {
196715249f73SWill Andrews 		(void)ipproto_unregister(IPPROTO_CARP);
196854bfbd51SWill Andrews 		pf_proto_unregister(PF_INET, IPPROTO_CARP, SOCK_RAW);
196954bfbd51SWill Andrews 		proto_reg[CARP_INET] = -1;
197054bfbd51SWill Andrews 	}
197154bfbd51SWill Andrews 	carp_iamatch_p = NULL;
197254bfbd51SWill Andrews #endif
197354bfbd51SWill Andrews #ifdef INET6
197454bfbd51SWill Andrews 	if (proto_reg[CARP_INET6] == 0) {
197515249f73SWill Andrews 		(void)ip6proto_unregister(IPPROTO_CARP);
197654bfbd51SWill Andrews 		pf_proto_unregister(PF_INET6, IPPROTO_CARP, SOCK_RAW);
197754bfbd51SWill Andrews 		proto_reg[CARP_INET6] = -1;
197854bfbd51SWill Andrews 	}
197954bfbd51SWill Andrews 	carp_iamatch6_p = NULL;
198054bfbd51SWill Andrews 	carp_macmatch6_p = NULL;
198154bfbd51SWill Andrews #endif
1982*08b68b0eSGleb Smirnoff 	carp_ioctl_p = NULL;
1983*08b68b0eSGleb Smirnoff 	carp_attach_p = NULL;
1984*08b68b0eSGleb Smirnoff 	carp_detach_p = NULL;
1985*08b68b0eSGleb Smirnoff 	carp_get_vhid_p = NULL;
198654bfbd51SWill Andrews 	carp_linkstate_p = NULL;
198754bfbd51SWill Andrews 	carp_forus_p = NULL;
198854bfbd51SWill Andrews 	carp_output_p = NULL;
198954bfbd51SWill Andrews 	mtx_destroy(&carp_mtx);
199054bfbd51SWill Andrews }
199154bfbd51SWill Andrews 
199254bfbd51SWill Andrews static int
199354bfbd51SWill Andrews carp_mod_load(void)
199454bfbd51SWill Andrews {
199515249f73SWill Andrews 	int err;
199654bfbd51SWill Andrews 
1997d92d54d5SGleb Smirnoff 	mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
1998*08b68b0eSGleb Smirnoff 	LIST_INIT(&carp_list);
1999*08b68b0eSGleb Smirnoff 	carp_get_vhid_p = carp_get_vhid;
200054bfbd51SWill Andrews 	carp_forus_p = carp_forus;
200154bfbd51SWill Andrews 	carp_output_p = carp_output;
2002*08b68b0eSGleb Smirnoff 	carp_linkstate_p = carp_linkstate;
2003*08b68b0eSGleb Smirnoff 	carp_ioctl_p = carp_ioctl;
2004*08b68b0eSGleb Smirnoff 	carp_attach_p = carp_attach;
2005*08b68b0eSGleb Smirnoff 	carp_detach_p = carp_detach;
200654bfbd51SWill Andrews #ifdef INET6
200754bfbd51SWill Andrews 	carp_iamatch6_p = carp_iamatch6;
200854bfbd51SWill Andrews 	carp_macmatch6_p = carp_macmatch6;
200954bfbd51SWill Andrews 	proto_reg[CARP_INET6] = pf_proto_register(PF_INET6,
201054bfbd51SWill Andrews 	    (struct protosw *)&in6_carp_protosw);
2011*08b68b0eSGleb Smirnoff 	if (proto_reg[CARP_INET6]) {
201254bfbd51SWill Andrews 		printf("carp: error %d attaching to PF_INET6\n",
201354bfbd51SWill Andrews 		    proto_reg[CARP_INET6]);
201454bfbd51SWill Andrews 		carp_mod_cleanup();
20156baf7a24SGleb Smirnoff 		return (proto_reg[CARP_INET6]);
201654bfbd51SWill Andrews 	}
201715249f73SWill Andrews 	err = ip6proto_register(IPPROTO_CARP);
201815249f73SWill Andrews 	if (err) {
201915249f73SWill Andrews 		printf("carp: error %d registering with INET6\n", err);
202015249f73SWill Andrews 		carp_mod_cleanup();
20216baf7a24SGleb Smirnoff 		return (err);
202215249f73SWill Andrews 	}
202354bfbd51SWill Andrews #endif
202454bfbd51SWill Andrews #ifdef INET
202554bfbd51SWill Andrews 	carp_iamatch_p = carp_iamatch;
202654bfbd51SWill Andrews 	proto_reg[CARP_INET] = pf_proto_register(PF_INET, &in_carp_protosw);
2027*08b68b0eSGleb Smirnoff 	if (proto_reg[CARP_INET]) {
202854bfbd51SWill Andrews 		printf("carp: error %d attaching to PF_INET\n",
202954bfbd51SWill Andrews 		    proto_reg[CARP_INET]);
203054bfbd51SWill Andrews 		carp_mod_cleanup();
20316baf7a24SGleb Smirnoff 		return (proto_reg[CARP_INET]);
203254bfbd51SWill Andrews 	}
203315249f73SWill Andrews 	err = ipproto_register(IPPROTO_CARP);
203415249f73SWill Andrews 	if (err) {
203515249f73SWill Andrews 		printf("carp: error %d registering with INET\n", err);
203615249f73SWill Andrews 		carp_mod_cleanup();
20376baf7a24SGleb Smirnoff 		return (err);
203815249f73SWill Andrews 	}
203954bfbd51SWill Andrews #endif
2040*08b68b0eSGleb Smirnoff 	return (0);
204154bfbd51SWill Andrews }
2042a9771948SGleb Smirnoff 
204354bfbd51SWill Andrews static int
204454bfbd51SWill Andrews carp_modevent(module_t mod, int type, void *data)
204554bfbd51SWill Andrews {
204654bfbd51SWill Andrews 	switch (type) {
204754bfbd51SWill Andrews 	case MOD_LOAD:
204854bfbd51SWill Andrews 		return carp_mod_load();
204954bfbd51SWill Andrews 		/* NOTREACHED */
2050a9771948SGleb Smirnoff 	case MOD_UNLOAD:
2051*08b68b0eSGleb Smirnoff 		mtx_lock(&carp_mtx);
2052*08b68b0eSGleb Smirnoff 		if (LIST_EMPTY(&carp_list))
205354bfbd51SWill Andrews 			carp_mod_cleanup();
2054*08b68b0eSGleb Smirnoff 		else {
2055*08b68b0eSGleb Smirnoff 			mtx_unlock(&carp_mtx);
205654bfbd51SWill Andrews 			return (EBUSY);
2057*08b68b0eSGleb Smirnoff 		}
2058a9771948SGleb Smirnoff 		break;
2059a9771948SGleb Smirnoff 
2060a9771948SGleb Smirnoff 	default:
20610fa08018SGleb Smirnoff 		return (EINVAL);
2062a9771948SGleb Smirnoff 	}
2063a9771948SGleb Smirnoff 
20640fa08018SGleb Smirnoff 	return (0);
2065a9771948SGleb Smirnoff }
2066a9771948SGleb Smirnoff 
2067a9771948SGleb Smirnoff static moduledata_t carp_mod = {
2068a9771948SGleb Smirnoff 	"carp",
2069a9771948SGleb Smirnoff 	carp_modevent,
2070a9771948SGleb Smirnoff 	0
2071a9771948SGleb Smirnoff };
2072a9771948SGleb Smirnoff 
2073e24fa11dSWill Andrews DECLARE_MODULE(carp, carp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
2074