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