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