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
53 * 1.126, 1.142 - deferred packets processing
54 * 1.173 - correct expire time processing
59 #include "opt_inet6.h"
62 #include <sys/cdefs.h>
63 __FBSDID("$FreeBSD$");
66 #endif /* __FreeBSD__ */
68 #include <sys/param.h>
69 #include <sys/kernel.h>
72 #include <sys/interrupt.h>
76 #include <sys/systm.h>
79 #include <sys/socket.h>
81 #include <sys/endian.h>
82 #include <sys/malloc.h>
83 #include <sys/module.h>
84 #include <sys/sockio.h>
85 #include <sys/taskqueue.h>
87 #include <sys/mutex.h>
88 #include <sys/protosw.h>
90 #include <sys/ioctl.h>
91 #include <sys/timeout.h>
93 #include <sys/sysctl.h>
100 #include <net/if_clone.h>
102 #include <net/if_types.h>
103 #include <net/route.h>
105 #include <net/netisr.h>
107 #include <net/vnet.h>
110 #include <netinet/in.h>
111 #include <netinet/if_ether.h>
112 #include <netinet/tcp.h>
113 #include <netinet/tcp_seq.h>
116 #include <netinet/in_systm.h>
117 #include <netinet/in_var.h>
118 #include <netinet/ip.h>
119 #include <netinet/ip_var.h>
123 #include <netinet6/nd6.h>
127 #include <netinet/ip_carp.h>
131 #include <netinet/ip_carp.h>
135 #include <net/pfvar.h>
136 #include <net/if_pfsync.h>
139 #include "bpfilter.h"
143 #define PFSYNC_MINPKT ( \
144 sizeof(struct ip) + \
145 sizeof(struct pfsync_header) + \
146 sizeof(struct pfsync_subheader) + \
147 sizeof(struct pfsync_eof))
155 int pfsync_input_hmac(struct mbuf *, int);
157 int pfsync_upd_tcp(struct pf_state *, struct pfsync_state_peer *,
158 struct pfsync_state_peer *);
160 int pfsync_in_clr(struct pfsync_pkt *, struct mbuf *, int, int);
161 int pfsync_in_ins(struct pfsync_pkt *, struct mbuf *, int, int);
162 int pfsync_in_iack(struct pfsync_pkt *, struct mbuf *, int, int);
163 int pfsync_in_upd(struct pfsync_pkt *, struct mbuf *, int, int);
164 int pfsync_in_upd_c(struct pfsync_pkt *, struct mbuf *, int, int);
165 int pfsync_in_ureq(struct pfsync_pkt *, struct mbuf *, int, int);
166 int pfsync_in_del(struct pfsync_pkt *, struct mbuf *, int, int);
167 int pfsync_in_del_c(struct pfsync_pkt *, struct mbuf *, int, int);
168 int pfsync_in_bus(struct pfsync_pkt *, struct mbuf *, int, int);
169 int pfsync_in_tdb(struct pfsync_pkt *, struct mbuf *, int, int);
170 int pfsync_in_eof(struct pfsync_pkt *, struct mbuf *, int, int);
172 int pfsync_in_error(struct pfsync_pkt *, struct mbuf *, int, int);
174 int (*pfsync_acts[])(struct pfsync_pkt *, struct mbuf *, int, int) = {
175 pfsync_in_clr, /* PFSYNC_ACT_CLR */
176 pfsync_in_ins, /* PFSYNC_ACT_INS */
177 pfsync_in_iack, /* PFSYNC_ACT_INS_ACK */
178 pfsync_in_upd, /* PFSYNC_ACT_UPD */
179 pfsync_in_upd_c, /* PFSYNC_ACT_UPD_C */
180 pfsync_in_ureq, /* PFSYNC_ACT_UPD_REQ */
181 pfsync_in_del, /* PFSYNC_ACT_DEL */
182 pfsync_in_del_c, /* PFSYNC_ACT_DEL_C */
183 pfsync_in_error, /* PFSYNC_ACT_INS_F */
184 pfsync_in_error, /* PFSYNC_ACT_DEL_F */
185 pfsync_in_bus, /* PFSYNC_ACT_BUS */
186 pfsync_in_tdb, /* PFSYNC_ACT_TDB */
187 pfsync_in_eof /* PFSYNC_ACT_EOF */
191 int (*write)(struct pf_state *, struct mbuf *, int);
196 /* we have one of these for every PFSYNC_S_ */
197 int pfsync_out_state(struct pf_state *, struct mbuf *, int);
198 int pfsync_out_iack(struct pf_state *, struct mbuf *, int);
199 int pfsync_out_upd_c(struct pf_state *, struct mbuf *, int);
200 int pfsync_out_del(struct pf_state *, struct mbuf *, int);
202 struct pfsync_q pfsync_qs[] = {
203 { pfsync_out_state, sizeof(struct pfsync_state), PFSYNC_ACT_INS },
204 { pfsync_out_iack, sizeof(struct pfsync_ins_ack), PFSYNC_ACT_INS_ACK },
205 { pfsync_out_state, sizeof(struct pfsync_state), PFSYNC_ACT_UPD },
206 { pfsync_out_upd_c, sizeof(struct pfsync_upd_c), PFSYNC_ACT_UPD_C },
207 { pfsync_out_del, sizeof(struct pfsync_del_c), PFSYNC_ACT_DEL_C }
210 void pfsync_q_ins(struct pf_state *, int);
211 void pfsync_q_del(struct pf_state *);
213 struct pfsync_upd_req_item {
214 TAILQ_ENTRY(pfsync_upd_req_item) ur_entry;
215 struct pfsync_upd_req ur_msg;
217 TAILQ_HEAD(pfsync_upd_reqs, pfsync_upd_req_item);
219 struct pfsync_deferral {
220 TAILQ_ENTRY(pfsync_deferral) pd_entry;
221 struct pf_state *pd_st;
224 struct callout pd_tmo;
226 struct timeout pd_tmo;
229 TAILQ_HEAD(pfsync_deferrals, pfsync_deferral);
231 #define PFSYNC_PLSIZE MAX(sizeof(struct pfsync_upd_req_item), \
232 sizeof(struct pfsync_deferral))
235 int pfsync_out_tdb(struct tdb *, struct mbuf *, int);
238 struct pfsync_softc {
240 struct ifnet *sc_ifp;
244 struct ifnet *sc_sync_if;
252 struct ip_moptions sc_imo;
254 struct in_addr sc_sync_peer;
255 u_int8_t sc_maxupdates;
260 struct ip sc_template;
262 struct pf_state_queue sc_qs[PFSYNC_S_COUNT];
265 struct pfsync_upd_reqs sc_upd_req_list;
268 struct pfsync_deferrals sc_deferrals;
274 u_int32_t sc_ureq_sent;
277 struct callout sc_bulkfail_tmo;
279 struct timeout sc_bulkfail_tmo;
282 u_int32_t sc_ureq_received;
283 struct pf_state *sc_bulk_next;
284 struct pf_state *sc_bulk_last;
286 struct callout sc_bulk_tmo;
288 struct timeout sc_bulk_tmo;
291 TAILQ_HEAD(, tdb) sc_tdb_q;
294 struct callout sc_tmo;
296 struct timeout sc_tmo;
301 static MALLOC_DEFINE(M_PFSYNC, "pfsync", "pfsync data");
302 static VNET_DEFINE(struct pfsync_softc *, pfsyncif) = NULL;
303 #define V_pfsyncif VNET(pfsyncif)
304 static VNET_DEFINE(void *, pfsync_swi_cookie) = NULL;
305 #define V_pfsync_swi_cookie VNET(pfsync_swi_cookie)
306 static VNET_DEFINE(struct pfsyncstats, pfsyncstats);
307 #define V_pfsyncstats VNET(pfsyncstats)
308 static VNET_DEFINE(int, pfsync_carp_adj) = CARP_MAXSKEW;
309 #define V_pfsync_carp_adj VNET(pfsync_carp_adj)
311 static void pfsyncintr(void *);
312 static int pfsync_multicast_setup(struct pfsync_softc *);
313 static void pfsync_multicast_cleanup(struct pfsync_softc *);
314 static int pfsync_init(void);
315 static void pfsync_uninit(void);
316 static void pfsync_sendout1(int);
318 #define schednetisr(NETISR_PFSYNC) swi_sched(V_pfsync_swi_cookie, 0)
320 SYSCTL_NODE(_net, OID_AUTO, pfsync, CTLFLAG_RW, 0, "PFSYNC");
321 SYSCTL_VNET_STRUCT(_net_pfsync, OID_AUTO, stats, CTLFLAG_RW,
322 &VNET_NAME(pfsyncstats), pfsyncstats,
323 "PFSYNC statistics (struct pfsyncstats, net/if_pfsync.h)");
324 SYSCTL_INT(_net_pfsync, OID_AUTO, carp_demotion_factor, CTLFLAG_RW,
325 &VNET_NAME(pfsync_carp_adj), 0, "pfsync's CARP demotion factor adjustment");
327 struct pfsync_softc *pfsyncif = NULL;
328 struct pfsyncstats pfsyncstats;
329 #define V_pfsyncstats pfsyncstats
332 void pfsyncattach(int);
334 int pfsync_clone_create(struct if_clone *, int, caddr_t);
335 void pfsync_clone_destroy(struct ifnet *);
337 int pfsync_clone_create(struct if_clone *, int);
338 int pfsync_clone_destroy(struct ifnet *);
340 int pfsync_alloc_scrub_memory(struct pfsync_state_peer *,
341 struct pf_state_peer *);
342 void pfsync_update_net_tdb(struct pfsync_tdb *);
343 int pfsyncoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
349 int pfsyncioctl(struct ifnet *, u_long, caddr_t);
350 void pfsyncstart(struct ifnet *);
352 struct mbuf *pfsync_if_dequeue(struct ifnet *);
354 void pfsync_deferred(struct pf_state *, int);
355 void pfsync_undefer(struct pfsync_deferral *, int);
356 void pfsync_defer_tmo(void *);
358 void pfsync_request_update(u_int32_t, u_int64_t);
359 void pfsync_update_state_req(struct pf_state *);
361 void pfsync_drop(struct pfsync_softc *);
362 void pfsync_sendout(void);
363 void pfsync_send_plus(void *, size_t);
364 void pfsync_timeout(void *);
365 void pfsync_tdb_timeout(void *);
367 void pfsync_bulk_start(void);
368 void pfsync_bulk_status(u_int8_t);
369 void pfsync_bulk_update(void *);
370 void pfsync_bulk_fail(void *);
374 #define betoh64 (unsigned long long)be64toh
375 #define timeout_del callout_stop
378 #define PFSYNC_MAX_BULKTRIES 12
384 VNET_DEFINE(struct ifc_simple_data, pfsync_cloner_data);
385 VNET_DEFINE(struct if_clone, pfsync_cloner);
386 #define V_pfsync_cloner_data VNET(pfsync_cloner_data)
387 #define V_pfsync_cloner VNET(pfsync_cloner)
388 IFC_SIMPLE_DECLARE(pfsync, 1);
390 struct if_clone pfsync_cloner =
391 IF_CLONE_INITIALIZER("pfsync", pfsync_clone_create, pfsync_clone_destroy);
395 pfsyncattach(int npfsync)
397 if_clone_attach(&pfsync_cloner);
401 pfsync_clone_create(struct if_clone *ifc, int unit, caddr_t param)
403 pfsync_clone_create(struct if_clone *ifc, int unit)
406 struct pfsync_softc *sc;
414 sc = malloc(sizeof(struct pfsync_softc), M_PFSYNC, M_WAITOK | M_ZERO);
415 sc->pfsync_sync_ok = 1;
418 sc = malloc(sizeof(*pfsyncif), M_DEVBUF, M_NOWAIT | M_ZERO);
421 for (q = 0; q < PFSYNC_S_COUNT; q++)
422 TAILQ_INIT(&sc->sc_qs[q]);
425 sc->sc_pool = uma_zcreate("pfsync", PFSYNC_PLSIZE, NULL, NULL, NULL,
426 NULL, UMA_ALIGN_PTR, 0);
428 pool_init(&sc->sc_pool, PFSYNC_PLSIZE, 0, 0, 0, "pfsync", NULL);
430 TAILQ_INIT(&sc->sc_upd_req_list);
431 TAILQ_INIT(&sc->sc_deferrals);
434 TAILQ_INIT(&sc->sc_tdb_q);
436 sc->sc_len = PFSYNC_MINPKT;
437 sc->sc_maxupdates = 128;
440 sc->sc_imo.imo_membership = (struct in_multi **)malloc(
441 (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_IPMOPTS,
443 sc->sc_imo.imo_max_memberships = IP_MIN_MEMBERSHIPS;
447 ifp = sc->sc_ifp = if_alloc(IFT_PFSYNC);
449 uma_zdestroy(sc->sc_pool);
453 if_initname(ifp, ifc->ifc_name, unit);
456 snprintf(ifp->if_xname, sizeof ifp->if_xname, "pfsync%d", unit);
459 ifp->if_ioctl = pfsyncioctl;
460 ifp->if_output = pfsyncoutput;
461 ifp->if_start = pfsyncstart;
462 ifp->if_type = IFT_PFSYNC;
463 ifp->if_snd.ifq_maxlen = ifqmaxlen;
464 ifp->if_hdrlen = sizeof(struct pfsync_header);
465 ifp->if_mtu = ETHERMTU;
467 callout_init(&sc->sc_tmo, CALLOUT_MPSAFE);
468 callout_init_mtx(&sc->sc_bulk_tmo, &pf_task_mtx, 0);
469 callout_init(&sc->sc_bulkfail_tmo, CALLOUT_MPSAFE);
471 timeout_set(&sc->sc_tmo, pfsync_timeout, sc);
472 timeout_set(&sc->sc_bulk_tmo, pfsync_bulk_update, sc);
473 timeout_set(&sc->sc_bulkfail_tmo, pfsync_bulk_fail, sc);
481 if_addgroup(ifp, "carp");
487 bpfattach(ifp, DLT_PFSYNC, PFSYNC_HDRLEN);
489 bpfattach(&sc->sc_if.if_bpf, ifp, DLT_PFSYNC, PFSYNC_HDRLEN);
507 pfsync_clone_destroy(struct ifnet *ifp)
509 struct pfsync_softc *sc = ifp->if_softc;
514 timeout_del(&sc->sc_bulkfail_tmo);
515 timeout_del(&sc->sc_bulk_tmo);
516 timeout_del(&sc->sc_tmo);
519 if (!sc->pfsync_sync_ok && carp_demote_adj_p)
520 (*carp_demote_adj_p)(-V_pfsync_carp_adj, "pfsync destroy");
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;
795 timeout = r->timeout[sp->timeout];
798 timeout = V_pf_default_rule.timeout[sp->timeout];
800 timeout = pf_default_rule.timeout[sp->timeout];
803 /* sp->expire may have been adaptively scaled by export. */
804 st->expire -= timeout - ntohl(sp->expire);
807 st->direction = sp->direction;
809 st->timeout = sp->timeout;
810 st->state_flags = sp->state_flags;
812 bcopy(sp->id, &st->id, sizeof(st->id));
813 st->creatorid = sp->creatorid;
814 pf_state_peer_ntoh(&sp->src, &st->src);
815 pf_state_peer_ntoh(&sp->dst, &st->dst);
818 st->nat_rule.ptr = NULL;
819 st->anchor.ptr = NULL;
822 st->pfsync_time = time_uptime;
823 st->sync_state = PFSYNC_S_NONE;
825 /* XXX when we have nat_rule/anchors, use STATE_INC_COUNTERS */
829 if (!ISSET(flags, PFSYNC_SI_IOCTL))
830 SET(st->state_flags, PFSTATE_NOSYNC);
832 if ((error = pf_state_insert(kif, skw, sks, st)) != 0) {
833 /* XXX when we have nat_rule/anchors, use STATE_DEC_COUNTERS */
838 if (!ISSET(flags, PFSYNC_SI_IOCTL)) {
839 CLR(st->state_flags, PFSTATE_NOSYNC);
840 if (ISSET(st->state_flags, PFSTATE_ACK)) {
841 pfsync_q_ins(st, PFSYNC_S_IACK);
842 schednetisr(NETISR_PFSYNC);
845 CLR(st->state_flags, PFSTATE_ACK);
855 pool_put(&V_pf_state_key_pl, skw);
857 pool_put(&V_pf_state_key_pl, sks);
860 pool_put(&pf_state_key_pl, skw);
862 pool_put(&pf_state_key_pl, sks);
865 cleanup_state: /* pf_state_insert frees the state keys */
869 pool_put(&V_pf_state_scrub_pl, st->dst.scrub);
871 pool_put(&V_pf_state_scrub_pl, st->src.scrub);
872 pool_put(&V_pf_state_pl, st);
875 pool_put(&pf_state_scrub_pl, st->dst.scrub);
877 pool_put(&pf_state_scrub_pl, st->src.scrub);
878 pool_put(&pf_state_pl, st);
886 pfsync_input(struct mbuf *m, __unused int off)
888 pfsync_input(struct mbuf *m, ...)
892 struct pfsync_softc *sc = V_pfsyncif;
894 struct pfsync_softc *sc = pfsyncif;
896 struct pfsync_pkt pkt;
897 struct ip *ip = mtod(m, struct ip *);
898 struct pfsync_header *ph;
899 struct pfsync_subheader subh;
904 V_pfsyncstats.pfsyncs_ipackets++;
906 /* verify that we have a sync interface configured */
908 if (!sc || !sc->sc_sync_if || !V_pf_status.running)
910 if (!sc || !sc->sc_sync_if || !pf_status.running)
914 /* verify that the packet came in on the right interface */
915 if (sc->sc_sync_if != m->m_pkthdr.rcvif) {
916 V_pfsyncstats.pfsyncs_badif++;
921 sc->sc_ifp->if_ipackets++;
922 sc->sc_ifp->if_ibytes += m->m_pkthdr.len;
924 sc->sc_if.if_ipackets++;
925 sc->sc_if.if_ibytes += m->m_pkthdr.len;
927 /* verify that the IP TTL is 255. */
928 if (ip->ip_ttl != PFSYNC_DFLTTL) {
929 V_pfsyncstats.pfsyncs_badttl++;
933 offset = ip->ip_hl << 2;
934 if (m->m_pkthdr.len < offset + sizeof(*ph)) {
935 V_pfsyncstats.pfsyncs_hdrops++;
939 if (offset + sizeof(*ph) > m->m_len) {
940 if (m_pullup(m, offset + sizeof(*ph)) == NULL) {
941 V_pfsyncstats.pfsyncs_hdrops++;
944 ip = mtod(m, struct ip *);
946 ph = (struct pfsync_header *)((char *)ip + offset);
948 /* verify the version */
949 if (ph->version != PFSYNC_VERSION) {
950 V_pfsyncstats.pfsyncs_badver++;
955 if (pfsync_input_hmac(m, offset) != 0) {
961 /* Cheaper to grab this now than having to mess with mbufs later */
963 pkt.src = ip->ip_src;
967 if (!bcmp(&ph->pfcksum, &V_pf_status.pf_chksum, PF_MD5_DIGEST_LENGTH))
969 if (!bcmp(&ph->pfcksum, &pf_status.pf_chksum, PF_MD5_DIGEST_LENGTH))
971 pkt.flags |= PFSYNC_SI_CKSUM;
973 offset += sizeof(*ph);
975 m_copydata(m, offset, sizeof(subh), (caddr_t)&subh);
976 offset += sizeof(subh);
978 if (subh.action >= PFSYNC_ACT_MAX) {
979 V_pfsyncstats.pfsyncs_badact++;
983 rv = (*pfsync_acts[subh.action])(&pkt, m, offset,
996 pfsync_in_clr(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
998 struct pfsync_clr *clr;
1000 int len = sizeof(*clr) * count;
1003 struct pf_state *st, *nexts;
1004 struct pf_state_key *sk, *nextsk;
1005 struct pf_state_item *si;
1006 u_int32_t creatorid;
1009 mp = m_pulldown(m, offset, len, &offp);
1011 V_pfsyncstats.pfsyncs_badlen++;
1014 clr = (struct pfsync_clr *)(mp->m_data + offp);
1020 for (i = 0; i < count; i++) {
1021 creatorid = clr[i].creatorid;
1023 if (clr[i].ifname[0] == '\0') {
1025 for (st = RB_MIN(pf_state_tree_id, &V_tree_id);
1027 nexts = RB_NEXT(pf_state_tree_id, &V_tree_id, st);
1029 for (st = RB_MIN(pf_state_tree_id, &tree_id);
1031 nexts = RB_NEXT(pf_state_tree_id, &tree_id, st);
1033 if (st->creatorid == creatorid) {
1034 SET(st->state_flags, PFSTATE_NOSYNC);
1035 pf_unlink_state(st);
1039 if (pfi_kif_get(clr[i].ifname) == NULL)
1044 for (sk = RB_MIN(pf_state_tree, &V_pf_statetbl);
1046 for (sk = RB_MIN(pf_state_tree, &pf_statetbl);
1049 nextsk = RB_NEXT(pf_state_tree,
1051 &V_pf_statetbl, sk);
1055 TAILQ_FOREACH(si, &sk->states, entry) {
1056 if (si->s->creatorid == creatorid) {
1057 SET(si->s->state_flags,
1059 pf_unlink_state(si->s);
1074 pfsync_in_ins(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1077 struct pfsync_state *sa, *sp;
1078 int len = sizeof(*sp) * count;
1083 mp = m_pulldown(m, offset, len, &offp);
1085 V_pfsyncstats.pfsyncs_badlen++;
1088 sa = (struct pfsync_state *)(mp->m_data + offp);
1094 for (i = 0; i < count; i++) {
1097 /* check for invalid values */
1098 if (sp->timeout >= PFTM_MAX ||
1099 sp->src.state > PF_TCPS_PROXY_DST ||
1100 sp->dst.state > PF_TCPS_PROXY_DST ||
1101 sp->direction > PF_OUT ||
1102 (sp->af != AF_INET && sp->af != AF_INET6)) {
1104 if (V_pf_status.debug >= PF_DEBUG_MISC) {
1106 if (pf_status.debug >= PF_DEBUG_MISC) {
1108 printf("pfsync_input: PFSYNC5_ACT_INS: "
1111 V_pfsyncstats.pfsyncs_badval++;
1115 if (pfsync_state_import(sp, pkt->flags) == ENOMEM) {
1116 /* drop out, but process the rest of the actions */
1129 pfsync_in_iack(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1131 struct pfsync_ins_ack *ia, *iaa;
1132 struct pf_state_cmp id_key;
1133 struct pf_state *st;
1136 int len = count * sizeof(*ia);
1140 mp = m_pulldown(m, offset, len, &offp);
1142 V_pfsyncstats.pfsyncs_badlen++;
1145 iaa = (struct pfsync_ins_ack *)(mp->m_data + offp);
1151 for (i = 0; i < count; i++) {
1154 bcopy(&ia->id, &id_key.id, sizeof(id_key.id));
1155 id_key.creatorid = ia->creatorid;
1157 st = pf_find_state_byid(&id_key);
1161 if (ISSET(st->state_flags, PFSTATE_ACK))
1162 pfsync_deferred(st, 0);
1169 * XXX this is not yet implemented, but we know the size of the
1170 * message so we can skip it.
1173 return (count * sizeof(struct pfsync_ins_ack));
1177 pfsync_upd_tcp(struct pf_state *st, struct pfsync_state_peer *src,
1178 struct pfsync_state_peer *dst)
1183 * The state should never go backwards except
1184 * for syn-proxy states. Neither should the
1185 * sequence window slide backwards.
1187 if (st->src.state > src->state &&
1188 (st->src.state < PF_TCPS_PROXY_SRC ||
1189 src->state >= PF_TCPS_PROXY_SRC))
1191 else if (SEQ_GT(st->src.seqlo, ntohl(src->seqlo)))
1193 else if (st->dst.state > dst->state) {
1194 /* There might still be useful
1195 * information about the src state here,
1196 * so import that part of the update,
1197 * then "fail" so we send the updated
1198 * state back to the peer who is missing
1199 * our what we know. */
1200 pf_state_peer_ntoh(src, &st->src);
1201 /* XXX do anything with timeouts? */
1203 } else if (st->dst.state >= TCPS_SYN_SENT &&
1204 SEQ_GT(st->dst.seqlo, ntohl(dst->seqlo)))
1211 pfsync_in_upd(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1213 struct pfsync_state *sa, *sp;
1214 struct pf_state_cmp id_key;
1215 struct pf_state_key *sk;
1216 struct pf_state *st;
1220 int len = count * sizeof(*sp);
1224 mp = m_pulldown(m, offset, len, &offp);
1226 V_pfsyncstats.pfsyncs_badlen++;
1229 sa = (struct pfsync_state *)(mp->m_data + offp);
1235 for (i = 0; i < count; i++) {
1238 /* check for invalid values */
1239 if (sp->timeout >= PFTM_MAX ||
1240 sp->src.state > PF_TCPS_PROXY_DST ||
1241 sp->dst.state > PF_TCPS_PROXY_DST) {
1243 if (V_pf_status.debug >= PF_DEBUG_MISC) {
1245 if (pf_status.debug >= PF_DEBUG_MISC) {
1247 printf("pfsync_input: PFSYNC_ACT_UPD: "
1250 V_pfsyncstats.pfsyncs_badval++;
1254 bcopy(sp->id, &id_key.id, sizeof(id_key.id));
1255 id_key.creatorid = sp->creatorid;
1257 st = pf_find_state_byid(&id_key);
1259 /* insert the update */
1260 if (pfsync_state_import(sp, 0))
1261 V_pfsyncstats.pfsyncs_badstate++;
1265 if (ISSET(st->state_flags, PFSTATE_ACK))
1266 pfsync_deferred(st, 1);
1268 sk = st->key[PF_SK_WIRE]; /* XXX right one? */
1270 if (sk->proto == IPPROTO_TCP)
1271 sfail = pfsync_upd_tcp(st, &sp->src, &sp->dst);
1274 * Non-TCP protocol state machine always go
1277 if (st->src.state > sp->src.state)
1279 else if (st->dst.state > sp->dst.state)
1285 if (V_pf_status.debug >= PF_DEBUG_MISC) {
1287 if (pf_status.debug >= PF_DEBUG_MISC) {
1289 printf("pfsync: %s stale update (%d)"
1290 " id: %016llx creatorid: %08x\n",
1291 (sfail < 7 ? "ignoring" : "partial"),
1292 sfail, betoh64(st->id),
1293 ntohl(st->creatorid));
1295 V_pfsyncstats.pfsyncs_stale++;
1297 pfsync_update_state(st);
1298 schednetisr(NETISR_PFSYNC);
1301 pfsync_alloc_scrub_memory(&sp->dst, &st->dst);
1302 pf_state_peer_ntoh(&sp->src, &st->src);
1303 pf_state_peer_ntoh(&sp->dst, &st->dst);
1304 st->expire = time_second;
1305 st->timeout = sp->timeout;
1306 st->pfsync_time = time_uptime;
1317 pfsync_in_upd_c(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1319 struct pfsync_upd_c *ua, *up;
1320 struct pf_state_key *sk;
1321 struct pf_state_cmp id_key;
1322 struct pf_state *st;
1324 int len = count * sizeof(*up);
1331 mp = m_pulldown(m, offset, len, &offp);
1333 V_pfsyncstats.pfsyncs_badlen++;
1336 ua = (struct pfsync_upd_c *)(mp->m_data + offp);
1342 for (i = 0; i < count; i++) {
1345 /* check for invalid values */
1346 if (up->timeout >= PFTM_MAX ||
1347 up->src.state > PF_TCPS_PROXY_DST ||
1348 up->dst.state > PF_TCPS_PROXY_DST) {
1350 if (V_pf_status.debug >= PF_DEBUG_MISC) {
1352 if (pf_status.debug >= PF_DEBUG_MISC) {
1354 printf("pfsync_input: "
1355 "PFSYNC_ACT_UPD_C: "
1358 V_pfsyncstats.pfsyncs_badval++;
1362 bcopy(&up->id, &id_key.id, sizeof(id_key.id));
1363 id_key.creatorid = up->creatorid;
1365 st = pf_find_state_byid(&id_key);
1367 /* We don't have this state. Ask for it. */
1368 pfsync_request_update(id_key.creatorid, id_key.id);
1372 if (ISSET(st->state_flags, PFSTATE_ACK))
1373 pfsync_deferred(st, 1);
1375 sk = st->key[PF_SK_WIRE]; /* XXX right one? */
1377 if (sk->proto == IPPROTO_TCP)
1378 sfail = pfsync_upd_tcp(st, &up->src, &up->dst);
1381 * Non-TCP protocol state machine always go forwards
1383 if (st->src.state > up->src.state)
1385 else if (st->dst.state > up->dst.state)
1391 if (V_pf_status.debug >= PF_DEBUG_MISC) {
1393 if (pf_status.debug >= PF_DEBUG_MISC) {
1395 printf("pfsync: ignoring stale update "
1397 "creatorid: %08x\n", sfail,
1399 ntohl(st->creatorid));
1401 V_pfsyncstats.pfsyncs_stale++;
1403 pfsync_update_state(st);
1404 schednetisr(NETISR_PFSYNC);
1407 pfsync_alloc_scrub_memory(&up->dst, &st->dst);
1408 pf_state_peer_ntoh(&up->src, &st->src);
1409 pf_state_peer_ntoh(&up->dst, &st->dst);
1410 st->expire = time_second;
1411 st->timeout = up->timeout;
1412 st->pfsync_time = time_uptime;
1423 pfsync_in_ureq(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1425 struct pfsync_upd_req *ur, *ura;
1427 int len = count * sizeof(*ur);
1430 struct pf_state_cmp id_key;
1431 struct pf_state *st;
1433 mp = m_pulldown(m, offset, len, &offp);
1435 V_pfsyncstats.pfsyncs_badlen++;
1438 ura = (struct pfsync_upd_req *)(mp->m_data + offp);
1443 for (i = 0; i < count; i++) {
1446 bcopy(&ur->id, &id_key.id, sizeof(id_key.id));
1447 id_key.creatorid = ur->creatorid;
1449 if (id_key.id == 0 && id_key.creatorid == 0)
1450 pfsync_bulk_start();
1452 st = pf_find_state_byid(&id_key);
1454 V_pfsyncstats.pfsyncs_badstate++;
1457 if (ISSET(st->state_flags, PFSTATE_NOSYNC))
1460 pfsync_update_state_req(st);
1471 pfsync_in_del(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1474 struct pfsync_state *sa, *sp;
1475 struct pf_state_cmp id_key;
1476 struct pf_state *st;
1477 int len = count * sizeof(*sp);
1481 mp = m_pulldown(m, offset, len, &offp);
1483 V_pfsyncstats.pfsyncs_badlen++;
1486 sa = (struct pfsync_state *)(mp->m_data + offp);
1492 for (i = 0; i < count; i++) {
1495 bcopy(sp->id, &id_key.id, sizeof(id_key.id));
1496 id_key.creatorid = sp->creatorid;
1498 st = pf_find_state_byid(&id_key);
1500 V_pfsyncstats.pfsyncs_badstate++;
1503 SET(st->state_flags, PFSTATE_NOSYNC);
1504 pf_unlink_state(st);
1515 pfsync_in_del_c(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1518 struct pfsync_del_c *sa, *sp;
1519 struct pf_state_cmp id_key;
1520 struct pf_state *st;
1521 int len = count * sizeof(*sp);
1525 mp = m_pulldown(m, offset, len, &offp);
1527 V_pfsyncstats.pfsyncs_badlen++;
1530 sa = (struct pfsync_del_c *)(mp->m_data + offp);
1536 for (i = 0; i < count; i++) {
1539 bcopy(&sp->id, &id_key.id, sizeof(id_key.id));
1540 id_key.creatorid = sp->creatorid;
1542 st = pf_find_state_byid(&id_key);
1544 V_pfsyncstats.pfsyncs_badstate++;
1548 SET(st->state_flags, PFSTATE_NOSYNC);
1549 pf_unlink_state(st);
1560 pfsync_in_bus(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1563 struct pfsync_softc *sc = V_pfsyncif;
1565 struct pfsync_softc *sc = pfsyncif;
1567 struct pfsync_bus *bus;
1569 int len = count * sizeof(*bus);
1572 /* If we're not waiting for a bulk update, who cares. */
1573 if (sc->sc_ureq_sent == 0)
1576 mp = m_pulldown(m, offset, len, &offp);
1578 V_pfsyncstats.pfsyncs_badlen++;
1581 bus = (struct pfsync_bus *)(mp->m_data + offp);
1583 switch (bus->status) {
1584 case PFSYNC_BUS_START:
1586 callout_reset(&sc->sc_bulkfail_tmo, 4 * hz +
1587 V_pf_pool_limits[PF_LIMIT_STATES].limit /
1588 ((sc->sc_ifp->if_mtu - PFSYNC_MINPKT) /
1589 sizeof(struct pfsync_state)),
1590 pfsync_bulk_fail, V_pfsyncif);
1592 timeout_add(&sc->sc_bulkfail_tmo, 4 * hz +
1593 pf_pool_limits[PF_LIMIT_STATES].limit /
1594 ((sc->sc_if.if_mtu - PFSYNC_MINPKT) /
1595 sizeof(struct pfsync_state)));
1598 if (V_pf_status.debug >= PF_DEBUG_MISC)
1600 if (pf_status.debug >= PF_DEBUG_MISC)
1602 printf("pfsync: received bulk update start\n");
1605 case PFSYNC_BUS_END:
1606 if (time_uptime - ntohl(bus->endtime) >=
1608 /* that's it, we're happy */
1609 sc->sc_ureq_sent = 0;
1610 sc->sc_bulk_tries = 0;
1611 timeout_del(&sc->sc_bulkfail_tmo);
1613 if (!sc->pfsync_sync_ok && carp_demote_adj_p)
1614 (*carp_demote_adj_p)(-V_pfsync_carp_adj,
1615 "pfsync bulk done");
1616 sc->pfsync_sync_ok = 1;
1619 if (!pfsync_sync_ok)
1620 carp_group_demote_adj(&sc->sc_if, -1);
1625 if (V_pf_status.debug >= PF_DEBUG_MISC)
1627 if (pf_status.debug >= PF_DEBUG_MISC)
1629 printf("pfsync: received valid "
1630 "bulk update end\n");
1633 if (V_pf_status.debug >= PF_DEBUG_MISC)
1635 if (pf_status.debug >= PF_DEBUG_MISC)
1637 printf("pfsync: received invalid "
1638 "bulk update end: bad timestamp\n");
1647 pfsync_in_tdb(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1649 int len = count * sizeof(struct pfsync_tdb);
1652 struct pfsync_tdb *tp;
1658 mp = m_pulldown(m, offset, len, &offp);
1660 V_pfsyncstats.pfsyncs_badlen++;
1663 tp = (struct pfsync_tdb *)(mp->m_data + offp);
1669 for (i = 0; i < count; i++)
1670 pfsync_update_net_tdb(&tp[i]);
1681 /* Update an in-kernel tdb. Silently fail if no tdb is found. */
1683 pfsync_update_net_tdb(struct pfsync_tdb *pt)
1688 /* check for invalid values */
1689 if (ntohl(pt->spi) <= SPI_RESERVED_MAX ||
1690 (pt->dst.sa.sa_family != AF_INET &&
1691 pt->dst.sa.sa_family != AF_INET6))
1695 tdb = gettdb(pt->spi, &pt->dst, pt->sproto);
1697 pt->rpl = ntohl(pt->rpl);
1698 pt->cur_bytes = betoh64(pt->cur_bytes);
1700 /* Neither replay nor byte counter should ever decrease. */
1701 if (pt->rpl < tdb->tdb_rpl ||
1702 pt->cur_bytes < tdb->tdb_cur_bytes) {
1707 tdb->tdb_rpl = pt->rpl;
1708 tdb->tdb_cur_bytes = pt->cur_bytes;
1715 if (V_pf_status.debug >= PF_DEBUG_MISC)
1717 if (pf_status.debug >= PF_DEBUG_MISC)
1719 printf("pfsync_insert: PFSYNC_ACT_TDB_UPD: "
1721 V_pfsyncstats.pfsyncs_badstate++;
1728 pfsync_in_eof(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1730 /* check if we are at the right place in the packet */
1731 if (offset != m->m_pkthdr.len - sizeof(struct pfsync_eof))
1732 V_pfsyncstats.pfsyncs_badact++;
1734 /* we're done. free and let the caller return */
1740 pfsync_in_error(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1742 V_pfsyncstats.pfsyncs_badact++;
1749 pfsyncoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
1762 pfsyncioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1765 struct proc *p = curproc;
1767 struct pfsync_softc *sc = ifp->if_softc;
1768 struct ifreq *ifr = (struct ifreq *)data;
1769 struct ip_moptions *imo = &sc->sc_imo;
1770 struct pfsyncreq pfsyncr;
1779 case SIOCSIFDSTADDR:
1783 if (ifp->if_flags & IFF_UP)
1784 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1786 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1788 if (ifp->if_flags & IFF_UP)
1789 ifp->if_flags |= IFF_RUNNING;
1791 ifp->if_flags &= ~IFF_RUNNING;
1795 if (!sc->sc_sync_if ||
1796 ifr->ifr_mtu <= PFSYNC_MINPKT ||
1797 ifr->ifr_mtu > sc->sc_sync_if->if_mtu)
1799 if (ifr->ifr_mtu < ifp->if_mtu) {
1810 ifp->if_mtu = ifr->ifr_mtu;
1813 bzero(&pfsyncr, sizeof(pfsyncr));
1814 if (sc->sc_sync_if) {
1815 strlcpy(pfsyncr.pfsyncr_syncdev,
1816 sc->sc_sync_if->if_xname, IFNAMSIZ);
1818 pfsyncr.pfsyncr_syncpeer = sc->sc_sync_peer;
1819 pfsyncr.pfsyncr_maxupdates = sc->sc_maxupdates;
1820 pfsyncr.pfsyncr_defer = sc->sc_defer;
1821 return (copyout(&pfsyncr, ifr->ifr_data, sizeof(pfsyncr)));
1825 if ((error = priv_check(curthread, PRIV_NETINET_PF)) != 0)
1827 if ((error = suser(p, p->p_acflag)) != 0)
1830 if ((error = copyin(ifr->ifr_data, &pfsyncr, sizeof(pfsyncr))))
1836 if (pfsyncr.pfsyncr_syncpeer.s_addr == 0)
1838 sc->sc_sync_peer.s_addr = htonl(INADDR_PFSYNC_GROUP);
1840 sc->sc_sync_peer.s_addr = INADDR_PFSYNC_GROUP;
1843 sc->sc_sync_peer.s_addr =
1844 pfsyncr.pfsyncr_syncpeer.s_addr;
1846 if (pfsyncr.pfsyncr_maxupdates > 255)
1855 sc->sc_maxupdates = pfsyncr.pfsyncr_maxupdates;
1856 sc->sc_defer = pfsyncr.pfsyncr_defer;
1858 if (pfsyncr.pfsyncr_syncdev[0] == 0) {
1859 sc->sc_sync_if = NULL;
1862 if (imo->imo_membership)
1863 pfsync_multicast_cleanup(sc);
1865 if (imo->imo_num_memberships > 0) {
1866 in_delmulti(imo->imo_membership[
1867 --imo->imo_num_memberships]);
1868 imo->imo_multicast_ifp = NULL;
1877 if ((sifp = ifunit(pfsyncr.pfsyncr_syncdev)) == NULL)
1885 if (sifp->if_mtu < sc->sc_ifp->if_mtu ||
1887 if (sifp->if_mtu < sc->sc_if.if_mtu ||
1889 (sc->sc_sync_if != NULL &&
1890 sifp->if_mtu < sc->sc_sync_if->if_mtu) ||
1891 sifp->if_mtu < MCLBYTES - sizeof(struct ip))
1893 sc->sc_sync_if = sifp;
1896 if (imo->imo_membership) {
1898 pfsync_multicast_cleanup(sc);
1902 if (imo->imo_num_memberships > 0) {
1903 in_delmulti(imo->imo_membership[--imo->imo_num_memberships]);
1904 imo->imo_multicast_ifp = NULL;
1909 if (sc->sc_sync_if &&
1910 sc->sc_sync_peer.s_addr == htonl(INADDR_PFSYNC_GROUP)) {
1912 error = pfsync_multicast_setup(sc);
1918 if (sc->sc_sync_if &&
1919 sc->sc_sync_peer.s_addr == INADDR_PFSYNC_GROUP) {
1920 struct in_addr addr;
1922 if (!(sc->sc_sync_if->if_flags & IFF_MULTICAST)) {
1923 sc->sc_sync_if = NULL;
1925 return (EADDRNOTAVAIL);
1928 addr.s_addr = INADDR_PFSYNC_GROUP;
1930 if ((imo->imo_membership[0] =
1931 in_addmulti(&addr, sc->sc_sync_if)) == NULL) {
1932 sc->sc_sync_if = NULL;
1936 imo->imo_num_memberships++;
1937 imo->imo_multicast_ifp = sc->sc_sync_if;
1938 imo->imo_multicast_ttl = PFSYNC_DFLTTL;
1939 imo->imo_multicast_loop = 0;
1941 #endif /* !__FreeBSD__ */
1943 ip = &sc->sc_template;
1944 bzero(ip, sizeof(*ip));
1945 ip->ip_v = IPVERSION;
1946 ip->ip_hl = sizeof(sc->sc_template) >> 2;
1947 ip->ip_tos = IPTOS_LOWDELAY;
1948 /* len and id are set later */
1952 ip->ip_off = htons(IP_DF);
1954 ip->ip_ttl = PFSYNC_DFLTTL;
1955 ip->ip_p = IPPROTO_PFSYNC;
1956 ip->ip_src.s_addr = INADDR_ANY;
1957 ip->ip_dst.s_addr = sc->sc_sync_peer.s_addr;
1959 if (sc->sc_sync_if) {
1960 /* Request a full state table update. */
1961 sc->sc_ureq_sent = time_uptime;
1963 if (sc->pfsync_sync_ok && carp_demote_adj_p)
1964 (*carp_demote_adj_p)(V_pfsync_carp_adj,
1965 "pfsync bulk start");
1966 sc->pfsync_sync_ok = 0;
1970 carp_group_demote_adj(&sc->sc_if, 1);
1975 if (V_pf_status.debug >= PF_DEBUG_MISC)
1977 if (pf_status.debug >= PF_DEBUG_MISC)
1979 printf("pfsync: requesting bulk update\n");
1981 callout_reset(&sc->sc_bulkfail_tmo, 5 * hz,
1982 pfsync_bulk_fail, V_pfsyncif);
1984 timeout_add_sec(&sc->sc_bulkfail_tmo, 5);
1986 pfsync_request_update(0, 0);
2003 pfsync_out_state(struct pf_state *st, struct mbuf *m, int offset)
2005 struct pfsync_state *sp = (struct pfsync_state *)(m->m_data + offset);
2007 pfsync_state_export(sp, st);
2009 return (sizeof(*sp));
2013 pfsync_out_iack(struct pf_state *st, struct mbuf *m, int offset)
2015 struct pfsync_ins_ack *iack =
2016 (struct pfsync_ins_ack *)(m->m_data + offset);
2019 iack->creatorid = st->creatorid;
2021 return (sizeof(*iack));
2025 pfsync_out_upd_c(struct pf_state *st, struct mbuf *m, int offset)
2027 struct pfsync_upd_c *up = (struct pfsync_upd_c *)(m->m_data + offset);
2029 bzero(up, sizeof(*up));
2031 pf_state_peer_hton(&st->src, &up->src);
2032 pf_state_peer_hton(&st->dst, &up->dst);
2033 up->creatorid = st->creatorid;
2034 up->timeout = st->timeout;
2036 return (sizeof(*up));
2040 pfsync_out_del(struct pf_state *st, struct mbuf *m, int offset)
2042 struct pfsync_del_c *dp = (struct pfsync_del_c *)(m->m_data + offset);
2045 dp->creatorid = st->creatorid;
2047 SET(st->state_flags, PFSTATE_NOSYNC);
2049 return (sizeof(*dp));
2053 pfsync_drop(struct pfsync_softc *sc)
2055 struct pf_state *st;
2056 struct pfsync_upd_req_item *ur;
2062 for (q = 0; q < PFSYNC_S_COUNT; q++) {
2063 if (TAILQ_EMPTY(&sc->sc_qs[q]))
2066 TAILQ_FOREACH(st, &sc->sc_qs[q], sync_list) {
2069 KASSERT(st->sync_state == q,
2070 ("%s: st->sync_state == q",
2073 KASSERT(st->sync_state == q);
2076 st->sync_state = PFSYNC_S_NONE;
2078 TAILQ_INIT(&sc->sc_qs[q]);
2081 while ((ur = TAILQ_FIRST(&sc->sc_upd_req_list)) != NULL) {
2082 TAILQ_REMOVE(&sc->sc_upd_req_list, ur, ur_entry);
2083 pool_put(&sc->sc_pool, ur);
2089 if (!TAILQ_EMPTY(&sc->sc_tdb_q)) {
2090 TAILQ_FOREACH(t, &sc->sc_tdb_q, tdb_sync_entry)
2091 CLR(t->tdb_flags, TDBF_PFSYNC);
2093 TAILQ_INIT(&sc->sc_tdb_q);
2097 sc->sc_len = PFSYNC_MINPKT;
2101 void pfsync_sendout()
2107 pfsync_sendout1(int schedswi)
2109 struct pfsync_softc *sc = V_pfsyncif;
2112 pfsync_sendout(void)
2114 struct pfsync_softc *sc = pfsyncif;
2118 struct ifnet *ifp = sc->sc_ifp;
2120 struct ifnet *ifp = &sc->sc_if;
2125 struct pfsync_header *ph;
2126 struct pfsync_subheader *subh;
2127 struct pf_state *st;
2128 struct pfsync_upd_req_item *ur;
2141 if (sc == NULL || sc->sc_len == PFSYNC_MINPKT)
2145 if (ifp->if_bpf == NULL && sc->sc_sync_if == NULL) {
2147 if (sc->sc_sync_if == NULL) {
2154 m = m_get2(M_NOWAIT, MT_DATA, M_PKTHDR, max_linkhdr + sc->sc_len);
2156 sc->sc_ifp->if_oerrors++;
2157 V_pfsyncstats.pfsyncs_onomem++;
2161 MGETHDR(m, M_DONTWAIT, MT_DATA);
2163 sc->sc_if.if_oerrors++;
2164 pfsyncstats.pfsyncs_onomem++;
2169 if (max_linkhdr + sc->sc_len > MHLEN) {
2170 MCLGETI(m, M_DONTWAIT, NULL, max_linkhdr + sc->sc_len);
2171 if (!ISSET(m->m_flags, M_EXT)) {
2173 sc->sc_if.if_oerrors++;
2174 pfsyncstats.pfsyncs_onomem++;
2180 m->m_data += max_linkhdr;
2181 m->m_len = m->m_pkthdr.len = sc->sc_len;
2183 /* build the ip header */
2184 ip = (struct ip *)m->m_data;
2185 bcopy(&sc->sc_template, ip, sizeof(*ip));
2186 offset = sizeof(*ip);
2189 ip->ip_len = m->m_pkthdr.len;
2191 ip->ip_len = htons(m->m_pkthdr.len);
2193 ip->ip_id = htons(ip_randomid());
2195 /* build the pfsync header */
2196 ph = (struct pfsync_header *)(m->m_data + offset);
2197 bzero(ph, sizeof(*ph));
2198 offset += sizeof(*ph);
2200 ph->version = PFSYNC_VERSION;
2201 ph->len = htons(sc->sc_len - sizeof(*ip));
2203 bcopy(V_pf_status.pf_chksum, ph->pfcksum, PF_MD5_DIGEST_LENGTH);
2205 bcopy(pf_status.pf_chksum, ph->pfcksum, PF_MD5_DIGEST_LENGTH);
2208 /* walk the queues */
2209 for (q = 0; q < PFSYNC_S_COUNT; q++) {
2210 if (TAILQ_EMPTY(&sc->sc_qs[q]))
2213 subh = (struct pfsync_subheader *)(m->m_data + offset);
2214 offset += sizeof(*subh);
2217 TAILQ_FOREACH(st, &sc->sc_qs[q], sync_list) {
2220 KASSERT(st->sync_state == q,
2221 ("%s: st->sync_state == q",
2224 KASSERT(st->sync_state == q);
2228 offset += pfsync_qs[q].write(st, m, offset);
2229 st->sync_state = PFSYNC_S_NONE;
2232 TAILQ_INIT(&sc->sc_qs[q]);
2234 bzero(subh, sizeof(*subh));
2235 subh->action = pfsync_qs[q].action;
2236 subh->count = htons(count);
2239 if (!TAILQ_EMPTY(&sc->sc_upd_req_list)) {
2240 subh = (struct pfsync_subheader *)(m->m_data + offset);
2241 offset += sizeof(*subh);
2244 while ((ur = TAILQ_FIRST(&sc->sc_upd_req_list)) != NULL) {
2245 TAILQ_REMOVE(&sc->sc_upd_req_list, ur, ur_entry);
2247 bcopy(&ur->ur_msg, m->m_data + offset,
2248 sizeof(ur->ur_msg));
2249 offset += sizeof(ur->ur_msg);
2251 pool_put(&sc->sc_pool, ur);
2256 bzero(subh, sizeof(*subh));
2257 subh->action = PFSYNC_ACT_UPD_REQ;
2258 subh->count = htons(count);
2261 /* has someone built a custom region for us to add? */
2262 if (sc->sc_plus != NULL) {
2263 bcopy(sc->sc_plus, m->m_data + offset, sc->sc_pluslen);
2264 offset += sc->sc_pluslen;
2270 if (!TAILQ_EMPTY(&sc->sc_tdb_q)) {
2271 subh = (struct pfsync_subheader *)(m->m_data + offset);
2272 offset += sizeof(*subh);
2275 TAILQ_FOREACH(t, &sc->sc_tdb_q, tdb_sync_entry) {
2276 offset += pfsync_out_tdb(t, m, offset);
2277 CLR(t->tdb_flags, TDBF_PFSYNC);
2281 TAILQ_INIT(&sc->sc_tdb_q);
2283 bzero(subh, sizeof(*subh));
2284 subh->action = PFSYNC_ACT_TDB;
2285 subh->count = htons(count);
2289 subh = (struct pfsync_subheader *)(m->m_data + offset);
2290 offset += sizeof(*subh);
2292 bzero(subh, sizeof(*subh));
2293 subh->action = PFSYNC_ACT_EOF;
2294 subh->count = htons(1);
2296 /* XXX write checksum in EOF here */
2298 /* we're done, let's put it on the wire */
2301 m->m_data += sizeof(*ip);
2302 m->m_len = m->m_pkthdr.len = sc->sc_len - sizeof(*ip);
2306 bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_OUT);
2308 m->m_data -= sizeof(*ip);
2309 m->m_len = m->m_pkthdr.len = sc->sc_len;
2312 if (sc->sc_sync_if == NULL) {
2313 sc->sc_len = PFSYNC_MINPKT;
2320 sc->sc_ifp->if_opackets++;
2321 sc->sc_ifp->if_obytes += m->m_pkthdr.len;
2322 sc->sc_len = PFSYNC_MINPKT;
2324 if (!_IF_QFULL(&sc->sc_ifp->if_snd))
2325 _IF_ENQUEUE(&sc->sc_ifp->if_snd, m);
2328 sc->sc_ifp->if_snd.ifq_drops++;
2331 swi_sched(V_pfsync_swi_cookie, 0);
2333 sc->sc_if.if_opackets++;
2334 sc->sc_if.if_obytes += m->m_pkthdr.len;
2336 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL) == 0)
2337 pfsyncstats.pfsyncs_opackets++;
2339 pfsyncstats.pfsyncs_oerrors++;
2342 sc->sc_len = PFSYNC_MINPKT;
2347 pfsync_insert_state(struct pf_state *st)
2350 struct pfsync_softc *sc = V_pfsyncif;
2352 struct pfsync_softc *sc = pfsyncif;
2358 splassert(IPL_SOFTNET);
2361 if (ISSET(st->rule.ptr->rule_flag, PFRULE_NOSYNC) ||
2362 st->key[PF_SK_WIRE]->proto == IPPROTO_PFSYNC) {
2363 SET(st->state_flags, PFSTATE_NOSYNC);
2367 if (sc == NULL || ISSET(st->state_flags, PFSTATE_NOSYNC))
2372 KASSERT(st->sync_state == PFSYNC_S_NONE,
2373 ("%s: st->sync_state == PFSYNC_S_NONE", __FUNCTION__));
2375 KASSERT(st->sync_state == PFSYNC_S_NONE);
2379 if (sc->sc_len == PFSYNC_MINPKT)
2381 callout_reset(&sc->sc_tmo, 1 * hz, pfsync_timeout,
2384 timeout_add_sec(&sc->sc_tmo, 1);
2387 pfsync_q_ins(st, PFSYNC_S_INS);
2389 st->sync_updates = 0;
2395 pfsync_defer(struct pf_state *st, struct mbuf *m)
2398 struct pfsync_softc *sc = V_pfsyncif;
2400 struct pfsync_softc *sc = pfsyncif;
2402 struct pfsync_deferral *pd;
2407 splassert(IPL_SOFTNET);
2410 if (!sc->sc_defer || m->m_flags & (M_BCAST|M_MCAST))
2413 if (sc->sc_deferred >= 128)
2414 pfsync_undefer(TAILQ_FIRST(&sc->sc_deferrals), 0);
2416 pd = pool_get(&sc->sc_pool, M_NOWAIT);
2422 m->m_flags |= M_SKIP_FIREWALL;
2424 m->m_pkthdr.pf.flags |= PF_TAG_GENERATED;
2426 SET(st->state_flags, PFSTATE_ACK);
2431 TAILQ_INSERT_TAIL(&sc->sc_deferrals, pd, pd_entry);
2433 callout_init(&pd->pd_tmo, CALLOUT_MPSAFE);
2434 callout_reset(&pd->pd_tmo, defer, pfsync_defer_tmo,
2437 timeout_set(&pd->pd_tmo, pfsync_defer_tmo, pd);
2438 timeout_add(&pd->pd_tmo, defer);
2441 swi_sched(V_pfsync_swi_cookie, 0);
2447 pfsync_undefer(struct pfsync_deferral *pd, int drop)
2450 struct pfsync_softc *sc = V_pfsyncif;
2452 struct pfsync_softc *sc = pfsyncif;
2459 splassert(IPL_SOFTNET);
2462 TAILQ_REMOVE(&sc->sc_deferrals, pd, pd_entry);
2465 CLR(pd->pd_st->state_flags, PFSTATE_ACK);
2466 timeout_del(&pd->pd_tmo); /* bah */
2472 /* XXX: use pf_defered?! */
2475 ip_output(pd->pd_m, (void *)NULL, (void *)NULL, 0,
2476 (void *)NULL, (void *)NULL);
2483 pool_put(&sc->sc_pool, pd);
2487 pfsync_defer_tmo(void *arg)
2489 #if defined(__FreeBSD__) && defined(VIMAGE)
2490 struct pfsync_deferral *pd = arg;
2496 CURVNET_SET(pd->pd_m->m_pkthdr.rcvif->if_vnet); /* XXX */
2499 pfsync_undefer(arg, 0);
2508 pfsync_deferred(struct pf_state *st, int drop)
2511 struct pfsync_softc *sc = V_pfsyncif;
2513 struct pfsync_softc *sc = pfsyncif;
2515 struct pfsync_deferral *pd;
2517 TAILQ_FOREACH(pd, &sc->sc_deferrals, pd_entry) {
2518 if (pd->pd_st == st) {
2519 pfsync_undefer(pd, drop);
2524 panic("pfsync_send_deferred: unable to find deferred state");
2527 u_int pfsync_upds = 0;
2530 pfsync_update_state(struct pf_state *st)
2533 struct pfsync_softc *sc = V_pfsyncif;
2535 struct pfsync_softc *sc = pfsyncif;
2542 splassert(IPL_SOFTNET);
2548 if (ISSET(st->state_flags, PFSTATE_ACK))
2549 pfsync_deferred(st, 0);
2550 if (ISSET(st->state_flags, PFSTATE_NOSYNC)) {
2551 if (st->sync_state != PFSYNC_S_NONE)
2556 if (sc->sc_len == PFSYNC_MINPKT)
2558 callout_reset(&sc->sc_tmo, 1 * hz, pfsync_timeout,
2561 timeout_add_sec(&sc->sc_tmo, 1);
2564 switch (st->sync_state) {
2565 case PFSYNC_S_UPD_C:
2568 /* we're already handling it */
2570 if (st->key[PF_SK_WIRE]->proto == IPPROTO_TCP) {
2572 if (st->sync_updates >= sc->sc_maxupdates)
2580 pfsync_q_ins(st, PFSYNC_S_UPD_C);
2581 st->sync_updates = 0;
2585 panic("pfsync_update_state: unexpected sync state %d",
2589 if (sync || (time_uptime - st->pfsync_time) < 2) {
2591 schednetisr(NETISR_PFSYNC);
2596 pfsync_request_update(u_int32_t creatorid, u_int64_t id)
2599 struct pfsync_softc *sc = V_pfsyncif;
2601 struct pfsync_softc *sc = pfsyncif;
2603 struct pfsync_upd_req_item *item;
2604 size_t nlen = sizeof(struct pfsync_upd_req);
2610 * this code does nothing to prevent multiple update requests for the
2611 * same state being generated.
2614 item = pool_get(&sc->sc_pool, PR_NOWAIT);
2620 item->ur_msg.id = id;
2621 item->ur_msg.creatorid = creatorid;
2623 if (TAILQ_EMPTY(&sc->sc_upd_req_list))
2624 nlen += sizeof(struct pfsync_subheader);
2627 if (sc->sc_len + nlen > sc->sc_ifp->if_mtu) {
2629 if (sc->sc_len + nlen > sc->sc_if.if_mtu) {
2635 nlen = sizeof(struct pfsync_subheader) +
2636 sizeof(struct pfsync_upd_req);
2639 TAILQ_INSERT_TAIL(&sc->sc_upd_req_list, item, ur_entry);
2642 schednetisr(NETISR_PFSYNC);
2646 pfsync_update_state_req(struct pf_state *st)
2649 struct pfsync_softc *sc = V_pfsyncif;
2651 struct pfsync_softc *sc = pfsyncif;
2657 panic("pfsync_update_state_req: nonexistant instance");
2659 if (ISSET(st->state_flags, PFSTATE_NOSYNC)) {
2660 if (st->sync_state != PFSYNC_S_NONE)
2665 switch (st->sync_state) {
2666 case PFSYNC_S_UPD_C:
2670 pfsync_q_ins(st, PFSYNC_S_UPD);
2671 schednetisr(NETISR_PFSYNC);
2677 /* we're already handling it */
2681 panic("pfsync_update_state_req: unexpected sync state %d",
2687 pfsync_delete_state(struct pf_state *st)
2690 struct pfsync_softc *sc = V_pfsyncif;
2692 struct pfsync_softc *sc = pfsyncif;
2698 splassert(IPL_SOFTNET);
2704 if (ISSET(st->state_flags, PFSTATE_ACK))
2705 pfsync_deferred(st, 1);
2706 if (ISSET(st->state_flags, PFSTATE_NOSYNC)) {
2707 if (st->sync_state != PFSYNC_S_NONE)
2712 if (sc->sc_len == PFSYNC_MINPKT)
2714 callout_reset(&sc->sc_tmo, 1 * hz, pfsync_timeout,
2717 timeout_add_sec(&sc->sc_tmo, 1);
2720 switch (st->sync_state) {
2722 /* we never got to tell the world so just forget about it */
2726 case PFSYNC_S_UPD_C:
2730 /* FALLTHROUGH to putting it on the del list */
2733 pfsync_q_ins(st, PFSYNC_S_DEL);
2737 panic("pfsync_delete_state: unexpected sync state %d",
2743 pfsync_clear_states(u_int32_t creatorid, const char *ifname)
2746 struct pfsync_subheader subh;
2747 struct pfsync_clr clr;
2751 struct pfsync_softc *sc = V_pfsyncif;
2753 struct pfsync_softc *sc = pfsyncif;
2759 splassert(IPL_SOFTNET);
2765 bzero(&r, sizeof(r));
2767 r.subh.action = PFSYNC_ACT_CLR;
2768 r.subh.count = htons(1);
2770 strlcpy(r.clr.ifname, ifname, sizeof(r.clr.ifname));
2771 r.clr.creatorid = creatorid;
2773 pfsync_send_plus(&r, sizeof(r));
2777 pfsync_q_ins(struct pf_state *st, int q)
2780 struct pfsync_softc *sc = V_pfsyncif;
2782 struct pfsync_softc *sc = pfsyncif;
2784 size_t nlen = pfsync_qs[q].len;
2790 KASSERT(st->sync_state == PFSYNC_S_NONE,
2791 ("%s: st->sync_state == PFSYNC_S_NONE", __FUNCTION__));
2793 KASSERT(st->sync_state == PFSYNC_S_NONE);
2796 #if 1 || defined(PFSYNC_DEBUG)
2797 if (sc->sc_len < PFSYNC_MINPKT)
2799 panic("pfsync pkt len is too low %zu", sc->sc_len);
2801 panic("pfsync pkt len is too low %d", sc->sc_len);
2804 if (TAILQ_EMPTY(&sc->sc_qs[q]))
2805 nlen += sizeof(struct pfsync_subheader);
2808 if (sc->sc_len + nlen > sc->sc_ifp->if_mtu) {
2810 if (sc->sc_len + nlen > sc->sc_if.if_mtu) {
2816 nlen = sizeof(struct pfsync_subheader) + pfsync_qs[q].len;
2820 TAILQ_INSERT_TAIL(&sc->sc_qs[q], st, sync_list);
2825 pfsync_q_del(struct pf_state *st)
2828 struct pfsync_softc *sc = V_pfsyncif;
2830 struct pfsync_softc *sc = pfsyncif;
2832 int q = st->sync_state;
2835 KASSERT(st->sync_state != PFSYNC_S_NONE,
2836 ("%s: st->sync_state != PFSYNC_S_NONE", __FUNCTION__));
2838 KASSERT(st->sync_state != PFSYNC_S_NONE);
2841 sc->sc_len -= pfsync_qs[q].len;
2842 TAILQ_REMOVE(&sc->sc_qs[q], st, sync_list);
2843 st->sync_state = PFSYNC_S_NONE;
2845 if (TAILQ_EMPTY(&sc->sc_qs[q]))
2846 sc->sc_len -= sizeof(struct pfsync_subheader);
2851 pfsync_update_tdb(struct tdb *t, int output)
2854 struct pfsync_softc *sc = V_pfsyncif;
2856 struct pfsync_softc *sc = pfsyncif;
2858 size_t nlen = sizeof(struct pfsync_tdb);
2864 if (!ISSET(t->tdb_flags, TDBF_PFSYNC)) {
2865 if (TAILQ_EMPTY(&sc->sc_tdb_q))
2866 nlen += sizeof(struct pfsync_subheader);
2868 if (sc->sc_len + nlen > sc->sc_if.if_mtu) {
2875 nlen = sizeof(struct pfsync_subheader) +
2876 sizeof(struct pfsync_tdb);
2880 TAILQ_INSERT_TAIL(&sc->sc_tdb_q, t, tdb_sync_entry);
2881 SET(t->tdb_flags, TDBF_PFSYNC);
2884 if (++t->tdb_updates >= sc->sc_maxupdates)
2885 schednetisr(NETISR_PFSYNC);
2889 SET(t->tdb_flags, TDBF_PFSYNC_RPL);
2891 CLR(t->tdb_flags, TDBF_PFSYNC_RPL);
2895 pfsync_delete_tdb(struct tdb *t)
2898 struct pfsync_softc *sc = V_pfsyncif;
2900 struct pfsync_softc *sc = pfsyncif;
2903 if (sc == NULL || !ISSET(t->tdb_flags, TDBF_PFSYNC))
2906 sc->sc_len -= sizeof(struct pfsync_tdb);
2907 TAILQ_REMOVE(&sc->sc_tdb_q, t, tdb_sync_entry);
2908 CLR(t->tdb_flags, TDBF_PFSYNC);
2910 if (TAILQ_EMPTY(&sc->sc_tdb_q))
2911 sc->sc_len -= sizeof(struct pfsync_subheader);
2915 pfsync_out_tdb(struct tdb *t, struct mbuf *m, int offset)
2917 struct pfsync_tdb *ut = (struct pfsync_tdb *)(m->m_data + offset);
2919 bzero(ut, sizeof(*ut));
2920 ut->spi = t->tdb_spi;
2921 bcopy(&t->tdb_dst, &ut->dst, sizeof(ut->dst));
2923 * When a failover happens, the master's rpl is probably above
2924 * what we see here (we may be up to a second late), so
2925 * increase it a bit for outbound tdbs to manage most such
2928 * For now, just add an offset that is likely to be larger
2929 * than the number of packets we can see in one second. The RFC
2930 * just says the next packet must have a higher seq value.
2932 * XXX What is a good algorithm for this? We could use
2933 * a rate-determined increase, but to know it, we would have
2934 * to extend struct tdb.
2935 * XXX pt->rpl can wrap over MAXINT, but if so the real tdb
2936 * will soon be replaced anyway. For now, just don't handle
2939 #define RPL_INCR 16384
2940 ut->rpl = htonl(t->tdb_rpl + (ISSET(t->tdb_flags, TDBF_PFSYNC_RPL) ?
2942 ut->cur_bytes = htobe64(t->tdb_cur_bytes);
2943 ut->sproto = t->tdb_sproto;
2945 return (sizeof(*ut));
2950 pfsync_bulk_start(void)
2953 struct pfsync_softc *sc = V_pfsyncif;
2955 struct pfsync_softc *sc = pfsyncif;
2959 if (V_pf_status.debug >= PF_DEBUG_MISC)
2961 if (pf_status.debug >= PF_DEBUG_MISC)
2963 printf("pfsync: received bulk update request\n");
2967 if (TAILQ_EMPTY(&V_state_list))
2969 if (TAILQ_EMPTY(&state_list))
2971 pfsync_bulk_status(PFSYNC_BUS_END);
2973 sc->sc_ureq_received = time_uptime;
2974 if (sc->sc_bulk_next == NULL)
2976 sc->sc_bulk_next = TAILQ_FIRST(&V_state_list);
2978 sc->sc_bulk_next = TAILQ_FIRST(&state_list);
2980 sc->sc_bulk_last = sc->sc_bulk_next;
2982 pfsync_bulk_status(PFSYNC_BUS_START);
2983 callout_reset(&sc->sc_bulk_tmo, 1, pfsync_bulk_update, sc);
2988 pfsync_bulk_update(void *arg)
2990 struct pfsync_softc *sc = arg;
2991 struct pf_state *st = sc->sc_bulk_next;
2999 CURVNET_SET(sc->sc_ifp->if_vnet);
3002 if (st->sync_state == PFSYNC_S_NONE &&
3003 st->timeout < PFTM_MAX &&
3004 st->pfsync_time <= sc->sc_ureq_received) {
3005 pfsync_update_state_req(st);
3009 st = TAILQ_NEXT(st, entry_list);
3012 st = TAILQ_FIRST(&V_state_list);
3014 st = TAILQ_FIRST(&state_list);
3017 if (st == sc->sc_bulk_last) {
3019 sc->sc_bulk_next = NULL;
3020 sc->sc_bulk_last = NULL;
3021 pfsync_bulk_status(PFSYNC_BUS_END);
3026 if (i > 1 && (sc->sc_ifp->if_mtu - sc->sc_len) <
3028 if (i > 1 && (sc->sc_if.if_mtu - sc->sc_len) <
3030 sizeof(struct pfsync_state)) {
3031 /* we've filled a packet */
3032 sc->sc_bulk_next = st;
3034 callout_reset(&sc->sc_bulk_tmo, 1,
3035 pfsync_bulk_update, sc);
3037 timeout_add(&sc->sc_bulk_tmo, 1);
3050 pfsync_bulk_status(u_int8_t status)
3053 struct pfsync_subheader subh;
3054 struct pfsync_bus bus;
3058 struct pfsync_softc *sc = V_pfsyncif;
3060 struct pfsync_softc *sc = pfsyncif;
3065 bzero(&r, sizeof(r));
3067 r.subh.action = PFSYNC_ACT_BUS;
3068 r.subh.count = htons(1);
3071 r.bus.creatorid = V_pf_status.hostid;
3073 r.bus.creatorid = pf_status.hostid;
3075 r.bus.endtime = htonl(time_uptime - sc->sc_ureq_received);
3076 r.bus.status = status;
3078 pfsync_send_plus(&r, sizeof(r));
3082 pfsync_bulk_fail(void *arg)
3084 struct pfsync_softc *sc = arg;
3087 CURVNET_SET(sc->sc_ifp->if_vnet);
3090 if (sc->sc_bulk_tries++ < PFSYNC_MAX_BULKTRIES) {
3093 callout_reset(&sc->sc_bulkfail_tmo, 5 * hz,
3094 pfsync_bulk_fail, V_pfsyncif);
3096 timeout_add_sec(&sc->sc_bulkfail_tmo, 5);
3099 pfsync_request_update(0, 0);
3102 /* Pretend like the transfer was ok */
3103 sc->sc_ureq_sent = 0;
3104 sc->sc_bulk_tries = 0;
3106 if (!sc->pfsync_sync_ok && carp_demote_adj_p)
3107 (*carp_demote_adj_p)(-V_pfsync_carp_adj,
3108 "pfsync bulk fail");
3109 sc->pfsync_sync_ok = 1;
3112 if (!pfsync_sync_ok)
3113 carp_group_demote_adj(&sc->sc_if, -1);
3118 if (V_pf_status.debug >= PF_DEBUG_MISC)
3120 if (pf_status.debug >= PF_DEBUG_MISC)
3122 printf("pfsync: failed to receive bulk update\n");
3131 pfsync_send_plus(void *plus, size_t pluslen)
3134 struct pfsync_softc *sc = V_pfsyncif;
3136 struct pfsync_softc *sc = pfsyncif;
3143 if (sc->sc_len + pluslen > sc->sc_ifp->if_mtu) {
3145 if (sc->sc_len + pluslen > sc->sc_if.if_mtu) {
3153 sc->sc_len += (sc->sc_pluslen = pluslen);
3164 struct pfsync_softc *sc = V_pfsyncif;
3166 struct pfsync_softc *sc = pfsyncif;
3170 if (sc == NULL || !ISSET(sc->sc_ifp->if_flags, IFF_DRV_RUNNING))
3172 if (sc == NULL || !ISSET(sc->sc_if.if_flags, IFF_RUNNING))
3180 pfsync_state_in_use(struct pf_state *st)
3183 struct pfsync_softc *sc = V_pfsyncif;
3185 struct pfsync_softc *sc = pfsyncif;
3191 if (st->sync_state != PFSYNC_S_NONE ||
3192 st == sc->sc_bulk_next ||
3193 st == sc->sc_bulk_last)
3203 pfsync_timeout(void *arg)
3205 #if defined(__FreeBSD__) && defined(VIMAGE)
3206 struct pfsync_softc *sc = arg;
3211 CURVNET_SET(sc->sc_ifp->if_vnet);
3231 /* this is a softnet/netisr handler */
3234 pfsyncintr(void *arg)
3236 struct pfsync_softc *sc = arg;
3239 CURVNET_SET(sc->sc_ifp->if_vnet);
3243 if (sc->sc_len > PFSYNC_MINPKT)
3245 _IF_DEQUEUE_ALL(&sc->sc_ifp->if_snd, m);
3248 for (; m != NULL; m = n) {
3251 m->m_nextpkt = NULL;
3252 if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo, NULL)
3254 V_pfsyncstats.pfsyncs_opackets++;
3256 V_pfsyncstats.pfsyncs_oerrors++;
3274 pfsync_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
3279 /* All sysctl names at this level are terminal. */
3284 case PFSYNCCTL_STATS:
3287 return (sysctl_struct(oldp, oldlenp, newp, newlen,
3288 &V_pfsyncstats, sizeof(V_pfsyncstats)));
3291 return (ENOPROTOOPT);
3296 pfsync_multicast_setup(struct pfsync_softc *sc)
3298 struct ip_moptions *imo = &sc->sc_imo;
3301 if (!(sc->sc_sync_if->if_flags & IFF_MULTICAST)) {
3302 sc->sc_sync_if = NULL;
3303 return (EADDRNOTAVAIL);
3306 imo->imo_membership = (struct in_multi **)malloc(
3307 (sizeof(struct in_multi *) * IP_MIN_MEMBERSHIPS), M_PFSYNC,
3309 imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
3310 imo->imo_multicast_vif = -1;
3312 if ((error = in_joingroup(sc->sc_sync_if, &sc->sc_sync_peer, NULL,
3313 &imo->imo_membership[0])) != 0) {
3314 free(imo->imo_membership, M_PFSYNC);
3317 imo->imo_num_memberships++;
3318 imo->imo_multicast_ifp = sc->sc_sync_if;
3319 imo->imo_multicast_ttl = PFSYNC_DFLTTL;
3320 imo->imo_multicast_loop = 0;
3326 pfsync_multicast_cleanup(struct pfsync_softc *sc)
3328 struct ip_moptions *imo = &sc->sc_imo;
3330 in_leavegroup(imo->imo_membership[0], NULL);
3331 free(imo->imo_membership, M_PFSYNC);
3332 imo->imo_membership = NULL;
3333 imo->imo_multicast_ifp = NULL;
3337 extern struct domain inetdomain;
3338 static struct protosw in_pfsync_protosw = {
3339 .pr_type = SOCK_RAW,
3340 .pr_domain = &inetdomain,
3341 .pr_protocol = IPPROTO_PFSYNC,
3342 .pr_flags = PR_ATOMIC|PR_ADDR,
3343 .pr_input = pfsync_input,
3344 .pr_output = (pr_output_t *)rip_output,
3345 .pr_ctloutput = rip_ctloutput,
3346 .pr_usrreqs = &rip_usrreqs
3353 VNET_ITERATOR_DECL(vnet_iter);
3357 VNET_FOREACH(vnet_iter) {
3358 CURVNET_SET(vnet_iter);
3359 V_pfsync_cloner = pfsync_cloner;
3360 V_pfsync_cloner_data = pfsync_cloner_data;
3361 V_pfsync_cloner.ifc_data = &V_pfsync_cloner_data;
3362 if_clone_attach(&V_pfsync_cloner);
3363 error = swi_add(NULL, "pfsync", pfsyncintr, V_pfsyncif,
3364 SWI_NET, INTR_MPSAFE, &V_pfsync_swi_cookie);
3369 VNET_LIST_RUNLOCK();
3371 error = pf_proto_register(PF_INET, &in_pfsync_protosw);
3374 error = ipproto_register(IPPROTO_PFSYNC);
3376 pf_proto_unregister(PF_INET, IPPROTO_PFSYNC, SOCK_RAW);
3381 pfsync_state_import_ptr = pfsync_state_import;
3382 pfsync_up_ptr = pfsync_up;
3383 pfsync_insert_state_ptr = pfsync_insert_state;
3384 pfsync_update_state_ptr = pfsync_update_state;
3385 pfsync_delete_state_ptr = pfsync_delete_state;
3386 pfsync_clear_states_ptr = pfsync_clear_states;
3387 pfsync_state_in_use_ptr = pfsync_state_in_use;
3388 pfsync_defer_ptr = pfsync_defer;
3396 VNET_FOREACH(vnet_iter) {
3397 CURVNET_SET(vnet_iter);
3398 if (V_pfsync_swi_cookie) {
3399 swi_remove(V_pfsync_swi_cookie);
3400 if_clone_detach(&V_pfsync_cloner);
3404 VNET_LIST_RUNLOCK();
3412 VNET_ITERATOR_DECL(vnet_iter);
3415 pfsync_state_import_ptr = NULL;
3416 pfsync_up_ptr = NULL;
3417 pfsync_insert_state_ptr = NULL;
3418 pfsync_update_state_ptr = NULL;
3419 pfsync_delete_state_ptr = NULL;
3420 pfsync_clear_states_ptr = NULL;
3421 pfsync_state_in_use_ptr = NULL;
3422 pfsync_defer_ptr = NULL;
3425 ipproto_unregister(IPPROTO_PFSYNC);
3426 pf_proto_unregister(PF_INET, IPPROTO_PFSYNC, SOCK_RAW);
3428 VNET_FOREACH(vnet_iter) {
3429 CURVNET_SET(vnet_iter);
3430 swi_remove(V_pfsync_swi_cookie);
3431 if_clone_detach(&V_pfsync_cloner);
3434 VNET_LIST_RUNLOCK();
3438 pfsync_modevent(module_t mod, int type, void *data)
3444 error = pfsync_init();
3448 * Module should not be unloaded due to race conditions.
3463 static moduledata_t pfsync_mod = {
3469 #define PFSYNC_MODVER 1
3471 DECLARE_MODULE(pfsync, pfsync_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
3472 MODULE_VERSION(pfsync, PFSYNC_MODVER);
3473 MODULE_DEPEND(pfsync, pf, PF_MODVER, PF_MODVER, PF_MODVER);
3474 #endif /* __FreeBSD__ */