xref: /freebsd/sys/netinet/ip_carp.c (revision 0fa08018953ba9f2ef3fc40aaf040c446cdea38a)
1a9771948SGleb Smirnoff /* 	$FreeBSD$ */
2a9771948SGleb Smirnoff 
3a9771948SGleb Smirnoff /*
4a9771948SGleb Smirnoff  * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
5a9771948SGleb Smirnoff  * Copyright (c) 2003 Ryan McBride. 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 
29a9771948SGleb Smirnoff #include "opt_carp.h"
30a9771948SGleb Smirnoff #include "opt_bpf.h"
31a9771948SGleb Smirnoff #include "opt_inet.h"
32a9771948SGleb Smirnoff #include "opt_inet6.h"
33a9771948SGleb Smirnoff 
34a9771948SGleb Smirnoff #include <sys/types.h>
35a9771948SGleb Smirnoff #include <sys/param.h>
36a9771948SGleb Smirnoff #include <sys/systm.h>
37a9771948SGleb Smirnoff #include <sys/conf.h>
38a9771948SGleb Smirnoff #include <sys/kernel.h>
39a9771948SGleb Smirnoff #include <sys/limits.h>
40a9771948SGleb Smirnoff #include <sys/malloc.h>
41a9771948SGleb Smirnoff #include <sys/mbuf.h>
42a9771948SGleb Smirnoff #include <sys/module.h>
43a9771948SGleb Smirnoff #include <sys/time.h>
44a9771948SGleb Smirnoff #include <sys/proc.h>
45a9771948SGleb Smirnoff #include <sys/sysctl.h>
46a9771948SGleb Smirnoff #include <sys/syslog.h>
47a9771948SGleb Smirnoff #include <sys/signalvar.h>
48a9771948SGleb Smirnoff #include <sys/filio.h>
49a9771948SGleb Smirnoff #include <sys/sockio.h>
50a9771948SGleb Smirnoff 
51a9771948SGleb Smirnoff #include <sys/socket.h>
52a9771948SGleb Smirnoff #include <sys/vnode.h>
53a9771948SGleb Smirnoff 
54a9771948SGleb Smirnoff #include <machine/stdarg.h>
55a9771948SGleb Smirnoff 
56a9771948SGleb Smirnoff #include <net/bpf.h>
57a9771948SGleb Smirnoff #include <net/ethernet.h>
58a9771948SGleb Smirnoff #include <net/fddi.h>
59a9771948SGleb Smirnoff #include <net/iso88025.h>
60a9771948SGleb Smirnoff #include <net/if.h>
61a9771948SGleb Smirnoff #include <net/if_clone.h>
62433aaf04SRuslan Ermilov #include <net/if_dl.h>
63a9771948SGleb Smirnoff #include <net/if_types.h>
64a9771948SGleb Smirnoff #include <net/route.h>
65a9771948SGleb Smirnoff 
66a9771948SGleb Smirnoff #ifdef INET
67a9771948SGleb Smirnoff #include <netinet/in.h>
68a9771948SGleb Smirnoff #include <netinet/in_var.h>
69a9771948SGleb Smirnoff #include <netinet/in_systm.h>
70a9771948SGleb Smirnoff #include <netinet/ip.h>
71a9771948SGleb Smirnoff #include <netinet/ip_var.h>
72a9771948SGleb Smirnoff #include <netinet/if_ether.h>
73a9771948SGleb Smirnoff #include <machine/in_cksum.h>
74a9771948SGleb Smirnoff #endif
75a9771948SGleb Smirnoff 
76a9771948SGleb Smirnoff #ifdef INET6
77a9771948SGleb Smirnoff #include <netinet/icmp6.h>
78a9771948SGleb Smirnoff #include <netinet/ip6.h>
79a9771948SGleb Smirnoff #include <netinet6/ip6_var.h>
8029da8af6SHajimu UMEMOTO #include <netinet6/scope6_var.h>
81a9771948SGleb Smirnoff #include <netinet6/nd6.h>
82a9771948SGleb Smirnoff #endif
83a9771948SGleb Smirnoff 
84a9771948SGleb Smirnoff #include <crypto/sha1.h>
85a9771948SGleb Smirnoff #include <netinet/ip_carp.h>
86a9771948SGleb Smirnoff 
87a9771948SGleb Smirnoff #define	CARP_IFNAME	"carp"
88a9771948SGleb Smirnoff static MALLOC_DEFINE(M_CARP, "CARP", "CARP interfaces");
89a9771948SGleb Smirnoff SYSCTL_DECL(_net_inet_carp);
90a9771948SGleb Smirnoff 
91a9771948SGleb Smirnoff struct carp_softc {
92fc74a9f9SBrooks Davis 	struct ifnet	 	*sc_ifp;	/* Interface clue */
933a84d72aSGleb Smirnoff 	struct ifnet		*sc_carpdev;	/* Pointer to parent interface */
94a9771948SGleb Smirnoff 	struct in_ifaddr 	*sc_ia;		/* primary iface address */
95a9771948SGleb Smirnoff 	struct ip_moptions 	 sc_imo;
96a9771948SGleb Smirnoff #ifdef INET6
97a9771948SGleb Smirnoff 	struct in6_ifaddr 	*sc_ia6;	/* primary iface address v6 */
98a9771948SGleb Smirnoff 	struct ip6_moptions 	 sc_im6o;
99a9771948SGleb Smirnoff #endif /* INET6 */
100a9771948SGleb Smirnoff 	TAILQ_ENTRY(carp_softc)	 sc_list;
101a9771948SGleb Smirnoff 
102a9771948SGleb Smirnoff 	enum { INIT = 0, BACKUP, MASTER }	sc_state;
103a9771948SGleb Smirnoff 
104a9771948SGleb Smirnoff 	int			 sc_flags_backup;
105a9771948SGleb Smirnoff 	int			 sc_suppress;
106a9771948SGleb Smirnoff 
107a9771948SGleb Smirnoff 	int			 sc_sendad_errors;
108a9771948SGleb Smirnoff #define	CARP_SENDAD_MAX_ERRORS	3
109a9771948SGleb Smirnoff 	int			 sc_sendad_success;
110a9771948SGleb Smirnoff #define	CARP_SENDAD_MIN_SUCCESS 3
111a9771948SGleb Smirnoff 
112a9771948SGleb Smirnoff 	int			 sc_vhid;
113a9771948SGleb Smirnoff 	int			 sc_advskew;
114a9771948SGleb Smirnoff 	int			 sc_naddrs;
115a9771948SGleb Smirnoff 	int			 sc_naddrs6;
116a9771948SGleb Smirnoff 	int			 sc_advbase;	/* seconds */
117a9771948SGleb Smirnoff 	int			 sc_init_counter;
118a9771948SGleb Smirnoff 	u_int64_t		 sc_counter;
119a9771948SGleb Smirnoff 
120a9771948SGleb Smirnoff 	/* authentication */
121a9771948SGleb Smirnoff #define CARP_HMAC_PAD	64
122a9771948SGleb Smirnoff 	unsigned char sc_key[CARP_KEY_LEN];
123a9771948SGleb Smirnoff 	unsigned char sc_pad[CARP_HMAC_PAD];
124a9771948SGleb Smirnoff 	SHA1_CTX sc_sha1;
125a9771948SGleb Smirnoff 
126a9771948SGleb Smirnoff 	struct callout		 sc_ad_tmo;	/* advertisement timeout */
127a9771948SGleb Smirnoff 	struct callout		 sc_md_tmo;	/* master down timeout */
128a9771948SGleb Smirnoff 	struct callout 		 sc_md6_tmo;	/* master down timeout */
129a9771948SGleb Smirnoff 
130a9771948SGleb Smirnoff 	LIST_ENTRY(carp_softc)	 sc_next;	/* Interface clue */
131a9771948SGleb Smirnoff };
132fc74a9f9SBrooks Davis #define	SC2IFP(sc)	((sc)->sc_ifp)
133a9771948SGleb Smirnoff 
134a9771948SGleb Smirnoff int carp_suppress_preempt = 0;
135a9771948SGleb Smirnoff int carp_opts[CARPCTL_MAXID] = { 0, 1, 0, 1, 0, 0 };	/* XXX for now */
136a9771948SGleb Smirnoff SYSCTL_INT(_net_inet_carp, CARPCTL_ALLOW, allow, CTLFLAG_RW,
137a9771948SGleb Smirnoff     &carp_opts[CARPCTL_ALLOW], 0, "Accept incoming CARP packets");
138a9771948SGleb Smirnoff SYSCTL_INT(_net_inet_carp, CARPCTL_PREEMPT, preempt, CTLFLAG_RW,
139a9771948SGleb Smirnoff     &carp_opts[CARPCTL_PREEMPT], 0, "high-priority backup preemption mode");
140a9771948SGleb Smirnoff SYSCTL_INT(_net_inet_carp, CARPCTL_LOG, log, CTLFLAG_RW,
141a9771948SGleb Smirnoff     &carp_opts[CARPCTL_LOG], 0, "log bad carp packets");
142a9771948SGleb Smirnoff SYSCTL_INT(_net_inet_carp, CARPCTL_ARPBALANCE, arpbalance, CTLFLAG_RW,
143a9771948SGleb Smirnoff     &carp_opts[CARPCTL_ARPBALANCE], 0, "balance arp responses");
14432247f86SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, suppress_preempt, CTLFLAG_RD,
14532247f86SGleb Smirnoff     &carp_suppress_preempt, 0, "Preemption is suppressed");
146a9771948SGleb Smirnoff 
147a9771948SGleb Smirnoff struct carpstats carpstats;
148a9771948SGleb Smirnoff SYSCTL_STRUCT(_net_inet_carp, CARPCTL_STATS, stats, CTLFLAG_RW,
149a9771948SGleb Smirnoff     &carpstats, carpstats,
150a9771948SGleb Smirnoff     "CARP statistics (struct carpstats, netinet/ip_carp.h)");
151a9771948SGleb Smirnoff 
152a9771948SGleb Smirnoff struct carp_if {
153a9771948SGleb Smirnoff 	TAILQ_HEAD(, carp_softc) vhif_vrs;
154a9771948SGleb Smirnoff 	int vhif_nvrs;
155a9771948SGleb Smirnoff 
156a9771948SGleb Smirnoff 	struct ifnet 	*vhif_ifp;
157a9771948SGleb Smirnoff 	struct mtx	 vhif_mtx;
158a9771948SGleb Smirnoff };
159d220759bSGleb Smirnoff 
160d220759bSGleb Smirnoff /* Get carp_if from softc. Valid after carp_set_addr{,6}. */
161d220759bSGleb Smirnoff #define	SC2CIF(sc)		((struct carp_if *)(sc)->sc_carpdev->if_carp)
162d220759bSGleb Smirnoff 
163a9771948SGleb Smirnoff /* lock per carp_if queue */
164d220759bSGleb Smirnoff #define	CARP_LOCK_INIT(cif)	mtx_init(&(cif)->vhif_mtx, "carp_if", 	\
165a9771948SGleb Smirnoff 	NULL, MTX_DEF)
166d220759bSGleb Smirnoff #define	CARP_LOCK_DESTROY(cif)	mtx_destroy(&(cif)->vhif_mtx)
167a9771948SGleb Smirnoff #define	CARP_LOCK_ASSERT(cif)	mtx_assert(&(cif)->vhif_mtx, MA_OWNED)
168a9771948SGleb Smirnoff #define	CARP_LOCK(cif)		mtx_lock(&(cif)->vhif_mtx)
169a9771948SGleb Smirnoff #define	CARP_UNLOCK(cif)	mtx_unlock(&(cif)->vhif_mtx)
170a9771948SGleb Smirnoff 
171d220759bSGleb Smirnoff #define	CARP_SCLOCK(sc)		mtx_lock(&SC2CIF(sc)->vhif_mtx)
172d220759bSGleb Smirnoff #define	CARP_SCUNLOCK(sc)	mtx_unlock(&SC2CIF(sc)->vhif_mtx)
173d220759bSGleb Smirnoff #define	CARP_SCLOCK_ASSERT(sc)	mtx_assert(&SC2CIF(sc)->vhif_mtx, MA_OWNED)
174d220759bSGleb Smirnoff 
175947b7cf3SGleb Smirnoff #define	CARP_LOG(...)	do {				\
1761e9e6572SGleb Smirnoff 	if (carp_opts[CARPCTL_LOG] > 0)			\
1771e9e6572SGleb Smirnoff 		log(LOG_INFO, __VA_ARGS__);		\
178947b7cf3SGleb Smirnoff } while (0)
1791e9e6572SGleb Smirnoff 
180947b7cf3SGleb Smirnoff #define	CARP_DEBUG(...)	do {				\
1811e9e6572SGleb Smirnoff 	if (carp_opts[CARPCTL_LOG] > 1)			\
1821e9e6572SGleb Smirnoff 		log(LOG_DEBUG, __VA_ARGS__);		\
183947b7cf3SGleb Smirnoff } while (0)
184a9771948SGleb Smirnoff 
1855c1f0f6dSGleb Smirnoff static void	carp_hmac_prepare(struct carp_softc *);
1865c1f0f6dSGleb Smirnoff static void	carp_hmac_generate(struct carp_softc *, u_int32_t *,
187a9771948SGleb Smirnoff 		    unsigned char *);
1885c1f0f6dSGleb Smirnoff static int	carp_hmac_verify(struct carp_softc *, u_int32_t *,
189a9771948SGleb Smirnoff 		    unsigned char *);
1905c1f0f6dSGleb Smirnoff static void	carp_setroute(struct carp_softc *, int);
1915c1f0f6dSGleb Smirnoff static void	carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
1925c1f0f6dSGleb Smirnoff static int 	carp_clone_create(struct if_clone *, int);
1935c1f0f6dSGleb Smirnoff static void 	carp_clone_destroy(struct ifnet *);
1945c1f0f6dSGleb Smirnoff static void	carpdetach(struct carp_softc *);
1955c1f0f6dSGleb Smirnoff static int	carp_prepare_ad(struct mbuf *, struct carp_softc *,
196a9771948SGleb Smirnoff 		    struct carp_header *);
1975c1f0f6dSGleb Smirnoff static void	carp_send_ad_all(void);
1985c1f0f6dSGleb Smirnoff static void	carp_send_ad(void *);
199d220759bSGleb Smirnoff static void	carp_send_ad_locked(struct carp_softc *);
2005c1f0f6dSGleb Smirnoff static void	carp_send_arp(struct carp_softc *);
2015c1f0f6dSGleb Smirnoff static void	carp_master_down(void *);
202d220759bSGleb Smirnoff static void	carp_master_down_locked(struct carp_softc *);
2035c1f0f6dSGleb Smirnoff static int	carp_ioctl(struct ifnet *, u_long, caddr_t);
204a9771948SGleb Smirnoff static int	carp_looutput(struct ifnet *, struct mbuf *, struct sockaddr *,
205a9771948SGleb Smirnoff 		    struct rtentry *);
2065c1f0f6dSGleb Smirnoff static void	carp_start(struct ifnet *);
2075c1f0f6dSGleb Smirnoff static void	carp_setrun(struct carp_softc *, sa_family_t);
2085c1f0f6dSGleb Smirnoff static void	carp_set_state(struct carp_softc *, int);
2095c1f0f6dSGleb Smirnoff static int	carp_addrcount(struct carp_if *, struct in_ifaddr *, int);
210a9771948SGleb Smirnoff enum	{ CARP_COUNT_MASTER, CARP_COUNT_RUNNING };
211a9771948SGleb Smirnoff 
2120fa08018SGleb Smirnoff static void	carp_multicast_cleanup(struct carp_softc *);
2135c1f0f6dSGleb Smirnoff static int	carp_set_addr(struct carp_softc *, struct sockaddr_in *);
2145c1f0f6dSGleb Smirnoff static int	carp_del_addr(struct carp_softc *, struct sockaddr_in *);
215d220759bSGleb Smirnoff static void	carp_carpdev_state_locked(struct carp_if *);
2164cb39345SGleb Smirnoff static void	carp_sc_state_locked(struct carp_softc *);
217a9771948SGleb Smirnoff #ifdef INET6
2185c1f0f6dSGleb Smirnoff static void	carp_send_na(struct carp_softc *);
2195c1f0f6dSGleb Smirnoff static int	carp_set_addr6(struct carp_softc *, struct sockaddr_in6 *);
2205c1f0f6dSGleb Smirnoff static int	carp_del_addr6(struct carp_softc *, struct sockaddr_in6 *);
221a9771948SGleb Smirnoff #endif
222a9771948SGleb Smirnoff 
223a9771948SGleb Smirnoff static LIST_HEAD(, carp_softc) carpif_list;
224d92d54d5SGleb Smirnoff static struct mtx carp_mtx;
225a9771948SGleb Smirnoff IFC_SIMPLE_DECLARE(carp, 0);
226a9771948SGleb Smirnoff 
2270fa08018SGleb Smirnoff static eventhandler_tag if_detach_event_tag;
2280fa08018SGleb Smirnoff 
229a9771948SGleb Smirnoff static __inline u_int16_t
230a9771948SGleb Smirnoff carp_cksum(struct mbuf *m, int len)
231a9771948SGleb Smirnoff {
232a9771948SGleb Smirnoff 	return (in_cksum(m, len));
233a9771948SGleb Smirnoff }
234a9771948SGleb Smirnoff 
2355c1f0f6dSGleb Smirnoff static void
236a9771948SGleb Smirnoff carp_hmac_prepare(struct carp_softc *sc)
237a9771948SGleb Smirnoff {
238a9771948SGleb Smirnoff 	u_int8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
239a9771948SGleb Smirnoff 	u_int8_t vhid = sc->sc_vhid & 0xff;
240a9771948SGleb Smirnoff 	struct ifaddr *ifa;
241a9771948SGleb Smirnoff 	int i;
242a9771948SGleb Smirnoff #ifdef INET6
243a9771948SGleb Smirnoff 	struct in6_addr in6;
244a9771948SGleb Smirnoff #endif
245a9771948SGleb Smirnoff 
246d220759bSGleb Smirnoff 	if (sc->sc_carpdev)
247d220759bSGleb Smirnoff 		CARP_SCLOCK(sc);
248d220759bSGleb Smirnoff 
249d220759bSGleb Smirnoff 	/* XXX: possible race here */
250d220759bSGleb Smirnoff 
251a9771948SGleb Smirnoff 	/* compute ipad from key */
252a9771948SGleb Smirnoff 	bzero(sc->sc_pad, sizeof(sc->sc_pad));
253a9771948SGleb Smirnoff 	bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
254a9771948SGleb Smirnoff 	for (i = 0; i < sizeof(sc->sc_pad); i++)
255a9771948SGleb Smirnoff 		sc->sc_pad[i] ^= 0x36;
256a9771948SGleb Smirnoff 
257a9771948SGleb Smirnoff 	/* precompute first part of inner hash */
258a9771948SGleb Smirnoff 	SHA1Init(&sc->sc_sha1);
259a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
260a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
261a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
262a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
263a9771948SGleb Smirnoff #ifdef INET
264fc74a9f9SBrooks Davis 	TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
265a9771948SGleb Smirnoff 		if (ifa->ifa_addr->sa_family == AF_INET)
266a9771948SGleb Smirnoff 			SHA1Update(&sc->sc_sha1,
267a9771948SGleb Smirnoff 			    (void *)&ifatoia(ifa)->ia_addr.sin_addr.s_addr,
268a9771948SGleb Smirnoff 			    sizeof(struct in_addr));
269a9771948SGleb Smirnoff 	}
270a9771948SGleb Smirnoff #endif /* INET */
271a9771948SGleb Smirnoff #ifdef INET6
272fc74a9f9SBrooks Davis 	TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
273a9771948SGleb Smirnoff 		if (ifa->ifa_addr->sa_family == AF_INET6) {
274a9771948SGleb Smirnoff 			in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
275a1f7e5f8SHajimu UMEMOTO 			in6_clearscope(&in6);
276a9771948SGleb Smirnoff 			SHA1Update(&sc->sc_sha1, (void *)&in6, sizeof(in6));
277a9771948SGleb Smirnoff 		}
278a9771948SGleb Smirnoff 	}
279a9771948SGleb Smirnoff #endif /* INET6 */
280a9771948SGleb Smirnoff 
281a9771948SGleb Smirnoff 	/* convert ipad to opad */
282a9771948SGleb Smirnoff 	for (i = 0; i < sizeof(sc->sc_pad); i++)
283a9771948SGleb Smirnoff 		sc->sc_pad[i] ^= 0x36 ^ 0x5c;
284d220759bSGleb Smirnoff 
285d220759bSGleb Smirnoff 	if (sc->sc_carpdev)
286d220759bSGleb Smirnoff 		CARP_SCUNLOCK(sc);
287a9771948SGleb Smirnoff }
288a9771948SGleb Smirnoff 
2895c1f0f6dSGleb Smirnoff static void
290a9771948SGleb Smirnoff carp_hmac_generate(struct carp_softc *sc, u_int32_t counter[2],
291a9771948SGleb Smirnoff     unsigned char md[20])
292a9771948SGleb Smirnoff {
293a9771948SGleb Smirnoff 	SHA1_CTX sha1ctx;
294a9771948SGleb Smirnoff 
295a9771948SGleb Smirnoff 	/* fetch first half of inner hash */
296a9771948SGleb Smirnoff 	bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
297a9771948SGleb Smirnoff 
298a9771948SGleb Smirnoff 	SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
299a9771948SGleb Smirnoff 	SHA1Final(md, &sha1ctx);
300a9771948SGleb Smirnoff 
301a9771948SGleb Smirnoff 	/* outer hash */
302a9771948SGleb Smirnoff 	SHA1Init(&sha1ctx);
303a9771948SGleb Smirnoff 	SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
304a9771948SGleb Smirnoff 	SHA1Update(&sha1ctx, md, 20);
305a9771948SGleb Smirnoff 	SHA1Final(md, &sha1ctx);
306a9771948SGleb Smirnoff }
307a9771948SGleb Smirnoff 
3085c1f0f6dSGleb Smirnoff static int
309a9771948SGleb Smirnoff carp_hmac_verify(struct carp_softc *sc, u_int32_t counter[2],
310a9771948SGleb Smirnoff     unsigned char md[20])
311a9771948SGleb Smirnoff {
312a9771948SGleb Smirnoff 	unsigned char md2[20];
313a9771948SGleb Smirnoff 
314d220759bSGleb Smirnoff 	CARP_SCLOCK_ASSERT(sc);
315d220759bSGleb Smirnoff 
316a9771948SGleb Smirnoff 	carp_hmac_generate(sc, counter, md2);
317a9771948SGleb Smirnoff 
318a9771948SGleb Smirnoff 	return (bcmp(md, md2, sizeof(md2)));
319a9771948SGleb Smirnoff }
320a9771948SGleb Smirnoff 
3215c1f0f6dSGleb Smirnoff static void
322a9771948SGleb Smirnoff carp_setroute(struct carp_softc *sc, int cmd)
323a9771948SGleb Smirnoff {
324a9771948SGleb Smirnoff 	struct ifaddr *ifa;
325a9771948SGleb Smirnoff 	int s;
326a9771948SGleb Smirnoff 
327d220759bSGleb Smirnoff 	if (sc->sc_carpdev)
328d220759bSGleb Smirnoff 		CARP_SCLOCK_ASSERT(sc);
329d220759bSGleb Smirnoff 
330a9771948SGleb Smirnoff 	s = splnet();
331fc74a9f9SBrooks Davis 	TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
332d220759bSGleb Smirnoff 		if (ifa->ifa_addr->sa_family == AF_INET &&
333d220759bSGleb Smirnoff 		    sc->sc_carpdev != NULL) {
334a9771948SGleb Smirnoff 			int count = carp_addrcount(
335e8c34a71SGleb Smirnoff 			    (struct carp_if *)sc->sc_carpdev->if_carp,
336a9771948SGleb Smirnoff 			    ifatoia(ifa), CARP_COUNT_MASTER);
337a9771948SGleb Smirnoff 
338a9771948SGleb Smirnoff 			if ((cmd == RTM_ADD && count == 1) ||
339a9771948SGleb Smirnoff 			    (cmd == RTM_DELETE && count == 0))
340a9771948SGleb Smirnoff 				rtinit(ifa, cmd, RTF_UP | RTF_HOST);
341a9771948SGleb Smirnoff 		}
342a9771948SGleb Smirnoff #ifdef INET6
343a9771948SGleb Smirnoff 		if (ifa->ifa_addr->sa_family == AF_INET6) {
344a9771948SGleb Smirnoff 			if (cmd == RTM_ADD)
345a9771948SGleb Smirnoff 				in6_ifaddloop(ifa);
346a9771948SGleb Smirnoff 			else
347a9771948SGleb Smirnoff 				in6_ifremloop(ifa);
348a9771948SGleb Smirnoff 		}
349a9771948SGleb Smirnoff #endif /* INET6 */
350a9771948SGleb Smirnoff 	}
351a9771948SGleb Smirnoff 	splx(s);
352a9771948SGleb Smirnoff }
353a9771948SGleb Smirnoff 
3545c1f0f6dSGleb Smirnoff static int
355a9771948SGleb Smirnoff carp_clone_create(struct if_clone *ifc, int unit)
356a9771948SGleb Smirnoff {
357a9771948SGleb Smirnoff 
358a9771948SGleb Smirnoff 	struct carp_softc *sc;
359a9771948SGleb Smirnoff 	struct ifnet *ifp;
360a9771948SGleb Smirnoff 
361a9771948SGleb Smirnoff 	MALLOC(sc, struct carp_softc *, sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
362fc74a9f9SBrooks Davis 	ifp = SC2IFP(sc) = if_alloc(IFT_ETHER);
363fc74a9f9SBrooks Davis 	if (ifp == NULL) {
364fc74a9f9SBrooks Davis 		FREE(sc, M_CARP);
365fc74a9f9SBrooks Davis 		return (ENOSPC);
366fc74a9f9SBrooks Davis 	}
367a9771948SGleb Smirnoff 
368a9771948SGleb Smirnoff 	sc->sc_flags_backup = 0;
369a9771948SGleb Smirnoff 	sc->sc_suppress = 0;
370a9771948SGleb Smirnoff 	sc->sc_advbase = CARP_DFLTINTV;
371a9771948SGleb Smirnoff 	sc->sc_vhid = -1;	/* required setting */
372a9771948SGleb Smirnoff 	sc->sc_advskew = 0;
373a9771948SGleb Smirnoff 	sc->sc_init_counter = 1;
374a9771948SGleb Smirnoff 	sc->sc_naddrs = sc->sc_naddrs6 = 0; /* M_ZERO? */
375a9771948SGleb Smirnoff #ifdef INET6
376a9771948SGleb Smirnoff 	sc->sc_im6o.im6o_multicast_hlim = CARP_DFLTTL;
377a9771948SGleb Smirnoff #endif
378a9771948SGleb Smirnoff 
379d220759bSGleb Smirnoff 	callout_init(&sc->sc_ad_tmo, NET_CALLOUT_MPSAFE);
380d220759bSGleb Smirnoff 	callout_init(&sc->sc_md_tmo, NET_CALLOUT_MPSAFE);
381d220759bSGleb Smirnoff 	callout_init(&sc->sc_md6_tmo, NET_CALLOUT_MPSAFE);
382a9771948SGleb Smirnoff 
383a9771948SGleb Smirnoff 	ifp->if_softc = sc;
384a9771948SGleb Smirnoff 	if_initname(ifp, CARP_IFNAME, unit);
385a9771948SGleb Smirnoff 	ifp->if_mtu = ETHERMTU;
3869f4abef9SYaroslav Tykhiy 	ifp->if_flags = IFF_LOOPBACK;
387a9771948SGleb Smirnoff 	ifp->if_ioctl = carp_ioctl;
388a9771948SGleb Smirnoff 	ifp->if_output = carp_looutput;
389a9771948SGleb Smirnoff 	ifp->if_start = carp_start;
390a9771948SGleb Smirnoff 	ifp->if_type = IFT_CARP;
391a9771948SGleb Smirnoff 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
392a9771948SGleb Smirnoff 	ifp->if_hdrlen = 0;
393a9771948SGleb Smirnoff 	if_attach(ifp);
394fc74a9f9SBrooks Davis 	bpfattach(SC2IFP(sc), DLT_NULL, sizeof(u_int32_t));
395d92d54d5SGleb Smirnoff 	mtx_lock(&carp_mtx);
396d92d54d5SGleb Smirnoff 	LIST_INSERT_HEAD(&carpif_list, sc, sc_next);
397d92d54d5SGleb Smirnoff 	mtx_unlock(&carp_mtx);
398a9771948SGleb Smirnoff 	return (0);
399a9771948SGleb Smirnoff }
400a9771948SGleb Smirnoff 
4015c1f0f6dSGleb Smirnoff static void
402a9771948SGleb Smirnoff carp_clone_destroy(struct ifnet *ifp)
403a9771948SGleb Smirnoff {
404a9771948SGleb Smirnoff 	struct carp_softc *sc = ifp->if_softc;
405a9771948SGleb Smirnoff 
4060fa08018SGleb Smirnoff 	if (sc->sc_carpdev)
4070fa08018SGleb Smirnoff 		CARP_SCLOCK(sc);
4080fa08018SGleb Smirnoff 	carpdetach(sc);
4090fa08018SGleb Smirnoff 	if (sc->sc_carpdev)
4100fa08018SGleb Smirnoff 		CARP_SCUNLOCK(sc);
411a9771948SGleb Smirnoff 
412d92d54d5SGleb Smirnoff 	mtx_lock(&carp_mtx);
413d92d54d5SGleb Smirnoff 	LIST_REMOVE(sc, sc_next);
414d92d54d5SGleb Smirnoff 	mtx_unlock(&carp_mtx);
415a9771948SGleb Smirnoff 	bpfdetach(ifp);
416a9771948SGleb Smirnoff 	if_detach(ifp);
417fc74a9f9SBrooks Davis 	if_free_type(ifp, IFT_ETHER);
418a9771948SGleb Smirnoff 	free(sc, M_CARP);
419a9771948SGleb Smirnoff }
420a9771948SGleb Smirnoff 
4210fa08018SGleb Smirnoff static void
4220fa08018SGleb Smirnoff carpdetach(struct carp_softc *sc)
4230fa08018SGleb Smirnoff {
4240fa08018SGleb Smirnoff 	struct carp_if *cif;
4250fa08018SGleb Smirnoff 
4260fa08018SGleb Smirnoff 	callout_stop(&sc->sc_ad_tmo);
4270fa08018SGleb Smirnoff 	callout_stop(&sc->sc_md_tmo);
4280fa08018SGleb Smirnoff 	callout_stop(&sc->sc_md6_tmo);
4290fa08018SGleb Smirnoff 
4300fa08018SGleb Smirnoff 	if (sc->sc_suppress)
4310fa08018SGleb Smirnoff 		carp_suppress_preempt--;
4320fa08018SGleb Smirnoff 	sc->sc_suppress = 0;
4330fa08018SGleb Smirnoff 
4340fa08018SGleb Smirnoff 	if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS)
4350fa08018SGleb Smirnoff 		carp_suppress_preempt--;
4360fa08018SGleb Smirnoff 	sc->sc_sendad_errors = 0;
4370fa08018SGleb Smirnoff 
4380fa08018SGleb Smirnoff 	carp_set_state(sc, INIT);
4390fa08018SGleb Smirnoff 	SC2IFP(sc)->if_flags &= ~IFF_UP;
4400fa08018SGleb Smirnoff 	carp_setrun(sc, 0);
4410fa08018SGleb Smirnoff 	carp_multicast_cleanup(sc);
4420fa08018SGleb Smirnoff 
4430fa08018SGleb Smirnoff 	if (sc->sc_carpdev != NULL) {
4440fa08018SGleb Smirnoff 		cif = (struct carp_if *)sc->sc_carpdev->if_carp;
4450fa08018SGleb Smirnoff 		CARP_LOCK_ASSERT(cif);
4460fa08018SGleb Smirnoff 		TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
4470fa08018SGleb Smirnoff 		if (!--cif->vhif_nvrs) {
4480fa08018SGleb Smirnoff 			ifpromisc(sc->sc_carpdev, 0);
4490fa08018SGleb Smirnoff 			sc->sc_carpdev->if_carp = NULL;
4500fa08018SGleb Smirnoff 			CARP_LOCK_DESTROY(cif);
4510fa08018SGleb Smirnoff 			FREE(cif, M_IFADDR);
4520fa08018SGleb Smirnoff 		}
4530fa08018SGleb Smirnoff 	}
4540fa08018SGleb Smirnoff         sc->sc_carpdev = NULL;
4550fa08018SGleb Smirnoff }
4560fa08018SGleb Smirnoff 
4570fa08018SGleb Smirnoff /* Detach an interface from the carp. */
4580fa08018SGleb Smirnoff static void
4590fa08018SGleb Smirnoff carp_ifdetach(void *arg __unused, struct ifnet *ifp)
4600fa08018SGleb Smirnoff {
4610fa08018SGleb Smirnoff 	struct carp_if *cif = (struct carp_if *)ifp->if_carp;
4620fa08018SGleb Smirnoff 	struct carp_softc *sc, *nextsc;
4630fa08018SGleb Smirnoff 
4640fa08018SGleb Smirnoff 	if (cif == NULL)
4650fa08018SGleb Smirnoff 		return;
4660fa08018SGleb Smirnoff 
4670fa08018SGleb Smirnoff 	/*
4680fa08018SGleb Smirnoff 	 * XXX: At the end of for() cycle the lock will be destroyed.
4690fa08018SGleb Smirnoff 	 */
4700fa08018SGleb Smirnoff 	CARP_LOCK(cif);
4710fa08018SGleb Smirnoff 	for (sc = TAILQ_FIRST(&cif->vhif_vrs); sc; sc = nextsc) {
4720fa08018SGleb Smirnoff 		nextsc = TAILQ_NEXT(sc, sc_list);
4730fa08018SGleb Smirnoff 		carpdetach(sc);
4740fa08018SGleb Smirnoff 	}
4750fa08018SGleb Smirnoff }
4760fa08018SGleb Smirnoff 
477a9771948SGleb Smirnoff /*
478a9771948SGleb Smirnoff  * process input packet.
479a9771948SGleb Smirnoff  * we have rearranged checks order compared to the rfc,
480a9771948SGleb Smirnoff  * but it seems more efficient this way or not possible otherwise.
481a9771948SGleb Smirnoff  */
482a9771948SGleb Smirnoff void
483a9771948SGleb Smirnoff carp_input(struct mbuf *m, int hlen)
484a9771948SGleb Smirnoff {
485a9771948SGleb Smirnoff 	struct ip *ip = mtod(m, struct ip *);
486a9771948SGleb Smirnoff 	struct carp_header *ch;
487a9771948SGleb Smirnoff 	int iplen, len;
488a9771948SGleb Smirnoff 
489a9771948SGleb Smirnoff 	carpstats.carps_ipackets++;
490a9771948SGleb Smirnoff 
491a9771948SGleb Smirnoff 	if (!carp_opts[CARPCTL_ALLOW]) {
492a9771948SGleb Smirnoff 		m_freem(m);
493a9771948SGleb Smirnoff 		return;
494a9771948SGleb Smirnoff 	}
495a9771948SGleb Smirnoff 
496a9771948SGleb Smirnoff 	/* check if received on a valid carp interface */
497a9771948SGleb Smirnoff 	if (m->m_pkthdr.rcvif->if_carp == NULL) {
498a9771948SGleb Smirnoff 		carpstats.carps_badif++;
49988bf82a6SGleb Smirnoff 		CARP_LOG("carp_input: packet received on non-carp "
50088bf82a6SGleb Smirnoff 		    "interface: %s\n",
5011e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
502a9771948SGleb Smirnoff 		m_freem(m);
503a9771948SGleb Smirnoff 		return;
504a9771948SGleb Smirnoff 	}
505a9771948SGleb Smirnoff 
506a9771948SGleb Smirnoff 	/* verify that the IP TTL is 255.  */
507a9771948SGleb Smirnoff 	if (ip->ip_ttl != CARP_DFLTTL) {
508a9771948SGleb Smirnoff 		carpstats.carps_badttl++;
50988bf82a6SGleb Smirnoff 		CARP_LOG("carp_input: received ttl %d != 255i on %s\n",
5101e9e6572SGleb Smirnoff 		    ip->ip_ttl,
5111e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
512a9771948SGleb Smirnoff 		m_freem(m);
513a9771948SGleb Smirnoff 		return;
514a9771948SGleb Smirnoff 	}
515a9771948SGleb Smirnoff 
516a9771948SGleb Smirnoff 	iplen = ip->ip_hl << 2;
517a9771948SGleb Smirnoff 
518a9771948SGleb Smirnoff 	if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
519a9771948SGleb Smirnoff 		carpstats.carps_badlen++;
5201e9e6572SGleb Smirnoff 		CARP_LOG("carp_input: received len %zd < "
52188bf82a6SGleb Smirnoff 		    "sizeof(struct carp_header)\n",
5221e9e6572SGleb Smirnoff 		    m->m_len - sizeof(struct ip));
523a9771948SGleb Smirnoff 		m_freem(m);
524a9771948SGleb Smirnoff 		return;
525a9771948SGleb Smirnoff 	}
526a9771948SGleb Smirnoff 
527a9771948SGleb Smirnoff 	if (iplen + sizeof(*ch) < m->m_len) {
528a9771948SGleb Smirnoff 		if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
529a9771948SGleb Smirnoff 			carpstats.carps_hdrops++;
53088bf82a6SGleb Smirnoff 			CARP_LOG("carp_input: pullup failed\n");
531a9771948SGleb Smirnoff 			return;
532a9771948SGleb Smirnoff 		}
533a9771948SGleb Smirnoff 		ip = mtod(m, struct ip *);
534a9771948SGleb Smirnoff 	}
535a9771948SGleb Smirnoff 	ch = (struct carp_header *)((char *)ip + iplen);
536a9771948SGleb Smirnoff 
537a9771948SGleb Smirnoff 	/*
538a9771948SGleb Smirnoff 	 * verify that the received packet length is
539a9771948SGleb Smirnoff 	 * equal to the CARP header
540a9771948SGleb Smirnoff 	 */
541a9771948SGleb Smirnoff 	len = iplen + sizeof(*ch);
542a9771948SGleb Smirnoff 	if (len > m->m_pkthdr.len) {
543a9771948SGleb Smirnoff 		carpstats.carps_badlen++;
54488bf82a6SGleb Smirnoff 		CARP_LOG("carp_input: packet too short %d on %s\n",
5451e9e6572SGleb Smirnoff 		    m->m_pkthdr.len,
5461e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
547a9771948SGleb Smirnoff 		m_freem(m);
548a9771948SGleb Smirnoff 		return;
549a9771948SGleb Smirnoff 	}
550a9771948SGleb Smirnoff 
551a9771948SGleb Smirnoff 	if ((m = m_pullup(m, len)) == NULL) {
552a9771948SGleb Smirnoff 		carpstats.carps_hdrops++;
553a9771948SGleb Smirnoff 		return;
554a9771948SGleb Smirnoff 	}
555a9771948SGleb Smirnoff 	ip = mtod(m, struct ip *);
556a9771948SGleb Smirnoff 	ch = (struct carp_header *)((char *)ip + iplen);
557a9771948SGleb Smirnoff 
558a9771948SGleb Smirnoff 	/* verify the CARP checksum */
559a9771948SGleb Smirnoff 	m->m_data += iplen;
560a9771948SGleb Smirnoff 	if (carp_cksum(m, len - iplen)) {
561a9771948SGleb Smirnoff 		carpstats.carps_badsum++;
56288bf82a6SGleb Smirnoff 		CARP_LOG("carp_input: checksum failed on %s\n",
5631e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
564a9771948SGleb Smirnoff 		m_freem(m);
565a9771948SGleb Smirnoff 		return;
566a9771948SGleb Smirnoff 	}
567a9771948SGleb Smirnoff 	m->m_data -= iplen;
568a9771948SGleb Smirnoff 
5691e9e6572SGleb Smirnoff 	carp_input_c(m, ch, AF_INET);
570a9771948SGleb Smirnoff }
571a9771948SGleb Smirnoff 
572a9771948SGleb Smirnoff #ifdef INET6
573a9771948SGleb Smirnoff int
574a9771948SGleb Smirnoff carp6_input(struct mbuf **mp, int *offp, int proto)
575a9771948SGleb Smirnoff {
576a9771948SGleb Smirnoff 	struct mbuf *m = *mp;
577a9771948SGleb Smirnoff 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
578a9771948SGleb Smirnoff 	struct carp_header *ch;
579a9771948SGleb Smirnoff 	u_int len;
580a9771948SGleb Smirnoff 
581a9771948SGleb Smirnoff 	carpstats.carps_ipackets6++;
582a9771948SGleb Smirnoff 
583a9771948SGleb Smirnoff 	if (!carp_opts[CARPCTL_ALLOW]) {
584a9771948SGleb Smirnoff 		m_freem(m);
585a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
586a9771948SGleb Smirnoff 	}
587a9771948SGleb Smirnoff 
588a9771948SGleb Smirnoff 	/* check if received on a valid carp interface */
589a9771948SGleb Smirnoff 	if (m->m_pkthdr.rcvif->if_carp == NULL) {
590a9771948SGleb Smirnoff 		carpstats.carps_badif++;
5911e9e6572SGleb Smirnoff 		CARP_LOG("carp6_input: packet received on non-carp "
59288bf82a6SGleb Smirnoff 		    "interface: %s\n",
5931e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
594a9771948SGleb Smirnoff 		m_freem(m);
595a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
596a9771948SGleb Smirnoff 	}
597a9771948SGleb Smirnoff 
598a9771948SGleb Smirnoff 	/* verify that the IP TTL is 255 */
599a9771948SGleb Smirnoff 	if (ip6->ip6_hlim != CARP_DFLTTL) {
600a9771948SGleb Smirnoff 		carpstats.carps_badttl++;
60188bf82a6SGleb Smirnoff 		CARP_LOG("carp6_input: received ttl %d != 255 on %s\n",
6021e9e6572SGleb Smirnoff 		    ip6->ip6_hlim,
6031e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
604a9771948SGleb Smirnoff 		m_freem(m);
605a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
606a9771948SGleb Smirnoff 	}
607a9771948SGleb Smirnoff 
608a9771948SGleb Smirnoff 	/* verify that we have a complete carp packet */
609a9771948SGleb Smirnoff 	len = m->m_len;
610a9771948SGleb Smirnoff 	IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
611a9771948SGleb Smirnoff 	if (ch == NULL) {
612a9771948SGleb Smirnoff 		carpstats.carps_badlen++;
6139860bab3SGleb Smirnoff 		CARP_LOG("carp6_input: packet size %u too small\n", len);
614a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
615a9771948SGleb Smirnoff 	}
616a9771948SGleb Smirnoff 
617a9771948SGleb Smirnoff 
618a9771948SGleb Smirnoff 	/* verify the CARP checksum */
619a9771948SGleb Smirnoff 	m->m_data += *offp;
620a9771948SGleb Smirnoff 	if (carp_cksum(m, sizeof(*ch))) {
621a9771948SGleb Smirnoff 		carpstats.carps_badsum++;
62288bf82a6SGleb Smirnoff 		CARP_LOG("carp6_input: checksum failed, on %s\n",
6231e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
624a9771948SGleb Smirnoff 		m_freem(m);
625a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
626a9771948SGleb Smirnoff 	}
627a9771948SGleb Smirnoff 	m->m_data -= *offp;
628a9771948SGleb Smirnoff 
6291e9e6572SGleb Smirnoff 	carp_input_c(m, ch, AF_INET6);
630a9771948SGleb Smirnoff 	return (IPPROTO_DONE);
631a9771948SGleb Smirnoff }
632a9771948SGleb Smirnoff #endif /* INET6 */
633a9771948SGleb Smirnoff 
6345c1f0f6dSGleb Smirnoff static void
6351e9e6572SGleb Smirnoff carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
636a9771948SGleb Smirnoff {
637a9771948SGleb Smirnoff 	struct ifnet *ifp = m->m_pkthdr.rcvif;
6381e9e6572SGleb Smirnoff 	struct carp_softc *sc;
639a9771948SGleb Smirnoff 	u_int64_t tmp_counter;
640a9771948SGleb Smirnoff 	struct timeval sc_tv, ch_tv;
641a9771948SGleb Smirnoff 
642a9771948SGleb Smirnoff 	/* verify that the VHID is valid on the receiving interface */
643a9771948SGleb Smirnoff 	CARP_LOCK(ifp->if_carp);
644a9771948SGleb Smirnoff 	TAILQ_FOREACH(sc, &((struct carp_if *)ifp->if_carp)->vhif_vrs, sc_list)
645a9771948SGleb Smirnoff 		if (sc->sc_vhid == ch->carp_vhid)
646a9771948SGleb Smirnoff 			break;
647d220759bSGleb Smirnoff 
64813f4c340SRobert Watson 	if (!sc || !((SC2IFP(sc)->if_flags & IFF_UP) &&
64913f4c340SRobert Watson 	    (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING))) {
650a9771948SGleb Smirnoff 		carpstats.carps_badvhid++;
651d220759bSGleb Smirnoff 		CARP_UNLOCK(ifp->if_carp);
652a9771948SGleb Smirnoff 		m_freem(m);
653a9771948SGleb Smirnoff 		return;
654a9771948SGleb Smirnoff 	}
655a9771948SGleb Smirnoff 
656fc74a9f9SBrooks Davis 	getmicrotime(&SC2IFP(sc)->if_lastchange);
657fc74a9f9SBrooks Davis 	SC2IFP(sc)->if_ipackets++;
658fc74a9f9SBrooks Davis 	SC2IFP(sc)->if_ibytes += m->m_pkthdr.len;
659a9771948SGleb Smirnoff 
660fc74a9f9SBrooks Davis 	if (SC2IFP(sc)->if_bpf) {
661a9771948SGleb Smirnoff 		struct ip *ip = mtod(m, struct ip *);
6623e07def4SGleb Smirnoff 		uint32_t af1 = af;
663a9771948SGleb Smirnoff 
664a9771948SGleb Smirnoff 		/* BPF wants net byte order */
6653e07def4SGleb Smirnoff 		ip->ip_len = htons(ip->ip_len + (ip->ip_hl << 2));
6663e07def4SGleb Smirnoff 		ip->ip_off = htons(ip->ip_off);
667fc74a9f9SBrooks Davis 		bpf_mtap2(SC2IFP(sc)->if_bpf, &af1, sizeof(af1), m);
668a9771948SGleb Smirnoff 	}
669a9771948SGleb Smirnoff 
670a9771948SGleb Smirnoff 	/* verify the CARP version. */
671a9771948SGleb Smirnoff 	if (ch->carp_version != CARP_VERSION) {
672a9771948SGleb Smirnoff 		carpstats.carps_badver++;
673fc74a9f9SBrooks Davis 		SC2IFP(sc)->if_ierrors++;
674d220759bSGleb Smirnoff 		CARP_UNLOCK(ifp->if_carp);
67588bf82a6SGleb Smirnoff 		CARP_LOG("%s; invalid version %d\n",
676fc74a9f9SBrooks Davis 		    SC2IFP(sc)->if_xname,
6771e9e6572SGleb Smirnoff 		    ch->carp_version);
678a9771948SGleb Smirnoff 		m_freem(m);
679a9771948SGleb Smirnoff 		return;
680a9771948SGleb Smirnoff 	}
681a9771948SGleb Smirnoff 
682a9771948SGleb Smirnoff 	/* verify the hash */
683a9771948SGleb Smirnoff 	if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
684a9771948SGleb Smirnoff 		carpstats.carps_badauth++;
685fc74a9f9SBrooks Davis 		SC2IFP(sc)->if_ierrors++;
686d220759bSGleb Smirnoff 		CARP_UNLOCK(ifp->if_carp);
687fc74a9f9SBrooks Davis 		CARP_LOG("%s: incorrect hash\n", SC2IFP(sc)->if_xname);
688a9771948SGleb Smirnoff 		m_freem(m);
689a9771948SGleb Smirnoff 		return;
690a9771948SGleb Smirnoff 	}
691a9771948SGleb Smirnoff 
692a9771948SGleb Smirnoff 	tmp_counter = ntohl(ch->carp_counter[0]);
693a9771948SGleb Smirnoff 	tmp_counter = tmp_counter<<32;
694a9771948SGleb Smirnoff 	tmp_counter += ntohl(ch->carp_counter[1]);
695a9771948SGleb Smirnoff 
696a9771948SGleb Smirnoff 	/* XXX Replay protection goes here */
697a9771948SGleb Smirnoff 
698a9771948SGleb Smirnoff 	sc->sc_init_counter = 0;
699a9771948SGleb Smirnoff 	sc->sc_counter = tmp_counter;
700a9771948SGleb Smirnoff 
701a9771948SGleb Smirnoff 	sc_tv.tv_sec = sc->sc_advbase;
702a9771948SGleb Smirnoff 	if (carp_suppress_preempt && sc->sc_advskew <  240)
703a9771948SGleb Smirnoff 		sc_tv.tv_usec = 240 * 1000000 / 256;
704a9771948SGleb Smirnoff 	else
705a9771948SGleb Smirnoff 		sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256;
706a9771948SGleb Smirnoff 	ch_tv.tv_sec = ch->carp_advbase;
707a9771948SGleb Smirnoff 	ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
708a9771948SGleb Smirnoff 
709a9771948SGleb Smirnoff 	switch (sc->sc_state) {
710a9771948SGleb Smirnoff 	case INIT:
711a9771948SGleb Smirnoff 		break;
712a9771948SGleb Smirnoff 	case MASTER:
713a9771948SGleb Smirnoff 		/*
714a9771948SGleb Smirnoff 		 * If we receive an advertisement from a master who's going to
715a9771948SGleb Smirnoff 		 * be more frequent than us, go into BACKUP state.
716a9771948SGleb Smirnoff 		 */
717a9771948SGleb Smirnoff 		if (timevalcmp(&sc_tv, &ch_tv, >) ||
718a9771948SGleb Smirnoff 		    timevalcmp(&sc_tv, &ch_tv, ==)) {
719a9771948SGleb Smirnoff 			callout_stop(&sc->sc_ad_tmo);
7201e9e6572SGleb Smirnoff 			CARP_DEBUG("%s: MASTER -> BACKUP "
72188bf82a6SGleb Smirnoff 			   "(more frequent advertisement received)\n",
722fc74a9f9SBrooks Davis 			   SC2IFP(sc)->if_xname);
723a9771948SGleb Smirnoff 			carp_set_state(sc, BACKUP);
724a9771948SGleb Smirnoff 			carp_setrun(sc, 0);
725a9771948SGleb Smirnoff 			carp_setroute(sc, RTM_DELETE);
726a9771948SGleb Smirnoff 		}
727a9771948SGleb Smirnoff 		break;
728a9771948SGleb Smirnoff 	case BACKUP:
729a9771948SGleb Smirnoff 		/*
730a9771948SGleb Smirnoff 		 * If we're pre-empting masters who advertise slower than us,
731a9771948SGleb Smirnoff 		 * and this one claims to be slower, treat him as down.
732a9771948SGleb Smirnoff 		 */
733a9771948SGleb Smirnoff 		if (carp_opts[CARPCTL_PREEMPT] &&
734a9771948SGleb Smirnoff 		    timevalcmp(&sc_tv, &ch_tv, <)) {
7351e9e6572SGleb Smirnoff 			CARP_DEBUG("%s: BACKUP -> MASTER "
73688bf82a6SGleb Smirnoff 			    "(preempting a slower master)\n",
737fc74a9f9SBrooks Davis 			    SC2IFP(sc)->if_xname);
738d220759bSGleb Smirnoff 			carp_master_down_locked(sc);
739a9771948SGleb Smirnoff 			break;
740a9771948SGleb Smirnoff 		}
741a9771948SGleb Smirnoff 
742a9771948SGleb Smirnoff 		/*
743a9771948SGleb Smirnoff 		 *  If the master is going to advertise at such a low frequency
744a9771948SGleb Smirnoff 		 *  that he's guaranteed to time out, we'd might as well just
745a9771948SGleb Smirnoff 		 *  treat him as timed out now.
746a9771948SGleb Smirnoff 		 */
747a9771948SGleb Smirnoff 		sc_tv.tv_sec = sc->sc_advbase * 3;
748a9771948SGleb Smirnoff 		if (timevalcmp(&sc_tv, &ch_tv, <)) {
7491e9e6572SGleb Smirnoff 			CARP_DEBUG("%s: BACKUP -> MASTER "
75088bf82a6SGleb Smirnoff 			    "(master timed out)\n",
751fc74a9f9SBrooks Davis 			    SC2IFP(sc)->if_xname);
752d220759bSGleb Smirnoff 			carp_master_down_locked(sc);
753a9771948SGleb Smirnoff 			break;
754a9771948SGleb Smirnoff 		}
755a9771948SGleb Smirnoff 
756a9771948SGleb Smirnoff 		/*
757a9771948SGleb Smirnoff 		 * Otherwise, we reset the counter and wait for the next
758a9771948SGleb Smirnoff 		 * advertisement.
759a9771948SGleb Smirnoff 		 */
760a9771948SGleb Smirnoff 		carp_setrun(sc, af);
761a9771948SGleb Smirnoff 		break;
762a9771948SGleb Smirnoff 	}
763a9771948SGleb Smirnoff 
764d220759bSGleb Smirnoff 	CARP_UNLOCK(ifp->if_carp);
765d220759bSGleb Smirnoff 
766a9771948SGleb Smirnoff 	m_freem(m);
767a9771948SGleb Smirnoff 	return;
768a9771948SGleb Smirnoff }
769a9771948SGleb Smirnoff 
7705c1f0f6dSGleb Smirnoff static int
771a9771948SGleb Smirnoff carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
772a9771948SGleb Smirnoff {
773a9771948SGleb Smirnoff 	struct m_tag *mtag;
774fc74a9f9SBrooks Davis 	struct ifnet *ifp = SC2IFP(sc);
775a9771948SGleb Smirnoff 
776a9771948SGleb Smirnoff 	if (sc->sc_init_counter) {
777a9771948SGleb Smirnoff 		/* this could also be seconds since unix epoch */
778a9771948SGleb Smirnoff 		sc->sc_counter = arc4random();
779a9771948SGleb Smirnoff 		sc->sc_counter = sc->sc_counter << 32;
780a9771948SGleb Smirnoff 		sc->sc_counter += arc4random();
781a9771948SGleb Smirnoff 	} else
782a9771948SGleb Smirnoff 		sc->sc_counter++;
783a9771948SGleb Smirnoff 
784a9771948SGleb Smirnoff 	ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
785a9771948SGleb Smirnoff 	ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
786a9771948SGleb Smirnoff 
787a9771948SGleb Smirnoff 	carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
788a9771948SGleb Smirnoff 
789a9771948SGleb Smirnoff 	/* Tag packet for carp_output */
790a9771948SGleb Smirnoff 	mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct ifnet *), M_NOWAIT);
791a9771948SGleb Smirnoff 	if (mtag == NULL) {
792a9771948SGleb Smirnoff 		m_freem(m);
793fc74a9f9SBrooks Davis 		SC2IFP(sc)->if_oerrors++;
794a9771948SGleb Smirnoff 		return (ENOMEM);
795a9771948SGleb Smirnoff 	}
796a9771948SGleb Smirnoff 	bcopy(&ifp, (caddr_t)(mtag + 1), sizeof(struct ifnet *));
797a9771948SGleb Smirnoff 	m_tag_prepend(m, mtag);
798a9771948SGleb Smirnoff 
799a9771948SGleb Smirnoff 	return (0);
800a9771948SGleb Smirnoff }
801a9771948SGleb Smirnoff 
8025c1f0f6dSGleb Smirnoff static void
803a9771948SGleb Smirnoff carp_send_ad_all(void)
804a9771948SGleb Smirnoff {
805d92d54d5SGleb Smirnoff 	struct carp_softc *sc;
806a9771948SGleb Smirnoff 
807d92d54d5SGleb Smirnoff 	mtx_lock(&carp_mtx);
808d92d54d5SGleb Smirnoff 	LIST_FOREACH(sc, &carpif_list, sc_next) {
809d92d54d5SGleb Smirnoff 		if (sc->sc_carpdev == NULL)
810a9771948SGleb Smirnoff 			continue;
811d92d54d5SGleb Smirnoff 		CARP_SCLOCK(sc);
81213f4c340SRobert Watson 		if ((SC2IFP(sc)->if_flags & IFF_UP) &&
81313f4c340SRobert Watson 		    (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING) &&
814d92d54d5SGleb Smirnoff 		     sc->sc_state == MASTER)
815d220759bSGleb Smirnoff 			carp_send_ad_locked(sc);
816d92d54d5SGleb Smirnoff 		CARP_SCUNLOCK(sc);
817a9771948SGleb Smirnoff 	}
818d92d54d5SGleb Smirnoff 	mtx_unlock(&carp_mtx);
819a9771948SGleb Smirnoff }
820a9771948SGleb Smirnoff 
8215c1f0f6dSGleb Smirnoff static void
822a9771948SGleb Smirnoff carp_send_ad(void *v)
823a9771948SGleb Smirnoff {
824d220759bSGleb Smirnoff 	struct carp_softc *sc = v;
825d220759bSGleb Smirnoff 
826d220759bSGleb Smirnoff 	CARP_SCLOCK(sc);
827d220759bSGleb Smirnoff 	carp_send_ad_locked(sc);
828d220759bSGleb Smirnoff 	CARP_SCUNLOCK(sc);
829d220759bSGleb Smirnoff }
830d220759bSGleb Smirnoff 
831d220759bSGleb Smirnoff static void
832d220759bSGleb Smirnoff carp_send_ad_locked(struct carp_softc *sc)
833d220759bSGleb Smirnoff {
834a9771948SGleb Smirnoff 	struct carp_header ch;
835a9771948SGleb Smirnoff 	struct timeval tv;
836a9771948SGleb Smirnoff 	struct carp_header *ch_ptr;
837a9771948SGleb Smirnoff 	struct mbuf *m;
838a9771948SGleb Smirnoff 	int len, advbase, advskew;
839a9771948SGleb Smirnoff 
840d220759bSGleb Smirnoff 	CARP_SCLOCK_ASSERT(sc);
841d220759bSGleb Smirnoff 
842a9771948SGleb Smirnoff 	/* bow out if we've lost our UPness or RUNNINGuiness */
84313f4c340SRobert Watson 	if (!((SC2IFP(sc)->if_flags & IFF_UP) &&
84413f4c340SRobert Watson 	    (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING))) {
845a9771948SGleb Smirnoff 		advbase = 255;
846a9771948SGleb Smirnoff 		advskew = 255;
847a9771948SGleb Smirnoff 	} else {
848a9771948SGleb Smirnoff 		advbase = sc->sc_advbase;
849a9771948SGleb Smirnoff 		if (!carp_suppress_preempt || sc->sc_advskew > 240)
850a9771948SGleb Smirnoff 			advskew = sc->sc_advskew;
851a9771948SGleb Smirnoff 		else
852a9771948SGleb Smirnoff 			advskew = 240;
853a9771948SGleb Smirnoff 		tv.tv_sec = advbase;
854a9771948SGleb Smirnoff 		tv.tv_usec = advskew * 1000000 / 256;
855a9771948SGleb Smirnoff 	}
856a9771948SGleb Smirnoff 
857a9771948SGleb Smirnoff 	ch.carp_version = CARP_VERSION;
858a9771948SGleb Smirnoff 	ch.carp_type = CARP_ADVERTISEMENT;
859a9771948SGleb Smirnoff 	ch.carp_vhid = sc->sc_vhid;
860a9771948SGleb Smirnoff 	ch.carp_advbase = advbase;
861a9771948SGleb Smirnoff 	ch.carp_advskew = advskew;
862a9771948SGleb Smirnoff 	ch.carp_authlen = 7;	/* XXX DEFINE */
863a9771948SGleb Smirnoff 	ch.carp_pad1 = 0;	/* must be zero */
864a9771948SGleb Smirnoff 	ch.carp_cksum = 0;
865a9771948SGleb Smirnoff 
866a9771948SGleb Smirnoff #ifdef INET
867a9771948SGleb Smirnoff 	if (sc->sc_ia) {
868a9771948SGleb Smirnoff 		struct ip *ip;
869a9771948SGleb Smirnoff 
870a9771948SGleb Smirnoff 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
871a9771948SGleb Smirnoff 		if (m == NULL) {
872fc74a9f9SBrooks Davis 			SC2IFP(sc)->if_oerrors++;
873a9771948SGleb Smirnoff 			carpstats.carps_onomem++;
874a9771948SGleb Smirnoff 			/* XXX maybe less ? */
875a9771948SGleb Smirnoff 			if (advbase != 255 || advskew != 255)
876a9771948SGleb Smirnoff 				callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
877a9771948SGleb Smirnoff 				    carp_send_ad, sc);
878a9771948SGleb Smirnoff 			return;
879a9771948SGleb Smirnoff 		}
880a9771948SGleb Smirnoff 		len = sizeof(*ip) + sizeof(ch);
881a9771948SGleb Smirnoff 		m->m_pkthdr.len = len;
882a9771948SGleb Smirnoff 		m->m_pkthdr.rcvif = NULL;
883a9771948SGleb Smirnoff 		m->m_len = len;
884a9771948SGleb Smirnoff 		MH_ALIGN(m, m->m_len);
885a9771948SGleb Smirnoff 		m->m_flags |= M_MCAST;
886a9771948SGleb Smirnoff 		ip = mtod(m, struct ip *);
887a9771948SGleb Smirnoff 		ip->ip_v = IPVERSION;
888a9771948SGleb Smirnoff 		ip->ip_hl = sizeof(*ip) >> 2;
889a9771948SGleb Smirnoff 		ip->ip_tos = IPTOS_LOWDELAY;
890a9771948SGleb Smirnoff 		ip->ip_len = len;
891a9771948SGleb Smirnoff 		ip->ip_id = ip_newid();
892a9771948SGleb Smirnoff 		ip->ip_off = IP_DF;
893a9771948SGleb Smirnoff 		ip->ip_ttl = CARP_DFLTTL;
894a9771948SGleb Smirnoff 		ip->ip_p = IPPROTO_CARP;
895a9771948SGleb Smirnoff 		ip->ip_sum = 0;
896a9771948SGleb Smirnoff 		ip->ip_src.s_addr = sc->sc_ia->ia_addr.sin_addr.s_addr;
897a9771948SGleb Smirnoff 		ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
898a9771948SGleb Smirnoff 
899a9771948SGleb Smirnoff 		ch_ptr = (struct carp_header *)(&ip[1]);
900a9771948SGleb Smirnoff 		bcopy(&ch, ch_ptr, sizeof(ch));
901a9771948SGleb Smirnoff 		if (carp_prepare_ad(m, sc, ch_ptr))
902a9771948SGleb Smirnoff 			return;
903a9771948SGleb Smirnoff 
904a9771948SGleb Smirnoff 		m->m_data += sizeof(*ip);
905a9771948SGleb Smirnoff 		ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip));
906a9771948SGleb Smirnoff 		m->m_data -= sizeof(*ip);
907a9771948SGleb Smirnoff 
908fc74a9f9SBrooks Davis 		getmicrotime(&SC2IFP(sc)->if_lastchange);
909fc74a9f9SBrooks Davis 		SC2IFP(sc)->if_opackets++;
910fc74a9f9SBrooks Davis 		SC2IFP(sc)->if_obytes += len;
911a9771948SGleb Smirnoff 		carpstats.carps_opackets++;
912a9771948SGleb Smirnoff 
913a9771948SGleb Smirnoff 		if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) {
914fc74a9f9SBrooks Davis 			SC2IFP(sc)->if_oerrors++;
915a9771948SGleb Smirnoff 			if (sc->sc_sendad_errors < INT_MAX)
916a9771948SGleb Smirnoff 				sc->sc_sendad_errors++;
917a9771948SGleb Smirnoff 			if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
918a9771948SGleb Smirnoff 				carp_suppress_preempt++;
919d220759bSGleb Smirnoff 				if (carp_suppress_preempt == 1) {
920d220759bSGleb Smirnoff 					CARP_SCUNLOCK(sc);
921a9771948SGleb Smirnoff 					carp_send_ad_all();
922d220759bSGleb Smirnoff 					CARP_SCLOCK(sc);
923d220759bSGleb Smirnoff 				}
924a9771948SGleb Smirnoff 			}
925a9771948SGleb Smirnoff 			sc->sc_sendad_success = 0;
926a9771948SGleb Smirnoff 		} else {
927a9771948SGleb Smirnoff 			if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
928a9771948SGleb Smirnoff 				if (++sc->sc_sendad_success >=
929a9771948SGleb Smirnoff 				    CARP_SENDAD_MIN_SUCCESS) {
930a9771948SGleb Smirnoff 					carp_suppress_preempt--;
931a9771948SGleb Smirnoff 					sc->sc_sendad_errors = 0;
932a9771948SGleb Smirnoff 				}
933a9771948SGleb Smirnoff 			} else
934a9771948SGleb Smirnoff 				sc->sc_sendad_errors = 0;
935a9771948SGleb Smirnoff 		}
936a9771948SGleb Smirnoff 	}
937a9771948SGleb Smirnoff #endif /* INET */
938a9771948SGleb Smirnoff #ifdef INET6
939a9771948SGleb Smirnoff 	if (sc->sc_ia6) {
940a9771948SGleb Smirnoff 		struct ip6_hdr *ip6;
941a9771948SGleb Smirnoff 
942a9771948SGleb Smirnoff 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
943a9771948SGleb Smirnoff 		if (m == NULL) {
944fc74a9f9SBrooks Davis 			SC2IFP(sc)->if_oerrors++;
945a9771948SGleb Smirnoff 			carpstats.carps_onomem++;
946a9771948SGleb Smirnoff 			/* XXX maybe less ? */
947a9771948SGleb Smirnoff 			if (advbase != 255 || advskew != 255)
948a9771948SGleb Smirnoff 				callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
949a9771948SGleb Smirnoff 				    carp_send_ad, sc);
950a9771948SGleb Smirnoff 			return;
951a9771948SGleb Smirnoff 		}
952a9771948SGleb Smirnoff 		len = sizeof(*ip6) + sizeof(ch);
953a9771948SGleb Smirnoff 		m->m_pkthdr.len = len;
954a9771948SGleb Smirnoff 		m->m_pkthdr.rcvif = NULL;
955a9771948SGleb Smirnoff 		m->m_len = len;
956a9771948SGleb Smirnoff 		MH_ALIGN(m, m->m_len);
957a9771948SGleb Smirnoff 		m->m_flags |= M_MCAST;
958a9771948SGleb Smirnoff 		ip6 = mtod(m, struct ip6_hdr *);
959a9771948SGleb Smirnoff 		bzero(ip6, sizeof(*ip6));
960a9771948SGleb Smirnoff 		ip6->ip6_vfc |= IPV6_VERSION;
961a9771948SGleb Smirnoff 		ip6->ip6_hlim = CARP_DFLTTL;
962a9771948SGleb Smirnoff 		ip6->ip6_nxt = IPPROTO_CARP;
963a9771948SGleb Smirnoff 		bcopy(&sc->sc_ia6->ia_addr.sin6_addr, &ip6->ip6_src,
964a9771948SGleb Smirnoff 		    sizeof(struct in6_addr));
965a9771948SGleb Smirnoff 		/* set the multicast destination */
966a9771948SGleb Smirnoff 
967a9771948SGleb Smirnoff 		ip6->ip6_dst.s6_addr8[0] = 0xff;
968a9771948SGleb Smirnoff 		ip6->ip6_dst.s6_addr8[1] = 0x02;
969a9771948SGleb Smirnoff 		ip6->ip6_dst.s6_addr8[15] = 0x12;
970a9771948SGleb Smirnoff 
971a9771948SGleb Smirnoff 		ch_ptr = (struct carp_header *)(&ip6[1]);
972a9771948SGleb Smirnoff 		bcopy(&ch, ch_ptr, sizeof(ch));
973a9771948SGleb Smirnoff 		if (carp_prepare_ad(m, sc, ch_ptr))
974a9771948SGleb Smirnoff 			return;
975a9771948SGleb Smirnoff 
976a9771948SGleb Smirnoff 		m->m_data += sizeof(*ip6);
977a9771948SGleb Smirnoff 		ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip6));
978a9771948SGleb Smirnoff 		m->m_data -= sizeof(*ip6);
979a9771948SGleb Smirnoff 
980fc74a9f9SBrooks Davis 		getmicrotime(&SC2IFP(sc)->if_lastchange);
981fc74a9f9SBrooks Davis 		SC2IFP(sc)->if_opackets++;
982fc74a9f9SBrooks Davis 		SC2IFP(sc)->if_obytes += len;
983a9771948SGleb Smirnoff 		carpstats.carps_opackets6++;
984a9771948SGleb Smirnoff 
985a9771948SGleb Smirnoff 		if (ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL)) {
986fc74a9f9SBrooks Davis 			SC2IFP(sc)->if_oerrors++;
987a9771948SGleb Smirnoff 			if (sc->sc_sendad_errors < INT_MAX)
988a9771948SGleb Smirnoff 				sc->sc_sendad_errors++;
989a9771948SGleb Smirnoff 			if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
990a9771948SGleb Smirnoff 				carp_suppress_preempt++;
991d220759bSGleb Smirnoff 				if (carp_suppress_preempt == 1) {
992d220759bSGleb Smirnoff 					CARP_SCUNLOCK(sc);
993a9771948SGleb Smirnoff 					carp_send_ad_all();
994d220759bSGleb Smirnoff 					CARP_SCLOCK(sc);
995d220759bSGleb Smirnoff 				}
996a9771948SGleb Smirnoff 			}
997a9771948SGleb Smirnoff 			sc->sc_sendad_success = 0;
998a9771948SGleb Smirnoff 		} else {
999a9771948SGleb Smirnoff 			if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
1000a9771948SGleb Smirnoff 				if (++sc->sc_sendad_success >=
1001a9771948SGleb Smirnoff 				    CARP_SENDAD_MIN_SUCCESS) {
1002a9771948SGleb Smirnoff 					carp_suppress_preempt--;
1003a9771948SGleb Smirnoff 					sc->sc_sendad_errors = 0;
1004a9771948SGleb Smirnoff 				}
1005a9771948SGleb Smirnoff 			} else
1006a9771948SGleb Smirnoff 				sc->sc_sendad_errors = 0;
1007a9771948SGleb Smirnoff 		}
1008a9771948SGleb Smirnoff 	}
1009a9771948SGleb Smirnoff #endif /* INET6 */
1010a9771948SGleb Smirnoff 
1011a9771948SGleb Smirnoff 	if (advbase != 255 || advskew != 255)
1012a9771948SGleb Smirnoff 		callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1013a9771948SGleb Smirnoff 		    carp_send_ad, sc);
1014a9771948SGleb Smirnoff 
1015a9771948SGleb Smirnoff }
1016a9771948SGleb Smirnoff 
1017a9771948SGleb Smirnoff /*
1018a9771948SGleb Smirnoff  * Broadcast a gratuitous ARP request containing
1019a9771948SGleb Smirnoff  * the virtual router MAC address for each IP address
1020a9771948SGleb Smirnoff  * associated with the virtual router.
1021a9771948SGleb Smirnoff  */
10225c1f0f6dSGleb Smirnoff static void
1023a9771948SGleb Smirnoff carp_send_arp(struct carp_softc *sc)
1024a9771948SGleb Smirnoff {
1025a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1026a9771948SGleb Smirnoff 
1027fc74a9f9SBrooks Davis 	TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1028a9771948SGleb Smirnoff 
1029a9771948SGleb Smirnoff 		if (ifa->ifa_addr->sa_family != AF_INET)
1030a9771948SGleb Smirnoff 			continue;
1031a9771948SGleb Smirnoff 
10324a0d6638SRuslan Ermilov /*		arprequest(sc->sc_carpdev, &in, &in, IF_LLADDR(sc->sc_ifp)); */
10334a0d6638SRuslan Ermilov 		arp_ifinit2(sc->sc_carpdev, ifa, IF_LLADDR(sc->sc_ifp));
1034a9771948SGleb Smirnoff 
1035a9771948SGleb Smirnoff 		DELAY(1000);	/* XXX */
1036a9771948SGleb Smirnoff 	}
1037a9771948SGleb Smirnoff }
1038a9771948SGleb Smirnoff 
1039a9771948SGleb Smirnoff #ifdef INET6
10405c1f0f6dSGleb Smirnoff static void
1041a9771948SGleb Smirnoff carp_send_na(struct carp_softc *sc)
1042a9771948SGleb Smirnoff {
1043a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1044a9771948SGleb Smirnoff 	struct in6_addr *in6;
1045a9771948SGleb Smirnoff 	static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1046a9771948SGleb Smirnoff 
1047fc74a9f9SBrooks Davis 	TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1048a9771948SGleb Smirnoff 
1049a9771948SGleb Smirnoff 		if (ifa->ifa_addr->sa_family != AF_INET6)
1050a9771948SGleb Smirnoff 			continue;
1051a9771948SGleb Smirnoff 
1052a9771948SGleb Smirnoff 		in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
1053e8c34a71SGleb Smirnoff 		nd6_na_output(sc->sc_carpdev, &mcast, in6,
1054a9771948SGleb Smirnoff 		    ND_NA_FLAG_OVERRIDE, 1, NULL);
1055a9771948SGleb Smirnoff 		DELAY(1000);	/* XXX */
1056a9771948SGleb Smirnoff 	}
1057a9771948SGleb Smirnoff }
1058a9771948SGleb Smirnoff #endif /* INET6 */
1059a9771948SGleb Smirnoff 
10605c1f0f6dSGleb Smirnoff static int
1061a9771948SGleb Smirnoff carp_addrcount(struct carp_if *cif, struct in_ifaddr *ia, int type)
1062a9771948SGleb Smirnoff {
1063a9771948SGleb Smirnoff 	struct carp_softc *vh;
1064a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1065a9771948SGleb Smirnoff 	int count = 0;
1066a9771948SGleb Smirnoff 
1067d220759bSGleb Smirnoff 	CARP_LOCK_ASSERT(cif);
1068d220759bSGleb Smirnoff 
1069a9771948SGleb Smirnoff 	TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1070a9771948SGleb Smirnoff 		if ((type == CARP_COUNT_RUNNING &&
107113f4c340SRobert Watson 		    (SC2IFP(vh)->if_flags & IFF_UP) &&
107213f4c340SRobert Watson 		    (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING)) ||
1073a9771948SGleb Smirnoff 		    (type == CARP_COUNT_MASTER && vh->sc_state == MASTER)) {
1074fc74a9f9SBrooks Davis 			TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist,
1075a9771948SGleb Smirnoff 			    ifa_list) {
1076a9771948SGleb Smirnoff 				if (ifa->ifa_addr->sa_family == AF_INET &&
1077a9771948SGleb Smirnoff 				    ia->ia_addr.sin_addr.s_addr ==
1078a9771948SGleb Smirnoff 				    ifatoia(ifa)->ia_addr.sin_addr.s_addr)
1079a9771948SGleb Smirnoff 					count++;
1080a9771948SGleb Smirnoff 			}
1081a9771948SGleb Smirnoff 		}
1082a9771948SGleb Smirnoff 	}
1083a9771948SGleb Smirnoff 	return (count);
1084a9771948SGleb Smirnoff }
1085a9771948SGleb Smirnoff 
1086a9771948SGleb Smirnoff int
1087a9771948SGleb Smirnoff carp_iamatch(void *v, struct in_ifaddr *ia,
1088a9771948SGleb Smirnoff     struct in_addr *isaddr, u_int8_t **enaddr)
1089a9771948SGleb Smirnoff {
1090a9771948SGleb Smirnoff 	struct carp_if *cif = v;
1091a9771948SGleb Smirnoff 	struct carp_softc *vh;
1092a9771948SGleb Smirnoff 	int index, count = 0;
1093a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1094a9771948SGleb Smirnoff 
1095a9771948SGleb Smirnoff 	CARP_LOCK(cif);
1096a9771948SGleb Smirnoff 
1097a9771948SGleb Smirnoff 	if (carp_opts[CARPCTL_ARPBALANCE]) {
1098a9771948SGleb Smirnoff 		/*
1099a9771948SGleb Smirnoff 		 * XXX proof of concept implementation.
1100a9771948SGleb Smirnoff 		 * We use the source ip to decide which virtual host should
1101a9771948SGleb Smirnoff 		 * handle the request. If we're master of that virtual host,
1102a9771948SGleb Smirnoff 		 * then we respond, otherwise, just drop the arp packet on
1103a9771948SGleb Smirnoff 		 * the floor.
1104a9771948SGleb Smirnoff 		 */
1105a9771948SGleb Smirnoff 		count = carp_addrcount(cif, ia, CARP_COUNT_RUNNING);
1106a9771948SGleb Smirnoff 		if (count == 0) {
1107a9771948SGleb Smirnoff 			/* should never reach this */
1108a9771948SGleb Smirnoff 			CARP_UNLOCK(cif);
1109a9771948SGleb Smirnoff 			return (0);
1110a9771948SGleb Smirnoff 		}
1111a9771948SGleb Smirnoff 
1112a9771948SGleb Smirnoff 		/* this should be a hash, like pf_hash() */
1113a196a3c8SGleb Smirnoff 		index = ntohl(isaddr->s_addr) % count;
1114a9771948SGleb Smirnoff 		count = 0;
1115a9771948SGleb Smirnoff 
1116a9771948SGleb Smirnoff 		TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
111713f4c340SRobert Watson 			if ((SC2IFP(vh)->if_flags & IFF_UP) &&
111813f4c340SRobert Watson 			    (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING)) {
1119fc74a9f9SBrooks Davis 				TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist,
1120a9771948SGleb Smirnoff 				    ifa_list) {
1121a9771948SGleb Smirnoff 					if (ifa->ifa_addr->sa_family ==
1122a9771948SGleb Smirnoff 					    AF_INET &&
1123a9771948SGleb Smirnoff 					    ia->ia_addr.sin_addr.s_addr ==
1124a9771948SGleb Smirnoff 					    ifatoia(ifa)->ia_addr.sin_addr.s_addr) {
1125a9771948SGleb Smirnoff 						if (count == index) {
1126a9771948SGleb Smirnoff 							if (vh->sc_state ==
1127a9771948SGleb Smirnoff 							    MASTER) {
11284a0d6638SRuslan Ermilov 								*enaddr = IF_LLADDR(vh->sc_ifp);
1129a9771948SGleb Smirnoff 								CARP_UNLOCK(cif);
1130a9771948SGleb Smirnoff 								return (1);
1131a9771948SGleb Smirnoff 							} else {
1132a9771948SGleb Smirnoff 								CARP_UNLOCK(cif);
1133a9771948SGleb Smirnoff 								return (0);
1134a9771948SGleb Smirnoff 							}
1135a9771948SGleb Smirnoff 						}
1136a9771948SGleb Smirnoff 						count++;
1137a9771948SGleb Smirnoff 					}
1138a9771948SGleb Smirnoff 				}
1139a9771948SGleb Smirnoff 			}
1140a9771948SGleb Smirnoff 		}
1141a9771948SGleb Smirnoff 	} else {
1142a9771948SGleb Smirnoff 		TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
114313f4c340SRobert Watson 			if ((SC2IFP(vh)->if_flags & IFF_UP) &&
114413f4c340SRobert Watson 			    (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
114521883761SGleb Smirnoff 			    ia->ia_ifp == SC2IFP(vh) &&
114621883761SGleb Smirnoff 			    vh->sc_state == MASTER) {
11474a0d6638SRuslan Ermilov 				*enaddr = IF_LLADDR(vh->sc_ifp);
1148a9771948SGleb Smirnoff 				CARP_UNLOCK(cif);
1149a9771948SGleb Smirnoff 				return (1);
1150a9771948SGleb Smirnoff 			}
1151a9771948SGleb Smirnoff 		}
1152a9771948SGleb Smirnoff 	}
1153a9771948SGleb Smirnoff 	CARP_UNLOCK(cif);
1154a9771948SGleb Smirnoff 	return (0);
1155a9771948SGleb Smirnoff }
1156a9771948SGleb Smirnoff 
1157a9771948SGleb Smirnoff #ifdef INET6
1158a4e53905SMax Laier struct ifaddr *
1159a9771948SGleb Smirnoff carp_iamatch6(void *v, struct in6_addr *taddr)
1160a9771948SGleb Smirnoff {
1161a9771948SGleb Smirnoff 	struct carp_if *cif = v;
1162a9771948SGleb Smirnoff 	struct carp_softc *vh;
1163a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1164a9771948SGleb Smirnoff 
1165a9771948SGleb Smirnoff 	CARP_LOCK(cif);
1166a9771948SGleb Smirnoff 	TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1167fc74a9f9SBrooks Davis 		TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist, ifa_list) {
1168a9771948SGleb Smirnoff 			if (IN6_ARE_ADDR_EQUAL(taddr,
1169a9771948SGleb Smirnoff 			    &ifatoia6(ifa)->ia_addr.sin6_addr) &&
117013f4c340SRobert Watson  			    (SC2IFP(vh)->if_flags & IFF_UP) &&
117121883761SGleb Smirnoff 			    (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
117221883761SGleb Smirnoff 			    vh->sc_state == MASTER) {
1173a9771948SGleb Smirnoff 			    	CARP_UNLOCK(cif);
1174a9771948SGleb Smirnoff 				return (ifa);
1175a9771948SGleb Smirnoff 			}
1176a9771948SGleb Smirnoff 		}
1177a9771948SGleb Smirnoff 	}
1178a9771948SGleb Smirnoff 	CARP_UNLOCK(cif);
1179a9771948SGleb Smirnoff 
1180a9771948SGleb Smirnoff 	return (NULL);
1181a9771948SGleb Smirnoff }
1182a9771948SGleb Smirnoff 
1183a4e53905SMax Laier void *
1184a9771948SGleb Smirnoff carp_macmatch6(void *v, struct mbuf *m, const struct in6_addr *taddr)
1185a9771948SGleb Smirnoff {
1186a9771948SGleb Smirnoff 	struct m_tag *mtag;
1187a9771948SGleb Smirnoff 	struct carp_if *cif = v;
1188a9771948SGleb Smirnoff 	struct carp_softc *sc;
1189a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1190a9771948SGleb Smirnoff 
1191a9771948SGleb Smirnoff 	CARP_LOCK(cif);
1192a9771948SGleb Smirnoff 	TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
1193fc74a9f9SBrooks Davis 		TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1194a9771948SGleb Smirnoff 			if (IN6_ARE_ADDR_EQUAL(taddr,
1195a9771948SGleb Smirnoff 			    &ifatoia6(ifa)->ia_addr.sin6_addr) &&
119613f4c340SRobert Watson  			    (SC2IFP(sc)->if_flags & IFF_UP) &&
119713f4c340SRobert Watson 			    (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING)) {
1198fc74a9f9SBrooks Davis 				struct ifnet *ifp = SC2IFP(sc);
1199a9771948SGleb Smirnoff 				mtag = m_tag_get(PACKET_TAG_CARP,
1200a9771948SGleb Smirnoff 				    sizeof(struct ifnet *), M_NOWAIT);
1201a9771948SGleb Smirnoff 				if (mtag == NULL) {
1202a9771948SGleb Smirnoff 					/* better a bit than nothing */
1203a9771948SGleb Smirnoff 					CARP_UNLOCK(cif);
12044a0d6638SRuslan Ermilov 					return (IF_LLADDR(sc->sc_ifp));
1205a9771948SGleb Smirnoff 				}
1206a9771948SGleb Smirnoff 				bcopy(&ifp, (caddr_t)(mtag + 1),
1207a9771948SGleb Smirnoff 				    sizeof(struct ifnet *));
1208a9771948SGleb Smirnoff 				m_tag_prepend(m, mtag);
1209a9771948SGleb Smirnoff 
1210a9771948SGleb Smirnoff 				CARP_UNLOCK(cif);
12114a0d6638SRuslan Ermilov 				return (IF_LLADDR(sc->sc_ifp));
1212a9771948SGleb Smirnoff 			}
1213a9771948SGleb Smirnoff 		}
1214a9771948SGleb Smirnoff 	}
1215a9771948SGleb Smirnoff 	CARP_UNLOCK(cif);
1216a9771948SGleb Smirnoff 
1217a9771948SGleb Smirnoff 	return (NULL);
1218a9771948SGleb Smirnoff }
1219a9771948SGleb Smirnoff #endif
1220a9771948SGleb Smirnoff 
1221a9771948SGleb Smirnoff struct ifnet *
1222a9771948SGleb Smirnoff carp_forus(void *v, void *dhost)
1223a9771948SGleb Smirnoff {
1224a9771948SGleb Smirnoff 	struct carp_if *cif = v;
1225a9771948SGleb Smirnoff 	struct carp_softc *vh;
1226a9771948SGleb Smirnoff 	u_int8_t *ena = dhost;
1227a9771948SGleb Smirnoff 
1228a9771948SGleb Smirnoff 	if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1229a9771948SGleb Smirnoff 		return (NULL);
1230a9771948SGleb Smirnoff 
1231a9771948SGleb Smirnoff 	CARP_LOCK(cif);
1232a9771948SGleb Smirnoff 	TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list)
123313f4c340SRobert Watson 		if ((SC2IFP(vh)->if_flags & IFF_UP) &&
123413f4c340SRobert Watson 		    (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
123513f4c340SRobert Watson 		    vh->sc_state == MASTER &&
12364a0d6638SRuslan Ermilov 		    !bcmp(dhost, IF_LLADDR(vh->sc_ifp), ETHER_ADDR_LEN)) {
1237a9771948SGleb Smirnoff 		    	CARP_UNLOCK(cif);
1238fc74a9f9SBrooks Davis 			return (SC2IFP(vh));
1239a9771948SGleb Smirnoff 		}
1240a9771948SGleb Smirnoff 
1241a9771948SGleb Smirnoff     	CARP_UNLOCK(cif);
1242a9771948SGleb Smirnoff 	return (NULL);
1243a9771948SGleb Smirnoff }
1244a9771948SGleb Smirnoff 
12455c1f0f6dSGleb Smirnoff static void
1246a9771948SGleb Smirnoff carp_master_down(void *v)
1247a9771948SGleb Smirnoff {
1248a9771948SGleb Smirnoff 	struct carp_softc *sc = v;
1249a9771948SGleb Smirnoff 
1250d220759bSGleb Smirnoff 	CARP_SCLOCK(sc);
1251d220759bSGleb Smirnoff 	carp_master_down_locked(sc);
1252d220759bSGleb Smirnoff 	CARP_SCUNLOCK(sc);
1253d220759bSGleb Smirnoff }
1254d220759bSGleb Smirnoff 
1255d220759bSGleb Smirnoff static void
1256d220759bSGleb Smirnoff carp_master_down_locked(struct carp_softc *sc)
1257d220759bSGleb Smirnoff {
1258d220759bSGleb Smirnoff 	if (sc->sc_carpdev)
1259d220759bSGleb Smirnoff 		CARP_SCLOCK_ASSERT(sc);
1260d220759bSGleb Smirnoff 
1261a9771948SGleb Smirnoff 	switch (sc->sc_state) {
1262a9771948SGleb Smirnoff 	case INIT:
1263a9771948SGleb Smirnoff 		printf("%s: master_down event in INIT state\n",
1264fc74a9f9SBrooks Davis 		    SC2IFP(sc)->if_xname);
1265a9771948SGleb Smirnoff 		break;
1266a9771948SGleb Smirnoff 	case MASTER:
1267a9771948SGleb Smirnoff 		break;
1268a9771948SGleb Smirnoff 	case BACKUP:
1269a9771948SGleb Smirnoff 		carp_set_state(sc, MASTER);
1270d220759bSGleb Smirnoff 		carp_send_ad_locked(sc);
1271a9771948SGleb Smirnoff 		carp_send_arp(sc);
1272a9771948SGleb Smirnoff #ifdef INET6
1273a9771948SGleb Smirnoff 		carp_send_na(sc);
1274a9771948SGleb Smirnoff #endif /* INET6 */
1275a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
1276a9771948SGleb Smirnoff 		carp_setroute(sc, RTM_ADD);
1277a9771948SGleb Smirnoff 		break;
1278a9771948SGleb Smirnoff 	}
1279a9771948SGleb Smirnoff }
1280a9771948SGleb Smirnoff 
1281a9771948SGleb Smirnoff /*
1282a9771948SGleb Smirnoff  * When in backup state, af indicates whether to reset the master down timer
1283a9771948SGleb Smirnoff  * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1284a9771948SGleb Smirnoff  */
12855c1f0f6dSGleb Smirnoff static void
1286a9771948SGleb Smirnoff carp_setrun(struct carp_softc *sc, sa_family_t af)
1287a9771948SGleb Smirnoff {
1288a9771948SGleb Smirnoff 	struct timeval tv;
1289a9771948SGleb Smirnoff 
12900fa08018SGleb Smirnoff 	if (sc->sc_carpdev == NULL) {
12910fa08018SGleb Smirnoff 		SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
12920fa08018SGleb Smirnoff 		carp_set_state(sc, INIT);
12930fa08018SGleb Smirnoff 		return;
12940fa08018SGleb Smirnoff 	} else
1295d220759bSGleb Smirnoff 		CARP_SCLOCK_ASSERT(sc);
1296d220759bSGleb Smirnoff 
1297fc74a9f9SBrooks Davis 	if (SC2IFP(sc)->if_flags & IFF_UP &&
1298a9771948SGleb Smirnoff 	    sc->sc_vhid > 0 && (sc->sc_naddrs || sc->sc_naddrs6))
129913f4c340SRobert Watson 		SC2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
1300a9771948SGleb Smirnoff 	else {
130113f4c340SRobert Watson 		SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1302a9771948SGleb Smirnoff 		carp_setroute(sc, RTM_DELETE);
1303a9771948SGleb Smirnoff 		return;
1304a9771948SGleb Smirnoff 	}
1305a9771948SGleb Smirnoff 
1306a9771948SGleb Smirnoff 	switch (sc->sc_state) {
1307a9771948SGleb Smirnoff 	case INIT:
1308a9771948SGleb Smirnoff 		if (carp_opts[CARPCTL_PREEMPT] && !carp_suppress_preempt) {
1309d220759bSGleb Smirnoff 			carp_send_ad_locked(sc);
1310a9771948SGleb Smirnoff 			carp_send_arp(sc);
1311a9771948SGleb Smirnoff #ifdef INET6
1312a9771948SGleb Smirnoff 			carp_send_na(sc);
1313a9771948SGleb Smirnoff #endif /* INET6 */
131488bf82a6SGleb Smirnoff 			CARP_DEBUG("%s: INIT -> MASTER (preempting)\n",
1315fc74a9f9SBrooks Davis 			    SC2IFP(sc)->if_xname);
1316a9771948SGleb Smirnoff 			carp_set_state(sc, MASTER);
1317a9771948SGleb Smirnoff 			carp_setroute(sc, RTM_ADD);
1318a9771948SGleb Smirnoff 		} else {
1319fc74a9f9SBrooks Davis 			CARP_DEBUG("%s: INIT -> BACKUP\n", SC2IFP(sc)->if_xname);
1320a9771948SGleb Smirnoff 			carp_set_state(sc, BACKUP);
1321a9771948SGleb Smirnoff 			carp_setroute(sc, RTM_DELETE);
1322a9771948SGleb Smirnoff 			carp_setrun(sc, 0);
1323a9771948SGleb Smirnoff 		}
1324a9771948SGleb Smirnoff 		break;
1325a9771948SGleb Smirnoff 	case BACKUP:
1326a9771948SGleb Smirnoff 		callout_stop(&sc->sc_ad_tmo);
1327a9771948SGleb Smirnoff 		tv.tv_sec = 3 * sc->sc_advbase;
1328a9771948SGleb Smirnoff 		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1329a9771948SGleb Smirnoff 		switch (af) {
1330a9771948SGleb Smirnoff #ifdef INET
1331a9771948SGleb Smirnoff 		case AF_INET:
1332a9771948SGleb Smirnoff 			callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1333a9771948SGleb Smirnoff 			    carp_master_down, sc);
1334a9771948SGleb Smirnoff 			break;
1335a9771948SGleb Smirnoff #endif /* INET */
1336a9771948SGleb Smirnoff #ifdef INET6
1337a9771948SGleb Smirnoff 		case AF_INET6:
1338a9771948SGleb Smirnoff 			callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1339a9771948SGleb Smirnoff 			    carp_master_down, sc);
1340a9771948SGleb Smirnoff 			break;
1341a9771948SGleb Smirnoff #endif /* INET6 */
1342a9771948SGleb Smirnoff 		default:
1343a9771948SGleb Smirnoff 			if (sc->sc_naddrs)
1344a9771948SGleb Smirnoff 				callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1345a9771948SGleb Smirnoff 				    carp_master_down, sc);
1346a9771948SGleb Smirnoff 			if (sc->sc_naddrs6)
1347a9771948SGleb Smirnoff 				callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1348a9771948SGleb Smirnoff 				    carp_master_down, sc);
1349a9771948SGleb Smirnoff 			break;
1350a9771948SGleb Smirnoff 		}
1351a9771948SGleb Smirnoff 		break;
1352a9771948SGleb Smirnoff 	case MASTER:
1353a9771948SGleb Smirnoff 		tv.tv_sec = sc->sc_advbase;
1354a9771948SGleb Smirnoff 		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1355a9771948SGleb Smirnoff 		callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1356a9771948SGleb Smirnoff 		    carp_send_ad, sc);
1357a9771948SGleb Smirnoff 		break;
1358a9771948SGleb Smirnoff 	}
1359a9771948SGleb Smirnoff }
1360a9771948SGleb Smirnoff 
13610fa08018SGleb Smirnoff void
13620fa08018SGleb Smirnoff carp_multicast_cleanup(struct carp_softc *sc)
13630fa08018SGleb Smirnoff {
13640fa08018SGleb Smirnoff 	struct ip_moptions *imo = &sc->sc_imo;
13650fa08018SGleb Smirnoff #ifdef INET6
13660fa08018SGleb Smirnoff 	struct ip6_moptions *im6o = &sc->sc_im6o;
13670fa08018SGleb Smirnoff #endif
13680fa08018SGleb Smirnoff 	u_int16_t n = imo->imo_num_memberships;
13690fa08018SGleb Smirnoff 
13700fa08018SGleb Smirnoff 	/* Clean up our own multicast memberships */
13710fa08018SGleb Smirnoff 	while (n-- > 0) {
13720fa08018SGleb Smirnoff 		if (imo->imo_membership[n] != NULL) {
13730fa08018SGleb Smirnoff 			in_delmulti(imo->imo_membership[n]);
13740fa08018SGleb Smirnoff 			imo->imo_membership[n] = NULL;
13750fa08018SGleb Smirnoff 		}
13760fa08018SGleb Smirnoff 	}
13770fa08018SGleb Smirnoff 	imo->imo_num_memberships = 0;
13780fa08018SGleb Smirnoff 	imo->imo_multicast_ifp = NULL;
13790fa08018SGleb Smirnoff 
13800fa08018SGleb Smirnoff #ifdef INET6
13810fa08018SGleb Smirnoff 	while (!LIST_EMPTY(&im6o->im6o_memberships)) {
13820fa08018SGleb Smirnoff 		struct in6_multi_mship *imm =
13830fa08018SGleb Smirnoff 		    LIST_FIRST(&im6o->im6o_memberships);
13840fa08018SGleb Smirnoff 
13850fa08018SGleb Smirnoff 		LIST_REMOVE(imm, i6mm_chain);
13860fa08018SGleb Smirnoff 		in6_leavegroup(imm);
13870fa08018SGleb Smirnoff 	}
13880fa08018SGleb Smirnoff 	im6o->im6o_multicast_ifp = NULL;
13890fa08018SGleb Smirnoff #endif
13900fa08018SGleb Smirnoff }
13910fa08018SGleb Smirnoff 
13925c1f0f6dSGleb Smirnoff static int
1393a9771948SGleb Smirnoff carp_set_addr(struct carp_softc *sc, struct sockaddr_in *sin)
1394a9771948SGleb Smirnoff {
1395a9771948SGleb Smirnoff 	struct ifnet *ifp;
1396a9771948SGleb Smirnoff 	struct carp_if *cif;
1397a9771948SGleb Smirnoff 	struct in_ifaddr *ia, *ia_if;
1398a9771948SGleb Smirnoff 	struct ip_moptions *imo = &sc->sc_imo;
1399a9771948SGleb Smirnoff 	struct in_addr addr;
1400a9771948SGleb Smirnoff 	u_long iaddr = htonl(sin->sin_addr.s_addr);
1401a9771948SGleb Smirnoff 	int own, error;
1402a9771948SGleb Smirnoff 
1403a9771948SGleb Smirnoff 	if (sin->sin_addr.s_addr == 0) {
1404fc74a9f9SBrooks Davis 		if (!(SC2IFP(sc)->if_flags & IFF_UP))
1405a9771948SGleb Smirnoff 			carp_set_state(sc, INIT);
1406a9771948SGleb Smirnoff 		if (sc->sc_naddrs)
1407fc74a9f9SBrooks Davis 			SC2IFP(sc)->if_flags |= IFF_UP;
1408a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
1409a9771948SGleb Smirnoff 		return (0);
1410a9771948SGleb Smirnoff 	}
1411a9771948SGleb Smirnoff 
1412a9771948SGleb Smirnoff 	/* we have to do it by hands to check we won't match on us */
1413a9771948SGleb Smirnoff 	ia_if = NULL; own = 0;
1414a9771948SGleb Smirnoff 	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
1415a9771948SGleb Smirnoff 		/* and, yeah, we need a multicast-capable iface too */
1416fc74a9f9SBrooks Davis 		if (ia->ia_ifp != SC2IFP(sc) &&
1417a9771948SGleb Smirnoff 		    (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1418a9771948SGleb Smirnoff 		    (iaddr & ia->ia_subnetmask) == ia->ia_subnet) {
1419a9771948SGleb Smirnoff 			if (!ia_if)
1420a9771948SGleb Smirnoff 				ia_if = ia;
1421a9771948SGleb Smirnoff 			if (sin->sin_addr.s_addr ==
1422a9771948SGleb Smirnoff 			    ia->ia_addr.sin_addr.s_addr)
1423a9771948SGleb Smirnoff 				own++;
1424a9771948SGleb Smirnoff 		}
1425a9771948SGleb Smirnoff 	}
1426a9771948SGleb Smirnoff 
1427a9771948SGleb Smirnoff 	if (!ia_if)
1428a9771948SGleb Smirnoff 		return (EADDRNOTAVAIL);
1429a9771948SGleb Smirnoff 
1430a9771948SGleb Smirnoff 	ia = ia_if;
1431a9771948SGleb Smirnoff 	ifp = ia->ia_ifp;
1432a9771948SGleb Smirnoff 
1433a9771948SGleb Smirnoff 	if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1434a9771948SGleb Smirnoff 	    (imo->imo_multicast_ifp && imo->imo_multicast_ifp != ifp))
1435a9771948SGleb Smirnoff 		return (EADDRNOTAVAIL);
1436a9771948SGleb Smirnoff 
1437a9771948SGleb Smirnoff 	if (imo->imo_num_memberships == 0) {
1438a9771948SGleb Smirnoff 		addr.s_addr = htonl(INADDR_CARP_GROUP);
1439a9771948SGleb Smirnoff 		if ((imo->imo_membership[0] = in_addmulti(&addr, ifp)) == NULL)
1440a9771948SGleb Smirnoff 			return (ENOBUFS);
1441a9771948SGleb Smirnoff 		imo->imo_num_memberships++;
1442a9771948SGleb Smirnoff 		imo->imo_multicast_ifp = ifp;
1443a9771948SGleb Smirnoff 		imo->imo_multicast_ttl = CARP_DFLTTL;
1444a9771948SGleb Smirnoff 		imo->imo_multicast_loop = 0;
1445a9771948SGleb Smirnoff 	}
1446a9771948SGleb Smirnoff 
1447a9771948SGleb Smirnoff 	if (!ifp->if_carp) {
1448a9771948SGleb Smirnoff 
1449a9771948SGleb Smirnoff 		MALLOC(cif, struct carp_if *, sizeof(*cif), M_CARP,
1450a9771948SGleb Smirnoff 		    M_WAITOK|M_ZERO);
1451a9771948SGleb Smirnoff 		if (!cif) {
1452a9771948SGleb Smirnoff 			error = ENOBUFS;
1453a9771948SGleb Smirnoff 			goto cleanup;
1454a9771948SGleb Smirnoff 		}
1455a9771948SGleb Smirnoff 		if ((error = ifpromisc(ifp, 1))) {
1456a9771948SGleb Smirnoff 			FREE(cif, M_CARP);
1457a9771948SGleb Smirnoff 			goto cleanup;
1458a9771948SGleb Smirnoff 		}
1459a9771948SGleb Smirnoff 
1460a9771948SGleb Smirnoff 		CARP_LOCK_INIT(cif);
1461a9771948SGleb Smirnoff 		CARP_LOCK(cif);
1462a9771948SGleb Smirnoff 		cif->vhif_ifp = ifp;
1463a9771948SGleb Smirnoff 		TAILQ_INIT(&cif->vhif_vrs);
1464a9771948SGleb Smirnoff 		ifp->if_carp = cif;
1465a9771948SGleb Smirnoff 
1466a9771948SGleb Smirnoff 	} else {
1467a9771948SGleb Smirnoff 		struct carp_softc *vr;
1468a9771948SGleb Smirnoff 
1469a9771948SGleb Smirnoff 		cif = (struct carp_if *)ifp->if_carp;
1470a9771948SGleb Smirnoff 		CARP_LOCK(cif);
1471a9771948SGleb Smirnoff 		TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1472a9771948SGleb Smirnoff 			if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1473a9771948SGleb Smirnoff 				CARP_UNLOCK(cif);
1474a9771948SGleb Smirnoff 				error = EINVAL;
1475a9771948SGleb Smirnoff 				goto cleanup;
1476a9771948SGleb Smirnoff 			}
1477a9771948SGleb Smirnoff 	}
1478a9771948SGleb Smirnoff 	sc->sc_ia = ia;
1479e8c34a71SGleb Smirnoff 	sc->sc_carpdev = ifp;
1480a9771948SGleb Smirnoff 
1481a9771948SGleb Smirnoff 	{ /* XXX prevent endless loop if already in queue */
1482a9771948SGleb Smirnoff 	struct carp_softc *vr, *after = NULL;
1483a9771948SGleb Smirnoff 	int myself = 0;
1484a9771948SGleb Smirnoff 	cif = (struct carp_if *)ifp->if_carp;
1485a9771948SGleb Smirnoff 
1486a9771948SGleb Smirnoff 	/* XXX: cif should not change, right? So we still hold the lock */
1487a9771948SGleb Smirnoff 	CARP_LOCK_ASSERT(cif);
1488a9771948SGleb Smirnoff 
1489a9771948SGleb Smirnoff 	TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1490a9771948SGleb Smirnoff 		if (vr == sc)
1491a9771948SGleb Smirnoff 			myself = 1;
1492a9771948SGleb Smirnoff 		if (vr->sc_vhid < sc->sc_vhid)
1493a9771948SGleb Smirnoff 			after = vr;
1494a9771948SGleb Smirnoff 	}
1495a9771948SGleb Smirnoff 
1496a9771948SGleb Smirnoff 	if (!myself) {
1497a9771948SGleb Smirnoff 		/* We're trying to keep things in order */
1498a9771948SGleb Smirnoff 		if (after == NULL) {
1499a9771948SGleb Smirnoff 			TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1500a9771948SGleb Smirnoff 		} else {
1501a9771948SGleb Smirnoff 			TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1502a9771948SGleb Smirnoff 		}
1503a9771948SGleb Smirnoff 		cif->vhif_nvrs++;
1504a9771948SGleb Smirnoff 	}
1505a9771948SGleb Smirnoff 	}
1506a9771948SGleb Smirnoff 
1507a9771948SGleb Smirnoff 	sc->sc_naddrs++;
1508fc74a9f9SBrooks Davis 	SC2IFP(sc)->if_flags |= IFF_UP;
1509a9771948SGleb Smirnoff 	if (own)
1510a9771948SGleb Smirnoff 		sc->sc_advskew = 0;
15114cb39345SGleb Smirnoff 	carp_sc_state_locked(sc);
1512a9771948SGleb Smirnoff 	carp_setrun(sc, 0);
1513a9771948SGleb Smirnoff 
1514d220759bSGleb Smirnoff 	CARP_UNLOCK(cif);
1515d220759bSGleb Smirnoff 
1516a9771948SGleb Smirnoff 	return (0);
1517a9771948SGleb Smirnoff 
1518a9771948SGleb Smirnoff cleanup:
1519a9771948SGleb Smirnoff 	in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1520a9771948SGleb Smirnoff 	return (error);
1521a9771948SGleb Smirnoff }
1522a9771948SGleb Smirnoff 
15235c1f0f6dSGleb Smirnoff static int
1524a9771948SGleb Smirnoff carp_del_addr(struct carp_softc *sc, struct sockaddr_in *sin)
1525a9771948SGleb Smirnoff {
1526a9771948SGleb Smirnoff 	int error = 0;
1527a9771948SGleb Smirnoff 
1528a9771948SGleb Smirnoff 	if (!--sc->sc_naddrs) {
1529e8c34a71SGleb Smirnoff 		struct carp_if *cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1530a9771948SGleb Smirnoff 		struct ip_moptions *imo = &sc->sc_imo;
1531a9771948SGleb Smirnoff 
1532d220759bSGleb Smirnoff 		CARP_LOCK(cif);
1533a9771948SGleb Smirnoff 		callout_stop(&sc->sc_ad_tmo);
153413f4c340SRobert Watson 		SC2IFP(sc)->if_flags &= ~IFF_UP;
153513f4c340SRobert Watson 		SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1536a9771948SGleb Smirnoff 		sc->sc_vhid = -1;
1537a9771948SGleb Smirnoff 		in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1538a9771948SGleb Smirnoff 		imo->imo_multicast_ifp = NULL;
1539a9771948SGleb Smirnoff 		TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1540a9771948SGleb Smirnoff 		if (!--cif->vhif_nvrs) {
1541e8c34a71SGleb Smirnoff 			sc->sc_carpdev->if_carp = NULL;
1542a9771948SGleb Smirnoff 			CARP_LOCK_DESTROY(cif);
1543a9771948SGleb Smirnoff 			FREE(cif, M_IFADDR);
1544a9771948SGleb Smirnoff 		} else {
1545a9771948SGleb Smirnoff 			CARP_UNLOCK(cif);
1546a9771948SGleb Smirnoff 		}
1547a9771948SGleb Smirnoff 	}
1548a9771948SGleb Smirnoff 
1549a9771948SGleb Smirnoff 	return (error);
1550a9771948SGleb Smirnoff }
1551a9771948SGleb Smirnoff 
1552a9771948SGleb Smirnoff #ifdef INET6
15535c1f0f6dSGleb Smirnoff static int
1554a9771948SGleb Smirnoff carp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1555a9771948SGleb Smirnoff {
1556a9771948SGleb Smirnoff 	struct ifnet *ifp;
1557a9771948SGleb Smirnoff 	struct carp_if *cif;
1558a9771948SGleb Smirnoff 	struct in6_ifaddr *ia, *ia_if;
1559a9771948SGleb Smirnoff 	struct ip6_moptions *im6o = &sc->sc_im6o;
1560a9771948SGleb Smirnoff 	struct in6_multi_mship *imm;
1561a1f7e5f8SHajimu UMEMOTO 	struct in6_addr in6;
1562a9771948SGleb Smirnoff 	int own, error;
1563a9771948SGleb Smirnoff 
1564a9771948SGleb Smirnoff 	if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1565fc74a9f9SBrooks Davis 		if (!(SC2IFP(sc)->if_flags & IFF_UP))
1566a9771948SGleb Smirnoff 			carp_set_state(sc, INIT);
1567a9771948SGleb Smirnoff 		if (sc->sc_naddrs6)
1568fc74a9f9SBrooks Davis 			SC2IFP(sc)->if_flags |= IFF_UP;
1569a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
1570a9771948SGleb Smirnoff 		return (0);
1571a9771948SGleb Smirnoff 	}
1572a9771948SGleb Smirnoff 
1573a9771948SGleb Smirnoff 	/* we have to do it by hands to check we won't match on us */
1574a9771948SGleb Smirnoff 	ia_if = NULL; own = 0;
1575a9771948SGleb Smirnoff 	for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1576a9771948SGleb Smirnoff 		int i;
1577a9771948SGleb Smirnoff 
1578a9771948SGleb Smirnoff 		for (i = 0; i < 4; i++) {
1579a9771948SGleb Smirnoff 			if ((sin6->sin6_addr.s6_addr32[i] &
1580a9771948SGleb Smirnoff 			    ia->ia_prefixmask.sin6_addr.s6_addr32[i]) !=
1581a9771948SGleb Smirnoff 			    (ia->ia_addr.sin6_addr.s6_addr32[i] &
1582a9771948SGleb Smirnoff 			    ia->ia_prefixmask.sin6_addr.s6_addr32[i]))
1583a9771948SGleb Smirnoff 				break;
1584a9771948SGleb Smirnoff 		}
1585a9771948SGleb Smirnoff 		/* and, yeah, we need a multicast-capable iface too */
1586fc74a9f9SBrooks Davis 		if (ia->ia_ifp != SC2IFP(sc) &&
1587a9771948SGleb Smirnoff 		    (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1588a9771948SGleb Smirnoff 		    (i == 4)) {
1589a9771948SGleb Smirnoff 			if (!ia_if)
1590a9771948SGleb Smirnoff 				ia_if = ia;
1591a9771948SGleb Smirnoff 			if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
1592a9771948SGleb Smirnoff 			    &ia->ia_addr.sin6_addr))
1593a9771948SGleb Smirnoff 				own++;
1594a9771948SGleb Smirnoff 		}
1595a9771948SGleb Smirnoff 	}
1596a9771948SGleb Smirnoff 
1597a9771948SGleb Smirnoff 	if (!ia_if)
1598a9771948SGleb Smirnoff 		return (EADDRNOTAVAIL);
1599a9771948SGleb Smirnoff 	ia = ia_if;
1600a9771948SGleb Smirnoff 	ifp = ia->ia_ifp;
1601a9771948SGleb Smirnoff 
1602a9771948SGleb Smirnoff 	if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1603a9771948SGleb Smirnoff 	    (im6o->im6o_multicast_ifp && im6o->im6o_multicast_ifp != ifp))
1604a9771948SGleb Smirnoff 		return (EADDRNOTAVAIL);
1605a9771948SGleb Smirnoff 
1606a9771948SGleb Smirnoff 	if (!sc->sc_naddrs6) {
1607a9771948SGleb Smirnoff 		im6o->im6o_multicast_ifp = ifp;
1608a9771948SGleb Smirnoff 
1609a9771948SGleb Smirnoff 		/* join CARP multicast address */
1610a1f7e5f8SHajimu UMEMOTO 		bzero(&in6, sizeof(in6));
1611a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr16[0] = htons(0xff02);
1612a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr8[15] = 0x12;
1613a1f7e5f8SHajimu UMEMOTO 		if (in6_setscope(&in6, ifp, NULL) != 0)
1614a1f7e5f8SHajimu UMEMOTO 			goto cleanup;
16151e4b3606SMax Laier 		if ((imm = in6_joingroup(ifp, &in6, &error, 0)) == NULL)
1616a9771948SGleb Smirnoff 			goto cleanup;
1617a9771948SGleb Smirnoff 		LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1618a9771948SGleb Smirnoff 
1619a9771948SGleb Smirnoff 		/* join solicited multicast address */
1620a1f7e5f8SHajimu UMEMOTO 		bzero(&in6, sizeof(in6));
1621a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr16[0] = htons(0xff02);
1622a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr32[1] = 0;
1623a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr32[2] = htonl(1);
1624a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr32[3] = sin6->sin6_addr.s6_addr32[3];
1625a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr8[12] = 0xff;
1626a1f7e5f8SHajimu UMEMOTO 		if (in6_setscope(&in6, ifp, NULL) != 0)
1627a1f7e5f8SHajimu UMEMOTO 			goto cleanup;
16281e4b3606SMax Laier 		if ((imm = in6_joingroup(ifp, &in6, &error, 0)) == NULL)
1629a9771948SGleb Smirnoff 			goto cleanup;
1630a9771948SGleb Smirnoff 		LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1631a9771948SGleb Smirnoff 	}
1632a9771948SGleb Smirnoff 
1633a9771948SGleb Smirnoff 	if (!ifp->if_carp) {
1634a9771948SGleb Smirnoff 		MALLOC(cif, struct carp_if *, sizeof(*cif), M_CARP,
1635a9771948SGleb Smirnoff 		    M_WAITOK|M_ZERO);
1636a9771948SGleb Smirnoff 		if (!cif) {
1637a9771948SGleb Smirnoff 			error = ENOBUFS;
1638a9771948SGleb Smirnoff 			goto cleanup;
1639a9771948SGleb Smirnoff 		}
1640a9771948SGleb Smirnoff 		if ((error = ifpromisc(ifp, 1))) {
1641a9771948SGleb Smirnoff 			FREE(cif, M_CARP);
1642a9771948SGleb Smirnoff 			goto cleanup;
1643a9771948SGleb Smirnoff 		}
1644a9771948SGleb Smirnoff 
1645a9771948SGleb Smirnoff 		CARP_LOCK_INIT(cif);
1646a9771948SGleb Smirnoff 		CARP_LOCK(cif);
1647a9771948SGleb Smirnoff 		cif->vhif_ifp = ifp;
1648a9771948SGleb Smirnoff 		TAILQ_INIT(&cif->vhif_vrs);
1649a9771948SGleb Smirnoff 		ifp->if_carp = cif;
1650a9771948SGleb Smirnoff 
1651a9771948SGleb Smirnoff 	} else {
1652a9771948SGleb Smirnoff 		struct carp_softc *vr;
1653a9771948SGleb Smirnoff 
1654a9771948SGleb Smirnoff 		cif = (struct carp_if *)ifp->if_carp;
1655a9771948SGleb Smirnoff 		CARP_LOCK(cif);
1656a9771948SGleb Smirnoff 		TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1657a9771948SGleb Smirnoff 			if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1658a9771948SGleb Smirnoff 				CARP_UNLOCK(cif);
1659a9771948SGleb Smirnoff 				error = EINVAL;
1660a9771948SGleb Smirnoff 				goto cleanup;
1661a9771948SGleb Smirnoff 			}
1662a9771948SGleb Smirnoff 	}
1663a9771948SGleb Smirnoff 	sc->sc_ia6 = ia;
1664e8c34a71SGleb Smirnoff 	sc->sc_carpdev = ifp;
1665a9771948SGleb Smirnoff 
1666a9771948SGleb Smirnoff 	{ /* XXX prevent endless loop if already in queue */
1667a9771948SGleb Smirnoff 	struct carp_softc *vr, *after = NULL;
1668a9771948SGleb Smirnoff 	int myself = 0;
1669a9771948SGleb Smirnoff 	cif = (struct carp_if *)ifp->if_carp;
1670a9771948SGleb Smirnoff 	CARP_LOCK_ASSERT(cif);
1671a9771948SGleb Smirnoff 
1672a9771948SGleb Smirnoff 	TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1673a9771948SGleb Smirnoff 		if (vr == sc)
1674a9771948SGleb Smirnoff 			myself = 1;
1675a9771948SGleb Smirnoff 		if (vr->sc_vhid < sc->sc_vhid)
1676a9771948SGleb Smirnoff 			after = vr;
1677a9771948SGleb Smirnoff 	}
1678a9771948SGleb Smirnoff 
1679a9771948SGleb Smirnoff 	if (!myself) {
1680a9771948SGleb Smirnoff 		/* We're trying to keep things in order */
1681a9771948SGleb Smirnoff 		if (after == NULL) {
1682a9771948SGleb Smirnoff 			TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1683a9771948SGleb Smirnoff 		} else {
1684a9771948SGleb Smirnoff 			TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1685a9771948SGleb Smirnoff 		}
1686a9771948SGleb Smirnoff 		cif->vhif_nvrs++;
1687a9771948SGleb Smirnoff 	}
1688a9771948SGleb Smirnoff 	}
1689a9771948SGleb Smirnoff 
1690a9771948SGleb Smirnoff 	sc->sc_naddrs6++;
1691fc74a9f9SBrooks Davis 	SC2IFP(sc)->if_flags |= IFF_UP;
1692a9771948SGleb Smirnoff 	if (own)
1693a9771948SGleb Smirnoff 		sc->sc_advskew = 0;
16944cb39345SGleb Smirnoff 	carp_sc_state_locked(sc);
1695a9771948SGleb Smirnoff 	carp_setrun(sc, 0);
1696a9771948SGleb Smirnoff 
1697d220759bSGleb Smirnoff 	CARP_UNLOCK(cif);
1698d220759bSGleb Smirnoff 
1699a9771948SGleb Smirnoff 	return (0);
1700a9771948SGleb Smirnoff 
1701a9771948SGleb Smirnoff cleanup:
1702a9771948SGleb Smirnoff 	/* clean up multicast memberships */
1703a9771948SGleb Smirnoff 	if (!sc->sc_naddrs6) {
1704a9771948SGleb Smirnoff 		while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1705a9771948SGleb Smirnoff 			imm = LIST_FIRST(&im6o->im6o_memberships);
1706a9771948SGleb Smirnoff 			LIST_REMOVE(imm, i6mm_chain);
1707a9771948SGleb Smirnoff 			in6_leavegroup(imm);
1708a9771948SGleb Smirnoff 		}
1709a9771948SGleb Smirnoff 	}
1710a9771948SGleb Smirnoff 	return (error);
1711a9771948SGleb Smirnoff }
1712a9771948SGleb Smirnoff 
17135c1f0f6dSGleb Smirnoff static int
1714a9771948SGleb Smirnoff carp_del_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1715a9771948SGleb Smirnoff {
1716a9771948SGleb Smirnoff 	int error = 0;
1717a9771948SGleb Smirnoff 
1718a9771948SGleb Smirnoff 	if (!--sc->sc_naddrs6) {
1719e8c34a71SGleb Smirnoff 		struct carp_if *cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1720a9771948SGleb Smirnoff 		struct ip6_moptions *im6o = &sc->sc_im6o;
1721a9771948SGleb Smirnoff 
1722d220759bSGleb Smirnoff 		CARP_LOCK(cif);
1723a9771948SGleb Smirnoff 		callout_stop(&sc->sc_ad_tmo);
172413f4c340SRobert Watson 		SC2IFP(sc)->if_flags &= ~IFF_UP;
172513f4c340SRobert Watson 		SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1726a9771948SGleb Smirnoff 		sc->sc_vhid = -1;
1727a9771948SGleb Smirnoff 		while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1728a9771948SGleb Smirnoff 			struct in6_multi_mship *imm =
1729a9771948SGleb Smirnoff 			    LIST_FIRST(&im6o->im6o_memberships);
1730a9771948SGleb Smirnoff 
1731a9771948SGleb Smirnoff 			LIST_REMOVE(imm, i6mm_chain);
1732a9771948SGleb Smirnoff 			in6_leavegroup(imm);
1733a9771948SGleb Smirnoff 		}
1734a9771948SGleb Smirnoff 		im6o->im6o_multicast_ifp = NULL;
1735a9771948SGleb Smirnoff 		TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1736a9771948SGleb Smirnoff 		if (!--cif->vhif_nvrs) {
1737a9771948SGleb Smirnoff 			CARP_LOCK_DESTROY(cif);
1738e8c34a71SGleb Smirnoff 			sc->sc_carpdev->if_carp = NULL;
1739a9771948SGleb Smirnoff 			FREE(cif, M_IFADDR);
1740a9771948SGleb Smirnoff 		} else
1741a9771948SGleb Smirnoff 			CARP_UNLOCK(cif);
1742a9771948SGleb Smirnoff 	}
1743a9771948SGleb Smirnoff 
1744a9771948SGleb Smirnoff 	return (error);
1745a9771948SGleb Smirnoff }
1746a9771948SGleb Smirnoff #endif /* INET6 */
1747a9771948SGleb Smirnoff 
17485c1f0f6dSGleb Smirnoff static int
1749a9771948SGleb Smirnoff carp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr)
1750a9771948SGleb Smirnoff {
1751a9771948SGleb Smirnoff 	struct carp_softc *sc = ifp->if_softc, *vr;
1752a9771948SGleb Smirnoff 	struct carpreq carpr;
1753a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1754a9771948SGleb Smirnoff 	struct ifreq *ifr;
1755a9771948SGleb Smirnoff 	struct ifaliasreq *ifra;
1756d220759bSGleb Smirnoff 	int locked = 0, error = 0;
1757a9771948SGleb Smirnoff 
1758a9771948SGleb Smirnoff 	ifa = (struct ifaddr *)addr;
1759a9771948SGleb Smirnoff 	ifra = (struct ifaliasreq *)addr;
1760a9771948SGleb Smirnoff 	ifr = (struct ifreq *)addr;
1761a9771948SGleb Smirnoff 
1762a9771948SGleb Smirnoff 	switch (cmd) {
1763a9771948SGleb Smirnoff 	case SIOCSIFADDR:
1764a9771948SGleb Smirnoff 		switch (ifa->ifa_addr->sa_family) {
1765a9771948SGleb Smirnoff #ifdef INET
1766a9771948SGleb Smirnoff 		case AF_INET:
1767fc74a9f9SBrooks Davis 			SC2IFP(sc)->if_flags |= IFF_UP;
1768a9771948SGleb Smirnoff 			bcopy(ifa->ifa_addr, ifa->ifa_dstaddr,
1769a9771948SGleb Smirnoff 			    sizeof(struct sockaddr));
1770a9771948SGleb Smirnoff 			error = carp_set_addr(sc, satosin(ifa->ifa_addr));
1771a9771948SGleb Smirnoff 			break;
1772a9771948SGleb Smirnoff #endif /* INET */
1773a9771948SGleb Smirnoff #ifdef INET6
1774a9771948SGleb Smirnoff 		case AF_INET6:
1775fc74a9f9SBrooks Davis 			SC2IFP(sc)->if_flags |= IFF_UP;
1776a9771948SGleb Smirnoff 			error = carp_set_addr6(sc, satosin6(ifa->ifa_addr));
1777a9771948SGleb Smirnoff 			break;
1778a9771948SGleb Smirnoff #endif /* INET6 */
1779a9771948SGleb Smirnoff 		default:
1780a9771948SGleb Smirnoff 			error = EAFNOSUPPORT;
1781a9771948SGleb Smirnoff 			break;
1782a9771948SGleb Smirnoff 		}
1783a9771948SGleb Smirnoff 		break;
1784a9771948SGleb Smirnoff 
1785a9771948SGleb Smirnoff 	case SIOCAIFADDR:
1786a9771948SGleb Smirnoff 		switch (ifa->ifa_addr->sa_family) {
1787a9771948SGleb Smirnoff #ifdef INET
1788a9771948SGleb Smirnoff 		case AF_INET:
1789fc74a9f9SBrooks Davis 			SC2IFP(sc)->if_flags |= IFF_UP;
1790a9771948SGleb Smirnoff 			bcopy(ifa->ifa_addr, ifa->ifa_dstaddr,
1791a9771948SGleb Smirnoff 			    sizeof(struct sockaddr));
1792a9771948SGleb Smirnoff 			error = carp_set_addr(sc, satosin(&ifra->ifra_addr));
1793a9771948SGleb Smirnoff 			break;
1794a9771948SGleb Smirnoff #endif /* INET */
1795a9771948SGleb Smirnoff #ifdef INET6
1796a9771948SGleb Smirnoff 		case AF_INET6:
1797fc74a9f9SBrooks Davis 			SC2IFP(sc)->if_flags |= IFF_UP;
1798a9771948SGleb Smirnoff 			error = carp_set_addr6(sc, satosin6(&ifra->ifra_addr));
1799a9771948SGleb Smirnoff 			break;
1800a9771948SGleb Smirnoff #endif /* INET6 */
1801a9771948SGleb Smirnoff 		default:
1802a9771948SGleb Smirnoff 			error = EAFNOSUPPORT;
1803a9771948SGleb Smirnoff 			break;
1804a9771948SGleb Smirnoff 		}
1805a9771948SGleb Smirnoff 		break;
1806a9771948SGleb Smirnoff 
1807a9771948SGleb Smirnoff 	case SIOCDIFADDR:
1808a9771948SGleb Smirnoff 		switch (ifa->ifa_addr->sa_family) {
1809a9771948SGleb Smirnoff #ifdef INET
1810a9771948SGleb Smirnoff 		case AF_INET:
1811a9771948SGleb Smirnoff 			error = carp_del_addr(sc, satosin(&ifra->ifra_addr));
1812a9771948SGleb Smirnoff 			break;
1813a9771948SGleb Smirnoff #endif /* INET */
1814a9771948SGleb Smirnoff #ifdef INET6
1815a9771948SGleb Smirnoff 		case AF_INET6:
1816a9771948SGleb Smirnoff 			error = carp_del_addr6(sc, satosin6(&ifra->ifra_addr));
1817a9771948SGleb Smirnoff 			break;
1818a9771948SGleb Smirnoff #endif /* INET6 */
1819a9771948SGleb Smirnoff 		default:
1820a9771948SGleb Smirnoff 			error = EAFNOSUPPORT;
1821a9771948SGleb Smirnoff 			break;
1822a9771948SGleb Smirnoff 		}
1823a9771948SGleb Smirnoff 		break;
1824a9771948SGleb Smirnoff 
1825a9771948SGleb Smirnoff 	case SIOCSIFFLAGS:
1826d220759bSGleb Smirnoff 		if (sc->sc_carpdev) {
1827d220759bSGleb Smirnoff 			locked = 1;
1828d220759bSGleb Smirnoff 			CARP_SCLOCK(sc);
1829d220759bSGleb Smirnoff 		}
1830a9771948SGleb Smirnoff 		if (sc->sc_state != INIT && !(ifr->ifr_flags & IFF_UP)) {
1831a9771948SGleb Smirnoff  			callout_stop(&sc->sc_ad_tmo);
1832a9771948SGleb Smirnoff  			callout_stop(&sc->sc_md_tmo);
1833a9771948SGleb Smirnoff  			callout_stop(&sc->sc_md6_tmo);
1834a9771948SGleb Smirnoff 			if (sc->sc_state == MASTER)
1835d220759bSGleb Smirnoff 				carp_send_ad_locked(sc);
1836a9771948SGleb Smirnoff 			carp_set_state(sc, INIT);
1837a9771948SGleb Smirnoff 			carp_setrun(sc, 0);
1838a9771948SGleb Smirnoff 		} else if (sc->sc_state == INIT && (ifr->ifr_flags & IFF_UP)) {
1839fc74a9f9SBrooks Davis 			SC2IFP(sc)->if_flags |= IFF_UP;
1840a9771948SGleb Smirnoff 			carp_setrun(sc, 0);
1841a9771948SGleb Smirnoff 		}
1842a9771948SGleb Smirnoff 		break;
1843a9771948SGleb Smirnoff 
1844a9771948SGleb Smirnoff 	case SIOCSVH:
1845a9771948SGleb Smirnoff 		if ((error = suser(curthread)) != 0)
1846a9771948SGleb Smirnoff 			break;
1847a9771948SGleb Smirnoff 		if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
1848a9771948SGleb Smirnoff 			break;
1849a9771948SGleb Smirnoff 		error = 1;
1850d220759bSGleb Smirnoff 		if (sc->sc_carpdev) {
1851d220759bSGleb Smirnoff 			locked = 1;
1852d220759bSGleb Smirnoff 			CARP_SCLOCK(sc);
1853d220759bSGleb Smirnoff 		}
1854a9771948SGleb Smirnoff 		if (sc->sc_state != INIT && carpr.carpr_state != sc->sc_state) {
1855a9771948SGleb Smirnoff 			switch (carpr.carpr_state) {
1856a9771948SGleb Smirnoff 			case BACKUP:
1857a9771948SGleb Smirnoff 				callout_stop(&sc->sc_ad_tmo);
1858a9771948SGleb Smirnoff 				carp_set_state(sc, BACKUP);
1859a9771948SGleb Smirnoff 				carp_setrun(sc, 0);
1860a9771948SGleb Smirnoff 				carp_setroute(sc, RTM_DELETE);
1861a9771948SGleb Smirnoff 				break;
1862a9771948SGleb Smirnoff 			case MASTER:
1863d220759bSGleb Smirnoff 				carp_master_down_locked(sc);
1864a9771948SGleb Smirnoff 				break;
1865a9771948SGleb Smirnoff 			default:
1866a9771948SGleb Smirnoff 				break;
1867a9771948SGleb Smirnoff 			}
1868a9771948SGleb Smirnoff 		}
1869a9771948SGleb Smirnoff 		if (carpr.carpr_vhid > 0) {
1870a9771948SGleb Smirnoff 			if (carpr.carpr_vhid > 255) {
1871a9771948SGleb Smirnoff 				error = EINVAL;
1872a9771948SGleb Smirnoff 				break;
1873a9771948SGleb Smirnoff 			}
1874e8c34a71SGleb Smirnoff 			if (sc->sc_carpdev) {
1875a9771948SGleb Smirnoff 				struct carp_if *cif;
1876e8c34a71SGleb Smirnoff 				cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1877a9771948SGleb Smirnoff 				TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1878a9771948SGleb Smirnoff 					if (vr != sc &&
1879ee6f2270SGleb Smirnoff 					    vr->sc_vhid == carpr.carpr_vhid)
1880ee6f2270SGleb Smirnoff 						return EEXIST;
1881a9771948SGleb Smirnoff 			}
1882a9771948SGleb Smirnoff 			sc->sc_vhid = carpr.carpr_vhid;
18834a0d6638SRuslan Ermilov 			IF_LLADDR(sc->sc_ifp)[0] = 0;
18844a0d6638SRuslan Ermilov 			IF_LLADDR(sc->sc_ifp)[1] = 0;
18854a0d6638SRuslan Ermilov 			IF_LLADDR(sc->sc_ifp)[2] = 0x5e;
18864a0d6638SRuslan Ermilov 			IF_LLADDR(sc->sc_ifp)[3] = 0;
18874a0d6638SRuslan Ermilov 			IF_LLADDR(sc->sc_ifp)[4] = 1;
18884a0d6638SRuslan Ermilov 			IF_LLADDR(sc->sc_ifp)[5] = sc->sc_vhid;
1889a9771948SGleb Smirnoff 			error--;
1890a9771948SGleb Smirnoff 		}
1891a9771948SGleb Smirnoff 		if (carpr.carpr_advbase > 0 || carpr.carpr_advskew > 0) {
1892a9771948SGleb Smirnoff 			if (carpr.carpr_advskew >= 255) {
1893a9771948SGleb Smirnoff 				error = EINVAL;
1894a9771948SGleb Smirnoff 				break;
1895a9771948SGleb Smirnoff 			}
1896a9771948SGleb Smirnoff 			if (carpr.carpr_advbase > 255) {
1897a9771948SGleb Smirnoff 				error = EINVAL;
1898a9771948SGleb Smirnoff 				break;
1899a9771948SGleb Smirnoff 			}
1900a9771948SGleb Smirnoff 			sc->sc_advbase = carpr.carpr_advbase;
1901a9771948SGleb Smirnoff 			sc->sc_advskew = carpr.carpr_advskew;
1902a9771948SGleb Smirnoff 			error--;
1903a9771948SGleb Smirnoff 		}
1904a9771948SGleb Smirnoff 		bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1905a9771948SGleb Smirnoff 		if (error > 0)
1906a9771948SGleb Smirnoff 			error = EINVAL;
1907a9771948SGleb Smirnoff 		else {
1908a9771948SGleb Smirnoff 			error = 0;
1909a9771948SGleb Smirnoff 			carp_setrun(sc, 0);
1910a9771948SGleb Smirnoff 		}
1911a9771948SGleb Smirnoff 		break;
1912a9771948SGleb Smirnoff 
1913a9771948SGleb Smirnoff 	case SIOCGVH:
1914d220759bSGleb Smirnoff 		/* XXX: lockless read */
1915a9771948SGleb Smirnoff 		bzero(&carpr, sizeof(carpr));
1916a9771948SGleb Smirnoff 		carpr.carpr_state = sc->sc_state;
1917a9771948SGleb Smirnoff 		carpr.carpr_vhid = sc->sc_vhid;
1918a9771948SGleb Smirnoff 		carpr.carpr_advbase = sc->sc_advbase;
1919a9771948SGleb Smirnoff 		carpr.carpr_advskew = sc->sc_advskew;
1920a9771948SGleb Smirnoff 		if (suser(curthread) == 0)
1921a9771948SGleb Smirnoff 			bcopy(sc->sc_key, carpr.carpr_key,
1922a9771948SGleb Smirnoff 			    sizeof(carpr.carpr_key));
1923a9771948SGleb Smirnoff 		error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
1924a9771948SGleb Smirnoff 		break;
1925a9771948SGleb Smirnoff 
1926a9771948SGleb Smirnoff 	default:
1927a9771948SGleb Smirnoff 		error = EINVAL;
1928a9771948SGleb Smirnoff 	}
1929a9771948SGleb Smirnoff 
1930d220759bSGleb Smirnoff 	if (locked)
1931d220759bSGleb Smirnoff 		CARP_SCUNLOCK(sc);
1932d220759bSGleb Smirnoff 
1933a9771948SGleb Smirnoff 	carp_hmac_prepare(sc);
1934d220759bSGleb Smirnoff 
1935a9771948SGleb Smirnoff 	return (error);
1936a9771948SGleb Smirnoff }
1937a9771948SGleb Smirnoff 
1938a9771948SGleb Smirnoff /*
1939a9771948SGleb Smirnoff  * XXX: this is looutput. We should eventually use it from there.
1940a9771948SGleb Smirnoff  */
1941a9771948SGleb Smirnoff static int
1942a9771948SGleb Smirnoff carp_looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
1943a9771948SGleb Smirnoff     struct rtentry *rt)
1944a9771948SGleb Smirnoff {
194501399f34SDavid Malone 	u_int32_t af;
194601399f34SDavid Malone 
1947a9771948SGleb Smirnoff 	M_ASSERTPKTHDR(m); /* check if we have the packet header */
1948a9771948SGleb Smirnoff 
1949a9771948SGleb Smirnoff 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
1950a9771948SGleb Smirnoff 		m_freem(m);
1951a9771948SGleb Smirnoff 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
1952a9771948SGleb Smirnoff 			rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1953a9771948SGleb Smirnoff 	}
1954a9771948SGleb Smirnoff 
1955a9771948SGleb Smirnoff 	ifp->if_opackets++;
1956a9771948SGleb Smirnoff 	ifp->if_obytes += m->m_pkthdr.len;
195701399f34SDavid Malone 
195801399f34SDavid Malone 	/* BPF writes need to be handled specially. */
195901399f34SDavid Malone 	if (dst->sa_family == AF_UNSPEC) {
196001399f34SDavid Malone 		bcopy(dst->sa_data, &af, sizeof(af));
196101399f34SDavid Malone 		dst->sa_family = af;
196201399f34SDavid Malone 	}
196301399f34SDavid Malone 
1964a9771948SGleb Smirnoff #if 1	/* XXX */
1965a9771948SGleb Smirnoff 	switch (dst->sa_family) {
1966a9771948SGleb Smirnoff 	case AF_INET:
1967a9771948SGleb Smirnoff 	case AF_INET6:
1968a9771948SGleb Smirnoff 	case AF_IPX:
1969a9771948SGleb Smirnoff 	case AF_APPLETALK:
1970a9771948SGleb Smirnoff 		break;
1971a9771948SGleb Smirnoff 	default:
1972a9771948SGleb Smirnoff 		printf("carp_looutput: af=%d unexpected\n", dst->sa_family);
1973a9771948SGleb Smirnoff 		m_freem(m);
1974a9771948SGleb Smirnoff 		return (EAFNOSUPPORT);
1975a9771948SGleb Smirnoff 	}
1976a9771948SGleb Smirnoff #endif
1977a9771948SGleb Smirnoff 	return(if_simloop(ifp, m, dst->sa_family, 0));
1978a9771948SGleb Smirnoff }
1979a9771948SGleb Smirnoff 
1980a9771948SGleb Smirnoff /*
1981a9771948SGleb Smirnoff  * Start output on carp interface. This function should never be called.
1982a9771948SGleb Smirnoff  */
19835c1f0f6dSGleb Smirnoff static void
1984a9771948SGleb Smirnoff carp_start(struct ifnet *ifp)
1985a9771948SGleb Smirnoff {
1986a9771948SGleb Smirnoff #ifdef DEBUG
1987a9771948SGleb Smirnoff 	printf("%s: start called\n", ifp->if_xname);
1988a9771948SGleb Smirnoff #endif
1989a9771948SGleb Smirnoff }
1990a9771948SGleb Smirnoff 
1991a9771948SGleb Smirnoff int
1992a9771948SGleb Smirnoff carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
1993a9771948SGleb Smirnoff     struct rtentry *rt)
1994a9771948SGleb Smirnoff {
1995a9771948SGleb Smirnoff 	struct m_tag *mtag;
1996a9771948SGleb Smirnoff 	struct carp_softc *sc;
1997a9771948SGleb Smirnoff 	struct ifnet *carp_ifp;
1998a9771948SGleb Smirnoff 
1999a9771948SGleb Smirnoff 	if (!sa)
2000a9771948SGleb Smirnoff 		return (0);
2001a9771948SGleb Smirnoff 
2002a9771948SGleb Smirnoff 	switch (sa->sa_family) {
2003a9771948SGleb Smirnoff #ifdef INET
2004a9771948SGleb Smirnoff 	case AF_INET:
2005a9771948SGleb Smirnoff 		break;
2006a9771948SGleb Smirnoff #endif /* INET */
2007a9771948SGleb Smirnoff #ifdef INET6
2008a9771948SGleb Smirnoff 	case AF_INET6:
2009a9771948SGleb Smirnoff 		break;
2010a9771948SGleb Smirnoff #endif /* INET6 */
2011a9771948SGleb Smirnoff 	default:
2012a9771948SGleb Smirnoff 		return (0);
2013a9771948SGleb Smirnoff 	}
2014a9771948SGleb Smirnoff 
2015a9771948SGleb Smirnoff 	mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
2016a9771948SGleb Smirnoff 	if (mtag == NULL)
2017a9771948SGleb Smirnoff 		return (0);
2018a9771948SGleb Smirnoff 
2019a9771948SGleb Smirnoff 	bcopy(mtag + 1, &carp_ifp, sizeof(struct ifnet *));
2020a9771948SGleb Smirnoff 	sc = carp_ifp->if_softc;
2021a9771948SGleb Smirnoff 
2022a9771948SGleb Smirnoff 	/* Set the source MAC address to Virtual Router MAC Address */
2023a9771948SGleb Smirnoff 	switch (ifp->if_type) {
2024630481bbSYaroslav Tykhiy 	case IFT_ETHER:
2025630481bbSYaroslav Tykhiy 	case IFT_L2VLAN: {
2026a9771948SGleb Smirnoff 			struct ether_header *eh;
2027a9771948SGleb Smirnoff 
2028a9771948SGleb Smirnoff 			eh = mtod(m, struct ether_header *);
2029a9771948SGleb Smirnoff 			eh->ether_shost[0] = 0;
2030a9771948SGleb Smirnoff 			eh->ether_shost[1] = 0;
2031a9771948SGleb Smirnoff 			eh->ether_shost[2] = 0x5e;
2032a9771948SGleb Smirnoff 			eh->ether_shost[3] = 0;
2033a9771948SGleb Smirnoff 			eh->ether_shost[4] = 1;
2034a9771948SGleb Smirnoff 			eh->ether_shost[5] = sc->sc_vhid;
2035a9771948SGleb Smirnoff 		}
2036a9771948SGleb Smirnoff 		break;
2037a9771948SGleb Smirnoff 	case IFT_FDDI: {
2038a9771948SGleb Smirnoff 			struct fddi_header *fh;
2039a9771948SGleb Smirnoff 
2040a9771948SGleb Smirnoff 			fh = mtod(m, struct fddi_header *);
2041a9771948SGleb Smirnoff 			fh->fddi_shost[0] = 0;
2042a9771948SGleb Smirnoff 			fh->fddi_shost[1] = 0;
2043a9771948SGleb Smirnoff 			fh->fddi_shost[2] = 0x5e;
2044a9771948SGleb Smirnoff 			fh->fddi_shost[3] = 0;
2045a9771948SGleb Smirnoff 			fh->fddi_shost[4] = 1;
2046a9771948SGleb Smirnoff 			fh->fddi_shost[5] = sc->sc_vhid;
2047a9771948SGleb Smirnoff 		}
2048a9771948SGleb Smirnoff 		break;
2049a9771948SGleb Smirnoff 	case IFT_ISO88025: {
2050a9771948SGleb Smirnoff  			struct iso88025_header *th;
2051a9771948SGleb Smirnoff  			th = mtod(m, struct iso88025_header *);
2052a9771948SGleb Smirnoff 			th->iso88025_shost[0] = 3;
2053a9771948SGleb Smirnoff 			th->iso88025_shost[1] = 0;
2054a9771948SGleb Smirnoff 			th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1);
2055a9771948SGleb Smirnoff 			th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1);
2056a9771948SGleb Smirnoff 			th->iso88025_shost[4] = 0;
2057a9771948SGleb Smirnoff 			th->iso88025_shost[5] = 0;
2058a9771948SGleb Smirnoff 		}
2059a9771948SGleb Smirnoff 		break;
2060a9771948SGleb Smirnoff 	default:
2061a9771948SGleb Smirnoff 		printf("%s: carp is not supported for this interface type\n",
2062a9771948SGleb Smirnoff 		    ifp->if_xname);
2063a9771948SGleb Smirnoff 		return (EOPNOTSUPP);
2064a9771948SGleb Smirnoff 	}
2065a9771948SGleb Smirnoff 
2066a9771948SGleb Smirnoff 	return (0);
2067a9771948SGleb Smirnoff }
2068a9771948SGleb Smirnoff 
20695c1f0f6dSGleb Smirnoff static void
2070a9771948SGleb Smirnoff carp_set_state(struct carp_softc *sc, int state)
2071a9771948SGleb Smirnoff {
2072d220759bSGleb Smirnoff 
2073d220759bSGleb Smirnoff 	if (sc->sc_carpdev)
2074d220759bSGleb Smirnoff 		CARP_SCLOCK_ASSERT(sc);
2075d220759bSGleb Smirnoff 
2076a9771948SGleb Smirnoff 	if (sc->sc_state == state)
2077a9771948SGleb Smirnoff 		return;
2078a9771948SGleb Smirnoff 
2079a9771948SGleb Smirnoff 	sc->sc_state = state;
2080a9771948SGleb Smirnoff 	switch (state) {
2081a9771948SGleb Smirnoff 	case BACKUP:
2082fc74a9f9SBrooks Davis 		SC2IFP(sc)->if_link_state = LINK_STATE_DOWN;
2083a9771948SGleb Smirnoff 		break;
2084a9771948SGleb Smirnoff 	case MASTER:
2085fc74a9f9SBrooks Davis 		SC2IFP(sc)->if_link_state = LINK_STATE_UP;
2086a9771948SGleb Smirnoff 		break;
2087a9771948SGleb Smirnoff 	default:
2088fc74a9f9SBrooks Davis 		SC2IFP(sc)->if_link_state = LINK_STATE_UNKNOWN;
2089a9771948SGleb Smirnoff 		break;
2090a9771948SGleb Smirnoff 	}
2091fc74a9f9SBrooks Davis 	rt_ifmsg(SC2IFP(sc));
2092a9771948SGleb Smirnoff }
2093a9771948SGleb Smirnoff 
2094a9771948SGleb Smirnoff void
2095a9771948SGleb Smirnoff carp_carpdev_state(void *v)
2096a9771948SGleb Smirnoff {
2097a9771948SGleb Smirnoff 	struct carp_if *cif = v;
2098d220759bSGleb Smirnoff 
2099a9771948SGleb Smirnoff 	CARP_LOCK(cif);
2100d220759bSGleb Smirnoff 	carp_carpdev_state_locked(cif);
2101d220759bSGleb Smirnoff 	CARP_UNLOCK(cif);
2102d220759bSGleb Smirnoff }
2103d220759bSGleb Smirnoff 
2104d220759bSGleb Smirnoff static void
2105d220759bSGleb Smirnoff carp_carpdev_state_locked(struct carp_if *cif)
2106d220759bSGleb Smirnoff {
2107d220759bSGleb Smirnoff 	struct carp_softc *sc;
2108d220759bSGleb Smirnoff 
21094cb39345SGleb Smirnoff 	TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list)
21104cb39345SGleb Smirnoff 		carp_sc_state_locked(sc);
21114cb39345SGleb Smirnoff }
21124cb39345SGleb Smirnoff 
21134cb39345SGleb Smirnoff static void
21144cb39345SGleb Smirnoff carp_sc_state_locked(struct carp_softc *sc)
21154cb39345SGleb Smirnoff {
21164cb39345SGleb Smirnoff 	CARP_SCLOCK_ASSERT(sc);
21174cb39345SGleb Smirnoff 
2118e8c34a71SGleb Smirnoff 	if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
2119e8c34a71SGleb Smirnoff 	    !(sc->sc_carpdev->if_flags & IFF_UP)) {
2120fc74a9f9SBrooks Davis 		sc->sc_flags_backup = SC2IFP(sc)->if_flags;
212113f4c340SRobert Watson 		SC2IFP(sc)->if_flags &= ~IFF_UP;
212213f4c340SRobert Watson 		SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
2123a9771948SGleb Smirnoff 		callout_stop(&sc->sc_ad_tmo);
2124a9771948SGleb Smirnoff 		callout_stop(&sc->sc_md_tmo);
2125a9771948SGleb Smirnoff 		callout_stop(&sc->sc_md6_tmo);
2126a9771948SGleb Smirnoff 		carp_set_state(sc, INIT);
2127a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
2128a9771948SGleb Smirnoff 		if (!sc->sc_suppress) {
2129a9771948SGleb Smirnoff 			carp_suppress_preempt++;
2130d220759bSGleb Smirnoff 			if (carp_suppress_preempt == 1) {
2131d220759bSGleb Smirnoff 				CARP_SCUNLOCK(sc);
2132a9771948SGleb Smirnoff 				carp_send_ad_all();
2133d220759bSGleb Smirnoff 				CARP_SCLOCK(sc);
2134d220759bSGleb Smirnoff 			}
2135a9771948SGleb Smirnoff 		}
2136a9771948SGleb Smirnoff 		sc->sc_suppress = 1;
2137a9771948SGleb Smirnoff 	} else {
2138fc74a9f9SBrooks Davis 		SC2IFP(sc)->if_flags |= sc->sc_flags_backup;
2139a9771948SGleb Smirnoff 		carp_set_state(sc, INIT);
2140a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
2141a9771948SGleb Smirnoff 		if (sc->sc_suppress)
2142a9771948SGleb Smirnoff 			carp_suppress_preempt--;
2143a9771948SGleb Smirnoff 		sc->sc_suppress = 0;
2144a9771948SGleb Smirnoff 	}
21454cb39345SGleb Smirnoff 
21464cb39345SGleb Smirnoff 	return;
2147a9771948SGleb Smirnoff }
2148a9771948SGleb Smirnoff 
2149a9771948SGleb Smirnoff static int
2150a9771948SGleb Smirnoff carp_modevent(module_t mod, int type, void *data)
2151a9771948SGleb Smirnoff {
2152a9771948SGleb Smirnoff 	switch (type) {
2153a9771948SGleb Smirnoff 	case MOD_LOAD:
21540fa08018SGleb Smirnoff 		if_detach_event_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
21550fa08018SGleb Smirnoff 		    carp_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
21560fa08018SGleb Smirnoff 		if (if_detach_event_tag == NULL)
21570fa08018SGleb Smirnoff 			return (ENOMEM);
2158d92d54d5SGleb Smirnoff 		mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
2159a9771948SGleb Smirnoff 		LIST_INIT(&carpif_list);
2160a9771948SGleb Smirnoff 		if_clone_attach(&carp_cloner);
2161a9771948SGleb Smirnoff 		break;
2162a9771948SGleb Smirnoff 
2163a9771948SGleb Smirnoff 	case MOD_UNLOAD:
21640fa08018SGleb Smirnoff 		EVENTHANDLER_DEREGISTER(ifnet_departure_event, if_detach_event_tag);
2165a9771948SGleb Smirnoff 		if_clone_detach(&carp_cloner);
2166d92d54d5SGleb Smirnoff 		mtx_destroy(&carp_mtx);
2167a9771948SGleb Smirnoff 		break;
2168a9771948SGleb Smirnoff 
2169a9771948SGleb Smirnoff 	default:
21700fa08018SGleb Smirnoff 		return (EINVAL);
2171a9771948SGleb Smirnoff 	}
2172a9771948SGleb Smirnoff 
21730fa08018SGleb Smirnoff 	return (0);
2174a9771948SGleb Smirnoff }
2175a9771948SGleb Smirnoff 
2176a9771948SGleb Smirnoff static moduledata_t carp_mod = {
2177a9771948SGleb Smirnoff 	"carp",
2178a9771948SGleb Smirnoff 	carp_modevent,
2179a9771948SGleb Smirnoff 	0
2180a9771948SGleb Smirnoff };
2181a9771948SGleb Smirnoff 
2182a9771948SGleb Smirnoff DECLARE_MODULE(carp, carp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
2183