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