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 #define NPF_FLOW_FORW 0 139 #define NPF_FLOW_BACK 1 140 141 typedef struct { 142 uint32_t nst_end; 143 uint32_t nst_maxend; 144 uint32_t nst_maxwin; 145 int nst_wscale; 146 } npf_tcpstate_t; 147 148 typedef struct { 149 unsigned nst_state; 150 npf_tcpstate_t nst_tcpst[2]; 151 } npf_state_t; 152 153 /* 154 * ALG FUNCTIONS. 155 */ 156 157 typedef struct { 158 bool (*match)(npf_cache_t *, npf_nat_t *, int); 159 bool (*translate)(npf_cache_t *, npf_nat_t *, bool); 160 npf_conn_t * (*inspect)(npf_cache_t *, int); 161 } npfa_funcs_t; 162 163 /* 164 * NBUF STRUCTURE. 165 */ 166 167 struct nbuf { 168 struct mbuf * nb_mbuf0; 169 struct mbuf * nb_mbuf; 170 void * nb_nptr; 171 const ifnet_t * nb_ifp; 172 unsigned nb_ifid; 173 int nb_flags; 174 const npf_mbufops_t *nb_mops; 175 }; 176 177 /* 178 * PARAMS. 179 */ 180 181 typedef struct npf_paraminfo npf_paraminfo_t; 182 183 typedef struct { 184 const char * name; 185 int * valp; 186 int default_val; 187 /* 188 * Minimum and maximum allowed values (inclusive). 189 */ 190 int min; 191 int max; 192 } npf_param_t; 193 194 typedef enum { 195 NPF_PARAMS_CONNDB = 0, 196 NPF_PARAMS_GENERIC_STATE, 197 NPF_PARAMS_TCP_STATE, 198 NPF_PARAMS_COUNT 199 } npf_paramgroup_t; 200 201 /* 202 * NPF INSTANCE (CONTEXT) STRUCTURE AND AUXILIARY OPERATIONS. 203 */ 204 205 struct npf { 206 /* Active NPF configuration. */ 207 kmutex_t config_lock; 208 ebr_t * ebr; 209 npf_config_t * config; 210 211 /* BPF byte-code context. */ 212 bpf_ctx_t * bpfctx; 213 const npf_mbufops_t * mbufops; 214 215 /* Parameters. */ 216 npf_paraminfo_t * paraminfo; 217 void * params[NPF_PARAMS_COUNT]; 218 219 /* 220 * Connection tracking state: disabled (off) or enabled (on). 221 * Connection tracking database, connection cache and the lock. 222 * There are two caches (pools): for IPv4 and IPv6. 223 */ 224 volatile int conn_tracking; 225 kmutex_t conn_lock; 226 npf_conndb_t * conn_db; 227 pool_cache_t conn_cache[2]; 228 229 /* NAT and ALGs. */ 230 npf_portmap_t * portmap; 231 npf_algset_t * algset; 232 233 /* Interface mapping. */ 234 const npf_ifops_t * ifops; 235 struct npf_ifmap * ifmap; 236 unsigned ifmap_cnt; 237 unsigned ifmap_off; 238 kmutex_t ifmap_lock; 239 240 /* Associated worker thread. */ 241 unsigned worker_id; 242 void * worker_entry; 243 bool sync_registered; 244 245 /* List of extensions and its lock. */ 246 LIST_HEAD(, npf_ext) ext_list; 247 kmutex_t ext_lock; 248 249 /* Statistics. */ 250 percpu_t * stats_percpu; 251 }; 252 253 /* 254 * NPF extensions and rule procedure interface. 255 */ 256 257 struct npf_rproc; 258 typedef struct npf_rproc npf_rproc_t; 259 260 typedef struct { 261 u_int version; 262 void * ctx; 263 int (*ctor)(npf_rproc_t *, const nvlist_t *); 264 void (*dtor)(npf_rproc_t *, void *); 265 bool (*proc)(npf_cache_t *, void *, const npf_match_info_t *, int *); 266 } npf_ext_ops_t; 267 268 void * npf_ext_register(npf_t *, const char *, const npf_ext_ops_t *); 269 int npf_ext_unregister(npf_t *, void *); 270 void npf_rproc_assign(npf_rproc_t *, void *); 271 272 /* 273 * INTERFACES. 274 */ 275 276 /* NPF config, statistics, etc. */ 277 void npf_config_init(npf_t *); 278 void npf_config_fini(npf_t *); 279 280 npf_config_t * npf_config_enter(npf_t *); 281 void npf_config_exit(npf_t *); 282 void npf_config_sync(npf_t *); 283 bool npf_config_locked_p(npf_t *); 284 int npf_config_read_enter(npf_t *); 285 void npf_config_read_exit(npf_t *, int); 286 287 npf_config_t * npf_config_create(void); 288 void npf_config_destroy(npf_config_t *); 289 void npf_config_load(npf_t *, npf_config_t *, npf_conndb_t *, bool); 290 npf_ruleset_t * npf_config_ruleset(npf_t *npf); 291 npf_ruleset_t * npf_config_natset(npf_t *npf); 292 npf_tableset_t *npf_config_tableset(npf_t *npf); 293 bool npf_default_pass(npf_t *); 294 bool npf_active_p(void); 295 296 int npf_worker_sysinit(unsigned); 297 void npf_worker_sysfini(void); 298 void npf_worker_signal(npf_t *); 299 void npf_worker_register(npf_t *, npf_workfunc_t); 300 void npf_worker_unregister(npf_t *, npf_workfunc_t); 301 302 int npfctl_save(npf_t *, u_long, void *); 303 int npfctl_load(npf_t *, u_long, void *); 304 int npfctl_rule(npf_t *, u_long, void *); 305 int npfctl_conn_lookup(npf_t *, u_long, void *); 306 int npfctl_table_replace(npf_t *, u_long, void *); 307 int npfctl_table(npf_t *, void *); 308 309 void npf_stats_inc(npf_t *, npf_stats_t); 310 void npf_stats_dec(npf_t *, npf_stats_t); 311 312 void npf_param_init(npf_t *); 313 void npf_param_fini(npf_t *); 314 void npf_param_register(npf_t *, npf_param_t *, unsigned); 315 void * npf_param_allocgroup(npf_t *, npf_paramgroup_t, size_t); 316 void npf_param_freegroup(npf_t *, npf_paramgroup_t, size_t); 317 int npf_param_check(npf_t *, const char *, int); 318 319 void npf_ifmap_init(npf_t *, const npf_ifops_t *); 320 void npf_ifmap_fini(npf_t *); 321 u_int npf_ifmap_register(npf_t *, const char *); 322 void npf_ifmap_flush(npf_t *); 323 u_int npf_ifmap_getid(npf_t *, const ifnet_t *); 324 void npf_ifmap_copylogname(npf_t *, unsigned, char *, size_t); 325 void npf_ifmap_copyname(npf_t *, unsigned, char *, size_t); 326 327 void npf_ifaddr_sync(npf_t *, ifnet_t *); 328 void npf_ifaddr_flush(npf_t *, ifnet_t *); 329 void npf_ifaddr_syncall(npf_t *); 330 331 /* Protocol helpers. */ 332 int npf_cache_all(npf_cache_t *); 333 void npf_recache(npf_cache_t *); 334 335 bool npf_rwrip(const npf_cache_t *, u_int, const npf_addr_t *); 336 bool npf_rwrport(const npf_cache_t *, u_int, const in_port_t); 337 bool npf_rwrcksum(const npf_cache_t *, u_int, 338 const npf_addr_t *, const in_port_t); 339 int npf_napt_rwr(const npf_cache_t *, u_int, const npf_addr_t *, 340 const in_addr_t); 341 int npf_npt66_rwr(const npf_cache_t *, u_int, const npf_addr_t *, 342 npf_netmask_t, uint16_t); 343 344 uint16_t npf_fixup16_cksum(uint16_t, uint16_t, uint16_t); 345 uint16_t npf_fixup32_cksum(uint16_t, uint32_t, uint32_t); 346 uint16_t npf_addr_cksum(uint16_t, int, const npf_addr_t *, 347 const npf_addr_t *); 348 uint32_t npf_addr_mix(const int, const npf_addr_t *, const npf_addr_t *); 349 int npf_addr_cmp(const npf_addr_t *, const npf_netmask_t, 350 const npf_addr_t *, const npf_netmask_t, const int); 351 void npf_addr_mask(const npf_addr_t *, const npf_netmask_t, 352 const int, npf_addr_t *); 353 void npf_addr_bitor(const npf_addr_t *, const npf_netmask_t, 354 const int, npf_addr_t *); 355 int npf_netmask_check(const int, npf_netmask_t); 356 357 int npf_tcpsaw(const npf_cache_t *, tcp_seq *, tcp_seq *, 358 uint32_t *); 359 bool npf_fetch_tcpopts(npf_cache_t *, uint16_t *, int *); 360 bool npf_set_mss(npf_cache_t *, uint16_t, uint16_t *, uint16_t *, 361 bool *); 362 bool npf_return_block(npf_cache_t *, const int); 363 364 /* BPF interface. */ 365 void npf_bpf_sysinit(void); 366 void npf_bpf_sysfini(void); 367 void npf_bpf_prepare(npf_cache_t *, bpf_args_t *, uint32_t *); 368 int npf_bpf_filter(bpf_args_t *, const void *, bpfjit_func_t); 369 void * npf_bpf_compile(void *, size_t); 370 bool npf_bpf_validate(const void *, size_t); 371 372 /* Tableset interface. */ 373 void npf_tableset_sysinit(void); 374 void npf_tableset_sysfini(void); 375 376 npf_tableset_t *npf_tableset_create(u_int); 377 void npf_tableset_destroy(npf_tableset_t *); 378 int npf_tableset_insert(npf_tableset_t *, npf_table_t *); 379 npf_table_t * npf_tableset_getbyname(npf_tableset_t *, const char *); 380 npf_table_t * npf_tableset_getbyid(npf_tableset_t *, u_int); 381 npf_table_t * npf_tableset_swap(npf_tableset_t *, npf_table_t *); 382 void npf_tableset_reload(npf_t *, npf_tableset_t *, npf_tableset_t *); 383 int npf_tableset_export(npf_t *, const npf_tableset_t *, nvlist_t *); 384 385 npf_table_t * npf_table_create(const char *, u_int, int, const void *, size_t); 386 void npf_table_destroy(npf_table_t *); 387 388 u_int npf_table_getid(npf_table_t *); 389 int npf_table_check(npf_tableset_t *, const char *, uint64_t, uint64_t, bool); 390 int npf_table_insert(npf_table_t *, const int, 391 const npf_addr_t *, const npf_netmask_t); 392 int npf_table_remove(npf_table_t *, const int, 393 const npf_addr_t *, const npf_netmask_t); 394 int npf_table_lookup(npf_table_t *, const int, const npf_addr_t *); 395 npf_addr_t * npf_table_getsome(npf_table_t *, const int, unsigned); 396 int npf_table_list(npf_table_t *, void *, size_t); 397 int npf_table_flush(npf_table_t *); 398 void npf_table_gc(npf_t *, npf_table_t *); 399 400 /* Ruleset interface. */ 401 npf_ruleset_t * npf_ruleset_create(size_t); 402 void npf_ruleset_destroy(npf_ruleset_t *); 403 void npf_ruleset_insert(npf_ruleset_t *, npf_rule_t *); 404 void npf_ruleset_reload(npf_t *, npf_ruleset_t *, 405 npf_ruleset_t *, bool); 406 npf_natpolicy_t *npf_ruleset_findnat(npf_ruleset_t *, uint64_t); 407 void npf_ruleset_freealg(npf_ruleset_t *, npf_alg_t *); 408 int npf_ruleset_export(npf_t *, const npf_ruleset_t *, 409 const char *, nvlist_t *); 410 411 npf_rule_t * npf_ruleset_lookup(npf_ruleset_t *, const char *); 412 int npf_ruleset_add(npf_ruleset_t *, const char *, npf_rule_t *); 413 int npf_ruleset_remove(npf_ruleset_t *, const char *, uint64_t); 414 int npf_ruleset_remkey(npf_ruleset_t *, const char *, 415 const void *, size_t); 416 nvlist_t * npf_ruleset_list(npf_t *, npf_ruleset_t *, const char *); 417 int npf_ruleset_flush(npf_ruleset_t *, const char *); 418 void npf_ruleset_gc(npf_ruleset_t *); 419 420 npf_rule_t * npf_ruleset_inspect(npf_cache_t *, const npf_ruleset_t *, 421 const int, const int); 422 int npf_rule_conclude(const npf_rule_t *, npf_match_info_t *); 423 424 /* Rule interface. */ 425 npf_rule_t * npf_rule_alloc(npf_t *, const nvlist_t *); 426 void npf_rule_setcode(npf_rule_t *, int, void *, size_t); 427 void npf_rule_setrproc(npf_rule_t *, npf_rproc_t *); 428 void npf_rule_free(npf_rule_t *); 429 uint64_t npf_rule_getid(const npf_rule_t *); 430 npf_natpolicy_t *npf_rule_getnat(const npf_rule_t *); 431 void npf_rule_setnat(npf_rule_t *, npf_natpolicy_t *); 432 npf_rproc_t * npf_rule_getrproc(const npf_rule_t *); 433 434 void npf_ext_init(npf_t *); 435 void npf_ext_fini(npf_t *); 436 int npf_ext_construct(npf_t *, const char *, 437 npf_rproc_t *, const nvlist_t *); 438 439 npf_rprocset_t *npf_rprocset_create(void); 440 void npf_rprocset_destroy(npf_rprocset_t *); 441 npf_rproc_t * npf_rprocset_lookup(npf_rprocset_t *, const char *); 442 void npf_rprocset_insert(npf_rprocset_t *, npf_rproc_t *); 443 int npf_rprocset_export(const npf_rprocset_t *, nvlist_t *); 444 445 npf_rproc_t * npf_rproc_create(const nvlist_t *); 446 void npf_rproc_acquire(npf_rproc_t *); 447 void npf_rproc_release(npf_rproc_t *); 448 const char * npf_rproc_getname(const npf_rproc_t *); 449 bool npf_rproc_run(npf_cache_t *, npf_rproc_t *, 450 const npf_match_info_t *, int *); 451 452 /* State handling. */ 453 void npf_state_sysinit(npf_t *); 454 void npf_state_sysfini(npf_t *); 455 456 bool npf_state_init(npf_cache_t *, npf_state_t *); 457 bool npf_state_inspect(npf_cache_t *, npf_state_t *, const bool); 458 int npf_state_etime(npf_t *, const npf_state_t *, const int); 459 void npf_state_destroy(npf_state_t *); 460 461 void npf_state_tcp_sysinit(npf_t *); 462 void npf_state_tcp_sysfini(npf_t *); 463 bool npf_state_tcp(npf_cache_t *, npf_state_t *, int); 464 int npf_state_tcp_timeout(npf_t *, const npf_state_t *); 465 466 /* Portmap. */ 467 void npf_portmap_init(npf_t *); 468 void npf_portmap_fini(npf_t *); 469 470 npf_portmap_t * npf_portmap_create(int, int); 471 void npf_portmap_destroy(npf_portmap_t *); 472 473 in_port_t npf_portmap_get(npf_portmap_t *, int, const npf_addr_t *); 474 bool npf_portmap_take(npf_portmap_t *, int, const npf_addr_t *, in_port_t); 475 void npf_portmap_put(npf_portmap_t *, int, const npf_addr_t *, in_port_t); 476 void npf_portmap_flush(npf_portmap_t *); 477 478 /* NAT. */ 479 void npf_nat_sysinit(void); 480 void npf_nat_sysfini(void); 481 npf_natpolicy_t *npf_nat_newpolicy(npf_t *, const nvlist_t *, npf_ruleset_t *); 482 int npf_nat_policyexport(const npf_natpolicy_t *, nvlist_t *); 483 void npf_nat_freepolicy(npf_natpolicy_t *); 484 bool npf_nat_cmppolicy(npf_natpolicy_t *, npf_natpolicy_t *); 485 void npf_nat_setid(npf_natpolicy_t *, uint64_t); 486 uint64_t npf_nat_getid(const npf_natpolicy_t *); 487 void npf_nat_freealg(npf_natpolicy_t *, npf_alg_t *); 488 489 int npf_do_nat(npf_cache_t *, npf_conn_t *, const int); 490 void npf_nat_destroy(npf_nat_t *); 491 void npf_nat_getorig(npf_nat_t *, npf_addr_t **, in_port_t *); 492 void npf_nat_gettrans(npf_nat_t *, npf_addr_t **, in_port_t *); 493 void npf_nat_setalg(npf_nat_t *, npf_alg_t *, uintptr_t); 494 495 void npf_nat_export(nvlist_t *, npf_nat_t *); 496 npf_nat_t * npf_nat_import(npf_t *, const nvlist_t *, npf_ruleset_t *, 497 npf_conn_t *); 498 499 /* ALG interface. */ 500 void npf_alg_sysinit(void); 501 void npf_alg_sysfini(void); 502 void npf_alg_init(npf_t *); 503 void npf_alg_fini(npf_t *); 504 npf_alg_t * npf_alg_register(npf_t *, const char *, const npfa_funcs_t *); 505 int npf_alg_unregister(npf_t *, npf_alg_t *); 506 npf_alg_t * npf_alg_construct(npf_t *, const char *); 507 bool npf_alg_match(npf_cache_t *, npf_nat_t *, int); 508 void npf_alg_exec(npf_cache_t *, npf_nat_t *, bool); 509 npf_conn_t * npf_alg_conn(npf_cache_t *, int); 510 int npf_alg_export(npf_t *, nvlist_t *); 511 512 /* Wrappers for the reclamation mechanism. */ 513 ebr_t * npf_ebr_create(void); 514 void npf_ebr_destroy(ebr_t *); 515 void npf_ebr_register(ebr_t *); 516 void npf_ebr_unregister(ebr_t *); 517 int npf_ebr_enter(ebr_t *); 518 void npf_ebr_exit(ebr_t *, int); 519 void npf_ebr_full_sync(ebr_t *); 520 bool npf_ebr_incrit_p(ebr_t *); 521 522 /* Debugging routines. */ 523 const char * npf_addr_dump(const npf_addr_t *, int); 524 void npf_state_dump(const npf_state_t *); 525 void npf_nat_dump(const npf_nat_t *); 526 void npf_ruleset_dump(npf_t *, const char *); 527 void npf_state_setsampler(void (*)(npf_state_t *, bool)); 528 529 /* In-kernel routines. */ 530 void npf_setkernctx(npf_t *); 531 npf_t * npf_getkernctx(void); 532 533 #endif /* _NPF_IMPL_H_ */ 534