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