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