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