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