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