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