carp: In ioctl, we have to recompute HMAC only if any configures are changed
[dragonfly.git] / sys / netinet / ip_carp.c
1 /*
2  * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
3  * Copyright (c) 2003 Ryan McBride. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
18  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24  * THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 /*
27  * $FreeBSD: src/sys/netinet/ip_carp.c,v 1.48 2007/02/02 09:39:09 glebius Exp $
28  */
29
30 #include "opt_carp.h"
31 #include "opt_inet.h"
32 #include "opt_inet6.h"
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/in_cksum.h>
38 #include <sys/limits.h>
39 #include <sys/malloc.h>
40 #include <sys/mbuf.h>
41 #include <sys/msgport2.h>
42 #include <sys/time.h>
43 #include <sys/proc.h>
44 #include <sys/priv.h>
45 #include <sys/sockio.h>
46 #include <sys/socket.h>
47 #include <sys/sysctl.h>
48 #include <sys/syslog.h>
49 #include <sys/thread.h>
50
51 #include <machine/stdarg.h>
52 #include <crypto/sha1.h>
53
54 #include <net/bpf.h>
55 #include <net/ethernet.h>
56 #include <net/if.h>
57 #include <net/if_dl.h>
58 #include <net/if_types.h>
59 #include <net/route.h>
60 #include <net/if_clone.h>
61 #include <net/if_var.h>
62 #include <net/ifq_var.h>
63 #include <net/netmsg2.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 #endif
73
74 #ifdef INET6
75 #include <netinet/icmp6.h>
76 #include <netinet/ip6.h>
77 #include <netinet6/ip6_var.h>
78 #include <netinet6/scope6_var.h>
79 #include <netinet6/nd6.h>
80 #endif
81
82 #include <netinet/ip_carp.h>
83
84 #define CARP_IFNAME             "carp"
85 #define CARP_IS_RUNNING(ifp)    \
86         (((ifp)->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
87
88 struct carp_vhaddr {
89         uint32_t                vha_flags;      /* CARP_VHAF_ */
90         struct in_ifaddr        *vha_ia;        /* carp address */
91         struct in_ifaddr        *vha_iaback;    /* backing address */
92         TAILQ_ENTRY(carp_vhaddr) vha_link;
93 };
94 TAILQ_HEAD(carp_vhaddr_list, carp_vhaddr);
95
96 struct carp_softc {
97         struct arpcom            arpcom;
98         struct ifnet            *sc_carpdev;    /* parent interface */
99         struct carp_vhaddr_list  sc_vha_list;   /* virtual addr list */
100
101         const struct in_ifaddr  *sc_ia;         /* primary iface address v4 */
102         struct ip_moptions       sc_imo;
103
104 #ifdef INET6
105         struct in6_ifaddr       *sc_ia6;        /* primary iface address v6 */
106         struct ip6_moptions      sc_im6o;
107 #endif /* INET6 */
108         TAILQ_ENTRY(carp_softc)  sc_list;
109
110         enum { INIT = 0, BACKUP, MASTER }
111                                  sc_state;
112         int                      sc_dead;
113
114         int                      sc_suppress;
115
116         int                      sc_sendad_errors;
117 #define CARP_SENDAD_MAX_ERRORS  3
118         int                      sc_sendad_success;
119 #define CARP_SENDAD_MIN_SUCCESS 3
120
121         int                      sc_vhid;
122         int                      sc_advskew;
123         int                      sc_naddrs;     /* actually used IPv4 vha */
124         int                      sc_naddrs6;
125         int                      sc_advbase;    /* seconds */
126         int                      sc_init_counter;
127         uint64_t                 sc_counter;
128
129         /* authentication */
130 #define CARP_HMAC_PAD   64
131         unsigned char            sc_key[CARP_KEY_LEN];
132         unsigned char            sc_pad[CARP_HMAC_PAD];
133         SHA1_CTX                 sc_sha1;
134
135         struct callout           sc_ad_tmo;     /* advertisement timeout */
136         struct callout           sc_md_tmo;     /* master down timeout */
137         struct callout           sc_md6_tmo;    /* master down timeout */
138
139         LIST_ENTRY(carp_softc)   sc_next;       /* Interface clue */
140 };
141
142 #define sc_if   arpcom.ac_if
143
144 struct carp_if {
145         TAILQ_HEAD(, carp_softc) vhif_vrs;
146 };
147
148 struct netmsg_carp {
149         struct netmsg_base      base;
150         struct ifnet            *nc_carpdev;
151         struct carp_softc       *nc_softc;
152         void                    *nc_data;
153         size_t                  nc_datalen;
154 };
155
156 SYSCTL_DECL(_net_inet_carp);
157
158 static int carp_opts[CARPCTL_MAXID] = { 0, 1, 0, 1, 0, 0 }; /* XXX for now */
159 SYSCTL_INT(_net_inet_carp, CARPCTL_ALLOW, allow, CTLFLAG_RW,
160     &carp_opts[CARPCTL_ALLOW], 0, "Accept incoming CARP packets");
161 SYSCTL_INT(_net_inet_carp, CARPCTL_PREEMPT, preempt, CTLFLAG_RW,
162     &carp_opts[CARPCTL_PREEMPT], 0, "high-priority backup preemption mode");
163 SYSCTL_INT(_net_inet_carp, CARPCTL_LOG, log, CTLFLAG_RW,
164     &carp_opts[CARPCTL_LOG], 0, "log bad carp packets");
165 SYSCTL_INT(_net_inet_carp, CARPCTL_ARPBALANCE, arpbalance, CTLFLAG_RW,
166     &carp_opts[CARPCTL_ARPBALANCE], 0, "balance arp responses");
167
168 static int carp_suppress_preempt = 0;
169 SYSCTL_INT(_net_inet_carp, OID_AUTO, suppress_preempt, CTLFLAG_RD,
170     &carp_suppress_preempt, 0, "Preemption is suppressed");
171
172 static struct carpstats carpstats;
173 SYSCTL_STRUCT(_net_inet_carp, CARPCTL_STATS, stats, CTLFLAG_RW,
174     &carpstats, carpstats,
175     "CARP statistics (struct carpstats, netinet/ip_carp.h)");
176
177 #define CARP_LOG(...)   do {                            \
178         if (carp_opts[CARPCTL_LOG] > 0)                 \
179                 log(LOG_INFO, __VA_ARGS__);             \
180 } while (0)
181
182 #define CARP_DEBUG(...) do {                            \
183         if (carp_opts[CARPCTL_LOG] > 1)                 \
184                 log(LOG_DEBUG, __VA_ARGS__);            \
185 } while (0)
186
187 static struct lwkt_token carp_tok = LWKT_TOKEN_INITIALIZER(carp_token);
188
189 static void     carp_hmac_prepare(struct carp_softc *);
190 static void     carp_hmac_generate(struct carp_softc *, uint32_t *,
191                     unsigned char *);
192 static int      carp_hmac_verify(struct carp_softc *, uint32_t *,
193                     unsigned char *);
194 static void     carp_setroute(struct carp_softc *, int);
195 static void     carp_proto_input_c(struct carp_softc *, struct mbuf *,
196                     struct carp_header *, sa_family_t);
197 static int      carp_clone_create(struct if_clone *, int, caddr_t);
198 static int      carp_clone_destroy(struct ifnet *);
199 static void     carp_detach(struct carp_softc *, int, boolean_t);
200 static void     carp_prepare_ad(struct carp_softc *, struct carp_header *);
201 static void     carp_send_ad_all(void);
202 static void     carp_send_ad_timeout(void *);
203 static void     carp_send_ad(struct carp_softc *);
204 static void     carp_send_arp(struct carp_softc *);
205 static void     carp_master_down_timeout(void *);
206 static void     carp_master_down(struct carp_softc *);
207 static void     carp_setrun(struct carp_softc *, sa_family_t);
208 static void     carp_set_state(struct carp_softc *, int);
209 static struct ifnet *carp_forus(struct carp_if *, const uint8_t *);
210
211 static void     carp_init(void *);
212 static int      carp_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
213 static int      carp_output(struct ifnet *, struct mbuf *, struct sockaddr *,
214                     struct rtentry *);
215 static void     carp_start(struct ifnet *);
216 static void     carp_serialize(struct ifnet *, enum ifnet_serialize);
217 static void     carp_deserialize(struct ifnet *, enum ifnet_serialize);
218 static int      carp_tryserialize(struct ifnet *, enum ifnet_serialize);
219 #ifdef INVARIANTS
220 static void     carp_serialize_assert(struct ifnet *, enum ifnet_serialize,
221                     boolean_t);
222 #endif
223
224 static void     carp_multicast_cleanup(struct carp_softc *);
225 static void     carp_add_addr(struct carp_softc *, struct ifaddr *);
226 static void     carp_del_addr(struct carp_softc *, struct ifaddr *);
227 static void     carp_config_addr(struct carp_softc *, struct ifaddr *);
228 static void     carp_link_addrs(struct carp_softc *, struct ifnet *,
229                     struct ifaddr *);
230 static void     carp_unlink_addrs(struct carp_softc *, struct ifnet *,
231                     struct ifaddr *);
232 static void     carp_update_addrs(struct carp_softc *, struct ifaddr *);
233
234 static int      carp_get_vhaddr(struct carp_softc *, struct ifdrv *);
235 static int      carp_config_vhaddr(struct carp_softc *, struct carp_vhaddr *,
236                     struct in_ifaddr *);
237 static int      carp_activate_vhaddr(struct carp_softc *, struct carp_vhaddr *,
238                     struct ifnet *, struct in_ifaddr *, int);
239 static void     carp_deactivate_vhaddr(struct carp_softc *,
240                     struct carp_vhaddr *, boolean_t);
241 static int      carp_addroute_vhaddr(struct carp_softc *, struct carp_vhaddr *);
242 static void     carp_delroute_vhaddr(struct carp_softc *, struct carp_vhaddr *,
243                     boolean_t);
244
245 static void     carp_sc_state(struct carp_softc *);
246 #ifdef INET6
247 static void     carp_send_na(struct carp_softc *);
248 #ifdef notyet
249 static int      carp_set_addr6(struct carp_softc *, struct sockaddr_in6 *);
250 static int      carp_del_addr6(struct carp_softc *, struct sockaddr_in6 *);
251 #endif
252 static void     carp_multicast6_cleanup(struct carp_softc *);
253 #endif
254 static void     carp_stop(struct carp_softc *, int);
255 static void     carp_suspend(struct carp_softc *, int);
256 static void     carp_ioctl_stop(struct carp_softc *);
257 static int      carp_ioctl_setvh(struct carp_softc *, struct carpreq *);
258 static void     carp_ioctl_getvh(struct carp_softc *, struct carpreq *);
259
260 static void     carp_ifaddr(void *, struct ifnet *, enum ifaddr_event,
261                             struct ifaddr *);
262 static void     carp_ifdetach(void *, struct ifnet *);
263
264 static void     carp_ifdetach_dispatch(netmsg_t);
265 static void     carp_clone_destroy_dispatch(netmsg_t);
266 static void     carp_init_dispatch(netmsg_t);
267 static void     carp_ioctl_stop_dispatch(netmsg_t);
268 static void     carp_ioctl_setvh_dispatch(netmsg_t);
269 static void     carp_ioctl_getvh_dispatch(netmsg_t);
270
271 static MALLOC_DEFINE(M_CARP, "CARP", "CARP interfaces");
272
273 static LIST_HEAD(, carp_softc) carpif_list;
274
275 static struct if_clone carp_cloner =
276 IF_CLONE_INITIALIZER(CARP_IFNAME, carp_clone_create, carp_clone_destroy,
277                      0, IF_MAXUNIT);
278
279 static uint8_t  carp_etheraddr[ETHER_ADDR_LEN] = { 0, 0, 0x5e, 0, 1, 0 };
280
281 static eventhandler_tag carp_ifdetach_event;
282 static eventhandler_tag carp_ifaddr_event;
283
284 static __inline void
285 carp_insert_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha_new)
286 {
287         struct carp_vhaddr *vha;
288         u_long new_addr, addr;
289
290         KKASSERT((vha_new->vha_flags & CARP_VHAF_ONLIST) == 0);
291
292         /*
293          * Virtual address list is sorted; smaller one first
294          */
295         new_addr = ntohl(vha_new->vha_ia->ia_addr.sin_addr.s_addr);
296
297         TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
298                 addr = ntohl(vha->vha_ia->ia_addr.sin_addr.s_addr);
299
300                 if (addr > new_addr)
301                         break;
302         }
303         if (vha == NULL)
304                 TAILQ_INSERT_TAIL(&sc->sc_vha_list, vha_new, vha_link);
305         else
306                 TAILQ_INSERT_BEFORE(vha, vha_new, vha_link);
307         vha_new->vha_flags |= CARP_VHAF_ONLIST;
308 }
309
310 static __inline void
311 carp_remove_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha)
312 {
313         KKASSERT(vha->vha_flags & CARP_VHAF_ONLIST);
314         vha->vha_flags &= ~CARP_VHAF_ONLIST;
315         TAILQ_REMOVE(&sc->sc_vha_list, vha, vha_link);
316 }
317
318 static void
319 carp_hmac_prepare(struct carp_softc *sc)
320 {
321         uint8_t version = CARP_VERSION, type = CARP_ADVERTISEMENT;
322         uint8_t vhid = sc->sc_vhid & 0xff;
323         int i;
324 #ifdef INET6
325         struct ifaddr_container *ifac;
326         struct in6_addr in6;
327 #endif
328 #ifdef INET
329         struct carp_vhaddr *vha;
330 #endif
331
332         /* XXX: possible race here */
333
334         /* compute ipad from key */
335         bzero(sc->sc_pad, sizeof(sc->sc_pad));
336         bcopy(sc->sc_key, sc->sc_pad, sizeof(sc->sc_key));
337         for (i = 0; i < sizeof(sc->sc_pad); i++)
338                 sc->sc_pad[i] ^= 0x36;
339
340         /* precompute first part of inner hash */
341         SHA1Init(&sc->sc_sha1);
342         SHA1Update(&sc->sc_sha1, sc->sc_pad, sizeof(sc->sc_pad));
343         SHA1Update(&sc->sc_sha1, (void *)&version, sizeof(version));
344         SHA1Update(&sc->sc_sha1, (void *)&type, sizeof(type));
345         SHA1Update(&sc->sc_sha1, (void *)&vhid, sizeof(vhid));
346 #ifdef INET
347         TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
348                 SHA1Update(&sc->sc_sha1,
349                     (const uint8_t *)&vha->vha_ia->ia_addr.sin_addr,
350                     sizeof(struct in_addr));
351         }
352 #endif /* INET */
353 #ifdef INET6
354         TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
355                 struct ifaddr *ifa = ifac->ifa;
356
357                 if (ifa->ifa_addr->sa_family == AF_INET6) {
358                         in6 = ifatoia6(ifa)->ia_addr.sin6_addr;
359                         in6_clearscope(&in6);
360                         SHA1Update(&sc->sc_sha1, (void *)&in6, sizeof(in6));
361                 }
362         }
363 #endif /* INET6 */
364
365         /* convert ipad to opad */
366         for (i = 0; i < sizeof(sc->sc_pad); i++)
367                 sc->sc_pad[i] ^= 0x36 ^ 0x5c;
368 }
369
370 static void
371 carp_hmac_generate(struct carp_softc *sc, uint32_t counter[2],
372     unsigned char md[20])
373 {
374         SHA1_CTX sha1ctx;
375
376         /* fetch first half of inner hash */
377         bcopy(&sc->sc_sha1, &sha1ctx, sizeof(sha1ctx));
378
379         SHA1Update(&sha1ctx, (void *)counter, sizeof(sc->sc_counter));
380         SHA1Final(md, &sha1ctx);
381
382         /* outer hash */
383         SHA1Init(&sha1ctx);
384         SHA1Update(&sha1ctx, sc->sc_pad, sizeof(sc->sc_pad));
385         SHA1Update(&sha1ctx, md, 20);
386         SHA1Final(md, &sha1ctx);
387 }
388
389 static int
390 carp_hmac_verify(struct carp_softc *sc, uint32_t counter[2],
391     unsigned char md[20])
392 {
393         unsigned char md2[20];
394
395         carp_hmac_generate(sc, counter, md2);
396         return (bcmp(md, md2, sizeof(md2)));
397 }
398
399 static void
400 carp_setroute(struct carp_softc *sc, int cmd)
401 {
402 #ifdef INET6
403         struct ifaddr_container *ifac;
404 #endif
405         struct carp_vhaddr *vha;
406
407         KKASSERT(cmd == RTM_DELETE || cmd == RTM_ADD);
408
409         TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
410                 if (vha->vha_iaback == NULL)
411                         continue;
412                 if (cmd == RTM_DELETE)
413                         carp_delroute_vhaddr(sc, vha, FALSE);
414                 else
415                         carp_addroute_vhaddr(sc, vha);
416         }
417
418 #ifdef INET6
419         TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
420                 struct ifaddr *ifa = ifac->ifa;
421
422                 if (ifa->ifa_addr->sa_family == AF_INET6) {
423                         if (cmd == RTM_ADD)
424                                 in6_ifaddloop(ifa);
425                         else
426                                 in6_ifremloop(ifa);
427                 }
428         }
429 #endif /* INET6 */
430 }
431
432 static int
433 carp_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused)
434 {
435         struct carp_softc *sc;
436         struct ifnet *ifp;
437
438         sc = kmalloc(sizeof(*sc), M_CARP, M_WAITOK | M_ZERO);
439         ifp = &sc->sc_if;
440
441         sc->sc_suppress = 0;
442         sc->sc_advbase = CARP_DFLTINTV;
443         sc->sc_vhid = -1;       /* required setting */
444         sc->sc_advskew = 0;
445         sc->sc_init_counter = 1;
446         sc->sc_naddrs = 0;
447         sc->sc_naddrs6 = 0;
448
449         TAILQ_INIT(&sc->sc_vha_list);
450
451 #ifdef INET6
452         sc->sc_im6o.im6o_multicast_hlim = CARP_DFLTTL;
453 #endif
454
455         callout_init_mp(&sc->sc_ad_tmo);
456         callout_init_mp(&sc->sc_md_tmo);
457         callout_init_mp(&sc->sc_md6_tmo);
458
459         if_initname(ifp, CARP_IFNAME, unit);
460         ifp->if_softc = sc;
461         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
462         ifp->if_init = carp_init;
463         ifp->if_ioctl = carp_ioctl;
464         ifp->if_start = carp_start;
465         ifp->if_serialize = carp_serialize;
466         ifp->if_deserialize = carp_deserialize;
467         ifp->if_tryserialize = carp_tryserialize;
468 #ifdef INVARIANTS
469         ifp->if_serialize_assert = carp_serialize_assert;
470 #endif
471         ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
472         ifq_set_ready(&ifp->if_snd);
473
474         ether_ifattach(ifp, carp_etheraddr, NULL);
475
476         ifp->if_type = IFT_CARP;
477         ifp->if_output = carp_output;
478
479         carp_gettok();
480         LIST_INSERT_HEAD(&carpif_list, sc, sc_next);
481         carp_reltok();
482
483         return (0);
484 }
485
486 static void
487 carp_clone_destroy_dispatch(netmsg_t msg)
488 {
489         struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
490         struct carp_softc *sc = cmsg->nc_softc;
491
492         carp_gettok();
493
494         sc->sc_dead = 1;
495         carp_detach(sc, 1, FALSE);
496
497         carp_reltok();
498
499         lwkt_replymsg(&cmsg->base.lmsg, 0);
500 }
501
502 static int
503 carp_clone_destroy(struct ifnet *ifp)
504 {
505         struct carp_softc *sc = ifp->if_softc;
506         struct netmsg_carp cmsg;
507
508         bzero(&cmsg, sizeof(cmsg));
509         netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
510             carp_clone_destroy_dispatch);
511         cmsg.nc_softc = sc;
512
513         lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0);
514
515         carp_gettok();
516         LIST_REMOVE(sc, sc_next);
517         carp_reltok();
518
519         bpfdetach(ifp);
520         if_detach(ifp);
521
522         KASSERT(sc->sc_naddrs == 0, ("certain inet address is still active\n"));
523         kfree(sc, M_CARP);
524
525         return 0;
526 }
527
528 static void
529 carp_detach(struct carp_softc *sc, int detach, boolean_t del_iaback)
530 {
531         struct carp_if *cif;
532
533         carp_suspend(sc, detach);
534
535         carp_multicast_cleanup(sc);
536 #ifdef INET6
537         carp_multicast6_cleanup(sc);
538 #endif
539
540         if (!sc->sc_dead && detach) {
541                 struct carp_vhaddr *vha;
542
543                 TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
544                         carp_deactivate_vhaddr(sc, vha, del_iaback);
545                 KKASSERT(sc->sc_naddrs == 0);
546         }
547
548         if (sc->sc_carpdev != NULL) {
549                 cif = sc->sc_carpdev->if_carp;
550                 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
551                 if (TAILQ_EMPTY(&cif->vhif_vrs)) {
552                         ifpromisc(sc->sc_carpdev, 0);
553                         sc->sc_carpdev->if_carp = NULL;
554                         kfree(cif, M_CARP);
555                 }
556                 sc->sc_carpdev = NULL;
557                 sc->sc_ia = NULL;
558         }
559 }
560
561 static void
562 carp_ifdetach_dispatch(netmsg_t msg)
563 {
564         struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
565         struct ifnet *ifp = cmsg->nc_carpdev;
566         struct carp_if *cif = ifp->if_carp;
567         struct carp_softc *sc;
568
569         carp_gettok();
570
571         while (ifp->if_carp &&
572                (sc = TAILQ_FIRST(&cif->vhif_vrs)) != NULL)
573                 carp_detach(sc, 1, TRUE);
574
575         carp_reltok();
576
577         lwkt_replymsg(&cmsg->base.lmsg, 0);
578 }
579
580 /* Detach an interface from the carp. */
581 static void
582 carp_ifdetach(void *arg __unused, struct ifnet *ifp)
583 {
584         struct netmsg_carp cmsg;
585
586         ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
587
588         bzero(&cmsg, sizeof(cmsg));
589         netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
590             carp_ifdetach_dispatch);
591         cmsg.nc_carpdev = ifp;
592
593         lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0);
594 }
595
596 /*
597  * process input packet.
598  * we have rearranged checks order compared to the rfc,
599  * but it seems more efficient this way or not possible otherwise.
600  */
601 int
602 carp_proto_input(struct mbuf **mp, int *offp, int proto)
603 {
604         struct mbuf *m = *mp;
605         struct ip *ip = mtod(m, struct ip *);
606         struct ifnet *ifp = m->m_pkthdr.rcvif;
607         struct carp_header *ch;
608         struct carp_softc *sc;
609         int len, iphlen;
610
611         carp_gettok();
612
613         iphlen = *offp;
614         *mp = NULL;
615
616         carpstats.carps_ipackets++;
617
618         if (!carp_opts[CARPCTL_ALLOW]) {
619                 m_freem(m);
620                 goto back;
621         }
622
623         /* Check if received on a valid carp interface */
624         if (ifp->if_type != IFT_CARP) {
625                 carpstats.carps_badif++;
626                 CARP_LOG("carp_proto_input: packet received on non-carp "
627                     "interface: %s\n", ifp->if_xname);
628                 m_freem(m);
629                 goto back;
630         }
631
632         if (!CARP_IS_RUNNING(ifp)) {
633                 carpstats.carps_badif++;
634                 CARP_LOG("carp_proto_input: packet received on stopped carp "
635                     "interface: %s\n", ifp->if_xname);
636                 m_freem(m);
637                 goto back;
638         }
639
640         sc = ifp->if_softc;
641         if (sc->sc_carpdev == NULL) {
642                 carpstats.carps_badif++;
643                 CARP_LOG("carp_proto_input: packet received on defunc carp "
644                     "interface: %s\n", ifp->if_xname);
645                 m_freem(m);
646                 goto back;
647         }
648
649         if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
650                 carpstats.carps_badif++;
651                 CARP_LOG("carp_proto_input: non-mcast packet on "
652                     "interface: %s\n", ifp->if_xname);
653                 m_freem(m);
654                 goto back;
655         }
656
657         /* Verify that the IP TTL is CARP_DFLTTL. */
658         if (ip->ip_ttl != CARP_DFLTTL) {
659                 carpstats.carps_badttl++;
660                 CARP_LOG("carp_proto_input: received ttl %d != %d on %s\n",
661                     ip->ip_ttl, CARP_DFLTTL, ifp->if_xname);
662                 m_freem(m);
663                 goto back;
664         }
665
666         /* Minimal CARP packet size */
667         len = iphlen + sizeof(*ch);
668
669         /*
670          * Verify that the received packet length is
671          * not less than the CARP header
672          */
673         if (m->m_pkthdr.len < len) {
674                 carpstats.carps_badlen++;
675                 CARP_LOG("packet too short %d on %s\n", m->m_pkthdr.len,
676                     ifp->if_xname);
677                 m_freem(m);
678                 goto back;
679         }
680
681         /* Make sure that CARP header is contiguous */
682         if (len > m->m_len) {
683                 m = m_pullup(m, len);
684                 if (m == NULL) {
685                         carpstats.carps_hdrops++;
686                         CARP_LOG("carp_proto_input: m_pullup failed\n");
687                         goto back;
688                 }
689                 ip = mtod(m, struct ip *);
690         }
691         ch = (struct carp_header *)((uint8_t *)ip + iphlen);
692
693         /* Verify the CARP checksum */
694         if (in_cksum_skip(m, len, iphlen)) {
695                 carpstats.carps_badsum++;
696                 CARP_LOG("carp_proto_input: checksum failed on %s\n",
697                     ifp->if_xname);
698                 m_freem(m);
699                 goto back;
700         }
701         carp_proto_input_c(sc, m, ch, AF_INET);
702 back:
703         carp_reltok();
704         return(IPPROTO_DONE);
705 }
706
707 #ifdef INET6
708 int
709 carp6_proto_input(struct mbuf **mp, int *offp, int proto)
710 {
711         struct mbuf *m = *mp;
712         struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
713         struct ifnet *ifp = m->m_pkthdr.rcvif;
714         struct carp_header *ch;
715         struct carp_softc *sc;
716         u_int len;
717
718         carp_gettok();
719
720         carpstats.carps_ipackets6++;
721
722         if (!carp_opts[CARPCTL_ALLOW]) {
723                 m_freem(m);
724                 goto back;
725         }
726
727         /* check if received on a valid carp interface */
728         if (ifp->if_type != IFT_CARP) {
729                 carpstats.carps_badif++;
730                 CARP_LOG("carp6_proto_input: packet received on non-carp "
731                     "interface: %s\n", ifp->if_xname);
732                 m_freem(m);
733                 goto back;
734         }
735
736         if (!CARP_IS_RUNNING(ifp)) {
737                 carpstats.carps_badif++;
738                 CARP_LOG("carp_proto_input: packet received on stopped carp "
739                     "interface: %s\n", ifp->if_xname);
740                 m_freem(m);
741                 goto back;
742         }
743
744         sc = ifp->if_softc;
745         if (sc->sc_carpdev == NULL) {
746                 carpstats.carps_badif++;
747                 CARP_LOG("carp6_proto_input: packet received on defunc-carp "
748                     "interface: %s\n", ifp->if_xname);
749                 m_freem(m);
750                 goto back;
751         }
752
753         /* verify that the IP TTL is 255 */
754         if (ip6->ip6_hlim != CARP_DFLTTL) {
755                 carpstats.carps_badttl++;
756                 CARP_LOG("carp6_proto_input: received ttl %d != 255 on %s\n",
757                     ip6->ip6_hlim, ifp->if_xname);
758                 m_freem(m);
759                 goto back;
760         }
761
762         /* verify that we have a complete carp packet */
763         len = m->m_len;
764         IP6_EXTHDR_GET(ch, struct carp_header *, m, *offp, sizeof(*ch));
765         if (ch == NULL) {
766                 carpstats.carps_badlen++;
767                 CARP_LOG("carp6_proto_input: packet size %u too small\n", len);
768                 goto back;
769         }
770
771         /* verify the CARP checksum */
772         if (in_cksum_range(m, 0, *offp, sizeof(*ch))) {
773                 carpstats.carps_badsum++;
774                 CARP_LOG("carp6_proto_input: checksum failed, on %s\n",
775                     ifp->if_xname);
776                 m_freem(m);
777                 goto back;
778         }
779
780         carp_proto_input_c(sc, m, ch, AF_INET6);
781 back:
782         carp_reltok();
783         return (IPPROTO_DONE);
784 }
785 #endif /* INET6 */
786
787 static void
788 carp_proto_input_c(struct carp_softc *sc, struct mbuf *m,
789     struct carp_header *ch, sa_family_t af)
790 {
791         struct ifnet *cifp;
792         uint64_t tmp_counter;
793         struct timeval sc_tv, ch_tv;
794
795         if (sc->sc_vhid != ch->carp_vhid) {
796                 /*
797                  * CARP uses multicast, however, multicast packets
798                  * are tapped to all CARP interfaces on the physical
799                  * interface receiving the CARP packets, so we don't
800                  * update any stats here.
801                  */
802                 m_freem(m);
803                 return;
804         }
805         cifp = &sc->sc_if;
806
807         /* verify the CARP version. */
808         if (ch->carp_version != CARP_VERSION) {
809                 carpstats.carps_badver++;
810                 CARP_LOG("%s; invalid version %d\n", cifp->if_xname,
811                          ch->carp_version);
812                 m_freem(m);
813                 return;
814         }
815
816         /* verify the hash */
817         if (carp_hmac_verify(sc, ch->carp_counter, ch->carp_md)) {
818                 carpstats.carps_badauth++;
819                 CARP_LOG("%s: incorrect hash\n", cifp->if_xname);
820                 m_freem(m);
821                 return;
822         }
823
824         tmp_counter = ntohl(ch->carp_counter[0]);
825         tmp_counter = tmp_counter<<32;
826         tmp_counter += ntohl(ch->carp_counter[1]);
827
828         /* XXX Replay protection goes here */
829
830         sc->sc_init_counter = 0;
831         sc->sc_counter = tmp_counter;
832
833         sc_tv.tv_sec = sc->sc_advbase;
834         if (carp_suppress_preempt && sc->sc_advskew <  240)
835                 sc_tv.tv_usec = 240 * 1000000 / 256;
836         else
837                 sc_tv.tv_usec = sc->sc_advskew * 1000000 / 256;
838         ch_tv.tv_sec = ch->carp_advbase;
839         ch_tv.tv_usec = ch->carp_advskew * 1000000 / 256;
840
841         switch (sc->sc_state) {
842         case INIT:
843                 break;
844
845         case MASTER:
846                 /*
847                  * If we receive an advertisement from a master who's going to
848                  * be more frequent than us, go into BACKUP state.
849                  */
850                 if (timevalcmp(&sc_tv, &ch_tv, >) ||
851                     timevalcmp(&sc_tv, &ch_tv, ==)) {
852                         callout_stop(&sc->sc_ad_tmo);
853                         CARP_DEBUG("%s: MASTER -> BACKUP "
854                            "(more frequent advertisement received)\n",
855                            cifp->if_xname);
856                         carp_set_state(sc, BACKUP);
857                         carp_setrun(sc, 0);
858                         carp_setroute(sc, RTM_DELETE);
859                 }
860                 break;
861
862         case BACKUP:
863                 /*
864                  * If we're pre-empting masters who advertise slower than us,
865                  * and this one claims to be slower, treat him as down.
866                  */
867                 if (carp_opts[CARPCTL_PREEMPT] &&
868                     timevalcmp(&sc_tv, &ch_tv, <)) {
869                         CARP_DEBUG("%s: BACKUP -> MASTER "
870                             "(preempting a slower master)\n", cifp->if_xname);
871                         carp_master_down(sc);
872                         break;
873                 }
874
875                 /*
876                  *  If the master is going to advertise at such a low frequency
877                  *  that he's guaranteed to time out, we'd might as well just
878                  *  treat him as timed out now.
879                  */
880                 sc_tv.tv_sec = sc->sc_advbase * 3;
881                 if (timevalcmp(&sc_tv, &ch_tv, <)) {
882                         CARP_DEBUG("%s: BACKUP -> MASTER (master timed out)\n",
883                                    cifp->if_xname);
884                         carp_master_down(sc);
885                         break;
886                 }
887
888                 /*
889                  * Otherwise, we reset the counter and wait for the next
890                  * advertisement.
891                  */
892                 carp_setrun(sc, af);
893                 break;
894         }
895         m_freem(m);
896 }
897
898 struct mbuf *
899 carp_input(void *v, struct mbuf *m)
900 {
901         struct carp_if *cif = v;
902         struct ether_header *eh;
903         struct carp_softc *sc;
904         struct ifnet *ifp;
905
906         ASSERT_LWKT_TOKEN_HELD(&carp_tok);
907
908         eh = mtod(m, struct ether_header *);
909
910         ifp = carp_forus(cif, eh->ether_dhost);
911         if (ifp != NULL) {
912                 ether_reinput_oncpu(ifp, m, REINPUT_RUNBPF);
913                 return NULL;
914         }
915
916         if ((m->m_flags & (M_BCAST | M_MCAST)) == 0)
917                 return m;
918
919         /*
920          * XXX Should really check the list of multicast addresses
921          * for each CARP interface _before_ copying.
922          */
923         TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
924                 struct mbuf *m0;
925
926                 if ((sc->sc_if.if_flags & IFF_UP) == 0)
927                         continue;
928
929                 m0 = m_dup(m, MB_DONTWAIT);
930                 if (m0 == NULL)
931                         continue;
932
933                 ether_reinput_oncpu(&sc->sc_if, m0, REINPUT_RUNBPF);
934         }
935         return m;
936 }
937
938 static void
939 carp_prepare_ad(struct carp_softc *sc, struct carp_header *ch)
940 {
941         if (sc->sc_init_counter) {
942                 /* this could also be seconds since unix epoch */
943                 sc->sc_counter = karc4random();
944                 sc->sc_counter = sc->sc_counter << 32;
945                 sc->sc_counter += karc4random();
946         } else {
947                 sc->sc_counter++;
948         }
949
950         ch->carp_counter[0] = htonl((sc->sc_counter >> 32) & 0xffffffff);
951         ch->carp_counter[1] = htonl(sc->sc_counter & 0xffffffff);
952
953         carp_hmac_generate(sc, ch->carp_counter, ch->carp_md);
954 }
955
956 static void
957 carp_send_ad_all(void)
958 {
959         struct carp_softc *sc;
960
961         LIST_FOREACH(sc, &carpif_list, sc_next) {
962                 if (sc->sc_carpdev == NULL)
963                         continue;
964
965                 if (CARP_IS_RUNNING(&sc->sc_if) && sc->sc_state == MASTER)
966                         carp_send_ad(sc);
967         }
968 }
969
970 static void
971 carp_send_ad_timeout(void *xsc)
972 {
973         carp_gettok();
974         carp_send_ad(xsc);
975         carp_reltok();
976 }
977
978 static void
979 carp_send_ad(struct carp_softc *sc)
980 {
981         struct ifnet *cifp = &sc->sc_if;
982         struct carp_header ch;
983         struct timeval tv;
984         struct carp_header *ch_ptr;
985         struct mbuf *m;
986         int len, advbase, advskew;
987
988         if (!CARP_IS_RUNNING(cifp)) {
989                 /* Bow out */
990                 advbase = 255;
991                 advskew = 255;
992         } else {
993                 advbase = sc->sc_advbase;
994                 if (!carp_suppress_preempt || sc->sc_advskew > 240)
995                         advskew = sc->sc_advskew;
996                 else
997                         advskew = 240;
998                 tv.tv_sec = advbase;
999                 tv.tv_usec = advskew * 1000000 / 256;
1000         }
1001
1002         ch.carp_version = CARP_VERSION;
1003         ch.carp_type = CARP_ADVERTISEMENT;
1004         ch.carp_vhid = sc->sc_vhid;
1005         ch.carp_advbase = advbase;
1006         ch.carp_advskew = advskew;
1007         ch.carp_authlen = 7;    /* XXX DEFINE */
1008         ch.carp_pad1 = 0;       /* must be zero */
1009         ch.carp_cksum = 0;
1010
1011 #ifdef INET
1012         if (sc->sc_ia != NULL) {
1013                 struct ip *ip;
1014
1015                 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1016                 if (m == NULL) {
1017                         cifp->if_oerrors++;
1018                         carpstats.carps_onomem++;
1019                         /* XXX maybe less ? */
1020                         if (advbase != 255 || advskew != 255)
1021                                 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1022                                     carp_send_ad_timeout, sc);
1023                         return;
1024                 }
1025                 len = sizeof(*ip) + sizeof(ch);
1026                 m->m_pkthdr.len = len;
1027                 m->m_pkthdr.rcvif = NULL;
1028                 m->m_len = len;
1029                 MH_ALIGN(m, m->m_len);
1030                 m->m_flags |= M_MCAST;
1031                 ip = mtod(m, struct ip *);
1032                 ip->ip_v = IPVERSION;
1033                 ip->ip_hl = sizeof(*ip) >> 2;
1034                 ip->ip_tos = IPTOS_LOWDELAY;
1035                 ip->ip_len = len;
1036                 ip->ip_id = ip_newid();
1037                 ip->ip_off = IP_DF;
1038                 ip->ip_ttl = CARP_DFLTTL;
1039                 ip->ip_p = IPPROTO_CARP;
1040                 ip->ip_sum = 0;
1041                 ip->ip_src = sc->sc_ia->ia_addr.sin_addr;
1042                 ip->ip_dst.s_addr = htonl(INADDR_CARP_GROUP);
1043
1044                 ch_ptr = (struct carp_header *)(&ip[1]);
1045                 bcopy(&ch, ch_ptr, sizeof(ch));
1046                 carp_prepare_ad(sc, ch_ptr);
1047                 ch_ptr->carp_cksum = in_cksum_skip(m, len, sizeof(*ip));
1048
1049                 getmicrotime(&cifp->if_lastchange);
1050                 cifp->if_opackets++;
1051                 cifp->if_obytes += len;
1052                 carpstats.carps_opackets++;
1053
1054                 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)) {
1055                         cifp->if_oerrors++;
1056                         if (sc->sc_sendad_errors < INT_MAX)
1057                                 sc->sc_sendad_errors++;
1058                         if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
1059                                 carp_suppress_preempt++;
1060                                 if (carp_suppress_preempt == 1) {
1061                                         carp_send_ad_all();
1062                                 }
1063                         }
1064                         sc->sc_sendad_success = 0;
1065                 } else {
1066                         if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
1067                                 if (++sc->sc_sendad_success >=
1068                                     CARP_SENDAD_MIN_SUCCESS) {
1069                                         carp_suppress_preempt--;
1070                                         sc->sc_sendad_errors = 0;
1071                                 }
1072                         } else {
1073                                 sc->sc_sendad_errors = 0;
1074                         }
1075                 }
1076         }
1077 #endif /* INET */
1078 #ifdef INET6
1079         if (sc->sc_ia6) {
1080                 struct ip6_hdr *ip6;
1081
1082                 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1083                 if (m == NULL) {
1084                         cifp->if_oerrors++;
1085                         carpstats.carps_onomem++;
1086                         /* XXX maybe less ? */
1087                         if (advbase != 255 || advskew != 255)
1088                                 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1089                                     carp_send_ad_timeout, sc);
1090                         return;
1091                 }
1092                 len = sizeof(*ip6) + sizeof(ch);
1093                 m->m_pkthdr.len = len;
1094                 m->m_pkthdr.rcvif = NULL;
1095                 m->m_len = len;
1096                 MH_ALIGN(m, m->m_len);
1097                 m->m_flags |= M_MCAST;
1098                 ip6 = mtod(m, struct ip6_hdr *);
1099                 bzero(ip6, sizeof(*ip6));
1100                 ip6->ip6_vfc |= IPV6_VERSION;
1101                 ip6->ip6_hlim = CARP_DFLTTL;
1102                 ip6->ip6_nxt = IPPROTO_CARP;
1103                 bcopy(&sc->sc_ia6->ia_addr.sin6_addr, &ip6->ip6_src,
1104                     sizeof(struct in6_addr));
1105                 /* set the multicast destination */
1106
1107                 ip6->ip6_dst.s6_addr16[0] = htons(0xff02);
1108                 ip6->ip6_dst.s6_addr8[15] = 0x12;
1109                 if (in6_setscope(&ip6->ip6_dst, sc->sc_carpdev, NULL) != 0) {
1110                         cifp->if_oerrors++;
1111                         m_freem(m);
1112                         CARP_LOG("%s: in6_setscope failed\n", __func__);
1113                         return;
1114                 }
1115
1116                 ch_ptr = (struct carp_header *)(&ip6[1]);
1117                 bcopy(&ch, ch_ptr, sizeof(ch));
1118                 carp_prepare_ad(sc, ch_ptr);
1119                 ch_ptr->carp_cksum = in_cksum_skip(m, len, sizeof(*ip6));
1120
1121                 getmicrotime(&cifp->if_lastchange);
1122                 cifp->if_opackets++;
1123                 cifp->if_obytes += len;
1124                 carpstats.carps_opackets6++;
1125
1126                 if (ip6_output(m, NULL, NULL, 0, &sc->sc_im6o, NULL, NULL)) {
1127                         cifp->if_oerrors++;
1128                         if (sc->sc_sendad_errors < INT_MAX)
1129                                 sc->sc_sendad_errors++;
1130                         if (sc->sc_sendad_errors == CARP_SENDAD_MAX_ERRORS) {
1131                                 carp_suppress_preempt++;
1132                                 if (carp_suppress_preempt == 1) {
1133                                         carp_send_ad_all();
1134                                 }
1135                         }
1136                         sc->sc_sendad_success = 0;
1137                 } else {
1138                         if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS) {
1139                                 if (++sc->sc_sendad_success >=
1140                                     CARP_SENDAD_MIN_SUCCESS) {
1141                                         carp_suppress_preempt--;
1142                                         sc->sc_sendad_errors = 0;
1143                                 }
1144                         } else {
1145                                 sc->sc_sendad_errors = 0;
1146                         }
1147                 }
1148         }
1149 #endif /* INET6 */
1150
1151         if (advbase != 255 || advskew != 255)
1152                 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1153                     carp_send_ad_timeout, sc);
1154 }
1155
1156 /*
1157  * Broadcast a gratuitous ARP request containing
1158  * the virtual router MAC address for each IP address
1159  * associated with the virtual router.
1160  */
1161 static void
1162 carp_send_arp(struct carp_softc *sc)
1163 {
1164         const struct carp_vhaddr *vha;
1165
1166         TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1167                 if (vha->vha_iaback == NULL)
1168                         continue;
1169                 arp_gratuitous(&sc->sc_if, &vha->vha_ia->ia_ifa);
1170         }
1171 }
1172
1173 #ifdef INET6
1174 static void
1175 carp_send_na(struct carp_softc *sc)
1176 {
1177         struct ifaddr_container *ifac;
1178         struct in6_addr *in6;
1179         static struct in6_addr mcast = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
1180
1181         TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid], ifa_link) {
1182                 struct ifaddr *ifa = ifac->ifa;
1183
1184                 if (ifa->ifa_addr->sa_family != AF_INET6)
1185                         continue;
1186
1187                 in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
1188                 nd6_na_output(sc->sc_carpdev, &mcast, in6,
1189                     ND_NA_FLAG_OVERRIDE, 1, NULL);
1190                 DELAY(1000);    /* XXX */
1191         }
1192 }
1193 #endif /* INET6 */
1194
1195 static __inline const struct carp_vhaddr *
1196 carp_find_addr(const struct carp_softc *sc, const struct in_addr *addr)
1197 {
1198         struct carp_vhaddr *vha;
1199
1200         TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1201                 if (vha->vha_iaback == NULL)
1202                         continue;
1203
1204                 if (vha->vha_ia->ia_addr.sin_addr.s_addr == addr->s_addr)
1205                         return vha;
1206         }
1207         return NULL;
1208 }
1209
1210 #ifdef notyet
1211 static int
1212 carp_iamatch_balance(const struct carp_if *cif, const struct in_addr *itaddr,
1213                      const struct in_addr *isaddr, uint8_t **enaddr)
1214 {
1215         const struct carp_softc *vh;
1216         int index, count = 0;
1217
1218         /*
1219          * XXX proof of concept implementation.
1220          * We use the source ip to decide which virtual host should
1221          * handle the request. If we're master of that virtual host,
1222          * then we respond, otherwise, just drop the arp packet on
1223          * the floor.
1224          */
1225
1226         TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1227                 if (!CARP_IS_RUNNING(&vh->sc_if))
1228                         continue;
1229
1230                 if (carp_find_addr(vh, itaddr) != NULL)
1231                         count++;
1232         }
1233         if (count == 0)
1234                 return 0;
1235
1236         /* this should be a hash, like pf_hash() */
1237         index = ntohl(isaddr->s_addr) % count;
1238         count = 0;
1239
1240         TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1241                 if (!CARP_IS_RUNNING(&vh->sc_if))
1242                         continue;
1243
1244                 if (carp_find_addr(vh, itaddr) == NULL)
1245                         continue;
1246
1247                 if (count == index) {
1248                         if (vh->sc_state == MASTER) {
1249                                 *enaddr = IF_LLADDR(&vh->sc_if);
1250                                 return 1;
1251                         } else {
1252                                 return 0;
1253                         }
1254                 }
1255                 count++;
1256         }
1257         return 0;
1258 }
1259 #endif
1260
1261 int
1262 carp_iamatch(const struct in_ifaddr *ia)
1263 {
1264         const struct carp_softc *sc = ia->ia_ifp->if_softc;
1265
1266         ASSERT_LWKT_TOKEN_HELD(&carp_tok);
1267
1268 #ifdef notyet
1269         if (carp_opts[CARPCTL_ARPBALANCE])
1270                 return carp_iamatch_balance(cif, itaddr, isaddr, enaddr);
1271 #endif
1272
1273         if (!CARP_IS_RUNNING(&sc->sc_if) || sc->sc_state != MASTER)
1274                 return 0;
1275
1276         return 1;
1277 }
1278
1279 #ifdef INET6
1280 struct ifaddr *
1281 carp_iamatch6(void *v, struct in6_addr *taddr)
1282 {
1283         struct carp_if *cif = v;
1284         struct carp_softc *vh;
1285
1286         ASSERT_LWKT_TOKEN_HELD(&carp_tok);
1287
1288         TAILQ_FOREACH(vh, &cif->vhif_vrs, sc_list) {
1289                 struct ifaddr_container *ifac;
1290
1291                 TAILQ_FOREACH(ifac, &vh->sc_if.if_addrheads[mycpuid],
1292                               ifa_link) {
1293                         struct ifaddr *ifa = ifac->ifa;
1294
1295                         if (IN6_ARE_ADDR_EQUAL(taddr,
1296                             &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1297                             CARP_IS_RUNNING(&vh->sc_if) &&
1298                             vh->sc_state == MASTER) {
1299                                 return (ifa);
1300                         }
1301                 }
1302         }
1303         return (NULL);
1304 }
1305
1306 void *
1307 carp_macmatch6(void *v, struct mbuf *m, const struct in6_addr *taddr)
1308 {
1309         struct m_tag *mtag;
1310         struct carp_if *cif = v;
1311         struct carp_softc *sc;
1312
1313         ASSERT_LWKT_TOKEN_HELD(&carp_tok);
1314
1315         TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
1316                 struct ifaddr_container *ifac;
1317
1318                 TAILQ_FOREACH(ifac, &sc->sc_if.if_addrheads[mycpuid],
1319                               ifa_link) {
1320                         struct ifaddr *ifa = ifac->ifa;
1321
1322                         if (IN6_ARE_ADDR_EQUAL(taddr,
1323                             &ifatoia6(ifa)->ia_addr.sin6_addr) &&
1324                             CARP_IS_RUNNING(&sc->sc_if)) {
1325                                 struct ifnet *ifp = &sc->sc_if;
1326
1327                                 mtag = m_tag_get(PACKET_TAG_CARP,
1328                                     sizeof(struct ifnet *), MB_DONTWAIT);
1329                                 if (mtag == NULL) {
1330                                         /* better a bit than nothing */
1331                                         return (IF_LLADDR(ifp));
1332                                 }
1333                                 bcopy(&ifp, (caddr_t)(mtag + 1),
1334                                     sizeof(struct ifnet *));
1335                                 m_tag_prepend(m, mtag);
1336
1337                                 return (IF_LLADDR(ifp));
1338                         }
1339                 }
1340         }
1341         return (NULL);
1342 }
1343 #endif
1344
1345 static struct ifnet *
1346 carp_forus(struct carp_if *cif, const uint8_t *dhost)
1347 {
1348         struct carp_softc *sc;
1349
1350         ASSERT_LWKT_TOKEN_HELD(&carp_tok);
1351
1352         if (memcmp(dhost, carp_etheraddr, ETHER_ADDR_LEN - 1) != 0)
1353                 return NULL;
1354
1355         TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list) {
1356                 struct ifnet *ifp = &sc->sc_if;
1357
1358                 if (CARP_IS_RUNNING(ifp) && sc->sc_state == MASTER &&
1359                     !bcmp(dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN))
1360                         return ifp;
1361         }
1362         return NULL;
1363 }
1364
1365 static void
1366 carp_master_down_timeout(void *xsc)
1367 {
1368         struct carp_softc *sc = xsc;
1369
1370         CARP_DEBUG("%s: BACKUP -> MASTER (master timed out)\n",
1371                    sc->sc_if.if_xname);
1372         carp_gettok();
1373         carp_master_down(sc);
1374         carp_reltok();
1375 }
1376
1377 static void
1378 carp_master_down(struct carp_softc *sc)
1379 {
1380         switch (sc->sc_state) {
1381         case INIT:
1382                 kprintf("%s: master_down event in INIT state\n",
1383                         sc->sc_if.if_xname);
1384                 break;
1385
1386         case MASTER:
1387                 break;
1388
1389         case BACKUP:
1390                 carp_set_state(sc, MASTER);
1391                 carp_send_ad(sc);
1392                 carp_send_arp(sc);
1393 #ifdef INET6
1394                 carp_send_na(sc);
1395 #endif /* INET6 */
1396                 carp_setrun(sc, 0);
1397                 carp_setroute(sc, RTM_ADD);
1398                 break;
1399         }
1400 }
1401
1402 /*
1403  * When in backup state, af indicates whether to reset the master down timer
1404  * for v4 or v6. If it's set to zero, reset the ones which are already pending.
1405  */
1406 static void
1407 carp_setrun(struct carp_softc *sc, sa_family_t af)
1408 {
1409         struct ifnet *cifp = &sc->sc_if;
1410         struct timeval tv;
1411
1412         if (sc->sc_carpdev == NULL) {
1413                 carp_set_state(sc, INIT);
1414                 return;
1415         }
1416
1417         if ((cifp->if_flags & IFF_RUNNING) && sc->sc_vhid > 0 &&
1418             (sc->sc_naddrs || sc->sc_naddrs6)) {
1419                 /* Nothing */
1420         } else {
1421                 carp_setroute(sc, RTM_DELETE);
1422                 return;
1423         }
1424
1425         switch (sc->sc_state) {
1426         case INIT:
1427                 if (carp_opts[CARPCTL_PREEMPT] && !carp_suppress_preempt) {
1428                         carp_send_ad(sc);
1429                         carp_send_arp(sc);
1430 #ifdef INET6
1431                         carp_send_na(sc);
1432 #endif /* INET6 */
1433                         CARP_DEBUG("%s: INIT -> MASTER (preempting)\n",
1434                                    cifp->if_xname);
1435                         carp_set_state(sc, MASTER);
1436                         carp_setroute(sc, RTM_ADD);
1437                 } else {
1438                         CARP_DEBUG("%s: INIT -> BACKUP\n", cifp->if_xname);
1439                         carp_set_state(sc, BACKUP);
1440                         carp_setroute(sc, RTM_DELETE);
1441                         carp_setrun(sc, 0);
1442                 }
1443                 break;
1444
1445         case BACKUP:
1446                 callout_stop(&sc->sc_ad_tmo);
1447                 tv.tv_sec = 3 * sc->sc_advbase;
1448                 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1449                 switch (af) {
1450 #ifdef INET
1451                 case AF_INET:
1452                         callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv),
1453                             carp_master_down_timeout, sc);
1454                         break;
1455 #endif /* INET */
1456 #ifdef INET6
1457                 case AF_INET6:
1458                         callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv),
1459                             carp_master_down_timeout, sc);
1460                         break;
1461 #endif /* INET6 */
1462                 default:
1463                         if (sc->sc_naddrs)
1464                                 callout_reset(&sc->sc_md_tmo, tvtohz_high(&tv),
1465                                     carp_master_down_timeout, sc);
1466                         if (sc->sc_naddrs6)
1467                                 callout_reset(&sc->sc_md6_tmo, tvtohz_high(&tv),
1468                                     carp_master_down_timeout, sc);
1469                         break;
1470                 }
1471                 break;
1472
1473         case MASTER:
1474                 tv.tv_sec = sc->sc_advbase;
1475                 tv.tv_usec = sc->sc_advskew * 1000000 / 256;
1476                 callout_reset(&sc->sc_ad_tmo, tvtohz_high(&tv),
1477                     carp_send_ad_timeout, sc);
1478                 break;
1479         }
1480 }
1481
1482 static void
1483 carp_multicast_cleanup(struct carp_softc *sc)
1484 {
1485         struct ip_moptions *imo = &sc->sc_imo;
1486
1487         if (imo->imo_num_memberships == 0)
1488                 return;
1489         KKASSERT(imo->imo_num_memberships == 1);
1490
1491         in_delmulti(imo->imo_membership[0]);
1492         imo->imo_membership[0] = NULL;
1493         imo->imo_num_memberships = 0;
1494         imo->imo_multicast_ifp = NULL;
1495 }
1496
1497 #ifdef INET6
1498 static void
1499 carp_multicast6_cleanup(struct carp_softc *sc)
1500 {
1501         struct ip6_moptions *im6o = &sc->sc_im6o;
1502
1503         while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1504                 struct in6_multi_mship *imm =
1505                     LIST_FIRST(&im6o->im6o_memberships);
1506
1507                 LIST_REMOVE(imm, i6mm_chain);
1508                 in6_leavegroup(imm);
1509         }
1510         im6o->im6o_multicast_ifp = NULL;
1511 }
1512 #endif
1513
1514 static int
1515 carp_get_vhaddr(struct carp_softc *sc, struct ifdrv *ifd)
1516 {
1517         const struct carp_vhaddr *vha;
1518         struct ifcarpvhaddr *carpa, *carpa0;
1519         int count, len, error;
1520
1521         count = 0;
1522         TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
1523                 ++count;
1524
1525         if (ifd->ifd_len == 0) {
1526                 ifd->ifd_len = count * sizeof(*carpa);
1527                 return 0;
1528         } else if (count == 0 || ifd->ifd_len < sizeof(*carpa)) {
1529                 ifd->ifd_len = 0;
1530                 return 0;
1531         }
1532         len = min(ifd->ifd_len, sizeof(*carpa) * count);
1533         KKASSERT(len >= sizeof(*carpa));
1534
1535         carpa0 = carpa = kmalloc(len, M_TEMP, M_WAITOK | M_NULLOK | M_ZERO);
1536         if (carpa == NULL)
1537                 return ENOMEM;
1538
1539         count = 0;
1540         TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1541                 if (len < sizeof(*carpa))
1542                         break;
1543
1544                 carpa->carpa_flags = vha->vha_flags;
1545                 carpa->carpa_addr.sin_family = AF_INET;
1546                 carpa->carpa_addr.sin_addr = vha->vha_ia->ia_addr.sin_addr;
1547
1548                 carpa->carpa_baddr.sin_family = AF_INET;
1549                 if (vha->vha_iaback == NULL) {
1550                         carpa->carpa_baddr.sin_addr.s_addr = INADDR_ANY;
1551                 } else {
1552                         carpa->carpa_baddr.sin_addr =
1553                         vha->vha_iaback->ia_addr.sin_addr;
1554                 }
1555
1556                 ++carpa;
1557                 ++count;
1558                 len -= sizeof(*carpa);
1559         }
1560         ifd->ifd_len = sizeof(*carpa) * count;
1561         KKASSERT(ifd->ifd_len > 0);
1562
1563         error = copyout(carpa0, ifd->ifd_data, ifd->ifd_len);
1564         kfree(carpa0, M_TEMP);
1565         return error;
1566 }
1567
1568 static int
1569 carp_config_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
1570     struct in_ifaddr *ia_del)
1571 {
1572         struct ifnet *ifp;
1573         struct in_ifaddr *ia_if;
1574         struct in_ifaddr_container *iac;
1575         const struct sockaddr_in *sin;
1576         u_long iaddr;
1577         int own;
1578
1579         KKASSERT(vha->vha_ia != NULL);
1580
1581         sin = &vha->vha_ia->ia_addr;
1582         iaddr = ntohl(sin->sin_addr.s_addr);
1583
1584         ia_if = NULL;
1585         own = 0;
1586         TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
1587                 struct in_ifaddr *ia = iac->ia;
1588
1589                 if (ia == ia_del)
1590                         continue;
1591
1592                 if (ia->ia_ifp->if_type == IFT_CARP)
1593                         continue;
1594
1595                 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
1596                         continue;
1597
1598                 /* and, yeah, we need a multicast-capable iface too */
1599                 if ((ia->ia_ifp->if_flags & IFF_MULTICAST) == 0)
1600                         continue;
1601
1602                 if ((iaddr & ia->ia_subnetmask) == ia->ia_subnet) {
1603                         if (sin->sin_addr.s_addr ==
1604                             ia->ia_addr.sin_addr.s_addr)
1605                                 own = 1;
1606                         if (ia_if == NULL)
1607                                 ia_if = ia;
1608                         else if (sc->sc_carpdev != NULL &&
1609                                  sc->sc_carpdev == ia->ia_ifp)
1610                                 ia_if = ia;
1611                 }
1612         }
1613
1614         carp_deactivate_vhaddr(sc, vha, FALSE);
1615         if (!ia_if)
1616                 return ENOENT;
1617
1618         ifp = ia_if->ia_ifp;
1619
1620         /* XXX Don't allow parent iface to be changed */
1621         if (sc->sc_carpdev != NULL && sc->sc_carpdev != ifp)
1622                 return EEXIST;
1623
1624         return carp_activate_vhaddr(sc, vha, ifp, ia_if, own);
1625 }
1626
1627 static void
1628 carp_add_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
1629 {
1630         struct carp_vhaddr *vha_new;
1631         struct in_ifaddr *carp_ia;
1632 #ifdef INVARIANTS
1633         struct carp_vhaddr *vha;
1634 #endif
1635
1636         KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
1637         carp_ia = ifatoia(carp_ifa);
1638
1639 #ifdef INVARIANTS
1640         TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
1641                 KKASSERT(vha->vha_ia != NULL && vha->vha_ia != carp_ia);
1642 #endif
1643
1644         vha_new = kmalloc(sizeof(*vha_new), M_CARP, M_WAITOK | M_ZERO);
1645         vha_new->vha_ia = carp_ia;
1646         carp_insert_vhaddr(sc, vha_new);
1647
1648         if (carp_config_vhaddr(sc, vha_new, NULL) != 0) {
1649                 /*
1650                  * If the above configuration fails, it may only mean
1651                  * that the new address is problematic.  However, the
1652                  * carp(4) interface may already have several working
1653                  * addresses.  Since the expected behaviour of
1654                  * SIOC[AS]IFADDR is to put the NIC into working state,
1655                  * we try starting the state machine manually here with
1656                  * the hope that the carp(4)'s previously working
1657                  * addresses still could be brought up.
1658                  */
1659                 carp_hmac_prepare(sc);
1660                 carp_set_state(sc, INIT);
1661                 carp_setrun(sc, 0);
1662         }
1663 }
1664
1665 static void
1666 carp_del_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
1667 {
1668         struct carp_vhaddr *vha;
1669         struct in_ifaddr *carp_ia;
1670
1671         KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
1672         carp_ia = ifatoia(carp_ifa);
1673
1674         TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1675                 KKASSERT(vha->vha_ia != NULL);
1676                 if (vha->vha_ia == carp_ia)
1677                         break;
1678         }
1679         KASSERT(vha != NULL, ("no corresponding vhaddr %p\n", carp_ifa));
1680
1681         /*
1682          * Remove the vhaddr from the list before deactivating
1683          * the vhaddr, so that the HMAC could be correctly
1684          * updated in carp_deactivate_vhaddr()
1685          */
1686         carp_remove_vhaddr(sc, vha);
1687
1688         carp_deactivate_vhaddr(sc, vha, FALSE);
1689         kfree(vha, M_CARP);
1690 }
1691
1692 static void
1693 carp_config_addr(struct carp_softc *sc, struct ifaddr *carp_ifa)
1694 {
1695         struct carp_vhaddr *vha;
1696         struct in_ifaddr *carp_ia;
1697
1698         KKASSERT(carp_ifa->ifa_addr->sa_family == AF_INET);
1699         carp_ia = ifatoia(carp_ifa);
1700
1701         TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
1702                 KKASSERT(vha->vha_ia != NULL);
1703                 if (vha->vha_ia == carp_ia)
1704                         break;
1705         }
1706         KASSERT(vha != NULL, ("no corresponding vhaddr %p\n", carp_ifa));
1707
1708         /* Remove then reinsert, to keep the vhaddr list sorted */
1709         carp_remove_vhaddr(sc, vha);
1710         carp_insert_vhaddr(sc, vha);
1711
1712         if (carp_config_vhaddr(sc, vha, NULL) != 0) {
1713                 /* See the comment in carp_add_addr() */
1714                 carp_hmac_prepare(sc);
1715                 carp_set_state(sc, INIT);
1716                 carp_setrun(sc, 0);
1717         }
1718 }
1719
1720 #ifdef notyet
1721
1722 #ifdef INET6
1723 static int
1724 carp_set_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1725 {
1726         struct ifnet *ifp;
1727         struct carp_if *cif;
1728         struct in6_ifaddr *ia, *ia_if;
1729         struct ip6_moptions *im6o = &sc->sc_im6o;
1730         struct in6_multi_mship *imm;
1731         struct in6_addr in6;
1732         int own, error;
1733
1734         if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1735                 carp_setrun(sc, 0);
1736                 return (0);
1737         }
1738
1739         /* we have to do it by hands to check we won't match on us */
1740         ia_if = NULL; own = 0;
1741         for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1742                 int i;
1743
1744                 for (i = 0; i < 4; i++) {
1745                         if ((sin6->sin6_addr.s6_addr32[i] &
1746                             ia->ia_prefixmask.sin6_addr.s6_addr32[i]) !=
1747                             (ia->ia_addr.sin6_addr.s6_addr32[i] &
1748                             ia->ia_prefixmask.sin6_addr.s6_addr32[i]))
1749                                 break;
1750                 }
1751                 /* and, yeah, we need a multicast-capable iface too */
1752                 if (ia->ia_ifp != &sc->sc_if &&
1753                     (ia->ia_ifp->if_flags & IFF_MULTICAST) &&
1754                     (i == 4)) {
1755                         if (!ia_if)
1756                                 ia_if = ia;
1757                         if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
1758                             &ia->ia_addr.sin6_addr))
1759                                 own++;
1760                 }
1761         }
1762
1763         if (!ia_if)
1764                 return (EADDRNOTAVAIL);
1765         ia = ia_if;
1766         ifp = ia->ia_ifp;
1767
1768         if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0 ||
1769             (im6o->im6o_multicast_ifp && im6o->im6o_multicast_ifp != ifp))
1770                 return (EADDRNOTAVAIL);
1771
1772         if (!sc->sc_naddrs6) {
1773                 im6o->im6o_multicast_ifp = ifp;
1774
1775                 /* join CARP multicast address */
1776                 bzero(&in6, sizeof(in6));
1777                 in6.s6_addr16[0] = htons(0xff02);
1778                 in6.s6_addr8[15] = 0x12;
1779                 if (in6_setscope(&in6, ifp, NULL) != 0)
1780                         goto cleanup;
1781                 if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL)
1782                         goto cleanup;
1783                 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1784
1785                 /* join solicited multicast address */
1786                 bzero(&in6, sizeof(in6));
1787                 in6.s6_addr16[0] = htons(0xff02);
1788                 in6.s6_addr32[1] = 0;
1789                 in6.s6_addr32[2] = htonl(1);
1790                 in6.s6_addr32[3] = sin6->sin6_addr.s6_addr32[3];
1791                 in6.s6_addr8[12] = 0xff;
1792                 if (in6_setscope(&in6, ifp, NULL) != 0)
1793                         goto cleanup;
1794                 if ((imm = in6_joingroup(ifp, &in6, &error)) == NULL)
1795                         goto cleanup;
1796                 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
1797         }
1798
1799         if (!ifp->if_carp) {
1800                 cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO);
1801
1802                 if ((error = ifpromisc(ifp, 1))) {
1803                         kfree(cif, M_CARP);
1804                         goto cleanup;
1805                 }
1806
1807                 TAILQ_INIT(&cif->vhif_vrs);
1808                 ifp->if_carp = cif;
1809         } else {
1810                 struct carp_softc *vr;
1811
1812                 cif = ifp->if_carp;
1813                 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1814                         if (vr != sc && vr->sc_vhid == sc->sc_vhid) {
1815                                 error = EINVAL;
1816                                 goto cleanup;
1817                         }
1818                 }
1819         }
1820         sc->sc_ia6 = ia;
1821         sc->sc_carpdev = ifp;
1822
1823         { /* XXX prevent endless loop if already in queue */
1824         struct carp_softc *vr, *after = NULL;
1825         int myself = 0;
1826         cif = ifp->if_carp;
1827
1828         TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
1829                 if (vr == sc)
1830                         myself = 1;
1831                 if (vr->sc_vhid < sc->sc_vhid)
1832                         after = vr;
1833         }
1834
1835         if (!myself) {
1836                 /* We're trying to keep things in order */
1837                 if (after == NULL)
1838                         TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
1839                 else
1840                         TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
1841         }
1842         }
1843
1844         sc->sc_naddrs6++;
1845         if (own)
1846                 sc->sc_advskew = 0;
1847         carp_sc_state(sc);
1848         carp_setrun(sc, 0);
1849
1850         return (0);
1851
1852 cleanup:
1853         /* clean up multicast memberships */
1854         if (!sc->sc_naddrs6) {
1855                 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1856                         imm = LIST_FIRST(&im6o->im6o_memberships);
1857                         LIST_REMOVE(imm, i6mm_chain);
1858                         in6_leavegroup(imm);
1859                 }
1860         }
1861         return (error);
1862 }
1863
1864 static int
1865 carp_del_addr6(struct carp_softc *sc, struct sockaddr_in6 *sin6)
1866 {
1867         int error = 0;
1868
1869         if (!--sc->sc_naddrs6) {
1870                 struct carp_if *cif = sc->sc_carpdev->if_carp;
1871                 struct ip6_moptions *im6o = &sc->sc_im6o;
1872
1873                 callout_stop(&sc->sc_ad_tmo);
1874                 sc->sc_vhid = -1;
1875                 while (!LIST_EMPTY(&im6o->im6o_memberships)) {
1876                         struct in6_multi_mship *imm =
1877                             LIST_FIRST(&im6o->im6o_memberships);
1878
1879                         LIST_REMOVE(imm, i6mm_chain);
1880                         in6_leavegroup(imm);
1881                 }
1882                 im6o->im6o_multicast_ifp = NULL;
1883                 TAILQ_REMOVE(&cif->vhif_vrs, sc, sc_list);
1884                 if (TAILQ_EMPTY(&cif->vhif_vrs)) {
1885                         sc->sc_carpdev->if_carp = NULL;
1886                         kfree(cif, M_IFADDR);
1887                 }
1888         }
1889         return (error);
1890 }
1891 #endif /* INET6 */
1892
1893 #endif
1894
1895 static int
1896 carp_ioctl(struct ifnet *ifp, u_long cmd, caddr_t addr, struct ucred *cr)
1897 {
1898         struct carp_softc *sc = ifp->if_softc;
1899         struct carpreq carpr;
1900         struct ifaddr *ifa;
1901         struct ifreq *ifr;
1902         struct ifaliasreq *ifra;
1903         struct ifdrv *ifd;
1904         char devname[IFNAMSIZ];
1905         int error = 0;
1906
1907         ASSERT_IFNET_SERIALIZED_ALL(ifp);
1908
1909         carp_gettok();
1910
1911         ifa = (struct ifaddr *)addr;
1912         ifra = (struct ifaliasreq *)addr;
1913         ifr = (struct ifreq *)addr;
1914         ifd = (struct ifdrv *)addr;
1915
1916         switch (cmd) {
1917         case SIOCSIFFLAGS:
1918                 if (ifp->if_flags & IFF_UP) {
1919                         if ((ifp->if_flags & IFF_RUNNING) == 0)
1920                                 carp_init(sc);
1921                 } else if (ifp->if_flags & IFF_RUNNING) {
1922                         carp_ioctl_stop(sc);
1923                 }
1924                 break;
1925
1926         case SIOCSVH:
1927                 error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY);
1928                 if (error)
1929                         break;
1930                 error = copyin(ifr->ifr_data, &carpr, sizeof(carpr));
1931                 if (error)
1932                         break;
1933
1934                 error = carp_ioctl_setvh(sc, &carpr);
1935                 break;
1936
1937         case SIOCGVH:
1938                 carp_ioctl_getvh(sc, &carpr);
1939                 error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY);
1940                 if (error)
1941                         bzero(carpr.carpr_key, sizeof(carpr.carpr_key));
1942
1943                 error = copyout(&carpr, ifr->ifr_data, sizeof(carpr));
1944                 break;
1945
1946         case SIOCGDRVSPEC:
1947                 switch (ifd->ifd_cmd) {
1948                 case CARPGDEVNAME:
1949                         if (ifd->ifd_len != sizeof(devname))
1950                                 error = EINVAL;
1951                         break;
1952
1953                 case CARPGVHADDR:
1954                         break;
1955
1956                 default:
1957                         error = EINVAL;
1958                         break;
1959                 }
1960                 if (error)
1961                         break;
1962
1963                 switch (ifd->ifd_cmd) {
1964                 case CARPGVHADDR:
1965                         error = carp_get_vhaddr(sc, ifd);
1966                         break;
1967
1968                 case CARPGDEVNAME:
1969                         bzero(devname, sizeof(devname));
1970                         if (sc->sc_carpdev != NULL) {
1971                                 strlcpy(devname, sc->sc_carpdev->if_xname,
1972                                         sizeof(devname));
1973                         }
1974                         error = copyout(devname, ifd->ifd_data,
1975                                         sizeof(devname));
1976                         break;
1977                 }
1978                 break;
1979
1980         default:
1981                 error = ether_ioctl(ifp, cmd, addr);
1982                 break;
1983         }
1984
1985         carp_reltok();
1986         return error;
1987 }
1988
1989 static void
1990 carp_ioctl_stop_dispatch(netmsg_t msg)
1991 {
1992         struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
1993         struct carp_softc *sc = cmsg->nc_softc;
1994
1995         carp_gettok();
1996         carp_stop(sc, 0);
1997         carp_reltok();
1998
1999         lwkt_replymsg(&cmsg->base.lmsg, 0);
2000 }
2001
2002 static void
2003 carp_ioctl_stop(struct carp_softc *sc)
2004 {
2005         struct ifnet *ifp = &sc->arpcom.ac_if;
2006         struct netmsg_carp cmsg;
2007
2008         ASSERT_IFNET_SERIALIZED_ALL(ifp);
2009
2010         ifnet_deserialize_all(ifp);
2011
2012         bzero(&cmsg, sizeof(cmsg));
2013         netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2014             carp_ioctl_stop_dispatch);
2015         cmsg.nc_softc = sc;
2016
2017         lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0);
2018
2019         ifnet_serialize_all(ifp);
2020 }
2021
2022 static void
2023 carp_ioctl_setvh_dispatch(netmsg_t msg)
2024 {
2025         struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2026         struct carp_softc *sc = cmsg->nc_softc, *vr;
2027         struct ifnet *ifp = &sc->arpcom.ac_if;
2028         const struct carpreq *carpr = cmsg->nc_data;
2029         int error;
2030
2031         carp_gettok();
2032
2033         error = 1;
2034         if ((ifp->if_flags & IFF_RUNNING) &&
2035             sc->sc_state != INIT && carpr->carpr_state != sc->sc_state) {
2036                 switch (carpr->carpr_state) {
2037                 case BACKUP:
2038                         callout_stop(&sc->sc_ad_tmo);
2039                         carp_set_state(sc, BACKUP);
2040                         carp_setrun(sc, 0);
2041                         carp_setroute(sc, RTM_DELETE);
2042                         break;
2043
2044                 case MASTER:
2045                         carp_master_down(sc);
2046                         break;
2047
2048                 default:
2049                         break;
2050                 }
2051         }
2052         if (carpr->carpr_vhid > 0) {
2053                 if (carpr->carpr_vhid > 255) {
2054                         error = EINVAL;
2055                         goto back;
2056                 }
2057                 if (sc->sc_carpdev) {
2058                         struct carp_if *cif = sc->sc_carpdev->if_carp;
2059
2060                         TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
2061                                 if (vr != sc &&
2062                                     vr->sc_vhid == carpr->carpr_vhid) {
2063                                         error = EEXIST;
2064                                         goto back;
2065                                 }
2066                         }
2067                 }
2068                 sc->sc_vhid = carpr->carpr_vhid;
2069
2070                 IF_LLADDR(ifp)[5] = sc->sc_vhid;
2071                 bcopy(IF_LLADDR(ifp), sc->arpcom.ac_enaddr,
2072                     ETHER_ADDR_LEN);
2073
2074                 error--;
2075         }
2076         if (carpr->carpr_advbase > 0 || carpr->carpr_advskew > 0) {
2077                 if (carpr->carpr_advskew >= 255) {
2078                         error = EINVAL;
2079                         goto back;
2080                 }
2081                 if (carpr->carpr_advbase > 255) {
2082                         error = EINVAL;
2083                         goto back;
2084                 }
2085                 sc->sc_advbase = carpr->carpr_advbase;
2086                 sc->sc_advskew = carpr->carpr_advskew;
2087                 error--;
2088         }
2089         bcopy(carpr->carpr_key, sc->sc_key, sizeof(sc->sc_key));
2090         if (error > 0) {
2091                 error = EINVAL;
2092         } else {
2093                 error = 0;
2094                 carp_setrun(sc, 0);
2095         }
2096 back:
2097         carp_hmac_prepare(sc);
2098         carp_gettok();
2099
2100         lwkt_replymsg(&cmsg->base.lmsg, error);
2101 }
2102
2103 static int
2104 carp_ioctl_setvh(struct carp_softc *sc, struct carpreq *carpr)
2105 {
2106         struct ifnet *ifp = &sc->arpcom.ac_if;
2107         struct netmsg_carp cmsg;
2108         int error;
2109
2110         ASSERT_IFNET_SERIALIZED_ALL(ifp);
2111
2112         ifnet_deserialize_all(ifp);
2113
2114         bzero(&cmsg, sizeof(cmsg));
2115         netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2116             carp_ioctl_setvh_dispatch);
2117         cmsg.nc_softc = sc;
2118         cmsg.nc_data = carpr;
2119
2120         error = lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0);
2121
2122         ifnet_serialize_all(ifp);
2123
2124         return error;
2125 }
2126
2127 static void
2128 carp_ioctl_getvh_dispatch(netmsg_t msg)
2129 {
2130         struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2131         struct carp_softc *sc = cmsg->nc_softc;
2132         struct carpreq *carpr = cmsg->nc_data;
2133
2134         carp_gettok();
2135
2136         carpr->carpr_state = sc->sc_state;
2137         carpr->carpr_vhid = sc->sc_vhid;
2138         carpr->carpr_advbase = sc->sc_advbase;
2139         carpr->carpr_advskew = sc->sc_advskew;
2140         bcopy(sc->sc_key, carpr->carpr_key, sizeof(carpr->carpr_key));
2141
2142         carp_reltok();
2143
2144         lwkt_replymsg(&cmsg->base.lmsg, 0);
2145 }
2146
2147 static void
2148 carp_ioctl_getvh(struct carp_softc *sc, struct carpreq *carpr)
2149 {
2150         struct ifnet *ifp = &sc->arpcom.ac_if;
2151         struct netmsg_carp cmsg;
2152
2153         ASSERT_IFNET_SERIALIZED_ALL(ifp);
2154
2155         ifnet_deserialize_all(ifp);
2156
2157         bzero(&cmsg, sizeof(cmsg));
2158         netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2159             carp_ioctl_getvh_dispatch);
2160         cmsg.nc_softc = sc;
2161         cmsg.nc_data = carpr;
2162
2163         lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0);
2164
2165         ifnet_serialize_all(ifp);
2166 }
2167
2168 static void
2169 carp_init_dispatch(netmsg_t msg)
2170 {
2171         struct netmsg_carp *cmsg = (struct netmsg_carp *)msg;
2172         struct carp_softc *sc = cmsg->nc_softc;
2173
2174         carp_gettok();
2175
2176         sc->sc_if.if_flags |= IFF_RUNNING;
2177         carp_hmac_prepare(sc);
2178         carp_set_state(sc, INIT);
2179         carp_setrun(sc, 0);
2180
2181         carp_reltok();
2182
2183         lwkt_replymsg(&cmsg->base.lmsg, 0);
2184 }
2185
2186 static void
2187 carp_init(void *xsc)
2188 {
2189         struct carp_softc *sc = xsc;
2190         struct ifnet *ifp = &sc->arpcom.ac_if;
2191         struct netmsg_carp cmsg;
2192
2193         ASSERT_IFNET_SERIALIZED_ALL(ifp);
2194
2195         ifnet_deserialize_all(ifp);
2196
2197         bzero(&cmsg, sizeof(cmsg));
2198         netmsg_init(&cmsg.base, NULL, &curthread->td_msgport, 0,
2199             carp_init_dispatch);
2200         cmsg.nc_softc = sc;
2201
2202         lwkt_domsg(cpu_portfn(0), &cmsg.base.lmsg, 0);
2203
2204         ifnet_serialize_all(ifp);
2205 }
2206
2207 static int
2208 carp_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
2209     struct rtentry *rt)
2210 {
2211         struct carp_softc *sc = ifp->if_softc;
2212         int error = 0;
2213
2214         carp_gettok();
2215         if (sc->sc_carpdev) {
2216                 /*
2217                  * NOTE:
2218                  * CARP's ifp is passed to backing device's
2219                  * if_output method.
2220                  */
2221                 sc->sc_carpdev->if_output(ifp, m, dst, rt);
2222         } else {
2223                 m_freem(m);
2224                 error = ENETUNREACH;
2225         }
2226         carp_reltok();
2227
2228         return error;
2229 }
2230
2231 /*
2232  * Start output on carp interface. This function should never be called.
2233  */
2234 static void
2235 carp_start(struct ifnet *ifp)
2236 {
2237         panic("%s: start called\n", ifp->if_xname);
2238 }
2239
2240 static void
2241 carp_serialize(struct ifnet *ifp __unused,
2242     enum ifnet_serialize slz __unused)
2243 {
2244 }
2245
2246 static void
2247 carp_deserialize(struct ifnet *ifp __unused,
2248     enum ifnet_serialize slz __unused)
2249 {
2250 }
2251
2252 static int
2253 carp_tryserialize(struct ifnet *ifp __unused,
2254     enum ifnet_serialize slz __unused)
2255 {
2256         return 1;
2257 }
2258
2259 #ifdef INVARIANTS
2260
2261 static void
2262 carp_serialize_assert(struct ifnet *ifp __unused,
2263     enum ifnet_serialize slz __unused, boolean_t serialized __unused)
2264 {
2265 }
2266
2267 #endif  /* INVARIANTS */
2268
2269 static void
2270 carp_set_state(struct carp_softc *sc, int state)
2271 {
2272         struct ifnet *cifp = &sc->sc_if;
2273
2274         if (sc->sc_state == state)
2275                 return;
2276         sc->sc_state = state;
2277
2278         switch (sc->sc_state) {
2279         case BACKUP:
2280                 cifp->if_link_state = LINK_STATE_DOWN;
2281                 break;
2282
2283         case MASTER:
2284                 cifp->if_link_state = LINK_STATE_UP;
2285                 break;
2286
2287         default:
2288                 cifp->if_link_state = LINK_STATE_UNKNOWN;
2289                 break;
2290         }
2291         rt_ifmsg(cifp);
2292 }
2293
2294 void
2295 carp_group_demote_adj(struct ifnet *ifp, int adj)
2296 {
2297         struct ifg_list *ifgl;
2298         int *dm;
2299
2300         carp_gettok();
2301
2302         TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
2303                 if (!strcmp(ifgl->ifgl_group->ifg_group, IFG_ALL))
2304                         continue;
2305                 dm = &ifgl->ifgl_group->ifg_carp_demoted;
2306
2307                 if (*dm + adj >= 0)
2308                         *dm += adj;
2309                 else
2310                         *dm = 0;
2311
2312                 if (adj > 0 && *dm == 1)
2313                         carp_send_ad_all();
2314                 CARP_LOG("%s demoted group %s to %d", ifp->if_xname,
2315                     ifgl->ifgl_group->ifg_group, *dm);
2316         }
2317
2318         carp_reltok();
2319 }
2320
2321 void
2322 carp_carpdev_state(void *v)
2323 {
2324         struct carp_if *cif = v;
2325         struct carp_softc *sc;
2326
2327         carp_gettok();
2328
2329         TAILQ_FOREACH(sc, &cif->vhif_vrs, sc_list)
2330                 carp_sc_state(sc);
2331
2332         carp_reltok();
2333 }
2334
2335 static void
2336 carp_sc_state(struct carp_softc *sc)
2337 {
2338         if (!(sc->sc_carpdev->if_flags & IFF_UP)) {
2339                 callout_stop(&sc->sc_ad_tmo);
2340                 callout_stop(&sc->sc_md_tmo);
2341                 callout_stop(&sc->sc_md6_tmo);
2342                 carp_set_state(sc, INIT);
2343                 carp_setrun(sc, 0);
2344                 if (!sc->sc_suppress) {
2345                         carp_suppress_preempt++;
2346                         if (carp_suppress_preempt == 1)
2347                                 carp_send_ad_all();
2348                 }
2349                 sc->sc_suppress = 1;
2350         } else {
2351                 carp_set_state(sc, INIT);
2352                 carp_setrun(sc, 0);
2353                 if (sc->sc_suppress)
2354                         carp_suppress_preempt--;
2355                 sc->sc_suppress = 0;
2356         }
2357 }
2358
2359 static void
2360 carp_stop(struct carp_softc *sc, int detach)
2361 {
2362         sc->sc_if.if_flags &= ~IFF_RUNNING;
2363
2364         callout_stop(&sc->sc_ad_tmo);
2365         callout_stop(&sc->sc_md_tmo);
2366         callout_stop(&sc->sc_md6_tmo);
2367
2368         if (!detach && sc->sc_state == MASTER)
2369                 carp_send_ad(sc);
2370
2371         if (sc->sc_suppress)
2372                 carp_suppress_preempt--;
2373         sc->sc_suppress = 0;
2374
2375         if (sc->sc_sendad_errors >= CARP_SENDAD_MAX_ERRORS)
2376                 carp_suppress_preempt--;
2377         sc->sc_sendad_errors = 0;
2378         sc->sc_sendad_success = 0;
2379
2380         carp_set_state(sc, INIT);
2381         carp_setrun(sc, 0);
2382 }
2383
2384 static void
2385 carp_suspend(struct carp_softc *sc, int detach)
2386 {
2387         struct ifnet *cifp = &sc->sc_if;
2388
2389         carp_stop(sc, detach);
2390
2391         /* Retain the running state, if we are not dead yet */
2392         if (!sc->sc_dead && (cifp->if_flags & IFF_UP))
2393                 cifp->if_flags |= IFF_RUNNING;
2394 }
2395
2396 static int
2397 carp_activate_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
2398     struct ifnet *ifp, struct in_ifaddr *ia_if, int own)
2399 {
2400         struct ip_moptions *imo = &sc->sc_imo;
2401         struct carp_if *cif;
2402         struct carp_softc *vr, *after = NULL;
2403         int onlist, error;
2404 #ifdef INVARIANTS
2405         int assert_onlist;
2406 #endif
2407
2408         KKASSERT(vha->vha_ia != NULL);
2409
2410         KASSERT(ia_if != NULL, ("NULL backing address\n"));
2411         KASSERT(vha->vha_iaback == NULL, ("%p is already activated\n", vha));
2412         KASSERT((vha->vha_flags & CARP_VHAF_OWNER) == 0,
2413                 ("inactive vhaddr %p is the address owner\n", vha));
2414
2415         KASSERT(sc->sc_carpdev == NULL || sc->sc_carpdev == ifp,
2416                 ("%s is already on %s\n", sc->sc_if.if_xname,
2417                  sc->sc_carpdev->if_xname));
2418
2419         if (!ifp->if_carp) {
2420                 KASSERT(sc->sc_carpdev == NULL,
2421                         ("%s is already on %s\n", sc->sc_if.if_xname,
2422                          sc->sc_carpdev->if_xname));
2423
2424                 cif = kmalloc(sizeof(*cif), M_CARP, M_WAITOK | M_ZERO);
2425
2426                 error = ifpromisc(ifp, 1);
2427                 if (error) {
2428                         kfree(cif, M_CARP);
2429                         return error;
2430                 }
2431
2432                 TAILQ_INIT(&cif->vhif_vrs);
2433                 ifp->if_carp = cif;
2434         } else {
2435                 cif = ifp->if_carp;
2436                 TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
2437                         if (vr != sc && vr->sc_vhid == sc->sc_vhid)
2438                                 return EINVAL;
2439                 }
2440         }
2441
2442 #ifdef INVARIANTS
2443         if (sc->sc_carpdev != NULL)
2444                 assert_onlist = 1;
2445         else
2446                 assert_onlist = 0;
2447 #endif
2448         sc->sc_ia = ia_if;
2449         sc->sc_carpdev = ifp;
2450
2451         cif = ifp->if_carp;
2452         onlist = 0;
2453         TAILQ_FOREACH(vr, &cif->vhif_vrs, sc_list) {
2454                 if (vr == sc)
2455                         onlist = 1;
2456                 if (vr->sc_vhid < sc->sc_vhid)
2457                         after = vr;
2458         }
2459
2460 #ifdef INVARIANTS
2461         if (assert_onlist) {
2462                 KASSERT(onlist, ("%s is not on %s carp list\n",
2463                         sc->sc_if.if_xname, ifp->if_xname));
2464         } else {
2465                 KASSERT(!onlist, ("%s is already on %s carp list\n",
2466                         sc->sc_if.if_xname, ifp->if_xname));
2467         }
2468 #endif
2469
2470         if (!onlist) {
2471                 /* We're trying to keep things in order */
2472                 if (after == NULL)
2473                         TAILQ_INSERT_TAIL(&cif->vhif_vrs, sc, sc_list);
2474                 else
2475                         TAILQ_INSERT_AFTER(&cif->vhif_vrs, after, sc, sc_list);
2476         }
2477
2478         vha->vha_iaback = ia_if;
2479         sc->sc_naddrs++;
2480
2481         if (own) {
2482                 vha->vha_flags |= CARP_VHAF_OWNER;
2483
2484                 /* XXX save user configured advskew? */
2485                 sc->sc_advskew = 0;
2486         }
2487
2488         carp_addroute_vhaddr(sc, vha);
2489
2490         /*
2491          * Join the multicast group only after the backing interface
2492          * has been hooked with the CARP interface.
2493          */
2494         KASSERT(imo->imo_multicast_ifp == NULL ||
2495                 imo->imo_multicast_ifp == &sc->sc_if,
2496                 ("%s didn't leave mcast group on %s\n",
2497                  sc->sc_if.if_xname, imo->imo_multicast_ifp->if_xname));
2498
2499         if (imo->imo_num_memberships == 0) {
2500                 struct in_addr addr;
2501
2502                 addr.s_addr = htonl(INADDR_CARP_GROUP);
2503                 imo->imo_membership[0] = in_addmulti(&addr, &sc->sc_if);
2504                 if (imo->imo_membership[0] == NULL) {
2505                         carp_deactivate_vhaddr(sc, vha, FALSE);
2506                         return ENOBUFS;
2507                 }
2508
2509                 imo->imo_num_memberships++;
2510                 imo->imo_multicast_ifp = &sc->sc_if;
2511                 imo->imo_multicast_ttl = CARP_DFLTTL;
2512                 imo->imo_multicast_loop = 0;
2513         }
2514
2515         carp_hmac_prepare(sc);
2516         carp_set_state(sc, INIT);
2517         carp_setrun(sc, 0);
2518         return 0;
2519 }
2520
2521 static void
2522 carp_deactivate_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
2523     boolean_t del_iaback)
2524 {
2525         KKASSERT(vha->vha_ia != NULL);
2526
2527         carp_hmac_prepare(sc);
2528
2529         if (vha->vha_iaback == NULL) {
2530                 KASSERT((vha->vha_flags & CARP_VHAF_OWNER) == 0,
2531                         ("inactive vhaddr %p is the address owner\n", vha));
2532                 return;
2533         }
2534
2535         vha->vha_flags &= ~CARP_VHAF_OWNER;
2536         carp_delroute_vhaddr(sc, vha, del_iaback);
2537
2538         KKASSERT(sc->sc_naddrs > 0);
2539         vha->vha_iaback = NULL;
2540         sc->sc_naddrs--;
2541         if (!sc->sc_naddrs) {
2542                 if (sc->sc_naddrs6) {
2543                         carp_multicast_cleanup(sc);
2544                         sc->sc_ia = NULL;
2545                 } else {
2546                         carp_detach(sc, 0, del_iaback);
2547                 }
2548         }
2549 }
2550
2551 static void
2552 carp_link_addrs(struct carp_softc *sc, struct ifnet *ifp, struct ifaddr *ifa_if)
2553 {
2554         struct carp_vhaddr *vha;
2555         struct in_ifaddr *ia_if;
2556
2557         KKASSERT(ifa_if->ifa_addr->sa_family == AF_INET);
2558         ia_if = ifatoia(ifa_if);
2559
2560         /*
2561          * Test each inactive vhaddr against the newly added address.
2562          * If the newly added address could be the backing address,
2563          * then activate the matching vhaddr.
2564          */
2565         TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
2566                 const struct in_ifaddr *ia;
2567                 u_long iaddr;
2568                 int own;
2569
2570                 if (vha->vha_iaback != NULL)
2571                         continue;
2572
2573                 ia = vha->vha_ia;
2574                 iaddr = ntohl(ia->ia_addr.sin_addr.s_addr);
2575
2576                 if ((iaddr & ia_if->ia_subnetmask) != ia_if->ia_subnet)
2577                         continue;
2578
2579                 own = 0;
2580                 if (ia->ia_addr.sin_addr.s_addr ==
2581                     ia_if->ia_addr.sin_addr.s_addr)
2582                         own = 1;
2583
2584                 carp_activate_vhaddr(sc, vha, ifp, ia_if, own);
2585         }
2586 }
2587
2588 static void
2589 carp_unlink_addrs(struct carp_softc *sc, struct ifnet *ifp,
2590                   struct ifaddr *ifa_if)
2591 {
2592         struct carp_vhaddr *vha;
2593         struct in_ifaddr *ia_if;
2594
2595         KKASSERT(ifa_if->ifa_addr->sa_family == AF_INET);
2596         ia_if = ifatoia(ifa_if);
2597
2598         /*
2599          * Ad src address is deleted; set it to NULL.
2600          * Following loop will try pick up a new ad src address
2601          * if one of the vhaddr could retain its backing address.
2602          */
2603         if (sc->sc_ia == ia_if)
2604                 sc->sc_ia = NULL;
2605
2606         /*
2607          * Test each active vhaddr against the deleted address.
2608          * If the deleted address is vhaddr address's backing
2609          * address, then deactivate the vhaddr.
2610          */
2611         TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link) {
2612                 if (vha->vha_iaback == NULL)
2613                         continue;
2614
2615                 if (vha->vha_iaback == ia_if)
2616                         carp_deactivate_vhaddr(sc, vha, TRUE);
2617                 else if (sc->sc_ia == NULL)
2618                         sc->sc_ia = vha->vha_iaback;
2619         }
2620 }
2621
2622 static void
2623 carp_update_addrs(struct carp_softc *sc, struct ifaddr *ifa_del)
2624 {
2625         struct carp_vhaddr *vha;
2626
2627         KKASSERT(sc->sc_carpdev == NULL);
2628
2629         TAILQ_FOREACH(vha, &sc->sc_vha_list, vha_link)
2630                 carp_config_vhaddr(sc, vha, ifatoia(ifa_del));
2631 }
2632
2633 static void
2634 carp_ifaddr(void *arg __unused, struct ifnet *ifp,
2635             enum ifaddr_event event, struct ifaddr *ifa)
2636 {
2637         struct carp_softc *sc;
2638
2639         carp_gettok();
2640
2641         if (ifa->ifa_addr->sa_family != AF_INET)
2642                 goto back;
2643
2644         KASSERT(&curthread->td_msgport == cpu_portfn(0),
2645             ("not in netisr0"));
2646
2647         if (ifp->if_type == IFT_CARP) {
2648                 /*
2649                  * Address is changed on carp(4) interface
2650                  */
2651                 switch (event) {
2652                 case IFADDR_EVENT_ADD:
2653                         carp_add_addr(ifp->if_softc, ifa);
2654                         break;
2655
2656                 case IFADDR_EVENT_CHANGE:
2657                         carp_config_addr(ifp->if_softc, ifa);
2658                         break;
2659
2660                 case IFADDR_EVENT_DELETE:
2661                         carp_del_addr(ifp->if_softc, ifa);
2662                         break;
2663                 }
2664                 goto back;
2665         }
2666
2667         /*
2668          * Address is changed on non-carp(4) interface
2669          */
2670         if ((ifp->if_flags & IFF_MULTICAST) == 0)
2671                 goto back;
2672
2673         LIST_FOREACH(sc, &carpif_list, sc_next) {
2674                 if (sc->sc_carpdev != NULL && sc->sc_carpdev != ifp) {
2675                         /* Not the parent iface; skip */
2676                         continue;
2677                 }
2678
2679                 switch (event) {
2680                 case IFADDR_EVENT_ADD:
2681                         carp_link_addrs(sc, ifp, ifa);
2682                         break;
2683
2684                 case IFADDR_EVENT_DELETE:
2685                         if (sc->sc_carpdev != NULL) {
2686                                 carp_unlink_addrs(sc, ifp, ifa);
2687                                 if (sc->sc_carpdev == NULL) {
2688                                         /*
2689                                          * We no longer have the parent
2690                                          * interface, however, certain
2691                                          * virtual addresses, which are
2692                                          * not used because they can't
2693                                          * match the previous parent
2694                                          * interface's addresses, may now
2695                                          * match different interface's
2696                                          * addresses.
2697                                          */
2698                                         carp_update_addrs(sc, ifa);
2699                                 }
2700                         } else {
2701                                 /*
2702                                  * The carp(4) interface didn't have a
2703                                  * parent iface, so it is not possible
2704                                  * that it will contain any address to
2705                                  * be unlinked.
2706                                  */
2707                         }
2708                         break;
2709
2710                 case IFADDR_EVENT_CHANGE:
2711                         if (sc->sc_carpdev == NULL) {
2712                                 /*
2713                                  * The carp(4) interface didn't have a
2714                                  * parent iface, so it is not possible
2715                                  * that it will contain any address to
2716                                  * be updated.
2717                                  */
2718                                 carp_link_addrs(sc, ifp, ifa);
2719                         } else {
2720                                 /*
2721                                  * First try breaking tie with the old
2722                                  * address.  Then see whether we could
2723                                  * link certain vhaddr to the new address.
2724                                  * If that fails, i.e. carpdev is NULL,
2725                                  * we try a global update.
2726                                  *
2727                                  * NOTE: The above order is critical.
2728                                  */
2729                                 carp_unlink_addrs(sc, ifp, ifa);
2730                                 carp_link_addrs(sc, ifp, ifa);
2731                                 if (sc->sc_carpdev == NULL) {
2732                                         /*
2733                                          * See the comment in the above
2734                                          * IFADDR_EVENT_DELETE block.
2735                                          */
2736                                         carp_update_addrs(sc, NULL);
2737                                 }
2738                         }
2739                         break;
2740                 }
2741         }
2742
2743 back:
2744         carp_reltok();
2745 }
2746
2747 void
2748 carp_proto_ctlinput(netmsg_t msg)
2749 {
2750         int cmd = msg->ctlinput.nm_cmd;
2751         struct sockaddr *sa = msg->ctlinput.nm_arg;
2752         struct in_ifaddr_container *iac;
2753
2754         carp_gettok();
2755
2756         TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
2757                 struct in_ifaddr *ia = iac->ia;
2758                 struct ifnet *ifp = ia->ia_ifp;
2759
2760                 if (ifp->if_type == IFT_CARP)
2761                         continue;
2762
2763                 if (ia->ia_ifa.ifa_addr == sa) {
2764                         if (cmd == PRC_IFDOWN) {
2765                                 carp_ifaddr(NULL, ifp, IFADDR_EVENT_DELETE,
2766                                     &ia->ia_ifa);
2767                         } else if (cmd == PRC_IFUP) {
2768                                 carp_ifaddr(NULL, ifp, IFADDR_EVENT_ADD,
2769                                     &ia->ia_ifa);
2770                         }
2771                         break;
2772                 }
2773         }
2774
2775         carp_reltok();
2776         lwkt_replymsg(&msg->lmsg, 0);
2777 }
2778
2779 void
2780 carp_gettok(void)
2781 {
2782         lwkt_gettoken(&carp_tok);
2783 }
2784
2785 void
2786 carp_reltok(void)
2787 {
2788         lwkt_reltoken(&carp_tok);
2789 }
2790
2791 struct ifnet *
2792 carp_parent(struct ifnet *cifp)
2793 {
2794         struct carp_softc *sc;
2795
2796         ASSERT_LWKT_TOKEN_HELD(&carp_tok);
2797
2798         KKASSERT(cifp->if_type == IFT_CARP);
2799         sc = cifp->if_softc;
2800
2801         return sc->sc_carpdev;
2802 }
2803
2804 #define rtinitflags(x) \
2805         (((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) \
2806                  ? RTF_HOST : 0)
2807
2808 static int
2809 carp_addroute_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha)
2810 {
2811         struct in_ifaddr *ia, *iaback;
2812         int error;
2813
2814         if (sc->sc_state != MASTER)
2815                 return 0;
2816
2817         ia = vha->vha_ia;
2818         KKASSERT(ia != NULL);
2819
2820         iaback = vha->vha_iaback;
2821         KKASSERT(iaback != NULL);
2822
2823         rtinit(&iaback->ia_ifa, RTM_DELETE, rtinitflags(iaback));
2824         in_ifadown(&iaback->ia_ifa, 1);
2825         iaback->ia_flags &= ~IFA_ROUTE;
2826
2827         error = rtinit(&ia->ia_ifa, RTM_ADD, rtinitflags(ia) | RTF_UP);
2828         if (!error)
2829                 ia->ia_flags |= IFA_ROUTE;
2830         return error;
2831 }
2832
2833 static void
2834 carp_delroute_vhaddr(struct carp_softc *sc, struct carp_vhaddr *vha,
2835     boolean_t del_iaback)
2836 {
2837         struct in_ifaddr *ia, *iaback;
2838
2839         ia = vha->vha_ia;
2840         KKASSERT(ia != NULL);
2841
2842         iaback = vha->vha_iaback;
2843         KKASSERT(iaback != NULL);
2844
2845         rtinit(&ia->ia_ifa, RTM_DELETE, rtinitflags(ia));
2846         in_ifadown(&ia->ia_ifa, 1);
2847         ia->ia_flags &= ~IFA_ROUTE;
2848
2849         if (!del_iaback && (iaback->ia_ifp->if_flags & IFF_UP)) {
2850                 int error;
2851
2852                 error = rtinit(&iaback->ia_ifa, RTM_ADD,
2853                     rtinitflags(iaback) | RTF_UP);
2854                 if (!error)
2855                         iaback->ia_flags |= IFA_ROUTE;
2856         }
2857 }
2858
2859 static int
2860 carp_modevent(module_t mod, int type, void *data)
2861 {
2862         switch (type) {
2863         case MOD_LOAD:
2864                 LIST_INIT(&carpif_list);
2865                 carp_ifdetach_event =
2866                 EVENTHANDLER_REGISTER(ifnet_detach_event, carp_ifdetach, NULL,
2867                                       EVENTHANDLER_PRI_ANY);
2868                 carp_ifaddr_event =
2869                 EVENTHANDLER_REGISTER(ifaddr_event, carp_ifaddr, NULL,
2870                                       EVENTHANDLER_PRI_FIRST);
2871                 if_clone_attach(&carp_cloner);
2872                 break;
2873
2874         case MOD_UNLOAD:
2875                 EVENTHANDLER_DEREGISTER(ifnet_detach_event,
2876                                         carp_ifdetach_event);
2877                 EVENTHANDLER_DEREGISTER(ifaddr_event,
2878                                         carp_ifaddr_event);
2879                 if_clone_detach(&carp_cloner);
2880                 break;
2881
2882         default:
2883                 return (EINVAL);
2884         }
2885         return (0);
2886 }
2887
2888 static moduledata_t carp_mod = {
2889         "carp",
2890         carp_modevent,
2891         0
2892 };
2893 DECLARE_MODULE(carp, carp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);