13c0c8717SLuigi Rizzo /* 23c0c8717SLuigi Rizzo * Copyright (c) 2002-2003 Luigi Rizzo 33c0c8717SLuigi Rizzo * Copyright (c) 1996 Alex Nash, Paul Traina, Poul-Henning Kamp 43c0c8717SLuigi Rizzo * Copyright (c) 1994 Ugen J.S.Antsilevich 53c0c8717SLuigi Rizzo * 63c0c8717SLuigi Rizzo * Idea and grammar partially left from: 73c0c8717SLuigi Rizzo * Copyright (c) 1993 Daniel Boulet 83c0c8717SLuigi Rizzo * 93c0c8717SLuigi Rizzo * Redistribution and use in source forms, with and without modification, 103c0c8717SLuigi Rizzo * are permitted provided that this entire comment appears intact. 113c0c8717SLuigi Rizzo * 123c0c8717SLuigi Rizzo * Redistribution in binary form may occur without any restrictions. 133c0c8717SLuigi Rizzo * Obviously, it would be nice if you gave credit where credit is due 143c0c8717SLuigi Rizzo * but requiring it would be too onerous. 153c0c8717SLuigi Rizzo * 163c0c8717SLuigi Rizzo * This software is provided ``AS IS'' without any warranties of any kind. 173c0c8717SLuigi Rizzo * 183c0c8717SLuigi Rizzo * NEW command line interface for IP firewall facility 193c0c8717SLuigi Rizzo * 203c0c8717SLuigi Rizzo * $FreeBSD$ 213c0c8717SLuigi Rizzo */ 223c0c8717SLuigi Rizzo 233c0c8717SLuigi Rizzo /* 243c0c8717SLuigi Rizzo * Options that can be set on the command line. 253c0c8717SLuigi Rizzo * When reading commands from a file, a subset of the options can also 263c0c8717SLuigi Rizzo * be applied globally by specifying them before the file name. 273c0c8717SLuigi Rizzo * After that, each line can contain its own option that changes 283c0c8717SLuigi Rizzo * the global value. 293c0c8717SLuigi Rizzo * XXX The context is not restored after each line. 303c0c8717SLuigi Rizzo */ 313c0c8717SLuigi Rizzo 323c0c8717SLuigi Rizzo struct cmdline_opts { 333c0c8717SLuigi Rizzo /* boolean options: */ 343c0c8717SLuigi Rizzo int do_value_as_ip; /* show table value as IP */ 353c0c8717SLuigi Rizzo int do_resolv; /* try to resolve all ip to names */ 363c0c8717SLuigi Rizzo int do_time; /* Show time stamps */ 373c0c8717SLuigi Rizzo int do_quiet; /* Be quiet in add and flush */ 38cc4d3c30SLuigi Rizzo int do_pipe; /* this cmd refers to a pipe/queue/sched */ 393c0c8717SLuigi Rizzo int do_nat; /* this cmd refers to a nat config */ 403c0c8717SLuigi Rizzo int do_dynamic; /* display dynamic rules */ 413c0c8717SLuigi Rizzo int do_expired; /* display expired dynamic rules */ 423c0c8717SLuigi Rizzo int do_compact; /* show rules in compact mode */ 433c0c8717SLuigi Rizzo int do_force; /* do not ask for confirmation */ 443c0c8717SLuigi Rizzo int show_sets; /* display the set each rule belongs to */ 453c0c8717SLuigi Rizzo int test_only; /* only check syntax */ 463c0c8717SLuigi Rizzo int comment_only; /* only print action and comment */ 473c0c8717SLuigi Rizzo int verbose; /* be verbose on some commands */ 483c0c8717SLuigi Rizzo 493c0c8717SLuigi Rizzo /* The options below can have multiple values. */ 503c0c8717SLuigi Rizzo 513c0c8717SLuigi Rizzo int do_sort; /* field to sort results (0 = no) */ 523c0c8717SLuigi Rizzo /* valid fields are 1 and above */ 533c0c8717SLuigi Rizzo 543c0c8717SLuigi Rizzo int use_set; /* work with specified set number */ 553c0c8717SLuigi Rizzo /* 0 means all sets, otherwise apply to set use_set - 1 */ 563c0c8717SLuigi Rizzo 573c0c8717SLuigi Rizzo }; 583c0c8717SLuigi Rizzo 593c0c8717SLuigi Rizzo extern struct cmdline_opts co; 603c0c8717SLuigi Rizzo 613c0c8717SLuigi Rizzo /* 623c0c8717SLuigi Rizzo * _s_x is a structure that stores a string <-> token pairs, used in 633c0c8717SLuigi Rizzo * various places in the parser. Entries are stored in arrays, 643c0c8717SLuigi Rizzo * with an entry with s=NULL as terminator. 653c0c8717SLuigi Rizzo * The search routines are match_token() and match_value(). 663c0c8717SLuigi Rizzo * Often, an element with x=0 contains an error string. 673c0c8717SLuigi Rizzo * 683c0c8717SLuigi Rizzo */ 693c0c8717SLuigi Rizzo struct _s_x { 703c0c8717SLuigi Rizzo char const *s; 713c0c8717SLuigi Rizzo int x; 723c0c8717SLuigi Rizzo }; 733c0c8717SLuigi Rizzo 744e9c8ae7SLuigi Rizzo enum tokens { 754e9c8ae7SLuigi Rizzo TOK_NULL=0, 764e9c8ae7SLuigi Rizzo 774e9c8ae7SLuigi Rizzo TOK_OR, 784e9c8ae7SLuigi Rizzo TOK_NOT, 794e9c8ae7SLuigi Rizzo TOK_STARTBRACE, 804e9c8ae7SLuigi Rizzo TOK_ENDBRACE, 814e9c8ae7SLuigi Rizzo 824e9c8ae7SLuigi Rizzo TOK_ACCEPT, 834e9c8ae7SLuigi Rizzo TOK_COUNT, 844e9c8ae7SLuigi Rizzo TOK_PIPE, 85cc4d3c30SLuigi Rizzo TOK_LINK, 864e9c8ae7SLuigi Rizzo TOK_QUEUE, 87cc4d3c30SLuigi Rizzo TOK_FLOWSET, 88cc4d3c30SLuigi Rizzo TOK_SCHED, 894e9c8ae7SLuigi Rizzo TOK_DIVERT, 904e9c8ae7SLuigi Rizzo TOK_TEE, 914e9c8ae7SLuigi Rizzo TOK_NETGRAPH, 924e9c8ae7SLuigi Rizzo TOK_NGTEE, 934e9c8ae7SLuigi Rizzo TOK_FORWARD, 944e9c8ae7SLuigi Rizzo TOK_SKIPTO, 954e9c8ae7SLuigi Rizzo TOK_DENY, 964e9c8ae7SLuigi Rizzo TOK_REJECT, 974e9c8ae7SLuigi Rizzo TOK_RESET, 984e9c8ae7SLuigi Rizzo TOK_UNREACH, 994e9c8ae7SLuigi Rizzo TOK_CHECKSTATE, 1004e9c8ae7SLuigi Rizzo TOK_NAT, 101eb2e4119SPaolo Pisati TOK_REASS, 1029527ec6eSAndrey V. Elsukov TOK_CALL, 1039527ec6eSAndrey V. Elsukov TOK_RETURN, 1044e9c8ae7SLuigi Rizzo 1054e9c8ae7SLuigi Rizzo TOK_ALTQ, 1064e9c8ae7SLuigi Rizzo TOK_LOG, 1074e9c8ae7SLuigi Rizzo TOK_TAG, 1084e9c8ae7SLuigi Rizzo TOK_UNTAG, 1094e9c8ae7SLuigi Rizzo 1104e9c8ae7SLuigi Rizzo TOK_TAGGED, 1114e9c8ae7SLuigi Rizzo TOK_UID, 1124e9c8ae7SLuigi Rizzo TOK_GID, 1134e9c8ae7SLuigi Rizzo TOK_JAIL, 1144e9c8ae7SLuigi Rizzo TOK_IN, 1154e9c8ae7SLuigi Rizzo TOK_LIMIT, 1164e9c8ae7SLuigi Rizzo TOK_KEEPSTATE, 1174e9c8ae7SLuigi Rizzo TOK_LAYER2, 1184e9c8ae7SLuigi Rizzo TOK_OUT, 1194e9c8ae7SLuigi Rizzo TOK_DIVERTED, 1204e9c8ae7SLuigi Rizzo TOK_DIVERTEDLOOPBACK, 1214e9c8ae7SLuigi Rizzo TOK_DIVERTEDOUTPUT, 1224e9c8ae7SLuigi Rizzo TOK_XMIT, 1234e9c8ae7SLuigi Rizzo TOK_RECV, 1244e9c8ae7SLuigi Rizzo TOK_VIA, 1254e9c8ae7SLuigi Rizzo TOK_FRAG, 1264e9c8ae7SLuigi Rizzo TOK_IPOPTS, 1274e9c8ae7SLuigi Rizzo TOK_IPLEN, 1284e9c8ae7SLuigi Rizzo TOK_IPID, 1294e9c8ae7SLuigi Rizzo TOK_IPPRECEDENCE, 13072662a75SLuigi Rizzo TOK_DSCP, 1314e9c8ae7SLuigi Rizzo TOK_IPTOS, 1324e9c8ae7SLuigi Rizzo TOK_IPTTL, 1334e9c8ae7SLuigi Rizzo TOK_IPVER, 1344e9c8ae7SLuigi Rizzo TOK_ESTAB, 1354e9c8ae7SLuigi Rizzo TOK_SETUP, 1364e9c8ae7SLuigi Rizzo TOK_TCPDATALEN, 1374e9c8ae7SLuigi Rizzo TOK_TCPFLAGS, 1384e9c8ae7SLuigi Rizzo TOK_TCPOPTS, 1394e9c8ae7SLuigi Rizzo TOK_TCPSEQ, 1404e9c8ae7SLuigi Rizzo TOK_TCPACK, 1414e9c8ae7SLuigi Rizzo TOK_TCPWIN, 1424e9c8ae7SLuigi Rizzo TOK_ICMPTYPES, 1434e9c8ae7SLuigi Rizzo TOK_MAC, 1444e9c8ae7SLuigi Rizzo TOK_MACTYPE, 1454e9c8ae7SLuigi Rizzo TOK_VERREVPATH, 1464e9c8ae7SLuigi Rizzo TOK_VERSRCREACH, 1474e9c8ae7SLuigi Rizzo TOK_ANTISPOOF, 1484e9c8ae7SLuigi Rizzo TOK_IPSEC, 1494e9c8ae7SLuigi Rizzo TOK_COMMENT, 1504e9c8ae7SLuigi Rizzo 1514e9c8ae7SLuigi Rizzo TOK_PLR, 1524e9c8ae7SLuigi Rizzo TOK_NOERROR, 1534e9c8ae7SLuigi Rizzo TOK_BUCKETS, 1544e9c8ae7SLuigi Rizzo TOK_DSTIP, 1554e9c8ae7SLuigi Rizzo TOK_SRCIP, 1564e9c8ae7SLuigi Rizzo TOK_DSTPORT, 1574e9c8ae7SLuigi Rizzo TOK_SRCPORT, 1584e9c8ae7SLuigi Rizzo TOK_ALL, 1594e9c8ae7SLuigi Rizzo TOK_MASK, 160cc4d3c30SLuigi Rizzo TOK_FLOW_MASK, 161cc4d3c30SLuigi Rizzo TOK_SCHED_MASK, 1624e9c8ae7SLuigi Rizzo TOK_BW, 1634e9c8ae7SLuigi Rizzo TOK_DELAY, 164cc4d3c30SLuigi Rizzo TOK_PROFILE, 1656882bf4dSOleg Bulyzhin TOK_BURST, 1664e9c8ae7SLuigi Rizzo TOK_RED, 1674e9c8ae7SLuigi Rizzo TOK_GRED, 1684e9c8ae7SLuigi Rizzo TOK_DROPTAIL, 1694e9c8ae7SLuigi Rizzo TOK_PROTO, 170cc4d3c30SLuigi Rizzo /* dummynet tokens */ 1714e9c8ae7SLuigi Rizzo TOK_WEIGHT, 172cc4d3c30SLuigi Rizzo TOK_LMAX, 173cc4d3c30SLuigi Rizzo TOK_PRI, 174cc4d3c30SLuigi Rizzo TOK_TYPE, 175cc4d3c30SLuigi Rizzo TOK_SLOTSIZE, 176cc4d3c30SLuigi Rizzo 1774e9c8ae7SLuigi Rizzo TOK_IP, 1784e9c8ae7SLuigi Rizzo TOK_IF, 1794e9c8ae7SLuigi Rizzo TOK_ALOG, 1804e9c8ae7SLuigi Rizzo TOK_DENY_INC, 1814e9c8ae7SLuigi Rizzo TOK_SAME_PORTS, 1824e9c8ae7SLuigi Rizzo TOK_UNREG_ONLY, 1831875bbfeSAndrey V. Elsukov TOK_SKIP_GLOBAL, 1844e9c8ae7SLuigi Rizzo TOK_RESET_ADDR, 1854e9c8ae7SLuigi Rizzo TOK_ALIAS_REV, 1864e9c8ae7SLuigi Rizzo TOK_PROXY_ONLY, 1874e9c8ae7SLuigi Rizzo TOK_REDIR_ADDR, 1884e9c8ae7SLuigi Rizzo TOK_REDIR_PORT, 1894e9c8ae7SLuigi Rizzo TOK_REDIR_PROTO, 1904e9c8ae7SLuigi Rizzo 1914e9c8ae7SLuigi Rizzo TOK_IPV6, 1924e9c8ae7SLuigi Rizzo TOK_FLOWID, 1934e9c8ae7SLuigi Rizzo TOK_ICMP6TYPES, 1944e9c8ae7SLuigi Rizzo TOK_EXT6HDR, 1954e9c8ae7SLuigi Rizzo TOK_DSTIP6, 1964e9c8ae7SLuigi Rizzo TOK_SRCIP6, 1974e9c8ae7SLuigi Rizzo 1984e9c8ae7SLuigi Rizzo TOK_IPV4, 1994e9c8ae7SLuigi Rizzo TOK_UNREACH6, 2004e9c8ae7SLuigi Rizzo TOK_RESET6, 2014e9c8ae7SLuigi Rizzo 2024e9c8ae7SLuigi Rizzo TOK_FIB, 2034e9c8ae7SLuigi Rizzo TOK_SETFIB, 204472099c4SLuigi Rizzo TOK_LOOKUP, 205ae99fd0eSLuigi Rizzo TOK_SOCKARG, 206*ae01d73cSAlexander V. Chernikov TOK_SETDSCP, 2074e9c8ae7SLuigi Rizzo }; 2083c0c8717SLuigi Rizzo /* 2093c0c8717SLuigi Rizzo * the following macro returns an error message if we run out of 2103c0c8717SLuigi Rizzo * arguments. 2113c0c8717SLuigi Rizzo */ 212cc4d3c30SLuigi Rizzo #define NEED(_p, msg) {if (!_p) errx(EX_USAGE, msg);} 213cc4d3c30SLuigi Rizzo #define NEED1(msg) {if (!(*av)) errx(EX_USAGE, msg);} 2143c0c8717SLuigi Rizzo 21537133ba7SLuigi Rizzo int pr_u64(uint64_t *pd, int width); 21650a99912SLuigi Rizzo 2173c0c8717SLuigi Rizzo /* memory allocation support */ 2183c0c8717SLuigi Rizzo void *safe_calloc(size_t number, size_t size); 2193c0c8717SLuigi Rizzo void *safe_realloc(void *ptr, size_t size); 2203c0c8717SLuigi Rizzo 221ead75a59SLuigi Rizzo /* string comparison functions used for historical compatibility */ 2223c0c8717SLuigi Rizzo int _substrcmp(const char *str1, const char* str2); 2234e9c8ae7SLuigi Rizzo int _substrcmp2(const char *str1, const char* str2, const char* str3); 2244e9c8ae7SLuigi Rizzo 225ead75a59SLuigi Rizzo /* utility functions */ 2264e9c8ae7SLuigi Rizzo int match_token(struct _s_x *table, char *string); 227ead75a59SLuigi Rizzo char const *match_value(struct _s_x *p, int value); 228ead75a59SLuigi Rizzo 2294e9c8ae7SLuigi Rizzo int do_cmd(int optname, void *optval, uintptr_t optlen); 2304e9c8ae7SLuigi Rizzo 2314e9c8ae7SLuigi Rizzo struct in6_addr; 2324e9c8ae7SLuigi Rizzo void n2mask(struct in6_addr *mask, int n); 233ead75a59SLuigi Rizzo int contigmask(uint8_t *p, int len); 234ead75a59SLuigi Rizzo 23516e3606fSLuigi Rizzo /* 23616e3606fSLuigi Rizzo * Forward declarations to avoid include way too many headers. 23716e3606fSLuigi Rizzo * C does not allow duplicated typedefs, so we use the base struct 23816e3606fSLuigi Rizzo * that the typedef points to. 23916e3606fSLuigi Rizzo * Should the typedefs use a different type, the compiler will 24016e3606fSLuigi Rizzo * still detect the change when compiling the body of the 24116e3606fSLuigi Rizzo * functions involved, so we do not lose error checking. 24216e3606fSLuigi Rizzo */ 24316e3606fSLuigi Rizzo struct _ipfw_insn; 24423c608c8SLuigi Rizzo struct _ipfw_insn_altq; 24516e3606fSLuigi Rizzo struct _ipfw_insn_u32; 24616e3606fSLuigi Rizzo struct _ipfw_insn_ip6; 24716e3606fSLuigi Rizzo struct _ipfw_insn_icmp6; 2483c0c8717SLuigi Rizzo 2493c0c8717SLuigi Rizzo /* 2503c0c8717SLuigi Rizzo * The reserved set numer. This is a constant in ip_fw.h 2513c0c8717SLuigi Rizzo * but we store it in a variable so other files do not depend 2523c0c8717SLuigi Rizzo * in that header just for one constant. 2533c0c8717SLuigi Rizzo */ 2543c0c8717SLuigi Rizzo extern int resvd_set_number; 2553c0c8717SLuigi Rizzo 256ead75a59SLuigi Rizzo /* first-level command handlers */ 257cc4d3c30SLuigi Rizzo void ipfw_add(char *av[]); 2583c0c8717SLuigi Rizzo void ipfw_show_nat(int ac, char **av); 2593c0c8717SLuigi Rizzo void ipfw_config_pipe(int ac, char **av); 2603c0c8717SLuigi Rizzo void ipfw_config_nat(int ac, char **av); 261cc4d3c30SLuigi Rizzo void ipfw_sets_handler(char *av[]); 2623c0c8717SLuigi Rizzo void ipfw_table_handler(int ac, char *av[]); 263cc4d3c30SLuigi Rizzo void ipfw_sysctl_handler(char *av[], int which); 264cc4d3c30SLuigi Rizzo void ipfw_delete(char *av[]); 2653c0c8717SLuigi Rizzo void ipfw_flush(int force); 2663c0c8717SLuigi Rizzo void ipfw_zero(int ac, char *av[], int optname); 2673c0c8717SLuigi Rizzo void ipfw_list(int ac, char *av[], int show_counters); 2683c0c8717SLuigi Rizzo 26923c608c8SLuigi Rizzo /* altq.c */ 27023c608c8SLuigi Rizzo void altq_set_enabled(int enabled); 27123c608c8SLuigi Rizzo u_int32_t altq_name_to_qid(const char *name); 27223c608c8SLuigi Rizzo 27323c608c8SLuigi Rizzo void print_altq_cmd(struct _ipfw_insn_altq *altqptr); 27423c608c8SLuigi Rizzo 275ead75a59SLuigi Rizzo /* dummynet.c */ 276cc4d3c30SLuigi Rizzo void dummynet_list(int ac, char *av[], int show_counters); 277cc4d3c30SLuigi Rizzo void dummynet_flush(void); 2784e9c8ae7SLuigi Rizzo int ipfw_delete_pipe(int pipe_or_queue, int n); 2794e9c8ae7SLuigi Rizzo 280ead75a59SLuigi Rizzo /* ipv6.c */ 281ead75a59SLuigi Rizzo void print_unreach6_code(uint16_t code); 28216e3606fSLuigi Rizzo void print_ip6(struct _ipfw_insn_ip6 *cmd, char const *s); 28316e3606fSLuigi Rizzo void print_flow6id(struct _ipfw_insn_u32 *cmd); 28416e3606fSLuigi Rizzo void print_icmp6types(struct _ipfw_insn_u32 *cmd); 28516e3606fSLuigi Rizzo void print_ext6hdr(struct _ipfw_insn *cmd ); 286ead75a59SLuigi Rizzo 287579ed7bdSAlexander V. Chernikov struct _ipfw_insn *add_srcip6(struct _ipfw_insn *cmd, char *av, int cblen); 288579ed7bdSAlexander V. Chernikov struct _ipfw_insn *add_dstip6(struct _ipfw_insn *cmd, char *av, int cblen); 289ead75a59SLuigi Rizzo 290579ed7bdSAlexander V. Chernikov void fill_flow6(struct _ipfw_insn_u32 *cmd, char *av, int cblen); 291ead75a59SLuigi Rizzo void fill_unreach6_code(u_short *codep, char *str); 292579ed7bdSAlexander V. Chernikov void fill_icmp6types(struct _ipfw_insn_icmp6 *cmd, char *av, int cblen); 29316e3606fSLuigi Rizzo int fill_ext6hdr(struct _ipfw_insn *cmd, char *av); 294