xref: /freebsd/sys/netinet/ip_carp.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 /*
2  * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
3  * Copyright (c) 2003 Ryan McBride. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
18  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24  * THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "opt_carp.h"
31 #include "opt_bpf.h"
32 #include "opt_inet.h"
33 #include "opt_inet6.h"
34 
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/conf.h>
39 #include <sys/kernel.h>
40 #include <sys/limits.h>
41 #include <sys/malloc.h>
42 #include <sys/mbuf.h>
43 #include <sys/module.h>
44 #include <sys/time.h>
45 #include <sys/priv.h>
46 #include <sys/proc.h>
47 #include <sys/sysctl.h>
48 #include <sys/syslog.h>
49 #include <sys/signalvar.h>
50 #include <sys/filio.h>
51 #include <sys/sockio.h>
52 
53 #include <sys/socket.h>
54 #include <sys/vnode.h>
55 
56 #include <machine/stdarg.h>
57 
58 #include <net/bpf.h>
59 #include <net/ethernet.h>
60 #include <net/fddi.h>
61 #include <net/iso88025.h>
62 #include <net/if.h>
63 #include <net/if_clone.h>
64 #include <net/if_dl.h>
65 #include <net/if_types.h>
66 #include <net/route.h>
67 
68 #ifdef INET
69 #include <netinet/in.h>
70 #include <netinet/in_var.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/ip.h>
73 #include <netinet/ip_var.h>
74 #include <netinet/if_ether.h>
75 #include <machine/in_cksum.h>
76 #endif
77 
78 #ifdef INET6
79 #include <netinet/icmp6.h>
80 #include <netinet/ip6.h>
81 #include <netinet6/ip6_var.h>
82 #include <netinet6/scope6_var.h>
83 #include <netinet6/nd6.h>
84 #endif
85 
86 #include <crypto/sha1.h>
87 #include <netinet/ip_carp.h>
88 
89 #define	CARP_IFNAME	"carp"
90 static MALLOC_DEFINE(M_CARP, "CARP", "CARP interfaces");
91 SYSCTL_DECL(_net_inet_carp);
92 
93 struct carp_softc {
94 	struct ifnet	 	*sc_ifp;	/* Interface clue */
95 	struct ifnet		*sc_carpdev;	/* Pointer to parent interface */
96 	struct in_ifaddr 	*sc_ia;		/* primary iface address */
97 	struct ip_moptions 	 sc_imo;
98 #ifdef INET6
99 	struct in6_ifaddr 	*sc_ia6;	/* primary iface address v6 */
100 	struct ip6_moptions 	 sc_im6o;
101 #endif /* INET6 */
102 	TAILQ_ENTRY(carp_softc)	 sc_list;
103 
104 	enum { INIT = 0, BACKUP, MASTER }	sc_state;
105 
106 	int			 sc_flags_backup;
107 	int			 sc_suppress;
108 
109 	int			 sc_sendad_errors;
110 #define	CARP_SENDAD_MAX_ERRORS	3
111 	int			 sc_sendad_success;
112 #define	CARP_SENDAD_MIN_SUCCESS 3
113 
114 	int			 sc_vhid;
115 	int			 sc_advskew;
116 	int			 sc_naddrs;
117 	int			 sc_naddrs6;
118 	int			 sc_advbase;	/* seconds */
119 	int			 sc_init_counter;
120 	u_int64_t		 sc_counter;
121 
122 	/* authentication */
123 #define CARP_HMAC_PAD	64
124 	unsigned char sc_key[CARP_KEY_LEN];
125 	unsigned char sc_pad[CARP_HMAC_PAD];
126 	SHA1_CTX sc_sha1;
127 
128 	struct callout		 sc_ad_tmo;	/* advertisement timeout */
129 	struct callout		 sc_md_tmo;	/* master down timeout */
130 	struct callout 		 sc_md6_tmo;	/* master down timeout */
131 
132 	LIST_ENTRY(carp_softc)	 sc_next;	/* Interface clue */
133 };
134 #define	SC2IFP(sc)	((sc)->sc_ifp)
135 
136 int carp_suppress_preempt = 0;
137 int carp_opts[CARPCTL_MAXID] = { 0, 1, 0, 1, 0, 0 };	/* XXX for now */
138 SYSCTL_INT(_net_inet_carp, CARPCTL_ALLOW, allow, CTLFLAG_RW,
139     &carp_opts[CARPCTL_ALLOW], 0, "Accept incoming CARP packets");
140 SYSCTL_INT(_net_inet_carp, CARPCTL_PREEMPT, preempt, CTLFLAG_RW,
141     &carp_opts[CARPCTL_PREEMPT], 0, "high-priority backup preemption mode");
142 SYSCTL_INT(_net_inet_carp, CARPCTL_LOG, log, CTLFLAG_RW,
143     &carp_opts[CARPCTL_LOG], 0, "log bad carp packets");
144 SYSCTL_INT(_net_inet_carp, CARPCTL_ARPBALANCE, arpbalance, CTLFLAG_RW,
145     &carp_opts[CARPCTL_ARPBALANCE], 0, "balance arp responses");
146 SYSCTL_INT(_net_inet_carp, OID_AUTO, suppress_preempt, CTLFLAG_RD,
147     &carp_suppress_preempt, 0, "Preemption is suppressed");
148 
149 struct carpstats carpstats;
150 SYSCTL_STRUCT(_net_inet_carp, CARPCTL_STATS, stats, CTLFLAG_RW,
151     &carpstats, carpstats,
152     "CARP statistics (struct carpstats, netinet/ip_carp.h)");
153 
154 struct carp_if {
155 	TAILQ_HEAD(, carp_softc) vhif_vrs;
156 	int vhif_nvrs;
157 
158 	struct ifnet 	*vhif_ifp;
159 	struct mtx	 vhif_mtx;
160 };
161 
162 /* Get carp_if from softc. Valid after carp_set_addr{,6}. */
163 #define	SC2CIF(sc)		((struct carp_if *)(sc)->sc_carpdev->if_carp)
164 
165 /* lock per carp_if queue */
166 #define	CARP_LOCK_INIT(cif)	mtx_init(&(cif)->vhif_mtx, "carp_if", 	\
167 	NULL, MTX_DEF)
168 #define	CARP_LOCK_DESTROY(cif)	mtx_destroy(&(cif)->vhif_mtx)
169 #define	CARP_LOCK_ASSERT(cif)	mtx_assert(&(cif)->vhif_mtx, MA_OWNED)
170 #define	CARP_LOCK(cif)		mtx_lock(&(cif)->vhif_mtx)
171 #define	CARP_UNLOCK(cif)	mtx_unlock(&(cif)->vhif_mtx)
172 
173 #define	CARP_SCLOCK(sc)		mtx_lock(&SC2CIF(sc)->vhif_mtx)
174 #define	CARP_SCUNLOCK(sc)	mtx_unlock(&SC2CIF(sc)->vhif_mtx)
175 #define	CARP_SCLOCK_ASSERT(sc)	mtx_assert(&SC2CIF(sc)->vhif_mtx, MA_OWNED)
176 
177 #define	CARP_LOG(...)	do {				\
178 	if (carp_opts[CARPCTL_LOG] > 0)			\
179 		log(LOG_INFO, __VA_ARGS__);		\
180 } while (0)
181 
182 #define	CARP_DEBUG(...)	do {				\
183 	if (carp_opts[CARPCTL_LOG] > 1)			\
184 		log(LOG_DEBUG, __VA_ARGS__);		\
185 } while (0)
186 
187 static void	carp_hmac_prepare(struct carp_softc *);
188 static void	carp_hmac_generate(struct carp_softc *, u_int32_t *,
189 		    unsigned char *);
190 static int	carp_hmac_verify(struct carp_softc *, u_int32_t *,
191 		    unsigned char *);
192 static void	carp_setroute(struct carp_softc *, int);
193 static void	carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
194 static int 	carp_clone_create(struct if_clone *, int, caddr_t);
195 static void 	carp_clone_destroy(struct ifnet *);
196 static void	carpdetach(struct carp_softc *, int);
197 static int	carp_prepare_ad(struct mbuf *, struct carp_softc *,
198 		    struct carp_header *);
199 static void	carp_send_ad_all(void);
200 static void	carp_send_ad(void *);
201 static void	carp_send_ad_locked(struct carp_softc *);
202 static void	carp_send_arp(struct carp_softc *);
203 static void	carp_master_down(void *);
204 static void	carp_master_down_locked(struct carp_softc *);
205 static int	carp_ioctl(struct ifnet *, u_long, caddr_t);
206 static int	carp_looutput(struct ifnet *, struct mbuf *, struct sockaddr *,
207 		    struct rtentry *);
208 static void	carp_start(struct ifnet *);
209 static void	carp_setrun(struct carp_softc *, sa_family_t);
210 static void	carp_set_state(struct carp_softc *, int);
211 static int	carp_addrcount(struct carp_if *, struct in_ifaddr *, int);
212 enum	{ CARP_COUNT_MASTER, CARP_COUNT_RUNNING };
213 
214 static void	carp_multicast_cleanup(struct carp_softc *);
215 static int	carp_set_addr(struct carp_softc *, struct sockaddr_in *);
216 static int	carp_del_addr(struct carp_softc *, struct sockaddr_in *);
217 static void	carp_carpdev_state_locked(struct carp_if *);
218 static void	carp_sc_state_locked(struct carp_softc *);
219 #ifdef INET6
220 static void	carp_send_na(struct carp_softc *);
221 static int	carp_set_addr6(struct carp_softc *, struct sockaddr_in6 *);
222 static int	carp_del_addr6(struct carp_softc *, struct sockaddr_in6 *);
223 static void	carp_multicast6_cleanup(struct carp_softc *);
224 #endif
225 
226 static LIST_HEAD(, carp_softc) carpif_list;
227 static struct mtx carp_mtx;
228 IFC_SIMPLE_DECLARE(carp, 0);
229 
230 static eventhandler_tag if_detach_event_tag;
231 
232 static __inline u_int16_t
233 carp_cksum(struct mbuf *m, int len)
234 {
235 	return (in_cksum(m, len));
236 }
237 
238 static void
239 carp_hmac_prepare(struct carp_softc *sc)
240 {
241 	u_int8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
242 	u_int8_t vhid = sc->sc_vhid & 0xff;
243 	struct ifaddr *ifa;
244 	int i;
245 #ifdef INET6
246 	struct in6_addr in6;
247 #endif
248 
249 	if (sc->sc_carpdev)
250 		CARP_SCLOCK(sc);
251 
252 	/* XXX: possible race here */
253 
254 	/* compute ipad from key */
255 	bzero(sc->sc_pad, sizeof(sc->sc_pad));
256 	bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
257 	for (i = 0; i < sizeof(sc->sc_pad); i++)
258 		sc->sc_pad[i] ^= 0x36;
259 
260 	/* precompute first part of inner hash */
261 	SHA1Init(&sc->sc_sha1);
262 	SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
263 	SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
264 	SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
265 	SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
266 #ifdef INET
267 	TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
268 		if (ifa->ifa_addr->sa_family == AF_INET)
269 			SHA1Update(&sc->sc_sha1,
270 			    (void *)&ifatoia(ifa)->ia_addr.sin_addr.s_addr,
271 			    sizeof(struct in_addr));
272 	}
273 #endif /* INET */
274 #ifdef INET6
275 	TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
276 		if (ifa->ifa_addr->sa_family == AF_INET6) {
277 			in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
278 			in6_clearscope(&in6);
279 			SHA1Update(&sc->sc_sha1, (void *)&in6, sizeof(in6));
280 		}
281 	}
282 #endif /* INET6 */
283 
284 	/* convert ipad to opad */
285 	for (i = 0; i < sizeof(sc->sc_pad); i++)
286 		sc->sc_pad[i] ^= 0x36 ^ 0x5c;
287 
288 	if (sc->sc_carpdev)
289 		CARP_SCUNLOCK(sc);
290 }
291 
292 static void
293 carp_hmac_generate(struct carp_softc *sc, u_int32_t counter[2],
294     unsigned char md[20])
295 {
296 	SHA1_CTX sha1ctx;
297 
298 	/* fetch first half of inner hash */
299 	bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
300 
301 	SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
302 	SHA1Final(md, &sha1ctx);
303 
304 	/* outer hash */
305 	SHA1Init(&sha1ctx);
306 	SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
307 	SHA1Update(&sha1ctx, md, 20);
308 	SHA1Final(md, &sha1ctx);
309 }
310 
311 static int
312 carp_hmac_verify(struct carp_softc *sc, u_int32_t counter[2],
313     unsigned char md[20])
314 {
315 	unsigned char md2[20];
316 
317 	CARP_SCLOCK_ASSERT(sc);
318 
319 	carp_hmac_generate(sc, counter, md2);
320 
321 	return (bcmp(md, md2, sizeof(md2)));
322 }
323 
324 static void
325 carp_setroute(struct carp_softc *sc, int cmd)
326 {
327 	struct ifaddr *ifa;
328 	int s;
329 
330 	if (sc->sc_carpdev)
331 		CARP_SCLOCK_ASSERT(sc);
332 
333 	s = splnet();
334 	TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
335 		if (ifa->ifa_addr->sa_family == AF_INET &&
336 		    sc->sc_carpdev != NULL) {
337 			int count = carp_addrcount(
338 			    (struct carp_if *)sc->sc_carpdev->if_carp,
339 			    ifatoia(ifa), CARP_COUNT_MASTER);
340 
341 			if ((cmd == RTM_ADD && count == 1) ||
342 			    (cmd == RTM_DELETE && count == 0))
343 				rtinit(ifa, cmd, RTF_UP | RTF_HOST);
344 		}
345 #ifdef INET6
346 		if (ifa->ifa_addr->sa_family == AF_INET6) {
347 			if (cmd == RTM_ADD)
348 				in6_ifaddloop(ifa);
349 			else
350 				in6_ifremloop(ifa);
351 		}
352 #endif /* INET6 */
353 	}
354 	splx(s);
355 }
356 
357 static int
358 carp_clone_create(struct if_clone *ifc, int unit, caddr_t params)
359 {
360 
361 	struct carp_softc *sc;
362 	struct ifnet *ifp;
363 
364 	MALLOC(sc, struct carp_softc *, sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
365 	ifp = SC2IFP(sc) = if_alloc(IFT_ETHER);
366 	if (ifp == NULL) {
367 		FREE(sc, M_CARP);
368 		return (ENOSPC);
369 	}
370 
371 	sc->sc_flags_backup = 0;
372 	sc->sc_suppress = 0;
373 	sc->sc_advbase = CARP_DFLTINTV;
374 	sc->sc_vhid = -1;	/* required setting */
375 	sc->sc_advskew = 0;
376 	sc->sc_init_counter = 1;
377 	sc->sc_naddrs = sc->sc_naddrs6 = 0; /* M_ZERO? */
378 #ifdef INET6
379 	sc->sc_im6o.im6o_multicast_hlim = CARP_DFLTTL;
380 #endif
381 	sc->sc_imo.imo_membership = (struct in_multi **)malloc(
382 	    (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_CARP,
383 	    M_WAITOK);
384 	sc->sc_imo.imo_mfilters = NULL;
385 	sc->sc_imo.imo_max_memberships = IP_MIN_MEMBERSHIPS;
386 	sc->sc_imo.imo_multicast_vif = -1;
387 
388 	callout_init(&sc->sc_ad_tmo, CALLOUT_MPSAFE);
389 	callout_init(&sc->sc_md_tmo, CALLOUT_MPSAFE);
390 	callout_init(&sc->sc_md6_tmo, CALLOUT_MPSAFE);
391 
392 	ifp->if_softc = sc;
393 	if_initname(ifp, CARP_IFNAME, unit);
394 	ifp->if_mtu = ETHERMTU;
395 	ifp->if_flags = IFF_LOOPBACK;
396 	ifp->if_ioctl = carp_ioctl;
397 	ifp->if_output = carp_looutput;
398 	ifp->if_start = carp_start;
399 	ifp->if_type = IFT_CARP;
400 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
401 	ifp->if_hdrlen = 0;
402 	if_attach(ifp);
403 	bpfattach(SC2IFP(sc), DLT_NULL, sizeof(u_int32_t));
404 	mtx_lock(&carp_mtx);
405 	LIST_INSERT_HEAD(&carpif_list, sc, sc_next);
406 	mtx_unlock(&carp_mtx);
407 	return (0);
408 }
409 
410 static void
411 carp_clone_destroy(struct ifnet *ifp)
412 {
413 	struct carp_softc *sc = ifp->if_softc;
414 
415 	if (sc->sc_carpdev)
416 		CARP_SCLOCK(sc);
417 	carpdetach(sc, 1);	/* Returns unlocked. */
418 
419 	mtx_lock(&carp_mtx);
420 	LIST_REMOVE(sc, sc_next);
421 	mtx_unlock(&carp_mtx);
422 	bpfdetach(ifp);
423 	if_detach(ifp);
424 	if_free_type(ifp, IFT_ETHER);
425 	free(sc->sc_imo.imo_membership, M_CARP);
426 	free(sc, M_CARP);
427 }
428 
429 /*
430  * This function can be called on CARP interface destroy path,
431  * and in case of the removal of the underlying interface as
432  * well. We differentiate these two cases. In the latter case
433  * we do not cleanup our multicast memberships, since they
434  * are already freed. Also, in the latter case we do not
435  * release the lock on return, because the function will be
436  * called once more, for another CARP instance on the same
437  * interface.
438  */
439 static void
440 carpdetach(struct carp_softc *sc, int unlock)
441 {
442 	struct carp_if *cif;
443 
444 	callout_stop(&sc->sc_ad_tmo);
445 	callout_stop(&sc->sc_md_tmo);
446 	callout_stop(&sc->sc_md6_tmo);
447 
448 	if (sc->sc_suppress)
449 		carp_suppress_preempt--;
450 	sc->sc_suppress = 0;
451 
452 	if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS)
453 		carp_suppress_preempt--;
454 	sc->sc_sendad_errors = 0;
455 
456 	carp_set_state(sc, INIT);
457 	SC2IFP(sc)->if_flags &= ~IFF_UP;
458 	carp_setrun(sc, 0);
459 	if (unlock)
460 		carp_multicast_cleanup(sc);
461 #ifdef INET6
462 	carp_multicast6_cleanup(sc);
463 #endif
464 
465 	if (sc->sc_carpdev != NULL) {
466 		cif = (struct carp_if *)sc->sc_carpdev->if_carp;
467 		CARP_LOCK_ASSERT(cif);
468 		TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
469 		if (!--cif->vhif_nvrs) {
470 			ifpromisc(sc->sc_carpdev, 0);
471 			sc->sc_carpdev->if_carp = NULL;
472 			CARP_LOCK_DESTROY(cif);
473 			FREE(cif, M_IFADDR);
474 		} else if (unlock)
475 			CARP_UNLOCK(cif);
476 		sc->sc_carpdev = NULL;
477 	}
478 }
479 
480 /* Detach an interface from the carp. */
481 static void
482 carp_ifdetach(void *arg __unused, struct ifnet *ifp)
483 {
484 	struct carp_if *cif = (struct carp_if *)ifp->if_carp;
485 	struct carp_softc *sc, *nextsc;
486 
487 	if (cif == NULL)
488 		return;
489 
490 	/*
491 	 * XXX: At the end of for() cycle the lock will be destroyed.
492 	 */
493 	CARP_LOCK(cif);
494 	for (sc = TAILQ_FIRST(&cif->vhif_vrs); sc; sc = nextsc) {
495 		nextsc = TAILQ_NEXT(sc, sc_list);
496 		carpdetach(sc, 0);
497 	}
498 }
499 
500 /*
501  * process input packet.
502  * we have rearranged checks order compared to the rfc,
503  * but it seems more efficient this way or not possible otherwise.
504  */
505 void
506 carp_input(struct mbuf *m, int hlen)
507 {
508 	struct ip *ip = mtod(m, struct ip *);
509 	struct carp_header *ch;
510 	int iplen, len;
511 
512 	carpstats.carps_ipackets++;
513 
514 	if (!carp_opts[CARPCTL_ALLOW]) {
515 		m_freem(m);
516 		return;
517 	}
518 
519 	/* check if received on a valid carp interface */
520 	if (m->m_pkthdr.rcvif->if_carp == NULL) {
521 		carpstats.carps_badif++;
522 		CARP_LOG("carp_input: packet received on non-carp "
523 		    "interface: %s\n",
524 		    m->m_pkthdr.rcvif->if_xname);
525 		m_freem(m);
526 		return;
527 	}
528 
529 	/* verify that the IP TTL is 255.  */
530 	if (ip->ip_ttl != CARP_DFLTTL) {
531 		carpstats.carps_badttl++;
532 		CARP_LOG("carp_input: received ttl %d != 255i on %s\n",
533 		    ip->ip_ttl,
534 		    m->m_pkthdr.rcvif->if_xname);
535 		m_freem(m);
536 		return;
537 	}
538 
539 	iplen = ip->ip_hl << 2;
540 
541 	if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
542 		carpstats.carps_badlen++;
543 		CARP_LOG("carp_input: received len %zd < "
544 		    "sizeof(struct carp_header)\n",
545 		    m->m_len - sizeof(struct ip));
546 		m_freem(m);
547 		return;
548 	}
549 
550 	if (iplen + sizeof(*ch) < m->m_len) {
551 		if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
552 			carpstats.carps_hdrops++;
553 			CARP_LOG("carp_input: pullup failed\n");
554 			return;
555 		}
556 		ip = mtod(m, struct ip *);
557 	}
558 	ch = (struct carp_header *)((char *)ip + iplen);
559 
560 	/*
561 	 * verify that the received packet length is
562 	 * equal to the CARP header
563 	 */
564 	len = iplen + sizeof(*ch);
565 	if (len > m->m_pkthdr.len) {
566 		carpstats.carps_badlen++;
567 		CARP_LOG("carp_input: packet too short %d on %s\n",
568 		    m->m_pkthdr.len,
569 		    m->m_pkthdr.rcvif->if_xname);
570 		m_freem(m);
571 		return;
572 	}
573 
574 	if ((m = m_pullup(m, len)) == NULL) {
575 		carpstats.carps_hdrops++;
576 		return;
577 	}
578 	ip = mtod(m, struct ip *);
579 	ch = (struct carp_header *)((char *)ip + iplen);
580 
581 	/* verify the CARP checksum */
582 	m->m_data += iplen;
583 	if (carp_cksum(m, len - iplen)) {
584 		carpstats.carps_badsum++;
585 		CARP_LOG("carp_input: checksum failed on %s\n",
586 		    m->m_pkthdr.rcvif->if_xname);
587 		m_freem(m);
588 		return;
589 	}
590 	m->m_data -= iplen;
591 
592 	carp_input_c(m, ch, AF_INET);
593 }
594 
595 #ifdef INET6
596 int
597 carp6_input(struct mbuf **mp, int *offp, int proto)
598 {
599 	struct mbuf *m = *mp;
600 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
601 	struct carp_header *ch;
602 	u_int len;
603 
604 	carpstats.carps_ipackets6++;
605 
606 	if (!carp_opts[CARPCTL_ALLOW]) {
607 		m_freem(m);
608 		return (IPPROTO_DONE);
609 	}
610 
611 	/* check if received on a valid carp interface */
612 	if (m->m_pkthdr.rcvif->if_carp == NULL) {
613 		carpstats.carps_badif++;
614 		CARP_LOG("carp6_input: packet received on non-carp "
615 		    "interface: %s\n",
616 		    m->m_pkthdr.rcvif->if_xname);
617 		m_freem(m);
618 		return (IPPROTO_DONE);
619 	}
620 
621 	/* verify that the IP TTL is 255 */
622 	if (ip6->ip6_hlim != CARP_DFLTTL) {
623 		carpstats.carps_badttl++;
624 		CARP_LOG("carp6_input: received ttl %d != 255 on %s\n",
625 		    ip6->ip6_hlim,
626 		    m->m_pkthdr.rcvif->if_xname);
627 		m_freem(m);
628 		return (IPPROTO_DONE);
629 	}
630 
631 	/* verify that we have a complete carp packet */
632 	len = m->m_len;
633 	IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
634 	if (ch == NULL) {
635 		carpstats.carps_badlen++;
636 		CARP_LOG("carp6_input: packet size %u too small\n", len);
637 		return (IPPROTO_DONE);
638 	}
639 
640 
641 	/* verify the CARP checksum */
642 	m->m_data += *offp;
643 	if (carp_cksum(m, sizeof(*ch))) {
644 		carpstats.carps_badsum++;
645 		CARP_LOG("carp6_input: checksum failed, on %s\n",
646 		    m->m_pkthdr.rcvif->if_xname);
647 		m_freem(m);
648 		return (IPPROTO_DONE);
649 	}
650 	m->m_data -= *offp;
651 
652 	carp_input_c(m, ch, AF_INET6);
653 	return (IPPROTO_DONE);
654 }
655 #endif /* INET6 */
656 
657 static void
658 carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
659 {
660 	struct ifnet *ifp = m->m_pkthdr.rcvif;
661 	struct carp_softc *sc;
662 	u_int64_t tmp_counter;
663 	struct timeval sc_tv, ch_tv;
664 
665 	/* verify that the VHID is valid on the receiving interface */
666 	CARP_LOCK(ifp->if_carp);
667 	TAILQ_FOREACH(sc, &((struct carp_if *)ifp->if_carp)->vhif_vrs, sc_list)
668 		if (sc->sc_vhid == ch->carp_vhid)
669 			break;
670 
671 	if (!sc || !((SC2IFP(sc)->if_flags & IFF_UP) &&
672 	    (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING))) {
673 		carpstats.carps_badvhid++;
674 		CARP_UNLOCK(ifp->if_carp);
675 		m_freem(m);
676 		return;
677 	}
678 
679 	getmicrotime(&SC2IFP(sc)->if_lastchange);
680 	SC2IFP(sc)->if_ipackets++;
681 	SC2IFP(sc)->if_ibytes += m->m_pkthdr.len;
682 
683 	if (bpf_peers_present(SC2IFP(sc)->if_bpf)) {
684 		struct ip *ip = mtod(m, struct ip *);
685 		uint32_t af1 = af;
686 
687 		/* BPF wants net byte order */
688 		ip->ip_len = htons(ip->ip_len + (ip->ip_hl << 2));
689 		ip->ip_off = htons(ip->ip_off);
690 		bpf_mtap2(SC2IFP(sc)->if_bpf, &af1, sizeof(af1), m);
691 	}
692 
693 	/* verify the CARP version. */
694 	if (ch->carp_version != CARP_VERSION) {
695 		carpstats.carps_badver++;
696 		SC2IFP(sc)->if_ierrors++;
697 		CARP_UNLOCK(ifp->if_carp);
698 		CARP_LOG("%s; invalid version %d\n",
699 		    SC2IFP(sc)->if_xname,
700 		    ch->carp_version);
701 		m_freem(m);
702 		return;
703 	}
704 
705 	/* verify the hash */
706 	if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
707 		carpstats.carps_badauth++;
708 		SC2IFP(sc)->if_ierrors++;
709 		CARP_UNLOCK(ifp->if_carp);
710 		CARP_LOG("%s: incorrect hash\n", SC2IFP(sc)->if_xname);
711 		m_freem(m);
712 		return;
713 	}
714 
715 	tmp_counter = ntohl(ch->carp_counter[0]);
716 	tmp_counter = tmp_counter<<32;
717 	tmp_counter += ntohl(ch->carp_counter[1]);
718 
719 	/* XXX Replay protection goes here */
720 
721 	sc->sc_init_counter = 0;
722 	sc->sc_counter = tmp_counter;
723 
724 	sc_tv.tv_sec = sc->sc_advbase;
725 	if (carp_suppress_preempt && sc->sc_advskew <  240)
726 		sc_tv.tv_usec = 240 * 1000000 / 256;
727 	else
728 		sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256;
729 	ch_tv.tv_sec = ch->carp_advbase;
730 	ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
731 
732 	switch (sc->sc_state) {
733 	case INIT:
734 		break;
735 	case MASTER:
736 		/*
737 		 * If we receive an advertisement from a master who's going to
738 		 * be more frequent than us, go into BACKUP state.
739 		 */
740 		if (timevalcmp(&sc_tv, &ch_tv, >) ||
741 		    timevalcmp(&sc_tv, &ch_tv, ==)) {
742 			callout_stop(&sc->sc_ad_tmo);
743 			CARP_DEBUG("%s: MASTER -> BACKUP "
744 			   "(more frequent advertisement received)\n",
745 			   SC2IFP(sc)->if_xname);
746 			carp_set_state(sc, BACKUP);
747 			carp_setrun(sc, 0);
748 			carp_setroute(sc, RTM_DELETE);
749 		}
750 		break;
751 	case BACKUP:
752 		/*
753 		 * If we're pre-empting masters who advertise slower than us,
754 		 * and this one claims to be slower, treat him as down.
755 		 */
756 		if (carp_opts[CARPCTL_PREEMPT] &&
757 		    timevalcmp(&sc_tv, &ch_tv, <)) {
758 			CARP_DEBUG("%s: BACKUP -> MASTER "
759 			    "(preempting a slower master)\n",
760 			    SC2IFP(sc)->if_xname);
761 			carp_master_down_locked(sc);
762 			break;
763 		}
764 
765 		/*
766 		 *  If the master is going to advertise at such a low frequency
767 		 *  that he's guaranteed to time out, we'd might as well just
768 		 *  treat him as timed out now.
769 		 */
770 		sc_tv.tv_sec = sc->sc_advbase * 3;
771 		if (timevalcmp(&sc_tv, &ch_tv, <)) {
772 			CARP_DEBUG("%s: BACKUP -> MASTER "
773 			    "(master timed out)\n",
774 			    SC2IFP(sc)->if_xname);
775 			carp_master_down_locked(sc);
776 			break;
777 		}
778 
779 		/*
780 		 * Otherwise, we reset the counter and wait for the next
781 		 * advertisement.
782 		 */
783 		carp_setrun(sc, af);
784 		break;
785 	}
786 
787 	CARP_UNLOCK(ifp->if_carp);
788 
789 	m_freem(m);
790 	return;
791 }
792 
793 static int
794 carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
795 {
796 	struct m_tag *mtag;
797 	struct ifnet *ifp = SC2IFP(sc);
798 
799 	if (sc->sc_init_counter) {
800 		/* this could also be seconds since unix epoch */
801 		sc->sc_counter = arc4random();
802 		sc->sc_counter = sc->sc_counter << 32;
803 		sc->sc_counter += arc4random();
804 	} else
805 		sc->sc_counter++;
806 
807 	ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
808 	ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
809 
810 	carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
811 
812 	/* Tag packet for carp_output */
813 	mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct ifnet *), M_NOWAIT);
814 	if (mtag == NULL) {
815 		m_freem(m);
816 		SC2IFP(sc)->if_oerrors++;
817 		return (ENOMEM);
818 	}
819 	bcopy(&ifp, (caddr_t)(mtag + 1), sizeof(struct ifnet *));
820 	m_tag_prepend(m, mtag);
821 
822 	return (0);
823 }
824 
825 static void
826 carp_send_ad_all(void)
827 {
828 	struct carp_softc *sc;
829 
830 	mtx_lock(&carp_mtx);
831 	LIST_FOREACH(sc, &carpif_list, sc_next) {
832 		if (sc->sc_carpdev == NULL)
833 			continue;
834 		CARP_SCLOCK(sc);
835 		if ((SC2IFP(sc)->if_flags & IFF_UP) &&
836 		    (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING) &&
837 		     sc->sc_state == MASTER)
838 			carp_send_ad_locked(sc);
839 		CARP_SCUNLOCK(sc);
840 	}
841 	mtx_unlock(&carp_mtx);
842 }
843 
844 static void
845 carp_send_ad(void *v)
846 {
847 	struct carp_softc *sc = v;
848 
849 	CARP_SCLOCK(sc);
850 	carp_send_ad_locked(sc);
851 	CARP_SCUNLOCK(sc);
852 }
853 
854 static void
855 carp_send_ad_locked(struct carp_softc *sc)
856 {
857 	struct carp_header ch;
858 	struct timeval tv;
859 	struct carp_header *ch_ptr;
860 	struct mbuf *m;
861 	int len, advbase, advskew;
862 
863 	CARP_SCLOCK_ASSERT(sc);
864 
865 	/* bow out if we've lost our UPness or RUNNINGuiness */
866 	if (!((SC2IFP(sc)->if_flags & IFF_UP) &&
867 	    (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING))) {
868 		advbase = 255;
869 		advskew = 255;
870 	} else {
871 		advbase = sc->sc_advbase;
872 		if (!carp_suppress_preempt || sc->sc_advskew > 240)
873 			advskew = sc->sc_advskew;
874 		else
875 			advskew = 240;
876 		tv.tv_sec = advbase;
877 		tv.tv_usec = advskew * 1000000 / 256;
878 	}
879 
880 	ch.carp_version = CARP_VERSION;
881 	ch.carp_type = CARP_ADVERTISEMENT;
882 	ch.carp_vhid = sc->sc_vhid;
883 	ch.carp_advbase = advbase;
884 	ch.carp_advskew = advskew;
885 	ch.carp_authlen = 7;	/* XXX DEFINE */
886 	ch.carp_pad1 = 0;	/* must be zero */
887 	ch.carp_cksum = 0;
888 
889 #ifdef INET
890 	if (sc->sc_ia) {
891 		struct ip *ip;
892 
893 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
894 		if (m == NULL) {
895 			SC2IFP(sc)->if_oerrors++;
896 			carpstats.carps_onomem++;
897 			/* XXX maybe less ? */
898 			if (advbase != 255 || advskew != 255)
899 				callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
900 				    carp_send_ad, sc);
901 			return;
902 		}
903 		len = sizeof(*ip) + sizeof(ch);
904 		m->m_pkthdr.len = len;
905 		m->m_pkthdr.rcvif = NULL;
906 		m->m_len = len;
907 		MH_ALIGN(m, m->m_len);
908 		m->m_flags |= M_MCAST;
909 		ip = mtod(m, struct ip *);
910 		ip->ip_v = IPVERSION;
911 		ip->ip_hl = sizeof(*ip) >> 2;
912 		ip->ip_tos = IPTOS_LOWDELAY;
913 		ip->ip_len = len;
914 		ip->ip_id = ip_newid();
915 		ip->ip_off = IP_DF;
916 		ip->ip_ttl = CARP_DFLTTL;
917 		ip->ip_p = IPPROTO_CARP;
918 		ip->ip_sum = 0;
919 		ip->ip_src.s_addr = sc->sc_ia->ia_addr.sin_addr.s_addr;
920 		ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
921 
922 		ch_ptr = (struct carp_header *)(&ip[1]);
923 		bcopy(&ch, ch_ptr, sizeof(ch));
924 		if (carp_prepare_ad(m, sc, ch_ptr))
925 			return;
926 
927 		m->m_data += sizeof(*ip);
928 		ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip));
929 		m->m_data -= sizeof(*ip);
930 
931 		getmicrotime(&SC2IFP(sc)->if_lastchange);
932 		SC2IFP(sc)->if_opackets++;
933 		SC2IFP(sc)->if_obytes += len;
934 		carpstats.carps_opackets++;
935 
936 		if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) {
937 			SC2IFP(sc)->if_oerrors++;
938 			if (sc->sc_sendad_errors < INT_MAX)
939 				sc->sc_sendad_errors++;
940 			if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
941 				carp_suppress_preempt++;
942 				if (carp_suppress_preempt == 1) {
943 					CARP_SCUNLOCK(sc);
944 					carp_send_ad_all();
945 					CARP_SCLOCK(sc);
946 				}
947 			}
948 			sc->sc_sendad_success = 0;
949 		} else {
950 			if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
951 				if (++sc->sc_sendad_success >=
952 				    CARP_SENDAD_MIN_SUCCESS) {
953 					carp_suppress_preempt--;
954 					sc->sc_sendad_errors = 0;
955 				}
956 			} else
957 				sc->sc_sendad_errors = 0;
958 		}
959 	}
960 #endif /* INET */
961 #ifdef INET6
962 	if (sc->sc_ia6) {
963 		struct ip6_hdr *ip6;
964 
965 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
966 		if (m == NULL) {
967 			SC2IFP(sc)->if_oerrors++;
968 			carpstats.carps_onomem++;
969 			/* XXX maybe less ? */
970 			if (advbase != 255 || advskew != 255)
971 				callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
972 				    carp_send_ad, sc);
973 			return;
974 		}
975 		len = sizeof(*ip6) + sizeof(ch);
976 		m->m_pkthdr.len = len;
977 		m->m_pkthdr.rcvif = NULL;
978 		m->m_len = len;
979 		MH_ALIGN(m, m->m_len);
980 		m->m_flags |= M_MCAST;
981 		ip6 = mtod(m, struct ip6_hdr *);
982 		bzero(ip6, sizeof(*ip6));
983 		ip6->ip6_vfc |= IPV6_VERSION;
984 		ip6->ip6_hlim = CARP_DFLTTL;
985 		ip6->ip6_nxt = IPPROTO_CARP;
986 		bcopy(&sc->sc_ia6->ia_addr.sin6_addr, &ip6->ip6_src,
987 		    sizeof(struct in6_addr));
988 		/* set the multicast destination */
989 
990 		ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
991 		ip6->ip6_dst.s6_addr8[15] = 0x12;
992 		if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
993 			SC2IFP(sc)->if_oerrors++;
994 			m_freem(m);
995 			CARP_LOG("%s: in6_setscope failed\n", __func__);
996 			return;
997 		}
998 
999 		ch_ptr = (struct carp_header *)(&ip6[1]);
1000 		bcopy(&ch, ch_ptr, sizeof(ch));
1001 		if (carp_prepare_ad(m, sc, ch_ptr))
1002 			return;
1003 
1004 		m->m_data += sizeof(*ip6);
1005 		ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip6));
1006 		m->m_data -= sizeof(*ip6);
1007 
1008 		getmicrotime(&SC2IFP(sc)->if_lastchange);
1009 		SC2IFP(sc)->if_opackets++;
1010 		SC2IFP(sc)->if_obytes += len;
1011 		carpstats.carps_opackets6++;
1012 
1013 		if (ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL)) {
1014 			SC2IFP(sc)->if_oerrors++;
1015 			if (sc->sc_sendad_errors < INT_MAX)
1016 				sc->sc_sendad_errors++;
1017 			if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
1018 				carp_suppress_preempt++;
1019 				if (carp_suppress_preempt == 1) {
1020 					CARP_SCUNLOCK(sc);
1021 					carp_send_ad_all();
1022 					CARP_SCLOCK(sc);
1023 				}
1024 			}
1025 			sc->sc_sendad_success = 0;
1026 		} else {
1027 			if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
1028 				if (++sc->sc_sendad_success >=
1029 				    CARP_SENDAD_MIN_SUCCESS) {
1030 					carp_suppress_preempt--;
1031 					sc->sc_sendad_errors = 0;
1032 				}
1033 			} else
1034 				sc->sc_sendad_errors = 0;
1035 		}
1036 	}
1037 #endif /* INET6 */
1038 
1039 	if (advbase != 255 || advskew != 255)
1040 		callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1041 		    carp_send_ad, sc);
1042 
1043 }
1044 
1045 /*
1046  * Broadcast a gratuitous ARP request containing
1047  * the virtual router MAC address for each IP address
1048  * associated with the virtual router.
1049  */
1050 static void
1051 carp_send_arp(struct carp_softc *sc)
1052 {
1053 	struct ifaddr *ifa;
1054 
1055 	TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1056 
1057 		if (ifa->ifa_addr->sa_family != AF_INET)
1058 			continue;
1059 
1060 /*		arprequest(sc->sc_carpdev, &in, &in, IF_LLADDR(sc->sc_ifp)); */
1061 		arp_ifinit2(sc->sc_carpdev, ifa, IF_LLADDR(sc->sc_ifp));
1062 
1063 		DELAY(1000);	/* XXX */
1064 	}
1065 }
1066 
1067 #ifdef INET6
1068 static void
1069 carp_send_na(struct carp_softc *sc)
1070 {
1071 	struct ifaddr *ifa;
1072 	struct in6_addr *in6;
1073 	static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1074 
1075 	TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1076 
1077 		if (ifa->ifa_addr->sa_family != AF_INET6)
1078 			continue;
1079 
1080 		in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
1081 		nd6_na_output(sc->sc_carpdev, &mcast, in6,
1082 		    ND_NA_FLAG_OVERRIDE, 1, NULL);
1083 		DELAY(1000);	/* XXX */
1084 	}
1085 }
1086 #endif /* INET6 */
1087 
1088 static int
1089 carp_addrcount(struct carp_if *cif, struct in_ifaddr *ia, int type)
1090 {
1091 	struct carp_softc *vh;
1092 	struct ifaddr *ifa;
1093 	int count = 0;
1094 
1095 	CARP_LOCK_ASSERT(cif);
1096 
1097 	TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1098 		if ((type == CARP_COUNT_RUNNING &&
1099 		    (SC2IFP(vh)->if_flags & IFF_UP) &&
1100 		    (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING)) ||
1101 		    (type == CARP_COUNT_MASTER && vh->sc_state == MASTER)) {
1102 			TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist,
1103 			    ifa_list) {
1104 				if (ifa->ifa_addr->sa_family == AF_INET &&
1105 				    ia->ia_addr.sin_addr.s_addr ==
1106 				    ifatoia(ifa)->ia_addr.sin_addr.s_addr)
1107 					count++;
1108 			}
1109 		}
1110 	}
1111 	return (count);
1112 }
1113 
1114 int
1115 carp_iamatch(void *v, struct in_ifaddr *ia,
1116     struct in_addr *isaddr, u_int8_t **enaddr)
1117 {
1118 	struct carp_if *cif = v;
1119 	struct carp_softc *vh;
1120 	int index, count = 0;
1121 	struct ifaddr *ifa;
1122 
1123 	CARP_LOCK(cif);
1124 
1125 	if (carp_opts[CARPCTL_ARPBALANCE]) {
1126 		/*
1127 		 * XXX proof of concept implementation.
1128 		 * We use the source ip to decide which virtual host should
1129 		 * handle the request. If we're master of that virtual host,
1130 		 * then we respond, otherwise, just drop the arp packet on
1131 		 * the floor.
1132 		 */
1133 		count = carp_addrcount(cif, ia, CARP_COUNT_RUNNING);
1134 		if (count == 0) {
1135 			/* should never reach this */
1136 			CARP_UNLOCK(cif);
1137 			return (0);
1138 		}
1139 
1140 		/* this should be a hash, like pf_hash() */
1141 		index = ntohl(isaddr->s_addr) % count;
1142 		count = 0;
1143 
1144 		TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1145 			if ((SC2IFP(vh)->if_flags & IFF_UP) &&
1146 			    (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING)) {
1147 				TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist,
1148 				    ifa_list) {
1149 					if (ifa->ifa_addr->sa_family ==
1150 					    AF_INET &&
1151 					    ia->ia_addr.sin_addr.s_addr ==
1152 					    ifatoia(ifa)->ia_addr.sin_addr.s_addr) {
1153 						if (count == index) {
1154 							if (vh->sc_state ==
1155 							    MASTER) {
1156 								*enaddr = IF_LLADDR(vh->sc_ifp);
1157 								CARP_UNLOCK(cif);
1158 								return (1);
1159 							} else {
1160 								CARP_UNLOCK(cif);
1161 								return (0);
1162 							}
1163 						}
1164 						count++;
1165 					}
1166 				}
1167 			}
1168 		}
1169 	} else {
1170 		TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1171 			if ((SC2IFP(vh)->if_flags & IFF_UP) &&
1172 			    (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
1173 			    ia->ia_ifp == SC2IFP(vh) &&
1174 			    vh->sc_state == MASTER) {
1175 				*enaddr = IF_LLADDR(vh->sc_ifp);
1176 				CARP_UNLOCK(cif);
1177 				return (1);
1178 			}
1179 		}
1180 	}
1181 	CARP_UNLOCK(cif);
1182 	return (0);
1183 }
1184 
1185 #ifdef INET6
1186 struct ifaddr *
1187 carp_iamatch6(void *v, struct in6_addr *taddr)
1188 {
1189 	struct carp_if *cif = v;
1190 	struct carp_softc *vh;
1191 	struct ifaddr *ifa;
1192 
1193 	CARP_LOCK(cif);
1194 	TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1195 		TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist, ifa_list) {
1196 			if (IN6_ARE_ADDR_EQUAL(taddr,
1197 			    &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1198  			    (SC2IFP(vh)->if_flags & IFF_UP) &&
1199 			    (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
1200 			    vh->sc_state == MASTER) {
1201 			    	CARP_UNLOCK(cif);
1202 				return (ifa);
1203 			}
1204 		}
1205 	}
1206 	CARP_UNLOCK(cif);
1207 
1208 	return (NULL);
1209 }
1210 
1211 void *
1212 carp_macmatch6(void *v, struct mbuf *m, const struct in6_addr *taddr)
1213 {
1214 	struct m_tag *mtag;
1215 	struct carp_if *cif = v;
1216 	struct carp_softc *sc;
1217 	struct ifaddr *ifa;
1218 
1219 	CARP_LOCK(cif);
1220 	TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
1221 		TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1222 			if (IN6_ARE_ADDR_EQUAL(taddr,
1223 			    &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1224  			    (SC2IFP(sc)->if_flags & IFF_UP) &&
1225 			    (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING)) {
1226 				struct ifnet *ifp = SC2IFP(sc);
1227 				mtag = m_tag_get(PACKET_TAG_CARP,
1228 				    sizeof(struct ifnet *), M_NOWAIT);
1229 				if (mtag == NULL) {
1230 					/* better a bit than nothing */
1231 					CARP_UNLOCK(cif);
1232 					return (IF_LLADDR(sc->sc_ifp));
1233 				}
1234 				bcopy(&ifp, (caddr_t)(mtag + 1),
1235 				    sizeof(struct ifnet *));
1236 				m_tag_prepend(m, mtag);
1237 
1238 				CARP_UNLOCK(cif);
1239 				return (IF_LLADDR(sc->sc_ifp));
1240 			}
1241 		}
1242 	}
1243 	CARP_UNLOCK(cif);
1244 
1245 	return (NULL);
1246 }
1247 #endif
1248 
1249 struct ifnet *
1250 carp_forus(void *v, void *dhost)
1251 {
1252 	struct carp_if *cif = v;
1253 	struct carp_softc *vh;
1254 	u_int8_t *ena = dhost;
1255 
1256 	if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1257 		return (NULL);
1258 
1259 	CARP_LOCK(cif);
1260 	TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list)
1261 		if ((SC2IFP(vh)->if_flags & IFF_UP) &&
1262 		    (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
1263 		    vh->sc_state == MASTER &&
1264 		    !bcmp(dhost, IF_LLADDR(vh->sc_ifp), ETHER_ADDR_LEN)) {
1265 		    	CARP_UNLOCK(cif);
1266 			return (SC2IFP(vh));
1267 		}
1268 
1269     	CARP_UNLOCK(cif);
1270 	return (NULL);
1271 }
1272 
1273 static void
1274 carp_master_down(void *v)
1275 {
1276 	struct carp_softc *sc = v;
1277 
1278 	CARP_SCLOCK(sc);
1279 	carp_master_down_locked(sc);
1280 	CARP_SCUNLOCK(sc);
1281 }
1282 
1283 static void
1284 carp_master_down_locked(struct carp_softc *sc)
1285 {
1286 	if (sc->sc_carpdev)
1287 		CARP_SCLOCK_ASSERT(sc);
1288 
1289 	switch (sc->sc_state) {
1290 	case INIT:
1291 		printf("%s: master_down event in INIT state\n",
1292 		    SC2IFP(sc)->if_xname);
1293 		break;
1294 	case MASTER:
1295 		break;
1296 	case BACKUP:
1297 		carp_set_state(sc, MASTER);
1298 		carp_send_ad_locked(sc);
1299 		carp_send_arp(sc);
1300 #ifdef INET6
1301 		carp_send_na(sc);
1302 #endif /* INET6 */
1303 		carp_setrun(sc, 0);
1304 		carp_setroute(sc, RTM_ADD);
1305 		break;
1306 	}
1307 }
1308 
1309 /*
1310  * When in backup state, af indicates whether to reset the master down timer
1311  * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1312  */
1313 static void
1314 carp_setrun(struct carp_softc *sc, sa_family_t af)
1315 {
1316 	struct timeval tv;
1317 
1318 	if (sc->sc_carpdev == NULL) {
1319 		SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1320 		carp_set_state(sc, INIT);
1321 		return;
1322 	} else
1323 		CARP_SCLOCK_ASSERT(sc);
1324 
1325 	if (SC2IFP(sc)->if_flags & IFF_UP &&
1326 	    sc->sc_vhid > 0 && (sc->sc_naddrs || sc->sc_naddrs6))
1327 		SC2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
1328 	else {
1329 		SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1330 		carp_setroute(sc, RTM_DELETE);
1331 		return;
1332 	}
1333 
1334 	switch (sc->sc_state) {
1335 	case INIT:
1336 		if (carp_opts[CARPCTL_PREEMPT] && !carp_suppress_preempt) {
1337 			carp_send_ad_locked(sc);
1338 			carp_send_arp(sc);
1339 #ifdef INET6
1340 			carp_send_na(sc);
1341 #endif /* INET6 */
1342 			CARP_DEBUG("%s: INIT -> MASTER (preempting)\n",
1343 			    SC2IFP(sc)->if_xname);
1344 			carp_set_state(sc, MASTER);
1345 			carp_setroute(sc, RTM_ADD);
1346 		} else {
1347 			CARP_DEBUG("%s: INIT -> BACKUP\n", SC2IFP(sc)->if_xname);
1348 			carp_set_state(sc, BACKUP);
1349 			carp_setroute(sc, RTM_DELETE);
1350 			carp_setrun(sc, 0);
1351 		}
1352 		break;
1353 	case BACKUP:
1354 		callout_stop(&sc->sc_ad_tmo);
1355 		tv.tv_sec = 3 * sc->sc_advbase;
1356 		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1357 		switch (af) {
1358 #ifdef INET
1359 		case AF_INET:
1360 			callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1361 			    carp_master_down, sc);
1362 			break;
1363 #endif /* INET */
1364 #ifdef INET6
1365 		case AF_INET6:
1366 			callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1367 			    carp_master_down, sc);
1368 			break;
1369 #endif /* INET6 */
1370 		default:
1371 			if (sc->sc_naddrs)
1372 				callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1373 				    carp_master_down, sc);
1374 			if (sc->sc_naddrs6)
1375 				callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1376 				    carp_master_down, sc);
1377 			break;
1378 		}
1379 		break;
1380 	case MASTER:
1381 		tv.tv_sec = sc->sc_advbase;
1382 		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1383 		callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1384 		    carp_send_ad, sc);
1385 		break;
1386 	}
1387 }
1388 
1389 static void
1390 carp_multicast_cleanup(struct carp_softc *sc)
1391 {
1392 	struct ip_moptions *imo = &sc->sc_imo;
1393 	u_int16_t n = imo->imo_num_memberships;
1394 
1395 	/* Clean up our own multicast memberships */
1396 	while (n-- > 0) {
1397 		if (imo->imo_membership[n] != NULL) {
1398 			in_delmulti(imo->imo_membership[n]);
1399 			imo->imo_membership[n] = NULL;
1400 		}
1401 	}
1402 	KASSERT(imo->imo_mfilters == NULL,
1403 	   ("%s: imo_mfilters != NULL", __func__));
1404 	imo->imo_num_memberships = 0;
1405 	imo->imo_multicast_ifp = NULL;
1406 }
1407 
1408 #ifdef INET6
1409 static void
1410 carp_multicast6_cleanup(struct carp_softc *sc)
1411 {
1412 	struct ip6_moptions *im6o = &sc->sc_im6o;
1413 
1414 	while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1415 		struct in6_multi_mship *imm =
1416 		    LIST_FIRST(&im6o->im6o_memberships);
1417 
1418 		LIST_REMOVE(imm, i6mm_chain);
1419 		in6_leavegroup(imm);
1420 	}
1421 	im6o->im6o_multicast_ifp = NULL;
1422 }
1423 #endif
1424 
1425 static int
1426 carp_set_addr(struct carp_softc *sc, struct sockaddr_in *sin)
1427 {
1428 	struct ifnet *ifp;
1429 	struct carp_if *cif;
1430 	struct in_ifaddr *ia, *ia_if;
1431 	struct ip_moptions *imo = &sc->sc_imo;
1432 	struct in_addr addr;
1433 	u_long iaddr = htonl(sin->sin_addr.s_addr);
1434 	int own, error;
1435 
1436 	if (sin->sin_addr.s_addr == 0) {
1437 		if (!(SC2IFP(sc)->if_flags & IFF_UP))
1438 			carp_set_state(sc, INIT);
1439 		if (sc->sc_naddrs)
1440 			SC2IFP(sc)->if_flags |= IFF_UP;
1441 		carp_setrun(sc, 0);
1442 		return (0);
1443 	}
1444 
1445 	/* we have to do it by hands to check we won't match on us */
1446 	ia_if = NULL; own = 0;
1447 	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
1448 		/* and, yeah, we need a multicast-capable iface too */
1449 		if (ia->ia_ifp != SC2IFP(sc) &&
1450 		    (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1451 		    (iaddr & ia->ia_subnetmask) == ia->ia_subnet) {
1452 			if (!ia_if)
1453 				ia_if = ia;
1454 			if (sin->sin_addr.s_addr ==
1455 			    ia->ia_addr.sin_addr.s_addr)
1456 				own++;
1457 		}
1458 	}
1459 
1460 	if (!ia_if)
1461 		return (EADDRNOTAVAIL);
1462 
1463 	ia = ia_if;
1464 	ifp = ia->ia_ifp;
1465 
1466 	if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1467 	    (imo->imo_multicast_ifp && imo->imo_multicast_ifp != ifp))
1468 		return (EADDRNOTAVAIL);
1469 
1470 	if (imo->imo_num_memberships == 0) {
1471 		addr.s_addr = htonl(INADDR_CARP_GROUP);
1472 		if ((imo->imo_membership[0] = in_addmulti(&addr, ifp)) == NULL)
1473 			return (ENOBUFS);
1474 		imo->imo_num_memberships++;
1475 		imo->imo_multicast_ifp = ifp;
1476 		imo->imo_multicast_ttl = CARP_DFLTTL;
1477 		imo->imo_multicast_loop = 0;
1478 	}
1479 
1480 	if (!ifp->if_carp) {
1481 
1482 		MALLOC(cif, struct carp_if *, sizeof(*cif), M_CARP,
1483 		    M_WAITOK|M_ZERO);
1484 		if (!cif) {
1485 			error = ENOBUFS;
1486 			goto cleanup;
1487 		}
1488 		if ((error = ifpromisc(ifp, 1))) {
1489 			FREE(cif, M_CARP);
1490 			goto cleanup;
1491 		}
1492 
1493 		CARP_LOCK_INIT(cif);
1494 		CARP_LOCK(cif);
1495 		cif->vhif_ifp = ifp;
1496 		TAILQ_INIT(&cif->vhif_vrs);
1497 		ifp->if_carp = cif;
1498 
1499 	} else {
1500 		struct carp_softc *vr;
1501 
1502 		cif = (struct carp_if *)ifp->if_carp;
1503 		CARP_LOCK(cif);
1504 		TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1505 			if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1506 				CARP_UNLOCK(cif);
1507 				error = EINVAL;
1508 				goto cleanup;
1509 			}
1510 	}
1511 	sc->sc_ia = ia;
1512 	sc->sc_carpdev = ifp;
1513 
1514 	{ /* XXX prevent endless loop if already in queue */
1515 	struct carp_softc *vr, *after = NULL;
1516 	int myself = 0;
1517 	cif = (struct carp_if *)ifp->if_carp;
1518 
1519 	/* XXX: cif should not change, right? So we still hold the lock */
1520 	CARP_LOCK_ASSERT(cif);
1521 
1522 	TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1523 		if (vr == sc)
1524 			myself = 1;
1525 		if (vr->sc_vhid < sc->sc_vhid)
1526 			after = vr;
1527 	}
1528 
1529 	if (!myself) {
1530 		/* We're trying to keep things in order */
1531 		if (after == NULL) {
1532 			TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1533 		} else {
1534 			TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1535 		}
1536 		cif->vhif_nvrs++;
1537 	}
1538 	}
1539 
1540 	sc->sc_naddrs++;
1541 	SC2IFP(sc)->if_flags |= IFF_UP;
1542 	if (own)
1543 		sc->sc_advskew = 0;
1544 	carp_sc_state_locked(sc);
1545 	carp_setrun(sc, 0);
1546 
1547 	CARP_UNLOCK(cif);
1548 
1549 	return (0);
1550 
1551 cleanup:
1552 	in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1553 	return (error);
1554 }
1555 
1556 static int
1557 carp_del_addr(struct carp_softc *sc, struct sockaddr_in *sin)
1558 {
1559 	int error = 0;
1560 
1561 	if (!--sc->sc_naddrs) {
1562 		struct carp_if *cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1563 		struct ip_moptions *imo = &sc->sc_imo;
1564 
1565 		CARP_LOCK(cif);
1566 		callout_stop(&sc->sc_ad_tmo);
1567 		SC2IFP(sc)->if_flags &= ~IFF_UP;
1568 		SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1569 		sc->sc_vhid = -1;
1570 		in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1571 		imo->imo_multicast_ifp = NULL;
1572 		TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1573 		if (!--cif->vhif_nvrs) {
1574 			sc->sc_carpdev->if_carp = NULL;
1575 			CARP_LOCK_DESTROY(cif);
1576 			FREE(cif, M_IFADDR);
1577 		} else {
1578 			CARP_UNLOCK(cif);
1579 		}
1580 	}
1581 
1582 	return (error);
1583 }
1584 
1585 #ifdef INET6
1586 static int
1587 carp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1588 {
1589 	struct ifnet *ifp;
1590 	struct carp_if *cif;
1591 	struct in6_ifaddr *ia, *ia_if;
1592 	struct ip6_moptions *im6o = &sc->sc_im6o;
1593 	struct in6_multi_mship *imm;
1594 	struct in6_addr in6;
1595 	int own, error;
1596 
1597 	if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1598 		if (!(SC2IFP(sc)->if_flags & IFF_UP))
1599 			carp_set_state(sc, INIT);
1600 		if (sc->sc_naddrs6)
1601 			SC2IFP(sc)->if_flags |= IFF_UP;
1602 		carp_setrun(sc, 0);
1603 		return (0);
1604 	}
1605 
1606 	/* we have to do it by hands to check we won't match on us */
1607 	ia_if = NULL; own = 0;
1608 	for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1609 		int i;
1610 
1611 		for (i = 0; i < 4; i++) {
1612 			if ((sin6->sin6_addr.s6_addr32[i] &
1613 			    ia->ia_prefixmask.sin6_addr.s6_addr32[i]) !=
1614 			    (ia->ia_addr.sin6_addr.s6_addr32[i] &
1615 			    ia->ia_prefixmask.sin6_addr.s6_addr32[i]))
1616 				break;
1617 		}
1618 		/* and, yeah, we need a multicast-capable iface too */
1619 		if (ia->ia_ifp != SC2IFP(sc) &&
1620 		    (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1621 		    (i == 4)) {
1622 			if (!ia_if)
1623 				ia_if = ia;
1624 			if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
1625 			    &ia->ia_addr.sin6_addr))
1626 				own++;
1627 		}
1628 	}
1629 
1630 	if (!ia_if)
1631 		return (EADDRNOTAVAIL);
1632 	ia = ia_if;
1633 	ifp = ia->ia_ifp;
1634 
1635 	if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1636 	    (im6o->im6o_multicast_ifp && im6o->im6o_multicast_ifp != ifp))
1637 		return (EADDRNOTAVAIL);
1638 
1639 	if (!sc->sc_naddrs6) {
1640 		im6o->im6o_multicast_ifp = ifp;
1641 
1642 		/* join CARP multicast address */
1643 		bzero(&in6, sizeof(in6));
1644 		in6.s6_addr16[0] = htons(0xff02);
1645 		in6.s6_addr8[15] = 0x12;
1646 		if (in6_setscope(&in6, ifp, NULL) != 0)
1647 			goto cleanup;
1648 		if ((imm = in6_joingroup(ifp, &in6, &error, 0)) == NULL)
1649 			goto cleanup;
1650 		LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1651 
1652 		/* join solicited multicast address */
1653 		bzero(&in6, sizeof(in6));
1654 		in6.s6_addr16[0] = htons(0xff02);
1655 		in6.s6_addr32[1] = 0;
1656 		in6.s6_addr32[2] = htonl(1);
1657 		in6.s6_addr32[3] = sin6->sin6_addr.s6_addr32[3];
1658 		in6.s6_addr8[12] = 0xff;
1659 		if (in6_setscope(&in6, ifp, NULL) != 0)
1660 			goto cleanup;
1661 		if ((imm = in6_joingroup(ifp, &in6, &error, 0)) == NULL)
1662 			goto cleanup;
1663 		LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1664 	}
1665 
1666 	if (!ifp->if_carp) {
1667 		MALLOC(cif, struct carp_if *, sizeof(*cif), M_CARP,
1668 		    M_WAITOK|M_ZERO);
1669 		if (!cif) {
1670 			error = ENOBUFS;
1671 			goto cleanup;
1672 		}
1673 		if ((error = ifpromisc(ifp, 1))) {
1674 			FREE(cif, M_CARP);
1675 			goto cleanup;
1676 		}
1677 
1678 		CARP_LOCK_INIT(cif);
1679 		CARP_LOCK(cif);
1680 		cif->vhif_ifp = ifp;
1681 		TAILQ_INIT(&cif->vhif_vrs);
1682 		ifp->if_carp = cif;
1683 
1684 	} else {
1685 		struct carp_softc *vr;
1686 
1687 		cif = (struct carp_if *)ifp->if_carp;
1688 		CARP_LOCK(cif);
1689 		TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1690 			if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1691 				CARP_UNLOCK(cif);
1692 				error = EINVAL;
1693 				goto cleanup;
1694 			}
1695 	}
1696 	sc->sc_ia6 = ia;
1697 	sc->sc_carpdev = ifp;
1698 
1699 	{ /* XXX prevent endless loop if already in queue */
1700 	struct carp_softc *vr, *after = NULL;
1701 	int myself = 0;
1702 	cif = (struct carp_if *)ifp->if_carp;
1703 	CARP_LOCK_ASSERT(cif);
1704 
1705 	TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1706 		if (vr == sc)
1707 			myself = 1;
1708 		if (vr->sc_vhid < sc->sc_vhid)
1709 			after = vr;
1710 	}
1711 
1712 	if (!myself) {
1713 		/* We're trying to keep things in order */
1714 		if (after == NULL) {
1715 			TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1716 		} else {
1717 			TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1718 		}
1719 		cif->vhif_nvrs++;
1720 	}
1721 	}
1722 
1723 	sc->sc_naddrs6++;
1724 	SC2IFP(sc)->if_flags |= IFF_UP;
1725 	if (own)
1726 		sc->sc_advskew = 0;
1727 	carp_sc_state_locked(sc);
1728 	carp_setrun(sc, 0);
1729 
1730 	CARP_UNLOCK(cif);
1731 
1732 	return (0);
1733 
1734 cleanup:
1735 	/* clean up multicast memberships */
1736 	if (!sc->sc_naddrs6) {
1737 		while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1738 			imm = LIST_FIRST(&im6o->im6o_memberships);
1739 			LIST_REMOVE(imm, i6mm_chain);
1740 			in6_leavegroup(imm);
1741 		}
1742 	}
1743 	return (error);
1744 }
1745 
1746 static int
1747 carp_del_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1748 {
1749 	int error = 0;
1750 
1751 	if (!--sc->sc_naddrs6) {
1752 		struct carp_if *cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1753 		struct ip6_moptions *im6o = &sc->sc_im6o;
1754 
1755 		CARP_LOCK(cif);
1756 		callout_stop(&sc->sc_ad_tmo);
1757 		SC2IFP(sc)->if_flags &= ~IFF_UP;
1758 		SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1759 		sc->sc_vhid = -1;
1760 		while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1761 			struct in6_multi_mship *imm =
1762 			    LIST_FIRST(&im6o->im6o_memberships);
1763 
1764 			LIST_REMOVE(imm, i6mm_chain);
1765 			in6_leavegroup(imm);
1766 		}
1767 		im6o->im6o_multicast_ifp = NULL;
1768 		TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1769 		if (!--cif->vhif_nvrs) {
1770 			CARP_LOCK_DESTROY(cif);
1771 			sc->sc_carpdev->if_carp = NULL;
1772 			FREE(cif, M_IFADDR);
1773 		} else
1774 			CARP_UNLOCK(cif);
1775 	}
1776 
1777 	return (error);
1778 }
1779 #endif /* INET6 */
1780 
1781 static int
1782 carp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr)
1783 {
1784 	struct carp_softc *sc = ifp->if_softc, *vr;
1785 	struct carpreq carpr;
1786 	struct ifaddr *ifa;
1787 	struct ifreq *ifr;
1788 	struct ifaliasreq *ifra;
1789 	int locked = 0, error = 0;
1790 
1791 	ifa = (struct ifaddr *)addr;
1792 	ifra = (struct ifaliasreq *)addr;
1793 	ifr = (struct ifreq *)addr;
1794 
1795 	switch (cmd) {
1796 	case SIOCSIFADDR:
1797 		switch (ifa->ifa_addr->sa_family) {
1798 #ifdef INET
1799 		case AF_INET:
1800 			SC2IFP(sc)->if_flags |= IFF_UP;
1801 			bcopy(ifa->ifa_addr, ifa->ifa_dstaddr,
1802 			    sizeof(struct sockaddr));
1803 			error = carp_set_addr(sc, satosin(ifa->ifa_addr));
1804 			break;
1805 #endif /* INET */
1806 #ifdef INET6
1807 		case AF_INET6:
1808 			SC2IFP(sc)->if_flags |= IFF_UP;
1809 			error = carp_set_addr6(sc, satosin6(ifa->ifa_addr));
1810 			break;
1811 #endif /* INET6 */
1812 		default:
1813 			error = EAFNOSUPPORT;
1814 			break;
1815 		}
1816 		break;
1817 
1818 	case SIOCAIFADDR:
1819 		switch (ifa->ifa_addr->sa_family) {
1820 #ifdef INET
1821 		case AF_INET:
1822 			SC2IFP(sc)->if_flags |= IFF_UP;
1823 			bcopy(ifa->ifa_addr, ifa->ifa_dstaddr,
1824 			    sizeof(struct sockaddr));
1825 			error = carp_set_addr(sc, satosin(&ifra->ifra_addr));
1826 			break;
1827 #endif /* INET */
1828 #ifdef INET6
1829 		case AF_INET6:
1830 			SC2IFP(sc)->if_flags |= IFF_UP;
1831 			error = carp_set_addr6(sc, satosin6(&ifra->ifra_addr));
1832 			break;
1833 #endif /* INET6 */
1834 		default:
1835 			error = EAFNOSUPPORT;
1836 			break;
1837 		}
1838 		break;
1839 
1840 	case SIOCDIFADDR:
1841 		switch (ifa->ifa_addr->sa_family) {
1842 #ifdef INET
1843 		case AF_INET:
1844 			error = carp_del_addr(sc, satosin(&ifra->ifra_addr));
1845 			break;
1846 #endif /* INET */
1847 #ifdef INET6
1848 		case AF_INET6:
1849 			error = carp_del_addr6(sc, satosin6(&ifra->ifra_addr));
1850 			break;
1851 #endif /* INET6 */
1852 		default:
1853 			error = EAFNOSUPPORT;
1854 			break;
1855 		}
1856 		break;
1857 
1858 	case SIOCSIFFLAGS:
1859 		if (sc->sc_carpdev) {
1860 			locked = 1;
1861 			CARP_SCLOCK(sc);
1862 		}
1863 		if (sc->sc_state != INIT && !(ifr->ifr_flags & IFF_UP)) {
1864  			callout_stop(&sc->sc_ad_tmo);
1865  			callout_stop(&sc->sc_md_tmo);
1866  			callout_stop(&sc->sc_md6_tmo);
1867 			if (sc->sc_state == MASTER)
1868 				carp_send_ad_locked(sc);
1869 			carp_set_state(sc, INIT);
1870 			carp_setrun(sc, 0);
1871 		} else if (sc->sc_state == INIT && (ifr->ifr_flags & IFF_UP)) {
1872 			SC2IFP(sc)->if_flags |= IFF_UP;
1873 			carp_setrun(sc, 0);
1874 		}
1875 		break;
1876 
1877 	case SIOCSVH:
1878 		error = priv_check(curthread, PRIV_NETINET_CARP);
1879 		if (error)
1880 			break;
1881 		if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
1882 			break;
1883 		error = 1;
1884 		if (sc->sc_carpdev) {
1885 			locked = 1;
1886 			CARP_SCLOCK(sc);
1887 		}
1888 		if (sc->sc_state != INIT && carpr.carpr_state != sc->sc_state) {
1889 			switch (carpr.carpr_state) {
1890 			case BACKUP:
1891 				callout_stop(&sc->sc_ad_tmo);
1892 				carp_set_state(sc, BACKUP);
1893 				carp_setrun(sc, 0);
1894 				carp_setroute(sc, RTM_DELETE);
1895 				break;
1896 			case MASTER:
1897 				carp_master_down_locked(sc);
1898 				break;
1899 			default:
1900 				break;
1901 			}
1902 		}
1903 		if (carpr.carpr_vhid > 0) {
1904 			if (carpr.carpr_vhid > 255) {
1905 				error = EINVAL;
1906 				break;
1907 			}
1908 			if (sc->sc_carpdev) {
1909 				struct carp_if *cif;
1910 				cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1911 				TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1912 					if (vr != sc &&
1913 					    vr->sc_vhid == carpr.carpr_vhid) {
1914 						error = EEXIST;
1915 						break;
1916 					}
1917 				if (error == EEXIST)
1918 					break;
1919 			}
1920 			sc->sc_vhid = carpr.carpr_vhid;
1921 			IF_LLADDR(sc->sc_ifp)[0] = 0;
1922 			IF_LLADDR(sc->sc_ifp)[1] = 0;
1923 			IF_LLADDR(sc->sc_ifp)[2] = 0x5e;
1924 			IF_LLADDR(sc->sc_ifp)[3] = 0;
1925 			IF_LLADDR(sc->sc_ifp)[4] = 1;
1926 			IF_LLADDR(sc->sc_ifp)[5] = sc->sc_vhid;
1927 			error--;
1928 		}
1929 		if (carpr.carpr_advbase > 0 || carpr.carpr_advskew > 0) {
1930 			if (carpr.carpr_advskew >= 255) {
1931 				error = EINVAL;
1932 				break;
1933 			}
1934 			if (carpr.carpr_advbase > 255) {
1935 				error = EINVAL;
1936 				break;
1937 			}
1938 			sc->sc_advbase = carpr.carpr_advbase;
1939 			sc->sc_advskew = carpr.carpr_advskew;
1940 			error--;
1941 		}
1942 		bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1943 		if (error > 0)
1944 			error = EINVAL;
1945 		else {
1946 			error = 0;
1947 			carp_setrun(sc, 0);
1948 		}
1949 		break;
1950 
1951 	case SIOCGVH:
1952 		/* XXX: lockless read */
1953 		bzero(&carpr, sizeof(carpr));
1954 		carpr.carpr_state = sc->sc_state;
1955 		carpr.carpr_vhid = sc->sc_vhid;
1956 		carpr.carpr_advbase = sc->sc_advbase;
1957 		carpr.carpr_advskew = sc->sc_advskew;
1958 		error = priv_check(curthread, PRIV_NETINET_CARP);
1959 		if (error == 0)
1960 			bcopy(sc->sc_key, carpr.carpr_key,
1961 			    sizeof(carpr.carpr_key));
1962 		error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
1963 		break;
1964 
1965 	default:
1966 		error = EINVAL;
1967 	}
1968 
1969 	if (locked)
1970 		CARP_SCUNLOCK(sc);
1971 
1972 	carp_hmac_prepare(sc);
1973 
1974 	return (error);
1975 }
1976 
1977 /*
1978  * XXX: this is looutput. We should eventually use it from there.
1979  */
1980 static int
1981 carp_looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
1982     struct rtentry *rt)
1983 {
1984 	u_int32_t af;
1985 
1986 	M_ASSERTPKTHDR(m); /* check if we have the packet header */
1987 
1988 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
1989 		m_freem(m);
1990 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
1991 			rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1992 	}
1993 
1994 	ifp->if_opackets++;
1995 	ifp->if_obytes += m->m_pkthdr.len;
1996 
1997 	/* BPF writes need to be handled specially. */
1998 	if (dst->sa_family == AF_UNSPEC) {
1999 		bcopy(dst->sa_data, &af, sizeof(af));
2000 		dst->sa_family = af;
2001 	}
2002 
2003 #if 1	/* XXX */
2004 	switch (dst->sa_family) {
2005 	case AF_INET:
2006 	case AF_INET6:
2007 	case AF_IPX:
2008 	case AF_APPLETALK:
2009 		break;
2010 	default:
2011 		printf("carp_looutput: af=%d unexpected\n", dst->sa_family);
2012 		m_freem(m);
2013 		return (EAFNOSUPPORT);
2014 	}
2015 #endif
2016 	return(if_simloop(ifp, m, dst->sa_family, 0));
2017 }
2018 
2019 /*
2020  * Start output on carp interface. This function should never be called.
2021  */
2022 static void
2023 carp_start(struct ifnet *ifp)
2024 {
2025 #ifdef DEBUG
2026 	printf("%s: start called\n", ifp->if_xname);
2027 #endif
2028 }
2029 
2030 int
2031 carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
2032     struct rtentry *rt)
2033 {
2034 	struct m_tag *mtag;
2035 	struct carp_softc *sc;
2036 	struct ifnet *carp_ifp;
2037 
2038 	if (!sa)
2039 		return (0);
2040 
2041 	switch (sa->sa_family) {
2042 #ifdef INET
2043 	case AF_INET:
2044 		break;
2045 #endif /* INET */
2046 #ifdef INET6
2047 	case AF_INET6:
2048 		break;
2049 #endif /* INET6 */
2050 	default:
2051 		return (0);
2052 	}
2053 
2054 	mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
2055 	if (mtag == NULL)
2056 		return (0);
2057 
2058 	bcopy(mtag + 1, &carp_ifp, sizeof(struct ifnet *));
2059 	sc = carp_ifp->if_softc;
2060 
2061 	/* Set the source MAC address to Virtual Router MAC Address */
2062 	switch (ifp->if_type) {
2063 	case IFT_ETHER:
2064 	case IFT_L2VLAN: {
2065 			struct ether_header *eh;
2066 
2067 			eh = mtod(m, struct ether_header *);
2068 			eh->ether_shost[0] = 0;
2069 			eh->ether_shost[1] = 0;
2070 			eh->ether_shost[2] = 0x5e;
2071 			eh->ether_shost[3] = 0;
2072 			eh->ether_shost[4] = 1;
2073 			eh->ether_shost[5] = sc->sc_vhid;
2074 		}
2075 		break;
2076 	case IFT_FDDI: {
2077 			struct fddi_header *fh;
2078 
2079 			fh = mtod(m, struct fddi_header *);
2080 			fh->fddi_shost[0] = 0;
2081 			fh->fddi_shost[1] = 0;
2082 			fh->fddi_shost[2] = 0x5e;
2083 			fh->fddi_shost[3] = 0;
2084 			fh->fddi_shost[4] = 1;
2085 			fh->fddi_shost[5] = sc->sc_vhid;
2086 		}
2087 		break;
2088 	case IFT_ISO88025: {
2089  			struct iso88025_header *th;
2090  			th = mtod(m, struct iso88025_header *);
2091 			th->iso88025_shost[0] = 3;
2092 			th->iso88025_shost[1] = 0;
2093 			th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1);
2094 			th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1);
2095 			th->iso88025_shost[4] = 0;
2096 			th->iso88025_shost[5] = 0;
2097 		}
2098 		break;
2099 	default:
2100 		printf("%s: carp is not supported for this interface type\n",
2101 		    ifp->if_xname);
2102 		return (EOPNOTSUPP);
2103 	}
2104 
2105 	return (0);
2106 }
2107 
2108 static void
2109 carp_set_state(struct carp_softc *sc, int state)
2110 {
2111 
2112 	if (sc->sc_carpdev)
2113 		CARP_SCLOCK_ASSERT(sc);
2114 
2115 	if (sc->sc_state == state)
2116 		return;
2117 
2118 	sc->sc_state = state;
2119 	switch (state) {
2120 	case BACKUP:
2121 		SC2IFP(sc)->if_link_state = LINK_STATE_DOWN;
2122 		break;
2123 	case MASTER:
2124 		SC2IFP(sc)->if_link_state = LINK_STATE_UP;
2125 		break;
2126 	default:
2127 		SC2IFP(sc)->if_link_state = LINK_STATE_UNKNOWN;
2128 		break;
2129 	}
2130 	rt_ifmsg(SC2IFP(sc));
2131 }
2132 
2133 void
2134 carp_carpdev_state(void *v)
2135 {
2136 	struct carp_if *cif = v;
2137 
2138 	CARP_LOCK(cif);
2139 	carp_carpdev_state_locked(cif);
2140 	CARP_UNLOCK(cif);
2141 }
2142 
2143 static void
2144 carp_carpdev_state_locked(struct carp_if *cif)
2145 {
2146 	struct carp_softc *sc;
2147 
2148 	TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list)
2149 		carp_sc_state_locked(sc);
2150 }
2151 
2152 static void
2153 carp_sc_state_locked(struct carp_softc *sc)
2154 {
2155 	CARP_SCLOCK_ASSERT(sc);
2156 
2157 	if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
2158 	    !(sc->sc_carpdev->if_flags & IFF_UP)) {
2159 		sc->sc_flags_backup = SC2IFP(sc)->if_flags;
2160 		SC2IFP(sc)->if_flags &= ~IFF_UP;
2161 		SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
2162 		callout_stop(&sc->sc_ad_tmo);
2163 		callout_stop(&sc->sc_md_tmo);
2164 		callout_stop(&sc->sc_md6_tmo);
2165 		carp_set_state(sc, INIT);
2166 		carp_setrun(sc, 0);
2167 		if (!sc->sc_suppress) {
2168 			carp_suppress_preempt++;
2169 			if (carp_suppress_preempt == 1) {
2170 				CARP_SCUNLOCK(sc);
2171 				carp_send_ad_all();
2172 				CARP_SCLOCK(sc);
2173 			}
2174 		}
2175 		sc->sc_suppress = 1;
2176 	} else {
2177 		SC2IFP(sc)->if_flags |= sc->sc_flags_backup;
2178 		carp_set_state(sc, INIT);
2179 		carp_setrun(sc, 0);
2180 		if (sc->sc_suppress)
2181 			carp_suppress_preempt--;
2182 		sc->sc_suppress = 0;
2183 	}
2184 
2185 	return;
2186 }
2187 
2188 static int
2189 carp_modevent(module_t mod, int type, void *data)
2190 {
2191 	switch (type) {
2192 	case MOD_LOAD:
2193 		if_detach_event_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
2194 		    carp_ifdetach, NULL, EVENTHANDLER_PRI_ANY);
2195 		if (if_detach_event_tag == NULL)
2196 			return (ENOMEM);
2197 		mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
2198 		LIST_INIT(&carpif_list);
2199 		if_clone_attach(&carp_cloner);
2200 		break;
2201 
2202 	case MOD_UNLOAD:
2203 		EVENTHANDLER_DEREGISTER(ifnet_departure_event, if_detach_event_tag);
2204 		if_clone_detach(&carp_cloner);
2205 		mtx_destroy(&carp_mtx);
2206 		break;
2207 
2208 	default:
2209 		return (EINVAL);
2210 	}
2211 
2212 	return (0);
2213 }
2214 
2215 static moduledata_t carp_mod = {
2216 	"carp",
2217 	carp_modevent,
2218 	0
2219 };
2220 
2221 DECLARE_MODULE(carp, carp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
2222