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