ipfw2.c revision 287963
1238384Sjkim/* 2238384Sjkim * Copyright (c) 2002-2003 Luigi Rizzo 3238384Sjkim * Copyright (c) 1996 Alex Nash, Paul Traina, Poul-Henning Kamp 4238384Sjkim * Copyright (c) 1994 Ugen J.S.Antsilevich 5238384Sjkim * 6238384Sjkim * Idea and grammar partially left from: 7238384Sjkim * Copyright (c) 1993 Daniel Boulet 8238384Sjkim * 9238384Sjkim * Redistribution and use in source forms, with and without modification, 10238384Sjkim * are permitted provided that this entire comment appears intact. 11238384Sjkim * 12238384Sjkim * Redistribution in binary form may occur without any restrictions. 13238384Sjkim * Obviously, it would be nice if you gave credit where credit is due 14238384Sjkim * but requiring it would be too onerous. 15238384Sjkim * 16238384Sjkim * This software is provided ``AS IS'' without any warranties of any kind. 17238384Sjkim * 18238384Sjkim * NEW command line interface for IP firewall facility 19238384Sjkim * 20238384Sjkim * $FreeBSD: stable/10/sbin/ipfw/ipfw2.c 287963 2015-09-18 17:29:24Z melifaro $ 21238384Sjkim */ 22238384Sjkim 23238384Sjkim#include <sys/types.h> 24238384Sjkim#include <sys/param.h> 25238384Sjkim#include <sys/socket.h> 26238384Sjkim#include <sys/sockio.h> 27238384Sjkim#include <sys/sysctl.h> 28238384Sjkim 29238384Sjkim#include "ipfw2.h" 30238384Sjkim 31238384Sjkim#include <ctype.h> 32238384Sjkim#include <err.h> 33238384Sjkim#include <errno.h> 34238384Sjkim#include <grp.h> 35238384Sjkim#include <netdb.h> 36238384Sjkim#include <pwd.h> 37238384Sjkim#include <stdio.h> 38238384Sjkim#include <stdlib.h> 39238384Sjkim#include <string.h> 40238384Sjkim#include <sysexits.h> 41238384Sjkim#include <time.h> /* ctime */ 42238384Sjkim#include <timeconv.h> /* _long_to_time */ 43238384Sjkim#include <unistd.h> 44238384Sjkim#include <fcntl.h> 45238384Sjkim#include <stddef.h> /* offsetof */ 46238384Sjkim 47238384Sjkim#include <net/ethernet.h> 48238384Sjkim#include <net/if.h> /* only IFNAMSIZ */ 49238384Sjkim#include <netinet/in.h> 50238384Sjkim#include <netinet/in_systm.h> /* only n_short, n_long */ 51238384Sjkim#include <netinet/ip.h> 52238384Sjkim#include <netinet/ip_icmp.h> 53238384Sjkim#include <netinet/ip_fw.h> 54238384Sjkim#include <netinet/tcp.h> 55238384Sjkim#include <arpa/inet.h> 56238384Sjkim 57238384Sjkimstruct cmdline_opts co; /* global options */ 58238384Sjkim 59238384Sjkimint resvd_set_number = RESVD_SET; 60238384Sjkim 61238384Sjkimint ipfw_socket = -1; 62238384Sjkim 63238384Sjkimuint32_t ipfw_tables_max = 0; /* Number of tables supported by kernel */ 64238384Sjkim 65238384Sjkim#ifndef s6_addr32 66238384Sjkim#define s6_addr32 __u6_addr.__u6_addr32 67238384Sjkim#endif 68238384Sjkim 69238384Sjkim#define CHECK_LENGTH(v, len) do { \ 70238384Sjkim if ((v) < (len)) \ 71246772Sjkim errx(EX_DATAERR, "Rule too long"); \ 72246772Sjkim } while (0) 73238384Sjkim/* 74238384Sjkim * Check if we have enough space in cmd buffer. Note that since 75238384Sjkim * first 8? u32 words are reserved by reserved header, full cmd 76238384Sjkim * buffer can't be used, so we need to protect from buffer overrun 77238384Sjkim * only. At the beginnig, cblen is less than actual buffer size by 78238384Sjkim * size of ipfw_insn_u32 instruction + 1 u32 work. This eliminates need 79238384Sjkim * for checking small instructions fitting in given range. 80238384Sjkim * We also (ab)use the fact that ipfw_insn is always the first field 81238384Sjkim * for any custom instruction. 82238384Sjkim */ 83238384Sjkim#define CHECK_CMDLEN CHECK_LENGTH(cblen, F_LEN((ipfw_insn *)cmd)) 84238384Sjkim 85238384Sjkim#define GET_UINT_ARG(arg, min, max, tok, s_x) do { \ 86238384Sjkim if (!av[0]) \ 87238384Sjkim errx(EX_USAGE, "%s: missing argument", match_value(s_x, tok)); \ 88238384Sjkim if (_substrcmp(*av, "tablearg") == 0) { \ 89238384Sjkim arg = IP_FW_TABLEARG; \ 90238384Sjkim break; \ 91238384Sjkim } \ 92238384Sjkim \ 93238384Sjkim { \ 94238384Sjkim long _xval; \ 95238384Sjkim char *end; \ 96238384Sjkim \ 97238384Sjkim _xval = strtol(*av, &end, 10); \ 98238384Sjkim \ 99238384Sjkim if (!isdigit(**av) || *end != '\0' || (_xval == 0 && errno == EINVAL)) \ 100238384Sjkim errx(EX_DATAERR, "%s: invalid argument: %s", \ 101238384Sjkim match_value(s_x, tok), *av); \ 102238384Sjkim \ 103238384Sjkim if (errno == ERANGE || _xval < min || _xval > max) \ 104238384Sjkim errx(EX_DATAERR, "%s: argument is out of range (%u..%u): %s", \ 105238384Sjkim match_value(s_x, tok), min, max, *av); \ 106238384Sjkim \ 107238384Sjkim if (_xval == IP_FW_TABLEARG) \ 108238384Sjkim errx(EX_DATAERR, "%s: illegal argument value: %s", \ 109238384Sjkim match_value(s_x, tok), *av); \ 110238384Sjkim arg = _xval; \ 111238384Sjkim } \ 112238384Sjkim} while (0) 113238384Sjkim 114238384Sjkimstatic void 115238384SjkimPRINT_UINT_ARG(const char *str, uint32_t arg) 116238384Sjkim{ 117238384Sjkim if (str != NULL) 118238384Sjkim printf("%s",str); 119238384Sjkim if (arg == IP_FW_TABLEARG) 120238384Sjkim printf("tablearg"); 121238384Sjkim else 122238384Sjkim printf("%u", arg); 123238384Sjkim} 124238384Sjkim 125238384Sjkimstatic struct _s_x f_tcpflags[] = { 126238384Sjkim { "syn", TH_SYN }, 127238384Sjkim { "fin", TH_FIN }, 128238384Sjkim { "ack", TH_ACK }, 129238384Sjkim { "psh", TH_PUSH }, 130238384Sjkim { "rst", TH_RST }, 131238384Sjkim { "urg", TH_URG }, 132238384Sjkim { "tcp flag", 0 }, 133238384Sjkim { NULL, 0 } 134238384Sjkim}; 135238384Sjkim 136238384Sjkimstatic struct _s_x f_tcpopts[] = { 137238384Sjkim { "mss", IP_FW_TCPOPT_MSS }, 138238384Sjkim { "maxseg", IP_FW_TCPOPT_MSS }, 139238384Sjkim { "window", IP_FW_TCPOPT_WINDOW }, 140238384Sjkim { "sack", IP_FW_TCPOPT_SACK }, 141238384Sjkim { "ts", IP_FW_TCPOPT_TS }, 142238384Sjkim { "timestamp", IP_FW_TCPOPT_TS }, 143238384Sjkim { "cc", IP_FW_TCPOPT_CC }, 144238384Sjkim { "tcp option", 0 }, 145238384Sjkim { NULL, 0 } 146238384Sjkim}; 147238384Sjkim 148238384Sjkim/* 149238384Sjkim * IP options span the range 0 to 255 so we need to remap them 150238384Sjkim * (though in fact only the low 5 bits are significant). 151238384Sjkim */ 152238384Sjkimstatic struct _s_x f_ipopts[] = { 153238384Sjkim { "ssrr", IP_FW_IPOPT_SSRR}, 154238384Sjkim { "lsrr", IP_FW_IPOPT_LSRR}, 155238384Sjkim { "rr", IP_FW_IPOPT_RR}, 156238384Sjkim { "ts", IP_FW_IPOPT_TS}, 157238384Sjkim { "ip option", 0 }, 158238384Sjkim { NULL, 0 } 159238384Sjkim}; 160238384Sjkim 161238384Sjkimstatic struct _s_x f_iptos[] = { 162238384Sjkim { "lowdelay", IPTOS_LOWDELAY}, 163238384Sjkim { "throughput", IPTOS_THROUGHPUT}, 164238384Sjkim { "reliability", IPTOS_RELIABILITY}, 165238384Sjkim { "mincost", IPTOS_MINCOST}, 166238384Sjkim { "congestion", IPTOS_ECN_CE}, 167238384Sjkim { "ecntransport", IPTOS_ECN_ECT0}, 168238384Sjkim { "ip tos option", 0}, 169238384Sjkim { NULL, 0 } 170238384Sjkim}; 171238384Sjkim 172238384Sjkimstatic struct _s_x f_ipdscp[] = { 173238384Sjkim { "af11", IPTOS_DSCP_AF11 >> 2 }, /* 001010 */ 174238384Sjkim { "af12", IPTOS_DSCP_AF12 >> 2 }, /* 001100 */ 175238384Sjkim { "af13", IPTOS_DSCP_AF13 >> 2 }, /* 001110 */ 176238384Sjkim { "af21", IPTOS_DSCP_AF21 >> 2 }, /* 010010 */ 177238384Sjkim { "af22", IPTOS_DSCP_AF22 >> 2 }, /* 010100 */ 178238384Sjkim { "af23", IPTOS_DSCP_AF23 >> 2 }, /* 010110 */ 179238384Sjkim { "af31", IPTOS_DSCP_AF31 >> 2 }, /* 011010 */ 180238384Sjkim { "af32", IPTOS_DSCP_AF32 >> 2 }, /* 011100 */ 181238384Sjkim { "af33", IPTOS_DSCP_AF33 >> 2 }, /* 011110 */ 182238384Sjkim { "af41", IPTOS_DSCP_AF41 >> 2 }, /* 100010 */ 183238384Sjkim { "af42", IPTOS_DSCP_AF42 >> 2 }, /* 100100 */ 184238384Sjkim { "af43", IPTOS_DSCP_AF43 >> 2 }, /* 100110 */ 185238384Sjkim { "be", IPTOS_DSCP_CS0 >> 2 }, /* 000000 */ 186238384Sjkim { "ef", IPTOS_DSCP_EF >> 2 }, /* 101110 */ 187238384Sjkim { "cs0", IPTOS_DSCP_CS0 >> 2 }, /* 000000 */ 188238384Sjkim { "cs1", IPTOS_DSCP_CS1 >> 2 }, /* 001000 */ 189238384Sjkim { "cs2", IPTOS_DSCP_CS2 >> 2 }, /* 010000 */ 190238384Sjkim { "cs3", IPTOS_DSCP_CS3 >> 2 }, /* 011000 */ 191238384Sjkim { "cs4", IPTOS_DSCP_CS4 >> 2 }, /* 100000 */ 192238384Sjkim { "cs5", IPTOS_DSCP_CS5 >> 2 }, /* 101000 */ 193238384Sjkim { "cs6", IPTOS_DSCP_CS6 >> 2 }, /* 110000 */ 194238384Sjkim { "cs7", IPTOS_DSCP_CS7 >> 2 }, /* 100000 */ 195238384Sjkim { NULL, 0 } 196238384Sjkim}; 197238384Sjkim 198238384Sjkimstatic struct _s_x limit_masks[] = { 199238384Sjkim {"all", DYN_SRC_ADDR|DYN_SRC_PORT|DYN_DST_ADDR|DYN_DST_PORT}, 200238384Sjkim {"src-addr", DYN_SRC_ADDR}, 201238384Sjkim {"src-port", DYN_SRC_PORT}, 202238384Sjkim {"dst-addr", DYN_DST_ADDR}, 203238384Sjkim {"dst-port", DYN_DST_PORT}, 204238384Sjkim {NULL, 0} 205238384Sjkim}; 206238384Sjkim 207238384Sjkim/* 208238384Sjkim * we use IPPROTO_ETHERTYPE as a fake protocol id to call the print routines 209238384Sjkim * This is only used in this code. 210238384Sjkim */ 211238384Sjkim#define IPPROTO_ETHERTYPE 0x1000 212238384Sjkimstatic struct _s_x ether_types[] = { 213238384Sjkim /* 214238384Sjkim * Note, we cannot use "-:&/" in the names because they are field 215238384Sjkim * separators in the type specifications. Also, we use s = NULL as 216238384Sjkim * end-delimiter, because a type of 0 can be legal. 217238384Sjkim */ 218238384Sjkim { "ip", 0x0800 }, 219238384Sjkim { "ipv4", 0x0800 }, 220238384Sjkim { "ipv6", 0x86dd }, 221238384Sjkim { "arp", 0x0806 }, 222238384Sjkim { "rarp", 0x8035 }, 223238384Sjkim { "vlan", 0x8100 }, 224238384Sjkim { "loop", 0x9000 }, 225238384Sjkim { "trail", 0x1000 }, 226238384Sjkim { "at", 0x809b }, 227238384Sjkim { "atalk", 0x809b }, 228238384Sjkim { "aarp", 0x80f3 }, 229238384Sjkim { "pppoe_disc", 0x8863 }, 230238384Sjkim { "pppoe_sess", 0x8864 }, 231238384Sjkim { "ipx_8022", 0x00E0 }, 232238384Sjkim { "ipx_8023", 0x0000 }, 233238384Sjkim { "ipx_ii", 0x8137 }, 234238384Sjkim { "ipx_snap", 0x8137 }, 235238384Sjkim { "ipx", 0x8137 }, 236238384Sjkim { "ns", 0x0600 }, 237238384Sjkim { NULL, 0 } 238238384Sjkim}; 239238384Sjkim 240238384Sjkim 241238384Sjkimstatic struct _s_x rule_actions[] = { 242238384Sjkim { "accept", TOK_ACCEPT }, 243238384Sjkim { "pass", TOK_ACCEPT }, 244238384Sjkim { "allow", TOK_ACCEPT }, 245238384Sjkim { "permit", TOK_ACCEPT }, 246238384Sjkim { "count", TOK_COUNT }, 247238384Sjkim { "pipe", TOK_PIPE }, 248238384Sjkim { "queue", TOK_QUEUE }, 249238384Sjkim { "divert", TOK_DIVERT }, 250238384Sjkim { "tee", TOK_TEE }, 251238384Sjkim { "netgraph", TOK_NETGRAPH }, 252238384Sjkim { "ngtee", TOK_NGTEE }, 253238384Sjkim { "fwd", TOK_FORWARD }, 254238384Sjkim { "forward", TOK_FORWARD }, 255238384Sjkim { "skipto", TOK_SKIPTO }, 256238384Sjkim { "deny", TOK_DENY }, 257238384Sjkim { "drop", TOK_DENY }, 258238384Sjkim { "reject", TOK_REJECT }, 259238384Sjkim { "reset6", TOK_RESET6 }, 260238384Sjkim { "reset", TOK_RESET }, 261238384Sjkim { "unreach6", TOK_UNREACH6 }, 262238384Sjkim { "unreach", TOK_UNREACH }, 263238384Sjkim { "check-state", TOK_CHECKSTATE }, 264238384Sjkim { "//", TOK_COMMENT }, 265238384Sjkim { "nat", TOK_NAT }, 266238384Sjkim { "reass", TOK_REASS }, 267238384Sjkim { "setfib", TOK_SETFIB }, 268238384Sjkim { "setdscp", TOK_SETDSCP }, 269238384Sjkim { "call", TOK_CALL }, 270238384Sjkim { "return", TOK_RETURN }, 271238384Sjkim { NULL, 0 } /* terminator */ 272238384Sjkim}; 273238384Sjkim 274238384Sjkimstatic struct _s_x rule_action_params[] = { 275238384Sjkim { "altq", TOK_ALTQ }, 276238384Sjkim { "log", TOK_LOG }, 277238384Sjkim { "tag", TOK_TAG }, 278238384Sjkim { "untag", TOK_UNTAG }, 279238384Sjkim { NULL, 0 } /* terminator */ 280238384Sjkim}; 281238384Sjkim 282238384Sjkim/* 283238384Sjkim * The 'lookup' instruction accepts one of the following arguments. 284238384Sjkim * -1 is a terminator for the list. 285238384Sjkim * Arguments are passed as v[1] in O_DST_LOOKUP options. 286238384Sjkim */ 287238384Sjkimstatic int lookup_key[] = { 288238384Sjkim TOK_DSTIP, TOK_SRCIP, TOK_DSTPORT, TOK_SRCPORT, 289238384Sjkim TOK_UID, TOK_JAIL, TOK_DSCP, -1 }; 290238384Sjkim 291238384Sjkimstatic struct _s_x rule_options[] = { 292238384Sjkim { "tagged", TOK_TAGGED }, 293238384Sjkim { "uid", TOK_UID }, 294238384Sjkim { "gid", TOK_GID }, 295238384Sjkim { "jail", TOK_JAIL }, 296238384Sjkim { "in", TOK_IN }, 297238384Sjkim { "limit", TOK_LIMIT }, 298238384Sjkim { "keep-state", TOK_KEEPSTATE }, 299238384Sjkim { "bridged", TOK_LAYER2 }, 300238384Sjkim { "layer2", TOK_LAYER2 }, 301238384Sjkim { "out", TOK_OUT }, 302238384Sjkim { "diverted", TOK_DIVERTED }, 303238384Sjkim { "diverted-loopback", TOK_DIVERTEDLOOPBACK }, 304238384Sjkim { "diverted-output", TOK_DIVERTEDOUTPUT }, 305238384Sjkim { "xmit", TOK_XMIT }, 306238384Sjkim { "recv", TOK_RECV }, 307238384Sjkim { "via", TOK_VIA }, 308238384Sjkim { "fragment", TOK_FRAG }, 309238384Sjkim { "frag", TOK_FRAG }, 310238384Sjkim { "fib", TOK_FIB }, 311238384Sjkim { "ipoptions", TOK_IPOPTS }, 312238384Sjkim { "ipopts", TOK_IPOPTS }, 313238384Sjkim { "iplen", TOK_IPLEN }, 314238384Sjkim { "ipid", TOK_IPID }, 315238384Sjkim { "ipprecedence", TOK_IPPRECEDENCE }, 316238384Sjkim { "dscp", TOK_DSCP }, 317238384Sjkim { "iptos", TOK_IPTOS }, 318238384Sjkim { "ipttl", TOK_IPTTL }, 319238384Sjkim { "ipversion", TOK_IPVER }, 320238384Sjkim { "ipver", TOK_IPVER }, 321238384Sjkim { "estab", TOK_ESTAB }, 322238384Sjkim { "established", TOK_ESTAB }, 323238384Sjkim { "setup", TOK_SETUP }, 324238384Sjkim { "sockarg", TOK_SOCKARG }, 325238384Sjkim { "tcpdatalen", TOK_TCPDATALEN }, 326238384Sjkim { "tcpflags", TOK_TCPFLAGS }, 327238384Sjkim { "tcpflgs", TOK_TCPFLAGS }, 328238384Sjkim { "tcpoptions", TOK_TCPOPTS }, 329238384Sjkim { "tcpopts", TOK_TCPOPTS }, 330238384Sjkim { "tcpseq", TOK_TCPSEQ }, 331238384Sjkim { "tcpack", TOK_TCPACK }, 332238384Sjkim { "tcpwin", TOK_TCPWIN }, 333238384Sjkim { "icmptype", TOK_ICMPTYPES }, 334238384Sjkim { "icmptypes", TOK_ICMPTYPES }, 335238384Sjkim { "dst-ip", TOK_DSTIP }, 336238384Sjkim { "src-ip", TOK_SRCIP }, 337238384Sjkim { "dst-port", TOK_DSTPORT }, 338238384Sjkim { "src-port", TOK_SRCPORT }, 339238384Sjkim { "proto", TOK_PROTO }, 340238384Sjkim { "MAC", TOK_MAC }, 341238384Sjkim { "mac", TOK_MAC }, 342238384Sjkim { "mac-type", TOK_MACTYPE }, 343238384Sjkim { "verrevpath", TOK_VERREVPATH }, 344238384Sjkim { "versrcreach", TOK_VERSRCREACH }, 345238384Sjkim { "antispoof", TOK_ANTISPOOF }, 346238384Sjkim { "ipsec", TOK_IPSEC }, 347238384Sjkim { "icmp6type", TOK_ICMP6TYPES }, 348238384Sjkim { "icmp6types", TOK_ICMP6TYPES }, 349238384Sjkim { "ext6hdr", TOK_EXT6HDR}, 350238384Sjkim { "flow-id", TOK_FLOWID}, 351238384Sjkim { "ipv6", TOK_IPV6}, 352238384Sjkim { "ip6", TOK_IPV6}, 353238384Sjkim { "ipv4", TOK_IPV4}, 354238384Sjkim { "ip4", TOK_IPV4}, 355238384Sjkim { "dst-ipv6", TOK_DSTIP6}, 356238384Sjkim { "dst-ip6", TOK_DSTIP6}, 357238384Sjkim { "src-ipv6", TOK_SRCIP6}, 358238384Sjkim { "src-ip6", TOK_SRCIP6}, 359238384Sjkim { "lookup", TOK_LOOKUP}, 360238384Sjkim { "//", TOK_COMMENT }, 361238384Sjkim 362238384Sjkim { "not", TOK_NOT }, /* pseudo option */ 363238384Sjkim { "!", /* escape ? */ TOK_NOT }, /* pseudo option */ 364238384Sjkim { "or", TOK_OR }, /* pseudo option */ 365238384Sjkim { "|", /* escape */ TOK_OR }, /* pseudo option */ 366238384Sjkim { "{", TOK_STARTBRACE }, /* pseudo option */ 367238384Sjkim { "(", TOK_STARTBRACE }, /* pseudo option */ 368238384Sjkim { "}", TOK_ENDBRACE }, /* pseudo option */ 369238384Sjkim { ")", TOK_ENDBRACE }, /* pseudo option */ 370238384Sjkim { NULL, 0 } /* terminator */ 371238384Sjkim}; 372238384Sjkim 373238384Sjkim/* 374238384Sjkim * Helper routine to print a possibly unaligned uint64_t on 375238384Sjkim * various platform. If width > 0, print the value with 376238384Sjkim * the desired width, followed by a space; 377238384Sjkim * otherwise, return the required width. 378238384Sjkim */ 379238384Sjkimint 380238384Sjkimpr_u64(uint64_t *pd, int width) 381238384Sjkim{ 382238384Sjkim#ifdef TCC 383238384Sjkim#define U64_FMT "I64" 384238384Sjkim#else 385238384Sjkim#define U64_FMT "llu" 386238384Sjkim#endif 387238384Sjkim uint64_t u; 388238384Sjkim unsigned long long d; 389238384Sjkim 390238384Sjkim bcopy (pd, &u, sizeof(u)); 391238384Sjkim d = u; 392238384Sjkim return (width > 0) ? 393238384Sjkim printf("%*" U64_FMT " ", width, d) : 394238384Sjkim snprintf(NULL, 0, "%" U64_FMT, d) ; 395238384Sjkim#undef U64_FMT 396238384Sjkim} 397238384Sjkim 398238384Sjkimvoid * 399238384Sjkimsafe_calloc(size_t number, size_t size) 400238384Sjkim{ 401238384Sjkim void *ret = calloc(number, size); 402238384Sjkim 403238384Sjkim if (ret == NULL) 404238384Sjkim err(EX_OSERR, "calloc"); 405238384Sjkim return ret; 406238384Sjkim} 407238384Sjkim 408238384Sjkimvoid * 409238384Sjkimsafe_realloc(void *ptr, size_t size) 410238384Sjkim{ 411238384Sjkim void *ret = realloc(ptr, size); 412238384Sjkim 413238384Sjkim if (ret == NULL) 414238384Sjkim err(EX_OSERR, "realloc"); 415238384Sjkim return ret; 416238384Sjkim} 417238384Sjkim 418238384Sjkim/* 419238384Sjkim * conditionally runs the command. 420238384Sjkim * Selected options or negative -> getsockopt 421238384Sjkim */ 422238384Sjkimint 423238384Sjkimdo_cmd(int optname, void *optval, uintptr_t optlen) 424238384Sjkim{ 425238384Sjkim int i; 426238384Sjkim 427238384Sjkim if (co.test_only) 428238384Sjkim return 0; 429238384Sjkim 430238384Sjkim if (ipfw_socket == -1) 431238384Sjkim ipfw_socket = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); 432238384Sjkim if (ipfw_socket < 0) 433238384Sjkim err(EX_UNAVAILABLE, "socket"); 434238384Sjkim 435238384Sjkim if (optname == IP_FW_GET || optname == IP_DUMMYNET_GET || 436238384Sjkim optname == IP_FW_ADD || optname == IP_FW3 || 437238384Sjkim optname == IP_FW_NAT_GET_CONFIG || 438238384Sjkim optname < 0 || 439238384Sjkim optname == IP_FW_NAT_GET_LOG) { 440238384Sjkim if (optname < 0) 441238384Sjkim optname = -optname; 442238384Sjkim i = getsockopt(ipfw_socket, IPPROTO_IP, optname, optval, 443238384Sjkim (socklen_t *)optlen); 444238384Sjkim } else { 445238384Sjkim i = setsockopt(ipfw_socket, IPPROTO_IP, optname, optval, optlen); 446238384Sjkim } 447238384Sjkim return i; 448238384Sjkim} 449238384Sjkim 450238384Sjkim/* 451238384Sjkim * do_setcmd3 - pass ipfw control cmd to kernel 452238384Sjkim * @optname: option name 453238384Sjkim * @optval: pointer to option data 454238384Sjkim * @optlen: option length 455238384Sjkim * 456238384Sjkim * Function encapsulates option value in IP_FW3 socket option 457238384Sjkim * and calls setsockopt(). 458238384Sjkim * Function returns 0 on success or -1 otherwise. 459238384Sjkim */ 460238384Sjkimstatic int 461238384Sjkimdo_setcmd3(int optname, void *optval, socklen_t optlen) 462238384Sjkim{ 463238384Sjkim socklen_t len; 464238384Sjkim ip_fw3_opheader *op3; 465238384Sjkim 466238384Sjkim if (co.test_only) 467238384Sjkim return (0); 468238384Sjkim 469238384Sjkim if (ipfw_socket == -1) 470238384Sjkim ipfw_socket = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); 471238384Sjkim if (ipfw_socket < 0) 472238384Sjkim err(EX_UNAVAILABLE, "socket"); 473238384Sjkim 474238384Sjkim len = sizeof(ip_fw3_opheader) + optlen; 475238384Sjkim op3 = alloca(len); 476238384Sjkim /* Zero reserved fields */ 477238384Sjkim memset(op3, 0, sizeof(ip_fw3_opheader)); 478238384Sjkim memcpy(op3 + 1, optval, optlen); 479238384Sjkim op3->opcode = optname; 480238384Sjkim 481238384Sjkim return setsockopt(ipfw_socket, IPPROTO_IP, IP_FW3, op3, len); 482238384Sjkim} 483238384Sjkim 484238384Sjkim/** 485238384Sjkim * match_token takes a table and a string, returns the value associated 486238384Sjkim * with the string (-1 in case of failure). 487238384Sjkim */ 488238384Sjkimint 489238384Sjkimmatch_token(struct _s_x *table, char *string) 490238384Sjkim{ 491238384Sjkim struct _s_x *pt; 492238384Sjkim uint i = strlen(string); 493238384Sjkim 494238384Sjkim for (pt = table ; i && pt->s != NULL ; pt++) 495238384Sjkim if (strlen(pt->s) == i && !bcmp(string, pt->s, i)) 496238384Sjkim return pt->x; 497238384Sjkim return -1; 498238384Sjkim} 499238384Sjkim 500238384Sjkim/** 501238384Sjkim * match_value takes a table and a value, returns the string associated 502238384Sjkim * with the value (NULL in case of failure). 503238384Sjkim */ 504238384Sjkimchar const * 505238384Sjkimmatch_value(struct _s_x *p, int value) 506238384Sjkim{ 507238384Sjkim for (; p->s != NULL; p++) 508238384Sjkim if (p->x == value) 509238384Sjkim return p->s; 510238384Sjkim return NULL; 511238384Sjkim} 512238384Sjkim 513238384Sjkim/* 514238384Sjkim * _substrcmp takes two strings and returns 1 if they do not match, 515238384Sjkim * and 0 if they match exactly or the first string is a sub-string 516238384Sjkim * of the second. A warning is printed to stderr in the case that the 517238384Sjkim * first string is a sub-string of the second. 518238384Sjkim * 519238384Sjkim * This function will be removed in the future through the usual 520238384Sjkim * deprecation process. 521238384Sjkim */ 522238384Sjkimint 523238384Sjkim_substrcmp(const char *str1, const char* str2) 524238384Sjkim{ 525238384Sjkim 526238384Sjkim if (strncmp(str1, str2, strlen(str1)) != 0) 527238384Sjkim return 1; 528238384Sjkim 529238384Sjkim if (strlen(str1) != strlen(str2)) 530238384Sjkim warnx("DEPRECATED: '%s' matched '%s' as a sub-string", 531238384Sjkim str1, str2); 532238384Sjkim return 0; 533238384Sjkim} 534238384Sjkim 535238384Sjkim/* 536238384Sjkim * _substrcmp2 takes three strings and returns 1 if the first two do not match, 537238384Sjkim * and 0 if they match exactly or the second string is a sub-string 538238384Sjkim * of the first. A warning is printed to stderr in the case that the 539238384Sjkim * first string does not match the third. 540238384Sjkim * 541238384Sjkim * This function exists to warn about the bizarre construction 542238384Sjkim * strncmp(str, "by", 2) which is used to allow people to use a shortcut 543238384Sjkim * for "bytes". The problem is that in addition to accepting "by", 544238384Sjkim * "byt", "byte", and "bytes", it also excepts "by_rabid_dogs" and any 545238384Sjkim * other string beginning with "by". 546238384Sjkim * 547238384Sjkim * This function will be removed in the future through the usual 548238384Sjkim * deprecation process. 549238384Sjkim */ 550238384Sjkimint 551238384Sjkim_substrcmp2(const char *str1, const char* str2, const char* str3) 552238384Sjkim{ 553238384Sjkim 554238384Sjkim if (strncmp(str1, str2, strlen(str2)) != 0) 555238384Sjkim return 1; 556238384Sjkim 557238384Sjkim if (strcmp(str1, str3) != 0) 558238384Sjkim warnx("DEPRECATED: '%s' matched '%s'", 559238384Sjkim str1, str3); 560238384Sjkim return 0; 561238384Sjkim} 562238384Sjkim 563238384Sjkim/* 564238384Sjkim * prints one port, symbolic or numeric 565238384Sjkim */ 566238384Sjkimstatic void 567238384Sjkimprint_port(int proto, uint16_t port) 568238384Sjkim{ 569238384Sjkim 570238384Sjkim if (proto == IPPROTO_ETHERTYPE) { 571238384Sjkim char const *s; 572238384Sjkim 573238384Sjkim if (co.do_resolv && (s = match_value(ether_types, port)) ) 574238384Sjkim printf("%s", s); 575238384Sjkim else 576238384Sjkim printf("0x%04x", port); 577238384Sjkim } else { 578238384Sjkim struct servent *se = NULL; 579238384Sjkim if (co.do_resolv) { 580238384Sjkim struct protoent *pe = getprotobynumber(proto); 581238384Sjkim 582238384Sjkim se = getservbyport(htons(port), pe ? pe->p_name : NULL); 583238384Sjkim } 584238384Sjkim if (se) 585238384Sjkim printf("%s", se->s_name); 586238384Sjkim else 587238384Sjkim printf("%d", port); 588238384Sjkim } 589238384Sjkim} 590238384Sjkim 591238384Sjkimstatic struct _s_x _port_name[] = { 592238384Sjkim {"dst-port", O_IP_DSTPORT}, 593238384Sjkim {"src-port", O_IP_SRCPORT}, 594238384Sjkim {"ipid", O_IPID}, 595238384Sjkim {"iplen", O_IPLEN}, 596238384Sjkim {"ipttl", O_IPTTL}, 597238384Sjkim {"mac-type", O_MAC_TYPE}, 598238384Sjkim {"tcpdatalen", O_TCPDATALEN}, 599238384Sjkim {"tcpwin", O_TCPWIN}, 600238384Sjkim {"tagged", O_TAGGED}, 601238384Sjkim {NULL, 0} 602238384Sjkim}; 603238384Sjkim 604238384Sjkim/* 605238384Sjkim * Print the values in a list 16-bit items of the types above. 606238384Sjkim * XXX todo: add support for mask. 607238384Sjkim */ 608238384Sjkimstatic void 609238384Sjkimprint_newports(ipfw_insn_u16 *cmd, int proto, int opcode) 610238384Sjkim{ 611238384Sjkim uint16_t *p = cmd->ports; 612238384Sjkim int i; 613238384Sjkim char const *sep; 614238384Sjkim 615238384Sjkim if (opcode != 0) { 616238384Sjkim sep = match_value(_port_name, opcode); 617238384Sjkim if (sep == NULL) 618238384Sjkim sep = "???"; 619238384Sjkim printf (" %s", sep); 620238384Sjkim } 621238384Sjkim sep = " "; 622238384Sjkim for (i = F_LEN((ipfw_insn *)cmd) - 1; i > 0; i--, p += 2) { 623238384Sjkim printf("%s", sep); 624238384Sjkim print_port(proto, p[0]); 625238384Sjkim if (p[0] != p[1]) { 626238384Sjkim printf("-"); 627238384Sjkim print_port(proto, p[1]); 628238384Sjkim } 629238384Sjkim sep = ","; 630238384Sjkim } 631238384Sjkim} 632238384Sjkim 633238384Sjkim/* 634238384Sjkim * Like strtol, but also translates service names into port numbers 635238384Sjkim * for some protocols. 636238384Sjkim * In particular: 637238384Sjkim * proto == -1 disables the protocol check; 638238384Sjkim * proto == IPPROTO_ETHERTYPE looks up an internal table 639238384Sjkim * proto == <some value in /etc/protocols> matches the values there. 640238384Sjkim * Returns *end == s in case the parameter is not found. 641238384Sjkim */ 642238384Sjkimstatic int 643238384Sjkimstrtoport(char *s, char **end, int base, int proto) 644238384Sjkim{ 645238384Sjkim char *p, *buf; 646238384Sjkim char *s1; 647238384Sjkim int i; 648238384Sjkim 649238384Sjkim *end = s; /* default - not found */ 650238384Sjkim if (*s == '\0') 651238384Sjkim return 0; /* not found */ 652238384Sjkim 653238384Sjkim if (isdigit(*s)) 654238384Sjkim return strtol(s, end, base); 655238384Sjkim 656238384Sjkim /* 657238384Sjkim * find separator. '\\' escapes the next char. 658238384Sjkim */ 659238384Sjkim for (s1 = s; *s1 && (isalnum(*s1) || *s1 == '\\') ; s1++) 660238384Sjkim if (*s1 == '\\' && s1[1] != '\0') 661238384Sjkim s1++; 662238384Sjkim 663238384Sjkim buf = safe_calloc(s1 - s + 1, 1); 664238384Sjkim 665238384Sjkim /* 666238384Sjkim * copy into a buffer skipping backslashes 667238384Sjkim */ 668238384Sjkim for (p = s, i = 0; p != s1 ; p++) 669238384Sjkim if (*p != '\\') 670238384Sjkim buf[i++] = *p; 671238384Sjkim buf[i++] = '\0'; 672238384Sjkim 673238384Sjkim if (proto == IPPROTO_ETHERTYPE) { 674238384Sjkim i = match_token(ether_types, buf); 675238384Sjkim free(buf); 676238384Sjkim if (i != -1) { /* found */ 677238384Sjkim *end = s1; 678238384Sjkim return i; 679238384Sjkim } 680238384Sjkim } else { 681238384Sjkim struct protoent *pe = NULL; 682238384Sjkim struct servent *se; 683238384Sjkim 684238384Sjkim if (proto != 0) 685238384Sjkim pe = getprotobynumber(proto); 686238384Sjkim setservent(1); 687238384Sjkim se = getservbyname(buf, pe ? pe->p_name : NULL); 688238384Sjkim free(buf); 689238384Sjkim if (se != NULL) { 690238384Sjkim *end = s1; 691238384Sjkim return ntohs(se->s_port); 692238384Sjkim } 693238384Sjkim } 694238384Sjkim return 0; /* not found */ 695238384Sjkim} 696238384Sjkim 697238384Sjkim/* 698238384Sjkim * Fill the body of the command with the list of port ranges. 699238384Sjkim */ 700238384Sjkimstatic int 701238384Sjkimfill_newports(ipfw_insn_u16 *cmd, char *av, int proto, int cblen) 702238384Sjkim{ 703238384Sjkim uint16_t a, b, *p = cmd->ports; 704238384Sjkim int i = 0; 705238384Sjkim char *s = av; 706238384Sjkim 707238384Sjkim while (*s) { 708238384Sjkim a = strtoport(av, &s, 0, proto); 709238384Sjkim if (s == av) /* empty or invalid argument */ 710238384Sjkim return (0); 711238384Sjkim 712238384Sjkim CHECK_LENGTH(cblen, i + 2); 713238384Sjkim 714238384Sjkim switch (*s) { 715238384Sjkim case '-': /* a range */ 716238384Sjkim av = s + 1; 717238384Sjkim b = strtoport(av, &s, 0, proto); 718238384Sjkim /* Reject expressions like '1-abc' or '1-2-3'. */ 719238384Sjkim if (s == av || (*s != ',' && *s != '\0')) 720238384Sjkim return (0); 721238384Sjkim p[0] = a; 722238384Sjkim p[1] = b; 723238384Sjkim break; 724238384Sjkim case ',': /* comma separated list */ 725238384Sjkim case '\0': 726238384Sjkim p[0] = p[1] = a; 727238384Sjkim break; 728238384Sjkim default: 729238384Sjkim warnx("port list: invalid separator <%c> in <%s>", 730238384Sjkim *s, av); 731238384Sjkim return (0); 732238384Sjkim } 733238384Sjkim 734238384Sjkim i++; 735238384Sjkim p += 2; 736238384Sjkim av = s + 1; 737238384Sjkim } 738238384Sjkim if (i > 0) { 739238384Sjkim if (i + 1 > F_LEN_MASK) 740238384Sjkim errx(EX_DATAERR, "too many ports/ranges\n"); 741238384Sjkim cmd->o.len |= i + 1; /* leave F_NOT and F_OR untouched */ 742238384Sjkim } 743238384Sjkim return (i); 744238384Sjkim} 745238384Sjkim 746238384Sjkim/* 747238384Sjkim * Fill the body of the command with the list of DiffServ codepoints. 748238384Sjkim */ 749238384Sjkimstatic void 750238384Sjkimfill_dscp(ipfw_insn *cmd, char *av, int cblen) 751238384Sjkim{ 752238384Sjkim uint32_t *low, *high; 753238384Sjkim char *s = av, *a; 754238384Sjkim int code; 755238384Sjkim 756238384Sjkim cmd->opcode = O_DSCP; 757238384Sjkim cmd->len |= F_INSN_SIZE(ipfw_insn_u32) + 1; 758238384Sjkim 759238384Sjkim CHECK_CMDLEN; 760238384Sjkim 761238384Sjkim low = (uint32_t *)(cmd + 1); 762238384Sjkim high = low + 1; 763238384Sjkim 764238384Sjkim *low = 0; 765238384Sjkim *high = 0; 766238384Sjkim 767238384Sjkim while (s != NULL) { 768238384Sjkim a = strchr(s, ','); 769238384Sjkim 770238384Sjkim if (a != NULL) 771238384Sjkim *a++ = '\0'; 772238384Sjkim 773238384Sjkim if (isalpha(*s)) { 774238384Sjkim if ((code = match_token(f_ipdscp, s)) == -1) 775238384Sjkim errx(EX_DATAERR, "Unknown DSCP code"); 776238384Sjkim } else { 777238384Sjkim code = strtoul(s, NULL, 10); 778238384Sjkim if (code < 0 || code > 63) 779238384Sjkim errx(EX_DATAERR, "Invalid DSCP value"); 780238384Sjkim } 781238384Sjkim 782238384Sjkim if (code > 32) 783238384Sjkim *high |= 1 << (code - 32); 784238384Sjkim else 785238384Sjkim *low |= 1 << code; 786238384Sjkim 787238384Sjkim s = a; 788238384Sjkim } 789238384Sjkim} 790238384Sjkim 791238384Sjkimstatic struct _s_x icmpcodes[] = { 792238384Sjkim { "net", ICMP_UNREACH_NET }, 793238384Sjkim { "host", ICMP_UNREACH_HOST }, 794238384Sjkim { "protocol", ICMP_UNREACH_PROTOCOL }, 795238384Sjkim { "port", ICMP_UNREACH_PORT }, 796238384Sjkim { "needfrag", ICMP_UNREACH_NEEDFRAG }, 797238384Sjkim { "srcfail", ICMP_UNREACH_SRCFAIL }, 798238384Sjkim { "net-unknown", ICMP_UNREACH_NET_UNKNOWN }, 799238384Sjkim { "host-unknown", ICMP_UNREACH_HOST_UNKNOWN }, 800238384Sjkim { "isolated", ICMP_UNREACH_ISOLATED }, 801238384Sjkim { "net-prohib", ICMP_UNREACH_NET_PROHIB }, 802238384Sjkim { "host-prohib", ICMP_UNREACH_HOST_PROHIB }, 803238384Sjkim { "tosnet", ICMP_UNREACH_TOSNET }, 804238384Sjkim { "toshost", ICMP_UNREACH_TOSHOST }, 805238384Sjkim { "filter-prohib", ICMP_UNREACH_FILTER_PROHIB }, 806238384Sjkim { "host-precedence", ICMP_UNREACH_HOST_PRECEDENCE }, 807238384Sjkim { "precedence-cutoff", ICMP_UNREACH_PRECEDENCE_CUTOFF }, 808238384Sjkim { NULL, 0 } 809238384Sjkim}; 810238384Sjkim 811238384Sjkimstatic void 812238384Sjkimfill_reject_code(u_short *codep, char *str) 813238384Sjkim{ 814238384Sjkim int val; 815238384Sjkim char *s; 816238384Sjkim 817238384Sjkim val = strtoul(str, &s, 0); 818238384Sjkim if (s == str || *s != '\0' || val >= 0x100) 819238384Sjkim val = match_token(icmpcodes, str); 820238384Sjkim if (val < 0) 821238384Sjkim errx(EX_DATAERR, "unknown ICMP unreachable code ``%s''", str); 822238384Sjkim *codep = val; 823238384Sjkim return; 824238384Sjkim} 825238384Sjkim 826238384Sjkimstatic void 827238384Sjkimprint_reject_code(uint16_t code) 828238384Sjkim{ 829238384Sjkim char const *s = match_value(icmpcodes, code); 830238384Sjkim 831238384Sjkim if (s != NULL) 832238384Sjkim printf("unreach %s", s); 833238384Sjkim else 834238384Sjkim printf("unreach %u", code); 835238384Sjkim} 836238384Sjkim 837238384Sjkim/* 838238384Sjkim * Returns the number of bits set (from left) in a contiguous bitmask, 839238384Sjkim * or -1 if the mask is not contiguous. 840238384Sjkim * XXX this needs a proper fix. 841238384Sjkim * This effectively works on masks in big-endian (network) format. 842238384Sjkim * when compiled on little endian architectures. 843238384Sjkim * 844238384Sjkim * First bit is bit 7 of the first byte -- note, for MAC addresses, 845238384Sjkim * the first bit on the wire is bit 0 of the first byte. 846238384Sjkim * len is the max length in bits. 847238384Sjkim */ 848238384Sjkimint 849238384Sjkimcontigmask(uint8_t *p, int len) 850238384Sjkim{ 851238384Sjkim int i, n; 852238384Sjkim 853238384Sjkim for (i=0; i<len ; i++) 854238384Sjkim if ( (p[i/8] & (1 << (7 - (i%8)))) == 0) /* first bit unset */ 855238384Sjkim break; 856238384Sjkim for (n=i+1; n < len; n++) 857238384Sjkim if ( (p[n/8] & (1 << (7 - (n%8)))) != 0) 858238384Sjkim return -1; /* mask not contiguous */ 859238384Sjkim return i; 860238384Sjkim} 861238384Sjkim 862238384Sjkim/* 863238384Sjkim * print flags set/clear in the two bitmasks passed as parameters. 864238384Sjkim * There is a specialized check for f_tcpflags. 865238384Sjkim */ 866238384Sjkimstatic void 867238384Sjkimprint_flags(char const *name, ipfw_insn *cmd, struct _s_x *list) 868238384Sjkim{ 869238384Sjkim char const *comma = ""; 870238384Sjkim int i; 871238384Sjkim uint8_t set = cmd->arg1 & 0xff; 872238384Sjkim uint8_t clear = (cmd->arg1 >> 8) & 0xff; 873238384Sjkim 874238384Sjkim if (list == f_tcpflags && set == TH_SYN && clear == TH_ACK) { 875238384Sjkim printf(" setup"); 876238384Sjkim return; 877238384Sjkim } 878238384Sjkim 879238384Sjkim printf(" %s ", name); 880238384Sjkim for (i=0; list[i].x != 0; i++) { 881238384Sjkim if (set & list[i].x) { 882238384Sjkim set &= ~list[i].x; 883238384Sjkim printf("%s%s", comma, list[i].s); 884238384Sjkim comma = ","; 885238384Sjkim } 886238384Sjkim if (clear & list[i].x) { 887238384Sjkim clear &= ~list[i].x; 888238384Sjkim printf("%s!%s", comma, list[i].s); 889238384Sjkim comma = ","; 890238384Sjkim } 891238384Sjkim } 892238384Sjkim} 893238384Sjkim 894238384Sjkim/* 895238384Sjkim * Print the ip address contained in a command. 896238384Sjkim */ 897238384Sjkimstatic void 898238384Sjkimprint_ip(ipfw_insn_ip *cmd, char const *s) 899238384Sjkim{ 900238384Sjkim struct hostent *he = NULL; 901238384Sjkim uint32_t len = F_LEN((ipfw_insn *)cmd); 902238384Sjkim uint32_t *a = ((ipfw_insn_u32 *)cmd)->d; 903238384Sjkim 904238384Sjkim if (cmd->o.opcode == O_IP_DST_LOOKUP && len > F_INSN_SIZE(ipfw_insn_u32)) { 905238384Sjkim uint32_t d = a[1]; 906238384Sjkim const char *arg = "<invalid>"; 907238384Sjkim 908238384Sjkim if (d < sizeof(lookup_key)/sizeof(lookup_key[0])) 909238384Sjkim arg = match_value(rule_options, lookup_key[d]); 910238384Sjkim printf("%s lookup %s %d", cmd->o.len & F_NOT ? " not": "", 911238384Sjkim arg, cmd->o.arg1); 912238384Sjkim return; 913238384Sjkim } 914238384Sjkim printf("%s%s ", cmd->o.len & F_NOT ? " not": "", s); 915238384Sjkim 916238384Sjkim if (cmd->o.opcode == O_IP_SRC_ME || cmd->o.opcode == O_IP_DST_ME) { 917238384Sjkim printf("me"); 918238384Sjkim return; 919238384Sjkim } 920238384Sjkim if (cmd->o.opcode == O_IP_SRC_LOOKUP || 921238384Sjkim cmd->o.opcode == O_IP_DST_LOOKUP) { 922238384Sjkim printf("table(%u", ((ipfw_insn *)cmd)->arg1); 923238384Sjkim if (len == F_INSN_SIZE(ipfw_insn_u32)) 924238384Sjkim printf(",%u", *a); 925238384Sjkim printf(")"); 926238384Sjkim return; 927238384Sjkim } 928238384Sjkim if (cmd->o.opcode == O_IP_SRC_SET || cmd->o.opcode == O_IP_DST_SET) { 929238384Sjkim uint32_t x, *map = (uint32_t *)&(cmd->mask); 930238384Sjkim int i, j; 931238384Sjkim char comma = '{'; 932238384Sjkim 933238384Sjkim x = cmd->o.arg1 - 1; 934238384Sjkim x = htonl( ~x ); 935238384Sjkim cmd->addr.s_addr = htonl(cmd->addr.s_addr); 936238384Sjkim printf("%s/%d", inet_ntoa(cmd->addr), 937238384Sjkim contigmask((uint8_t *)&x, 32)); 938238384Sjkim x = cmd->addr.s_addr = htonl(cmd->addr.s_addr); 939238384Sjkim x &= 0xff; /* base */ 940238384Sjkim /* 941238384Sjkim * Print bits and ranges. 942238384Sjkim * Locate first bit set (i), then locate first bit unset (j). 943238384Sjkim * If we have 3+ consecutive bits set, then print them as a 944238384Sjkim * range, otherwise only print the initial bit and rescan. 945238384Sjkim */ 946238384Sjkim for (i=0; i < cmd->o.arg1; i++) 947238384Sjkim if (map[i/32] & (1<<(i & 31))) { 948238384Sjkim for (j=i+1; j < cmd->o.arg1; j++) 949238384Sjkim if (!(map[ j/32] & (1<<(j & 31)))) 950238384Sjkim break; 951238384Sjkim printf("%c%d", comma, i+x); 952238384Sjkim if (j>i+2) { /* range has at least 3 elements */ 953238384Sjkim printf("-%d", j-1+x); 954238384Sjkim i = j-1; 955238384Sjkim } 956238384Sjkim comma = ','; 957238384Sjkim } 958238384Sjkim printf("}"); 959238384Sjkim return; 960238384Sjkim } 961238384Sjkim /* 962238384Sjkim * len == 2 indicates a single IP, whereas lists of 1 or more 963238384Sjkim * addr/mask pairs have len = (2n+1). We convert len to n so we 964238384Sjkim * use that to count the number of entries. 965238384Sjkim */ 966238384Sjkim for (len = len / 2; len > 0; len--, a += 2) { 967238384Sjkim int mb = /* mask length */ 968238384Sjkim (cmd->o.opcode == O_IP_SRC || cmd->o.opcode == O_IP_DST) ? 969238384Sjkim 32 : contigmask((uint8_t *)&(a[1]), 32); 970238384Sjkim if (mb == 32 && co.do_resolv) 971238384Sjkim he = gethostbyaddr((char *)&(a[0]), sizeof(u_long), AF_INET); 972238384Sjkim if (he != NULL) /* resolved to name */ 973238384Sjkim printf("%s", he->h_name); 974238384Sjkim else if (mb == 0) /* any */ 975238384Sjkim printf("any"); 976238384Sjkim else { /* numeric IP followed by some kind of mask */ 977238384Sjkim printf("%s", inet_ntoa( *((struct in_addr *)&a[0]) ) ); 978238384Sjkim if (mb < 0) 979238384Sjkim printf(":%s", inet_ntoa( *((struct in_addr *)&a[1]) ) ); 980238384Sjkim else if (mb < 32) 981238384Sjkim printf("/%d", mb); 982238384Sjkim } 983238384Sjkim if (len > 1) 984238384Sjkim printf(","); 985238384Sjkim } 986238384Sjkim} 987238384Sjkim 988238384Sjkim/* 989238384Sjkim * prints a MAC address/mask pair 990238384Sjkim */ 991238384Sjkimstatic void 992238384Sjkimprint_mac(uint8_t *addr, uint8_t *mask) 993238384Sjkim{ 994238384Sjkim int l = contigmask(mask, 48); 995238384Sjkim 996238384Sjkim if (l == 0) 997238384Sjkim printf(" any"); 998238384Sjkim else { 999238384Sjkim printf(" %02x:%02x:%02x:%02x:%02x:%02x", 1000238384Sjkim addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); 1001238384Sjkim if (l == -1) 1002238384Sjkim printf("&%02x:%02x:%02x:%02x:%02x:%02x", 1003238384Sjkim mask[0], mask[1], mask[2], 1004238384Sjkim mask[3], mask[4], mask[5]); 1005238384Sjkim else if (l < 48) 1006238384Sjkim printf("/%d", l); 1007238384Sjkim } 1008238384Sjkim} 1009238384Sjkim 1010238384Sjkimstatic void 1011238384Sjkimfill_icmptypes(ipfw_insn_u32 *cmd, char *av) 1012238384Sjkim{ 1013238384Sjkim uint8_t type; 1014238384Sjkim 1015238384Sjkim cmd->d[0] = 0; 1016238384Sjkim while (*av) { 1017238384Sjkim if (*av == ',') 1018238384Sjkim av++; 1019238384Sjkim 1020238384Sjkim type = strtoul(av, &av, 0); 1021238384Sjkim 1022238384Sjkim if (*av != ',' && *av != '\0') 1023238384Sjkim errx(EX_DATAERR, "invalid ICMP type"); 1024238384Sjkim 1025238384Sjkim if (type > 31) 1026238384Sjkim errx(EX_DATAERR, "ICMP type out of range"); 1027238384Sjkim 1028238384Sjkim cmd->d[0] |= 1 << type; 1029238384Sjkim } 1030238384Sjkim cmd->o.opcode = O_ICMPTYPE; 1031238384Sjkim cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32); 1032238384Sjkim} 1033238384Sjkim 1034238384Sjkimstatic void 1035238384Sjkimprint_icmptypes(ipfw_insn_u32 *cmd) 1036238384Sjkim{ 1037238384Sjkim int i; 1038238384Sjkim char sep= ' '; 1039238384Sjkim 1040238384Sjkim printf(" icmptypes"); 1041238384Sjkim for (i = 0; i < 32; i++) { 1042238384Sjkim if ( (cmd->d[0] & (1 << (i))) == 0) 1043238384Sjkim continue; 1044238384Sjkim printf("%c%d", sep, i); 1045238384Sjkim sep = ','; 1046238384Sjkim } 1047238384Sjkim} 1048238384Sjkim 1049238384Sjkimstatic void 1050238384Sjkimprint_dscp(ipfw_insn_u32 *cmd) 1051238384Sjkim{ 1052238384Sjkim int i, c; 1053238384Sjkim uint32_t *v; 1054238384Sjkim char sep= ' '; 1055238384Sjkim const char *code; 1056238384Sjkim 1057238384Sjkim printf(" dscp"); 1058238384Sjkim i = 0; 1059238384Sjkim c = 0; 1060238384Sjkim v = cmd->d; 1061238384Sjkim while (i < 64) { 1062238384Sjkim if (*v & (1 << i)) { 1063238384Sjkim if ((code = match_value(f_ipdscp, i)) != NULL) 1064238384Sjkim printf("%c%s", sep, code); 1065238384Sjkim else 1066238384Sjkim printf("%c%d", sep, i); 1067238384Sjkim sep = ','; 1068238384Sjkim } 1069238384Sjkim 1070238384Sjkim if ((++i % 32) == 0) 1071238384Sjkim v++; 1072238384Sjkim } 1073238384Sjkim} 1074238384Sjkim 1075238384Sjkim/* 1076238384Sjkim * show_ipfw() prints the body of an ipfw rule. 1077238384Sjkim * Because the standard rule has at least proto src_ip dst_ip, we use 1078238384Sjkim * a helper function to produce these entries if not provided explicitly. 1079238384Sjkim * The first argument is the list of fields we have, the second is 1080238384Sjkim * the list of fields we want to be printed. 1081238384Sjkim * 1082238384Sjkim * Special cases if we have provided a MAC header: 1083238384Sjkim * + if the rule does not contain IP addresses/ports, do not print them; 1084238384Sjkim * + if the rule does not contain an IP proto, print "all" instead of "ip"; 1085238384Sjkim * 1086238384Sjkim * Once we have 'have_options', IP header fields are printed as options. 1087238384Sjkim */ 1088238384Sjkim#define HAVE_PROTO 0x0001 1089238384Sjkim#define HAVE_SRCIP 0x0002 1090238384Sjkim#define HAVE_DSTIP 0x0004 1091238384Sjkim#define HAVE_PROTO4 0x0008 1092238384Sjkim#define HAVE_PROTO6 0x0010 1093238384Sjkim#define HAVE_IP 0x0100 1094238384Sjkim#define HAVE_OPTIONS 0x8000 1095238384Sjkim 1096238384Sjkimstatic void 1097238384Sjkimshow_prerequisites(int *flags, int want, int cmd) 1098238384Sjkim{ 1099238384Sjkim (void)cmd; /* UNUSED */ 1100238384Sjkim if (co.comment_only) 1101238384Sjkim return; 1102238384Sjkim if ( (*flags & HAVE_IP) == HAVE_IP) 1103238384Sjkim *flags |= HAVE_OPTIONS; 1104238384Sjkim 1105238384Sjkim if ( !(*flags & HAVE_OPTIONS)) { 1106238384Sjkim if ( !(*flags & HAVE_PROTO) && (want & HAVE_PROTO)) { 1107238384Sjkim if ( (*flags & HAVE_PROTO4)) 1108238384Sjkim printf(" ip4"); 1109238384Sjkim else if ( (*flags & HAVE_PROTO6)) 1110238384Sjkim printf(" ip6"); 1111238384Sjkim else 1112238384Sjkim printf(" ip"); 1113238384Sjkim } 1114238384Sjkim if ( !(*flags & HAVE_SRCIP) && (want & HAVE_SRCIP)) 1115238384Sjkim printf(" from any"); 1116238384Sjkim if ( !(*flags & HAVE_DSTIP) && (want & HAVE_DSTIP)) 1117238384Sjkim printf(" to any"); 1118238384Sjkim } 1119238384Sjkim *flags |= want; 1120238384Sjkim} 1121238384Sjkim 1122238384Sjkimstatic void 1123238384Sjkimshow_ipfw(struct ip_fw *rule, int pcwidth, int bcwidth) 1124238384Sjkim{ 1125238384Sjkim static int twidth = 0; 1126238384Sjkim int l; 1127238384Sjkim ipfw_insn *cmd, *tagptr = NULL; 1128238384Sjkim const char *comment = NULL; /* ptr to comment if we have one */ 1129238384Sjkim int proto = 0; /* default */ 1130238384Sjkim int flags = 0; /* prerequisites */ 1131238384Sjkim ipfw_insn_log *logptr = NULL; /* set if we find an O_LOG */ 1132238384Sjkim ipfw_insn_altq *altqptr = NULL; /* set if we find an O_ALTQ */ 1133238384Sjkim int or_block = 0; /* we are in an or block */ 1134238384Sjkim uint32_t set_disable; 1135238384Sjkim 1136238384Sjkim bcopy(&rule->next_rule, &set_disable, sizeof(set_disable)); 1137238384Sjkim 1138238384Sjkim if (set_disable & (1 << rule->set)) { /* disabled */ 1139238384Sjkim if (!co.show_sets) 1140238384Sjkim return; 1141238384Sjkim else 1142238384Sjkim printf("# DISABLED "); 1143238384Sjkim } 1144238384Sjkim printf("%05u ", rule->rulenum); 1145238384Sjkim 1146238384Sjkim if (pcwidth > 0 || bcwidth > 0) { 1147238384Sjkim pr_u64(&rule->pcnt, pcwidth); 1148238384Sjkim pr_u64(&rule->bcnt, bcwidth); 1149238384Sjkim } 1150238384Sjkim 1151238384Sjkim if (co.do_time == 2) 1152238384Sjkim printf("%10u ", rule->timestamp); 1153238384Sjkim else if (co.do_time == 1) { 1154238384Sjkim char timestr[30]; 1155238384Sjkim time_t t = (time_t)0; 1156238384Sjkim 1157238384Sjkim if (twidth == 0) { 1158238384Sjkim strcpy(timestr, ctime(&t)); 1159238384Sjkim *strchr(timestr, '\n') = '\0'; 1160238384Sjkim twidth = strlen(timestr); 1161238384Sjkim } 1162238384Sjkim if (rule->timestamp) { 1163238384Sjkim t = _long_to_time(rule->timestamp); 1164238384Sjkim 1165238384Sjkim strcpy(timestr, ctime(&t)); 1166238384Sjkim *strchr(timestr, '\n') = '\0'; 1167238384Sjkim printf("%s ", timestr); 1168238384Sjkim } else { 1169238384Sjkim printf("%*s", twidth, " "); 1170238384Sjkim } 1171238384Sjkim } 1172238384Sjkim 1173238384Sjkim if (co.show_sets) 1174238384Sjkim printf("set %d ", rule->set); 1175238384Sjkim 1176238384Sjkim /* 1177238384Sjkim * print the optional "match probability" 1178238384Sjkim */ 1179238384Sjkim if (rule->cmd_len > 0) { 1180238384Sjkim cmd = rule->cmd ; 1181238384Sjkim if (cmd->opcode == O_PROB) { 1182238384Sjkim ipfw_insn_u32 *p = (ipfw_insn_u32 *)cmd; 1183238384Sjkim double d = 1.0 * p->d[0]; 1184238384Sjkim 1185238384Sjkim d = (d / 0x7fffffff); 1186238384Sjkim printf("prob %f ", d); 1187238384Sjkim } 1188238384Sjkim } 1189238384Sjkim 1190238384Sjkim /* 1191238384Sjkim * first print actions 1192238384Sjkim */ 1193238384Sjkim for (l = rule->cmd_len - rule->act_ofs, cmd = ACTION_PTR(rule); 1194238384Sjkim l > 0 ; l -= F_LEN(cmd), cmd += F_LEN(cmd)) { 1195238384Sjkim switch(cmd->opcode) { 1196238384Sjkim case O_CHECK_STATE: 1197238384Sjkim printf("check-state"); 1198238384Sjkim /* avoid printing anything else */ 1199238384Sjkim flags = HAVE_PROTO | HAVE_SRCIP | 1200238384Sjkim HAVE_DSTIP | HAVE_IP; 1201238384Sjkim break; 1202238384Sjkim 1203238384Sjkim case O_ACCEPT: 1204238384Sjkim printf("allow"); 1205238384Sjkim break; 1206238384Sjkim 1207238384Sjkim case O_COUNT: 1208238384Sjkim printf("count"); 1209238384Sjkim break; 1210238384Sjkim 1211238384Sjkim case O_DENY: 1212238384Sjkim printf("deny"); 1213238384Sjkim break; 1214238384Sjkim 1215238384Sjkim case O_REJECT: 1216238384Sjkim if (cmd->arg1 == ICMP_REJECT_RST) 1217238384Sjkim printf("reset"); 1218238384Sjkim else if (cmd->arg1 == ICMP_UNREACH_HOST) 1219238384Sjkim printf("reject"); 1220238384Sjkim else 1221238384Sjkim print_reject_code(cmd->arg1); 1222238384Sjkim break; 1223238384Sjkim 1224238384Sjkim case O_UNREACH6: 1225238384Sjkim if (cmd->arg1 == ICMP6_UNREACH_RST) 1226238384Sjkim printf("reset6"); 1227238384Sjkim else 1228238384Sjkim print_unreach6_code(cmd->arg1); 1229238384Sjkim break; 1230238384Sjkim 1231238384Sjkim case O_SKIPTO: 1232238384Sjkim PRINT_UINT_ARG("skipto ", cmd->arg1); 1233238384Sjkim break; 1234238384Sjkim 1235238384Sjkim case O_PIPE: 1236238384Sjkim PRINT_UINT_ARG("pipe ", cmd->arg1); 1237238384Sjkim break; 1238238384Sjkim 1239238384Sjkim case O_QUEUE: 1240238384Sjkim PRINT_UINT_ARG("queue ", cmd->arg1); 1241238384Sjkim break; 1242238384Sjkim 1243238384Sjkim case O_DIVERT: 1244238384Sjkim PRINT_UINT_ARG("divert ", cmd->arg1); 1245238384Sjkim break; 1246238384Sjkim 1247238384Sjkim case O_TEE: 1248238384Sjkim PRINT_UINT_ARG("tee ", cmd->arg1); 1249238384Sjkim break; 1250238384Sjkim 1251238384Sjkim case O_NETGRAPH: 1252238384Sjkim PRINT_UINT_ARG("netgraph ", cmd->arg1); 1253238384Sjkim break; 1254238384Sjkim 1255238384Sjkim case O_NGTEE: 1256238384Sjkim PRINT_UINT_ARG("ngtee ", cmd->arg1); 1257238384Sjkim break; 1258238384Sjkim 1259238384Sjkim case O_FORWARD_IP: 1260238384Sjkim { 1261238384Sjkim ipfw_insn_sa *s = (ipfw_insn_sa *)cmd; 1262238384Sjkim 1263238384Sjkim if (s->sa.sin_addr.s_addr == INADDR_ANY) { 1264238384Sjkim printf("fwd tablearg"); 1265238384Sjkim } else { 1266238384Sjkim printf("fwd %s", inet_ntoa(s->sa.sin_addr)); 1267238384Sjkim } 1268238384Sjkim if (s->sa.sin_port) 1269238384Sjkim printf(",%d", s->sa.sin_port); 1270238384Sjkim } 1271238384Sjkim break; 1272238384Sjkim 1273238384Sjkim case O_FORWARD_IP6: 1274238384Sjkim { 1275238384Sjkim char buf[4 + INET6_ADDRSTRLEN + 1]; 1276238384Sjkim ipfw_insn_sa6 *s = (ipfw_insn_sa6 *)cmd; 1277238384Sjkim 1278238384Sjkim printf("fwd %s", inet_ntop(AF_INET6, &s->sa.sin6_addr, 1279238384Sjkim buf, sizeof(buf))); 1280238384Sjkim if (s->sa.sin6_port) 1281238384Sjkim printf(",%d", s->sa.sin6_port); 1282238384Sjkim } 1283238384Sjkim break; 1284238384Sjkim 1285238384Sjkim case O_LOG: /* O_LOG is printed last */ 1286238384Sjkim logptr = (ipfw_insn_log *)cmd; 1287238384Sjkim break; 1288238384Sjkim 1289238384Sjkim case O_ALTQ: /* O_ALTQ is printed after O_LOG */ 1290238384Sjkim altqptr = (ipfw_insn_altq *)cmd; 1291238384Sjkim break; 1292238384Sjkim 1293238384Sjkim case O_TAG: 1294238384Sjkim tagptr = cmd; 1295238384Sjkim break; 1296238384Sjkim 1297238384Sjkim case O_NAT: 1298238384Sjkim if (cmd->arg1 != 0) 1299238384Sjkim PRINT_UINT_ARG("nat ", cmd->arg1); 1300238384Sjkim else 1301238384Sjkim printf("nat global"); 1302238384Sjkim break; 1303238384Sjkim 1304238384Sjkim case O_SETFIB: 1305238384Sjkim PRINT_UINT_ARG("setfib ", cmd->arg1); 1306238384Sjkim break; 1307238384Sjkim 1308238384Sjkim case O_SETDSCP: 1309238384Sjkim { 1310238384Sjkim const char *code; 1311238384Sjkim 1312238384Sjkim if ((code = match_value(f_ipdscp, cmd->arg1)) != NULL) 1313238384Sjkim printf("setdscp %s", code); 1314238384Sjkim else 1315238384Sjkim PRINT_UINT_ARG("setdscp ", cmd->arg1); 1316238384Sjkim } 1317238384Sjkim break; 1318238384Sjkim 1319238384Sjkim case O_REASS: 1320238384Sjkim printf("reass"); 1321238384Sjkim break; 1322238384Sjkim 1323238384Sjkim case O_CALLRETURN: 1324238384Sjkim if (cmd->len & F_NOT) 1325238384Sjkim printf("return"); 1326238384Sjkim else 1327238384Sjkim PRINT_UINT_ARG("call ", cmd->arg1); 1328238384Sjkim break; 1329238384Sjkim 1330238384Sjkim default: 1331238384Sjkim printf("** unrecognized action %d len %d ", 1332238384Sjkim cmd->opcode, cmd->len); 1333238384Sjkim } 1334238384Sjkim } 1335238384Sjkim if (logptr) { 1336238384Sjkim if (logptr->max_log > 0) 1337238384Sjkim printf(" log logamount %d", logptr->max_log); 1338238384Sjkim else 1339238384Sjkim printf(" log"); 1340238384Sjkim } 1341238384Sjkim#ifndef NO_ALTQ 1342238384Sjkim if (altqptr) { 1343238384Sjkim print_altq_cmd(altqptr); 1344238384Sjkim } 1345238384Sjkim#endif 1346238384Sjkim if (tagptr) { 1347238384Sjkim if (tagptr->len & F_NOT) 1348238384Sjkim PRINT_UINT_ARG(" untag ", tagptr->arg1); 1349238384Sjkim else 1350238384Sjkim PRINT_UINT_ARG(" tag ", tagptr->arg1); 1351238384Sjkim } 1352238384Sjkim 1353238384Sjkim /* 1354238384Sjkim * then print the body. 1355238384Sjkim */ 1356238384Sjkim for (l = rule->act_ofs, cmd = rule->cmd ; 1357238384Sjkim l > 0 ; l -= F_LEN(cmd) , cmd += F_LEN(cmd)) { 1358238384Sjkim if ((cmd->len & F_OR) || (cmd->len & F_NOT)) 1359238384Sjkim continue; 1360238384Sjkim if (cmd->opcode == O_IP4) { 1361238384Sjkim flags |= HAVE_PROTO4; 1362238384Sjkim break; 1363238384Sjkim } else if (cmd->opcode == O_IP6) { 1364238384Sjkim flags |= HAVE_PROTO6; 1365238384Sjkim break; 1366238384Sjkim } 1367238384Sjkim } 1368238384Sjkim if (rule->_pad & 1) { /* empty rules before options */ 1369238384Sjkim if (!co.do_compact) { 1370238384Sjkim show_prerequisites(&flags, HAVE_PROTO, 0); 1371238384Sjkim printf(" from any to any"); 1372238384Sjkim } 1373238384Sjkim flags |= HAVE_IP | HAVE_OPTIONS | HAVE_PROTO | 1374238384Sjkim HAVE_SRCIP | HAVE_DSTIP; 1375238384Sjkim } 1376238384Sjkim 1377238384Sjkim if (co.comment_only) 1378238384Sjkim comment = "..."; 1379238384Sjkim 1380238384Sjkim for (l = rule->act_ofs, cmd = rule->cmd ; 1381238384Sjkim l > 0 ; l -= F_LEN(cmd) , cmd += F_LEN(cmd)) { 1382238384Sjkim /* useful alias */ 1383238384Sjkim ipfw_insn_u32 *cmd32 = (ipfw_insn_u32 *)cmd; 1384238384Sjkim 1385238384Sjkim if (co.comment_only) { 1386238384Sjkim if (cmd->opcode != O_NOP) 1387238384Sjkim continue; 1388238384Sjkim printf(" // %s\n", (char *)(cmd + 1)); 1389238384Sjkim return; 1390238384Sjkim } 1391238384Sjkim 1392238384Sjkim show_prerequisites(&flags, 0, cmd->opcode); 1393238384Sjkim 1394238384Sjkim switch(cmd->opcode) { 1395238384Sjkim case O_PROB: 1396238384Sjkim break; /* done already */ 1397238384Sjkim 1398238384Sjkim case O_PROBE_STATE: 1399238384Sjkim break; /* no need to print anything here */ 1400238384Sjkim 1401238384Sjkim case O_IP_SRC: 1402238384Sjkim case O_IP_SRC_LOOKUP: 1403238384Sjkim case O_IP_SRC_MASK: 1404238384Sjkim case O_IP_SRC_ME: 1405238384Sjkim case O_IP_SRC_SET: 1406238384Sjkim show_prerequisites(&flags, HAVE_PROTO, 0); 1407238384Sjkim if (!(flags & HAVE_SRCIP)) 1408238384Sjkim printf(" from"); 1409238384Sjkim if ((cmd->len & F_OR) && !or_block) 1410238384Sjkim printf(" {"); 1411238384Sjkim print_ip((ipfw_insn_ip *)cmd, 1412238384Sjkim (flags & HAVE_OPTIONS) ? " src-ip" : ""); 1413238384Sjkim flags |= HAVE_SRCIP; 1414238384Sjkim break; 1415238384Sjkim 1416238384Sjkim case O_IP_DST: 1417238384Sjkim case O_IP_DST_LOOKUP: 1418238384Sjkim case O_IP_DST_MASK: 1419238384Sjkim case O_IP_DST_ME: 1420238384Sjkim case O_IP_DST_SET: 1421238384Sjkim show_prerequisites(&flags, HAVE_PROTO|HAVE_SRCIP, 0); 1422238384Sjkim if (!(flags & HAVE_DSTIP)) 1423238384Sjkim printf(" to"); 1424238384Sjkim if ((cmd->len & F_OR) && !or_block) 1425238384Sjkim printf(" {"); 1426238384Sjkim print_ip((ipfw_insn_ip *)cmd, 1427238384Sjkim (flags & HAVE_OPTIONS) ? " dst-ip" : ""); 1428238384Sjkim flags |= HAVE_DSTIP; 1429238384Sjkim break; 1430238384Sjkim 1431238384Sjkim case O_IP6_SRC: 1432238384Sjkim case O_IP6_SRC_MASK: 1433238384Sjkim case O_IP6_SRC_ME: 1434238384Sjkim show_prerequisites(&flags, HAVE_PROTO, 0); 1435238384Sjkim if (!(flags & HAVE_SRCIP)) 1436238384Sjkim printf(" from"); 1437238384Sjkim if ((cmd->len & F_OR) && !or_block) 1438238384Sjkim printf(" {"); 1439238384Sjkim print_ip6((ipfw_insn_ip6 *)cmd, 1440238384Sjkim (flags & HAVE_OPTIONS) ? " src-ip6" : ""); 1441238384Sjkim flags |= HAVE_SRCIP | HAVE_PROTO; 1442238384Sjkim break; 1443238384Sjkim 1444238384Sjkim case O_IP6_DST: 1445238384Sjkim case O_IP6_DST_MASK: 1446238384Sjkim case O_IP6_DST_ME: 1447238384Sjkim show_prerequisites(&flags, HAVE_PROTO|HAVE_SRCIP, 0); 1448238384Sjkim if (!(flags & HAVE_DSTIP)) 1449238384Sjkim printf(" to"); 1450238384Sjkim if ((cmd->len & F_OR) && !or_block) 1451238384Sjkim printf(" {"); 1452238384Sjkim print_ip6((ipfw_insn_ip6 *)cmd, 1453238384Sjkim (flags & HAVE_OPTIONS) ? " dst-ip6" : ""); 1454238384Sjkim flags |= HAVE_DSTIP; 1455238384Sjkim break; 1456238384Sjkim 1457238384Sjkim case O_FLOW6ID: 1458238384Sjkim print_flow6id( (ipfw_insn_u32 *) cmd ); 1459238384Sjkim flags |= HAVE_OPTIONS; 1460238384Sjkim break; 1461238384Sjkim 1462238384Sjkim case O_IP_DSTPORT: 1463238384Sjkim show_prerequisites(&flags, 1464238384Sjkim HAVE_PROTO | HAVE_SRCIP | 1465238384Sjkim HAVE_DSTIP | HAVE_IP, 0); 1466238384Sjkim case O_IP_SRCPORT: 1467238384Sjkim if (flags & HAVE_DSTIP) 1468238384Sjkim flags |= HAVE_IP; 1469238384Sjkim show_prerequisites(&flags, 1470238384Sjkim HAVE_PROTO | HAVE_SRCIP, 0); 1471238384Sjkim if ((cmd->len & F_OR) && !or_block) 1472238384Sjkim printf(" {"); 1473238384Sjkim if (cmd->len & F_NOT) 1474238384Sjkim printf(" not"); 1475238384Sjkim print_newports((ipfw_insn_u16 *)cmd, proto, 1476238384Sjkim (flags & HAVE_OPTIONS) ? cmd->opcode : 0); 1477238384Sjkim break; 1478238384Sjkim 1479238384Sjkim case O_PROTO: { 1480238384Sjkim struct protoent *pe = NULL; 1481238384Sjkim 1482238384Sjkim if ((cmd->len & F_OR) && !or_block) 1483238384Sjkim printf(" {"); 1484238384Sjkim if (cmd->len & F_NOT) 1485238384Sjkim printf(" not"); 1486238384Sjkim proto = cmd->arg1; 1487238384Sjkim pe = getprotobynumber(cmd->arg1); 1488238384Sjkim if ((flags & (HAVE_PROTO4 | HAVE_PROTO6)) && 1489238384Sjkim !(flags & HAVE_PROTO)) 1490238384Sjkim show_prerequisites(&flags, 1491238384Sjkim HAVE_PROTO | HAVE_IP | HAVE_SRCIP | 1492238384Sjkim HAVE_DSTIP | HAVE_OPTIONS, 0); 1493238384Sjkim if (flags & HAVE_OPTIONS) 1494238384Sjkim printf(" proto"); 1495238384Sjkim if (pe) 1496238384Sjkim printf(" %s", pe->p_name); 1497238384Sjkim else 1498 printf(" %u", cmd->arg1); 1499 } 1500 flags |= HAVE_PROTO; 1501 break; 1502 1503 default: /*options ... */ 1504 if (!(cmd->len & (F_OR|F_NOT))) 1505 if (((cmd->opcode == O_IP6) && 1506 (flags & HAVE_PROTO6)) || 1507 ((cmd->opcode == O_IP4) && 1508 (flags & HAVE_PROTO4))) 1509 break; 1510 show_prerequisites(&flags, HAVE_PROTO | HAVE_SRCIP | 1511 HAVE_DSTIP | HAVE_IP | HAVE_OPTIONS, 0); 1512 if ((cmd->len & F_OR) && !or_block) 1513 printf(" {"); 1514 if (cmd->len & F_NOT && cmd->opcode != O_IN) 1515 printf(" not"); 1516 switch(cmd->opcode) { 1517 case O_MACADDR2: { 1518 ipfw_insn_mac *m = (ipfw_insn_mac *)cmd; 1519 1520 printf(" MAC"); 1521 print_mac(m->addr, m->mask); 1522 print_mac(m->addr + 6, m->mask + 6); 1523 } 1524 break; 1525 1526 case O_MAC_TYPE: 1527 print_newports((ipfw_insn_u16 *)cmd, 1528 IPPROTO_ETHERTYPE, cmd->opcode); 1529 break; 1530 1531 1532 case O_FRAG: 1533 printf(" frag"); 1534 break; 1535 1536 case O_FIB: 1537 printf(" fib %u", cmd->arg1 ); 1538 break; 1539 case O_SOCKARG: 1540 printf(" sockarg"); 1541 break; 1542 1543 case O_IN: 1544 printf(cmd->len & F_NOT ? " out" : " in"); 1545 break; 1546 1547 case O_DIVERTED: 1548 switch (cmd->arg1) { 1549 case 3: 1550 printf(" diverted"); 1551 break; 1552 case 1: 1553 printf(" diverted-loopback"); 1554 break; 1555 case 2: 1556 printf(" diverted-output"); 1557 break; 1558 default: 1559 printf(" diverted-?<%u>", cmd->arg1); 1560 break; 1561 } 1562 break; 1563 1564 case O_LAYER2: 1565 printf(" layer2"); 1566 break; 1567 case O_XMIT: 1568 case O_RECV: 1569 case O_VIA: 1570 { 1571 char const *s; 1572 ipfw_insn_if *cmdif = (ipfw_insn_if *)cmd; 1573 1574 if (cmd->opcode == O_XMIT) 1575 s = "xmit"; 1576 else if (cmd->opcode == O_RECV) 1577 s = "recv"; 1578 else /* if (cmd->opcode == O_VIA) */ 1579 s = "via"; 1580 if (cmdif->name[0] == '\0') 1581 printf(" %s %s", s, 1582 inet_ntoa(cmdif->p.ip)); 1583 else if (cmdif->name[0] == '\1') /* interface table */ 1584 printf(" %s table(%d)", s, cmdif->p.glob); 1585 else 1586 printf(" %s %s", s, cmdif->name); 1587 1588 break; 1589 } 1590 case O_IPID: 1591 if (F_LEN(cmd) == 1) 1592 printf(" ipid %u", cmd->arg1 ); 1593 else 1594 print_newports((ipfw_insn_u16 *)cmd, 0, 1595 O_IPID); 1596 break; 1597 1598 case O_IPTTL: 1599 if (F_LEN(cmd) == 1) 1600 printf(" ipttl %u", cmd->arg1 ); 1601 else 1602 print_newports((ipfw_insn_u16 *)cmd, 0, 1603 O_IPTTL); 1604 break; 1605 1606 case O_IPVER: 1607 printf(" ipver %u", cmd->arg1 ); 1608 break; 1609 1610 case O_IPPRECEDENCE: 1611 printf(" ipprecedence %u", (cmd->arg1) >> 5 ); 1612 break; 1613 1614 case O_DSCP: 1615 print_dscp((ipfw_insn_u32 *)cmd); 1616 break; 1617 1618 case O_IPLEN: 1619 if (F_LEN(cmd) == 1) 1620 printf(" iplen %u", cmd->arg1 ); 1621 else 1622 print_newports((ipfw_insn_u16 *)cmd, 0, 1623 O_IPLEN); 1624 break; 1625 1626 case O_IPOPT: 1627 print_flags("ipoptions", cmd, f_ipopts); 1628 break; 1629 1630 case O_IPTOS: 1631 print_flags("iptos", cmd, f_iptos); 1632 break; 1633 1634 case O_ICMPTYPE: 1635 print_icmptypes((ipfw_insn_u32 *)cmd); 1636 break; 1637 1638 case O_ESTAB: 1639 printf(" established"); 1640 break; 1641 1642 case O_TCPDATALEN: 1643 if (F_LEN(cmd) == 1) 1644 printf(" tcpdatalen %u", cmd->arg1 ); 1645 else 1646 print_newports((ipfw_insn_u16 *)cmd, 0, 1647 O_TCPDATALEN); 1648 break; 1649 1650 case O_TCPFLAGS: 1651 print_flags("tcpflags", cmd, f_tcpflags); 1652 break; 1653 1654 case O_TCPOPTS: 1655 print_flags("tcpoptions", cmd, f_tcpopts); 1656 break; 1657 1658 case O_TCPWIN: 1659 if (F_LEN(cmd) == 1) 1660 printf(" tcpwin %u", cmd->arg1); 1661 else 1662 print_newports((ipfw_insn_u16 *)cmd, 0, 1663 O_TCPWIN); 1664 break; 1665 1666 case O_TCPACK: 1667 printf(" tcpack %d", ntohl(cmd32->d[0])); 1668 break; 1669 1670 case O_TCPSEQ: 1671 printf(" tcpseq %d", ntohl(cmd32->d[0])); 1672 break; 1673 1674 case O_UID: 1675 { 1676 struct passwd *pwd = getpwuid(cmd32->d[0]); 1677 1678 if (pwd) 1679 printf(" uid %s", pwd->pw_name); 1680 else 1681 printf(" uid %u", cmd32->d[0]); 1682 } 1683 break; 1684 1685 case O_GID: 1686 { 1687 struct group *grp = getgrgid(cmd32->d[0]); 1688 1689 if (grp) 1690 printf(" gid %s", grp->gr_name); 1691 else 1692 printf(" gid %u", cmd32->d[0]); 1693 } 1694 break; 1695 1696 case O_JAIL: 1697 printf(" jail %d", cmd32->d[0]); 1698 break; 1699 1700 case O_VERREVPATH: 1701 printf(" verrevpath"); 1702 break; 1703 1704 case O_VERSRCREACH: 1705 printf(" versrcreach"); 1706 break; 1707 1708 case O_ANTISPOOF: 1709 printf(" antispoof"); 1710 break; 1711 1712 case O_IPSEC: 1713 printf(" ipsec"); 1714 break; 1715 1716 case O_NOP: 1717 comment = (char *)(cmd + 1); 1718 break; 1719 1720 case O_KEEP_STATE: 1721 printf(" keep-state"); 1722 break; 1723 1724 case O_LIMIT: { 1725 struct _s_x *p = limit_masks; 1726 ipfw_insn_limit *c = (ipfw_insn_limit *)cmd; 1727 uint8_t x = c->limit_mask; 1728 char const *comma = " "; 1729 1730 printf(" limit"); 1731 for (; p->x != 0 ; p++) 1732 if ((x & p->x) == p->x) { 1733 x &= ~p->x; 1734 printf("%s%s", comma, p->s); 1735 comma = ","; 1736 } 1737 PRINT_UINT_ARG(" ", c->conn_limit); 1738 break; 1739 } 1740 1741 case O_IP6: 1742 printf(" ip6"); 1743 break; 1744 1745 case O_IP4: 1746 printf(" ip4"); 1747 break; 1748 1749 case O_ICMP6TYPE: 1750 print_icmp6types((ipfw_insn_u32 *)cmd); 1751 break; 1752 1753 case O_EXT_HDR: 1754 print_ext6hdr( (ipfw_insn *) cmd ); 1755 break; 1756 1757 case O_TAGGED: 1758 if (F_LEN(cmd) == 1) 1759 PRINT_UINT_ARG(" tagged ", cmd->arg1); 1760 else 1761 print_newports((ipfw_insn_u16 *)cmd, 0, 1762 O_TAGGED); 1763 break; 1764 1765 default: 1766 printf(" [opcode %d len %d]", 1767 cmd->opcode, cmd->len); 1768 } 1769 } 1770 if (cmd->len & F_OR) { 1771 printf(" or"); 1772 or_block = 1; 1773 } else if (or_block) { 1774 printf(" }"); 1775 or_block = 0; 1776 } 1777 } 1778 show_prerequisites(&flags, HAVE_PROTO | HAVE_SRCIP | HAVE_DSTIP 1779 | HAVE_IP, 0); 1780 if (comment) 1781 printf(" // %s", comment); 1782 printf("\n"); 1783} 1784 1785static void 1786show_dyn_ipfw(ipfw_dyn_rule *d, int pcwidth, int bcwidth) 1787{ 1788 struct protoent *pe; 1789 struct in_addr a; 1790 uint16_t rulenum; 1791 char buf[INET6_ADDRSTRLEN]; 1792 1793 if (!co.do_expired) { 1794 if (!d->expire && !(d->dyn_type == O_LIMIT_PARENT)) 1795 return; 1796 } 1797 bcopy(&d->rule, &rulenum, sizeof(rulenum)); 1798 printf("%05d", rulenum); 1799 if (pcwidth > 0 || bcwidth > 0) { 1800 printf(" "); 1801 pr_u64(&d->pcnt, pcwidth); 1802 pr_u64(&d->bcnt, bcwidth); 1803 printf("(%ds)", d->expire); 1804 } 1805 switch (d->dyn_type) { 1806 case O_LIMIT_PARENT: 1807 printf(" PARENT %d", d->count); 1808 break; 1809 case O_LIMIT: 1810 printf(" LIMIT"); 1811 break; 1812 case O_KEEP_STATE: /* bidir, no mask */ 1813 printf(" STATE"); 1814 break; 1815 } 1816 1817 if ((pe = getprotobynumber(d->id.proto)) != NULL) 1818 printf(" %s", pe->p_name); 1819 else 1820 printf(" proto %u", d->id.proto); 1821 1822 if (d->id.addr_type == 4) { 1823 a.s_addr = htonl(d->id.src_ip); 1824 printf(" %s %d", inet_ntoa(a), d->id.src_port); 1825 1826 a.s_addr = htonl(d->id.dst_ip); 1827 printf(" <-> %s %d", inet_ntoa(a), d->id.dst_port); 1828 } else if (d->id.addr_type == 6) { 1829 printf(" %s %d", inet_ntop(AF_INET6, &d->id.src_ip6, buf, 1830 sizeof(buf)), d->id.src_port); 1831 printf(" <-> %s %d", inet_ntop(AF_INET6, &d->id.dst_ip6, buf, 1832 sizeof(buf)), d->id.dst_port); 1833 } else 1834 printf(" UNKNOWN <-> UNKNOWN\n"); 1835 1836 printf("\n"); 1837} 1838 1839/* 1840 * This one handles all set-related commands 1841 * ipfw set { show | enable | disable } 1842 * ipfw set swap X Y 1843 * ipfw set move X to Y 1844 * ipfw set move rule X to Y 1845 */ 1846void 1847ipfw_sets_handler(char *av[]) 1848{ 1849 uint32_t set_disable, masks[2]; 1850 int i, nbytes; 1851 uint16_t rulenum; 1852 uint8_t cmd, new_set; 1853 1854 av++; 1855 1856 if (av[0] == NULL) 1857 errx(EX_USAGE, "set needs command"); 1858 if (_substrcmp(*av, "show") == 0) { 1859 void *data = NULL; 1860 char const *msg; 1861 int nalloc; 1862 1863 nalloc = nbytes = sizeof(struct ip_fw); 1864 while (nbytes >= nalloc) { 1865 if (data) 1866 free(data); 1867 nalloc = nalloc * 2 + 200; 1868 nbytes = nalloc; 1869 data = safe_calloc(1, nbytes); 1870 if (do_cmd(IP_FW_GET, data, (uintptr_t)&nbytes) < 0) 1871 err(EX_OSERR, "getsockopt(IP_FW_GET)"); 1872 } 1873 1874 bcopy(&((struct ip_fw *)data)->next_rule, 1875 &set_disable, sizeof(set_disable)); 1876 1877 for (i = 0, msg = "disable" ; i < RESVD_SET; i++) 1878 if ((set_disable & (1<<i))) { 1879 printf("%s %d", msg, i); 1880 msg = ""; 1881 } 1882 msg = (set_disable) ? " enable" : "enable"; 1883 for (i = 0; i < RESVD_SET; i++) 1884 if (!(set_disable & (1<<i))) { 1885 printf("%s %d", msg, i); 1886 msg = ""; 1887 } 1888 printf("\n"); 1889 } else if (_substrcmp(*av, "swap") == 0) { 1890 av++; 1891 if ( av[0] == NULL || av[1] == NULL ) 1892 errx(EX_USAGE, "set swap needs 2 set numbers\n"); 1893 rulenum = atoi(av[0]); 1894 new_set = atoi(av[1]); 1895 if (!isdigit(*(av[0])) || rulenum > RESVD_SET) 1896 errx(EX_DATAERR, "invalid set number %s\n", av[0]); 1897 if (!isdigit(*(av[1])) || new_set > RESVD_SET) 1898 errx(EX_DATAERR, "invalid set number %s\n", av[1]); 1899 masks[0] = (4 << 24) | (new_set << 16) | (rulenum); 1900 i = do_cmd(IP_FW_DEL, masks, sizeof(uint32_t)); 1901 } else if (_substrcmp(*av, "move") == 0) { 1902 av++; 1903 if (av[0] && _substrcmp(*av, "rule") == 0) { 1904 cmd = 2; 1905 av++; 1906 } else 1907 cmd = 3; 1908 if (av[0] == NULL || av[1] == NULL || av[2] == NULL || 1909 av[3] != NULL || _substrcmp(av[1], "to") != 0) 1910 errx(EX_USAGE, "syntax: set move [rule] X to Y\n"); 1911 rulenum = atoi(av[0]); 1912 new_set = atoi(av[2]); 1913 if (!isdigit(*(av[0])) || (cmd == 3 && rulenum > RESVD_SET) || 1914 (cmd == 2 && rulenum == IPFW_DEFAULT_RULE) ) 1915 errx(EX_DATAERR, "invalid source number %s\n", av[0]); 1916 if (!isdigit(*(av[2])) || new_set > RESVD_SET) 1917 errx(EX_DATAERR, "invalid dest. set %s\n", av[1]); 1918 masks[0] = (cmd << 24) | (new_set << 16) | (rulenum); 1919 i = do_cmd(IP_FW_DEL, masks, sizeof(uint32_t)); 1920 } else if (_substrcmp(*av, "disable") == 0 || 1921 _substrcmp(*av, "enable") == 0 ) { 1922 int which = _substrcmp(*av, "enable") == 0 ? 1 : 0; 1923 1924 av++; 1925 masks[0] = masks[1] = 0; 1926 1927 while (av[0]) { 1928 if (isdigit(**av)) { 1929 i = atoi(*av); 1930 if (i < 0 || i > RESVD_SET) 1931 errx(EX_DATAERR, 1932 "invalid set number %d\n", i); 1933 masks[which] |= (1<<i); 1934 } else if (_substrcmp(*av, "disable") == 0) 1935 which = 0; 1936 else if (_substrcmp(*av, "enable") == 0) 1937 which = 1; 1938 else 1939 errx(EX_DATAERR, 1940 "invalid set command %s\n", *av); 1941 av++; 1942 } 1943 if ( (masks[0] & masks[1]) != 0 ) 1944 errx(EX_DATAERR, 1945 "cannot enable and disable the same set\n"); 1946 1947 i = do_cmd(IP_FW_DEL, masks, sizeof(masks)); 1948 if (i) 1949 warn("set enable/disable: setsockopt(IP_FW_DEL)"); 1950 } else 1951 errx(EX_USAGE, "invalid set command %s\n", *av); 1952} 1953 1954void 1955ipfw_sysctl_handler(char *av[], int which) 1956{ 1957 av++; 1958 1959 if (av[0] == NULL) { 1960 warnx("missing keyword to enable/disable\n"); 1961 } else if (_substrcmp(*av, "firewall") == 0) { 1962 sysctlbyname("net.inet.ip.fw.enable", NULL, 0, 1963 &which, sizeof(which)); 1964 sysctlbyname("net.inet6.ip6.fw.enable", NULL, 0, 1965 &which, sizeof(which)); 1966 } else if (_substrcmp(*av, "one_pass") == 0) { 1967 sysctlbyname("net.inet.ip.fw.one_pass", NULL, 0, 1968 &which, sizeof(which)); 1969 } else if (_substrcmp(*av, "debug") == 0) { 1970 sysctlbyname("net.inet.ip.fw.debug", NULL, 0, 1971 &which, sizeof(which)); 1972 } else if (_substrcmp(*av, "verbose") == 0) { 1973 sysctlbyname("net.inet.ip.fw.verbose", NULL, 0, 1974 &which, sizeof(which)); 1975 } else if (_substrcmp(*av, "dyn_keepalive") == 0) { 1976 sysctlbyname("net.inet.ip.fw.dyn_keepalive", NULL, 0, 1977 &which, sizeof(which)); 1978#ifndef NO_ALTQ 1979 } else if (_substrcmp(*av, "altq") == 0) { 1980 altq_set_enabled(which); 1981#endif 1982 } else { 1983 warnx("unrecognize enable/disable keyword: %s\n", *av); 1984 } 1985} 1986 1987void 1988ipfw_list(int ac, char *av[], int show_counters) 1989{ 1990 struct ip_fw *r; 1991 ipfw_dyn_rule *dynrules, *d; 1992 1993#define NEXT(r) ((struct ip_fw *)((char *)r + RULESIZE(r))) 1994 char *lim; 1995 void *data = NULL; 1996 int bcwidth, n, nbytes, nstat, ndyn, pcwidth, width; 1997 int exitval = EX_OK; 1998 int lac; 1999 char **lav; 2000 u_long rnum, last; 2001 char *endptr; 2002 int seen = 0; 2003 uint8_t set; 2004 2005 const int ocmd = co.do_pipe ? IP_DUMMYNET_GET : IP_FW_GET; 2006 int nalloc = 1024; /* start somewhere... */ 2007 2008 last = 0; 2009 2010 if (co.test_only) { 2011 fprintf(stderr, "Testing only, list disabled\n"); 2012 return; 2013 } 2014 if (co.do_pipe) { 2015 dummynet_list(ac, av, show_counters); 2016 return; 2017 } 2018 2019 ac--; 2020 av++; 2021 2022 /* get rules or pipes from kernel, resizing array as necessary */ 2023 nbytes = nalloc; 2024 2025 while (nbytes >= nalloc) { 2026 nalloc = nalloc * 2 + 200; 2027 nbytes = nalloc; 2028 data = safe_realloc(data, nbytes); 2029 if (do_cmd(ocmd, data, (uintptr_t)&nbytes) < 0) 2030 err(EX_OSERR, "getsockopt(IP_%s_GET)", 2031 co.do_pipe ? "DUMMYNET" : "FW"); 2032 } 2033 2034 /* 2035 * Count static rules. They have variable size so we 2036 * need to scan the list to count them. 2037 */ 2038 for (nstat = 1, r = data, lim = (char *)data + nbytes; 2039 r->rulenum < IPFW_DEFAULT_RULE && (char *)r < lim; 2040 ++nstat, r = NEXT(r) ) 2041 ; /* nothing */ 2042 2043 /* 2044 * Count dynamic rules. This is easier as they have 2045 * fixed size. 2046 */ 2047 r = NEXT(r); 2048 dynrules = (ipfw_dyn_rule *)r ; 2049 n = (char *)r - (char *)data; 2050 ndyn = (nbytes - n) / sizeof *dynrules; 2051 2052 /* if showing stats, figure out column widths ahead of time */ 2053 bcwidth = pcwidth = 0; 2054 if (show_counters) { 2055 for (n = 0, r = data; n < nstat; n++, r = NEXT(r)) { 2056 /* skip rules from another set */ 2057 if (co.use_set && r->set != co.use_set - 1) 2058 continue; 2059 2060 /* packet counter */ 2061 width = pr_u64(&r->pcnt, 0); 2062 if (width > pcwidth) 2063 pcwidth = width; 2064 2065 /* byte counter */ 2066 width = pr_u64(&r->bcnt, 0); 2067 if (width > bcwidth) 2068 bcwidth = width; 2069 } 2070 } 2071 if (co.do_dynamic && ndyn) { 2072 for (n = 0, d = dynrules; n < ndyn; n++, d++) { 2073 if (co.use_set) { 2074 /* skip rules from another set */ 2075 bcopy((char *)&d->rule + sizeof(uint16_t), 2076 &set, sizeof(uint8_t)); 2077 if (set != co.use_set - 1) 2078 continue; 2079 } 2080 width = pr_u64(&d->pcnt, 0); 2081 if (width > pcwidth) 2082 pcwidth = width; 2083 2084 width = pr_u64(&d->bcnt, 0); 2085 if (width > bcwidth) 2086 bcwidth = width; 2087 } 2088 } 2089 /* if no rule numbers were specified, list all rules */ 2090 if (ac == 0) { 2091 for (n = 0, r = data; n < nstat; n++, r = NEXT(r)) { 2092 if (co.use_set && r->set != co.use_set - 1) 2093 continue; 2094 show_ipfw(r, pcwidth, bcwidth); 2095 } 2096 2097 if (co.do_dynamic && ndyn) { 2098 printf("## Dynamic rules (%d):\n", ndyn); 2099 for (n = 0, d = dynrules; n < ndyn; n++, d++) { 2100 if (co.use_set) { 2101 bcopy((char *)&d->rule + sizeof(uint16_t), 2102 &set, sizeof(uint8_t)); 2103 if (set != co.use_set - 1) 2104 continue; 2105 } 2106 show_dyn_ipfw(d, pcwidth, bcwidth); 2107 } 2108 } 2109 goto done; 2110 } 2111 2112 /* display specific rules requested on command line */ 2113 2114 for (lac = ac, lav = av; lac != 0; lac--) { 2115 /* convert command line rule # */ 2116 last = rnum = strtoul(*lav++, &endptr, 10); 2117 if (*endptr == '-') 2118 last = strtoul(endptr+1, &endptr, 10); 2119 if (*endptr) { 2120 exitval = EX_USAGE; 2121 warnx("invalid rule number: %s", *(lav - 1)); 2122 continue; 2123 } 2124 for (n = seen = 0, r = data; n < nstat; n++, r = NEXT(r) ) { 2125 if (r->rulenum > last) 2126 break; 2127 if (co.use_set && r->set != co.use_set - 1) 2128 continue; 2129 if (r->rulenum >= rnum && r->rulenum <= last) { 2130 show_ipfw(r, pcwidth, bcwidth); 2131 seen = 1; 2132 } 2133 } 2134 if (!seen) { 2135 /* give precedence to other error(s) */ 2136 if (exitval == EX_OK) 2137 exitval = EX_UNAVAILABLE; 2138 warnx("rule %lu does not exist", rnum); 2139 } 2140 } 2141 2142 if (co.do_dynamic && ndyn) { 2143 printf("## Dynamic rules:\n"); 2144 for (lac = ac, lav = av; lac != 0; lac--) { 2145 last = rnum = strtoul(*lav++, &endptr, 10); 2146 if (*endptr == '-') 2147 last = strtoul(endptr+1, &endptr, 10); 2148 if (*endptr) 2149 /* already warned */ 2150 continue; 2151 for (n = 0, d = dynrules; n < ndyn; n++, d++) { 2152 uint16_t rulenum; 2153 2154 bcopy(&d->rule, &rulenum, sizeof(rulenum)); 2155 if (rulenum > rnum) 2156 break; 2157 if (co.use_set) { 2158 bcopy((char *)&d->rule + sizeof(uint16_t), 2159 &set, sizeof(uint8_t)); 2160 if (set != co.use_set - 1) 2161 continue; 2162 } 2163 if (r->rulenum >= rnum && r->rulenum <= last) 2164 show_dyn_ipfw(d, pcwidth, bcwidth); 2165 } 2166 } 2167 } 2168 2169 ac = 0; 2170 2171done: 2172 free(data); 2173 2174 if (exitval != EX_OK) 2175 exit(exitval); 2176#undef NEXT 2177} 2178 2179static int 2180lookup_host (char *host, struct in_addr *ipaddr) 2181{ 2182 struct hostent *he; 2183 2184 if (!inet_aton(host, ipaddr)) { 2185 if ((he = gethostbyname(host)) == NULL) 2186 return(-1); 2187 *ipaddr = *(struct in_addr *)he->h_addr_list[0]; 2188 } 2189 return(0); 2190} 2191 2192/* 2193 * fills the addr and mask fields in the instruction as appropriate from av. 2194 * Update length as appropriate. 2195 * The following formats are allowed: 2196 * me returns O_IP_*_ME 2197 * 1.2.3.4 single IP address 2198 * 1.2.3.4:5.6.7.8 address:mask 2199 * 1.2.3.4/24 address/mask 2200 * 1.2.3.4/26{1,6,5,4,23} set of addresses in a subnet 2201 * We can have multiple comma-separated address/mask entries. 2202 */ 2203static void 2204fill_ip(ipfw_insn_ip *cmd, char *av, int cblen) 2205{ 2206 int len = 0; 2207 uint32_t *d = ((ipfw_insn_u32 *)cmd)->d; 2208 uint32_t tables_max; 2209 2210 cmd->o.len &= ~F_LEN_MASK; /* zero len */ 2211 2212 if (_substrcmp(av, "any") == 0) 2213 return; 2214 2215 if (_substrcmp(av, "me") == 0) { 2216 cmd->o.len |= F_INSN_SIZE(ipfw_insn); 2217 return; 2218 } 2219 2220 if (strncmp(av, "table(", 6) == 0) { 2221 char *p = strchr(av + 6, ','); 2222 2223 if (p) 2224 *p++ = '\0'; 2225 cmd->o.opcode = O_IP_DST_LOOKUP; 2226 cmd->o.arg1 = strtoul(av + 6, NULL, 0); 2227 tables_max = ipfw_get_tables_max(); 2228 if (cmd->o.arg1 > tables_max) 2229 errx(EX_USAGE, "The table number exceeds the maximum " 2230 "allowed value (%u)", tables_max - 1); 2231 if (p) { 2232 cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32); 2233 d[0] = strtoul(p, NULL, 0); 2234 } else 2235 cmd->o.len |= F_INSN_SIZE(ipfw_insn); 2236 return; 2237 } 2238 2239 while (av) { 2240 /* 2241 * After the address we can have '/' or ':' indicating a mask, 2242 * ',' indicating another address follows, '{' indicating a 2243 * set of addresses of unspecified size. 2244 */ 2245 char *t = NULL, *p = strpbrk(av, "/:,{"); 2246 int masklen; 2247 char md, nd = '\0'; 2248 2249 CHECK_LENGTH(cblen, F_INSN_SIZE(ipfw_insn) + 2 + len); 2250 2251 if (p) { 2252 md = *p; 2253 *p++ = '\0'; 2254 if ((t = strpbrk(p, ",{")) != NULL) { 2255 nd = *t; 2256 *t = '\0'; 2257 } 2258 } else 2259 md = '\0'; 2260 2261 if (lookup_host(av, (struct in_addr *)&d[0]) != 0) 2262 errx(EX_NOHOST, "hostname ``%s'' unknown", av); 2263 switch (md) { 2264 case ':': 2265 if (!inet_aton(p, (struct in_addr *)&d[1])) 2266 errx(EX_DATAERR, "bad netmask ``%s''", p); 2267 break; 2268 case '/': 2269 masklen = atoi(p); 2270 if (masklen == 0) 2271 d[1] = htonl(0); /* mask */ 2272 else if (masklen > 32) 2273 errx(EX_DATAERR, "bad width ``%s''", p); 2274 else 2275 d[1] = htonl(~0 << (32 - masklen)); 2276 break; 2277 case '{': /* no mask, assume /24 and put back the '{' */ 2278 d[1] = htonl(~0 << (32 - 24)); 2279 *(--p) = md; 2280 break; 2281 2282 case ',': /* single address plus continuation */ 2283 *(--p) = md; 2284 /* FALLTHROUGH */ 2285 case 0: /* initialization value */ 2286 default: 2287 d[1] = htonl(~0); /* force /32 */ 2288 break; 2289 } 2290 d[0] &= d[1]; /* mask base address with mask */ 2291 if (t) 2292 *t = nd; 2293 /* find next separator */ 2294 if (p) 2295 p = strpbrk(p, ",{"); 2296 if (p && *p == '{') { 2297 /* 2298 * We have a set of addresses. They are stored as follows: 2299 * arg1 is the set size (powers of 2, 2..256) 2300 * addr is the base address IN HOST FORMAT 2301 * mask.. is an array of arg1 bits (rounded up to 2302 * the next multiple of 32) with bits set 2303 * for each host in the map. 2304 */ 2305 uint32_t *map = (uint32_t *)&cmd->mask; 2306 int low, high; 2307 int i = contigmask((uint8_t *)&(d[1]), 32); 2308 2309 if (len > 0) 2310 errx(EX_DATAERR, "address set cannot be in a list"); 2311 if (i < 24 || i > 31) 2312 errx(EX_DATAERR, "invalid set with mask %d\n", i); 2313 cmd->o.arg1 = 1<<(32-i); /* map length */ 2314 d[0] = ntohl(d[0]); /* base addr in host format */ 2315 cmd->o.opcode = O_IP_DST_SET; /* default */ 2316 cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32) + (cmd->o.arg1+31)/32; 2317 for (i = 0; i < (cmd->o.arg1+31)/32 ; i++) 2318 map[i] = 0; /* clear map */ 2319 2320 av = p + 1; 2321 low = d[0] & 0xff; 2322 high = low + cmd->o.arg1 - 1; 2323 /* 2324 * Here, i stores the previous value when we specify a range 2325 * of addresses within a mask, e.g. 45-63. i = -1 means we 2326 * have no previous value. 2327 */ 2328 i = -1; /* previous value in a range */ 2329 while (isdigit(*av)) { 2330 char *s; 2331 int a = strtol(av, &s, 0); 2332 2333 if (s == av) { /* no parameter */ 2334 if (*av != '}') 2335 errx(EX_DATAERR, "set not closed\n"); 2336 if (i != -1) 2337 errx(EX_DATAERR, "incomplete range %d-", i); 2338 break; 2339 } 2340 if (a < low || a > high) 2341 errx(EX_DATAERR, "addr %d out of range [%d-%d]\n", 2342 a, low, high); 2343 a -= low; 2344 if (i == -1) /* no previous in range */ 2345 i = a; 2346 else { /* check that range is valid */ 2347 if (i > a) 2348 errx(EX_DATAERR, "invalid range %d-%d", 2349 i+low, a+low); 2350 if (*s == '-') 2351 errx(EX_DATAERR, "double '-' in range"); 2352 } 2353 for (; i <= a; i++) 2354 map[i/32] |= 1<<(i & 31); 2355 i = -1; 2356 if (*s == '-') 2357 i = a; 2358 else if (*s == '}') 2359 break; 2360 av = s+1; 2361 } 2362 return; 2363 } 2364 av = p; 2365 if (av) /* then *av must be a ',' */ 2366 av++; 2367 2368 /* Check this entry */ 2369 if (d[1] == 0) { /* "any", specified as x.x.x.x/0 */ 2370 /* 2371 * 'any' turns the entire list into a NOP. 2372 * 'not any' never matches, so it is removed from the 2373 * list unless it is the only item, in which case we 2374 * report an error. 2375 */ 2376 if (cmd->o.len & F_NOT) { /* "not any" never matches */ 2377 if (av == NULL && len == 0) /* only this entry */ 2378 errx(EX_DATAERR, "not any never matches"); 2379 } 2380 /* else do nothing and skip this entry */ 2381 return; 2382 } 2383 /* A single IP can be stored in an optimized format */ 2384 if (d[1] == (uint32_t)~0 && av == NULL && len == 0) { 2385 cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32); 2386 return; 2387 } 2388 len += 2; /* two words... */ 2389 d += 2; 2390 } /* end while */ 2391 if (len + 1 > F_LEN_MASK) 2392 errx(EX_DATAERR, "address list too long"); 2393 cmd->o.len |= len+1; 2394} 2395 2396 2397/* n2mask sets n bits of the mask */ 2398void 2399n2mask(struct in6_addr *mask, int n) 2400{ 2401 static int minimask[9] = 2402 { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff }; 2403 u_char *p; 2404 2405 memset(mask, 0, sizeof(struct in6_addr)); 2406 p = (u_char *) mask; 2407 for (; n > 0; p++, n -= 8) { 2408 if (n >= 8) 2409 *p = 0xff; 2410 else 2411 *p = minimask[n]; 2412 } 2413 return; 2414} 2415 2416/* 2417 * helper function to process a set of flags and set bits in the 2418 * appropriate masks. 2419 */ 2420static void 2421fill_flags(ipfw_insn *cmd, enum ipfw_opcodes opcode, 2422 struct _s_x *flags, char *p) 2423{ 2424 uint8_t set=0, clear=0; 2425 2426 while (p && *p) { 2427 char *q; /* points to the separator */ 2428 int val; 2429 uint8_t *which; /* mask we are working on */ 2430 2431 if (*p == '!') { 2432 p++; 2433 which = &clear; 2434 } else 2435 which = &set; 2436 q = strchr(p, ','); 2437 if (q) 2438 *q++ = '\0'; 2439 val = match_token(flags, p); 2440 if (val <= 0) 2441 errx(EX_DATAERR, "invalid flag %s", p); 2442 *which |= (uint8_t)val; 2443 p = q; 2444 } 2445 cmd->opcode = opcode; 2446 cmd->len = (cmd->len & (F_NOT | F_OR)) | 1; 2447 cmd->arg1 = (set & 0xff) | ( (clear & 0xff) << 8); 2448} 2449 2450 2451void 2452ipfw_delete(char *av[]) 2453{ 2454 uint32_t rulenum; 2455 int i; 2456 int exitval = EX_OK; 2457 int do_set = 0; 2458 2459 av++; 2460 NEED1("missing rule specification"); 2461 if ( *av && _substrcmp(*av, "set") == 0) { 2462 /* Do not allow using the following syntax: 2463 * ipfw set N delete set M 2464 */ 2465 if (co.use_set) 2466 errx(EX_DATAERR, "invalid syntax"); 2467 do_set = 1; /* delete set */ 2468 av++; 2469 } 2470 2471 /* Rule number */ 2472 while (*av && isdigit(**av)) { 2473 i = atoi(*av); av++; 2474 if (co.do_nat) { 2475 exitval = do_cmd(IP_FW_NAT_DEL, &i, sizeof i); 2476 if (exitval) { 2477 exitval = EX_UNAVAILABLE; 2478 warn("rule %u not available", i); 2479 } 2480 } else if (co.do_pipe) { 2481 exitval = ipfw_delete_pipe(co.do_pipe, i); 2482 } else { 2483 if (co.use_set) 2484 rulenum = (i & 0xffff) | (5 << 24) | 2485 ((co.use_set - 1) << 16); 2486 else 2487 rulenum = (i & 0xffff) | (do_set << 24); 2488 i = do_cmd(IP_FW_DEL, &rulenum, sizeof rulenum); 2489 if (i) { 2490 exitval = EX_UNAVAILABLE; 2491 warn("rule %u: setsockopt(IP_FW_DEL)", 2492 rulenum); 2493 } 2494 } 2495 } 2496 if (exitval != EX_OK) 2497 exit(exitval); 2498} 2499 2500 2501/* 2502 * fill the interface structure. We do not check the name as we can 2503 * create interfaces dynamically, so checking them at insert time 2504 * makes relatively little sense. 2505 * Interface names containing '*', '?', or '[' are assumed to be shell 2506 * patterns which match interfaces. 2507 */ 2508static void 2509fill_iface(ipfw_insn_if *cmd, char *arg, int cblen) 2510{ 2511 cmd->name[0] = '\0'; 2512 cmd->o.len |= F_INSN_SIZE(ipfw_insn_if); 2513 2514 CHECK_CMDLEN; 2515 2516 /* Parse the interface or address */ 2517 if (strcmp(arg, "any") == 0) 2518 cmd->o.len = 0; /* effectively ignore this command */ 2519 else if (strncmp(arg, "table(", 6) == 0) { 2520 char *p = strchr(arg + 6, ','); 2521 if (p) 2522 *p++ = '\0'; 2523 cmd->name[0] = '\1'; /* Special value indicating table */ 2524 cmd->p.glob = strtoul(arg + 6, NULL, 0); 2525 } else if (!isdigit(*arg)) { 2526 strlcpy(cmd->name, arg, sizeof(cmd->name)); 2527 cmd->p.glob = strpbrk(arg, "*?[") != NULL ? 1 : 0; 2528 } else if (!inet_aton(arg, &cmd->p.ip)) 2529 errx(EX_DATAERR, "bad ip address ``%s''", arg); 2530} 2531 2532static void 2533get_mac_addr_mask(const char *p, uint8_t *addr, uint8_t *mask) 2534{ 2535 int i; 2536 size_t l; 2537 char *ap, *ptr, *optr; 2538 struct ether_addr *mac; 2539 const char *macset = "0123456789abcdefABCDEF:"; 2540 2541 if (strcmp(p, "any") == 0) { 2542 for (i = 0; i < ETHER_ADDR_LEN; i++) 2543 addr[i] = mask[i] = 0; 2544 return; 2545 } 2546 2547 optr = ptr = strdup(p); 2548 if ((ap = strsep(&ptr, "&/")) != NULL && *ap != 0) { 2549 l = strlen(ap); 2550 if (strspn(ap, macset) != l || (mac = ether_aton(ap)) == NULL) 2551 errx(EX_DATAERR, "Incorrect MAC address"); 2552 bcopy(mac, addr, ETHER_ADDR_LEN); 2553 } else 2554 errx(EX_DATAERR, "Incorrect MAC address"); 2555 2556 if (ptr != NULL) { /* we have mask? */ 2557 if (p[ptr - optr - 1] == '/') { /* mask len */ 2558 long ml = strtol(ptr, &ap, 10); 2559 if (*ap != 0 || ml > ETHER_ADDR_LEN * 8 || ml < 0) 2560 errx(EX_DATAERR, "Incorrect mask length"); 2561 for (i = 0; ml > 0 && i < ETHER_ADDR_LEN; ml -= 8, i++) 2562 mask[i] = (ml >= 8) ? 0xff: (~0) << (8 - ml); 2563 } else { /* mask */ 2564 l = strlen(ptr); 2565 if (strspn(ptr, macset) != l || 2566 (mac = ether_aton(ptr)) == NULL) 2567 errx(EX_DATAERR, "Incorrect mask"); 2568 bcopy(mac, mask, ETHER_ADDR_LEN); 2569 } 2570 } else { /* default mask: ff:ff:ff:ff:ff:ff */ 2571 for (i = 0; i < ETHER_ADDR_LEN; i++) 2572 mask[i] = 0xff; 2573 } 2574 for (i = 0; i < ETHER_ADDR_LEN; i++) 2575 addr[i] &= mask[i]; 2576 2577 free(optr); 2578} 2579 2580/* 2581 * helper function, updates the pointer to cmd with the length 2582 * of the current command, and also cleans up the first word of 2583 * the new command in case it has been clobbered before. 2584 */ 2585static ipfw_insn * 2586next_cmd(ipfw_insn *cmd, int *len) 2587{ 2588 *len -= F_LEN(cmd); 2589 CHECK_LENGTH(*len, 0); 2590 cmd += F_LEN(cmd); 2591 bzero(cmd, sizeof(*cmd)); 2592 return cmd; 2593} 2594 2595/* 2596 * Takes arguments and copies them into a comment 2597 */ 2598static void 2599fill_comment(ipfw_insn *cmd, char **av, int cblen) 2600{ 2601 int i, l; 2602 char *p = (char *)(cmd + 1); 2603 2604 cmd->opcode = O_NOP; 2605 cmd->len = (cmd->len & (F_NOT | F_OR)); 2606 2607 /* Compute length of comment string. */ 2608 for (i = 0, l = 0; av[i] != NULL; i++) 2609 l += strlen(av[i]) + 1; 2610 if (l == 0) 2611 return; 2612 if (l > 84) 2613 errx(EX_DATAERR, 2614 "comment too long (max 80 chars)"); 2615 l = 1 + (l+3)/4; 2616 cmd->len = (cmd->len & (F_NOT | F_OR)) | l; 2617 CHECK_CMDLEN; 2618 2619 for (i = 0; av[i] != NULL; i++) { 2620 strcpy(p, av[i]); 2621 p += strlen(av[i]); 2622 *p++ = ' '; 2623 } 2624 *(--p) = '\0'; 2625} 2626 2627/* 2628 * A function to fill simple commands of size 1. 2629 * Existing flags are preserved. 2630 */ 2631static void 2632fill_cmd(ipfw_insn *cmd, enum ipfw_opcodes opcode, int flags, uint16_t arg) 2633{ 2634 cmd->opcode = opcode; 2635 cmd->len = ((cmd->len | flags) & (F_NOT | F_OR)) | 1; 2636 cmd->arg1 = arg; 2637} 2638 2639/* 2640 * Fetch and add the MAC address and type, with masks. This generates one or 2641 * two microinstructions, and returns the pointer to the last one. 2642 */ 2643static ipfw_insn * 2644add_mac(ipfw_insn *cmd, char *av[], int cblen) 2645{ 2646 ipfw_insn_mac *mac; 2647 2648 if ( ( av[0] == NULL ) || ( av[1] == NULL ) ) 2649 errx(EX_DATAERR, "MAC dst src"); 2650 2651 cmd->opcode = O_MACADDR2; 2652 cmd->len = (cmd->len & (F_NOT | F_OR)) | F_INSN_SIZE(ipfw_insn_mac); 2653 CHECK_CMDLEN; 2654 2655 mac = (ipfw_insn_mac *)cmd; 2656 get_mac_addr_mask(av[0], mac->addr, mac->mask); /* dst */ 2657 get_mac_addr_mask(av[1], &(mac->addr[ETHER_ADDR_LEN]), 2658 &(mac->mask[ETHER_ADDR_LEN])); /* src */ 2659 return cmd; 2660} 2661 2662static ipfw_insn * 2663add_mactype(ipfw_insn *cmd, char *av, int cblen) 2664{ 2665 if (!av) 2666 errx(EX_DATAERR, "missing MAC type"); 2667 if (strcmp(av, "any") != 0) { /* we have a non-null type */ 2668 fill_newports((ipfw_insn_u16 *)cmd, av, IPPROTO_ETHERTYPE, 2669 cblen); 2670 cmd->opcode = O_MAC_TYPE; 2671 return cmd; 2672 } else 2673 return NULL; 2674} 2675 2676static ipfw_insn * 2677add_proto0(ipfw_insn *cmd, char *av, u_char *protop) 2678{ 2679 struct protoent *pe; 2680 char *ep; 2681 int proto; 2682 2683 proto = strtol(av, &ep, 10); 2684 if (*ep != '\0' || proto <= 0) { 2685 if ((pe = getprotobyname(av)) == NULL) 2686 return NULL; 2687 proto = pe->p_proto; 2688 } 2689 2690 fill_cmd(cmd, O_PROTO, 0, proto); 2691 *protop = proto; 2692 return cmd; 2693} 2694 2695static ipfw_insn * 2696add_proto(ipfw_insn *cmd, char *av, u_char *protop) 2697{ 2698 u_char proto = IPPROTO_IP; 2699 2700 if (_substrcmp(av, "all") == 0 || strcmp(av, "ip") == 0) 2701 ; /* do not set O_IP4 nor O_IP6 */ 2702 else if (strcmp(av, "ip4") == 0) 2703 /* explicit "just IPv4" rule */ 2704 fill_cmd(cmd, O_IP4, 0, 0); 2705 else if (strcmp(av, "ip6") == 0) { 2706 /* explicit "just IPv6" rule */ 2707 proto = IPPROTO_IPV6; 2708 fill_cmd(cmd, O_IP6, 0, 0); 2709 } else 2710 return add_proto0(cmd, av, protop); 2711 2712 *protop = proto; 2713 return cmd; 2714} 2715 2716static ipfw_insn * 2717add_proto_compat(ipfw_insn *cmd, char *av, u_char *protop) 2718{ 2719 u_char proto = IPPROTO_IP; 2720 2721 if (_substrcmp(av, "all") == 0 || strcmp(av, "ip") == 0) 2722 ; /* do not set O_IP4 nor O_IP6 */ 2723 else if (strcmp(av, "ipv4") == 0 || strcmp(av, "ip4") == 0) 2724 /* explicit "just IPv4" rule */ 2725 fill_cmd(cmd, O_IP4, 0, 0); 2726 else if (strcmp(av, "ipv6") == 0 || strcmp(av, "ip6") == 0) { 2727 /* explicit "just IPv6" rule */ 2728 proto = IPPROTO_IPV6; 2729 fill_cmd(cmd, O_IP6, 0, 0); 2730 } else 2731 return add_proto0(cmd, av, protop); 2732 2733 *protop = proto; 2734 return cmd; 2735} 2736 2737static ipfw_insn * 2738add_srcip(ipfw_insn *cmd, char *av, int cblen) 2739{ 2740 fill_ip((ipfw_insn_ip *)cmd, av, cblen); 2741 if (cmd->opcode == O_IP_DST_SET) /* set */ 2742 cmd->opcode = O_IP_SRC_SET; 2743 else if (cmd->opcode == O_IP_DST_LOOKUP) /* table */ 2744 cmd->opcode = O_IP_SRC_LOOKUP; 2745 else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn)) /* me */ 2746 cmd->opcode = O_IP_SRC_ME; 2747 else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn_u32)) /* one IP */ 2748 cmd->opcode = O_IP_SRC; 2749 else /* addr/mask */ 2750 cmd->opcode = O_IP_SRC_MASK; 2751 return cmd; 2752} 2753 2754static ipfw_insn * 2755add_dstip(ipfw_insn *cmd, char *av, int cblen) 2756{ 2757 fill_ip((ipfw_insn_ip *)cmd, av, cblen); 2758 if (cmd->opcode == O_IP_DST_SET) /* set */ 2759 ; 2760 else if (cmd->opcode == O_IP_DST_LOOKUP) /* table */ 2761 ; 2762 else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn)) /* me */ 2763 cmd->opcode = O_IP_DST_ME; 2764 else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn_u32)) /* one IP */ 2765 cmd->opcode = O_IP_DST; 2766 else /* addr/mask */ 2767 cmd->opcode = O_IP_DST_MASK; 2768 return cmd; 2769} 2770 2771static ipfw_insn * 2772add_ports(ipfw_insn *cmd, char *av, u_char proto, int opcode, int cblen) 2773{ 2774 /* XXX "any" is trapped before. Perhaps "to" */ 2775 if (_substrcmp(av, "any") == 0) { 2776 return NULL; 2777 } else if (fill_newports((ipfw_insn_u16 *)cmd, av, proto, cblen)) { 2778 /* XXX todo: check that we have a protocol with ports */ 2779 cmd->opcode = opcode; 2780 return cmd; 2781 } 2782 return NULL; 2783} 2784 2785static ipfw_insn * 2786add_src(ipfw_insn *cmd, char *av, u_char proto, int cblen) 2787{ 2788 struct in6_addr a; 2789 char *host, *ch, buf[INET6_ADDRSTRLEN]; 2790 ipfw_insn *ret = NULL; 2791 int len; 2792 2793 /* Copy first address in set if needed */ 2794 if ((ch = strpbrk(av, "/,")) != NULL) { 2795 len = ch - av; 2796 strlcpy(buf, av, sizeof(buf)); 2797 if (len < sizeof(buf)) 2798 buf[len] = '\0'; 2799 host = buf; 2800 } else 2801 host = av; 2802 2803 if (proto == IPPROTO_IPV6 || strcmp(av, "me6") == 0 || 2804 inet_pton(AF_INET6, host, &a) == 1) 2805 ret = add_srcip6(cmd, av, cblen); 2806 /* XXX: should check for IPv4, not !IPv6 */ 2807 if (ret == NULL && (proto == IPPROTO_IP || strcmp(av, "me") == 0 || 2808 inet_pton(AF_INET6, host, &a) != 1)) 2809 ret = add_srcip(cmd, av, cblen); 2810 if (ret == NULL && strcmp(av, "any") != 0) 2811 ret = cmd; 2812 2813 return ret; 2814} 2815 2816static ipfw_insn * 2817add_dst(ipfw_insn *cmd, char *av, u_char proto, int cblen) 2818{ 2819 struct in6_addr a; 2820 char *host, *ch, buf[INET6_ADDRSTRLEN]; 2821 ipfw_insn *ret = NULL; 2822 int len; 2823 2824 /* Copy first address in set if needed */ 2825 if ((ch = strpbrk(av, "/,")) != NULL) { 2826 len = ch - av; 2827 strlcpy(buf, av, sizeof(buf)); 2828 if (len < sizeof(buf)) 2829 buf[len] = '\0'; 2830 host = buf; 2831 } else 2832 host = av; 2833 2834 if (proto == IPPROTO_IPV6 || strcmp(av, "me6") == 0 || 2835 inet_pton(AF_INET6, host, &a) == 1) 2836 ret = add_dstip6(cmd, av, cblen); 2837 /* XXX: should check for IPv4, not !IPv6 */ 2838 if (ret == NULL && (proto == IPPROTO_IP || strcmp(av, "me") == 0 || 2839 inet_pton(AF_INET6, host, &a) != 1)) 2840 ret = add_dstip(cmd, av, cblen); 2841 if (ret == NULL && strcmp(av, "any") != 0) 2842 ret = cmd; 2843 2844 return ret; 2845} 2846 2847/* 2848 * Parse arguments and assemble the microinstructions which make up a rule. 2849 * Rules are added into the 'rulebuf' and then copied in the correct order 2850 * into the actual rule. 2851 * 2852 * The syntax for a rule starts with the action, followed by 2853 * optional action parameters, and the various match patterns. 2854 * In the assembled microcode, the first opcode must be an O_PROBE_STATE 2855 * (generated if the rule includes a keep-state option), then the 2856 * various match patterns, log/altq actions, and the actual action. 2857 * 2858 */ 2859void 2860ipfw_add(char *av[]) 2861{ 2862 /* 2863 * rules are added into the 'rulebuf' and then copied in 2864 * the correct order into the actual rule. 2865 * Some things that need to go out of order (prob, action etc.) 2866 * go into actbuf[]. 2867 */ 2868 static uint32_t rulebuf[255], actbuf[255], cmdbuf[255]; 2869 int rblen, ablen, cblen; 2870 2871 ipfw_insn *src, *dst, *cmd, *action, *prev=NULL; 2872 ipfw_insn *first_cmd; /* first match pattern */ 2873 2874 struct ip_fw *rule; 2875 2876 /* 2877 * various flags used to record that we entered some fields. 2878 */ 2879 ipfw_insn *have_state = NULL; /* check-state or keep-state */ 2880 ipfw_insn *have_log = NULL, *have_altq = NULL, *have_tag = NULL; 2881 size_t len; 2882 2883 int i; 2884 2885 int open_par = 0; /* open parenthesis ( */ 2886 2887 /* proto is here because it is used to fetch ports */ 2888 u_char proto = IPPROTO_IP; /* default protocol */ 2889 2890 double match_prob = 1; /* match probability, default is always match */ 2891 2892 bzero(actbuf, sizeof(actbuf)); /* actions go here */ 2893 bzero(cmdbuf, sizeof(cmdbuf)); 2894 bzero(rulebuf, sizeof(rulebuf)); 2895 2896 rule = (struct ip_fw *)rulebuf; 2897 cmd = (ipfw_insn *)cmdbuf; 2898 action = (ipfw_insn *)actbuf; 2899 2900 rblen = sizeof(rulebuf) / sizeof(rulebuf[0]); 2901 rblen -= offsetof(struct ip_fw, cmd) / sizeof(rulebuf[0]); 2902 ablen = sizeof(actbuf) / sizeof(actbuf[0]); 2903 cblen = sizeof(cmdbuf) / sizeof(cmdbuf[0]); 2904 cblen -= F_INSN_SIZE(ipfw_insn_u32) + 1; 2905 2906#define CHECK_RBUFLEN(len) { CHECK_LENGTH(rblen, len); rblen -= len; } 2907#define CHECK_ACTLEN CHECK_LENGTH(ablen, action->len) 2908 2909 av++; 2910 2911 /* [rule N] -- Rule number optional */ 2912 if (av[0] && isdigit(**av)) { 2913 rule->rulenum = atoi(*av); 2914 av++; 2915 } 2916 2917 /* [set N] -- set number (0..RESVD_SET), optional */ 2918 if (av[0] && av[1] && _substrcmp(*av, "set") == 0) { 2919 int set = strtoul(av[1], NULL, 10); 2920 if (set < 0 || set > RESVD_SET) 2921 errx(EX_DATAERR, "illegal set %s", av[1]); 2922 rule->set = set; 2923 av += 2; 2924 } 2925 2926 /* [prob D] -- match probability, optional */ 2927 if (av[0] && av[1] && _substrcmp(*av, "prob") == 0) { 2928 match_prob = strtod(av[1], NULL); 2929 2930 if (match_prob <= 0 || match_prob > 1) 2931 errx(EX_DATAERR, "illegal match prob. %s", av[1]); 2932 av += 2; 2933 } 2934 2935 /* action -- mandatory */ 2936 NEED1("missing action"); 2937 i = match_token(rule_actions, *av); 2938 av++; 2939 action->len = 1; /* default */ 2940 CHECK_ACTLEN; 2941 switch(i) { 2942 case TOK_CHECKSTATE: 2943 have_state = action; 2944 action->opcode = O_CHECK_STATE; 2945 break; 2946 2947 case TOK_ACCEPT: 2948 action->opcode = O_ACCEPT; 2949 break; 2950 2951 case TOK_DENY: 2952 action->opcode = O_DENY; 2953 action->arg1 = 0; 2954 break; 2955 2956 case TOK_REJECT: 2957 action->opcode = O_REJECT; 2958 action->arg1 = ICMP_UNREACH_HOST; 2959 break; 2960 2961 case TOK_RESET: 2962 action->opcode = O_REJECT; 2963 action->arg1 = ICMP_REJECT_RST; 2964 break; 2965 2966 case TOK_RESET6: 2967 action->opcode = O_UNREACH6; 2968 action->arg1 = ICMP6_UNREACH_RST; 2969 break; 2970 2971 case TOK_UNREACH: 2972 action->opcode = O_REJECT; 2973 NEED1("missing reject code"); 2974 fill_reject_code(&action->arg1, *av); 2975 av++; 2976 break; 2977 2978 case TOK_UNREACH6: 2979 action->opcode = O_UNREACH6; 2980 NEED1("missing unreach code"); 2981 fill_unreach6_code(&action->arg1, *av); 2982 av++; 2983 break; 2984 2985 case TOK_COUNT: 2986 action->opcode = O_COUNT; 2987 break; 2988 2989 case TOK_NAT: 2990 action->opcode = O_NAT; 2991 action->len = F_INSN_SIZE(ipfw_insn_nat); 2992 CHECK_ACTLEN; 2993 if (_substrcmp(*av, "global") == 0) { 2994 action->arg1 = 0; 2995 av++; 2996 break; 2997 } else 2998 goto chkarg; 2999 3000 case TOK_QUEUE: 3001 action->opcode = O_QUEUE; 3002 goto chkarg; 3003 case TOK_PIPE: 3004 action->opcode = O_PIPE; 3005 goto chkarg; 3006 case TOK_SKIPTO: 3007 action->opcode = O_SKIPTO; 3008 goto chkarg; 3009 case TOK_NETGRAPH: 3010 action->opcode = O_NETGRAPH; 3011 goto chkarg; 3012 case TOK_NGTEE: 3013 action->opcode = O_NGTEE; 3014 goto chkarg; 3015 case TOK_DIVERT: 3016 action->opcode = O_DIVERT; 3017 goto chkarg; 3018 case TOK_TEE: 3019 action->opcode = O_TEE; 3020 goto chkarg; 3021 case TOK_CALL: 3022 action->opcode = O_CALLRETURN; 3023chkarg: 3024 if (!av[0]) 3025 errx(EX_USAGE, "missing argument for %s", *(av - 1)); 3026 if (isdigit(**av)) { 3027 action->arg1 = strtoul(*av, NULL, 10); 3028 if (action->arg1 <= 0 || action->arg1 >= IP_FW_TABLEARG) 3029 errx(EX_DATAERR, "illegal argument for %s", 3030 *(av - 1)); 3031 } else if (_substrcmp(*av, "tablearg") == 0) { 3032 action->arg1 = IP_FW_TABLEARG; 3033 } else if (i == TOK_DIVERT || i == TOK_TEE) { 3034 struct servent *s; 3035 setservent(1); 3036 s = getservbyname(av[0], "divert"); 3037 if (s != NULL) 3038 action->arg1 = ntohs(s->s_port); 3039 else 3040 errx(EX_DATAERR, "illegal divert/tee port"); 3041 } else 3042 errx(EX_DATAERR, "illegal argument for %s", *(av - 1)); 3043 av++; 3044 break; 3045 3046 case TOK_FORWARD: { 3047 /* 3048 * Locate the address-port separator (':' or ','). 3049 * Could be one of the following: 3050 * hostname:port 3051 * IPv4 a.b.c.d,port 3052 * IPv4 a.b.c.d:port 3053 * IPv6 w:x:y::z,port 3054 * The ':' can only be used with hostname and IPv4 address. 3055 * XXX-BZ Should we also support [w:x:y::z]:port? 3056 */ 3057 struct sockaddr_storage result; 3058 struct addrinfo *res; 3059 char *s, *end; 3060 int family; 3061 u_short port_number; 3062 3063 NEED1("missing forward address[:port]"); 3064 3065 /* 3066 * locate the address-port separator (':' or ',') 3067 */ 3068 s = strchr(*av, ','); 3069 if (s == NULL) { 3070 /* Distinguish between IPv4:port and IPv6 cases. */ 3071 s = strchr(*av, ':'); 3072 if (s && strchr(s+1, ':')) 3073 s = NULL; /* no port */ 3074 } 3075 3076 port_number = 0; 3077 if (s != NULL) { 3078 /* Terminate host portion and set s to start of port. */ 3079 *(s++) = '\0'; 3080 i = strtoport(s, &end, 0 /* base */, 0 /* proto */); 3081 if (s == end) 3082 errx(EX_DATAERR, 3083 "illegal forwarding port ``%s''", s); 3084 port_number = (u_short)i; 3085 } 3086 3087 if (_substrcmp(*av, "tablearg") == 0) { 3088 family = PF_INET; 3089 ((struct sockaddr_in*)&result)->sin_addr.s_addr = 3090 INADDR_ANY; 3091 } else { 3092 /* 3093 * Resolve the host name or address to a family and a 3094 * network representation of the address. 3095 */ 3096 if (getaddrinfo(*av, NULL, NULL, &res)) 3097 errx(EX_DATAERR, NULL); 3098 /* Just use the first host in the answer. */ 3099 family = res->ai_family; 3100 memcpy(&result, res->ai_addr, res->ai_addrlen); 3101 freeaddrinfo(res); 3102 } 3103 3104 if (family == PF_INET) { 3105 ipfw_insn_sa *p = (ipfw_insn_sa *)action; 3106 3107 action->opcode = O_FORWARD_IP; 3108 action->len = F_INSN_SIZE(ipfw_insn_sa); 3109 CHECK_ACTLEN; 3110 3111 /* 3112 * In the kernel we assume AF_INET and use only 3113 * sin_port and sin_addr. Remember to set sin_len as 3114 * the routing code seems to use it too. 3115 */ 3116 p->sa.sin_len = sizeof(struct sockaddr_in); 3117 p->sa.sin_family = AF_INET; 3118 p->sa.sin_port = port_number; 3119 p->sa.sin_addr.s_addr = 3120 ((struct sockaddr_in *)&result)->sin_addr.s_addr; 3121 } else if (family == PF_INET6) { 3122 ipfw_insn_sa6 *p = (ipfw_insn_sa6 *)action; 3123 3124 action->opcode = O_FORWARD_IP6; 3125 action->len = F_INSN_SIZE(ipfw_insn_sa6); 3126 CHECK_ACTLEN; 3127 3128 p->sa.sin6_len = sizeof(struct sockaddr_in6); 3129 p->sa.sin6_family = AF_INET6; 3130 p->sa.sin6_port = port_number; 3131 p->sa.sin6_flowinfo = 0; 3132 p->sa.sin6_scope_id = 0; 3133 /* No table support for v6 yet. */ 3134 bcopy(&((struct sockaddr_in6*)&result)->sin6_addr, 3135 &p->sa.sin6_addr, sizeof(p->sa.sin6_addr)); 3136 } else { 3137 errx(EX_DATAERR, "Invalid address family in forward action"); 3138 } 3139 av++; 3140 break; 3141 } 3142 case TOK_COMMENT: 3143 /* pretend it is a 'count' rule followed by the comment */ 3144 action->opcode = O_COUNT; 3145 av--; /* go back... */ 3146 break; 3147 3148 case TOK_SETFIB: 3149 { 3150 int numfibs; 3151 size_t intsize = sizeof(int); 3152 3153 action->opcode = O_SETFIB; 3154 NEED1("missing fib number"); 3155 if (_substrcmp(*av, "tablearg") == 0) { 3156 action->arg1 = IP_FW_TABLEARG; 3157 } else { 3158 action->arg1 = strtoul(*av, NULL, 10); 3159 if (sysctlbyname("net.fibs", &numfibs, &intsize, 3160 NULL, 0) == -1) 3161 errx(EX_DATAERR, "fibs not suported.\n"); 3162 if (action->arg1 >= numfibs) /* Temporary */ 3163 errx(EX_DATAERR, "fib too large.\n"); 3164 } 3165 av++; 3166 break; 3167 } 3168 3169 case TOK_SETDSCP: 3170 { 3171 int code; 3172 3173 action->opcode = O_SETDSCP; 3174 NEED1("missing DSCP code"); 3175 if (_substrcmp(*av, "tablearg") == 0) { 3176 action->arg1 = IP_FW_TABLEARG; 3177 } else if (isalpha(*av[0])) { 3178 if ((code = match_token(f_ipdscp, *av)) == -1) 3179 errx(EX_DATAERR, "Unknown DSCP code"); 3180 action->arg1 = code; 3181 } else 3182 action->arg1 = strtoul(*av, NULL, 10); 3183 av++; 3184 break; 3185 } 3186 3187 case TOK_REASS: 3188 action->opcode = O_REASS; 3189 break; 3190 3191 case TOK_RETURN: 3192 fill_cmd(action, O_CALLRETURN, F_NOT, 0); 3193 break; 3194 3195 default: 3196 errx(EX_DATAERR, "invalid action %s\n", av[-1]); 3197 } 3198 action = next_cmd(action, &ablen); 3199 3200 /* 3201 * [altq queuename] -- altq tag, optional 3202 * [log [logamount N]] -- log, optional 3203 * 3204 * If they exist, it go first in the cmdbuf, but then it is 3205 * skipped in the copy section to the end of the buffer. 3206 */ 3207 while (av[0] != NULL && (i = match_token(rule_action_params, *av)) != -1) { 3208 av++; 3209 switch (i) { 3210 case TOK_LOG: 3211 { 3212 ipfw_insn_log *c = (ipfw_insn_log *)cmd; 3213 int l; 3214 3215 if (have_log) 3216 errx(EX_DATAERR, 3217 "log cannot be specified more than once"); 3218 have_log = (ipfw_insn *)c; 3219 cmd->len = F_INSN_SIZE(ipfw_insn_log); 3220 CHECK_CMDLEN; 3221 cmd->opcode = O_LOG; 3222 if (av[0] && _substrcmp(*av, "logamount") == 0) { 3223 av++; 3224 NEED1("logamount requires argument"); 3225 l = atoi(*av); 3226 if (l < 0) 3227 errx(EX_DATAERR, 3228 "logamount must be positive"); 3229 c->max_log = l; 3230 av++; 3231 } else { 3232 len = sizeof(c->max_log); 3233 if (sysctlbyname("net.inet.ip.fw.verbose_limit", 3234 &c->max_log, &len, NULL, 0) == -1) { 3235 if (co.test_only) { 3236 c->max_log = 0; 3237 break; 3238 } 3239 errx(1, "sysctlbyname(\"%s\")", 3240 "net.inet.ip.fw.verbose_limit"); 3241 } 3242 } 3243 } 3244 break; 3245 3246#ifndef NO_ALTQ 3247 case TOK_ALTQ: 3248 { 3249 ipfw_insn_altq *a = (ipfw_insn_altq *)cmd; 3250 3251 NEED1("missing altq queue name"); 3252 if (have_altq) 3253 errx(EX_DATAERR, 3254 "altq cannot be specified more than once"); 3255 have_altq = (ipfw_insn *)a; 3256 cmd->len = F_INSN_SIZE(ipfw_insn_altq); 3257 CHECK_CMDLEN; 3258 cmd->opcode = O_ALTQ; 3259 a->qid = altq_name_to_qid(*av); 3260 av++; 3261 } 3262 break; 3263#endif 3264 3265 case TOK_TAG: 3266 case TOK_UNTAG: { 3267 uint16_t tag; 3268 3269 if (have_tag) 3270 errx(EX_USAGE, "tag and untag cannot be " 3271 "specified more than once"); 3272 GET_UINT_ARG(tag, IPFW_ARG_MIN, IPFW_ARG_MAX, i, 3273 rule_action_params); 3274 have_tag = cmd; 3275 fill_cmd(cmd, O_TAG, (i == TOK_TAG) ? 0: F_NOT, tag); 3276 av++; 3277 break; 3278 } 3279 3280 default: 3281 abort(); 3282 } 3283 cmd = next_cmd(cmd, &cblen); 3284 } 3285 3286 if (have_state) /* must be a check-state, we are done */ 3287 goto done; 3288 3289#define OR_START(target) \ 3290 if (av[0] && (*av[0] == '(' || *av[0] == '{')) { \ 3291 if (open_par) \ 3292 errx(EX_USAGE, "nested \"(\" not allowed\n"); \ 3293 prev = NULL; \ 3294 open_par = 1; \ 3295 if ( (av[0])[1] == '\0') { \ 3296 av++; \ 3297 } else \ 3298 (*av)++; \ 3299 } \ 3300 target: \ 3301 3302 3303#define CLOSE_PAR \ 3304 if (open_par) { \ 3305 if (av[0] && ( \ 3306 strcmp(*av, ")") == 0 || \ 3307 strcmp(*av, "}") == 0)) { \ 3308 prev = NULL; \ 3309 open_par = 0; \ 3310 av++; \ 3311 } else \ 3312 errx(EX_USAGE, "missing \")\"\n"); \ 3313 } 3314 3315#define NOT_BLOCK \ 3316 if (av[0] && _substrcmp(*av, "not") == 0) { \ 3317 if (cmd->len & F_NOT) \ 3318 errx(EX_USAGE, "double \"not\" not allowed\n"); \ 3319 cmd->len |= F_NOT; \ 3320 av++; \ 3321 } 3322 3323#define OR_BLOCK(target) \ 3324 if (av[0] && _substrcmp(*av, "or") == 0) { \ 3325 if (prev == NULL || open_par == 0) \ 3326 errx(EX_DATAERR, "invalid OR block"); \ 3327 prev->len |= F_OR; \ 3328 av++; \ 3329 goto target; \ 3330 } \ 3331 CLOSE_PAR; 3332 3333 first_cmd = cmd; 3334 3335#if 0 3336 /* 3337 * MAC addresses, optional. 3338 * If we have this, we skip the part "proto from src to dst" 3339 * and jump straight to the option parsing. 3340 */ 3341 NOT_BLOCK; 3342 NEED1("missing protocol"); 3343 if (_substrcmp(*av, "MAC") == 0 || 3344 _substrcmp(*av, "mac") == 0) { 3345 av++; /* the "MAC" keyword */ 3346 add_mac(cmd, av); /* exits in case of errors */ 3347 cmd = next_cmd(cmd); 3348 av += 2; /* dst-mac and src-mac */ 3349 NOT_BLOCK; 3350 NEED1("missing mac type"); 3351 if (add_mactype(cmd, av[0])) 3352 cmd = next_cmd(cmd); 3353 av++; /* any or mac-type */ 3354 goto read_options; 3355 } 3356#endif 3357 3358 /* 3359 * protocol, mandatory 3360 */ 3361 OR_START(get_proto); 3362 NOT_BLOCK; 3363 NEED1("missing protocol"); 3364 if (add_proto_compat(cmd, *av, &proto)) { 3365 av++; 3366 if (F_LEN(cmd) != 0) { 3367 prev = cmd; 3368 cmd = next_cmd(cmd, &cblen); 3369 } 3370 } else if (first_cmd != cmd) { 3371 errx(EX_DATAERR, "invalid protocol ``%s''", *av); 3372 } else 3373 goto read_options; 3374 OR_BLOCK(get_proto); 3375 3376 /* 3377 * "from", mandatory 3378 */ 3379 if ((av[0] == NULL) || _substrcmp(*av, "from") != 0) 3380 errx(EX_USAGE, "missing ``from''"); 3381 av++; 3382 3383 /* 3384 * source IP, mandatory 3385 */ 3386 OR_START(source_ip); 3387 NOT_BLOCK; /* optional "not" */ 3388 NEED1("missing source address"); 3389 if (add_src(cmd, *av, proto, cblen)) { 3390 av++; 3391 if (F_LEN(cmd) != 0) { /* ! any */ 3392 prev = cmd; 3393 cmd = next_cmd(cmd, &cblen); 3394 } 3395 } else 3396 errx(EX_USAGE, "bad source address %s", *av); 3397 OR_BLOCK(source_ip); 3398 3399 /* 3400 * source ports, optional 3401 */ 3402 NOT_BLOCK; /* optional "not" */ 3403 if ( av[0] != NULL ) { 3404 if (_substrcmp(*av, "any") == 0 || 3405 add_ports(cmd, *av, proto, O_IP_SRCPORT, cblen)) { 3406 av++; 3407 if (F_LEN(cmd) != 0) 3408 cmd = next_cmd(cmd, &cblen); 3409 } 3410 } 3411 3412 /* 3413 * "to", mandatory 3414 */ 3415 if ( (av[0] == NULL) || _substrcmp(*av, "to") != 0 ) 3416 errx(EX_USAGE, "missing ``to''"); 3417 av++; 3418 3419 /* 3420 * destination, mandatory 3421 */ 3422 OR_START(dest_ip); 3423 NOT_BLOCK; /* optional "not" */ 3424 NEED1("missing dst address"); 3425 if (add_dst(cmd, *av, proto, cblen)) { 3426 av++; 3427 if (F_LEN(cmd) != 0) { /* ! any */ 3428 prev = cmd; 3429 cmd = next_cmd(cmd, &cblen); 3430 } 3431 } else 3432 errx( EX_USAGE, "bad destination address %s", *av); 3433 OR_BLOCK(dest_ip); 3434 3435 /* 3436 * dest. ports, optional 3437 */ 3438 NOT_BLOCK; /* optional "not" */ 3439 if (av[0]) { 3440 if (_substrcmp(*av, "any") == 0 || 3441 add_ports(cmd, *av, proto, O_IP_DSTPORT, cblen)) { 3442 av++; 3443 if (F_LEN(cmd) != 0) 3444 cmd = next_cmd(cmd, &cblen); 3445 } 3446 } 3447 3448read_options: 3449 if (av[0] && first_cmd == cmd) { 3450 /* 3451 * nothing specified so far, store in the rule to ease 3452 * printout later. 3453 */ 3454 rule->_pad = 1; 3455 } 3456 prev = NULL; 3457 while ( av[0] != NULL ) { 3458 char *s; 3459 ipfw_insn_u32 *cmd32; /* alias for cmd */ 3460 3461 s = *av; 3462 cmd32 = (ipfw_insn_u32 *)cmd; 3463 3464 if (*s == '!') { /* alternate syntax for NOT */ 3465 if (cmd->len & F_NOT) 3466 errx(EX_USAGE, "double \"not\" not allowed\n"); 3467 cmd->len = F_NOT; 3468 s++; 3469 } 3470 i = match_token(rule_options, s); 3471 av++; 3472 switch(i) { 3473 case TOK_NOT: 3474 if (cmd->len & F_NOT) 3475 errx(EX_USAGE, "double \"not\" not allowed\n"); 3476 cmd->len = F_NOT; 3477 break; 3478 3479 case TOK_OR: 3480 if (open_par == 0 || prev == NULL) 3481 errx(EX_USAGE, "invalid \"or\" block\n"); 3482 prev->len |= F_OR; 3483 break; 3484 3485 case TOK_STARTBRACE: 3486 if (open_par) 3487 errx(EX_USAGE, "+nested \"(\" not allowed\n"); 3488 open_par = 1; 3489 break; 3490 3491 case TOK_ENDBRACE: 3492 if (!open_par) 3493 errx(EX_USAGE, "+missing \")\"\n"); 3494 open_par = 0; 3495 prev = NULL; 3496 break; 3497 3498 case TOK_IN: 3499 fill_cmd(cmd, O_IN, 0, 0); 3500 break; 3501 3502 case TOK_OUT: 3503 cmd->len ^= F_NOT; /* toggle F_NOT */ 3504 fill_cmd(cmd, O_IN, 0, 0); 3505 break; 3506 3507 case TOK_DIVERTED: 3508 fill_cmd(cmd, O_DIVERTED, 0, 3); 3509 break; 3510 3511 case TOK_DIVERTEDLOOPBACK: 3512 fill_cmd(cmd, O_DIVERTED, 0, 1); 3513 break; 3514 3515 case TOK_DIVERTEDOUTPUT: 3516 fill_cmd(cmd, O_DIVERTED, 0, 2); 3517 break; 3518 3519 case TOK_FRAG: 3520 fill_cmd(cmd, O_FRAG, 0, 0); 3521 break; 3522 3523 case TOK_LAYER2: 3524 fill_cmd(cmd, O_LAYER2, 0, 0); 3525 break; 3526 3527 case TOK_XMIT: 3528 case TOK_RECV: 3529 case TOK_VIA: 3530 NEED1("recv, xmit, via require interface name" 3531 " or address"); 3532 fill_iface((ipfw_insn_if *)cmd, av[0], cblen); 3533 av++; 3534 if (F_LEN(cmd) == 0) /* not a valid address */ 3535 break; 3536 if (i == TOK_XMIT) 3537 cmd->opcode = O_XMIT; 3538 else if (i == TOK_RECV) 3539 cmd->opcode = O_RECV; 3540 else if (i == TOK_VIA) 3541 cmd->opcode = O_VIA; 3542 break; 3543 3544 case TOK_ICMPTYPES: 3545 NEED1("icmptypes requires list of types"); 3546 fill_icmptypes((ipfw_insn_u32 *)cmd, *av); 3547 av++; 3548 break; 3549 3550 case TOK_ICMP6TYPES: 3551 NEED1("icmptypes requires list of types"); 3552 fill_icmp6types((ipfw_insn_icmp6 *)cmd, *av, cblen); 3553 av++; 3554 break; 3555 3556 case TOK_IPTTL: 3557 NEED1("ipttl requires TTL"); 3558 if (strpbrk(*av, "-,")) { 3559 if (!add_ports(cmd, *av, 0, O_IPTTL, cblen)) 3560 errx(EX_DATAERR, "invalid ipttl %s", *av); 3561 } else 3562 fill_cmd(cmd, O_IPTTL, 0, strtoul(*av, NULL, 0)); 3563 av++; 3564 break; 3565 3566 case TOK_IPID: 3567 NEED1("ipid requires id"); 3568 if (strpbrk(*av, "-,")) { 3569 if (!add_ports(cmd, *av, 0, O_IPID, cblen)) 3570 errx(EX_DATAERR, "invalid ipid %s", *av); 3571 } else 3572 fill_cmd(cmd, O_IPID, 0, strtoul(*av, NULL, 0)); 3573 av++; 3574 break; 3575 3576 case TOK_IPLEN: 3577 NEED1("iplen requires length"); 3578 if (strpbrk(*av, "-,")) { 3579 if (!add_ports(cmd, *av, 0, O_IPLEN, cblen)) 3580 errx(EX_DATAERR, "invalid ip len %s", *av); 3581 } else 3582 fill_cmd(cmd, O_IPLEN, 0, strtoul(*av, NULL, 0)); 3583 av++; 3584 break; 3585 3586 case TOK_IPVER: 3587 NEED1("ipver requires version"); 3588 fill_cmd(cmd, O_IPVER, 0, strtoul(*av, NULL, 0)); 3589 av++; 3590 break; 3591 3592 case TOK_IPPRECEDENCE: 3593 NEED1("ipprecedence requires value"); 3594 fill_cmd(cmd, O_IPPRECEDENCE, 0, 3595 (strtoul(*av, NULL, 0) & 7) << 5); 3596 av++; 3597 break; 3598 3599 case TOK_DSCP: 3600 NEED1("missing DSCP code"); 3601 fill_dscp(cmd, *av, cblen); 3602 av++; 3603 break; 3604 3605 case TOK_IPOPTS: 3606 NEED1("missing argument for ipoptions"); 3607 fill_flags(cmd, O_IPOPT, f_ipopts, *av); 3608 av++; 3609 break; 3610 3611 case TOK_IPTOS: 3612 NEED1("missing argument for iptos"); 3613 fill_flags(cmd, O_IPTOS, f_iptos, *av); 3614 av++; 3615 break; 3616 3617 case TOK_UID: 3618 NEED1("uid requires argument"); 3619 { 3620 char *end; 3621 uid_t uid; 3622 struct passwd *pwd; 3623 3624 cmd->opcode = O_UID; 3625 uid = strtoul(*av, &end, 0); 3626 pwd = (*end == '\0') ? getpwuid(uid) : getpwnam(*av); 3627 if (pwd == NULL) 3628 errx(EX_DATAERR, "uid \"%s\" nonexistent", *av); 3629 cmd32->d[0] = pwd->pw_uid; 3630 cmd->len |= F_INSN_SIZE(ipfw_insn_u32); 3631 av++; 3632 } 3633 break; 3634 3635 case TOK_GID: 3636 NEED1("gid requires argument"); 3637 { 3638 char *end; 3639 gid_t gid; 3640 struct group *grp; 3641 3642 cmd->opcode = O_GID; 3643 gid = strtoul(*av, &end, 0); 3644 grp = (*end == '\0') ? getgrgid(gid) : getgrnam(*av); 3645 if (grp == NULL) 3646 errx(EX_DATAERR, "gid \"%s\" nonexistent", *av); 3647 cmd32->d[0] = grp->gr_gid; 3648 cmd->len |= F_INSN_SIZE(ipfw_insn_u32); 3649 av++; 3650 } 3651 break; 3652 3653 case TOK_JAIL: 3654 NEED1("jail requires argument"); 3655 { 3656 char *end; 3657 int jid; 3658 3659 cmd->opcode = O_JAIL; 3660 jid = (int)strtol(*av, &end, 0); 3661 if (jid < 0 || *end != '\0') 3662 errx(EX_DATAERR, "jail requires prison ID"); 3663 cmd32->d[0] = (uint32_t)jid; 3664 cmd->len |= F_INSN_SIZE(ipfw_insn_u32); 3665 av++; 3666 } 3667 break; 3668 3669 case TOK_ESTAB: 3670 fill_cmd(cmd, O_ESTAB, 0, 0); 3671 break; 3672 3673 case TOK_SETUP: 3674 fill_cmd(cmd, O_TCPFLAGS, 0, 3675 (TH_SYN) | ( (TH_ACK) & 0xff) <<8 ); 3676 break; 3677 3678 case TOK_TCPDATALEN: 3679 NEED1("tcpdatalen requires length"); 3680 if (strpbrk(*av, "-,")) { 3681 if (!add_ports(cmd, *av, 0, O_TCPDATALEN, cblen)) 3682 errx(EX_DATAERR, "invalid tcpdata len %s", *av); 3683 } else 3684 fill_cmd(cmd, O_TCPDATALEN, 0, 3685 strtoul(*av, NULL, 0)); 3686 av++; 3687 break; 3688 3689 case TOK_TCPOPTS: 3690 NEED1("missing argument for tcpoptions"); 3691 fill_flags(cmd, O_TCPOPTS, f_tcpopts, *av); 3692 av++; 3693 break; 3694 3695 case TOK_TCPSEQ: 3696 case TOK_TCPACK: 3697 NEED1("tcpseq/tcpack requires argument"); 3698 cmd->len = F_INSN_SIZE(ipfw_insn_u32); 3699 cmd->opcode = (i == TOK_TCPSEQ) ? O_TCPSEQ : O_TCPACK; 3700 cmd32->d[0] = htonl(strtoul(*av, NULL, 0)); 3701 av++; 3702 break; 3703 3704 case TOK_TCPWIN: 3705 NEED1("tcpwin requires length"); 3706 if (strpbrk(*av, "-,")) { 3707 if (!add_ports(cmd, *av, 0, O_TCPWIN, cblen)) 3708 errx(EX_DATAERR, "invalid tcpwin len %s", *av); 3709 } else 3710 fill_cmd(cmd, O_TCPWIN, 0, 3711 strtoul(*av, NULL, 0)); 3712 av++; 3713 break; 3714 3715 case TOK_TCPFLAGS: 3716 NEED1("missing argument for tcpflags"); 3717 cmd->opcode = O_TCPFLAGS; 3718 fill_flags(cmd, O_TCPFLAGS, f_tcpflags, *av); 3719 av++; 3720 break; 3721 3722 case TOK_KEEPSTATE: 3723 if (open_par) 3724 errx(EX_USAGE, "keep-state cannot be part " 3725 "of an or block"); 3726 if (have_state) 3727 errx(EX_USAGE, "only one of keep-state " 3728 "and limit is allowed"); 3729 have_state = cmd; 3730 fill_cmd(cmd, O_KEEP_STATE, 0, 0); 3731 break; 3732 3733 case TOK_LIMIT: { 3734 ipfw_insn_limit *c = (ipfw_insn_limit *)cmd; 3735 int val; 3736 3737 if (open_par) 3738 errx(EX_USAGE, 3739 "limit cannot be part of an or block"); 3740 if (have_state) 3741 errx(EX_USAGE, "only one of keep-state and " 3742 "limit is allowed"); 3743 have_state = cmd; 3744 3745 cmd->len = F_INSN_SIZE(ipfw_insn_limit); 3746 CHECK_CMDLEN; 3747 cmd->opcode = O_LIMIT; 3748 c->limit_mask = c->conn_limit = 0; 3749 3750 while ( av[0] != NULL ) { 3751 if ((val = match_token(limit_masks, *av)) <= 0) 3752 break; 3753 c->limit_mask |= val; 3754 av++; 3755 } 3756 3757 if (c->limit_mask == 0) 3758 errx(EX_USAGE, "limit: missing limit mask"); 3759 3760 GET_UINT_ARG(c->conn_limit, IPFW_ARG_MIN, IPFW_ARG_MAX, 3761 TOK_LIMIT, rule_options); 3762 3763 av++; 3764 break; 3765 } 3766 3767 case TOK_PROTO: 3768 NEED1("missing protocol"); 3769 if (add_proto(cmd, *av, &proto)) { 3770 av++; 3771 } else 3772 errx(EX_DATAERR, "invalid protocol ``%s''", 3773 *av); 3774 break; 3775 3776 case TOK_SRCIP: 3777 NEED1("missing source IP"); 3778 if (add_srcip(cmd, *av, cblen)) { 3779 av++; 3780 } 3781 break; 3782 3783 case TOK_DSTIP: 3784 NEED1("missing destination IP"); 3785 if (add_dstip(cmd, *av, cblen)) { 3786 av++; 3787 } 3788 break; 3789 3790 case TOK_SRCIP6: 3791 NEED1("missing source IP6"); 3792 if (add_srcip6(cmd, *av, cblen)) { 3793 av++; 3794 } 3795 break; 3796 3797 case TOK_DSTIP6: 3798 NEED1("missing destination IP6"); 3799 if (add_dstip6(cmd, *av, cblen)) { 3800 av++; 3801 } 3802 break; 3803 3804 case TOK_SRCPORT: 3805 NEED1("missing source port"); 3806 if (_substrcmp(*av, "any") == 0 || 3807 add_ports(cmd, *av, proto, O_IP_SRCPORT, cblen)) { 3808 av++; 3809 } else 3810 errx(EX_DATAERR, "invalid source port %s", *av); 3811 break; 3812 3813 case TOK_DSTPORT: 3814 NEED1("missing destination port"); 3815 if (_substrcmp(*av, "any") == 0 || 3816 add_ports(cmd, *av, proto, O_IP_DSTPORT, cblen)) { 3817 av++; 3818 } else 3819 errx(EX_DATAERR, "invalid destination port %s", 3820 *av); 3821 break; 3822 3823 case TOK_MAC: 3824 if (add_mac(cmd, av, cblen)) 3825 av += 2; 3826 break; 3827 3828 case TOK_MACTYPE: 3829 NEED1("missing mac type"); 3830 if (!add_mactype(cmd, *av, cblen)) 3831 errx(EX_DATAERR, "invalid mac type %s", *av); 3832 av++; 3833 break; 3834 3835 case TOK_VERREVPATH: 3836 fill_cmd(cmd, O_VERREVPATH, 0, 0); 3837 break; 3838 3839 case TOK_VERSRCREACH: 3840 fill_cmd(cmd, O_VERSRCREACH, 0, 0); 3841 break; 3842 3843 case TOK_ANTISPOOF: 3844 fill_cmd(cmd, O_ANTISPOOF, 0, 0); 3845 break; 3846 3847 case TOK_IPSEC: 3848 fill_cmd(cmd, O_IPSEC, 0, 0); 3849 break; 3850 3851 case TOK_IPV6: 3852 fill_cmd(cmd, O_IP6, 0, 0); 3853 break; 3854 3855 case TOK_IPV4: 3856 fill_cmd(cmd, O_IP4, 0, 0); 3857 break; 3858 3859 case TOK_EXT6HDR: 3860 fill_ext6hdr( cmd, *av ); 3861 av++; 3862 break; 3863 3864 case TOK_FLOWID: 3865 if (proto != IPPROTO_IPV6 ) 3866 errx( EX_USAGE, "flow-id filter is active " 3867 "only for ipv6 protocol\n"); 3868 fill_flow6( (ipfw_insn_u32 *) cmd, *av, cblen); 3869 av++; 3870 break; 3871 3872 case TOK_COMMENT: 3873 fill_comment(cmd, av, cblen); 3874 av[0]=NULL; 3875 break; 3876 3877 case TOK_TAGGED: 3878 if (av[0] && strpbrk(*av, "-,")) { 3879 if (!add_ports(cmd, *av, 0, O_TAGGED, cblen)) 3880 errx(EX_DATAERR, "tagged: invalid tag" 3881 " list: %s", *av); 3882 } 3883 else { 3884 uint16_t tag; 3885 3886 GET_UINT_ARG(tag, IPFW_ARG_MIN, IPFW_ARG_MAX, 3887 TOK_TAGGED, rule_options); 3888 fill_cmd(cmd, O_TAGGED, 0, tag); 3889 } 3890 av++; 3891 break; 3892 3893 case TOK_FIB: 3894 NEED1("fib requires fib number"); 3895 fill_cmd(cmd, O_FIB, 0, strtoul(*av, NULL, 0)); 3896 av++; 3897 break; 3898 case TOK_SOCKARG: 3899 fill_cmd(cmd, O_SOCKARG, 0, 0); 3900 break; 3901 3902 case TOK_LOOKUP: { 3903 ipfw_insn_u32 *c = (ipfw_insn_u32 *)cmd; 3904 char *p; 3905 int j; 3906 3907 if (!av[0] || !av[1]) 3908 errx(EX_USAGE, "format: lookup argument tablenum"); 3909 cmd->opcode = O_IP_DST_LOOKUP; 3910 cmd->len |= F_INSN_SIZE(ipfw_insn) + 2; 3911 i = match_token(rule_options, *av); 3912 for (j = 0; lookup_key[j] >= 0 ; j++) { 3913 if (i == lookup_key[j]) 3914 break; 3915 } 3916 if (lookup_key[j] <= 0) 3917 errx(EX_USAGE, "format: cannot lookup on %s", *av); 3918 __PAST_END(c->d, 1) = j; // i converted to option 3919 av++; 3920 cmd->arg1 = strtoul(*av, &p, 0); 3921 if (p && *p) 3922 errx(EX_USAGE, "format: lookup argument tablenum"); 3923 av++; 3924 } 3925 break; 3926 3927 default: 3928 errx(EX_USAGE, "unrecognised option [%d] %s\n", i, s); 3929 } 3930 if (F_LEN(cmd) > 0) { /* prepare to advance */ 3931 prev = cmd; 3932 cmd = next_cmd(cmd, &cblen); 3933 } 3934 } 3935 3936done: 3937 /* 3938 * Now copy stuff into the rule. 3939 * If we have a keep-state option, the first instruction 3940 * must be a PROBE_STATE (which is generated here). 3941 * If we have a LOG option, it was stored as the first command, 3942 * and now must be moved to the top of the action part. 3943 */ 3944 dst = (ipfw_insn *)rule->cmd; 3945 3946 /* 3947 * First thing to write into the command stream is the match probability. 3948 */ 3949 if (match_prob != 1) { /* 1 means always match */ 3950 dst->opcode = O_PROB; 3951 dst->len = 2; 3952 *((int32_t *)(dst+1)) = (int32_t)(match_prob * 0x7fffffff); 3953 dst += dst->len; 3954 } 3955 3956 /* 3957 * generate O_PROBE_STATE if necessary 3958 */ 3959 if (have_state && have_state->opcode != O_CHECK_STATE) { 3960 fill_cmd(dst, O_PROBE_STATE, 0, 0); 3961 dst = next_cmd(dst, &rblen); 3962 } 3963 3964 /* copy all commands but O_LOG, O_KEEP_STATE, O_LIMIT, O_ALTQ, O_TAG */ 3965 for (src = (ipfw_insn *)cmdbuf; src != cmd; src += i) { 3966 i = F_LEN(src); 3967 CHECK_RBUFLEN(i); 3968 3969 switch (src->opcode) { 3970 case O_LOG: 3971 case O_KEEP_STATE: 3972 case O_LIMIT: 3973 case O_ALTQ: 3974 case O_TAG: 3975 break; 3976 default: 3977 bcopy(src, dst, i * sizeof(uint32_t)); 3978 dst += i; 3979 } 3980 } 3981 3982 /* 3983 * put back the have_state command as last opcode 3984 */ 3985 if (have_state && have_state->opcode != O_CHECK_STATE) { 3986 i = F_LEN(have_state); 3987 CHECK_RBUFLEN(i); 3988 bcopy(have_state, dst, i * sizeof(uint32_t)); 3989 dst += i; 3990 } 3991 /* 3992 * start action section 3993 */ 3994 rule->act_ofs = dst - rule->cmd; 3995 3996 /* put back O_LOG, O_ALTQ, O_TAG if necessary */ 3997 if (have_log) { 3998 i = F_LEN(have_log); 3999 CHECK_RBUFLEN(i); 4000 bcopy(have_log, dst, i * sizeof(uint32_t)); 4001 dst += i; 4002 } 4003 if (have_altq) { 4004 i = F_LEN(have_altq); 4005 CHECK_RBUFLEN(i); 4006 bcopy(have_altq, dst, i * sizeof(uint32_t)); 4007 dst += i; 4008 } 4009 if (have_tag) { 4010 i = F_LEN(have_tag); 4011 CHECK_RBUFLEN(i); 4012 bcopy(have_tag, dst, i * sizeof(uint32_t)); 4013 dst += i; 4014 } 4015 4016 /* 4017 * copy all other actions 4018 */ 4019 for (src = (ipfw_insn *)actbuf; src != action; src += i) { 4020 i = F_LEN(src); 4021 CHECK_RBUFLEN(i); 4022 bcopy(src, dst, i * sizeof(uint32_t)); 4023 dst += i; 4024 } 4025 4026 rule->cmd_len = (uint32_t *)dst - (uint32_t *)(rule->cmd); 4027 i = (char *)dst - (char *)rule; 4028 if (do_cmd(IP_FW_ADD, rule, (uintptr_t)&i) == -1) 4029 err(EX_UNAVAILABLE, "getsockopt(%s)", "IP_FW_ADD"); 4030 if (!co.do_quiet) 4031 show_ipfw(rule, 0, 0); 4032} 4033 4034/* 4035 * clear the counters or the log counters. 4036 */ 4037void 4038ipfw_zero(int ac, char *av[], int optname /* 0 = IP_FW_ZERO, 1 = IP_FW_RESETLOG */) 4039{ 4040 uint32_t arg, saved_arg; 4041 int failed = EX_OK; 4042 char const *errstr; 4043 char const *name = optname ? "RESETLOG" : "ZERO"; 4044 4045 optname = optname ? IP_FW_RESETLOG : IP_FW_ZERO; 4046 4047 av++; ac--; 4048 4049 if (!ac) { 4050 /* clear all entries */ 4051 if (do_cmd(optname, NULL, 0) < 0) 4052 err(EX_UNAVAILABLE, "setsockopt(IP_FW_%s)", name); 4053 if (!co.do_quiet) 4054 printf("%s.\n", optname == IP_FW_ZERO ? 4055 "Accounting cleared":"Logging counts reset"); 4056 4057 return; 4058 } 4059 4060 while (ac) { 4061 /* Rule number */ 4062 if (isdigit(**av)) { 4063 arg = strtonum(*av, 0, 0xffff, &errstr); 4064 if (errstr) 4065 errx(EX_DATAERR, 4066 "invalid rule number %s\n", *av); 4067 saved_arg = arg; 4068 if (co.use_set) 4069 arg |= (1 << 24) | ((co.use_set - 1) << 16); 4070 av++; 4071 ac--; 4072 if (do_cmd(optname, &arg, sizeof(arg))) { 4073 warn("rule %u: setsockopt(IP_FW_%s)", 4074 saved_arg, name); 4075 failed = EX_UNAVAILABLE; 4076 } else if (!co.do_quiet) 4077 printf("Entry %d %s.\n", saved_arg, 4078 optname == IP_FW_ZERO ? 4079 "cleared" : "logging count reset"); 4080 } else { 4081 errx(EX_USAGE, "invalid rule number ``%s''", *av); 4082 } 4083 } 4084 if (failed != EX_OK) 4085 exit(failed); 4086} 4087 4088void 4089ipfw_flush(int force) 4090{ 4091 int cmd = co.do_pipe ? IP_DUMMYNET_FLUSH : IP_FW_FLUSH; 4092 4093 if (!force && !co.do_quiet) { /* need to ask user */ 4094 int c; 4095 4096 printf("Are you sure? [yn] "); 4097 fflush(stdout); 4098 do { 4099 c = toupper(getc(stdin)); 4100 while (c != '\n' && getc(stdin) != '\n') 4101 if (feof(stdin)) 4102 return; /* and do not flush */ 4103 } while (c != 'Y' && c != 'N'); 4104 printf("\n"); 4105 if (c == 'N') /* user said no */ 4106 return; 4107 } 4108 if (co.do_pipe) { 4109 dummynet_flush(); 4110 return; 4111 } 4112 /* `ipfw set N flush` - is the same that `ipfw delete set N` */ 4113 if (co.use_set) { 4114 uint32_t arg = ((co.use_set - 1) & 0xffff) | (1 << 24); 4115 if (do_cmd(IP_FW_DEL, &arg, sizeof(arg)) < 0) 4116 err(EX_UNAVAILABLE, "setsockopt(IP_FW_DEL)"); 4117 } else if (do_cmd(cmd, NULL, 0) < 0) 4118 err(EX_UNAVAILABLE, "setsockopt(IP_%s_FLUSH)", 4119 co.do_pipe ? "DUMMYNET" : "FW"); 4120 if (!co.do_quiet) 4121 printf("Flushed all %s.\n", co.do_pipe ? "pipes" : "rules"); 4122} 4123 4124 4125static void table_list(uint16_t num, int need_header); 4126static void table_fill_xentry(char *arg, ipfw_table_xentry *xent); 4127 4128/* 4129 * Retrieve maximum number of tables supported by ipfw(4) module. 4130 */ 4131uint32_t 4132ipfw_get_tables_max() 4133{ 4134 size_t len; 4135 uint32_t tables_max; 4136 4137 if (ipfw_tables_max != 0) 4138 return (ipfw_tables_max); 4139 4140 len = sizeof(tables_max); 4141 if (sysctlbyname("net.inet.ip.fw.tables_max", &tables_max, &len, 4142 NULL, 0) == -1) { 4143 if (co.test_only) 4144 tables_max = 128; /* Old conservative default */ 4145 else 4146 errx(1, "Can't determine maximum number of ipfw tables." 4147 " Perhaps you forgot to load ipfw module?"); 4148 } 4149 4150 ipfw_tables_max = tables_max; 4151 4152 return (ipfw_tables_max); 4153} 4154 4155/* 4156 * This one handles all table-related commands 4157 * ipfw table N add addr[/masklen] [value] 4158 * ipfw table N delete addr[/masklen] 4159 * ipfw table {N | all} flush 4160 * ipfw table {N | all} list 4161 */ 4162void 4163ipfw_table_handler(int ac, char *av[]) 4164{ 4165 ipfw_table_xentry xent; 4166 int do_add; 4167 int is_all; 4168 uint32_t a; 4169 uint32_t tables_max; 4170 4171 tables_max = ipfw_get_tables_max(); 4172 4173 memset(&xent, 0, sizeof(xent)); 4174 4175 ac--; av++; 4176 if (ac && isdigit(**av)) { 4177 xent.tbl = atoi(*av); 4178 is_all = 0; 4179 ac--; av++; 4180 } else if (ac && _substrcmp(*av, "all") == 0) { 4181 xent.tbl = 0; 4182 is_all = 1; 4183 ac--; av++; 4184 } else 4185 errx(EX_USAGE, "table number or 'all' keyword required"); 4186 if (xent.tbl >= tables_max) 4187 errx(EX_USAGE, "The table number exceeds the maximum allowed " 4188 "value (%d)", tables_max - 1); 4189 NEED1("table needs command"); 4190 if (is_all && _substrcmp(*av, "list") != 0 4191 && _substrcmp(*av, "flush") != 0) 4192 errx(EX_USAGE, "table number required"); 4193 4194 if (_substrcmp(*av, "add") == 0 || 4195 _substrcmp(*av, "delete") == 0) { 4196 do_add = **av == 'a'; 4197 ac--; av++; 4198 if (!ac) 4199 errx(EX_USAGE, "address required"); 4200 4201 table_fill_xentry(*av, &xent); 4202 4203 ac--; av++; 4204 if (do_add && ac) { 4205 unsigned int tval; 4206 /* isdigit is a bit of a hack here.. */ 4207 if (strchr(*av, (int)'.') == NULL && isdigit(**av)) { 4208 xent.value = strtoul(*av, NULL, 0); 4209 } else { 4210 if (lookup_host(*av, (struct in_addr *)&tval) == 0) { 4211 /* The value must be stored in host order * 4212 * so that the values < 65k can be distinguished */ 4213 xent.value = ntohl(tval); 4214 } else { 4215 errx(EX_NOHOST, "hostname ``%s'' unknown", *av); 4216 } 4217 } 4218 } else 4219 xent.value = 0; 4220 if (do_setcmd3(do_add ? IP_FW_TABLE_XADD : IP_FW_TABLE_XDEL, 4221 &xent, xent.len) < 0) { 4222 /* If running silent, don't bomb out on these errors. */ 4223 if (!(co.do_quiet && (errno == (do_add ? EEXIST : ESRCH)))) 4224 err(EX_OSERR, "setsockopt(IP_FW_TABLE_%s)", 4225 do_add ? "XADD" : "XDEL"); 4226 /* In silent mode, react to a failed add by deleting */ 4227 if (do_add) { 4228 do_setcmd3(IP_FW_TABLE_XDEL, &xent, xent.len); 4229 if (do_setcmd3(IP_FW_TABLE_XADD, &xent, xent.len) < 0) 4230 err(EX_OSERR, 4231 "setsockopt(IP_FW_TABLE_XADD)"); 4232 } 4233 } 4234 } else if (_substrcmp(*av, "flush") == 0) { 4235 a = is_all ? tables_max : (uint32_t)(xent.tbl + 1); 4236 do { 4237 if (do_cmd(IP_FW_TABLE_FLUSH, &xent.tbl, 4238 sizeof(xent.tbl)) < 0) 4239 err(EX_OSERR, "setsockopt(IP_FW_TABLE_FLUSH)"); 4240 } while (++xent.tbl < a); 4241 } else if (_substrcmp(*av, "list") == 0) { 4242 a = is_all ? tables_max : (uint32_t)(xent.tbl + 1); 4243 do { 4244 table_list(xent.tbl, is_all); 4245 } while (++xent.tbl < a); 4246 } else 4247 errx(EX_USAGE, "invalid table command %s", *av); 4248} 4249 4250static void 4251table_fill_xentry(char *arg, ipfw_table_xentry *xent) 4252{ 4253 int addrlen, mask, masklen, type; 4254 struct in6_addr *paddr; 4255 uint32_t *pkey; 4256 char *p; 4257 uint32_t key; 4258 4259 mask = 0; 4260 type = 0; 4261 addrlen = 0; 4262 masklen = 0; 4263 4264 /* 4265 * Let's try to guess type by agrument. 4266 * Possible types: 4267 * 1) IPv4[/mask] 4268 * 2) IPv6[/mask] 4269 * 3) interface name 4270 * 4) port, uid/gid or other u32 key (base 10 format) 4271 * 5) hostname 4272 */ 4273 paddr = &xent->k.addr6; 4274 if (ishexnumber(*arg) != 0 || *arg == ':') { 4275 /* Remove / if exists */ 4276 if ((p = strchr(arg, '/')) != NULL) { 4277 *p = '\0'; 4278 mask = atoi(p + 1); 4279 } 4280 4281 if (inet_pton(AF_INET, arg, paddr) == 1) { 4282 if (p != NULL && mask > 32) 4283 errx(EX_DATAERR, "bad IPv4 mask width: %s", 4284 p + 1); 4285 4286 type = IPFW_TABLE_CIDR; 4287 masklen = p ? mask : 32; 4288 addrlen = sizeof(struct in_addr); 4289 } else if (inet_pton(AF_INET6, arg, paddr) == 1) { 4290 if (IN6_IS_ADDR_V4COMPAT(paddr)) 4291 errx(EX_DATAERR, 4292 "Use IPv4 instead of v4-compatible"); 4293 if (p != NULL && mask > 128) 4294 errx(EX_DATAERR, "bad IPv6 mask width: %s", 4295 p + 1); 4296 4297 type = IPFW_TABLE_CIDR; 4298 masklen = p ? mask : 128; 4299 addrlen = sizeof(struct in6_addr); 4300 } else { 4301 /* Port or any other key */ 4302 /* Skip non-base 10 entries like 'fa1' */ 4303 key = strtol(arg, &p, 10); 4304 if (*p == '\0') { 4305 pkey = (uint32_t *)paddr; 4306 *pkey = htonl(key); 4307 type = IPFW_TABLE_CIDR; 4308 masklen = 32; 4309 addrlen = sizeof(uint32_t); 4310 } else if ((p != arg) && (*p == '.')) { 4311 /* 4312 * Warn on IPv4 address strings 4313 * which are "valid" for inet_aton() but not 4314 * in inet_pton(). 4315 * 4316 * Typical examples: '10.5' or '10.0.0.05' 4317 */ 4318 errx(EX_DATAERR, 4319 "Invalid IPv4 address: %s", arg); 4320 } 4321 } 4322 } 4323 4324 if (type == 0 && strchr(arg, '.') == NULL) { 4325 /* Assume interface name. Copy significant data only */ 4326 mask = MIN(strlen(arg), IF_NAMESIZE - 1); 4327 memcpy(xent->k.iface, arg, mask); 4328 /* Set mask to exact match */ 4329 masklen = 8 * IF_NAMESIZE; 4330 type = IPFW_TABLE_INTERFACE; 4331 addrlen = IF_NAMESIZE; 4332 } 4333 4334 if (type == 0) { 4335 if (lookup_host(arg, (struct in_addr *)paddr) != 0) 4336 errx(EX_NOHOST, "hostname ``%s'' unknown", arg); 4337 4338 masklen = 32; 4339 type = IPFW_TABLE_CIDR; 4340 addrlen = sizeof(struct in_addr); 4341 } 4342 4343 xent->type = type; 4344 xent->masklen = masklen; 4345 xent->len = offsetof(ipfw_table_xentry, k) + addrlen; 4346} 4347 4348static void 4349table_list(uint16_t num, int need_header) 4350{ 4351 ipfw_xtable *tbl; 4352 ipfw_table_xentry *xent; 4353 socklen_t l; 4354 uint32_t *a, sz, tval; 4355 char tbuf[128]; 4356 struct in6_addr *addr6; 4357 ip_fw3_opheader *op3; 4358 4359 /* Prepend value with IP_FW3 header */ 4360 l = sizeof(ip_fw3_opheader) + sizeof(uint32_t); 4361 op3 = alloca(l); 4362 /* Zero reserved fields */ 4363 memset(op3, 0, sizeof(ip_fw3_opheader)); 4364 a = (uint32_t *)(op3 + 1); 4365 *a = num; 4366 op3->opcode = IP_FW_TABLE_XGETSIZE; 4367 if (do_cmd(IP_FW3, op3, (uintptr_t)&l) < 0) 4368 err(EX_OSERR, "getsockopt(IP_FW_TABLE_XGETSIZE)"); 4369 4370 /* If a is zero we have nothing to do, the table is empty. */ 4371 if (*a == 0) 4372 return; 4373 4374 l = *a; 4375 tbl = safe_calloc(1, l); 4376 tbl->opheader.opcode = IP_FW_TABLE_XLIST; 4377 tbl->tbl = num; 4378 if (do_cmd(IP_FW3, tbl, (uintptr_t)&l) < 0) 4379 err(EX_OSERR, "getsockopt(IP_FW_TABLE_XLIST)"); 4380 if (tbl->cnt && need_header) 4381 printf("---table(%d)---\n", tbl->tbl); 4382 sz = tbl->size - sizeof(ipfw_xtable); 4383 xent = &tbl->xent[0]; 4384 while (sz > 0) { 4385 switch (tbl->type) { 4386 case IPFW_TABLE_CIDR: 4387 /* IPv4 or IPv6 prefixes */ 4388 tval = xent->value; 4389 addr6 = &xent->k.addr6; 4390 4391 4392 if ((xent->flags & IPFW_TCF_INET) != 0) { 4393 /* IPv4 address */ 4394 inet_ntop(AF_INET, &addr6->s6_addr32[3], tbuf, sizeof(tbuf)); 4395 } else { 4396 /* IPv6 address */ 4397 inet_ntop(AF_INET6, addr6, tbuf, sizeof(tbuf)); 4398 } 4399 4400 if (co.do_value_as_ip) { 4401 tval = htonl(tval); 4402 printf("%s/%u %s\n", tbuf, xent->masklen, 4403 inet_ntoa(*(struct in_addr *)&tval)); 4404 } else 4405 printf("%s/%u %u\n", tbuf, xent->masklen, tval); 4406 break; 4407 case IPFW_TABLE_INTERFACE: 4408 /* Interface names */ 4409 tval = xent->value; 4410 if (co.do_value_as_ip) { 4411 tval = htonl(tval); 4412 printf("%s %s\n", xent->k.iface, 4413 inet_ntoa(*(struct in_addr *)&tval)); 4414 } else 4415 printf("%s %u\n", xent->k.iface, tval); 4416 } 4417 4418 if (sz < xent->len) 4419 break; 4420 sz -= xent->len; 4421 xent = (ipfw_table_xentry *)((char *)xent + xent->len); 4422 } 4423 4424 free(tbl); 4425} 4426