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