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