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