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