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