npf_impl.h revision 1.79 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
238 /* Associated worker thread. */
239 unsigned worker_id;
240 void * worker_entry;
241 bool sync_registered;
242
243 /* List of extensions and its lock. */
244 LIST_HEAD(, npf_ext) ext_list;
245 kmutex_t ext_lock;
246
247 /* Statistics. */
248 percpu_t * stats_percpu;
249 };
250
251 /*
252 * NPF extensions and rule procedure interface.
253 */
254
255 struct npf_rproc;
256 typedef struct npf_rproc npf_rproc_t;
257
258 typedef struct {
259 u_int version;
260 void * ctx;
261 int (*ctor)(npf_rproc_t *, const nvlist_t *);
262 void (*dtor)(npf_rproc_t *, void *);
263 bool (*proc)(npf_cache_t *, void *, const npf_match_info_t *, int *);
264 } npf_ext_ops_t;
265
266 void * npf_ext_register(npf_t *, const char *, const npf_ext_ops_t *);
267 int npf_ext_unregister(npf_t *, void *);
268 void npf_rproc_assign(npf_rproc_t *, void *);
269
270 /*
271 * INTERFACES.
272 */
273
274 /* NPF config, statistics, etc. */
275 void npf_config_init(npf_t *);
276 void npf_config_fini(npf_t *);
277
278 npf_config_t * npf_config_enter(npf_t *);
279 void npf_config_exit(npf_t *);
280 void npf_config_sync(npf_t *);
281 bool npf_config_locked_p(npf_t *);
282 int npf_config_read_enter(npf_t *);
283 void npf_config_read_exit(npf_t *, int);
284
285 npf_config_t * npf_config_create(void);
286 void npf_config_destroy(npf_config_t *);
287 void npf_config_load(npf_t *, npf_config_t *, npf_conndb_t *, bool);
288 npf_ruleset_t * npf_config_ruleset(npf_t *npf);
289 npf_ruleset_t * npf_config_natset(npf_t *npf);
290 npf_tableset_t *npf_config_tableset(npf_t *npf);
291 bool npf_default_pass(npf_t *);
292 bool npf_active_p(void);
293
294 int npf_worker_sysinit(unsigned);
295 void npf_worker_sysfini(void);
296 void npf_worker_signal(npf_t *);
297 void npf_worker_register(npf_t *, npf_workfunc_t);
298 void npf_worker_unregister(npf_t *, npf_workfunc_t);
299
300 int npfctl_save(npf_t *, u_long, void *);
301 int npfctl_load(npf_t *, u_long, void *);
302 int npfctl_rule(npf_t *, u_long, void *);
303 int npfctl_conn_lookup(npf_t *, u_long, void *);
304 int npfctl_table_replace(npf_t *, u_long, void *);
305 int npfctl_table(npf_t *, void *);
306
307 void npf_stats_inc(npf_t *, npf_stats_t);
308 void npf_stats_dec(npf_t *, npf_stats_t);
309
310 void npf_param_init(npf_t *);
311 void npf_param_fini(npf_t *);
312 void npf_param_register(npf_t *, npf_param_t *, unsigned);
313 void * npf_param_allocgroup(npf_t *, npf_paramgroup_t, size_t);
314 void npf_param_freegroup(npf_t *, npf_paramgroup_t, size_t);
315 int npf_param_check(npf_t *, const char *, int);
316
317 void npf_ifmap_init(npf_t *, const npf_ifops_t *);
318 void npf_ifmap_fini(npf_t *);
319 u_int npf_ifmap_register(npf_t *, const char *);
320 void npf_ifmap_flush(npf_t *);
321 u_int npf_ifmap_getid(npf_t *, const ifnet_t *);
322 const char * npf_ifmap_getname(npf_t *, const u_int);
323 void npf_ifmap_copyname(npf_t *, u_int, char *, size_t);
324
325 void npf_ifaddr_sync(npf_t *, ifnet_t *);
326 void npf_ifaddr_flush(npf_t *, ifnet_t *);
327 void npf_ifaddr_syncall(npf_t *);
328
329 /* Protocol helpers. */
330 int npf_cache_all(npf_cache_t *);
331 void npf_recache(npf_cache_t *);
332
333 bool npf_rwrip(const npf_cache_t *, u_int, const npf_addr_t *);
334 bool npf_rwrport(const npf_cache_t *, u_int, const in_port_t);
335 bool npf_rwrcksum(const npf_cache_t *, u_int,
336 const npf_addr_t *, const in_port_t);
337 int npf_napt_rwr(const npf_cache_t *, u_int, const npf_addr_t *,
338 const in_addr_t);
339 int npf_npt66_rwr(const npf_cache_t *, u_int, const npf_addr_t *,
340 npf_netmask_t, uint16_t);
341
342 uint16_t npf_fixup16_cksum(uint16_t, uint16_t, uint16_t);
343 uint16_t npf_fixup32_cksum(uint16_t, uint32_t, uint32_t);
344 uint16_t npf_addr_cksum(uint16_t, int, const npf_addr_t *,
345 const npf_addr_t *);
346 uint32_t npf_addr_mix(const int, const npf_addr_t *, const npf_addr_t *);
347 int npf_addr_cmp(const npf_addr_t *, const npf_netmask_t,
348 const npf_addr_t *, const npf_netmask_t, const int);
349 void npf_addr_mask(const npf_addr_t *, const npf_netmask_t,
350 const int, npf_addr_t *);
351 void npf_addr_bitor(const npf_addr_t *, const npf_netmask_t,
352 const int, npf_addr_t *);
353 int npf_netmask_check(const int, npf_netmask_t);
354
355 int npf_tcpsaw(const npf_cache_t *, tcp_seq *, tcp_seq *,
356 uint32_t *);
357 bool npf_fetch_tcpopts(npf_cache_t *, uint16_t *, int *);
358 bool npf_set_mss(npf_cache_t *, uint16_t, uint16_t *, uint16_t *,
359 bool *);
360 bool npf_return_block(npf_cache_t *, const int);
361
362 /* BPF interface. */
363 void npf_bpf_sysinit(void);
364 void npf_bpf_sysfini(void);
365 void npf_bpf_prepare(npf_cache_t *, bpf_args_t *, uint32_t *);
366 int npf_bpf_filter(bpf_args_t *, const void *, bpfjit_func_t);
367 void * npf_bpf_compile(void *, size_t);
368 bool npf_bpf_validate(const void *, size_t);
369
370 /* Tableset interface. */
371 void npf_tableset_sysinit(void);
372 void npf_tableset_sysfini(void);
373
374 npf_tableset_t *npf_tableset_create(u_int);
375 void npf_tableset_destroy(npf_tableset_t *);
376 int npf_tableset_insert(npf_tableset_t *, npf_table_t *);
377 npf_table_t * npf_tableset_getbyname(npf_tableset_t *, const char *);
378 npf_table_t * npf_tableset_getbyid(npf_tableset_t *, u_int);
379 npf_table_t * npf_tableset_swap(npf_tableset_t *, npf_table_t *);
380 void npf_tableset_reload(npf_t *, npf_tableset_t *, npf_tableset_t *);
381 int npf_tableset_export(npf_t *, const npf_tableset_t *, nvlist_t *);
382
383 npf_table_t * npf_table_create(const char *, u_int, int, const void *, size_t);
384 void npf_table_destroy(npf_table_t *);
385
386 u_int npf_table_getid(npf_table_t *);
387 int npf_table_check(npf_tableset_t *, const char *, uint64_t, uint64_t, bool);
388 int npf_table_insert(npf_table_t *, const int,
389 const npf_addr_t *, const npf_netmask_t);
390 int npf_table_remove(npf_table_t *, const int,
391 const npf_addr_t *, const npf_netmask_t);
392 int npf_table_lookup(npf_table_t *, const int, const npf_addr_t *);
393 npf_addr_t * npf_table_getsome(npf_table_t *, const int, unsigned);
394 int npf_table_list(npf_table_t *, void *, size_t);
395 int npf_table_flush(npf_table_t *);
396 void npf_table_gc(npf_t *, npf_table_t *);
397
398 /* Ruleset interface. */
399 npf_ruleset_t * npf_ruleset_create(size_t);
400 void npf_ruleset_destroy(npf_ruleset_t *);
401 void npf_ruleset_insert(npf_ruleset_t *, npf_rule_t *);
402 void npf_ruleset_reload(npf_t *, npf_ruleset_t *,
403 npf_ruleset_t *, bool);
404 npf_natpolicy_t *npf_ruleset_findnat(npf_ruleset_t *, uint64_t);
405 void npf_ruleset_freealg(npf_ruleset_t *, npf_alg_t *);
406 int npf_ruleset_export(npf_t *, const npf_ruleset_t *,
407 const char *, nvlist_t *);
408
409 npf_rule_t * npf_ruleset_lookup(npf_ruleset_t *, const char *);
410 int npf_ruleset_add(npf_ruleset_t *, const char *, npf_rule_t *);
411 int npf_ruleset_remove(npf_ruleset_t *, const char *, uint64_t);
412 int npf_ruleset_remkey(npf_ruleset_t *, const char *,
413 const void *, size_t);
414 nvlist_t * npf_ruleset_list(npf_t *, npf_ruleset_t *, const char *);
415 int npf_ruleset_flush(npf_ruleset_t *, const char *);
416 void npf_ruleset_gc(npf_ruleset_t *);
417
418 npf_rule_t * npf_ruleset_inspect(npf_cache_t *, const npf_ruleset_t *,
419 const int, const int);
420 int npf_rule_conclude(const npf_rule_t *, npf_match_info_t *);
421
422 /* Rule interface. */
423 npf_rule_t * npf_rule_alloc(npf_t *, const nvlist_t *);
424 void npf_rule_setcode(npf_rule_t *, int, void *, size_t);
425 void npf_rule_setrproc(npf_rule_t *, npf_rproc_t *);
426 void npf_rule_free(npf_rule_t *);
427 uint64_t npf_rule_getid(const npf_rule_t *);
428 npf_natpolicy_t *npf_rule_getnat(const npf_rule_t *);
429 void npf_rule_setnat(npf_rule_t *, npf_natpolicy_t *);
430 npf_rproc_t * npf_rule_getrproc(const npf_rule_t *);
431
432 void npf_ext_init(npf_t *);
433 void npf_ext_fini(npf_t *);
434 int npf_ext_construct(npf_t *, const char *,
435 npf_rproc_t *, const nvlist_t *);
436
437 npf_rprocset_t *npf_rprocset_create(void);
438 void npf_rprocset_destroy(npf_rprocset_t *);
439 npf_rproc_t * npf_rprocset_lookup(npf_rprocset_t *, const char *);
440 void npf_rprocset_insert(npf_rprocset_t *, npf_rproc_t *);
441 int npf_rprocset_export(const npf_rprocset_t *, nvlist_t *);
442
443 npf_rproc_t * npf_rproc_create(const nvlist_t *);
444 void npf_rproc_acquire(npf_rproc_t *);
445 void npf_rproc_release(npf_rproc_t *);
446 const char * npf_rproc_getname(const npf_rproc_t *);
447 bool npf_rproc_run(npf_cache_t *, npf_rproc_t *,
448 const npf_match_info_t *, int *);
449
450 /* State handling. */
451 void npf_state_sysinit(npf_t *);
452 void npf_state_sysfini(npf_t *);
453
454 bool npf_state_init(npf_cache_t *, npf_state_t *);
455 bool npf_state_inspect(npf_cache_t *, npf_state_t *, const bool);
456 int npf_state_etime(npf_t *, const npf_state_t *, const int);
457 void npf_state_destroy(npf_state_t *);
458
459 void npf_state_tcp_sysinit(npf_t *);
460 void npf_state_tcp_sysfini(npf_t *);
461 bool npf_state_tcp(npf_cache_t *, npf_state_t *, int);
462 int npf_state_tcp_timeout(npf_t *, const npf_state_t *);
463
464 /* Portmap. */
465 void npf_portmap_init(npf_t *);
466 void npf_portmap_fini(npf_t *);
467
468 npf_portmap_t * npf_portmap_create(int, int);
469 void npf_portmap_destroy(npf_portmap_t *);
470
471 in_port_t npf_portmap_get(npf_portmap_t *, int, const npf_addr_t *);
472 bool npf_portmap_take(npf_portmap_t *, int, const npf_addr_t *, in_port_t);
473 void npf_portmap_put(npf_portmap_t *, int, const npf_addr_t *, in_port_t);
474 void npf_portmap_flush(npf_portmap_t *);
475
476 /* NAT. */
477 void npf_nat_sysinit(void);
478 void npf_nat_sysfini(void);
479 npf_natpolicy_t *npf_nat_newpolicy(npf_t *, const nvlist_t *, npf_ruleset_t *);
480 int npf_nat_policyexport(const npf_natpolicy_t *, nvlist_t *);
481 void npf_nat_freepolicy(npf_natpolicy_t *);
482 bool npf_nat_cmppolicy(npf_natpolicy_t *, npf_natpolicy_t *);
483 void npf_nat_setid(npf_natpolicy_t *, uint64_t);
484 uint64_t npf_nat_getid(const npf_natpolicy_t *);
485 void npf_nat_freealg(npf_natpolicy_t *, npf_alg_t *);
486
487 int npf_do_nat(npf_cache_t *, npf_conn_t *, const int);
488 void npf_nat_destroy(npf_nat_t *);
489 void npf_nat_getorig(npf_nat_t *, npf_addr_t **, in_port_t *);
490 void npf_nat_gettrans(npf_nat_t *, npf_addr_t **, in_port_t *);
491 void npf_nat_setalg(npf_nat_t *, npf_alg_t *, uintptr_t);
492
493 void npf_nat_export(nvlist_t *, npf_nat_t *);
494 npf_nat_t * npf_nat_import(npf_t *, const nvlist_t *, npf_ruleset_t *,
495 npf_conn_t *);
496
497 /* ALG interface. */
498 void npf_alg_sysinit(void);
499 void npf_alg_sysfini(void);
500 void npf_alg_init(npf_t *);
501 void npf_alg_fini(npf_t *);
502 npf_alg_t * npf_alg_register(npf_t *, const char *, const npfa_funcs_t *);
503 int npf_alg_unregister(npf_t *, npf_alg_t *);
504 npf_alg_t * npf_alg_construct(npf_t *, const char *);
505 bool npf_alg_match(npf_cache_t *, npf_nat_t *, int);
506 void npf_alg_exec(npf_cache_t *, npf_nat_t *, bool);
507 npf_conn_t * npf_alg_conn(npf_cache_t *, int);
508 int npf_alg_export(npf_t *, nvlist_t *);
509
510 /* Wrappers for the reclamation mechanism. */
511 ebr_t * npf_ebr_create(void);
512 void npf_ebr_destroy(ebr_t *);
513 void npf_ebr_register(ebr_t *);
514 void npf_ebr_unregister(ebr_t *);
515 int npf_ebr_enter(ebr_t *);
516 void npf_ebr_exit(ebr_t *, int);
517 void npf_ebr_full_sync(ebr_t *);
518 bool npf_ebr_incrit_p(ebr_t *);
519
520 /* Debugging routines. */
521 const char * npf_addr_dump(const npf_addr_t *, int);
522 void npf_state_dump(const npf_state_t *);
523 void npf_nat_dump(const npf_nat_t *);
524 void npf_ruleset_dump(npf_t *, const char *);
525 void npf_state_setsampler(void (*)(npf_state_t *, bool));
526
527 /* In-kernel routines. */
528 void npf_setkernctx(npf_t *);
529 npf_t * npf_getkernctx(void);
530
531 #endif /* _NPF_IMPL_H_ */
532