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