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