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