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.
87 #define IPFW_ARGS_ETHER 0x0001 /* has valid ethernet header */
88 #define IPFW_ARGS_NH4 0x0002 /* has IPv4 next hop in hopstore */
89 #define IPFW_ARGS_NH6 0x0004 /* has IPv6 next hop in hopstore */
90 #define IPFW_ARGS_NH4PTR 0x0008 /* has IPv4 next hop in next_hop */
91 #define IPFW_ARGS_NH6PTR 0x0010 /* has IPv6 next hop in next_hop6 */
92 #define IPFW_ARGS_REF 0x0020 /* has valid ipfw_rule_ref */
94 * On return, it points to the matching rule.
95 * On entry, rule.slot > 0 means the info is valid and
96 * contains the starting rule for an ipfw search.
97 * If chain_id == chain->id && slot >0 then jump to that slot.
98 * Otherwise, we locate the first rule >= rulenum:rule_id
100 struct ipfw_rule_ref rule; /* match/restart info */
102 struct ifnet *oif; /* output interface */
106 * We don't support forwarding on layer2, thus we can
107 * keep eh pointer in this union.
108 * next_hop[6] pointers can be used to point to next hop
109 * stored in rule's opcode to avoid copying into hopstore.
110 * Also, it is expected that all 0x1-0x10 flags are mutually
113 struct ether_header *eh; /* for bridged packets */
114 struct sockaddr_in *next_hop;
115 struct sockaddr_in6 *next_hop6;
116 /* ipfw next hop storage */
117 struct sockaddr_in hopstore;
119 struct in6_addr sin6_addr;
120 uint32_t sin6_scope_id;
125 struct mbuf *m; /* the mbuf chain */
126 struct ipfw_flow_id f_id; /* grabbed from IP header */
129 MALLOC_DECLARE(M_IPFW);
132 * Hooks sometime need to know the direction of the packet
133 * (divert, dummynet, netgraph, ...)
134 * We use a generic definition here, with bit0-1 indicating the
135 * direction, bit 2 indicating layer2 or 3, bit 3-4 indicating the
137 * indicating the protocol (if necessary)
145 PROTO_LAYER2 = 0x4, /* set for layer 2 */
146 /* PROTO_DEFAULT = 0, */
149 PROTO_IFB = 0x0c, /* layer2 + ifbridge */
150 /* PROTO_OLDBDG = 0x14, unused, old bridge */
153 /* wrapper for freeing a packet, in case we need to do more work */
155 #if defined(__linux__) || defined(_WIN32)
156 #define FREE_PKT(m) netisr_dispatch(-1, m)
158 #define FREE_PKT(m) m_freem(m)
160 #endif /* !FREE_PKT */
163 * Function definitions.
165 int ipfw_chk(struct ip_fw_args *args);
166 struct mbuf *ipfw_send_pkt(struct mbuf *, struct ipfw_flow_id *,
167 u_int32_t, u_int32_t, int);
169 /* attach (arg = 1) or detach (arg = 0) hooks */
170 int ipfw_attach_hooks(int);
172 void ipfw_nat_destroy(void);
179 void ipfw_bpf_init(int);
180 void ipfw_bpf_uninit(int);
181 void ipfw_bpf_mtap2(void *, u_int, struct mbuf *);
182 void ipfw_log(struct ip_fw_chain *chain, struct ip_fw *f, u_int hlen,
183 struct ip_fw_args *args, struct mbuf *m, struct ifnet *oif,
184 u_short offset, uint32_t tablearg, struct ip *ip);
185 VNET_DECLARE(u_int64_t, norule_counter);
186 #define V_norule_counter VNET(norule_counter)
187 VNET_DECLARE(int, verbose_limit);
188 #define V_verbose_limit VNET(verbose_limit)
190 /* In ip_fw_dynamic.c */
193 enum { /* result for matching dynamic rules */
201 * Macro to determine that we need to do or redo dynamic state lookup.
202 * direction == MATCH_UNKNOWN means that this is first lookup, then we need
204 * Otherwise check the state name, if previous lookup was for "any" name,
205 * this means there is no state with specific name. Thus no need to do
206 * lookup. If previous name was not "any", redo lookup for specific name.
208 #define DYN_LOOKUP_NEEDED(p, cmd) \
209 ((p)->direction == MATCH_UNKNOWN || \
210 ((p)->kidx != 0 && (p)->kidx != (cmd)->arg1))
211 #define DYN_INFO_INIT(p) do { \
212 (p)->direction = MATCH_UNKNOWN; \
215 struct ipfw_dyn_info {
216 uint16_t direction; /* match direction */
217 uint16_t kidx; /* state name kidx */
218 uint32_t hashval; /* hash value */
219 uint32_t version; /* bucket version */
222 int ipfw_dyn_install_state(struct ip_fw_chain *chain, struct ip_fw *rule,
223 const ipfw_insn_limit *cmd, const struct ip_fw_args *args,
224 const void *ulp, int pktlen, struct ipfw_dyn_info *info,
226 struct ip_fw *ipfw_dyn_lookup_state(const struct ip_fw_args *args,
227 const void *ulp, int pktlen, const ipfw_insn *cmd,
228 struct ipfw_dyn_info *info);
230 int ipfw_is_dyn_rule(struct ip_fw *rule);
231 void ipfw_expire_dyn_states(struct ip_fw_chain *, ipfw_range_tlv *);
232 void ipfw_get_dynamic(struct ip_fw_chain *chain, char **bp, const char *ep);
233 int ipfw_dump_states(struct ip_fw_chain *chain, struct sockopt_data *sd);
235 void ipfw_dyn_init(struct ip_fw_chain *); /* per-vnet initialization */
236 void ipfw_dyn_uninit(int); /* per-vnet deinitialization */
237 int ipfw_dyn_len(void);
238 uint32_t ipfw_dyn_get_count(uint32_t *, int *);
239 void ipfw_dyn_reset_eaction(struct ip_fw_chain *ch, uint16_t eaction_id,
240 uint16_t default_id, uint16_t instance_id);
242 /* common variables */
243 VNET_DECLARE(int, fw_one_pass);
244 #define V_fw_one_pass VNET(fw_one_pass)
246 VNET_DECLARE(int, fw_verbose);
247 #define V_fw_verbose VNET(fw_verbose)
249 VNET_DECLARE(struct ip_fw_chain, layer3_chain);
250 #define V_layer3_chain VNET(layer3_chain)
252 VNET_DECLARE(int, ipfw_vnet_ready);
253 #define V_ipfw_vnet_ready VNET(ipfw_vnet_ready)
255 VNET_DECLARE(u_int32_t, set_disable);
256 #define V_set_disable VNET(set_disable)
258 VNET_DECLARE(int, autoinc_step);
259 #define V_autoinc_step VNET(autoinc_step)
261 VNET_DECLARE(unsigned int, fw_tables_max);
262 #define V_fw_tables_max VNET(fw_tables_max)
264 VNET_DECLARE(unsigned int, fw_tables_sets);
265 #define V_fw_tables_sets VNET(fw_tables_sets)
267 struct tables_config;
271 * Here we have the structure representing an ipfw rule.
273 * It starts with a general area
274 * followed by an array of one or more instructions, which the code
275 * accesses as an array of 32-bit values.
277 * Given a rule pointer r:
279 * r->cmd is the start of the first instruction.
280 * ACTION_PTR(r) is the start of the first action (things to do
281 * once a rule matched).
285 uint16_t act_ofs; /* offset of action in 32-bit units */
286 uint16_t cmd_len; /* # of 32-bit words in cmd */
287 uint16_t rulenum; /* rule number */
288 uint8_t set; /* rule set (0..31) */
289 uint8_t flags; /* currently unused */
290 counter_u64_t cntr; /* Pointer to rule counters */
291 uint32_t timestamp; /* tv_sec of last match */
292 uint32_t id; /* rule id */
293 uint32_t cached_id; /* used by jump_fast */
294 uint32_t cached_pos; /* used by jump_fast */
295 uint32_t refcnt; /* number of references */
297 struct ip_fw *next; /* linked list of deleted rules */
298 ipfw_insn cmd[1]; /* storage for commands */
301 #define IPFW_RULE_CNTR_SIZE (2 * sizeof(uint64_t))
306 struct ip_fw **map; /* array of rule ptrs to ease lookup */
307 uint32_t id; /* ruleset id */
308 int n_rules; /* number of static rules */
309 void *tablestate; /* runtime table info */
310 void *valuestate; /* runtime table value info */
311 int *idxmap; /* skipto array of rules */
312 void **srvstate; /* runtime service mappings */
313 #if defined( __linux__ ) || defined( _WIN32 )
318 int static_len; /* total len of static rules (v0) */
319 uint32_t gencnt; /* NAT generation count */
320 LIST_HEAD(nat_list, cfg_nat) nat; /* list of nat entries */
321 struct ip_fw *default_rule;
322 struct tables_config *tblcfg; /* tables module data */
323 void *ifcfg; /* interface module data */
324 int *idxmap_back; /* standby skipto array of rules */
325 struct namedobj_instance *srvmap; /* cfg name->number mappings */
326 #if defined( __linux__ ) || defined( _WIN32 )
329 struct rwlock uh_lock; /* lock for upper half */
333 /* 64-byte structure representing multi-field table value */
335 uint32_t tag; /* O_TAG/O_TAGGED */
336 uint32_t pipe; /* O_PIPE/O_QUEUE */
337 uint16_t divert; /* O_DIVERT/O_TEE */
338 uint16_t skipto; /* skipto, CALLRET */
339 uint32_t netgraph; /* O_NETGRAPH/O_NGTEE */
340 uint32_t fib; /* O_SETFIB */
341 uint32_t nat; /* O_NAT */
348 uint32_t limit; /* O_LIMIT */
349 uint32_t zoneid; /* scope zone id for nh6 */
350 uint64_t refcnt; /* Number of references */
354 struct named_object {
355 TAILQ_ENTRY(named_object) nn_next; /* namehash */
356 TAILQ_ENTRY(named_object) nv_next; /* valuehash */
357 char *name; /* object name */
358 uint16_t etlv; /* Export TLV id */
359 uint8_t subtype;/* object subtype within class */
360 uint8_t set; /* set object belongs to */
361 uint16_t kidx; /* object kernel index */
363 uint32_t ocnt; /* object counter for internal use */
364 uint32_t refcnt; /* number of references */
366 TAILQ_HEAD(namedobjects_head, named_object);
368 struct sockopt; /* used by tcp_var.h */
369 struct sockopt_data {
370 caddr_t kbuf; /* allocated buffer */
371 size_t ksize; /* given buffer size */
372 size_t koff; /* data already used */
373 size_t kavail; /* number of bytes available */
374 size_t ktotal; /* total bytes pushed */
375 struct sockopt *sopt; /* socket data */
376 caddr_t sopt_val; /* sopt user buffer */
377 size_t valsize; /* original data size */
382 typedef void (ipfw_ifc_cb)(struct ip_fw_chain *ch, void *cbdata,
386 struct named_object no;
392 TAILQ_HEAD(, ipfw_ifc) consumers;
396 TAILQ_ENTRY(ipfw_ifc) next;
397 struct ipfw_iface *iface;
402 /* Macro for working with various counters */
403 #define IPFW_INC_RULE_COUNTER(_cntr, _bytes) do { \
404 counter_u64_add((_cntr)->cntr, 1); \
405 counter_u64_add((_cntr)->cntr + 1, _bytes); \
406 if ((_cntr)->timestamp != time_uptime) \
407 (_cntr)->timestamp = time_uptime; \
410 #define IPFW_INC_DYN_COUNTER(_cntr, _bytes) do { \
412 (_cntr)->bcnt += _bytes; \
415 #define IPFW_ZERO_RULE_COUNTER(_cntr) do { \
416 counter_u64_zero((_cntr)->cntr); \
417 counter_u64_zero((_cntr)->cntr + 1); \
418 (_cntr)->timestamp = 0; \
421 #define IPFW_ZERO_DYN_COUNTER(_cntr) do { \
426 #define TARG_VAL(ch, k, f) ((struct table_value *)((ch)->valuestate))[k].f
427 #define IP_FW_ARG_TABLEARG(ch, a, f) \
428 (((a) == IP_FW_TARG) ? TARG_VAL(ch, tablearg, f) : (a))
430 * The lock is heavily used by ip_fw2.c (the main file) and ip_fw_nat.c
431 * so the variable and the macros must be here.
434 #if defined( __linux__ ) || defined( _WIN32 )
435 #define IPFW_LOCK_INIT(_chain) do { \
436 rw_init(&(_chain)->rwmtx, "IPFW static rules"); \
437 rw_init(&(_chain)->uh_lock, "IPFW UH lock"); \
440 #define IPFW_LOCK_DESTROY(_chain) do { \
441 rw_destroy(&(_chain)->rwmtx); \
442 rw_destroy(&(_chain)->uh_lock); \
445 #define IPFW_RLOCK_ASSERT(_chain) rw_assert(&(_chain)->rwmtx, RA_RLOCKED)
446 #define IPFW_WLOCK_ASSERT(_chain) rw_assert(&(_chain)->rwmtx, RA_WLOCKED)
448 #define IPFW_RLOCK_TRACKER
449 #define IPFW_RLOCK(p) rw_rlock(&(p)->rwmtx)
450 #define IPFW_RUNLOCK(p) rw_runlock(&(p)->rwmtx)
451 #define IPFW_WLOCK(p) rw_wlock(&(p)->rwmtx)
452 #define IPFW_WUNLOCK(p) rw_wunlock(&(p)->rwmtx)
453 #define IPFW_PF_RLOCK(p) IPFW_RLOCK(p)
454 #define IPFW_PF_RUNLOCK(p) IPFW_RUNLOCK(p)
456 #define IPFW_LOCK_INIT(_chain) do { \
457 rm_init_flags(&(_chain)->rwmtx, "IPFW static rules", RM_RECURSE); \
458 rw_init(&(_chain)->uh_lock, "IPFW UH lock"); \
461 #define IPFW_LOCK_DESTROY(_chain) do { \
462 rm_destroy(&(_chain)->rwmtx); \
463 rw_destroy(&(_chain)->uh_lock); \
466 #define IPFW_RLOCK_ASSERT(_chain) rm_assert(&(_chain)->rwmtx, RA_RLOCKED)
467 #define IPFW_WLOCK_ASSERT(_chain) rm_assert(&(_chain)->rwmtx, RA_WLOCKED)
469 #define IPFW_RLOCK_TRACKER struct rm_priotracker _tracker
470 #define IPFW_RLOCK(p) rm_rlock(&(p)->rwmtx, &_tracker)
471 #define IPFW_RUNLOCK(p) rm_runlock(&(p)->rwmtx, &_tracker)
472 #define IPFW_WLOCK(p) rm_wlock(&(p)->rwmtx)
473 #define IPFW_WUNLOCK(p) rm_wunlock(&(p)->rwmtx)
474 #define IPFW_PF_RLOCK(p) IPFW_RLOCK(p)
475 #define IPFW_PF_RUNLOCK(p) IPFW_RUNLOCK(p)
478 #define IPFW_UH_RLOCK_ASSERT(_chain) rw_assert(&(_chain)->uh_lock, RA_RLOCKED)
479 #define IPFW_UH_WLOCK_ASSERT(_chain) rw_assert(&(_chain)->uh_lock, RA_WLOCKED)
480 #define IPFW_UH_UNLOCK_ASSERT(_chain) rw_assert(&(_chain)->uh_lock, RA_UNLOCKED)
482 #define IPFW_UH_RLOCK(p) rw_rlock(&(p)->uh_lock)
483 #define IPFW_UH_RUNLOCK(p) rw_runlock(&(p)->uh_lock)
484 #define IPFW_UH_WLOCK(p) rw_wlock(&(p)->uh_lock)
485 #define IPFW_UH_WUNLOCK(p) rw_wunlock(&(p)->uh_lock)
488 uint16_t uidx; /* internal index supplied by userland */
489 uint16_t kidx; /* kernel object index */
490 uint16_t off; /* tlv offset from rule end in 4-byte words */
492 uint8_t type; /* object type within its category */
495 struct rule_check_info {
496 uint16_t flags; /* rule-specific check flags */
497 uint16_t object_opcodes; /* num of opcodes referencing objects */
498 uint16_t urule_numoff; /* offset of rulenum in bytes */
499 uint8_t version; /* rule version */
501 ipfw_obj_ctlv *ctlv; /* name TLV containter */
502 struct ip_fw *krule; /* resulting rule pointer */
503 caddr_t urule; /* original rule pointer */
504 struct obj_idx obuf[8]; /* table references storage */
507 /* Legacy interface support */
509 * FreeBSD 8 export rule format
512 struct ip_fw *x_next; /* linked list of rules */
513 struct ip_fw *next_rule; /* ptr to next [skipto] rule */
514 /* 'next_rule' is used to pass up 'set_disable' status */
516 uint16_t act_ofs; /* offset of action in 32-bit units */
517 uint16_t cmd_len; /* # of 32-bit words in cmd */
518 uint16_t rulenum; /* rule number */
519 uint8_t set; /* rule set (0..31) */
520 uint8_t _pad; /* padding */
521 uint32_t id; /* rule id */
523 /* These fields are present in all rules. */
524 uint64_t pcnt; /* Packet counter */
525 uint64_t bcnt; /* Byte counter */
526 uint32_t timestamp; /* tv_sec of last match */
528 ipfw_insn cmd[1]; /* storage for commands */
531 struct ip_fw_bcounter0 {
532 uint64_t pcnt; /* Packet counter */
533 uint64_t bcnt; /* Byte counter */
534 uint32_t timestamp; /* tv_sec of last match */
537 /* Kernel rule length */
539 * RULE _K_ SIZE _V_ ->
540 * get kernel size from userland rool version _V_.
541 * RULE _U_ SIZE _V_ ->
542 * get user size version _V_ from kernel rule
544 * get user size rule length
546 /* FreeBSD8 <> current kernel format */
547 #define RULEUSIZE0(r) (sizeof(struct ip_fw_rule0) + (r)->cmd_len * 4 - 4)
548 #define RULEKSIZE0(r) roundup2((sizeof(struct ip_fw) + (r)->cmd_len*4 - 4), 8)
549 /* FreeBSD11 <> current kernel format */
550 #define RULEUSIZE1(r) (roundup2(sizeof(struct ip_fw_rule) + \
551 (r)->cmd_len * 4 - 4, 8))
552 #define RULEKSIZE1(r) roundup2((sizeof(struct ip_fw) + (r)->cmd_len*4 - 4), 8)
555 * Tables/Objects index rewriting code
558 /* Default and maximum number of ipfw tables/objects. */
559 #define IPFW_TABLES_MAX 65536
560 #define IPFW_TABLES_DEFAULT 128
561 #define IPFW_OBJECTS_MAX 65536
562 #define IPFW_OBJECTS_DEFAULT 1024
564 #define CHAIN_TO_SRV(ch) ((ch)->srvmap)
565 #define SRV_OBJECT(ch, idx) ((ch)->srvstate[(idx)])
568 uint32_t set; /* table set */
569 uint16_t uidx; /* table index */
570 uint8_t type; /* table type */
573 int tlen; /* Total TLV size block */
574 void *tlvs; /* Pointer to first TLV */
578 * Classifier callback. Checks if @cmd opcode contains kernel object reference.
579 * If true, returns its index and type.
580 * Returns 0 if match is found, 1 overwise.
582 typedef int (ipfw_obj_rw_cl)(ipfw_insn *cmd, uint16_t *puidx, uint8_t *ptype);
584 * Updater callback. Sets kernel object reference index to @puidx
586 typedef void (ipfw_obj_rw_upd)(ipfw_insn *cmd, uint16_t puidx);
588 * Finder callback. Tries to find named object by name (specified via @ti).
589 * Stores found named object pointer in @pno.
590 * If object was not found, NULL is stored.
592 * Return 0 if input data was valid.
594 typedef int (ipfw_obj_fname_cb)(struct ip_fw_chain *ch,
595 struct tid_info *ti, struct named_object **pno);
597 * Another finder callback. Tries to findex named object by kernel index.
599 * Returns pointer to named object or NULL.
601 typedef struct named_object *(ipfw_obj_fidx_cb)(struct ip_fw_chain *ch,
604 * Object creator callback. Tries to create object specified by @ti.
605 * Stores newly-allocated object index in @pkidx.
607 * Returns 0 on success.
609 typedef int (ipfw_obj_create_cb)(struct ip_fw_chain *ch, struct tid_info *ti,
612 * Object destroy callback. Intended to free resources allocated by
613 * create_object callback.
615 typedef void (ipfw_obj_destroy_cb)(struct ip_fw_chain *ch,
616 struct named_object *no);
618 * Sets handler callback. Handles moving and swaping set of named object.
619 * SWAP_ALL moves all named objects from set `set' to `new_set' and vise versa;
620 * TEST_ALL checks that there aren't any named object with conflicting names;
621 * MOVE_ALL moves all named objects from set `set' to `new_set';
622 * COUNT_ONE used to count number of references used by object with kidx `set';
623 * TEST_ONE checks that named object with kidx `set' can be moved to `new_set`;
624 * MOVE_ONE moves named object with kidx `set' to set `new_set'.
627 SWAP_ALL = 0, TEST_ALL, MOVE_ALL, COUNT_ONE, TEST_ONE, MOVE_ONE
629 typedef int (ipfw_obj_sets_cb)(struct ip_fw_chain *ch,
630 uint16_t set, uint8_t new_set, enum ipfw_sets_cmd cmd);
633 struct opcode_obj_rewrite {
634 uint32_t opcode; /* Opcode to act upon */
635 uint32_t etlv; /* Relevant export TLV id */
636 ipfw_obj_rw_cl *classifier; /* Check if rewrite is needed */
637 ipfw_obj_rw_upd *update; /* update cmd with new value */
638 ipfw_obj_fname_cb *find_byname; /* Find named object by name */
639 ipfw_obj_fidx_cb *find_bykidx; /* Find named object by kidx */
640 ipfw_obj_create_cb *create_object; /* Create named object */
641 ipfw_obj_destroy_cb *destroy_object;/* Destroy named object */
642 ipfw_obj_sets_cb *manage_sets; /* Swap or move sets */
645 #define IPFW_ADD_OBJ_REWRITER(f, c) do { \
647 ipfw_add_obj_rewriter(c, \
648 sizeof(c) / sizeof(c[0])); \
650 #define IPFW_DEL_OBJ_REWRITER(l, c) do { \
652 ipfw_del_obj_rewriter(c, \
653 sizeof(c) / sizeof(c[0])); \
656 /* In ip_fw_iface.c */
657 int ipfw_iface_init(void);
658 void ipfw_iface_destroy(void);
659 void vnet_ipfw_iface_destroy(struct ip_fw_chain *ch);
660 int ipfw_iface_ref(struct ip_fw_chain *ch, char *name,
661 struct ipfw_ifc *ic);
662 void ipfw_iface_unref(struct ip_fw_chain *ch, struct ipfw_ifc *ic);
663 void ipfw_iface_add_notify(struct ip_fw_chain *ch, struct ipfw_ifc *ic);
664 void ipfw_iface_del_notify(struct ip_fw_chain *ch, struct ipfw_ifc *ic);
666 /* In ip_fw_sockopt.c */
667 void ipfw_init_skipto_cache(struct ip_fw_chain *chain);
668 void ipfw_destroy_skipto_cache(struct ip_fw_chain *chain);
669 int ipfw_find_rule(struct ip_fw_chain *chain, uint32_t key, uint32_t id);
670 int ipfw_ctl3(struct sockopt *sopt);
671 int ipfw_add_protected_rule(struct ip_fw_chain *chain, struct ip_fw *rule,
673 void ipfw_reap_add(struct ip_fw_chain *chain, struct ip_fw **head,
675 void ipfw_reap_rules(struct ip_fw *head);
676 void ipfw_init_counters(void);
677 void ipfw_destroy_counters(void);
678 struct ip_fw *ipfw_alloc_rule(struct ip_fw_chain *chain, size_t rulesize);
679 void ipfw_free_rule(struct ip_fw *rule);
680 int ipfw_match_range(struct ip_fw *rule, ipfw_range_tlv *rt);
681 int ipfw_mark_object_kidx(uint32_t *bmask, uint16_t etlv, uint16_t kidx);
683 typedef int (sopt_handler_f)(struct ip_fw_chain *ch,
684 ip_fw3_opheader *op3, struct sockopt_data *sd);
685 struct ipfw_sopt_handler {
689 sopt_handler_f *handler;
692 #define HDIR_SET 0x01 /* Handler is used to set some data */
693 #define HDIR_GET 0x02 /* Handler is used to retrieve data */
694 #define HDIR_BOTH HDIR_GET|HDIR_SET
696 void ipfw_init_sopt_handler(void);
697 void ipfw_destroy_sopt_handler(void);
698 void ipfw_add_sopt_handler(struct ipfw_sopt_handler *sh, size_t count);
699 int ipfw_del_sopt_handler(struct ipfw_sopt_handler *sh, size_t count);
700 caddr_t ipfw_get_sopt_space(struct sockopt_data *sd, size_t needed);
701 caddr_t ipfw_get_sopt_header(struct sockopt_data *sd, size_t needed);
702 #define IPFW_ADD_SOPT_HANDLER(f, c) do { \
704 ipfw_add_sopt_handler(c, \
705 sizeof(c) / sizeof(c[0])); \
707 #define IPFW_DEL_SOPT_HANDLER(l, c) do { \
709 ipfw_del_sopt_handler(c, \
710 sizeof(c) / sizeof(c[0])); \
713 struct namedobj_instance;
714 typedef int (objhash_cb_t)(struct namedobj_instance *ni, struct named_object *,
716 typedef uint32_t (objhash_hash_f)(struct namedobj_instance *ni, const void *key,
718 typedef int (objhash_cmp_f)(struct named_object *no, const void *key,
720 struct namedobj_instance *ipfw_objhash_create(uint32_t items);
721 void ipfw_objhash_destroy(struct namedobj_instance *);
722 void ipfw_objhash_bitmap_alloc(uint32_t items, void **idx, int *pblocks);
723 void ipfw_objhash_bitmap_merge(struct namedobj_instance *ni,
724 void **idx, int *blocks);
725 void ipfw_objhash_bitmap_swap(struct namedobj_instance *ni,
726 void **idx, int *blocks);
727 void ipfw_objhash_bitmap_free(void *idx, int blocks);
728 void ipfw_objhash_set_hashf(struct namedobj_instance *ni, objhash_hash_f *f);
729 struct named_object *ipfw_objhash_lookup_name(struct namedobj_instance *ni,
730 uint32_t set, char *name);
731 struct named_object *ipfw_objhash_lookup_name_type(struct namedobj_instance *ni,
732 uint32_t set, uint32_t type, const char *name);
733 struct named_object *ipfw_objhash_lookup_kidx(struct namedobj_instance *ni,
735 int ipfw_objhash_same_name(struct namedobj_instance *ni, struct named_object *a,
736 struct named_object *b);
737 void ipfw_objhash_add(struct namedobj_instance *ni, struct named_object *no);
738 void ipfw_objhash_del(struct namedobj_instance *ni, struct named_object *no);
739 uint32_t ipfw_objhash_count(struct namedobj_instance *ni);
740 uint32_t ipfw_objhash_count_type(struct namedobj_instance *ni, uint16_t type);
741 int ipfw_objhash_foreach(struct namedobj_instance *ni, objhash_cb_t *f,
743 int ipfw_objhash_foreach_type(struct namedobj_instance *ni, objhash_cb_t *f,
744 void *arg, uint16_t type);
745 int ipfw_objhash_free_idx(struct namedobj_instance *ni, uint16_t idx);
746 int ipfw_objhash_alloc_idx(void *n, uint16_t *pidx);
747 void ipfw_objhash_set_funcs(struct namedobj_instance *ni,
748 objhash_hash_f *hash_f, objhash_cmp_f *cmp_f);
749 int ipfw_objhash_find_type(struct namedobj_instance *ni, struct tid_info *ti,
750 uint32_t etlv, struct named_object **pno);
751 void ipfw_export_obj_ntlv(struct named_object *no, ipfw_obj_ntlv *ntlv);
752 ipfw_obj_ntlv *ipfw_find_name_tlv_type(void *tlvs, int len, uint16_t uidx,
754 void ipfw_init_obj_rewriter(void);
755 void ipfw_destroy_obj_rewriter(void);
756 void ipfw_add_obj_rewriter(struct opcode_obj_rewrite *rw, size_t count);
757 int ipfw_del_obj_rewriter(struct opcode_obj_rewrite *rw, size_t count);
759 int create_objects_compat(struct ip_fw_chain *ch, ipfw_insn *cmd,
760 struct obj_idx *oib, struct obj_idx *pidx, struct tid_info *ti);
761 void update_opcode_kidx(ipfw_insn *cmd, uint16_t idx);
762 int classify_opcode_kidx(ipfw_insn *cmd, uint16_t *puidx);
763 void ipfw_init_srv(struct ip_fw_chain *ch);
764 void ipfw_destroy_srv(struct ip_fw_chain *ch);
765 int ipfw_check_object_name_generic(const char *name);
766 int ipfw_obj_manage_sets(struct namedobj_instance *ni, uint16_t type,
767 uint16_t set, uint8_t new_set, enum ipfw_sets_cmd cmd);
769 /* In ip_fw_eaction.c */
770 typedef int (ipfw_eaction_t)(struct ip_fw_chain *ch, struct ip_fw_args *args,
771 ipfw_insn *cmd, int *done);
772 int ipfw_eaction_init(struct ip_fw_chain *ch, int first);
773 void ipfw_eaction_uninit(struct ip_fw_chain *ch, int last);
775 uint16_t ipfw_add_eaction(struct ip_fw_chain *ch, ipfw_eaction_t handler,
777 int ipfw_del_eaction(struct ip_fw_chain *ch, uint16_t eaction_id);
778 int ipfw_run_eaction(struct ip_fw_chain *ch, struct ip_fw_args *args,
779 ipfw_insn *cmd, int *done);
780 int ipfw_reset_eaction(struct ip_fw_chain *ch, struct ip_fw *rule,
781 uint16_t eaction_id, uint16_t default_id, uint16_t instance_id);
782 int ipfw_reset_eaction_instance(struct ip_fw_chain *ch, uint16_t eaction_id,
783 uint16_t instance_id);
785 /* In ip_fw_table.c */
788 typedef int (table_lookup_t)(struct table_info *ti, void *key, uint32_t keylen,
791 int ipfw_lookup_table(struct ip_fw_chain *ch, uint16_t tbl, uint16_t plen,
792 void *paddr, uint32_t *val);
793 struct named_object *ipfw_objhash_lookup_table_kidx(struct ip_fw_chain *ch,
795 int ipfw_ref_table(struct ip_fw_chain *ch, ipfw_obj_ntlv *ntlv, uint16_t *kidx);
796 void ipfw_unref_table(struct ip_fw_chain *ch, uint16_t kidx);
797 int ipfw_init_tables(struct ip_fw_chain *ch, int first);
798 int ipfw_resize_tables(struct ip_fw_chain *ch, unsigned int ntables);
799 int ipfw_switch_tables_namespace(struct ip_fw_chain *ch, unsigned int nsets);
800 void ipfw_destroy_tables(struct ip_fw_chain *ch, int last);
802 /* In ip_fw_nat.c -- XXX to be moved to ip_var.h */
804 extern struct cfg_nat *(*lookup_nat_ptr)(struct nat_list *, int);
806 typedef int ipfw_nat_t(struct ip_fw_args *, struct cfg_nat *, struct mbuf *);
807 typedef int ipfw_nat_cfg_t(struct sockopt *);
809 VNET_DECLARE(int, ipfw_nat_ready);
810 #define V_ipfw_nat_ready VNET(ipfw_nat_ready)
811 #define IPFW_NAT_LOADED (V_ipfw_nat_ready)
813 extern ipfw_nat_t *ipfw_nat_ptr;
814 extern ipfw_nat_cfg_t *ipfw_nat_cfg_ptr;
815 extern ipfw_nat_cfg_t *ipfw_nat_del_ptr;
816 extern ipfw_nat_cfg_t *ipfw_nat_get_cfg_ptr;
817 extern ipfw_nat_cfg_t *ipfw_nat_get_log_ptr;
819 /* Helper functions for IP checksum adjustment */
820 static __inline uint16_t
821 cksum_add(uint16_t sum, uint16_t a)
826 return (res + (res < a));
829 static __inline uint16_t
830 cksum_adjust(uint16_t oldsum, uint16_t old, uint16_t new)
833 return (~cksum_add(cksum_add(~oldsum, ~old), new));
837 #endif /* _IPFW2_PRIVATE_H */