ip_carp.c revision 148387
1142215Sglebius/* 	$FreeBSD: head/sys/netinet/ip_carp.c 148387 2005-07-25 12:36:43Z ume $ */
2142215Sglebius
3142215Sglebius/*
4142215Sglebius * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
5142215Sglebius * Copyright (c) 2003 Ryan McBride. 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
29142215Sglebius#include "opt_carp.h"
30142215Sglebius#include "opt_bpf.h"
31142215Sglebius#include "opt_inet.h"
32142215Sglebius#include "opt_inet6.h"
33142215Sglebius
34142215Sglebius#include <sys/types.h>
35142215Sglebius#include <sys/param.h>
36142215Sglebius#include <sys/systm.h>
37142215Sglebius#include <sys/conf.h>
38142215Sglebius#include <sys/kernel.h>
39142215Sglebius#include <sys/limits.h>
40142215Sglebius#include <sys/malloc.h>
41142215Sglebius#include <sys/mbuf.h>
42142215Sglebius#include <sys/module.h>
43142215Sglebius#include <sys/time.h>
44142215Sglebius#include <sys/proc.h>
45142215Sglebius#include <sys/sysctl.h>
46142215Sglebius#include <sys/syslog.h>
47142215Sglebius#include <sys/signalvar.h>
48142215Sglebius#include <sys/filio.h>
49142215Sglebius#include <sys/sockio.h>
50142215Sglebius
51142215Sglebius#include <sys/socket.h>
52142215Sglebius#include <sys/vnode.h>
53142215Sglebius
54142215Sglebius#include <machine/stdarg.h>
55142215Sglebius
56142215Sglebius#include <net/bpf.h>
57142215Sglebius#include <net/ethernet.h>
58142215Sglebius#include <net/fddi.h>
59142215Sglebius#include <net/iso88025.h>
60142215Sglebius#include <net/if.h>
61142215Sglebius#include <net/if_clone.h>
62142215Sglebius#include <net/if_types.h>
63142215Sglebius#include <net/route.h>
64142215Sglebius
65142215Sglebius#ifdef INET
66142215Sglebius#include <netinet/in.h>
67142215Sglebius#include <netinet/in_var.h>
68142215Sglebius#include <netinet/in_systm.h>
69142215Sglebius#include <netinet/ip.h>
70142215Sglebius#include <netinet/ip_var.h>
71142215Sglebius#include <netinet/if_ether.h>
72142215Sglebius#include <machine/in_cksum.h>
73142215Sglebius#endif
74142215Sglebius
75142215Sglebius#ifdef INET6
76142215Sglebius#include <netinet/icmp6.h>
77142215Sglebius#include <netinet/ip6.h>
78142215Sglebius#include <netinet6/ip6_var.h>
79148387Sume#include <netinet6/scope6_var.h>
80142215Sglebius#include <netinet6/nd6.h>
81142215Sglebius#include <net/if_dl.h>
82142215Sglebius#endif
83142215Sglebius
84142215Sglebius#include <crypto/sha1.h>
85142215Sglebius#include <netinet/ip_carp.h>
86142215Sglebius
87142215Sglebius#define	CARP_IFNAME	"carp"
88142215Sglebiusstatic MALLOC_DEFINE(M_CARP, "CARP", "CARP interfaces");
89142215SglebiusSYSCTL_DECL(_net_inet_carp);
90142215Sglebius
91142215Sglebiusstruct carp_softc {
92147256Sbrooks	struct ifnet	 	*sc_ifp;	/* Interface clue */
93142901Sglebius	struct ifnet		*sc_carpdev;	/* Pointer to parent interface */
94142215Sglebius	struct in_ifaddr 	*sc_ia;		/* primary iface address */
95142215Sglebius	struct ip_moptions 	 sc_imo;
96142215Sglebius#ifdef INET6
97142215Sglebius	struct in6_ifaddr 	*sc_ia6;	/* primary iface address v6 */
98142215Sglebius	struct ip6_moptions 	 sc_im6o;
99142215Sglebius#endif /* INET6 */
100142215Sglebius	TAILQ_ENTRY(carp_softc)	 sc_list;
101142215Sglebius
102142215Sglebius	enum { INIT = 0, BACKUP, MASTER }	sc_state;
103142215Sglebius
104142215Sglebius	int			 sc_flags_backup;
105142215Sglebius	int			 sc_suppress;
106142215Sglebius
107142215Sglebius	int			 sc_sendad_errors;
108142215Sglebius#define	CARP_SENDAD_MAX_ERRORS	3
109142215Sglebius	int			 sc_sendad_success;
110142215Sglebius#define	CARP_SENDAD_MIN_SUCCESS 3
111142215Sglebius
112142215Sglebius	int			 sc_vhid;
113142215Sglebius	int			 sc_advskew;
114142215Sglebius	int			 sc_naddrs;
115142215Sglebius	int			 sc_naddrs6;
116142215Sglebius	int			 sc_advbase;	/* seconds */
117142215Sglebius	int			 sc_init_counter;
118142215Sglebius	u_int64_t		 sc_counter;
119142215Sglebius
120142215Sglebius	/* authentication */
121142215Sglebius#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
126142215Sglebius	struct callout		 sc_ad_tmo;	/* advertisement timeout */
127142215Sglebius	struct callout		 sc_md_tmo;	/* master down timeout */
128142215Sglebius	struct callout 		 sc_md6_tmo;	/* master down timeout */
129142215Sglebius
130142215Sglebius	LIST_ENTRY(carp_softc)	 sc_next;	/* Interface clue */
131142215Sglebius};
132147256Sbrooks#define	SC2IFP(sc)	((sc)->sc_ifp)
133142215Sglebius
134142215Sglebiusint carp_suppress_preempt = 0;
135142215Sglebiusint carp_opts[CARPCTL_MAXID] = { 0, 1, 0, 1, 0, 0 };	/* XXX for now */
136142215SglebiusSYSCTL_INT(_net_inet_carp, CARPCTL_ALLOW, allow, CTLFLAG_RW,
137142215Sglebius    &carp_opts[CARPCTL_ALLOW], 0, "Accept incoming CARP packets");
138142215SglebiusSYSCTL_INT(_net_inet_carp, CARPCTL_PREEMPT, preempt, CTLFLAG_RW,
139142215Sglebius    &carp_opts[CARPCTL_PREEMPT], 0, "high-priority backup preemption mode");
140142215SglebiusSYSCTL_INT(_net_inet_carp, CARPCTL_LOG, log, CTLFLAG_RW,
141142215Sglebius    &carp_opts[CARPCTL_LOG], 0, "log bad carp packets");
142142215SglebiusSYSCTL_INT(_net_inet_carp, CARPCTL_ARPBALANCE, arpbalance, CTLFLAG_RW,
143142215Sglebius    &carp_opts[CARPCTL_ARPBALANCE], 0, "balance arp responses");
144146226SglebiusSYSCTL_INT(_net_inet_carp, OID_AUTO, suppress_preempt, CTLFLAG_RD,
145146226Sglebius    &carp_suppress_preempt, 0, "Preemption is suppressed");
146142215Sglebius
147142215Sglebiusstruct carpstats carpstats;
148142215SglebiusSYSCTL_STRUCT(_net_inet_carp, CARPCTL_STATS, stats, CTLFLAG_RW,
149142215Sglebius    &carpstats, carpstats,
150142215Sglebius    "CARP statistics (struct carpstats, netinet/ip_carp.h)");
151142215Sglebius
152142215Sglebiusstruct carp_if {
153142215Sglebius	TAILQ_HEAD(, carp_softc) vhif_vrs;
154142215Sglebius	int vhif_nvrs;
155142215Sglebius
156142215Sglebius	struct ifnet 	*vhif_ifp;
157142215Sglebius	struct mtx	 vhif_mtx;
158142215Sglebius};
159142914Sglebius
160142914Sglebius/* Get carp_if from softc. Valid after carp_set_addr{,6}. */
161142914Sglebius#define	SC2CIF(sc)		((struct carp_if *)(sc)->sc_carpdev->if_carp)
162142914Sglebius
163142215Sglebius/* lock per carp_if queue */
164142914Sglebius#define	CARP_LOCK_INIT(cif)	mtx_init(&(cif)->vhif_mtx, "carp_if", 	\
165142215Sglebius	NULL, MTX_DEF)
166142914Sglebius#define	CARP_LOCK_DESTROY(cif)	mtx_destroy(&(cif)->vhif_mtx)
167142215Sglebius#define	CARP_LOCK_ASSERT(cif)	mtx_assert(&(cif)->vhif_mtx, MA_OWNED)
168142215Sglebius#define	CARP_LOCK(cif)		mtx_lock(&(cif)->vhif_mtx)
169142215Sglebius#define	CARP_UNLOCK(cif)	mtx_unlock(&(cif)->vhif_mtx)
170142215Sglebius
171142914Sglebius#define	CARP_SCLOCK(sc)		mtx_lock(&SC2CIF(sc)->vhif_mtx)
172142914Sglebius#define	CARP_SCUNLOCK(sc)	mtx_unlock(&SC2CIF(sc)->vhif_mtx)
173142914Sglebius#define	CARP_SCLOCK_ASSERT(sc)	mtx_assert(&SC2CIF(sc)->vhif_mtx, MA_OWNED)
174142914Sglebius
175142451Sglebius#define	CARP_LOG(...)	do {				\
176142446Sglebius	if (carp_opts[CARPCTL_LOG] > 0)			\
177142446Sglebius		log(LOG_INFO, __VA_ARGS__);		\
178142451Sglebius} while (0)
179142215Sglebius
180142451Sglebius#define	CARP_DEBUG(...)	do {				\
181142446Sglebius	if (carp_opts[CARPCTL_LOG] > 1)			\
182142446Sglebius		log(LOG_DEBUG, __VA_ARGS__);		\
183142451Sglebius} while (0)
184142446Sglebius
185142559Sglebiusstatic void	carp_hmac_prepare(struct carp_softc *);
186142559Sglebiusstatic void	carp_hmac_generate(struct carp_softc *, u_int32_t *,
187142559Sglebius		    unsigned char *);
188142559Sglebiusstatic int	carp_hmac_verify(struct carp_softc *, u_int32_t *,
189142559Sglebius		    unsigned char *);
190142559Sglebiusstatic void	carp_setroute(struct carp_softc *, int);
191142559Sglebiusstatic void	carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
192142559Sglebiusstatic int 	carp_clone_create(struct if_clone *, int);
193142559Sglebiusstatic void 	carp_clone_destroy(struct ifnet *);
194142559Sglebiusstatic void	carpdetach(struct carp_softc *);
195142559Sglebiusstatic int	carp_prepare_ad(struct mbuf *, struct carp_softc *,
196142559Sglebius		    struct carp_header *);
197142559Sglebiusstatic void	carp_send_ad_all(void);
198142559Sglebiusstatic void	carp_send_ad(void *);
199142914Sglebiusstatic void	carp_send_ad_locked(struct carp_softc *);
200142559Sglebiusstatic void	carp_send_arp(struct carp_softc *);
201142559Sglebiusstatic void	carp_master_down(void *);
202142914Sglebiusstatic void	carp_master_down_locked(struct carp_softc *);
203142559Sglebiusstatic int	carp_ioctl(struct ifnet *, u_long, caddr_t);
204142559Sglebiusstatic int	carp_looutput(struct ifnet *, struct mbuf *, struct sockaddr *,
205142559Sglebius		    struct rtentry *);
206142559Sglebiusstatic void	carp_start(struct ifnet *);
207142559Sglebiusstatic void	carp_setrun(struct carp_softc *, sa_family_t);
208142559Sglebiusstatic void	carp_set_state(struct carp_softc *, int);
209142559Sglebiusstatic int	carp_addrcount(struct carp_if *, struct in_ifaddr *, int);
210142215Sglebiusenum	{ CARP_COUNT_MASTER, CARP_COUNT_RUNNING };
211142215Sglebius
212142559Sglebiusstatic int	carp_set_addr(struct carp_softc *, struct sockaddr_in *);
213142559Sglebiusstatic int	carp_del_addr(struct carp_softc *, struct sockaddr_in *);
214142914Sglebiusstatic void	carp_carpdev_state_locked(struct carp_if *);
215144329Sglebiusstatic void	carp_sc_state_locked(struct carp_softc *);
216142215Sglebius#ifdef INET6
217142559Sglebiusstatic void	carp_send_na(struct carp_softc *);
218142559Sglebiusstatic int	carp_set_addr6(struct carp_softc *, struct sockaddr_in6 *);
219142559Sglebiusstatic int	carp_del_addr6(struct carp_softc *, struct sockaddr_in6 *);
220142215Sglebius#endif
221142215Sglebius
222142215Sglebiusstatic LIST_HEAD(, carp_softc) carpif_list;
223142911Sglebiusstatic struct mtx carp_mtx;
224142215SglebiusIFC_SIMPLE_DECLARE(carp, 0);
225142215Sglebius
226142215Sglebiusstatic __inline u_int16_t
227142215Sglebiuscarp_cksum(struct mbuf *m, int len)
228142215Sglebius{
229142215Sglebius	return (in_cksum(m, len));
230142215Sglebius}
231142215Sglebius
232142559Sglebiusstatic void
233142215Sglebiuscarp_hmac_prepare(struct carp_softc *sc)
234142215Sglebius{
235142215Sglebius	u_int8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
236142215Sglebius	u_int8_t vhid = sc->sc_vhid & 0xff;
237142215Sglebius	struct ifaddr *ifa;
238142215Sglebius	int i;
239142215Sglebius#ifdef INET6
240142215Sglebius	struct in6_addr in6;
241142215Sglebius#endif
242142215Sglebius
243142914Sglebius	if (sc->sc_carpdev)
244142914Sglebius		CARP_SCLOCK(sc);
245142914Sglebius
246142914Sglebius	/* XXX: possible race here */
247142914Sglebius
248142215Sglebius	/* compute ipad from key */
249142215Sglebius	bzero(sc->sc_pad, sizeof(sc->sc_pad));
250142215Sglebius	bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
251142215Sglebius	for (i = 0; i < sizeof(sc->sc_pad); i++)
252142215Sglebius		sc->sc_pad[i] ^= 0x36;
253142215Sglebius
254142215Sglebius	/* precompute first part of inner hash */
255142215Sglebius	SHA1Init(&sc->sc_sha1);
256142215Sglebius	SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
257142215Sglebius	SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
258142215Sglebius	SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
259142215Sglebius	SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
260142215Sglebius#ifdef INET
261147256Sbrooks	TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
262142215Sglebius		if (ifa->ifa_addr->sa_family == AF_INET)
263142215Sglebius			SHA1Update(&sc->sc_sha1,
264142215Sglebius			    (void *)&ifatoia(ifa)->ia_addr.sin_addr.s_addr,
265142215Sglebius			    sizeof(struct in_addr));
266142215Sglebius	}
267142215Sglebius#endif /* INET */
268142215Sglebius#ifdef INET6
269147256Sbrooks	TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
270142215Sglebius		if (ifa->ifa_addr->sa_family == AF_INET6) {
271142215Sglebius			in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
272148385Sume			in6_clearscope(&in6);
273142215Sglebius			SHA1Update(&sc->sc_sha1, (void *)&in6, sizeof(in6));
274142215Sglebius		}
275142215Sglebius	}
276142215Sglebius#endif /* INET6 */
277142215Sglebius
278142215Sglebius	/* convert ipad to opad */
279142215Sglebius	for (i = 0; i < sizeof(sc->sc_pad); i++)
280142215Sglebius		sc->sc_pad[i] ^= 0x36 ^ 0x5c;
281142914Sglebius
282142914Sglebius	if (sc->sc_carpdev)
283142914Sglebius		CARP_SCUNLOCK(sc);
284142215Sglebius}
285142215Sglebius
286142559Sglebiusstatic void
287142215Sglebiuscarp_hmac_generate(struct carp_softc *sc, u_int32_t counter[2],
288142215Sglebius    unsigned char md[20])
289142215Sglebius{
290142215Sglebius	SHA1_CTX sha1ctx;
291142215Sglebius
292142215Sglebius	/* fetch first half of inner hash */
293142215Sglebius	bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
294142215Sglebius
295142215Sglebius	SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
296142215Sglebius	SHA1Final(md, &sha1ctx);
297142215Sglebius
298142215Sglebius	/* outer hash */
299142215Sglebius	SHA1Init(&sha1ctx);
300142215Sglebius	SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
301142215Sglebius	SHA1Update(&sha1ctx, md, 20);
302142215Sglebius	SHA1Final(md, &sha1ctx);
303142215Sglebius}
304142215Sglebius
305142559Sglebiusstatic int
306142215Sglebiuscarp_hmac_verify(struct carp_softc *sc, u_int32_t counter[2],
307142215Sglebius    unsigned char md[20])
308142215Sglebius{
309142215Sglebius	unsigned char md2[20];
310142215Sglebius
311142914Sglebius	CARP_SCLOCK_ASSERT(sc);
312142914Sglebius
313142215Sglebius	carp_hmac_generate(sc, counter, md2);
314142215Sglebius
315142215Sglebius	return (bcmp(md, md2, sizeof(md2)));
316142215Sglebius}
317142215Sglebius
318142559Sglebiusstatic void
319142215Sglebiuscarp_setroute(struct carp_softc *sc, int cmd)
320142215Sglebius{
321142215Sglebius	struct ifaddr *ifa;
322142215Sglebius	int s;
323142215Sglebius
324142914Sglebius	if (sc->sc_carpdev)
325142914Sglebius		CARP_SCLOCK_ASSERT(sc);
326142914Sglebius
327142215Sglebius	s = splnet();
328147256Sbrooks	TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
329142914Sglebius		if (ifa->ifa_addr->sa_family == AF_INET &&
330142914Sglebius		    sc->sc_carpdev != NULL) {
331142215Sglebius			int count = carp_addrcount(
332142564Sglebius			    (struct carp_if *)sc->sc_carpdev->if_carp,
333142215Sglebius			    ifatoia(ifa), CARP_COUNT_MASTER);
334142215Sglebius
335142215Sglebius			if ((cmd == RTM_ADD && count == 1) ||
336142215Sglebius			    (cmd == RTM_DELETE && count == 0))
337142215Sglebius				rtinit(ifa, cmd, RTF_UP | RTF_HOST);
338142215Sglebius		}
339142215Sglebius#ifdef INET6
340142215Sglebius		if (ifa->ifa_addr->sa_family == AF_INET6) {
341142215Sglebius			if (cmd == RTM_ADD)
342142215Sglebius				in6_ifaddloop(ifa);
343142215Sglebius			else
344142215Sglebius				in6_ifremloop(ifa);
345142215Sglebius		}
346142215Sglebius#endif /* INET6 */
347142215Sglebius	}
348142215Sglebius	splx(s);
349142215Sglebius}
350142215Sglebius
351142559Sglebiusstatic int
352142215Sglebiuscarp_clone_create(struct if_clone *ifc, int unit)
353142215Sglebius{
354142215Sglebius
355142215Sglebius	struct carp_softc *sc;
356142215Sglebius	struct ifnet *ifp;
357142215Sglebius
358142215Sglebius	MALLOC(sc, struct carp_softc *, sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
359147256Sbrooks	ifp = SC2IFP(sc) = if_alloc(IFT_ETHER);
360147256Sbrooks	if (ifp == NULL) {
361147256Sbrooks		FREE(sc, M_CARP);
362147256Sbrooks		return (ENOSPC);
363147256Sbrooks	}
364142215Sglebius
365142215Sglebius	sc->sc_flags_backup = 0;
366142215Sglebius	sc->sc_suppress = 0;
367142215Sglebius	sc->sc_advbase = CARP_DFLTINTV;
368142215Sglebius	sc->sc_vhid = -1;	/* required setting */
369142215Sglebius	sc->sc_advskew = 0;
370142215Sglebius	sc->sc_init_counter = 1;
371142215Sglebius	sc->sc_naddrs = sc->sc_naddrs6 = 0; /* M_ZERO? */
372142215Sglebius#ifdef INET6
373142215Sglebius	sc->sc_im6o.im6o_multicast_hlim = CARP_DFLTTL;
374142215Sglebius#endif
375142215Sglebius
376142914Sglebius	callout_init(&sc->sc_ad_tmo, NET_CALLOUT_MPSAFE);
377142914Sglebius	callout_init(&sc->sc_md_tmo, NET_CALLOUT_MPSAFE);
378142914Sglebius	callout_init(&sc->sc_md6_tmo, NET_CALLOUT_MPSAFE);
379142215Sglebius
380142215Sglebius	ifp->if_softc = sc;
381142215Sglebius	if_initname(ifp, CARP_IFNAME, unit);
382142215Sglebius	ifp->if_mtu = ETHERMTU;
383142215Sglebius	ifp->if_flags = 0;
384142215Sglebius	ifp->if_ioctl = carp_ioctl;
385142215Sglebius	ifp->if_output = carp_looutput;
386142215Sglebius	ifp->if_start = carp_start;
387142215Sglebius	ifp->if_type = IFT_CARP;
388142215Sglebius	ifp->if_snd.ifq_maxlen = ifqmaxlen;
389142215Sglebius	ifp->if_hdrlen = 0;
390142215Sglebius	if_attach(ifp);
391147256Sbrooks	bpfattach(SC2IFP(sc), DLT_NULL, sizeof(u_int32_t));
392142911Sglebius	mtx_lock(&carp_mtx);
393142215Sglebius	LIST_INSERT_HEAD(&carpif_list, sc, sc_next);
394142911Sglebius	mtx_unlock(&carp_mtx);
395142215Sglebius	return (0);
396142215Sglebius}
397142215Sglebius
398142559Sglebiusstatic void
399142215Sglebiuscarp_clone_destroy(struct ifnet *ifp)
400142215Sglebius{
401142215Sglebius	struct carp_softc *sc = ifp->if_softc;
402142215Sglebius	struct carp_if *cif;
403142215Sglebius	struct ip_moptions *imo = &sc->sc_imo;
404142215Sglebius#ifdef INET6
405142215Sglebius	struct ip6_moptions *im6o = &sc->sc_im6o;
406142215Sglebius#endif
407142215Sglebius
408142215Sglebius/*	carpdetach(sc); */
409146226Sglebius
410146226Sglebius	/*
411146226Sglebius	 * If an interface is destroyed which is suppressing the preemption,
412146226Sglebius	 * decrease the global counter, otherwise the host will never get
413146226Sglebius	 * out of the carp supressing state.
414146226Sglebius	 */
415146226Sglebius	if (sc->sc_suppress)
416146226Sglebius		carp_suppress_preempt--;
417146226Sglebius	sc->sc_suppress = 0;
418146226Sglebius
419142215Sglebius	callout_stop(&sc->sc_ad_tmo);
420142215Sglebius	callout_stop(&sc->sc_md_tmo);
421142215Sglebius	callout_stop(&sc->sc_md6_tmo);
422142215Sglebius
423142215Sglebius	if (imo->imo_num_memberships) {
424142215Sglebius		in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
425142215Sglebius		imo->imo_multicast_ifp = NULL;
426142215Sglebius	}
427142215Sglebius#ifdef INET6
428142215Sglebius	while (!LIST_EMPTY(&im6o->im6o_memberships)) {
429142215Sglebius		struct in6_multi_mship *imm =
430142215Sglebius		    LIST_FIRST(&im6o->im6o_memberships);
431142215Sglebius		LIST_REMOVE(imm, i6mm_chain);
432142215Sglebius		in6_leavegroup(imm);
433142215Sglebius	}
434142215Sglebius	im6o->im6o_multicast_ifp = NULL;
435142215Sglebius#endif
436142215Sglebius
437142215Sglebius	/* Remove ourself from parents if_carp queue */
438142564Sglebius	if (sc->sc_carpdev && (cif = sc->sc_carpdev->if_carp)) {
439142215Sglebius		CARP_LOCK(cif);
440142215Sglebius		TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
441142215Sglebius		if (!--cif->vhif_nvrs) {
442142564Sglebius			sc->sc_carpdev->if_carp = NULL;
443142215Sglebius			CARP_LOCK_DESTROY(cif);
444142215Sglebius			FREE(cif, M_CARP);
445142564Sglebius			ifpromisc(sc->sc_carpdev, 0);
446142215Sglebius		} else {
447142215Sglebius			CARP_UNLOCK(cif);
448142215Sglebius		}
449142215Sglebius	}
450142215Sglebius
451142911Sglebius	mtx_lock(&carp_mtx);
452142911Sglebius	LIST_REMOVE(sc, sc_next);
453142911Sglebius	mtx_unlock(&carp_mtx);
454142215Sglebius	bpfdetach(ifp);
455142215Sglebius	if_detach(ifp);
456147256Sbrooks	if_free_type(ifp, IFT_ETHER);
457142215Sglebius	free(sc, M_CARP);
458142215Sglebius}
459142215Sglebius
460142215Sglebius/*
461142215Sglebius * process input packet.
462142215Sglebius * we have rearranged checks order compared to the rfc,
463142215Sglebius * but it seems more efficient this way or not possible otherwise.
464142215Sglebius */
465142215Sglebiusvoid
466142215Sglebiuscarp_input(struct mbuf *m, int hlen)
467142215Sglebius{
468142215Sglebius	struct ip *ip = mtod(m, struct ip *);
469142215Sglebius	struct carp_header *ch;
470142215Sglebius	int iplen, len;
471142215Sglebius
472142215Sglebius	carpstats.carps_ipackets++;
473142215Sglebius
474142215Sglebius	if (!carp_opts[CARPCTL_ALLOW]) {
475142215Sglebius		m_freem(m);
476142215Sglebius		return;
477142215Sglebius	}
478142215Sglebius
479142215Sglebius	/* check if received on a valid carp interface */
480142215Sglebius	if (m->m_pkthdr.rcvif->if_carp == NULL) {
481142215Sglebius		carpstats.carps_badif++;
482142452Sglebius		CARP_LOG("carp_input: packet received on non-carp "
483142452Sglebius		    "interface: %s\n",
484142446Sglebius		    m->m_pkthdr.rcvif->if_xname);
485142215Sglebius		m_freem(m);
486142215Sglebius		return;
487142215Sglebius	}
488142215Sglebius
489142215Sglebius	/* verify that the IP TTL is 255.  */
490142215Sglebius	if (ip->ip_ttl != CARP_DFLTTL) {
491142215Sglebius		carpstats.carps_badttl++;
492142452Sglebius		CARP_LOG("carp_input: received ttl %d != 255i on %s\n",
493142446Sglebius		    ip->ip_ttl,
494142446Sglebius		    m->m_pkthdr.rcvif->if_xname);
495142215Sglebius		m_freem(m);
496142215Sglebius		return;
497142215Sglebius	}
498142215Sglebius
499142215Sglebius	iplen = ip->ip_hl << 2;
500142215Sglebius
501142215Sglebius	if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
502142215Sglebius		carpstats.carps_badlen++;
503142446Sglebius		CARP_LOG("carp_input: received len %zd < "
504142452Sglebius		    "sizeof(struct carp_header)\n",
505142446Sglebius		    m->m_len - sizeof(struct ip));
506142215Sglebius		m_freem(m);
507142215Sglebius		return;
508142215Sglebius	}
509142215Sglebius
510142215Sglebius	if (iplen + sizeof(*ch) < m->m_len) {
511142215Sglebius		if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
512142215Sglebius			carpstats.carps_hdrops++;
513142452Sglebius			CARP_LOG("carp_input: pullup failed\n");
514142215Sglebius			return;
515142215Sglebius		}
516142215Sglebius		ip = mtod(m, struct ip *);
517142215Sglebius	}
518142215Sglebius	ch = (struct carp_header *)((char *)ip + iplen);
519142215Sglebius
520142215Sglebius	/*
521142215Sglebius	 * verify that the received packet length is
522142215Sglebius	 * equal to the CARP header
523142215Sglebius	 */
524142215Sglebius	len = iplen + sizeof(*ch);
525142215Sglebius	if (len > m->m_pkthdr.len) {
526142215Sglebius		carpstats.carps_badlen++;
527142452Sglebius		CARP_LOG("carp_input: packet too short %d on %s\n",
528142446Sglebius		    m->m_pkthdr.len,
529142446Sglebius		    m->m_pkthdr.rcvif->if_xname);
530142215Sglebius		m_freem(m);
531142215Sglebius		return;
532142215Sglebius	}
533142215Sglebius
534142215Sglebius	if ((m = m_pullup(m, len)) == NULL) {
535142215Sglebius		carpstats.carps_hdrops++;
536142215Sglebius		return;
537142215Sglebius	}
538142215Sglebius	ip = mtod(m, struct ip *);
539142215Sglebius	ch = (struct carp_header *)((char *)ip + iplen);
540142215Sglebius
541142215Sglebius	/* verify the CARP checksum */
542142215Sglebius	m->m_data += iplen;
543142215Sglebius	if (carp_cksum(m, len - iplen)) {
544142215Sglebius		carpstats.carps_badsum++;
545142452Sglebius		CARP_LOG("carp_input: checksum failed on %s\n",
546142446Sglebius		    m->m_pkthdr.rcvif->if_xname);
547142215Sglebius		m_freem(m);
548142215Sglebius		return;
549142215Sglebius	}
550142215Sglebius	m->m_data -= iplen;
551142215Sglebius
552142446Sglebius	carp_input_c(m, ch, AF_INET);
553142215Sglebius}
554142215Sglebius
555142215Sglebius#ifdef INET6
556142215Sglebiusint
557142215Sglebiuscarp6_input(struct mbuf **mp, int *offp, int proto)
558142215Sglebius{
559142215Sglebius	struct mbuf *m = *mp;
560142215Sglebius	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
561142215Sglebius	struct carp_header *ch;
562142215Sglebius	u_int len;
563142215Sglebius
564142215Sglebius	carpstats.carps_ipackets6++;
565142215Sglebius
566142215Sglebius	if (!carp_opts[CARPCTL_ALLOW]) {
567142215Sglebius		m_freem(m);
568142215Sglebius		return (IPPROTO_DONE);
569142215Sglebius	}
570142215Sglebius
571142215Sglebius	/* check if received on a valid carp interface */
572142215Sglebius	if (m->m_pkthdr.rcvif->if_carp == NULL) {
573142215Sglebius		carpstats.carps_badif++;
574142446Sglebius		CARP_LOG("carp6_input: packet received on non-carp "
575142452Sglebius		    "interface: %s\n",
576142446Sglebius		    m->m_pkthdr.rcvif->if_xname);
577142215Sglebius		m_freem(m);
578142215Sglebius		return (IPPROTO_DONE);
579142215Sglebius	}
580142215Sglebius
581142215Sglebius	/* verify that the IP TTL is 255 */
582142215Sglebius	if (ip6->ip6_hlim != CARP_DFLTTL) {
583142215Sglebius		carpstats.carps_badttl++;
584142452Sglebius		CARP_LOG("carp6_input: received ttl %d != 255 on %s\n",
585142446Sglebius		    ip6->ip6_hlim,
586142446Sglebius		    m->m_pkthdr.rcvif->if_xname);
587142215Sglebius		m_freem(m);
588142215Sglebius		return (IPPROTO_DONE);
589142215Sglebius	}
590142215Sglebius
591142215Sglebius	/* verify that we have a complete carp packet */
592142215Sglebius	len = m->m_len;
593142215Sglebius	IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
594142215Sglebius	if (ch == NULL) {
595142215Sglebius		carpstats.carps_badlen++;
596143804Sglebius		CARP_LOG("carp6_input: packet size %u too small\n", len);
597142215Sglebius		return (IPPROTO_DONE);
598142215Sglebius	}
599142215Sglebius
600142215Sglebius
601142215Sglebius	/* verify the CARP checksum */
602142215Sglebius	m->m_data += *offp;
603142215Sglebius	if (carp_cksum(m, sizeof(*ch))) {
604142215Sglebius		carpstats.carps_badsum++;
605142452Sglebius		CARP_LOG("carp6_input: checksum failed, on %s\n",
606142446Sglebius		    m->m_pkthdr.rcvif->if_xname);
607142215Sglebius		m_freem(m);
608142215Sglebius		return (IPPROTO_DONE);
609142215Sglebius	}
610142215Sglebius	m->m_data -= *offp;
611142215Sglebius
612142446Sglebius	carp_input_c(m, ch, AF_INET6);
613142215Sglebius	return (IPPROTO_DONE);
614142215Sglebius}
615142215Sglebius#endif /* INET6 */
616142215Sglebius
617142559Sglebiusstatic void
618142446Sglebiuscarp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
619142215Sglebius{
620142215Sglebius	struct ifnet *ifp = m->m_pkthdr.rcvif;
621142446Sglebius	struct carp_softc *sc;
622142215Sglebius	u_int64_t tmp_counter;
623142215Sglebius	struct timeval sc_tv, ch_tv;
624142215Sglebius
625142215Sglebius	/* verify that the VHID is valid on the receiving interface */
626142215Sglebius	CARP_LOCK(ifp->if_carp);
627142215Sglebius	TAILQ_FOREACH(sc, &((struct carp_if *)ifp->if_carp)->vhif_vrs, sc_list)
628142215Sglebius		if (sc->sc_vhid == ch->carp_vhid)
629142215Sglebius			break;
630142914Sglebius
631147256Sbrooks	if (!sc || (SC2IFP(sc)->if_flags & (IFF_UP|IFF_RUNNING)) !=
632142215Sglebius	    (IFF_UP|IFF_RUNNING)) {
633142215Sglebius		carpstats.carps_badvhid++;
634142914Sglebius		CARP_UNLOCK(ifp->if_carp);
635142215Sglebius		m_freem(m);
636142215Sglebius		return;
637142215Sglebius	}
638142215Sglebius
639147256Sbrooks	getmicrotime(&SC2IFP(sc)->if_lastchange);
640147256Sbrooks	SC2IFP(sc)->if_ipackets++;
641147256Sbrooks	SC2IFP(sc)->if_ibytes += m->m_pkthdr.len;
642142215Sglebius
643147256Sbrooks	if (SC2IFP(sc)->if_bpf) {
644142215Sglebius		struct ip *ip = mtod(m, struct ip *);
645142784Sglebius		uint32_t af1 = af;
646142215Sglebius
647142215Sglebius		/* BPF wants net byte order */
648142784Sglebius		ip->ip_len = htons(ip->ip_len + (ip->ip_hl << 2));
649142784Sglebius		ip->ip_off = htons(ip->ip_off);
650147256Sbrooks		bpf_mtap2(SC2IFP(sc)->if_bpf, &af1, sizeof(af1), m);
651142215Sglebius	}
652142215Sglebius
653142215Sglebius	/* verify the CARP version. */
654142215Sglebius	if (ch->carp_version != CARP_VERSION) {
655142215Sglebius		carpstats.carps_badver++;
656147256Sbrooks		SC2IFP(sc)->if_ierrors++;
657142914Sglebius		CARP_UNLOCK(ifp->if_carp);
658142452Sglebius		CARP_LOG("%s; invalid version %d\n",
659147256Sbrooks		    SC2IFP(sc)->if_xname,
660142446Sglebius		    ch->carp_version);
661142215Sglebius		m_freem(m);
662142215Sglebius		return;
663142215Sglebius	}
664142215Sglebius
665142215Sglebius	/* verify the hash */
666142215Sglebius	if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
667142215Sglebius		carpstats.carps_badauth++;
668147256Sbrooks		SC2IFP(sc)->if_ierrors++;
669142914Sglebius		CARP_UNLOCK(ifp->if_carp);
670147256Sbrooks		CARP_LOG("%s: incorrect hash\n", SC2IFP(sc)->if_xname);
671142215Sglebius		m_freem(m);
672142215Sglebius		return;
673142215Sglebius	}
674142215Sglebius
675142215Sglebius	tmp_counter = ntohl(ch->carp_counter[0]);
676142215Sglebius	tmp_counter = tmp_counter<<32;
677142215Sglebius	tmp_counter += ntohl(ch->carp_counter[1]);
678142215Sglebius
679142215Sglebius	/* XXX Replay protection goes here */
680142215Sglebius
681142215Sglebius	sc->sc_init_counter = 0;
682142215Sglebius	sc->sc_counter = tmp_counter;
683142215Sglebius
684142215Sglebius	sc_tv.tv_sec = sc->sc_advbase;
685142215Sglebius	if (carp_suppress_preempt && sc->sc_advskew <  240)
686142215Sglebius		sc_tv.tv_usec = 240 * 1000000 / 256;
687142215Sglebius	else
688142215Sglebius		sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256;
689142215Sglebius	ch_tv.tv_sec = ch->carp_advbase;
690142215Sglebius	ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
691142215Sglebius
692142215Sglebius	switch (sc->sc_state) {
693142215Sglebius	case INIT:
694142215Sglebius		break;
695142215Sglebius	case MASTER:
696142215Sglebius		/*
697142215Sglebius		 * If we receive an advertisement from a master who's going to
698142215Sglebius		 * be more frequent than us, go into BACKUP state.
699142215Sglebius		 */
700142215Sglebius		if (timevalcmp(&sc_tv, &ch_tv, >) ||
701142215Sglebius		    timevalcmp(&sc_tv, &ch_tv, ==)) {
702142215Sglebius			callout_stop(&sc->sc_ad_tmo);
703142446Sglebius			CARP_DEBUG("%s: MASTER -> BACKUP "
704142452Sglebius			   "(more frequent advertisement received)\n",
705147256Sbrooks			   SC2IFP(sc)->if_xname);
706142215Sglebius			carp_set_state(sc, BACKUP);
707142215Sglebius			carp_setrun(sc, 0);
708142215Sglebius			carp_setroute(sc, RTM_DELETE);
709142215Sglebius		}
710142215Sglebius		break;
711142215Sglebius	case BACKUP:
712142215Sglebius		/*
713142215Sglebius		 * If we're pre-empting masters who advertise slower than us,
714142215Sglebius		 * and this one claims to be slower, treat him as down.
715142215Sglebius		 */
716142215Sglebius		if (carp_opts[CARPCTL_PREEMPT] &&
717142215Sglebius		    timevalcmp(&sc_tv, &ch_tv, <)) {
718142446Sglebius			CARP_DEBUG("%s: BACKUP -> MASTER "
719142452Sglebius			    "(preempting a slower master)\n",
720147256Sbrooks			    SC2IFP(sc)->if_xname);
721142914Sglebius			carp_master_down_locked(sc);
722142215Sglebius			break;
723142215Sglebius		}
724142215Sglebius
725142215Sglebius		/*
726142215Sglebius		 *  If the master is going to advertise at such a low frequency
727142215Sglebius		 *  that he's guaranteed to time out, we'd might as well just
728142215Sglebius		 *  treat him as timed out now.
729142215Sglebius		 */
730142215Sglebius		sc_tv.tv_sec = sc->sc_advbase * 3;
731142215Sglebius		if (timevalcmp(&sc_tv, &ch_tv, <)) {
732142446Sglebius			CARP_DEBUG("%s: BACKUP -> MASTER "
733142452Sglebius			    "(master timed out)\n",
734147256Sbrooks			    SC2IFP(sc)->if_xname);
735142914Sglebius			carp_master_down_locked(sc);
736142215Sglebius			break;
737142215Sglebius		}
738142215Sglebius
739142215Sglebius		/*
740142215Sglebius		 * Otherwise, we reset the counter and wait for the next
741142215Sglebius		 * advertisement.
742142215Sglebius		 */
743142215Sglebius		carp_setrun(sc, af);
744142215Sglebius		break;
745142215Sglebius	}
746142215Sglebius
747142914Sglebius	CARP_UNLOCK(ifp->if_carp);
748142914Sglebius
749142215Sglebius	m_freem(m);
750142215Sglebius	return;
751142215Sglebius}
752142215Sglebius
753142559Sglebiusstatic void
754142215Sglebiuscarpdetach(struct carp_softc *sc)
755142215Sglebius{
756142215Sglebius	struct ifaddr *ifa;
757142215Sglebius
758142215Sglebius	callout_stop(&sc->sc_ad_tmo);
759142215Sglebius	callout_stop(&sc->sc_md_tmo);
760142215Sglebius	callout_stop(&sc->sc_md6_tmo);
761142215Sglebius
762147256Sbrooks	while ((ifa = TAILQ_FIRST(&SC2IFP(sc)->if_addrlist)) != NULL)
763142215Sglebius		if (ifa->ifa_addr->sa_family == AF_INET) {
764142215Sglebius			struct in_ifaddr *ia = ifatoia(ifa);
765142215Sglebius
766142215Sglebius			carp_del_addr(sc, &ia->ia_addr);
767142215Sglebius
768142215Sglebius			/* ripped screaming from in_control(SIOCDIFADDR) */
769147256Sbrooks			in_ifscrub(SC2IFP(sc), ia);
770147256Sbrooks			TAILQ_REMOVE(&SC2IFP(sc)->if_addrlist, ifa, ifa_link);
771142215Sglebius			TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
772142215Sglebius			IFAFREE((&ia->ia_ifa));
773142215Sglebius		}
774142215Sglebius}
775142215Sglebius
776142215Sglebius/* Detach an interface from the carp.  */
777142215Sglebiusvoid
778142215Sglebiuscarp_ifdetach(struct ifnet *ifp)
779142215Sglebius{
780142215Sglebius	struct carp_softc *sc;
781142215Sglebius	struct carp_if *cif = (struct carp_if *)ifp->if_carp;
782142215Sglebius
783142215Sglebius	CARP_LOCK(cif);
784142215Sglebius	TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list)
785142215Sglebius		carpdetach(sc);
786142215Sglebius	CARP_UNLOCK(cif);
787142215Sglebius}
788142215Sglebius
789142559Sglebiusstatic int
790142215Sglebiuscarp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
791142215Sglebius{
792142215Sglebius	struct m_tag *mtag;
793147256Sbrooks	struct ifnet *ifp = SC2IFP(sc);
794142215Sglebius
795142215Sglebius	if (sc->sc_init_counter) {
796142215Sglebius		/* this could also be seconds since unix epoch */
797142215Sglebius		sc->sc_counter = arc4random();
798142215Sglebius		sc->sc_counter = sc->sc_counter << 32;
799142215Sglebius		sc->sc_counter += arc4random();
800142215Sglebius	} else
801142215Sglebius		sc->sc_counter++;
802142215Sglebius
803142215Sglebius	ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
804142215Sglebius	ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
805142215Sglebius
806142215Sglebius	carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
807142215Sglebius
808142215Sglebius	/* Tag packet for carp_output */
809142215Sglebius	mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct ifnet *), M_NOWAIT);
810142215Sglebius	if (mtag == NULL) {
811142215Sglebius		m_freem(m);
812147256Sbrooks		SC2IFP(sc)->if_oerrors++;
813142215Sglebius		return (ENOMEM);
814142215Sglebius	}
815142215Sglebius	bcopy(&ifp, (caddr_t)(mtag + 1), sizeof(struct ifnet *));
816142215Sglebius	m_tag_prepend(m, mtag);
817142215Sglebius
818142215Sglebius	return (0);
819142215Sglebius}
820142215Sglebius
821142559Sglebiusstatic void
822142215Sglebiuscarp_send_ad_all(void)
823142215Sglebius{
824142911Sglebius	struct carp_softc *sc;
825142215Sglebius
826142911Sglebius	mtx_lock(&carp_mtx);
827142911Sglebius	LIST_FOREACH(sc, &carpif_list, sc_next) {
828142911Sglebius		if (sc->sc_carpdev == NULL)
829142215Sglebius			continue;
830142911Sglebius		CARP_SCLOCK(sc);
831147256Sbrooks		if ((SC2IFP(sc)->if_flags & (IFF_UP|IFF_RUNNING)) &&
832142911Sglebius		     sc->sc_state == MASTER)
833142914Sglebius			carp_send_ad_locked(sc);
834142911Sglebius		CARP_SCUNLOCK(sc);
835142215Sglebius	}
836142911Sglebius	mtx_unlock(&carp_mtx);
837142215Sglebius}
838142215Sglebius
839142559Sglebiusstatic void
840142215Sglebiuscarp_send_ad(void *v)
841142215Sglebius{
842142914Sglebius	struct carp_softc *sc = v;
843142914Sglebius
844142914Sglebius	CARP_SCLOCK(sc);
845142914Sglebius	carp_send_ad_locked(sc);
846142914Sglebius	CARP_SCUNLOCK(sc);
847142914Sglebius}
848142914Sglebius
849142914Sglebiusstatic void
850142914Sglebiuscarp_send_ad_locked(struct carp_softc *sc)
851142914Sglebius{
852142215Sglebius	struct carp_header ch;
853142215Sglebius	struct timeval tv;
854142215Sglebius	struct carp_header *ch_ptr;
855142215Sglebius	struct mbuf *m;
856142215Sglebius	int len, advbase, advskew;
857142215Sglebius
858142914Sglebius	CARP_SCLOCK_ASSERT(sc);
859142914Sglebius
860142215Sglebius	/* bow out if we've lost our UPness or RUNNINGuiness */
861147256Sbrooks	if ((SC2IFP(sc)->if_flags &
862142215Sglebius	    (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
863142215Sglebius		advbase = 255;
864142215Sglebius		advskew = 255;
865142215Sglebius	} else {
866142215Sglebius		advbase = sc->sc_advbase;
867142215Sglebius		if (!carp_suppress_preempt || sc->sc_advskew > 240)
868142215Sglebius			advskew = sc->sc_advskew;
869142215Sglebius		else
870142215Sglebius			advskew = 240;
871142215Sglebius		tv.tv_sec = advbase;
872142215Sglebius		tv.tv_usec = advskew * 1000000 / 256;
873142215Sglebius	}
874142215Sglebius
875142215Sglebius	ch.carp_version = CARP_VERSION;
876142215Sglebius	ch.carp_type = CARP_ADVERTISEMENT;
877142215Sglebius	ch.carp_vhid = sc->sc_vhid;
878142215Sglebius	ch.carp_advbase = advbase;
879142215Sglebius	ch.carp_advskew = advskew;
880142215Sglebius	ch.carp_authlen = 7;	/* XXX DEFINE */
881142215Sglebius	ch.carp_pad1 = 0;	/* must be zero */
882142215Sglebius	ch.carp_cksum = 0;
883142215Sglebius
884142215Sglebius#ifdef INET
885142215Sglebius	if (sc->sc_ia) {
886142215Sglebius		struct ip *ip;
887142215Sglebius
888142215Sglebius		MGETHDR(m, M_DONTWAIT, MT_HEADER);
889142215Sglebius		if (m == NULL) {
890147256Sbrooks			SC2IFP(sc)->if_oerrors++;
891142215Sglebius			carpstats.carps_onomem++;
892142215Sglebius			/* XXX maybe less ? */
893142215Sglebius			if (advbase != 255 || advskew != 255)
894142215Sglebius				callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
895142215Sglebius				    carp_send_ad, sc);
896142215Sglebius			return;
897142215Sglebius		}
898142215Sglebius		len = sizeof(*ip) + sizeof(ch);
899142215Sglebius		m->m_pkthdr.len = len;
900142215Sglebius		m->m_pkthdr.rcvif = NULL;
901142215Sglebius		m->m_len = len;
902142215Sglebius		MH_ALIGN(m, m->m_len);
903142215Sglebius		m->m_flags |= M_MCAST;
904142215Sglebius		ip = mtod(m, struct ip *);
905142215Sglebius		ip->ip_v = IPVERSION;
906142215Sglebius		ip->ip_hl = sizeof(*ip) >> 2;
907142215Sglebius		ip->ip_tos = IPTOS_LOWDELAY;
908142215Sglebius		ip->ip_len = len;
909142215Sglebius		ip->ip_id = ip_newid();
910142215Sglebius		ip->ip_off = IP_DF;
911142215Sglebius		ip->ip_ttl = CARP_DFLTTL;
912142215Sglebius		ip->ip_p = IPPROTO_CARP;
913142215Sglebius		ip->ip_sum = 0;
914142215Sglebius		ip->ip_src.s_addr = sc->sc_ia->ia_addr.sin_addr.s_addr;
915142215Sglebius		ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
916142215Sglebius
917142215Sglebius		ch_ptr = (struct carp_header *)(&ip[1]);
918142215Sglebius		bcopy(&ch, ch_ptr, sizeof(ch));
919142215Sglebius		if (carp_prepare_ad(m, sc, ch_ptr))
920142215Sglebius			return;
921142215Sglebius
922142215Sglebius		m->m_data += sizeof(*ip);
923142215Sglebius		ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip));
924142215Sglebius		m->m_data -= sizeof(*ip);
925142215Sglebius
926147256Sbrooks		getmicrotime(&SC2IFP(sc)->if_lastchange);
927147256Sbrooks		SC2IFP(sc)->if_opackets++;
928147256Sbrooks		SC2IFP(sc)->if_obytes += len;
929142215Sglebius		carpstats.carps_opackets++;
930142215Sglebius
931142215Sglebius		if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) {
932147256Sbrooks			SC2IFP(sc)->if_oerrors++;
933142215Sglebius			if (sc->sc_sendad_errors < INT_MAX)
934142215Sglebius				sc->sc_sendad_errors++;
935142215Sglebius			if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
936142215Sglebius				carp_suppress_preempt++;
937142914Sglebius				if (carp_suppress_preempt == 1) {
938142914Sglebius					CARP_SCUNLOCK(sc);
939142215Sglebius					carp_send_ad_all();
940142914Sglebius					CARP_SCLOCK(sc);
941142914Sglebius				}
942142215Sglebius			}
943142215Sglebius			sc->sc_sendad_success = 0;
944142215Sglebius		} else {
945142215Sglebius			if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
946142215Sglebius				if (++sc->sc_sendad_success >=
947142215Sglebius				    CARP_SENDAD_MIN_SUCCESS) {
948142215Sglebius					carp_suppress_preempt--;
949142215Sglebius					sc->sc_sendad_errors = 0;
950142215Sglebius				}
951142215Sglebius			} else
952142215Sglebius				sc->sc_sendad_errors = 0;
953142215Sglebius		}
954142215Sglebius	}
955142215Sglebius#endif /* INET */
956142215Sglebius#ifdef INET6
957142215Sglebius	if (sc->sc_ia6) {
958142215Sglebius		struct ip6_hdr *ip6;
959142215Sglebius
960142215Sglebius		MGETHDR(m, M_DONTWAIT, MT_HEADER);
961142215Sglebius		if (m == NULL) {
962147256Sbrooks			SC2IFP(sc)->if_oerrors++;
963142215Sglebius			carpstats.carps_onomem++;
964142215Sglebius			/* XXX maybe less ? */
965142215Sglebius			if (advbase != 255 || advskew != 255)
966142215Sglebius				callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
967142215Sglebius				    carp_send_ad, sc);
968142215Sglebius			return;
969142215Sglebius		}
970142215Sglebius		len = sizeof(*ip6) + sizeof(ch);
971142215Sglebius		m->m_pkthdr.len = len;
972142215Sglebius		m->m_pkthdr.rcvif = NULL;
973142215Sglebius		m->m_len = len;
974142215Sglebius		MH_ALIGN(m, m->m_len);
975142215Sglebius		m->m_flags |= M_MCAST;
976142215Sglebius		ip6 = mtod(m, struct ip6_hdr *);
977142215Sglebius		bzero(ip6, sizeof(*ip6));
978142215Sglebius		ip6->ip6_vfc |= IPV6_VERSION;
979142215Sglebius		ip6->ip6_hlim = CARP_DFLTTL;
980142215Sglebius		ip6->ip6_nxt = IPPROTO_CARP;
981142215Sglebius		bcopy(&sc->sc_ia6->ia_addr.sin6_addr, &ip6->ip6_src,
982142215Sglebius		    sizeof(struct in6_addr));
983142215Sglebius		/* set the multicast destination */
984142215Sglebius
985142215Sglebius		ip6->ip6_dst.s6_addr8[0] = 0xff;
986142215Sglebius		ip6->ip6_dst.s6_addr8[1] = 0x02;
987142215Sglebius		ip6->ip6_dst.s6_addr8[15] = 0x12;
988142215Sglebius
989142215Sglebius		ch_ptr = (struct carp_header *)(&ip6[1]);
990142215Sglebius		bcopy(&ch, ch_ptr, sizeof(ch));
991142215Sglebius		if (carp_prepare_ad(m, sc, ch_ptr))
992142215Sglebius			return;
993142215Sglebius
994142215Sglebius		m->m_data += sizeof(*ip6);
995142215Sglebius		ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip6));
996142215Sglebius		m->m_data -= sizeof(*ip6);
997142215Sglebius
998147256Sbrooks		getmicrotime(&SC2IFP(sc)->if_lastchange);
999147256Sbrooks		SC2IFP(sc)->if_opackets++;
1000147256Sbrooks		SC2IFP(sc)->if_obytes += len;
1001142215Sglebius		carpstats.carps_opackets6++;
1002142215Sglebius
1003142215Sglebius		if (ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL)) {
1004147256Sbrooks			SC2IFP(sc)->if_oerrors++;
1005142215Sglebius			if (sc->sc_sendad_errors < INT_MAX)
1006142215Sglebius				sc->sc_sendad_errors++;
1007142215Sglebius			if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
1008142215Sglebius				carp_suppress_preempt++;
1009142914Sglebius				if (carp_suppress_preempt == 1) {
1010142914Sglebius					CARP_SCUNLOCK(sc);
1011142215Sglebius					carp_send_ad_all();
1012142914Sglebius					CARP_SCLOCK(sc);
1013142914Sglebius				}
1014142215Sglebius			}
1015142215Sglebius			sc->sc_sendad_success = 0;
1016142215Sglebius		} else {
1017142215Sglebius			if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
1018142215Sglebius				if (++sc->sc_sendad_success >=
1019142215Sglebius				    CARP_SENDAD_MIN_SUCCESS) {
1020142215Sglebius					carp_suppress_preempt--;
1021142215Sglebius					sc->sc_sendad_errors = 0;
1022142215Sglebius				}
1023142215Sglebius			} else
1024142215Sglebius				sc->sc_sendad_errors = 0;
1025142215Sglebius		}
1026142215Sglebius	}
1027142215Sglebius#endif /* INET6 */
1028142215Sglebius
1029142215Sglebius	if (advbase != 255 || advskew != 255)
1030142215Sglebius		callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1031142215Sglebius		    carp_send_ad, sc);
1032142215Sglebius
1033142215Sglebius}
1034142215Sglebius
1035142215Sglebius/*
1036142215Sglebius * Broadcast a gratuitous ARP request containing
1037142215Sglebius * the virtual router MAC address for each IP address
1038142215Sglebius * associated with the virtual router.
1039142215Sglebius */
1040142559Sglebiusstatic void
1041142215Sglebiuscarp_send_arp(struct carp_softc *sc)
1042142215Sglebius{
1043142215Sglebius	struct ifaddr *ifa;
1044142215Sglebius
1045147256Sbrooks	TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1046142215Sglebius
1047142215Sglebius		if (ifa->ifa_addr->sa_family != AF_INET)
1048142215Sglebius			continue;
1049142215Sglebius
1050147256Sbrooks/*		arprequest(sc->sc_carpdev, &in, &in, IFP2ENADDR(sc->sc_ifp)); */
1051147256Sbrooks		arp_ifinit2(sc->sc_carpdev, ifa, IFP2ENADDR(sc->sc_ifp));
1052142215Sglebius
1053142215Sglebius		DELAY(1000);	/* XXX */
1054142215Sglebius	}
1055142215Sglebius}
1056142215Sglebius
1057142215Sglebius#ifdef INET6
1058142559Sglebiusstatic void
1059142215Sglebiuscarp_send_na(struct carp_softc *sc)
1060142215Sglebius{
1061142215Sglebius	struct ifaddr *ifa;
1062142215Sglebius	struct in6_addr *in6;
1063142215Sglebius	static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1064142215Sglebius
1065147256Sbrooks	TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1066142215Sglebius
1067142215Sglebius		if (ifa->ifa_addr->sa_family != AF_INET6)
1068142215Sglebius			continue;
1069142215Sglebius
1070142215Sglebius		in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
1071142564Sglebius		nd6_na_output(sc->sc_carpdev, &mcast, in6,
1072142215Sglebius		    ND_NA_FLAG_OVERRIDE, 1, NULL);
1073142215Sglebius		DELAY(1000);	/* XXX */
1074142215Sglebius	}
1075142215Sglebius}
1076142215Sglebius#endif /* INET6 */
1077142215Sglebius
1078142559Sglebiusstatic int
1079142215Sglebiuscarp_addrcount(struct carp_if *cif, struct in_ifaddr *ia, int type)
1080142215Sglebius{
1081142215Sglebius	struct carp_softc *vh;
1082142215Sglebius	struct ifaddr *ifa;
1083142215Sglebius	int count = 0;
1084142215Sglebius
1085142914Sglebius	CARP_LOCK_ASSERT(cif);
1086142914Sglebius
1087142215Sglebius	TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1088142215Sglebius		if ((type == CARP_COUNT_RUNNING &&
1089147256Sbrooks		    (SC2IFP(vh)->if_flags & (IFF_UP|IFF_RUNNING)) ==
1090142215Sglebius		    (IFF_UP|IFF_RUNNING)) ||
1091142215Sglebius		    (type == CARP_COUNT_MASTER && vh->sc_state == MASTER)) {
1092147256Sbrooks			TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist,
1093142215Sglebius			    ifa_list) {
1094142215Sglebius				if (ifa->ifa_addr->sa_family == AF_INET &&
1095142215Sglebius				    ia->ia_addr.sin_addr.s_addr ==
1096142215Sglebius				    ifatoia(ifa)->ia_addr.sin_addr.s_addr)
1097142215Sglebius					count++;
1098142215Sglebius			}
1099142215Sglebius		}
1100142215Sglebius	}
1101142215Sglebius	return (count);
1102142215Sglebius}
1103142215Sglebius
1104142215Sglebiusint
1105142215Sglebiuscarp_iamatch(void *v, struct in_ifaddr *ia,
1106142215Sglebius    struct in_addr *isaddr, u_int8_t **enaddr)
1107142215Sglebius{
1108142215Sglebius	struct carp_if *cif = v;
1109142215Sglebius	struct carp_softc *vh;
1110142215Sglebius	int index, count = 0;
1111142215Sglebius	struct ifaddr *ifa;
1112142215Sglebius
1113142215Sglebius	CARP_LOCK(cif);
1114142215Sglebius
1115142215Sglebius	if (carp_opts[CARPCTL_ARPBALANCE]) {
1116142215Sglebius		/*
1117142215Sglebius		 * XXX proof of concept implementation.
1118142215Sglebius		 * We use the source ip to decide which virtual host should
1119142215Sglebius		 * handle the request. If we're master of that virtual host,
1120142215Sglebius		 * then we respond, otherwise, just drop the arp packet on
1121142215Sglebius		 * the floor.
1122142215Sglebius		 */
1123142215Sglebius		count = carp_addrcount(cif, ia, CARP_COUNT_RUNNING);
1124142215Sglebius		if (count == 0) {
1125142215Sglebius			/* should never reach this */
1126142215Sglebius			CARP_UNLOCK(cif);
1127142215Sglebius			return (0);
1128142215Sglebius		}
1129142215Sglebius
1130142215Sglebius		/* this should be a hash, like pf_hash() */
1131147718Sglebius		index = ntohl(isaddr->s_addr) % count;
1132142215Sglebius		count = 0;
1133142215Sglebius
1134142215Sglebius		TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1135147256Sbrooks			if ((SC2IFP(vh)->if_flags & (IFF_UP|IFF_RUNNING)) ==
1136142215Sglebius			    (IFF_UP|IFF_RUNNING)) {
1137147256Sbrooks				TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist,
1138142215Sglebius				    ifa_list) {
1139142215Sglebius					if (ifa->ifa_addr->sa_family ==
1140142215Sglebius					    AF_INET &&
1141142215Sglebius					    ia->ia_addr.sin_addr.s_addr ==
1142142215Sglebius					    ifatoia(ifa)->ia_addr.sin_addr.s_addr) {
1143142215Sglebius						if (count == index) {
1144142215Sglebius							if (vh->sc_state ==
1145142215Sglebius							    MASTER) {
1146147256Sbrooks								*enaddr = IFP2ENADDR(vh->sc_ifp);
1147142215Sglebius								CARP_UNLOCK(cif);
1148142215Sglebius								return (1);
1149142215Sglebius							} else {
1150142215Sglebius								CARP_UNLOCK(cif);
1151142215Sglebius								return (0);
1152142215Sglebius							}
1153142215Sglebius						}
1154142215Sglebius						count++;
1155142215Sglebius					}
1156142215Sglebius				}
1157142215Sglebius			}
1158142215Sglebius		}
1159142215Sglebius	} else {
1160142215Sglebius		TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1161147256Sbrooks			if ((SC2IFP(vh)->if_flags & (IFF_UP|IFF_RUNNING)) ==
1162142215Sglebius			    (IFF_UP|IFF_RUNNING) && ia->ia_ifp ==
1163147256Sbrooks			    SC2IFP(vh)) {
1164147256Sbrooks				*enaddr = IFP2ENADDR(vh->sc_ifp);
1165142215Sglebius				CARP_UNLOCK(cif);
1166142215Sglebius				return (1);
1167142215Sglebius			}
1168142215Sglebius		}
1169142215Sglebius	}
1170142215Sglebius	CARP_UNLOCK(cif);
1171142215Sglebius	return (0);
1172142215Sglebius}
1173142215Sglebius
1174142215Sglebius#ifdef INET6
1175142641Smlaierstruct ifaddr *
1176142215Sglebiuscarp_iamatch6(void *v, struct in6_addr *taddr)
1177142215Sglebius{
1178142215Sglebius	struct carp_if *cif = v;
1179142215Sglebius	struct carp_softc *vh;
1180142215Sglebius	struct ifaddr *ifa;
1181142215Sglebius
1182142215Sglebius	CARP_LOCK(cif);
1183142215Sglebius	TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1184147256Sbrooks		TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist, ifa_list) {
1185142215Sglebius			if (IN6_ARE_ADDR_EQUAL(taddr,
1186142215Sglebius			    &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1187147256Sbrooks 			    ((SC2IFP(vh)->if_flags &
1188142215Sglebius			    (IFF_UP|IFF_RUNNING)) == (IFF_UP|IFF_RUNNING))) {
1189142215Sglebius			    	CARP_UNLOCK(cif);
1190142215Sglebius				return (ifa);
1191142215Sglebius			}
1192142215Sglebius		}
1193142215Sglebius	}
1194142215Sglebius	CARP_UNLOCK(cif);
1195142215Sglebius
1196142215Sglebius	return (NULL);
1197142215Sglebius}
1198142215Sglebius
1199142641Smlaiervoid *
1200142215Sglebiuscarp_macmatch6(void *v, struct mbuf *m, const struct in6_addr *taddr)
1201142215Sglebius{
1202142215Sglebius	struct m_tag *mtag;
1203142215Sglebius	struct carp_if *cif = v;
1204142215Sglebius	struct carp_softc *sc;
1205142215Sglebius	struct ifaddr *ifa;
1206142215Sglebius
1207142215Sglebius	CARP_LOCK(cif);
1208142215Sglebius	TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
1209147256Sbrooks		TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1210142215Sglebius			if (IN6_ARE_ADDR_EQUAL(taddr,
1211142215Sglebius			    &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1212147256Sbrooks 			    ((SC2IFP(sc)->if_flags &
1213142215Sglebius			    (IFF_UP|IFF_RUNNING)) == (IFF_UP|IFF_RUNNING))) {
1214147256Sbrooks				struct ifnet *ifp = SC2IFP(sc);
1215142215Sglebius				mtag = m_tag_get(PACKET_TAG_CARP,
1216142215Sglebius				    sizeof(struct ifnet *), M_NOWAIT);
1217142215Sglebius				if (mtag == NULL) {
1218142215Sglebius					/* better a bit than nothing */
1219142215Sglebius					CARP_UNLOCK(cif);
1220147256Sbrooks					return (IFP2ENADDR(sc->sc_ifp));
1221142215Sglebius				}
1222142215Sglebius				bcopy(&ifp, (caddr_t)(mtag + 1),
1223142215Sglebius				    sizeof(struct ifnet *));
1224142215Sglebius				m_tag_prepend(m, mtag);
1225142215Sglebius
1226142215Sglebius				CARP_UNLOCK(cif);
1227147256Sbrooks				return (IFP2ENADDR(sc->sc_ifp));
1228142215Sglebius			}
1229142215Sglebius		}
1230142215Sglebius	}
1231142215Sglebius	CARP_UNLOCK(cif);
1232142215Sglebius
1233142215Sglebius	return (NULL);
1234142215Sglebius}
1235142215Sglebius#endif
1236142215Sglebius
1237142215Sglebiusstruct ifnet *
1238142215Sglebiuscarp_forus(void *v, void *dhost)
1239142215Sglebius{
1240142215Sglebius	struct carp_if *cif = v;
1241142215Sglebius	struct carp_softc *vh;
1242142215Sglebius	u_int8_t *ena = dhost;
1243142215Sglebius
1244142215Sglebius	if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1245142215Sglebius		return (NULL);
1246142215Sglebius
1247142215Sglebius	CARP_LOCK(cif);
1248142215Sglebius	TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list)
1249147256Sbrooks		if ((SC2IFP(vh)->if_flags & (IFF_UP|IFF_RUNNING)) ==
1250142215Sglebius		    (IFF_UP|IFF_RUNNING) && vh->sc_state == MASTER &&
1251147256Sbrooks		    !bcmp(dhost, IFP2ENADDR(vh->sc_ifp), ETHER_ADDR_LEN)) {
1252142215Sglebius		    	CARP_UNLOCK(cif);
1253147256Sbrooks			return (SC2IFP(vh));
1254142215Sglebius		}
1255142215Sglebius
1256142215Sglebius    	CARP_UNLOCK(cif);
1257142215Sglebius	return (NULL);
1258142215Sglebius}
1259142215Sglebius
1260142559Sglebiusstatic void
1261142215Sglebiuscarp_master_down(void *v)
1262142215Sglebius{
1263142215Sglebius	struct carp_softc *sc = v;
1264142215Sglebius
1265142914Sglebius	CARP_SCLOCK(sc);
1266142914Sglebius	carp_master_down_locked(sc);
1267142914Sglebius	CARP_SCUNLOCK(sc);
1268142914Sglebius}
1269142914Sglebius
1270142914Sglebiusstatic void
1271142914Sglebiuscarp_master_down_locked(struct carp_softc *sc)
1272142914Sglebius{
1273142914Sglebius	if (sc->sc_carpdev)
1274142914Sglebius		CARP_SCLOCK_ASSERT(sc);
1275142914Sglebius
1276142215Sglebius	switch (sc->sc_state) {
1277142215Sglebius	case INIT:
1278142215Sglebius		printf("%s: master_down event in INIT state\n",
1279147256Sbrooks		    SC2IFP(sc)->if_xname);
1280142215Sglebius		break;
1281142215Sglebius	case MASTER:
1282142215Sglebius		break;
1283142215Sglebius	case BACKUP:
1284142215Sglebius		carp_set_state(sc, MASTER);
1285142914Sglebius		carp_send_ad_locked(sc);
1286142215Sglebius		carp_send_arp(sc);
1287142215Sglebius#ifdef INET6
1288142215Sglebius		carp_send_na(sc);
1289142215Sglebius#endif /* INET6 */
1290142215Sglebius		carp_setrun(sc, 0);
1291142215Sglebius		carp_setroute(sc, RTM_ADD);
1292142215Sglebius		break;
1293142215Sglebius	}
1294142215Sglebius}
1295142215Sglebius
1296142215Sglebius/*
1297142215Sglebius * When in backup state, af indicates whether to reset the master down timer
1298142215Sglebius * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1299142215Sglebius */
1300142559Sglebiusstatic void
1301142215Sglebiuscarp_setrun(struct carp_softc *sc, sa_family_t af)
1302142215Sglebius{
1303142215Sglebius	struct timeval tv;
1304142215Sglebius
1305142914Sglebius	if (sc->sc_carpdev)
1306142914Sglebius		CARP_SCLOCK_ASSERT(sc);
1307142914Sglebius
1308147256Sbrooks	if (SC2IFP(sc)->if_flags & IFF_UP &&
1309142215Sglebius	    sc->sc_vhid > 0 && (sc->sc_naddrs || sc->sc_naddrs6))
1310147256Sbrooks		SC2IFP(sc)->if_flags |= IFF_RUNNING;
1311142215Sglebius	else {
1312147256Sbrooks		SC2IFP(sc)->if_flags &= ~IFF_RUNNING;
1313142215Sglebius		carp_setroute(sc, RTM_DELETE);
1314142215Sglebius		return;
1315142215Sglebius	}
1316142215Sglebius
1317142215Sglebius	switch (sc->sc_state) {
1318142215Sglebius	case INIT:
1319142215Sglebius		if (carp_opts[CARPCTL_PREEMPT] && !carp_suppress_preempt) {
1320142914Sglebius			carp_send_ad_locked(sc);
1321142215Sglebius			carp_send_arp(sc);
1322142215Sglebius#ifdef INET6
1323142215Sglebius			carp_send_na(sc);
1324142215Sglebius#endif /* INET6 */
1325142452Sglebius			CARP_DEBUG("%s: INIT -> MASTER (preempting)\n",
1326147256Sbrooks			    SC2IFP(sc)->if_xname);
1327142215Sglebius			carp_set_state(sc, MASTER);
1328142215Sglebius			carp_setroute(sc, RTM_ADD);
1329142215Sglebius		} else {
1330147256Sbrooks			CARP_DEBUG("%s: INIT -> BACKUP\n", SC2IFP(sc)->if_xname);
1331142215Sglebius			carp_set_state(sc, BACKUP);
1332142215Sglebius			carp_setroute(sc, RTM_DELETE);
1333142215Sglebius			carp_setrun(sc, 0);
1334142215Sglebius		}
1335142215Sglebius		break;
1336142215Sglebius	case BACKUP:
1337142215Sglebius		callout_stop(&sc->sc_ad_tmo);
1338142215Sglebius		tv.tv_sec = 3 * sc->sc_advbase;
1339142215Sglebius		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1340142215Sglebius		switch (af) {
1341142215Sglebius#ifdef INET
1342142215Sglebius		case AF_INET:
1343142215Sglebius			callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1344142215Sglebius			    carp_master_down, sc);
1345142215Sglebius			break;
1346142215Sglebius#endif /* INET */
1347142215Sglebius#ifdef INET6
1348142215Sglebius		case AF_INET6:
1349142215Sglebius			callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1350142215Sglebius			    carp_master_down, sc);
1351142215Sglebius			break;
1352142215Sglebius#endif /* INET6 */
1353142215Sglebius		default:
1354142215Sglebius			if (sc->sc_naddrs)
1355142215Sglebius				callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1356142215Sglebius				    carp_master_down, sc);
1357142215Sglebius			if (sc->sc_naddrs6)
1358142215Sglebius				callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1359142215Sglebius				    carp_master_down, sc);
1360142215Sglebius			break;
1361142215Sglebius		}
1362142215Sglebius		break;
1363142215Sglebius	case MASTER:
1364142215Sglebius		tv.tv_sec = sc->sc_advbase;
1365142215Sglebius		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1366142215Sglebius		callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1367142215Sglebius		    carp_send_ad, sc);
1368142215Sglebius		break;
1369142215Sglebius	}
1370142215Sglebius}
1371142215Sglebius
1372142559Sglebiusstatic int
1373142215Sglebiuscarp_set_addr(struct carp_softc *sc, struct sockaddr_in *sin)
1374142215Sglebius{
1375142215Sglebius	struct ifnet *ifp;
1376142215Sglebius	struct carp_if *cif;
1377142215Sglebius	struct in_ifaddr *ia, *ia_if;
1378142215Sglebius	struct ip_moptions *imo = &sc->sc_imo;
1379142215Sglebius	struct in_addr addr;
1380142215Sglebius	u_long iaddr = htonl(sin->sin_addr.s_addr);
1381142215Sglebius	int own, error;
1382142215Sglebius
1383142215Sglebius	if (sin->sin_addr.s_addr == 0) {
1384147256Sbrooks		if (!(SC2IFP(sc)->if_flags & IFF_UP))
1385142215Sglebius			carp_set_state(sc, INIT);
1386142215Sglebius		if (sc->sc_naddrs)
1387147256Sbrooks			SC2IFP(sc)->if_flags |= IFF_UP;
1388142215Sglebius		carp_setrun(sc, 0);
1389142215Sglebius		return (0);
1390142215Sglebius	}
1391142215Sglebius
1392142215Sglebius	/* we have to do it by hands to check we won't match on us */
1393142215Sglebius	ia_if = NULL; own = 0;
1394142215Sglebius	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
1395142215Sglebius		/* and, yeah, we need a multicast-capable iface too */
1396147256Sbrooks		if (ia->ia_ifp != SC2IFP(sc) &&
1397142215Sglebius		    (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1398142215Sglebius		    (iaddr & ia->ia_subnetmask) == ia->ia_subnet) {
1399142215Sglebius			if (!ia_if)
1400142215Sglebius				ia_if = ia;
1401142215Sglebius			if (sin->sin_addr.s_addr ==
1402142215Sglebius			    ia->ia_addr.sin_addr.s_addr)
1403142215Sglebius				own++;
1404142215Sglebius		}
1405142215Sglebius	}
1406142215Sglebius
1407142215Sglebius	if (!ia_if)
1408142215Sglebius		return (EADDRNOTAVAIL);
1409142215Sglebius
1410142215Sglebius	ia = ia_if;
1411142215Sglebius	ifp = ia->ia_ifp;
1412142215Sglebius
1413142215Sglebius	if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1414142215Sglebius	    (imo->imo_multicast_ifp && imo->imo_multicast_ifp != ifp))
1415142215Sglebius		return (EADDRNOTAVAIL);
1416142215Sglebius
1417142215Sglebius	if (imo->imo_num_memberships == 0) {
1418142215Sglebius		addr.s_addr = htonl(INADDR_CARP_GROUP);
1419142215Sglebius		if ((imo->imo_membership[0] = in_addmulti(&addr, ifp)) == NULL)
1420142215Sglebius			return (ENOBUFS);
1421142215Sglebius		imo->imo_num_memberships++;
1422142215Sglebius		imo->imo_multicast_ifp = ifp;
1423142215Sglebius		imo->imo_multicast_ttl = CARP_DFLTTL;
1424142215Sglebius		imo->imo_multicast_loop = 0;
1425142215Sglebius	}
1426142215Sglebius
1427142215Sglebius	if (!ifp->if_carp) {
1428142215Sglebius
1429142215Sglebius		MALLOC(cif, struct carp_if *, sizeof(*cif), M_CARP,
1430142215Sglebius		    M_WAITOK|M_ZERO);
1431142215Sglebius		if (!cif) {
1432142215Sglebius			error = ENOBUFS;
1433142215Sglebius			goto cleanup;
1434142215Sglebius		}
1435142215Sglebius		if ((error = ifpromisc(ifp, 1))) {
1436142215Sglebius			FREE(cif, M_CARP);
1437142215Sglebius			goto cleanup;
1438142215Sglebius		}
1439142215Sglebius
1440142215Sglebius		CARP_LOCK_INIT(cif);
1441142215Sglebius		CARP_LOCK(cif);
1442142215Sglebius		cif->vhif_ifp = ifp;
1443142215Sglebius		TAILQ_INIT(&cif->vhif_vrs);
1444142215Sglebius		ifp->if_carp = cif;
1445142215Sglebius
1446142215Sglebius	} else {
1447142215Sglebius		struct carp_softc *vr;
1448142215Sglebius
1449142215Sglebius		cif = (struct carp_if *)ifp->if_carp;
1450142215Sglebius		CARP_LOCK(cif);
1451142215Sglebius		TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1452142215Sglebius			if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1453142215Sglebius				CARP_UNLOCK(cif);
1454142215Sglebius				error = EINVAL;
1455142215Sglebius				goto cleanup;
1456142215Sglebius			}
1457142215Sglebius	}
1458142215Sglebius	sc->sc_ia = ia;
1459142564Sglebius	sc->sc_carpdev = ifp;
1460142215Sglebius
1461142215Sglebius	{ /* XXX prevent endless loop if already in queue */
1462142215Sglebius	struct carp_softc *vr, *after = NULL;
1463142215Sglebius	int myself = 0;
1464142215Sglebius	cif = (struct carp_if *)ifp->if_carp;
1465142215Sglebius
1466142215Sglebius	/* XXX: cif should not change, right? So we still hold the lock */
1467142215Sglebius	CARP_LOCK_ASSERT(cif);
1468142215Sglebius
1469142215Sglebius	TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1470142215Sglebius		if (vr == sc)
1471142215Sglebius			myself = 1;
1472142215Sglebius		if (vr->sc_vhid < sc->sc_vhid)
1473142215Sglebius			after = vr;
1474142215Sglebius	}
1475142215Sglebius
1476142215Sglebius	if (!myself) {
1477142215Sglebius		/* We're trying to keep things in order */
1478142215Sglebius		if (after == NULL) {
1479142215Sglebius			TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1480142215Sglebius		} else {
1481142215Sglebius			TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1482142215Sglebius		}
1483142215Sglebius		cif->vhif_nvrs++;
1484142215Sglebius	}
1485142215Sglebius	}
1486142215Sglebius
1487142215Sglebius	sc->sc_naddrs++;
1488147256Sbrooks	SC2IFP(sc)->if_flags |= IFF_UP;
1489142215Sglebius	if (own)
1490142215Sglebius		sc->sc_advskew = 0;
1491144329Sglebius	carp_sc_state_locked(sc);
1492142215Sglebius	carp_setrun(sc, 0);
1493142215Sglebius
1494142914Sglebius	CARP_UNLOCK(cif);
1495142914Sglebius
1496142215Sglebius	return (0);
1497142215Sglebius
1498142215Sglebiuscleanup:
1499142215Sglebius	in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1500142215Sglebius	return (error);
1501142215Sglebius}
1502142215Sglebius
1503142559Sglebiusstatic int
1504142215Sglebiuscarp_del_addr(struct carp_softc *sc, struct sockaddr_in *sin)
1505142215Sglebius{
1506142215Sglebius	int error = 0;
1507142215Sglebius
1508142215Sglebius	if (!--sc->sc_naddrs) {
1509142564Sglebius		struct carp_if *cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1510142215Sglebius		struct ip_moptions *imo = &sc->sc_imo;
1511142215Sglebius
1512142914Sglebius		CARP_LOCK(cif);
1513142215Sglebius		callout_stop(&sc->sc_ad_tmo);
1514147256Sbrooks		SC2IFP(sc)->if_flags &= ~(IFF_UP|IFF_RUNNING);
1515142215Sglebius		sc->sc_vhid = -1;
1516142215Sglebius		in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1517142215Sglebius		imo->imo_multicast_ifp = NULL;
1518142215Sglebius		TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1519142215Sglebius		if (!--cif->vhif_nvrs) {
1520142564Sglebius			sc->sc_carpdev->if_carp = NULL;
1521142215Sglebius			CARP_LOCK_DESTROY(cif);
1522142215Sglebius			FREE(cif, M_IFADDR);
1523142215Sglebius		} else {
1524142215Sglebius			CARP_UNLOCK(cif);
1525142215Sglebius		}
1526142215Sglebius	}
1527142215Sglebius
1528142215Sglebius	return (error);
1529142215Sglebius}
1530142215Sglebius
1531142215Sglebius#ifdef INET6
1532142559Sglebiusstatic int
1533142215Sglebiuscarp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1534142215Sglebius{
1535142215Sglebius	struct ifnet *ifp;
1536142215Sglebius	struct carp_if *cif;
1537142215Sglebius	struct in6_ifaddr *ia, *ia_if;
1538142215Sglebius	struct ip6_moptions *im6o = &sc->sc_im6o;
1539142215Sglebius	struct in6_multi_mship *imm;
1540148385Sume	struct in6_addr in6;
1541142215Sglebius	int own, error;
1542142215Sglebius
1543142215Sglebius	if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1544147256Sbrooks		if (!(SC2IFP(sc)->if_flags & IFF_UP))
1545142215Sglebius			carp_set_state(sc, INIT);
1546142215Sglebius		if (sc->sc_naddrs6)
1547147256Sbrooks			SC2IFP(sc)->if_flags |= IFF_UP;
1548142215Sglebius		carp_setrun(sc, 0);
1549142215Sglebius		return (0);
1550142215Sglebius	}
1551142215Sglebius
1552142215Sglebius	/* we have to do it by hands to check we won't match on us */
1553142215Sglebius	ia_if = NULL; own = 0;
1554142215Sglebius	for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1555142215Sglebius		int i;
1556142215Sglebius
1557142215Sglebius		for (i = 0; i < 4; i++) {
1558142215Sglebius			if ((sin6->sin6_addr.s6_addr32[i] &
1559142215Sglebius			    ia->ia_prefixmask.sin6_addr.s6_addr32[i]) !=
1560142215Sglebius			    (ia->ia_addr.sin6_addr.s6_addr32[i] &
1561142215Sglebius			    ia->ia_prefixmask.sin6_addr.s6_addr32[i]))
1562142215Sglebius				break;
1563142215Sglebius		}
1564142215Sglebius		/* and, yeah, we need a multicast-capable iface too */
1565147256Sbrooks		if (ia->ia_ifp != SC2IFP(sc) &&
1566142215Sglebius		    (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1567142215Sglebius		    (i == 4)) {
1568142215Sglebius			if (!ia_if)
1569142215Sglebius				ia_if = ia;
1570142215Sglebius			if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
1571142215Sglebius			    &ia->ia_addr.sin6_addr))
1572142215Sglebius				own++;
1573142215Sglebius		}
1574142215Sglebius	}
1575142215Sglebius
1576142215Sglebius	if (!ia_if)
1577142215Sglebius		return (EADDRNOTAVAIL);
1578142215Sglebius	ia = ia_if;
1579142215Sglebius	ifp = ia->ia_ifp;
1580142215Sglebius
1581142215Sglebius	if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1582142215Sglebius	    (im6o->im6o_multicast_ifp && im6o->im6o_multicast_ifp != ifp))
1583142215Sglebius		return (EADDRNOTAVAIL);
1584142215Sglebius
1585142215Sglebius	if (!sc->sc_naddrs6) {
1586142215Sglebius		im6o->im6o_multicast_ifp = ifp;
1587142215Sglebius
1588142215Sglebius		/* join CARP multicast address */
1589148385Sume		bzero(&in6, sizeof(in6));
1590148385Sume		in6.s6_addr16[0] = htons(0xff02);
1591148385Sume		in6.s6_addr8[15] = 0x12;
1592148385Sume		if (in6_setscope(&in6, ifp, NULL) != 0)
1593142215Sglebius			goto cleanup;
1594148385Sume		if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL)
1595148385Sume			goto cleanup;
1596142215Sglebius		LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1597142215Sglebius
1598142215Sglebius		/* join solicited multicast address */
1599148385Sume		bzero(&in6, sizeof(in6));
1600148385Sume		in6.s6_addr16[0] = htons(0xff02);
1601148385Sume		in6.s6_addr32[1] = 0;
1602148385Sume		in6.s6_addr32[2] = htonl(1);
1603148385Sume		in6.s6_addr32[3] = sin6->sin6_addr.s6_addr32[3];
1604148385Sume		in6.s6_addr8[12] = 0xff;
1605148385Sume		if (in6_setscope(&in6, ifp, NULL) != 0)
1606142215Sglebius			goto cleanup;
1607148385Sume		if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL)
1608148385Sume			goto cleanup;
1609142215Sglebius		LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1610142215Sglebius	}
1611142215Sglebius
1612142215Sglebius	if (!ifp->if_carp) {
1613142215Sglebius		MALLOC(cif, struct carp_if *, sizeof(*cif), M_CARP,
1614142215Sglebius		    M_WAITOK|M_ZERO);
1615142215Sglebius		if (!cif) {
1616142215Sglebius			error = ENOBUFS;
1617142215Sglebius			goto cleanup;
1618142215Sglebius		}
1619142215Sglebius		if ((error = ifpromisc(ifp, 1))) {
1620142215Sglebius			FREE(cif, M_CARP);
1621142215Sglebius			goto cleanup;
1622142215Sglebius		}
1623142215Sglebius
1624142215Sglebius		CARP_LOCK_INIT(cif);
1625142215Sglebius		CARP_LOCK(cif);
1626142215Sglebius		cif->vhif_ifp = ifp;
1627142215Sglebius		TAILQ_INIT(&cif->vhif_vrs);
1628142215Sglebius		ifp->if_carp = cif;
1629142215Sglebius
1630142215Sglebius	} else {
1631142215Sglebius		struct carp_softc *vr;
1632142215Sglebius
1633142215Sglebius		cif = (struct carp_if *)ifp->if_carp;
1634142215Sglebius		CARP_LOCK(cif);
1635142215Sglebius		TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1636142215Sglebius			if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1637142215Sglebius				CARP_UNLOCK(cif);
1638142215Sglebius				error = EINVAL;
1639142215Sglebius				goto cleanup;
1640142215Sglebius			}
1641142215Sglebius	}
1642142215Sglebius	sc->sc_ia6 = ia;
1643142564Sglebius	sc->sc_carpdev = ifp;
1644142215Sglebius
1645142215Sglebius	{ /* XXX prevent endless loop if already in queue */
1646142215Sglebius	struct carp_softc *vr, *after = NULL;
1647142215Sglebius	int myself = 0;
1648142215Sglebius	cif = (struct carp_if *)ifp->if_carp;
1649142215Sglebius	CARP_LOCK_ASSERT(cif);
1650142215Sglebius
1651142215Sglebius	TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1652142215Sglebius		if (vr == sc)
1653142215Sglebius			myself = 1;
1654142215Sglebius		if (vr->sc_vhid < sc->sc_vhid)
1655142215Sglebius			after = vr;
1656142215Sglebius	}
1657142215Sglebius
1658142215Sglebius	if (!myself) {
1659142215Sglebius		/* We're trying to keep things in order */
1660142215Sglebius		if (after == NULL) {
1661142215Sglebius			TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1662142215Sglebius		} else {
1663142215Sglebius			TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1664142215Sglebius		}
1665142215Sglebius		cif->vhif_nvrs++;
1666142215Sglebius	}
1667142215Sglebius	}
1668142215Sglebius
1669142215Sglebius	sc->sc_naddrs6++;
1670147256Sbrooks	SC2IFP(sc)->if_flags |= IFF_UP;
1671142215Sglebius	if (own)
1672142215Sglebius		sc->sc_advskew = 0;
1673144329Sglebius	carp_sc_state_locked(sc);
1674142215Sglebius	carp_setrun(sc, 0);
1675142215Sglebius
1676142914Sglebius	CARP_UNLOCK(cif);
1677142914Sglebius
1678142215Sglebius	return (0);
1679142215Sglebius
1680142215Sglebiuscleanup:
1681142215Sglebius	/* clean up multicast memberships */
1682142215Sglebius	if (!sc->sc_naddrs6) {
1683142215Sglebius		while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1684142215Sglebius			imm = LIST_FIRST(&im6o->im6o_memberships);
1685142215Sglebius			LIST_REMOVE(imm, i6mm_chain);
1686142215Sglebius			in6_leavegroup(imm);
1687142215Sglebius		}
1688142215Sglebius	}
1689142215Sglebius	return (error);
1690142215Sglebius}
1691142215Sglebius
1692142559Sglebiusstatic int
1693142215Sglebiuscarp_del_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1694142215Sglebius{
1695142215Sglebius	int error = 0;
1696142215Sglebius
1697142215Sglebius	if (!--sc->sc_naddrs6) {
1698142564Sglebius		struct carp_if *cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1699142215Sglebius		struct ip6_moptions *im6o = &sc->sc_im6o;
1700142215Sglebius
1701142914Sglebius		CARP_LOCK(cif);
1702142215Sglebius		callout_stop(&sc->sc_ad_tmo);
1703147256Sbrooks		SC2IFP(sc)->if_flags &= ~(IFF_UP|IFF_RUNNING);
1704142215Sglebius		sc->sc_vhid = -1;
1705142215Sglebius		while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1706142215Sglebius			struct in6_multi_mship *imm =
1707142215Sglebius			    LIST_FIRST(&im6o->im6o_memberships);
1708142215Sglebius
1709142215Sglebius			LIST_REMOVE(imm, i6mm_chain);
1710142215Sglebius			in6_leavegroup(imm);
1711142215Sglebius		}
1712142215Sglebius		im6o->im6o_multicast_ifp = NULL;
1713142215Sglebius		TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1714142215Sglebius		if (!--cif->vhif_nvrs) {
1715142215Sglebius			CARP_LOCK_DESTROY(cif);
1716142564Sglebius			sc->sc_carpdev->if_carp = NULL;
1717142215Sglebius			FREE(cif, M_IFADDR);
1718142215Sglebius		} else
1719142215Sglebius			CARP_UNLOCK(cif);
1720142215Sglebius	}
1721142215Sglebius
1722142215Sglebius	return (error);
1723142215Sglebius}
1724142215Sglebius#endif /* INET6 */
1725142215Sglebius
1726142559Sglebiusstatic int
1727142215Sglebiuscarp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr)
1728142215Sglebius{
1729142215Sglebius	struct carp_softc *sc = ifp->if_softc, *vr;
1730142215Sglebius	struct carpreq carpr;
1731142215Sglebius	struct ifaddr *ifa;
1732142215Sglebius	struct ifreq *ifr;
1733142215Sglebius	struct ifaliasreq *ifra;
1734142914Sglebius	int locked = 0, error = 0;
1735142215Sglebius
1736142215Sglebius	ifa = (struct ifaddr *)addr;
1737142215Sglebius	ifra = (struct ifaliasreq *)addr;
1738142215Sglebius	ifr = (struct ifreq *)addr;
1739142215Sglebius
1740142215Sglebius	switch (cmd) {
1741142215Sglebius	case SIOCSIFADDR:
1742142215Sglebius		switch (ifa->ifa_addr->sa_family) {
1743142215Sglebius#ifdef INET
1744142215Sglebius		case AF_INET:
1745147256Sbrooks			SC2IFP(sc)->if_flags |= IFF_UP;
1746142215Sglebius			bcopy(ifa->ifa_addr, ifa->ifa_dstaddr,
1747142215Sglebius			    sizeof(struct sockaddr));
1748142215Sglebius			error = carp_set_addr(sc, satosin(ifa->ifa_addr));
1749142215Sglebius			break;
1750142215Sglebius#endif /* INET */
1751142215Sglebius#ifdef INET6
1752142215Sglebius		case AF_INET6:
1753147256Sbrooks			SC2IFP(sc)->if_flags |= IFF_UP;
1754142215Sglebius			error = carp_set_addr6(sc, satosin6(ifa->ifa_addr));
1755142215Sglebius			break;
1756142215Sglebius#endif /* INET6 */
1757142215Sglebius		default:
1758142215Sglebius			error = EAFNOSUPPORT;
1759142215Sglebius			break;
1760142215Sglebius		}
1761142215Sglebius		break;
1762142215Sglebius
1763142215Sglebius	case SIOCAIFADDR:
1764142215Sglebius		switch (ifa->ifa_addr->sa_family) {
1765142215Sglebius#ifdef INET
1766142215Sglebius		case AF_INET:
1767147256Sbrooks			SC2IFP(sc)->if_flags |= IFF_UP;
1768142215Sglebius			bcopy(ifa->ifa_addr, ifa->ifa_dstaddr,
1769142215Sglebius			    sizeof(struct sockaddr));
1770142215Sglebius			error = carp_set_addr(sc, satosin(&ifra->ifra_addr));
1771142215Sglebius			break;
1772142215Sglebius#endif /* INET */
1773142215Sglebius#ifdef INET6
1774142215Sglebius		case AF_INET6:
1775147256Sbrooks			SC2IFP(sc)->if_flags |= IFF_UP;
1776142215Sglebius			error = carp_set_addr6(sc, satosin6(&ifra->ifra_addr));
1777142215Sglebius			break;
1778142215Sglebius#endif /* INET6 */
1779142215Sglebius		default:
1780142215Sglebius			error = EAFNOSUPPORT;
1781142215Sglebius			break;
1782142215Sglebius		}
1783142215Sglebius		break;
1784142215Sglebius
1785142215Sglebius	case SIOCDIFADDR:
1786142215Sglebius		switch (ifa->ifa_addr->sa_family) {
1787142215Sglebius#ifdef INET
1788142215Sglebius		case AF_INET:
1789142215Sglebius			error = carp_del_addr(sc, satosin(&ifra->ifra_addr));
1790142215Sglebius			break;
1791142215Sglebius#endif /* INET */
1792142215Sglebius#ifdef INET6
1793142215Sglebius		case AF_INET6:
1794142215Sglebius			error = carp_del_addr6(sc, satosin6(&ifra->ifra_addr));
1795142215Sglebius			break;
1796142215Sglebius#endif /* INET6 */
1797142215Sglebius		default:
1798142215Sglebius			error = EAFNOSUPPORT;
1799142215Sglebius			break;
1800142215Sglebius		}
1801142215Sglebius		break;
1802142215Sglebius
1803142215Sglebius	case SIOCSIFFLAGS:
1804142914Sglebius		if (sc->sc_carpdev) {
1805142914Sglebius			locked = 1;
1806142914Sglebius			CARP_SCLOCK(sc);
1807142914Sglebius		}
1808142215Sglebius		if (sc->sc_state != INIT && !(ifr->ifr_flags & IFF_UP)) {
1809142215Sglebius 			callout_stop(&sc->sc_ad_tmo);
1810142215Sglebius 			callout_stop(&sc->sc_md_tmo);
1811142215Sglebius 			callout_stop(&sc->sc_md6_tmo);
1812142215Sglebius			if (sc->sc_state == MASTER)
1813142914Sglebius				carp_send_ad_locked(sc);
1814142215Sglebius			carp_set_state(sc, INIT);
1815142215Sglebius			carp_setrun(sc, 0);
1816142215Sglebius		} else if (sc->sc_state == INIT && (ifr->ifr_flags & IFF_UP)) {
1817147256Sbrooks			SC2IFP(sc)->if_flags |= IFF_UP;
1818142215Sglebius			carp_setrun(sc, 0);
1819142215Sglebius		}
1820142215Sglebius		break;
1821142215Sglebius
1822142215Sglebius	case SIOCSVH:
1823142215Sglebius		if ((error = suser(curthread)) != 0)
1824142215Sglebius			break;
1825142215Sglebius		if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
1826142215Sglebius			break;
1827142215Sglebius		error = 1;
1828142914Sglebius		if (sc->sc_carpdev) {
1829142914Sglebius			locked = 1;
1830142914Sglebius			CARP_SCLOCK(sc);
1831142914Sglebius		}
1832142215Sglebius		if (sc->sc_state != INIT && carpr.carpr_state != sc->sc_state) {
1833142215Sglebius			switch (carpr.carpr_state) {
1834142215Sglebius			case BACKUP:
1835142215Sglebius				callout_stop(&sc->sc_ad_tmo);
1836142215Sglebius				carp_set_state(sc, BACKUP);
1837142215Sglebius				carp_setrun(sc, 0);
1838142215Sglebius				carp_setroute(sc, RTM_DELETE);
1839142215Sglebius				break;
1840142215Sglebius			case MASTER:
1841142914Sglebius				carp_master_down_locked(sc);
1842142215Sglebius				break;
1843142215Sglebius			default:
1844142215Sglebius				break;
1845142215Sglebius			}
1846142215Sglebius		}
1847142215Sglebius		if (carpr.carpr_vhid > 0) {
1848142215Sglebius			if (carpr.carpr_vhid > 255) {
1849142215Sglebius				error = EINVAL;
1850142215Sglebius				break;
1851142215Sglebius			}
1852142564Sglebius			if (sc->sc_carpdev) {
1853142215Sglebius				struct carp_if *cif;
1854142564Sglebius				cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1855142215Sglebius				TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1856142215Sglebius					if (vr != sc &&
1857143806Sglebius					    vr->sc_vhid == carpr.carpr_vhid)
1858143806Sglebius						return EEXIST;
1859142215Sglebius			}
1860142215Sglebius			sc->sc_vhid = carpr.carpr_vhid;
1861147256Sbrooks			IFP2ENADDR(sc->sc_ifp)[0] = 0;
1862147256Sbrooks			IFP2ENADDR(sc->sc_ifp)[1] = 0;
1863147256Sbrooks			IFP2ENADDR(sc->sc_ifp)[2] = 0x5e;
1864147256Sbrooks			IFP2ENADDR(sc->sc_ifp)[3] = 0;
1865147256Sbrooks			IFP2ENADDR(sc->sc_ifp)[4] = 1;
1866147256Sbrooks			IFP2ENADDR(sc->sc_ifp)[5] = sc->sc_vhid;
1867142215Sglebius			error--;
1868142215Sglebius		}
1869142215Sglebius		if (carpr.carpr_advbase > 0 || carpr.carpr_advskew > 0) {
1870142215Sglebius			if (carpr.carpr_advskew >= 255) {
1871142215Sglebius				error = EINVAL;
1872142215Sglebius				break;
1873142215Sglebius			}
1874142215Sglebius			if (carpr.carpr_advbase > 255) {
1875142215Sglebius				error = EINVAL;
1876142215Sglebius				break;
1877142215Sglebius			}
1878142215Sglebius			sc->sc_advbase = carpr.carpr_advbase;
1879142215Sglebius			sc->sc_advskew = carpr.carpr_advskew;
1880142215Sglebius			error--;
1881142215Sglebius		}
1882142215Sglebius		bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1883142215Sglebius		if (error > 0)
1884142215Sglebius			error = EINVAL;
1885142215Sglebius		else {
1886142215Sglebius			error = 0;
1887142215Sglebius			carp_setrun(sc, 0);
1888142215Sglebius		}
1889142215Sglebius		break;
1890142215Sglebius
1891142215Sglebius	case SIOCGVH:
1892142914Sglebius		/* XXX: lockless read */
1893142215Sglebius		bzero(&carpr, sizeof(carpr));
1894142215Sglebius		carpr.carpr_state = sc->sc_state;
1895142215Sglebius		carpr.carpr_vhid = sc->sc_vhid;
1896142215Sglebius		carpr.carpr_advbase = sc->sc_advbase;
1897142215Sglebius		carpr.carpr_advskew = sc->sc_advskew;
1898142215Sglebius		if (suser(curthread) == 0)
1899142215Sglebius			bcopy(sc->sc_key, carpr.carpr_key,
1900142215Sglebius			    sizeof(carpr.carpr_key));
1901142215Sglebius		error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
1902142215Sglebius		break;
1903142215Sglebius
1904142215Sglebius	default:
1905142215Sglebius		error = EINVAL;
1906142215Sglebius	}
1907142215Sglebius
1908142914Sglebius	if (locked)
1909142914Sglebius		CARP_SCUNLOCK(sc);
1910142914Sglebius
1911142215Sglebius	carp_hmac_prepare(sc);
1912142914Sglebius
1913142215Sglebius	return (error);
1914142215Sglebius}
1915142215Sglebius
1916142215Sglebius/*
1917142215Sglebius * XXX: this is looutput. We should eventually use it from there.
1918142215Sglebius */
1919142215Sglebiusstatic int
1920142215Sglebiuscarp_looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
1921142215Sglebius    struct rtentry *rt)
1922142215Sglebius{
1923147611Sdwmalone	u_int32_t af;
1924147611Sdwmalone
1925142215Sglebius	M_ASSERTPKTHDR(m); /* check if we have the packet header */
1926142215Sglebius
1927142215Sglebius	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
1928142215Sglebius		m_freem(m);
1929142215Sglebius		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
1930142215Sglebius			rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1931142215Sglebius	}
1932142215Sglebius
1933142215Sglebius	ifp->if_opackets++;
1934142215Sglebius	ifp->if_obytes += m->m_pkthdr.len;
1935147611Sdwmalone
1936147611Sdwmalone	/* BPF writes need to be handled specially. */
1937147611Sdwmalone	if (dst->sa_family == AF_UNSPEC) {
1938147611Sdwmalone		bcopy(dst->sa_data, &af, sizeof(af));
1939147611Sdwmalone		dst->sa_family = af;
1940147611Sdwmalone	}
1941147611Sdwmalone
1942142215Sglebius#if 1	/* XXX */
1943142215Sglebius	switch (dst->sa_family) {
1944142215Sglebius	case AF_INET:
1945142215Sglebius	case AF_INET6:
1946142215Sglebius	case AF_IPX:
1947142215Sglebius	case AF_APPLETALK:
1948142215Sglebius		break;
1949142215Sglebius	default:
1950142215Sglebius		printf("carp_looutput: af=%d unexpected\n", dst->sa_family);
1951142215Sglebius		m_freem(m);
1952142215Sglebius		return (EAFNOSUPPORT);
1953142215Sglebius	}
1954142215Sglebius#endif
1955142215Sglebius	return(if_simloop(ifp, m, dst->sa_family, 0));
1956142215Sglebius}
1957142215Sglebius
1958142215Sglebius/*
1959142215Sglebius * Start output on carp interface. This function should never be called.
1960142215Sglebius */
1961142559Sglebiusstatic void
1962142215Sglebiuscarp_start(struct ifnet *ifp)
1963142215Sglebius{
1964142215Sglebius#ifdef DEBUG
1965142215Sglebius	printf("%s: start called\n", ifp->if_xname);
1966142215Sglebius#endif
1967142215Sglebius}
1968142215Sglebius
1969142215Sglebiusint
1970142215Sglebiuscarp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
1971142215Sglebius    struct rtentry *rt)
1972142215Sglebius{
1973142215Sglebius	struct m_tag *mtag;
1974142215Sglebius	struct carp_softc *sc;
1975142215Sglebius	struct ifnet *carp_ifp;
1976142215Sglebius
1977142215Sglebius	if (!sa)
1978142215Sglebius		return (0);
1979142215Sglebius
1980142215Sglebius	switch (sa->sa_family) {
1981142215Sglebius#ifdef INET
1982142215Sglebius	case AF_INET:
1983142215Sglebius		break;
1984142215Sglebius#endif /* INET */
1985142215Sglebius#ifdef INET6
1986142215Sglebius	case AF_INET6:
1987142215Sglebius		break;
1988142215Sglebius#endif /* INET6 */
1989142215Sglebius	default:
1990142215Sglebius		return (0);
1991142215Sglebius	}
1992142215Sglebius
1993142215Sglebius	mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
1994142215Sglebius	if (mtag == NULL)
1995142215Sglebius		return (0);
1996142215Sglebius
1997142215Sglebius	bcopy(mtag + 1, &carp_ifp, sizeof(struct ifnet *));
1998142215Sglebius	sc = carp_ifp->if_softc;
1999142215Sglebius
2000142215Sglebius	/* Set the source MAC address to Virtual Router MAC Address */
2001142215Sglebius	switch (ifp->if_type) {
2002142798Syar	case IFT_ETHER:
2003142798Syar	case IFT_L2VLAN: {
2004142215Sglebius			struct ether_header *eh;
2005142215Sglebius
2006142215Sglebius			eh = mtod(m, struct ether_header *);
2007142215Sglebius			eh->ether_shost[0] = 0;
2008142215Sglebius			eh->ether_shost[1] = 0;
2009142215Sglebius			eh->ether_shost[2] = 0x5e;
2010142215Sglebius			eh->ether_shost[3] = 0;
2011142215Sglebius			eh->ether_shost[4] = 1;
2012142215Sglebius			eh->ether_shost[5] = sc->sc_vhid;
2013142215Sglebius		}
2014142215Sglebius		break;
2015142215Sglebius	case IFT_FDDI: {
2016142215Sglebius			struct fddi_header *fh;
2017142215Sglebius
2018142215Sglebius			fh = mtod(m, struct fddi_header *);
2019142215Sglebius			fh->fddi_shost[0] = 0;
2020142215Sglebius			fh->fddi_shost[1] = 0;
2021142215Sglebius			fh->fddi_shost[2] = 0x5e;
2022142215Sglebius			fh->fddi_shost[3] = 0;
2023142215Sglebius			fh->fddi_shost[4] = 1;
2024142215Sglebius			fh->fddi_shost[5] = sc->sc_vhid;
2025142215Sglebius		}
2026142215Sglebius		break;
2027142215Sglebius	case IFT_ISO88025: {
2028142215Sglebius 			struct iso88025_header *th;
2029142215Sglebius 			th = mtod(m, struct iso88025_header *);
2030142215Sglebius			th->iso88025_shost[0] = 3;
2031142215Sglebius			th->iso88025_shost[1] = 0;
2032142215Sglebius			th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1);
2033142215Sglebius			th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1);
2034142215Sglebius			th->iso88025_shost[4] = 0;
2035142215Sglebius			th->iso88025_shost[5] = 0;
2036142215Sglebius		}
2037142215Sglebius		break;
2038142215Sglebius	default:
2039142215Sglebius		printf("%s: carp is not supported for this interface type\n",
2040142215Sglebius		    ifp->if_xname);
2041142215Sglebius		return (EOPNOTSUPP);
2042142215Sglebius	}
2043142215Sglebius
2044142215Sglebius	return (0);
2045142215Sglebius}
2046142215Sglebius
2047142559Sglebiusstatic void
2048142215Sglebiuscarp_set_state(struct carp_softc *sc, int state)
2049142215Sglebius{
2050142914Sglebius
2051142914Sglebius	if (sc->sc_carpdev)
2052142914Sglebius		CARP_SCLOCK_ASSERT(sc);
2053142914Sglebius
2054142215Sglebius	if (sc->sc_state == state)
2055142215Sglebius		return;
2056142215Sglebius
2057142215Sglebius	sc->sc_state = state;
2058142215Sglebius	switch (state) {
2059142215Sglebius	case BACKUP:
2060147256Sbrooks		SC2IFP(sc)->if_link_state = LINK_STATE_DOWN;
2061142215Sglebius		break;
2062142215Sglebius	case MASTER:
2063147256Sbrooks		SC2IFP(sc)->if_link_state = LINK_STATE_UP;
2064142215Sglebius		break;
2065142215Sglebius	default:
2066147256Sbrooks		SC2IFP(sc)->if_link_state = LINK_STATE_UNKNOWN;
2067142215Sglebius		break;
2068142215Sglebius	}
2069147256Sbrooks	rt_ifmsg(SC2IFP(sc));
2070142215Sglebius}
2071142215Sglebius
2072142215Sglebiusvoid
2073142215Sglebiuscarp_carpdev_state(void *v)
2074142215Sglebius{
2075142215Sglebius	struct carp_if *cif = v;
2076142914Sglebius
2077142914Sglebius	CARP_LOCK(cif);
2078142914Sglebius	carp_carpdev_state_locked(cif);
2079142914Sglebius	CARP_UNLOCK(cif);
2080142914Sglebius}
2081142914Sglebius
2082142914Sglebiusstatic void
2083142914Sglebiuscarp_carpdev_state_locked(struct carp_if *cif)
2084142914Sglebius{
2085142215Sglebius	struct carp_softc *sc;
2086142215Sglebius
2087144329Sglebius	TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list)
2088144329Sglebius		carp_sc_state_locked(sc);
2089144329Sglebius}
2090144329Sglebius
2091144329Sglebiusstatic void
2092144329Sglebiuscarp_sc_state_locked(struct carp_softc *sc)
2093144329Sglebius{
2094144329Sglebius	CARP_SCLOCK_ASSERT(sc);
2095144329Sglebius
2096144329Sglebius	if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
2097144329Sglebius	    !(sc->sc_carpdev->if_flags & IFF_UP)) {
2098147256Sbrooks		sc->sc_flags_backup = SC2IFP(sc)->if_flags;
2099147256Sbrooks		SC2IFP(sc)->if_flags &= ~(IFF_UP|IFF_RUNNING);
2100144329Sglebius		callout_stop(&sc->sc_ad_tmo);
2101144329Sglebius		callout_stop(&sc->sc_md_tmo);
2102144329Sglebius		callout_stop(&sc->sc_md6_tmo);
2103144329Sglebius		carp_set_state(sc, INIT);
2104144329Sglebius		carp_setrun(sc, 0);
2105144329Sglebius		if (!sc->sc_suppress) {
2106144329Sglebius			carp_suppress_preempt++;
2107144329Sglebius			if (carp_suppress_preempt == 1) {
2108144329Sglebius				CARP_SCUNLOCK(sc);
2109144329Sglebius				carp_send_ad_all();
2110144329Sglebius				CARP_SCLOCK(sc);
2111142215Sglebius			}
2112142215Sglebius		}
2113144329Sglebius		sc->sc_suppress = 1;
2114144329Sglebius	} else {
2115147256Sbrooks		SC2IFP(sc)->if_flags |= sc->sc_flags_backup;
2116144329Sglebius		carp_set_state(sc, INIT);
2117144329Sglebius		carp_setrun(sc, 0);
2118144329Sglebius		if (sc->sc_suppress)
2119144329Sglebius			carp_suppress_preempt--;
2120144329Sglebius		sc->sc_suppress = 0;
2121142215Sglebius	}
2122144329Sglebius
2123144329Sglebius	return;
2124142215Sglebius}
2125142215Sglebius
2126142215Sglebiusstatic int
2127142215Sglebiuscarp_modevent(module_t mod, int type, void *data)
2128142215Sglebius{
2129142215Sglebius	int error = 0;
2130142215Sglebius
2131142215Sglebius	switch (type) {
2132142215Sglebius	case MOD_LOAD:
2133142911Sglebius		mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
2134142215Sglebius		LIST_INIT(&carpif_list);
2135142215Sglebius		if_clone_attach(&carp_cloner);
2136142215Sglebius		break;
2137142215Sglebius
2138142215Sglebius	case MOD_UNLOAD:
2139142215Sglebius		if_clone_detach(&carp_cloner);
2140142215Sglebius		while (!LIST_EMPTY(&carpif_list))
2141147256Sbrooks			carp_clone_destroy(SC2IFP(LIST_FIRST(&carpif_list)));
2142142911Sglebius		mtx_destroy(&carp_mtx);
2143142215Sglebius		break;
2144142215Sglebius
2145142215Sglebius	default:
2146142215Sglebius		error = EINVAL;
2147142215Sglebius		break;
2148142215Sglebius	}
2149142215Sglebius
2150142215Sglebius	return error;
2151142215Sglebius}
2152142215Sglebius
2153142215Sglebiusstatic moduledata_t carp_mod = {
2154142215Sglebius	"carp",
2155142215Sglebius	carp_modevent,
2156142215Sglebius	0
2157142215Sglebius};
2158142215Sglebius
2159142215SglebiusDECLARE_MODULE(carp, carp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
2160