ip_carp.c revision 228736
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 228736 2011-12-20 13:53:31Z 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)
226142215Sglebius
227142451Sglebius#define	CARP_LOG(...)	do {				\
228228736Sglebius	if (carp_log > 0)				\
229228571Sglebius		log(LOG_INFO, "carp: " __VA_ARGS__);	\
230142451Sglebius} while (0)
231142215Sglebius
232142451Sglebius#define	CARP_DEBUG(...)	do {				\
233228736Sglebius	if (carp_log > 1)				\
234142446Sglebius		log(LOG_DEBUG, __VA_ARGS__);		\
235142451Sglebius} while (0)
236142446Sglebius
237228571Sglebius#define	IFNET_FOREACH_IFA(ifp, ifa)					\
238228571Sglebius	IF_ADDR_LOCK_ASSERT(ifp);					\
239228571Sglebius	TAILQ_FOREACH((ifa), &(ifp)->if_addrhead, ifa_link)		\
240228571Sglebius		if ((ifa)->ifa_carp != NULL)
241228571Sglebius
242228571Sglebius#define	CARP_FOREACH_IFA(sc, ifa)					\
243228571Sglebius	CARP_LOCK_ASSERT(sc);						\
244228571Sglebius	for (int _i = 0;						\
245228571Sglebius		_i < (sc)->sc_naddrs + (sc)->sc_naddrs6 &&		\
246228571Sglebius		((ifa) = sc->sc_ifas[_i]) != NULL;			\
247228571Sglebius		++_i)
248228571Sglebius
249228571Sglebius#define	IFNET_FOREACH_CARP(ifp, sc)					\
250228571Sglebius	CIF_LOCK_ASSERT(ifp->if_carp);					\
251228571Sglebius	TAILQ_FOREACH((sc), &(ifp)->if_carp->cif_vrs, sc_list)
252228571Sglebius
253228736Sglebius#define	DEMOTE_ADVSKEW(sc)					\
254228736Sglebius    (((sc)->sc_advskew + carp_demotion > CARP_MAXSKEW) ?	\
255228736Sglebius    CARP_MAXSKEW : ((sc)->sc_advskew + carp_demotion))
256228736Sglebius
257142559Sglebiusstatic void	carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
258228571Sglebiusstatic struct carp_softc
259228571Sglebius		*carp_alloc(struct ifnet *);
260228571Sglebiusstatic void	carp_destroy(struct carp_softc *);
261228571Sglebiusstatic struct carp_if
262228571Sglebius		*carp_alloc_if(struct ifnet *);
263228571Sglebiusstatic void	carp_free_if(struct carp_if *);
264228571Sglebiusstatic void	carp_set_state(struct carp_softc *, int);
265228571Sglebiusstatic void	carp_sc_state(struct carp_softc *);
266228571Sglebiusstatic void	carp_setrun(struct carp_softc *, sa_family_t);
267228571Sglebiusstatic void	carp_master_down(void *);
268228571Sglebiusstatic void	carp_master_down_locked(struct carp_softc *);
269142559Sglebiusstatic void	carp_send_ad(void *);
270142914Sglebiusstatic void	carp_send_ad_locked(struct carp_softc *);
271228571Sglebiusstatic void	carp_addroute(struct carp_softc *);
272228571Sglebiusstatic void	carp_delroute(struct carp_softc *);
273228736Sglebiusstatic void	carp_send_ad_all(void *, int);
274228736Sglebiusstatic void	carp_demote_adj(int, char *);
275142215Sglebius
276228571Sglebiusstatic LIST_HEAD(, carp_softc) carp_list;
277142911Sglebiusstatic struct mtx carp_mtx;
278228736Sglebiusstatic struct task carp_sendall_task =
279228736Sglebius    TASK_INITIALIZER(0, carp_send_ad_all, NULL);
280142215Sglebius
281228571Sglebiusstatic __inline uint16_t
282142215Sglebiuscarp_cksum(struct mbuf *m, int len)
283142215Sglebius{
284142215Sglebius	return (in_cksum(m, len));
285142215Sglebius}
286142215Sglebius
287142559Sglebiusstatic void
288142215Sglebiuscarp_hmac_prepare(struct carp_softc *sc)
289142215Sglebius{
290228571Sglebius	uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
291228571Sglebius	uint8_t vhid = sc->sc_vhid & 0xff;
292142215Sglebius	struct ifaddr *ifa;
293179490Smlaier	int i, found;
294179490Smlaier#ifdef INET
295179490Smlaier	struct in_addr last, cur, in;
296179490Smlaier#endif
297142215Sglebius#ifdef INET6
298179490Smlaier	struct in6_addr last6, cur6, in6;
299142215Sglebius#endif
300142215Sglebius
301228571Sglebius	CARP_LOCK_ASSERT(sc);
302142914Sglebius
303228571Sglebius	/* Compute ipad from key. */
304142215Sglebius	bzero(sc->sc_pad, sizeof(sc->sc_pad));
305142215Sglebius	bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
306142215Sglebius	for (i = 0; i < sizeof(sc->sc_pad); i++)
307142215Sglebius		sc->sc_pad[i] ^= 0x36;
308142215Sglebius
309228571Sglebius	/* Precompute first part of inner hash. */
310142215Sglebius	SHA1Init(&sc->sc_sha1);
311142215Sglebius	SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
312142215Sglebius	SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
313142215Sglebius	SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
314142215Sglebius	SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
315142215Sglebius#ifdef INET
316179490Smlaier	cur.s_addr = 0;
317179490Smlaier	do {
318179490Smlaier		found = 0;
319179490Smlaier		last = cur;
320179490Smlaier		cur.s_addr = 0xffffffff;
321228571Sglebius		CARP_FOREACH_IFA(sc, ifa) {
322179490Smlaier			in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr;
323179490Smlaier			if (ifa->ifa_addr->sa_family == AF_INET &&
324179490Smlaier			    ntohl(in.s_addr) > ntohl(last.s_addr) &&
325179490Smlaier			    ntohl(in.s_addr) < ntohl(cur.s_addr)) {
326179490Smlaier				cur.s_addr = in.s_addr;
327179490Smlaier				found++;
328179490Smlaier			}
329179490Smlaier		}
330179490Smlaier		if (found)
331179490Smlaier			SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur));
332179490Smlaier	} while (found);
333142215Sglebius#endif /* INET */
334142215Sglebius#ifdef INET6
335179490Smlaier	memset(&cur6, 0, sizeof(cur6));
336179490Smlaier	do {
337179490Smlaier		found = 0;
338179490Smlaier		last6 = cur6;
339179490Smlaier		memset(&cur6, 0xff, sizeof(cur6));
340228571Sglebius		CARP_FOREACH_IFA(sc, ifa) {
341142215Sglebius			in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
342179490Smlaier			if (IN6_IS_SCOPE_EMBED(&in6))
343179490Smlaier				in6.s6_addr16[1] = 0;
344179490Smlaier			if (ifa->ifa_addr->sa_family == AF_INET6 &&
345179490Smlaier			    memcmp(&in6, &last6, sizeof(in6)) > 0 &&
346179490Smlaier			    memcmp(&in6, &cur6, sizeof(in6)) < 0) {
347179490Smlaier				cur6 = in6;
348179490Smlaier				found++;
349179490Smlaier			}
350142215Sglebius		}
351179490Smlaier		if (found)
352179490Smlaier			SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6));
353179490Smlaier	} while (found);
354142215Sglebius#endif /* INET6 */
355142215Sglebius
356142215Sglebius	/* convert ipad to opad */
357142215Sglebius	for (i = 0; i < sizeof(sc->sc_pad); i++)
358142215Sglebius		sc->sc_pad[i] ^= 0x36 ^ 0x5c;
359142215Sglebius}
360142215Sglebius
361142559Sglebiusstatic void
362228571Sglebiuscarp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
363142215Sglebius    unsigned char md[20])
364142215Sglebius{
365142215Sglebius	SHA1_CTX sha1ctx;
366142215Sglebius
367228571Sglebius	CARP_LOCK_ASSERT(sc);
368228571Sglebius
369142215Sglebius	/* fetch first half of inner hash */
370142215Sglebius	bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
371142215Sglebius
372142215Sglebius	SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
373142215Sglebius	SHA1Final(md, &sha1ctx);
374142215Sglebius
375142215Sglebius	/* outer hash */
376142215Sglebius	SHA1Init(&sha1ctx);
377142215Sglebius	SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
378142215Sglebius	SHA1Update(&sha1ctx, md, 20);
379142215Sglebius	SHA1Final(md, &sha1ctx);
380142215Sglebius}
381142215Sglebius
382142559Sglebiusstatic int
383228571Sglebiuscarp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
384142215Sglebius    unsigned char md[20])
385142215Sglebius{
386142215Sglebius	unsigned char md2[20];
387142215Sglebius
388228571Sglebius	CARP_LOCK_ASSERT(sc);
389142914Sglebius
390142215Sglebius	carp_hmac_generate(sc, counter, md2);
391142215Sglebius
392142215Sglebius	return (bcmp(md, md2, sizeof(md2)));
393142215Sglebius}
394142215Sglebius
395166423Sglebius/*
396142215Sglebius * process input packet.
397142215Sglebius * we have rearranged checks order compared to the rfc,
398142215Sglebius * but it seems more efficient this way or not possible otherwise.
399142215Sglebius */
400221130Sbz#ifdef INET
401142215Sglebiusvoid
402142215Sglebiuscarp_input(struct mbuf *m, int hlen)
403142215Sglebius{
404142215Sglebius	struct ip *ip = mtod(m, struct ip *);
405142215Sglebius	struct carp_header *ch;
406142215Sglebius	int iplen, len;
407142215Sglebius
408190968Srwatson	CARPSTATS_INC(carps_ipackets);
409142215Sglebius
410228736Sglebius	if (!carp_allow) {
411142215Sglebius		m_freem(m);
412142215Sglebius		return;
413142215Sglebius	}
414142215Sglebius
415142215Sglebius	/* verify that the IP TTL is 255.  */
416142215Sglebius	if (ip->ip_ttl != CARP_DFLTTL) {
417190968Srwatson		CARPSTATS_INC(carps_badttl);
418228571Sglebius		CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
419142446Sglebius		    ip->ip_ttl,
420142446Sglebius		    m->m_pkthdr.rcvif->if_xname);
421142215Sglebius		m_freem(m);
422142215Sglebius		return;
423142215Sglebius	}
424142215Sglebius
425142215Sglebius	iplen = ip->ip_hl << 2;
426142215Sglebius
427142215Sglebius	if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
428190968Srwatson		CARPSTATS_INC(carps_badlen);
429228571Sglebius		CARP_DEBUG("%s: received len %zd < sizeof(struct carp_header) "
430228571Sglebius		    "on %s\n", __func__, m->m_len - sizeof(struct ip),
431195976Sdelphij		    m->m_pkthdr.rcvif->if_xname);
432142215Sglebius		m_freem(m);
433142215Sglebius		return;
434142215Sglebius	}
435142215Sglebius
436142215Sglebius	if (iplen + sizeof(*ch) < m->m_len) {
437142215Sglebius		if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
438190968Srwatson			CARPSTATS_INC(carps_hdrops);
439228571Sglebius			CARP_DEBUG("%s: pullup failed\n", __func__);
440142215Sglebius			return;
441142215Sglebius		}
442142215Sglebius		ip = mtod(m, struct ip *);
443142215Sglebius	}
444142215Sglebius	ch = (struct carp_header *)((char *)ip + iplen);
445142215Sglebius
446142215Sglebius	/*
447142215Sglebius	 * verify that the received packet length is
448142215Sglebius	 * equal to the CARP header
449142215Sglebius	 */
450142215Sglebius	len = iplen + sizeof(*ch);
451142215Sglebius	if (len > m->m_pkthdr.len) {
452190968Srwatson		CARPSTATS_INC(carps_badlen);
453228571Sglebius		CARP_DEBUG("%s: packet too short %d on %s\n", __func__,
454142446Sglebius		    m->m_pkthdr.len,
455142446Sglebius		    m->m_pkthdr.rcvif->if_xname);
456142215Sglebius		m_freem(m);
457142215Sglebius		return;
458142215Sglebius	}
459142215Sglebius
460142215Sglebius	if ((m = m_pullup(m, len)) == NULL) {
461190968Srwatson		CARPSTATS_INC(carps_hdrops);
462142215Sglebius		return;
463142215Sglebius	}
464142215Sglebius	ip = mtod(m, struct ip *);
465142215Sglebius	ch = (struct carp_header *)((char *)ip + iplen);
466142215Sglebius
467142215Sglebius	/* verify the CARP checksum */
468142215Sglebius	m->m_data += iplen;
469142215Sglebius	if (carp_cksum(m, len - iplen)) {
470190968Srwatson		CARPSTATS_INC(carps_badsum);
471228571Sglebius		CARP_DEBUG("%s: checksum failed on %s\n", __func__,
472142446Sglebius		    m->m_pkthdr.rcvif->if_xname);
473142215Sglebius		m_freem(m);
474142215Sglebius		return;
475142215Sglebius	}
476142215Sglebius	m->m_data -= iplen;
477142215Sglebius
478142446Sglebius	carp_input_c(m, ch, AF_INET);
479142215Sglebius}
480221130Sbz#endif
481142215Sglebius
482142215Sglebius#ifdef INET6
483142215Sglebiusint
484142215Sglebiuscarp6_input(struct mbuf **mp, int *offp, int proto)
485142215Sglebius{
486142215Sglebius	struct mbuf *m = *mp;
487142215Sglebius	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
488142215Sglebius	struct carp_header *ch;
489142215Sglebius	u_int len;
490142215Sglebius
491190968Srwatson	CARPSTATS_INC(carps_ipackets6);
492142215Sglebius
493228736Sglebius	if (!carp_allow) {
494142215Sglebius		m_freem(m);
495142215Sglebius		return (IPPROTO_DONE);
496142215Sglebius	}
497142215Sglebius
498142215Sglebius	/* check if received on a valid carp interface */
499142215Sglebius	if (m->m_pkthdr.rcvif->if_carp == NULL) {
500190968Srwatson		CARPSTATS_INC(carps_badif);
501228571Sglebius		CARP_DEBUG("%s: packet received on non-carp interface: %s\n",
502228571Sglebius		    __func__, m->m_pkthdr.rcvif->if_xname);
503142215Sglebius		m_freem(m);
504142215Sglebius		return (IPPROTO_DONE);
505142215Sglebius	}
506142215Sglebius
507142215Sglebius	/* verify that the IP TTL is 255 */
508142215Sglebius	if (ip6->ip6_hlim != CARP_DFLTTL) {
509190968Srwatson		CARPSTATS_INC(carps_badttl);
510228571Sglebius		CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
511228571Sglebius		    ip6->ip6_hlim, m->m_pkthdr.rcvif->if_xname);
512142215Sglebius		m_freem(m);
513142215Sglebius		return (IPPROTO_DONE);
514142215Sglebius	}
515142215Sglebius
516142215Sglebius	/* verify that we have a complete carp packet */
517142215Sglebius	len = m->m_len;
518142215Sglebius	IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
519142215Sglebius	if (ch == NULL) {
520190968Srwatson		CARPSTATS_INC(carps_badlen);
521228571Sglebius		CARP_DEBUG("%s: packet size %u too small\n", __func__, len);
522142215Sglebius		return (IPPROTO_DONE);
523142215Sglebius	}
524142215Sglebius
525142215Sglebius
526142215Sglebius	/* verify the CARP checksum */
527142215Sglebius	m->m_data += *offp;
528142215Sglebius	if (carp_cksum(m, sizeof(*ch))) {
529190968Srwatson		CARPSTATS_INC(carps_badsum);
530228571Sglebius		CARP_DEBUG("%s: checksum failed, on %s\n", __func__,
531142446Sglebius		    m->m_pkthdr.rcvif->if_xname);
532142215Sglebius		m_freem(m);
533142215Sglebius		return (IPPROTO_DONE);
534142215Sglebius	}
535142215Sglebius	m->m_data -= *offp;
536142215Sglebius
537142446Sglebius	carp_input_c(m, ch, AF_INET6);
538142215Sglebius	return (IPPROTO_DONE);
539142215Sglebius}
540142215Sglebius#endif /* INET6 */
541142215Sglebius
542142559Sglebiusstatic void
543142446Sglebiuscarp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
544142215Sglebius{
545142215Sglebius	struct ifnet *ifp = m->m_pkthdr.rcvif;
546228571Sglebius	struct ifaddr *ifa;
547142446Sglebius	struct carp_softc *sc;
548228571Sglebius	uint64_t tmp_counter;
549142215Sglebius	struct timeval sc_tv, ch_tv;
550142215Sglebius
551142215Sglebius	/* verify that the VHID is valid on the receiving interface */
552228571Sglebius	IF_ADDR_LOCK(ifp);
553228571Sglebius	IFNET_FOREACH_IFA(ifp, ifa)
554228571Sglebius		if (ifa->ifa_addr->sa_family == af &&
555228571Sglebius		    ifa->ifa_carp->sc_vhid == ch->carp_vhid) {
556228571Sglebius			ifa_ref(ifa);
557142215Sglebius			break;
558228571Sglebius		}
559228571Sglebius	IF_ADDR_UNLOCK(ifp);
560142914Sglebius
561228571Sglebius	if (ifa == NULL) {
562190968Srwatson		CARPSTATS_INC(carps_badvhid);
563142215Sglebius		m_freem(m);
564142215Sglebius		return;
565142215Sglebius	}
566142215Sglebius
567142215Sglebius	/* verify the CARP version. */
568142215Sglebius	if (ch->carp_version != CARP_VERSION) {
569190968Srwatson		CARPSTATS_INC(carps_badver);
570228571Sglebius		CARP_DEBUG("%s: invalid version %d\n", ifp->if_xname,
571142446Sglebius		    ch->carp_version);
572228571Sglebius		ifa_free(ifa);
573142215Sglebius		m_freem(m);
574142215Sglebius		return;
575142215Sglebius	}
576142215Sglebius
577228571Sglebius	sc = ifa->ifa_carp;
578228571Sglebius	CARP_LOCK(sc);
579228571Sglebius	ifa_free(ifa);
580228571Sglebius
581142215Sglebius	if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
582190968Srwatson		CARPSTATS_INC(carps_badauth);
583228571Sglebius		CARP_DEBUG("%s: incorrect hash for VHID %u@%s\n", __func__,
584228571Sglebius		    sc->sc_vhid, ifp->if_xname);
585228571Sglebius		goto out;
586142215Sglebius	}
587142215Sglebius
588142215Sglebius	tmp_counter = ntohl(ch->carp_counter[0]);
589142215Sglebius	tmp_counter = tmp_counter<<32;
590142215Sglebius	tmp_counter += ntohl(ch->carp_counter[1]);
591142215Sglebius
592142215Sglebius	/* XXX Replay protection goes here */
593142215Sglebius
594142215Sglebius	sc->sc_init_counter = 0;
595142215Sglebius	sc->sc_counter = tmp_counter;
596142215Sglebius
597142215Sglebius	sc_tv.tv_sec = sc->sc_advbase;
598228736Sglebius	sc_tv.tv_usec = DEMOTE_ADVSKEW(sc) * 1000000 / 256;
599142215Sglebius	ch_tv.tv_sec = ch->carp_advbase;
600142215Sglebius	ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
601142215Sglebius
602142215Sglebius	switch (sc->sc_state) {
603142215Sglebius	case INIT:
604142215Sglebius		break;
605142215Sglebius	case MASTER:
606142215Sglebius		/*
607142215Sglebius		 * If we receive an advertisement from a master who's going to
608142215Sglebius		 * be more frequent than us, go into BACKUP state.
609142215Sglebius		 */
610142215Sglebius		if (timevalcmp(&sc_tv, &ch_tv, >) ||
611142215Sglebius		    timevalcmp(&sc_tv, &ch_tv, ==)) {
612142215Sglebius			callout_stop(&sc->sc_ad_tmo);
613228571Sglebius			CARP_LOG("VHID %u@%s: MASTER -> BACKUP "
614228571Sglebius			    "(more frequent advertisement received)\n",
615228571Sglebius			    sc->sc_vhid,
616228571Sglebius			    sc->sc_carpdev->if_xname);
617142215Sglebius			carp_set_state(sc, BACKUP);
618142215Sglebius			carp_setrun(sc, 0);
619228571Sglebius			carp_delroute(sc);
620142215Sglebius		}
621142215Sglebius		break;
622142215Sglebius	case BACKUP:
623142215Sglebius		/*
624142215Sglebius		 * If we're pre-empting masters who advertise slower than us,
625142215Sglebius		 * and this one claims to be slower, treat him as down.
626142215Sglebius		 */
627228736Sglebius		if (carp_preempt && timevalcmp(&sc_tv, &ch_tv, <)) {
628228571Sglebius			CARP_LOG("VHID %u@%s: BACKUP -> MASTER "
629142452Sglebius			    "(preempting a slower master)\n",
630228571Sglebius			    sc->sc_vhid,
631228571Sglebius			    sc->sc_carpdev->if_xname);
632142914Sglebius			carp_master_down_locked(sc);
633142215Sglebius			break;
634142215Sglebius		}
635142215Sglebius
636142215Sglebius		/*
637142215Sglebius		 *  If the master is going to advertise at such a low frequency
638142215Sglebius		 *  that he's guaranteed to time out, we'd might as well just
639142215Sglebius		 *  treat him as timed out now.
640142215Sglebius		 */
641142215Sglebius		sc_tv.tv_sec = sc->sc_advbase * 3;
642142215Sglebius		if (timevalcmp(&sc_tv, &ch_tv, <)) {
643228571Sglebius			CARP_LOG("VHID %u@%s: BACKUP -> MASTER "
644142452Sglebius			    "(master timed out)\n",
645228571Sglebius			    sc->sc_vhid,
646228571Sglebius			    sc->sc_carpdev->if_xname);
647142914Sglebius			carp_master_down_locked(sc);
648142215Sglebius			break;
649142215Sglebius		}
650142215Sglebius
651142215Sglebius		/*
652142215Sglebius		 * Otherwise, we reset the counter and wait for the next
653142215Sglebius		 * advertisement.
654142215Sglebius		 */
655142215Sglebius		carp_setrun(sc, af);
656142215Sglebius		break;
657142215Sglebius	}
658142215Sglebius
659228571Sglebiusout:
660228571Sglebius	CARP_UNLOCK(sc);
661142215Sglebius	m_freem(m);
662142215Sglebius}
663142215Sglebius
664142559Sglebiusstatic int
665142215Sglebiuscarp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
666142215Sglebius{
667142215Sglebius	struct m_tag *mtag;
668142215Sglebius
669142215Sglebius	if (sc->sc_init_counter) {
670142215Sglebius		/* this could also be seconds since unix epoch */
671142215Sglebius		sc->sc_counter = arc4random();
672142215Sglebius		sc->sc_counter = sc->sc_counter << 32;
673142215Sglebius		sc->sc_counter += arc4random();
674142215Sglebius	} else
675142215Sglebius		sc->sc_counter++;
676142215Sglebius
677142215Sglebius	ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
678142215Sglebius	ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
679142215Sglebius
680142215Sglebius	carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
681142215Sglebius
682142215Sglebius	/* Tag packet for carp_output */
683228571Sglebius	if ((mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct carp_softc *),
684228571Sglebius	    M_NOWAIT)) == NULL) {
685142215Sglebius		m_freem(m);
686228571Sglebius		CARPSTATS_INC(carps_onomem);
687142215Sglebius		return (ENOMEM);
688142215Sglebius	}
689228571Sglebius	bcopy(&sc, (caddr_t)(mtag + 1), sizeof(struct carp_softc *));
690142215Sglebius	m_tag_prepend(m, mtag);
691142215Sglebius
692142215Sglebius	return (0);
693142215Sglebius}
694142215Sglebius
695228736Sglebius/*
696228736Sglebius * To avoid LORs and possible recursions this function shouldn't
697228736Sglebius * be called directly, but scheduled via taskqueue.
698228736Sglebius */
699142559Sglebiusstatic void
700228736Sglebiuscarp_send_ad_all(void *ctx __unused, int pending __unused)
701142215Sglebius{
702142911Sglebius	struct carp_softc *sc;
703142215Sglebius
704142911Sglebius	mtx_lock(&carp_mtx);
705228571Sglebius	LIST_FOREACH(sc, &carp_list, sc_next)
706228736Sglebius		if (sc->sc_state == MASTER) {
707228571Sglebius			CARP_LOCK(sc);
708142914Sglebius			carp_send_ad_locked(sc);
709228571Sglebius			CARP_UNLOCK(sc);
710228571Sglebius		}
711142911Sglebius	mtx_unlock(&carp_mtx);
712142215Sglebius}
713142215Sglebius
714142559Sglebiusstatic void
715142215Sglebiuscarp_send_ad(void *v)
716142215Sglebius{
717142914Sglebius	struct carp_softc *sc = v;
718142914Sglebius
719228571Sglebius	CARP_LOCK_ASSERT(sc);
720142914Sglebius	carp_send_ad_locked(sc);
721228571Sglebius	CARP_UNLOCK(sc);
722142914Sglebius}
723142914Sglebius
724142914Sglebiusstatic void
725142914Sglebiuscarp_send_ad_locked(struct carp_softc *sc)
726142914Sglebius{
727142215Sglebius	struct carp_header ch;
728142215Sglebius	struct timeval tv;
729228571Sglebius	struct sockaddr sa;
730228571Sglebius	struct ifaddr *ifa;
731142215Sglebius	struct carp_header *ch_ptr;
732142215Sglebius	struct mbuf *m;
733228571Sglebius	int len, advskew;
734142215Sglebius
735228571Sglebius	CARP_LOCK_ASSERT(sc);
736142914Sglebius
737228736Sglebius	advskew = DEMOTE_ADVSKEW(sc);
738228571Sglebius	tv.tv_sec = sc->sc_advbase;
739228571Sglebius	tv.tv_usec = advskew * 1000000 / 256;
740142215Sglebius
741142215Sglebius	ch.carp_version = CARP_VERSION;
742142215Sglebius	ch.carp_type = CARP_ADVERTISEMENT;
743142215Sglebius	ch.carp_vhid = sc->sc_vhid;
744228571Sglebius	ch.carp_advbase = sc->sc_advbase;
745142215Sglebius	ch.carp_advskew = advskew;
746142215Sglebius	ch.carp_authlen = 7;	/* XXX DEFINE */
747142215Sglebius	ch.carp_pad1 = 0;	/* must be zero */
748142215Sglebius	ch.carp_cksum = 0;
749142215Sglebius
750228571Sglebius	/* XXXGL: OpenBSD picks first ifaddr with needed family. */
751228571Sglebius
752142215Sglebius#ifdef INET
753228571Sglebius	if (sc->sc_naddrs) {
754142215Sglebius		struct ip *ip;
755142215Sglebius
756228571Sglebius		MGETHDR(m, M_NOWAIT, MT_HEADER);
757142215Sglebius		if (m == NULL) {
758190968Srwatson			CARPSTATS_INC(carps_onomem);
759142215Sglebius			/* XXX maybe less ? */
760228571Sglebius			callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
761228571Sglebius			    carp_send_ad, sc);
762142215Sglebius			return;
763142215Sglebius		}
764142215Sglebius		len = sizeof(*ip) + sizeof(ch);
765142215Sglebius		m->m_pkthdr.len = len;
766142215Sglebius		m->m_pkthdr.rcvif = NULL;
767142215Sglebius		m->m_len = len;
768142215Sglebius		MH_ALIGN(m, m->m_len);
769142215Sglebius		m->m_flags |= M_MCAST;
770142215Sglebius		ip = mtod(m, struct ip *);
771142215Sglebius		ip->ip_v = IPVERSION;
772142215Sglebius		ip->ip_hl = sizeof(*ip) >> 2;
773142215Sglebius		ip->ip_tos = IPTOS_LOWDELAY;
774142215Sglebius		ip->ip_len = len;
775142215Sglebius		ip->ip_id = ip_newid();
776142215Sglebius		ip->ip_off = IP_DF;
777142215Sglebius		ip->ip_ttl = CARP_DFLTTL;
778142215Sglebius		ip->ip_p = IPPROTO_CARP;
779142215Sglebius		ip->ip_sum = 0;
780228571Sglebius
781228571Sglebius		bzero(&sa, sizeof(sa));
782228571Sglebius		sa.sa_family = AF_INET;
783228571Sglebius		ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
784228571Sglebius		if (ifa != NULL) {
785228571Sglebius			ip->ip_src.s_addr =
786228571Sglebius			    ifatoia(ifa)->ia_addr.sin_addr.s_addr;
787228571Sglebius			ifa_free(ifa);
788228571Sglebius		} else
789228571Sglebius			ip->ip_src.s_addr = 0;
790142215Sglebius		ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
791142215Sglebius
792142215Sglebius		ch_ptr = (struct carp_header *)(&ip[1]);
793142215Sglebius		bcopy(&ch, ch_ptr, sizeof(ch));
794142215Sglebius		if (carp_prepare_ad(m, sc, ch_ptr))
795142215Sglebius			return;
796142215Sglebius
797142215Sglebius		m->m_data += sizeof(*ip);
798142215Sglebius		ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip));
799142215Sglebius		m->m_data -= sizeof(*ip);
800142215Sglebius
801190968Srwatson		CARPSTATS_INC(carps_opackets);
802142215Sglebius
803228571Sglebius		if (ip_output(m, NULL, NULL, IP_RAWOUTPUT,
804228571Sglebius		    &sc->sc_carpdev->if_carp->cif_imo, NULL)) {
805142215Sglebius			if (sc->sc_sendad_errors < INT_MAX)
806142215Sglebius				sc->sc_sendad_errors++;
807228736Sglebius			if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS)
808228736Sglebius				carp_demote_adj(carp_senderr_adj, "send error");
809142215Sglebius			sc->sc_sendad_success = 0;
810142215Sglebius		} else {
811142215Sglebius			if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
812142215Sglebius				if (++sc->sc_sendad_success >=
813142215Sglebius				    CARP_SENDAD_MIN_SUCCESS) {
814228736Sglebius					carp_demote_adj(-carp_senderr_adj,
815228736Sglebius					    "send ok");
816142215Sglebius					sc->sc_sendad_errors = 0;
817142215Sglebius				}
818142215Sglebius			} else
819142215Sglebius				sc->sc_sendad_errors = 0;
820142215Sglebius		}
821142215Sglebius	}
822142215Sglebius#endif /* INET */
823142215Sglebius#ifdef INET6
824228571Sglebius	if (sc->sc_naddrs6) {
825142215Sglebius		struct ip6_hdr *ip6;
826142215Sglebius
827228571Sglebius		MGETHDR(m, M_NOWAIT, MT_HEADER);
828142215Sglebius		if (m == NULL) {
829190968Srwatson			CARPSTATS_INC(carps_onomem);
830142215Sglebius			/* XXX maybe less ? */
831228571Sglebius			callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
832228571Sglebius			    carp_send_ad, sc);
833142215Sglebius			return;
834142215Sglebius		}
835142215Sglebius		len = sizeof(*ip6) + sizeof(ch);
836142215Sglebius		m->m_pkthdr.len = len;
837142215Sglebius		m->m_pkthdr.rcvif = NULL;
838142215Sglebius		m->m_len = len;
839142215Sglebius		MH_ALIGN(m, m->m_len);
840142215Sglebius		m->m_flags |= M_MCAST;
841142215Sglebius		ip6 = mtod(m, struct ip6_hdr *);
842142215Sglebius		bzero(ip6, sizeof(*ip6));
843142215Sglebius		ip6->ip6_vfc |= IPV6_VERSION;
844142215Sglebius		ip6->ip6_hlim = CARP_DFLTTL;
845142215Sglebius		ip6->ip6_nxt = IPPROTO_CARP;
846228571Sglebius		bzero(&sa, sizeof(sa));
847142215Sglebius
848228571Sglebius		/* set the source address */
849228571Sglebius		sa.sa_family = AF_INET6;
850228571Sglebius		ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
851228571Sglebius		if (ifa != NULL) {
852228571Sglebius			bcopy(IFA_IN6(ifa), &ip6->ip6_src,
853228571Sglebius			    sizeof(struct in6_addr));
854228571Sglebius			ifa_free(ifa);
855228571Sglebius		} else
856228571Sglebius			/* This should never happen with IPv6. */
857228571Sglebius			bzero(&ip6->ip6_src, sizeof(struct in6_addr));
858228571Sglebius
859228571Sglebius		/* Set the multicast destination. */
860163069Sbz		ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
861142215Sglebius		ip6->ip6_dst.s6_addr8[15] = 0x12;
862163069Sbz		if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
863163069Sbz			m_freem(m);
864200026Sglebius			CARP_DEBUG("%s: in6_setscope failed\n", __func__);
865163069Sbz			return;
866163069Sbz		}
867142215Sglebius
868142215Sglebius		ch_ptr = (struct carp_header *)(&ip6[1]);
869142215Sglebius		bcopy(&ch, ch_ptr, sizeof(ch));
870142215Sglebius		if (carp_prepare_ad(m, sc, ch_ptr))
871142215Sglebius			return;
872142215Sglebius
873142215Sglebius		m->m_data += sizeof(*ip6);
874142215Sglebius		ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip6));
875142215Sglebius		m->m_data -= sizeof(*ip6);
876142215Sglebius
877190968Srwatson		CARPSTATS_INC(carps_opackets6);
878142215Sglebius
879228571Sglebius		if (ip6_output(m, NULL, NULL, 0,
880228571Sglebius		    &sc->sc_carpdev->if_carp->cif_im6o, NULL, NULL)) {
881142215Sglebius			if (sc->sc_sendad_errors < INT_MAX)
882142215Sglebius				sc->sc_sendad_errors++;
883228736Sglebius			if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS)
884228736Sglebius				carp_demote_adj(carp_senderr_adj,
885228736Sglebius				    "send6 error");
886142215Sglebius			sc->sc_sendad_success = 0;
887142215Sglebius		} else {
888142215Sglebius			if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
889142215Sglebius				if (++sc->sc_sendad_success >=
890142215Sglebius				    CARP_SENDAD_MIN_SUCCESS) {
891228736Sglebius					carp_demote_adj(-carp_senderr_adj,
892228736Sglebius					    "send6 ok");
893142215Sglebius					sc->sc_sendad_errors = 0;
894142215Sglebius				}
895142215Sglebius			} else
896142215Sglebius				sc->sc_sendad_errors = 0;
897142215Sglebius		}
898142215Sglebius	}
899142215Sglebius#endif /* INET6 */
900142215Sglebius
901228571Sglebius	callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), carp_send_ad, sc);
902228571Sglebius}
903142215Sglebius
904228571Sglebiusstatic void
905228571Sglebiuscarp_addroute(struct carp_softc *sc)
906228571Sglebius{
907228571Sglebius	struct ifaddr *ifa;
908228571Sglebius
909228571Sglebius	CARP_FOREACH_IFA(sc, ifa)
910228571Sglebius		switch (ifa->ifa_addr->sa_family) {
911228571Sglebius#ifdef INET
912228571Sglebius		case AF_INET:
913228571Sglebius			in_addprefix(ifatoia(ifa), RTF_UP);
914228571Sglebius			ifa_add_loopback_route(ifa,
915228571Sglebius			    (struct sockaddr *)&ifatoia(ifa)->ia_addr);
916228571Sglebius			break;
917228571Sglebius#endif
918228571Sglebius#ifdef INET6
919228571Sglebius		case AF_INET6:
920228571Sglebius			ifa_add_loopback_route(ifa,
921228571Sglebius			    (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
922228571Sglebius			in6_ifaddloop(ifa);
923228571Sglebius			break;
924228571Sglebius#endif
925228571Sglebius		}
926142215Sglebius}
927142215Sglebius
928228571Sglebiusstatic void
929228571Sglebiuscarp_delroute(struct carp_softc *sc)
930228571Sglebius{
931228571Sglebius	struct ifaddr *ifa;
932228571Sglebius
933228571Sglebius	CARP_FOREACH_IFA(sc, ifa)
934228571Sglebius		switch (ifa->ifa_addr->sa_family) {
935221130Sbz#ifdef INET
936228571Sglebius		case AF_INET:
937228571Sglebius			ifa_del_loopback_route(ifa,
938228571Sglebius			    (struct sockaddr *)&ifatoia(ifa)->ia_addr);
939228571Sglebius			in_scrubprefix(ifatoia(ifa), LLE_STATIC);
940228571Sglebius			break;
941228571Sglebius#endif
942228571Sglebius#ifdef INET6
943228571Sglebius		case AF_INET6:
944228571Sglebius			ifa_del_loopback_route(ifa,
945228571Sglebius			    (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
946228571Sglebius			in6_ifremloop(ifa);
947228571Sglebius			break;
948228571Sglebius#endif
949228571Sglebius		}
950228571Sglebius}
951228571Sglebius
952228571Sglebius#ifdef INET
953142215Sglebius/*
954142215Sglebius * Broadcast a gratuitous ARP request containing
955142215Sglebius * the virtual router MAC address for each IP address
956142215Sglebius * associated with the virtual router.
957142215Sglebius */
958142559Sglebiusstatic void
959142215Sglebiuscarp_send_arp(struct carp_softc *sc)
960142215Sglebius{
961142215Sglebius	struct ifaddr *ifa;
962142215Sglebius
963228571Sglebius	CARP_FOREACH_IFA(sc, ifa)
964228571Sglebius		if (ifa->ifa_addr->sa_family == AF_INET)
965228571Sglebius			arp_ifinit2(sc->sc_carpdev, ifa, LLADDR(&sc->sc_addr));
966228571Sglebius}
967142215Sglebius
968228571Sglebiusint
969228571Sglebiuscarp_iamatch(struct ifaddr *ifa, uint8_t **enaddr)
970228571Sglebius{
971228571Sglebius	struct carp_softc *sc = ifa->ifa_carp;
972142215Sglebius
973228571Sglebius	if (sc->sc_state == MASTER) {
974228571Sglebius		*enaddr = LLADDR(&sc->sc_addr);
975228571Sglebius		return (1);
976228571Sglebius	}
977142215Sglebius
978228571Sglebius	return (0);
979142215Sglebius}
980221130Sbz#endif
981142215Sglebius
982142215Sglebius#ifdef INET6
983142559Sglebiusstatic void
984142215Sglebiuscarp_send_na(struct carp_softc *sc)
985142215Sglebius{
986228571Sglebius	static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
987142215Sglebius	struct ifaddr *ifa;
988142215Sglebius	struct in6_addr *in6;
989142215Sglebius
990228571Sglebius	CARP_FOREACH_IFA(sc, ifa) {
991142215Sglebius		if (ifa->ifa_addr->sa_family != AF_INET6)
992142215Sglebius			continue;
993142215Sglebius
994228571Sglebius		in6 = IFA_IN6(ifa);
995142564Sglebius		nd6_na_output(sc->sc_carpdev, &mcast, in6,
996142215Sglebius		    ND_NA_FLAG_OVERRIDE, 1, NULL);
997142215Sglebius		DELAY(1000);	/* XXX */
998142215Sglebius	}
999142215Sglebius}
1000142215Sglebius
1001142641Smlaierstruct ifaddr *
1002211157Swillcarp_iamatch6(struct ifnet *ifp, struct in6_addr *taddr)
1003142215Sglebius{
1004142215Sglebius	struct ifaddr *ifa;
1005142215Sglebius
1006228571Sglebius	IF_ADDR_LOCK(ifp);
1007228571Sglebius	IFNET_FOREACH_IFA(ifp, ifa)
1008228571Sglebius		if (ifa->ifa_addr->sa_family == AF_INET6 &&
1009228571Sglebius		    ifa->ifa_carp->sc_state == MASTER &&
1010228571Sglebius		    IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) {
1011228571Sglebius			ifa_ref(ifa);
1012228571Sglebius			IF_ADDR_UNLOCK(ifp);
1013228571Sglebius			return (ifa);
1014142215Sglebius		}
1015228571Sglebius	IF_ADDR_UNLOCK(ifp);
1016228571Sglebius
1017142215Sglebius	return (NULL);
1018142215Sglebius}
1019142215Sglebius
1020211157Swillcaddr_t
1021211157Swillcarp_macmatch6(struct ifnet *ifp, struct mbuf *m, const struct in6_addr *taddr)
1022142215Sglebius{
1023142215Sglebius	struct ifaddr *ifa;
1024142215Sglebius
1025228571Sglebius	IF_ADDR_LOCK(ifp);
1026228571Sglebius	IFNET_FOREACH_IFA(ifp, ifa)
1027228571Sglebius		if (ifa->ifa_addr->sa_family == AF_INET6 &&
1028228571Sglebius		    IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) {
1029228571Sglebius			struct carp_softc *sc = ifa->ifa_carp;
1030228571Sglebius			struct m_tag *mtag;
1031142215Sglebius
1032228571Sglebius			IF_ADDR_UNLOCK(ifp);
1033228571Sglebius
1034228571Sglebius			mtag = m_tag_get(PACKET_TAG_CARP,
1035228571Sglebius			    sizeof(struct ifnet *), M_NOWAIT);
1036228571Sglebius			if (mtag == NULL)
1037228571Sglebius				/* Better a bit than nothing. */
1038228571Sglebius				return (LLADDR(&sc->sc_addr));
1039228571Sglebius
1040228571Sglebius			bcopy(&ifp, (caddr_t)(mtag + 1),
1041228571Sglebius			    sizeof(struct ifnet *));
1042228571Sglebius			m_tag_prepend(m, mtag);
1043228571Sglebius
1044228571Sglebius			return (LLADDR(&sc->sc_addr));
1045142215Sglebius		}
1046228571Sglebius	IF_ADDR_UNLOCK(ifp);
1047142215Sglebius
1048142215Sglebius	return (NULL);
1049142215Sglebius}
1050228571Sglebius#endif /* INET6 */
1051142215Sglebius
1052228571Sglebiusint
1053211157Swillcarp_forus(struct ifnet *ifp, u_char *dhost)
1054142215Sglebius{
1055228571Sglebius	struct carp_softc *sc;
1056228571Sglebius	uint8_t *ena = dhost;
1057142215Sglebius
1058142215Sglebius	if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1059228571Sglebius		return (0);
1060142215Sglebius
1061228571Sglebius	CIF_LOCK(ifp->if_carp);
1062228571Sglebius	IFNET_FOREACH_CARP(ifp, sc) {
1063228571Sglebius		CARP_LOCK(sc);
1064228571Sglebius		if (sc->sc_state == MASTER && !bcmp(dhost, LLADDR(&sc->sc_addr),
1065228571Sglebius		    ETHER_ADDR_LEN)) {
1066228571Sglebius			CARP_UNLOCK(sc);
1067228571Sglebius			CIF_UNLOCK(ifp->if_carp);
1068228571Sglebius			return (1);
1069142215Sglebius		}
1070228571Sglebius		CARP_UNLOCK(sc);
1071228571Sglebius	}
1072228571Sglebius	CIF_UNLOCK(ifp->if_carp);
1073142215Sglebius
1074228571Sglebius	return (0);
1075142215Sglebius}
1076142215Sglebius
1077142559Sglebiusstatic void
1078142215Sglebiuscarp_master_down(void *v)
1079142215Sglebius{
1080142215Sglebius	struct carp_softc *sc = v;
1081142215Sglebius
1082228571Sglebius	CARP_LOCK_ASSERT(sc);
1083228571Sglebius
1084228571Sglebius	if (sc->sc_state == BACKUP) {
1085228571Sglebius		CARP_LOG("VHID %u@%s: BACKUP -> MASTER (preempting)\n",
1086228571Sglebius		    sc->sc_vhid,
1087228571Sglebius		    sc->sc_carpdev->if_xname);
1088228571Sglebius		carp_master_down_locked(sc);
1089228571Sglebius	}
1090228571Sglebius
1091228571Sglebius	CARP_UNLOCK(sc);
1092142914Sglebius}
1093142914Sglebius
1094142914Sglebiusstatic void
1095142914Sglebiuscarp_master_down_locked(struct carp_softc *sc)
1096142914Sglebius{
1097142914Sglebius
1098228571Sglebius	CARP_LOCK_ASSERT(sc);
1099228571Sglebius
1100142215Sglebius	switch (sc->sc_state) {
1101142215Sglebius	case BACKUP:
1102142215Sglebius		carp_set_state(sc, MASTER);
1103142914Sglebius		carp_send_ad_locked(sc);
1104221130Sbz#ifdef INET
1105142215Sglebius		carp_send_arp(sc);
1106221130Sbz#endif
1107142215Sglebius#ifdef INET6
1108142215Sglebius		carp_send_na(sc);
1109228571Sglebius#endif
1110142215Sglebius		carp_setrun(sc, 0);
1111228571Sglebius		carp_addroute(sc);
1112142215Sglebius		break;
1113228571Sglebius	case INIT:
1114228571Sglebius	case MASTER:
1115228571Sglebius#ifdef INVARIANTS
1116228571Sglebius		panic("carp: VHID %u@%s: master_down event in %s state\n",
1117228571Sglebius		    sc->sc_vhid,
1118228571Sglebius		    sc->sc_carpdev->if_xname,
1119228571Sglebius		    sc->sc_state ? "MASTER" : "INIT");
1120228571Sglebius#endif
1121228571Sglebius		break;
1122142215Sglebius	}
1123142215Sglebius}
1124142215Sglebius
1125142215Sglebius/*
1126142215Sglebius * When in backup state, af indicates whether to reset the master down timer
1127142215Sglebius * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1128142215Sglebius */
1129142559Sglebiusstatic void
1130142215Sglebiuscarp_setrun(struct carp_softc *sc, sa_family_t af)
1131142215Sglebius{
1132142215Sglebius	struct timeval tv;
1133142215Sglebius
1134228571Sglebius	CARP_LOCK_ASSERT(sc);
1135142914Sglebius
1136228571Sglebius	if ((sc->sc_carpdev->if_flags & IFF_UP) == 0 ||
1137228571Sglebius	    sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1138228571Sglebius	    (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0))
1139142215Sglebius		return;
1140142215Sglebius
1141142215Sglebius	switch (sc->sc_state) {
1142142215Sglebius	case INIT:
1143228571Sglebius		CARP_LOG("VHID %u@%s: INIT -> BACKUP\n",
1144228571Sglebius		    sc->sc_vhid,
1145228571Sglebius		    sc->sc_carpdev->if_xname);
1146226367Sglebius		carp_set_state(sc, BACKUP);
1147226367Sglebius		carp_setrun(sc, 0);
1148142215Sglebius		break;
1149142215Sglebius	case BACKUP:
1150142215Sglebius		callout_stop(&sc->sc_ad_tmo);
1151142215Sglebius		tv.tv_sec = 3 * sc->sc_advbase;
1152142215Sglebius		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1153142215Sglebius		switch (af) {
1154142215Sglebius#ifdef INET
1155142215Sglebius		case AF_INET:
1156142215Sglebius			callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1157142215Sglebius			    carp_master_down, sc);
1158142215Sglebius			break;
1159228571Sglebius#endif
1160142215Sglebius#ifdef INET6
1161142215Sglebius		case AF_INET6:
1162142215Sglebius			callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1163142215Sglebius			    carp_master_down, sc);
1164142215Sglebius			break;
1165228571Sglebius#endif
1166142215Sglebius		default:
1167228571Sglebius#ifdef INET
1168142215Sglebius			if (sc->sc_naddrs)
1169142215Sglebius				callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1170142215Sglebius				    carp_master_down, sc);
1171228571Sglebius#endif
1172228571Sglebius#ifdef INET6
1173142215Sglebius			if (sc->sc_naddrs6)
1174142215Sglebius				callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1175142215Sglebius				    carp_master_down, sc);
1176228571Sglebius#endif
1177142215Sglebius			break;
1178142215Sglebius		}
1179142215Sglebius		break;
1180142215Sglebius	case MASTER:
1181142215Sglebius		tv.tv_sec = sc->sc_advbase;
1182142215Sglebius		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1183142215Sglebius		callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1184142215Sglebius		    carp_send_ad, sc);
1185142215Sglebius		break;
1186142215Sglebius	}
1187142215Sglebius}
1188142215Sglebius
1189228571Sglebius/*
1190228571Sglebius * Setup multicast structures.
1191228571Sglebius */
1192228571Sglebiusstatic int
1193228571Sglebiuscarp_multicast_setup(struct carp_softc *sc, sa_family_t sa)
1194156947Sglebius{
1195228571Sglebius	struct ifnet *ifp = sc->sc_carpdev;
1196228571Sglebius	struct carp_if *cif = ifp->if_carp;
1197228571Sglebius	int error = 0;
1198166226Sglebius
1199228571Sglebius	switch (sa) {
1200228571Sglebius#ifdef INET
1201228571Sglebius	case AF_INET:
1202228571Sglebius	    {
1203228571Sglebius		struct ip_moptions *imo = &cif->cif_imo;
1204228571Sglebius		struct in_addr addr;
1205228571Sglebius
1206228571Sglebius		if (imo->imo_membership)
1207228571Sglebius			return (0);
1208228571Sglebius
1209228571Sglebius		imo->imo_membership = (struct in_multi **)malloc(
1210228571Sglebius		    (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_CARP,
1211228571Sglebius		    M_WAITOK);
1212228571Sglebius		imo->imo_mfilters = NULL;
1213228571Sglebius		imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
1214228571Sglebius		imo->imo_multicast_vif = -1;
1215228571Sglebius
1216228571Sglebius		addr.s_addr = htonl(INADDR_CARP_GROUP);
1217228571Sglebius		if ((error = in_joingroup(ifp, &addr, NULL,
1218228571Sglebius		    &imo->imo_membership[0])) != 0) {
1219228571Sglebius			free(imo->imo_membership, M_CARP);
1220228571Sglebius			break;
1221156947Sglebius		}
1222228571Sglebius		imo->imo_num_memberships++;
1223228571Sglebius		imo->imo_multicast_ifp = ifp;
1224228571Sglebius		imo->imo_multicast_ttl = CARP_DFLTTL;
1225228571Sglebius		imo->imo_multicast_loop = 0;
1226228571Sglebius		break;
1227228571Sglebius	   }
1228228571Sglebius#endif
1229228571Sglebius#ifdef INET6
1230228571Sglebius	case AF_INET6:
1231228571Sglebius	    {
1232228571Sglebius		struct ip6_moptions *im6o = &cif->cif_im6o;
1233228571Sglebius		struct in6_addr in6;
1234228571Sglebius		struct in6_multi *in6m;
1235228571Sglebius
1236228571Sglebius		if (im6o->im6o_membership)
1237228571Sglebius			return (0);
1238228571Sglebius
1239228571Sglebius		im6o->im6o_membership = (struct in6_multi **)malloc(
1240228571Sglebius		    (sizeof(struct in6_multi *) * IPV6_MIN_MEMBERSHIPS), M_CARP,
1241228571Sglebius		    M_ZERO|M_WAITOK);
1242228571Sglebius		im6o->im6o_mfilters = NULL;
1243228571Sglebius		im6o->im6o_max_memberships = IPV6_MIN_MEMBERSHIPS;
1244228571Sglebius		im6o->im6o_multicast_hlim = CARP_DFLTTL;
1245228571Sglebius		im6o->im6o_multicast_ifp = ifp;
1246228571Sglebius
1247228571Sglebius		/* Join IPv6 CARP multicast group. */
1248228571Sglebius		bzero(&in6, sizeof(in6));
1249228571Sglebius		in6.s6_addr16[0] = htons(0xff02);
1250228571Sglebius		in6.s6_addr8[15] = 0x12;
1251228571Sglebius		if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1252228571Sglebius			free(im6o->im6o_membership, M_CARP);
1253228571Sglebius			break;
1254228571Sglebius		}
1255228571Sglebius		in6m = NULL;
1256228571Sglebius		if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
1257228571Sglebius			free(im6o->im6o_membership, M_CARP);
1258228571Sglebius			break;
1259228571Sglebius		}
1260228571Sglebius		im6o->im6o_membership[0] = in6m;
1261228571Sglebius		im6o->im6o_num_memberships++;
1262228571Sglebius
1263228571Sglebius		/* Join solicited multicast address. */
1264228571Sglebius		bzero(&in6, sizeof(in6));
1265228571Sglebius		in6.s6_addr16[0] = htons(0xff02);
1266228571Sglebius		in6.s6_addr32[1] = 0;
1267228571Sglebius		in6.s6_addr32[2] = htonl(1);
1268228571Sglebius		in6.s6_addr32[3] = 0;
1269228571Sglebius		in6.s6_addr8[12] = 0xff;
1270228571Sglebius		if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1271228571Sglebius			in6_mc_leave(im6o->im6o_membership[0], NULL);
1272228571Sglebius			free(im6o->im6o_membership, M_CARP);
1273228571Sglebius			break;
1274228571Sglebius		}
1275228571Sglebius		in6m = NULL;
1276228571Sglebius		if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
1277228571Sglebius			in6_mc_leave(im6o->im6o_membership[0], NULL);
1278228571Sglebius			free(im6o->im6o_membership, M_CARP);
1279228571Sglebius			break;
1280228571Sglebius		}
1281228571Sglebius		im6o->im6o_membership[1] = in6m;
1282228571Sglebius		im6o->im6o_num_memberships++;
1283228571Sglebius		break;
1284228571Sglebius	    }
1285228571Sglebius#endif
1286156947Sglebius	}
1287228571Sglebius
1288228571Sglebius	return (error);
1289166423Sglebius}
1290156947Sglebius
1291228571Sglebius/*
1292228571Sglebius * Free multicast structures.
1293228571Sglebius */
1294166423Sglebiusstatic void
1295228571Sglebiuscarp_multicast_cleanup(struct carp_softc *sc, sa_family_t sa)
1296166423Sglebius{
1297228571Sglebius	struct ifnet *ifp = sc->sc_carpdev;
1298228571Sglebius	struct carp_if *cif = ifp->if_carp;
1299166423Sglebius
1300228571Sglebius	switch (sa) {
1301228571Sglebius#ifdef INET
1302228571Sglebius	case AF_INET:
1303228571Sglebius		if (sc->sc_naddrs == 0) {
1304228571Sglebius			struct ip_moptions *imo = &cif->cif_imo;
1305228571Sglebius
1306228571Sglebius			in_leavegroup(imo->imo_membership[0], NULL);
1307228571Sglebius			KASSERT(imo->imo_mfilters == NULL,
1308228571Sglebius			    ("%s: imo_mfilters != NULL", __func__));
1309228571Sglebius			free(imo->imo_membership, M_CARP);
1310228571Sglebius			imo->imo_membership = NULL;
1311228571Sglebius
1312191672Sbms		}
1313228571Sglebius		break;
1314228571Sglebius#endif
1315228571Sglebius#ifdef INET6
1316228571Sglebius	case AF_INET6:
1317228571Sglebius		if (sc->sc_naddrs6 == 0) {
1318228571Sglebius			struct ip6_moptions *im6o = &cif->cif_im6o;
1319228571Sglebius
1320228571Sglebius			in6_mc_leave(im6o->im6o_membership[0], NULL);
1321228571Sglebius			in6_mc_leave(im6o->im6o_membership[1], NULL);
1322228571Sglebius			KASSERT(im6o->im6o_mfilters == NULL,
1323228571Sglebius			    ("%s: im6o_mfilters != NULL", __func__));
1324228571Sglebius			free(im6o->im6o_membership, M_CARP);
1325228571Sglebius			im6o->im6o_membership = NULL;
1326228571Sglebius		}
1327228571Sglebius		break;
1328228571Sglebius#endif
1329156947Sglebius	}
1330166423Sglebius}
1331156947Sglebius
1332228571Sglebiusint
1333228571Sglebiuscarp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa)
1334142215Sglebius{
1335228571Sglebius	struct m_tag *mtag;
1336228571Sglebius	struct carp_softc *sc;
1337142215Sglebius
1338228571Sglebius	if (!sa)
1339142215Sglebius		return (0);
1340142215Sglebius
1341228571Sglebius	switch (sa->sa_family) {
1342228571Sglebius#ifdef INET
1343228571Sglebius	case AF_INET:
1344228571Sglebius		break;
1345228571Sglebius#endif
1346228571Sglebius#ifdef INET6
1347228571Sglebius	case AF_INET6:
1348228571Sglebius		break;
1349228571Sglebius#endif
1350228571Sglebius	default:
1351228571Sglebius		return (0);
1352142215Sglebius	}
1353142215Sglebius
1354228571Sglebius	mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
1355228571Sglebius	if (mtag == NULL)
1356228571Sglebius		return (0);
1357142215Sglebius
1358228571Sglebius	bcopy(mtag + 1, &sc, sizeof(struct carp_softc *));
1359194951Srwatson
1360228571Sglebius	/* Set the source MAC address to the Virtual Router MAC Address. */
1361228571Sglebius	switch (ifp->if_type) {
1362228571Sglebius	case IFT_ETHER:
1363228571Sglebius	case IFT_L2VLAN: {
1364228571Sglebius			struct ether_header *eh;
1365142215Sglebius
1366228571Sglebius			eh = mtod(m, struct ether_header *);
1367228571Sglebius			eh->ether_shost[0] = 0;
1368228571Sglebius			eh->ether_shost[1] = 0;
1369228571Sglebius			eh->ether_shost[2] = 0x5e;
1370228571Sglebius			eh->ether_shost[3] = 0;
1371228571Sglebius			eh->ether_shost[4] = 1;
1372228571Sglebius			eh->ether_shost[5] = sc->sc_vhid;
1373228571Sglebius		}
1374228571Sglebius		break;
1375228571Sglebius	case IFT_FDDI: {
1376228571Sglebius			struct fddi_header *fh;
1377142215Sglebius
1378228571Sglebius			fh = mtod(m, struct fddi_header *);
1379228571Sglebius			fh->fddi_shost[0] = 0;
1380228571Sglebius			fh->fddi_shost[1] = 0;
1381228571Sglebius			fh->fddi_shost[2] = 0x5e;
1382228571Sglebius			fh->fddi_shost[3] = 0;
1383228571Sglebius			fh->fddi_shost[4] = 1;
1384228571Sglebius			fh->fddi_shost[5] = sc->sc_vhid;
1385194951Srwatson		}
1386228571Sglebius		break;
1387228571Sglebius	case IFT_ISO88025: {
1388228571Sglebius 			struct iso88025_header *th;
1389228571Sglebius 			th = mtod(m, struct iso88025_header *);
1390228571Sglebius			th->iso88025_shost[0] = 3;
1391228571Sglebius			th->iso88025_shost[1] = 0;
1392228571Sglebius			th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1);
1393228571Sglebius			th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1);
1394228571Sglebius			th->iso88025_shost[4] = 0;
1395228571Sglebius			th->iso88025_shost[5] = 0;
1396228571Sglebius		}
1397228571Sglebius		break;
1398228571Sglebius	default:
1399228571Sglebius		printf("%s: carp is not supported for the %d interface type\n",
1400228571Sglebius		    ifp->if_xname, ifp->if_type);
1401228571Sglebius		return (EOPNOTSUPP);
1402142215Sglebius	}
1403142215Sglebius
1404228571Sglebius	return (0);
1405228571Sglebius}
1406142215Sglebius
1407228571Sglebiusstatic struct carp_softc*
1408228571Sglebiuscarp_alloc(struct ifnet *ifp)
1409228571Sglebius{
1410228571Sglebius	struct carp_softc *sc;
1411228571Sglebius	struct carp_if *cif;
1412142215Sglebius
1413228571Sglebius	if ((cif = ifp->if_carp) == NULL) {
1414228571Sglebius		cif = carp_alloc_if(ifp);
1415228571Sglebius		if (cif == NULL)
1416228571Sglebius			return (NULL);
1417142215Sglebius	}
1418142215Sglebius
1419228571Sglebius	sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
1420142215Sglebius
1421228571Sglebius	sc->sc_advbase = CARP_DFLTINTV;
1422228571Sglebius	sc->sc_vhid = -1;	/* required setting */
1423228571Sglebius	sc->sc_init_counter = 1;
1424228571Sglebius	sc->sc_state = INIT;
1425142215Sglebius
1426228571Sglebius	sc->sc_ifasiz = sizeof(struct ifaddr *);
1427228571Sglebius	sc->sc_ifas = malloc(sc->sc_ifasiz, M_CARP, M_WAITOK|M_ZERO);
1428228571Sglebius	sc->sc_carpdev = ifp;
1429142215Sglebius
1430228571Sglebius	CARP_LOCK_INIT(sc);
1431228571Sglebius#ifdef INET
1432228571Sglebius	callout_init_mtx(&sc->sc_md_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1433228571Sglebius#endif
1434228571Sglebius#ifdef INET6
1435228571Sglebius	callout_init_mtx(&sc->sc_md6_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1436228571Sglebius#endif
1437228571Sglebius	callout_init_mtx(&sc->sc_ad_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1438142215Sglebius
1439228571Sglebius	CIF_LOCK(cif);
1440228571Sglebius	TAILQ_INSERT_TAIL(&cif->cif_vrs, sc, sc_list);
1441228571Sglebius	CIF_UNLOCK(cif);
1442142215Sglebius
1443228571Sglebius	mtx_lock(&carp_mtx);
1444228571Sglebius	LIST_INSERT_HEAD(&carp_list, sc, sc_next);
1445228571Sglebius	mtx_unlock(&carp_mtx);
1446142914Sglebius
1447228571Sglebius	return (sc);
1448142215Sglebius}
1449142215Sglebius
1450142559Sglebiusstatic int
1451228571Sglebiuscarp_grow_ifas(struct carp_softc *sc)
1452142215Sglebius{
1453228571Sglebius	struct ifaddr **new;
1454142215Sglebius
1455228571Sglebius	CARP_LOCK_ASSERT(sc);
1456142215Sglebius
1457228571Sglebius	new = malloc(sc->sc_ifasiz * 2, M_CARP, M_NOWAIT|M_ZERO);
1458228571Sglebius	if (new == NULL)
1459228571Sglebius		return (ENOMEM);
1460228571Sglebius	bcopy(sc->sc_ifas, new, sc->sc_ifasiz);
1461228571Sglebius	free(sc->sc_ifas, M_CARP);
1462228571Sglebius	sc->sc_ifas = new;
1463228571Sglebius	sc->sc_ifasiz *= 2;
1464142215Sglebius
1465228571Sglebius	return (0);
1466142215Sglebius}
1467142215Sglebius
1468228571Sglebiusstatic void
1469228571Sglebiuscarp_destroy(struct carp_softc *sc)
1470142215Sglebius{
1471228571Sglebius	struct ifnet *ifp = sc->sc_carpdev;
1472228571Sglebius	struct carp_if *cif = ifp->if_carp;
1473142215Sglebius
1474228571Sglebius	CIF_LOCK(cif);
1475228571Sglebius	TAILQ_REMOVE(&cif->cif_vrs, sc, sc_list);
1476228571Sglebius	if (TAILQ_EMPTY(&cif->cif_vrs))
1477228571Sglebius		carp_free_if(cif);
1478228571Sglebius	else
1479228571Sglebius		CIF_UNLOCK(cif);
1480191672Sbms
1481228571Sglebius	mtx_lock(&carp_mtx);
1482228571Sglebius	LIST_REMOVE(sc, sc_next);
1483228571Sglebius	mtx_unlock(&carp_mtx);
1484142215Sglebius
1485228571Sglebius	CARP_LOCK(sc);
1486228736Sglebius	if (sc->sc_suppress)
1487228736Sglebius		carp_demote_adj(-carp_ifdown_adj, "vhid removed");
1488228571Sglebius	callout_drain(&sc->sc_ad_tmo);
1489228571Sglebius#ifdef INET
1490228571Sglebius	callout_drain(&sc->sc_md_tmo);
1491228571Sglebius#endif
1492228571Sglebius#ifdef INET6
1493228571Sglebius	callout_drain(&sc->sc_md6_tmo);
1494228571Sglebius#endif
1495228571Sglebius	CARP_LOCK_DESTROY(sc);
1496142215Sglebius
1497228571Sglebius	free(sc->sc_ifas, M_CARP);
1498228571Sglebius	free(sc, M_CARP);
1499228571Sglebius}
1500142215Sglebius
1501228571Sglebiusstatic struct carp_if*
1502228571Sglebiuscarp_alloc_if(struct ifnet *ifp)
1503228571Sglebius{
1504228571Sglebius	struct carp_if *cif;
1505142215Sglebius
1506228571Sglebius	cif = malloc(sizeof(*cif), M_CARP, M_WAITOK|M_ZERO);
1507142215Sglebius
1508228571Sglebius	if (ifpromisc(ifp, 1) != 0)
1509228571Sglebius		goto cleanup;
1510191672Sbms
1511228571Sglebius	CIF_LOCK_INIT(cif);
1512228571Sglebius	cif->cif_ifp = ifp;
1513228571Sglebius	TAILQ_INIT(&cif->cif_vrs);
1514142215Sglebius
1515228571Sglebius	IF_ADDR_LOCK(ifp);
1516228571Sglebius	ifp->if_carp = cif;
1517228571Sglebius	if_ref(ifp);
1518228571Sglebius	IF_ADDR_UNLOCK(ifp);
1519142215Sglebius
1520228571Sglebius	return (cif);
1521142215Sglebius
1522228571Sglebiuscleanup:
1523228571Sglebius	free(cif, M_CARP);
1524142215Sglebius
1525228571Sglebius	return (NULL);
1526228571Sglebius}
1527142215Sglebius
1528228571Sglebiusstatic void
1529228571Sglebiuscarp_free_if(struct carp_if *cif)
1530228571Sglebius{
1531228571Sglebius	struct ifnet *ifp = cif->cif_ifp;
1532142215Sglebius
1533228571Sglebius	CIF_LOCK_ASSERT(cif);
1534228571Sglebius	KASSERT(TAILQ_EMPTY(&cif->cif_vrs), ("%s: softc list not empty",
1535228571Sglebius	    __func__));
1536142215Sglebius
1537228571Sglebius	IF_ADDR_LOCK(ifp);
1538228571Sglebius	ifp->if_carp = NULL;
1539228571Sglebius	if_rele(ifp);
1540228571Sglebius	IF_ADDR_UNLOCK(ifp);
1541142215Sglebius
1542228571Sglebius	CIF_LOCK_DESTROY(cif);
1543142215Sglebius
1544228571Sglebius	ifpromisc(ifp, 0);
1545142215Sglebius
1546228571Sglebius	free(cif, M_CARP);
1547142215Sglebius}
1548142215Sglebius
1549228571Sglebiusstatic void
1550228571Sglebiuscarp_carprcp(struct carpreq *carpr, struct carp_softc *sc, int priv)
1551142215Sglebius{
1552142215Sglebius
1553228571Sglebius	CARP_LOCK(sc);
1554228571Sglebius	carpr->carpr_state = sc->sc_state;
1555228571Sglebius	carpr->carpr_vhid = sc->sc_vhid;
1556228571Sglebius	carpr->carpr_advbase = sc->sc_advbase;
1557228571Sglebius	carpr->carpr_advskew = sc->sc_advskew;
1558228571Sglebius	if (priv)
1559228571Sglebius		bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key));
1560228571Sglebius	else
1561228571Sglebius		bzero(carpr->carpr_key, sizeof(carpr->carpr_key));
1562228571Sglebius	CARP_UNLOCK(sc);
1563142215Sglebius}
1564142215Sglebius
1565228571Sglebiusint
1566228571Sglebiuscarp_ioctl(struct ifreq *ifr, u_long cmd, struct thread *td)
1567142215Sglebius{
1568142215Sglebius	struct carpreq carpr;
1569228571Sglebius	struct ifnet *ifp;
1570228571Sglebius	struct carp_softc *sc = NULL;
1571228571Sglebius	int error = 0, locked = 0;
1572142215Sglebius
1573228571Sglebius	if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
1574228571Sglebius		return (error);
1575142215Sglebius
1576228571Sglebius	ifp = ifunit_ref(ifr->ifr_name);
1577228571Sglebius	if (ifp == NULL)
1578228571Sglebius		return (ENXIO);
1579228571Sglebius
1580228571Sglebius	switch (ifp->if_type) {
1581228571Sglebius	case IFT_ETHER:
1582228571Sglebius	case IFT_L2VLAN:
1583228571Sglebius	case IFT_FDDI:
1584228571Sglebius	case IFT_ISO88025:
1585142215Sglebius		break;
1586228571Sglebius	default:
1587228571Sglebius		error = EOPNOTSUPP;
1588228571Sglebius		goto out;
1589228571Sglebius	}
1590142215Sglebius
1591228571Sglebius	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1592228571Sglebius		error = EADDRNOTAVAIL;
1593228571Sglebius		goto out;
1594228571Sglebius	}
1595228571Sglebius
1596228571Sglebius	switch (cmd) {
1597228571Sglebius	case SIOCSVH:
1598228571Sglebius		if ((error = priv_check(td, PRIV_NETINET_CARP)))
1599142215Sglebius			break;
1600228571Sglebius		if (carpr.carpr_vhid <= 0 || carpr.carpr_vhid > CARP_MAXVHID ||
1601228571Sglebius		    carpr.carpr_advbase < 0 || carpr.carpr_advskew < 0) {
1602228571Sglebius			error = EINVAL;
1603142215Sglebius			break;
1604142215Sglebius		}
1605142215Sglebius
1606228571Sglebius		if (ifp->if_carp) {
1607228571Sglebius			CIF_LOCK(ifp->if_carp);
1608228571Sglebius			IFNET_FOREACH_CARP(ifp, sc)
1609228571Sglebius				if (sc->sc_vhid == carpr.carpr_vhid)
1610228571Sglebius					break;
1611228571Sglebius			CIF_UNLOCK(ifp->if_carp);
1612142215Sglebius		}
1613228571Sglebius		if (sc == NULL) {
1614228571Sglebius			sc = carp_alloc(ifp);
1615228571Sglebius			if (sc == NULL) {
1616228571Sglebius				error = EINVAL; /* XXX: ifpromisc failed */
1617228571Sglebius				break;
1618228571Sglebius			}
1619142215Sglebius
1620228571Sglebius			CARP_LOCK(sc);
1621228571Sglebius			sc->sc_vhid = carpr.carpr_vhid;
1622228571Sglebius			LLADDR(&sc->sc_addr)[0] = 0;
1623228571Sglebius			LLADDR(&sc->sc_addr)[1] = 0;
1624228571Sglebius			LLADDR(&sc->sc_addr)[2] = 0x5e;
1625228571Sglebius			LLADDR(&sc->sc_addr)[3] = 0;
1626228571Sglebius			LLADDR(&sc->sc_addr)[4] = 1;
1627228571Sglebius			LLADDR(&sc->sc_addr)[5] = sc->sc_vhid;
1628228571Sglebius		} else
1629228571Sglebius			CARP_LOCK(sc);
1630228571Sglebius		locked = 1;
1631228571Sglebius		if (carpr.carpr_advbase > 0) {
1632228571Sglebius			if (carpr.carpr_advbase > 255 ||
1633228571Sglebius			    carpr.carpr_advbase < CARP_DFLTINTV) {
1634228571Sglebius				error = EINVAL;
1635228571Sglebius				break;
1636228571Sglebius			}
1637228571Sglebius			sc->sc_advbase = carpr.carpr_advbase;
1638142914Sglebius		}
1639228571Sglebius		if (carpr.carpr_advskew > 0) {
1640228571Sglebius			if (carpr.carpr_advskew >= 255) {
1641228571Sglebius				error = EINVAL;
1642228571Sglebius				break;
1643228571Sglebius			}
1644228571Sglebius			sc->sc_advskew = carpr.carpr_advskew;
1645142215Sglebius		}
1646228571Sglebius		if (carpr.carpr_key[0] != '\0') {
1647228571Sglebius			bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1648228571Sglebius			carp_hmac_prepare(sc);
1649142914Sglebius		}
1650228571Sglebius		if (sc->sc_state != INIT &&
1651228571Sglebius		    carpr.carpr_state != sc->sc_state) {
1652142215Sglebius			switch (carpr.carpr_state) {
1653142215Sglebius			case BACKUP:
1654142215Sglebius				callout_stop(&sc->sc_ad_tmo);
1655142215Sglebius				carp_set_state(sc, BACKUP);
1656142215Sglebius				carp_setrun(sc, 0);
1657228571Sglebius				carp_delroute(sc);
1658142215Sglebius				break;
1659142215Sglebius			case MASTER:
1660142914Sglebius				carp_master_down_locked(sc);
1661142215Sglebius				break;
1662142215Sglebius			default:
1663142215Sglebius				break;
1664142215Sglebius			}
1665142215Sglebius		}
1666228571Sglebius		break;
1667228571Sglebius
1668228571Sglebius	case SIOCGVH:
1669228571Sglebius	    {
1670228571Sglebius		int priveleged;
1671228571Sglebius
1672228571Sglebius		if (carpr.carpr_vhid < 0 || carpr.carpr_vhid > CARP_MAXVHID) {
1673228571Sglebius			error = EINVAL;
1674228571Sglebius			break;
1675228571Sglebius		}
1676228571Sglebius		if (carpr.carpr_count < 1) {
1677228571Sglebius			error = EMSGSIZE;
1678228571Sglebius			break;
1679228571Sglebius		}
1680228571Sglebius		if (ifp->if_carp == NULL) {
1681228571Sglebius			error = ENOENT;
1682228571Sglebius			break;
1683228571Sglebius		}
1684228571Sglebius
1685228571Sglebius		priveleged = (priv_check(td, PRIV_NETINET_CARP) == 0);
1686228571Sglebius		if (carpr.carpr_vhid != 0) {
1687228571Sglebius			CIF_LOCK(ifp->if_carp);
1688228571Sglebius			IFNET_FOREACH_CARP(ifp, sc)
1689228571Sglebius				if (sc->sc_vhid == carpr.carpr_vhid)
1690170373Sglebius					break;
1691228571Sglebius			CIF_UNLOCK(ifp->if_carp);
1692228571Sglebius			if (sc == NULL) {
1693228571Sglebius				error = ENOENT;
1694142215Sglebius				break;
1695142215Sglebius			}
1696228571Sglebius			carp_carprcp(&carpr, sc, priveleged);
1697228571Sglebius			error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
1698228571Sglebius		} else  {
1699228571Sglebius			int i, count;
1700228571Sglebius
1701228571Sglebius			count = 0;
1702228571Sglebius			CIF_LOCK(ifp->if_carp);
1703228571Sglebius			IFNET_FOREACH_CARP(ifp, sc)
1704228571Sglebius				count++;
1705228571Sglebius
1706228571Sglebius			if (count > carpr.carpr_count) {
1707228571Sglebius				CIF_UNLOCK(ifp->if_carp);
1708228571Sglebius				error = EMSGSIZE;
1709142215Sglebius				break;
1710142215Sglebius			}
1711228571Sglebius
1712228571Sglebius			i = 0;
1713228571Sglebius			IFNET_FOREACH_CARP(ifp, sc) {
1714228571Sglebius				carp_carprcp(&carpr, sc, priveleged);
1715228571Sglebius				carpr.carpr_count = count;
1716228571Sglebius				error = copyout(&carpr, ifr->ifr_data +
1717228571Sglebius				    (i * sizeof(carpr)), sizeof(carpr));
1718228571Sglebius				if (error) {
1719228571Sglebius					CIF_UNLOCK(ifp->if_carp);
1720228571Sglebius					break;
1721228571Sglebius				}
1722228571Sglebius				i++;
1723228571Sglebius			}
1724228571Sglebius			CIF_UNLOCK(ifp->if_carp);
1725142215Sglebius		}
1726142215Sglebius		break;
1727228571Sglebius	    }
1728142215Sglebius	default:
1729142215Sglebius		error = EINVAL;
1730142215Sglebius	}
1731142215Sglebius
1732228571Sglebiusout:
1733142914Sglebius	if (locked)
1734228571Sglebius		CARP_UNLOCK(sc);
1735228571Sglebius	if_rele(ifp);
1736142914Sglebius
1737142215Sglebius	return (error);
1738142215Sglebius}
1739142215Sglebius
1740142215Sglebiusstatic int
1741228571Sglebiuscarp_get_vhid(struct ifaddr *ifa)
1742142215Sglebius{
1743147611Sdwmalone
1744228571Sglebius	if (ifa == NULL || ifa->ifa_carp == NULL)
1745228571Sglebius		return (0);
1746142215Sglebius
1747228571Sglebius	return (ifa->ifa_carp->sc_vhid);
1748142215Sglebius}
1749142215Sglebius
1750142215Sglebiusint
1751228571Sglebiuscarp_attach(struct ifaddr *ifa, int vhid)
1752142215Sglebius{
1753228571Sglebius	struct ifnet *ifp = ifa->ifa_ifp;
1754142215Sglebius	struct carp_softc *sc;
1755228571Sglebius	int index, error;
1756142215Sglebius
1757228571Sglebius	if (ifp->if_carp == NULL)
1758228571Sglebius		return (ENOPROTOOPT);
1759142215Sglebius
1760228571Sglebius	switch (ifa->ifa_addr->sa_family) {
1761142215Sglebius#ifdef INET
1762142215Sglebius	case AF_INET:
1763228571Sglebius#endif
1764142215Sglebius#ifdef INET6
1765142215Sglebius	case AF_INET6:
1766228571Sglebius#endif
1767142215Sglebius		break;
1768142215Sglebius	default:
1769228571Sglebius		return (EPROTOTYPE);
1770142215Sglebius	}
1771142215Sglebius
1772228571Sglebius	CIF_LOCK(ifp->if_carp);
1773228571Sglebius	IFNET_FOREACH_CARP(ifp, sc)
1774228571Sglebius		if (sc->sc_vhid == vhid)
1775228571Sglebius			break;
1776228571Sglebius	CIF_UNLOCK(ifp->if_carp);
1777228571Sglebius	if (sc == NULL)
1778228571Sglebius		return (ENOENT);
1779142215Sglebius
1780228571Sglebius	if (ifa->ifa_carp) {
1781228571Sglebius		if (ifa->ifa_carp->sc_vhid != vhid)
1782228571Sglebius			carp_detach(ifa);
1783228571Sglebius		else
1784228571Sglebius			return (0);
1785228571Sglebius	}
1786142215Sglebius
1787228571Sglebius	error = carp_multicast_setup(sc, ifa->ifa_addr->sa_family);
1788228571Sglebius	if (error)
1789228571Sglebius		return (error);
1790142215Sglebius
1791228571Sglebius	CARP_LOCK(sc);
1792228571Sglebius	index = sc->sc_naddrs + sc->sc_naddrs6 + 1;
1793228571Sglebius	if (index > sc->sc_ifasiz / sizeof(struct ifaddr *))
1794228571Sglebius		if ((error = carp_grow_ifas(sc)) != 0) {
1795228571Sglebius			carp_multicast_cleanup(sc,
1796228571Sglebius			    ifa->ifa_addr->sa_family);
1797228571Sglebius			CARP_UNLOCK(sc);
1798228571Sglebius			return (error);
1799142215Sglebius		}
1800142215Sglebius
1801228571Sglebius	switch (ifa->ifa_addr->sa_family) {
1802228571Sglebius#ifdef INET
1803228571Sglebius	case AF_INET:
1804228571Sglebius		sc->sc_naddrs++;
1805142215Sglebius		break;
1806228571Sglebius#endif
1807228571Sglebius#ifdef INET6
1808228571Sglebius	case AF_INET6:
1809228571Sglebius		sc->sc_naddrs6++;
1810142215Sglebius		break;
1811228571Sglebius#endif
1812142215Sglebius	}
1813142215Sglebius
1814228571Sglebius	ifa_ref(ifa);
1815228571Sglebius	sc->sc_ifas[index - 1] = ifa;
1816228571Sglebius	ifa->ifa_carp = sc;
1817228571Sglebius
1818228571Sglebius	carp_hmac_prepare(sc);
1819228571Sglebius	carp_sc_state(sc);
1820228571Sglebius
1821228571Sglebius	CARP_UNLOCK(sc);
1822228571Sglebius
1823142215Sglebius	return (0);
1824142215Sglebius}
1825142215Sglebius
1826228571Sglebiusvoid
1827228571Sglebiuscarp_detach(struct ifaddr *ifa)
1828142215Sglebius{
1829228571Sglebius	struct carp_softc *sc = ifa->ifa_carp;
1830228571Sglebius	int i, index;
1831142914Sglebius
1832228571Sglebius	KASSERT(sc != NULL, ("%s: %p not attached", __func__, ifa));
1833142914Sglebius
1834228571Sglebius	CARP_LOCK(sc);
1835142215Sglebius
1836228571Sglebius	/* Shift array. */
1837228571Sglebius	index = sc->sc_naddrs + sc->sc_naddrs6;
1838228571Sglebius	for (i = 0; i < index; i++)
1839228571Sglebius		if (sc->sc_ifas[i] == ifa)
1840228571Sglebius			break;
1841228571Sglebius	KASSERT(i < index, ("%s: %p no backref", __func__, ifa));
1842228571Sglebius	for (; i < index - 1; i++)
1843228571Sglebius		sc->sc_ifas[i] = sc->sc_ifas[i+1];
1844228571Sglebius	sc->sc_ifas[index - 1] = NULL;
1845228571Sglebius
1846228571Sglebius	switch (ifa->ifa_addr->sa_family) {
1847228571Sglebius#ifdef INET
1848228571Sglebius	case AF_INET:
1849228571Sglebius		sc->sc_naddrs--;
1850142215Sglebius		break;
1851228571Sglebius#endif
1852228571Sglebius#ifdef INET6
1853228571Sglebius	case AF_INET6:
1854228571Sglebius		sc->sc_naddrs6--;
1855142215Sglebius		break;
1856228571Sglebius#endif
1857142215Sglebius	}
1858228571Sglebius
1859228571Sglebius	carp_multicast_cleanup(sc, ifa->ifa_addr->sa_family);
1860228571Sglebius
1861228571Sglebius	ifa->ifa_carp = NULL;
1862228571Sglebius	ifa_free(ifa);
1863228571Sglebius
1864228571Sglebius	carp_hmac_prepare(sc);
1865228571Sglebius	carp_sc_state(sc);
1866228571Sglebius
1867228571Sglebius	if (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) {
1868228571Sglebius		CARP_UNLOCK(sc);
1869228571Sglebius		carp_destroy(sc);
1870228571Sglebius	} else
1871228571Sglebius		CARP_UNLOCK(sc);
1872142215Sglebius}
1873142215Sglebius
1874228571Sglebiusstatic void
1875228571Sglebiuscarp_set_state(struct carp_softc *sc, int state)
1876142215Sglebius{
1877142914Sglebius
1878228571Sglebius	CARP_LOCK_ASSERT(sc);
1879228571Sglebius
1880228571Sglebius	if (sc->sc_state != state) {
1881228571Sglebius		const char *carp_states[] = { CARP_STATES };
1882228571Sglebius		char subsys[IFNAMSIZ+5];
1883228571Sglebius
1884228571Sglebius		sc->sc_state = state;
1885228571Sglebius
1886228571Sglebius		snprintf(subsys, IFNAMSIZ+5, "%u@%s", sc->sc_vhid,
1887228571Sglebius		    sc->sc_carpdev->if_xname);
1888228571Sglebius		devctl_notify("CARP", subsys, carp_states[state], NULL);
1889228571Sglebius	}
1890142914Sglebius}
1891142914Sglebius
1892142914Sglebiusstatic void
1893228571Sglebiuscarp_linkstate(struct ifnet *ifp)
1894142914Sglebius{
1895142215Sglebius	struct carp_softc *sc;
1896142215Sglebius
1897228571Sglebius	CIF_LOCK(ifp->if_carp);
1898228571Sglebius	IFNET_FOREACH_CARP(ifp, sc) {
1899228571Sglebius		CARP_LOCK(sc);
1900228571Sglebius		carp_sc_state(sc);
1901228571Sglebius		CARP_UNLOCK(sc);
1902228571Sglebius	}
1903228571Sglebius	CIF_UNLOCK(ifp->if_carp);
1904144329Sglebius}
1905144329Sglebius
1906144329Sglebiusstatic void
1907228571Sglebiuscarp_sc_state(struct carp_softc *sc)
1908144329Sglebius{
1909144329Sglebius
1910228571Sglebius	CARP_LOCK_ASSERT(sc);
1911228571Sglebius
1912144329Sglebius	if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1913144329Sglebius	    !(sc->sc_carpdev->if_flags & IFF_UP)) {
1914144329Sglebius		callout_stop(&sc->sc_ad_tmo);
1915228571Sglebius#ifdef INET
1916144329Sglebius		callout_stop(&sc->sc_md_tmo);
1917228571Sglebius#endif
1918228571Sglebius#ifdef INET6
1919144329Sglebius		callout_stop(&sc->sc_md6_tmo);
1920228571Sglebius#endif
1921144329Sglebius		carp_set_state(sc, INIT);
1922144329Sglebius		carp_setrun(sc, 0);
1923228736Sglebius		if (!sc->sc_suppress)
1924228736Sglebius			carp_demote_adj(carp_ifdown_adj, "interface down");
1925144329Sglebius		sc->sc_suppress = 1;
1926144329Sglebius	} else {
1927144329Sglebius		carp_set_state(sc, INIT);
1928144329Sglebius		carp_setrun(sc, 0);
1929144329Sglebius		if (sc->sc_suppress)
1930228736Sglebius			carp_demote_adj(-carp_ifdown_adj, "interface up");
1931144329Sglebius		sc->sc_suppress = 0;
1932142215Sglebius	}
1933142215Sglebius}
1934142215Sglebius
1935228736Sglebiusstatic void
1936228736Sglebiuscarp_demote_adj(int adj, char *reason)
1937228736Sglebius{
1938228736Sglebius	carp_demotion += adj;
1939228736Sglebius	CARP_LOG("demoted by %d to %d (%s)\n", adj, carp_demotion, reason);
1940228736Sglebius	taskqueue_enqueue(taskqueue_swi, &carp_sendall_task);
1941228736Sglebius}
1942228571Sglebius
1943211157Swill#ifdef INET
1944211157Swillextern  struct domain inetdomain;
1945211157Swillstatic struct protosw in_carp_protosw = {
1946211157Swill	.pr_type =		SOCK_RAW,
1947211157Swill	.pr_domain =		&inetdomain,
1948211157Swill	.pr_protocol =		IPPROTO_CARP,
1949211157Swill	.pr_flags =		PR_ATOMIC|PR_ADDR,
1950211157Swill	.pr_input =		carp_input,
1951211157Swill	.pr_output =		(pr_output_t *)rip_output,
1952211157Swill	.pr_ctloutput =		rip_ctloutput,
1953211157Swill	.pr_usrreqs =		&rip_usrreqs
1954211157Swill};
1955211157Swill#endif
1956211157Swill
1957211157Swill#ifdef INET6
1958211157Swillextern	struct domain inet6domain;
1959211157Swillstatic struct ip6protosw in6_carp_protosw = {
1960211157Swill	.pr_type =		SOCK_RAW,
1961211157Swill	.pr_domain =		&inet6domain,
1962211157Swill	.pr_protocol =		IPPROTO_CARP,
1963211157Swill	.pr_flags =		PR_ATOMIC|PR_ADDR,
1964211157Swill	.pr_input =		carp6_input,
1965211157Swill	.pr_output =		rip6_output,
1966211157Swill	.pr_ctloutput =		rip6_ctloutput,
1967211157Swill	.pr_usrreqs =		&rip6_usrreqs
1968211157Swill};
1969211157Swill#endif
1970211157Swill
1971211157Swillstatic void
1972211157Swillcarp_mod_cleanup(void)
1973211157Swill{
1974211157Swill
1975211157Swill#ifdef INET
1976211157Swill	if (proto_reg[CARP_INET] == 0) {
1977212266Swill		(void)ipproto_unregister(IPPROTO_CARP);
1978211157Swill		pf_proto_unregister(PF_INET, IPPROTO_CARP, SOCK_RAW);
1979211157Swill		proto_reg[CARP_INET] = -1;
1980211157Swill	}
1981211157Swill	carp_iamatch_p = NULL;
1982211157Swill#endif
1983211157Swill#ifdef INET6
1984211157Swill	if (proto_reg[CARP_INET6] == 0) {
1985212266Swill		(void)ip6proto_unregister(IPPROTO_CARP);
1986211157Swill		pf_proto_unregister(PF_INET6, IPPROTO_CARP, SOCK_RAW);
1987211157Swill		proto_reg[CARP_INET6] = -1;
1988211157Swill	}
1989211157Swill	carp_iamatch6_p = NULL;
1990211157Swill	carp_macmatch6_p = NULL;
1991211157Swill#endif
1992228571Sglebius	carp_ioctl_p = NULL;
1993228571Sglebius	carp_attach_p = NULL;
1994228571Sglebius	carp_detach_p = NULL;
1995228571Sglebius	carp_get_vhid_p = NULL;
1996211157Swill	carp_linkstate_p = NULL;
1997211157Swill	carp_forus_p = NULL;
1998211157Swill	carp_output_p = NULL;
1999228736Sglebius	carp_demote_adj_p = NULL;
2000228736Sglebius	mtx_unlock(&carp_mtx);
2001228736Sglebius	taskqueue_drain(taskqueue_swi, &carp_sendall_task);
2002211157Swill	mtx_destroy(&carp_mtx);
2003211157Swill}
2004211157Swill
2005142215Sglebiusstatic int
2006211157Swillcarp_mod_load(void)
2007211157Swill{
2008212266Swill	int err;
2009211157Swill
2010211157Swill	mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
2011228571Sglebius	LIST_INIT(&carp_list);
2012228571Sglebius	carp_get_vhid_p = carp_get_vhid;
2013211157Swill	carp_forus_p = carp_forus;
2014211157Swill	carp_output_p = carp_output;
2015228571Sglebius	carp_linkstate_p = carp_linkstate;
2016228571Sglebius	carp_ioctl_p = carp_ioctl;
2017228571Sglebius	carp_attach_p = carp_attach;
2018228571Sglebius	carp_detach_p = carp_detach;
2019228736Sglebius	carp_demote_adj_p = carp_demote_adj;
2020211157Swill#ifdef INET6
2021211157Swill	carp_iamatch6_p = carp_iamatch6;
2022211157Swill	carp_macmatch6_p = carp_macmatch6;
2023211157Swill	proto_reg[CARP_INET6] = pf_proto_register(PF_INET6,
2024211157Swill	    (struct protosw *)&in6_carp_protosw);
2025228571Sglebius	if (proto_reg[CARP_INET6]) {
2026211157Swill		printf("carp: error %d attaching to PF_INET6\n",
2027211157Swill		    proto_reg[CARP_INET6]);
2028211157Swill		carp_mod_cleanup();
2029212898Sglebius		return (proto_reg[CARP_INET6]);
2030211157Swill	}
2031212266Swill	err = ip6proto_register(IPPROTO_CARP);
2032212266Swill	if (err) {
2033212266Swill		printf("carp: error %d registering with INET6\n", err);
2034212266Swill		carp_mod_cleanup();
2035212898Sglebius		return (err);
2036212266Swill	}
2037211157Swill#endif
2038211157Swill#ifdef INET
2039211157Swill	carp_iamatch_p = carp_iamatch;
2040211157Swill	proto_reg[CARP_INET] = pf_proto_register(PF_INET, &in_carp_protosw);
2041228571Sglebius	if (proto_reg[CARP_INET]) {
2042211157Swill		printf("carp: error %d attaching to PF_INET\n",
2043211157Swill		    proto_reg[CARP_INET]);
2044211157Swill		carp_mod_cleanup();
2045212898Sglebius		return (proto_reg[CARP_INET]);
2046211157Swill	}
2047212266Swill	err = ipproto_register(IPPROTO_CARP);
2048212266Swill	if (err) {
2049212266Swill		printf("carp: error %d registering with INET\n", err);
2050212266Swill		carp_mod_cleanup();
2051212898Sglebius		return (err);
2052212266Swill	}
2053211157Swill#endif
2054228571Sglebius	return (0);
2055211157Swill}
2056211157Swill
2057211157Swillstatic int
2058142215Sglebiuscarp_modevent(module_t mod, int type, void *data)
2059142215Sglebius{
2060142215Sglebius	switch (type) {
2061142215Sglebius	case MOD_LOAD:
2062211157Swill		return carp_mod_load();
2063211157Swill		/* NOTREACHED */
2064142215Sglebius	case MOD_UNLOAD:
2065228571Sglebius		mtx_lock(&carp_mtx);
2066228571Sglebius		if (LIST_EMPTY(&carp_list))
2067228571Sglebius			carp_mod_cleanup();
2068228571Sglebius		else {
2069228571Sglebius			mtx_unlock(&carp_mtx);
2070228571Sglebius			return (EBUSY);
2071228571Sglebius		}
2072142215Sglebius		break;
2073142215Sglebius
2074142215Sglebius	default:
2075156947Sglebius		return (EINVAL);
2076142215Sglebius	}
2077142215Sglebius
2078156947Sglebius	return (0);
2079142215Sglebius}
2080142215Sglebius
2081142215Sglebiusstatic moduledata_t carp_mod = {
2082142215Sglebius	"carp",
2083142215Sglebius	carp_modevent,
2084142215Sglebius	0
2085142215Sglebius};
2086142215Sglebius
2087212265SwillDECLARE_MODULE(carp, carp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
2088