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