1 /* 2 * Copyright (c) 2014 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Bill Yuan <bycn82@dragonflybsd.org> 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 3. Neither the name of The DragonFly Project nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific, prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <ctype.h> 36 #include <err.h> 37 #include <errno.h> 38 #include <grp.h> 39 #include <limits.h> 40 #include <netdb.h> 41 #include <pwd.h> 42 #include <signal.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <stdarg.h> 46 #include <string.h> 47 #include <sysexits.h> 48 #include <timeconv.h> 49 #include <unistd.h> 50 51 #include <netinet/in.h> 52 53 #include <arpa/inet.h> 54 #include <net/if.h> 55 #include <net/route.h> 56 #include <net/pfil.h> 57 58 #include "../../../sys/net/ipfw3/ip_fw3.h" 59 #include "../../../sbin/ipfw3/ipfw3.h" 60 #include "ipfw3_basic.h" 61 62 63 #define IP_MASK_ALL 0xffffffff 64 /* 65 * we use IPPROTO_ETHERTYPE as a fake protocol id to call the print routines 66 * This is only used in this code. 67 */ 68 #define IPPROTO_ETHERTYPE 0x1000 69 70 71 struct char_int_map limit_types[] = { 72 { "src-addr", 1 }, 73 { "src-port", 2 }, 74 { "dst-addr", 3 }, 75 { "dst-port", 4 }, 76 { NULL, 0 } 77 }; 78 79 static struct char_int_map ether_types[] = { 80 { "ip", 0x0800 }, 81 { "ipv4", 0x0800 }, 82 { "ipv6", 0x86dd }, 83 { "arp", 0x0806 }, 84 { "rarp", 0x8035 }, 85 { "vlan", 0x8100 }, 86 { "loop", 0x9000 }, 87 { "trail", 0x1000 }, 88 { "pppoe_disc", 0x8863 }, 89 { "pppoe_sess", 0x8864 }, 90 { "ipx_8022", 0x00E0 }, 91 { "ipx_8023", 0x0000 }, 92 { "ipx_ii", 0x8137 }, 93 { "ipx_snap", 0x8137 }, 94 { "ipx", 0x8137 }, 95 { "ns", 0x0600 }, 96 { NULL, 0 } 97 }; 98 99 100 101 static char * 102 match_token2(struct char_int_map *table, int val) 103 { 104 while (table->val) { 105 if (table->val == val) 106 return table->key; 107 108 table++; 109 } 110 return NULL; 111 }; 112 113 static void 114 fill_iface(ipfw_insn_if *cmd, char *arg) 115 { 116 cmd->name[0] = '\0'; 117 cmd->o.len |= F_INSN_SIZE(ipfw_insn_if); 118 119 /* Parse the interface or address */ 120 if (!strcmp(arg, "any")){ 121 cmd->o.len = 0; 122 } else if (!isdigit(*arg)) { 123 strlcpy(cmd->name, arg, sizeof(cmd->name)); 124 cmd->p.glob = strpbrk(arg, "*?[") != NULL ? 1 : 0; 125 } else if (!inet_aton(arg, &cmd->p.ip)) 126 errx(EX_DATAERR, "bad ip address ``%s''", arg); 127 } 128 129 static int 130 lookup_host (char *host, struct in_addr *ipaddr) 131 { 132 struct hostent *he; 133 134 if (!inet_aton(host, ipaddr)) { 135 if ((he = gethostbyname(host)) == NULL) 136 return -1; 137 *ipaddr = *(struct in_addr *)he->h_addr_list[0]; 138 } 139 return 0; 140 } 141 142 /* 143 * Like strtol, but also translates service names into port numbers 144 * for some protocols. 145 * In particular: 146 * proto == -1 disables the protocol check; 147 * proto == IPPROTO_ETHERTYPE looks up an internal table 148 * proto == <some value in /etc/protocols> matches the values there. 149 * Returns *end == s in case the parameter is not found. 150 */ 151 static int 152 strtoport(char *s, char **end, int base, int proto) 153 { 154 char *p, *buf; 155 char *s1; 156 int i; 157 158 *end = s; /* default - not found */ 159 if ( *s == '\0') 160 return 0; /* not found */ 161 162 if (isdigit(*s)) 163 return strtol(s, end, base); 164 165 /* 166 * find separator. '\\' escapes the next char. 167 */ 168 for (s1 = s; *s1 && (isalnum(*s1) || *s1 == '\\') ; s1++) { 169 if (*s1 == '\\' && s1[1] != '\0') 170 s1++; 171 } 172 173 buf = malloc(s1 - s + 1); 174 if (buf == NULL) 175 return 0; 176 177 /* 178 * copy into a buffer skipping backslashes 179 */ 180 for (p = s, i = 0; p != s1 ; p++) 181 if ( *p != '\\') 182 buf[i++] = *p; 183 buf[i++] = '\0'; 184 185 if (proto == IPPROTO_ETHERTYPE) { 186 i = match_token(ether_types, buf); 187 free(buf); 188 if (i != -1) { /* found */ 189 *end = s1; 190 return i; 191 } 192 } else { 193 struct protoent *pe = NULL; 194 struct servent *se; 195 196 if (proto != 0) 197 pe = getprotobynumber(proto); 198 setservent(1); 199 se = getservbyname(buf, pe ? pe->p_name : NULL); 200 free(buf); 201 if (se != NULL) { 202 *end = s1; 203 return ntohs(se->s_port); 204 } 205 } 206 return 0; /* not found */ 207 } 208 209 static int 210 contigmask(u_char *p, int len) 211 { 212 int i, n; 213 for (i=0; i<len ; i++) { 214 if ( (p[i/8] & (1 << (7 - (i%8)))) == 0) /* first bit unset */ 215 break; 216 } 217 for (n=i+1; n < len; n++) { 218 if ( (p[n/8] & (1 << (7 - (n%8)))) != 0) 219 return -1; /* mask not contiguous */ 220 } 221 return i; 222 } 223 224 static ipfw_insn *add_proto(ipfw_insn *cmd, char *av) 225 { 226 struct protoent *pe; 227 u_char proto = 0; 228 if (!strncmp(av, "all", strlen(av))) { 229 ; 230 } else if ((proto = atoi(av)) > 0) { 231 ; 232 } else if ((pe = getprotobyname(av)) != NULL) { 233 proto = pe->p_proto; 234 } else { 235 errx(EX_USAGE, "protocol `%s' not recognizable\n", av); 236 } 237 if (proto != IPPROTO_IP) { 238 cmd->opcode = O_BASIC_PROTO; 239 cmd->module = MODULE_BASIC_ID; 240 cmd->len = cmd->len|LEN_OF_IPFWINSN; 241 cmd->arg1 = proto; 242 } 243 return cmd; 244 } 245 246 void 247 parse_count(ipfw_insn **cmd, int *ac, char **av[]) 248 { 249 (*cmd)->opcode = O_BASIC_COUNT; 250 (*cmd)->module = MODULE_BASIC_ID; 251 (*cmd)->len = LEN_OF_IPFWINSN; 252 NEXT_ARG1; 253 } 254 255 void 256 parse_skipto(ipfw_insn **cmd, int *ac, char **av[]) 257 { 258 NEXT_ARG1; 259 (*cmd)->opcode = O_BASIC_SKIPTO; 260 (*cmd)->module = MODULE_BASIC_ID; 261 (*cmd)->len = LEN_OF_IPFWINSN; 262 (*cmd)->arg1 = strtoul(**av, NULL, 10); 263 NEXT_ARG1; 264 } 265 266 /* 267 * cmd->arg3 is count of the destination 268 * cmd->arg1 is the type, random 0, round-robin 1, sticky 2 269 */ 270 void 271 parse_forward(ipfw_insn **cmd, int *ac, char **av[]) 272 { 273 ipfw_insn_sa *p = (ipfw_insn_sa *)(*cmd); 274 struct sockaddr_in *sa; 275 char *tok, *end = '\0'; 276 char *str; 277 int count, port; 278 279 (*cmd)->opcode = O_BASIC_FORWARD; 280 NEXT_ARG1; 281 /* 282 * multiple forward destinations are seperated by colon 283 * ip address and port are seperated by comma 284 * e.g. 192.168.1.1:80,192.168.1.2:8080 285 * 192.168.1.1,192.168.1.2 or keep the port the same 286 */ 287 tok = strtok(**av, ","); 288 sa = &p->sa; 289 count = 0; 290 while (tok != NULL) { 291 sa->sin_len = sizeof(struct sockaddr_in); 292 sa->sin_family = AF_INET; 293 sa->sin_port = 0; 294 str = strchr(tok,':'); 295 if (str != NULL) { 296 *(str++) = '\0'; 297 port = strtoport(str, &end, 0, 0); 298 sa->sin_port = (u_short)port; 299 } 300 if (lookup_host(tok, &(sa->sin_addr)) != 0) 301 errx(EX_DATAERR, "forward `%s' invalid dst", tok); 302 tok = strtok (NULL, ","); 303 sa++; 304 count++; 305 } 306 (*cmd)->arg3 = count; 307 if (count == 0) { 308 errx(EX_DATAERR, "forward `%s' not recognizable", **av); 309 } 310 NEXT_ARG1; 311 if (count > 1) { 312 if (strcmp(**av, "round-robin") == 0) { 313 NEXT_ARG1; 314 (*cmd)->arg1 = 1; 315 } else if (strcmp(**av, "sticky") == 0) { 316 NEXT_ARG1; 317 (*cmd)->arg1 = 2; 318 } else { 319 /* random */ 320 (*cmd)->arg1 = 0; 321 } 322 } 323 (*cmd)->len = LEN_OF_IPFWINSN + count * sizeof(struct sockaddr_in); 324 } 325 326 void 327 parse_in(ipfw_insn **cmd, int *ac, char **av[]) 328 { 329 (*cmd)->opcode = O_BASIC_IN; 330 (*cmd)->module = MODULE_BASIC_ID; 331 (*cmd)->len = LEN_OF_IPFWINSN; 332 (*cmd)->arg1 = 0; 333 NEXT_ARG1; 334 } 335 336 void 337 parse_out(ipfw_insn **cmd, int *ac, char **av[]) 338 { 339 (*cmd)->opcode = O_BASIC_OUT; 340 (*cmd)->module = MODULE_BASIC_ID; 341 (*cmd)->len = LEN_OF_IPFWINSN; 342 (*cmd)->arg1 = 0; 343 NEXT_ARG1; 344 } 345 346 347 void 348 parse_via(ipfw_insn **cmd, int *ac, char **av[]) 349 { 350 (*cmd)->module = MODULE_BASIC_ID; 351 (*cmd)->len = LEN_OF_IPFWINSN; 352 if (strcmp(*av[0], "via")==0) { 353 (*cmd)->opcode = O_BASIC_VIA; 354 } else if (strcmp(*av[0], "xmit")==0) { 355 (*cmd)->opcode = O_BASIC_XMIT; 356 } else if (strcmp(*av[0], "recv")==0) { 357 (*cmd)->opcode = O_BASIC_RECV; 358 } 359 NEXT_ARG1; 360 fill_iface((ipfw_insn_if *)(*cmd), *av[0]); 361 NEXT_ARG1; 362 } 363 364 void 365 parse_src_port(ipfw_insn **cmd, int *ac, char **av[]) 366 { 367 368 NEXT_ARG1; 369 (*cmd)->opcode = O_BASIC_IP_SRCPORT; 370 (*cmd)->module = MODULE_BASIC_ID; 371 (*cmd)->len = LEN_OF_IPFWINSN; 372 double v = strtol(**av, NULL, 0); 373 if (v <= 0 || v >= 65535) 374 errx(EX_NOHOST, "port `%s' invalid", **av); 375 (*cmd)->arg1 = v; 376 NEXT_ARG1; 377 } 378 379 void 380 parse_dst_port(ipfw_insn **cmd, int *ac, char **av[]) 381 { 382 NEXT_ARG1; 383 (*cmd)->opcode = O_BASIC_IP_DSTPORT; 384 (*cmd)->module = MODULE_BASIC_ID; 385 (*cmd)->len = LEN_OF_IPFWINSN; 386 double v = strtol(**av, NULL, 0); 387 if (v <= 0 || v >= 65535) 388 errx(EX_NOHOST, "port `%s' invalid", **av); 389 (*cmd)->arg1 = v; 390 NEXT_ARG1; 391 } 392 393 /* 394 * Below formats are supported: 395 * from table 1 O_BASIC_IP_SRC_LOOKUP 396 * from any return 0 len instruction 397 * from me O_BASIC_IP_SRC_ME 398 * from 1.2.3.4 O_BASIC_IP_SRC 399 * from 1.2.3.4/24 O_BASIC_IP_SRC_MASK 400 */ 401 void 402 parse_from(ipfw_insn **cmd, int *ac, char **av[]) 403 { 404 ipfw_insn_ip *p = (ipfw_insn_ip *)(*cmd); 405 double port; 406 int i; 407 408 (*cmd)->module = MODULE_BASIC_ID; 409 NEXT_ARG1; 410 if (strcmp(**av, "table") == 0) { 411 NEXT_ARG1; 412 NEED(*ac, 1, "table id missing"); 413 (*cmd)->len = F_INSN_SIZE(ipfw_insn); 414 (*cmd)->opcode = O_BASIC_IP_SRC_LOOKUP; 415 (*cmd)->arg1 = strtoul(**av, NULL, 10); 416 } else if (strcmp(**av, "any") == 0) { 417 (*cmd)->len &= ~F_LEN_MASK; 418 } else if (strcmp(**av, "me") == 0) { 419 (*cmd)->len |= F_INSN_SIZE(ipfw_insn); 420 (*cmd)->opcode = O_BASIC_IP_SRC_ME; 421 } else { 422 char *c = NULL, md = 0; 423 c = strchr(**av, '/'); 424 if (!c) 425 c = strchr(**av, ':'); 426 if (c) { 427 md = *c; 428 *c++ = '\0'; 429 } 430 if (lookup_host(**av, &p->addr) != 0) 431 errx(EX_NOHOST, "hostname ``%s'' unknown", **av); 432 switch (md) { 433 case ':': 434 port = strtol(c, NULL, 0); 435 if (port <= 0 || port >= 65535) 436 errx(EX_NOHOST, "port `%s' invalid", c); 437 (*cmd)->arg1 = port; 438 (*cmd)->len |= F_INSN_SIZE(ipfw_insn_ip); 439 (*cmd)->opcode = O_BASIC_IP_SRC_N_PORT; 440 break; 441 case '/': 442 i = atoi(c); 443 if (i == 0) 444 p->mask.s_addr = htonl(0); 445 else if (i > 32) 446 errx(EX_DATAERR, "bad width ``%s''", c); 447 else 448 p->mask.s_addr = htonl(~0 << (32 - i)); 449 (*cmd)->len |= F_INSN_SIZE(ipfw_insn_ip); 450 (*cmd)->opcode = O_BASIC_IP_SRC_MASK; 451 p->addr.s_addr &= p->mask.s_addr; 452 break; 453 default: 454 p->mask.s_addr = htonl(~0); 455 (*cmd)->len |= F_INSN_SIZE(ipfw_insn_u32); 456 (*cmd)->opcode = O_BASIC_IP_SRC; 457 break; 458 } 459 } 460 NEXT_ARG1; 461 } 462 463 void 464 parse_to(ipfw_insn **cmd, int *ac, char **av[]) 465 { 466 ipfw_insn_ip *p = (ipfw_insn_ip *)(*cmd); 467 double port; 468 int i; 469 470 (*cmd)->module = MODULE_BASIC_ID; 471 NEXT_ARG1; 472 if (strcmp(**av, "table") == 0) { 473 NEXT_ARG1; 474 NEED(*ac, 1, "table id missing"); 475 (*cmd)->len = F_INSN_SIZE(ipfw_insn); 476 (*cmd)->opcode = O_BASIC_IP_DST_LOOKUP; 477 (*cmd)->arg1 = strtoul(**av, NULL, 10); 478 } else if (strcmp(**av, "any") == 0) { 479 (*cmd)->len &= ~F_LEN_MASK; 480 } else if (strcmp(**av, "me") == 0) { 481 (*cmd)->len |= F_INSN_SIZE(ipfw_insn); 482 (*cmd)->opcode = O_BASIC_IP_DST_ME; 483 } else { 484 char *c = NULL, md = 0; 485 c = strchr(**av, '/'); 486 if (!c) 487 c = strchr(**av, ':'); 488 if (c) { 489 md = *c; 490 *c++ = '\0'; 491 } 492 if (lookup_host(**av, &p->addr) != 0) 493 errx(EX_NOHOST, "hostname ``%s'' unknown", **av); 494 switch (md) { 495 case ':': 496 port = strtol(c, NULL, 0); 497 if (port <= 0 || port >= 65535) 498 errx(EX_NOHOST, "port `%s' invalid", c); 499 (*cmd)->arg1 = port; 500 (*cmd)->len |= F_INSN_SIZE(ipfw_insn_ip); 501 (*cmd)->opcode = O_BASIC_IP_DST_N_PORT; 502 break; 503 case '/': 504 i = atoi(c); 505 if (i == 0) 506 p->mask.s_addr = htonl(0); 507 else if (i > 32) 508 errx(EX_DATAERR, "bad width ``%s''", c); 509 else 510 p->mask.s_addr = htonl(~0 << (32 - i)); 511 (*cmd)->len |= F_INSN_SIZE(ipfw_insn_ip); 512 (*cmd)->opcode = O_BASIC_IP_DST_MASK; 513 p->addr.s_addr &= p->mask.s_addr; 514 break; 515 default: 516 p->mask.s_addr = htonl(~0); 517 (*cmd)->len |= F_INSN_SIZE(ipfw_insn_u32); 518 (*cmd)->opcode = O_BASIC_IP_DST; 519 break; 520 } 521 } 522 NEXT_ARG1; 523 524 } 525 526 void 527 parse_proto(ipfw_insn **cmd, int *ac, char **av[]) 528 { 529 add_proto(*cmd, **av); 530 NEXT_ARG1; 531 } 532 533 void 534 parse_prob(ipfw_insn **cmd, int *ac, char **av[]) 535 { 536 NEXT_ARG1; 537 (*cmd)->opcode = O_BASIC_PROB; 538 (*cmd)->module = MODULE_BASIC_ID; 539 (*cmd)->len = LEN_OF_IPFWINSN; 540 (*cmd)->arg1 = strtoul(**av, NULL, 10); 541 NEXT_ARG1; 542 } 543 544 void 545 parse_keep_state(ipfw_insn **cmd, int *ac, char **av[]) 546 { 547 NEXT_ARG1; 548 (*cmd)->opcode = O_BASIC_KEEP_STATE; 549 (*cmd)->module = MODULE_BASIC_ID; 550 (*cmd)->len = LEN_OF_IPFWINSN; 551 if (strcmp(**av, "limit") == 0) { 552 NEXT_ARG1; 553 (*cmd)->arg3 = match_token(limit_types, **av); 554 if ((*cmd)->arg3 == 0) 555 errx(EX_DATAERR, "limit `%s' not recognizable", **av); 556 557 NEXT_ARG1; 558 (*cmd)->arg1 = strtoul(**av, NULL, 10); 559 if ((*cmd)->arg1 == 0) 560 errx(EX_DATAERR, "bad limit `%s'", **av); 561 562 NEXT_ARG1; 563 } 564 if (strcmp(**av, "live") == 0) { 565 NEXT_ARG1; 566 (*cmd)->arg2 = strtoul(**av, NULL, 10); 567 NEXT_ARG1; 568 } 569 } 570 571 void 572 parse_check_state(ipfw_insn **cmd, int *ac, char **av[]) 573 { 574 NEXT_ARG1; 575 (*cmd)->opcode = O_BASIC_CHECK_STATE; 576 (*cmd)->module = MODULE_BASIC_ID; 577 (*cmd)->len = LEN_OF_IPFWINSN; 578 } 579 580 void 581 parse_tagged(ipfw_insn **cmd, int *ac, char **av[]) 582 { 583 NEXT_ARG1; 584 (*cmd)->opcode = O_BASIC_TAGGED; 585 (*cmd)->module = MODULE_BASIC_ID; 586 (*cmd)->len = LEN_OF_IPFWINSN; 587 (*cmd)->arg1 = strtoul(**av, NULL, 10); 588 NEXT_ARG1; 589 } 590 591 void 592 parse_comment(ipfw_insn **cmd, int *ac, char **av[]) 593 { 594 int l = 0; 595 char *p = (char *)((*cmd) + 1); 596 597 NEXT_ARG1; 598 (*cmd)->opcode = O_BASIC_COMMENT; 599 (*cmd)->module = MODULE_BASIC_ID; 600 601 while (*ac > 0) { 602 l += strlen(**av) + 1; 603 if (l > 84) { 604 errx(EX_DATAERR, "comment too long (max 80 chars)"); 605 } 606 strcpy(p, **av); 607 p += strlen(**av); 608 *p++ = ' '; 609 NEXT_ARG1; 610 } 611 l = 1 + (l + 3) / 4; 612 (*cmd)->len = l; 613 *(--p) = '\0'; 614 } 615 616 void 617 parse_tag(ipfw_insn **cmd, int *ac, char **av[]) 618 { 619 NEXT_ARG1; 620 (*cmd)->opcode = O_BASIC_TAG; 621 (*cmd)->module = MODULE_BASIC_ID; 622 (*cmd)->len = LEN_OF_IPFWINSN; 623 (*cmd)->arg1 = strtoul(**av, NULL, 10); 624 NEXT_ARG1; 625 } 626 627 void 628 parse_untag(ipfw_insn **cmd, int *ac, char **av[]) 629 { 630 NEXT_ARG1; 631 (*cmd)->opcode = O_BASIC_UNTAG; 632 (*cmd)->module = MODULE_BASIC_ID; 633 (*cmd)->len = LEN_OF_IPFWINSN; 634 (*cmd)->arg1 = strtoul(**av, NULL, 10); 635 NEXT_ARG1; 636 } 637 638 void 639 show_count(ipfw_insn *cmd, int show_or) 640 { 641 printf(" count"); 642 } 643 644 void 645 show_skipto(ipfw_insn *cmd, int show_or) 646 { 647 printf(" skipto %u", cmd->arg1); 648 } 649 650 void 651 show_forward(ipfw_insn *cmd, int show_or) 652 { 653 struct sockaddr_in *sa; 654 int i; 655 656 ipfw_insn_sa *s = (ipfw_insn_sa *)cmd; 657 sa = &s->sa; 658 printf(" forward"); 659 for (i = 0; i < cmd->arg3; i++){ 660 if (i > 0) 661 printf(","); 662 else 663 printf(" "); 664 665 printf("%s", inet_ntoa(sa->sin_addr)); 666 if (sa->sin_port != 0) 667 printf(":%d", sa->sin_port); 668 669 sa++; 670 } 671 if (cmd->arg1 == 1) 672 printf(" round-robin"); 673 else if (cmd->arg1 == 2) 674 printf(" sticky"); 675 676 } 677 678 void 679 show_in(ipfw_insn *cmd, int show_or) 680 { 681 printf(" in"); 682 } 683 684 void 685 show_out(ipfw_insn *cmd, int show_or) 686 { 687 printf(" out"); 688 } 689 690 void 691 show_via(ipfw_insn *cmd, int show_or) 692 { 693 char *s; 694 ipfw_insn_if *cmdif = (ipfw_insn_if *)cmd; 695 696 if ((int)cmd->opcode == O_BASIC_XMIT) 697 s = "xmit"; 698 else if ((int)cmd->opcode == O_BASIC_RECV) 699 s = "recv"; 700 else if ((int)cmd->opcode == O_BASIC_VIA) 701 s = "via"; 702 else 703 s = "?huh?"; 704 if (show_or) 705 s = "or"; 706 if (cmdif->name[0] == '\0') 707 printf(" %s %s", s, inet_ntoa(cmdif->p.ip)); 708 709 printf(" %s %s", s, cmdif->name); 710 } 711 712 void 713 show_src_port(ipfw_insn *cmd, int show_or) 714 { 715 printf(" src-port %d", cmd->arg1); 716 } 717 718 void 719 show_dst_port(ipfw_insn *cmd, int show_or) 720 { 721 printf(" dst-port %d", cmd->arg1); 722 } 723 724 void 725 show_from(ipfw_insn *cmd, int show_or) 726 { 727 char *word = "from"; 728 if (show_or) 729 word = "or"; 730 printf(" %s %s", word, inet_ntoa(((ipfw_insn_ip *)cmd)->addr)); 731 } 732 733 void 734 show_from_lookup(ipfw_insn *cmd, int show_or) 735 { 736 char *word = "from"; 737 if (show_or) 738 word = "or"; 739 printf(" %s table %d", word, cmd->arg1); 740 } 741 742 void 743 show_from_me(ipfw_insn *cmd, int show_or) 744 { 745 char *word = "from"; 746 if (show_or) 747 word = "or"; 748 printf(" %s me", word); 749 } 750 751 void 752 show_from_mask(ipfw_insn *cmd, int show_or) 753 { 754 int mask; 755 char *word = "from"; 756 if (show_or) 757 word = "or"; 758 ipfw_insn_ip *p = (ipfw_insn_ip *)cmd; 759 printf(" %s %s", word, inet_ntoa(p->addr)); 760 761 mask = contigmask((u_char *)&(p->mask.s_addr), 32); 762 if (mask < 32) 763 printf("/%d", mask); 764 } 765 766 void 767 show_from_src_n_port(ipfw_insn *cmd, int show_or) 768 { 769 char *word = "from"; 770 if (show_or) 771 word = "or"; 772 ipfw_insn_ip *p = (ipfw_insn_ip *)cmd; 773 printf(" %s %s", word, inet_ntoa(p->addr)); 774 printf(":%d", cmd->arg1); 775 } 776 777 void 778 show_to(ipfw_insn *cmd, int show_or) 779 { 780 char *word = "to"; 781 if (show_or) 782 word = "or"; 783 ipfw_insn_ip *p = (ipfw_insn_ip *)cmd; 784 printf(" %s %s", word, inet_ntoa(p->addr)); 785 } 786 787 void 788 show_to_lookup(ipfw_insn *cmd, int show_or) 789 { 790 char *word = "to"; 791 if (show_or) 792 word = "or"; 793 printf(" %s table %d", word, cmd->arg1); 794 } 795 796 void 797 show_to_me(ipfw_insn *cmd, int show_or) 798 { 799 char *word = "to"; 800 if (show_or) 801 word = "or"; 802 printf(" %s me", word); 803 } 804 805 void 806 show_to_mask(ipfw_insn *cmd, int show_or) 807 { 808 int mask; 809 char *word = "to"; 810 if (show_or) 811 word = "or"; 812 ipfw_insn_ip *p = (ipfw_insn_ip *)cmd; 813 printf(" %s %s", word, inet_ntoa(p->addr)); 814 815 mask = contigmask((u_char *)&(p->mask.s_addr), 32); 816 if (mask < 32) 817 printf("/%d", mask); 818 } 819 820 void 821 show_to_src_n_port(ipfw_insn *cmd, int show_or) 822 { 823 char *word = "to"; 824 if (show_or) 825 word = "or"; 826 printf(" %s %s", word, inet_ntoa(((ipfw_insn_ip *)cmd)->addr)); 827 printf(":%d", cmd->arg1); 828 } 829 830 void 831 show_proto(ipfw_insn *cmd, int show_or) 832 { 833 struct protoent *pe; 834 u_char proto = 0; 835 proto = cmd->arg1; 836 pe = getprotobynumber(cmd->arg1); 837 printf(" %s", pe->p_name); 838 } 839 840 void 841 show_prob(ipfw_insn *cmd, int show_or) 842 { 843 printf(" prob %d%%", cmd->arg1); 844 } 845 846 void 847 show_keep_state(ipfw_insn *cmd, int show_or) 848 { 849 printf(" keep-state"); 850 if (cmd->arg1 != 0) { 851 char *type=match_token2(limit_types, cmd->arg3); 852 printf(" limit %s %d", type, cmd->arg1); 853 } 854 if (cmd->arg2 != 0) { 855 printf(" live %d", cmd->arg2); 856 } 857 } 858 859 void 860 show_check_state(ipfw_insn *cmd, int show_or) 861 { 862 printf(" check-state"); 863 } 864 865 void 866 show_tagged(ipfw_insn *cmd, int show_or) 867 { 868 printf(" tagged %d", cmd->arg1); 869 } 870 871 void 872 show_comment(ipfw_insn *cmd, int show_or) 873 { 874 printf(" // %s", (char *)(cmd + 1)); 875 } 876 877 void 878 show_tag(ipfw_insn *cmd, int show_or) 879 { 880 printf(" tag %d", cmd->arg1); 881 } 882 883 void 884 show_untag(ipfw_insn *cmd, int show_or) 885 { 886 printf(" untag %d", cmd->arg1); 887 } 888 889 void 890 load_module(register_func function, register_keyword keyword) 891 { 892 keyword(MODULE_BASIC_ID, O_BASIC_COUNT, "count", ACTION); 893 function(MODULE_BASIC_ID, O_BASIC_COUNT, 894 (parser_func)parse_count, (shower_func)show_count); 895 896 keyword(MODULE_BASIC_ID, O_BASIC_SKIPTO, "skipto", ACTION); 897 function(MODULE_BASIC_ID, O_BASIC_SKIPTO, 898 (parser_func)parse_skipto, (shower_func)show_skipto); 899 900 keyword(MODULE_BASIC_ID, O_BASIC_FORWARD, "forward", ACTION); 901 function(MODULE_BASIC_ID, O_BASIC_FORWARD, 902 (parser_func)parse_forward, (shower_func)show_forward); 903 904 keyword(MODULE_BASIC_ID, O_BASIC_IN, "in", FILTER); 905 function(MODULE_BASIC_ID, O_BASIC_IN, 906 (parser_func)parse_in, (shower_func)show_in); 907 908 keyword(MODULE_BASIC_ID, O_BASIC_OUT, "out", FILTER); 909 function(MODULE_BASIC_ID, O_BASIC_OUT, 910 (parser_func)parse_out, (shower_func)show_out); 911 912 keyword(MODULE_BASIC_ID, O_BASIC_VIA, "via", FILTER); 913 function(MODULE_BASIC_ID, O_BASIC_VIA, 914 (parser_func)parse_via, (shower_func)show_via); 915 916 keyword(MODULE_BASIC_ID, O_BASIC_XMIT, "xmit", FILTER); 917 function(MODULE_BASIC_ID, O_BASIC_XMIT, 918 (parser_func)parse_via, (shower_func)show_via); 919 920 keyword(MODULE_BASIC_ID, O_BASIC_RECV, "recv", FILTER); 921 function(MODULE_BASIC_ID, O_BASIC_RECV, 922 (parser_func)parse_via, (shower_func)show_via); 923 924 keyword(MODULE_BASIC_ID, O_BASIC_IP_SRCPORT, "src-port", FILTER); 925 function(MODULE_BASIC_ID, O_BASIC_IP_SRCPORT, 926 (parser_func)parse_src_port, (shower_func)show_src_port); 927 928 keyword(MODULE_BASIC_ID, O_BASIC_IP_DSTPORT, "dst-port", FILTER); 929 function(MODULE_BASIC_ID, O_BASIC_IP_DSTPORT, 930 (parser_func)parse_dst_port, (shower_func)show_dst_port); 931 932 keyword(MODULE_BASIC_ID, O_BASIC_IP_SRC, "from", FROM); 933 function(MODULE_BASIC_ID, O_BASIC_IP_SRC, 934 (parser_func)parse_from, (shower_func)show_from); 935 936 keyword(MODULE_BASIC_ID, O_BASIC_IP_SRC_LOOKUP, "from-[table]", FROM); 937 function(MODULE_BASIC_ID, O_BASIC_IP_SRC_LOOKUP, 938 (parser_func)parse_from, (shower_func)show_from_lookup); 939 940 keyword(MODULE_BASIC_ID, O_BASIC_IP_SRC_ME, "from-[me]", FROM); 941 function(MODULE_BASIC_ID, O_BASIC_IP_SRC_ME, 942 (parser_func)parse_from, (shower_func)show_from_me); 943 944 keyword(MODULE_BASIC_ID, O_BASIC_IP_SRC_MASK, "from-[mask]", FROM); 945 function(MODULE_BASIC_ID, O_BASIC_IP_SRC_MASK, 946 (parser_func)parse_from, (shower_func)show_from_mask); 947 948 keyword(MODULE_BASIC_ID, O_BASIC_IP_SRC_N_PORT, "from-[ip:port]", FROM); 949 function(MODULE_BASIC_ID, O_BASIC_IP_SRC_N_PORT, 950 (parser_func)parse_from, (shower_func)show_from_src_n_port); 951 952 keyword(MODULE_BASIC_ID, O_BASIC_IP_DST, "to", TO); 953 function(MODULE_BASIC_ID, O_BASIC_IP_DST, 954 (parser_func)parse_to, (shower_func)show_to); 955 956 keyword(MODULE_BASIC_ID, O_BASIC_IP_DST_LOOKUP, "to-[table]", TO); 957 function(MODULE_BASIC_ID, O_BASIC_IP_DST_LOOKUP, 958 (parser_func)parse_to, (shower_func)show_to_lookup); 959 960 keyword(MODULE_BASIC_ID, O_BASIC_IP_DST_ME, "to-[me]", TO); 961 function(MODULE_BASIC_ID, O_BASIC_IP_DST_ME, 962 (parser_func)parse_to, (shower_func)show_to_me); 963 964 keyword(MODULE_BASIC_ID, O_BASIC_IP_DST_MASK, "to-[mask]", TO); 965 function(MODULE_BASIC_ID, O_BASIC_IP_DST_MASK, 966 (parser_func)parse_to, (shower_func)show_to_mask); 967 968 keyword(MODULE_BASIC_ID, O_BASIC_IP_DST_N_PORT, "to-[ip:port]", FROM); 969 function(MODULE_BASIC_ID, O_BASIC_IP_DST_N_PORT, 970 (parser_func)parse_to, (shower_func)show_to_src_n_port); 971 972 keyword(MODULE_BASIC_ID, O_BASIC_PROTO, "proto", PROTO); 973 function(MODULE_BASIC_ID, O_BASIC_PROTO, 974 (parser_func)parse_proto, (shower_func)show_proto); 975 976 keyword(MODULE_BASIC_ID, O_BASIC_PROB, "prob", FILTER); 977 function(MODULE_BASIC_ID, O_BASIC_PROB, 978 (parser_func)parse_prob, (shower_func)show_prob); 979 980 keyword(MODULE_BASIC_ID, O_BASIC_KEEP_STATE, "keep-state", FILTER); 981 function(MODULE_BASIC_ID, O_BASIC_KEEP_STATE, 982 (parser_func)parse_keep_state, 983 (shower_func)show_keep_state); 984 985 keyword(MODULE_BASIC_ID, O_BASIC_CHECK_STATE, "check-state", BEFORE); 986 function(MODULE_BASIC_ID, O_BASIC_CHECK_STATE, 987 (parser_func)parse_check_state, 988 (shower_func)show_check_state); 989 990 keyword(MODULE_BASIC_ID, O_BASIC_TAG, "tag", ACTION); 991 function(MODULE_BASIC_ID, O_BASIC_TAG, 992 (parser_func)parse_tag, (shower_func)show_tag); 993 994 keyword(MODULE_BASIC_ID, O_BASIC_UNTAG, "untag", ACTION); 995 function(MODULE_BASIC_ID, O_BASIC_UNTAG, 996 (parser_func)parse_untag, (shower_func)show_untag); 997 998 keyword(MODULE_BASIC_ID, O_BASIC_TAGGED, "tagged", FILTER); 999 function(MODULE_BASIC_ID, O_BASIC_TAGGED, 1000 (parser_func)parse_tagged, (shower_func)show_tagged); 1001 1002 keyword(MODULE_BASIC_ID, O_BASIC_COMMENT, "//", AFTER); 1003 function(MODULE_BASIC_ID, O_BASIC_COMMENT, 1004 (parser_func)parse_comment, (shower_func)show_comment); 1005 } 1006