FreeBSD/Linux Kernel Cross Reference
sys/netinet/ip_carp.c
1 /* $FreeBSD: releng/6.1/sys/netinet/ip_carp.c 153719 2005-12-25 21:59:20Z mlaier $ */
2
3 /*
4 * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
5 * Copyright (c) 2003 Ryan McBride. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26 * THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "opt_carp.h"
30 #include "opt_bpf.h"
31 #include "opt_inet.h"
32 #include "opt_inet6.h"
33
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/conf.h>
38 #include <sys/kernel.h>
39 #include <sys/limits.h>
40 #include <sys/malloc.h>
41 #include <sys/mbuf.h>
42 #include <sys/module.h>
43 #include <sys/time.h>
44 #include <sys/proc.h>
45 #include <sys/sysctl.h>
46 #include <sys/syslog.h>
47 #include <sys/signalvar.h>
48 #include <sys/filio.h>
49 #include <sys/sockio.h>
50
51 #include <sys/socket.h>
52 #include <sys/vnode.h>
53
54 #include <machine/stdarg.h>
55
56 #include <net/bpf.h>
57 #include <net/ethernet.h>
58 #include <net/fddi.h>
59 #include <net/iso88025.h>
60 #include <net/if.h>
61 #include <net/if_clone.h>
62 #include <net/if_types.h>
63 #include <net/route.h>
64
65 #ifdef INET
66 #include <netinet/in.h>
67 #include <netinet/in_var.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/ip.h>
70 #include <netinet/ip_var.h>
71 #include <netinet/if_ether.h>
72 #include <machine/in_cksum.h>
73 #endif
74
75 #ifdef INET6
76 #include <netinet/icmp6.h>
77 #include <netinet/ip6.h>
78 #include <netinet6/ip6_var.h>
79 #include <netinet6/scope6_var.h>
80 #include <netinet6/nd6.h>
81 #include <net/if_dl.h>
82 #endif
83
84 #include <crypto/sha1.h>
85 #include <netinet/ip_carp.h>
86
87 #define CARP_IFNAME "carp"
88 static MALLOC_DEFINE(M_CARP, "CARP", "CARP interfaces");
89 SYSCTL_DECL(_net_inet_carp);
90
91 struct carp_softc {
92 struct ifnet *sc_ifp; /* Interface clue */
93 struct ifnet *sc_carpdev; /* Pointer to parent interface */
94 struct in_ifaddr *sc_ia; /* primary iface address */
95 struct ip_moptions sc_imo;
96 #ifdef INET6
97 struct in6_ifaddr *sc_ia6; /* primary iface address v6 */
98 struct ip6_moptions sc_im6o;
99 #endif /* INET6 */
100 TAILQ_ENTRY(carp_softc) sc_list;
101
102 enum { INIT = 0, BACKUP, MASTER } sc_state;
103
104 int sc_flags_backup;
105 int sc_suppress;
106
107 int sc_sendad_errors;
108 #define CARP_SENDAD_MAX_ERRORS 3
109 int sc_sendad_success;
110 #define CARP_SENDAD_MIN_SUCCESS 3
111
112 int sc_vhid;
113 int sc_advskew;
114 int sc_naddrs;
115 int sc_naddrs6;
116 int sc_advbase; /* seconds */
117 int sc_init_counter;
118 u_int64_t sc_counter;
119
120 /* authentication */
121 #define CARP_HMAC_PAD 64
122 unsigned char sc_key[CARP_KEY_LEN];
123 unsigned char sc_pad[CARP_HMAC_PAD];
124 SHA1_CTX sc_sha1;
125
126 struct callout sc_ad_tmo; /* advertisement timeout */
127 struct callout sc_md_tmo; /* master down timeout */
128 struct callout sc_md6_tmo; /* master down timeout */
129
130 LIST_ENTRY(carp_softc) sc_next; /* Interface clue */
131 };
132 #define SC2IFP(sc) ((sc)->sc_ifp)
133
134 int carp_suppress_preempt = 0;
135 int carp_opts[CARPCTL_MAXID] = { 0, 1, 0, 1, 0, 0 }; /* XXX for now */
136 SYSCTL_INT(_net_inet_carp, CARPCTL_ALLOW, allow, CTLFLAG_RW,
137 &carp_opts[CARPCTL_ALLOW], 0, "Accept incoming CARP packets");
138 SYSCTL_INT(_net_inet_carp, CARPCTL_PREEMPT, preempt, CTLFLAG_RW,
139 &carp_opts[CARPCTL_PREEMPT], 0, "high-priority backup preemption mode");
140 SYSCTL_INT(_net_inet_carp, CARPCTL_LOG, log, CTLFLAG_RW,
141 &carp_opts[CARPCTL_LOG], 0, "log bad carp packets");
142 SYSCTL_INT(_net_inet_carp, CARPCTL_ARPBALANCE, arpbalance, CTLFLAG_RW,
143 &carp_opts[CARPCTL_ARPBALANCE], 0, "balance arp responses");
144 SYSCTL_INT(_net_inet_carp, OID_AUTO, suppress_preempt, CTLFLAG_RD,
145 &carp_suppress_preempt, 0, "Preemption is suppressed");
146
147 struct carpstats carpstats;
148 SYSCTL_STRUCT(_net_inet_carp, CARPCTL_STATS, stats, CTLFLAG_RW,
149 &carpstats, carpstats,
150 "CARP statistics (struct carpstats, netinet/ip_carp.h)");
151
152 struct carp_if {
153 TAILQ_HEAD(, carp_softc) vhif_vrs;
154 int vhif_nvrs;
155
156 struct ifnet *vhif_ifp;
157 struct mtx vhif_mtx;
158 };
159
160 /* Get carp_if from softc. Valid after carp_set_addr{,6}. */
161 #define SC2CIF(sc) ((struct carp_if *)(sc)->sc_carpdev->if_carp)
162
163 /* lock per carp_if queue */
164 #define CARP_LOCK_INIT(cif) mtx_init(&(cif)->vhif_mtx, "carp_if", \
165 NULL, MTX_DEF)
166 #define CARP_LOCK_DESTROY(cif) mtx_destroy(&(cif)->vhif_mtx)
167 #define CARP_LOCK_ASSERT(cif) mtx_assert(&(cif)->vhif_mtx, MA_OWNED)
168 #define CARP_LOCK(cif) mtx_lock(&(cif)->vhif_mtx)
169 #define CARP_UNLOCK(cif) mtx_unlock(&(cif)->vhif_mtx)
170
171 #define CARP_SCLOCK(sc) mtx_lock(&SC2CIF(sc)->vhif_mtx)
172 #define CARP_SCUNLOCK(sc) mtx_unlock(&SC2CIF(sc)->vhif_mtx)
173 #define CARP_SCLOCK_ASSERT(sc) mtx_assert(&SC2CIF(sc)->vhif_mtx, MA_OWNED)
174
175 #define CARP_LOG(...) do { \
176 if (carp_opts[CARPCTL_LOG] > 0) \
177 log(LOG_INFO, __VA_ARGS__); \
178 } while (0)
179
180 #define CARP_DEBUG(...) do { \
181 if (carp_opts[CARPCTL_LOG] > 1) \
182 log(LOG_DEBUG, __VA_ARGS__); \
183 } while (0)
184
185 static void carp_hmac_prepare(struct carp_softc *);
186 static void carp_hmac_generate(struct carp_softc *, u_int32_t *,
187 unsigned char *);
188 static int carp_hmac_verify(struct carp_softc *, u_int32_t *,
189 unsigned char *);
190 static void carp_setroute(struct carp_softc *, int);
191 static void carp_input_c(struct mbuf *, struct carp_header *, sa_family_t);
192 static int carp_clone_create(struct if_clone *, int);
193 static void carp_clone_destroy(struct ifnet *);
194 static void carpdetach(struct carp_softc *);
195 static int carp_prepare_ad(struct mbuf *, struct carp_softc *,
196 struct carp_header *);
197 static void carp_send_ad_all(void);
198 static void carp_send_ad(void *);
199 static void carp_send_ad_locked(struct carp_softc *);
200 static void carp_send_arp(struct carp_softc *);
201 static void carp_master_down(void *);
202 static void carp_master_down_locked(struct carp_softc *);
203 static int carp_ioctl(struct ifnet *, u_long, caddr_t);
204 static int carp_looutput(struct ifnet *, struct mbuf *, struct sockaddr *,
205 struct rtentry *);
206 static void carp_start(struct ifnet *);
207 static void carp_setrun(struct carp_softc *, sa_family_t);
208 static void carp_set_state(struct carp_softc *, int);
209 static int carp_addrcount(struct carp_if *, struct in_ifaddr *, int);
210 enum { CARP_COUNT_MASTER, CARP_COUNT_RUNNING };
211
212 static int carp_set_addr(struct carp_softc *, struct sockaddr_in *);
213 static int carp_del_addr(struct carp_softc *, struct sockaddr_in *);
214 static void carp_carpdev_state_locked(struct carp_if *);
215 static void carp_sc_state_locked(struct carp_softc *);
216 #ifdef INET6
217 static void carp_send_na(struct carp_softc *);
218 static int carp_set_addr6(struct carp_softc *, struct sockaddr_in6 *);
219 static int carp_del_addr6(struct carp_softc *, struct sockaddr_in6 *);
220 #endif
221
222 static LIST_HEAD(, carp_softc) carpif_list;
223 static struct mtx carp_mtx;
224 IFC_SIMPLE_DECLARE(carp, 0);
225
226 static __inline u_int16_t
227 carp_cksum(struct mbuf *m, int len)
228 {
229 return (in_cksum(m, len));
230 }
231
232 static void
233 carp_hmac_prepare(struct carp_softc *sc)
234 {
235 u_int8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
236 u_int8_t vhid = sc->sc_vhid & 0xff;
237 struct ifaddr *ifa;
238 int i;
239 #ifdef INET6
240 struct in6_addr in6;
241 #endif
242
243 if (sc->sc_carpdev)
244 CARP_SCLOCK(sc);
245
246 /* XXX: possible race here */
247
248 /* compute ipad from key */
249 bzero(sc->sc_pad, sizeof(sc->sc_pad));
250 bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
251 for (i = 0; i < sizeof(sc->sc_pad); i++)
252 sc->sc_pad[i] ^= 0x36;
253
254 /* precompute first part of inner hash */
255 SHA1Init(&sc->sc_sha1);
256 SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
257 SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
258 SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
259 SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
260 #ifdef INET
261 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
262 if (ifa->ifa_addr->sa_family == AF_INET)
263 SHA1Update(&sc->sc_sha1,
264 (void *)&ifatoia(ifa)->ia_addr.sin_addr.s_addr,
265 sizeof(struct in_addr));
266 }
267 #endif /* INET */
268 #ifdef INET6
269 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
270 if (ifa->ifa_addr->sa_family == AF_INET6) {
271 in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
272 in6_clearscope(&in6);
273 SHA1Update(&sc->sc_sha1, (void *)&in6, sizeof(in6));
274 }
275 }
276 #endif /* INET6 */
277
278 /* convert ipad to opad */
279 for (i = 0; i < sizeof(sc->sc_pad); i++)
280 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
281
282 if (sc->sc_carpdev)
283 CARP_SCUNLOCK(sc);
284 }
285
286 static void
287 carp_hmac_generate(struct carp_softc *sc, u_int32_t counter[2],
288 unsigned char md[20])
289 {
290 SHA1_CTX sha1ctx;
291
292 /* fetch first half of inner hash */
293 bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
294
295 SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
296 SHA1Final(md, &sha1ctx);
297
298 /* outer hash */
299 SHA1Init(&sha1ctx);
300 SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
301 SHA1Update(&sha1ctx, md, 20);
302 SHA1Final(md, &sha1ctx);
303 }
304
305 static int
306 carp_hmac_verify(struct carp_softc *sc, u_int32_t counter[2],
307 unsigned char md[20])
308 {
309 unsigned char md2[20];
310
311 CARP_SCLOCK_ASSERT(sc);
312
313 carp_hmac_generate(sc, counter, md2);
314
315 return (bcmp(md, md2, sizeof(md2)));
316 }
317
318 static void
319 carp_setroute(struct carp_softc *sc, int cmd)
320 {
321 struct ifaddr *ifa;
322 int s;
323
324 if (sc->sc_carpdev)
325 CARP_SCLOCK_ASSERT(sc);
326
327 s = splnet();
328 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
329 if (ifa->ifa_addr->sa_family == AF_INET &&
330 sc->sc_carpdev != NULL) {
331 int count = carp_addrcount(
332 (struct carp_if *)sc->sc_carpdev->if_carp,
333 ifatoia(ifa), CARP_COUNT_MASTER);
334
335 if ((cmd == RTM_ADD && count == 1) ||
336 (cmd == RTM_DELETE && count == 0))
337 rtinit(ifa, cmd, RTF_UP | RTF_HOST);
338 }
339 #ifdef INET6
340 if (ifa->ifa_addr->sa_family == AF_INET6) {
341 if (cmd == RTM_ADD)
342 in6_ifaddloop(ifa);
343 else
344 in6_ifremloop(ifa);
345 }
346 #endif /* INET6 */
347 }
348 splx(s);
349 }
350
351 static int
352 carp_clone_create(struct if_clone *ifc, int unit)
353 {
354
355 struct carp_softc *sc;
356 struct ifnet *ifp;
357
358 MALLOC(sc, struct carp_softc *, sizeof(*sc), M_CARP, M_WAITOK|M_ZERO);
359 ifp = SC2IFP(sc) = if_alloc(IFT_ETHER);
360 if (ifp == NULL) {
361 FREE(sc, M_CARP);
362 return (ENOSPC);
363 }
364
365 sc->sc_flags_backup = 0;
366 sc->sc_suppress = 0;
367 sc->sc_advbase = CARP_DFLTINTV;
368 sc->sc_vhid = -1; /* required setting */
369 sc->sc_advskew = 0;
370 sc->sc_init_counter = 1;
371 sc->sc_naddrs = sc->sc_naddrs6 = 0; /* M_ZERO? */
372 #ifdef INET6
373 sc->sc_im6o.im6o_multicast_hlim = CARP_DFLTTL;
374 #endif
375
376 callout_init(&sc->sc_ad_tmo, NET_CALLOUT_MPSAFE);
377 callout_init(&sc->sc_md_tmo, NET_CALLOUT_MPSAFE);
378 callout_init(&sc->sc_md6_tmo, NET_CALLOUT_MPSAFE);
379
380 ifp->if_softc = sc;
381 if_initname(ifp, CARP_IFNAME, unit);
382 ifp->if_mtu = ETHERMTU;
383 ifp->if_flags = IFF_LOOPBACK;
384 ifp->if_ioctl = carp_ioctl;
385 ifp->if_output = carp_looutput;
386 ifp->if_start = carp_start;
387 ifp->if_type = IFT_CARP;
388 ifp->if_snd.ifq_maxlen = ifqmaxlen;
389 ifp->if_hdrlen = 0;
390 if_attach(ifp);
391 bpfattach(SC2IFP(sc), DLT_NULL, sizeof(u_int32_t));
392 mtx_lock(&carp_mtx);
393 LIST_INSERT_HEAD(&carpif_list, sc, sc_next);
394 mtx_unlock(&carp_mtx);
395 return (0);
396 }
397
398 static void
399 carp_clone_destroy(struct ifnet *ifp)
400 {
401 struct carp_softc *sc = ifp->if_softc;
402 struct carp_if *cif;
403 struct ip_moptions *imo = &sc->sc_imo;
404 #ifdef INET6
405 struct ip6_moptions *im6o = &sc->sc_im6o;
406 #endif
407
408 /* carpdetach(sc); */
409
410 /*
411 * If an interface is destroyed which is suppressing the preemption,
412 * decrease the global counter, otherwise the host will never get
413 * out of the carp supressing state.
414 */
415 if (sc->sc_suppress)
416 carp_suppress_preempt--;
417 sc->sc_suppress = 0;
418
419 callout_stop(&sc->sc_ad_tmo);
420 callout_stop(&sc->sc_md_tmo);
421 callout_stop(&sc->sc_md6_tmo);
422
423 if (imo->imo_num_memberships) {
424 in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
425 imo->imo_multicast_ifp = NULL;
426 }
427 #ifdef INET6
428 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
429 struct in6_multi_mship *imm =
430 LIST_FIRST(&im6o->im6o_memberships);
431 LIST_REMOVE(imm, i6mm_chain);
432 in6_leavegroup(imm);
433 }
434 im6o->im6o_multicast_ifp = NULL;
435 #endif
436
437 /* Remove ourself from parents if_carp queue */
438 if (sc->sc_carpdev && (cif = sc->sc_carpdev->if_carp)) {
439 CARP_LOCK(cif);
440 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
441 if (!--cif->vhif_nvrs) {
442 sc->sc_carpdev->if_carp = NULL;
443 CARP_LOCK_DESTROY(cif);
444 FREE(cif, M_CARP);
445 ifpromisc(sc->sc_carpdev, 0);
446 sc->sc_carpdev = NULL;
447 } else {
448 CARP_UNLOCK(cif);
449 }
450 }
451
452 mtx_lock(&carp_mtx);
453 LIST_REMOVE(sc, sc_next);
454 mtx_unlock(&carp_mtx);
455 bpfdetach(ifp);
456 if_detach(ifp);
457 if_free_type(ifp, IFT_ETHER);
458 free(sc, M_CARP);
459 }
460
461 /*
462 * process input packet.
463 * we have rearranged checks order compared to the rfc,
464 * but it seems more efficient this way or not possible otherwise.
465 */
466 void
467 carp_input(struct mbuf *m, int hlen)
468 {
469 struct ip *ip = mtod(m, struct ip *);
470 struct carp_header *ch;
471 int iplen, len;
472
473 carpstats.carps_ipackets++;
474
475 if (!carp_opts[CARPCTL_ALLOW]) {
476 m_freem(m);
477 return;
478 }
479
480 /* check if received on a valid carp interface */
481 if (m->m_pkthdr.rcvif->if_carp == NULL) {
482 carpstats.carps_badif++;
483 CARP_LOG("carp_input: packet received on non-carp "
484 "interface: %s\n",
485 m->m_pkthdr.rcvif->if_xname);
486 m_freem(m);
487 return;
488 }
489
490 /* verify that the IP TTL is 255. */
491 if (ip->ip_ttl != CARP_DFLTTL) {
492 carpstats.carps_badttl++;
493 CARP_LOG("carp_input: received ttl %d != 255i on %s\n",
494 ip->ip_ttl,
495 m->m_pkthdr.rcvif->if_xname);
496 m_freem(m);
497 return;
498 }
499
500 iplen = ip->ip_hl << 2;
501
502 if (m->m_pkthdr.len < iplen + sizeof(*ch)) {
503 carpstats.carps_badlen++;
504 CARP_LOG("carp_input: received len %zd < "
505 "sizeof(struct carp_header)\n",
506 m->m_len - sizeof(struct ip));
507 m_freem(m);
508 return;
509 }
510
511 if (iplen + sizeof(*ch) < m->m_len) {
512 if ((m = m_pullup(m, iplen + sizeof(*ch))) == NULL) {
513 carpstats.carps_hdrops++;
514 CARP_LOG("carp_input: pullup failed\n");
515 return;
516 }
517 ip = mtod(m, struct ip *);
518 }
519 ch = (struct carp_header *)((char *)ip + iplen);
520
521 /*
522 * verify that the received packet length is
523 * equal to the CARP header
524 */
525 len = iplen + sizeof(*ch);
526 if (len > m->m_pkthdr.len) {
527 carpstats.carps_badlen++;
528 CARP_LOG("carp_input: packet too short %d on %s\n",
529 m->m_pkthdr.len,
530 m->m_pkthdr.rcvif->if_xname);
531 m_freem(m);
532 return;
533 }
534
535 if ((m = m_pullup(m, len)) == NULL) {
536 carpstats.carps_hdrops++;
537 return;
538 }
539 ip = mtod(m, struct ip *);
540 ch = (struct carp_header *)((char *)ip + iplen);
541
542 /* verify the CARP checksum */
543 m->m_data += iplen;
544 if (carp_cksum(m, len - iplen)) {
545 carpstats.carps_badsum++;
546 CARP_LOG("carp_input: checksum failed on %s\n",
547 m->m_pkthdr.rcvif->if_xname);
548 m_freem(m);
549 return;
550 }
551 m->m_data -= iplen;
552
553 carp_input_c(m, ch, AF_INET);
554 }
555
556 #ifdef INET6
557 int
558 carp6_input(struct mbuf **mp, int *offp, int proto)
559 {
560 struct mbuf *m = *mp;
561 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
562 struct carp_header *ch;
563 u_int len;
564
565 carpstats.carps_ipackets6++;
566
567 if (!carp_opts[CARPCTL_ALLOW]) {
568 m_freem(m);
569 return (IPPROTO_DONE);
570 }
571
572 /* check if received on a valid carp interface */
573 if (m->m_pkthdr.rcvif->if_carp == NULL) {
574 carpstats.carps_badif++;
575 CARP_LOG("carp6_input: packet received on non-carp "
576 "interface: %s\n",
577 m->m_pkthdr.rcvif->if_xname);
578 m_freem(m);
579 return (IPPROTO_DONE);
580 }
581
582 /* verify that the IP TTL is 255 */
583 if (ip6->ip6_hlim != CARP_DFLTTL) {
584 carpstats.carps_badttl++;
585 CARP_LOG("carp6_input: received ttl %d != 255 on %s\n",
586 ip6->ip6_hlim,
587 m->m_pkthdr.rcvif->if_xname);
588 m_freem(m);
589 return (IPPROTO_DONE);
590 }
591
592 /* verify that we have a complete carp packet */
593 len = m->m_len;
594 IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
595 if (ch == NULL) {
596 carpstats.carps_badlen++;
597 CARP_LOG("carp6_input: packet size %u too small\n", len);
598 return (IPPROTO_DONE);
599 }
600
601
602 /* verify the CARP checksum */
603 m->m_data += *offp;
604 if (carp_cksum(m, sizeof(*ch))) {
605 carpstats.carps_badsum++;
606 CARP_LOG("carp6_input: checksum failed, on %s\n",
607 m->m_pkthdr.rcvif->if_xname);
608 m_freem(m);
609 return (IPPROTO_DONE);
610 }
611 m->m_data -= *offp;
612
613 carp_input_c(m, ch, AF_INET6);
614 return (IPPROTO_DONE);
615 }
616 #endif /* INET6 */
617
618 static void
619 carp_input_c(struct mbuf *m, struct carp_header *ch, sa_family_t af)
620 {
621 struct ifnet *ifp = m->m_pkthdr.rcvif;
622 struct carp_softc *sc;
623 u_int64_t tmp_counter;
624 struct timeval sc_tv, ch_tv;
625
626 /* verify that the VHID is valid on the receiving interface */
627 CARP_LOCK(ifp->if_carp);
628 TAILQ_FOREACH(sc, &((struct carp_if *)ifp->if_carp)->vhif_vrs, sc_list)
629 if (sc->sc_vhid == ch->carp_vhid)
630 break;
631
632 if (!sc || !((SC2IFP(sc)->if_flags & IFF_UP) &&
633 (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING))) {
634 carpstats.carps_badvhid++;
635 CARP_UNLOCK(ifp->if_carp);
636 m_freem(m);
637 return;
638 }
639
640 getmicrotime(&SC2IFP(sc)->if_lastchange);
641 SC2IFP(sc)->if_ipackets++;
642 SC2IFP(sc)->if_ibytes += m->m_pkthdr.len;
643
644 if (SC2IFP(sc)->if_bpf) {
645 struct ip *ip = mtod(m, struct ip *);
646 uint32_t af1 = af;
647
648 /* BPF wants net byte order */
649 ip->ip_len = htons(ip->ip_len + (ip->ip_hl << 2));
650 ip->ip_off = htons(ip->ip_off);
651 bpf_mtap2(SC2IFP(sc)->if_bpf, &af1, sizeof(af1), m);
652 }
653
654 /* verify the CARP version. */
655 if (ch->carp_version != CARP_VERSION) {
656 carpstats.carps_badver++;
657 SC2IFP(sc)->if_ierrors++;
658 CARP_UNLOCK(ifp->if_carp);
659 CARP_LOG("%s; invalid version %d\n",
660 SC2IFP(sc)->if_xname,
661 ch->carp_version);
662 m_freem(m);
663 return;
664 }
665
666 /* verify the hash */
667 if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
668 carpstats.carps_badauth++;
669 SC2IFP(sc)->if_ierrors++;
670 CARP_UNLOCK(ifp->if_carp);
671 CARP_LOG("%s: incorrect hash\n", SC2IFP(sc)->if_xname);
672 m_freem(m);
673 return;
674 }
675
676 tmp_counter = ntohl(ch->carp_counter[0]);
677 tmp_counter = tmp_counter<<32;
678 tmp_counter += ntohl(ch->carp_counter[1]);
679
680 /* XXX Replay protection goes here */
681
682 sc->sc_init_counter = 0;
683 sc->sc_counter = tmp_counter;
684
685 sc_tv.tv_sec = sc->sc_advbase;
686 if (carp_suppress_preempt && sc->sc_advskew < 240)
687 sc_tv.tv_usec = 240 * 1000000 / 256;
688 else
689 sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256;
690 ch_tv.tv_sec = ch->carp_advbase;
691 ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
692
693 switch (sc->sc_state) {
694 case INIT:
695 break;
696 case MASTER:
697 /*
698 * If we receive an advertisement from a master who's going to
699 * be more frequent than us, go into BACKUP state.
700 */
701 if (timevalcmp(&sc_tv, &ch_tv, >) ||
702 timevalcmp(&sc_tv, &ch_tv, ==)) {
703 callout_stop(&sc->sc_ad_tmo);
704 CARP_DEBUG("%s: MASTER -> BACKUP "
705 "(more frequent advertisement received)\n",
706 SC2IFP(sc)->if_xname);
707 carp_set_state(sc, BACKUP);
708 carp_setrun(sc, 0);
709 carp_setroute(sc, RTM_DELETE);
710 }
711 break;
712 case BACKUP:
713 /*
714 * If we're pre-empting masters who advertise slower than us,
715 * and this one claims to be slower, treat him as down.
716 */
717 if (carp_opts[CARPCTL_PREEMPT] &&
718 timevalcmp(&sc_tv, &ch_tv, <)) {
719 CARP_DEBUG("%s: BACKUP -> MASTER "
720 "(preempting a slower master)\n",
721 SC2IFP(sc)->if_xname);
722 carp_master_down_locked(sc);
723 break;
724 }
725
726 /*
727 * If the master is going to advertise at such a low frequency
728 * that he's guaranteed to time out, we'd might as well just
729 * treat him as timed out now.
730 */
731 sc_tv.tv_sec = sc->sc_advbase * 3;
732 if (timevalcmp(&sc_tv, &ch_tv, <)) {
733 CARP_DEBUG("%s: BACKUP -> MASTER "
734 "(master timed out)\n",
735 SC2IFP(sc)->if_xname);
736 carp_master_down_locked(sc);
737 break;
738 }
739
740 /*
741 * Otherwise, we reset the counter and wait for the next
742 * advertisement.
743 */
744 carp_setrun(sc, af);
745 break;
746 }
747
748 CARP_UNLOCK(ifp->if_carp);
749
750 m_freem(m);
751 return;
752 }
753
754 static void
755 carpdetach(struct carp_softc *sc)
756 {
757 struct ifaddr *ifa;
758
759 callout_stop(&sc->sc_ad_tmo);
760 callout_stop(&sc->sc_md_tmo);
761 callout_stop(&sc->sc_md6_tmo);
762
763 while ((ifa = TAILQ_FIRST(&SC2IFP(sc)->if_addrlist)) != NULL)
764 if (ifa->ifa_addr->sa_family == AF_INET) {
765 struct in_ifaddr *ia = ifatoia(ifa);
766
767 carp_del_addr(sc, &ia->ia_addr);
768
769 /* ripped screaming from in_control(SIOCDIFADDR) */
770 in_ifscrub(SC2IFP(sc), ia);
771 TAILQ_REMOVE(&SC2IFP(sc)->if_addrlist, ifa, ifa_link);
772 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
773 IFAFREE((&ia->ia_ifa));
774 }
775 }
776
777 /* Detach an interface from the carp. */
778 void
779 carp_ifdetach(struct ifnet *ifp)
780 {
781 struct carp_softc *sc;
782 struct carp_if *cif = (struct carp_if *)ifp->if_carp;
783
784 CARP_LOCK(cif);
785 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list)
786 carpdetach(sc);
787 CARP_UNLOCK(cif);
788 }
789
790 static int
791 carp_prepare_ad(struct mbuf *m, struct carp_softc *sc, struct carp_header *ch)
792 {
793 struct m_tag *mtag;
794 struct ifnet *ifp = SC2IFP(sc);
795
796 if (sc->sc_init_counter) {
797 /* this could also be seconds since unix epoch */
798 sc->sc_counter = arc4random();
799 sc->sc_counter = sc->sc_counter << 32;
800 sc->sc_counter += arc4random();
801 } else
802 sc->sc_counter++;
803
804 ch->carp_counter[0] = htonl((sc->sc_counter>>32)&0xffffffff);
805 ch->carp_counter[1] = htonl(sc->sc_counter&0xffffffff);
806
807 carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
808
809 /* Tag packet for carp_output */
810 mtag = m_tag_get(PACKET_TAG_CARP, sizeof(struct ifnet *), M_NOWAIT);
811 if (mtag == NULL) {
812 m_freem(m);
813 SC2IFP(sc)->if_oerrors++;
814 return (ENOMEM);
815 }
816 bcopy(&ifp, (caddr_t)(mtag + 1), sizeof(struct ifnet *));
817 m_tag_prepend(m, mtag);
818
819 return (0);
820 }
821
822 static void
823 carp_send_ad_all(void)
824 {
825 struct carp_softc *sc;
826
827 mtx_lock(&carp_mtx);
828 LIST_FOREACH(sc, &carpif_list, sc_next) {
829 if (sc->sc_carpdev == NULL)
830 continue;
831 CARP_SCLOCK(sc);
832 if ((SC2IFP(sc)->if_flags & IFF_UP) &&
833 (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING) &&
834 sc->sc_state == MASTER)
835 carp_send_ad_locked(sc);
836 CARP_SCUNLOCK(sc);
837 }
838 mtx_unlock(&carp_mtx);
839 }
840
841 static void
842 carp_send_ad(void *v)
843 {
844 struct carp_softc *sc = v;
845
846 CARP_SCLOCK(sc);
847 carp_send_ad_locked(sc);
848 CARP_SCUNLOCK(sc);
849 }
850
851 static void
852 carp_send_ad_locked(struct carp_softc *sc)
853 {
854 struct carp_header ch;
855 struct timeval tv;
856 struct carp_header *ch_ptr;
857 struct mbuf *m;
858 int len, advbase, advskew;
859
860 CARP_SCLOCK_ASSERT(sc);
861
862 /* bow out if we've lost our UPness or RUNNINGuiness */
863 if (!((SC2IFP(sc)->if_flags & IFF_UP) &&
864 (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING))) {
865 advbase = 255;
866 advskew = 255;
867 } else {
868 advbase = sc->sc_advbase;
869 if (!carp_suppress_preempt || sc->sc_advskew > 240)
870 advskew = sc->sc_advskew;
871 else
872 advskew = 240;
873 tv.tv_sec = advbase;
874 tv.tv_usec = advskew * 1000000 / 256;
875 }
876
877 ch.carp_version = CARP_VERSION;
878 ch.carp_type = CARP_ADVERTISEMENT;
879 ch.carp_vhid = sc->sc_vhid;
880 ch.carp_advbase = advbase;
881 ch.carp_advskew = advskew;
882 ch.carp_authlen = 7; /* XXX DEFINE */
883 ch.carp_pad1 = 0; /* must be zero */
884 ch.carp_cksum = 0;
885
886 #ifdef INET
887 if (sc->sc_ia) {
888 struct ip *ip;
889
890 MGETHDR(m, M_DONTWAIT, MT_HEADER);
891 if (m == NULL) {
892 SC2IFP(sc)->if_oerrors++;
893 carpstats.carps_onomem++;
894 /* XXX maybe less ? */
895 if (advbase != 255 || advskew != 255)
896 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
897 carp_send_ad, sc);
898 return;
899 }
900 len = sizeof(*ip) + sizeof(ch);
901 m->m_pkthdr.len = len;
902 m->m_pkthdr.rcvif = NULL;
903 m->m_len = len;
904 MH_ALIGN(m, m->m_len);
905 m->m_flags |= M_MCAST;
906 ip = mtod(m, struct ip *);
907 ip->ip_v = IPVERSION;
908 ip->ip_hl = sizeof(*ip) >> 2;
909 ip->ip_tos = IPTOS_LOWDELAY;
910 ip->ip_len = len;
911 ip->ip_id = ip_newid();
912 ip->ip_off = IP_DF;
913 ip->ip_ttl = CARP_DFLTTL;
914 ip->ip_p = IPPROTO_CARP;
915 ip->ip_sum = 0;
916 ip->ip_src.s_addr = sc->sc_ia->ia_addr.sin_addr.s_addr;
917 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
918
919 ch_ptr = (struct carp_header *)(&ip[1]);
920 bcopy(&ch, ch_ptr, sizeof(ch));
921 if (carp_prepare_ad(m, sc, ch_ptr))
922 return;
923
924 m->m_data += sizeof(*ip);
925 ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip));
926 m->m_data -= sizeof(*ip);
927
928 getmicrotime(&SC2IFP(sc)->if_lastchange);
929 SC2IFP(sc)->if_opackets++;
930 SC2IFP(sc)->if_obytes += len;
931 carpstats.carps_opackets++;
932
933 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) {
934 SC2IFP(sc)->if_oerrors++;
935 if (sc->sc_sendad_errors < INT_MAX)
936 sc->sc_sendad_errors++;
937 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
938 carp_suppress_preempt++;
939 if (carp_suppress_preempt == 1) {
940 CARP_SCUNLOCK(sc);
941 carp_send_ad_all();
942 CARP_SCLOCK(sc);
943 }
944 }
945 sc->sc_sendad_success = 0;
946 } else {
947 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
948 if (++sc->sc_sendad_success >=
949 CARP_SENDAD_MIN_SUCCESS) {
950 carp_suppress_preempt--;
951 sc->sc_sendad_errors = 0;
952 }
953 } else
954 sc->sc_sendad_errors = 0;
955 }
956 }
957 #endif /* INET */
958 #ifdef INET6
959 if (sc->sc_ia6) {
960 struct ip6_hdr *ip6;
961
962 MGETHDR(m, M_DONTWAIT, MT_HEADER);
963 if (m == NULL) {
964 SC2IFP(sc)->if_oerrors++;
965 carpstats.carps_onomem++;
966 /* XXX maybe less ? */
967 if (advbase != 255 || advskew != 255)
968 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
969 carp_send_ad, sc);
970 return;
971 }
972 len = sizeof(*ip6) + sizeof(ch);
973 m->m_pkthdr.len = len;
974 m->m_pkthdr.rcvif = NULL;
975 m->m_len = len;
976 MH_ALIGN(m, m->m_len);
977 m->m_flags |= M_MCAST;
978 ip6 = mtod(m, struct ip6_hdr *);
979 bzero(ip6, sizeof(*ip6));
980 ip6->ip6_vfc |= IPV6_VERSION;
981 ip6->ip6_hlim = CARP_DFLTTL;
982 ip6->ip6_nxt = IPPROTO_CARP;
983 bcopy(&sc->sc_ia6->ia_addr.sin6_addr, &ip6->ip6_src,
984 sizeof(struct in6_addr));
985 /* set the multicast destination */
986
987 ip6->ip6_dst.s6_addr8[0] = 0xff;
988 ip6->ip6_dst.s6_addr8[1] = 0x02;
989 ip6->ip6_dst.s6_addr8[15] = 0x12;
990
991 ch_ptr = (struct carp_header *)(&ip6[1]);
992 bcopy(&ch, ch_ptr, sizeof(ch));
993 if (carp_prepare_ad(m, sc, ch_ptr))
994 return;
995
996 m->m_data += sizeof(*ip6);
997 ch_ptr->carp_cksum = carp_cksum(m, len - sizeof(*ip6));
998 m->m_data -= sizeof(*ip6);
999
1000 getmicrotime(&SC2IFP(sc)->if_lastchange);
1001 SC2IFP(sc)->if_opackets++;
1002 SC2IFP(sc)->if_obytes += len;
1003 carpstats.carps_opackets6++;
1004
1005 if (ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL)) {
1006 SC2IFP(sc)->if_oerrors++;
1007 if (sc->sc_sendad_errors < INT_MAX)
1008 sc->sc_sendad_errors++;
1009 if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
1010 carp_suppress_preempt++;
1011 if (carp_suppress_preempt == 1) {
1012 CARP_SCUNLOCK(sc);
1013 carp_send_ad_all();
1014 CARP_SCLOCK(sc);
1015 }
1016 }
1017 sc->sc_sendad_success = 0;
1018 } else {
1019 if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
1020 if (++sc->sc_sendad_success >=
1021 CARP_SENDAD_MIN_SUCCESS) {
1022 carp_suppress_preempt--;
1023 sc->sc_sendad_errors = 0;
1024 }
1025 } else
1026 sc->sc_sendad_errors = 0;
1027 }
1028 }
1029 #endif /* INET6 */
1030
1031 if (advbase != 255 || advskew != 255)
1032 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1033 carp_send_ad, sc);
1034
1035 }
1036
1037 /*
1038 * Broadcast a gratuitous ARP request containing
1039 * the virtual router MAC address for each IP address
1040 * associated with the virtual router.
1041 */
1042 static void
1043 carp_send_arp(struct carp_softc *sc)
1044 {
1045 struct ifaddr *ifa;
1046
1047 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1048
1049 if (ifa->ifa_addr->sa_family != AF_INET)
1050 continue;
1051
1052 /* arprequest(sc->sc_carpdev, &in, &in, IFP2ENADDR(sc->sc_ifp)); */
1053 arp_ifinit2(sc->sc_carpdev, ifa, IFP2ENADDR(sc->sc_ifp));
1054
1055 DELAY(1000); /* XXX */
1056 }
1057 }
1058
1059 #ifdef INET6
1060 static void
1061 carp_send_na(struct carp_softc *sc)
1062 {
1063 struct ifaddr *ifa;
1064 struct in6_addr *in6;
1065 static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1066
1067 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1068
1069 if (ifa->ifa_addr->sa_family != AF_INET6)
1070 continue;
1071
1072 in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
1073 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1074 ND_NA_FLAG_OVERRIDE, 1, NULL);
1075 DELAY(1000); /* XXX */
1076 }
1077 }
1078 #endif /* INET6 */
1079
1080 static int
1081 carp_addrcount(struct carp_if *cif, struct in_ifaddr *ia, int type)
1082 {
1083 struct carp_softc *vh;
1084 struct ifaddr *ifa;
1085 int count = 0;
1086
1087 CARP_LOCK_ASSERT(cif);
1088
1089 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1090 if ((type == CARP_COUNT_RUNNING &&
1091 (SC2IFP(vh)->if_flags & IFF_UP) &&
1092 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING)) ||
1093 (type == CARP_COUNT_MASTER && vh->sc_state == MASTER)) {
1094 TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist,
1095 ifa_list) {
1096 if (ifa->ifa_addr->sa_family == AF_INET &&
1097 ia->ia_addr.sin_addr.s_addr ==
1098 ifatoia(ifa)->ia_addr.sin_addr.s_addr)
1099 count++;
1100 }
1101 }
1102 }
1103 return (count);
1104 }
1105
1106 int
1107 carp_iamatch(void *v, struct in_ifaddr *ia,
1108 struct in_addr *isaddr, u_int8_t **enaddr)
1109 {
1110 struct carp_if *cif = v;
1111 struct carp_softc *vh;
1112 int index, count = 0;
1113 struct ifaddr *ifa;
1114
1115 CARP_LOCK(cif);
1116
1117 if (carp_opts[CARPCTL_ARPBALANCE]) {
1118 /*
1119 * XXX proof of concept implementation.
1120 * We use the source ip to decide which virtual host should
1121 * handle the request. If we're master of that virtual host,
1122 * then we respond, otherwise, just drop the arp packet on
1123 * the floor.
1124 */
1125 count = carp_addrcount(cif, ia, CARP_COUNT_RUNNING);
1126 if (count == 0) {
1127 /* should never reach this */
1128 CARP_UNLOCK(cif);
1129 return (0);
1130 }
1131
1132 /* this should be a hash, like pf_hash() */
1133 index = ntohl(isaddr->s_addr) % count;
1134 count = 0;
1135
1136 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1137 if ((SC2IFP(vh)->if_flags & IFF_UP) &&
1138 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING)) {
1139 TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist,
1140 ifa_list) {
1141 if (ifa->ifa_addr->sa_family ==
1142 AF_INET &&
1143 ia->ia_addr.sin_addr.s_addr ==
1144 ifatoia(ifa)->ia_addr.sin_addr.s_addr) {
1145 if (count == index) {
1146 if (vh->sc_state ==
1147 MASTER) {
1148 *enaddr = IFP2ENADDR(vh->sc_ifp);
1149 CARP_UNLOCK(cif);
1150 return (1);
1151 } else {
1152 CARP_UNLOCK(cif);
1153 return (0);
1154 }
1155 }
1156 count++;
1157 }
1158 }
1159 }
1160 }
1161 } else {
1162 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1163 if ((SC2IFP(vh)->if_flags & IFF_UP) &&
1164 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
1165 ia->ia_ifp == SC2IFP(vh) &&
1166 vh->sc_state == MASTER) {
1167 *enaddr = IFP2ENADDR(vh->sc_ifp);
1168 CARP_UNLOCK(cif);
1169 return (1);
1170 }
1171 }
1172 }
1173 CARP_UNLOCK(cif);
1174 return (0);
1175 }
1176
1177 #ifdef INET6
1178 struct ifaddr *
1179 carp_iamatch6(void *v, struct in6_addr *taddr)
1180 {
1181 struct carp_if *cif = v;
1182 struct carp_softc *vh;
1183 struct ifaddr *ifa;
1184
1185 CARP_LOCK(cif);
1186 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1187 TAILQ_FOREACH(ifa, &SC2IFP(vh)->if_addrlist, ifa_list) {
1188 if (IN6_ARE_ADDR_EQUAL(taddr,
1189 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1190 (SC2IFP(vh)->if_flags & IFF_UP) &&
1191 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
1192 vh->sc_state == MASTER) {
1193 CARP_UNLOCK(cif);
1194 return (ifa);
1195 }
1196 }
1197 }
1198 CARP_UNLOCK(cif);
1199
1200 return (NULL);
1201 }
1202
1203 void *
1204 carp_macmatch6(void *v, struct mbuf *m, const struct in6_addr *taddr)
1205 {
1206 struct m_tag *mtag;
1207 struct carp_if *cif = v;
1208 struct carp_softc *sc;
1209 struct ifaddr *ifa;
1210
1211 CARP_LOCK(cif);
1212 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
1213 TAILQ_FOREACH(ifa, &SC2IFP(sc)->if_addrlist, ifa_list) {
1214 if (IN6_ARE_ADDR_EQUAL(taddr,
1215 &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1216 (SC2IFP(sc)->if_flags & IFF_UP) &&
1217 (SC2IFP(sc)->if_drv_flags & IFF_DRV_RUNNING)) {
1218 struct ifnet *ifp = SC2IFP(sc);
1219 mtag = m_tag_get(PACKET_TAG_CARP,
1220 sizeof(struct ifnet *), M_NOWAIT);
1221 if (mtag == NULL) {
1222 /* better a bit than nothing */
1223 CARP_UNLOCK(cif);
1224 return (IFP2ENADDR(sc->sc_ifp));
1225 }
1226 bcopy(&ifp, (caddr_t)(mtag + 1),
1227 sizeof(struct ifnet *));
1228 m_tag_prepend(m, mtag);
1229
1230 CARP_UNLOCK(cif);
1231 return (IFP2ENADDR(sc->sc_ifp));
1232 }
1233 }
1234 }
1235 CARP_UNLOCK(cif);
1236
1237 return (NULL);
1238 }
1239 #endif
1240
1241 struct ifnet *
1242 carp_forus(void *v, void *dhost)
1243 {
1244 struct carp_if *cif = v;
1245 struct carp_softc *vh;
1246 u_int8_t *ena = dhost;
1247
1248 if (ena[0] || ena[1] || ena[2] != 0x5e || ena[3] || ena[4] != 1)
1249 return (NULL);
1250
1251 CARP_LOCK(cif);
1252 TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list)
1253 if ((SC2IFP(vh)->if_flags & IFF_UP) &&
1254 (SC2IFP(vh)->if_drv_flags & IFF_DRV_RUNNING) &&
1255 vh->sc_state == MASTER &&
1256 !bcmp(dhost, IFP2ENADDR(vh->sc_ifp), ETHER_ADDR_LEN)) {
1257 CARP_UNLOCK(cif);
1258 return (SC2IFP(vh));
1259 }
1260
1261 CARP_UNLOCK(cif);
1262 return (NULL);
1263 }
1264
1265 static void
1266 carp_master_down(void *v)
1267 {
1268 struct carp_softc *sc = v;
1269
1270 CARP_SCLOCK(sc);
1271 carp_master_down_locked(sc);
1272 CARP_SCUNLOCK(sc);
1273 }
1274
1275 static void
1276 carp_master_down_locked(struct carp_softc *sc)
1277 {
1278 if (sc->sc_carpdev)
1279 CARP_SCLOCK_ASSERT(sc);
1280
1281 switch (sc->sc_state) {
1282 case INIT:
1283 printf("%s: master_down event in INIT state\n",
1284 SC2IFP(sc)->if_xname);
1285 break;
1286 case MASTER:
1287 break;
1288 case BACKUP:
1289 carp_set_state(sc, MASTER);
1290 carp_send_ad_locked(sc);
1291 carp_send_arp(sc);
1292 #ifdef INET6
1293 carp_send_na(sc);
1294 #endif /* INET6 */
1295 carp_setrun(sc, 0);
1296 carp_setroute(sc, RTM_ADD);
1297 break;
1298 }
1299 }
1300
1301 /*
1302 * When in backup state, af indicates whether to reset the master down timer
1303 * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1304 */
1305 static void
1306 carp_setrun(struct carp_softc *sc, sa_family_t af)
1307 {
1308 struct timeval tv;
1309
1310 if (sc->sc_carpdev)
1311 CARP_SCLOCK_ASSERT(sc);
1312
1313 if (SC2IFP(sc)->if_flags & IFF_UP &&
1314 sc->sc_vhid > 0 && (sc->sc_naddrs || sc->sc_naddrs6))
1315 SC2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
1316 else {
1317 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1318 carp_setroute(sc, RTM_DELETE);
1319 return;
1320 }
1321
1322 switch (sc->sc_state) {
1323 case INIT:
1324 if (carp_opts[CARPCTL_PREEMPT] && !carp_suppress_preempt) {
1325 carp_send_ad_locked(sc);
1326 carp_send_arp(sc);
1327 #ifdef INET6
1328 carp_send_na(sc);
1329 #endif /* INET6 */
1330 CARP_DEBUG("%s: INIT -> MASTER (preempting)\n",
1331 SC2IFP(sc)->if_xname);
1332 carp_set_state(sc, MASTER);
1333 carp_setroute(sc, RTM_ADD);
1334 } else {
1335 CARP_DEBUG("%s: INIT -> BACKUP\n", SC2IFP(sc)->if_xname);
1336 carp_set_state(sc, BACKUP);
1337 carp_setroute(sc, RTM_DELETE);
1338 carp_setrun(sc, 0);
1339 }
1340 break;
1341 case BACKUP:
1342 callout_stop(&sc->sc_ad_tmo);
1343 tv.tv_sec = 3 * sc->sc_advbase;
1344 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1345 switch (af) {
1346 #ifdef INET
1347 case AF_INET:
1348 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1349 carp_master_down, sc);
1350 break;
1351 #endif /* INET */
1352 #ifdef INET6
1353 case AF_INET6:
1354 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1355 carp_master_down, sc);
1356 break;
1357 #endif /* INET6 */
1358 default:
1359 if (sc->sc_naddrs)
1360 callout_reset(&sc->sc_md_tmo, tvtohz(&tv),
1361 carp_master_down, sc);
1362 if (sc->sc_naddrs6)
1363 callout_reset(&sc->sc_md6_tmo, tvtohz(&tv),
1364 carp_master_down, sc);
1365 break;
1366 }
1367 break;
1368 case MASTER:
1369 tv.tv_sec = sc->sc_advbase;
1370 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1371 callout_reset(&sc->sc_ad_tmo, tvtohz(&tv),
1372 carp_send_ad, sc);
1373 break;
1374 }
1375 }
1376
1377 static int
1378 carp_set_addr(struct carp_softc *sc, struct sockaddr_in *sin)
1379 {
1380 struct ifnet *ifp;
1381 struct carp_if *cif;
1382 struct in_ifaddr *ia, *ia_if;
1383 struct ip_moptions *imo = &sc->sc_imo;
1384 struct in_addr addr;
1385 u_long iaddr = htonl(sin->sin_addr.s_addr);
1386 int own, error;
1387
1388 if (sin->sin_addr.s_addr == 0) {
1389 if (!(SC2IFP(sc)->if_flags & IFF_UP))
1390 carp_set_state(sc, INIT);
1391 if (sc->sc_naddrs)
1392 SC2IFP(sc)->if_flags |= IFF_UP;
1393 carp_setrun(sc, 0);
1394 return (0);
1395 }
1396
1397 /* we have to do it by hands to check we won't match on us */
1398 ia_if = NULL; own = 0;
1399 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
1400 /* and, yeah, we need a multicast-capable iface too */
1401 if (ia->ia_ifp != SC2IFP(sc) &&
1402 (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1403 (iaddr & ia->ia_subnetmask) == ia->ia_subnet) {
1404 if (!ia_if)
1405 ia_if = ia;
1406 if (sin->sin_addr.s_addr ==
1407 ia->ia_addr.sin_addr.s_addr)
1408 own++;
1409 }
1410 }
1411
1412 if (!ia_if)
1413 return (EADDRNOTAVAIL);
1414
1415 ia = ia_if;
1416 ifp = ia->ia_ifp;
1417
1418 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1419 (imo->imo_multicast_ifp && imo->imo_multicast_ifp != ifp))
1420 return (EADDRNOTAVAIL);
1421
1422 if (imo->imo_num_memberships == 0) {
1423 addr.s_addr = htonl(INADDR_CARP_GROUP);
1424 if ((imo->imo_membership[0] = in_addmulti(&addr, ifp)) == NULL)
1425 return (ENOBUFS);
1426 imo->imo_num_memberships++;
1427 imo->imo_multicast_ifp = ifp;
1428 imo->imo_multicast_ttl = CARP_DFLTTL;
1429 imo->imo_multicast_loop = 0;
1430 }
1431
1432 if (!ifp->if_carp) {
1433
1434 MALLOC(cif, struct carp_if *, sizeof(*cif), M_CARP,
1435 M_WAITOK|M_ZERO);
1436 if (!cif) {
1437 error = ENOBUFS;
1438 goto cleanup;
1439 }
1440 if ((error = ifpromisc(ifp, 1))) {
1441 FREE(cif, M_CARP);
1442 goto cleanup;
1443 }
1444
1445 CARP_LOCK_INIT(cif);
1446 CARP_LOCK(cif);
1447 cif->vhif_ifp = ifp;
1448 TAILQ_INIT(&cif->vhif_vrs);
1449 ifp->if_carp = cif;
1450
1451 } else {
1452 struct carp_softc *vr;
1453
1454 cif = (struct carp_if *)ifp->if_carp;
1455 CARP_LOCK(cif);
1456 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1457 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1458 CARP_UNLOCK(cif);
1459 error = EINVAL;
1460 goto cleanup;
1461 }
1462 }
1463 sc->sc_ia = ia;
1464 sc->sc_carpdev = ifp;
1465
1466 { /* XXX prevent endless loop if already in queue */
1467 struct carp_softc *vr, *after = NULL;
1468 int myself = 0;
1469 cif = (struct carp_if *)ifp->if_carp;
1470
1471 /* XXX: cif should not change, right? So we still hold the lock */
1472 CARP_LOCK_ASSERT(cif);
1473
1474 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1475 if (vr == sc)
1476 myself = 1;
1477 if (vr->sc_vhid < sc->sc_vhid)
1478 after = vr;
1479 }
1480
1481 if (!myself) {
1482 /* We're trying to keep things in order */
1483 if (after == NULL) {
1484 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1485 } else {
1486 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1487 }
1488 cif->vhif_nvrs++;
1489 }
1490 }
1491
1492 sc->sc_naddrs++;
1493 SC2IFP(sc)->if_flags |= IFF_UP;
1494 if (own)
1495 sc->sc_advskew = 0;
1496 carp_sc_state_locked(sc);
1497 carp_setrun(sc, 0);
1498
1499 CARP_UNLOCK(cif);
1500
1501 return (0);
1502
1503 cleanup:
1504 in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1505 return (error);
1506 }
1507
1508 static int
1509 carp_del_addr(struct carp_softc *sc, struct sockaddr_in *sin)
1510 {
1511 int error = 0;
1512
1513 if (!--sc->sc_naddrs) {
1514 struct carp_if *cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1515 struct ip_moptions *imo = &sc->sc_imo;
1516
1517 CARP_LOCK(cif);
1518 callout_stop(&sc->sc_ad_tmo);
1519 SC2IFP(sc)->if_flags &= ~IFF_UP;
1520 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1521 sc->sc_vhid = -1;
1522 in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1523 imo->imo_multicast_ifp = NULL;
1524 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1525 if (!--cif->vhif_nvrs) {
1526 sc->sc_carpdev->if_carp = NULL;
1527 CARP_LOCK_DESTROY(cif);
1528 FREE(cif, M_IFADDR);
1529 } else {
1530 CARP_UNLOCK(cif);
1531 }
1532 }
1533
1534 return (error);
1535 }
1536
1537 #ifdef INET6
1538 static int
1539 carp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1540 {
1541 struct ifnet *ifp;
1542 struct carp_if *cif;
1543 struct in6_ifaddr *ia, *ia_if;
1544 struct ip6_moptions *im6o = &sc->sc_im6o;
1545 struct in6_multi_mship *imm;
1546 struct in6_addr in6;
1547 int own, error;
1548
1549 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1550 if (!(SC2IFP(sc)->if_flags & IFF_UP))
1551 carp_set_state(sc, INIT);
1552 if (sc->sc_naddrs6)
1553 SC2IFP(sc)->if_flags |= IFF_UP;
1554 carp_setrun(sc, 0);
1555 return (0);
1556 }
1557
1558 /* we have to do it by hands to check we won't match on us */
1559 ia_if = NULL; own = 0;
1560 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1561 int i;
1562
1563 for (i = 0; i < 4; i++) {
1564 if ((sin6->sin6_addr.s6_addr32[i] &
1565 ia->ia_prefixmask.sin6_addr.s6_addr32[i]) !=
1566 (ia->ia_addr.sin6_addr.s6_addr32[i] &
1567 ia->ia_prefixmask.sin6_addr.s6_addr32[i]))
1568 break;
1569 }
1570 /* and, yeah, we need a multicast-capable iface too */
1571 if (ia->ia_ifp != SC2IFP(sc) &&
1572 (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1573 (i == 4)) {
1574 if (!ia_if)
1575 ia_if = ia;
1576 if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
1577 &ia->ia_addr.sin6_addr))
1578 own++;
1579 }
1580 }
1581
1582 if (!ia_if)
1583 return (EADDRNOTAVAIL);
1584 ia = ia_if;
1585 ifp = ia->ia_ifp;
1586
1587 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1588 (im6o->im6o_multicast_ifp && im6o->im6o_multicast_ifp != ifp))
1589 return (EADDRNOTAVAIL);
1590
1591 if (!sc->sc_naddrs6) {
1592 im6o->im6o_multicast_ifp = ifp;
1593
1594 /* join CARP multicast address */
1595 bzero(&in6, sizeof(in6));
1596 in6.s6_addr16[0] = htons(0xff02);
1597 in6.s6_addr8[15] = 0x12;
1598 if (in6_setscope(&in6, ifp, NULL) != 0)
1599 goto cleanup;
1600 if ((imm = in6_joingroup(ifp, &in6, &error, 0)) == NULL)
1601 goto cleanup;
1602 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1603
1604 /* join solicited multicast address */
1605 bzero(&in6, sizeof(in6));
1606 in6.s6_addr16[0] = htons(0xff02);
1607 in6.s6_addr32[1] = 0;
1608 in6.s6_addr32[2] = htonl(1);
1609 in6.s6_addr32[3] = sin6->sin6_addr.s6_addr32[3];
1610 in6.s6_addr8[12] = 0xff;
1611 if (in6_setscope(&in6, ifp, NULL) != 0)
1612 goto cleanup;
1613 if ((imm = in6_joingroup(ifp, &in6, &error, 0)) == NULL)
1614 goto cleanup;
1615 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1616 }
1617
1618 if (!ifp->if_carp) {
1619 MALLOC(cif, struct carp_if *, sizeof(*cif), M_CARP,
1620 M_WAITOK|M_ZERO);
1621 if (!cif) {
1622 error = ENOBUFS;
1623 goto cleanup;
1624 }
1625 if ((error = ifpromisc(ifp, 1))) {
1626 FREE(cif, M_CARP);
1627 goto cleanup;
1628 }
1629
1630 CARP_LOCK_INIT(cif);
1631 CARP_LOCK(cif);
1632 cif->vhif_ifp = ifp;
1633 TAILQ_INIT(&cif->vhif_vrs);
1634 ifp->if_carp = cif;
1635
1636 } else {
1637 struct carp_softc *vr;
1638
1639 cif = (struct carp_if *)ifp->if_carp;
1640 CARP_LOCK(cif);
1641 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1642 if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1643 CARP_UNLOCK(cif);
1644 error = EINVAL;
1645 goto cleanup;
1646 }
1647 }
1648 sc->sc_ia6 = ia;
1649 sc->sc_carpdev = ifp;
1650
1651 { /* XXX prevent endless loop if already in queue */
1652 struct carp_softc *vr, *after = NULL;
1653 int myself = 0;
1654 cif = (struct carp_if *)ifp->if_carp;
1655 CARP_LOCK_ASSERT(cif);
1656
1657 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1658 if (vr == sc)
1659 myself = 1;
1660 if (vr->sc_vhid < sc->sc_vhid)
1661 after = vr;
1662 }
1663
1664 if (!myself) {
1665 /* We're trying to keep things in order */
1666 if (after == NULL) {
1667 TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1668 } else {
1669 TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1670 }
1671 cif->vhif_nvrs++;
1672 }
1673 }
1674
1675 sc->sc_naddrs6++;
1676 SC2IFP(sc)->if_flags |= IFF_UP;
1677 if (own)
1678 sc->sc_advskew = 0;
1679 carp_sc_state_locked(sc);
1680 carp_setrun(sc, 0);
1681
1682 CARP_UNLOCK(cif);
1683
1684 return (0);
1685
1686 cleanup:
1687 /* clean up multicast memberships */
1688 if (!sc->sc_naddrs6) {
1689 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1690 imm = LIST_FIRST(&im6o->im6o_memberships);
1691 LIST_REMOVE(imm, i6mm_chain);
1692 in6_leavegroup(imm);
1693 }
1694 }
1695 return (error);
1696 }
1697
1698 static int
1699 carp_del_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1700 {
1701 int error = 0;
1702
1703 if (!--sc->sc_naddrs6) {
1704 struct carp_if *cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1705 struct ip6_moptions *im6o = &sc->sc_im6o;
1706
1707 CARP_LOCK(cif);
1708 callout_stop(&sc->sc_ad_tmo);
1709 SC2IFP(sc)->if_flags &= ~IFF_UP;
1710 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
1711 sc->sc_vhid = -1;
1712 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1713 struct in6_multi_mship *imm =
1714 LIST_FIRST(&im6o->im6o_memberships);
1715
1716 LIST_REMOVE(imm, i6mm_chain);
1717 in6_leavegroup(imm);
1718 }
1719 im6o->im6o_multicast_ifp = NULL;
1720 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1721 if (!--cif->vhif_nvrs) {
1722 CARP_LOCK_DESTROY(cif);
1723 sc->sc_carpdev->if_carp = NULL;
1724 FREE(cif, M_IFADDR);
1725 } else
1726 CARP_UNLOCK(cif);
1727 }
1728
1729 return (error);
1730 }
1731 #endif /* INET6 */
1732
1733 static int
1734 carp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr)
1735 {
1736 struct carp_softc *sc = ifp->if_softc, *vr;
1737 struct carpreq carpr;
1738 struct ifaddr *ifa;
1739 struct ifreq *ifr;
1740 struct ifaliasreq *ifra;
1741 int locked = 0, error = 0;
1742
1743 ifa = (struct ifaddr *)addr;
1744 ifra = (struct ifaliasreq *)addr;
1745 ifr = (struct ifreq *)addr;
1746
1747 switch (cmd) {
1748 case SIOCSIFADDR:
1749 switch (ifa->ifa_addr->sa_family) {
1750 #ifdef INET
1751 case AF_INET:
1752 SC2IFP(sc)->if_flags |= IFF_UP;
1753 bcopy(ifa->ifa_addr, ifa->ifa_dstaddr,
1754 sizeof(struct sockaddr));
1755 error = carp_set_addr(sc, satosin(ifa->ifa_addr));
1756 break;
1757 #endif /* INET */
1758 #ifdef INET6
1759 case AF_INET6:
1760 SC2IFP(sc)->if_flags |= IFF_UP;
1761 error = carp_set_addr6(sc, satosin6(ifa->ifa_addr));
1762 break;
1763 #endif /* INET6 */
1764 default:
1765 error = EAFNOSUPPORT;
1766 break;
1767 }
1768 break;
1769
1770 case SIOCAIFADDR:
1771 switch (ifa->ifa_addr->sa_family) {
1772 #ifdef INET
1773 case AF_INET:
1774 SC2IFP(sc)->if_flags |= IFF_UP;
1775 bcopy(ifa->ifa_addr, ifa->ifa_dstaddr,
1776 sizeof(struct sockaddr));
1777 error = carp_set_addr(sc, satosin(&ifra->ifra_addr));
1778 break;
1779 #endif /* INET */
1780 #ifdef INET6
1781 case AF_INET6:
1782 SC2IFP(sc)->if_flags |= IFF_UP;
1783 error = carp_set_addr6(sc, satosin6(&ifra->ifra_addr));
1784 break;
1785 #endif /* INET6 */
1786 default:
1787 error = EAFNOSUPPORT;
1788 break;
1789 }
1790 break;
1791
1792 case SIOCDIFADDR:
1793 switch (ifa->ifa_addr->sa_family) {
1794 #ifdef INET
1795 case AF_INET:
1796 error = carp_del_addr(sc, satosin(&ifra->ifra_addr));
1797 break;
1798 #endif /* INET */
1799 #ifdef INET6
1800 case AF_INET6:
1801 error = carp_del_addr6(sc, satosin6(&ifra->ifra_addr));
1802 break;
1803 #endif /* INET6 */
1804 default:
1805 error = EAFNOSUPPORT;
1806 break;
1807 }
1808 break;
1809
1810 case SIOCSIFFLAGS:
1811 if (sc->sc_carpdev) {
1812 locked = 1;
1813 CARP_SCLOCK(sc);
1814 }
1815 if (sc->sc_state != INIT && !(ifr->ifr_flags & IFF_UP)) {
1816 callout_stop(&sc->sc_ad_tmo);
1817 callout_stop(&sc->sc_md_tmo);
1818 callout_stop(&sc->sc_md6_tmo);
1819 if (sc->sc_state == MASTER)
1820 carp_send_ad_locked(sc);
1821 carp_set_state(sc, INIT);
1822 carp_setrun(sc, 0);
1823 } else if (sc->sc_state == INIT && (ifr->ifr_flags & IFF_UP)) {
1824 SC2IFP(sc)->if_flags |= IFF_UP;
1825 carp_setrun(sc, 0);
1826 }
1827 break;
1828
1829 case SIOCSVH:
1830 if ((error = suser(curthread)) != 0)
1831 break;
1832 if ((error = copyin(ifr->ifr_data, &carpr, sizeof carpr)))
1833 break;
1834 error = 1;
1835 if (sc->sc_carpdev) {
1836 locked = 1;
1837 CARP_SCLOCK(sc);
1838 }
1839 if (sc->sc_state != INIT && carpr.carpr_state != sc->sc_state) {
1840 switch (carpr.carpr_state) {
1841 case BACKUP:
1842 callout_stop(&sc->sc_ad_tmo);
1843 carp_set_state(sc, BACKUP);
1844 carp_setrun(sc, 0);
1845 carp_setroute(sc, RTM_DELETE);
1846 break;
1847 case MASTER:
1848 carp_master_down_locked(sc);
1849 break;
1850 default:
1851 break;
1852 }
1853 }
1854 if (carpr.carpr_vhid > 0) {
1855 if (carpr.carpr_vhid > 255) {
1856 error = EINVAL;
1857 break;
1858 }
1859 if (sc->sc_carpdev) {
1860 struct carp_if *cif;
1861 cif = (struct carp_if *)sc->sc_carpdev->if_carp;
1862 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list)
1863 if (vr != sc &&
1864 vr->sc_vhid == carpr.carpr_vhid)
1865 return EEXIST;
1866 }
1867 sc->sc_vhid = carpr.carpr_vhid;
1868 IFP2ENADDR(sc->sc_ifp)[0] = 0;
1869 IFP2ENADDR(sc->sc_ifp)[1] = 0;
1870 IFP2ENADDR(sc->sc_ifp)[2] = 0x5e;
1871 IFP2ENADDR(sc->sc_ifp)[3] = 0;
1872 IFP2ENADDR(sc->sc_ifp)[4] = 1;
1873 IFP2ENADDR(sc->sc_ifp)[5] = sc->sc_vhid;
1874 error--;
1875 }
1876 if (carpr.carpr_advbase > 0 || carpr.carpr_advskew > 0) {
1877 if (carpr.carpr_advskew >= 255) {
1878 error = EINVAL;
1879 break;
1880 }
1881 if (carpr.carpr_advbase > 255) {
1882 error = EINVAL;
1883 break;
1884 }
1885 sc->sc_advbase = carpr.carpr_advbase;
1886 sc->sc_advskew = carpr.carpr_advskew;
1887 error--;
1888 }
1889 bcopy(carpr.carpr_key, sc->sc_key, sizeof(sc->sc_key));
1890 if (error > 0)
1891 error = EINVAL;
1892 else {
1893 error = 0;
1894 carp_setrun(sc, 0);
1895 }
1896 break;
1897
1898 case SIOCGVH:
1899 /* XXX: lockless read */
1900 bzero(&carpr, sizeof(carpr));
1901 carpr.carpr_state = sc->sc_state;
1902 carpr.carpr_vhid = sc->sc_vhid;
1903 carpr.carpr_advbase = sc->sc_advbase;
1904 carpr.carpr_advskew = sc->sc_advskew;
1905 if (suser(curthread) == 0)
1906 bcopy(sc->sc_key, carpr.carpr_key,
1907 sizeof(carpr.carpr_key));
1908 error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
1909 break;
1910
1911 default:
1912 error = EINVAL;
1913 }
1914
1915 if (locked)
1916 CARP_SCUNLOCK(sc);
1917
1918 carp_hmac_prepare(sc);
1919
1920 return (error);
1921 }
1922
1923 /*
1924 * XXX: this is looutput. We should eventually use it from there.
1925 */
1926 static int
1927 carp_looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
1928 struct rtentry *rt)
1929 {
1930 u_int32_t af;
1931
1932 M_ASSERTPKTHDR(m); /* check if we have the packet header */
1933
1934 if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
1935 m_freem(m);
1936 return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
1937 rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1938 }
1939
1940 ifp->if_opackets++;
1941 ifp->if_obytes += m->m_pkthdr.len;
1942
1943 /* BPF writes need to be handled specially. */
1944 if (dst->sa_family == AF_UNSPEC) {
1945 bcopy(dst->sa_data, &af, sizeof(af));
1946 dst->sa_family = af;
1947 }
1948
1949 #if 1 /* XXX */
1950 switch (dst->sa_family) {
1951 case AF_INET:
1952 case AF_INET6:
1953 case AF_IPX:
1954 case AF_APPLETALK:
1955 break;
1956 default:
1957 printf("carp_looutput: af=%d unexpected\n", dst->sa_family);
1958 m_freem(m);
1959 return (EAFNOSUPPORT);
1960 }
1961 #endif
1962 return(if_simloop(ifp, m, dst->sa_family, 0));
1963 }
1964
1965 /*
1966 * Start output on carp interface. This function should never be called.
1967 */
1968 static void
1969 carp_start(struct ifnet *ifp)
1970 {
1971 #ifdef DEBUG
1972 printf("%s: start called\n", ifp->if_xname);
1973 #endif
1974 }
1975
1976 int
1977 carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
1978 struct rtentry *rt)
1979 {
1980 struct m_tag *mtag;
1981 struct carp_softc *sc;
1982 struct ifnet *carp_ifp;
1983
1984 if (!sa)
1985 return (0);
1986
1987 switch (sa->sa_family) {
1988 #ifdef INET
1989 case AF_INET:
1990 break;
1991 #endif /* INET */
1992 #ifdef INET6
1993 case AF_INET6:
1994 break;
1995 #endif /* INET6 */
1996 default:
1997 return (0);
1998 }
1999
2000 mtag = m_tag_find(m, PACKET_TAG_CARP, NULL);
2001 if (mtag == NULL)
2002 return (0);
2003
2004 bcopy(mtag + 1, &carp_ifp, sizeof(struct ifnet *));
2005 sc = carp_ifp->if_softc;
2006
2007 /* Set the source MAC address to Virtual Router MAC Address */
2008 switch (ifp->if_type) {
2009 case IFT_ETHER:
2010 case IFT_L2VLAN: {
2011 struct ether_header *eh;
2012
2013 eh = mtod(m, struct ether_header *);
2014 eh->ether_shost[0] = 0;
2015 eh->ether_shost[1] = 0;
2016 eh->ether_shost[2] = 0x5e;
2017 eh->ether_shost[3] = 0;
2018 eh->ether_shost[4] = 1;
2019 eh->ether_shost[5] = sc->sc_vhid;
2020 }
2021 break;
2022 case IFT_FDDI: {
2023 struct fddi_header *fh;
2024
2025 fh = mtod(m, struct fddi_header *);
2026 fh->fddi_shost[0] = 0;
2027 fh->fddi_shost[1] = 0;
2028 fh->fddi_shost[2] = 0x5e;
2029 fh->fddi_shost[3] = 0;
2030 fh->fddi_shost[4] = 1;
2031 fh->fddi_shost[5] = sc->sc_vhid;
2032 }
2033 break;
2034 case IFT_ISO88025: {
2035 struct iso88025_header *th;
2036 th = mtod(m, struct iso88025_header *);
2037 th->iso88025_shost[0] = 3;
2038 th->iso88025_shost[1] = 0;
2039 th->iso88025_shost[2] = 0x40 >> (sc->sc_vhid - 1);
2040 th->iso88025_shost[3] = 0x40000 >> (sc->sc_vhid - 1);
2041 th->iso88025_shost[4] = 0;
2042 th->iso88025_shost[5] = 0;
2043 }
2044 break;
2045 default:
2046 printf("%s: carp is not supported for this interface type\n",
2047 ifp->if_xname);
2048 return (EOPNOTSUPP);
2049 }
2050
2051 return (0);
2052 }
2053
2054 static void
2055 carp_set_state(struct carp_softc *sc, int state)
2056 {
2057
2058 if (sc->sc_carpdev)
2059 CARP_SCLOCK_ASSERT(sc);
2060
2061 if (sc->sc_state == state)
2062 return;
2063
2064 sc->sc_state = state;
2065 switch (state) {
2066 case BACKUP:
2067 SC2IFP(sc)->if_link_state = LINK_STATE_DOWN;
2068 break;
2069 case MASTER:
2070 SC2IFP(sc)->if_link_state = LINK_STATE_UP;
2071 break;
2072 default:
2073 SC2IFP(sc)->if_link_state = LINK_STATE_UNKNOWN;
2074 break;
2075 }
2076 rt_ifmsg(SC2IFP(sc));
2077 }
2078
2079 void
2080 carp_carpdev_state(void *v)
2081 {
2082 struct carp_if *cif = v;
2083
2084 CARP_LOCK(cif);
2085 carp_carpdev_state_locked(cif);
2086 CARP_UNLOCK(cif);
2087 }
2088
2089 static void
2090 carp_carpdev_state_locked(struct carp_if *cif)
2091 {
2092 struct carp_softc *sc;
2093
2094 TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list)
2095 carp_sc_state_locked(sc);
2096 }
2097
2098 static void
2099 carp_sc_state_locked(struct carp_softc *sc)
2100 {
2101 CARP_SCLOCK_ASSERT(sc);
2102
2103 if (sc->sc_carpdev->if_link_state != LINK_STATE_UP ||
2104 !(sc->sc_carpdev->if_flags & IFF_UP)) {
2105 sc->sc_flags_backup = SC2IFP(sc)->if_flags;
2106 SC2IFP(sc)->if_flags &= ~IFF_UP;
2107 SC2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
2108 callout_stop(&sc->sc_ad_tmo);
2109 callout_stop(&sc->sc_md_tmo);
2110 callout_stop(&sc->sc_md6_tmo);
2111 carp_set_state(sc, INIT);
2112 carp_setrun(sc, 0);
2113 if (!sc->sc_suppress) {
2114 carp_suppress_preempt++;
2115 if (carp_suppress_preempt == 1) {
2116 CARP_SCUNLOCK(sc);
2117 carp_send_ad_all();
2118 CARP_SCLOCK(sc);
2119 }
2120 }
2121 sc->sc_suppress = 1;
2122 } else {
2123 SC2IFP(sc)->if_flags |= sc->sc_flags_backup;
2124 carp_set_state(sc, INIT);
2125 carp_setrun(sc, 0);
2126 if (sc->sc_suppress)
2127 carp_suppress_preempt--;
2128 sc->sc_suppress = 0;
2129 }
2130
2131 return;
2132 }
2133
2134 static int
2135 carp_modevent(module_t mod, int type, void *data)
2136 {
2137 int error = 0;
2138
2139 switch (type) {
2140 case MOD_LOAD:
2141 mtx_init(&carp_mtx, "carp_mtx", NULL, MTX_DEF);
2142 LIST_INIT(&carpif_list);
2143 if_clone_attach(&carp_cloner);
2144 break;
2145
2146 case MOD_UNLOAD:
2147 if_clone_detach(&carp_cloner);
2148 while (!LIST_EMPTY(&carpif_list))
2149 carp_clone_destroy(SC2IFP(LIST_FIRST(&carpif_list)));
2150 mtx_destroy(&carp_mtx);
2151 break;
2152
2153 default:
2154 error = EINVAL;
2155 break;
2156 }
2157
2158 return error;
2159 }
2160
2161 static moduledata_t carp_mod = {
2162 "carp",
2163 carp_modevent,
2164 0
2165 };
2166
2167 DECLARE_MODULE(carp, carp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
Cache object: e7fb4965aea4e849e860005badf27fef
|