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