xref: /freebsd/sys/netpfil/pf/if_pfsync.c (revision 5e3190f700637fcfc1a52daeaa4a031fdd2557c7)
1 /*-
2  * SPDX-License-Identifier: (BSD-2-Clause AND ISC)
3  *
4  * Copyright (c) 2002 Michael Shalayeff
5  * Copyright (c) 2012 Gleb Smirnoff <glebius@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
21  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /*-
31  * Copyright (c) 2009 David Gwynne <dlg@openbsd.org>
32  *
33  * Permission to use, copy, modify, and distribute this software for any
34  * purpose with or without fee is hereby granted, provided that the above
35  * copyright notice and this permission notice appear in all copies.
36  *
37  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
38  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
39  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
40  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
41  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
42  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
43  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
44  */
45 
46 /*
47  * $OpenBSD: if_pfsync.c,v 1.110 2009/02/24 05:39:19 dlg Exp $
48  *
49  * Revisions picked from OpenBSD after revision 1.110 import:
50  * 1.119 - don't m_copydata() beyond the len of mbuf in pfsync_input()
51  * 1.118, 1.124, 1.148, 1.149, 1.151, 1.171 - fixes to bulk updates
52  * 1.120, 1.175 - use monotonic time_uptime
53  * 1.122 - reduce number of updates for non-TCP sessions
54  * 1.125, 1.127 - rewrite merge or stale processing
55  * 1.128 - cleanups
56  * 1.146 - bzero() mbuf before sparsely filling it with data
57  * 1.170 - SIOCSIFMTU checks
58  * 1.126, 1.142 - deferred packets processing
59  * 1.173 - correct expire time processing
60  */
61 
62 #include <sys/cdefs.h>
63 #include "opt_inet.h"
64 #include "opt_inet6.h"
65 #include "opt_pf.h"
66 
67 #include <sys/param.h>
68 #include <sys/bus.h>
69 #include <sys/endian.h>
70 #include <sys/interrupt.h>
71 #include <sys/kernel.h>
72 #include <sys/lock.h>
73 #include <sys/mbuf.h>
74 #include <sys/module.h>
75 #include <sys/mutex.h>
76 #include <sys/nv.h>
77 #include <sys/priv.h>
78 #include <sys/smp.h>
79 #include <sys/socket.h>
80 #include <sys/sockio.h>
81 #include <sys/sysctl.h>
82 #include <sys/syslog.h>
83 
84 #include <net/bpf.h>
85 #include <net/if.h>
86 #include <net/if_var.h>
87 #include <net/if_clone.h>
88 #include <net/if_private.h>
89 #include <net/if_types.h>
90 #include <net/vnet.h>
91 #include <net/pfvar.h>
92 #include <net/route.h>
93 #include <net/if_pfsync.h>
94 
95 #include <netinet/if_ether.h>
96 #include <netinet/in.h>
97 #include <netinet/in_var.h>
98 #include <netinet6/in6_var.h>
99 #include <netinet/ip.h>
100 #include <netinet/ip6.h>
101 #include <netinet/ip_carp.h>
102 #include <netinet/ip_var.h>
103 #include <netinet/tcp.h>
104 #include <netinet/tcp_fsm.h>
105 #include <netinet/tcp_seq.h>
106 
107 #include <netinet/ip6.h>
108 #include <netinet6/ip6_var.h>
109 #include <netinet6/scope6_var.h>
110 
111 #include <netpfil/pf/pfsync_nv.h>
112 
113 struct pfsync_bucket;
114 struct pfsync_softc;
115 
116 union inet_template {
117 	struct ip	ipv4;
118 	struct ip6_hdr	ipv6;
119 };
120 
121 #define PFSYNC_MINPKT ( \
122 	sizeof(union inet_template) + \
123 	sizeof(struct pfsync_header) + \
124 	sizeof(struct pfsync_subheader) )
125 
126 static int	pfsync_upd_tcp(struct pf_kstate *, struct pfsync_state_peer *,
127 		    struct pfsync_state_peer *);
128 static int	pfsync_in_clr(struct mbuf *, int, int, int, int);
129 static int	pfsync_in_ins(struct mbuf *, int, int, int, int);
130 static int	pfsync_in_iack(struct mbuf *, int, int, int, int);
131 static int	pfsync_in_upd(struct mbuf *, int, int, int, int);
132 static int	pfsync_in_upd_c(struct mbuf *, int, int, int, int);
133 static int	pfsync_in_ureq(struct mbuf *, int, int, int, int);
134 static int	pfsync_in_del_c(struct mbuf *, int, int, int, int);
135 static int	pfsync_in_bus(struct mbuf *, int, int, int, int);
136 static int	pfsync_in_tdb(struct mbuf *, int, int, int, int);
137 static int	pfsync_in_eof(struct mbuf *, int, int, int, int);
138 static int	pfsync_in_error(struct mbuf *, int, int, int, int);
139 
140 static int (*pfsync_acts[])(struct mbuf *, int, int, int, int) = {
141 	pfsync_in_clr,			/* PFSYNC_ACT_CLR */
142 	pfsync_in_ins,			/* PFSYNC_ACT_INS_1301 */
143 	pfsync_in_iack,			/* PFSYNC_ACT_INS_ACK */
144 	pfsync_in_upd,			/* PFSYNC_ACT_UPD_1301 */
145 	pfsync_in_upd_c,		/* PFSYNC_ACT_UPD_C */
146 	pfsync_in_ureq,			/* PFSYNC_ACT_UPD_REQ */
147 	pfsync_in_error,		/* PFSYNC_ACT_DEL */
148 	pfsync_in_del_c,		/* PFSYNC_ACT_DEL_C */
149 	pfsync_in_error,		/* PFSYNC_ACT_INS_F */
150 	pfsync_in_error,		/* PFSYNC_ACT_DEL_F */
151 	pfsync_in_bus,			/* PFSYNC_ACT_BUS */
152 	pfsync_in_tdb,			/* PFSYNC_ACT_TDB */
153 	pfsync_in_eof,			/* PFSYNC_ACT_EOF */
154 	pfsync_in_ins,			/* PFSYNC_ACT_INS_1400 */
155 	pfsync_in_upd,			/* PFSYNC_ACT_UPD_1400 */
156 };
157 
158 struct pfsync_q {
159 	void		(*write)(struct pf_kstate *, void *);
160 	size_t		len;
161 	u_int8_t	action;
162 };
163 
164 /* We have the following sync queues */
165 enum pfsync_q_id {
166 	PFSYNC_Q_INS_1301,
167 	PFSYNC_Q_INS_1400,
168 	PFSYNC_Q_IACK,
169 	PFSYNC_Q_UPD_1301,
170 	PFSYNC_Q_UPD_1400,
171 	PFSYNC_Q_UPD_C,
172 	PFSYNC_Q_DEL_C,
173 	PFSYNC_Q_COUNT,
174 };
175 
176 /* Functions for building messages for given queue */
177 static void	pfsync_out_state_1301(struct pf_kstate *, void *);
178 static void	pfsync_out_state_1400(struct pf_kstate *, void *);
179 static void	pfsync_out_iack(struct pf_kstate *, void *);
180 static void	pfsync_out_upd_c(struct pf_kstate *, void *);
181 static void	pfsync_out_del_c(struct pf_kstate *, void *);
182 
183 /* Attach those functions to queue */
184 static struct pfsync_q pfsync_qs[] = {
185 	{ pfsync_out_state_1301, sizeof(struct pfsync_state_1301), PFSYNC_ACT_INS_1301 },
186 	{ pfsync_out_state_1400, sizeof(struct pfsync_state_1400), PFSYNC_ACT_INS_1400 },
187 	{ pfsync_out_iack,       sizeof(struct pfsync_ins_ack),    PFSYNC_ACT_INS_ACK },
188 	{ pfsync_out_state_1301, sizeof(struct pfsync_state_1301), PFSYNC_ACT_UPD_1301 },
189 	{ pfsync_out_state_1400, sizeof(struct pfsync_state_1400), PFSYNC_ACT_UPD_1400 },
190 	{ pfsync_out_upd_c,      sizeof(struct pfsync_upd_c),      PFSYNC_ACT_UPD_C },
191 	{ pfsync_out_del_c,      sizeof(struct pfsync_del_c),      PFSYNC_ACT_DEL_C }
192 };
193 
194 /* Map queue to pf_kstate->sync_state */
195 static u_int8_t pfsync_qid_sstate[] = {
196 	PFSYNC_S_INS,   /* PFSYNC_Q_INS_1301 */
197 	PFSYNC_S_INS,   /* PFSYNC_Q_INS_1400 */
198 	PFSYNC_S_IACK,  /* PFSYNC_Q_IACK */
199 	PFSYNC_S_UPD,   /* PFSYNC_Q_UPD_1301 */
200 	PFSYNC_S_UPD,   /* PFSYNC_Q_UPD_1400 */
201 	PFSYNC_S_UPD_C, /* PFSYNC_Q_UPD_C */
202 	PFSYNC_S_DEL_C, /* PFSYNC_Q_DEL_C */
203 };
204 
205 /* Map pf_kstate->sync_state to queue */
206 static enum pfsync_q_id pfsync_sstate_to_qid(u_int8_t);
207 
208 static void	pfsync_q_ins(struct pf_kstate *, int sync_state, bool);
209 static void	pfsync_q_del(struct pf_kstate *, bool, struct pfsync_bucket *);
210 
211 static void	pfsync_update_state(struct pf_kstate *);
212 static void	pfsync_tx(struct pfsync_softc *, struct mbuf *);
213 
214 struct pfsync_upd_req_item {
215 	TAILQ_ENTRY(pfsync_upd_req_item)	ur_entry;
216 	struct pfsync_upd_req			ur_msg;
217 };
218 
219 struct pfsync_deferral {
220 	struct pfsync_softc		*pd_sc;
221 	TAILQ_ENTRY(pfsync_deferral)	pd_entry;
222 	struct callout			pd_tmo;
223 
224 	struct pf_kstate		*pd_st;
225 	struct mbuf			*pd_m;
226 };
227 
228 struct pfsync_bucket
229 {
230 	int			b_id;
231 	struct pfsync_softc	*b_sc;
232 	struct mtx		b_mtx;
233 	struct callout		b_tmo;
234 	int			b_flags;
235 #define	PFSYNCF_BUCKET_PUSH	0x00000001
236 
237 	size_t			b_len;
238 	TAILQ_HEAD(, pf_kstate)			b_qs[PFSYNC_Q_COUNT];
239 	TAILQ_HEAD(, pfsync_upd_req_item)	b_upd_req_list;
240 	TAILQ_HEAD(, pfsync_deferral)		b_deferrals;
241 	u_int			b_deferred;
242 	void			*b_plus;
243 	size_t			b_pluslen;
244 
245 	struct  ifaltq b_snd;
246 };
247 
248 struct pfsync_softc {
249 	/* Configuration */
250 	struct ifnet		*sc_ifp;
251 	struct ifnet		*sc_sync_if;
252 	struct ip_moptions	sc_imo;
253 	struct ip6_moptions	sc_im6o;
254 	struct sockaddr_storage	sc_sync_peer;
255 	uint32_t		sc_flags;
256 	uint8_t			sc_maxupdates;
257 	union inet_template     sc_template;
258 	struct mtx		sc_mtx;
259 	uint32_t		sc_version;
260 
261 	/* Queued data */
262 	struct pfsync_bucket	*sc_buckets;
263 
264 	/* Bulk update info */
265 	struct mtx		sc_bulk_mtx;
266 	uint32_t		sc_ureq_sent;
267 	int			sc_bulk_tries;
268 	uint32_t		sc_ureq_received;
269 	int			sc_bulk_hashid;
270 	uint64_t		sc_bulk_stateid;
271 	uint32_t		sc_bulk_creatorid;
272 	struct callout		sc_bulk_tmo;
273 	struct callout		sc_bulkfail_tmo;
274 };
275 
276 #define	PFSYNC_LOCK(sc)		mtx_lock(&(sc)->sc_mtx)
277 #define	PFSYNC_UNLOCK(sc)	mtx_unlock(&(sc)->sc_mtx)
278 #define	PFSYNC_LOCK_ASSERT(sc)	mtx_assert(&(sc)->sc_mtx, MA_OWNED)
279 
280 #define PFSYNC_BUCKET_LOCK(b)		mtx_lock(&(b)->b_mtx)
281 #define PFSYNC_BUCKET_UNLOCK(b)		mtx_unlock(&(b)->b_mtx)
282 #define PFSYNC_BUCKET_LOCK_ASSERT(b)	mtx_assert(&(b)->b_mtx, MA_OWNED)
283 
284 #define	PFSYNC_BLOCK(sc)	mtx_lock(&(sc)->sc_bulk_mtx)
285 #define	PFSYNC_BUNLOCK(sc)	mtx_unlock(&(sc)->sc_bulk_mtx)
286 #define	PFSYNC_BLOCK_ASSERT(sc)	mtx_assert(&(sc)->sc_bulk_mtx, MA_OWNED)
287 
288 #define PFSYNC_DEFER_TIMEOUT	20
289 
290 static const char pfsyncname[] = "pfsync";
291 static MALLOC_DEFINE(M_PFSYNC, pfsyncname, "pfsync(4) data");
292 VNET_DEFINE_STATIC(struct pfsync_softc	*, pfsyncif) = NULL;
293 #define	V_pfsyncif		VNET(pfsyncif)
294 VNET_DEFINE_STATIC(void *, pfsync_swi_cookie) = NULL;
295 #define	V_pfsync_swi_cookie	VNET(pfsync_swi_cookie)
296 VNET_DEFINE_STATIC(struct intr_event *, pfsync_swi_ie);
297 #define	V_pfsync_swi_ie		VNET(pfsync_swi_ie)
298 VNET_DEFINE_STATIC(struct pfsyncstats, pfsyncstats);
299 #define	V_pfsyncstats		VNET(pfsyncstats)
300 VNET_DEFINE_STATIC(int, pfsync_carp_adj) = CARP_MAXSKEW;
301 #define	V_pfsync_carp_adj	VNET(pfsync_carp_adj)
302 VNET_DEFINE_STATIC(unsigned int, pfsync_defer_timeout) = PFSYNC_DEFER_TIMEOUT;
303 #define	V_pfsync_defer_timeout	VNET(pfsync_defer_timeout)
304 
305 static void	pfsync_timeout(void *);
306 static void	pfsync_push(struct pfsync_bucket *);
307 static void	pfsync_push_all(struct pfsync_softc *);
308 static void	pfsyncintr(void *);
309 static int	pfsync_multicast_setup(struct pfsync_softc *, struct ifnet *,
310 		    struct in_mfilter *, struct in6_mfilter *);
311 static void	pfsync_multicast_cleanup(struct pfsync_softc *);
312 static void	pfsync_pointers_init(void);
313 static void	pfsync_pointers_uninit(void);
314 static int	pfsync_init(void);
315 static void	pfsync_uninit(void);
316 
317 static unsigned long pfsync_buckets;
318 
319 SYSCTL_NODE(_net, OID_AUTO, pfsync, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
320     "PFSYNC");
321 SYSCTL_STRUCT(_net_pfsync, OID_AUTO, stats, CTLFLAG_VNET | 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_VNET | CTLFLAG_RW,
325     &VNET_NAME(pfsync_carp_adj), 0, "pfsync's CARP demotion factor adjustment");
326 SYSCTL_ULONG(_net_pfsync, OID_AUTO, pfsync_buckets, CTLFLAG_RDTUN,
327     &pfsync_buckets, 0, "Number of pfsync hash buckets");
328 SYSCTL_UINT(_net_pfsync, OID_AUTO, defer_delay, CTLFLAG_VNET | CTLFLAG_RW,
329     &VNET_NAME(pfsync_defer_timeout), 0, "Deferred packet timeout (in ms)");
330 
331 static int	pfsync_clone_create(struct if_clone *, int, caddr_t);
332 static void	pfsync_clone_destroy(struct ifnet *);
333 static int	pfsync_alloc_scrub_memory(struct pfsync_state_peer *,
334 		    struct pf_state_peer *);
335 static int	pfsyncoutput(struct ifnet *, struct mbuf *,
336 		    const struct sockaddr *, struct route *);
337 static int	pfsyncioctl(struct ifnet *, u_long, caddr_t);
338 
339 static int	pfsync_defer(struct pf_kstate *, struct mbuf *);
340 static void	pfsync_undefer(struct pfsync_deferral *, int);
341 static void	pfsync_undefer_state_locked(struct pf_kstate *, int);
342 static void	pfsync_undefer_state(struct pf_kstate *, int);
343 static void	pfsync_defer_tmo(void *);
344 
345 static void	pfsync_request_update(u_int32_t, u_int64_t);
346 static bool	pfsync_update_state_req(struct pf_kstate *);
347 
348 static void	pfsync_drop(struct pfsync_softc *);
349 static void	pfsync_sendout(int, int);
350 static void	pfsync_send_plus(void *, size_t);
351 
352 static void	pfsync_bulk_start(void);
353 static void	pfsync_bulk_status(u_int8_t);
354 static void	pfsync_bulk_update(void *);
355 static void	pfsync_bulk_fail(void *);
356 
357 static void	pfsync_detach_ifnet(struct ifnet *);
358 
359 static int pfsync_pfsyncreq_to_kstatus(struct pfsyncreq *,
360     struct pfsync_kstatus *);
361 static int pfsync_kstatus_to_softc(struct pfsync_kstatus *,
362     struct pfsync_softc *);
363 
364 #ifdef IPSEC
365 static void	pfsync_update_net_tdb(struct pfsync_tdb *);
366 #endif
367 static struct pfsync_bucket	*pfsync_get_bucket(struct pfsync_softc *,
368 		    struct pf_kstate *);
369 
370 #define PFSYNC_MAX_BULKTRIES	12
371 
372 VNET_DEFINE(struct if_clone *, pfsync_cloner);
373 #define	V_pfsync_cloner	VNET(pfsync_cloner)
374 
375 const struct in6_addr in6addr_linklocal_pfsync_group =
376 	{{{ 0xff, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
377 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0 }}};
378 static int
379 pfsync_clone_create(struct if_clone *ifc, int unit, caddr_t param)
380 {
381 	struct pfsync_softc *sc;
382 	struct ifnet *ifp;
383 	struct pfsync_bucket *b;
384 	int c;
385 	enum pfsync_q_id q;
386 
387 	if (unit != 0)
388 		return (EINVAL);
389 
390 	if (! pfsync_buckets)
391 		pfsync_buckets = mp_ncpus * 2;
392 
393 	sc = malloc(sizeof(struct pfsync_softc), M_PFSYNC, M_WAITOK | M_ZERO);
394 	sc->sc_flags |= PFSYNCF_OK;
395 	sc->sc_maxupdates = 128;
396 	sc->sc_version = PFSYNC_MSG_VERSION_DEFAULT;
397 
398 	ifp = sc->sc_ifp = if_alloc(IFT_PFSYNC);
399 	if (ifp == NULL) {
400 		free(sc, M_PFSYNC);
401 		return (ENOSPC);
402 	}
403 	if_initname(ifp, pfsyncname, unit);
404 	ifp->if_softc = sc;
405 	ifp->if_ioctl = pfsyncioctl;
406 	ifp->if_output = pfsyncoutput;
407 	ifp->if_type = IFT_PFSYNC;
408 	ifp->if_hdrlen = sizeof(struct pfsync_header);
409 	ifp->if_mtu = ETHERMTU;
410 	mtx_init(&sc->sc_mtx, pfsyncname, NULL, MTX_DEF);
411 	mtx_init(&sc->sc_bulk_mtx, "pfsync bulk", NULL, MTX_DEF);
412 	callout_init_mtx(&sc->sc_bulk_tmo, &sc->sc_bulk_mtx, 0);
413 	callout_init_mtx(&sc->sc_bulkfail_tmo, &sc->sc_bulk_mtx, 0);
414 
415 	if_attach(ifp);
416 
417 	bpfattach(ifp, DLT_PFSYNC, PFSYNC_HDRLEN);
418 
419 	sc->sc_buckets = mallocarray(pfsync_buckets, sizeof(*sc->sc_buckets),
420 	    M_PFSYNC, M_ZERO | M_WAITOK);
421 	for (c = 0; c < pfsync_buckets; c++) {
422 		b = &sc->sc_buckets[c];
423 		mtx_init(&b->b_mtx, "pfsync bucket", NULL, MTX_DEF);
424 
425 		b->b_id = c;
426 		b->b_sc = sc;
427 		b->b_len = PFSYNC_MINPKT;
428 
429 		for (q = 0; q < PFSYNC_Q_COUNT; q++)
430 			TAILQ_INIT(&b->b_qs[q]);
431 
432 		TAILQ_INIT(&b->b_upd_req_list);
433 		TAILQ_INIT(&b->b_deferrals);
434 
435 		callout_init(&b->b_tmo, 1);
436 
437 		b->b_snd.ifq_maxlen = ifqmaxlen;
438 	}
439 
440 	V_pfsyncif = sc;
441 
442 	return (0);
443 }
444 
445 static void
446 pfsync_clone_destroy(struct ifnet *ifp)
447 {
448 	struct pfsync_softc *sc = ifp->if_softc;
449 	struct pfsync_bucket *b;
450 	int c, ret;
451 
452 	for (c = 0; c < pfsync_buckets; c++) {
453 		b = &sc->sc_buckets[c];
454 		/*
455 		 * At this stage, everything should have already been
456 		 * cleared by pfsync_uninit(), and we have only to
457 		 * drain callouts.
458 		 */
459 		PFSYNC_BUCKET_LOCK(b);
460 		while (b->b_deferred > 0) {
461 			struct pfsync_deferral *pd =
462 			    TAILQ_FIRST(&b->b_deferrals);
463 
464 			ret = callout_stop(&pd->pd_tmo);
465 			PFSYNC_BUCKET_UNLOCK(b);
466 			if (ret > 0) {
467 				pfsync_undefer(pd, 1);
468 			} else {
469 				callout_drain(&pd->pd_tmo);
470 			}
471 			PFSYNC_BUCKET_LOCK(b);
472 		}
473 		MPASS(b->b_deferred == 0);
474 		MPASS(TAILQ_EMPTY(&b->b_deferrals));
475 		PFSYNC_BUCKET_UNLOCK(b);
476 
477 		callout_drain(&b->b_tmo);
478 	}
479 
480 	callout_drain(&sc->sc_bulkfail_tmo);
481 	callout_drain(&sc->sc_bulk_tmo);
482 
483 	if (!(sc->sc_flags & PFSYNCF_OK) && carp_demote_adj_p)
484 		(*carp_demote_adj_p)(-V_pfsync_carp_adj, "pfsync destroy");
485 	bpfdetach(ifp);
486 	if_detach(ifp);
487 
488 	pfsync_drop(sc);
489 
490 	if_free(ifp);
491 	pfsync_multicast_cleanup(sc);
492 	mtx_destroy(&sc->sc_mtx);
493 	mtx_destroy(&sc->sc_bulk_mtx);
494 
495 	free(sc->sc_buckets, M_PFSYNC);
496 	free(sc, M_PFSYNC);
497 
498 	V_pfsyncif = NULL;
499 }
500 
501 static int
502 pfsync_alloc_scrub_memory(struct pfsync_state_peer *s,
503     struct pf_state_peer *d)
504 {
505 	if (s->scrub.scrub_flag && d->scrub == NULL) {
506 		d->scrub = uma_zalloc(V_pf_state_scrub_z, M_NOWAIT | M_ZERO);
507 		if (d->scrub == NULL)
508 			return (ENOMEM);
509 	}
510 
511 	return (0);
512 }
513 
514 static int
515 pfsync_state_import(union pfsync_state_union *sp, int flags, int msg_version)
516 {
517 	struct pfsync_softc *sc = V_pfsyncif;
518 #ifndef	__NO_STRICT_ALIGNMENT
519 	struct pfsync_state_key key[2];
520 #endif
521 	struct pfsync_state_key *kw, *ks;
522 	struct pf_kstate	*st = NULL;
523 	struct pf_state_key *skw = NULL, *sks = NULL;
524 	struct pf_krule *r = NULL;
525 	struct pfi_kkif	*kif;
526 	int error;
527 
528 	PF_RULES_RASSERT();
529 
530 	if (sp->pfs_1301.creatorid == 0) {
531 		if (V_pf_status.debug >= PF_DEBUG_MISC)
532 			printf("%s: invalid creator id: %08x\n", __func__,
533 			    ntohl(sp->pfs_1301.creatorid));
534 		return (EINVAL);
535 	}
536 
537 	if ((kif = pfi_kkif_find(sp->pfs_1301.ifname)) == NULL) {
538 		if (V_pf_status.debug >= PF_DEBUG_MISC)
539 			printf("%s: unknown interface: %s\n", __func__,
540 			    sp->pfs_1301.ifname);
541 		if (flags & PFSYNC_SI_IOCTL)
542 			return (EINVAL);
543 		return (0);	/* skip this state */
544 	}
545 
546 	/*
547 	 * If the ruleset checksums match or the state is coming from the ioctl,
548 	 * it's safe to associate the state with the rule of that number.
549 	 */
550 	if (sp->pfs_1301.rule != htonl(-1) && sp->pfs_1301.anchor == htonl(-1) &&
551 	    (flags & (PFSYNC_SI_IOCTL | PFSYNC_SI_CKSUM)) && ntohl(sp->pfs_1301.rule) <
552 	    pf_main_ruleset.rules[PF_RULESET_FILTER].active.rcount)
553 		r = pf_main_ruleset.rules[
554 		    PF_RULESET_FILTER].active.ptr_array[ntohl(sp->pfs_1301.rule)];
555 	else
556 		r = &V_pf_default_rule;
557 
558 	if ((r->max_states &&
559 	    counter_u64_fetch(r->states_cur) >= r->max_states))
560 		goto cleanup;
561 
562 	/*
563 	 * XXXGL: consider M_WAITOK in ioctl path after.
564 	 */
565 	st = pf_alloc_state(M_NOWAIT);
566 	if (__predict_false(st == NULL))
567 		goto cleanup;
568 
569 	if ((skw = uma_zalloc(V_pf_state_key_z, M_NOWAIT)) == NULL)
570 		goto cleanup;
571 
572 #ifndef	__NO_STRICT_ALIGNMENT
573 	bcopy(&sp->pfs_1301.key, key, sizeof(struct pfsync_state_key) * 2);
574 	kw = &key[PF_SK_WIRE];
575 	ks = &key[PF_SK_STACK];
576 #else
577 	kw = &sp->pfs_1301.key[PF_SK_WIRE];
578 	ks = &sp->pfs_1301.key[PF_SK_STACK];
579 #endif
580 
581 	if (PF_ANEQ(&kw->addr[0], &ks->addr[0], sp->pfs_1301.af) ||
582 	    PF_ANEQ(&kw->addr[1], &ks->addr[1], sp->pfs_1301.af) ||
583 	    kw->port[0] != ks->port[0] ||
584 	    kw->port[1] != ks->port[1]) {
585 		sks = uma_zalloc(V_pf_state_key_z, M_NOWAIT);
586 		if (sks == NULL)
587 			goto cleanup;
588 	} else
589 		sks = skw;
590 
591 	/* allocate memory for scrub info */
592 	if (pfsync_alloc_scrub_memory(&sp->pfs_1301.src, &st->src) ||
593 	    pfsync_alloc_scrub_memory(&sp->pfs_1301.dst, &st->dst))
594 		goto cleanup;
595 
596 	/* Copy to state key(s). */
597 	skw->addr[0] = kw->addr[0];
598 	skw->addr[1] = kw->addr[1];
599 	skw->port[0] = kw->port[0];
600 	skw->port[1] = kw->port[1];
601 	skw->proto = sp->pfs_1301.proto;
602 	skw->af = sp->pfs_1301.af;
603 	if (sks != skw) {
604 		sks->addr[0] = ks->addr[0];
605 		sks->addr[1] = ks->addr[1];
606 		sks->port[0] = ks->port[0];
607 		sks->port[1] = ks->port[1];
608 		sks->proto = sp->pfs_1301.proto;
609 		sks->af = sp->pfs_1301.af;
610 	}
611 
612 	/* copy to state */
613 	bcopy(&sp->pfs_1301.rt_addr, &st->rt_addr, sizeof(st->rt_addr));
614 	st->creation = time_uptime - ntohl(sp->pfs_1301.creation);
615 	st->expire = time_uptime;
616 	if (sp->pfs_1301.expire) {
617 		uint32_t timeout;
618 
619 		timeout = r->timeout[sp->pfs_1301.timeout];
620 		if (!timeout)
621 			timeout = V_pf_default_rule.timeout[sp->pfs_1301.timeout];
622 
623 		/* sp->expire may have been adaptively scaled by export. */
624 		st->expire -= timeout - ntohl(sp->pfs_1301.expire);
625 	}
626 
627 	st->direction = sp->pfs_1301.direction;
628 	st->act.log = sp->pfs_1301.log;
629 	st->timeout = sp->pfs_1301.timeout;
630 
631 	switch (msg_version) {
632 		case PFSYNC_MSG_VERSION_1301:
633 			st->state_flags = sp->pfs_1301.state_flags;
634 			/*
635 			 * In FreeBSD 13 pfsync lacks many attributes. Copy them
636 			 * from the rule if possible. If rule can't be matched
637 			 * clear any set options as we can't recover their
638 			 * parameters.
639 			*/
640 			if (r == &V_pf_default_rule) {
641 				st->state_flags &= ~PFSTATE_SETMASK;
642 			} else {
643 				/*
644 				 * Similar to pf_rule_to_actions(). This code
645 				 * won't set the actions properly if they come
646 				 * from multiple "match" rules as only rule
647 				 * creating the state is send over pfsync.
648 				 */
649 				st->act.qid = r->qid;
650 				st->act.pqid = r->pqid;
651 				st->act.rtableid = r->rtableid;
652 				if (r->scrub_flags & PFSTATE_SETTOS)
653 					st->act.set_tos = r->set_tos;
654 				st->act.min_ttl = r->min_ttl;
655 				st->act.max_mss = r->max_mss;
656 				st->state_flags |= (r->scrub_flags &
657 				    (PFSTATE_NODF|PFSTATE_RANDOMID|
658 				    PFSTATE_SETTOS|PFSTATE_SCRUB_TCP|
659 				    PFSTATE_SETPRIO));
660 				if (r->dnpipe || r->dnrpipe) {
661 					if (r->free_flags & PFRULE_DN_IS_PIPE)
662 						st->state_flags |= PFSTATE_DN_IS_PIPE;
663 					else
664 						st->state_flags &= ~PFSTATE_DN_IS_PIPE;
665 				}
666 				st->act.dnpipe = r->dnpipe;
667 				st->act.dnrpipe = r->dnrpipe;
668 			}
669 			break;
670 		case PFSYNC_MSG_VERSION_1400:
671 			st->state_flags = ntohs(sp->pfs_1400.state_flags);
672 			st->act.qid = ntohs(sp->pfs_1400.qid);
673 			st->act.pqid = ntohs(sp->pfs_1400.pqid);
674 			st->act.dnpipe = ntohs(sp->pfs_1400.dnpipe);
675 			st->act.dnrpipe = ntohs(sp->pfs_1400.dnrpipe);
676 			st->act.rtableid = ntohl(sp->pfs_1400.rtableid);
677 			st->act.min_ttl = sp->pfs_1400.min_ttl;
678 			st->act.set_tos = sp->pfs_1400.set_tos;
679 			st->act.max_mss = ntohs(sp->pfs_1400.max_mss);
680 			st->act.set_prio[0] = sp->pfs_1400.set_prio[0];
681 			st->act.set_prio[1] = sp->pfs_1400.set_prio[1];
682 			st->rt = sp->pfs_1400.rt;
683 			if (st->rt && (st->rt_kif = pfi_kkif_find(sp->pfs_1400.rt_ifname)) == NULL) {
684 				if (V_pf_status.debug >= PF_DEBUG_MISC)
685 					printf("%s: unknown route interface: %s\n",
686 					    __func__, sp->pfs_1400.rt_ifname);
687 				if (flags & PFSYNC_SI_IOCTL)
688 					error = EINVAL;
689 				else
690 					error = 0;
691 				goto cleanup_keys;
692 			}
693 			break;
694 		default:
695 			panic("%s: Unsupported pfsync_msg_version %d",
696 			    __func__, msg_version);
697 	}
698 
699 	st->id = sp->pfs_1301.id;
700 	st->creatorid = sp->pfs_1301.creatorid;
701 	pf_state_peer_ntoh(&sp->pfs_1301.src, &st->src);
702 	pf_state_peer_ntoh(&sp->pfs_1301.dst, &st->dst);
703 
704 	st->rule.ptr = r;
705 	st->nat_rule.ptr = NULL;
706 	st->anchor.ptr = NULL;
707 
708 	st->pfsync_time = time_uptime;
709 	st->sync_state = PFSYNC_S_NONE;
710 
711 	if (!(flags & PFSYNC_SI_IOCTL))
712 		st->state_flags |= PFSTATE_NOSYNC;
713 
714 	if ((error = pf_state_insert(kif, kif, skw, sks, st)) != 0)
715 		goto cleanup_state;
716 
717 	/* XXX when we have nat_rule/anchors, use STATE_INC_COUNTERS */
718 	counter_u64_add(r->states_cur, 1);
719 	counter_u64_add(r->states_tot, 1);
720 
721 	if (!(flags & PFSYNC_SI_IOCTL)) {
722 		st->state_flags &= ~PFSTATE_NOSYNC;
723 		if (st->state_flags & PFSTATE_ACK) {
724 			struct pfsync_bucket *b = pfsync_get_bucket(sc, st);
725 			PFSYNC_BUCKET_LOCK(b);
726 			pfsync_q_ins(st, PFSYNC_S_IACK, true);
727 			PFSYNC_BUCKET_UNLOCK(b);
728 
729 			pfsync_push_all(sc);
730 		}
731 	}
732 	st->state_flags &= ~PFSTATE_ACK;
733 	PF_STATE_UNLOCK(st);
734 
735 	return (0);
736 
737 cleanup:
738 	error = ENOMEM;
739 cleanup_keys:
740 	if (skw == sks)
741 		sks = NULL;
742 	uma_zfree(V_pf_state_key_z, skw);
743 	uma_zfree(V_pf_state_key_z, sks);
744 
745 cleanup_state:	/* pf_state_insert() frees the state keys. */
746 	if (st) {
747 		st->timeout = PFTM_UNLINKED; /* appease an assert */
748 		pf_free_state(st);
749 	}
750 	return (error);
751 }
752 
753 #ifdef INET
754 static int
755 pfsync_input(struct mbuf **mp, int *offp __unused, int proto __unused)
756 {
757 	struct pfsync_softc *sc = V_pfsyncif;
758 	struct mbuf *m = *mp;
759 	struct ip *ip = mtod(m, struct ip *);
760 	struct pfsync_header *ph;
761 	struct pfsync_subheader subh;
762 
763 	int offset, len, flags = 0;
764 	int rv;
765 	uint16_t count;
766 
767 	PF_RULES_RLOCK_TRACKER;
768 
769 	*mp = NULL;
770 	V_pfsyncstats.pfsyncs_ipackets++;
771 
772 	/* Verify that we have a sync interface configured. */
773 	if (!sc || !sc->sc_sync_if || !V_pf_status.running ||
774 	    (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
775 		goto done;
776 
777 	/* verify that the packet came in on the right interface */
778 	if (sc->sc_sync_if != m->m_pkthdr.rcvif) {
779 		V_pfsyncstats.pfsyncs_badif++;
780 		goto done;
781 	}
782 
783 	if_inc_counter(sc->sc_ifp, IFCOUNTER_IPACKETS, 1);
784 	if_inc_counter(sc->sc_ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
785 	/* verify that the IP TTL is 255. */
786 	if (ip->ip_ttl != PFSYNC_DFLTTL) {
787 		V_pfsyncstats.pfsyncs_badttl++;
788 		goto done;
789 	}
790 
791 	offset = ip->ip_hl << 2;
792 	if (m->m_pkthdr.len < offset + sizeof(*ph)) {
793 		V_pfsyncstats.pfsyncs_hdrops++;
794 		goto done;
795 	}
796 
797 	if (offset + sizeof(*ph) > m->m_len) {
798 		if (m_pullup(m, offset + sizeof(*ph)) == NULL) {
799 			V_pfsyncstats.pfsyncs_hdrops++;
800 			return (IPPROTO_DONE);
801 		}
802 		ip = mtod(m, struct ip *);
803 	}
804 	ph = (struct pfsync_header *)((char *)ip + offset);
805 
806 	/* verify the version */
807 	if (ph->version != PFSYNC_VERSION) {
808 		V_pfsyncstats.pfsyncs_badver++;
809 		goto done;
810 	}
811 
812 	len = ntohs(ph->len) + offset;
813 	if (m->m_pkthdr.len < len) {
814 		V_pfsyncstats.pfsyncs_badlen++;
815 		goto done;
816 	}
817 
818 	/*
819 	 * Trusting pf_chksum during packet processing, as well as seeking
820 	 * in interface name tree, require holding PF_RULES_RLOCK().
821 	 */
822 	PF_RULES_RLOCK();
823 	if (!bcmp(&ph->pfcksum, &V_pf_status.pf_chksum, PF_MD5_DIGEST_LENGTH))
824 		flags = PFSYNC_SI_CKSUM;
825 
826 	offset += sizeof(*ph);
827 	while (offset <= len - sizeof(subh)) {
828 		m_copydata(m, offset, sizeof(subh), (caddr_t)&subh);
829 		offset += sizeof(subh);
830 
831 		if (subh.action >= PFSYNC_ACT_MAX) {
832 			V_pfsyncstats.pfsyncs_badact++;
833 			PF_RULES_RUNLOCK();
834 			goto done;
835 		}
836 
837 		count = ntohs(subh.count);
838 		V_pfsyncstats.pfsyncs_iacts[subh.action] += count;
839 		rv = (*pfsync_acts[subh.action])(m, offset, count, flags, subh.action);
840 		if (rv == -1) {
841 			PF_RULES_RUNLOCK();
842 			return (IPPROTO_DONE);
843 		}
844 
845 		offset += rv;
846 	}
847 	PF_RULES_RUNLOCK();
848 
849 done:
850 	m_freem(m);
851 	return (IPPROTO_DONE);
852 }
853 #endif
854 
855 #ifdef INET6
856 static int
857 pfsync6_input(struct mbuf **mp, int *offp __unused, int proto __unused)
858 {
859 	struct pfsync_softc *sc = V_pfsyncif;
860 	struct mbuf *m = *mp;
861 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
862 	struct pfsync_header *ph;
863 	struct pfsync_subheader subh;
864 
865 	int offset, len, flags = 0;
866 	int rv;
867 	uint16_t count;
868 
869 	PF_RULES_RLOCK_TRACKER;
870 
871 	*mp = NULL;
872 	V_pfsyncstats.pfsyncs_ipackets++;
873 
874 	/* Verify that we have a sync interface configured. */
875 	if (!sc || !sc->sc_sync_if || !V_pf_status.running ||
876 	    (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
877 		goto done;
878 
879 	/* verify that the packet came in on the right interface */
880 	if (sc->sc_sync_if != m->m_pkthdr.rcvif) {
881 		V_pfsyncstats.pfsyncs_badif++;
882 		goto done;
883 	}
884 
885 	if_inc_counter(sc->sc_ifp, IFCOUNTER_IPACKETS, 1);
886 	if_inc_counter(sc->sc_ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
887 	/* verify that the IP TTL is 255. */
888 	if (ip6->ip6_hlim != PFSYNC_DFLTTL) {
889 		V_pfsyncstats.pfsyncs_badttl++;
890 		goto done;
891 	}
892 
893 
894 	offset = sizeof(*ip6);
895 	if (m->m_pkthdr.len < offset + sizeof(*ph)) {
896 		V_pfsyncstats.pfsyncs_hdrops++;
897 		goto done;
898 	}
899 
900 	if (offset + sizeof(*ph) > m->m_len) {
901 		if (m_pullup(m, offset + sizeof(*ph)) == NULL) {
902 			V_pfsyncstats.pfsyncs_hdrops++;
903 			return (IPPROTO_DONE);
904 		}
905 		ip6 = mtod(m, struct ip6_hdr *);
906 	}
907 	ph = (struct pfsync_header *)((char *)ip6 + offset);
908 
909 	/* verify the version */
910 	if (ph->version != PFSYNC_VERSION) {
911 		V_pfsyncstats.pfsyncs_badver++;
912 		goto done;
913 	}
914 
915 	len = ntohs(ph->len) + offset;
916 	if (m->m_pkthdr.len < len) {
917 		V_pfsyncstats.pfsyncs_badlen++;
918 		goto done;
919 	}
920 
921 	/*
922 	 * Trusting pf_chksum during packet processing, as well as seeking
923 	 * in interface name tree, require holding PF_RULES_RLOCK().
924 	 */
925 	PF_RULES_RLOCK();
926 	if (!bcmp(&ph->pfcksum, &V_pf_status.pf_chksum, PF_MD5_DIGEST_LENGTH))
927 		flags = PFSYNC_SI_CKSUM;
928 
929 	offset += sizeof(*ph);
930 	while (offset <= len - sizeof(subh)) {
931 		m_copydata(m, offset, sizeof(subh), (caddr_t)&subh);
932 		offset += sizeof(subh);
933 
934 		if (subh.action >= PFSYNC_ACT_MAX) {
935 			V_pfsyncstats.pfsyncs_badact++;
936 			PF_RULES_RUNLOCK();
937 			goto done;
938 		}
939 
940 		count = ntohs(subh.count);
941 		V_pfsyncstats.pfsyncs_iacts[subh.action] += count;
942 		rv = (*pfsync_acts[subh.action])(m, offset, count, flags, subh.action);
943 		if (rv == -1) {
944 			PF_RULES_RUNLOCK();
945 			return (IPPROTO_DONE);
946 		}
947 
948 		offset += rv;
949 	}
950 	PF_RULES_RUNLOCK();
951 
952 done:
953 	m_freem(m);
954 	return (IPPROTO_DONE);
955 }
956 #endif
957 
958 static int
959 pfsync_in_clr(struct mbuf *m, int offset, int count, int flags, int action)
960 {
961 	struct pfsync_clr *clr;
962 	struct mbuf *mp;
963 	int len = sizeof(*clr) * count;
964 	int i, offp;
965 	u_int32_t creatorid;
966 
967 	mp = m_pulldown(m, offset, len, &offp);
968 	if (mp == NULL) {
969 		V_pfsyncstats.pfsyncs_badlen++;
970 		return (-1);
971 	}
972 	clr = (struct pfsync_clr *)(mp->m_data + offp);
973 
974 	for (i = 0; i < count; i++) {
975 		creatorid = clr[i].creatorid;
976 
977 		if (clr[i].ifname[0] != '\0' &&
978 		    pfi_kkif_find(clr[i].ifname) == NULL)
979 			continue;
980 
981 		for (int i = 0; i <= pf_hashmask; i++) {
982 			struct pf_idhash *ih = &V_pf_idhash[i];
983 			struct pf_kstate *s;
984 relock:
985 			PF_HASHROW_LOCK(ih);
986 			LIST_FOREACH(s, &ih->states, entry) {
987 				if (s->creatorid == creatorid) {
988 					s->state_flags |= PFSTATE_NOSYNC;
989 					pf_unlink_state(s);
990 					goto relock;
991 				}
992 			}
993 			PF_HASHROW_UNLOCK(ih);
994 		}
995 	}
996 
997 	return (len);
998 }
999 
1000 static int
1001 pfsync_in_ins(struct mbuf *m, int offset, int count, int flags, int action)
1002 {
1003 	struct mbuf *mp;
1004 	union pfsync_state_union *sa, *sp;
1005 	int i, offp, len, msg_version;
1006 
1007 	switch (action) {
1008 		case PFSYNC_ACT_INS_1301:
1009 			len = sizeof(struct pfsync_state_1301) * count;
1010 			msg_version = PFSYNC_MSG_VERSION_1301;
1011 			break;
1012 		case PFSYNC_ACT_INS_1400:
1013 			len = sizeof(struct pfsync_state_1400) * count;
1014 			msg_version = PFSYNC_MSG_VERSION_1400;
1015 			break;
1016 		default:
1017 			V_pfsyncstats.pfsyncs_badact++;
1018 			return (-1);
1019 	}
1020 
1021 	mp = m_pulldown(m, offset, len, &offp);
1022 	if (mp == NULL) {
1023 		V_pfsyncstats.pfsyncs_badlen++;
1024 		return (-1);
1025 	}
1026 	sa = (union pfsync_state_union *)(mp->m_data + offp);
1027 
1028 	for (i = 0; i < count; i++) {
1029 		sp = &sa[i];
1030 
1031 		/* Check for invalid values. */
1032 		if (sp->pfs_1301.timeout >= PFTM_MAX ||
1033 		    sp->pfs_1301.src.state > PF_TCPS_PROXY_DST ||
1034 		    sp->pfs_1301.dst.state > PF_TCPS_PROXY_DST ||
1035 		    sp->pfs_1301.direction > PF_OUT ||
1036 		    (sp->pfs_1301.af != AF_INET &&
1037 		    sp->pfs_1301.af != AF_INET6)) {
1038 			if (V_pf_status.debug >= PF_DEBUG_MISC)
1039 				printf("%s: invalid value\n", __func__);
1040 			V_pfsyncstats.pfsyncs_badval++;
1041 			continue;
1042 		}
1043 
1044 		if (pfsync_state_import(sp, flags, msg_version) == ENOMEM)
1045 			/* Drop out, but process the rest of the actions. */
1046 			break;
1047 	}
1048 
1049 	return (len);
1050 }
1051 
1052 static int
1053 pfsync_in_iack(struct mbuf *m, int offset, int count, int flags, int action)
1054 {
1055 	struct pfsync_ins_ack *ia, *iaa;
1056 	struct pf_kstate *st;
1057 
1058 	struct mbuf *mp;
1059 	int len = count * sizeof(*ia);
1060 	int offp, i;
1061 
1062 	mp = m_pulldown(m, offset, len, &offp);
1063 	if (mp == NULL) {
1064 		V_pfsyncstats.pfsyncs_badlen++;
1065 		return (-1);
1066 	}
1067 	iaa = (struct pfsync_ins_ack *)(mp->m_data + offp);
1068 
1069 	for (i = 0; i < count; i++) {
1070 		ia = &iaa[i];
1071 
1072 		st = pf_find_state_byid(ia->id, ia->creatorid);
1073 		if (st == NULL)
1074 			continue;
1075 
1076 		if (st->state_flags & PFSTATE_ACK) {
1077 			pfsync_undefer_state(st, 0);
1078 		}
1079 		PF_STATE_UNLOCK(st);
1080 	}
1081 	/*
1082 	 * XXX this is not yet implemented, but we know the size of the
1083 	 * message so we can skip it.
1084 	 */
1085 
1086 	return (count * sizeof(struct pfsync_ins_ack));
1087 }
1088 
1089 static int
1090 pfsync_upd_tcp(struct pf_kstate *st, struct pfsync_state_peer *src,
1091     struct pfsync_state_peer *dst)
1092 {
1093 	int sync = 0;
1094 
1095 	PF_STATE_LOCK_ASSERT(st);
1096 
1097 	/*
1098 	 * The state should never go backwards except
1099 	 * for syn-proxy states.  Neither should the
1100 	 * sequence window slide backwards.
1101 	 */
1102 	if ((st->src.state > src->state &&
1103 	    (st->src.state < PF_TCPS_PROXY_SRC ||
1104 	    src->state >= PF_TCPS_PROXY_SRC)) ||
1105 
1106 	    (st->src.state == src->state &&
1107 	    SEQ_GT(st->src.seqlo, ntohl(src->seqlo))))
1108 		sync++;
1109 	else
1110 		pf_state_peer_ntoh(src, &st->src);
1111 
1112 	if ((st->dst.state > dst->state) ||
1113 
1114 	    (st->dst.state >= TCPS_SYN_SENT &&
1115 	    SEQ_GT(st->dst.seqlo, ntohl(dst->seqlo))))
1116 		sync++;
1117 	else
1118 		pf_state_peer_ntoh(dst, &st->dst);
1119 
1120 	return (sync);
1121 }
1122 
1123 static int
1124 pfsync_in_upd(struct mbuf *m, int offset, int count, int flags, int action)
1125 {
1126 	struct pfsync_softc *sc = V_pfsyncif;
1127 	union pfsync_state_union *sa, *sp;
1128 	struct pf_kstate *st;
1129 	struct mbuf *mp;
1130 	int sync, offp, i, len, msg_version;
1131 
1132 	switch (action) {
1133 		case PFSYNC_ACT_UPD_1301:
1134 			len = sizeof(struct pfsync_state_1301) * count;
1135 			msg_version = PFSYNC_MSG_VERSION_1301;
1136 			break;
1137 		case PFSYNC_ACT_UPD_1400:
1138 			len = sizeof(struct pfsync_state_1400) * count;
1139 			msg_version = PFSYNC_MSG_VERSION_1400;
1140 			break;
1141 		default:
1142 			V_pfsyncstats.pfsyncs_badact++;
1143 			return (-1);
1144 	}
1145 
1146 	mp = m_pulldown(m, offset, len, &offp);
1147 	if (mp == NULL) {
1148 		V_pfsyncstats.pfsyncs_badlen++;
1149 		return (-1);
1150 	}
1151 	sa = (union pfsync_state_union *)(mp->m_data + offp);
1152 
1153 	for (i = 0; i < count; i++) {
1154 		sp = &sa[i];
1155 
1156 		/* check for invalid values */
1157 		if (sp->pfs_1301.timeout >= PFTM_MAX ||
1158 		    sp->pfs_1301.src.state > PF_TCPS_PROXY_DST ||
1159 		    sp->pfs_1301.dst.state > PF_TCPS_PROXY_DST) {
1160 			if (V_pf_status.debug >= PF_DEBUG_MISC) {
1161 				printf("pfsync_input: PFSYNC_ACT_UPD: "
1162 				    "invalid value\n");
1163 			}
1164 			V_pfsyncstats.pfsyncs_badval++;
1165 			continue;
1166 		}
1167 
1168 		st = pf_find_state_byid(sp->pfs_1301.id, sp->pfs_1301.creatorid);
1169 		if (st == NULL) {
1170 			/* insert the update */
1171 			if (pfsync_state_import(sp, flags, msg_version))
1172 				V_pfsyncstats.pfsyncs_badstate++;
1173 			continue;
1174 		}
1175 
1176 		if (st->state_flags & PFSTATE_ACK) {
1177 			pfsync_undefer_state(st, 1);
1178 		}
1179 
1180 		if (st->key[PF_SK_WIRE]->proto == IPPROTO_TCP)
1181 			sync = pfsync_upd_tcp(st, &sp->pfs_1301.src, &sp->pfs_1301.dst);
1182 		else {
1183 			sync = 0;
1184 
1185 			/*
1186 			 * Non-TCP protocol state machine always go
1187 			 * forwards
1188 			 */
1189 			if (st->src.state > sp->pfs_1301.src.state)
1190 				sync++;
1191 			else
1192 				pf_state_peer_ntoh(&sp->pfs_1301.src, &st->src);
1193 			if (st->dst.state > sp->pfs_1301.dst.state)
1194 				sync++;
1195 			else
1196 				pf_state_peer_ntoh(&sp->pfs_1301.dst, &st->dst);
1197 		}
1198 		if (sync < 2) {
1199 			pfsync_alloc_scrub_memory(&sp->pfs_1301.dst, &st->dst);
1200 			pf_state_peer_ntoh(&sp->pfs_1301.dst, &st->dst);
1201 			st->expire = time_uptime;
1202 			st->timeout = sp->pfs_1301.timeout;
1203 		}
1204 		st->pfsync_time = time_uptime;
1205 
1206 		if (sync) {
1207 			V_pfsyncstats.pfsyncs_stale++;
1208 
1209 			pfsync_update_state(st);
1210 			PF_STATE_UNLOCK(st);
1211 			pfsync_push_all(sc);
1212 			continue;
1213 		}
1214 		PF_STATE_UNLOCK(st);
1215 	}
1216 
1217 	return (len);
1218 }
1219 
1220 static int
1221 pfsync_in_upd_c(struct mbuf *m, int offset, int count, int flags, int action)
1222 {
1223 	struct pfsync_softc *sc = V_pfsyncif;
1224 	struct pfsync_upd_c *ua, *up;
1225 	struct pf_kstate *st;
1226 	int len = count * sizeof(*up);
1227 	int sync;
1228 	struct mbuf *mp;
1229 	int offp, i;
1230 
1231 	mp = m_pulldown(m, offset, len, &offp);
1232 	if (mp == NULL) {
1233 		V_pfsyncstats.pfsyncs_badlen++;
1234 		return (-1);
1235 	}
1236 	ua = (struct pfsync_upd_c *)(mp->m_data + offp);
1237 
1238 	for (i = 0; i < count; i++) {
1239 		up = &ua[i];
1240 
1241 		/* check for invalid values */
1242 		if (up->timeout >= PFTM_MAX ||
1243 		    up->src.state > PF_TCPS_PROXY_DST ||
1244 		    up->dst.state > PF_TCPS_PROXY_DST) {
1245 			if (V_pf_status.debug >= PF_DEBUG_MISC) {
1246 				printf("pfsync_input: "
1247 				    "PFSYNC_ACT_UPD_C: "
1248 				    "invalid value\n");
1249 			}
1250 			V_pfsyncstats.pfsyncs_badval++;
1251 			continue;
1252 		}
1253 
1254 		st = pf_find_state_byid(up->id, up->creatorid);
1255 		if (st == NULL) {
1256 			/* We don't have this state. Ask for it. */
1257 			PFSYNC_BUCKET_LOCK(&sc->sc_buckets[0]);
1258 			pfsync_request_update(up->creatorid, up->id);
1259 			PFSYNC_BUCKET_UNLOCK(&sc->sc_buckets[0]);
1260 			continue;
1261 		}
1262 
1263 		if (st->state_flags & PFSTATE_ACK) {
1264 			pfsync_undefer_state(st, 1);
1265 		}
1266 
1267 		if (st->key[PF_SK_WIRE]->proto == IPPROTO_TCP)
1268 			sync = pfsync_upd_tcp(st, &up->src, &up->dst);
1269 		else {
1270 			sync = 0;
1271 
1272 			/*
1273 			 * Non-TCP protocol state machine always go
1274 			 * forwards
1275 			 */
1276 			if (st->src.state > up->src.state)
1277 				sync++;
1278 			else
1279 				pf_state_peer_ntoh(&up->src, &st->src);
1280 			if (st->dst.state > up->dst.state)
1281 				sync++;
1282 			else
1283 				pf_state_peer_ntoh(&up->dst, &st->dst);
1284 		}
1285 		if (sync < 2) {
1286 			pfsync_alloc_scrub_memory(&up->dst, &st->dst);
1287 			pf_state_peer_ntoh(&up->dst, &st->dst);
1288 			st->expire = time_uptime;
1289 			st->timeout = up->timeout;
1290 		}
1291 		st->pfsync_time = time_uptime;
1292 
1293 		if (sync) {
1294 			V_pfsyncstats.pfsyncs_stale++;
1295 
1296 			pfsync_update_state(st);
1297 			PF_STATE_UNLOCK(st);
1298 			pfsync_push_all(sc);
1299 			continue;
1300 		}
1301 		PF_STATE_UNLOCK(st);
1302 	}
1303 
1304 	return (len);
1305 }
1306 
1307 static int
1308 pfsync_in_ureq(struct mbuf *m, int offset, int count, int flags, int action)
1309 {
1310 	struct pfsync_upd_req *ur, *ura;
1311 	struct mbuf *mp;
1312 	int len = count * sizeof(*ur);
1313 	int i, offp;
1314 
1315 	struct pf_kstate *st;
1316 
1317 	mp = m_pulldown(m, offset, len, &offp);
1318 	if (mp == NULL) {
1319 		V_pfsyncstats.pfsyncs_badlen++;
1320 		return (-1);
1321 	}
1322 	ura = (struct pfsync_upd_req *)(mp->m_data + offp);
1323 
1324 	for (i = 0; i < count; i++) {
1325 		ur = &ura[i];
1326 
1327 		if (ur->id == 0 && ur->creatorid == 0)
1328 			pfsync_bulk_start();
1329 		else {
1330 			st = pf_find_state_byid(ur->id, ur->creatorid);
1331 			if (st == NULL) {
1332 				V_pfsyncstats.pfsyncs_badstate++;
1333 				continue;
1334 			}
1335 			if (st->state_flags & PFSTATE_NOSYNC) {
1336 				PF_STATE_UNLOCK(st);
1337 				continue;
1338 			}
1339 
1340 			pfsync_update_state_req(st);
1341 			PF_STATE_UNLOCK(st);
1342 		}
1343 	}
1344 
1345 	return (len);
1346 }
1347 
1348 static int
1349 pfsync_in_del_c(struct mbuf *m, int offset, int count, int flags, int action)
1350 {
1351 	struct mbuf *mp;
1352 	struct pfsync_del_c *sa, *sp;
1353 	struct pf_kstate *st;
1354 	int len = count * sizeof(*sp);
1355 	int offp, i;
1356 
1357 	mp = m_pulldown(m, offset, len, &offp);
1358 	if (mp == NULL) {
1359 		V_pfsyncstats.pfsyncs_badlen++;
1360 		return (-1);
1361 	}
1362 	sa = (struct pfsync_del_c *)(mp->m_data + offp);
1363 
1364 	for (i = 0; i < count; i++) {
1365 		sp = &sa[i];
1366 
1367 		st = pf_find_state_byid(sp->id, sp->creatorid);
1368 		if (st == NULL) {
1369 			V_pfsyncstats.pfsyncs_badstate++;
1370 			continue;
1371 		}
1372 
1373 		st->state_flags |= PFSTATE_NOSYNC;
1374 		pf_unlink_state(st);
1375 	}
1376 
1377 	return (len);
1378 }
1379 
1380 static int
1381 pfsync_in_bus(struct mbuf *m, int offset, int count, int flags, int action)
1382 {
1383 	struct pfsync_softc *sc = V_pfsyncif;
1384 	struct pfsync_bus *bus;
1385 	struct mbuf *mp;
1386 	int len = count * sizeof(*bus);
1387 	int offp;
1388 
1389 	PFSYNC_BLOCK(sc);
1390 
1391 	/* If we're not waiting for a bulk update, who cares. */
1392 	if (sc->sc_ureq_sent == 0) {
1393 		PFSYNC_BUNLOCK(sc);
1394 		return (len);
1395 	}
1396 
1397 	mp = m_pulldown(m, offset, len, &offp);
1398 	if (mp == NULL) {
1399 		PFSYNC_BUNLOCK(sc);
1400 		V_pfsyncstats.pfsyncs_badlen++;
1401 		return (-1);
1402 	}
1403 	bus = (struct pfsync_bus *)(mp->m_data + offp);
1404 
1405 	switch (bus->status) {
1406 	case PFSYNC_BUS_START:
1407 		callout_reset(&sc->sc_bulkfail_tmo, 4 * hz +
1408 		    V_pf_limits[PF_LIMIT_STATES].limit /
1409 		    ((sc->sc_ifp->if_mtu - PFSYNC_MINPKT) /
1410 		    sizeof(union pfsync_state_union)),
1411 		    pfsync_bulk_fail, sc);
1412 		if (V_pf_status.debug >= PF_DEBUG_MISC)
1413 			printf("pfsync: received bulk update start\n");
1414 		break;
1415 
1416 	case PFSYNC_BUS_END:
1417 		if (time_uptime - ntohl(bus->endtime) >=
1418 		    sc->sc_ureq_sent) {
1419 			/* that's it, we're happy */
1420 			sc->sc_ureq_sent = 0;
1421 			sc->sc_bulk_tries = 0;
1422 			callout_stop(&sc->sc_bulkfail_tmo);
1423 			if (!(sc->sc_flags & PFSYNCF_OK) && carp_demote_adj_p)
1424 				(*carp_demote_adj_p)(-V_pfsync_carp_adj,
1425 				    "pfsync bulk done");
1426 			sc->sc_flags |= PFSYNCF_OK;
1427 			if (V_pf_status.debug >= PF_DEBUG_MISC)
1428 				printf("pfsync: received valid "
1429 				    "bulk update end\n");
1430 		} else {
1431 			if (V_pf_status.debug >= PF_DEBUG_MISC)
1432 				printf("pfsync: received invalid "
1433 				    "bulk update end: bad timestamp\n");
1434 		}
1435 		break;
1436 	}
1437 	PFSYNC_BUNLOCK(sc);
1438 
1439 	return (len);
1440 }
1441 
1442 static int
1443 pfsync_in_tdb(struct mbuf *m, int offset, int count, int flags, int action)
1444 {
1445 	int len = count * sizeof(struct pfsync_tdb);
1446 
1447 #if defined(IPSEC)
1448 	struct pfsync_tdb *tp;
1449 	struct mbuf *mp;
1450 	int offp;
1451 	int i;
1452 	int s;
1453 
1454 	mp = m_pulldown(m, offset, len, &offp);
1455 	if (mp == NULL) {
1456 		V_pfsyncstats.pfsyncs_badlen++;
1457 		return (-1);
1458 	}
1459 	tp = (struct pfsync_tdb *)(mp->m_data + offp);
1460 
1461 	for (i = 0; i < count; i++)
1462 		pfsync_update_net_tdb(&tp[i]);
1463 #endif
1464 
1465 	return (len);
1466 }
1467 
1468 #if defined(IPSEC)
1469 /* Update an in-kernel tdb. Silently fail if no tdb is found. */
1470 static void
1471 pfsync_update_net_tdb(struct pfsync_tdb *pt)
1472 {
1473 	struct tdb		*tdb;
1474 	int			 s;
1475 
1476 	/* check for invalid values */
1477 	if (ntohl(pt->spi) <= SPI_RESERVED_MAX ||
1478 	    (pt->dst.sa.sa_family != AF_INET &&
1479 	    pt->dst.sa.sa_family != AF_INET6))
1480 		goto bad;
1481 
1482 	tdb = gettdb(pt->spi, &pt->dst, pt->sproto);
1483 	if (tdb) {
1484 		pt->rpl = ntohl(pt->rpl);
1485 		pt->cur_bytes = (unsigned long long)be64toh(pt->cur_bytes);
1486 
1487 		/* Neither replay nor byte counter should ever decrease. */
1488 		if (pt->rpl < tdb->tdb_rpl ||
1489 		    pt->cur_bytes < tdb->tdb_cur_bytes) {
1490 			goto bad;
1491 		}
1492 
1493 		tdb->tdb_rpl = pt->rpl;
1494 		tdb->tdb_cur_bytes = pt->cur_bytes;
1495 	}
1496 	return;
1497 
1498 bad:
1499 	if (V_pf_status.debug >= PF_DEBUG_MISC)
1500 		printf("pfsync_insert: PFSYNC_ACT_TDB_UPD: "
1501 		    "invalid value\n");
1502 	V_pfsyncstats.pfsyncs_badstate++;
1503 	return;
1504 }
1505 #endif
1506 
1507 static int
1508 pfsync_in_eof(struct mbuf *m, int offset, int count, int flags, int action)
1509 {
1510 	/* check if we are at the right place in the packet */
1511 	if (offset != m->m_pkthdr.len)
1512 		V_pfsyncstats.pfsyncs_badlen++;
1513 
1514 	/* we're done. free and let the caller return */
1515 	m_freem(m);
1516 	return (-1);
1517 }
1518 
1519 static int
1520 pfsync_in_error(struct mbuf *m, int offset, int count, int flags, int action)
1521 {
1522 	V_pfsyncstats.pfsyncs_badact++;
1523 
1524 	m_freem(m);
1525 	return (-1);
1526 }
1527 
1528 static int
1529 pfsyncoutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
1530 	struct route *rt)
1531 {
1532 	m_freem(m);
1533 	return (0);
1534 }
1535 
1536 /* ARGSUSED */
1537 static int
1538 pfsyncioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1539 {
1540 	struct pfsync_softc *sc = ifp->if_softc;
1541 	struct ifreq *ifr = (struct ifreq *)data;
1542 	struct pfsyncreq pfsyncr;
1543 	size_t nvbuflen;
1544 	int error;
1545 	int c;
1546 
1547 	switch (cmd) {
1548 	case SIOCSIFFLAGS:
1549 		PFSYNC_LOCK(sc);
1550 		if (ifp->if_flags & IFF_UP) {
1551 			ifp->if_drv_flags |= IFF_DRV_RUNNING;
1552 			PFSYNC_UNLOCK(sc);
1553 			pfsync_pointers_init();
1554 		} else {
1555 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1556 			PFSYNC_UNLOCK(sc);
1557 			pfsync_pointers_uninit();
1558 		}
1559 		break;
1560 	case SIOCSIFMTU:
1561 		if (!sc->sc_sync_if ||
1562 		    ifr->ifr_mtu <= PFSYNC_MINPKT ||
1563 		    ifr->ifr_mtu > sc->sc_sync_if->if_mtu)
1564 			return (EINVAL);
1565 		if (ifr->ifr_mtu < ifp->if_mtu) {
1566 			for (c = 0; c < pfsync_buckets; c++) {
1567 				PFSYNC_BUCKET_LOCK(&sc->sc_buckets[c]);
1568 				if (sc->sc_buckets[c].b_len > PFSYNC_MINPKT)
1569 					pfsync_sendout(1, c);
1570 				PFSYNC_BUCKET_UNLOCK(&sc->sc_buckets[c]);
1571 			}
1572 		}
1573 		ifp->if_mtu = ifr->ifr_mtu;
1574 		break;
1575 	case SIOCGETPFSYNC:
1576 		bzero(&pfsyncr, sizeof(pfsyncr));
1577 		PFSYNC_LOCK(sc);
1578 		if (sc->sc_sync_if) {
1579 			strlcpy(pfsyncr.pfsyncr_syncdev,
1580 			    sc->sc_sync_if->if_xname, IFNAMSIZ);
1581 		}
1582 		pfsyncr.pfsyncr_syncpeer = ((struct sockaddr_in *)&sc->sc_sync_peer)->sin_addr;
1583 		pfsyncr.pfsyncr_maxupdates = sc->sc_maxupdates;
1584 		pfsyncr.pfsyncr_defer = sc->sc_flags;
1585 		PFSYNC_UNLOCK(sc);
1586 		return (copyout(&pfsyncr, ifr_data_get_ptr(ifr),
1587 		    sizeof(pfsyncr)));
1588 
1589 	case SIOCGETPFSYNCNV:
1590 	    {
1591 		nvlist_t *nvl_syncpeer;
1592 		nvlist_t *nvl = nvlist_create(0);
1593 
1594 		if (nvl == NULL)
1595 			return (ENOMEM);
1596 
1597 		if (sc->sc_sync_if)
1598 			nvlist_add_string(nvl, "syncdev", sc->sc_sync_if->if_xname);
1599 		nvlist_add_number(nvl, "maxupdates", sc->sc_maxupdates);
1600 		nvlist_add_number(nvl, "flags", sc->sc_flags);
1601 		nvlist_add_number(nvl, "version", sc->sc_version);
1602 		if ((nvl_syncpeer = pfsync_sockaddr_to_syncpeer_nvlist(&sc->sc_sync_peer)) != NULL)
1603 			nvlist_add_nvlist(nvl, "syncpeer", nvl_syncpeer);
1604 
1605 		void *packed = NULL;
1606 		packed = nvlist_pack(nvl, &nvbuflen);
1607 		if (packed == NULL) {
1608 			free(packed, M_NVLIST);
1609 			nvlist_destroy(nvl);
1610 			return (ENOMEM);
1611 		}
1612 
1613 		if (nvbuflen > ifr->ifr_cap_nv.buf_length) {
1614 			ifr->ifr_cap_nv.length = nvbuflen;
1615 			ifr->ifr_cap_nv.buffer = NULL;
1616 			free(packed, M_NVLIST);
1617 			nvlist_destroy(nvl);
1618 			return (EFBIG);
1619 		}
1620 
1621 		ifr->ifr_cap_nv.length = nvbuflen;
1622 		error = copyout(packed, ifr->ifr_cap_nv.buffer, nvbuflen);
1623 
1624 		nvlist_destroy(nvl);
1625 		nvlist_destroy(nvl_syncpeer);
1626 		free(packed, M_NVLIST);
1627 		break;
1628 	    }
1629 
1630 	case SIOCSETPFSYNC:
1631 	    {
1632 		struct pfsync_kstatus status;
1633 
1634 		if ((error = priv_check(curthread, PRIV_NETINET_PF)) != 0)
1635 			return (error);
1636 		if ((error = copyin(ifr_data_get_ptr(ifr), &pfsyncr,
1637 		    sizeof(pfsyncr))))
1638 			return (error);
1639 
1640 		memset((char *)&status, 0, sizeof(struct pfsync_kstatus));
1641 		pfsync_pfsyncreq_to_kstatus(&pfsyncr, &status);
1642 
1643 		error = pfsync_kstatus_to_softc(&status, sc);
1644 		return (error);
1645 	    }
1646 	case SIOCSETPFSYNCNV:
1647 	    {
1648 		struct pfsync_kstatus status;
1649 		void *data;
1650 		nvlist_t *nvl;
1651 
1652 		if ((error = priv_check(curthread, PRIV_NETINET_PF)) != 0)
1653 			return (error);
1654 		if (ifr->ifr_cap_nv.length > IFR_CAP_NV_MAXBUFSIZE)
1655 			return (EINVAL);
1656 
1657 		data = malloc(ifr->ifr_cap_nv.length, M_TEMP, M_WAITOK);
1658 
1659 		if ((error = copyin(ifr->ifr_cap_nv.buffer, data,
1660 		    ifr->ifr_cap_nv.length)) != 0) {
1661 			free(data, M_TEMP);
1662 			return (error);
1663 		}
1664 
1665 		if ((nvl = nvlist_unpack(data, ifr->ifr_cap_nv.length, 0)) == NULL) {
1666 			free(data, M_TEMP);
1667 			return (EINVAL);
1668 		}
1669 
1670 		memset((char *)&status, 0, sizeof(struct pfsync_kstatus));
1671 		pfsync_nvstatus_to_kstatus(nvl, &status);
1672 
1673 		nvlist_destroy(nvl);
1674 		free(data, M_TEMP);
1675 
1676 		error = pfsync_kstatus_to_softc(&status, sc);
1677 		return (error);
1678 	    }
1679 	default:
1680 		return (ENOTTY);
1681 	}
1682 
1683 	return (0);
1684 }
1685 
1686 static void
1687 pfsync_out_state_1301(struct pf_kstate *st, void *buf)
1688 {
1689 	union pfsync_state_union *sp = buf;
1690 
1691 	pfsync_state_export(sp, st, PFSYNC_MSG_VERSION_1301);
1692 }
1693 
1694 static void
1695 pfsync_out_state_1400(struct pf_kstate *st, void *buf)
1696 {
1697 	union pfsync_state_union *sp = buf;
1698 
1699 	pfsync_state_export(sp, st, PFSYNC_MSG_VERSION_1400);
1700 }
1701 
1702 static void
1703 pfsync_out_iack(struct pf_kstate *st, void *buf)
1704 {
1705 	struct pfsync_ins_ack *iack = buf;
1706 
1707 	iack->id = st->id;
1708 	iack->creatorid = st->creatorid;
1709 }
1710 
1711 static void
1712 pfsync_out_upd_c(struct pf_kstate *st, void *buf)
1713 {
1714 	struct pfsync_upd_c *up = buf;
1715 
1716 	bzero(up, sizeof(*up));
1717 	up->id = st->id;
1718 	pf_state_peer_hton(&st->src, &up->src);
1719 	pf_state_peer_hton(&st->dst, &up->dst);
1720 	up->creatorid = st->creatorid;
1721 	up->timeout = st->timeout;
1722 }
1723 
1724 static void
1725 pfsync_out_del_c(struct pf_kstate *st, void *buf)
1726 {
1727 	struct pfsync_del_c *dp = buf;
1728 
1729 	dp->id = st->id;
1730 	dp->creatorid = st->creatorid;
1731 	st->state_flags |= PFSTATE_NOSYNC;
1732 }
1733 
1734 static void
1735 pfsync_drop(struct pfsync_softc *sc)
1736 {
1737 	struct pf_kstate *st, *next;
1738 	struct pfsync_upd_req_item *ur;
1739 	struct pfsync_bucket *b;
1740 	int c;
1741 	enum pfsync_q_id q;
1742 
1743 	for (c = 0; c < pfsync_buckets; c++) {
1744 		b = &sc->sc_buckets[c];
1745 		for (q = 0; q < PFSYNC_Q_COUNT; q++) {
1746 			if (TAILQ_EMPTY(&b->b_qs[q]))
1747 				continue;
1748 
1749 			TAILQ_FOREACH_SAFE(st, &b->b_qs[q], sync_list, next) {
1750 				KASSERT(st->sync_state == pfsync_qid_sstate[q],
1751 					("%s: st->sync_state == q",
1752 						__func__));
1753 				st->sync_state = PFSYNC_S_NONE;
1754 				pf_release_state(st);
1755 			}
1756 			TAILQ_INIT(&b->b_qs[q]);
1757 		}
1758 
1759 		while ((ur = TAILQ_FIRST(&b->b_upd_req_list)) != NULL) {
1760 			TAILQ_REMOVE(&b->b_upd_req_list, ur, ur_entry);
1761 			free(ur, M_PFSYNC);
1762 		}
1763 
1764 		b->b_len = PFSYNC_MINPKT;
1765 		b->b_plus = NULL;
1766 	}
1767 }
1768 
1769 static void
1770 pfsync_sendout(int schedswi, int c)
1771 {
1772 	struct pfsync_softc *sc = V_pfsyncif;
1773 	struct ifnet *ifp = sc->sc_ifp;
1774 	struct mbuf *m;
1775 	struct pfsync_header *ph;
1776 	struct pfsync_subheader *subh;
1777 	struct pf_kstate *st, *st_next;
1778 	struct pfsync_upd_req_item *ur;
1779 	struct pfsync_bucket *b = &sc->sc_buckets[c];
1780 	int aflen, offset, count = 0;
1781 	enum pfsync_q_id q;
1782 
1783 	KASSERT(sc != NULL, ("%s: null sc", __func__));
1784 	KASSERT(b->b_len > PFSYNC_MINPKT,
1785 	    ("%s: sc_len %zu", __func__, b->b_len));
1786 	PFSYNC_BUCKET_LOCK_ASSERT(b);
1787 
1788 	if (ifp->if_bpf == NULL && sc->sc_sync_if == NULL) {
1789 		pfsync_drop(sc);
1790 		return;
1791 	}
1792 
1793 	m = m_get2(max_linkhdr + b->b_len, M_NOWAIT, MT_DATA, M_PKTHDR);
1794 	if (m == NULL) {
1795 		if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
1796 		V_pfsyncstats.pfsyncs_onomem++;
1797 		return;
1798 	}
1799 	m->m_data += max_linkhdr;
1800 	m->m_len = m->m_pkthdr.len = b->b_len;
1801 
1802 	/* build the ip header */
1803 	switch (sc->sc_sync_peer.ss_family) {
1804 #ifdef INET
1805 	case AF_INET:
1806 	    {
1807 		struct ip *ip;
1808 
1809 		ip = mtod(m, struct ip *);
1810 		bcopy(&sc->sc_template.ipv4, ip, sizeof(*ip));
1811 		aflen = offset = sizeof(*ip);
1812 
1813 		ip->ip_len = htons(m->m_pkthdr.len);
1814 		ip_fillid(ip);
1815 		break;
1816 	    }
1817 #endif
1818 #ifdef INET6
1819 	case AF_INET6:
1820 		{
1821 		struct ip6_hdr *ip6;
1822 
1823 		ip6 = mtod(m, struct ip6_hdr *);
1824 		bcopy(&sc->sc_template.ipv6, ip6, sizeof(*ip6));
1825 		aflen = offset = sizeof(*ip6);
1826 
1827 		ip6->ip6_plen = htons(m->m_pkthdr.len);
1828 		break;
1829 		}
1830 #endif
1831 	default:
1832 		m_freem(m);
1833 		return;
1834 	}
1835 
1836 	/* build the pfsync header */
1837 	ph = (struct pfsync_header *)(m->m_data + offset);
1838 	bzero(ph, sizeof(*ph));
1839 	offset += sizeof(*ph);
1840 
1841 	ph->version = PFSYNC_VERSION;
1842 	ph->len = htons(b->b_len - aflen);
1843 	bcopy(V_pf_status.pf_chksum, ph->pfcksum, PF_MD5_DIGEST_LENGTH);
1844 
1845 	/* walk the queues */
1846 	for (q = 0; q < PFSYNC_Q_COUNT; q++) {
1847 		if (TAILQ_EMPTY(&b->b_qs[q]))
1848 			continue;
1849 
1850 		subh = (struct pfsync_subheader *)(m->m_data + offset);
1851 		offset += sizeof(*subh);
1852 
1853 		count = 0;
1854 		TAILQ_FOREACH_SAFE(st, &b->b_qs[q], sync_list, st_next) {
1855 			KASSERT(st->sync_state == pfsync_qid_sstate[q],
1856 				("%s: st->sync_state == q",
1857 					__func__));
1858 			/*
1859 			 * XXXGL: some of write methods do unlocked reads
1860 			 * of state data :(
1861 			 */
1862 			pfsync_qs[q].write(st, m->m_data + offset);
1863 			offset += pfsync_qs[q].len;
1864 			st->sync_state = PFSYNC_S_NONE;
1865 			pf_release_state(st);
1866 			count++;
1867 		}
1868 		TAILQ_INIT(&b->b_qs[q]);
1869 
1870 		bzero(subh, sizeof(*subh));
1871 		subh->action = pfsync_qs[q].action;
1872 		subh->count = htons(count);
1873 		V_pfsyncstats.pfsyncs_oacts[pfsync_qs[q].action] += count;
1874 	}
1875 
1876 	if (!TAILQ_EMPTY(&b->b_upd_req_list)) {
1877 		subh = (struct pfsync_subheader *)(m->m_data + offset);
1878 		offset += sizeof(*subh);
1879 
1880 		count = 0;
1881 		while ((ur = TAILQ_FIRST(&b->b_upd_req_list)) != NULL) {
1882 			TAILQ_REMOVE(&b->b_upd_req_list, ur, ur_entry);
1883 
1884 			bcopy(&ur->ur_msg, m->m_data + offset,
1885 			    sizeof(ur->ur_msg));
1886 			offset += sizeof(ur->ur_msg);
1887 			free(ur, M_PFSYNC);
1888 			count++;
1889 		}
1890 
1891 		bzero(subh, sizeof(*subh));
1892 		subh->action = PFSYNC_ACT_UPD_REQ;
1893 		subh->count = htons(count);
1894 		V_pfsyncstats.pfsyncs_oacts[PFSYNC_ACT_UPD_REQ] += count;
1895 	}
1896 
1897 	/* has someone built a custom region for us to add? */
1898 	if (b->b_plus != NULL) {
1899 		bcopy(b->b_plus, m->m_data + offset, b->b_pluslen);
1900 		offset += b->b_pluslen;
1901 
1902 		b->b_plus = NULL;
1903 	}
1904 
1905 	subh = (struct pfsync_subheader *)(m->m_data + offset);
1906 	offset += sizeof(*subh);
1907 
1908 	bzero(subh, sizeof(*subh));
1909 	subh->action = PFSYNC_ACT_EOF;
1910 	subh->count = htons(1);
1911 	V_pfsyncstats.pfsyncs_oacts[PFSYNC_ACT_EOF]++;
1912 
1913 	/* we're done, let's put it on the wire */
1914 	if (ifp->if_bpf) {
1915 		m->m_data += aflen;
1916 		m->m_len = m->m_pkthdr.len = b->b_len - aflen;
1917 		BPF_MTAP(ifp, m);
1918 		m->m_data -= aflen;
1919 		m->m_len = m->m_pkthdr.len = b->b_len;
1920 	}
1921 
1922 	if (sc->sc_sync_if == NULL) {
1923 		b->b_len = PFSYNC_MINPKT;
1924 		m_freem(m);
1925 		return;
1926 	}
1927 
1928 	if_inc_counter(sc->sc_ifp, IFCOUNTER_OPACKETS, 1);
1929 	if_inc_counter(sc->sc_ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
1930 	b->b_len = PFSYNC_MINPKT;
1931 
1932 	if (!_IF_QFULL(&b->b_snd))
1933 		_IF_ENQUEUE(&b->b_snd, m);
1934 	else {
1935 		m_freem(m);
1936 		if_inc_counter(sc->sc_ifp, IFCOUNTER_OQDROPS, 1);
1937 	}
1938 	if (schedswi)
1939 		swi_sched(V_pfsync_swi_cookie, 0);
1940 }
1941 
1942 static void
1943 pfsync_insert_state(struct pf_kstate *st)
1944 {
1945 	struct pfsync_softc *sc = V_pfsyncif;
1946 	struct pfsync_bucket *b = pfsync_get_bucket(sc, st);
1947 
1948 	if (st->state_flags & PFSTATE_NOSYNC)
1949 		return;
1950 
1951 	if ((st->rule.ptr->rule_flag & PFRULE_NOSYNC) ||
1952 	    st->key[PF_SK_WIRE]->proto == IPPROTO_PFSYNC) {
1953 		st->state_flags |= PFSTATE_NOSYNC;
1954 		return;
1955 	}
1956 
1957 	KASSERT(st->sync_state == PFSYNC_S_NONE,
1958 		("%s: st->sync_state %u", __func__, st->sync_state));
1959 
1960 	PFSYNC_BUCKET_LOCK(b);
1961 	if (b->b_len == PFSYNC_MINPKT)
1962 		callout_reset(&b->b_tmo, 1 * hz, pfsync_timeout, b);
1963 
1964 	pfsync_q_ins(st, PFSYNC_S_INS, true);
1965 	PFSYNC_BUCKET_UNLOCK(b);
1966 
1967 	st->sync_updates = 0;
1968 }
1969 
1970 static int
1971 pfsync_defer(struct pf_kstate *st, struct mbuf *m)
1972 {
1973 	struct pfsync_softc *sc = V_pfsyncif;
1974 	struct pfsync_deferral *pd;
1975 	struct pfsync_bucket *b;
1976 
1977 	if (m->m_flags & (M_BCAST|M_MCAST))
1978 		return (0);
1979 
1980 	if (sc == NULL)
1981 		return (0);
1982 
1983 	b = pfsync_get_bucket(sc, st);
1984 
1985 	PFSYNC_LOCK(sc);
1986 
1987 	if (!(sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) ||
1988 	    !(sc->sc_flags & PFSYNCF_DEFER)) {
1989 		PFSYNC_UNLOCK(sc);
1990 		return (0);
1991 	}
1992 
1993 	PFSYNC_BUCKET_LOCK(b);
1994 	PFSYNC_UNLOCK(sc);
1995 
1996 	if (b->b_deferred >= 128)
1997 		pfsync_undefer(TAILQ_FIRST(&b->b_deferrals), 0);
1998 
1999 	pd = malloc(sizeof(*pd), M_PFSYNC, M_NOWAIT);
2000 	if (pd == NULL) {
2001 		PFSYNC_BUCKET_UNLOCK(b);
2002 		return (0);
2003 	}
2004 	b->b_deferred++;
2005 
2006 	m->m_flags |= M_SKIP_FIREWALL;
2007 	st->state_flags |= PFSTATE_ACK;
2008 
2009 	pd->pd_sc = sc;
2010 	pd->pd_st = st;
2011 	pf_ref_state(st);
2012 	pd->pd_m = m;
2013 
2014 	TAILQ_INSERT_TAIL(&b->b_deferrals, pd, pd_entry);
2015 	callout_init_mtx(&pd->pd_tmo, &b->b_mtx, CALLOUT_RETURNUNLOCKED);
2016 	callout_reset(&pd->pd_tmo, (V_pfsync_defer_timeout * hz) / 1000,
2017 	    pfsync_defer_tmo, pd);
2018 
2019 	pfsync_push(b);
2020 	PFSYNC_BUCKET_UNLOCK(b);
2021 
2022 	return (1);
2023 }
2024 
2025 static void
2026 pfsync_undefer(struct pfsync_deferral *pd, int drop)
2027 {
2028 	struct pfsync_softc *sc = pd->pd_sc;
2029 	struct mbuf *m = pd->pd_m;
2030 	struct pf_kstate *st = pd->pd_st;
2031 	struct pfsync_bucket *b = pfsync_get_bucket(sc, st);
2032 
2033 	PFSYNC_BUCKET_LOCK_ASSERT(b);
2034 
2035 	TAILQ_REMOVE(&b->b_deferrals, pd, pd_entry);
2036 	b->b_deferred--;
2037 	pd->pd_st->state_flags &= ~PFSTATE_ACK;	/* XXX: locking! */
2038 	free(pd, M_PFSYNC);
2039 	pf_release_state(st);
2040 
2041 	if (drop)
2042 		m_freem(m);
2043 	else {
2044 		_IF_ENQUEUE(&b->b_snd, m);
2045 		pfsync_push(b);
2046 	}
2047 }
2048 
2049 static void
2050 pfsync_defer_tmo(void *arg)
2051 {
2052 	struct epoch_tracker et;
2053 	struct pfsync_deferral *pd = arg;
2054 	struct pfsync_softc *sc = pd->pd_sc;
2055 	struct mbuf *m = pd->pd_m;
2056 	struct pf_kstate *st = pd->pd_st;
2057 	struct pfsync_bucket *b = pfsync_get_bucket(sc, st);
2058 
2059 	PFSYNC_BUCKET_LOCK_ASSERT(b);
2060 
2061 	TAILQ_REMOVE(&b->b_deferrals, pd, pd_entry);
2062 	b->b_deferred--;
2063 	pd->pd_st->state_flags &= ~PFSTATE_ACK;	/* XXX: locking! */
2064 	PFSYNC_BUCKET_UNLOCK(b);
2065 	free(pd, M_PFSYNC);
2066 
2067 	if (sc->sc_sync_if == NULL) {
2068 		pf_release_state(st);
2069 		m_freem(m);
2070 		return;
2071 	}
2072 
2073 	NET_EPOCH_ENTER(et);
2074 	CURVNET_SET(sc->sc_sync_if->if_vnet);
2075 
2076 	pfsync_tx(sc, m);
2077 
2078 	pf_release_state(st);
2079 
2080 	CURVNET_RESTORE();
2081 	NET_EPOCH_EXIT(et);
2082 }
2083 
2084 static void
2085 pfsync_undefer_state_locked(struct pf_kstate *st, int drop)
2086 {
2087 	struct pfsync_softc *sc = V_pfsyncif;
2088 	struct pfsync_deferral *pd;
2089 	struct pfsync_bucket *b = pfsync_get_bucket(sc, st);
2090 
2091 	PFSYNC_BUCKET_LOCK_ASSERT(b);
2092 
2093 	TAILQ_FOREACH(pd, &b->b_deferrals, pd_entry) {
2094 		 if (pd->pd_st == st) {
2095 			if (callout_stop(&pd->pd_tmo) > 0)
2096 				pfsync_undefer(pd, drop);
2097 
2098 			return;
2099 		}
2100 	}
2101 
2102 	panic("%s: unable to find deferred state", __func__);
2103 }
2104 
2105 static void
2106 pfsync_undefer_state(struct pf_kstate *st, int drop)
2107 {
2108 	struct pfsync_softc *sc = V_pfsyncif;
2109 	struct pfsync_bucket *b = pfsync_get_bucket(sc, st);
2110 
2111 	PFSYNC_BUCKET_LOCK(b);
2112 	pfsync_undefer_state_locked(st, drop);
2113 	PFSYNC_BUCKET_UNLOCK(b);
2114 }
2115 
2116 static struct pfsync_bucket*
2117 pfsync_get_bucket(struct pfsync_softc *sc, struct pf_kstate *st)
2118 {
2119 	int c = PF_IDHASH(st) % pfsync_buckets;
2120 	return &sc->sc_buckets[c];
2121 }
2122 
2123 static void
2124 pfsync_update_state(struct pf_kstate *st)
2125 {
2126 	struct pfsync_softc *sc = V_pfsyncif;
2127 	bool sync = false, ref = true;
2128 	struct pfsync_bucket *b = pfsync_get_bucket(sc, st);
2129 
2130 	PF_STATE_LOCK_ASSERT(st);
2131 	PFSYNC_BUCKET_LOCK(b);
2132 
2133 	if (st->state_flags & PFSTATE_ACK)
2134 		pfsync_undefer_state_locked(st, 0);
2135 	if (st->state_flags & PFSTATE_NOSYNC) {
2136 		if (st->sync_state != PFSYNC_S_NONE)
2137 			pfsync_q_del(st, true, b);
2138 		PFSYNC_BUCKET_UNLOCK(b);
2139 		return;
2140 	}
2141 
2142 	if (b->b_len == PFSYNC_MINPKT)
2143 		callout_reset(&b->b_tmo, 1 * hz, pfsync_timeout, b);
2144 
2145 	switch (st->sync_state) {
2146 	case PFSYNC_S_UPD_C:
2147 	case PFSYNC_S_UPD:
2148 	case PFSYNC_S_INS:
2149 		/* we're already handling it */
2150 
2151 		if (st->key[PF_SK_WIRE]->proto == IPPROTO_TCP) {
2152 			st->sync_updates++;
2153 			if (st->sync_updates >= sc->sc_maxupdates)
2154 				sync = true;
2155 		}
2156 		break;
2157 
2158 	case PFSYNC_S_IACK:
2159 		pfsync_q_del(st, false, b);
2160 		ref = false;
2161 		/* FALLTHROUGH */
2162 
2163 	case PFSYNC_S_NONE:
2164 		pfsync_q_ins(st, PFSYNC_S_UPD_C, ref);
2165 		st->sync_updates = 0;
2166 		break;
2167 
2168 	default:
2169 		panic("%s: unexpected sync state %d", __func__, st->sync_state);
2170 	}
2171 
2172 	if (sync || (time_uptime - st->pfsync_time) < 2)
2173 		pfsync_push(b);
2174 
2175 	PFSYNC_BUCKET_UNLOCK(b);
2176 }
2177 
2178 static void
2179 pfsync_request_update(u_int32_t creatorid, u_int64_t id)
2180 {
2181 	struct pfsync_softc *sc = V_pfsyncif;
2182 	struct pfsync_bucket *b = &sc->sc_buckets[0];
2183 	struct pfsync_upd_req_item *item;
2184 	size_t nlen = sizeof(struct pfsync_upd_req);
2185 
2186 	PFSYNC_BUCKET_LOCK_ASSERT(b);
2187 
2188 	/*
2189 	 * This code does a bit to prevent multiple update requests for the
2190 	 * same state being generated. It searches current subheader queue,
2191 	 * but it doesn't lookup into queue of already packed datagrams.
2192 	 */
2193 	TAILQ_FOREACH(item, &b->b_upd_req_list, ur_entry)
2194 		if (item->ur_msg.id == id &&
2195 		    item->ur_msg.creatorid == creatorid)
2196 			return;
2197 
2198 	item = malloc(sizeof(*item), M_PFSYNC, M_NOWAIT);
2199 	if (item == NULL)
2200 		return; /* XXX stats */
2201 
2202 	item->ur_msg.id = id;
2203 	item->ur_msg.creatorid = creatorid;
2204 
2205 	if (TAILQ_EMPTY(&b->b_upd_req_list))
2206 		nlen += sizeof(struct pfsync_subheader);
2207 
2208 	if (b->b_len + nlen > sc->sc_ifp->if_mtu) {
2209 		pfsync_sendout(0, 0);
2210 
2211 		nlen = sizeof(struct pfsync_subheader) +
2212 		    sizeof(struct pfsync_upd_req);
2213 	}
2214 
2215 	TAILQ_INSERT_TAIL(&b->b_upd_req_list, item, ur_entry);
2216 	b->b_len += nlen;
2217 
2218 	pfsync_push(b);
2219 }
2220 
2221 static bool
2222 pfsync_update_state_req(struct pf_kstate *st)
2223 {
2224 	struct pfsync_softc *sc = V_pfsyncif;
2225 	bool ref = true, full = false;
2226 	struct pfsync_bucket *b = pfsync_get_bucket(sc, st);
2227 
2228 	PF_STATE_LOCK_ASSERT(st);
2229 	PFSYNC_BUCKET_LOCK(b);
2230 
2231 	if (st->state_flags & PFSTATE_NOSYNC) {
2232 		if (st->sync_state != PFSYNC_S_NONE)
2233 			pfsync_q_del(st, true, b);
2234 		PFSYNC_BUCKET_UNLOCK(b);
2235 		return (full);
2236 	}
2237 
2238 	switch (st->sync_state) {
2239 	case PFSYNC_S_UPD_C:
2240 	case PFSYNC_S_IACK:
2241 		pfsync_q_del(st, false, b);
2242 		ref = false;
2243 		/* FALLTHROUGH */
2244 
2245 	case PFSYNC_S_NONE:
2246 		pfsync_q_ins(st, PFSYNC_S_UPD, ref);
2247 		pfsync_push(b);
2248 		break;
2249 
2250 	case PFSYNC_S_INS:
2251 	case PFSYNC_S_UPD:
2252 	case PFSYNC_S_DEL_C:
2253 		/* we're already handling it */
2254 		break;
2255 
2256 	default:
2257 		panic("%s: unexpected sync state %d", __func__, st->sync_state);
2258 	}
2259 
2260 	if ((sc->sc_ifp->if_mtu - b->b_len) < sizeof(union pfsync_state_union))
2261 		full = true;
2262 
2263 	PFSYNC_BUCKET_UNLOCK(b);
2264 
2265 	return (full);
2266 }
2267 
2268 static void
2269 pfsync_delete_state(struct pf_kstate *st)
2270 {
2271 	struct pfsync_softc *sc = V_pfsyncif;
2272 	struct pfsync_bucket *b = pfsync_get_bucket(sc, st);
2273 	bool ref = true;
2274 
2275 	PFSYNC_BUCKET_LOCK(b);
2276 	if (st->state_flags & PFSTATE_ACK)
2277 		pfsync_undefer_state_locked(st, 1);
2278 	if (st->state_flags & PFSTATE_NOSYNC) {
2279 		if (st->sync_state != PFSYNC_S_NONE)
2280 			pfsync_q_del(st, true, b);
2281 		PFSYNC_BUCKET_UNLOCK(b);
2282 		return;
2283 	}
2284 
2285 	if (b->b_len == PFSYNC_MINPKT)
2286 		callout_reset(&b->b_tmo, 1 * hz, pfsync_timeout, b);
2287 
2288 	switch (st->sync_state) {
2289 	case PFSYNC_S_INS:
2290 		/* We never got to tell the world so just forget about it. */
2291 		pfsync_q_del(st, true, b);
2292 		break;
2293 
2294 	case PFSYNC_S_UPD_C:
2295 	case PFSYNC_S_UPD:
2296 	case PFSYNC_S_IACK:
2297 		pfsync_q_del(st, false, b);
2298 		ref = false;
2299 		/* FALLTHROUGH */
2300 
2301 	case PFSYNC_S_NONE:
2302 		pfsync_q_ins(st, PFSYNC_S_DEL_C, ref);
2303 		break;
2304 
2305 	default:
2306 		panic("%s: unexpected sync state %d", __func__, st->sync_state);
2307 	}
2308 
2309 	PFSYNC_BUCKET_UNLOCK(b);
2310 }
2311 
2312 static void
2313 pfsync_clear_states(u_int32_t creatorid, const char *ifname)
2314 {
2315 	struct {
2316 		struct pfsync_subheader subh;
2317 		struct pfsync_clr clr;
2318 	} __packed r;
2319 
2320 	bzero(&r, sizeof(r));
2321 
2322 	r.subh.action = PFSYNC_ACT_CLR;
2323 	r.subh.count = htons(1);
2324 	V_pfsyncstats.pfsyncs_oacts[PFSYNC_ACT_CLR]++;
2325 
2326 	strlcpy(r.clr.ifname, ifname, sizeof(r.clr.ifname));
2327 	r.clr.creatorid = creatorid;
2328 
2329 	pfsync_send_plus(&r, sizeof(r));
2330 }
2331 
2332 static enum pfsync_q_id
2333 pfsync_sstate_to_qid(u_int8_t sync_state)
2334 {
2335 	struct pfsync_softc *sc = V_pfsyncif;
2336 
2337 	switch (sync_state) {
2338 		case PFSYNC_S_INS:
2339 			switch (sc->sc_version) {
2340 				case PFSYNC_MSG_VERSION_1301:
2341 					return PFSYNC_Q_INS_1301;
2342 				case PFSYNC_MSG_VERSION_1400:
2343 					return PFSYNC_Q_INS_1400;
2344 			}
2345 			break;
2346 		case PFSYNC_S_IACK:
2347 			return PFSYNC_Q_IACK;
2348 		case PFSYNC_S_UPD:
2349 			switch (sc->sc_version) {
2350 				case PFSYNC_MSG_VERSION_1301:
2351 					return PFSYNC_Q_UPD_1301;
2352 				case PFSYNC_MSG_VERSION_1400:
2353 					return PFSYNC_Q_UPD_1400;
2354 			}
2355 			break;
2356 		case PFSYNC_S_UPD_C:
2357 			return PFSYNC_Q_UPD_C;
2358 		case PFSYNC_S_DEL_C:
2359 			return PFSYNC_Q_DEL_C;
2360 		default:
2361 			panic("%s: Unsupported st->sync_state 0x%02x",
2362 			__func__, sync_state);
2363 	}
2364 
2365 	panic("%s: Unsupported pfsync_msg_version %d",
2366 	    __func__, sc->sc_version);
2367 }
2368 
2369 static void
2370 pfsync_q_ins(struct pf_kstate *st, int sync_state, bool ref)
2371 {
2372 	enum pfsync_q_id q = pfsync_sstate_to_qid(sync_state);
2373 	struct pfsync_softc *sc = V_pfsyncif;
2374 	size_t nlen = pfsync_qs[q].len;
2375 	struct pfsync_bucket *b = pfsync_get_bucket(sc, st);
2376 
2377 	PFSYNC_BUCKET_LOCK_ASSERT(b);
2378 
2379 	KASSERT(st->sync_state == PFSYNC_S_NONE,
2380 		("%s: st->sync_state %u", __func__, st->sync_state));
2381 	KASSERT(b->b_len >= PFSYNC_MINPKT, ("pfsync pkt len is too low %zu",
2382 	    b->b_len));
2383 
2384 	if (TAILQ_EMPTY(&b->b_qs[q]))
2385 		nlen += sizeof(struct pfsync_subheader);
2386 
2387 	if (b->b_len + nlen > sc->sc_ifp->if_mtu) {
2388 		pfsync_sendout(1, b->b_id);
2389 
2390 		nlen = sizeof(struct pfsync_subheader) + pfsync_qs[q].len;
2391 	}
2392 
2393 	b->b_len += nlen;
2394 	TAILQ_INSERT_TAIL(&b->b_qs[q], st, sync_list);
2395 	st->sync_state = pfsync_qid_sstate[q];
2396 	if (ref)
2397 		pf_ref_state(st);
2398 }
2399 
2400 static void
2401 pfsync_q_del(struct pf_kstate *st, bool unref, struct pfsync_bucket *b)
2402 {
2403 	enum pfsync_q_id q;
2404 
2405 	PFSYNC_BUCKET_LOCK_ASSERT(b);
2406 	KASSERT(st->sync_state != PFSYNC_S_NONE,
2407 		("%s: st->sync_state != PFSYNC_S_NONE", __func__));
2408 
2409 	q =  pfsync_sstate_to_qid(st->sync_state);
2410 	b->b_len -= pfsync_qs[q].len;
2411 	TAILQ_REMOVE(&b->b_qs[q], st, sync_list);
2412 	st->sync_state = PFSYNC_S_NONE;
2413 	if (unref)
2414 		pf_release_state(st);
2415 
2416 	if (TAILQ_EMPTY(&b->b_qs[q]))
2417 		b->b_len -= sizeof(struct pfsync_subheader);
2418 }
2419 
2420 static void
2421 pfsync_bulk_start(void)
2422 {
2423 	struct pfsync_softc *sc = V_pfsyncif;
2424 
2425 	if (V_pf_status.debug >= PF_DEBUG_MISC)
2426 		printf("pfsync: received bulk update request\n");
2427 
2428 	PFSYNC_BLOCK(sc);
2429 
2430 	sc->sc_ureq_received = time_uptime;
2431 	sc->sc_bulk_hashid = 0;
2432 	sc->sc_bulk_stateid = 0;
2433 	pfsync_bulk_status(PFSYNC_BUS_START);
2434 	callout_reset(&sc->sc_bulk_tmo, 1, pfsync_bulk_update, sc);
2435 	PFSYNC_BUNLOCK(sc);
2436 }
2437 
2438 static void
2439 pfsync_bulk_update(void *arg)
2440 {
2441 	struct pfsync_softc *sc = arg;
2442 	struct pf_kstate *s;
2443 	int i;
2444 
2445 	PFSYNC_BLOCK_ASSERT(sc);
2446 	CURVNET_SET(sc->sc_ifp->if_vnet);
2447 
2448 	/*
2449 	 * Start with last state from previous invocation.
2450 	 * It may had gone, in this case start from the
2451 	 * hash slot.
2452 	 */
2453 	s = pf_find_state_byid(sc->sc_bulk_stateid, sc->sc_bulk_creatorid);
2454 
2455 	if (s != NULL)
2456 		i = PF_IDHASH(s);
2457 	else
2458 		i = sc->sc_bulk_hashid;
2459 
2460 	for (; i <= pf_hashmask; i++) {
2461 		struct pf_idhash *ih = &V_pf_idhash[i];
2462 
2463 		if (s != NULL)
2464 			PF_HASHROW_ASSERT(ih);
2465 		else {
2466 			PF_HASHROW_LOCK(ih);
2467 			s = LIST_FIRST(&ih->states);
2468 		}
2469 
2470 		for (; s; s = LIST_NEXT(s, entry)) {
2471 			if (s->sync_state == PFSYNC_S_NONE &&
2472 			    s->timeout < PFTM_MAX &&
2473 			    s->pfsync_time <= sc->sc_ureq_received) {
2474 				if (pfsync_update_state_req(s)) {
2475 					/* We've filled a packet. */
2476 					sc->sc_bulk_hashid = i;
2477 					sc->sc_bulk_stateid = s->id;
2478 					sc->sc_bulk_creatorid = s->creatorid;
2479 					PF_HASHROW_UNLOCK(ih);
2480 					callout_reset(&sc->sc_bulk_tmo, 1,
2481 					    pfsync_bulk_update, sc);
2482 					goto full;
2483 				}
2484 			}
2485 		}
2486 		PF_HASHROW_UNLOCK(ih);
2487 	}
2488 
2489 	/* We're done. */
2490 	pfsync_bulk_status(PFSYNC_BUS_END);
2491 full:
2492 	CURVNET_RESTORE();
2493 }
2494 
2495 static void
2496 pfsync_bulk_status(u_int8_t status)
2497 {
2498 	struct {
2499 		struct pfsync_subheader subh;
2500 		struct pfsync_bus bus;
2501 	} __packed r;
2502 
2503 	struct pfsync_softc *sc = V_pfsyncif;
2504 
2505 	bzero(&r, sizeof(r));
2506 
2507 	r.subh.action = PFSYNC_ACT_BUS;
2508 	r.subh.count = htons(1);
2509 	V_pfsyncstats.pfsyncs_oacts[PFSYNC_ACT_BUS]++;
2510 
2511 	r.bus.creatorid = V_pf_status.hostid;
2512 	r.bus.endtime = htonl(time_uptime - sc->sc_ureq_received);
2513 	r.bus.status = status;
2514 
2515 	pfsync_send_plus(&r, sizeof(r));
2516 }
2517 
2518 static void
2519 pfsync_bulk_fail(void *arg)
2520 {
2521 	struct pfsync_softc *sc = arg;
2522 	struct pfsync_bucket *b = &sc->sc_buckets[0];
2523 
2524 	CURVNET_SET(sc->sc_ifp->if_vnet);
2525 
2526 	PFSYNC_BLOCK_ASSERT(sc);
2527 
2528 	if (sc->sc_bulk_tries++ < PFSYNC_MAX_BULKTRIES) {
2529 		/* Try again */
2530 		callout_reset(&sc->sc_bulkfail_tmo, 5 * hz,
2531 		    pfsync_bulk_fail, V_pfsyncif);
2532 		PFSYNC_BUCKET_LOCK(b);
2533 		pfsync_request_update(0, 0);
2534 		PFSYNC_BUCKET_UNLOCK(b);
2535 	} else {
2536 		/* Pretend like the transfer was ok. */
2537 		sc->sc_ureq_sent = 0;
2538 		sc->sc_bulk_tries = 0;
2539 		PFSYNC_LOCK(sc);
2540 		if (!(sc->sc_flags & PFSYNCF_OK) && carp_demote_adj_p)
2541 			(*carp_demote_adj_p)(-V_pfsync_carp_adj,
2542 			    "pfsync bulk fail");
2543 		sc->sc_flags |= PFSYNCF_OK;
2544 		PFSYNC_UNLOCK(sc);
2545 		if (V_pf_status.debug >= PF_DEBUG_MISC)
2546 			printf("pfsync: failed to receive bulk update\n");
2547 	}
2548 
2549 	CURVNET_RESTORE();
2550 }
2551 
2552 static void
2553 pfsync_send_plus(void *plus, size_t pluslen)
2554 {
2555 	struct pfsync_softc *sc = V_pfsyncif;
2556 	struct pfsync_bucket *b = &sc->sc_buckets[0];
2557 
2558 	PFSYNC_BUCKET_LOCK(b);
2559 
2560 	if (b->b_len + pluslen > sc->sc_ifp->if_mtu)
2561 		pfsync_sendout(1, b->b_id);
2562 
2563 	b->b_plus = plus;
2564 	b->b_len += (b->b_pluslen = pluslen);
2565 
2566 	pfsync_sendout(1, b->b_id);
2567 	PFSYNC_BUCKET_UNLOCK(b);
2568 }
2569 
2570 static void
2571 pfsync_timeout(void *arg)
2572 {
2573 	struct pfsync_bucket *b = arg;
2574 
2575 	CURVNET_SET(b->b_sc->sc_ifp->if_vnet);
2576 	PFSYNC_BUCKET_LOCK(b);
2577 	pfsync_push(b);
2578 	PFSYNC_BUCKET_UNLOCK(b);
2579 	CURVNET_RESTORE();
2580 }
2581 
2582 static void
2583 pfsync_push(struct pfsync_bucket *b)
2584 {
2585 
2586 	PFSYNC_BUCKET_LOCK_ASSERT(b);
2587 
2588 	b->b_flags |= PFSYNCF_BUCKET_PUSH;
2589 	swi_sched(V_pfsync_swi_cookie, 0);
2590 }
2591 
2592 static void
2593 pfsync_push_all(struct pfsync_softc *sc)
2594 {
2595 	int c;
2596 	struct pfsync_bucket *b;
2597 
2598 	for (c = 0; c < pfsync_buckets; c++) {
2599 		b = &sc->sc_buckets[c];
2600 
2601 		PFSYNC_BUCKET_LOCK(b);
2602 		pfsync_push(b);
2603 		PFSYNC_BUCKET_UNLOCK(b);
2604 	}
2605 }
2606 
2607 static void
2608 pfsync_tx(struct pfsync_softc *sc, struct mbuf *m)
2609 {
2610 	struct ip *ip;
2611 	int af, error = 0;
2612 
2613 	ip = mtod(m, struct ip *);
2614 	MPASS(ip->ip_v == IPVERSION || ip->ip_v == (IPV6_VERSION >> 4));
2615 
2616 	af = ip->ip_v == IPVERSION ? AF_INET : AF_INET6;
2617 
2618 	/*
2619 	 * We distinguish between a deferral packet and our
2620 	 * own pfsync packet based on M_SKIP_FIREWALL
2621 	 * flag. This is XXX.
2622 	 */
2623 	switch (af) {
2624 #ifdef INET
2625 	case AF_INET:
2626 		if (m->m_flags & M_SKIP_FIREWALL) {
2627 			error = ip_output(m, NULL, NULL, 0,
2628 			    NULL, NULL);
2629 		} else {
2630 			error = ip_output(m, NULL, NULL,
2631 			    IP_RAWOUTPUT, &sc->sc_imo, NULL);
2632 		}
2633 		break;
2634 #endif
2635 #ifdef INET6
2636 	case AF_INET6:
2637 		if (m->m_flags & M_SKIP_FIREWALL) {
2638 			error = ip6_output(m, NULL, NULL, 0,
2639 			    NULL, NULL, NULL);
2640 		} else {
2641 			error = ip6_output(m, NULL, NULL, 0,
2642 				&sc->sc_im6o, NULL, NULL);
2643 		}
2644 		break;
2645 #endif
2646 	}
2647 
2648 	if (error == 0)
2649 		V_pfsyncstats.pfsyncs_opackets++;
2650 	else
2651 		V_pfsyncstats.pfsyncs_oerrors++;
2652 
2653 }
2654 
2655 static void
2656 pfsyncintr(void *arg)
2657 {
2658 	struct epoch_tracker et;
2659 	struct pfsync_softc *sc = arg;
2660 	struct pfsync_bucket *b;
2661 	struct mbuf *m, *n;
2662 	int c;
2663 
2664 	NET_EPOCH_ENTER(et);
2665 	CURVNET_SET(sc->sc_ifp->if_vnet);
2666 
2667 	for (c = 0; c < pfsync_buckets; c++) {
2668 		b = &sc->sc_buckets[c];
2669 
2670 		PFSYNC_BUCKET_LOCK(b);
2671 		if ((b->b_flags & PFSYNCF_BUCKET_PUSH) && b->b_len > PFSYNC_MINPKT) {
2672 			pfsync_sendout(0, b->b_id);
2673 			b->b_flags &= ~PFSYNCF_BUCKET_PUSH;
2674 		}
2675 		_IF_DEQUEUE_ALL(&b->b_snd, m);
2676 		PFSYNC_BUCKET_UNLOCK(b);
2677 
2678 		for (; m != NULL; m = n) {
2679 			n = m->m_nextpkt;
2680 			m->m_nextpkt = NULL;
2681 
2682 			pfsync_tx(sc, m);
2683 		}
2684 	}
2685 	CURVNET_RESTORE();
2686 	NET_EPOCH_EXIT(et);
2687 }
2688 
2689 static int
2690 pfsync_multicast_setup(struct pfsync_softc *sc, struct ifnet *ifp,
2691     struct in_mfilter* imf, struct in6_mfilter* im6f)
2692 {
2693 #ifdef  INET
2694 	struct ip_moptions *imo = &sc->sc_imo;
2695 #endif
2696 #ifdef INET6
2697 	struct ip6_moptions *im6o = &sc->sc_im6o;
2698 	struct sockaddr_in6 *syncpeer_sa6 = NULL;
2699 #endif
2700 
2701 	if (!(ifp->if_flags & IFF_MULTICAST))
2702 		return (EADDRNOTAVAIL);
2703 
2704 	switch (sc->sc_sync_peer.ss_family) {
2705 #ifdef INET
2706 	case AF_INET:
2707 	{
2708 		int error;
2709 
2710 		ip_mfilter_init(&imo->imo_head);
2711 		imo->imo_multicast_vif = -1;
2712 		if ((error = in_joingroup(ifp,
2713 		    &((struct sockaddr_in *)&sc->sc_sync_peer)->sin_addr, NULL,
2714 		    &imf->imf_inm)) != 0)
2715 			return (error);
2716 
2717 		ip_mfilter_insert(&imo->imo_head, imf);
2718 		imo->imo_multicast_ifp = ifp;
2719 		imo->imo_multicast_ttl = PFSYNC_DFLTTL;
2720 		imo->imo_multicast_loop = 0;
2721 		break;
2722 	}
2723 #endif
2724 #ifdef INET6
2725 	case AF_INET6:
2726 	{
2727 		int error;
2728 
2729 		syncpeer_sa6 = (struct sockaddr_in6 *)&sc->sc_sync_peer;
2730 		if ((error = in6_setscope(&syncpeer_sa6->sin6_addr, ifp, NULL)))
2731 			return (error);
2732 
2733 		ip6_mfilter_init(&im6o->im6o_head);
2734 		if ((error = in6_joingroup(ifp, &syncpeer_sa6->sin6_addr, NULL,
2735 		    &(im6f->im6f_in6m), 0)) != 0)
2736 			return (error);
2737 
2738 		ip6_mfilter_insert(&im6o->im6o_head, im6f);
2739 		im6o->im6o_multicast_ifp = ifp;
2740 		im6o->im6o_multicast_hlim = PFSYNC_DFLTTL;
2741 		im6o->im6o_multicast_loop = 0;
2742 		break;
2743 	}
2744 #endif
2745 	}
2746 
2747 	return (0);
2748 }
2749 
2750 static void
2751 pfsync_multicast_cleanup(struct pfsync_softc *sc)
2752 {
2753 #ifdef INET
2754 	struct ip_moptions *imo = &sc->sc_imo;
2755 	struct in_mfilter *imf;
2756 
2757 	while ((imf = ip_mfilter_first(&imo->imo_head)) != NULL) {
2758 		ip_mfilter_remove(&imo->imo_head, imf);
2759 		in_leavegroup(imf->imf_inm, NULL);
2760 		ip_mfilter_free(imf);
2761 	}
2762 	imo->imo_multicast_ifp = NULL;
2763 #endif
2764 
2765 #ifdef INET6
2766 	struct ip6_moptions *im6o = &sc->sc_im6o;
2767 	struct in6_mfilter *im6f;
2768 
2769 	while ((im6f = ip6_mfilter_first(&im6o->im6o_head)) != NULL) {
2770 		ip6_mfilter_remove(&im6o->im6o_head, im6f);
2771 		in6_leavegroup(im6f->im6f_in6m, NULL);
2772 		ip6_mfilter_free(im6f);
2773 	}
2774 	im6o->im6o_multicast_ifp = NULL;
2775 #endif
2776 }
2777 
2778 void
2779 pfsync_detach_ifnet(struct ifnet *ifp)
2780 {
2781 	struct pfsync_softc *sc = V_pfsyncif;
2782 
2783 	if (sc == NULL)
2784 		return;
2785 
2786 	PFSYNC_LOCK(sc);
2787 
2788 	if (sc->sc_sync_if == ifp) {
2789 		/* We don't need mutlicast cleanup here, because the interface
2790 		 * is going away. We do need to ensure we don't try to do
2791 		 * cleanup later.
2792 		 */
2793 		ip_mfilter_init(&sc->sc_imo.imo_head);
2794 		sc->sc_imo.imo_multicast_ifp = NULL;
2795 		sc->sc_im6o.im6o_multicast_ifp = NULL;
2796 		sc->sc_sync_if = NULL;
2797 	}
2798 
2799 	PFSYNC_UNLOCK(sc);
2800 }
2801 
2802 static int
2803 pfsync_pfsyncreq_to_kstatus(struct pfsyncreq *pfsyncr, struct pfsync_kstatus *status)
2804 {
2805 	struct sockaddr_storage sa;
2806 	status->maxupdates = pfsyncr->pfsyncr_maxupdates;
2807 	status->flags = pfsyncr->pfsyncr_defer;
2808 
2809 	strlcpy(status->syncdev, pfsyncr->pfsyncr_syncdev, IFNAMSIZ);
2810 
2811 	memset(&sa, 0, sizeof(sa));
2812 	if (pfsyncr->pfsyncr_syncpeer.s_addr != 0) {
2813 		struct sockaddr_in *in = (struct sockaddr_in *)&sa;
2814 		in->sin_family = AF_INET;
2815 		in->sin_len = sizeof(*in);
2816 		in->sin_addr.s_addr = pfsyncr->pfsyncr_syncpeer.s_addr;
2817 	}
2818 	status->syncpeer = sa;
2819 
2820 	return 0;
2821 }
2822 
2823 static int
2824 pfsync_kstatus_to_softc(struct pfsync_kstatus *status, struct pfsync_softc *sc)
2825 {
2826 	struct ifnet *sifp;
2827 	struct in_mfilter *imf = NULL;
2828 	struct in6_mfilter *im6f = NULL;
2829 	int error;
2830 	int c;
2831 
2832 	if ((status->maxupdates < 0) || (status->maxupdates > 255))
2833 		return (EINVAL);
2834 
2835 	if (status->syncdev[0] == '\0')
2836 		sifp = NULL;
2837 	else if ((sifp = ifunit_ref(status->syncdev)) == NULL)
2838 		return (EINVAL);
2839 
2840 	switch (status->syncpeer.ss_family) {
2841 #ifdef INET
2842 	case AF_UNSPEC:
2843 	case AF_INET: {
2844 		struct sockaddr_in *status_sin;
2845 		status_sin = (struct sockaddr_in *)&(status->syncpeer);
2846 		if (sifp != NULL) {
2847 			if (status_sin->sin_addr.s_addr == 0 ||
2848 			    status_sin->sin_addr.s_addr ==
2849 			    htonl(INADDR_PFSYNC_GROUP)) {
2850 				status_sin->sin_family = AF_INET;
2851 				status_sin->sin_len = sizeof(*status_sin);
2852 				status_sin->sin_addr.s_addr =
2853 				    htonl(INADDR_PFSYNC_GROUP);
2854 			}
2855 
2856 			if (IN_MULTICAST(ntohl(status_sin->sin_addr.s_addr))) {
2857 				imf = ip_mfilter_alloc(M_WAITOK, 0, 0);
2858 			}
2859 		}
2860 		break;
2861 	}
2862 #endif
2863 #ifdef INET6
2864 	case AF_INET6: {
2865 		struct sockaddr_in6 *status_sin6;
2866 		status_sin6 = (struct sockaddr_in6*)&(status->syncpeer);
2867 		if (sifp != NULL) {
2868 			if (IN6_IS_ADDR_UNSPECIFIED(&status_sin6->sin6_addr) ||
2869 			    IN6_ARE_ADDR_EQUAL(&status_sin6->sin6_addr,
2870 				&in6addr_linklocal_pfsync_group)) {
2871 				status_sin6->sin6_family = AF_INET6;
2872 				status_sin6->sin6_len = sizeof(*status_sin6);
2873 				status_sin6->sin6_addr =
2874 				    in6addr_linklocal_pfsync_group;
2875 			}
2876 
2877 			if (IN6_IS_ADDR_MULTICAST(&status_sin6->sin6_addr)) {
2878 				im6f = ip6_mfilter_alloc(M_WAITOK, 0, 0);
2879 			}
2880 		}
2881 		break;
2882 	}
2883 #endif
2884 	}
2885 
2886 	PFSYNC_LOCK(sc);
2887 
2888 	switch (status->version) {
2889 		case PFSYNC_MSG_VERSION_UNSPECIFIED:
2890 			sc->sc_version = PFSYNC_MSG_VERSION_DEFAULT;
2891 			break;
2892 		case PFSYNC_MSG_VERSION_1301:
2893 		case PFSYNC_MSG_VERSION_1400:
2894 			sc->sc_version = status->version;
2895 			break;
2896 		default:
2897 			PFSYNC_UNLOCK(sc);
2898 			return (EINVAL);
2899 	}
2900 
2901 	switch (status->syncpeer.ss_family) {
2902 	case AF_INET: {
2903 		struct sockaddr_in *status_sin = (struct sockaddr_in *)&(status->syncpeer);
2904 		struct sockaddr_in *sc_sin = (struct sockaddr_in *)&sc->sc_sync_peer;
2905 		sc_sin->sin_family = AF_INET;
2906 		sc_sin->sin_len = sizeof(*sc_sin);
2907 		if (status_sin->sin_addr.s_addr == 0) {
2908 			sc_sin->sin_addr.s_addr = htonl(INADDR_PFSYNC_GROUP);
2909 		} else {
2910 			sc_sin->sin_addr.s_addr = status_sin->sin_addr.s_addr;
2911 		}
2912 		break;
2913 	}
2914 	case AF_INET6: {
2915 		struct sockaddr_in6 *status_sin = (struct sockaddr_in6 *)&(status->syncpeer);
2916 		struct sockaddr_in6 *sc_sin = (struct sockaddr_in6 *)&sc->sc_sync_peer;
2917 		sc_sin->sin6_family = AF_INET6;
2918 		sc_sin->sin6_len = sizeof(*sc_sin);
2919 		if(IN6_IS_ADDR_UNSPECIFIED(&status_sin->sin6_addr)) {
2920 			sc_sin->sin6_addr = in6addr_linklocal_pfsync_group;
2921 		} else {
2922 			sc_sin->sin6_addr = status_sin->sin6_addr;
2923 		}
2924 		break;
2925 	}
2926 	}
2927 
2928 	sc->sc_maxupdates = status->maxupdates;
2929 	if (status->flags & PFSYNCF_DEFER) {
2930 		sc->sc_flags |= PFSYNCF_DEFER;
2931 		V_pfsync_defer_ptr = pfsync_defer;
2932 	} else {
2933 		sc->sc_flags &= ~PFSYNCF_DEFER;
2934 		V_pfsync_defer_ptr = NULL;
2935 	}
2936 
2937 	if (sifp == NULL) {
2938 		if (sc->sc_sync_if)
2939 			if_rele(sc->sc_sync_if);
2940 		sc->sc_sync_if = NULL;
2941 		pfsync_multicast_cleanup(sc);
2942 		PFSYNC_UNLOCK(sc);
2943 		return (0);
2944 	}
2945 
2946 	for (c = 0; c < pfsync_buckets; c++) {
2947 		PFSYNC_BUCKET_LOCK(&sc->sc_buckets[c]);
2948 		if (sc->sc_buckets[c].b_len > PFSYNC_MINPKT &&
2949 		    (sifp->if_mtu < sc->sc_ifp->if_mtu ||
2950 			(sc->sc_sync_if != NULL &&
2951 			    sifp->if_mtu < sc->sc_sync_if->if_mtu) ||
2952 			sifp->if_mtu < MCLBYTES - sizeof(struct ip)))
2953 			pfsync_sendout(1, c);
2954 		PFSYNC_BUCKET_UNLOCK(&sc->sc_buckets[c]);
2955 	}
2956 
2957 	pfsync_multicast_cleanup(sc);
2958 
2959 	if (((sc->sc_sync_peer.ss_family == AF_INET) &&
2960 	    IN_MULTICAST(ntohl(((struct sockaddr_in *)
2961 	        &sc->sc_sync_peer)->sin_addr.s_addr))) ||
2962 	    ((sc->sc_sync_peer.ss_family == AF_INET6) &&
2963 	    IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6*)
2964 	        &sc->sc_sync_peer)->sin6_addr))) {
2965 		error = pfsync_multicast_setup(sc, sifp, imf, im6f);
2966 		if (error) {
2967 			if_rele(sifp);
2968 			PFSYNC_UNLOCK(sc);
2969 #ifdef INET
2970 			if (imf != NULL)
2971 				ip_mfilter_free(imf);
2972 #endif
2973 #ifdef INET6
2974 			if (im6f != NULL)
2975 				ip6_mfilter_free(im6f);
2976 #endif
2977 			return (error);
2978 		}
2979 	}
2980 	if (sc->sc_sync_if)
2981 		if_rele(sc->sc_sync_if);
2982 	sc->sc_sync_if = sifp;
2983 
2984 	switch (sc->sc_sync_peer.ss_family) {
2985 #ifdef INET
2986 	case AF_INET: {
2987 		struct ip *ip;
2988 		ip = &sc->sc_template.ipv4;
2989 		bzero(ip, sizeof(*ip));
2990 		ip->ip_v = IPVERSION;
2991 		ip->ip_hl = sizeof(sc->sc_template.ipv4) >> 2;
2992 		ip->ip_tos = IPTOS_LOWDELAY;
2993 		/* len and id are set later. */
2994 		ip->ip_off = htons(IP_DF);
2995 		ip->ip_ttl = PFSYNC_DFLTTL;
2996 		ip->ip_p = IPPROTO_PFSYNC;
2997 		ip->ip_src.s_addr = INADDR_ANY;
2998 		ip->ip_dst = ((struct sockaddr_in *)&sc->sc_sync_peer)->sin_addr;
2999 		break;
3000 	}
3001 #endif
3002 #ifdef INET6
3003 	case AF_INET6: {
3004 		struct ip6_hdr *ip6;
3005 		ip6 = &sc->sc_template.ipv6;
3006 		bzero(ip6, sizeof(*ip6));
3007 		ip6->ip6_vfc = IPV6_VERSION;
3008 		ip6->ip6_hlim = PFSYNC_DFLTTL;
3009 		ip6->ip6_nxt = IPPROTO_PFSYNC;
3010 		ip6->ip6_dst = ((struct sockaddr_in6 *)&sc->sc_sync_peer)->sin6_addr;
3011 
3012 		struct epoch_tracker et;
3013 		NET_EPOCH_ENTER(et);
3014 		in6_selectsrc_addr(if_getfib(sc->sc_sync_if), &ip6->ip6_dst, 0,
3015 		    sc->sc_sync_if, &ip6->ip6_src, NULL);
3016 		NET_EPOCH_EXIT(et);
3017 		break;
3018 	}
3019 #endif
3020 	}
3021 
3022 	/* Request a full state table update. */
3023 	if ((sc->sc_flags & PFSYNCF_OK) && carp_demote_adj_p)
3024 		(*carp_demote_adj_p)(V_pfsync_carp_adj,
3025 		    "pfsync bulk start");
3026 	sc->sc_flags &= ~PFSYNCF_OK;
3027 	if (V_pf_status.debug >= PF_DEBUG_MISC)
3028 		printf("pfsync: requesting bulk update\n");
3029 	PFSYNC_UNLOCK(sc);
3030 	PFSYNC_BUCKET_LOCK(&sc->sc_buckets[0]);
3031 	pfsync_request_update(0, 0);
3032 	PFSYNC_BUCKET_UNLOCK(&sc->sc_buckets[0]);
3033 	PFSYNC_BLOCK(sc);
3034 	sc->sc_ureq_sent = time_uptime;
3035 	callout_reset(&sc->sc_bulkfail_tmo, 5 * hz, pfsync_bulk_fail, sc);
3036 	PFSYNC_BUNLOCK(sc);
3037 	return (0);
3038 }
3039 
3040 static void
3041 pfsync_pointers_init(void)
3042 {
3043 
3044 	PF_RULES_WLOCK();
3045 	V_pfsync_state_import_ptr = pfsync_state_import;
3046 	V_pfsync_insert_state_ptr = pfsync_insert_state;
3047 	V_pfsync_update_state_ptr = pfsync_update_state;
3048 	V_pfsync_delete_state_ptr = pfsync_delete_state;
3049 	V_pfsync_clear_states_ptr = pfsync_clear_states;
3050 	V_pfsync_defer_ptr = pfsync_defer;
3051 	PF_RULES_WUNLOCK();
3052 }
3053 
3054 static void
3055 pfsync_pointers_uninit(void)
3056 {
3057 
3058 	PF_RULES_WLOCK();
3059 	V_pfsync_state_import_ptr = NULL;
3060 	V_pfsync_insert_state_ptr = NULL;
3061 	V_pfsync_update_state_ptr = NULL;
3062 	V_pfsync_delete_state_ptr = NULL;
3063 	V_pfsync_clear_states_ptr = NULL;
3064 	V_pfsync_defer_ptr = NULL;
3065 	PF_RULES_WUNLOCK();
3066 }
3067 
3068 static void
3069 vnet_pfsync_init(const void *unused __unused)
3070 {
3071 	int error;
3072 
3073 	V_pfsync_cloner = if_clone_simple(pfsyncname,
3074 	    pfsync_clone_create, pfsync_clone_destroy, 1);
3075 	error = swi_add(&V_pfsync_swi_ie, pfsyncname, pfsyncintr, V_pfsyncif,
3076 	    SWI_NET, INTR_MPSAFE, &V_pfsync_swi_cookie);
3077 	if (error) {
3078 		if_clone_detach(V_pfsync_cloner);
3079 		log(LOG_INFO, "swi_add() failed in %s\n", __func__);
3080 	}
3081 
3082 	pfsync_pointers_init();
3083 }
3084 VNET_SYSINIT(vnet_pfsync_init, SI_SUB_PROTO_FIREWALL, SI_ORDER_ANY,
3085     vnet_pfsync_init, NULL);
3086 
3087 static void
3088 vnet_pfsync_uninit(const void *unused __unused)
3089 {
3090 	int ret __diagused;
3091 
3092 	pfsync_pointers_uninit();
3093 
3094 	if_clone_detach(V_pfsync_cloner);
3095 	ret = swi_remove(V_pfsync_swi_cookie);
3096 	MPASS(ret == 0);
3097 	ret = intr_event_destroy(V_pfsync_swi_ie);
3098 	MPASS(ret == 0);
3099 }
3100 
3101 VNET_SYSUNINIT(vnet_pfsync_uninit, SI_SUB_PROTO_FIREWALL, SI_ORDER_FOURTH,
3102     vnet_pfsync_uninit, NULL);
3103 
3104 static int
3105 pfsync_init(void)
3106 {
3107 	int error;
3108 
3109 	pfsync_detach_ifnet_ptr = pfsync_detach_ifnet;
3110 
3111 #ifdef INET
3112 	error = ipproto_register(IPPROTO_PFSYNC, pfsync_input, NULL);
3113 	if (error)
3114 		return (error);
3115 #endif
3116 #ifdef INET6
3117 	error = ip6proto_register(IPPROTO_PFSYNC, pfsync6_input, NULL);
3118 	if (error) {
3119 		ipproto_unregister(IPPROTO_PFSYNC);
3120 		return (error);
3121 	}
3122 #endif
3123 
3124 	return (0);
3125 }
3126 
3127 static void
3128 pfsync_uninit(void)
3129 {
3130 	pfsync_detach_ifnet_ptr = NULL;
3131 
3132 #ifdef INET
3133 	ipproto_unregister(IPPROTO_PFSYNC);
3134 #endif
3135 #ifdef INET6
3136 	ip6proto_unregister(IPPROTO_PFSYNC);
3137 #endif
3138 }
3139 
3140 static int
3141 pfsync_modevent(module_t mod, int type, void *data)
3142 {
3143 	int error = 0;
3144 
3145 	switch (type) {
3146 	case MOD_LOAD:
3147 		error = pfsync_init();
3148 		break;
3149 	case MOD_UNLOAD:
3150 		pfsync_uninit();
3151 		break;
3152 	default:
3153 		error = EINVAL;
3154 		break;
3155 	}
3156 
3157 	return (error);
3158 }
3159 
3160 static moduledata_t pfsync_mod = {
3161 	pfsyncname,
3162 	pfsync_modevent,
3163 	0
3164 };
3165 
3166 #define PFSYNC_MODVER 1
3167 
3168 /* Stay on FIREWALL as we depend on pf being initialized and on inetdomain. */
3169 DECLARE_MODULE(pfsync, pfsync_mod, SI_SUB_PROTO_FIREWALL, SI_ORDER_ANY);
3170 MODULE_VERSION(pfsync, PFSYNC_MODVER);
3171 MODULE_DEPEND(pfsync, pf, PF_MODVER, PF_MODVER, PF_MODVER);
3172