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