1 /* $OpenBSD: if_pfsync.c,v 1.110 2009/02/24 05:39:19 dlg 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 * Copyright (c) 2009 David Gwynne <dlg@openbsd.org>
32 * Permission to use, copy, modify, and distribute this software for any
33 * purpose with or without fee is hereby granted, provided that the above
34 * copyright notice and this permission notice appear in all copies.
36 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
37 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
38 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
39 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
40 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
41 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
42 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
46 * Revisions picked from OpenBSD after revision 1.110 import:
47 * 1.118, 1.124, 1.148, 1.149, 1.151, 1.171 - fixes to bulk updates
48 * 1.120, 1.175 - use monotonic time_uptime
49 * 1.122 - reduce number of updates for non-TCP sessions
51 * 1.146 - bzero() mbuf before sparsely filling it with data
52 * 1.170 - SIOCSIFMTU checks
57 #include "opt_inet6.h"
60 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD$");
66 #define NCARP DEV_CARP
70 #endif /* __FreeBSD__ */
72 #include <sys/param.h>
73 #include <sys/kernel.h>
76 #include <sys/interrupt.h>
80 #include <sys/systm.h>
83 #include <sys/socket.h>
85 #include <sys/endian.h>
86 #include <sys/malloc.h>
87 #include <sys/module.h>
88 #include <sys/sockio.h>
89 #include <sys/taskqueue.h>
91 #include <sys/mutex.h>
92 #include <sys/protosw.h>
94 #include <sys/ioctl.h>
95 #include <sys/timeout.h>
97 #include <sys/sysctl.h>
104 #include <net/if_clone.h>
106 #include <net/if_types.h>
107 #include <net/route.h>
109 #include <net/netisr.h>
111 #include <net/vnet.h>
114 #include <netinet/in.h>
115 #include <netinet/if_ether.h>
116 #include <netinet/tcp.h>
117 #include <netinet/tcp_seq.h>
120 #include <netinet/in_systm.h>
121 #include <netinet/in_var.h>
122 #include <netinet/ip.h>
123 #include <netinet/ip_var.h>
127 #include <netinet6/nd6.h>
134 #include <netinet/ip_carp.h>
137 #include <net/pfvar.h>
138 #include <net/if_pfsync.h>
141 #include "bpfilter.h"
145 #define PFSYNC_MINPKT ( \
146 sizeof(struct ip) + \
147 sizeof(struct pfsync_header) + \
148 sizeof(struct pfsync_subheader) + \
149 sizeof(struct pfsync_eof))
157 int pfsync_input_hmac(struct mbuf *, int);
159 int pfsync_upd_tcp(struct pf_state *, struct pfsync_state_peer *,
160 struct pfsync_state_peer *);
162 int pfsync_in_clr(struct pfsync_pkt *, struct mbuf *, int, int);
163 int pfsync_in_ins(struct pfsync_pkt *, struct mbuf *, int, int);
164 int pfsync_in_iack(struct pfsync_pkt *, struct mbuf *, int, int);
165 int pfsync_in_upd(struct pfsync_pkt *, struct mbuf *, int, int);
166 int pfsync_in_upd_c(struct pfsync_pkt *, struct mbuf *, int, int);
167 int pfsync_in_ureq(struct pfsync_pkt *, struct mbuf *, int, int);
168 int pfsync_in_del(struct pfsync_pkt *, struct mbuf *, int, int);
169 int pfsync_in_del_c(struct pfsync_pkt *, struct mbuf *, int, int);
170 int pfsync_in_bus(struct pfsync_pkt *, struct mbuf *, int, int);
171 int pfsync_in_tdb(struct pfsync_pkt *, struct mbuf *, int, int);
172 int pfsync_in_eof(struct pfsync_pkt *, struct mbuf *, int, int);
174 int pfsync_in_error(struct pfsync_pkt *, struct mbuf *, int, int);
176 int (*pfsync_acts[])(struct pfsync_pkt *, struct mbuf *, int, int) = {
177 pfsync_in_clr, /* PFSYNC_ACT_CLR */
178 pfsync_in_ins, /* PFSYNC_ACT_INS */
179 pfsync_in_iack, /* PFSYNC_ACT_INS_ACK */
180 pfsync_in_upd, /* PFSYNC_ACT_UPD */
181 pfsync_in_upd_c, /* PFSYNC_ACT_UPD_C */
182 pfsync_in_ureq, /* PFSYNC_ACT_UPD_REQ */
183 pfsync_in_del, /* PFSYNC_ACT_DEL */
184 pfsync_in_del_c, /* PFSYNC_ACT_DEL_C */
185 pfsync_in_error, /* PFSYNC_ACT_INS_F */
186 pfsync_in_error, /* PFSYNC_ACT_DEL_F */
187 pfsync_in_bus, /* PFSYNC_ACT_BUS */
188 pfsync_in_tdb, /* PFSYNC_ACT_TDB */
189 pfsync_in_eof /* PFSYNC_ACT_EOF */
193 int (*write)(struct pf_state *, struct mbuf *, int);
198 /* we have one of these for every PFSYNC_S_ */
199 int pfsync_out_state(struct pf_state *, struct mbuf *, int);
200 int pfsync_out_iack(struct pf_state *, struct mbuf *, int);
201 int pfsync_out_upd_c(struct pf_state *, struct mbuf *, int);
202 int pfsync_out_del(struct pf_state *, struct mbuf *, int);
204 struct pfsync_q pfsync_qs[] = {
205 { pfsync_out_state, sizeof(struct pfsync_state), PFSYNC_ACT_INS },
206 { pfsync_out_iack, sizeof(struct pfsync_ins_ack), PFSYNC_ACT_INS_ACK },
207 { pfsync_out_state, sizeof(struct pfsync_state), PFSYNC_ACT_UPD },
208 { pfsync_out_upd_c, sizeof(struct pfsync_upd_c), PFSYNC_ACT_UPD_C },
209 { pfsync_out_del, sizeof(struct pfsync_del_c), PFSYNC_ACT_DEL_C }
212 void pfsync_q_ins(struct pf_state *, int);
213 void pfsync_q_del(struct pf_state *);
215 struct pfsync_upd_req_item {
216 TAILQ_ENTRY(pfsync_upd_req_item) ur_entry;
217 struct pfsync_upd_req ur_msg;
219 TAILQ_HEAD(pfsync_upd_reqs, pfsync_upd_req_item);
221 struct pfsync_deferral {
222 TAILQ_ENTRY(pfsync_deferral) pd_entry;
223 struct pf_state *pd_st;
226 struct callout pd_tmo;
228 struct timeout pd_tmo;
231 TAILQ_HEAD(pfsync_deferrals, pfsync_deferral);
233 #define PFSYNC_PLSIZE MAX(sizeof(struct pfsync_upd_req_item), \
234 sizeof(struct pfsync_deferral))
237 int pfsync_out_tdb(struct tdb *, struct mbuf *, int);
240 struct pfsync_softc {
242 struct ifnet *sc_ifp;
246 struct ifnet *sc_sync_if;
254 struct ip_moptions sc_imo;
256 struct in_addr sc_sync_peer;
257 u_int8_t sc_maxupdates;
262 struct ip sc_template;
264 struct pf_state_queue sc_qs[PFSYNC_S_COUNT];
267 struct pfsync_upd_reqs sc_upd_req_list;
269 struct pfsync_deferrals sc_deferrals;
275 u_int32_t sc_ureq_sent;
278 struct callout sc_bulkfail_tmo;
280 struct timeout sc_bulkfail_tmo;
283 u_int32_t sc_ureq_received;
284 struct pf_state *sc_bulk_next;
285 struct pf_state *sc_bulk_last;
287 struct callout sc_bulk_tmo;
289 struct timeout sc_bulk_tmo;
292 TAILQ_HEAD(, tdb) sc_tdb_q;
295 struct callout sc_tmo;
297 struct timeout sc_tmo;
302 static MALLOC_DEFINE(M_PFSYNC, "pfsync", "pfsync data");
303 static VNET_DEFINE(struct pfsync_softc *, pfsyncif) = NULL;
304 #define V_pfsyncif VNET(pfsyncif)
305 static VNET_DEFINE(void *, pfsync_swi_cookie) = NULL;
306 #define V_pfsync_swi_cookie VNET(pfsync_swi_cookie)
307 static VNET_DEFINE(struct pfsyncstats, pfsyncstats);
308 #define V_pfsyncstats VNET(pfsyncstats)
310 static void pfsyncintr(void *);
311 static int pfsync_multicast_setup(struct pfsync_softc *);
312 static void pfsync_multicast_cleanup(struct pfsync_softc *);
313 static int pfsync_init(void);
314 static void pfsync_uninit(void);
315 static void pfsync_sendout1(int);
317 #define schednetisr(NETISR_PFSYNC) swi_sched(V_pfsync_swi_cookie, 0)
319 SYSCTL_NODE(_net, OID_AUTO, pfsync, CTLFLAG_RW, 0, "PFSYNC");
320 SYSCTL_VNET_STRUCT(_net_pfsync, OID_AUTO, stats, CTLFLAG_RW,
321 &VNET_NAME(pfsyncstats), pfsyncstats,
322 "PFSYNC statistics (struct pfsyncstats, net/if_pfsync.h)");
324 struct pfsync_softc *pfsyncif = NULL;
325 struct pfsyncstats pfsyncstats;
326 #define V_pfsyncstats pfsyncstats
329 void pfsyncattach(int);
331 int pfsync_clone_create(struct if_clone *, int, caddr_t);
332 void pfsync_clone_destroy(struct ifnet *);
334 int pfsync_clone_create(struct if_clone *, int);
335 int pfsync_clone_destroy(struct ifnet *);
337 int pfsync_alloc_scrub_memory(struct pfsync_state_peer *,
338 struct pf_state_peer *);
339 void pfsync_update_net_tdb(struct pfsync_tdb *);
340 int pfsyncoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
346 int pfsyncioctl(struct ifnet *, u_long, caddr_t);
347 void pfsyncstart(struct ifnet *);
349 struct mbuf *pfsync_if_dequeue(struct ifnet *);
351 void pfsync_deferred(struct pf_state *, int);
352 void pfsync_undefer(struct pfsync_deferral *, int);
353 void pfsync_defer_tmo(void *);
355 void pfsync_request_update(u_int32_t, u_int64_t);
356 void pfsync_update_state_req(struct pf_state *);
358 void pfsync_drop(struct pfsync_softc *);
359 void pfsync_sendout(void);
360 void pfsync_send_plus(void *, size_t);
361 void pfsync_timeout(void *);
362 void pfsync_tdb_timeout(void *);
364 void pfsync_bulk_start(void);
365 void pfsync_bulk_status(u_int8_t);
366 void pfsync_bulk_update(void *);
367 void pfsync_bulk_fail(void *);
371 #define betoh64 (unsigned long long)be64toh
372 #define timeout_del callout_stop
375 #define PFSYNC_MAX_BULKTRIES 12
381 VNET_DEFINE(struct ifc_simple_data, pfsync_cloner_data);
382 VNET_DEFINE(struct if_clone, pfsync_cloner);
383 #define V_pfsync_cloner_data VNET(pfsync_cloner_data)
384 #define V_pfsync_cloner VNET(pfsync_cloner)
385 IFC_SIMPLE_DECLARE(pfsync, 1);
387 struct if_clone pfsync_cloner =
388 IF_CLONE_INITIALIZER("pfsync", pfsync_clone_create, pfsync_clone_destroy);
392 pfsyncattach(int npfsync)
394 if_clone_attach(&pfsync_cloner);
398 pfsync_clone_create(struct if_clone *ifc, int unit, caddr_t param)
400 pfsync_clone_create(struct if_clone *ifc, int unit)
403 struct pfsync_softc *sc;
411 sc = malloc(sizeof(struct pfsync_softc), M_PFSYNC, M_WAITOK | M_ZERO);
412 sc->pfsync_sync_ok = 1;
415 sc = malloc(sizeof(*pfsyncif), M_DEVBUF, M_NOWAIT | M_ZERO);
418 for (q = 0; q < PFSYNC_S_COUNT; q++)
419 TAILQ_INIT(&sc->sc_qs[q]);
422 sc->sc_pool = uma_zcreate("pfsync", PFSYNC_PLSIZE, NULL, NULL, NULL,
423 NULL, UMA_ALIGN_PTR, 0);
425 pool_init(&sc->sc_pool, PFSYNC_PLSIZE, 0, 0, 0, "pfsync", NULL);
427 TAILQ_INIT(&sc->sc_upd_req_list);
428 TAILQ_INIT(&sc->sc_deferrals);
431 TAILQ_INIT(&sc->sc_tdb_q);
433 sc->sc_len = PFSYNC_MINPKT;
434 sc->sc_maxupdates = 128;
437 sc->sc_imo.imo_membership = (struct in_multi **)malloc(
438 (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_IPMOPTS,
440 sc->sc_imo.imo_max_memberships = IP_MIN_MEMBERSHIPS;
444 ifp = sc->sc_ifp = if_alloc(IFT_PFSYNC);
446 uma_zdestroy(sc->sc_pool);
450 if_initname(ifp, ifc->ifc_name, unit);
453 snprintf(ifp->if_xname, sizeof ifp->if_xname, "pfsync%d", unit);
456 ifp->if_ioctl = pfsyncioctl;
457 ifp->if_output = pfsyncoutput;
458 ifp->if_start = pfsyncstart;
459 ifp->if_type = IFT_PFSYNC;
460 ifp->if_snd.ifq_maxlen = ifqmaxlen;
461 ifp->if_hdrlen = sizeof(struct pfsync_header);
462 ifp->if_mtu = ETHERMTU;
464 callout_init(&sc->sc_tmo, CALLOUT_MPSAFE);
465 callout_init_mtx(&sc->sc_bulk_tmo, &pf_task_mtx, 0);
466 callout_init(&sc->sc_bulkfail_tmo, CALLOUT_MPSAFE);
468 timeout_set(&sc->sc_tmo, pfsync_timeout, sc);
469 timeout_set(&sc->sc_bulk_tmo, pfsync_bulk_update, sc);
470 timeout_set(&sc->sc_bulkfail_tmo, pfsync_bulk_fail, sc);
479 if_addgroup(ifp, "carp");
484 bpfattach(ifp, DLT_PFSYNC, PFSYNC_HDRLEN);
486 bpfattach(&sc->sc_if.if_bpf, ifp, DLT_PFSYNC, PFSYNC_HDRLEN);
504 pfsync_clone_destroy(struct ifnet *ifp)
506 struct pfsync_softc *sc = ifp->if_softc;
511 timeout_del(&sc->sc_bulkfail_tmo);
512 timeout_del(&sc->sc_bulk_tmo);
513 timeout_del(&sc->sc_tmo);
520 if (!sc->pfsync_sync_ok)
524 carp_group_demote_adj(&sc->sc_if, -1);
534 while (sc->sc_deferred > 0)
535 pfsync_undefer(TAILQ_FIRST(&sc->sc_deferrals), 0);
538 UMA_DESTROY(sc->sc_pool);
540 pool_destroy(&sc->sc_pool);
544 if (sc->sc_imo.imo_membership)
545 pfsync_multicast_cleanup(sc);
548 free(sc->sc_imo.imo_membership, M_IPMOPTS);
564 pfsync_if_dequeue(struct ifnet *ifp)
572 IF_LOCK(&ifp->if_snd);
573 _IF_DROP(&ifp->if_snd);
574 _IF_DEQUEUE(&ifp->if_snd, m);
575 IF_UNLOCK(&ifp->if_snd);
578 IF_DEQUEUE(&ifp->if_snd, m);
586 * Start output on the pfsync interface.
589 pfsyncstart(struct ifnet *ifp)
593 while ((m = pfsync_if_dequeue(ifp)) != NULL) {
595 IF_DROP(&ifp->if_snd);
602 pfsync_alloc_scrub_memory(struct pfsync_state_peer *s,
603 struct pf_state_peer *d)
605 if (s->scrub.scrub_flag && d->scrub == NULL) {
607 d->scrub = pool_get(&V_pf_state_scrub_pl, PR_NOWAIT | PR_ZERO);
609 d->scrub = pool_get(&pf_state_scrub_pl, PR_NOWAIT | PR_ZERO);
611 if (d->scrub == NULL)
620 pfsync_state_export(struct pfsync_state *sp, struct pf_state *st)
622 bzero(sp, sizeof(struct pfsync_state));
624 /* copy from state key */
625 sp->key[PF_SK_WIRE].addr[0] = st->key[PF_SK_WIRE]->addr[0];
626 sp->key[PF_SK_WIRE].addr[1] = st->key[PF_SK_WIRE]->addr[1];
627 sp->key[PF_SK_WIRE].port[0] = st->key[PF_SK_WIRE]->port[0];
628 sp->key[PF_SK_WIRE].port[1] = st->key[PF_SK_WIRE]->port[1];
629 sp->key[PF_SK_STACK].addr[0] = st->key[PF_SK_STACK]->addr[0];
630 sp->key[PF_SK_STACK].addr[1] = st->key[PF_SK_STACK]->addr[1];
631 sp->key[PF_SK_STACK].port[0] = st->key[PF_SK_STACK]->port[0];
632 sp->key[PF_SK_STACK].port[1] = st->key[PF_SK_STACK]->port[1];
633 sp->proto = st->key[PF_SK_WIRE]->proto;
634 sp->af = st->key[PF_SK_WIRE]->af;
636 /* copy from state */
637 strlcpy(sp->ifname, st->kif->pfik_name, sizeof(sp->ifname));
638 bcopy(&st->rt_addr, &sp->rt_addr, sizeof(sp->rt_addr));
639 sp->creation = htonl(time_uptime - st->creation);
640 sp->expire = pf_state_expires(st);
641 if (sp->expire <= time_second)
642 sp->expire = htonl(0);
644 sp->expire = htonl(sp->expire - time_second);
646 sp->direction = st->direction;
648 sp->timeout = st->timeout;
649 sp->state_flags = st->state_flags;
651 sp->sync_flags |= PFSYNC_FLAG_SRCNODE;
652 if (st->nat_src_node)
653 sp->sync_flags |= PFSYNC_FLAG_NATSRCNODE;
655 bcopy(&st->id, &sp->id, sizeof(sp->id));
656 sp->creatorid = st->creatorid;
657 pf_state_peer_hton(&st->src, &sp->src);
658 pf_state_peer_hton(&st->dst, &sp->dst);
660 if (st->rule.ptr == NULL)
661 sp->rule = htonl(-1);
663 sp->rule = htonl(st->rule.ptr->nr);
664 if (st->anchor.ptr == NULL)
665 sp->anchor = htonl(-1);
667 sp->anchor = htonl(st->anchor.ptr->nr);
668 if (st->nat_rule.ptr == NULL)
669 sp->nat_rule = htonl(-1);
671 sp->nat_rule = htonl(st->nat_rule.ptr->nr);
673 pf_state_counter_hton(st->packets[0], sp->packets[0]);
674 pf_state_counter_hton(st->packets[1], sp->packets[1]);
675 pf_state_counter_hton(st->bytes[0], sp->bytes[0]);
676 pf_state_counter_hton(st->bytes[1], sp->bytes[1]);
682 pfsync_state_import(struct pfsync_state *sp, u_int8_t flags)
684 struct pf_state *st = NULL;
685 struct pf_state_key *skw = NULL, *sks = NULL;
686 struct pf_rule *r = NULL;
694 if (sp->creatorid == 0 && V_pf_status.debug >= PF_DEBUG_MISC) {
696 if (sp->creatorid == 0 && pf_status.debug >= PF_DEBUG_MISC) {
698 printf("pfsync_state_import: invalid creator id:"
699 " %08x\n", ntohl(sp->creatorid));
703 if ((kif = pfi_kif_get(sp->ifname)) == NULL) {
705 if (V_pf_status.debug >= PF_DEBUG_MISC)
707 if (pf_status.debug >= PF_DEBUG_MISC)
709 printf("pfsync_state_import: "
710 "unknown interface: %s\n", sp->ifname);
711 if (flags & PFSYNC_SI_IOCTL)
713 return (0); /* skip this state */
717 * If the ruleset checksums match or the state is coming from the ioctl,
718 * it's safe to associate the state with the rule of that number.
720 if (sp->rule != htonl(-1) && sp->anchor == htonl(-1) &&
721 (flags & (PFSYNC_SI_IOCTL | PFSYNC_SI_CKSUM)) && ntohl(sp->rule) <
722 pf_main_ruleset.rules[PF_RULESET_FILTER].active.rcount)
723 r = pf_main_ruleset.rules[
724 PF_RULESET_FILTER].active.ptr_array[ntohl(sp->rule)];
727 r = &V_pf_default_rule;
729 r = &pf_default_rule;
732 if ((r->max_states && r->states_cur >= r->max_states))
736 if (flags & PFSYNC_SI_IOCTL)
737 pool_flags = PR_WAITOK | PR_ZERO;
739 pool_flags = PR_NOWAIT | PR_ZERO;
741 if ((st = pool_get(&V_pf_state_pl, pool_flags)) == NULL)
744 if (flags & PFSYNC_SI_IOCTL)
745 pool_flags = PR_WAITOK | PR_LIMITFAIL | PR_ZERO;
747 pool_flags = PR_LIMITFAIL | PR_ZERO;
749 if ((st = pool_get(&pf_state_pl, pool_flags)) == NULL)
753 if ((skw = pf_alloc_state_key(pool_flags)) == NULL)
756 if (PF_ANEQ(&sp->key[PF_SK_WIRE].addr[0],
757 &sp->key[PF_SK_STACK].addr[0], sp->af) ||
758 PF_ANEQ(&sp->key[PF_SK_WIRE].addr[1],
759 &sp->key[PF_SK_STACK].addr[1], sp->af) ||
760 sp->key[PF_SK_WIRE].port[0] != sp->key[PF_SK_STACK].port[0] ||
761 sp->key[PF_SK_WIRE].port[1] != sp->key[PF_SK_STACK].port[1]) {
762 if ((sks = pf_alloc_state_key(pool_flags)) == NULL)
767 /* allocate memory for scrub info */
768 if (pfsync_alloc_scrub_memory(&sp->src, &st->src) ||
769 pfsync_alloc_scrub_memory(&sp->dst, &st->dst))
772 /* copy to state key(s) */
773 skw->addr[0] = sp->key[PF_SK_WIRE].addr[0];
774 skw->addr[1] = sp->key[PF_SK_WIRE].addr[1];
775 skw->port[0] = sp->key[PF_SK_WIRE].port[0];
776 skw->port[1] = sp->key[PF_SK_WIRE].port[1];
777 skw->proto = sp->proto;
780 sks->addr[0] = sp->key[PF_SK_STACK].addr[0];
781 sks->addr[1] = sp->key[PF_SK_STACK].addr[1];
782 sks->port[0] = sp->key[PF_SK_STACK].port[0];
783 sks->port[1] = sp->key[PF_SK_STACK].port[1];
784 sks->proto = sp->proto;
789 bcopy(&sp->rt_addr, &st->rt_addr, sizeof(st->rt_addr));
790 st->creation = time_uptime - ntohl(sp->creation);
791 st->expire = time_second;
793 /* XXX No adaptive scaling. */
794 st->expire -= r->timeout[sp->timeout] - ntohl(sp->expire);
797 st->expire = ntohl(sp->expire) + time_second;
798 st->direction = sp->direction;
800 st->timeout = sp->timeout;
801 st->state_flags = sp->state_flags;
803 bcopy(sp->id, &st->id, sizeof(st->id));
804 st->creatorid = sp->creatorid;
805 pf_state_peer_ntoh(&sp->src, &st->src);
806 pf_state_peer_ntoh(&sp->dst, &st->dst);
809 st->nat_rule.ptr = NULL;
810 st->anchor.ptr = NULL;
813 st->pfsync_time = time_uptime;
814 st->sync_state = PFSYNC_S_NONE;
816 /* XXX when we have nat_rule/anchors, use STATE_INC_COUNTERS */
820 if (!ISSET(flags, PFSYNC_SI_IOCTL))
821 SET(st->state_flags, PFSTATE_NOSYNC);
823 if ((error = pf_state_insert(kif, skw, sks, st)) != 0) {
824 /* XXX when we have nat_rule/anchors, use STATE_DEC_COUNTERS */
829 if (!ISSET(flags, PFSYNC_SI_IOCTL)) {
830 CLR(st->state_flags, PFSTATE_NOSYNC);
831 if (ISSET(st->state_flags, PFSTATE_ACK)) {
832 pfsync_q_ins(st, PFSYNC_S_IACK);
833 schednetisr(NETISR_PFSYNC);
836 CLR(st->state_flags, PFSTATE_ACK);
846 pool_put(&V_pf_state_key_pl, skw);
848 pool_put(&V_pf_state_key_pl, sks);
851 pool_put(&pf_state_key_pl, skw);
853 pool_put(&pf_state_key_pl, sks);
856 cleanup_state: /* pf_state_insert frees the state keys */
860 pool_put(&V_pf_state_scrub_pl, st->dst.scrub);
862 pool_put(&V_pf_state_scrub_pl, st->src.scrub);
863 pool_put(&V_pf_state_pl, st);
866 pool_put(&pf_state_scrub_pl, st->dst.scrub);
868 pool_put(&pf_state_scrub_pl, st->src.scrub);
869 pool_put(&pf_state_pl, st);
877 pfsync_input(struct mbuf *m, __unused int off)
879 pfsync_input(struct mbuf *m, ...)
883 struct pfsync_softc *sc = V_pfsyncif;
885 struct pfsync_softc *sc = pfsyncif;
887 struct pfsync_pkt pkt;
888 struct ip *ip = mtod(m, struct ip *);
889 struct pfsync_header *ph;
890 struct pfsync_subheader subh;
895 V_pfsyncstats.pfsyncs_ipackets++;
897 /* verify that we have a sync interface configured */
899 if (!sc || !sc->sc_sync_if || !V_pf_status.running)
901 if (!sc || !sc->sc_sync_if || !pf_status.running)
905 /* verify that the packet came in on the right interface */
906 if (sc->sc_sync_if != m->m_pkthdr.rcvif) {
907 V_pfsyncstats.pfsyncs_badif++;
912 sc->sc_ifp->if_ipackets++;
913 sc->sc_ifp->if_ibytes += m->m_pkthdr.len;
915 sc->sc_if.if_ipackets++;
916 sc->sc_if.if_ibytes += m->m_pkthdr.len;
918 /* verify that the IP TTL is 255. */
919 if (ip->ip_ttl != PFSYNC_DFLTTL) {
920 V_pfsyncstats.pfsyncs_badttl++;
924 offset = ip->ip_hl << 2;
925 if (m->m_pkthdr.len < offset + sizeof(*ph)) {
926 V_pfsyncstats.pfsyncs_hdrops++;
930 if (offset + sizeof(*ph) > m->m_len) {
931 if (m_pullup(m, offset + sizeof(*ph)) == NULL) {
932 V_pfsyncstats.pfsyncs_hdrops++;
935 ip = mtod(m, struct ip *);
937 ph = (struct pfsync_header *)((char *)ip + offset);
939 /* verify the version */
940 if (ph->version != PFSYNC_VERSION) {
941 V_pfsyncstats.pfsyncs_badver++;
946 if (pfsync_input_hmac(m, offset) != 0) {
952 /* Cheaper to grab this now than having to mess with mbufs later */
954 pkt.src = ip->ip_src;
958 if (!bcmp(&ph->pfcksum, &V_pf_status.pf_chksum, PF_MD5_DIGEST_LENGTH))
960 if (!bcmp(&ph->pfcksum, &pf_status.pf_chksum, PF_MD5_DIGEST_LENGTH))
962 pkt.flags |= PFSYNC_SI_CKSUM;
964 offset += sizeof(*ph);
966 m_copydata(m, offset, sizeof(subh), (caddr_t)&subh);
967 offset += sizeof(subh);
969 if (subh.action >= PFSYNC_ACT_MAX) {
970 V_pfsyncstats.pfsyncs_badact++;
974 rv = (*pfsync_acts[subh.action])(&pkt, m, offset,
987 pfsync_in_clr(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
989 struct pfsync_clr *clr;
991 int len = sizeof(*clr) * count;
994 struct pf_state *st, *nexts;
995 struct pf_state_key *sk, *nextsk;
996 struct pf_state_item *si;
1000 mp = m_pulldown(m, offset, len, &offp);
1002 V_pfsyncstats.pfsyncs_badlen++;
1005 clr = (struct pfsync_clr *)(mp->m_data + offp);
1011 for (i = 0; i < count; i++) {
1012 creatorid = clr[i].creatorid;
1014 if (clr[i].ifname[0] == '\0') {
1016 for (st = RB_MIN(pf_state_tree_id, &V_tree_id);
1018 nexts = RB_NEXT(pf_state_tree_id, &V_tree_id, st);
1020 for (st = RB_MIN(pf_state_tree_id, &tree_id);
1022 nexts = RB_NEXT(pf_state_tree_id, &tree_id, st);
1024 if (st->creatorid == creatorid) {
1025 SET(st->state_flags, PFSTATE_NOSYNC);
1026 pf_unlink_state(st);
1030 if (pfi_kif_get(clr[i].ifname) == NULL)
1035 for (sk = RB_MIN(pf_state_tree, &V_pf_statetbl);
1037 for (sk = RB_MIN(pf_state_tree, &pf_statetbl);
1040 nextsk = RB_NEXT(pf_state_tree,
1042 &V_pf_statetbl, sk);
1046 TAILQ_FOREACH(si, &sk->states, entry) {
1047 if (si->s->creatorid == creatorid) {
1048 SET(si->s->state_flags,
1050 pf_unlink_state(si->s);
1065 pfsync_in_ins(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1068 struct pfsync_state *sa, *sp;
1069 int len = sizeof(*sp) * count;
1074 mp = m_pulldown(m, offset, len, &offp);
1076 V_pfsyncstats.pfsyncs_badlen++;
1079 sa = (struct pfsync_state *)(mp->m_data + offp);
1085 for (i = 0; i < count; i++) {
1088 /* check for invalid values */
1089 if (sp->timeout >= PFTM_MAX ||
1090 sp->src.state > PF_TCPS_PROXY_DST ||
1091 sp->dst.state > PF_TCPS_PROXY_DST ||
1092 sp->direction > PF_OUT ||
1093 (sp->af != AF_INET && sp->af != AF_INET6)) {
1095 if (V_pf_status.debug >= PF_DEBUG_MISC) {
1097 if (pf_status.debug >= PF_DEBUG_MISC) {
1099 printf("pfsync_input: PFSYNC5_ACT_INS: "
1102 V_pfsyncstats.pfsyncs_badval++;
1106 if (pfsync_state_import(sp, pkt->flags) == ENOMEM) {
1107 /* drop out, but process the rest of the actions */
1120 pfsync_in_iack(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1122 struct pfsync_ins_ack *ia, *iaa;
1123 struct pf_state_cmp id_key;
1124 struct pf_state *st;
1127 int len = count * sizeof(*ia);
1131 mp = m_pulldown(m, offset, len, &offp);
1133 V_pfsyncstats.pfsyncs_badlen++;
1136 iaa = (struct pfsync_ins_ack *)(mp->m_data + offp);
1142 for (i = 0; i < count; i++) {
1145 bcopy(&ia->id, &id_key.id, sizeof(id_key.id));
1146 id_key.creatorid = ia->creatorid;
1148 st = pf_find_state_byid(&id_key);
1152 if (ISSET(st->state_flags, PFSTATE_ACK))
1153 pfsync_deferred(st, 0);
1160 * XXX this is not yet implemented, but we know the size of the
1161 * message so we can skip it.
1164 return (count * sizeof(struct pfsync_ins_ack));
1168 pfsync_upd_tcp(struct pf_state *st, struct pfsync_state_peer *src,
1169 struct pfsync_state_peer *dst)
1174 * The state should never go backwards except
1175 * for syn-proxy states. Neither should the
1176 * sequence window slide backwards.
1178 if (st->src.state > src->state &&
1179 (st->src.state < PF_TCPS_PROXY_SRC ||
1180 src->state >= PF_TCPS_PROXY_SRC))
1182 else if (SEQ_GT(st->src.seqlo, ntohl(src->seqlo)))
1184 else if (st->dst.state > dst->state) {
1185 /* There might still be useful
1186 * information about the src state here,
1187 * so import that part of the update,
1188 * then "fail" so we send the updated
1189 * state back to the peer who is missing
1190 * our what we know. */
1191 pf_state_peer_ntoh(src, &st->src);
1192 /* XXX do anything with timeouts? */
1194 } else if (st->dst.state >= TCPS_SYN_SENT &&
1195 SEQ_GT(st->dst.seqlo, ntohl(dst->seqlo)))
1202 pfsync_in_upd(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1204 struct pfsync_state *sa, *sp;
1205 struct pf_state_cmp id_key;
1206 struct pf_state_key *sk;
1207 struct pf_state *st;
1211 int len = count * sizeof(*sp);
1215 mp = m_pulldown(m, offset, len, &offp);
1217 V_pfsyncstats.pfsyncs_badlen++;
1220 sa = (struct pfsync_state *)(mp->m_data + offp);
1226 for (i = 0; i < count; i++) {
1229 /* check for invalid values */
1230 if (sp->timeout >= PFTM_MAX ||
1231 sp->src.state > PF_TCPS_PROXY_DST ||
1232 sp->dst.state > PF_TCPS_PROXY_DST) {
1234 if (V_pf_status.debug >= PF_DEBUG_MISC) {
1236 if (pf_status.debug >= PF_DEBUG_MISC) {
1238 printf("pfsync_input: PFSYNC_ACT_UPD: "
1241 V_pfsyncstats.pfsyncs_badval++;
1245 bcopy(sp->id, &id_key.id, sizeof(id_key.id));
1246 id_key.creatorid = sp->creatorid;
1248 st = pf_find_state_byid(&id_key);
1250 /* insert the update */
1251 if (pfsync_state_import(sp, 0))
1252 V_pfsyncstats.pfsyncs_badstate++;
1256 if (ISSET(st->state_flags, PFSTATE_ACK))
1257 pfsync_deferred(st, 1);
1259 sk = st->key[PF_SK_WIRE]; /* XXX right one? */
1261 if (sk->proto == IPPROTO_TCP)
1262 sfail = pfsync_upd_tcp(st, &sp->src, &sp->dst);
1265 * Non-TCP protocol state machine always go
1268 if (st->src.state > sp->src.state)
1270 else if (st->dst.state > sp->dst.state)
1276 if (V_pf_status.debug >= PF_DEBUG_MISC) {
1278 if (pf_status.debug >= PF_DEBUG_MISC) {
1280 printf("pfsync: %s stale update (%d)"
1281 " id: %016llx creatorid: %08x\n",
1282 (sfail < 7 ? "ignoring" : "partial"),
1283 sfail, betoh64(st->id),
1284 ntohl(st->creatorid));
1286 V_pfsyncstats.pfsyncs_stale++;
1288 pfsync_update_state(st);
1289 schednetisr(NETISR_PFSYNC);
1292 pfsync_alloc_scrub_memory(&sp->dst, &st->dst);
1293 pf_state_peer_ntoh(&sp->src, &st->src);
1294 pf_state_peer_ntoh(&sp->dst, &st->dst);
1295 st->expire = ntohl(sp->expire) + time_second;
1296 st->timeout = sp->timeout;
1297 st->pfsync_time = time_uptime;
1308 pfsync_in_upd_c(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1310 struct pfsync_upd_c *ua, *up;
1311 struct pf_state_key *sk;
1312 struct pf_state_cmp id_key;
1313 struct pf_state *st;
1315 int len = count * sizeof(*up);
1322 mp = m_pulldown(m, offset, len, &offp);
1324 V_pfsyncstats.pfsyncs_badlen++;
1327 ua = (struct pfsync_upd_c *)(mp->m_data + offp);
1333 for (i = 0; i < count; i++) {
1336 /* check for invalid values */
1337 if (up->timeout >= PFTM_MAX ||
1338 up->src.state > PF_TCPS_PROXY_DST ||
1339 up->dst.state > PF_TCPS_PROXY_DST) {
1341 if (V_pf_status.debug >= PF_DEBUG_MISC) {
1343 if (pf_status.debug >= PF_DEBUG_MISC) {
1345 printf("pfsync_input: "
1346 "PFSYNC_ACT_UPD_C: "
1349 V_pfsyncstats.pfsyncs_badval++;
1353 bcopy(&up->id, &id_key.id, sizeof(id_key.id));
1354 id_key.creatorid = up->creatorid;
1356 st = pf_find_state_byid(&id_key);
1358 /* We don't have this state. Ask for it. */
1359 pfsync_request_update(id_key.creatorid, id_key.id);
1363 if (ISSET(st->state_flags, PFSTATE_ACK))
1364 pfsync_deferred(st, 1);
1366 sk = st->key[PF_SK_WIRE]; /* XXX right one? */
1368 if (sk->proto == IPPROTO_TCP)
1369 sfail = pfsync_upd_tcp(st, &up->src, &up->dst);
1372 * Non-TCP protocol state machine always go forwards
1374 if (st->src.state > up->src.state)
1376 else if (st->dst.state > up->dst.state)
1382 if (V_pf_status.debug >= PF_DEBUG_MISC) {
1384 if (pf_status.debug >= PF_DEBUG_MISC) {
1386 printf("pfsync: ignoring stale update "
1388 "creatorid: %08x\n", sfail,
1390 ntohl(st->creatorid));
1392 V_pfsyncstats.pfsyncs_stale++;
1394 pfsync_update_state(st);
1395 schednetisr(NETISR_PFSYNC);
1398 pfsync_alloc_scrub_memory(&up->dst, &st->dst);
1399 pf_state_peer_ntoh(&up->src, &st->src);
1400 pf_state_peer_ntoh(&up->dst, &st->dst);
1401 st->expire = ntohl(up->expire) + time_second;
1402 st->timeout = up->timeout;
1403 st->pfsync_time = time_uptime;
1414 pfsync_in_ureq(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1416 struct pfsync_upd_req *ur, *ura;
1418 int len = count * sizeof(*ur);
1421 struct pf_state_cmp id_key;
1422 struct pf_state *st;
1424 mp = m_pulldown(m, offset, len, &offp);
1426 V_pfsyncstats.pfsyncs_badlen++;
1429 ura = (struct pfsync_upd_req *)(mp->m_data + offp);
1434 for (i = 0; i < count; i++) {
1437 bcopy(&ur->id, &id_key.id, sizeof(id_key.id));
1438 id_key.creatorid = ur->creatorid;
1440 if (id_key.id == 0 && id_key.creatorid == 0)
1441 pfsync_bulk_start();
1443 st = pf_find_state_byid(&id_key);
1445 V_pfsyncstats.pfsyncs_badstate++;
1448 if (ISSET(st->state_flags, PFSTATE_NOSYNC))
1451 pfsync_update_state_req(st);
1462 pfsync_in_del(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1465 struct pfsync_state *sa, *sp;
1466 struct pf_state_cmp id_key;
1467 struct pf_state *st;
1468 int len = count * sizeof(*sp);
1472 mp = m_pulldown(m, offset, len, &offp);
1474 V_pfsyncstats.pfsyncs_badlen++;
1477 sa = (struct pfsync_state *)(mp->m_data + offp);
1483 for (i = 0; i < count; i++) {
1486 bcopy(sp->id, &id_key.id, sizeof(id_key.id));
1487 id_key.creatorid = sp->creatorid;
1489 st = pf_find_state_byid(&id_key);
1491 V_pfsyncstats.pfsyncs_badstate++;
1494 SET(st->state_flags, PFSTATE_NOSYNC);
1495 pf_unlink_state(st);
1506 pfsync_in_del_c(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1509 struct pfsync_del_c *sa, *sp;
1510 struct pf_state_cmp id_key;
1511 struct pf_state *st;
1512 int len = count * sizeof(*sp);
1516 mp = m_pulldown(m, offset, len, &offp);
1518 V_pfsyncstats.pfsyncs_badlen++;
1521 sa = (struct pfsync_del_c *)(mp->m_data + offp);
1527 for (i = 0; i < count; i++) {
1530 bcopy(&sp->id, &id_key.id, sizeof(id_key.id));
1531 id_key.creatorid = sp->creatorid;
1533 st = pf_find_state_byid(&id_key);
1535 V_pfsyncstats.pfsyncs_badstate++;
1539 SET(st->state_flags, PFSTATE_NOSYNC);
1540 pf_unlink_state(st);
1551 pfsync_in_bus(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1554 struct pfsync_softc *sc = V_pfsyncif;
1556 struct pfsync_softc *sc = pfsyncif;
1558 struct pfsync_bus *bus;
1560 int len = count * sizeof(*bus);
1563 /* If we're not waiting for a bulk update, who cares. */
1564 if (sc->sc_ureq_sent == 0)
1567 mp = m_pulldown(m, offset, len, &offp);
1569 V_pfsyncstats.pfsyncs_badlen++;
1572 bus = (struct pfsync_bus *)(mp->m_data + offp);
1574 switch (bus->status) {
1575 case PFSYNC_BUS_START:
1577 callout_reset(&sc->sc_bulkfail_tmo, 4 * hz +
1578 V_pf_pool_limits[PF_LIMIT_STATES].limit /
1579 ((sc->sc_ifp->if_mtu - PFSYNC_MINPKT) /
1580 sizeof(struct pfsync_state)),
1581 pfsync_bulk_fail, V_pfsyncif);
1583 timeout_add(&sc->sc_bulkfail_tmo, 4 * hz +
1584 pf_pool_limits[PF_LIMIT_STATES].limit /
1585 ((sc->sc_if.if_mtu - PFSYNC_MINPKT) /
1586 sizeof(struct pfsync_state)));
1589 if (V_pf_status.debug >= PF_DEBUG_MISC)
1591 if (pf_status.debug >= PF_DEBUG_MISC)
1593 printf("pfsync: received bulk update start\n");
1596 case PFSYNC_BUS_END:
1597 if (time_uptime - ntohl(bus->endtime) >=
1599 /* that's it, we're happy */
1600 sc->sc_ureq_sent = 0;
1601 sc->sc_bulk_tries = 0;
1602 timeout_del(&sc->sc_bulkfail_tmo);
1606 if (!sc->pfsync_sync_ok)
1608 if (!pfsync_sync_ok)
1610 carp_group_demote_adj(&sc->sc_if, -1);
1614 sc->pfsync_sync_ok = 1;
1619 if (V_pf_status.debug >= PF_DEBUG_MISC)
1621 if (pf_status.debug >= PF_DEBUG_MISC)
1623 printf("pfsync: received valid "
1624 "bulk update end\n");
1627 if (V_pf_status.debug >= PF_DEBUG_MISC)
1629 if (pf_status.debug >= PF_DEBUG_MISC)
1631 printf("pfsync: received invalid "
1632 "bulk update end: bad timestamp\n");
1641 pfsync_in_tdb(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1643 int len = count * sizeof(struct pfsync_tdb);
1646 struct pfsync_tdb *tp;
1652 mp = m_pulldown(m, offset, len, &offp);
1654 V_pfsyncstats.pfsyncs_badlen++;
1657 tp = (struct pfsync_tdb *)(mp->m_data + offp);
1663 for (i = 0; i < count; i++)
1664 pfsync_update_net_tdb(&tp[i]);
1675 /* Update an in-kernel tdb. Silently fail if no tdb is found. */
1677 pfsync_update_net_tdb(struct pfsync_tdb *pt)
1682 /* check for invalid values */
1683 if (ntohl(pt->spi) <= SPI_RESERVED_MAX ||
1684 (pt->dst.sa.sa_family != AF_INET &&
1685 pt->dst.sa.sa_family != AF_INET6))
1689 tdb = gettdb(pt->spi, &pt->dst, pt->sproto);
1691 pt->rpl = ntohl(pt->rpl);
1692 pt->cur_bytes = betoh64(pt->cur_bytes);
1694 /* Neither replay nor byte counter should ever decrease. */
1695 if (pt->rpl < tdb->tdb_rpl ||
1696 pt->cur_bytes < tdb->tdb_cur_bytes) {
1701 tdb->tdb_rpl = pt->rpl;
1702 tdb->tdb_cur_bytes = pt->cur_bytes;
1709 if (V_pf_status.debug >= PF_DEBUG_MISC)
1711 if (pf_status.debug >= PF_DEBUG_MISC)
1713 printf("pfsync_insert: PFSYNC_ACT_TDB_UPD: "
1715 V_pfsyncstats.pfsyncs_badstate++;
1722 pfsync_in_eof(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1724 /* check if we are at the right place in the packet */
1725 if (offset != m->m_pkthdr.len - sizeof(struct pfsync_eof))
1726 V_pfsyncstats.pfsyncs_badact++;
1728 /* we're done. free and let the caller return */
1734 pfsync_in_error(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1736 V_pfsyncstats.pfsyncs_badact++;
1743 pfsyncoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
1756 pfsyncioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1759 struct proc *p = curproc;
1761 struct pfsync_softc *sc = ifp->if_softc;
1762 struct ifreq *ifr = (struct ifreq *)data;
1763 struct ip_moptions *imo = &sc->sc_imo;
1764 struct pfsyncreq pfsyncr;
1773 case SIOCSIFDSTADDR:
1777 if (ifp->if_flags & IFF_UP)
1778 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1780 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1782 if (ifp->if_flags & IFF_UP)
1783 ifp->if_flags |= IFF_RUNNING;
1785 ifp->if_flags &= ~IFF_RUNNING;
1789 if (!sc->sc_sync_if ||
1790 ifr->ifr_mtu <= PFSYNC_MINPKT ||
1791 ifr->ifr_mtu > sc->sc_sync_if->if_mtu)
1793 if (ifr->ifr_mtu < ifp->if_mtu) {
1804 ifp->if_mtu = ifr->ifr_mtu;
1807 bzero(&pfsyncr, sizeof(pfsyncr));
1808 if (sc->sc_sync_if) {
1809 strlcpy(pfsyncr.pfsyncr_syncdev,
1810 sc->sc_sync_if->if_xname, IFNAMSIZ);
1812 pfsyncr.pfsyncr_syncpeer = sc->sc_sync_peer;
1813 pfsyncr.pfsyncr_maxupdates = sc->sc_maxupdates;
1814 return (copyout(&pfsyncr, ifr->ifr_data, sizeof(pfsyncr)));
1818 if ((error = priv_check(curthread, PRIV_NETINET_PF)) != 0)
1820 if ((error = suser(p, p->p_acflag)) != 0)
1823 if ((error = copyin(ifr->ifr_data, &pfsyncr, sizeof(pfsyncr))))
1829 if (pfsyncr.pfsyncr_syncpeer.s_addr == 0)
1831 sc->sc_sync_peer.s_addr = htonl(INADDR_PFSYNC_GROUP);
1833 sc->sc_sync_peer.s_addr = INADDR_PFSYNC_GROUP;
1836 sc->sc_sync_peer.s_addr =
1837 pfsyncr.pfsyncr_syncpeer.s_addr;
1839 if (pfsyncr.pfsyncr_maxupdates > 255)
1848 sc->sc_maxupdates = pfsyncr.pfsyncr_maxupdates;
1850 if (pfsyncr.pfsyncr_syncdev[0] == 0) {
1851 sc->sc_sync_if = NULL;
1854 if (imo->imo_membership)
1855 pfsync_multicast_cleanup(sc);
1857 if (imo->imo_num_memberships > 0) {
1858 in_delmulti(imo->imo_membership[
1859 --imo->imo_num_memberships]);
1860 imo->imo_multicast_ifp = NULL;
1869 if ((sifp = ifunit(pfsyncr.pfsyncr_syncdev)) == NULL)
1877 if (sifp->if_mtu < sc->sc_ifp->if_mtu ||
1879 if (sifp->if_mtu < sc->sc_if.if_mtu ||
1881 (sc->sc_sync_if != NULL &&
1882 sifp->if_mtu < sc->sc_sync_if->if_mtu) ||
1883 sifp->if_mtu < MCLBYTES - sizeof(struct ip))
1885 sc->sc_sync_if = sifp;
1888 if (imo->imo_membership) {
1890 pfsync_multicast_cleanup(sc);
1894 if (imo->imo_num_memberships > 0) {
1895 in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1896 imo->imo_multicast_ifp = NULL;
1901 if (sc->sc_sync_if &&
1902 sc->sc_sync_peer.s_addr == htonl(INADDR_PFSYNC_GROUP)) {
1904 error = pfsync_multicast_setup(sc);
1910 if (sc->sc_sync_if &&
1911 sc->sc_sync_peer.s_addr == INADDR_PFSYNC_GROUP) {
1912 struct in_addr addr;
1914 if (!(sc->sc_sync_if->if_flags & IFF_MULTICAST)) {
1915 sc->sc_sync_if = NULL;
1917 return (EADDRNOTAVAIL);
1920 addr.s_addr = INADDR_PFSYNC_GROUP;
1922 if ((imo->imo_membership[0] =
1923 in_addmulti(&addr, sc->sc_sync_if)) == NULL) {
1924 sc->sc_sync_if = NULL;
1928 imo->imo_num_memberships++;
1929 imo->imo_multicast_ifp = sc->sc_sync_if;
1930 imo->imo_multicast_ttl = PFSYNC_DFLTTL;
1931 imo->imo_multicast_loop = 0;
1933 #endif /* !__FreeBSD__ */
1935 ip = &sc->sc_template;
1936 bzero(ip, sizeof(*ip));
1937 ip->ip_v = IPVERSION;
1938 ip->ip_hl = sizeof(sc->sc_template) >> 2;
1939 ip->ip_tos = IPTOS_LOWDELAY;
1940 /* len and id are set later */
1944 ip->ip_off = htons(IP_DF);
1946 ip->ip_ttl = PFSYNC_DFLTTL;
1947 ip->ip_p = IPPROTO_PFSYNC;
1948 ip->ip_src.s_addr = INADDR_ANY;
1949 ip->ip_dst.s_addr = sc->sc_sync_peer.s_addr;
1951 if (sc->sc_sync_if) {
1952 /* Request a full state table update. */
1953 sc->sc_ureq_sent = time_uptime;
1957 if (sc->pfsync_sync_ok)
1961 carp_group_demote_adj(&sc->sc_if, 1);
1965 sc->pfsync_sync_ok = 0;
1970 if (V_pf_status.debug >= PF_DEBUG_MISC)
1972 if (pf_status.debug >= PF_DEBUG_MISC)
1974 printf("pfsync: requesting bulk update\n");
1976 callout_reset(&sc->sc_bulkfail_tmo, 5 * hz,
1977 pfsync_bulk_fail, V_pfsyncif);
1979 timeout_add_sec(&sc->sc_bulkfail_tmo, 5);
1981 pfsync_request_update(0, 0);
1998 pfsync_out_state(struct pf_state *st, struct mbuf *m, int offset)
2000 struct pfsync_state *sp = (struct pfsync_state *)(m->m_data + offset);
2002 pfsync_state_export(sp, st);
2004 return (sizeof(*sp));
2008 pfsync_out_iack(struct pf_state *st, struct mbuf *m, int offset)
2010 struct pfsync_ins_ack *iack =
2011 (struct pfsync_ins_ack *)(m->m_data + offset);
2014 iack->creatorid = st->creatorid;
2016 return (sizeof(*iack));
2020 pfsync_out_upd_c(struct pf_state *st, struct mbuf *m, int offset)
2022 struct pfsync_upd_c *up = (struct pfsync_upd_c *)(m->m_data + offset);
2024 bzero(up, sizeof(*up));
2026 pf_state_peer_hton(&st->src, &up->src);
2027 pf_state_peer_hton(&st->dst, &up->dst);
2028 up->creatorid = st->creatorid;
2030 up->expire = pf_state_expires(st);
2031 if (up->expire <= time_second)
2032 up->expire = htonl(0);
2034 up->expire = htonl(up->expire - time_second);
2035 up->timeout = st->timeout;
2037 return (sizeof(*up));
2041 pfsync_out_del(struct pf_state *st, struct mbuf *m, int offset)
2043 struct pfsync_del_c *dp = (struct pfsync_del_c *)(m->m_data + offset);
2046 dp->creatorid = st->creatorid;
2048 SET(st->state_flags, PFSTATE_NOSYNC);
2050 return (sizeof(*dp));
2054 pfsync_drop(struct pfsync_softc *sc)
2056 struct pf_state *st;
2057 struct pfsync_upd_req_item *ur;
2063 for (q = 0; q < PFSYNC_S_COUNT; q++) {
2064 if (TAILQ_EMPTY(&sc->sc_qs[q]))
2067 TAILQ_FOREACH(st, &sc->sc_qs[q], sync_list) {
2070 KASSERT(st->sync_state == q,
2071 ("%s: st->sync_state == q",
2074 KASSERT(st->sync_state == q);
2077 st->sync_state = PFSYNC_S_NONE;
2079 TAILQ_INIT(&sc->sc_qs[q]);
2082 while ((ur = TAILQ_FIRST(&sc->sc_upd_req_list)) != NULL) {
2083 TAILQ_REMOVE(&sc->sc_upd_req_list, ur, ur_entry);
2084 pool_put(&sc->sc_pool, ur);
2090 if (!TAILQ_EMPTY(&sc->sc_tdb_q)) {
2091 TAILQ_FOREACH(t, &sc->sc_tdb_q, tdb_sync_entry)
2092 CLR(t->tdb_flags, TDBF_PFSYNC);
2094 TAILQ_INIT(&sc->sc_tdb_q);
2098 sc->sc_len = PFSYNC_MINPKT;
2102 void pfsync_sendout()
2108 pfsync_sendout1(int schedswi)
2110 struct pfsync_softc *sc = V_pfsyncif;
2113 pfsync_sendout(void)
2115 struct pfsync_softc *sc = pfsyncif;
2119 struct ifnet *ifp = sc->sc_ifp;
2121 struct ifnet *ifp = &sc->sc_if;
2126 struct pfsync_header *ph;
2127 struct pfsync_subheader *subh;
2128 struct pf_state *st;
2129 struct pfsync_upd_req_item *ur;
2145 if (sc == NULL || sc->sc_len == PFSYNC_MINPKT)
2149 if (ifp->if_bpf == NULL && sc->sc_sync_if == NULL) {
2151 if (sc->sc_sync_if == NULL) {
2157 MGETHDR(m, M_DONTWAIT, MT_DATA);
2160 sc->sc_ifp->if_oerrors++;
2162 sc->sc_if.if_oerrors++;
2164 V_pfsyncstats.pfsyncs_onomem++;
2170 pktlen = max_linkhdr + sc->sc_len;
2171 if (pktlen > MHLEN) {
2172 /* Find the right pool to allocate from. */
2173 /* XXX: This is ugly. */
2174 m_cljget(m, M_DONTWAIT, pktlen <= MCLBYTES ? MCLBYTES :
2175 #if MJUMPAGESIZE != MCLBYTES
2176 pktlen <= MJUMPAGESIZE ? MJUMPAGESIZE :
2178 pktlen <= MJUM9BYTES ? MJUM9BYTES : MJUM16BYTES);
2180 if (max_linkhdr + sc->sc_len > MHLEN) {
2181 MCLGETI(m, M_DONTWAIT, NULL, max_linkhdr + sc->sc_len);
2183 if (!ISSET(m->m_flags, M_EXT)) {
2186 sc->sc_ifp->if_oerrors++;
2188 sc->sc_if.if_oerrors++;
2190 V_pfsyncstats.pfsyncs_onomem++;
2195 m->m_data += max_linkhdr;
2196 m->m_len = m->m_pkthdr.len = sc->sc_len;
2198 /* build the ip header */
2199 ip = (struct ip *)m->m_data;
2200 bcopy(&sc->sc_template, ip, sizeof(*ip));
2201 offset = sizeof(*ip);
2204 ip->ip_len = m->m_pkthdr.len;
2206 ip->ip_len = htons(m->m_pkthdr.len);
2208 ip->ip_id = htons(ip_randomid());
2210 /* build the pfsync header */
2211 ph = (struct pfsync_header *)(m->m_data + offset);
2212 bzero(ph, sizeof(*ph));
2213 offset += sizeof(*ph);
2215 ph->version = PFSYNC_VERSION;
2216 ph->len = htons(sc->sc_len - sizeof(*ip));
2218 bcopy(V_pf_status.pf_chksum, ph->pfcksum, PF_MD5_DIGEST_LENGTH);
2220 bcopy(pf_status.pf_chksum, ph->pfcksum, PF_MD5_DIGEST_LENGTH);
2223 /* walk the queues */
2224 for (q = 0; q < PFSYNC_S_COUNT; q++) {
2225 if (TAILQ_EMPTY(&sc->sc_qs[q]))
2228 subh = (struct pfsync_subheader *)(m->m_data + offset);
2229 offset += sizeof(*subh);
2232 TAILQ_FOREACH(st, &sc->sc_qs[q], sync_list) {
2235 KASSERT(st->sync_state == q,
2236 ("%s: st->sync_state == q",
2239 KASSERT(st->sync_state == q);
2243 offset += pfsync_qs[q].write(st, m, offset);
2244 st->sync_state = PFSYNC_S_NONE;
2247 TAILQ_INIT(&sc->sc_qs[q]);
2249 bzero(subh, sizeof(*subh));
2250 subh->action = pfsync_qs[q].action;
2251 subh->count = htons(count);
2254 if (!TAILQ_EMPTY(&sc->sc_upd_req_list)) {
2255 subh = (struct pfsync_subheader *)(m->m_data + offset);
2256 offset += sizeof(*subh);
2259 while ((ur = TAILQ_FIRST(&sc->sc_upd_req_list)) != NULL) {
2260 TAILQ_REMOVE(&sc->sc_upd_req_list, ur, ur_entry);
2262 bcopy(&ur->ur_msg, m->m_data + offset,
2263 sizeof(ur->ur_msg));
2264 offset += sizeof(ur->ur_msg);
2266 pool_put(&sc->sc_pool, ur);
2271 bzero(subh, sizeof(*subh));
2272 subh->action = PFSYNC_ACT_UPD_REQ;
2273 subh->count = htons(count);
2276 /* has someone built a custom region for us to add? */
2277 if (sc->sc_plus != NULL) {
2278 bcopy(sc->sc_plus, m->m_data + offset, sc->sc_pluslen);
2279 offset += sc->sc_pluslen;
2285 if (!TAILQ_EMPTY(&sc->sc_tdb_q)) {
2286 subh = (struct pfsync_subheader *)(m->m_data + offset);
2287 offset += sizeof(*subh);
2290 TAILQ_FOREACH(t, &sc->sc_tdb_q, tdb_sync_entry) {
2291 offset += pfsync_out_tdb(t, m, offset);
2292 CLR(t->tdb_flags, TDBF_PFSYNC);
2296 TAILQ_INIT(&sc->sc_tdb_q);
2298 bzero(subh, sizeof(*subh));
2299 subh->action = PFSYNC_ACT_TDB;
2300 subh->count = htons(count);
2304 subh = (struct pfsync_subheader *)(m->m_data + offset);
2305 offset += sizeof(*subh);
2307 bzero(subh, sizeof(*subh));
2308 subh->action = PFSYNC_ACT_EOF;
2309 subh->count = htons(1);
2311 /* XXX write checksum in EOF here */
2313 /* we're done, let's put it on the wire */
2316 m->m_data += sizeof(*ip);
2317 m->m_len = m->m_pkthdr.len = sc->sc_len - sizeof(*ip);
2321 bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_OUT);
2323 m->m_data -= sizeof(*ip);
2324 m->m_len = m->m_pkthdr.len = sc->sc_len;
2327 if (sc->sc_sync_if == NULL) {
2328 sc->sc_len = PFSYNC_MINPKT;
2335 sc->sc_ifp->if_opackets++;
2336 sc->sc_ifp->if_obytes += m->m_pkthdr.len;
2337 sc->sc_len = PFSYNC_MINPKT;
2339 if (!_IF_QFULL(&sc->sc_ifp->if_snd))
2340 _IF_ENQUEUE(&sc->sc_ifp->if_snd, m);
2343 sc->sc_ifp->if_snd.ifq_drops++;
2346 swi_sched(V_pfsync_swi_cookie, 0);
2348 sc->sc_if.if_opackets++;
2349 sc->sc_if.if_obytes += m->m_pkthdr.len;
2351 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL) == 0)
2352 pfsyncstats.pfsyncs_opackets++;
2354 pfsyncstats.pfsyncs_oerrors++;
2357 sc->sc_len = PFSYNC_MINPKT;
2362 pfsync_insert_state(struct pf_state *st)
2365 struct pfsync_softc *sc = V_pfsyncif;
2367 struct pfsync_softc *sc = pfsyncif;
2373 splassert(IPL_SOFTNET);
2376 if (ISSET(st->rule.ptr->rule_flag, PFRULE_NOSYNC) ||
2377 st->key[PF_SK_WIRE]->proto == IPPROTO_PFSYNC) {
2378 SET(st->state_flags, PFSTATE_NOSYNC);
2382 if (sc == NULL || ISSET(st->state_flags, PFSTATE_NOSYNC))
2387 KASSERT(st->sync_state == PFSYNC_S_NONE,
2388 ("%s: st->sync_state == PFSYNC_S_NONE", __FUNCTION__));
2390 KASSERT(st->sync_state == PFSYNC_S_NONE);
2394 if (sc->sc_len == PFSYNC_MINPKT)
2396 callout_reset(&sc->sc_tmo, 1 * hz, pfsync_timeout,
2399 timeout_add_sec(&sc->sc_tmo, 1);
2402 pfsync_q_ins(st, PFSYNC_S_INS);
2404 if (ISSET(st->state_flags, PFSTATE_ACK))
2405 schednetisr(NETISR_PFSYNC);
2407 st->sync_updates = 0;
2413 pfsync_defer(struct pf_state *st, struct mbuf *m)
2416 struct pfsync_softc *sc = V_pfsyncif;
2418 struct pfsync_softc *sc = pfsyncif;
2420 struct pfsync_deferral *pd;
2425 splassert(IPL_SOFTNET);
2428 if (sc->sc_deferred >= 128)
2429 pfsync_undefer(TAILQ_FIRST(&sc->sc_deferrals), 0);
2431 pd = pool_get(&sc->sc_pool, M_NOWAIT);
2437 m->m_flags |= M_SKIP_FIREWALL;
2439 m->m_pkthdr.pf.flags |= PF_TAG_GENERATED;
2441 SET(st->state_flags, PFSTATE_ACK);
2446 TAILQ_INSERT_TAIL(&sc->sc_deferrals, pd, pd_entry);
2448 callout_init(&pd->pd_tmo, CALLOUT_MPSAFE);
2449 callout_reset(&pd->pd_tmo, defer, pfsync_defer_tmo,
2452 timeout_set(&pd->pd_tmo, pfsync_defer_tmo, pd);
2453 timeout_add(&pd->pd_tmo, defer);
2460 pfsync_undefer(struct pfsync_deferral *pd, int drop)
2463 struct pfsync_softc *sc = V_pfsyncif;
2465 struct pfsync_softc *sc = pfsyncif;
2472 splassert(IPL_SOFTNET);
2475 TAILQ_REMOVE(&sc->sc_deferrals, pd, pd_entry);
2478 CLR(pd->pd_st->state_flags, PFSTATE_ACK);
2479 timeout_del(&pd->pd_tmo); /* bah */
2485 /* XXX: use pf_defered?! */
2488 ip_output(pd->pd_m, (void *)NULL, (void *)NULL, 0,
2489 (void *)NULL, (void *)NULL);
2496 pool_put(&sc->sc_pool, pd);
2500 pfsync_defer_tmo(void *arg)
2502 #if defined(__FreeBSD__) && defined(VIMAGE)
2503 struct pfsync_deferral *pd = arg;
2509 CURVNET_SET(pd->pd_m->m_pkthdr.rcvif->if_vnet); /* XXX */
2512 pfsync_undefer(arg, 0);
2521 pfsync_deferred(struct pf_state *st, int drop)
2524 struct pfsync_softc *sc = V_pfsyncif;
2526 struct pfsync_softc *sc = pfsyncif;
2528 struct pfsync_deferral *pd;
2530 TAILQ_FOREACH(pd, &sc->sc_deferrals, pd_entry) {
2531 if (pd->pd_st == st) {
2532 pfsync_undefer(pd, drop);
2537 panic("pfsync_send_deferred: unable to find deferred state");
2540 u_int pfsync_upds = 0;
2543 pfsync_update_state(struct pf_state *st)
2546 struct pfsync_softc *sc = V_pfsyncif;
2548 struct pfsync_softc *sc = pfsyncif;
2555 splassert(IPL_SOFTNET);
2561 if (ISSET(st->state_flags, PFSTATE_ACK))
2562 pfsync_deferred(st, 0);
2563 if (ISSET(st->state_flags, PFSTATE_NOSYNC)) {
2564 if (st->sync_state != PFSYNC_S_NONE)
2569 if (sc->sc_len == PFSYNC_MINPKT)
2571 callout_reset(&sc->sc_tmo, 1 * hz, pfsync_timeout,
2574 timeout_add_sec(&sc->sc_tmo, 1);
2577 switch (st->sync_state) {
2578 case PFSYNC_S_UPD_C:
2581 /* we're already handling it */
2583 if (st->key[PF_SK_WIRE]->proto == IPPROTO_TCP) {
2585 if (st->sync_updates >= sc->sc_maxupdates)
2593 pfsync_q_ins(st, PFSYNC_S_UPD_C);
2594 st->sync_updates = 0;
2598 panic("pfsync_update_state: unexpected sync state %d",
2602 if (sync || (time_uptime - st->pfsync_time) < 2) {
2604 schednetisr(NETISR_PFSYNC);
2609 pfsync_request_update(u_int32_t creatorid, u_int64_t id)
2612 struct pfsync_softc *sc = V_pfsyncif;
2614 struct pfsync_softc *sc = pfsyncif;
2616 struct pfsync_upd_req_item *item;
2617 size_t nlen = sizeof(struct pfsync_upd_req);
2623 * this code does nothing to prevent multiple update requests for the
2624 * same state being generated.
2627 item = pool_get(&sc->sc_pool, PR_NOWAIT);
2633 item->ur_msg.id = id;
2634 item->ur_msg.creatorid = creatorid;
2636 if (TAILQ_EMPTY(&sc->sc_upd_req_list))
2637 nlen += sizeof(struct pfsync_subheader);
2640 if (sc->sc_len + nlen > sc->sc_ifp->if_mtu) {
2642 if (sc->sc_len + nlen > sc->sc_if.if_mtu) {
2648 nlen = sizeof(struct pfsync_subheader) +
2649 sizeof(struct pfsync_upd_req);
2652 TAILQ_INSERT_TAIL(&sc->sc_upd_req_list, item, ur_entry);
2655 schednetisr(NETISR_PFSYNC);
2659 pfsync_update_state_req(struct pf_state *st)
2662 struct pfsync_softc *sc = V_pfsyncif;
2664 struct pfsync_softc *sc = pfsyncif;
2670 panic("pfsync_update_state_req: nonexistant instance");
2672 if (ISSET(st->state_flags, PFSTATE_NOSYNC)) {
2673 if (st->sync_state != PFSYNC_S_NONE)
2678 switch (st->sync_state) {
2679 case PFSYNC_S_UPD_C:
2683 pfsync_q_ins(st, PFSYNC_S_UPD);
2684 schednetisr(NETISR_PFSYNC);
2690 /* we're already handling it */
2694 panic("pfsync_update_state_req: unexpected sync state %d",
2700 pfsync_delete_state(struct pf_state *st)
2703 struct pfsync_softc *sc = V_pfsyncif;
2705 struct pfsync_softc *sc = pfsyncif;
2711 splassert(IPL_SOFTNET);
2717 if (ISSET(st->state_flags, PFSTATE_ACK))
2718 pfsync_deferred(st, 1);
2719 if (ISSET(st->state_flags, PFSTATE_NOSYNC)) {
2720 if (st->sync_state != PFSYNC_S_NONE)
2725 if (sc->sc_len == PFSYNC_MINPKT)
2727 callout_reset(&sc->sc_tmo, 1 * hz, pfsync_timeout,
2730 timeout_add_sec(&sc->sc_tmo, 1);
2733 switch (st->sync_state) {
2735 /* we never got to tell the world so just forget about it */
2739 case PFSYNC_S_UPD_C:
2743 /* FALLTHROUGH to putting it on the del list */
2746 pfsync_q_ins(st, PFSYNC_S_DEL);
2750 panic("pfsync_delete_state: unexpected sync state %d",
2756 pfsync_clear_states(u_int32_t creatorid, const char *ifname)
2759 struct pfsync_subheader subh;
2760 struct pfsync_clr clr;
2764 struct pfsync_softc *sc = V_pfsyncif;
2766 struct pfsync_softc *sc = pfsyncif;
2772 splassert(IPL_SOFTNET);
2778 bzero(&r, sizeof(r));
2780 r.subh.action = PFSYNC_ACT_CLR;
2781 r.subh.count = htons(1);
2783 strlcpy(r.clr.ifname, ifname, sizeof(r.clr.ifname));
2784 r.clr.creatorid = creatorid;
2786 pfsync_send_plus(&r, sizeof(r));
2790 pfsync_q_ins(struct pf_state *st, int q)
2793 struct pfsync_softc *sc = V_pfsyncif;
2795 struct pfsync_softc *sc = pfsyncif;
2797 size_t nlen = pfsync_qs[q].len;
2803 KASSERT(st->sync_state == PFSYNC_S_NONE,
2804 ("%s: st->sync_state == PFSYNC_S_NONE", __FUNCTION__));
2806 KASSERT(st->sync_state == PFSYNC_S_NONE);
2809 #if 1 || defined(PFSYNC_DEBUG)
2810 if (sc->sc_len < PFSYNC_MINPKT)
2812 panic("pfsync pkt len is too low %zu", sc->sc_len);
2814 panic("pfsync pkt len is too low %d", sc->sc_len);
2817 if (TAILQ_EMPTY(&sc->sc_qs[q]))
2818 nlen += sizeof(struct pfsync_subheader);
2821 if (sc->sc_len + nlen > sc->sc_ifp->if_mtu) {
2823 if (sc->sc_len + nlen > sc->sc_if.if_mtu) {
2829 nlen = sizeof(struct pfsync_subheader) + pfsync_qs[q].len;
2833 TAILQ_INSERT_TAIL(&sc->sc_qs[q], st, sync_list);
2838 pfsync_q_del(struct pf_state *st)
2841 struct pfsync_softc *sc = V_pfsyncif;
2843 struct pfsync_softc *sc = pfsyncif;
2845 int q = st->sync_state;
2848 KASSERT(st->sync_state != PFSYNC_S_NONE,
2849 ("%s: st->sync_state != PFSYNC_S_NONE", __FUNCTION__));
2851 KASSERT(st->sync_state != PFSYNC_S_NONE);
2854 sc->sc_len -= pfsync_qs[q].len;
2855 TAILQ_REMOVE(&sc->sc_qs[q], st, sync_list);
2856 st->sync_state = PFSYNC_S_NONE;
2858 if (TAILQ_EMPTY(&sc->sc_qs[q]))
2859 sc->sc_len -= sizeof(struct pfsync_subheader);
2864 pfsync_update_tdb(struct tdb *t, int output)
2867 struct pfsync_softc *sc = V_pfsyncif;
2869 struct pfsync_softc *sc = pfsyncif;
2871 size_t nlen = sizeof(struct pfsync_tdb);
2877 if (!ISSET(t->tdb_flags, TDBF_PFSYNC)) {
2878 if (TAILQ_EMPTY(&sc->sc_tdb_q))
2879 nlen += sizeof(struct pfsync_subheader);
2881 if (sc->sc_len + nlen > sc->sc_if.if_mtu) {
2888 nlen = sizeof(struct pfsync_subheader) +
2889 sizeof(struct pfsync_tdb);
2893 TAILQ_INSERT_TAIL(&sc->sc_tdb_q, t, tdb_sync_entry);
2894 SET(t->tdb_flags, TDBF_PFSYNC);
2897 if (++t->tdb_updates >= sc->sc_maxupdates)
2898 schednetisr(NETISR_PFSYNC);
2902 SET(t->tdb_flags, TDBF_PFSYNC_RPL);
2904 CLR(t->tdb_flags, TDBF_PFSYNC_RPL);
2908 pfsync_delete_tdb(struct tdb *t)
2911 struct pfsync_softc *sc = V_pfsyncif;
2913 struct pfsync_softc *sc = pfsyncif;
2916 if (sc == NULL || !ISSET(t->tdb_flags, TDBF_PFSYNC))
2919 sc->sc_len -= sizeof(struct pfsync_tdb);
2920 TAILQ_REMOVE(&sc->sc_tdb_q, t, tdb_sync_entry);
2921 CLR(t->tdb_flags, TDBF_PFSYNC);
2923 if (TAILQ_EMPTY(&sc->sc_tdb_q))
2924 sc->sc_len -= sizeof(struct pfsync_subheader);
2928 pfsync_out_tdb(struct tdb *t, struct mbuf *m, int offset)
2930 struct pfsync_tdb *ut = (struct pfsync_tdb *)(m->m_data + offset);
2932 bzero(ut, sizeof(*ut));
2933 ut->spi = t->tdb_spi;
2934 bcopy(&t->tdb_dst, &ut->dst, sizeof(ut->dst));
2936 * When a failover happens, the master's rpl is probably above
2937 * what we see here (we may be up to a second late), so
2938 * increase it a bit for outbound tdbs to manage most such
2941 * For now, just add an offset that is likely to be larger
2942 * than the number of packets we can see in one second. The RFC
2943 * just says the next packet must have a higher seq value.
2945 * XXX What is a good algorithm for this? We could use
2946 * a rate-determined increase, but to know it, we would have
2947 * to extend struct tdb.
2948 * XXX pt->rpl can wrap over MAXINT, but if so the real tdb
2949 * will soon be replaced anyway. For now, just don't handle
2952 #define RPL_INCR 16384
2953 ut->rpl = htonl(t->tdb_rpl + (ISSET(t->tdb_flags, TDBF_PFSYNC_RPL) ?
2955 ut->cur_bytes = htobe64(t->tdb_cur_bytes);
2956 ut->sproto = t->tdb_sproto;
2958 return (sizeof(*ut));
2963 pfsync_bulk_start(void)
2966 struct pfsync_softc *sc = V_pfsyncif;
2968 struct pfsync_softc *sc = pfsyncif;
2972 if (V_pf_status.debug >= PF_DEBUG_MISC)
2974 if (pf_status.debug >= PF_DEBUG_MISC)
2976 printf("pfsync: received bulk update request\n");
2980 if (TAILQ_EMPTY(&V_state_list))
2982 if (TAILQ_EMPTY(&state_list))
2984 pfsync_bulk_status(PFSYNC_BUS_END);
2986 sc->sc_ureq_received = time_uptime;
2987 if (sc->sc_bulk_next == NULL)
2989 sc->sc_bulk_next = TAILQ_FIRST(&V_state_list);
2991 sc->sc_bulk_next = TAILQ_FIRST(&state_list);
2993 sc->sc_bulk_last = sc->sc_bulk_next;
2995 pfsync_bulk_status(PFSYNC_BUS_START);
2996 callout_reset(&sc->sc_bulk_tmo, 1, pfsync_bulk_update, sc);
3001 pfsync_bulk_update(void *arg)
3003 struct pfsync_softc *sc = arg;
3004 struct pf_state *st = sc->sc_bulk_next;
3012 CURVNET_SET(sc->sc_ifp->if_vnet);
3015 if (st->sync_state == PFSYNC_S_NONE &&
3016 st->timeout < PFTM_MAX &&
3017 st->pfsync_time <= sc->sc_ureq_received) {
3018 pfsync_update_state_req(st);
3022 st = TAILQ_NEXT(st, entry_list);
3025 st = TAILQ_FIRST(&V_state_list);
3027 st = TAILQ_FIRST(&state_list);
3030 if (st == sc->sc_bulk_last) {
3032 sc->sc_bulk_next = NULL;
3033 sc->sc_bulk_last = NULL;
3034 pfsync_bulk_status(PFSYNC_BUS_END);
3039 if (i > 1 && (sc->sc_ifp->if_mtu - sc->sc_len) <
3041 if (i > 1 && (sc->sc_if.if_mtu - sc->sc_len) <
3043 sizeof(struct pfsync_state)) {
3044 /* we've filled a packet */
3045 sc->sc_bulk_next = st;
3047 callout_reset(&sc->sc_bulk_tmo, 1,
3048 pfsync_bulk_update, sc);
3050 timeout_add(&sc->sc_bulk_tmo, 1);
3063 pfsync_bulk_status(u_int8_t status)
3066 struct pfsync_subheader subh;
3067 struct pfsync_bus bus;
3071 struct pfsync_softc *sc = V_pfsyncif;
3073 struct pfsync_softc *sc = pfsyncif;
3078 bzero(&r, sizeof(r));
3080 r.subh.action = PFSYNC_ACT_BUS;
3081 r.subh.count = htons(1);
3084 r.bus.creatorid = V_pf_status.hostid;
3086 r.bus.creatorid = pf_status.hostid;
3088 r.bus.endtime = htonl(time_uptime - sc->sc_ureq_received);
3089 r.bus.status = status;
3091 pfsync_send_plus(&r, sizeof(r));
3095 pfsync_bulk_fail(void *arg)
3097 struct pfsync_softc *sc = arg;
3100 CURVNET_SET(sc->sc_ifp->if_vnet);
3103 if (sc->sc_bulk_tries++ < PFSYNC_MAX_BULKTRIES) {
3106 callout_reset(&sc->sc_bulkfail_tmo, 5 * hz,
3107 pfsync_bulk_fail, V_pfsyncif);
3109 timeout_add_sec(&sc->sc_bulkfail_tmo, 5);
3112 pfsync_request_update(0, 0);
3115 /* Pretend like the transfer was ok */
3116 sc->sc_ureq_sent = 0;
3117 sc->sc_bulk_tries = 0;
3121 if (!sc->pfsync_sync_ok)
3123 if (!pfsync_sync_ok)
3125 carp_group_demote_adj(&sc->sc_if, -1);
3129 sc->pfsync_sync_ok = 1;
3134 if (V_pf_status.debug >= PF_DEBUG_MISC)
3136 if (pf_status.debug >= PF_DEBUG_MISC)
3138 printf("pfsync: failed to receive bulk update\n");
3147 pfsync_send_plus(void *plus, size_t pluslen)
3150 struct pfsync_softc *sc = V_pfsyncif;
3152 struct pfsync_softc *sc = pfsyncif;
3159 if (sc->sc_len + pluslen > sc->sc_ifp->if_mtu) {
3161 if (sc->sc_len + pluslen > sc->sc_if.if_mtu) {
3169 sc->sc_len += (sc->sc_pluslen = pluslen);
3180 struct pfsync_softc *sc = V_pfsyncif;
3182 struct pfsync_softc *sc = pfsyncif;
3186 if (sc == NULL || !ISSET(sc->sc_ifp->if_flags, IFF_DRV_RUNNING))
3188 if (sc == NULL || !ISSET(sc->sc_if.if_flags, IFF_RUNNING))
3196 pfsync_state_in_use(struct pf_state *st)
3199 struct pfsync_softc *sc = V_pfsyncif;
3201 struct pfsync_softc *sc = pfsyncif;
3207 if (st->sync_state != PFSYNC_S_NONE ||
3208 st == sc->sc_bulk_next ||
3209 st == sc->sc_bulk_last)
3219 pfsync_timeout(void *arg)
3221 #if defined(__FreeBSD__) && defined(VIMAGE)
3222 struct pfsync_softc *sc = arg;
3227 CURVNET_SET(sc->sc_ifp->if_vnet);
3247 /* this is a softnet/netisr handler */
3250 pfsyncintr(void *arg)
3252 struct pfsync_softc *sc = arg;
3255 CURVNET_SET(sc->sc_ifp->if_vnet);
3259 if (sc->sc_len > PFSYNC_MINPKT)
3261 _IF_DEQUEUE_ALL(&sc->sc_ifp->if_snd, m);
3264 for (; m != NULL; m = n) {
3267 m->m_nextpkt = NULL;
3268 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)
3270 V_pfsyncstats.pfsyncs_opackets++;
3272 V_pfsyncstats.pfsyncs_oerrors++;
3290 pfsync_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
3295 /* All sysctl names at this level are terminal. */
3300 case PFSYNCCTL_STATS:
3303 return (sysctl_struct(oldp, oldlenp, newp, newlen,
3304 &V_pfsyncstats, sizeof(V_pfsyncstats)));
3307 return (ENOPROTOOPT);
3312 pfsync_multicast_setup(struct pfsync_softc *sc)
3314 struct ip_moptions *imo = &sc->sc_imo;
3317 if (!(sc->sc_sync_if->if_flags & IFF_MULTICAST)) {
3318 sc->sc_sync_if = NULL;
3319 return (EADDRNOTAVAIL);
3322 imo->imo_membership = (struct in_multi **)malloc(
3323 (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_PFSYNC,
3325 imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
3326 imo->imo_multicast_vif = -1;
3328 if ((error = in_joingroup(sc->sc_sync_if, &sc->sc_sync_peer, NULL,
3329 &imo->imo_membership[0])) != 0) {
3330 free(imo->imo_membership, M_PFSYNC);
3333 imo->imo_num_memberships++;
3334 imo->imo_multicast_ifp = sc->sc_sync_if;
3335 imo->imo_multicast_ttl = PFSYNC_DFLTTL;
3336 imo->imo_multicast_loop = 0;
3342 pfsync_multicast_cleanup(struct pfsync_softc *sc)
3344 struct ip_moptions *imo = &sc->sc_imo;
3346 in_leavegroup(imo->imo_membership[0], NULL);
3347 free(imo->imo_membership, M_PFSYNC);
3348 imo->imo_membership = NULL;
3349 imo->imo_multicast_ifp = NULL;
3353 extern struct domain inetdomain;
3354 static struct protosw in_pfsync_protosw = {
3355 .pr_type = SOCK_RAW,
3356 .pr_domain = &inetdomain,
3357 .pr_protocol = IPPROTO_PFSYNC,
3358 .pr_flags = PR_ATOMIC|PR_ADDR,
3359 .pr_input = pfsync_input,
3360 .pr_output = (pr_output_t *)rip_output,
3361 .pr_ctloutput = rip_ctloutput,
3362 .pr_usrreqs = &rip_usrreqs
3369 VNET_ITERATOR_DECL(vnet_iter);
3373 VNET_FOREACH(vnet_iter) {
3374 CURVNET_SET(vnet_iter);
3375 V_pfsync_cloner = pfsync_cloner;
3376 V_pfsync_cloner_data = pfsync_cloner_data;
3377 V_pfsync_cloner.ifc_data = &V_pfsync_cloner_data;
3378 if_clone_attach(&V_pfsync_cloner);
3379 error = swi_add(NULL, "pfsync", pfsyncintr, V_pfsyncif,
3380 SWI_NET, INTR_MPSAFE, &V_pfsync_swi_cookie);
3385 VNET_LIST_RUNLOCK();
3387 error = pf_proto_register(PF_INET, &in_pfsync_protosw);
3390 error = ipproto_register(IPPROTO_PFSYNC);
3392 pf_proto_unregister(PF_INET, IPPROTO_PFSYNC, SOCK_RAW);
3397 pfsync_state_import_ptr = pfsync_state_import;
3398 pfsync_up_ptr = pfsync_up;
3399 pfsync_insert_state_ptr = pfsync_insert_state;
3400 pfsync_update_state_ptr = pfsync_update_state;
3401 pfsync_delete_state_ptr = pfsync_delete_state;
3402 pfsync_clear_states_ptr = pfsync_clear_states;
3403 pfsync_state_in_use_ptr = pfsync_state_in_use;
3404 pfsync_defer_ptr = pfsync_defer;
3412 VNET_FOREACH(vnet_iter) {
3413 CURVNET_SET(vnet_iter);
3414 if (V_pfsync_swi_cookie) {
3415 swi_remove(V_pfsync_swi_cookie);
3416 if_clone_detach(&V_pfsync_cloner);
3420 VNET_LIST_RUNLOCK();
3428 VNET_ITERATOR_DECL(vnet_iter);
3431 pfsync_state_import_ptr = NULL;
3432 pfsync_up_ptr = NULL;
3433 pfsync_insert_state_ptr = NULL;
3434 pfsync_update_state_ptr = NULL;
3435 pfsync_delete_state_ptr = NULL;
3436 pfsync_clear_states_ptr = NULL;
3437 pfsync_state_in_use_ptr = NULL;
3438 pfsync_defer_ptr = NULL;
3441 ipproto_unregister(IPPROTO_PFSYNC);
3442 pf_proto_unregister(PF_INET, IPPROTO_PFSYNC, SOCK_RAW);
3444 VNET_FOREACH(vnet_iter) {
3445 CURVNET_SET(vnet_iter);
3446 swi_remove(V_pfsync_swi_cookie);
3447 if_clone_detach(&V_pfsync_cloner);
3450 VNET_LIST_RUNLOCK();
3454 pfsync_modevent(module_t mod, int type, void *data)
3460 error = pfsync_init();
3464 * Module should not be unloaded due to race conditions.
3479 static moduledata_t pfsync_mod = {
3485 #define PFSYNC_MODVER 1
3487 DECLARE_MODULE(pfsync, pfsync_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
3488 MODULE_VERSION(pfsync, PFSYNC_MODVER);
3489 MODULE_DEPEND(pfsync, pf, PF_MODVER, PF_MODVER, PF_MODVER);
3490 #endif /* __FreeBSD__ */