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