ip_carp.c revision 236310
1228571Sglebius/*-
2228571Sglebius * Copyright (c) 2002 Michael Shalayeff.
3228571Sglebius * Copyright (c) 2003 Ryan McBride.
4228571Sglebius * Copyright (c) 2011 Gleb Smirnoff <glebius@FreeBSD.org>
5228571Sglebius * All rights reserved.
6142215Sglebius *
7142215Sglebius * Redistribution and use in source and binary forms, with or without
8142215Sglebius * modification, are permitted provided that the following conditions
9142215Sglebius * are met:
10142215Sglebius * 1. Redistributions of source code must retain the above copyright
11142215Sglebius *    notice, this list of conditions and the following disclaimer.
12142215Sglebius * 2. Redistributions in binary form must reproduce the above copyright
13142215Sglebius *    notice, this list of conditions and the following disclaimer in the
14142215Sglebius *    documentation and/or other materials provided with the distribution.
15142215Sglebius *
16142215Sglebius * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17142215Sglebius * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18142215Sglebius * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19142215Sglebius * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
20142215Sglebius * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21142215Sglebius * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22142215Sglebius * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23142215Sglebius * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24142215Sglebius * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25142215Sglebius * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26142215Sglebius * THE POSSIBILITY OF SUCH DAMAGE.
27142215Sglebius */
28142215Sglebius
29172467Ssilby#include <sys/cdefs.h>
30172467Ssilby__FBSDID("$FreeBSD: head/sys/netinet/ip_carp.c 236310 2012-05-30 13:51:00Z glebius $");
31172467Ssilby
32142215Sglebius#include "opt_bpf.h"
33142215Sglebius#include "opt_inet.h"
34142215Sglebius#include "opt_inet6.h"
35142215Sglebius
36142215Sglebius#include <sys/param.h>
37142215Sglebius#include <sys/systm.h>
38228571Sglebius#include <sys/bus.h>
39228571Sglebius#include <sys/jail.h>
40142215Sglebius#include <sys/kernel.h>
41142215Sglebius#include <sys/limits.h>
42142215Sglebius#include <sys/malloc.h>
43142215Sglebius#include <sys/mbuf.h>
44142215Sglebius#include <sys/module.h>
45164033Srwatson#include <sys/priv.h>
46142215Sglebius#include <sys/proc.h>
47211157Swill#include <sys/protosw.h>
48228571Sglebius#include <sys/socket.h>
49228571Sglebius#include <sys/sockio.h>
50142215Sglebius#include <sys/sysctl.h>
51142215Sglebius#include <sys/syslog.h>
52228736Sglebius#include <sys/taskqueue.h>
53142215Sglebius
54142215Sglebius#include <net/ethernet.h>
55142215Sglebius#include <net/fddi.h>
56142215Sglebius#include <net/if.h>
57152410Sru#include <net/if_dl.h>
58228571Sglebius#include <net/if_llatbl.h>
59142215Sglebius#include <net/if_types.h>
60228571Sglebius#include <net/iso88025.h>
61142215Sglebius#include <net/route.h>
62196019Srwatson#include <net/vnet.h>
63142215Sglebius
64221130Sbz#if defined(INET) || defined(INET6)
65142215Sglebius#include <netinet/in.h>
66142215Sglebius#include <netinet/in_var.h>
67221130Sbz#include <netinet/ip_carp.h>
68221130Sbz#include <netinet/ip.h>
69221130Sbz#include <machine/in_cksum.h>
70221130Sbz#endif
71221130Sbz#ifdef INET
72142215Sglebius#include <netinet/ip_var.h>
73142215Sglebius#include <netinet/if_ether.h>
74142215Sglebius#endif
75142215Sglebius
76142215Sglebius#ifdef INET6
77142215Sglebius#include <netinet/icmp6.h>
78142215Sglebius#include <netinet/ip6.h>
79211157Swill#include <netinet6/ip6protosw.h>
80228571Sglebius#include <netinet6/in6_var.h>
81142215Sglebius#include <netinet6/ip6_var.h>
82148387Sume#include <netinet6/scope6_var.h>
83142215Sglebius#include <netinet6/nd6.h>
84142215Sglebius#endif
85142215Sglebius
86142215Sglebius#include <crypto/sha1.h>
87142215Sglebius
88228571Sglebiusstatic MALLOC_DEFINE(M_CARP, "CARP", "CARP addresses");
89142215Sglebius
90142215Sglebiusstruct carp_softc {
91228571Sglebius	struct ifnet		*sc_carpdev;	/* Pointer to parent ifnet. */
92228571Sglebius	struct ifaddr		**sc_ifas;	/* Our ifaddrs. */
93228571Sglebius	struct sockaddr_dl	sc_addr;	/* Our link level address. */
94228571Sglebius	struct callout		sc_ad_tmo;	/* Advertising timeout. */
95221130Sbz#ifdef INET
96228571Sglebius	struct callout		sc_md_tmo;	/* Master down timeout. */
97221130Sbz#endif
98142215Sglebius#ifdef INET6
99228571Sglebius	struct callout 		sc_md6_tmo;	/* XXX: Master down timeout. */
100228571Sglebius#endif
101228571Sglebius	struct mtx		sc_mtx;
102142215Sglebius
103228571Sglebius	int			sc_vhid;
104228571Sglebius	int			sc_advskew;
105228571Sglebius	int			sc_advbase;
106228571Sglebius
107228571Sglebius	int			sc_naddrs;
108228571Sglebius	int			sc_naddrs6;
109228571Sglebius	int			sc_ifasiz;
110142215Sglebius	enum { INIT = 0, BACKUP, MASTER }	sc_state;
111228571Sglebius	int			sc_suppress;
112228571Sglebius	int			sc_sendad_errors;
113142215Sglebius#define	CARP_SENDAD_MAX_ERRORS	3
114228571Sglebius	int			sc_sendad_success;
115142215Sglebius#define	CARP_SENDAD_MIN_SUCCESS 3
116142215Sglebius
117228571Sglebius	int			sc_init_counter;
118228571Sglebius	uint64_t		sc_counter;
119142215Sglebius
120142215Sglebius	/* authentication */
121228571Sglebius#define	CARP_HMAC_PAD	64
122142215Sglebius	unsigned char sc_key[CARP_KEY_LEN];
123142215Sglebius	unsigned char sc_pad[CARP_HMAC_PAD];
124142215Sglebius	SHA1_CTX sc_sha1;
125142215Sglebius
126228571Sglebius	TAILQ_ENTRY(carp_softc)	sc_list;	/* On the carp_if list. */
127228571Sglebius	LIST_ENTRY(carp_softc)	sc_next;	/* On the global list. */
128142215Sglebius};
129142215Sglebius
130228571Sglebiusstruct carp_if {
131228571Sglebius#ifdef INET
132228571Sglebius	int	cif_naddrs;
133228571Sglebius#endif
134228571Sglebius#ifdef INET6
135228571Sglebius	int	cif_naddrs6;
136228571Sglebius#endif
137228571Sglebius	TAILQ_HEAD(, carp_softc) cif_vrs;
138228571Sglebius#ifdef INET
139228571Sglebius	struct ip_moptions 	 cif_imo;
140228571Sglebius#endif
141228571Sglebius#ifdef INET6
142228571Sglebius	struct ip6_moptions 	 cif_im6o;
143228571Sglebius#endif
144228571Sglebius	struct ifnet	*cif_ifp;
145228571Sglebius	struct mtx	cif_mtx;
146228571Sglebius};
147228571Sglebius
148228571Sglebius#define	CARP_INET	0
149228571Sglebius#define	CARP_INET6	1
150228571Sglebiusstatic int proto_reg[] = {-1, -1};
151228571Sglebius
152228571Sglebius/*
153228571Sglebius * Brief design of carp(4).
154228571Sglebius *
155228571Sglebius * Any carp-capable ifnet may have a list of carp softcs hanging off
156228571Sglebius * its ifp->if_carp pointer. Each softc represents one unique virtual
157228571Sglebius * host id, or vhid. The softc has a back pointer to the ifnet. All
158228571Sglebius * softcs are joined in a global list, which has quite limited use.
159228571Sglebius *
160228571Sglebius * Any interface address that takes part in CARP negotiation has a
161228571Sglebius * pointer to the softc of its vhid, ifa->ifa_carp. That could be either
162228571Sglebius * AF_INET or AF_INET6 address.
163228571Sglebius *
164228571Sglebius * Although, one can get the softc's backpointer to ifnet and traverse
165228571Sglebius * through its ifp->if_addrhead queue to find all interface addresses
166228571Sglebius * involved in CARP, we keep a growable array of ifaddr pointers. This
167228571Sglebius * allows us to avoid grabbing the IF_ADDR_LOCK() in many traversals that
168228571Sglebius * do calls into the network stack, thus avoiding LORs.
169228571Sglebius *
170228571Sglebius * Locking:
171228571Sglebius *
172228571Sglebius * Each softc has a lock sc_mtx. It is used to synchronise carp_input_c(),
173228571Sglebius * callout-driven events and ioctl()s.
174228571Sglebius *
175228571Sglebius * To traverse the list of softcs on an ifnet we use CIF_LOCK(), to
176228571Sglebius * traverse the global list we use the mutex carp_mtx.
177228571Sglebius *
178228571Sglebius * Known issues with locking:
179228571Sglebius *
180228571Sglebius * - There is no protection for races between two ioctl() requests,
181228571Sglebius *   neither SIOCSVH, nor SIOCAIFADDR & SIOCAIFADDR_IN6. I think that all
182228571Sglebius *   interface ioctl()s should be serialized right in net/if.c.
183228571Sglebius * - Sending ad, we put the pointer to the softc in an mtag, and no reference
184228571Sglebius *   counting is done on the softc.
185228571Sglebius * - On module unload we may race (?) with packet processing thread
186228571Sglebius *   dereferencing our function pointers.
187228571Sglebius */
188228571Sglebius
189228736Sglebiusstatic int carp_allow = 1;		/* Accept incoming CARP packets. */
190228736Sglebiusstatic int carp_preempt = 0;		/* Preempt slower nodes. */
191228736Sglebiusstatic int carp_log = 1;		/* Log level. */
192228736Sglebiusstatic int carp_demotion = 0;		/* Global advskew demotion. */
193228736Sglebiusstatic int carp_senderr_adj = CARP_MAXSKEW;	/* Send error demotion factor */
194228736Sglebiusstatic int carp_ifdown_adj = CARP_MAXSKEW;	/* Iface down demotion factor */
195228736Sglebius
196211157SwillSYSCTL_NODE(_net_inet, IPPROTO_CARP,	carp,	CTLFLAG_RW, 0,	"CARP");
197228736SglebiusSYSCTL_INT(_net_inet_carp, OID_AUTO, allow, CTLFLAG_RW, &carp_allow, 0,
198228736Sglebius    "Accept incoming CARP packets");
199228736SglebiusSYSCTL_INT(_net_inet_carp, OID_AUTO, preempt, CTLFLAG_RW, &carp_preempt, 0,
200228736Sglebius    "High-priority backup preemption mode");
201228736SglebiusSYSCTL_INT(_net_inet_carp, OID_AUTO, log, CTLFLAG_RW, &carp_log, 0,
202228736Sglebius    "CARP log level");
203228736SglebiusSYSCTL_INT(_net_inet_carp, OID_AUTO, demotion, CTLFLAG_RW, &carp_demotion, 0,
204228736Sglebius    "Demotion factor (skew of advskew)");
205228736SglebiusSYSCTL_INT(_net_inet_carp, OID_AUTO, senderr_demotion_factor, CTLFLAG_RW,
206228736Sglebius    &carp_senderr_adj, 0, "Send error demotion factor adjustment");
207228736SglebiusSYSCTL_INT(_net_inet_carp, OID_AUTO, ifdown_demotion_factor, CTLFLAG_RW,
208228736Sglebius    &carp_ifdown_adj, 0, "Interface down demotion factor adjustment");
209142215Sglebius
210228736Sglebiusstatic struct carpstats carpstats;
211228736SglebiusSYSCTL_STRUCT(_net_inet_carp, OID_AUTO, stats, CTLFLAG_RW, &carpstats,
212228736Sglebius    carpstats, "CARP statistics (struct carpstats, netinet/ip_carp.h)");
213142215Sglebius
214228571Sglebius#define	CARP_LOCK_INIT(sc)	mtx_init(&(sc)->sc_mtx, "carp_softc",   \
215142215Sglebius	NULL, MTX_DEF)
216228571Sglebius#define	CARP_LOCK_DESTROY(sc)	mtx_destroy(&(sc)->sc_mtx)
217228571Sglebius#define	CARP_LOCK_ASSERT(sc)	mtx_assert(&(sc)->sc_mtx, MA_OWNED)
218228571Sglebius#define	CARP_LOCK(sc)		mtx_lock(&(sc)->sc_mtx)
219228571Sglebius#define	CARP_UNLOCK(sc)		mtx_unlock(&(sc)->sc_mtx)
220228571Sglebius#define	CIF_LOCK_INIT(cif)	mtx_init(&(cif)->cif_mtx, "carp_if",   \
221228571Sglebius	NULL, MTX_DEF)
222228571Sglebius#define	CIF_LOCK_DESTROY(cif)	mtx_destroy(&(cif)->cif_mtx)
223228571Sglebius#define	CIF_LOCK_ASSERT(cif)	mtx_assert(&(cif)->cif_mtx, MA_OWNED)
224228571Sglebius#define	CIF_LOCK(cif)		mtx_lock(&(cif)->cif_mtx)
225228571Sglebius#define	CIF_UNLOCK(cif)		mtx_unlock(&(cif)->cif_mtx)
226234130Sglebius#define	CIF_FREE(cif)	do {				\
227234130Sglebius		CIF_LOCK_ASSERT(cif);			\
228234130Sglebius		if (TAILQ_EMPTY(&(cif)->cif_vrs))	\
229234130Sglebius			carp_free_if(cif);		\
230234130Sglebius		else					\
231234130Sglebius			CIF_UNLOCK(cif);		\
232234130Sglebius} while (0)
233142215Sglebius
234142451Sglebius#define	CARP_LOG(...)	do {				\
235228736Sglebius	if (carp_log > 0)				\
236228571Sglebius		log(LOG_INFO, "carp: " __VA_ARGS__);	\
237142451Sglebius} while (0)
238142215Sglebius
239142451Sglebius#define	CARP_DEBUG(...)	do {				\
240228736Sglebius	if (carp_log > 1)				\
241142446Sglebius		log(LOG_DEBUG, __VA_ARGS__);		\
242142451Sglebius} while (0)
243142446Sglebius
244228571Sglebius#define	IFNET_FOREACH_IFA(ifp, ifa)					\
245228571Sglebius	IF_ADDR_LOCK_ASSERT(ifp);					\
246228571Sglebius	TAILQ_FOREACH((ifa), &(ifp)->if_addrhead, ifa_link)		\
247228571Sglebius		if ((ifa)->ifa_carp != NULL)
248228571Sglebius
249228571Sglebius#define	CARP_FOREACH_IFA(sc, ifa)					\
250228571Sglebius	CARP_LOCK_ASSERT(sc);						\
251228571Sglebius	for (int _i = 0;						\
252228571Sglebius		_i < (sc)->sc_naddrs + (sc)->sc_naddrs6 &&		\
253228571Sglebius		((ifa) = sc->sc_ifas[_i]) != NULL;			\
254228571Sglebius		++_i)
255228571Sglebius
256228571Sglebius#define	IFNET_FOREACH_CARP(ifp, sc)					\
257228571Sglebius	CIF_LOCK_ASSERT(ifp->if_carp);					\
258228571Sglebius	TAILQ_FOREACH((sc), &(ifp)->if_carp->cif_vrs, sc_list)
259228571Sglebius
260228736Sglebius#define	DEMOTE_ADVSKEW(sc)					\
261228736Sglebius    (((sc)->sc_advskew + carp_demotion > CARP_MAXSKEW) ?	\
262228736Sglebius    CARP_MAXSKEW : ((sc)->sc_advskew + carp_demotion))
263228736Sglebius
264142559Sglebiusstatic void	carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
265228571Sglebiusstatic struct carp_softc
266228571Sglebius		*carp_alloc(struct ifnet *);
267234130Sglebiusstatic void	carp_detach_locked(struct ifaddr *);
268228571Sglebiusstatic void	carp_destroy(struct carp_softc *);
269228571Sglebiusstatic struct carp_if
270228571Sglebius		*carp_alloc_if(struct ifnet *);
271228571Sglebiusstatic void	carp_free_if(struct carp_if *);
272228571Sglebiusstatic void	carp_set_state(struct carp_softc *, int);
273228571Sglebiusstatic void	carp_sc_state(struct carp_softc *);
274228571Sglebiusstatic void	carp_setrun(struct carp_softc *, sa_family_t);
275228571Sglebiusstatic void	carp_master_down(void *);
276228571Sglebiusstatic void	carp_master_down_locked(struct carp_softc *);
277142559Sglebiusstatic void	carp_send_ad(void *);
278142914Sglebiusstatic void	carp_send_ad_locked(struct carp_softc *);
279228571Sglebiusstatic void	carp_addroute(struct carp_softc *);
280230863Sglebiusstatic void	carp_ifa_addroute(struct ifaddr *);
281228571Sglebiusstatic void	carp_delroute(struct carp_softc *);
282230863Sglebiusstatic void	carp_ifa_delroute(struct ifaddr *);
283228736Sglebiusstatic void	carp_send_ad_all(void *, int);
284228736Sglebiusstatic void	carp_demote_adj(int, char *);
285142215Sglebius
286228571Sglebiusstatic LIST_HEAD(, carp_softc) carp_list;
287142911Sglebiusstatic struct mtx carp_mtx;
288228736Sglebiusstatic struct task carp_sendall_task =
289228736Sglebius    TASK_INITIALIZER(0, carp_send_ad_all, NULL);
290142215Sglebius
291228571Sglebiusstatic __inline uint16_t
292142215Sglebiuscarp_cksum(struct mbuf *m, int len)
293142215Sglebius{
294142215Sglebius	return (in_cksum(m, len));
295142215Sglebius}
296142215Sglebius
297142559Sglebiusstatic void
298142215Sglebiuscarp_hmac_prepare(struct carp_softc *sc)
299142215Sglebius{
300228571Sglebius	uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
301228571Sglebius	uint8_t vhid = sc->sc_vhid & 0xff;
302142215Sglebius	struct ifaddr *ifa;
303179490Smlaier	int i, found;
304179490Smlaier#ifdef INET
305179490Smlaier	struct in_addr last, cur, in;
306179490Smlaier#endif
307142215Sglebius#ifdef INET6
308179490Smlaier	struct in6_addr last6, cur6, in6;
309142215Sglebius#endif
310142215Sglebius
311228571Sglebius	CARP_LOCK_ASSERT(sc);
312142914Sglebius
313228571Sglebius	/* Compute ipad from key. */
314142215Sglebius	bzero(sc->sc_pad, sizeof(sc->sc_pad));
315142215Sglebius	bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
316142215Sglebius	for (i = 0; i < sizeof(sc->sc_pad); i++)
317142215Sglebius		sc->sc_pad[i] ^= 0x36;
318142215Sglebius
319228571Sglebius	/* Precompute first part of inner hash. */
320142215Sglebius	SHA1Init(&sc->sc_sha1);
321142215Sglebius	SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
322142215Sglebius	SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
323142215Sglebius	SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
324142215Sglebius	SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
325142215Sglebius#ifdef INET
326179490Smlaier	cur.s_addr = 0;
327179490Smlaier	do {
328179490Smlaier		found = 0;
329179490Smlaier		last = cur;
330179490Smlaier		cur.s_addr = 0xffffffff;
331228571Sglebius		CARP_FOREACH_IFA(sc, ifa) {
332179490Smlaier			in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr;
333179490Smlaier			if (ifa->ifa_addr->sa_family == AF_INET &&
334179490Smlaier			    ntohl(in.s_addr) > ntohl(last.s_addr) &&
335179490Smlaier			    ntohl(in.s_addr) < ntohl(cur.s_addr)) {
336179490Smlaier				cur.s_addr = in.s_addr;
337179490Smlaier				found++;
338179490Smlaier			}
339179490Smlaier		}
340179490Smlaier		if (found)
341179490Smlaier			SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur));
342179490Smlaier	} while (found);
343142215Sglebius#endif /* INET */
344142215Sglebius#ifdef INET6
345179490Smlaier	memset(&cur6, 0, sizeof(cur6));
346179490Smlaier	do {
347179490Smlaier		found = 0;
348179490Smlaier		last6 = cur6;
349179490Smlaier		memset(&cur6, 0xff, sizeof(cur6));
350228571Sglebius		CARP_FOREACH_IFA(sc, ifa) {
351142215Sglebius			in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
352179490Smlaier			if (IN6_IS_SCOPE_EMBED(&in6))
353179490Smlaier				in6.s6_addr16[1] = 0;
354179490Smlaier			if (ifa->ifa_addr->sa_family == AF_INET6 &&
355179490Smlaier			    memcmp(&in6, &last6, sizeof(in6)) > 0 &&
356179490Smlaier			    memcmp(&in6, &cur6, sizeof(in6)) < 0) {
357179490Smlaier				cur6 = in6;
358179490Smlaier				found++;
359179490Smlaier			}
360142215Sglebius		}
361179490Smlaier		if (found)
362179490Smlaier			SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6));
363179490Smlaier	} while (found);
364142215Sglebius#endif /* INET6 */
365142215Sglebius
366142215Sglebius	/* convert ipad to opad */
367142215Sglebius	for (i = 0; i < sizeof(sc->sc_pad); i++)
368142215Sglebius		sc->sc_pad[i] ^= 0x36 ^ 0x5c;
369142215Sglebius}
370142215Sglebius
371142559Sglebiusstatic void
372228571Sglebiuscarp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
373142215Sglebius    unsigned char md[20])
374142215Sglebius{
375142215Sglebius	SHA1_CTX sha1ctx;
376142215Sglebius
377228571Sglebius	CARP_LOCK_ASSERT(sc);
378228571Sglebius
379142215Sglebius	/* fetch first half of inner hash */
380142215Sglebius	bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
381142215Sglebius
382142215Sglebius	SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
383142215Sglebius	SHA1Final(md, &sha1ctx);
384142215Sglebius
385142215Sglebius	/* outer hash */
386142215Sglebius	SHA1Init(&sha1ctx);
387142215Sglebius	SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
388142215Sglebius	SHA1Update(&sha1ctx, md, 20);
389142215Sglebius	SHA1Final(md, &sha1ctx);
390142215Sglebius}
391142215Sglebius
392142559Sglebiusstatic int
393228571Sglebiuscarp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
394142215Sglebius    unsigned char md[20])
395142215Sglebius{
396142215Sglebius	unsigned char md2[20];
397142215Sglebius
398228571Sglebius	CARP_LOCK_ASSERT(sc);
399142914Sglebius
400142215Sglebius	carp_hmac_generate(sc, counter, md2);
401142215Sglebius
402142215Sglebius	return (bcmp(md, md2, sizeof(md2)));
403142215Sglebius}
404142215Sglebius
405166423Sglebius/*
406142215Sglebius * process input packet.
407142215Sglebius * we have rearranged checks order compared to the rfc,
408142215Sglebius * but it seems more efficient this way or not possible otherwise.
409142215Sglebius */
410221130Sbz#ifdef INET
411142215Sglebiusvoid
412142215Sglebiuscarp_input(struct mbuf *m, int hlen)
413142215Sglebius{
414142215Sglebius	struct ip *ip = mtod(m, struct ip *);
415142215Sglebius	struct carp_header *ch;
416142215Sglebius	int iplen, len;
417142215Sglebius
418190968Srwatson	CARPSTATS_INC(carps_ipackets);
419142215Sglebius
420228736Sglebius	if (!carp_allow) {
421142215Sglebius		m_freem(m);
422142215Sglebius		return;
423142215Sglebius	}
424142215Sglebius
425142215Sglebius	/* verify that the IP TTL is 255.  */
426142215Sglebius	if (ip->ip_ttl != CARP_DFLTTL) {
427190968Srwatson		CARPSTATS_INC(carps_badttl);
428228571Sglebius		CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
429142446Sglebius		    ip->ip_ttl,
430142446Sglebius		    m->m_pkthdr.rcvif->if_xname);
431142215Sglebius		m_freem(m);
432142215Sglebius		return;
433142215Sglebius	}
434142215Sglebius
435142215Sglebius	iplen = ip->ip_hl << 2;
436142215Sglebius
437142215Sglebius	if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
438190968Srwatson		CARPSTATS_INC(carps_badlen);
439228571Sglebius		CARP_DEBUG("%s: received len %zd < sizeof(struct carp_header) "
440228571Sglebius		    "on %s\n", __func__, m->m_len - sizeof(struct ip),
441195976Sdelphij		    m->m_pkthdr.rcvif->if_xname);
442142215Sglebius		m_freem(m);
443142215Sglebius		return;
444142215Sglebius	}
445142215Sglebius
446142215Sglebius	if (iplen + sizeof(*ch) < m->m_len) {
447142215Sglebius		if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
448190968Srwatson			CARPSTATS_INC(carps_hdrops);
449228571Sglebius			CARP_DEBUG("%s: pullup failed\n", __func__);
450142215Sglebius			return;
451142215Sglebius		}
452142215Sglebius		ip = mtod(m, struct ip *);
453142215Sglebius	}
454142215Sglebius	ch = (struct carp_header *)((char *)ip + iplen);
455142215Sglebius
456142215Sglebius	/*
457142215Sglebius	 * verify that the received packet length is
458142215Sglebius	 * equal to the CARP header
459142215Sglebius	 */
460142215Sglebius	len = iplen + sizeof(*ch);
461142215Sglebius	if (len > m->m_pkthdr.len) {
462190968Srwatson		CARPSTATS_INC(carps_badlen);
463228571Sglebius		CARP_DEBUG("%s: packet too short %d on %s\n", __func__,
464142446Sglebius		    m->m_pkthdr.len,
465142446Sglebius		    m->m_pkthdr.rcvif->if_xname);
466142215Sglebius		m_freem(m);
467142215Sglebius		return;
468142215Sglebius	}
469142215Sglebius
470142215Sglebius	if ((m = m_pullup(m, len)) == NULL) {
471190968Srwatson		CARPSTATS_INC(carps_hdrops);
472142215Sglebius		return;
473142215Sglebius	}
474142215Sglebius	ip = mtod(m, struct ip *);
475142215Sglebius	ch = (struct carp_header *)((char *)ip + iplen);
476142215Sglebius
477142215Sglebius	/* verify the CARP checksum */
478142215Sglebius	m->m_data += iplen;
479142215Sglebius	if (carp_cksum(m, len - iplen)) {
480190968Srwatson		CARPSTATS_INC(carps_badsum);
481228571Sglebius		CARP_DEBUG("%s: checksum failed on %s\n", __func__,
482142446Sglebius		    m->m_pkthdr.rcvif->if_xname);
483142215Sglebius		m_freem(m);
484142215Sglebius		return;
485142215Sglebius	}
486142215Sglebius	m->m_data -= iplen;
487142215Sglebius
488142446Sglebius	carp_input_c(m, ch, AF_INET);
489142215Sglebius}
490221130Sbz#endif
491142215Sglebius
492142215Sglebius#ifdef INET6
493142215Sglebiusint
494142215Sglebiuscarp6_input(struct mbuf **mp, int *offp, int proto)
495142215Sglebius{
496142215Sglebius	struct mbuf *m = *mp;
497142215Sglebius	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
498142215Sglebius	struct carp_header *ch;
499142215Sglebius	u_int len;
500142215Sglebius
501190968Srwatson	CARPSTATS_INC(carps_ipackets6);
502142215Sglebius
503228736Sglebius	if (!carp_allow) {
504142215Sglebius		m_freem(m);
505142215Sglebius		return (IPPROTO_DONE);
506142215Sglebius	}
507142215Sglebius
508142215Sglebius	/* check if received on a valid carp interface */
509142215Sglebius	if (m->m_pkthdr.rcvif->if_carp == NULL) {
510190968Srwatson		CARPSTATS_INC(carps_badif);
511228571Sglebius		CARP_DEBUG("%s: packet received on non-carp interface: %s\n",
512228571Sglebius		    __func__, m->m_pkthdr.rcvif->if_xname);
513142215Sglebius		m_freem(m);
514142215Sglebius		return (IPPROTO_DONE);
515142215Sglebius	}
516142215Sglebius
517142215Sglebius	/* verify that the IP TTL is 255 */
518142215Sglebius	if (ip6->ip6_hlim != CARP_DFLTTL) {
519190968Srwatson		CARPSTATS_INC(carps_badttl);
520228571Sglebius		CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
521228571Sglebius		    ip6->ip6_hlim, m->m_pkthdr.rcvif->if_xname);
522142215Sglebius		m_freem(m);
523142215Sglebius		return (IPPROTO_DONE);
524142215Sglebius	}
525142215Sglebius
526142215Sglebius	/* verify that we have a complete carp packet */
527142215Sglebius	len = m->m_len;
528142215Sglebius	IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
529142215Sglebius	if (ch == NULL) {
530190968Srwatson		CARPSTATS_INC(carps_badlen);
531228571Sglebius		CARP_DEBUG("%s: packet size %u too small\n", __func__, len);
532142215Sglebius		return (IPPROTO_DONE);
533142215Sglebius	}
534142215Sglebius
535142215Sglebius
536142215Sglebius	/* verify the CARP checksum */
537142215Sglebius	m->m_data += *offp;
538142215Sglebius	if (carp_cksum(m, sizeof(*ch))) {
539190968Srwatson		CARPSTATS_INC(carps_badsum);
540228571Sglebius		CARP_DEBUG("%s: checksum failed, on %s\n", __func__,
541142446Sglebius		    m->m_pkthdr.rcvif->if_xname);
542142215Sglebius		m_freem(m);
543142215Sglebius		return (IPPROTO_DONE);
544142215Sglebius	}
545142215Sglebius	m->m_data -= *offp;
546142215Sglebius
547142446Sglebius	carp_input_c(m, ch, AF_INET6);
548142215Sglebius	return (IPPROTO_DONE);
549142215Sglebius}
550142215Sglebius#endif /* INET6 */
551142215Sglebius
552142559Sglebiusstatic void
553142446Sglebiuscarp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
554142215Sglebius{
555142215Sglebius	struct ifnet *ifp = m->m_pkthdr.rcvif;
556228571Sglebius	struct ifaddr *ifa;
557142446Sglebius	struct carp_softc *sc;
558228571Sglebius	uint64_t tmp_counter;
559142215Sglebius	struct timeval sc_tv, ch_tv;
560142215Sglebius
561142215Sglebius	/* verify that the VHID is valid on the receiving interface */
562229621Sjhb	IF_ADDR_RLOCK(ifp);
563228571Sglebius	IFNET_FOREACH_IFA(ifp, ifa)
564228571Sglebius		if (ifa->ifa_addr->sa_family == af &&
565228571Sglebius		    ifa->ifa_carp->sc_vhid == ch->carp_vhid) {
566228571Sglebius			ifa_ref(ifa);
567142215Sglebius			break;
568228571Sglebius		}
569229621Sjhb	IF_ADDR_RUNLOCK(ifp);
570142914Sglebius
571228571Sglebius	if (ifa == NULL) {
572190968Srwatson		CARPSTATS_INC(carps_badvhid);
573142215Sglebius		m_freem(m);
574142215Sglebius		return;
575142215Sglebius	}
576142215Sglebius
577142215Sglebius	/* verify the CARP version. */
578142215Sglebius	if (ch->carp_version != CARP_VERSION) {
579190968Srwatson		CARPSTATS_INC(carps_badver);
580228571Sglebius		CARP_DEBUG("%s: invalid version %d\n", ifp->if_xname,
581142446Sglebius		    ch->carp_version);
582228571Sglebius		ifa_free(ifa);
583142215Sglebius		m_freem(m);
584142215Sglebius		return;
585142215Sglebius	}
586142215Sglebius
587228571Sglebius	sc = ifa->ifa_carp;
588228571Sglebius	CARP_LOCK(sc);
589228571Sglebius	ifa_free(ifa);
590228571Sglebius
591142215Sglebius	if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
592190968Srwatson		CARPSTATS_INC(carps_badauth);
593228571Sglebius		CARP_DEBUG("%s: incorrect hash for VHID %u@%s\n", __func__,
594228571Sglebius		    sc->sc_vhid, ifp->if_xname);
595228571Sglebius		goto out;
596142215Sglebius	}
597142215Sglebius
598142215Sglebius	tmp_counter = ntohl(ch->carp_counter[0]);
599142215Sglebius	tmp_counter = tmp_counter<<32;
600142215Sglebius	tmp_counter += ntohl(ch->carp_counter[1]);
601142215Sglebius
602142215Sglebius	/* XXX Replay protection goes here */
603142215Sglebius
604142215Sglebius	sc->sc_init_counter = 0;
605142215Sglebius	sc->sc_counter = tmp_counter;
606142215Sglebius
607142215Sglebius	sc_tv.tv_sec = sc->sc_advbase;
608228736Sglebius	sc_tv.tv_usec = DEMOTE_ADVSKEW(sc) * 1000000 / 256;
609142215Sglebius	ch_tv.tv_sec = ch->carp_advbase;
610142215Sglebius	ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
611142215Sglebius
612142215Sglebius	switch (sc->sc_state) {
613142215Sglebius	case INIT:
614142215Sglebius		break;
615142215Sglebius	case MASTER:
616142215Sglebius		/*
617142215Sglebius		 * If we receive an advertisement from a master who's going to
618142215Sglebius		 * be more frequent than us, go into BACKUP state.
619142215Sglebius		 */
620142215Sglebius		if (timevalcmp(&sc_tv, &ch_tv, >) ||
621142215Sglebius		    timevalcmp(&sc_tv, &ch_tv, ==)) {
622142215Sglebius			callout_stop(&sc->sc_ad_tmo);
623228571Sglebius			CARP_LOG("VHID %u@%s: MASTER -> BACKUP "
624228571Sglebius			    "(more frequent advertisement received)\n",
625228571Sglebius			    sc->sc_vhid,
626228571Sglebius			    sc->sc_carpdev->if_xname);
627142215Sglebius			carp_set_state(sc, BACKUP);
628142215Sglebius			carp_setrun(sc, 0);
629228571Sglebius			carp_delroute(sc);
630142215Sglebius		}
631142215Sglebius		break;
632142215Sglebius	case BACKUP:
633142215Sglebius		/*
634142215Sglebius		 * If we're pre-empting masters who advertise slower than us,
635142215Sglebius		 * and this one claims to be slower, treat him as down.
636142215Sglebius		 */
637228736Sglebius		if (carp_preempt && timevalcmp(&sc_tv, &ch_tv, <)) {
638228571Sglebius			CARP_LOG("VHID %u@%s: BACKUP -> MASTER "
639142452Sglebius			    "(preempting a slower master)\n",
640228571Sglebius			    sc->sc_vhid,
641228571Sglebius			    sc->sc_carpdev->if_xname);
642142914Sglebius			carp_master_down_locked(sc);
643142215Sglebius			break;
644142215Sglebius		}
645142215Sglebius
646142215Sglebius		/*
647142215Sglebius		 *  If the master is going to advertise at such a low frequency
648142215Sglebius		 *  that he's guaranteed to time out, we'd might as well just
649142215Sglebius		 *  treat him as timed out now.
650142215Sglebius		 */
651142215Sglebius		sc_tv.tv_sec = sc->sc_advbase * 3;
652142215Sglebius		if (timevalcmp(&sc_tv, &ch_tv, <)) {
653228571Sglebius			CARP_LOG("VHID %u@%s: BACKUP -> MASTER "
654142452Sglebius			    "(master timed out)\n",
655228571Sglebius			    sc->sc_vhid,
656228571Sglebius			    sc->sc_carpdev->if_xname);
657142914Sglebius			carp_master_down_locked(sc);
658142215Sglebius			break;
659142215Sglebius		}
660142215Sglebius
661142215Sglebius		/*
662142215Sglebius		 * Otherwise, we reset the counter and wait for the next
663142215Sglebius		 * advertisement.
664142215Sglebius		 */
665142215Sglebius		carp_setrun(sc, af);
666142215Sglebius		break;
667142215Sglebius	}
668142215Sglebius
669228571Sglebiusout:
670228571Sglebius	CARP_UNLOCK(sc);
671142215Sglebius	m_freem(m);
672142215Sglebius}
673142215Sglebius
674142559Sglebiusstatic int
675142215Sglebiuscarp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
676142215Sglebius{
677142215Sglebius	struct m_tag *mtag;
678142215Sglebius
679142215Sglebius	if (sc->sc_init_counter) {
680142215Sglebius		/* this could also be seconds since unix epoch */
681142215Sglebius		sc->sc_counter = arc4random();
682142215Sglebius		sc->sc_counter = sc->sc_counter << 32;
683142215Sglebius		sc->sc_counter += arc4random();
684142215Sglebius	} else
685142215Sglebius		sc->sc_counter++;
686142215Sglebius
687142215Sglebius	ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
688142215Sglebius	ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
689142215Sglebius
690142215Sglebius	carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
691142215Sglebius
692142215Sglebius	/* Tag packet for carp_output */
693228571Sglebius	if ((mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct carp_softc *),
694228571Sglebius	    M_NOWAIT)) == NULL) {
695142215Sglebius		m_freem(m);
696228571Sglebius		CARPSTATS_INC(carps_onomem);
697142215Sglebius		return (ENOMEM);
698142215Sglebius	}
699236310Sglebius	bcopy(&sc, mtag + 1, sizeof(sc));
700142215Sglebius	m_tag_prepend(m, mtag);
701142215Sglebius
702142215Sglebius	return (0);
703142215Sglebius}
704142215Sglebius
705228736Sglebius/*
706228736Sglebius * To avoid LORs and possible recursions this function shouldn't
707228736Sglebius * be called directly, but scheduled via taskqueue.
708228736Sglebius */
709142559Sglebiusstatic void
710228736Sglebiuscarp_send_ad_all(void *ctx __unused, int pending __unused)
711142215Sglebius{
712142911Sglebius	struct carp_softc *sc;
713142215Sglebius
714142911Sglebius	mtx_lock(&carp_mtx);
715228571Sglebius	LIST_FOREACH(sc, &carp_list, sc_next)
716228736Sglebius		if (sc->sc_state == MASTER) {
717228571Sglebius			CARP_LOCK(sc);
718231201Sglebius			CURVNET_SET(sc->sc_carpdev->if_vnet);
719142914Sglebius			carp_send_ad_locked(sc);
720231201Sglebius			CURVNET_RESTORE();
721228571Sglebius			CARP_UNLOCK(sc);
722228571Sglebius		}
723142911Sglebius	mtx_unlock(&carp_mtx);
724142215Sglebius}
725142215Sglebius
726231201Sglebius/* Send a periodic advertisement, executed in callout context. */
727142559Sglebiusstatic void
728142215Sglebiuscarp_send_ad(void *v)
729142215Sglebius{
730142914Sglebius	struct carp_softc *sc = v;
731142914Sglebius
732228571Sglebius	CARP_LOCK_ASSERT(sc);
733231201Sglebius	CURVNET_SET(sc->sc_carpdev->if_vnet);
734142914Sglebius	carp_send_ad_locked(sc);
735231201Sglebius	CURVNET_RESTORE();
736228571Sglebius	CARP_UNLOCK(sc);
737142914Sglebius}
738142914Sglebius
739142914Sglebiusstatic void
740142914Sglebiuscarp_send_ad_locked(struct carp_softc *sc)
741142914Sglebius{
742142215Sglebius	struct carp_header ch;
743142215Sglebius	struct timeval tv;
744228571Sglebius	struct sockaddr sa;
745228571Sglebius	struct ifaddr *ifa;
746142215Sglebius	struct carp_header *ch_ptr;
747142215Sglebius	struct mbuf *m;
748228571Sglebius	int len, advskew;
749142215Sglebius
750228571Sglebius	CARP_LOCK_ASSERT(sc);
751142914Sglebius
752228736Sglebius	advskew = DEMOTE_ADVSKEW(sc);
753228571Sglebius	tv.tv_sec = sc->sc_advbase;
754228571Sglebius	tv.tv_usec = advskew * 1000000 / 256;
755142215Sglebius
756142215Sglebius	ch.carp_version = CARP_VERSION;
757142215Sglebius	ch.carp_type = CARP_ADVERTISEMENT;
758142215Sglebius	ch.carp_vhid = sc->sc_vhid;
759228571Sglebius	ch.carp_advbase = sc->sc_advbase;
760142215Sglebius	ch.carp_advskew = advskew;
761142215Sglebius	ch.carp_authlen = 7;	/* XXX DEFINE */
762142215Sglebius	ch.carp_pad1 = 0;	/* must be zero */
763142215Sglebius	ch.carp_cksum = 0;
764142215Sglebius
765228571Sglebius	/* XXXGL: OpenBSD picks first ifaddr with needed family. */
766228571Sglebius
767142215Sglebius#ifdef INET
768228571Sglebius	if (sc->sc_naddrs) {
769142215Sglebius		struct ip *ip;
770142215Sglebius
771228571Sglebius		MGETHDR(m, M_NOWAIT, MT_HEADER);
772142215Sglebius		if (m == NULL) {
773190968Srwatson			CARPSTATS_INC(carps_onomem);
774142215Sglebius			/* XXX maybe less ? */
775228571Sglebius			callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
776228571Sglebius			    carp_send_ad, sc);
777142215Sglebius			return;
778142215Sglebius		}
779142215Sglebius		len = sizeof(*ip) + sizeof(ch);
780142215Sglebius		m->m_pkthdr.len = len;
781142215Sglebius		m->m_pkthdr.rcvif = NULL;
782142215Sglebius		m->m_len = len;
783142215Sglebius		MH_ALIGN(m, m->m_len);
784142215Sglebius		m->m_flags |= M_MCAST;
785142215Sglebius		ip = mtod(m, struct ip *);
786142215Sglebius		ip->ip_v = IPVERSION;
787142215Sglebius		ip->ip_hl = sizeof(*ip) >> 2;
788142215Sglebius		ip->ip_tos = IPTOS_LOWDELAY;
789142215Sglebius		ip->ip_len = len;
790142215Sglebius		ip->ip_id = ip_newid();
791142215Sglebius		ip->ip_off = IP_DF;
792142215Sglebius		ip->ip_ttl = CARP_DFLTTL;
793142215Sglebius		ip->ip_p = IPPROTO_CARP;
794142215Sglebius		ip->ip_sum = 0;
795228571Sglebius
796228571Sglebius		bzero(&sa, sizeof(sa));
797228571Sglebius		sa.sa_family = AF_INET;
798228571Sglebius		ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
799228571Sglebius		if (ifa != NULL) {
800228571Sglebius			ip->ip_src.s_addr =
801228571Sglebius			    ifatoia(ifa)->ia_addr.sin_addr.s_addr;
802228571Sglebius			ifa_free(ifa);
803228571Sglebius		} else
804228571Sglebius			ip->ip_src.s_addr = 0;
805142215Sglebius		ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
806142215Sglebius
807142215Sglebius		ch_ptr = (struct carp_header *)(&ip[1]);
808142215Sglebius		bcopy(&ch, ch_ptr, sizeof(ch));
809142215Sglebius		if (carp_prepare_ad(m, sc, ch_ptr))
810142215Sglebius			return;
811142215Sglebius
812142215Sglebius		m->m_data += sizeof(*ip);
813142215Sglebius		ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip));
814142215Sglebius		m->m_data -= sizeof(*ip);
815142215Sglebius
816190968Srwatson		CARPSTATS_INC(carps_opackets);
817142215Sglebius
818228571Sglebius		if (ip_output(m, NULL, NULL, IP_RAWOUTPUT,
819228571Sglebius		    &sc->sc_carpdev->if_carp->cif_imo, NULL)) {
820142215Sglebius			if (sc->sc_sendad_errors < INT_MAX)
821142215Sglebius				sc->sc_sendad_errors++;
822228736Sglebius			if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS)
823228736Sglebius				carp_demote_adj(carp_senderr_adj, "send error");
824142215Sglebius			sc->sc_sendad_success = 0;
825142215Sglebius		} else {
826142215Sglebius			if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
827142215Sglebius				if (++sc->sc_sendad_success >=
828142215Sglebius				    CARP_SENDAD_MIN_SUCCESS) {
829228736Sglebius					carp_demote_adj(-carp_senderr_adj,
830228736Sglebius					    "send ok");
831142215Sglebius					sc->sc_sendad_errors = 0;
832142215Sglebius				}
833142215Sglebius			} else
834142215Sglebius				sc->sc_sendad_errors = 0;
835142215Sglebius		}
836142215Sglebius	}
837142215Sglebius#endif /* INET */
838142215Sglebius#ifdef INET6
839228571Sglebius	if (sc->sc_naddrs6) {
840142215Sglebius		struct ip6_hdr *ip6;
841142215Sglebius
842228571Sglebius		MGETHDR(m, M_NOWAIT, MT_HEADER);
843142215Sglebius		if (m == NULL) {
844190968Srwatson			CARPSTATS_INC(carps_onomem);
845142215Sglebius			/* XXX maybe less ? */
846228571Sglebius			callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
847228571Sglebius			    carp_send_ad, sc);
848142215Sglebius			return;
849142215Sglebius		}
850142215Sglebius		len = sizeof(*ip6) + sizeof(ch);
851142215Sglebius		m->m_pkthdr.len = len;
852142215Sglebius		m->m_pkthdr.rcvif = NULL;
853142215Sglebius		m->m_len = len;
854142215Sglebius		MH_ALIGN(m, m->m_len);
855142215Sglebius		m->m_flags |= M_MCAST;
856142215Sglebius		ip6 = mtod(m, struct ip6_hdr *);
857142215Sglebius		bzero(ip6, sizeof(*ip6));
858142215Sglebius		ip6->ip6_vfc |= IPV6_VERSION;
859142215Sglebius		ip6->ip6_hlim = CARP_DFLTTL;
860142215Sglebius		ip6->ip6_nxt = IPPROTO_CARP;
861228571Sglebius		bzero(&sa, sizeof(sa));
862142215Sglebius
863228571Sglebius		/* set the source address */
864228571Sglebius		sa.sa_family = AF_INET6;
865228571Sglebius		ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
866228571Sglebius		if (ifa != NULL) {
867228571Sglebius			bcopy(IFA_IN6(ifa), &ip6->ip6_src,
868228571Sglebius			    sizeof(struct in6_addr));
869228571Sglebius			ifa_free(ifa);
870228571Sglebius		} else
871228571Sglebius			/* This should never happen with IPv6. */
872228571Sglebius			bzero(&ip6->ip6_src, sizeof(struct in6_addr));
873228571Sglebius
874228571Sglebius		/* Set the multicast destination. */
875163069Sbz		ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
876142215Sglebius		ip6->ip6_dst.s6_addr8[15] = 0x12;
877163069Sbz		if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
878163069Sbz			m_freem(m);
879200026Sglebius			CARP_DEBUG("%s: in6_setscope failed\n", __func__);
880163069Sbz			return;
881163069Sbz		}
882142215Sglebius
883142215Sglebius		ch_ptr = (struct carp_header *)(&ip6[1]);
884142215Sglebius		bcopy(&ch, ch_ptr, sizeof(ch));
885142215Sglebius		if (carp_prepare_ad(m, sc, ch_ptr))
886142215Sglebius			return;
887142215Sglebius
888142215Sglebius		m->m_data += sizeof(*ip6);
889142215Sglebius		ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip6));
890142215Sglebius		m->m_data -= sizeof(*ip6);
891142215Sglebius
892190968Srwatson		CARPSTATS_INC(carps_opackets6);
893142215Sglebius
894228571Sglebius		if (ip6_output(m, NULL, NULL, 0,
895228571Sglebius		    &sc->sc_carpdev->if_carp->cif_im6o, NULL, NULL)) {
896142215Sglebius			if (sc->sc_sendad_errors < INT_MAX)
897142215Sglebius				sc->sc_sendad_errors++;
898228736Sglebius			if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS)
899228736Sglebius				carp_demote_adj(carp_senderr_adj,
900228736Sglebius				    "send6 error");
901142215Sglebius			sc->sc_sendad_success = 0;
902142215Sglebius		} else {
903142215Sglebius			if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
904142215Sglebius				if (++sc->sc_sendad_success >=
905142215Sglebius				    CARP_SENDAD_MIN_SUCCESS) {
906228736Sglebius					carp_demote_adj(-carp_senderr_adj,
907228736Sglebius					    "send6 ok");
908142215Sglebius					sc->sc_sendad_errors = 0;
909142215Sglebius				}
910142215Sglebius			} else
911142215Sglebius				sc->sc_sendad_errors = 0;
912142215Sglebius		}
913142215Sglebius	}
914142215Sglebius#endif /* INET6 */
915142215Sglebius
916228571Sglebius	callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), carp_send_ad, sc);
917228571Sglebius}
918142215Sglebius
919228571Sglebiusstatic void
920228571Sglebiuscarp_addroute(struct carp_softc *sc)
921228571Sglebius{
922228571Sglebius	struct ifaddr *ifa;
923228571Sglebius
924228571Sglebius	CARP_FOREACH_IFA(sc, ifa)
925230863Sglebius		carp_ifa_addroute(ifa);
926230863Sglebius}
927230863Sglebius
928230863Sglebiusstatic void
929230863Sglebiuscarp_ifa_addroute(struct ifaddr *ifa)
930230863Sglebius{
931230863Sglebius
932230863Sglebius	switch (ifa->ifa_addr->sa_family) {
933228571Sglebius#ifdef INET
934230863Sglebius	case AF_INET:
935230863Sglebius		in_addprefix(ifatoia(ifa), RTF_UP);
936230863Sglebius		ifa_add_loopback_route(ifa,
937230863Sglebius		    (struct sockaddr *)&ifatoia(ifa)->ia_addr);
938230863Sglebius		break;
939228571Sglebius#endif
940228571Sglebius#ifdef INET6
941230863Sglebius	case AF_INET6:
942230863Sglebius		ifa_add_loopback_route(ifa,
943230863Sglebius		    (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
944230863Sglebius		in6_ifaddloop(ifa);
945230863Sglebius		break;
946228571Sglebius#endif
947230863Sglebius	}
948142215Sglebius}
949142215Sglebius
950228571Sglebiusstatic void
951228571Sglebiuscarp_delroute(struct carp_softc *sc)
952228571Sglebius{
953228571Sglebius	struct ifaddr *ifa;
954228571Sglebius
955228571Sglebius	CARP_FOREACH_IFA(sc, ifa)
956230863Sglebius		carp_ifa_delroute(ifa);
957230863Sglebius}
958230863Sglebius
959230863Sglebiusstatic void
960230863Sglebiuscarp_ifa_delroute(struct ifaddr *ifa)
961230863Sglebius{
962230863Sglebius
963230863Sglebius	switch (ifa->ifa_addr->sa_family) {
964221130Sbz#ifdef INET
965230863Sglebius	case AF_INET:
966230863Sglebius		ifa_del_loopback_route(ifa,
967230863Sglebius		    (struct sockaddr *)&ifatoia(ifa)->ia_addr);
968230863Sglebius		in_scrubprefix(ifatoia(ifa), LLE_STATIC);
969230863Sglebius		break;
970228571Sglebius#endif
971228571Sglebius#ifdef INET6
972230863Sglebius	case AF_INET6:
973230863Sglebius		ifa_del_loopback_route(ifa,
974230863Sglebius		    (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
975230863Sglebius		in6_ifremloop(ifa);
976230863Sglebius		break;
977228571Sglebius#endif
978230863Sglebius	}
979228571Sglebius}
980228571Sglebius
981228571Sglebius#ifdef INET
982142215Sglebius/*
983142215Sglebius * Broadcast a gratuitous ARP request containing
984142215Sglebius * the virtual router MAC address for each IP address
985142215Sglebius * associated with the virtual router.
986142215Sglebius */
987142559Sglebiusstatic void
988142215Sglebiuscarp_send_arp(struct carp_softc *sc)
989142215Sglebius{
990142215Sglebius	struct ifaddr *ifa;
991142215Sglebius
992228571Sglebius	CARP_FOREACH_IFA(sc, ifa)
993228571Sglebius		if (ifa->ifa_addr->sa_family == AF_INET)
994228571Sglebius			arp_ifinit2(sc->sc_carpdev, ifa, LLADDR(&sc->sc_addr));
995228571Sglebius}
996142215Sglebius
997228571Sglebiusint
998228571Sglebiuscarp_iamatch(struct ifaddr *ifa, uint8_t **enaddr)
999228571Sglebius{
1000228571Sglebius	struct carp_softc *sc = ifa->ifa_carp;
1001142215Sglebius
1002228571Sglebius	if (sc->sc_state == MASTER) {
1003228571Sglebius		*enaddr = LLADDR(&sc->sc_addr);
1004228571Sglebius		return (1);
1005228571Sglebius	}
1006142215Sglebius
1007228571Sglebius	return (0);
1008142215Sglebius}
1009221130Sbz#endif
1010142215Sglebius
1011142215Sglebius#ifdef INET6
1012142559Sglebiusstatic void
1013142215Sglebiuscarp_send_na(struct carp_softc *sc)
1014142215Sglebius{
1015228571Sglebius	static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1016142215Sglebius	struct ifaddr *ifa;
1017142215Sglebius	struct in6_addr *in6;
1018142215Sglebius
1019228571Sglebius	CARP_FOREACH_IFA(sc, ifa) {
1020142215Sglebius		if (ifa->ifa_addr->sa_family != AF_INET6)
1021142215Sglebius			continue;
1022142215Sglebius
1023228571Sglebius		in6 = IFA_IN6(ifa);
1024142564Sglebius		nd6_na_output(sc->sc_carpdev, &mcast, in6,
1025142215Sglebius		    ND_NA_FLAG_OVERRIDE, 1, NULL);
1026142215Sglebius		DELAY(1000);	/* XXX */
1027142215Sglebius	}
1028142215Sglebius}
1029142215Sglebius
1030142641Smlaierstruct ifaddr *
1031211157Swillcarp_iamatch6(struct ifnet *ifp, struct in6_addr *taddr)
1032142215Sglebius{
1033142215Sglebius	struct ifaddr *ifa;
1034142215Sglebius
1035229621Sjhb	IF_ADDR_RLOCK(ifp);
1036228571Sglebius	IFNET_FOREACH_IFA(ifp, ifa)
1037228571Sglebius		if (ifa->ifa_addr->sa_family == AF_INET6 &&
1038228571Sglebius		    ifa->ifa_carp->sc_state == MASTER &&
1039228571Sglebius		    IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) {
1040228571Sglebius			ifa_ref(ifa);
1041229621Sjhb			IF_ADDR_RUNLOCK(ifp);
1042228571Sglebius			return (ifa);
1043142215Sglebius		}
1044229621Sjhb	IF_ADDR_RUNLOCK(ifp);
1045228571Sglebius
1046142215Sglebius	return (NULL);
1047142215Sglebius}
1048142215Sglebius
1049211157Swillcaddr_t
1050211157Swillcarp_macmatch6(struct ifnet *ifp, struct mbuf *m, const struct in6_addr *taddr)
1051142215Sglebius{
1052142215Sglebius	struct ifaddr *ifa;
1053142215Sglebius
1054229621Sjhb	IF_ADDR_RLOCK(ifp);
1055228571Sglebius	IFNET_FOREACH_IFA(ifp, ifa)
1056228571Sglebius		if (ifa->ifa_addr->sa_family == AF_INET6 &&
1057228571Sglebius		    IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) {
1058228571Sglebius			struct carp_softc *sc = ifa->ifa_carp;
1059228571Sglebius			struct m_tag *mtag;
1060142215Sglebius
1061229621Sjhb			IF_ADDR_RUNLOCK(ifp);
1062228571Sglebius
1063228571Sglebius			mtag = m_tag_get(PACKET_TAG_CARP,
1064236297Sglebius			    sizeof(struct carp_softc *), M_NOWAIT);
1065228571Sglebius			if (mtag == NULL)
1066228571Sglebius				/* Better a bit than nothing. */
1067228571Sglebius				return (LLADDR(&sc->sc_addr));
1068228571Sglebius
1069236310Sglebius			bcopy(&sc, mtag + 1, sizeof(sc));
1070228571Sglebius			m_tag_prepend(m, mtag);
1071228571Sglebius
1072228571Sglebius			return (LLADDR(&sc->sc_addr));
1073142215Sglebius		}
1074229621Sjhb	IF_ADDR_RUNLOCK(ifp);
1075142215Sglebius
1076142215Sglebius	return (NULL);
1077142215Sglebius}
1078228571Sglebius#endif /* INET6 */
1079142215Sglebius
1080228571Sglebiusint
1081211157Swillcarp_forus(struct ifnet *ifp, u_char *dhost)
1082142215Sglebius{
1083228571Sglebius	struct carp_softc *sc;
1084228571Sglebius	uint8_t *ena = dhost;
1085142215Sglebius
1086142215Sglebius	if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1087228571Sglebius		return (0);
1088142215Sglebius
1089228571Sglebius	CIF_LOCK(ifp->if_carp);
1090228571Sglebius	IFNET_FOREACH_CARP(ifp, sc) {
1091228571Sglebius		CARP_LOCK(sc);
1092228571Sglebius		if (sc->sc_state == MASTER && !bcmp(dhost, LLADDR(&sc->sc_addr),
1093228571Sglebius		    ETHER_ADDR_LEN)) {
1094228571Sglebius			CARP_UNLOCK(sc);
1095228571Sglebius			CIF_UNLOCK(ifp->if_carp);
1096228571Sglebius			return (1);
1097142215Sglebius		}
1098228571Sglebius		CARP_UNLOCK(sc);
1099228571Sglebius	}
1100228571Sglebius	CIF_UNLOCK(ifp->if_carp);
1101142215Sglebius
1102228571Sglebius	return (0);
1103142215Sglebius}
1104142215Sglebius
1105231201Sglebius/* Master down timeout event, executed in callout context. */
1106142559Sglebiusstatic void
1107142215Sglebiuscarp_master_down(void *v)
1108142215Sglebius{
1109142215Sglebius	struct carp_softc *sc = v;
1110142215Sglebius
1111228571Sglebius	CARP_LOCK_ASSERT(sc);
1112228571Sglebius
1113231201Sglebius	CURVNET_SET(sc->sc_carpdev->if_vnet);
1114228571Sglebius	if (sc->sc_state == BACKUP) {
1115228812Sglebius		CARP_LOG("VHID %u@%s: BACKUP -> MASTER (master down)\n",
1116228571Sglebius		    sc->sc_vhid,
1117228571Sglebius		    sc->sc_carpdev->if_xname);
1118228571Sglebius		carp_master_down_locked(sc);
1119228571Sglebius	}
1120231201Sglebius	CURVNET_RESTORE();
1121228571Sglebius
1122228571Sglebius	CARP_UNLOCK(sc);
1123142914Sglebius}
1124142914Sglebius
1125142914Sglebiusstatic void
1126142914Sglebiuscarp_master_down_locked(struct carp_softc *sc)
1127142914Sglebius{
1128142914Sglebius
1129228571Sglebius	CARP_LOCK_ASSERT(sc);
1130228571Sglebius
1131142215Sglebius	switch (sc->sc_state) {
1132142215Sglebius	case BACKUP:
1133142215Sglebius		carp_set_state(sc, MASTER);
1134142914Sglebius		carp_send_ad_locked(sc);
1135221130Sbz#ifdef INET
1136142215Sglebius		carp_send_arp(sc);
1137221130Sbz#endif
1138142215Sglebius#ifdef INET6
1139142215Sglebius		carp_send_na(sc);
1140228571Sglebius#endif
1141142215Sglebius		carp_setrun(sc, 0);
1142228571Sglebius		carp_addroute(sc);
1143142215Sglebius		break;
1144228571Sglebius	case INIT:
1145228571Sglebius	case MASTER:
1146228571Sglebius#ifdef INVARIANTS
1147228571Sglebius		panic("carp: VHID %u@%s: master_down event in %s state\n",
1148228571Sglebius		    sc->sc_vhid,
1149228571Sglebius		    sc->sc_carpdev->if_xname,
1150228571Sglebius		    sc->sc_state ? "MASTER" : "INIT");
1151228571Sglebius#endif
1152228571Sglebius		break;
1153142215Sglebius	}
1154142215Sglebius}
1155142215Sglebius
1156142215Sglebius/*
1157142215Sglebius * When in backup state, af indicates whether to reset the master down timer
1158142215Sglebius * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1159142215Sglebius */
1160142559Sglebiusstatic void
1161142215Sglebiuscarp_setrun(struct carp_softc *sc, sa_family_t af)
1162142215Sglebius{
1163142215Sglebius	struct timeval tv;
1164142215Sglebius
1165228571Sglebius	CARP_LOCK_ASSERT(sc);
1166142914Sglebius
1167228571Sglebius	if ((sc->sc_carpdev->if_flags & IFF_UP) == 0 ||
1168228571Sglebius	    sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1169228571Sglebius	    (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0))
1170142215Sglebius		return;
1171142215Sglebius
1172142215Sglebius	switch (sc->sc_state) {
1173142215Sglebius	case INIT:
1174228571Sglebius		CARP_LOG("VHID %u@%s: INIT -> BACKUP\n",
1175228571Sglebius		    sc->sc_vhid,
1176228571Sglebius		    sc->sc_carpdev->if_xname);
1177226367Sglebius		carp_set_state(sc, BACKUP);
1178226367Sglebius		carp_setrun(sc, 0);
1179142215Sglebius		break;
1180142215Sglebius	case BACKUP:
1181142215Sglebius		callout_stop(&sc->sc_ad_tmo);
1182142215Sglebius		tv.tv_sec = 3 * sc->sc_advbase;
1183142215Sglebius		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1184142215Sglebius		switch (af) {
1185142215Sglebius#ifdef INET
1186142215Sglebius		case AF_INET:
1187142215Sglebius			callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1188142215Sglebius			    carp_master_down, sc);
1189142215Sglebius			break;
1190228571Sglebius#endif
1191142215Sglebius#ifdef INET6
1192142215Sglebius		case AF_INET6:
1193142215Sglebius			callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1194142215Sglebius			    carp_master_down, sc);
1195142215Sglebius			break;
1196228571Sglebius#endif
1197142215Sglebius		default:
1198228571Sglebius#ifdef INET
1199142215Sglebius			if (sc->sc_naddrs)
1200142215Sglebius				callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1201142215Sglebius				    carp_master_down, sc);
1202228571Sglebius#endif
1203228571Sglebius#ifdef INET6
1204142215Sglebius			if (sc->sc_naddrs6)
1205142215Sglebius				callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1206142215Sglebius				    carp_master_down, sc);
1207228571Sglebius#endif
1208142215Sglebius			break;
1209142215Sglebius		}
1210142215Sglebius		break;
1211142215Sglebius	case MASTER:
1212142215Sglebius		tv.tv_sec = sc->sc_advbase;
1213142215Sglebius		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1214142215Sglebius		callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1215142215Sglebius		    carp_send_ad, sc);
1216142215Sglebius		break;
1217142215Sglebius	}
1218142215Sglebius}
1219142215Sglebius
1220228571Sglebius/*
1221228571Sglebius * Setup multicast structures.
1222228571Sglebius */
1223228571Sglebiusstatic int
1224234130Sglebiuscarp_multicast_setup(struct carp_if *cif, sa_family_t sa)
1225156947Sglebius{
1226234130Sglebius	struct ifnet *ifp = cif->cif_ifp;
1227228571Sglebius	int error = 0;
1228166226Sglebius
1229234130Sglebius	CIF_LOCK_ASSERT(cif);
1230234130Sglebius
1231228571Sglebius	switch (sa) {
1232228571Sglebius#ifdef INET
1233228571Sglebius	case AF_INET:
1234228571Sglebius	    {
1235228571Sglebius		struct ip_moptions *imo = &cif->cif_imo;
1236228571Sglebius		struct in_addr addr;
1237228571Sglebius
1238228571Sglebius		if (imo->imo_membership)
1239228571Sglebius			return (0);
1240228571Sglebius
1241228571Sglebius		imo->imo_membership = (struct in_multi **)malloc(
1242228571Sglebius		    (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_CARP,
1243234130Sglebius		    M_NOWAIT);
1244234130Sglebius		if (imo->imo_membership == NULL)
1245234130Sglebius			return (ENOMEM);
1246228571Sglebius		imo->imo_mfilters = NULL;
1247228571Sglebius		imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
1248228571Sglebius		imo->imo_multicast_vif = -1;
1249228571Sglebius
1250228571Sglebius		addr.s_addr = htonl(INADDR_CARP_GROUP);
1251228571Sglebius		if ((error = in_joingroup(ifp, &addr, NULL,
1252228571Sglebius		    &imo->imo_membership[0])) != 0) {
1253228571Sglebius			free(imo->imo_membership, M_CARP);
1254228571Sglebius			break;
1255156947Sglebius		}
1256228571Sglebius		imo->imo_num_memberships++;
1257228571Sglebius		imo->imo_multicast_ifp = ifp;
1258228571Sglebius		imo->imo_multicast_ttl = CARP_DFLTTL;
1259228571Sglebius		imo->imo_multicast_loop = 0;
1260228571Sglebius		break;
1261228571Sglebius	   }
1262228571Sglebius#endif
1263228571Sglebius#ifdef INET6
1264228571Sglebius	case AF_INET6:
1265228571Sglebius	    {
1266228571Sglebius		struct ip6_moptions *im6o = &cif->cif_im6o;
1267228571Sglebius		struct in6_addr in6;
1268228571Sglebius		struct in6_multi *in6m;
1269228571Sglebius
1270228571Sglebius		if (im6o->im6o_membership)
1271228571Sglebius			return (0);
1272228571Sglebius
1273228571Sglebius		im6o->im6o_membership = (struct in6_multi **)malloc(
1274228571Sglebius		    (sizeof(struct in6_multi *) * IPV6_MIN_MEMBERSHIPS), M_CARP,
1275234130Sglebius		    M_ZERO | M_NOWAIT);
1276234130Sglebius		if (im6o->im6o_membership == NULL)
1277234130Sglebius			return (ENOMEM);
1278228571Sglebius		im6o->im6o_mfilters = NULL;
1279228571Sglebius		im6o->im6o_max_memberships = IPV6_MIN_MEMBERSHIPS;
1280228571Sglebius		im6o->im6o_multicast_hlim = CARP_DFLTTL;
1281228571Sglebius		im6o->im6o_multicast_ifp = ifp;
1282228571Sglebius
1283228571Sglebius		/* Join IPv6 CARP multicast group. */
1284228571Sglebius		bzero(&in6, sizeof(in6));
1285228571Sglebius		in6.s6_addr16[0] = htons(0xff02);
1286228571Sglebius		in6.s6_addr8[15] = 0x12;
1287228571Sglebius		if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1288228571Sglebius			free(im6o->im6o_membership, M_CARP);
1289228571Sglebius			break;
1290228571Sglebius		}
1291228571Sglebius		in6m = NULL;
1292228571Sglebius		if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
1293228571Sglebius			free(im6o->im6o_membership, M_CARP);
1294228571Sglebius			break;
1295228571Sglebius		}
1296228571Sglebius		im6o->im6o_membership[0] = in6m;
1297228571Sglebius		im6o->im6o_num_memberships++;
1298228571Sglebius
1299228571Sglebius		/* Join solicited multicast address. */
1300228571Sglebius		bzero(&in6, sizeof(in6));
1301228571Sglebius		in6.s6_addr16[0] = htons(0xff02);
1302228571Sglebius		in6.s6_addr32[1] = 0;
1303228571Sglebius		in6.s6_addr32[2] = htonl(1);
1304228571Sglebius		in6.s6_addr32[3] = 0;
1305228571Sglebius		in6.s6_addr8[12] = 0xff;
1306228571Sglebius		if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1307228571Sglebius			in6_mc_leave(im6o->im6o_membership[0], NULL);
1308228571Sglebius			free(im6o->im6o_membership, M_CARP);
1309228571Sglebius			break;
1310228571Sglebius		}
1311228571Sglebius		in6m = NULL;
1312228571Sglebius		if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
1313228571Sglebius			in6_mc_leave(im6o->im6o_membership[0], NULL);
1314228571Sglebius			free(im6o->im6o_membership, M_CARP);
1315228571Sglebius			break;
1316228571Sglebius		}
1317228571Sglebius		im6o->im6o_membership[1] = in6m;
1318228571Sglebius		im6o->im6o_num_memberships++;
1319228571Sglebius		break;
1320228571Sglebius	    }
1321228571Sglebius#endif
1322156947Sglebius	}
1323228571Sglebius
1324228571Sglebius	return (error);
1325166423Sglebius}
1326156947Sglebius
1327228571Sglebius/*
1328228571Sglebius * Free multicast structures.
1329228571Sglebius */
1330166423Sglebiusstatic void
1331234130Sglebiuscarp_multicast_cleanup(struct carp_if *cif, sa_family_t sa)
1332166423Sglebius{
1333166423Sglebius
1334234130Sglebius	CIF_LOCK_ASSERT(cif);
1335228571Sglebius	switch (sa) {
1336228571Sglebius#ifdef INET
1337228571Sglebius	case AF_INET:
1338234130Sglebius		if (cif->cif_naddrs == 0) {
1339228571Sglebius			struct ip_moptions *imo = &cif->cif_imo;
1340228571Sglebius
1341228571Sglebius			in_leavegroup(imo->imo_membership[0], NULL);
1342228571Sglebius			KASSERT(imo->imo_mfilters == NULL,
1343228571Sglebius			    ("%s: imo_mfilters != NULL", __func__));
1344228571Sglebius			free(imo->imo_membership, M_CARP);
1345228571Sglebius			imo->imo_membership = NULL;
1346228571Sglebius
1347191672Sbms		}
1348228571Sglebius		break;
1349228571Sglebius#endif
1350228571Sglebius#ifdef INET6
1351228571Sglebius	case AF_INET6:
1352234130Sglebius		if (cif->cif_naddrs6 == 0) {
1353228571Sglebius			struct ip6_moptions *im6o = &cif->cif_im6o;
1354228571Sglebius
1355228571Sglebius			in6_mc_leave(im6o->im6o_membership[0], NULL);
1356228571Sglebius			in6_mc_leave(im6o->im6o_membership[1], NULL);
1357228571Sglebius			KASSERT(im6o->im6o_mfilters == NULL,
1358228571Sglebius			    ("%s: im6o_mfilters != NULL", __func__));
1359228571Sglebius			free(im6o->im6o_membership, M_CARP);
1360228571Sglebius			im6o->im6o_membership = NULL;
1361228571Sglebius		}
1362228571Sglebius		break;
1363228571Sglebius#endif
1364156947Sglebius	}
1365166423Sglebius}
1366156947Sglebius
1367228571Sglebiusint
1368228571Sglebiuscarp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa)
1369142215Sglebius{
1370228571Sglebius	struct m_tag *mtag;
1371228571Sglebius	struct carp_softc *sc;
1372142215Sglebius
1373228571Sglebius	if (!sa)
1374142215Sglebius		return (0);
1375142215Sglebius
1376228571Sglebius	switch (sa->sa_family) {
1377228571Sglebius#ifdef INET
1378228571Sglebius	case AF_INET:
1379228571Sglebius		break;
1380228571Sglebius#endif
1381228571Sglebius#ifdef INET6
1382228571Sglebius	case AF_INET6:
1383228571Sglebius		break;
1384228571Sglebius#endif
1385228571Sglebius	default:
1386228571Sglebius		return (0);
1387142215Sglebius	}
1388142215Sglebius
1389228571Sglebius	mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
1390228571Sglebius	if (mtag == NULL)
1391228571Sglebius		return (0);
1392142215Sglebius
1393236310Sglebius	bcopy(mtag + 1, &sc, sizeof(sc));
1394194951Srwatson
1395228571Sglebius	/* Set the source MAC address to the Virtual Router MAC Address. */
1396228571Sglebius	switch (ifp->if_type) {
1397228571Sglebius	case IFT_ETHER:
1398234084Sglebius	case IFT_BRIDGE:
1399228571Sglebius	case IFT_L2VLAN: {
1400228571Sglebius			struct ether_header *eh;
1401142215Sglebius
1402228571Sglebius			eh = mtod(m, struct ether_header *);
1403228571Sglebius			eh->ether_shost[0] = 0;
1404228571Sglebius			eh->ether_shost[1] = 0;
1405228571Sglebius			eh->ether_shost[2] = 0x5e;
1406228571Sglebius			eh->ether_shost[3] = 0;
1407228571Sglebius			eh->ether_shost[4] = 1;
1408228571Sglebius			eh->ether_shost[5] = sc->sc_vhid;
1409228571Sglebius		}
1410228571Sglebius		break;
1411228571Sglebius	case IFT_FDDI: {
1412228571Sglebius			struct fddi_header *fh;
1413142215Sglebius
1414228571Sglebius			fh = mtod(m, struct fddi_header *);
1415228571Sglebius			fh->fddi_shost[0] = 0;
1416228571Sglebius			fh->fddi_shost[1] = 0;
1417228571Sglebius			fh->fddi_shost[2] = 0x5e;
1418228571Sglebius			fh->fddi_shost[3] = 0;
1419228571Sglebius			fh->fddi_shost[4] = 1;
1420228571Sglebius			fh->fddi_shost[5] = sc->sc_vhid;
1421194951Srwatson		}
1422228571Sglebius		break;
1423228571Sglebius	case IFT_ISO88025: {
1424228571Sglebius 			struct iso88025_header *th;
1425228571Sglebius 			th = mtod(m, struct iso88025_header *);
1426228571Sglebius			th->iso88025_shost[0] = 3;
1427228571Sglebius			th->iso88025_shost[1] = 0;
1428228571Sglebius			th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1);
1429228571Sglebius			th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1);
1430228571Sglebius			th->iso88025_shost[4] = 0;
1431228571Sglebius			th->iso88025_shost[5] = 0;
1432228571Sglebius		}
1433228571Sglebius		break;
1434228571Sglebius	default:
1435228571Sglebius		printf("%s: carp is not supported for the %d interface type\n",
1436228571Sglebius		    ifp->if_xname, ifp->if_type);
1437228571Sglebius		return (EOPNOTSUPP);
1438142215Sglebius	}
1439142215Sglebius
1440228571Sglebius	return (0);
1441228571Sglebius}
1442142215Sglebius
1443228571Sglebiusstatic struct carp_softc*
1444228571Sglebiuscarp_alloc(struct ifnet *ifp)
1445228571Sglebius{
1446228571Sglebius	struct carp_softc *sc;
1447228571Sglebius	struct carp_if *cif;
1448142215Sglebius
1449228571Sglebius	if ((cif = ifp->if_carp) == NULL) {
1450228571Sglebius		cif = carp_alloc_if(ifp);
1451228571Sglebius		if (cif == NULL)
1452228571Sglebius			return (NULL);
1453142215Sglebius	}
1454142215Sglebius
1455228571Sglebius	sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
1456142215Sglebius
1457228571Sglebius	sc->sc_advbase = CARP_DFLTINTV;
1458228571Sglebius	sc->sc_vhid = -1;	/* required setting */
1459228571Sglebius	sc->sc_init_counter = 1;
1460228571Sglebius	sc->sc_state = INIT;
1461142215Sglebius
1462228571Sglebius	sc->sc_ifasiz = sizeof(struct ifaddr *);
1463228571Sglebius	sc->sc_ifas = malloc(sc->sc_ifasiz, M_CARP, M_WAITOK|M_ZERO);
1464228571Sglebius	sc->sc_carpdev = ifp;
1465142215Sglebius
1466228571Sglebius	CARP_LOCK_INIT(sc);
1467228571Sglebius#ifdef INET
1468228571Sglebius	callout_init_mtx(&sc->sc_md_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1469228571Sglebius#endif
1470228571Sglebius#ifdef INET6
1471228571Sglebius	callout_init_mtx(&sc->sc_md6_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1472228571Sglebius#endif
1473228571Sglebius	callout_init_mtx(&sc->sc_ad_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1474142215Sglebius
1475228571Sglebius	CIF_LOCK(cif);
1476228571Sglebius	TAILQ_INSERT_TAIL(&cif->cif_vrs, sc, sc_list);
1477228571Sglebius	CIF_UNLOCK(cif);
1478142215Sglebius
1479228571Sglebius	mtx_lock(&carp_mtx);
1480228571Sglebius	LIST_INSERT_HEAD(&carp_list, sc, sc_next);
1481228571Sglebius	mtx_unlock(&carp_mtx);
1482142914Sglebius
1483228571Sglebius	return (sc);
1484142215Sglebius}
1485142215Sglebius
1486142559Sglebiusstatic int
1487228571Sglebiuscarp_grow_ifas(struct carp_softc *sc)
1488142215Sglebius{
1489228571Sglebius	struct ifaddr **new;
1490142215Sglebius
1491228571Sglebius	CARP_LOCK_ASSERT(sc);
1492142215Sglebius
1493228571Sglebius	new = malloc(sc->sc_ifasiz * 2, M_CARP, M_NOWAIT|M_ZERO);
1494228571Sglebius	if (new == NULL)
1495228571Sglebius		return (ENOMEM);
1496228571Sglebius	bcopy(sc->sc_ifas, new, sc->sc_ifasiz);
1497228571Sglebius	free(sc->sc_ifas, M_CARP);
1498228571Sglebius	sc->sc_ifas = new;
1499228571Sglebius	sc->sc_ifasiz *= 2;
1500142215Sglebius
1501228571Sglebius	return (0);
1502142215Sglebius}
1503142215Sglebius
1504228571Sglebiusstatic void
1505228571Sglebiuscarp_destroy(struct carp_softc *sc)
1506142215Sglebius{
1507228571Sglebius	struct ifnet *ifp = sc->sc_carpdev;
1508228571Sglebius	struct carp_if *cif = ifp->if_carp;
1509142215Sglebius
1510234130Sglebius	CIF_LOCK_ASSERT(cif);
1511234130Sglebius
1512228571Sglebius	TAILQ_REMOVE(&cif->cif_vrs, sc, sc_list);
1513191672Sbms
1514228571Sglebius	mtx_lock(&carp_mtx);
1515228571Sglebius	LIST_REMOVE(sc, sc_next);
1516228571Sglebius	mtx_unlock(&carp_mtx);
1517142215Sglebius
1518228571Sglebius	CARP_LOCK(sc);
1519228736Sglebius	if (sc->sc_suppress)
1520228736Sglebius		carp_demote_adj(-carp_ifdown_adj, "vhid removed");
1521228571Sglebius	callout_drain(&sc->sc_ad_tmo);
1522228571Sglebius#ifdef INET
1523228571Sglebius	callout_drain(&sc->sc_md_tmo);
1524228571Sglebius#endif
1525228571Sglebius#ifdef INET6
1526228571Sglebius	callout_drain(&sc->sc_md6_tmo);
1527228571Sglebius#endif
1528228571Sglebius	CARP_LOCK_DESTROY(sc);
1529142215Sglebius
1530228571Sglebius	free(sc->sc_ifas, M_CARP);
1531228571Sglebius	free(sc, M_CARP);
1532228571Sglebius}
1533142215Sglebius
1534228571Sglebiusstatic struct carp_if*
1535228571Sglebiuscarp_alloc_if(struct ifnet *ifp)
1536228571Sglebius{
1537228571Sglebius	struct carp_if *cif;
1538142215Sglebius
1539228571Sglebius	cif = malloc(sizeof(*cif), M_CARP, M_WAITOK|M_ZERO);
1540142215Sglebius
1541228571Sglebius	if (ifpromisc(ifp, 1) != 0)
1542228571Sglebius		goto cleanup;
1543191672Sbms
1544228571Sglebius	CIF_LOCK_INIT(cif);
1545228571Sglebius	cif->cif_ifp = ifp;
1546228571Sglebius	TAILQ_INIT(&cif->cif_vrs);
1547142215Sglebius
1548229621Sjhb	IF_ADDR_WLOCK(ifp);
1549228571Sglebius	ifp->if_carp = cif;
1550228571Sglebius	if_ref(ifp);
1551229621Sjhb	IF_ADDR_WUNLOCK(ifp);
1552142215Sglebius
1553228571Sglebius	return (cif);
1554142215Sglebius
1555228571Sglebiuscleanup:
1556228571Sglebius	free(cif, M_CARP);
1557142215Sglebius
1558228571Sglebius	return (NULL);
1559228571Sglebius}
1560142215Sglebius
1561228571Sglebiusstatic void
1562228571Sglebiuscarp_free_if(struct carp_if *cif)
1563228571Sglebius{
1564228571Sglebius	struct ifnet *ifp = cif->cif_ifp;
1565142215Sglebius
1566228571Sglebius	CIF_LOCK_ASSERT(cif);
1567228571Sglebius	KASSERT(TAILQ_EMPTY(&cif->cif_vrs), ("%s: softc list not empty",
1568228571Sglebius	    __func__));
1569142215Sglebius
1570229621Sjhb	IF_ADDR_WLOCK(ifp);
1571228571Sglebius	ifp->if_carp = NULL;
1572228571Sglebius	if_rele(ifp);
1573229621Sjhb	IF_ADDR_WUNLOCK(ifp);
1574142215Sglebius
1575228571Sglebius	CIF_LOCK_DESTROY(cif);
1576142215Sglebius
1577228571Sglebius	ifpromisc(ifp, 0);
1578142215Sglebius
1579228571Sglebius	free(cif, M_CARP);
1580142215Sglebius}
1581142215Sglebius
1582228571Sglebiusstatic void
1583228571Sglebiuscarp_carprcp(struct carpreq *carpr, struct carp_softc *sc, int priv)
1584142215Sglebius{
1585142215Sglebius
1586228571Sglebius	CARP_LOCK(sc);
1587228571Sglebius	carpr->carpr_state = sc->sc_state;
1588228571Sglebius	carpr->carpr_vhid = sc->sc_vhid;
1589228571Sglebius	carpr->carpr_advbase = sc->sc_advbase;
1590228571Sglebius	carpr->carpr_advskew = sc->sc_advskew;
1591228571Sglebius	if (priv)
1592228571Sglebius		bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key));
1593228571Sglebius	else
1594228571Sglebius		bzero(carpr->carpr_key, sizeof(carpr->carpr_key));
1595228571Sglebius	CARP_UNLOCK(sc);
1596142215Sglebius}
1597142215Sglebius
1598228571Sglebiusint
1599228571Sglebiuscarp_ioctl(struct ifreq *ifr, u_long cmd, struct thread *td)
1600142215Sglebius{
1601142215Sglebius	struct carpreq carpr;
1602228571Sglebius	struct ifnet *ifp;
1603228571Sglebius	struct carp_softc *sc = NULL;
1604228571Sglebius	int error = 0, locked = 0;
1605142215Sglebius
1606228571Sglebius	if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
1607228571Sglebius		return (error);
1608142215Sglebius
1609228571Sglebius	ifp = ifunit_ref(ifr->ifr_name);
1610228571Sglebius	if (ifp == NULL)
1611228571Sglebius		return (ENXIO);
1612228571Sglebius
1613228571Sglebius	switch (ifp->if_type) {
1614228571Sglebius	case IFT_ETHER:
1615228571Sglebius	case IFT_L2VLAN:
1616234084Sglebius	case IFT_BRIDGE:
1617228571Sglebius	case IFT_FDDI:
1618228571Sglebius	case IFT_ISO88025:
1619142215Sglebius		break;
1620228571Sglebius	default:
1621228571Sglebius		error = EOPNOTSUPP;
1622228571Sglebius		goto out;
1623228571Sglebius	}
1624142215Sglebius
1625228571Sglebius	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1626228571Sglebius		error = EADDRNOTAVAIL;
1627228571Sglebius		goto out;
1628228571Sglebius	}
1629228571Sglebius
1630228571Sglebius	switch (cmd) {
1631228571Sglebius	case SIOCSVH:
1632228571Sglebius		if ((error = priv_check(td, PRIV_NETINET_CARP)))
1633142215Sglebius			break;
1634228571Sglebius		if (carpr.carpr_vhid <= 0 || carpr.carpr_vhid > CARP_MAXVHID ||
1635228571Sglebius		    carpr.carpr_advbase < 0 || carpr.carpr_advskew < 0) {
1636228571Sglebius			error = EINVAL;
1637142215Sglebius			break;
1638142215Sglebius		}
1639142215Sglebius
1640228571Sglebius		if (ifp->if_carp) {
1641228571Sglebius			CIF_LOCK(ifp->if_carp);
1642228571Sglebius			IFNET_FOREACH_CARP(ifp, sc)
1643228571Sglebius				if (sc->sc_vhid == carpr.carpr_vhid)
1644228571Sglebius					break;
1645228571Sglebius			CIF_UNLOCK(ifp->if_carp);
1646142215Sglebius		}
1647228571Sglebius		if (sc == NULL) {
1648228571Sglebius			sc = carp_alloc(ifp);
1649228571Sglebius			if (sc == NULL) {
1650228571Sglebius				error = EINVAL; /* XXX: ifpromisc failed */
1651228571Sglebius				break;
1652228571Sglebius			}
1653142215Sglebius
1654228571Sglebius			CARP_LOCK(sc);
1655228571Sglebius			sc->sc_vhid = carpr.carpr_vhid;
1656228571Sglebius			LLADDR(&sc->sc_addr)[0] = 0;
1657228571Sglebius			LLADDR(&sc->sc_addr)[1] = 0;
1658228571Sglebius			LLADDR(&sc->sc_addr)[2] = 0x5e;
1659228571Sglebius			LLADDR(&sc->sc_addr)[3] = 0;
1660228571Sglebius			LLADDR(&sc->sc_addr)[4] = 1;
1661228571Sglebius			LLADDR(&sc->sc_addr)[5] = sc->sc_vhid;
1662228571Sglebius		} else
1663228571Sglebius			CARP_LOCK(sc);
1664228571Sglebius		locked = 1;
1665228571Sglebius		if (carpr.carpr_advbase > 0) {
1666228571Sglebius			if (carpr.carpr_advbase > 255 ||
1667228571Sglebius			    carpr.carpr_advbase < CARP_DFLTINTV) {
1668228571Sglebius				error = EINVAL;
1669228571Sglebius				break;
1670228571Sglebius			}
1671228571Sglebius			sc->sc_advbase = carpr.carpr_advbase;
1672142914Sglebius		}
1673228571Sglebius		if (carpr.carpr_advskew > 0) {
1674228571Sglebius			if (carpr.carpr_advskew >= 255) {
1675228571Sglebius				error = EINVAL;
1676228571Sglebius				break;
1677228571Sglebius			}
1678228571Sglebius			sc->sc_advskew = carpr.carpr_advskew;
1679142215Sglebius		}
1680228571Sglebius		if (carpr.carpr_key[0] != '\0') {
1681228571Sglebius			bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1682228571Sglebius			carp_hmac_prepare(sc);
1683142914Sglebius		}
1684228571Sglebius		if (sc->sc_state != INIT &&
1685228571Sglebius		    carpr.carpr_state != sc->sc_state) {
1686142215Sglebius			switch (carpr.carpr_state) {
1687142215Sglebius			case BACKUP:
1688142215Sglebius				callout_stop(&sc->sc_ad_tmo);
1689142215Sglebius				carp_set_state(sc, BACKUP);
1690142215Sglebius				carp_setrun(sc, 0);
1691228571Sglebius				carp_delroute(sc);
1692142215Sglebius				break;
1693142215Sglebius			case MASTER:
1694142914Sglebius				carp_master_down_locked(sc);
1695142215Sglebius				break;
1696142215Sglebius			default:
1697142215Sglebius				break;
1698142215Sglebius			}
1699142215Sglebius		}
1700228571Sglebius		break;
1701228571Sglebius
1702228571Sglebius	case SIOCGVH:
1703228571Sglebius	    {
1704228571Sglebius		int priveleged;
1705228571Sglebius
1706228571Sglebius		if (carpr.carpr_vhid < 0 || carpr.carpr_vhid > CARP_MAXVHID) {
1707228571Sglebius			error = EINVAL;
1708228571Sglebius			break;
1709228571Sglebius		}
1710228571Sglebius		if (carpr.carpr_count < 1) {
1711228571Sglebius			error = EMSGSIZE;
1712228571Sglebius			break;
1713228571Sglebius		}
1714228571Sglebius		if (ifp->if_carp == NULL) {
1715228571Sglebius			error = ENOENT;
1716228571Sglebius			break;
1717228571Sglebius		}
1718228571Sglebius
1719228571Sglebius		priveleged = (priv_check(td, PRIV_NETINET_CARP) == 0);
1720228571Sglebius		if (carpr.carpr_vhid != 0) {
1721228571Sglebius			CIF_LOCK(ifp->if_carp);
1722228571Sglebius			IFNET_FOREACH_CARP(ifp, sc)
1723228571Sglebius				if (sc->sc_vhid == carpr.carpr_vhid)
1724170373Sglebius					break;
1725228571Sglebius			CIF_UNLOCK(ifp->if_carp);
1726228571Sglebius			if (sc == NULL) {
1727228571Sglebius				error = ENOENT;
1728142215Sglebius				break;
1729142215Sglebius			}
1730228571Sglebius			carp_carprcp(&carpr, sc, priveleged);
1731228571Sglebius			error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
1732228571Sglebius		} else  {
1733228571Sglebius			int i, count;
1734228571Sglebius
1735228571Sglebius			count = 0;
1736228571Sglebius			CIF_LOCK(ifp->if_carp);
1737228571Sglebius			IFNET_FOREACH_CARP(ifp, sc)
1738228571Sglebius				count++;
1739228571Sglebius
1740228571Sglebius			if (count > carpr.carpr_count) {
1741228571Sglebius				CIF_UNLOCK(ifp->if_carp);
1742228571Sglebius				error = EMSGSIZE;
1743142215Sglebius				break;
1744142215Sglebius			}
1745228571Sglebius
1746228571Sglebius			i = 0;
1747228571Sglebius			IFNET_FOREACH_CARP(ifp, sc) {
1748228571Sglebius				carp_carprcp(&carpr, sc, priveleged);
1749228571Sglebius				carpr.carpr_count = count;
1750228571Sglebius				error = copyout(&carpr, ifr->ifr_data +
1751228571Sglebius				    (i * sizeof(carpr)), sizeof(carpr));
1752228571Sglebius				if (error) {
1753228571Sglebius					CIF_UNLOCK(ifp->if_carp);
1754228571Sglebius					break;
1755228571Sglebius				}
1756228571Sglebius				i++;
1757228571Sglebius			}
1758228571Sglebius			CIF_UNLOCK(ifp->if_carp);
1759142215Sglebius		}
1760142215Sglebius		break;
1761228571Sglebius	    }
1762142215Sglebius	default:
1763142215Sglebius		error = EINVAL;
1764142215Sglebius	}
1765142215Sglebius
1766228571Sglebiusout:
1767142914Sglebius	if (locked)
1768228571Sglebius		CARP_UNLOCK(sc);
1769228571Sglebius	if_rele(ifp);
1770142914Sglebius
1771142215Sglebius	return (error);
1772142215Sglebius}
1773142215Sglebius
1774142215Sglebiusstatic int
1775228571Sglebiuscarp_get_vhid(struct ifaddr *ifa)
1776142215Sglebius{
1777147611Sdwmalone
1778228571Sglebius	if (ifa == NULL || ifa->ifa_carp == NULL)
1779228571Sglebius		return (0);
1780142215Sglebius
1781228571Sglebius	return (ifa->ifa_carp->sc_vhid);
1782142215Sglebius}
1783142215Sglebius
1784142215Sglebiusint
1785228571Sglebiuscarp_attach(struct ifaddr *ifa, int vhid)
1786142215Sglebius{
1787228571Sglebius	struct ifnet *ifp = ifa->ifa_ifp;
1788234130Sglebius	struct carp_if *cif = ifp->if_carp;
1789142215Sglebius	struct carp_softc *sc;
1790228571Sglebius	int index, error;
1791142215Sglebius
1792228571Sglebius	if (ifp->if_carp == NULL)
1793228571Sglebius		return (ENOPROTOOPT);
1794142215Sglebius
1795228571Sglebius	switch (ifa->ifa_addr->sa_family) {
1796142215Sglebius#ifdef INET
1797142215Sglebius	case AF_INET:
1798228571Sglebius#endif
1799142215Sglebius#ifdef INET6
1800142215Sglebius	case AF_INET6:
1801228571Sglebius#endif
1802142215Sglebius		break;
1803142215Sglebius	default:
1804228571Sglebius		return (EPROTOTYPE);
1805142215Sglebius	}
1806142215Sglebius
1807234130Sglebius	CIF_LOCK(cif);
1808228571Sglebius	IFNET_FOREACH_CARP(ifp, sc)
1809228571Sglebius		if (sc->sc_vhid == vhid)
1810228571Sglebius			break;
1811234130Sglebius	if (sc == NULL) {
1812234130Sglebius		CIF_UNLOCK(cif);
1813228571Sglebius		return (ENOENT);
1814234130Sglebius	}
1815142215Sglebius
1816228571Sglebius	if (ifa->ifa_carp) {
1817228571Sglebius		if (ifa->ifa_carp->sc_vhid != vhid)
1818234130Sglebius			carp_detach_locked(ifa);
1819234130Sglebius		else {
1820234130Sglebius			CIF_UNLOCK(cif);
1821228571Sglebius			return (0);
1822234130Sglebius		}
1823228571Sglebius	}
1824142215Sglebius
1825234130Sglebius	error = carp_multicast_setup(cif, ifa->ifa_addr->sa_family);
1826234130Sglebius	if (error) {
1827234130Sglebius		CIF_FREE(cif);
1828228571Sglebius		return (error);
1829234130Sglebius	}
1830142215Sglebius
1831228571Sglebius	CARP_LOCK(sc);
1832228571Sglebius	index = sc->sc_naddrs + sc->sc_naddrs6 + 1;
1833228571Sglebius	if (index > sc->sc_ifasiz / sizeof(struct ifaddr *))
1834228571Sglebius		if ((error = carp_grow_ifas(sc)) != 0) {
1835234130Sglebius			carp_multicast_cleanup(cif,
1836228571Sglebius			    ifa->ifa_addr->sa_family);
1837228571Sglebius			CARP_UNLOCK(sc);
1838234130Sglebius			CIF_FREE(cif);
1839228571Sglebius			return (error);
1840142215Sglebius		}
1841142215Sglebius
1842228571Sglebius	switch (ifa->ifa_addr->sa_family) {
1843228571Sglebius#ifdef INET
1844228571Sglebius	case AF_INET:
1845234130Sglebius		cif->cif_naddrs++;
1846228571Sglebius		sc->sc_naddrs++;
1847142215Sglebius		break;
1848228571Sglebius#endif
1849228571Sglebius#ifdef INET6
1850228571Sglebius	case AF_INET6:
1851234130Sglebius		cif->cif_naddrs6++;
1852228571Sglebius		sc->sc_naddrs6++;
1853142215Sglebius		break;
1854228571Sglebius#endif
1855142215Sglebius	}
1856142215Sglebius
1857228571Sglebius	ifa_ref(ifa);
1858228571Sglebius	sc->sc_ifas[index - 1] = ifa;
1859228571Sglebius	ifa->ifa_carp = sc;
1860228571Sglebius
1861228571Sglebius	carp_hmac_prepare(sc);
1862228571Sglebius	carp_sc_state(sc);
1863228571Sglebius
1864228571Sglebius	CARP_UNLOCK(sc);
1865234130Sglebius	CIF_UNLOCK(cif);
1866228571Sglebius
1867142215Sglebius	return (0);
1868142215Sglebius}
1869142215Sglebius
1870228571Sglebiusvoid
1871228571Sglebiuscarp_detach(struct ifaddr *ifa)
1872142215Sglebius{
1873234130Sglebius	struct ifnet *ifp = ifa->ifa_ifp;
1874234130Sglebius	struct carp_if *cif = ifp->if_carp;
1875234130Sglebius
1876234130Sglebius	CIF_LOCK(cif);
1877234130Sglebius	carp_detach_locked(ifa);
1878234130Sglebius	CIF_FREE(cif);
1879234130Sglebius}
1880234130Sglebius
1881234130Sglebiusstatic void
1882234130Sglebiuscarp_detach_locked(struct ifaddr *ifa)
1883234130Sglebius{
1884234130Sglebius	struct ifnet *ifp = ifa->ifa_ifp;
1885234130Sglebius	struct carp_if *cif = ifp->if_carp;
1886228571Sglebius	struct carp_softc *sc = ifa->ifa_carp;
1887228571Sglebius	int i, index;
1888142914Sglebius
1889228571Sglebius	KASSERT(sc != NULL, ("%s: %p not attached", __func__, ifa));
1890142914Sglebius
1891234130Sglebius	CIF_LOCK_ASSERT(cif);
1892228571Sglebius	CARP_LOCK(sc);
1893142215Sglebius
1894228571Sglebius	/* Shift array. */
1895228571Sglebius	index = sc->sc_naddrs + sc->sc_naddrs6;
1896228571Sglebius	for (i = 0; i < index; i++)
1897228571Sglebius		if (sc->sc_ifas[i] == ifa)
1898228571Sglebius			break;
1899228571Sglebius	KASSERT(i < index, ("%s: %p no backref", __func__, ifa));
1900228571Sglebius	for (; i < index - 1; i++)
1901228571Sglebius		sc->sc_ifas[i] = sc->sc_ifas[i+1];
1902228571Sglebius	sc->sc_ifas[index - 1] = NULL;
1903228571Sglebius
1904228571Sglebius	switch (ifa->ifa_addr->sa_family) {
1905228571Sglebius#ifdef INET
1906228571Sglebius	case AF_INET:
1907234130Sglebius		cif->cif_naddrs--;
1908228571Sglebius		sc->sc_naddrs--;
1909142215Sglebius		break;
1910228571Sglebius#endif
1911228571Sglebius#ifdef INET6
1912228571Sglebius	case AF_INET6:
1913234130Sglebius		cif->cif_naddrs6--;
1914228571Sglebius		sc->sc_naddrs6--;
1915142215Sglebius		break;
1916228571Sglebius#endif
1917142215Sglebius	}
1918228571Sglebius
1919230863Sglebius	carp_ifa_delroute(ifa);
1920234130Sglebius	carp_multicast_cleanup(cif, ifa->ifa_addr->sa_family);
1921228571Sglebius
1922228571Sglebius	ifa->ifa_carp = NULL;
1923228571Sglebius	ifa_free(ifa);
1924228571Sglebius
1925228571Sglebius	carp_hmac_prepare(sc);
1926228571Sglebius	carp_sc_state(sc);
1927228571Sglebius
1928228571Sglebius	if (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) {
1929228571Sglebius		CARP_UNLOCK(sc);
1930228571Sglebius		carp_destroy(sc);
1931228571Sglebius	} else
1932228571Sglebius		CARP_UNLOCK(sc);
1933142215Sglebius}
1934142215Sglebius
1935228571Sglebiusstatic void
1936228571Sglebiuscarp_set_state(struct carp_softc *sc, int state)
1937142215Sglebius{
1938142914Sglebius
1939228571Sglebius	CARP_LOCK_ASSERT(sc);
1940228571Sglebius
1941228571Sglebius	if (sc->sc_state != state) {
1942228571Sglebius		const char *carp_states[] = { CARP_STATES };
1943228571Sglebius		char subsys[IFNAMSIZ+5];
1944228571Sglebius
1945228571Sglebius		sc->sc_state = state;
1946228571Sglebius
1947228571Sglebius		snprintf(subsys, IFNAMSIZ+5, "%u@%s", sc->sc_vhid,
1948228571Sglebius		    sc->sc_carpdev->if_xname);
1949228571Sglebius		devctl_notify("CARP", subsys, carp_states[state], NULL);
1950228571Sglebius	}
1951142914Sglebius}
1952142914Sglebius
1953142914Sglebiusstatic void
1954228571Sglebiuscarp_linkstate(struct ifnet *ifp)
1955142914Sglebius{
1956142215Sglebius	struct carp_softc *sc;
1957142215Sglebius
1958228571Sglebius	CIF_LOCK(ifp->if_carp);
1959228571Sglebius	IFNET_FOREACH_CARP(ifp, sc) {
1960228571Sglebius		CARP_LOCK(sc);
1961228571Sglebius		carp_sc_state(sc);
1962228571Sglebius		CARP_UNLOCK(sc);
1963228571Sglebius	}
1964228571Sglebius	CIF_UNLOCK(ifp->if_carp);
1965144329Sglebius}
1966144329Sglebius
1967144329Sglebiusstatic void
1968228571Sglebiuscarp_sc_state(struct carp_softc *sc)
1969144329Sglebius{
1970144329Sglebius
1971228571Sglebius	CARP_LOCK_ASSERT(sc);
1972228571Sglebius
1973144329Sglebius	if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1974144329Sglebius	    !(sc->sc_carpdev->if_flags & IFF_UP)) {
1975144329Sglebius		callout_stop(&sc->sc_ad_tmo);
1976228571Sglebius#ifdef INET
1977144329Sglebius		callout_stop(&sc->sc_md_tmo);
1978228571Sglebius#endif
1979228571Sglebius#ifdef INET6
1980144329Sglebius		callout_stop(&sc->sc_md6_tmo);
1981228571Sglebius#endif
1982144329Sglebius		carp_set_state(sc, INIT);
1983144329Sglebius		carp_setrun(sc, 0);
1984228736Sglebius		if (!sc->sc_suppress)
1985228736Sglebius			carp_demote_adj(carp_ifdown_adj, "interface down");
1986144329Sglebius		sc->sc_suppress = 1;
1987144329Sglebius	} else {
1988144329Sglebius		carp_set_state(sc, INIT);
1989144329Sglebius		carp_setrun(sc, 0);
1990144329Sglebius		if (sc->sc_suppress)
1991228736Sglebius			carp_demote_adj(-carp_ifdown_adj, "interface up");
1992144329Sglebius		sc->sc_suppress = 0;
1993142215Sglebius	}
1994142215Sglebius}
1995142215Sglebius
1996228736Sglebiusstatic void
1997228736Sglebiuscarp_demote_adj(int adj, char *reason)
1998228736Sglebius{
1999228736Sglebius	carp_demotion += adj;
2000228736Sglebius	CARP_LOG("demoted by %d to %d (%s)\n", adj, carp_demotion, reason);
2001228736Sglebius	taskqueue_enqueue(taskqueue_swi, &carp_sendall_task);
2002228736Sglebius}
2003228571Sglebius
2004211157Swill#ifdef INET
2005211157Swillextern  struct domain inetdomain;
2006211157Swillstatic struct protosw in_carp_protosw = {
2007211157Swill	.pr_type =		SOCK_RAW,
2008211157Swill	.pr_domain =		&inetdomain,
2009211157Swill	.pr_protocol =		IPPROTO_CARP,
2010211157Swill	.pr_flags =		PR_ATOMIC|PR_ADDR,
2011211157Swill	.pr_input =		carp_input,
2012211157Swill	.pr_output =		(pr_output_t *)rip_output,
2013211157Swill	.pr_ctloutput =		rip_ctloutput,
2014211157Swill	.pr_usrreqs =		&rip_usrreqs
2015211157Swill};
2016211157Swill#endif
2017211157Swill
2018211157Swill#ifdef INET6
2019211157Swillextern	struct domain inet6domain;
2020211157Swillstatic struct ip6protosw in6_carp_protosw = {
2021211157Swill	.pr_type =		SOCK_RAW,
2022211157Swill	.pr_domain =		&inet6domain,
2023211157Swill	.pr_protocol =		IPPROTO_CARP,
2024211157Swill	.pr_flags =		PR_ATOMIC|PR_ADDR,
2025211157Swill	.pr_input =		carp6_input,
2026211157Swill	.pr_output =		rip6_output,
2027211157Swill	.pr_ctloutput =		rip6_ctloutput,
2028211157Swill	.pr_usrreqs =		&rip6_usrreqs
2029211157Swill};
2030211157Swill#endif
2031211157Swill
2032211157Swillstatic void
2033211157Swillcarp_mod_cleanup(void)
2034211157Swill{
2035211157Swill
2036211157Swill#ifdef INET
2037211157Swill	if (proto_reg[CARP_INET] == 0) {
2038212266Swill		(void)ipproto_unregister(IPPROTO_CARP);
2039211157Swill		pf_proto_unregister(PF_INET, IPPROTO_CARP, SOCK_RAW);
2040211157Swill		proto_reg[CARP_INET] = -1;
2041211157Swill	}
2042211157Swill	carp_iamatch_p = NULL;
2043211157Swill#endif
2044211157Swill#ifdef INET6
2045211157Swill	if (proto_reg[CARP_INET6] == 0) {
2046212266Swill		(void)ip6proto_unregister(IPPROTO_CARP);
2047211157Swill		pf_proto_unregister(PF_INET6, IPPROTO_CARP, SOCK_RAW);
2048211157Swill		proto_reg[CARP_INET6] = -1;
2049211157Swill	}
2050211157Swill	carp_iamatch6_p = NULL;
2051211157Swill	carp_macmatch6_p = NULL;
2052211157Swill#endif
2053228571Sglebius	carp_ioctl_p = NULL;
2054228571Sglebius	carp_attach_p = NULL;
2055228571Sglebius	carp_detach_p = NULL;
2056228571Sglebius	carp_get_vhid_p = NULL;
2057211157Swill	carp_linkstate_p = NULL;
2058211157Swill	carp_forus_p = NULL;
2059211157Swill	carp_output_p = NULL;
2060228736Sglebius	carp_demote_adj_p = NULL;
2061228736Sglebius	mtx_unlock(&carp_mtx);
2062228736Sglebius	taskqueue_drain(taskqueue_swi, &carp_sendall_task);
2063211157Swill	mtx_destroy(&carp_mtx);
2064211157Swill}
2065211157Swill
2066142215Sglebiusstatic int
2067211157Swillcarp_mod_load(void)
2068211157Swill{
2069212266Swill	int err;
2070211157Swill
2071211157Swill	mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
2072228571Sglebius	LIST_INIT(&carp_list);
2073228571Sglebius	carp_get_vhid_p = carp_get_vhid;
2074211157Swill	carp_forus_p = carp_forus;
2075211157Swill	carp_output_p = carp_output;
2076228571Sglebius	carp_linkstate_p = carp_linkstate;
2077228571Sglebius	carp_ioctl_p = carp_ioctl;
2078228571Sglebius	carp_attach_p = carp_attach;
2079228571Sglebius	carp_detach_p = carp_detach;
2080228736Sglebius	carp_demote_adj_p = carp_demote_adj;
2081211157Swill#ifdef INET6
2082211157Swill	carp_iamatch6_p = carp_iamatch6;
2083211157Swill	carp_macmatch6_p = carp_macmatch6;
2084211157Swill	proto_reg[CARP_INET6] = pf_proto_register(PF_INET6,
2085211157Swill	    (struct protosw *)&in6_carp_protosw);
2086228571Sglebius	if (proto_reg[CARP_INET6]) {
2087211157Swill		printf("carp: error %d attaching to PF_INET6\n",
2088211157Swill		    proto_reg[CARP_INET6]);
2089211157Swill		carp_mod_cleanup();
2090212898Sglebius		return (proto_reg[CARP_INET6]);
2091211157Swill	}
2092212266Swill	err = ip6proto_register(IPPROTO_CARP);
2093212266Swill	if (err) {
2094212266Swill		printf("carp: error %d registering with INET6\n", err);
2095212266Swill		carp_mod_cleanup();
2096212898Sglebius		return (err);
2097212266Swill	}
2098211157Swill#endif
2099211157Swill#ifdef INET
2100211157Swill	carp_iamatch_p = carp_iamatch;
2101211157Swill	proto_reg[CARP_INET] = pf_proto_register(PF_INET, &in_carp_protosw);
2102228571Sglebius	if (proto_reg[CARP_INET]) {
2103211157Swill		printf("carp: error %d attaching to PF_INET\n",
2104211157Swill		    proto_reg[CARP_INET]);
2105211157Swill		carp_mod_cleanup();
2106212898Sglebius		return (proto_reg[CARP_INET]);
2107211157Swill	}
2108212266Swill	err = ipproto_register(IPPROTO_CARP);
2109212266Swill	if (err) {
2110212266Swill		printf("carp: error %d registering with INET\n", err);
2111212266Swill		carp_mod_cleanup();
2112212898Sglebius		return (err);
2113212266Swill	}
2114211157Swill#endif
2115228571Sglebius	return (0);
2116211157Swill}
2117211157Swill
2118211157Swillstatic int
2119142215Sglebiuscarp_modevent(module_t mod, int type, void *data)
2120142215Sglebius{
2121142215Sglebius	switch (type) {
2122142215Sglebius	case MOD_LOAD:
2123211157Swill		return carp_mod_load();
2124211157Swill		/* NOTREACHED */
2125142215Sglebius	case MOD_UNLOAD:
2126228571Sglebius		mtx_lock(&carp_mtx);
2127228571Sglebius		if (LIST_EMPTY(&carp_list))
2128228571Sglebius			carp_mod_cleanup();
2129228571Sglebius		else {
2130228571Sglebius			mtx_unlock(&carp_mtx);
2131228571Sglebius			return (EBUSY);
2132228571Sglebius		}
2133142215Sglebius		break;
2134142215Sglebius
2135142215Sglebius	default:
2136156947Sglebius		return (EINVAL);
2137142215Sglebius	}
2138142215Sglebius
2139156947Sglebius	return (0);
2140142215Sglebius}
2141142215Sglebius
2142142215Sglebiusstatic moduledata_t carp_mod = {
2143142215Sglebius	"carp",
2144142215Sglebius	carp_modevent,
2145142215Sglebius	0
2146142215Sglebius};
2147142215Sglebius
2148212265SwillDECLARE_MODULE(carp, carp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
2149