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