xref: /freebsd/sys/netinet/ip_carp.c (revision 6baf7a243afee6db28e86c7c229ddad9ffe755f6)
1a9771948SGleb Smirnoff /*
2a9771948SGleb Smirnoff  * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
3a9771948SGleb Smirnoff  * Copyright (c) 2003 Ryan McBride. All rights reserved.
4a9771948SGleb Smirnoff  *
5a9771948SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
6a9771948SGleb Smirnoff  * modification, are permitted provided that the following conditions
7a9771948SGleb Smirnoff  * are met:
8a9771948SGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
9a9771948SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
10a9771948SGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
11a9771948SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
12a9771948SGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
13a9771948SGleb Smirnoff  *
14a9771948SGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15a9771948SGleb Smirnoff  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16a9771948SGleb Smirnoff  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17a9771948SGleb Smirnoff  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
18a9771948SGleb Smirnoff  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19a9771948SGleb Smirnoff  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20a9771948SGleb Smirnoff  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21a9771948SGleb Smirnoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22a9771948SGleb Smirnoff  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23a9771948SGleb Smirnoff  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24a9771948SGleb Smirnoff  * THE POSSIBILITY OF SUCH DAMAGE.
25a9771948SGleb Smirnoff  */
26a9771948SGleb Smirnoff 
274b421e2dSMike Silbersack #include <sys/cdefs.h>
284b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
294b421e2dSMike Silbersack 
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>
44acd3428bSRobert Watson #include <sys/priv.h>
45a9771948SGleb Smirnoff #include <sys/proc.h>
4654bfbd51SWill Andrews #include <sys/protosw.h>
47a9771948SGleb Smirnoff #include <sys/sysctl.h>
48a9771948SGleb Smirnoff #include <sys/syslog.h>
49a9771948SGleb Smirnoff #include <sys/signalvar.h>
50a9771948SGleb Smirnoff #include <sys/filio.h>
51a9771948SGleb Smirnoff #include <sys/sockio.h>
52a9771948SGleb Smirnoff 
53a9771948SGleb Smirnoff #include <sys/socket.h>
54a9771948SGleb Smirnoff #include <sys/vnode.h>
55a9771948SGleb Smirnoff 
56a9771948SGleb Smirnoff #include <machine/stdarg.h>
57a9771948SGleb Smirnoff 
58a9771948SGleb Smirnoff #include <net/bpf.h>
59a9771948SGleb Smirnoff #include <net/ethernet.h>
60a9771948SGleb Smirnoff #include <net/fddi.h>
61a9771948SGleb Smirnoff #include <net/iso88025.h>
62a9771948SGleb Smirnoff #include <net/if.h>
63a9771948SGleb Smirnoff #include <net/if_clone.h>
64433aaf04SRuslan Ermilov #include <net/if_dl.h>
65a9771948SGleb Smirnoff #include <net/if_types.h>
66a9771948SGleb Smirnoff #include <net/route.h>
67530c0060SRobert Watson #include <net/vnet.h>
68a9771948SGleb Smirnoff 
69a9771948SGleb Smirnoff #ifdef INET
70a9771948SGleb Smirnoff #include <netinet/in.h>
71a9771948SGleb Smirnoff #include <netinet/in_var.h>
72a9771948SGleb Smirnoff #include <netinet/in_systm.h>
73a9771948SGleb Smirnoff #include <netinet/ip.h>
74a9771948SGleb Smirnoff #include <netinet/ip_var.h>
75a9771948SGleb Smirnoff #include <netinet/if_ether.h>
76a9771948SGleb Smirnoff #include <machine/in_cksum.h>
77a9771948SGleb Smirnoff #endif
78a9771948SGleb Smirnoff 
79a9771948SGleb Smirnoff #ifdef INET6
80a9771948SGleb Smirnoff #include <netinet/icmp6.h>
81a9771948SGleb Smirnoff #include <netinet/ip6.h>
8254bfbd51SWill Andrews #include <netinet6/ip6protosw.h>
83a9771948SGleb Smirnoff #include <netinet6/ip6_var.h>
8429da8af6SHajimu UMEMOTO #include <netinet6/scope6_var.h>
85a9771948SGleb Smirnoff #include <netinet6/nd6.h>
86a9771948SGleb Smirnoff #endif
87a9771948SGleb Smirnoff 
88a9771948SGleb Smirnoff #include <crypto/sha1.h>
89a9771948SGleb Smirnoff #include <netinet/ip_carp.h>
90a9771948SGleb Smirnoff 
91a9771948SGleb Smirnoff #define	CARP_IFNAME	"carp"
92a9771948SGleb Smirnoff static MALLOC_DEFINE(M_CARP, "CARP", "CARP interfaces");
93a9771948SGleb Smirnoff SYSCTL_DECL(_net_inet_carp);
94a9771948SGleb Smirnoff 
95a9771948SGleb Smirnoff struct carp_softc {
96fc74a9f9SBrooks Davis 	struct ifnet	 	*sc_ifp;	/* Interface clue */
973a84d72aSGleb Smirnoff 	struct ifnet		*sc_carpdev;	/* Pointer to parent interface */
98a9771948SGleb Smirnoff 	struct in_ifaddr 	*sc_ia;		/* primary iface address */
99a9771948SGleb Smirnoff 	struct ip_moptions 	 sc_imo;
100a9771948SGleb Smirnoff #ifdef INET6
101a9771948SGleb Smirnoff 	struct in6_ifaddr 	*sc_ia6;	/* primary iface address v6 */
102a9771948SGleb Smirnoff 	struct ip6_moptions 	 sc_im6o;
103a9771948SGleb Smirnoff #endif /* INET6 */
104a9771948SGleb Smirnoff 	TAILQ_ENTRY(carp_softc)	 sc_list;
105a9771948SGleb Smirnoff 
106a9771948SGleb Smirnoff 	enum { INIT = 0, BACKUP, MASTER }	sc_state;
107a9771948SGleb Smirnoff 
108a9771948SGleb Smirnoff 	int			 sc_flags_backup;
109a9771948SGleb Smirnoff 	int			 sc_suppress;
110a9771948SGleb Smirnoff 
111a9771948SGleb Smirnoff 	int			 sc_sendad_errors;
112a9771948SGleb Smirnoff #define	CARP_SENDAD_MAX_ERRORS	3
113a9771948SGleb Smirnoff 	int			 sc_sendad_success;
114a9771948SGleb Smirnoff #define	CARP_SENDAD_MIN_SUCCESS 3
115a9771948SGleb Smirnoff 
116a9771948SGleb Smirnoff 	int			 sc_vhid;
117a9771948SGleb Smirnoff 	int			 sc_advskew;
118a9771948SGleb Smirnoff 	int			 sc_naddrs;
119a9771948SGleb Smirnoff 	int			 sc_naddrs6;
120a9771948SGleb Smirnoff 	int			 sc_advbase;	/* seconds */
121a9771948SGleb Smirnoff 	int			 sc_init_counter;
122a9771948SGleb Smirnoff 	u_int64_t		 sc_counter;
123a9771948SGleb Smirnoff 
124a9771948SGleb Smirnoff 	/* authentication */
125a9771948SGleb Smirnoff #define CARP_HMAC_PAD	64
126a9771948SGleb Smirnoff 	unsigned char sc_key[CARP_KEY_LEN];
127a9771948SGleb Smirnoff 	unsigned char sc_pad[CARP_HMAC_PAD];
128a9771948SGleb Smirnoff 	SHA1_CTX sc_sha1;
129a9771948SGleb Smirnoff 
130a9771948SGleb Smirnoff 	struct callout		 sc_ad_tmo;	/* advertisement timeout */
131a9771948SGleb Smirnoff 	struct callout		 sc_md_tmo;	/* master down timeout */
132a9771948SGleb Smirnoff 	struct callout 		 sc_md6_tmo;	/* master down timeout */
133a9771948SGleb Smirnoff 
134a9771948SGleb Smirnoff 	LIST_ENTRY(carp_softc)	 sc_next;	/* Interface clue */
135a9771948SGleb Smirnoff };
136fc74a9f9SBrooks Davis #define	SC2IFP(sc)	((sc)->sc_ifp)
137a9771948SGleb Smirnoff 
138a9771948SGleb Smirnoff int carp_suppress_preempt = 0;
139a9771948SGleb Smirnoff int carp_opts[CARPCTL_MAXID] = { 0, 1, 0, 1, 0, 0 };	/* XXX for now */
14054bfbd51SWill Andrews SYSCTL_NODE(_net_inet, IPPROTO_CARP,	carp,	CTLFLAG_RW, 0,	"CARP");
141a9771948SGleb Smirnoff SYSCTL_INT(_net_inet_carp, CARPCTL_ALLOW, allow, CTLFLAG_RW,
142a9771948SGleb Smirnoff     &carp_opts[CARPCTL_ALLOW], 0, "Accept incoming CARP packets");
143a9771948SGleb Smirnoff SYSCTL_INT(_net_inet_carp, CARPCTL_PREEMPT, preempt, CTLFLAG_RW,
144a9771948SGleb Smirnoff     &carp_opts[CARPCTL_PREEMPT], 0, "high-priority backup preemption mode");
145a9771948SGleb Smirnoff SYSCTL_INT(_net_inet_carp, CARPCTL_LOG, log, CTLFLAG_RW,
146a9771948SGleb Smirnoff     &carp_opts[CARPCTL_LOG], 0, "log bad carp packets");
147a9771948SGleb Smirnoff SYSCTL_INT(_net_inet_carp, CARPCTL_ARPBALANCE, arpbalance, CTLFLAG_RW,
148a9771948SGleb Smirnoff     &carp_opts[CARPCTL_ARPBALANCE], 0, "balance arp responses");
14932247f86SGleb Smirnoff SYSCTL_INT(_net_inet_carp, OID_AUTO, suppress_preempt, CTLFLAG_RD,
15032247f86SGleb Smirnoff     &carp_suppress_preempt, 0, "Preemption is suppressed");
151a9771948SGleb Smirnoff 
152a9771948SGleb Smirnoff struct carpstats carpstats;
153a9771948SGleb Smirnoff SYSCTL_STRUCT(_net_inet_carp, CARPCTL_STATS, stats, CTLFLAG_RW,
154a9771948SGleb Smirnoff     &carpstats, carpstats,
155a9771948SGleb Smirnoff     "CARP statistics (struct carpstats, netinet/ip_carp.h)");
156a9771948SGleb Smirnoff 
157a9771948SGleb Smirnoff struct carp_if {
158a9771948SGleb Smirnoff 	TAILQ_HEAD(, carp_softc) vhif_vrs;
159a9771948SGleb Smirnoff 	int vhif_nvrs;
160a9771948SGleb Smirnoff 
161a9771948SGleb Smirnoff 	struct ifnet 	*vhif_ifp;
162a9771948SGleb Smirnoff 	struct mtx	 vhif_mtx;
163a9771948SGleb Smirnoff };
164d220759bSGleb Smirnoff 
16554bfbd51SWill Andrews #define	CARP_INET	0
16654bfbd51SWill Andrews #define	CARP_INET6	1
16754bfbd51SWill Andrews static int proto_reg[] = {-1, -1};
16854bfbd51SWill Andrews 
169d220759bSGleb Smirnoff /* Get carp_if from softc. Valid after carp_set_addr{,6}. */
170d220759bSGleb Smirnoff #define	SC2CIF(sc)		((struct carp_if *)(sc)->sc_carpdev->if_carp)
171d220759bSGleb Smirnoff 
172a9771948SGleb Smirnoff /* lock per carp_if queue */
173d220759bSGleb Smirnoff #define	CARP_LOCK_INIT(cif)	mtx_init(&(cif)->vhif_mtx, "carp_if", 	\
174a9771948SGleb Smirnoff 	NULL, MTX_DEF)
175d220759bSGleb Smirnoff #define	CARP_LOCK_DESTROY(cif)	mtx_destroy(&(cif)->vhif_mtx)
176a9771948SGleb Smirnoff #define	CARP_LOCK_ASSERT(cif)	mtx_assert(&(cif)->vhif_mtx, MA_OWNED)
177a9771948SGleb Smirnoff #define	CARP_LOCK(cif)		mtx_lock(&(cif)->vhif_mtx)
178a9771948SGleb Smirnoff #define	CARP_UNLOCK(cif)	mtx_unlock(&(cif)->vhif_mtx)
179a9771948SGleb Smirnoff 
180d220759bSGleb Smirnoff #define	CARP_SCLOCK(sc)		mtx_lock(&SC2CIF(sc)->vhif_mtx)
181d220759bSGleb Smirnoff #define	CARP_SCUNLOCK(sc)	mtx_unlock(&SC2CIF(sc)->vhif_mtx)
182d220759bSGleb Smirnoff #define	CARP_SCLOCK_ASSERT(sc)	mtx_assert(&SC2CIF(sc)->vhif_mtx, MA_OWNED)
183d220759bSGleb Smirnoff 
184947b7cf3SGleb Smirnoff #define	CARP_LOG(...)	do {				\
1851e9e6572SGleb Smirnoff 	if (carp_opts[CARPCTL_LOG] > 0)			\
1861e9e6572SGleb Smirnoff 		log(LOG_INFO, __VA_ARGS__);		\
187947b7cf3SGleb Smirnoff } while (0)
1881e9e6572SGleb Smirnoff 
189947b7cf3SGleb Smirnoff #define	CARP_DEBUG(...)	do {				\
1901e9e6572SGleb Smirnoff 	if (carp_opts[CARPCTL_LOG] > 1)			\
1911e9e6572SGleb Smirnoff 		log(LOG_DEBUG, __VA_ARGS__);		\
192947b7cf3SGleb Smirnoff } while (0)
193a9771948SGleb Smirnoff 
1945c1f0f6dSGleb Smirnoff static void	carp_hmac_prepare(struct carp_softc *);
1955c1f0f6dSGleb Smirnoff static void	carp_hmac_generate(struct carp_softc *, u_int32_t *,
196a9771948SGleb Smirnoff 		    unsigned char *);
1975c1f0f6dSGleb Smirnoff static int	carp_hmac_verify(struct carp_softc *, u_int32_t *,
198a9771948SGleb Smirnoff 		    unsigned char *);
1995c1f0f6dSGleb Smirnoff static void	carp_setroute(struct carp_softc *, int);
2005c1f0f6dSGleb Smirnoff static void	carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
2016b7330e2SSam Leffler static int 	carp_clone_create(struct if_clone *, int, caddr_t);
2025c1f0f6dSGleb Smirnoff static void 	carp_clone_destroy(struct ifnet *);
2033cf0d024SGleb Smirnoff static void	carpdetach(struct carp_softc *, int);
2045c1f0f6dSGleb Smirnoff static int	carp_prepare_ad(struct mbuf *, struct carp_softc *,
205a9771948SGleb Smirnoff 		    struct carp_header *);
2065c1f0f6dSGleb Smirnoff static void	carp_send_ad_all(void);
2075c1f0f6dSGleb Smirnoff static void	carp_send_ad(void *);
208d220759bSGleb Smirnoff static void	carp_send_ad_locked(struct carp_softc *);
2095c1f0f6dSGleb Smirnoff static void	carp_send_arp(struct carp_softc *);
2105c1f0f6dSGleb Smirnoff static void	carp_master_down(void *);
211d220759bSGleb Smirnoff static void	carp_master_down_locked(struct carp_softc *);
2125c1f0f6dSGleb Smirnoff static int	carp_ioctl(struct ifnet *, u_long, caddr_t);
213a9771948SGleb Smirnoff static int	carp_looutput(struct ifnet *, struct mbuf *, struct sockaddr *,
214279aa3d4SKip Macy     		    struct route *);
2155c1f0f6dSGleb Smirnoff static void	carp_start(struct ifnet *);
2165c1f0f6dSGleb Smirnoff static void	carp_setrun(struct carp_softc *, sa_family_t);
2175c1f0f6dSGleb Smirnoff static void	carp_set_state(struct carp_softc *, int);
2185c1f0f6dSGleb Smirnoff static int	carp_addrcount(struct carp_if *, struct in_ifaddr *, int);
219a9771948SGleb Smirnoff enum	{ CARP_COUNT_MASTER, CARP_COUNT_RUNNING };
220a9771948SGleb Smirnoff 
2210fa08018SGleb Smirnoff static void	carp_multicast_cleanup(struct carp_softc *);
2225c1f0f6dSGleb Smirnoff static int	carp_set_addr(struct carp_softc *, struct sockaddr_in *);
2235c1f0f6dSGleb Smirnoff static int	carp_del_addr(struct carp_softc *, struct sockaddr_in *);
224d220759bSGleb Smirnoff static void	carp_carpdev_state_locked(struct carp_if *);
2254cb39345SGleb Smirnoff static void	carp_sc_state_locked(struct carp_softc *);
226a9771948SGleb Smirnoff #ifdef INET6
2275c1f0f6dSGleb Smirnoff static void	carp_send_na(struct carp_softc *);
2285c1f0f6dSGleb Smirnoff static int	carp_set_addr6(struct carp_softc *, struct sockaddr_in6 *);
2295c1f0f6dSGleb Smirnoff static int	carp_del_addr6(struct carp_softc *, struct sockaddr_in6 *);
230fbfdcf87SGleb Smirnoff static void	carp_multicast6_cleanup(struct carp_softc *);
231a9771948SGleb Smirnoff #endif
232a9771948SGleb Smirnoff 
233a9771948SGleb Smirnoff static LIST_HEAD(, carp_softc) carpif_list;
234d92d54d5SGleb Smirnoff static struct mtx carp_mtx;
235a9771948SGleb Smirnoff IFC_SIMPLE_DECLARE(carp, 0);
236a9771948SGleb Smirnoff 
2370fa08018SGleb Smirnoff static eventhandler_tag if_detach_event_tag;
2380fa08018SGleb Smirnoff 
239a9771948SGleb Smirnoff static __inline u_int16_t
240a9771948SGleb Smirnoff carp_cksum(struct mbuf *m, int len)
241a9771948SGleb Smirnoff {
242a9771948SGleb Smirnoff 	return (in_cksum(m, len));
243a9771948SGleb Smirnoff }
244a9771948SGleb Smirnoff 
2455c1f0f6dSGleb Smirnoff static void
246a9771948SGleb Smirnoff carp_hmac_prepare(struct carp_softc *sc)
247a9771948SGleb Smirnoff {
248a9771948SGleb Smirnoff 	u_int8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
249a9771948SGleb Smirnoff 	u_int8_t vhid = sc->sc_vhid & 0xff;
250a9771948SGleb Smirnoff 	struct ifaddr *ifa;
2511ead26d4SMax Laier 	int i, found;
2521ead26d4SMax Laier #ifdef INET
2531ead26d4SMax Laier 	struct in_addr last, cur, in;
2541ead26d4SMax Laier #endif
255a9771948SGleb Smirnoff #ifdef INET6
2561ead26d4SMax Laier 	struct in6_addr last6, cur6, in6;
257a9771948SGleb Smirnoff #endif
258a9771948SGleb Smirnoff 
259d220759bSGleb Smirnoff 	if (sc->sc_carpdev)
260d220759bSGleb Smirnoff 		CARP_SCLOCK(sc);
261d220759bSGleb Smirnoff 
262d220759bSGleb Smirnoff 	/* XXX: possible race here */
263d220759bSGleb Smirnoff 
264a9771948SGleb Smirnoff 	/* compute ipad from key */
265a9771948SGleb Smirnoff 	bzero(sc->sc_pad, sizeof(sc->sc_pad));
266a9771948SGleb Smirnoff 	bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
267a9771948SGleb Smirnoff 	for (i = 0; i < sizeof(sc->sc_pad); i++)
268a9771948SGleb Smirnoff 		sc->sc_pad[i] ^= 0x36;
269a9771948SGleb Smirnoff 
270a9771948SGleb Smirnoff 	/* precompute first part of inner hash */
271a9771948SGleb Smirnoff 	SHA1Init(&sc->sc_sha1);
272a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
273a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
274a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
275a9771948SGleb Smirnoff 	SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
276a9771948SGleb Smirnoff #ifdef INET
2771ead26d4SMax Laier 	cur.s_addr = 0;
2781ead26d4SMax Laier 	do {
2791ead26d4SMax Laier 		found = 0;
2801ead26d4SMax Laier 		last = cur;
2811ead26d4SMax Laier 		cur.s_addr = 0xffffffff;
282db091502SRobert Watson 		IF_ADDR_LOCK(SC2IFP(sc));
283fc74a9f9SBrooks Davis 		TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
2841ead26d4SMax Laier 			in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr;
2851ead26d4SMax Laier 			if (ifa->ifa_addr->sa_family == AF_INET &&
2861ead26d4SMax Laier 			    ntohl(in.s_addr) > ntohl(last.s_addr) &&
2871ead26d4SMax Laier 			    ntohl(in.s_addr) < ntohl(cur.s_addr)) {
2881ead26d4SMax Laier 				cur.s_addr = in.s_addr;
2891ead26d4SMax Laier 				found++;
290a9771948SGleb Smirnoff 			}
2911ead26d4SMax Laier 		}
292db091502SRobert Watson 		IF_ADDR_UNLOCK(SC2IFP(sc));
2931ead26d4SMax Laier 		if (found)
2941ead26d4SMax Laier 			SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur));
2951ead26d4SMax Laier 	} while (found);
296a9771948SGleb Smirnoff #endif /* INET */
297a9771948SGleb Smirnoff #ifdef INET6
2981ead26d4SMax Laier 	memset(&cur6, 0, sizeof(cur6));
2991ead26d4SMax Laier 	do {
3001ead26d4SMax Laier 		found = 0;
3011ead26d4SMax Laier 		last6 = cur6;
3021ead26d4SMax Laier 		memset(&cur6, 0xff, sizeof(cur6));
303db091502SRobert Watson 		IF_ADDR_LOCK(SC2IFP(sc));
304fc74a9f9SBrooks Davis 		TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
305a9771948SGleb Smirnoff 			in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
3061ead26d4SMax Laier 			if (IN6_IS_SCOPE_EMBED(&in6))
3071ead26d4SMax Laier 				in6.s6_addr16[1] = 0;
3081ead26d4SMax Laier 			if (ifa->ifa_addr->sa_family == AF_INET6 &&
3091ead26d4SMax Laier 			    memcmp(&in6, &last6, sizeof(in6)) > 0 &&
3101ead26d4SMax Laier 			    memcmp(&in6, &cur6, sizeof(in6)) < 0) {
3111ead26d4SMax Laier 				cur6 = in6;
3121ead26d4SMax Laier 				found++;
313a9771948SGleb Smirnoff 			}
314a9771948SGleb Smirnoff 		}
315db091502SRobert Watson 		IF_ADDR_UNLOCK(SC2IFP(sc));
3161ead26d4SMax Laier 		if (found)
3171ead26d4SMax Laier 			SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6));
3181ead26d4SMax Laier 	} while (found);
319a9771948SGleb Smirnoff #endif /* INET6 */
320a9771948SGleb Smirnoff 
321a9771948SGleb Smirnoff 	/* convert ipad to opad */
322a9771948SGleb Smirnoff 	for (i = 0; i < sizeof(sc->sc_pad); i++)
323a9771948SGleb Smirnoff 		sc->sc_pad[i] ^= 0x36 ^ 0x5c;
324d220759bSGleb Smirnoff 
325d220759bSGleb Smirnoff 	if (sc->sc_carpdev)
326d220759bSGleb Smirnoff 		CARP_SCUNLOCK(sc);
327a9771948SGleb Smirnoff }
328a9771948SGleb Smirnoff 
3295c1f0f6dSGleb Smirnoff static void
330a9771948SGleb Smirnoff carp_hmac_generate(struct carp_softc *sc, u_int32_t counter[2],
331a9771948SGleb Smirnoff     unsigned char md[20])
332a9771948SGleb Smirnoff {
333a9771948SGleb Smirnoff 	SHA1_CTX sha1ctx;
334a9771948SGleb Smirnoff 
335a9771948SGleb Smirnoff 	/* fetch first half of inner hash */
336a9771948SGleb Smirnoff 	bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
337a9771948SGleb Smirnoff 
338a9771948SGleb Smirnoff 	SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
339a9771948SGleb Smirnoff 	SHA1Final(md, &sha1ctx);
340a9771948SGleb Smirnoff 
341a9771948SGleb Smirnoff 	/* outer hash */
342a9771948SGleb Smirnoff 	SHA1Init(&sha1ctx);
343a9771948SGleb Smirnoff 	SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
344a9771948SGleb Smirnoff 	SHA1Update(&sha1ctx, md, 20);
345a9771948SGleb Smirnoff 	SHA1Final(md, &sha1ctx);
346a9771948SGleb Smirnoff }
347a9771948SGleb Smirnoff 
3485c1f0f6dSGleb Smirnoff static int
349a9771948SGleb Smirnoff carp_hmac_verify(struct carp_softc *sc, u_int32_t counter[2],
350a9771948SGleb Smirnoff     unsigned char md[20])
351a9771948SGleb Smirnoff {
352a9771948SGleb Smirnoff 	unsigned char md2[20];
353a9771948SGleb Smirnoff 
354d220759bSGleb Smirnoff 	CARP_SCLOCK_ASSERT(sc);
355d220759bSGleb Smirnoff 
356a9771948SGleb Smirnoff 	carp_hmac_generate(sc, counter, md2);
357a9771948SGleb Smirnoff 
358a9771948SGleb Smirnoff 	return (bcmp(md, md2, sizeof(md2)));
359a9771948SGleb Smirnoff }
360a9771948SGleb Smirnoff 
3615c1f0f6dSGleb Smirnoff static void
362a9771948SGleb Smirnoff carp_setroute(struct carp_softc *sc, int cmd)
363a9771948SGleb Smirnoff {
364a9771948SGleb Smirnoff 	struct ifaddr *ifa;
365a9771948SGleb Smirnoff 	int s;
366a9771948SGleb Smirnoff 
367d220759bSGleb Smirnoff 	if (sc->sc_carpdev)
368d220759bSGleb Smirnoff 		CARP_SCLOCK_ASSERT(sc);
369d220759bSGleb Smirnoff 
370a9771948SGleb Smirnoff 	s = splnet();
371fc74a9f9SBrooks Davis 	TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
372d220759bSGleb Smirnoff 		if (ifa->ifa_addr->sa_family == AF_INET &&
373d220759bSGleb Smirnoff 		    sc->sc_carpdev != NULL) {
374a9771948SGleb Smirnoff 			int count = carp_addrcount(
375e8c34a71SGleb Smirnoff 			    (struct carp_if *)sc->sc_carpdev->if_carp,
376a9771948SGleb Smirnoff 			    ifatoia(ifa), CARP_COUNT_MASTER);
377a9771948SGleb Smirnoff 
378a9771948SGleb Smirnoff 			if ((cmd == RTM_ADD && count == 1) ||
379a9771948SGleb Smirnoff 			    (cmd == RTM_DELETE && count == 0))
380a9771948SGleb Smirnoff 				rtinit(ifa, cmd, RTF_UP | RTF_HOST);
381a9771948SGleb Smirnoff 		}
382a9771948SGleb Smirnoff 	}
383a9771948SGleb Smirnoff 	splx(s);
384a9771948SGleb Smirnoff }
385a9771948SGleb Smirnoff 
3865c1f0f6dSGleb Smirnoff static int
3876b7330e2SSam Leffler carp_clone_create(struct if_clone *ifc, int unit, caddr_t params)
388a9771948SGleb Smirnoff {
389a9771948SGleb Smirnoff 
390a9771948SGleb Smirnoff 	struct carp_softc *sc;
391a9771948SGleb Smirnoff 	struct ifnet *ifp;
392a9771948SGleb Smirnoff 
3931ede983cSDag-Erling Smørgrav 	sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
394fc74a9f9SBrooks Davis 	ifp = SC2IFP(sc) = if_alloc(IFT_ETHER);
395fc74a9f9SBrooks Davis 	if (ifp == NULL) {
3961ede983cSDag-Erling Smørgrav 		free(sc, M_CARP);
397fc74a9f9SBrooks Davis 		return (ENOSPC);
398fc74a9f9SBrooks Davis 	}
399a9771948SGleb Smirnoff 
400a9771948SGleb Smirnoff 	sc->sc_flags_backup = 0;
401a9771948SGleb Smirnoff 	sc->sc_suppress = 0;
402a9771948SGleb Smirnoff 	sc->sc_advbase = CARP_DFLTINTV;
403a9771948SGleb Smirnoff 	sc->sc_vhid = -1;	/* required setting */
404a9771948SGleb Smirnoff 	sc->sc_advskew = 0;
405a9771948SGleb Smirnoff 	sc->sc_init_counter = 1;
406a9771948SGleb Smirnoff 	sc->sc_naddrs = sc->sc_naddrs6 = 0; /* M_ZERO? */
40705206588SMax Laier 	sc->sc_imo.imo_membership = (struct in_multi **)malloc(
40805206588SMax Laier 	    (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_CARP,
40905206588SMax Laier 	    M_WAITOK);
41071498f30SBruce M Simpson 	sc->sc_imo.imo_mfilters = NULL;
41105206588SMax Laier 	sc->sc_imo.imo_max_memberships = IP_MIN_MEMBERSHIPS;
41213c83844SBruce M Simpson 	sc->sc_imo.imo_multicast_vif = -1;
41333cde130SBruce M Simpson #ifdef INET6
41433cde130SBruce M Simpson 	sc->sc_im6o.im6o_membership = (struct in6_multi **)malloc(
41533cde130SBruce M Simpson 	    (sizeof(struct in6_multi *) * IPV6_MIN_MEMBERSHIPS), M_CARP,
41633cde130SBruce M Simpson 	    M_WAITOK);
41733cde130SBruce M Simpson 	sc->sc_im6o.im6o_mfilters = NULL;
41833cde130SBruce M Simpson 	sc->sc_im6o.im6o_max_memberships = IPV6_MIN_MEMBERSHIPS;
41933cde130SBruce M Simpson 	sc->sc_im6o.im6o_multicast_hlim = CARP_DFLTTL;
42033cde130SBruce M Simpson #endif
421a9771948SGleb Smirnoff 
422c6b28997SRobert Watson 	callout_init(&sc->sc_ad_tmo, CALLOUT_MPSAFE);
423c6b28997SRobert Watson 	callout_init(&sc->sc_md_tmo, CALLOUT_MPSAFE);
424c6b28997SRobert Watson 	callout_init(&sc->sc_md6_tmo, CALLOUT_MPSAFE);
425a9771948SGleb Smirnoff 
426a9771948SGleb Smirnoff 	ifp->if_softc = sc;
427a9771948SGleb Smirnoff 	if_initname(ifp, CARP_IFNAME, unit);
428a9771948SGleb Smirnoff 	ifp->if_mtu = ETHERMTU;
4299f4abef9SYaroslav Tykhiy 	ifp->if_flags = IFF_LOOPBACK;
430a9771948SGleb Smirnoff 	ifp->if_ioctl = carp_ioctl;
431a9771948SGleb Smirnoff 	ifp->if_output = carp_looutput;
432a9771948SGleb Smirnoff 	ifp->if_start = carp_start;
433a9771948SGleb Smirnoff 	ifp->if_type = IFT_CARP;
434a9771948SGleb Smirnoff 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
435a9771948SGleb Smirnoff 	ifp->if_hdrlen = 0;
436a9771948SGleb Smirnoff 	if_attach(ifp);
437fc74a9f9SBrooks Davis 	bpfattach(SC2IFP(sc), DLT_NULL, sizeof(u_int32_t));
438d92d54d5SGleb Smirnoff 	mtx_lock(&carp_mtx);
439d92d54d5SGleb Smirnoff 	LIST_INSERT_HEAD(&carpif_list, sc, sc_next);
440d92d54d5SGleb Smirnoff 	mtx_unlock(&carp_mtx);
441a9771948SGleb Smirnoff 	return (0);
442a9771948SGleb Smirnoff }
443a9771948SGleb Smirnoff 
4445c1f0f6dSGleb Smirnoff static void
445a9771948SGleb Smirnoff carp_clone_destroy(struct ifnet *ifp)
446a9771948SGleb Smirnoff {
447a9771948SGleb Smirnoff 	struct carp_softc *sc = ifp->if_softc;
448a9771948SGleb Smirnoff 
4490fa08018SGleb Smirnoff 	if (sc->sc_carpdev)
4500fa08018SGleb Smirnoff 		CARP_SCLOCK(sc);
4513cf0d024SGleb Smirnoff 	carpdetach(sc, 1);	/* Returns unlocked. */
452a9771948SGleb Smirnoff 
453d92d54d5SGleb Smirnoff 	mtx_lock(&carp_mtx);
454d92d54d5SGleb Smirnoff 	LIST_REMOVE(sc, sc_next);
455d92d54d5SGleb Smirnoff 	mtx_unlock(&carp_mtx);
456a9771948SGleb Smirnoff 	bpfdetach(ifp);
457a9771948SGleb Smirnoff 	if_detach(ifp);
458fc74a9f9SBrooks Davis 	if_free_type(ifp, IFT_ETHER);
45905206588SMax Laier 	free(sc->sc_imo.imo_membership, M_CARP);
46033cde130SBruce M Simpson #ifdef INET6
46133cde130SBruce M Simpson 	free(sc->sc_im6o.im6o_membership, M_CARP);
46233cde130SBruce M Simpson #endif
463a9771948SGleb Smirnoff 	free(sc, M_CARP);
464a9771948SGleb Smirnoff }
465a9771948SGleb Smirnoff 
466fbfdcf87SGleb Smirnoff /*
467fbfdcf87SGleb Smirnoff  * This function can be called on CARP interface destroy path,
468fbfdcf87SGleb Smirnoff  * and in case of the removal of the underlying interface as
469fbfdcf87SGleb Smirnoff  * well. We differentiate these two cases. In the latter case
470fbfdcf87SGleb Smirnoff  * we do not cleanup our multicast memberships, since they
471fbfdcf87SGleb Smirnoff  * are already freed. Also, in the latter case we do not
472fbfdcf87SGleb Smirnoff  * release the lock on return, because the function will be
473fbfdcf87SGleb Smirnoff  * called once more, for another CARP instance on the same
474fbfdcf87SGleb Smirnoff  * interface.
475fbfdcf87SGleb Smirnoff  */
4760fa08018SGleb Smirnoff static void
4773cf0d024SGleb Smirnoff carpdetach(struct carp_softc *sc, int unlock)
4780fa08018SGleb Smirnoff {
4790fa08018SGleb Smirnoff 	struct carp_if *cif;
4800fa08018SGleb Smirnoff 
4810fa08018SGleb Smirnoff 	callout_stop(&sc->sc_ad_tmo);
4820fa08018SGleb Smirnoff 	callout_stop(&sc->sc_md_tmo);
4830fa08018SGleb Smirnoff 	callout_stop(&sc->sc_md6_tmo);
4840fa08018SGleb Smirnoff 
4850fa08018SGleb Smirnoff 	if (sc->sc_suppress)
4860fa08018SGleb Smirnoff 		carp_suppress_preempt--;
4870fa08018SGleb Smirnoff 	sc->sc_suppress = 0;
4880fa08018SGleb Smirnoff 
4890fa08018SGleb Smirnoff 	if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS)
4900fa08018SGleb Smirnoff 		carp_suppress_preempt--;
4910fa08018SGleb Smirnoff 	sc->sc_sendad_errors = 0;
4920fa08018SGleb Smirnoff 
4930fa08018SGleb Smirnoff 	carp_set_state(sc, INIT);
4940fa08018SGleb Smirnoff 	SC2IFP(sc)->if_flags &= ~IFF_UP;
4950fa08018SGleb Smirnoff 	carp_setrun(sc, 0);
496fbfdcf87SGleb Smirnoff 	if (unlock)
4970fa08018SGleb Smirnoff 		carp_multicast_cleanup(sc);
498fbfdcf87SGleb Smirnoff #ifdef INET6
499fbfdcf87SGleb Smirnoff 	carp_multicast6_cleanup(sc);
500fbfdcf87SGleb Smirnoff #endif
5010fa08018SGleb Smirnoff 
5020fa08018SGleb Smirnoff 	if (sc->sc_carpdev != NULL) {
5030fa08018SGleb Smirnoff 		cif = (struct carp_if *)sc->sc_carpdev->if_carp;
5040fa08018SGleb Smirnoff 		CARP_LOCK_ASSERT(cif);
5050fa08018SGleb Smirnoff 		TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
5060fa08018SGleb Smirnoff 		if (!--cif->vhif_nvrs) {
5070fa08018SGleb Smirnoff 			ifpromisc(sc->sc_carpdev, 0);
5080fa08018SGleb Smirnoff 			sc->sc_carpdev->if_carp = NULL;
5090fa08018SGleb Smirnoff 			CARP_LOCK_DESTROY(cif);
51052e12426SWill Andrews 			free(cif, M_CARP);
5113cf0d024SGleb Smirnoff 		} else if (unlock)
5123cf0d024SGleb Smirnoff 			CARP_UNLOCK(cif);
5130fa08018SGleb Smirnoff 		sc->sc_carpdev = NULL;
5140fa08018SGleb Smirnoff 	}
5153cf0d024SGleb Smirnoff }
5160fa08018SGleb Smirnoff 
5170fa08018SGleb Smirnoff /* Detach an interface from the carp. */
5180fa08018SGleb Smirnoff static void
5190fa08018SGleb Smirnoff carp_ifdetach(void *arg __unused, struct ifnet *ifp)
5200fa08018SGleb Smirnoff {
5210fa08018SGleb Smirnoff 	struct carp_if *cif = (struct carp_if *)ifp->if_carp;
5220fa08018SGleb Smirnoff 	struct carp_softc *sc, *nextsc;
5230fa08018SGleb Smirnoff 
5240fa08018SGleb Smirnoff 	if (cif == NULL)
5250fa08018SGleb Smirnoff 		return;
5260fa08018SGleb Smirnoff 
5270fa08018SGleb Smirnoff 	/*
5280fa08018SGleb Smirnoff 	 * XXX: At the end of for() cycle the lock will be destroyed.
5290fa08018SGleb Smirnoff 	 */
5300fa08018SGleb Smirnoff 	CARP_LOCK(cif);
5310fa08018SGleb Smirnoff 	for (sc = TAILQ_FIRST(&cif->vhif_vrs); sc; sc = nextsc) {
5320fa08018SGleb Smirnoff 		nextsc = TAILQ_NEXT(sc, sc_list);
5333cf0d024SGleb Smirnoff 		carpdetach(sc, 0);
5340fa08018SGleb Smirnoff 	}
5350fa08018SGleb Smirnoff }
5360fa08018SGleb Smirnoff 
537a9771948SGleb Smirnoff /*
538a9771948SGleb Smirnoff  * process input packet.
539a9771948SGleb Smirnoff  * we have rearranged checks order compared to the rfc,
540a9771948SGleb Smirnoff  * but it seems more efficient this way or not possible otherwise.
541a9771948SGleb Smirnoff  */
542a9771948SGleb Smirnoff void
543a9771948SGleb Smirnoff carp_input(struct mbuf *m, int hlen)
544a9771948SGleb Smirnoff {
545a9771948SGleb Smirnoff 	struct ip *ip = mtod(m, struct ip *);
546a9771948SGleb Smirnoff 	struct carp_header *ch;
547a9771948SGleb Smirnoff 	int iplen, len;
548a9771948SGleb Smirnoff 
5496bf65bcfSRobert Watson 	CARPSTATS_INC(carps_ipackets);
550a9771948SGleb Smirnoff 
551a9771948SGleb Smirnoff 	if (!carp_opts[CARPCTL_ALLOW]) {
552a9771948SGleb Smirnoff 		m_freem(m);
553a9771948SGleb Smirnoff 		return;
554a9771948SGleb Smirnoff 	}
555a9771948SGleb Smirnoff 
556a9771948SGleb Smirnoff 	/* check if received on a valid carp interface */
557a9771948SGleb Smirnoff 	if (m->m_pkthdr.rcvif->if_carp == NULL) {
5586bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badif);
559e81ab876SGleb Smirnoff 		CARP_DEBUG("carp_input: packet received on non-carp "
56088bf82a6SGleb Smirnoff 		    "interface: %s\n",
5611e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
562a9771948SGleb Smirnoff 		m_freem(m);
563a9771948SGleb Smirnoff 		return;
564a9771948SGleb Smirnoff 	}
565a9771948SGleb Smirnoff 
566a9771948SGleb Smirnoff 	/* verify that the IP TTL is 255.  */
567a9771948SGleb Smirnoff 	if (ip->ip_ttl != CARP_DFLTTL) {
5686bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badttl);
569acc0fee0SRuslan Ermilov 		CARP_DEBUG("carp_input: received ttl %d != 255 on %s\n",
5701e9e6572SGleb Smirnoff 		    ip->ip_ttl,
5711e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
572a9771948SGleb Smirnoff 		m_freem(m);
573a9771948SGleb Smirnoff 		return;
574a9771948SGleb Smirnoff 	}
575a9771948SGleb Smirnoff 
576a9771948SGleb Smirnoff 	iplen = ip->ip_hl << 2;
577a9771948SGleb Smirnoff 
578a9771948SGleb Smirnoff 	if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
5796bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badlen);
580e81ab876SGleb Smirnoff 		CARP_DEBUG("carp_input: received len %zd < "
58116e324fcSXin LI 		    "sizeof(struct carp_header) on %s\n",
58216e324fcSXin LI 		    m->m_len - sizeof(struct ip),
58316e324fcSXin LI 		    m->m_pkthdr.rcvif->if_xname);
584a9771948SGleb Smirnoff 		m_freem(m);
585a9771948SGleb Smirnoff 		return;
586a9771948SGleb Smirnoff 	}
587a9771948SGleb Smirnoff 
588a9771948SGleb Smirnoff 	if (iplen + sizeof(*ch) < m->m_len) {
589a9771948SGleb Smirnoff 		if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
5906bf65bcfSRobert Watson 			CARPSTATS_INC(carps_hdrops);
591e81ab876SGleb Smirnoff 			CARP_DEBUG("carp_input: pullup failed\n");
592a9771948SGleb Smirnoff 			return;
593a9771948SGleb Smirnoff 		}
594a9771948SGleb Smirnoff 		ip = mtod(m, struct ip *);
595a9771948SGleb Smirnoff 	}
596a9771948SGleb Smirnoff 	ch = (struct carp_header *)((char *)ip + iplen);
597a9771948SGleb Smirnoff 
598a9771948SGleb Smirnoff 	/*
599a9771948SGleb Smirnoff 	 * verify that the received packet length is
600a9771948SGleb Smirnoff 	 * equal to the CARP header
601a9771948SGleb Smirnoff 	 */
602a9771948SGleb Smirnoff 	len = iplen + sizeof(*ch);
603a9771948SGleb Smirnoff 	if (len > m->m_pkthdr.len) {
6046bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badlen);
605e81ab876SGleb Smirnoff 		CARP_DEBUG("carp_input: packet too short %d on %s\n",
6061e9e6572SGleb Smirnoff 		    m->m_pkthdr.len,
6071e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
608a9771948SGleb Smirnoff 		m_freem(m);
609a9771948SGleb Smirnoff 		return;
610a9771948SGleb Smirnoff 	}
611a9771948SGleb Smirnoff 
612a9771948SGleb Smirnoff 	if ((m = m_pullup(m, len)) == NULL) {
6136bf65bcfSRobert Watson 		CARPSTATS_INC(carps_hdrops);
614a9771948SGleb Smirnoff 		return;
615a9771948SGleb Smirnoff 	}
616a9771948SGleb Smirnoff 	ip = mtod(m, struct ip *);
617a9771948SGleb Smirnoff 	ch = (struct carp_header *)((char *)ip + iplen);
618a9771948SGleb Smirnoff 
619a9771948SGleb Smirnoff 	/* verify the CARP checksum */
620a9771948SGleb Smirnoff 	m->m_data += iplen;
621a9771948SGleb Smirnoff 	if (carp_cksum(m, len - iplen)) {
6226bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badsum);
623e81ab876SGleb Smirnoff 		CARP_DEBUG("carp_input: checksum failed on %s\n",
6241e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
625a9771948SGleb Smirnoff 		m_freem(m);
626a9771948SGleb Smirnoff 		return;
627a9771948SGleb Smirnoff 	}
628a9771948SGleb Smirnoff 	m->m_data -= iplen;
629a9771948SGleb Smirnoff 
6301e9e6572SGleb Smirnoff 	carp_input_c(m, ch, AF_INET);
631a9771948SGleb Smirnoff }
632a9771948SGleb Smirnoff 
633a9771948SGleb Smirnoff #ifdef INET6
634a9771948SGleb Smirnoff int
635a9771948SGleb Smirnoff carp6_input(struct mbuf **mp, int *offp, int proto)
636a9771948SGleb Smirnoff {
637a9771948SGleb Smirnoff 	struct mbuf *m = *mp;
638a9771948SGleb Smirnoff 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
639a9771948SGleb Smirnoff 	struct carp_header *ch;
640a9771948SGleb Smirnoff 	u_int len;
641a9771948SGleb Smirnoff 
6426bf65bcfSRobert Watson 	CARPSTATS_INC(carps_ipackets6);
643a9771948SGleb Smirnoff 
644a9771948SGleb Smirnoff 	if (!carp_opts[CARPCTL_ALLOW]) {
645a9771948SGleb Smirnoff 		m_freem(m);
646a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
647a9771948SGleb Smirnoff 	}
648a9771948SGleb Smirnoff 
649a9771948SGleb Smirnoff 	/* check if received on a valid carp interface */
650a9771948SGleb Smirnoff 	if (m->m_pkthdr.rcvif->if_carp == NULL) {
6516bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badif);
652e81ab876SGleb Smirnoff 		CARP_DEBUG("carp6_input: packet received on non-carp "
65388bf82a6SGleb Smirnoff 		    "interface: %s\n",
6541e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
655a9771948SGleb Smirnoff 		m_freem(m);
656a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
657a9771948SGleb Smirnoff 	}
658a9771948SGleb Smirnoff 
659a9771948SGleb Smirnoff 	/* verify that the IP TTL is 255 */
660a9771948SGleb Smirnoff 	if (ip6->ip6_hlim != CARP_DFLTTL) {
6616bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badttl);
662e81ab876SGleb Smirnoff 		CARP_DEBUG("carp6_input: received ttl %d != 255 on %s\n",
6631e9e6572SGleb Smirnoff 		    ip6->ip6_hlim,
6641e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
665a9771948SGleb Smirnoff 		m_freem(m);
666a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
667a9771948SGleb Smirnoff 	}
668a9771948SGleb Smirnoff 
669a9771948SGleb Smirnoff 	/* verify that we have a complete carp packet */
670a9771948SGleb Smirnoff 	len = m->m_len;
671a9771948SGleb Smirnoff 	IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
672a9771948SGleb Smirnoff 	if (ch == NULL) {
6736bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badlen);
674e81ab876SGleb Smirnoff 		CARP_DEBUG("carp6_input: packet size %u too small\n", len);
675a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
676a9771948SGleb Smirnoff 	}
677a9771948SGleb Smirnoff 
678a9771948SGleb Smirnoff 
679a9771948SGleb Smirnoff 	/* verify the CARP checksum */
680a9771948SGleb Smirnoff 	m->m_data += *offp;
681a9771948SGleb Smirnoff 	if (carp_cksum(m, sizeof(*ch))) {
6826bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badsum);
683e81ab876SGleb Smirnoff 		CARP_DEBUG("carp6_input: checksum failed, on %s\n",
6841e9e6572SGleb Smirnoff 		    m->m_pkthdr.rcvif->if_xname);
685a9771948SGleb Smirnoff 		m_freem(m);
686a9771948SGleb Smirnoff 		return (IPPROTO_DONE);
687a9771948SGleb Smirnoff 	}
688a9771948SGleb Smirnoff 	m->m_data -= *offp;
689a9771948SGleb Smirnoff 
6901e9e6572SGleb Smirnoff 	carp_input_c(m, ch, AF_INET6);
691a9771948SGleb Smirnoff 	return (IPPROTO_DONE);
692a9771948SGleb Smirnoff }
693a9771948SGleb Smirnoff #endif /* INET6 */
694a9771948SGleb Smirnoff 
6955c1f0f6dSGleb Smirnoff static void
6961e9e6572SGleb Smirnoff carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
697a9771948SGleb Smirnoff {
698a9771948SGleb Smirnoff 	struct ifnet *ifp = m->m_pkthdr.rcvif;
6991e9e6572SGleb Smirnoff 	struct carp_softc *sc;
700a9771948SGleb Smirnoff 	u_int64_t tmp_counter;
701a9771948SGleb Smirnoff 	struct timeval sc_tv, ch_tv;
702a9771948SGleb Smirnoff 
703a9771948SGleb Smirnoff 	/* verify that the VHID is valid on the receiving interface */
704a9771948SGleb Smirnoff 	CARP_LOCK(ifp->if_carp);
705a9771948SGleb Smirnoff 	TAILQ_FOREACH(sc, &((struct carp_if *)ifp->if_carp)->vhif_vrs, sc_list)
706a9771948SGleb Smirnoff 		if (sc->sc_vhid == ch->carp_vhid)
707a9771948SGleb Smirnoff 			break;
708d220759bSGleb Smirnoff 
70913f4c340SRobert Watson 	if (!sc || !((SC2IFP(sc)->if_flags & IFF_UP) &&
71013f4c340SRobert Watson 	    (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING))) {
7116bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badvhid);
712d220759bSGleb Smirnoff 		CARP_UNLOCK(ifp->if_carp);
713a9771948SGleb Smirnoff 		m_freem(m);
714a9771948SGleb Smirnoff 		return;
715a9771948SGleb Smirnoff 	}
716a9771948SGleb Smirnoff 
717fc74a9f9SBrooks Davis 	getmicrotime(&SC2IFP(sc)->if_lastchange);
718fc74a9f9SBrooks Davis 	SC2IFP(sc)->if_ipackets++;
719fc74a9f9SBrooks Davis 	SC2IFP(sc)->if_ibytes += m->m_pkthdr.len;
720a9771948SGleb Smirnoff 
72116d878ccSChristian S.J. Peron 	if (bpf_peers_present(SC2IFP(sc)->if_bpf)) {
722a9771948SGleb Smirnoff 		struct ip *ip = mtod(m, struct ip *);
7233e07def4SGleb Smirnoff 		uint32_t af1 = af;
724a9771948SGleb Smirnoff 
725a9771948SGleb Smirnoff 		/* BPF wants net byte order */
7263e07def4SGleb Smirnoff 		ip->ip_len = htons(ip->ip_len + (ip->ip_hl << 2));
7273e07def4SGleb Smirnoff 		ip->ip_off = htons(ip->ip_off);
728fc74a9f9SBrooks Davis 		bpf_mtap2(SC2IFP(sc)->if_bpf, &af1, sizeof(af1), m);
729a9771948SGleb Smirnoff 	}
730a9771948SGleb Smirnoff 
731a9771948SGleb Smirnoff 	/* verify the CARP version. */
732a9771948SGleb Smirnoff 	if (ch->carp_version != CARP_VERSION) {
7336bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badver);
734fc74a9f9SBrooks Davis 		SC2IFP(sc)->if_ierrors++;
735d220759bSGleb Smirnoff 		CARP_UNLOCK(ifp->if_carp);
736e81ab876SGleb Smirnoff 		CARP_DEBUG("%s; invalid version %d\n",
737fc74a9f9SBrooks Davis 		    SC2IFP(sc)->if_xname,
7381e9e6572SGleb Smirnoff 		    ch->carp_version);
739a9771948SGleb Smirnoff 		m_freem(m);
740a9771948SGleb Smirnoff 		return;
741a9771948SGleb Smirnoff 	}
742a9771948SGleb Smirnoff 
743a9771948SGleb Smirnoff 	/* verify the hash */
744a9771948SGleb Smirnoff 	if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
7456bf65bcfSRobert Watson 		CARPSTATS_INC(carps_badauth);
746fc74a9f9SBrooks Davis 		SC2IFP(sc)->if_ierrors++;
747d220759bSGleb Smirnoff 		CARP_UNLOCK(ifp->if_carp);
748acc0fee0SRuslan Ermilov 		CARP_DEBUG("%s: incorrect hash\n", SC2IFP(sc)->if_xname);
749a9771948SGleb Smirnoff 		m_freem(m);
750a9771948SGleb Smirnoff 		return;
751a9771948SGleb Smirnoff 	}
752a9771948SGleb Smirnoff 
753a9771948SGleb Smirnoff 	tmp_counter = ntohl(ch->carp_counter[0]);
754a9771948SGleb Smirnoff 	tmp_counter = tmp_counter<<32;
755a9771948SGleb Smirnoff 	tmp_counter += ntohl(ch->carp_counter[1]);
756a9771948SGleb Smirnoff 
757a9771948SGleb Smirnoff 	/* XXX Replay protection goes here */
758a9771948SGleb Smirnoff 
759a9771948SGleb Smirnoff 	sc->sc_init_counter = 0;
760a9771948SGleb Smirnoff 	sc->sc_counter = tmp_counter;
761a9771948SGleb Smirnoff 
762a9771948SGleb Smirnoff 	sc_tv.tv_sec = sc->sc_advbase;
763a9771948SGleb Smirnoff 	if (carp_suppress_preempt && sc->sc_advskew <  240)
764a9771948SGleb Smirnoff 		sc_tv.tv_usec = 240 * 1000000 / 256;
765a9771948SGleb Smirnoff 	else
766a9771948SGleb Smirnoff 		sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256;
767a9771948SGleb Smirnoff 	ch_tv.tv_sec = ch->carp_advbase;
768a9771948SGleb Smirnoff 	ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
769a9771948SGleb Smirnoff 
770a9771948SGleb Smirnoff 	switch (sc->sc_state) {
771a9771948SGleb Smirnoff 	case INIT:
772a9771948SGleb Smirnoff 		break;
773a9771948SGleb Smirnoff 	case MASTER:
774a9771948SGleb Smirnoff 		/*
775a9771948SGleb Smirnoff 		 * If we receive an advertisement from a master who's going to
776a9771948SGleb Smirnoff 		 * be more frequent than us, go into BACKUP state.
777a9771948SGleb Smirnoff 		 */
778a9771948SGleb Smirnoff 		if (timevalcmp(&sc_tv, &ch_tv, >) ||
779a9771948SGleb Smirnoff 		    timevalcmp(&sc_tv, &ch_tv, ==)) {
780a9771948SGleb Smirnoff 			callout_stop(&sc->sc_ad_tmo);
781e81ab876SGleb Smirnoff 			CARP_LOG("%s: MASTER -> BACKUP "
78288bf82a6SGleb Smirnoff 			   "(more frequent advertisement received)\n",
783fc74a9f9SBrooks Davis 			   SC2IFP(sc)->if_xname);
784a9771948SGleb Smirnoff 			carp_set_state(sc, BACKUP);
785a9771948SGleb Smirnoff 			carp_setrun(sc, 0);
786a9771948SGleb Smirnoff 			carp_setroute(sc, RTM_DELETE);
787a9771948SGleb Smirnoff 		}
788a9771948SGleb Smirnoff 		break;
789a9771948SGleb Smirnoff 	case BACKUP:
790a9771948SGleb Smirnoff 		/*
791a9771948SGleb Smirnoff 		 * If we're pre-empting masters who advertise slower than us,
792a9771948SGleb Smirnoff 		 * and this one claims to be slower, treat him as down.
793a9771948SGleb Smirnoff 		 */
794a9771948SGleb Smirnoff 		if (carp_opts[CARPCTL_PREEMPT] &&
795a9771948SGleb Smirnoff 		    timevalcmp(&sc_tv, &ch_tv, <)) {
796e81ab876SGleb Smirnoff 			CARP_LOG("%s: BACKUP -> MASTER "
79788bf82a6SGleb Smirnoff 			    "(preempting a slower master)\n",
798fc74a9f9SBrooks Davis 			    SC2IFP(sc)->if_xname);
799d220759bSGleb Smirnoff 			carp_master_down_locked(sc);
800a9771948SGleb Smirnoff 			break;
801a9771948SGleb Smirnoff 		}
802a9771948SGleb Smirnoff 
803a9771948SGleb Smirnoff 		/*
804a9771948SGleb Smirnoff 		 *  If the master is going to advertise at such a low frequency
805a9771948SGleb Smirnoff 		 *  that he's guaranteed to time out, we'd might as well just
806a9771948SGleb Smirnoff 		 *  treat him as timed out now.
807a9771948SGleb Smirnoff 		 */
808a9771948SGleb Smirnoff 		sc_tv.tv_sec = sc->sc_advbase * 3;
809a9771948SGleb Smirnoff 		if (timevalcmp(&sc_tv, &ch_tv, <)) {
810e81ab876SGleb Smirnoff 			CARP_LOG("%s: BACKUP -> MASTER "
81188bf82a6SGleb Smirnoff 			    "(master timed out)\n",
812fc74a9f9SBrooks Davis 			    SC2IFP(sc)->if_xname);
813d220759bSGleb Smirnoff 			carp_master_down_locked(sc);
814a9771948SGleb Smirnoff 			break;
815a9771948SGleb Smirnoff 		}
816a9771948SGleb Smirnoff 
817a9771948SGleb Smirnoff 		/*
818a9771948SGleb Smirnoff 		 * Otherwise, we reset the counter and wait for the next
819a9771948SGleb Smirnoff 		 * advertisement.
820a9771948SGleb Smirnoff 		 */
821a9771948SGleb Smirnoff 		carp_setrun(sc, af);
822a9771948SGleb Smirnoff 		break;
823a9771948SGleb Smirnoff 	}
824a9771948SGleb Smirnoff 
825d220759bSGleb Smirnoff 	CARP_UNLOCK(ifp->if_carp);
826d220759bSGleb Smirnoff 
827a9771948SGleb Smirnoff 	m_freem(m);
828a9771948SGleb Smirnoff 	return;
829a9771948SGleb Smirnoff }
830a9771948SGleb Smirnoff 
8315c1f0f6dSGleb Smirnoff static int
832a9771948SGleb Smirnoff carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
833a9771948SGleb Smirnoff {
834a9771948SGleb Smirnoff 	struct m_tag *mtag;
835fc74a9f9SBrooks Davis 	struct ifnet *ifp = SC2IFP(sc);
836a9771948SGleb Smirnoff 
837a9771948SGleb Smirnoff 	if (sc->sc_init_counter) {
838a9771948SGleb Smirnoff 		/* this could also be seconds since unix epoch */
839a9771948SGleb Smirnoff 		sc->sc_counter = arc4random();
840a9771948SGleb Smirnoff 		sc->sc_counter = sc->sc_counter << 32;
841a9771948SGleb Smirnoff 		sc->sc_counter += arc4random();
842a9771948SGleb Smirnoff 	} else
843a9771948SGleb Smirnoff 		sc->sc_counter++;
844a9771948SGleb Smirnoff 
845a9771948SGleb Smirnoff 	ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
846a9771948SGleb Smirnoff 	ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
847a9771948SGleb Smirnoff 
848a9771948SGleb Smirnoff 	carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
849a9771948SGleb Smirnoff 
850a9771948SGleb Smirnoff 	/* Tag packet for carp_output */
851a9771948SGleb Smirnoff 	mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct ifnet *), M_NOWAIT);
852a9771948SGleb Smirnoff 	if (mtag == NULL) {
853a9771948SGleb Smirnoff 		m_freem(m);
854fc74a9f9SBrooks Davis 		SC2IFP(sc)->if_oerrors++;
855a9771948SGleb Smirnoff 		return (ENOMEM);
856a9771948SGleb Smirnoff 	}
857a9771948SGleb Smirnoff 	bcopy(&ifp, (caddr_t)(mtag + 1), sizeof(struct ifnet *));
858a9771948SGleb Smirnoff 	m_tag_prepend(m, mtag);
859a9771948SGleb Smirnoff 
860a9771948SGleb Smirnoff 	return (0);
861a9771948SGleb Smirnoff }
862a9771948SGleb Smirnoff 
8635c1f0f6dSGleb Smirnoff static void
864a9771948SGleb Smirnoff carp_send_ad_all(void)
865a9771948SGleb Smirnoff {
866d92d54d5SGleb Smirnoff 	struct carp_softc *sc;
867a9771948SGleb Smirnoff 
868d92d54d5SGleb Smirnoff 	mtx_lock(&carp_mtx);
869d92d54d5SGleb Smirnoff 	LIST_FOREACH(sc, &carpif_list, sc_next) {
870d92d54d5SGleb Smirnoff 		if (sc->sc_carpdev == NULL)
871a9771948SGleb Smirnoff 			continue;
872d92d54d5SGleb Smirnoff 		CARP_SCLOCK(sc);
87313f4c340SRobert Watson 		if ((SC2IFP(sc)->if_flags & IFF_UP) &&
87413f4c340SRobert Watson 		    (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING) &&
875d92d54d5SGleb Smirnoff 		     sc->sc_state == MASTER)
876d220759bSGleb Smirnoff 			carp_send_ad_locked(sc);
877d92d54d5SGleb Smirnoff 		CARP_SCUNLOCK(sc);
878a9771948SGleb Smirnoff 	}
879d92d54d5SGleb Smirnoff 	mtx_unlock(&carp_mtx);
880a9771948SGleb Smirnoff }
881a9771948SGleb Smirnoff 
8825c1f0f6dSGleb Smirnoff static void
883a9771948SGleb Smirnoff carp_send_ad(void *v)
884a9771948SGleb Smirnoff {
885d220759bSGleb Smirnoff 	struct carp_softc *sc = v;
886d220759bSGleb Smirnoff 
887d220759bSGleb Smirnoff 	CARP_SCLOCK(sc);
888d220759bSGleb Smirnoff 	carp_send_ad_locked(sc);
889d220759bSGleb Smirnoff 	CARP_SCUNLOCK(sc);
890d220759bSGleb Smirnoff }
891d220759bSGleb Smirnoff 
892d220759bSGleb Smirnoff static void
893d220759bSGleb Smirnoff carp_send_ad_locked(struct carp_softc *sc)
894d220759bSGleb Smirnoff {
895a9771948SGleb Smirnoff 	struct carp_header ch;
896a9771948SGleb Smirnoff 	struct timeval tv;
897a9771948SGleb Smirnoff 	struct carp_header *ch_ptr;
898a9771948SGleb Smirnoff 	struct mbuf *m;
899a9771948SGleb Smirnoff 	int len, advbase, advskew;
900a9771948SGleb Smirnoff 
901d220759bSGleb Smirnoff 	CARP_SCLOCK_ASSERT(sc);
902d220759bSGleb Smirnoff 
903a9771948SGleb Smirnoff 	/* bow out if we've lost our UPness or RUNNINGuiness */
90413f4c340SRobert Watson 	if (!((SC2IFP(sc)->if_flags & IFF_UP) &&
90513f4c340SRobert Watson 	    (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING))) {
906a9771948SGleb Smirnoff 		advbase = 255;
907a9771948SGleb Smirnoff 		advskew = 255;
908a9771948SGleb Smirnoff 	} else {
909a9771948SGleb Smirnoff 		advbase = sc->sc_advbase;
910a9771948SGleb Smirnoff 		if (!carp_suppress_preempt || sc->sc_advskew > 240)
911a9771948SGleb Smirnoff 			advskew = sc->sc_advskew;
912a9771948SGleb Smirnoff 		else
913a9771948SGleb Smirnoff 			advskew = 240;
914a9771948SGleb Smirnoff 		tv.tv_sec = advbase;
915a9771948SGleb Smirnoff 		tv.tv_usec = advskew * 1000000 / 256;
916a9771948SGleb Smirnoff 	}
917a9771948SGleb Smirnoff 
918a9771948SGleb Smirnoff 	ch.carp_version = CARP_VERSION;
919a9771948SGleb Smirnoff 	ch.carp_type = CARP_ADVERTISEMENT;
920a9771948SGleb Smirnoff 	ch.carp_vhid = sc->sc_vhid;
921a9771948SGleb Smirnoff 	ch.carp_advbase = advbase;
922a9771948SGleb Smirnoff 	ch.carp_advskew = advskew;
923a9771948SGleb Smirnoff 	ch.carp_authlen = 7;	/* XXX DEFINE */
924a9771948SGleb Smirnoff 	ch.carp_pad1 = 0;	/* must be zero */
925a9771948SGleb Smirnoff 	ch.carp_cksum = 0;
926a9771948SGleb Smirnoff 
927a9771948SGleb Smirnoff #ifdef INET
928a9771948SGleb Smirnoff 	if (sc->sc_ia) {
929a9771948SGleb Smirnoff 		struct ip *ip;
930a9771948SGleb Smirnoff 
931a9771948SGleb Smirnoff 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
932a9771948SGleb Smirnoff 		if (m == NULL) {
933fc74a9f9SBrooks Davis 			SC2IFP(sc)->if_oerrors++;
9346bf65bcfSRobert Watson 			CARPSTATS_INC(carps_onomem);
935a9771948SGleb Smirnoff 			/* XXX maybe less ? */
936a9771948SGleb Smirnoff 			if (advbase != 255 || advskew != 255)
937a9771948SGleb Smirnoff 				callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
938a9771948SGleb Smirnoff 				    carp_send_ad, sc);
939a9771948SGleb Smirnoff 			return;
940a9771948SGleb Smirnoff 		}
941a9771948SGleb Smirnoff 		len = sizeof(*ip) + sizeof(ch);
942a9771948SGleb Smirnoff 		m->m_pkthdr.len = len;
943a9771948SGleb Smirnoff 		m->m_pkthdr.rcvif = NULL;
944a9771948SGleb Smirnoff 		m->m_len = len;
945a9771948SGleb Smirnoff 		MH_ALIGN(m, m->m_len);
946a9771948SGleb Smirnoff 		m->m_flags |= M_MCAST;
947a9771948SGleb Smirnoff 		ip = mtod(m, struct ip *);
948a9771948SGleb Smirnoff 		ip->ip_v = IPVERSION;
949a9771948SGleb Smirnoff 		ip->ip_hl = sizeof(*ip) >> 2;
950a9771948SGleb Smirnoff 		ip->ip_tos = IPTOS_LOWDELAY;
951a9771948SGleb Smirnoff 		ip->ip_len = len;
952a9771948SGleb Smirnoff 		ip->ip_id = ip_newid();
953a9771948SGleb Smirnoff 		ip->ip_off = IP_DF;
954a9771948SGleb Smirnoff 		ip->ip_ttl = CARP_DFLTTL;
955a9771948SGleb Smirnoff 		ip->ip_p = IPPROTO_CARP;
956a9771948SGleb Smirnoff 		ip->ip_sum = 0;
957a9771948SGleb Smirnoff 		ip->ip_src.s_addr = sc->sc_ia->ia_addr.sin_addr.s_addr;
958a9771948SGleb Smirnoff 		ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
959a9771948SGleb Smirnoff 
960a9771948SGleb Smirnoff 		ch_ptr = (struct carp_header *)(&ip[1]);
961a9771948SGleb Smirnoff 		bcopy(&ch, ch_ptr, sizeof(ch));
962a9771948SGleb Smirnoff 		if (carp_prepare_ad(m, sc, ch_ptr))
963a9771948SGleb Smirnoff 			return;
964a9771948SGleb Smirnoff 
965a9771948SGleb Smirnoff 		m->m_data += sizeof(*ip);
966a9771948SGleb Smirnoff 		ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip));
967a9771948SGleb Smirnoff 		m->m_data -= sizeof(*ip);
968a9771948SGleb Smirnoff 
969fc74a9f9SBrooks Davis 		getmicrotime(&SC2IFP(sc)->if_lastchange);
970fc74a9f9SBrooks Davis 		SC2IFP(sc)->if_opackets++;
971fc74a9f9SBrooks Davis 		SC2IFP(sc)->if_obytes += len;
9726bf65bcfSRobert Watson 		CARPSTATS_INC(carps_opackets);
973a9771948SGleb Smirnoff 
974a9771948SGleb Smirnoff 		if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) {
975fc74a9f9SBrooks Davis 			SC2IFP(sc)->if_oerrors++;
976a9771948SGleb Smirnoff 			if (sc->sc_sendad_errors < INT_MAX)
977a9771948SGleb Smirnoff 				sc->sc_sendad_errors++;
978a9771948SGleb Smirnoff 			if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
979a9771948SGleb Smirnoff 				carp_suppress_preempt++;
980d220759bSGleb Smirnoff 				if (carp_suppress_preempt == 1) {
981d220759bSGleb Smirnoff 					CARP_SCUNLOCK(sc);
982a9771948SGleb Smirnoff 					carp_send_ad_all();
983d220759bSGleb Smirnoff 					CARP_SCLOCK(sc);
984d220759bSGleb Smirnoff 				}
985a9771948SGleb Smirnoff 			}
986a9771948SGleb Smirnoff 			sc->sc_sendad_success = 0;
987a9771948SGleb Smirnoff 		} else {
988a9771948SGleb Smirnoff 			if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
989a9771948SGleb Smirnoff 				if (++sc->sc_sendad_success >=
990a9771948SGleb Smirnoff 				    CARP_SENDAD_MIN_SUCCESS) {
991a9771948SGleb Smirnoff 					carp_suppress_preempt--;
992a9771948SGleb Smirnoff 					sc->sc_sendad_errors = 0;
993a9771948SGleb Smirnoff 				}
994a9771948SGleb Smirnoff 			} else
995a9771948SGleb Smirnoff 				sc->sc_sendad_errors = 0;
996a9771948SGleb Smirnoff 		}
997a9771948SGleb Smirnoff 	}
998a9771948SGleb Smirnoff #endif /* INET */
999a9771948SGleb Smirnoff #ifdef INET6
1000a9771948SGleb Smirnoff 	if (sc->sc_ia6) {
1001a9771948SGleb Smirnoff 		struct ip6_hdr *ip6;
1002a9771948SGleb Smirnoff 
1003a9771948SGleb Smirnoff 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
1004a9771948SGleb Smirnoff 		if (m == NULL) {
1005fc74a9f9SBrooks Davis 			SC2IFP(sc)->if_oerrors++;
10066bf65bcfSRobert Watson 			CARPSTATS_INC(carps_onomem);
1007a9771948SGleb Smirnoff 			/* XXX maybe less ? */
1008a9771948SGleb Smirnoff 			if (advbase != 255 || advskew != 255)
1009a9771948SGleb Smirnoff 				callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1010a9771948SGleb Smirnoff 				    carp_send_ad, sc);
1011a9771948SGleb Smirnoff 			return;
1012a9771948SGleb Smirnoff 		}
1013a9771948SGleb Smirnoff 		len = sizeof(*ip6) + sizeof(ch);
1014a9771948SGleb Smirnoff 		m->m_pkthdr.len = len;
1015a9771948SGleb Smirnoff 		m->m_pkthdr.rcvif = NULL;
1016a9771948SGleb Smirnoff 		m->m_len = len;
1017a9771948SGleb Smirnoff 		MH_ALIGN(m, m->m_len);
1018a9771948SGleb Smirnoff 		m->m_flags |= M_MCAST;
1019a9771948SGleb Smirnoff 		ip6 = mtod(m, struct ip6_hdr *);
1020a9771948SGleb Smirnoff 		bzero(ip6, sizeof(*ip6));
1021a9771948SGleb Smirnoff 		ip6->ip6_vfc |= IPV6_VERSION;
1022a9771948SGleb Smirnoff 		ip6->ip6_hlim = CARP_DFLTTL;
1023a9771948SGleb Smirnoff 		ip6->ip6_nxt = IPPROTO_CARP;
1024a9771948SGleb Smirnoff 		bcopy(&sc->sc_ia6->ia_addr.sin6_addr, &ip6->ip6_src,
1025a9771948SGleb Smirnoff 		    sizeof(struct in6_addr));
1026a9771948SGleb Smirnoff 		/* set the multicast destination */
1027a9771948SGleb Smirnoff 
10287002145dSBjoern A. Zeeb 		ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
1029a9771948SGleb Smirnoff 		ip6->ip6_dst.s6_addr8[15] = 0x12;
10307002145dSBjoern A. Zeeb 		if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
10317002145dSBjoern A. Zeeb 			SC2IFP(sc)->if_oerrors++;
10327002145dSBjoern A. Zeeb 			m_freem(m);
1033e81ab876SGleb Smirnoff 			CARP_DEBUG("%s: in6_setscope failed\n", __func__);
10347002145dSBjoern A. Zeeb 			return;
10357002145dSBjoern A. Zeeb 		}
1036a9771948SGleb Smirnoff 
1037a9771948SGleb Smirnoff 		ch_ptr = (struct carp_header *)(&ip6[1]);
1038a9771948SGleb Smirnoff 		bcopy(&ch, ch_ptr, sizeof(ch));
1039a9771948SGleb Smirnoff 		if (carp_prepare_ad(m, sc, ch_ptr))
1040a9771948SGleb Smirnoff 			return;
1041a9771948SGleb Smirnoff 
1042a9771948SGleb Smirnoff 		m->m_data += sizeof(*ip6);
1043a9771948SGleb Smirnoff 		ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip6));
1044a9771948SGleb Smirnoff 		m->m_data -= sizeof(*ip6);
1045a9771948SGleb Smirnoff 
1046fc74a9f9SBrooks Davis 		getmicrotime(&SC2IFP(sc)->if_lastchange);
1047fc74a9f9SBrooks Davis 		SC2IFP(sc)->if_opackets++;
1048fc74a9f9SBrooks Davis 		SC2IFP(sc)->if_obytes += len;
10496bf65bcfSRobert Watson 		CARPSTATS_INC(carps_opackets6);
1050a9771948SGleb Smirnoff 
1051a9771948SGleb Smirnoff 		if (ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL)) {
1052fc74a9f9SBrooks Davis 			SC2IFP(sc)->if_oerrors++;
1053a9771948SGleb Smirnoff 			if (sc->sc_sendad_errors < INT_MAX)
1054a9771948SGleb Smirnoff 				sc->sc_sendad_errors++;
1055a9771948SGleb Smirnoff 			if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
1056a9771948SGleb Smirnoff 				carp_suppress_preempt++;
1057d220759bSGleb Smirnoff 				if (carp_suppress_preempt == 1) {
1058d220759bSGleb Smirnoff 					CARP_SCUNLOCK(sc);
1059a9771948SGleb Smirnoff 					carp_send_ad_all();
1060d220759bSGleb Smirnoff 					CARP_SCLOCK(sc);
1061d220759bSGleb Smirnoff 				}
1062a9771948SGleb Smirnoff 			}
1063a9771948SGleb Smirnoff 			sc->sc_sendad_success = 0;
1064a9771948SGleb Smirnoff 		} else {
1065a9771948SGleb Smirnoff 			if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
1066a9771948SGleb Smirnoff 				if (++sc->sc_sendad_success >=
1067a9771948SGleb Smirnoff 				    CARP_SENDAD_MIN_SUCCESS) {
1068a9771948SGleb Smirnoff 					carp_suppress_preempt--;
1069a9771948SGleb Smirnoff 					sc->sc_sendad_errors = 0;
1070a9771948SGleb Smirnoff 				}
1071a9771948SGleb Smirnoff 			} else
1072a9771948SGleb Smirnoff 				sc->sc_sendad_errors = 0;
1073a9771948SGleb Smirnoff 		}
1074a9771948SGleb Smirnoff 	}
1075a9771948SGleb Smirnoff #endif /* INET6 */
1076a9771948SGleb Smirnoff 
1077a9771948SGleb Smirnoff 	if (advbase != 255 || advskew != 255)
1078a9771948SGleb Smirnoff 		callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1079a9771948SGleb Smirnoff 		    carp_send_ad, sc);
1080a9771948SGleb Smirnoff 
1081a9771948SGleb Smirnoff }
1082a9771948SGleb Smirnoff 
1083a9771948SGleb Smirnoff /*
1084a9771948SGleb Smirnoff  * Broadcast a gratuitous ARP request containing
1085a9771948SGleb Smirnoff  * the virtual router MAC address for each IP address
1086a9771948SGleb Smirnoff  * associated with the virtual router.
1087a9771948SGleb Smirnoff  */
10885c1f0f6dSGleb Smirnoff static void
1089a9771948SGleb Smirnoff carp_send_arp(struct carp_softc *sc)
1090a9771948SGleb Smirnoff {
1091a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1092a9771948SGleb Smirnoff 
1093fc74a9f9SBrooks Davis 	TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1094a9771948SGleb Smirnoff 
1095a9771948SGleb Smirnoff 		if (ifa->ifa_addr->sa_family != AF_INET)
1096a9771948SGleb Smirnoff 			continue;
1097a9771948SGleb Smirnoff 
10984a0d6638SRuslan Ermilov /*		arprequest(sc->sc_carpdev, &in, &in, IF_LLADDR(sc->sc_ifp)); */
10994a0d6638SRuslan Ermilov 		arp_ifinit2(sc->sc_carpdev, ifa, IF_LLADDR(sc->sc_ifp));
1100a9771948SGleb Smirnoff 
1101a9771948SGleb Smirnoff 		DELAY(1000);	/* XXX */
1102a9771948SGleb Smirnoff 	}
1103a9771948SGleb Smirnoff }
1104a9771948SGleb Smirnoff 
1105a9771948SGleb Smirnoff #ifdef INET6
11065c1f0f6dSGleb Smirnoff static void
1107a9771948SGleb Smirnoff carp_send_na(struct carp_softc *sc)
1108a9771948SGleb Smirnoff {
1109a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1110a9771948SGleb Smirnoff 	struct in6_addr *in6;
1111a9771948SGleb Smirnoff 	static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1112a9771948SGleb Smirnoff 
1113fc74a9f9SBrooks Davis 	TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1114a9771948SGleb Smirnoff 
1115a9771948SGleb Smirnoff 		if (ifa->ifa_addr->sa_family != AF_INET6)
1116a9771948SGleb Smirnoff 			continue;
1117a9771948SGleb Smirnoff 
1118a9771948SGleb Smirnoff 		in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
1119e8c34a71SGleb Smirnoff 		nd6_na_output(sc->sc_carpdev, &mcast, in6,
1120a9771948SGleb Smirnoff 		    ND_NA_FLAG_OVERRIDE, 1, NULL);
1121a9771948SGleb Smirnoff 		DELAY(1000);	/* XXX */
1122a9771948SGleb Smirnoff 	}
1123a9771948SGleb Smirnoff }
1124a9771948SGleb Smirnoff #endif /* INET6 */
1125a9771948SGleb Smirnoff 
11265c1f0f6dSGleb Smirnoff static int
1127a9771948SGleb Smirnoff carp_addrcount(struct carp_if *cif, struct in_ifaddr *ia, int type)
1128a9771948SGleb Smirnoff {
1129a9771948SGleb Smirnoff 	struct carp_softc *vh;
1130a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1131a9771948SGleb Smirnoff 	int count = 0;
1132a9771948SGleb Smirnoff 
1133d220759bSGleb Smirnoff 	CARP_LOCK_ASSERT(cif);
1134d220759bSGleb Smirnoff 
1135a9771948SGleb Smirnoff 	TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1136a9771948SGleb Smirnoff 		if ((type == CARP_COUNT_RUNNING &&
113713f4c340SRobert Watson 		    (SC2IFP(vh)->if_flags & IFF_UP) &&
113813f4c340SRobert Watson 		    (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING)) ||
1139a9771948SGleb Smirnoff 		    (type == CARP_COUNT_MASTER && vh->sc_state == MASTER)) {
1140db091502SRobert Watson 			IF_ADDR_LOCK(SC2IFP(vh));
1141fc74a9f9SBrooks Davis 			TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist,
1142a9771948SGleb Smirnoff 			    ifa_list) {
1143a9771948SGleb Smirnoff 				if (ifa->ifa_addr->sa_family == AF_INET &&
1144a9771948SGleb Smirnoff 				    ia->ia_addr.sin_addr.s_addr ==
1145a9771948SGleb Smirnoff 				    ifatoia(ifa)->ia_addr.sin_addr.s_addr)
1146a9771948SGleb Smirnoff 					count++;
1147a9771948SGleb Smirnoff 			}
1148db091502SRobert Watson 			IF_ADDR_UNLOCK(SC2IFP(vh));
1149a9771948SGleb Smirnoff 		}
1150a9771948SGleb Smirnoff 	}
1151a9771948SGleb Smirnoff 	return (count);
1152a9771948SGleb Smirnoff }
1153a9771948SGleb Smirnoff 
1154a9771948SGleb Smirnoff int
115554bfbd51SWill Andrews carp_iamatch(struct ifnet *ifp, struct in_ifaddr *ia,
1156a9771948SGleb Smirnoff     struct in_addr *isaddr, u_int8_t **enaddr)
1157a9771948SGleb Smirnoff {
115854bfbd51SWill Andrews 	struct carp_if *cif;
1159a9771948SGleb Smirnoff 	struct carp_softc *vh;
1160a9771948SGleb Smirnoff 	int index, count = 0;
1161a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1162a9771948SGleb Smirnoff 
116354bfbd51SWill Andrews 	cif = ifp->if_carp;
1164a9771948SGleb Smirnoff 	CARP_LOCK(cif);
1165a9771948SGleb Smirnoff 
1166a9771948SGleb Smirnoff 	if (carp_opts[CARPCTL_ARPBALANCE]) {
1167a9771948SGleb Smirnoff 		/*
1168a9771948SGleb Smirnoff 		 * XXX proof of concept implementation.
1169a9771948SGleb Smirnoff 		 * We use the source ip to decide which virtual host should
1170a9771948SGleb Smirnoff 		 * handle the request. If we're master of that virtual host,
1171a9771948SGleb Smirnoff 		 * then we respond, otherwise, just drop the arp packet on
1172a9771948SGleb Smirnoff 		 * the floor.
1173a9771948SGleb Smirnoff 		 */
1174a9771948SGleb Smirnoff 		count = carp_addrcount(cif, ia, CARP_COUNT_RUNNING);
1175a9771948SGleb Smirnoff 		if (count == 0) {
1176a9771948SGleb Smirnoff 			/* should never reach this */
1177a9771948SGleb Smirnoff 			CARP_UNLOCK(cif);
1178a9771948SGleb Smirnoff 			return (0);
1179a9771948SGleb Smirnoff 		}
1180a9771948SGleb Smirnoff 
1181a9771948SGleb Smirnoff 		/* this should be a hash, like pf_hash() */
1182a196a3c8SGleb Smirnoff 		index = ntohl(isaddr->s_addr) % count;
1183a9771948SGleb Smirnoff 		count = 0;
1184a9771948SGleb Smirnoff 
1185a9771948SGleb Smirnoff 		TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
118613f4c340SRobert Watson 			if ((SC2IFP(vh)->if_flags & IFF_UP) &&
118713f4c340SRobert Watson 			    (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING)) {
1188db091502SRobert Watson 				IF_ADDR_LOCK(SC2IFP(vh));
1189fc74a9f9SBrooks Davis 				TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist,
1190a9771948SGleb Smirnoff 				    ifa_list) {
1191a9771948SGleb Smirnoff 					if (ifa->ifa_addr->sa_family ==
1192a9771948SGleb Smirnoff 					    AF_INET &&
1193a9771948SGleb Smirnoff 					    ia->ia_addr.sin_addr.s_addr ==
1194a9771948SGleb Smirnoff 					    ifatoia(ifa)->ia_addr.sin_addr.s_addr) {
1195a9771948SGleb Smirnoff 						if (count == index) {
1196a9771948SGleb Smirnoff 							if (vh->sc_state ==
1197a9771948SGleb Smirnoff 							    MASTER) {
11984a0d6638SRuslan Ermilov 								*enaddr = IF_LLADDR(vh->sc_ifp);
1199db091502SRobert Watson 								IF_ADDR_UNLOCK(SC2IFP(vh));
1200a9771948SGleb Smirnoff 								CARP_UNLOCK(cif);
1201a9771948SGleb Smirnoff 								return (1);
1202a9771948SGleb Smirnoff 							} else {
1203db091502SRobert Watson 								IF_ADDR_UNLOCK(SC2IFP(vh));
1204a9771948SGleb Smirnoff 								CARP_UNLOCK(cif);
1205a9771948SGleb Smirnoff 								return (0);
1206a9771948SGleb Smirnoff 							}
1207a9771948SGleb Smirnoff 						}
1208a9771948SGleb Smirnoff 						count++;
1209a9771948SGleb Smirnoff 					}
1210a9771948SGleb Smirnoff 				}
1211db091502SRobert Watson 				IF_ADDR_UNLOCK(SC2IFP(vh));
1212a9771948SGleb Smirnoff 			}
1213a9771948SGleb Smirnoff 		}
1214a9771948SGleb Smirnoff 	} else {
1215a9771948SGleb Smirnoff 		TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
121613f4c340SRobert Watson 			if ((SC2IFP(vh)->if_flags & IFF_UP) &&
121713f4c340SRobert Watson 			    (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
121821883761SGleb Smirnoff 			    ia->ia_ifp == SC2IFP(vh) &&
121921883761SGleb Smirnoff 			    vh->sc_state == MASTER) {
12204a0d6638SRuslan Ermilov 				*enaddr = IF_LLADDR(vh->sc_ifp);
1221a9771948SGleb Smirnoff 				CARP_UNLOCK(cif);
1222a9771948SGleb Smirnoff 				return (1);
1223a9771948SGleb Smirnoff 			}
1224a9771948SGleb Smirnoff 		}
1225a9771948SGleb Smirnoff 	}
1226a9771948SGleb Smirnoff 	CARP_UNLOCK(cif);
1227a9771948SGleb Smirnoff 	return (0);
1228a9771948SGleb Smirnoff }
1229a9771948SGleb Smirnoff 
1230a9771948SGleb Smirnoff #ifdef INET6
1231a4e53905SMax Laier struct ifaddr *
123254bfbd51SWill Andrews carp_iamatch6(struct ifnet *ifp, struct in6_addr *taddr)
1233a9771948SGleb Smirnoff {
123454bfbd51SWill Andrews 	struct carp_if *cif;
1235a9771948SGleb Smirnoff 	struct carp_softc *vh;
1236a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1237a9771948SGleb Smirnoff 
123854bfbd51SWill Andrews 	cif = ifp->if_carp;
1239a9771948SGleb Smirnoff 	CARP_LOCK(cif);
1240a9771948SGleb Smirnoff 	TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1241db091502SRobert Watson 		IF_ADDR_LOCK(SC2IFP(vh));
1242fc74a9f9SBrooks Davis 		TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist, ifa_list) {
1243a9771948SGleb Smirnoff 			if (IN6_ARE_ADDR_EQUAL(taddr,
1244a9771948SGleb Smirnoff 			    &ifatoia6(ifa)->ia_addr.sin6_addr) &&
124513f4c340SRobert Watson  			    (SC2IFP(vh)->if_flags & IFF_UP) &&
124621883761SGleb Smirnoff 			    (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
124721883761SGleb Smirnoff 			    vh->sc_state == MASTER) {
12488c0fec80SRobert Watson 				ifa_ref(ifa);
1249db091502SRobert Watson 				IF_ADDR_UNLOCK(SC2IFP(vh));
1250a9771948SGleb Smirnoff 			    	CARP_UNLOCK(cif);
1251a9771948SGleb Smirnoff 				return (ifa);
1252a9771948SGleb Smirnoff 			}
1253a9771948SGleb Smirnoff 		}
1254db091502SRobert Watson 		IF_ADDR_UNLOCK(SC2IFP(vh));
1255a9771948SGleb Smirnoff 	}
1256a9771948SGleb Smirnoff 	CARP_UNLOCK(cif);
1257a9771948SGleb Smirnoff 
1258a9771948SGleb Smirnoff 	return (NULL);
1259a9771948SGleb Smirnoff }
1260a9771948SGleb Smirnoff 
126154bfbd51SWill Andrews caddr_t
126254bfbd51SWill Andrews carp_macmatch6(struct ifnet *ifp, struct mbuf *m, const struct in6_addr *taddr)
1263a9771948SGleb Smirnoff {
1264a9771948SGleb Smirnoff 	struct m_tag *mtag;
126554bfbd51SWill Andrews 	struct carp_if *cif;
1266a9771948SGleb Smirnoff 	struct carp_softc *sc;
1267a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1268a9771948SGleb Smirnoff 
126954bfbd51SWill Andrews 	cif = ifp->if_carp;
1270a9771948SGleb Smirnoff 	CARP_LOCK(cif);
1271a9771948SGleb Smirnoff 	TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
1272db091502SRobert Watson 		IF_ADDR_LOCK(SC2IFP(sc));
1273fc74a9f9SBrooks Davis 		TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1274a9771948SGleb Smirnoff 			if (IN6_ARE_ADDR_EQUAL(taddr,
1275a9771948SGleb Smirnoff 			    &ifatoia6(ifa)->ia_addr.sin6_addr) &&
127613f4c340SRobert Watson  			    (SC2IFP(sc)->if_flags & IFF_UP) &&
127713f4c340SRobert Watson 			    (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING)) {
1278fc74a9f9SBrooks Davis 				struct ifnet *ifp = SC2IFP(sc);
1279a9771948SGleb Smirnoff 				mtag = m_tag_get(PACKET_TAG_CARP,
1280a9771948SGleb Smirnoff 				    sizeof(struct ifnet *), M_NOWAIT);
1281a9771948SGleb Smirnoff 				if (mtag == NULL) {
1282a9771948SGleb Smirnoff 					/* better a bit than nothing */
1283db091502SRobert Watson 					IF_ADDR_UNLOCK(SC2IFP(sc));
1284a9771948SGleb Smirnoff 					CARP_UNLOCK(cif);
12854a0d6638SRuslan Ermilov 					return (IF_LLADDR(sc->sc_ifp));
1286a9771948SGleb Smirnoff 				}
1287a9771948SGleb Smirnoff 				bcopy(&ifp, (caddr_t)(mtag + 1),
1288a9771948SGleb Smirnoff 				    sizeof(struct ifnet *));
1289a9771948SGleb Smirnoff 				m_tag_prepend(m, mtag);
1290a9771948SGleb Smirnoff 
1291db091502SRobert Watson 				IF_ADDR_UNLOCK(SC2IFP(sc));
1292a9771948SGleb Smirnoff 				CARP_UNLOCK(cif);
12934a0d6638SRuslan Ermilov 				return (IF_LLADDR(sc->sc_ifp));
1294a9771948SGleb Smirnoff 			}
1295a9771948SGleb Smirnoff 		}
1296db091502SRobert Watson 		IF_ADDR_UNLOCK(SC2IFP(sc));
1297a9771948SGleb Smirnoff 	}
1298a9771948SGleb Smirnoff 	CARP_UNLOCK(cif);
1299a9771948SGleb Smirnoff 
1300a9771948SGleb Smirnoff 	return (NULL);
1301a9771948SGleb Smirnoff }
1302a9771948SGleb Smirnoff #endif
1303a9771948SGleb Smirnoff 
1304a9771948SGleb Smirnoff struct ifnet *
130554bfbd51SWill Andrews carp_forus(struct ifnet *ifp, u_char *dhost)
1306a9771948SGleb Smirnoff {
130754bfbd51SWill Andrews 	struct carp_if *cif;
1308a9771948SGleb Smirnoff 	struct carp_softc *vh;
1309a9771948SGleb Smirnoff 	u_int8_t *ena = dhost;
1310a9771948SGleb Smirnoff 
1311a9771948SGleb Smirnoff 	if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1312a9771948SGleb Smirnoff 		return (NULL);
1313a9771948SGleb Smirnoff 
131454bfbd51SWill Andrews 	cif = ifp->if_carp;
1315a9771948SGleb Smirnoff 	CARP_LOCK(cif);
1316a9771948SGleb Smirnoff 	TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list)
131713f4c340SRobert Watson 		if ((SC2IFP(vh)->if_flags & IFF_UP) &&
131813f4c340SRobert Watson 		    (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
131913f4c340SRobert Watson 		    vh->sc_state == MASTER &&
13204a0d6638SRuslan Ermilov 		    !bcmp(dhost, IF_LLADDR(vh->sc_ifp), ETHER_ADDR_LEN)) {
1321a9771948SGleb Smirnoff 		    	CARP_UNLOCK(cif);
1322fc74a9f9SBrooks Davis 			return (SC2IFP(vh));
1323a9771948SGleb Smirnoff 		}
1324a9771948SGleb Smirnoff 
1325a9771948SGleb Smirnoff     	CARP_UNLOCK(cif);
1326a9771948SGleb Smirnoff 	return (NULL);
1327a9771948SGleb Smirnoff }
1328a9771948SGleb Smirnoff 
13295c1f0f6dSGleb Smirnoff static void
1330a9771948SGleb Smirnoff carp_master_down(void *v)
1331a9771948SGleb Smirnoff {
1332a9771948SGleb Smirnoff 	struct carp_softc *sc = v;
1333a9771948SGleb Smirnoff 
1334d220759bSGleb Smirnoff 	CARP_SCLOCK(sc);
1335d220759bSGleb Smirnoff 	carp_master_down_locked(sc);
1336d220759bSGleb Smirnoff 	CARP_SCUNLOCK(sc);
1337d220759bSGleb Smirnoff }
1338d220759bSGleb Smirnoff 
1339d220759bSGleb Smirnoff static void
1340d220759bSGleb Smirnoff carp_master_down_locked(struct carp_softc *sc)
1341d220759bSGleb Smirnoff {
1342d220759bSGleb Smirnoff 	if (sc->sc_carpdev)
1343d220759bSGleb Smirnoff 		CARP_SCLOCK_ASSERT(sc);
1344d220759bSGleb Smirnoff 
1345a9771948SGleb Smirnoff 	switch (sc->sc_state) {
1346a9771948SGleb Smirnoff 	case INIT:
1347a9771948SGleb Smirnoff 		printf("%s: master_down event in INIT state\n",
1348fc74a9f9SBrooks Davis 		    SC2IFP(sc)->if_xname);
1349a9771948SGleb Smirnoff 		break;
1350a9771948SGleb Smirnoff 	case MASTER:
1351a9771948SGleb Smirnoff 		break;
1352a9771948SGleb Smirnoff 	case BACKUP:
1353a9771948SGleb Smirnoff 		carp_set_state(sc, MASTER);
1354d220759bSGleb Smirnoff 		carp_send_ad_locked(sc);
1355a9771948SGleb Smirnoff 		carp_send_arp(sc);
1356a9771948SGleb Smirnoff #ifdef INET6
1357a9771948SGleb Smirnoff 		carp_send_na(sc);
1358a9771948SGleb Smirnoff #endif /* INET6 */
1359a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
1360a9771948SGleb Smirnoff 		carp_setroute(sc, RTM_ADD);
1361a9771948SGleb Smirnoff 		break;
1362a9771948SGleb Smirnoff 	}
1363a9771948SGleb Smirnoff }
1364a9771948SGleb Smirnoff 
1365a9771948SGleb Smirnoff /*
1366a9771948SGleb Smirnoff  * When in backup state, af indicates whether to reset the master down timer
1367a9771948SGleb Smirnoff  * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1368a9771948SGleb Smirnoff  */
13695c1f0f6dSGleb Smirnoff static void
1370a9771948SGleb Smirnoff carp_setrun(struct carp_softc *sc, sa_family_t af)
1371a9771948SGleb Smirnoff {
1372a9771948SGleb Smirnoff 	struct timeval tv;
1373a9771948SGleb Smirnoff 
13740fa08018SGleb Smirnoff 	if (sc->sc_carpdev == NULL) {
13750fa08018SGleb Smirnoff 		SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
13760fa08018SGleb Smirnoff 		carp_set_state(sc, INIT);
13770fa08018SGleb Smirnoff 		return;
13780fa08018SGleb Smirnoff 	} else
1379d220759bSGleb Smirnoff 		CARP_SCLOCK_ASSERT(sc);
1380d220759bSGleb Smirnoff 
1381fc74a9f9SBrooks Davis 	if (SC2IFP(sc)->if_flags & IFF_UP &&
13829fe5092dSXin LI 	    sc->sc_vhid > 0 && (sc->sc_naddrs || sc->sc_naddrs6) &&
13839fe5092dSXin LI 	    sc->sc_carpdev->if_link_state == LINK_STATE_UP)
138413f4c340SRobert Watson 		SC2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
1385a9771948SGleb Smirnoff 	else {
138613f4c340SRobert Watson 		SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1387a9771948SGleb Smirnoff 		carp_setroute(sc, RTM_DELETE);
1388a9771948SGleb Smirnoff 		return;
1389a9771948SGleb Smirnoff 	}
1390a9771948SGleb Smirnoff 
1391a9771948SGleb Smirnoff 	switch (sc->sc_state) {
1392a9771948SGleb Smirnoff 	case INIT:
1393a9771948SGleb Smirnoff 		if (carp_opts[CARPCTL_PREEMPT] && !carp_suppress_preempt) {
1394d220759bSGleb Smirnoff 			carp_send_ad_locked(sc);
1395a9771948SGleb Smirnoff 			carp_send_arp(sc);
1396a9771948SGleb Smirnoff #ifdef INET6
1397a9771948SGleb Smirnoff 			carp_send_na(sc);
1398a9771948SGleb Smirnoff #endif /* INET6 */
1399e81ab876SGleb Smirnoff 			CARP_LOG("%s: INIT -> MASTER (preempting)\n",
1400fc74a9f9SBrooks Davis 			    SC2IFP(sc)->if_xname);
1401a9771948SGleb Smirnoff 			carp_set_state(sc, MASTER);
1402a9771948SGleb Smirnoff 			carp_setroute(sc, RTM_ADD);
1403a9771948SGleb Smirnoff 		} else {
1404e81ab876SGleb Smirnoff 			CARP_LOG("%s: INIT -> BACKUP\n", SC2IFP(sc)->if_xname);
1405a9771948SGleb Smirnoff 			carp_set_state(sc, BACKUP);
1406a9771948SGleb Smirnoff 			carp_setroute(sc, RTM_DELETE);
1407a9771948SGleb Smirnoff 			carp_setrun(sc, 0);
1408a9771948SGleb Smirnoff 		}
1409a9771948SGleb Smirnoff 		break;
1410a9771948SGleb Smirnoff 	case BACKUP:
1411a9771948SGleb Smirnoff 		callout_stop(&sc->sc_ad_tmo);
1412a9771948SGleb Smirnoff 		tv.tv_sec = 3 * sc->sc_advbase;
1413a9771948SGleb Smirnoff 		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1414a9771948SGleb Smirnoff 		switch (af) {
1415a9771948SGleb Smirnoff #ifdef INET
1416a9771948SGleb Smirnoff 		case AF_INET:
1417a9771948SGleb Smirnoff 			callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1418a9771948SGleb Smirnoff 			    carp_master_down, sc);
1419a9771948SGleb Smirnoff 			break;
1420a9771948SGleb Smirnoff #endif /* INET */
1421a9771948SGleb Smirnoff #ifdef INET6
1422a9771948SGleb Smirnoff 		case AF_INET6:
1423a9771948SGleb Smirnoff 			callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1424a9771948SGleb Smirnoff 			    carp_master_down, sc);
1425a9771948SGleb Smirnoff 			break;
1426a9771948SGleb Smirnoff #endif /* INET6 */
1427a9771948SGleb Smirnoff 		default:
1428a9771948SGleb Smirnoff 			if (sc->sc_naddrs)
1429a9771948SGleb Smirnoff 				callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1430a9771948SGleb Smirnoff 				    carp_master_down, sc);
1431a9771948SGleb Smirnoff 			if (sc->sc_naddrs6)
1432a9771948SGleb Smirnoff 				callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1433a9771948SGleb Smirnoff 				    carp_master_down, sc);
1434a9771948SGleb Smirnoff 			break;
1435a9771948SGleb Smirnoff 		}
1436a9771948SGleb Smirnoff 		break;
1437a9771948SGleb Smirnoff 	case MASTER:
1438a9771948SGleb Smirnoff 		tv.tv_sec = sc->sc_advbase;
1439a9771948SGleb Smirnoff 		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1440a9771948SGleb Smirnoff 		callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1441a9771948SGleb Smirnoff 		    carp_send_ad, sc);
1442a9771948SGleb Smirnoff 		break;
1443a9771948SGleb Smirnoff 	}
1444a9771948SGleb Smirnoff }
1445a9771948SGleb Smirnoff 
1446fbfdcf87SGleb Smirnoff static void
14470fa08018SGleb Smirnoff carp_multicast_cleanup(struct carp_softc *sc)
14480fa08018SGleb Smirnoff {
14490fa08018SGleb Smirnoff 	struct ip_moptions *imo = &sc->sc_imo;
14500fa08018SGleb Smirnoff 	u_int16_t n = imo->imo_num_memberships;
14510fa08018SGleb Smirnoff 
14520fa08018SGleb Smirnoff 	/* Clean up our own multicast memberships */
14530fa08018SGleb Smirnoff 	while (n-- > 0) {
14540fa08018SGleb Smirnoff 		if (imo->imo_membership[n] != NULL) {
14550fa08018SGleb Smirnoff 			in_delmulti(imo->imo_membership[n]);
14560fa08018SGleb Smirnoff 			imo->imo_membership[n] = NULL;
14570fa08018SGleb Smirnoff 		}
14580fa08018SGleb Smirnoff 	}
145971498f30SBruce M Simpson 	KASSERT(imo->imo_mfilters == NULL,
146071498f30SBruce M Simpson 	   ("%s: imo_mfilters != NULL", __func__));
14610fa08018SGleb Smirnoff 	imo->imo_num_memberships = 0;
14620fa08018SGleb Smirnoff 	imo->imo_multicast_ifp = NULL;
1463fbfdcf87SGleb Smirnoff }
14640fa08018SGleb Smirnoff 
14650fa08018SGleb Smirnoff #ifdef INET6
1466fbfdcf87SGleb Smirnoff static void
1467fbfdcf87SGleb Smirnoff carp_multicast6_cleanup(struct carp_softc *sc)
1468fbfdcf87SGleb Smirnoff {
1469fbfdcf87SGleb Smirnoff 	struct ip6_moptions *im6o = &sc->sc_im6o;
147033cde130SBruce M Simpson 	u_int16_t n = im6o->im6o_num_memberships;
1471fbfdcf87SGleb Smirnoff 
147233cde130SBruce M Simpson 	while (n-- > 0) {
147333cde130SBruce M Simpson 		if (im6o->im6o_membership[n] != NULL) {
147433cde130SBruce M Simpson 			in6_mc_leave(im6o->im6o_membership[n], NULL);
147533cde130SBruce M Simpson 			im6o->im6o_membership[n] = NULL;
14760fa08018SGleb Smirnoff 		}
147733cde130SBruce M Simpson 	}
147833cde130SBruce M Simpson 	KASSERT(im6o->im6o_mfilters == NULL,
147933cde130SBruce M Simpson 	   ("%s: im6o_mfilters != NULL", __func__));
148033cde130SBruce M Simpson 	im6o->im6o_num_memberships = 0;
14810fa08018SGleb Smirnoff 	im6o->im6o_multicast_ifp = NULL;
14820fa08018SGleb Smirnoff }
1483fbfdcf87SGleb Smirnoff #endif
14840fa08018SGleb Smirnoff 
14855c1f0f6dSGleb Smirnoff static int
1486a9771948SGleb Smirnoff carp_set_addr(struct carp_softc *sc, struct sockaddr_in *sin)
1487a9771948SGleb Smirnoff {
1488a9771948SGleb Smirnoff 	struct ifnet *ifp;
1489a9771948SGleb Smirnoff 	struct carp_if *cif;
1490a9771948SGleb Smirnoff 	struct in_ifaddr *ia, *ia_if;
1491a9771948SGleb Smirnoff 	struct ip_moptions *imo = &sc->sc_imo;
1492a9771948SGleb Smirnoff 	struct in_addr addr;
1493a9771948SGleb Smirnoff 	u_long iaddr = htonl(sin->sin_addr.s_addr);
1494a9771948SGleb Smirnoff 	int own, error;
1495a9771948SGleb Smirnoff 
1496a9771948SGleb Smirnoff 	if (sin->sin_addr.s_addr == 0) {
1497fc74a9f9SBrooks Davis 		if (!(SC2IFP(sc)->if_flags & IFF_UP))
1498a9771948SGleb Smirnoff 			carp_set_state(sc, INIT);
1499a9771948SGleb Smirnoff 		if (sc->sc_naddrs)
1500fc74a9f9SBrooks Davis 			SC2IFP(sc)->if_flags |= IFF_UP;
15017972c979SErmal Luçi 		if (sc->sc_carpdev)
15027972c979SErmal Luçi 			CARP_SCLOCK(sc);
1503a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
15047972c979SErmal Luçi 		if (sc->sc_carpdev)
15057972c979SErmal Luçi 			CARP_SCUNLOCK(sc);
1506a9771948SGleb Smirnoff 		return (0);
1507a9771948SGleb Smirnoff 	}
1508a9771948SGleb Smirnoff 
1509a9771948SGleb Smirnoff 	/* we have to do it by hands to check we won't match on us */
1510a9771948SGleb Smirnoff 	ia_if = NULL; own = 0;
15112d9cfabaSRobert Watson 	IN_IFADDR_RLOCK();
1512603724d3SBjoern A. Zeeb 	TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1513a9771948SGleb Smirnoff 		/* and, yeah, we need a multicast-capable iface too */
1514fc74a9f9SBrooks Davis 		if (ia->ia_ifp != SC2IFP(sc) &&
1515a9771948SGleb Smirnoff 		    (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1516a9771948SGleb Smirnoff 		    (iaddr & ia->ia_subnetmask) == ia->ia_subnet) {
1517a9771948SGleb Smirnoff 			if (!ia_if)
1518a9771948SGleb Smirnoff 				ia_if = ia;
1519a9771948SGleb Smirnoff 			if (sin->sin_addr.s_addr ==
1520a9771948SGleb Smirnoff 			    ia->ia_addr.sin_addr.s_addr)
1521a9771948SGleb Smirnoff 				own++;
1522a9771948SGleb Smirnoff 		}
1523a9771948SGleb Smirnoff 	}
1524a9771948SGleb Smirnoff 
15252d9cfabaSRobert Watson 	if (!ia_if) {
15262d9cfabaSRobert Watson 		IN_IFADDR_RUNLOCK();
1527a9771948SGleb Smirnoff 		return (EADDRNOTAVAIL);
15282d9cfabaSRobert Watson 	}
1529a9771948SGleb Smirnoff 
1530a9771948SGleb Smirnoff 	ia = ia_if;
15312d9cfabaSRobert Watson 	ifa_ref(&ia->ia_ifa);
15322d9cfabaSRobert Watson 	IN_IFADDR_RUNLOCK();
15332d9cfabaSRobert Watson 
1534a9771948SGleb Smirnoff 	ifp = ia->ia_ifp;
1535a9771948SGleb Smirnoff 
1536a9771948SGleb Smirnoff 	if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
15372d9cfabaSRobert Watson 	    (imo->imo_multicast_ifp && imo->imo_multicast_ifp != ifp)) {
15382d9cfabaSRobert Watson 		ifa_free(&ia->ia_ifa);
1539a9771948SGleb Smirnoff 		return (EADDRNOTAVAIL);
15402d9cfabaSRobert Watson 	}
1541a9771948SGleb Smirnoff 
1542a9771948SGleb Smirnoff 	if (imo->imo_num_memberships == 0) {
1543a9771948SGleb Smirnoff 		addr.s_addr = htonl(INADDR_CARP_GROUP);
15442d9cfabaSRobert Watson 		if ((imo->imo_membership[0] = in_addmulti(&addr, ifp)) ==
15452d9cfabaSRobert Watson 		    NULL) {
15462d9cfabaSRobert Watson 			ifa_free(&ia->ia_ifa);
1547a9771948SGleb Smirnoff 			return (ENOBUFS);
15482d9cfabaSRobert Watson 		}
1549a9771948SGleb Smirnoff 		imo->imo_num_memberships++;
1550a9771948SGleb Smirnoff 		imo->imo_multicast_ifp = ifp;
1551a9771948SGleb Smirnoff 		imo->imo_multicast_ttl = CARP_DFLTTL;
1552a9771948SGleb Smirnoff 		imo->imo_multicast_loop = 0;
1553a9771948SGleb Smirnoff 	}
1554a9771948SGleb Smirnoff 
1555a9771948SGleb Smirnoff 	if (!ifp->if_carp) {
1556a9771948SGleb Smirnoff 
15571ede983cSDag-Erling Smørgrav 		cif = malloc(sizeof(*cif), M_CARP,
1558a9771948SGleb Smirnoff 		    M_WAITOK|M_ZERO);
1559a9771948SGleb Smirnoff 		if (!cif) {
1560a9771948SGleb Smirnoff 			error = ENOBUFS;
1561a9771948SGleb Smirnoff 			goto cleanup;
1562a9771948SGleb Smirnoff 		}
1563a9771948SGleb Smirnoff 		if ((error = ifpromisc(ifp, 1))) {
15641ede983cSDag-Erling Smørgrav 			free(cif, M_CARP);
1565a9771948SGleb Smirnoff 			goto cleanup;
1566a9771948SGleb Smirnoff 		}
1567a9771948SGleb Smirnoff 
1568a9771948SGleb Smirnoff 		CARP_LOCK_INIT(cif);
1569a9771948SGleb Smirnoff 		CARP_LOCK(cif);
1570a9771948SGleb Smirnoff 		cif->vhif_ifp = ifp;
1571a9771948SGleb Smirnoff 		TAILQ_INIT(&cif->vhif_vrs);
1572a9771948SGleb Smirnoff 		ifp->if_carp = cif;
1573a9771948SGleb Smirnoff 
1574a9771948SGleb Smirnoff 	} else {
1575a9771948SGleb Smirnoff 		struct carp_softc *vr;
1576a9771948SGleb Smirnoff 
1577a9771948SGleb Smirnoff 		cif = (struct carp_if *)ifp->if_carp;
1578a9771948SGleb Smirnoff 		CARP_LOCK(cif);
1579a9771948SGleb Smirnoff 		TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1580a9771948SGleb Smirnoff 			if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1581a9771948SGleb Smirnoff 				CARP_UNLOCK(cif);
1582e60a0104SGleb Smirnoff 				error = EEXIST;
1583a9771948SGleb Smirnoff 				goto cleanup;
1584a9771948SGleb Smirnoff 			}
1585a9771948SGleb Smirnoff 	}
1586a9771948SGleb Smirnoff 	sc->sc_ia = ia;
1587e8c34a71SGleb Smirnoff 	sc->sc_carpdev = ifp;
1588a9771948SGleb Smirnoff 
1589a9771948SGleb Smirnoff 	{ /* XXX prevent endless loop if already in queue */
1590a9771948SGleb Smirnoff 	struct carp_softc *vr, *after = NULL;
1591a9771948SGleb Smirnoff 	int myself = 0;
1592a9771948SGleb Smirnoff 	cif = (struct carp_if *)ifp->if_carp;
1593a9771948SGleb Smirnoff 
1594a9771948SGleb Smirnoff 	/* XXX: cif should not change, right? So we still hold the lock */
1595a9771948SGleb Smirnoff 	CARP_LOCK_ASSERT(cif);
1596a9771948SGleb Smirnoff 
1597a9771948SGleb Smirnoff 	TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1598a9771948SGleb Smirnoff 		if (vr == sc)
1599a9771948SGleb Smirnoff 			myself = 1;
1600a9771948SGleb Smirnoff 		if (vr->sc_vhid < sc->sc_vhid)
1601a9771948SGleb Smirnoff 			after = vr;
1602a9771948SGleb Smirnoff 	}
1603a9771948SGleb Smirnoff 
1604a9771948SGleb Smirnoff 	if (!myself) {
1605a9771948SGleb Smirnoff 		/* We're trying to keep things in order */
1606a9771948SGleb Smirnoff 		if (after == NULL) {
1607a9771948SGleb Smirnoff 			TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1608a9771948SGleb Smirnoff 		} else {
1609a9771948SGleb Smirnoff 			TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1610a9771948SGleb Smirnoff 		}
1611a9771948SGleb Smirnoff 		cif->vhif_nvrs++;
1612a9771948SGleb Smirnoff 	}
1613a9771948SGleb Smirnoff 	}
1614a9771948SGleb Smirnoff 
1615a9771948SGleb Smirnoff 	sc->sc_naddrs++;
1616fc74a9f9SBrooks Davis 	SC2IFP(sc)->if_flags |= IFF_UP;
1617a9771948SGleb Smirnoff 	if (own)
1618a9771948SGleb Smirnoff 		sc->sc_advskew = 0;
16194cb39345SGleb Smirnoff 	carp_sc_state_locked(sc);
1620a9771948SGleb Smirnoff 	carp_setrun(sc, 0);
1621a9771948SGleb Smirnoff 
1622d220759bSGleb Smirnoff 	CARP_UNLOCK(cif);
16232d9cfabaSRobert Watson 	ifa_free(&ia->ia_ifa);	/* XXXRW: should hold reference for softc. */
1624d220759bSGleb Smirnoff 
1625a9771948SGleb Smirnoff 	return (0);
1626a9771948SGleb Smirnoff 
1627a9771948SGleb Smirnoff cleanup:
1628a9771948SGleb Smirnoff 	in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
16292d9cfabaSRobert Watson 	ifa_free(&ia->ia_ifa);
1630a9771948SGleb Smirnoff 	return (error);
1631a9771948SGleb Smirnoff }
1632a9771948SGleb Smirnoff 
16335c1f0f6dSGleb Smirnoff static int
1634a9771948SGleb Smirnoff carp_del_addr(struct carp_softc *sc, struct sockaddr_in *sin)
1635a9771948SGleb Smirnoff {
1636a9771948SGleb Smirnoff 	int error = 0;
1637a9771948SGleb Smirnoff 
1638a9771948SGleb Smirnoff 	if (!--sc->sc_naddrs) {
1639e8c34a71SGleb Smirnoff 		struct carp_if *cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1640a9771948SGleb Smirnoff 		struct ip_moptions *imo = &sc->sc_imo;
1641a9771948SGleb Smirnoff 
1642d220759bSGleb Smirnoff 		CARP_LOCK(cif);
1643a9771948SGleb Smirnoff 		callout_stop(&sc->sc_ad_tmo);
164413f4c340SRobert Watson 		SC2IFP(sc)->if_flags &= ~IFF_UP;
164513f4c340SRobert Watson 		SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1646a9771948SGleb Smirnoff 		sc->sc_vhid = -1;
1647a9771948SGleb Smirnoff 		in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1648a9771948SGleb Smirnoff 		imo->imo_multicast_ifp = NULL;
1649a9771948SGleb Smirnoff 		TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1650a9771948SGleb Smirnoff 		if (!--cif->vhif_nvrs) {
1651e8c34a71SGleb Smirnoff 			sc->sc_carpdev->if_carp = NULL;
1652a9771948SGleb Smirnoff 			CARP_LOCK_DESTROY(cif);
165352e12426SWill Andrews 			free(cif, M_CARP);
1654a9771948SGleb Smirnoff 		} else {
1655a9771948SGleb Smirnoff 			CARP_UNLOCK(cif);
1656a9771948SGleb Smirnoff 		}
1657a9771948SGleb Smirnoff 	}
1658a9771948SGleb Smirnoff 
1659a9771948SGleb Smirnoff 	return (error);
1660a9771948SGleb Smirnoff }
1661a9771948SGleb Smirnoff 
1662a9771948SGleb Smirnoff #ifdef INET6
16635c1f0f6dSGleb Smirnoff static int
1664a9771948SGleb Smirnoff carp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1665a9771948SGleb Smirnoff {
1666a9771948SGleb Smirnoff 	struct ifnet *ifp;
1667a9771948SGleb Smirnoff 	struct carp_if *cif;
1668a9771948SGleb Smirnoff 	struct in6_ifaddr *ia, *ia_if;
1669a9771948SGleb Smirnoff 	struct ip6_moptions *im6o = &sc->sc_im6o;
1670a1f7e5f8SHajimu UMEMOTO 	struct in6_addr in6;
1671a9771948SGleb Smirnoff 	int own, error;
1672a9771948SGleb Smirnoff 
167333cde130SBruce M Simpson 	error = 0;
167433cde130SBruce M Simpson 
1675a9771948SGleb Smirnoff 	if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1676fc74a9f9SBrooks Davis 		if (!(SC2IFP(sc)->if_flags & IFF_UP))
1677a9771948SGleb Smirnoff 			carp_set_state(sc, INIT);
1678a9771948SGleb Smirnoff 		if (sc->sc_naddrs6)
1679fc74a9f9SBrooks Davis 			SC2IFP(sc)->if_flags |= IFF_UP;
16807972c979SErmal Luçi 		if (sc->sc_carpdev)
16817972c979SErmal Luçi 			CARP_SCLOCK(sc);
1682a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
16837972c979SErmal Luçi 		if (sc->sc_carpdev)
16847972c979SErmal Luçi 			CARP_SCUNLOCK(sc);
1685a9771948SGleb Smirnoff 		return (0);
1686a9771948SGleb Smirnoff 	}
1687a9771948SGleb Smirnoff 
1688a9771948SGleb Smirnoff 	/* we have to do it by hands to check we won't match on us */
1689a9771948SGleb Smirnoff 	ia_if = NULL; own = 0;
1690d1da0a06SRobert Watson 	IN6_IFADDR_RLOCK();
169132187eb6SRobert Watson 	TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
1692a9771948SGleb Smirnoff 		int i;
1693a9771948SGleb Smirnoff 
1694a9771948SGleb Smirnoff 		for (i = 0; i < 4; i++) {
1695a9771948SGleb Smirnoff 			if ((sin6->sin6_addr.s6_addr32[i] &
1696a9771948SGleb Smirnoff 			    ia->ia_prefixmask.sin6_addr.s6_addr32[i]) !=
1697a9771948SGleb Smirnoff 			    (ia->ia_addr.sin6_addr.s6_addr32[i] &
1698a9771948SGleb Smirnoff 			    ia->ia_prefixmask.sin6_addr.s6_addr32[i]))
1699a9771948SGleb Smirnoff 				break;
1700a9771948SGleb Smirnoff 		}
1701a9771948SGleb Smirnoff 		/* and, yeah, we need a multicast-capable iface too */
1702fc74a9f9SBrooks Davis 		if (ia->ia_ifp != SC2IFP(sc) &&
1703a9771948SGleb Smirnoff 		    (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1704a9771948SGleb Smirnoff 		    (i == 4)) {
1705a9771948SGleb Smirnoff 			if (!ia_if)
1706a9771948SGleb Smirnoff 				ia_if = ia;
1707a9771948SGleb Smirnoff 			if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
1708a9771948SGleb Smirnoff 			    &ia->ia_addr.sin6_addr))
1709a9771948SGleb Smirnoff 				own++;
1710a9771948SGleb Smirnoff 		}
1711a9771948SGleb Smirnoff 	}
1712a9771948SGleb Smirnoff 
1713d1da0a06SRobert Watson 	if (!ia_if) {
1714d1da0a06SRobert Watson 		IN6_IFADDR_RUNLOCK();
1715a9771948SGleb Smirnoff 		return (EADDRNOTAVAIL);
1716d1da0a06SRobert Watson 	}
1717a9771948SGleb Smirnoff 	ia = ia_if;
1718d1da0a06SRobert Watson 	ifa_ref(&ia->ia_ifa);
1719d1da0a06SRobert Watson 	IN6_IFADDR_RUNLOCK();
1720a9771948SGleb Smirnoff 	ifp = ia->ia_ifp;
1721a9771948SGleb Smirnoff 
1722a9771948SGleb Smirnoff 	if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1723d1da0a06SRobert Watson 	    (im6o->im6o_multicast_ifp && im6o->im6o_multicast_ifp != ifp)) {
1724d1da0a06SRobert Watson 		ifa_free(&ia->ia_ifa);
1725a9771948SGleb Smirnoff 		return (EADDRNOTAVAIL);
1726d1da0a06SRobert Watson 	}
1727a9771948SGleb Smirnoff 
1728a9771948SGleb Smirnoff 	if (!sc->sc_naddrs6) {
172933cde130SBruce M Simpson 		struct in6_multi *in6m;
173033cde130SBruce M Simpson 
1731a9771948SGleb Smirnoff 		im6o->im6o_multicast_ifp = ifp;
1732a9771948SGleb Smirnoff 
1733a9771948SGleb Smirnoff 		/* join CARP multicast address */
1734a1f7e5f8SHajimu UMEMOTO 		bzero(&in6, sizeof(in6));
1735a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr16[0] = htons(0xff02);
1736a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr8[15] = 0x12;
1737a1f7e5f8SHajimu UMEMOTO 		if (in6_setscope(&in6, ifp, NULL) != 0)
1738a1f7e5f8SHajimu UMEMOTO 			goto cleanup;
173933cde130SBruce M Simpson 		in6m = NULL;
174033cde130SBruce M Simpson 		error = in6_mc_join(ifp, &in6, NULL, &in6m, 0);
174133cde130SBruce M Simpson 		if (error)
1742a9771948SGleb Smirnoff 			goto cleanup;
174333cde130SBruce M Simpson 		im6o->im6o_membership[0] = in6m;
174433cde130SBruce M Simpson 		im6o->im6o_num_memberships++;
1745a9771948SGleb Smirnoff 
1746a9771948SGleb Smirnoff 		/* join solicited multicast address */
1747a1f7e5f8SHajimu UMEMOTO 		bzero(&in6, sizeof(in6));
1748a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr16[0] = htons(0xff02);
1749a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr32[1] = 0;
1750a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr32[2] = htonl(1);
1751a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr32[3] = sin6->sin6_addr.s6_addr32[3];
1752a1f7e5f8SHajimu UMEMOTO 		in6.s6_addr8[12] = 0xff;
1753a1f7e5f8SHajimu UMEMOTO 		if (in6_setscope(&in6, ifp, NULL) != 0)
1754a1f7e5f8SHajimu UMEMOTO 			goto cleanup;
175533cde130SBruce M Simpson 		in6m = NULL;
175633cde130SBruce M Simpson 		error = in6_mc_join(ifp, &in6, NULL, &in6m, 0);
175733cde130SBruce M Simpson 		if (error)
1758a9771948SGleb Smirnoff 			goto cleanup;
175933cde130SBruce M Simpson 		im6o->im6o_membership[1] = in6m;
176033cde130SBruce M Simpson 		im6o->im6o_num_memberships++;
1761a9771948SGleb Smirnoff 	}
1762a9771948SGleb Smirnoff 
1763a9771948SGleb Smirnoff 	if (!ifp->if_carp) {
17641ede983cSDag-Erling Smørgrav 		cif = malloc(sizeof(*cif), M_CARP,
1765a9771948SGleb Smirnoff 		    M_WAITOK|M_ZERO);
1766a9771948SGleb Smirnoff 		if (!cif) {
1767a9771948SGleb Smirnoff 			error = ENOBUFS;
1768a9771948SGleb Smirnoff 			goto cleanup;
1769a9771948SGleb Smirnoff 		}
1770a9771948SGleb Smirnoff 		if ((error = ifpromisc(ifp, 1))) {
17711ede983cSDag-Erling Smørgrav 			free(cif, M_CARP);
1772a9771948SGleb Smirnoff 			goto cleanup;
1773a9771948SGleb Smirnoff 		}
1774a9771948SGleb Smirnoff 
1775a9771948SGleb Smirnoff 		CARP_LOCK_INIT(cif);
1776a9771948SGleb Smirnoff 		CARP_LOCK(cif);
1777a9771948SGleb Smirnoff 		cif->vhif_ifp = ifp;
1778a9771948SGleb Smirnoff 		TAILQ_INIT(&cif->vhif_vrs);
1779a9771948SGleb Smirnoff 		ifp->if_carp = cif;
1780a9771948SGleb Smirnoff 
1781a9771948SGleb Smirnoff 	} else {
1782a9771948SGleb Smirnoff 		struct carp_softc *vr;
1783a9771948SGleb Smirnoff 
1784a9771948SGleb Smirnoff 		cif = (struct carp_if *)ifp->if_carp;
1785a9771948SGleb Smirnoff 		CARP_LOCK(cif);
1786a9771948SGleb Smirnoff 		TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1787a9771948SGleb Smirnoff 			if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1788a9771948SGleb Smirnoff 				CARP_UNLOCK(cif);
1789a9771948SGleb Smirnoff 				error = EINVAL;
1790a9771948SGleb Smirnoff 				goto cleanup;
1791a9771948SGleb Smirnoff 			}
1792a9771948SGleb Smirnoff 	}
1793a9771948SGleb Smirnoff 	sc->sc_ia6 = ia;
1794e8c34a71SGleb Smirnoff 	sc->sc_carpdev = ifp;
1795a9771948SGleb Smirnoff 
1796a9771948SGleb Smirnoff 	{ /* XXX prevent endless loop if already in queue */
1797a9771948SGleb Smirnoff 	struct carp_softc *vr, *after = NULL;
1798a9771948SGleb Smirnoff 	int myself = 0;
1799a9771948SGleb Smirnoff 	cif = (struct carp_if *)ifp->if_carp;
1800a9771948SGleb Smirnoff 	CARP_LOCK_ASSERT(cif);
1801a9771948SGleb Smirnoff 
1802a9771948SGleb Smirnoff 	TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1803a9771948SGleb Smirnoff 		if (vr == sc)
1804a9771948SGleb Smirnoff 			myself = 1;
1805a9771948SGleb Smirnoff 		if (vr->sc_vhid < sc->sc_vhid)
1806a9771948SGleb Smirnoff 			after = vr;
1807a9771948SGleb Smirnoff 	}
1808a9771948SGleb Smirnoff 
1809a9771948SGleb Smirnoff 	if (!myself) {
1810a9771948SGleb Smirnoff 		/* We're trying to keep things in order */
1811a9771948SGleb Smirnoff 		if (after == NULL) {
1812a9771948SGleb Smirnoff 			TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1813a9771948SGleb Smirnoff 		} else {
1814a9771948SGleb Smirnoff 			TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1815a9771948SGleb Smirnoff 		}
1816a9771948SGleb Smirnoff 		cif->vhif_nvrs++;
1817a9771948SGleb Smirnoff 	}
1818a9771948SGleb Smirnoff 	}
1819a9771948SGleb Smirnoff 
1820a9771948SGleb Smirnoff 	sc->sc_naddrs6++;
1821fc74a9f9SBrooks Davis 	SC2IFP(sc)->if_flags |= IFF_UP;
1822a9771948SGleb Smirnoff 	if (own)
1823a9771948SGleb Smirnoff 		sc->sc_advskew = 0;
18244cb39345SGleb Smirnoff 	carp_sc_state_locked(sc);
1825a9771948SGleb Smirnoff 	carp_setrun(sc, 0);
1826a9771948SGleb Smirnoff 
1827d220759bSGleb Smirnoff 	CARP_UNLOCK(cif);
1828d1da0a06SRobert Watson 	ifa_free(&ia->ia_ifa);	/* XXXRW: should hold reference for softc. */
1829d220759bSGleb Smirnoff 
1830a9771948SGleb Smirnoff 	return (0);
1831a9771948SGleb Smirnoff 
1832a9771948SGleb Smirnoff cleanup:
183333cde130SBruce M Simpson 	if (!sc->sc_naddrs6)
183433cde130SBruce M Simpson 		carp_multicast6_cleanup(sc);
1835d1da0a06SRobert Watson 	ifa_free(&ia->ia_ifa);
1836a9771948SGleb Smirnoff 	return (error);
1837a9771948SGleb Smirnoff }
1838a9771948SGleb Smirnoff 
18395c1f0f6dSGleb Smirnoff static int
1840a9771948SGleb Smirnoff carp_del_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1841a9771948SGleb Smirnoff {
1842a9771948SGleb Smirnoff 	int error = 0;
1843a9771948SGleb Smirnoff 
1844a9771948SGleb Smirnoff 	if (!--sc->sc_naddrs6) {
1845e8c34a71SGleb Smirnoff 		struct carp_if *cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1846a9771948SGleb Smirnoff 
1847d220759bSGleb Smirnoff 		CARP_LOCK(cif);
1848a9771948SGleb Smirnoff 		callout_stop(&sc->sc_ad_tmo);
184913f4c340SRobert Watson 		SC2IFP(sc)->if_flags &= ~IFF_UP;
185013f4c340SRobert Watson 		SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1851a9771948SGleb Smirnoff 		sc->sc_vhid = -1;
185233cde130SBruce M Simpson 		carp_multicast6_cleanup(sc);
1853a9771948SGleb Smirnoff 		TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1854a9771948SGleb Smirnoff 		if (!--cif->vhif_nvrs) {
1855a9771948SGleb Smirnoff 			CARP_LOCK_DESTROY(cif);
1856e8c34a71SGleb Smirnoff 			sc->sc_carpdev->if_carp = NULL;
185752e12426SWill Andrews 			free(cif, M_CARP);
1858a9771948SGleb Smirnoff 		} else
1859a9771948SGleb Smirnoff 			CARP_UNLOCK(cif);
1860a9771948SGleb Smirnoff 	}
1861a9771948SGleb Smirnoff 
1862a9771948SGleb Smirnoff 	return (error);
1863a9771948SGleb Smirnoff }
1864a9771948SGleb Smirnoff #endif /* INET6 */
1865a9771948SGleb Smirnoff 
18665c1f0f6dSGleb Smirnoff static int
1867a9771948SGleb Smirnoff carp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr)
1868a9771948SGleb Smirnoff {
1869a9771948SGleb Smirnoff 	struct carp_softc *sc = ifp->if_softc, *vr;
1870a9771948SGleb Smirnoff 	struct carpreq carpr;
1871a9771948SGleb Smirnoff 	struct ifaddr *ifa;
1872a9771948SGleb Smirnoff 	struct ifreq *ifr;
1873a9771948SGleb Smirnoff 	struct ifaliasreq *ifra;
1874d220759bSGleb Smirnoff 	int locked = 0, error = 0;
1875a9771948SGleb Smirnoff 
1876a9771948SGleb Smirnoff 	ifa = (struct ifaddr *)addr;
1877a9771948SGleb Smirnoff 	ifra = (struct ifaliasreq *)addr;
1878a9771948SGleb Smirnoff 	ifr = (struct ifreq *)addr;
1879a9771948SGleb Smirnoff 
1880a9771948SGleb Smirnoff 	switch (cmd) {
1881a9771948SGleb Smirnoff 	case SIOCSIFADDR:
1882a9771948SGleb Smirnoff 		switch (ifa->ifa_addr->sa_family) {
1883a9771948SGleb Smirnoff #ifdef INET
1884a9771948SGleb Smirnoff 		case AF_INET:
1885fc74a9f9SBrooks Davis 			SC2IFP(sc)->if_flags |= IFF_UP;
1886a9771948SGleb Smirnoff 			bcopy(ifa->ifa_addr, ifa->ifa_dstaddr,
1887a9771948SGleb Smirnoff 			    sizeof(struct sockaddr));
1888a9771948SGleb Smirnoff 			error = carp_set_addr(sc, satosin(ifa->ifa_addr));
1889a9771948SGleb Smirnoff 			break;
1890a9771948SGleb Smirnoff #endif /* INET */
1891a9771948SGleb Smirnoff #ifdef INET6
1892a9771948SGleb Smirnoff 		case AF_INET6:
1893fc74a9f9SBrooks Davis 			SC2IFP(sc)->if_flags |= IFF_UP;
1894a9771948SGleb Smirnoff 			error = carp_set_addr6(sc, satosin6(ifa->ifa_addr));
1895a9771948SGleb Smirnoff 			break;
1896a9771948SGleb Smirnoff #endif /* INET6 */
1897a9771948SGleb Smirnoff 		default:
1898a9771948SGleb Smirnoff 			error = EAFNOSUPPORT;
1899a9771948SGleb Smirnoff 			break;
1900a9771948SGleb Smirnoff 		}
1901a9771948SGleb Smirnoff 		break;
1902a9771948SGleb Smirnoff 
1903a9771948SGleb Smirnoff 	case SIOCAIFADDR:
1904a9771948SGleb Smirnoff 		switch (ifa->ifa_addr->sa_family) {
1905a9771948SGleb Smirnoff #ifdef INET
1906a9771948SGleb Smirnoff 		case AF_INET:
1907fc74a9f9SBrooks Davis 			SC2IFP(sc)->if_flags |= IFF_UP;
1908a9771948SGleb Smirnoff 			bcopy(ifa->ifa_addr, ifa->ifa_dstaddr,
1909a9771948SGleb Smirnoff 			    sizeof(struct sockaddr));
1910a9771948SGleb Smirnoff 			error = carp_set_addr(sc, satosin(&ifra->ifra_addr));
1911a9771948SGleb Smirnoff 			break;
1912a9771948SGleb Smirnoff #endif /* INET */
1913a9771948SGleb Smirnoff #ifdef INET6
1914a9771948SGleb Smirnoff 		case AF_INET6:
1915fc74a9f9SBrooks Davis 			SC2IFP(sc)->if_flags |= IFF_UP;
1916a9771948SGleb Smirnoff 			error = carp_set_addr6(sc, satosin6(&ifra->ifra_addr));
1917a9771948SGleb Smirnoff 			break;
1918a9771948SGleb Smirnoff #endif /* INET6 */
1919a9771948SGleb Smirnoff 		default:
1920a9771948SGleb Smirnoff 			error = EAFNOSUPPORT;
1921a9771948SGleb Smirnoff 			break;
1922a9771948SGleb Smirnoff 		}
1923a9771948SGleb Smirnoff 		break;
1924a9771948SGleb Smirnoff 
1925a9771948SGleb Smirnoff 	case SIOCDIFADDR:
1926a9771948SGleb Smirnoff 		switch (ifa->ifa_addr->sa_family) {
1927a9771948SGleb Smirnoff #ifdef INET
1928a9771948SGleb Smirnoff 		case AF_INET:
1929a9771948SGleb Smirnoff 			error = carp_del_addr(sc, satosin(&ifra->ifra_addr));
1930a9771948SGleb Smirnoff 			break;
1931a9771948SGleb Smirnoff #endif /* INET */
1932a9771948SGleb Smirnoff #ifdef INET6
1933a9771948SGleb Smirnoff 		case AF_INET6:
1934a9771948SGleb Smirnoff 			error = carp_del_addr6(sc, satosin6(&ifra->ifra_addr));
1935a9771948SGleb Smirnoff 			break;
1936a9771948SGleb Smirnoff #endif /* INET6 */
1937a9771948SGleb Smirnoff 		default:
1938a9771948SGleb Smirnoff 			error = EAFNOSUPPORT;
1939a9771948SGleb Smirnoff 			break;
1940a9771948SGleb Smirnoff 		}
1941a9771948SGleb Smirnoff 		break;
1942a9771948SGleb Smirnoff 
1943a9771948SGleb Smirnoff 	case SIOCSIFFLAGS:
1944d220759bSGleb Smirnoff 		if (sc->sc_carpdev) {
1945d220759bSGleb Smirnoff 			locked = 1;
1946d220759bSGleb Smirnoff 			CARP_SCLOCK(sc);
1947d220759bSGleb Smirnoff 		}
1948a9771948SGleb Smirnoff 		if (sc->sc_state != INIT && !(ifr->ifr_flags & IFF_UP)) {
1949a9771948SGleb Smirnoff  			callout_stop(&sc->sc_ad_tmo);
1950a9771948SGleb Smirnoff  			callout_stop(&sc->sc_md_tmo);
1951a9771948SGleb Smirnoff  			callout_stop(&sc->sc_md6_tmo);
1952a9771948SGleb Smirnoff 			if (sc->sc_state == MASTER)
1953d220759bSGleb Smirnoff 				carp_send_ad_locked(sc);
1954a9771948SGleb Smirnoff 			carp_set_state(sc, INIT);
1955a9771948SGleb Smirnoff 			carp_setrun(sc, 0);
1956a9771948SGleb Smirnoff 		} else if (sc->sc_state == INIT && (ifr->ifr_flags & IFF_UP)) {
1957fc74a9f9SBrooks Davis 			SC2IFP(sc)->if_flags |= IFF_UP;
1958a9771948SGleb Smirnoff 			carp_setrun(sc, 0);
1959a9771948SGleb Smirnoff 		}
1960a9771948SGleb Smirnoff 		break;
1961a9771948SGleb Smirnoff 
1962a9771948SGleb Smirnoff 	case SIOCSVH:
1963acd3428bSRobert Watson 		error = priv_check(curthread, PRIV_NETINET_CARP);
1964acd3428bSRobert Watson 		if (error)
1965a9771948SGleb Smirnoff 			break;
1966a9771948SGleb Smirnoff 		if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
1967a9771948SGleb Smirnoff 			break;
1968a9771948SGleb Smirnoff 		error = 1;
1969d220759bSGleb Smirnoff 		if (sc->sc_carpdev) {
1970d220759bSGleb Smirnoff 			locked = 1;
1971d220759bSGleb Smirnoff 			CARP_SCLOCK(sc);
1972d220759bSGleb Smirnoff 		}
1973a9771948SGleb Smirnoff 		if (sc->sc_state != INIT && carpr.carpr_state != sc->sc_state) {
1974a9771948SGleb Smirnoff 			switch (carpr.carpr_state) {
1975a9771948SGleb Smirnoff 			case BACKUP:
1976a9771948SGleb Smirnoff 				callout_stop(&sc->sc_ad_tmo);
1977a9771948SGleb Smirnoff 				carp_set_state(sc, BACKUP);
1978a9771948SGleb Smirnoff 				carp_setrun(sc, 0);
1979a9771948SGleb Smirnoff 				carp_setroute(sc, RTM_DELETE);
1980a9771948SGleb Smirnoff 				break;
1981a9771948SGleb Smirnoff 			case MASTER:
1982d220759bSGleb Smirnoff 				carp_master_down_locked(sc);
1983a9771948SGleb Smirnoff 				break;
1984a9771948SGleb Smirnoff 			default:
1985a9771948SGleb Smirnoff 				break;
1986a9771948SGleb Smirnoff 			}
1987a9771948SGleb Smirnoff 		}
1988a9771948SGleb Smirnoff 		if (carpr.carpr_vhid > 0) {
1989a9771948SGleb Smirnoff 			if (carpr.carpr_vhid > 255) {
1990a9771948SGleb Smirnoff 				error = EINVAL;
1991a9771948SGleb Smirnoff 				break;
1992a9771948SGleb Smirnoff 			}
1993e8c34a71SGleb Smirnoff 			if (sc->sc_carpdev) {
1994a9771948SGleb Smirnoff 				struct carp_if *cif;
1995e8c34a71SGleb Smirnoff 				cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1996a9771948SGleb Smirnoff 				TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1997a9771948SGleb Smirnoff 					if (vr != sc &&
1998e9bf9fb6SGleb Smirnoff 					    vr->sc_vhid == carpr.carpr_vhid) {
1999e9bf9fb6SGleb Smirnoff 						error = EEXIST;
2000e9bf9fb6SGleb Smirnoff 						break;
2001e9bf9fb6SGleb Smirnoff 					}
2002e9bf9fb6SGleb Smirnoff 				if (error == EEXIST)
2003e9bf9fb6SGleb Smirnoff 					break;
2004a9771948SGleb Smirnoff 			}
2005a9771948SGleb Smirnoff 			sc->sc_vhid = carpr.carpr_vhid;
20064a0d6638SRuslan Ermilov 			IF_LLADDR(sc->sc_ifp)[0] = 0;
20074a0d6638SRuslan Ermilov 			IF_LLADDR(sc->sc_ifp)[1] = 0;
20084a0d6638SRuslan Ermilov 			IF_LLADDR(sc->sc_ifp)[2] = 0x5e;
20094a0d6638SRuslan Ermilov 			IF_LLADDR(sc->sc_ifp)[3] = 0;
20104a0d6638SRuslan Ermilov 			IF_LLADDR(sc->sc_ifp)[4] = 1;
20114a0d6638SRuslan Ermilov 			IF_LLADDR(sc->sc_ifp)[5] = sc->sc_vhid;
2012a9771948SGleb Smirnoff 			error--;
2013a9771948SGleb Smirnoff 		}
2014a9771948SGleb Smirnoff 		if (carpr.carpr_advbase > 0 || carpr.carpr_advskew > 0) {
2015a9771948SGleb Smirnoff 			if (carpr.carpr_advskew >= 255) {
2016a9771948SGleb Smirnoff 				error = EINVAL;
2017a9771948SGleb Smirnoff 				break;
2018a9771948SGleb Smirnoff 			}
2019a9771948SGleb Smirnoff 			if (carpr.carpr_advbase > 255) {
2020a9771948SGleb Smirnoff 				error = EINVAL;
2021a9771948SGleb Smirnoff 				break;
2022a9771948SGleb Smirnoff 			}
2023a9771948SGleb Smirnoff 			sc->sc_advbase = carpr.carpr_advbase;
2024a9771948SGleb Smirnoff 			sc->sc_advskew = carpr.carpr_advskew;
2025a9771948SGleb Smirnoff 			error--;
2026a9771948SGleb Smirnoff 		}
2027a9771948SGleb Smirnoff 		bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
2028a9771948SGleb Smirnoff 		if (error > 0)
2029a9771948SGleb Smirnoff 			error = EINVAL;
2030a9771948SGleb Smirnoff 		else {
2031a9771948SGleb Smirnoff 			error = 0;
2032a9771948SGleb Smirnoff 			carp_setrun(sc, 0);
2033a9771948SGleb Smirnoff 		}
2034a9771948SGleb Smirnoff 		break;
2035a9771948SGleb Smirnoff 
2036a9771948SGleb Smirnoff 	case SIOCGVH:
2037d220759bSGleb Smirnoff 		/* XXX: lockless read */
2038a9771948SGleb Smirnoff 		bzero(&carpr, sizeof(carpr));
2039a9771948SGleb Smirnoff 		carpr.carpr_state = sc->sc_state;
2040a9771948SGleb Smirnoff 		carpr.carpr_vhid = sc->sc_vhid;
2041a9771948SGleb Smirnoff 		carpr.carpr_advbase = sc->sc_advbase;
2042a9771948SGleb Smirnoff 		carpr.carpr_advskew = sc->sc_advskew;
2043acd3428bSRobert Watson 		error = priv_check(curthread, PRIV_NETINET_CARP);
2044acd3428bSRobert Watson 		if (error == 0)
2045a9771948SGleb Smirnoff 			bcopy(sc->sc_key, carpr.carpr_key,
2046a9771948SGleb Smirnoff 			    sizeof(carpr.carpr_key));
2047a9771948SGleb Smirnoff 		error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
2048a9771948SGleb Smirnoff 		break;
2049a9771948SGleb Smirnoff 
2050a9771948SGleb Smirnoff 	default:
2051a9771948SGleb Smirnoff 		error = EINVAL;
2052a9771948SGleb Smirnoff 	}
2053a9771948SGleb Smirnoff 
2054d220759bSGleb Smirnoff 	if (locked)
2055d220759bSGleb Smirnoff 		CARP_SCUNLOCK(sc);
2056d220759bSGleb Smirnoff 
2057a9771948SGleb Smirnoff 	carp_hmac_prepare(sc);
2058d220759bSGleb Smirnoff 
2059a9771948SGleb Smirnoff 	return (error);
2060a9771948SGleb Smirnoff }
2061a9771948SGleb Smirnoff 
2062a9771948SGleb Smirnoff /*
2063a9771948SGleb Smirnoff  * XXX: this is looutput. We should eventually use it from there.
2064a9771948SGleb Smirnoff  */
2065a9771948SGleb Smirnoff static int
2066a9771948SGleb Smirnoff carp_looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
2067279aa3d4SKip Macy     struct route *ro)
2068a9771948SGleb Smirnoff {
206901399f34SDavid Malone 	u_int32_t af;
2070279aa3d4SKip Macy 	struct rtentry *rt = NULL;
207101399f34SDavid Malone 
2072a9771948SGleb Smirnoff 	M_ASSERTPKTHDR(m); /* check if we have the packet header */
2073a9771948SGleb Smirnoff 
2074279aa3d4SKip Macy 	if (ro != NULL)
2075279aa3d4SKip Macy 		rt = ro->ro_rt;
2076a9771948SGleb Smirnoff 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
2077a9771948SGleb Smirnoff 		m_freem(m);
2078a9771948SGleb Smirnoff 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
2079a9771948SGleb Smirnoff 			rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
2080a9771948SGleb Smirnoff 	}
2081a9771948SGleb Smirnoff 
2082a9771948SGleb Smirnoff 	ifp->if_opackets++;
2083a9771948SGleb Smirnoff 	ifp->if_obytes += m->m_pkthdr.len;
208401399f34SDavid Malone 
208501399f34SDavid Malone 	/* BPF writes need to be handled specially. */
208601399f34SDavid Malone 	if (dst->sa_family == AF_UNSPEC) {
208701399f34SDavid Malone 		bcopy(dst->sa_data, &af, sizeof(af));
208801399f34SDavid Malone 		dst->sa_family = af;
208901399f34SDavid Malone 	}
209001399f34SDavid Malone 
2091a9771948SGleb Smirnoff #if 1	/* XXX */
2092a9771948SGleb Smirnoff 	switch (dst->sa_family) {
2093a9771948SGleb Smirnoff 	case AF_INET:
2094a9771948SGleb Smirnoff 	case AF_INET6:
2095a9771948SGleb Smirnoff 	case AF_IPX:
2096a9771948SGleb Smirnoff 	case AF_APPLETALK:
2097a9771948SGleb Smirnoff 		break;
2098a9771948SGleb Smirnoff 	default:
2099a9771948SGleb Smirnoff 		printf("carp_looutput: af=%d unexpected\n", dst->sa_family);
2100a9771948SGleb Smirnoff 		m_freem(m);
2101a9771948SGleb Smirnoff 		return (EAFNOSUPPORT);
2102a9771948SGleb Smirnoff 	}
2103a9771948SGleb Smirnoff #endif
2104a9771948SGleb Smirnoff 	return(if_simloop(ifp, m, dst->sa_family, 0));
2105a9771948SGleb Smirnoff }
2106a9771948SGleb Smirnoff 
2107a9771948SGleb Smirnoff /*
2108a9771948SGleb Smirnoff  * Start output on carp interface. This function should never be called.
2109a9771948SGleb Smirnoff  */
21105c1f0f6dSGleb Smirnoff static void
2111a9771948SGleb Smirnoff carp_start(struct ifnet *ifp)
2112a9771948SGleb Smirnoff {
2113a9771948SGleb Smirnoff #ifdef DEBUG
2114a9771948SGleb Smirnoff 	printf("%s: start called\n", ifp->if_xname);
2115a9771948SGleb Smirnoff #endif
2116a9771948SGleb Smirnoff }
2117a9771948SGleb Smirnoff 
2118a9771948SGleb Smirnoff int
2119a9771948SGleb Smirnoff carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
2120a9771948SGleb Smirnoff     struct rtentry *rt)
2121a9771948SGleb Smirnoff {
2122a9771948SGleb Smirnoff 	struct m_tag *mtag;
2123a9771948SGleb Smirnoff 	struct carp_softc *sc;
2124a9771948SGleb Smirnoff 	struct ifnet *carp_ifp;
2125a9771948SGleb Smirnoff 
2126a9771948SGleb Smirnoff 	if (!sa)
2127a9771948SGleb Smirnoff 		return (0);
2128a9771948SGleb Smirnoff 
2129a9771948SGleb Smirnoff 	switch (sa->sa_family) {
2130a9771948SGleb Smirnoff #ifdef INET
2131a9771948SGleb Smirnoff 	case AF_INET:
2132a9771948SGleb Smirnoff 		break;
2133a9771948SGleb Smirnoff #endif /* INET */
2134a9771948SGleb Smirnoff #ifdef INET6
2135a9771948SGleb Smirnoff 	case AF_INET6:
2136a9771948SGleb Smirnoff 		break;
2137a9771948SGleb Smirnoff #endif /* INET6 */
2138a9771948SGleb Smirnoff 	default:
2139a9771948SGleb Smirnoff 		return (0);
2140a9771948SGleb Smirnoff 	}
2141a9771948SGleb Smirnoff 
2142a9771948SGleb Smirnoff 	mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
2143a9771948SGleb Smirnoff 	if (mtag == NULL)
2144a9771948SGleb Smirnoff 		return (0);
2145a9771948SGleb Smirnoff 
2146a9771948SGleb Smirnoff 	bcopy(mtag + 1, &carp_ifp, sizeof(struct ifnet *));
2147a9771948SGleb Smirnoff 	sc = carp_ifp->if_softc;
2148a9771948SGleb Smirnoff 
2149a9771948SGleb Smirnoff 	/* Set the source MAC address to Virtual Router MAC Address */
2150a9771948SGleb Smirnoff 	switch (ifp->if_type) {
2151630481bbSYaroslav Tykhiy 	case IFT_ETHER:
2152630481bbSYaroslav Tykhiy 	case IFT_L2VLAN: {
2153a9771948SGleb Smirnoff 			struct ether_header *eh;
2154a9771948SGleb Smirnoff 
2155a9771948SGleb Smirnoff 			eh = mtod(m, struct ether_header *);
2156a9771948SGleb Smirnoff 			eh->ether_shost[0] = 0;
2157a9771948SGleb Smirnoff 			eh->ether_shost[1] = 0;
2158a9771948SGleb Smirnoff 			eh->ether_shost[2] = 0x5e;
2159a9771948SGleb Smirnoff 			eh->ether_shost[3] = 0;
2160a9771948SGleb Smirnoff 			eh->ether_shost[4] = 1;
2161a9771948SGleb Smirnoff 			eh->ether_shost[5] = sc->sc_vhid;
2162a9771948SGleb Smirnoff 		}
2163a9771948SGleb Smirnoff 		break;
2164a9771948SGleb Smirnoff 	case IFT_FDDI: {
2165a9771948SGleb Smirnoff 			struct fddi_header *fh;
2166a9771948SGleb Smirnoff 
2167a9771948SGleb Smirnoff 			fh = mtod(m, struct fddi_header *);
2168a9771948SGleb Smirnoff 			fh->fddi_shost[0] = 0;
2169a9771948SGleb Smirnoff 			fh->fddi_shost[1] = 0;
2170a9771948SGleb Smirnoff 			fh->fddi_shost[2] = 0x5e;
2171a9771948SGleb Smirnoff 			fh->fddi_shost[3] = 0;
2172a9771948SGleb Smirnoff 			fh->fddi_shost[4] = 1;
2173a9771948SGleb Smirnoff 			fh->fddi_shost[5] = sc->sc_vhid;
2174a9771948SGleb Smirnoff 		}
2175a9771948SGleb Smirnoff 		break;
2176a9771948SGleb Smirnoff 	case IFT_ISO88025: {
2177a9771948SGleb Smirnoff  			struct iso88025_header *th;
2178a9771948SGleb Smirnoff  			th = mtod(m, struct iso88025_header *);
2179a9771948SGleb Smirnoff 			th->iso88025_shost[0] = 3;
2180a9771948SGleb Smirnoff 			th->iso88025_shost[1] = 0;
2181a9771948SGleb Smirnoff 			th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1);
2182a9771948SGleb Smirnoff 			th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1);
2183a9771948SGleb Smirnoff 			th->iso88025_shost[4] = 0;
2184a9771948SGleb Smirnoff 			th->iso88025_shost[5] = 0;
2185a9771948SGleb Smirnoff 		}
2186a9771948SGleb Smirnoff 		break;
2187a9771948SGleb Smirnoff 	default:
2188a9771948SGleb Smirnoff 		printf("%s: carp is not supported for this interface type\n",
2189a9771948SGleb Smirnoff 		    ifp->if_xname);
2190a9771948SGleb Smirnoff 		return (EOPNOTSUPP);
2191a9771948SGleb Smirnoff 	}
2192a9771948SGleb Smirnoff 
2193a9771948SGleb Smirnoff 	return (0);
2194a9771948SGleb Smirnoff }
2195a9771948SGleb Smirnoff 
21965c1f0f6dSGleb Smirnoff static void
2197a9771948SGleb Smirnoff carp_set_state(struct carp_softc *sc, int state)
2198a9771948SGleb Smirnoff {
21990b476f1cSGleb Smirnoff 	int link_state;
2200d220759bSGleb Smirnoff 
2201d220759bSGleb Smirnoff 	if (sc->sc_carpdev)
2202d220759bSGleb Smirnoff 		CARP_SCLOCK_ASSERT(sc);
2203d220759bSGleb Smirnoff 
2204a9771948SGleb Smirnoff 	if (sc->sc_state == state)
2205a9771948SGleb Smirnoff 		return;
2206a9771948SGleb Smirnoff 
2207a9771948SGleb Smirnoff 	sc->sc_state = state;
2208a9771948SGleb Smirnoff 	switch (state) {
2209a9771948SGleb Smirnoff 	case BACKUP:
22100b476f1cSGleb Smirnoff 		link_state = LINK_STATE_DOWN;
2211a9771948SGleb Smirnoff 		break;
2212a9771948SGleb Smirnoff 	case MASTER:
22130b476f1cSGleb Smirnoff 		link_state = LINK_STATE_UP;
2214a9771948SGleb Smirnoff 		break;
2215a9771948SGleb Smirnoff 	default:
22160b476f1cSGleb Smirnoff 		link_state = LINK_STATE_UNKNOWN;
2217a9771948SGleb Smirnoff 		break;
2218a9771948SGleb Smirnoff 	}
22190b476f1cSGleb Smirnoff 	if_link_state_change(SC2IFP(sc), link_state);
2220a9771948SGleb Smirnoff }
2221a9771948SGleb Smirnoff 
2222a9771948SGleb Smirnoff void
222354bfbd51SWill Andrews carp_carpdev_state(struct ifnet *ifp)
2224a9771948SGleb Smirnoff {
222554bfbd51SWill Andrews 	struct carp_if *cif;
2226d220759bSGleb Smirnoff 
222754bfbd51SWill Andrews 	cif = ifp->if_carp;
2228a9771948SGleb Smirnoff 	CARP_LOCK(cif);
2229d220759bSGleb Smirnoff 	carp_carpdev_state_locked(cif);
2230d220759bSGleb Smirnoff 	CARP_UNLOCK(cif);
2231d220759bSGleb Smirnoff }
2232d220759bSGleb Smirnoff 
2233d220759bSGleb Smirnoff static void
2234d220759bSGleb Smirnoff carp_carpdev_state_locked(struct carp_if *cif)
2235d220759bSGleb Smirnoff {
2236d220759bSGleb Smirnoff 	struct carp_softc *sc;
2237d220759bSGleb Smirnoff 
22384cb39345SGleb Smirnoff 	TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list)
22394cb39345SGleb Smirnoff 		carp_sc_state_locked(sc);
22404cb39345SGleb Smirnoff }
22414cb39345SGleb Smirnoff 
22424cb39345SGleb Smirnoff static void
22434cb39345SGleb Smirnoff carp_sc_state_locked(struct carp_softc *sc)
22444cb39345SGleb Smirnoff {
22454cb39345SGleb Smirnoff 	CARP_SCLOCK_ASSERT(sc);
22464cb39345SGleb Smirnoff 
2247e8c34a71SGleb Smirnoff 	if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
2248e8c34a71SGleb Smirnoff 	    !(sc->sc_carpdev->if_flags & IFF_UP)) {
2249fc74a9f9SBrooks Davis 		sc->sc_flags_backup = SC2IFP(sc)->if_flags;
225013f4c340SRobert Watson 		SC2IFP(sc)->if_flags &= ~IFF_UP;
225113f4c340SRobert Watson 		SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
2252a9771948SGleb Smirnoff 		callout_stop(&sc->sc_ad_tmo);
2253a9771948SGleb Smirnoff 		callout_stop(&sc->sc_md_tmo);
2254a9771948SGleb Smirnoff 		callout_stop(&sc->sc_md6_tmo);
2255a9771948SGleb Smirnoff 		carp_set_state(sc, INIT);
2256a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
2257a9771948SGleb Smirnoff 		if (!sc->sc_suppress) {
2258a9771948SGleb Smirnoff 			carp_suppress_preempt++;
2259d220759bSGleb Smirnoff 			if (carp_suppress_preempt == 1) {
2260d220759bSGleb Smirnoff 				CARP_SCUNLOCK(sc);
2261a9771948SGleb Smirnoff 				carp_send_ad_all();
2262d220759bSGleb Smirnoff 				CARP_SCLOCK(sc);
2263d220759bSGleb Smirnoff 			}
2264a9771948SGleb Smirnoff 		}
2265a9771948SGleb Smirnoff 		sc->sc_suppress = 1;
2266a9771948SGleb Smirnoff 	} else {
2267fc74a9f9SBrooks Davis 		SC2IFP(sc)->if_flags |= sc->sc_flags_backup;
2268a9771948SGleb Smirnoff 		carp_set_state(sc, INIT);
2269a9771948SGleb Smirnoff 		carp_setrun(sc, 0);
2270a9771948SGleb Smirnoff 		if (sc->sc_suppress)
2271a9771948SGleb Smirnoff 			carp_suppress_preempt--;
2272a9771948SGleb Smirnoff 		sc->sc_suppress = 0;
2273a9771948SGleb Smirnoff 	}
22744cb39345SGleb Smirnoff 
22754cb39345SGleb Smirnoff 	return;
2276a9771948SGleb Smirnoff }
2277a9771948SGleb Smirnoff 
227854bfbd51SWill Andrews #ifdef INET
227954bfbd51SWill Andrews extern  struct domain inetdomain;
228054bfbd51SWill Andrews static struct protosw in_carp_protosw = {
228154bfbd51SWill Andrews 	.pr_type =		SOCK_RAW,
228254bfbd51SWill Andrews 	.pr_domain =		&inetdomain,
228354bfbd51SWill Andrews 	.pr_protocol =		IPPROTO_CARP,
228454bfbd51SWill Andrews 	.pr_flags =		PR_ATOMIC|PR_ADDR,
228554bfbd51SWill Andrews 	.pr_input =		carp_input,
228654bfbd51SWill Andrews 	.pr_output =		(pr_output_t *)rip_output,
228754bfbd51SWill Andrews 	.pr_ctloutput =		rip_ctloutput,
228854bfbd51SWill Andrews 	.pr_usrreqs =		&rip_usrreqs
228954bfbd51SWill Andrews };
229054bfbd51SWill Andrews #endif
229154bfbd51SWill Andrews 
229254bfbd51SWill Andrews #ifdef INET6
229354bfbd51SWill Andrews extern	struct domain inet6domain;
229454bfbd51SWill Andrews static struct ip6protosw in6_carp_protosw = {
229554bfbd51SWill Andrews 	.pr_type =		SOCK_RAW,
229654bfbd51SWill Andrews 	.pr_domain =		&inet6domain,
229754bfbd51SWill Andrews 	.pr_protocol =		IPPROTO_CARP,
229854bfbd51SWill Andrews 	.pr_flags =		PR_ATOMIC|PR_ADDR,
229954bfbd51SWill Andrews 	.pr_input =		carp6_input,
230054bfbd51SWill Andrews 	.pr_output =		rip6_output,
230154bfbd51SWill Andrews 	.pr_ctloutput =		rip6_ctloutput,
230254bfbd51SWill Andrews 	.pr_usrreqs =		&rip6_usrreqs
230354bfbd51SWill Andrews };
230454bfbd51SWill Andrews #endif
230554bfbd51SWill Andrews 
230654bfbd51SWill Andrews static void
230754bfbd51SWill Andrews carp_mod_cleanup(void)
2308a9771948SGleb Smirnoff {
230954bfbd51SWill Andrews 
231054bfbd51SWill Andrews 	if (if_detach_event_tag == NULL)
231154bfbd51SWill Andrews 		return;
231254bfbd51SWill Andrews 	EVENTHANDLER_DEREGISTER(ifnet_departure_event, if_detach_event_tag);
231354bfbd51SWill Andrews 	if_clone_detach(&carp_cloner);
231454bfbd51SWill Andrews #ifdef INET
231554bfbd51SWill Andrews 	if (proto_reg[CARP_INET] == 0) {
231615249f73SWill Andrews 		(void)ipproto_unregister(IPPROTO_CARP);
231754bfbd51SWill Andrews 		pf_proto_unregister(PF_INET, IPPROTO_CARP, SOCK_RAW);
231854bfbd51SWill Andrews 		proto_reg[CARP_INET] = -1;
231954bfbd51SWill Andrews 	}
232054bfbd51SWill Andrews 	carp_iamatch_p = NULL;
232154bfbd51SWill Andrews #endif
232254bfbd51SWill Andrews #ifdef INET6
232354bfbd51SWill Andrews 	if (proto_reg[CARP_INET6] == 0) {
232415249f73SWill Andrews 		(void)ip6proto_unregister(IPPROTO_CARP);
232554bfbd51SWill Andrews 		pf_proto_unregister(PF_INET6, IPPROTO_CARP, SOCK_RAW);
232654bfbd51SWill Andrews 		proto_reg[CARP_INET6] = -1;
232754bfbd51SWill Andrews 	}
232854bfbd51SWill Andrews 	carp_iamatch6_p = NULL;
232954bfbd51SWill Andrews 	carp_macmatch6_p = NULL;
233054bfbd51SWill Andrews #endif
233154bfbd51SWill Andrews 	carp_linkstate_p = NULL;
233254bfbd51SWill Andrews 	carp_forus_p = NULL;
233354bfbd51SWill Andrews 	carp_output_p = NULL;
233454bfbd51SWill Andrews 	mtx_destroy(&carp_mtx);
233554bfbd51SWill Andrews }
233654bfbd51SWill Andrews 
233754bfbd51SWill Andrews static int
233854bfbd51SWill Andrews carp_mod_load(void)
233954bfbd51SWill Andrews {
234015249f73SWill Andrews 	int err;
234154bfbd51SWill Andrews 
23420fa08018SGleb Smirnoff 	if_detach_event_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
23430fa08018SGleb Smirnoff 		carp_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
23440fa08018SGleb Smirnoff 	if (if_detach_event_tag == NULL)
23450fa08018SGleb Smirnoff 		return (ENOMEM);
2346d92d54d5SGleb Smirnoff 	mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
2347a9771948SGleb Smirnoff 	LIST_INIT(&carpif_list);
2348a9771948SGleb Smirnoff 	if_clone_attach(&carp_cloner);
234954bfbd51SWill Andrews 	carp_linkstate_p = carp_carpdev_state;
235054bfbd51SWill Andrews 	carp_forus_p = carp_forus;
235154bfbd51SWill Andrews 	carp_output_p = carp_output;
235254bfbd51SWill Andrews #ifdef INET6
235354bfbd51SWill Andrews 	carp_iamatch6_p = carp_iamatch6;
235454bfbd51SWill Andrews 	carp_macmatch6_p = carp_macmatch6;
235554bfbd51SWill Andrews 	proto_reg[CARP_INET6] = pf_proto_register(PF_INET6,
235654bfbd51SWill Andrews 	    (struct protosw *)&in6_carp_protosw);
235754bfbd51SWill Andrews 	if (proto_reg[CARP_INET6] != 0) {
235854bfbd51SWill Andrews 		printf("carp: error %d attaching to PF_INET6\n",
235954bfbd51SWill Andrews 		    proto_reg[CARP_INET6]);
236054bfbd51SWill Andrews 		carp_mod_cleanup();
2361*6baf7a24SGleb Smirnoff 		return (proto_reg[CARP_INET6]);
236254bfbd51SWill Andrews 	}
236315249f73SWill Andrews 	err = ip6proto_register(IPPROTO_CARP);
236415249f73SWill Andrews 	if (err) {
236515249f73SWill Andrews 		printf("carp: error %d registering with INET6\n", err);
236615249f73SWill Andrews 		carp_mod_cleanup();
2367*6baf7a24SGleb Smirnoff 		return (err);
236815249f73SWill Andrews 	}
236954bfbd51SWill Andrews #endif
237054bfbd51SWill Andrews #ifdef INET
237154bfbd51SWill Andrews 	carp_iamatch_p = carp_iamatch;
237254bfbd51SWill Andrews 	proto_reg[CARP_INET] = pf_proto_register(PF_INET, &in_carp_protosw);
237354bfbd51SWill Andrews 	if (proto_reg[CARP_INET] != 0) {
237454bfbd51SWill Andrews 		printf("carp: error %d attaching to PF_INET\n",
237554bfbd51SWill Andrews 		    proto_reg[CARP_INET]);
237654bfbd51SWill Andrews 		carp_mod_cleanup();
2377*6baf7a24SGleb Smirnoff 		return (proto_reg[CARP_INET]);
237854bfbd51SWill Andrews 	}
237915249f73SWill Andrews 	err = ipproto_register(IPPROTO_CARP);
238015249f73SWill Andrews 	if (err) {
238115249f73SWill Andrews 		printf("carp: error %d registering with INET\n", err);
238215249f73SWill Andrews 		carp_mod_cleanup();
2383*6baf7a24SGleb Smirnoff 		return (err);
238415249f73SWill Andrews 	}
238554bfbd51SWill Andrews #endif
238654bfbd51SWill Andrews 	return 0;
238754bfbd51SWill Andrews }
2388a9771948SGleb Smirnoff 
238954bfbd51SWill Andrews static int
239054bfbd51SWill Andrews carp_modevent(module_t mod, int type, void *data)
239154bfbd51SWill Andrews {
239254bfbd51SWill Andrews 	switch (type) {
239354bfbd51SWill Andrews 	case MOD_LOAD:
239454bfbd51SWill Andrews 		return carp_mod_load();
239554bfbd51SWill Andrews 		/* NOTREACHED */
2396a9771948SGleb Smirnoff 	case MOD_UNLOAD:
239754bfbd51SWill Andrews 		/*
239854bfbd51SWill Andrews 		 * XXX: For now, disallow module unloading by default due to
239954bfbd51SWill Andrews 		 * a race condition where a thread may dereference one of the
240054bfbd51SWill Andrews 		 * function pointer hooks after the module has been
240154bfbd51SWill Andrews 		 * unloaded, during processing of a packet, causing a panic.
240254bfbd51SWill Andrews 		 */
240354bfbd51SWill Andrews #ifdef CARPMOD_CAN_UNLOAD
240454bfbd51SWill Andrews 		carp_mod_cleanup();
240554bfbd51SWill Andrews #else
240654bfbd51SWill Andrews 		return (EBUSY);
240754bfbd51SWill Andrews #endif
2408a9771948SGleb Smirnoff 		break;
2409a9771948SGleb Smirnoff 
2410a9771948SGleb Smirnoff 	default:
24110fa08018SGleb Smirnoff 		return (EINVAL);
2412a9771948SGleb Smirnoff 	}
2413a9771948SGleb Smirnoff 
24140fa08018SGleb Smirnoff 	return (0);
2415a9771948SGleb Smirnoff }
2416a9771948SGleb Smirnoff 
2417a9771948SGleb Smirnoff static moduledata_t carp_mod = {
2418a9771948SGleb Smirnoff 	"carp",
2419a9771948SGleb Smirnoff 	carp_modevent,
2420a9771948SGleb Smirnoff 	0
2421a9771948SGleb Smirnoff };
2422a9771948SGleb Smirnoff 
2423e24fa11dSWill Andrews DECLARE_MODULE(carp, carp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
2424