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