1d17aef79SPedro F. Giffuni /*-
2cc4d3c30SLuigi Rizzo * Copyright (c) 2002-2003,2010 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 * Command line interface for IP firewall facility
193c0c8717SLuigi Rizzo */
203c0c8717SLuigi Rizzo
213c0c8717SLuigi Rizzo #include <sys/wait.h>
223c0c8717SLuigi Rizzo #include <ctype.h>
233c0c8717SLuigi Rizzo #include <err.h>
243c0c8717SLuigi Rizzo #include <errno.h>
253c0c8717SLuigi Rizzo #include <signal.h>
263c0c8717SLuigi Rizzo #include <stdio.h>
273c0c8717SLuigi Rizzo #include <stdlib.h>
283c0c8717SLuigi Rizzo #include <string.h>
293c0c8717SLuigi Rizzo #include <sysexits.h>
303c0c8717SLuigi Rizzo #include <unistd.h>
313599da15SGoran Mekic #include <libgen.h>
323c0c8717SLuigi Rizzo
333c0c8717SLuigi Rizzo #include "ipfw2.h"
343c0c8717SLuigi Rizzo
353c0c8717SLuigi Rizzo static void
help(void)363c0c8717SLuigi Rizzo help(void)
373c0c8717SLuigi Rizzo {
380b95680eSKristof Provost if (is_ipfw()) {
393c0c8717SLuigi Rizzo fprintf(stderr,
403c0c8717SLuigi Rizzo "ipfw syntax summary (but please do read the ipfw(8) manpage):\n\n"
413c0c8717SLuigi Rizzo "\tipfw [-abcdefhnNqStTv] <command>\n\n"
423c0c8717SLuigi Rizzo "where <command> is one of the following:\n\n"
433c0c8717SLuigi Rizzo "add [num] [set N] [prob x] RULE-BODY\n"
443c0c8717SLuigi Rizzo "{pipe|queue} N config PIPE-BODY\n"
453c0c8717SLuigi Rizzo "[pipe|queue] {zero|delete|show} [N{,N}]\n"
46*b6c90b90SDamjan Jovanovic "nat N config {ip IPADDR|if IFNAME|log|deny_in|same_ports|unreg_only|unreg_cgn|udp_eim|\n"
4775b89337SAlexander V. Chernikov " reset|reverse|proxy_only|redirect_addr linkspec|\n"
48a08cdb6cSNeel Chauhan " redirect_port linkspec|redirect_proto linkspec|\n"
49a08cdb6cSNeel Chauhan " port_range lower-upper}\n"
503c0c8717SLuigi Rizzo "set [disable N... enable N...] | move [rule] X to Y | swap X Y | show\n"
513c0c8717SLuigi Rizzo "set N {show|list|zero|resetlog|delete} [N{,N}] | flush\n"
523c0c8717SLuigi Rizzo "table N {add ip[/bits] [value] | delete ip[/bits] | flush | list}\n"
533c0c8717SLuigi Rizzo "table all {flush | list}\n"
543c0c8717SLuigi Rizzo "\n"
553c0c8717SLuigi Rizzo "RULE-BODY: check-state [PARAMS] | ACTION [PARAMS] ADDR [OPTION_LIST]\n"
563c0c8717SLuigi Rizzo "ACTION: check-state | allow | count | deny | unreach{,6} CODE |\n"
573c0c8717SLuigi Rizzo " skipto N | {divert|tee} PORT | forward ADDR |\n"
58eb2e4119SPaolo Pisati " pipe N | queue N | nat N | setfib FIB | reass\n"
593c0c8717SLuigi Rizzo "PARAMS: [log [logamount LOGLIMIT]] [altq QUEUE_NAME]\n"
603c0c8717SLuigi Rizzo "ADDR: [ MAC dst src ether_type ] \n"
613c0c8717SLuigi Rizzo " [ ip from IPADDR [ PORT ] to IPADDR [ PORTLIST ] ]\n"
623c0c8717SLuigi Rizzo " [ ipv6|ip6 from IP6ADDR [ PORT ] to IP6ADDR [ PORTLIST ] ]\n"
633c0c8717SLuigi Rizzo "IPADDR: [not] { any | me | ip/bits{x,y,z} | table(t[,v]) | IPLIST }\n"
643c0c8717SLuigi Rizzo "IP6ADDR: [not] { any | me | me6 | ip6/bits | IP6LIST }\n"
653c0c8717SLuigi Rizzo "IP6LIST: { ip6 | ip6/bits }[,IP6LIST]\n"
663c0c8717SLuigi Rizzo "IPLIST: { ip | ip/bits | ip:mask }[,IPLIST]\n"
673c0c8717SLuigi Rizzo "OPTION_LIST: OPTION [OPTION_LIST]\n"
683c0c8717SLuigi Rizzo "OPTION: bridged | diverted | diverted-loopback | diverted-output |\n"
693c0c8717SLuigi Rizzo " {dst-ip|src-ip} IPADDR | {dst-ip6|src-ip6|dst-ipv6|src-ipv6} IP6ADDR |\n"
703c0c8717SLuigi Rizzo " {dst-port|src-port} LIST |\n"
713c0c8717SLuigi Rizzo " estab | frag | {gid|uid} N | icmptypes LIST | in | out | ipid LIST |\n"
723c0c8717SLuigi Rizzo " iplen LIST | ipoptions SPEC | ipprecedence | ipsec | iptos SPEC |\n"
733c0c8717SLuigi Rizzo " ipttl LIST | ipversion VER | keep-state | layer2 | limit ... |\n"
743c0c8717SLuigi Rizzo " icmp6types LIST | ext6hdr LIST | flow-id N[,N] | fib FIB |\n"
753c0c8717SLuigi Rizzo " mac ... | mac-type LIST | proto LIST | {recv|xmit|via} {IF|IPADDR} |\n"
763c0c8717SLuigi Rizzo " setup | {tcpack|tcpseq|tcpwin} NN | tcpflags SPEC | tcpoptions SPEC |\n"
773c0c8717SLuigi Rizzo " tcpdatalen LIST | verrevpath | versrcreach | antispoof\n"
783c0c8717SLuigi Rizzo );
790b95680eSKristof Provost } else {
800b95680eSKristof Provost fprintf(stderr,
810b95680eSKristof Provost "dnctl syntax summary (but please do read the dnctl(8) manpage):\n\n"
820b95680eSKristof Provost "\tdnctl [-hnsv] <command>\n\n"
830b95680eSKristof Provost "where <command> is one of the following:\n\n"
840b95680eSKristof Provost "[pipe|queue|sched] N config PIPE-BODY\n"
850b95680eSKristof Provost "[pipe|queue|sched] {delete|list|show} [N{,N}]\n"
860b95680eSKristof Provost "\n"
870b95680eSKristof Provost );
880b95680eSKristof Provost }
893c0c8717SLuigi Rizzo
903c0c8717SLuigi Rizzo exit(0);
913c0c8717SLuigi Rizzo }
923c0c8717SLuigi Rizzo
933c0c8717SLuigi Rizzo /*
943c0c8717SLuigi Rizzo * Called with the arguments, including program name because getopt
953c0c8717SLuigi Rizzo * wants it to be present.
963c0c8717SLuigi Rizzo * Returns 0 if successful, 1 if empty command, errx() in case of errors.
97cc4d3c30SLuigi Rizzo * First thing we do is process parameters creating an argv[] array
98cc4d3c30SLuigi Rizzo * which includes the program name and a NULL entry at the end.
99cc4d3c30SLuigi Rizzo * If we are called with a single string, we split it on whitespace.
100cc4d3c30SLuigi Rizzo * Also, arguments with a trailing ',' are joined to the next one.
1013b6dc18eSEitan Adler * The pointers (av[]) and data are in a single chunk of memory.
102cc4d3c30SLuigi Rizzo * av[0] points to the original program name, all other entries
103cc4d3c30SLuigi Rizzo * point into the allocated chunk.
1043c0c8717SLuigi Rizzo */
1053c0c8717SLuigi Rizzo static int
ipfw_main(int oldac,char ** oldav)1063c0c8717SLuigi Rizzo ipfw_main(int oldac, char **oldav)
1073c0c8717SLuigi Rizzo {
108cc4d3c30SLuigi Rizzo int ch, ac;
1093c0c8717SLuigi Rizzo const char *errstr;
1103c0c8717SLuigi Rizzo char **av, **save_av;
1113c0c8717SLuigi Rizzo int do_acct = 0; /* Show packet/byte count */
1121fd3fc69SLuigi Rizzo int try_next = 0; /* set if pipe cmd not found */
113cc4d3c30SLuigi Rizzo int av_size; /* compute the av size */
114cc4d3c30SLuigi Rizzo char *av_p; /* used to build the av list */
1153c0c8717SLuigi Rizzo
1163c0c8717SLuigi Rizzo #define WHITESP " \t\f\v\n\r"
1173c0c8717SLuigi Rizzo if (oldac < 2)
1183c0c8717SLuigi Rizzo return 1; /* need at least one argument */
1193c0c8717SLuigi Rizzo
1203c0c8717SLuigi Rizzo if (oldac == 2) {
1213c0c8717SLuigi Rizzo /*
122cc4d3c30SLuigi Rizzo * If we are called with one argument, try to split it into
123cc4d3c30SLuigi Rizzo * words for subsequent parsing. Spaces after a ',' are
124cc4d3c30SLuigi Rizzo * removed by copying the string in-place.
1253c0c8717SLuigi Rizzo */
1263c0c8717SLuigi Rizzo char *arg = oldav[1]; /* The string is the first arg. */
1273c0c8717SLuigi Rizzo int l = strlen(arg);
1283c0c8717SLuigi Rizzo int copy = 0; /* 1 if we need to copy, 0 otherwise */
1293c0c8717SLuigi Rizzo int i, j;
1303c0c8717SLuigi Rizzo
1313c0c8717SLuigi Rizzo for (i = j = 0; i < l; i++) {
1323c0c8717SLuigi Rizzo if (arg[i] == '#') /* comment marker */
1333c0c8717SLuigi Rizzo break;
1343c0c8717SLuigi Rizzo if (copy) {
1353c0c8717SLuigi Rizzo arg[j++] = arg[i];
136b3608ae1SEd Schouten copy = !strchr("," WHITESP, arg[i]);
1373c0c8717SLuigi Rizzo } else {
138b3608ae1SEd Schouten copy = !strchr(WHITESP, arg[i]);
1393c0c8717SLuigi Rizzo if (copy)
1403c0c8717SLuigi Rizzo arg[j++] = arg[i];
1413c0c8717SLuigi Rizzo }
1423c0c8717SLuigi Rizzo }
1433c0c8717SLuigi Rizzo if (!copy && j > 0) /* last char was a 'blank', remove it */
1443c0c8717SLuigi Rizzo j--;
1453c0c8717SLuigi Rizzo l = j; /* the new argument length */
1463c0c8717SLuigi Rizzo arg[j++] = '\0';
1473c0c8717SLuigi Rizzo if (l == 0) /* empty string! */
1483c0c8717SLuigi Rizzo return 1;
1493c0c8717SLuigi Rizzo
1503c0c8717SLuigi Rizzo /*
1513c0c8717SLuigi Rizzo * First, count number of arguments. Because of the previous
1523c0c8717SLuigi Rizzo * processing, this is just the number of blanks plus 1.
1533c0c8717SLuigi Rizzo */
1543c0c8717SLuigi Rizzo for (i = 0, ac = 1; i < l; i++)
155b3608ae1SEd Schouten if (strchr(WHITESP, arg[i]) != NULL)
1563c0c8717SLuigi Rizzo ac++;
1573c0c8717SLuigi Rizzo
1583c0c8717SLuigi Rizzo /*
159cc4d3c30SLuigi Rizzo * Allocate the argument list structure as a single block
160cc4d3c30SLuigi Rizzo * of memory, containing pointers and the argument
161cc4d3c30SLuigi Rizzo * strings. We include one entry for the program name
162cc4d3c30SLuigi Rizzo * because getopt expects it, and a NULL at the end
163cc4d3c30SLuigi Rizzo * to simplify further parsing.
1643c0c8717SLuigi Rizzo */
165cc4d3c30SLuigi Rizzo ac++; /* add 1 for the program name */
166cc4d3c30SLuigi Rizzo av_size = (ac+1) * sizeof(char *) + l + 1;
167cc4d3c30SLuigi Rizzo av = safe_calloc(av_size, 1);
1683c0c8717SLuigi Rizzo
1693c0c8717SLuigi Rizzo /*
170cc4d3c30SLuigi Rizzo * Init the argument pointer to the end of the array
171cc4d3c30SLuigi Rizzo * and copy arguments from arg[] to av[]. For each one,
1723c0c8717SLuigi Rizzo * j is the initial character, i is the one past the end.
1733c0c8717SLuigi Rizzo */
174cc4d3c30SLuigi Rizzo av_p = (char *)&av[ac+1];
175cc4d3c30SLuigi Rizzo for (ac = 1, i = j = 0; i < l; i++) {
176b3608ae1SEd Schouten if (strchr(WHITESP, arg[i]) != NULL || i == l-1) {
1773c0c8717SLuigi Rizzo if (i == l-1)
1783c0c8717SLuigi Rizzo i++;
179cc4d3c30SLuigi Rizzo bcopy(arg+j, av_p, i-j);
180cc4d3c30SLuigi Rizzo av[ac] = av_p;
1814b85a12fSUlrich Spörlein av_p += i-j; /* the length of the string */
182cc4d3c30SLuigi Rizzo *av_p++ = '\0';
1833c0c8717SLuigi Rizzo ac++;
1843c0c8717SLuigi Rizzo j = i + 1;
1853c0c8717SLuigi Rizzo }
186cc4d3c30SLuigi Rizzo }
1873c0c8717SLuigi Rizzo } else {
1883c0c8717SLuigi Rizzo /*
1893c0c8717SLuigi Rizzo * If an argument ends with ',' join with the next one.
1903c0c8717SLuigi Rizzo */
191cc4d3c30SLuigi Rizzo int first, i, l=0;
1923c0c8717SLuigi Rizzo
193cc4d3c30SLuigi Rizzo /*
194cc4d3c30SLuigi Rizzo * Allocate the argument list structure as a single block
195cc4d3c30SLuigi Rizzo * of memory, containing both pointers and the argument
196cc4d3c30SLuigi Rizzo * strings. We include some space for the program name
197cc4d3c30SLuigi Rizzo * because getopt expects it.
198cc4d3c30SLuigi Rizzo * We add an extra pointer to the end of the array,
199cc4d3c30SLuigi Rizzo * to make simpler further parsing.
200cc4d3c30SLuigi Rizzo */
201cc4d3c30SLuigi Rizzo for (i=0; i<oldac; i++)
202cc4d3c30SLuigi Rizzo l += strlen(oldav[i]);
203cc4d3c30SLuigi Rizzo
204cc4d3c30SLuigi Rizzo av_size = (oldac+1) * sizeof(char *) + l + oldac;
205cc4d3c30SLuigi Rizzo av = safe_calloc(av_size, 1);
206cc4d3c30SLuigi Rizzo
207cc4d3c30SLuigi Rizzo /*
208cc4d3c30SLuigi Rizzo * Init the argument pointer to the end of the array
209cc4d3c30SLuigi Rizzo * and copy arguments from arg[] to av[]
210cc4d3c30SLuigi Rizzo */
211cc4d3c30SLuigi Rizzo av_p = (char *)&av[oldac+1];
2123c0c8717SLuigi Rizzo for (first = i = ac = 1, l = 0; i < oldac; i++) {
2133c0c8717SLuigi Rizzo char *arg = oldav[i];
2143c0c8717SLuigi Rizzo int k = strlen(arg);
2153c0c8717SLuigi Rizzo
2163c0c8717SLuigi Rizzo l += k;
2173c0c8717SLuigi Rizzo if (arg[k-1] != ',' || i == oldac-1) {
2183c0c8717SLuigi Rizzo /* Time to copy. */
219cc4d3c30SLuigi Rizzo av[ac] = av_p;
2203c0c8717SLuigi Rizzo for (l=0; first <= i; first++) {
221cc4d3c30SLuigi Rizzo strcat(av_p, oldav[first]);
222cc4d3c30SLuigi Rizzo av_p += strlen(oldav[first]);
2233c0c8717SLuigi Rizzo }
224cc4d3c30SLuigi Rizzo *av_p++ = '\0';
2253c0c8717SLuigi Rizzo ac++;
2263c0c8717SLuigi Rizzo l = 0;
2273c0c8717SLuigi Rizzo first = i+1;
2283c0c8717SLuigi Rizzo }
2293c0c8717SLuigi Rizzo }
2303c0c8717SLuigi Rizzo }
2313c0c8717SLuigi Rizzo
232cc4d3c30SLuigi Rizzo /*
233cc4d3c30SLuigi Rizzo * set the progname pointer to the original string
234cc4d3c30SLuigi Rizzo * and terminate the array with null
235cc4d3c30SLuigi Rizzo */
236cc4d3c30SLuigi Rizzo av[0] = oldav[0];
237cc4d3c30SLuigi Rizzo av[ac] = NULL;
238cc4d3c30SLuigi Rizzo
2393c0c8717SLuigi Rizzo /* Set the force flag for non-interactive processes */
24056707beeSMark Johnston if (!g_co.do_force)
24156707beeSMark Johnston g_co.do_force = !isatty(STDIN_FILENO);
2423c0c8717SLuigi Rizzo
243cc4d3c30SLuigi Rizzo #ifdef EMULATE_SYSCTL /* sysctl emulation */
2440b95680eSKristof Provost if (is_ipfw() && ac >= 2 &&
2450b95680eSKristof Provost !strcmp(av[1], "sysctl")) {
246cc4d3c30SLuigi Rizzo char *s;
247cc4d3c30SLuigi Rizzo int i;
248cc4d3c30SLuigi Rizzo
249cc4d3c30SLuigi Rizzo if (ac != 3) {
250cc4d3c30SLuigi Rizzo printf( "sysctl emulation usage:\n"
251cc4d3c30SLuigi Rizzo " ipfw sysctl name[=value]\n"
252cc4d3c30SLuigi Rizzo " ipfw sysctl -a\n");
253cc4d3c30SLuigi Rizzo return 0;
254cc4d3c30SLuigi Rizzo }
255b3608ae1SEd Schouten s = strchr(av[2], '=');
256cc4d3c30SLuigi Rizzo if (s == NULL) {
257cc4d3c30SLuigi Rizzo s = !strcmp(av[2], "-a") ? NULL : av[2];
258cc4d3c30SLuigi Rizzo sysctlbyname(s, NULL, NULL, NULL, 0);
259cc4d3c30SLuigi Rizzo } else { /* ipfw sysctl x.y.z=value */
260cc4d3c30SLuigi Rizzo /* assume an INT value, will extend later */
261cc4d3c30SLuigi Rizzo if (s[1] == '\0') {
262cc4d3c30SLuigi Rizzo printf("ipfw sysctl: missing value\n\n");
263cc4d3c30SLuigi Rizzo return 0;
264cc4d3c30SLuigi Rizzo }
265cc4d3c30SLuigi Rizzo *s = '\0';
266cc4d3c30SLuigi Rizzo i = strtol(s+1, NULL, 0);
267cc4d3c30SLuigi Rizzo sysctlbyname(av[2], NULL, NULL, &i, sizeof(int));
268cc4d3c30SLuigi Rizzo }
269cc4d3c30SLuigi Rizzo return 0;
270cc4d3c30SLuigi Rizzo }
271cc4d3c30SLuigi Rizzo #endif
272cc4d3c30SLuigi Rizzo
2733c0c8717SLuigi Rizzo /* Save arguments for final freeing of memory. */
2743c0c8717SLuigi Rizzo save_av = av;
2753c0c8717SLuigi Rizzo
2763c0c8717SLuigi Rizzo optind = optreset = 1; /* restart getopt() */
2770b95680eSKristof Provost if (is_ipfw()) {
2789f44a47fSAlexander V. Chernikov while ((ch = getopt(ac, av, "abcdDefhinNp:qs:STtvx")) != -1)
2793c0c8717SLuigi Rizzo switch (ch) {
2803c0c8717SLuigi Rizzo case 'a':
2813c0c8717SLuigi Rizzo do_acct = 1;
2823c0c8717SLuigi Rizzo break;
2833c0c8717SLuigi Rizzo
2843c0c8717SLuigi Rizzo case 'b':
28556707beeSMark Johnston g_co.comment_only = 1;
28656707beeSMark Johnston g_co.do_compact = 1;
2873c0c8717SLuigi Rizzo break;
2883c0c8717SLuigi Rizzo
2893c0c8717SLuigi Rizzo case 'c':
29056707beeSMark Johnston g_co.do_compact = 1;
2913c0c8717SLuigi Rizzo break;
2923c0c8717SLuigi Rizzo
2933c0c8717SLuigi Rizzo case 'd':
29456707beeSMark Johnston g_co.do_dynamic = 1;
2953c0c8717SLuigi Rizzo break;
2963c0c8717SLuigi Rizzo
297d66f9c86SAndrey V. Elsukov case 'D':
29856707beeSMark Johnston g_co.do_dynamic = 2;
299d66f9c86SAndrey V. Elsukov break;
300d66f9c86SAndrey V. Elsukov
3013c0c8717SLuigi Rizzo case 'e':
302d66f9c86SAndrey V. Elsukov /* nop for compatibility */
3033c0c8717SLuigi Rizzo break;
3043c0c8717SLuigi Rizzo
3053c0c8717SLuigi Rizzo case 'f':
30656707beeSMark Johnston g_co.do_force = 1;
3073c0c8717SLuigi Rizzo break;
3083c0c8717SLuigi Rizzo
3093c0c8717SLuigi Rizzo case 'h': /* help */
310cc4d3c30SLuigi Rizzo free(save_av);
3113c0c8717SLuigi Rizzo help();
3123c0c8717SLuigi Rizzo break; /* NOTREACHED */
3133c0c8717SLuigi Rizzo
3143c0c8717SLuigi Rizzo case 'i':
31556707beeSMark Johnston g_co.do_value_as_ip = 1;
3163c0c8717SLuigi Rizzo break;
3173c0c8717SLuigi Rizzo
3183c0c8717SLuigi Rizzo case 'n':
31956707beeSMark Johnston g_co.test_only = 1;
3203c0c8717SLuigi Rizzo break;
3213c0c8717SLuigi Rizzo
3223c0c8717SLuigi Rizzo case 'N':
32356707beeSMark Johnston g_co.do_resolv = 1;
3243c0c8717SLuigi Rizzo break;
3253c0c8717SLuigi Rizzo
3260dba401dSAndrey V. Elsukov case 'p':
3270dba401dSAndrey V. Elsukov errx(EX_USAGE, "An absolute pathname must be used "
3280dba401dSAndrey V. Elsukov "with -p option.");
3299dad7387SEitan Adler /* NOTREACHED */
3300dba401dSAndrey V. Elsukov
3313c0c8717SLuigi Rizzo case 'q':
33256707beeSMark Johnston g_co.do_quiet = 1;
3333c0c8717SLuigi Rizzo break;
3343c0c8717SLuigi Rizzo
3353c0c8717SLuigi Rizzo case 's': /* sort */
33656707beeSMark Johnston g_co.do_sort = atoi(optarg);
3373c0c8717SLuigi Rizzo break;
3383c0c8717SLuigi Rizzo
3393c0c8717SLuigi Rizzo case 'S':
34056707beeSMark Johnston g_co.show_sets = 1;
3413c0c8717SLuigi Rizzo break;
3423c0c8717SLuigi Rizzo
3433c0c8717SLuigi Rizzo case 't':
34456707beeSMark Johnston g_co.do_time = TIMESTAMP_STRING;
3453c0c8717SLuigi Rizzo break;
3463c0c8717SLuigi Rizzo
3473c0c8717SLuigi Rizzo case 'T':
34856707beeSMark Johnston g_co.do_time = TIMESTAMP_NUMERIC;
3493c0c8717SLuigi Rizzo break;
3503c0c8717SLuigi Rizzo
3513c0c8717SLuigi Rizzo case 'v': /* verbose */
35256707beeSMark Johnston g_co.verbose = 1;
3533c0c8717SLuigi Rizzo break;
3543c0c8717SLuigi Rizzo
3559f44a47fSAlexander V. Chernikov case 'x': /* debug output */
3569f44a47fSAlexander V. Chernikov g_co.debug_only = 1;
3579f44a47fSAlexander V. Chernikov break;
3589f44a47fSAlexander V. Chernikov
3593c0c8717SLuigi Rizzo default:
360cc4d3c30SLuigi Rizzo free(save_av);
3613c0c8717SLuigi Rizzo return 1;
3623c0c8717SLuigi Rizzo }
3630b95680eSKristof Provost } else {
3640b95680eSKristof Provost while ((ch = getopt(ac, av, "hns:v")) != -1)
3650b95680eSKristof Provost switch (ch) {
3660b95680eSKristof Provost
3670b95680eSKristof Provost case 'h': /* help */
3680b95680eSKristof Provost free(save_av);
3690b95680eSKristof Provost help();
3700b95680eSKristof Provost break; /* NOTREACHED */
3710b95680eSKristof Provost
3720b95680eSKristof Provost case 'n':
3730b95680eSKristof Provost g_co.test_only = 1;
3740b95680eSKristof Provost break;
3750b95680eSKristof Provost
3760b95680eSKristof Provost case 's': /* sort */
3770b95680eSKristof Provost g_co.do_sort = atoi(optarg);
3780b95680eSKristof Provost break;
3790b95680eSKristof Provost
3800b95680eSKristof Provost case 'v': /* verbose */
3810b95680eSKristof Provost g_co.verbose = 1;
3820b95680eSKristof Provost break;
3830b95680eSKristof Provost
3840b95680eSKristof Provost default:
3850b95680eSKristof Provost free(save_av);
3860b95680eSKristof Provost return 1;
3870b95680eSKristof Provost }
3880b95680eSKristof Provost
3890b95680eSKristof Provost }
3903c0c8717SLuigi Rizzo
3913c0c8717SLuigi Rizzo ac -= optind;
3923c0c8717SLuigi Rizzo av += optind;
3933c0c8717SLuigi Rizzo NEED1("bad arguments, for usage summary ``ipfw''");
3943c0c8717SLuigi Rizzo
3953c0c8717SLuigi Rizzo /*
3963c0c8717SLuigi Rizzo * An undocumented behaviour of ipfw1 was to allow rule numbers first,
3973c0c8717SLuigi Rizzo * e.g. "100 add allow ..." instead of "add 100 allow ...".
3983c0c8717SLuigi Rizzo * In case, swap first and second argument to get the normal form.
3993c0c8717SLuigi Rizzo */
4003c0c8717SLuigi Rizzo if (ac > 1 && isdigit(*av[0])) {
4013c0c8717SLuigi Rizzo char *p = av[0];
4023c0c8717SLuigi Rizzo
4033c0c8717SLuigi Rizzo av[0] = av[1];
4043c0c8717SLuigi Rizzo av[1] = p;
4053c0c8717SLuigi Rizzo }
4063c0c8717SLuigi Rizzo
4073c0c8717SLuigi Rizzo /*
4083c0c8717SLuigi Rizzo * Optional: pipe, queue or nat.
4093c0c8717SLuigi Rizzo */
41056707beeSMark Johnston g_co.do_nat = 0;
41156707beeSMark Johnston g_co.do_pipe = 0;
41256707beeSMark Johnston g_co.use_set = 0;
4130b95680eSKristof Provost if (is_ipfw() && !strncmp(*av, "nat", strlen(*av)))
41456707beeSMark Johnston g_co.do_nat = 1;
4153c0c8717SLuigi Rizzo else if (!strncmp(*av, "pipe", strlen(*av)))
41656707beeSMark Johnston g_co.do_pipe = 1;
4173c0c8717SLuigi Rizzo else if (_substrcmp(*av, "queue") == 0)
41856707beeSMark Johnston g_co.do_pipe = 2;
419cc4d3c30SLuigi Rizzo else if (_substrcmp(*av, "flowset") == 0)
42056707beeSMark Johnston g_co.do_pipe = 2;
421cc4d3c30SLuigi Rizzo else if (_substrcmp(*av, "sched") == 0)
42256707beeSMark Johnston g_co.do_pipe = 3;
4230b95680eSKristof Provost else if (is_ipfw() && !strncmp(*av, "set", strlen(*av))) {
4243c0c8717SLuigi Rizzo if (ac > 1 && isdigit(av[1][0])) {
42556707beeSMark Johnston g_co.use_set = strtonum(av[1], 0, resvd_set_number,
4263c0c8717SLuigi Rizzo &errstr);
4273c0c8717SLuigi Rizzo if (errstr)
4283c0c8717SLuigi Rizzo errx(EX_DATAERR,
4293c0c8717SLuigi Rizzo "invalid set number %s\n", av[1]);
43056707beeSMark Johnston ac -= 2; av += 2; g_co.use_set++;
4313c0c8717SLuigi Rizzo }
4323c0c8717SLuigi Rizzo }
4333c0c8717SLuigi Rizzo
43456707beeSMark Johnston if (g_co.do_pipe || g_co.do_nat) {
4353c0c8717SLuigi Rizzo ac--;
4363c0c8717SLuigi Rizzo av++;
4373c0c8717SLuigi Rizzo }
4383c0c8717SLuigi Rizzo NEED1("missing command");
4393c0c8717SLuigi Rizzo
4403c0c8717SLuigi Rizzo /*
4413c0c8717SLuigi Rizzo * For pipes, queues and nats we normally say 'nat|pipe NN config'
4423c0c8717SLuigi Rizzo * but the code is easier to parse as 'nat|pipe config NN'
4433c0c8717SLuigi Rizzo * so we swap the two arguments.
4443c0c8717SLuigi Rizzo */
44556707beeSMark Johnston if ((g_co.do_pipe || g_co.do_nat) && ac > 1 && isdigit(*av[0])) {
4463c0c8717SLuigi Rizzo char *p = av[0];
4473c0c8717SLuigi Rizzo
4483c0c8717SLuigi Rizzo av[0] = av[1];
4493c0c8717SLuigi Rizzo av[1] = p;
4503c0c8717SLuigi Rizzo }
4513c0c8717SLuigi Rizzo
4520b95680eSKristof Provost if (! is_ipfw() && g_co.do_pipe == 0) {
4530b95680eSKristof Provost help();
4540b95680eSKristof Provost }
4550b95680eSKristof Provost
45656707beeSMark Johnston if (g_co.use_set == 0) {
4570b95680eSKristof Provost if (is_ipfw() && _substrcmp(*av, "add") == 0)
458cc4d3c30SLuigi Rizzo ipfw_add(av);
45956707beeSMark Johnston else if (g_co.do_nat && _substrcmp(*av, "show") == 0)
4603c0c8717SLuigi Rizzo ipfw_show_nat(ac, av);
46156707beeSMark Johnston else if (g_co.do_pipe && _substrcmp(*av, "config") == 0)
4623c0c8717SLuigi Rizzo ipfw_config_pipe(ac, av);
46356707beeSMark Johnston else if (g_co.do_nat && _substrcmp(*av, "config") == 0)
4643c0c8717SLuigi Rizzo ipfw_config_nat(ac, av);
4650b95680eSKristof Provost else if (is_ipfw() && _substrcmp(*av, "set") == 0)
466cc4d3c30SLuigi Rizzo ipfw_sets_handler(av);
4670b95680eSKristof Provost else if (is_ipfw() && _substrcmp(*av, "table") == 0)
4683c0c8717SLuigi Rizzo ipfw_table_handler(ac, av);
4690b95680eSKristof Provost else if (is_ipfw() && _substrcmp(*av, "enable") == 0)
470cc4d3c30SLuigi Rizzo ipfw_sysctl_handler(av, 1);
4710b95680eSKristof Provost else if (is_ipfw() && _substrcmp(*av, "disable") == 0)
472cc4d3c30SLuigi Rizzo ipfw_sysctl_handler(av, 0);
4733c0c8717SLuigi Rizzo else
4743c0c8717SLuigi Rizzo try_next = 1;
4753c0c8717SLuigi Rizzo }
4763c0c8717SLuigi Rizzo
47756707beeSMark Johnston if (g_co.use_set || try_next) {
4783c0c8717SLuigi Rizzo if (_substrcmp(*av, "delete") == 0)
479cc4d3c30SLuigi Rizzo ipfw_delete(av);
4800b95680eSKristof Provost else if (is_ipfw() && !strncmp(*av, "nat64clat", strlen(*av)))
4815c04f73eSAndrey V. Elsukov ipfw_nat64clat_handler(ac, av);
4820b95680eSKristof Provost else if (is_ipfw() && !strncmp(*av, "nat64stl", strlen(*av)))
483d8caf56eSAndrey V. Elsukov ipfw_nat64stl_handler(ac, av);
4840b95680eSKristof Provost else if (is_ipfw() && !strncmp(*av, "nat64lsn", strlen(*av)))
485d8caf56eSAndrey V. Elsukov ipfw_nat64lsn_handler(ac, av);
4860b95680eSKristof Provost else if (is_ipfw() && !strncmp(*av, "nptv6", strlen(*av)))
487b867e84eSAndrey V. Elsukov ipfw_nptv6_handler(ac, av);
4883c0c8717SLuigi Rizzo else if (_substrcmp(*av, "flush") == 0)
48956707beeSMark Johnston ipfw_flush(g_co.do_force);
4900b95680eSKristof Provost else if (is_ipfw() && _substrcmp(*av, "zero") == 0)
4913c0c8717SLuigi Rizzo ipfw_zero(ac, av, 0 /* IP_FW_ZERO */);
4920b95680eSKristof Provost else if (is_ipfw() && _substrcmp(*av, "resetlog") == 0)
4933c0c8717SLuigi Rizzo ipfw_zero(ac, av, 1 /* IP_FW_RESETLOG */);
4943c0c8717SLuigi Rizzo else if (_substrcmp(*av, "print") == 0 ||
4953c0c8717SLuigi Rizzo _substrcmp(*av, "list") == 0)
4963c0c8717SLuigi Rizzo ipfw_list(ac, av, do_acct);
4973c0c8717SLuigi Rizzo else if (_substrcmp(*av, "show") == 0)
4983c0c8717SLuigi Rizzo ipfw_list(ac, av, 1 /* show counters */);
4990b95680eSKristof Provost else if (is_ipfw() && _substrcmp(*av, "table") == 0)
500ac35ff17SAlexander V. Chernikov ipfw_table_handler(ac, av);
5010b95680eSKristof Provost else if (is_ipfw() && _substrcmp(*av, "internal") == 0)
502358b9d09SAlexander V. Chernikov ipfw_internal_handler(ac, av);
5033c0c8717SLuigi Rizzo else
5043c0c8717SLuigi Rizzo errx(EX_USAGE, "bad command `%s'", *av);
5053c0c8717SLuigi Rizzo }
5063c0c8717SLuigi Rizzo
5073c0c8717SLuigi Rizzo /* Free memory allocated in the argument parsing. */
508cc4d3c30SLuigi Rizzo free(save_av);
5093c0c8717SLuigi Rizzo return 0;
5103c0c8717SLuigi Rizzo }
5113c0c8717SLuigi Rizzo
5123c0c8717SLuigi Rizzo
5133c0c8717SLuigi Rizzo static void
ipfw_readfile(int ac,char * av[])5143c0c8717SLuigi Rizzo ipfw_readfile(int ac, char *av[])
5153c0c8717SLuigi Rizzo {
5163c0c8717SLuigi Rizzo #define MAX_ARGS 32
517796051d6SAndrey V. Elsukov char buf[4096];
5183c0c8717SLuigi Rizzo char *progname = av[0]; /* original program name */
5193c0c8717SLuigi Rizzo const char *cmd = NULL; /* preprocessor name, if any */
5203c0c8717SLuigi Rizzo const char *filename = av[ac-1]; /* file to read */
5213c0c8717SLuigi Rizzo int c, lineno=0;
5223c0c8717SLuigi Rizzo FILE *f = NULL;
5233c0c8717SLuigi Rizzo pid_t preproc = 0;
5243c0c8717SLuigi Rizzo
5259d406e08SKristof Provost if (is_ipfw()) {
5263c0c8717SLuigi Rizzo while ((c = getopt(ac, av, "cfNnp:qS")) != -1) {
5273c0c8717SLuigi Rizzo switch(c) {
5283c0c8717SLuigi Rizzo case 'c':
52956707beeSMark Johnston g_co.do_compact = 1;
5303c0c8717SLuigi Rizzo break;
5313c0c8717SLuigi Rizzo
5323c0c8717SLuigi Rizzo case 'f':
53356707beeSMark Johnston g_co.do_force = 1;
5343c0c8717SLuigi Rizzo break;
5353c0c8717SLuigi Rizzo
5363c0c8717SLuigi Rizzo case 'N':
53756707beeSMark Johnston g_co.do_resolv = 1;
5383c0c8717SLuigi Rizzo break;
5393c0c8717SLuigi Rizzo
5403c0c8717SLuigi Rizzo case 'n':
54156707beeSMark Johnston g_co.test_only = 1;
5423c0c8717SLuigi Rizzo break;
5433c0c8717SLuigi Rizzo
5443c0c8717SLuigi Rizzo case 'p':
5453c0c8717SLuigi Rizzo /*
5463c0c8717SLuigi Rizzo * ipfw -p cmd [args] filename
5473c0c8717SLuigi Rizzo *
5483c0c8717SLuigi Rizzo * We are done with getopt(). All arguments
5493c0c8717SLuigi Rizzo * except the filename go to the preprocessor,
5503c0c8717SLuigi Rizzo * so we need to do the following:
5513c0c8717SLuigi Rizzo * - check that a filename is actually present;
5523c0c8717SLuigi Rizzo * - advance av by optind-1 to skip arguments
5533c0c8717SLuigi Rizzo * already processed;
5543c0c8717SLuigi Rizzo * - decrease ac by optind, to remove the args
5553c0c8717SLuigi Rizzo * already processed and the final filename;
5563c0c8717SLuigi Rizzo * - set the last entry in av[] to NULL so
5573c0c8717SLuigi Rizzo * popen() can detect the end of the array;
5583c0c8717SLuigi Rizzo * - set optind=ac to let getopt() terminate.
5593c0c8717SLuigi Rizzo */
5603c0c8717SLuigi Rizzo if (optind == ac)
5613c0c8717SLuigi Rizzo errx(EX_USAGE, "no filename argument");
5623c0c8717SLuigi Rizzo cmd = optarg;
5633c0c8717SLuigi Rizzo av[ac-1] = NULL;
5643c0c8717SLuigi Rizzo av += optind - 1;
5653c0c8717SLuigi Rizzo ac -= optind;
5663c0c8717SLuigi Rizzo optind = ac;
5673c0c8717SLuigi Rizzo break;
5683c0c8717SLuigi Rizzo
5693c0c8717SLuigi Rizzo case 'q':
57056707beeSMark Johnston g_co.do_quiet = 1;
5713c0c8717SLuigi Rizzo break;
5723c0c8717SLuigi Rizzo
5733c0c8717SLuigi Rizzo case 'S':
57456707beeSMark Johnston g_co.show_sets = 1;
5753c0c8717SLuigi Rizzo break;
5763c0c8717SLuigi Rizzo
5773c0c8717SLuigi Rizzo default:
5783c0c8717SLuigi Rizzo errx(EX_USAGE, "bad arguments, for usage"
5793c0c8717SLuigi Rizzo " summary ``ipfw''");
5803c0c8717SLuigi Rizzo }
5819d406e08SKristof Provost }
5829d406e08SKristof Provost } else {
5839d406e08SKristof Provost while ((c = getopt(ac, av, "nq")) != -1) {
5849d406e08SKristof Provost switch(c) {
5859d406e08SKristof Provost case 'n':
5869d406e08SKristof Provost g_co.test_only = 1;
5879d406e08SKristof Provost break;
5883c0c8717SLuigi Rizzo
5899d406e08SKristof Provost case 'q':
5909d406e08SKristof Provost g_co.do_quiet = 1;
5919d406e08SKristof Provost break;
5929d406e08SKristof Provost
5939d406e08SKristof Provost default:
5949d406e08SKristof Provost errx(EX_USAGE, "bad arguments, for usage"
5959d406e08SKristof Provost " summary ``dnctl''");
5969d406e08SKristof Provost }
5979d406e08SKristof Provost }
5983c0c8717SLuigi Rizzo }
5993c0c8717SLuigi Rizzo
6003c0c8717SLuigi Rizzo if (cmd == NULL && ac != optind + 1)
6013c0c8717SLuigi Rizzo errx(EX_USAGE, "extraneous filename arguments %s", av[ac-1]);
6023c0c8717SLuigi Rizzo
6033c0c8717SLuigi Rizzo if ((f = fopen(filename, "r")) == NULL)
6043c0c8717SLuigi Rizzo err(EX_UNAVAILABLE, "fopen: %s", filename);
6053c0c8717SLuigi Rizzo
6063c0c8717SLuigi Rizzo if (cmd != NULL) { /* pipe through preprocessor */
6073c0c8717SLuigi Rizzo int pipedes[2];
6083c0c8717SLuigi Rizzo
6093c0c8717SLuigi Rizzo if (pipe(pipedes) == -1)
6103c0c8717SLuigi Rizzo err(EX_OSERR, "cannot create pipe");
6113c0c8717SLuigi Rizzo
6123c0c8717SLuigi Rizzo preproc = fork();
6133c0c8717SLuigi Rizzo if (preproc == -1)
6143c0c8717SLuigi Rizzo err(EX_OSERR, "cannot fork");
6153c0c8717SLuigi Rizzo
6163c0c8717SLuigi Rizzo if (preproc == 0) {
6173c0c8717SLuigi Rizzo /*
6183c0c8717SLuigi Rizzo * Child, will run the preprocessor with the
6193c0c8717SLuigi Rizzo * file on stdin and the pipe on stdout.
6203c0c8717SLuigi Rizzo */
6213c0c8717SLuigi Rizzo if (dup2(fileno(f), 0) == -1
6223c0c8717SLuigi Rizzo || dup2(pipedes[1], 1) == -1)
6233c0c8717SLuigi Rizzo err(EX_OSERR, "dup2()");
6243c0c8717SLuigi Rizzo fclose(f);
6253c0c8717SLuigi Rizzo close(pipedes[1]);
6263c0c8717SLuigi Rizzo close(pipedes[0]);
6273c0c8717SLuigi Rizzo execvp(cmd, av);
6283c0c8717SLuigi Rizzo err(EX_OSERR, "execvp(%s) failed", cmd);
6293c0c8717SLuigi Rizzo } else { /* parent, will reopen f as the pipe */
6303c0c8717SLuigi Rizzo fclose(f);
6313c0c8717SLuigi Rizzo close(pipedes[1]);
6323c0c8717SLuigi Rizzo if ((f = fdopen(pipedes[0], "r")) == NULL) {
6333c0c8717SLuigi Rizzo int savederrno = errno;
6343c0c8717SLuigi Rizzo
6353c0c8717SLuigi Rizzo (void)kill(preproc, SIGTERM);
6363c0c8717SLuigi Rizzo errno = savederrno;
6373c0c8717SLuigi Rizzo err(EX_OSERR, "fdopen()");
6383c0c8717SLuigi Rizzo }
6393c0c8717SLuigi Rizzo }
6403c0c8717SLuigi Rizzo }
6413c0c8717SLuigi Rizzo
642796051d6SAndrey V. Elsukov while (fgets(buf, sizeof(buf), f)) { /* read commands */
643c0831342SLuigi Rizzo char linename[20];
6443c0c8717SLuigi Rizzo char *args[2];
6453c0c8717SLuigi Rizzo
6463c0c8717SLuigi Rizzo lineno++;
647c0831342SLuigi Rizzo snprintf(linename, sizeof(linename), "Line %d", lineno);
6483c0c8717SLuigi Rizzo setprogname(linename); /* XXX */
6493c0c8717SLuigi Rizzo args[0] = progname;
6503c0c8717SLuigi Rizzo args[1] = buf;
6513c0c8717SLuigi Rizzo ipfw_main(2, args);
6523c0c8717SLuigi Rizzo }
6533c0c8717SLuigi Rizzo fclose(f);
6543c0c8717SLuigi Rizzo if (cmd != NULL) {
6553c0c8717SLuigi Rizzo int status;
6563c0c8717SLuigi Rizzo
6573c0c8717SLuigi Rizzo if (waitpid(preproc, &status, 0) == -1)
6583c0c8717SLuigi Rizzo errx(EX_OSERR, "waitpid()");
6593c0c8717SLuigi Rizzo if (WIFEXITED(status) && WEXITSTATUS(status) != EX_OK)
6603c0c8717SLuigi Rizzo errx(EX_UNAVAILABLE,
6613c0c8717SLuigi Rizzo "preprocessor exited with status %d",
6623c0c8717SLuigi Rizzo WEXITSTATUS(status));
6633c0c8717SLuigi Rizzo else if (WIFSIGNALED(status))
6643c0c8717SLuigi Rizzo errx(EX_UNAVAILABLE,
6653c0c8717SLuigi Rizzo "preprocessor exited with signal %d",
6663c0c8717SLuigi Rizzo WTERMSIG(status));
6673c0c8717SLuigi Rizzo }
6683c0c8717SLuigi Rizzo }
6693c0c8717SLuigi Rizzo
6703c0c8717SLuigi Rizzo int
main(int ac,char * av[])6713c0c8717SLuigi Rizzo main(int ac, char *av[])
6723c0c8717SLuigi Rizzo {
67364290678SLuigi Rizzo #if defined(_WIN32) && defined(TCC)
67464290678SLuigi Rizzo {
67564290678SLuigi Rizzo WSADATA wsaData;
67664290678SLuigi Rizzo int ret=0;
67764290678SLuigi Rizzo unsigned short wVersionRequested = MAKEWORD(2, 2);
67864290678SLuigi Rizzo ret = WSAStartup(wVersionRequested, &wsaData);
67964290678SLuigi Rizzo if (ret != 0) {
68064290678SLuigi Rizzo /* Tell the user that we could not find a usable */
68164290678SLuigi Rizzo /* Winsock DLL. */
68264290678SLuigi Rizzo printf("WSAStartup failed with error: %d\n", ret);
68364290678SLuigi Rizzo return 1;
68464290678SLuigi Rizzo }
68564290678SLuigi Rizzo }
68664290678SLuigi Rizzo #endif
6870b95680eSKristof Provost
6883599da15SGoran Mekic if (strcmp("dnctl", basename(av[0])) == 0)
6890b95680eSKristof Provost g_co.prog = cmdline_prog_dnctl;
6900b95680eSKristof Provost else
6910b95680eSKristof Provost g_co.prog = cmdline_prog_ipfw;
6920b95680eSKristof Provost
6933c0c8717SLuigi Rizzo /*
6943c0c8717SLuigi Rizzo * If the last argument is an absolute pathname, interpret it
6953c0c8717SLuigi Rizzo * as a file to be preprocessed.
6963c0c8717SLuigi Rizzo */
6973c0c8717SLuigi Rizzo
6980dba401dSAndrey V. Elsukov if (ac > 1 && av[ac - 1][0] == '/') {
6990dba401dSAndrey V. Elsukov if (access(av[ac - 1], R_OK) == 0)
7003c0c8717SLuigi Rizzo ipfw_readfile(ac, av);
7010dba401dSAndrey V. Elsukov else
7020dba401dSAndrey V. Elsukov err(EX_USAGE, "pathname: %s", av[ac - 1]);
7030dba401dSAndrey V. Elsukov } else {
7043c0c8717SLuigi Rizzo if (ipfw_main(ac, av)) {
7053c0c8717SLuigi Rizzo errx(EX_USAGE,
7060b95680eSKristof Provost "usage: %s [options]\n"
7070b95680eSKristof Provost "do \"%s -h\" or \"man %s\" for details", av[0],
7080b95680eSKristof Provost av[0], av[0]);
7093c0c8717SLuigi Rizzo }
7103c0c8717SLuigi Rizzo }
7113c0c8717SLuigi Rizzo return EX_OK;
7123c0c8717SLuigi Rizzo }
713