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