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