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