2 * Copyright (c) 2002-2009 Luigi Rizzo, Universita` di Pisa
3 * Copyright (c) 2014 Yandex LLC
4 * Copyright (c) 2014 Alexander V. Chernikov
6 * Supported by: Valeria Paoli
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 * Control socket and rule management routines for ipfw.
35 * Control is currently implemented via IP_FW3 setsockopt() code.
41 #error IPFIREWALL requires INET.
43 #include "opt_inet6.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/malloc.h>
48 #include <sys/mbuf.h> /* struct m_tag used by nested headers */
49 #include <sys/kernel.h>
53 #include <sys/rwlock.h>
54 #include <sys/rmlock.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
57 #include <sys/sysctl.h>
58 #include <sys/syslog.h>
59 #include <sys/fnv_hash.h>
62 #include <net/route.h>
65 #include <vm/vm_extern.h>
67 #include <netinet/in.h>
68 #include <netinet/ip_var.h> /* hooks */
69 #include <netinet/ip_fw.h>
71 #include <netpfil/ipfw/ip_fw_private.h>
72 #include <netpfil/ipfw/ip_fw_table.h>
75 #include <security/mac/mac_framework.h>
78 static int ipfw_ctl(struct sockopt *sopt);
79 static int check_ipfw_rule_body(ipfw_insn *cmd, int cmd_len,
80 struct rule_check_info *ci);
81 static int check_ipfw_rule1(struct ip_fw_rule *rule, int size,
82 struct rule_check_info *ci);
83 static int check_ipfw_rule0(struct ip_fw_rule0 *rule, int size,
84 struct rule_check_info *ci);
85 static int rewrite_rule_uidx(struct ip_fw_chain *chain,
86 struct rule_check_info *ci);
88 #define NAMEDOBJ_HASH_SIZE 32
90 struct namedobj_instance {
91 struct namedobjects_head *names;
92 struct namedobjects_head *values;
93 uint32_t nn_size; /* names hash size */
94 uint32_t nv_size; /* number hash size */
95 u_long *idx_mask; /* used items bitmask */
96 uint32_t max_blocks; /* number of "long" blocks in bitmask */
97 uint32_t count; /* number of items */
98 uint16_t free_off[IPFW_MAX_SETS]; /* first possible free offset */
99 objhash_hash_f *hash_f;
100 objhash_cmp_f *cmp_f;
102 #define BLOCK_ITEMS (8 * sizeof(u_long)) /* Number of items for ffsl() */
104 static uint32_t objhash_hash_name(struct namedobj_instance *ni,
105 const void *key, uint32_t kopt);
106 static uint32_t objhash_hash_idx(struct namedobj_instance *ni, uint32_t val);
107 static int objhash_cmp_name(struct named_object *no, const void *name,
110 MALLOC_DEFINE(M_IPFW, "IpFw/IpAcct", "IpFw/IpAcct chain's");
112 static int dump_config(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
113 struct sockopt_data *sd);
114 static int add_rules(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
115 struct sockopt_data *sd);
116 static int del_rules(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
117 struct sockopt_data *sd);
118 static int clear_rules(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
119 struct sockopt_data *sd);
120 static int move_rules(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
121 struct sockopt_data *sd);
122 static int manage_sets(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
123 struct sockopt_data *sd);
124 static int dump_soptcodes(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
125 struct sockopt_data *sd);
126 static int dump_srvobjects(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
127 struct sockopt_data *sd);
129 /* ctl3 handler data */
130 struct mtx ctl3_lock;
131 #define CTL3_LOCK_INIT() mtx_init(&ctl3_lock, "ctl3_lock", NULL, MTX_DEF)
132 #define CTL3_LOCK_DESTROY() mtx_destroy(&ctl3_lock)
133 #define CTL3_LOCK() mtx_lock(&ctl3_lock)
134 #define CTL3_UNLOCK() mtx_unlock(&ctl3_lock)
136 static struct ipfw_sopt_handler *ctl3_handlers;
137 static size_t ctl3_hsize;
138 static uint64_t ctl3_refct, ctl3_gencnt;
139 #define CTL3_SMALLBUF 4096 /* small page-size write buffer */
140 #define CTL3_LARGEBUF 16 * 1024 * 1024 /* handle large rulesets */
142 static int ipfw_flush_sopt_data(struct sockopt_data *sd);
144 static struct ipfw_sopt_handler scodes[] = {
145 { IP_FW_XGET, 0, HDIR_GET, dump_config },
146 { IP_FW_XADD, 0, HDIR_BOTH, add_rules },
147 { IP_FW_XDEL, 0, HDIR_BOTH, del_rules },
148 { IP_FW_XZERO, 0, HDIR_SET, clear_rules },
149 { IP_FW_XRESETLOG, 0, HDIR_SET, clear_rules },
150 { IP_FW_XMOVE, 0, HDIR_SET, move_rules },
151 { IP_FW_SET_SWAP, 0, HDIR_SET, manage_sets },
152 { IP_FW_SET_MOVE, 0, HDIR_SET, manage_sets },
153 { IP_FW_SET_ENABLE, 0, HDIR_SET, manage_sets },
154 { IP_FW_DUMP_SOPTCODES, 0, HDIR_GET, dump_soptcodes },
155 { IP_FW_DUMP_SRVOBJECTS,0, HDIR_GET, dump_srvobjects },
159 set_legacy_obj_kidx(struct ip_fw_chain *ch, struct ip_fw_rule0 *rule);
160 static struct opcode_obj_rewrite *find_op_rw(ipfw_insn *cmd,
161 uint16_t *puidx, uint8_t *ptype);
162 static int mark_object_kidx(struct ip_fw_chain *ch, struct ip_fw *rule,
164 static int ref_rule_objects(struct ip_fw_chain *ch, struct ip_fw *rule,
165 struct rule_check_info *ci, struct obj_idx *oib, struct tid_info *ti);
166 static int ref_opcode_object(struct ip_fw_chain *ch, ipfw_insn *cmd,
167 struct tid_info *ti, struct obj_idx *pidx, int *unresolved);
168 static void unref_rule_objects(struct ip_fw_chain *chain, struct ip_fw *rule);
169 static void unref_oib_objects(struct ip_fw_chain *ch, ipfw_insn *cmd,
170 struct obj_idx *oib, struct obj_idx *end);
171 static int export_objhash_ntlv(struct namedobj_instance *ni, uint16_t kidx,
172 struct sockopt_data *sd);
175 * Opcode object rewriter variables
177 struct opcode_obj_rewrite *ctl3_rewriters;
178 static size_t ctl3_rsize;
181 * static variables followed by global ones
184 static VNET_DEFINE(uma_zone_t, ipfw_cntr_zone);
185 #define V_ipfw_cntr_zone VNET(ipfw_cntr_zone)
191 V_ipfw_cntr_zone = uma_zcreate("IPFW counters",
192 IPFW_RULE_CNTR_SIZE, NULL, NULL, NULL, NULL,
193 UMA_ALIGN_PTR, UMA_ZONE_PCPU);
197 ipfw_destroy_counters()
200 uma_zdestroy(V_ipfw_cntr_zone);
204 ipfw_alloc_rule(struct ip_fw_chain *chain, size_t rulesize)
208 rule = malloc(rulesize, M_IPFW, M_WAITOK | M_ZERO);
209 rule->cntr = uma_zalloc(V_ipfw_cntr_zone, M_WAITOK | M_ZERO);
215 free_rule(struct ip_fw *rule)
218 uma_zfree(V_ipfw_cntr_zone, rule->cntr);
224 * Find the smallest rule >= key, id.
225 * We could use bsearch but it is so simple that we code it directly
228 ipfw_find_rule(struct ip_fw_chain *chain, uint32_t key, uint32_t id)
233 for (lo = 0, hi = chain->n_rules - 1; lo < hi;) {
236 if (r->rulenum < key)
237 lo = i + 1; /* continue from the next one */
238 else if (r->rulenum > key)
239 hi = i; /* this might be good */
241 lo = i + 1; /* continue from the next one */
242 else /* r->id >= id */
243 hi = i; /* this might be good */
249 * Builds skipto cache on rule set @map.
252 update_skipto_cache(struct ip_fw_chain *chain, struct ip_fw **map)
257 IPFW_UH_WLOCK_ASSERT(chain);
260 rulenum = map[mi]->rulenum;
261 smap = chain->idxmap_back;
266 for (i = 0; i < 65536; i++) {
268 /* Use the same rule index until i < rulenum */
269 if (i != rulenum || i == 65535)
271 /* Find next rule with num > i */
272 rulenum = map[++mi]->rulenum;
274 rulenum = map[++mi]->rulenum;
279 * Swaps prepared (backup) index with current one.
282 swap_skipto_cache(struct ip_fw_chain *chain)
286 IPFW_UH_WLOCK_ASSERT(chain);
287 IPFW_WLOCK_ASSERT(chain);
290 chain->idxmap = chain->idxmap_back;
291 chain->idxmap_back = map;
295 * Allocate and initialize skipto cache.
298 ipfw_init_skipto_cache(struct ip_fw_chain *chain)
300 int *idxmap, *idxmap_back;
302 idxmap = malloc(65536 * sizeof(uint32_t *), M_IPFW,
304 idxmap_back = malloc(65536 * sizeof(uint32_t *), M_IPFW,
308 * Note we may be called at any time after initialization,
309 * for example, on first skipto rule, so we need to
310 * provide valid chain->idxmap on return
313 IPFW_UH_WLOCK(chain);
314 if (chain->idxmap != NULL) {
315 IPFW_UH_WUNLOCK(chain);
316 free(idxmap, M_IPFW);
317 free(idxmap_back, M_IPFW);
321 /* Set backup pointer first to permit building cache */
322 chain->idxmap_back = idxmap_back;
323 update_skipto_cache(chain, chain->map);
325 /* It is now safe to set chain->idxmap ptr */
326 chain->idxmap = idxmap;
327 swap_skipto_cache(chain);
329 IPFW_UH_WUNLOCK(chain);
333 * Destroys skipto cache.
336 ipfw_destroy_skipto_cache(struct ip_fw_chain *chain)
339 if (chain->idxmap != NULL)
340 free(chain->idxmap, M_IPFW);
341 if (chain->idxmap != NULL)
342 free(chain->idxmap_back, M_IPFW);
347 * allocate a new map, returns the chain locked. extra is the number
348 * of entries to add or delete.
350 static struct ip_fw **
351 get_map(struct ip_fw_chain *chain, int extra, int locked)
358 mflags = M_ZERO | ((locked != 0) ? M_NOWAIT : M_WAITOK);
360 i = chain->n_rules + extra;
361 map = malloc(i * sizeof(struct ip_fw *), M_IPFW, mflags);
363 printf("%s: cannot allocate map\n", __FUNCTION__);
367 IPFW_UH_WLOCK(chain);
368 if (i >= chain->n_rules + extra) /* good */
370 /* otherwise we lost the race, free and retry */
372 IPFW_UH_WUNLOCK(chain);
378 * swap the maps. It is supposed to be called with IPFW_UH_WLOCK
380 static struct ip_fw **
381 swap_map(struct ip_fw_chain *chain, struct ip_fw **new_map, int new_len)
383 struct ip_fw **old_map;
387 chain->n_rules = new_len;
388 old_map = chain->map;
389 chain->map = new_map;
390 swap_skipto_cache(chain);
397 export_cntr1_base(struct ip_fw *krule, struct ip_fw_bcounter *cntr)
399 struct timeval boottime;
401 cntr->size = sizeof(*cntr);
403 if (krule->cntr != NULL) {
404 cntr->pcnt = counter_u64_fetch(krule->cntr);
405 cntr->bcnt = counter_u64_fetch(krule->cntr + 1);
406 cntr->timestamp = krule->timestamp;
408 if (cntr->timestamp > 0) {
409 getboottime(&boottime);
410 cntr->timestamp += boottime.tv_sec;
415 export_cntr0_base(struct ip_fw *krule, struct ip_fw_bcounter0 *cntr)
417 struct timeval boottime;
419 if (krule->cntr != NULL) {
420 cntr->pcnt = counter_u64_fetch(krule->cntr);
421 cntr->bcnt = counter_u64_fetch(krule->cntr + 1);
422 cntr->timestamp = krule->timestamp;
424 if (cntr->timestamp > 0) {
425 getboottime(&boottime);
426 cntr->timestamp += boottime.tv_sec;
431 * Copies rule @urule from v1 userland format (current).
433 * Assume @krule is zeroed.
436 import_rule1(struct rule_check_info *ci)
438 struct ip_fw_rule *urule;
441 urule = (struct ip_fw_rule *)ci->urule;
442 krule = (struct ip_fw *)ci->krule;
445 krule->act_ofs = urule->act_ofs;
446 krule->cmd_len = urule->cmd_len;
447 krule->rulenum = urule->rulenum;
448 krule->set = urule->set;
449 krule->flags = urule->flags;
451 /* Save rulenum offset */
452 ci->urule_numoff = offsetof(struct ip_fw_rule, rulenum);
455 memcpy(krule->cmd, urule->cmd, krule->cmd_len * sizeof(uint32_t));
459 * Export rule into v1 format (Current).
461 * [ ipfw_obj_tlv(IPFW_TLV_RULE_ENT)
463 * [ ip_fw_bcounter ip_fw_rule] (depends on rcntrs).
465 * Assume @data is zeroed.
468 export_rule1(struct ip_fw *krule, caddr_t data, int len, int rcntrs)
470 struct ip_fw_bcounter *cntr;
471 struct ip_fw_rule *urule;
474 /* Fill in TLV header */
475 tlv = (ipfw_obj_tlv *)data;
476 tlv->type = IPFW_TLV_RULE_ENT;
481 cntr = (struct ip_fw_bcounter *)(tlv + 1);
482 urule = (struct ip_fw_rule *)(cntr + 1);
483 export_cntr1_base(krule, cntr);
485 urule = (struct ip_fw_rule *)(tlv + 1);
488 urule->act_ofs = krule->act_ofs;
489 urule->cmd_len = krule->cmd_len;
490 urule->rulenum = krule->rulenum;
491 urule->set = krule->set;
492 urule->flags = krule->flags;
493 urule->id = krule->id;
496 memcpy(urule->cmd, krule->cmd, krule->cmd_len * sizeof(uint32_t));
501 * Copies rule @urule from FreeBSD8 userland format (v0)
503 * Assume @krule is zeroed.
506 import_rule0(struct rule_check_info *ci)
508 struct ip_fw_rule0 *urule;
512 ipfw_insn_limit *lcmd;
515 urule = (struct ip_fw_rule0 *)ci->urule;
516 krule = (struct ip_fw *)ci->krule;
519 krule->act_ofs = urule->act_ofs;
520 krule->cmd_len = urule->cmd_len;
521 krule->rulenum = urule->rulenum;
522 krule->set = urule->set;
523 if ((urule->_pad & 1) != 0)
524 krule->flags |= IPFW_RULE_NOOPT;
526 /* Save rulenum offset */
527 ci->urule_numoff = offsetof(struct ip_fw_rule0, rulenum);
530 memcpy(krule->cmd, urule->cmd, krule->cmd_len * sizeof(uint32_t));
534 * 1) convert tablearg value from 65535 to 0
535 * 2) Add high bit to O_SETFIB/O_SETDSCP values (to make room
537 * 3) convert table number in iface opcodes to u16
538 * 4) convert old `nat global` into new 65535
544 for ( ; l > 0 ; l -= cmdlen, cmd += cmdlen) {
547 switch (cmd->opcode) {
548 /* Opcodes supporting tablearg */
560 if (cmd->arg1 == IP_FW_TABLEARG)
561 cmd->arg1 = IP_FW_TARG;
562 else if (cmd->arg1 == 0)
563 cmd->arg1 = IP_FW_NAT44_GLOBAL;
567 if (cmd->arg1 == IP_FW_TABLEARG)
568 cmd->arg1 = IP_FW_TARG;
573 lcmd = (ipfw_insn_limit *)cmd;
574 if (lcmd->conn_limit == IP_FW_TABLEARG)
575 lcmd->conn_limit = IP_FW_TARG;
577 /* Interface tables */
581 /* Interface table, possibly */
582 cmdif = (ipfw_insn_if *)cmd;
583 if (cmdif->name[0] != '\1')
586 cmdif->p.kidx = (uint16_t)cmdif->p.glob;
593 * Copies rule @krule from kernel to FreeBSD8 userland format (v0)
596 export_rule0(struct ip_fw *krule, struct ip_fw_rule0 *urule, int len)
600 ipfw_insn_limit *lcmd;
604 memset(urule, 0, len);
605 urule->act_ofs = krule->act_ofs;
606 urule->cmd_len = krule->cmd_len;
607 urule->rulenum = krule->rulenum;
608 urule->set = krule->set;
609 if ((krule->flags & IPFW_RULE_NOOPT) != 0)
613 memcpy(urule->cmd, krule->cmd, krule->cmd_len * sizeof(uint32_t));
615 /* Export counters */
616 export_cntr0_base(krule, (struct ip_fw_bcounter0 *)&urule->pcnt);
620 * 1) convert tablearg value from 0 to 65535
621 * 2) Remove highest bit from O_SETFIB/O_SETDSCP values.
622 * 3) convert table number in iface opcodes to int
628 for ( ; l > 0 ; l -= cmdlen, cmd += cmdlen) {
631 switch (cmd->opcode) {
632 /* Opcodes supporting tablearg */
644 if (cmd->arg1 == IP_FW_TARG)
645 cmd->arg1 = IP_FW_TABLEARG;
646 else if (cmd->arg1 == IP_FW_NAT44_GLOBAL)
651 if (cmd->arg1 == IP_FW_TARG)
652 cmd->arg1 = IP_FW_TABLEARG;
654 cmd->arg1 &= ~0x8000;
657 lcmd = (ipfw_insn_limit *)cmd;
658 if (lcmd->conn_limit == IP_FW_TARG)
659 lcmd->conn_limit = IP_FW_TABLEARG;
661 /* Interface tables */
665 /* Interface table, possibly */
666 cmdif = (ipfw_insn_if *)cmd;
667 if (cmdif->name[0] != '\1')
670 cmdif->p.glob = cmdif->p.kidx;
677 * Add new rule(s) to the list possibly creating rule number for each.
678 * Update the rule_number in the input struct so the caller knows it as well.
679 * Must be called without IPFW_UH held
682 commit_rules(struct ip_fw_chain *chain, struct rule_check_info *rci, int count)
684 int error, i, insert_before, tcount;
685 uint16_t rulenum, *pnum;
686 struct rule_check_info *ci;
688 struct ip_fw **map; /* the new array of pointers */
690 /* Check if we need to do table/obj index remap */
692 for (ci = rci, i = 0; i < count; ci++, i++) {
693 if (ci->object_opcodes == 0)
697 * Rule has some object opcodes.
698 * We need to find (and create non-existing)
699 * kernel objects, and reference existing ones.
701 error = rewrite_rule_uidx(chain, ci);
705 * rewrite failed, state for current rule
706 * has been reverted. Check if we need to
712 * We have some more table rules
713 * we need to rollback.
716 IPFW_UH_WLOCK(chain);
719 if (ci->object_opcodes == 0)
721 unref_rule_objects(chain,ci->krule);
724 IPFW_UH_WUNLOCK(chain);
734 /* get_map returns with IPFW_UH_WLOCK if successful */
735 map = get_map(chain, count, 0 /* not locked */);
739 IPFW_UH_WLOCK(chain);
740 for (ci = rci, i = 0; i < count; ci++, i++) {
741 if (ci->object_opcodes == 0)
744 unref_rule_objects(chain, ci->krule);
746 IPFW_UH_WUNLOCK(chain);
752 if (V_autoinc_step < 1)
754 else if (V_autoinc_step > 1000)
755 V_autoinc_step = 1000;
757 /* FIXME: Handle count > 1 */
760 rulenum = krule->rulenum;
762 /* find the insertion point, we will insert before */
763 insert_before = rulenum ? rulenum + 1 : IPFW_DEFAULT_RULE;
764 i = ipfw_find_rule(chain, insert_before, 0);
765 /* duplicate first part */
767 bcopy(chain->map, map, i * sizeof(struct ip_fw *));
769 /* duplicate remaining part, we always have the default rule */
770 bcopy(chain->map + i, map + i + 1,
771 sizeof(struct ip_fw *) *(chain->n_rules - i));
773 /* Compute rule number and write it back */
774 rulenum = i > 0 ? map[i-1]->rulenum : 0;
775 if (rulenum < IPFW_DEFAULT_RULE - V_autoinc_step)
776 rulenum += V_autoinc_step;
777 krule->rulenum = rulenum;
778 /* Save number to userland rule */
779 pnum = (uint16_t *)((caddr_t)ci->urule + ci->urule_numoff);
783 krule->id = chain->id + 1;
784 update_skipto_cache(chain, map);
785 map = swap_map(chain, map, chain->n_rules + 1);
786 chain->static_len += RULEUSIZE0(krule);
787 IPFW_UH_WUNLOCK(chain);
794 * Adds @rule to the list of rules to reap
797 ipfw_reap_add(struct ip_fw_chain *chain, struct ip_fw **head,
801 IPFW_UH_WLOCK_ASSERT(chain);
803 /* Unlink rule from everywhere */
804 unref_rule_objects(chain, rule);
806 *((struct ip_fw **)rule) = *head;
811 * Reclaim storage associated with a list of rules. This is
812 * typically the list created using remove_rule.
813 * A NULL pointer on input is handled correctly.
816 ipfw_reap_rules(struct ip_fw *head)
820 while ((rule = head) != NULL) {
821 head = *((struct ip_fw **)head);
828 * (default || reserved || !match_set || !match_number)
830 * default ::= (rule->rulenum == IPFW_DEFAULT_RULE)
831 * // the default rule is always protected
833 * reserved ::= (cmd == 0 && n == 0 && rule->set == RESVD_SET)
834 * // RESVD_SET is protected only if cmd == 0 and n == 0 ("ipfw flush")
836 * match_set ::= (cmd == 0 || rule->set == set)
837 * // set number is ignored for cmd == 0
839 * match_number ::= (cmd == 1 || n == 0 || n == rule->rulenum)
840 * // number is ignored for cmd == 1 or n == 0
844 ipfw_match_range(struct ip_fw *rule, ipfw_range_tlv *rt)
847 /* Don't match default rule for modification queries */
848 if (rule->rulenum == IPFW_DEFAULT_RULE &&
849 (rt->flags & IPFW_RCFLAG_DEFAULT) == 0)
852 /* Don't match rules in reserved set for flush requests */
853 if ((rt->flags & IPFW_RCFLAG_ALL) != 0 && rule->set == RESVD_SET)
856 /* If we're filtering by set, don't match other sets */
857 if ((rt->flags & IPFW_RCFLAG_SET) != 0 && rule->set != rt->set)
860 if ((rt->flags & IPFW_RCFLAG_RANGE) != 0 &&
861 (rule->rulenum < rt->start_rule || rule->rulenum > rt->end_rule))
867 struct manage_sets_args {
873 swap_sets_cb(struct namedobj_instance *ni, struct named_object *no,
876 struct manage_sets_args *args;
878 args = (struct manage_sets_args *)arg;
879 if (no->set == (uint8_t)args->set)
880 no->set = args->new_set;
881 else if (no->set == args->new_set)
882 no->set = (uint8_t)args->set;
887 move_sets_cb(struct namedobj_instance *ni, struct named_object *no,
890 struct manage_sets_args *args;
892 args = (struct manage_sets_args *)arg;
893 if (no->set == (uint8_t)args->set)
894 no->set = args->new_set;
899 test_sets_cb(struct namedobj_instance *ni, struct named_object *no,
902 struct manage_sets_args *args;
904 args = (struct manage_sets_args *)arg;
905 if (no->set != (uint8_t)args->set)
907 if (ipfw_objhash_lookup_name_type(ni, args->new_set,
908 no->etlv, no->name) != NULL)
914 * Generic function to handler moving and swapping sets.
917 ipfw_obj_manage_sets(struct namedobj_instance *ni, uint16_t type,
918 uint16_t set, uint8_t new_set, enum ipfw_sets_cmd cmd)
920 struct manage_sets_args args;
921 struct named_object *no;
924 args.new_set = new_set;
927 return (ipfw_objhash_foreach_type(ni, swap_sets_cb,
930 return (ipfw_objhash_foreach_type(ni, test_sets_cb,
933 return (ipfw_objhash_foreach_type(ni, move_sets_cb,
937 * @set used to pass kidx.
938 * When @new_set is zero - reset object counter,
939 * otherwise increment it.
941 no = ipfw_objhash_lookup_kidx(ni, set);
948 /* @set used to pass kidx */
949 no = ipfw_objhash_lookup_kidx(ni, set);
951 * First check number of references:
952 * when it differs, this mean other rules are holding
953 * reference to given object, so it is not possible to
954 * change its set. Note that refcnt may account references
955 * to some going-to-be-added rules. Since we don't know
956 * their numbers (and even if they will be added) it is
957 * perfectly OK to return error here.
959 if (no->ocnt != no->refcnt)
961 if (ipfw_objhash_lookup_name_type(ni, new_set, type,
966 /* @set used to pass kidx */
967 no = ipfw_objhash_lookup_kidx(ni, set);
975 * Delete rules matching range @rt.
976 * Saves number of deleted rules in @ndel.
978 * Returns 0 on success.
981 delete_range(struct ip_fw_chain *chain, ipfw_range_tlv *rt, int *ndel)
983 struct ip_fw *reap, *rule, **map;
988 IPFW_UH_WLOCK(chain); /* arbitrate writers */
991 * Stage 1: Determine range to inspect.
992 * Range is half-inclusive, e.g [start, end).
995 end = chain->n_rules - 1;
997 if ((rt->flags & IPFW_RCFLAG_RANGE) != 0) {
998 start = ipfw_find_rule(chain, rt->start_rule, 0);
1000 end = ipfw_find_rule(chain, rt->end_rule, 0);
1001 if (rt->end_rule != IPFW_DEFAULT_RULE)
1002 while (chain->map[end]->rulenum == rt->end_rule)
1006 /* Allocate new map of the same size */
1007 map = get_map(chain, 0, 1 /* locked */);
1009 IPFW_UH_WUNLOCK(chain);
1016 /* 1. bcopy the initial part of the map */
1018 bcopy(chain->map, map, start * sizeof(struct ip_fw *));
1019 /* 2. copy active rules between start and end */
1020 for (i = start; i < end; i++) {
1021 rule = chain->map[i];
1022 if (ipfw_match_range(rule, rt) == 0) {
1028 if (ipfw_is_dyn_rule(rule) != 0)
1031 /* 3. copy the final part of the map */
1032 bcopy(chain->map + end, map + ofs,
1033 (chain->n_rules - end) * sizeof(struct ip_fw *));
1034 /* 4. recalculate skipto cache */
1035 update_skipto_cache(chain, map);
1036 /* 5. swap the maps (under UH_WLOCK + WHLOCK) */
1037 map = swap_map(chain, map, chain->n_rules - n);
1038 /* 6. Remove all dynamic states originated by deleted rules */
1040 ipfw_expire_dyn_rules(chain, rt);
1041 /* 7. now remove the rules deleted from the old map */
1042 for (i = start; i < end; i++) {
1044 if (ipfw_match_range(rule, rt) == 0)
1046 chain->static_len -= RULEUSIZE0(rule);
1047 ipfw_reap_add(chain, &reap, rule);
1049 IPFW_UH_WUNLOCK(chain);
1051 ipfw_reap_rules(reap);
1059 move_objects(struct ip_fw_chain *ch, ipfw_range_tlv *rt)
1061 struct opcode_obj_rewrite *rw;
1064 int cmdlen, i, l, c;
1067 IPFW_UH_WLOCK_ASSERT(ch);
1069 /* Stage 1: count number of references by given rules */
1070 for (c = 0, i = 0; i < ch->n_rules - 1; i++) {
1072 if (ipfw_match_range(rule, rt) == 0)
1074 if (rule->set == rt->new_set) /* nothing to do */
1076 /* Search opcodes with named objects */
1077 for (l = rule->cmd_len, cmdlen = 0, cmd = rule->cmd;
1078 l > 0; l -= cmdlen, cmd += cmdlen) {
1079 cmdlen = F_LEN(cmd);
1080 rw = find_op_rw(cmd, &kidx, NULL);
1081 if (rw == NULL || rw->manage_sets == NULL)
1084 * When manage_sets() returns non-zero value to
1085 * COUNT_ONE command, consider this as an object
1086 * doesn't support sets (e.g. disabled with sysctl).
1087 * So, skip checks for this object.
1089 if (rw->manage_sets(ch, kidx, 1, COUNT_ONE) != 0)
1094 if (c == 0) /* No objects found */
1096 /* Stage 2: verify "ownership" */
1097 for (c = 0, i = 0; (i < ch->n_rules - 1) && c == 0; i++) {
1099 if (ipfw_match_range(rule, rt) == 0)
1101 if (rule->set == rt->new_set) /* nothing to do */
1103 /* Search opcodes with named objects */
1104 for (l = rule->cmd_len, cmdlen = 0, cmd = rule->cmd;
1105 l > 0 && c == 0; l -= cmdlen, cmd += cmdlen) {
1106 cmdlen = F_LEN(cmd);
1107 rw = find_op_rw(cmd, &kidx, NULL);
1108 if (rw == NULL || rw->manage_sets == NULL)
1110 /* Test for ownership and conflicting names */
1111 c = rw->manage_sets(ch, kidx,
1112 (uint8_t)rt->new_set, TEST_ONE);
1115 /* Stage 3: change set and cleanup */
1116 for (i = 0; i < ch->n_rules - 1; i++) {
1118 if (ipfw_match_range(rule, rt) == 0)
1120 if (rule->set == rt->new_set) /* nothing to do */
1122 /* Search opcodes with named objects */
1123 for (l = rule->cmd_len, cmdlen = 0, cmd = rule->cmd;
1124 l > 0; l -= cmdlen, cmd += cmdlen) {
1125 cmdlen = F_LEN(cmd);
1126 rw = find_op_rw(cmd, &kidx, NULL);
1127 if (rw == NULL || rw->manage_sets == NULL)
1129 /* cleanup object counter */
1130 rw->manage_sets(ch, kidx,
1131 0 /* reset counter */, COUNT_ONE);
1135 rw->manage_sets(ch, kidx,
1136 (uint8_t)rt->new_set, MOVE_ONE);
1141 * Changes set of given rule rannge @rt
1144 * Returns 0 on success.
1147 move_range(struct ip_fw_chain *chain, ipfw_range_tlv *rt)
1152 IPFW_UH_WLOCK(chain);
1155 * Move rules with matching paramenerts to a new set.
1156 * This one is much more complex. We have to ensure
1157 * that all referenced tables (if any) are referenced
1158 * by given rule subset only. Otherwise, we can't move
1159 * them to new set and have to return error.
1161 if ((i = move_objects(chain, rt)) != 0) {
1162 IPFW_UH_WUNLOCK(chain);
1166 /* XXX: We have to do swap holding WLOCK */
1167 for (i = 0; i < chain->n_rules; i++) {
1168 rule = chain->map[i];
1169 if (ipfw_match_range(rule, rt) == 0)
1171 rule->set = rt->new_set;
1174 IPFW_UH_WUNLOCK(chain);
1180 * Clear counters for a specific rule.
1181 * Normally run under IPFW_UH_RLOCK, but these are idempotent ops
1182 * so we only care that rules do not disappear.
1185 clear_counters(struct ip_fw *rule, int log_only)
1187 ipfw_insn_log *l = (ipfw_insn_log *)ACTION_PTR(rule);
1190 IPFW_ZERO_RULE_COUNTER(rule);
1191 if (l->o.opcode == O_LOG)
1192 l->log_left = l->max_log;
1196 * Flushes rules counters and/or log values on matching range.
1198 * Returns number of items cleared.
1201 clear_range(struct ip_fw_chain *chain, ipfw_range_tlv *rt, int log_only)
1208 rt->flags |= IPFW_RCFLAG_DEFAULT;
1210 IPFW_UH_WLOCK(chain); /* arbitrate writers */
1211 for (i = 0; i < chain->n_rules; i++) {
1212 rule = chain->map[i];
1213 if (ipfw_match_range(rule, rt) == 0)
1215 clear_counters(rule, log_only);
1218 IPFW_UH_WUNLOCK(chain);
1224 check_range_tlv(ipfw_range_tlv *rt)
1227 if (rt->head.length != sizeof(*rt))
1229 if (rt->start_rule > rt->end_rule)
1231 if (rt->set >= IPFW_MAX_SETS || rt->new_set >= IPFW_MAX_SETS)
1234 if ((rt->flags & IPFW_RCFLAG_USER) != rt->flags)
1241 * Delete rules matching specified parameters
1242 * Data layout (v0)(current):
1243 * Request: [ ipfw_obj_header ipfw_range_tlv ]
1244 * Reply: [ ipfw_obj_header ipfw_range_tlv ]
1246 * Saves number of deleted rules in ipfw_range_tlv->new_set.
1248 * Returns 0 on success.
1251 del_rules(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
1252 struct sockopt_data *sd)
1254 ipfw_range_header *rh;
1257 if (sd->valsize != sizeof(*rh))
1260 rh = (ipfw_range_header *)ipfw_get_sopt_space(sd, sd->valsize);
1262 if (check_range_tlv(&rh->range) != 0)
1266 if ((error = delete_range(chain, &rh->range, &ndel)) != 0)
1269 /* Save number of rules deleted */
1270 rh->range.new_set = ndel;
1275 * Move rules/sets matching specified parameters
1276 * Data layout (v0)(current):
1277 * Request: [ ipfw_obj_header ipfw_range_tlv ]
1279 * Returns 0 on success.
1282 move_rules(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
1283 struct sockopt_data *sd)
1285 ipfw_range_header *rh;
1287 if (sd->valsize != sizeof(*rh))
1290 rh = (ipfw_range_header *)ipfw_get_sopt_space(sd, sd->valsize);
1292 if (check_range_tlv(&rh->range) != 0)
1295 return (move_range(chain, &rh->range));
1299 * Clear rule accounting data matching specified parameters
1300 * Data layout (v0)(current):
1301 * Request: [ ipfw_obj_header ipfw_range_tlv ]
1302 * Reply: [ ipfw_obj_header ipfw_range_tlv ]
1304 * Saves number of cleared rules in ipfw_range_tlv->new_set.
1306 * Returns 0 on success.
1309 clear_rules(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
1310 struct sockopt_data *sd)
1312 ipfw_range_header *rh;
1316 if (sd->valsize != sizeof(*rh))
1319 rh = (ipfw_range_header *)ipfw_get_sopt_space(sd, sd->valsize);
1321 if (check_range_tlv(&rh->range) != 0)
1324 log_only = (op3->opcode == IP_FW_XRESETLOG);
1326 num = clear_range(chain, &rh->range, log_only);
1328 if (rh->range.flags & IPFW_RCFLAG_ALL)
1329 msg = log_only ? "All logging counts reset" :
1330 "Accounting cleared";
1332 msg = log_only ? "logging count reset" : "cleared";
1335 int lev = LOG_SECURITY | LOG_NOTICE;
1336 log(lev, "ipfw: %s.\n", msg);
1339 /* Save number of rules cleared */
1340 rh->range.new_set = num;
1345 enable_sets(struct ip_fw_chain *chain, ipfw_range_tlv *rt)
1349 IPFW_UH_WLOCK_ASSERT(chain);
1351 /* Change enabled/disabled sets mask */
1352 v_set = (V_set_disable | rt->set) & ~rt->new_set;
1353 v_set &= ~(1 << RESVD_SET); /* set RESVD_SET always enabled */
1355 V_set_disable = v_set;
1356 IPFW_WUNLOCK(chain);
1360 swap_sets(struct ip_fw_chain *chain, ipfw_range_tlv *rt, int mv)
1362 struct opcode_obj_rewrite *rw;
1366 IPFW_UH_WLOCK_ASSERT(chain);
1368 if (rt->set == rt->new_set) /* nothing to do */
1373 * Berfore moving the rules we need to check that
1374 * there aren't any conflicting named objects.
1376 for (rw = ctl3_rewriters;
1377 rw < ctl3_rewriters + ctl3_rsize; rw++) {
1378 if (rw->manage_sets == NULL)
1380 i = rw->manage_sets(chain, (uint8_t)rt->set,
1381 (uint8_t)rt->new_set, TEST_ALL);
1386 /* Swap or move two sets */
1387 for (i = 0; i < chain->n_rules - 1; i++) {
1388 rule = chain->map[i];
1389 if (rule->set == (uint8_t)rt->set)
1390 rule->set = (uint8_t)rt->new_set;
1391 else if (rule->set == (uint8_t)rt->new_set && mv == 0)
1392 rule->set = (uint8_t)rt->set;
1394 for (rw = ctl3_rewriters; rw < ctl3_rewriters + ctl3_rsize; rw++) {
1395 if (rw->manage_sets == NULL)
1397 rw->manage_sets(chain, (uint8_t)rt->set,
1398 (uint8_t)rt->new_set, mv != 0 ? MOVE_ALL: SWAP_ALL);
1404 * Swaps or moves set
1405 * Data layout (v0)(current):
1406 * Request: [ ipfw_obj_header ipfw_range_tlv ]
1408 * Returns 0 on success.
1411 manage_sets(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
1412 struct sockopt_data *sd)
1414 ipfw_range_header *rh;
1417 if (sd->valsize != sizeof(*rh))
1420 rh = (ipfw_range_header *)ipfw_get_sopt_space(sd, sd->valsize);
1422 if (rh->range.head.length != sizeof(ipfw_range_tlv))
1424 /* enable_sets() expects bitmasks. */
1425 if (op3->opcode != IP_FW_SET_ENABLE &&
1426 (rh->range.set >= IPFW_MAX_SETS ||
1427 rh->range.new_set >= IPFW_MAX_SETS))
1431 IPFW_UH_WLOCK(chain);
1432 switch (op3->opcode) {
1433 case IP_FW_SET_SWAP:
1434 case IP_FW_SET_MOVE:
1435 ret = swap_sets(chain, &rh->range,
1436 op3->opcode == IP_FW_SET_MOVE);
1438 case IP_FW_SET_ENABLE:
1439 enable_sets(chain, &rh->range);
1442 IPFW_UH_WUNLOCK(chain);
1448 * Remove all rules with given number, or do set manipulation.
1449 * Assumes chain != NULL && *chain != NULL.
1451 * The argument is an uint32_t. The low 16 bit are the rule or set number;
1452 * the next 8 bits are the new set; the top 8 bits indicate the command:
1454 * 0 delete rules numbered "rulenum"
1455 * 1 delete rules in set "rulenum"
1456 * 2 move rules "rulenum" to set "new_set"
1457 * 3 move rules from set "rulenum" to set "new_set"
1458 * 4 swap sets "rulenum" and "new_set"
1459 * 5 delete rules "rulenum" and set "new_set"
1462 del_entry(struct ip_fw_chain *chain, uint32_t arg)
1464 uint32_t num; /* rule number or old_set */
1465 uint8_t cmd, new_set;
1471 cmd = (arg >> 24) & 0xff;
1472 new_set = (arg >> 16) & 0xff;
1474 if (cmd > 5 || new_set > RESVD_SET)
1476 if (cmd == 0 || cmd == 2 || cmd == 5) {
1477 if (num >= IPFW_DEFAULT_RULE)
1480 if (num > RESVD_SET) /* old_set */
1484 /* Convert old requests into new representation */
1485 memset(&rt, 0, sizeof(rt));
1486 rt.start_rule = num;
1489 rt.new_set = new_set;
1493 case 0: /* delete rules numbered "rulenum" */
1495 rt.flags |= IPFW_RCFLAG_ALL;
1497 rt.flags |= IPFW_RCFLAG_RANGE;
1500 case 1: /* delete rules in set "rulenum" */
1501 rt.flags |= IPFW_RCFLAG_SET;
1504 case 5: /* delete rules "rulenum" and set "new_set" */
1505 rt.flags |= IPFW_RCFLAG_RANGE | IPFW_RCFLAG_SET;
1510 case 2: /* move rules "rulenum" to set "new_set" */
1511 rt.flags |= IPFW_RCFLAG_RANGE;
1513 case 3: /* move rules from set "rulenum" to set "new_set" */
1514 IPFW_UH_WLOCK(chain);
1515 error = swap_sets(chain, &rt, 1);
1516 IPFW_UH_WUNLOCK(chain);
1518 case 4: /* swap sets "rulenum" and "new_set" */
1519 IPFW_UH_WLOCK(chain);
1520 error = swap_sets(chain, &rt, 0);
1521 IPFW_UH_WUNLOCK(chain);
1528 if ((error = delete_range(chain, &rt, &ndel)) != 0)
1531 if (ndel == 0 && (cmd != 1 && num != 0))
1537 return (move_range(chain, &rt));
1541 * Reset some or all counters on firewall rules.
1542 * The argument `arg' is an u_int32_t. The low 16 bit are the rule number,
1543 * the next 8 bits are the set number, the top 8 bits are the command:
1544 * 0 work with rules from all set's;
1545 * 1 work with rules only from specified set.
1546 * Specified rule number is zero if we want to clear all entries.
1547 * log_only is 1 if we only want to reset logs, zero otherwise.
1550 zero_entry(struct ip_fw_chain *chain, u_int32_t arg, int log_only)
1556 uint16_t rulenum = arg & 0xffff;
1557 uint8_t set = (arg >> 16) & 0xff;
1558 uint8_t cmd = (arg >> 24) & 0xff;
1562 if (cmd == 1 && set > RESVD_SET)
1565 IPFW_UH_RLOCK(chain);
1567 V_norule_counter = 0;
1568 for (i = 0; i < chain->n_rules; i++) {
1569 rule = chain->map[i];
1570 /* Skip rules not in our set. */
1571 if (cmd == 1 && rule->set != set)
1573 clear_counters(rule, log_only);
1575 msg = log_only ? "All logging counts reset" :
1576 "Accounting cleared";
1579 for (i = 0; i < chain->n_rules; i++) {
1580 rule = chain->map[i];
1581 if (rule->rulenum == rulenum) {
1582 if (cmd == 0 || rule->set == set)
1583 clear_counters(rule, log_only);
1586 if (rule->rulenum > rulenum)
1589 if (!cleared) { /* we did not find any matching rules */
1590 IPFW_UH_RUNLOCK(chain);
1593 msg = log_only ? "logging count reset" : "cleared";
1595 IPFW_UH_RUNLOCK(chain);
1598 int lev = LOG_SECURITY | LOG_NOTICE;
1601 log(lev, "ipfw: Entry %d %s.\n", rulenum, msg);
1603 log(lev, "ipfw: %s.\n", msg);
1610 * Check rule head in FreeBSD11 format
1614 check_ipfw_rule1(struct ip_fw_rule *rule, int size,
1615 struct rule_check_info *ci)
1619 if (size < sizeof(*rule)) {
1620 printf("ipfw: rule too short\n");
1624 /* Check for valid cmd_len */
1625 l = roundup2(RULESIZE(rule), sizeof(uint64_t));
1627 printf("ipfw: size mismatch (have %d want %d)\n", size, l);
1630 if (rule->act_ofs >= rule->cmd_len) {
1631 printf("ipfw: bogus action offset (%u > %u)\n",
1632 rule->act_ofs, rule->cmd_len - 1);
1636 if (rule->rulenum > IPFW_DEFAULT_RULE - 1)
1639 return (check_ipfw_rule_body(rule->cmd, rule->cmd_len, ci));
1643 * Check rule head in FreeBSD8 format
1647 check_ipfw_rule0(struct ip_fw_rule0 *rule, int size,
1648 struct rule_check_info *ci)
1652 if (size < sizeof(*rule)) {
1653 printf("ipfw: rule too short\n");
1657 /* Check for valid cmd_len */
1658 l = sizeof(*rule) + rule->cmd_len * 4 - 4;
1660 printf("ipfw: size mismatch (have %d want %d)\n", size, l);
1663 if (rule->act_ofs >= rule->cmd_len) {
1664 printf("ipfw: bogus action offset (%u > %u)\n",
1665 rule->act_ofs, rule->cmd_len - 1);
1669 if (rule->rulenum > IPFW_DEFAULT_RULE - 1)
1672 return (check_ipfw_rule_body(rule->cmd, rule->cmd_len, ci));
1676 check_ipfw_rule_body(ipfw_insn *cmd, int cmd_len, struct rule_check_info *ci)
1684 * Now go for the individual checks. Very simple ones, basically only
1685 * instruction sizes.
1687 for (l = cmd_len; l > 0 ; l -= cmdlen, cmd += cmdlen) {
1688 cmdlen = F_LEN(cmd);
1690 printf("ipfw: opcode %d size truncated\n",
1694 switch (cmd->opcode) {
1697 if (cmdlen != F_INSN_SIZE(ipfw_insn))
1699 ci->object_opcodes++;
1710 case O_IPPRECEDENCE:
1728 if (cmdlen != F_INSN_SIZE(ipfw_insn))
1732 case O_EXTERNAL_ACTION:
1733 if (cmd->arg1 == 0 ||
1734 cmdlen != F_INSN_SIZE(ipfw_insn)) {
1735 printf("ipfw: invalid external "
1739 ci->object_opcodes++;
1741 * Do we have O_EXTERNAL_INSTANCE or O_EXTERNAL_DATA
1747 cmdlen = F_LEN(cmd);
1748 if (cmd->opcode == O_EXTERNAL_DATA)
1750 if (cmd->opcode != O_EXTERNAL_INSTANCE) {
1751 printf("ipfw: invalid opcode "
1752 "next to external action %u\n",
1756 if (cmd->arg1 == 0 ||
1757 cmdlen != F_INSN_SIZE(ipfw_insn)) {
1758 printf("ipfw: invalid external "
1759 "action instance opcode\n");
1762 ci->object_opcodes++;
1767 if (cmdlen != F_INSN_SIZE(ipfw_insn))
1769 if (cmd->arg1 >= rt_numfibs) {
1770 printf("ipfw: invalid fib number %d\n",
1777 if (cmdlen != F_INSN_SIZE(ipfw_insn))
1779 if ((cmd->arg1 != IP_FW_TARG) &&
1780 ((cmd->arg1 & 0x7FFF) >= rt_numfibs)) {
1781 printf("ipfw: invalid fib number %d\n",
1782 cmd->arg1 & 0x7FFF);
1796 if (cmdlen != F_INSN_SIZE(ipfw_insn_u32))
1801 if (cmdlen != F_INSN_SIZE(ipfw_insn_limit))
1803 ci->object_opcodes++;
1807 if (cmdlen != F_INSN_SIZE(ipfw_insn_log))
1810 ((ipfw_insn_log *)cmd)->log_left =
1811 ((ipfw_insn_log *)cmd)->max_log;
1817 /* only odd command lengths */
1818 if ((cmdlen & 1) == 0)
1824 if (cmd->arg1 == 0 || cmd->arg1 > 256) {
1825 printf("ipfw: invalid set size %d\n",
1829 if (cmdlen != F_INSN_SIZE(ipfw_insn_u32) +
1834 case O_IP_SRC_LOOKUP:
1835 if (cmdlen > F_INSN_SIZE(ipfw_insn_u32))
1837 case O_IP_DST_LOOKUP:
1838 if (cmd->arg1 >= V_fw_tables_max) {
1839 printf("ipfw: invalid table number %d\n",
1843 if (cmdlen != F_INSN_SIZE(ipfw_insn) &&
1844 cmdlen != F_INSN_SIZE(ipfw_insn_u32) + 1 &&
1845 cmdlen != F_INSN_SIZE(ipfw_insn_u32))
1847 ci->object_opcodes++;
1849 case O_IP_FLOW_LOOKUP:
1850 if (cmd->arg1 >= V_fw_tables_max) {
1851 printf("ipfw: invalid table number %d\n",
1855 if (cmdlen != F_INSN_SIZE(ipfw_insn) &&
1856 cmdlen != F_INSN_SIZE(ipfw_insn_u32))
1858 ci->object_opcodes++;
1861 if (cmdlen != F_INSN_SIZE(ipfw_insn_mac))
1872 if (cmdlen < 1 || cmdlen > 31)
1877 if (cmdlen != F_INSN_SIZE(ipfw_insn_u32) + 1)
1883 case O_IP_DSTPORT: /* XXX artificial limit, 30 port pairs */
1884 if (cmdlen < 2 || cmdlen > 31)
1891 if (cmdlen != F_INSN_SIZE(ipfw_insn_if))
1893 ci->object_opcodes++;
1897 if (cmdlen != F_INSN_SIZE(ipfw_insn_altq))
1903 if (cmdlen != F_INSN_SIZE(ipfw_insn))
1908 if (cmdlen != F_INSN_SIZE(ipfw_insn_sa))
1913 if (cmdlen != F_INSN_SIZE(ipfw_insn_sa6))
1920 if (ip_divert_ptr == NULL)
1926 if (ng_ipfw_input_p == NULL)
1931 if (!IPFW_NAT_LOADED)
1933 if (cmdlen != F_INSN_SIZE(ipfw_insn_nat))
1937 ci->object_opcodes++;
1939 case O_FORWARD_MAC: /* XXX not implemented yet */
1952 if (cmdlen != F_INSN_SIZE(ipfw_insn))
1956 printf("ipfw: opcode %d, multiple actions"
1963 printf("ipfw: opcode %d, action must be"
1972 if (cmdlen != F_INSN_SIZE(struct in6_addr) +
1973 F_INSN_SIZE(ipfw_insn))
1978 if (cmdlen != F_INSN_SIZE(ipfw_insn_u32) +
1979 ((ipfw_insn_u32 *)cmd)->o.arg1)
1983 case O_IP6_SRC_MASK:
1984 case O_IP6_DST_MASK:
1985 if ( !(cmdlen & 1) || cmdlen > 127)
1989 if( cmdlen != F_INSN_SIZE( ipfw_insn_icmp6 ) )
1995 switch (cmd->opcode) {
2005 case O_IP6_SRC_MASK:
2006 case O_IP6_DST_MASK:
2008 printf("ipfw: no IPv6 support in kernel\n");
2009 return (EPROTONOSUPPORT);
2012 printf("ipfw: opcode %d, unknown opcode\n",
2018 if (have_action == 0) {
2019 printf("ipfw: missing action\n");
2025 printf("ipfw: opcode %d size %d wrong\n",
2026 cmd->opcode, cmdlen);
2032 * Translation of requests for compatibility with FreeBSD 7.2/8.
2033 * a static variable tells us if we have an old client from userland,
2034 * and if necessary we translate requests and responses between the
2040 struct ip_fw7 *next; /* linked list of rules */
2041 struct ip_fw7 *next_rule; /* ptr to next [skipto] rule */
2042 /* 'next_rule' is used to pass up 'set_disable' status */
2044 uint16_t act_ofs; /* offset of action in 32-bit units */
2045 uint16_t cmd_len; /* # of 32-bit words in cmd */
2046 uint16_t rulenum; /* rule number */
2047 uint8_t set; /* rule set (0..31) */
2048 // #define RESVD_SET 31 /* set for default and persistent rules */
2049 uint8_t _pad; /* padding */
2050 // uint32_t id; /* rule id, only in v.8 */
2051 /* These fields are present in all rules. */
2052 uint64_t pcnt; /* Packet counter */
2053 uint64_t bcnt; /* Byte counter */
2054 uint32_t timestamp; /* tv_sec of last match */
2056 ipfw_insn cmd[1]; /* storage for commands */
2059 static int convert_rule_to_7(struct ip_fw_rule0 *rule);
2060 static int convert_rule_to_8(struct ip_fw_rule0 *rule);
2063 #define RULESIZE7(rule) (sizeof(struct ip_fw7) + \
2064 ((struct ip_fw7 *)(rule))->cmd_len * 4 - 4)
2069 * Copy the static and dynamic rules to the supplied buffer
2070 * and return the amount of space actually used.
2071 * Must be run under IPFW_UH_RLOCK
2074 ipfw_getrules(struct ip_fw_chain *chain, void *buf, size_t space)
2077 char *ep = bp + space;
2079 struct ip_fw_rule0 *dst;
2080 struct timeval boottime;
2081 int error, i, l, warnflag;
2082 time_t boot_seconds;
2086 getboottime(&boottime);
2087 boot_seconds = boottime.tv_sec;
2088 for (i = 0; i < chain->n_rules; i++) {
2089 rule = chain->map[i];
2092 /* Convert rule to FreeBSd 7.2 format */
2093 l = RULESIZE7(rule);
2094 if (bp + l + sizeof(uint32_t) <= ep) {
2095 bcopy(rule, bp, l + sizeof(uint32_t));
2096 error = set_legacy_obj_kidx(chain,
2097 (struct ip_fw_rule0 *)bp);
2100 error = convert_rule_to_7((struct ip_fw_rule0 *) bp);
2102 return 0; /*XXX correct? */
2104 * XXX HACK. Store the disable mask in the "next"
2105 * pointer in a wild attempt to keep the ABI the same.
2106 * Why do we do this on EVERY rule?
2108 bcopy(&V_set_disable,
2109 &(((struct ip_fw7 *)bp)->next_rule),
2110 sizeof(V_set_disable));
2111 if (((struct ip_fw7 *)bp)->timestamp)
2112 ((struct ip_fw7 *)bp)->timestamp += boot_seconds;
2115 continue; /* go to next rule */
2118 l = RULEUSIZE0(rule);
2119 if (bp + l > ep) { /* should not happen */
2120 printf("overflow dumping static rules\n");
2123 dst = (struct ip_fw_rule0 *)bp;
2124 export_rule0(rule, dst, l);
2125 error = set_legacy_obj_kidx(chain, dst);
2128 * XXX HACK. Store the disable mask in the "next"
2129 * pointer in a wild attempt to keep the ABI the same.
2130 * Why do we do this on EVERY rule?
2132 * XXX: "ipfw set show" (ab)uses IP_FW_GET to read disabled mask
2133 * so we need to fail _after_ saving at least one mask.
2135 bcopy(&V_set_disable, &dst->next_rule, sizeof(V_set_disable));
2137 dst->timestamp += boot_seconds;
2142 /* Non-fatal table rewrite error. */
2146 printf("Stop on rule %d. Fail to convert table\n",
2152 printf("ipfw: process %s is using legacy interfaces,"
2153 " consider rebuilding\n", "");
2154 ipfw_get_dynamic(chain, &bp, ep); /* protected by the dynamic lock */
2155 return (bp - (char *)buf);
2160 uint32_t b; /* start rule */
2161 uint32_t e; /* end rule */
2162 uint32_t rcount; /* number of rules */
2163 uint32_t rsize; /* rules size */
2164 uint32_t tcount; /* number of tables */
2165 int rcounters; /* counters */
2169 ipfw_export_obj_ntlv(struct named_object *no, ipfw_obj_ntlv *ntlv)
2172 ntlv->head.type = no->etlv;
2173 ntlv->head.length = sizeof(*ntlv);
2174 ntlv->idx = no->kidx;
2175 strlcpy(ntlv->name, no->name, sizeof(ntlv->name));
2179 * Export named object info in instance @ni, identified by @kidx
2180 * to ipfw_obj_ntlv. TLV is allocated from @sd space.
2182 * Returns 0 on success.
2185 export_objhash_ntlv(struct namedobj_instance *ni, uint16_t kidx,
2186 struct sockopt_data *sd)
2188 struct named_object *no;
2189 ipfw_obj_ntlv *ntlv;
2191 no = ipfw_objhash_lookup_kidx(ni, kidx);
2192 KASSERT(no != NULL, ("invalid object kernel index passed"));
2194 ntlv = (ipfw_obj_ntlv *)ipfw_get_sopt_space(sd, sizeof(*ntlv));
2198 ipfw_export_obj_ntlv(no, ntlv);
2203 * Dumps static rules with table TLVs in buffer @sd.
2205 * Returns 0 on success.
2208 dump_static_rules(struct ip_fw_chain *chain, struct dump_args *da,
2209 uint32_t *bmask, struct sockopt_data *sd)
2214 ipfw_obj_ctlv *ctlv;
2215 struct ip_fw *krule;
2216 struct namedobj_instance *ni;
2219 /* Dump table names first (if any) */
2220 if (da->tcount > 0) {
2222 ctlv = (ipfw_obj_ctlv *)ipfw_get_sopt_space(sd, sizeof(*ctlv));
2225 ctlv->head.type = IPFW_TLV_TBLNAME_LIST;
2226 ctlv->head.length = da->tcount * sizeof(ipfw_obj_ntlv) +
2228 ctlv->count = da->tcount;
2229 ctlv->objsize = sizeof(ipfw_obj_ntlv);
2233 tcount = da->tcount;
2234 ni = ipfw_get_table_objhash(chain);
2235 while (tcount > 0) {
2236 if ((bmask[i / 32] & (1 << (i % 32))) == 0) {
2241 /* Jump to shared named object bitmask */
2242 if (i >= IPFW_TABLES_MAX) {
2243 ni = CHAIN_TO_SRV(chain);
2244 i -= IPFW_TABLES_MAX;
2245 bmask += IPFW_TABLES_MAX / 32;
2248 if ((error = export_objhash_ntlv(ni, i, sd)) != 0)
2256 ctlv = (ipfw_obj_ctlv *)ipfw_get_sopt_space(sd, sizeof(*ctlv));
2259 ctlv->head.type = IPFW_TLV_RULE_LIST;
2260 ctlv->head.length = da->rsize + sizeof(*ctlv);
2261 ctlv->count = da->rcount;
2263 for (i = da->b; i < da->e; i++) {
2264 krule = chain->map[i];
2266 l = RULEUSIZE1(krule) + sizeof(ipfw_obj_tlv);
2267 if (da->rcounters != 0)
2268 l += sizeof(struct ip_fw_bcounter);
2269 dst = (caddr_t)ipfw_get_sopt_space(sd, l);
2273 export_rule1(krule, dst, l, da->rcounters);
2280 * Marks every object index used in @rule with bit in @bmask.
2281 * Used to generate bitmask of referenced tables/objects for given ruleset
2284 * Returns number of newly-referenced objects.
2287 mark_object_kidx(struct ip_fw_chain *ch, struct ip_fw *rule,
2290 struct opcode_obj_rewrite *rw;
2292 int bidx, cmdlen, l, count;
2300 for ( ; l > 0 ; l -= cmdlen, cmd += cmdlen) {
2301 cmdlen = F_LEN(cmd);
2303 rw = find_op_rw(cmd, &kidx, &subtype);
2309 * Maintain separate bitmasks for table and
2310 * non-table objects.
2312 if (rw->etlv != IPFW_TLV_TBL_NAME)
2313 bidx += IPFW_TABLES_MAX / 32;
2315 if ((bmask[bidx] & (1 << (kidx % 32))) == 0)
2318 bmask[bidx] |= 1 << (kidx % 32);
2325 * Dumps requested objects data
2326 * Data layout (version 0)(current):
2327 * Request: [ ipfw_cfg_lheader ] + IPFW_CFG_GET_* flags
2328 * size = ipfw_cfg_lheader.size
2329 * Reply: [ ipfw_cfg_lheader
2330 * [ ipfw_obj_ctlv(IPFW_TLV_TBL_LIST) ipfw_obj_ntlv x N ] (optional)
2331 * [ ipfw_obj_ctlv(IPFW_TLV_RULE_LIST)
2332 * ipfw_obj_tlv(IPFW_TLV_RULE_ENT) [ ip_fw_bcounter (optional) ip_fw_rule ]
2334 * [ ipfw_obj_ctlv(IPFW_TLV_STATE_LIST) ipfw_obj_dyntlv x N ] (optional)
2336 * * NOTE IPFW_TLV_STATE_LIST has the single valid field: objsize.
2337 * The rest (size, count) are set to zero and needs to be ignored.
2339 * Returns 0 on success.
2342 dump_config(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
2343 struct sockopt_data *sd)
2345 ipfw_cfg_lheader *hdr;
2350 struct dump_args da;
2353 hdr = (ipfw_cfg_lheader *)ipfw_get_sopt_header(sd, sizeof(*hdr));
2359 /* Allocate needed state. Note we allocate 2xspace mask, for table&srv */
2360 if (hdr->flags & IPFW_CFG_GET_STATIC)
2361 bmask = malloc(IPFW_TABLES_MAX / 4, M_TEMP, M_WAITOK | M_ZERO);
2363 IPFW_UH_RLOCK(chain);
2366 * STAGE 1: Determine size/count for objects in range.
2367 * Prepare used tables bitmask.
2369 sz = sizeof(ipfw_cfg_lheader);
2370 memset(&da, 0, sizeof(da));
2373 da.e = chain->n_rules;
2375 if (hdr->end_rule != 0) {
2376 /* Handle custom range */
2377 if ((rnum = hdr->start_rule) > IPFW_DEFAULT_RULE)
2378 rnum = IPFW_DEFAULT_RULE;
2379 da.b = ipfw_find_rule(chain, rnum, 0);
2380 rnum = hdr->end_rule;
2381 rnum = (rnum < IPFW_DEFAULT_RULE) ? rnum+1 : IPFW_DEFAULT_RULE;
2382 da.e = ipfw_find_rule(chain, rnum, 0) + 1;
2385 if (hdr->flags & IPFW_CFG_GET_STATIC) {
2386 for (i = da.b; i < da.e; i++) {
2387 rule = chain->map[i];
2388 da.rsize += RULEUSIZE1(rule) + sizeof(ipfw_obj_tlv);
2390 /* Update bitmask of used objects for given range */
2391 da.tcount += mark_object_kidx(chain, rule, bmask);
2393 /* Add counters if requested */
2394 if (hdr->flags & IPFW_CFG_GET_COUNTERS) {
2395 da.rsize += sizeof(struct ip_fw_bcounter) * da.rcount;
2400 sz += da.tcount * sizeof(ipfw_obj_ntlv) +
2401 sizeof(ipfw_obj_ctlv);
2402 sz += da.rsize + sizeof(ipfw_obj_ctlv);
2405 if (hdr->flags & IPFW_CFG_GET_STATES)
2406 sz += ipfw_dyn_get_count() * sizeof(ipfw_obj_dyntlv) +
2407 sizeof(ipfw_obj_ctlv);
2411 * Fill header anyway.
2412 * Note we have to save header fields to stable storage
2413 * buffer inside @sd can be flushed after dumping rules
2416 hdr->set_mask = ~V_set_disable;
2417 hdr_flags = hdr->flags;
2420 if (sd->valsize < sz) {
2425 /* STAGE2: Store actual data */
2426 if (hdr_flags & IPFW_CFG_GET_STATIC) {
2427 error = dump_static_rules(chain, &da, bmask, sd);
2432 if (hdr_flags & IPFW_CFG_GET_STATES)
2433 error = ipfw_dump_states(chain, sd);
2436 IPFW_UH_RUNLOCK(chain);
2439 free(bmask, M_TEMP);
2445 ipfw_check_object_name_generic(const char *name)
2449 nsize = sizeof(((ipfw_obj_ntlv *)0)->name);
2450 if (strnlen(name, nsize) == nsize)
2452 if (name[0] == '\0')
2458 * Creates non-existent objects referenced by rule.
2460 * Return 0 on success.
2463 create_objects_compat(struct ip_fw_chain *ch, ipfw_insn *cmd,
2464 struct obj_idx *oib, struct obj_idx *pidx, struct tid_info *ti)
2466 struct opcode_obj_rewrite *rw;
2472 * Compatibility stuff: do actual creation for non-existing,
2473 * but referenced objects.
2475 for (p = oib; p < pidx; p++) {
2483 rw = find_op_rw(cmd + p->off, NULL, NULL);
2484 KASSERT(rw != NULL, ("Unable to find handler for op %d",
2485 (cmd + p->off)->opcode));
2487 if (rw->create_object == NULL)
2490 error = rw->create_object(ch, ti, &kidx);
2497 * Error happened. We have to rollback everything.
2498 * Drop all already acquired references.
2501 unref_oib_objects(ch, cmd, oib, pidx);
2502 IPFW_UH_WUNLOCK(ch);
2511 * Compatibility function for old ipfw(8) binaries.
2512 * Rewrites table/nat kernel indices with userland ones.
2513 * Convert tables matching '/^\d+$/' to their atoi() value.
2514 * Use number 65535 for other tables.
2516 * Returns 0 on success.
2519 set_legacy_obj_kidx(struct ip_fw_chain *ch, struct ip_fw_rule0 *rule)
2521 struct opcode_obj_rewrite *rw;
2522 struct named_object *no;
2526 int cmdlen, error, l;
2527 uint16_t kidx, uidx;
2535 for ( ; l > 0 ; l -= cmdlen, cmd += cmdlen) {
2536 cmdlen = F_LEN(cmd);
2538 /* Check if is index in given opcode */
2539 rw = find_op_rw(cmd, &kidx, &subtype);
2543 /* Try to find referenced kernel object */
2544 no = rw->find_bykidx(ch, kidx);
2548 val = strtol(no->name, &end, 10);
2549 if (*end == '\0' && val < 65535) {
2554 * We are called via legacy opcode.
2555 * Save error and show table as fake number
2556 * not to make ipfw(8) hang.
2562 rw->update(cmd, uidx);
2570 * Unreferences all already-referenced objects in given @cmd rule,
2571 * using information in @oib.
2573 * Used to rollback partially converted rule on error.
2576 unref_oib_objects(struct ip_fw_chain *ch, ipfw_insn *cmd, struct obj_idx *oib,
2577 struct obj_idx *end)
2579 struct opcode_obj_rewrite *rw;
2580 struct named_object *no;
2583 IPFW_UH_WLOCK_ASSERT(ch);
2585 for (p = oib; p < end; p++) {
2589 rw = find_op_rw(cmd + p->off, NULL, NULL);
2590 KASSERT(rw != NULL, ("Unable to find handler for op %d",
2591 (cmd + p->off)->opcode));
2593 /* Find & unref by existing idx */
2594 no = rw->find_bykidx(ch, p->kidx);
2595 KASSERT(no != NULL, ("Ref'd object %d disappeared", p->kidx));
2601 * Remove references from every object used in @rule.
2602 * Used at rule removal code.
2605 unref_rule_objects(struct ip_fw_chain *ch, struct ip_fw *rule)
2607 struct opcode_obj_rewrite *rw;
2608 struct named_object *no;
2614 IPFW_UH_WLOCK_ASSERT(ch);
2619 for ( ; l > 0 ; l -= cmdlen, cmd += cmdlen) {
2620 cmdlen = F_LEN(cmd);
2622 rw = find_op_rw(cmd, &kidx, &subtype);
2625 no = rw->find_bykidx(ch, kidx);
2627 KASSERT(no != NULL, ("object id %d not found", kidx));
2628 KASSERT(no->subtype == subtype,
2629 ("wrong type %d (%d) for object id %d",
2630 no->subtype, subtype, kidx));
2631 KASSERT(no->refcnt > 0, ("refcount for object %d is %d",
2634 if (no->refcnt == 1 && rw->destroy_object != NULL)
2635 rw->destroy_object(ch, no);
2643 * Find and reference object (if any) stored in instruction @cmd.
2645 * Saves object info in @pidx, sets
2646 * - @unresolved to 1 if object should exists but not found
2648 * Returns non-zero value in case of error.
2651 ref_opcode_object(struct ip_fw_chain *ch, ipfw_insn *cmd, struct tid_info *ti,
2652 struct obj_idx *pidx, int *unresolved)
2654 struct named_object *no;
2655 struct opcode_obj_rewrite *rw;
2658 /* Check if this opcode is candidate for rewrite */
2659 rw = find_op_rw(cmd, &ti->uidx, &ti->type);
2663 /* Need to rewrite. Save necessary fields */
2664 pidx->uidx = ti->uidx;
2665 pidx->type = ti->type;
2667 /* Try to find referenced kernel object */
2668 error = rw->find_byname(ch, ti, &no);
2673 * Report about unresolved object for automaic
2681 * Object is already exist.
2682 * Its subtype should match with expected value.
2684 if (ti->type != no->subtype)
2687 /* Bump refcount and update kidx. */
2689 rw->update(cmd, no->kidx);
2694 * Finds and bumps refcount for objects referenced by given @rule.
2695 * Auto-creates non-existing tables.
2696 * Fills in @oib array with userland/kernel indexes.
2698 * Returns 0 on success.
2701 ref_rule_objects(struct ip_fw_chain *ch, struct ip_fw *rule,
2702 struct rule_check_info *ci, struct obj_idx *oib, struct tid_info *ti)
2704 struct obj_idx *pidx;
2706 int cmdlen, error, l, unresolved;
2716 /* Increase refcount on each existing referenced table. */
2717 for ( ; l > 0 ; l -= cmdlen, cmd += cmdlen) {
2718 cmdlen = F_LEN(cmd);
2721 error = ref_opcode_object(ch, cmd, ti, pidx, &unresolved);
2725 * Compatibility stuff for old clients:
2726 * prepare to automaitcally create non-existing objects.
2728 if (unresolved != 0) {
2729 pidx->off = rule->cmd_len - l;
2735 /* Unref everything we have already done */
2736 unref_oib_objects(ch, rule->cmd, oib, pidx);
2737 IPFW_UH_WUNLOCK(ch);
2740 IPFW_UH_WUNLOCK(ch);
2742 /* Perform auto-creation for non-existing objects */
2744 error = create_objects_compat(ch, rule->cmd, oib, pidx, ti);
2746 /* Calculate real number of dynamic objects */
2747 ci->object_opcodes = (uint16_t)(pidx - oib);
2753 * Checks is opcode is referencing table of appropriate type.
2754 * Adds reference count for found table if true.
2755 * Rewrites user-supplied opcode values with kernel ones.
2757 * Returns 0 on success and appropriate error code otherwise.
2760 rewrite_rule_uidx(struct ip_fw_chain *chain, struct rule_check_info *ci)
2765 struct obj_idx *p, *pidx_first, *pidx_last;
2769 * Prepare an array for storing opcode indices.
2770 * Use stack allocation by default.
2772 if (ci->object_opcodes <= (sizeof(ci->obuf)/sizeof(ci->obuf[0]))) {
2774 pidx_first = ci->obuf;
2776 pidx_first = malloc(
2777 ci->object_opcodes * sizeof(struct obj_idx),
2778 M_IPFW, M_WAITOK | M_ZERO);
2782 memset(&ti, 0, sizeof(ti));
2784 /* Use set rule is assigned to. */
2785 ti.set = ci->krule->set;
2786 if (ci->ctlv != NULL) {
2787 ti.tlvs = (void *)(ci->ctlv + 1);
2788 ti.tlen = ci->ctlv->head.length - sizeof(ipfw_obj_ctlv);
2791 /* Reference all used tables and other objects */
2792 error = ref_rule_objects(chain, ci->krule, ci, pidx_first, &ti);
2796 * Note that ref_rule_objects() might have updated ci->object_opcodes
2797 * to reflect actual number of object opcodes.
2800 /* Perform rewrite of remaining opcodes */
2802 pidx_last = pidx_first + ci->object_opcodes;
2803 for (p = pidx_first; p < pidx_last; p++) {
2804 cmd = ci->krule->cmd + p->off;
2805 update_opcode_kidx(cmd, p->kidx);
2809 if (pidx_first != ci->obuf)
2810 free(pidx_first, M_IPFW);
2816 * Adds one or more rules to ipfw @chain.
2817 * Data layout (version 0)(current):
2821 * [ ipfw_obj_ctlv(IPFW_TLV_TBL_LIST) ipfw_obj_ntlv x N ] (optional *1)
2822 * [ ipfw_obj_ctlv(IPFW_TLV_RULE_LIST) ip_fw x N ] (*2) (*3)
2827 * [ ipfw_obj_ctlv(IPFW_TLV_TBL_LIST) ipfw_obj_ntlv x N ] (optional)
2828 * [ ipfw_obj_ctlv(IPFW_TLV_RULE_LIST) ip_fw x N ]
2831 * Rules in reply are modified to store their actual ruleset number.
2833 * (*1) TLVs inside IPFW_TLV_TBL_LIST needs to be sorted ascending
2834 * according to their idx field and there has to be no duplicates.
2835 * (*2) Numbered rules inside IPFW_TLV_RULE_LIST needs to be sorted ascending.
2836 * (*3) Each ip_fw structure needs to be aligned to u64 boundary.
2838 * Returns 0 on success.
2841 add_rules(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
2842 struct sockopt_data *sd)
2844 ipfw_obj_ctlv *ctlv, *rtlv, *tstate;
2845 ipfw_obj_ntlv *ntlv;
2846 int clen, error, idx;
2847 uint32_t count, read;
2848 struct ip_fw_rule *r;
2849 struct rule_check_info rci, *ci, *cbuf;
2852 op3 = (ip_fw3_opheader *)ipfw_get_sopt_space(sd, sd->valsize);
2853 ctlv = (ipfw_obj_ctlv *)(op3 + 1);
2855 read = sizeof(ip_fw3_opheader);
2859 memset(&rci, 0, sizeof(struct rule_check_info));
2861 if (read + sizeof(*ctlv) > sd->valsize)
2864 if (ctlv->head.type == IPFW_TLV_TBLNAME_LIST) {
2865 clen = ctlv->head.length;
2866 /* Check size and alignment */
2867 if (clen > sd->valsize || clen < sizeof(*ctlv))
2869 if ((clen % sizeof(uint64_t)) != 0)
2873 * Some table names or other named objects.
2874 * Check for validness.
2876 count = (ctlv->head.length - sizeof(*ctlv)) / sizeof(*ntlv);
2877 if (ctlv->count != count || ctlv->objsize != sizeof(*ntlv))
2882 * Ensure TLVs are sorted ascending and
2883 * there are no duplicates.
2886 ntlv = (ipfw_obj_ntlv *)(ctlv + 1);
2888 if (ntlv->head.length != sizeof(ipfw_obj_ntlv))
2891 error = ipfw_check_object_name_generic(ntlv->name);
2895 if (ntlv->idx <= idx)
2904 read += ctlv->head.length;
2905 ctlv = (ipfw_obj_ctlv *)((caddr_t)ctlv + ctlv->head.length);
2908 if (read + sizeof(*ctlv) > sd->valsize)
2911 if (ctlv->head.type == IPFW_TLV_RULE_LIST) {
2912 clen = ctlv->head.length;
2913 if (clen + read > sd->valsize || clen < sizeof(*ctlv))
2915 if ((clen % sizeof(uint64_t)) != 0)
2919 * TODO: Permit adding multiple rules at once
2921 if (ctlv->count != 1)
2924 clen -= sizeof(*ctlv);
2926 if (ctlv->count > clen / sizeof(struct ip_fw_rule))
2929 /* Allocate state for each rule or use stack */
2930 if (ctlv->count == 1) {
2931 memset(&rci, 0, sizeof(struct rule_check_info));
2934 cbuf = malloc(ctlv->count * sizeof(*ci), M_TEMP,
2939 * Check each rule for validness.
2940 * Ensure numbered rules are sorted ascending
2941 * and properly aligned
2944 r = (struct ip_fw_rule *)(ctlv + 1);
2948 rsize = roundup2(RULESIZE(r), sizeof(uint64_t));
2949 if (rsize > clen || ctlv->count <= count) {
2955 error = check_ipfw_rule1(r, rsize, ci);
2960 if (r->rulenum != 0 && r->rulenum < idx) {
2961 printf("rulenum %d idx %d\n", r->rulenum, idx);
2967 ci->urule = (caddr_t)r;
2969 rsize = roundup2(rsize, sizeof(uint64_t));
2971 r = (struct ip_fw_rule *)((caddr_t)r + rsize);
2976 if (ctlv->count != count || error != 0) {
2983 read += ctlv->head.length;
2984 ctlv = (ipfw_obj_ctlv *)((caddr_t)ctlv + ctlv->head.length);
2987 if (read != sd->valsize || rtlv == NULL || rtlv->count == 0) {
2988 if (cbuf != NULL && cbuf != &rci)
2994 * Passed rules seems to be valid.
2995 * Allocate storage and try to add them to chain.
2997 for (i = 0, ci = cbuf; i < rtlv->count; i++, ci++) {
2998 clen = RULEKSIZE1((struct ip_fw_rule *)ci->urule);
2999 ci->krule = ipfw_alloc_rule(chain, clen);
3003 if ((error = commit_rules(chain, cbuf, rtlv->count)) != 0) {
3004 /* Free allocate krules */
3005 for (i = 0, ci = cbuf; i < rtlv->count; i++, ci++)
3006 free_rule(ci->krule);
3009 if (cbuf != NULL && cbuf != &rci)
3016 * Lists all sopts currently registered.
3017 * Data layout (v0)(current):
3018 * Request: [ ipfw_obj_lheader ], size = ipfw_obj_lheader.size
3019 * Reply: [ ipfw_obj_lheader ipfw_sopt_info x N ]
3021 * Returns 0 on success
3024 dump_soptcodes(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
3025 struct sockopt_data *sd)
3027 struct _ipfw_obj_lheader *olh;
3029 struct ipfw_sopt_handler *sh;
3030 uint32_t count, n, size;
3032 olh = (struct _ipfw_obj_lheader *)ipfw_get_sopt_header(sd,sizeof(*olh));
3035 if (sd->valsize < olh->size)
3040 size = count * sizeof(ipfw_sopt_info) + sizeof(ipfw_obj_lheader);
3042 /* Fill in header regadless of buffer size */
3044 olh->objsize = sizeof(ipfw_sopt_info);
3046 if (size > olh->size) {
3053 for (n = 1; n <= count; n++) {
3054 i = (ipfw_sopt_info *)ipfw_get_sopt_space(sd, sizeof(*i));
3055 KASSERT(i != NULL, ("previously checked buffer is not enough"));
3056 sh = &ctl3_handlers[n];
3057 i->opcode = sh->opcode;
3058 i->version = sh->version;
3059 i->refcnt = sh->refcnt;
3067 * Compares two opcodes.
3068 * Used both in qsort() and bsearch().
3070 * Returns 0 if match is found.
3073 compare_opcodes(const void *_a, const void *_b)
3075 const struct opcode_obj_rewrite *a, *b;
3077 a = (const struct opcode_obj_rewrite *)_a;
3078 b = (const struct opcode_obj_rewrite *)_b;
3080 if (a->opcode < b->opcode)
3082 else if (a->opcode > b->opcode)
3089 * XXX: Rewrite bsearch()
3092 find_op_rw_range(uint16_t op, struct opcode_obj_rewrite **plo,
3093 struct opcode_obj_rewrite **phi)
3095 struct opcode_obj_rewrite *ctl3_max, *lo, *hi, h, *rw;
3097 memset(&h, 0, sizeof(h));
3100 rw = (struct opcode_obj_rewrite *)bsearch(&h, ctl3_rewriters,
3101 ctl3_rsize, sizeof(h), compare_opcodes);
3105 /* Find the first element matching the same opcode */
3107 for ( ; lo > ctl3_rewriters && (lo - 1)->opcode == op; lo--)
3110 /* Find the last element matching the same opcode */
3112 ctl3_max = ctl3_rewriters + ctl3_rsize;
3113 for ( ; (hi + 1) < ctl3_max && (hi + 1)->opcode == op; hi++)
3123 * Finds opcode object rewriter based on @code.
3125 * Returns pointer to handler or NULL.
3127 static struct opcode_obj_rewrite *
3128 find_op_rw(ipfw_insn *cmd, uint16_t *puidx, uint8_t *ptype)
3130 struct opcode_obj_rewrite *rw, *lo, *hi;
3134 if (find_op_rw_range(cmd->opcode, &lo, &hi) != 0)
3137 for (rw = lo; rw <= hi; rw++) {
3138 if (rw->classifier(cmd, &uidx, &subtype) == 0) {
3150 classify_opcode_kidx(ipfw_insn *cmd, uint16_t *puidx)
3153 if (find_op_rw(cmd, puidx, NULL) == NULL)
3159 update_opcode_kidx(ipfw_insn *cmd, uint16_t idx)
3161 struct opcode_obj_rewrite *rw;
3163 rw = find_op_rw(cmd, NULL, NULL);
3164 KASSERT(rw != NULL, ("No handler to update opcode %d", cmd->opcode));
3165 rw->update(cmd, idx);
3169 ipfw_init_obj_rewriter()
3172 ctl3_rewriters = NULL;
3177 ipfw_destroy_obj_rewriter()
3180 if (ctl3_rewriters != NULL)
3181 free(ctl3_rewriters, M_IPFW);
3182 ctl3_rewriters = NULL;
3187 * Adds one or more opcode object rewrite handlers to the global array.
3188 * Function may sleep.
3191 ipfw_add_obj_rewriter(struct opcode_obj_rewrite *rw, size_t count)
3194 struct opcode_obj_rewrite *tmp;
3199 sz = ctl3_rsize + count;
3201 tmp = malloc(sizeof(*rw) * sz, M_IPFW, M_WAITOK | M_ZERO);
3203 if (ctl3_rsize + count <= sz)
3210 /* Merge old & new arrays */
3211 sz = ctl3_rsize + count;
3212 memcpy(tmp, ctl3_rewriters, ctl3_rsize * sizeof(*rw));
3213 memcpy(&tmp[ctl3_rsize], rw, count * sizeof(*rw));
3214 qsort(tmp, sz, sizeof(*rw), compare_opcodes);
3215 /* Switch new and free old */
3216 if (ctl3_rewriters != NULL)
3217 free(ctl3_rewriters, M_IPFW);
3218 ctl3_rewriters = tmp;
3225 * Removes one or more object rewrite handlers from the global array.
3228 ipfw_del_obj_rewriter(struct opcode_obj_rewrite *rw, size_t count)
3231 struct opcode_obj_rewrite *ctl3_max, *ktmp, *lo, *hi;
3236 for (i = 0; i < count; i++) {
3237 if (find_op_rw_range(rw[i].opcode, &lo, &hi) != 0)
3240 for (ktmp = lo; ktmp <= hi; ktmp++) {
3241 if (ktmp->classifier != rw[i].classifier)
3244 ctl3_max = ctl3_rewriters + ctl3_rsize;
3245 sz = (ctl3_max - (ktmp + 1)) * sizeof(*ktmp);
3246 memmove(ktmp, ktmp + 1, sz);
3253 if (ctl3_rsize == 0) {
3254 if (ctl3_rewriters != NULL)
3255 free(ctl3_rewriters, M_IPFW);
3256 ctl3_rewriters = NULL;
3265 export_objhash_ntlv_internal(struct namedobj_instance *ni,
3266 struct named_object *no, void *arg)
3268 struct sockopt_data *sd;
3269 ipfw_obj_ntlv *ntlv;
3271 sd = (struct sockopt_data *)arg;
3272 ntlv = (ipfw_obj_ntlv *)ipfw_get_sopt_space(sd, sizeof(*ntlv));
3275 ipfw_export_obj_ntlv(no, ntlv);
3280 * Lists all service objects.
3281 * Data layout (v0)(current):
3282 * Request: [ ipfw_obj_lheader ] size = ipfw_obj_lheader.size
3283 * Reply: [ ipfw_obj_lheader [ ipfw_obj_ntlv x N ] (optional) ]
3284 * Returns 0 on success
3287 dump_srvobjects(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
3288 struct sockopt_data *sd)
3290 ipfw_obj_lheader *hdr;
3293 hdr = (ipfw_obj_lheader *)ipfw_get_sopt_header(sd, sizeof(*hdr));
3297 IPFW_UH_RLOCK(chain);
3298 count = ipfw_objhash_count(CHAIN_TO_SRV(chain));
3299 hdr->size = sizeof(ipfw_obj_lheader) + count * sizeof(ipfw_obj_ntlv);
3300 if (sd->valsize < hdr->size) {
3301 IPFW_UH_RUNLOCK(chain);
3305 hdr->objsize = sizeof(ipfw_obj_ntlv);
3307 ipfw_objhash_foreach(CHAIN_TO_SRV(chain),
3308 export_objhash_ntlv_internal, sd);
3309 IPFW_UH_RUNLOCK(chain);
3314 * Compares two sopt handlers (code, version and handler ptr).
3315 * Used both as qsort() and bsearch().
3316 * Does not compare handler for latter case.
3318 * Returns 0 if match is found.
3321 compare_sh(const void *_a, const void *_b)
3323 const struct ipfw_sopt_handler *a, *b;
3325 a = (const struct ipfw_sopt_handler *)_a;
3326 b = (const struct ipfw_sopt_handler *)_b;
3328 if (a->opcode < b->opcode)
3330 else if (a->opcode > b->opcode)
3333 if (a->version < b->version)
3335 else if (a->version > b->version)
3338 /* bsearch helper */
3339 if (a->handler == NULL)
3342 if ((uintptr_t)a->handler < (uintptr_t)b->handler)
3344 else if ((uintptr_t)a->handler > (uintptr_t)b->handler)
3351 * Finds sopt handler based on @code and @version.
3353 * Returns pointer to handler or NULL.
3355 static struct ipfw_sopt_handler *
3356 find_sh(uint16_t code, uint8_t version, sopt_handler_f *handler)
3358 struct ipfw_sopt_handler *sh, h;
3360 memset(&h, 0, sizeof(h));
3362 h.version = version;
3363 h.handler = handler;
3365 sh = (struct ipfw_sopt_handler *)bsearch(&h, ctl3_handlers,
3366 ctl3_hsize, sizeof(h), compare_sh);
3372 find_ref_sh(uint16_t opcode, uint8_t version, struct ipfw_sopt_handler *psh)
3374 struct ipfw_sopt_handler *sh;
3377 if ((sh = find_sh(opcode, version, NULL)) == NULL) {
3379 printf("ipfw: ipfw_ctl3 invalid option %d""v""%d\n",
3385 /* Copy handler data to requested buffer */
3393 find_unref_sh(struct ipfw_sopt_handler *psh)
3395 struct ipfw_sopt_handler *sh;
3398 sh = find_sh(psh->opcode, psh->version, NULL);
3399 KASSERT(sh != NULL, ("ctl3 handler disappeared"));
3406 ipfw_init_sopt_handler()
3410 IPFW_ADD_SOPT_HANDLER(1, scodes);
3414 ipfw_destroy_sopt_handler()
3417 IPFW_DEL_SOPT_HANDLER(1, scodes);
3418 CTL3_LOCK_DESTROY();
3422 * Adds one or more sockopt handlers to the global array.
3423 * Function may sleep.
3426 ipfw_add_sopt_handler(struct ipfw_sopt_handler *sh, size_t count)
3429 struct ipfw_sopt_handler *tmp;
3434 sz = ctl3_hsize + count;
3436 tmp = malloc(sizeof(*sh) * sz, M_IPFW, M_WAITOK | M_ZERO);
3438 if (ctl3_hsize + count <= sz)
3445 /* Merge old & new arrays */
3446 sz = ctl3_hsize + count;
3447 memcpy(tmp, ctl3_handlers, ctl3_hsize * sizeof(*sh));
3448 memcpy(&tmp[ctl3_hsize], sh, count * sizeof(*sh));
3449 qsort(tmp, sz, sizeof(*sh), compare_sh);
3450 /* Switch new and free old */
3451 if (ctl3_handlers != NULL)
3452 free(ctl3_handlers, M_IPFW);
3453 ctl3_handlers = tmp;
3461 * Removes one or more sockopt handlers from the global array.
3464 ipfw_del_sopt_handler(struct ipfw_sopt_handler *sh, size_t count)
3467 struct ipfw_sopt_handler *tmp, *h;
3472 for (i = 0; i < count; i++) {
3474 h = find_sh(tmp->opcode, tmp->version, tmp->handler);
3478 sz = (ctl3_handlers + ctl3_hsize - (h + 1)) * sizeof(*h);
3479 memmove(h, h + 1, sz);
3483 if (ctl3_hsize == 0) {
3484 if (ctl3_handlers != NULL)
3485 free(ctl3_handlers, M_IPFW);
3486 ctl3_handlers = NULL;
3497 * Writes data accumulated in @sd to sockopt buffer.
3498 * Zeroes internal @sd buffer.
3501 ipfw_flush_sopt_data(struct sockopt_data *sd)
3503 struct sockopt *sopt;
3513 if (sopt->sopt_dir == SOPT_GET) {
3514 error = copyout(sd->kbuf, sopt->sopt_val, sz);
3519 memset(sd->kbuf, 0, sd->ksize);
3522 if (sd->ktotal + sd->ksize < sd->valsize)
3523 sd->kavail = sd->ksize;
3525 sd->kavail = sd->valsize - sd->ktotal;
3527 /* Update sopt buffer data */
3528 sopt->sopt_valsize = sd->ktotal;
3529 sopt->sopt_val = sd->sopt_val + sd->ktotal;
3535 * Ensures that @sd buffer has contiguous @neeeded number of
3538 * Returns pointer to requested space or NULL.
3541 ipfw_get_sopt_space(struct sockopt_data *sd, size_t needed)
3546 if (sd->kavail < needed) {
3548 * Flush data and try another time.
3550 error = ipfw_flush_sopt_data(sd);
3552 if (sd->kavail < needed || error != 0)
3556 addr = sd->kbuf + sd->koff;
3558 sd->kavail -= needed;
3563 * Requests @needed contiguous bytes from @sd buffer.
3564 * Function is used to notify subsystem that we are
3565 * interesed in first @needed bytes (request header)
3566 * and the rest buffer can be safely zeroed.
3568 * Returns pointer to requested space or NULL.
3571 ipfw_get_sopt_header(struct sockopt_data *sd, size_t needed)
3575 if ((addr = ipfw_get_sopt_space(sd, needed)) == NULL)
3579 memset(sd->kbuf + sd->koff, 0, sd->kavail);
3585 * New sockopt handler.
3588 ipfw_ctl3(struct sockopt *sopt)
3591 size_t size, valsize;
3592 struct ip_fw_chain *chain;
3594 struct sockopt_data sdata;
3595 struct ipfw_sopt_handler h;
3596 ip_fw3_opheader *op3 = NULL;
3598 error = priv_check(sopt->sopt_td, PRIV_NETINET_IPFW);
3602 if (sopt->sopt_name != IP_FW3)
3603 return (ipfw_ctl(sopt));
3605 chain = &V_layer3_chain;
3608 /* Save original valsize before it is altered via sooptcopyin() */
3609 valsize = sopt->sopt_valsize;
3610 memset(&sdata, 0, sizeof(sdata));
3611 /* Read op3 header first to determine actual operation */
3612 op3 = (ip_fw3_opheader *)xbuf;
3613 error = sooptcopyin(sopt, op3, sizeof(*op3), sizeof(*op3));
3616 sopt->sopt_valsize = valsize;
3619 * Find and reference command.
3621 error = find_ref_sh(op3->opcode, op3->version, &h);
3626 * Disallow modifications in really-really secure mode, but still allow
3627 * the logging counters to be reset.
3629 if ((h.dir & HDIR_SET) != 0 && h.opcode != IP_FW_XRESETLOG) {
3630 error = securelevel_ge(sopt->sopt_td->td_ucred, 3);
3638 * Fill in sockopt_data structure that may be useful for
3639 * IP_FW3 get requests.
3642 if (valsize <= sizeof(xbuf)) {
3643 /* use on-stack buffer */
3645 sdata.ksize = sizeof(xbuf);
3646 sdata.kavail = valsize;
3650 * Determine opcode type/buffer size:
3651 * allocate sliding-window buf for data export or
3652 * contiguous buffer for special ops.
3654 if ((h.dir & HDIR_SET) != 0) {
3655 /* Set request. Allocate contigous buffer. */
3656 if (valsize > CTL3_LARGEBUF) {
3663 /* Get request. Allocate sliding window buffer */
3664 size = (valsize<CTL3_SMALLBUF) ? valsize:CTL3_SMALLBUF;
3666 if (size < valsize) {
3667 /* We have to wire user buffer */
3668 error = vslock(sopt->sopt_val, valsize);
3675 sdata.kbuf = malloc(size, M_TEMP, M_WAITOK | M_ZERO);
3677 sdata.kavail = size;
3681 sdata.sopt_val = sopt->sopt_val;
3682 sdata.valsize = valsize;
3685 * Copy either all request (if valsize < bsize_max)
3686 * or first bsize_max bytes to guarantee most consumers
3687 * that all necessary data has been copied).
3688 * Anyway, copy not less than sizeof(ip_fw3_opheader).
3690 if ((error = sooptcopyin(sopt, sdata.kbuf, sdata.ksize,
3691 sizeof(ip_fw3_opheader))) != 0)
3693 op3 = (ip_fw3_opheader *)sdata.kbuf;
3695 /* Finally, run handler */
3696 error = h.handler(chain, op3, &sdata);
3699 /* Flush state and free buffers */
3701 error = ipfw_flush_sopt_data(&sdata);
3703 ipfw_flush_sopt_data(&sdata);
3706 vsunlock(sdata.sopt_val, valsize);
3708 /* Restore original pointer and set number of bytes written */
3709 sopt->sopt_val = sdata.sopt_val;
3710 sopt->sopt_valsize = sdata.ktotal;
3711 if (sdata.kbuf != xbuf)
3712 free(sdata.kbuf, M_TEMP);
3718 * {set|get}sockopt parser.
3721 ipfw_ctl(struct sockopt *sopt)
3723 #define RULE_MAXSIZE (512*sizeof(u_int32_t))
3725 size_t size, valsize;
3727 struct ip_fw_rule0 *rule;
3728 struct ip_fw_chain *chain;
3729 u_int32_t rulenum[2];
3731 struct rule_check_info ci;
3734 chain = &V_layer3_chain;
3737 /* Save original valsize before it is altered via sooptcopyin() */
3738 valsize = sopt->sopt_valsize;
3739 opt = sopt->sopt_name;
3742 * Disallow modifications in really-really secure mode, but still allow
3743 * the logging counters to be reset.
3745 if (opt == IP_FW_ADD ||
3746 (sopt->sopt_dir == SOPT_SET && opt != IP_FW_RESETLOG)) {
3747 error = securelevel_ge(sopt->sopt_td->td_ucred, 3);
3755 * pass up a copy of the current rules. Static rules
3756 * come first (the last of which has number IPFW_DEFAULT_RULE),
3757 * followed by a possibly empty list of dynamic rule.
3758 * The last dynamic rule has NULL in the "next" field.
3760 * Note that the calculated size is used to bound the
3761 * amount of data returned to the user. The rule set may
3762 * change between calculating the size and returning the
3763 * data in which case we'll just return what fits.
3768 size = chain->static_len;
3769 size += ipfw_dyn_len();
3770 if (size >= sopt->sopt_valsize)
3772 buf = malloc(size, M_TEMP, M_WAITOK | M_ZERO);
3773 IPFW_UH_RLOCK(chain);
3774 /* check again how much space we need */
3775 want = chain->static_len + ipfw_dyn_len();
3777 len = ipfw_getrules(chain, buf, size);
3778 IPFW_UH_RUNLOCK(chain);
3780 error = sooptcopyout(sopt, buf, len);
3788 /* locking is done within del_entry() */
3789 error = del_entry(chain, 0); /* special case, rule=0, cmd=0 means all */
3793 rule = malloc(RULE_MAXSIZE, M_TEMP, M_WAITOK);
3794 error = sooptcopyin(sopt, rule, RULE_MAXSIZE,
3795 sizeof(struct ip_fw7) );
3797 memset(&ci, 0, sizeof(struct rule_check_info));
3800 * If the size of commands equals RULESIZE7 then we assume
3801 * a FreeBSD7.2 binary is talking to us (set is7=1).
3802 * is7 is persistent so the next 'ipfw list' command
3803 * will use this format.
3804 * NOTE: If wrong version is guessed (this can happen if
3805 * the first ipfw command is 'ipfw [pipe] list')
3806 * the ipfw binary may crash or loop infinitly...
3808 size = sopt->sopt_valsize;
3809 if (size == RULESIZE7(rule)) {
3811 error = convert_rule_to_8(rule);
3816 size = RULESIZE(rule);
3820 error = check_ipfw_rule0(rule, size, &ci);
3822 /* locking is done within add_rule() */
3823 struct ip_fw *krule;
3824 krule = ipfw_alloc_rule(chain, RULEKSIZE0(rule));
3825 ci.urule = (caddr_t)rule;
3828 error = commit_rules(chain, &ci, 1);
3830 free_rule(ci.krule);
3831 else if (sopt->sopt_dir == SOPT_GET) {
3833 error = convert_rule_to_7(rule);
3834 size = RULESIZE7(rule);
3840 error = sooptcopyout(sopt, rule, size);
3848 * IP_FW_DEL is used for deleting single rules or sets,
3849 * and (ab)used to atomically manipulate sets. Argument size
3850 * is used to distinguish between the two:
3852 * delete single rule or set of rules,
3853 * or reassign rules (or sets) to a different set.
3854 * 2*sizeof(u_int32_t)
3855 * atomic disable/enable sets.
3856 * first u_int32_t contains sets to be disabled,
3857 * second u_int32_t contains sets to be enabled.
3859 error = sooptcopyin(sopt, rulenum,
3860 2*sizeof(u_int32_t), sizeof(u_int32_t));
3863 size = sopt->sopt_valsize;
3864 if (size == sizeof(u_int32_t) && rulenum[0] != 0) {
3865 /* delete or reassign, locking done in del_entry() */
3866 error = del_entry(chain, rulenum[0]);
3867 } else if (size == 2*sizeof(u_int32_t)) { /* set enable/disable */
3868 IPFW_UH_WLOCK(chain);
3870 (V_set_disable | rulenum[0]) & ~rulenum[1] &
3871 ~(1<<RESVD_SET); /* set RESVD_SET always enabled */
3872 IPFW_UH_WUNLOCK(chain);
3878 case IP_FW_RESETLOG: /* argument is an u_int_32, the rule number */
3880 if (sopt->sopt_val != 0) {
3881 error = sooptcopyin(sopt, rulenum,
3882 sizeof(u_int32_t), sizeof(u_int32_t));
3886 error = zero_entry(chain, rulenum[0],
3887 sopt->sopt_name == IP_FW_RESETLOG);
3890 /*--- TABLE opcodes ---*/
3891 case IP_FW_TABLE_ADD:
3892 case IP_FW_TABLE_DEL:
3894 ipfw_table_entry ent;
3895 struct tentry_info tei;
3897 struct table_value v;
3899 error = sooptcopyin(sopt, &ent,
3900 sizeof(ent), sizeof(ent));
3904 memset(&tei, 0, sizeof(tei));
3905 tei.paddr = &ent.addr;
3906 tei.subtype = AF_INET;
3907 tei.masklen = ent.masklen;
3908 ipfw_import_table_value_legacy(ent.value, &v);
3910 memset(&ti, 0, sizeof(ti));
3912 ti.type = IPFW_TABLE_CIDR;
3914 error = (opt == IP_FW_TABLE_ADD) ?
3915 add_table_entry(chain, &ti, &tei, 0, 1) :
3916 del_table_entry(chain, &ti, &tei, 0, 1);
3921 case IP_FW_TABLE_FLUSH:
3926 error = sooptcopyin(sopt, &tbl,
3927 sizeof(tbl), sizeof(tbl));
3930 memset(&ti, 0, sizeof(ti));
3932 error = flush_table(chain, &ti);
3936 case IP_FW_TABLE_GETSIZE:
3941 if ((error = sooptcopyin(sopt, &tbl, sizeof(tbl),
3944 memset(&ti, 0, sizeof(ti));
3947 error = ipfw_count_table(chain, &ti, &cnt);
3948 IPFW_RUNLOCK(chain);
3951 error = sooptcopyout(sopt, &cnt, sizeof(cnt));
3955 case IP_FW_TABLE_LIST:
3960 if (sopt->sopt_valsize < sizeof(*tbl)) {
3964 size = sopt->sopt_valsize;
3965 tbl = malloc(size, M_TEMP, M_WAITOK);
3966 error = sooptcopyin(sopt, tbl, size, sizeof(*tbl));
3971 tbl->size = (size - sizeof(*tbl)) /
3972 sizeof(ipfw_table_entry);
3973 memset(&ti, 0, sizeof(ti));
3976 error = ipfw_dump_table_legacy(chain, &ti, tbl);
3977 IPFW_RUNLOCK(chain);
3982 error = sooptcopyout(sopt, tbl, size);
3987 /*--- NAT operations are protected by the IPFW_LOCK ---*/
3989 if (IPFW_NAT_LOADED)
3990 error = ipfw_nat_cfg_ptr(sopt);
3992 printf("IP_FW_NAT_CFG: %s\n",
3993 "ipfw_nat not present, please load it");
3999 if (IPFW_NAT_LOADED)
4000 error = ipfw_nat_del_ptr(sopt);
4002 printf("IP_FW_NAT_DEL: %s\n",
4003 "ipfw_nat not present, please load it");
4008 case IP_FW_NAT_GET_CONFIG:
4009 if (IPFW_NAT_LOADED)
4010 error = ipfw_nat_get_cfg_ptr(sopt);
4012 printf("IP_FW_NAT_GET_CFG: %s\n",
4013 "ipfw_nat not present, please load it");
4018 case IP_FW_NAT_GET_LOG:
4019 if (IPFW_NAT_LOADED)
4020 error = ipfw_nat_get_log_ptr(sopt);
4022 printf("IP_FW_NAT_GET_LOG: %s\n",
4023 "ipfw_nat not present, please load it");
4029 printf("ipfw: ipfw_ctl invalid option %d\n", sopt->sopt_name);
4036 #define RULE_MAXSIZE (256*sizeof(u_int32_t))
4038 /* Functions to convert rules 7.2 <==> 8.0 */
4040 convert_rule_to_7(struct ip_fw_rule0 *rule)
4042 /* Used to modify original rule */
4043 struct ip_fw7 *rule7 = (struct ip_fw7 *)rule;
4044 /* copy of original rule, version 8 */
4045 struct ip_fw_rule0 *tmp;
4047 /* Used to copy commands */
4048 ipfw_insn *ccmd, *dst;
4049 int ll = 0, ccmdlen = 0;
4051 tmp = malloc(RULE_MAXSIZE, M_TEMP, M_NOWAIT | M_ZERO);
4053 return 1; //XXX error
4055 bcopy(rule, tmp, RULE_MAXSIZE);
4058 //rule7->_pad = tmp->_pad;
4059 rule7->set = tmp->set;
4060 rule7->rulenum = tmp->rulenum;
4061 rule7->cmd_len = tmp->cmd_len;
4062 rule7->act_ofs = tmp->act_ofs;
4063 rule7->next_rule = (struct ip_fw7 *)tmp->next_rule;
4064 rule7->cmd_len = tmp->cmd_len;
4065 rule7->pcnt = tmp->pcnt;
4066 rule7->bcnt = tmp->bcnt;
4067 rule7->timestamp = tmp->timestamp;
4070 for (ll = tmp->cmd_len, ccmd = tmp->cmd, dst = rule7->cmd ;
4071 ll > 0 ; ll -= ccmdlen, ccmd += ccmdlen, dst += ccmdlen) {
4072 ccmdlen = F_LEN(ccmd);
4074 bcopy(ccmd, dst, F_LEN(ccmd)*sizeof(uint32_t));
4076 if (dst->opcode > O_NAT)
4077 /* O_REASS doesn't exists in 7.2 version, so
4078 * decrement opcode if it is after O_REASS
4083 printf("ipfw: opcode %d size truncated\n",
4094 convert_rule_to_8(struct ip_fw_rule0 *rule)
4096 /* Used to modify original rule */
4097 struct ip_fw7 *rule7 = (struct ip_fw7 *) rule;
4099 /* Used to copy commands */
4100 ipfw_insn *ccmd, *dst;
4101 int ll = 0, ccmdlen = 0;
4103 /* Copy of original rule */
4104 struct ip_fw7 *tmp = malloc(RULE_MAXSIZE, M_TEMP, M_NOWAIT | M_ZERO);
4106 return 1; //XXX error
4109 bcopy(rule7, tmp, RULE_MAXSIZE);
4111 for (ll = tmp->cmd_len, ccmd = tmp->cmd, dst = rule->cmd ;
4112 ll > 0 ; ll -= ccmdlen, ccmd += ccmdlen, dst += ccmdlen) {
4113 ccmdlen = F_LEN(ccmd);
4115 bcopy(ccmd, dst, F_LEN(ccmd)*sizeof(uint32_t));
4117 if (dst->opcode > O_NAT)
4118 /* O_REASS doesn't exists in 7.2 version, so
4119 * increment opcode if it is after O_REASS
4124 printf("ipfw: opcode %d size truncated\n",
4130 rule->_pad = tmp->_pad;
4131 rule->set = tmp->set;
4132 rule->rulenum = tmp->rulenum;
4133 rule->cmd_len = tmp->cmd_len;
4134 rule->act_ofs = tmp->act_ofs;
4135 rule->next_rule = (struct ip_fw *)tmp->next_rule;
4136 rule->cmd_len = tmp->cmd_len;
4137 rule->id = 0; /* XXX see if is ok = 0 */
4138 rule->pcnt = tmp->pcnt;
4139 rule->bcnt = tmp->bcnt;
4140 rule->timestamp = tmp->timestamp;
4152 ipfw_init_srv(struct ip_fw_chain *ch)
4155 ch->srvmap = ipfw_objhash_create(IPFW_OBJECTS_DEFAULT);
4156 ch->srvstate = malloc(sizeof(void *) * IPFW_OBJECTS_DEFAULT,
4157 M_IPFW, M_WAITOK | M_ZERO);
4161 ipfw_destroy_srv(struct ip_fw_chain *ch)
4164 free(ch->srvstate, M_IPFW);
4165 ipfw_objhash_destroy(ch->srvmap);
4169 * Allocate new bitmask which can be used to enlarge/shrink
4170 * named instance index.
4173 ipfw_objhash_bitmap_alloc(uint32_t items, void **idx, int *pblocks)
4179 KASSERT((items % BLOCK_ITEMS) == 0,
4180 ("bitmask size needs to power of 2 and greater or equal to %zu",
4183 max_blocks = items / BLOCK_ITEMS;
4185 idx_mask = malloc(size * IPFW_MAX_SETS, M_IPFW, M_WAITOK);
4186 /* Mark all as free */
4187 memset(idx_mask, 0xFF, size * IPFW_MAX_SETS);
4188 *idx_mask &= ~(u_long)1; /* Skip index 0 */
4191 *pblocks = max_blocks;
4195 * Copy current bitmask index to new one.
4198 ipfw_objhash_bitmap_merge(struct namedobj_instance *ni, void **idx, int *blocks)
4200 int old_blocks, new_blocks;
4201 u_long *old_idx, *new_idx;
4204 old_idx = ni->idx_mask;
4205 old_blocks = ni->max_blocks;
4207 new_blocks = *blocks;
4209 for (i = 0; i < IPFW_MAX_SETS; i++) {
4210 memcpy(&new_idx[new_blocks * i], &old_idx[old_blocks * i],
4211 old_blocks * sizeof(u_long));
4216 * Swaps current @ni index with new one.
4219 ipfw_objhash_bitmap_swap(struct namedobj_instance *ni, void **idx, int *blocks)
4224 old_idx = ni->idx_mask;
4225 old_blocks = ni->max_blocks;
4227 ni->idx_mask = *idx;
4228 ni->max_blocks = *blocks;
4230 /* Save old values */
4232 *blocks = old_blocks;
4236 ipfw_objhash_bitmap_free(void *idx, int blocks)
4243 * Creates named hash instance.
4244 * Must be called without holding any locks.
4245 * Return pointer to new instance.
4247 struct namedobj_instance *
4248 ipfw_objhash_create(uint32_t items)
4250 struct namedobj_instance *ni;
4254 size = sizeof(struct namedobj_instance) +
4255 sizeof(struct namedobjects_head) * NAMEDOBJ_HASH_SIZE +
4256 sizeof(struct namedobjects_head) * NAMEDOBJ_HASH_SIZE;
4258 ni = malloc(size, M_IPFW, M_WAITOK | M_ZERO);
4259 ni->nn_size = NAMEDOBJ_HASH_SIZE;
4260 ni->nv_size = NAMEDOBJ_HASH_SIZE;
4262 ni->names = (struct namedobjects_head *)(ni +1);
4263 ni->values = &ni->names[ni->nn_size];
4265 for (i = 0; i < ni->nn_size; i++)
4266 TAILQ_INIT(&ni->names[i]);
4268 for (i = 0; i < ni->nv_size; i++)
4269 TAILQ_INIT(&ni->values[i]);
4271 /* Set default hashing/comparison functions */
4272 ni->hash_f = objhash_hash_name;
4273 ni->cmp_f = objhash_cmp_name;
4275 /* Allocate bitmask separately due to possible resize */
4276 ipfw_objhash_bitmap_alloc(items, (void*)&ni->idx_mask, &ni->max_blocks);
4282 ipfw_objhash_destroy(struct namedobj_instance *ni)
4285 free(ni->idx_mask, M_IPFW);
4290 ipfw_objhash_set_funcs(struct namedobj_instance *ni, objhash_hash_f *hash_f,
4291 objhash_cmp_f *cmp_f)
4294 ni->hash_f = hash_f;
4299 objhash_hash_name(struct namedobj_instance *ni, const void *name, uint32_t set)
4302 return (fnv_32_str((const char *)name, FNV1_32_INIT));
4306 objhash_cmp_name(struct named_object *no, const void *name, uint32_t set)
4309 if ((strcmp(no->name, (const char *)name) == 0) && (no->set == set))
4316 objhash_hash_idx(struct namedobj_instance *ni, uint32_t val)
4320 v = val % (ni->nv_size - 1);
4325 struct named_object *
4326 ipfw_objhash_lookup_name(struct namedobj_instance *ni, uint32_t set, char *name)
4328 struct named_object *no;
4331 hash = ni->hash_f(ni, name, set) % ni->nn_size;
4333 TAILQ_FOREACH(no, &ni->names[hash], nn_next) {
4334 if (ni->cmp_f(no, name, set) == 0)
4342 * Find named object by @uid.
4343 * Check @tlvs for valid data inside.
4345 * Returns pointer to found TLV or NULL.
4348 ipfw_find_name_tlv_type(void *tlvs, int len, uint16_t uidx, uint32_t etlv)
4350 ipfw_obj_ntlv *ntlv;
4354 pa = (uintptr_t)tlvs;
4357 for (; pa < pe; pa += l) {
4358 ntlv = (ipfw_obj_ntlv *)pa;
4359 l = ntlv->head.length;
4361 if (l != sizeof(*ntlv))
4364 if (ntlv->idx != uidx)
4367 * When userland has specified zero TLV type, do
4368 * not compare it with eltv. In some cases userland
4369 * doesn't know what type should it have. Use only
4370 * uidx and name for search named_object.
4372 if (ntlv->head.type != 0 &&
4373 ntlv->head.type != (uint16_t)etlv)
4376 if (ipfw_check_object_name_generic(ntlv->name) != 0)
4386 * Finds object config based on either legacy index
4388 * Note @ti structure contains unchecked data from userland.
4390 * Returns 0 in success and fills in @pno with found config
4393 ipfw_objhash_find_type(struct namedobj_instance *ni, struct tid_info *ti,
4394 uint32_t etlv, struct named_object **pno)
4397 ipfw_obj_ntlv *ntlv;
4400 if (ti->tlvs == NULL)
4403 ntlv = ipfw_find_name_tlv_type(ti->tlvs, ti->tlen, ti->uidx, etlv);
4409 * Use set provided by @ti instead of @ntlv one.
4410 * This is needed due to different sets behavior
4411 * controlled by V_fw_tables_sets.
4414 *pno = ipfw_objhash_lookup_name(ni, set, name);
4421 * Find named object by name, considering also its TLV type.
4423 struct named_object *
4424 ipfw_objhash_lookup_name_type(struct namedobj_instance *ni, uint32_t set,
4425 uint32_t type, const char *name)
4427 struct named_object *no;
4430 hash = ni->hash_f(ni, name, set) % ni->nn_size;
4432 TAILQ_FOREACH(no, &ni->names[hash], nn_next) {
4433 if (ni->cmp_f(no, name, set) == 0 &&
4434 no->etlv == (uint16_t)type)
4441 struct named_object *
4442 ipfw_objhash_lookup_kidx(struct namedobj_instance *ni, uint16_t kidx)
4444 struct named_object *no;
4447 hash = objhash_hash_idx(ni, kidx);
4449 TAILQ_FOREACH(no, &ni->values[hash], nv_next) {
4450 if (no->kidx == kidx)
4458 ipfw_objhash_same_name(struct namedobj_instance *ni, struct named_object *a,
4459 struct named_object *b)
4462 if ((strcmp(a->name, b->name) == 0) && a->set == b->set)
4469 ipfw_objhash_add(struct namedobj_instance *ni, struct named_object *no)
4473 hash = ni->hash_f(ni, no->name, no->set) % ni->nn_size;
4474 TAILQ_INSERT_HEAD(&ni->names[hash], no, nn_next);
4476 hash = objhash_hash_idx(ni, no->kidx);
4477 TAILQ_INSERT_HEAD(&ni->values[hash], no, nv_next);
4483 ipfw_objhash_del(struct namedobj_instance *ni, struct named_object *no)
4487 hash = ni->hash_f(ni, no->name, no->set) % ni->nn_size;
4488 TAILQ_REMOVE(&ni->names[hash], no, nn_next);
4490 hash = objhash_hash_idx(ni, no->kidx);
4491 TAILQ_REMOVE(&ni->values[hash], no, nv_next);
4497 ipfw_objhash_count(struct namedobj_instance *ni)
4504 ipfw_objhash_count_type(struct namedobj_instance *ni, uint16_t type)
4506 struct named_object *no;
4511 for (i = 0; i < ni->nn_size; i++) {
4512 TAILQ_FOREACH(no, &ni->names[i], nn_next) {
4513 if (no->etlv == type)
4521 * Runs @func for each found named object.
4522 * It is safe to delete objects from callback
4525 ipfw_objhash_foreach(struct namedobj_instance *ni, objhash_cb_t *f, void *arg)
4527 struct named_object *no, *no_tmp;
4530 for (i = 0; i < ni->nn_size; i++) {
4531 TAILQ_FOREACH_SAFE(no, &ni->names[i], nn_next, no_tmp) {
4532 ret = f(ni, no, arg);
4541 * Runs @f for each found named object with type @type.
4542 * It is safe to delete objects from callback
4545 ipfw_objhash_foreach_type(struct namedobj_instance *ni, objhash_cb_t *f,
4546 void *arg, uint16_t type)
4548 struct named_object *no, *no_tmp;
4551 for (i = 0; i < ni->nn_size; i++) {
4552 TAILQ_FOREACH_SAFE(no, &ni->names[i], nn_next, no_tmp) {
4553 if (no->etlv != type)
4555 ret = f(ni, no, arg);
4564 * Removes index from given set.
4565 * Returns 0 on success.
4568 ipfw_objhash_free_idx(struct namedobj_instance *ni, uint16_t idx)
4573 i = idx / BLOCK_ITEMS;
4574 v = idx % BLOCK_ITEMS;
4576 if (i >= ni->max_blocks)
4579 mask = &ni->idx_mask[i];
4581 if ((*mask & ((u_long)1 << v)) != 0)
4585 *mask |= (u_long)1 << v;
4587 /* Update free offset */
4588 if (ni->free_off[0] > i)
4589 ni->free_off[0] = i;
4595 * Allocate new index in given instance and stores in in @pidx.
4596 * Returns 0 on success.
4599 ipfw_objhash_alloc_idx(void *n, uint16_t *pidx)
4601 struct namedobj_instance *ni;
4605 ni = (struct namedobj_instance *)n;
4607 off = ni->free_off[0];
4608 mask = &ni->idx_mask[off];
4610 for (i = off; i < ni->max_blocks; i++, mask++) {
4611 if ((v = ffsl(*mask)) == 0)
4615 *mask &= ~ ((u_long)1 << (v - 1));
4617 ni->free_off[0] = i;
4619 v = BLOCK_ITEMS * i + v - 1;