This repo is obsolete, please see git://git.code.sf.net/p/dummynet/code@master
[ipfw.git] / dummynet2 / ip_fw_sockopt.c
1 /*-
2  * Copyright (c) 2002-2009 Luigi Rizzo, Universita` di Pisa
3  *
4  * Supported by: Valeria Paoli
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD: head/sys/netinet/ipfw/ip_fw_sockopt.c 206339 2010-04-07 08:23:58Z luigi $");
30
31 /*
32  * Sockopt support for ipfw. The routines here implement
33  * the upper half of the ipfw code.
34  */
35
36 #if !defined(KLD_MODULE)
37 #include "opt_ipfw.h"
38 #include "opt_ipdivert.h"
39 #include "opt_ipdn.h"
40 #include "opt_inet.h"
41 #ifndef INET
42 #error IPFIREWALL requires INET.
43 #endif /* INET */
44 #endif
45 #include "opt_inet6.h"
46 #include "opt_ipsec.h"
47
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>
53 #include <sys/lock.h>
54 #include <sys/priv.h>
55 #include <sys/proc.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>
61 #include <net/if.h>
62 #include <net/route.h>
63 #include <net/vnet.h>
64
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>
69
70 #ifdef MAC
71 #include <security/mac/mac_framework.h>
72 #endif
73
74 MALLOC_DEFINE(M_IPFW, "IpFw/IpAcct", "IpFw/IpAcct chain's");
75
76 /*
77  * static variables followed by global ones (none in this file)
78  */
79
80 /*
81  * Find the smallest rule >= key, id.
82  * We could use bsearch but it is so simple that we code it directly
83  */
84 int
85 ipfw_find_rule(struct ip_fw_chain *chain, uint32_t key, uint32_t id)
86 {
87         int i, lo, hi;
88         struct ip_fw *r;
89
90         for (lo = 0, hi = chain->n_rules - 1; lo < hi;) {
91                 i = (lo + hi) / 2;
92                 r = chain->map[i];
93                 if (r->rulenum < key)
94                         lo = i + 1;     /* continue from the next one */
95                 else if (r->rulenum > key)
96                         hi = i;         /* this might be good */
97                 else if (r->id < id)
98                         lo = i + 1;     /* continue from the next one */
99                 else /* r->id >= id */
100                         hi = i;         /* this might be good */
101         };
102         return hi;
103 }
104
105 /*
106  * allocate a new map, returns the chain locked. extra is the number
107  * of entries to add or delete.
108  */
109 static struct ip_fw **
110 get_map(struct ip_fw_chain *chain, int extra, int locked)
111 {
112
113         for (;;) {
114                 struct ip_fw **map;
115                 int i;
116
117                 i = chain->n_rules + extra;
118                 map = malloc(i * sizeof(struct ip_fw *), M_IPFW,
119                         locked ? M_NOWAIT : M_WAITOK);
120                 if (map == NULL) {
121                         printf("%s: cannot allocate map\n", __FUNCTION__);
122                         return NULL;
123                 }
124                 if (!locked)
125                         IPFW_UH_WLOCK(chain);
126                 if (i >= chain->n_rules + extra) /* good */
127                         return map;
128                 /* otherwise we lost the race, free and retry */
129                 if (!locked)
130                         IPFW_UH_WUNLOCK(chain);
131                 free(map, M_IPFW);
132         }
133 }
134
135 /*
136  * swap the maps. It is supposed to be called with IPFW_UH_WLOCK
137  */
138 static struct ip_fw **
139 swap_map(struct ip_fw_chain *chain, struct ip_fw **new_map, int new_len)
140 {
141         struct ip_fw **old_map;
142
143         IPFW_WLOCK(chain);
144         chain->id++;
145         chain->n_rules = new_len;
146         old_map = chain->map;
147         chain->map = new_map;
148         IPFW_WUNLOCK(chain);
149         return old_map;
150 }
151
152 /*
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
158  */
159 int
160 ipfw_add_rule(struct ip_fw_chain *chain, struct ip_fw *input_rule)
161 {
162         struct ip_fw *rule;
163         int i, l, insert_before;
164         struct ip_fw **map;     /* the new array of pointers */
165
166         if (chain->rules == NULL || input_rule->rulenum > IPFW_DEFAULT_RULE-1)
167                 return (EINVAL);
168
169         l = RULESIZE(input_rule);
170         rule = malloc(l, M_IPFW, M_WAITOK | M_ZERO);
171         if (rule == NULL)
172                 return (ENOSPC);
173         /* get_map returns with IPFW_UH_WLOCK if successful */
174         map = get_map(chain, 1, 0 /* not locked */);
175         if (map == NULL) {
176                 free(rule, M_IPFW);
177                 return ENOSPC;
178         }
179
180         bcopy(input_rule, rule, l);
181         /* clear fields not settable from userland */
182         rule->x_next = NULL;
183         rule->next_rule = NULL;
184         rule->pcnt = 0;
185         rule->bcnt = 0;
186         rule->timestamp = 0;
187
188         if (V_autoinc_step < 1)
189                 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 */
196         if (i > 0)
197                 bcopy(chain->map, map, i * sizeof(struct ip_fw *));
198         map[i] = rule;
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;
208         }
209
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);
214         if (map)
215                 free(map, M_IPFW);
216         return (0);
217 }
218
219 /*
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.
223  */
224 void
225 ipfw_reap_rules(struct ip_fw *head)
226 {
227         struct ip_fw *rule;
228
229         while ((rule = head) != NULL) {
230                 head = head->x_next;
231                 free(rule, M_IPFW);
232         }
233 }
234
235 /*
236  * Used by del_entry() to check if a rule should be kept.
237  * Returns 1 if the rule must be kept, 0 otherwise.
238  *
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.
243  *
244  * n == 0 is a wildcard for rule numbers, there is no wildcard for sets.
245  *
246  * Rules to keep are
247  *      (default || reserved || !match_set || !match_number)
248  * where
249  *   default ::= (rule->rulenum == IPFW_DEFAULT_RULE)
250  *      // the default rule is always protected
251  *
252  *   reserved ::= (cmd == 0 && n == 0 && rule->set == RESVD_SET)
253  *      // RESVD_SET is protected only if cmd == 0 and n == 0 ("ipfw flush")
254  *
255  *   match_set ::= (cmd == 0 || rule->set == set)
256  *      // set number is ignored for cmd == 0
257  *
258  *   match_number ::= (cmd == 1 || n == 0 || n == rule->rulenum)
259  *      // number is ignored for cmd == 1 or n == 0
260  *
261  */
262 static int
263 keep_rule(struct ip_fw *rule, uint8_t cmd, uint8_t set, uint32_t n)
264 {
265         return
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);
270 }
271
272 /**
273  * Remove all rules with given number, or do set manipulation.
274  * Assumes chain != NULL && *chain != NULL.
275  *
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:
278  *
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"
285  */
286 static int
287 del_entry(struct ip_fw_chain *chain, uint32_t arg)
288 {
289         struct ip_fw *rule;
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;
294         int error = 0;
295
296         num = arg & 0xffff;
297         cmd = (arg >> 24) & 0xff;
298         new_set = (arg >> 16) & 0xff;
299
300         if (cmd > 5 || new_set > RESVD_SET)
301                 return EINVAL;
302         if (cmd == 0 || cmd == 2 || cmd == 5) {
303                 if (num >= IPFW_DEFAULT_RULE)
304                         return EINVAL;
305         } else {
306                 if (num > RESVD_SET)    /* old_set */
307                         return EINVAL;
308         }
309
310         IPFW_UH_WLOCK(chain);   /* arbitrate writers */
311         chain->reap = NULL;     /* prepare for deletions */
312
313         switch (cmd) {
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". */
317
318                 /*
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.
323                  */
324                 n = 0;
325                 if (cmd == 1) {
326                         /* look for a specific set including RESVD_SET.
327                          * Must scan the entire range, ignore num.
328                          */
329                         new_set = num;
330                         for (start = -1, end = i = 0; i < chain->n_rules; i++) {
331                                 if (keep_rule(chain->map[i], cmd, new_set, 0))
332                                         continue;
333                                 if (start < 0)
334                                         start = i;
335                                 end = i;
336                                 n++;
337                         }
338                         end++;  /* first non-matching */
339                 } else {
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)
345                                         break;
346                                 if (!keep_rule(rule, cmd, new_set, num))
347                                         n++;
348                         }
349                 }
350
351                 if (n == 0) {
352                         /* A flush request (arg == 0) on empty ruleset
353                          * returns with no error. On the contrary,
354                          * if there is no match on a specific request,
355                          * we return EINVAL.
356                          */
357                         error = (arg == 0) ? 0 : EINVAL;
358                         break;
359                 }
360
361                 /* We have something to delete. Allocate the new map */
362                 map = get_map(chain, -n, 1 /* locked */);
363                 if (map == NULL) {
364                         error = EINVAL;
365                         break;
366                 }
367
368                 /* 1. bcopy the initial part of the map */
369                 if (start > 0)
370                         bcopy(chain->map, map, start * sizeof(struct ip_fw *));
371                 /* 2. copy active rules between start and end */
372                 for (i = ofs = start; i < end; i++) {
373                         rule = chain->map[i];
374                         if (keep_rule(rule, cmd, new_set, num))
375                                 map[ofs++] = rule;
376                 }
377                 /* 3. copy the final part of the map */
378                 bcopy(chain->map + end, map + ofs,
379                         (chain->n_rules - end) * sizeof(struct ip_fw *));
380                 /* 4. swap the maps (under BH_LOCK) */
381                 map = swap_map(chain, map, chain->n_rules - n);
382                 /* 5. now remove the rules deleted from the old map */
383                 for (i = start; i < end; i++) {
384                         int l;
385                         rule = map[i];
386                         if (keep_rule(rule, cmd, new_set, num))
387                                 continue;
388                         l = RULESIZE(rule);
389                         chain->static_len -= l;
390                         ipfw_remove_dyn_children(rule);
391                         rule->x_next = chain->reap;
392                         chain->reap = rule;
393                 }
394                 break;
395
396         /*
397          * In the next 3 cases the loop stops at (n_rules - 1)
398          * because the default rule is never eligible..
399          */
400
401         case 2: /* move rules with given RULE number to new set */
402                 for (i = 0; i < chain->n_rules - 1; i++) {
403                         rule = chain->map[i];
404                         if (rule->rulenum == num)
405                                 rule->set = new_set;
406                 }
407                 break;
408
409         case 3: /* move rules with given SET number to new set */
410                 for (i = 0; i < chain->n_rules - 1; i++) {
411                         rule = chain->map[i];
412                         if (rule->set == num)
413                                 rule->set = new_set;
414                 }
415                 break;
416
417         case 4: /* swap two sets */
418                 for (i = 0; i < chain->n_rules - 1; i++) {
419                         rule = chain->map[i];
420                         if (rule->set == num)
421                                 rule->set = new_set;
422                         else if (rule->set == new_set)
423                                 rule->set = num;
424                 }
425                 break;
426         }
427
428         rule = chain->reap;
429         chain->reap = NULL;
430         IPFW_UH_WUNLOCK(chain);
431         ipfw_reap_rules(rule);
432         if (map)
433                 free(map, M_IPFW);
434         return error;
435 }
436
437 /*
438  * Clear counters for a specific rule.
439  * Normally run under IPFW_UH_RLOCK, but these are idempotent ops
440  * so we only care that rules do not disappear.
441  */
442 static void
443 clear_counters(struct ip_fw *rule, int log_only)
444 {
445         ipfw_insn_log *l = (ipfw_insn_log *)ACTION_PTR(rule);
446
447         if (log_only == 0) {
448                 rule->bcnt = rule->pcnt = 0;
449                 rule->timestamp = 0;
450         }
451         if (l->o.opcode == O_LOG)
452                 l->log_left = l->max_log;
453 }
454
455 /**
456  * Reset some or all counters on firewall rules.
457  * The argument `arg' is an u_int32_t. The low 16 bit are the rule number,
458  * the next 8 bits are the set number, the top 8 bits are the command:
459  *      0       work with rules from all set's;
460  *      1       work with rules only from specified set.
461  * Specified rule number is zero if we want to clear all entries.
462  * log_only is 1 if we only want to reset logs, zero otherwise.
463  */
464 static int
465 zero_entry(struct ip_fw_chain *chain, u_int32_t arg, int log_only)
466 {
467         struct ip_fw *rule;
468         char *msg;
469         int i;
470
471         uint16_t rulenum = arg & 0xffff;
472         uint8_t set = (arg >> 16) & 0xff;
473         uint8_t cmd = (arg >> 24) & 0xff;
474
475         if (cmd > 1)
476                 return (EINVAL);
477         if (cmd == 1 && set > RESVD_SET)
478                 return (EINVAL);
479
480         IPFW_UH_RLOCK(chain);
481         if (rulenum == 0) {
482                 V_norule_counter = 0;
483                 for (i = 0; i < chain->n_rules; i++) {
484                         rule = chain->map[i];
485                         /* Skip rules not in our set. */
486                         if (cmd == 1 && rule->set != set)
487                                 continue;
488                         clear_counters(rule, log_only);
489                 }
490                 msg = log_only ? "All logging counts reset" :
491                     "Accounting cleared";
492         } else {
493                 int cleared = 0;
494                 for (i = 0; i < chain->n_rules; i++) {
495                         rule = chain->map[i];
496                         if (rule->rulenum == rulenum) {
497                                 if (cmd == 0 || rule->set == set)
498                                         clear_counters(rule, log_only);
499                                 cleared = 1;
500                         }
501                         if (rule->rulenum > rulenum)
502                                 break;
503                 }
504                 if (!cleared) { /* we did not find any matching rules */
505                         IPFW_UH_RUNLOCK(chain);
506                         return (EINVAL);
507                 }
508                 msg = log_only ? "logging count reset" : "cleared";
509         }
510         IPFW_UH_RUNLOCK(chain);
511
512         if (V_fw_verbose) {
513                 int lev = LOG_SECURITY | LOG_NOTICE;
514
515                 if (rulenum)
516                         log(lev, "ipfw: Entry %d %s.\n", rulenum, msg);
517                 else
518                         log(lev, "ipfw: %s.\n", msg);
519         }
520         return (0);
521 }
522
523 /*
524  * Check validity of the structure before insert.
525  * Rules are simple, so this mostly need to check rule sizes.
526  */
527 static int
528 check_ipfw_struct(struct ip_fw *rule, int size)
529 {
530         int l, cmdlen = 0;
531         int have_action=0;
532         ipfw_insn *cmd;
533
534         if (size < sizeof(*rule)) {
535                 printf("ipfw: rule too short\n");
536                 return (EINVAL);
537         }
538         /* first, check for valid size */
539         l = RULESIZE(rule);
540         if (l != size) {
541                 printf("ipfw: size mismatch (have %d want %d)\n", size, l);
542                 return (EINVAL);
543         }
544         if (rule->act_ofs >= rule->cmd_len) {
545                 printf("ipfw: bogus action offset (%u > %u)\n",
546                     rule->act_ofs, rule->cmd_len - 1);
547                 return (EINVAL);
548         }
549         /*
550          * Now go for the individual checks. Very simple ones, basically only
551          * instruction sizes.
552          */
553         for (l = rule->cmd_len, cmd = rule->cmd ;
554                         l > 0 ; l -= cmdlen, cmd += cmdlen) {
555                 cmdlen = F_LEN(cmd);
556                 if (cmdlen > l) {
557                         printf("ipfw: opcode %d size truncated\n",
558                             cmd->opcode);
559                         return EINVAL;
560                 }
561                 switch (cmd->opcode) {
562                 case O_PROBE_STATE:
563                 case O_KEEP_STATE:
564                 case O_PROTO:
565                 case O_IP_SRC_ME:
566                 case O_IP_DST_ME:
567                 case O_LAYER2:
568                 case O_IN:
569                 case O_FRAG:
570                 case O_DIVERTED:
571                 case O_IPOPT:
572                 case O_IPTOS:
573                 case O_IPPRECEDENCE:
574                 case O_IPVER:
575                 case O_TCPWIN:
576                 case O_TCPFLAGS:
577                 case O_TCPOPTS:
578                 case O_ESTAB:
579                 case O_VERREVPATH:
580                 case O_VERSRCREACH:
581                 case O_ANTISPOOF:
582                 case O_IPSEC:
583 #ifdef INET6
584                 case O_IP6_SRC_ME:
585                 case O_IP6_DST_ME:
586                 case O_EXT_HDR:
587                 case O_IP6:
588 #endif
589                 case O_IP4:
590                 case O_TAG:
591                         if (cmdlen != F_INSN_SIZE(ipfw_insn))
592                                 goto bad_size;
593                         break;
594
595                 case O_FIB:
596                         if (cmdlen != F_INSN_SIZE(ipfw_insn))
597                                 goto bad_size;
598                         if (cmd->arg1 >= rt_numfibs) {
599                                 printf("ipfw: invalid fib number %d\n",
600                                         cmd->arg1);
601                                 return EINVAL;
602                         }
603                         break;
604
605                 case O_SETFIB:
606                         if (cmdlen != F_INSN_SIZE(ipfw_insn))
607                                 goto bad_size;
608                         if (cmd->arg1 >= rt_numfibs) {
609                                 printf("ipfw: invalid fib number %d\n",
610                                         cmd->arg1);
611                                 return EINVAL;
612                         }
613                         goto check_action;
614
615                 case O_UID:
616                 case O_GID:
617                 case O_JAIL:
618                 case O_IP_SRC:
619                 case O_IP_DST:
620                 case O_TCPSEQ:
621                 case O_TCPACK:
622                 case O_PROB:
623                 case O_ICMPTYPE:
624                         if (cmdlen != F_INSN_SIZE(ipfw_insn_u32))
625                                 goto bad_size;
626                         break;
627
628                 case O_LIMIT:
629                         if (cmdlen != F_INSN_SIZE(ipfw_insn_limit))
630                                 goto bad_size;
631                         break;
632
633                 case O_LOG:
634                         if (cmdlen != F_INSN_SIZE(ipfw_insn_log))
635                                 goto bad_size;
636
637                         ((ipfw_insn_log *)cmd)->log_left =
638                             ((ipfw_insn_log *)cmd)->max_log;
639
640                         break;
641
642                 case O_IP_SRC_MASK:
643                 case O_IP_DST_MASK:
644                         /* only odd command lengths */
645                         if ( !(cmdlen & 1) || cmdlen > 31)
646                                 goto bad_size;
647                         break;
648
649                 case O_IP_SRC_SET:
650                 case O_IP_DST_SET:
651                         if (cmd->arg1 == 0 || cmd->arg1 > 256) {
652                                 printf("ipfw: invalid set size %d\n",
653                                         cmd->arg1);
654                                 return EINVAL;
655                         }
656                         if (cmdlen != F_INSN_SIZE(ipfw_insn_u32) +
657                             (cmd->arg1+31)/32 )
658                                 goto bad_size;
659                         break;
660
661                 case O_IP_SRC_LOOKUP:
662                 case O_IP_DST_LOOKUP:
663                         if (cmd->arg1 >= IPFW_TABLES_MAX) {
664                                 printf("ipfw: invalid table number %d\n",
665                                     cmd->arg1);
666                                 return (EINVAL);
667                         }
668                         if (cmdlen != F_INSN_SIZE(ipfw_insn) &&
669                             cmdlen != F_INSN_SIZE(ipfw_insn_u32) + 1 &&
670                             cmdlen != F_INSN_SIZE(ipfw_insn_u32))
671                                 goto bad_size;
672                         break;
673
674                 case O_MACADDR2:
675                         if (cmdlen != F_INSN_SIZE(ipfw_insn_mac))
676                                 goto bad_size;
677                         break;
678
679                 case O_NOP:
680                 case O_IPID:
681                 case O_IPTTL:
682                 case O_IPLEN:
683                 case O_TCPDATALEN:
684                 case O_TAGGED:
685                         if (cmdlen < 1 || cmdlen > 31)
686                                 goto bad_size;
687                         break;
688
689                 case O_MAC_TYPE:
690                 case O_IP_SRCPORT:
691                 case O_IP_DSTPORT: /* XXX artificial limit, 30 port pairs */
692                         if (cmdlen < 2 || cmdlen > 31)
693                                 goto bad_size;
694                         break;
695
696                 case O_RECV:
697                 case O_XMIT:
698                 case O_VIA:
699                         if (cmdlen != F_INSN_SIZE(ipfw_insn_if))
700                                 goto bad_size;
701                         break;
702
703                 case O_ALTQ:
704                         if (cmdlen != F_INSN_SIZE(ipfw_insn_altq))
705                                 goto bad_size;
706                         break;
707
708                 case O_PIPE:
709                 case O_QUEUE:
710                         if (cmdlen != F_INSN_SIZE(ipfw_insn))
711                                 goto bad_size;
712                         goto check_action;
713
714                 case O_FORWARD_IP:
715 #ifdef  IPFIREWALL_FORWARD
716                         if (cmdlen != F_INSN_SIZE(ipfw_insn_sa))
717                                 goto bad_size;
718                         goto check_action;
719 #else
720                         return EINVAL;
721 #endif
722
723                 case O_DIVERT:
724                 case O_TEE:
725                         if (ip_divert_ptr == NULL)
726                                 return EINVAL;
727                         else
728                                 goto check_size;
729                 case O_NETGRAPH:
730                 case O_NGTEE:
731                         if (ng_ipfw_input_p == NULL)
732                                 return EINVAL;
733                         else
734                                 goto check_size;
735                 case O_NAT:
736                         if (!IPFW_NAT_LOADED)
737                                 return EINVAL;
738                         if (cmdlen != F_INSN_SIZE(ipfw_insn_nat))
739                                 goto bad_size;          
740                         goto check_action;
741                 case O_FORWARD_MAC: /* XXX not implemented yet */
742                 case O_CHECK_STATE:
743                 case O_COUNT:
744                 case O_ACCEPT:
745                 case O_DENY:
746                 case O_REJECT:
747 #ifdef INET6
748                 case O_UNREACH6:
749 #endif
750                 case O_SKIPTO:
751                 case O_REASS:
752 check_size:
753                         if (cmdlen != F_INSN_SIZE(ipfw_insn))
754                                 goto bad_size;
755 check_action:
756                         if (have_action) {
757                                 printf("ipfw: opcode %d, multiple actions"
758                                         " not allowed\n",
759                                         cmd->opcode);
760                                 return EINVAL;
761                         }
762                         have_action = 1;
763                         if (l != cmdlen) {
764                                 printf("ipfw: opcode %d, action must be"
765                                         " last opcode\n",
766                                         cmd->opcode);
767                                 return EINVAL;
768                         }
769                         break;
770 #ifdef INET6
771                 case O_IP6_SRC:
772                 case O_IP6_DST:
773                         if (cmdlen != F_INSN_SIZE(struct in6_addr) +
774                             F_INSN_SIZE(ipfw_insn))
775                                 goto bad_size;
776                         break;
777
778                 case O_FLOW6ID:
779                         if (cmdlen != F_INSN_SIZE(ipfw_insn_u32) +
780                             ((ipfw_insn_u32 *)cmd)->o.arg1)
781                                 goto bad_size;
782                         break;
783
784                 case O_IP6_SRC_MASK:
785                 case O_IP6_DST_MASK:
786                         if ( !(cmdlen & 1) || cmdlen > 127)
787                                 goto bad_size;
788                         break;
789                 case O_ICMP6TYPE:
790                         if( cmdlen != F_INSN_SIZE( ipfw_insn_icmp6 ) )
791                                 goto bad_size;
792                         break;
793 #endif
794
795                 default:
796                         switch (cmd->opcode) {
797 #ifndef INET6
798                         case O_IP6_SRC_ME:
799                         case O_IP6_DST_ME:
800                         case O_EXT_HDR:
801                         case O_IP6:
802                         case O_UNREACH6:
803                         case O_IP6_SRC:
804                         case O_IP6_DST:
805                         case O_FLOW6ID:
806                         case O_IP6_SRC_MASK:
807                         case O_IP6_DST_MASK:
808                         case O_ICMP6TYPE:
809                                 printf("ipfw: no IPv6 support in kernel\n");
810                                 return EPROTONOSUPPORT;
811 #endif
812                         default:
813                                 printf("ipfw: opcode %d, unknown opcode\n",
814                                         cmd->opcode);
815                                 return EINVAL;
816                         }
817                 }
818         }
819         if (have_action == 0) {
820                 printf("ipfw: missing action\n");
821                 return EINVAL;
822         }
823         return 0;
824
825 bad_size:
826         printf("ipfw: opcode %d size %d wrong\n",
827                 cmd->opcode, cmdlen);
828         return EINVAL;
829 }
830
831
832 /*
833  * Translation of requests for compatibility with FreeBSD 7.2/8.
834  * a static variable tells us if we have an old client from userland,
835  * and if necessary we translate requests and responses between the
836  * two formats.
837  */
838 static int is7 = 0;
839
840 struct ip_fw7 {
841         struct ip_fw7   *next;          /* linked list of rules     */
842         struct ip_fw7   *next_rule;     /* ptr to next [skipto] rule    */
843         /* 'next_rule' is used to pass up 'set_disable' status      */
844
845         uint16_t        act_ofs;        /* offset of action in 32-bit units */
846         uint16_t        cmd_len;        /* # of 32-bit words in cmd */
847         uint16_t        rulenum;        /* rule number          */
848         uint8_t         set;            /* rule set (0..31)     */
849         // #define RESVD_SET   31  /* set for default and persistent rules */
850         uint8_t         _pad;           /* padding          */
851         // uint32_t        id;             /* rule id, only in v.8 */
852         /* These fields are present in all rules.           */
853         uint64_t        pcnt;           /* Packet counter       */
854         uint64_t        bcnt;           /* Byte counter         */
855         uint32_t        timestamp;      /* tv_sec of last match     */
856
857         ipfw_insn       cmd[1];         /* storage for commands     */
858 };
859
860         int convert_rule_to_7(struct ip_fw *rule);
861 int convert_rule_to_8(struct ip_fw *rule);
862
863 #ifndef RULESIZE7
864 #define RULESIZE7(rule)  (sizeof(struct ip_fw7) + \
865         ((struct ip_fw7 *)(rule))->cmd_len * 4 - 4)
866 #endif
867
868
869 /*
870  * Copy the static and dynamic rules to the supplied buffer
871  * and return the amount of space actually used.
872  * Must be run under IPFW_UH_RLOCK
873  */
874 static size_t
875 ipfw_getrules(struct ip_fw_chain *chain, void *buf, size_t space)
876 {
877         char *bp = buf;
878         char *ep = bp + space;
879         struct ip_fw *rule, *dst;
880         int l, i;
881         time_t  boot_seconds;
882
883         boot_seconds = boottime.tv_sec;
884         for (i = 0; i < chain->n_rules; i++) {
885                 rule = chain->map[i];
886
887                 if (is7) {
888                     /* Convert rule to FreeBSd 7.2 format */
889                     l = RULESIZE7(rule);
890                     if (bp + l + sizeof(uint32_t) <= ep) {
891                         int error;
892                         bcopy(rule, bp, l + sizeof(uint32_t));
893                         error = convert_rule_to_7((struct ip_fw *) bp);
894                         if (error)
895                                 return 0; /*XXX correct? */
896                         /*
897                          * XXX HACK. Store the disable mask in the "next"
898                          * pointer in a wild attempt to keep the ABI the same.
899                          * Why do we do this on EVERY rule?
900                          */
901                         bcopy(&V_set_disable,
902                                 &(((struct ip_fw7 *)bp)->next_rule),
903                                 sizeof(V_set_disable));
904                         if (((struct ip_fw7 *)bp)->timestamp)
905                             ((struct ip_fw7 *)bp)->timestamp += boot_seconds;
906                         bp += l;
907                     }
908                     continue; /* go to next rule */
909                 }
910
911                 /* normal mode, don't touch rules */
912                 l = RULESIZE(rule);
913                 if (bp + l > ep) { /* should not happen */
914                         printf("overflow dumping static rules\n");
915                         break;
916                 }
917                 dst = (struct ip_fw *)bp;
918                 bcopy(rule, dst, l);
919                 /*
920                  * XXX HACK. Store the disable mask in the "next"
921                  * pointer in a wild attempt to keep the ABI the same.
922                  * Why do we do this on EVERY rule?
923                  */
924                 bcopy(&V_set_disable, &dst->next_rule, sizeof(V_set_disable));
925                 if (dst->timestamp)
926                         dst->timestamp += boot_seconds;
927                 bp += l;
928         }
929         ipfw_get_dynamic(&bp, ep); /* protected by the dynamic lock */
930         return (bp - (char *)buf);
931 }
932
933
934 /**
935  * {set|get}sockopt parser.
936  */
937 int
938 ipfw_ctl(struct sockopt *sopt)
939 {
940 #define RULE_MAXSIZE    (256*sizeof(u_int32_t))
941         int error;
942         size_t size;
943         struct ip_fw *buf, *rule;
944         struct ip_fw_chain *chain;
945         u_int32_t rulenum[2];
946
947         error = priv_check(sopt->sopt_td, PRIV_NETINET_IPFW);
948         if (error)
949                 return (error);
950
951         /*
952          * Disallow modifications in really-really secure mode, but still allow
953          * the logging counters to be reset.
954          */
955         if (sopt->sopt_name == IP_FW_ADD ||
956             (sopt->sopt_dir == SOPT_SET && sopt->sopt_name != IP_FW_RESETLOG)) {
957                 error = securelevel_ge(sopt->sopt_td->td_ucred, 3);
958                 if (error)
959                         return (error);
960         }
961
962         chain = &V_layer3_chain;
963         error = 0;
964
965         switch (sopt->sopt_name) {
966         case IP_FW_GET:
967                 /*
968                  * pass up a copy of the current rules. Static rules
969                  * come first (the last of which has number IPFW_DEFAULT_RULE),
970                  * followed by a possibly empty list of dynamic rule.
971                  * The last dynamic rule has NULL in the "next" field.
972                  *
973                  * Note that the calculated size is used to bound the
974                  * amount of data returned to the user.  The rule set may
975                  * change between calculating the size and returning the
976                  * data in which case we'll just return what fits.
977                  */
978                 for (;;) {
979                         int len = 0, want;
980
981                         size = chain->static_len;
982                         size += ipfw_dyn_len();
983                         if (size >= sopt->sopt_valsize)
984                                 break;
985                         buf = malloc(size, M_TEMP, M_WAITOK);
986                         if (buf == NULL)
987                                 break;
988                         IPFW_UH_RLOCK(chain);
989                         /* check again how much space we need */
990                         want = chain->static_len + ipfw_dyn_len();
991                         if (size >= want)
992                                 len = ipfw_getrules(chain, buf, size);
993                         IPFW_UH_RUNLOCK(chain);
994                         if (size >= want)
995                                 error = sooptcopyout(sopt, buf, len);
996                         free(buf, M_TEMP);
997                         if (size >= want)
998                                 break;
999                 }
1000                 break;
1001
1002         case IP_FW_FLUSH:
1003                 /* locking is done within del_entry() */
1004                 error = del_entry(chain, 0); /* special case, rule=0, cmd=0 means all */
1005                 break;
1006
1007         case IP_FW_ADD:
1008                 rule = malloc(RULE_MAXSIZE, M_TEMP, M_WAITOK);
1009                 error = sooptcopyin(sopt, rule, RULE_MAXSIZE,
1010                         sizeof(struct ip_fw7) );
1011
1012                 /*
1013                  * If the size of commands equals RULESIZE7 then we assume
1014                  * a FreeBSD7.2 binary is talking to us (set is7=1).
1015                  * is7 is persistent so the next 'ipfw list' command
1016                  * will use this format.
1017                  * NOTE: If wrong version is guessed (this can happen if
1018                  *       the first ipfw command is 'ipfw [pipe] list')
1019                  *       the ipfw binary may crash or loop infinitly...
1020                  */
1021                 if (sopt->sopt_valsize == RULESIZE7(rule)) {
1022                     is7 = 1;
1023                     error = convert_rule_to_8(rule);
1024                     if (error)
1025                         return error;
1026                     if (error == 0)
1027                         error = check_ipfw_struct(rule, RULESIZE(rule));
1028                 } else {
1029                     is7 = 0;
1030                 if (error == 0)
1031                         error = check_ipfw_struct(rule, sopt->sopt_valsize);
1032                 }
1033                 if (error == 0) {
1034                         /* locking is done within ipfw_add_rule() */
1035                         error = ipfw_add_rule(chain, rule);
1036                         size = RULESIZE(rule);
1037                         if (!error && sopt->sopt_dir == SOPT_GET) {
1038                                 if (is7) {
1039                                         error = convert_rule_to_7(rule);
1040                                         size = RULESIZE7(rule);
1041                                         if (error)
1042                                                 return error;
1043                                 }
1044                                 error = sooptcopyout(sopt, rule, size);
1045                 }
1046                 }
1047                 free(rule, M_TEMP);
1048                 break;
1049
1050         case IP_FW_DEL:
1051                 /*
1052                  * IP_FW_DEL is used for deleting single rules or sets,
1053                  * and (ab)used to atomically manipulate sets. Argument size
1054                  * is used to distinguish between the two:
1055                  *    sizeof(u_int32_t)
1056                  *      delete single rule or set of rules,
1057                  *      or reassign rules (or sets) to a different set.
1058                  *    2*sizeof(u_int32_t)
1059                  *      atomic disable/enable sets.
1060                  *      first u_int32_t contains sets to be disabled,
1061                  *      second u_int32_t contains sets to be enabled.
1062                  */
1063                 error = sooptcopyin(sopt, rulenum,
1064                         2*sizeof(u_int32_t), sizeof(u_int32_t));
1065                 if (error)
1066                         break;
1067                 size = sopt->sopt_valsize;
1068                 if (size == sizeof(u_int32_t) && rulenum[0] != 0) {
1069                         /* delete or reassign, locking done in del_entry() */
1070                         error = del_entry(chain, rulenum[0]);
1071                 } else if (size == 2*sizeof(u_int32_t)) { /* set enable/disable */
1072                         IPFW_UH_WLOCK(chain);
1073                         V_set_disable =
1074                             (V_set_disable | rulenum[0]) & ~rulenum[1] &
1075                             ~(1<<RESVD_SET); /* set RESVD_SET always enabled */
1076                         IPFW_UH_WUNLOCK(chain);
1077                 } else
1078                         error = EINVAL;
1079                 break;
1080
1081         case IP_FW_ZERO:
1082         case IP_FW_RESETLOG: /* argument is an u_int_32, the rule number */
1083                 rulenum[0] = 0;
1084                 if (sopt->sopt_val != 0) {
1085                     error = sooptcopyin(sopt, rulenum,
1086                             sizeof(u_int32_t), sizeof(u_int32_t));
1087                     if (error)
1088                         break;
1089                 }
1090                 error = zero_entry(chain, rulenum[0],
1091                         sopt->sopt_name == IP_FW_RESETLOG);
1092                 break;
1093
1094         /*--- TABLE manipulations are protected by the IPFW_LOCK ---*/
1095         case IP_FW_TABLE_ADD:
1096                 {
1097                         ipfw_table_entry ent;
1098
1099                         error = sooptcopyin(sopt, &ent,
1100                             sizeof(ent), sizeof(ent));
1101                         if (error)
1102                                 break;
1103                         error = ipfw_add_table_entry(chain, ent.tbl,
1104                             ent.addr, ent.masklen, ent.value);
1105                 }
1106                 break;
1107
1108         case IP_FW_TABLE_DEL:
1109                 {
1110                         ipfw_table_entry ent;
1111
1112                         error = sooptcopyin(sopt, &ent,
1113                             sizeof(ent), sizeof(ent));
1114                         if (error)
1115                                 break;
1116                         error = ipfw_del_table_entry(chain, ent.tbl,
1117                             ent.addr, ent.masklen);
1118                 }
1119                 break;
1120
1121         case IP_FW_TABLE_FLUSH:
1122                 {
1123                         u_int16_t tbl;
1124
1125                         error = sooptcopyin(sopt, &tbl,
1126                             sizeof(tbl), sizeof(tbl));
1127                         if (error)
1128                                 break;
1129                         IPFW_WLOCK(chain);
1130                         error = ipfw_flush_table(chain, tbl);
1131                         IPFW_WUNLOCK(chain);
1132                 }
1133                 break;
1134
1135         case IP_FW_TABLE_GETSIZE:
1136                 {
1137                         u_int32_t tbl, cnt;
1138
1139                         if ((error = sooptcopyin(sopt, &tbl, sizeof(tbl),
1140                             sizeof(tbl))))
1141                                 break;
1142                         IPFW_RLOCK(chain);
1143                         error = ipfw_count_table(chain, tbl, &cnt);
1144                         IPFW_RUNLOCK(chain);
1145                         if (error)
1146                                 break;
1147                         error = sooptcopyout(sopt, &cnt, sizeof(cnt));
1148                 }
1149                 break;
1150
1151         case IP_FW_TABLE_LIST:
1152                 {
1153                         ipfw_table *tbl;
1154
1155                         if (sopt->sopt_valsize < sizeof(*tbl)) {
1156                                 error = EINVAL;
1157                                 break;
1158                         }
1159                         size = sopt->sopt_valsize;
1160                         tbl = malloc(size, M_TEMP, M_WAITOK);
1161                         error = sooptcopyin(sopt, tbl, size, sizeof(*tbl));
1162                         if (error) {
1163                                 free(tbl, M_TEMP);
1164                                 break;
1165                         }
1166                         tbl->size = (size - sizeof(*tbl)) /
1167                             sizeof(ipfw_table_entry);
1168                         IPFW_RLOCK(chain);
1169                         error = ipfw_dump_table(chain, tbl);
1170                         IPFW_RUNLOCK(chain);
1171                         if (error) {
1172                                 free(tbl, M_TEMP);
1173                                 break;
1174                         }
1175                         error = sooptcopyout(sopt, tbl, size);
1176                         free(tbl, M_TEMP);
1177                 }
1178                 break;
1179
1180         /*--- NAT operations are protected by the IPFW_LOCK ---*/
1181         case IP_FW_NAT_CFG:
1182                 if (IPFW_NAT_LOADED)
1183                         error = ipfw_nat_cfg_ptr(sopt);
1184                 else {
1185                         printf("IP_FW_NAT_CFG: %s\n",
1186                             "ipfw_nat not present, please load it");
1187                         error = EINVAL;
1188                 }
1189                 break;
1190
1191         case IP_FW_NAT_DEL:
1192                 if (IPFW_NAT_LOADED)
1193                         error = ipfw_nat_del_ptr(sopt);
1194                 else {
1195                         printf("IP_FW_NAT_DEL: %s\n",
1196                             "ipfw_nat not present, please load it");
1197                         error = EINVAL;
1198                 }
1199                 break;
1200
1201         case IP_FW_NAT_GET_CONFIG:
1202                 if (IPFW_NAT_LOADED)
1203                         error = ipfw_nat_get_cfg_ptr(sopt);
1204                 else {
1205                         printf("IP_FW_NAT_GET_CFG: %s\n",
1206                             "ipfw_nat not present, please load it");
1207                         error = EINVAL;
1208                 }
1209                 break;
1210
1211         case IP_FW_NAT_GET_LOG:
1212                 if (IPFW_NAT_LOADED)
1213                         error = ipfw_nat_get_log_ptr(sopt);
1214                 else {
1215                         printf("IP_FW_NAT_GET_LOG: %s\n",
1216                             "ipfw_nat not present, please load it");
1217                         error = EINVAL;
1218                 }
1219                 break;
1220
1221         default:
1222                 printf("ipfw: ipfw_ctl invalid option %d\n", sopt->sopt_name);
1223                 error = EINVAL;
1224         }
1225
1226         return (error);
1227 #undef RULE_MAXSIZE
1228 }
1229
1230
1231 #define RULE_MAXSIZE    (256*sizeof(u_int32_t))
1232
1233 /* Functions to convert rules 7.2 <==> 8.0 */
1234 int
1235 convert_rule_to_7(struct ip_fw *rule)
1236 {
1237         /* Used to modify original rule */
1238         struct ip_fw7 *rule7 = (struct ip_fw7 *)rule;
1239         /* copy of original rule, version 8 */
1240         struct ip_fw *tmp;
1241
1242         /* Used to copy commands */
1243         ipfw_insn *ccmd, *dst;
1244         int ll = 0, ccmdlen = 0;
1245
1246         tmp = malloc(RULE_MAXSIZE, M_TEMP, M_NOWAIT | M_ZERO);
1247         if (tmp == NULL) {
1248                 return 1; //XXX error
1249         }
1250         bcopy(rule, tmp, RULE_MAXSIZE);
1251
1252         /* Copy fields */
1253         rule7->_pad = tmp->_pad;
1254         rule7->set = tmp->set;
1255         rule7->rulenum = tmp->rulenum;
1256         rule7->cmd_len = tmp->cmd_len;
1257         rule7->act_ofs = tmp->act_ofs;
1258         rule7->next_rule = (struct ip_fw7 *)tmp->next_rule;
1259         rule7->next = (struct ip_fw7 *)tmp->x_next;
1260         rule7->cmd_len = tmp->cmd_len;
1261         rule7->pcnt = tmp->pcnt;
1262         rule7->bcnt = tmp->bcnt;
1263         rule7->timestamp = tmp->timestamp;
1264
1265         /* Copy commands */
1266         for (ll = tmp->cmd_len, ccmd = tmp->cmd, dst = rule7->cmd ;
1267                         ll > 0 ; ll -= ccmdlen, ccmd += ccmdlen, dst += ccmdlen) {
1268                 ccmdlen = F_LEN(ccmd);
1269
1270                 bcopy(ccmd, dst, F_LEN(ccmd)*sizeof(uint32_t));
1271
1272                 if (dst->opcode > O_NAT)
1273                         /* O_REASS doesn't exists in 7.2 version, so
1274                          * decrement opcode if it is after O_REASS
1275                          */
1276                         dst->opcode--;
1277
1278                 if (ccmdlen > ll) {
1279                         printf("ipfw: opcode %d size truncated\n",
1280                                 ccmd->opcode);
1281                         return EINVAL;
1282                 }
1283         }
1284         free(tmp, M_TEMP);
1285
1286         return 0;
1287 }
1288
1289 int
1290 convert_rule_to_8(struct ip_fw *rule)
1291 {
1292         /* Used to modify original rule */
1293         struct ip_fw7 *rule7 = (struct ip_fw7 *) rule;
1294
1295         /* Used to copy commands */
1296         ipfw_insn *ccmd, *dst;
1297         int ll = 0, ccmdlen = 0;
1298
1299         /* Copy of original rule */
1300         struct ip_fw7 *tmp = malloc(RULE_MAXSIZE, M_TEMP, M_NOWAIT | M_ZERO);
1301         if (tmp == NULL) {
1302                 return 1; //XXX error
1303         }
1304
1305         bcopy(rule7, tmp, RULE_MAXSIZE);
1306
1307         for (ll = tmp->cmd_len, ccmd = tmp->cmd, dst = rule->cmd ;
1308                         ll > 0 ; ll -= ccmdlen, ccmd += ccmdlen, dst += ccmdlen) {
1309                 ccmdlen = F_LEN(ccmd);
1310                 
1311                 bcopy(ccmd, dst, F_LEN(ccmd)*sizeof(uint32_t));
1312
1313                 if (dst->opcode > O_NAT)
1314                         /* O_REASS doesn't exists in 7.2 version, so
1315                          * increment opcode if it is after O_REASS
1316                          */
1317                         dst->opcode++;
1318
1319                 if (ccmdlen > ll) {
1320                         printf("ipfw: opcode %d size truncated\n",
1321                             ccmd->opcode);
1322                         return EINVAL;
1323                 }
1324         }
1325
1326         rule->_pad = tmp->_pad;
1327         rule->set = tmp->set;
1328         rule->rulenum = tmp->rulenum;
1329         rule->cmd_len = tmp->cmd_len;
1330         rule->act_ofs = tmp->act_ofs;
1331         rule->next_rule = (struct ip_fw *)tmp->next_rule;
1332         rule->x_next = (struct ip_fw *)tmp->next;
1333         rule->cmd_len = tmp->cmd_len;
1334         rule->id = 0; /* XXX see if is ok = 0 */
1335         rule->pcnt = tmp->pcnt;
1336         rule->bcnt = tmp->bcnt;
1337         rule->timestamp = tmp->timestamp;
1338
1339         free (tmp, M_TEMP);
1340         return 0;
1341 }
1342
1343 /* end of file */