ip_carp.c revision 195976
1142215Sglebius/*
2142215Sglebius * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
3142215Sglebius * Copyright (c) 2003 Ryan McBride. All rights reserved.
4142215Sglebius *
5142215Sglebius * Redistribution and use in source and binary forms, with or without
6142215Sglebius * modification, are permitted provided that the following conditions
7142215Sglebius * are met:
8142215Sglebius * 1. Redistributions of source code must retain the above copyright
9142215Sglebius *    notice, this list of conditions and the following disclaimer.
10142215Sglebius * 2. Redistributions in binary form must reproduce the above copyright
11142215Sglebius *    notice, this list of conditions and the following disclaimer in the
12142215Sglebius *    documentation and/or other materials provided with the distribution.
13142215Sglebius *
14142215Sglebius * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15142215Sglebius * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16142215Sglebius * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17142215Sglebius * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
18142215Sglebius * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19142215Sglebius * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20142215Sglebius * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21142215Sglebius * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22142215Sglebius * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23142215Sglebius * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24142215Sglebius * THE POSSIBILITY OF SUCH DAMAGE.
25142215Sglebius */
26142215Sglebius
27172467Ssilby#include <sys/cdefs.h>
28172467Ssilby__FBSDID("$FreeBSD: head/sys/netinet/ip_carp.c 195976 2009-07-30 17:40:47Z delphij $");
29172467Ssilby
30142215Sglebius#include "opt_carp.h"
31142215Sglebius#include "opt_bpf.h"
32142215Sglebius#include "opt_inet.h"
33142215Sglebius#include "opt_inet6.h"
34142215Sglebius
35142215Sglebius#include <sys/types.h>
36142215Sglebius#include <sys/param.h>
37142215Sglebius#include <sys/systm.h>
38142215Sglebius#include <sys/conf.h>
39142215Sglebius#include <sys/kernel.h>
40142215Sglebius#include <sys/limits.h>
41142215Sglebius#include <sys/malloc.h>
42142215Sglebius#include <sys/mbuf.h>
43142215Sglebius#include <sys/module.h>
44142215Sglebius#include <sys/time.h>
45164033Srwatson#include <sys/priv.h>
46142215Sglebius#include <sys/proc.h>
47142215Sglebius#include <sys/sysctl.h>
48142215Sglebius#include <sys/syslog.h>
49142215Sglebius#include <sys/signalvar.h>
50142215Sglebius#include <sys/filio.h>
51142215Sglebius#include <sys/sockio.h>
52142215Sglebius
53142215Sglebius#include <sys/socket.h>
54142215Sglebius#include <sys/vnode.h>
55181803Sbz#include <sys/vimage.h>
56142215Sglebius
57142215Sglebius#include <machine/stdarg.h>
58142215Sglebius
59142215Sglebius#include <net/bpf.h>
60142215Sglebius#include <net/ethernet.h>
61142215Sglebius#include <net/fddi.h>
62142215Sglebius#include <net/iso88025.h>
63142215Sglebius#include <net/if.h>
64142215Sglebius#include <net/if_clone.h>
65152410Sru#include <net/if_dl.h>
66142215Sglebius#include <net/if_types.h>
67142215Sglebius#include <net/route.h>
68142215Sglebius
69142215Sglebius#ifdef INET
70142215Sglebius#include <netinet/in.h>
71142215Sglebius#include <netinet/in_var.h>
72142215Sglebius#include <netinet/in_systm.h>
73142215Sglebius#include <netinet/ip.h>
74142215Sglebius#include <netinet/ip_var.h>
75142215Sglebius#include <netinet/if_ether.h>
76142215Sglebius#include <machine/in_cksum.h>
77142215Sglebius#endif
78142215Sglebius
79142215Sglebius#ifdef INET6
80142215Sglebius#include <netinet/icmp6.h>
81142215Sglebius#include <netinet/ip6.h>
82142215Sglebius#include <netinet6/ip6_var.h>
83148387Sume#include <netinet6/scope6_var.h>
84142215Sglebius#include <netinet6/nd6.h>
85142215Sglebius#endif
86142215Sglebius
87142215Sglebius#include <crypto/sha1.h>
88142215Sglebius#include <netinet/ip_carp.h>
89142215Sglebius
90142215Sglebius#define	CARP_IFNAME	"carp"
91142215Sglebiusstatic MALLOC_DEFINE(M_CARP, "CARP", "CARP interfaces");
92142215SglebiusSYSCTL_DECL(_net_inet_carp);
93142215Sglebius
94142215Sglebiusstruct carp_softc {
95147256Sbrooks	struct ifnet	 	*sc_ifp;	/* Interface clue */
96142901Sglebius	struct ifnet		*sc_carpdev;	/* Pointer to parent interface */
97142215Sglebius	struct in_ifaddr 	*sc_ia;		/* primary iface address */
98142215Sglebius	struct ip_moptions 	 sc_imo;
99142215Sglebius#ifdef INET6
100142215Sglebius	struct in6_ifaddr 	*sc_ia6;	/* primary iface address v6 */
101142215Sglebius	struct ip6_moptions 	 sc_im6o;
102142215Sglebius#endif /* INET6 */
103142215Sglebius	TAILQ_ENTRY(carp_softc)	 sc_list;
104142215Sglebius
105142215Sglebius	enum { INIT = 0, BACKUP, MASTER }	sc_state;
106142215Sglebius
107142215Sglebius	int			 sc_flags_backup;
108142215Sglebius	int			 sc_suppress;
109142215Sglebius
110142215Sglebius	int			 sc_sendad_errors;
111142215Sglebius#define	CARP_SENDAD_MAX_ERRORS	3
112142215Sglebius	int			 sc_sendad_success;
113142215Sglebius#define	CARP_SENDAD_MIN_SUCCESS 3
114142215Sglebius
115142215Sglebius	int			 sc_vhid;
116142215Sglebius	int			 sc_advskew;
117142215Sglebius	int			 sc_naddrs;
118142215Sglebius	int			 sc_naddrs6;
119142215Sglebius	int			 sc_advbase;	/* seconds */
120142215Sglebius	int			 sc_init_counter;
121142215Sglebius	u_int64_t		 sc_counter;
122142215Sglebius
123142215Sglebius	/* authentication */
124142215Sglebius#define CARP_HMAC_PAD	64
125142215Sglebius	unsigned char sc_key[CARP_KEY_LEN];
126142215Sglebius	unsigned char sc_pad[CARP_HMAC_PAD];
127142215Sglebius	SHA1_CTX sc_sha1;
128142215Sglebius
129142215Sglebius	struct callout		 sc_ad_tmo;	/* advertisement timeout */
130142215Sglebius	struct callout		 sc_md_tmo;	/* master down timeout */
131142215Sglebius	struct callout 		 sc_md6_tmo;	/* master down timeout */
132142215Sglebius
133142215Sglebius	LIST_ENTRY(carp_softc)	 sc_next;	/* Interface clue */
134142215Sglebius};
135147256Sbrooks#define	SC2IFP(sc)	((sc)->sc_ifp)
136142215Sglebius
137142215Sglebiusint carp_suppress_preempt = 0;
138142215Sglebiusint carp_opts[CARPCTL_MAXID] = { 0, 1, 0, 1, 0, 0 };	/* XXX for now */
139142215SglebiusSYSCTL_INT(_net_inet_carp, CARPCTL_ALLOW, allow, CTLFLAG_RW,
140142215Sglebius    &carp_opts[CARPCTL_ALLOW], 0, "Accept incoming CARP packets");
141142215SglebiusSYSCTL_INT(_net_inet_carp, CARPCTL_PREEMPT, preempt, CTLFLAG_RW,
142142215Sglebius    &carp_opts[CARPCTL_PREEMPT], 0, "high-priority backup preemption mode");
143142215SglebiusSYSCTL_INT(_net_inet_carp, CARPCTL_LOG, log, CTLFLAG_RW,
144142215Sglebius    &carp_opts[CARPCTL_LOG], 0, "log bad carp packets");
145142215SglebiusSYSCTL_INT(_net_inet_carp, CARPCTL_ARPBALANCE, arpbalance, CTLFLAG_RW,
146142215Sglebius    &carp_opts[CARPCTL_ARPBALANCE], 0, "balance arp responses");
147146226SglebiusSYSCTL_INT(_net_inet_carp, OID_AUTO, suppress_preempt, CTLFLAG_RD,
148146226Sglebius    &carp_suppress_preempt, 0, "Preemption is suppressed");
149142215Sglebius
150142215Sglebiusstruct carpstats carpstats;
151142215SglebiusSYSCTL_STRUCT(_net_inet_carp, CARPCTL_STATS, stats, CTLFLAG_RW,
152142215Sglebius    &carpstats, carpstats,
153142215Sglebius    "CARP statistics (struct carpstats, netinet/ip_carp.h)");
154142215Sglebius
155142215Sglebiusstruct carp_if {
156142215Sglebius	TAILQ_HEAD(, carp_softc) vhif_vrs;
157142215Sglebius	int vhif_nvrs;
158142215Sglebius
159142215Sglebius	struct ifnet 	*vhif_ifp;
160142215Sglebius	struct mtx	 vhif_mtx;
161142215Sglebius};
162142914Sglebius
163142914Sglebius/* Get carp_if from softc. Valid after carp_set_addr{,6}. */
164142914Sglebius#define	SC2CIF(sc)		((struct carp_if *)(sc)->sc_carpdev->if_carp)
165142914Sglebius
166142215Sglebius/* lock per carp_if queue */
167142914Sglebius#define	CARP_LOCK_INIT(cif)	mtx_init(&(cif)->vhif_mtx, "carp_if", 	\
168142215Sglebius	NULL, MTX_DEF)
169142914Sglebius#define	CARP_LOCK_DESTROY(cif)	mtx_destroy(&(cif)->vhif_mtx)
170142215Sglebius#define	CARP_LOCK_ASSERT(cif)	mtx_assert(&(cif)->vhif_mtx, MA_OWNED)
171142215Sglebius#define	CARP_LOCK(cif)		mtx_lock(&(cif)->vhif_mtx)
172142215Sglebius#define	CARP_UNLOCK(cif)	mtx_unlock(&(cif)->vhif_mtx)
173142215Sglebius
174142914Sglebius#define	CARP_SCLOCK(sc)		mtx_lock(&SC2CIF(sc)->vhif_mtx)
175142914Sglebius#define	CARP_SCUNLOCK(sc)	mtx_unlock(&SC2CIF(sc)->vhif_mtx)
176142914Sglebius#define	CARP_SCLOCK_ASSERT(sc)	mtx_assert(&SC2CIF(sc)->vhif_mtx, MA_OWNED)
177142914Sglebius
178142451Sglebius#define	CARP_LOG(...)	do {				\
179142446Sglebius	if (carp_opts[CARPCTL_LOG] > 0)			\
180142446Sglebius		log(LOG_INFO, __VA_ARGS__);		\
181142451Sglebius} while (0)
182142215Sglebius
183142451Sglebius#define	CARP_DEBUG(...)	do {				\
184142446Sglebius	if (carp_opts[CARPCTL_LOG] > 1)			\
185142446Sglebius		log(LOG_DEBUG, __VA_ARGS__);		\
186142451Sglebius} while (0)
187142446Sglebius
188142559Sglebiusstatic void	carp_hmac_prepare(struct carp_softc *);
189142559Sglebiusstatic void	carp_hmac_generate(struct carp_softc *, u_int32_t *,
190142559Sglebius		    unsigned char *);
191142559Sglebiusstatic int	carp_hmac_verify(struct carp_softc *, u_int32_t *,
192142559Sglebius		    unsigned char *);
193142559Sglebiusstatic void	carp_setroute(struct carp_softc *, int);
194142559Sglebiusstatic void	carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
195160195Ssamstatic int 	carp_clone_create(struct if_clone *, int, caddr_t);
196142559Sglebiusstatic void 	carp_clone_destroy(struct ifnet *);
197166228Sglebiusstatic void	carpdetach(struct carp_softc *, int);
198142559Sglebiusstatic int	carp_prepare_ad(struct mbuf *, struct carp_softc *,
199142559Sglebius		    struct carp_header *);
200142559Sglebiusstatic void	carp_send_ad_all(void);
201142559Sglebiusstatic void	carp_send_ad(void *);
202142914Sglebiusstatic void	carp_send_ad_locked(struct carp_softc *);
203142559Sglebiusstatic void	carp_send_arp(struct carp_softc *);
204142559Sglebiusstatic void	carp_master_down(void *);
205142914Sglebiusstatic void	carp_master_down_locked(struct carp_softc *);
206142559Sglebiusstatic int	carp_ioctl(struct ifnet *, u_long, caddr_t);
207142559Sglebiusstatic int	carp_looutput(struct ifnet *, struct mbuf *, struct sockaddr *,
208191148Skmacy    		    struct route *);
209142559Sglebiusstatic void	carp_start(struct ifnet *);
210142559Sglebiusstatic void	carp_setrun(struct carp_softc *, sa_family_t);
211142559Sglebiusstatic void	carp_set_state(struct carp_softc *, int);
212142559Sglebiusstatic int	carp_addrcount(struct carp_if *, struct in_ifaddr *, int);
213142215Sglebiusenum	{ CARP_COUNT_MASTER, CARP_COUNT_RUNNING };
214142215Sglebius
215156947Sglebiusstatic void	carp_multicast_cleanup(struct carp_softc *);
216142559Sglebiusstatic int	carp_set_addr(struct carp_softc *, struct sockaddr_in *);
217142559Sglebiusstatic int	carp_del_addr(struct carp_softc *, struct sockaddr_in *);
218142914Sglebiusstatic void	carp_carpdev_state_locked(struct carp_if *);
219144329Sglebiusstatic void	carp_sc_state_locked(struct carp_softc *);
220142215Sglebius#ifdef INET6
221142559Sglebiusstatic void	carp_send_na(struct carp_softc *);
222142559Sglebiusstatic int	carp_set_addr6(struct carp_softc *, struct sockaddr_in6 *);
223142559Sglebiusstatic int	carp_del_addr6(struct carp_softc *, struct sockaddr_in6 *);
224166423Sglebiusstatic void	carp_multicast6_cleanup(struct carp_softc *);
225142215Sglebius#endif
226142215Sglebius
227142215Sglebiusstatic LIST_HEAD(, carp_softc) carpif_list;
228142911Sglebiusstatic struct mtx carp_mtx;
229142215SglebiusIFC_SIMPLE_DECLARE(carp, 0);
230142215Sglebius
231156947Sglebiusstatic eventhandler_tag if_detach_event_tag;
232156947Sglebius
233142215Sglebiusstatic __inline u_int16_t
234142215Sglebiuscarp_cksum(struct mbuf *m, int len)
235142215Sglebius{
236142215Sglebius	return (in_cksum(m, len));
237142215Sglebius}
238142215Sglebius
239142559Sglebiusstatic void
240142215Sglebiuscarp_hmac_prepare(struct carp_softc *sc)
241142215Sglebius{
242142215Sglebius	u_int8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
243142215Sglebius	u_int8_t vhid = sc->sc_vhid & 0xff;
244142215Sglebius	struct ifaddr *ifa;
245179490Smlaier	int i, found;
246179490Smlaier#ifdef INET
247179490Smlaier	struct in_addr last, cur, in;
248179490Smlaier#endif
249142215Sglebius#ifdef INET6
250179490Smlaier	struct in6_addr last6, cur6, in6;
251142215Sglebius#endif
252142215Sglebius
253142914Sglebius	if (sc->sc_carpdev)
254142914Sglebius		CARP_SCLOCK(sc);
255142914Sglebius
256142914Sglebius	/* XXX: possible race here */
257142914Sglebius
258142215Sglebius	/* compute ipad from key */
259142215Sglebius	bzero(sc->sc_pad, sizeof(sc->sc_pad));
260142215Sglebius	bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
261142215Sglebius	for (i = 0; i < sizeof(sc->sc_pad); i++)
262142215Sglebius		sc->sc_pad[i] ^= 0x36;
263142215Sglebius
264142215Sglebius	/* precompute first part of inner hash */
265142215Sglebius	SHA1Init(&sc->sc_sha1);
266142215Sglebius	SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
267142215Sglebius	SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
268142215Sglebius	SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
269142215Sglebius	SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
270142215Sglebius#ifdef INET
271179490Smlaier	cur.s_addr = 0;
272179490Smlaier	do {
273179490Smlaier		found = 0;
274179490Smlaier		last = cur;
275179490Smlaier		cur.s_addr = 0xffffffff;
276191528Srwatson		IF_ADDR_LOCK(SC2IFP(sc));
277179490Smlaier		TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
278179490Smlaier			in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr;
279179490Smlaier			if (ifa->ifa_addr->sa_family == AF_INET &&
280179490Smlaier			    ntohl(in.s_addr) > ntohl(last.s_addr) &&
281179490Smlaier			    ntohl(in.s_addr) < ntohl(cur.s_addr)) {
282179490Smlaier				cur.s_addr = in.s_addr;
283179490Smlaier				found++;
284179490Smlaier			}
285179490Smlaier		}
286191528Srwatson		IF_ADDR_UNLOCK(SC2IFP(sc));
287179490Smlaier		if (found)
288179490Smlaier			SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur));
289179490Smlaier	} while (found);
290142215Sglebius#endif /* INET */
291142215Sglebius#ifdef INET6
292179490Smlaier	memset(&cur6, 0, sizeof(cur6));
293179490Smlaier	do {
294179490Smlaier		found = 0;
295179490Smlaier		last6 = cur6;
296179490Smlaier		memset(&cur6, 0xff, sizeof(cur6));
297191528Srwatson		IF_ADDR_LOCK(SC2IFP(sc));
298179490Smlaier		TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
299142215Sglebius			in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
300179490Smlaier			if (IN6_IS_SCOPE_EMBED(&in6))
301179490Smlaier				in6.s6_addr16[1] = 0;
302179490Smlaier			if (ifa->ifa_addr->sa_family == AF_INET6 &&
303179490Smlaier			    memcmp(&in6, &last6, sizeof(in6)) > 0 &&
304179490Smlaier			    memcmp(&in6, &cur6, sizeof(in6)) < 0) {
305179490Smlaier				cur6 = in6;
306179490Smlaier				found++;
307179490Smlaier			}
308142215Sglebius		}
309191528Srwatson		IF_ADDR_UNLOCK(SC2IFP(sc));
310179490Smlaier		if (found)
311179490Smlaier			SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6));
312179490Smlaier	} while (found);
313142215Sglebius#endif /* INET6 */
314142215Sglebius
315142215Sglebius	/* convert ipad to opad */
316142215Sglebius	for (i = 0; i < sizeof(sc->sc_pad); i++)
317142215Sglebius		sc->sc_pad[i] ^= 0x36 ^ 0x5c;
318142914Sglebius
319142914Sglebius	if (sc->sc_carpdev)
320142914Sglebius		CARP_SCUNLOCK(sc);
321142215Sglebius}
322142215Sglebius
323142559Sglebiusstatic void
324142215Sglebiuscarp_hmac_generate(struct carp_softc *sc, u_int32_t counter[2],
325142215Sglebius    unsigned char md[20])
326142215Sglebius{
327142215Sglebius	SHA1_CTX sha1ctx;
328142215Sglebius
329142215Sglebius	/* fetch first half of inner hash */
330142215Sglebius	bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
331142215Sglebius
332142215Sglebius	SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
333142215Sglebius	SHA1Final(md, &sha1ctx);
334142215Sglebius
335142215Sglebius	/* outer hash */
336142215Sglebius	SHA1Init(&sha1ctx);
337142215Sglebius	SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
338142215Sglebius	SHA1Update(&sha1ctx, md, 20);
339142215Sglebius	SHA1Final(md, &sha1ctx);
340142215Sglebius}
341142215Sglebius
342142559Sglebiusstatic int
343142215Sglebiuscarp_hmac_verify(struct carp_softc *sc, u_int32_t counter[2],
344142215Sglebius    unsigned char md[20])
345142215Sglebius{
346142215Sglebius	unsigned char md2[20];
347142215Sglebius
348142914Sglebius	CARP_SCLOCK_ASSERT(sc);
349142914Sglebius
350142215Sglebius	carp_hmac_generate(sc, counter, md2);
351142215Sglebius
352142215Sglebius	return (bcmp(md, md2, sizeof(md2)));
353142215Sglebius}
354142215Sglebius
355142559Sglebiusstatic void
356142215Sglebiuscarp_setroute(struct carp_softc *sc, int cmd)
357142215Sglebius{
358142215Sglebius	struct ifaddr *ifa;
359142215Sglebius	int s;
360142215Sglebius
361142914Sglebius	if (sc->sc_carpdev)
362142914Sglebius		CARP_SCLOCK_ASSERT(sc);
363142914Sglebius
364142215Sglebius	s = splnet();
365147256Sbrooks	TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
366142914Sglebius		if (ifa->ifa_addr->sa_family == AF_INET &&
367142914Sglebius		    sc->sc_carpdev != NULL) {
368142215Sglebius			int count = carp_addrcount(
369142564Sglebius			    (struct carp_if *)sc->sc_carpdev->if_carp,
370142215Sglebius			    ifatoia(ifa), CARP_COUNT_MASTER);
371142215Sglebius
372142215Sglebius			if ((cmd == RTM_ADD && count == 1) ||
373142215Sglebius			    (cmd == RTM_DELETE && count == 0))
374142215Sglebius				rtinit(ifa, cmd, RTF_UP | RTF_HOST);
375142215Sglebius		}
376142215Sglebius	}
377142215Sglebius	splx(s);
378142215Sglebius}
379142215Sglebius
380142559Sglebiusstatic int
381160195Ssamcarp_clone_create(struct if_clone *ifc, int unit, caddr_t params)
382142215Sglebius{
383142215Sglebius
384142215Sglebius	struct carp_softc *sc;
385142215Sglebius	struct ifnet *ifp;
386142215Sglebius
387184205Sdes	sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
388147256Sbrooks	ifp = SC2IFP(sc) = if_alloc(IFT_ETHER);
389147256Sbrooks	if (ifp == NULL) {
390184205Sdes		free(sc, M_CARP);
391147256Sbrooks		return (ENOSPC);
392147256Sbrooks	}
393142215Sglebius
394142215Sglebius	sc->sc_flags_backup = 0;
395142215Sglebius	sc->sc_suppress = 0;
396142215Sglebius	sc->sc_advbase = CARP_DFLTINTV;
397142215Sglebius	sc->sc_vhid = -1;	/* required setting */
398142215Sglebius	sc->sc_advskew = 0;
399142215Sglebius	sc->sc_init_counter = 1;
400142215Sglebius	sc->sc_naddrs = sc->sc_naddrs6 = 0; /* M_ZERO? */
401160164Smlaier	sc->sc_imo.imo_membership = (struct in_multi **)malloc(
402160164Smlaier	    (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_CARP,
403160164Smlaier	    M_WAITOK);
404170613Sbms	sc->sc_imo.imo_mfilters = NULL;
405160164Smlaier	sc->sc_imo.imo_max_memberships = IP_MIN_MEMBERSHIPS;
406162627Sbms	sc->sc_imo.imo_multicast_vif = -1;
407191672Sbms#ifdef INET6
408191672Sbms	sc->sc_im6o.im6o_membership = (struct in6_multi **)malloc(
409191672Sbms	    (sizeof(struct in6_multi *) * IPV6_MIN_MEMBERSHIPS), M_CARP,
410191672Sbms	    M_WAITOK);
411191672Sbms	sc->sc_im6o.im6o_mfilters = NULL;
412191672Sbms	sc->sc_im6o.im6o_max_memberships = IPV6_MIN_MEMBERSHIPS;
413191672Sbms	sc->sc_im6o.im6o_multicast_hlim = CARP_DFLTTL;
414191672Sbms#endif
415142215Sglebius
416171637Srwatson	callout_init(&sc->sc_ad_tmo, CALLOUT_MPSAFE);
417171637Srwatson	callout_init(&sc->sc_md_tmo, CALLOUT_MPSAFE);
418171637Srwatson	callout_init(&sc->sc_md6_tmo, CALLOUT_MPSAFE);
419142215Sglebius
420142215Sglebius	ifp->if_softc = sc;
421142215Sglebius	if_initname(ifp, CARP_IFNAME, unit);
422142215Sglebius	ifp->if_mtu = ETHERMTU;
423151688Syar	ifp->if_flags = IFF_LOOPBACK;
424142215Sglebius	ifp->if_ioctl = carp_ioctl;
425142215Sglebius	ifp->if_output = carp_looutput;
426142215Sglebius	ifp->if_start = carp_start;
427142215Sglebius	ifp->if_type = IFT_CARP;
428142215Sglebius	ifp->if_snd.ifq_maxlen = ifqmaxlen;
429142215Sglebius	ifp->if_hdrlen = 0;
430142215Sglebius	if_attach(ifp);
431147256Sbrooks	bpfattach(SC2IFP(sc), DLT_NULL, sizeof(u_int32_t));
432142911Sglebius	mtx_lock(&carp_mtx);
433142215Sglebius	LIST_INSERT_HEAD(&carpif_list, sc, sc_next);
434142911Sglebius	mtx_unlock(&carp_mtx);
435142215Sglebius	return (0);
436142215Sglebius}
437142215Sglebius
438142559Sglebiusstatic void
439142215Sglebiuscarp_clone_destroy(struct ifnet *ifp)
440142215Sglebius{
441142215Sglebius	struct carp_softc *sc = ifp->if_softc;
442156947Sglebius
443156947Sglebius	if (sc->sc_carpdev)
444156947Sglebius		CARP_SCLOCK(sc);
445166228Sglebius	carpdetach(sc, 1);	/* Returns unlocked. */
446156947Sglebius
447156947Sglebius	mtx_lock(&carp_mtx);
448156947Sglebius	LIST_REMOVE(sc, sc_next);
449156947Sglebius	mtx_unlock(&carp_mtx);
450156947Sglebius	bpfdetach(ifp);
451156947Sglebius	if_detach(ifp);
452156947Sglebius	if_free_type(ifp, IFT_ETHER);
453160164Smlaier	free(sc->sc_imo.imo_membership, M_CARP);
454191672Sbms#ifdef INET6
455191672Sbms	free(sc->sc_im6o.im6o_membership, M_CARP);
456191672Sbms#endif
457156947Sglebius	free(sc, M_CARP);
458156947Sglebius}
459156947Sglebius
460166423Sglebius/*
461166423Sglebius * This function can be called on CARP interface destroy path,
462166423Sglebius * and in case of the removal of the underlying interface as
463166423Sglebius * well. We differentiate these two cases. In the latter case
464166423Sglebius * we do not cleanup our multicast memberships, since they
465166423Sglebius * are already freed. Also, in the latter case we do not
466166423Sglebius * release the lock on return, because the function will be
467166423Sglebius * called once more, for another CARP instance on the same
468166423Sglebius * interface.
469166423Sglebius */
470156947Sglebiusstatic void
471166228Sglebiuscarpdetach(struct carp_softc *sc, int unlock)
472156947Sglebius{
473142215Sglebius	struct carp_if *cif;
474146226Sglebius
475156947Sglebius	callout_stop(&sc->sc_ad_tmo);
476156947Sglebius	callout_stop(&sc->sc_md_tmo);
477156947Sglebius	callout_stop(&sc->sc_md6_tmo);
478156947Sglebius
479146226Sglebius	if (sc->sc_suppress)
480146226Sglebius		carp_suppress_preempt--;
481146226Sglebius	sc->sc_suppress = 0;
482146226Sglebius
483156947Sglebius	if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS)
484156947Sglebius		carp_suppress_preempt--;
485156947Sglebius	sc->sc_sendad_errors = 0;
486142215Sglebius
487156947Sglebius	carp_set_state(sc, INIT);
488156947Sglebius	SC2IFP(sc)->if_flags &= ~IFF_UP;
489156947Sglebius	carp_setrun(sc, 0);
490166423Sglebius	if (unlock)
491166423Sglebius		carp_multicast_cleanup(sc);
492166423Sglebius#ifdef INET6
493166423Sglebius	carp_multicast6_cleanup(sc);
494166423Sglebius#endif
495142215Sglebius
496156947Sglebius	if (sc->sc_carpdev != NULL) {
497156947Sglebius		cif = (struct carp_if *)sc->sc_carpdev->if_carp;
498156947Sglebius		CARP_LOCK_ASSERT(cif);
499142215Sglebius		TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
500142215Sglebius		if (!--cif->vhif_nvrs) {
501156947Sglebius			ifpromisc(sc->sc_carpdev, 0);
502142564Sglebius			sc->sc_carpdev->if_carp = NULL;
503142215Sglebius			CARP_LOCK_DESTROY(cif);
504184205Sdes			free(cif, M_IFADDR);
505166228Sglebius		} else if (unlock)
506166228Sglebius			CARP_UNLOCK(cif);
507166228Sglebius		sc->sc_carpdev = NULL;
508142215Sglebius	}
509156947Sglebius}
510142215Sglebius
511156947Sglebius/* Detach an interface from the carp. */
512156947Sglebiusstatic void
513156947Sglebiuscarp_ifdetach(void *arg __unused, struct ifnet *ifp)
514156947Sglebius{
515156947Sglebius	struct carp_if *cif = (struct carp_if *)ifp->if_carp;
516156947Sglebius	struct carp_softc *sc, *nextsc;
517166226Sglebius
518156947Sglebius	if (cif == NULL)
519156947Sglebius		return;
520156947Sglebius
521156947Sglebius	/*
522156947Sglebius	 * XXX: At the end of for() cycle the lock will be destroyed.
523156947Sglebius	 */
524156947Sglebius	CARP_LOCK(cif);
525156947Sglebius	for (sc = TAILQ_FIRST(&cif->vhif_vrs); sc; sc = nextsc) {
526156947Sglebius		nextsc = TAILQ_NEXT(sc, sc_list);
527166228Sglebius		carpdetach(sc, 0);
528156947Sglebius	}
529142215Sglebius}
530142215Sglebius
531142215Sglebius/*
532142215Sglebius * process input packet.
533142215Sglebius * we have rearranged checks order compared to the rfc,
534142215Sglebius * but it seems more efficient this way or not possible otherwise.
535142215Sglebius */
536142215Sglebiusvoid
537142215Sglebiuscarp_input(struct mbuf *m, int hlen)
538142215Sglebius{
539142215Sglebius	struct ip *ip = mtod(m, struct ip *);
540142215Sglebius	struct carp_header *ch;
541142215Sglebius	int iplen, len;
542142215Sglebius
543190968Srwatson	CARPSTATS_INC(carps_ipackets);
544142215Sglebius
545142215Sglebius	if (!carp_opts[CARPCTL_ALLOW]) {
546142215Sglebius		m_freem(m);
547142215Sglebius		return;
548142215Sglebius	}
549142215Sglebius
550142215Sglebius	/* check if received on a valid carp interface */
551142215Sglebius	if (m->m_pkthdr.rcvif->if_carp == NULL) {
552190968Srwatson		CARPSTATS_INC(carps_badif);
553142452Sglebius		CARP_LOG("carp_input: packet received on non-carp "
554142452Sglebius		    "interface: %s\n",
555142446Sglebius		    m->m_pkthdr.rcvif->if_xname);
556142215Sglebius		m_freem(m);
557142215Sglebius		return;
558142215Sglebius	}
559142215Sglebius
560142215Sglebius	/* verify that the IP TTL is 255.  */
561142215Sglebius	if (ip->ip_ttl != CARP_DFLTTL) {
562190968Srwatson		CARPSTATS_INC(carps_badttl);
563142452Sglebius		CARP_LOG("carp_input: received ttl %d != 255i on %s\n",
564142446Sglebius		    ip->ip_ttl,
565142446Sglebius		    m->m_pkthdr.rcvif->if_xname);
566142215Sglebius		m_freem(m);
567142215Sglebius		return;
568142215Sglebius	}
569142215Sglebius
570142215Sglebius	iplen = ip->ip_hl << 2;
571142215Sglebius
572142215Sglebius	if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
573190968Srwatson		CARPSTATS_INC(carps_badlen);
574142446Sglebius		CARP_LOG("carp_input: received len %zd < "
575195976Sdelphij		    "sizeof(struct carp_header) on %s\n",
576195976Sdelphij		    m->m_len - sizeof(struct ip),
577195976Sdelphij		    m->m_pkthdr.rcvif->if_xname);
578142215Sglebius		m_freem(m);
579142215Sglebius		return;
580142215Sglebius	}
581142215Sglebius
582142215Sglebius	if (iplen + sizeof(*ch) < m->m_len) {
583142215Sglebius		if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
584190968Srwatson			CARPSTATS_INC(carps_hdrops);
585142452Sglebius			CARP_LOG("carp_input: pullup failed\n");
586142215Sglebius			return;
587142215Sglebius		}
588142215Sglebius		ip = mtod(m, struct ip *);
589142215Sglebius	}
590142215Sglebius	ch = (struct carp_header *)((char *)ip + iplen);
591142215Sglebius
592142215Sglebius	/*
593142215Sglebius	 * verify that the received packet length is
594142215Sglebius	 * equal to the CARP header
595142215Sglebius	 */
596142215Sglebius	len = iplen + sizeof(*ch);
597142215Sglebius	if (len > m->m_pkthdr.len) {
598190968Srwatson		CARPSTATS_INC(carps_badlen);
599142452Sglebius		CARP_LOG("carp_input: packet too short %d on %s\n",
600142446Sglebius		    m->m_pkthdr.len,
601142446Sglebius		    m->m_pkthdr.rcvif->if_xname);
602142215Sglebius		m_freem(m);
603142215Sglebius		return;
604142215Sglebius	}
605142215Sglebius
606142215Sglebius	if ((m = m_pullup(m, len)) == NULL) {
607190968Srwatson		CARPSTATS_INC(carps_hdrops);
608142215Sglebius		return;
609142215Sglebius	}
610142215Sglebius	ip = mtod(m, struct ip *);
611142215Sglebius	ch = (struct carp_header *)((char *)ip + iplen);
612142215Sglebius
613142215Sglebius	/* verify the CARP checksum */
614142215Sglebius	m->m_data += iplen;
615142215Sglebius	if (carp_cksum(m, len - iplen)) {
616190968Srwatson		CARPSTATS_INC(carps_badsum);
617142452Sglebius		CARP_LOG("carp_input: checksum failed on %s\n",
618142446Sglebius		    m->m_pkthdr.rcvif->if_xname);
619142215Sglebius		m_freem(m);
620142215Sglebius		return;
621142215Sglebius	}
622142215Sglebius	m->m_data -= iplen;
623142215Sglebius
624142446Sglebius	carp_input_c(m, ch, AF_INET);
625142215Sglebius}
626142215Sglebius
627142215Sglebius#ifdef INET6
628142215Sglebiusint
629142215Sglebiuscarp6_input(struct mbuf **mp, int *offp, int proto)
630142215Sglebius{
631142215Sglebius	struct mbuf *m = *mp;
632142215Sglebius	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
633142215Sglebius	struct carp_header *ch;
634142215Sglebius	u_int len;
635142215Sglebius
636190968Srwatson	CARPSTATS_INC(carps_ipackets6);
637142215Sglebius
638142215Sglebius	if (!carp_opts[CARPCTL_ALLOW]) {
639142215Sglebius		m_freem(m);
640142215Sglebius		return (IPPROTO_DONE);
641142215Sglebius	}
642142215Sglebius
643142215Sglebius	/* check if received on a valid carp interface */
644142215Sglebius	if (m->m_pkthdr.rcvif->if_carp == NULL) {
645190968Srwatson		CARPSTATS_INC(carps_badif);
646142446Sglebius		CARP_LOG("carp6_input: packet received on non-carp "
647142452Sglebius		    "interface: %s\n",
648142446Sglebius		    m->m_pkthdr.rcvif->if_xname);
649142215Sglebius		m_freem(m);
650142215Sglebius		return (IPPROTO_DONE);
651142215Sglebius	}
652142215Sglebius
653142215Sglebius	/* verify that the IP TTL is 255 */
654142215Sglebius	if (ip6->ip6_hlim != CARP_DFLTTL) {
655190968Srwatson		CARPSTATS_INC(carps_badttl);
656142452Sglebius		CARP_LOG("carp6_input: received ttl %d != 255 on %s\n",
657142446Sglebius		    ip6->ip6_hlim,
658142446Sglebius		    m->m_pkthdr.rcvif->if_xname);
659142215Sglebius		m_freem(m);
660142215Sglebius		return (IPPROTO_DONE);
661142215Sglebius	}
662142215Sglebius
663142215Sglebius	/* verify that we have a complete carp packet */
664142215Sglebius	len = m->m_len;
665142215Sglebius	IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
666142215Sglebius	if (ch == NULL) {
667190968Srwatson		CARPSTATS_INC(carps_badlen);
668143804Sglebius		CARP_LOG("carp6_input: packet size %u too small\n", len);
669142215Sglebius		return (IPPROTO_DONE);
670142215Sglebius	}
671142215Sglebius
672142215Sglebius
673142215Sglebius	/* verify the CARP checksum */
674142215Sglebius	m->m_data += *offp;
675142215Sglebius	if (carp_cksum(m, sizeof(*ch))) {
676190968Srwatson		CARPSTATS_INC(carps_badsum);
677142452Sglebius		CARP_LOG("carp6_input: checksum failed, on %s\n",
678142446Sglebius		    m->m_pkthdr.rcvif->if_xname);
679142215Sglebius		m_freem(m);
680142215Sglebius		return (IPPROTO_DONE);
681142215Sglebius	}
682142215Sglebius	m->m_data -= *offp;
683142215Sglebius
684142446Sglebius	carp_input_c(m, ch, AF_INET6);
685142215Sglebius	return (IPPROTO_DONE);
686142215Sglebius}
687142215Sglebius#endif /* INET6 */
688142215Sglebius
689142559Sglebiusstatic void
690142446Sglebiuscarp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
691142215Sglebius{
692142215Sglebius	struct ifnet *ifp = m->m_pkthdr.rcvif;
693142446Sglebius	struct carp_softc *sc;
694142215Sglebius	u_int64_t tmp_counter;
695142215Sglebius	struct timeval sc_tv, ch_tv;
696142215Sglebius
697142215Sglebius	/* verify that the VHID is valid on the receiving interface */
698142215Sglebius	CARP_LOCK(ifp->if_carp);
699142215Sglebius	TAILQ_FOREACH(sc, &((struct carp_if *)ifp->if_carp)->vhif_vrs, sc_list)
700142215Sglebius		if (sc->sc_vhid == ch->carp_vhid)
701142215Sglebius			break;
702142914Sglebius
703148887Srwatson	if (!sc || !((SC2IFP(sc)->if_flags & IFF_UP) &&
704148887Srwatson	    (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING))) {
705190968Srwatson		CARPSTATS_INC(carps_badvhid);
706142914Sglebius		CARP_UNLOCK(ifp->if_carp);
707142215Sglebius		m_freem(m);
708142215Sglebius		return;
709142215Sglebius	}
710142215Sglebius
711147256Sbrooks	getmicrotime(&SC2IFP(sc)->if_lastchange);
712147256Sbrooks	SC2IFP(sc)->if_ipackets++;
713147256Sbrooks	SC2IFP(sc)->if_ibytes += m->m_pkthdr.len;
714142215Sglebius
715159180Scsjp	if (bpf_peers_present(SC2IFP(sc)->if_bpf)) {
716142215Sglebius		struct ip *ip = mtod(m, struct ip *);
717142784Sglebius		uint32_t af1 = af;
718142215Sglebius
719142215Sglebius		/* BPF wants net byte order */
720142784Sglebius		ip->ip_len = htons(ip->ip_len + (ip->ip_hl << 2));
721142784Sglebius		ip->ip_off = htons(ip->ip_off);
722147256Sbrooks		bpf_mtap2(SC2IFP(sc)->if_bpf, &af1, sizeof(af1), m);
723142215Sglebius	}
724142215Sglebius
725142215Sglebius	/* verify the CARP version. */
726142215Sglebius	if (ch->carp_version != CARP_VERSION) {
727190968Srwatson		CARPSTATS_INC(carps_badver);
728147256Sbrooks		SC2IFP(sc)->if_ierrors++;
729142914Sglebius		CARP_UNLOCK(ifp->if_carp);
730142452Sglebius		CARP_LOG("%s; invalid version %d\n",
731147256Sbrooks		    SC2IFP(sc)->if_xname,
732142446Sglebius		    ch->carp_version);
733142215Sglebius		m_freem(m);
734142215Sglebius		return;
735142215Sglebius	}
736142215Sglebius
737142215Sglebius	/* verify the hash */
738142215Sglebius	if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
739190968Srwatson		CARPSTATS_INC(carps_badauth);
740147256Sbrooks		SC2IFP(sc)->if_ierrors++;
741142914Sglebius		CARP_UNLOCK(ifp->if_carp);
742147256Sbrooks		CARP_LOG("%s: incorrect hash\n", SC2IFP(sc)->if_xname);
743142215Sglebius		m_freem(m);
744142215Sglebius		return;
745142215Sglebius	}
746142215Sglebius
747142215Sglebius	tmp_counter = ntohl(ch->carp_counter[0]);
748142215Sglebius	tmp_counter = tmp_counter<<32;
749142215Sglebius	tmp_counter += ntohl(ch->carp_counter[1]);
750142215Sglebius
751142215Sglebius	/* XXX Replay protection goes here */
752142215Sglebius
753142215Sglebius	sc->sc_init_counter = 0;
754142215Sglebius	sc->sc_counter = tmp_counter;
755142215Sglebius
756142215Sglebius	sc_tv.tv_sec = sc->sc_advbase;
757142215Sglebius	if (carp_suppress_preempt && sc->sc_advskew <  240)
758142215Sglebius		sc_tv.tv_usec = 240 * 1000000 / 256;
759142215Sglebius	else
760142215Sglebius		sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256;
761142215Sglebius	ch_tv.tv_sec = ch->carp_advbase;
762142215Sglebius	ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
763142215Sglebius
764142215Sglebius	switch (sc->sc_state) {
765142215Sglebius	case INIT:
766142215Sglebius		break;
767142215Sglebius	case MASTER:
768142215Sglebius		/*
769142215Sglebius		 * If we receive an advertisement from a master who's going to
770142215Sglebius		 * be more frequent than us, go into BACKUP state.
771142215Sglebius		 */
772142215Sglebius		if (timevalcmp(&sc_tv, &ch_tv, >) ||
773142215Sglebius		    timevalcmp(&sc_tv, &ch_tv, ==)) {
774142215Sglebius			callout_stop(&sc->sc_ad_tmo);
775142446Sglebius			CARP_DEBUG("%s: MASTER -> BACKUP "
776142452Sglebius			   "(more frequent advertisement received)\n",
777147256Sbrooks			   SC2IFP(sc)->if_xname);
778142215Sglebius			carp_set_state(sc, BACKUP);
779142215Sglebius			carp_setrun(sc, 0);
780142215Sglebius			carp_setroute(sc, RTM_DELETE);
781142215Sglebius		}
782142215Sglebius		break;
783142215Sglebius	case BACKUP:
784142215Sglebius		/*
785142215Sglebius		 * If we're pre-empting masters who advertise slower than us,
786142215Sglebius		 * and this one claims to be slower, treat him as down.
787142215Sglebius		 */
788142215Sglebius		if (carp_opts[CARPCTL_PREEMPT] &&
789142215Sglebius		    timevalcmp(&sc_tv, &ch_tv, <)) {
790142446Sglebius			CARP_DEBUG("%s: BACKUP -> MASTER "
791142452Sglebius			    "(preempting a slower master)\n",
792147256Sbrooks			    SC2IFP(sc)->if_xname);
793142914Sglebius			carp_master_down_locked(sc);
794142215Sglebius			break;
795142215Sglebius		}
796142215Sglebius
797142215Sglebius		/*
798142215Sglebius		 *  If the master is going to advertise at such a low frequency
799142215Sglebius		 *  that he's guaranteed to time out, we'd might as well just
800142215Sglebius		 *  treat him as timed out now.
801142215Sglebius		 */
802142215Sglebius		sc_tv.tv_sec = sc->sc_advbase * 3;
803142215Sglebius		if (timevalcmp(&sc_tv, &ch_tv, <)) {
804142446Sglebius			CARP_DEBUG("%s: BACKUP -> MASTER "
805142452Sglebius			    "(master timed out)\n",
806147256Sbrooks			    SC2IFP(sc)->if_xname);
807142914Sglebius			carp_master_down_locked(sc);
808142215Sglebius			break;
809142215Sglebius		}
810142215Sglebius
811142215Sglebius		/*
812142215Sglebius		 * Otherwise, we reset the counter and wait for the next
813142215Sglebius		 * advertisement.
814142215Sglebius		 */
815142215Sglebius		carp_setrun(sc, af);
816142215Sglebius		break;
817142215Sglebius	}
818142215Sglebius
819142914Sglebius	CARP_UNLOCK(ifp->if_carp);
820142914Sglebius
821142215Sglebius	m_freem(m);
822142215Sglebius	return;
823142215Sglebius}
824142215Sglebius
825142559Sglebiusstatic int
826142215Sglebiuscarp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
827142215Sglebius{
828142215Sglebius	struct m_tag *mtag;
829147256Sbrooks	struct ifnet *ifp = SC2IFP(sc);
830142215Sglebius
831142215Sglebius	if (sc->sc_init_counter) {
832142215Sglebius		/* this could also be seconds since unix epoch */
833142215Sglebius		sc->sc_counter = arc4random();
834142215Sglebius		sc->sc_counter = sc->sc_counter << 32;
835142215Sglebius		sc->sc_counter += arc4random();
836142215Sglebius	} else
837142215Sglebius		sc->sc_counter++;
838142215Sglebius
839142215Sglebius	ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
840142215Sglebius	ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
841142215Sglebius
842142215Sglebius	carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
843142215Sglebius
844142215Sglebius	/* Tag packet for carp_output */
845142215Sglebius	mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct ifnet *), M_NOWAIT);
846142215Sglebius	if (mtag == NULL) {
847142215Sglebius		m_freem(m);
848147256Sbrooks		SC2IFP(sc)->if_oerrors++;
849142215Sglebius		return (ENOMEM);
850142215Sglebius	}
851142215Sglebius	bcopy(&ifp, (caddr_t)(mtag + 1), sizeof(struct ifnet *));
852142215Sglebius	m_tag_prepend(m, mtag);
853142215Sglebius
854142215Sglebius	return (0);
855142215Sglebius}
856142215Sglebius
857142559Sglebiusstatic void
858142215Sglebiuscarp_send_ad_all(void)
859142215Sglebius{
860142911Sglebius	struct carp_softc *sc;
861142215Sglebius
862142911Sglebius	mtx_lock(&carp_mtx);
863142911Sglebius	LIST_FOREACH(sc, &carpif_list, sc_next) {
864142911Sglebius		if (sc->sc_carpdev == NULL)
865142215Sglebius			continue;
866142911Sglebius		CARP_SCLOCK(sc);
867148887Srwatson		if ((SC2IFP(sc)->if_flags & IFF_UP) &&
868148887Srwatson		    (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING) &&
869142911Sglebius		     sc->sc_state == MASTER)
870142914Sglebius			carp_send_ad_locked(sc);
871142911Sglebius		CARP_SCUNLOCK(sc);
872142215Sglebius	}
873142911Sglebius	mtx_unlock(&carp_mtx);
874142215Sglebius}
875142215Sglebius
876142559Sglebiusstatic void
877142215Sglebiuscarp_send_ad(void *v)
878142215Sglebius{
879142914Sglebius	struct carp_softc *sc = v;
880142914Sglebius
881142914Sglebius	CARP_SCLOCK(sc);
882142914Sglebius	carp_send_ad_locked(sc);
883142914Sglebius	CARP_SCUNLOCK(sc);
884142914Sglebius}
885142914Sglebius
886142914Sglebiusstatic void
887142914Sglebiuscarp_send_ad_locked(struct carp_softc *sc)
888142914Sglebius{
889142215Sglebius	struct carp_header ch;
890142215Sglebius	struct timeval tv;
891142215Sglebius	struct carp_header *ch_ptr;
892142215Sglebius	struct mbuf *m;
893142215Sglebius	int len, advbase, advskew;
894142215Sglebius
895142914Sglebius	CARP_SCLOCK_ASSERT(sc);
896142914Sglebius
897142215Sglebius	/* bow out if we've lost our UPness or RUNNINGuiness */
898148887Srwatson	if (!((SC2IFP(sc)->if_flags & IFF_UP) &&
899148887Srwatson	    (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING))) {
900142215Sglebius		advbase = 255;
901142215Sglebius		advskew = 255;
902142215Sglebius	} else {
903142215Sglebius		advbase = sc->sc_advbase;
904142215Sglebius		if (!carp_suppress_preempt || sc->sc_advskew > 240)
905142215Sglebius			advskew = sc->sc_advskew;
906142215Sglebius		else
907142215Sglebius			advskew = 240;
908142215Sglebius		tv.tv_sec = advbase;
909142215Sglebius		tv.tv_usec = advskew * 1000000 / 256;
910142215Sglebius	}
911142215Sglebius
912142215Sglebius	ch.carp_version = CARP_VERSION;
913142215Sglebius	ch.carp_type = CARP_ADVERTISEMENT;
914142215Sglebius	ch.carp_vhid = sc->sc_vhid;
915142215Sglebius	ch.carp_advbase = advbase;
916142215Sglebius	ch.carp_advskew = advskew;
917142215Sglebius	ch.carp_authlen = 7;	/* XXX DEFINE */
918142215Sglebius	ch.carp_pad1 = 0;	/* must be zero */
919142215Sglebius	ch.carp_cksum = 0;
920142215Sglebius
921142215Sglebius#ifdef INET
922142215Sglebius	if (sc->sc_ia) {
923142215Sglebius		struct ip *ip;
924142215Sglebius
925142215Sglebius		MGETHDR(m, M_DONTWAIT, MT_HEADER);
926142215Sglebius		if (m == NULL) {
927147256Sbrooks			SC2IFP(sc)->if_oerrors++;
928190968Srwatson			CARPSTATS_INC(carps_onomem);
929142215Sglebius			/* XXX maybe less ? */
930142215Sglebius			if (advbase != 255 || advskew != 255)
931142215Sglebius				callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
932142215Sglebius				    carp_send_ad, sc);
933142215Sglebius			return;
934142215Sglebius		}
935142215Sglebius		len = sizeof(*ip) + sizeof(ch);
936142215Sglebius		m->m_pkthdr.len = len;
937142215Sglebius		m->m_pkthdr.rcvif = NULL;
938142215Sglebius		m->m_len = len;
939142215Sglebius		MH_ALIGN(m, m->m_len);
940142215Sglebius		m->m_flags |= M_MCAST;
941142215Sglebius		ip = mtod(m, struct ip *);
942142215Sglebius		ip->ip_v = IPVERSION;
943142215Sglebius		ip->ip_hl = sizeof(*ip) >> 2;
944142215Sglebius		ip->ip_tos = IPTOS_LOWDELAY;
945142215Sglebius		ip->ip_len = len;
946142215Sglebius		ip->ip_id = ip_newid();
947142215Sglebius		ip->ip_off = IP_DF;
948142215Sglebius		ip->ip_ttl = CARP_DFLTTL;
949142215Sglebius		ip->ip_p = IPPROTO_CARP;
950142215Sglebius		ip->ip_sum = 0;
951142215Sglebius		ip->ip_src.s_addr = sc->sc_ia->ia_addr.sin_addr.s_addr;
952142215Sglebius		ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
953142215Sglebius
954142215Sglebius		ch_ptr = (struct carp_header *)(&ip[1]);
955142215Sglebius		bcopy(&ch, ch_ptr, sizeof(ch));
956142215Sglebius		if (carp_prepare_ad(m, sc, ch_ptr))
957142215Sglebius			return;
958142215Sglebius
959142215Sglebius		m->m_data += sizeof(*ip);
960142215Sglebius		ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip));
961142215Sglebius		m->m_data -= sizeof(*ip);
962142215Sglebius
963147256Sbrooks		getmicrotime(&SC2IFP(sc)->if_lastchange);
964147256Sbrooks		SC2IFP(sc)->if_opackets++;
965147256Sbrooks		SC2IFP(sc)->if_obytes += len;
966190968Srwatson		CARPSTATS_INC(carps_opackets);
967142215Sglebius
968142215Sglebius		if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) {
969147256Sbrooks			SC2IFP(sc)->if_oerrors++;
970142215Sglebius			if (sc->sc_sendad_errors < INT_MAX)
971142215Sglebius				sc->sc_sendad_errors++;
972142215Sglebius			if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
973142215Sglebius				carp_suppress_preempt++;
974142914Sglebius				if (carp_suppress_preempt == 1) {
975142914Sglebius					CARP_SCUNLOCK(sc);
976142215Sglebius					carp_send_ad_all();
977142914Sglebius					CARP_SCLOCK(sc);
978142914Sglebius				}
979142215Sglebius			}
980142215Sglebius			sc->sc_sendad_success = 0;
981142215Sglebius		} else {
982142215Sglebius			if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
983142215Sglebius				if (++sc->sc_sendad_success >=
984142215Sglebius				    CARP_SENDAD_MIN_SUCCESS) {
985142215Sglebius					carp_suppress_preempt--;
986142215Sglebius					sc->sc_sendad_errors = 0;
987142215Sglebius				}
988142215Sglebius			} else
989142215Sglebius				sc->sc_sendad_errors = 0;
990142215Sglebius		}
991142215Sglebius	}
992142215Sglebius#endif /* INET */
993142215Sglebius#ifdef INET6
994142215Sglebius	if (sc->sc_ia6) {
995142215Sglebius		struct ip6_hdr *ip6;
996142215Sglebius
997142215Sglebius		MGETHDR(m, M_DONTWAIT, MT_HEADER);
998142215Sglebius		if (m == NULL) {
999147256Sbrooks			SC2IFP(sc)->if_oerrors++;
1000190968Srwatson			CARPSTATS_INC(carps_onomem);
1001142215Sglebius			/* XXX maybe less ? */
1002142215Sglebius			if (advbase != 255 || advskew != 255)
1003142215Sglebius				callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1004142215Sglebius				    carp_send_ad, sc);
1005142215Sglebius			return;
1006142215Sglebius		}
1007142215Sglebius		len = sizeof(*ip6) + sizeof(ch);
1008142215Sglebius		m->m_pkthdr.len = len;
1009142215Sglebius		m->m_pkthdr.rcvif = NULL;
1010142215Sglebius		m->m_len = len;
1011142215Sglebius		MH_ALIGN(m, m->m_len);
1012142215Sglebius		m->m_flags |= M_MCAST;
1013142215Sglebius		ip6 = mtod(m, struct ip6_hdr *);
1014142215Sglebius		bzero(ip6, sizeof(*ip6));
1015142215Sglebius		ip6->ip6_vfc |= IPV6_VERSION;
1016142215Sglebius		ip6->ip6_hlim = CARP_DFLTTL;
1017142215Sglebius		ip6->ip6_nxt = IPPROTO_CARP;
1018142215Sglebius		bcopy(&sc->sc_ia6->ia_addr.sin6_addr, &ip6->ip6_src,
1019142215Sglebius		    sizeof(struct in6_addr));
1020142215Sglebius		/* set the multicast destination */
1021142215Sglebius
1022163069Sbz		ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
1023142215Sglebius		ip6->ip6_dst.s6_addr8[15] = 0x12;
1024163069Sbz		if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
1025163069Sbz			SC2IFP(sc)->if_oerrors++;
1026163069Sbz			m_freem(m);
1027163069Sbz			CARP_LOG("%s: in6_setscope failed\n", __func__);
1028163069Sbz			return;
1029163069Sbz		}
1030142215Sglebius
1031142215Sglebius		ch_ptr = (struct carp_header *)(&ip6[1]);
1032142215Sglebius		bcopy(&ch, ch_ptr, sizeof(ch));
1033142215Sglebius		if (carp_prepare_ad(m, sc, ch_ptr))
1034142215Sglebius			return;
1035142215Sglebius
1036142215Sglebius		m->m_data += sizeof(*ip6);
1037142215Sglebius		ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip6));
1038142215Sglebius		m->m_data -= sizeof(*ip6);
1039142215Sglebius
1040147256Sbrooks		getmicrotime(&SC2IFP(sc)->if_lastchange);
1041147256Sbrooks		SC2IFP(sc)->if_opackets++;
1042147256Sbrooks		SC2IFP(sc)->if_obytes += len;
1043190968Srwatson		CARPSTATS_INC(carps_opackets6);
1044142215Sglebius
1045142215Sglebius		if (ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL)) {
1046147256Sbrooks			SC2IFP(sc)->if_oerrors++;
1047142215Sglebius			if (sc->sc_sendad_errors < INT_MAX)
1048142215Sglebius				sc->sc_sendad_errors++;
1049142215Sglebius			if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
1050142215Sglebius				carp_suppress_preempt++;
1051142914Sglebius				if (carp_suppress_preempt == 1) {
1052142914Sglebius					CARP_SCUNLOCK(sc);
1053142215Sglebius					carp_send_ad_all();
1054142914Sglebius					CARP_SCLOCK(sc);
1055142914Sglebius				}
1056142215Sglebius			}
1057142215Sglebius			sc->sc_sendad_success = 0;
1058142215Sglebius		} else {
1059142215Sglebius			if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
1060142215Sglebius				if (++sc->sc_sendad_success >=
1061142215Sglebius				    CARP_SENDAD_MIN_SUCCESS) {
1062142215Sglebius					carp_suppress_preempt--;
1063142215Sglebius					sc->sc_sendad_errors = 0;
1064142215Sglebius				}
1065142215Sglebius			} else
1066142215Sglebius				sc->sc_sendad_errors = 0;
1067142215Sglebius		}
1068142215Sglebius	}
1069142215Sglebius#endif /* INET6 */
1070142215Sglebius
1071142215Sglebius	if (advbase != 255 || advskew != 255)
1072142215Sglebius		callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1073142215Sglebius		    carp_send_ad, sc);
1074142215Sglebius
1075142215Sglebius}
1076142215Sglebius
1077142215Sglebius/*
1078142215Sglebius * Broadcast a gratuitous ARP request containing
1079142215Sglebius * the virtual router MAC address for each IP address
1080142215Sglebius * associated with the virtual router.
1081142215Sglebius */
1082142559Sglebiusstatic void
1083142215Sglebiuscarp_send_arp(struct carp_softc *sc)
1084142215Sglebius{
1085142215Sglebius	struct ifaddr *ifa;
1086142215Sglebius
1087147256Sbrooks	TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1088142215Sglebius
1089142215Sglebius		if (ifa->ifa_addr->sa_family != AF_INET)
1090142215Sglebius			continue;
1091142215Sglebius
1092152315Sru/*		arprequest(sc->sc_carpdev, &in, &in, IF_LLADDR(sc->sc_ifp)); */
1093152315Sru		arp_ifinit2(sc->sc_carpdev, ifa, IF_LLADDR(sc->sc_ifp));
1094142215Sglebius
1095142215Sglebius		DELAY(1000);	/* XXX */
1096142215Sglebius	}
1097142215Sglebius}
1098142215Sglebius
1099142215Sglebius#ifdef INET6
1100142559Sglebiusstatic void
1101142215Sglebiuscarp_send_na(struct carp_softc *sc)
1102142215Sglebius{
1103142215Sglebius	struct ifaddr *ifa;
1104142215Sglebius	struct in6_addr *in6;
1105142215Sglebius	static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1106142215Sglebius
1107147256Sbrooks	TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1108142215Sglebius
1109142215Sglebius		if (ifa->ifa_addr->sa_family != AF_INET6)
1110142215Sglebius			continue;
1111142215Sglebius
1112142215Sglebius		in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
1113142564Sglebius		nd6_na_output(sc->sc_carpdev, &mcast, in6,
1114142215Sglebius		    ND_NA_FLAG_OVERRIDE, 1, NULL);
1115142215Sglebius		DELAY(1000);	/* XXX */
1116142215Sglebius	}
1117142215Sglebius}
1118142215Sglebius#endif /* INET6 */
1119142215Sglebius
1120142559Sglebiusstatic int
1121142215Sglebiuscarp_addrcount(struct carp_if *cif, struct in_ifaddr *ia, int type)
1122142215Sglebius{
1123142215Sglebius	struct carp_softc *vh;
1124142215Sglebius	struct ifaddr *ifa;
1125142215Sglebius	int count = 0;
1126142215Sglebius
1127142914Sglebius	CARP_LOCK_ASSERT(cif);
1128142914Sglebius
1129142215Sglebius	TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1130142215Sglebius		if ((type == CARP_COUNT_RUNNING &&
1131148887Srwatson		    (SC2IFP(vh)->if_flags & IFF_UP) &&
1132148887Srwatson		    (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING)) ||
1133142215Sglebius		    (type == CARP_COUNT_MASTER && vh->sc_state == MASTER)) {
1134191528Srwatson			IF_ADDR_LOCK(SC2IFP(vh));
1135147256Sbrooks			TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist,
1136142215Sglebius			    ifa_list) {
1137142215Sglebius				if (ifa->ifa_addr->sa_family == AF_INET &&
1138142215Sglebius				    ia->ia_addr.sin_addr.s_addr ==
1139142215Sglebius				    ifatoia(ifa)->ia_addr.sin_addr.s_addr)
1140142215Sglebius					count++;
1141142215Sglebius			}
1142191528Srwatson			IF_ADDR_UNLOCK(SC2IFP(vh));
1143142215Sglebius		}
1144142215Sglebius	}
1145142215Sglebius	return (count);
1146142215Sglebius}
1147142215Sglebius
1148142215Sglebiusint
1149142215Sglebiuscarp_iamatch(void *v, struct in_ifaddr *ia,
1150142215Sglebius    struct in_addr *isaddr, u_int8_t **enaddr)
1151142215Sglebius{
1152142215Sglebius	struct carp_if *cif = v;
1153142215Sglebius	struct carp_softc *vh;
1154142215Sglebius	int index, count = 0;
1155142215Sglebius	struct ifaddr *ifa;
1156142215Sglebius
1157142215Sglebius	CARP_LOCK(cif);
1158142215Sglebius
1159142215Sglebius	if (carp_opts[CARPCTL_ARPBALANCE]) {
1160142215Sglebius		/*
1161142215Sglebius		 * XXX proof of concept implementation.
1162142215Sglebius		 * We use the source ip to decide which virtual host should
1163142215Sglebius		 * handle the request. If we're master of that virtual host,
1164142215Sglebius		 * then we respond, otherwise, just drop the arp packet on
1165142215Sglebius		 * the floor.
1166142215Sglebius		 */
1167142215Sglebius		count = carp_addrcount(cif, ia, CARP_COUNT_RUNNING);
1168142215Sglebius		if (count == 0) {
1169142215Sglebius			/* should never reach this */
1170142215Sglebius			CARP_UNLOCK(cif);
1171142215Sglebius			return (0);
1172142215Sglebius		}
1173142215Sglebius
1174142215Sglebius		/* this should be a hash, like pf_hash() */
1175147718Sglebius		index = ntohl(isaddr->s_addr) % count;
1176142215Sglebius		count = 0;
1177142215Sglebius
1178142215Sglebius		TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1179148887Srwatson			if ((SC2IFP(vh)->if_flags & IFF_UP) &&
1180148887Srwatson			    (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING)) {
1181191528Srwatson				IF_ADDR_LOCK(SC2IFP(vh));
1182147256Sbrooks				TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist,
1183142215Sglebius				    ifa_list) {
1184142215Sglebius					if (ifa->ifa_addr->sa_family ==
1185142215Sglebius					    AF_INET &&
1186142215Sglebius					    ia->ia_addr.sin_addr.s_addr ==
1187142215Sglebius					    ifatoia(ifa)->ia_addr.sin_addr.s_addr) {
1188142215Sglebius						if (count == index) {
1189142215Sglebius							if (vh->sc_state ==
1190142215Sglebius							    MASTER) {
1191152315Sru								*enaddr = IF_LLADDR(vh->sc_ifp);
1192191528Srwatson								IF_ADDR_UNLOCK(SC2IFP(vh));
1193142215Sglebius								CARP_UNLOCK(cif);
1194142215Sglebius								return (1);
1195142215Sglebius							} else {
1196191528Srwatson								IF_ADDR_UNLOCK(SC2IFP(vh));
1197142215Sglebius								CARP_UNLOCK(cif);
1198142215Sglebius								return (0);
1199142215Sglebius							}
1200142215Sglebius						}
1201142215Sglebius						count++;
1202142215Sglebius					}
1203142215Sglebius				}
1204191528Srwatson				IF_ADDR_UNLOCK(SC2IFP(vh));
1205142215Sglebius			}
1206142215Sglebius		}
1207142215Sglebius	} else {
1208142215Sglebius		TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1209148887Srwatson			if ((SC2IFP(vh)->if_flags & IFF_UP) &&
1210148887Srwatson			    (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
1211152550Sglebius			    ia->ia_ifp == SC2IFP(vh) &&
1212152550Sglebius			    vh->sc_state == MASTER) {
1213152315Sru				*enaddr = IF_LLADDR(vh->sc_ifp);
1214142215Sglebius				CARP_UNLOCK(cif);
1215142215Sglebius				return (1);
1216142215Sglebius			}
1217142215Sglebius		}
1218142215Sglebius	}
1219142215Sglebius	CARP_UNLOCK(cif);
1220142215Sglebius	return (0);
1221142215Sglebius}
1222142215Sglebius
1223142215Sglebius#ifdef INET6
1224142641Smlaierstruct ifaddr *
1225142215Sglebiuscarp_iamatch6(void *v, struct in6_addr *taddr)
1226142215Sglebius{
1227142215Sglebius	struct carp_if *cif = v;
1228142215Sglebius	struct carp_softc *vh;
1229142215Sglebius	struct ifaddr *ifa;
1230142215Sglebius
1231142215Sglebius	CARP_LOCK(cif);
1232142215Sglebius	TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1233191528Srwatson		IF_ADDR_LOCK(SC2IFP(vh));
1234147256Sbrooks		TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist, ifa_list) {
1235142215Sglebius			if (IN6_ARE_ADDR_EQUAL(taddr,
1236142215Sglebius			    &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1237148887Srwatson 			    (SC2IFP(vh)->if_flags & IFF_UP) &&
1238152550Sglebius			    (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
1239152550Sglebius			    vh->sc_state == MASTER) {
1240194760Srwatson				ifa_ref(ifa);
1241191528Srwatson				IF_ADDR_UNLOCK(SC2IFP(vh));
1242142215Sglebius			    	CARP_UNLOCK(cif);
1243142215Sglebius				return (ifa);
1244142215Sglebius			}
1245142215Sglebius		}
1246191528Srwatson		IF_ADDR_UNLOCK(SC2IFP(vh));
1247142215Sglebius	}
1248142215Sglebius	CARP_UNLOCK(cif);
1249142215Sglebius
1250142215Sglebius	return (NULL);
1251142215Sglebius}
1252142215Sglebius
1253142641Smlaiervoid *
1254142215Sglebiuscarp_macmatch6(void *v, struct mbuf *m, const struct in6_addr *taddr)
1255142215Sglebius{
1256142215Sglebius	struct m_tag *mtag;
1257142215Sglebius	struct carp_if *cif = v;
1258142215Sglebius	struct carp_softc *sc;
1259142215Sglebius	struct ifaddr *ifa;
1260142215Sglebius
1261142215Sglebius	CARP_LOCK(cif);
1262142215Sglebius	TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
1263191528Srwatson		IF_ADDR_LOCK(SC2IFP(sc));
1264147256Sbrooks		TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1265142215Sglebius			if (IN6_ARE_ADDR_EQUAL(taddr,
1266142215Sglebius			    &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1267148887Srwatson 			    (SC2IFP(sc)->if_flags & IFF_UP) &&
1268148887Srwatson			    (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING)) {
1269147256Sbrooks				struct ifnet *ifp = SC2IFP(sc);
1270142215Sglebius				mtag = m_tag_get(PACKET_TAG_CARP,
1271142215Sglebius				    sizeof(struct ifnet *), M_NOWAIT);
1272142215Sglebius				if (mtag == NULL) {
1273142215Sglebius					/* better a bit than nothing */
1274191528Srwatson					IF_ADDR_UNLOCK(SC2IFP(sc));
1275142215Sglebius					CARP_UNLOCK(cif);
1276152315Sru					return (IF_LLADDR(sc->sc_ifp));
1277142215Sglebius				}
1278142215Sglebius				bcopy(&ifp, (caddr_t)(mtag + 1),
1279142215Sglebius				    sizeof(struct ifnet *));
1280142215Sglebius				m_tag_prepend(m, mtag);
1281142215Sglebius
1282191528Srwatson				IF_ADDR_UNLOCK(SC2IFP(sc));
1283142215Sglebius				CARP_UNLOCK(cif);
1284152315Sru				return (IF_LLADDR(sc->sc_ifp));
1285142215Sglebius			}
1286142215Sglebius		}
1287191528Srwatson		IF_ADDR_UNLOCK(SC2IFP(sc));
1288142215Sglebius	}
1289142215Sglebius	CARP_UNLOCK(cif);
1290142215Sglebius
1291142215Sglebius	return (NULL);
1292142215Sglebius}
1293142215Sglebius#endif
1294142215Sglebius
1295142215Sglebiusstruct ifnet *
1296142215Sglebiuscarp_forus(void *v, void *dhost)
1297142215Sglebius{
1298142215Sglebius	struct carp_if *cif = v;
1299142215Sglebius	struct carp_softc *vh;
1300142215Sglebius	u_int8_t *ena = dhost;
1301142215Sglebius
1302142215Sglebius	if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1303142215Sglebius		return (NULL);
1304142215Sglebius
1305142215Sglebius	CARP_LOCK(cif);
1306142215Sglebius	TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list)
1307148887Srwatson		if ((SC2IFP(vh)->if_flags & IFF_UP) &&
1308148887Srwatson		    (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
1309148887Srwatson		    vh->sc_state == MASTER &&
1310152315Sru		    !bcmp(dhost, IF_LLADDR(vh->sc_ifp), ETHER_ADDR_LEN)) {
1311142215Sglebius		    	CARP_UNLOCK(cif);
1312147256Sbrooks			return (SC2IFP(vh));
1313142215Sglebius		}
1314142215Sglebius
1315142215Sglebius    	CARP_UNLOCK(cif);
1316142215Sglebius	return (NULL);
1317142215Sglebius}
1318142215Sglebius
1319142559Sglebiusstatic void
1320142215Sglebiuscarp_master_down(void *v)
1321142215Sglebius{
1322142215Sglebius	struct carp_softc *sc = v;
1323142215Sglebius
1324142914Sglebius	CARP_SCLOCK(sc);
1325142914Sglebius	carp_master_down_locked(sc);
1326142914Sglebius	CARP_SCUNLOCK(sc);
1327142914Sglebius}
1328142914Sglebius
1329142914Sglebiusstatic void
1330142914Sglebiuscarp_master_down_locked(struct carp_softc *sc)
1331142914Sglebius{
1332142914Sglebius	if (sc->sc_carpdev)
1333142914Sglebius		CARP_SCLOCK_ASSERT(sc);
1334142914Sglebius
1335142215Sglebius	switch (sc->sc_state) {
1336142215Sglebius	case INIT:
1337142215Sglebius		printf("%s: master_down event in INIT state\n",
1338147256Sbrooks		    SC2IFP(sc)->if_xname);
1339142215Sglebius		break;
1340142215Sglebius	case MASTER:
1341142215Sglebius		break;
1342142215Sglebius	case BACKUP:
1343142215Sglebius		carp_set_state(sc, MASTER);
1344142914Sglebius		carp_send_ad_locked(sc);
1345142215Sglebius		carp_send_arp(sc);
1346142215Sglebius#ifdef INET6
1347142215Sglebius		carp_send_na(sc);
1348142215Sglebius#endif /* INET6 */
1349142215Sglebius		carp_setrun(sc, 0);
1350142215Sglebius		carp_setroute(sc, RTM_ADD);
1351142215Sglebius		break;
1352142215Sglebius	}
1353142215Sglebius}
1354142215Sglebius
1355142215Sglebius/*
1356142215Sglebius * When in backup state, af indicates whether to reset the master down timer
1357142215Sglebius * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1358142215Sglebius */
1359142559Sglebiusstatic void
1360142215Sglebiuscarp_setrun(struct carp_softc *sc, sa_family_t af)
1361142215Sglebius{
1362142215Sglebius	struct timeval tv;
1363142215Sglebius
1364156947Sglebius	if (sc->sc_carpdev == NULL) {
1365156947Sglebius		SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1366156947Sglebius		carp_set_state(sc, INIT);
1367156947Sglebius		return;
1368156947Sglebius	} else
1369142914Sglebius		CARP_SCLOCK_ASSERT(sc);
1370142914Sglebius
1371147256Sbrooks	if (SC2IFP(sc)->if_flags & IFF_UP &&
1372142215Sglebius	    sc->sc_vhid > 0 && (sc->sc_naddrs || sc->sc_naddrs6))
1373148887Srwatson		SC2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
1374142215Sglebius	else {
1375148887Srwatson		SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1376142215Sglebius		carp_setroute(sc, RTM_DELETE);
1377142215Sglebius		return;
1378142215Sglebius	}
1379142215Sglebius
1380142215Sglebius	switch (sc->sc_state) {
1381142215Sglebius	case INIT:
1382142215Sglebius		if (carp_opts[CARPCTL_PREEMPT] && !carp_suppress_preempt) {
1383142914Sglebius			carp_send_ad_locked(sc);
1384142215Sglebius			carp_send_arp(sc);
1385142215Sglebius#ifdef INET6
1386142215Sglebius			carp_send_na(sc);
1387142215Sglebius#endif /* INET6 */
1388142452Sglebius			CARP_DEBUG("%s: INIT -> MASTER (preempting)\n",
1389147256Sbrooks			    SC2IFP(sc)->if_xname);
1390142215Sglebius			carp_set_state(sc, MASTER);
1391142215Sglebius			carp_setroute(sc, RTM_ADD);
1392142215Sglebius		} else {
1393147256Sbrooks			CARP_DEBUG("%s: INIT -> BACKUP\n", SC2IFP(sc)->if_xname);
1394142215Sglebius			carp_set_state(sc, BACKUP);
1395142215Sglebius			carp_setroute(sc, RTM_DELETE);
1396142215Sglebius			carp_setrun(sc, 0);
1397142215Sglebius		}
1398142215Sglebius		break;
1399142215Sglebius	case BACKUP:
1400142215Sglebius		callout_stop(&sc->sc_ad_tmo);
1401142215Sglebius		tv.tv_sec = 3 * sc->sc_advbase;
1402142215Sglebius		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1403142215Sglebius		switch (af) {
1404142215Sglebius#ifdef INET
1405142215Sglebius		case AF_INET:
1406142215Sglebius			callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1407142215Sglebius			    carp_master_down, sc);
1408142215Sglebius			break;
1409142215Sglebius#endif /* INET */
1410142215Sglebius#ifdef INET6
1411142215Sglebius		case AF_INET6:
1412142215Sglebius			callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1413142215Sglebius			    carp_master_down, sc);
1414142215Sglebius			break;
1415142215Sglebius#endif /* INET6 */
1416142215Sglebius		default:
1417142215Sglebius			if (sc->sc_naddrs)
1418142215Sglebius				callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1419142215Sglebius				    carp_master_down, sc);
1420142215Sglebius			if (sc->sc_naddrs6)
1421142215Sglebius				callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1422142215Sglebius				    carp_master_down, sc);
1423142215Sglebius			break;
1424142215Sglebius		}
1425142215Sglebius		break;
1426142215Sglebius	case MASTER:
1427142215Sglebius		tv.tv_sec = sc->sc_advbase;
1428142215Sglebius		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1429142215Sglebius		callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1430142215Sglebius		    carp_send_ad, sc);
1431142215Sglebius		break;
1432142215Sglebius	}
1433142215Sglebius}
1434142215Sglebius
1435166423Sglebiusstatic void
1436156947Sglebiuscarp_multicast_cleanup(struct carp_softc *sc)
1437156947Sglebius{
1438156947Sglebius	struct ip_moptions *imo = &sc->sc_imo;
1439156947Sglebius	u_int16_t n = imo->imo_num_memberships;
1440166226Sglebius
1441156947Sglebius	/* Clean up our own multicast memberships */
1442156947Sglebius	while (n-- > 0) {
1443156947Sglebius		if (imo->imo_membership[n] != NULL) {
1444156947Sglebius			in_delmulti(imo->imo_membership[n]);
1445156947Sglebius			imo->imo_membership[n] = NULL;
1446156947Sglebius		}
1447156947Sglebius	}
1448170613Sbms	KASSERT(imo->imo_mfilters == NULL,
1449170613Sbms	   ("%s: imo_mfilters != NULL", __func__));
1450156947Sglebius	imo->imo_num_memberships = 0;
1451156947Sglebius	imo->imo_multicast_ifp = NULL;
1452166423Sglebius}
1453156947Sglebius
1454156947Sglebius#ifdef INET6
1455166423Sglebiusstatic void
1456166423Sglebiuscarp_multicast6_cleanup(struct carp_softc *sc)
1457166423Sglebius{
1458166423Sglebius	struct ip6_moptions *im6o = &sc->sc_im6o;
1459191672Sbms	u_int16_t n = im6o->im6o_num_memberships;
1460166423Sglebius
1461191672Sbms	while (n-- > 0) {
1462191672Sbms		if (im6o->im6o_membership[n] != NULL) {
1463191672Sbms			in6_mc_leave(im6o->im6o_membership[n], NULL);
1464191672Sbms			im6o->im6o_membership[n] = NULL;
1465191672Sbms		}
1466156947Sglebius	}
1467191672Sbms	KASSERT(im6o->im6o_mfilters == NULL,
1468191672Sbms	   ("%s: im6o_mfilters != NULL", __func__));
1469191672Sbms	im6o->im6o_num_memberships = 0;
1470156947Sglebius	im6o->im6o_multicast_ifp = NULL;
1471166423Sglebius}
1472156947Sglebius#endif
1473156947Sglebius
1474142559Sglebiusstatic int
1475142215Sglebiuscarp_set_addr(struct carp_softc *sc, struct sockaddr_in *sin)
1476142215Sglebius{
1477142215Sglebius	struct ifnet *ifp;
1478142215Sglebius	struct carp_if *cif;
1479142215Sglebius	struct in_ifaddr *ia, *ia_if;
1480142215Sglebius	struct ip_moptions *imo = &sc->sc_imo;
1481142215Sglebius	struct in_addr addr;
1482142215Sglebius	u_long iaddr = htonl(sin->sin_addr.s_addr);
1483142215Sglebius	int own, error;
1484142215Sglebius
1485142215Sglebius	if (sin->sin_addr.s_addr == 0) {
1486147256Sbrooks		if (!(SC2IFP(sc)->if_flags & IFF_UP))
1487142215Sglebius			carp_set_state(sc, INIT);
1488142215Sglebius		if (sc->sc_naddrs)
1489147256Sbrooks			SC2IFP(sc)->if_flags |= IFF_UP;
1490180513Seri		if (sc->sc_carpdev)
1491180513Seri			CARP_SCLOCK(sc);
1492142215Sglebius		carp_setrun(sc, 0);
1493180513Seri		if (sc->sc_carpdev)
1494180513Seri			CARP_SCUNLOCK(sc);
1495142215Sglebius		return (0);
1496142215Sglebius	}
1497142215Sglebius
1498142215Sglebius	/* we have to do it by hands to check we won't match on us */
1499142215Sglebius	ia_if = NULL; own = 0;
1500194951Srwatson	IN_IFADDR_RLOCK();
1501181803Sbz	TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1502142215Sglebius		/* and, yeah, we need a multicast-capable iface too */
1503147256Sbrooks		if (ia->ia_ifp != SC2IFP(sc) &&
1504142215Sglebius		    (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1505142215Sglebius		    (iaddr & ia->ia_subnetmask) == ia->ia_subnet) {
1506142215Sglebius			if (!ia_if)
1507142215Sglebius				ia_if = ia;
1508142215Sglebius			if (sin->sin_addr.s_addr ==
1509142215Sglebius			    ia->ia_addr.sin_addr.s_addr)
1510142215Sglebius				own++;
1511142215Sglebius		}
1512142215Sglebius	}
1513142215Sglebius
1514194951Srwatson	if (!ia_if) {
1515194951Srwatson		IN_IFADDR_RUNLOCK();
1516142215Sglebius		return (EADDRNOTAVAIL);
1517194951Srwatson	}
1518142215Sglebius
1519142215Sglebius	ia = ia_if;
1520194951Srwatson	ifa_ref(&ia->ia_ifa);
1521194951Srwatson	IN_IFADDR_RUNLOCK();
1522194951Srwatson
1523142215Sglebius	ifp = ia->ia_ifp;
1524142215Sglebius
1525142215Sglebius	if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1526194951Srwatson	    (imo->imo_multicast_ifp && imo->imo_multicast_ifp != ifp)) {
1527194951Srwatson		ifa_free(&ia->ia_ifa);
1528142215Sglebius		return (EADDRNOTAVAIL);
1529194951Srwatson	}
1530142215Sglebius
1531142215Sglebius	if (imo->imo_num_memberships == 0) {
1532142215Sglebius		addr.s_addr = htonl(INADDR_CARP_GROUP);
1533194951Srwatson		if ((imo->imo_membership[0] = in_addmulti(&addr, ifp)) ==
1534194951Srwatson		    NULL) {
1535194951Srwatson			ifa_free(&ia->ia_ifa);
1536142215Sglebius			return (ENOBUFS);
1537194951Srwatson		}
1538142215Sglebius		imo->imo_num_memberships++;
1539142215Sglebius		imo->imo_multicast_ifp = ifp;
1540142215Sglebius		imo->imo_multicast_ttl = CARP_DFLTTL;
1541142215Sglebius		imo->imo_multicast_loop = 0;
1542142215Sglebius	}
1543142215Sglebius
1544142215Sglebius	if (!ifp->if_carp) {
1545142215Sglebius
1546184205Sdes		cif = malloc(sizeof(*cif), M_CARP,
1547142215Sglebius		    M_WAITOK|M_ZERO);
1548142215Sglebius		if (!cif) {
1549142215Sglebius			error = ENOBUFS;
1550142215Sglebius			goto cleanup;
1551142215Sglebius		}
1552142215Sglebius		if ((error = ifpromisc(ifp, 1))) {
1553184205Sdes			free(cif, M_CARP);
1554142215Sglebius			goto cleanup;
1555142215Sglebius		}
1556142215Sglebius
1557142215Sglebius		CARP_LOCK_INIT(cif);
1558142215Sglebius		CARP_LOCK(cif);
1559142215Sglebius		cif->vhif_ifp = ifp;
1560142215Sglebius		TAILQ_INIT(&cif->vhif_vrs);
1561142215Sglebius		ifp->if_carp = cif;
1562142215Sglebius
1563142215Sglebius	} else {
1564142215Sglebius		struct carp_softc *vr;
1565142215Sglebius
1566142215Sglebius		cif = (struct carp_if *)ifp->if_carp;
1567142215Sglebius		CARP_LOCK(cif);
1568142215Sglebius		TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1569142215Sglebius			if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1570142215Sglebius				CARP_UNLOCK(cif);
1571176093Sglebius				error = EEXIST;
1572142215Sglebius				goto cleanup;
1573142215Sglebius			}
1574142215Sglebius	}
1575142215Sglebius	sc->sc_ia = ia;
1576142564Sglebius	sc->sc_carpdev = ifp;
1577142215Sglebius
1578142215Sglebius	{ /* XXX prevent endless loop if already in queue */
1579142215Sglebius	struct carp_softc *vr, *after = NULL;
1580142215Sglebius	int myself = 0;
1581142215Sglebius	cif = (struct carp_if *)ifp->if_carp;
1582142215Sglebius
1583142215Sglebius	/* XXX: cif should not change, right? So we still hold the lock */
1584142215Sglebius	CARP_LOCK_ASSERT(cif);
1585142215Sglebius
1586142215Sglebius	TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1587142215Sglebius		if (vr == sc)
1588142215Sglebius			myself = 1;
1589142215Sglebius		if (vr->sc_vhid < sc->sc_vhid)
1590142215Sglebius			after = vr;
1591142215Sglebius	}
1592142215Sglebius
1593142215Sglebius	if (!myself) {
1594142215Sglebius		/* We're trying to keep things in order */
1595142215Sglebius		if (after == NULL) {
1596142215Sglebius			TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1597142215Sglebius		} else {
1598142215Sglebius			TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1599142215Sglebius		}
1600142215Sglebius		cif->vhif_nvrs++;
1601142215Sglebius	}
1602142215Sglebius	}
1603142215Sglebius
1604142215Sglebius	sc->sc_naddrs++;
1605147256Sbrooks	SC2IFP(sc)->if_flags |= IFF_UP;
1606142215Sglebius	if (own)
1607142215Sglebius		sc->sc_advskew = 0;
1608144329Sglebius	carp_sc_state_locked(sc);
1609142215Sglebius	carp_setrun(sc, 0);
1610142215Sglebius
1611142914Sglebius	CARP_UNLOCK(cif);
1612194951Srwatson	ifa_free(&ia->ia_ifa);	/* XXXRW: should hold reference for softc. */
1613142914Sglebius
1614142215Sglebius	return (0);
1615142215Sglebius
1616142215Sglebiuscleanup:
1617142215Sglebius	in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1618194951Srwatson	ifa_free(&ia->ia_ifa);
1619142215Sglebius	return (error);
1620142215Sglebius}
1621142215Sglebius
1622142559Sglebiusstatic int
1623142215Sglebiuscarp_del_addr(struct carp_softc *sc, struct sockaddr_in *sin)
1624142215Sglebius{
1625142215Sglebius	int error = 0;
1626142215Sglebius
1627142215Sglebius	if (!--sc->sc_naddrs) {
1628142564Sglebius		struct carp_if *cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1629142215Sglebius		struct ip_moptions *imo = &sc->sc_imo;
1630142215Sglebius
1631142914Sglebius		CARP_LOCK(cif);
1632142215Sglebius		callout_stop(&sc->sc_ad_tmo);
1633148887Srwatson		SC2IFP(sc)->if_flags &= ~IFF_UP;
1634148887Srwatson		SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1635142215Sglebius		sc->sc_vhid = -1;
1636142215Sglebius		in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1637142215Sglebius		imo->imo_multicast_ifp = NULL;
1638142215Sglebius		TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1639142215Sglebius		if (!--cif->vhif_nvrs) {
1640142564Sglebius			sc->sc_carpdev->if_carp = NULL;
1641142215Sglebius			CARP_LOCK_DESTROY(cif);
1642184205Sdes			free(cif, M_IFADDR);
1643142215Sglebius		} else {
1644142215Sglebius			CARP_UNLOCK(cif);
1645142215Sglebius		}
1646142215Sglebius	}
1647142215Sglebius
1648142215Sglebius	return (error);
1649142215Sglebius}
1650142215Sglebius
1651142215Sglebius#ifdef INET6
1652142559Sglebiusstatic int
1653142215Sglebiuscarp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1654142215Sglebius{
1655142215Sglebius	struct ifnet *ifp;
1656142215Sglebius	struct carp_if *cif;
1657142215Sglebius	struct in6_ifaddr *ia, *ia_if;
1658142215Sglebius	struct ip6_moptions *im6o = &sc->sc_im6o;
1659148385Sume	struct in6_addr in6;
1660142215Sglebius	int own, error;
1661142215Sglebius
1662191672Sbms	error = 0;
1663191672Sbms
1664142215Sglebius	if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1665147256Sbrooks		if (!(SC2IFP(sc)->if_flags & IFF_UP))
1666142215Sglebius			carp_set_state(sc, INIT);
1667142215Sglebius		if (sc->sc_naddrs6)
1668147256Sbrooks			SC2IFP(sc)->if_flags |= IFF_UP;
1669180513Seri		if (sc->sc_carpdev)
1670180513Seri			CARP_SCLOCK(sc);
1671142215Sglebius		carp_setrun(sc, 0);
1672180513Seri		if (sc->sc_carpdev)
1673180513Seri			CARP_SCUNLOCK(sc);
1674142215Sglebius		return (0);
1675142215Sglebius	}
1676142215Sglebius
1677142215Sglebius	/* we have to do it by hands to check we won't match on us */
1678142215Sglebius	ia_if = NULL; own = 0;
1679194971Srwatson	IN6_IFADDR_RLOCK();
1680194912Srwatson	TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
1681142215Sglebius		int i;
1682142215Sglebius
1683142215Sglebius		for (i = 0; i < 4; i++) {
1684142215Sglebius			if ((sin6->sin6_addr.s6_addr32[i] &
1685142215Sglebius			    ia->ia_prefixmask.sin6_addr.s6_addr32[i]) !=
1686142215Sglebius			    (ia->ia_addr.sin6_addr.s6_addr32[i] &
1687142215Sglebius			    ia->ia_prefixmask.sin6_addr.s6_addr32[i]))
1688142215Sglebius				break;
1689142215Sglebius		}
1690142215Sglebius		/* and, yeah, we need a multicast-capable iface too */
1691147256Sbrooks		if (ia->ia_ifp != SC2IFP(sc) &&
1692142215Sglebius		    (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1693142215Sglebius		    (i == 4)) {
1694142215Sglebius			if (!ia_if)
1695142215Sglebius				ia_if = ia;
1696142215Sglebius			if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
1697142215Sglebius			    &ia->ia_addr.sin6_addr))
1698142215Sglebius				own++;
1699142215Sglebius		}
1700142215Sglebius	}
1701142215Sglebius
1702194971Srwatson	if (!ia_if) {
1703194971Srwatson		IN6_IFADDR_RUNLOCK();
1704142215Sglebius		return (EADDRNOTAVAIL);
1705194971Srwatson	}
1706142215Sglebius	ia = ia_if;
1707194971Srwatson	ifa_ref(&ia->ia_ifa);
1708194971Srwatson	IN6_IFADDR_RUNLOCK();
1709142215Sglebius	ifp = ia->ia_ifp;
1710142215Sglebius
1711142215Sglebius	if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1712194971Srwatson	    (im6o->im6o_multicast_ifp && im6o->im6o_multicast_ifp != ifp)) {
1713194971Srwatson		ifa_free(&ia->ia_ifa);
1714142215Sglebius		return (EADDRNOTAVAIL);
1715194971Srwatson	}
1716142215Sglebius
1717142215Sglebius	if (!sc->sc_naddrs6) {
1718191672Sbms		struct in6_multi *in6m;
1719191672Sbms
1720142215Sglebius		im6o->im6o_multicast_ifp = ifp;
1721142215Sglebius
1722142215Sglebius		/* join CARP multicast address */
1723148385Sume		bzero(&in6, sizeof(in6));
1724148385Sume		in6.s6_addr16[0] = htons(0xff02);
1725148385Sume		in6.s6_addr8[15] = 0x12;
1726148385Sume		if (in6_setscope(&in6, ifp, NULL) != 0)
1727142215Sglebius			goto cleanup;
1728191672Sbms		in6m = NULL;
1729191672Sbms		error = in6_mc_join(ifp, &in6, NULL, &in6m, 0);
1730191672Sbms		if (error)
1731148385Sume			goto cleanup;
1732191672Sbms		im6o->im6o_membership[0] = in6m;
1733191672Sbms		im6o->im6o_num_memberships++;
1734142215Sglebius
1735142215Sglebius		/* join solicited multicast address */
1736148385Sume		bzero(&in6, sizeof(in6));
1737148385Sume		in6.s6_addr16[0] = htons(0xff02);
1738148385Sume		in6.s6_addr32[1] = 0;
1739148385Sume		in6.s6_addr32[2] = htonl(1);
1740148385Sume		in6.s6_addr32[3] = sin6->sin6_addr.s6_addr32[3];
1741148385Sume		in6.s6_addr8[12] = 0xff;
1742148385Sume		if (in6_setscope(&in6, ifp, NULL) != 0)
1743142215Sglebius			goto cleanup;
1744191672Sbms		in6m = NULL;
1745191672Sbms		error = in6_mc_join(ifp, &in6, NULL, &in6m, 0);
1746191672Sbms		if (error)
1747148385Sume			goto cleanup;
1748191672Sbms		im6o->im6o_membership[1] = in6m;
1749191672Sbms		im6o->im6o_num_memberships++;
1750142215Sglebius	}
1751142215Sglebius
1752142215Sglebius	if (!ifp->if_carp) {
1753184205Sdes		cif = malloc(sizeof(*cif), M_CARP,
1754142215Sglebius		    M_WAITOK|M_ZERO);
1755142215Sglebius		if (!cif) {
1756142215Sglebius			error = ENOBUFS;
1757142215Sglebius			goto cleanup;
1758142215Sglebius		}
1759142215Sglebius		if ((error = ifpromisc(ifp, 1))) {
1760184205Sdes			free(cif, M_CARP);
1761142215Sglebius			goto cleanup;
1762142215Sglebius		}
1763142215Sglebius
1764142215Sglebius		CARP_LOCK_INIT(cif);
1765142215Sglebius		CARP_LOCK(cif);
1766142215Sglebius		cif->vhif_ifp = ifp;
1767142215Sglebius		TAILQ_INIT(&cif->vhif_vrs);
1768142215Sglebius		ifp->if_carp = cif;
1769142215Sglebius
1770142215Sglebius	} else {
1771142215Sglebius		struct carp_softc *vr;
1772142215Sglebius
1773142215Sglebius		cif = (struct carp_if *)ifp->if_carp;
1774142215Sglebius		CARP_LOCK(cif);
1775142215Sglebius		TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1776142215Sglebius			if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1777142215Sglebius				CARP_UNLOCK(cif);
1778142215Sglebius				error = EINVAL;
1779142215Sglebius				goto cleanup;
1780142215Sglebius			}
1781142215Sglebius	}
1782142215Sglebius	sc->sc_ia6 = ia;
1783142564Sglebius	sc->sc_carpdev = ifp;
1784142215Sglebius
1785142215Sglebius	{ /* XXX prevent endless loop if already in queue */
1786142215Sglebius	struct carp_softc *vr, *after = NULL;
1787142215Sglebius	int myself = 0;
1788142215Sglebius	cif = (struct carp_if *)ifp->if_carp;
1789142215Sglebius	CARP_LOCK_ASSERT(cif);
1790142215Sglebius
1791142215Sglebius	TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1792142215Sglebius		if (vr == sc)
1793142215Sglebius			myself = 1;
1794142215Sglebius		if (vr->sc_vhid < sc->sc_vhid)
1795142215Sglebius			after = vr;
1796142215Sglebius	}
1797142215Sglebius
1798142215Sglebius	if (!myself) {
1799142215Sglebius		/* We're trying to keep things in order */
1800142215Sglebius		if (after == NULL) {
1801142215Sglebius			TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1802142215Sglebius		} else {
1803142215Sglebius			TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1804142215Sglebius		}
1805142215Sglebius		cif->vhif_nvrs++;
1806142215Sglebius	}
1807142215Sglebius	}
1808142215Sglebius
1809142215Sglebius	sc->sc_naddrs6++;
1810147256Sbrooks	SC2IFP(sc)->if_flags |= IFF_UP;
1811142215Sglebius	if (own)
1812142215Sglebius		sc->sc_advskew = 0;
1813144329Sglebius	carp_sc_state_locked(sc);
1814142215Sglebius	carp_setrun(sc, 0);
1815142215Sglebius
1816142914Sglebius	CARP_UNLOCK(cif);
1817194971Srwatson	ifa_free(&ia->ia_ifa);	/* XXXRW: should hold reference for softc. */
1818142914Sglebius
1819142215Sglebius	return (0);
1820142215Sglebius
1821142215Sglebiuscleanup:
1822191672Sbms	if (!sc->sc_naddrs6)
1823191672Sbms		carp_multicast6_cleanup(sc);
1824194971Srwatson	ifa_free(&ia->ia_ifa);
1825142215Sglebius	return (error);
1826142215Sglebius}
1827142215Sglebius
1828142559Sglebiusstatic int
1829142215Sglebiuscarp_del_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1830142215Sglebius{
1831142215Sglebius	int error = 0;
1832142215Sglebius
1833142215Sglebius	if (!--sc->sc_naddrs6) {
1834142564Sglebius		struct carp_if *cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1835142215Sglebius
1836142914Sglebius		CARP_LOCK(cif);
1837142215Sglebius		callout_stop(&sc->sc_ad_tmo);
1838148887Srwatson		SC2IFP(sc)->if_flags &= ~IFF_UP;
1839148887Srwatson		SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1840142215Sglebius		sc->sc_vhid = -1;
1841191672Sbms		carp_multicast6_cleanup(sc);
1842142215Sglebius		TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1843142215Sglebius		if (!--cif->vhif_nvrs) {
1844142215Sglebius			CARP_LOCK_DESTROY(cif);
1845142564Sglebius			sc->sc_carpdev->if_carp = NULL;
1846184205Sdes			free(cif, M_IFADDR);
1847142215Sglebius		} else
1848142215Sglebius			CARP_UNLOCK(cif);
1849142215Sglebius	}
1850142215Sglebius
1851142215Sglebius	return (error);
1852142215Sglebius}
1853142215Sglebius#endif /* INET6 */
1854142215Sglebius
1855142559Sglebiusstatic int
1856142215Sglebiuscarp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr)
1857142215Sglebius{
1858142215Sglebius	struct carp_softc *sc = ifp->if_softc, *vr;
1859142215Sglebius	struct carpreq carpr;
1860142215Sglebius	struct ifaddr *ifa;
1861142215Sglebius	struct ifreq *ifr;
1862142215Sglebius	struct ifaliasreq *ifra;
1863142914Sglebius	int locked = 0, error = 0;
1864142215Sglebius
1865142215Sglebius	ifa = (struct ifaddr *)addr;
1866142215Sglebius	ifra = (struct ifaliasreq *)addr;
1867142215Sglebius	ifr = (struct ifreq *)addr;
1868142215Sglebius
1869142215Sglebius	switch (cmd) {
1870142215Sglebius	case SIOCSIFADDR:
1871142215Sglebius		switch (ifa->ifa_addr->sa_family) {
1872142215Sglebius#ifdef INET
1873142215Sglebius		case AF_INET:
1874147256Sbrooks			SC2IFP(sc)->if_flags |= IFF_UP;
1875142215Sglebius			bcopy(ifa->ifa_addr, ifa->ifa_dstaddr,
1876142215Sglebius			    sizeof(struct sockaddr));
1877142215Sglebius			error = carp_set_addr(sc, satosin(ifa->ifa_addr));
1878142215Sglebius			break;
1879142215Sglebius#endif /* INET */
1880142215Sglebius#ifdef INET6
1881142215Sglebius		case AF_INET6:
1882147256Sbrooks			SC2IFP(sc)->if_flags |= IFF_UP;
1883142215Sglebius			error = carp_set_addr6(sc, satosin6(ifa->ifa_addr));
1884142215Sglebius			break;
1885142215Sglebius#endif /* INET6 */
1886142215Sglebius		default:
1887142215Sglebius			error = EAFNOSUPPORT;
1888142215Sglebius			break;
1889142215Sglebius		}
1890142215Sglebius		break;
1891142215Sglebius
1892142215Sglebius	case SIOCAIFADDR:
1893142215Sglebius		switch (ifa->ifa_addr->sa_family) {
1894142215Sglebius#ifdef INET
1895142215Sglebius		case AF_INET:
1896147256Sbrooks			SC2IFP(sc)->if_flags |= IFF_UP;
1897142215Sglebius			bcopy(ifa->ifa_addr, ifa->ifa_dstaddr,
1898142215Sglebius			    sizeof(struct sockaddr));
1899142215Sglebius			error = carp_set_addr(sc, satosin(&ifra->ifra_addr));
1900142215Sglebius			break;
1901142215Sglebius#endif /* INET */
1902142215Sglebius#ifdef INET6
1903142215Sglebius		case AF_INET6:
1904147256Sbrooks			SC2IFP(sc)->if_flags |= IFF_UP;
1905142215Sglebius			error = carp_set_addr6(sc, satosin6(&ifra->ifra_addr));
1906142215Sglebius			break;
1907142215Sglebius#endif /* INET6 */
1908142215Sglebius		default:
1909142215Sglebius			error = EAFNOSUPPORT;
1910142215Sglebius			break;
1911142215Sglebius		}
1912142215Sglebius		break;
1913142215Sglebius
1914142215Sglebius	case SIOCDIFADDR:
1915142215Sglebius		switch (ifa->ifa_addr->sa_family) {
1916142215Sglebius#ifdef INET
1917142215Sglebius		case AF_INET:
1918142215Sglebius			error = carp_del_addr(sc, satosin(&ifra->ifra_addr));
1919142215Sglebius			break;
1920142215Sglebius#endif /* INET */
1921142215Sglebius#ifdef INET6
1922142215Sglebius		case AF_INET6:
1923142215Sglebius			error = carp_del_addr6(sc, satosin6(&ifra->ifra_addr));
1924142215Sglebius			break;
1925142215Sglebius#endif /* INET6 */
1926142215Sglebius		default:
1927142215Sglebius			error = EAFNOSUPPORT;
1928142215Sglebius			break;
1929142215Sglebius		}
1930142215Sglebius		break;
1931142215Sglebius
1932142215Sglebius	case SIOCSIFFLAGS:
1933142914Sglebius		if (sc->sc_carpdev) {
1934142914Sglebius			locked = 1;
1935142914Sglebius			CARP_SCLOCK(sc);
1936142914Sglebius		}
1937142215Sglebius		if (sc->sc_state != INIT && !(ifr->ifr_flags & IFF_UP)) {
1938142215Sglebius 			callout_stop(&sc->sc_ad_tmo);
1939142215Sglebius 			callout_stop(&sc->sc_md_tmo);
1940142215Sglebius 			callout_stop(&sc->sc_md6_tmo);
1941142215Sglebius			if (sc->sc_state == MASTER)
1942142914Sglebius				carp_send_ad_locked(sc);
1943142215Sglebius			carp_set_state(sc, INIT);
1944142215Sglebius			carp_setrun(sc, 0);
1945142215Sglebius		} else if (sc->sc_state == INIT && (ifr->ifr_flags & IFF_UP)) {
1946147256Sbrooks			SC2IFP(sc)->if_flags |= IFF_UP;
1947142215Sglebius			carp_setrun(sc, 0);
1948142215Sglebius		}
1949142215Sglebius		break;
1950142215Sglebius
1951142215Sglebius	case SIOCSVH:
1952164033Srwatson		error = priv_check(curthread, PRIV_NETINET_CARP);
1953164033Srwatson		if (error)
1954142215Sglebius			break;
1955142215Sglebius		if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
1956142215Sglebius			break;
1957142215Sglebius		error = 1;
1958142914Sglebius		if (sc->sc_carpdev) {
1959142914Sglebius			locked = 1;
1960142914Sglebius			CARP_SCLOCK(sc);
1961142914Sglebius		}
1962142215Sglebius		if (sc->sc_state != INIT && carpr.carpr_state != sc->sc_state) {
1963142215Sglebius			switch (carpr.carpr_state) {
1964142215Sglebius			case BACKUP:
1965142215Sglebius				callout_stop(&sc->sc_ad_tmo);
1966142215Sglebius				carp_set_state(sc, BACKUP);
1967142215Sglebius				carp_setrun(sc, 0);
1968142215Sglebius				carp_setroute(sc, RTM_DELETE);
1969142215Sglebius				break;
1970142215Sglebius			case MASTER:
1971142914Sglebius				carp_master_down_locked(sc);
1972142215Sglebius				break;
1973142215Sglebius			default:
1974142215Sglebius				break;
1975142215Sglebius			}
1976142215Sglebius		}
1977142215Sglebius		if (carpr.carpr_vhid > 0) {
1978142215Sglebius			if (carpr.carpr_vhid > 255) {
1979142215Sglebius				error = EINVAL;
1980142215Sglebius				break;
1981142215Sglebius			}
1982142564Sglebius			if (sc->sc_carpdev) {
1983142215Sglebius				struct carp_if *cif;
1984142564Sglebius				cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1985142215Sglebius				TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1986142215Sglebius					if (vr != sc &&
1987170373Sglebius					    vr->sc_vhid == carpr.carpr_vhid) {
1988170373Sglebius						error = EEXIST;
1989170373Sglebius						break;
1990170373Sglebius					}
1991170373Sglebius				if (error == EEXIST)
1992170373Sglebius					break;
1993142215Sglebius			}
1994142215Sglebius			sc->sc_vhid = carpr.carpr_vhid;
1995152315Sru			IF_LLADDR(sc->sc_ifp)[0] = 0;
1996152315Sru			IF_LLADDR(sc->sc_ifp)[1] = 0;
1997152315Sru			IF_LLADDR(sc->sc_ifp)[2] = 0x5e;
1998152315Sru			IF_LLADDR(sc->sc_ifp)[3] = 0;
1999152315Sru			IF_LLADDR(sc->sc_ifp)[4] = 1;
2000152315Sru			IF_LLADDR(sc->sc_ifp)[5] = sc->sc_vhid;
2001142215Sglebius			error--;
2002142215Sglebius		}
2003142215Sglebius		if (carpr.carpr_advbase > 0 || carpr.carpr_advskew > 0) {
2004142215Sglebius			if (carpr.carpr_advskew >= 255) {
2005142215Sglebius				error = EINVAL;
2006142215Sglebius				break;
2007142215Sglebius			}
2008142215Sglebius			if (carpr.carpr_advbase > 255) {
2009142215Sglebius				error = EINVAL;
2010142215Sglebius				break;
2011142215Sglebius			}
2012142215Sglebius			sc->sc_advbase = carpr.carpr_advbase;
2013142215Sglebius			sc->sc_advskew = carpr.carpr_advskew;
2014142215Sglebius			error--;
2015142215Sglebius		}
2016142215Sglebius		bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
2017142215Sglebius		if (error > 0)
2018142215Sglebius			error = EINVAL;
2019142215Sglebius		else {
2020142215Sglebius			error = 0;
2021142215Sglebius			carp_setrun(sc, 0);
2022142215Sglebius		}
2023142215Sglebius		break;
2024142215Sglebius
2025142215Sglebius	case SIOCGVH:
2026142914Sglebius		/* XXX: lockless read */
2027142215Sglebius		bzero(&carpr, sizeof(carpr));
2028142215Sglebius		carpr.carpr_state = sc->sc_state;
2029142215Sglebius		carpr.carpr_vhid = sc->sc_vhid;
2030142215Sglebius		carpr.carpr_advbase = sc->sc_advbase;
2031142215Sglebius		carpr.carpr_advskew = sc->sc_advskew;
2032164033Srwatson		error = priv_check(curthread, PRIV_NETINET_CARP);
2033164033Srwatson		if (error == 0)
2034142215Sglebius			bcopy(sc->sc_key, carpr.carpr_key,
2035142215Sglebius			    sizeof(carpr.carpr_key));
2036142215Sglebius		error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
2037142215Sglebius		break;
2038142215Sglebius
2039142215Sglebius	default:
2040142215Sglebius		error = EINVAL;
2041142215Sglebius	}
2042142215Sglebius
2043142914Sglebius	if (locked)
2044142914Sglebius		CARP_SCUNLOCK(sc);
2045142914Sglebius
2046142215Sglebius	carp_hmac_prepare(sc);
2047142914Sglebius
2048142215Sglebius	return (error);
2049142215Sglebius}
2050142215Sglebius
2051142215Sglebius/*
2052142215Sglebius * XXX: this is looutput. We should eventually use it from there.
2053142215Sglebius */
2054142215Sglebiusstatic int
2055142215Sglebiuscarp_looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
2056191148Skmacy    struct route *ro)
2057142215Sglebius{
2058147611Sdwmalone	u_int32_t af;
2059191148Skmacy	struct rtentry *rt = NULL;
2060147611Sdwmalone
2061142215Sglebius	M_ASSERTPKTHDR(m); /* check if we have the packet header */
2062142215Sglebius
2063191148Skmacy	if (ro != NULL)
2064191148Skmacy		rt = ro->ro_rt;
2065142215Sglebius	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
2066142215Sglebius		m_freem(m);
2067142215Sglebius		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
2068142215Sglebius			rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
2069142215Sglebius	}
2070142215Sglebius
2071142215Sglebius	ifp->if_opackets++;
2072142215Sglebius	ifp->if_obytes += m->m_pkthdr.len;
2073147611Sdwmalone
2074147611Sdwmalone	/* BPF writes need to be handled specially. */
2075147611Sdwmalone	if (dst->sa_family == AF_UNSPEC) {
2076147611Sdwmalone		bcopy(dst->sa_data, &af, sizeof(af));
2077147611Sdwmalone		dst->sa_family = af;
2078147611Sdwmalone	}
2079147611Sdwmalone
2080142215Sglebius#if 1	/* XXX */
2081142215Sglebius	switch (dst->sa_family) {
2082142215Sglebius	case AF_INET:
2083142215Sglebius	case AF_INET6:
2084142215Sglebius	case AF_IPX:
2085142215Sglebius	case AF_APPLETALK:
2086142215Sglebius		break;
2087142215Sglebius	default:
2088142215Sglebius		printf("carp_looutput: af=%d unexpected\n", dst->sa_family);
2089142215Sglebius		m_freem(m);
2090142215Sglebius		return (EAFNOSUPPORT);
2091142215Sglebius	}
2092142215Sglebius#endif
2093142215Sglebius	return(if_simloop(ifp, m, dst->sa_family, 0));
2094142215Sglebius}
2095142215Sglebius
2096142215Sglebius/*
2097142215Sglebius * Start output on carp interface. This function should never be called.
2098142215Sglebius */
2099142559Sglebiusstatic void
2100142215Sglebiuscarp_start(struct ifnet *ifp)
2101142215Sglebius{
2102142215Sglebius#ifdef DEBUG
2103142215Sglebius	printf("%s: start called\n", ifp->if_xname);
2104142215Sglebius#endif
2105142215Sglebius}
2106142215Sglebius
2107142215Sglebiusint
2108142215Sglebiuscarp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
2109142215Sglebius    struct rtentry *rt)
2110142215Sglebius{
2111142215Sglebius	struct m_tag *mtag;
2112142215Sglebius	struct carp_softc *sc;
2113142215Sglebius	struct ifnet *carp_ifp;
2114142215Sglebius
2115142215Sglebius	if (!sa)
2116142215Sglebius		return (0);
2117142215Sglebius
2118142215Sglebius	switch (sa->sa_family) {
2119142215Sglebius#ifdef INET
2120142215Sglebius	case AF_INET:
2121142215Sglebius		break;
2122142215Sglebius#endif /* INET */
2123142215Sglebius#ifdef INET6
2124142215Sglebius	case AF_INET6:
2125142215Sglebius		break;
2126142215Sglebius#endif /* INET6 */
2127142215Sglebius	default:
2128142215Sglebius		return (0);
2129142215Sglebius	}
2130142215Sglebius
2131142215Sglebius	mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
2132142215Sglebius	if (mtag == NULL)
2133142215Sglebius		return (0);
2134142215Sglebius
2135142215Sglebius	bcopy(mtag + 1, &carp_ifp, sizeof(struct ifnet *));
2136142215Sglebius	sc = carp_ifp->if_softc;
2137142215Sglebius
2138142215Sglebius	/* Set the source MAC address to Virtual Router MAC Address */
2139142215Sglebius	switch (ifp->if_type) {
2140142798Syar	case IFT_ETHER:
2141142798Syar	case IFT_L2VLAN: {
2142142215Sglebius			struct ether_header *eh;
2143142215Sglebius
2144142215Sglebius			eh = mtod(m, struct ether_header *);
2145142215Sglebius			eh->ether_shost[0] = 0;
2146142215Sglebius			eh->ether_shost[1] = 0;
2147142215Sglebius			eh->ether_shost[2] = 0x5e;
2148142215Sglebius			eh->ether_shost[3] = 0;
2149142215Sglebius			eh->ether_shost[4] = 1;
2150142215Sglebius			eh->ether_shost[5] = sc->sc_vhid;
2151142215Sglebius		}
2152142215Sglebius		break;
2153142215Sglebius	case IFT_FDDI: {
2154142215Sglebius			struct fddi_header *fh;
2155142215Sglebius
2156142215Sglebius			fh = mtod(m, struct fddi_header *);
2157142215Sglebius			fh->fddi_shost[0] = 0;
2158142215Sglebius			fh->fddi_shost[1] = 0;
2159142215Sglebius			fh->fddi_shost[2] = 0x5e;
2160142215Sglebius			fh->fddi_shost[3] = 0;
2161142215Sglebius			fh->fddi_shost[4] = 1;
2162142215Sglebius			fh->fddi_shost[5] = sc->sc_vhid;
2163142215Sglebius		}
2164142215Sglebius		break;
2165142215Sglebius	case IFT_ISO88025: {
2166142215Sglebius 			struct iso88025_header *th;
2167142215Sglebius 			th = mtod(m, struct iso88025_header *);
2168142215Sglebius			th->iso88025_shost[0] = 3;
2169142215Sglebius			th->iso88025_shost[1] = 0;
2170142215Sglebius			th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1);
2171142215Sglebius			th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1);
2172142215Sglebius			th->iso88025_shost[4] = 0;
2173142215Sglebius			th->iso88025_shost[5] = 0;
2174142215Sglebius		}
2175142215Sglebius		break;
2176142215Sglebius	default:
2177142215Sglebius		printf("%s: carp is not supported for this interface type\n",
2178142215Sglebius		    ifp->if_xname);
2179142215Sglebius		return (EOPNOTSUPP);
2180142215Sglebius	}
2181142215Sglebius
2182142215Sglebius	return (0);
2183142215Sglebius}
2184142215Sglebius
2185142559Sglebiusstatic void
2186142215Sglebiuscarp_set_state(struct carp_softc *sc, int state)
2187142215Sglebius{
2188185636Sglebius	int link_state;
2189142914Sglebius
2190142914Sglebius	if (sc->sc_carpdev)
2191142914Sglebius		CARP_SCLOCK_ASSERT(sc);
2192142914Sglebius
2193142215Sglebius	if (sc->sc_state == state)
2194142215Sglebius		return;
2195142215Sglebius
2196142215Sglebius	sc->sc_state = state;
2197142215Sglebius	switch (state) {
2198142215Sglebius	case BACKUP:
2199185636Sglebius		link_state = LINK_STATE_DOWN;
2200142215Sglebius		break;
2201142215Sglebius	case MASTER:
2202185636Sglebius		link_state = LINK_STATE_UP;
2203142215Sglebius		break;
2204142215Sglebius	default:
2205185636Sglebius		link_state = LINK_STATE_UNKNOWN;
2206142215Sglebius		break;
2207142215Sglebius	}
2208185636Sglebius	if_link_state_change(SC2IFP(sc), link_state);
2209142215Sglebius}
2210142215Sglebius
2211142215Sglebiusvoid
2212142215Sglebiuscarp_carpdev_state(void *v)
2213142215Sglebius{
2214142215Sglebius	struct carp_if *cif = v;
2215142914Sglebius
2216142914Sglebius	CARP_LOCK(cif);
2217142914Sglebius	carp_carpdev_state_locked(cif);
2218142914Sglebius	CARP_UNLOCK(cif);
2219142914Sglebius}
2220142914Sglebius
2221142914Sglebiusstatic void
2222142914Sglebiuscarp_carpdev_state_locked(struct carp_if *cif)
2223142914Sglebius{
2224142215Sglebius	struct carp_softc *sc;
2225142215Sglebius
2226144329Sglebius	TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list)
2227144329Sglebius		carp_sc_state_locked(sc);
2228144329Sglebius}
2229144329Sglebius
2230144329Sglebiusstatic void
2231144329Sglebiuscarp_sc_state_locked(struct carp_softc *sc)
2232144329Sglebius{
2233144329Sglebius	CARP_SCLOCK_ASSERT(sc);
2234144329Sglebius
2235144329Sglebius	if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
2236144329Sglebius	    !(sc->sc_carpdev->if_flags & IFF_UP)) {
2237147256Sbrooks		sc->sc_flags_backup = SC2IFP(sc)->if_flags;
2238148887Srwatson		SC2IFP(sc)->if_flags &= ~IFF_UP;
2239148887Srwatson		SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
2240144329Sglebius		callout_stop(&sc->sc_ad_tmo);
2241144329Sglebius		callout_stop(&sc->sc_md_tmo);
2242144329Sglebius		callout_stop(&sc->sc_md6_tmo);
2243144329Sglebius		carp_set_state(sc, INIT);
2244144329Sglebius		carp_setrun(sc, 0);
2245144329Sglebius		if (!sc->sc_suppress) {
2246144329Sglebius			carp_suppress_preempt++;
2247144329Sglebius			if (carp_suppress_preempt == 1) {
2248144329Sglebius				CARP_SCUNLOCK(sc);
2249144329Sglebius				carp_send_ad_all();
2250144329Sglebius				CARP_SCLOCK(sc);
2251142215Sglebius			}
2252142215Sglebius		}
2253144329Sglebius		sc->sc_suppress = 1;
2254144329Sglebius	} else {
2255147256Sbrooks		SC2IFP(sc)->if_flags |= sc->sc_flags_backup;
2256144329Sglebius		carp_set_state(sc, INIT);
2257144329Sglebius		carp_setrun(sc, 0);
2258144329Sglebius		if (sc->sc_suppress)
2259144329Sglebius			carp_suppress_preempt--;
2260144329Sglebius		sc->sc_suppress = 0;
2261142215Sglebius	}
2262144329Sglebius
2263144329Sglebius	return;
2264142215Sglebius}
2265142215Sglebius
2266142215Sglebiusstatic int
2267142215Sglebiuscarp_modevent(module_t mod, int type, void *data)
2268142215Sglebius{
2269142215Sglebius	switch (type) {
2270142215Sglebius	case MOD_LOAD:
2271156947Sglebius		if_detach_event_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
2272156947Sglebius		    carp_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
2273156947Sglebius		if (if_detach_event_tag == NULL)
2274156947Sglebius			return (ENOMEM);
2275142911Sglebius		mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
2276142215Sglebius		LIST_INIT(&carpif_list);
2277142215Sglebius		if_clone_attach(&carp_cloner);
2278142215Sglebius		break;
2279142215Sglebius
2280142215Sglebius	case MOD_UNLOAD:
2281156947Sglebius		EVENTHANDLER_DEREGISTER(ifnet_departure_event, if_detach_event_tag);
2282142215Sglebius		if_clone_detach(&carp_cloner);
2283142911Sglebius		mtx_destroy(&carp_mtx);
2284142215Sglebius		break;
2285142215Sglebius
2286142215Sglebius	default:
2287156947Sglebius		return (EINVAL);
2288142215Sglebius	}
2289142215Sglebius
2290156947Sglebius	return (0);
2291142215Sglebius}
2292142215Sglebius
2293142215Sglebiusstatic moduledata_t carp_mod = {
2294142215Sglebius	"carp",
2295142215Sglebius	carp_modevent,
2296142215Sglebius	0
2297142215Sglebius};
2298142215Sglebius
2299142215SglebiusDECLARE_MODULE(carp, carp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
2300