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