xref: /freebsd/sys/netpfil/pf/if_pfsync.c (revision 6dcefcac2b043be030851e03a721607b414b666b)
1 /*-
2  * Copyright (c) 2002 Michael Shalayeff
3  * Copyright (c) 2012 Gleb Smirnoff <glebius@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
25  * THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 /*-
29  * Copyright (c) 2009 David Gwynne <dlg@openbsd.org>
30  *
31  * Permission to use, copy, modify, and distribute this software for any
32  * purpose with or without fee is hereby granted, provided that the above
33  * copyright notice and this permission notice appear in all copies.
34  *
35  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
36  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
37  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
38  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
39  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
40  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
41  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
42  */
43 
44 /*
45  * $OpenBSD: if_pfsync.c,v 1.110 2009/02/24 05:39:19 dlg Exp $
46  *
47  * Revisions picked from OpenBSD after revision 1.110 import:
48  * 1.119 - don't m_copydata() beyond the len of mbuf in pfsync_input()
49  * 1.118, 1.124, 1.148, 1.149, 1.151, 1.171 - fixes to bulk updates
50  * 1.120, 1.175 - use monotonic time_uptime
51  * 1.122 - reduce number of updates for non-TCP sessions
52  * 1.125, 1.127 - rewrite merge or stale processing
53  * 1.128 - cleanups
54  * 1.146 - bzero() mbuf before sparsely filling it with data
55  * 1.170 - SIOCSIFMTU checks
56  * 1.126, 1.142 - deferred packets processing
57  * 1.173 - correct expire time processing
58  */
59 
60 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD$");
62 
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/priv.h>
77 #include <sys/protosw.h>
78 #include <sys/socket.h>
79 #include <sys/sockio.h>
80 #include <sys/sysctl.h>
81 
82 #include <net/bpf.h>
83 #include <net/if.h>
84 #include <net/if_clone.h>
85 #include <net/if_types.h>
86 #include <net/pfvar.h>
87 #include <net/if_pfsync.h>
88 
89 #include <netinet/if_ether.h>
90 #include <netinet/in.h>
91 #include <netinet/in_var.h>
92 #include <netinet/ip.h>
93 #include <netinet/ip_carp.h>
94 #include <netinet/ip_var.h>
95 #include <netinet/tcp.h>
96 #include <netinet/tcp_fsm.h>
97 #include <netinet/tcp_seq.h>
98 
99 #define PFSYNC_MINPKT ( \
100 	sizeof(struct ip) + \
101 	sizeof(struct pfsync_header) + \
102 	sizeof(struct pfsync_subheader) )
103 
104 struct pfsync_pkt {
105 	struct ip *ip;
106 	struct in_addr src;
107 	u_int8_t flags;
108 };
109 
110 static int	pfsync_upd_tcp(struct pf_state *, struct pfsync_state_peer *,
111 		    struct pfsync_state_peer *);
112 static int	pfsync_in_clr(struct pfsync_pkt *, struct mbuf *, int, int);
113 static int	pfsync_in_ins(struct pfsync_pkt *, struct mbuf *, int, int);
114 static int	pfsync_in_iack(struct pfsync_pkt *, struct mbuf *, int, int);
115 static int	pfsync_in_upd(struct pfsync_pkt *, struct mbuf *, int, int);
116 static int	pfsync_in_upd_c(struct pfsync_pkt *, struct mbuf *, int, int);
117 static int	pfsync_in_ureq(struct pfsync_pkt *, struct mbuf *, int, int);
118 static int	pfsync_in_del(struct pfsync_pkt *, struct mbuf *, int, int);
119 static int	pfsync_in_del_c(struct pfsync_pkt *, struct mbuf *, int, int);
120 static int	pfsync_in_bus(struct pfsync_pkt *, struct mbuf *, int, int);
121 static int	pfsync_in_tdb(struct pfsync_pkt *, struct mbuf *, int, int);
122 static int	pfsync_in_eof(struct pfsync_pkt *, struct mbuf *, int, int);
123 static int	pfsync_in_error(struct pfsync_pkt *, struct mbuf *, int, int);
124 
125 static int (*pfsync_acts[])(struct pfsync_pkt *, struct mbuf *, int, int) = {
126 	pfsync_in_clr,			/* PFSYNC_ACT_CLR */
127 	pfsync_in_ins,			/* PFSYNC_ACT_INS */
128 	pfsync_in_iack,			/* PFSYNC_ACT_INS_ACK */
129 	pfsync_in_upd,			/* PFSYNC_ACT_UPD */
130 	pfsync_in_upd_c,		/* PFSYNC_ACT_UPD_C */
131 	pfsync_in_ureq,			/* PFSYNC_ACT_UPD_REQ */
132 	pfsync_in_del,			/* PFSYNC_ACT_DEL */
133 	pfsync_in_del_c,		/* PFSYNC_ACT_DEL_C */
134 	pfsync_in_error,		/* PFSYNC_ACT_INS_F */
135 	pfsync_in_error,		/* PFSYNC_ACT_DEL_F */
136 	pfsync_in_bus,			/* PFSYNC_ACT_BUS */
137 	pfsync_in_tdb,			/* PFSYNC_ACT_TDB */
138 	pfsync_in_eof			/* PFSYNC_ACT_EOF */
139 };
140 
141 struct pfsync_q {
142 	void		(*write)(struct pf_state *, void *);
143 	size_t		len;
144 	u_int8_t	action;
145 };
146 
147 /* we have one of these for every PFSYNC_S_ */
148 static void	pfsync_out_state(struct pf_state *, void *);
149 static void	pfsync_out_iack(struct pf_state *, void *);
150 static void	pfsync_out_upd_c(struct pf_state *, void *);
151 static void	pfsync_out_del(struct pf_state *, void *);
152 
153 static struct pfsync_q pfsync_qs[] = {
154 	{ pfsync_out_state, sizeof(struct pfsync_state),   PFSYNC_ACT_INS },
155 	{ pfsync_out_iack,  sizeof(struct pfsync_ins_ack), PFSYNC_ACT_INS_ACK },
156 	{ pfsync_out_state, sizeof(struct pfsync_state),   PFSYNC_ACT_UPD },
157 	{ pfsync_out_upd_c, sizeof(struct pfsync_upd_c),   PFSYNC_ACT_UPD_C },
158 	{ pfsync_out_del,   sizeof(struct pfsync_del_c),   PFSYNC_ACT_DEL_C }
159 };
160 
161 static void	pfsync_q_ins(struct pf_state *, int);
162 static void	pfsync_q_del(struct pf_state *);
163 
164 static void	pfsync_update_state(struct pf_state *);
165 
166 struct pfsync_upd_req_item {
167 	TAILQ_ENTRY(pfsync_upd_req_item)	ur_entry;
168 	struct pfsync_upd_req			ur_msg;
169 };
170 
171 struct pfsync_deferral {
172 	struct pfsync_softc		*pd_sc;
173 	TAILQ_ENTRY(pfsync_deferral)	pd_entry;
174 	u_int				pd_refs;
175 	struct callout			pd_tmo;
176 
177 	struct pf_state			*pd_st;
178 	struct mbuf			*pd_m;
179 };
180 
181 struct pfsync_softc {
182 	/* Configuration */
183 	struct ifnet		*sc_ifp;
184 	struct ifnet		*sc_sync_if;
185 	struct ip_moptions	sc_imo;
186 	struct in_addr		sc_sync_peer;
187 	uint32_t		sc_flags;
188 #define	PFSYNCF_OK		0x00000001
189 #define	PFSYNCF_DEFER		0x00000002
190 #define	PFSYNCF_PUSH		0x00000004
191 	uint8_t			sc_maxupdates;
192 	struct ip		sc_template;
193 	struct callout		sc_tmo;
194 	struct mtx		sc_mtx;
195 
196 	/* Queued data */
197 	size_t			sc_len;
198 	TAILQ_HEAD(, pf_state)			sc_qs[PFSYNC_S_COUNT];
199 	TAILQ_HEAD(, pfsync_upd_req_item)	sc_upd_req_list;
200 	TAILQ_HEAD(, pfsync_deferral)		sc_deferrals;
201 	u_int			sc_deferred;
202 	void			*sc_plus;
203 	size_t			sc_pluslen;
204 
205 	/* Bulk update info */
206 	struct mtx		sc_bulk_mtx;
207 	uint32_t		sc_ureq_sent;
208 	int			sc_bulk_tries;
209 	uint32_t		sc_ureq_received;
210 	int			sc_bulk_hashid;
211 	uint64_t		sc_bulk_stateid;
212 	uint32_t		sc_bulk_creatorid;
213 	struct callout		sc_bulk_tmo;
214 	struct callout		sc_bulkfail_tmo;
215 };
216 
217 #define	PFSYNC_LOCK(sc)		mtx_lock(&(sc)->sc_mtx)
218 #define	PFSYNC_UNLOCK(sc)	mtx_unlock(&(sc)->sc_mtx)
219 #define	PFSYNC_LOCK_ASSERT(sc)	mtx_assert(&(sc)->sc_mtx, MA_OWNED)
220 
221 #define	PFSYNC_BLOCK(sc)	mtx_lock(&(sc)->sc_bulk_mtx)
222 #define	PFSYNC_BUNLOCK(sc)	mtx_unlock(&(sc)->sc_bulk_mtx)
223 #define	PFSYNC_BLOCK_ASSERT(sc)	mtx_assert(&(sc)->sc_bulk_mtx, MA_OWNED)
224 
225 static const char pfsyncname[] = "pfsync";
226 static MALLOC_DEFINE(M_PFSYNC, pfsyncname, "pfsync(4) data");
227 static VNET_DEFINE(struct pfsync_softc	*, pfsyncif) = NULL;
228 #define	V_pfsyncif		VNET(pfsyncif)
229 static VNET_DEFINE(void *, pfsync_swi_cookie) = NULL;
230 #define	V_pfsync_swi_cookie	VNET(pfsync_swi_cookie)
231 static VNET_DEFINE(struct pfsyncstats, pfsyncstats);
232 #define	V_pfsyncstats		VNET(pfsyncstats)
233 static VNET_DEFINE(int, pfsync_carp_adj) = CARP_MAXSKEW;
234 #define	V_pfsync_carp_adj	VNET(pfsync_carp_adj)
235 
236 static void	pfsync_timeout(void *);
237 static void	pfsync_push(struct pfsync_softc *);
238 static void	pfsyncintr(void *);
239 static int	pfsync_multicast_setup(struct pfsync_softc *, struct ifnet *,
240 		    void *);
241 static void	pfsync_multicast_cleanup(struct pfsync_softc *);
242 static void	pfsync_pointers_init(void);
243 static void	pfsync_pointers_uninit(void);
244 static int	pfsync_init(void);
245 static void	pfsync_uninit(void);
246 
247 SYSCTL_NODE(_net, OID_AUTO, pfsync, CTLFLAG_RW, 0, "PFSYNC");
248 SYSCTL_VNET_STRUCT(_net_pfsync, OID_AUTO, stats, CTLFLAG_RW,
249     &VNET_NAME(pfsyncstats), pfsyncstats,
250     "PFSYNC statistics (struct pfsyncstats, net/if_pfsync.h)");
251 SYSCTL_INT(_net_pfsync, OID_AUTO, carp_demotion_factor, CTLFLAG_RW,
252     &VNET_NAME(pfsync_carp_adj), 0, "pfsync's CARP demotion factor adjustment");
253 
254 static int	pfsync_clone_create(struct if_clone *, int, caddr_t);
255 static void	pfsync_clone_destroy(struct ifnet *);
256 static int	pfsync_alloc_scrub_memory(struct pfsync_state_peer *,
257 		    struct pf_state_peer *);
258 static int	pfsyncoutput(struct ifnet *, struct mbuf *,
259 		    const struct sockaddr *, struct route *);
260 static int	pfsyncioctl(struct ifnet *, u_long, caddr_t);
261 
262 static int	pfsync_defer(struct pf_state *, struct mbuf *);
263 static void	pfsync_undefer(struct pfsync_deferral *, int);
264 static void	pfsync_undefer_state(struct pf_state *, int);
265 static void	pfsync_defer_tmo(void *);
266 
267 static void	pfsync_request_update(u_int32_t, u_int64_t);
268 static void	pfsync_update_state_req(struct pf_state *);
269 
270 static void	pfsync_drop(struct pfsync_softc *);
271 static void	pfsync_sendout(int);
272 static void	pfsync_send_plus(void *, size_t);
273 
274 static void	pfsync_bulk_start(void);
275 static void	pfsync_bulk_status(u_int8_t);
276 static void	pfsync_bulk_update(void *);
277 static void	pfsync_bulk_fail(void *);
278 
279 #ifdef IPSEC
280 static void	pfsync_update_net_tdb(struct pfsync_tdb *);
281 #endif
282 
283 #define PFSYNC_MAX_BULKTRIES	12
284 
285 VNET_DEFINE(struct if_clone *, pfsync_cloner);
286 #define	V_pfsync_cloner	VNET(pfsync_cloner)
287 
288 static int
289 pfsync_clone_create(struct if_clone *ifc, int unit, caddr_t param)
290 {
291 	struct pfsync_softc *sc;
292 	struct ifnet *ifp;
293 	int q;
294 
295 	if (unit != 0)
296 		return (EINVAL);
297 
298 	sc = malloc(sizeof(struct pfsync_softc), M_PFSYNC, M_WAITOK | M_ZERO);
299 	sc->sc_flags |= PFSYNCF_OK;
300 
301 	for (q = 0; q < PFSYNC_S_COUNT; q++)
302 		TAILQ_INIT(&sc->sc_qs[q]);
303 
304 	TAILQ_INIT(&sc->sc_upd_req_list);
305 	TAILQ_INIT(&sc->sc_deferrals);
306 
307 	sc->sc_len = PFSYNC_MINPKT;
308 	sc->sc_maxupdates = 128;
309 
310 	ifp = sc->sc_ifp = if_alloc(IFT_PFSYNC);
311 	if (ifp == NULL) {
312 		free(sc, M_PFSYNC);
313 		return (ENOSPC);
314 	}
315 	if_initname(ifp, pfsyncname, unit);
316 	ifp->if_softc = sc;
317 	ifp->if_ioctl = pfsyncioctl;
318 	ifp->if_output = pfsyncoutput;
319 	ifp->if_type = IFT_PFSYNC;
320 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
321 	ifp->if_hdrlen = sizeof(struct pfsync_header);
322 	ifp->if_mtu = ETHERMTU;
323 	mtx_init(&sc->sc_mtx, pfsyncname, NULL, MTX_DEF);
324 	mtx_init(&sc->sc_bulk_mtx, "pfsync bulk", NULL, MTX_DEF);
325 	callout_init(&sc->sc_tmo, CALLOUT_MPSAFE);
326 	callout_init_mtx(&sc->sc_bulk_tmo, &sc->sc_bulk_mtx, 0);
327 	callout_init_mtx(&sc->sc_bulkfail_tmo, &sc->sc_bulk_mtx, 0);
328 
329 	if_attach(ifp);
330 
331 	bpfattach(ifp, DLT_PFSYNC, PFSYNC_HDRLEN);
332 
333 	V_pfsyncif = sc;
334 
335 	return (0);
336 }
337 
338 static void
339 pfsync_clone_destroy(struct ifnet *ifp)
340 {
341 	struct pfsync_softc *sc = ifp->if_softc;
342 
343 	/*
344 	 * At this stage, everything should have already been
345 	 * cleared by pfsync_uninit(), and we have only to
346 	 * drain callouts.
347 	 */
348 	while (sc->sc_deferred > 0) {
349 		struct pfsync_deferral *pd = TAILQ_FIRST(&sc->sc_deferrals);
350 
351 		TAILQ_REMOVE(&sc->sc_deferrals, pd, pd_entry);
352 		sc->sc_deferred--;
353 		if (callout_stop(&pd->pd_tmo)) {
354 			pf_release_state(pd->pd_st);
355 			m_freem(pd->pd_m);
356 			free(pd, M_PFSYNC);
357 		} else {
358 			pd->pd_refs++;
359 			callout_drain(&pd->pd_tmo);
360 			free(pd, M_PFSYNC);
361 		}
362 	}
363 
364 	callout_drain(&sc->sc_tmo);
365 	callout_drain(&sc->sc_bulkfail_tmo);
366 	callout_drain(&sc->sc_bulk_tmo);
367 
368 	if (!(sc->sc_flags & PFSYNCF_OK) && carp_demote_adj_p)
369 		(*carp_demote_adj_p)(-V_pfsync_carp_adj, "pfsync destroy");
370 	bpfdetach(ifp);
371 	if_detach(ifp);
372 
373 	pfsync_drop(sc);
374 
375 	if_free(ifp);
376 	if (sc->sc_imo.imo_membership)
377 		pfsync_multicast_cleanup(sc);
378 	mtx_destroy(&sc->sc_mtx);
379 	mtx_destroy(&sc->sc_bulk_mtx);
380 	free(sc, M_PFSYNC);
381 
382 	V_pfsyncif = NULL;
383 }
384 
385 static int
386 pfsync_alloc_scrub_memory(struct pfsync_state_peer *s,
387     struct pf_state_peer *d)
388 {
389 	if (s->scrub.scrub_flag && d->scrub == NULL) {
390 		d->scrub = uma_zalloc(V_pf_state_scrub_z, M_NOWAIT | M_ZERO);
391 		if (d->scrub == NULL)
392 			return (ENOMEM);
393 	}
394 
395 	return (0);
396 }
397 
398 
399 static int
400 pfsync_state_import(struct pfsync_state *sp, u_int8_t flags)
401 {
402 	struct pfsync_softc *sc = V_pfsyncif;
403 	struct pf_state	*st = NULL;
404 	struct pf_state_key *skw = NULL, *sks = NULL;
405 	struct pf_rule *r = NULL;
406 	struct pfi_kif	*kif;
407 	int error;
408 
409 	PF_RULES_RASSERT();
410 
411 	if (sp->creatorid == 0) {
412 		if (V_pf_status.debug >= PF_DEBUG_MISC)
413 			printf("%s: invalid creator id: %08x\n", __func__,
414 			    ntohl(sp->creatorid));
415 		return (EINVAL);
416 	}
417 
418 	if ((kif = pfi_kif_find(sp->ifname)) == NULL) {
419 		if (V_pf_status.debug >= PF_DEBUG_MISC)
420 			printf("%s: unknown interface: %s\n", __func__,
421 			    sp->ifname);
422 		if (flags & PFSYNC_SI_IOCTL)
423 			return (EINVAL);
424 		return (0);	/* skip this state */
425 	}
426 
427 	/*
428 	 * If the ruleset checksums match or the state is coming from the ioctl,
429 	 * it's safe to associate the state with the rule of that number.
430 	 */
431 	if (sp->rule != htonl(-1) && sp->anchor == htonl(-1) &&
432 	    (flags & (PFSYNC_SI_IOCTL | PFSYNC_SI_CKSUM)) && ntohl(sp->rule) <
433 	    pf_main_ruleset.rules[PF_RULESET_FILTER].active.rcount)
434 		r = pf_main_ruleset.rules[
435 		    PF_RULESET_FILTER].active.ptr_array[ntohl(sp->rule)];
436 	else
437 		r = &V_pf_default_rule;
438 
439 	if ((r->max_states && r->states_cur >= r->max_states))
440 		goto cleanup;
441 
442 	/*
443 	 * XXXGL: consider M_WAITOK in ioctl path after.
444 	 */
445 	if ((st = uma_zalloc(V_pf_state_z, M_NOWAIT | M_ZERO)) == NULL)
446 		goto cleanup;
447 
448 	if ((skw = uma_zalloc(V_pf_state_key_z, M_NOWAIT)) == NULL)
449 		goto cleanup;
450 
451 	if (PF_ANEQ(&sp->key[PF_SK_WIRE].addr[0],
452 	    &sp->key[PF_SK_STACK].addr[0], sp->af) ||
453 	    PF_ANEQ(&sp->key[PF_SK_WIRE].addr[1],
454 	    &sp->key[PF_SK_STACK].addr[1], sp->af) ||
455 	    sp->key[PF_SK_WIRE].port[0] != sp->key[PF_SK_STACK].port[0] ||
456 	    sp->key[PF_SK_WIRE].port[1] != sp->key[PF_SK_STACK].port[1]) {
457 		sks = uma_zalloc(V_pf_state_key_z, M_NOWAIT);
458 		if (sks == NULL)
459 			goto cleanup;
460 	} else
461 		sks = skw;
462 
463 	/* allocate memory for scrub info */
464 	if (pfsync_alloc_scrub_memory(&sp->src, &st->src) ||
465 	    pfsync_alloc_scrub_memory(&sp->dst, &st->dst))
466 		goto cleanup;
467 
468 	/* copy to state key(s) */
469 	skw->addr[0] = sp->key[PF_SK_WIRE].addr[0];
470 	skw->addr[1] = sp->key[PF_SK_WIRE].addr[1];
471 	skw->port[0] = sp->key[PF_SK_WIRE].port[0];
472 	skw->port[1] = sp->key[PF_SK_WIRE].port[1];
473 	skw->proto = sp->proto;
474 	skw->af = sp->af;
475 	if (sks != skw) {
476 		sks->addr[0] = sp->key[PF_SK_STACK].addr[0];
477 		sks->addr[1] = sp->key[PF_SK_STACK].addr[1];
478 		sks->port[0] = sp->key[PF_SK_STACK].port[0];
479 		sks->port[1] = sp->key[PF_SK_STACK].port[1];
480 		sks->proto = sp->proto;
481 		sks->af = sp->af;
482 	}
483 
484 	/* copy to state */
485 	bcopy(&sp->rt_addr, &st->rt_addr, sizeof(st->rt_addr));
486 	st->creation = time_uptime - ntohl(sp->creation);
487 	st->expire = time_uptime;
488 	if (sp->expire) {
489 		uint32_t timeout;
490 
491 		timeout = r->timeout[sp->timeout];
492 		if (!timeout)
493 			timeout = V_pf_default_rule.timeout[sp->timeout];
494 
495 		/* sp->expire may have been adaptively scaled by export. */
496 		st->expire -= timeout - ntohl(sp->expire);
497 	}
498 
499 	st->direction = sp->direction;
500 	st->log = sp->log;
501 	st->timeout = sp->timeout;
502 	st->state_flags = sp->state_flags;
503 
504 	st->id = sp->id;
505 	st->creatorid = sp->creatorid;
506 	pf_state_peer_ntoh(&sp->src, &st->src);
507 	pf_state_peer_ntoh(&sp->dst, &st->dst);
508 
509 	st->rule.ptr = r;
510 	st->nat_rule.ptr = NULL;
511 	st->anchor.ptr = NULL;
512 	st->rt_kif = NULL;
513 
514 	st->pfsync_time = time_uptime;
515 	st->sync_state = PFSYNC_S_NONE;
516 
517 	/* XXX when we have nat_rule/anchors, use STATE_INC_COUNTERS */
518 	r->states_cur++;
519 	r->states_tot++;
520 
521 	if (!(flags & PFSYNC_SI_IOCTL))
522 		st->state_flags |= PFSTATE_NOSYNC;
523 
524 	if ((error = pf_state_insert(kif, skw, sks, st)) != 0) {
525 		/* XXX when we have nat_rule/anchors, use STATE_DEC_COUNTERS */
526 		r->states_cur--;
527 		goto cleanup_state;
528 	}
529 
530 	if (!(flags & PFSYNC_SI_IOCTL)) {
531 		st->state_flags &= ~PFSTATE_NOSYNC;
532 		if (st->state_flags & PFSTATE_ACK) {
533 			pfsync_q_ins(st, PFSYNC_S_IACK);
534 			pfsync_push(sc);
535 		}
536 	}
537 	st->state_flags &= ~PFSTATE_ACK;
538 	PF_STATE_UNLOCK(st);
539 
540 	return (0);
541 
542 cleanup:
543 	error = ENOMEM;
544 	if (skw == sks)
545 		sks = NULL;
546 	if (skw != NULL)
547 		uma_zfree(V_pf_state_key_z, skw);
548 	if (sks != NULL)
549 		uma_zfree(V_pf_state_key_z, sks);
550 
551 cleanup_state:	/* pf_state_insert() frees the state keys. */
552 	if (st) {
553 		if (st->dst.scrub)
554 			uma_zfree(V_pf_state_scrub_z, st->dst.scrub);
555 		if (st->src.scrub)
556 			uma_zfree(V_pf_state_scrub_z, st->src.scrub);
557 		uma_zfree(V_pf_state_z, st);
558 	}
559 	return (error);
560 }
561 
562 static void
563 pfsync_input(struct mbuf *m, __unused int off)
564 {
565 	struct pfsync_softc *sc = V_pfsyncif;
566 	struct pfsync_pkt pkt;
567 	struct ip *ip = mtod(m, struct ip *);
568 	struct pfsync_header *ph;
569 	struct pfsync_subheader subh;
570 
571 	int offset, len;
572 	int rv;
573 	uint16_t count;
574 
575 	V_pfsyncstats.pfsyncs_ipackets++;
576 
577 	/* Verify that we have a sync interface configured. */
578 	if (!sc || !sc->sc_sync_if || !V_pf_status.running ||
579 	    (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
580 		goto done;
581 
582 	/* verify that the packet came in on the right interface */
583 	if (sc->sc_sync_if != m->m_pkthdr.rcvif) {
584 		V_pfsyncstats.pfsyncs_badif++;
585 		goto done;
586 	}
587 
588 	sc->sc_ifp->if_ipackets++;
589 	sc->sc_ifp->if_ibytes += m->m_pkthdr.len;
590 	/* verify that the IP TTL is 255. */
591 	if (ip->ip_ttl != PFSYNC_DFLTTL) {
592 		V_pfsyncstats.pfsyncs_badttl++;
593 		goto done;
594 	}
595 
596 	offset = ip->ip_hl << 2;
597 	if (m->m_pkthdr.len < offset + sizeof(*ph)) {
598 		V_pfsyncstats.pfsyncs_hdrops++;
599 		goto done;
600 	}
601 
602 	if (offset + sizeof(*ph) > m->m_len) {
603 		if (m_pullup(m, offset + sizeof(*ph)) == NULL) {
604 			V_pfsyncstats.pfsyncs_hdrops++;
605 			return;
606 		}
607 		ip = mtod(m, struct ip *);
608 	}
609 	ph = (struct pfsync_header *)((char *)ip + offset);
610 
611 	/* verify the version */
612 	if (ph->version != PFSYNC_VERSION) {
613 		V_pfsyncstats.pfsyncs_badver++;
614 		goto done;
615 	}
616 
617 	len = ntohs(ph->len) + offset;
618 	if (m->m_pkthdr.len < len) {
619 		V_pfsyncstats.pfsyncs_badlen++;
620 		goto done;
621 	}
622 
623 	/* Cheaper to grab this now than having to mess with mbufs later */
624 	pkt.ip = ip;
625 	pkt.src = ip->ip_src;
626 	pkt.flags = 0;
627 
628 	/*
629 	 * Trusting pf_chksum during packet processing, as well as seeking
630 	 * in interface name tree, require holding PF_RULES_RLOCK().
631 	 */
632 	PF_RULES_RLOCK();
633 	if (!bcmp(&ph->pfcksum, &V_pf_status.pf_chksum, PF_MD5_DIGEST_LENGTH))
634 		pkt.flags |= PFSYNC_SI_CKSUM;
635 
636 	offset += sizeof(*ph);
637 	while (offset <= len - sizeof(subh)) {
638 		m_copydata(m, offset, sizeof(subh), (caddr_t)&subh);
639 		offset += sizeof(subh);
640 
641 		if (subh.action >= PFSYNC_ACT_MAX) {
642 			V_pfsyncstats.pfsyncs_badact++;
643 			PF_RULES_RUNLOCK();
644 			goto done;
645 		}
646 
647 		count = ntohs(subh.count);
648 		V_pfsyncstats.pfsyncs_iacts[subh.action] += count;
649 		rv = (*pfsync_acts[subh.action])(&pkt, m, offset, count);
650 		if (rv == -1) {
651 			PF_RULES_RUNLOCK();
652 			return;
653 		}
654 
655 		offset += rv;
656 	}
657 	PF_RULES_RUNLOCK();
658 
659 done:
660 	m_freem(m);
661 }
662 
663 static int
664 pfsync_in_clr(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
665 {
666 	struct pfsync_clr *clr;
667 	struct mbuf *mp;
668 	int len = sizeof(*clr) * count;
669 	int i, offp;
670 	u_int32_t creatorid;
671 
672 	mp = m_pulldown(m, offset, len, &offp);
673 	if (mp == NULL) {
674 		V_pfsyncstats.pfsyncs_badlen++;
675 		return (-1);
676 	}
677 	clr = (struct pfsync_clr *)(mp->m_data + offp);
678 
679 	for (i = 0; i < count; i++) {
680 		creatorid = clr[i].creatorid;
681 
682 		if (clr[i].ifname[0] != '\0' &&
683 		    pfi_kif_find(clr[i].ifname) == NULL)
684 			continue;
685 
686 		for (int i = 0; i <= V_pf_hashmask; i++) {
687 			struct pf_idhash *ih = &V_pf_idhash[i];
688 			struct pf_state *s;
689 relock:
690 			PF_HASHROW_LOCK(ih);
691 			LIST_FOREACH(s, &ih->states, entry) {
692 				if (s->creatorid == creatorid) {
693 					s->state_flags |= PFSTATE_NOSYNC;
694 					pf_unlink_state(s, PF_ENTER_LOCKED);
695 					goto relock;
696 				}
697 			}
698 			PF_HASHROW_UNLOCK(ih);
699 		}
700 	}
701 
702 	return (len);
703 }
704 
705 static int
706 pfsync_in_ins(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
707 {
708 	struct mbuf *mp;
709 	struct pfsync_state *sa, *sp;
710 	int len = sizeof(*sp) * count;
711 	int i, offp;
712 
713 	mp = m_pulldown(m, offset, len, &offp);
714 	if (mp == NULL) {
715 		V_pfsyncstats.pfsyncs_badlen++;
716 		return (-1);
717 	}
718 	sa = (struct pfsync_state *)(mp->m_data + offp);
719 
720 	for (i = 0; i < count; i++) {
721 		sp = &sa[i];
722 
723 		/* Check for invalid values. */
724 		if (sp->timeout >= PFTM_MAX ||
725 		    sp->src.state > PF_TCPS_PROXY_DST ||
726 		    sp->dst.state > PF_TCPS_PROXY_DST ||
727 		    sp->direction > PF_OUT ||
728 		    (sp->af != AF_INET && sp->af != AF_INET6)) {
729 			if (V_pf_status.debug >= PF_DEBUG_MISC)
730 				printf("%s: invalid value\n", __func__);
731 			V_pfsyncstats.pfsyncs_badval++;
732 			continue;
733 		}
734 
735 		if (pfsync_state_import(sp, pkt->flags) == ENOMEM)
736 			/* Drop out, but process the rest of the actions. */
737 			break;
738 	}
739 
740 	return (len);
741 }
742 
743 static int
744 pfsync_in_iack(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
745 {
746 	struct pfsync_ins_ack *ia, *iaa;
747 	struct pf_state *st;
748 
749 	struct mbuf *mp;
750 	int len = count * sizeof(*ia);
751 	int offp, i;
752 
753 	mp = m_pulldown(m, offset, len, &offp);
754 	if (mp == NULL) {
755 		V_pfsyncstats.pfsyncs_badlen++;
756 		return (-1);
757 	}
758 	iaa = (struct pfsync_ins_ack *)(mp->m_data + offp);
759 
760 	for (i = 0; i < count; i++) {
761 		ia = &iaa[i];
762 
763 		st = pf_find_state_byid(ia->id, ia->creatorid);
764 		if (st == NULL)
765 			continue;
766 
767 		if (st->state_flags & PFSTATE_ACK) {
768 			PFSYNC_LOCK(V_pfsyncif);
769 			pfsync_undefer_state(st, 0);
770 			PFSYNC_UNLOCK(V_pfsyncif);
771 		}
772 		PF_STATE_UNLOCK(st);
773 	}
774 	/*
775 	 * XXX this is not yet implemented, but we know the size of the
776 	 * message so we can skip it.
777 	 */
778 
779 	return (count * sizeof(struct pfsync_ins_ack));
780 }
781 
782 static int
783 pfsync_upd_tcp(struct pf_state *st, struct pfsync_state_peer *src,
784     struct pfsync_state_peer *dst)
785 {
786 	int sync = 0;
787 
788 	PF_STATE_LOCK_ASSERT(st);
789 
790 	/*
791 	 * The state should never go backwards except
792 	 * for syn-proxy states.  Neither should the
793 	 * sequence window slide backwards.
794 	 */
795 	if ((st->src.state > src->state &&
796 	    (st->src.state < PF_TCPS_PROXY_SRC ||
797 	    src->state >= PF_TCPS_PROXY_SRC)) ||
798 
799 	    (st->src.state == src->state &&
800 	    SEQ_GT(st->src.seqlo, ntohl(src->seqlo))))
801 		sync++;
802 	else
803 		pf_state_peer_ntoh(src, &st->src);
804 
805 	if ((st->dst.state > dst->state) ||
806 
807 	    (st->dst.state >= TCPS_SYN_SENT &&
808 	    SEQ_GT(st->dst.seqlo, ntohl(dst->seqlo))))
809 		sync++;
810 	else
811 		pf_state_peer_ntoh(dst, &st->dst);
812 
813 	return (sync);
814 }
815 
816 static int
817 pfsync_in_upd(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
818 {
819 	struct pfsync_softc *sc = V_pfsyncif;
820 	struct pfsync_state *sa, *sp;
821 	struct pf_state *st;
822 	int sync;
823 
824 	struct mbuf *mp;
825 	int len = count * sizeof(*sp);
826 	int offp, i;
827 
828 	mp = m_pulldown(m, offset, len, &offp);
829 	if (mp == NULL) {
830 		V_pfsyncstats.pfsyncs_badlen++;
831 		return (-1);
832 	}
833 	sa = (struct pfsync_state *)(mp->m_data + offp);
834 
835 	for (i = 0; i < count; i++) {
836 		sp = &sa[i];
837 
838 		/* check for invalid values */
839 		if (sp->timeout >= PFTM_MAX ||
840 		    sp->src.state > PF_TCPS_PROXY_DST ||
841 		    sp->dst.state > PF_TCPS_PROXY_DST) {
842 			if (V_pf_status.debug >= PF_DEBUG_MISC) {
843 				printf("pfsync_input: PFSYNC_ACT_UPD: "
844 				    "invalid value\n");
845 			}
846 			V_pfsyncstats.pfsyncs_badval++;
847 			continue;
848 		}
849 
850 		st = pf_find_state_byid(sp->id, sp->creatorid);
851 		if (st == NULL) {
852 			/* insert the update */
853 			if (pfsync_state_import(sp, 0))
854 				V_pfsyncstats.pfsyncs_badstate++;
855 			continue;
856 		}
857 
858 		if (st->state_flags & PFSTATE_ACK) {
859 			PFSYNC_LOCK(sc);
860 			pfsync_undefer_state(st, 1);
861 			PFSYNC_UNLOCK(sc);
862 		}
863 
864 		if (st->key[PF_SK_WIRE]->proto == IPPROTO_TCP)
865 			sync = pfsync_upd_tcp(st, &sp->src, &sp->dst);
866 		else {
867 			sync = 0;
868 
869 			/*
870 			 * Non-TCP protocol state machine always go
871 			 * forwards
872 			 */
873 			if (st->src.state > sp->src.state)
874 				sync++;
875 			else
876 				pf_state_peer_ntoh(&sp->src, &st->src);
877 			if (st->dst.state > sp->dst.state)
878 				sync++;
879 			else
880 				pf_state_peer_ntoh(&sp->dst, &st->dst);
881 		}
882 		if (sync < 2) {
883 			pfsync_alloc_scrub_memory(&sp->dst, &st->dst);
884 			pf_state_peer_ntoh(&sp->dst, &st->dst);
885 			st->expire = time_uptime;
886 			st->timeout = sp->timeout;
887 		}
888 		st->pfsync_time = time_uptime;
889 
890 		if (sync) {
891 			V_pfsyncstats.pfsyncs_stale++;
892 
893 			pfsync_update_state(st);
894 			PF_STATE_UNLOCK(st);
895 			PFSYNC_LOCK(sc);
896 			pfsync_push(sc);
897 			PFSYNC_UNLOCK(sc);
898 			continue;
899 		}
900 		PF_STATE_UNLOCK(st);
901 	}
902 
903 	return (len);
904 }
905 
906 static int
907 pfsync_in_upd_c(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
908 {
909 	struct pfsync_softc *sc = V_pfsyncif;
910 	struct pfsync_upd_c *ua, *up;
911 	struct pf_state *st;
912 	int len = count * sizeof(*up);
913 	int sync;
914 	struct mbuf *mp;
915 	int offp, i;
916 
917 	mp = m_pulldown(m, offset, len, &offp);
918 	if (mp == NULL) {
919 		V_pfsyncstats.pfsyncs_badlen++;
920 		return (-1);
921 	}
922 	ua = (struct pfsync_upd_c *)(mp->m_data + offp);
923 
924 	for (i = 0; i < count; i++) {
925 		up = &ua[i];
926 
927 		/* check for invalid values */
928 		if (up->timeout >= PFTM_MAX ||
929 		    up->src.state > PF_TCPS_PROXY_DST ||
930 		    up->dst.state > PF_TCPS_PROXY_DST) {
931 			if (V_pf_status.debug >= PF_DEBUG_MISC) {
932 				printf("pfsync_input: "
933 				    "PFSYNC_ACT_UPD_C: "
934 				    "invalid value\n");
935 			}
936 			V_pfsyncstats.pfsyncs_badval++;
937 			continue;
938 		}
939 
940 		st = pf_find_state_byid(up->id, up->creatorid);
941 		if (st == NULL) {
942 			/* We don't have this state. Ask for it. */
943 			PFSYNC_LOCK(sc);
944 			pfsync_request_update(up->creatorid, up->id);
945 			PFSYNC_UNLOCK(sc);
946 			continue;
947 		}
948 
949 		if (st->state_flags & PFSTATE_ACK) {
950 			PFSYNC_LOCK(sc);
951 			pfsync_undefer_state(st, 1);
952 			PFSYNC_UNLOCK(sc);
953 		}
954 
955 		if (st->key[PF_SK_WIRE]->proto == IPPROTO_TCP)
956 			sync = pfsync_upd_tcp(st, &up->src, &up->dst);
957 		else {
958 			sync = 0;
959 
960 			/*
961 			 * Non-TCP protocol state machine always go
962 			 * forwards
963 			 */
964 			if (st->src.state > up->src.state)
965 				sync++;
966 			else
967 				pf_state_peer_ntoh(&up->src, &st->src);
968 			if (st->dst.state > up->dst.state)
969 				sync++;
970 			else
971 				pf_state_peer_ntoh(&up->dst, &st->dst);
972 		}
973 		if (sync < 2) {
974 			pfsync_alloc_scrub_memory(&up->dst, &st->dst);
975 			pf_state_peer_ntoh(&up->dst, &st->dst);
976 			st->expire = time_uptime;
977 			st->timeout = up->timeout;
978 		}
979 		st->pfsync_time = time_uptime;
980 
981 		if (sync) {
982 			V_pfsyncstats.pfsyncs_stale++;
983 
984 			pfsync_update_state(st);
985 			PF_STATE_UNLOCK(st);
986 			PFSYNC_LOCK(sc);
987 			pfsync_push(sc);
988 			PFSYNC_UNLOCK(sc);
989 			continue;
990 		}
991 		PF_STATE_UNLOCK(st);
992 	}
993 
994 	return (len);
995 }
996 
997 static int
998 pfsync_in_ureq(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
999 {
1000 	struct pfsync_upd_req *ur, *ura;
1001 	struct mbuf *mp;
1002 	int len = count * sizeof(*ur);
1003 	int i, offp;
1004 
1005 	struct pf_state *st;
1006 
1007 	mp = m_pulldown(m, offset, len, &offp);
1008 	if (mp == NULL) {
1009 		V_pfsyncstats.pfsyncs_badlen++;
1010 		return (-1);
1011 	}
1012 	ura = (struct pfsync_upd_req *)(mp->m_data + offp);
1013 
1014 	for (i = 0; i < count; i++) {
1015 		ur = &ura[i];
1016 
1017 		if (ur->id == 0 && ur->creatorid == 0)
1018 			pfsync_bulk_start();
1019 		else {
1020 			st = pf_find_state_byid(ur->id, ur->creatorid);
1021 			if (st == NULL) {
1022 				V_pfsyncstats.pfsyncs_badstate++;
1023 				continue;
1024 			}
1025 			if (st->state_flags & PFSTATE_NOSYNC) {
1026 				PF_STATE_UNLOCK(st);
1027 				continue;
1028 			}
1029 
1030 			pfsync_update_state_req(st);
1031 			PF_STATE_UNLOCK(st);
1032 		}
1033 	}
1034 
1035 	return (len);
1036 }
1037 
1038 static int
1039 pfsync_in_del(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1040 {
1041 	struct mbuf *mp;
1042 	struct pfsync_state *sa, *sp;
1043 	struct pf_state *st;
1044 	int len = count * sizeof(*sp);
1045 	int offp, i;
1046 
1047 	mp = m_pulldown(m, offset, len, &offp);
1048 	if (mp == NULL) {
1049 		V_pfsyncstats.pfsyncs_badlen++;
1050 		return (-1);
1051 	}
1052 	sa = (struct pfsync_state *)(mp->m_data + offp);
1053 
1054 	for (i = 0; i < count; i++) {
1055 		sp = &sa[i];
1056 
1057 		st = pf_find_state_byid(sp->id, sp->creatorid);
1058 		if (st == NULL) {
1059 			V_pfsyncstats.pfsyncs_badstate++;
1060 			continue;
1061 		}
1062 		st->state_flags |= PFSTATE_NOSYNC;
1063 		pf_unlink_state(st, PF_ENTER_LOCKED);
1064 	}
1065 
1066 	return (len);
1067 }
1068 
1069 static int
1070 pfsync_in_del_c(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1071 {
1072 	struct mbuf *mp;
1073 	struct pfsync_del_c *sa, *sp;
1074 	struct pf_state *st;
1075 	int len = count * sizeof(*sp);
1076 	int offp, i;
1077 
1078 	mp = m_pulldown(m, offset, len, &offp);
1079 	if (mp == NULL) {
1080 		V_pfsyncstats.pfsyncs_badlen++;
1081 		return (-1);
1082 	}
1083 	sa = (struct pfsync_del_c *)(mp->m_data + offp);
1084 
1085 	for (i = 0; i < count; i++) {
1086 		sp = &sa[i];
1087 
1088 		st = pf_find_state_byid(sp->id, sp->creatorid);
1089 		if (st == NULL) {
1090 			V_pfsyncstats.pfsyncs_badstate++;
1091 			continue;
1092 		}
1093 
1094 		st->state_flags |= PFSTATE_NOSYNC;
1095 		pf_unlink_state(st, PF_ENTER_LOCKED);
1096 	}
1097 
1098 	return (len);
1099 }
1100 
1101 static int
1102 pfsync_in_bus(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1103 {
1104 	struct pfsync_softc *sc = V_pfsyncif;
1105 	struct pfsync_bus *bus;
1106 	struct mbuf *mp;
1107 	int len = count * sizeof(*bus);
1108 	int offp;
1109 
1110 	PFSYNC_BLOCK(sc);
1111 
1112 	/* If we're not waiting for a bulk update, who cares. */
1113 	if (sc->sc_ureq_sent == 0) {
1114 		PFSYNC_BUNLOCK(sc);
1115 		return (len);
1116 	}
1117 
1118 	mp = m_pulldown(m, offset, len, &offp);
1119 	if (mp == NULL) {
1120 		PFSYNC_BUNLOCK(sc);
1121 		V_pfsyncstats.pfsyncs_badlen++;
1122 		return (-1);
1123 	}
1124 	bus = (struct pfsync_bus *)(mp->m_data + offp);
1125 
1126 	switch (bus->status) {
1127 	case PFSYNC_BUS_START:
1128 		callout_reset(&sc->sc_bulkfail_tmo, 4 * hz +
1129 		    V_pf_limits[PF_LIMIT_STATES].limit /
1130 		    ((sc->sc_ifp->if_mtu - PFSYNC_MINPKT) /
1131 		    sizeof(struct pfsync_state)),
1132 		    pfsync_bulk_fail, sc);
1133 		if (V_pf_status.debug >= PF_DEBUG_MISC)
1134 			printf("pfsync: received bulk update start\n");
1135 		break;
1136 
1137 	case PFSYNC_BUS_END:
1138 		if (time_uptime - ntohl(bus->endtime) >=
1139 		    sc->sc_ureq_sent) {
1140 			/* that's it, we're happy */
1141 			sc->sc_ureq_sent = 0;
1142 			sc->sc_bulk_tries = 0;
1143 			callout_stop(&sc->sc_bulkfail_tmo);
1144 			if (!(sc->sc_flags & PFSYNCF_OK) && carp_demote_adj_p)
1145 				(*carp_demote_adj_p)(-V_pfsync_carp_adj,
1146 				    "pfsync bulk done");
1147 			sc->sc_flags |= PFSYNCF_OK;
1148 			if (V_pf_status.debug >= PF_DEBUG_MISC)
1149 				printf("pfsync: received valid "
1150 				    "bulk update end\n");
1151 		} else {
1152 			if (V_pf_status.debug >= PF_DEBUG_MISC)
1153 				printf("pfsync: received invalid "
1154 				    "bulk update end: bad timestamp\n");
1155 		}
1156 		break;
1157 	}
1158 	PFSYNC_BUNLOCK(sc);
1159 
1160 	return (len);
1161 }
1162 
1163 static int
1164 pfsync_in_tdb(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1165 {
1166 	int len = count * sizeof(struct pfsync_tdb);
1167 
1168 #if defined(IPSEC)
1169 	struct pfsync_tdb *tp;
1170 	struct mbuf *mp;
1171 	int offp;
1172 	int i;
1173 	int s;
1174 
1175 	mp = m_pulldown(m, offset, len, &offp);
1176 	if (mp == NULL) {
1177 		V_pfsyncstats.pfsyncs_badlen++;
1178 		return (-1);
1179 	}
1180 	tp = (struct pfsync_tdb *)(mp->m_data + offp);
1181 
1182 	for (i = 0; i < count; i++)
1183 		pfsync_update_net_tdb(&tp[i]);
1184 #endif
1185 
1186 	return (len);
1187 }
1188 
1189 #if defined(IPSEC)
1190 /* Update an in-kernel tdb. Silently fail if no tdb is found. */
1191 static void
1192 pfsync_update_net_tdb(struct pfsync_tdb *pt)
1193 {
1194 	struct tdb		*tdb;
1195 	int			 s;
1196 
1197 	/* check for invalid values */
1198 	if (ntohl(pt->spi) <= SPI_RESERVED_MAX ||
1199 	    (pt->dst.sa.sa_family != AF_INET &&
1200 	    pt->dst.sa.sa_family != AF_INET6))
1201 		goto bad;
1202 
1203 	tdb = gettdb(pt->spi, &pt->dst, pt->sproto);
1204 	if (tdb) {
1205 		pt->rpl = ntohl(pt->rpl);
1206 		pt->cur_bytes = (unsigned long long)be64toh(pt->cur_bytes);
1207 
1208 		/* Neither replay nor byte counter should ever decrease. */
1209 		if (pt->rpl < tdb->tdb_rpl ||
1210 		    pt->cur_bytes < tdb->tdb_cur_bytes) {
1211 			goto bad;
1212 		}
1213 
1214 		tdb->tdb_rpl = pt->rpl;
1215 		tdb->tdb_cur_bytes = pt->cur_bytes;
1216 	}
1217 	return;
1218 
1219 bad:
1220 	if (V_pf_status.debug >= PF_DEBUG_MISC)
1221 		printf("pfsync_insert: PFSYNC_ACT_TDB_UPD: "
1222 		    "invalid value\n");
1223 	V_pfsyncstats.pfsyncs_badstate++;
1224 	return;
1225 }
1226 #endif
1227 
1228 
1229 static int
1230 pfsync_in_eof(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1231 {
1232 	/* check if we are at the right place in the packet */
1233 	if (offset != m->m_pkthdr.len)
1234 		V_pfsyncstats.pfsyncs_badlen++;
1235 
1236 	/* we're done. free and let the caller return */
1237 	m_freem(m);
1238 	return (-1);
1239 }
1240 
1241 static int
1242 pfsync_in_error(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1243 {
1244 	V_pfsyncstats.pfsyncs_badact++;
1245 
1246 	m_freem(m);
1247 	return (-1);
1248 }
1249 
1250 static int
1251 pfsyncoutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
1252 	struct route *rt)
1253 {
1254 	m_freem(m);
1255 	return (0);
1256 }
1257 
1258 /* ARGSUSED */
1259 static int
1260 pfsyncioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1261 {
1262 	struct pfsync_softc *sc = ifp->if_softc;
1263 	struct ifreq *ifr = (struct ifreq *)data;
1264 	struct pfsyncreq pfsyncr;
1265 	int error;
1266 
1267 	switch (cmd) {
1268 	case SIOCSIFFLAGS:
1269 		PFSYNC_LOCK(sc);
1270 		if (ifp->if_flags & IFF_UP) {
1271 			ifp->if_drv_flags |= IFF_DRV_RUNNING;
1272 			PFSYNC_UNLOCK(sc);
1273 			pfsync_pointers_init();
1274 		} else {
1275 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1276 			PFSYNC_UNLOCK(sc);
1277 			pfsync_pointers_uninit();
1278 		}
1279 		break;
1280 	case SIOCSIFMTU:
1281 		if (!sc->sc_sync_if ||
1282 		    ifr->ifr_mtu <= PFSYNC_MINPKT ||
1283 		    ifr->ifr_mtu > sc->sc_sync_if->if_mtu)
1284 			return (EINVAL);
1285 		if (ifr->ifr_mtu < ifp->if_mtu) {
1286 			PFSYNC_LOCK(sc);
1287 			if (sc->sc_len > PFSYNC_MINPKT)
1288 				pfsync_sendout(1);
1289 			PFSYNC_UNLOCK(sc);
1290 		}
1291 		ifp->if_mtu = ifr->ifr_mtu;
1292 		break;
1293 	case SIOCGETPFSYNC:
1294 		bzero(&pfsyncr, sizeof(pfsyncr));
1295 		PFSYNC_LOCK(sc);
1296 		if (sc->sc_sync_if) {
1297 			strlcpy(pfsyncr.pfsyncr_syncdev,
1298 			    sc->sc_sync_if->if_xname, IFNAMSIZ);
1299 		}
1300 		pfsyncr.pfsyncr_syncpeer = sc->sc_sync_peer;
1301 		pfsyncr.pfsyncr_maxupdates = sc->sc_maxupdates;
1302 		pfsyncr.pfsyncr_defer = (PFSYNCF_DEFER ==
1303 		    (sc->sc_flags & PFSYNCF_DEFER));
1304 		PFSYNC_UNLOCK(sc);
1305 		return (copyout(&pfsyncr, ifr->ifr_data, sizeof(pfsyncr)));
1306 
1307 	case SIOCSETPFSYNC:
1308 	    {
1309 		struct ip_moptions *imo = &sc->sc_imo;
1310 		struct ifnet *sifp;
1311 		struct ip *ip;
1312 		void *mship = NULL;
1313 
1314 		if ((error = priv_check(curthread, PRIV_NETINET_PF)) != 0)
1315 			return (error);
1316 		if ((error = copyin(ifr->ifr_data, &pfsyncr, sizeof(pfsyncr))))
1317 			return (error);
1318 
1319 		if (pfsyncr.pfsyncr_maxupdates > 255)
1320 			return (EINVAL);
1321 
1322 		if (pfsyncr.pfsyncr_syncdev[0] == 0)
1323 			sifp = NULL;
1324 		else if ((sifp = ifunit_ref(pfsyncr.pfsyncr_syncdev)) == NULL)
1325 			return (EINVAL);
1326 
1327 		if (pfsyncr.pfsyncr_syncpeer.s_addr == 0 && sifp != NULL)
1328 			mship = malloc((sizeof(struct in_multi *) *
1329 			    IP_MIN_MEMBERSHIPS), M_PFSYNC, M_WAITOK | M_ZERO);
1330 
1331 		PFSYNC_LOCK(sc);
1332 		if (pfsyncr.pfsyncr_syncpeer.s_addr == 0)
1333 			sc->sc_sync_peer.s_addr = htonl(INADDR_PFSYNC_GROUP);
1334 		else
1335 			sc->sc_sync_peer.s_addr =
1336 			    pfsyncr.pfsyncr_syncpeer.s_addr;
1337 
1338 		sc->sc_maxupdates = pfsyncr.pfsyncr_maxupdates;
1339 		if (pfsyncr.pfsyncr_defer) {
1340 			sc->sc_flags |= PFSYNCF_DEFER;
1341 			pfsync_defer_ptr = pfsync_defer;
1342 		} else {
1343 			sc->sc_flags &= ~PFSYNCF_DEFER;
1344 			pfsync_defer_ptr = NULL;
1345 		}
1346 
1347 		if (sifp == NULL) {
1348 			if (sc->sc_sync_if)
1349 				if_rele(sc->sc_sync_if);
1350 			sc->sc_sync_if = NULL;
1351 			if (imo->imo_membership)
1352 				pfsync_multicast_cleanup(sc);
1353 			PFSYNC_UNLOCK(sc);
1354 			break;
1355 		}
1356 
1357 		if (sc->sc_len > PFSYNC_MINPKT &&
1358 		    (sifp->if_mtu < sc->sc_ifp->if_mtu ||
1359 		    (sc->sc_sync_if != NULL &&
1360 		    sifp->if_mtu < sc->sc_sync_if->if_mtu) ||
1361 		    sifp->if_mtu < MCLBYTES - sizeof(struct ip)))
1362 			pfsync_sendout(1);
1363 
1364 		if (imo->imo_membership)
1365 			pfsync_multicast_cleanup(sc);
1366 
1367 		if (sc->sc_sync_peer.s_addr == htonl(INADDR_PFSYNC_GROUP)) {
1368 			error = pfsync_multicast_setup(sc, sifp, mship);
1369 			if (error) {
1370 				if_rele(sifp);
1371 				free(mship, M_PFSYNC);
1372 				return (error);
1373 			}
1374 		}
1375 		if (sc->sc_sync_if)
1376 			if_rele(sc->sc_sync_if);
1377 		sc->sc_sync_if = sifp;
1378 
1379 		ip = &sc->sc_template;
1380 		bzero(ip, sizeof(*ip));
1381 		ip->ip_v = IPVERSION;
1382 		ip->ip_hl = sizeof(sc->sc_template) >> 2;
1383 		ip->ip_tos = IPTOS_LOWDELAY;
1384 		/* len and id are set later. */
1385 		ip->ip_off = htons(IP_DF);
1386 		ip->ip_ttl = PFSYNC_DFLTTL;
1387 		ip->ip_p = IPPROTO_PFSYNC;
1388 		ip->ip_src.s_addr = INADDR_ANY;
1389 		ip->ip_dst.s_addr = sc->sc_sync_peer.s_addr;
1390 
1391 		/* Request a full state table update. */
1392 		if ((sc->sc_flags & PFSYNCF_OK) && carp_demote_adj_p)
1393 			(*carp_demote_adj_p)(V_pfsync_carp_adj,
1394 			    "pfsync bulk start");
1395 		sc->sc_flags &= ~PFSYNCF_OK;
1396 		if (V_pf_status.debug >= PF_DEBUG_MISC)
1397 			printf("pfsync: requesting bulk update\n");
1398 		pfsync_request_update(0, 0);
1399 		PFSYNC_UNLOCK(sc);
1400 		PFSYNC_BLOCK(sc);
1401 		sc->sc_ureq_sent = time_uptime;
1402 		callout_reset(&sc->sc_bulkfail_tmo, 5 * hz, pfsync_bulk_fail,
1403 		    sc);
1404 		PFSYNC_BUNLOCK(sc);
1405 
1406 		break;
1407 	    }
1408 	default:
1409 		return (ENOTTY);
1410 	}
1411 
1412 	return (0);
1413 }
1414 
1415 static void
1416 pfsync_out_state(struct pf_state *st, void *buf)
1417 {
1418 	struct pfsync_state *sp = buf;
1419 
1420 	pfsync_state_export(sp, st);
1421 }
1422 
1423 static void
1424 pfsync_out_iack(struct pf_state *st, void *buf)
1425 {
1426 	struct pfsync_ins_ack *iack = buf;
1427 
1428 	iack->id = st->id;
1429 	iack->creatorid = st->creatorid;
1430 }
1431 
1432 static void
1433 pfsync_out_upd_c(struct pf_state *st, void *buf)
1434 {
1435 	struct pfsync_upd_c *up = buf;
1436 
1437 	bzero(up, sizeof(*up));
1438 	up->id = st->id;
1439 	pf_state_peer_hton(&st->src, &up->src);
1440 	pf_state_peer_hton(&st->dst, &up->dst);
1441 	up->creatorid = st->creatorid;
1442 	up->timeout = st->timeout;
1443 }
1444 
1445 static void
1446 pfsync_out_del(struct pf_state *st, void *buf)
1447 {
1448 	struct pfsync_del_c *dp = buf;
1449 
1450 	dp->id = st->id;
1451 	dp->creatorid = st->creatorid;
1452 	st->state_flags |= PFSTATE_NOSYNC;
1453 }
1454 
1455 static void
1456 pfsync_drop(struct pfsync_softc *sc)
1457 {
1458 	struct pf_state *st, *next;
1459 	struct pfsync_upd_req_item *ur;
1460 	int q;
1461 
1462 	for (q = 0; q < PFSYNC_S_COUNT; q++) {
1463 		if (TAILQ_EMPTY(&sc->sc_qs[q]))
1464 			continue;
1465 
1466 		TAILQ_FOREACH_SAFE(st, &sc->sc_qs[q], sync_list, next) {
1467 			KASSERT(st->sync_state == q,
1468 				("%s: st->sync_state == q",
1469 					__func__));
1470 			st->sync_state = PFSYNC_S_NONE;
1471 			pf_release_state(st);
1472 		}
1473 		TAILQ_INIT(&sc->sc_qs[q]);
1474 	}
1475 
1476 	while ((ur = TAILQ_FIRST(&sc->sc_upd_req_list)) != NULL) {
1477 		TAILQ_REMOVE(&sc->sc_upd_req_list, ur, ur_entry);
1478 		free(ur, M_PFSYNC);
1479 	}
1480 
1481 	sc->sc_plus = NULL;
1482 	sc->sc_len = PFSYNC_MINPKT;
1483 }
1484 
1485 static void
1486 pfsync_sendout(int schedswi)
1487 {
1488 	struct pfsync_softc *sc = V_pfsyncif;
1489 	struct ifnet *ifp = sc->sc_ifp;
1490 	struct mbuf *m;
1491 	struct ip *ip;
1492 	struct pfsync_header *ph;
1493 	struct pfsync_subheader *subh;
1494 	struct pf_state *st;
1495 	struct pfsync_upd_req_item *ur;
1496 	int offset;
1497 	int q, count = 0;
1498 
1499 	KASSERT(sc != NULL, ("%s: null sc", __func__));
1500 	KASSERT(sc->sc_len > PFSYNC_MINPKT,
1501 	    ("%s: sc_len %zu", __func__, sc->sc_len));
1502 	PFSYNC_LOCK_ASSERT(sc);
1503 
1504 	if (ifp->if_bpf == NULL && sc->sc_sync_if == NULL) {
1505 		pfsync_drop(sc);
1506 		return;
1507 	}
1508 
1509 	m = m_get2(max_linkhdr + sc->sc_len, M_NOWAIT, MT_DATA, M_PKTHDR);
1510 	if (m == NULL) {
1511 		sc->sc_ifp->if_oerrors++;
1512 		V_pfsyncstats.pfsyncs_onomem++;
1513 		return;
1514 	}
1515 	m->m_data += max_linkhdr;
1516 	m->m_len = m->m_pkthdr.len = sc->sc_len;
1517 
1518 	/* build the ip header */
1519 	ip = (struct ip *)m->m_data;
1520 	bcopy(&sc->sc_template, ip, sizeof(*ip));
1521 	offset = sizeof(*ip);
1522 
1523 	ip->ip_len = htons(m->m_pkthdr.len);
1524 	ip->ip_id = htons(ip_randomid());
1525 
1526 	/* build the pfsync header */
1527 	ph = (struct pfsync_header *)(m->m_data + offset);
1528 	bzero(ph, sizeof(*ph));
1529 	offset += sizeof(*ph);
1530 
1531 	ph->version = PFSYNC_VERSION;
1532 	ph->len = htons(sc->sc_len - sizeof(*ip));
1533 	bcopy(V_pf_status.pf_chksum, ph->pfcksum, PF_MD5_DIGEST_LENGTH);
1534 
1535 	/* walk the queues */
1536 	for (q = 0; q < PFSYNC_S_COUNT; q++) {
1537 		if (TAILQ_EMPTY(&sc->sc_qs[q]))
1538 			continue;
1539 
1540 		subh = (struct pfsync_subheader *)(m->m_data + offset);
1541 		offset += sizeof(*subh);
1542 
1543 		count = 0;
1544 		TAILQ_FOREACH(st, &sc->sc_qs[q], sync_list) {
1545 			KASSERT(st->sync_state == q,
1546 				("%s: st->sync_state == q",
1547 					__func__));
1548 			/*
1549 			 * XXXGL: some of write methods do unlocked reads
1550 			 * of state data :(
1551 			 */
1552 			pfsync_qs[q].write(st, m->m_data + offset);
1553 			offset += pfsync_qs[q].len;
1554 			st->sync_state = PFSYNC_S_NONE;
1555 			pf_release_state(st);
1556 			count++;
1557 		}
1558 		TAILQ_INIT(&sc->sc_qs[q]);
1559 
1560 		bzero(subh, sizeof(*subh));
1561 		subh->action = pfsync_qs[q].action;
1562 		subh->count = htons(count);
1563 		V_pfsyncstats.pfsyncs_oacts[pfsync_qs[q].action] += count;
1564 	}
1565 
1566 	if (!TAILQ_EMPTY(&sc->sc_upd_req_list)) {
1567 		subh = (struct pfsync_subheader *)(m->m_data + offset);
1568 		offset += sizeof(*subh);
1569 
1570 		count = 0;
1571 		while ((ur = TAILQ_FIRST(&sc->sc_upd_req_list)) != NULL) {
1572 			TAILQ_REMOVE(&sc->sc_upd_req_list, ur, ur_entry);
1573 
1574 			bcopy(&ur->ur_msg, m->m_data + offset,
1575 			    sizeof(ur->ur_msg));
1576 			offset += sizeof(ur->ur_msg);
1577 			free(ur, M_PFSYNC);
1578 			count++;
1579 		}
1580 
1581 		bzero(subh, sizeof(*subh));
1582 		subh->action = PFSYNC_ACT_UPD_REQ;
1583 		subh->count = htons(count);
1584 		V_pfsyncstats.pfsyncs_oacts[PFSYNC_ACT_UPD_REQ] += count;
1585 	}
1586 
1587 	/* has someone built a custom region for us to add? */
1588 	if (sc->sc_plus != NULL) {
1589 		bcopy(sc->sc_plus, m->m_data + offset, sc->sc_pluslen);
1590 		offset += sc->sc_pluslen;
1591 
1592 		sc->sc_plus = NULL;
1593 	}
1594 
1595 	subh = (struct pfsync_subheader *)(m->m_data + offset);
1596 	offset += sizeof(*subh);
1597 
1598 	bzero(subh, sizeof(*subh));
1599 	subh->action = PFSYNC_ACT_EOF;
1600 	subh->count = htons(1);
1601 	V_pfsyncstats.pfsyncs_oacts[PFSYNC_ACT_EOF]++;
1602 
1603 	/* we're done, let's put it on the wire */
1604 	if (ifp->if_bpf) {
1605 		m->m_data += sizeof(*ip);
1606 		m->m_len = m->m_pkthdr.len = sc->sc_len - sizeof(*ip);
1607 		BPF_MTAP(ifp, m);
1608 		m->m_data -= sizeof(*ip);
1609 		m->m_len = m->m_pkthdr.len = sc->sc_len;
1610 	}
1611 
1612 	if (sc->sc_sync_if == NULL) {
1613 		sc->sc_len = PFSYNC_MINPKT;
1614 		m_freem(m);
1615 		return;
1616 	}
1617 
1618 	sc->sc_ifp->if_opackets++;
1619 	sc->sc_ifp->if_obytes += m->m_pkthdr.len;
1620 	sc->sc_len = PFSYNC_MINPKT;
1621 
1622 	if (!_IF_QFULL(&sc->sc_ifp->if_snd))
1623 		_IF_ENQUEUE(&sc->sc_ifp->if_snd, m);
1624 	else {
1625 		m_freem(m);
1626 		sc->sc_ifp->if_snd.ifq_drops++;
1627 	}
1628 	if (schedswi)
1629 		swi_sched(V_pfsync_swi_cookie, 0);
1630 }
1631 
1632 static void
1633 pfsync_insert_state(struct pf_state *st)
1634 {
1635 	struct pfsync_softc *sc = V_pfsyncif;
1636 
1637 	if (st->state_flags & PFSTATE_NOSYNC)
1638 		return;
1639 
1640 	if ((st->rule.ptr->rule_flag & PFRULE_NOSYNC) ||
1641 	    st->key[PF_SK_WIRE]->proto == IPPROTO_PFSYNC) {
1642 		st->state_flags |= PFSTATE_NOSYNC;
1643 		return;
1644 	}
1645 
1646 	KASSERT(st->sync_state == PFSYNC_S_NONE,
1647 		("%s: st->sync_state %u", __func__, st->sync_state));
1648 
1649 	PFSYNC_LOCK(sc);
1650 	if (sc->sc_len == PFSYNC_MINPKT)
1651 		callout_reset(&sc->sc_tmo, 1 * hz, pfsync_timeout, V_pfsyncif);
1652 
1653 	pfsync_q_ins(st, PFSYNC_S_INS);
1654 	PFSYNC_UNLOCK(sc);
1655 
1656 	st->sync_updates = 0;
1657 }
1658 
1659 static int
1660 pfsync_defer(struct pf_state *st, struct mbuf *m)
1661 {
1662 	struct pfsync_softc *sc = V_pfsyncif;
1663 	struct pfsync_deferral *pd;
1664 
1665 	if (m->m_flags & (M_BCAST|M_MCAST))
1666 		return (0);
1667 
1668 	PFSYNC_LOCK(sc);
1669 
1670 	if (sc == NULL || !(sc->sc_ifp->if_flags & IFF_DRV_RUNNING) ||
1671 	    !(sc->sc_flags & PFSYNCF_DEFER)) {
1672 		PFSYNC_UNLOCK(sc);
1673 		return (0);
1674 	}
1675 
1676 	 if (sc->sc_deferred >= 128)
1677 		pfsync_undefer(TAILQ_FIRST(&sc->sc_deferrals), 0);
1678 
1679 	pd = malloc(sizeof(*pd), M_PFSYNC, M_NOWAIT);
1680 	if (pd == NULL)
1681 		return (0);
1682 	sc->sc_deferred++;
1683 
1684 	m->m_flags |= M_SKIP_FIREWALL;
1685 	st->state_flags |= PFSTATE_ACK;
1686 
1687 	pd->pd_sc = sc;
1688 	pd->pd_refs = 0;
1689 	pd->pd_st = st;
1690 	pf_ref_state(st);
1691 	pd->pd_m = m;
1692 
1693 	TAILQ_INSERT_TAIL(&sc->sc_deferrals, pd, pd_entry);
1694 	callout_init_mtx(&pd->pd_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1695 	callout_reset(&pd->pd_tmo, 10, pfsync_defer_tmo, pd);
1696 
1697 	pfsync_push(sc);
1698 
1699 	return (1);
1700 }
1701 
1702 static void
1703 pfsync_undefer(struct pfsync_deferral *pd, int drop)
1704 {
1705 	struct pfsync_softc *sc = pd->pd_sc;
1706 	struct mbuf *m = pd->pd_m;
1707 	struct pf_state *st = pd->pd_st;
1708 
1709 	PFSYNC_LOCK_ASSERT(sc);
1710 
1711 	TAILQ_REMOVE(&sc->sc_deferrals, pd, pd_entry);
1712 	sc->sc_deferred--;
1713 	pd->pd_st->state_flags &= ~PFSTATE_ACK;	/* XXX: locking! */
1714 	free(pd, M_PFSYNC);
1715 	pf_release_state(st);
1716 
1717 	if (drop)
1718 		m_freem(m);
1719 	else {
1720 		_IF_ENQUEUE(&sc->sc_ifp->if_snd, m);
1721 		pfsync_push(sc);
1722 	}
1723 }
1724 
1725 static void
1726 pfsync_defer_tmo(void *arg)
1727 {
1728 	struct pfsync_deferral *pd = arg;
1729 	struct pfsync_softc *sc = pd->pd_sc;
1730 	struct mbuf *m = pd->pd_m;
1731 	struct pf_state *st = pd->pd_st;
1732 
1733 	PFSYNC_LOCK_ASSERT(sc);
1734 
1735 	CURVNET_SET(m->m_pkthdr.rcvif->if_vnet);
1736 
1737 	TAILQ_REMOVE(&sc->sc_deferrals, pd, pd_entry);
1738 	sc->sc_deferred--;
1739 	pd->pd_st->state_flags &= ~PFSTATE_ACK;	/* XXX: locking! */
1740 	if (pd->pd_refs == 0)
1741 		free(pd, M_PFSYNC);
1742 	PFSYNC_UNLOCK(sc);
1743 
1744 	ip_output(m, NULL, NULL, 0, NULL, NULL);
1745 
1746 	pf_release_state(st);
1747 
1748 	CURVNET_RESTORE();
1749 }
1750 
1751 static void
1752 pfsync_undefer_state(struct pf_state *st, int drop)
1753 {
1754 	struct pfsync_softc *sc = V_pfsyncif;
1755 	struct pfsync_deferral *pd;
1756 
1757 	PFSYNC_LOCK_ASSERT(sc);
1758 
1759 	TAILQ_FOREACH(pd, &sc->sc_deferrals, pd_entry) {
1760 		 if (pd->pd_st == st) {
1761 			if (callout_stop(&pd->pd_tmo))
1762 				pfsync_undefer(pd, drop);
1763 			return;
1764 		}
1765 	}
1766 
1767 	panic("%s: unable to find deferred state", __func__);
1768 }
1769 
1770 static void
1771 pfsync_update_state(struct pf_state *st)
1772 {
1773 	struct pfsync_softc *sc = V_pfsyncif;
1774 	int sync = 0;
1775 
1776 	PF_STATE_LOCK_ASSERT(st);
1777 	PFSYNC_LOCK(sc);
1778 
1779 	if (st->state_flags & PFSTATE_ACK)
1780 		pfsync_undefer_state(st, 0);
1781 	if (st->state_flags & PFSTATE_NOSYNC) {
1782 		if (st->sync_state != PFSYNC_S_NONE)
1783 			pfsync_q_del(st);
1784 		PFSYNC_UNLOCK(sc);
1785 		return;
1786 	}
1787 
1788 	if (sc->sc_len == PFSYNC_MINPKT)
1789 		callout_reset(&sc->sc_tmo, 1 * hz, pfsync_timeout, V_pfsyncif);
1790 
1791 	switch (st->sync_state) {
1792 	case PFSYNC_S_UPD_C:
1793 	case PFSYNC_S_UPD:
1794 	case PFSYNC_S_INS:
1795 		/* we're already handling it */
1796 
1797 		if (st->key[PF_SK_WIRE]->proto == IPPROTO_TCP) {
1798 			st->sync_updates++;
1799 			if (st->sync_updates >= sc->sc_maxupdates)
1800 				sync = 1;
1801 		}
1802 		break;
1803 
1804 	case PFSYNC_S_IACK:
1805 		pfsync_q_del(st);
1806 	case PFSYNC_S_NONE:
1807 		pfsync_q_ins(st, PFSYNC_S_UPD_C);
1808 		st->sync_updates = 0;
1809 		break;
1810 
1811 	default:
1812 		panic("%s: unexpected sync state %d", __func__, st->sync_state);
1813 	}
1814 
1815 	if (sync || (time_uptime - st->pfsync_time) < 2)
1816 		pfsync_push(sc);
1817 
1818 	PFSYNC_UNLOCK(sc);
1819 }
1820 
1821 static void
1822 pfsync_request_update(u_int32_t creatorid, u_int64_t id)
1823 {
1824 	struct pfsync_softc *sc = V_pfsyncif;
1825 	struct pfsync_upd_req_item *item;
1826 	size_t nlen = sizeof(struct pfsync_upd_req);
1827 
1828 	PFSYNC_LOCK_ASSERT(sc);
1829 
1830 	/*
1831 	 * This code does a bit to prevent multiple update requests for the
1832 	 * same state being generated. It searches current subheader queue,
1833 	 * but it doesn't lookup into queue of already packed datagrams.
1834 	 */
1835 	TAILQ_FOREACH(item, &sc->sc_upd_req_list, ur_entry)
1836 		if (item->ur_msg.id == id &&
1837 		    item->ur_msg.creatorid == creatorid)
1838 			return;
1839 
1840 	item = malloc(sizeof(*item), M_PFSYNC, M_NOWAIT);
1841 	if (item == NULL)
1842 		return; /* XXX stats */
1843 
1844 	item->ur_msg.id = id;
1845 	item->ur_msg.creatorid = creatorid;
1846 
1847 	if (TAILQ_EMPTY(&sc->sc_upd_req_list))
1848 		nlen += sizeof(struct pfsync_subheader);
1849 
1850 	if (sc->sc_len + nlen > sc->sc_ifp->if_mtu) {
1851 		pfsync_sendout(1);
1852 
1853 		nlen = sizeof(struct pfsync_subheader) +
1854 		    sizeof(struct pfsync_upd_req);
1855 	}
1856 
1857 	TAILQ_INSERT_TAIL(&sc->sc_upd_req_list, item, ur_entry);
1858 	sc->sc_len += nlen;
1859 }
1860 
1861 static void
1862 pfsync_update_state_req(struct pf_state *st)
1863 {
1864 	struct pfsync_softc *sc = V_pfsyncif;
1865 
1866 	PF_STATE_LOCK_ASSERT(st);
1867 	PFSYNC_LOCK(sc);
1868 
1869 	if (st->state_flags & PFSTATE_NOSYNC) {
1870 		if (st->sync_state != PFSYNC_S_NONE)
1871 			pfsync_q_del(st);
1872 		PFSYNC_UNLOCK(sc);
1873 		return;
1874 	}
1875 
1876 	switch (st->sync_state) {
1877 	case PFSYNC_S_UPD_C:
1878 	case PFSYNC_S_IACK:
1879 		pfsync_q_del(st);
1880 	case PFSYNC_S_NONE:
1881 		pfsync_q_ins(st, PFSYNC_S_UPD);
1882 		pfsync_push(sc);
1883 		break;
1884 
1885 	case PFSYNC_S_INS:
1886 	case PFSYNC_S_UPD:
1887 	case PFSYNC_S_DEL:
1888 		/* we're already handling it */
1889 		break;
1890 
1891 	default:
1892 		panic("%s: unexpected sync state %d", __func__, st->sync_state);
1893 	}
1894 
1895 	PFSYNC_UNLOCK(sc);
1896 }
1897 
1898 static void
1899 pfsync_delete_state(struct pf_state *st)
1900 {
1901 	struct pfsync_softc *sc = V_pfsyncif;
1902 
1903 	PFSYNC_LOCK(sc);
1904 	if (st->state_flags & PFSTATE_ACK)
1905 		pfsync_undefer_state(st, 1);
1906 	if (st->state_flags & PFSTATE_NOSYNC) {
1907 		if (st->sync_state != PFSYNC_S_NONE)
1908 			pfsync_q_del(st);
1909 		PFSYNC_UNLOCK(sc);
1910 		return;
1911 	}
1912 
1913 	if (sc->sc_len == PFSYNC_MINPKT)
1914 		callout_reset(&sc->sc_tmo, 1 * hz, pfsync_timeout, V_pfsyncif);
1915 
1916 	switch (st->sync_state) {
1917 	case PFSYNC_S_INS:
1918 		/* We never got to tell the world so just forget about it. */
1919 		pfsync_q_del(st);
1920 		break;
1921 
1922 	case PFSYNC_S_UPD_C:
1923 	case PFSYNC_S_UPD:
1924 	case PFSYNC_S_IACK:
1925 		pfsync_q_del(st);
1926 		/* FALLTHROUGH to putting it on the del list */
1927 
1928 	case PFSYNC_S_NONE:
1929 		pfsync_q_ins(st, PFSYNC_S_DEL);
1930 		break;
1931 
1932 	default:
1933 		panic("%s: unexpected sync state %d", __func__, st->sync_state);
1934 	}
1935 	PFSYNC_UNLOCK(sc);
1936 }
1937 
1938 static void
1939 pfsync_clear_states(u_int32_t creatorid, const char *ifname)
1940 {
1941 	struct pfsync_softc *sc = V_pfsyncif;
1942 	struct {
1943 		struct pfsync_subheader subh;
1944 		struct pfsync_clr clr;
1945 	} __packed r;
1946 
1947 	bzero(&r, sizeof(r));
1948 
1949 	r.subh.action = PFSYNC_ACT_CLR;
1950 	r.subh.count = htons(1);
1951 	V_pfsyncstats.pfsyncs_oacts[PFSYNC_ACT_CLR]++;
1952 
1953 	strlcpy(r.clr.ifname, ifname, sizeof(r.clr.ifname));
1954 	r.clr.creatorid = creatorid;
1955 
1956 	PFSYNC_LOCK(sc);
1957 	pfsync_send_plus(&r, sizeof(r));
1958 	PFSYNC_UNLOCK(sc);
1959 }
1960 
1961 static void
1962 pfsync_q_ins(struct pf_state *st, int q)
1963 {
1964 	struct pfsync_softc *sc = V_pfsyncif;
1965 	size_t nlen = pfsync_qs[q].len;
1966 
1967 	PFSYNC_LOCK_ASSERT(sc);
1968 
1969 	KASSERT(st->sync_state == PFSYNC_S_NONE,
1970 		("%s: st->sync_state %u", __func__, st->sync_state));
1971 	KASSERT(sc->sc_len >= PFSYNC_MINPKT, ("pfsync pkt len is too low %zu",
1972 	    sc->sc_len));
1973 
1974 	if (TAILQ_EMPTY(&sc->sc_qs[q]))
1975 		nlen += sizeof(struct pfsync_subheader);
1976 
1977 	if (sc->sc_len + nlen > sc->sc_ifp->if_mtu) {
1978 		pfsync_sendout(1);
1979 
1980 		nlen = sizeof(struct pfsync_subheader) + pfsync_qs[q].len;
1981 	}
1982 
1983 	sc->sc_len += nlen;
1984 	TAILQ_INSERT_TAIL(&sc->sc_qs[q], st, sync_list);
1985 	st->sync_state = q;
1986 	pf_ref_state(st);
1987 }
1988 
1989 static void
1990 pfsync_q_del(struct pf_state *st)
1991 {
1992 	struct pfsync_softc *sc = V_pfsyncif;
1993 	int q = st->sync_state;
1994 
1995 	PFSYNC_LOCK_ASSERT(sc);
1996 	KASSERT(st->sync_state != PFSYNC_S_NONE,
1997 		("%s: st->sync_state != PFSYNC_S_NONE", __func__));
1998 
1999 	sc->sc_len -= pfsync_qs[q].len;
2000 	TAILQ_REMOVE(&sc->sc_qs[q], st, sync_list);
2001 	st->sync_state = PFSYNC_S_NONE;
2002 	pf_release_state(st);
2003 
2004 	if (TAILQ_EMPTY(&sc->sc_qs[q]))
2005 		sc->sc_len -= sizeof(struct pfsync_subheader);
2006 }
2007 
2008 static void
2009 pfsync_bulk_start(void)
2010 {
2011 	struct pfsync_softc *sc = V_pfsyncif;
2012 
2013 	if (V_pf_status.debug >= PF_DEBUG_MISC)
2014 		printf("pfsync: received bulk update request\n");
2015 
2016 	PFSYNC_BLOCK(sc);
2017 
2018 	sc->sc_ureq_received = time_uptime;
2019 	sc->sc_bulk_hashid = 0;
2020 	sc->sc_bulk_stateid = 0;
2021 	pfsync_bulk_status(PFSYNC_BUS_START);
2022 	callout_reset(&sc->sc_bulk_tmo, 1, pfsync_bulk_update, sc);
2023 	PFSYNC_BUNLOCK(sc);
2024 }
2025 
2026 static void
2027 pfsync_bulk_update(void *arg)
2028 {
2029 	struct pfsync_softc *sc = arg;
2030 	struct pf_state *s;
2031 	int i, sent = 0;
2032 
2033 	PFSYNC_BLOCK_ASSERT(sc);
2034 	CURVNET_SET(sc->sc_ifp->if_vnet);
2035 
2036 	/*
2037 	 * Start with last state from previous invocation.
2038 	 * It may had gone, in this case start from the
2039 	 * hash slot.
2040 	 */
2041 	s = pf_find_state_byid(sc->sc_bulk_stateid, sc->sc_bulk_creatorid);
2042 
2043 	if (s != NULL)
2044 		i = PF_IDHASH(s);
2045 	else
2046 		i = sc->sc_bulk_hashid;
2047 
2048 	for (; i <= V_pf_hashmask; i++) {
2049 		struct pf_idhash *ih = &V_pf_idhash[i];
2050 
2051 		if (s != NULL)
2052 			PF_HASHROW_ASSERT(ih);
2053 		else {
2054 			PF_HASHROW_LOCK(ih);
2055 			s = LIST_FIRST(&ih->states);
2056 		}
2057 
2058 		for (; s; s = LIST_NEXT(s, entry)) {
2059 
2060 			if (sent > 1 && (sc->sc_ifp->if_mtu - sc->sc_len) <
2061 			    sizeof(struct pfsync_state)) {
2062 				/* We've filled a packet. */
2063 				sc->sc_bulk_hashid = i;
2064 				sc->sc_bulk_stateid = s->id;
2065 				sc->sc_bulk_creatorid = s->creatorid;
2066 				PF_HASHROW_UNLOCK(ih);
2067 				callout_reset(&sc->sc_bulk_tmo, 1,
2068 				    pfsync_bulk_update, sc);
2069 				goto full;
2070 			}
2071 
2072 			if (s->sync_state == PFSYNC_S_NONE &&
2073 			    s->timeout < PFTM_MAX &&
2074 			    s->pfsync_time <= sc->sc_ureq_received) {
2075 				pfsync_update_state_req(s);
2076 				sent++;
2077 			}
2078 		}
2079 		PF_HASHROW_UNLOCK(ih);
2080 	}
2081 
2082 	/* We're done. */
2083 	pfsync_bulk_status(PFSYNC_BUS_END);
2084 
2085 full:
2086 	CURVNET_RESTORE();
2087 }
2088 
2089 static void
2090 pfsync_bulk_status(u_int8_t status)
2091 {
2092 	struct {
2093 		struct pfsync_subheader subh;
2094 		struct pfsync_bus bus;
2095 	} __packed r;
2096 
2097 	struct pfsync_softc *sc = V_pfsyncif;
2098 
2099 	bzero(&r, sizeof(r));
2100 
2101 	r.subh.action = PFSYNC_ACT_BUS;
2102 	r.subh.count = htons(1);
2103 	V_pfsyncstats.pfsyncs_oacts[PFSYNC_ACT_BUS]++;
2104 
2105 	r.bus.creatorid = V_pf_status.hostid;
2106 	r.bus.endtime = htonl(time_uptime - sc->sc_ureq_received);
2107 	r.bus.status = status;
2108 
2109 	PFSYNC_LOCK(sc);
2110 	pfsync_send_plus(&r, sizeof(r));
2111 	PFSYNC_UNLOCK(sc);
2112 }
2113 
2114 static void
2115 pfsync_bulk_fail(void *arg)
2116 {
2117 	struct pfsync_softc *sc = arg;
2118 
2119 	CURVNET_SET(sc->sc_ifp->if_vnet);
2120 
2121 	PFSYNC_BLOCK_ASSERT(sc);
2122 
2123 	if (sc->sc_bulk_tries++ < PFSYNC_MAX_BULKTRIES) {
2124 		/* Try again */
2125 		callout_reset(&sc->sc_bulkfail_tmo, 5 * hz,
2126 		    pfsync_bulk_fail, V_pfsyncif);
2127 		PFSYNC_LOCK(sc);
2128 		pfsync_request_update(0, 0);
2129 		PFSYNC_UNLOCK(sc);
2130 	} else {
2131 		/* Pretend like the transfer was ok. */
2132 		sc->sc_ureq_sent = 0;
2133 		sc->sc_bulk_tries = 0;
2134 		PFSYNC_LOCK(sc);
2135 		if (!(sc->sc_flags & PFSYNCF_OK) && carp_demote_adj_p)
2136 			(*carp_demote_adj_p)(-V_pfsync_carp_adj,
2137 			    "pfsync bulk fail");
2138 		sc->sc_flags |= PFSYNCF_OK;
2139 		PFSYNC_UNLOCK(sc);
2140 		if (V_pf_status.debug >= PF_DEBUG_MISC)
2141 			printf("pfsync: failed to receive bulk update\n");
2142 	}
2143 
2144 	CURVNET_RESTORE();
2145 }
2146 
2147 static void
2148 pfsync_send_plus(void *plus, size_t pluslen)
2149 {
2150 	struct pfsync_softc *sc = V_pfsyncif;
2151 
2152 	PFSYNC_LOCK_ASSERT(sc);
2153 
2154 	if (sc->sc_len + pluslen > sc->sc_ifp->if_mtu)
2155 		pfsync_sendout(1);
2156 
2157 	sc->sc_plus = plus;
2158 	sc->sc_len += (sc->sc_pluslen = pluslen);
2159 
2160 	pfsync_sendout(1);
2161 }
2162 
2163 static void
2164 pfsync_timeout(void *arg)
2165 {
2166 	struct pfsync_softc *sc = arg;
2167 
2168 	CURVNET_SET(sc->sc_ifp->if_vnet);
2169 	PFSYNC_LOCK(sc);
2170 	pfsync_push(sc);
2171 	PFSYNC_UNLOCK(sc);
2172 	CURVNET_RESTORE();
2173 }
2174 
2175 static void
2176 pfsync_push(struct pfsync_softc *sc)
2177 {
2178 
2179 	PFSYNC_LOCK_ASSERT(sc);
2180 
2181 	sc->sc_flags |= PFSYNCF_PUSH;
2182 	swi_sched(V_pfsync_swi_cookie, 0);
2183 }
2184 
2185 static void
2186 pfsyncintr(void *arg)
2187 {
2188 	struct pfsync_softc *sc = arg;
2189 	struct mbuf *m, *n;
2190 
2191 	CURVNET_SET(sc->sc_ifp->if_vnet);
2192 
2193 	PFSYNC_LOCK(sc);
2194 	if ((sc->sc_flags & PFSYNCF_PUSH) && sc->sc_len > PFSYNC_MINPKT) {
2195 		pfsync_sendout(0);
2196 		sc->sc_flags &= ~PFSYNCF_PUSH;
2197 	}
2198 	_IF_DEQUEUE_ALL(&sc->sc_ifp->if_snd, m);
2199 	PFSYNC_UNLOCK(sc);
2200 
2201 	for (; m != NULL; m = n) {
2202 
2203 		n = m->m_nextpkt;
2204 		m->m_nextpkt = NULL;
2205 
2206 		/*
2207 		 * We distinguish between a deferral packet and our
2208 		 * own pfsync packet based on M_SKIP_FIREWALL
2209 		 * flag. This is XXX.
2210 		 */
2211 		if (m->m_flags & M_SKIP_FIREWALL)
2212 			ip_output(m, NULL, NULL, 0, NULL, NULL);
2213 		else if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo,
2214 		    NULL) == 0)
2215 			V_pfsyncstats.pfsyncs_opackets++;
2216 		else
2217 			V_pfsyncstats.pfsyncs_oerrors++;
2218 	}
2219 	CURVNET_RESTORE();
2220 }
2221 
2222 static int
2223 pfsync_multicast_setup(struct pfsync_softc *sc, struct ifnet *ifp, void *mship)
2224 {
2225 	struct ip_moptions *imo = &sc->sc_imo;
2226 	int error;
2227 
2228 	if (!(ifp->if_flags & IFF_MULTICAST))
2229 		return (EADDRNOTAVAIL);
2230 
2231 	imo->imo_membership = (struct in_multi **)mship;
2232 	imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
2233 	imo->imo_multicast_vif = -1;
2234 
2235 	if ((error = in_joingroup(ifp, &sc->sc_sync_peer, NULL,
2236 	    &imo->imo_membership[0])) != 0) {
2237 		imo->imo_membership = NULL;
2238 		return (error);
2239 	}
2240 	imo->imo_num_memberships++;
2241 	imo->imo_multicast_ifp = ifp;
2242 	imo->imo_multicast_ttl = PFSYNC_DFLTTL;
2243 	imo->imo_multicast_loop = 0;
2244 
2245 	return (0);
2246 }
2247 
2248 static void
2249 pfsync_multicast_cleanup(struct pfsync_softc *sc)
2250 {
2251 	struct ip_moptions *imo = &sc->sc_imo;
2252 
2253 	in_leavegroup(imo->imo_membership[0], NULL);
2254 	free(imo->imo_membership, M_PFSYNC);
2255 	imo->imo_membership = NULL;
2256 	imo->imo_multicast_ifp = NULL;
2257 }
2258 
2259 #ifdef INET
2260 extern  struct domain inetdomain;
2261 static struct protosw in_pfsync_protosw = {
2262 	.pr_type =		SOCK_RAW,
2263 	.pr_domain =		&inetdomain,
2264 	.pr_protocol =		IPPROTO_PFSYNC,
2265 	.pr_flags =		PR_ATOMIC|PR_ADDR,
2266 	.pr_input =		pfsync_input,
2267 	.pr_output =		(pr_output_t *)rip_output,
2268 	.pr_ctloutput =		rip_ctloutput,
2269 	.pr_usrreqs =		&rip_usrreqs
2270 };
2271 #endif
2272 
2273 static void
2274 pfsync_pointers_init()
2275 {
2276 
2277 	PF_RULES_WLOCK();
2278 	pfsync_state_import_ptr = pfsync_state_import;
2279 	pfsync_insert_state_ptr = pfsync_insert_state;
2280 	pfsync_update_state_ptr = pfsync_update_state;
2281 	pfsync_delete_state_ptr = pfsync_delete_state;
2282 	pfsync_clear_states_ptr = pfsync_clear_states;
2283 	pfsync_defer_ptr = pfsync_defer;
2284 	PF_RULES_WUNLOCK();
2285 }
2286 
2287 static void
2288 pfsync_pointers_uninit()
2289 {
2290 
2291 	PF_RULES_WLOCK();
2292 	pfsync_state_import_ptr = NULL;
2293 	pfsync_insert_state_ptr = NULL;
2294 	pfsync_update_state_ptr = NULL;
2295 	pfsync_delete_state_ptr = NULL;
2296 	pfsync_clear_states_ptr = NULL;
2297 	pfsync_defer_ptr = NULL;
2298 	PF_RULES_WUNLOCK();
2299 }
2300 
2301 static int
2302 pfsync_init()
2303 {
2304 	VNET_ITERATOR_DECL(vnet_iter);
2305 	int error = 0;
2306 
2307 	VNET_LIST_RLOCK();
2308 	VNET_FOREACH(vnet_iter) {
2309 		CURVNET_SET(vnet_iter);
2310 		V_pfsync_cloner = if_clone_simple(pfsyncname,
2311 		    pfsync_clone_create, pfsync_clone_destroy, 1);
2312 		error = swi_add(NULL, pfsyncname, pfsyncintr, V_pfsyncif,
2313 		    SWI_NET, INTR_MPSAFE, &V_pfsync_swi_cookie);
2314 		CURVNET_RESTORE();
2315 		if (error)
2316 			goto fail_locked;
2317 	}
2318 	VNET_LIST_RUNLOCK();
2319 #ifdef INET
2320 	error = pf_proto_register(PF_INET, &in_pfsync_protosw);
2321 	if (error)
2322 		goto fail;
2323 	error = ipproto_register(IPPROTO_PFSYNC);
2324 	if (error) {
2325 		pf_proto_unregister(PF_INET, IPPROTO_PFSYNC, SOCK_RAW);
2326 		goto fail;
2327 	}
2328 #endif
2329 	pfsync_pointers_init();
2330 
2331 	return (0);
2332 
2333 fail:
2334 	VNET_LIST_RLOCK();
2335 fail_locked:
2336 	VNET_FOREACH(vnet_iter) {
2337 		CURVNET_SET(vnet_iter);
2338 		if (V_pfsync_swi_cookie) {
2339 			swi_remove(V_pfsync_swi_cookie);
2340 			if_clone_detach(V_pfsync_cloner);
2341 		}
2342 		CURVNET_RESTORE();
2343 	}
2344 	VNET_LIST_RUNLOCK();
2345 
2346 	return (error);
2347 }
2348 
2349 static void
2350 pfsync_uninit()
2351 {
2352 	VNET_ITERATOR_DECL(vnet_iter);
2353 
2354 	pfsync_pointers_uninit();
2355 
2356 	ipproto_unregister(IPPROTO_PFSYNC);
2357 	pf_proto_unregister(PF_INET, IPPROTO_PFSYNC, SOCK_RAW);
2358 	VNET_LIST_RLOCK();
2359 	VNET_FOREACH(vnet_iter) {
2360 		CURVNET_SET(vnet_iter);
2361 		if_clone_detach(V_pfsync_cloner);
2362 		swi_remove(V_pfsync_swi_cookie);
2363 		CURVNET_RESTORE();
2364 	}
2365 	VNET_LIST_RUNLOCK();
2366 }
2367 
2368 static int
2369 pfsync_modevent(module_t mod, int type, void *data)
2370 {
2371 	int error = 0;
2372 
2373 	switch (type) {
2374 	case MOD_LOAD:
2375 		error = pfsync_init();
2376 		break;
2377 	case MOD_QUIESCE:
2378 		/*
2379 		 * Module should not be unloaded due to race conditions.
2380 		 */
2381 		error = EBUSY;
2382 		break;
2383 	case MOD_UNLOAD:
2384 		pfsync_uninit();
2385 		break;
2386 	default:
2387 		error = EINVAL;
2388 		break;
2389 	}
2390 
2391 	return (error);
2392 }
2393 
2394 static moduledata_t pfsync_mod = {
2395 	pfsyncname,
2396 	pfsync_modevent,
2397 	0
2398 };
2399 
2400 #define PFSYNC_MODVER 1
2401 
2402 DECLARE_MODULE(pfsync, pfsync_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
2403 MODULE_VERSION(pfsync, PFSYNC_MODVER);
2404 MODULE_DEPEND(pfsync, pf, PF_MODVER, PF_MODVER, PF_MODVER);
2405