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