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>
61 #include <net/route.h>
64 #include <vm/vm_extern.h>
66 #include <netinet/in.h>
67 #include <netinet/ip_var.h> /* hooks */
68 #include <netinet/ip_fw.h>
70 #include <netpfil/ipfw/ip_fw_private.h>
71 #include <netpfil/ipfw/ip_fw_table.h>
74 #include <security/mac/mac_framework.h>
77 static int ipfw_ctl(struct sockopt *sopt);
78 static int check_ipfw_rule_body(ipfw_insn *cmd, int cmd_len,
79 struct rule_check_info *ci);
80 static int check_ipfw_rule1(struct ip_fw_rule *rule, int size,
81 struct rule_check_info *ci);
82 static int check_ipfw_rule0(struct ip_fw_rule0 *rule, int size,
83 struct rule_check_info *ci);
85 #define NAMEDOBJ_HASH_SIZE 32
87 struct namedobj_instance {
88 struct namedobjects_head *names;
89 struct namedobjects_head *values;
90 uint32_t nn_size; /* names hash size */
91 uint32_t nv_size; /* number hash size */
92 u_long *idx_mask; /* used items bitmask */
93 uint32_t max_blocks; /* number of "long" blocks in bitmask */
94 uint32_t count; /* number of items */
95 uint16_t free_off[IPFW_MAX_SETS]; /* first possible free offset */
96 objhash_hash_f *hash_f;
99 #define BLOCK_ITEMS (8 * sizeof(u_long)) /* Number of items for ffsl() */
101 static uint32_t objhash_hash_name(struct namedobj_instance *ni, void *key,
103 static uint32_t objhash_hash_idx(struct namedobj_instance *ni, uint32_t val);
104 static int objhash_cmp_name(struct named_object *no, void *name, uint32_t set);
106 MALLOC_DEFINE(M_IPFW, "IpFw/IpAcct", "IpFw/IpAcct chain's");
108 static int dump_config(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
109 struct sockopt_data *sd);
110 static int add_rules(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
111 struct sockopt_data *sd);
112 static int del_rules(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
113 struct sockopt_data *sd);
114 static int clear_rules(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
115 struct sockopt_data *sd);
116 static int move_rules(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
117 struct sockopt_data *sd);
118 static int manage_sets(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
119 struct sockopt_data *sd);
120 static int dump_soptcodes(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
121 struct sockopt_data *sd);
123 /* ctl3 handler data */
124 struct mtx ctl3_lock;
125 #define CTL3_LOCK_INIT() mtx_init(&ctl3_lock, "ctl3_lock", NULL, MTX_DEF)
126 #define CTL3_LOCK_DESTROY() mtx_destroy(&ctl3_lock)
127 #define CTL3_LOCK() mtx_lock(&ctl3_lock)
128 #define CTL3_UNLOCK() mtx_unlock(&ctl3_lock)
130 static struct ipfw_sopt_handler *ctl3_handlers;
131 static size_t ctl3_hsize;
132 static uint64_t ctl3_refct, ctl3_gencnt;
133 #define CTL3_SMALLBUF 4096 /* small page-size write buffer */
134 #define CTL3_LARGEBUF 16 * 1024 * 1024 /* handle large rulesets */
136 static int ipfw_flush_sopt_data(struct sockopt_data *sd);
138 static struct ipfw_sopt_handler scodes[] = {
139 { IP_FW_XGET, 0, HDIR_GET, dump_config },
140 { IP_FW_XADD, 0, HDIR_BOTH, add_rules },
141 { IP_FW_XDEL, 0, HDIR_BOTH, del_rules },
142 { IP_FW_XZERO, 0, HDIR_SET, clear_rules },
143 { IP_FW_XRESETLOG, 0, HDIR_SET, clear_rules },
144 { IP_FW_XMOVE, 0, HDIR_SET, move_rules },
145 { IP_FW_SET_SWAP, 0, HDIR_SET, manage_sets },
146 { IP_FW_SET_MOVE, 0, HDIR_SET, manage_sets },
147 { IP_FW_SET_ENABLE, 0, HDIR_SET, manage_sets },
148 { IP_FW_DUMP_SOPTCODES, 0, HDIR_GET, dump_soptcodes },
152 * static variables followed by global ones
155 static VNET_DEFINE(uma_zone_t, ipfw_cntr_zone);
156 #define V_ipfw_cntr_zone VNET(ipfw_cntr_zone)
162 V_ipfw_cntr_zone = uma_zcreate("IPFW counters",
163 IPFW_RULE_CNTR_SIZE, NULL, NULL, NULL, NULL,
164 UMA_ALIGN_PTR, UMA_ZONE_PCPU);
168 ipfw_destroy_counters()
171 uma_zdestroy(V_ipfw_cntr_zone);
175 ipfw_alloc_rule(struct ip_fw_chain *chain, size_t rulesize)
179 rule = malloc(rulesize, M_IPFW, M_WAITOK | M_ZERO);
180 rule->cntr = uma_zalloc(V_ipfw_cntr_zone, M_WAITOK | M_ZERO);
186 free_rule(struct ip_fw *rule)
189 uma_zfree(V_ipfw_cntr_zone, rule->cntr);
195 * Find the smallest rule >= key, id.
196 * We could use bsearch but it is so simple that we code it directly
199 ipfw_find_rule(struct ip_fw_chain *chain, uint32_t key, uint32_t id)
204 for (lo = 0, hi = chain->n_rules - 1; lo < hi;) {
207 if (r->rulenum < key)
208 lo = i + 1; /* continue from the next one */
209 else if (r->rulenum > key)
210 hi = i; /* this might be good */
212 lo = i + 1; /* continue from the next one */
213 else /* r->id >= id */
214 hi = i; /* this might be good */
220 * Builds skipto cache on rule set @map.
223 update_skipto_cache(struct ip_fw_chain *chain, struct ip_fw **map)
228 IPFW_UH_WLOCK_ASSERT(chain);
231 rulenum = map[mi]->rulenum;
232 smap = chain->idxmap_back;
237 for (i = 0; i < 65536; i++) {
239 /* Use the same rule index until i < rulenum */
240 if (i != rulenum || i == 65535)
242 /* Find next rule with num > i */
243 rulenum = map[++mi]->rulenum;
245 rulenum = map[++mi]->rulenum;
250 * Swaps prepared (backup) index with current one.
253 swap_skipto_cache(struct ip_fw_chain *chain)
257 IPFW_UH_WLOCK_ASSERT(chain);
258 IPFW_WLOCK_ASSERT(chain);
261 chain->idxmap = chain->idxmap_back;
262 chain->idxmap_back = map;
266 * Allocate and initialize skipto cache.
269 ipfw_init_skipto_cache(struct ip_fw_chain *chain)
271 int *idxmap, *idxmap_back;
273 idxmap = malloc(65536 * sizeof(uint32_t *), M_IPFW,
275 idxmap_back = malloc(65536 * sizeof(uint32_t *), M_IPFW,
279 * Note we may be called at any time after initialization,
280 * for example, on first skipto rule, so we need to
281 * provide valid chain->idxmap on return
284 IPFW_UH_WLOCK(chain);
285 if (chain->idxmap != NULL) {
286 IPFW_UH_WUNLOCK(chain);
287 free(idxmap, M_IPFW);
288 free(idxmap_back, M_IPFW);
292 /* Set backup pointer first to permit building cache */
293 chain->idxmap_back = idxmap_back;
294 update_skipto_cache(chain, chain->map);
296 /* It is now safe to set chain->idxmap ptr */
297 chain->idxmap = idxmap;
298 swap_skipto_cache(chain);
300 IPFW_UH_WUNLOCK(chain);
304 * Destroys skipto cache.
307 ipfw_destroy_skipto_cache(struct ip_fw_chain *chain)
310 if (chain->idxmap != NULL)
311 free(chain->idxmap, M_IPFW);
312 if (chain->idxmap != NULL)
313 free(chain->idxmap_back, M_IPFW);
318 * allocate a new map, returns the chain locked. extra is the number
319 * of entries to add or delete.
321 static struct ip_fw **
322 get_map(struct ip_fw_chain *chain, int extra, int locked)
329 mflags = M_ZERO | ((locked != 0) ? M_NOWAIT : M_WAITOK);
331 i = chain->n_rules + extra;
332 map = malloc(i * sizeof(struct ip_fw *), M_IPFW, mflags);
334 printf("%s: cannot allocate map\n", __FUNCTION__);
338 IPFW_UH_WLOCK(chain);
339 if (i >= chain->n_rules + extra) /* good */
341 /* otherwise we lost the race, free and retry */
343 IPFW_UH_WUNLOCK(chain);
349 * swap the maps. It is supposed to be called with IPFW_UH_WLOCK
351 static struct ip_fw **
352 swap_map(struct ip_fw_chain *chain, struct ip_fw **new_map, int new_len)
354 struct ip_fw **old_map;
358 chain->n_rules = new_len;
359 old_map = chain->map;
360 chain->map = new_map;
361 swap_skipto_cache(chain);
368 export_cntr1_base(struct ip_fw *krule, struct ip_fw_bcounter *cntr)
371 cntr->size = sizeof(*cntr);
373 if (krule->cntr != NULL) {
374 cntr->pcnt = counter_u64_fetch(krule->cntr);
375 cntr->bcnt = counter_u64_fetch(krule->cntr + 1);
376 cntr->timestamp = krule->timestamp;
378 if (cntr->timestamp > 0)
379 cntr->timestamp += boottime.tv_sec;
383 export_cntr0_base(struct ip_fw *krule, struct ip_fw_bcounter0 *cntr)
386 if (krule->cntr != NULL) {
387 cntr->pcnt = counter_u64_fetch(krule->cntr);
388 cntr->bcnt = counter_u64_fetch(krule->cntr + 1);
389 cntr->timestamp = krule->timestamp;
391 if (cntr->timestamp > 0)
392 cntr->timestamp += boottime.tv_sec;
396 * Copies rule @urule from v1 userland format (current).
398 * Assume @krule is zeroed.
401 import_rule1(struct rule_check_info *ci)
403 struct ip_fw_rule *urule;
406 urule = (struct ip_fw_rule *)ci->urule;
407 krule = (struct ip_fw *)ci->krule;
410 krule->act_ofs = urule->act_ofs;
411 krule->cmd_len = urule->cmd_len;
412 krule->rulenum = urule->rulenum;
413 krule->set = urule->set;
414 krule->flags = urule->flags;
416 /* Save rulenum offset */
417 ci->urule_numoff = offsetof(struct ip_fw_rule, rulenum);
420 memcpy(krule->cmd, urule->cmd, krule->cmd_len * sizeof(uint32_t));
424 * Export rule into v1 format (Current).
426 * [ ipfw_obj_tlv(IPFW_TLV_RULE_ENT)
428 * [ ip_fw_bcounter ip_fw_rule] (depends on rcntrs).
430 * Assume @data is zeroed.
433 export_rule1(struct ip_fw *krule, caddr_t data, int len, int rcntrs)
435 struct ip_fw_bcounter *cntr;
436 struct ip_fw_rule *urule;
439 /* Fill in TLV header */
440 tlv = (ipfw_obj_tlv *)data;
441 tlv->type = IPFW_TLV_RULE_ENT;
446 cntr = (struct ip_fw_bcounter *)(tlv + 1);
447 urule = (struct ip_fw_rule *)(cntr + 1);
448 export_cntr1_base(krule, cntr);
450 urule = (struct ip_fw_rule *)(tlv + 1);
453 urule->act_ofs = krule->act_ofs;
454 urule->cmd_len = krule->cmd_len;
455 urule->rulenum = krule->rulenum;
456 urule->set = krule->set;
457 urule->flags = krule->flags;
458 urule->id = krule->id;
461 memcpy(urule->cmd, krule->cmd, krule->cmd_len * sizeof(uint32_t));
466 * Copies rule @urule from FreeBSD8 userland format (v0)
468 * Assume @krule is zeroed.
471 import_rule0(struct rule_check_info *ci)
473 struct ip_fw_rule0 *urule;
477 ipfw_insn_limit *lcmd;
480 urule = (struct ip_fw_rule0 *)ci->urule;
481 krule = (struct ip_fw *)ci->krule;
484 krule->act_ofs = urule->act_ofs;
485 krule->cmd_len = urule->cmd_len;
486 krule->rulenum = urule->rulenum;
487 krule->set = urule->set;
488 if ((urule->_pad & 1) != 0)
489 krule->flags |= IPFW_RULE_NOOPT;
491 /* Save rulenum offset */
492 ci->urule_numoff = offsetof(struct ip_fw_rule0, rulenum);
495 memcpy(krule->cmd, urule->cmd, krule->cmd_len * sizeof(uint32_t));
499 * 1) convert tablearg value from 65335 to 0
500 * 2) Add high bit to O_SETFIB/O_SETDSCP values (to make room for targ).
501 * 3) convert table number in iface opcodes to u16
507 for ( ; l > 0 ; l -= cmdlen, cmd += cmdlen) {
510 switch (cmd->opcode) {
511 /* Opcodes supporting tablearg */
523 if (cmd->arg1 == 65535)
524 cmd->arg1 = IP_FW_TARG;
528 if (cmd->arg1 == 65535)
529 cmd->arg1 = IP_FW_TARG;
534 lcmd = (ipfw_insn_limit *)cmd;
535 if (lcmd->conn_limit == 65535)
536 lcmd->conn_limit = IP_FW_TARG;
538 /* Interface tables */
542 /* Interface table, possibly */
543 cmdif = (ipfw_insn_if *)cmd;
544 if (cmdif->name[0] != '\1')
547 cmdif->p.kidx = (uint16_t)cmdif->p.glob;
554 * Copies rule @krule from kernel to FreeBSD8 userland format (v0)
557 export_rule0(struct ip_fw *krule, struct ip_fw_rule0 *urule, int len)
561 ipfw_insn_limit *lcmd;
565 memset(urule, 0, len);
566 urule->act_ofs = krule->act_ofs;
567 urule->cmd_len = krule->cmd_len;
568 urule->rulenum = krule->rulenum;
569 urule->set = krule->set;
570 if ((krule->flags & IPFW_RULE_NOOPT) != 0)
574 memcpy(urule->cmd, krule->cmd, krule->cmd_len * sizeof(uint32_t));
576 /* Export counters */
577 export_cntr0_base(krule, (struct ip_fw_bcounter0 *)&urule->pcnt);
581 * 1) convert tablearg value from 0 to 65335
582 * 2) Remove highest bit from O_SETFIB/O_SETDSCP values.
583 * 3) convert table number in iface opcodes to int
589 for ( ; l > 0 ; l -= cmdlen, cmd += cmdlen) {
592 switch (cmd->opcode) {
593 /* Opcodes supporting tablearg */
605 if (cmd->arg1 == IP_FW_TARG)
610 if (cmd->arg1 == IP_FW_TARG)
613 cmd->arg1 &= ~0x8000;
616 lcmd = (ipfw_insn_limit *)cmd;
617 if (lcmd->conn_limit == IP_FW_TARG)
618 lcmd->conn_limit = 65535;
620 /* Interface tables */
624 /* Interface table, possibly */
625 cmdif = (ipfw_insn_if *)cmd;
626 if (cmdif->name[0] != '\1')
629 cmdif->p.glob = cmdif->p.kidx;
636 * Add new rule(s) to the list possibly creating rule number for each.
637 * Update the rule_number in the input struct so the caller knows it as well.
638 * Must be called without IPFW_UH held
641 commit_rules(struct ip_fw_chain *chain, struct rule_check_info *rci, int count)
643 int error, i, insert_before, tcount;
644 uint16_t rulenum, *pnum;
645 struct rule_check_info *ci;
647 struct ip_fw **map; /* the new array of pointers */
649 /* Check if we need to do table remap */
651 for (ci = rci, i = 0; i < count; ci++, i++) {
652 if (ci->table_opcodes == 0)
656 * Rule has some table opcodes.
657 * Reference & allocate needed tables/
659 error = ipfw_rewrite_table_uidx(chain, ci);
663 * rewrite failed, state for current rule
664 * has been reverted. Check if we need to
670 * We have some more table rules
671 * we need to rollback.
674 IPFW_UH_WLOCK(chain);
677 if (ci->table_opcodes == 0)
679 ipfw_unref_rule_tables(chain,ci->krule);
682 IPFW_UH_WUNLOCK(chain);
692 /* get_map returns with IPFW_UH_WLOCK if successful */
693 map = get_map(chain, count, 0 /* not locked */);
697 IPFW_UH_WLOCK(chain);
698 for (ci = rci, i = 0; i < count; ci++, i++) {
699 if (ci->table_opcodes == 0)
702 ipfw_unref_rule_tables(chain, ci->krule);
704 IPFW_UH_WUNLOCK(chain);
710 if (V_autoinc_step < 1)
712 else if (V_autoinc_step > 1000)
713 V_autoinc_step = 1000;
715 /* FIXME: Handle count > 1 */
718 rulenum = krule->rulenum;
720 /* find the insertion point, we will insert before */
721 insert_before = rulenum ? rulenum + 1 : IPFW_DEFAULT_RULE;
722 i = ipfw_find_rule(chain, insert_before, 0);
723 /* duplicate first part */
725 bcopy(chain->map, map, i * sizeof(struct ip_fw *));
727 /* duplicate remaining part, we always have the default rule */
728 bcopy(chain->map + i, map + i + 1,
729 sizeof(struct ip_fw *) *(chain->n_rules - i));
731 /* Compute rule number and write it back */
732 rulenum = i > 0 ? map[i-1]->rulenum : 0;
733 if (rulenum < IPFW_DEFAULT_RULE - V_autoinc_step)
734 rulenum += V_autoinc_step;
735 krule->rulenum = rulenum;
736 /* Save number to userland rule */
737 pnum = (uint16_t *)((caddr_t)ci->urule + ci->urule_numoff);
741 krule->id = chain->id + 1;
742 update_skipto_cache(chain, map);
743 map = swap_map(chain, map, chain->n_rules + 1);
744 chain->static_len += RULEUSIZE0(krule);
745 IPFW_UH_WUNLOCK(chain);
752 * Adds @rule to the list of rules to reap
755 ipfw_reap_add(struct ip_fw_chain *chain, struct ip_fw **head,
759 IPFW_UH_WLOCK_ASSERT(chain);
761 /* Unlink rule from everywhere */
762 ipfw_unref_rule_tables(chain, rule);
764 *((struct ip_fw **)rule) = *head;
769 * Reclaim storage associated with a list of rules. This is
770 * typically the list created using remove_rule.
771 * A NULL pointer on input is handled correctly.
774 ipfw_reap_rules(struct ip_fw *head)
778 while ((rule = head) != NULL) {
779 head = *((struct ip_fw **)head);
786 * (default || reserved || !match_set || !match_number)
788 * default ::= (rule->rulenum == IPFW_DEFAULT_RULE)
789 * // the default rule is always protected
791 * reserved ::= (cmd == 0 && n == 0 && rule->set == RESVD_SET)
792 * // RESVD_SET is protected only if cmd == 0 and n == 0 ("ipfw flush")
794 * match_set ::= (cmd == 0 || rule->set == set)
795 * // set number is ignored for cmd == 0
797 * match_number ::= (cmd == 1 || n == 0 || n == rule->rulenum)
798 * // number is ignored for cmd == 1 or n == 0
802 ipfw_match_range(struct ip_fw *rule, ipfw_range_tlv *rt)
805 /* Don't match default rule for modification queries */
806 if (rule->rulenum == IPFW_DEFAULT_RULE &&
807 (rt->flags & IPFW_RCFLAG_DEFAULT) == 0)
810 /* Don't match rules in reserved set for flush requests */
811 if ((rt->flags & IPFW_RCFLAG_ALL) != 0 && rule->set == RESVD_SET)
814 /* If we're filtering by set, don't match other sets */
815 if ((rt->flags & IPFW_RCFLAG_SET) != 0 && rule->set != rt->set)
818 if ((rt->flags & IPFW_RCFLAG_RANGE) != 0 &&
819 (rule->rulenum < rt->start_rule || rule->rulenum > rt->end_rule))
826 * Delete rules matching range @rt.
827 * Saves number of deleted rules in @ndel.
829 * Returns 0 on success.
832 delete_range(struct ip_fw_chain *chain, ipfw_range_tlv *rt, int *ndel)
834 struct ip_fw *reap, *rule, **map;
839 IPFW_UH_WLOCK(chain); /* arbitrate writers */
842 * Stage 1: Determine range to inspect.
843 * Range is half-inclusive, e.g [start, end).
846 end = chain->n_rules - 1;
848 if ((rt->flags & IPFW_RCFLAG_RANGE) != 0) {
849 start = ipfw_find_rule(chain, rt->start_rule, 0);
851 end = ipfw_find_rule(chain, rt->end_rule, 0);
852 if (rt->end_rule != IPFW_DEFAULT_RULE)
853 while (chain->map[end]->rulenum == rt->end_rule)
857 /* Allocate new map of the same size */
858 map = get_map(chain, 0, 1 /* locked */);
860 IPFW_UH_WUNLOCK(chain);
867 /* 1. bcopy the initial part of the map */
869 bcopy(chain->map, map, start * sizeof(struct ip_fw *));
870 /* 2. copy active rules between start and end */
871 for (i = start; i < end; i++) {
872 rule = chain->map[i];
873 if (ipfw_match_range(rule, rt) == 0) {
879 if (ipfw_is_dyn_rule(rule) != 0)
882 /* 3. copy the final part of the map */
883 bcopy(chain->map + end, map + ofs,
884 (chain->n_rules - end) * sizeof(struct ip_fw *));
885 /* 4. recalculate skipto cache */
886 update_skipto_cache(chain, map);
887 /* 5. swap the maps (under UH_WLOCK + WHLOCK) */
888 map = swap_map(chain, map, chain->n_rules - n);
889 /* 6. Remove all dynamic states originated by deleted rules */
891 ipfw_expire_dyn_rules(chain, rt);
892 /* 7. now remove the rules deleted from the old map */
893 for (i = start; i < end; i++) {
895 if (ipfw_match_range(rule, rt) == 0)
897 chain->static_len -= RULEUSIZE0(rule);
898 ipfw_reap_add(chain, &reap, rule);
900 IPFW_UH_WUNLOCK(chain);
902 ipfw_reap_rules(reap);
910 * Changes set of given rule rannge @rt
913 * Returns 0 on success.
916 move_range(struct ip_fw_chain *chain, ipfw_range_tlv *rt)
921 IPFW_UH_WLOCK(chain);
924 * Move rules with matching paramenerts to a new set.
925 * This one is much more complex. We have to ensure
926 * that all referenced tables (if any) are referenced
927 * by given rule subset only. Otherwise, we can't move
928 * them to new set and have to return error.
930 if (V_fw_tables_sets != 0) {
931 if (ipfw_move_tables_sets(chain, rt, rt->new_set) != 0) {
932 IPFW_UH_WUNLOCK(chain);
937 /* XXX: We have to do swap holding WLOCK */
938 for (i = 0; i < chain->n_rules; i++) {
939 rule = chain->map[i];
940 if (ipfw_match_range(rule, rt) == 0)
942 rule->set = rt->new_set;
945 IPFW_UH_WUNLOCK(chain);
951 * Clear counters for a specific rule.
952 * Normally run under IPFW_UH_RLOCK, but these are idempotent ops
953 * so we only care that rules do not disappear.
956 clear_counters(struct ip_fw *rule, int log_only)
958 ipfw_insn_log *l = (ipfw_insn_log *)ACTION_PTR(rule);
961 IPFW_ZERO_RULE_COUNTER(rule);
962 if (l->o.opcode == O_LOG)
963 l->log_left = l->max_log;
967 * Flushes rules counters and/or log values on matching range.
969 * Returns number of items cleared.
972 clear_range(struct ip_fw_chain *chain, ipfw_range_tlv *rt, int log_only)
979 rt->flags |= IPFW_RCFLAG_DEFAULT;
981 IPFW_UH_WLOCK(chain); /* arbitrate writers */
982 for (i = 0; i < chain->n_rules; i++) {
983 rule = chain->map[i];
984 if (ipfw_match_range(rule, rt) == 0)
986 clear_counters(rule, log_only);
989 IPFW_UH_WUNLOCK(chain);
995 check_range_tlv(ipfw_range_tlv *rt)
998 if (rt->head.length != sizeof(*rt))
1000 if (rt->start_rule > rt->end_rule)
1002 if (rt->set >= IPFW_MAX_SETS || rt->new_set >= IPFW_MAX_SETS)
1005 if ((rt->flags & IPFW_RCFLAG_USER) != rt->flags)
1012 * Delete rules matching specified parameters
1013 * Data layout (v0)(current):
1014 * Request: [ ipfw_obj_header ipfw_range_tlv ]
1015 * Reply: [ ipfw_obj_header ipfw_range_tlv ]
1017 * Saves number of deleted rules in ipfw_range_tlv->new_set.
1019 * Returns 0 on success.
1022 del_rules(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
1023 struct sockopt_data *sd)
1025 ipfw_range_header *rh;
1028 if (sd->valsize != sizeof(*rh))
1031 rh = (ipfw_range_header *)ipfw_get_sopt_space(sd, sd->valsize);
1033 if (check_range_tlv(&rh->range) != 0)
1037 if ((error = delete_range(chain, &rh->range, &ndel)) != 0)
1040 /* Save number of rules deleted */
1041 rh->range.new_set = ndel;
1046 * Move rules/sets matching specified parameters
1047 * Data layout (v0)(current):
1048 * Request: [ ipfw_obj_header ipfw_range_tlv ]
1050 * Returns 0 on success.
1053 move_rules(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
1054 struct sockopt_data *sd)
1056 ipfw_range_header *rh;
1058 if (sd->valsize != sizeof(*rh))
1061 rh = (ipfw_range_header *)ipfw_get_sopt_space(sd, sd->valsize);
1063 if (check_range_tlv(&rh->range) != 0)
1066 return (move_range(chain, &rh->range));
1070 * Clear rule accounting data matching specified parameters
1071 * Data layout (v0)(current):
1072 * Request: [ ipfw_obj_header ipfw_range_tlv ]
1073 * Reply: [ ipfw_obj_header ipfw_range_tlv ]
1075 * Saves number of cleared rules in ipfw_range_tlv->new_set.
1077 * Returns 0 on success.
1080 clear_rules(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
1081 struct sockopt_data *sd)
1083 ipfw_range_header *rh;
1087 if (sd->valsize != sizeof(*rh))
1090 rh = (ipfw_range_header *)ipfw_get_sopt_space(sd, sd->valsize);
1092 if (check_range_tlv(&rh->range) != 0)
1095 log_only = (op3->opcode == IP_FW_XRESETLOG);
1097 num = clear_range(chain, &rh->range, log_only);
1099 if (rh->range.flags & IPFW_RCFLAG_ALL)
1100 msg = log_only ? "All logging counts reset" :
1101 "Accounting cleared";
1103 msg = log_only ? "logging count reset" : "cleared";
1106 int lev = LOG_SECURITY | LOG_NOTICE;
1107 log(lev, "ipfw: %s.\n", msg);
1110 /* Save number of rules cleared */
1111 rh->range.new_set = num;
1116 enable_sets(struct ip_fw_chain *chain, ipfw_range_tlv *rt)
1120 IPFW_UH_WLOCK_ASSERT(chain);
1122 /* Change enabled/disabled sets mask */
1123 v_set = (V_set_disable | rt->set) & ~rt->new_set;
1124 v_set &= ~(1 << RESVD_SET); /* set RESVD_SET always enabled */
1126 V_set_disable = v_set;
1127 IPFW_WUNLOCK(chain);
1131 swap_sets(struct ip_fw_chain *chain, ipfw_range_tlv *rt, int mv)
1136 IPFW_UH_WLOCK_ASSERT(chain);
1138 /* Swap or move two sets */
1139 for (i = 0; i < chain->n_rules - 1; i++) {
1140 rule = chain->map[i];
1141 if (rule->set == rt->set)
1142 rule->set = rt->new_set;
1143 else if (rule->set == rt->new_set && mv == 0)
1144 rule->set = rt->set;
1146 if (V_fw_tables_sets != 0)
1147 ipfw_swap_tables_sets(chain, rt->set, rt->new_set, mv);
1151 * Swaps or moves set
1152 * Data layout (v0)(current):
1153 * Request: [ ipfw_obj_header ipfw_range_tlv ]
1155 * Returns 0 on success.
1158 manage_sets(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
1159 struct sockopt_data *sd)
1161 ipfw_range_header *rh;
1163 if (sd->valsize != sizeof(*rh))
1166 rh = (ipfw_range_header *)ipfw_get_sopt_space(sd, sd->valsize);
1168 if (rh->range.head.length != sizeof(ipfw_range_tlv))
1171 IPFW_UH_WLOCK(chain);
1172 switch (op3->opcode) {
1173 case IP_FW_SET_SWAP:
1174 case IP_FW_SET_MOVE:
1175 swap_sets(chain, &rh->range, op3->opcode == IP_FW_SET_MOVE);
1177 case IP_FW_SET_ENABLE:
1178 enable_sets(chain, &rh->range);
1181 IPFW_UH_WUNLOCK(chain);
1187 * Remove all rules with given number, or do set manipulation.
1188 * Assumes chain != NULL && *chain != NULL.
1190 * The argument is an uint32_t. The low 16 bit are the rule or set number;
1191 * the next 8 bits are the new set; the top 8 bits indicate the command:
1193 * 0 delete rules numbered "rulenum"
1194 * 1 delete rules in set "rulenum"
1195 * 2 move rules "rulenum" to set "new_set"
1196 * 3 move rules from set "rulenum" to set "new_set"
1197 * 4 swap sets "rulenum" and "new_set"
1198 * 5 delete rules "rulenum" and set "new_set"
1201 del_entry(struct ip_fw_chain *chain, uint32_t arg)
1203 uint32_t num; /* rule number or old_set */
1204 uint8_t cmd, new_set;
1210 cmd = (arg >> 24) & 0xff;
1211 new_set = (arg >> 16) & 0xff;
1213 if (cmd > 5 || new_set > RESVD_SET)
1215 if (cmd == 0 || cmd == 2 || cmd == 5) {
1216 if (num >= IPFW_DEFAULT_RULE)
1219 if (num > RESVD_SET) /* old_set */
1223 /* Convert old requests into new representation */
1224 memset(&rt, 0, sizeof(rt));
1225 rt.start_rule = num;
1228 rt.new_set = new_set;
1232 case 0: /* delete rules numbered "rulenum" */
1234 rt.flags |= IPFW_RCFLAG_ALL;
1236 rt.flags |= IPFW_RCFLAG_RANGE;
1239 case 1: /* delete rules in set "rulenum" */
1240 rt.flags |= IPFW_RCFLAG_SET;
1243 case 5: /* delete rules "rulenum" and set "new_set" */
1244 rt.flags |= IPFW_RCFLAG_RANGE | IPFW_RCFLAG_SET;
1249 case 2: /* move rules "rulenum" to set "new_set" */
1250 rt.flags |= IPFW_RCFLAG_RANGE;
1252 case 3: /* move rules from set "rulenum" to set "new_set" */
1253 IPFW_UH_WLOCK(chain);
1254 swap_sets(chain, &rt, 1);
1255 IPFW_UH_WUNLOCK(chain);
1257 case 4: /* swap sets "rulenum" and "new_set" */
1258 IPFW_UH_WLOCK(chain);
1259 swap_sets(chain, &rt, 0);
1260 IPFW_UH_WUNLOCK(chain);
1267 if ((error = delete_range(chain, &rt, &ndel)) != 0)
1270 if (ndel == 0 && (cmd != 1 && num != 0))
1276 return (move_range(chain, &rt));
1280 * Reset some or all counters on firewall rules.
1281 * The argument `arg' is an u_int32_t. The low 16 bit are the rule number,
1282 * the next 8 bits are the set number, the top 8 bits are the command:
1283 * 0 work with rules from all set's;
1284 * 1 work with rules only from specified set.
1285 * Specified rule number is zero if we want to clear all entries.
1286 * log_only is 1 if we only want to reset logs, zero otherwise.
1289 zero_entry(struct ip_fw_chain *chain, u_int32_t arg, int log_only)
1295 uint16_t rulenum = arg & 0xffff;
1296 uint8_t set = (arg >> 16) & 0xff;
1297 uint8_t cmd = (arg >> 24) & 0xff;
1301 if (cmd == 1 && set > RESVD_SET)
1304 IPFW_UH_RLOCK(chain);
1306 V_norule_counter = 0;
1307 for (i = 0; i < chain->n_rules; i++) {
1308 rule = chain->map[i];
1309 /* Skip rules not in our set. */
1310 if (cmd == 1 && rule->set != set)
1312 clear_counters(rule, log_only);
1314 msg = log_only ? "All logging counts reset" :
1315 "Accounting cleared";
1318 for (i = 0; i < chain->n_rules; i++) {
1319 rule = chain->map[i];
1320 if (rule->rulenum == rulenum) {
1321 if (cmd == 0 || rule->set == set)
1322 clear_counters(rule, log_only);
1325 if (rule->rulenum > rulenum)
1328 if (!cleared) { /* we did not find any matching rules */
1329 IPFW_UH_RUNLOCK(chain);
1332 msg = log_only ? "logging count reset" : "cleared";
1334 IPFW_UH_RUNLOCK(chain);
1337 int lev = LOG_SECURITY | LOG_NOTICE;
1340 log(lev, "ipfw: Entry %d %s.\n", rulenum, msg);
1342 log(lev, "ipfw: %s.\n", msg);
1349 * Check rule head in FreeBSD11 format
1353 check_ipfw_rule1(struct ip_fw_rule *rule, int size,
1354 struct rule_check_info *ci)
1358 if (size < sizeof(*rule)) {
1359 printf("ipfw: rule too short\n");
1363 /* Check for valid cmd_len */
1364 l = roundup2(RULESIZE(rule), sizeof(uint64_t));
1366 printf("ipfw: size mismatch (have %d want %d)\n", size, l);
1369 if (rule->act_ofs >= rule->cmd_len) {
1370 printf("ipfw: bogus action offset (%u > %u)\n",
1371 rule->act_ofs, rule->cmd_len - 1);
1375 if (rule->rulenum > IPFW_DEFAULT_RULE - 1)
1378 return (check_ipfw_rule_body(rule->cmd, rule->cmd_len, ci));
1382 * Check rule head in FreeBSD8 format
1386 check_ipfw_rule0(struct ip_fw_rule0 *rule, int size,
1387 struct rule_check_info *ci)
1391 if (size < sizeof(*rule)) {
1392 printf("ipfw: rule too short\n");
1396 /* Check for valid cmd_len */
1397 l = sizeof(*rule) + rule->cmd_len * 4 - 4;
1399 printf("ipfw: size mismatch (have %d want %d)\n", size, l);
1402 if (rule->act_ofs >= rule->cmd_len) {
1403 printf("ipfw: bogus action offset (%u > %u)\n",
1404 rule->act_ofs, rule->cmd_len - 1);
1408 if (rule->rulenum > IPFW_DEFAULT_RULE - 1)
1411 return (check_ipfw_rule_body(rule->cmd, rule->cmd_len, ci));
1415 check_ipfw_rule_body(ipfw_insn *cmd, int cmd_len, struct rule_check_info *ci)
1423 * Now go for the individual checks. Very simple ones, basically only
1424 * instruction sizes.
1426 for (l = cmd_len; l > 0 ; l -= cmdlen, cmd += cmdlen) {
1427 cmdlen = F_LEN(cmd);
1429 printf("ipfw: opcode %d size truncated\n",
1433 switch (cmd->opcode) {
1445 case O_IPPRECEDENCE:
1463 if (cmdlen != F_INSN_SIZE(ipfw_insn))
1468 if (cmdlen != F_INSN_SIZE(ipfw_insn))
1470 if (cmd->arg1 >= rt_numfibs) {
1471 printf("ipfw: invalid fib number %d\n",
1478 if (cmdlen != F_INSN_SIZE(ipfw_insn))
1480 if ((cmd->arg1 != IP_FW_TARG) &&
1481 ((cmd->arg1 & 0x7FFFF) >= rt_numfibs)) {
1482 printf("ipfw: invalid fib number %d\n",
1483 cmd->arg1 & 0x7FFFF);
1497 if (cmdlen != F_INSN_SIZE(ipfw_insn_u32))
1502 if (cmdlen != F_INSN_SIZE(ipfw_insn_limit))
1507 if (cmdlen != F_INSN_SIZE(ipfw_insn_log))
1510 ((ipfw_insn_log *)cmd)->log_left =
1511 ((ipfw_insn_log *)cmd)->max_log;
1517 /* only odd command lengths */
1518 if ( !(cmdlen & 1) || cmdlen > 31)
1524 if (cmd->arg1 == 0 || cmd->arg1 > 256) {
1525 printf("ipfw: invalid set size %d\n",
1529 if (cmdlen != F_INSN_SIZE(ipfw_insn_u32) +
1534 case O_IP_SRC_LOOKUP:
1535 case O_IP_DST_LOOKUP:
1536 if (cmd->arg1 >= V_fw_tables_max) {
1537 printf("ipfw: invalid table number %d\n",
1541 if (cmdlen != F_INSN_SIZE(ipfw_insn) &&
1542 cmdlen != F_INSN_SIZE(ipfw_insn_u32) + 1 &&
1543 cmdlen != F_INSN_SIZE(ipfw_insn_u32))
1545 ci->table_opcodes++;
1547 case O_IP_FLOW_LOOKUP:
1548 if (cmd->arg1 >= V_fw_tables_max) {
1549 printf("ipfw: invalid table number %d\n",
1553 if (cmdlen != F_INSN_SIZE(ipfw_insn) &&
1554 cmdlen != F_INSN_SIZE(ipfw_insn_u32))
1556 ci->table_opcodes++;
1559 if (cmdlen != F_INSN_SIZE(ipfw_insn_mac))
1570 if (cmdlen < 1 || cmdlen > 31)
1575 if (cmdlen != F_INSN_SIZE(ipfw_insn_u32) + 1)
1581 case O_IP_DSTPORT: /* XXX artificial limit, 30 port pairs */
1582 if (cmdlen < 2 || cmdlen > 31)
1589 if (((ipfw_insn_if *)cmd)->name[0] == '\1')
1590 ci->table_opcodes++;
1591 if (cmdlen != F_INSN_SIZE(ipfw_insn_if))
1596 if (cmdlen != F_INSN_SIZE(ipfw_insn_altq))
1602 if (cmdlen != F_INSN_SIZE(ipfw_insn))
1607 if (cmdlen != F_INSN_SIZE(ipfw_insn_sa))
1612 if (cmdlen != F_INSN_SIZE(ipfw_insn_sa6))
1619 if (ip_divert_ptr == NULL)
1625 if (ng_ipfw_input_p == NULL)
1630 if (!IPFW_NAT_LOADED)
1632 if (cmdlen != F_INSN_SIZE(ipfw_insn_nat))
1635 case O_FORWARD_MAC: /* XXX not implemented yet */
1649 if (cmdlen != F_INSN_SIZE(ipfw_insn))
1653 printf("ipfw: opcode %d, multiple actions"
1660 printf("ipfw: opcode %d, action must be"
1669 if (cmdlen != F_INSN_SIZE(struct in6_addr) +
1670 F_INSN_SIZE(ipfw_insn))
1675 if (cmdlen != F_INSN_SIZE(ipfw_insn_u32) +
1676 ((ipfw_insn_u32 *)cmd)->o.arg1)
1680 case O_IP6_SRC_MASK:
1681 case O_IP6_DST_MASK:
1682 if ( !(cmdlen & 1) || cmdlen > 127)
1686 if( cmdlen != F_INSN_SIZE( ipfw_insn_icmp6 ) )
1692 switch (cmd->opcode) {
1702 case O_IP6_SRC_MASK:
1703 case O_IP6_DST_MASK:
1705 printf("ipfw: no IPv6 support in kernel\n");
1706 return (EPROTONOSUPPORT);
1709 printf("ipfw: opcode %d, unknown opcode\n",
1715 if (have_action == 0) {
1716 printf("ipfw: missing action\n");
1722 printf("ipfw: opcode %d size %d wrong\n",
1723 cmd->opcode, cmdlen);
1729 * Translation of requests for compatibility with FreeBSD 7.2/8.
1730 * a static variable tells us if we have an old client from userland,
1731 * and if necessary we translate requests and responses between the
1737 struct ip_fw7 *next; /* linked list of rules */
1738 struct ip_fw7 *next_rule; /* ptr to next [skipto] rule */
1739 /* 'next_rule' is used to pass up 'set_disable' status */
1741 uint16_t act_ofs; /* offset of action in 32-bit units */
1742 uint16_t cmd_len; /* # of 32-bit words in cmd */
1743 uint16_t rulenum; /* rule number */
1744 uint8_t set; /* rule set (0..31) */
1745 // #define RESVD_SET 31 /* set for default and persistent rules */
1746 uint8_t _pad; /* padding */
1747 // uint32_t id; /* rule id, only in v.8 */
1748 /* These fields are present in all rules. */
1749 uint64_t pcnt; /* Packet counter */
1750 uint64_t bcnt; /* Byte counter */
1751 uint32_t timestamp; /* tv_sec of last match */
1753 ipfw_insn cmd[1]; /* storage for commands */
1756 static int convert_rule_to_7(struct ip_fw_rule0 *rule);
1757 static int convert_rule_to_8(struct ip_fw_rule0 *rule);
1760 #define RULESIZE7(rule) (sizeof(struct ip_fw7) + \
1761 ((struct ip_fw7 *)(rule))->cmd_len * 4 - 4)
1766 * Copy the static and dynamic rules to the supplied buffer
1767 * and return the amount of space actually used.
1768 * Must be run under IPFW_UH_RLOCK
1771 ipfw_getrules(struct ip_fw_chain *chain, void *buf, size_t space)
1774 char *ep = bp + space;
1776 struct ip_fw_rule0 *dst;
1777 int error, i, l, warnflag;
1778 time_t boot_seconds;
1782 boot_seconds = boottime.tv_sec;
1783 for (i = 0; i < chain->n_rules; i++) {
1784 rule = chain->map[i];
1787 /* Convert rule to FreeBSd 7.2 format */
1788 l = RULESIZE7(rule);
1789 if (bp + l + sizeof(uint32_t) <= ep) {
1790 bcopy(rule, bp, l + sizeof(uint32_t));
1791 error = ipfw_rewrite_table_kidx(chain,
1792 (struct ip_fw_rule0 *)bp);
1795 error = convert_rule_to_7((struct ip_fw_rule0 *) bp);
1797 return 0; /*XXX correct? */
1799 * XXX HACK. Store the disable mask in the "next"
1800 * pointer in a wild attempt to keep the ABI the same.
1801 * Why do we do this on EVERY rule?
1803 bcopy(&V_set_disable,
1804 &(((struct ip_fw7 *)bp)->next_rule),
1805 sizeof(V_set_disable));
1806 if (((struct ip_fw7 *)bp)->timestamp)
1807 ((struct ip_fw7 *)bp)->timestamp += boot_seconds;
1810 continue; /* go to next rule */
1813 l = RULEUSIZE0(rule);
1814 if (bp + l > ep) { /* should not happen */
1815 printf("overflow dumping static rules\n");
1818 dst = (struct ip_fw_rule0 *)bp;
1819 export_rule0(rule, dst, l);
1820 error = ipfw_rewrite_table_kidx(chain, dst);
1823 * XXX HACK. Store the disable mask in the "next"
1824 * pointer in a wild attempt to keep the ABI the same.
1825 * Why do we do this on EVERY rule?
1827 * XXX: "ipfw set show" (ab)uses IP_FW_GET to read disabled mask
1828 * so we need to fail _after_ saving at least one mask.
1830 bcopy(&V_set_disable, &dst->next_rule, sizeof(V_set_disable));
1832 dst->timestamp += boot_seconds;
1837 /* Non-fatal table rewrite error. */
1841 printf("Stop on rule %d. Fail to convert table\n",
1847 printf("ipfw: process %s is using legacy interfaces,"
1848 " consider rebuilding\n", "");
1849 ipfw_get_dynamic(chain, &bp, ep); /* protected by the dynamic lock */
1850 return (bp - (char *)buf);
1855 uint32_t b; /* start rule */
1856 uint32_t e; /* end rule */
1857 uint32_t rcount; /* number of rules */
1858 uint32_t rsize; /* rules size */
1859 uint32_t tcount; /* number of tables */
1860 int rcounters; /* counters */
1864 * Dumps static rules with table TLVs in buffer @sd.
1866 * Returns 0 on success.
1869 dump_static_rules(struct ip_fw_chain *chain, struct dump_args *da,
1870 uint32_t *bmask, struct sockopt_data *sd)
1875 ipfw_obj_ctlv *ctlv;
1876 struct ip_fw *krule;
1879 /* Dump table names first (if any) */
1880 if (da->tcount > 0) {
1882 ctlv = (ipfw_obj_ctlv *)ipfw_get_sopt_space(sd, sizeof(*ctlv));
1885 ctlv->head.type = IPFW_TLV_TBLNAME_LIST;
1886 ctlv->head.length = da->tcount * sizeof(ipfw_obj_ntlv) +
1888 ctlv->count = da->tcount;
1889 ctlv->objsize = sizeof(ipfw_obj_ntlv);
1893 tcount = da->tcount;
1894 while (tcount > 0) {
1895 if ((bmask[i / 32] & (1 << (i % 32))) == 0) {
1900 if ((error = ipfw_export_table_ntlv(chain, i, sd)) != 0)
1908 ctlv = (ipfw_obj_ctlv *)ipfw_get_sopt_space(sd, sizeof(*ctlv));
1911 ctlv->head.type = IPFW_TLV_RULE_LIST;
1912 ctlv->head.length = da->rsize + sizeof(*ctlv);
1913 ctlv->count = da->rcount;
1915 for (i = da->b; i < da->e; i++) {
1916 krule = chain->map[i];
1918 l = RULEUSIZE1(krule) + sizeof(ipfw_obj_tlv);
1919 if (da->rcounters != 0)
1920 l += sizeof(struct ip_fw_bcounter);
1921 dst = (caddr_t)ipfw_get_sopt_space(sd, l);
1925 export_rule1(krule, dst, l, da->rcounters);
1932 * Dumps requested objects data
1933 * Data layout (version 0)(current):
1934 * Request: [ ipfw_cfg_lheader ] + IPFW_CFG_GET_* flags
1935 * size = ipfw_cfg_lheader.size
1936 * Reply: [ ipfw_cfg_lheader
1937 * [ ipfw_obj_ctlv(IPFW_TLV_TBL_LIST) ipfw_obj_ntlv x N ] (optional)
1938 * [ ipfw_obj_ctlv(IPFW_TLV_RULE_LIST)
1939 * ipfw_obj_tlv(IPFW_TLV_RULE_ENT) [ ip_fw_bcounter (optional) ip_fw_rule ]
1941 * [ ipfw_obj_ctlv(IPFW_TLV_STATE_LIST) ipfw_obj_dyntlv x N ] (optional)
1943 * * NOTE IPFW_TLV_STATE_LIST has the single valid field: objsize.
1944 * The rest (size, count) are set to zero and needs to be ignored.
1946 * Returns 0 on success.
1949 dump_config(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
1950 struct sockopt_data *sd)
1952 ipfw_cfg_lheader *hdr;
1957 struct dump_args da;
1960 hdr = (ipfw_cfg_lheader *)ipfw_get_sopt_header(sd, sizeof(*hdr));
1966 /* Allocate needed state */
1967 if (hdr->flags & IPFW_CFG_GET_STATIC)
1968 bmask = malloc(IPFW_TABLES_MAX / 8, M_TEMP, M_WAITOK | M_ZERO);
1970 IPFW_UH_RLOCK(chain);
1973 * STAGE 1: Determine size/count for objects in range.
1974 * Prepare used tables bitmask.
1976 sz = sizeof(ipfw_cfg_lheader);
1977 memset(&da, 0, sizeof(da));
1980 da.e = chain->n_rules;
1982 if (hdr->end_rule != 0) {
1983 /* Handle custom range */
1984 if ((rnum = hdr->start_rule) > IPFW_DEFAULT_RULE)
1985 rnum = IPFW_DEFAULT_RULE;
1986 da.b = ipfw_find_rule(chain, rnum, 0);
1987 rnum = hdr->end_rule;
1988 rnum = (rnum < IPFW_DEFAULT_RULE) ? rnum+1 : IPFW_DEFAULT_RULE;
1989 da.e = ipfw_find_rule(chain, rnum, 0) + 1;
1992 if (hdr->flags & IPFW_CFG_GET_STATIC) {
1993 for (i = da.b; i < da.e; i++) {
1994 rule = chain->map[i];
1995 da.rsize += RULEUSIZE1(rule) + sizeof(ipfw_obj_tlv);
1997 da.tcount += ipfw_mark_table_kidx(chain, rule, bmask);
1999 /* Add counters if requested */
2000 if (hdr->flags & IPFW_CFG_GET_COUNTERS) {
2001 da.rsize += sizeof(struct ip_fw_bcounter) * da.rcount;
2006 sz += da.tcount * sizeof(ipfw_obj_ntlv) +
2007 sizeof(ipfw_obj_ctlv);
2008 sz += da.rsize + sizeof(ipfw_obj_ctlv);
2011 if (hdr->flags & IPFW_CFG_GET_STATES)
2012 sz += ipfw_dyn_get_count() * sizeof(ipfw_obj_dyntlv) +
2013 sizeof(ipfw_obj_ctlv);
2017 * Fill header anyway.
2018 * Note we have to save header fields to stable storage
2019 * buffer inside @sd can be flushed after dumping rules
2022 hdr->set_mask = ~V_set_disable;
2023 hdr_flags = hdr->flags;
2026 if (sd->valsize < sz) {
2031 /* STAGE2: Store actual data */
2032 if (hdr_flags & IPFW_CFG_GET_STATIC) {
2033 error = dump_static_rules(chain, &da, bmask, sd);
2038 if (hdr_flags & IPFW_CFG_GET_STATES)
2039 error = ipfw_dump_states(chain, sd);
2042 IPFW_UH_RUNLOCK(chain);
2045 free(bmask, M_TEMP);
2051 check_object_name(ipfw_obj_ntlv *ntlv)
2055 switch (ntlv->head.type) {
2056 case IPFW_TLV_TBL_NAME:
2057 error = ipfw_check_table_name(ntlv->name);
2067 * Adds one or more rules to ipfw @chain.
2068 * Data layout (version 0)(current):
2072 * [ ipfw_obj_ctlv(IPFW_TLV_TBL_LIST) ipfw_obj_ntlv x N ] (optional *1)
2073 * [ ipfw_obj_ctlv(IPFW_TLV_RULE_LIST) ip_fw x N ] (*2) (*3)
2078 * [ ipfw_obj_ctlv(IPFW_TLV_TBL_LIST) ipfw_obj_ntlv x N ] (optional)
2079 * [ ipfw_obj_ctlv(IPFW_TLV_RULE_LIST) ip_fw x N ]
2082 * Rules in reply are modified to store their actual ruleset number.
2084 * (*1) TLVs inside IPFW_TLV_TBL_LIST needs to be sorted ascending
2085 * accoring to their idx field and there has to be no duplicates.
2086 * (*2) Numbered rules inside IPFW_TLV_RULE_LIST needs to be sorted ascending.
2087 * (*3) Each ip_fw structure needs to be aligned to u64 boundary.
2089 * Returns 0 on success.
2092 add_rules(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
2093 struct sockopt_data *sd)
2095 ipfw_obj_ctlv *ctlv, *rtlv, *tstate;
2096 ipfw_obj_ntlv *ntlv;
2097 int clen, error, idx;
2098 uint32_t count, read;
2099 struct ip_fw_rule *r;
2100 struct rule_check_info rci, *ci, *cbuf;
2103 op3 = (ip_fw3_opheader *)ipfw_get_sopt_space(sd, sd->valsize);
2104 ctlv = (ipfw_obj_ctlv *)(op3 + 1);
2106 read = sizeof(ip_fw3_opheader);
2110 memset(&rci, 0, sizeof(struct rule_check_info));
2112 if (read + sizeof(*ctlv) > sd->valsize)
2115 if (ctlv->head.type == IPFW_TLV_TBLNAME_LIST) {
2116 clen = ctlv->head.length;
2117 /* Check size and alignment */
2118 if (clen > sd->valsize || clen < sizeof(*ctlv))
2120 if ((clen % sizeof(uint64_t)) != 0)
2124 * Some table names or other named objects.
2125 * Check for validness.
2127 count = (ctlv->head.length - sizeof(*ctlv)) / sizeof(*ntlv);
2128 if (ctlv->count != count || ctlv->objsize != sizeof(*ntlv))
2133 * Ensure TLVs are sorted ascending and
2134 * there are no duplicates.
2137 ntlv = (ipfw_obj_ntlv *)(ctlv + 1);
2139 if (ntlv->head.length != sizeof(ipfw_obj_ntlv))
2142 error = check_object_name(ntlv);
2146 if (ntlv->idx <= idx)
2155 read += ctlv->head.length;
2156 ctlv = (ipfw_obj_ctlv *)((caddr_t)ctlv + ctlv->head.length);
2159 if (read + sizeof(*ctlv) > sd->valsize)
2162 if (ctlv->head.type == IPFW_TLV_RULE_LIST) {
2163 clen = ctlv->head.length;
2164 if (clen + read > sd->valsize || clen < sizeof(*ctlv))
2166 if ((clen % sizeof(uint64_t)) != 0)
2170 * TODO: Permit adding multiple rules at once
2172 if (ctlv->count != 1)
2175 clen -= sizeof(*ctlv);
2177 if (ctlv->count > clen / sizeof(struct ip_fw_rule))
2180 /* Allocate state for each rule or use stack */
2181 if (ctlv->count == 1) {
2182 memset(&rci, 0, sizeof(struct rule_check_info));
2185 cbuf = malloc(ctlv->count * sizeof(*ci), M_TEMP,
2190 * Check each rule for validness.
2191 * Ensure numbered rules are sorted ascending
2192 * and properly aligned
2195 r = (struct ip_fw_rule *)(ctlv + 1);
2199 rsize = roundup2(RULESIZE(r), sizeof(uint64_t));
2200 if (rsize > clen || ctlv->count <= count) {
2206 error = check_ipfw_rule1(r, rsize, ci);
2211 if (r->rulenum != 0 && r->rulenum < idx) {
2212 printf("rulenum %d idx %d\n", r->rulenum, idx);
2218 ci->urule = (caddr_t)r;
2220 rsize = roundup2(rsize, sizeof(uint64_t));
2222 r = (struct ip_fw_rule *)((caddr_t)r + rsize);
2227 if (ctlv->count != count || error != 0) {
2234 read += ctlv->head.length;
2235 ctlv = (ipfw_obj_ctlv *)((caddr_t)ctlv + ctlv->head.length);
2238 if (read != sd->valsize || rtlv == NULL || rtlv->count == 0) {
2239 if (cbuf != NULL && cbuf != &rci)
2245 * Passed rules seems to be valid.
2246 * Allocate storage and try to add them to chain.
2248 for (i = 0, ci = cbuf; i < rtlv->count; i++, ci++) {
2249 clen = RULEKSIZE1((struct ip_fw_rule *)ci->urule);
2250 ci->krule = ipfw_alloc_rule(chain, clen);
2254 if ((error = commit_rules(chain, cbuf, rtlv->count)) != 0) {
2255 /* Free allocate krules */
2256 for (i = 0, ci = cbuf; i < rtlv->count; i++, ci++)
2257 free(ci->krule, M_IPFW);
2260 if (cbuf != NULL && cbuf != &rci)
2267 * Lists all sopts currently registered.
2268 * Data layout (v0)(current):
2269 * Request: [ ipfw_obj_lheader ], size = ipfw_obj_lheader.size
2270 * Reply: [ ipfw_obj_lheader ipfw_sopt_info x N ]
2272 * Returns 0 on success
2275 dump_soptcodes(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
2276 struct sockopt_data *sd)
2278 struct _ipfw_obj_lheader *olh;
2280 struct ipfw_sopt_handler *sh;
2281 uint32_t count, n, size;
2283 olh = (struct _ipfw_obj_lheader *)ipfw_get_sopt_header(sd,sizeof(*olh));
2286 if (sd->valsize < olh->size)
2291 size = count * sizeof(ipfw_sopt_info) + sizeof(ipfw_obj_lheader);
2293 /* Fill in header regadless of buffer size */
2295 olh->objsize = sizeof(ipfw_sopt_info);
2297 if (size > olh->size) {
2304 for (n = 1; n <= count; n++) {
2305 i = (ipfw_sopt_info *)ipfw_get_sopt_space(sd, sizeof(*i));
2306 KASSERT(i != 0, ("previously checked buffer is not enough"));
2307 sh = &ctl3_handlers[n];
2308 i->opcode = sh->opcode;
2309 i->version = sh->version;
2310 i->refcnt = sh->refcnt;
2318 * Compares two sopt handlers (code, version and handler ptr).
2319 * Used both as qsort() and bsearch().
2320 * Does not compare handler for latter case.
2322 * Returns 0 if match is found.
2325 compare_sh(const void *_a, const void *_b)
2327 const struct ipfw_sopt_handler *a, *b;
2329 a = (const struct ipfw_sopt_handler *)_a;
2330 b = (const struct ipfw_sopt_handler *)_b;
2332 if (a->opcode < b->opcode)
2334 else if (a->opcode > b->opcode)
2337 if (a->version < b->version)
2339 else if (a->version > b->version)
2342 /* bsearch helper */
2343 if (a->handler == NULL)
2346 if ((uintptr_t)a->handler < (uintptr_t)b->handler)
2348 else if ((uintptr_t)b->handler > (uintptr_t)b->handler)
2355 * Finds sopt handler based on @code and @version.
2357 * Returns pointer to handler or NULL.
2359 static struct ipfw_sopt_handler *
2360 find_sh(uint16_t code, uint8_t version, void *handler)
2362 struct ipfw_sopt_handler *sh, h;
2364 memset(&h, 0, sizeof(h));
2366 h.version = version;
2367 h.handler = handler;
2369 sh = (struct ipfw_sopt_handler *)bsearch(&h, ctl3_handlers,
2370 ctl3_hsize, sizeof(h), compare_sh);
2376 find_ref_sh(uint16_t opcode, uint8_t version, struct ipfw_sopt_handler *psh)
2378 struct ipfw_sopt_handler *sh;
2381 if ((sh = find_sh(opcode, version, NULL)) == NULL) {
2383 printf("ipfw: ipfw_ctl3 invalid option %d""v""%d\n",
2389 /* Copy handler data to requested buffer */
2397 find_unref_sh(struct ipfw_sopt_handler *psh)
2399 struct ipfw_sopt_handler *sh;
2402 sh = find_sh(psh->opcode, psh->version, NULL);
2403 KASSERT(sh != NULL, ("ctl3 handler disappeared"));
2410 ipfw_init_sopt_handler()
2414 IPFW_ADD_SOPT_HANDLER(1, scodes);
2418 ipfw_destroy_sopt_handler()
2421 IPFW_DEL_SOPT_HANDLER(1, scodes);
2422 CTL3_LOCK_DESTROY();
2426 * Adds one or more sockopt handlers to the global array.
2427 * Function may sleep.
2430 ipfw_add_sopt_handler(struct ipfw_sopt_handler *sh, size_t count)
2433 struct ipfw_sopt_handler *tmp;
2438 sz = ctl3_hsize + count;
2440 tmp = malloc(sizeof(*sh) * sz, M_IPFW, M_WAITOK | M_ZERO);
2442 if (ctl3_hsize + count <= sz)
2449 /* Merge old & new arrays */
2450 sz = ctl3_hsize + count;
2451 memcpy(tmp, ctl3_handlers, ctl3_hsize * sizeof(*sh));
2452 memcpy(&tmp[ctl3_hsize], sh, count * sizeof(*sh));
2453 qsort(tmp, sz, sizeof(*sh), compare_sh);
2454 /* Switch new and free old */
2455 if (ctl3_handlers != NULL)
2456 free(ctl3_handlers, M_IPFW);
2457 ctl3_handlers = tmp;
2465 * Removes one or more sockopt handlers from the global array.
2468 ipfw_del_sopt_handler(struct ipfw_sopt_handler *sh, size_t count)
2471 struct ipfw_sopt_handler *tmp, *h;
2476 for (i = 0; i < count; i++) {
2478 h = find_sh(tmp->opcode, tmp->version, tmp->handler);
2482 sz = (ctl3_handlers + ctl3_hsize - (h + 1)) * sizeof(*h);
2483 memmove(h, h + 1, sz);
2487 if (ctl3_hsize == 0) {
2488 if (ctl3_handlers != NULL)
2489 free(ctl3_handlers, M_IPFW);
2490 ctl3_handlers = NULL;
2501 * Writes data accumulated in @sd to sockopt buffer.
2502 * Zeroes internal @sd buffer.
2505 ipfw_flush_sopt_data(struct sockopt_data *sd)
2507 struct sockopt *sopt;
2517 if (sopt->sopt_dir == SOPT_GET) {
2518 error = copyout(sd->kbuf, sopt->sopt_val, sz);
2523 memset(sd->kbuf, 0, sd->ksize);
2526 if (sd->ktotal + sd->ksize < sd->valsize)
2527 sd->kavail = sd->ksize;
2529 sd->kavail = sd->valsize - sd->ktotal;
2531 /* Update sopt buffer data */
2532 sopt->sopt_valsize = sd->ktotal;
2533 sopt->sopt_val = sd->sopt_val + sd->ktotal;
2539 * Ensures that @sd buffer has contigious @neeeded number of
2542 * Returns pointer to requested space or NULL.
2545 ipfw_get_sopt_space(struct sockopt_data *sd, size_t needed)
2550 if (sd->kavail < needed) {
2552 * Flush data and try another time.
2554 error = ipfw_flush_sopt_data(sd);
2556 if (sd->kavail < needed || error != 0)
2560 addr = sd->kbuf + sd->koff;
2562 sd->kavail -= needed;
2567 * Requests @needed contigious bytes from @sd buffer.
2568 * Function is used to notify subsystem that we are
2569 * interesed in first @needed bytes (request header)
2570 * and the rest buffer can be safely zeroed.
2572 * Returns pointer to requested space or NULL.
2575 ipfw_get_sopt_header(struct sockopt_data *sd, size_t needed)
2579 if ((addr = ipfw_get_sopt_space(sd, needed)) == NULL)
2583 memset(sd->kbuf + sd->koff, 0, sd->kavail);
2589 * New sockopt handler.
2592 ipfw_ctl3(struct sockopt *sopt)
2595 size_t size, valsize;
2596 struct ip_fw_chain *chain;
2598 struct sockopt_data sdata;
2599 struct ipfw_sopt_handler h;
2600 ip_fw3_opheader *op3 = NULL;
2602 error = priv_check(sopt->sopt_td, PRIV_NETINET_IPFW);
2606 if (sopt->sopt_name != IP_FW3)
2607 return (ipfw_ctl(sopt));
2609 chain = &V_layer3_chain;
2612 /* Save original valsize before it is altered via sooptcopyin() */
2613 valsize = sopt->sopt_valsize;
2614 memset(&sdata, 0, sizeof(sdata));
2615 /* Read op3 header first to determine actual operation */
2616 op3 = (ip_fw3_opheader *)xbuf;
2617 error = sooptcopyin(sopt, op3, sizeof(*op3), sizeof(*op3));
2620 sopt->sopt_valsize = valsize;
2623 * Find and reference command.
2625 error = find_ref_sh(op3->opcode, op3->version, &h);
2630 * Disallow modifications in really-really secure mode, but still allow
2631 * the logging counters to be reset.
2633 if ((h.dir & HDIR_SET) != 0 && h.opcode != IP_FW_XRESETLOG) {
2634 error = securelevel_ge(sopt->sopt_td->td_ucred, 3);
2642 * Fill in sockopt_data structure that may be useful for
2643 * IP_FW3 get requests.
2646 if (valsize <= sizeof(xbuf)) {
2647 /* use on-stack buffer */
2649 sdata.ksize = sizeof(xbuf);
2650 sdata.kavail = valsize;
2654 * Determine opcode type/buffer size:
2655 * allocate sliding-window buf for data export or
2656 * contigious buffer for special ops.
2658 if ((h.dir & HDIR_SET) != 0) {
2659 /* Set request. Allocate contigous buffer. */
2660 if (valsize > CTL3_LARGEBUF) {
2667 /* Get request. Allocate sliding window buffer */
2668 size = (valsize<CTL3_SMALLBUF) ? valsize:CTL3_SMALLBUF;
2670 if (size < valsize) {
2671 /* We have to wire user buffer */
2672 error = vslock(sopt->sopt_val, valsize);
2679 sdata.kbuf = malloc(size, M_TEMP, M_WAITOK | M_ZERO);
2681 sdata.kavail = size;
2685 sdata.sopt_val = sopt->sopt_val;
2686 sdata.valsize = valsize;
2689 * Copy either all request (if valsize < bsize_max)
2690 * or first bsize_max bytes to guarantee most consumers
2691 * that all necessary data has been copied).
2692 * Anyway, copy not less than sizeof(ip_fw3_opheader).
2694 if ((error = sooptcopyin(sopt, sdata.kbuf, sdata.ksize,
2695 sizeof(ip_fw3_opheader))) != 0)
2697 op3 = (ip_fw3_opheader *)sdata.kbuf;
2699 /* Finally, run handler */
2700 error = h.handler(chain, op3, &sdata);
2703 /* Flush state and free buffers */
2705 error = ipfw_flush_sopt_data(&sdata);
2707 ipfw_flush_sopt_data(&sdata);
2710 vsunlock(sdata.sopt_val, valsize);
2712 /* Restore original pointer and set number of bytes written */
2713 sopt->sopt_val = sdata.sopt_val;
2714 sopt->sopt_valsize = sdata.ktotal;
2715 if (sdata.kbuf != xbuf)
2716 free(sdata.kbuf, M_TEMP);
2722 * {set|get}sockopt parser.
2725 ipfw_ctl(struct sockopt *sopt)
2727 #define RULE_MAXSIZE (512*sizeof(u_int32_t))
2729 size_t size, valsize;
2731 struct ip_fw_rule0 *rule;
2732 struct ip_fw_chain *chain;
2733 u_int32_t rulenum[2];
2735 struct rule_check_info ci;
2738 chain = &V_layer3_chain;
2741 /* Save original valsize before it is altered via sooptcopyin() */
2742 valsize = sopt->sopt_valsize;
2743 opt = sopt->sopt_name;
2746 * Disallow modifications in really-really secure mode, but still allow
2747 * the logging counters to be reset.
2749 if (opt == IP_FW_ADD ||
2750 (sopt->sopt_dir == SOPT_SET && opt != IP_FW_RESETLOG)) {
2751 error = securelevel_ge(sopt->sopt_td->td_ucred, 3);
2759 * pass up a copy of the current rules. Static rules
2760 * come first (the last of which has number IPFW_DEFAULT_RULE),
2761 * followed by a possibly empty list of dynamic rule.
2762 * The last dynamic rule has NULL in the "next" field.
2764 * Note that the calculated size is used to bound the
2765 * amount of data returned to the user. The rule set may
2766 * change between calculating the size and returning the
2767 * data in which case we'll just return what fits.
2772 size = chain->static_len;
2773 size += ipfw_dyn_len();
2774 if (size >= sopt->sopt_valsize)
2776 buf = malloc(size, M_TEMP, M_WAITOK | M_ZERO);
2777 IPFW_UH_RLOCK(chain);
2778 /* check again how much space we need */
2779 want = chain->static_len + ipfw_dyn_len();
2781 len = ipfw_getrules(chain, buf, size);
2782 IPFW_UH_RUNLOCK(chain);
2784 error = sooptcopyout(sopt, buf, len);
2792 /* locking is done within del_entry() */
2793 error = del_entry(chain, 0); /* special case, rule=0, cmd=0 means all */
2797 rule = malloc(RULE_MAXSIZE, M_TEMP, M_WAITOK);
2798 error = sooptcopyin(sopt, rule, RULE_MAXSIZE,
2799 sizeof(struct ip_fw7) );
2801 memset(&ci, 0, sizeof(struct rule_check_info));
2804 * If the size of commands equals RULESIZE7 then we assume
2805 * a FreeBSD7.2 binary is talking to us (set is7=1).
2806 * is7 is persistent so the next 'ipfw list' command
2807 * will use this format.
2808 * NOTE: If wrong version is guessed (this can happen if
2809 * the first ipfw command is 'ipfw [pipe] list')
2810 * the ipfw binary may crash or loop infinitly...
2812 size = sopt->sopt_valsize;
2813 if (size == RULESIZE7(rule)) {
2815 error = convert_rule_to_8(rule);
2820 size = RULESIZE(rule);
2824 error = check_ipfw_rule0(rule, size, &ci);
2826 /* locking is done within add_rule() */
2827 struct ip_fw *krule;
2828 krule = ipfw_alloc_rule(chain, RULEKSIZE0(rule));
2829 ci.urule = (caddr_t)rule;
2832 error = commit_rules(chain, &ci, 1);
2833 if (!error && sopt->sopt_dir == SOPT_GET) {
2835 error = convert_rule_to_7(rule);
2836 size = RULESIZE7(rule);
2842 error = sooptcopyout(sopt, rule, size);
2850 * IP_FW_DEL is used for deleting single rules or sets,
2851 * and (ab)used to atomically manipulate sets. Argument size
2852 * is used to distinguish between the two:
2854 * delete single rule or set of rules,
2855 * or reassign rules (or sets) to a different set.
2856 * 2*sizeof(u_int32_t)
2857 * atomic disable/enable sets.
2858 * first u_int32_t contains sets to be disabled,
2859 * second u_int32_t contains sets to be enabled.
2861 error = sooptcopyin(sopt, rulenum,
2862 2*sizeof(u_int32_t), sizeof(u_int32_t));
2865 size = sopt->sopt_valsize;
2866 if (size == sizeof(u_int32_t) && rulenum[0] != 0) {
2867 /* delete or reassign, locking done in del_entry() */
2868 error = del_entry(chain, rulenum[0]);
2869 } else if (size == 2*sizeof(u_int32_t)) { /* set enable/disable */
2870 IPFW_UH_WLOCK(chain);
2872 (V_set_disable | rulenum[0]) & ~rulenum[1] &
2873 ~(1<<RESVD_SET); /* set RESVD_SET always enabled */
2874 IPFW_UH_WUNLOCK(chain);
2880 case IP_FW_RESETLOG: /* argument is an u_int_32, the rule number */
2882 if (sopt->sopt_val != 0) {
2883 error = sooptcopyin(sopt, rulenum,
2884 sizeof(u_int32_t), sizeof(u_int32_t));
2888 error = zero_entry(chain, rulenum[0],
2889 sopt->sopt_name == IP_FW_RESETLOG);
2892 /*--- TABLE opcodes ---*/
2893 case IP_FW_TABLE_ADD:
2894 case IP_FW_TABLE_DEL:
2896 ipfw_table_entry ent;
2897 struct tentry_info tei;
2899 struct table_value v;
2901 error = sooptcopyin(sopt, &ent,
2902 sizeof(ent), sizeof(ent));
2906 memset(&tei, 0, sizeof(tei));
2907 tei.paddr = &ent.addr;
2908 tei.subtype = AF_INET;
2909 tei.masklen = ent.masklen;
2910 ipfw_import_table_value_legacy(ent.value, &v);
2912 memset(&ti, 0, sizeof(ti));
2914 ti.type = IPFW_TABLE_CIDR;
2916 error = (opt == IP_FW_TABLE_ADD) ?
2917 add_table_entry(chain, &ti, &tei, 0, 1) :
2918 del_table_entry(chain, &ti, &tei, 0, 1);
2923 case IP_FW_TABLE_FLUSH:
2928 error = sooptcopyin(sopt, &tbl,
2929 sizeof(tbl), sizeof(tbl));
2932 memset(&ti, 0, sizeof(ti));
2934 error = flush_table(chain, &ti);
2938 case IP_FW_TABLE_GETSIZE:
2943 if ((error = sooptcopyin(sopt, &tbl, sizeof(tbl),
2946 memset(&ti, 0, sizeof(ti));
2949 error = ipfw_count_table(chain, &ti, &cnt);
2950 IPFW_RUNLOCK(chain);
2953 error = sooptcopyout(sopt, &cnt, sizeof(cnt));
2957 case IP_FW_TABLE_LIST:
2962 if (sopt->sopt_valsize < sizeof(*tbl)) {
2966 size = sopt->sopt_valsize;
2967 tbl = malloc(size, M_TEMP, M_WAITOK);
2968 error = sooptcopyin(sopt, tbl, size, sizeof(*tbl));
2973 tbl->size = (size - sizeof(*tbl)) /
2974 sizeof(ipfw_table_entry);
2975 memset(&ti, 0, sizeof(ti));
2978 error = ipfw_dump_table_legacy(chain, &ti, tbl);
2979 IPFW_RUNLOCK(chain);
2984 error = sooptcopyout(sopt, tbl, size);
2989 /*--- NAT operations are protected by the IPFW_LOCK ---*/
2991 if (IPFW_NAT_LOADED)
2992 error = ipfw_nat_cfg_ptr(sopt);
2994 printf("IP_FW_NAT_CFG: %s\n",
2995 "ipfw_nat not present, please load it");
3001 if (IPFW_NAT_LOADED)
3002 error = ipfw_nat_del_ptr(sopt);
3004 printf("IP_FW_NAT_DEL: %s\n",
3005 "ipfw_nat not present, please load it");
3010 case IP_FW_NAT_GET_CONFIG:
3011 if (IPFW_NAT_LOADED)
3012 error = ipfw_nat_get_cfg_ptr(sopt);
3014 printf("IP_FW_NAT_GET_CFG: %s\n",
3015 "ipfw_nat not present, please load it");
3020 case IP_FW_NAT_GET_LOG:
3021 if (IPFW_NAT_LOADED)
3022 error = ipfw_nat_get_log_ptr(sopt);
3024 printf("IP_FW_NAT_GET_LOG: %s\n",
3025 "ipfw_nat not present, please load it");
3031 printf("ipfw: ipfw_ctl invalid option %d\n", sopt->sopt_name);
3038 #define RULE_MAXSIZE (256*sizeof(u_int32_t))
3040 /* Functions to convert rules 7.2 <==> 8.0 */
3042 convert_rule_to_7(struct ip_fw_rule0 *rule)
3044 /* Used to modify original rule */
3045 struct ip_fw7 *rule7 = (struct ip_fw7 *)rule;
3046 /* copy of original rule, version 8 */
3047 struct ip_fw_rule0 *tmp;
3049 /* Used to copy commands */
3050 ipfw_insn *ccmd, *dst;
3051 int ll = 0, ccmdlen = 0;
3053 tmp = malloc(RULE_MAXSIZE, M_TEMP, M_NOWAIT | M_ZERO);
3055 return 1; //XXX error
3057 bcopy(rule, tmp, RULE_MAXSIZE);
3060 //rule7->_pad = tmp->_pad;
3061 rule7->set = tmp->set;
3062 rule7->rulenum = tmp->rulenum;
3063 rule7->cmd_len = tmp->cmd_len;
3064 rule7->act_ofs = tmp->act_ofs;
3065 rule7->next_rule = (struct ip_fw7 *)tmp->next_rule;
3066 rule7->cmd_len = tmp->cmd_len;
3067 rule7->pcnt = tmp->pcnt;
3068 rule7->bcnt = tmp->bcnt;
3069 rule7->timestamp = tmp->timestamp;
3072 for (ll = tmp->cmd_len, ccmd = tmp->cmd, dst = rule7->cmd ;
3073 ll > 0 ; ll -= ccmdlen, ccmd += ccmdlen, dst += ccmdlen) {
3074 ccmdlen = F_LEN(ccmd);
3076 bcopy(ccmd, dst, F_LEN(ccmd)*sizeof(uint32_t));
3078 if (dst->opcode > O_NAT)
3079 /* O_REASS doesn't exists in 7.2 version, so
3080 * decrement opcode if it is after O_REASS
3085 printf("ipfw: opcode %d size truncated\n",
3096 convert_rule_to_8(struct ip_fw_rule0 *rule)
3098 /* Used to modify original rule */
3099 struct ip_fw7 *rule7 = (struct ip_fw7 *) rule;
3101 /* Used to copy commands */
3102 ipfw_insn *ccmd, *dst;
3103 int ll = 0, ccmdlen = 0;
3105 /* Copy of original rule */
3106 struct ip_fw7 *tmp = malloc(RULE_MAXSIZE, M_TEMP, M_NOWAIT | M_ZERO);
3108 return 1; //XXX error
3111 bcopy(rule7, tmp, RULE_MAXSIZE);
3113 for (ll = tmp->cmd_len, ccmd = tmp->cmd, dst = rule->cmd ;
3114 ll > 0 ; ll -= ccmdlen, ccmd += ccmdlen, dst += ccmdlen) {
3115 ccmdlen = F_LEN(ccmd);
3117 bcopy(ccmd, dst, F_LEN(ccmd)*sizeof(uint32_t));
3119 if (dst->opcode > O_NAT)
3120 /* O_REASS doesn't exists in 7.2 version, so
3121 * increment opcode if it is after O_REASS
3126 printf("ipfw: opcode %d size truncated\n",
3132 rule->_pad = tmp->_pad;
3133 rule->set = tmp->set;
3134 rule->rulenum = tmp->rulenum;
3135 rule->cmd_len = tmp->cmd_len;
3136 rule->act_ofs = tmp->act_ofs;
3137 rule->next_rule = (struct ip_fw *)tmp->next_rule;
3138 rule->cmd_len = tmp->cmd_len;
3139 rule->id = 0; /* XXX see if is ok = 0 */
3140 rule->pcnt = tmp->pcnt;
3141 rule->bcnt = tmp->bcnt;
3142 rule->timestamp = tmp->timestamp;
3154 * Allocate new bitmask which can be used to enlarge/shrink
3155 * named instance index.
3158 ipfw_objhash_bitmap_alloc(uint32_t items, void **idx, int *pblocks)
3164 KASSERT((items % BLOCK_ITEMS) == 0,
3165 ("bitmask size needs to power of 2 and greater or equal to %zu",
3168 max_blocks = items / BLOCK_ITEMS;
3170 idx_mask = malloc(size * IPFW_MAX_SETS, M_IPFW, M_WAITOK);
3171 /* Mark all as free */
3172 memset(idx_mask, 0xFF, size * IPFW_MAX_SETS);
3173 *idx_mask &= ~(u_long)1; /* Skip index 0 */
3176 *pblocks = max_blocks;
3180 * Copy current bitmask index to new one.
3183 ipfw_objhash_bitmap_merge(struct namedobj_instance *ni, void **idx, int *blocks)
3185 int old_blocks, new_blocks;
3186 u_long *old_idx, *new_idx;
3189 old_idx = ni->idx_mask;
3190 old_blocks = ni->max_blocks;
3192 new_blocks = *blocks;
3194 for (i = 0; i < IPFW_MAX_SETS; i++) {
3195 memcpy(&new_idx[new_blocks * i], &old_idx[old_blocks * i],
3196 old_blocks * sizeof(u_long));
3201 * Swaps current @ni index with new one.
3204 ipfw_objhash_bitmap_swap(struct namedobj_instance *ni, void **idx, int *blocks)
3209 old_idx = ni->idx_mask;
3210 old_blocks = ni->max_blocks;
3212 ni->idx_mask = *idx;
3213 ni->max_blocks = *blocks;
3215 /* Save old values */
3217 *blocks = old_blocks;
3221 ipfw_objhash_bitmap_free(void *idx, int blocks)
3228 * Creates named hash instance.
3229 * Must be called without holding any locks.
3230 * Return pointer to new instance.
3232 struct namedobj_instance *
3233 ipfw_objhash_create(uint32_t items)
3235 struct namedobj_instance *ni;
3239 size = sizeof(struct namedobj_instance) +
3240 sizeof(struct namedobjects_head) * NAMEDOBJ_HASH_SIZE +
3241 sizeof(struct namedobjects_head) * NAMEDOBJ_HASH_SIZE;
3243 ni = malloc(size, M_IPFW, M_WAITOK | M_ZERO);
3244 ni->nn_size = NAMEDOBJ_HASH_SIZE;
3245 ni->nv_size = NAMEDOBJ_HASH_SIZE;
3247 ni->names = (struct namedobjects_head *)(ni +1);
3248 ni->values = &ni->names[ni->nn_size];
3250 for (i = 0; i < ni->nn_size; i++)
3251 TAILQ_INIT(&ni->names[i]);
3253 for (i = 0; i < ni->nv_size; i++)
3254 TAILQ_INIT(&ni->values[i]);
3256 /* Set default hashing/comparison functions */
3257 ni->hash_f = objhash_hash_name;
3258 ni->cmp_f = objhash_cmp_name;
3260 /* Allocate bitmask separately due to possible resize */
3261 ipfw_objhash_bitmap_alloc(items, (void*)&ni->idx_mask, &ni->max_blocks);
3267 ipfw_objhash_destroy(struct namedobj_instance *ni)
3270 free(ni->idx_mask, M_IPFW);
3275 ipfw_objhash_set_funcs(struct namedobj_instance *ni, objhash_hash_f *hash_f,
3276 objhash_cmp_f *cmp_f)
3279 ni->hash_f = hash_f;
3284 objhash_hash_name(struct namedobj_instance *ni, void *name, uint32_t set)
3287 return (fnv_32_str((char *)name, FNV1_32_INIT));
3291 objhash_cmp_name(struct named_object *no, void *name, uint32_t set)
3294 if ((strcmp(no->name, (char *)name) == 0) && (no->set == set))
3301 objhash_hash_idx(struct namedobj_instance *ni, uint32_t val)
3305 v = val % (ni->nv_size - 1);
3310 struct named_object *
3311 ipfw_objhash_lookup_name(struct namedobj_instance *ni, uint32_t set, char *name)
3313 struct named_object *no;
3316 hash = ni->hash_f(ni, name, set) % ni->nn_size;
3318 TAILQ_FOREACH(no, &ni->names[hash], nn_next) {
3319 if (ni->cmp_f(no, name, set) == 0)
3326 struct named_object *
3327 ipfw_objhash_lookup_kidx(struct namedobj_instance *ni, uint16_t kidx)
3329 struct named_object *no;
3332 hash = objhash_hash_idx(ni, kidx);
3334 TAILQ_FOREACH(no, &ni->values[hash], nv_next) {
3335 if (no->kidx == kidx)
3343 ipfw_objhash_same_name(struct namedobj_instance *ni, struct named_object *a,
3344 struct named_object *b)
3347 if ((strcmp(a->name, b->name) == 0) && a->set == b->set)
3354 ipfw_objhash_add(struct namedobj_instance *ni, struct named_object *no)
3358 hash = ni->hash_f(ni, no->name, no->set) % ni->nn_size;
3359 TAILQ_INSERT_HEAD(&ni->names[hash], no, nn_next);
3361 hash = objhash_hash_idx(ni, no->kidx);
3362 TAILQ_INSERT_HEAD(&ni->values[hash], no, nv_next);
3368 ipfw_objhash_del(struct namedobj_instance *ni, struct named_object *no)
3372 hash = ni->hash_f(ni, no->name, no->set) % ni->nn_size;
3373 TAILQ_REMOVE(&ni->names[hash], no, nn_next);
3375 hash = objhash_hash_idx(ni, no->kidx);
3376 TAILQ_REMOVE(&ni->values[hash], no, nv_next);
3382 ipfw_objhash_count(struct namedobj_instance *ni)
3389 * Runs @func for each found named object.
3390 * It is safe to delete objects from callback
3393 ipfw_objhash_foreach(struct namedobj_instance *ni, objhash_cb_t *f, void *arg)
3395 struct named_object *no, *no_tmp;
3398 for (i = 0; i < ni->nn_size; i++) {
3399 TAILQ_FOREACH_SAFE(no, &ni->names[i], nn_next, no_tmp)
3405 * Removes index from given set.
3406 * Returns 0 on success.
3409 ipfw_objhash_free_idx(struct namedobj_instance *ni, uint16_t idx)
3414 i = idx / BLOCK_ITEMS;
3415 v = idx % BLOCK_ITEMS;
3417 if (i >= ni->max_blocks)
3420 mask = &ni->idx_mask[i];
3422 if ((*mask & ((u_long)1 << v)) != 0)
3426 *mask |= (u_long)1 << v;
3428 /* Update free offset */
3429 if (ni->free_off[0] > i)
3430 ni->free_off[0] = i;
3436 * Allocate new index in given instance and stores in in @pidx.
3437 * Returns 0 on success.
3440 ipfw_objhash_alloc_idx(void *n, uint16_t *pidx)
3442 struct namedobj_instance *ni;
3446 ni = (struct namedobj_instance *)n;
3448 off = ni->free_off[0];
3449 mask = &ni->idx_mask[off];
3451 for (i = off; i < ni->max_blocks; i++, mask++) {
3452 if ((v = ffsl(*mask)) == 0)
3456 *mask &= ~ ((u_long)1 << (v - 1));
3458 ni->free_off[0] = i;
3460 v = BLOCK_ITEMS * i + v - 1;