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