2 * Copyright (c) 2002-2009 Luigi Rizzo, Universita` di Pisa
4 * Supported by: Valeria Paoli
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 * Sockopt support for ipfw. The routines here implement
33 * the upper half of the ipfw code.
37 #if !defined(KLD_MODULE)
38 #include "opt_ipdivert.h"
42 #error IPFIREWALL requires INET.
45 #include "opt_inet6.h"
46 #include "opt_ipsec.h"
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/malloc.h>
51 #include <sys/mbuf.h> /* struct m_tag used by nested headers */
52 #include <sys/kernel.h>
56 #include <sys/rwlock.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
59 #include <sys/sysctl.h>
60 #include <sys/syslog.h>
62 #include <net/route.h>
65 #include <netinet/in.h>
66 #include <netinet/ip_var.h> /* hooks */
67 #include <netinet/ip_fw.h>
68 #include <netinet/ipfw/ip_fw_private.h>
71 #include <security/mac/mac_framework.h>
74 MALLOC_DEFINE(M_IPFW, "IpFw/IpAcct", "IpFw/IpAcct chain's");
77 * static variables followed by global ones (none in this file)
81 * Find the smallest rule >= key, id.
82 * We could use bsearch but it is so simple that we code it directly
85 ipfw_find_rule(struct ip_fw_chain *chain, uint32_t key, uint32_t id)
90 for (lo = 0, hi = chain->n_rules - 1; lo < hi;) {
94 lo = i + 1; /* continue from the next one */
95 else if (r->rulenum > key)
96 hi = i; /* this might be good */
98 lo = i + 1; /* continue from the next one */
99 else /* r->id >= id */
100 hi = i; /* this might be good */
106 * allocate a new map, returns the chain locked. extra is the number
107 * of entries to add or delete.
109 static struct ip_fw **
110 get_map(struct ip_fw_chain *chain, int extra, int locked)
117 i = chain->n_rules + extra;
118 map = malloc(i * sizeof(struct ip_fw *), M_IPFW,
119 locked ? M_NOWAIT : M_WAITOK);
121 printf("%s: cannot allocate map\n", __FUNCTION__);
125 IPFW_UH_WLOCK(chain);
126 if (i >= chain->n_rules + extra) /* good */
128 /* otherwise we lost the race, free and retry */
130 IPFW_UH_WUNLOCK(chain);
136 * swap the maps. It is supposed to be called with IPFW_UH_WLOCK
138 static struct ip_fw **
139 swap_map(struct ip_fw_chain *chain, struct ip_fw **new_map, int new_len)
141 struct ip_fw **old_map;
145 chain->n_rules = new_len;
146 old_map = chain->map;
147 chain->map = new_map;
153 * Add a new rule to the list. Copy the rule into a malloc'ed area, then
154 * possibly create a rule number and add the rule to the list.
155 * Update the rule_number in the input struct so the caller knows it as well.
156 * XXX DO NOT USE FOR THE DEFAULT RULE.
157 * Must be called without IPFW_UH held
160 ipfw_add_rule(struct ip_fw_chain *chain, struct ip_fw *input_rule)
163 int i, l, insert_before;
164 struct ip_fw **map; /* the new array of pointers */
166 if (chain->rules == NULL || input_rule->rulenum > IPFW_DEFAULT_RULE-1)
169 l = RULESIZE(input_rule);
170 rule = malloc(l, M_IPFW, M_WAITOK | M_ZERO);
173 /* get_map returns with IPFW_UH_WLOCK if successful */
174 map = get_map(chain, 1, 0 /* not locked */);
180 bcopy(input_rule, rule, l);
181 /* clear fields not settable from userland */
183 rule->next_rule = NULL;
188 if (V_autoinc_step < 1)
190 else if (V_autoinc_step > 1000)
191 V_autoinc_step = 1000;
192 /* find the insertion point, we will insert before */
193 insert_before = rule->rulenum ? rule->rulenum + 1 : IPFW_DEFAULT_RULE;
194 i = ipfw_find_rule(chain, insert_before, 0);
195 /* duplicate first part */
197 bcopy(chain->map, map, i * sizeof(struct ip_fw *));
199 /* duplicate remaining part, we always have the default rule */
200 bcopy(chain->map + i, map + i + 1,
201 sizeof(struct ip_fw *) *(chain->n_rules - i));
202 if (rule->rulenum == 0) {
203 /* write back the number */
204 rule->rulenum = i > 0 ? map[i-1]->rulenum : 0;
205 if (rule->rulenum < IPFW_DEFAULT_RULE - V_autoinc_step)
206 rule->rulenum += V_autoinc_step;
207 input_rule->rulenum = rule->rulenum;
210 rule->id = chain->id + 1;
211 map = swap_map(chain, map, chain->n_rules + 1);
212 chain->static_len += l;
213 IPFW_UH_WUNLOCK(chain);
220 * Reclaim storage associated with a list of rules. This is
221 * typically the list created using remove_rule.
222 * A NULL pointer on input is handled correctly.
225 ipfw_reap_rules(struct ip_fw *head)
229 while ((rule = head) != NULL) {
236 * Used by del_entry() to check if a rule should be kept.
237 * Returns 1 if the rule must be kept, 0 otherwise.
239 * Called with cmd = {0,1,5}.
240 * cmd == 0 matches on rule numbers, excludes rules in RESVD_SET if n == 0 ;
241 * cmd == 1 matches on set numbers only, rule numbers are ignored;
242 * cmd == 5 matches on rule and set numbers.
244 * n == 0 is a wildcard for rule numbers, there is no wildcard for sets.
247 * (default || reserved || !match_set || !match_number)
249 * default ::= (rule->rulenum == IPFW_DEFAULT_RULE)
250 * // the default rule is always protected
252 * reserved ::= (cmd == 0 && n == 0 && rule->set == RESVD_SET)
253 * // RESVD_SET is protected only if cmd == 0 and n == 0 ("ipfw flush")
255 * match_set ::= (cmd == 0 || rule->set == set)
256 * // set number is ignored for cmd == 0
258 * match_number ::= (cmd == 1 || n == 0 || n == rule->rulenum)
259 * // number is ignored for cmd == 1 or n == 0
263 keep_rule(struct ip_fw *rule, uint8_t cmd, uint8_t set, uint32_t n)
266 (rule->rulenum == IPFW_DEFAULT_RULE) ||
267 (cmd == 0 && n == 0 && rule->set == RESVD_SET) ||
268 !(cmd == 0 || rule->set == set) ||
269 !(cmd == 1 || n == 0 || n == rule->rulenum);
273 * Remove all rules with given number, or do set manipulation.
274 * Assumes chain != NULL && *chain != NULL.
276 * The argument is an uint32_t. The low 16 bit are the rule or set number;
277 * the next 8 bits are the new set; the top 8 bits indicate the command:
279 * 0 delete rules numbered "rulenum"
280 * 1 delete rules in set "rulenum"
281 * 2 move rules "rulenum" to set "new_set"
282 * 3 move rules from set "rulenum" to set "new_set"
283 * 4 swap sets "rulenum" and "new_set"
284 * 5 delete rules "rulenum" and set "new_set"
287 del_entry(struct ip_fw_chain *chain, uint32_t arg)
290 uint32_t num; /* rule number or old_set */
291 uint8_t cmd, new_set;
292 int start, end, i, ofs, n;
293 struct ip_fw **map = NULL;
297 cmd = (arg >> 24) & 0xff;
298 new_set = (arg >> 16) & 0xff;
300 if (cmd > 5 || new_set > RESVD_SET)
302 if (cmd == 0 || cmd == 2 || cmd == 5) {
303 if (num >= IPFW_DEFAULT_RULE)
306 if (num > RESVD_SET) /* old_set */
310 IPFW_UH_WLOCK(chain); /* arbitrate writers */
311 chain->reap = NULL; /* prepare for deletions */
314 case 0: /* delete rules "num" (num == 0 matches all) */
315 case 1: /* delete all rules in set N */
316 case 5: /* delete rules with number N and set "new_set". */
319 * Locate first rule to delete (start), the rule after
320 * the last one to delete (end), and count how many
321 * rules to delete (n). Always use keep_rule() to
322 * determine which rules to keep.
326 /* look for a specific set including RESVD_SET.
327 * Must scan the entire range, ignore num.
330 for (start = -1, end = i = 0; i < chain->n_rules; i++) {
331 if (keep_rule(chain->map[i], cmd, new_set, 0))
338 end++; /* first non-matching */
340 /* Optimized search on rule numbers */
341 start = ipfw_find_rule(chain, num, 0);
342 for (end = start; end < chain->n_rules; end++) {
343 rule = chain->map[end];
344 if (num > 0 && rule->rulenum != num)
346 if (!keep_rule(rule, cmd, new_set, num))
352 /* A flush request (arg == 0 or cmd == 1) on empty
353 * ruleset returns with no error. On the contrary,
354 * if there is no match on a specific request,
357 if (arg != 0 && cmd != 1)
362 /* We have something to delete. Allocate the new map */
363 map = get_map(chain, -n, 1 /* locked */);
369 /* 1. bcopy the initial part of the map */
371 bcopy(chain->map, map, start * sizeof(struct ip_fw *));
372 /* 2. copy active rules between start and end */
373 for (i = ofs = start; i < end; i++) {
374 rule = chain->map[i];
375 if (keep_rule(rule, cmd, new_set, num))
378 /* 3. copy the final part of the map */
379 bcopy(chain->map + end, map + ofs,
380 (chain->n_rules - end) * sizeof(struct ip_fw *));
381 /* 4. swap the maps (under BH_LOCK) */
382 map = swap_map(chain, map, chain->n_rules - n);
383 /* 5. now remove the rules deleted from the old map */
384 for (i = start; i < end; i++) {
387 if (keep_rule(rule, cmd, new_set, num))
390 chain->static_len -= l;
391 ipfw_remove_dyn_children(rule);
392 rule->x_next = chain->reap;
398 * In the next 3 cases the loop stops at (n_rules - 1)
399 * because the default rule is never eligible..
402 case 2: /* move rules with given RULE number to new set */
403 for (i = 0; i < chain->n_rules - 1; i++) {
404 rule = chain->map[i];
405 if (rule->rulenum == num)
410 case 3: /* move rules with given SET number to new set */
411 for (i = 0; i < chain->n_rules - 1; i++) {
412 rule = chain->map[i];
413 if (rule->set == num)
418 case 4: /* swap two sets */
419 for (i = 0; i < chain->n_rules - 1; i++) {
420 rule = chain->map[i];
421 if (rule->set == num)
423 else if (rule->set == new_set)
431 IPFW_UH_WUNLOCK(chain);
432 ipfw_reap_rules(rule);
439 * Clear counters for a specific rule.
440 * Normally run under IPFW_UH_RLOCK, but these are idempotent ops
441 * so we only care that rules do not disappear.
444 clear_counters(struct ip_fw *rule, int log_only)
446 ipfw_insn_log *l = (ipfw_insn_log *)ACTION_PTR(rule);
449 rule->bcnt = rule->pcnt = 0;
452 if (l->o.opcode == O_LOG)
453 l->log_left = l->max_log;
457 * Reset some or all counters on firewall rules.
458 * The argument `arg' is an u_int32_t. The low 16 bit are the rule number,
459 * the next 8 bits are the set number, the top 8 bits are the command:
460 * 0 work with rules from all set's;
461 * 1 work with rules only from specified set.
462 * Specified rule number is zero if we want to clear all entries.
463 * log_only is 1 if we only want to reset logs, zero otherwise.
466 zero_entry(struct ip_fw_chain *chain, u_int32_t arg, int log_only)
472 uint16_t rulenum = arg & 0xffff;
473 uint8_t set = (arg >> 16) & 0xff;
474 uint8_t cmd = (arg >> 24) & 0xff;
478 if (cmd == 1 && set > RESVD_SET)
481 IPFW_UH_RLOCK(chain);
483 V_norule_counter = 0;
484 for (i = 0; i < chain->n_rules; i++) {
485 rule = chain->map[i];
486 /* Skip rules not in our set. */
487 if (cmd == 1 && rule->set != set)
489 clear_counters(rule, log_only);
491 msg = log_only ? "All logging counts reset" :
492 "Accounting cleared";
495 for (i = 0; i < chain->n_rules; i++) {
496 rule = chain->map[i];
497 if (rule->rulenum == rulenum) {
498 if (cmd == 0 || rule->set == set)
499 clear_counters(rule, log_only);
502 if (rule->rulenum > rulenum)
505 if (!cleared) { /* we did not find any matching rules */
506 IPFW_UH_RUNLOCK(chain);
509 msg = log_only ? "logging count reset" : "cleared";
511 IPFW_UH_RUNLOCK(chain);
514 int lev = LOG_SECURITY | LOG_NOTICE;
517 log(lev, "ipfw: Entry %d %s.\n", rulenum, msg);
519 log(lev, "ipfw: %s.\n", msg);
525 * Check validity of the structure before insert.
526 * Rules are simple, so this mostly need to check rule sizes.
529 check_ipfw_struct(struct ip_fw *rule, int size)
535 if (size < sizeof(*rule)) {
536 printf("ipfw: rule too short\n");
539 /* first, check for valid size */
542 printf("ipfw: size mismatch (have %d want %d)\n", size, l);
545 if (rule->act_ofs >= rule->cmd_len) {
546 printf("ipfw: bogus action offset (%u > %u)\n",
547 rule->act_ofs, rule->cmd_len - 1);
551 * Now go for the individual checks. Very simple ones, basically only
554 for (l = rule->cmd_len, cmd = rule->cmd ;
555 l > 0 ; l -= cmdlen, cmd += cmdlen) {
558 printf("ipfw: opcode %d size truncated\n",
562 switch (cmd->opcode) {
593 if (cmdlen != F_INSN_SIZE(ipfw_insn))
598 if (cmdlen != F_INSN_SIZE(ipfw_insn))
600 if (cmd->arg1 >= rt_numfibs) {
601 printf("ipfw: invalid fib number %d\n",
608 if (cmdlen != F_INSN_SIZE(ipfw_insn))
610 if ((cmd->arg1 != IP_FW_TABLEARG) &&
611 (cmd->arg1 >= rt_numfibs)) {
612 printf("ipfw: invalid fib number %d\n",
627 if (cmdlen != F_INSN_SIZE(ipfw_insn_u32))
632 if (cmdlen != F_INSN_SIZE(ipfw_insn_limit))
637 if (cmdlen != F_INSN_SIZE(ipfw_insn_log))
640 ((ipfw_insn_log *)cmd)->log_left =
641 ((ipfw_insn_log *)cmd)->max_log;
647 /* only odd command lengths */
648 if ( !(cmdlen & 1) || cmdlen > 31)
654 if (cmd->arg1 == 0 || cmd->arg1 > 256) {
655 printf("ipfw: invalid set size %d\n",
659 if (cmdlen != F_INSN_SIZE(ipfw_insn_u32) +
664 case O_IP_SRC_LOOKUP:
665 case O_IP_DST_LOOKUP:
666 if (cmd->arg1 >= IPFW_TABLES_MAX) {
667 printf("ipfw: invalid table number %d\n",
671 if (cmdlen != F_INSN_SIZE(ipfw_insn) &&
672 cmdlen != F_INSN_SIZE(ipfw_insn_u32) + 1 &&
673 cmdlen != F_INSN_SIZE(ipfw_insn_u32))
678 if (cmdlen != F_INSN_SIZE(ipfw_insn_mac))
688 if (cmdlen < 1 || cmdlen > 31)
694 case O_IP_DSTPORT: /* XXX artificial limit, 30 port pairs */
695 if (cmdlen < 2 || cmdlen > 31)
702 if (cmdlen != F_INSN_SIZE(ipfw_insn_if))
707 if (cmdlen != F_INSN_SIZE(ipfw_insn_altq))
713 if (cmdlen != F_INSN_SIZE(ipfw_insn))
718 #ifdef IPFIREWALL_FORWARD
719 if (cmdlen != F_INSN_SIZE(ipfw_insn_sa))
728 #ifdef IPFIREWALL_FORWARD
729 if (cmdlen != F_INSN_SIZE(ipfw_insn_sa6))
739 if (ip_divert_ptr == NULL)
745 if (ng_ipfw_input_p == NULL)
750 if (!IPFW_NAT_LOADED)
752 if (cmdlen != F_INSN_SIZE(ipfw_insn_nat))
755 case O_FORWARD_MAC: /* XXX not implemented yet */
768 if (cmdlen != F_INSN_SIZE(ipfw_insn))
772 printf("ipfw: opcode %d, multiple actions"
779 printf("ipfw: opcode %d, action must be"
788 if (cmdlen != F_INSN_SIZE(struct in6_addr) +
789 F_INSN_SIZE(ipfw_insn))
794 if (cmdlen != F_INSN_SIZE(ipfw_insn_u32) +
795 ((ipfw_insn_u32 *)cmd)->o.arg1)
801 if ( !(cmdlen & 1) || cmdlen > 127)
805 if( cmdlen != F_INSN_SIZE( ipfw_insn_icmp6 ) )
811 switch (cmd->opcode) {
824 printf("ipfw: no IPv6 support in kernel\n");
825 return EPROTONOSUPPORT;
828 printf("ipfw: opcode %d, unknown opcode\n",
834 if (have_action == 0) {
835 printf("ipfw: missing action\n");
841 printf("ipfw: opcode %d size %d wrong\n",
842 cmd->opcode, cmdlen);
848 * Translation of requests for compatibility with FreeBSD 7.2/8.
849 * a static variable tells us if we have an old client from userland,
850 * and if necessary we translate requests and responses between the
856 struct ip_fw7 *next; /* linked list of rules */
857 struct ip_fw7 *next_rule; /* ptr to next [skipto] rule */
858 /* 'next_rule' is used to pass up 'set_disable' status */
860 uint16_t act_ofs; /* offset of action in 32-bit units */
861 uint16_t cmd_len; /* # of 32-bit words in cmd */
862 uint16_t rulenum; /* rule number */
863 uint8_t set; /* rule set (0..31) */
864 // #define RESVD_SET 31 /* set for default and persistent rules */
865 uint8_t _pad; /* padding */
866 // uint32_t id; /* rule id, only in v.8 */
867 /* These fields are present in all rules. */
868 uint64_t pcnt; /* Packet counter */
869 uint64_t bcnt; /* Byte counter */
870 uint32_t timestamp; /* tv_sec of last match */
872 ipfw_insn cmd[1]; /* storage for commands */
875 int convert_rule_to_7(struct ip_fw *rule);
876 int convert_rule_to_8(struct ip_fw *rule);
879 #define RULESIZE7(rule) (sizeof(struct ip_fw7) + \
880 ((struct ip_fw7 *)(rule))->cmd_len * 4 - 4)
885 * Copy the static and dynamic rules to the supplied buffer
886 * and return the amount of space actually used.
887 * Must be run under IPFW_UH_RLOCK
890 ipfw_getrules(struct ip_fw_chain *chain, void *buf, size_t space)
893 char *ep = bp + space;
894 struct ip_fw *rule, *dst;
898 boot_seconds = boottime.tv_sec;
899 for (i = 0; i < chain->n_rules; i++) {
900 rule = chain->map[i];
903 /* Convert rule to FreeBSd 7.2 format */
905 if (bp + l + sizeof(uint32_t) <= ep) {
907 bcopy(rule, bp, l + sizeof(uint32_t));
908 error = convert_rule_to_7((struct ip_fw *) bp);
910 return 0; /*XXX correct? */
912 * XXX HACK. Store the disable mask in the "next"
913 * pointer in a wild attempt to keep the ABI the same.
914 * Why do we do this on EVERY rule?
916 bcopy(&V_set_disable,
917 &(((struct ip_fw7 *)bp)->next_rule),
918 sizeof(V_set_disable));
919 if (((struct ip_fw7 *)bp)->timestamp)
920 ((struct ip_fw7 *)bp)->timestamp += boot_seconds;
923 continue; /* go to next rule */
926 /* normal mode, don't touch rules */
928 if (bp + l > ep) { /* should not happen */
929 printf("overflow dumping static rules\n");
932 dst = (struct ip_fw *)bp;
935 * XXX HACK. Store the disable mask in the "next"
936 * pointer in a wild attempt to keep the ABI the same.
937 * Why do we do this on EVERY rule?
939 bcopy(&V_set_disable, &dst->next_rule, sizeof(V_set_disable));
941 dst->timestamp += boot_seconds;
944 ipfw_get_dynamic(&bp, ep); /* protected by the dynamic lock */
945 return (bp - (char *)buf);
950 * {set|get}sockopt parser.
953 ipfw_ctl(struct sockopt *sopt)
955 #define RULE_MAXSIZE (256*sizeof(u_int32_t))
958 struct ip_fw *buf, *rule;
959 struct ip_fw_chain *chain;
960 u_int32_t rulenum[2];
962 error = priv_check(sopt->sopt_td, PRIV_NETINET_IPFW);
967 * Disallow modifications in really-really secure mode, but still allow
968 * the logging counters to be reset.
970 if (sopt->sopt_name == IP_FW_ADD ||
971 (sopt->sopt_dir == SOPT_SET && sopt->sopt_name != IP_FW_RESETLOG)) {
972 error = securelevel_ge(sopt->sopt_td->td_ucred, 3);
977 chain = &V_layer3_chain;
980 switch (sopt->sopt_name) {
983 * pass up a copy of the current rules. Static rules
984 * come first (the last of which has number IPFW_DEFAULT_RULE),
985 * followed by a possibly empty list of dynamic rule.
986 * The last dynamic rule has NULL in the "next" field.
988 * Note that the calculated size is used to bound the
989 * amount of data returned to the user. The rule set may
990 * change between calculating the size and returning the
991 * data in which case we'll just return what fits.
996 size = chain->static_len;
997 size += ipfw_dyn_len();
998 if (size >= sopt->sopt_valsize)
1000 buf = malloc(size, M_TEMP, M_WAITOK);
1003 IPFW_UH_RLOCK(chain);
1004 /* check again how much space we need */
1005 want = chain->static_len + ipfw_dyn_len();
1007 len = ipfw_getrules(chain, buf, size);
1008 IPFW_UH_RUNLOCK(chain);
1010 error = sooptcopyout(sopt, buf, len);
1018 /* locking is done within del_entry() */
1019 error = del_entry(chain, 0); /* special case, rule=0, cmd=0 means all */
1023 rule = malloc(RULE_MAXSIZE, M_TEMP, M_WAITOK);
1024 error = sooptcopyin(sopt, rule, RULE_MAXSIZE,
1025 sizeof(struct ip_fw7) );
1028 * If the size of commands equals RULESIZE7 then we assume
1029 * a FreeBSD7.2 binary is talking to us (set is7=1).
1030 * is7 is persistent so the next 'ipfw list' command
1031 * will use this format.
1032 * NOTE: If wrong version is guessed (this can happen if
1033 * the first ipfw command is 'ipfw [pipe] list')
1034 * the ipfw binary may crash or loop infinitly...
1036 if (sopt->sopt_valsize == RULESIZE7(rule)) {
1038 error = convert_rule_to_8(rule);
1042 error = check_ipfw_struct(rule, RULESIZE(rule));
1046 error = check_ipfw_struct(rule, sopt->sopt_valsize);
1049 /* locking is done within ipfw_add_rule() */
1050 error = ipfw_add_rule(chain, rule);
1051 size = RULESIZE(rule);
1052 if (!error && sopt->sopt_dir == SOPT_GET) {
1054 error = convert_rule_to_7(rule);
1055 size = RULESIZE7(rule);
1059 error = sooptcopyout(sopt, rule, size);
1067 * IP_FW_DEL is used for deleting single rules or sets,
1068 * and (ab)used to atomically manipulate sets. Argument size
1069 * is used to distinguish between the two:
1071 * delete single rule or set of rules,
1072 * or reassign rules (or sets) to a different set.
1073 * 2*sizeof(u_int32_t)
1074 * atomic disable/enable sets.
1075 * first u_int32_t contains sets to be disabled,
1076 * second u_int32_t contains sets to be enabled.
1078 error = sooptcopyin(sopt, rulenum,
1079 2*sizeof(u_int32_t), sizeof(u_int32_t));
1082 size = sopt->sopt_valsize;
1083 if (size == sizeof(u_int32_t) && rulenum[0] != 0) {
1084 /* delete or reassign, locking done in del_entry() */
1085 error = del_entry(chain, rulenum[0]);
1086 } else if (size == 2*sizeof(u_int32_t)) { /* set enable/disable */
1087 IPFW_UH_WLOCK(chain);
1089 (V_set_disable | rulenum[0]) & ~rulenum[1] &
1090 ~(1<<RESVD_SET); /* set RESVD_SET always enabled */
1091 IPFW_UH_WUNLOCK(chain);
1097 case IP_FW_RESETLOG: /* argument is an u_int_32, the rule number */
1099 if (sopt->sopt_val != 0) {
1100 error = sooptcopyin(sopt, rulenum,
1101 sizeof(u_int32_t), sizeof(u_int32_t));
1105 error = zero_entry(chain, rulenum[0],
1106 sopt->sopt_name == IP_FW_RESETLOG);
1109 /*--- TABLE manipulations are protected by the IPFW_LOCK ---*/
1110 case IP_FW_TABLE_ADD:
1112 ipfw_table_entry ent;
1114 error = sooptcopyin(sopt, &ent,
1115 sizeof(ent), sizeof(ent));
1118 error = ipfw_add_table_entry(chain, ent.tbl,
1119 ent.addr, ent.masklen, ent.value);
1123 case IP_FW_TABLE_DEL:
1125 ipfw_table_entry ent;
1127 error = sooptcopyin(sopt, &ent,
1128 sizeof(ent), sizeof(ent));
1131 error = ipfw_del_table_entry(chain, ent.tbl,
1132 ent.addr, ent.masklen);
1136 case IP_FW_TABLE_FLUSH:
1140 error = sooptcopyin(sopt, &tbl,
1141 sizeof(tbl), sizeof(tbl));
1145 error = ipfw_flush_table(chain, tbl);
1146 IPFW_WUNLOCK(chain);
1150 case IP_FW_TABLE_GETSIZE:
1154 if ((error = sooptcopyin(sopt, &tbl, sizeof(tbl),
1158 error = ipfw_count_table(chain, tbl, &cnt);
1159 IPFW_RUNLOCK(chain);
1162 error = sooptcopyout(sopt, &cnt, sizeof(cnt));
1166 case IP_FW_TABLE_LIST:
1170 if (sopt->sopt_valsize < sizeof(*tbl)) {
1174 size = sopt->sopt_valsize;
1175 tbl = malloc(size, M_TEMP, M_WAITOK);
1176 error = sooptcopyin(sopt, tbl, size, sizeof(*tbl));
1181 tbl->size = (size - sizeof(*tbl)) /
1182 sizeof(ipfw_table_entry);
1184 error = ipfw_dump_table(chain, tbl);
1185 IPFW_RUNLOCK(chain);
1190 error = sooptcopyout(sopt, tbl, size);
1195 /*--- NAT operations are protected by the IPFW_LOCK ---*/
1197 if (IPFW_NAT_LOADED)
1198 error = ipfw_nat_cfg_ptr(sopt);
1200 printf("IP_FW_NAT_CFG: %s\n",
1201 "ipfw_nat not present, please load it");
1207 if (IPFW_NAT_LOADED)
1208 error = ipfw_nat_del_ptr(sopt);
1210 printf("IP_FW_NAT_DEL: %s\n",
1211 "ipfw_nat not present, please load it");
1216 case IP_FW_NAT_GET_CONFIG:
1217 if (IPFW_NAT_LOADED)
1218 error = ipfw_nat_get_cfg_ptr(sopt);
1220 printf("IP_FW_NAT_GET_CFG: %s\n",
1221 "ipfw_nat not present, please load it");
1226 case IP_FW_NAT_GET_LOG:
1227 if (IPFW_NAT_LOADED)
1228 error = ipfw_nat_get_log_ptr(sopt);
1230 printf("IP_FW_NAT_GET_LOG: %s\n",
1231 "ipfw_nat not present, please load it");
1237 printf("ipfw: ipfw_ctl invalid option %d\n", sopt->sopt_name);
1246 #define RULE_MAXSIZE (256*sizeof(u_int32_t))
1248 /* Functions to convert rules 7.2 <==> 8.0 */
1250 convert_rule_to_7(struct ip_fw *rule)
1252 /* Used to modify original rule */
1253 struct ip_fw7 *rule7 = (struct ip_fw7 *)rule;
1254 /* copy of original rule, version 8 */
1257 /* Used to copy commands */
1258 ipfw_insn *ccmd, *dst;
1259 int ll = 0, ccmdlen = 0;
1261 tmp = malloc(RULE_MAXSIZE, M_TEMP, M_NOWAIT | M_ZERO);
1263 return 1; //XXX error
1265 bcopy(rule, tmp, RULE_MAXSIZE);
1268 rule7->_pad = tmp->_pad;
1269 rule7->set = tmp->set;
1270 rule7->rulenum = tmp->rulenum;
1271 rule7->cmd_len = tmp->cmd_len;
1272 rule7->act_ofs = tmp->act_ofs;
1273 rule7->next_rule = (struct ip_fw7 *)tmp->next_rule;
1274 rule7->next = (struct ip_fw7 *)tmp->x_next;
1275 rule7->cmd_len = tmp->cmd_len;
1276 rule7->pcnt = tmp->pcnt;
1277 rule7->bcnt = tmp->bcnt;
1278 rule7->timestamp = tmp->timestamp;
1281 for (ll = tmp->cmd_len, ccmd = tmp->cmd, dst = rule7->cmd ;
1282 ll > 0 ; ll -= ccmdlen, ccmd += ccmdlen, dst += ccmdlen) {
1283 ccmdlen = F_LEN(ccmd);
1285 bcopy(ccmd, dst, F_LEN(ccmd)*sizeof(uint32_t));
1287 if (dst->opcode > O_NAT)
1288 /* O_REASS doesn't exists in 7.2 version, so
1289 * decrement opcode if it is after O_REASS
1294 printf("ipfw: opcode %d size truncated\n",
1305 convert_rule_to_8(struct ip_fw *rule)
1307 /* Used to modify original rule */
1308 struct ip_fw7 *rule7 = (struct ip_fw7 *) rule;
1310 /* Used to copy commands */
1311 ipfw_insn *ccmd, *dst;
1312 int ll = 0, ccmdlen = 0;
1314 /* Copy of original rule */
1315 struct ip_fw7 *tmp = malloc(RULE_MAXSIZE, M_TEMP, M_NOWAIT | M_ZERO);
1317 return 1; //XXX error
1320 bcopy(rule7, tmp, RULE_MAXSIZE);
1322 for (ll = tmp->cmd_len, ccmd = tmp->cmd, dst = rule->cmd ;
1323 ll > 0 ; ll -= ccmdlen, ccmd += ccmdlen, dst += ccmdlen) {
1324 ccmdlen = F_LEN(ccmd);
1326 bcopy(ccmd, dst, F_LEN(ccmd)*sizeof(uint32_t));
1328 if (dst->opcode > O_NAT)
1329 /* O_REASS doesn't exists in 7.2 version, so
1330 * increment opcode if it is after O_REASS
1335 printf("ipfw: opcode %d size truncated\n",
1341 rule->_pad = tmp->_pad;
1342 rule->set = tmp->set;
1343 rule->rulenum = tmp->rulenum;
1344 rule->cmd_len = tmp->cmd_len;
1345 rule->act_ofs = tmp->act_ofs;
1346 rule->next_rule = (struct ip_fw *)tmp->next_rule;
1347 rule->x_next = (struct ip_fw *)tmp->next;
1348 rule->cmd_len = tmp->cmd_len;
1349 rule->id = 0; /* XXX see if is ok = 0 */
1350 rule->pcnt = tmp->pcnt;
1351 rule->bcnt = tmp->bcnt;
1352 rule->timestamp = tmp->timestamp;