ipfw2.c revision 145567
1/* 2 * Copyright (c) 2002-2003 Luigi Rizzo 3 * Copyright (c) 1996 Alex Nash, Paul Traina, Poul-Henning Kamp 4 * Copyright (c) 1994 Ugen J.S.Antsilevich 5 * 6 * Idea and grammar partially left from: 7 * Copyright (c) 1993 Daniel Boulet 8 * 9 * Redistribution and use in source forms, with and without modification, 10 * are permitted provided that this entire comment appears intact. 11 * 12 * Redistribution in binary form may occur without any restrictions. 13 * Obviously, it would be nice if you gave credit where credit is due 14 * but requiring it would be too onerous. 15 * 16 * This software is provided ``AS IS'' without any warranties of any kind. 17 * 18 * NEW command line interface for IP firewall facility 19 * 20 * $FreeBSD: head/sbin/ipfw/ipfw2.c 145567 2005-04-26 20:22:31Z brooks $ 21 */ 22 23#include <sys/param.h> 24#include <sys/mbuf.h> 25#include <sys/socket.h> 26#include <sys/sockio.h> 27#include <sys/sysctl.h> 28#include <sys/time.h> 29#include <sys/wait.h> 30#include <sys/queue.h> 31 32#include <ctype.h> 33#include <err.h> 34#include <errno.h> 35#include <grp.h> 36#include <limits.h> 37#include <netdb.h> 38#include <pwd.h> 39#include <signal.h> 40#include <stdio.h> 41#include <stdlib.h> 42#include <stdarg.h> 43#include <string.h> 44#include <timeconv.h> /* XXX do we need this ? */ 45#include <unistd.h> 46#include <sysexits.h> 47#include <unistd.h> 48#include <fcntl.h> 49 50#include <net/if.h> 51#include <net/pfvar.h> 52#include <net/route.h> /* def. of struct route */ 53#include <netinet/in.h> 54#include <netinet/in_systm.h> 55#include <netinet/ip.h> 56#include <netinet/ip_icmp.h> 57#include <netinet/icmp6.h> 58#include <netinet/ip_fw.h> 59#include <netinet/ip_dummynet.h> 60#include <netinet/tcp.h> 61#include <arpa/inet.h> 62 63int 64 do_resolv, /* Would try to resolve all */ 65 do_time, /* Show time stamps */ 66 do_quiet, /* Be quiet in add and flush */ 67 do_pipe, /* this cmd refers to a pipe */ 68 do_sort, /* field to sort results (0 = no) */ 69 do_dynamic, /* display dynamic rules */ 70 do_expired, /* display expired dynamic rules */ 71 do_compact, /* show rules in compact mode */ 72 do_force, /* do not ask for confirmation */ 73 show_sets, /* display rule sets */ 74 test_only, /* only check syntax */ 75 comment_only, /* only print action and comment */ 76 verbose; 77 78#define IP_MASK_ALL 0xffffffff 79/* 80 * the following macro returns an error message if we run out of 81 * arguments. 82 */ 83#define NEED1(msg) {if (!ac) errx(EX_USAGE, msg);} 84 85/* 86 * _s_x is a structure that stores a string <-> token pairs, used in 87 * various places in the parser. Entries are stored in arrays, 88 * with an entry with s=NULL as terminator. 89 * The search routines are match_token() and match_value(). 90 * Often, an element with x=0 contains an error string. 91 * 92 */ 93struct _s_x { 94 char const *s; 95 int x; 96}; 97 98static struct _s_x f_tcpflags[] = { 99 { "syn", TH_SYN }, 100 { "fin", TH_FIN }, 101 { "ack", TH_ACK }, 102 { "psh", TH_PUSH }, 103 { "rst", TH_RST }, 104 { "urg", TH_URG }, 105 { "tcp flag", 0 }, 106 { NULL, 0 } 107}; 108 109static struct _s_x f_tcpopts[] = { 110 { "mss", IP_FW_TCPOPT_MSS }, 111 { "maxseg", IP_FW_TCPOPT_MSS }, 112 { "window", IP_FW_TCPOPT_WINDOW }, 113 { "sack", IP_FW_TCPOPT_SACK }, 114 { "ts", IP_FW_TCPOPT_TS }, 115 { "timestamp", IP_FW_TCPOPT_TS }, 116 { "cc", IP_FW_TCPOPT_CC }, 117 { "tcp option", 0 }, 118 { NULL, 0 } 119}; 120 121/* 122 * IP options span the range 0 to 255 so we need to remap them 123 * (though in fact only the low 5 bits are significant). 124 */ 125static struct _s_x f_ipopts[] = { 126 { "ssrr", IP_FW_IPOPT_SSRR}, 127 { "lsrr", IP_FW_IPOPT_LSRR}, 128 { "rr", IP_FW_IPOPT_RR}, 129 { "ts", IP_FW_IPOPT_TS}, 130 { "ip option", 0 }, 131 { NULL, 0 } 132}; 133 134static struct _s_x f_iptos[] = { 135 { "lowdelay", IPTOS_LOWDELAY}, 136 { "throughput", IPTOS_THROUGHPUT}, 137 { "reliability", IPTOS_RELIABILITY}, 138 { "mincost", IPTOS_MINCOST}, 139 { "congestion", IPTOS_CE}, 140 { "ecntransport", IPTOS_ECT}, 141 { "ip tos option", 0}, 142 { NULL, 0 } 143}; 144 145static struct _s_x limit_masks[] = { 146 {"all", DYN_SRC_ADDR|DYN_SRC_PORT|DYN_DST_ADDR|DYN_DST_PORT}, 147 {"src-addr", DYN_SRC_ADDR}, 148 {"src-port", DYN_SRC_PORT}, 149 {"dst-addr", DYN_DST_ADDR}, 150 {"dst-port", DYN_DST_PORT}, 151 {NULL, 0} 152}; 153 154/* 155 * we use IPPROTO_ETHERTYPE as a fake protocol id to call the print routines 156 * This is only used in this code. 157 */ 158#define IPPROTO_ETHERTYPE 0x1000 159static struct _s_x ether_types[] = { 160 /* 161 * Note, we cannot use "-:&/" in the names because they are field 162 * separators in the type specifications. Also, we use s = NULL as 163 * end-delimiter, because a type of 0 can be legal. 164 */ 165 { "ip", 0x0800 }, 166 { "ipv4", 0x0800 }, 167 { "ipv6", 0x86dd }, 168 { "arp", 0x0806 }, 169 { "rarp", 0x8035 }, 170 { "vlan", 0x8100 }, 171 { "loop", 0x9000 }, 172 { "trail", 0x1000 }, 173 { "at", 0x809b }, 174 { "atalk", 0x809b }, 175 { "aarp", 0x80f3 }, 176 { "pppoe_disc", 0x8863 }, 177 { "pppoe_sess", 0x8864 }, 178 { "ipx_8022", 0x00E0 }, 179 { "ipx_8023", 0x0000 }, 180 { "ipx_ii", 0x8137 }, 181 { "ipx_snap", 0x8137 }, 182 { "ipx", 0x8137 }, 183 { "ns", 0x0600 }, 184 { NULL, 0 } 185}; 186 187static void show_usage(void); 188 189enum tokens { 190 TOK_NULL=0, 191 192 TOK_OR, 193 TOK_NOT, 194 TOK_STARTBRACE, 195 TOK_ENDBRACE, 196 197 TOK_ACCEPT, 198 TOK_COUNT, 199 TOK_PIPE, 200 TOK_QUEUE, 201 TOK_DIVERT, 202 TOK_TEE, 203 TOK_NETGRAPH, 204 TOK_NGTEE, 205 TOK_FORWARD, 206 TOK_SKIPTO, 207 TOK_DENY, 208 TOK_REJECT, 209 TOK_RESET, 210 TOK_UNREACH, 211 TOK_CHECKSTATE, 212 213 TOK_ALTQ, 214 TOK_LOG, 215 216 TOK_UID, 217 TOK_GID, 218 TOK_JAIL, 219 TOK_IN, 220 TOK_LIMIT, 221 TOK_KEEPSTATE, 222 TOK_LAYER2, 223 TOK_OUT, 224 TOK_DIVERTED, 225 TOK_DIVERTEDLOOPBACK, 226 TOK_DIVERTEDOUTPUT, 227 TOK_XMIT, 228 TOK_RECV, 229 TOK_VIA, 230 TOK_FRAG, 231 TOK_IPOPTS, 232 TOK_IPLEN, 233 TOK_IPID, 234 TOK_IPPRECEDENCE, 235 TOK_IPTOS, 236 TOK_IPTTL, 237 TOK_IPVER, 238 TOK_ESTAB, 239 TOK_SETUP, 240 TOK_TCPDATALEN, 241 TOK_TCPFLAGS, 242 TOK_TCPOPTS, 243 TOK_TCPSEQ, 244 TOK_TCPACK, 245 TOK_TCPWIN, 246 TOK_ICMPTYPES, 247 TOK_MAC, 248 TOK_MACTYPE, 249 TOK_VERREVPATH, 250 TOK_VERSRCREACH, 251 TOK_ANTISPOOF, 252 TOK_IPSEC, 253 TOK_COMMENT, 254 255 TOK_PLR, 256 TOK_NOERROR, 257 TOK_BUCKETS, 258 TOK_DSTIP, 259 TOK_SRCIP, 260 TOK_DSTPORT, 261 TOK_SRCPORT, 262 TOK_ALL, 263 TOK_MASK, 264 TOK_BW, 265 TOK_DELAY, 266 TOK_RED, 267 TOK_GRED, 268 TOK_DROPTAIL, 269 TOK_PROTO, 270 TOK_WEIGHT, 271 272 TOK_IPV6, 273 TOK_FLOWID, 274 TOK_ICMP6TYPES, 275 TOK_EXT6HDR, 276 TOK_DSTIP6, 277 TOK_SRCIP6, 278}; 279 280struct _s_x dummynet_params[] = { 281 { "plr", TOK_PLR }, 282 { "noerror", TOK_NOERROR }, 283 { "buckets", TOK_BUCKETS }, 284 { "dst-ip", TOK_DSTIP }, 285 { "src-ip", TOK_SRCIP }, 286 { "dst-port", TOK_DSTPORT }, 287 { "src-port", TOK_SRCPORT }, 288 { "proto", TOK_PROTO }, 289 { "weight", TOK_WEIGHT }, 290 { "all", TOK_ALL }, 291 { "mask", TOK_MASK }, 292 { "droptail", TOK_DROPTAIL }, 293 { "red", TOK_RED }, 294 { "gred", TOK_GRED }, 295 { "bw", TOK_BW }, 296 { "bandwidth", TOK_BW }, 297 { "delay", TOK_DELAY }, 298 { "pipe", TOK_PIPE }, 299 { "queue", TOK_QUEUE }, 300 { "flow-id", TOK_FLOWID}, 301 { "dst-ipv6", TOK_DSTIP6}, 302 { "dst-ip6", TOK_DSTIP6}, 303 { "src-ipv6", TOK_SRCIP6}, 304 { "src-ip6", TOK_SRCIP6}, 305 { "dummynet-params", TOK_NULL }, 306 { NULL, 0 } /* terminator */ 307}; 308 309struct _s_x rule_actions[] = { 310 { "accept", TOK_ACCEPT }, 311 { "pass", TOK_ACCEPT }, 312 { "allow", TOK_ACCEPT }, 313 { "permit", TOK_ACCEPT }, 314 { "count", TOK_COUNT }, 315 { "pipe", TOK_PIPE }, 316 { "queue", TOK_QUEUE }, 317 { "divert", TOK_DIVERT }, 318 { "tee", TOK_TEE }, 319 { "netgraph", TOK_NETGRAPH }, 320 { "ngtee", TOK_NGTEE }, 321 { "fwd", TOK_FORWARD }, 322 { "forward", TOK_FORWARD }, 323 { "skipto", TOK_SKIPTO }, 324 { "deny", TOK_DENY }, 325 { "drop", TOK_DENY }, 326 { "reject", TOK_REJECT }, 327 { "reset", TOK_RESET }, 328 { "unreach", TOK_UNREACH }, 329 { "check-state", TOK_CHECKSTATE }, 330 { "//", TOK_COMMENT }, 331 { NULL, 0 } /* terminator */ 332}; 333 334struct _s_x rule_action_params[] = { 335 { "altq", TOK_ALTQ }, 336 { "log", TOK_LOG }, 337 { NULL, 0 } /* terminator */ 338}; 339 340struct _s_x rule_options[] = { 341 { "uid", TOK_UID }, 342 { "gid", TOK_GID }, 343 { "jail", TOK_JAIL }, 344 { "in", TOK_IN }, 345 { "limit", TOK_LIMIT }, 346 { "keep-state", TOK_KEEPSTATE }, 347 { "bridged", TOK_LAYER2 }, 348 { "layer2", TOK_LAYER2 }, 349 { "out", TOK_OUT }, 350 { "diverted", TOK_DIVERTED }, 351 { "diverted-loopback", TOK_DIVERTEDLOOPBACK }, 352 { "diverted-output", TOK_DIVERTEDOUTPUT }, 353 { "xmit", TOK_XMIT }, 354 { "recv", TOK_RECV }, 355 { "via", TOK_VIA }, 356 { "fragment", TOK_FRAG }, 357 { "frag", TOK_FRAG }, 358 { "ipoptions", TOK_IPOPTS }, 359 { "ipopts", TOK_IPOPTS }, 360 { "iplen", TOK_IPLEN }, 361 { "ipid", TOK_IPID }, 362 { "ipprecedence", TOK_IPPRECEDENCE }, 363 { "iptos", TOK_IPTOS }, 364 { "ipttl", TOK_IPTTL }, 365 { "ipversion", TOK_IPVER }, 366 { "ipver", TOK_IPVER }, 367 { "estab", TOK_ESTAB }, 368 { "established", TOK_ESTAB }, 369 { "setup", TOK_SETUP }, 370 { "tcpdatalen", TOK_TCPDATALEN }, 371 { "tcpflags", TOK_TCPFLAGS }, 372 { "tcpflgs", TOK_TCPFLAGS }, 373 { "tcpoptions", TOK_TCPOPTS }, 374 { "tcpopts", TOK_TCPOPTS }, 375 { "tcpseq", TOK_TCPSEQ }, 376 { "tcpack", TOK_TCPACK }, 377 { "tcpwin", TOK_TCPWIN }, 378 { "icmptype", TOK_ICMPTYPES }, 379 { "icmptypes", TOK_ICMPTYPES }, 380 { "dst-ip", TOK_DSTIP }, 381 { "src-ip", TOK_SRCIP }, 382 { "dst-port", TOK_DSTPORT }, 383 { "src-port", TOK_SRCPORT }, 384 { "proto", TOK_PROTO }, 385 { "MAC", TOK_MAC }, 386 { "mac", TOK_MAC }, 387 { "mac-type", TOK_MACTYPE }, 388 { "verrevpath", TOK_VERREVPATH }, 389 { "versrcreach", TOK_VERSRCREACH }, 390 { "antispoof", TOK_ANTISPOOF }, 391 { "ipsec", TOK_IPSEC }, 392 { "icmp6type", TOK_ICMP6TYPES }, 393 { "icmp6types", TOK_ICMP6TYPES }, 394 { "ext6hdr", TOK_EXT6HDR}, 395 { "flow-id", TOK_FLOWID}, 396 { "ipv6", TOK_IPV6}, 397 { "ip6", TOK_IPV6}, 398 { "dst-ipv6", TOK_DSTIP6}, 399 { "dst-ip6", TOK_DSTIP6}, 400 { "src-ipv6", TOK_SRCIP6}, 401 { "src-ip6", TOK_SRCIP6}, 402 { "//", TOK_COMMENT }, 403 404 { "not", TOK_NOT }, /* pseudo option */ 405 { "!", /* escape ? */ TOK_NOT }, /* pseudo option */ 406 { "or", TOK_OR }, /* pseudo option */ 407 { "|", /* escape */ TOK_OR }, /* pseudo option */ 408 { "{", TOK_STARTBRACE }, /* pseudo option */ 409 { "(", TOK_STARTBRACE }, /* pseudo option */ 410 { "}", TOK_ENDBRACE }, /* pseudo option */ 411 { ")", TOK_ENDBRACE }, /* pseudo option */ 412 { NULL, 0 } /* terminator */ 413}; 414 415static __inline uint64_t 416align_uint64(uint64_t *pll) { 417 uint64_t ret; 418 419 bcopy (pll, &ret, sizeof(ret)); 420 return ret; 421} 422 423/* 424 * conditionally runs the command. 425 */ 426static int 427do_cmd(int optname, void *optval, uintptr_t optlen) 428{ 429 static int s = -1; /* the socket */ 430 int i; 431 432 if (test_only) 433 return 0; 434 435 if (s == -1) 436 s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); 437 if (s < 0) 438 err(EX_UNAVAILABLE, "socket"); 439 440 if (optname == IP_FW_GET || optname == IP_DUMMYNET_GET || 441 optname == IP_FW_ADD || optname == IP_FW_TABLE_LIST || 442 optname == IP_FW_TABLE_GETSIZE) 443 i = getsockopt(s, IPPROTO_IP, optname, optval, 444 (socklen_t *)optlen); 445 else 446 i = setsockopt(s, IPPROTO_IP, optname, optval, optlen); 447 return i; 448} 449 450/** 451 * match_token takes a table and a string, returns the value associated 452 * with the string (-1 in case of failure). 453 */ 454static int 455match_token(struct _s_x *table, char *string) 456{ 457 struct _s_x *pt; 458 uint i = strlen(string); 459 460 for (pt = table ; i && pt->s != NULL ; pt++) 461 if (strlen(pt->s) == i && !bcmp(string, pt->s, i)) 462 return pt->x; 463 return -1; 464} 465 466/** 467 * match_value takes a table and a value, returns the string associated 468 * with the value (NULL in case of failure). 469 */ 470static char const * 471match_value(struct _s_x *p, int value) 472{ 473 for (; p->s != NULL; p++) 474 if (p->x == value) 475 return p->s; 476 return NULL; 477} 478 479/* 480 * _substrcmp takes two strings and returns 1 if they do not match, 481 * and 0 if they match exactly or the first string is a sub-string 482 * of the second. A warning is printed to stderr in the case that the 483 * first string is a sub-string of the second. 484 * 485 * This function will be removed in the future through the usual 486 * deprecation process. 487 */ 488static int 489_substrcmp(const char *str1, const char* str2) 490{ 491 492 if (strncmp(str1, str2, strlen(str1)) != 0) 493 return 1; 494 495 if (strlen(str1) != strlen(str2)) 496 warnx("DEPRECATED: '%s' matched '%s' as a sub-string", 497 str1, str2); 498 return 0; 499} 500 501/* 502 * _substrcmp2 takes three strings and returns 1 if the first two do not match, 503 * and 0 if they match exactly or the second string is a sub-string 504 * of the first. A warning is printed to stderr in the case that the 505 * first string does not match the third. 506 * 507 * This function exists to warn about the bizzare construction 508 * strncmp(str, "by", 2) which is used to allow people to use a shotcut 509 * for "bytes". The problem is that in addition to accepting "by", 510 * "byt", "byte", and "bytes", it also excepts "by_rabid_dogs" and any 511 * other string beginning with "by". 512 * 513 * This function will be removed in the future through the usual 514 * deprecation process. 515 */ 516static int 517_substrcmp2(const char *str1, const char* str2, const char* str3) 518{ 519 520 if (strncmp(str1, str2, strlen(str2)) != 0) 521 return 1; 522 523 if (strcmp(str1, str3) != 0) 524 warnx("DEPRECATED: '%s' matched '%s'", 525 str1, str3); 526 return 0; 527} 528 529/* 530 * prints one port, symbolic or numeric 531 */ 532static void 533print_port(int proto, uint16_t port) 534{ 535 536 if (proto == IPPROTO_ETHERTYPE) { 537 char const *s; 538 539 if (do_resolv && (s = match_value(ether_types, port)) ) 540 printf("%s", s); 541 else 542 printf("0x%04x", port); 543 } else { 544 struct servent *se = NULL; 545 if (do_resolv) { 546 struct protoent *pe = getprotobynumber(proto); 547 548 se = getservbyport(htons(port), pe ? pe->p_name : NULL); 549 } 550 if (se) 551 printf("%s", se->s_name); 552 else 553 printf("%d", port); 554 } 555} 556 557struct _s_x _port_name[] = { 558 {"dst-port", O_IP_DSTPORT}, 559 {"src-port", O_IP_SRCPORT}, 560 {"ipid", O_IPID}, 561 {"iplen", O_IPLEN}, 562 {"ipttl", O_IPTTL}, 563 {"mac-type", O_MAC_TYPE}, 564 {"tcpdatalen", O_TCPDATALEN}, 565 {NULL, 0} 566}; 567 568/* 569 * Print the values in a list 16-bit items of the types above. 570 * XXX todo: add support for mask. 571 */ 572static void 573print_newports(ipfw_insn_u16 *cmd, int proto, int opcode) 574{ 575 uint16_t *p = cmd->ports; 576 int i; 577 char const *sep; 578 579 if (cmd->o.len & F_NOT) 580 printf(" not"); 581 if (opcode != 0) { 582 sep = match_value(_port_name, opcode); 583 if (sep == NULL) 584 sep = "???"; 585 printf (" %s", sep); 586 } 587 sep = " "; 588 for (i = F_LEN((ipfw_insn *)cmd) - 1; i > 0; i--, p += 2) { 589 printf(sep); 590 print_port(proto, p[0]); 591 if (p[0] != p[1]) { 592 printf("-"); 593 print_port(proto, p[1]); 594 } 595 sep = ","; 596 } 597} 598 599/* 600 * Like strtol, but also translates service names into port numbers 601 * for some protocols. 602 * In particular: 603 * proto == -1 disables the protocol check; 604 * proto == IPPROTO_ETHERTYPE looks up an internal table 605 * proto == <some value in /etc/protocols> matches the values there. 606 * Returns *end == s in case the parameter is not found. 607 */ 608static int 609strtoport(char *s, char **end, int base, int proto) 610{ 611 char *p, *buf; 612 char *s1; 613 int i; 614 615 *end = s; /* default - not found */ 616 if (*s == '\0') 617 return 0; /* not found */ 618 619 if (isdigit(*s)) 620 return strtol(s, end, base); 621 622 /* 623 * find separator. '\\' escapes the next char. 624 */ 625 for (s1 = s; *s1 && (isalnum(*s1) || *s1 == '\\') ; s1++) 626 if (*s1 == '\\' && s1[1] != '\0') 627 s1++; 628 629 buf = malloc(s1 - s + 1); 630 if (buf == NULL) 631 return 0; 632 633 /* 634 * copy into a buffer skipping backslashes 635 */ 636 for (p = s, i = 0; p != s1 ; p++) 637 if (*p != '\\') 638 buf[i++] = *p; 639 buf[i++] = '\0'; 640 641 if (proto == IPPROTO_ETHERTYPE) { 642 i = match_token(ether_types, buf); 643 free(buf); 644 if (i != -1) { /* found */ 645 *end = s1; 646 return i; 647 } 648 } else { 649 struct protoent *pe = NULL; 650 struct servent *se; 651 652 if (proto != 0) 653 pe = getprotobynumber(proto); 654 setservent(1); 655 se = getservbyname(buf, pe ? pe->p_name : NULL); 656 free(buf); 657 if (se != NULL) { 658 *end = s1; 659 return ntohs(se->s_port); 660 } 661 } 662 return 0; /* not found */ 663} 664 665/* 666 * Map between current altq queue id numbers and names. 667 */ 668static int altq_fetched = 0; 669static TAILQ_HEAD(, pf_altq) altq_entries = 670 TAILQ_HEAD_INITIALIZER(altq_entries); 671 672static void 673altq_set_enabled(int enabled) 674{ 675 int pffd; 676 677 pffd = open("/dev/pf", O_RDWR); 678 if (pffd == -1) 679 err(EX_UNAVAILABLE, 680 "altq support opening pf(4) control device"); 681 if (enabled) { 682 if (ioctl(pffd, DIOCSTARTALTQ) != 0 && errno != EEXIST) 683 err(EX_UNAVAILABLE, "enabling altq"); 684 } else { 685 if (ioctl(pffd, DIOCSTOPALTQ) != 0 && errno != ENOENT) 686 err(EX_UNAVAILABLE, "disabling altq"); 687 } 688 close(pffd); 689} 690 691static void 692altq_fetch() 693{ 694 struct pfioc_altq pfioc; 695 struct pf_altq *altq; 696 int pffd, mnr; 697 698 if (altq_fetched) 699 return; 700 altq_fetched = 1; 701 pffd = open("/dev/pf", O_RDONLY); 702 if (pffd == -1) { 703 warn("altq support opening pf(4) control device"); 704 return; 705 } 706 bzero(&pfioc, sizeof(pfioc)); 707 if (ioctl(pffd, DIOCGETALTQS, &pfioc) != 0) { 708 warn("altq support getting queue list"); 709 close(pffd); 710 return; 711 } 712 mnr = pfioc.nr; 713 for (pfioc.nr = 0; pfioc.nr < mnr; pfioc.nr++) { 714 if (ioctl(pffd, DIOCGETALTQ, &pfioc) != 0) { 715 if (errno == EBUSY) 716 break; 717 warn("altq support getting queue list"); 718 close(pffd); 719 return; 720 } 721 if (pfioc.altq.qid == 0) 722 continue; 723 altq = malloc(sizeof(*altq)); 724 if (altq == NULL) 725 err(EX_OSERR, "malloc"); 726 *altq = pfioc.altq; 727 TAILQ_INSERT_TAIL(&altq_entries, altq, entries); 728 } 729 close(pffd); 730} 731 732static u_int32_t 733altq_name_to_qid(const char *name) 734{ 735 struct pf_altq *altq; 736 737 altq_fetch(); 738 TAILQ_FOREACH(altq, &altq_entries, entries) 739 if (strcmp(name, altq->qname) == 0) 740 break; 741 if (altq == NULL) 742 errx(EX_DATAERR, "altq has no queue named `%s'", name); 743 return altq->qid; 744} 745 746static const char * 747altq_qid_to_name(u_int32_t qid) 748{ 749 struct pf_altq *altq; 750 751 altq_fetch(); 752 TAILQ_FOREACH(altq, &altq_entries, entries) 753 if (qid == altq->qid) 754 break; 755 if (altq == NULL) 756 return NULL; 757 return altq->qname; 758} 759 760static void 761fill_altq_qid(u_int32_t *qid, const char *av) 762{ 763 *qid = altq_name_to_qid(av); 764} 765 766/* 767 * Fill the body of the command with the list of port ranges. 768 */ 769static int 770fill_newports(ipfw_insn_u16 *cmd, char *av, int proto) 771{ 772 uint16_t a, b, *p = cmd->ports; 773 int i = 0; 774 char *s = av; 775 776 while (*s) { 777 a = strtoport(av, &s, 0, proto); 778 if (s == av) /* no parameter */ 779 break; 780 if (*s == '-') { /* a range */ 781 av = s+1; 782 b = strtoport(av, &s, 0, proto); 783 if (s == av) /* no parameter */ 784 break; 785 p[0] = a; 786 p[1] = b; 787 } else if (*s == ',' || *s == '\0' ) 788 p[0] = p[1] = a; 789 else /* invalid separator */ 790 errx(EX_DATAERR, "invalid separator <%c> in <%s>\n", 791 *s, av); 792 i++; 793 p += 2; 794 av = s+1; 795 } 796 if (i > 0) { 797 if (i+1 > F_LEN_MASK) 798 errx(EX_DATAERR, "too many ports/ranges\n"); 799 cmd->o.len |= i+1; /* leave F_NOT and F_OR untouched */ 800 } 801 return i; 802} 803 804static struct _s_x icmpcodes[] = { 805 { "net", ICMP_UNREACH_NET }, 806 { "host", ICMP_UNREACH_HOST }, 807 { "protocol", ICMP_UNREACH_PROTOCOL }, 808 { "port", ICMP_UNREACH_PORT }, 809 { "needfrag", ICMP_UNREACH_NEEDFRAG }, 810 { "srcfail", ICMP_UNREACH_SRCFAIL }, 811 { "net-unknown", ICMP_UNREACH_NET_UNKNOWN }, 812 { "host-unknown", ICMP_UNREACH_HOST_UNKNOWN }, 813 { "isolated", ICMP_UNREACH_ISOLATED }, 814 { "net-prohib", ICMP_UNREACH_NET_PROHIB }, 815 { "host-prohib", ICMP_UNREACH_HOST_PROHIB }, 816 { "tosnet", ICMP_UNREACH_TOSNET }, 817 { "toshost", ICMP_UNREACH_TOSHOST }, 818 { "filter-prohib", ICMP_UNREACH_FILTER_PROHIB }, 819 { "host-precedence", ICMP_UNREACH_HOST_PRECEDENCE }, 820 { "precedence-cutoff", ICMP_UNREACH_PRECEDENCE_CUTOFF }, 821 { NULL, 0 } 822}; 823 824static void 825fill_reject_code(u_short *codep, char *str) 826{ 827 int val; 828 char *s; 829 830 val = strtoul(str, &s, 0); 831 if (s == str || *s != '\0' || val >= 0x100) 832 val = match_token(icmpcodes, str); 833 if (val < 0) 834 errx(EX_DATAERR, "unknown ICMP unreachable code ``%s''", str); 835 *codep = val; 836 return; 837} 838 839static void 840print_reject_code(uint16_t code) 841{ 842 char const *s = match_value(icmpcodes, code); 843 844 if (s != NULL) 845 printf("unreach %s", s); 846 else 847 printf("unreach %u", code); 848} 849 850/* 851 * Returns the number of bits set (from left) in a contiguous bitmask, 852 * or -1 if the mask is not contiguous. 853 * XXX this needs a proper fix. 854 * This effectively works on masks in big-endian (network) format. 855 * when compiled on little endian architectures. 856 * 857 * First bit is bit 7 of the first byte -- note, for MAC addresses, 858 * the first bit on the wire is bit 0 of the first byte. 859 * len is the max length in bits. 860 */ 861static int 862contigmask(uint8_t *p, int len) 863{ 864 int i, n; 865 866 for (i=0; i<len ; i++) 867 if ( (p[i/8] & (1 << (7 - (i%8)))) == 0) /* first bit unset */ 868 break; 869 for (n=i+1; n < len; n++) 870 if ( (p[n/8] & (1 << (7 - (n%8)))) != 0) 871 return -1; /* mask not contiguous */ 872 return i; 873} 874 875/* 876 * print flags set/clear in the two bitmasks passed as parameters. 877 * There is a specialized check for f_tcpflags. 878 */ 879static void 880print_flags(char const *name, ipfw_insn *cmd, struct _s_x *list) 881{ 882 char const *comma = ""; 883 int i; 884 uint8_t set = cmd->arg1 & 0xff; 885 uint8_t clear = (cmd->arg1 >> 8) & 0xff; 886 887 if (list == f_tcpflags && set == TH_SYN && clear == TH_ACK) { 888 printf(" setup"); 889 return; 890 } 891 892 printf(" %s ", name); 893 for (i=0; list[i].x != 0; i++) { 894 if (set & list[i].x) { 895 set &= ~list[i].x; 896 printf("%s%s", comma, list[i].s); 897 comma = ","; 898 } 899 if (clear & list[i].x) { 900 clear &= ~list[i].x; 901 printf("%s!%s", comma, list[i].s); 902 comma = ","; 903 } 904 } 905} 906 907/* 908 * Print the ip address contained in a command. 909 */ 910static void 911print_ip(ipfw_insn_ip *cmd, char const *s) 912{ 913 struct hostent *he = NULL; 914 int len = F_LEN((ipfw_insn *)cmd); 915 uint32_t *a = ((ipfw_insn_u32 *)cmd)->d; 916 917 printf("%s%s ", cmd->o.len & F_NOT ? " not": "", s); 918 919 if (cmd->o.opcode == O_IP_SRC_ME || cmd->o.opcode == O_IP_DST_ME) { 920 printf("me"); 921 return; 922 } 923 if (cmd->o.opcode == O_IP_SRC_LOOKUP || 924 cmd->o.opcode == O_IP_DST_LOOKUP) { 925 printf("table(%u", ((ipfw_insn *)cmd)->arg1); 926 if (len == F_INSN_SIZE(ipfw_insn_u32)) 927 printf(",%u", *a); 928 printf(")"); 929 return; 930 } 931 if (cmd->o.opcode == O_IP_SRC_SET || cmd->o.opcode == O_IP_DST_SET) { 932 uint32_t x, *map = (uint32_t *)&(cmd->mask); 933 int i, j; 934 char comma = '{'; 935 936 x = cmd->o.arg1 - 1; 937 x = htonl( ~x ); 938 cmd->addr.s_addr = htonl(cmd->addr.s_addr); 939 printf("%s/%d", inet_ntoa(cmd->addr), 940 contigmask((uint8_t *)&x, 32)); 941 x = cmd->addr.s_addr = htonl(cmd->addr.s_addr); 942 x &= 0xff; /* base */ 943 /* 944 * Print bits and ranges. 945 * Locate first bit set (i), then locate first bit unset (j). 946 * If we have 3+ consecutive bits set, then print them as a 947 * range, otherwise only print the initial bit and rescan. 948 */ 949 for (i=0; i < cmd->o.arg1; i++) 950 if (map[i/32] & (1<<(i & 31))) { 951 for (j=i+1; j < cmd->o.arg1; j++) 952 if (!(map[ j/32] & (1<<(j & 31)))) 953 break; 954 printf("%c%d", comma, i+x); 955 if (j>i+2) { /* range has at least 3 elements */ 956 printf("-%d", j-1+x); 957 i = j-1; 958 } 959 comma = ','; 960 } 961 printf("}"); 962 return; 963 } 964 /* 965 * len == 2 indicates a single IP, whereas lists of 1 or more 966 * addr/mask pairs have len = (2n+1). We convert len to n so we 967 * use that to count the number of entries. 968 */ 969 for (len = len / 2; len > 0; len--, a += 2) { 970 int mb = /* mask length */ 971 (cmd->o.opcode == O_IP_SRC || cmd->o.opcode == O_IP_DST) ? 972 32 : contigmask((uint8_t *)&(a[1]), 32); 973 if (mb == 32 && do_resolv) 974 he = gethostbyaddr((char *)&(a[0]), sizeof(u_long), AF_INET); 975 if (he != NULL) /* resolved to name */ 976 printf("%s", he->h_name); 977 else if (mb == 0) /* any */ 978 printf("any"); 979 else { /* numeric IP followed by some kind of mask */ 980 printf("%s", inet_ntoa( *((struct in_addr *)&a[0]) ) ); 981 if (mb < 0) 982 printf(":%s", inet_ntoa( *((struct in_addr *)&a[1]) ) ); 983 else if (mb < 32) 984 printf("/%d", mb); 985 } 986 if (len > 1) 987 printf(","); 988 } 989} 990 991/* 992 * prints a MAC address/mask pair 993 */ 994static void 995print_mac(uint8_t *addr, uint8_t *mask) 996{ 997 int l = contigmask(mask, 48); 998 999 if (l == 0) 1000 printf(" any"); 1001 else { 1002 printf(" %02x:%02x:%02x:%02x:%02x:%02x", 1003 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); 1004 if (l == -1) 1005 printf("&%02x:%02x:%02x:%02x:%02x:%02x", 1006 mask[0], mask[1], mask[2], 1007 mask[3], mask[4], mask[5]); 1008 else if (l < 48) 1009 printf("/%d", l); 1010 } 1011} 1012 1013static void 1014fill_icmptypes(ipfw_insn_u32 *cmd, char *av) 1015{ 1016 uint8_t type; 1017 1018 cmd->d[0] = 0; 1019 while (*av) { 1020 if (*av == ',') 1021 av++; 1022 1023 type = strtoul(av, &av, 0); 1024 1025 if (*av != ',' && *av != '\0') 1026 errx(EX_DATAERR, "invalid ICMP type"); 1027 1028 if (type > 31) 1029 errx(EX_DATAERR, "ICMP type out of range"); 1030 1031 cmd->d[0] |= 1 << type; 1032 } 1033 cmd->o.opcode = O_ICMPTYPE; 1034 cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32); 1035} 1036 1037static void 1038print_icmptypes(ipfw_insn_u32 *cmd) 1039{ 1040 int i; 1041 char sep= ' '; 1042 1043 printf(" icmptypes"); 1044 for (i = 0; i < 32; i++) { 1045 if ( (cmd->d[0] & (1 << (i))) == 0) 1046 continue; 1047 printf("%c%d", sep, i); 1048 sep = ','; 1049 } 1050} 1051 1052/* 1053 * Print the ip address contained in a command. 1054 */ 1055static void 1056print_ip6(ipfw_insn_ip6 *cmd, char const *s) 1057{ 1058 struct hostent *he = NULL; 1059 int len = F_LEN((ipfw_insn *) cmd) - 1; 1060 struct in6_addr *a = &(cmd->addr6); 1061 char trad[255]; 1062 1063 printf("%s%s ", cmd->o.len & F_NOT ? " not": "", s); 1064 1065 if (cmd->o.opcode == O_IP6_SRC_ME || cmd->o.opcode == O_IP6_DST_ME) { 1066 printf("me6"); 1067 return; 1068 } 1069 if (cmd->o.opcode == O_IP6) { 1070 printf(" ipv6"); 1071 return; 1072 } 1073 1074 /* 1075 * len == 4 indicates a single IP, whereas lists of 1 or more 1076 * addr/mask pairs have len = (2n+1). We convert len to n so we 1077 * use that to count the number of entries. 1078 */ 1079 1080 for (len = len / 4; len > 0; len -= 2, a += 2) { 1081 int mb = /* mask length */ 1082 (cmd->o.opcode == O_IP6_SRC || cmd->o.opcode == O_IP6_DST) ? 1083 128 : contigmask((uint8_t *)&(a[1]), 128); 1084 1085 if (mb == 128 && do_resolv) 1086 he = gethostbyaddr((char *)a, sizeof(*a), AF_INET6); 1087 if (he != NULL) /* resolved to name */ 1088 printf("%s", he->h_name); 1089 else if (mb == 0) /* any */ 1090 printf("any"); 1091 else { /* numeric IP followed by some kind of mask */ 1092 if (inet_ntop(AF_INET6, a, trad, sizeof( trad ) ) == NULL) 1093 printf("Error ntop in print_ip6\n"); 1094 printf("%s", trad ); 1095 if (mb < 0) /* XXX not really legal... */ 1096 printf(":%s", 1097 inet_ntop(AF_INET6, &a[1], trad, sizeof(trad))); 1098 else if (mb < 128) 1099 printf("/%d", mb); 1100 } 1101 if (len > 2) 1102 printf(","); 1103 } 1104} 1105 1106static void 1107fill_icmp6types(ipfw_insn_icmp6 *cmd, char *av) 1108{ 1109 uint8_t type; 1110 1111 cmd->d[0] = 0; 1112 while (*av) { 1113 if (*av == ',') 1114 av++; 1115 type = strtoul(av, &av, 0); 1116 if (*av != ',' && *av != '\0') 1117 errx(EX_DATAERR, "invalid ICMP6 type"); 1118 /* 1119 * XXX: shouldn't this be 0xFF? I can't see any reason why 1120 * we shouldn't be able to filter all possiable values 1121 * regardless of the ability of the rest of the kernel to do 1122 * anything useful with them. 1123 */ 1124 if (type > ICMP6_MAXTYPE) 1125 errx(EX_DATAERR, "ICMP6 type out of range"); 1126 cmd->d[type / 32] |= ( 1 << (type % 32)); 1127 } 1128 cmd->o.opcode = O_ICMP6TYPE; 1129 cmd->o.len |= F_INSN_SIZE(ipfw_insn_icmp6); 1130} 1131 1132 1133static void 1134print_icmp6types(ipfw_insn_u32 *cmd) 1135{ 1136 int i, j; 1137 char sep= ' '; 1138 1139 printf(" ipv6 icmp6types"); 1140 for (i = 0; i < 7; i++) 1141 for (j=0; j < 32; ++j) { 1142 if ( (cmd->d[i] & (1 << (j))) == 0) 1143 continue; 1144 printf("%c%d", sep, (i*32 + j)); 1145 sep = ','; 1146 } 1147} 1148 1149static void 1150print_flow6id( ipfw_insn_u32 *cmd) 1151{ 1152 uint16_t i, limit = cmd->o.arg1; 1153 char sep = ','; 1154 1155 printf(" flow-id "); 1156 for( i=0; i < limit; ++i) { 1157 if (i == limit - 1) 1158 sep = ' '; 1159 printf("%d%c", cmd->d[i], sep); 1160 } 1161} 1162 1163/* structure and define for the extension header in ipv6 */ 1164static struct _s_x ext6hdrcodes[] = { 1165 { "frag", EXT_FRAGMENT }, 1166 { "hopopt", EXT_HOPOPTS }, 1167 { "route", EXT_ROUTING }, 1168 { "ah", EXT_AH }, 1169 { "esp", EXT_ESP }, 1170 { NULL, 0 } 1171}; 1172 1173/* fills command for the extension header filtering */ 1174int 1175fill_ext6hdr( ipfw_insn *cmd, char *av) 1176{ 1177 int tok; 1178 char *s = av; 1179 1180 cmd->arg1 = 0; 1181 1182 while(s) { 1183 av = strsep( &s, ",") ; 1184 tok = match_token(ext6hdrcodes, av); 1185 switch (tok) { 1186 case EXT_FRAGMENT: 1187 cmd->arg1 |= EXT_FRAGMENT; 1188 break; 1189 1190 case EXT_HOPOPTS: 1191 cmd->arg1 |= EXT_HOPOPTS; 1192 break; 1193 1194 case EXT_ROUTING: 1195 cmd->arg1 |= EXT_ROUTING; 1196 break; 1197 1198 case EXT_AH: 1199 cmd->arg1 |= EXT_AH; 1200 break; 1201 1202 case EXT_ESP: 1203 cmd->arg1 |= EXT_ESP; 1204 break; 1205 1206 default: 1207 errx( EX_DATAERR, "invalid option for ipv6 exten header" ); 1208 break; 1209 } 1210 } 1211 if (cmd->arg1 == 0 ) 1212 return 0; 1213 cmd->opcode = O_EXT_HDR; 1214 cmd->len |= F_INSN_SIZE( ipfw_insn ); 1215 return 1; 1216} 1217 1218void 1219print_ext6hdr( ipfw_insn *cmd ) 1220{ 1221 char sep = ' '; 1222 1223 printf(" extension header:"); 1224 if (cmd->arg1 & EXT_FRAGMENT ) { 1225 printf("%cfragmentation", sep); 1226 sep = ','; 1227 } 1228 if (cmd->arg1 & EXT_HOPOPTS ) { 1229 printf("%chop options", sep); 1230 sep = ','; 1231 } 1232 if (cmd->arg1 & EXT_ROUTING ) { 1233 printf("%crouting options", sep); 1234 sep = ','; 1235 } 1236 if (cmd->arg1 & EXT_AH ) { 1237 printf("%cauthentication header", sep); 1238 sep = ','; 1239 } 1240 if (cmd->arg1 & EXT_ESP ) { 1241 printf("%cencapsulated security payload", sep); 1242 } 1243} 1244 1245/* 1246 * show_ipfw() prints the body of an ipfw rule. 1247 * Because the standard rule has at least proto src_ip dst_ip, we use 1248 * a helper function to produce these entries if not provided explicitly. 1249 * The first argument is the list of fields we have, the second is 1250 * the list of fields we want to be printed. 1251 * 1252 * Special cases if we have provided a MAC header: 1253 * + if the rule does not contain IP addresses/ports, do not print them; 1254 * + if the rule does not contain an IP proto, print "all" instead of "ip"; 1255 * 1256 * Once we have 'have_options', IP header fields are printed as options. 1257 */ 1258#define HAVE_PROTO 0x0001 1259#define HAVE_SRCIP 0x0002 1260#define HAVE_DSTIP 0x0004 1261#define HAVE_MAC 0x0008 1262#define HAVE_MACTYPE 0x0010 1263#define HAVE_PROTO6 0x0080 1264#define HAVE_OPTIONS 0x8000 1265 1266#define HAVE_IP (HAVE_PROTO | HAVE_SRCIP | HAVE_DSTIP) 1267static void 1268show_prerequisites(int *flags, int want, int cmd) 1269{ 1270 if (comment_only) 1271 return; 1272 if ( (*flags & HAVE_IP) == HAVE_IP) 1273 *flags |= HAVE_OPTIONS; 1274 1275 if ( (*flags & (HAVE_MAC|HAVE_MACTYPE|HAVE_OPTIONS)) == HAVE_MAC && 1276 cmd != O_MAC_TYPE) { 1277 /* 1278 * mac-type was optimized out by the compiler, 1279 * restore it 1280 */ 1281 printf(" any"); 1282 *flags |= HAVE_MACTYPE | HAVE_OPTIONS; 1283 return; 1284 } 1285 if ( !(*flags & HAVE_OPTIONS)) { 1286 /* XXX BED: !(*flags & HAVE_PROTO) in patch */ 1287 if ( !(*flags & HAVE_PROTO6) && (want & HAVE_PROTO6)) 1288 printf(" ipv6"); 1289 if ( !(*flags & HAVE_PROTO) && (want & HAVE_PROTO)) 1290 printf(" ip"); 1291 if ( !(*flags & HAVE_SRCIP) && (want & HAVE_SRCIP)) 1292 printf(" from any"); 1293 if ( !(*flags & HAVE_DSTIP) && (want & HAVE_DSTIP)) 1294 printf(" to any"); 1295 } 1296 *flags |= want; 1297} 1298 1299static void 1300show_ipfw(struct ip_fw *rule, int pcwidth, int bcwidth) 1301{ 1302 static int twidth = 0; 1303 int l; 1304 ipfw_insn *cmd; 1305 char *comment = NULL; /* ptr to comment if we have one */ 1306 int proto = 0; /* default */ 1307 int flags = 0; /* prerequisites */ 1308 ipfw_insn_log *logptr = NULL; /* set if we find an O_LOG */ 1309 ipfw_insn_altq *altqptr = NULL; /* set if we find an O_ALTQ */ 1310 int or_block = 0; /* we are in an or block */ 1311 uint32_t set_disable; 1312 1313 bcopy(&rule->next_rule, &set_disable, sizeof(set_disable)); 1314 1315 if (set_disable & (1 << rule->set)) { /* disabled */ 1316 if (!show_sets) 1317 return; 1318 else 1319 printf("# DISABLED "); 1320 } 1321 printf("%05u ", rule->rulenum); 1322 1323 if (pcwidth>0 || bcwidth>0) 1324 printf("%*llu %*llu ", pcwidth, align_uint64(&rule->pcnt), 1325 bcwidth, align_uint64(&rule->bcnt)); 1326 1327 if (do_time == 2) 1328 printf("%10u ", rule->timestamp); 1329 else if (do_time == 1) { 1330 char timestr[30]; 1331 time_t t = (time_t)0; 1332 1333 if (twidth == 0) { 1334 strcpy(timestr, ctime(&t)); 1335 *strchr(timestr, '\n') = '\0'; 1336 twidth = strlen(timestr); 1337 } 1338 if (rule->timestamp) { 1339#if _FreeBSD_version < 500000 /* XXX check */ 1340#define _long_to_time(x) (time_t)(x) 1341#endif 1342 t = _long_to_time(rule->timestamp); 1343 1344 strcpy(timestr, ctime(&t)); 1345 *strchr(timestr, '\n') = '\0'; 1346 printf("%s ", timestr); 1347 } else { 1348 printf("%*s", twidth, " "); 1349 } 1350 } 1351 1352 if (show_sets) 1353 printf("set %d ", rule->set); 1354 1355 /* 1356 * print the optional "match probability" 1357 */ 1358 if (rule->cmd_len > 0) { 1359 cmd = rule->cmd ; 1360 if (cmd->opcode == O_PROB) { 1361 ipfw_insn_u32 *p = (ipfw_insn_u32 *)cmd; 1362 double d = 1.0 * p->d[0]; 1363 1364 d = (d / 0x7fffffff); 1365 printf("prob %f ", d); 1366 } 1367 } 1368 1369 /* 1370 * first print actions 1371 */ 1372 for (l = rule->cmd_len - rule->act_ofs, cmd = ACTION_PTR(rule); 1373 l > 0 ; l -= F_LEN(cmd), cmd += F_LEN(cmd)) { 1374 switch(cmd->opcode) { 1375 case O_CHECK_STATE: 1376 printf("check-state"); 1377 flags = HAVE_IP; /* avoid printing anything else */ 1378 break; 1379 1380 case O_ACCEPT: 1381 printf("allow"); 1382 break; 1383 1384 case O_COUNT: 1385 printf("count"); 1386 break; 1387 1388 case O_DENY: 1389 printf("deny"); 1390 break; 1391 1392 case O_REJECT: 1393 if (cmd->arg1 == ICMP_REJECT_RST) 1394 printf("reset"); 1395 else if (cmd->arg1 == ICMP_UNREACH_HOST) 1396 printf("reject"); 1397 else 1398 print_reject_code(cmd->arg1); 1399 break; 1400 1401 case O_SKIPTO: 1402 printf("skipto %u", cmd->arg1); 1403 break; 1404 1405 case O_PIPE: 1406 printf("pipe %u", cmd->arg1); 1407 break; 1408 1409 case O_QUEUE: 1410 printf("queue %u", cmd->arg1); 1411 break; 1412 1413 case O_DIVERT: 1414 printf("divert %u", cmd->arg1); 1415 break; 1416 1417 case O_TEE: 1418 printf("tee %u", cmd->arg1); 1419 break; 1420 1421 case O_NETGRAPH: 1422 printf("netgraph %u", cmd->arg1); 1423 break; 1424 1425 case O_NGTEE: 1426 printf("ngtee %u", cmd->arg1); 1427 break; 1428 1429 case O_FORWARD_IP: 1430 { 1431 ipfw_insn_sa *s = (ipfw_insn_sa *)cmd; 1432 1433 printf("fwd %s", inet_ntoa(s->sa.sin_addr)); 1434 if (s->sa.sin_port) 1435 printf(",%d", s->sa.sin_port); 1436 } 1437 break; 1438 1439 case O_LOG: /* O_LOG is printed last */ 1440 logptr = (ipfw_insn_log *)cmd; 1441 break; 1442 1443 case O_ALTQ: /* O_ALTQ is printed after O_LOG */ 1444 altqptr = (ipfw_insn_altq *)cmd; 1445 break; 1446 1447 default: 1448 printf("** unrecognized action %d len %d ", 1449 cmd->opcode, cmd->len); 1450 } 1451 } 1452 if (logptr) { 1453 if (logptr->max_log > 0) 1454 printf(" log logamount %d", logptr->max_log); 1455 else 1456 printf(" log"); 1457 } 1458 if (altqptr) { 1459 const char *qname; 1460 1461 qname = altq_qid_to_name(altqptr->qid); 1462 if (qname == NULL) 1463 printf(" altq ?<%u>", altqptr->qid); 1464 else 1465 printf(" altq %s", qname); 1466 } 1467 1468 /* 1469 * then print the body. 1470 */ 1471 if (rule->_pad & 1) { /* empty rules before options */ 1472 if (!do_compact) 1473 printf(" ip from any to any"); 1474 flags |= HAVE_IP | HAVE_OPTIONS; 1475 } 1476 1477 if (comment_only) 1478 comment = "..."; 1479 1480 for (l = rule->act_ofs, cmd = rule->cmd ; 1481 l > 0 ; l -= F_LEN(cmd) , cmd += F_LEN(cmd)) { 1482 /* useful alias */ 1483 ipfw_insn_u32 *cmd32 = (ipfw_insn_u32 *)cmd; 1484 1485 if (comment_only) { 1486 if (cmd->opcode != O_NOP) 1487 continue; 1488 printf(" // %s\n", (char *)(cmd + 1)); 1489 return; 1490 } 1491 1492 show_prerequisites(&flags, 0, cmd->opcode); 1493 1494 switch(cmd->opcode) { 1495 case O_PROB: 1496 break; /* done already */ 1497 1498 case O_PROBE_STATE: 1499 break; /* no need to print anything here */ 1500 1501 case O_MACADDR2: { 1502 ipfw_insn_mac *m = (ipfw_insn_mac *)cmd; 1503 1504 if ((cmd->len & F_OR) && !or_block) 1505 printf(" {"); 1506 if (cmd->len & F_NOT) 1507 printf(" not"); 1508 printf(" MAC"); 1509 flags |= HAVE_MAC; 1510 print_mac(m->addr, m->mask); 1511 print_mac(m->addr + 6, m->mask + 6); 1512 } 1513 break; 1514 1515 case O_MAC_TYPE: 1516 if ((cmd->len & F_OR) && !or_block) 1517 printf(" {"); 1518 print_newports((ipfw_insn_u16 *)cmd, IPPROTO_ETHERTYPE, 1519 (flags & HAVE_OPTIONS) ? cmd->opcode : 0); 1520 flags |= HAVE_MAC | HAVE_MACTYPE | HAVE_OPTIONS; 1521 break; 1522 1523 case O_IP_SRC: 1524 case O_IP_SRC_LOOKUP: 1525 case O_IP_SRC_MASK: 1526 case O_IP_SRC_ME: 1527 case O_IP_SRC_SET: 1528 show_prerequisites(&flags, HAVE_PROTO, 0); 1529 if (!(flags & HAVE_SRCIP)) 1530 printf(" from"); 1531 if ((cmd->len & F_OR) && !or_block) 1532 printf(" {"); 1533 print_ip((ipfw_insn_ip *)cmd, 1534 (flags & HAVE_OPTIONS) ? " src-ip" : ""); 1535 flags |= HAVE_SRCIP; 1536 break; 1537 1538 case O_IP_DST: 1539 case O_IP_DST_LOOKUP: 1540 case O_IP_DST_MASK: 1541 case O_IP_DST_ME: 1542 case O_IP_DST_SET: 1543 show_prerequisites(&flags, HAVE_PROTO|HAVE_SRCIP, 0); 1544 if (!(flags & HAVE_DSTIP)) 1545 printf(" to"); 1546 if ((cmd->len & F_OR) && !or_block) 1547 printf(" {"); 1548 print_ip((ipfw_insn_ip *)cmd, 1549 (flags & HAVE_OPTIONS) ? " dst-ip" : ""); 1550 flags |= HAVE_DSTIP; 1551 break; 1552 1553 case O_IP6_SRC: 1554 case O_IP6_SRC_MASK: 1555 case O_IP6_SRC_ME: 1556 show_prerequisites(&flags, HAVE_PROTO6, 0); 1557 if (!(flags & HAVE_SRCIP)) 1558 printf(" from"); 1559 if ((cmd->len & F_OR) && !or_block) 1560 printf(" {"); 1561 print_ip6((ipfw_insn_ip6 *)cmd, 1562 (flags & HAVE_OPTIONS) ? " src-ip6" : ""); 1563 flags |= HAVE_SRCIP | HAVE_PROTO; 1564 break; 1565 1566 case O_IP6_DST: 1567 case O_IP6_DST_MASK: 1568 case O_IP6_DST_ME: 1569 show_prerequisites(&flags, HAVE_PROTO|HAVE_SRCIP, 0); 1570 if (!(flags & HAVE_DSTIP)) 1571 printf(" to"); 1572 if ((cmd->len & F_OR) && !or_block) 1573 printf(" {"); 1574 print_ip6((ipfw_insn_ip6 *)cmd, 1575 (flags & HAVE_OPTIONS) ? " dst-ip6" : ""); 1576 flags |= HAVE_DSTIP; 1577 break; 1578 1579 case O_FLOW6ID: 1580 print_flow6id( (ipfw_insn_u32 *) cmd ); 1581 flags |= HAVE_OPTIONS; 1582 break; 1583 1584 case O_IP_DSTPORT: 1585 show_prerequisites(&flags, HAVE_IP, 0); 1586 case O_IP_SRCPORT: 1587 show_prerequisites(&flags, HAVE_PROTO|HAVE_SRCIP, 0); 1588 if ((cmd->len & F_OR) && !or_block) 1589 printf(" {"); 1590 print_newports((ipfw_insn_u16 *)cmd, proto, 1591 (flags & HAVE_OPTIONS) ? cmd->opcode : 0); 1592 break; 1593 1594 case O_PROTO: { 1595 struct protoent *pe = NULL; 1596 1597 if ((cmd->len & F_OR) && !or_block) 1598 printf(" {"); 1599 if (cmd->len & F_NOT) 1600 printf(" not"); 1601 proto = cmd->arg1; 1602 pe = getprotobynumber(cmd->arg1); 1603 if (flags & HAVE_OPTIONS) 1604 printf(" proto"); 1605 if (pe) 1606 printf(" %s", pe->p_name); 1607 else 1608 printf(" %u", cmd->arg1); 1609 } 1610 flags |= HAVE_PROTO; 1611 break; 1612 1613 default: /*options ... */ 1614 show_prerequisites(&flags, HAVE_IP | HAVE_OPTIONS, 0); 1615 if ((cmd->len & F_OR) && !or_block) 1616 printf(" {"); 1617 if (cmd->len & F_NOT && cmd->opcode != O_IN) 1618 printf(" not"); 1619 switch(cmd->opcode) { 1620 case O_FRAG: 1621 printf(" frag"); 1622 break; 1623 1624 case O_IN: 1625 printf(cmd->len & F_NOT ? " out" : " in"); 1626 break; 1627 1628 case O_DIVERTED: 1629 switch (cmd->arg1) { 1630 case 3: 1631 printf(" diverted"); 1632 break; 1633 case 1: 1634 printf(" diverted-loopback"); 1635 break; 1636 case 2: 1637 printf(" diverted-output"); 1638 break; 1639 default: 1640 printf(" diverted-?<%u>", cmd->arg1); 1641 break; 1642 } 1643 break; 1644 1645 case O_LAYER2: 1646 printf(" layer2"); 1647 break; 1648 case O_XMIT: 1649 case O_RECV: 1650 case O_VIA: 1651 { 1652 char const *s; 1653 ipfw_insn_if *cmdif = (ipfw_insn_if *)cmd; 1654 1655 if (cmd->opcode == O_XMIT) 1656 s = "xmit"; 1657 else if (cmd->opcode == O_RECV) 1658 s = "recv"; 1659 else /* if (cmd->opcode == O_VIA) */ 1660 s = "via"; 1661 if (cmdif->name[0] == '\0') 1662 printf(" %s %s", s, 1663 inet_ntoa(cmdif->p.ip)); 1664 else 1665 printf(" %s %s", s, cmdif->name); 1666 1667 break; 1668 } 1669 case O_IPID: 1670 if (F_LEN(cmd) == 1) 1671 printf(" ipid %u", cmd->arg1 ); 1672 else 1673 print_newports((ipfw_insn_u16 *)cmd, 0, 1674 O_IPID); 1675 break; 1676 1677 case O_IPTTL: 1678 if (F_LEN(cmd) == 1) 1679 printf(" ipttl %u", cmd->arg1 ); 1680 else 1681 print_newports((ipfw_insn_u16 *)cmd, 0, 1682 O_IPTTL); 1683 break; 1684 1685 case O_IPVER: 1686 printf(" ipver %u", cmd->arg1 ); 1687 break; 1688 1689 case O_IPPRECEDENCE: 1690 printf(" ipprecedence %u", (cmd->arg1) >> 5 ); 1691 break; 1692 1693 case O_IPLEN: 1694 if (F_LEN(cmd) == 1) 1695 printf(" iplen %u", cmd->arg1 ); 1696 else 1697 print_newports((ipfw_insn_u16 *)cmd, 0, 1698 O_IPLEN); 1699 break; 1700 1701 case O_IPOPT: 1702 print_flags("ipoptions", cmd, f_ipopts); 1703 break; 1704 1705 case O_IPTOS: 1706 print_flags("iptos", cmd, f_iptos); 1707 break; 1708 1709 case O_ICMPTYPE: 1710 print_icmptypes((ipfw_insn_u32 *)cmd); 1711 break; 1712 1713 case O_ESTAB: 1714 printf(" established"); 1715 break; 1716 1717 case O_TCPDATALEN: 1718 if (F_LEN(cmd) == 1) 1719 printf(" tcpdatalen %u", cmd->arg1 ); 1720 else 1721 print_newports((ipfw_insn_u16 *)cmd, 0, 1722 O_TCPDATALEN); 1723 break; 1724 1725 case O_TCPFLAGS: 1726 print_flags("tcpflags", cmd, f_tcpflags); 1727 break; 1728 1729 case O_TCPOPTS: 1730 print_flags("tcpoptions", cmd, f_tcpopts); 1731 break; 1732 1733 case O_TCPWIN: 1734 printf(" tcpwin %d", ntohs(cmd->arg1)); 1735 break; 1736 1737 case O_TCPACK: 1738 printf(" tcpack %d", ntohl(cmd32->d[0])); 1739 break; 1740 1741 case O_TCPSEQ: 1742 printf(" tcpseq %d", ntohl(cmd32->d[0])); 1743 break; 1744 1745 case O_UID: 1746 { 1747 struct passwd *pwd = getpwuid(cmd32->d[0]); 1748 1749 if (pwd) 1750 printf(" uid %s", pwd->pw_name); 1751 else 1752 printf(" uid %u", cmd32->d[0]); 1753 } 1754 break; 1755 1756 case O_GID: 1757 { 1758 struct group *grp = getgrgid(cmd32->d[0]); 1759 1760 if (grp) 1761 printf(" gid %s", grp->gr_name); 1762 else 1763 printf(" gid %u", cmd32->d[0]); 1764 } 1765 break; 1766 1767 case O_JAIL: 1768 printf(" jail %d", cmd32->d[0]); 1769 break; 1770 1771 case O_VERREVPATH: 1772 printf(" verrevpath"); 1773 break; 1774 1775 case O_VERSRCREACH: 1776 printf(" versrcreach"); 1777 break; 1778 1779 case O_ANTISPOOF: 1780 printf(" antispoof"); 1781 break; 1782 1783 case O_IPSEC: 1784 printf(" ipsec"); 1785 break; 1786 1787 case O_NOP: 1788 comment = (char *)(cmd + 1); 1789 break; 1790 1791 case O_KEEP_STATE: 1792 printf(" keep-state"); 1793 break; 1794 1795 case O_LIMIT: 1796 { 1797 struct _s_x *p = limit_masks; 1798 ipfw_insn_limit *c = (ipfw_insn_limit *)cmd; 1799 uint8_t x = c->limit_mask; 1800 char const *comma = " "; 1801 1802 printf(" limit"); 1803 for (; p->x != 0 ; p++) 1804 if ((x & p->x) == p->x) { 1805 x &= ~p->x; 1806 printf("%s%s", comma, p->s); 1807 comma = ","; 1808 } 1809 printf(" %d", c->conn_limit); 1810 } 1811 break; 1812 1813 case O_IP6: 1814 printf(" ipv6"); 1815 break; 1816 1817 case O_ICMP6TYPE: 1818 print_icmp6types((ipfw_insn_u32 *)cmd); 1819 break; 1820 1821 case O_EXT_HDR: 1822 print_ext6hdr( (ipfw_insn *) cmd ); 1823 break; 1824 1825 default: 1826 printf(" [opcode %d len %d]", 1827 cmd->opcode, cmd->len); 1828 } 1829 } 1830 if (cmd->len & F_OR) { 1831 printf(" or"); 1832 or_block = 1; 1833 } else if (or_block) { 1834 printf(" }"); 1835 or_block = 0; 1836 } 1837 } 1838 show_prerequisites(&flags, HAVE_IP, 0); 1839 if (comment) 1840 printf(" // %s", comment); 1841 printf("\n"); 1842} 1843 1844static void 1845show_dyn_ipfw(ipfw_dyn_rule *d, int pcwidth, int bcwidth) 1846{ 1847 struct protoent *pe; 1848 struct in_addr a; 1849 uint16_t rulenum; 1850 1851 if (!do_expired) { 1852 if (!d->expire && !(d->dyn_type == O_LIMIT_PARENT)) 1853 return; 1854 } 1855 bcopy(&d->rule, &rulenum, sizeof(rulenum)); 1856 printf("%05d", rulenum); 1857 if (pcwidth>0 || bcwidth>0) 1858 printf(" %*llu %*llu (%ds)", pcwidth, 1859 align_uint64(&d->pcnt), bcwidth, 1860 align_uint64(&d->bcnt), d->expire); 1861 switch (d->dyn_type) { 1862 case O_LIMIT_PARENT: 1863 printf(" PARENT %d", d->count); 1864 break; 1865 case O_LIMIT: 1866 printf(" LIMIT"); 1867 break; 1868 case O_KEEP_STATE: /* bidir, no mask */ 1869 printf(" STATE"); 1870 break; 1871 } 1872 1873 if ((pe = getprotobynumber(d->id.proto)) != NULL) 1874 printf(" %s", pe->p_name); 1875 else 1876 printf(" proto %u", d->id.proto); 1877 1878 a.s_addr = htonl(d->id.src_ip); 1879 printf(" %s %d", inet_ntoa(a), d->id.src_port); 1880 1881 a.s_addr = htonl(d->id.dst_ip); 1882 printf(" <-> %s %d", inet_ntoa(a), d->id.dst_port); 1883 printf("\n"); 1884} 1885 1886static int 1887sort_q(const void *pa, const void *pb) 1888{ 1889 int rev = (do_sort < 0); 1890 int field = rev ? -do_sort : do_sort; 1891 long long res = 0; 1892 const struct dn_flow_queue *a = pa; 1893 const struct dn_flow_queue *b = pb; 1894 1895 switch (field) { 1896 case 1: /* pkts */ 1897 res = a->len - b->len; 1898 break; 1899 case 2: /* bytes */ 1900 res = a->len_bytes - b->len_bytes; 1901 break; 1902 1903 case 3: /* tot pkts */ 1904 res = a->tot_pkts - b->tot_pkts; 1905 break; 1906 1907 case 4: /* tot bytes */ 1908 res = a->tot_bytes - b->tot_bytes; 1909 break; 1910 } 1911 if (res < 0) 1912 res = -1; 1913 if (res > 0) 1914 res = 1; 1915 return (int)(rev ? res : -res); 1916} 1917 1918static void 1919list_queues(struct dn_flow_set *fs, struct dn_flow_queue *q) 1920{ 1921 int l; 1922 int index_printed, indexes = 0; 1923 char buff[255]; 1924 struct protoent *pe; 1925 1926 if (fs->rq_elements == 0) 1927 return; 1928 1929 if (do_sort != 0) 1930 heapsort(q, fs->rq_elements, sizeof *q, sort_q); 1931 1932 /* Print IPv4 flows */ 1933 index_printed = 0; 1934 for (l = 0; l < fs->rq_elements; l++) { 1935 struct in_addr ina; 1936 1937 /* XXX: Should check for IPv4 flows */ 1938 if (IS_IP6_FLOW_ID(&(q[l].id))) 1939 continue; 1940 1941 if (!index_printed) { 1942 index_printed = 1; 1943 if (indexes > 0) /* currently a no-op */ 1944 printf("\n"); 1945 indexes++; 1946 printf(" " 1947 "mask: 0x%02x 0x%08x/0x%04x -> 0x%08x/0x%04x\n", 1948 fs->flow_mask.proto, 1949 fs->flow_mask.src_ip, fs->flow_mask.src_port, 1950 fs->flow_mask.dst_ip, fs->flow_mask.dst_port); 1951 1952 printf("BKT Prot ___Source IP/port____ " 1953 "____Dest. IP/port____ " 1954 "Tot_pkt/bytes Pkt/Byte Drp\n"); 1955 } 1956 1957 printf("%3d ", q[l].hash_slot); 1958 pe = getprotobynumber(q[l].id.proto); 1959 if (pe) 1960 printf("%-4s ", pe->p_name); 1961 else 1962 printf("%4u ", q[l].id.proto); 1963 ina.s_addr = htonl(q[l].id.src_ip); 1964 printf("%15s/%-5d ", 1965 inet_ntoa(ina), q[l].id.src_port); 1966 ina.s_addr = htonl(q[l].id.dst_ip); 1967 printf("%15s/%-5d ", 1968 inet_ntoa(ina), q[l].id.dst_port); 1969 printf("%4qu %8qu %2u %4u %3u\n", 1970 q[l].tot_pkts, q[l].tot_bytes, 1971 q[l].len, q[l].len_bytes, q[l].drops); 1972 if (verbose) 1973 printf(" S %20qd F %20qd\n", 1974 q[l].S, q[l].F); 1975 } 1976 1977 /* Print IPv6 flows */ 1978 index_printed = 0; 1979 for (l = 0; l < fs->rq_elements; l++) { 1980 if (!IS_IP6_FLOW_ID(&(q[l].id))) 1981 continue; 1982 1983 if (!index_printed) { 1984 index_printed = 1; 1985 if (indexes > 0) 1986 printf("\n"); 1987 indexes++; 1988 printf("\n mask: proto: 0x%02x, flow_id: 0x%08x, ", 1989 fs->flow_mask.proto, fs->flow_mask.flow_id6); 1990 inet_ntop(AF_INET6, &(fs->flow_mask.src_ip6), 1991 buff, sizeof(buff)); 1992 printf("%s/0x%04x -> ", buff, fs->flow_mask.src_port); 1993 inet_ntop( AF_INET6, &(fs->flow_mask.dst_ip6), 1994 buff, sizeof(buff) ); 1995 printf("%s/0x%04x\n", buff, fs->flow_mask.dst_port); 1996 1997 printf("BKT ___Prot___ _flow-id_ " 1998 "______________Source IPv6/port_______________ " 1999 "_______________Dest. IPv6/port_______________ " 2000 "Tot_pkt/bytes Pkt/Byte Drp\n"); 2001 } 2002 printf("%3d ", q[l].hash_slot); 2003 pe = getprotobynumber(q[l].id.proto); 2004 if (pe != NULL) 2005 printf("%9s ", pe->p_name); 2006 else 2007 printf("%9u ", q[l].id.proto); 2008 printf("%7d %39s/%-5d ", q[l].id.flow_id6, 2009 inet_ntop(AF_INET6, &(q[l].id.src_ip6), buff, sizeof(buff)), 2010 q[l].id.src_port); 2011 printf(" %39s/%-5d ", 2012 inet_ntop(AF_INET6, &(q[l].id.dst_ip6), buff, sizeof(buff)), 2013 q[l].id.dst_port); 2014 printf(" %4qu %8qu %2u %4u %3u\n", 2015 q[l].tot_pkts, q[l].tot_bytes, 2016 q[l].len, q[l].len_bytes, q[l].drops); 2017 if (verbose) 2018 printf(" S %20qd F %20qd\n", q[l].S, q[l].F); 2019 } 2020} 2021 2022static void 2023print_flowset_parms(struct dn_flow_set *fs, char *prefix) 2024{ 2025 int l; 2026 char qs[30]; 2027 char plr[30]; 2028 char red[90]; /* Display RED parameters */ 2029 2030 l = fs->qsize; 2031 if (fs->flags_fs & DN_QSIZE_IS_BYTES) { 2032 if (l >= 8192) 2033 sprintf(qs, "%d KB", l / 1024); 2034 else 2035 sprintf(qs, "%d B", l); 2036 } else 2037 sprintf(qs, "%3d sl.", l); 2038 if (fs->plr) 2039 sprintf(plr, "plr %f", 1.0 * fs->plr / (double)(0x7fffffff)); 2040 else 2041 plr[0] = '\0'; 2042 if (fs->flags_fs & DN_IS_RED) /* RED parameters */ 2043 sprintf(red, 2044 "\n\t %cRED w_q %f min_th %d max_th %d max_p %f", 2045 (fs->flags_fs & DN_IS_GENTLE_RED) ? 'G' : ' ', 2046 1.0 * fs->w_q / (double)(1 << SCALE_RED), 2047 SCALE_VAL(fs->min_th), 2048 SCALE_VAL(fs->max_th), 2049 1.0 * fs->max_p / (double)(1 << SCALE_RED)); 2050 else 2051 sprintf(red, "droptail"); 2052 2053 printf("%s %s%s %d queues (%d buckets) %s\n", 2054 prefix, qs, plr, fs->rq_elements, fs->rq_size, red); 2055} 2056 2057static void 2058list_pipes(void *data, uint nbytes, int ac, char *av[]) 2059{ 2060 int rulenum; 2061 void *next = data; 2062 struct dn_pipe *p = (struct dn_pipe *) data; 2063 struct dn_flow_set *fs; 2064 struct dn_flow_queue *q; 2065 int l; 2066 2067 if (ac > 0) 2068 rulenum = strtoul(*av++, NULL, 10); 2069 else 2070 rulenum = 0; 2071 for (; nbytes >= sizeof *p; p = (struct dn_pipe *)next) { 2072 double b = p->bandwidth; 2073 char buf[30]; 2074 char prefix[80]; 2075 2076 if (p->next != (struct dn_pipe *)DN_IS_PIPE) 2077 break; /* done with pipes, now queues */ 2078 2079 /* 2080 * compute length, as pipe have variable size 2081 */ 2082 l = sizeof(*p) + p->fs.rq_elements * sizeof(*q); 2083 next = (char *)p + l; 2084 nbytes -= l; 2085 2086 if ((rulenum != 0 && rulenum != p->pipe_nr) || do_pipe == 2) 2087 continue; 2088 2089 /* 2090 * Print rate (or clocking interface) 2091 */ 2092 if (p->if_name[0] != '\0') 2093 sprintf(buf, "%s", p->if_name); 2094 else if (b == 0) 2095 sprintf(buf, "unlimited"); 2096 else if (b >= 1000000) 2097 sprintf(buf, "%7.3f Mbit/s", b/1000000); 2098 else if (b >= 1000) 2099 sprintf(buf, "%7.3f Kbit/s", b/1000); 2100 else 2101 sprintf(buf, "%7.3f bit/s ", b); 2102 2103 sprintf(prefix, "%05d: %s %4d ms ", 2104 p->pipe_nr, buf, p->delay); 2105 print_flowset_parms(&(p->fs), prefix); 2106 if (verbose) 2107 printf(" V %20qd\n", p->V >> MY_M); 2108 2109 q = (struct dn_flow_queue *)(p+1); 2110 list_queues(&(p->fs), q); 2111 } 2112 for (fs = next; nbytes >= sizeof *fs; fs = next) { 2113 char prefix[80]; 2114 2115 if (fs->next != (struct dn_flow_set *)DN_IS_QUEUE) 2116 break; 2117 l = sizeof(*fs) + fs->rq_elements * sizeof(*q); 2118 next = (char *)fs + l; 2119 nbytes -= l; 2120 2121 if (rulenum != 0 && ((rulenum != fs->fs_nr && do_pipe == 2) || 2122 (rulenum != fs->parent_nr && do_pipe == 1))) { 2123 continue; 2124 } 2125 2126 q = (struct dn_flow_queue *)(fs+1); 2127 sprintf(prefix, "q%05d: weight %d pipe %d ", 2128 fs->fs_nr, fs->weight, fs->parent_nr); 2129 print_flowset_parms(fs, prefix); 2130 list_queues(fs, q); 2131 } 2132} 2133 2134/* 2135 * This one handles all set-related commands 2136 * ipfw set { show | enable | disable } 2137 * ipfw set swap X Y 2138 * ipfw set move X to Y 2139 * ipfw set move rule X to Y 2140 */ 2141static void 2142sets_handler(int ac, char *av[]) 2143{ 2144 uint32_t set_disable, masks[2]; 2145 int i, nbytes; 2146 uint16_t rulenum; 2147 uint8_t cmd, new_set; 2148 2149 ac--; 2150 av++; 2151 2152 if (!ac) 2153 errx(EX_USAGE, "set needs command"); 2154 if (_substrcmp(*av, "show") == 0) { 2155 void *data; 2156 char const *msg; 2157 2158 nbytes = sizeof(struct ip_fw); 2159 if ((data = calloc(1, nbytes)) == NULL) 2160 err(EX_OSERR, "calloc"); 2161 if (do_cmd(IP_FW_GET, data, (uintptr_t)&nbytes) < 0) 2162 err(EX_OSERR, "getsockopt(IP_FW_GET)"); 2163 bcopy(&((struct ip_fw *)data)->next_rule, 2164 &set_disable, sizeof(set_disable)); 2165 2166 for (i = 0, msg = "disable" ; i < RESVD_SET; i++) 2167 if ((set_disable & (1<<i))) { 2168 printf("%s %d", msg, i); 2169 msg = ""; 2170 } 2171 msg = (set_disable) ? " enable" : "enable"; 2172 for (i = 0; i < RESVD_SET; i++) 2173 if (!(set_disable & (1<<i))) { 2174 printf("%s %d", msg, i); 2175 msg = ""; 2176 } 2177 printf("\n"); 2178 } else if (_substrcmp(*av, "swap") == 0) { 2179 ac--; av++; 2180 if (ac != 2) 2181 errx(EX_USAGE, "set swap needs 2 set numbers\n"); 2182 rulenum = atoi(av[0]); 2183 new_set = atoi(av[1]); 2184 if (!isdigit(*(av[0])) || rulenum > RESVD_SET) 2185 errx(EX_DATAERR, "invalid set number %s\n", av[0]); 2186 if (!isdigit(*(av[1])) || new_set > RESVD_SET) 2187 errx(EX_DATAERR, "invalid set number %s\n", av[1]); 2188 masks[0] = (4 << 24) | (new_set << 16) | (rulenum); 2189 i = do_cmd(IP_FW_DEL, masks, sizeof(uint32_t)); 2190 } else if (_substrcmp(*av, "move") == 0) { 2191 ac--; av++; 2192 if (ac && _substrcmp(*av, "rule") == 0) { 2193 cmd = 2; 2194 ac--; av++; 2195 } else 2196 cmd = 3; 2197 if (ac != 3 || _substrcmp(av[1], "to") != 0) 2198 errx(EX_USAGE, "syntax: set move [rule] X to Y\n"); 2199 rulenum = atoi(av[0]); 2200 new_set = atoi(av[2]); 2201 if (!isdigit(*(av[0])) || (cmd == 3 && rulenum > RESVD_SET) || 2202 (cmd == 2 && rulenum == 65535) ) 2203 errx(EX_DATAERR, "invalid source number %s\n", av[0]); 2204 if (!isdigit(*(av[2])) || new_set > RESVD_SET) 2205 errx(EX_DATAERR, "invalid dest. set %s\n", av[1]); 2206 masks[0] = (cmd << 24) | (new_set << 16) | (rulenum); 2207 i = do_cmd(IP_FW_DEL, masks, sizeof(uint32_t)); 2208 } else if (_substrcmp(*av, "disable") == 0 || 2209 _substrcmp(*av, "enable") == 0 ) { 2210 int which = _substrcmp(*av, "enable") == 0 ? 1 : 0; 2211 2212 ac--; av++; 2213 masks[0] = masks[1] = 0; 2214 2215 while (ac) { 2216 if (isdigit(**av)) { 2217 i = atoi(*av); 2218 if (i < 0 || i > RESVD_SET) 2219 errx(EX_DATAERR, 2220 "invalid set number %d\n", i); 2221 masks[which] |= (1<<i); 2222 } else if (_substrcmp(*av, "disable") == 0) 2223 which = 0; 2224 else if (_substrcmp(*av, "enable") == 0) 2225 which = 1; 2226 else 2227 errx(EX_DATAERR, 2228 "invalid set command %s\n", *av); 2229 av++; ac--; 2230 } 2231 if ( (masks[0] & masks[1]) != 0 ) 2232 errx(EX_DATAERR, 2233 "cannot enable and disable the same set\n"); 2234 2235 i = do_cmd(IP_FW_DEL, masks, sizeof(masks)); 2236 if (i) 2237 warn("set enable/disable: setsockopt(IP_FW_DEL)"); 2238 } else 2239 errx(EX_USAGE, "invalid set command %s\n", *av); 2240} 2241 2242static void 2243sysctl_handler(int ac, char *av[], int which) 2244{ 2245 ac--; 2246 av++; 2247 2248 if (ac == 0) { 2249 warnx("missing keyword to enable/disable\n"); 2250 } else if (_substrcmp(*av, "firewall") == 0) { 2251 sysctlbyname("net.inet.ip.fw.enable", NULL, 0, 2252 &which, sizeof(which)); 2253 } else if (_substrcmp(*av, "one_pass") == 0) { 2254 sysctlbyname("net.inet.ip.fw.one_pass", NULL, 0, 2255 &which, sizeof(which)); 2256 } else if (_substrcmp(*av, "debug") == 0) { 2257 sysctlbyname("net.inet.ip.fw.debug", NULL, 0, 2258 &which, sizeof(which)); 2259 } else if (_substrcmp(*av, "verbose") == 0) { 2260 sysctlbyname("net.inet.ip.fw.verbose", NULL, 0, 2261 &which, sizeof(which)); 2262 } else if (_substrcmp(*av, "dyn_keepalive") == 0) { 2263 sysctlbyname("net.inet.ip.fw.dyn_keepalive", NULL, 0, 2264 &which, sizeof(which)); 2265 } else if (_substrcmp(*av, "altq") == 0) { 2266 altq_set_enabled(which); 2267 } else { 2268 warnx("unrecognize enable/disable keyword: %s\n", *av); 2269 } 2270} 2271 2272static void 2273list(int ac, char *av[], int show_counters) 2274{ 2275 struct ip_fw *r; 2276 ipfw_dyn_rule *dynrules, *d; 2277 2278#define NEXT(r) ((struct ip_fw *)((char *)r + RULESIZE(r))) 2279 char *lim; 2280 void *data = NULL; 2281 int bcwidth, n, nbytes, nstat, ndyn, pcwidth, width; 2282 int exitval = EX_OK; 2283 int lac; 2284 char **lav; 2285 u_long rnum, last; 2286 char *endptr; 2287 int seen = 0; 2288 2289 const int ocmd = do_pipe ? IP_DUMMYNET_GET : IP_FW_GET; 2290 int nalloc = 1024; /* start somewhere... */ 2291 2292 last = 0; 2293 2294 if (test_only) { 2295 fprintf(stderr, "Testing only, list disabled\n"); 2296 return; 2297 } 2298 2299 ac--; 2300 av++; 2301 2302 /* get rules or pipes from kernel, resizing array as necessary */ 2303 nbytes = nalloc; 2304 2305 while (nbytes >= nalloc) { 2306 nalloc = nalloc * 2 + 200; 2307 nbytes = nalloc; 2308 if ((data = realloc(data, nbytes)) == NULL) 2309 err(EX_OSERR, "realloc"); 2310 if (do_cmd(ocmd, data, (uintptr_t)&nbytes) < 0) 2311 err(EX_OSERR, "getsockopt(IP_%s_GET)", 2312 do_pipe ? "DUMMYNET" : "FW"); 2313 } 2314 2315 if (do_pipe) { 2316 list_pipes(data, nbytes, ac, av); 2317 goto done; 2318 } 2319 2320 /* 2321 * Count static rules. They have variable size so we 2322 * need to scan the list to count them. 2323 */ 2324 for (nstat = 1, r = data, lim = (char *)data + nbytes; 2325 r->rulenum < 65535 && (char *)r < lim; 2326 ++nstat, r = NEXT(r) ) 2327 ; /* nothing */ 2328 2329 /* 2330 * Count dynamic rules. This is easier as they have 2331 * fixed size. 2332 */ 2333 r = NEXT(r); 2334 dynrules = (ipfw_dyn_rule *)r ; 2335 n = (char *)r - (char *)data; 2336 ndyn = (nbytes - n) / sizeof *dynrules; 2337 2338 /* if showing stats, figure out column widths ahead of time */ 2339 bcwidth = pcwidth = 0; 2340 if (show_counters) { 2341 for (n = 0, r = data; n < nstat; n++, r = NEXT(r)) { 2342 /* packet counter */ 2343 width = snprintf(NULL, 0, "%llu", 2344 align_uint64(&r->pcnt)); 2345 if (width > pcwidth) 2346 pcwidth = width; 2347 2348 /* byte counter */ 2349 width = snprintf(NULL, 0, "%llu", 2350 align_uint64(&r->bcnt)); 2351 if (width > bcwidth) 2352 bcwidth = width; 2353 } 2354 } 2355 if (do_dynamic && ndyn) { 2356 for (n = 0, d = dynrules; n < ndyn; n++, d++) { 2357 width = snprintf(NULL, 0, "%llu", 2358 align_uint64(&d->pcnt)); 2359 if (width > pcwidth) 2360 pcwidth = width; 2361 2362 width = snprintf(NULL, 0, "%llu", 2363 align_uint64(&d->bcnt)); 2364 if (width > bcwidth) 2365 bcwidth = width; 2366 } 2367 } 2368 /* if no rule numbers were specified, list all rules */ 2369 if (ac == 0) { 2370 for (n = 0, r = data; n < nstat; n++, r = NEXT(r) ) 2371 show_ipfw(r, pcwidth, bcwidth); 2372 2373 if (do_dynamic && ndyn) { 2374 printf("## Dynamic rules (%d):\n", ndyn); 2375 for (n = 0, d = dynrules; n < ndyn; n++, d++) 2376 show_dyn_ipfw(d, pcwidth, bcwidth); 2377 } 2378 goto done; 2379 } 2380 2381 /* display specific rules requested on command line */ 2382 2383 for (lac = ac, lav = av; lac != 0; lac--) { 2384 /* convert command line rule # */ 2385 last = rnum = strtoul(*lav++, &endptr, 10); 2386 if (*endptr == '-') 2387 last = strtoul(endptr+1, &endptr, 10); 2388 if (*endptr) { 2389 exitval = EX_USAGE; 2390 warnx("invalid rule number: %s", *(lav - 1)); 2391 continue; 2392 } 2393 for (n = seen = 0, r = data; n < nstat; n++, r = NEXT(r) ) { 2394 if (r->rulenum > last) 2395 break; 2396 if (r->rulenum >= rnum && r->rulenum <= last) { 2397 show_ipfw(r, pcwidth, bcwidth); 2398 seen = 1; 2399 } 2400 } 2401 if (!seen) { 2402 /* give precedence to other error(s) */ 2403 if (exitval == EX_OK) 2404 exitval = EX_UNAVAILABLE; 2405 warnx("rule %lu does not exist", rnum); 2406 } 2407 } 2408 2409 if (do_dynamic && ndyn) { 2410 printf("## Dynamic rules:\n"); 2411 for (lac = ac, lav = av; lac != 0; lac--) { 2412 last = rnum = strtoul(*lav++, &endptr, 10); 2413 if (*endptr == '-') 2414 last = strtoul(endptr+1, &endptr, 10); 2415 if (*endptr) 2416 /* already warned */ 2417 continue; 2418 for (n = 0, d = dynrules; n < ndyn; n++, d++) { 2419 uint16_t rulenum; 2420 2421 bcopy(&d->rule, &rulenum, sizeof(rulenum)); 2422 if (rulenum > rnum) 2423 break; 2424 if (r->rulenum >= rnum && r->rulenum <= last) 2425 show_dyn_ipfw(d, pcwidth, bcwidth); 2426 } 2427 } 2428 } 2429 2430 ac = 0; 2431 2432done: 2433 free(data); 2434 2435 if (exitval != EX_OK) 2436 exit(exitval); 2437#undef NEXT 2438} 2439 2440static void 2441show_usage(void) 2442{ 2443 fprintf(stderr, "usage: ipfw [options]\n" 2444"do \"ipfw -h\" or see ipfw manpage for details\n" 2445); 2446 exit(EX_USAGE); 2447} 2448 2449static void 2450help(void) 2451{ 2452 fprintf(stderr, 2453"ipfw syntax summary (but please do read the ipfw(8) manpage):\n" 2454"ipfw [-abcdefhnNqStTv] <command> where <command> is one of:\n" 2455"add [num] [set N] [prob x] RULE-BODY\n" 2456"{pipe|queue} N config PIPE-BODY\n" 2457"[pipe|queue] {zero|delete|show} [N{,N}]\n" 2458"set [disable N... enable N...] | move [rule] X to Y | swap X Y | show\n" 2459"table N {add ip[/bits] [value] | delete ip[/bits] | flush | list}\n" 2460"\n" 2461"RULE-BODY: check-state [PARAMS] | ACTION [PARAMS] ADDR [OPTION_LIST]\n" 2462"ACTION: check-state | allow | count | deny | unreach CODE | skipto N |\n" 2463" {divert|tee} PORT | forward ADDR | pipe N | queue N\n" 2464"PARAMS: [log [logamount LOGLIMIT]] [altq QUEUE_NAME]\n" 2465"ADDR: [ MAC dst src ether_type ] \n" 2466" [ ip from IPADDR [ PORT ] to IPADDR [ PORTLIST ] ]\n" 2467" [ ipv6|ip6 from IP6ADDR [ PORT ] to IP6ADDR [ PORTLIST ] ]\n" 2468"IPADDR: [not] { any | me | ip/bits{x,y,z} | table(t[,v]) | IPLIST }\n" 2469"IP6ADDR: [not] { any | me | me6 | ip6/bits | IP6LIST }\n" 2470"IP6LIST: { ip6 | ip6/bits }[,IP6LIST]\n" 2471"IPLIST: { ip | ip/bits | ip:mask }[,IPLIST]\n" 2472"OPTION_LIST: OPTION [OPTION_LIST]\n" 2473"OPTION: bridged | diverted | diverted-loopback | diverted-output |\n" 2474" {dst-ip|src-ip} IPADDR | {dst-ip6|src-ip6|dst-ipv6|src-ipv6} IP6ADDR |\n" 2475" {dst-port|src-port} LIST |\n" 2476" estab | frag | {gid|uid} N | icmptypes LIST | in | out | ipid LIST |\n" 2477" iplen LIST | ipoptions SPEC | ipprecedence | ipsec | iptos SPEC |\n" 2478" ipttl LIST | ipversion VER | keep-state | layer2 | limit ... |\n" 2479" icmp6types LIST | ext6hdr LIST | flow-id N[,N] |\n" 2480" mac ... | mac-type LIST | proto LIST | {recv|xmit|via} {IF|IPADDR} |\n" 2481" setup | {tcpack|tcpseq|tcpwin} NN | tcpflags SPEC | tcpoptions SPEC |\n" 2482" tcpdatalen LIST | verrevpath | versrcreach | antispoof\n" 2483); 2484exit(0); 2485} 2486 2487 2488static int 2489lookup_host (char *host, struct in_addr *ipaddr) 2490{ 2491 struct hostent *he; 2492 2493 if (!inet_aton(host, ipaddr)) { 2494 if ((he = gethostbyname(host)) == NULL) 2495 return(-1); 2496 *ipaddr = *(struct in_addr *)he->h_addr_list[0]; 2497 } 2498 return(0); 2499} 2500 2501/* 2502 * fills the addr and mask fields in the instruction as appropriate from av. 2503 * Update length as appropriate. 2504 * The following formats are allowed: 2505 * me returns O_IP_*_ME 2506 * 1.2.3.4 single IP address 2507 * 1.2.3.4:5.6.7.8 address:mask 2508 * 1.2.3.4/24 address/mask 2509 * 1.2.3.4/26{1,6,5,4,23} set of addresses in a subnet 2510 * We can have multiple comma-separated address/mask entries. 2511 */ 2512static void 2513fill_ip(ipfw_insn_ip *cmd, char *av) 2514{ 2515 int len = 0; 2516 uint32_t *d = ((ipfw_insn_u32 *)cmd)->d; 2517 2518 cmd->o.len &= ~F_LEN_MASK; /* zero len */ 2519 2520 if (_substrcmp(av, "any") == 0) 2521 return; 2522 2523 if (_substrcmp(av, "me") == 0) { 2524 cmd->o.len |= F_INSN_SIZE(ipfw_insn); 2525 return; 2526 } 2527 2528 if (strncmp(av, "table(", 6) == 0) { 2529 char *p = strchr(av + 6, ','); 2530 2531 if (p) 2532 *p++ = '\0'; 2533 cmd->o.opcode = O_IP_DST_LOOKUP; 2534 cmd->o.arg1 = strtoul(av + 6, NULL, 0); 2535 if (p) { 2536 cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32); 2537 d[0] = strtoul(p, NULL, 0); 2538 } else 2539 cmd->o.len |= F_INSN_SIZE(ipfw_insn); 2540 return; 2541 } 2542 2543 while (av) { 2544 /* 2545 * After the address we can have '/' or ':' indicating a mask, 2546 * ',' indicating another address follows, '{' indicating a 2547 * set of addresses of unspecified size. 2548 */ 2549 char *p = strpbrk(av, "/:,{"); 2550 int masklen; 2551 char md; 2552 2553 if (p) { 2554 md = *p; 2555 *p++ = '\0'; 2556 } else 2557 md = '\0'; 2558 2559 if (lookup_host(av, (struct in_addr *)&d[0]) != 0) 2560 errx(EX_NOHOST, "hostname ``%s'' unknown", av); 2561 switch (md) { 2562 case ':': 2563 if (!inet_aton(p, (struct in_addr *)&d[1])) 2564 errx(EX_DATAERR, "bad netmask ``%s''", p); 2565 break; 2566 case '/': 2567 masklen = atoi(p); 2568 if (masklen == 0) 2569 d[1] = htonl(0); /* mask */ 2570 else if (masklen > 32) 2571 errx(EX_DATAERR, "bad width ``%s''", p); 2572 else 2573 d[1] = htonl(~0 << (32 - masklen)); 2574 break; 2575 case '{': /* no mask, assume /24 and put back the '{' */ 2576 d[1] = htonl(~0 << (32 - 24)); 2577 *(--p) = md; 2578 break; 2579 2580 case ',': /* single address plus continuation */ 2581 *(--p) = md; 2582 /* FALLTHROUGH */ 2583 case 0: /* initialization value */ 2584 default: 2585 d[1] = htonl(~0); /* force /32 */ 2586 break; 2587 } 2588 d[0] &= d[1]; /* mask base address with mask */ 2589 /* find next separator */ 2590 if (p) 2591 p = strpbrk(p, ",{"); 2592 if (p && *p == '{') { 2593 /* 2594 * We have a set of addresses. They are stored as follows: 2595 * arg1 is the set size (powers of 2, 2..256) 2596 * addr is the base address IN HOST FORMAT 2597 * mask.. is an array of arg1 bits (rounded up to 2598 * the next multiple of 32) with bits set 2599 * for each host in the map. 2600 */ 2601 uint32_t *map = (uint32_t *)&cmd->mask; 2602 int low, high; 2603 int i = contigmask((uint8_t *)&(d[1]), 32); 2604 2605 if (len > 0) 2606 errx(EX_DATAERR, "address set cannot be in a list"); 2607 if (i < 24 || i > 31) 2608 errx(EX_DATAERR, "invalid set with mask %d\n", i); 2609 cmd->o.arg1 = 1<<(32-i); /* map length */ 2610 d[0] = ntohl(d[0]); /* base addr in host format */ 2611 cmd->o.opcode = O_IP_DST_SET; /* default */ 2612 cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32) + (cmd->o.arg1+31)/32; 2613 for (i = 0; i < (cmd->o.arg1+31)/32 ; i++) 2614 map[i] = 0; /* clear map */ 2615 2616 av = p + 1; 2617 low = d[0] & 0xff; 2618 high = low + cmd->o.arg1 - 1; 2619 /* 2620 * Here, i stores the previous value when we specify a range 2621 * of addresses within a mask, e.g. 45-63. i = -1 means we 2622 * have no previous value. 2623 */ 2624 i = -1; /* previous value in a range */ 2625 while (isdigit(*av)) { 2626 char *s; 2627 int a = strtol(av, &s, 0); 2628 2629 if (s == av) { /* no parameter */ 2630 if (*av != '}') 2631 errx(EX_DATAERR, "set not closed\n"); 2632 if (i != -1) 2633 errx(EX_DATAERR, "incomplete range %d-", i); 2634 break; 2635 } 2636 if (a < low || a > high) 2637 errx(EX_DATAERR, "addr %d out of range [%d-%d]\n", 2638 a, low, high); 2639 a -= low; 2640 if (i == -1) /* no previous in range */ 2641 i = a; 2642 else { /* check that range is valid */ 2643 if (i > a) 2644 errx(EX_DATAERR, "invalid range %d-%d", 2645 i+low, a+low); 2646 if (*s == '-') 2647 errx(EX_DATAERR, "double '-' in range"); 2648 } 2649 for (; i <= a; i++) 2650 map[i/32] |= 1<<(i & 31); 2651 i = -1; 2652 if (*s == '-') 2653 i = a; 2654 else if (*s == '}') 2655 break; 2656 av = s+1; 2657 } 2658 return; 2659 } 2660 av = p; 2661 if (av) /* then *av must be a ',' */ 2662 av++; 2663 2664 /* Check this entry */ 2665 if (d[1] == 0) { /* "any", specified as x.x.x.x/0 */ 2666 /* 2667 * 'any' turns the entire list into a NOP. 2668 * 'not any' never matches, so it is removed from the 2669 * list unless it is the only item, in which case we 2670 * report an error. 2671 */ 2672 if (cmd->o.len & F_NOT) { /* "not any" never matches */ 2673 if (av == NULL && len == 0) /* only this entry */ 2674 errx(EX_DATAERR, "not any never matches"); 2675 } 2676 /* else do nothing and skip this entry */ 2677 return; 2678 } 2679 /* A single IP can be stored in an optimized format */ 2680 if (d[1] == IP_MASK_ALL && av == NULL && len == 0) { 2681 cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32); 2682 return; 2683 } 2684 len += 2; /* two words... */ 2685 d += 2; 2686 } /* end while */ 2687 cmd->o.len |= len+1; 2688} 2689 2690 2691/* Try to find ipv6 address by hostname */ 2692static int 2693lookup_host6 (char *host, struct in6_addr *ip6addr) 2694{ 2695 struct hostent *he; 2696 2697 if (!inet_pton(AF_INET6, host, ip6addr)) { 2698 if ((he = gethostbyname2(host, AF_INET6)) == NULL) 2699 return(-1); 2700 memcpy(ip6addr, he->h_addr_list[0], sizeof( struct in6_addr)); 2701 } 2702 return(0); 2703} 2704 2705 2706/* n2mask sets n bits of the mask */ 2707static void 2708n2mask(struct in6_addr *mask, int n) 2709{ 2710 static int minimask[9] = 2711 { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff }; 2712 u_char *p; 2713 2714 memset(mask, 0, sizeof(struct in6_addr)); 2715 p = (u_char *) mask; 2716 for (; n > 0; p++, n -= 8) { 2717 if (n >= 8) 2718 *p = 0xff; 2719 else 2720 *p = minimask[n]; 2721 } 2722 return; 2723} 2724 2725 2726/* 2727 * fill the addr and mask fields in the instruction as appropriate from av. 2728 * Update length as appropriate. 2729 * The following formats are allowed: 2730 * any matches any IP6. Actually returns an empty instruction. 2731 * me returns O_IP6_*_ME 2732 * 2733 * 03f1::234:123:0342 single IP6 addres 2734 * 03f1::234:123:0342/24 address/mask 2735 * 03f1::234:123:0342/24,03f1::234:123:0343/ List of address 2736 * 2737 * Set of address (as in ipv6) not supported because ipv6 address 2738 * are typically random past the initial prefix. 2739 * Return 1 on success, 0 on failure. 2740 */ 2741static int 2742fill_ip6(ipfw_insn_ip6 *cmd, char *av) 2743{ 2744 int len = 0; 2745 struct in6_addr *d = &(cmd->addr6); 2746 /* 2747 * Needed for multiple address. 2748 * Note d[1] points to struct in6_add r mask6 of cmd 2749 */ 2750 2751 cmd->o.len &= ~F_LEN_MASK; /* zero len */ 2752 2753 if (strcmp(av, "any") == 0) 2754 return (1); 2755 2756 2757 if (strcmp(av, "me") == 0) { /* Set the data for "me" opt*/ 2758 cmd->o.len |= F_INSN_SIZE(ipfw_insn); 2759 return (1); 2760 } 2761 2762 if (strcmp(av, "me6") == 0) { /* Set the data for "me" opt*/ 2763 cmd->o.len |= F_INSN_SIZE(ipfw_insn); 2764 return (1); 2765 } 2766 2767 av = strdup(av); 2768 while (av) { 2769 /* 2770 * After the address we can have '/' indicating a mask, 2771 * or ',' indicating another address follows. 2772 */ 2773 2774 char *p; 2775 int masklen; 2776 char md = '\0'; 2777 2778 if ((p = strpbrk(av, "/,")) ) { 2779 md = *p; /* save the separator */ 2780 *p = '\0'; /* terminate address string */ 2781 p++; /* and skip past it */ 2782 } 2783 /* now p points to NULL, mask or next entry */ 2784 2785 /* lookup stores address in *d as a side effect */ 2786 if (lookup_host6(av, d) != 0) { 2787 /* XXX: failed. Free memory and go */ 2788 errx(EX_DATAERR, "bad address \"%s\"", av); 2789 } 2790 /* next, look at the mask, if any */ 2791 masklen = (md == '/') ? atoi(p) : 128; 2792 if (masklen > 128 || masklen < 0) 2793 errx(EX_DATAERR, "bad width \"%s\''", p); 2794 else 2795 n2mask(&d[1], masklen); 2796 2797 APPLY_MASK(d, &d[1]) /* mask base address with mask */ 2798 2799 /* find next separator */ 2800 2801 if (md == '/') { /* find separator past the mask */ 2802 p = strpbrk(p, ","); 2803 if (p != NULL) 2804 p++; 2805 } 2806 av = p; 2807 2808 /* Check this entry */ 2809 if (masklen == 0) { 2810 /* 2811 * 'any' turns the entire list into a NOP. 2812 * 'not any' never matches, so it is removed from the 2813 * list unless it is the only item, in which case we 2814 * report an error. 2815 */ 2816 if (cmd->o.len & F_NOT && av == NULL && len == 0) 2817 errx(EX_DATAERR, "not any never matches"); 2818 continue; 2819 } 2820 2821 /* 2822 * A single IP can be stored alone 2823 */ 2824 if (masklen == 128 && av == NULL && len == 0) { 2825 len = F_INSN_SIZE(struct in6_addr); 2826 break; 2827 } 2828 2829 /* Update length and pointer to arguments */ 2830 len += F_INSN_SIZE(struct in6_addr)*2; 2831 d += 2; 2832 } /* end while */ 2833 2834 /* 2835 * Total length of the command, remember that 1 is the size of 2836 * the base command. 2837 */ 2838 cmd->o.len |= len+1; 2839 free(av); 2840 return (1); 2841} 2842 2843/* 2844 * fills command for ipv6 flow-id filtering 2845 * note that the 20 bit flow number is stored in a array of u_int32_t 2846 * it's supported lists of flow-id, so in the o.arg1 we store how many 2847 * additional flow-id we want to filter, the basic is 1 2848 */ 2849void 2850fill_flow6( ipfw_insn_u32 *cmd, char *av ) 2851{ 2852 u_int32_t type; /* Current flow number */ 2853 u_int16_t nflow = 0; /* Current flow index */ 2854 char *s = av; 2855 cmd->d[0] = 0; /* Initializing the base number*/ 2856 2857 while (s) { 2858 av = strsep( &s, ",") ; 2859 type = strtoul(av, &av, 0); 2860 if (*av != ',' && *av != '\0') 2861 errx(EX_DATAERR, "invalid ipv6 flow number %s", av); 2862 if (type > 0xfffff) 2863 errx(EX_DATAERR, "flow number out of range %s", av); 2864 cmd->d[nflow] |= type; 2865 nflow++; 2866 } 2867 if( nflow > 0 ) { 2868 cmd->o.opcode = O_FLOW6ID; 2869 cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32) + nflow; 2870 cmd->o.arg1 = nflow; 2871 } 2872 else { 2873 errx(EX_DATAERR, "invalid ipv6 flow number %s", av); 2874 } 2875} 2876 2877static ipfw_insn * 2878add_srcip6(ipfw_insn *cmd, char *av) 2879{ 2880 2881 fill_ip6((ipfw_insn_ip6 *)cmd, av); 2882 if (F_LEN(cmd) == 0) /* any */ 2883 ; 2884 if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn)) { /* "me" */ 2885 cmd->opcode = O_IP6_SRC_ME; 2886 } else if (F_LEN(cmd) == 2887 (F_INSN_SIZE(struct in6_addr) + F_INSN_SIZE(ipfw_insn))) { 2888 /* single IP, no mask*/ 2889 cmd->opcode = O_IP6_SRC; 2890 } else { /* addr/mask opt */ 2891 cmd->opcode = O_IP6_SRC_MASK; 2892 } 2893 return cmd; 2894} 2895 2896static ipfw_insn * 2897add_dstip6(ipfw_insn *cmd, char *av) 2898{ 2899 2900 fill_ip6((ipfw_insn_ip6 *)cmd, av); 2901 if (F_LEN(cmd) == 0) /* any */ 2902 ; 2903 if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn)) { /* "me" */ 2904 cmd->opcode = O_IP6_DST_ME; 2905 } else if (F_LEN(cmd) == 2906 (F_INSN_SIZE(struct in6_addr) + F_INSN_SIZE(ipfw_insn))) { 2907 /* single IP, no mask*/ 2908 cmd->opcode = O_IP6_DST; 2909 } else { /* addr/mask opt */ 2910 cmd->opcode = O_IP6_DST_MASK; 2911 } 2912 return cmd; 2913} 2914 2915 2916/* 2917 * helper function to process a set of flags and set bits in the 2918 * appropriate masks. 2919 */ 2920static void 2921fill_flags(ipfw_insn *cmd, enum ipfw_opcodes opcode, 2922 struct _s_x *flags, char *p) 2923{ 2924 uint8_t set=0, clear=0; 2925 2926 while (p && *p) { 2927 char *q; /* points to the separator */ 2928 int val; 2929 uint8_t *which; /* mask we are working on */ 2930 2931 if (*p == '!') { 2932 p++; 2933 which = &clear; 2934 } else 2935 which = &set; 2936 q = strchr(p, ','); 2937 if (q) 2938 *q++ = '\0'; 2939 val = match_token(flags, p); 2940 if (val <= 0) 2941 errx(EX_DATAERR, "invalid flag %s", p); 2942 *which |= (uint8_t)val; 2943 p = q; 2944 } 2945 cmd->opcode = opcode; 2946 cmd->len = (cmd->len & (F_NOT | F_OR)) | 1; 2947 cmd->arg1 = (set & 0xff) | ( (clear & 0xff) << 8); 2948} 2949 2950 2951static void 2952delete(int ac, char *av[]) 2953{ 2954 uint32_t rulenum; 2955 struct dn_pipe p; 2956 int i; 2957 int exitval = EX_OK; 2958 int do_set = 0; 2959 2960 memset(&p, 0, sizeof p); 2961 2962 av++; ac--; 2963 NEED1("missing rule specification"); 2964 if (ac > 0 && _substrcmp(*av, "set") == 0) { 2965 do_set = 1; /* delete set */ 2966 ac--; av++; 2967 } 2968 2969 /* Rule number */ 2970 while (ac && isdigit(**av)) { 2971 i = atoi(*av); av++; ac--; 2972 if (do_pipe) { 2973 if (do_pipe == 1) 2974 p.pipe_nr = i; 2975 else 2976 p.fs.fs_nr = i; 2977 i = do_cmd(IP_DUMMYNET_DEL, &p, sizeof p); 2978 if (i) { 2979 exitval = 1; 2980 warn("rule %u: setsockopt(IP_DUMMYNET_DEL)", 2981 do_pipe == 1 ? p.pipe_nr : p.fs.fs_nr); 2982 } 2983 } else { 2984 rulenum = (i & 0xffff) | (do_set << 24); 2985 i = do_cmd(IP_FW_DEL, &rulenum, sizeof rulenum); 2986 if (i) { 2987 exitval = EX_UNAVAILABLE; 2988 warn("rule %u: setsockopt(IP_FW_DEL)", 2989 rulenum); 2990 } 2991 } 2992 } 2993 if (exitval != EX_OK) 2994 exit(exitval); 2995} 2996 2997 2998/* 2999 * fill the interface structure. We do not check the name as we can 3000 * create interfaces dynamically, so checking them at insert time 3001 * makes relatively little sense. 3002 * Interface names containing '*', '?', or '[' are assumed to be shell 3003 * patterns which match interfaces. 3004 */ 3005static void 3006fill_iface(ipfw_insn_if *cmd, char *arg) 3007{ 3008 cmd->name[0] = '\0'; 3009 cmd->o.len |= F_INSN_SIZE(ipfw_insn_if); 3010 3011 /* Parse the interface or address */ 3012 if (strcmp(arg, "any") == 0) 3013 cmd->o.len = 0; /* effectively ignore this command */ 3014 else if (!isdigit(*arg)) { 3015 strlcpy(cmd->name, arg, sizeof(cmd->name)); 3016 cmd->p.glob = strpbrk(arg, "*?[") != NULL ? 1 : 0; 3017 } else if (!inet_aton(arg, &cmd->p.ip)) 3018 errx(EX_DATAERR, "bad ip address ``%s''", arg); 3019} 3020 3021static void 3022config_pipe(int ac, char **av) 3023{ 3024 struct dn_pipe p; 3025 int i; 3026 char *end; 3027 void *par = NULL; 3028 3029 memset(&p, 0, sizeof p); 3030 3031 av++; ac--; 3032 /* Pipe number */ 3033 if (ac && isdigit(**av)) { 3034 i = atoi(*av); av++; ac--; 3035 if (do_pipe == 1) 3036 p.pipe_nr = i; 3037 else 3038 p.fs.fs_nr = i; 3039 } 3040 while (ac > 0) { 3041 double d; 3042 int tok = match_token(dummynet_params, *av); 3043 ac--; av++; 3044 3045 switch(tok) { 3046 case TOK_NOERROR: 3047 p.fs.flags_fs |= DN_NOERROR; 3048 break; 3049 3050 case TOK_PLR: 3051 NEED1("plr needs argument 0..1\n"); 3052 d = strtod(av[0], NULL); 3053 if (d > 1) 3054 d = 1; 3055 else if (d < 0) 3056 d = 0; 3057 p.fs.plr = (int)(d*0x7fffffff); 3058 ac--; av++; 3059 break; 3060 3061 case TOK_QUEUE: 3062 NEED1("queue needs queue size\n"); 3063 end = NULL; 3064 p.fs.qsize = strtoul(av[0], &end, 0); 3065 if (*end == 'K' || *end == 'k') { 3066 p.fs.flags_fs |= DN_QSIZE_IS_BYTES; 3067 p.fs.qsize *= 1024; 3068 } else if (*end == 'B' || 3069 _substrcmp2(end, "by", "bytes") == 0) { 3070 p.fs.flags_fs |= DN_QSIZE_IS_BYTES; 3071 } 3072 ac--; av++; 3073 break; 3074 3075 case TOK_BUCKETS: 3076 NEED1("buckets needs argument\n"); 3077 p.fs.rq_size = strtoul(av[0], NULL, 0); 3078 ac--; av++; 3079 break; 3080 3081 case TOK_MASK: 3082 NEED1("mask needs mask specifier\n"); 3083 /* 3084 * per-flow queue, mask is dst_ip, dst_port, 3085 * src_ip, src_port, proto measured in bits 3086 */ 3087 par = NULL; 3088 3089 bzero(&p.fs.flow_mask, sizeof(p.fs.flow_mask)); 3090 end = NULL; 3091 3092 while (ac >= 1) { 3093 uint32_t *p32 = NULL; 3094 uint16_t *p16 = NULL; 3095 uint32_t *p20 = NULL; 3096 struct in6_addr *pa6 = NULL; 3097 uint32_t a; 3098 3099 tok = match_token(dummynet_params, *av); 3100 ac--; av++; 3101 switch(tok) { 3102 case TOK_ALL: 3103 /* 3104 * special case, all bits significant 3105 */ 3106 p.fs.flow_mask.dst_ip = ~0; 3107 p.fs.flow_mask.src_ip = ~0; 3108 p.fs.flow_mask.dst_port = ~0; 3109 p.fs.flow_mask.src_port = ~0; 3110 p.fs.flow_mask.proto = ~0; 3111 n2mask(&(p.fs.flow_mask.dst_ip6), 128); 3112 n2mask(&(p.fs.flow_mask.src_ip6), 128); 3113 p.fs.flow_mask.flow_id6 = ~0; 3114 p.fs.flags_fs |= DN_HAVE_FLOW_MASK; 3115 goto end_mask; 3116 3117 case TOK_DSTIP: 3118 p32 = &p.fs.flow_mask.dst_ip; 3119 break; 3120 3121 case TOK_SRCIP: 3122 p32 = &p.fs.flow_mask.src_ip; 3123 break; 3124 3125 case TOK_DSTIP6: 3126 pa6 = &(p.fs.flow_mask.dst_ip6); 3127 break; 3128 3129 case TOK_SRCIP6: 3130 pa6 = &(p.fs.flow_mask.src_ip6); 3131 break; 3132 3133 case TOK_FLOWID: 3134 p20 = &p.fs.flow_mask.flow_id6; 3135 break; 3136 3137 case TOK_DSTPORT: 3138 p16 = &p.fs.flow_mask.dst_port; 3139 break; 3140 3141 case TOK_SRCPORT: 3142 p16 = &p.fs.flow_mask.src_port; 3143 break; 3144 3145 case TOK_PROTO: 3146 break; 3147 3148 default: 3149 ac++; av--; /* backtrack */ 3150 goto end_mask; 3151 } 3152 if (ac < 1) 3153 errx(EX_USAGE, "mask: value missing"); 3154 if (*av[0] == '/') { 3155 a = strtoul(av[0]+1, &end, 0); 3156 if (pa6 == NULL) 3157 a = (a == 32) ? ~0 : (1 << a) - 1; 3158 } else 3159 a = strtoul(av[0], &end, 0); 3160 if (p32 != NULL) 3161 *p32 = a; 3162 else if (p16 != NULL) { 3163 if (a > 0xFFFF) 3164 errx(EX_DATAERR, 3165 "port mask must be 16 bit"); 3166 *p16 = (uint16_t)a; 3167 } else if (p20 != NULL) { 3168 if (a > 0xfffff) 3169 errx(EX_DATAERR, 3170 "flow_id mask must be 20 bit"); 3171 *p20 = (uint32_t)a; 3172 } else if (pa6 != NULL) { 3173 if (a < 0 || a > 128) 3174 errx(EX_DATAERR, 3175 "in6addr invalid mask len"); 3176 else 3177 n2mask(pa6, a); 3178 } else { 3179 if (a > 0xFF) 3180 errx(EX_DATAERR, 3181 "proto mask must be 8 bit"); 3182 p.fs.flow_mask.proto = (uint8_t)a; 3183 } 3184 if (a != 0) 3185 p.fs.flags_fs |= DN_HAVE_FLOW_MASK; 3186 ac--; av++; 3187 } /* end while, config masks */ 3188end_mask: 3189 break; 3190 3191 case TOK_RED: 3192 case TOK_GRED: 3193 NEED1("red/gred needs w_q/min_th/max_th/max_p\n"); 3194 p.fs.flags_fs |= DN_IS_RED; 3195 if (tok == TOK_GRED) 3196 p.fs.flags_fs |= DN_IS_GENTLE_RED; 3197 /* 3198 * the format for parameters is w_q/min_th/max_th/max_p 3199 */ 3200 if ((end = strsep(&av[0], "/"))) { 3201 double w_q = strtod(end, NULL); 3202 if (w_q > 1 || w_q <= 0) 3203 errx(EX_DATAERR, "0 < w_q <= 1"); 3204 p.fs.w_q = (int) (w_q * (1 << SCALE_RED)); 3205 } 3206 if ((end = strsep(&av[0], "/"))) { 3207 p.fs.min_th = strtoul(end, &end, 0); 3208 if (*end == 'K' || *end == 'k') 3209 p.fs.min_th *= 1024; 3210 } 3211 if ((end = strsep(&av[0], "/"))) { 3212 p.fs.max_th = strtoul(end, &end, 0); 3213 if (*end == 'K' || *end == 'k') 3214 p.fs.max_th *= 1024; 3215 } 3216 if ((end = strsep(&av[0], "/"))) { 3217 double max_p = strtod(end, NULL); 3218 if (max_p > 1 || max_p <= 0) 3219 errx(EX_DATAERR, "0 < max_p <= 1"); 3220 p.fs.max_p = (int)(max_p * (1 << SCALE_RED)); 3221 } 3222 ac--; av++; 3223 break; 3224 3225 case TOK_DROPTAIL: 3226 p.fs.flags_fs &= ~(DN_IS_RED|DN_IS_GENTLE_RED); 3227 break; 3228 3229 case TOK_BW: 3230 NEED1("bw needs bandwidth or interface\n"); 3231 if (do_pipe != 1) 3232 errx(EX_DATAERR, "bandwidth only valid for pipes"); 3233 /* 3234 * set clocking interface or bandwidth value 3235 */ 3236 if (av[0][0] >= 'a' && av[0][0] <= 'z') { 3237 int l = sizeof(p.if_name)-1; 3238 /* interface name */ 3239 strncpy(p.if_name, av[0], l); 3240 p.if_name[l] = '\0'; 3241 p.bandwidth = 0; 3242 } else { 3243 p.if_name[0] = '\0'; 3244 p.bandwidth = strtoul(av[0], &end, 0); 3245 if (*end == 'K' || *end == 'k') { 3246 end++; 3247 p.bandwidth *= 1000; 3248 } else if (*end == 'M') { 3249 end++; 3250 p.bandwidth *= 1000000; 3251 } 3252 if (*end == 'B' || 3253 _substrcmp2(end, "by", "bytes") == 0) 3254 p.bandwidth *= 8; 3255 if (p.bandwidth < 0) 3256 errx(EX_DATAERR, "bandwidth too large"); 3257 } 3258 ac--; av++; 3259 break; 3260 3261 case TOK_DELAY: 3262 if (do_pipe != 1) 3263 errx(EX_DATAERR, "delay only valid for pipes"); 3264 NEED1("delay needs argument 0..10000ms\n"); 3265 p.delay = strtoul(av[0], NULL, 0); 3266 ac--; av++; 3267 break; 3268 3269 case TOK_WEIGHT: 3270 if (do_pipe == 1) 3271 errx(EX_DATAERR,"weight only valid for queues"); 3272 NEED1("weight needs argument 0..100\n"); 3273 p.fs.weight = strtoul(av[0], &end, 0); 3274 ac--; av++; 3275 break; 3276 3277 case TOK_PIPE: 3278 if (do_pipe == 1) 3279 errx(EX_DATAERR,"pipe only valid for queues"); 3280 NEED1("pipe needs pipe_number\n"); 3281 p.fs.parent_nr = strtoul(av[0], &end, 0); 3282 ac--; av++; 3283 break; 3284 3285 default: 3286 errx(EX_DATAERR, "unrecognised option ``%s''", av[-1]); 3287 } 3288 } 3289 if (do_pipe == 1) { 3290 if (p.pipe_nr == 0) 3291 errx(EX_DATAERR, "pipe_nr must be > 0"); 3292 if (p.delay > 10000) 3293 errx(EX_DATAERR, "delay must be < 10000"); 3294 } else { /* do_pipe == 2, queue */ 3295 if (p.fs.parent_nr == 0) 3296 errx(EX_DATAERR, "pipe must be > 0"); 3297 if (p.fs.weight >100) 3298 errx(EX_DATAERR, "weight must be <= 100"); 3299 } 3300 if (p.fs.flags_fs & DN_QSIZE_IS_BYTES) { 3301 if (p.fs.qsize > 1024*1024) 3302 errx(EX_DATAERR, "queue size must be < 1MB"); 3303 } else { 3304 if (p.fs.qsize > 100) 3305 errx(EX_DATAERR, "2 <= queue size <= 100"); 3306 } 3307 if (p.fs.flags_fs & DN_IS_RED) { 3308 size_t len; 3309 int lookup_depth, avg_pkt_size; 3310 double s, idle, weight, w_q; 3311 struct clockinfo ck; 3312 int t; 3313 3314 if (p.fs.min_th >= p.fs.max_th) 3315 errx(EX_DATAERR, "min_th %d must be < than max_th %d", 3316 p.fs.min_th, p.fs.max_th); 3317 if (p.fs.max_th == 0) 3318 errx(EX_DATAERR, "max_th must be > 0"); 3319 3320 len = sizeof(int); 3321 if (sysctlbyname("net.inet.ip.dummynet.red_lookup_depth", 3322 &lookup_depth, &len, NULL, 0) == -1) 3323 3324 errx(1, "sysctlbyname(\"%s\")", 3325 "net.inet.ip.dummynet.red_lookup_depth"); 3326 if (lookup_depth == 0) 3327 errx(EX_DATAERR, "net.inet.ip.dummynet.red_lookup_depth" 3328 " must be greater than zero"); 3329 3330 len = sizeof(int); 3331 if (sysctlbyname("net.inet.ip.dummynet.red_avg_pkt_size", 3332 &avg_pkt_size, &len, NULL, 0) == -1) 3333 3334 errx(1, "sysctlbyname(\"%s\")", 3335 "net.inet.ip.dummynet.red_avg_pkt_size"); 3336 if (avg_pkt_size == 0) 3337 errx(EX_DATAERR, 3338 "net.inet.ip.dummynet.red_avg_pkt_size must" 3339 " be greater than zero"); 3340 3341 len = sizeof(struct clockinfo); 3342 if (sysctlbyname("kern.clockrate", &ck, &len, NULL, 0) == -1) 3343 errx(1, "sysctlbyname(\"%s\")", "kern.clockrate"); 3344 3345 /* 3346 * Ticks needed for sending a medium-sized packet. 3347 * Unfortunately, when we are configuring a WF2Q+ queue, we 3348 * do not have bandwidth information, because that is stored 3349 * in the parent pipe, and also we have multiple queues 3350 * competing for it. So we set s=0, which is not very 3351 * correct. But on the other hand, why do we want RED with 3352 * WF2Q+ ? 3353 */ 3354 if (p.bandwidth==0) /* this is a WF2Q+ queue */ 3355 s = 0; 3356 else 3357 s = ck.hz * avg_pkt_size * 8 / p.bandwidth; 3358 3359 /* 3360 * max idle time (in ticks) before avg queue size becomes 0. 3361 * NOTA: (3/w_q) is approx the value x so that 3362 * (1-w_q)^x < 10^-3. 3363 */ 3364 w_q = ((double)p.fs.w_q) / (1 << SCALE_RED); 3365 idle = s * 3. / w_q; 3366 p.fs.lookup_step = (int)idle / lookup_depth; 3367 if (!p.fs.lookup_step) 3368 p.fs.lookup_step = 1; 3369 weight = 1 - w_q; 3370 for (t = p.fs.lookup_step; t > 0; --t) 3371 weight *= weight; 3372 p.fs.lookup_weight = (int)(weight * (1 << SCALE_RED)); 3373 } 3374 i = do_cmd(IP_DUMMYNET_CONFIGURE, &p, sizeof p); 3375 if (i) 3376 err(1, "setsockopt(%s)", "IP_DUMMYNET_CONFIGURE"); 3377} 3378 3379static void 3380get_mac_addr_mask(char *p, uint8_t *addr, uint8_t *mask) 3381{ 3382 int i, l; 3383 3384 for (i=0; i<6; i++) 3385 addr[i] = mask[i] = 0; 3386 if (strcmp(p, "any") == 0) 3387 return; 3388 3389 for (i=0; *p && i<6;i++, p++) { 3390 addr[i] = strtol(p, &p, 16); 3391 if (*p != ':') /* we start with the mask */ 3392 break; 3393 } 3394 if (*p == '/') { /* mask len */ 3395 l = strtol(p+1, &p, 0); 3396 for (i=0; l>0; l -=8, i++) 3397 mask[i] = (l >=8) ? 0xff : (~0) << (8-l); 3398 } else if (*p == '&') { /* mask */ 3399 for (i=0, p++; *p && i<6;i++, p++) { 3400 mask[i] = strtol(p, &p, 16); 3401 if (*p != ':') 3402 break; 3403 } 3404 } else if (*p == '\0') { 3405 for (i=0; i<6; i++) 3406 mask[i] = 0xff; 3407 } 3408 for (i=0; i<6; i++) 3409 addr[i] &= mask[i]; 3410} 3411 3412/* 3413 * helper function, updates the pointer to cmd with the length 3414 * of the current command, and also cleans up the first word of 3415 * the new command in case it has been clobbered before. 3416 */ 3417static ipfw_insn * 3418next_cmd(ipfw_insn *cmd) 3419{ 3420 cmd += F_LEN(cmd); 3421 bzero(cmd, sizeof(*cmd)); 3422 return cmd; 3423} 3424 3425/* 3426 * Takes arguments and copies them into a comment 3427 */ 3428static void 3429fill_comment(ipfw_insn *cmd, int ac, char **av) 3430{ 3431 int i, l; 3432 char *p = (char *)(cmd + 1); 3433 3434 cmd->opcode = O_NOP; 3435 cmd->len = (cmd->len & (F_NOT | F_OR)); 3436 3437 /* Compute length of comment string. */ 3438 for (i = 0, l = 0; i < ac; i++) 3439 l += strlen(av[i]) + 1; 3440 if (l == 0) 3441 return; 3442 if (l > 84) 3443 errx(EX_DATAERR, 3444 "comment too long (max 80 chars)"); 3445 l = 1 + (l+3)/4; 3446 cmd->len = (cmd->len & (F_NOT | F_OR)) | l; 3447 for (i = 0; i < ac; i++) { 3448 strcpy(p, av[i]); 3449 p += strlen(av[i]); 3450 *p++ = ' '; 3451 } 3452 *(--p) = '\0'; 3453} 3454 3455/* 3456 * A function to fill simple commands of size 1. 3457 * Existing flags are preserved. 3458 */ 3459static void 3460fill_cmd(ipfw_insn *cmd, enum ipfw_opcodes opcode, int flags, uint16_t arg) 3461{ 3462 cmd->opcode = opcode; 3463 cmd->len = ((cmd->len | flags) & (F_NOT | F_OR)) | 1; 3464 cmd->arg1 = arg; 3465} 3466 3467/* 3468 * Fetch and add the MAC address and type, with masks. This generates one or 3469 * two microinstructions, and returns the pointer to the last one. 3470 */ 3471static ipfw_insn * 3472add_mac(ipfw_insn *cmd, int ac, char *av[]) 3473{ 3474 ipfw_insn_mac *mac; 3475 3476 if (ac < 2) 3477 errx(EX_DATAERR, "MAC dst src"); 3478 3479 cmd->opcode = O_MACADDR2; 3480 cmd->len = (cmd->len & (F_NOT | F_OR)) | F_INSN_SIZE(ipfw_insn_mac); 3481 3482 mac = (ipfw_insn_mac *)cmd; 3483 get_mac_addr_mask(av[0], mac->addr, mac->mask); /* dst */ 3484 get_mac_addr_mask(av[1], &(mac->addr[6]), &(mac->mask[6])); /* src */ 3485 return cmd; 3486} 3487 3488static ipfw_insn * 3489add_mactype(ipfw_insn *cmd, int ac, char *av) 3490{ 3491 if (ac < 1) 3492 errx(EX_DATAERR, "missing MAC type"); 3493 if (strcmp(av, "any") != 0) { /* we have a non-null type */ 3494 fill_newports((ipfw_insn_u16 *)cmd, av, IPPROTO_ETHERTYPE); 3495 cmd->opcode = O_MAC_TYPE; 3496 return cmd; 3497 } else 3498 return NULL; 3499} 3500 3501static ipfw_insn * 3502add_proto(ipfw_insn *cmd, char *av, u_char *proto) 3503{ 3504 struct protoent *pe; 3505 3506 *proto = IPPROTO_IP; 3507 3508 if (_substrcmp(av, "all") == 0) 3509 ; /* same as "ip" */ 3510 else if ((*proto = atoi(av)) > 0) 3511 ; /* all done! */ 3512 else if ((pe = getprotobyname(av)) != NULL) 3513 *proto = pe->p_proto; 3514 else if (strcmp(av, "ipv6") == 0 || strcmp(av, "ip6")) 3515 *proto = IPPROTO_IPV6; 3516 else 3517 return NULL; 3518 if (*proto != IPPROTO_IP && *proto != IPPROTO_IPV6) 3519 fill_cmd(cmd, O_PROTO, 0, *proto); 3520 3521 return cmd; 3522} 3523 3524static ipfw_insn * 3525add_srcip(ipfw_insn *cmd, char *av) 3526{ 3527 fill_ip((ipfw_insn_ip *)cmd, av); 3528 if (cmd->opcode == O_IP_DST_SET) /* set */ 3529 cmd->opcode = O_IP_SRC_SET; 3530 else if (cmd->opcode == O_IP_DST_LOOKUP) /* table */ 3531 cmd->opcode = O_IP_SRC_LOOKUP; 3532 else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn)) /* me */ 3533 cmd->opcode = O_IP_SRC_ME; 3534 else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn_u32)) /* one IP */ 3535 cmd->opcode = O_IP_SRC; 3536 else /* addr/mask */ 3537 cmd->opcode = O_IP_SRC_MASK; 3538 return cmd; 3539} 3540 3541static ipfw_insn * 3542add_dstip(ipfw_insn *cmd, char *av) 3543{ 3544 fill_ip((ipfw_insn_ip *)cmd, av); 3545 if (cmd->opcode == O_IP_DST_SET) /* set */ 3546 ; 3547 else if (cmd->opcode == O_IP_DST_LOOKUP) /* table */ 3548 ; 3549 else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn)) /* me */ 3550 cmd->opcode = O_IP_DST_ME; 3551 else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn_u32)) /* one IP */ 3552 cmd->opcode = O_IP_DST; 3553 else /* addr/mask */ 3554 cmd->opcode = O_IP_DST_MASK; 3555 return cmd; 3556} 3557 3558static ipfw_insn * 3559add_ports(ipfw_insn *cmd, char *av, u_char proto, int opcode) 3560{ 3561 if (_substrcmp(av, "any") == 0) { 3562 return NULL; 3563 } else if (fill_newports((ipfw_insn_u16 *)cmd, av, proto)) { 3564 /* XXX todo: check that we have a protocol with ports */ 3565 cmd->opcode = opcode; 3566 return cmd; 3567 } 3568 return NULL; 3569} 3570 3571static ipfw_insn * 3572add_src(ipfw_insn *cmd, char *av, u_char proto) 3573{ 3574 struct in6_addr a; 3575 3576 if (proto == IPPROTO_IPV6 || strcmp(av, "me6") == 0 || 3577 inet_pton(AF_INET6, av, &a)) 3578 return add_srcip6(cmd, av); 3579 /* XXX: should check for IPv4, not !IPv6 */ 3580 if (proto == IPPROTO_IP || strcmp(av, "me") == 0 || 3581 !inet_pton(AF_INET6, av, &a)) 3582 return add_srcip(cmd, av); 3583 if (strcmp(av, "any") != 0) 3584 return cmd; 3585 3586 return NULL; 3587} 3588 3589static ipfw_insn * 3590add_dst(ipfw_insn *cmd, char *av, u_char proto) 3591{ 3592 struct in6_addr a; 3593 3594 if (proto == IPPROTO_IPV6 || strcmp(av, "me6") == 0 || 3595 inet_pton(AF_INET6, av, &a)) 3596 return add_dstip6(cmd, av); 3597 /* XXX: should check for IPv4, not !IPv6 */ 3598 if (proto == IPPROTO_IP || strcmp(av, "me") == 0 || 3599 !inet_pton(AF_INET6, av, &a)) 3600 return add_dstip(cmd, av); 3601 if (strcmp(av, "any") != 0) 3602 return cmd; 3603 3604 return NULL; 3605} 3606 3607/* 3608 * Parse arguments and assemble the microinstructions which make up a rule. 3609 * Rules are added into the 'rulebuf' and then copied in the correct order 3610 * into the actual rule. 3611 * 3612 * The syntax for a rule starts with the action, followed by 3613 * optional action parameters, and the various match patterns. 3614 * In the assembled microcode, the first opcode must be an O_PROBE_STATE 3615 * (generated if the rule includes a keep-state option), then the 3616 * various match patterns, log/altq actions, and the actual action. 3617 * 3618 */ 3619static void 3620add(int ac, char *av[]) 3621{ 3622 /* 3623 * rules are added into the 'rulebuf' and then copied in 3624 * the correct order into the actual rule. 3625 * Some things that need to go out of order (prob, action etc.) 3626 * go into actbuf[]. 3627 */ 3628 static uint32_t rulebuf[255], actbuf[255], cmdbuf[255]; 3629 3630 ipfw_insn *src, *dst, *cmd, *action, *prev=NULL; 3631 ipfw_insn *first_cmd; /* first match pattern */ 3632 3633 struct ip_fw *rule; 3634 3635 /* 3636 * various flags used to record that we entered some fields. 3637 */ 3638 ipfw_insn *have_state = NULL; /* check-state or keep-state */ 3639 ipfw_insn *have_log = NULL, *have_altq = NULL; 3640 size_t len; 3641 3642 int i; 3643 3644 int open_par = 0; /* open parenthesis ( */ 3645 3646 /* proto is here because it is used to fetch ports */ 3647 u_char proto = IPPROTO_IP; /* default protocol */ 3648 3649 double match_prob = 1; /* match probability, default is always match */ 3650 3651 bzero(actbuf, sizeof(actbuf)); /* actions go here */ 3652 bzero(cmdbuf, sizeof(cmdbuf)); 3653 bzero(rulebuf, sizeof(rulebuf)); 3654 3655 rule = (struct ip_fw *)rulebuf; 3656 cmd = (ipfw_insn *)cmdbuf; 3657 action = (ipfw_insn *)actbuf; 3658 3659 av++; ac--; 3660 3661 /* [rule N] -- Rule number optional */ 3662 if (ac && isdigit(**av)) { 3663 rule->rulenum = atoi(*av); 3664 av++; 3665 ac--; 3666 } 3667 3668 /* [set N] -- set number (0..RESVD_SET), optional */ 3669 if (ac > 1 && _substrcmp(*av, "set") == 0) { 3670 int set = strtoul(av[1], NULL, 10); 3671 if (set < 0 || set > RESVD_SET) 3672 errx(EX_DATAERR, "illegal set %s", av[1]); 3673 rule->set = set; 3674 av += 2; ac -= 2; 3675 } 3676 3677 /* [prob D] -- match probability, optional */ 3678 if (ac > 1 && _substrcmp(*av, "prob") == 0) { 3679 match_prob = strtod(av[1], NULL); 3680 3681 if (match_prob <= 0 || match_prob > 1) 3682 errx(EX_DATAERR, "illegal match prob. %s", av[1]); 3683 av += 2; ac -= 2; 3684 } 3685 3686 /* action -- mandatory */ 3687 NEED1("missing action"); 3688 i = match_token(rule_actions, *av); 3689 ac--; av++; 3690 action->len = 1; /* default */ 3691 switch(i) { 3692 case TOK_CHECKSTATE: 3693 have_state = action; 3694 action->opcode = O_CHECK_STATE; 3695 break; 3696 3697 case TOK_ACCEPT: 3698 action->opcode = O_ACCEPT; 3699 break; 3700 3701 case TOK_DENY: 3702 action->opcode = O_DENY; 3703 action->arg1 = 0; 3704 break; 3705 3706 case TOK_REJECT: 3707 action->opcode = O_REJECT; 3708 action->arg1 = ICMP_UNREACH_HOST; 3709 break; 3710 3711 case TOK_RESET: 3712 action->opcode = O_REJECT; 3713 action->arg1 = ICMP_REJECT_RST; 3714 break; 3715 3716 case TOK_UNREACH: 3717 action->opcode = O_REJECT; 3718 NEED1("missing reject code"); 3719 fill_reject_code(&action->arg1, *av); 3720 ac--; av++; 3721 break; 3722 3723 case TOK_COUNT: 3724 action->opcode = O_COUNT; 3725 break; 3726 3727 case TOK_QUEUE: 3728 case TOK_PIPE: 3729 action->len = F_INSN_SIZE(ipfw_insn_pipe); 3730 case TOK_SKIPTO: 3731 if (i == TOK_QUEUE) 3732 action->opcode = O_QUEUE; 3733 else if (i == TOK_PIPE) 3734 action->opcode = O_PIPE; 3735 else if (i == TOK_SKIPTO) 3736 action->opcode = O_SKIPTO; 3737 NEED1("missing skipto/pipe/queue number"); 3738 action->arg1 = strtoul(*av, NULL, 10); 3739 av++; ac--; 3740 break; 3741 3742 case TOK_DIVERT: 3743 case TOK_TEE: 3744 action->opcode = (i == TOK_DIVERT) ? O_DIVERT : O_TEE; 3745 NEED1("missing divert/tee port"); 3746 action->arg1 = strtoul(*av, NULL, 0); 3747 if (action->arg1 == 0) { 3748 struct servent *s; 3749 setservent(1); 3750 s = getservbyname(av[0], "divert"); 3751 if (s != NULL) 3752 action->arg1 = ntohs(s->s_port); 3753 else 3754 errx(EX_DATAERR, "illegal divert/tee port"); 3755 } 3756 ac--; av++; 3757 break; 3758 3759 case TOK_NETGRAPH: 3760 case TOK_NGTEE: 3761 action->opcode = (i == TOK_NETGRAPH ) ? O_NETGRAPH : O_NGTEE; 3762 NEED1("missing netgraph cookie"); 3763 action->arg1 = strtoul(*av, NULL, 0); 3764 if (action->arg1 == 0) 3765 errx(EX_DATAERR, "illegal netgraph cookie"); 3766 ac--; av++; 3767 break; 3768 3769 case TOK_FORWARD: { 3770 ipfw_insn_sa *p = (ipfw_insn_sa *)action; 3771 char *s, *end; 3772 3773 NEED1("missing forward address[:port]"); 3774 3775 action->opcode = O_FORWARD_IP; 3776 action->len = F_INSN_SIZE(ipfw_insn_sa); 3777 3778 p->sa.sin_len = sizeof(struct sockaddr_in); 3779 p->sa.sin_family = AF_INET; 3780 p->sa.sin_port = 0; 3781 /* 3782 * locate the address-port separator (':' or ',') 3783 */ 3784 s = strchr(*av, ':'); 3785 if (s == NULL) 3786 s = strchr(*av, ','); 3787 if (s != NULL) { 3788 *(s++) = '\0'; 3789 i = strtoport(s, &end, 0 /* base */, 0 /* proto */); 3790 if (s == end) 3791 errx(EX_DATAERR, 3792 "illegal forwarding port ``%s''", s); 3793 p->sa.sin_port = (u_short)i; 3794 } 3795 lookup_host(*av, &(p->sa.sin_addr)); 3796 } 3797 ac--; av++; 3798 break; 3799 3800 case TOK_COMMENT: 3801 /* pretend it is a 'count' rule followed by the comment */ 3802 action->opcode = O_COUNT; 3803 ac++; av--; /* go back... */ 3804 break; 3805 3806 default: 3807 errx(EX_DATAERR, "invalid action %s\n", av[-1]); 3808 } 3809 action = next_cmd(action); 3810 3811 /* 3812 * [altq queuename] -- altq tag, optional 3813 * [log [logamount N]] -- log, optional 3814 * 3815 * If they exist, it go first in the cmdbuf, but then it is 3816 * skipped in the copy section to the end of the buffer. 3817 */ 3818 while (ac != 0 && (i = match_token(rule_action_params, *av)) != -1) { 3819 ac--; av++; 3820 switch (i) { 3821 case TOK_LOG: 3822 { 3823 ipfw_insn_log *c = (ipfw_insn_log *)cmd; 3824 int l; 3825 3826 if (have_log) 3827 errx(EX_DATAERR, 3828 "log cannot be specified more than once"); 3829 have_log = (ipfw_insn *)c; 3830 cmd->len = F_INSN_SIZE(ipfw_insn_log); 3831 cmd->opcode = O_LOG; 3832 if (ac && _substrcmp(*av, "logamount") == 0) { 3833 ac--; av++; 3834 NEED1("logamount requires argument"); 3835 l = atoi(*av); 3836 if (l < 0) 3837 errx(EX_DATAERR, 3838 "logamount must be positive"); 3839 c->max_log = l; 3840 ac--; av++; 3841 } else { 3842 len = sizeof(c->max_log); 3843 if (sysctlbyname("net.inet.ip.fw.verbose_limit", 3844 &c->max_log, &len, NULL, 0) == -1) 3845 errx(1, "sysctlbyname(\"%s\")", 3846 "net.inet.ip.fw.verbose_limit"); 3847 } 3848 } 3849 break; 3850 3851 case TOK_ALTQ: 3852 { 3853 ipfw_insn_altq *a = (ipfw_insn_altq *)cmd; 3854 3855 NEED1("missing altq queue name"); 3856 if (have_altq) 3857 errx(EX_DATAERR, 3858 "altq cannot be specified more than once"); 3859 have_altq = (ipfw_insn *)a; 3860 cmd->len = F_INSN_SIZE(ipfw_insn_altq); 3861 cmd->opcode = O_ALTQ; 3862 fill_altq_qid(&a->qid, *av); 3863 ac--; av++; 3864 } 3865 break; 3866 3867 default: 3868 abort(); 3869 } 3870 cmd = next_cmd(cmd); 3871 } 3872 3873 if (have_state) /* must be a check-state, we are done */ 3874 goto done; 3875 3876#define OR_START(target) \ 3877 if (ac && (*av[0] == '(' || *av[0] == '{')) { \ 3878 if (open_par) \ 3879 errx(EX_USAGE, "nested \"(\" not allowed\n"); \ 3880 prev = NULL; \ 3881 open_par = 1; \ 3882 if ( (av[0])[1] == '\0') { \ 3883 ac--; av++; \ 3884 } else \ 3885 (*av)++; \ 3886 } \ 3887 target: \ 3888 3889 3890#define CLOSE_PAR \ 3891 if (open_par) { \ 3892 if (ac && ( \ 3893 strcmp(*av, ")") == 0 || \ 3894 strcmp(*av, "}") == 0)) { \ 3895 prev = NULL; \ 3896 open_par = 0; \ 3897 ac--; av++; \ 3898 } else \ 3899 errx(EX_USAGE, "missing \")\"\n"); \ 3900 } 3901 3902#define NOT_BLOCK \ 3903 if (ac && _substrcmp(*av, "not") == 0) { \ 3904 if (cmd->len & F_NOT) \ 3905 errx(EX_USAGE, "double \"not\" not allowed\n"); \ 3906 cmd->len |= F_NOT; \ 3907 ac--; av++; \ 3908 } 3909 3910#define OR_BLOCK(target) \ 3911 if (ac && _substrcmp(*av, "or") == 0) { \ 3912 if (prev == NULL || open_par == 0) \ 3913 errx(EX_DATAERR, "invalid OR block"); \ 3914 prev->len |= F_OR; \ 3915 ac--; av++; \ 3916 goto target; \ 3917 } \ 3918 CLOSE_PAR; 3919 3920 first_cmd = cmd; 3921 3922#if 0 3923 /* 3924 * MAC addresses, optional. 3925 * If we have this, we skip the part "proto from src to dst" 3926 * and jump straight to the option parsing. 3927 */ 3928 NOT_BLOCK; 3929 NEED1("missing protocol"); 3930 if (_substrcmp(*av, "MAC") == 0 || 3931 _substrcmp(*av, "mac") == 0) { 3932 ac--; av++; /* the "MAC" keyword */ 3933 add_mac(cmd, ac, av); /* exits in case of errors */ 3934 cmd = next_cmd(cmd); 3935 ac -= 2; av += 2; /* dst-mac and src-mac */ 3936 NOT_BLOCK; 3937 NEED1("missing mac type"); 3938 if (add_mactype(cmd, ac, av[0])) 3939 cmd = next_cmd(cmd); 3940 ac--; av++; /* any or mac-type */ 3941 goto read_options; 3942 } 3943#endif 3944 3945 /* 3946 * protocol, mandatory 3947 */ 3948 OR_START(get_proto); 3949 NOT_BLOCK; 3950 NEED1("missing protocol"); 3951 if (add_proto(cmd, *av, &proto)) { 3952 av++; ac--; 3953 if (F_LEN(cmd) == 0) /* plain IP */ 3954 proto = 0; 3955 else { 3956 proto = cmd->arg1; 3957 prev = cmd; 3958 cmd = next_cmd(cmd); 3959 } 3960 } else if (first_cmd != cmd) { 3961 errx(EX_DATAERR, "invalid protocol ``%s''", *av); 3962 } else 3963 goto read_options; 3964 OR_BLOCK(get_proto); 3965 3966 /* 3967 * "from", mandatory 3968 */ 3969 if (!ac || _substrcmp(*av, "from") != 0) 3970 errx(EX_USAGE, "missing ``from''"); 3971 ac--; av++; 3972 3973 /* 3974 * source IP, mandatory 3975 */ 3976 OR_START(source_ip); 3977 NOT_BLOCK; /* optional "not" */ 3978 NEED1("missing source address"); 3979 if (add_src(cmd, *av, proto)) { 3980 ac--; av++; 3981 if (F_LEN(cmd) != 0) { /* ! any */ 3982 prev = cmd; 3983 cmd = next_cmd(cmd); 3984 } 3985 } else 3986 errx(EX_USAGE, "bad source address %s", *av); 3987 OR_BLOCK(source_ip); 3988 3989 /* 3990 * source ports, optional 3991 */ 3992 NOT_BLOCK; /* optional "not" */ 3993 if (ac) { 3994 if (_substrcmp(*av, "any") == 0 || 3995 add_ports(cmd, *av, proto, O_IP_SRCPORT)) { 3996 ac--; av++; 3997 if (F_LEN(cmd) != 0) 3998 cmd = next_cmd(cmd); 3999 } 4000 } 4001 4002 /* 4003 * "to", mandatory 4004 */ 4005 if (!ac || _substrcmp(*av, "to") != 0) 4006 errx(EX_USAGE, "missing ``to''"); 4007 av++; ac--; 4008 4009 /* 4010 * destination, mandatory 4011 */ 4012 OR_START(dest_ip); 4013 NOT_BLOCK; /* optional "not" */ 4014 NEED1("missing dst address"); 4015 if (add_dst(cmd, *av, proto)) { 4016 ac--; av++; 4017 if (F_LEN(cmd) != 0) { /* ! any */ 4018 prev = cmd; 4019 cmd = next_cmd(cmd); 4020 } 4021 } else 4022 errx( EX_USAGE, "bad destination address %s", *av); 4023 OR_BLOCK(dest_ip); 4024 4025 /* 4026 * dest. ports, optional 4027 */ 4028 NOT_BLOCK; /* optional "not" */ 4029 if (ac) { 4030 if (_substrcmp(*av, "any") == 0 || 4031 add_ports(cmd, *av, proto, O_IP_DSTPORT)) { 4032 ac--; av++; 4033 if (F_LEN(cmd) != 0) 4034 cmd = next_cmd(cmd); 4035 } 4036 } 4037 4038read_options: 4039 if (ac && first_cmd == cmd) { 4040 /* 4041 * nothing specified so far, store in the rule to ease 4042 * printout later. 4043 */ 4044 rule->_pad = 1; 4045 } 4046 prev = NULL; 4047 while (ac) { 4048 char *s; 4049 ipfw_insn_u32 *cmd32; /* alias for cmd */ 4050 4051 s = *av; 4052 cmd32 = (ipfw_insn_u32 *)cmd; 4053 4054 if (*s == '!') { /* alternate syntax for NOT */ 4055 if (cmd->len & F_NOT) 4056 errx(EX_USAGE, "double \"not\" not allowed\n"); 4057 cmd->len = F_NOT; 4058 s++; 4059 } 4060 i = match_token(rule_options, s); 4061 ac--; av++; 4062 switch(i) { 4063 case TOK_NOT: 4064 if (cmd->len & F_NOT) 4065 errx(EX_USAGE, "double \"not\" not allowed\n"); 4066 cmd->len = F_NOT; 4067 break; 4068 4069 case TOK_OR: 4070 if (open_par == 0 || prev == NULL) 4071 errx(EX_USAGE, "invalid \"or\" block\n"); 4072 prev->len |= F_OR; 4073 break; 4074 4075 case TOK_STARTBRACE: 4076 if (open_par) 4077 errx(EX_USAGE, "+nested \"(\" not allowed\n"); 4078 open_par = 1; 4079 break; 4080 4081 case TOK_ENDBRACE: 4082 if (!open_par) 4083 errx(EX_USAGE, "+missing \")\"\n"); 4084 open_par = 0; 4085 prev = NULL; 4086 break; 4087 4088 case TOK_IN: 4089 fill_cmd(cmd, O_IN, 0, 0); 4090 break; 4091 4092 case TOK_OUT: 4093 cmd->len ^= F_NOT; /* toggle F_NOT */ 4094 fill_cmd(cmd, O_IN, 0, 0); 4095 break; 4096 4097 case TOK_DIVERTED: 4098 fill_cmd(cmd, O_DIVERTED, 0, 3); 4099 break; 4100 4101 case TOK_DIVERTEDLOOPBACK: 4102 fill_cmd(cmd, O_DIVERTED, 0, 1); 4103 break; 4104 4105 case TOK_DIVERTEDOUTPUT: 4106 fill_cmd(cmd, O_DIVERTED, 0, 2); 4107 break; 4108 4109 case TOK_FRAG: 4110 fill_cmd(cmd, O_FRAG, 0, 0); 4111 break; 4112 4113 case TOK_LAYER2: 4114 fill_cmd(cmd, O_LAYER2, 0, 0); 4115 break; 4116 4117 case TOK_XMIT: 4118 case TOK_RECV: 4119 case TOK_VIA: 4120 NEED1("recv, xmit, via require interface name" 4121 " or address"); 4122 fill_iface((ipfw_insn_if *)cmd, av[0]); 4123 ac--; av++; 4124 if (F_LEN(cmd) == 0) /* not a valid address */ 4125 break; 4126 if (i == TOK_XMIT) 4127 cmd->opcode = O_XMIT; 4128 else if (i == TOK_RECV) 4129 cmd->opcode = O_RECV; 4130 else if (i == TOK_VIA) 4131 cmd->opcode = O_VIA; 4132 break; 4133 4134 case TOK_ICMPTYPES: 4135 NEED1("icmptypes requires list of types"); 4136 fill_icmptypes((ipfw_insn_u32 *)cmd, *av); 4137 av++; ac--; 4138 break; 4139 4140 case TOK_ICMP6TYPES: 4141 NEED1("icmptypes requires list of types"); 4142 fill_icmp6types((ipfw_insn_icmp6 *)cmd, *av); 4143 av++; ac--; 4144 break; 4145 4146 case TOK_IPTTL: 4147 NEED1("ipttl requires TTL"); 4148 if (strpbrk(*av, "-,")) { 4149 if (!add_ports(cmd, *av, 0, O_IPTTL)) 4150 errx(EX_DATAERR, "invalid ipttl %s", *av); 4151 } else 4152 fill_cmd(cmd, O_IPTTL, 0, strtoul(*av, NULL, 0)); 4153 ac--; av++; 4154 break; 4155 4156 case TOK_IPID: 4157 NEED1("ipid requires id"); 4158 if (strpbrk(*av, "-,")) { 4159 if (!add_ports(cmd, *av, 0, O_IPID)) 4160 errx(EX_DATAERR, "invalid ipid %s", *av); 4161 } else 4162 fill_cmd(cmd, O_IPID, 0, strtoul(*av, NULL, 0)); 4163 ac--; av++; 4164 break; 4165 4166 case TOK_IPLEN: 4167 NEED1("iplen requires length"); 4168 if (strpbrk(*av, "-,")) { 4169 if (!add_ports(cmd, *av, 0, O_IPLEN)) 4170 errx(EX_DATAERR, "invalid ip len %s", *av); 4171 } else 4172 fill_cmd(cmd, O_IPLEN, 0, strtoul(*av, NULL, 0)); 4173 ac--; av++; 4174 break; 4175 4176 case TOK_IPVER: 4177 NEED1("ipver requires version"); 4178 fill_cmd(cmd, O_IPVER, 0, strtoul(*av, NULL, 0)); 4179 ac--; av++; 4180 break; 4181 4182 case TOK_IPPRECEDENCE: 4183 NEED1("ipprecedence requires value"); 4184 fill_cmd(cmd, O_IPPRECEDENCE, 0, 4185 (strtoul(*av, NULL, 0) & 7) << 5); 4186 ac--; av++; 4187 break; 4188 4189 case TOK_IPOPTS: 4190 NEED1("missing argument for ipoptions"); 4191 fill_flags(cmd, O_IPOPT, f_ipopts, *av); 4192 ac--; av++; 4193 break; 4194 4195 case TOK_IPTOS: 4196 NEED1("missing argument for iptos"); 4197 fill_flags(cmd, O_IPTOS, f_iptos, *av); 4198 ac--; av++; 4199 break; 4200 4201 case TOK_UID: 4202 NEED1("uid requires argument"); 4203 { 4204 char *end; 4205 uid_t uid; 4206 struct passwd *pwd; 4207 4208 cmd->opcode = O_UID; 4209 uid = strtoul(*av, &end, 0); 4210 pwd = (*end == '\0') ? getpwuid(uid) : getpwnam(*av); 4211 if (pwd == NULL) 4212 errx(EX_DATAERR, "uid \"%s\" nonexistent", *av); 4213 cmd32->d[0] = pwd->pw_uid; 4214 cmd->len |= F_INSN_SIZE(ipfw_insn_u32); 4215 ac--; av++; 4216 } 4217 break; 4218 4219 case TOK_GID: 4220 NEED1("gid requires argument"); 4221 { 4222 char *end; 4223 gid_t gid; 4224 struct group *grp; 4225 4226 cmd->opcode = O_GID; 4227 gid = strtoul(*av, &end, 0); 4228 grp = (*end == '\0') ? getgrgid(gid) : getgrnam(*av); 4229 if (grp == NULL) 4230 errx(EX_DATAERR, "gid \"%s\" nonexistent", *av); 4231 cmd32->d[0] = grp->gr_gid; 4232 cmd->len |= F_INSN_SIZE(ipfw_insn_u32); 4233 ac--; av++; 4234 } 4235 break; 4236 4237 case TOK_JAIL: 4238 NEED1("jail requires argument"); 4239 { 4240 char *end; 4241 int jid; 4242 4243 cmd->opcode = O_JAIL; 4244 jid = (int)strtol(*av, &end, 0); 4245 if (jid < 0 || *end != '\0') 4246 errx(EX_DATAERR, "jail requires prison ID"); 4247 cmd32->d[0] = (uint32_t)jid; 4248 cmd->len |= F_INSN_SIZE(ipfw_insn_u32); 4249 ac--; av++; 4250 } 4251 break; 4252 4253 case TOK_ESTAB: 4254 fill_cmd(cmd, O_ESTAB, 0, 0); 4255 break; 4256 4257 case TOK_SETUP: 4258 fill_cmd(cmd, O_TCPFLAGS, 0, 4259 (TH_SYN) | ( (TH_ACK) & 0xff) <<8 ); 4260 break; 4261 4262 case TOK_TCPDATALEN: 4263 NEED1("tcpdatalen requires length"); 4264 if (strpbrk(*av, "-,")) { 4265 if (!add_ports(cmd, *av, 0, O_TCPDATALEN)) 4266 errx(EX_DATAERR, "invalid tcpdata len %s", *av); 4267 } else 4268 fill_cmd(cmd, O_TCPDATALEN, 0, 4269 strtoul(*av, NULL, 0)); 4270 ac--; av++; 4271 break; 4272 4273 case TOK_TCPOPTS: 4274 NEED1("missing argument for tcpoptions"); 4275 fill_flags(cmd, O_TCPOPTS, f_tcpopts, *av); 4276 ac--; av++; 4277 break; 4278 4279 case TOK_TCPSEQ: 4280 case TOK_TCPACK: 4281 NEED1("tcpseq/tcpack requires argument"); 4282 cmd->len = F_INSN_SIZE(ipfw_insn_u32); 4283 cmd->opcode = (i == TOK_TCPSEQ) ? O_TCPSEQ : O_TCPACK; 4284 cmd32->d[0] = htonl(strtoul(*av, NULL, 0)); 4285 ac--; av++; 4286 break; 4287 4288 case TOK_TCPWIN: 4289 NEED1("tcpwin requires length"); 4290 fill_cmd(cmd, O_TCPWIN, 0, 4291 htons(strtoul(*av, NULL, 0))); 4292 ac--; av++; 4293 break; 4294 4295 case TOK_TCPFLAGS: 4296 NEED1("missing argument for tcpflags"); 4297 cmd->opcode = O_TCPFLAGS; 4298 fill_flags(cmd, O_TCPFLAGS, f_tcpflags, *av); 4299 ac--; av++; 4300 break; 4301 4302 case TOK_KEEPSTATE: 4303 if (open_par) 4304 errx(EX_USAGE, "keep-state cannot be part " 4305 "of an or block"); 4306 if (have_state) 4307 errx(EX_USAGE, "only one of keep-state " 4308 "and limit is allowed"); 4309 have_state = cmd; 4310 fill_cmd(cmd, O_KEEP_STATE, 0, 0); 4311 break; 4312 4313 case TOK_LIMIT: 4314 if (open_par) 4315 errx(EX_USAGE, "limit cannot be part " 4316 "of an or block"); 4317 if (have_state) 4318 errx(EX_USAGE, "only one of keep-state " 4319 "and limit is allowed"); 4320 NEED1("limit needs mask and # of connections"); 4321 have_state = cmd; 4322 { 4323 ipfw_insn_limit *c = (ipfw_insn_limit *)cmd; 4324 4325 cmd->len = F_INSN_SIZE(ipfw_insn_limit); 4326 cmd->opcode = O_LIMIT; 4327 c->limit_mask = 0; 4328 c->conn_limit = 0; 4329 for (; ac >1 ;) { 4330 int val; 4331 4332 val = match_token(limit_masks, *av); 4333 if (val <= 0) 4334 break; 4335 c->limit_mask |= val; 4336 ac--; av++; 4337 } 4338 c->conn_limit = atoi(*av); 4339 if (c->conn_limit == 0) 4340 errx(EX_USAGE, "limit: limit must be >0"); 4341 if (c->limit_mask == 0) 4342 errx(EX_USAGE, "missing limit mask"); 4343 ac--; av++; 4344 } 4345 break; 4346 4347 case TOK_PROTO: 4348 NEED1("missing protocol"); 4349 if (add_proto(cmd, *av, &proto)) { 4350 if (proto == IPPROTO_IPV6) 4351 fill_cmd(cmd, O_IP6, 0, 0); 4352 ac--; av++; 4353 } else 4354 errx(EX_DATAERR, "invalid protocol ``%s''", 4355 *av); 4356 break; 4357 4358 case TOK_SRCIP: 4359 NEED1("missing source IP"); 4360 if (add_srcip(cmd, *av)) { 4361 ac--; av++; 4362 } 4363 break; 4364 4365 case TOK_DSTIP: 4366 NEED1("missing destination IP"); 4367 if (add_dstip(cmd, *av)) { 4368 ac--; av++; 4369 } 4370 break; 4371 4372 case TOK_SRCIP6: 4373 NEED1("missing source IP6"); 4374 if (add_srcip6(cmd, *av)) { 4375 ac--; av++; 4376 } 4377 break; 4378 4379 case TOK_DSTIP6: 4380 NEED1("missing destination IP6"); 4381 if (add_dstip6(cmd, *av)) { 4382 ac--; av++; 4383 } 4384 break; 4385 4386 case TOK_SRCPORT: 4387 NEED1("missing source port"); 4388 if (_substrcmp(*av, "any") == 0 || 4389 add_ports(cmd, *av, proto, O_IP_SRCPORT)) { 4390 ac--; av++; 4391 } else 4392 errx(EX_DATAERR, "invalid source port %s", *av); 4393 break; 4394 4395 case TOK_DSTPORT: 4396 NEED1("missing destination port"); 4397 if (_substrcmp(*av, "any") == 0 || 4398 add_ports(cmd, *av, proto, O_IP_DSTPORT)) { 4399 ac--; av++; 4400 } else 4401 errx(EX_DATAERR, "invalid destination port %s", 4402 *av); 4403 break; 4404 4405 case TOK_MAC: 4406 if (add_mac(cmd, ac, av)) { 4407 ac -= 2; av += 2; 4408 } 4409 break; 4410 4411 case TOK_MACTYPE: 4412 NEED1("missing mac type"); 4413 if (!add_mactype(cmd, ac, *av)) 4414 errx(EX_DATAERR, "invalid mac type %s", *av); 4415 ac--; av++; 4416 break; 4417 4418 case TOK_VERREVPATH: 4419 fill_cmd(cmd, O_VERREVPATH, 0, 0); 4420 break; 4421 4422 case TOK_VERSRCREACH: 4423 fill_cmd(cmd, O_VERSRCREACH, 0, 0); 4424 break; 4425 4426 case TOK_ANTISPOOF: 4427 fill_cmd(cmd, O_ANTISPOOF, 0, 0); 4428 break; 4429 4430 case TOK_IPSEC: 4431 fill_cmd(cmd, O_IPSEC, 0, 0); 4432 break; 4433 4434 case TOK_IPV6: 4435 fill_cmd(cmd, O_IP6, 0, 0); 4436 ac--; av++; 4437 break; 4438 4439 case TOK_EXT6HDR: 4440 fill_ext6hdr( cmd, *av ); 4441 ac--; av++; 4442 break; 4443 4444 case TOK_FLOWID: 4445 if (proto != IPPROTO_IPV6 ) 4446 errx( EX_USAGE, "flow-id filter is active " 4447 "only for ipv6 protocol\n"); 4448 fill_flow6( (ipfw_insn_u32 *) cmd, *av ); 4449 ac--; av++; 4450 break; 4451 4452 case TOK_COMMENT: 4453 fill_comment(cmd, ac, av); 4454 av += ac; 4455 ac = 0; 4456 break; 4457 4458 default: 4459 errx(EX_USAGE, "unrecognised option [%d] %s\n", i, s); 4460 } 4461 if (F_LEN(cmd) > 0) { /* prepare to advance */ 4462 prev = cmd; 4463 cmd = next_cmd(cmd); 4464 } 4465 } 4466 4467done: 4468 /* 4469 * Now copy stuff into the rule. 4470 * If we have a keep-state option, the first instruction 4471 * must be a PROBE_STATE (which is generated here). 4472 * If we have a LOG option, it was stored as the first command, 4473 * and now must be moved to the top of the action part. 4474 */ 4475 dst = (ipfw_insn *)rule->cmd; 4476 4477 /* 4478 * First thing to write into the command stream is the match probability. 4479 */ 4480 if (match_prob != 1) { /* 1 means always match */ 4481 dst->opcode = O_PROB; 4482 dst->len = 2; 4483 *((int32_t *)(dst+1)) = (int32_t)(match_prob * 0x7fffffff); 4484 dst += dst->len; 4485 } 4486 4487 /* 4488 * generate O_PROBE_STATE if necessary 4489 */ 4490 if (have_state && have_state->opcode != O_CHECK_STATE) { 4491 fill_cmd(dst, O_PROBE_STATE, 0, 0); 4492 dst = next_cmd(dst); 4493 } 4494 /* 4495 * copy all commands but O_LOG, O_KEEP_STATE, O_LIMIT, O_ALTQ 4496 */ 4497 for (src = (ipfw_insn *)cmdbuf; src != cmd; src += i) { 4498 i = F_LEN(src); 4499 4500 switch (src->opcode) { 4501 case O_LOG: 4502 case O_KEEP_STATE: 4503 case O_LIMIT: 4504 case O_ALTQ: 4505 break; 4506 default: 4507 bcopy(src, dst, i * sizeof(uint32_t)); 4508 dst += i; 4509 } 4510 } 4511 4512 /* 4513 * put back the have_state command as last opcode 4514 */ 4515 if (have_state && have_state->opcode != O_CHECK_STATE) { 4516 i = F_LEN(have_state); 4517 bcopy(have_state, dst, i * sizeof(uint32_t)); 4518 dst += i; 4519 } 4520 /* 4521 * start action section 4522 */ 4523 rule->act_ofs = dst - rule->cmd; 4524 4525 /* 4526 * put back O_LOG, O_ALTQ if necessary 4527 */ 4528 if (have_log) { 4529 i = F_LEN(have_log); 4530 bcopy(have_log, dst, i * sizeof(uint32_t)); 4531 dst += i; 4532 } 4533 if (have_altq) { 4534 i = F_LEN(have_altq); 4535 bcopy(have_altq, dst, i * sizeof(uint32_t)); 4536 dst += i; 4537 } 4538 /* 4539 * copy all other actions 4540 */ 4541 for (src = (ipfw_insn *)actbuf; src != action; src += i) { 4542 i = F_LEN(src); 4543 bcopy(src, dst, i * sizeof(uint32_t)); 4544 dst += i; 4545 } 4546 4547 rule->cmd_len = (uint32_t *)dst - (uint32_t *)(rule->cmd); 4548 i = (char *)dst - (char *)rule; 4549 if (do_cmd(IP_FW_ADD, rule, (uintptr_t)&i) == -1) 4550 err(EX_UNAVAILABLE, "getsockopt(%s)", "IP_FW_ADD"); 4551 if (!do_quiet) 4552 show_ipfw(rule, 0, 0); 4553} 4554 4555static void 4556zero(int ac, char *av[], int optname /* IP_FW_ZERO or IP_FW_RESETLOG */) 4557{ 4558 int rulenum; 4559 int failed = EX_OK; 4560 char const *name = optname == IP_FW_ZERO ? "ZERO" : "RESETLOG"; 4561 4562 av++; ac--; 4563 4564 if (!ac) { 4565 /* clear all entries */ 4566 if (do_cmd(optname, NULL, 0) < 0) 4567 err(EX_UNAVAILABLE, "setsockopt(IP_FW_%s)", name); 4568 if (!do_quiet) 4569 printf("%s.\n", optname == IP_FW_ZERO ? 4570 "Accounting cleared":"Logging counts reset"); 4571 4572 return; 4573 } 4574 4575 while (ac) { 4576 /* Rule number */ 4577 if (isdigit(**av)) { 4578 rulenum = atoi(*av); 4579 av++; 4580 ac--; 4581 if (do_cmd(optname, &rulenum, sizeof rulenum)) { 4582 warn("rule %u: setsockopt(IP_FW_%s)", 4583 rulenum, name); 4584 failed = EX_UNAVAILABLE; 4585 } else if (!do_quiet) 4586 printf("Entry %d %s.\n", rulenum, 4587 optname == IP_FW_ZERO ? 4588 "cleared" : "logging count reset"); 4589 } else { 4590 errx(EX_USAGE, "invalid rule number ``%s''", *av); 4591 } 4592 } 4593 if (failed != EX_OK) 4594 exit(failed); 4595} 4596 4597static void 4598flush(int force) 4599{ 4600 int cmd = do_pipe ? IP_DUMMYNET_FLUSH : IP_FW_FLUSH; 4601 4602 if (!force && !do_quiet) { /* need to ask user */ 4603 int c; 4604 4605 printf("Are you sure? [yn] "); 4606 fflush(stdout); 4607 do { 4608 c = toupper(getc(stdin)); 4609 while (c != '\n' && getc(stdin) != '\n') 4610 if (feof(stdin)) 4611 return; /* and do not flush */ 4612 } while (c != 'Y' && c != 'N'); 4613 printf("\n"); 4614 if (c == 'N') /* user said no */ 4615 return; 4616 } 4617 if (do_cmd(cmd, NULL, 0) < 0) 4618 err(EX_UNAVAILABLE, "setsockopt(IP_%s_FLUSH)", 4619 do_pipe ? "DUMMYNET" : "FW"); 4620 if (!do_quiet) 4621 printf("Flushed all %s.\n", do_pipe ? "pipes" : "rules"); 4622} 4623 4624/* 4625 * Free a the (locally allocated) copy of command line arguments. 4626 */ 4627static void 4628free_args(int ac, char **av) 4629{ 4630 int i; 4631 4632 for (i=0; i < ac; i++) 4633 free(av[i]); 4634 free(av); 4635} 4636 4637/* 4638 * This one handles all table-related commands 4639 * ipfw table N add addr[/masklen] [value] 4640 * ipfw table N delete addr[/masklen] 4641 * ipfw table N flush 4642 * ipfw table N list 4643 */ 4644static void 4645table_handler(int ac, char *av[]) 4646{ 4647 ipfw_table_entry ent; 4648 ipfw_table *tbl; 4649 int do_add; 4650 char *p; 4651 socklen_t l; 4652 uint32_t a; 4653 4654 ac--; av++; 4655 if (ac && isdigit(**av)) { 4656 ent.tbl = atoi(*av); 4657 ac--; av++; 4658 } else 4659 errx(EX_USAGE, "table number required"); 4660 NEED1("table needs command"); 4661 if (_substrcmp(*av, "add") == 0 || 4662 _substrcmp(*av, "delete") == 0) { 4663 do_add = **av == 'a'; 4664 ac--; av++; 4665 if (!ac) 4666 errx(EX_USAGE, "IP address required"); 4667 p = strchr(*av, '/'); 4668 if (p) { 4669 *p++ = '\0'; 4670 ent.masklen = atoi(p); 4671 if (ent.masklen > 32) 4672 errx(EX_DATAERR, "bad width ``%s''", p); 4673 } else 4674 ent.masklen = 32; 4675 if (lookup_host(*av, (struct in_addr *)&ent.addr) != 0) 4676 errx(EX_NOHOST, "hostname ``%s'' unknown", *av); 4677 ac--; av++; 4678 if (do_add && ac) 4679 ent.value = strtoul(*av, NULL, 0); 4680 else 4681 ent.value = 0; 4682 if (do_cmd(do_add ? IP_FW_TABLE_ADD : IP_FW_TABLE_DEL, 4683 &ent, sizeof(ent)) < 0) 4684 err(EX_OSERR, "setsockopt(IP_FW_TABLE_%s)", 4685 do_add ? "ADD" : "DEL"); 4686 } else if (_substrcmp(*av, "flush") == 0) { 4687 if (do_cmd(IP_FW_TABLE_FLUSH, &ent.tbl, sizeof(ent.tbl)) < 0) 4688 err(EX_OSERR, "setsockopt(IP_FW_TABLE_FLUSH)"); 4689 } else if (_substrcmp(*av, "list") == 0) { 4690 a = ent.tbl; 4691 l = sizeof(a); 4692 if (do_cmd(IP_FW_TABLE_GETSIZE, &a, (uintptr_t)&l) < 0) 4693 err(EX_OSERR, "getsockopt(IP_FW_TABLE_GETSIZE)"); 4694 l = sizeof(*tbl) + a * sizeof(ipfw_table_entry); 4695 tbl = malloc(l); 4696 if (tbl == NULL) 4697 err(EX_OSERR, "malloc"); 4698 tbl->tbl = ent.tbl; 4699 if (do_cmd(IP_FW_TABLE_LIST, tbl, (uintptr_t)&l) < 0) 4700 err(EX_OSERR, "getsockopt(IP_FW_TABLE_LIST)"); 4701 for (a = 0; a < tbl->cnt; a++) { 4702 printf("%s/%u %u\n", 4703 inet_ntoa(*(struct in_addr *)&tbl->ent[a].addr), 4704 tbl->ent[a].masklen, tbl->ent[a].value); 4705 } 4706 } else 4707 errx(EX_USAGE, "invalid table command %s", *av); 4708} 4709 4710/* 4711 * Called with the arguments (excluding program name). 4712 * Returns 0 if successful, 1 if empty command, errx() in case of errors. 4713 */ 4714static int 4715ipfw_main(int oldac, char **oldav) 4716{ 4717 int ch, ac, save_ac; 4718 char **av, **save_av; 4719 int do_acct = 0; /* Show packet/byte count */ 4720 4721#define WHITESP " \t\f\v\n\r" 4722 if (oldac == 0) 4723 return 1; 4724 else if (oldac == 1) { 4725 /* 4726 * If we are called with a single string, try to split it into 4727 * arguments for subsequent parsing. 4728 * But first, remove spaces after a ',', by copying the string 4729 * in-place. 4730 */ 4731 char *arg = oldav[0]; /* The string... */ 4732 int l = strlen(arg); 4733 int copy = 0; /* 1 if we need to copy, 0 otherwise */ 4734 int i, j; 4735 for (i = j = 0; i < l; i++) { 4736 if (arg[i] == '#') /* comment marker */ 4737 break; 4738 if (copy) { 4739 arg[j++] = arg[i]; 4740 copy = !index("," WHITESP, arg[i]); 4741 } else { 4742 copy = !index(WHITESP, arg[i]); 4743 if (copy) 4744 arg[j++] = arg[i]; 4745 } 4746 } 4747 if (!copy && j > 0) /* last char was a 'blank', remove it */ 4748 j--; 4749 l = j; /* the new argument length */ 4750 arg[j++] = '\0'; 4751 if (l == 0) /* empty string! */ 4752 return 1; 4753 4754 /* 4755 * First, count number of arguments. Because of the previous 4756 * processing, this is just the number of blanks plus 1. 4757 */ 4758 for (i = 0, ac = 1; i < l; i++) 4759 if (index(WHITESP, arg[i]) != NULL) 4760 ac++; 4761 4762 av = calloc(ac, sizeof(char *)); 4763 4764 /* 4765 * Second, copy arguments from cmd[] to av[]. For each one, 4766 * j is the initial character, i is the one past the end. 4767 */ 4768 for (ac = 0, i = j = 0; i < l; i++) 4769 if (index(WHITESP, arg[i]) != NULL || i == l-1) { 4770 if (i == l-1) 4771 i++; 4772 av[ac] = calloc(i-j+1, 1); 4773 bcopy(arg+j, av[ac], i-j); 4774 ac++; 4775 j = i + 1; 4776 } 4777 } else { 4778 /* 4779 * If an argument ends with ',' join with the next one. 4780 */ 4781 int first, i, l; 4782 4783 av = calloc(oldac, sizeof(char *)); 4784 for (first = i = ac = 0, l = 0; i < oldac; i++) { 4785 char *arg = oldav[i]; 4786 int k = strlen(arg); 4787 4788 l += k; 4789 if (arg[k-1] != ',' || i == oldac-1) { 4790 /* Time to copy. */ 4791 av[ac] = calloc(l+1, 1); 4792 for (l=0; first <= i; first++) { 4793 strcat(av[ac]+l, oldav[first]); 4794 l += strlen(oldav[first]); 4795 } 4796 ac++; 4797 l = 0; 4798 first = i+1; 4799 } 4800 } 4801 } 4802 4803 /* Set the force flag for non-interactive processes */ 4804 if (!do_force) 4805 do_force = !isatty(STDIN_FILENO); 4806 4807 /* Save arguments for final freeing of memory. */ 4808 save_ac = ac; 4809 save_av = av; 4810 4811 optind = optreset = 0; 4812 while ((ch = getopt(ac, av, "abcdefhnNqs:STtv")) != -1) 4813 switch (ch) { 4814 case 'a': 4815 do_acct = 1; 4816 break; 4817 4818 case 'b': 4819 comment_only = 1; 4820 do_compact = 1; 4821 break; 4822 4823 case 'c': 4824 do_compact = 1; 4825 break; 4826 4827 case 'd': 4828 do_dynamic = 1; 4829 break; 4830 4831 case 'e': 4832 do_expired = 1; 4833 break; 4834 4835 case 'f': 4836 do_force = 1; 4837 break; 4838 4839 case 'h': /* help */ 4840 free_args(save_ac, save_av); 4841 help(); 4842 break; /* NOTREACHED */ 4843 4844 case 'n': 4845 test_only = 1; 4846 break; 4847 4848 case 'N': 4849 do_resolv = 1; 4850 break; 4851 4852 case 'q': 4853 do_quiet = 1; 4854 break; 4855 4856 case 's': /* sort */ 4857 do_sort = atoi(optarg); 4858 break; 4859 4860 case 'S': 4861 show_sets = 1; 4862 break; 4863 4864 case 't': 4865 do_time = 1; 4866 break; 4867 4868 case 'T': 4869 do_time = 2; /* numeric timestamp */ 4870 break; 4871 4872 case 'v': /* verbose */ 4873 verbose = 1; 4874 break; 4875 4876 default: 4877 free_args(save_ac, save_av); 4878 return 1; 4879 } 4880 4881 ac -= optind; 4882 av += optind; 4883 NEED1("bad arguments, for usage summary ``ipfw''"); 4884 4885 /* 4886 * An undocumented behaviour of ipfw1 was to allow rule numbers first, 4887 * e.g. "100 add allow ..." instead of "add 100 allow ...". 4888 * In case, swap first and second argument to get the normal form. 4889 */ 4890 if (ac > 1 && isdigit(*av[0])) { 4891 char *p = av[0]; 4892 4893 av[0] = av[1]; 4894 av[1] = p; 4895 } 4896 4897 /* 4898 * optional: pipe or queue 4899 */ 4900 do_pipe = 0; 4901 if (_substrcmp(*av, "pipe") == 0) 4902 do_pipe = 1; 4903 else if (_substrcmp(*av, "queue") == 0) 4904 do_pipe = 2; 4905 if (do_pipe) { 4906 ac--; 4907 av++; 4908 } 4909 NEED1("missing command"); 4910 4911 /* 4912 * For pipes and queues we normally say 'pipe NN config' 4913 * but the code is easier to parse as 'pipe config NN' 4914 * so we swap the two arguments. 4915 */ 4916 if (do_pipe > 0 && ac > 1 && isdigit(*av[0])) { 4917 char *p = av[0]; 4918 4919 av[0] = av[1]; 4920 av[1] = p; 4921 } 4922 4923 if (_substrcmp(*av, "add") == 0) 4924 add(ac, av); 4925 else if (do_pipe && _substrcmp(*av, "config") == 0) 4926 config_pipe(ac, av); 4927 else if (_substrcmp(*av, "delete") == 0) 4928 delete(ac, av); 4929 else if (_substrcmp(*av, "flush") == 0) 4930 flush(do_force); 4931 else if (_substrcmp(*av, "zero") == 0) 4932 zero(ac, av, IP_FW_ZERO); 4933 else if (_substrcmp(*av, "resetlog") == 0) 4934 zero(ac, av, IP_FW_RESETLOG); 4935 else if (_substrcmp(*av, "print") == 0 || 4936 _substrcmp(*av, "list") == 0) 4937 list(ac, av, do_acct); 4938 else if (_substrcmp(*av, "set") == 0) 4939 sets_handler(ac, av); 4940 else if (_substrcmp(*av, "table") == 0) 4941 table_handler(ac, av); 4942 else if (_substrcmp(*av, "enable") == 0) 4943 sysctl_handler(ac, av, 1); 4944 else if (_substrcmp(*av, "disable") == 0) 4945 sysctl_handler(ac, av, 0); 4946 else if (_substrcmp(*av, "show") == 0) 4947 list(ac, av, 1 /* show counters */); 4948 else 4949 errx(EX_USAGE, "bad command `%s'", *av); 4950 4951 /* Free memory allocated in the argument parsing. */ 4952 free_args(save_ac, save_av); 4953 return 0; 4954} 4955 4956 4957static void 4958ipfw_readfile(int ac, char *av[]) 4959{ 4960#define MAX_ARGS 32 4961 char buf[BUFSIZ]; 4962 char *cmd = NULL, *filename = av[ac-1]; 4963 int c, lineno=0; 4964 FILE *f = NULL; 4965 pid_t preproc = 0; 4966 4967 filename = av[ac-1]; 4968 4969 while ((c = getopt(ac, av, "cfNnp:qS")) != -1) { 4970 switch(c) { 4971 case 'c': 4972 do_compact = 1; 4973 break; 4974 4975 case 'f': 4976 do_force = 1; 4977 break; 4978 4979 case 'N': 4980 do_resolv = 1; 4981 break; 4982 4983 case 'n': 4984 test_only = 1; 4985 break; 4986 4987 case 'p': 4988 cmd = optarg; 4989 /* 4990 * Skip previous args and delete last one, so we 4991 * pass all but the last argument to the preprocessor 4992 * via av[optind-1] 4993 */ 4994 av += optind - 1; 4995 ac -= optind - 1; 4996 av[ac-1] = NULL; 4997 fprintf(stderr, "command is %s\n", av[0]); 4998 break; 4999 5000 case 'q': 5001 do_quiet = 1; 5002 break; 5003 5004 case 'S': 5005 show_sets = 1; 5006 break; 5007 5008 default: 5009 errx(EX_USAGE, "bad arguments, for usage" 5010 " summary ``ipfw''"); 5011 } 5012 5013 if (cmd != NULL) 5014 break; 5015 } 5016 5017 if (cmd == NULL && ac != optind + 1) { 5018 fprintf(stderr, "ac %d, optind %d\n", ac, optind); 5019 errx(EX_USAGE, "extraneous filename arguments"); 5020 } 5021 5022 if ((f = fopen(filename, "r")) == NULL) 5023 err(EX_UNAVAILABLE, "fopen: %s", filename); 5024 5025 if (cmd != NULL) { /* pipe through preprocessor */ 5026 int pipedes[2]; 5027 5028 if (pipe(pipedes) == -1) 5029 err(EX_OSERR, "cannot create pipe"); 5030 5031 preproc = fork(); 5032 if (preproc == -1) 5033 err(EX_OSERR, "cannot fork"); 5034 5035 if (preproc == 0) { 5036 /* 5037 * Child, will run the preprocessor with the 5038 * file on stdin and the pipe on stdout. 5039 */ 5040 if (dup2(fileno(f), 0) == -1 5041 || dup2(pipedes[1], 1) == -1) 5042 err(EX_OSERR, "dup2()"); 5043 fclose(f); 5044 close(pipedes[1]); 5045 close(pipedes[0]); 5046 execvp(cmd, av); 5047 err(EX_OSERR, "execvp(%s) failed", cmd); 5048 } else { /* parent, will reopen f as the pipe */ 5049 fclose(f); 5050 close(pipedes[1]); 5051 if ((f = fdopen(pipedes[0], "r")) == NULL) { 5052 int savederrno = errno; 5053 5054 (void)kill(preproc, SIGTERM); 5055 errno = savederrno; 5056 err(EX_OSERR, "fdopen()"); 5057 } 5058 } 5059 } 5060 5061 while (fgets(buf, BUFSIZ, f)) { /* read commands */ 5062 char linename[10]; 5063 char *args[1]; 5064 5065 lineno++; 5066 sprintf(linename, "Line %d", lineno); 5067 setprogname(linename); /* XXX */ 5068 args[0] = buf; 5069 ipfw_main(1, args); 5070 } 5071 fclose(f); 5072 if (cmd != NULL) { 5073 int status; 5074 5075 if (waitpid(preproc, &status, 0) == -1) 5076 errx(EX_OSERR, "waitpid()"); 5077 if (WIFEXITED(status) && WEXITSTATUS(status) != EX_OK) 5078 errx(EX_UNAVAILABLE, 5079 "preprocessor exited with status %d", 5080 WEXITSTATUS(status)); 5081 else if (WIFSIGNALED(status)) 5082 errx(EX_UNAVAILABLE, 5083 "preprocessor exited with signal %d", 5084 WTERMSIG(status)); 5085 } 5086} 5087 5088int 5089main(int ac, char *av[]) 5090{ 5091 /* 5092 * If the last argument is an absolute pathname, interpret it 5093 * as a file to be preprocessed. 5094 */ 5095 5096 if (ac > 1 && av[ac - 1][0] == '/' && access(av[ac - 1], R_OK) == 0) 5097 ipfw_readfile(ac, av); 5098 else { 5099 if (ipfw_main(ac-1, av+1)) 5100 show_usage(); 5101 } 5102 return EX_OK; 5103} 5104