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