1 /*- 2 * Copyright (c) 2009-2019 The NetBSD Foundation, Inc. 3 * All rights reserved. 4 * 5 * This material is based upon work partially supported by The 6 * NetBSD Foundation under a contract with Mindaugas Rasiukevicius. 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 /* 31 * Private NPF structures and interfaces. 32 * For internal use within NPF core only. 33 */ 34 35 #ifndef _NPF_IMPL_H_ 36 #define _NPF_IMPL_H_ 37 38 #if !defined(_KERNEL) && !defined(_NPF_STANDALONE) 39 #error "Kernel-level header only" 40 #endif 41 42 #ifdef _KERNEL_OPT 43 /* For INET/INET6 definitions. */ 44 #include "opt_inet.h" 45 #include "opt_inet6.h" 46 #endif 47 48 #ifdef _KERNEL 49 #include <sys/types.h> 50 #include <sys/queue.h> 51 52 #include <net/bpf.h> 53 #include <net/bpfjit.h> 54 #include <net/if.h> 55 #endif 56 #include <dnv.h> 57 #include <nv.h> 58 59 #include "npf.h" 60 #include "npfkern.h" 61 62 #ifdef _NPF_DEBUG 63 #define NPF_PRINTF(x) printf x 64 #else 65 #define NPF_PRINTF(x) 66 #endif 67 68 /* 69 * STRUCTURE DECLARATIONS. 70 */ 71 72 struct npf_ruleset; 73 struct npf_rule; 74 struct npf_rprocset; 75 struct npf_portmap; 76 struct npf_nat; 77 struct npf_conn; 78 79 typedef struct npf_ruleset npf_ruleset_t; 80 typedef struct npf_rule npf_rule_t; 81 typedef struct npf_portmap npf_portmap_t; 82 typedef struct npf_nat npf_nat_t; 83 typedef struct npf_rprocset npf_rprocset_t; 84 typedef struct npf_alg npf_alg_t; 85 typedef struct npf_natpolicy npf_natpolicy_t; 86 typedef struct npf_conn npf_conn_t; 87 88 struct npf_conndb; 89 struct npf_table; 90 struct npf_tableset; 91 struct npf_algset; 92 struct npf_ifmap; 93 94 typedef struct npf_conndb npf_conndb_t; 95 typedef struct npf_table npf_table_t; 96 typedef struct npf_tableset npf_tableset_t; 97 typedef struct npf_algset npf_algset_t; 98 99 #ifdef __NetBSD__ 100 typedef void ebr_t; 101 #endif 102 103 /* 104 * DEFINITIONS. 105 */ 106 107 typedef struct { 108 npf_ruleset_t * ruleset; 109 npf_ruleset_t * nat_ruleset; 110 npf_rprocset_t * rule_procs; 111 npf_tableset_t * tableset; 112 bool default_pass; 113 } npf_config_t; 114 115 typedef void (*npf_workfunc_t)(npf_t *); 116 117 typedef struct { 118 uint64_t mi_rid; 119 unsigned mi_retfl; 120 unsigned mi_di; 121 } npf_match_info_t; 122 123 /* 124 * Some artificial limits. 125 * Note: very unlikely to have many ALGs. 126 */ 127 #define NPF_MAX_RULES (1024 * 1024) 128 #define NPF_MAX_TABLES 128 129 #define NPF_MAX_RPROCS 128 130 #define NPF_MAX_IFMAP 64 131 #define NPF_MAX_ALGS 4 132 #define NPF_MAX_WORKS 4 133 134 /* 135 * CONNECTION STATE STRUCTURES 136 */ 137 138 typedef enum { 139 NPF_FLOW_FORW = 0, 140 NPF_FLOW_BACK = 1, 141 } npf_flow_t; 142 143 typedef struct { 144 uint32_t nst_end; 145 uint32_t nst_maxend; 146 uint32_t nst_maxwin; 147 int nst_wscale; 148 } npf_tcpstate_t; 149 150 typedef struct { 151 unsigned nst_state; 152 npf_tcpstate_t nst_tcpst[2]; 153 } npf_state_t; 154 155 /* 156 * ALG FUNCTIONS. 157 */ 158 159 typedef struct { 160 bool (*match)(npf_cache_t *, npf_nat_t *, int); 161 bool (*translate)(npf_cache_t *, npf_nat_t *, npf_flow_t); 162 npf_conn_t * (*inspect)(npf_cache_t *, int); 163 void (*destroy)(npf_t *, npf_nat_t *, npf_conn_t *); 164 } npfa_funcs_t; 165 166 /* 167 * NBUF STRUCTURE. 168 */ 169 170 struct nbuf { 171 struct mbuf * nb_mbuf0; 172 struct mbuf * nb_mbuf; 173 void * nb_nptr; 174 const ifnet_t * nb_ifp; 175 unsigned nb_ifid; 176 int nb_flags; 177 const npf_mbufops_t *nb_mops; 178 }; 179 180 /* 181 * PARAMS. 182 */ 183 184 typedef struct npf_paraminfo npf_paraminfo_t; 185 186 typedef struct { 187 const char * name; 188 int * valp; 189 int default_val; 190 /* 191 * Minimum and maximum allowed values (inclusive). 192 */ 193 int min; 194 int max; 195 } npf_param_t; 196 197 typedef enum { 198 NPF_PARAMS_CONN = 0, 199 NPF_PARAMS_CONNDB, 200 NPF_PARAMS_GENERIC_STATE, 201 NPF_PARAMS_TCP_STATE, 202 NPF_PARAMS_COUNT 203 } npf_paramgroup_t; 204 205 /* 206 * NPF INSTANCE (CONTEXT) STRUCTURE AND AUXILIARY OPERATIONS. 207 */ 208 209 struct npf { 210 /* Active NPF configuration. */ 211 kmutex_t config_lock; 212 ebr_t * ebr; 213 npf_config_t * config; 214 215 /* 216 * BPF byte-code context, mbuf operations an arbitrary user argument. 217 */ 218 bpf_ctx_t * bpfctx; 219 const npf_mbufops_t * mbufops; 220 void * arg; 221 222 /* Parameters. */ 223 npf_paraminfo_t * paraminfo; 224 void * params[NPF_PARAMS_COUNT]; 225 int ip4_reassembly; 226 int ip6_reassembly; 227 228 /* 229 * Connection tracking state: disabled (off) or enabled (on). 230 * Connection tracking database, connection cache and the lock. 231 * There are two caches (pools): for IPv4 and IPv6. 232 */ 233 volatile int conn_tracking; 234 kmutex_t conn_lock; 235 npf_conndb_t * conn_db; 236 pool_cache_t conn_cache[2]; 237 238 /* NAT and ALGs. */ 239 npf_portmap_t * portmap; 240 npf_algset_t * algset; 241 242 /* Interface mapping. */ 243 const npf_ifops_t * ifops; 244 struct npf_ifmap * ifmap; 245 unsigned ifmap_cnt; 246 unsigned ifmap_off; 247 kmutex_t ifmap_lock; 248 249 /* List of extensions and its lock. */ 250 LIST_HEAD(, npf_ext) ext_list; 251 kmutex_t ext_lock; 252 253 /* Associated worker information. */ 254 unsigned worker_flags; 255 LIST_ENTRY(npf) worker_entry; 256 unsigned worker_wait_time; 257 npf_workfunc_t worker_funcs[NPF_MAX_WORKS]; 258 259 /* Statistics. */ 260 percpu_t * stats_percpu; 261 }; 262 263 /* 264 * NPF extensions and rule procedure interface. 265 */ 266 267 struct npf_rproc; 268 typedef struct npf_rproc npf_rproc_t; 269 270 typedef struct { 271 u_int version; 272 void * ctx; 273 int (*ctor)(npf_rproc_t *, const nvlist_t *); 274 void (*dtor)(npf_rproc_t *, void *); 275 bool (*proc)(npf_cache_t *, void *, const npf_match_info_t *, int *); 276 } npf_ext_ops_t; 277 278 void * npf_ext_register(npf_t *, const char *, const npf_ext_ops_t *); 279 int npf_ext_unregister(npf_t *, void *); 280 void npf_rproc_assign(npf_rproc_t *, void *); 281 282 /* 283 * INTERFACES. 284 */ 285 286 /* NPF config, statistics, etc. */ 287 void npf_config_init(npf_t *); 288 void npf_config_fini(npf_t *); 289 290 npf_config_t * npf_config_enter(npf_t *); 291 void npf_config_exit(npf_t *); 292 void npf_config_sync(npf_t *); 293 bool npf_config_locked_p(npf_t *); 294 int npf_config_read_enter(npf_t *); 295 void npf_config_read_exit(npf_t *, int); 296 297 npf_config_t * npf_config_create(void); 298 void npf_config_destroy(npf_config_t *); 299 void npf_config_load(npf_t *, npf_config_t *, npf_conndb_t *, bool); 300 npf_ruleset_t * npf_config_ruleset(npf_t *npf); 301 npf_ruleset_t * npf_config_natset(npf_t *npf); 302 npf_tableset_t *npf_config_tableset(npf_t *npf); 303 bool npf_default_pass(npf_t *); 304 bool npf_active_p(void); 305 306 int npf_worker_sysinit(unsigned); 307 void npf_worker_sysfini(void); 308 int npf_worker_addfunc(npf_t *, npf_workfunc_t); 309 void npf_worker_enlist(npf_t *); 310 void npf_worker_discharge(npf_t *); 311 void npf_worker_signal(npf_t *); 312 313 int npfctl_run_op(npf_t *, unsigned, const nvlist_t *, nvlist_t *); 314 int npfctl_table(npf_t *, void *); 315 316 void npf_stats_inc(npf_t *, npf_stats_t); 317 void npf_stats_dec(npf_t *, npf_stats_t); 318 319 void npf_param_init(npf_t *); 320 void npf_param_fini(npf_t *); 321 void npf_param_register(npf_t *, npf_param_t *, unsigned); 322 void * npf_param_allocgroup(npf_t *, npf_paramgroup_t, size_t); 323 void npf_param_freegroup(npf_t *, npf_paramgroup_t, size_t); 324 int npf_param_check(npf_t *, const char *, int); 325 int npf_params_export(const npf_t *, nvlist_t *); 326 327 void npf_ifmap_init(npf_t *, const npf_ifops_t *); 328 void npf_ifmap_fini(npf_t *); 329 u_int npf_ifmap_register(npf_t *, const char *); 330 void npf_ifmap_flush(npf_t *); 331 u_int npf_ifmap_getid(npf_t *, const ifnet_t *); 332 void npf_ifmap_copylogname(npf_t *, unsigned, char *, size_t); 333 void npf_ifmap_copyname(npf_t *, unsigned, char *, size_t); 334 335 void npf_ifaddr_sync(npf_t *, ifnet_t *); 336 void npf_ifaddr_flush(npf_t *, ifnet_t *); 337 void npf_ifaddr_syncall(npf_t *); 338 339 /* Protocol helpers. */ 340 int npf_cache_all(npf_cache_t *); 341 void npf_recache(npf_cache_t *); 342 343 bool npf_rwrip(const npf_cache_t *, u_int, const npf_addr_t *); 344 bool npf_rwrport(const npf_cache_t *, u_int, const in_port_t); 345 bool npf_rwrcksum(const npf_cache_t *, u_int, 346 const npf_addr_t *, const in_port_t); 347 int npf_napt_rwr(const npf_cache_t *, u_int, const npf_addr_t *, 348 const in_addr_t); 349 int npf_npt66_rwr(const npf_cache_t *, u_int, const npf_addr_t *, 350 npf_netmask_t, uint16_t); 351 352 uint16_t npf_fixup16_cksum(uint16_t, uint16_t, uint16_t); 353 uint16_t npf_fixup32_cksum(uint16_t, uint32_t, uint32_t); 354 uint16_t npf_addr_cksum(uint16_t, int, const npf_addr_t *, 355 const npf_addr_t *); 356 uint32_t npf_addr_mix(const int, const npf_addr_t *, const npf_addr_t *); 357 int npf_addr_cmp(const npf_addr_t *, const npf_netmask_t, 358 const npf_addr_t *, const npf_netmask_t, const int); 359 void npf_addr_mask(const npf_addr_t *, const npf_netmask_t, 360 const int, npf_addr_t *); 361 void npf_addr_bitor(const npf_addr_t *, const npf_netmask_t, 362 const int, npf_addr_t *); 363 int npf_netmask_check(const int, npf_netmask_t); 364 365 int npf_tcpsaw(const npf_cache_t *, tcp_seq *, tcp_seq *, 366 uint32_t *); 367 bool npf_fetch_tcpopts(npf_cache_t *, uint16_t *, int *); 368 bool npf_set_mss(npf_cache_t *, uint16_t, uint16_t *, uint16_t *, 369 bool *); 370 bool npf_return_block(npf_cache_t *, const int); 371 372 /* BPF interface. */ 373 void npf_bpf_sysinit(void); 374 void npf_bpf_sysfini(void); 375 void npf_bpf_prepare(npf_cache_t *, bpf_args_t *, uint32_t *); 376 int npf_bpf_filter(bpf_args_t *, const void *, bpfjit_func_t); 377 void * npf_bpf_compile(void *, size_t); 378 bool npf_bpf_validate(const void *, size_t); 379 380 /* Tableset interface. */ 381 void npf_tableset_sysinit(void); 382 void npf_tableset_sysfini(void); 383 384 npf_tableset_t *npf_tableset_create(u_int); 385 void npf_tableset_destroy(npf_tableset_t *); 386 int npf_tableset_insert(npf_tableset_t *, npf_table_t *); 387 npf_table_t * npf_tableset_getbyname(npf_tableset_t *, const char *); 388 npf_table_t * npf_tableset_getbyid(npf_tableset_t *, u_int); 389 npf_table_t * npf_tableset_swap(npf_tableset_t *, npf_table_t *); 390 void npf_tableset_reload(npf_t *, npf_tableset_t *, npf_tableset_t *); 391 int npf_tableset_export(npf_t *, const npf_tableset_t *, nvlist_t *); 392 393 npf_table_t * npf_table_create(const char *, u_int, int, const void *, size_t); 394 void npf_table_destroy(npf_table_t *); 395 396 u_int npf_table_getid(npf_table_t *); 397 int npf_table_check(npf_tableset_t *, const char *, uint64_t, uint64_t, bool); 398 int npf_table_insert(npf_table_t *, const int, 399 const npf_addr_t *, const npf_netmask_t); 400 int npf_table_remove(npf_table_t *, const int, 401 const npf_addr_t *, const npf_netmask_t); 402 int npf_table_lookup(npf_table_t *, const int, const npf_addr_t *); 403 npf_addr_t * npf_table_getsome(npf_table_t *, const int, unsigned); 404 int npf_table_list(npf_table_t *, void *, size_t); 405 int npf_table_flush(npf_table_t *); 406 void npf_table_gc(npf_t *, npf_table_t *); 407 408 /* Ruleset interface. */ 409 npf_ruleset_t * npf_ruleset_create(size_t); 410 void npf_ruleset_destroy(npf_ruleset_t *); 411 void npf_ruleset_insert(npf_ruleset_t *, npf_rule_t *); 412 void npf_ruleset_reload(npf_t *, npf_ruleset_t *, 413 npf_ruleset_t *, bool); 414 npf_natpolicy_t *npf_ruleset_findnat(npf_ruleset_t *, uint64_t); 415 void npf_ruleset_freealg(npf_ruleset_t *, npf_alg_t *); 416 int npf_ruleset_export(npf_t *, const npf_ruleset_t *, 417 const char *, nvlist_t *); 418 419 npf_rule_t * npf_ruleset_lookup(npf_ruleset_t *, const char *); 420 int npf_ruleset_add(npf_ruleset_t *, const char *, npf_rule_t *); 421 int npf_ruleset_remove(npf_ruleset_t *, const char *, uint64_t); 422 int npf_ruleset_remkey(npf_ruleset_t *, const char *, 423 const void *, size_t); 424 int npf_ruleset_list(npf_t *, npf_ruleset_t *, const char *, nvlist_t *); 425 int npf_ruleset_flush(npf_ruleset_t *, const char *); 426 void npf_ruleset_gc(npf_ruleset_t *); 427 428 npf_rule_t * npf_ruleset_inspect(npf_cache_t *, const npf_ruleset_t *, 429 const int, const int); 430 int npf_rule_conclude(const npf_rule_t *, npf_match_info_t *); 431 432 /* Rule interface. */ 433 npf_rule_t * npf_rule_alloc(npf_t *, const nvlist_t *); 434 void npf_rule_setcode(npf_rule_t *, int, void *, size_t); 435 void npf_rule_setrproc(npf_rule_t *, npf_rproc_t *); 436 void npf_rule_free(npf_rule_t *); 437 uint64_t npf_rule_getid(const npf_rule_t *); 438 npf_natpolicy_t *npf_rule_getnat(const npf_rule_t *); 439 void npf_rule_setnat(npf_rule_t *, npf_natpolicy_t *); 440 npf_rproc_t * npf_rule_getrproc(const npf_rule_t *); 441 442 void npf_ext_init(npf_t *); 443 void npf_ext_fini(npf_t *); 444 int npf_ext_construct(npf_t *, const char *, 445 npf_rproc_t *, const nvlist_t *); 446 447 npf_rprocset_t *npf_rprocset_create(void); 448 void npf_rprocset_destroy(npf_rprocset_t *); 449 npf_rproc_t * npf_rprocset_lookup(npf_rprocset_t *, const char *); 450 void npf_rprocset_insert(npf_rprocset_t *, npf_rproc_t *); 451 int npf_rprocset_export(const npf_rprocset_t *, nvlist_t *); 452 453 npf_rproc_t * npf_rproc_create(const nvlist_t *); 454 void npf_rproc_acquire(npf_rproc_t *); 455 void npf_rproc_release(npf_rproc_t *); 456 const char * npf_rproc_getname(const npf_rproc_t *); 457 bool npf_rproc_run(npf_cache_t *, npf_rproc_t *, 458 const npf_match_info_t *, int *); 459 460 /* State handling. */ 461 void npf_state_sysinit(npf_t *); 462 void npf_state_sysfini(npf_t *); 463 464 bool npf_state_init(npf_cache_t *, npf_state_t *); 465 bool npf_state_inspect(npf_cache_t *, npf_state_t *, npf_flow_t); 466 int npf_state_etime(npf_t *, const npf_state_t *, const int); 467 void npf_state_destroy(npf_state_t *); 468 469 void npf_state_tcp_sysinit(npf_t *); 470 void npf_state_tcp_sysfini(npf_t *); 471 bool npf_state_tcp(npf_cache_t *, npf_state_t *, npf_flow_t); 472 int npf_state_tcp_timeout(npf_t *, const npf_state_t *); 473 474 /* Portmap. */ 475 void npf_portmap_sysinit(void); 476 void npf_portmap_sysfini(void); 477 478 void npf_portmap_init(npf_t *); 479 void npf_portmap_fini(npf_t *); 480 481 npf_portmap_t * npf_portmap_create(int, int); 482 void npf_portmap_destroy(npf_portmap_t *); 483 484 in_port_t npf_portmap_get(npf_portmap_t *, int, const npf_addr_t *); 485 bool npf_portmap_take(npf_portmap_t *, int, const npf_addr_t *, in_port_t); 486 void npf_portmap_put(npf_portmap_t *, int, const npf_addr_t *, in_port_t); 487 void npf_portmap_flush(npf_portmap_t *); 488 489 /* NAT. */ 490 void npf_nat_sysinit(void); 491 void npf_nat_sysfini(void); 492 npf_natpolicy_t *npf_natpolicy_create(npf_t *, const nvlist_t *, npf_ruleset_t *); 493 int npf_natpolicy_export(const npf_natpolicy_t *, nvlist_t *); 494 void npf_natpolicy_destroy(npf_natpolicy_t *); 495 bool npf_natpolicy_cmp(npf_natpolicy_t *, npf_natpolicy_t *); 496 void npf_nat_setid(npf_natpolicy_t *, uint64_t); 497 uint64_t npf_nat_getid(const npf_natpolicy_t *); 498 void npf_nat_freealg(npf_natpolicy_t *, npf_alg_t *); 499 500 int npf_do_nat(npf_cache_t *, npf_conn_t *, const unsigned); 501 npf_nat_t * npf_nat_share_policy(npf_cache_t *, npf_conn_t *, npf_nat_t *); 502 void npf_nat_destroy(npf_conn_t *, npf_nat_t *); 503 void npf_nat_getorig(npf_nat_t *, npf_addr_t **, in_port_t *); 504 void npf_nat_gettrans(npf_nat_t *, npf_addr_t **, in_port_t *); 505 void npf_nat_setalg(npf_nat_t *, npf_alg_t *, uintptr_t); 506 npf_alg_t * npf_nat_getalg(const npf_nat_t *); 507 uintptr_t npf_nat_getalgarg(const npf_nat_t *); 508 509 void npf_nat_export(npf_t *, const npf_nat_t *, nvlist_t *); 510 npf_nat_t * npf_nat_import(npf_t *, const nvlist_t *, npf_ruleset_t *, 511 npf_conn_t *); 512 513 /* ALG interface. */ 514 void npf_alg_sysinit(void); 515 void npf_alg_sysfini(void); 516 void npf_alg_init(npf_t *); 517 void npf_alg_fini(npf_t *); 518 npf_alg_t * npf_alg_register(npf_t *, const char *, const npfa_funcs_t *); 519 int npf_alg_unregister(npf_t *, npf_alg_t *); 520 npf_alg_t * npf_alg_construct(npf_t *, const char *); 521 bool npf_alg_match(npf_cache_t *, npf_nat_t *, int); 522 void npf_alg_exec(npf_cache_t *, npf_nat_t *, const npf_flow_t); 523 npf_conn_t * npf_alg_conn(npf_cache_t *, int); 524 int npf_alg_export(npf_t *, nvlist_t *); 525 void npf_alg_destroy(npf_t *, npf_alg_t *, npf_nat_t *, npf_conn_t *); 526 527 /* Wrappers for the reclamation mechanism. */ 528 ebr_t * npf_ebr_create(void); 529 void npf_ebr_destroy(ebr_t *); 530 void npf_ebr_register(ebr_t *); 531 void npf_ebr_unregister(ebr_t *); 532 int npf_ebr_enter(ebr_t *); 533 void npf_ebr_exit(ebr_t *, int); 534 void npf_ebr_full_sync(ebr_t *); 535 bool npf_ebr_incrit_p(ebr_t *); 536 537 /* Debugging routines. */ 538 const char * npf_addr_dump(const npf_addr_t *, int); 539 void npf_state_dump(const npf_state_t *); 540 void npf_nat_dump(const npf_nat_t *); 541 void npf_ruleset_dump(npf_t *, const char *); 542 void npf_state_setsampler(void (*)(npf_state_t *, bool)); 543 544 /* In-kernel routines. */ 545 void npf_setkernctx(npf_t *); 546 npf_t * npf_getkernctx(void); 547 548 #endif /* _NPF_IMPL_H_ */ 549