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