1 /* $OpenBSD: if_pfsync.c,v 1.98 2008/06/29 08:42:15 mcbride Exp $ */
4 * Copyright (c) 2002 Michael Shalayeff
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26 * THE POSSIBILITY OF SUCH DAMAGE.
30 #include "opt_inet6.h"
33 #include <sys/param.h>
34 #include <sys/endian.h>
37 #include <sys/systm.h>
40 #include <sys/socket.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/msgport2.h>
45 #include <sys/sockio.h>
46 #include <sys/thread2.h>
48 #include <machine/inttypes.h>
51 #include <net/if_types.h>
52 #include <net/ifq_var.h>
53 #include <net/route.h>
55 #include <net/netisr2.h>
56 #include <net/netmsg2.h>
57 #include <netinet/in.h>
58 #include <netinet/if_ether.h>
59 #include <netinet/ip_carp.h>
60 #include <netinet/tcp.h>
61 #include <netinet/tcp_seq.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/in_var.h>
66 #include <netinet/ip.h>
67 #include <netinet/ip_var.h>
71 #include <netinet6/nd6.h>
74 #include <net/pf/pfvar.h>
75 #include <net/pf/if_pfsync.h>
77 #define PFSYNCNAME "pfsync"
79 #define PFSYNC_MINMTU \
80 (sizeof(struct pfsync_header) + sizeof(struct pf_state))
83 #define DPRINTF(x) do { if (pfsyncdebug) kprintf x ; } while (0)
89 struct pfsync_softc *pfsyncif = NULL;
90 struct pfsyncstats pfsyncstats;
92 void pfsyncattach(int);
93 static int pfsync_clone_destroy(struct ifnet *);
94 static int pfsync_clone_create(struct if_clone *, int, caddr_t);
95 void pfsync_setmtu(struct pfsync_softc *, int);
96 int pfsync_alloc_scrub_memory(struct pfsync_state_peer *,
97 struct pf_state_peer *);
98 int pfsyncoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
100 int pfsyncioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
101 void pfsyncstart(struct ifnet *, struct ifaltq_subque *);
103 struct mbuf *pfsync_get_mbuf(struct pfsync_softc *, u_int8_t, void **);
104 int pfsync_request_update(struct pfsync_state_upd *, struct in_addr *);
105 int pfsync_sendout(struct pfsync_softc *);
106 int pfsync_sendout_mbuf(struct pfsync_softc *, struct mbuf *);
107 void pfsync_timeout(void *);
108 void pfsync_send_bus(struct pfsync_softc *, u_int8_t);
109 void pfsync_bulk_update(void *);
110 void pfsync_bulkfail(void *);
112 static struct in_multi *pfsync_in_addmulti(struct ifnet *);
113 static void pfsync_in_delmulti(struct in_multi *);
115 static MALLOC_DEFINE(M_PFSYNC, PFSYNCNAME, "Packet Filter State Sync. Interface");
116 static LIST_HEAD(pfsync_list, pfsync_softc) pfsync_list;
120 struct if_clone pfsync_cloner =
121 IF_CLONE_INITIALIZER("pfsync", pfsync_clone_create, pfsync_clone_destroy, 1 ,1);
124 pfsyncattach(int npfsync)
126 if_clone_attach(&pfsync_cloner);
129 pfsync_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused)
131 struct pfsync_softc *sc;
134 lwkt_gettoken(&pf_token);
136 sc = kmalloc(sizeof(*sc), M_PFSYNC, M_WAITOK | M_ZERO);
139 sc->sc_mbuf_net = NULL;
140 sc->sc_mbuf_tdb = NULL;
141 sc->sc_statep.s = NULL;
142 sc->sc_statep_net.s = NULL;
143 sc->sc_statep_tdb.t = NULL;
144 sc->sc_maxupdates = 128;
145 sc->sc_sync_peer.s_addr =htonl(INADDR_PFSYNC_GROUP);
146 sc->sc_sendaddr.s_addr = htonl(INADDR_PFSYNC_GROUP);
147 sc->sc_ureq_received = 0;
148 sc->sc_ureq_sent = 0;
149 sc->sc_bulk_send_next = NULL;
150 sc->sc_bulk_terminator = NULL;
151 sc->sc_imo.imo_max_memberships = IP_MAX_MEMBERSHIPS;
152 lwkt_reltoken(&pf_token);
154 ksnprintf(ifp->if_xname, sizeof ifp->if_xname, "pfsync%d", unit);
155 if_initname(ifp, ifc->ifc_name, unit);
156 ifp->if_ioctl = pfsyncioctl;
157 ifp->if_output = pfsyncoutput;
158 ifp->if_start = pfsyncstart;
159 ifp->if_type = IFT_PFSYNC;
160 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
161 ifp->if_hdrlen = PFSYNC_HDRLEN;
162 ifp->if_baudrate = IF_Mbps(100);
164 pfsync_setmtu(sc, MCLBYTES);
165 callout_init(&sc->sc_tmo);
166 /* callout_init(&sc->sc_tdb_tmo); XXX we don't support tdb (yet) */
167 callout_init(&sc->sc_bulk_tmo);
168 callout_init(&sc->sc_bulkfail_tmo);
169 if_attach(ifp, NULL);
171 LIST_INSERT_HEAD(&pfsync_list, sc, sc_next);
175 if_addgroup(ifp, "carp");
179 bpfattach(&sc->sc_if, DLT_PFSYNC, PFSYNC_HDRLEN);
181 lwkt_gettoken(&pf_token);
183 lwkt_reltoken(&pf_token);
188 pfsync_clone_destroy(struct ifnet *ifp)
190 lwkt_gettoken(&pf_token);
191 lwkt_reltoken(&pf_token);
193 struct pfsync_softc *sc = ifp->if_softc;
194 callout_stop(&sc->sc_tmo);
195 /* callout_stop(&sc->sc_tdb_tmo); XXX we don't support tdb (yet) */
196 callout_stop(&sc->sc_bulk_tmo);
197 callout_stop(&sc->sc_bulkfail_tmo);
200 carp_group_demote_adj(&sc->sc_if, -1);
206 lwkt_gettoken(&pf_token);
207 LIST_REMOVE(sc, sc_next);
209 lwkt_reltoken(&pf_token);
216 * Start output on the pfsync interface.
219 pfsyncstart(struct ifnet *ifp, struct ifaltq_subque *ifsq)
221 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
226 pfsync_alloc_scrub_memory(struct pfsync_state_peer *s,
227 struct pf_state_peer *d)
229 if (s->scrub.scrub_flag && d->scrub == NULL) {
230 d->scrub = kmalloc(sizeof(struct pf_state_scrub), M_PFSYNC, M_NOWAIT|M_ZERO);
232 if (d->scrub == NULL)
240 pfsync_state_export(struct pfsync_state *sp, struct pf_state *st)
242 bzero(sp, sizeof(struct pfsync_state));
244 /* copy from state key */
245 sp->key[PF_SK_WIRE].addr[0] = st->key[PF_SK_WIRE]->addr[0];
246 sp->key[PF_SK_WIRE].addr[1] = st->key[PF_SK_WIRE]->addr[1];
247 sp->key[PF_SK_WIRE].port[0] = st->key[PF_SK_WIRE]->port[0];
248 sp->key[PF_SK_WIRE].port[1] = st->key[PF_SK_WIRE]->port[1];
249 sp->key[PF_SK_STACK].addr[0] = st->key[PF_SK_STACK]->addr[0];
250 sp->key[PF_SK_STACK].addr[1] = st->key[PF_SK_STACK]->addr[1];
251 sp->key[PF_SK_STACK].port[0] = st->key[PF_SK_STACK]->port[0];
252 sp->key[PF_SK_STACK].port[1] = st->key[PF_SK_STACK]->port[1];
253 sp->proto = st->key[PF_SK_WIRE]->proto;
254 sp->af = st->key[PF_SK_WIRE]->af;
256 /* copy from state */
257 strlcpy(sp->ifname, st->kif->pfik_name, sizeof(sp->ifname));
258 bcopy(&st->rt_addr, &sp->rt_addr, sizeof(sp->rt_addr));
259 sp->creation = htonl(time_second - st->creation);
260 sp->expire = pf_state_expires(st);
261 if (sp->expire <= time_second)
262 sp->expire = htonl(0);
264 sp->expire = htonl(sp->expire - time_second);
266 sp->direction = st->direction;
268 sp->timeout = st->timeout;
269 sp->state_flags = st->state_flags;
271 sp->sync_flags |= PFSYNC_FLAG_SRCNODE;
272 if (st->nat_src_node)
273 sp->sync_flags |= PFSYNC_FLAG_NATSRCNODE;
275 bcopy(&st->id, &sp->id, sizeof(sp->id));
276 sp->creatorid = st->creatorid;
277 pf_state_peer_hton(&st->src, &sp->src);
278 pf_state_peer_hton(&st->dst, &sp->dst);
280 if (st->rule.ptr == NULL)
281 sp->rule = htonl(-1);
283 sp->rule = htonl(st->rule.ptr->nr);
284 if (st->anchor.ptr == NULL)
285 sp->anchor = htonl(-1);
287 sp->anchor = htonl(st->anchor.ptr->nr);
288 if (st->nat_rule.ptr == NULL)
289 sp->nat_rule = htonl(-1);
291 sp->nat_rule = htonl(st->nat_rule.ptr->nr);
293 pf_state_counter_hton(st->packets[0], sp->packets[0]);
294 pf_state_counter_hton(st->packets[1], sp->packets[1]);
295 pf_state_counter_hton(st->bytes[0], sp->bytes[0]);
296 pf_state_counter_hton(st->bytes[1], sp->bytes[1]);
301 pfsync_state_import(struct pfsync_state *sp, u_int8_t flags)
303 struct pf_state *st = NULL;
304 struct pf_state_key *skw = NULL, *sks = NULL;
305 struct pf_rule *r = NULL;
310 if (sp->creatorid == 0 && pf_status.debug >= PF_DEBUG_MISC) {
311 kprintf("pfsync_insert_net_state: invalid creator id:"
312 " %08x\n", ntohl(sp->creatorid));
316 if ((kif = pfi_kif_get(sp->ifname)) == NULL) {
317 if (pf_status.debug >= PF_DEBUG_MISC)
318 kprintf("pfsync_insert_net_state: "
319 "unknown interface: %s\n", sp->ifname);
320 if (flags & PFSYNC_SI_IOCTL)
322 return (0); /* skip this state */
326 * If the ruleset checksums match or the state is coming from the ioctl,
327 * it's safe to associate the state with the rule of that number.
329 if (sp->rule != htonl(-1) && sp->anchor == htonl(-1) &&
330 (flags & (PFSYNC_SI_IOCTL | PFSYNC_SI_CKSUM)) && ntohl(sp->rule) <
331 pf_main_ruleset.rules[PF_RULESET_FILTER].active.rcount)
332 r = pf_main_ruleset.rules[
333 PF_RULESET_FILTER].active.ptr_array[ntohl(sp->rule)];
335 r = &pf_default_rule;
337 if ((r->max_states && r->states_cur >= r->max_states))
340 if (flags & PFSYNC_SI_IOCTL)
341 pool_flags = M_WAITOK | M_NULLOK | M_ZERO;
343 pool_flags = M_WAITOK | M_ZERO;
345 if ((st = kmalloc(sizeof(struct pf_state), M_PFSYNC, pool_flags)) == NULL)
348 if ((skw = pf_alloc_state_key(pool_flags)) == NULL)
351 if (PF_ANEQ(&sp->key[PF_SK_WIRE].addr[0],
352 &sp->key[PF_SK_STACK].addr[0], sp->af) ||
353 PF_ANEQ(&sp->key[PF_SK_WIRE].addr[1],
354 &sp->key[PF_SK_STACK].addr[1], sp->af) ||
355 sp->key[PF_SK_WIRE].port[0] != sp->key[PF_SK_STACK].port[0] ||
356 sp->key[PF_SK_WIRE].port[1] != sp->key[PF_SK_STACK].port[1]) {
357 if ((sks = pf_alloc_state_key(pool_flags)) == NULL)
362 /* allocate memory for scrub info */
363 if (pfsync_alloc_scrub_memory(&sp->src, &st->src) ||
364 pfsync_alloc_scrub_memory(&sp->dst, &st->dst))
367 /* copy to state key(s) */
368 skw->addr[0] = sp->key[PF_SK_WIRE].addr[0];
369 skw->addr[1] = sp->key[PF_SK_WIRE].addr[1];
370 skw->port[0] = sp->key[PF_SK_WIRE].port[0];
371 skw->port[1] = sp->key[PF_SK_WIRE].port[1];
372 skw->proto = sp->proto;
375 sks->addr[0] = sp->key[PF_SK_STACK].addr[0];
376 sks->addr[1] = sp->key[PF_SK_STACK].addr[1];
377 sks->port[0] = sp->key[PF_SK_STACK].port[0];
378 sks->port[1] = sp->key[PF_SK_STACK].port[1];
379 sks->proto = sp->proto;
384 bcopy(&sp->rt_addr, &st->rt_addr, sizeof(st->rt_addr));
385 st->creation = time_second - ntohl(sp->creation);
386 st->expire = time_second;
388 /* XXX No adaptive scaling. */
389 st->expire -= r->timeout[sp->timeout] - ntohl(sp->expire);
392 st->expire = ntohl(sp->expire) + time_second;
393 st->direction = sp->direction;
395 st->timeout = sp->timeout;
396 st->state_flags = sp->state_flags;
397 if (!(flags & PFSYNC_SI_IOCTL))
398 st->sync_flags = PFSTATE_FROMSYNC;
400 bcopy(sp->id, &st->id, sizeof(st->id));
401 st->creatorid = sp->creatorid;
402 pf_state_peer_ntoh(&sp->src, &st->src);
403 pf_state_peer_ntoh(&sp->dst, &st->dst);
406 st->nat_rule.ptr = NULL;
407 st->anchor.ptr = NULL;
413 /* XXX when we have nat_rule/anchors, use STATE_INC_COUNTERS */
417 if ((error = pf_state_insert(kif, skw, sks, st)) != 0) {
418 /* XXX when we have nat_rule/anchors, use STATE_DEC_COUNTERS */
430 kfree(skw, M_PFSYNC);
432 kfree(sks, M_PFSYNC);
434 cleanup_state: /* pf_state_insert frees the state keys */
437 kfree(st->dst.scrub, M_PFSYNC);
439 kfree(st->src.scrub, M_PFSYNC);
446 pfsync_input(struct mbuf *m, ...)
448 struct ip *ip = mtod(m, struct ip *);
449 struct pfsync_header *ph;
450 struct pfsync_softc *sc = pfsyncif;
452 struct pf_state_key *sk;
453 struct pf_state_item *si;
454 struct pf_state_cmp id_key;
455 struct pfsync_state *sp;
456 struct pfsync_state_upd *up;
457 struct pfsync_state_del *dp;
458 struct pfsync_state_clr *cp;
459 struct pfsync_state_upd_req *rup;
460 struct pfsync_state_bus *bus;
462 struct pfsync_tdb *pt;
466 int iplen, action, error, i, count, offp, sfail, stale = 0;
469 /* This function is not yet called from anywhere */
470 /* Still we assume for safety that pf_token must be held */
471 ASSERT_LWKT_TOKEN_HELD(&pf_token);
473 pfsyncstats.pfsyncs_ipackets++;
475 /* verify that we have a sync interface configured */
476 if (!sc || !sc->sc_sync_ifp || !pf_status.running)
479 /* verify that the packet came in on the right interface */
480 if (sc->sc_sync_ifp != m->m_pkthdr.rcvif) {
481 pfsyncstats.pfsyncs_badif++;
485 /* verify that the IP TTL is 255. */
486 if (ip->ip_ttl != PFSYNC_DFLTTL) {
487 pfsyncstats.pfsyncs_badttl++;
491 iplen = ip->ip_hl << 2;
493 if (m->m_pkthdr.len < iplen + sizeof(*ph)) {
494 pfsyncstats.pfsyncs_hdrops++;
498 if (iplen + sizeof(*ph) > m->m_len) {
499 if ((m = m_pullup(m, iplen + sizeof(*ph))) == NULL) {
500 pfsyncstats.pfsyncs_hdrops++;
503 ip = mtod(m, struct ip *);
505 ph = (struct pfsync_header *)((char *)ip + iplen);
507 /* verify the version */
508 if (ph->version != PFSYNC_VERSION) {
509 pfsyncstats.pfsyncs_badver++;
516 /* make sure it's a valid action code */
517 if (action >= PFSYNC_ACT_MAX) {
518 pfsyncstats.pfsyncs_badact++;
522 /* Cheaper to grab this now than having to mess with mbufs later */
525 if (!bcmp(&ph->pf_chksum, &pf_status.pf_chksum, PF_MD5_DIGEST_LENGTH))
526 flags |= PFSYNC_SI_CKSUM;
529 case PFSYNC_ACT_CLR: {
530 struct pf_state *nexts;
531 struct pf_state_key *nextsk;
534 if ((mp = m_pulldown(m, iplen + sizeof(*ph),
535 sizeof(*cp), &offp)) == NULL) {
536 pfsyncstats.pfsyncs_badlen++;
539 cp = (struct pfsync_state_clr *)(mp->m_data + offp);
540 creatorid = cp->creatorid;
543 if (cp->ifname[0] == '\0') {
544 for (st = RB_MIN(pf_state_tree_id, &tree_id);
546 nexts = RB_NEXT(pf_state_tree_id, &tree_id, st);
547 if (st->creatorid == creatorid) {
548 st->sync_flags |= PFSTATE_FROMSYNC;
553 if ((kif = pfi_kif_get(cp->ifname)) == NULL) {
558 for (sk = RB_MIN(pf_state_tree,
559 &pf_statetbl); sk; sk = nextsk) {
560 nextsk = RB_NEXT(pf_state_tree,
562 TAILQ_FOREACH(si, &sk->states, entry) {
563 if (si->s->creatorid == creatorid) {
566 pf_unlink_state(si->s);
576 if ((mp = m_pulldown(m, iplen + sizeof(*ph),
577 count * sizeof(*sp), &offp)) == NULL) {
578 pfsyncstats.pfsyncs_badlen++;
583 for (i = 0, sp = (struct pfsync_state *)(mp->m_data + offp);
584 i < count; i++, sp++) {
585 /* check for invalid values */
586 if (sp->timeout >= PFTM_MAX ||
587 sp->src.state > PF_TCPS_PROXY_DST ||
588 sp->dst.state > PF_TCPS_PROXY_DST ||
589 sp->direction > PF_OUT ||
590 (sp->af != AF_INET && sp->af != AF_INET6)) {
591 if (pf_status.debug >= PF_DEBUG_MISC)
592 kprintf("pfsync_insert: PFSYNC_ACT_INS: "
594 pfsyncstats.pfsyncs_badval++;
598 if ((error = pfsync_state_import(sp, flags))) {
599 if (error == ENOMEM) {
609 if ((mp = m_pulldown(m, iplen + sizeof(*ph),
610 count * sizeof(*sp), &offp)) == NULL) {
611 pfsyncstats.pfsyncs_badlen++;
616 for (i = 0, sp = (struct pfsync_state *)(mp->m_data + offp);
617 i < count; i++, sp++) {
618 int flags = PFSYNC_FLAG_STALE;
620 /* check for invalid values */
621 if (sp->timeout >= PFTM_MAX ||
622 sp->src.state > PF_TCPS_PROXY_DST ||
623 sp->dst.state > PF_TCPS_PROXY_DST) {
624 if (pf_status.debug >= PF_DEBUG_MISC)
625 kprintf("pfsync_insert: PFSYNC_ACT_UPD: "
627 pfsyncstats.pfsyncs_badval++;
631 bcopy(sp->id, &id_key.id, sizeof(id_key.id));
632 id_key.creatorid = sp->creatorid;
634 st = pf_find_state_byid(&id_key);
636 /* insert the update */
637 if (pfsync_state_import(sp, flags))
638 pfsyncstats.pfsyncs_badstate++;
641 sk = st->key[PF_SK_WIRE]; /* XXX right one? */
643 if (sk->proto == IPPROTO_TCP) {
645 * The state should never go backwards except
646 * for syn-proxy states. Neither should the
647 * sequence window slide backwards.
649 if (st->src.state > sp->src.state &&
650 (st->src.state < PF_TCPS_PROXY_SRC ||
651 sp->src.state >= PF_TCPS_PROXY_SRC))
653 else if (SEQ_GT(st->src.seqlo,
654 ntohl(sp->src.seqlo)))
656 else if (st->dst.state > sp->dst.state) {
657 /* There might still be useful
658 * information about the src state here,
659 * so import that part of the update,
660 * then "fail" so we send the updated
661 * state back to the peer who is missing
662 * our what we know. */
663 pf_state_peer_ntoh(&sp->src, &st->src);
664 /* XXX do anything with timeouts? */
667 } else if (st->dst.state >= TCPS_SYN_SENT &&
668 SEQ_GT(st->dst.seqlo, ntohl(sp->dst.seqlo)))
672 * Non-TCP protocol state machine always go
675 if (st->src.state > sp->src.state)
677 else if (st->dst.state > sp->dst.state)
681 if (pf_status.debug >= PF_DEBUG_MISC)
682 kprintf("pfsync: %s stale update "
685 (sfail < 7 ? "ignoring"
687 (uintmax_t)be64toh(st->id),
688 ntohl(st->creatorid));
689 pfsyncstats.pfsyncs_stale++;
691 if (!(sp->sync_flags & PFSTATE_STALE)) {
692 /* we have a better state, send it */
693 if (sc->sc_mbuf != NULL && !stale)
698 PFSYNC_ACT_UPD, st, flags);
702 pfsync_alloc_scrub_memory(&sp->dst, &st->dst);
703 pf_state_peer_ntoh(&sp->src, &st->src);
704 pf_state_peer_ntoh(&sp->dst, &st->dst);
705 st->expire = ntohl(sp->expire) + time_second;
706 st->timeout = sp->timeout;
708 if (stale && sc->sc_mbuf != NULL)
713 * It's not strictly necessary for us to support the "uncompressed"
714 * delete action, but it's relatively simple and maintains consistency.
717 if ((mp = m_pulldown(m, iplen + sizeof(*ph),
718 count * sizeof(*sp), &offp)) == NULL) {
719 pfsyncstats.pfsyncs_badlen++;
724 for (i = 0, sp = (struct pfsync_state *)(mp->m_data + offp);
725 i < count; i++, sp++) {
726 bcopy(sp->id, &id_key.id, sizeof(id_key.id));
727 id_key.creatorid = sp->creatorid;
729 st = pf_find_state_byid(&id_key);
731 pfsyncstats.pfsyncs_badstate++;
734 st->sync_flags |= PFSTATE_FROMSYNC;
739 case PFSYNC_ACT_UPD_C: {
740 int update_requested = 0;
742 if ((mp = m_pulldown(m, iplen + sizeof(*ph),
743 count * sizeof(*up), &offp)) == NULL) {
744 pfsyncstats.pfsyncs_badlen++;
749 for (i = 0, up = (struct pfsync_state_upd *)(mp->m_data + offp);
750 i < count; i++, up++) {
751 /* check for invalid values */
752 if (up->timeout >= PFTM_MAX ||
753 up->src.state > PF_TCPS_PROXY_DST ||
754 up->dst.state > PF_TCPS_PROXY_DST) {
755 if (pf_status.debug >= PF_DEBUG_MISC)
756 kprintf("pfsync_insert: "
759 pfsyncstats.pfsyncs_badval++;
763 bcopy(up->id, &id_key.id, sizeof(id_key.id));
764 id_key.creatorid = up->creatorid;
766 st = pf_find_state_byid(&id_key);
768 /* We don't have this state. Ask for it. */
769 error = pfsync_request_update(up, &src);
770 if (error == ENOMEM) {
774 update_requested = 1;
775 pfsyncstats.pfsyncs_badstate++;
778 sk = st->key[PF_SK_WIRE]; /* XXX right one? */
780 if (sk->proto == IPPROTO_TCP) {
782 * The state should never go backwards except
783 * for syn-proxy states. Neither should the
784 * sequence window slide backwards.
786 if (st->src.state > up->src.state &&
787 (st->src.state < PF_TCPS_PROXY_SRC ||
788 up->src.state >= PF_TCPS_PROXY_SRC))
790 else if (st->dst.state > up->dst.state)
792 else if (SEQ_GT(st->src.seqlo,
793 ntohl(up->src.seqlo)))
795 else if (st->dst.state >= TCPS_SYN_SENT &&
796 SEQ_GT(st->dst.seqlo, ntohl(up->dst.seqlo)))
800 * Non-TCP protocol state machine always go
803 if (st->src.state > up->src.state)
805 else if (st->dst.state > up->dst.state)
809 if (pf_status.debug >= PF_DEBUG_MISC)
810 kprintf("pfsync: ignoring stale update "
811 "(%d) id: %016" PRIx64 " "
812 "creatorid: %08x\n", sfail,
814 ntohl(st->creatorid));
815 pfsyncstats.pfsyncs_stale++;
817 /* we have a better state, send it out */
818 if ((!stale || update_requested) &&
819 sc->sc_mbuf != NULL) {
821 update_requested = 0;
825 pfsync_pack_state(PFSYNC_ACT_UPD, st,
829 pfsync_alloc_scrub_memory(&up->dst, &st->dst);
830 pf_state_peer_ntoh(&up->src, &st->src);
831 pf_state_peer_ntoh(&up->dst, &st->dst);
832 st->expire = ntohl(up->expire) + time_second;
833 st->timeout = up->timeout;
835 if ((update_requested || stale) && sc->sc_mbuf)
840 case PFSYNC_ACT_DEL_C:
841 if ((mp = m_pulldown(m, iplen + sizeof(*ph),
842 count * sizeof(*dp), &offp)) == NULL) {
843 pfsyncstats.pfsyncs_badlen++;
848 for (i = 0, dp = (struct pfsync_state_del *)(mp->m_data + offp);
849 i < count; i++, dp++) {
850 bcopy(dp->id, &id_key.id, sizeof(id_key.id));
851 id_key.creatorid = dp->creatorid;
853 st = pf_find_state_byid(&id_key);
855 pfsyncstats.pfsyncs_badstate++;
858 st->sync_flags |= PFSTATE_FROMSYNC;
863 case PFSYNC_ACT_INS_F:
864 case PFSYNC_ACT_DEL_F:
865 /* not implemented */
867 case PFSYNC_ACT_UREQ:
868 if ((mp = m_pulldown(m, iplen + sizeof(*ph),
869 count * sizeof(*rup), &offp)) == NULL) {
870 pfsyncstats.pfsyncs_badlen++;
875 if (sc->sc_mbuf != NULL)
878 rup = (struct pfsync_state_upd_req *)(mp->m_data + offp);
879 i < count; i++, rup++) {
880 bcopy(rup->id, &id_key.id, sizeof(id_key.id));
881 id_key.creatorid = rup->creatorid;
883 if (id_key.id == 0 && id_key.creatorid == 0) {
884 sc->sc_ureq_received = mycpu->gd_time_seconds;
885 if (sc->sc_bulk_send_next == NULL)
886 sc->sc_bulk_send_next =
887 TAILQ_FIRST(&state_list);
888 sc->sc_bulk_terminator = sc->sc_bulk_send_next;
889 if (pf_status.debug >= PF_DEBUG_MISC)
890 kprintf("pfsync: received "
891 "bulk update request\n");
892 pfsync_send_bus(sc, PFSYNC_BUS_START);
893 lwkt_reltoken(&pf_token);
894 callout_init(&sc->sc_bulk_tmo);
895 lwkt_gettoken(&pf_token);
897 st = pf_find_state_byid(&id_key);
899 pfsyncstats.pfsyncs_badstate++;
903 pfsync_pack_state(PFSYNC_ACT_UPD,
907 if (sc->sc_mbuf != NULL)
912 /* If we're not waiting for a bulk update, who cares. */
913 if (sc->sc_ureq_sent == 0)
916 if ((mp = m_pulldown(m, iplen + sizeof(*ph),
917 sizeof(*bus), &offp)) == NULL) {
918 pfsyncstats.pfsyncs_badlen++;
921 bus = (struct pfsync_state_bus *)(mp->m_data + offp);
922 switch (bus->status) {
923 case PFSYNC_BUS_START:
924 lwkt_reltoken(&pf_token);
925 callout_reset(&sc->sc_bulkfail_tmo,
926 pf_pool_limits[PF_LIMIT_STATES].limit /
927 (PFSYNC_BULKPACKETS * sc->sc_maxcount),
928 pfsync_bulkfail, LIST_FIRST(&pfsync_list));
929 lwkt_gettoken(&pf_token);
930 if (pf_status.debug >= PF_DEBUG_MISC)
931 kprintf("pfsync: received bulk "
935 if (mycpu->gd_time_seconds - ntohl(bus->endtime) >=
937 /* that's it, we're happy */
938 sc->sc_ureq_sent = 0;
939 sc->sc_bulk_tries = 0;
940 lwkt_reltoken(&pf_token);
941 callout_stop(&sc->sc_bulkfail_tmo);
942 lwkt_gettoken(&pf_token);
944 if (!pfsync_sync_ok) {
945 lwkt_reltoken(&pf_token);
946 carp_group_demote_adj(&sc->sc_if, -1);
947 lwkt_gettoken(&pf_token);
951 if (pf_status.debug >= PF_DEBUG_MISC)
952 kprintf("pfsync: received valid "
953 "bulk update end\n");
955 if (pf_status.debug >= PF_DEBUG_MISC)
956 kprintf("pfsync: received invalid "
957 "bulk update end: bad timestamp\n");
963 case PFSYNC_ACT_TDB_UPD:
964 if ((mp = m_pulldown(m, iplen + sizeof(*ph),
965 count * sizeof(*pt), &offp)) == NULL) {
966 pfsyncstats.pfsyncs_badlen++;
970 for (i = 0, pt = (struct pfsync_tdb *)(mp->m_data + offp);
971 i < count; i++, pt++)
972 pfsync_update_net_tdb(pt);
984 pfsyncoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
993 pfsyncioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
995 struct pfsync_softc *sc = ifp->if_softc;
996 struct ifreq *ifr = (struct ifreq *)data;
997 struct ip_moptions *imo = &sc->sc_imo;
998 struct pfsyncreq pfsyncr;
1002 lwkt_gettoken(&pf_token);
1007 case SIOCSIFDSTADDR:
1009 if (ifp->if_flags & IFF_UP)
1010 ifp->if_flags |= IFF_RUNNING;
1012 ifp->if_flags &= ~IFF_RUNNING;
1015 if (ifr->ifr_mtu < PFSYNC_MINMTU) {
1016 lwkt_reltoken(&pf_token);
1019 if (ifr->ifr_mtu > MCLBYTES)
1020 ifr->ifr_mtu = MCLBYTES;
1022 if (ifr->ifr_mtu < ifp->if_mtu)
1024 pfsync_setmtu(sc, ifr->ifr_mtu);
1028 bzero(&pfsyncr, sizeof(pfsyncr));
1029 if (sc->sc_sync_ifp)
1030 strlcpy(pfsyncr.pfsyncr_syncdev,
1031 sc->sc_sync_ifp->if_xname, IFNAMSIZ);
1032 pfsyncr.pfsyncr_syncpeer = sc->sc_sync_peer;
1033 pfsyncr.pfsyncr_maxupdates = sc->sc_maxupdates;
1034 lwkt_reltoken(&pf_token);
1035 if ((error = copyout(&pfsyncr, ifr->ifr_data, sizeof(pfsyncr))))
1037 lwkt_gettoken(&pf_token);
1040 if ((error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY)) != 0) {
1041 lwkt_reltoken(&pf_token);
1044 if ((error = copyin(ifr->ifr_data, &pfsyncr, sizeof(pfsyncr)))) {
1045 lwkt_reltoken(&pf_token);
1049 if (pfsyncr.pfsyncr_syncpeer.s_addr == 0)
1050 sc->sc_sync_peer.s_addr = INADDR_PFSYNC_GROUP;
1052 sc->sc_sync_peer.s_addr =
1053 pfsyncr.pfsyncr_syncpeer.s_addr;
1055 if (pfsyncr.pfsyncr_maxupdates > 255) {
1056 lwkt_reltoken(&pf_token);
1059 sc->sc_maxupdates = pfsyncr.pfsyncr_maxupdates;
1061 if (pfsyncr.pfsyncr_syncdev[0] == 0) {
1062 sc->sc_sync_ifp = NULL;
1063 if (sc->sc_mbuf_net != NULL) {
1064 /* Don't keep stale pfsync packets around. */
1066 m_freem(sc->sc_mbuf_net);
1067 sc->sc_mbuf_net = NULL;
1068 sc->sc_statep_net.s = NULL;
1071 if (imo->imo_num_memberships > 0) {
1072 pfsync_in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1073 imo->imo_multicast_ifp = NULL;
1078 if ((sifp = ifunit(pfsyncr.pfsyncr_syncdev)) == NULL) {
1079 lwkt_reltoken(&pf_token);
1084 if (sifp->if_mtu < sc->sc_if.if_mtu ||
1085 (sc->sc_sync_ifp != NULL &&
1086 sifp->if_mtu < sc->sc_sync_ifp->if_mtu) ||
1087 sifp->if_mtu < MCLBYTES - sizeof(struct ip))
1089 sc->sc_sync_ifp = sifp;
1091 pfsync_setmtu(sc, sc->sc_if.if_mtu);
1093 if (imo->imo_num_memberships > 0) {
1094 pfsync_in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1095 imo->imo_multicast_ifp = NULL;
1098 if (sc->sc_sync_ifp &&
1099 sc->sc_sync_peer.s_addr == INADDR_PFSYNC_GROUP) {
1100 if (!(sc->sc_sync_ifp->if_flags & IFF_MULTICAST)) {
1101 sc->sc_sync_ifp = NULL;
1102 lwkt_reltoken(&pf_token);
1104 return (EADDRNOTAVAIL);
1107 if ((imo->imo_membership[0] =
1108 pfsync_in_addmulti(sc->sc_sync_ifp)) == NULL) {
1109 sc->sc_sync_ifp = NULL;
1110 lwkt_reltoken(&pf_token);
1114 imo->imo_num_memberships++;
1115 imo->imo_multicast_ifp = sc->sc_sync_ifp;
1116 imo->imo_multicast_ttl = PFSYNC_DFLTTL;
1117 imo->imo_multicast_loop = 0;
1120 if (sc->sc_sync_ifp ||
1121 sc->sc_sendaddr.s_addr != INADDR_PFSYNC_GROUP) {
1122 /* Request a full state table update. */
1123 sc->sc_ureq_sent = mycpu->gd_time_seconds;
1126 carp_group_demote_adj(&sc->sc_if, 1);
1129 if (pf_status.debug >= PF_DEBUG_MISC)
1130 kprintf("pfsync: requesting bulk update\n");
1131 lwkt_reltoken(&pf_token);
1132 callout_reset(&sc->sc_bulkfail_tmo, 5 * hz,
1133 pfsync_bulkfail, LIST_FIRST(&pfsync_list));
1134 lwkt_gettoken(&pf_token);
1135 error = pfsync_request_update(NULL, NULL);
1136 if (error == ENOMEM) {
1137 lwkt_reltoken(&pf_token);
1148 lwkt_reltoken(&pf_token);
1152 lwkt_reltoken(&pf_token);
1157 pfsync_setmtu(struct pfsync_softc *sc, int mtu_req)
1161 if (sc->sc_sync_ifp && sc->sc_sync_ifp->if_mtu < mtu_req)
1162 mtu = sc->sc_sync_ifp->if_mtu;
1166 sc->sc_maxcount = (mtu - sizeof(struct pfsync_header)) /
1167 sizeof(struct pfsync_state);
1168 if (sc->sc_maxcount > 254)
1169 sc->sc_maxcount = 254;
1170 sc->sc_if.if_mtu = sizeof(struct pfsync_header) +
1171 sc->sc_maxcount * sizeof(struct pfsync_state);
1175 pfsync_get_mbuf(struct pfsync_softc *sc, u_int8_t action, void **sp)
1177 struct pfsync_header *h;
1181 ASSERT_LWKT_TOKEN_HELD(&pf_token);
1183 MGETHDR(m, M_WAITOK, MT_DATA);
1185 IFNET_STAT_INC(&sc->sc_if, oerrors, 1);
1190 case PFSYNC_ACT_CLR:
1191 len = sizeof(struct pfsync_header) +
1192 sizeof(struct pfsync_state_clr);
1194 case PFSYNC_ACT_UPD_C:
1195 len = (sc->sc_maxcount * sizeof(struct pfsync_state_upd)) +
1196 sizeof(struct pfsync_header);
1198 case PFSYNC_ACT_DEL_C:
1199 len = (sc->sc_maxcount * sizeof(struct pfsync_state_del)) +
1200 sizeof(struct pfsync_header);
1202 case PFSYNC_ACT_UREQ:
1203 len = (sc->sc_maxcount * sizeof(struct pfsync_state_upd_req)) +
1204 sizeof(struct pfsync_header);
1206 case PFSYNC_ACT_BUS:
1207 len = sizeof(struct pfsync_header) +
1208 sizeof(struct pfsync_state_bus);
1210 case PFSYNC_ACT_TDB_UPD:
1211 len = (sc->sc_maxcount * sizeof(struct pfsync_tdb)) +
1212 sizeof(struct pfsync_header);
1215 len = (sc->sc_maxcount * sizeof(struct pfsync_state)) +
1216 sizeof(struct pfsync_header);
1221 MCLGET(m, M_WAITOK);
1222 if ((m->m_flags & M_EXT) == 0) {
1224 IFNET_STAT_INC(&sc->sc_if, oerrors, 1);
1227 m->m_data += (MCLBYTES - len) &~ (sizeof(long) - 1);
1231 m->m_pkthdr.rcvif = NULL;
1232 m->m_pkthdr.len = m->m_len = sizeof(struct pfsync_header);
1233 h = mtod(m, struct pfsync_header *);
1234 h->version = PFSYNC_VERSION;
1239 *sp = (void *)((char *)h + PFSYNC_HDRLEN);
1240 lwkt_reltoken(&pf_token);
1241 callout_reset(&sc->sc_tmo, hz, pfsync_timeout,
1242 LIST_FIRST(&pfsync_list));
1243 lwkt_gettoken(&pf_token);
1248 pfsync_pack_state(u_int8_t action, struct pf_state *st, int flags)
1250 struct ifnet *ifp = NULL;
1251 struct pfsync_softc *sc = pfsyncif;
1252 struct pfsync_header *h, *h_net;
1253 struct pfsync_state *sp = NULL;
1254 struct pfsync_state_upd *up = NULL;
1255 struct pfsync_state_del *dp = NULL;
1257 u_int8_t i = 255, newaction = 0;
1264 * If a packet falls in the forest and there's nobody around to
1265 * hear, does it make a sound?
1267 if (ifp->if_bpf == NULL && sc->sc_sync_ifp == NULL &&
1268 sc->sc_sync_peer.s_addr == INADDR_PFSYNC_GROUP) {
1269 /* Don't leave any stale pfsync packets hanging around. */
1270 if (sc->sc_mbuf != NULL) {
1271 m_freem(sc->sc_mbuf);
1273 sc->sc_statep.s = NULL;
1278 if (action >= PFSYNC_ACT_MAX)
1282 if (sc->sc_mbuf == NULL) {
1283 if ((sc->sc_mbuf = pfsync_get_mbuf(sc, action,
1284 (void *)&sc->sc_statep.s)) == NULL) {
1288 h = mtod(sc->sc_mbuf, struct pfsync_header *);
1290 h = mtod(sc->sc_mbuf, struct pfsync_header *);
1291 if (h->action != action) {
1293 if ((sc->sc_mbuf = pfsync_get_mbuf(sc, action,
1294 (void *)&sc->sc_statep.s)) == NULL) {
1298 h = mtod(sc->sc_mbuf, struct pfsync_header *);
1301 * If it's an update, look in the packet to see if
1302 * we already have an update for the state.
1304 if (action == PFSYNC_ACT_UPD && sc->sc_maxupdates) {
1305 struct pfsync_state *usp =
1306 (void *)((char *)h + PFSYNC_HDRLEN);
1308 for (i = 0; i < h->count; i++) {
1309 if (!memcmp(usp->id, &st->id,
1311 usp->creatorid == st->creatorid) {
1322 st->pfsync_time = mycpu->gd_time_seconds;
1325 /* not a "duplicate" update */
1327 sp = sc->sc_statep.s++;
1328 sc->sc_mbuf->m_pkthdr.len =
1329 sc->sc_mbuf->m_len += sizeof(struct pfsync_state);
1331 bzero(sp, sizeof(*sp));
1333 pfsync_state_export(sp, st);
1335 if (flags & PFSYNC_FLAG_STALE)
1336 sp->sync_flags |= PFSTATE_STALE;
1338 pf_state_peer_hton(&st->src, &sp->src);
1339 pf_state_peer_hton(&st->dst, &sp->dst);
1341 if (st->expire <= time_second)
1342 sp->expire = htonl(0);
1344 sp->expire = htonl(st->expire - time_second);
1347 /* do we need to build "compressed" actions for network transfer? */
1348 if (sc->sc_sync_ifp && flags & PFSYNC_FLAG_COMPRESS) {
1350 case PFSYNC_ACT_UPD:
1351 newaction = PFSYNC_ACT_UPD_C;
1353 case PFSYNC_ACT_DEL:
1354 newaction = PFSYNC_ACT_DEL_C;
1357 /* by default we just send the uncompressed states */
1363 if (sc->sc_mbuf_net == NULL) {
1364 if ((sc->sc_mbuf_net = pfsync_get_mbuf(sc, newaction,
1365 (void *)&sc->sc_statep_net.s)) == NULL) {
1370 h_net = mtod(sc->sc_mbuf_net, struct pfsync_header *);
1372 switch (newaction) {
1373 case PFSYNC_ACT_UPD_C:
1375 up = (void *)((char *)h_net +
1376 PFSYNC_HDRLEN + (i * sizeof(*up)));
1380 sc->sc_mbuf_net->m_pkthdr.len =
1381 sc->sc_mbuf_net->m_len += sizeof(*up);
1382 up = sc->sc_statep_net.u++;
1384 bzero(up, sizeof(*up));
1385 bcopy(&st->id, up->id, sizeof(up->id));
1386 up->creatorid = st->creatorid;
1388 up->timeout = st->timeout;
1389 up->expire = sp->expire;
1393 case PFSYNC_ACT_DEL_C:
1394 sc->sc_mbuf_net->m_pkthdr.len =
1395 sc->sc_mbuf_net->m_len += sizeof(*dp);
1396 dp = sc->sc_statep_net.d++;
1399 bzero(dp, sizeof(*dp));
1400 bcopy(&st->id, dp->id, sizeof(dp->id));
1401 dp->creatorid = st->creatorid;
1406 if (h->count == sc->sc_maxcount ||
1407 (sc->sc_maxupdates && (sp->updates >= sc->sc_maxupdates)))
1408 ret = pfsync_sendout(sc);
1415 pfsync_request_update(struct pfsync_state_upd *up, struct in_addr *src)
1417 struct pfsync_header *h;
1418 struct pfsync_softc *sc = pfsyncif;
1419 struct pfsync_state_upd_req *rup;
1425 if (sc->sc_mbuf == NULL) {
1426 if ((sc->sc_mbuf = pfsync_get_mbuf(sc, PFSYNC_ACT_UREQ,
1427 (void *)&sc->sc_statep.s)) == NULL)
1429 h = mtod(sc->sc_mbuf, struct pfsync_header *);
1431 h = mtod(sc->sc_mbuf, struct pfsync_header *);
1432 if (h->action != PFSYNC_ACT_UREQ) {
1434 if ((sc->sc_mbuf = pfsync_get_mbuf(sc, PFSYNC_ACT_UREQ,
1435 (void *)&sc->sc_statep.s)) == NULL)
1437 h = mtod(sc->sc_mbuf, struct pfsync_header *);
1442 sc->sc_sendaddr = *src;
1443 sc->sc_mbuf->m_pkthdr.len = sc->sc_mbuf->m_len += sizeof(*rup);
1445 rup = sc->sc_statep.r++;
1446 bzero(rup, sizeof(*rup));
1448 bcopy(up->id, rup->id, sizeof(rup->id));
1449 rup->creatorid = up->creatorid;
1452 if (h->count == sc->sc_maxcount)
1453 ret = pfsync_sendout(sc);
1459 pfsync_clear_states(u_int32_t creatorid, char *ifname)
1461 struct pfsync_softc *sc = pfsyncif;
1462 struct pfsync_state_clr *cp;
1469 if (sc->sc_mbuf != NULL)
1471 if ((sc->sc_mbuf = pfsync_get_mbuf(sc, PFSYNC_ACT_CLR,
1472 (void *)&sc->sc_statep.c)) == NULL) {
1476 sc->sc_mbuf->m_pkthdr.len = sc->sc_mbuf->m_len += sizeof(*cp);
1477 cp = sc->sc_statep.c;
1478 cp->creatorid = creatorid;
1480 strlcpy(cp->ifname, ifname, IFNAMSIZ);
1482 ret = (pfsync_sendout(sc));
1488 pfsync_timeout(void *v)
1490 struct pfsync_softc *sc = v;
1498 pfsync_send_bus(struct pfsync_softc *sc, u_int8_t status)
1500 struct pfsync_state_bus *bus;
1502 if (sc->sc_mbuf != NULL)
1505 if (pfsync_sync_ok &&
1506 (sc->sc_mbuf = pfsync_get_mbuf(sc, PFSYNC_ACT_BUS,
1507 (void *)&sc->sc_statep.b)) != NULL) {
1508 sc->sc_mbuf->m_pkthdr.len = sc->sc_mbuf->m_len += sizeof(*bus);
1509 bus = sc->sc_statep.b;
1510 bus->creatorid = pf_status.hostid;
1511 bus->status = status;
1512 bus->endtime = htonl(mycpu->gd_time_seconds - sc->sc_ureq_received);
1518 pfsync_bulk_update(void *v)
1520 struct pfsync_softc *sc = v;
1522 struct pf_state *state;
1524 ASSERT_LWKT_TOKEN_HELD(&pf_token);
1527 if (sc->sc_mbuf != NULL)
1531 * Grab at most PFSYNC_BULKPACKETS worth of states which have not
1532 * been sent since the latest request was made.
1534 state = sc->sc_bulk_send_next;
1537 /* send state update if syncable and not already sent */
1538 if (!state->sync_flags
1539 && state->timeout < PFTM_MAX
1540 && state->pfsync_time <= sc->sc_ureq_received) {
1541 pfsync_pack_state(PFSYNC_ACT_UPD, state, 0);
1545 /* figure next state to send */
1546 state = TAILQ_NEXT(state, entry_list);
1548 /* wrap to start of list if we hit the end */
1550 state = TAILQ_FIRST(&state_list);
1551 } while (i < sc->sc_maxcount * PFSYNC_BULKPACKETS &&
1552 state != sc->sc_bulk_terminator);
1554 if (!state || state == sc->sc_bulk_terminator) {
1556 pfsync_send_bus(sc, PFSYNC_BUS_END);
1557 sc->sc_ureq_received = 0;
1558 sc->sc_bulk_send_next = NULL;
1559 sc->sc_bulk_terminator = NULL;
1560 lwkt_reltoken(&pf_token);
1561 callout_stop(&sc->sc_bulk_tmo);
1562 lwkt_gettoken(&pf_token);
1563 if (pf_status.debug >= PF_DEBUG_MISC)
1564 kprintf("pfsync: bulk update complete\n");
1566 /* look again for more in a bit */
1567 lwkt_reltoken(&pf_token);
1568 callout_reset(&sc->sc_bulk_tmo, 1, pfsync_timeout,
1569 LIST_FIRST(&pfsync_list));
1570 lwkt_gettoken(&pf_token);
1571 sc->sc_bulk_send_next = state;
1573 if (sc->sc_mbuf != NULL)
1579 pfsync_bulkfail(void *v)
1581 struct pfsync_softc *sc = v;
1584 ASSERT_LWKT_TOKEN_HELD(&pf_token);
1586 if (sc->sc_bulk_tries++ < PFSYNC_MAX_BULKTRIES) {
1587 /* Try again in a bit */
1588 lwkt_reltoken(&pf_token);
1589 callout_reset(&sc->sc_bulkfail_tmo, 5 * hz, pfsync_bulkfail,
1590 LIST_FIRST(&pfsync_list));
1591 lwkt_gettoken(&pf_token);
1593 error = pfsync_request_update(NULL, NULL);
1594 if (error == ENOMEM) {
1595 if (pf_status.debug >= PF_DEBUG_MISC)
1596 kprintf("pfsync: cannot allocate mbufs for "
1602 /* Pretend like the transfer was ok */
1603 sc->sc_ureq_sent = 0;
1604 sc->sc_bulk_tries = 0;
1606 if (!pfsync_sync_ok)
1607 carp_group_demote_adj(&sc->sc_if, -1);
1610 if (pf_status.debug >= PF_DEBUG_MISC)
1611 kprintf("pfsync: failed to receive "
1612 "bulk update status\n");
1613 lwkt_reltoken(&pf_token);
1614 callout_stop(&sc->sc_bulkfail_tmo);
1615 lwkt_gettoken(&pf_token);
1619 /* This must be called in splnet() */
1621 pfsync_sendout(struct pfsync_softc *sc)
1624 struct ifnet *ifp = &sc->sc_if;
1628 ASSERT_LWKT_TOKEN_HELD(&pf_token);
1630 lwkt_reltoken(&pf_token);
1631 callout_stop(&sc->sc_tmo);
1632 lwkt_gettoken(&pf_token);
1634 if (sc->sc_mbuf == NULL)
1638 sc->sc_statep.s = NULL;
1644 bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_OUT);
1649 if (sc->sc_mbuf_net) {
1651 m = sc->sc_mbuf_net;
1652 sc->sc_mbuf_net = NULL;
1653 sc->sc_statep_net.s = NULL;
1656 return pfsync_sendout_mbuf(sc, m);
1660 pfsync_sendout_mbuf(struct pfsync_softc *sc, struct mbuf *m)
1665 if (sc->sc_sync_ifp ||
1666 sc->sc_sync_peer.s_addr != INADDR_PFSYNC_GROUP) {
1667 M_PREPEND(m, sizeof(struct ip), M_WAITOK);
1669 pfsyncstats.pfsyncs_onomem++;
1672 ip = mtod(m, struct ip *);
1673 ip->ip_v = IPVERSION;
1674 ip->ip_hl = sizeof(*ip) >> 2;
1675 ip->ip_tos = IPTOS_LOWDELAY;
1676 ip->ip_len = htons(m->m_pkthdr.len);
1677 ip->ip_id = htons(ip_randomid());
1678 ip->ip_off = htons(IP_DF);
1679 ip->ip_ttl = PFSYNC_DFLTTL;
1680 ip->ip_p = IPPROTO_PFSYNC;
1683 bzero(&sa, sizeof(sa));
1684 ip->ip_src.s_addr = INADDR_ANY;
1686 if (sc->sc_sendaddr.s_addr == INADDR_PFSYNC_GROUP)
1687 m->m_flags |= M_MCAST;
1688 ip->ip_dst = sc->sc_sendaddr;
1689 sc->sc_sendaddr.s_addr = sc->sc_sync_peer.s_addr;
1691 pfsyncstats.pfsyncs_opackets++;
1693 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL))
1694 pfsyncstats.pfsyncs_oerrors++;
1702 pfsync_modevent(module_t mod, int type, void *data)
1706 struct pfsync_softc *pfs_if, *tmp;
1708 lwkt_gettoken(&pf_token);
1712 LIST_INIT(&pfsync_list);
1713 lwkt_reltoken(&pf_token);
1714 if_clone_attach(&pfsync_cloner);
1715 lwkt_gettoken(&pf_token);
1716 /* Override the function pointer for pf_ioctl.c */
1720 lwkt_reltoken(&pf_token);
1721 if_clone_detach(&pfsync_cloner);
1722 lwkt_gettoken(&pf_token);
1723 LIST_FOREACH_MUTABLE(pfs_if, &pfsync_list, sc_next, tmp) {
1724 pfsync_clone_destroy(&pfs_if->sc_if);
1733 lwkt_reltoken(&pf_token);
1737 static moduledata_t pfsync_mod = {
1743 #define PFSYNC_MODVER 44
1745 DECLARE_MODULE(pfsync, pfsync_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
1746 MODULE_VERSION(pfsync, PFSYNC_MODVER);
1749 pfsync_in_addmulti_dispatch(netmsg_t nmsg)
1751 struct lwkt_msg *lmsg = &nmsg->lmsg;
1752 struct ifnet *ifp = lmsg->u.ms_resultp;
1753 struct in_addr addr;
1755 addr.s_addr = INADDR_PFSYNC_GROUP;
1756 lmsg->u.ms_resultp = in_addmulti(&addr, ifp);
1758 lwkt_replymsg(lmsg, 0);
1761 static struct in_multi *
1762 pfsync_in_addmulti(struct ifnet *ifp)
1764 struct netmsg_base nmsg;
1765 struct lwkt_msg *lmsg = &nmsg.lmsg;
1767 netmsg_init(&nmsg, NULL, &curthread->td_msgport, 0,
1768 pfsync_in_addmulti_dispatch);
1769 lmsg->u.ms_resultp = ifp;
1771 lwkt_domsg(netisr_cpuport(0), lmsg, 0);
1772 return lmsg->u.ms_resultp;
1776 pfsync_in_delmulti_dispatch(netmsg_t nmsg)
1778 struct lwkt_msg *lmsg = &nmsg->lmsg;
1780 in_delmulti(lmsg->u.ms_resultp);
1781 lwkt_replymsg(lmsg, 0);
1785 pfsync_in_delmulti(struct in_multi *inm)
1787 struct netmsg_base nmsg;
1788 struct lwkt_msg *lmsg = &nmsg.lmsg;
1790 netmsg_init(&nmsg, NULL, &curthread->td_msgport, 0,
1791 pfsync_in_delmulti_dispatch);
1792 lmsg->u.ms_resultp = inm;
1794 lwkt_domsg(netisr_cpuport(0), lmsg, 0);