xref: /freebsd/sbin/ipfw/main.c (revision ba3c1f5972d7b90feb6e6da47905ff2757e0fe57)
1 /*-
2  * Copyright (c) 2002-2003,2010 Luigi Rizzo
3  * Copyright (c) 1996 Alex Nash, Paul Traina, Poul-Henning Kamp
4  * Copyright (c) 1994 Ugen J.S.Antsilevich
5  *
6  * Idea and grammar partially left from:
7  * Copyright (c) 1993 Daniel Boulet
8  *
9  * Redistribution and use in source forms, with and without modification,
10  * are permitted provided that this entire comment appears intact.
11  *
12  * Redistribution in binary form may occur without any restrictions.
13  * Obviously, it would be nice if you gave credit where credit is due
14  * but requiring it would be too onerous.
15  *
16  * This software is provided ``AS IS'' without any warranties of any kind.
17  *
18  * Command line interface for IP firewall facility
19  *
20  * $FreeBSD$
21  */
22 
23 #include <sys/wait.h>
24 #include <ctype.h>
25 #include <err.h>
26 #include <errno.h>
27 #include <signal.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sysexits.h>
32 #include <unistd.h>
33 #include <libgen.h>
34 
35 #include "ipfw2.h"
36 
37 static void
38 help(void)
39 {
40 	if (is_ipfw()) {
41 		fprintf(stderr,
42 "ipfw syntax summary (but please do read the ipfw(8) manpage):\n\n"
43 "\tipfw [-abcdefhnNqStTv] <command>\n\n"
44 "where <command> is one of the following:\n\n"
45 "add [num] [set N] [prob x] RULE-BODY\n"
46 "{pipe|queue} N config PIPE-BODY\n"
47 "[pipe|queue] {zero|delete|show} [N{,N}]\n"
48 "nat N config {ip IPADDR|if IFNAME|log|deny_in|same_ports|unreg_only|unreg_cgn|\n"
49 "		reset|reverse|proxy_only|redirect_addr linkspec|\n"
50 "		redirect_port linkspec|redirect_proto linkspec|\n"
51 "		port_range lower-upper}\n"
52 "set [disable N... enable N...] | move [rule] X to Y | swap X Y | show\n"
53 "set N {show|list|zero|resetlog|delete} [N{,N}] | flush\n"
54 "table N {add ip[/bits] [value] | delete ip[/bits] | flush | list}\n"
55 "table all {flush | list}\n"
56 "\n"
57 "RULE-BODY:	check-state [PARAMS] | ACTION [PARAMS] ADDR [OPTION_LIST]\n"
58 "ACTION:	check-state | allow | count | deny | unreach{,6} CODE |\n"
59 "               skipto N | {divert|tee} PORT | forward ADDR |\n"
60 "               pipe N | queue N | nat N | setfib FIB | reass\n"
61 "PARAMS: 	[log [logamount LOGLIMIT]] [altq QUEUE_NAME]\n"
62 "ADDR:		[ MAC dst src ether_type ] \n"
63 "		[ ip from IPADDR [ PORT ] to IPADDR [ PORTLIST ] ]\n"
64 "		[ ipv6|ip6 from IP6ADDR [ PORT ] to IP6ADDR [ PORTLIST ] ]\n"
65 "IPADDR:	[not] { any | me | ip/bits{x,y,z} | table(t[,v]) | IPLIST }\n"
66 "IP6ADDR:	[not] { any | me | me6 | ip6/bits | IP6LIST }\n"
67 "IP6LIST:	{ ip6 | ip6/bits }[,IP6LIST]\n"
68 "IPLIST:	{ ip | ip/bits | ip:mask }[,IPLIST]\n"
69 "OPTION_LIST:	OPTION [OPTION_LIST]\n"
70 "OPTION:	bridged | diverted | diverted-loopback | diverted-output |\n"
71 "	{dst-ip|src-ip} IPADDR | {dst-ip6|src-ip6|dst-ipv6|src-ipv6} IP6ADDR |\n"
72 "	{dst-port|src-port} LIST |\n"
73 "	estab | frag | {gid|uid} N | icmptypes LIST | in | out | ipid LIST |\n"
74 "	iplen LIST | ipoptions SPEC | ipprecedence | ipsec | iptos SPEC |\n"
75 "	ipttl LIST | ipversion VER | keep-state | layer2 | limit ... |\n"
76 "	icmp6types LIST | ext6hdr LIST | flow-id N[,N] | fib FIB |\n"
77 "	mac ... | mac-type LIST | proto LIST | {recv|xmit|via} {IF|IPADDR} |\n"
78 "	setup | {tcpack|tcpseq|tcpwin} NN | tcpflags SPEC | tcpoptions SPEC |\n"
79 "	tcpdatalen LIST | verrevpath | versrcreach | antispoof\n"
80 );
81 	} else {
82 		fprintf(stderr,
83 "dnctl syntax summary (but please do read the dnctl(8) manpage):\n\n"
84 "\tdnctl [-hnsv] <command>\n\n"
85 "where <command> is one of the following:\n\n"
86 "[pipe|queue|sched] N config PIPE-BODY\n"
87 "[pipe|queue|sched] {delete|list|show} [N{,N}]\n"
88 "\n"
89 );
90 	}
91 
92 	exit(0);
93 }
94 
95 /*
96  * Called with the arguments, including program name because getopt
97  * wants it to be present.
98  * Returns 0 if successful, 1 if empty command, errx() in case of errors.
99  * First thing we do is process parameters creating an argv[] array
100  * which includes the program name and a NULL entry at the end.
101  * If we are called with a single string, we split it on whitespace.
102  * Also, arguments with a trailing ',' are joined to the next one.
103  * The pointers (av[]) and data are in a single chunk of memory.
104  * av[0] points to the original program name, all other entries
105  * point into the allocated chunk.
106  */
107 static int
108 ipfw_main(int oldac, char **oldav)
109 {
110 	int ch, ac;
111 	const char *errstr;
112 	char **av, **save_av;
113 	int do_acct = 0;		/* Show packet/byte count */
114 	int try_next = 0;		/* set if pipe cmd not found */
115 	int av_size;			/* compute the av size */
116 	char *av_p;			/* used to build the av list */
117 
118 #define WHITESP		" \t\f\v\n\r"
119 	if (oldac < 2)
120 		return 1;	/* need at least one argument */
121 
122 	if (oldac == 2) {
123 		/*
124 		 * If we are called with one argument, try to split it into
125 		 * words for subsequent parsing. Spaces after a ',' are
126 		 * removed by copying the string in-place.
127 		 */
128 		char *arg = oldav[1];	/* The string is the first arg. */
129 		int l = strlen(arg);
130 		int copy = 0;		/* 1 if we need to copy, 0 otherwise */
131 		int i, j;
132 
133 		for (i = j = 0; i < l; i++) {
134 			if (arg[i] == '#')	/* comment marker */
135 				break;
136 			if (copy) {
137 				arg[j++] = arg[i];
138 				copy = !strchr("," WHITESP, arg[i]);
139 			} else {
140 				copy = !strchr(WHITESP, arg[i]);
141 				if (copy)
142 					arg[j++] = arg[i];
143 			}
144 		}
145 		if (!copy && j > 0)	/* last char was a 'blank', remove it */
146 			j--;
147 		l = j;			/* the new argument length */
148 		arg[j++] = '\0';
149 		if (l == 0)		/* empty string! */
150 			return 1;
151 
152 		/*
153 		 * First, count number of arguments. Because of the previous
154 		 * processing, this is just the number of blanks plus 1.
155 		 */
156 		for (i = 0, ac = 1; i < l; i++)
157 			if (strchr(WHITESP, arg[i]) != NULL)
158 				ac++;
159 
160 		/*
161 		 * Allocate the argument list structure as a single block
162 		 * of memory, containing pointers and the argument
163 		 * strings. We include one entry for the program name
164 		 * because getopt expects it, and a NULL at the end
165 		 * to simplify further parsing.
166 		 */
167 		ac++;		/* add 1 for the program name */
168 		av_size = (ac+1) * sizeof(char *) + l + 1;
169 		av = safe_calloc(av_size, 1);
170 
171 		/*
172 		 * Init the argument pointer to the end of the array
173 		 * and copy arguments from arg[] to av[]. For each one,
174 		 * j is the initial character, i is the one past the end.
175 		 */
176 		av_p = (char *)&av[ac+1];
177 		for (ac = 1, i = j = 0; i < l; i++) {
178 			if (strchr(WHITESP, arg[i]) != NULL || i == l-1) {
179 				if (i == l-1)
180 					i++;
181 				bcopy(arg+j, av_p, i-j);
182 				av[ac] = av_p;
183 				av_p += i-j;	/* the length of the string */
184 				*av_p++ = '\0';
185 				ac++;
186 				j = i + 1;
187 			}
188 		}
189 	} else {
190 		/*
191 		 * If an argument ends with ',' join with the next one.
192 		 */
193 		int first, i, l=0;
194 
195 		/*
196 		 * Allocate the argument list structure as a single block
197 		 * of memory, containing both pointers and the argument
198 		 * strings. We include some space for the program name
199 		 * because getopt expects it.
200 		 * We add an extra pointer to the end of the array,
201 		 * to make simpler further parsing.
202 		 */
203 		for (i=0; i<oldac; i++)
204 			l += strlen(oldav[i]);
205 
206 		av_size = (oldac+1) * sizeof(char *) + l + oldac;
207 		av = safe_calloc(av_size, 1);
208 
209 		/*
210 		 * Init the argument pointer to the end of the array
211 		 * and copy arguments from arg[] to av[]
212 		 */
213 		av_p = (char *)&av[oldac+1];
214 		for (first = i = ac = 1, l = 0; i < oldac; i++) {
215 			char *arg = oldav[i];
216 			int k = strlen(arg);
217 
218 			l += k;
219 			if (arg[k-1] != ',' || i == oldac-1) {
220 				/* Time to copy. */
221 				av[ac] = av_p;
222 				for (l=0; first <= i; first++) {
223 					strcat(av_p, oldav[first]);
224 					av_p += strlen(oldav[first]);
225 				}
226 				*av_p++ = '\0';
227 				ac++;
228 				l = 0;
229 				first = i+1;
230 			}
231 		}
232 	}
233 
234 	/*
235 	 * set the progname pointer to the original string
236 	 * and terminate the array with null
237 	 */
238 	av[0] = oldav[0];
239 	av[ac] = NULL;
240 
241 	/* Set the force flag for non-interactive processes */
242 	if (!g_co.do_force)
243 		g_co.do_force = !isatty(STDIN_FILENO);
244 
245 #ifdef EMULATE_SYSCTL /* sysctl emulation */
246 	if (is_ipfw() && ac >= 2 &&
247 	    !strcmp(av[1], "sysctl")) {
248 		char *s;
249 		int i;
250 
251 		if (ac != 3) {
252 			printf(	"sysctl emulation usage:\n"
253 				"	ipfw sysctl name[=value]\n"
254 				"	ipfw sysctl -a\n");
255 			return 0;
256 		}
257 		s = strchr(av[2], '=');
258 		if (s == NULL) {
259 			s = !strcmp(av[2], "-a") ? NULL : av[2];
260 			sysctlbyname(s, NULL, NULL, NULL, 0);
261 		} else {	/* ipfw sysctl x.y.z=value */
262 			/* assume an INT value, will extend later */
263 			if (s[1] == '\0') {
264 				printf("ipfw sysctl: missing value\n\n");
265 				return 0;
266 			}
267 			*s = '\0';
268 			i = strtol(s+1, NULL, 0);
269 			sysctlbyname(av[2], NULL, NULL, &i, sizeof(int));
270 		}
271 		return 0;
272 	}
273 #endif
274 
275 	/* Save arguments for final freeing of memory. */
276 	save_av = av;
277 
278 	optind = optreset = 1;	/* restart getopt() */
279 	if (is_ipfw()) {
280 		while ((ch = getopt(ac, av, "abcdDefhinNp:qs:STtvx")) != -1)
281 			switch (ch) {
282 			case 'a':
283 				do_acct = 1;
284 				break;
285 
286 			case 'b':
287 				g_co.comment_only = 1;
288 				g_co.do_compact = 1;
289 				break;
290 
291 			case 'c':
292 				g_co.do_compact = 1;
293 				break;
294 
295 			case 'd':
296 				g_co.do_dynamic = 1;
297 				break;
298 
299 			case 'D':
300 				g_co.do_dynamic = 2;
301 				break;
302 
303 			case 'e':
304 				/* nop for compatibility */
305 				break;
306 
307 			case 'f':
308 				g_co.do_force = 1;
309 				break;
310 
311 			case 'h': /* help */
312 				free(save_av);
313 				help();
314 				break;	/* NOTREACHED */
315 
316 			case 'i':
317 				g_co.do_value_as_ip = 1;
318 				break;
319 
320 			case 'n':
321 				g_co.test_only = 1;
322 				break;
323 
324 			case 'N':
325 				g_co.do_resolv = 1;
326 				break;
327 
328 			case 'p':
329 				errx(EX_USAGE, "An absolute pathname must be used "
330 				    "with -p option.");
331 				/* NOTREACHED */
332 
333 			case 'q':
334 				g_co.do_quiet = 1;
335 				break;
336 
337 			case 's': /* sort */
338 				g_co.do_sort = atoi(optarg);
339 				break;
340 
341 			case 'S':
342 				g_co.show_sets = 1;
343 				break;
344 
345 			case 't':
346 				g_co.do_time = TIMESTAMP_STRING;
347 				break;
348 
349 			case 'T':
350 				g_co.do_time = TIMESTAMP_NUMERIC;
351 				break;
352 
353 			case 'v': /* verbose */
354 				g_co.verbose = 1;
355 				break;
356 
357 			case 'x': /* debug output */
358 				g_co.debug_only = 1;
359 				break;
360 
361 			default:
362 				free(save_av);
363 				return 1;
364 			}
365 	} else {
366 		while ((ch = getopt(ac, av, "hns:v")) != -1)
367 			switch (ch) {
368 
369 			case 'h': /* help */
370 				free(save_av);
371 				help();
372 				break;	/* NOTREACHED */
373 
374 			case 'n':
375 				g_co.test_only = 1;
376 				break;
377 
378 			case 's': /* sort */
379 				g_co.do_sort = atoi(optarg);
380 				break;
381 
382 			case 'v': /* verbose */
383 				g_co.verbose = 1;
384 				break;
385 
386 			default:
387 				free(save_av);
388 				return 1;
389 			}
390 
391 	}
392 
393 	ac -= optind;
394 	av += optind;
395 	NEED1("bad arguments, for usage summary ``ipfw''");
396 
397 	/*
398 	 * An undocumented behaviour of ipfw1 was to allow rule numbers first,
399 	 * e.g. "100 add allow ..." instead of "add 100 allow ...".
400 	 * In case, swap first and second argument to get the normal form.
401 	 */
402 	if (ac > 1 && isdigit(*av[0])) {
403 		char *p = av[0];
404 
405 		av[0] = av[1];
406 		av[1] = p;
407 	}
408 
409 	/*
410 	 * Optional: pipe, queue or nat.
411 	 */
412 	g_co.do_nat = 0;
413 	g_co.do_pipe = 0;
414 	g_co.use_set = 0;
415 	if (is_ipfw() && !strncmp(*av, "nat", strlen(*av)))
416 		g_co.do_nat = 1;
417 	else if (!strncmp(*av, "pipe", strlen(*av)))
418 		g_co.do_pipe = 1;
419 	else if (_substrcmp(*av, "queue") == 0)
420 		g_co.do_pipe = 2;
421 	else if (_substrcmp(*av, "flowset") == 0)
422 		g_co.do_pipe = 2;
423 	else if (_substrcmp(*av, "sched") == 0)
424 		g_co.do_pipe = 3;
425 	else if (is_ipfw() && !strncmp(*av, "set", strlen(*av))) {
426 		if (ac > 1 && isdigit(av[1][0])) {
427 			g_co.use_set = strtonum(av[1], 0, resvd_set_number,
428 					&errstr);
429 			if (errstr)
430 				errx(EX_DATAERR,
431 				    "invalid set number %s\n", av[1]);
432 			ac -= 2; av += 2; g_co.use_set++;
433 		}
434 	}
435 
436 	if (g_co.do_pipe || g_co.do_nat) {
437 		ac--;
438 		av++;
439 	}
440 	NEED1("missing command");
441 
442 	/*
443 	 * For pipes, queues and nats we normally say 'nat|pipe NN config'
444 	 * but the code is easier to parse as 'nat|pipe config NN'
445 	 * so we swap the two arguments.
446 	 */
447 	if ((g_co.do_pipe || g_co.do_nat) && ac > 1 && isdigit(*av[0])) {
448 		char *p = av[0];
449 
450 		av[0] = av[1];
451 		av[1] = p;
452 	}
453 
454 	if (! is_ipfw() && g_co.do_pipe == 0) {
455 		help();
456 	}
457 
458 	if (g_co.use_set == 0) {
459 		if (is_ipfw() && _substrcmp(*av, "add") == 0)
460 			ipfw_add(av);
461 		else if (g_co.do_nat && _substrcmp(*av, "show") == 0)
462  			ipfw_show_nat(ac, av);
463 		else if (g_co.do_pipe && _substrcmp(*av, "config") == 0)
464 			ipfw_config_pipe(ac, av);
465 		else if (g_co.do_nat && _substrcmp(*av, "config") == 0)
466  			ipfw_config_nat(ac, av);
467 		else if (is_ipfw() && _substrcmp(*av, "set") == 0)
468 			ipfw_sets_handler(av);
469 		else if (is_ipfw() && _substrcmp(*av, "table") == 0)
470 			ipfw_table_handler(ac, av);
471 		else if (is_ipfw() && _substrcmp(*av, "enable") == 0)
472 			ipfw_sysctl_handler(av, 1);
473 		else if (is_ipfw() && _substrcmp(*av, "disable") == 0)
474 			ipfw_sysctl_handler(av, 0);
475 		else
476 			try_next = 1;
477 	}
478 
479 	if (g_co.use_set || try_next) {
480 		if (_substrcmp(*av, "delete") == 0)
481 			ipfw_delete(av);
482 		else if (is_ipfw() && !strncmp(*av, "nat64clat", strlen(*av)))
483 			ipfw_nat64clat_handler(ac, av);
484 		else if (is_ipfw() && !strncmp(*av, "nat64stl", strlen(*av)))
485 			ipfw_nat64stl_handler(ac, av);
486 		else if (is_ipfw() && !strncmp(*av, "nat64lsn", strlen(*av)))
487 			ipfw_nat64lsn_handler(ac, av);
488 		else if (is_ipfw() && !strncmp(*av, "nptv6", strlen(*av)))
489 			ipfw_nptv6_handler(ac, av);
490 		else if (_substrcmp(*av, "flush") == 0)
491 			ipfw_flush(g_co.do_force);
492 		else if (is_ipfw() && _substrcmp(*av, "zero") == 0)
493 			ipfw_zero(ac, av, 0 /* IP_FW_ZERO */);
494 		else if (is_ipfw() && _substrcmp(*av, "resetlog") == 0)
495 			ipfw_zero(ac, av, 1 /* IP_FW_RESETLOG */);
496 		else if (_substrcmp(*av, "print") == 0 ||
497 			 _substrcmp(*av, "list") == 0)
498 			ipfw_list(ac, av, do_acct);
499 		else if (_substrcmp(*av, "show") == 0)
500 			ipfw_list(ac, av, 1 /* show counters */);
501 		else if (is_ipfw() && _substrcmp(*av, "table") == 0)
502 			ipfw_table_handler(ac, av);
503 		else if (is_ipfw() && _substrcmp(*av, "internal") == 0)
504 			ipfw_internal_handler(ac, av);
505 		else
506 			errx(EX_USAGE, "bad command `%s'", *av);
507 	}
508 
509 	/* Free memory allocated in the argument parsing. */
510 	free(save_av);
511 	return 0;
512 }
513 
514 
515 static void
516 ipfw_readfile(int ac, char *av[])
517 {
518 #define MAX_ARGS	32
519 	char buf[4096];
520 	char *progname = av[0];		/* original program name */
521 	const char *cmd = NULL;		/* preprocessor name, if any */
522 	const char *filename = av[ac-1]; /* file to read */
523 	int	c, lineno=0;
524 	FILE	*f = NULL;
525 	pid_t	preproc = 0;
526 
527 	if (is_ipfw()) {
528 		while ((c = getopt(ac, av, "cfNnp:qS")) != -1) {
529 			switch(c) {
530 			case 'c':
531 				g_co.do_compact = 1;
532 				break;
533 
534 			case 'f':
535 				g_co.do_force = 1;
536 				break;
537 
538 			case 'N':
539 				g_co.do_resolv = 1;
540 				break;
541 
542 			case 'n':
543 				g_co.test_only = 1;
544 				break;
545 
546 			case 'p':
547 				/*
548 				 * ipfw -p cmd [args] filename
549 				 *
550 				 * We are done with getopt(). All arguments
551 				 * except the filename go to the preprocessor,
552 				 * so we need to do the following:
553 				 * - check that a filename is actually present;
554 				 * - advance av by optind-1 to skip arguments
555 				 *   already processed;
556 				 * - decrease ac by optind, to remove the args
557 				 *   already processed and the final filename;
558 				 * - set the last entry in av[] to NULL so
559 				 *   popen() can detect the end of the array;
560 				 * - set optind=ac to let getopt() terminate.
561 				 */
562 				if (optind == ac)
563 					errx(EX_USAGE, "no filename argument");
564 				cmd = optarg;
565 				av[ac-1] = NULL;
566 				av += optind - 1;
567 				ac -= optind;
568 				optind = ac;
569 				break;
570 
571 			case 'q':
572 				g_co.do_quiet = 1;
573 				break;
574 
575 			case 'S':
576 				g_co.show_sets = 1;
577 				break;
578 
579 			default:
580 				errx(EX_USAGE, "bad arguments, for usage"
581 				     " summary ``ipfw''");
582 			}
583 		}
584 	} else {
585 		while ((c = getopt(ac, av, "nq")) != -1) {
586 			switch(c) {
587 			case 'n':
588 				g_co.test_only = 1;
589 				break;
590 
591 			case 'q':
592 				g_co.do_quiet = 1;
593 				break;
594 
595 			default:
596 				errx(EX_USAGE, "bad arguments, for usage"
597 				     " summary ``dnctl''");
598 			}
599 		}
600 	}
601 
602 	if (cmd == NULL && ac != optind + 1)
603 		errx(EX_USAGE, "extraneous filename arguments %s", av[ac-1]);
604 
605 	if ((f = fopen(filename, "r")) == NULL)
606 		err(EX_UNAVAILABLE, "fopen: %s", filename);
607 
608 	if (cmd != NULL) {			/* pipe through preprocessor */
609 		int pipedes[2];
610 
611 		if (pipe(pipedes) == -1)
612 			err(EX_OSERR, "cannot create pipe");
613 
614 		preproc = fork();
615 		if (preproc == -1)
616 			err(EX_OSERR, "cannot fork");
617 
618 		if (preproc == 0) {
619 			/*
620 			 * Child, will run the preprocessor with the
621 			 * file on stdin and the pipe on stdout.
622 			 */
623 			if (dup2(fileno(f), 0) == -1
624 			    || dup2(pipedes[1], 1) == -1)
625 				err(EX_OSERR, "dup2()");
626 			fclose(f);
627 			close(pipedes[1]);
628 			close(pipedes[0]);
629 			execvp(cmd, av);
630 			err(EX_OSERR, "execvp(%s) failed", cmd);
631 		} else { /* parent, will reopen f as the pipe */
632 			fclose(f);
633 			close(pipedes[1]);
634 			if ((f = fdopen(pipedes[0], "r")) == NULL) {
635 				int savederrno = errno;
636 
637 				(void)kill(preproc, SIGTERM);
638 				errno = savederrno;
639 				err(EX_OSERR, "fdopen()");
640 			}
641 		}
642 	}
643 
644 	while (fgets(buf, sizeof(buf), f)) {		/* read commands */
645 		char linename[20];
646 		char *args[2];
647 
648 		lineno++;
649 		snprintf(linename, sizeof(linename), "Line %d", lineno);
650 		setprogname(linename); /* XXX */
651 		args[0] = progname;
652 		args[1] = buf;
653 		ipfw_main(2, args);
654 	}
655 	fclose(f);
656 	if (cmd != NULL) {
657 		int status;
658 
659 		if (waitpid(preproc, &status, 0) == -1)
660 			errx(EX_OSERR, "waitpid()");
661 		if (WIFEXITED(status) && WEXITSTATUS(status) != EX_OK)
662 			errx(EX_UNAVAILABLE,
663 			    "preprocessor exited with status %d",
664 			    WEXITSTATUS(status));
665 		else if (WIFSIGNALED(status))
666 			errx(EX_UNAVAILABLE,
667 			    "preprocessor exited with signal %d",
668 			    WTERMSIG(status));
669 	}
670 }
671 
672 int
673 main(int ac, char *av[])
674 {
675 #if defined(_WIN32) && defined(TCC)
676 	{
677 		WSADATA wsaData;
678 		int ret=0;
679 		unsigned short wVersionRequested = MAKEWORD(2, 2);
680 		ret = WSAStartup(wVersionRequested, &wsaData);
681 		if (ret != 0) {
682 			/* Tell the user that we could not find a usable */
683 			/* Winsock DLL.				  */
684 			printf("WSAStartup failed with error: %d\n", ret);
685 			return 1;
686 		}
687 	}
688 #endif
689 
690 	if (strcmp("dnctl", basename(av[0])) == 0)
691 		g_co.prog = cmdline_prog_dnctl;
692 	else
693 		g_co.prog = cmdline_prog_ipfw;
694 
695 	/*
696 	 * If the last argument is an absolute pathname, interpret it
697 	 * as a file to be preprocessed.
698 	 */
699 
700 	if (ac > 1 && av[ac - 1][0] == '/') {
701 		if (access(av[ac - 1], R_OK) == 0)
702 			ipfw_readfile(ac, av);
703 		else
704 			err(EX_USAGE, "pathname: %s", av[ac - 1]);
705 	} else {
706 		if (ipfw_main(ac, av)) {
707 			errx(EX_USAGE,
708 			    "usage: %s [options]\n"
709 			    "do \"%s -h\" or \"man %s\" for details", av[0],
710 			    av[0], av[0]);
711 		}
712 	}
713 	return EX_OK;
714 }
715