1244769Sglebius/*-
2126258Smlaier * Copyright (c) 2002 Michael Shalayeff
3244769Sglebius * Copyright (c) 2012 Gleb Smirnoff <glebius@FreeBSD.org>
4126258Smlaier * All rights reserved.
5126258Smlaier *
6126258Smlaier * Redistribution and use in source and binary forms, with or without
7126258Smlaier * modification, are permitted provided that the following conditions
8126258Smlaier * are met:
9126258Smlaier * 1. Redistributions of source code must retain the above copyright
10126258Smlaier *    notice, this list of conditions and the following disclaimer.
11126258Smlaier * 2. Redistributions in binary form must reproduce the above copyright
12126258Smlaier *    notice, this list of conditions and the following disclaimer in the
13126258Smlaier *    documentation and/or other materials provided with the distribution.
14126258Smlaier *
15126258Smlaier * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16126258Smlaier * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17126258Smlaier * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18126258Smlaier * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
19126258Smlaier * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20126258Smlaier * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21126258Smlaier * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22126258Smlaier * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23126258Smlaier * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24126258Smlaier * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
25126258Smlaier * THE POSSIBILITY OF SUCH DAMAGE.
26126258Smlaier */
27126258Smlaier
28244769Sglebius/*-
29223637Sbz * Copyright (c) 2009 David Gwynne <dlg@openbsd.org>
30223637Sbz *
31223637Sbz * Permission to use, copy, modify, and distribute this software for any
32223637Sbz * purpose with or without fee is hereby granted, provided that the above
33223637Sbz * copyright notice and this permission notice appear in all copies.
34223637Sbz *
35223637Sbz * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
36223637Sbz * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
37223637Sbz * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
38223637Sbz * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
39223637Sbz * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
40223637Sbz * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
41223637Sbz * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
42223637Sbz */
43223637Sbz
44228814Sglebius/*
45244769Sglebius * $OpenBSD: if_pfsync.c,v 1.110 2009/02/24 05:39:19 dlg Exp $
46244769Sglebius *
47228814Sglebius * Revisions picked from OpenBSD after revision 1.110 import:
48244185Sglebius * 1.119 - don't m_copydata() beyond the len of mbuf in pfsync_input()
49228814Sglebius * 1.118, 1.124, 1.148, 1.149, 1.151, 1.171 - fixes to bulk updates
50228815Sglebius * 1.120, 1.175 - use monotonic time_uptime
51228816Sglebius * 1.122 - reduce number of updates for non-TCP sessions
52244113Sglebius * 1.125, 1.127 - rewrite merge or stale processing
53229961Sglebius * 1.128 - cleanups
54232685Sglebius * 1.146 - bzero() mbuf before sparsely filling it with data
55229777Sglebius * 1.170 - SIOCSIFMTU checks
56233846Sglebius * 1.126, 1.142 - deferred packets processing
57233874Sglebius * 1.173 - correct expire time processing
58228814Sglebius */
59228814Sglebius
60240233Sglebius#include <sys/cdefs.h>
61240233Sglebius__FBSDID("$FreeBSD: stable/10/sys/netpfil/pf/if_pfsync.c 314667 2017-03-04 13:03:31Z avg $");
62240233Sglebius
63126261Smlaier#include "opt_inet.h"
64126261Smlaier#include "opt_inet6.h"
65126261Smlaier#include "opt_pf.h"
66153110Sru
67126258Smlaier#include <sys/param.h>
68223637Sbz#include <sys/bus.h>
69240233Sglebius#include <sys/endian.h>
70223637Sbz#include <sys/interrupt.h>
71240233Sglebius#include <sys/kernel.h>
72240233Sglebius#include <sys/lock.h>
73126258Smlaier#include <sys/mbuf.h>
74129907Smlaier#include <sys/module.h>
75130613Smlaier#include <sys/mutex.h>
76240233Sglebius#include <sys/priv.h>
77229850Sglebius#include <sys/protosw.h>
78240233Sglebius#include <sys/socket.h>
79240233Sglebius#include <sys/sockio.h>
80223637Sbz#include <sys/sysctl.h>
81126258Smlaier
82240233Sglebius#include <net/bpf.h>
83126258Smlaier#include <net/if.h>
84130933Sbrooks#include <net/if_clone.h>
85126258Smlaier#include <net/if_types.h>
86240233Sglebius#include <net/pfvar.h>
87240233Sglebius#include <net/if_pfsync.h>
88223637Sbz
89240233Sglebius#include <netinet/if_ether.h>
90171168Smlaier#include <netinet/in.h>
91126258Smlaier#include <netinet/in_var.h>
92130613Smlaier#include <netinet/ip.h>
93240233Sglebius#include <netinet/ip_carp.h>
94130613Smlaier#include <netinet/ip_var.h>
95240233Sglebius#include <netinet/tcp.h>
96240233Sglebius#include <netinet/tcp_fsm.h>
97240233Sglebius#include <netinet/tcp_seq.h>
98126258Smlaier
99223637Sbz#define PFSYNC_MINPKT ( \
100223637Sbz	sizeof(struct ip) + \
101223637Sbz	sizeof(struct pfsync_header) + \
102246822Sglebius	sizeof(struct pfsync_subheader) )
103126258Smlaier
104223637Sbzstruct pfsync_pkt {
105223637Sbz	struct ip *ip;
106223637Sbz	struct in_addr src;
107223637Sbz	u_int8_t flags;
108223637Sbz};
109223637Sbz
110240233Sglebiusstatic int	pfsync_upd_tcp(struct pf_state *, struct pfsync_state_peer *,
111240233Sglebius		    struct pfsync_state_peer *);
112240233Sglebiusstatic int	pfsync_in_clr(struct pfsync_pkt *, struct mbuf *, int, int);
113240233Sglebiusstatic int	pfsync_in_ins(struct pfsync_pkt *, struct mbuf *, int, int);
114240233Sglebiusstatic int	pfsync_in_iack(struct pfsync_pkt *, struct mbuf *, int, int);
115240233Sglebiusstatic int	pfsync_in_upd(struct pfsync_pkt *, struct mbuf *, int, int);
116240233Sglebiusstatic int	pfsync_in_upd_c(struct pfsync_pkt *, struct mbuf *, int, int);
117240233Sglebiusstatic int	pfsync_in_ureq(struct pfsync_pkt *, struct mbuf *, int, int);
118240233Sglebiusstatic int	pfsync_in_del(struct pfsync_pkt *, struct mbuf *, int, int);
119240233Sglebiusstatic int	pfsync_in_del_c(struct pfsync_pkt *, struct mbuf *, int, int);
120240233Sglebiusstatic int	pfsync_in_bus(struct pfsync_pkt *, struct mbuf *, int, int);
121240233Sglebiusstatic int	pfsync_in_tdb(struct pfsync_pkt *, struct mbuf *, int, int);
122240233Sglebiusstatic int	pfsync_in_eof(struct pfsync_pkt *, struct mbuf *, int, int);
123240233Sglebiusstatic int	pfsync_in_error(struct pfsync_pkt *, struct mbuf *, int, int);
124223637Sbz
125240233Sglebiusstatic int (*pfsync_acts[])(struct pfsync_pkt *, struct mbuf *, int, int) = {
126223637Sbz	pfsync_in_clr,			/* PFSYNC_ACT_CLR */
127223637Sbz	pfsync_in_ins,			/* PFSYNC_ACT_INS */
128223637Sbz	pfsync_in_iack,			/* PFSYNC_ACT_INS_ACK */
129223637Sbz	pfsync_in_upd,			/* PFSYNC_ACT_UPD */
130223637Sbz	pfsync_in_upd_c,		/* PFSYNC_ACT_UPD_C */
131223637Sbz	pfsync_in_ureq,			/* PFSYNC_ACT_UPD_REQ */
132223637Sbz	pfsync_in_del,			/* PFSYNC_ACT_DEL */
133223637Sbz	pfsync_in_del_c,		/* PFSYNC_ACT_DEL_C */
134223637Sbz	pfsync_in_error,		/* PFSYNC_ACT_INS_F */
135223637Sbz	pfsync_in_error,		/* PFSYNC_ACT_DEL_F */
136223637Sbz	pfsync_in_bus,			/* PFSYNC_ACT_BUS */
137223637Sbz	pfsync_in_tdb,			/* PFSYNC_ACT_TDB */
138223637Sbz	pfsync_in_eof			/* PFSYNC_ACT_EOF */
139223637Sbz};
140223637Sbz
141223637Sbzstruct pfsync_q {
142241056Sglebius	void		(*write)(struct pf_state *, void *);
143223637Sbz	size_t		len;
144223637Sbz	u_int8_t	action;
145223637Sbz};
146223637Sbz
147223637Sbz/* we have one of these for every PFSYNC_S_ */
148241056Sglebiusstatic void	pfsync_out_state(struct pf_state *, void *);
149241056Sglebiusstatic void	pfsync_out_iack(struct pf_state *, void *);
150241056Sglebiusstatic void	pfsync_out_upd_c(struct pf_state *, void *);
151241056Sglebiusstatic void	pfsync_out_del(struct pf_state *, void *);
152223637Sbz
153240233Sglebiusstatic struct pfsync_q pfsync_qs[] = {
154223637Sbz	{ pfsync_out_state, sizeof(struct pfsync_state),   PFSYNC_ACT_INS },
155223637Sbz	{ pfsync_out_iack,  sizeof(struct pfsync_ins_ack), PFSYNC_ACT_INS_ACK },
156223637Sbz	{ pfsync_out_state, sizeof(struct pfsync_state),   PFSYNC_ACT_UPD },
157223637Sbz	{ pfsync_out_upd_c, sizeof(struct pfsync_upd_c),   PFSYNC_ACT_UPD_C },
158223637Sbz	{ pfsync_out_del,   sizeof(struct pfsync_del_c),   PFSYNC_ACT_DEL_C }
159223637Sbz};
160223637Sbz
161240233Sglebiusstatic void	pfsync_q_ins(struct pf_state *, int);
162240233Sglebiusstatic void	pfsync_q_del(struct pf_state *);
163223637Sbz
164240233Sglebiusstatic void	pfsync_update_state(struct pf_state *);
165240233Sglebius
166223637Sbzstruct pfsync_upd_req_item {
167223637Sbz	TAILQ_ENTRY(pfsync_upd_req_item)	ur_entry;
168223637Sbz	struct pfsync_upd_req			ur_msg;
169223637Sbz};
170223637Sbz
171223637Sbzstruct pfsync_deferral {
172240233Sglebius	struct pfsync_softc		*pd_sc;
173240233Sglebius	TAILQ_ENTRY(pfsync_deferral)	pd_entry;
174240233Sglebius	u_int				pd_refs;
175240233Sglebius	struct callout			pd_tmo;
176240233Sglebius
177240233Sglebius	struct pf_state			*pd_st;
178240233Sglebius	struct mbuf			*pd_m;
179223637Sbz};
180126258Smlaier
181223637Sbzstruct pfsync_softc {
182240233Sglebius	/* Configuration */
183223637Sbz	struct ifnet		*sc_ifp;
184223637Sbz	struct ifnet		*sc_sync_if;
185240233Sglebius	struct ip_moptions	sc_imo;
186240233Sglebius	struct in_addr		sc_sync_peer;
187240233Sglebius	uint32_t		sc_flags;
188240233Sglebius#define	PFSYNCF_OK		0x00000001
189240233Sglebius#define	PFSYNCF_DEFER		0x00000002
190240233Sglebius#define	PFSYNCF_PUSH		0x00000004
191240233Sglebius	uint8_t			sc_maxupdates;
192240233Sglebius	struct ip		sc_template;
193240233Sglebius	struct callout		sc_tmo;
194240233Sglebius	struct mtx		sc_mtx;
195223637Sbz
196240233Sglebius	/* Queued data */
197240233Sglebius	size_t			sc_len;
198240233Sglebius	TAILQ_HEAD(, pf_state)			sc_qs[PFSYNC_S_COUNT];
199240233Sglebius	TAILQ_HEAD(, pfsync_upd_req_item)	sc_upd_req_list;
200240233Sglebius	TAILQ_HEAD(, pfsync_deferral)		sc_deferrals;
201240233Sglebius	u_int			sc_deferred;
202223637Sbz	void			*sc_plus;
203240233Sglebius	size_t			sc_pluslen;
204223637Sbz
205240233Sglebius	/* Bulk update info */
206240233Sglebius	struct mtx		sc_bulk_mtx;
207240233Sglebius	uint32_t		sc_ureq_sent;
208240233Sglebius	int			sc_bulk_tries;
209240233Sglebius	uint32_t		sc_ureq_received;
210240233Sglebius	int			sc_bulk_hashid;
211240233Sglebius	uint64_t		sc_bulk_stateid;
212240233Sglebius	uint32_t		sc_bulk_creatorid;
213240233Sglebius	struct callout		sc_bulk_tmo;
214240233Sglebius	struct callout		sc_bulkfail_tmo;
215240233Sglebius};
216223637Sbz
217240233Sglebius#define	PFSYNC_LOCK(sc)		mtx_lock(&(sc)->sc_mtx)
218240233Sglebius#define	PFSYNC_UNLOCK(sc)	mtx_unlock(&(sc)->sc_mtx)
219240233Sglebius#define	PFSYNC_LOCK_ASSERT(sc)	mtx_assert(&(sc)->sc_mtx, MA_OWNED)
220223637Sbz
221240233Sglebius#define	PFSYNC_BLOCK(sc)	mtx_lock(&(sc)->sc_bulk_mtx)
222240233Sglebius#define	PFSYNC_BUNLOCK(sc)	mtx_unlock(&(sc)->sc_bulk_mtx)
223240233Sglebius#define	PFSYNC_BLOCK_ASSERT(sc)	mtx_assert(&(sc)->sc_bulk_mtx, MA_OWNED)
224223637Sbz
225241610Sglebiusstatic const char pfsyncname[] = "pfsync";
226241610Sglebiusstatic MALLOC_DEFINE(M_PFSYNC, pfsyncname, "pfsync(4) data");
227223637Sbzstatic VNET_DEFINE(struct pfsync_softc	*, pfsyncif) = NULL;
228223637Sbz#define	V_pfsyncif		VNET(pfsyncif)
229229850Sglebiusstatic VNET_DEFINE(void *, pfsync_swi_cookie) = NULL;
230229850Sglebius#define	V_pfsync_swi_cookie	VNET(pfsync_swi_cookie)
231223637Sbzstatic VNET_DEFINE(struct pfsyncstats, pfsyncstats);
232223637Sbz#define	V_pfsyncstats		VNET(pfsyncstats)
233228736Sglebiusstatic VNET_DEFINE(int, pfsync_carp_adj) = CARP_MAXSKEW;
234228736Sglebius#define	V_pfsync_carp_adj	VNET(pfsync_carp_adj)
235223637Sbz
236240233Sglebiusstatic void	pfsync_timeout(void *);
237240233Sglebiusstatic void	pfsync_push(struct pfsync_softc *);
238229850Sglebiusstatic void	pfsyncintr(void *);
239240233Sglebiusstatic int	pfsync_multicast_setup(struct pfsync_softc *, struct ifnet *,
240240233Sglebius		    void *);
241229850Sglebiusstatic void	pfsync_multicast_cleanup(struct pfsync_softc *);
242241057Sglebiusstatic void	pfsync_pointers_init(void);
243241057Sglebiusstatic void	pfsync_pointers_uninit(void);
244229850Sglebiusstatic int	pfsync_init(void);
245229850Sglebiusstatic void	pfsync_uninit(void);
246229850Sglebius
247223637SbzSYSCTL_NODE(_net, OID_AUTO, pfsync, CTLFLAG_RW, 0, "PFSYNC");
248223637SbzSYSCTL_VNET_STRUCT(_net_pfsync, OID_AUTO, stats, CTLFLAG_RW,
249223637Sbz    &VNET_NAME(pfsyncstats), pfsyncstats,
250223637Sbz    "PFSYNC statistics (struct pfsyncstats, net/if_pfsync.h)");
251228736SglebiusSYSCTL_INT(_net_pfsync, OID_AUTO, carp_demotion_factor, CTLFLAG_RW,
252228736Sglebius    &VNET_NAME(pfsync_carp_adj), 0, "pfsync's CARP demotion factor adjustment");
253223637Sbz
254240233Sglebiusstatic int	pfsync_clone_create(struct if_clone *, int, caddr_t);
255240233Sglebiusstatic void	pfsync_clone_destroy(struct ifnet *);
256240233Sglebiusstatic int	pfsync_alloc_scrub_memory(struct pfsync_state_peer *,
257240233Sglebius		    struct pf_state_peer *);
258249925Sglebiusstatic int	pfsyncoutput(struct ifnet *, struct mbuf *,
259249925Sglebius		    const struct sockaddr *, struct route *);
260240233Sglebiusstatic int	pfsyncioctl(struct ifnet *, u_long, caddr_t);
261126258Smlaier
262240233Sglebiusstatic int	pfsync_defer(struct pf_state *, struct mbuf *);
263240233Sglebiusstatic void	pfsync_undefer(struct pfsync_deferral *, int);
264240233Sglebiusstatic void	pfsync_undefer_state(struct pf_state *, int);
265240233Sglebiusstatic void	pfsync_defer_tmo(void *);
266223637Sbz
267240233Sglebiusstatic void	pfsync_request_update(u_int32_t, u_int64_t);
268240233Sglebiusstatic void	pfsync_update_state_req(struct pf_state *);
269223637Sbz
270240233Sglebiusstatic void	pfsync_drop(struct pfsync_softc *);
271240233Sglebiusstatic void	pfsync_sendout(int);
272240233Sglebiusstatic void	pfsync_send_plus(void *, size_t);
273223637Sbz
274240233Sglebiusstatic void	pfsync_bulk_start(void);
275240233Sglebiusstatic void	pfsync_bulk_status(u_int8_t);
276240233Sglebiusstatic void	pfsync_bulk_update(void *);
277240233Sglebiusstatic void	pfsync_bulk_fail(void *);
278223637Sbz
279240233Sglebius#ifdef IPSEC
280240233Sglebiusstatic void	pfsync_update_net_tdb(struct pfsync_tdb *);
281167710Sbms#endif
282126258Smlaier
283223637Sbz#define PFSYNC_MAX_BULKTRIES	12
284126258Smlaier
285241610SglebiusVNET_DEFINE(struct if_clone *, pfsync_cloner);
286241610Sglebius#define	V_pfsync_cloner	VNET(pfsync_cloner)
287126261Smlaier
288240233Sglebiusstatic int
289171168Smlaierpfsync_clone_create(struct if_clone *ifc, int unit, caddr_t param)
290126261Smlaier{
291223637Sbz	struct pfsync_softc *sc;
292130613Smlaier	struct ifnet *ifp;
293223637Sbz	int q;
294126261Smlaier
295171168Smlaier	if (unit != 0)
296171168Smlaier		return (EINVAL);
297171168Smlaier
298229850Sglebius	sc = malloc(sizeof(struct pfsync_softc), M_PFSYNC, M_WAITOK | M_ZERO);
299240233Sglebius	sc->sc_flags |= PFSYNCF_OK;
300223637Sbz
301223637Sbz	for (q = 0; q < PFSYNC_S_COUNT; q++)
302223637Sbz		TAILQ_INIT(&sc->sc_qs[q]);
303223637Sbz
304223637Sbz	TAILQ_INIT(&sc->sc_upd_req_list);
305223637Sbz	TAILQ_INIT(&sc->sc_deferrals);
306171168Smlaier
307223637Sbz	sc->sc_len = PFSYNC_MINPKT;
308223637Sbz	sc->sc_maxupdates = 128;
309223637Sbz
310223637Sbz	ifp = sc->sc_ifp = if_alloc(IFT_PFSYNC);
311147256Sbrooks	if (ifp == NULL) {
312229850Sglebius		free(sc, M_PFSYNC);
313147256Sbrooks		return (ENOSPC);
314147256Sbrooks	}
315241610Sglebius	if_initname(ifp, pfsyncname, unit);
316223637Sbz	ifp->if_softc = sc;
317130613Smlaier	ifp->if_ioctl = pfsyncioctl;
318130613Smlaier	ifp->if_output = pfsyncoutput;
319171168Smlaier	ifp->if_type = IFT_PFSYNC;
320130613Smlaier	ifp->if_snd.ifq_maxlen = ifqmaxlen;
321223637Sbz	ifp->if_hdrlen = sizeof(struct pfsync_header);
322229777Sglebius	ifp->if_mtu = ETHERMTU;
323241610Sglebius	mtx_init(&sc->sc_mtx, pfsyncname, NULL, MTX_DEF);
324240233Sglebius	mtx_init(&sc->sc_bulk_mtx, "pfsync bulk", NULL, MTX_DEF);
325314667Savg	callout_init(&sc->sc_tmo, 1);
326240233Sglebius	callout_init_mtx(&sc->sc_bulk_tmo, &sc->sc_bulk_mtx, 0);
327240233Sglebius	callout_init_mtx(&sc->sc_bulkfail_tmo, &sc->sc_bulk_mtx, 0);
328223637Sbz
329141584Smlaier	if_attach(ifp);
330126261Smlaier
331141584Smlaier	bpfattach(ifp, DLT_PFSYNC, PFSYNC_HDRLEN);
332126261Smlaier
333223637Sbz	V_pfsyncif = sc;
334223637Sbz
335126261Smlaier	return (0);
336126261Smlaier}
337171168Smlaier
338240233Sglebiusstatic void
339171168Smlaierpfsync_clone_destroy(struct ifnet *ifp)
340126258Smlaier{
341223637Sbz	struct pfsync_softc *sc = ifp->if_softc;
342223637Sbz
343240233Sglebius	/*
344240233Sglebius	 * At this stage, everything should have already been
345240233Sglebius	 * cleared by pfsync_uninit(), and we have only to
346240233Sglebius	 * drain callouts.
347240233Sglebius	 */
348240233Sglebius	while (sc->sc_deferred > 0) {
349240233Sglebius		struct pfsync_deferral *pd = TAILQ_FIRST(&sc->sc_deferrals);
350240233Sglebius
351240233Sglebius		TAILQ_REMOVE(&sc->sc_deferrals, pd, pd_entry);
352240233Sglebius		sc->sc_deferred--;
353240233Sglebius		if (callout_stop(&pd->pd_tmo)) {
354240233Sglebius			pf_release_state(pd->pd_st);
355240233Sglebius			m_freem(pd->pd_m);
356240233Sglebius			free(pd, M_PFSYNC);
357240233Sglebius		} else {
358240233Sglebius			pd->pd_refs++;
359240233Sglebius			callout_drain(&pd->pd_tmo);
360240233Sglebius			free(pd, M_PFSYNC);
361240233Sglebius		}
362240233Sglebius	}
363240233Sglebius
364240233Sglebius	callout_drain(&sc->sc_tmo);
365240233Sglebius	callout_drain(&sc->sc_bulkfail_tmo);
366240233Sglebius	callout_drain(&sc->sc_bulk_tmo);
367240233Sglebius
368240233Sglebius	if (!(sc->sc_flags & PFSYNCF_OK) && carp_demote_adj_p)
369228736Sglebius		(*carp_demote_adj_p)(-V_pfsync_carp_adj, "pfsync destroy");
370171168Smlaier	bpfdetach(ifp);
371171168Smlaier	if_detach(ifp);
372223637Sbz
373223637Sbz	pfsync_drop(sc);
374223637Sbz
375171168Smlaier	if_free(ifp);
376229850Sglebius	if (sc->sc_imo.imo_membership)
377229850Sglebius		pfsync_multicast_cleanup(sc);
378240233Sglebius	mtx_destroy(&sc->sc_mtx);
379240233Sglebius	mtx_destroy(&sc->sc_bulk_mtx);
380229850Sglebius	free(sc, M_PFSYNC);
381223637Sbz
382223637Sbz	V_pfsyncif = NULL;
383126258Smlaier}
384126258Smlaier
385240233Sglebiusstatic int
386171168Smlaierpfsync_alloc_scrub_memory(struct pfsync_state_peer *s,
387171168Smlaier    struct pf_state_peer *d)
388130613Smlaier{
389171168Smlaier	if (s->scrub.scrub_flag && d->scrub == NULL) {
390240233Sglebius		d->scrub = uma_zalloc(V_pf_state_scrub_z, M_NOWAIT | M_ZERO);
391171168Smlaier		if (d->scrub == NULL)
392171168Smlaier			return (ENOMEM);
393171168Smlaier	}
394171168Smlaier
395171168Smlaier	return (0);
396171168Smlaier}
397171168Smlaier
398223637Sbz
399240233Sglebiusstatic int
400223637Sbzpfsync_state_import(struct pfsync_state *sp, u_int8_t flags)
401171168Smlaier{
402240233Sglebius	struct pfsync_softc *sc = V_pfsyncif;
403270328Sglebius#ifndef	__NO_STRICT_ALIGNMENT
404270328Sglebius	struct pfsync_state_key key[2];
405270328Sglebius#endif
406270328Sglebius	struct pfsync_state_key *kw, *ks;
407130613Smlaier	struct pf_state	*st = NULL;
408223637Sbz	struct pf_state_key *skw = NULL, *sks = NULL;
409130613Smlaier	struct pf_rule *r = NULL;
410130613Smlaier	struct pfi_kif	*kif;
411223637Sbz	int error;
412130613Smlaier
413240233Sglebius	PF_RULES_RASSERT();
414226544Sbz
415250518Sglebius	if (sp->creatorid == 0) {
416250518Sglebius		if (V_pf_status.debug >= PF_DEBUG_MISC)
417250518Sglebius			printf("%s: invalid creator id: %08x\n", __func__,
418250518Sglebius			    ntohl(sp->creatorid));
419130613Smlaier		return (EINVAL);
420130613Smlaier	}
421130613Smlaier
422240233Sglebius	if ((kif = pfi_kif_find(sp->ifname)) == NULL) {
423223637Sbz		if (V_pf_status.debug >= PF_DEBUG_MISC)
424240233Sglebius			printf("%s: unknown interface: %s\n", __func__,
425240233Sglebius			    sp->ifname);
426223637Sbz		if (flags & PFSYNC_SI_IOCTL)
427223637Sbz			return (EINVAL);
428223637Sbz		return (0);	/* skip this state */
429130613Smlaier	}
430130613Smlaier
431130613Smlaier	/*
432223637Sbz	 * If the ruleset checksums match or the state is coming from the ioctl,
433223637Sbz	 * it's safe to associate the state with the rule of that number.
434130613Smlaier	 */
435223637Sbz	if (sp->rule != htonl(-1) && sp->anchor == htonl(-1) &&
436223637Sbz	    (flags & (PFSYNC_SI_IOCTL | PFSYNC_SI_CKSUM)) && ntohl(sp->rule) <
437223637Sbz	    pf_main_ruleset.rules[PF_RULESET_FILTER].active.rcount)
438171168Smlaier		r = pf_main_ruleset.rules[
439171168Smlaier		    PF_RULESET_FILTER].active.ptr_array[ntohl(sp->rule)];
440171168Smlaier	else
441223637Sbz		r = &V_pf_default_rule;
442130613Smlaier
443263029Sglebius	if ((r->max_states &&
444263029Sglebius	    counter_u64_fetch(r->states_cur) >= r->max_states))
445223637Sbz		goto cleanup;
446130613Smlaier
447240233Sglebius	/*
448240233Sglebius	 * XXXGL: consider M_WAITOK in ioctl path after.
449240233Sglebius	 */
450240233Sglebius	if ((st = uma_zalloc(V_pf_state_z, M_NOWAIT | M_ZERO)) == NULL)
451223637Sbz		goto cleanup;
452130613Smlaier
453240233Sglebius	if ((skw = uma_zalloc(V_pf_state_key_z, M_NOWAIT)) == NULL)
454223637Sbz		goto cleanup;
455145836Smlaier
456270328Sglebius#ifndef	__NO_STRICT_ALIGNMENT
457270328Sglebius	bcopy(&sp->key, key, sizeof(struct pfsync_state_key) * 2);
458270328Sglebius	kw = &key[PF_SK_WIRE];
459270328Sglebius	ks = &key[PF_SK_STACK];
460270328Sglebius#else
461270328Sglebius	kw = &sp->key[PF_SK_WIRE];
462270328Sglebius	ks = &sp->key[PF_SK_STACK];
463270328Sglebius#endif
464270328Sglebius
465270328Sglebius	if (PF_ANEQ(&kw->addr[0], &ks->addr[0], sp->af) ||
466270328Sglebius	    PF_ANEQ(&kw->addr[1], &ks->addr[1], sp->af) ||
467270328Sglebius	    kw->port[0] != ks->port[0] ||
468270328Sglebius	    kw->port[1] != ks->port[1]) {
469240233Sglebius		sks = uma_zalloc(V_pf_state_key_z, M_NOWAIT);
470240233Sglebius		if (sks == NULL)
471223637Sbz			goto cleanup;
472223637Sbz	} else
473223637Sbz		sks = skw;
474130613Smlaier
475223637Sbz	/* allocate memory for scrub info */
476223637Sbz	if (pfsync_alloc_scrub_memory(&sp->src, &st->src) ||
477223637Sbz	    pfsync_alloc_scrub_memory(&sp->dst, &st->dst))
478223637Sbz		goto cleanup;
479223637Sbz
480270328Sglebius	/* Copy to state key(s). */
481270328Sglebius	skw->addr[0] = kw->addr[0];
482270328Sglebius	skw->addr[1] = kw->addr[1];
483270328Sglebius	skw->port[0] = kw->port[0];
484270328Sglebius	skw->port[1] = kw->port[1];
485223637Sbz	skw->proto = sp->proto;
486223637Sbz	skw->af = sp->af;
487223637Sbz	if (sks != skw) {
488270328Sglebius		sks->addr[0] = ks->addr[0];
489270328Sglebius		sks->addr[1] = ks->addr[1];
490270328Sglebius		sks->port[0] = ks->port[0];
491270328Sglebius		sks->port[1] = ks->port[1];
492223637Sbz		sks->proto = sp->proto;
493223637Sbz		sks->af = sp->af;
494223637Sbz	}
495223637Sbz
496223637Sbz	/* copy to state */
497130613Smlaier	bcopy(&sp->rt_addr, &st->rt_addr, sizeof(st->rt_addr));
498228815Sglebius	st->creation = time_uptime - ntohl(sp->creation);
499240233Sglebius	st->expire = time_uptime;
500223637Sbz	if (sp->expire) {
501233874Sglebius		uint32_t timeout;
502233874Sglebius
503233874Sglebius		timeout = r->timeout[sp->timeout];
504233874Sglebius		if (!timeout)
505233917Sae			timeout = V_pf_default_rule.timeout[sp->timeout];
506233874Sglebius
507233874Sglebius		/* sp->expire may have been adaptively scaled by export. */
508233874Sglebius		st->expire -= timeout - ntohl(sp->expire);
509223637Sbz	}
510223637Sbz
511130613Smlaier	st->direction = sp->direction;
512130613Smlaier	st->log = sp->log;
513130613Smlaier	st->timeout = sp->timeout;
514200930Sdelphij	st->state_flags = sp->state_flags;
515130613Smlaier
516240233Sglebius	st->id = sp->id;
517130613Smlaier	st->creatorid = sp->creatorid;
518223637Sbz	pf_state_peer_ntoh(&sp->src, &st->src);
519223637Sbz	pf_state_peer_ntoh(&sp->dst, &st->dst);
520130613Smlaier
521223637Sbz	st->rule.ptr = r;
522223637Sbz	st->nat_rule.ptr = NULL;
523223637Sbz	st->anchor.ptr = NULL;
524223637Sbz	st->rt_kif = NULL;
525223637Sbz
526228815Sglebius	st->pfsync_time = time_uptime;
527223637Sbz	st->sync_state = PFSYNC_S_NONE;
528223637Sbz
529240233Sglebius	if (!(flags & PFSYNC_SI_IOCTL))
530240233Sglebius		st->state_flags |= PFSTATE_NOSYNC;
531223637Sbz
532263029Sglebius	if ((error = pf_state_insert(kif, skw, sks, st)) != 0)
533223637Sbz		goto cleanup_state;
534223637Sbz
535263029Sglebius	/* XXX when we have nat_rule/anchors, use STATE_INC_COUNTERS */
536263029Sglebius	counter_u64_add(r->states_cur, 1);
537263029Sglebius	counter_u64_add(r->states_tot, 1);
538263029Sglebius
539240233Sglebius	if (!(flags & PFSYNC_SI_IOCTL)) {
540240233Sglebius		st->state_flags &= ~PFSTATE_NOSYNC;
541240233Sglebius		if (st->state_flags & PFSTATE_ACK) {
542223637Sbz			pfsync_q_ins(st, PFSYNC_S_IACK);
543240233Sglebius			pfsync_push(sc);
544223637Sbz		}
545223637Sbz	}
546240233Sglebius	st->state_flags &= ~PFSTATE_ACK;
547240233Sglebius	PF_STATE_UNLOCK(st);
548223637Sbz
549223637Sbz	return (0);
550223637Sbz
551223637Sbzcleanup:
552223637Sbz	error = ENOMEM;
553223637Sbz	if (skw == sks)
554223637Sbz		sks = NULL;
555223637Sbz	if (skw != NULL)
556240233Sglebius		uma_zfree(V_pf_state_key_z, skw);
557223637Sbz	if (sks != NULL)
558240233Sglebius		uma_zfree(V_pf_state_key_z, sks);
559223637Sbz
560240233Sglebiuscleanup_state:	/* pf_state_insert() frees the state keys. */
561223637Sbz	if (st) {
562171168Smlaier		if (st->dst.scrub)
563240233Sglebius			uma_zfree(V_pf_state_scrub_z, st->dst.scrub);
564223637Sbz		if (st->src.scrub)
565240233Sglebius			uma_zfree(V_pf_state_scrub_z, st->src.scrub);
566240233Sglebius		uma_zfree(V_pf_state_z, st);
567130613Smlaier	}
568223637Sbz	return (error);
569130613Smlaier}
570130613Smlaier
571240233Sglebiusstatic void
572130613Smlaierpfsync_input(struct mbuf *m, __unused int off)
573130613Smlaier{
574223637Sbz	struct pfsync_softc *sc = V_pfsyncif;
575223637Sbz	struct pfsync_pkt pkt;
576130613Smlaier	struct ip *ip = mtod(m, struct ip *);
577130613Smlaier	struct pfsync_header *ph;
578223637Sbz	struct pfsync_subheader subh;
579130613Smlaier
580244185Sglebius	int offset, len;
581223637Sbz	int rv;
582240233Sglebius	uint16_t count;
583130613Smlaier
584223637Sbz	V_pfsyncstats.pfsyncs_ipackets++;
585223637Sbz
586240233Sglebius	/* Verify that we have a sync interface configured. */
587240233Sglebius	if (!sc || !sc->sc_sync_if || !V_pf_status.running ||
588240233Sglebius	    (sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
589130613Smlaier		goto done;
590130613Smlaier
591130613Smlaier	/* verify that the packet came in on the right interface */
592223637Sbz	if (sc->sc_sync_if != m->m_pkthdr.rcvif) {
593223637Sbz		V_pfsyncstats.pfsyncs_badif++;
594130613Smlaier		goto done;
595130613Smlaier	}
596130613Smlaier
597223637Sbz	sc->sc_ifp->if_ipackets++;
598223637Sbz	sc->sc_ifp->if_ibytes += m->m_pkthdr.len;
599223637Sbz	/* verify that the IP TTL is 255. */
600130613Smlaier	if (ip->ip_ttl != PFSYNC_DFLTTL) {
601223637Sbz		V_pfsyncstats.pfsyncs_badttl++;
602130613Smlaier		goto done;
603130613Smlaier	}
604130613Smlaier
605223637Sbz	offset = ip->ip_hl << 2;
606223637Sbz	if (m->m_pkthdr.len < offset + sizeof(*ph)) {
607223637Sbz		V_pfsyncstats.pfsyncs_hdrops++;
608130613Smlaier		goto done;
609130613Smlaier	}
610130613Smlaier
611223637Sbz	if (offset + sizeof(*ph) > m->m_len) {
612223637Sbz		if (m_pullup(m, offset + sizeof(*ph)) == NULL) {
613223637Sbz			V_pfsyncstats.pfsyncs_hdrops++;
614223637Sbz			return;
615130613Smlaier		}
616130613Smlaier		ip = mtod(m, struct ip *);
617130613Smlaier	}
618223637Sbz	ph = (struct pfsync_header *)((char *)ip + offset);
619130613Smlaier
620130613Smlaier	/* verify the version */
621130613Smlaier	if (ph->version != PFSYNC_VERSION) {
622223637Sbz		V_pfsyncstats.pfsyncs_badver++;
623130613Smlaier		goto done;
624130613Smlaier	}
625130613Smlaier
626244185Sglebius	len = ntohs(ph->len) + offset;
627244185Sglebius	if (m->m_pkthdr.len < len) {
628244202Sglebius		V_pfsyncstats.pfsyncs_badlen++;
629244185Sglebius		goto done;
630244185Sglebius	}
631244185Sglebius
632130613Smlaier	/* Cheaper to grab this now than having to mess with mbufs later */
633223637Sbz	pkt.ip = ip;
634223637Sbz	pkt.src = ip->ip_src;
635223637Sbz	pkt.flags = 0;
636130613Smlaier
637240233Sglebius	/*
638240233Sglebius	 * Trusting pf_chksum during packet processing, as well as seeking
639240233Sglebius	 * in interface name tree, require holding PF_RULES_RLOCK().
640240233Sglebius	 */
641240233Sglebius	PF_RULES_RLOCK();
642223637Sbz	if (!bcmp(&ph->pfcksum, &V_pf_status.pf_chksum, PF_MD5_DIGEST_LENGTH))
643223637Sbz		pkt.flags |= PFSYNC_SI_CKSUM;
644171168Smlaier
645223637Sbz	offset += sizeof(*ph);
646244185Sglebius	while (offset <= len - sizeof(subh)) {
647223637Sbz		m_copydata(m, offset, sizeof(subh), (caddr_t)&subh);
648223637Sbz		offset += sizeof(subh);
649223637Sbz
650223637Sbz		if (subh.action >= PFSYNC_ACT_MAX) {
651223637Sbz			V_pfsyncstats.pfsyncs_badact++;
652240233Sglebius			PF_RULES_RUNLOCK();
653223637Sbz			goto done;
654130613Smlaier		}
655130613Smlaier
656240233Sglebius		count = ntohs(subh.count);
657240233Sglebius		V_pfsyncstats.pfsyncs_iacts[subh.action] += count;
658240233Sglebius		rv = (*pfsync_acts[subh.action])(&pkt, m, offset, count);
659240233Sglebius		if (rv == -1) {
660240233Sglebius			PF_RULES_RUNLOCK();
661223637Sbz			return;
662240233Sglebius		}
663223637Sbz
664223637Sbz		offset += rv;
665223637Sbz	}
666240233Sglebius	PF_RULES_RUNLOCK();
667223637Sbz
668223637Sbzdone:
669223637Sbz	m_freem(m);
670223637Sbz}
671223637Sbz
672240233Sglebiusstatic int
673223637Sbzpfsync_in_clr(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
674223637Sbz{
675223637Sbz	struct pfsync_clr *clr;
676223637Sbz	struct mbuf *mp;
677223637Sbz	int len = sizeof(*clr) * count;
678223637Sbz	int i, offp;
679223637Sbz	u_int32_t creatorid;
680223637Sbz
681223637Sbz	mp = m_pulldown(m, offset, len, &offp);
682223637Sbz	if (mp == NULL) {
683223637Sbz		V_pfsyncstats.pfsyncs_badlen++;
684223637Sbz		return (-1);
685223637Sbz	}
686223637Sbz	clr = (struct pfsync_clr *)(mp->m_data + offp);
687223637Sbz
688223637Sbz	for (i = 0; i < count; i++) {
689223637Sbz		creatorid = clr[i].creatorid;
690223637Sbz
691240233Sglebius		if (clr[i].ifname[0] != '\0' &&
692240233Sglebius		    pfi_kif_find(clr[i].ifname) == NULL)
693240233Sglebius			continue;
694223637Sbz
695273736Shselasky		for (int i = 0; i <= pf_hashmask; i++) {
696240233Sglebius			struct pf_idhash *ih = &V_pf_idhash[i];
697240233Sglebius			struct pf_state *s;
698240233Sglebiusrelock:
699240233Sglebius			PF_HASHROW_LOCK(ih);
700240233Sglebius			LIST_FOREACH(s, &ih->states, entry) {
701240233Sglebius				if (s->creatorid == creatorid) {
702240233Sglebius					s->state_flags |= PFSTATE_NOSYNC;
703240233Sglebius					pf_unlink_state(s, PF_ENTER_LOCKED);
704240233Sglebius					goto relock;
705145836Smlaier				}
706130613Smlaier			}
707240233Sglebius			PF_HASHROW_UNLOCK(ih);
708130613Smlaier		}
709223637Sbz	}
710130613Smlaier
711223637Sbz	return (len);
712223637Sbz}
713223637Sbz
714240233Sglebiusstatic int
715223637Sbzpfsync_in_ins(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
716223637Sbz{
717223637Sbz	struct mbuf *mp;
718223637Sbz	struct pfsync_state *sa, *sp;
719223637Sbz	int len = sizeof(*sp) * count;
720223637Sbz	int i, offp;
721223637Sbz
722223637Sbz	mp = m_pulldown(m, offset, len, &offp);
723223637Sbz	if (mp == NULL) {
724223637Sbz		V_pfsyncstats.pfsyncs_badlen++;
725223637Sbz		return (-1);
726130613Smlaier	}
727223637Sbz	sa = (struct pfsync_state *)(mp->m_data + offp);
728130613Smlaier
729223637Sbz	for (i = 0; i < count; i++) {
730223637Sbz		sp = &sa[i];
731130613Smlaier
732240233Sglebius		/* Check for invalid values. */
733223637Sbz		if (sp->timeout >= PFTM_MAX ||
734223637Sbz		    sp->src.state > PF_TCPS_PROXY_DST ||
735223637Sbz		    sp->dst.state > PF_TCPS_PROXY_DST ||
736223637Sbz		    sp->direction > PF_OUT ||
737223637Sbz		    (sp->af != AF_INET && sp->af != AF_INET6)) {
738240233Sglebius			if (V_pf_status.debug >= PF_DEBUG_MISC)
739240233Sglebius				printf("%s: invalid value\n", __func__);
740223637Sbz			V_pfsyncstats.pfsyncs_badval++;
741223637Sbz			continue;
742130613Smlaier		}
743223637Sbz
744240233Sglebius		if (pfsync_state_import(sp, pkt->flags) == ENOMEM)
745240233Sglebius			/* Drop out, but process the rest of the actions. */
746223637Sbz			break;
747223637Sbz	}
748130613Smlaier
749223637Sbz	return (len);
750223637Sbz}
751223637Sbz
752240233Sglebiusstatic int
753223637Sbzpfsync_in_iack(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
754223637Sbz{
755223637Sbz	struct pfsync_ins_ack *ia, *iaa;
756223637Sbz	struct pf_state *st;
757223637Sbz
758223637Sbz	struct mbuf *mp;
759223637Sbz	int len = count * sizeof(*ia);
760223637Sbz	int offp, i;
761223637Sbz
762223637Sbz	mp = m_pulldown(m, offset, len, &offp);
763223637Sbz	if (mp == NULL) {
764223637Sbz		V_pfsyncstats.pfsyncs_badlen++;
765223637Sbz		return (-1);
766223637Sbz	}
767223637Sbz	iaa = (struct pfsync_ins_ack *)(mp->m_data + offp);
768223637Sbz
769223637Sbz	for (i = 0; i < count; i++) {
770223637Sbz		ia = &iaa[i];
771145836Smlaier
772240233Sglebius		st = pf_find_state_byid(ia->id, ia->creatorid);
773223637Sbz		if (st == NULL)
774223637Sbz			continue;
775130613Smlaier
776240233Sglebius		if (st->state_flags & PFSTATE_ACK) {
777240233Sglebius			PFSYNC_LOCK(V_pfsyncif);
778240233Sglebius			pfsync_undefer_state(st, 0);
779240233Sglebius			PFSYNC_UNLOCK(V_pfsyncif);
780240233Sglebius		}
781240233Sglebius		PF_STATE_UNLOCK(st);
782223637Sbz	}
783130613Smlaier	/*
784223637Sbz	 * XXX this is not yet implemented, but we know the size of the
785223637Sbz	 * message so we can skip it.
786130613Smlaier	 */
787130613Smlaier
788223637Sbz	return (count * sizeof(struct pfsync_ins_ack));
789223637Sbz}
790223637Sbz
791240233Sglebiusstatic int
792223637Sbzpfsync_upd_tcp(struct pf_state *st, struct pfsync_state_peer *src,
793223637Sbz    struct pfsync_state_peer *dst)
794223637Sbz{
795242694Sglebius	int sync = 0;
796223637Sbz
797240233Sglebius	PF_STATE_LOCK_ASSERT(st);
798240233Sglebius
799223637Sbz	/*
800223637Sbz	 * The state should never go backwards except
801223637Sbz	 * for syn-proxy states.  Neither should the
802223637Sbz	 * sequence window slide backwards.
803223637Sbz	 */
804242694Sglebius	if ((st->src.state > src->state &&
805223637Sbz	    (st->src.state < PF_TCPS_PROXY_SRC ||
806242694Sglebius	    src->state >= PF_TCPS_PROXY_SRC)) ||
807244113Sglebius
808244113Sglebius	    (st->src.state == src->state &&
809244113Sglebius	    SEQ_GT(st->src.seqlo, ntohl(src->seqlo))))
810242694Sglebius		sync++;
811242694Sglebius	else
812223637Sbz		pf_state_peer_ntoh(src, &st->src);
813223637Sbz
814244113Sglebius	if ((st->dst.state > dst->state) ||
815244113Sglebius
816242694Sglebius	    (st->dst.state >= TCPS_SYN_SENT &&
817242694Sglebius	    SEQ_GT(st->dst.seqlo, ntohl(dst->seqlo))))
818242694Sglebius		sync++;
819242694Sglebius	else
820242694Sglebius		pf_state_peer_ntoh(dst, &st->dst);
821242694Sglebius
822242694Sglebius	return (sync);
823223637Sbz}
824223637Sbz
825240233Sglebiusstatic int
826223637Sbzpfsync_in_upd(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
827223637Sbz{
828240233Sglebius	struct pfsync_softc *sc = V_pfsyncif;
829223637Sbz	struct pfsync_state *sa, *sp;
830223637Sbz	struct pf_state *st;
831242694Sglebius	int sync;
832223637Sbz
833223637Sbz	struct mbuf *mp;
834223637Sbz	int len = count * sizeof(*sp);
835223637Sbz	int offp, i;
836223637Sbz
837223637Sbz	mp = m_pulldown(m, offset, len, &offp);
838223637Sbz	if (mp == NULL) {
839223637Sbz		V_pfsyncstats.pfsyncs_badlen++;
840223637Sbz		return (-1);
841223637Sbz	}
842223637Sbz	sa = (struct pfsync_state *)(mp->m_data + offp);
843223637Sbz
844223637Sbz	for (i = 0; i < count; i++) {
845223637Sbz		sp = &sa[i];
846130613Smlaier
847223637Sbz		/* check for invalid values */
848223637Sbz		if (sp->timeout >= PFTM_MAX ||
849223637Sbz		    sp->src.state > PF_TCPS_PROXY_DST ||
850223637Sbz		    sp->dst.state > PF_TCPS_PROXY_DST) {
851223637Sbz			if (V_pf_status.debug >= PF_DEBUG_MISC) {
852223637Sbz				printf("pfsync_input: PFSYNC_ACT_UPD: "
853223637Sbz				    "invalid value\n");
854130613Smlaier			}
855223637Sbz			V_pfsyncstats.pfsyncs_badval++;
856223637Sbz			continue;
857130613Smlaier		}
858223637Sbz
859240233Sglebius		st = pf_find_state_byid(sp->id, sp->creatorid);
860223637Sbz		if (st == NULL) {
861223637Sbz			/* insert the update */
862223637Sbz			if (pfsync_state_import(sp, 0))
863223637Sbz				V_pfsyncstats.pfsyncs_badstate++;
864223637Sbz			continue;
865223637Sbz		}
866223637Sbz
867240233Sglebius		if (st->state_flags & PFSTATE_ACK) {
868240233Sglebius			PFSYNC_LOCK(sc);
869240233Sglebius			pfsync_undefer_state(st, 1);
870240233Sglebius			PFSYNC_UNLOCK(sc);
871240233Sglebius		}
872223637Sbz
873242694Sglebius		if (st->key[PF_SK_WIRE]->proto == IPPROTO_TCP)
874242694Sglebius			sync = pfsync_upd_tcp(st, &sp->src, &sp->dst);
875223637Sbz		else {
876242694Sglebius			sync = 0;
877242694Sglebius
878223637Sbz			/*
879223637Sbz			 * Non-TCP protocol state machine always go
880223637Sbz			 * forwards
881223637Sbz			 */
882223637Sbz			if (st->src.state > sp->src.state)
883242694Sglebius				sync++;
884242694Sglebius			else
885242694Sglebius				pf_state_peer_ntoh(&sp->src, &st->src);
886242694Sglebius			if (st->dst.state > sp->dst.state)
887242694Sglebius				sync++;
888242694Sglebius			else
889242694Sglebius				pf_state_peer_ntoh(&sp->dst, &st->dst);
890223637Sbz		}
891242694Sglebius		if (sync < 2) {
892242694Sglebius			pfsync_alloc_scrub_memory(&sp->dst, &st->dst);
893242694Sglebius			pf_state_peer_ntoh(&sp->dst, &st->dst);
894242694Sglebius			st->expire = time_uptime;
895242694Sglebius			st->timeout = sp->timeout;
896242694Sglebius		}
897242694Sglebius		st->pfsync_time = time_uptime;
898223637Sbz
899242694Sglebius		if (sync) {
900223637Sbz			V_pfsyncstats.pfsyncs_stale++;
901130613Smlaier
902223637Sbz			pfsync_update_state(st);
903240233Sglebius			PF_STATE_UNLOCK(st);
904240233Sglebius			PFSYNC_LOCK(sc);
905240233Sglebius			pfsync_push(sc);
906240233Sglebius			PFSYNC_UNLOCK(sc);
907223637Sbz			continue;
908130613Smlaier		}
909240233Sglebius		PF_STATE_UNLOCK(st);
910223637Sbz	}
911130613Smlaier
912223637Sbz	return (len);
913223637Sbz}
914223637Sbz
915240233Sglebiusstatic int
916223637Sbzpfsync_in_upd_c(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
917223637Sbz{
918240233Sglebius	struct pfsync_softc *sc = V_pfsyncif;
919223637Sbz	struct pfsync_upd_c *ua, *up;
920223637Sbz	struct pf_state *st;
921223637Sbz	int len = count * sizeof(*up);
922242694Sglebius	int sync;
923223637Sbz	struct mbuf *mp;
924223637Sbz	int offp, i;
925223637Sbz
926223637Sbz	mp = m_pulldown(m, offset, len, &offp);
927223637Sbz	if (mp == NULL) {
928223637Sbz		V_pfsyncstats.pfsyncs_badlen++;
929223637Sbz		return (-1);
930223637Sbz	}
931223637Sbz	ua = (struct pfsync_upd_c *)(mp->m_data + offp);
932223637Sbz
933223637Sbz	for (i = 0; i < count; i++) {
934223637Sbz		up = &ua[i];
935223637Sbz
936223637Sbz		/* check for invalid values */
937223637Sbz		if (up->timeout >= PFTM_MAX ||
938223637Sbz		    up->src.state > PF_TCPS_PROXY_DST ||
939223637Sbz		    up->dst.state > PF_TCPS_PROXY_DST) {
940223637Sbz			if (V_pf_status.debug >= PF_DEBUG_MISC) {
941223637Sbz				printf("pfsync_input: "
942223637Sbz				    "PFSYNC_ACT_UPD_C: "
943223637Sbz				    "invalid value\n");
944130613Smlaier			}
945223637Sbz			V_pfsyncstats.pfsyncs_badval++;
946223637Sbz			continue;
947223637Sbz		}
948130613Smlaier
949240233Sglebius		st = pf_find_state_byid(up->id, up->creatorid);
950223637Sbz		if (st == NULL) {
951223637Sbz			/* We don't have this state. Ask for it. */
952240233Sglebius			PFSYNC_LOCK(sc);
953240233Sglebius			pfsync_request_update(up->creatorid, up->id);
954240233Sglebius			PFSYNC_UNLOCK(sc);
955223637Sbz			continue;
956223637Sbz		}
957223637Sbz
958240233Sglebius		if (st->state_flags & PFSTATE_ACK) {
959240233Sglebius			PFSYNC_LOCK(sc);
960240233Sglebius			pfsync_undefer_state(st, 1);
961240233Sglebius			PFSYNC_UNLOCK(sc);
962240233Sglebius		}
963223637Sbz
964242694Sglebius		if (st->key[PF_SK_WIRE]->proto == IPPROTO_TCP)
965242694Sglebius			sync = pfsync_upd_tcp(st, &up->src, &up->dst);
966223637Sbz		else {
967242694Sglebius			sync = 0;
968242694Sglebius
969223637Sbz			/*
970242694Sglebius			 * Non-TCP protocol state machine always go
971242694Sglebius			 * forwards
972223637Sbz			 */
973223637Sbz			if (st->src.state > up->src.state)
974242694Sglebius				sync++;
975242694Sglebius			else
976242694Sglebius				pf_state_peer_ntoh(&up->src, &st->src);
977242694Sglebius			if (st->dst.state > up->dst.state)
978242694Sglebius				sync++;
979242694Sglebius			else
980242694Sglebius				pf_state_peer_ntoh(&up->dst, &st->dst);
981223637Sbz		}
982242694Sglebius		if (sync < 2) {
983242694Sglebius			pfsync_alloc_scrub_memory(&up->dst, &st->dst);
984242694Sglebius			pf_state_peer_ntoh(&up->dst, &st->dst);
985242694Sglebius			st->expire = time_uptime;
986242694Sglebius			st->timeout = up->timeout;
987242694Sglebius		}
988242694Sglebius		st->pfsync_time = time_uptime;
989223637Sbz
990242694Sglebius		if (sync) {
991223637Sbz			V_pfsyncstats.pfsyncs_stale++;
992145836Smlaier
993223637Sbz			pfsync_update_state(st);
994240233Sglebius			PF_STATE_UNLOCK(st);
995240233Sglebius			PFSYNC_LOCK(sc);
996240233Sglebius			pfsync_push(sc);
997240233Sglebius			PFSYNC_UNLOCK(sc);
998223637Sbz			continue;
999130613Smlaier		}
1000240233Sglebius		PF_STATE_UNLOCK(st);
1001223637Sbz	}
1002223637Sbz
1003223637Sbz	return (len);
1004223637Sbz}
1005223637Sbz
1006240233Sglebiusstatic int
1007223637Sbzpfsync_in_ureq(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1008223637Sbz{
1009223637Sbz	struct pfsync_upd_req *ur, *ura;
1010223637Sbz	struct mbuf *mp;
1011223637Sbz	int len = count * sizeof(*ur);
1012223637Sbz	int i, offp;
1013223637Sbz
1014223637Sbz	struct pf_state *st;
1015223637Sbz
1016223637Sbz	mp = m_pulldown(m, offset, len, &offp);
1017223637Sbz	if (mp == NULL) {
1018223637Sbz		V_pfsyncstats.pfsyncs_badlen++;
1019223637Sbz		return (-1);
1020130613Smlaier	}
1021223637Sbz	ura = (struct pfsync_upd_req *)(mp->m_data + offp);
1022130613Smlaier
1023223637Sbz	for (i = 0; i < count; i++) {
1024223637Sbz		ur = &ura[i];
1025130613Smlaier
1026240233Sglebius		if (ur->id == 0 && ur->creatorid == 0)
1027223637Sbz			pfsync_bulk_start();
1028223637Sbz		else {
1029240233Sglebius			st = pf_find_state_byid(ur->id, ur->creatorid);
1030130613Smlaier			if (st == NULL) {
1031223637Sbz				V_pfsyncstats.pfsyncs_badstate++;
1032130613Smlaier				continue;
1033130613Smlaier			}
1034240233Sglebius			if (st->state_flags & PFSTATE_NOSYNC) {
1035240233Sglebius				PF_STATE_UNLOCK(st);
1036223637Sbz				continue;
1037240233Sglebius			}
1038223637Sbz
1039223637Sbz			pfsync_update_state_req(st);
1040240233Sglebius			PF_STATE_UNLOCK(st);
1041130613Smlaier		}
1042223637Sbz	}
1043223637Sbz
1044223637Sbz	return (len);
1045223637Sbz}
1046223637Sbz
1047240233Sglebiusstatic int
1048223637Sbzpfsync_in_del(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1049223637Sbz{
1050223637Sbz	struct mbuf *mp;
1051223637Sbz	struct pfsync_state *sa, *sp;
1052223637Sbz	struct pf_state *st;
1053223637Sbz	int len = count * sizeof(*sp);
1054223637Sbz	int offp, i;
1055223637Sbz
1056223637Sbz	mp = m_pulldown(m, offset, len, &offp);
1057223637Sbz	if (mp == NULL) {
1058223637Sbz		V_pfsyncstats.pfsyncs_badlen++;
1059223637Sbz		return (-1);
1060223637Sbz	}
1061223637Sbz	sa = (struct pfsync_state *)(mp->m_data + offp);
1062223637Sbz
1063223637Sbz	for (i = 0; i < count; i++) {
1064223637Sbz		sp = &sa[i];
1065223637Sbz
1066240233Sglebius		st = pf_find_state_byid(sp->id, sp->creatorid);
1067223637Sbz		if (st == NULL) {
1068223637Sbz			V_pfsyncstats.pfsyncs_badstate++;
1069223637Sbz			continue;
1070130613Smlaier		}
1071240233Sglebius		st->state_flags |= PFSTATE_NOSYNC;
1072240233Sglebius		pf_unlink_state(st, PF_ENTER_LOCKED);
1073223637Sbz	}
1074130613Smlaier
1075223637Sbz	return (len);
1076223637Sbz}
1077223637Sbz
1078240233Sglebiusstatic int
1079223637Sbzpfsync_in_del_c(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1080223637Sbz{
1081223637Sbz	struct mbuf *mp;
1082223637Sbz	struct pfsync_del_c *sa, *sp;
1083223637Sbz	struct pf_state *st;
1084223637Sbz	int len = count * sizeof(*sp);
1085223637Sbz	int offp, i;
1086223637Sbz
1087223637Sbz	mp = m_pulldown(m, offset, len, &offp);
1088223637Sbz	if (mp == NULL) {
1089223637Sbz		V_pfsyncstats.pfsyncs_badlen++;
1090223637Sbz		return (-1);
1091223637Sbz	}
1092223637Sbz	sa = (struct pfsync_del_c *)(mp->m_data + offp);
1093223637Sbz
1094223637Sbz	for (i = 0; i < count; i++) {
1095223637Sbz		sp = &sa[i];
1096130613Smlaier
1097240233Sglebius		st = pf_find_state_byid(sp->id, sp->creatorid);
1098223637Sbz		if (st == NULL) {
1099223637Sbz			V_pfsyncstats.pfsyncs_badstate++;
1100223637Sbz			continue;
1101223637Sbz		}
1102223637Sbz
1103240233Sglebius		st->state_flags |= PFSTATE_NOSYNC;
1104240233Sglebius		pf_unlink_state(st, PF_ENTER_LOCKED);
1105223637Sbz	}
1106223637Sbz
1107223637Sbz	return (len);
1108223637Sbz}
1109223637Sbz
1110240233Sglebiusstatic int
1111223637Sbzpfsync_in_bus(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1112223637Sbz{
1113223637Sbz	struct pfsync_softc *sc = V_pfsyncif;
1114223637Sbz	struct pfsync_bus *bus;
1115223637Sbz	struct mbuf *mp;
1116223637Sbz	int len = count * sizeof(*bus);
1117223637Sbz	int offp;
1118223637Sbz
1119240233Sglebius	PFSYNC_BLOCK(sc);
1120240233Sglebius
1121223637Sbz	/* If we're not waiting for a bulk update, who cares. */
1122240233Sglebius	if (sc->sc_ureq_sent == 0) {
1123240233Sglebius		PFSYNC_BUNLOCK(sc);
1124223637Sbz		return (len);
1125240233Sglebius	}
1126223637Sbz
1127223637Sbz	mp = m_pulldown(m, offset, len, &offp);
1128223637Sbz	if (mp == NULL) {
1129240233Sglebius		PFSYNC_BUNLOCK(sc);
1130223637Sbz		V_pfsyncstats.pfsyncs_badlen++;
1131223637Sbz		return (-1);
1132223637Sbz	}
1133223637Sbz	bus = (struct pfsync_bus *)(mp->m_data + offp);
1134223637Sbz
1135223637Sbz	switch (bus->status) {
1136223637Sbz	case PFSYNC_BUS_START:
1137228814Sglebius		callout_reset(&sc->sc_bulkfail_tmo, 4 * hz +
1138240233Sglebius		    V_pf_limits[PF_LIMIT_STATES].limit /
1139229777Sglebius		    ((sc->sc_ifp->if_mtu - PFSYNC_MINPKT) /
1140228814Sglebius		    sizeof(struct pfsync_state)),
1141240233Sglebius		    pfsync_bulk_fail, sc);
1142223637Sbz		if (V_pf_status.debug >= PF_DEBUG_MISC)
1143223637Sbz			printf("pfsync: received bulk update start\n");
1144130613Smlaier		break;
1145130613Smlaier
1146223637Sbz	case PFSYNC_BUS_END:
1147223637Sbz		if (time_uptime - ntohl(bus->endtime) >=
1148223637Sbz		    sc->sc_ureq_sent) {
1149223637Sbz			/* that's it, we're happy */
1150223637Sbz			sc->sc_ureq_sent = 0;
1151223637Sbz			sc->sc_bulk_tries = 0;
1152240233Sglebius			callout_stop(&sc->sc_bulkfail_tmo);
1153240233Sglebius			if (!(sc->sc_flags & PFSYNCF_OK) && carp_demote_adj_p)
1154228736Sglebius				(*carp_demote_adj_p)(-V_pfsync_carp_adj,
1155228736Sglebius				    "pfsync bulk done");
1156240233Sglebius			sc->sc_flags |= PFSYNCF_OK;
1157223637Sbz			if (V_pf_status.debug >= PF_DEBUG_MISC)
1158223637Sbz				printf("pfsync: received valid "
1159223637Sbz				    "bulk update end\n");
1160223637Sbz		} else {
1161223637Sbz			if (V_pf_status.debug >= PF_DEBUG_MISC)
1162223637Sbz				printf("pfsync: received invalid "
1163223637Sbz				    "bulk update end: bad timestamp\n");
1164130613Smlaier		}
1165130613Smlaier		break;
1166223637Sbz	}
1167240233Sglebius	PFSYNC_BUNLOCK(sc);
1168223637Sbz
1169223637Sbz	return (len);
1170223637Sbz}
1171223637Sbz
1172240233Sglebiusstatic int
1173223637Sbzpfsync_in_tdb(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1174223637Sbz{
1175223637Sbz	int len = count * sizeof(struct pfsync_tdb);
1176223637Sbz
1177223637Sbz#if defined(IPSEC)
1178223637Sbz	struct pfsync_tdb *tp;
1179223637Sbz	struct mbuf *mp;
1180223637Sbz	int offp;
1181223637Sbz	int i;
1182223637Sbz	int s;
1183223637Sbz
1184223637Sbz	mp = m_pulldown(m, offset, len, &offp);
1185223637Sbz	if (mp == NULL) {
1186223637Sbz		V_pfsyncstats.pfsyncs_badlen++;
1187223637Sbz		return (-1);
1188223637Sbz	}
1189223637Sbz	tp = (struct pfsync_tdb *)(mp->m_data + offp);
1190223637Sbz
1191223637Sbz	for (i = 0; i < count; i++)
1192223637Sbz		pfsync_update_net_tdb(&tp[i]);
1193171168Smlaier#endif
1194223637Sbz
1195223637Sbz	return (len);
1196223637Sbz}
1197223637Sbz
1198223637Sbz#if defined(IPSEC)
1199223637Sbz/* Update an in-kernel tdb. Silently fail if no tdb is found. */
1200240233Sglebiusstatic void
1201223637Sbzpfsync_update_net_tdb(struct pfsync_tdb *pt)
1202223637Sbz{
1203223637Sbz	struct tdb		*tdb;
1204223637Sbz	int			 s;
1205223637Sbz
1206223637Sbz	/* check for invalid values */
1207223637Sbz	if (ntohl(pt->spi) <= SPI_RESERVED_MAX ||
1208223637Sbz	    (pt->dst.sa.sa_family != AF_INET &&
1209240233Sglebius	    pt->dst.sa.sa_family != AF_INET6))
1210223637Sbz		goto bad;
1211223637Sbz
1212223637Sbz	tdb = gettdb(pt->spi, &pt->dst, pt->sproto);
1213223637Sbz	if (tdb) {
1214223637Sbz		pt->rpl = ntohl(pt->rpl);
1215240233Sglebius		pt->cur_bytes = (unsigned long long)be64toh(pt->cur_bytes);
1216223637Sbz
1217223637Sbz		/* Neither replay nor byte counter should ever decrease. */
1218223637Sbz		if (pt->rpl < tdb->tdb_rpl ||
1219223637Sbz		    pt->cur_bytes < tdb->tdb_cur_bytes) {
1220223637Sbz			goto bad;
1221223637Sbz		}
1222223637Sbz
1223223637Sbz		tdb->tdb_rpl = pt->rpl;
1224223637Sbz		tdb->tdb_cur_bytes = pt->cur_bytes;
1225130613Smlaier	}
1226223637Sbz	return;
1227130613Smlaier
1228223637Sbzbad:
1229223637Sbz	if (V_pf_status.debug >= PF_DEBUG_MISC)
1230223637Sbz		printf("pfsync_insert: PFSYNC_ACT_TDB_UPD: "
1231223637Sbz		    "invalid value\n");
1232223637Sbz	V_pfsyncstats.pfsyncs_badstate++;
1233223637Sbz	return;
1234130613Smlaier}
1235223637Sbz#endif
1236130613Smlaier
1237223637Sbz
1238240233Sglebiusstatic int
1239223637Sbzpfsync_in_eof(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1240223637Sbz{
1241223637Sbz	/* check if we are at the right place in the packet */
1242244185Sglebius	if (offset != m->m_pkthdr.len)
1243244185Sglebius		V_pfsyncstats.pfsyncs_badlen++;
1244223637Sbz
1245223637Sbz	/* we're done. free and let the caller return */
1246223637Sbz	m_freem(m);
1247223637Sbz	return (-1);
1248223637Sbz}
1249223637Sbz
1250240233Sglebiusstatic int
1251223637Sbzpfsync_in_error(struct pfsync_pkt *pkt, struct mbuf *m, int offset, int count)
1252223637Sbz{
1253223637Sbz	V_pfsyncstats.pfsyncs_badact++;
1254223637Sbz
1255223637Sbz	m_freem(m);
1256223637Sbz	return (-1);
1257223637Sbz}
1258223637Sbz
1259240233Sglebiusstatic int
1260249925Sglebiuspfsyncoutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
1261223637Sbz	struct route *rt)
1262126258Smlaier{
1263126258Smlaier	m_freem(m);
1264126258Smlaier	return (0);
1265126258Smlaier}
1266126258Smlaier
1267126258Smlaier/* ARGSUSED */
1268240233Sglebiusstatic int
1269126258Smlaierpfsyncioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1270126258Smlaier{
1271126258Smlaier	struct pfsync_softc *sc = ifp->if_softc;
1272126258Smlaier	struct ifreq *ifr = (struct ifreq *)data;
1273130613Smlaier	struct pfsyncreq pfsyncr;
1274240233Sglebius	int error;
1275126258Smlaier
1276126258Smlaier	switch (cmd) {
1277126258Smlaier	case SIOCSIFFLAGS:
1278240233Sglebius		PFSYNC_LOCK(sc);
1279241057Sglebius		if (ifp->if_flags & IFF_UP) {
1280148887Srwatson			ifp->if_drv_flags |= IFF_DRV_RUNNING;
1281241057Sglebius			PFSYNC_UNLOCK(sc);
1282241057Sglebius			pfsync_pointers_init();
1283241057Sglebius		} else {
1284148887Srwatson			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1285241057Sglebius			PFSYNC_UNLOCK(sc);
1286241057Sglebius			pfsync_pointers_uninit();
1287241057Sglebius		}
1288126258Smlaier		break;
1289126258Smlaier	case SIOCSIFMTU:
1290229777Sglebius		if (!sc->sc_sync_if ||
1291229777Sglebius		    ifr->ifr_mtu <= PFSYNC_MINPKT ||
1292229777Sglebius		    ifr->ifr_mtu > sc->sc_sync_if->if_mtu)
1293126258Smlaier			return (EINVAL);
1294223637Sbz		if (ifr->ifr_mtu < ifp->if_mtu) {
1295240233Sglebius			PFSYNC_LOCK(sc);
1296240233Sglebius			if (sc->sc_len > PFSYNC_MINPKT)
1297240233Sglebius				pfsync_sendout(1);
1298240233Sglebius			PFSYNC_UNLOCK(sc);
1299223637Sbz		}
1300223637Sbz		ifp->if_mtu = ifr->ifr_mtu;
1301126258Smlaier		break;
1302130613Smlaier	case SIOCGETPFSYNC:
1303130613Smlaier		bzero(&pfsyncr, sizeof(pfsyncr));
1304240233Sglebius		PFSYNC_LOCK(sc);
1305223637Sbz		if (sc->sc_sync_if) {
1306145836Smlaier			strlcpy(pfsyncr.pfsyncr_syncdev,
1307223637Sbz			    sc->sc_sync_if->if_xname, IFNAMSIZ);
1308223637Sbz		}
1309145836Smlaier		pfsyncr.pfsyncr_syncpeer = sc->sc_sync_peer;
1310130613Smlaier		pfsyncr.pfsyncr_maxupdates = sc->sc_maxupdates;
1311240233Sglebius		pfsyncr.pfsyncr_defer = (PFSYNCF_DEFER ==
1312240233Sglebius		    (sc->sc_flags & PFSYNCF_DEFER));
1313240233Sglebius		PFSYNC_UNLOCK(sc);
1314223637Sbz		return (copyout(&pfsyncr, ifr->ifr_data, sizeof(pfsyncr)));
1315223637Sbz
1316130613Smlaier	case SIOCSETPFSYNC:
1317240233Sglebius	    {
1318240233Sglebius		struct ip_moptions *imo = &sc->sc_imo;
1319240233Sglebius		struct ifnet *sifp;
1320240233Sglebius		struct ip *ip;
1321240233Sglebius		void *mship = NULL;
1322240233Sglebius
1323164033Srwatson		if ((error = priv_check(curthread, PRIV_NETINET_PF)) != 0)
1324130613Smlaier			return (error);
1325130613Smlaier		if ((error = copyin(ifr->ifr_data, &pfsyncr, sizeof(pfsyncr))))
1326130613Smlaier			return (error);
1327130613Smlaier
1328240233Sglebius		if (pfsyncr.pfsyncr_maxupdates > 255)
1329240233Sglebius			return (EINVAL);
1330240233Sglebius
1331240233Sglebius		if (pfsyncr.pfsyncr_syncdev[0] == 0)
1332240233Sglebius			sifp = NULL;
1333240233Sglebius		else if ((sifp = ifunit_ref(pfsyncr.pfsyncr_syncdev)) == NULL)
1334240233Sglebius			return (EINVAL);
1335240233Sglebius
1336253769Sae		if (sifp != NULL && (
1337253769Sae		    pfsyncr.pfsyncr_syncpeer.s_addr == 0 ||
1338253769Sae		    pfsyncr.pfsyncr_syncpeer.s_addr ==
1339253769Sae		    htonl(INADDR_PFSYNC_GROUP)))
1340240233Sglebius			mship = malloc((sizeof(struct in_multi *) *
1341240233Sglebius			    IP_MIN_MEMBERSHIPS), M_PFSYNC, M_WAITOK | M_ZERO);
1342240233Sglebius
1343240233Sglebius		PFSYNC_LOCK(sc);
1344145836Smlaier		if (pfsyncr.pfsyncr_syncpeer.s_addr == 0)
1345159603Smlaier			sc->sc_sync_peer.s_addr = htonl(INADDR_PFSYNC_GROUP);
1346145836Smlaier		else
1347145836Smlaier			sc->sc_sync_peer.s_addr =
1348145836Smlaier			    pfsyncr.pfsyncr_syncpeer.s_addr;
1349145836Smlaier
1350240233Sglebius		sc->sc_maxupdates = pfsyncr.pfsyncr_maxupdates;
1351240233Sglebius		if (pfsyncr.pfsyncr_defer) {
1352240233Sglebius			sc->sc_flags |= PFSYNCF_DEFER;
1353240233Sglebius			pfsync_defer_ptr = pfsync_defer;
1354240233Sglebius		} else {
1355240233Sglebius			sc->sc_flags &= ~PFSYNCF_DEFER;
1356240233Sglebius			pfsync_defer_ptr = NULL;
1357171168Smlaier		}
1358130613Smlaier
1359240233Sglebius		if (sifp == NULL) {
1360240233Sglebius			if (sc->sc_sync_if)
1361240233Sglebius				if_rele(sc->sc_sync_if);
1362223637Sbz			sc->sc_sync_if = NULL;
1363229850Sglebius			if (imo->imo_membership)
1364229850Sglebius				pfsync_multicast_cleanup(sc);
1365240233Sglebius			PFSYNC_UNLOCK(sc);
1366130613Smlaier			break;
1367130613Smlaier		}
1368145836Smlaier
1369240233Sglebius		if (sc->sc_len > PFSYNC_MINPKT &&
1370240233Sglebius		    (sifp->if_mtu < sc->sc_ifp->if_mtu ||
1371223637Sbz		    (sc->sc_sync_if != NULL &&
1372223637Sbz		    sifp->if_mtu < sc->sc_sync_if->if_mtu) ||
1373240233Sglebius		    sifp->if_mtu < MCLBYTES - sizeof(struct ip)))
1374240233Sglebius			pfsync_sendout(1);
1375130613Smlaier
1376240233Sglebius		if (imo->imo_membership)
1377229850Sglebius			pfsync_multicast_cleanup(sc);
1378130613Smlaier
1379240233Sglebius		if (sc->sc_sync_peer.s_addr == htonl(INADDR_PFSYNC_GROUP)) {
1380240233Sglebius			error = pfsync_multicast_setup(sc, sifp, mship);
1381240233Sglebius			if (error) {
1382240233Sglebius				if_rele(sifp);
1383240233Sglebius				free(mship, M_PFSYNC);
1384229850Sglebius				return (error);
1385145836Smlaier			}
1386145836Smlaier		}
1387240233Sglebius		if (sc->sc_sync_if)
1388240233Sglebius			if_rele(sc->sc_sync_if);
1389240233Sglebius		sc->sc_sync_if = sifp;
1390130613Smlaier
1391223637Sbz		ip = &sc->sc_template;
1392223637Sbz		bzero(ip, sizeof(*ip));
1393223637Sbz		ip->ip_v = IPVERSION;
1394223637Sbz		ip->ip_hl = sizeof(sc->sc_template) >> 2;
1395223637Sbz		ip->ip_tos = IPTOS_LOWDELAY;
1396240233Sglebius		/* len and id are set later. */
1397241913Sglebius		ip->ip_off = htons(IP_DF);
1398223637Sbz		ip->ip_ttl = PFSYNC_DFLTTL;
1399223637Sbz		ip->ip_p = IPPROTO_PFSYNC;
1400223637Sbz		ip->ip_src.s_addr = INADDR_ANY;
1401223637Sbz		ip->ip_dst.s_addr = sc->sc_sync_peer.s_addr;
1402223637Sbz
1403240233Sglebius		/* Request a full state table update. */
1404240233Sglebius		if ((sc->sc_flags & PFSYNCF_OK) && carp_demote_adj_p)
1405240233Sglebius			(*carp_demote_adj_p)(V_pfsync_carp_adj,
1406240233Sglebius			    "pfsync bulk start");
1407240233Sglebius		sc->sc_flags &= ~PFSYNCF_OK;
1408240233Sglebius		if (V_pf_status.debug >= PF_DEBUG_MISC)
1409240233Sglebius			printf("pfsync: requesting bulk update\n");
1410240233Sglebius		pfsync_request_update(0, 0);
1411240233Sglebius		PFSYNC_UNLOCK(sc);
1412240233Sglebius		PFSYNC_BLOCK(sc);
1413240233Sglebius		sc->sc_ureq_sent = time_uptime;
1414240233Sglebius		callout_reset(&sc->sc_bulkfail_tmo, 5 * hz, pfsync_bulk_fail,
1415240233Sglebius		    sc);
1416240233Sglebius		PFSYNC_BUNLOCK(sc);
1417130613Smlaier
1418130613Smlaier		break;
1419240233Sglebius	    }
1420126258Smlaier	default:
1421126258Smlaier		return (ENOTTY);
1422126258Smlaier	}
1423126258Smlaier
1424126258Smlaier	return (0);
1425126258Smlaier}
1426126258Smlaier
1427241056Sglebiusstatic void
1428241056Sglebiuspfsync_out_state(struct pf_state *st, void *buf)
1429130613Smlaier{
1430241056Sglebius	struct pfsync_state *sp = buf;
1431130613Smlaier
1432223637Sbz	pfsync_state_export(sp, st);
1433223637Sbz}
1434223637Sbz
1435241056Sglebiusstatic void
1436241056Sglebiuspfsync_out_iack(struct pf_state *st, void *buf)
1437223637Sbz{
1438241056Sglebius	struct pfsync_ins_ack *iack = buf;
1439223637Sbz
1440223637Sbz	iack->id = st->id;
1441223637Sbz	iack->creatorid = st->creatorid;
1442223637Sbz}
1443223637Sbz
1444241056Sglebiusstatic void
1445241056Sglebiuspfsync_out_upd_c(struct pf_state *st, void *buf)
1446223637Sbz{
1447241056Sglebius	struct pfsync_upd_c *up = buf;
1448223637Sbz
1449232685Sglebius	bzero(up, sizeof(*up));
1450223637Sbz	up->id = st->id;
1451223637Sbz	pf_state_peer_hton(&st->src, &up->src);
1452223637Sbz	pf_state_peer_hton(&st->dst, &up->dst);
1453223637Sbz	up->creatorid = st->creatorid;
1454223637Sbz	up->timeout = st->timeout;
1455223637Sbz}
1456223637Sbz
1457241056Sglebiusstatic void
1458241056Sglebiuspfsync_out_del(struct pf_state *st, void *buf)
1459223637Sbz{
1460241056Sglebius	struct pfsync_del_c *dp = buf;
1461223637Sbz
1462223637Sbz	dp->id = st->id;
1463223637Sbz	dp->creatorid = st->creatorid;
1464240233Sglebius	st->state_flags |= PFSTATE_NOSYNC;
1465223637Sbz}
1466223637Sbz
1467240233Sglebiusstatic void
1468223637Sbzpfsync_drop(struct pfsync_softc *sc)
1469223637Sbz{
1470240233Sglebius	struct pf_state *st, *next;
1471223637Sbz	struct pfsync_upd_req_item *ur;
1472223637Sbz	int q;
1473223637Sbz
1474223637Sbz	for (q = 0; q < PFSYNC_S_COUNT; q++) {
1475223637Sbz		if (TAILQ_EMPTY(&sc->sc_qs[q]))
1476223637Sbz			continue;
1477223637Sbz
1478240233Sglebius		TAILQ_FOREACH_SAFE(st, &sc->sc_qs[q], sync_list, next) {
1479223637Sbz			KASSERT(st->sync_state == q,
1480229964Sglebius				("%s: st->sync_state == q",
1481240233Sglebius					__func__));
1482223637Sbz			st->sync_state = PFSYNC_S_NONE;
1483240233Sglebius			pf_release_state(st);
1484223637Sbz		}
1485223637Sbz		TAILQ_INIT(&sc->sc_qs[q]);
1486223637Sbz	}
1487223637Sbz
1488223637Sbz	while ((ur = TAILQ_FIRST(&sc->sc_upd_req_list)) != NULL) {
1489223637Sbz		TAILQ_REMOVE(&sc->sc_upd_req_list, ur, ur_entry);
1490240233Sglebius		free(ur, M_PFSYNC);
1491223637Sbz	}
1492223637Sbz
1493223637Sbz	sc->sc_plus = NULL;
1494223637Sbz	sc->sc_len = PFSYNC_MINPKT;
1495126258Smlaier}
1496126258Smlaier
1497229976Sglebiusstatic void
1498240233Sglebiuspfsync_sendout(int schedswi)
1499229976Sglebius{
1500229976Sglebius	struct pfsync_softc *sc = V_pfsyncif;
1501223637Sbz	struct ifnet *ifp = sc->sc_ifp;
1502126258Smlaier	struct mbuf *m;
1503223637Sbz	struct ip *ip;
1504223637Sbz	struct pfsync_header *ph;
1505223637Sbz	struct pfsync_subheader *subh;
1506241056Sglebius	struct pf_state *st;
1507223637Sbz	struct pfsync_upd_req_item *ur;
1508223637Sbz	int offset;
1509223637Sbz	int q, count = 0;
1510126258Smlaier
1511240233Sglebius	KASSERT(sc != NULL, ("%s: null sc", __func__));
1512240233Sglebius	KASSERT(sc->sc_len > PFSYNC_MINPKT,
1513240233Sglebius	    ("%s: sc_len %zu", __func__, sc->sc_len));
1514240233Sglebius	PFSYNC_LOCK_ASSERT(sc);
1515223637Sbz
1516223637Sbz	if (ifp->if_bpf == NULL && sc->sc_sync_if == NULL) {
1517223637Sbz		pfsync_drop(sc);
1518223637Sbz		return;
1519223637Sbz	}
1520223637Sbz
1521248207Sglebius	m = m_get2(max_linkhdr + sc->sc_len, M_NOWAIT, MT_DATA, M_PKTHDR);
1522126258Smlaier	if (m == NULL) {
1523171168Smlaier		sc->sc_ifp->if_oerrors++;
1524230265Sglebius		V_pfsyncstats.pfsyncs_onomem++;
1525230265Sglebius		return;
1526230265Sglebius	}
1527223637Sbz	m->m_data += max_linkhdr;
1528223637Sbz	m->m_len = m->m_pkthdr.len = sc->sc_len;
1529130613Smlaier
1530223637Sbz	/* build the ip header */
1531223637Sbz	ip = (struct ip *)m->m_data;
1532223637Sbz	bcopy(&sc->sc_template, ip, sizeof(*ip));
1533223637Sbz	offset = sizeof(*ip);
1534126258Smlaier
1535241913Sglebius	ip->ip_len = htons(m->m_pkthdr.len);
1536223637Sbz	ip->ip_id = htons(ip_randomid());
1537126258Smlaier
1538223637Sbz	/* build the pfsync header */
1539223637Sbz	ph = (struct pfsync_header *)(m->m_data + offset);
1540223637Sbz	bzero(ph, sizeof(*ph));
1541223637Sbz	offset += sizeof(*ph);
1542126258Smlaier
1543223637Sbz	ph->version = PFSYNC_VERSION;
1544223637Sbz	ph->len = htons(sc->sc_len - sizeof(*ip));
1545223637Sbz	bcopy(V_pf_status.pf_chksum, ph->pfcksum, PF_MD5_DIGEST_LENGTH);
1546171168Smlaier
1547223637Sbz	/* walk the queues */
1548223637Sbz	for (q = 0; q < PFSYNC_S_COUNT; q++) {
1549223637Sbz		if (TAILQ_EMPTY(&sc->sc_qs[q]))
1550223637Sbz			continue;
1551223637Sbz
1552223637Sbz		subh = (struct pfsync_subheader *)(m->m_data + offset);
1553223637Sbz		offset += sizeof(*subh);
1554223637Sbz
1555223637Sbz		count = 0;
1556241056Sglebius		TAILQ_FOREACH(st, &sc->sc_qs[q], sync_list) {
1557223637Sbz			KASSERT(st->sync_state == q,
1558223637Sbz				("%s: st->sync_state == q",
1559240233Sglebius					__func__));
1560240233Sglebius			/*
1561240233Sglebius			 * XXXGL: some of write methods do unlocked reads
1562240233Sglebius			 * of state data :(
1563240233Sglebius			 */
1564241056Sglebius			pfsync_qs[q].write(st, m->m_data + offset);
1565241056Sglebius			offset += pfsync_qs[q].len;
1566223637Sbz			st->sync_state = PFSYNC_S_NONE;
1567240233Sglebius			pf_release_state(st);
1568223637Sbz			count++;
1569126258Smlaier		}
1570223637Sbz		TAILQ_INIT(&sc->sc_qs[q]);
1571130613Smlaier
1572223637Sbz		bzero(subh, sizeof(*subh));
1573223637Sbz		subh->action = pfsync_qs[q].action;
1574223637Sbz		subh->count = htons(count);
1575240233Sglebius		V_pfsyncstats.pfsyncs_oacts[pfsync_qs[q].action] += count;
1576126258Smlaier	}
1577126258Smlaier
1578223637Sbz	if (!TAILQ_EMPTY(&sc->sc_upd_req_list)) {
1579223637Sbz		subh = (struct pfsync_subheader *)(m->m_data + offset);
1580223637Sbz		offset += sizeof(*subh);
1581126258Smlaier
1582223637Sbz		count = 0;
1583223637Sbz		while ((ur = TAILQ_FIRST(&sc->sc_upd_req_list)) != NULL) {
1584223637Sbz			TAILQ_REMOVE(&sc->sc_upd_req_list, ur, ur_entry);
1585130613Smlaier
1586223637Sbz			bcopy(&ur->ur_msg, m->m_data + offset,
1587223637Sbz			    sizeof(ur->ur_msg));
1588223637Sbz			offset += sizeof(ur->ur_msg);
1589240233Sglebius			free(ur, M_PFSYNC);
1590223637Sbz			count++;
1591223637Sbz		}
1592130613Smlaier
1593223637Sbz		bzero(subh, sizeof(*subh));
1594223637Sbz		subh->action = PFSYNC_ACT_UPD_REQ;
1595223637Sbz		subh->count = htons(count);
1596240233Sglebius		V_pfsyncstats.pfsyncs_oacts[PFSYNC_ACT_UPD_REQ] += count;
1597223637Sbz	}
1598130613Smlaier
1599223637Sbz	/* has someone built a custom region for us to add? */
1600223637Sbz	if (sc->sc_plus != NULL) {
1601223637Sbz		bcopy(sc->sc_plus, m->m_data + offset, sc->sc_pluslen);
1602223637Sbz		offset += sc->sc_pluslen;
1603130613Smlaier
1604223637Sbz		sc->sc_plus = NULL;
1605130613Smlaier	}
1606130613Smlaier
1607223637Sbz	subh = (struct pfsync_subheader *)(m->m_data + offset);
1608223637Sbz	offset += sizeof(*subh);
1609130613Smlaier
1610223637Sbz	bzero(subh, sizeof(*subh));
1611223637Sbz	subh->action = PFSYNC_ACT_EOF;
1612223637Sbz	subh->count = htons(1);
1613240233Sglebius	V_pfsyncstats.pfsyncs_oacts[PFSYNC_ACT_EOF]++;
1614130613Smlaier
1615223637Sbz	/* we're done, let's put it on the wire */
1616223637Sbz	if (ifp->if_bpf) {
1617223637Sbz		m->m_data += sizeof(*ip);
1618223637Sbz		m->m_len = m->m_pkthdr.len = sc->sc_len - sizeof(*ip);
1619223637Sbz		BPF_MTAP(ifp, m);
1620223637Sbz		m->m_data -= sizeof(*ip);
1621223637Sbz		m->m_len = m->m_pkthdr.len = sc->sc_len;
1622130613Smlaier	}
1623130613Smlaier
1624223637Sbz	if (sc->sc_sync_if == NULL) {
1625223637Sbz		sc->sc_len = PFSYNC_MINPKT;
1626223637Sbz		m_freem(m);
1627223637Sbz		return;
1628223637Sbz	}
1629126258Smlaier
1630223637Sbz	sc->sc_ifp->if_opackets++;
1631223637Sbz	sc->sc_ifp->if_obytes += m->m_pkthdr.len;
1632226660Sglebius	sc->sc_len = PFSYNC_MINPKT;
1633226660Sglebius
1634229976Sglebius	if (!_IF_QFULL(&sc->sc_ifp->if_snd))
1635229976Sglebius		_IF_ENQUEUE(&sc->sc_ifp->if_snd, m);
1636229976Sglebius	else {
1637229976Sglebius		m_freem(m);
1638240233Sglebius		sc->sc_ifp->if_snd.ifq_drops++;
1639229976Sglebius	}
1640229976Sglebius	if (schedswi)
1641229976Sglebius		swi_sched(V_pfsync_swi_cookie, 0);
1642126258Smlaier}
1643126258Smlaier
1644240233Sglebiusstatic void
1645223637Sbzpfsync_insert_state(struct pf_state *st)
1646126258Smlaier{
1647223637Sbz	struct pfsync_softc *sc = V_pfsyncif;
1648130613Smlaier
1649240233Sglebius	if (st->state_flags & PFSTATE_NOSYNC)
1650240233Sglebius		return;
1651130613Smlaier
1652240233Sglebius	if ((st->rule.ptr->rule_flag & PFRULE_NOSYNC) ||
1653223637Sbz	    st->key[PF_SK_WIRE]->proto == IPPROTO_PFSYNC) {
1654240233Sglebius		st->state_flags |= PFSTATE_NOSYNC;
1655223637Sbz		return;
1656130613Smlaier	}
1657130613Smlaier
1658223637Sbz	KASSERT(st->sync_state == PFSYNC_S_NONE,
1659250313Sglebius		("%s: st->sync_state %u", __func__, st->sync_state));
1660223637Sbz
1661240233Sglebius	PFSYNC_LOCK(sc);
1662223637Sbz	if (sc->sc_len == PFSYNC_MINPKT)
1663240233Sglebius		callout_reset(&sc->sc_tmo, 1 * hz, pfsync_timeout, V_pfsyncif);
1664223637Sbz
1665223637Sbz	pfsync_q_ins(st, PFSYNC_S_INS);
1666240233Sglebius	PFSYNC_UNLOCK(sc);
1667223637Sbz
1668233846Sglebius	st->sync_updates = 0;
1669130613Smlaier}
1670130613Smlaier
1671240233Sglebiusstatic int
1672223637Sbzpfsync_defer(struct pf_state *st, struct mbuf *m)
1673130613Smlaier{
1674223637Sbz	struct pfsync_softc *sc = V_pfsyncif;
1675223637Sbz	struct pfsync_deferral *pd;
1676126258Smlaier
1677240233Sglebius	if (m->m_flags & (M_BCAST|M_MCAST))
1678240233Sglebius		return (0);
1679223637Sbz
1680240233Sglebius	PFSYNC_LOCK(sc);
1681240233Sglebius
1682240233Sglebius	if (sc == NULL || !(sc->sc_ifp->if_flags & IFF_DRV_RUNNING) ||
1683240233Sglebius	    !(sc->sc_flags & PFSYNCF_DEFER)) {
1684240233Sglebius		PFSYNC_UNLOCK(sc);
1685233846Sglebius		return (0);
1686240233Sglebius	}
1687233846Sglebius
1688240233Sglebius	 if (sc->sc_deferred >= 128)
1689223637Sbz		pfsync_undefer(TAILQ_FIRST(&sc->sc_deferrals), 0);
1690223637Sbz
1691240233Sglebius	pd = malloc(sizeof(*pd), M_PFSYNC, M_NOWAIT);
1692223637Sbz	if (pd == NULL)
1693171168Smlaier		return (0);
1694223637Sbz	sc->sc_deferred++;
1695171168Smlaier
1696223637Sbz	m->m_flags |= M_SKIP_FIREWALL;
1697240233Sglebius	st->state_flags |= PFSTATE_ACK;
1698223637Sbz
1699240233Sglebius	pd->pd_sc = sc;
1700240233Sglebius	pd->pd_refs = 0;
1701223637Sbz	pd->pd_st = st;
1702240233Sglebius	pf_ref_state(st);
1703223637Sbz	pd->pd_m = m;
1704223637Sbz
1705223637Sbz	TAILQ_INSERT_TAIL(&sc->sc_deferrals, pd, pd_entry);
1706240233Sglebius	callout_init_mtx(&pd->pd_tmo, &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
1707240233Sglebius	callout_reset(&pd->pd_tmo, 10, pfsync_defer_tmo, pd);
1708126258Smlaier
1709240233Sglebius	pfsync_push(sc);
1710233846Sglebius
1711223637Sbz	return (1);
1712126258Smlaier}
1713126258Smlaier
1714240233Sglebiusstatic void
1715223637Sbzpfsync_undefer(struct pfsync_deferral *pd, int drop)
1716126258Smlaier{
1717240233Sglebius	struct pfsync_softc *sc = pd->pd_sc;
1718240233Sglebius	struct mbuf *m = pd->pd_m;
1719240233Sglebius	struct pf_state *st = pd->pd_st;
1720126258Smlaier
1721240233Sglebius	PFSYNC_LOCK_ASSERT(sc);
1722223637Sbz
1723223637Sbz	TAILQ_REMOVE(&sc->sc_deferrals, pd, pd_entry);
1724223637Sbz	sc->sc_deferred--;
1725240233Sglebius	pd->pd_st->state_flags &= ~PFSTATE_ACK;	/* XXX: locking! */
1726240233Sglebius	free(pd, M_PFSYNC);
1727240233Sglebius	pf_release_state(st);
1728223637Sbz
1729223637Sbz	if (drop)
1730240233Sglebius		m_freem(m);
1731223637Sbz	else {
1732240233Sglebius		_IF_ENQUEUE(&sc->sc_ifp->if_snd, m);
1733240233Sglebius		pfsync_push(sc);
1734223637Sbz	}
1735126258Smlaier}
1736126258Smlaier
1737240233Sglebiusstatic void
1738223637Sbzpfsync_defer_tmo(void *arg)
1739171168Smlaier{
1740223637Sbz	struct pfsync_deferral *pd = arg;
1741240233Sglebius	struct pfsync_softc *sc = pd->pd_sc;
1742240233Sglebius	struct mbuf *m = pd->pd_m;
1743240233Sglebius	struct pf_state *st = pd->pd_st;
1744171168Smlaier
1745240233Sglebius	PFSYNC_LOCK_ASSERT(sc);
1746240233Sglebius
1747240233Sglebius	CURVNET_SET(m->m_pkthdr.rcvif->if_vnet);
1748240233Sglebius
1749240233Sglebius	TAILQ_REMOVE(&sc->sc_deferrals, pd, pd_entry);
1750240233Sglebius	sc->sc_deferred--;
1751240233Sglebius	pd->pd_st->state_flags &= ~PFSTATE_ACK;	/* XXX: locking! */
1752240233Sglebius	if (pd->pd_refs == 0)
1753240233Sglebius		free(pd, M_PFSYNC);
1754240233Sglebius	PFSYNC_UNLOCK(sc);
1755240233Sglebius
1756240233Sglebius	ip_output(m, NULL, NULL, 0, NULL, NULL);
1757240233Sglebius
1758240233Sglebius	pf_release_state(st);
1759240233Sglebius
1760223637Sbz	CURVNET_RESTORE();
1761171168Smlaier}
1762223637Sbz
1763240233Sglebiusstatic void
1764240233Sglebiuspfsync_undefer_state(struct pf_state *st, int drop)
1765223637Sbz{
1766223637Sbz	struct pfsync_softc *sc = V_pfsyncif;
1767223637Sbz	struct pfsync_deferral *pd;
1768171168Smlaier
1769240233Sglebius	PFSYNC_LOCK_ASSERT(sc);
1770240233Sglebius
1771223637Sbz	TAILQ_FOREACH(pd, &sc->sc_deferrals, pd_entry) {
1772223637Sbz		 if (pd->pd_st == st) {
1773240233Sglebius			if (callout_stop(&pd->pd_tmo))
1774240233Sglebius				pfsync_undefer(pd, drop);
1775223637Sbz			return;
1776223637Sbz		}
1777223637Sbz	}
1778223637Sbz
1779240233Sglebius	panic("%s: unable to find deferred state", __func__);
1780223637Sbz}
1781223637Sbz
1782240233Sglebiusstatic void
1783223637Sbzpfsync_update_state(struct pf_state *st)
1784130613Smlaier{
1785223637Sbz	struct pfsync_softc *sc = V_pfsyncif;
1786223637Sbz	int sync = 0;
1787130613Smlaier
1788240233Sglebius	PF_STATE_LOCK_ASSERT(st);
1789240233Sglebius	PFSYNC_LOCK(sc);
1790130613Smlaier
1791240233Sglebius	if (st->state_flags & PFSTATE_ACK)
1792240233Sglebius		pfsync_undefer_state(st, 0);
1793240233Sglebius	if (st->state_flags & PFSTATE_NOSYNC) {
1794223637Sbz		if (st->sync_state != PFSYNC_S_NONE)
1795223637Sbz			pfsync_q_del(st);
1796240233Sglebius		PFSYNC_UNLOCK(sc);
1797223637Sbz		return;
1798130613Smlaier	}
1799223637Sbz
1800223637Sbz	if (sc->sc_len == PFSYNC_MINPKT)
1801240233Sglebius		callout_reset(&sc->sc_tmo, 1 * hz, pfsync_timeout, V_pfsyncif);
1802223637Sbz
1803223637Sbz	switch (st->sync_state) {
1804223637Sbz	case PFSYNC_S_UPD_C:
1805223637Sbz	case PFSYNC_S_UPD:
1806223637Sbz	case PFSYNC_S_INS:
1807223637Sbz		/* we're already handling it */
1808223637Sbz
1809228816Sglebius		if (st->key[PF_SK_WIRE]->proto == IPPROTO_TCP) {
1810228816Sglebius			st->sync_updates++;
1811228816Sglebius			if (st->sync_updates >= sc->sc_maxupdates)
1812228816Sglebius				sync = 1;
1813228816Sglebius		}
1814223637Sbz		break;
1815223637Sbz
1816223637Sbz	case PFSYNC_S_IACK:
1817223637Sbz		pfsync_q_del(st);
1818223637Sbz	case PFSYNC_S_NONE:
1819223637Sbz		pfsync_q_ins(st, PFSYNC_S_UPD_C);
1820223637Sbz		st->sync_updates = 0;
1821223637Sbz		break;
1822223637Sbz
1823223637Sbz	default:
1824240233Sglebius		panic("%s: unexpected sync state %d", __func__, st->sync_state);
1825223637Sbz	}
1826223637Sbz
1827240233Sglebius	if (sync || (time_uptime - st->pfsync_time) < 2)
1828240233Sglebius		pfsync_push(sc);
1829240233Sglebius
1830240233Sglebius	PFSYNC_UNLOCK(sc);
1831130613Smlaier}
1832130613Smlaier
1833240233Sglebiusstatic void
1834223637Sbzpfsync_request_update(u_int32_t creatorid, u_int64_t id)
1835130613Smlaier{
1836223637Sbz	struct pfsync_softc *sc = V_pfsyncif;
1837223637Sbz	struct pfsync_upd_req_item *item;
1838223637Sbz	size_t nlen = sizeof(struct pfsync_upd_req);
1839130613Smlaier
1840240233Sglebius	PFSYNC_LOCK_ASSERT(sc);
1841226544Sbz
1842130613Smlaier	/*
1843241131Sglebius	 * This code does a bit to prevent multiple update requests for the
1844241131Sglebius	 * same state being generated. It searches current subheader queue,
1845241131Sglebius	 * but it doesn't lookup into queue of already packed datagrams.
1846130613Smlaier	 */
1847241131Sglebius	TAILQ_FOREACH(item, &sc->sc_upd_req_list, ur_entry)
1848241131Sglebius		if (item->ur_msg.id == id &&
1849241131Sglebius		    item->ur_msg.creatorid == creatorid)
1850241131Sglebius			return;
1851241131Sglebius
1852240233Sglebius	item = malloc(sizeof(*item), M_PFSYNC, M_NOWAIT);
1853240233Sglebius	if (item == NULL)
1854240233Sglebius		return; /* XXX stats */
1855130613Smlaier
1856223637Sbz	item->ur_msg.id = id;
1857223637Sbz	item->ur_msg.creatorid = creatorid;
1858171168Smlaier
1859223637Sbz	if (TAILQ_EMPTY(&sc->sc_upd_req_list))
1860223637Sbz		nlen += sizeof(struct pfsync_subheader);
1861223637Sbz
1862229777Sglebius	if (sc->sc_len + nlen > sc->sc_ifp->if_mtu) {
1863240233Sglebius		pfsync_sendout(1);
1864223637Sbz
1865223637Sbz		nlen = sizeof(struct pfsync_subheader) +
1866223637Sbz		    sizeof(struct pfsync_upd_req);
1867130613Smlaier	}
1868223637Sbz
1869223637Sbz	TAILQ_INSERT_TAIL(&sc->sc_upd_req_list, item, ur_entry);
1870223637Sbz	sc->sc_len += nlen;
1871223637Sbz}
1872223637Sbz
1873240233Sglebiusstatic void
1874223637Sbzpfsync_update_state_req(struct pf_state *st)
1875223637Sbz{
1876223637Sbz	struct pfsync_softc *sc = V_pfsyncif;
1877223637Sbz
1878240233Sglebius	PF_STATE_LOCK_ASSERT(st);
1879240233Sglebius	PFSYNC_LOCK(sc);
1880226544Sbz
1881240233Sglebius	if (st->state_flags & PFSTATE_NOSYNC) {
1882223637Sbz		if (st->sync_state != PFSYNC_S_NONE)
1883223637Sbz			pfsync_q_del(st);
1884240233Sglebius		PFSYNC_UNLOCK(sc);
1885223637Sbz		return;
1886223637Sbz	}
1887223637Sbz
1888223637Sbz	switch (st->sync_state) {
1889223637Sbz	case PFSYNC_S_UPD_C:
1890223637Sbz	case PFSYNC_S_IACK:
1891223637Sbz		pfsync_q_del(st);
1892223637Sbz	case PFSYNC_S_NONE:
1893223637Sbz		pfsync_q_ins(st, PFSYNC_S_UPD);
1894240233Sglebius		pfsync_push(sc);
1895240233Sglebius		break;
1896223637Sbz
1897223637Sbz	case PFSYNC_S_INS:
1898223637Sbz	case PFSYNC_S_UPD:
1899223637Sbz	case PFSYNC_S_DEL:
1900223637Sbz		/* we're already handling it */
1901240233Sglebius		break;
1902223637Sbz
1903223637Sbz	default:
1904240233Sglebius		panic("%s: unexpected sync state %d", __func__, st->sync_state);
1905223637Sbz	}
1906240233Sglebius
1907240233Sglebius	PFSYNC_UNLOCK(sc);
1908130613Smlaier}
1909130613Smlaier
1910240233Sglebiusstatic void
1911223637Sbzpfsync_delete_state(struct pf_state *st)
1912130613Smlaier{
1913223637Sbz	struct pfsync_softc *sc = V_pfsyncif;
1914223637Sbz
1915240233Sglebius	PFSYNC_LOCK(sc);
1916240233Sglebius	if (st->state_flags & PFSTATE_ACK)
1917240233Sglebius		pfsync_undefer_state(st, 1);
1918240233Sglebius	if (st->state_flags & PFSTATE_NOSYNC) {
1919223637Sbz		if (st->sync_state != PFSYNC_S_NONE)
1920223637Sbz			pfsync_q_del(st);
1921240233Sglebius		PFSYNC_UNLOCK(sc);
1922223637Sbz		return;
1923223637Sbz	}
1924223637Sbz
1925223637Sbz	if (sc->sc_len == PFSYNC_MINPKT)
1926240233Sglebius		callout_reset(&sc->sc_tmo, 1 * hz, pfsync_timeout, V_pfsyncif);
1927223637Sbz
1928223637Sbz	switch (st->sync_state) {
1929223637Sbz	case PFSYNC_S_INS:
1930240233Sglebius		/* We never got to tell the world so just forget about it. */
1931223637Sbz		pfsync_q_del(st);
1932240233Sglebius		break;
1933223637Sbz
1934223637Sbz	case PFSYNC_S_UPD_C:
1935223637Sbz	case PFSYNC_S_UPD:
1936223637Sbz	case PFSYNC_S_IACK:
1937223637Sbz		pfsync_q_del(st);
1938223637Sbz		/* FALLTHROUGH to putting it on the del list */
1939223637Sbz
1940223637Sbz	case PFSYNC_S_NONE:
1941223637Sbz		pfsync_q_ins(st, PFSYNC_S_DEL);
1942240233Sglebius		break;
1943223637Sbz
1944223637Sbz	default:
1945240233Sglebius		panic("%s: unexpected sync state %d", __func__, st->sync_state);
1946130613Smlaier	}
1947240233Sglebius	PFSYNC_UNLOCK(sc);
1948223637Sbz}
1949223637Sbz
1950240233Sglebiusstatic void
1951223637Sbzpfsync_clear_states(u_int32_t creatorid, const char *ifname)
1952223637Sbz{
1953240233Sglebius	struct pfsync_softc *sc = V_pfsyncif;
1954223637Sbz	struct {
1955223637Sbz		struct pfsync_subheader subh;
1956223637Sbz		struct pfsync_clr clr;
1957223637Sbz	} __packed r;
1958223637Sbz
1959223637Sbz	bzero(&r, sizeof(r));
1960223637Sbz
1961223637Sbz	r.subh.action = PFSYNC_ACT_CLR;
1962223637Sbz	r.subh.count = htons(1);
1963240233Sglebius	V_pfsyncstats.pfsyncs_oacts[PFSYNC_ACT_CLR]++;
1964223637Sbz
1965223637Sbz	strlcpy(r.clr.ifname, ifname, sizeof(r.clr.ifname));
1966223637Sbz	r.clr.creatorid = creatorid;
1967223637Sbz
1968240233Sglebius	PFSYNC_LOCK(sc);
1969223637Sbz	pfsync_send_plus(&r, sizeof(r));
1970240233Sglebius	PFSYNC_UNLOCK(sc);
1971130613Smlaier}
1972130613Smlaier
1973240233Sglebiusstatic void
1974223637Sbzpfsync_q_ins(struct pf_state *st, int q)
1975126258Smlaier{
1976223637Sbz	struct pfsync_softc *sc = V_pfsyncif;
1977223637Sbz	size_t nlen = pfsync_qs[q].len;
1978223637Sbz
1979240233Sglebius	PFSYNC_LOCK_ASSERT(sc);
1980226544Sbz
1981223637Sbz	KASSERT(st->sync_state == PFSYNC_S_NONE,
1982250313Sglebius		("%s: st->sync_state %u", __func__, st->sync_state));
1983240233Sglebius	KASSERT(sc->sc_len >= PFSYNC_MINPKT, ("pfsync pkt len is too low %zu",
1984240233Sglebius	    sc->sc_len));
1985126258Smlaier
1986223637Sbz	if (TAILQ_EMPTY(&sc->sc_qs[q]))
1987223637Sbz		nlen += sizeof(struct pfsync_subheader);
1988130613Smlaier
1989223637Sbz	if (sc->sc_len + nlen > sc->sc_ifp->if_mtu) {
1990240233Sglebius		pfsync_sendout(1);
1991126258Smlaier
1992223637Sbz		nlen = sizeof(struct pfsync_subheader) + pfsync_qs[q].len;
1993130613Smlaier	}
1994126258Smlaier
1995223637Sbz	sc->sc_len += nlen;
1996223637Sbz	TAILQ_INSERT_TAIL(&sc->sc_qs[q], st, sync_list);
1997223637Sbz	st->sync_state = q;
1998240233Sglebius	pf_ref_state(st);
1999171168Smlaier}
2000171168Smlaier
2001240233Sglebiusstatic void
2002223637Sbzpfsync_q_del(struct pf_state *st)
2003171168Smlaier{
2004223637Sbz	struct pfsync_softc *sc = V_pfsyncif;
2005223637Sbz	int q = st->sync_state;
2006171168Smlaier
2007240233Sglebius	PFSYNC_LOCK_ASSERT(sc);
2008229964Sglebius	KASSERT(st->sync_state != PFSYNC_S_NONE,
2009240233Sglebius		("%s: st->sync_state != PFSYNC_S_NONE", __func__));
2010171168Smlaier
2011223637Sbz	sc->sc_len -= pfsync_qs[q].len;
2012223637Sbz	TAILQ_REMOVE(&sc->sc_qs[q], st, sync_list);
2013223637Sbz	st->sync_state = PFSYNC_S_NONE;
2014240233Sglebius	pf_release_state(st);
2015171168Smlaier
2016223637Sbz	if (TAILQ_EMPTY(&sc->sc_qs[q]))
2017223637Sbz		sc->sc_len -= sizeof(struct pfsync_subheader);
2018223637Sbz}
2019223637Sbz
2020240233Sglebiusstatic void
2021223637Sbzpfsync_bulk_start(void)
2022223637Sbz{
2023223637Sbz	struct pfsync_softc *sc = V_pfsyncif;
2024223637Sbz
2025226663Sglebius	if (V_pf_status.debug >= PF_DEBUG_MISC)
2026226663Sglebius		printf("pfsync: received bulk update request\n");
2027223637Sbz
2028240233Sglebius	PFSYNC_BLOCK(sc);
2029223637Sbz
2030240233Sglebius	sc->sc_ureq_received = time_uptime;
2031240233Sglebius	sc->sc_bulk_hashid = 0;
2032240233Sglebius	sc->sc_bulk_stateid = 0;
2033240233Sglebius	pfsync_bulk_status(PFSYNC_BUS_START);
2034240233Sglebius	callout_reset(&sc->sc_bulk_tmo, 1, pfsync_bulk_update, sc);
2035240233Sglebius	PFSYNC_BUNLOCK(sc);
2036223637Sbz}
2037223637Sbz
2038240233Sglebiusstatic void
2039223637Sbzpfsync_bulk_update(void *arg)
2040223637Sbz{
2041223637Sbz	struct pfsync_softc *sc = arg;
2042240233Sglebius	struct pf_state *s;
2043240233Sglebius	int i, sent = 0;
2044223637Sbz
2045240233Sglebius	PFSYNC_BLOCK_ASSERT(sc);
2046223637Sbz	CURVNET_SET(sc->sc_ifp->if_vnet);
2047223637Sbz
2048240233Sglebius	/*
2049240233Sglebius	 * Start with last state from previous invocation.
2050240233Sglebius	 * It may had gone, in this case start from the
2051240233Sglebius	 * hash slot.
2052240233Sglebius	 */
2053240233Sglebius	s = pf_find_state_byid(sc->sc_bulk_stateid, sc->sc_bulk_creatorid);
2054223637Sbz
2055240233Sglebius	if (s != NULL)
2056240233Sglebius		i = PF_IDHASH(s);
2057240233Sglebius	else
2058240233Sglebius		i = sc->sc_bulk_hashid;
2059240233Sglebius
2060273736Shselasky	for (; i <= pf_hashmask; i++) {
2061240233Sglebius		struct pf_idhash *ih = &V_pf_idhash[i];
2062240233Sglebius
2063240233Sglebius		if (s != NULL)
2064240233Sglebius			PF_HASHROW_ASSERT(ih);
2065240233Sglebius		else {
2066240233Sglebius			PF_HASHROW_LOCK(ih);
2067240233Sglebius			s = LIST_FIRST(&ih->states);
2068226663Sglebius		}
2069226663Sglebius
2070240233Sglebius		for (; s; s = LIST_NEXT(s, entry)) {
2071240233Sglebius
2072240233Sglebius			if (sent > 1 && (sc->sc_ifp->if_mtu - sc->sc_len) <
2073240233Sglebius			    sizeof(struct pfsync_state)) {
2074240233Sglebius				/* We've filled a packet. */
2075240233Sglebius				sc->sc_bulk_hashid = i;
2076240233Sglebius				sc->sc_bulk_stateid = s->id;
2077240233Sglebius				sc->sc_bulk_creatorid = s->creatorid;
2078240233Sglebius				PF_HASHROW_UNLOCK(ih);
2079240233Sglebius				callout_reset(&sc->sc_bulk_tmo, 1,
2080240233Sglebius				    pfsync_bulk_update, sc);
2081240233Sglebius				goto full;
2082240233Sglebius			}
2083240233Sglebius
2084240233Sglebius			if (s->sync_state == PFSYNC_S_NONE &&
2085240233Sglebius			    s->timeout < PFTM_MAX &&
2086240233Sglebius			    s->pfsync_time <= sc->sc_ureq_received) {
2087240233Sglebius				pfsync_update_state_req(s);
2088240233Sglebius				sent++;
2089240233Sglebius			}
2090223637Sbz		}
2091240233Sglebius		PF_HASHROW_UNLOCK(ih);
2092226663Sglebius	}
2093130613Smlaier
2094240233Sglebius	/* We're done. */
2095240233Sglebius	pfsync_bulk_status(PFSYNC_BUS_END);
2096240233Sglebius
2097240233Sglebiusfull:
2098223637Sbz	CURVNET_RESTORE();
2099223637Sbz}
2100223637Sbz
2101240233Sglebiusstatic void
2102223637Sbzpfsync_bulk_status(u_int8_t status)
2103223637Sbz{
2104223637Sbz	struct {
2105223637Sbz		struct pfsync_subheader subh;
2106223637Sbz		struct pfsync_bus bus;
2107223637Sbz	} __packed r;
2108223637Sbz
2109223637Sbz	struct pfsync_softc *sc = V_pfsyncif;
2110130613Smlaier
2111223637Sbz	bzero(&r, sizeof(r));
2112171168Smlaier
2113223637Sbz	r.subh.action = PFSYNC_ACT_BUS;
2114223637Sbz	r.subh.count = htons(1);
2115240233Sglebius	V_pfsyncstats.pfsyncs_oacts[PFSYNC_ACT_BUS]++;
2116223637Sbz
2117223637Sbz	r.bus.creatorid = V_pf_status.hostid;
2118223637Sbz	r.bus.endtime = htonl(time_uptime - sc->sc_ureq_received);
2119223637Sbz	r.bus.status = status;
2120130613Smlaier
2121240233Sglebius	PFSYNC_LOCK(sc);
2122223637Sbz	pfsync_send_plus(&r, sizeof(r));
2123240233Sglebius	PFSYNC_UNLOCK(sc);
2124126258Smlaier}
2125126261Smlaier
2126240233Sglebiusstatic void
2127223637Sbzpfsync_bulk_fail(void *arg)
2128171168Smlaier{
2129223637Sbz	struct pfsync_softc *sc = arg;
2130171168Smlaier
2131223637Sbz	CURVNET_SET(sc->sc_ifp->if_vnet);
2132171168Smlaier
2133240233Sglebius	PFSYNC_BLOCK_ASSERT(sc);
2134240233Sglebius
2135223637Sbz	if (sc->sc_bulk_tries++ < PFSYNC_MAX_BULKTRIES) {
2136223637Sbz		/* Try again */
2137223637Sbz		callout_reset(&sc->sc_bulkfail_tmo, 5 * hz,
2138223637Sbz		    pfsync_bulk_fail, V_pfsyncif);
2139240233Sglebius		PFSYNC_LOCK(sc);
2140223637Sbz		pfsync_request_update(0, 0);
2141240233Sglebius		PFSYNC_UNLOCK(sc);
2142223637Sbz	} else {
2143240233Sglebius		/* Pretend like the transfer was ok. */
2144223637Sbz		sc->sc_ureq_sent = 0;
2145223637Sbz		sc->sc_bulk_tries = 0;
2146240233Sglebius		PFSYNC_LOCK(sc);
2147240233Sglebius		if (!(sc->sc_flags & PFSYNCF_OK) && carp_demote_adj_p)
2148228736Sglebius			(*carp_demote_adj_p)(-V_pfsync_carp_adj,
2149228736Sglebius			    "pfsync bulk fail");
2150240233Sglebius		sc->sc_flags |= PFSYNCF_OK;
2151240233Sglebius		PFSYNC_UNLOCK(sc);
2152223637Sbz		if (V_pf_status.debug >= PF_DEBUG_MISC)
2153223637Sbz			printf("pfsync: failed to receive bulk update\n");
2154223637Sbz	}
2155171168Smlaier
2156223637Sbz	CURVNET_RESTORE();
2157223637Sbz}
2158171168Smlaier
2159240233Sglebiusstatic void
2160223637Sbzpfsync_send_plus(void *plus, size_t pluslen)
2161223637Sbz{
2162223637Sbz	struct pfsync_softc *sc = V_pfsyncif;
2163223637Sbz
2164240233Sglebius	PFSYNC_LOCK_ASSERT(sc);
2165226544Sbz
2166240233Sglebius	if (sc->sc_len + pluslen > sc->sc_ifp->if_mtu)
2167240233Sglebius		pfsync_sendout(1);
2168223637Sbz
2169223637Sbz	sc->sc_plus = plus;
2170223637Sbz	sc->sc_len += (sc->sc_pluslen = pluslen);
2171223637Sbz
2172240233Sglebius	pfsync_sendout(1);
2173171168Smlaier}
2174171168Smlaier
2175240233Sglebiusstatic void
2176223637Sbzpfsync_timeout(void *arg)
2177223637Sbz{
2178223637Sbz	struct pfsync_softc *sc = arg;
2179223637Sbz
2180223637Sbz	CURVNET_SET(sc->sc_ifp->if_vnet);
2181240233Sglebius	PFSYNC_LOCK(sc);
2182240233Sglebius	pfsync_push(sc);
2183240233Sglebius	PFSYNC_UNLOCK(sc);
2184240233Sglebius	CURVNET_RESTORE();
2185240233Sglebius}
2186223637Sbz
2187240233Sglebiusstatic void
2188240233Sglebiuspfsync_push(struct pfsync_softc *sc)
2189240233Sglebius{
2190223637Sbz
2191240233Sglebius	PFSYNC_LOCK_ASSERT(sc);
2192171168Smlaier
2193240233Sglebius	sc->sc_flags |= PFSYNCF_PUSH;
2194240233Sglebius	swi_sched(V_pfsync_swi_cookie, 0);
2195223637Sbz}
2196171168Smlaier
2197240233Sglebiusstatic void
2198223637Sbzpfsyncintr(void *arg)
2199226660Sglebius{
2200226660Sglebius	struct pfsync_softc *sc = arg;
2201226831Sglebius	struct mbuf *m, *n;
2202226660Sglebius
2203226660Sglebius	CURVNET_SET(sc->sc_ifp->if_vnet);
2204226660Sglebius
2205240233Sglebius	PFSYNC_LOCK(sc);
2206240233Sglebius	if ((sc->sc_flags & PFSYNCF_PUSH) && sc->sc_len > PFSYNC_MINPKT) {
2207240233Sglebius		pfsync_sendout(0);
2208240233Sglebius		sc->sc_flags &= ~PFSYNCF_PUSH;
2209240233Sglebius	}
2210229976Sglebius	_IF_DEQUEUE_ALL(&sc->sc_ifp->if_snd, m);
2211240233Sglebius	PFSYNC_UNLOCK(sc);
2212226660Sglebius
2213226831Sglebius	for (; m != NULL; m = n) {
2214226831Sglebius
2215226831Sglebius		n = m->m_nextpkt;
2216226831Sglebius		m->m_nextpkt = NULL;
2217240233Sglebius
2218240233Sglebius		/*
2219240233Sglebius		 * We distinguish between a deferral packet and our
2220240233Sglebius		 * own pfsync packet based on M_SKIP_FIREWALL
2221240233Sglebius		 * flag. This is XXX.
2222240233Sglebius		 */
2223240233Sglebius		if (m->m_flags & M_SKIP_FIREWALL)
2224240233Sglebius			ip_output(m, NULL, NULL, 0, NULL, NULL);
2225240233Sglebius		else if (ip_output(m, NULL, NULL, IP_RAWOUTPUT, &sc->sc_imo,
2226240233Sglebius		    NULL) == 0)
2227226660Sglebius			V_pfsyncstats.pfsyncs_opackets++;
2228226660Sglebius		else
2229226660Sglebius			V_pfsyncstats.pfsyncs_oerrors++;
2230226660Sglebius	}
2231226660Sglebius	CURVNET_RESTORE();
2232226660Sglebius}
2233171168Smlaier
2234229850Sglebiusstatic int
2235240233Sglebiuspfsync_multicast_setup(struct pfsync_softc *sc, struct ifnet *ifp, void *mship)
2236167710Sbms{
2237229850Sglebius	struct ip_moptions *imo = &sc->sc_imo;
2238229850Sglebius	int error;
2239167710Sbms
2240240233Sglebius	if (!(ifp->if_flags & IFF_MULTICAST))
2241229850Sglebius		return (EADDRNOTAVAIL);
2242167710Sbms
2243240233Sglebius	imo->imo_membership = (struct in_multi **)mship;
2244229850Sglebius	imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
2245229850Sglebius	imo->imo_multicast_vif = -1;
2246223637Sbz
2247240233Sglebius	if ((error = in_joingroup(ifp, &sc->sc_sync_peer, NULL,
2248229850Sglebius	    &imo->imo_membership[0])) != 0) {
2249240233Sglebius		imo->imo_membership = NULL;
2250229850Sglebius		return (error);
2251167710Sbms	}
2252229850Sglebius	imo->imo_num_memberships++;
2253240233Sglebius	imo->imo_multicast_ifp = ifp;
2254229850Sglebius	imo->imo_multicast_ttl = PFSYNC_DFLTTL;
2255229850Sglebius	imo->imo_multicast_loop = 0;
2256167710Sbms
2257229850Sglebius	return (0);
2258167710Sbms}
2259167710Sbms
2260229850Sglebiusstatic void
2261229850Sglebiuspfsync_multicast_cleanup(struct pfsync_softc *sc)
2262229850Sglebius{
2263229850Sglebius	struct ip_moptions *imo = &sc->sc_imo;
2264229850Sglebius
2265229850Sglebius	in_leavegroup(imo->imo_membership[0], NULL);
2266229850Sglebius	free(imo->imo_membership, M_PFSYNC);
2267229850Sglebius	imo->imo_membership = NULL;
2268229850Sglebius	imo->imo_multicast_ifp = NULL;
2269229850Sglebius}
2270229850Sglebius
2271229850Sglebius#ifdef INET
2272229850Sglebiusextern  struct domain inetdomain;
2273229850Sglebiusstatic struct protosw in_pfsync_protosw = {
2274229964Sglebius	.pr_type =		SOCK_RAW,
2275229964Sglebius	.pr_domain =		&inetdomain,
2276229964Sglebius	.pr_protocol =		IPPROTO_PFSYNC,
2277229964Sglebius	.pr_flags =		PR_ATOMIC|PR_ADDR,
2278229964Sglebius	.pr_input =		pfsync_input,
2279229964Sglebius	.pr_output =		(pr_output_t *)rip_output,
2280229964Sglebius	.pr_ctloutput =		rip_ctloutput,
2281229964Sglebius	.pr_usrreqs =		&rip_usrreqs
2282229850Sglebius};
2283229850Sglebius#endif
2284229850Sglebius
2285241057Sglebiusstatic void
2286241057Sglebiuspfsync_pointers_init()
2287241057Sglebius{
2288241057Sglebius
2289241057Sglebius	PF_RULES_WLOCK();
2290241057Sglebius	pfsync_state_import_ptr = pfsync_state_import;
2291241057Sglebius	pfsync_insert_state_ptr = pfsync_insert_state;
2292241057Sglebius	pfsync_update_state_ptr = pfsync_update_state;
2293241057Sglebius	pfsync_delete_state_ptr = pfsync_delete_state;
2294241057Sglebius	pfsync_clear_states_ptr = pfsync_clear_states;
2295241057Sglebius	pfsync_defer_ptr = pfsync_defer;
2296241057Sglebius	PF_RULES_WUNLOCK();
2297241057Sglebius}
2298241057Sglebius
2299241057Sglebiusstatic void
2300241057Sglebiuspfsync_pointers_uninit()
2301241057Sglebius{
2302241057Sglebius
2303241057Sglebius	PF_RULES_WLOCK();
2304241057Sglebius	pfsync_state_import_ptr = NULL;
2305241057Sglebius	pfsync_insert_state_ptr = NULL;
2306241057Sglebius	pfsync_update_state_ptr = NULL;
2307241057Sglebius	pfsync_delete_state_ptr = NULL;
2308241057Sglebius	pfsync_clear_states_ptr = NULL;
2309241057Sglebius	pfsync_defer_ptr = NULL;
2310241057Sglebius	PF_RULES_WUNLOCK();
2311241057Sglebius}
2312241057Sglebius
2313223637Sbzstatic int
2314229850Sglebiuspfsync_init()
2315147261Smlaier{
2316229850Sglebius	VNET_ITERATOR_DECL(vnet_iter);
2317223637Sbz	int error = 0;
2318126261Smlaier
2319229850Sglebius	VNET_LIST_RLOCK();
2320229850Sglebius	VNET_FOREACH(vnet_iter) {
2321229850Sglebius		CURVNET_SET(vnet_iter);
2322241610Sglebius		V_pfsync_cloner = if_clone_simple(pfsyncname,
2323241610Sglebius		    pfsync_clone_create, pfsync_clone_destroy, 1);
2324241610Sglebius		error = swi_add(NULL, pfsyncname, pfsyncintr, V_pfsyncif,
2325229850Sglebius		    SWI_NET, INTR_MPSAFE, &V_pfsync_swi_cookie);
2326229850Sglebius		CURVNET_RESTORE();
2327229850Sglebius		if (error)
2328229850Sglebius			goto fail_locked;
2329229850Sglebius	}
2330229850Sglebius	VNET_LIST_RUNLOCK();
2331229850Sglebius#ifdef INET
2332229850Sglebius	error = pf_proto_register(PF_INET, &in_pfsync_protosw);
2333223637Sbz	if (error)
2334229850Sglebius		goto fail;
2335229850Sglebius	error = ipproto_register(IPPROTO_PFSYNC);
2336229850Sglebius	if (error) {
2337229850Sglebius		pf_proto_unregister(PF_INET, IPPROTO_PFSYNC, SOCK_RAW);
2338229850Sglebius		goto fail;
2339229850Sglebius	}
2340229850Sglebius#endif
2341241057Sglebius	pfsync_pointers_init();
2342223637Sbz
2343223637Sbz	return (0);
2344229850Sglebius
2345229850Sglebiusfail:
2346229850Sglebius	VNET_LIST_RLOCK();
2347229850Sglebiusfail_locked:
2348229850Sglebius	VNET_FOREACH(vnet_iter) {
2349229850Sglebius		CURVNET_SET(vnet_iter);
2350229850Sglebius		if (V_pfsync_swi_cookie) {
2351229850Sglebius			swi_remove(V_pfsync_swi_cookie);
2352241610Sglebius			if_clone_detach(V_pfsync_cloner);
2353229850Sglebius		}
2354229850Sglebius		CURVNET_RESTORE();
2355229850Sglebius	}
2356229850Sglebius	VNET_LIST_RUNLOCK();
2357229850Sglebius
2358229850Sglebius	return (error);
2359147261Smlaier}
2360147261Smlaier
2361229850Sglebiusstatic void
2362229850Sglebiuspfsync_uninit()
2363223637Sbz{
2364229850Sglebius	VNET_ITERATOR_DECL(vnet_iter);
2365223637Sbz
2366241057Sglebius	pfsync_pointers_uninit();
2367223637Sbz
2368229850Sglebius	ipproto_unregister(IPPROTO_PFSYNC);
2369229850Sglebius	pf_proto_unregister(PF_INET, IPPROTO_PFSYNC, SOCK_RAW);
2370229850Sglebius	VNET_LIST_RLOCK();
2371229850Sglebius	VNET_FOREACH(vnet_iter) {
2372229850Sglebius		CURVNET_SET(vnet_iter);
2373241610Sglebius		if_clone_detach(V_pfsync_cloner);
2374229850Sglebius		swi_remove(V_pfsync_swi_cookie);
2375229850Sglebius		CURVNET_RESTORE();
2376229850Sglebius	}
2377229850Sglebius	VNET_LIST_RUNLOCK();
2378223637Sbz}
2379223637Sbz
2380223637Sbzstatic int
2381126261Smlaierpfsync_modevent(module_t mod, int type, void *data)
2382126261Smlaier{
2383126261Smlaier	int error = 0;
2384126261Smlaier
2385126261Smlaier	switch (type) {
2386126261Smlaier	case MOD_LOAD:
2387229850Sglebius		error = pfsync_init();
2388126261Smlaier		break;
2389229850Sglebius	case MOD_QUIESCE:
2390229850Sglebius		/*
2391229850Sglebius		 * Module should not be unloaded due to race conditions.
2392229850Sglebius		 */
2393240836Sglebius		error = EBUSY;
2394229850Sglebius		break;
2395126261Smlaier	case MOD_UNLOAD:
2396229850Sglebius		pfsync_uninit();
2397126261Smlaier		break;
2398126261Smlaier	default:
2399126261Smlaier		error = EINVAL;
2400126261Smlaier		break;
2401126261Smlaier	}
2402126261Smlaier
2403229850Sglebius	return (error);
2404126261Smlaier}
2405126261Smlaier
2406126261Smlaierstatic moduledata_t pfsync_mod = {
2407241610Sglebius	pfsyncname,
2408126261Smlaier	pfsync_modevent,
2409241394Skevlo	0
2410126261Smlaier};
2411126261Smlaier
2412126261Smlaier#define PFSYNC_MODVER 1
2413126261Smlaier
2414229853SglebiusDECLARE_MODULE(pfsync, pfsync_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
2415126261SmlaierMODULE_VERSION(pfsync, PFSYNC_MODVER);
2416223637SbzMODULE_DEPEND(pfsync, pf, PF_MODVER, PF_MODVER, PF_MODVER);
2417