xref: /freebsd/sbin/ipfw/main.c (revision 9768746b)
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:STtv")) != -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 			default:
358 				free(save_av);
359 				return 1;
360 			}
361 	} else {
362 		while ((ch = getopt(ac, av, "hns:v")) != -1)
363 			switch (ch) {
364 
365 			case 'h': /* help */
366 				free(save_av);
367 				help();
368 				break;	/* NOTREACHED */
369 
370 			case 'n':
371 				g_co.test_only = 1;
372 				break;
373 
374 			case 's': /* sort */
375 				g_co.do_sort = atoi(optarg);
376 				break;
377 
378 			case 'v': /* verbose */
379 				g_co.verbose = 1;
380 				break;
381 
382 			default:
383 				free(save_av);
384 				return 1;
385 			}
386 
387 	}
388 
389 	ac -= optind;
390 	av += optind;
391 	NEED1("bad arguments, for usage summary ``ipfw''");
392 
393 	/*
394 	 * An undocumented behaviour of ipfw1 was to allow rule numbers first,
395 	 * e.g. "100 add allow ..." instead of "add 100 allow ...".
396 	 * In case, swap first and second argument to get the normal form.
397 	 */
398 	if (ac > 1 && isdigit(*av[0])) {
399 		char *p = av[0];
400 
401 		av[0] = av[1];
402 		av[1] = p;
403 	}
404 
405 	/*
406 	 * Optional: pipe, queue or nat.
407 	 */
408 	g_co.do_nat = 0;
409 	g_co.do_pipe = 0;
410 	g_co.use_set = 0;
411 	if (is_ipfw() && !strncmp(*av, "nat", strlen(*av)))
412 		g_co.do_nat = 1;
413 	else if (!strncmp(*av, "pipe", strlen(*av)))
414 		g_co.do_pipe = 1;
415 	else if (_substrcmp(*av, "queue") == 0)
416 		g_co.do_pipe = 2;
417 	else if (_substrcmp(*av, "flowset") == 0)
418 		g_co.do_pipe = 2;
419 	else if (_substrcmp(*av, "sched") == 0)
420 		g_co.do_pipe = 3;
421 	else if (is_ipfw() && !strncmp(*av, "set", strlen(*av))) {
422 		if (ac > 1 && isdigit(av[1][0])) {
423 			g_co.use_set = strtonum(av[1], 0, resvd_set_number,
424 					&errstr);
425 			if (errstr)
426 				errx(EX_DATAERR,
427 				    "invalid set number %s\n", av[1]);
428 			ac -= 2; av += 2; g_co.use_set++;
429 		}
430 	}
431 
432 	if (g_co.do_pipe || g_co.do_nat) {
433 		ac--;
434 		av++;
435 	}
436 	NEED1("missing command");
437 
438 	/*
439 	 * For pipes, queues and nats we normally say 'nat|pipe NN config'
440 	 * but the code is easier to parse as 'nat|pipe config NN'
441 	 * so we swap the two arguments.
442 	 */
443 	if ((g_co.do_pipe || g_co.do_nat) && ac > 1 && isdigit(*av[0])) {
444 		char *p = av[0];
445 
446 		av[0] = av[1];
447 		av[1] = p;
448 	}
449 
450 	if (! is_ipfw() && g_co.do_pipe == 0) {
451 		help();
452 	}
453 
454 	if (g_co.use_set == 0) {
455 		if (is_ipfw() && _substrcmp(*av, "add") == 0)
456 			ipfw_add(av);
457 		else if (g_co.do_nat && _substrcmp(*av, "show") == 0)
458  			ipfw_show_nat(ac, av);
459 		else if (g_co.do_pipe && _substrcmp(*av, "config") == 0)
460 			ipfw_config_pipe(ac, av);
461 		else if (g_co.do_nat && _substrcmp(*av, "config") == 0)
462  			ipfw_config_nat(ac, av);
463 		else if (is_ipfw() && _substrcmp(*av, "set") == 0)
464 			ipfw_sets_handler(av);
465 		else if (is_ipfw() && _substrcmp(*av, "table") == 0)
466 			ipfw_table_handler(ac, av);
467 		else if (is_ipfw() && _substrcmp(*av, "enable") == 0)
468 			ipfw_sysctl_handler(av, 1);
469 		else if (is_ipfw() && _substrcmp(*av, "disable") == 0)
470 			ipfw_sysctl_handler(av, 0);
471 		else
472 			try_next = 1;
473 	}
474 
475 	if (g_co.use_set || try_next) {
476 		if (_substrcmp(*av, "delete") == 0)
477 			ipfw_delete(av);
478 		else if (is_ipfw() && !strncmp(*av, "nat64clat", strlen(*av)))
479 			ipfw_nat64clat_handler(ac, av);
480 		else if (is_ipfw() && !strncmp(*av, "nat64stl", strlen(*av)))
481 			ipfw_nat64stl_handler(ac, av);
482 		else if (is_ipfw() && !strncmp(*av, "nat64lsn", strlen(*av)))
483 			ipfw_nat64lsn_handler(ac, av);
484 		else if (is_ipfw() && !strncmp(*av, "nptv6", strlen(*av)))
485 			ipfw_nptv6_handler(ac, av);
486 		else if (_substrcmp(*av, "flush") == 0)
487 			ipfw_flush(g_co.do_force);
488 		else if (is_ipfw() && _substrcmp(*av, "zero") == 0)
489 			ipfw_zero(ac, av, 0 /* IP_FW_ZERO */);
490 		else if (is_ipfw() && _substrcmp(*av, "resetlog") == 0)
491 			ipfw_zero(ac, av, 1 /* IP_FW_RESETLOG */);
492 		else if (_substrcmp(*av, "print") == 0 ||
493 			 _substrcmp(*av, "list") == 0)
494 			ipfw_list(ac, av, do_acct);
495 		else if (_substrcmp(*av, "show") == 0)
496 			ipfw_list(ac, av, 1 /* show counters */);
497 		else if (is_ipfw() && _substrcmp(*av, "table") == 0)
498 			ipfw_table_handler(ac, av);
499 		else if (is_ipfw() && _substrcmp(*av, "internal") == 0)
500 			ipfw_internal_handler(ac, av);
501 		else
502 			errx(EX_USAGE, "bad command `%s'", *av);
503 	}
504 
505 	/* Free memory allocated in the argument parsing. */
506 	free(save_av);
507 	return 0;
508 }
509 
510 
511 static void
512 ipfw_readfile(int ac, char *av[])
513 {
514 #define MAX_ARGS	32
515 	char buf[4096];
516 	char *progname = av[0];		/* original program name */
517 	const char *cmd = NULL;		/* preprocessor name, if any */
518 	const char *filename = av[ac-1]; /* file to read */
519 	int	c, lineno=0;
520 	FILE	*f = NULL;
521 	pid_t	preproc = 0;
522 
523 	if (is_ipfw()) {
524 		while ((c = getopt(ac, av, "cfNnp:qS")) != -1) {
525 			switch(c) {
526 			case 'c':
527 				g_co.do_compact = 1;
528 				break;
529 
530 			case 'f':
531 				g_co.do_force = 1;
532 				break;
533 
534 			case 'N':
535 				g_co.do_resolv = 1;
536 				break;
537 
538 			case 'n':
539 				g_co.test_only = 1;
540 				break;
541 
542 			case 'p':
543 				/*
544 				 * ipfw -p cmd [args] filename
545 				 *
546 				 * We are done with getopt(). All arguments
547 				 * except the filename go to the preprocessor,
548 				 * so we need to do the following:
549 				 * - check that a filename is actually present;
550 				 * - advance av by optind-1 to skip arguments
551 				 *   already processed;
552 				 * - decrease ac by optind, to remove the args
553 				 *   already processed and the final filename;
554 				 * - set the last entry in av[] to NULL so
555 				 *   popen() can detect the end of the array;
556 				 * - set optind=ac to let getopt() terminate.
557 				 */
558 				if (optind == ac)
559 					errx(EX_USAGE, "no filename argument");
560 				cmd = optarg;
561 				av[ac-1] = NULL;
562 				av += optind - 1;
563 				ac -= optind;
564 				optind = ac;
565 				break;
566 
567 			case 'q':
568 				g_co.do_quiet = 1;
569 				break;
570 
571 			case 'S':
572 				g_co.show_sets = 1;
573 				break;
574 
575 			default:
576 				errx(EX_USAGE, "bad arguments, for usage"
577 				     " summary ``ipfw''");
578 			}
579 		}
580 	} else {
581 		while ((c = getopt(ac, av, "nq")) != -1) {
582 			switch(c) {
583 			case 'n':
584 				g_co.test_only = 1;
585 				break;
586 
587 			case 'q':
588 				g_co.do_quiet = 1;
589 				break;
590 
591 			default:
592 				errx(EX_USAGE, "bad arguments, for usage"
593 				     " summary ``dnctl''");
594 			}
595 		}
596 	}
597 
598 	if (cmd == NULL && ac != optind + 1)
599 		errx(EX_USAGE, "extraneous filename arguments %s", av[ac-1]);
600 
601 	if ((f = fopen(filename, "r")) == NULL)
602 		err(EX_UNAVAILABLE, "fopen: %s", filename);
603 
604 	if (cmd != NULL) {			/* pipe through preprocessor */
605 		int pipedes[2];
606 
607 		if (pipe(pipedes) == -1)
608 			err(EX_OSERR, "cannot create pipe");
609 
610 		preproc = fork();
611 		if (preproc == -1)
612 			err(EX_OSERR, "cannot fork");
613 
614 		if (preproc == 0) {
615 			/*
616 			 * Child, will run the preprocessor with the
617 			 * file on stdin and the pipe on stdout.
618 			 */
619 			if (dup2(fileno(f), 0) == -1
620 			    || dup2(pipedes[1], 1) == -1)
621 				err(EX_OSERR, "dup2()");
622 			fclose(f);
623 			close(pipedes[1]);
624 			close(pipedes[0]);
625 			execvp(cmd, av);
626 			err(EX_OSERR, "execvp(%s) failed", cmd);
627 		} else { /* parent, will reopen f as the pipe */
628 			fclose(f);
629 			close(pipedes[1]);
630 			if ((f = fdopen(pipedes[0], "r")) == NULL) {
631 				int savederrno = errno;
632 
633 				(void)kill(preproc, SIGTERM);
634 				errno = savederrno;
635 				err(EX_OSERR, "fdopen()");
636 			}
637 		}
638 	}
639 
640 	while (fgets(buf, sizeof(buf), f)) {		/* read commands */
641 		char linename[20];
642 		char *args[2];
643 
644 		lineno++;
645 		snprintf(linename, sizeof(linename), "Line %d", lineno);
646 		setprogname(linename); /* XXX */
647 		args[0] = progname;
648 		args[1] = buf;
649 		ipfw_main(2, args);
650 	}
651 	fclose(f);
652 	if (cmd != NULL) {
653 		int status;
654 
655 		if (waitpid(preproc, &status, 0) == -1)
656 			errx(EX_OSERR, "waitpid()");
657 		if (WIFEXITED(status) && WEXITSTATUS(status) != EX_OK)
658 			errx(EX_UNAVAILABLE,
659 			    "preprocessor exited with status %d",
660 			    WEXITSTATUS(status));
661 		else if (WIFSIGNALED(status))
662 			errx(EX_UNAVAILABLE,
663 			    "preprocessor exited with signal %d",
664 			    WTERMSIG(status));
665 	}
666 }
667 
668 int
669 main(int ac, char *av[])
670 {
671 #if defined(_WIN32) && defined(TCC)
672 	{
673 		WSADATA wsaData;
674 		int ret=0;
675 		unsigned short wVersionRequested = MAKEWORD(2, 2);
676 		ret = WSAStartup(wVersionRequested, &wsaData);
677 		if (ret != 0) {
678 			/* Tell the user that we could not find a usable */
679 			/* Winsock DLL.				  */
680 			printf("WSAStartup failed with error: %d\n", ret);
681 			return 1;
682 		}
683 	}
684 #endif
685 
686 	if (strcmp("dnctl", basename(av[0])) == 0)
687 		g_co.prog = cmdline_prog_dnctl;
688 	else
689 		g_co.prog = cmdline_prog_ipfw;
690 
691 	/*
692 	 * If the last argument is an absolute pathname, interpret it
693 	 * as a file to be preprocessed.
694 	 */
695 
696 	if (ac > 1 && av[ac - 1][0] == '/') {
697 		if (access(av[ac - 1], R_OK) == 0)
698 			ipfw_readfile(ac, av);
699 		else
700 			err(EX_USAGE, "pathname: %s", av[ac - 1]);
701 	} else {
702 		if (ipfw_main(ac, av)) {
703 			errx(EX_USAGE,
704 			    "usage: %s [options]\n"
705 			    "do \"%s -h\" or \"man %s\" for details", av[0],
706 			    av[0], av[0]);
707 		}
708 	}
709 	return EX_OK;
710 }
711