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