1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2002-2009 Luigi Rizzo, Universita` di Pisa
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #ifndef _IPFW2_PRIVATE_H
29 #define _IPFW2_PRIVATE_H
30
31 #include <sys/queue.h>
32 #include <sys/tree.h>
33
34 /*
35 * Internal constants and data structures used by ipfw components
36 * and not meant to be exported outside the kernel.
37 */
38
39 #ifdef _KERNEL
40
41 /*
42 * For platforms that do not have SYSCTL support, we wrap the
43 * SYSCTL_* into a function (one per file) to collect the values
44 * into an array at module initialization. The wrapping macros,
45 * SYSBEGIN() and SYSEND, are empty in the default case.
46 */
47 #ifndef SYSBEGIN
48 #define SYSBEGIN(x)
49 #endif
50 #ifndef SYSEND
51 #define SYSEND
52 #endif
53
54 /* Return values from ipfw_chk() */
55 enum {
56 IP_FW_PASS = 0,
57 IP_FW_DENY,
58 IP_FW_DIVERT,
59 IP_FW_TEE,
60 IP_FW_DUMMYNET,
61 IP_FW_NETGRAPH,
62 IP_FW_NGTEE,
63 IP_FW_NAT,
64 IP_FW_REASS,
65 IP_FW_NAT64,
66 };
67
68 /*
69 * Structure for collecting parameters to dummynet for ip6_output forwarding
70 */
71 struct _ip6dn_args {
72 struct ip6_pktopts *opt_or;
73 int flags_or;
74 struct ip6_moptions *im6o_or;
75 struct ifnet *origifp_or;
76 struct ifnet *ifp_or;
77 struct sockaddr_in6 dst_or;
78 u_long mtu_or;
79 };
80
81 /*
82 * Arguments for calling ipfw_chk() and dummynet_io(). We put them
83 * all into a structure because this way it is easier and more
84 * efficient to pass variables around and extend the interface.
85 */
86 struct ip_fw_args {
87 uint32_t flags;
88 #define IPFW_ARGS_ETHER 0x00010000 /* valid ethernet header */
89 #define IPFW_ARGS_NH4 0x00020000 /* IPv4 next hop in hopstore */
90 #define IPFW_ARGS_NH6 0x00040000 /* IPv6 next hop in hopstore */
91 #define IPFW_ARGS_NH4PTR 0x00080000 /* IPv4 next hop in next_hop */
92 #define IPFW_ARGS_NH6PTR 0x00100000 /* IPv6 next hop in next_hop6 */
93 #define IPFW_ARGS_REF 0x00200000 /* valid ipfw_rule_ref */
94 #define IPFW_ARGS_IN 0x00400000 /* called on input */
95 #define IPFW_ARGS_OUT 0x00800000 /* called on output */
96 #define IPFW_ARGS_IP4 0x01000000 /* belongs to v4 ISR */
97 #define IPFW_ARGS_IP6 0x02000000 /* belongs to v6 ISR */
98 #define IPFW_ARGS_DROP 0x04000000 /* drop it (dummynet) */
99 #define IPFW_ARGS_LENMASK 0x0000ffff /* length of data in *mem */
100 #define IPFW_ARGS_LENGTH(f) ((f) & IPFW_ARGS_LENMASK)
101 /*
102 * On return, it points to the matching rule.
103 * On entry, rule.slot > 0 means the info is valid and
104 * contains the starting rule for an ipfw search.
105 * If chain_id == chain->id && slot >0 then jump to that slot.
106 * Otherwise, we locate the first rule >= rulenum:rule_id
107 */
108 struct ipfw_rule_ref rule; /* match/restart info */
109
110 struct ifnet *ifp; /* input/output interface */
111 struct inpcb *inp;
112 union {
113 /*
114 * next_hop[6] pointers can be used to point to next hop
115 * stored in rule's opcode to avoid copying into hopstore.
116 * Also, it is expected that all 0x1-0x10 flags are mutually
117 * exclusive.
118 */
119 struct sockaddr_in *next_hop;
120 struct sockaddr_in6 *next_hop6;
121 /* ipfw next hop storage */
122 struct sockaddr_in hopstore;
123 struct ip_fw_nh6 {
124 struct in6_addr sin6_addr;
125 uint32_t sin6_scope_id;
126 uint16_t sin6_port;
127 } hopstore6;
128 };
129 union {
130 struct mbuf *m; /* the mbuf chain */
131 void *mem; /* or memory pointer */
132 };
133 struct ipfw_flow_id f_id; /* grabbed from IP header */
134 };
135
136 MALLOC_DECLARE(M_IPFW);
137
138 /* wrapper for freeing a packet, in case we need to do more work */
139 #ifndef FREE_PKT
140 #if defined(__linux__) || defined(_WIN32)
141 #define FREE_PKT(m) netisr_dispatch(-1, m)
142 #else
143 #define FREE_PKT(m) m_freem(m)
144 #endif
145 #endif /* !FREE_PKT */
146
147 /*
148 * Function definitions.
149 */
150 int ipfw_chk(struct ip_fw_args *args);
151 struct mbuf *ipfw_send_pkt(struct mbuf *, struct ipfw_flow_id *,
152 u_int32_t, u_int32_t, int);
153
154 int ipfw_attach_hooks(void);
155 void ipfw_detach_hooks(void);
156 #ifdef NOTYET
157 void ipfw_nat_destroy(void);
158 #endif
159
160 /* In ip_fw_log.c */
161 struct ip;
162 struct ip_fw;
163 struct ip_fw_chain;
164
165 void ipfw_bpf_init(int);
166 void ipfw_bpf_uninit(int);
167 void ipfw_tap_alloc(struct ip_fw_chain *, uint32_t);
168 void ipfw_tap_free(struct ip_fw_chain *, uint32_t);
169 void ipfw_bpf_tap(struct ip_fw_chain *, struct ip_fw_args *, struct ip *,
170 uint32_t);
171 void ipfw_pflog_tap(void *, struct mbuf *);
172 void ipfw_log(struct ip_fw_chain *chain, struct ip_fw *f, u_int hlen,
173 struct ip_fw_args *args, u_short offset, uint32_t tablearg, struct ip *ip,
174 void *eh);
175 VNET_DECLARE(u_int64_t, norule_counter);
176 #define V_norule_counter VNET(norule_counter)
177 VNET_DECLARE(int, verbose_limit);
178 #define V_verbose_limit VNET(verbose_limit)
179
180 /* In ip_fw_dynamic.c */
181 struct sockopt_data;
182
183 enum { /* result for matching dynamic rules */
184 MATCH_REVERSE = 0,
185 MATCH_FORWARD,
186 MATCH_NONE,
187 MATCH_UNKNOWN,
188 };
189
190 /*
191 * Macro to determine that we need to do or redo dynamic state lookup.
192 * direction == MATCH_UNKNOWN means that this is first lookup, then we need
193 * to do lookup.
194 * Otherwise check the state name, if previous lookup was for "any" name,
195 * this means there is no state with specific name. Thus no need to do
196 * lookup. If previous name was not "any", redo lookup for specific name.
197 */
198 #define DYN_LOOKUP_NEEDED(p, cmd) \
199 ((p)->direction == MATCH_UNKNOWN || \
200 ((p)->kidx != 0 && (p)->kidx != (cmd)->arg1))
201 #define DYN_INFO_INIT(p) do { \
202 (p)->direction = MATCH_UNKNOWN; \
203 (p)->kidx = 0; \
204 } while (0)
205 struct ipfw_dyn_info {
206 uint32_t direction; /* match direction */
207 uint32_t kidx; /* state name kidx */
208 uint32_t hashval; /* hash value */
209 uint32_t version; /* bucket version */
210 uint32_t f_pos;
211 };
212 int ipfw_dyn_install_state(struct ip_fw_chain *chain, struct ip_fw *rule,
213 const ipfw_insn_limit *cmd, const struct ip_fw_args *args,
214 const void *ulp, int pktlen, struct ipfw_dyn_info *info,
215 uint32_t tablearg);
216 struct ip_fw *ipfw_dyn_lookup_state(const struct ip_fw_args *args,
217 const void *ulp, int pktlen, const ipfw_insn *cmd,
218 struct ipfw_dyn_info *info);
219
220 int ipfw_is_dyn_rule(struct ip_fw *rule);
221 void ipfw_expire_dyn_states(struct ip_fw_chain *, ipfw_range_tlv *);
222 void ipfw_get_dynamic(struct ip_fw_chain *chain, char **bp, const char *ep);
223 int ipfw_dump_states(struct ip_fw_chain *chain, struct sockopt_data *sd);
224
225 void ipfw_dyn_init(struct ip_fw_chain *); /* per-vnet initialization */
226 void ipfw_dyn_uninit(int); /* per-vnet deinitialization */
227 int ipfw_dyn_len(void);
228 uint32_t ipfw_dyn_get_count(uint32_t *, int *);
229 void ipfw_dyn_reset_eaction(struct ip_fw_chain *ch, uint32_t eaction_id,
230 uint32_t default_id, uint32_t instance_id);
231
232 /* common variables */
233 VNET_DECLARE(int, fw_one_pass);
234 #define V_fw_one_pass VNET(fw_one_pass)
235
236 VNET_DECLARE(int, fw_verbose);
237 #define V_fw_verbose VNET(fw_verbose)
238
239 VNET_DECLARE(struct ip_fw_chain, layer3_chain);
240 #define V_layer3_chain VNET(layer3_chain)
241
242 VNET_DECLARE(int, ipfw_vnet_ready);
243 #define V_ipfw_vnet_ready VNET(ipfw_vnet_ready)
244
245 VNET_DECLARE(int, skipto_cache);
246 #define V_skipto_cache VNET(skipto_cache)
247
248 VNET_DECLARE(u_int32_t, set_disable);
249 #define V_set_disable VNET(set_disable)
250
251 VNET_DECLARE(int, autoinc_step);
252 #define V_autoinc_step VNET(autoinc_step)
253
254 VNET_DECLARE(unsigned int, fw_tables_max);
255 #define V_fw_tables_max VNET(fw_tables_max)
256
257 VNET_DECLARE(unsigned int, fw_tables_sets);
258 #define V_fw_tables_sets VNET(fw_tables_sets)
259
260 struct tables_config;
261
262 #ifdef _KERNEL
263 /*
264 * Here we have the structure representing an ipfw rule.
265 *
266 * It starts with a general area
267 * followed by an array of one or more instructions, which the code
268 * accesses as an array of 32-bit values.
269 *
270 * Given a rule pointer r:
271 *
272 * r->cmd is the start of the first instruction.
273 * ACTION_PTR(r) is the start of the first action (things to do
274 * once a rule matched).
275 */
276 struct ip_fw_jump_cache {
277 union {
278 struct {
279 uint32_t id;
280 uint32_t pos;
281 };
282 uint64_t raw_value;
283 };
284 };
285
286 struct ip_fw {
287 uint16_t act_ofs; /* offset of action in 32-bit units */
288 uint16_t cmd_len; /* # of 32-bit words in cmd */
289 uint32_t rulenum; /* rule number */
290 uint8_t set; /* rule set (0..31) */
291 uint8_t flags; /* currently unused */
292 uint16_t _pad;
293 counter_u64_t cntr; /* Pointer to rule counters */
294 struct ip_fw_jump_cache cache; /* used by jump_fast */
295 uint32_t timestamp; /* tv_sec of last match */
296 uint32_t id; /* rule id */
297 uint32_t refcnt; /* number of references */
298
299 struct ip_fw *next; /* linked list of deleted rules */
300 ipfw_insn cmd[1]; /* storage for commands */
301 };
302
303 #define IPFW_RULE_CNTR_SIZE (2 * sizeof(uint64_t))
304
305 #endif
306
307 struct ip_fw_chain {
308 struct ip_fw **map; /* array of rule ptrs to ease lookup */
309 uint32_t id; /* ruleset id */
310 int n_rules; /* number of static rules */
311 void *tablestate; /* runtime table info */
312 void *valuestate; /* runtime table value info */
313 int *idxmap; /* skipto array of rules */
314 void **srvstate; /* runtime service mappings */
315 #if defined( __linux__ ) || defined( _WIN32 )
316 spinlock_t rwmtx;
317 #else
318 struct rmlock rwmtx;
319 #endif
320 uint32_t gencnt; /* NAT generation count */
321 LIST_HEAD(nat_list, cfg_nat) nat; /* list of nat entries */
322 struct ip_fw *default_rule;
323 struct tables_config *tblcfg; /* tables module data */
324 void *ifcfg; /* interface module data */
325 int *idxmap_back; /* standby skipto array of rules */
326 struct namedobj_instance *srvmap; /* cfg name->number mappings */
327 RB_HEAD(tap_tree, ipfw_tap) taps; /* see ip_fw_bpf.c */
328 #if defined( __linux__ ) || defined( _WIN32 )
329 spinlock_t uh_lock;
330 #else
331 struct sx uh_lock; /* lock for upper half */
332 #endif
333 };
334
335 /* 64-byte structure representing multi-field table value */
336 struct table_value {
337 uint32_t tag; /* O_TAG/O_TAGGED */
338 uint16_t pipe; /* O_PIPE/O_QUEUE */
339 uint16_t divert; /* O_DIVERT/O_TEE */
340 uint32_t skipto; /* skipto, CALLRET */
341 uint32_t netgraph; /* O_NETGRAPH/O_NGTEE */
342 uint16_t fib; /* O_SETFIB */
343 uint16_t nat; /* O_NAT */
344 uint32_t mark; /* O_SETMARK/O_MARK */
345 uint32_t nh4;
346 uint8_t dscp;
347 uint8_t spare0;
348 uint16_t kidx; /* value kernel index */
349 /* -- 32 bytes -- */
350 struct in6_addr nh6;
351 uint32_t limit; /* O_LIMIT */
352 uint32_t zoneid; /* scope zone id for nh6 */
353 uint64_t refcnt; /* Number of references */
354 };
355
356 struct named_object {
357 TAILQ_ENTRY(named_object) nn_next; /* namehash */
358 TAILQ_ENTRY(named_object) nv_next; /* valuehash */
359 char *name; /* object name */
360 uint16_t etlv; /* Export TLV id */
361 uint8_t subtype;/* object subtype within class */
362 uint8_t set; /* set object belongs to */
363 uint32_t kidx; /* object kernel index */
364 uint32_t ocnt; /* object counter for internal use */
365 uint32_t refcnt; /* number of references */
366 };
367 TAILQ_HEAD(namedobjects_head, named_object);
368
369 struct sockopt; /* used by tcp_var.h */
370 struct sockopt_data {
371 caddr_t kbuf; /* allocated buffer */
372 size_t ksize; /* given buffer size */
373 size_t koff; /* data already used */
374 size_t kavail; /* number of bytes available */
375 size_t ktotal; /* total bytes pushed */
376 struct sockopt *sopt; /* socket data */
377 caddr_t sopt_val; /* sopt user buffer */
378 size_t valsize; /* original data size */
379 };
380
381 struct ipfw_ifc;
382
383 typedef void (ipfw_ifc_cb)(struct ip_fw_chain *ch, void *cbdata,
384 uint16_t ifindex);
385
386 struct ipfw_iface {
387 struct named_object no;
388 char ifname[64];
389 int resolved;
390 uint16_t ifindex;
391 uint16_t spare;
392 uint64_t gencnt;
393 TAILQ_HEAD(, ipfw_ifc) consumers;
394 };
395
396 struct ipfw_ifc {
397 TAILQ_ENTRY(ipfw_ifc) next;
398 struct ipfw_iface *iface;
399 ipfw_ifc_cb *cb;
400 void *cbdata;
401 };
402
403 /* Macro for working with various counters */
404 #define IPFW_INC_RULE_COUNTER(_cntr, _bytes) do { \
405 counter_u64_add((_cntr)->cntr, 1); \
406 counter_u64_add((_cntr)->cntr + 1, _bytes); \
407 if ((_cntr)->timestamp != time_uptime) \
408 (_cntr)->timestamp = time_uptime; \
409 } while (0)
410
411 #define IPFW_INC_DYN_COUNTER(_cntr, _bytes) do { \
412 (_cntr)->pcnt++; \
413 (_cntr)->bcnt += _bytes; \
414 } while (0)
415
416 #define IPFW_ZERO_RULE_COUNTER(_cntr) do { \
417 counter_u64_zero((_cntr)->cntr); \
418 counter_u64_zero((_cntr)->cntr + 1); \
419 (_cntr)->timestamp = 0; \
420 } while (0)
421
422 #define IPFW_ZERO_DYN_COUNTER(_cntr) do { \
423 (_cntr)->pcnt = 0; \
424 (_cntr)->bcnt = 0; \
425 } while (0)
426
427 #define TARG_VAL(ch, k, f) ((struct table_value *)((ch)->valuestate))[k].f
428 #define IP_FW_ARG_TABLEARG(ch, a, f) \
429 (((a) == IP_FW_TARG) ? TARG_VAL(ch, tablearg, f) : (a))
430 /*
431 * The lock is heavily used by ip_fw2.c (the main file) and ip_fw_nat.c
432 * so the variable and the macros must be here.
433 */
434
435 #if defined( __linux__ ) || defined( _WIN32 )
436 #define IPFW_LOCK_INIT(_chain) do { \
437 rw_init(&(_chain)->rwmtx, "IPFW static rules"); \
438 rw_init(&(_chain)->uh_lock, "IPFW UH lock"); \
439 } while (0)
440
441 #define IPFW_LOCK_DESTROY(_chain) do { \
442 rw_destroy(&(_chain)->rwmtx); \
443 rw_destroy(&(_chain)->uh_lock); \
444 } while (0)
445
446 #define IPFW_RLOCK_ASSERT(_chain) rw_assert(&(_chain)->rwmtx, RA_RLOCKED)
447 #define IPFW_WLOCK_ASSERT(_chain) rw_assert(&(_chain)->rwmtx, RA_WLOCKED)
448
449 #define IPFW_RLOCK_TRACKER
450 #define IPFW_RLOCK(p) rw_rlock(&(p)->rwmtx)
451 #define IPFW_RUNLOCK(p) rw_runlock(&(p)->rwmtx)
452 #define IPFW_WLOCK(p) rw_wlock(&(p)->rwmtx)
453 #define IPFW_WUNLOCK(p) rw_wunlock(&(p)->rwmtx)
454 #define IPFW_PF_RLOCK(p) IPFW_RLOCK(p)
455 #define IPFW_PF_RUNLOCK(p) IPFW_RUNLOCK(p)
456 #else /* FreeBSD */
457 #define IPFW_LOCK_INIT(_chain) do { \
458 rm_init_flags(&(_chain)->rwmtx, "IPFW static rules", RM_RECURSE); \
459 sx_init(&(_chain)->uh_lock, "IPFW UH lock"); \
460 } while (0)
461
462 #define IPFW_LOCK_DESTROY(_chain) do { \
463 rm_destroy(&(_chain)->rwmtx); \
464 sx_destroy(&(_chain)->uh_lock); \
465 } while (0)
466
467 #define IPFW_RLOCK_ASSERT(_chain) rm_assert(&(_chain)->rwmtx, RA_RLOCKED)
468 #define IPFW_WLOCK_ASSERT(_chain) rm_assert(&(_chain)->rwmtx, RA_WLOCKED)
469
470 #define IPFW_RLOCK_TRACKER struct rm_priotracker _tracker
471 #define IPFW_RLOCK(p) rm_rlock(&(p)->rwmtx, &_tracker)
472 #define IPFW_RUNLOCK(p) rm_runlock(&(p)->rwmtx, &_tracker)
473 #define IPFW_WLOCK(p) rm_wlock(&(p)->rwmtx)
474 #define IPFW_WUNLOCK(p) rm_wunlock(&(p)->rwmtx)
475 #define IPFW_PF_RLOCK(p) IPFW_RLOCK(p)
476 #define IPFW_PF_RUNLOCK(p) IPFW_RUNLOCK(p)
477 #endif
478
479 #define IPFW_UH_RLOCK_ASSERT(_chain) sx_assert(&(_chain)->uh_lock, SA_SLOCKED)
480 #define IPFW_UH_WLOCK_ASSERT(_chain) sx_assert(&(_chain)->uh_lock, SA_XLOCKED)
481 #define IPFW_UH_UNLOCK_ASSERT(_chain) sx_assert(&(_chain)->uh_lock, SA_UNLOCKED)
482
483 #define IPFW_UH_RLOCK(p) sx_slock(&(p)->uh_lock)
484 #define IPFW_UH_RUNLOCK(p) sx_sunlock(&(p)->uh_lock)
485 #define IPFW_UH_WLOCK(p) sx_xlock(&(p)->uh_lock)
486 #define IPFW_UH_WUNLOCK(p) sx_xunlock(&(p)->uh_lock)
487
488 struct obj_idx {
489 uint32_t uidx; /* internal index supplied by userland */
490 uint32_t kidx; /* kernel object index */
491 uint16_t off; /* tlv offset from rule end in 4-byte words */
492 uint8_t spare;
493 uint8_t type; /* object type within its category */
494 };
495
496 struct rule_check_info {
497 uint16_t flags; /* rule-specific check flags */
498 #define IPFW_RCIFLAG_HAS_STATE 0x0001
499 uint16_t object_opcodes; /* num of opcodes referencing objects */
500 uint16_t urule_numoff; /* offset of rulenum in bytes */
501 uint8_t version; /* rule version */
502 uint8_t spare;
503 ipfw_obj_ctlv *ctlv; /* name TLV containter */
504 struct ip_fw *krule; /* resulting rule pointer */
505 caddr_t urule; /* original rule pointer */
506 struct obj_idx obuf[8]; /* table references storage */
507 };
508
509 /* Kernel rule length */
510 /*
511 * RULE _K_ SIZE _V_ ->
512 * get kernel size from userland rool version _V_.
513 * RULE _U_ SIZE _V_ ->
514 * get user size version _V_ from kernel rule
515 * RULESIZE _V_ ->
516 * get user size rule length
517 */
518 /* FreeBSD11 <> current kernel format */
519 #define RULEUSIZE1(r) (roundup2(sizeof(struct ip_fw_rule) + \
520 (r)->cmd_len * 4 - 4, 8))
521 #define RULEKSIZE1(r) roundup2((sizeof(struct ip_fw) + (r)->cmd_len*4 - 4), 8)
522
523 /*
524 * Tables/Objects index rewriting code
525 */
526
527 /* Default and maximum number of ipfw tables/objects. */
528 #define IPFW_TABLES_MAX 65536
529 #define IPFW_TABLES_DEFAULT 128
530 #define IPFW_OBJECTS_MAX 65536
531 #define IPFW_OBJECTS_DEFAULT 4096
532
533 #define CHAIN_TO_SRV(ch) ((ch)->srvmap)
534 #define SRV_OBJECT(ch, idx) ((ch)->srvstate[(idx)])
535
536 struct tid_info {
537 uint32_t set; /* table set */
538 uint32_t uidx; /* table index */
539 uint8_t type; /* table type */
540 uint8_t atype;
541 uint16_t spare;
542 int tlen; /* Total TLV size block */
543 void *tlvs; /* Pointer to first TLV */
544 };
545
546 /*
547 * Classifier callback. Checks if @cmd opcode contains kernel object reference.
548 * If true, returns its index and type.
549 * Returns 0 if match is found, 1 overwise.
550 */
551 typedef int (ipfw_obj_rw_cl)(ipfw_insn *cmd, uint32_t *puidx, uint8_t *ptype);
552 /*
553 * Updater callback. Sets kernel object reference index to @puidx
554 */
555 typedef void (ipfw_obj_rw_upd)(ipfw_insn *cmd, uint32_t puidx);
556 /*
557 * Finder callback. Tries to find named object by name (specified via @ti).
558 * Stores found named object pointer in @pno.
559 * If object was not found, NULL is stored.
560 *
561 * Return 0 if input data was valid.
562 */
563 typedef int (ipfw_obj_fname_cb)(struct ip_fw_chain *ch,
564 struct tid_info *ti, struct named_object **pno);
565 /*
566 * Another finder callback. Tries to findex named object by kernel index.
567 *
568 * Returns pointer to named object or NULL.
569 */
570 typedef struct named_object *(ipfw_obj_fidx_cb)(struct ip_fw_chain *ch,
571 uint32_t kidx);
572 /*
573 * Object creator callback. Tries to create object specified by @ti.
574 * Stores newly-allocated object index in @pkidx.
575 *
576 * Returns 0 on success.
577 */
578 typedef int (ipfw_obj_create_cb)(struct ip_fw_chain *ch, struct tid_info *ti,
579 uint32_t *pkidx);
580 /*
581 * Object destroy callback. Intended to free resources allocated by
582 * create_object callback.
583 */
584 typedef void (ipfw_obj_destroy_cb)(struct ip_fw_chain *ch,
585 struct named_object *no);
586 /*
587 * Sets handler callback. Handles moving and swaping set of named object.
588 * SWAP_ALL moves all named objects from set `set' to `new_set' and vise versa;
589 * TEST_ALL checks that there aren't any named object with conflicting names;
590 * MOVE_ALL moves all named objects from set `set' to `new_set';
591 * COUNT_ONE used to count number of references used by object with kidx `set';
592 * TEST_ONE checks that named object with kidx `set' can be moved to `new_set`;
593 * MOVE_ONE moves named object with kidx `set' to set `new_set'.
594 */
595 enum ipfw_sets_cmd {
596 SWAP_ALL = 0, TEST_ALL, MOVE_ALL, COUNT_ONE, TEST_ONE, MOVE_ONE
597 };
598 typedef int (ipfw_obj_sets_cb)(struct ip_fw_chain *ch,
599 uint32_t set, uint8_t new_set, enum ipfw_sets_cmd cmd);
600
601 struct opcode_obj_rewrite {
602 uint32_t opcode; /* Opcode to act upon */
603 uint32_t etlv; /* Relevant export TLV id */
604 ipfw_obj_rw_cl *classifier; /* Check if rewrite is needed */
605 ipfw_obj_rw_upd *update; /* update cmd with new value */
606 ipfw_obj_fname_cb *find_byname; /* Find named object by name */
607 ipfw_obj_fidx_cb *find_bykidx; /* Find named object by kidx */
608 ipfw_obj_create_cb *create_object; /* Create named object */
609 ipfw_obj_destroy_cb *destroy_object;/* Destroy named object */
610 ipfw_obj_sets_cb *manage_sets; /* Swap or move sets */
611 };
612
613 #define IPFW_ADD_OBJ_REWRITER(f, c) do { \
614 if ((f) != 0) \
615 ipfw_add_obj_rewriter(c, \
616 sizeof(c) / sizeof(c[0])); \
617 } while(0)
618 #define IPFW_DEL_OBJ_REWRITER(l, c) do { \
619 if ((l) != 0) \
620 ipfw_del_obj_rewriter(c, \
621 sizeof(c) / sizeof(c[0])); \
622 } while(0)
623
624 /* In ip_fw_iface.c */
625 int ipfw_iface_init(void);
626 void ipfw_iface_destroy(void);
627 void vnet_ipfw_iface_destroy(struct ip_fw_chain *ch);
628 int ipfw_iface_ref(struct ip_fw_chain *ch, char *name,
629 struct ipfw_ifc *ic);
630 void ipfw_iface_unref(struct ip_fw_chain *ch, struct ipfw_ifc *ic);
631 void ipfw_iface_add_notify(struct ip_fw_chain *ch, struct ipfw_ifc *ic);
632 void ipfw_iface_del_notify(struct ip_fw_chain *ch, struct ipfw_ifc *ic);
633
634 /* In ip_fw_sockopt.c */
635 enum ipfw_opcheck_result {
636 SUCCESS = 0,
637 FAILED,
638 BAD_SIZE,
639 CHECK_ACTION,
640 };
641 typedef enum ipfw_opcheck_result (*ipfw_check_opcode_t)(ipfw_insn **,
642 int *, struct rule_check_info *);
643
644 void ipfw_register_compat(ipfw_check_opcode_t);
645 void ipfw_unregister_compat(void);
646
647 enum ipfw_opcheck_result ipfw_check_opcode(ipfw_insn **, int *,
648 struct rule_check_info *);
649 void ipfw_init_skipto_cache(struct ip_fw_chain *chain);
650 void ipfw_destroy_skipto_cache(struct ip_fw_chain *chain);
651 void ipfw_enable_skipto_cache(struct ip_fw_chain *chain);
652 int ipfw_find_rule(struct ip_fw_chain *chain, uint32_t key, uint32_t id);
653 int ipfw_ctl3(struct sockopt *sopt);
654 int ipfw_add_protected_rule(struct ip_fw_chain *chain, struct ip_fw *rule);
655 void ipfw_reap_add(struct ip_fw_chain *chain, struct ip_fw **head,
656 struct ip_fw *rule);
657 void ipfw_reap_rules(struct ip_fw *head);
658 void ipfw_init_counters(void);
659 void ipfw_destroy_counters(void);
660 int ipfw_commit_rules(struct ip_fw_chain *chain, struct rule_check_info *rci,
661 int count);
662 int delete_range(struct ip_fw_chain *chain, ipfw_range_tlv *rt, int *ndel);
663 struct ip_fw *ipfw_alloc_rule(struct ip_fw_chain *chain, size_t rulesize);
664 void ipfw_free_rule(struct ip_fw *rule);
665 int ipfw_match_range(struct ip_fw *rule, ipfw_range_tlv *rt);
666 int ipfw_mark_object_kidx(uint32_t *bmask, uint16_t etlv, uint32_t kidx);
667 ipfw_insn *ipfw_get_action(struct ip_fw *);
668 int ipfw_check_rule(struct ip_fw_rule *rule, size_t size,
669 struct rule_check_info *ci);
670
671 typedef int (sopt_handler_f)(struct ip_fw_chain *ch,
672 ip_fw3_opheader *op3, struct sockopt_data *sd);
673 struct ipfw_sopt_handler {
674 uint16_t opcode;
675 uint8_t version;
676 uint8_t dir;
677 sopt_handler_f *handler;
678 uint64_t refcnt;
679 };
680 #define HDIR_SET 0x01 /* Handler is used to set some data */
681 #define HDIR_GET 0x02 /* Handler is used to retrieve data */
682 #define HDIR_BOTH HDIR_GET|HDIR_SET
683
684 void ipfw_init_sopt_handler(void);
685 void ipfw_destroy_sopt_handler(void);
686 void ipfw_add_sopt_handler(struct ipfw_sopt_handler *sh, size_t count);
687 int ipfw_del_sopt_handler(struct ipfw_sopt_handler *sh, size_t count);
688 caddr_t ipfw_get_sopt_space(struct sockopt_data *sd, size_t needed);
689 caddr_t ipfw_get_sopt_header(struct sockopt_data *sd, size_t needed);
690 #define IPFW_ADD_SOPT_HANDLER(f, c) do { \
691 if ((f) != 0) \
692 ipfw_add_sopt_handler(c, \
693 sizeof(c) / sizeof(c[0])); \
694 } while(0)
695 #define IPFW_DEL_SOPT_HANDLER(l, c) do { \
696 if ((l) != 0) \
697 ipfw_del_sopt_handler(c, \
698 sizeof(c) / sizeof(c[0])); \
699 } while(0)
700
701 #define DEFAULT_OBJHASH_SIZE 32
702 struct namedobj_instance;
703 typedef int (objhash_cb_t)(struct namedobj_instance *ni, struct named_object *,
704 void *arg);
705 typedef uint32_t (objhash_hash_f)(struct namedobj_instance *ni, const void *key,
706 uint32_t kopt);
707 typedef int (objhash_cmp_f)(struct named_object *no, const void *key,
708 uint32_t kopt);
709 struct namedobj_instance *ipfw_objhash_create(uint32_t items, size_t hash_size);
710 void ipfw_objhash_destroy(struct namedobj_instance *);
711 void ipfw_objhash_bitmap_alloc(uint32_t items, void **idx, int *pblocks);
712 void ipfw_objhash_bitmap_merge(struct namedobj_instance *ni,
713 void **idx, int *blocks);
714 void ipfw_objhash_bitmap_swap(struct namedobj_instance *ni,
715 void **idx, int *blocks);
716 void ipfw_objhash_bitmap_free(void *idx, int blocks);
717 void ipfw_objhash_set_hashf(struct namedobj_instance *ni, objhash_hash_f *f);
718 struct named_object *ipfw_objhash_lookup_name(struct namedobj_instance *ni,
719 uint32_t set, const char *name);
720 struct named_object *ipfw_objhash_lookup_name_type(struct namedobj_instance *ni,
721 uint32_t set, uint32_t type, const char *name);
722 struct named_object *ipfw_objhash_lookup_kidx(struct namedobj_instance *ni,
723 uint32_t idx);
724 int ipfw_objhash_same_name(struct namedobj_instance *ni, struct named_object *a,
725 struct named_object *b);
726 void ipfw_objhash_add(struct namedobj_instance *ni, struct named_object *no);
727 void ipfw_objhash_del(struct namedobj_instance *ni, struct named_object *no);
728 uint32_t ipfw_objhash_count(struct namedobj_instance *ni);
729 uint32_t ipfw_objhash_count_type(struct namedobj_instance *ni, uint16_t type);
730 int ipfw_objhash_foreach(struct namedobj_instance *ni, objhash_cb_t *f,
731 void *arg);
732 int ipfw_objhash_foreach_type(struct namedobj_instance *ni, objhash_cb_t *f,
733 void *arg, uint16_t type);
734 int ipfw_objhash_free_idx(struct namedobj_instance *ni, uint32_t idx);
735 int ipfw_objhash_alloc_idx(void *n, uint32_t *pidx);
736 void ipfw_objhash_set_funcs(struct namedobj_instance *ni,
737 objhash_hash_f *hash_f, objhash_cmp_f *cmp_f);
738 int ipfw_objhash_find_type(struct namedobj_instance *ni, struct tid_info *ti,
739 uint32_t etlv, struct named_object **pno);
740 void ipfw_export_obj_ntlv(struct named_object *no, ipfw_obj_ntlv *ntlv);
741 ipfw_obj_ntlv *ipfw_find_name_tlv_type(void *tlvs, int len, uint32_t uidx,
742 uint32_t etlv);
743 void ipfw_init_obj_rewriter(void);
744 void ipfw_destroy_obj_rewriter(void);
745 void ipfw_add_obj_rewriter(struct opcode_obj_rewrite *rw, size_t count);
746 int ipfw_del_obj_rewriter(struct opcode_obj_rewrite *rw, size_t count);
747
748 void update_opcode_kidx(ipfw_insn *cmd, uint32_t idx);
749 int classify_opcode_kidx(ipfw_insn *cmd, uint32_t *puidx);
750 void ipfw_init_srv(struct ip_fw_chain *ch);
751 void ipfw_destroy_srv(struct ip_fw_chain *ch);
752 int ipfw_check_object_name_generic(const char *name);
753 int ipfw_obj_manage_sets(struct namedobj_instance *ni, uint16_t type,
754 uint32_t set, uint8_t new_set, enum ipfw_sets_cmd cmd);
755
756 /* In ip_fw_eaction.c */
757 typedef int (ipfw_eaction_t)(struct ip_fw_chain *ch, struct ip_fw_args *args,
758 ipfw_insn *cmd, int *done);
759 int ipfw_eaction_init(struct ip_fw_chain *ch, int first);
760 void ipfw_eaction_uninit(struct ip_fw_chain *ch, int last);
761
762 uint32_t ipfw_add_eaction(struct ip_fw_chain *ch, ipfw_eaction_t handler,
763 const char *name);
764 int ipfw_del_eaction(struct ip_fw_chain *ch, uint32_t eaction_id);
765 int ipfw_run_eaction(struct ip_fw_chain *ch, struct ip_fw_args *args,
766 ipfw_insn *cmd, int *done);
767 int ipfw_reset_eaction(struct ip_fw_chain *ch, struct ip_fw *rule,
768 uint32_t eaction_id, uint32_t default_id, uint32_t instance_id);
769 int ipfw_reset_eaction_instance(struct ip_fw_chain *ch, uint32_t eaction_id,
770 uint32_t instance_id);
771
772 /* In ip_fw_table.c */
773 struct table_info;
774
775 typedef int (table_lookup_t)(struct table_info *ti, void *key, uint32_t keylen,
776 uint32_t *val);
777
778 int ipfw_lookup_table(struct ip_fw_chain *ch, uint32_t tbl, uint16_t plen,
779 void *paddr, uint32_t *val);
780 struct named_object *ipfw_objhash_lookup_table_kidx(struct ip_fw_chain *ch,
781 uint32_t kidx);
782 int ipfw_ref_table(struct ip_fw_chain *ch, ipfw_obj_ntlv *ntlv, uint32_t *kidx);
783 void ipfw_unref_table(struct ip_fw_chain *ch, uint32_t kidx);
784 int ipfw_init_tables(struct ip_fw_chain *ch, int first);
785 int ipfw_resize_tables(struct ip_fw_chain *ch, unsigned int ntables);
786 int ipfw_switch_tables_namespace(struct ip_fw_chain *ch, unsigned int nsets);
787 void ipfw_destroy_tables(struct ip_fw_chain *ch, int last);
788
789 /* In ip_fw_nat.c -- XXX to be moved to ip_var.h */
790
791 extern struct cfg_nat *(*lookup_nat_ptr)(struct nat_list *, int);
792
793 typedef int ipfw_nat_t(struct ip_fw_args *, struct cfg_nat *, struct mbuf *);
794 typedef int ipfw_nat_cfg_t(struct sockopt *);
795
796 VNET_DECLARE(int, ipfw_nat_ready);
797 #define V_ipfw_nat_ready VNET(ipfw_nat_ready)
798 #define IPFW_NAT_LOADED (V_ipfw_nat_ready)
799
800 extern ipfw_nat_t *ipfw_nat_ptr;
801 extern ipfw_nat_cfg_t *ipfw_nat_cfg_ptr;
802 extern ipfw_nat_cfg_t *ipfw_nat_del_ptr;
803 extern ipfw_nat_cfg_t *ipfw_nat_get_cfg_ptr;
804 extern ipfw_nat_cfg_t *ipfw_nat_get_log_ptr;
805
806 /* Helper functions for IP checksum adjustment */
807 static __inline uint16_t
cksum_add(uint16_t sum,uint16_t a)808 cksum_add(uint16_t sum, uint16_t a)
809 {
810 uint16_t res;
811
812 res = sum + a;
813 return (res + (res < a));
814 }
815
816 static __inline uint16_t
cksum_adjust(uint16_t oldsum,uint16_t old,uint16_t new)817 cksum_adjust(uint16_t oldsum, uint16_t old, uint16_t new)
818 {
819
820 return (~cksum_add(cksum_add(~oldsum, ~old), new));
821 }
822
823 #endif /* _KERNEL */
824 #endif /* _IPFW2_PRIVATE_H */
825