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/sockio.h>
45 #include <sys/thread2.h>
47 #include <machine/inttypes.h>
50 #include <net/if_types.h>
51 #include <net/route.h>
53 #include <netinet/in.h>
54 #include <netinet/if_ether.h>
55 #include <netinet/ip_carp.h>
56 #include <netinet/tcp.h>
57 #include <netinet/tcp_seq.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/in_var.h>
62 #include <netinet/ip.h>
63 #include <netinet/ip_var.h>
67 #include <netinet6/nd6.h>
70 #include <net/pf/pfvar.h>
71 #include <net/pf/if_pfsync.h>
73 #define PFSYNCNAME "pfsync"
75 #define PFSYNC_MINMTU \
76 (sizeof(struct pfsync_header) + sizeof(struct pf_state))
79 #define DPRINTF(x) do { if (pfsyncdebug) kprintf x ; } while (0)
85 struct pfsync_softc *pfsyncif = NULL;
86 struct pfsyncstats pfsyncstats;
88 void pfsyncattach(int);
89 static int pfsync_clone_destroy(struct ifnet *);
90 static int pfsync_clone_create(struct if_clone *, int, caddr_t);
91 void pfsync_setmtu(struct pfsync_softc *, int);
92 int pfsync_alloc_scrub_memory(struct pfsync_state_peer *,
93 struct pf_state_peer *);
94 int pfsyncoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
96 int pfsyncioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
97 void pfsyncstart(struct ifnet *);
99 struct mbuf *pfsync_get_mbuf(struct pfsync_softc *, u_int8_t, void **);
100 int pfsync_request_update(struct pfsync_state_upd *, struct in_addr *);
101 int pfsync_sendout(struct pfsync_softc *);
102 int pfsync_sendout_mbuf(struct pfsync_softc *, struct mbuf *);
103 void pfsync_timeout(void *);
104 void pfsync_send_bus(struct pfsync_softc *, u_int8_t);
105 void pfsync_bulk_update(void *);
106 void pfsync_bulkfail(void *);
108 static MALLOC_DEFINE(M_PFSYNC, PFSYNCNAME, "Packet Filter State Sync. Interface");
109 static LIST_HEAD(pfsync_list, pfsync_softc) pfsync_list;
113 struct if_clone pfsync_cloner =
114 IF_CLONE_INITIALIZER("pfsync", pfsync_clone_create, pfsync_clone_destroy, 1 ,1);
117 pfsyncattach(int npfsync)
119 if_clone_attach(&pfsync_cloner);
122 pfsync_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused)
124 struct pfsync_softc *sc;
127 lwkt_gettoken(&pf_token);
129 sc = kmalloc(sizeof(*sc), M_PFSYNC, M_WAITOK | M_ZERO);
132 sc->sc_mbuf_net = NULL;
133 sc->sc_mbuf_tdb = NULL;
134 sc->sc_statep.s = NULL;
135 sc->sc_statep_net.s = NULL;
136 sc->sc_statep_tdb.t = NULL;
137 sc->sc_maxupdates = 128;
138 sc->sc_sync_peer.s_addr =htonl(INADDR_PFSYNC_GROUP);
139 sc->sc_sendaddr.s_addr = htonl(INADDR_PFSYNC_GROUP);
140 sc->sc_ureq_received = 0;
141 sc->sc_ureq_sent = 0;
142 sc->sc_bulk_send_next = NULL;
143 sc->sc_bulk_terminator = NULL;
144 sc->sc_imo.imo_max_memberships = IP_MAX_MEMBERSHIPS;
145 lwkt_reltoken(&pf_token);
147 ksnprintf(ifp->if_xname, sizeof ifp->if_xname, "pfsync%d", unit);
148 if_initname(ifp, ifc->ifc_name, unit);
149 ifp->if_ioctl = pfsyncioctl;
150 ifp->if_output = pfsyncoutput;
151 ifp->if_start = pfsyncstart;
152 ifp->if_type = IFT_PFSYNC;
153 ifp->if_snd.ifq_maxlen = ifqmaxlen;
154 ifp->if_hdrlen = PFSYNC_HDRLEN;
155 ifp->if_baudrate = IF_Mbps(100);
157 pfsync_setmtu(sc, MCLBYTES);
158 callout_init(&sc->sc_tmo);
159 /* callout_init(&sc->sc_tdb_tmo); XXX we don't support tdb (yet) */
160 callout_init(&sc->sc_bulk_tmo);
161 callout_init(&sc->sc_bulkfail_tmo);
162 if_attach(ifp, NULL);
164 LIST_INSERT_HEAD(&pfsync_list, sc, sc_next);
168 if_addgroup(ifp, "carp");
172 bpfattach(&sc->sc_if, DLT_PFSYNC, PFSYNC_HDRLEN);
174 lwkt_gettoken(&pf_token);
176 lwkt_reltoken(&pf_token);
181 pfsync_clone_destroy(struct ifnet *ifp)
183 lwkt_gettoken(&pf_token);
184 lwkt_reltoken(&pf_token);
186 struct pfsync_softc *sc = ifp->if_softc;
187 callout_stop(&sc->sc_tmo);
188 /* callout_stop(&sc->sc_tdb_tmo); XXX we don't support tdb (yet) */
189 callout_stop(&sc->sc_bulk_tmo);
190 callout_stop(&sc->sc_bulkfail_tmo);
193 carp_group_demote_adj(&sc->sc_if, -1);
199 lwkt_gettoken(&pf_token);
200 LIST_REMOVE(sc, sc_next);
202 lwkt_reltoken(&pf_token);
209 * Start output on the pfsync interface.
212 pfsyncstart(struct ifnet *ifp)
215 IF_DROP(&ifp->if_snd);
216 IF_DRAIN(&ifp->if_snd);
221 pfsync_alloc_scrub_memory(struct pfsync_state_peer *s,
222 struct pf_state_peer *d)
224 if (s->scrub.scrub_flag && d->scrub == NULL) {
225 d->scrub = kmalloc(sizeof(struct pf_state_scrub), M_PFSYNC, M_NOWAIT|M_ZERO);
227 if (d->scrub == NULL)
235 pfsync_state_export(struct pfsync_state *sp, struct pf_state *st)
237 bzero(sp, sizeof(struct pfsync_state));
239 /* copy from state key */
240 sp->key[PF_SK_WIRE].addr[0] = st->key[PF_SK_WIRE]->addr[0];
241 sp->key[PF_SK_WIRE].addr[1] = st->key[PF_SK_WIRE]->addr[1];
242 sp->key[PF_SK_WIRE].port[0] = st->key[PF_SK_WIRE]->port[0];
243 sp->key[PF_SK_WIRE].port[1] = st->key[PF_SK_WIRE]->port[1];
244 sp->key[PF_SK_STACK].addr[0] = st->key[PF_SK_STACK]->addr[0];
245 sp->key[PF_SK_STACK].addr[1] = st->key[PF_SK_STACK]->addr[1];
246 sp->key[PF_SK_STACK].port[0] = st->key[PF_SK_STACK]->port[0];
247 sp->key[PF_SK_STACK].port[1] = st->key[PF_SK_STACK]->port[1];
248 sp->proto = st->key[PF_SK_WIRE]->proto;
249 sp->af = st->key[PF_SK_WIRE]->af;
251 /* copy from state */
252 strlcpy(sp->ifname, st->kif->pfik_name, sizeof(sp->ifname));
253 bcopy(&st->rt_addr, &sp->rt_addr, sizeof(sp->rt_addr));
254 sp->creation = htonl(time_second - st->creation);
255 sp->expire = pf_state_expires(st);
256 if (sp->expire <= time_second)
257 sp->expire = htonl(0);
259 sp->expire = htonl(sp->expire - time_second);
261 sp->direction = st->direction;
263 sp->timeout = st->timeout;
264 sp->state_flags = st->state_flags;
266 sp->sync_flags |= PFSYNC_FLAG_SRCNODE;
267 if (st->nat_src_node)
268 sp->sync_flags |= PFSYNC_FLAG_NATSRCNODE;
270 bcopy(&st->id, &sp->id, sizeof(sp->id));
271 sp->creatorid = st->creatorid;
272 pf_state_peer_hton(&st->src, &sp->src);
273 pf_state_peer_hton(&st->dst, &sp->dst);
275 if (st->rule.ptr == NULL)
276 sp->rule = htonl(-1);
278 sp->rule = htonl(st->rule.ptr->nr);
279 if (st->anchor.ptr == NULL)
280 sp->anchor = htonl(-1);
282 sp->anchor = htonl(st->anchor.ptr->nr);
283 if (st->nat_rule.ptr == NULL)
284 sp->nat_rule = htonl(-1);
286 sp->nat_rule = htonl(st->nat_rule.ptr->nr);
288 pf_state_counter_hton(st->packets[0], sp->packets[0]);
289 pf_state_counter_hton(st->packets[1], sp->packets[1]);
290 pf_state_counter_hton(st->bytes[0], sp->bytes[0]);
291 pf_state_counter_hton(st->bytes[1], sp->bytes[1]);
296 pfsync_state_import(struct pfsync_state *sp, u_int8_t flags)
298 struct pf_state *st = NULL;
299 struct pf_state_key *skw = NULL, *sks = NULL;
300 struct pf_rule *r = NULL;
305 if (sp->creatorid == 0 && pf_status.debug >= PF_DEBUG_MISC) {
306 kprintf("pfsync_insert_net_state: invalid creator id:"
307 " %08x\n", ntohl(sp->creatorid));
311 if ((kif = pfi_kif_get(sp->ifname)) == NULL) {
312 if (pf_status.debug >= PF_DEBUG_MISC)
313 kprintf("pfsync_insert_net_state: "
314 "unknown interface: %s\n", sp->ifname);
315 if (flags & PFSYNC_SI_IOCTL)
317 return (0); /* skip this state */
321 * If the ruleset checksums match or the state is coming from the ioctl,
322 * it's safe to associate the state with the rule of that number.
324 if (sp->rule != htonl(-1) && sp->anchor == htonl(-1) &&
325 (flags & (PFSYNC_SI_IOCTL | PFSYNC_SI_CKSUM)) && ntohl(sp->rule) <
326 pf_main_ruleset.rules[PF_RULESET_FILTER].active.rcount)
327 r = pf_main_ruleset.rules[
328 PF_RULESET_FILTER].active.ptr_array[ntohl(sp->rule)];
330 r = &pf_default_rule;
332 if ((r->max_states && r->states_cur >= r->max_states))
335 if (flags & PFSYNC_SI_IOCTL)
336 pool_flags = M_WAITOK | M_NULLOK | M_ZERO;
338 pool_flags = M_WAITOK | M_ZERO;
340 if ((st = kmalloc(sizeof(struct pf_state), M_PFSYNC, pool_flags)) == NULL)
343 if ((skw = pf_alloc_state_key(pool_flags)) == NULL)
346 if (PF_ANEQ(&sp->key[PF_SK_WIRE].addr[0],
347 &sp->key[PF_SK_STACK].addr[0], sp->af) ||
348 PF_ANEQ(&sp->key[PF_SK_WIRE].addr[1],
349 &sp->key[PF_SK_STACK].addr[1], sp->af) ||
350 sp->key[PF_SK_WIRE].port[0] != sp->key[PF_SK_STACK].port[0] ||
351 sp->key[PF_SK_WIRE].port[1] != sp->key[PF_SK_STACK].port[1]) {
352 if ((sks = pf_alloc_state_key(pool_flags)) == NULL)
357 /* allocate memory for scrub info */
358 if (pfsync_alloc_scrub_memory(&sp->src, &st->src) ||
359 pfsync_alloc_scrub_memory(&sp->dst, &st->dst))
362 /* copy to state key(s) */
363 skw->addr[0] = sp->key[PF_SK_WIRE].addr[0];
364 skw->addr[1] = sp->key[PF_SK_WIRE].addr[1];
365 skw->port[0] = sp->key[PF_SK_WIRE].port[0];
366 skw->port[1] = sp->key[PF_SK_WIRE].port[1];
367 skw->proto = sp->proto;
370 sks->addr[0] = sp->key[PF_SK_STACK].addr[0];
371 sks->addr[1] = sp->key[PF_SK_STACK].addr[1];
372 sks->port[0] = sp->key[PF_SK_STACK].port[0];
373 sks->port[1] = sp->key[PF_SK_STACK].port[1];
374 sks->proto = sp->proto;
379 bcopy(&sp->rt_addr, &st->rt_addr, sizeof(st->rt_addr));
380 st->creation = time_second - ntohl(sp->creation);
381 st->expire = time_second;
383 /* XXX No adaptive scaling. */
384 st->expire -= r->timeout[sp->timeout] - ntohl(sp->expire);
387 st->expire = ntohl(sp->expire) + time_second;
388 st->direction = sp->direction;
390 st->timeout = sp->timeout;
391 st->state_flags = sp->state_flags;
392 if (!(flags & PFSYNC_SI_IOCTL))
393 st->sync_flags = PFSTATE_FROMSYNC;
395 bcopy(sp->id, &st->id, sizeof(st->id));
396 st->creatorid = sp->creatorid;
397 pf_state_peer_ntoh(&sp->src, &st->src);
398 pf_state_peer_ntoh(&sp->dst, &st->dst);
401 st->nat_rule.ptr = NULL;
402 st->anchor.ptr = NULL;
408 /* XXX when we have nat_rule/anchors, use STATE_INC_COUNTERS */
412 if ((error = pf_state_insert(kif, skw, sks, st)) != 0) {
413 /* XXX when we have nat_rule/anchors, use STATE_DEC_COUNTERS */
425 kfree(skw, M_PFSYNC);
427 kfree(sks, M_PFSYNC);
429 cleanup_state: /* pf_state_insert frees the state keys */
432 kfree(st->dst.scrub, M_PFSYNC);
434 kfree(st->src.scrub, M_PFSYNC);
441 pfsync_input(struct mbuf *m, ...)
443 struct ip *ip = mtod(m, struct ip *);
444 struct pfsync_header *ph;
445 struct pfsync_softc *sc = pfsyncif;
447 struct pf_state_key *sk;
448 struct pf_state_item *si;
449 struct pf_state_cmp id_key;
450 struct pfsync_state *sp;
451 struct pfsync_state_upd *up;
452 struct pfsync_state_del *dp;
453 struct pfsync_state_clr *cp;
454 struct pfsync_state_upd_req *rup;
455 struct pfsync_state_bus *bus;
457 struct pfsync_tdb *pt;
461 int iplen, action, error, i, count, offp, sfail, stale = 0;
464 /* This function is not yet called from anywhere */
465 /* Still we assume for safety that pf_token must be held */
466 ASSERT_LWKT_TOKEN_HELD(&pf_token);
468 pfsyncstats.pfsyncs_ipackets++;
470 /* verify that we have a sync interface configured */
471 if (!sc || !sc->sc_sync_ifp || !pf_status.running)
474 /* verify that the packet came in on the right interface */
475 if (sc->sc_sync_ifp != m->m_pkthdr.rcvif) {
476 pfsyncstats.pfsyncs_badif++;
480 /* verify that the IP TTL is 255. */
481 if (ip->ip_ttl != PFSYNC_DFLTTL) {
482 pfsyncstats.pfsyncs_badttl++;
486 iplen = ip->ip_hl << 2;
488 if (m->m_pkthdr.len < iplen + sizeof(*ph)) {
489 pfsyncstats.pfsyncs_hdrops++;
493 if (iplen + sizeof(*ph) > m->m_len) {
494 if ((m = m_pullup(m, iplen + sizeof(*ph))) == NULL) {
495 pfsyncstats.pfsyncs_hdrops++;
498 ip = mtod(m, struct ip *);
500 ph = (struct pfsync_header *)((char *)ip + iplen);
502 /* verify the version */
503 if (ph->version != PFSYNC_VERSION) {
504 pfsyncstats.pfsyncs_badver++;
511 /* make sure it's a valid action code */
512 if (action >= PFSYNC_ACT_MAX) {
513 pfsyncstats.pfsyncs_badact++;
517 /* Cheaper to grab this now than having to mess with mbufs later */
520 if (!bcmp(&ph->pf_chksum, &pf_status.pf_chksum, PF_MD5_DIGEST_LENGTH))
521 flags |= PFSYNC_SI_CKSUM;
524 case PFSYNC_ACT_CLR: {
525 struct pf_state *nexts;
526 struct pf_state_key *nextsk;
529 if ((mp = m_pulldown(m, iplen + sizeof(*ph),
530 sizeof(*cp), &offp)) == NULL) {
531 pfsyncstats.pfsyncs_badlen++;
534 cp = (struct pfsync_state_clr *)(mp->m_data + offp);
535 creatorid = cp->creatorid;
538 if (cp->ifname[0] == '\0') {
539 for (st = RB_MIN(pf_state_tree_id, &tree_id);
541 nexts = RB_NEXT(pf_state_tree_id, &tree_id, st);
542 if (st->creatorid == creatorid) {
543 st->sync_flags |= PFSTATE_FROMSYNC;
548 if ((kif = pfi_kif_get(cp->ifname)) == NULL) {
553 for (sk = RB_MIN(pf_state_tree,
554 &pf_statetbl); sk; sk = nextsk) {
555 nextsk = RB_NEXT(pf_state_tree,
557 TAILQ_FOREACH(si, &sk->states, entry) {
558 if (si->s->creatorid == creatorid) {
561 pf_unlink_state(si->s);
571 if ((mp = m_pulldown(m, iplen + sizeof(*ph),
572 count * sizeof(*sp), &offp)) == NULL) {
573 pfsyncstats.pfsyncs_badlen++;
578 for (i = 0, sp = (struct pfsync_state *)(mp->m_data + offp);
579 i < count; i++, sp++) {
580 /* check for invalid values */
581 if (sp->timeout >= PFTM_MAX ||
582 sp->src.state > PF_TCPS_PROXY_DST ||
583 sp->dst.state > PF_TCPS_PROXY_DST ||
584 sp->direction > PF_OUT ||
585 (sp->af != AF_INET && sp->af != AF_INET6)) {
586 if (pf_status.debug >= PF_DEBUG_MISC)
587 kprintf("pfsync_insert: PFSYNC_ACT_INS: "
589 pfsyncstats.pfsyncs_badval++;
593 if ((error = pfsync_state_import(sp, flags))) {
594 if (error == ENOMEM) {
604 if ((mp = m_pulldown(m, iplen + sizeof(*ph),
605 count * sizeof(*sp), &offp)) == NULL) {
606 pfsyncstats.pfsyncs_badlen++;
611 for (i = 0, sp = (struct pfsync_state *)(mp->m_data + offp);
612 i < count; i++, sp++) {
613 int flags = PFSYNC_FLAG_STALE;
615 /* check for invalid values */
616 if (sp->timeout >= PFTM_MAX ||
617 sp->src.state > PF_TCPS_PROXY_DST ||
618 sp->dst.state > PF_TCPS_PROXY_DST) {
619 if (pf_status.debug >= PF_DEBUG_MISC)
620 kprintf("pfsync_insert: PFSYNC_ACT_UPD: "
622 pfsyncstats.pfsyncs_badval++;
626 bcopy(sp->id, &id_key.id, sizeof(id_key.id));
627 id_key.creatorid = sp->creatorid;
629 st = pf_find_state_byid(&id_key);
631 /* insert the update */
632 if (pfsync_state_import(sp, flags))
633 pfsyncstats.pfsyncs_badstate++;
636 sk = st->key[PF_SK_WIRE]; /* XXX right one? */
638 if (sk->proto == IPPROTO_TCP) {
640 * The state should never go backwards except
641 * for syn-proxy states. Neither should the
642 * sequence window slide backwards.
644 if (st->src.state > sp->src.state &&
645 (st->src.state < PF_TCPS_PROXY_SRC ||
646 sp->src.state >= PF_TCPS_PROXY_SRC))
648 else if (SEQ_GT(st->src.seqlo,
649 ntohl(sp->src.seqlo)))
651 else if (st->dst.state > sp->dst.state) {
652 /* There might still be useful
653 * information about the src state here,
654 * so import that part of the update,
655 * then "fail" so we send the updated
656 * state back to the peer who is missing
657 * our what we know. */
658 pf_state_peer_ntoh(&sp->src, &st->src);
659 /* XXX do anything with timeouts? */
662 } else if (st->dst.state >= TCPS_SYN_SENT &&
663 SEQ_GT(st->dst.seqlo, ntohl(sp->dst.seqlo)))
667 * Non-TCP protocol state machine always go
670 if (st->src.state > sp->src.state)
672 else if (st->dst.state > sp->dst.state)
676 if (pf_status.debug >= PF_DEBUG_MISC)
677 kprintf("pfsync: %s stale update "
680 (sfail < 7 ? "ignoring"
682 (uintmax_t)be64toh(st->id),
683 ntohl(st->creatorid));
684 pfsyncstats.pfsyncs_stale++;
686 if (!(sp->sync_flags & PFSTATE_STALE)) {
687 /* we have a better state, send it */
688 if (sc->sc_mbuf != NULL && !stale)
693 PFSYNC_ACT_UPD, st, flags);
697 pfsync_alloc_scrub_memory(&sp->dst, &st->dst);
698 pf_state_peer_ntoh(&sp->src, &st->src);
699 pf_state_peer_ntoh(&sp->dst, &st->dst);
700 st->expire = ntohl(sp->expire) + time_second;
701 st->timeout = sp->timeout;
703 if (stale && sc->sc_mbuf != NULL)
708 * It's not strictly necessary for us to support the "uncompressed"
709 * delete action, but it's relatively simple and maintains consistency.
712 if ((mp = m_pulldown(m, iplen + sizeof(*ph),
713 count * sizeof(*sp), &offp)) == NULL) {
714 pfsyncstats.pfsyncs_badlen++;
719 for (i = 0, sp = (struct pfsync_state *)(mp->m_data + offp);
720 i < count; i++, sp++) {
721 bcopy(sp->id, &id_key.id, sizeof(id_key.id));
722 id_key.creatorid = sp->creatorid;
724 st = pf_find_state_byid(&id_key);
726 pfsyncstats.pfsyncs_badstate++;
729 st->sync_flags |= PFSTATE_FROMSYNC;
734 case PFSYNC_ACT_UPD_C: {
735 int update_requested = 0;
737 if ((mp = m_pulldown(m, iplen + sizeof(*ph),
738 count * sizeof(*up), &offp)) == NULL) {
739 pfsyncstats.pfsyncs_badlen++;
744 for (i = 0, up = (struct pfsync_state_upd *)(mp->m_data + offp);
745 i < count; i++, up++) {
746 /* check for invalid values */
747 if (up->timeout >= PFTM_MAX ||
748 up->src.state > PF_TCPS_PROXY_DST ||
749 up->dst.state > PF_TCPS_PROXY_DST) {
750 if (pf_status.debug >= PF_DEBUG_MISC)
751 kprintf("pfsync_insert: "
754 pfsyncstats.pfsyncs_badval++;
758 bcopy(up->id, &id_key.id, sizeof(id_key.id));
759 id_key.creatorid = up->creatorid;
761 st = pf_find_state_byid(&id_key);
763 /* We don't have this state. Ask for it. */
764 error = pfsync_request_update(up, &src);
765 if (error == ENOMEM) {
769 update_requested = 1;
770 pfsyncstats.pfsyncs_badstate++;
773 sk = st->key[PF_SK_WIRE]; /* XXX right one? */
775 if (sk->proto == IPPROTO_TCP) {
777 * The state should never go backwards except
778 * for syn-proxy states. Neither should the
779 * sequence window slide backwards.
781 if (st->src.state > up->src.state &&
782 (st->src.state < PF_TCPS_PROXY_SRC ||
783 up->src.state >= PF_TCPS_PROXY_SRC))
785 else if (st->dst.state > up->dst.state)
787 else if (SEQ_GT(st->src.seqlo,
788 ntohl(up->src.seqlo)))
790 else if (st->dst.state >= TCPS_SYN_SENT &&
791 SEQ_GT(st->dst.seqlo, ntohl(up->dst.seqlo)))
795 * Non-TCP protocol state machine always go
798 if (st->src.state > up->src.state)
800 else if (st->dst.state > up->dst.state)
804 if (pf_status.debug >= PF_DEBUG_MISC)
805 kprintf("pfsync: ignoring stale update "
806 "(%d) id: %016" PRIx64 " "
807 "creatorid: %08x\n", sfail,
809 ntohl(st->creatorid));
810 pfsyncstats.pfsyncs_stale++;
812 /* we have a better state, send it out */
813 if ((!stale || update_requested) &&
814 sc->sc_mbuf != NULL) {
816 update_requested = 0;
820 pfsync_pack_state(PFSYNC_ACT_UPD, st,
824 pfsync_alloc_scrub_memory(&up->dst, &st->dst);
825 pf_state_peer_ntoh(&up->src, &st->src);
826 pf_state_peer_ntoh(&up->dst, &st->dst);
827 st->expire = ntohl(up->expire) + time_second;
828 st->timeout = up->timeout;
830 if ((update_requested || stale) && sc->sc_mbuf)
835 case PFSYNC_ACT_DEL_C:
836 if ((mp = m_pulldown(m, iplen + sizeof(*ph),
837 count * sizeof(*dp), &offp)) == NULL) {
838 pfsyncstats.pfsyncs_badlen++;
843 for (i = 0, dp = (struct pfsync_state_del *)(mp->m_data + offp);
844 i < count; i++, dp++) {
845 bcopy(dp->id, &id_key.id, sizeof(id_key.id));
846 id_key.creatorid = dp->creatorid;
848 st = pf_find_state_byid(&id_key);
850 pfsyncstats.pfsyncs_badstate++;
853 st->sync_flags |= PFSTATE_FROMSYNC;
858 case PFSYNC_ACT_INS_F:
859 case PFSYNC_ACT_DEL_F:
860 /* not implemented */
862 case PFSYNC_ACT_UREQ:
863 if ((mp = m_pulldown(m, iplen + sizeof(*ph),
864 count * sizeof(*rup), &offp)) == NULL) {
865 pfsyncstats.pfsyncs_badlen++;
870 if (sc->sc_mbuf != NULL)
873 rup = (struct pfsync_state_upd_req *)(mp->m_data + offp);
874 i < count; i++, rup++) {
875 bcopy(rup->id, &id_key.id, sizeof(id_key.id));
876 id_key.creatorid = rup->creatorid;
878 if (id_key.id == 0 && id_key.creatorid == 0) {
879 sc->sc_ureq_received = mycpu->gd_time_seconds;
880 if (sc->sc_bulk_send_next == NULL)
881 sc->sc_bulk_send_next =
882 TAILQ_FIRST(&state_list);
883 sc->sc_bulk_terminator = sc->sc_bulk_send_next;
884 if (pf_status.debug >= PF_DEBUG_MISC)
885 kprintf("pfsync: received "
886 "bulk update request\n");
887 pfsync_send_bus(sc, PFSYNC_BUS_START);
888 lwkt_reltoken(&pf_token);
889 callout_init(&sc->sc_bulk_tmo);
890 lwkt_gettoken(&pf_token);
892 st = pf_find_state_byid(&id_key);
894 pfsyncstats.pfsyncs_badstate++;
898 pfsync_pack_state(PFSYNC_ACT_UPD,
902 if (sc->sc_mbuf != NULL)
907 /* If we're not waiting for a bulk update, who cares. */
908 if (sc->sc_ureq_sent == 0)
911 if ((mp = m_pulldown(m, iplen + sizeof(*ph),
912 sizeof(*bus), &offp)) == NULL) {
913 pfsyncstats.pfsyncs_badlen++;
916 bus = (struct pfsync_state_bus *)(mp->m_data + offp);
917 switch (bus->status) {
918 case PFSYNC_BUS_START:
919 lwkt_reltoken(&pf_token);
920 callout_reset(&sc->sc_bulkfail_tmo,
921 pf_pool_limits[PF_LIMIT_STATES].limit /
922 (PFSYNC_BULKPACKETS * sc->sc_maxcount),
923 pfsync_bulkfail, LIST_FIRST(&pfsync_list));
924 lwkt_gettoken(&pf_token);
925 if (pf_status.debug >= PF_DEBUG_MISC)
926 kprintf("pfsync: received bulk "
930 if (mycpu->gd_time_seconds - ntohl(bus->endtime) >=
932 /* that's it, we're happy */
933 sc->sc_ureq_sent = 0;
934 sc->sc_bulk_tries = 0;
935 lwkt_reltoken(&pf_token);
936 callout_stop(&sc->sc_bulkfail_tmo);
937 lwkt_gettoken(&pf_token);
939 if (!pfsync_sync_ok) {
940 lwkt_reltoken(&pf_token);
941 carp_group_demote_adj(&sc->sc_if, -1);
942 lwkt_gettoken(&pf_token);
946 if (pf_status.debug >= PF_DEBUG_MISC)
947 kprintf("pfsync: received valid "
948 "bulk update end\n");
950 if (pf_status.debug >= PF_DEBUG_MISC)
951 kprintf("pfsync: received invalid "
952 "bulk update end: bad timestamp\n");
958 case PFSYNC_ACT_TDB_UPD:
959 if ((mp = m_pulldown(m, iplen + sizeof(*ph),
960 count * sizeof(*pt), &offp)) == NULL) {
961 pfsyncstats.pfsyncs_badlen++;
965 for (i = 0, pt = (struct pfsync_tdb *)(mp->m_data + offp);
966 i < count; i++, pt++)
967 pfsync_update_net_tdb(pt);
979 pfsyncoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
988 pfsyncioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
990 struct pfsync_softc *sc = ifp->if_softc;
991 struct ifreq *ifr = (struct ifreq *)data;
992 struct ip_moptions *imo = &sc->sc_imo;
993 struct pfsyncreq pfsyncr;
997 lwkt_gettoken(&pf_token);
1002 case SIOCSIFDSTADDR:
1004 if (ifp->if_flags & IFF_UP)
1005 ifp->if_flags |= IFF_RUNNING;
1007 ifp->if_flags &= ~IFF_RUNNING;
1010 if (ifr->ifr_mtu < PFSYNC_MINMTU) {
1011 lwkt_reltoken(&pf_token);
1014 if (ifr->ifr_mtu > MCLBYTES)
1015 ifr->ifr_mtu = MCLBYTES;
1017 if (ifr->ifr_mtu < ifp->if_mtu)
1019 pfsync_setmtu(sc, ifr->ifr_mtu);
1023 bzero(&pfsyncr, sizeof(pfsyncr));
1024 if (sc->sc_sync_ifp)
1025 strlcpy(pfsyncr.pfsyncr_syncdev,
1026 sc->sc_sync_ifp->if_xname, IFNAMSIZ);
1027 pfsyncr.pfsyncr_syncpeer = sc->sc_sync_peer;
1028 pfsyncr.pfsyncr_maxupdates = sc->sc_maxupdates;
1029 lwkt_reltoken(&pf_token);
1030 if ((error = copyout(&pfsyncr, ifr->ifr_data, sizeof(pfsyncr))))
1032 lwkt_gettoken(&pf_token);
1035 if ((error = priv_check_cred(cr, PRIV_ROOT, NULL_CRED_OKAY)) != 0) {
1036 lwkt_reltoken(&pf_token);
1039 if ((error = copyin(ifr->ifr_data, &pfsyncr, sizeof(pfsyncr)))) {
1040 lwkt_reltoken(&pf_token);
1044 if (pfsyncr.pfsyncr_syncpeer.s_addr == 0)
1045 sc->sc_sync_peer.s_addr = INADDR_PFSYNC_GROUP;
1047 sc->sc_sync_peer.s_addr =
1048 pfsyncr.pfsyncr_syncpeer.s_addr;
1050 if (pfsyncr.pfsyncr_maxupdates > 255) {
1051 lwkt_reltoken(&pf_token);
1054 sc->sc_maxupdates = pfsyncr.pfsyncr_maxupdates;
1056 if (pfsyncr.pfsyncr_syncdev[0] == 0) {
1057 sc->sc_sync_ifp = NULL;
1058 if (sc->sc_mbuf_net != NULL) {
1059 /* Don't keep stale pfsync packets around. */
1061 m_freem(sc->sc_mbuf_net);
1062 sc->sc_mbuf_net = NULL;
1063 sc->sc_statep_net.s = NULL;
1066 if (imo->imo_num_memberships > 0) {
1067 in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1068 imo->imo_multicast_ifp = NULL;
1073 if ((sifp = ifunit(pfsyncr.pfsyncr_syncdev)) == NULL) {
1074 lwkt_reltoken(&pf_token);
1079 if (sifp->if_mtu < sc->sc_if.if_mtu ||
1080 (sc->sc_sync_ifp != NULL &&
1081 sifp->if_mtu < sc->sc_sync_ifp->if_mtu) ||
1082 sifp->if_mtu < MCLBYTES - sizeof(struct ip))
1084 sc->sc_sync_ifp = sifp;
1086 pfsync_setmtu(sc, sc->sc_if.if_mtu);
1088 if (imo->imo_num_memberships > 0) {
1089 in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1090 imo->imo_multicast_ifp = NULL;
1093 if (sc->sc_sync_ifp &&
1094 sc->sc_sync_peer.s_addr == INADDR_PFSYNC_GROUP) {
1095 struct in_addr addr;
1097 if (!(sc->sc_sync_ifp->if_flags & IFF_MULTICAST)) {
1098 sc->sc_sync_ifp = NULL;
1099 lwkt_reltoken(&pf_token);
1101 return (EADDRNOTAVAIL);
1104 addr.s_addr = INADDR_PFSYNC_GROUP;
1106 if ((imo->imo_membership[0] =
1107 in_addmulti(&addr, sc->sc_sync_ifp)) == NULL) {
1108 sc->sc_sync_ifp = NULL;
1109 lwkt_reltoken(&pf_token);
1113 imo->imo_num_memberships++;
1114 imo->imo_multicast_ifp = sc->sc_sync_ifp;
1115 imo->imo_multicast_ttl = PFSYNC_DFLTTL;
1116 imo->imo_multicast_loop = 0;
1119 if (sc->sc_sync_ifp ||
1120 sc->sc_sendaddr.s_addr != INADDR_PFSYNC_GROUP) {
1121 /* Request a full state table update. */
1122 sc->sc_ureq_sent = mycpu->gd_time_seconds;
1125 carp_group_demote_adj(&sc->sc_if, 1);
1128 if (pf_status.debug >= PF_DEBUG_MISC)
1129 kprintf("pfsync: requesting bulk update\n");
1130 lwkt_reltoken(&pf_token);
1131 callout_reset(&sc->sc_bulkfail_tmo, 5 * hz,
1132 pfsync_bulkfail, LIST_FIRST(&pfsync_list));
1133 lwkt_gettoken(&pf_token);
1134 error = pfsync_request_update(NULL, NULL);
1135 if (error == ENOMEM) {
1136 lwkt_reltoken(&pf_token);
1147 lwkt_reltoken(&pf_token);
1151 lwkt_reltoken(&pf_token);
1156 pfsync_setmtu(struct pfsync_softc *sc, int mtu_req)
1160 if (sc->sc_sync_ifp && sc->sc_sync_ifp->if_mtu < mtu_req)
1161 mtu = sc->sc_sync_ifp->if_mtu;
1165 sc->sc_maxcount = (mtu - sizeof(struct pfsync_header)) /
1166 sizeof(struct pfsync_state);
1167 if (sc->sc_maxcount > 254)
1168 sc->sc_maxcount = 254;
1169 sc->sc_if.if_mtu = sizeof(struct pfsync_header) +
1170 sc->sc_maxcount * sizeof(struct pfsync_state);
1174 pfsync_get_mbuf(struct pfsync_softc *sc, u_int8_t action, void **sp)
1176 struct pfsync_header *h;
1180 ASSERT_LWKT_TOKEN_HELD(&pf_token);
1182 MGETHDR(m, M_WAITOK, MT_DATA);
1184 sc->sc_if.if_oerrors++;
1189 case PFSYNC_ACT_CLR:
1190 len = sizeof(struct pfsync_header) +
1191 sizeof(struct pfsync_state_clr);
1193 case PFSYNC_ACT_UPD_C:
1194 len = (sc->sc_maxcount * sizeof(struct pfsync_state_upd)) +
1195 sizeof(struct pfsync_header);
1197 case PFSYNC_ACT_DEL_C:
1198 len = (sc->sc_maxcount * sizeof(struct pfsync_state_del)) +
1199 sizeof(struct pfsync_header);
1201 case PFSYNC_ACT_UREQ:
1202 len = (sc->sc_maxcount * sizeof(struct pfsync_state_upd_req)) +
1203 sizeof(struct pfsync_header);
1205 case PFSYNC_ACT_BUS:
1206 len = sizeof(struct pfsync_header) +
1207 sizeof(struct pfsync_state_bus);
1209 case PFSYNC_ACT_TDB_UPD:
1210 len = (sc->sc_maxcount * sizeof(struct pfsync_tdb)) +
1211 sizeof(struct pfsync_header);
1214 len = (sc->sc_maxcount * sizeof(struct pfsync_state)) +
1215 sizeof(struct pfsync_header);
1220 MCLGET(m, M_WAITOK);
1221 if ((m->m_flags & M_EXT) == 0) {
1223 sc->sc_if.if_oerrors++;
1226 m->m_data += (MCLBYTES - len) &~ (sizeof(long) - 1);
1230 m->m_pkthdr.rcvif = NULL;
1231 m->m_pkthdr.len = m->m_len = sizeof(struct pfsync_header);
1232 h = mtod(m, struct pfsync_header *);
1233 h->version = PFSYNC_VERSION;
1238 *sp = (void *)((char *)h + PFSYNC_HDRLEN);
1239 lwkt_reltoken(&pf_token);
1240 callout_reset(&sc->sc_tmo, hz, pfsync_timeout,
1241 LIST_FIRST(&pfsync_list));
1242 lwkt_gettoken(&pf_token);
1247 pfsync_pack_state(u_int8_t action, struct pf_state *st, int flags)
1249 struct ifnet *ifp = NULL;
1250 struct pfsync_softc *sc = pfsyncif;
1251 struct pfsync_header *h, *h_net;
1252 struct pfsync_state *sp = NULL;
1253 struct pfsync_state_upd *up = NULL;
1254 struct pfsync_state_del *dp = NULL;
1256 u_int8_t i = 255, newaction = 0;
1263 * If a packet falls in the forest and there's nobody around to
1264 * hear, does it make a sound?
1266 if (ifp->if_bpf == NULL && sc->sc_sync_ifp == NULL &&
1267 sc->sc_sync_peer.s_addr == INADDR_PFSYNC_GROUP) {
1268 /* Don't leave any stale pfsync packets hanging around. */
1269 if (sc->sc_mbuf != NULL) {
1270 m_freem(sc->sc_mbuf);
1272 sc->sc_statep.s = NULL;
1277 if (action >= PFSYNC_ACT_MAX)
1281 if (sc->sc_mbuf == NULL) {
1282 if ((sc->sc_mbuf = pfsync_get_mbuf(sc, action,
1283 (void *)&sc->sc_statep.s)) == NULL) {
1287 h = mtod(sc->sc_mbuf, struct pfsync_header *);
1289 h = mtod(sc->sc_mbuf, struct pfsync_header *);
1290 if (h->action != action) {
1292 if ((sc->sc_mbuf = pfsync_get_mbuf(sc, action,
1293 (void *)&sc->sc_statep.s)) == NULL) {
1297 h = mtod(sc->sc_mbuf, struct pfsync_header *);
1300 * If it's an update, look in the packet to see if
1301 * we already have an update for the state.
1303 if (action == PFSYNC_ACT_UPD && sc->sc_maxupdates) {
1304 struct pfsync_state *usp =
1305 (void *)((char *)h + PFSYNC_HDRLEN);
1307 for (i = 0; i < h->count; i++) {
1308 if (!memcmp(usp->id, &st->id,
1310 usp->creatorid == st->creatorid) {
1321 st->pfsync_time = mycpu->gd_time_seconds;;
1324 /* not a "duplicate" update */
1326 sp = sc->sc_statep.s++;
1327 sc->sc_mbuf->m_pkthdr.len =
1328 sc->sc_mbuf->m_len += sizeof(struct pfsync_state);
1330 bzero(sp, sizeof(*sp));
1332 pfsync_state_export(sp, st);
1334 if (flags & PFSYNC_FLAG_STALE)
1335 sp->sync_flags |= PFSTATE_STALE;
1337 pf_state_peer_hton(&st->src, &sp->src);
1338 pf_state_peer_hton(&st->dst, &sp->dst);
1340 if (st->expire <= time_second)
1341 sp->expire = htonl(0);
1343 sp->expire = htonl(st->expire - time_second);
1346 /* do we need to build "compressed" actions for network transfer? */
1347 if (sc->sc_sync_ifp && flags & PFSYNC_FLAG_COMPRESS) {
1349 case PFSYNC_ACT_UPD:
1350 newaction = PFSYNC_ACT_UPD_C;
1352 case PFSYNC_ACT_DEL:
1353 newaction = PFSYNC_ACT_DEL_C;
1356 /* by default we just send the uncompressed states */
1362 if (sc->sc_mbuf_net == NULL) {
1363 if ((sc->sc_mbuf_net = pfsync_get_mbuf(sc, newaction,
1364 (void *)&sc->sc_statep_net.s)) == NULL) {
1369 h_net = mtod(sc->sc_mbuf_net, struct pfsync_header *);
1371 switch (newaction) {
1372 case PFSYNC_ACT_UPD_C:
1374 up = (void *)((char *)h_net +
1375 PFSYNC_HDRLEN + (i * sizeof(*up)));
1379 sc->sc_mbuf_net->m_pkthdr.len =
1380 sc->sc_mbuf_net->m_len += sizeof(*up);
1381 up = sc->sc_statep_net.u++;
1383 bzero(up, sizeof(*up));
1384 bcopy(&st->id, up->id, sizeof(up->id));
1385 up->creatorid = st->creatorid;
1387 up->timeout = st->timeout;
1388 up->expire = sp->expire;
1392 case PFSYNC_ACT_DEL_C:
1393 sc->sc_mbuf_net->m_pkthdr.len =
1394 sc->sc_mbuf_net->m_len += sizeof(*dp);
1395 dp = sc->sc_statep_net.d++;
1398 bzero(dp, sizeof(*dp));
1399 bcopy(&st->id, dp->id, sizeof(dp->id));
1400 dp->creatorid = st->creatorid;
1405 if (h->count == sc->sc_maxcount ||
1406 (sc->sc_maxupdates && (sp->updates >= sc->sc_maxupdates)))
1407 ret = pfsync_sendout(sc);
1414 pfsync_request_update(struct pfsync_state_upd *up, struct in_addr *src)
1416 struct ifnet *ifp = NULL;
1417 struct pfsync_header *h;
1418 struct pfsync_softc *sc = pfsyncif;
1419 struct pfsync_state_upd_req *rup;
1426 if (sc->sc_mbuf == NULL) {
1427 if ((sc->sc_mbuf = pfsync_get_mbuf(sc, PFSYNC_ACT_UREQ,
1428 (void *)&sc->sc_statep.s)) == NULL)
1430 h = mtod(sc->sc_mbuf, struct pfsync_header *);
1432 h = mtod(sc->sc_mbuf, struct pfsync_header *);
1433 if (h->action != PFSYNC_ACT_UREQ) {
1435 if ((sc->sc_mbuf = pfsync_get_mbuf(sc, PFSYNC_ACT_UREQ,
1436 (void *)&sc->sc_statep.s)) == NULL)
1438 h = mtod(sc->sc_mbuf, struct pfsync_header *);
1443 sc->sc_sendaddr = *src;
1444 sc->sc_mbuf->m_pkthdr.len = sc->sc_mbuf->m_len += sizeof(*rup);
1446 rup = sc->sc_statep.r++;
1447 bzero(rup, sizeof(*rup));
1449 bcopy(up->id, rup->id, sizeof(rup->id));
1450 rup->creatorid = up->creatorid;
1453 if (h->count == sc->sc_maxcount)
1454 ret = pfsync_sendout(sc);
1460 pfsync_clear_states(u_int32_t creatorid, char *ifname)
1462 struct ifnet *ifp = NULL;
1463 struct pfsync_softc *sc = pfsyncif;
1464 struct pfsync_state_clr *cp;
1472 if (sc->sc_mbuf != NULL)
1474 if ((sc->sc_mbuf = pfsync_get_mbuf(sc, PFSYNC_ACT_CLR,
1475 (void *)&sc->sc_statep.c)) == NULL) {
1479 sc->sc_mbuf->m_pkthdr.len = sc->sc_mbuf->m_len += sizeof(*cp);
1480 cp = sc->sc_statep.c;
1481 cp->creatorid = creatorid;
1483 strlcpy(cp->ifname, ifname, IFNAMSIZ);
1485 ret = (pfsync_sendout(sc));
1491 pfsync_timeout(void *v)
1493 struct pfsync_softc *sc = v;
1501 pfsync_send_bus(struct pfsync_softc *sc, u_int8_t status)
1503 struct pfsync_state_bus *bus;
1505 if (sc->sc_mbuf != NULL)
1508 if (pfsync_sync_ok &&
1509 (sc->sc_mbuf = pfsync_get_mbuf(sc, PFSYNC_ACT_BUS,
1510 (void *)&sc->sc_statep.b)) != NULL) {
1511 sc->sc_mbuf->m_pkthdr.len = sc->sc_mbuf->m_len += sizeof(*bus);
1512 bus = sc->sc_statep.b;
1513 bus->creatorid = pf_status.hostid;
1514 bus->status = status;
1515 bus->endtime = htonl(mycpu->gd_time_seconds - sc->sc_ureq_received);
1521 pfsync_bulk_update(void *v)
1523 struct pfsync_softc *sc = v;
1525 struct pf_state *state;
1527 ASSERT_LWKT_TOKEN_HELD(&pf_token);
1530 if (sc->sc_mbuf != NULL)
1534 * Grab at most PFSYNC_BULKPACKETS worth of states which have not
1535 * been sent since the latest request was made.
1537 state = sc->sc_bulk_send_next;
1540 /* send state update if syncable and not already sent */
1541 if (!state->sync_flags
1542 && state->timeout < PFTM_MAX
1543 && state->pfsync_time <= sc->sc_ureq_received) {
1544 pfsync_pack_state(PFSYNC_ACT_UPD, state, 0);
1548 /* figure next state to send */
1549 state = TAILQ_NEXT(state, entry_list);
1551 /* wrap to start of list if we hit the end */
1553 state = TAILQ_FIRST(&state_list);
1554 } while (i < sc->sc_maxcount * PFSYNC_BULKPACKETS &&
1555 state != sc->sc_bulk_terminator);
1557 if (!state || state == sc->sc_bulk_terminator) {
1559 pfsync_send_bus(sc, PFSYNC_BUS_END);
1560 sc->sc_ureq_received = 0;
1561 sc->sc_bulk_send_next = NULL;
1562 sc->sc_bulk_terminator = NULL;
1563 lwkt_reltoken(&pf_token);
1564 callout_stop(&sc->sc_bulk_tmo);
1565 lwkt_gettoken(&pf_token);
1566 if (pf_status.debug >= PF_DEBUG_MISC)
1567 kprintf("pfsync: bulk update complete\n");
1569 /* look again for more in a bit */
1570 lwkt_reltoken(&pf_token);
1571 callout_reset(&sc->sc_bulk_tmo, 1, pfsync_timeout,
1572 LIST_FIRST(&pfsync_list));
1573 lwkt_gettoken(&pf_token);
1574 sc->sc_bulk_send_next = state;
1576 if (sc->sc_mbuf != NULL)
1582 pfsync_bulkfail(void *v)
1584 struct pfsync_softc *sc = v;
1587 ASSERT_LWKT_TOKEN_HELD(&pf_token);
1589 if (sc->sc_bulk_tries++ < PFSYNC_MAX_BULKTRIES) {
1590 /* Try again in a bit */
1591 lwkt_reltoken(&pf_token);
1592 callout_reset(&sc->sc_bulkfail_tmo, 5 * hz, pfsync_bulkfail,
1593 LIST_FIRST(&pfsync_list));
1594 lwkt_gettoken(&pf_token);
1596 error = pfsync_request_update(NULL, NULL);
1597 if (error == ENOMEM) {
1598 if (pf_status.debug >= PF_DEBUG_MISC)
1599 kprintf("pfsync: cannot allocate mbufs for "
1605 /* Pretend like the transfer was ok */
1606 sc->sc_ureq_sent = 0;
1607 sc->sc_bulk_tries = 0;
1609 if (!pfsync_sync_ok)
1610 carp_group_demote_adj(&sc->sc_if, -1);
1613 if (pf_status.debug >= PF_DEBUG_MISC)
1614 kprintf("pfsync: failed to receive "
1615 "bulk update status\n");
1616 lwkt_reltoken(&pf_token);
1617 callout_stop(&sc->sc_bulkfail_tmo);
1618 lwkt_gettoken(&pf_token);
1622 /* This must be called in splnet() */
1624 pfsync_sendout(struct pfsync_softc *sc)
1627 struct ifnet *ifp = &sc->sc_if;
1631 ASSERT_LWKT_TOKEN_HELD(&pf_token);
1633 lwkt_reltoken(&pf_token);
1634 callout_stop(&sc->sc_tmo);
1635 lwkt_gettoken(&pf_token);
1637 if (sc->sc_mbuf == NULL)
1641 sc->sc_statep.s = NULL;
1645 lwkt_reltoken(&pf_token);
1646 bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_OUT);
1647 lwkt_gettoken(&pf_token);
1651 if (sc->sc_mbuf_net) {
1653 m = sc->sc_mbuf_net;
1654 sc->sc_mbuf_net = NULL;
1655 sc->sc_statep_net.s = NULL;
1658 return pfsync_sendout_mbuf(sc, m);
1662 pfsync_sendout_mbuf(struct pfsync_softc *sc, struct mbuf *m)
1667 if (sc->sc_sync_ifp ||
1668 sc->sc_sync_peer.s_addr != INADDR_PFSYNC_GROUP) {
1669 M_PREPEND(m, sizeof(struct ip), M_WAITOK);
1671 pfsyncstats.pfsyncs_onomem++;
1674 ip = mtod(m, struct ip *);
1675 ip->ip_v = IPVERSION;
1676 ip->ip_hl = sizeof(*ip) >> 2;
1677 ip->ip_tos = IPTOS_LOWDELAY;
1678 ip->ip_len = htons(m->m_pkthdr.len);
1679 ip->ip_id = htons(ip_randomid());
1680 ip->ip_off = htons(IP_DF);
1681 ip->ip_ttl = PFSYNC_DFLTTL;
1682 ip->ip_p = IPPROTO_PFSYNC;
1685 bzero(&sa, sizeof(sa));
1686 ip->ip_src.s_addr = INADDR_ANY;
1688 if (sc->sc_sendaddr.s_addr == INADDR_PFSYNC_GROUP)
1689 m->m_flags |= M_MCAST;
1690 ip->ip_dst = sc->sc_sendaddr;
1691 sc->sc_sendaddr.s_addr = sc->sc_sync_peer.s_addr;
1693 pfsyncstats.pfsyncs_opackets++;
1695 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL))
1696 pfsyncstats.pfsyncs_oerrors++;
1704 pfsync_modevent(module_t mod, int type, void *data)
1708 struct pfsync_softc *pfs_if, *tmp;
1710 lwkt_gettoken(&pf_token);
1714 LIST_INIT(&pfsync_list);
1715 lwkt_reltoken(&pf_token);
1716 if_clone_attach(&pfsync_cloner);
1717 lwkt_gettoken(&pf_token);
1718 /* Override the function pointer for pf_ioctl.c */
1722 lwkt_reltoken(&pf_token);
1723 if_clone_detach(&pfsync_cloner);
1724 lwkt_gettoken(&pf_token);
1725 LIST_FOREACH_MUTABLE(pfs_if, &pfsync_list, sc_next, tmp) {
1726 pfsync_clone_destroy(&pfs_if->sc_if);
1735 lwkt_reltoken(&pf_token);
1739 static moduledata_t pfsync_mod = {
1745 #define PFSYNC_MODVER 44
1747 DECLARE_MODULE(pfsync, pfsync_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
1748 MODULE_VERSION(pfsync, PFSYNC_MODVER);