xref: /netbsd-src/sys/netinet/ip_carp.c (revision 2e2322c9c07009df921d11b1268f8506affbb8ba)
1 /*	$NetBSD: ip_carp.c,v 1.80 2016/12/12 03:55:57 ozaki-r 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.80 2016/12/12 03:55:57 ozaki-r 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 
694 		CARP_STATINC(CARP_STAT_BADAUTH);
695 		sc->sc_if.if_ierrors++;
696 
697 		switch(af) {
698 
699 		case AF_INET:
700 			ip = mtod(m, struct ip *);
701 			CARP_LOG(sc, ("incorrect hash from %s",
702 			    in_fmtaddr(ip->ip_src)));
703 			break;
704 
705 		case AF_INET6:
706 			ip6 = mtod(m, struct ip6_hdr *);
707 			CARP_LOG(sc, ("incorrect hash from %s",
708 				ip6_sprintf(&ip6->ip6_src)));
709 			break;
710 
711 		default: CARP_LOG(sc, ("incorrect hash"));
712 			break;
713 		}
714 		m_freem(m);
715 		return;
716 	}
717 
718 	tmp_counter = ntohl(ch->carp_counter[0]);
719 	tmp_counter = tmp_counter<<32;
720 	tmp_counter += ntohl(ch->carp_counter[1]);
721 
722 	/* XXX Replay protection goes here */
723 
724 	sc->sc_init_counter = 0;
725 	sc->sc_counter = tmp_counter;
726 
727 
728 	sc_tv.tv_sec = sc->sc_advbase;
729 	if (carp_suppress_preempt && sc->sc_advskew <  240)
730 		sc_tv.tv_usec = 240 * 1000000 / 256;
731 	else
732 		sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256;
733 	ch_tv.tv_sec = ch->carp_advbase;
734 	ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
735 
736 	switch (sc->sc_state) {
737 	case INIT:
738 		break;
739 	case MASTER:
740 		/*
741 		 * If we receive an advertisement from a backup who's going to
742 		 * be more frequent than us, go into BACKUP state.
743 		 */
744 		if (timercmp(&sc_tv, &ch_tv, >) ||
745 		    timercmp(&sc_tv, &ch_tv, ==)) {
746 			callout_stop(&sc->sc_ad_tmo);
747 			CARP_LOG(sc, ("MASTER -> BACKUP (more frequent advertisement received)"));
748 			carp_set_state(sc, BACKUP);
749 			carp_setrun(sc, 0);
750 			carp_setroute(sc, RTM_DELETE);
751 		}
752 		break;
753 	case BACKUP:
754 		/*
755 		 * If we're pre-empting masters who advertise slower than us,
756 		 * and this one claims to be slower, treat him as down.
757 		 */
758 		if (carp_opts[CARPCTL_PREEMPT] && timercmp(&sc_tv, &ch_tv, <)) {
759 			CARP_LOG(sc, ("BACKUP -> MASTER (preempting a slower master)"));
760 			carp_master_down(sc);
761 			break;
762 		}
763 
764 		/*
765 		 *  If the master is going to advertise at such a low frequency
766 		 *  that he's guaranteed to time out, we'd might as well just
767 		 *  treat him as timed out now.
768 		 */
769 		sc_tv.tv_sec = sc->sc_advbase * 3;
770 		if (timercmp(&sc_tv, &ch_tv, <)) {
771 			CARP_LOG(sc, ("BACKUP -> MASTER (master timed out)"));
772 			carp_master_down(sc);
773 			break;
774 		}
775 
776 		/*
777 		 * Otherwise, we reset the counter and wait for the next
778 		 * advertisement.
779 		 */
780 		carp_setrun(sc, af);
781 		break;
782 	}
783 
784 	m_freem(m);
785 	return;
786 }
787 
788 /*
789  * Interface side of the CARP implementation.
790  */
791 
792 /* ARGSUSED */
793 void
794 carpattach(int n)
795 {
796 	if_clone_attach(&carp_cloner);
797 
798 	carpstat_percpu = percpu_alloc(sizeof(uint64_t) * CARP_NSTATS);
799 }
800 
801 static int
802 carp_clone_create(struct if_clone *ifc, int unit)
803 {
804 	extern int ifqmaxlen;
805 	struct carp_softc *sc;
806 	struct ifnet *ifp;
807 
808 	sc = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT|M_ZERO);
809 	if (!sc)
810 		return (ENOMEM);
811 
812 	sc->sc_suppress = 0;
813 	sc->sc_advbase = CARP_DFLTINTV;
814 	sc->sc_vhid = -1;	/* required setting */
815 	sc->sc_advskew = 0;
816 	sc->sc_init_counter = 1;
817 	sc->sc_naddrs = sc->sc_naddrs6 = 0;
818 #ifdef INET6
819 	sc->sc_im6o.im6o_multicast_hlim = CARP_DFLTTL;
820 #endif /* INET6 */
821 
822 	callout_init(&sc->sc_ad_tmo, 0);
823 	callout_init(&sc->sc_md_tmo, 0);
824 	callout_init(&sc->sc_md6_tmo, 0);
825 
826 	callout_setfunc(&sc->sc_ad_tmo, carp_send_ad, sc);
827 	callout_setfunc(&sc->sc_md_tmo, carp_master_down, sc);
828 	callout_setfunc(&sc->sc_md6_tmo, carp_master_down, sc);
829 
830 	LIST_INIT(&sc->carp_mc_listhead);
831 	ifp = &sc->sc_if;
832 	ifp->if_softc = sc;
833 	snprintf(ifp->if_xname, sizeof ifp->if_xname, "%s%d", ifc->ifc_name,
834 	    unit);
835 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
836 	ifp->if_ioctl = carp_ioctl;
837 	ifp->if_start = carp_start;
838 	ifp->if_output = carp_output;
839 	ifp->if_type = IFT_CARP;
840 	ifp->if_addrlen = ETHER_ADDR_LEN;
841 	ifp->if_hdrlen = ETHER_HDR_LEN;
842 	ifp->if_mtu = ETHERMTU;
843 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
844 	IFQ_SET_READY(&ifp->if_snd);
845 	if_attach(ifp);
846 
847 	if_alloc_sadl(ifp);
848 	ifp->if_broadcastaddr = etherbroadcastaddr;
849 	carp_set_enaddr(sc);
850 	LIST_INIT(&sc->sc_ac.ec_multiaddrs);
851 	bpf_attach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
852 #ifdef MBUFTRACE
853 	strlcpy(sc->sc_ac.ec_tx_mowner.mo_name, ifp->if_xname,
854 	    sizeof(sc->sc_ac.ec_tx_mowner.mo_name));
855 	strlcpy(sc->sc_ac.ec_tx_mowner.mo_descr, "tx",
856 	    sizeof(sc->sc_ac.ec_tx_mowner.mo_descr));
857 	strlcpy(sc->sc_ac.ec_rx_mowner.mo_name, ifp->if_xname,
858 	    sizeof(sc->sc_ac.ec_rx_mowner.mo_name));
859 	strlcpy(sc->sc_ac.ec_rx_mowner.mo_descr, "rx",
860 	    sizeof(sc->sc_ac.ec_rx_mowner.mo_descr));
861 	MOWNER_ATTACH(&sc->sc_ac.ec_tx_mowner);
862 	MOWNER_ATTACH(&sc->sc_ac.ec_rx_mowner);
863 	ifp->if_mowner = &sc->sc_ac.ec_tx_mowner;
864 #endif
865 	return (0);
866 }
867 
868 static int
869 carp_clone_destroy(struct ifnet *ifp)
870 {
871 	struct carp_softc *sc = ifp->if_softc;
872 
873 	carpdetach(ifp->if_softc);
874 	ether_ifdetach(ifp);
875 	if_detach(ifp);
876 	callout_destroy(&sc->sc_ad_tmo);
877 	callout_destroy(&sc->sc_md_tmo);
878 	callout_destroy(&sc->sc_md6_tmo);
879 	free(ifp->if_softc, M_DEVBUF);
880 
881 	return (0);
882 }
883 
884 static void
885 carpdetach(struct carp_softc *sc)
886 {
887 	struct carp_if *cif;
888 	int s;
889 
890 	callout_stop(&sc->sc_ad_tmo);
891 	callout_stop(&sc->sc_md_tmo);
892 	callout_stop(&sc->sc_md6_tmo);
893 
894 	if (sc->sc_suppress)
895 		carp_suppress_preempt--;
896 	sc->sc_suppress = 0;
897 
898 	if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS)
899 		carp_suppress_preempt--;
900 	sc->sc_sendad_errors = 0;
901 
902 	carp_set_state(sc, INIT);
903 	sc->sc_if.if_flags &= ~IFF_UP;
904 	carp_setrun(sc, 0);
905 	carp_multicast_cleanup(sc);
906 
907 	KERNEL_LOCK(1, NULL);
908 	s = splnet();
909 	if (sc->sc_carpdev != NULL) {
910 		/* XXX linkstatehook removal */
911 		cif = (struct carp_if *)sc->sc_carpdev->if_carp;
912 		TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
913 		if (!--cif->vhif_nvrs) {
914 			ifpromisc(sc->sc_carpdev, 0);
915 			sc->sc_carpdev->if_carp = NULL;
916 			free(cif, M_IFADDR);
917 		}
918 	}
919 	sc->sc_carpdev = NULL;
920 	splx(s);
921 	KERNEL_UNLOCK_ONE(NULL);
922 }
923 
924 /* Detach an interface from the carp. */
925 void
926 carp_ifdetach(struct ifnet *ifp)
927 {
928 	struct carp_softc *sc, *nextsc;
929 	struct carp_if *cif = (struct carp_if *)ifp->if_carp;
930 
931 	for (sc = TAILQ_FIRST(&cif->vhif_vrs); sc; sc = nextsc) {
932 		nextsc = TAILQ_NEXT(sc, sc_list);
933 		carpdetach(sc);
934 	}
935 }
936 
937 static int
938 carp_prepare_ad(struct mbuf *m, struct carp_softc *sc,
939     struct carp_header *ch)
940 {
941 	if (sc->sc_init_counter) {
942 		/* this could also be seconds since unix epoch */
943 		sc->sc_counter = cprng_fast64();
944 	} else
945 		sc->sc_counter++;
946 
947 	ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
948 	ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
949 
950 	carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
951 
952 	return (0);
953 }
954 
955 static void
956 carp_send_ad_all(void)
957 {
958 	struct ifnet *ifp;
959 	struct carp_if *cif;
960 	struct carp_softc *vh;
961 	int s;
962 	int bound = curlwp_bind();
963 
964 	s = pserialize_read_enter();
965 	IFNET_READER_FOREACH(ifp) {
966 		struct psref psref;
967 		if (ifp->if_carp == NULL || ifp->if_type == IFT_CARP)
968 			continue;
969 
970 		psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class);
971 		pserialize_read_exit(s);
972 
973 		cif = (struct carp_if *)ifp->if_carp;
974 		TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
975 			if ((vh->sc_if.if_flags & (IFF_UP|IFF_RUNNING)) ==
976 			    (IFF_UP|IFF_RUNNING) && vh->sc_state == MASTER)
977 				carp_send_ad(vh);
978 		}
979 
980 		s = pserialize_read_enter();
981 		psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
982 	}
983 	pserialize_read_exit(s);
984 	curlwp_bindx(bound);
985 }
986 
987 
988 static void
989 carp_send_ad(void *v)
990 {
991 	struct carp_header ch;
992 	struct timeval tv;
993 	struct carp_softc *sc = v;
994 	struct carp_header *ch_ptr;
995 	struct mbuf *m;
996 	int error, len, advbase, advskew, s;
997 	struct sockaddr sa;
998 
999 	KERNEL_LOCK(1, NULL);
1000 	s = splsoftnet();
1001 
1002 	advbase = advskew = 0; /* Sssssh compiler */
1003 	if (sc->sc_carpdev == NULL) {
1004 		sc->sc_if.if_oerrors++;
1005 		goto retry_later;
1006 	}
1007 
1008 	/* bow out if we've gone to backup (the carp interface is going down) */
1009 	if (sc->sc_bow_out) {
1010 		sc->sc_bow_out = 0;
1011 		advbase = 255;
1012 		advskew = 255;
1013 	} else {
1014 		advbase = sc->sc_advbase;
1015 		if (!carp_suppress_preempt || sc->sc_advskew > 240)
1016 			advskew = sc->sc_advskew;
1017 		else
1018 			advskew = 240;
1019 		tv.tv_sec = advbase;
1020 		tv.tv_usec = advskew * 1000000 / 256;
1021 	}
1022 
1023 	ch.carp_version = CARP_VERSION;
1024 	ch.carp_type = CARP_ADVERTISEMENT;
1025 	ch.carp_vhid = sc->sc_vhid;
1026 	ch.carp_advbase = advbase;
1027 	ch.carp_advskew = advskew;
1028 	ch.carp_authlen = 7;	/* XXX DEFINE */
1029 	ch.carp_pad1 = 0;	/* must be zero */
1030 	ch.carp_cksum = 0;
1031 
1032 
1033 #ifdef INET
1034 	if (sc->sc_naddrs) {
1035 		struct ip *ip;
1036 		struct ifaddr *ifa;
1037 		int _s;
1038 
1039 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
1040 		if (m == NULL) {
1041 			sc->sc_if.if_oerrors++;
1042 			CARP_STATINC(CARP_STAT_ONOMEM);
1043 			/* XXX maybe less ? */
1044 			goto retry_later;
1045 		}
1046 		MCLAIM(m, &carp_proto_mowner_tx);
1047 		len = sizeof(*ip) + sizeof(ch);
1048 		m->m_pkthdr.len = len;
1049 		m_reset_rcvif(m);
1050 		m->m_len = len;
1051 		MH_ALIGN(m, m->m_len);
1052 		m->m_flags |= M_MCAST;
1053 		ip = mtod(m, struct ip *);
1054 		ip->ip_v = IPVERSION;
1055 		ip->ip_hl = sizeof(*ip) >> 2;
1056 		ip->ip_tos = IPTOS_LOWDELAY;
1057 		ip->ip_len = htons(len);
1058 		ip->ip_id = 0;	/* no need for id, we don't support fragments */
1059 		ip->ip_off = htons(IP_DF);
1060 		ip->ip_ttl = CARP_DFLTTL;
1061 		ip->ip_p = IPPROTO_CARP;
1062 		ip->ip_sum = 0;
1063 
1064 		memset(&sa, 0, sizeof(sa));
1065 		sa.sa_family = AF_INET;
1066 		_s = pserialize_read_enter();
1067 		ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
1068 		if (ifa == NULL)
1069 			ip->ip_src.s_addr = 0;
1070 		else
1071 			ip->ip_src.s_addr =
1072 			    ifatoia(ifa)->ia_addr.sin_addr.s_addr;
1073 		pserialize_read_exit(_s);
1074 		ip->ip_dst.s_addr = INADDR_CARP_GROUP;
1075 
1076 		ch_ptr = (struct carp_header *)(&ip[1]);
1077 		memcpy(ch_ptr, &ch, sizeof(ch));
1078 		if (carp_prepare_ad(m, sc, ch_ptr))
1079 			goto retry_later;
1080 
1081 		m->m_data += sizeof(*ip);
1082 		ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip));
1083 		m->m_data -= sizeof(*ip);
1084 
1085 		nanotime(&sc->sc_if.if_lastchange);
1086 		sc->sc_if.if_opackets++;
1087 		sc->sc_if.if_obytes += len;
1088 		CARP_STATINC(CARP_STAT_OPACKETS);
1089 
1090 		error = ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo,
1091 		    NULL);
1092 		if (error) {
1093 			if (error == ENOBUFS)
1094 				CARP_STATINC(CARP_STAT_ONOMEM);
1095 			else
1096 				CARP_LOG(sc, ("ip_output failed: %d", error));
1097 			sc->sc_if.if_oerrors++;
1098 			if (sc->sc_sendad_errors < INT_MAX)
1099 				sc->sc_sendad_errors++;
1100 			if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
1101 				carp_suppress_preempt++;
1102 				if (carp_suppress_preempt == 1)
1103 					carp_send_ad_all();
1104 			}
1105 			sc->sc_sendad_success = 0;
1106 		} else {
1107 			if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
1108 				if (++sc->sc_sendad_success >=
1109 				    CARP_SENDAD_MIN_SUCCESS) {
1110 					carp_suppress_preempt--;
1111 					sc->sc_sendad_errors = 0;
1112 				}
1113 			} else
1114 				sc->sc_sendad_errors = 0;
1115 		}
1116 	}
1117 #endif /* INET */
1118 #ifdef INET6_notyet
1119 	if (sc->sc_naddrs6) {
1120 		struct ip6_hdr *ip6;
1121 		struct ifaddr *ifa;
1122 		int _s;
1123 
1124 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
1125 		if (m == NULL) {
1126 			sc->sc_if.if_oerrors++;
1127 			CARP_STATINC(CARP_STAT_ONOMEM);
1128 			/* XXX maybe less ? */
1129 			goto retry_later;
1130 		}
1131 		MCLAIM(m, &carp_proto6_mowner_tx);
1132 		len = sizeof(*ip6) + sizeof(ch);
1133 		m->m_pkthdr.len = len;
1134 		m_reset_rcvif(m);
1135 		m->m_len = len;
1136 		MH_ALIGN(m, m->m_len);
1137 		m->m_flags |= M_MCAST;
1138 		ip6 = mtod(m, struct ip6_hdr *);
1139 		memset(ip6, 0, sizeof(*ip6));
1140 		ip6->ip6_vfc |= IPV6_VERSION;
1141 		ip6->ip6_hlim = CARP_DFLTTL;
1142 		ip6->ip6_nxt = IPPROTO_CARP;
1143 
1144 		/* set the source address */
1145 		memset(&sa, 0, sizeof(sa));
1146 		sa.sa_family = AF_INET6;
1147 		_s = pserialize_read_enter();
1148 		ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
1149 		if (ifa == NULL)	/* This should never happen with IPv6 */
1150 			memset(&ip6->ip6_src, 0, sizeof(struct in6_addr));
1151 		else
1152 			bcopy(ifatoia6(ifa)->ia_addr.sin6_addr.s6_addr,
1153 			    &ip6->ip6_src, sizeof(struct in6_addr));
1154 		pserialize_read_exit(_s);
1155 		/* set the multicast destination */
1156 
1157 		ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
1158 		ip6->ip6_dst.s6_addr8[15] = 0x12;
1159 		if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
1160 			sc->sc_if.if_oerrors++;
1161 			m_freem(m);
1162 			CARP_LOG(sc, ("in6_setscope failed"));
1163 			goto retry_later;
1164 		}
1165 
1166 		ch_ptr = (struct carp_header *)(&ip6[1]);
1167 		memcpy(ch_ptr, &ch, sizeof(ch));
1168 		if (carp_prepare_ad(m, sc, ch_ptr))
1169 			goto retry_later;
1170 
1171 		m->m_data += sizeof(*ip6);
1172 		ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip6));
1173 		m->m_data -= sizeof(*ip6);
1174 
1175 		nanotime(&sc->sc_if.if_lastchange);
1176 		sc->sc_if.if_opackets++;
1177 		sc->sc_if.if_obytes += len;
1178 		CARP_STATINC(CARP_STAT_OPACKETS6);
1179 
1180 		error = ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL);
1181 		if (error) {
1182 			if (error == ENOBUFS)
1183 				CARP_STATINC(CARP_STAT_ONOMEM);
1184 			else
1185 				CARP_LOG(sc, ("ip6_output failed: %d", error));
1186 			sc->sc_if.if_oerrors++;
1187 			if (sc->sc_sendad_errors < INT_MAX)
1188 				sc->sc_sendad_errors++;
1189 			if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
1190 				carp_suppress_preempt++;
1191 				if (carp_suppress_preempt == 1)
1192 					carp_send_ad_all();
1193 			}
1194 			sc->sc_sendad_success = 0;
1195 		} else {
1196 			if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
1197 				if (++sc->sc_sendad_success >=
1198 				    CARP_SENDAD_MIN_SUCCESS) {
1199 					carp_suppress_preempt--;
1200 					sc->sc_sendad_errors = 0;
1201 				}
1202 			} else
1203 				sc->sc_sendad_errors = 0;
1204 		}
1205 	}
1206 #endif /* INET6 */
1207 
1208 retry_later:
1209 	splx(s);
1210 	KERNEL_UNLOCK_ONE(NULL);
1211 	if (advbase != 255 || advskew != 255)
1212 		callout_schedule(&sc->sc_ad_tmo, tvtohz(&tv));
1213 }
1214 
1215 /*
1216  * Broadcast a gratuitous ARP request containing
1217  * the virtual router MAC address for each IP address
1218  * associated with the virtual router.
1219  */
1220 static void
1221 carp_send_arp(struct carp_softc *sc)
1222 {
1223 	struct ifaddr *ifa;
1224 	int s;
1225 
1226 	KERNEL_LOCK(1, NULL);
1227 	s = splsoftnet();
1228 	IFADDR_READER_FOREACH(ifa, &sc->sc_if) {
1229 
1230 		if (ifa->ifa_addr->sa_family != AF_INET)
1231 			continue;
1232 
1233 		arpannounce(sc->sc_carpdev, ifa, CLLADDR(sc->sc_if.if_sadl));
1234 	}
1235 	splx(s);
1236 	KERNEL_UNLOCK_ONE(NULL);
1237 }
1238 
1239 #ifdef INET6
1240 static void
1241 carp_send_na(struct carp_softc *sc)
1242 {
1243 	struct ifaddr *ifa;
1244 	struct in6_addr *in6;
1245 	static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1246 	int s;
1247 
1248 	KERNEL_LOCK(1, NULL);
1249 	s = splsoftnet();
1250 
1251 	IFADDR_READER_FOREACH(ifa, &sc->sc_if) {
1252 
1253 		if (ifa->ifa_addr->sa_family != AF_INET6)
1254 			continue;
1255 
1256 		in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
1257 		nd6_na_output(sc->sc_carpdev, &mcast, in6,
1258 		    ND_NA_FLAG_OVERRIDE, 1, NULL);
1259 	}
1260 	splx(s);
1261 	KERNEL_UNLOCK_ONE(NULL);
1262 }
1263 #endif /* INET6 */
1264 
1265 /*
1266  * Based on bridge_hash() in if_bridge.c
1267  */
1268 #define	mix(a,b,c) \
1269 	do {						\
1270 		a -= b; a -= c; a ^= (c >> 13);		\
1271 		b -= c; b -= a; b ^= (a << 8);		\
1272 		c -= a; c -= b; c ^= (b >> 13);		\
1273 		a -= b; a -= c; a ^= (c >> 12);		\
1274 		b -= c; b -= a; b ^= (a << 16);		\
1275 		c -= a; c -= b; c ^= (b >> 5);		\
1276 		a -= b; a -= c; a ^= (c >> 3);		\
1277 		b -= c; b -= a; b ^= (a << 10);		\
1278 		c -= a; c -= b; c ^= (b >> 15);		\
1279 	} while (0)
1280 
1281 static u_int32_t
1282 carp_hash(struct carp_softc *sc, u_char *src)
1283 {
1284 	u_int32_t a = 0x9e3779b9, b = sc->sc_hashkey[0], c = sc->sc_hashkey[1];
1285 
1286 	c += sc->sc_key[3] << 24;
1287 	c += sc->sc_key[2] << 16;
1288 	c += sc->sc_key[1] << 8;
1289 	c += sc->sc_key[0];
1290 	b += src[5] << 8;
1291 	b += src[4];
1292 	a += src[3] << 24;
1293 	a += src[2] << 16;
1294 	a += src[1] << 8;
1295 	a += src[0];
1296 
1297 	mix(a, b, c);
1298 	return (c);
1299 }
1300 
1301 static int
1302 carp_addrcount(struct carp_if *cif, struct in_ifaddr *ia, int type)
1303 {
1304 	struct carp_softc *vh;
1305 	struct ifaddr *ifa;
1306 	int count = 0;
1307 
1308 	TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1309 		if ((type == CARP_COUNT_RUNNING &&
1310 		    (vh->sc_if.if_flags & (IFF_UP|IFF_RUNNING)) ==
1311 		    (IFF_UP|IFF_RUNNING)) ||
1312 		    (type == CARP_COUNT_MASTER && vh->sc_state == MASTER)) {
1313 			IFADDR_READER_FOREACH(ifa, &vh->sc_if) {
1314 				if (ifa->ifa_addr->sa_family == AF_INET &&
1315 				    ia->ia_addr.sin_addr.s_addr ==
1316 				    ifatoia(ifa)->ia_addr.sin_addr.s_addr)
1317 					count++;
1318 			}
1319 		}
1320 	}
1321 	return (count);
1322 }
1323 
1324 int
1325 carp_iamatch(struct in_ifaddr *ia, u_char *src,
1326     u_int32_t *count, u_int32_t index)
1327 {
1328 	struct carp_softc *sc = ia->ia_ifp->if_softc;
1329 
1330 	if (carp_opts[CARPCTL_ARPBALANCE]) {
1331 		/*
1332 		 * We use the source ip to decide which virtual host should
1333 		 * handle the request. If we're master of that virtual host,
1334 		 * then we respond, otherwise, just drop the arp packet on
1335 		 * the floor.
1336 		 */
1337 
1338 		/* Count the elegible carp interfaces with this address */
1339 		if (*count == 0)
1340 			*count = carp_addrcount(
1341 			    (struct carp_if *)ia->ia_ifp->if_carpdev->if_carp,
1342 			    ia, CARP_COUNT_RUNNING);
1343 
1344 		/* This should never happen, but... */
1345 		if (*count == 0)
1346 			return (0);
1347 
1348 		if (carp_hash(sc, src) % *count == index - 1 &&
1349 		    sc->sc_state == MASTER) {
1350 			return (1);
1351 		}
1352 	} else {
1353 		if (sc->sc_state == MASTER)
1354 			return (1);
1355 	}
1356 
1357 	return (0);
1358 }
1359 
1360 #ifdef INET6
1361 struct ifaddr *
1362 carp_iamatch6(void *v, struct in6_addr *taddr)
1363 {
1364 	struct carp_if *cif = v;
1365 	struct carp_softc *vh;
1366 	struct ifaddr *ifa;
1367 
1368 	TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1369 		IFADDR_READER_FOREACH(ifa, &vh->sc_if) {
1370 			if (IN6_ARE_ADDR_EQUAL(taddr,
1371 			    &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1372 			    ((vh->sc_if.if_flags & (IFF_UP|IFF_RUNNING)) ==
1373 			    (IFF_UP|IFF_RUNNING)) && vh->sc_state == MASTER)
1374 				return (ifa);
1375 		}
1376 	}
1377 
1378 	return (NULL);
1379 }
1380 #endif /* INET6 */
1381 
1382 struct ifnet *
1383 carp_ourether(void *v, struct ether_header *eh, u_char iftype, int src)
1384 {
1385 	struct carp_if *cif = (struct carp_if *)v;
1386 	struct carp_softc *vh;
1387 	u_int8_t *ena;
1388 
1389 	if (src)
1390 		ena = (u_int8_t *)&eh->ether_shost;
1391 	else
1392 		ena = (u_int8_t *)&eh->ether_dhost;
1393 
1394 	switch (iftype) {
1395 	case IFT_ETHER:
1396 	case IFT_FDDI:
1397 		if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1398 			return (NULL);
1399 		break;
1400 	case IFT_ISO88025:
1401 		if (ena[0] != 3 || ena[1] || ena[4] || ena[5])
1402 			return (NULL);
1403 		break;
1404 	default:
1405 		return (NULL);
1406 		break;
1407 	}
1408 
1409 	TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list)
1410 		if ((vh->sc_if.if_flags & (IFF_UP|IFF_RUNNING)) ==
1411 		    (IFF_UP|IFF_RUNNING) && vh->sc_state == MASTER &&
1412 		    !memcmp(ena, CLLADDR(vh->sc_if.if_sadl),
1413 		    ETHER_ADDR_LEN)) {
1414 			return (&vh->sc_if);
1415 		    }
1416 
1417 	return (NULL);
1418 }
1419 
1420 int
1421 carp_input(struct mbuf *m, u_int8_t *shost, u_int8_t *dhost, u_int16_t etype)
1422 {
1423 	struct ether_header eh;
1424 	struct carp_if *cif = (struct carp_if *)m_get_rcvif_NOMPSAFE(m)->if_carp;
1425 	struct ifnet *ifp;
1426 
1427 	memcpy(&eh.ether_shost, shost, sizeof(eh.ether_shost));
1428 	memcpy(&eh.ether_dhost, dhost, sizeof(eh.ether_dhost));
1429 	eh.ether_type = etype;
1430 
1431 	if (m->m_flags & (M_BCAST|M_MCAST)) {
1432 		struct carp_softc *vh;
1433 		struct mbuf *m0;
1434 
1435 		/*
1436 		 * XXX Should really check the list of multicast addresses
1437 		 * for each CARP interface _before_ copying.
1438 		 */
1439 		TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1440 			m0 = m_copym(m, 0, M_COPYALL, M_DONTWAIT);
1441 			if (m0 == NULL)
1442 				continue;
1443 			m_set_rcvif(m0, &vh->sc_if);
1444 			ether_input(&vh->sc_if, m0);
1445 		}
1446 		return (1);
1447 	}
1448 
1449 	ifp = carp_ourether(cif, &eh, m_get_rcvif_NOMPSAFE(m)->if_type, 0);
1450 	if (ifp == NULL) {
1451 		return (1);
1452 	}
1453 
1454 	m_set_rcvif(m, ifp);
1455 
1456 	bpf_mtap(ifp, m);
1457 	ifp->if_ipackets++;
1458 	ether_input(ifp, m);
1459 	return (0);
1460 }
1461 
1462 static void
1463 carp_master_down(void *v)
1464 {
1465 	struct carp_softc *sc = v;
1466 
1467 	switch (sc->sc_state) {
1468 	case INIT:
1469 		printf("%s: master_down event in INIT state\n",
1470 		    sc->sc_if.if_xname);
1471 		break;
1472 	case MASTER:
1473 		break;
1474 	case BACKUP:
1475 		CARP_LOG(sc, ("INIT -> MASTER (preempting)"));
1476 		carp_set_state(sc, MASTER);
1477 		carp_send_ad(sc);
1478 		carp_send_arp(sc);
1479 #ifdef INET6
1480 		carp_send_na(sc);
1481 #endif /* INET6 */
1482 		carp_setrun(sc, 0);
1483 		carp_setroute(sc, RTM_ADD);
1484 		break;
1485 	}
1486 }
1487 
1488 /*
1489  * When in backup state, af indicates whether to reset the master down timer
1490  * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1491  */
1492 static void
1493 carp_setrun(struct carp_softc *sc, sa_family_t af)
1494 {
1495 	struct timeval tv;
1496 
1497 	if (sc->sc_carpdev == NULL) {
1498 		sc->sc_if.if_flags &= ~IFF_RUNNING;
1499 		carp_set_state(sc, INIT);
1500 		return;
1501 	}
1502 
1503 	if (sc->sc_if.if_flags & IFF_UP && sc->sc_vhid > 0 &&
1504 	    (sc->sc_naddrs || sc->sc_naddrs6) && !sc->sc_suppress) {
1505 		sc->sc_if.if_flags |= IFF_RUNNING;
1506 	} else {
1507 		sc->sc_if.if_flags &= ~IFF_RUNNING;
1508 		carp_setroute(sc, RTM_DELETE);
1509 		return;
1510 	}
1511 
1512 	switch (sc->sc_state) {
1513 	case INIT:
1514 		carp_set_state(sc, BACKUP);
1515 		carp_setroute(sc, RTM_DELETE);
1516 		carp_setrun(sc, 0);
1517 		break;
1518 	case BACKUP:
1519 		callout_stop(&sc->sc_ad_tmo);
1520 		tv.tv_sec = 3 * sc->sc_advbase;
1521 		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1522 		switch (af) {
1523 #ifdef INET
1524 		case AF_INET:
1525 			callout_schedule(&sc->sc_md_tmo, tvtohz(&tv));
1526 			break;
1527 #endif /* INET */
1528 #ifdef INET6_notyet
1529 		case AF_INET6:
1530 			callout_schedule(&sc->sc_md6_tmo, tvtohz(&tv));
1531 			break;
1532 #endif /* INET6 */
1533 		default:
1534 			if (sc->sc_naddrs)
1535 				callout_schedule(&sc->sc_md_tmo, tvtohz(&tv));
1536 #ifdef INET6_notyet
1537 			if (sc->sc_naddrs6)
1538 				callout_schedule(&sc->sc_md6_tmo, tvtohz(&tv));
1539 #endif /* INET6 */
1540 			break;
1541 		}
1542 		break;
1543 	case MASTER:
1544 		tv.tv_sec = sc->sc_advbase;
1545 		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1546 		callout_schedule(&sc->sc_ad_tmo, tvtohz(&tv));
1547 		break;
1548 	}
1549 }
1550 
1551 static void
1552 carp_multicast_cleanup(struct carp_softc *sc)
1553 {
1554 	struct ip_moptions *imo = &sc->sc_imo;
1555 #ifdef INET6
1556 	struct ip6_moptions *im6o = &sc->sc_im6o;
1557 #endif
1558 	u_int16_t n = imo->imo_num_memberships;
1559 
1560 	/* Clean up our own multicast memberships */
1561 	while (n-- > 0) {
1562 		if (imo->imo_membership[n] != NULL) {
1563 			in_delmulti(imo->imo_membership[n]);
1564 			imo->imo_membership[n] = NULL;
1565 		}
1566 	}
1567 	imo->imo_num_memberships = 0;
1568 	imo->imo_multicast_if_index = 0;
1569 
1570 #ifdef INET6
1571 	while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1572 		struct in6_multi_mship *imm =
1573 		    LIST_FIRST(&im6o->im6o_memberships);
1574 
1575 		LIST_REMOVE(imm, i6mm_chain);
1576 		in6_leavegroup(imm);
1577 	}
1578 	im6o->im6o_multicast_if_index = 0;
1579 #endif
1580 
1581 	/* And any other multicast memberships */
1582 	carp_ether_purgemulti(sc);
1583 }
1584 
1585 static int
1586 carp_set_ifp(struct carp_softc *sc, struct ifnet *ifp)
1587 {
1588 	struct carp_if *cif, *ncif = NULL;
1589 	struct carp_softc *vr, *after = NULL;
1590 	int myself = 0, error = 0;
1591 	int s;
1592 
1593 	if (ifp == sc->sc_carpdev)
1594 		return (0);
1595 
1596 	if (ifp != NULL) {
1597 		if ((ifp->if_flags & IFF_MULTICAST) == 0)
1598 			return (EADDRNOTAVAIL);
1599 
1600 		if (ifp->if_type == IFT_CARP)
1601 			return (EINVAL);
1602 
1603 		if (ifp->if_carp == NULL) {
1604 			ncif = malloc(sizeof(*cif), M_IFADDR, M_NOWAIT);
1605 			if (ncif == NULL)
1606 				return (ENOBUFS);
1607 			if ((error = ifpromisc(ifp, 1))) {
1608 				free(ncif, M_IFADDR);
1609 				return (error);
1610 			}
1611 
1612 			ncif->vhif_ifp = ifp;
1613 			TAILQ_INIT(&ncif->vhif_vrs);
1614 		} else {
1615 			cif = (struct carp_if *)ifp->if_carp;
1616 			TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1617 				if (vr != sc && vr->sc_vhid == sc->sc_vhid)
1618 					return (EINVAL);
1619 		}
1620 
1621 		/* detach from old interface */
1622 		if (sc->sc_carpdev != NULL)
1623 			carpdetach(sc);
1624 
1625 		/* join multicast groups */
1626 		if (sc->sc_naddrs < 0 &&
1627 		    (error = carp_join_multicast(sc)) != 0) {
1628 			if (ncif != NULL)
1629 				free(ncif, M_IFADDR);
1630 			return (error);
1631 		}
1632 
1633 #ifdef INET6
1634 		if (sc->sc_naddrs6 < 0 &&
1635 		    (error = carp_join_multicast6(sc)) != 0) {
1636 			if (ncif != NULL)
1637 				free(ncif, M_IFADDR);
1638 			carp_multicast_cleanup(sc);
1639 			return (error);
1640 		}
1641 #endif
1642 
1643 		/* attach carp interface to physical interface */
1644 		if (ncif != NULL)
1645 			ifp->if_carp = (void *)ncif;
1646 		sc->sc_carpdev = ifp;
1647 		sc->sc_if.if_capabilities = ifp->if_capabilities &
1648 		             (IFCAP_TSOv4 | IFCAP_TSOv6 |
1649                              IFCAP_CSUM_IPv4_Tx|IFCAP_CSUM_IPv4_Rx|
1650                              IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_TCPv4_Rx|
1651                              IFCAP_CSUM_UDPv4_Tx|IFCAP_CSUM_UDPv4_Rx|
1652                              IFCAP_CSUM_TCPv6_Tx|IFCAP_CSUM_TCPv6_Rx|
1653                              IFCAP_CSUM_UDPv6_Tx|IFCAP_CSUM_UDPv6_Rx);
1654 
1655 		cif = (struct carp_if *)ifp->if_carp;
1656 		TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1657 			if (vr == sc)
1658 				myself = 1;
1659 			if (vr->sc_vhid < sc->sc_vhid)
1660 				after = vr;
1661 		}
1662 
1663 		if (!myself) {
1664 			/* We're trying to keep things in order */
1665 			if (after == NULL) {
1666 				TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1667 			} else {
1668 				TAILQ_INSERT_AFTER(&cif->vhif_vrs, after,
1669 				    sc, sc_list);
1670 			}
1671 			cif->vhif_nvrs++;
1672 		}
1673 		if (sc->sc_naddrs || sc->sc_naddrs6)
1674 			sc->sc_if.if_flags |= IFF_UP;
1675 		carp_set_enaddr(sc);
1676 		KERNEL_LOCK(1, NULL);
1677 		s = splnet();
1678 		/* XXX linkstatehooks establish */
1679 		carp_carpdev_state(ifp);
1680 		splx(s);
1681 		KERNEL_UNLOCK_ONE(NULL);
1682 	} else {
1683 		carpdetach(sc);
1684 		sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
1685 	}
1686 	return (0);
1687 }
1688 
1689 static void
1690 carp_set_enaddr(struct carp_softc *sc)
1691 {
1692 	uint8_t enaddr[ETHER_ADDR_LEN];
1693 	if (sc->sc_carpdev && sc->sc_carpdev->if_type == IFT_ISO88025) {
1694 		enaddr[0] = 3;
1695 		enaddr[1] = 0;
1696 		enaddr[2] = 0x40 >> (sc->sc_vhid - 1);
1697 		enaddr[3] = 0x40000 >> (sc->sc_vhid - 1);
1698 		enaddr[4] = 0;
1699 		enaddr[5] = 0;
1700 	} else {
1701 		enaddr[0] = 0;
1702 		enaddr[1] = 0;
1703 		enaddr[2] = 0x5e;
1704 		enaddr[3] = 0;
1705 		enaddr[4] = 1;
1706 		enaddr[5] = sc->sc_vhid;
1707 	}
1708 	if_set_sadl(&sc->sc_if, enaddr, sizeof(enaddr), false);
1709 }
1710 
1711 #if 0
1712 static void
1713 carp_addr_updated(void *v)
1714 {
1715 	struct carp_softc *sc = (struct carp_softc *) v;
1716 	struct ifaddr *ifa;
1717 	int new_naddrs = 0, new_naddrs6 = 0;
1718 
1719 	IFADDR_READER_FOREACH(ifa, &sc->sc_if) {
1720 		if (ifa->ifa_addr->sa_family == AF_INET)
1721 			new_naddrs++;
1722 		else if (ifa->ifa_addr->sa_family == AF_INET6)
1723 			new_naddrs6++;
1724 	}
1725 
1726 	/* Handle a callback after SIOCDIFADDR */
1727 	if (new_naddrs < sc->sc_naddrs || new_naddrs6 < sc->sc_naddrs6) {
1728 		struct in_addr mc_addr;
1729 
1730 		sc->sc_naddrs = new_naddrs;
1731 		sc->sc_naddrs6 = new_naddrs6;
1732 
1733 		/* Re-establish multicast membership removed by in_control */
1734 		mc_addr.s_addr = INADDR_CARP_GROUP;
1735 		if (!in_multi_group(mc_addr, &sc->sc_if, 0)) {
1736 			memset(&sc->sc_imo, 0, sizeof(sc->sc_imo));
1737 
1738 			if (sc->sc_carpdev != NULL && sc->sc_naddrs > 0)
1739 				carp_join_multicast(sc);
1740 		}
1741 
1742 		if (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) {
1743 			sc->sc_if.if_flags &= ~IFF_UP;
1744 			carp_set_state(sc, INIT);
1745 		} else
1746 			carp_hmac_prepare(sc);
1747 	}
1748 
1749 	carp_setrun(sc, 0);
1750 }
1751 #endif
1752 
1753 static int
1754 carp_set_addr(struct carp_softc *sc, struct sockaddr_in *sin)
1755 {
1756 	struct ifnet *ifp = sc->sc_carpdev;
1757 	struct in_ifaddr *ia, *ia_if;
1758 	int error = 0;
1759 	int s;
1760 
1761 	if (sin->sin_addr.s_addr == 0) {
1762 		if (!(sc->sc_if.if_flags & IFF_UP))
1763 			carp_set_state(sc, INIT);
1764 		if (sc->sc_naddrs)
1765 			sc->sc_if.if_flags |= IFF_UP;
1766 		carp_setrun(sc, 0);
1767 		return (0);
1768 	}
1769 
1770 	/* we have to do this by hand to ensure we don't match on ourselves */
1771 	ia_if = NULL;
1772 	s = pserialize_read_enter();
1773 	IN_ADDRLIST_READER_FOREACH(ia) {
1774 		/* and, yeah, we need a multicast-capable iface too */
1775 		if (ia->ia_ifp != &sc->sc_if &&
1776 		    ia->ia_ifp->if_type != IFT_CARP &&
1777 		    (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1778 		    (sin->sin_addr.s_addr & ia->ia_subnetmask) ==
1779 		    ia->ia_subnet) {
1780 			if (!ia_if)
1781 				ia_if = ia;
1782 		}
1783 	}
1784 
1785 	if (ia_if) {
1786 		ia = ia_if;
1787 		if (ifp) {
1788 			if (ifp != ia->ia_ifp)
1789 				return (EADDRNOTAVAIL);
1790 		} else {
1791 			/* FIXME NOMPSAFE */
1792 			ifp = ia->ia_ifp;
1793 		}
1794 	}
1795 	pserialize_read_exit(s);
1796 
1797 	if ((error = carp_set_ifp(sc, ifp)))
1798 		return (error);
1799 
1800 	if (sc->sc_carpdev == NULL)
1801 		return (EADDRNOTAVAIL);
1802 
1803 	if (sc->sc_naddrs == 0 && (error = carp_join_multicast(sc)) != 0)
1804 		return (error);
1805 
1806 	sc->sc_naddrs++;
1807 	if (sc->sc_carpdev != NULL)
1808 		sc->sc_if.if_flags |= IFF_UP;
1809 
1810 	carp_set_state(sc, INIT);
1811 	carp_setrun(sc, 0);
1812 
1813 	/*
1814 	 * Hook if_addrhooks so that we get a callback after in_ifinit has run,
1815 	 * to correct any inappropriate routes that it inserted.
1816 	 */
1817 	if (sc->ah_cookie == 0) {
1818 		/* XXX link address hook */
1819 	}
1820 
1821 	return (0);
1822 }
1823 
1824 static int
1825 carp_join_multicast(struct carp_softc *sc)
1826 {
1827 	struct ip_moptions *imo = &sc->sc_imo, tmpimo;
1828 	struct in_addr addr;
1829 
1830 	memset(&tmpimo, 0, sizeof(tmpimo));
1831 	addr.s_addr = INADDR_CARP_GROUP;
1832 	if ((tmpimo.imo_membership[0] =
1833 	    in_addmulti(&addr, &sc->sc_if)) == NULL) {
1834 		return (ENOBUFS);
1835 	}
1836 
1837 	imo->imo_membership[0] = tmpimo.imo_membership[0];
1838 	imo->imo_num_memberships = 1;
1839 	imo->imo_multicast_if_index = sc->sc_if.if_index;
1840 	imo->imo_multicast_ttl = CARP_DFLTTL;
1841 	imo->imo_multicast_loop = 0;
1842 	return (0);
1843 }
1844 
1845 
1846 #ifdef INET6
1847 static int
1848 carp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1849 {
1850 	struct ifnet *ifp = sc->sc_carpdev;
1851 	struct in6_ifaddr *ia, *ia_if;
1852 	int error = 0;
1853 	int s;
1854 
1855 	if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1856 		if (!(sc->sc_if.if_flags & IFF_UP))
1857 			carp_set_state(sc, INIT);
1858 		if (sc->sc_naddrs6)
1859 			sc->sc_if.if_flags |= IFF_UP;
1860 		carp_setrun(sc, 0);
1861 		return (0);
1862 	}
1863 
1864 	/* we have to do this by hand to ensure we don't match on ourselves */
1865 	ia_if = NULL;
1866 	s = pserialize_read_enter();
1867 	IN6_ADDRLIST_READER_FOREACH(ia) {
1868 		int i;
1869 
1870 		for (i = 0; i < 4; i++) {
1871 			if ((sin6->sin6_addr.s6_addr32[i] &
1872 			    ia->ia_prefixmask.sin6_addr.s6_addr32[i]) !=
1873 			    (ia->ia_addr.sin6_addr.s6_addr32[i] &
1874 			    ia->ia_prefixmask.sin6_addr.s6_addr32[i]))
1875 				break;
1876 		}
1877 		/* and, yeah, we need a multicast-capable iface too */
1878 		if (ia->ia_ifp != &sc->sc_if &&
1879 		    ia->ia_ifp->if_type != IFT_CARP &&
1880 		    (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1881 		    (i == 4)) {
1882 			if (!ia_if)
1883 				ia_if = ia;
1884 		}
1885 	}
1886 	pserialize_read_exit(s);
1887 
1888 	if (ia_if) {
1889 		ia = ia_if;
1890 		if (sc->sc_carpdev) {
1891 			if (sc->sc_carpdev != ia->ia_ifp)
1892 				return (EADDRNOTAVAIL);
1893 		} else {
1894 			ifp = ia->ia_ifp;
1895 		}
1896 	}
1897 
1898 	if ((error = carp_set_ifp(sc, ifp)))
1899 		return (error);
1900 
1901 	if (sc->sc_carpdev == NULL)
1902 		return (EADDRNOTAVAIL);
1903 
1904 	if (sc->sc_naddrs6 == 0 && (error = carp_join_multicast6(sc)) != 0)
1905 		return (error);
1906 
1907 	sc->sc_naddrs6++;
1908 	if (sc->sc_carpdev != NULL)
1909 		sc->sc_if.if_flags |= IFF_UP;
1910 	carp_set_state(sc, INIT);
1911 	carp_setrun(sc, 0);
1912 
1913 	return (0);
1914 }
1915 
1916 static int
1917 carp_join_multicast6(struct carp_softc *sc)
1918 {
1919 	struct in6_multi_mship *imm, *imm2;
1920 	struct ip6_moptions *im6o = &sc->sc_im6o;
1921 	struct sockaddr_in6 addr6;
1922 	int error;
1923 
1924 	/* Join IPv6 CARP multicast group */
1925 	memset(&addr6, 0, sizeof(addr6));
1926 	addr6.sin6_family = AF_INET6;
1927 	addr6.sin6_len = sizeof(addr6);
1928 	addr6.sin6_addr.s6_addr16[0] = htons(0xff02);
1929 	addr6.sin6_addr.s6_addr16[1] = htons(sc->sc_if.if_index);
1930 	addr6.sin6_addr.s6_addr8[15] = 0x12;
1931 	if ((imm = in6_joingroup(&sc->sc_if,
1932 	    &addr6.sin6_addr, &error, 0)) == NULL) {
1933 		return (error);
1934 	}
1935 	/* join solicited multicast address */
1936 	memset(&addr6.sin6_addr, 0, sizeof(addr6.sin6_addr));
1937 	addr6.sin6_addr.s6_addr16[0] = htons(0xff02);
1938 	addr6.sin6_addr.s6_addr16[1] = htons(sc->sc_if.if_index);
1939 	addr6.sin6_addr.s6_addr32[1] = 0;
1940 	addr6.sin6_addr.s6_addr32[2] = htonl(1);
1941 	addr6.sin6_addr.s6_addr32[3] = 0;
1942 	addr6.sin6_addr.s6_addr8[12] = 0xff;
1943 	if ((imm2 = in6_joingroup(&sc->sc_if,
1944 	    &addr6.sin6_addr, &error, 0)) == NULL) {
1945 		in6_leavegroup(imm);
1946 		return (error);
1947 	}
1948 
1949 	/* apply v6 multicast membership */
1950 	im6o->im6o_multicast_if_index = sc->sc_if.if_index;
1951 	if (imm)
1952 		LIST_INSERT_HEAD(&im6o->im6o_memberships, imm,
1953 		    i6mm_chain);
1954 	if (imm2)
1955 		LIST_INSERT_HEAD(&im6o->im6o_memberships, imm2,
1956 		    i6mm_chain);
1957 
1958 	return (0);
1959 }
1960 
1961 #endif /* INET6 */
1962 
1963 static int
1964 carp_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1965 {
1966 	struct lwp *l = curlwp;		/* XXX */
1967 	struct carp_softc *sc = ifp->if_softc, *vr;
1968 	struct carpreq carpr;
1969 	struct ifaddr *ifa;
1970 	struct ifreq *ifr;
1971 	struct ifnet *cdev = NULL;
1972 	int error = 0;
1973 
1974 	ifa = (struct ifaddr *)data;
1975 	ifr = (struct ifreq *)data;
1976 
1977 	switch (cmd) {
1978 	case SIOCINITIFADDR:
1979 		switch (ifa->ifa_addr->sa_family) {
1980 #ifdef INET
1981 		case AF_INET:
1982 			sc->sc_if.if_flags |= IFF_UP;
1983 			memcpy(ifa->ifa_dstaddr, ifa->ifa_addr,
1984 			    sizeof(struct sockaddr));
1985 			error = carp_set_addr(sc, satosin(ifa->ifa_addr));
1986 			break;
1987 #endif /* INET */
1988 #ifdef INET6
1989 		case AF_INET6:
1990 			sc->sc_if.if_flags|= IFF_UP;
1991 			error = carp_set_addr6(sc, satosin6(ifa->ifa_addr));
1992 			break;
1993 #endif /* INET6 */
1994 		default:
1995 			error = EAFNOSUPPORT;
1996 			break;
1997 		}
1998 		break;
1999 
2000 	case SIOCSIFFLAGS:
2001 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
2002 			break;
2003 		if (sc->sc_state != INIT && !(ifr->ifr_flags & IFF_UP)) {
2004 			callout_stop(&sc->sc_ad_tmo);
2005 			callout_stop(&sc->sc_md_tmo);
2006 			callout_stop(&sc->sc_md6_tmo);
2007 			if (sc->sc_state == MASTER) {
2008 				/* we need the interface up to bow out */
2009 				sc->sc_if.if_flags |= IFF_UP;
2010 				sc->sc_bow_out = 1;
2011 				carp_send_ad(sc);
2012 			}
2013 			sc->sc_if.if_flags &= ~IFF_UP;
2014 			carp_set_state(sc, INIT);
2015 			carp_setrun(sc, 0);
2016 		} else if (sc->sc_state == INIT && (ifr->ifr_flags & IFF_UP)) {
2017 			sc->sc_if.if_flags |= IFF_UP;
2018 			carp_setrun(sc, 0);
2019 		}
2020 		break;
2021 
2022 	case SIOCSVH:
2023 		if (l == NULL)
2024 			break;
2025 		if ((error = kauth_authorize_network(l->l_cred,
2026 		    KAUTH_NETWORK_INTERFACE,
2027 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
2028 		    NULL)) != 0)
2029 			break;
2030 		if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
2031 			break;
2032 		error = 1;
2033 		if (carpr.carpr_carpdev[0] != '\0' &&
2034 		    (cdev = ifunit(carpr.carpr_carpdev)) == NULL)
2035 			return (EINVAL);
2036 		if ((error = carp_set_ifp(sc, cdev)))
2037 			return (error);
2038 		if (sc->sc_state != INIT && carpr.carpr_state != sc->sc_state) {
2039 			switch (carpr.carpr_state) {
2040 			case BACKUP:
2041 				callout_stop(&sc->sc_ad_tmo);
2042 				carp_set_state(sc, BACKUP);
2043 				carp_setrun(sc, 0);
2044 				carp_setroute(sc, RTM_DELETE);
2045 				break;
2046 			case MASTER:
2047 				carp_master_down(sc);
2048 				break;
2049 			default:
2050 				break;
2051 			}
2052 		}
2053 		if (carpr.carpr_vhid > 0) {
2054 			if (carpr.carpr_vhid > 255) {
2055 				error = EINVAL;
2056 				break;
2057 			}
2058 			if (sc->sc_carpdev) {
2059 				struct carp_if *cif;
2060 				cif = (struct carp_if *)sc->sc_carpdev->if_carp;
2061 				TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
2062 					if (vr != sc &&
2063 					    vr->sc_vhid == carpr.carpr_vhid)
2064 						return (EINVAL);
2065 			}
2066 			sc->sc_vhid = carpr.carpr_vhid;
2067 			carp_set_enaddr(sc);
2068 			carp_set_state(sc, INIT);
2069 			error--;
2070 		}
2071 		if (carpr.carpr_advbase > 0 || carpr.carpr_advskew > 0) {
2072 			if (carpr.carpr_advskew > 254) {
2073 				error = EINVAL;
2074 				break;
2075 			}
2076 			if (carpr.carpr_advbase > 255) {
2077 				error = EINVAL;
2078 				break;
2079 			}
2080 			sc->sc_advbase = carpr.carpr_advbase;
2081 			sc->sc_advskew = carpr.carpr_advskew;
2082 			error--;
2083 		}
2084 		memcpy(sc->sc_key, carpr.carpr_key, sizeof(sc->sc_key));
2085 		if (error > 0)
2086 			error = EINVAL;
2087 		else {
2088 			error = 0;
2089 			carp_setrun(sc, 0);
2090 		}
2091 		break;
2092 
2093 	case SIOCGVH:
2094 		memset(&carpr, 0, sizeof(carpr));
2095 		if (sc->sc_carpdev != NULL)
2096 			strlcpy(carpr.carpr_carpdev, sc->sc_carpdev->if_xname,
2097 			    IFNAMSIZ);
2098 		carpr.carpr_state = sc->sc_state;
2099 		carpr.carpr_vhid = sc->sc_vhid;
2100 		carpr.carpr_advbase = sc->sc_advbase;
2101 		carpr.carpr_advskew = sc->sc_advskew;
2102 
2103 		if ((l != NULL) && (error = kauth_authorize_network(l->l_cred,
2104 		    KAUTH_NETWORK_INTERFACE,
2105 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
2106 		    NULL)) == 0)
2107 			memcpy(carpr.carpr_key, sc->sc_key,
2108 			    sizeof(carpr.carpr_key));
2109 		error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
2110 		break;
2111 
2112 	case SIOCADDMULTI:
2113 		error = carp_ether_addmulti(sc, ifr);
2114 		break;
2115 
2116 	case SIOCDELMULTI:
2117 		error = carp_ether_delmulti(sc, ifr);
2118 		break;
2119 
2120 	case SIOCSIFCAP:
2121 		if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
2122 			error = 0;
2123 		break;
2124 
2125 	default:
2126 		error = ether_ioctl(ifp, cmd, data);
2127 	}
2128 
2129 	carp_hmac_prepare(sc);
2130 	return (error);
2131 }
2132 
2133 
2134 /*
2135  * Start output on carp interface. This function should never be called.
2136  */
2137 static void
2138 carp_start(struct ifnet *ifp)
2139 {
2140 #ifdef DEBUG
2141 	printf("%s: start called\n", ifp->if_xname);
2142 #endif
2143 }
2144 
2145 int
2146 carp_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *sa,
2147     const struct rtentry *rt)
2148 {
2149 	struct carp_softc *sc = ((struct carp_softc *)ifp->if_softc);
2150 	KASSERT(KERNEL_LOCKED_P());
2151 
2152 	if (sc->sc_carpdev != NULL && sc->sc_state == MASTER) {
2153 		return if_output_lock(sc->sc_carpdev, ifp, m, sa, rt);
2154 	} else {
2155 		m_freem(m);
2156 		return (ENETUNREACH);
2157 	}
2158 }
2159 
2160 static void
2161 carp_set_state(struct carp_softc *sc, int state)
2162 {
2163 	static const char *carp_states[] = { CARP_STATES };
2164 	if (sc->sc_state == state)
2165 		return;
2166 
2167 	CARP_LOG(sc, ("state transition from: %s -> to: %s", carp_states[sc->sc_state], carp_states[state]));
2168 
2169 	sc->sc_state = state;
2170 	switch (state) {
2171 	case BACKUP:
2172 		sc->sc_if.if_link_state = LINK_STATE_DOWN;
2173 		break;
2174 	case MASTER:
2175 		sc->sc_if.if_link_state = LINK_STATE_UP;
2176 		break;
2177 	default:
2178 		sc->sc_if.if_link_state = LINK_STATE_UNKNOWN;
2179 		break;
2180 	}
2181 	rt_ifmsg(&sc->sc_if);
2182 }
2183 
2184 void
2185 carp_carpdev_state(void *v)
2186 {
2187 	struct carp_if *cif;
2188 	struct carp_softc *sc;
2189 	struct ifnet *ifp = v;
2190 
2191 	if (ifp->if_type == IFT_CARP)
2192 		return;
2193 
2194 	cif = (struct carp_if *)ifp->if_carp;
2195 
2196 	TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
2197 		int suppressed = sc->sc_suppress;
2198 
2199 		if (sc->sc_carpdev->if_link_state == LINK_STATE_DOWN ||
2200 		    !(sc->sc_carpdev->if_flags & IFF_UP)) {
2201 			sc->sc_if.if_flags &= ~IFF_RUNNING;
2202 			callout_stop(&sc->sc_ad_tmo);
2203 			callout_stop(&sc->sc_md_tmo);
2204 			callout_stop(&sc->sc_md6_tmo);
2205 			carp_set_state(sc, INIT);
2206 			sc->sc_suppress = 1;
2207 			carp_setrun(sc, 0);
2208 			if (!suppressed) {
2209 				carp_suppress_preempt++;
2210 				if (carp_suppress_preempt == 1)
2211 					carp_send_ad_all();
2212 			}
2213 		} else {
2214 			carp_set_state(sc, INIT);
2215 			sc->sc_suppress = 0;
2216 			carp_setrun(sc, 0);
2217 			if (suppressed)
2218 				carp_suppress_preempt--;
2219 		}
2220 	}
2221 }
2222 
2223 static int
2224 carp_ether_addmulti(struct carp_softc *sc, struct ifreq *ifr)
2225 {
2226 	const struct sockaddr *sa = ifreq_getaddr(SIOCADDMULTI, ifr);
2227 	struct ifnet *ifp;
2228 	struct carp_mc_entry *mc;
2229 	u_int8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
2230 	int error;
2231 
2232 	ifp = sc->sc_carpdev;
2233 	if (ifp == NULL)
2234 		return (EINVAL);
2235 
2236 	error = ether_addmulti(sa, &sc->sc_ac);
2237 	if (error != ENETRESET)
2238 		return (error);
2239 
2240 	/*
2241 	 * This is new multicast address.  We have to tell parent
2242 	 * about it.  Also, remember this multicast address so that
2243 	 * we can delete them on unconfigure.
2244 	 */
2245 	mc = malloc(sizeof(struct carp_mc_entry), M_DEVBUF, M_NOWAIT);
2246 	if (mc == NULL) {
2247 		error = ENOMEM;
2248 		goto alloc_failed;
2249 	}
2250 
2251 	/*
2252 	 * As ether_addmulti() returns ENETRESET, following two
2253 	 * statement shouldn't fail.
2254 	 */
2255 	(void)ether_multiaddr(sa, addrlo, addrhi);
2256 	ETHER_LOOKUP_MULTI(addrlo, addrhi, &sc->sc_ac, mc->mc_enm);
2257 	memcpy(&mc->mc_addr, sa, sa->sa_len);
2258 	LIST_INSERT_HEAD(&sc->carp_mc_listhead, mc, mc_entries);
2259 
2260 	error = if_mcast_op(ifp, SIOCADDMULTI, sa);
2261 	if (error != 0)
2262 		goto ioctl_failed;
2263 
2264 	return (error);
2265 
2266  ioctl_failed:
2267 	LIST_REMOVE(mc, mc_entries);
2268 	free(mc, M_DEVBUF);
2269  alloc_failed:
2270 	(void)ether_delmulti(sa, &sc->sc_ac);
2271 
2272 	return (error);
2273 }
2274 
2275 static int
2276 carp_ether_delmulti(struct carp_softc *sc, struct ifreq *ifr)
2277 {
2278 	const struct sockaddr *sa = ifreq_getaddr(SIOCDELMULTI, ifr);
2279 	struct ifnet *ifp;
2280 	struct ether_multi *enm;
2281 	struct carp_mc_entry *mc;
2282 	u_int8_t addrlo[ETHER_ADDR_LEN], addrhi[ETHER_ADDR_LEN];
2283 	int error;
2284 
2285 	ifp = sc->sc_carpdev;
2286 	if (ifp == NULL)
2287 		return (EINVAL);
2288 
2289 	/*
2290 	 * Find a key to lookup carp_mc_entry.  We have to do this
2291 	 * before calling ether_delmulti for obvious reason.
2292 	 */
2293 	if ((error = ether_multiaddr(sa, addrlo, addrhi)) != 0)
2294 		return (error);
2295 	ETHER_LOOKUP_MULTI(addrlo, addrhi, &sc->sc_ac, enm);
2296 	if (enm == NULL)
2297 		return (EINVAL);
2298 
2299 	LIST_FOREACH(mc, &sc->carp_mc_listhead, mc_entries)
2300 		if (mc->mc_enm == enm)
2301 			break;
2302 
2303 	/* We won't delete entries we didn't add */
2304 	if (mc == NULL)
2305 		return (EINVAL);
2306 
2307 	error = ether_delmulti(sa, &sc->sc_ac);
2308 	if (error != ENETRESET)
2309 		return (error);
2310 
2311 	/* We no longer use this multicast address.  Tell parent so. */
2312 	error = if_mcast_op(ifp, SIOCDELMULTI, sa);
2313 	if (error == 0) {
2314 		/* And forget about this address. */
2315 		LIST_REMOVE(mc, mc_entries);
2316 		free(mc, M_DEVBUF);
2317 	} else
2318 		(void)ether_addmulti(sa, &sc->sc_ac);
2319 	return (error);
2320 }
2321 
2322 /*
2323  * Delete any multicast address we have asked to add from parent
2324  * interface.  Called when the carp is being unconfigured.
2325  */
2326 static void
2327 carp_ether_purgemulti(struct carp_softc *sc)
2328 {
2329 	struct ifnet *ifp = sc->sc_carpdev;		/* Parent. */
2330 	struct carp_mc_entry *mc;
2331 
2332 	if (ifp == NULL)
2333 		return;
2334 
2335 	while ((mc = LIST_FIRST(&sc->carp_mc_listhead)) != NULL) {
2336 		(void)if_mcast_op(ifp, SIOCDELMULTI, sstosa(&mc->mc_addr));
2337 		LIST_REMOVE(mc, mc_entries);
2338 		free(mc, M_DEVBUF);
2339 	}
2340 }
2341 
2342 static int
2343 sysctl_net_inet_carp_stats(SYSCTLFN_ARGS)
2344 {
2345 
2346 	return (NETSTAT_SYSCTL(carpstat_percpu, CARP_NSTATS));
2347 }
2348 
2349 void
2350 carp_init(void)
2351 {
2352 
2353 	sysctl_net_inet_carp_setup(NULL);
2354 #ifdef MBUFTRACE
2355 	MOWNER_ATTACH(&carp_proto_mowner_rx);
2356 	MOWNER_ATTACH(&carp_proto_mowner_tx);
2357 	MOWNER_ATTACH(&carp_proto6_mowner_rx);
2358 	MOWNER_ATTACH(&carp_proto6_mowner_tx);
2359 #endif
2360 }
2361 
2362 static void
2363 sysctl_net_inet_carp_setup(struct sysctllog **clog)
2364 {
2365 
2366 	sysctl_createv(clog, 0, NULL, NULL,
2367 		       CTLFLAG_PERMANENT,
2368 		       CTLTYPE_NODE, "inet", NULL,
2369 		       NULL, 0, NULL, 0,
2370 		       CTL_NET, PF_INET, CTL_EOL);
2371 	sysctl_createv(clog, 0, NULL, NULL,
2372 		       CTLFLAG_PERMANENT,
2373 		       CTLTYPE_NODE, "carp",
2374 		       SYSCTL_DESCR("CARP related settings"),
2375 		       NULL, 0, NULL, 0,
2376 		       CTL_NET, PF_INET, IPPROTO_CARP, CTL_EOL);
2377 
2378 	sysctl_createv(clog, 0, NULL, NULL,
2379 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2380 		       CTLTYPE_INT, "preempt",
2381 		       SYSCTL_DESCR("Enable CARP Preempt"),
2382 		       NULL, 0, &carp_opts[CARPCTL_PREEMPT], 0,
2383 		       CTL_NET, PF_INET, IPPROTO_CARP,
2384 		       CTL_CREATE, CTL_EOL);
2385 	sysctl_createv(clog, 0, NULL, NULL,
2386 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2387 		       CTLTYPE_INT, "arpbalance",
2388 		       SYSCTL_DESCR("Enable ARP balancing"),
2389 		       NULL, 0, &carp_opts[CARPCTL_ARPBALANCE], 0,
2390 		       CTL_NET, PF_INET, IPPROTO_CARP,
2391 		       CTL_CREATE, CTL_EOL);
2392 	sysctl_createv(clog, 0, NULL, NULL,
2393 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2394 		       CTLTYPE_INT, "allow",
2395 		       SYSCTL_DESCR("Enable CARP"),
2396 		       NULL, 0, &carp_opts[CARPCTL_ALLOW], 0,
2397 		       CTL_NET, PF_INET, IPPROTO_CARP,
2398 		       CTL_CREATE, CTL_EOL);
2399 	sysctl_createv(clog, 0, NULL, NULL,
2400 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2401 		       CTLTYPE_INT, "log",
2402 		       SYSCTL_DESCR("CARP logging"),
2403 		       NULL, 0, &carp_opts[CARPCTL_LOG], 0,
2404 		       CTL_NET, PF_INET, IPPROTO_CARP,
2405 		       CTL_CREATE, CTL_EOL);
2406 	sysctl_createv(clog, 0, NULL, NULL,
2407 		       CTLFLAG_PERMANENT,
2408 		       CTLTYPE_STRUCT, "stats",
2409 		       SYSCTL_DESCR("CARP statistics"),
2410 		       sysctl_net_inet_carp_stats, 0, NULL, 0,
2411 		       CTL_NET, PF_INET, IPPROTO_CARP, CARPCTL_STATS,
2412 		       CTL_EOL);
2413 }
2414