2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002-2009 Luigi Rizzo, Universita` di Pisa
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
30 #ifndef _IPFW2_PRIVATE_H
31 #define _IPFW2_PRIVATE_H
34 * Internal constants and data structures used by ipfw components
35 * and not meant to be exported outside the kernel.
41 * For platforms that do not have SYSCTL support, we wrap the
42 * SYSCTL_* into a function (one per file) to collect the values
43 * into an array at module initialization. The wrapping macros,
44 * SYSBEGIN() and SYSEND, are empty in the default case.
53 /* Return values from ipfw_chk() */
67 * Structure for collecting parameters to dummynet for ip6_output forwarding
70 struct ip6_pktopts *opt_or;
72 struct ip6_moptions *im6o_or;
73 struct ifnet *origifp_or;
75 struct sockaddr_in6 dst_or;
81 * Arguments for calling ipfw_chk() and dummynet_io(). We put them
82 * all into a structure because this way it is easier and more
83 * efficient to pass variables around and extend the interface.
86 struct mbuf *m; /* the mbuf chain */
87 struct ifnet *oif; /* output interface */
88 struct sockaddr_in *next_hop; /* forward address */
89 struct sockaddr_in6 *next_hop6; /* ipv6 forward address */
92 * On return, it points to the matching rule.
93 * On entry, rule.slot > 0 means the info is valid and
94 * contains the starting rule for an ipfw search.
95 * If chain_id == chain->id && slot >0 then jump to that slot.
96 * Otherwise, we locate the first rule >= rulenum:rule_id
98 struct ipfw_rule_ref rule; /* match/restart info */
100 struct ether_header *eh; /* for bridged packets */
102 struct ipfw_flow_id f_id; /* grabbed from IP header */
103 //uint32_t cookie; /* a cookie depending on rule action */
106 struct _ip6dn_args dummypar; /* dummynet->ip6_output */
107 union { /* store here if cannot use a pointer */
108 struct sockaddr_in hopstore;
109 struct sockaddr_in6 hopstore6;
113 MALLOC_DECLARE(M_IPFW);
116 * Hooks sometime need to know the direction of the packet
117 * (divert, dummynet, netgraph, ...)
118 * We use a generic definition here, with bit0-1 indicating the
119 * direction, bit 2 indicating layer2 or 3, bit 3-4 indicating the
121 * indicating the protocol (if necessary)
129 PROTO_LAYER2 = 0x4, /* set for layer 2 */
130 /* PROTO_DEFAULT = 0, */
133 PROTO_IFB = 0x0c, /* layer2 + ifbridge */
134 /* PROTO_OLDBDG = 0x14, unused, old bridge */
137 /* wrapper for freeing a packet, in case we need to do more work */
139 #if defined(__linux__) || defined(_WIN32)
140 #define FREE_PKT(m) netisr_dispatch(-1, m)
142 #define FREE_PKT(m) m_freem(m)
144 #endif /* !FREE_PKT */
147 * Function definitions.
150 /* attach (arg = 1) or detach (arg = 0) hooks */
151 int ipfw_attach_hooks(int);
153 void ipfw_nat_destroy(void);
159 void ipfw_bpf_init(int);
160 void ipfw_bpf_uninit(int);
161 void ipfw_bpf_mtap2(void *, u_int, struct mbuf *);
162 void ipfw_log(struct ip_fw_chain *chain, struct ip_fw *f, u_int hlen,
163 struct ip_fw_args *args, struct mbuf *m, struct ifnet *oif,
164 u_short offset, uint32_t tablearg, struct ip *ip);
165 VNET_DECLARE(u_int64_t, norule_counter);
166 #define V_norule_counter VNET(norule_counter)
167 VNET_DECLARE(int, verbose_limit);
168 #define V_verbose_limit VNET(verbose_limit)
170 /* In ip_fw_dynamic.c */
172 enum { /* result for matching dynamic rules */
180 * The lock for dynamic rules is only used once outside the file,
181 * and only to release the result of lookup_dyn_rule().
182 * Eventually we may implement it with a callback on the function.
186 int ipfw_is_dyn_rule(struct ip_fw *rule);
187 void ipfw_expire_dyn_rules(struct ip_fw_chain *, ipfw_range_tlv *);
190 struct mbuf *ipfw_send_pkt(struct mbuf *, struct ipfw_flow_id *,
191 u_int32_t, u_int32_t, int);
192 int ipfw_dyn_install_state(struct ip_fw_chain *chain, struct ip_fw *rule,
193 ipfw_insn_limit *cmd, struct ip_fw_args *args, uint32_t tablearg);
194 struct ip_fw *ipfw_dyn_lookup_state(const struct ipfw_flow_id *pkt,
195 const void *ulp, int pktlen, int *match_direction, uint16_t kidx);
196 void ipfw_remove_dyn_children(struct ip_fw *rule);
197 void ipfw_get_dynamic(struct ip_fw_chain *chain, char **bp, const char *ep);
198 int ipfw_dump_states(struct ip_fw_chain *chain, struct sockopt_data *sd);
200 void ipfw_dyn_init(struct ip_fw_chain *); /* per-vnet initialization */
201 void ipfw_dyn_uninit(int); /* per-vnet deinitialization */
202 int ipfw_dyn_len(void);
203 int ipfw_dyn_get_count(void);
205 /* common variables */
206 VNET_DECLARE(int, fw_one_pass);
207 #define V_fw_one_pass VNET(fw_one_pass)
209 VNET_DECLARE(int, fw_verbose);
210 #define V_fw_verbose VNET(fw_verbose)
212 VNET_DECLARE(struct ip_fw_chain, layer3_chain);
213 #define V_layer3_chain VNET(layer3_chain)
215 VNET_DECLARE(int, ipfw_vnet_ready);
216 #define V_ipfw_vnet_ready VNET(ipfw_vnet_ready)
218 VNET_DECLARE(u_int32_t, set_disable);
219 #define V_set_disable VNET(set_disable)
221 VNET_DECLARE(int, autoinc_step);
222 #define V_autoinc_step VNET(autoinc_step)
224 VNET_DECLARE(unsigned int, fw_tables_max);
225 #define V_fw_tables_max VNET(fw_tables_max)
227 VNET_DECLARE(unsigned int, fw_tables_sets);
228 #define V_fw_tables_sets VNET(fw_tables_sets)
230 struct tables_config;
234 * Here we have the structure representing an ipfw rule.
236 * It starts with a general area
237 * followed by an array of one or more instructions, which the code
238 * accesses as an array of 32-bit values.
240 * Given a rule pointer r:
242 * r->cmd is the start of the first instruction.
243 * ACTION_PTR(r) is the start of the first action (things to do
244 * once a rule matched).
248 uint16_t act_ofs; /* offset of action in 32-bit units */
249 uint16_t cmd_len; /* # of 32-bit words in cmd */
250 uint16_t rulenum; /* rule number */
251 uint8_t set; /* rule set (0..31) */
252 uint8_t flags; /* currently unused */
253 counter_u64_t cntr; /* Pointer to rule counters */
254 uint32_t timestamp; /* tv_sec of last match */
255 uint32_t id; /* rule id */
256 uint32_t cached_id; /* used by jump_fast */
257 uint32_t cached_pos; /* used by jump_fast */
259 ipfw_insn cmd[1]; /* storage for commands */
262 #define IPFW_RULE_CNTR_SIZE (2 * sizeof(uint64_t))
267 struct ip_fw **map; /* array of rule ptrs to ease lookup */
268 uint32_t id; /* ruleset id */
269 int n_rules; /* number of static rules */
270 void *tablestate; /* runtime table info */
271 void *valuestate; /* runtime table value info */
272 int *idxmap; /* skipto array of rules */
273 void **srvstate; /* runtime service mappings */
274 #if defined( __linux__ ) || defined( _WIN32 )
277 int static_len; /* total len of static rules (v0) */
278 uint32_t gencnt; /* NAT generation count */
279 LIST_HEAD(nat_list, cfg_nat) nat; /* list of nat entries */
280 struct ip_fw *default_rule;
281 struct tables_config *tblcfg; /* tables module data */
282 void *ifcfg; /* interface module data */
283 int *idxmap_back; /* standby skipto array of rules */
284 struct namedobj_instance *srvmap; /* cfg name->number mappings */
285 #if defined( __linux__ ) || defined( _WIN32 )
288 struct rwlock uh_lock; /* lock for upper half */
292 /* 64-byte structure representing multi-field table value */
294 uint32_t tag; /* O_TAG/O_TAGGED */
295 uint32_t pipe; /* O_PIPE/O_QUEUE */
296 uint16_t divert; /* O_DIVERT/O_TEE */
297 uint16_t skipto; /* skipto, CALLRET */
298 uint32_t netgraph; /* O_NETGRAPH/O_NGTEE */
299 uint32_t fib; /* O_SETFIB */
300 uint32_t nat; /* O_NAT */
307 uint32_t limit; /* O_LIMIT */
308 uint32_t zoneid; /* scope zone id for nh6 */
309 uint64_t refcnt; /* Number of references */
313 struct named_object {
314 TAILQ_ENTRY(named_object) nn_next; /* namehash */
315 TAILQ_ENTRY(named_object) nv_next; /* valuehash */
316 char *name; /* object name */
317 uint16_t etlv; /* Export TLV id */
318 uint8_t subtype;/* object subtype within class */
319 uint8_t set; /* set object belongs to */
320 uint16_t kidx; /* object kernel index */
322 uint32_t ocnt; /* object counter for internal use */
323 uint32_t refcnt; /* number of references */
325 TAILQ_HEAD(namedobjects_head, named_object);
327 struct sockopt; /* used by tcp_var.h */
328 struct sockopt_data {
329 caddr_t kbuf; /* allocated buffer */
330 size_t ksize; /* given buffer size */
331 size_t koff; /* data already used */
332 size_t kavail; /* number of bytes available */
333 size_t ktotal; /* total bytes pushed */
334 struct sockopt *sopt; /* socket data */
335 caddr_t sopt_val; /* sopt user buffer */
336 size_t valsize; /* original data size */
341 typedef void (ipfw_ifc_cb)(struct ip_fw_chain *ch, void *cbdata,
345 struct named_object no;
351 TAILQ_HEAD(, ipfw_ifc) consumers;
355 TAILQ_ENTRY(ipfw_ifc) next;
356 struct ipfw_iface *iface;
361 /* Macro for working with various counters */
362 #define IPFW_INC_RULE_COUNTER(_cntr, _bytes) do { \
363 counter_u64_add((_cntr)->cntr, 1); \
364 counter_u64_add((_cntr)->cntr + 1, _bytes); \
365 if ((_cntr)->timestamp != time_uptime) \
366 (_cntr)->timestamp = time_uptime; \
369 #define IPFW_INC_DYN_COUNTER(_cntr, _bytes) do { \
371 (_cntr)->bcnt += _bytes; \
374 #define IPFW_ZERO_RULE_COUNTER(_cntr) do { \
375 counter_u64_zero((_cntr)->cntr); \
376 counter_u64_zero((_cntr)->cntr + 1); \
377 (_cntr)->timestamp = 0; \
380 #define IPFW_ZERO_DYN_COUNTER(_cntr) do { \
385 #define TARG_VAL(ch, k, f) ((struct table_value *)((ch)->valuestate))[k].f
386 #define IP_FW_ARG_TABLEARG(ch, a, f) \
387 (((a) == IP_FW_TARG) ? TARG_VAL(ch, tablearg, f) : (a))
389 * The lock is heavily used by ip_fw2.c (the main file) and ip_fw_nat.c
390 * so the variable and the macros must be here.
393 #if defined( __linux__ ) || defined( _WIN32 )
394 #define IPFW_LOCK_INIT(_chain) do { \
395 rw_init(&(_chain)->rwmtx, "IPFW static rules"); \
396 rw_init(&(_chain)->uh_lock, "IPFW UH lock"); \
399 #define IPFW_LOCK_DESTROY(_chain) do { \
400 rw_destroy(&(_chain)->rwmtx); \
401 rw_destroy(&(_chain)->uh_lock); \
404 #define IPFW_RLOCK_ASSERT(_chain) rw_assert(&(_chain)->rwmtx, RA_RLOCKED)
405 #define IPFW_WLOCK_ASSERT(_chain) rw_assert(&(_chain)->rwmtx, RA_WLOCKED)
407 #define IPFW_RLOCK_TRACKER
408 #define IPFW_RLOCK(p) rw_rlock(&(p)->rwmtx)
409 #define IPFW_RUNLOCK(p) rw_runlock(&(p)->rwmtx)
410 #define IPFW_WLOCK(p) rw_wlock(&(p)->rwmtx)
411 #define IPFW_WUNLOCK(p) rw_wunlock(&(p)->rwmtx)
412 #define IPFW_PF_RLOCK(p) IPFW_RLOCK(p)
413 #define IPFW_PF_RUNLOCK(p) IPFW_RUNLOCK(p)
415 #define IPFW_LOCK_INIT(_chain) do { \
416 rw_init(&(_chain)->uh_lock, "IPFW UH lock"); \
419 #define IPFW_LOCK_DESTROY(_chain) do { \
420 rw_destroy(&(_chain)->uh_lock); \
423 #define IPFW_RLOCK_ASSERT(_chain) rm_assert(&V_pfil_lock, RA_RLOCKED)
424 #define IPFW_WLOCK_ASSERT(_chain) rm_assert(&V_pfil_lock, RA_WLOCKED)
426 #define IPFW_RLOCK_TRACKER struct rm_priotracker _tracker
427 #define IPFW_RLOCK(p) rm_rlock(&V_pfil_lock, &_tracker)
428 #define IPFW_RUNLOCK(p) rm_runlock(&V_pfil_lock, &_tracker)
429 #define IPFW_WLOCK(p) rm_wlock(&V_pfil_lock)
430 #define IPFW_WUNLOCK(p) rm_wunlock(&V_pfil_lock)
431 #define IPFW_PF_RLOCK(p)
432 #define IPFW_PF_RUNLOCK(p)
435 #define IPFW_UH_RLOCK_ASSERT(_chain) rw_assert(&(_chain)->uh_lock, RA_RLOCKED)
436 #define IPFW_UH_WLOCK_ASSERT(_chain) rw_assert(&(_chain)->uh_lock, RA_WLOCKED)
437 #define IPFW_UH_UNLOCK_ASSERT(_chain) rw_assert(&(_chain)->uh_lock, RA_UNLOCKED)
439 #define IPFW_UH_RLOCK(p) rw_rlock(&(p)->uh_lock)
440 #define IPFW_UH_RUNLOCK(p) rw_runlock(&(p)->uh_lock)
441 #define IPFW_UH_WLOCK(p) rw_wlock(&(p)->uh_lock)
442 #define IPFW_UH_WUNLOCK(p) rw_wunlock(&(p)->uh_lock)
445 uint16_t uidx; /* internal index supplied by userland */
446 uint16_t kidx; /* kernel object index */
447 uint16_t off; /* tlv offset from rule end in 4-byte words */
449 uint8_t type; /* object type within its category */
452 struct rule_check_info {
453 uint16_t flags; /* rule-specific check flags */
454 uint16_t object_opcodes; /* num of opcodes referencing objects */
455 uint16_t urule_numoff; /* offset of rulenum in bytes */
456 uint8_t version; /* rule version */
458 ipfw_obj_ctlv *ctlv; /* name TLV containter */
459 struct ip_fw *krule; /* resulting rule pointer */
460 caddr_t urule; /* original rule pointer */
461 struct obj_idx obuf[8]; /* table references storage */
464 /* Legacy interface support */
466 * FreeBSD 8 export rule format
469 struct ip_fw *x_next; /* linked list of rules */
470 struct ip_fw *next_rule; /* ptr to next [skipto] rule */
471 /* 'next_rule' is used to pass up 'set_disable' status */
473 uint16_t act_ofs; /* offset of action in 32-bit units */
474 uint16_t cmd_len; /* # of 32-bit words in cmd */
475 uint16_t rulenum; /* rule number */
476 uint8_t set; /* rule set (0..31) */
477 uint8_t _pad; /* padding */
478 uint32_t id; /* rule id */
480 /* These fields are present in all rules. */
481 uint64_t pcnt; /* Packet counter */
482 uint64_t bcnt; /* Byte counter */
483 uint32_t timestamp; /* tv_sec of last match */
485 ipfw_insn cmd[1]; /* storage for commands */
488 struct ip_fw_bcounter0 {
489 uint64_t pcnt; /* Packet counter */
490 uint64_t bcnt; /* Byte counter */
491 uint32_t timestamp; /* tv_sec of last match */
494 /* Kernel rule length */
496 * RULE _K_ SIZE _V_ ->
497 * get kernel size from userland rool version _V_.
498 * RULE _U_ SIZE _V_ ->
499 * get user size version _V_ from kernel rule
501 * get user size rule length
503 /* FreeBSD8 <> current kernel format */
504 #define RULEUSIZE0(r) (sizeof(struct ip_fw_rule0) + (r)->cmd_len * 4 - 4)
505 #define RULEKSIZE0(r) roundup2((sizeof(struct ip_fw) + (r)->cmd_len*4 - 4), 8)
506 /* FreeBSD11 <> current kernel format */
507 #define RULEUSIZE1(r) (roundup2(sizeof(struct ip_fw_rule) + \
508 (r)->cmd_len * 4 - 4, 8))
509 #define RULEKSIZE1(r) roundup2((sizeof(struct ip_fw) + (r)->cmd_len*4 - 4), 8)
512 * Tables/Objects index rewriting code
515 /* Default and maximum number of ipfw tables/objects. */
516 #define IPFW_TABLES_MAX 65536
517 #define IPFW_TABLES_DEFAULT 128
518 #define IPFW_OBJECTS_MAX 65536
519 #define IPFW_OBJECTS_DEFAULT 1024
521 #define CHAIN_TO_SRV(ch) ((ch)->srvmap)
522 #define SRV_OBJECT(ch, idx) ((ch)->srvstate[(idx)])
525 uint32_t set; /* table set */
526 uint16_t uidx; /* table index */
527 uint8_t type; /* table type */
530 int tlen; /* Total TLV size block */
531 void *tlvs; /* Pointer to first TLV */
535 * Classifier callback. Checks if @cmd opcode contains kernel object reference.
536 * If true, returns its index and type.
537 * Returns 0 if match is found, 1 overwise.
539 typedef int (ipfw_obj_rw_cl)(ipfw_insn *cmd, uint16_t *puidx, uint8_t *ptype);
541 * Updater callback. Sets kernel object reference index to @puidx
543 typedef void (ipfw_obj_rw_upd)(ipfw_insn *cmd, uint16_t puidx);
545 * Finder callback. Tries to find named object by name (specified via @ti).
546 * Stores found named object pointer in @pno.
547 * If object was not found, NULL is stored.
549 * Return 0 if input data was valid.
551 typedef int (ipfw_obj_fname_cb)(struct ip_fw_chain *ch,
552 struct tid_info *ti, struct named_object **pno);
554 * Another finder callback. Tries to findex named object by kernel index.
556 * Returns pointer to named object or NULL.
558 typedef struct named_object *(ipfw_obj_fidx_cb)(struct ip_fw_chain *ch,
561 * Object creator callback. Tries to create object specified by @ti.
562 * Stores newly-allocated object index in @pkidx.
564 * Returns 0 on success.
566 typedef int (ipfw_obj_create_cb)(struct ip_fw_chain *ch, struct tid_info *ti,
569 * Object destroy callback. Intended to free resources allocated by
570 * create_object callback.
572 typedef void (ipfw_obj_destroy_cb)(struct ip_fw_chain *ch,
573 struct named_object *no);
575 * Sets handler callback. Handles moving and swaping set of named object.
576 * SWAP_ALL moves all named objects from set `set' to `new_set' and vise versa;
577 * TEST_ALL checks that there aren't any named object with conflicting names;
578 * MOVE_ALL moves all named objects from set `set' to `new_set';
579 * COUNT_ONE used to count number of references used by object with kidx `set';
580 * TEST_ONE checks that named object with kidx `set' can be moved to `new_set`;
581 * MOVE_ONE moves named object with kidx `set' to set `new_set'.
584 SWAP_ALL = 0, TEST_ALL, MOVE_ALL, COUNT_ONE, TEST_ONE, MOVE_ONE
586 typedef int (ipfw_obj_sets_cb)(struct ip_fw_chain *ch,
587 uint16_t set, uint8_t new_set, enum ipfw_sets_cmd cmd);
590 struct opcode_obj_rewrite {
591 uint32_t opcode; /* Opcode to act upon */
592 uint32_t etlv; /* Relevant export TLV id */
593 ipfw_obj_rw_cl *classifier; /* Check if rewrite is needed */
594 ipfw_obj_rw_upd *update; /* update cmd with new value */
595 ipfw_obj_fname_cb *find_byname; /* Find named object by name */
596 ipfw_obj_fidx_cb *find_bykidx; /* Find named object by kidx */
597 ipfw_obj_create_cb *create_object; /* Create named object */
598 ipfw_obj_destroy_cb *destroy_object;/* Destroy named object */
599 ipfw_obj_sets_cb *manage_sets; /* Swap or move sets */
602 #define IPFW_ADD_OBJ_REWRITER(f, c) do { \
604 ipfw_add_obj_rewriter(c, \
605 sizeof(c) / sizeof(c[0])); \
607 #define IPFW_DEL_OBJ_REWRITER(l, c) do { \
609 ipfw_del_obj_rewriter(c, \
610 sizeof(c) / sizeof(c[0])); \
613 /* In ip_fw_iface.c */
614 int ipfw_iface_init(void);
615 void ipfw_iface_destroy(void);
616 void vnet_ipfw_iface_destroy(struct ip_fw_chain *ch);
617 int ipfw_iface_ref(struct ip_fw_chain *ch, char *name,
618 struct ipfw_ifc *ic);
619 void ipfw_iface_unref(struct ip_fw_chain *ch, struct ipfw_ifc *ic);
620 void ipfw_iface_add_notify(struct ip_fw_chain *ch, struct ipfw_ifc *ic);
621 void ipfw_iface_del_notify(struct ip_fw_chain *ch, struct ipfw_ifc *ic);
623 /* In ip_fw_sockopt.c */
624 void ipfw_init_skipto_cache(struct ip_fw_chain *chain);
625 void ipfw_destroy_skipto_cache(struct ip_fw_chain *chain);
626 int ipfw_find_rule(struct ip_fw_chain *chain, uint32_t key, uint32_t id);
627 int ipfw_ctl3(struct sockopt *sopt);
628 int ipfw_chk(struct ip_fw_args *args);
629 int ipfw_add_protected_rule(struct ip_fw_chain *chain, struct ip_fw *rule,
631 void ipfw_reap_add(struct ip_fw_chain *chain, struct ip_fw **head,
633 void ipfw_reap_rules(struct ip_fw *head);
634 void ipfw_init_counters(void);
635 void ipfw_destroy_counters(void);
636 struct ip_fw *ipfw_alloc_rule(struct ip_fw_chain *chain, size_t rulesize);
637 int ipfw_match_range(struct ip_fw *rule, ipfw_range_tlv *rt);
639 typedef int (sopt_handler_f)(struct ip_fw_chain *ch,
640 ip_fw3_opheader *op3, struct sockopt_data *sd);
641 struct ipfw_sopt_handler {
645 sopt_handler_f *handler;
648 #define HDIR_SET 0x01 /* Handler is used to set some data */
649 #define HDIR_GET 0x02 /* Handler is used to retrieve data */
650 #define HDIR_BOTH HDIR_GET|HDIR_SET
652 void ipfw_init_sopt_handler(void);
653 void ipfw_destroy_sopt_handler(void);
654 void ipfw_add_sopt_handler(struct ipfw_sopt_handler *sh, size_t count);
655 int ipfw_del_sopt_handler(struct ipfw_sopt_handler *sh, size_t count);
656 caddr_t ipfw_get_sopt_space(struct sockopt_data *sd, size_t needed);
657 caddr_t ipfw_get_sopt_header(struct sockopt_data *sd, size_t needed);
658 #define IPFW_ADD_SOPT_HANDLER(f, c) do { \
660 ipfw_add_sopt_handler(c, \
661 sizeof(c) / sizeof(c[0])); \
663 #define IPFW_DEL_SOPT_HANDLER(l, c) do { \
665 ipfw_del_sopt_handler(c, \
666 sizeof(c) / sizeof(c[0])); \
669 struct namedobj_instance;
670 typedef int (objhash_cb_t)(struct namedobj_instance *ni, struct named_object *,
672 typedef uint32_t (objhash_hash_f)(struct namedobj_instance *ni, const void *key,
674 typedef int (objhash_cmp_f)(struct named_object *no, const void *key,
676 struct namedobj_instance *ipfw_objhash_create(uint32_t items);
677 void ipfw_objhash_destroy(struct namedobj_instance *);
678 void ipfw_objhash_bitmap_alloc(uint32_t items, void **idx, int *pblocks);
679 void ipfw_objhash_bitmap_merge(struct namedobj_instance *ni,
680 void **idx, int *blocks);
681 void ipfw_objhash_bitmap_swap(struct namedobj_instance *ni,
682 void **idx, int *blocks);
683 void ipfw_objhash_bitmap_free(void *idx, int blocks);
684 void ipfw_objhash_set_hashf(struct namedobj_instance *ni, objhash_hash_f *f);
685 struct named_object *ipfw_objhash_lookup_name(struct namedobj_instance *ni,
686 uint32_t set, char *name);
687 struct named_object *ipfw_objhash_lookup_name_type(struct namedobj_instance *ni,
688 uint32_t set, uint32_t type, const char *name);
689 struct named_object *ipfw_objhash_lookup_kidx(struct namedobj_instance *ni,
691 int ipfw_objhash_same_name(struct namedobj_instance *ni, struct named_object *a,
692 struct named_object *b);
693 void ipfw_objhash_add(struct namedobj_instance *ni, struct named_object *no);
694 void ipfw_objhash_del(struct namedobj_instance *ni, struct named_object *no);
695 uint32_t ipfw_objhash_count(struct namedobj_instance *ni);
696 uint32_t ipfw_objhash_count_type(struct namedobj_instance *ni, uint16_t type);
697 int ipfw_objhash_foreach(struct namedobj_instance *ni, objhash_cb_t *f,
699 int ipfw_objhash_foreach_type(struct namedobj_instance *ni, objhash_cb_t *f,
700 void *arg, uint16_t type);
701 int ipfw_objhash_free_idx(struct namedobj_instance *ni, uint16_t idx);
702 int ipfw_objhash_alloc_idx(void *n, uint16_t *pidx);
703 void ipfw_objhash_set_funcs(struct namedobj_instance *ni,
704 objhash_hash_f *hash_f, objhash_cmp_f *cmp_f);
705 int ipfw_objhash_find_type(struct namedobj_instance *ni, struct tid_info *ti,
706 uint32_t etlv, struct named_object **pno);
707 void ipfw_export_obj_ntlv(struct named_object *no, ipfw_obj_ntlv *ntlv);
708 ipfw_obj_ntlv *ipfw_find_name_tlv_type(void *tlvs, int len, uint16_t uidx,
710 void ipfw_init_obj_rewriter(void);
711 void ipfw_destroy_obj_rewriter(void);
712 void ipfw_add_obj_rewriter(struct opcode_obj_rewrite *rw, size_t count);
713 int ipfw_del_obj_rewriter(struct opcode_obj_rewrite *rw, size_t count);
715 int create_objects_compat(struct ip_fw_chain *ch, ipfw_insn *cmd,
716 struct obj_idx *oib, struct obj_idx *pidx, struct tid_info *ti);
717 void update_opcode_kidx(ipfw_insn *cmd, uint16_t idx);
718 int classify_opcode_kidx(ipfw_insn *cmd, uint16_t *puidx);
719 void ipfw_init_srv(struct ip_fw_chain *ch);
720 void ipfw_destroy_srv(struct ip_fw_chain *ch);
721 int ipfw_check_object_name_generic(const char *name);
722 int ipfw_obj_manage_sets(struct namedobj_instance *ni, uint16_t type,
723 uint16_t set, uint8_t new_set, enum ipfw_sets_cmd cmd);
725 /* In ip_fw_eaction.c */
726 typedef int (ipfw_eaction_t)(struct ip_fw_chain *ch, struct ip_fw_args *args,
727 ipfw_insn *cmd, int *done);
728 int ipfw_eaction_init(struct ip_fw_chain *ch, int first);
729 void ipfw_eaction_uninit(struct ip_fw_chain *ch, int last);
731 uint16_t ipfw_add_eaction(struct ip_fw_chain *ch, ipfw_eaction_t handler,
733 int ipfw_del_eaction(struct ip_fw_chain *ch, uint16_t eaction_id);
734 int ipfw_run_eaction(struct ip_fw_chain *ch, struct ip_fw_args *args,
735 ipfw_insn *cmd, int *done);
737 /* In ip_fw_table.c */
740 typedef int (table_lookup_t)(struct table_info *ti, void *key, uint32_t keylen,
743 int ipfw_lookup_table(struct ip_fw_chain *ch, uint16_t tbl, uint16_t plen,
744 void *paddr, uint32_t *val);
745 struct named_object *ipfw_objhash_lookup_table_kidx(struct ip_fw_chain *ch,
747 int ipfw_ref_table(struct ip_fw_chain *ch, ipfw_obj_ntlv *ntlv, uint16_t *kidx);
748 void ipfw_unref_table(struct ip_fw_chain *ch, uint16_t kidx);
749 int ipfw_init_tables(struct ip_fw_chain *ch, int first);
750 int ipfw_resize_tables(struct ip_fw_chain *ch, unsigned int ntables);
751 int ipfw_switch_tables_namespace(struct ip_fw_chain *ch, unsigned int nsets);
752 void ipfw_destroy_tables(struct ip_fw_chain *ch, int last);
754 /* In ip_fw_nat.c -- XXX to be moved to ip_var.h */
756 extern struct cfg_nat *(*lookup_nat_ptr)(struct nat_list *, int);
758 typedef int ipfw_nat_t(struct ip_fw_args *, struct cfg_nat *, struct mbuf *);
759 typedef int ipfw_nat_cfg_t(struct sockopt *);
761 VNET_DECLARE(int, ipfw_nat_ready);
762 #define V_ipfw_nat_ready VNET(ipfw_nat_ready)
763 #define IPFW_NAT_LOADED (V_ipfw_nat_ready)
765 extern ipfw_nat_t *ipfw_nat_ptr;
766 extern ipfw_nat_cfg_t *ipfw_nat_cfg_ptr;
767 extern ipfw_nat_cfg_t *ipfw_nat_del_ptr;
768 extern ipfw_nat_cfg_t *ipfw_nat_get_cfg_ptr;
769 extern ipfw_nat_cfg_t *ipfw_nat_get_log_ptr;
771 /* Helper functions for IP checksum adjustment */
772 static __inline uint16_t
773 cksum_add(uint16_t sum, uint16_t a)
778 return (res + (res < a));
781 static __inline uint16_t
782 cksum_adjust(uint16_t oldsum, uint16_t old, uint16_t new)
785 return (~cksum_add(cksum_add(~oldsum, ~old), new));
789 #endif /* _IPFW2_PRIVATE_H */