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