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