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