xref: /freebsd/sys/netinet/ip_carp.c (revision c243e4902be8df1e643c76b5f18b68bb77cc5268)
1 /*-
2  * Copyright (c) 2002 Michael Shalayeff.
3  * Copyright (c) 2003 Ryan McBride.
4  * Copyright (c) 2011 Gleb Smirnoff <glebius@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26  * THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_bpf.h"
33 #include "opt_inet.h"
34 #include "opt_inet6.h"
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/jail.h>
40 #include <sys/kernel.h>
41 #include <sys/limits.h>
42 #include <sys/malloc.h>
43 #include <sys/mbuf.h>
44 #include <sys/module.h>
45 #include <sys/priv.h>
46 #include <sys/proc.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/sockio.h>
50 #include <sys/sysctl.h>
51 #include <sys/syslog.h>
52 #include <sys/taskqueue.h>
53 
54 #include <net/ethernet.h>
55 #include <net/fddi.h>
56 #include <net/if.h>
57 #include <net/if_dl.h>
58 #include <net/if_llatbl.h>
59 #include <net/if_types.h>
60 #include <net/iso88025.h>
61 #include <net/route.h>
62 #include <net/vnet.h>
63 
64 #if defined(INET) || defined(INET6)
65 #include <netinet/in.h>
66 #include <netinet/in_var.h>
67 #include <netinet/ip_carp.h>
68 #include <netinet/ip.h>
69 #include <machine/in_cksum.h>
70 #endif
71 #ifdef INET
72 #include <netinet/ip_var.h>
73 #include <netinet/if_ether.h>
74 #endif
75 
76 #ifdef INET6
77 #include <netinet/icmp6.h>
78 #include <netinet/ip6.h>
79 #include <netinet6/ip6protosw.h>
80 #include <netinet6/in6_var.h>
81 #include <netinet6/ip6_var.h>
82 #include <netinet6/scope6_var.h>
83 #include <netinet6/nd6.h>
84 #endif
85 
86 #include <crypto/sha1.h>
87 
88 static MALLOC_DEFINE(M_CARP, "CARP", "CARP addresses");
89 
90 struct carp_softc {
91 	struct ifnet		*sc_carpdev;	/* Pointer to parent ifnet. */
92 	struct ifaddr		**sc_ifas;	/* Our ifaddrs. */
93 	struct sockaddr_dl	sc_addr;	/* Our link level address. */
94 	struct callout		sc_ad_tmo;	/* Advertising timeout. */
95 #ifdef INET
96 	struct callout		sc_md_tmo;	/* Master down timeout. */
97 #endif
98 #ifdef INET6
99 	struct callout 		sc_md6_tmo;	/* XXX: Master down timeout. */
100 #endif
101 	struct mtx		sc_mtx;
102 
103 	int			sc_vhid;
104 	int			sc_advskew;
105 	int			sc_advbase;
106 
107 	int			sc_naddrs;
108 	int			sc_naddrs6;
109 	int			sc_ifasiz;
110 	enum { INIT = 0, BACKUP, MASTER }	sc_state;
111 	int			sc_suppress;
112 	int			sc_sendad_errors;
113 #define	CARP_SENDAD_MAX_ERRORS	3
114 	int			sc_sendad_success;
115 #define	CARP_SENDAD_MIN_SUCCESS 3
116 
117 	int			sc_init_counter;
118 	uint64_t		sc_counter;
119 
120 	/* authentication */
121 #define	CARP_HMAC_PAD	64
122 	unsigned char sc_key[CARP_KEY_LEN];
123 	unsigned char sc_pad[CARP_HMAC_PAD];
124 	SHA1_CTX sc_sha1;
125 
126 	TAILQ_ENTRY(carp_softc)	sc_list;	/* On the carp_if list. */
127 	LIST_ENTRY(carp_softc)	sc_next;	/* On the global list. */
128 };
129 
130 struct carp_if {
131 #ifdef INET
132 	int	cif_naddrs;
133 #endif
134 #ifdef INET6
135 	int	cif_naddrs6;
136 #endif
137 	TAILQ_HEAD(, carp_softc) cif_vrs;
138 #ifdef INET
139 	struct ip_moptions 	 cif_imo;
140 #endif
141 #ifdef INET6
142 	struct ip6_moptions 	 cif_im6o;
143 #endif
144 	struct ifnet	*cif_ifp;
145 	struct mtx	cif_mtx;
146 };
147 
148 #define	CARP_INET	0
149 #define	CARP_INET6	1
150 static int proto_reg[] = {-1, -1};
151 
152 /*
153  * Brief design of carp(4).
154  *
155  * Any carp-capable ifnet may have a list of carp softcs hanging off
156  * its ifp->if_carp pointer. Each softc represents one unique virtual
157  * host id, or vhid. The softc has a back pointer to the ifnet. All
158  * softcs are joined in a global list, which has quite limited use.
159  *
160  * Any interface address that takes part in CARP negotiation has a
161  * pointer to the softc of its vhid, ifa->ifa_carp. That could be either
162  * AF_INET or AF_INET6 address.
163  *
164  * Although, one can get the softc's backpointer to ifnet and traverse
165  * through its ifp->if_addrhead queue to find all interface addresses
166  * involved in CARP, we keep a growable array of ifaddr pointers. This
167  * allows us to avoid grabbing the IF_ADDR_LOCK() in many traversals that
168  * do calls into the network stack, thus avoiding LORs.
169  *
170  * Locking:
171  *
172  * Each softc has a lock sc_mtx. It is used to synchronise carp_input_c(),
173  * callout-driven events and ioctl()s.
174  *
175  * To traverse the list of softcs on an ifnet we use CIF_LOCK(), to
176  * traverse the global list we use the mutex carp_mtx.
177  *
178  * Known issues with locking:
179  *
180  * - There is no protection for races between two ioctl() requests,
181  *   neither SIOCSVH, nor SIOCAIFADDR & SIOCAIFADDR_IN6. I think that all
182  *   interface ioctl()s should be serialized right in net/if.c.
183  * - Sending ad, we put the pointer to the softc in an mtag, and no reference
184  *   counting is done on the softc.
185  * - On module unload we may race (?) with packet processing thread
186  *   dereferencing our function pointers.
187  */
188 
189 static int carp_allow = 1;		/* Accept incoming CARP packets. */
190 static int carp_preempt = 0;		/* Preempt slower nodes. */
191 static int carp_log = 1;		/* Log level. */
192 static int carp_demotion = 0;		/* Global advskew demotion. */
193 static int carp_senderr_adj = CARP_MAXSKEW;	/* Send error demotion factor */
194 static int carp_ifdown_adj = CARP_MAXSKEW;	/* Iface down demotion factor */
195 
196 SYSCTL_NODE(_net_inet, IPPROTO_CARP,	carp,	CTLFLAG_RW, 0,	"CARP");
197 SYSCTL_INT(_net_inet_carp, OID_AUTO, allow, CTLFLAG_RW, &carp_allow, 0,
198     "Accept incoming CARP packets");
199 SYSCTL_INT(_net_inet_carp, OID_AUTO, preempt, CTLFLAG_RW, &carp_preempt, 0,
200     "High-priority backup preemption mode");
201 SYSCTL_INT(_net_inet_carp, OID_AUTO, log, CTLFLAG_RW, &carp_log, 0,
202     "CARP log level");
203 SYSCTL_INT(_net_inet_carp, OID_AUTO, demotion, CTLFLAG_RW, &carp_demotion, 0,
204     "Demotion factor (skew of advskew)");
205 SYSCTL_INT(_net_inet_carp, OID_AUTO, senderr_demotion_factor, CTLFLAG_RW,
206     &carp_senderr_adj, 0, "Send error demotion factor adjustment");
207 SYSCTL_INT(_net_inet_carp, OID_AUTO, ifdown_demotion_factor, CTLFLAG_RW,
208     &carp_ifdown_adj, 0, "Interface down demotion factor adjustment");
209 
210 static struct carpstats carpstats;
211 SYSCTL_STRUCT(_net_inet_carp, OID_AUTO, stats, CTLFLAG_RW, &carpstats,
212     carpstats, "CARP statistics (struct carpstats, netinet/ip_carp.h)");
213 
214 #define	CARP_LOCK_INIT(sc)	mtx_init(&(sc)->sc_mtx, "carp_softc",   \
215 	NULL, MTX_DEF)
216 #define	CARP_LOCK_DESTROY(sc)	mtx_destroy(&(sc)->sc_mtx)
217 #define	CARP_LOCK_ASSERT(sc)	mtx_assert(&(sc)->sc_mtx, MA_OWNED)
218 #define	CARP_LOCK(sc)		mtx_lock(&(sc)->sc_mtx)
219 #define	CARP_UNLOCK(sc)		mtx_unlock(&(sc)->sc_mtx)
220 #define	CIF_LOCK_INIT(cif)	mtx_init(&(cif)->cif_mtx, "carp_if",   \
221 	NULL, MTX_DEF)
222 #define	CIF_LOCK_DESTROY(cif)	mtx_destroy(&(cif)->cif_mtx)
223 #define	CIF_LOCK_ASSERT(cif)	mtx_assert(&(cif)->cif_mtx, MA_OWNED)
224 #define	CIF_LOCK(cif)		mtx_lock(&(cif)->cif_mtx)
225 #define	CIF_UNLOCK(cif)		mtx_unlock(&(cif)->cif_mtx)
226 #define	CIF_FREE(cif)	do {				\
227 		CIF_LOCK_ASSERT(cif);			\
228 		if (TAILQ_EMPTY(&(cif)->cif_vrs))	\
229 			carp_free_if(cif);		\
230 		else					\
231 			CIF_UNLOCK(cif);		\
232 } while (0)
233 
234 #define	CARP_LOG(...)	do {				\
235 	if (carp_log > 0)				\
236 		log(LOG_INFO, "carp: " __VA_ARGS__);	\
237 } while (0)
238 
239 #define	CARP_DEBUG(...)	do {				\
240 	if (carp_log > 1)				\
241 		log(LOG_DEBUG, __VA_ARGS__);		\
242 } while (0)
243 
244 #define	IFNET_FOREACH_IFA(ifp, ifa)					\
245 	IF_ADDR_LOCK_ASSERT(ifp);					\
246 	TAILQ_FOREACH((ifa), &(ifp)->if_addrhead, ifa_link)		\
247 		if ((ifa)->ifa_carp != NULL)
248 
249 #define	CARP_FOREACH_IFA(sc, ifa)					\
250 	CARP_LOCK_ASSERT(sc);						\
251 	for (int _i = 0;						\
252 		_i < (sc)->sc_naddrs + (sc)->sc_naddrs6 &&		\
253 		((ifa) = sc->sc_ifas[_i]) != NULL;			\
254 		++_i)
255 
256 #define	IFNET_FOREACH_CARP(ifp, sc)					\
257 	CIF_LOCK_ASSERT(ifp->if_carp);					\
258 	TAILQ_FOREACH((sc), &(ifp)->if_carp->cif_vrs, sc_list)
259 
260 #define	DEMOTE_ADVSKEW(sc)					\
261     (((sc)->sc_advskew + carp_demotion > CARP_MAXSKEW) ?	\
262     CARP_MAXSKEW : ((sc)->sc_advskew + carp_demotion))
263 
264 static void	carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
265 static struct carp_softc
266 		*carp_alloc(struct ifnet *);
267 static void	carp_detach_locked(struct ifaddr *);
268 static void	carp_destroy(struct carp_softc *);
269 static struct carp_if
270 		*carp_alloc_if(struct ifnet *);
271 static void	carp_free_if(struct carp_if *);
272 static void	carp_set_state(struct carp_softc *, int);
273 static void	carp_sc_state(struct carp_softc *);
274 static void	carp_setrun(struct carp_softc *, sa_family_t);
275 static void	carp_master_down(void *);
276 static void	carp_master_down_locked(struct carp_softc *);
277 static void	carp_send_ad(void *);
278 static void	carp_send_ad_locked(struct carp_softc *);
279 static void	carp_addroute(struct carp_softc *);
280 static void	carp_ifa_addroute(struct ifaddr *);
281 static void	carp_delroute(struct carp_softc *);
282 static void	carp_ifa_delroute(struct ifaddr *);
283 static void	carp_send_ad_all(void *, int);
284 static void	carp_demote_adj(int, char *);
285 
286 static LIST_HEAD(, carp_softc) carp_list;
287 static struct mtx carp_mtx;
288 static struct task carp_sendall_task =
289     TASK_INITIALIZER(0, carp_send_ad_all, NULL);
290 
291 static __inline uint16_t
292 carp_cksum(struct mbuf *m, int len)
293 {
294 	return (in_cksum(m, len));
295 }
296 
297 static void
298 carp_hmac_prepare(struct carp_softc *sc)
299 {
300 	uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
301 	uint8_t vhid = sc->sc_vhid & 0xff;
302 	struct ifaddr *ifa;
303 	int i, found;
304 #ifdef INET
305 	struct in_addr last, cur, in;
306 #endif
307 #ifdef INET6
308 	struct in6_addr last6, cur6, in6;
309 #endif
310 
311 	CARP_LOCK_ASSERT(sc);
312 
313 	/* Compute ipad from key. */
314 	bzero(sc->sc_pad, sizeof(sc->sc_pad));
315 	bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
316 	for (i = 0; i < sizeof(sc->sc_pad); i++)
317 		sc->sc_pad[i] ^= 0x36;
318 
319 	/* Precompute first part of inner hash. */
320 	SHA1Init(&sc->sc_sha1);
321 	SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
322 	SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
323 	SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
324 	SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
325 #ifdef INET
326 	cur.s_addr = 0;
327 	do {
328 		found = 0;
329 		last = cur;
330 		cur.s_addr = 0xffffffff;
331 		CARP_FOREACH_IFA(sc, ifa) {
332 			in.s_addr = ifatoia(ifa)->ia_addr.sin_addr.s_addr;
333 			if (ifa->ifa_addr->sa_family == AF_INET &&
334 			    ntohl(in.s_addr) > ntohl(last.s_addr) &&
335 			    ntohl(in.s_addr) < ntohl(cur.s_addr)) {
336 				cur.s_addr = in.s_addr;
337 				found++;
338 			}
339 		}
340 		if (found)
341 			SHA1Update(&sc->sc_sha1, (void *)&cur, sizeof(cur));
342 	} while (found);
343 #endif /* INET */
344 #ifdef INET6
345 	memset(&cur6, 0, sizeof(cur6));
346 	do {
347 		found = 0;
348 		last6 = cur6;
349 		memset(&cur6, 0xff, sizeof(cur6));
350 		CARP_FOREACH_IFA(sc, ifa) {
351 			in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
352 			if (IN6_IS_SCOPE_EMBED(&in6))
353 				in6.s6_addr16[1] = 0;
354 			if (ifa->ifa_addr->sa_family == AF_INET6 &&
355 			    memcmp(&in6, &last6, sizeof(in6)) > 0 &&
356 			    memcmp(&in6, &cur6, sizeof(in6)) < 0) {
357 				cur6 = in6;
358 				found++;
359 			}
360 		}
361 		if (found)
362 			SHA1Update(&sc->sc_sha1, (void *)&cur6, sizeof(cur6));
363 	} while (found);
364 #endif /* INET6 */
365 
366 	/* convert ipad to opad */
367 	for (i = 0; i < sizeof(sc->sc_pad); i++)
368 		sc->sc_pad[i] ^= 0x36 ^ 0x5c;
369 }
370 
371 static void
372 carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
373     unsigned char md[20])
374 {
375 	SHA1_CTX sha1ctx;
376 
377 	CARP_LOCK_ASSERT(sc);
378 
379 	/* fetch first half of inner hash */
380 	bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
381 
382 	SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
383 	SHA1Final(md, &sha1ctx);
384 
385 	/* outer hash */
386 	SHA1Init(&sha1ctx);
387 	SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
388 	SHA1Update(&sha1ctx, md, 20);
389 	SHA1Final(md, &sha1ctx);
390 }
391 
392 static int
393 carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
394     unsigned char md[20])
395 {
396 	unsigned char md2[20];
397 
398 	CARP_LOCK_ASSERT(sc);
399 
400 	carp_hmac_generate(sc, counter, md2);
401 
402 	return (bcmp(md, md2, sizeof(md2)));
403 }
404 
405 /*
406  * process input packet.
407  * we have rearranged checks order compared to the rfc,
408  * but it seems more efficient this way or not possible otherwise.
409  */
410 #ifdef INET
411 void
412 carp_input(struct mbuf *m, int hlen)
413 {
414 	struct ip *ip = mtod(m, struct ip *);
415 	struct carp_header *ch;
416 	int iplen, len;
417 
418 	CARPSTATS_INC(carps_ipackets);
419 
420 	if (!carp_allow) {
421 		m_freem(m);
422 		return;
423 	}
424 
425 	/* verify that the IP TTL is 255.  */
426 	if (ip->ip_ttl != CARP_DFLTTL) {
427 		CARPSTATS_INC(carps_badttl);
428 		CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
429 		    ip->ip_ttl,
430 		    m->m_pkthdr.rcvif->if_xname);
431 		m_freem(m);
432 		return;
433 	}
434 
435 	iplen = ip->ip_hl << 2;
436 
437 	if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
438 		CARPSTATS_INC(carps_badlen);
439 		CARP_DEBUG("%s: received len %zd < sizeof(struct carp_header) "
440 		    "on %s\n", __func__, m->m_len - sizeof(struct ip),
441 		    m->m_pkthdr.rcvif->if_xname);
442 		m_freem(m);
443 		return;
444 	}
445 
446 	if (iplen + sizeof(*ch) < m->m_len) {
447 		if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
448 			CARPSTATS_INC(carps_hdrops);
449 			CARP_DEBUG("%s: pullup failed\n", __func__);
450 			return;
451 		}
452 		ip = mtod(m, struct ip *);
453 	}
454 	ch = (struct carp_header *)((char *)ip + iplen);
455 
456 	/*
457 	 * verify that the received packet length is
458 	 * equal to the CARP header
459 	 */
460 	len = iplen + sizeof(*ch);
461 	if (len > m->m_pkthdr.len) {
462 		CARPSTATS_INC(carps_badlen);
463 		CARP_DEBUG("%s: packet too short %d on %s\n", __func__,
464 		    m->m_pkthdr.len,
465 		    m->m_pkthdr.rcvif->if_xname);
466 		m_freem(m);
467 		return;
468 	}
469 
470 	if ((m = m_pullup(m, len)) == NULL) {
471 		CARPSTATS_INC(carps_hdrops);
472 		return;
473 	}
474 	ip = mtod(m, struct ip *);
475 	ch = (struct carp_header *)((char *)ip + iplen);
476 
477 	/* verify the CARP checksum */
478 	m->m_data += iplen;
479 	if (carp_cksum(m, len - iplen)) {
480 		CARPSTATS_INC(carps_badsum);
481 		CARP_DEBUG("%s: checksum failed on %s\n", __func__,
482 		    m->m_pkthdr.rcvif->if_xname);
483 		m_freem(m);
484 		return;
485 	}
486 	m->m_data -= iplen;
487 
488 	carp_input_c(m, ch, AF_INET);
489 }
490 #endif
491 
492 #ifdef INET6
493 int
494 carp6_input(struct mbuf **mp, int *offp, int proto)
495 {
496 	struct mbuf *m = *mp;
497 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
498 	struct carp_header *ch;
499 	u_int len;
500 
501 	CARPSTATS_INC(carps_ipackets6);
502 
503 	if (!carp_allow) {
504 		m_freem(m);
505 		return (IPPROTO_DONE);
506 	}
507 
508 	/* check if received on a valid carp interface */
509 	if (m->m_pkthdr.rcvif->if_carp == NULL) {
510 		CARPSTATS_INC(carps_badif);
511 		CARP_DEBUG("%s: packet received on non-carp interface: %s\n",
512 		    __func__, m->m_pkthdr.rcvif->if_xname);
513 		m_freem(m);
514 		return (IPPROTO_DONE);
515 	}
516 
517 	/* verify that the IP TTL is 255 */
518 	if (ip6->ip6_hlim != CARP_DFLTTL) {
519 		CARPSTATS_INC(carps_badttl);
520 		CARP_DEBUG("%s: received ttl %d != 255 on %s\n", __func__,
521 		    ip6->ip6_hlim, m->m_pkthdr.rcvif->if_xname);
522 		m_freem(m);
523 		return (IPPROTO_DONE);
524 	}
525 
526 	/* verify that we have a complete carp packet */
527 	len = m->m_len;
528 	IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
529 	if (ch == NULL) {
530 		CARPSTATS_INC(carps_badlen);
531 		CARP_DEBUG("%s: packet size %u too small\n", __func__, len);
532 		return (IPPROTO_DONE);
533 	}
534 
535 
536 	/* verify the CARP checksum */
537 	m->m_data += *offp;
538 	if (carp_cksum(m, sizeof(*ch))) {
539 		CARPSTATS_INC(carps_badsum);
540 		CARP_DEBUG("%s: checksum failed, on %s\n", __func__,
541 		    m->m_pkthdr.rcvif->if_xname);
542 		m_freem(m);
543 		return (IPPROTO_DONE);
544 	}
545 	m->m_data -= *offp;
546 
547 	carp_input_c(m, ch, AF_INET6);
548 	return (IPPROTO_DONE);
549 }
550 #endif /* INET6 */
551 
552 static void
553 carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
554 {
555 	struct ifnet *ifp = m->m_pkthdr.rcvif;
556 	struct ifaddr *ifa;
557 	struct carp_softc *sc;
558 	uint64_t tmp_counter;
559 	struct timeval sc_tv, ch_tv;
560 
561 	/* verify that the VHID is valid on the receiving interface */
562 	IF_ADDR_RLOCK(ifp);
563 	IFNET_FOREACH_IFA(ifp, ifa)
564 		if (ifa->ifa_addr->sa_family == af &&
565 		    ifa->ifa_carp->sc_vhid == ch->carp_vhid) {
566 			ifa_ref(ifa);
567 			break;
568 		}
569 	IF_ADDR_RUNLOCK(ifp);
570 
571 	if (ifa == NULL) {
572 		CARPSTATS_INC(carps_badvhid);
573 		m_freem(m);
574 		return;
575 	}
576 
577 	/* verify the CARP version. */
578 	if (ch->carp_version != CARP_VERSION) {
579 		CARPSTATS_INC(carps_badver);
580 		CARP_DEBUG("%s: invalid version %d\n", ifp->if_xname,
581 		    ch->carp_version);
582 		ifa_free(ifa);
583 		m_freem(m);
584 		return;
585 	}
586 
587 	sc = ifa->ifa_carp;
588 	CARP_LOCK(sc);
589 	ifa_free(ifa);
590 
591 	if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
592 		CARPSTATS_INC(carps_badauth);
593 		CARP_DEBUG("%s: incorrect hash for VHID %u@%s\n", __func__,
594 		    sc->sc_vhid, ifp->if_xname);
595 		goto out;
596 	}
597 
598 	tmp_counter = ntohl(ch->carp_counter[0]);
599 	tmp_counter = tmp_counter<<32;
600 	tmp_counter += ntohl(ch->carp_counter[1]);
601 
602 	/* XXX Replay protection goes here */
603 
604 	sc->sc_init_counter = 0;
605 	sc->sc_counter = tmp_counter;
606 
607 	sc_tv.tv_sec = sc->sc_advbase;
608 	sc_tv.tv_usec = DEMOTE_ADVSKEW(sc) * 1000000 / 256;
609 	ch_tv.tv_sec = ch->carp_advbase;
610 	ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
611 
612 	switch (sc->sc_state) {
613 	case INIT:
614 		break;
615 	case MASTER:
616 		/*
617 		 * If we receive an advertisement from a master who's going to
618 		 * be more frequent than us, go into BACKUP state.
619 		 */
620 		if (timevalcmp(&sc_tv, &ch_tv, >) ||
621 		    timevalcmp(&sc_tv, &ch_tv, ==)) {
622 			callout_stop(&sc->sc_ad_tmo);
623 			CARP_LOG("VHID %u@%s: MASTER -> BACKUP "
624 			    "(more frequent advertisement received)\n",
625 			    sc->sc_vhid,
626 			    sc->sc_carpdev->if_xname);
627 			carp_set_state(sc, BACKUP);
628 			carp_setrun(sc, 0);
629 			carp_delroute(sc);
630 		}
631 		break;
632 	case BACKUP:
633 		/*
634 		 * If we're pre-empting masters who advertise slower than us,
635 		 * and this one claims to be slower, treat him as down.
636 		 */
637 		if (carp_preempt && timevalcmp(&sc_tv, &ch_tv, <)) {
638 			CARP_LOG("VHID %u@%s: BACKUP -> MASTER "
639 			    "(preempting a slower master)\n",
640 			    sc->sc_vhid,
641 			    sc->sc_carpdev->if_xname);
642 			carp_master_down_locked(sc);
643 			break;
644 		}
645 
646 		/*
647 		 *  If the master is going to advertise at such a low frequency
648 		 *  that he's guaranteed to time out, we'd might as well just
649 		 *  treat him as timed out now.
650 		 */
651 		sc_tv.tv_sec = sc->sc_advbase * 3;
652 		if (timevalcmp(&sc_tv, &ch_tv, <)) {
653 			CARP_LOG("VHID %u@%s: BACKUP -> MASTER "
654 			    "(master timed out)\n",
655 			    sc->sc_vhid,
656 			    sc->sc_carpdev->if_xname);
657 			carp_master_down_locked(sc);
658 			break;
659 		}
660 
661 		/*
662 		 * Otherwise, we reset the counter and wait for the next
663 		 * advertisement.
664 		 */
665 		carp_setrun(sc, af);
666 		break;
667 	}
668 
669 out:
670 	CARP_UNLOCK(sc);
671 	m_freem(m);
672 }
673 
674 static int
675 carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
676 {
677 	struct m_tag *mtag;
678 
679 	if (sc->sc_init_counter) {
680 		/* this could also be seconds since unix epoch */
681 		sc->sc_counter = arc4random();
682 		sc->sc_counter = sc->sc_counter << 32;
683 		sc->sc_counter += arc4random();
684 	} else
685 		sc->sc_counter++;
686 
687 	ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
688 	ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
689 
690 	carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
691 
692 	/* Tag packet for carp_output */
693 	if ((mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct carp_softc *),
694 	    M_NOWAIT)) == NULL) {
695 		m_freem(m);
696 		CARPSTATS_INC(carps_onomem);
697 		return (ENOMEM);
698 	}
699 	bcopy(&sc, mtag + 1, sizeof(sc));
700 	m_tag_prepend(m, mtag);
701 
702 	return (0);
703 }
704 
705 /*
706  * To avoid LORs and possible recursions this function shouldn't
707  * be called directly, but scheduled via taskqueue.
708  */
709 static void
710 carp_send_ad_all(void *ctx __unused, int pending __unused)
711 {
712 	struct carp_softc *sc;
713 
714 	mtx_lock(&carp_mtx);
715 	LIST_FOREACH(sc, &carp_list, sc_next)
716 		if (sc->sc_state == MASTER) {
717 			CARP_LOCK(sc);
718 			CURVNET_SET(sc->sc_carpdev->if_vnet);
719 			carp_send_ad_locked(sc);
720 			CURVNET_RESTORE();
721 			CARP_UNLOCK(sc);
722 		}
723 	mtx_unlock(&carp_mtx);
724 }
725 
726 /* Send a periodic advertisement, executed in callout context. */
727 static void
728 carp_send_ad(void *v)
729 {
730 	struct carp_softc *sc = v;
731 
732 	CARP_LOCK_ASSERT(sc);
733 	CURVNET_SET(sc->sc_carpdev->if_vnet);
734 	carp_send_ad_locked(sc);
735 	CURVNET_RESTORE();
736 	CARP_UNLOCK(sc);
737 }
738 
739 static void
740 carp_send_ad_locked(struct carp_softc *sc)
741 {
742 	struct carp_header ch;
743 	struct timeval tv;
744 	struct sockaddr sa;
745 	struct ifaddr *ifa;
746 	struct carp_header *ch_ptr;
747 	struct mbuf *m;
748 	int len, advskew;
749 
750 	CARP_LOCK_ASSERT(sc);
751 
752 	advskew = DEMOTE_ADVSKEW(sc);
753 	tv.tv_sec = sc->sc_advbase;
754 	tv.tv_usec = advskew * 1000000 / 256;
755 
756 	ch.carp_version = CARP_VERSION;
757 	ch.carp_type = CARP_ADVERTISEMENT;
758 	ch.carp_vhid = sc->sc_vhid;
759 	ch.carp_advbase = sc->sc_advbase;
760 	ch.carp_advskew = advskew;
761 	ch.carp_authlen = 7;	/* XXX DEFINE */
762 	ch.carp_pad1 = 0;	/* must be zero */
763 	ch.carp_cksum = 0;
764 
765 	/* XXXGL: OpenBSD picks first ifaddr with needed family. */
766 
767 #ifdef INET
768 	if (sc->sc_naddrs) {
769 		struct ip *ip;
770 
771 		MGETHDR(m, M_NOWAIT, MT_HEADER);
772 		if (m == NULL) {
773 			CARPSTATS_INC(carps_onomem);
774 			/* XXX maybe less ? */
775 			callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
776 			    carp_send_ad, sc);
777 			return;
778 		}
779 		len = sizeof(*ip) + sizeof(ch);
780 		m->m_pkthdr.len = len;
781 		m->m_pkthdr.rcvif = NULL;
782 		m->m_len = len;
783 		MH_ALIGN(m, m->m_len);
784 		m->m_flags |= M_MCAST;
785 		ip = mtod(m, struct ip *);
786 		ip->ip_v = IPVERSION;
787 		ip->ip_hl = sizeof(*ip) >> 2;
788 		ip->ip_tos = IPTOS_LOWDELAY;
789 		ip->ip_len = len;
790 		ip->ip_id = ip_newid();
791 		ip->ip_off = IP_DF;
792 		ip->ip_ttl = CARP_DFLTTL;
793 		ip->ip_p = IPPROTO_CARP;
794 		ip->ip_sum = 0;
795 
796 		bzero(&sa, sizeof(sa));
797 		sa.sa_family = AF_INET;
798 		ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
799 		if (ifa != NULL) {
800 			ip->ip_src.s_addr =
801 			    ifatoia(ifa)->ia_addr.sin_addr.s_addr;
802 			ifa_free(ifa);
803 		} else
804 			ip->ip_src.s_addr = 0;
805 		ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
806 
807 		ch_ptr = (struct carp_header *)(&ip[1]);
808 		bcopy(&ch, ch_ptr, sizeof(ch));
809 		if (carp_prepare_ad(m, sc, ch_ptr))
810 			return;
811 
812 		m->m_data += sizeof(*ip);
813 		ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip));
814 		m->m_data -= sizeof(*ip);
815 
816 		CARPSTATS_INC(carps_opackets);
817 
818 		if (ip_output(m, NULL, NULL, IP_RAWOUTPUT,
819 		    &sc->sc_carpdev->if_carp->cif_imo, NULL)) {
820 			if (sc->sc_sendad_errors < INT_MAX)
821 				sc->sc_sendad_errors++;
822 			if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS)
823 				carp_demote_adj(carp_senderr_adj, "send error");
824 			sc->sc_sendad_success = 0;
825 		} else {
826 			if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
827 				if (++sc->sc_sendad_success >=
828 				    CARP_SENDAD_MIN_SUCCESS) {
829 					carp_demote_adj(-carp_senderr_adj,
830 					    "send ok");
831 					sc->sc_sendad_errors = 0;
832 				}
833 			} else
834 				sc->sc_sendad_errors = 0;
835 		}
836 	}
837 #endif /* INET */
838 #ifdef INET6
839 	if (sc->sc_naddrs6) {
840 		struct ip6_hdr *ip6;
841 
842 		MGETHDR(m, M_NOWAIT, MT_HEADER);
843 		if (m == NULL) {
844 			CARPSTATS_INC(carps_onomem);
845 			/* XXX maybe less ? */
846 			callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
847 			    carp_send_ad, sc);
848 			return;
849 		}
850 		len = sizeof(*ip6) + sizeof(ch);
851 		m->m_pkthdr.len = len;
852 		m->m_pkthdr.rcvif = NULL;
853 		m->m_len = len;
854 		MH_ALIGN(m, m->m_len);
855 		m->m_flags |= M_MCAST;
856 		ip6 = mtod(m, struct ip6_hdr *);
857 		bzero(ip6, sizeof(*ip6));
858 		ip6->ip6_vfc |= IPV6_VERSION;
859 		ip6->ip6_hlim = CARP_DFLTTL;
860 		ip6->ip6_nxt = IPPROTO_CARP;
861 		bzero(&sa, sizeof(sa));
862 
863 		/* set the source address */
864 		sa.sa_family = AF_INET6;
865 		ifa = ifaof_ifpforaddr(&sa, sc->sc_carpdev);
866 		if (ifa != NULL) {
867 			bcopy(IFA_IN6(ifa), &ip6->ip6_src,
868 			    sizeof(struct in6_addr));
869 			ifa_free(ifa);
870 		} else
871 			/* This should never happen with IPv6. */
872 			bzero(&ip6->ip6_src, sizeof(struct in6_addr));
873 
874 		/* Set the multicast destination. */
875 		ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
876 		ip6->ip6_dst.s6_addr8[15] = 0x12;
877 		if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
878 			m_freem(m);
879 			CARP_DEBUG("%s: in6_setscope failed\n", __func__);
880 			return;
881 		}
882 
883 		ch_ptr = (struct carp_header *)(&ip6[1]);
884 		bcopy(&ch, ch_ptr, sizeof(ch));
885 		if (carp_prepare_ad(m, sc, ch_ptr))
886 			return;
887 
888 		m->m_data += sizeof(*ip6);
889 		ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip6));
890 		m->m_data -= sizeof(*ip6);
891 
892 		CARPSTATS_INC(carps_opackets6);
893 
894 		if (ip6_output(m, NULL, NULL, 0,
895 		    &sc->sc_carpdev->if_carp->cif_im6o, NULL, NULL)) {
896 			if (sc->sc_sendad_errors < INT_MAX)
897 				sc->sc_sendad_errors++;
898 			if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS)
899 				carp_demote_adj(carp_senderr_adj,
900 				    "send6 error");
901 			sc->sc_sendad_success = 0;
902 		} else {
903 			if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
904 				if (++sc->sc_sendad_success >=
905 				    CARP_SENDAD_MIN_SUCCESS) {
906 					carp_demote_adj(-carp_senderr_adj,
907 					    "send6 ok");
908 					sc->sc_sendad_errors = 0;
909 				}
910 			} else
911 				sc->sc_sendad_errors = 0;
912 		}
913 	}
914 #endif /* INET6 */
915 
916 	callout_reset(&sc->sc_ad_tmo, tvtohz(&tv), carp_send_ad, sc);
917 }
918 
919 static void
920 carp_addroute(struct carp_softc *sc)
921 {
922 	struct ifaddr *ifa;
923 
924 	CARP_FOREACH_IFA(sc, ifa)
925 		carp_ifa_addroute(ifa);
926 }
927 
928 static void
929 carp_ifa_addroute(struct ifaddr *ifa)
930 {
931 
932 	switch (ifa->ifa_addr->sa_family) {
933 #ifdef INET
934 	case AF_INET:
935 		in_addprefix(ifatoia(ifa), RTF_UP);
936 		ifa_add_loopback_route(ifa,
937 		    (struct sockaddr *)&ifatoia(ifa)->ia_addr);
938 		break;
939 #endif
940 #ifdef INET6
941 	case AF_INET6:
942 		ifa_add_loopback_route(ifa,
943 		    (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
944 		in6_ifaddloop(ifa);
945 		break;
946 #endif
947 	}
948 }
949 
950 static void
951 carp_delroute(struct carp_softc *sc)
952 {
953 	struct ifaddr *ifa;
954 
955 	CARP_FOREACH_IFA(sc, ifa)
956 		carp_ifa_delroute(ifa);
957 }
958 
959 static void
960 carp_ifa_delroute(struct ifaddr *ifa)
961 {
962 
963 	switch (ifa->ifa_addr->sa_family) {
964 #ifdef INET
965 	case AF_INET:
966 		ifa_del_loopback_route(ifa,
967 		    (struct sockaddr *)&ifatoia(ifa)->ia_addr);
968 		in_scrubprefix(ifatoia(ifa), LLE_STATIC);
969 		break;
970 #endif
971 #ifdef INET6
972 	case AF_INET6:
973 		ifa_del_loopback_route(ifa,
974 		    (struct sockaddr *)&ifatoia6(ifa)->ia_addr);
975 		in6_ifremloop(ifa);
976 		break;
977 #endif
978 	}
979 }
980 
981 #ifdef INET
982 /*
983  * Broadcast a gratuitous ARP request containing
984  * the virtual router MAC address for each IP address
985  * associated with the virtual router.
986  */
987 static void
988 carp_send_arp(struct carp_softc *sc)
989 {
990 	struct ifaddr *ifa;
991 
992 	CARP_FOREACH_IFA(sc, ifa)
993 		if (ifa->ifa_addr->sa_family == AF_INET)
994 			arp_ifinit2(sc->sc_carpdev, ifa, LLADDR(&sc->sc_addr));
995 }
996 
997 int
998 carp_iamatch(struct ifaddr *ifa, uint8_t **enaddr)
999 {
1000 	struct carp_softc *sc = ifa->ifa_carp;
1001 
1002 	if (sc->sc_state == MASTER) {
1003 		*enaddr = LLADDR(&sc->sc_addr);
1004 		return (1);
1005 	}
1006 
1007 	return (0);
1008 }
1009 #endif
1010 
1011 #ifdef INET6
1012 static void
1013 carp_send_na(struct carp_softc *sc)
1014 {
1015 	static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1016 	struct ifaddr *ifa;
1017 	struct in6_addr *in6;
1018 
1019 	CARP_FOREACH_IFA(sc, ifa) {
1020 		if (ifa->ifa_addr->sa_family != AF_INET6)
1021 			continue;
1022 
1023 		in6 = IFA_IN6(ifa);
1024 		nd6_na_output(sc->sc_carpdev, &mcast, in6,
1025 		    ND_NA_FLAG_OVERRIDE, 1, NULL);
1026 		DELAY(1000);	/* XXX */
1027 	}
1028 }
1029 
1030 /*
1031  * Returns ifa in case it's a carp address and it is MASTER, or if the address
1032  * matches and is not a carp address.  Returns NULL otherwise.
1033  */
1034 struct ifaddr *
1035 carp_iamatch6(struct ifnet *ifp, struct in6_addr *taddr)
1036 {
1037 	struct ifaddr *ifa;
1038 
1039 	ifa = NULL;
1040 	IF_ADDR_RLOCK(ifp);
1041 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1042 		if (ifa->ifa_addr->sa_family != AF_INET6)
1043 			continue;
1044 		if (!IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa)))
1045 			continue;
1046 		if (ifa->ifa_carp && ifa->ifa_carp->sc_state != MASTER)
1047 			ifa = NULL;
1048 		else
1049 			ifa_ref(ifa);
1050 		break;
1051 	}
1052 	IF_ADDR_RUNLOCK(ifp);
1053 
1054 	return (ifa);
1055 }
1056 
1057 caddr_t
1058 carp_macmatch6(struct ifnet *ifp, struct mbuf *m, const struct in6_addr *taddr)
1059 {
1060 	struct ifaddr *ifa;
1061 
1062 	IF_ADDR_RLOCK(ifp);
1063 	IFNET_FOREACH_IFA(ifp, ifa)
1064 		if (ifa->ifa_addr->sa_family == AF_INET6 &&
1065 		    IN6_ARE_ADDR_EQUAL(taddr, IFA_IN6(ifa))) {
1066 			struct carp_softc *sc = ifa->ifa_carp;
1067 			struct m_tag *mtag;
1068 
1069 			IF_ADDR_RUNLOCK(ifp);
1070 
1071 			mtag = m_tag_get(PACKET_TAG_CARP,
1072 			    sizeof(struct carp_softc *), M_NOWAIT);
1073 			if (mtag == NULL)
1074 				/* Better a bit than nothing. */
1075 				return (LLADDR(&sc->sc_addr));
1076 
1077 			bcopy(&sc, mtag + 1, sizeof(sc));
1078 			m_tag_prepend(m, mtag);
1079 
1080 			return (LLADDR(&sc->sc_addr));
1081 		}
1082 	IF_ADDR_RUNLOCK(ifp);
1083 
1084 	return (NULL);
1085 }
1086 #endif /* INET6 */
1087 
1088 int
1089 carp_forus(struct ifnet *ifp, u_char *dhost)
1090 {
1091 	struct carp_softc *sc;
1092 	uint8_t *ena = dhost;
1093 
1094 	if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1095 		return (0);
1096 
1097 	CIF_LOCK(ifp->if_carp);
1098 	IFNET_FOREACH_CARP(ifp, sc) {
1099 		CARP_LOCK(sc);
1100 		if (sc->sc_state == MASTER && !bcmp(dhost, LLADDR(&sc->sc_addr),
1101 		    ETHER_ADDR_LEN)) {
1102 			CARP_UNLOCK(sc);
1103 			CIF_UNLOCK(ifp->if_carp);
1104 			return (1);
1105 		}
1106 		CARP_UNLOCK(sc);
1107 	}
1108 	CIF_UNLOCK(ifp->if_carp);
1109 
1110 	return (0);
1111 }
1112 
1113 /* Master down timeout event, executed in callout context. */
1114 static void
1115 carp_master_down(void *v)
1116 {
1117 	struct carp_softc *sc = v;
1118 
1119 	CARP_LOCK_ASSERT(sc);
1120 
1121 	CURVNET_SET(sc->sc_carpdev->if_vnet);
1122 	if (sc->sc_state == BACKUP) {
1123 		CARP_LOG("VHID %u@%s: BACKUP -> MASTER (master down)\n",
1124 		    sc->sc_vhid,
1125 		    sc->sc_carpdev->if_xname);
1126 		carp_master_down_locked(sc);
1127 	}
1128 	CURVNET_RESTORE();
1129 
1130 	CARP_UNLOCK(sc);
1131 }
1132 
1133 static void
1134 carp_master_down_locked(struct carp_softc *sc)
1135 {
1136 
1137 	CARP_LOCK_ASSERT(sc);
1138 
1139 	switch (sc->sc_state) {
1140 	case BACKUP:
1141 		carp_set_state(sc, MASTER);
1142 		carp_send_ad_locked(sc);
1143 #ifdef INET
1144 		carp_send_arp(sc);
1145 #endif
1146 #ifdef INET6
1147 		carp_send_na(sc);
1148 #endif
1149 		carp_setrun(sc, 0);
1150 		carp_addroute(sc);
1151 		break;
1152 	case INIT:
1153 	case MASTER:
1154 #ifdef INVARIANTS
1155 		panic("carp: VHID %u@%s: master_down event in %s state\n",
1156 		    sc->sc_vhid,
1157 		    sc->sc_carpdev->if_xname,
1158 		    sc->sc_state ? "MASTER" : "INIT");
1159 #endif
1160 		break;
1161 	}
1162 }
1163 
1164 /*
1165  * When in backup state, af indicates whether to reset the master down timer
1166  * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1167  */
1168 static void
1169 carp_setrun(struct carp_softc *sc, sa_family_t af)
1170 {
1171 	struct timeval tv;
1172 
1173 	CARP_LOCK_ASSERT(sc);
1174 
1175 	if ((sc->sc_carpdev->if_flags & IFF_UP) == 0 ||
1176 	    sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1177 	    (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0))
1178 		return;
1179 
1180 	switch (sc->sc_state) {
1181 	case INIT:
1182 		CARP_LOG("VHID %u@%s: INIT -> BACKUP\n",
1183 		    sc->sc_vhid,
1184 		    sc->sc_carpdev->if_xname);
1185 		carp_set_state(sc, BACKUP);
1186 		carp_setrun(sc, 0);
1187 		break;
1188 	case BACKUP:
1189 		callout_stop(&sc->sc_ad_tmo);
1190 		tv.tv_sec = 3 * sc->sc_advbase;
1191 		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1192 		switch (af) {
1193 #ifdef INET
1194 		case AF_INET:
1195 			callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1196 			    carp_master_down, sc);
1197 			break;
1198 #endif
1199 #ifdef INET6
1200 		case AF_INET6:
1201 			callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1202 			    carp_master_down, sc);
1203 			break;
1204 #endif
1205 		default:
1206 #ifdef INET
1207 			if (sc->sc_naddrs)
1208 				callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1209 				    carp_master_down, sc);
1210 #endif
1211 #ifdef INET6
1212 			if (sc->sc_naddrs6)
1213 				callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1214 				    carp_master_down, sc);
1215 #endif
1216 			break;
1217 		}
1218 		break;
1219 	case MASTER:
1220 		tv.tv_sec = sc->sc_advbase;
1221 		tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1222 		callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1223 		    carp_send_ad, sc);
1224 		break;
1225 	}
1226 }
1227 
1228 /*
1229  * Setup multicast structures.
1230  */
1231 static int
1232 carp_multicast_setup(struct carp_if *cif, sa_family_t sa)
1233 {
1234 	struct ifnet *ifp = cif->cif_ifp;
1235 	int error = 0;
1236 
1237 	CIF_LOCK_ASSERT(cif);
1238 
1239 	switch (sa) {
1240 #ifdef INET
1241 	case AF_INET:
1242 	    {
1243 		struct ip_moptions *imo = &cif->cif_imo;
1244 		struct in_addr addr;
1245 
1246 		if (imo->imo_membership)
1247 			return (0);
1248 
1249 		imo->imo_membership = (struct in_multi **)malloc(
1250 		    (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_CARP,
1251 		    M_NOWAIT);
1252 		if (imo->imo_membership == NULL)
1253 			return (ENOMEM);
1254 		imo->imo_mfilters = NULL;
1255 		imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
1256 		imo->imo_multicast_vif = -1;
1257 
1258 		addr.s_addr = htonl(INADDR_CARP_GROUP);
1259 		if ((error = in_joingroup(ifp, &addr, NULL,
1260 		    &imo->imo_membership[0])) != 0) {
1261 			free(imo->imo_membership, M_CARP);
1262 			break;
1263 		}
1264 		imo->imo_num_memberships++;
1265 		imo->imo_multicast_ifp = ifp;
1266 		imo->imo_multicast_ttl = CARP_DFLTTL;
1267 		imo->imo_multicast_loop = 0;
1268 		break;
1269 	   }
1270 #endif
1271 #ifdef INET6
1272 	case AF_INET6:
1273 	    {
1274 		struct ip6_moptions *im6o = &cif->cif_im6o;
1275 		struct in6_addr in6;
1276 		struct in6_multi *in6m;
1277 
1278 		if (im6o->im6o_membership)
1279 			return (0);
1280 
1281 		im6o->im6o_membership = (struct in6_multi **)malloc(
1282 		    (sizeof(struct in6_multi *) * IPV6_MIN_MEMBERSHIPS), M_CARP,
1283 		    M_ZERO | M_NOWAIT);
1284 		if (im6o->im6o_membership == NULL)
1285 			return (ENOMEM);
1286 		im6o->im6o_mfilters = NULL;
1287 		im6o->im6o_max_memberships = IPV6_MIN_MEMBERSHIPS;
1288 		im6o->im6o_multicast_hlim = CARP_DFLTTL;
1289 		im6o->im6o_multicast_ifp = ifp;
1290 
1291 		/* Join IPv6 CARP multicast group. */
1292 		bzero(&in6, sizeof(in6));
1293 		in6.s6_addr16[0] = htons(0xff02);
1294 		in6.s6_addr8[15] = 0x12;
1295 		if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1296 			free(im6o->im6o_membership, M_CARP);
1297 			break;
1298 		}
1299 		in6m = NULL;
1300 		if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
1301 			free(im6o->im6o_membership, M_CARP);
1302 			break;
1303 		}
1304 		im6o->im6o_membership[0] = in6m;
1305 		im6o->im6o_num_memberships++;
1306 
1307 		/* Join solicited multicast address. */
1308 		bzero(&in6, sizeof(in6));
1309 		in6.s6_addr16[0] = htons(0xff02);
1310 		in6.s6_addr32[1] = 0;
1311 		in6.s6_addr32[2] = htonl(1);
1312 		in6.s6_addr32[3] = 0;
1313 		in6.s6_addr8[12] = 0xff;
1314 		if ((error = in6_setscope(&in6, ifp, NULL)) != 0) {
1315 			in6_mc_leave(im6o->im6o_membership[0], NULL);
1316 			free(im6o->im6o_membership, M_CARP);
1317 			break;
1318 		}
1319 		in6m = NULL;
1320 		if ((error = in6_mc_join(ifp, &in6, NULL, &in6m, 0)) != 0) {
1321 			in6_mc_leave(im6o->im6o_membership[0], NULL);
1322 			free(im6o->im6o_membership, M_CARP);
1323 			break;
1324 		}
1325 		im6o->im6o_membership[1] = in6m;
1326 		im6o->im6o_num_memberships++;
1327 		break;
1328 	    }
1329 #endif
1330 	}
1331 
1332 	return (error);
1333 }
1334 
1335 /*
1336  * Free multicast structures.
1337  */
1338 static void
1339 carp_multicast_cleanup(struct carp_if *cif, sa_family_t sa)
1340 {
1341 
1342 	CIF_LOCK_ASSERT(cif);
1343 	switch (sa) {
1344 #ifdef INET
1345 	case AF_INET:
1346 		if (cif->cif_naddrs == 0) {
1347 			struct ip_moptions *imo = &cif->cif_imo;
1348 
1349 			in_leavegroup(imo->imo_membership[0], NULL);
1350 			KASSERT(imo->imo_mfilters == NULL,
1351 			    ("%s: imo_mfilters != NULL", __func__));
1352 			free(imo->imo_membership, M_CARP);
1353 			imo->imo_membership = NULL;
1354 
1355 		}
1356 		break;
1357 #endif
1358 #ifdef INET6
1359 	case AF_INET6:
1360 		if (cif->cif_naddrs6 == 0) {
1361 			struct ip6_moptions *im6o = &cif->cif_im6o;
1362 
1363 			in6_mc_leave(im6o->im6o_membership[0], NULL);
1364 			in6_mc_leave(im6o->im6o_membership[1], NULL);
1365 			KASSERT(im6o->im6o_mfilters == NULL,
1366 			    ("%s: im6o_mfilters != NULL", __func__));
1367 			free(im6o->im6o_membership, M_CARP);
1368 			im6o->im6o_membership = NULL;
1369 		}
1370 		break;
1371 #endif
1372 	}
1373 }
1374 
1375 int
1376 carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa)
1377 {
1378 	struct m_tag *mtag;
1379 	struct carp_softc *sc;
1380 
1381 	if (!sa)
1382 		return (0);
1383 
1384 	switch (sa->sa_family) {
1385 #ifdef INET
1386 	case AF_INET:
1387 		break;
1388 #endif
1389 #ifdef INET6
1390 	case AF_INET6:
1391 		break;
1392 #endif
1393 	default:
1394 		return (0);
1395 	}
1396 
1397 	mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
1398 	if (mtag == NULL)
1399 		return (0);
1400 
1401 	bcopy(mtag + 1, &sc, sizeof(sc));
1402 
1403 	/* Set the source MAC address to the Virtual Router MAC Address. */
1404 	switch (ifp->if_type) {
1405 	case IFT_ETHER:
1406 	case IFT_BRIDGE:
1407 	case IFT_L2VLAN: {
1408 			struct ether_header *eh;
1409 
1410 			eh = mtod(m, struct ether_header *);
1411 			eh->ether_shost[0] = 0;
1412 			eh->ether_shost[1] = 0;
1413 			eh->ether_shost[2] = 0x5e;
1414 			eh->ether_shost[3] = 0;
1415 			eh->ether_shost[4] = 1;
1416 			eh->ether_shost[5] = sc->sc_vhid;
1417 		}
1418 		break;
1419 	case IFT_FDDI: {
1420 			struct fddi_header *fh;
1421 
1422 			fh = mtod(m, struct fddi_header *);
1423 			fh->fddi_shost[0] = 0;
1424 			fh->fddi_shost[1] = 0;
1425 			fh->fddi_shost[2] = 0x5e;
1426 			fh->fddi_shost[3] = 0;
1427 			fh->fddi_shost[4] = 1;
1428 			fh->fddi_shost[5] = sc->sc_vhid;
1429 		}
1430 		break;
1431 	case IFT_ISO88025: {
1432  			struct iso88025_header *th;
1433  			th = mtod(m, struct iso88025_header *);
1434 			th->iso88025_shost[0] = 3;
1435 			th->iso88025_shost[1] = 0;
1436 			th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1);
1437 			th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1);
1438 			th->iso88025_shost[4] = 0;
1439 			th->iso88025_shost[5] = 0;
1440 		}
1441 		break;
1442 	default:
1443 		printf("%s: carp is not supported for the %d interface type\n",
1444 		    ifp->if_xname, ifp->if_type);
1445 		return (EOPNOTSUPP);
1446 	}
1447 
1448 	return (0);
1449 }
1450 
1451 static struct carp_softc*
1452 carp_alloc(struct ifnet *ifp)
1453 {
1454 	struct carp_softc *sc;
1455 	struct carp_if *cif;
1456 
1457 	if ((cif = ifp->if_carp) == NULL) {
1458 		cif = carp_alloc_if(ifp);
1459 		if (cif == NULL)
1460 			return (NULL);
1461 	}
1462 
1463 	sc = malloc(sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
1464 
1465 	sc->sc_advbase = CARP_DFLTINTV;
1466 	sc->sc_vhid = -1;	/* required setting */
1467 	sc->sc_init_counter = 1;
1468 	sc->sc_state = INIT;
1469 
1470 	sc->sc_ifasiz = sizeof(struct ifaddr *);
1471 	sc->sc_ifas = malloc(sc->sc_ifasiz, M_CARP, M_WAITOK|M_ZERO);
1472 	sc->sc_carpdev = ifp;
1473 
1474 	CARP_LOCK_INIT(sc);
1475 #ifdef INET
1476 	callout_init_mtx(&sc->sc_md_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1477 #endif
1478 #ifdef INET6
1479 	callout_init_mtx(&sc->sc_md6_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1480 #endif
1481 	callout_init_mtx(&sc->sc_ad_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1482 
1483 	CIF_LOCK(cif);
1484 	TAILQ_INSERT_TAIL(&cif->cif_vrs, sc, sc_list);
1485 	CIF_UNLOCK(cif);
1486 
1487 	mtx_lock(&carp_mtx);
1488 	LIST_INSERT_HEAD(&carp_list, sc, sc_next);
1489 	mtx_unlock(&carp_mtx);
1490 
1491 	return (sc);
1492 }
1493 
1494 static int
1495 carp_grow_ifas(struct carp_softc *sc)
1496 {
1497 	struct ifaddr **new;
1498 
1499 	CARP_LOCK_ASSERT(sc);
1500 
1501 	new = malloc(sc->sc_ifasiz * 2, M_CARP, M_NOWAIT|M_ZERO);
1502 	if (new == NULL)
1503 		return (ENOMEM);
1504 	bcopy(sc->sc_ifas, new, sc->sc_ifasiz);
1505 	free(sc->sc_ifas, M_CARP);
1506 	sc->sc_ifas = new;
1507 	sc->sc_ifasiz *= 2;
1508 
1509 	return (0);
1510 }
1511 
1512 static void
1513 carp_destroy(struct carp_softc *sc)
1514 {
1515 	struct ifnet *ifp = sc->sc_carpdev;
1516 	struct carp_if *cif = ifp->if_carp;
1517 
1518 	CIF_LOCK_ASSERT(cif);
1519 
1520 	TAILQ_REMOVE(&cif->cif_vrs, sc, sc_list);
1521 
1522 	mtx_lock(&carp_mtx);
1523 	LIST_REMOVE(sc, sc_next);
1524 	mtx_unlock(&carp_mtx);
1525 
1526 	CARP_LOCK(sc);
1527 	if (sc->sc_suppress)
1528 		carp_demote_adj(-carp_ifdown_adj, "vhid removed");
1529 	callout_drain(&sc->sc_ad_tmo);
1530 #ifdef INET
1531 	callout_drain(&sc->sc_md_tmo);
1532 #endif
1533 #ifdef INET6
1534 	callout_drain(&sc->sc_md6_tmo);
1535 #endif
1536 	CARP_LOCK_DESTROY(sc);
1537 
1538 	free(sc->sc_ifas, M_CARP);
1539 	free(sc, M_CARP);
1540 }
1541 
1542 static struct carp_if*
1543 carp_alloc_if(struct ifnet *ifp)
1544 {
1545 	struct carp_if *cif;
1546 
1547 	cif = malloc(sizeof(*cif), M_CARP, M_WAITOK|M_ZERO);
1548 
1549 	if (ifpromisc(ifp, 1) != 0)
1550 		goto cleanup;
1551 
1552 	CIF_LOCK_INIT(cif);
1553 	cif->cif_ifp = ifp;
1554 	TAILQ_INIT(&cif->cif_vrs);
1555 
1556 	IF_ADDR_WLOCK(ifp);
1557 	ifp->if_carp = cif;
1558 	if_ref(ifp);
1559 	IF_ADDR_WUNLOCK(ifp);
1560 
1561 	return (cif);
1562 
1563 cleanup:
1564 	free(cif, M_CARP);
1565 
1566 	return (NULL);
1567 }
1568 
1569 static void
1570 carp_free_if(struct carp_if *cif)
1571 {
1572 	struct ifnet *ifp = cif->cif_ifp;
1573 
1574 	CIF_LOCK_ASSERT(cif);
1575 	KASSERT(TAILQ_EMPTY(&cif->cif_vrs), ("%s: softc list not empty",
1576 	    __func__));
1577 
1578 	IF_ADDR_WLOCK(ifp);
1579 	ifp->if_carp = NULL;
1580 	if_rele(ifp);
1581 	IF_ADDR_WUNLOCK(ifp);
1582 
1583 	CIF_LOCK_DESTROY(cif);
1584 
1585 	ifpromisc(ifp, 0);
1586 
1587 	free(cif, M_CARP);
1588 }
1589 
1590 static void
1591 carp_carprcp(struct carpreq *carpr, struct carp_softc *sc, int priv)
1592 {
1593 
1594 	CARP_LOCK(sc);
1595 	carpr->carpr_state = sc->sc_state;
1596 	carpr->carpr_vhid = sc->sc_vhid;
1597 	carpr->carpr_advbase = sc->sc_advbase;
1598 	carpr->carpr_advskew = sc->sc_advskew;
1599 	if (priv)
1600 		bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key));
1601 	else
1602 		bzero(carpr->carpr_key, sizeof(carpr->carpr_key));
1603 	CARP_UNLOCK(sc);
1604 }
1605 
1606 int
1607 carp_ioctl(struct ifreq *ifr, u_long cmd, struct thread *td)
1608 {
1609 	struct carpreq carpr;
1610 	struct ifnet *ifp;
1611 	struct carp_softc *sc = NULL;
1612 	int error = 0, locked = 0;
1613 
1614 	if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
1615 		return (error);
1616 
1617 	ifp = ifunit_ref(ifr->ifr_name);
1618 	if (ifp == NULL)
1619 		return (ENXIO);
1620 
1621 	switch (ifp->if_type) {
1622 	case IFT_ETHER:
1623 	case IFT_L2VLAN:
1624 	case IFT_BRIDGE:
1625 	case IFT_FDDI:
1626 	case IFT_ISO88025:
1627 		break;
1628 	default:
1629 		error = EOPNOTSUPP;
1630 		goto out;
1631 	}
1632 
1633 	if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1634 		error = EADDRNOTAVAIL;
1635 		goto out;
1636 	}
1637 
1638 	switch (cmd) {
1639 	case SIOCSVH:
1640 		if ((error = priv_check(td, PRIV_NETINET_CARP)))
1641 			break;
1642 		if (carpr.carpr_vhid <= 0 || carpr.carpr_vhid > CARP_MAXVHID ||
1643 		    carpr.carpr_advbase < 0 || carpr.carpr_advskew < 0) {
1644 			error = EINVAL;
1645 			break;
1646 		}
1647 
1648 		if (ifp->if_carp) {
1649 			CIF_LOCK(ifp->if_carp);
1650 			IFNET_FOREACH_CARP(ifp, sc)
1651 				if (sc->sc_vhid == carpr.carpr_vhid)
1652 					break;
1653 			CIF_UNLOCK(ifp->if_carp);
1654 		}
1655 		if (sc == NULL) {
1656 			sc = carp_alloc(ifp);
1657 			if (sc == NULL) {
1658 				error = EINVAL; /* XXX: ifpromisc failed */
1659 				break;
1660 			}
1661 
1662 			CARP_LOCK(sc);
1663 			sc->sc_vhid = carpr.carpr_vhid;
1664 			LLADDR(&sc->sc_addr)[0] = 0;
1665 			LLADDR(&sc->sc_addr)[1] = 0;
1666 			LLADDR(&sc->sc_addr)[2] = 0x5e;
1667 			LLADDR(&sc->sc_addr)[3] = 0;
1668 			LLADDR(&sc->sc_addr)[4] = 1;
1669 			LLADDR(&sc->sc_addr)[5] = sc->sc_vhid;
1670 		} else
1671 			CARP_LOCK(sc);
1672 		locked = 1;
1673 		if (carpr.carpr_advbase > 0) {
1674 			if (carpr.carpr_advbase > 255 ||
1675 			    carpr.carpr_advbase < CARP_DFLTINTV) {
1676 				error = EINVAL;
1677 				break;
1678 			}
1679 			sc->sc_advbase = carpr.carpr_advbase;
1680 		}
1681 		if (carpr.carpr_advskew > 0) {
1682 			if (carpr.carpr_advskew >= 255) {
1683 				error = EINVAL;
1684 				break;
1685 			}
1686 			sc->sc_advskew = carpr.carpr_advskew;
1687 		}
1688 		if (carpr.carpr_key[0] != '\0') {
1689 			bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1690 			carp_hmac_prepare(sc);
1691 		}
1692 		if (sc->sc_state != INIT &&
1693 		    carpr.carpr_state != sc->sc_state) {
1694 			switch (carpr.carpr_state) {
1695 			case BACKUP:
1696 				callout_stop(&sc->sc_ad_tmo);
1697 				carp_set_state(sc, BACKUP);
1698 				carp_setrun(sc, 0);
1699 				carp_delroute(sc);
1700 				break;
1701 			case MASTER:
1702 				carp_master_down_locked(sc);
1703 				break;
1704 			default:
1705 				break;
1706 			}
1707 		}
1708 		break;
1709 
1710 	case SIOCGVH:
1711 	    {
1712 		int priveleged;
1713 
1714 		if (carpr.carpr_vhid < 0 || carpr.carpr_vhid > CARP_MAXVHID) {
1715 			error = EINVAL;
1716 			break;
1717 		}
1718 		if (carpr.carpr_count < 1) {
1719 			error = EMSGSIZE;
1720 			break;
1721 		}
1722 		if (ifp->if_carp == NULL) {
1723 			error = ENOENT;
1724 			break;
1725 		}
1726 
1727 		priveleged = (priv_check(td, PRIV_NETINET_CARP) == 0);
1728 		if (carpr.carpr_vhid != 0) {
1729 			CIF_LOCK(ifp->if_carp);
1730 			IFNET_FOREACH_CARP(ifp, sc)
1731 				if (sc->sc_vhid == carpr.carpr_vhid)
1732 					break;
1733 			CIF_UNLOCK(ifp->if_carp);
1734 			if (sc == NULL) {
1735 				error = ENOENT;
1736 				break;
1737 			}
1738 			carp_carprcp(&carpr, sc, priveleged);
1739 			error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
1740 		} else  {
1741 			int i, count;
1742 
1743 			count = 0;
1744 			CIF_LOCK(ifp->if_carp);
1745 			IFNET_FOREACH_CARP(ifp, sc)
1746 				count++;
1747 
1748 			if (count > carpr.carpr_count) {
1749 				CIF_UNLOCK(ifp->if_carp);
1750 				error = EMSGSIZE;
1751 				break;
1752 			}
1753 
1754 			i = 0;
1755 			IFNET_FOREACH_CARP(ifp, sc) {
1756 				carp_carprcp(&carpr, sc, priveleged);
1757 				carpr.carpr_count = count;
1758 				error = copyout(&carpr, ifr->ifr_data +
1759 				    (i * sizeof(carpr)), sizeof(carpr));
1760 				if (error) {
1761 					CIF_UNLOCK(ifp->if_carp);
1762 					break;
1763 				}
1764 				i++;
1765 			}
1766 			CIF_UNLOCK(ifp->if_carp);
1767 		}
1768 		break;
1769 	    }
1770 	default:
1771 		error = EINVAL;
1772 	}
1773 
1774 out:
1775 	if (locked)
1776 		CARP_UNLOCK(sc);
1777 	if_rele(ifp);
1778 
1779 	return (error);
1780 }
1781 
1782 static int
1783 carp_get_vhid(struct ifaddr *ifa)
1784 {
1785 
1786 	if (ifa == NULL || ifa->ifa_carp == NULL)
1787 		return (0);
1788 
1789 	return (ifa->ifa_carp->sc_vhid);
1790 }
1791 
1792 int
1793 carp_attach(struct ifaddr *ifa, int vhid)
1794 {
1795 	struct ifnet *ifp = ifa->ifa_ifp;
1796 	struct carp_if *cif = ifp->if_carp;
1797 	struct carp_softc *sc;
1798 	int index, error;
1799 
1800 	if (ifp->if_carp == NULL)
1801 		return (ENOPROTOOPT);
1802 
1803 	switch (ifa->ifa_addr->sa_family) {
1804 #ifdef INET
1805 	case AF_INET:
1806 #endif
1807 #ifdef INET6
1808 	case AF_INET6:
1809 #endif
1810 		break;
1811 	default:
1812 		return (EPROTOTYPE);
1813 	}
1814 
1815 	CIF_LOCK(cif);
1816 	IFNET_FOREACH_CARP(ifp, sc)
1817 		if (sc->sc_vhid == vhid)
1818 			break;
1819 	if (sc == NULL) {
1820 		CIF_UNLOCK(cif);
1821 		return (ENOENT);
1822 	}
1823 
1824 	if (ifa->ifa_carp) {
1825 		if (ifa->ifa_carp->sc_vhid != vhid)
1826 			carp_detach_locked(ifa);
1827 		else {
1828 			CIF_UNLOCK(cif);
1829 			return (0);
1830 		}
1831 	}
1832 
1833 	error = carp_multicast_setup(cif, ifa->ifa_addr->sa_family);
1834 	if (error) {
1835 		CIF_FREE(cif);
1836 		return (error);
1837 	}
1838 
1839 	CARP_LOCK(sc);
1840 	index = sc->sc_naddrs + sc->sc_naddrs6 + 1;
1841 	if (index > sc->sc_ifasiz / sizeof(struct ifaddr *))
1842 		if ((error = carp_grow_ifas(sc)) != 0) {
1843 			carp_multicast_cleanup(cif,
1844 			    ifa->ifa_addr->sa_family);
1845 			CARP_UNLOCK(sc);
1846 			CIF_FREE(cif);
1847 			return (error);
1848 		}
1849 
1850 	switch (ifa->ifa_addr->sa_family) {
1851 #ifdef INET
1852 	case AF_INET:
1853 		cif->cif_naddrs++;
1854 		sc->sc_naddrs++;
1855 		break;
1856 #endif
1857 #ifdef INET6
1858 	case AF_INET6:
1859 		cif->cif_naddrs6++;
1860 		sc->sc_naddrs6++;
1861 		break;
1862 #endif
1863 	}
1864 
1865 	ifa_ref(ifa);
1866 	sc->sc_ifas[index - 1] = ifa;
1867 	ifa->ifa_carp = sc;
1868 
1869 	carp_hmac_prepare(sc);
1870 	carp_sc_state(sc);
1871 
1872 	CARP_UNLOCK(sc);
1873 	CIF_UNLOCK(cif);
1874 
1875 	return (0);
1876 }
1877 
1878 void
1879 carp_detach(struct ifaddr *ifa)
1880 {
1881 	struct ifnet *ifp = ifa->ifa_ifp;
1882 	struct carp_if *cif = ifp->if_carp;
1883 
1884 	CIF_LOCK(cif);
1885 	carp_detach_locked(ifa);
1886 	CIF_FREE(cif);
1887 }
1888 
1889 static void
1890 carp_detach_locked(struct ifaddr *ifa)
1891 {
1892 	struct ifnet *ifp = ifa->ifa_ifp;
1893 	struct carp_if *cif = ifp->if_carp;
1894 	struct carp_softc *sc = ifa->ifa_carp;
1895 	int i, index;
1896 
1897 	KASSERT(sc != NULL, ("%s: %p not attached", __func__, ifa));
1898 
1899 	CIF_LOCK_ASSERT(cif);
1900 	CARP_LOCK(sc);
1901 
1902 	/* Shift array. */
1903 	index = sc->sc_naddrs + sc->sc_naddrs6;
1904 	for (i = 0; i < index; i++)
1905 		if (sc->sc_ifas[i] == ifa)
1906 			break;
1907 	KASSERT(i < index, ("%s: %p no backref", __func__, ifa));
1908 	for (; i < index - 1; i++)
1909 		sc->sc_ifas[i] = sc->sc_ifas[i+1];
1910 	sc->sc_ifas[index - 1] = NULL;
1911 
1912 	switch (ifa->ifa_addr->sa_family) {
1913 #ifdef INET
1914 	case AF_INET:
1915 		cif->cif_naddrs--;
1916 		sc->sc_naddrs--;
1917 		break;
1918 #endif
1919 #ifdef INET6
1920 	case AF_INET6:
1921 		cif->cif_naddrs6--;
1922 		sc->sc_naddrs6--;
1923 		break;
1924 #endif
1925 	}
1926 
1927 	carp_ifa_delroute(ifa);
1928 	carp_multicast_cleanup(cif, ifa->ifa_addr->sa_family);
1929 
1930 	ifa->ifa_carp = NULL;
1931 	ifa_free(ifa);
1932 
1933 	carp_hmac_prepare(sc);
1934 	carp_sc_state(sc);
1935 
1936 	if (sc->sc_naddrs == 0 && sc->sc_naddrs6 == 0) {
1937 		CARP_UNLOCK(sc);
1938 		carp_destroy(sc);
1939 	} else
1940 		CARP_UNLOCK(sc);
1941 }
1942 
1943 static void
1944 carp_set_state(struct carp_softc *sc, int state)
1945 {
1946 
1947 	CARP_LOCK_ASSERT(sc);
1948 
1949 	if (sc->sc_state != state) {
1950 		const char *carp_states[] = { CARP_STATES };
1951 		char subsys[IFNAMSIZ+5];
1952 
1953 		sc->sc_state = state;
1954 
1955 		snprintf(subsys, IFNAMSIZ+5, "%u@%s", sc->sc_vhid,
1956 		    sc->sc_carpdev->if_xname);
1957 		devctl_notify("CARP", subsys, carp_states[state], NULL);
1958 	}
1959 }
1960 
1961 static void
1962 carp_linkstate(struct ifnet *ifp)
1963 {
1964 	struct carp_softc *sc;
1965 
1966 	CIF_LOCK(ifp->if_carp);
1967 	IFNET_FOREACH_CARP(ifp, sc) {
1968 		CARP_LOCK(sc);
1969 		carp_sc_state(sc);
1970 		CARP_UNLOCK(sc);
1971 	}
1972 	CIF_UNLOCK(ifp->if_carp);
1973 }
1974 
1975 static void
1976 carp_sc_state(struct carp_softc *sc)
1977 {
1978 
1979 	CARP_LOCK_ASSERT(sc);
1980 
1981 	if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
1982 	    !(sc->sc_carpdev->if_flags & IFF_UP)) {
1983 		callout_stop(&sc->sc_ad_tmo);
1984 #ifdef INET
1985 		callout_stop(&sc->sc_md_tmo);
1986 #endif
1987 #ifdef INET6
1988 		callout_stop(&sc->sc_md6_tmo);
1989 #endif
1990 		carp_set_state(sc, INIT);
1991 		carp_setrun(sc, 0);
1992 		if (!sc->sc_suppress)
1993 			carp_demote_adj(carp_ifdown_adj, "interface down");
1994 		sc->sc_suppress = 1;
1995 	} else {
1996 		carp_set_state(sc, INIT);
1997 		carp_setrun(sc, 0);
1998 		if (sc->sc_suppress)
1999 			carp_demote_adj(-carp_ifdown_adj, "interface up");
2000 		sc->sc_suppress = 0;
2001 	}
2002 }
2003 
2004 static void
2005 carp_demote_adj(int adj, char *reason)
2006 {
2007 	carp_demotion += adj;
2008 	CARP_LOG("demoted by %d to %d (%s)\n", adj, carp_demotion, reason);
2009 	taskqueue_enqueue(taskqueue_swi, &carp_sendall_task);
2010 }
2011 
2012 #ifdef INET
2013 extern  struct domain inetdomain;
2014 static struct protosw in_carp_protosw = {
2015 	.pr_type =		SOCK_RAW,
2016 	.pr_domain =		&inetdomain,
2017 	.pr_protocol =		IPPROTO_CARP,
2018 	.pr_flags =		PR_ATOMIC|PR_ADDR,
2019 	.pr_input =		carp_input,
2020 	.pr_output =		(pr_output_t *)rip_output,
2021 	.pr_ctloutput =		rip_ctloutput,
2022 	.pr_usrreqs =		&rip_usrreqs
2023 };
2024 #endif
2025 
2026 #ifdef INET6
2027 extern	struct domain inet6domain;
2028 static struct ip6protosw in6_carp_protosw = {
2029 	.pr_type =		SOCK_RAW,
2030 	.pr_domain =		&inet6domain,
2031 	.pr_protocol =		IPPROTO_CARP,
2032 	.pr_flags =		PR_ATOMIC|PR_ADDR,
2033 	.pr_input =		carp6_input,
2034 	.pr_output =		rip6_output,
2035 	.pr_ctloutput =		rip6_ctloutput,
2036 	.pr_usrreqs =		&rip6_usrreqs
2037 };
2038 #endif
2039 
2040 static void
2041 carp_mod_cleanup(void)
2042 {
2043 
2044 #ifdef INET
2045 	if (proto_reg[CARP_INET] == 0) {
2046 		(void)ipproto_unregister(IPPROTO_CARP);
2047 		pf_proto_unregister(PF_INET, IPPROTO_CARP, SOCK_RAW);
2048 		proto_reg[CARP_INET] = -1;
2049 	}
2050 	carp_iamatch_p = NULL;
2051 #endif
2052 #ifdef INET6
2053 	if (proto_reg[CARP_INET6] == 0) {
2054 		(void)ip6proto_unregister(IPPROTO_CARP);
2055 		pf_proto_unregister(PF_INET6, IPPROTO_CARP, SOCK_RAW);
2056 		proto_reg[CARP_INET6] = -1;
2057 	}
2058 	carp_iamatch6_p = NULL;
2059 	carp_macmatch6_p = NULL;
2060 #endif
2061 	carp_ioctl_p = NULL;
2062 	carp_attach_p = NULL;
2063 	carp_detach_p = NULL;
2064 	carp_get_vhid_p = NULL;
2065 	carp_linkstate_p = NULL;
2066 	carp_forus_p = NULL;
2067 	carp_output_p = NULL;
2068 	carp_demote_adj_p = NULL;
2069 	mtx_unlock(&carp_mtx);
2070 	taskqueue_drain(taskqueue_swi, &carp_sendall_task);
2071 	mtx_destroy(&carp_mtx);
2072 }
2073 
2074 static int
2075 carp_mod_load(void)
2076 {
2077 	int err;
2078 
2079 	mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
2080 	LIST_INIT(&carp_list);
2081 	carp_get_vhid_p = carp_get_vhid;
2082 	carp_forus_p = carp_forus;
2083 	carp_output_p = carp_output;
2084 	carp_linkstate_p = carp_linkstate;
2085 	carp_ioctl_p = carp_ioctl;
2086 	carp_attach_p = carp_attach;
2087 	carp_detach_p = carp_detach;
2088 	carp_demote_adj_p = carp_demote_adj;
2089 #ifdef INET6
2090 	carp_iamatch6_p = carp_iamatch6;
2091 	carp_macmatch6_p = carp_macmatch6;
2092 	proto_reg[CARP_INET6] = pf_proto_register(PF_INET6,
2093 	    (struct protosw *)&in6_carp_protosw);
2094 	if (proto_reg[CARP_INET6]) {
2095 		printf("carp: error %d attaching to PF_INET6\n",
2096 		    proto_reg[CARP_INET6]);
2097 		carp_mod_cleanup();
2098 		return (proto_reg[CARP_INET6]);
2099 	}
2100 	err = ip6proto_register(IPPROTO_CARP);
2101 	if (err) {
2102 		printf("carp: error %d registering with INET6\n", err);
2103 		carp_mod_cleanup();
2104 		return (err);
2105 	}
2106 #endif
2107 #ifdef INET
2108 	carp_iamatch_p = carp_iamatch;
2109 	proto_reg[CARP_INET] = pf_proto_register(PF_INET, &in_carp_protosw);
2110 	if (proto_reg[CARP_INET]) {
2111 		printf("carp: error %d attaching to PF_INET\n",
2112 		    proto_reg[CARP_INET]);
2113 		carp_mod_cleanup();
2114 		return (proto_reg[CARP_INET]);
2115 	}
2116 	err = ipproto_register(IPPROTO_CARP);
2117 	if (err) {
2118 		printf("carp: error %d registering with INET\n", err);
2119 		carp_mod_cleanup();
2120 		return (err);
2121 	}
2122 #endif
2123 	return (0);
2124 }
2125 
2126 static int
2127 carp_modevent(module_t mod, int type, void *data)
2128 {
2129 	switch (type) {
2130 	case MOD_LOAD:
2131 		return carp_mod_load();
2132 		/* NOTREACHED */
2133 	case MOD_UNLOAD:
2134 		mtx_lock(&carp_mtx);
2135 		if (LIST_EMPTY(&carp_list))
2136 			carp_mod_cleanup();
2137 		else {
2138 			mtx_unlock(&carp_mtx);
2139 			return (EBUSY);
2140 		}
2141 		break;
2142 
2143 	default:
2144 		return (EINVAL);
2145 	}
2146 
2147 	return (0);
2148 }
2149 
2150 static moduledata_t carp_mod = {
2151 	"carp",
2152 	carp_modevent,
2153 	0
2154 };
2155 
2156 DECLARE_MODULE(carp, carp_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
2157