xref: /freebsd/sbin/ipfw/ipfw2.c (revision 36c263cca91339db88f48abb2f2b188cac9b2aa3)
19758b77fSLuigi Rizzo /*
2571f8c1bSLuigi Rizzo  * Copyright (c) 2002-2003 Luigi Rizzo
39758b77fSLuigi Rizzo  * Copyright (c) 1996 Alex Nash, Paul Traina, Poul-Henning Kamp
49758b77fSLuigi Rizzo  * Copyright (c) 1994 Ugen J.S.Antsilevich
59758b77fSLuigi Rizzo  *
69758b77fSLuigi Rizzo  * Idea and grammar partially left from:
79758b77fSLuigi Rizzo  * Copyright (c) 1993 Daniel Boulet
89758b77fSLuigi Rizzo  *
99758b77fSLuigi Rizzo  * Redistribution and use in source forms, with and without modification,
109758b77fSLuigi Rizzo  * are permitted provided that this entire comment appears intact.
119758b77fSLuigi Rizzo  *
129758b77fSLuigi Rizzo  * Redistribution in binary form may occur without any restrictions.
139758b77fSLuigi Rizzo  * Obviously, it would be nice if you gave credit where credit is due
149758b77fSLuigi Rizzo  * but requiring it would be too onerous.
159758b77fSLuigi Rizzo  *
169758b77fSLuigi Rizzo  * This software is provided ``AS IS'' without any warranties of any kind.
179758b77fSLuigi Rizzo  *
189758b77fSLuigi Rizzo  * NEW command line interface for IP firewall facility
199758b77fSLuigi Rizzo  *
209758b77fSLuigi Rizzo  * $FreeBSD$
219758b77fSLuigi Rizzo  */
229758b77fSLuigi Rizzo 
239758b77fSLuigi Rizzo #include <sys/param.h>
249758b77fSLuigi Rizzo #include <sys/mbuf.h>
259758b77fSLuigi Rizzo #include <sys/socket.h>
269758b77fSLuigi Rizzo #include <sys/sockio.h>
279758b77fSLuigi Rizzo #include <sys/sysctl.h>
289758b77fSLuigi Rizzo #include <sys/time.h>
299758b77fSLuigi Rizzo #include <sys/wait.h>
30974dfe30SBrian Feldman #include <sys/queue.h>
319758b77fSLuigi Rizzo 
329758b77fSLuigi Rizzo #include <ctype.h>
339758b77fSLuigi Rizzo #include <err.h>
349758b77fSLuigi Rizzo #include <errno.h>
359758b77fSLuigi Rizzo #include <grp.h>
369758b77fSLuigi Rizzo #include <limits.h>
379758b77fSLuigi Rizzo #include <netdb.h>
389758b77fSLuigi Rizzo #include <pwd.h>
399758b77fSLuigi Rizzo #include <signal.h>
409758b77fSLuigi Rizzo #include <stdio.h>
419758b77fSLuigi Rizzo #include <stdlib.h>
429758b77fSLuigi Rizzo #include <stdarg.h>
439758b77fSLuigi Rizzo #include <string.h>
4462ff38aeSLuigi Rizzo #include <timeconv.h>	/* XXX do we need this ? */
459758b77fSLuigi Rizzo #include <unistd.h>
469758b77fSLuigi Rizzo #include <sysexits.h>
47974dfe30SBrian Feldman #include <unistd.h>
48974dfe30SBrian Feldman #include <fcntl.h>
499758b77fSLuigi Rizzo 
509758b77fSLuigi Rizzo #include <net/if.h>
51974dfe30SBrian Feldman #include <net/pfvar.h>
528195404bSBrooks Davis #include <net/route.h> /* def. of struct route */
539758b77fSLuigi Rizzo #include <netinet/in.h>
549758b77fSLuigi Rizzo #include <netinet/in_systm.h>
559758b77fSLuigi Rizzo #include <netinet/ip.h>
569758b77fSLuigi Rizzo #include <netinet/ip_icmp.h>
578195404bSBrooks Davis #include <netinet/icmp6.h>
589758b77fSLuigi Rizzo #include <netinet/ip_fw.h>
599758b77fSLuigi Rizzo #include <netinet/ip_dummynet.h>
609758b77fSLuigi Rizzo #include <netinet/tcp.h>
619758b77fSLuigi Rizzo #include <arpa/inet.h>
629758b77fSLuigi Rizzo 
63571f8c1bSLuigi Rizzo int
649758b77fSLuigi Rizzo 		do_resolv,		/* Would try to resolve all */
659758b77fSLuigi Rizzo 		do_time,		/* Show time stamps */
669758b77fSLuigi Rizzo 		do_quiet,		/* Be quiet in add and flush */
679758b77fSLuigi Rizzo 		do_pipe,		/* this cmd refers to a pipe */
689758b77fSLuigi Rizzo 		do_sort,		/* field to sort results (0 = no) */
699758b77fSLuigi Rizzo 		do_dynamic,		/* display dynamic rules */
709758b77fSLuigi Rizzo 		do_expired,		/* display expired dynamic rules */
715a155b40SLuigi Rizzo 		do_compact,		/* show rules in compact mode */
72cec4ab6aSMaxim Konovalov 		do_force,		/* do not ask for confirmation */
7343405724SLuigi Rizzo 		show_sets,		/* display rule sets */
74571f8c1bSLuigi Rizzo 		test_only,		/* only check syntax */
75ac6cec51SLuigi Rizzo 		comment_only,		/* only print action and comment */
769758b77fSLuigi Rizzo 		verbose;
779758b77fSLuigi Rizzo 
789758b77fSLuigi Rizzo #define	IP_MASK_ALL	0xffffffff
7904f70834SChristian S.J. Peron /*
8004f70834SChristian S.J. Peron  * the following macro returns an error message if we run out of
8104f70834SChristian S.J. Peron  * arguments.
8204f70834SChristian S.J. Peron  */
8304f70834SChristian S.J. Peron #define NEED1(msg)      {if (!ac) errx(EX_USAGE, msg);}
849758b77fSLuigi Rizzo 
859758b77fSLuigi Rizzo /*
86571f8c1bSLuigi Rizzo  * _s_x is a structure that stores a string <-> token pairs, used in
87571f8c1bSLuigi Rizzo  * various places in the parser. Entries are stored in arrays,
88571f8c1bSLuigi Rizzo  * with an entry with s=NULL as terminator.
89571f8c1bSLuigi Rizzo  * The search routines are match_token() and match_value().
90571f8c1bSLuigi Rizzo  * Often, an element with x=0 contains an error string.
919758b77fSLuigi Rizzo  *
929758b77fSLuigi Rizzo  */
939758b77fSLuigi Rizzo struct _s_x {
9462ff38aeSLuigi Rizzo 	char const *s;
959758b77fSLuigi Rizzo 	int x;
969758b77fSLuigi Rizzo };
979758b77fSLuigi Rizzo 
989758b77fSLuigi Rizzo static struct _s_x f_tcpflags[] = {
999758b77fSLuigi Rizzo 	{ "syn", TH_SYN },
1009758b77fSLuigi Rizzo 	{ "fin", TH_FIN },
1019758b77fSLuigi Rizzo 	{ "ack", TH_ACK },
1029758b77fSLuigi Rizzo 	{ "psh", TH_PUSH },
1039758b77fSLuigi Rizzo 	{ "rst", TH_RST },
1049758b77fSLuigi Rizzo 	{ "urg", TH_URG },
1059758b77fSLuigi Rizzo 	{ "tcp flag", 0 },
1069758b77fSLuigi Rizzo 	{ NULL,	0 }
1079758b77fSLuigi Rizzo };
1089758b77fSLuigi Rizzo 
1099758b77fSLuigi Rizzo static struct _s_x f_tcpopts[] = {
1109758b77fSLuigi Rizzo 	{ "mss",	IP_FW_TCPOPT_MSS },
1119758b77fSLuigi Rizzo 	{ "maxseg",	IP_FW_TCPOPT_MSS },
1129758b77fSLuigi Rizzo 	{ "window",	IP_FW_TCPOPT_WINDOW },
1139758b77fSLuigi Rizzo 	{ "sack",	IP_FW_TCPOPT_SACK },
1149758b77fSLuigi Rizzo 	{ "ts",		IP_FW_TCPOPT_TS },
1159758b77fSLuigi Rizzo 	{ "timestamp",	IP_FW_TCPOPT_TS },
1169758b77fSLuigi Rizzo 	{ "cc",		IP_FW_TCPOPT_CC },
1179758b77fSLuigi Rizzo 	{ "tcp option",	0 },
1189758b77fSLuigi Rizzo 	{ NULL,	0 }
1199758b77fSLuigi Rizzo };
1209758b77fSLuigi Rizzo 
1219758b77fSLuigi Rizzo /*
1229758b77fSLuigi Rizzo  * IP options span the range 0 to 255 so we need to remap them
1239758b77fSLuigi Rizzo  * (though in fact only the low 5 bits are significant).
1249758b77fSLuigi Rizzo  */
1259758b77fSLuigi Rizzo static struct _s_x f_ipopts[] = {
1269758b77fSLuigi Rizzo 	{ "ssrr",	IP_FW_IPOPT_SSRR},
1279758b77fSLuigi Rizzo 	{ "lsrr",	IP_FW_IPOPT_LSRR},
1289758b77fSLuigi Rizzo 	{ "rr",		IP_FW_IPOPT_RR},
1299758b77fSLuigi Rizzo 	{ "ts",		IP_FW_IPOPT_TS},
1309758b77fSLuigi Rizzo 	{ "ip option",	0 },
1319758b77fSLuigi Rizzo 	{ NULL,	0 }
1329758b77fSLuigi Rizzo };
1339758b77fSLuigi Rizzo 
1349758b77fSLuigi Rizzo static struct _s_x f_iptos[] = {
1359758b77fSLuigi Rizzo 	{ "lowdelay",	IPTOS_LOWDELAY},
1369758b77fSLuigi Rizzo 	{ "throughput",	IPTOS_THROUGHPUT},
1379758b77fSLuigi Rizzo 	{ "reliability", IPTOS_RELIABILITY},
1389758b77fSLuigi Rizzo 	{ "mincost",	IPTOS_MINCOST},
1399758b77fSLuigi Rizzo 	{ "congestion",	IPTOS_CE},
1409758b77fSLuigi Rizzo 	{ "ecntransport", IPTOS_ECT},
1419758b77fSLuigi Rizzo 	{ "ip tos option", 0},
1429758b77fSLuigi Rizzo 	{ NULL,	0 }
1439758b77fSLuigi Rizzo };
1449758b77fSLuigi Rizzo 
1459758b77fSLuigi Rizzo static struct _s_x limit_masks[] = {
1469758b77fSLuigi Rizzo 	{"all",		DYN_SRC_ADDR|DYN_SRC_PORT|DYN_DST_ADDR|DYN_DST_PORT},
1479758b77fSLuigi Rizzo 	{"src-addr",	DYN_SRC_ADDR},
1489758b77fSLuigi Rizzo 	{"src-port",	DYN_SRC_PORT},
1499758b77fSLuigi Rizzo 	{"dst-addr",	DYN_DST_ADDR},
1509758b77fSLuigi Rizzo 	{"dst-port",	DYN_DST_PORT},
1519758b77fSLuigi Rizzo 	{NULL,		0}
1529758b77fSLuigi Rizzo };
1539758b77fSLuigi Rizzo 
1549758b77fSLuigi Rizzo /*
1559758b77fSLuigi Rizzo  * we use IPPROTO_ETHERTYPE as a fake protocol id to call the print routines
1569758b77fSLuigi Rizzo  * This is only used in this code.
1579758b77fSLuigi Rizzo  */
1589758b77fSLuigi Rizzo #define IPPROTO_ETHERTYPE	0x1000
1599758b77fSLuigi Rizzo static struct _s_x ether_types[] = {
1609758b77fSLuigi Rizzo     /*
1619758b77fSLuigi Rizzo      * Note, we cannot use "-:&/" in the names because they are field
1629758b77fSLuigi Rizzo      * separators in the type specifications. Also, we use s = NULL as
1639758b77fSLuigi Rizzo      * end-delimiter, because a type of 0 can be legal.
1649758b77fSLuigi Rizzo      */
1659758b77fSLuigi Rizzo 	{ "ip",		0x0800 },
1669758b77fSLuigi Rizzo 	{ "ipv4",	0x0800 },
1679758b77fSLuigi Rizzo 	{ "ipv6",	0x86dd },
1689758b77fSLuigi Rizzo 	{ "arp",	0x0806 },
1699758b77fSLuigi Rizzo 	{ "rarp",	0x8035 },
1709758b77fSLuigi Rizzo 	{ "vlan",	0x8100 },
1719758b77fSLuigi Rizzo 	{ "loop",	0x9000 },
1729758b77fSLuigi Rizzo 	{ "trail",	0x1000 },
1739758b77fSLuigi Rizzo 	{ "at",		0x809b },
1749758b77fSLuigi Rizzo 	{ "atalk",	0x809b },
1759758b77fSLuigi Rizzo 	{ "aarp",	0x80f3 },
1769758b77fSLuigi Rizzo 	{ "pppoe_disc",	0x8863 },
1779758b77fSLuigi Rizzo 	{ "pppoe_sess",	0x8864 },
1789758b77fSLuigi Rizzo 	{ "ipx_8022",	0x00E0 },
1799758b77fSLuigi Rizzo 	{ "ipx_8023",	0x0000 },
1809758b77fSLuigi Rizzo 	{ "ipx_ii",	0x8137 },
1819758b77fSLuigi Rizzo 	{ "ipx_snap",	0x8137 },
1829758b77fSLuigi Rizzo 	{ "ipx",	0x8137 },
1839758b77fSLuigi Rizzo 	{ "ns",		0x0600 },
1849758b77fSLuigi Rizzo 	{ NULL,		0 }
1859758b77fSLuigi Rizzo };
1869758b77fSLuigi Rizzo 
1879758b77fSLuigi Rizzo static void show_usage(void);
1889758b77fSLuigi Rizzo 
1899758b77fSLuigi Rizzo enum tokens {
1909758b77fSLuigi Rizzo 	TOK_NULL=0,
1919758b77fSLuigi Rizzo 
1929758b77fSLuigi Rizzo 	TOK_OR,
1939758b77fSLuigi Rizzo 	TOK_NOT,
1948ed2d749SLuigi Rizzo 	TOK_STARTBRACE,
1958ed2d749SLuigi Rizzo 	TOK_ENDBRACE,
1969758b77fSLuigi Rizzo 
1979758b77fSLuigi Rizzo 	TOK_ACCEPT,
1989758b77fSLuigi Rizzo 	TOK_COUNT,
1999758b77fSLuigi Rizzo 	TOK_PIPE,
2009758b77fSLuigi Rizzo 	TOK_QUEUE,
2019758b77fSLuigi Rizzo 	TOK_DIVERT,
2029758b77fSLuigi Rizzo 	TOK_TEE,
203670742a1SGleb Smirnoff 	TOK_NETGRAPH,
204670742a1SGleb Smirnoff 	TOK_NGTEE,
2059758b77fSLuigi Rizzo 	TOK_FORWARD,
2069758b77fSLuigi Rizzo 	TOK_SKIPTO,
2079758b77fSLuigi Rizzo 	TOK_DENY,
2089758b77fSLuigi Rizzo 	TOK_REJECT,
2099758b77fSLuigi Rizzo 	TOK_RESET,
2109758b77fSLuigi Rizzo 	TOK_UNREACH,
2119758b77fSLuigi Rizzo 	TOK_CHECKSTATE,
2129758b77fSLuigi Rizzo 
213974dfe30SBrian Feldman 	TOK_ALTQ,
214974dfe30SBrian Feldman 	TOK_LOG,
215974dfe30SBrian Feldman 
2169758b77fSLuigi Rizzo 	TOK_UID,
2179758b77fSLuigi Rizzo 	TOK_GID,
21831c88a30SChristian S.J. Peron 	TOK_JAIL,
2199758b77fSLuigi Rizzo 	TOK_IN,
2209758b77fSLuigi Rizzo 	TOK_LIMIT,
2219758b77fSLuigi Rizzo 	TOK_KEEPSTATE,
2229758b77fSLuigi Rizzo 	TOK_LAYER2,
2239758b77fSLuigi Rizzo 	TOK_OUT,
2246daf7ebdSBrian Feldman 	TOK_DIVERTED,
2256daf7ebdSBrian Feldman 	TOK_DIVERTEDLOOPBACK,
2266daf7ebdSBrian Feldman 	TOK_DIVERTEDOUTPUT,
2279758b77fSLuigi Rizzo 	TOK_XMIT,
2289758b77fSLuigi Rizzo 	TOK_RECV,
2299758b77fSLuigi Rizzo 	TOK_VIA,
2309758b77fSLuigi Rizzo 	TOK_FRAG,
2319758b77fSLuigi Rizzo 	TOK_IPOPTS,
2329758b77fSLuigi Rizzo 	TOK_IPLEN,
2339758b77fSLuigi Rizzo 	TOK_IPID,
2349758b77fSLuigi Rizzo 	TOK_IPPRECEDENCE,
2359758b77fSLuigi Rizzo 	TOK_IPTOS,
2369758b77fSLuigi Rizzo 	TOK_IPTTL,
2379758b77fSLuigi Rizzo 	TOK_IPVER,
2389758b77fSLuigi Rizzo 	TOK_ESTAB,
2399758b77fSLuigi Rizzo 	TOK_SETUP,
240c99ee9e0SBrian Feldman 	TOK_TCPDATALEN,
2419758b77fSLuigi Rizzo 	TOK_TCPFLAGS,
2429758b77fSLuigi Rizzo 	TOK_TCPOPTS,
2439758b77fSLuigi Rizzo 	TOK_TCPSEQ,
2449758b77fSLuigi Rizzo 	TOK_TCPACK,
2459758b77fSLuigi Rizzo 	TOK_TCPWIN,
2469758b77fSLuigi Rizzo 	TOK_ICMPTYPES,
247e706181bSLuigi Rizzo 	TOK_MAC,
248e706181bSLuigi Rizzo 	TOK_MACTYPE,
249010dabb0SCrist J. Clark 	TOK_VERREVPATH,
25022b5770bSAndre Oppermann 	TOK_VERSRCREACH,
2515f9541ecSAndre Oppermann 	TOK_ANTISPOOF,
252c3e5b9f1SLuigi Rizzo 	TOK_IPSEC,
25362ff38aeSLuigi Rizzo 	TOK_COMMENT,
2549758b77fSLuigi Rizzo 
2559758b77fSLuigi Rizzo 	TOK_PLR,
25699e5e645SLuigi Rizzo 	TOK_NOERROR,
2579758b77fSLuigi Rizzo 	TOK_BUCKETS,
2589758b77fSLuigi Rizzo 	TOK_DSTIP,
2599758b77fSLuigi Rizzo 	TOK_SRCIP,
2609758b77fSLuigi Rizzo 	TOK_DSTPORT,
2619758b77fSLuigi Rizzo 	TOK_SRCPORT,
2629758b77fSLuigi Rizzo 	TOK_ALL,
2639758b77fSLuigi Rizzo 	TOK_MASK,
2649758b77fSLuigi Rizzo 	TOK_BW,
2659758b77fSLuigi Rizzo 	TOK_DELAY,
2669758b77fSLuigi Rizzo 	TOK_RED,
2679758b77fSLuigi Rizzo 	TOK_GRED,
2689758b77fSLuigi Rizzo 	TOK_DROPTAIL,
2699758b77fSLuigi Rizzo 	TOK_PROTO,
2709758b77fSLuigi Rizzo 	TOK_WEIGHT,
2718195404bSBrooks Davis 
2728195404bSBrooks Davis 	TOK_IPV6,
2738195404bSBrooks Davis 	TOK_FLOWID,
2748195404bSBrooks Davis 	TOK_ICMP6TYPES,
2758195404bSBrooks Davis 	TOK_EXT6HDR,
2768195404bSBrooks Davis 	TOK_DSTIP6,
2778195404bSBrooks Davis 	TOK_SRCIP6,
27857cd6d26SMax Laier 
27957cd6d26SMax Laier 	TOK_IPV4,
2809066356bSBjoern A. Zeeb 	TOK_UNREACH6,
2819066356bSBjoern A. Zeeb 	TOK_RESET6,
2829758b77fSLuigi Rizzo };
2839758b77fSLuigi Rizzo 
2849758b77fSLuigi Rizzo struct _s_x dummynet_params[] = {
2859758b77fSLuigi Rizzo 	{ "plr",		TOK_PLR },
28699e5e645SLuigi Rizzo 	{ "noerror",		TOK_NOERROR },
2879758b77fSLuigi Rizzo 	{ "buckets",		TOK_BUCKETS },
2889758b77fSLuigi Rizzo 	{ "dst-ip",		TOK_DSTIP },
2899758b77fSLuigi Rizzo 	{ "src-ip",		TOK_SRCIP },
2909758b77fSLuigi Rizzo 	{ "dst-port",		TOK_DSTPORT },
2919758b77fSLuigi Rizzo 	{ "src-port",		TOK_SRCPORT },
2929758b77fSLuigi Rizzo 	{ "proto",		TOK_PROTO },
2939758b77fSLuigi Rizzo 	{ "weight",		TOK_WEIGHT },
2949758b77fSLuigi Rizzo 	{ "all",		TOK_ALL },
2959758b77fSLuigi Rizzo 	{ "mask",		TOK_MASK },
2969758b77fSLuigi Rizzo 	{ "droptail",		TOK_DROPTAIL },
2979758b77fSLuigi Rizzo 	{ "red",		TOK_RED },
2989758b77fSLuigi Rizzo 	{ "gred",		TOK_GRED },
2999758b77fSLuigi Rizzo 	{ "bw",			TOK_BW },
3009758b77fSLuigi Rizzo 	{ "bandwidth",		TOK_BW },
3019758b77fSLuigi Rizzo 	{ "delay",		TOK_DELAY },
3025e43aef8SLuigi Rizzo 	{ "pipe",		TOK_PIPE },
3039758b77fSLuigi Rizzo 	{ "queue",		TOK_QUEUE },
3048195404bSBrooks Davis 	{ "flow-id",		TOK_FLOWID},
3058195404bSBrooks Davis 	{ "dst-ipv6",		TOK_DSTIP6},
3068195404bSBrooks Davis 	{ "dst-ip6",		TOK_DSTIP6},
3078195404bSBrooks Davis 	{ "src-ipv6",		TOK_SRCIP6},
3088195404bSBrooks Davis 	{ "src-ip6",		TOK_SRCIP6},
3099758b77fSLuigi Rizzo 	{ "dummynet-params",	TOK_NULL },
310571f8c1bSLuigi Rizzo 	{ NULL, 0 }	/* terminator */
3119758b77fSLuigi Rizzo };
3129758b77fSLuigi Rizzo 
3139758b77fSLuigi Rizzo struct _s_x rule_actions[] = {
3149758b77fSLuigi Rizzo 	{ "accept",		TOK_ACCEPT },
3159758b77fSLuigi Rizzo 	{ "pass",		TOK_ACCEPT },
3169758b77fSLuigi Rizzo 	{ "allow",		TOK_ACCEPT },
3179758b77fSLuigi Rizzo 	{ "permit",		TOK_ACCEPT },
3189758b77fSLuigi Rizzo 	{ "count",		TOK_COUNT },
3199758b77fSLuigi Rizzo 	{ "pipe",		TOK_PIPE },
3209758b77fSLuigi Rizzo 	{ "queue",		TOK_QUEUE },
3219758b77fSLuigi Rizzo 	{ "divert",		TOK_DIVERT },
3229758b77fSLuigi Rizzo 	{ "tee",		TOK_TEE },
323670742a1SGleb Smirnoff 	{ "netgraph",		TOK_NETGRAPH },
324670742a1SGleb Smirnoff 	{ "ngtee",		TOK_NGTEE },
3259758b77fSLuigi Rizzo 	{ "fwd",		TOK_FORWARD },
3269758b77fSLuigi Rizzo 	{ "forward",		TOK_FORWARD },
3279758b77fSLuigi Rizzo 	{ "skipto",		TOK_SKIPTO },
3289758b77fSLuigi Rizzo 	{ "deny",		TOK_DENY },
3299758b77fSLuigi Rizzo 	{ "drop",		TOK_DENY },
3309758b77fSLuigi Rizzo 	{ "reject",		TOK_REJECT },
3319066356bSBjoern A. Zeeb 	{ "reset6",		TOK_RESET6 },
3329758b77fSLuigi Rizzo 	{ "reset",		TOK_RESET },
3339066356bSBjoern A. Zeeb 	{ "unreach6",		TOK_UNREACH6 },
3345e43aef8SLuigi Rizzo 	{ "unreach",		TOK_UNREACH },
3359758b77fSLuigi Rizzo 	{ "check-state",	TOK_CHECKSTATE },
33662ff38aeSLuigi Rizzo 	{ "//",			TOK_COMMENT },
337571f8c1bSLuigi Rizzo 	{ NULL, 0 }	/* terminator */
3389758b77fSLuigi Rizzo };
3399758b77fSLuigi Rizzo 
340974dfe30SBrian Feldman struct _s_x rule_action_params[] = {
341974dfe30SBrian Feldman 	{ "altq",		TOK_ALTQ },
342974dfe30SBrian Feldman 	{ "log",		TOK_LOG },
343974dfe30SBrian Feldman 	{ NULL, 0 }	/* terminator */
344974dfe30SBrian Feldman };
345974dfe30SBrian Feldman 
3469758b77fSLuigi Rizzo struct _s_x rule_options[] = {
3479758b77fSLuigi Rizzo 	{ "uid",		TOK_UID },
3489758b77fSLuigi Rizzo 	{ "gid",		TOK_GID },
34931c88a30SChristian S.J. Peron 	{ "jail",		TOK_JAIL },
3509758b77fSLuigi Rizzo 	{ "in",			TOK_IN },
3519758b77fSLuigi Rizzo 	{ "limit",		TOK_LIMIT },
3529758b77fSLuigi Rizzo 	{ "keep-state",		TOK_KEEPSTATE },
3539758b77fSLuigi Rizzo 	{ "bridged",		TOK_LAYER2 },
3549758b77fSLuigi Rizzo 	{ "layer2",		TOK_LAYER2 },
3559758b77fSLuigi Rizzo 	{ "out",		TOK_OUT },
3566daf7ebdSBrian Feldman 	{ "diverted",		TOK_DIVERTED },
3576daf7ebdSBrian Feldman 	{ "diverted-loopback",	TOK_DIVERTEDLOOPBACK },
3586daf7ebdSBrian Feldman 	{ "diverted-output",	TOK_DIVERTEDOUTPUT },
3599758b77fSLuigi Rizzo 	{ "xmit",		TOK_XMIT },
3609758b77fSLuigi Rizzo 	{ "recv",		TOK_RECV },
3619758b77fSLuigi Rizzo 	{ "via",		TOK_VIA },
3629758b77fSLuigi Rizzo 	{ "fragment",		TOK_FRAG },
3639758b77fSLuigi Rizzo 	{ "frag",		TOK_FRAG },
3649758b77fSLuigi Rizzo 	{ "ipoptions",		TOK_IPOPTS },
3659758b77fSLuigi Rizzo 	{ "ipopts",		TOK_IPOPTS },
3669758b77fSLuigi Rizzo 	{ "iplen",		TOK_IPLEN },
3679758b77fSLuigi Rizzo 	{ "ipid",		TOK_IPID },
3689758b77fSLuigi Rizzo 	{ "ipprecedence",	TOK_IPPRECEDENCE },
3699758b77fSLuigi Rizzo 	{ "iptos",		TOK_IPTOS },
3709758b77fSLuigi Rizzo 	{ "ipttl",		TOK_IPTTL },
3719758b77fSLuigi Rizzo 	{ "ipversion",		TOK_IPVER },
3729758b77fSLuigi Rizzo 	{ "ipver",		TOK_IPVER },
3739758b77fSLuigi Rizzo 	{ "estab",		TOK_ESTAB },
3749758b77fSLuigi Rizzo 	{ "established",	TOK_ESTAB },
3759758b77fSLuigi Rizzo 	{ "setup",		TOK_SETUP },
376c99ee9e0SBrian Feldman 	{ "tcpdatalen",		TOK_TCPDATALEN },
3779758b77fSLuigi Rizzo 	{ "tcpflags",		TOK_TCPFLAGS },
3789758b77fSLuigi Rizzo 	{ "tcpflgs",		TOK_TCPFLAGS },
3799758b77fSLuigi Rizzo 	{ "tcpoptions",		TOK_TCPOPTS },
3809758b77fSLuigi Rizzo 	{ "tcpopts",		TOK_TCPOPTS },
3819758b77fSLuigi Rizzo 	{ "tcpseq",		TOK_TCPSEQ },
3829758b77fSLuigi Rizzo 	{ "tcpack",		TOK_TCPACK },
3839758b77fSLuigi Rizzo 	{ "tcpwin",		TOK_TCPWIN },
3840a7197a8SLuigi Rizzo 	{ "icmptype",		TOK_ICMPTYPES },
3859758b77fSLuigi Rizzo 	{ "icmptypes",		TOK_ICMPTYPES },
386e706181bSLuigi Rizzo 	{ "dst-ip",		TOK_DSTIP },
387e706181bSLuigi Rizzo 	{ "src-ip",		TOK_SRCIP },
388e706181bSLuigi Rizzo 	{ "dst-port",		TOK_DSTPORT },
389e706181bSLuigi Rizzo 	{ "src-port",		TOK_SRCPORT },
390e706181bSLuigi Rizzo 	{ "proto",		TOK_PROTO },
391e706181bSLuigi Rizzo 	{ "MAC",		TOK_MAC },
392e706181bSLuigi Rizzo 	{ "mac",		TOK_MAC },
393e706181bSLuigi Rizzo 	{ "mac-type",		TOK_MACTYPE },
394010dabb0SCrist J. Clark 	{ "verrevpath",		TOK_VERREVPATH },
39522b5770bSAndre Oppermann 	{ "versrcreach",	TOK_VERSRCREACH },
3965f9541ecSAndre Oppermann 	{ "antispoof",		TOK_ANTISPOOF },
397c3e5b9f1SLuigi Rizzo 	{ "ipsec",		TOK_IPSEC },
3988195404bSBrooks Davis 	{ "icmp6type",		TOK_ICMP6TYPES },
3998195404bSBrooks Davis 	{ "icmp6types",		TOK_ICMP6TYPES },
4008195404bSBrooks Davis 	{ "ext6hdr",		TOK_EXT6HDR},
4018195404bSBrooks Davis 	{ "flow-id",		TOK_FLOWID},
4028195404bSBrooks Davis 	{ "ipv6",		TOK_IPV6},
4038195404bSBrooks Davis 	{ "ip6",		TOK_IPV6},
40457cd6d26SMax Laier 	{ "ipv4",		TOK_IPV4},
40557cd6d26SMax Laier 	{ "ip4",		TOK_IPV4},
4068195404bSBrooks Davis 	{ "dst-ipv6",		TOK_DSTIP6},
4078195404bSBrooks Davis 	{ "dst-ip6",		TOK_DSTIP6},
4088195404bSBrooks Davis 	{ "src-ipv6",		TOK_SRCIP6},
4098195404bSBrooks Davis 	{ "src-ip6",		TOK_SRCIP6},
41062ff38aeSLuigi Rizzo 	{ "//",			TOK_COMMENT },
4119758b77fSLuigi Rizzo 
4129758b77fSLuigi Rizzo 	{ "not",		TOK_NOT },		/* pseudo option */
4139758b77fSLuigi Rizzo 	{ "!", /* escape ? */	TOK_NOT },		/* pseudo option */
4149758b77fSLuigi Rizzo 	{ "or",			TOK_OR },		/* pseudo option */
4159758b77fSLuigi Rizzo 	{ "|", /* escape */	TOK_OR },		/* pseudo option */
4168ed2d749SLuigi Rizzo 	{ "{",			TOK_STARTBRACE },	/* pseudo option */
4178ed2d749SLuigi Rizzo 	{ "(",			TOK_STARTBRACE },	/* pseudo option */
4188ed2d749SLuigi Rizzo 	{ "}",			TOK_ENDBRACE },		/* pseudo option */
4198ed2d749SLuigi Rizzo 	{ ")",			TOK_ENDBRACE },		/* pseudo option */
420571f8c1bSLuigi Rizzo 	{ NULL, 0 }	/* terminator */
4219758b77fSLuigi Rizzo };
4229758b77fSLuigi Rizzo 
423571f8c1bSLuigi Rizzo static __inline uint64_t
424571f8c1bSLuigi Rizzo align_uint64(uint64_t *pll) {
425571f8c1bSLuigi Rizzo 	uint64_t ret;
426330462a3SBernd Walter 
427330462a3SBernd Walter 	bcopy (pll, &ret, sizeof(ret));
428330462a3SBernd Walter 	return ret;
429c85c1d27SStefan Farfeleder }
430330462a3SBernd Walter 
431571f8c1bSLuigi Rizzo /*
432571f8c1bSLuigi Rizzo  * conditionally runs the command.
433571f8c1bSLuigi Rizzo  */
43462ff38aeSLuigi Rizzo static int
435884be75cSThomas Moestl do_cmd(int optname, void *optval, uintptr_t optlen)
436571f8c1bSLuigi Rizzo {
437571f8c1bSLuigi Rizzo 	static int s = -1;	/* the socket */
438571f8c1bSLuigi Rizzo 	int i;
439571f8c1bSLuigi Rizzo 
440571f8c1bSLuigi Rizzo 	if (test_only)
441571f8c1bSLuigi Rizzo 		return 0;
442571f8c1bSLuigi Rizzo 
443571f8c1bSLuigi Rizzo 	if (s == -1)
444571f8c1bSLuigi Rizzo 		s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
445571f8c1bSLuigi Rizzo 	if (s < 0)
446571f8c1bSLuigi Rizzo 		err(EX_UNAVAILABLE, "socket");
447571f8c1bSLuigi Rizzo 
448571f8c1bSLuigi Rizzo 	if (optname == IP_FW_GET || optname == IP_DUMMYNET_GET ||
449cd8b5ae0SRuslan Ermilov 	    optname == IP_FW_ADD || optname == IP_FW_TABLE_LIST ||
450cd8b5ae0SRuslan Ermilov 	    optname == IP_FW_TABLE_GETSIZE)
451571f8c1bSLuigi Rizzo 		i = getsockopt(s, IPPROTO_IP, optname, optval,
452571f8c1bSLuigi Rizzo 			(socklen_t *)optlen);
453571f8c1bSLuigi Rizzo 	else
454571f8c1bSLuigi Rizzo 		i = setsockopt(s, IPPROTO_IP, optname, optval, optlen);
455571f8c1bSLuigi Rizzo 	return i;
456571f8c1bSLuigi Rizzo }
457571f8c1bSLuigi Rizzo 
4589758b77fSLuigi Rizzo /**
4599758b77fSLuigi Rizzo  * match_token takes a table and a string, returns the value associated
460571f8c1bSLuigi Rizzo  * with the string (-1 in case of failure).
4619758b77fSLuigi Rizzo  */
4629758b77fSLuigi Rizzo static int
4639758b77fSLuigi Rizzo match_token(struct _s_x *table, char *string)
4649758b77fSLuigi Rizzo {
4659758b77fSLuigi Rizzo 	struct _s_x *pt;
46662ff38aeSLuigi Rizzo 	uint i = strlen(string);
4679758b77fSLuigi Rizzo 
4689758b77fSLuigi Rizzo 	for (pt = table ; i && pt->s != NULL ; pt++)
4699758b77fSLuigi Rizzo 		if (strlen(pt->s) == i && !bcmp(string, pt->s, i))
4709758b77fSLuigi Rizzo 			return pt->x;
4719758b77fSLuigi Rizzo 	return -1;
472c85c1d27SStefan Farfeleder }
4739758b77fSLuigi Rizzo 
474571f8c1bSLuigi Rizzo /**
475571f8c1bSLuigi Rizzo  * match_value takes a table and a value, returns the string associated
476571f8c1bSLuigi Rizzo  * with the value (NULL in case of failure).
477571f8c1bSLuigi Rizzo  */
47862ff38aeSLuigi Rizzo static char const *
47962ff38aeSLuigi Rizzo match_value(struct _s_x *p, int value)
4809758b77fSLuigi Rizzo {
4819758b77fSLuigi Rizzo 	for (; p->s != NULL; p++)
4829758b77fSLuigi Rizzo 		if (p->x == value)
4839758b77fSLuigi Rizzo 			return p->s;
4849758b77fSLuigi Rizzo 	return NULL;
4859758b77fSLuigi Rizzo }
4869758b77fSLuigi Rizzo 
4879758b77fSLuigi Rizzo /*
48801750186SBrooks Davis  * _substrcmp takes two strings and returns 1 if they do not match,
48901750186SBrooks Davis  * and 0 if they match exactly or the first string is a sub-string
49001750186SBrooks Davis  * of the second.  A warning is printed to stderr in the case that the
49101750186SBrooks Davis  * first string is a sub-string of the second.
49201750186SBrooks Davis  *
49301750186SBrooks Davis  * This function will be removed in the future through the usual
49401750186SBrooks Davis  * deprecation process.
49501750186SBrooks Davis  */
49601750186SBrooks Davis static int
49701750186SBrooks Davis _substrcmp(const char *str1, const char* str2)
49801750186SBrooks Davis {
49901750186SBrooks Davis 
50001750186SBrooks Davis 	if (strncmp(str1, str2, strlen(str1)) != 0)
50101750186SBrooks Davis 		return 1;
50201750186SBrooks Davis 
50301750186SBrooks Davis 	if (strlen(str1) != strlen(str2))
50401750186SBrooks Davis 		warnx("DEPRECATED: '%s' matched '%s' as a sub-string",
50501750186SBrooks Davis 		    str1, str2);
50601750186SBrooks Davis 	return 0;
50701750186SBrooks Davis }
50801750186SBrooks Davis 
50901750186SBrooks Davis /*
51001750186SBrooks Davis  * _substrcmp2 takes three strings and returns 1 if the first two do not match,
51101750186SBrooks Davis  * and 0 if they match exactly or the second string is a sub-string
51201750186SBrooks Davis  * of the first.  A warning is printed to stderr in the case that the
51301750186SBrooks Davis  * first string does not match the third.
51401750186SBrooks Davis  *
51501750186SBrooks Davis  * This function exists to warn about the bizzare construction
51601750186SBrooks Davis  * strncmp(str, "by", 2) which is used to allow people to use a shotcut
51701750186SBrooks Davis  * for "bytes".  The problem is that in addition to accepting "by",
51801750186SBrooks Davis  * "byt", "byte", and "bytes", it also excepts "by_rabid_dogs" and any
51901750186SBrooks Davis  * other string beginning with "by".
52001750186SBrooks Davis  *
52101750186SBrooks Davis  * This function will be removed in the future through the usual
52201750186SBrooks Davis  * deprecation process.
52301750186SBrooks Davis  */
52401750186SBrooks Davis static int
52501750186SBrooks Davis _substrcmp2(const char *str1, const char* str2, const char* str3)
52601750186SBrooks Davis {
52701750186SBrooks Davis 
52801750186SBrooks Davis 	if (strncmp(str1, str2, strlen(str2)) != 0)
52901750186SBrooks Davis 		return 1;
53001750186SBrooks Davis 
53101750186SBrooks Davis 	if (strcmp(str1, str3) != 0)
53201750186SBrooks Davis 		warnx("DEPRECATED: '%s' matched '%s'",
53301750186SBrooks Davis 		    str1, str3);
53401750186SBrooks Davis 	return 0;
53501750186SBrooks Davis }
53601750186SBrooks Davis 
53701750186SBrooks Davis /*
5389758b77fSLuigi Rizzo  * prints one port, symbolic or numeric
5399758b77fSLuigi Rizzo  */
5409758b77fSLuigi Rizzo static void
541571f8c1bSLuigi Rizzo print_port(int proto, uint16_t port)
5429758b77fSLuigi Rizzo {
5439758b77fSLuigi Rizzo 
5449758b77fSLuigi Rizzo 	if (proto == IPPROTO_ETHERTYPE) {
54562ff38aeSLuigi Rizzo 		char const *s;
5469758b77fSLuigi Rizzo 
5479758b77fSLuigi Rizzo 		if (do_resolv && (s = match_value(ether_types, port)) )
5489758b77fSLuigi Rizzo 			printf("%s", s);
5499758b77fSLuigi Rizzo 		else
5509758b77fSLuigi Rizzo 			printf("0x%04x", port);
5519758b77fSLuigi Rizzo 	} else {
5529758b77fSLuigi Rizzo 		struct servent *se = NULL;
5539758b77fSLuigi Rizzo 		if (do_resolv) {
5549758b77fSLuigi Rizzo 			struct protoent *pe = getprotobynumber(proto);
5559758b77fSLuigi Rizzo 
5569758b77fSLuigi Rizzo 			se = getservbyport(htons(port), pe ? pe->p_name : NULL);
5579758b77fSLuigi Rizzo 		}
5589758b77fSLuigi Rizzo 		if (se)
5599758b77fSLuigi Rizzo 			printf("%s", se->s_name);
5609758b77fSLuigi Rizzo 		else
5619758b77fSLuigi Rizzo 			printf("%d", port);
5629758b77fSLuigi Rizzo 	}
5639758b77fSLuigi Rizzo }
5649758b77fSLuigi Rizzo 
565571f8c1bSLuigi Rizzo struct _s_x _port_name[] = {
566571f8c1bSLuigi Rizzo 	{"dst-port",	O_IP_DSTPORT},
567571f8c1bSLuigi Rizzo 	{"src-port",	O_IP_SRCPORT},
568571f8c1bSLuigi Rizzo 	{"ipid",	O_IPID},
569571f8c1bSLuigi Rizzo 	{"iplen",	O_IPLEN},
570571f8c1bSLuigi Rizzo 	{"ipttl",	O_IPTTL},
571571f8c1bSLuigi Rizzo 	{"mac-type",	O_MAC_TYPE},
572c99ee9e0SBrian Feldman 	{"tcpdatalen",	O_TCPDATALEN},
573571f8c1bSLuigi Rizzo 	{NULL,		0}
574571f8c1bSLuigi Rizzo };
575571f8c1bSLuigi Rizzo 
5769758b77fSLuigi Rizzo /*
577571f8c1bSLuigi Rizzo  * Print the values in a list 16-bit items of the types above.
5789758b77fSLuigi Rizzo  * XXX todo: add support for mask.
5799758b77fSLuigi Rizzo  */
5809758b77fSLuigi Rizzo static void
581e706181bSLuigi Rizzo print_newports(ipfw_insn_u16 *cmd, int proto, int opcode)
5829758b77fSLuigi Rizzo {
583571f8c1bSLuigi Rizzo 	uint16_t *p = cmd->ports;
5849758b77fSLuigi Rizzo 	int i;
58562ff38aeSLuigi Rizzo 	char const *sep;
5869758b77fSLuigi Rizzo 
5879758b77fSLuigi Rizzo 	if (cmd->o.len & F_NOT)
5889758b77fSLuigi Rizzo 		printf(" not");
58944c884e1SLuigi Rizzo 	if (opcode != 0) {
590571f8c1bSLuigi Rizzo 		sep = match_value(_port_name, opcode);
591571f8c1bSLuigi Rizzo 		if (sep == NULL)
59244c884e1SLuigi Rizzo 			sep = "???";
59344c884e1SLuigi Rizzo 		printf (" %s", sep);
59444c884e1SLuigi Rizzo 	}
59544c884e1SLuigi Rizzo 	sep = " ";
5969758b77fSLuigi Rizzo 	for (i = F_LEN((ipfw_insn *)cmd) - 1; i > 0; i--, p += 2) {
5979758b77fSLuigi Rizzo 		printf(sep);
5989758b77fSLuigi Rizzo 		print_port(proto, p[0]);
5999758b77fSLuigi Rizzo 		if (p[0] != p[1]) {
6009758b77fSLuigi Rizzo 			printf("-");
6019758b77fSLuigi Rizzo 			print_port(proto, p[1]);
6029758b77fSLuigi Rizzo 		}
6039758b77fSLuigi Rizzo 		sep = ",";
6049758b77fSLuigi Rizzo 	}
6059758b77fSLuigi Rizzo }
6069758b77fSLuigi Rizzo 
6079758b77fSLuigi Rizzo /*
6089758b77fSLuigi Rizzo  * Like strtol, but also translates service names into port numbers
6099758b77fSLuigi Rizzo  * for some protocols.
6109758b77fSLuigi Rizzo  * In particular:
6119758b77fSLuigi Rizzo  *	proto == -1 disables the protocol check;
6129758b77fSLuigi Rizzo  *	proto == IPPROTO_ETHERTYPE looks up an internal table
6139758b77fSLuigi Rizzo  *	proto == <some value in /etc/protocols> matches the values there.
61443405724SLuigi Rizzo  * Returns *end == s in case the parameter is not found.
6159758b77fSLuigi Rizzo  */
6169758b77fSLuigi Rizzo static int
6179758b77fSLuigi Rizzo strtoport(char *s, char **end, int base, int proto)
6189758b77fSLuigi Rizzo {
61943405724SLuigi Rizzo 	char *p, *buf;
62043405724SLuigi Rizzo 	char *s1;
6219758b77fSLuigi Rizzo 	int i;
6229758b77fSLuigi Rizzo 
62343405724SLuigi Rizzo 	*end = s;		/* default - not found */
6249758b77fSLuigi Rizzo 	if (*s == '\0')
62543405724SLuigi Rizzo 		return 0;	/* not found */
6269758b77fSLuigi Rizzo 
6279758b77fSLuigi Rizzo 	if (isdigit(*s))
6289758b77fSLuigi Rizzo 		return strtol(s, end, base);
6299758b77fSLuigi Rizzo 
6309758b77fSLuigi Rizzo 	/*
63143405724SLuigi Rizzo 	 * find separator. '\\' escapes the next char.
6329758b77fSLuigi Rizzo 	 */
63343405724SLuigi Rizzo 	for (s1 = s; *s1 && (isalnum(*s1) || *s1 == '\\') ; s1++)
63443405724SLuigi Rizzo 		if (*s1 == '\\' && s1[1] != '\0')
63543405724SLuigi Rizzo 			s1++;
63643405724SLuigi Rizzo 
63743405724SLuigi Rizzo 	buf = malloc(s1 - s + 1);
63843405724SLuigi Rizzo 	if (buf == NULL)
63943405724SLuigi Rizzo 		return 0;
64043405724SLuigi Rizzo 
64143405724SLuigi Rizzo 	/*
64243405724SLuigi Rizzo 	 * copy into a buffer skipping backslashes
64343405724SLuigi Rizzo 	 */
64443405724SLuigi Rizzo 	for (p = s, i = 0; p != s1 ; p++)
64543405724SLuigi Rizzo 		if (*p != '\\')
64643405724SLuigi Rizzo 			buf[i++] = *p;
64743405724SLuigi Rizzo 	buf[i++] = '\0';
6489758b77fSLuigi Rizzo 
6499758b77fSLuigi Rizzo 	if (proto == IPPROTO_ETHERTYPE) {
65043405724SLuigi Rizzo 		i = match_token(ether_types, buf);
65143405724SLuigi Rizzo 		free(buf);
65243405724SLuigi Rizzo 		if (i != -1) {	/* found */
6539758b77fSLuigi Rizzo 			*end = s1;
6549758b77fSLuigi Rizzo 			return i;
6559758b77fSLuigi Rizzo 		}
6569758b77fSLuigi Rizzo 	} else {
6579758b77fSLuigi Rizzo 		struct protoent *pe = NULL;
6589758b77fSLuigi Rizzo 		struct servent *se;
6599758b77fSLuigi Rizzo 
6609758b77fSLuigi Rizzo 		if (proto != 0)
6619758b77fSLuigi Rizzo 			pe = getprotobynumber(proto);
6629758b77fSLuigi Rizzo 		setservent(1);
66343405724SLuigi Rizzo 		se = getservbyname(buf, pe ? pe->p_name : NULL);
66443405724SLuigi Rizzo 		free(buf);
6659758b77fSLuigi Rizzo 		if (se != NULL) {
6669758b77fSLuigi Rizzo 			*end = s1;
6679758b77fSLuigi Rizzo 			return ntohs(se->s_port);
6689758b77fSLuigi Rizzo 		}
6699758b77fSLuigi Rizzo 	}
67043405724SLuigi Rizzo 	return 0;	/* not found */
6719758b77fSLuigi Rizzo }
6729758b77fSLuigi Rizzo 
6739758b77fSLuigi Rizzo /*
674974dfe30SBrian Feldman  * Map between current altq queue id numbers and names.
675974dfe30SBrian Feldman  */
676974dfe30SBrian Feldman static int altq_fetched = 0;
677974dfe30SBrian Feldman static TAILQ_HEAD(, pf_altq) altq_entries =
678974dfe30SBrian Feldman 	TAILQ_HEAD_INITIALIZER(altq_entries);
679974dfe30SBrian Feldman 
680974dfe30SBrian Feldman static void
681974dfe30SBrian Feldman altq_set_enabled(int enabled)
682974dfe30SBrian Feldman {
683974dfe30SBrian Feldman 	int pffd;
684974dfe30SBrian Feldman 
685974dfe30SBrian Feldman 	pffd = open("/dev/pf", O_RDWR);
686974dfe30SBrian Feldman 	if (pffd == -1)
687974dfe30SBrian Feldman 		err(EX_UNAVAILABLE,
688974dfe30SBrian Feldman 		    "altq support opening pf(4) control device");
689974dfe30SBrian Feldman 	if (enabled) {
690974dfe30SBrian Feldman 		if (ioctl(pffd, DIOCSTARTALTQ) != 0 && errno != EEXIST)
691974dfe30SBrian Feldman 			err(EX_UNAVAILABLE, "enabling altq");
692974dfe30SBrian Feldman 	} else {
693974dfe30SBrian Feldman 		if (ioctl(pffd, DIOCSTOPALTQ) != 0 && errno != ENOENT)
694974dfe30SBrian Feldman 			err(EX_UNAVAILABLE, "disabling altq");
695974dfe30SBrian Feldman 	}
696974dfe30SBrian Feldman 	close(pffd);
697974dfe30SBrian Feldman }
698974dfe30SBrian Feldman 
699974dfe30SBrian Feldman static void
700974dfe30SBrian Feldman altq_fetch()
701974dfe30SBrian Feldman {
702974dfe30SBrian Feldman 	struct pfioc_altq pfioc;
703974dfe30SBrian Feldman 	struct pf_altq *altq;
704974dfe30SBrian Feldman 	int pffd, mnr;
705974dfe30SBrian Feldman 
706974dfe30SBrian Feldman 	if (altq_fetched)
707974dfe30SBrian Feldman 		return;
708974dfe30SBrian Feldman 	altq_fetched = 1;
709974dfe30SBrian Feldman 	pffd = open("/dev/pf", O_RDONLY);
710974dfe30SBrian Feldman 	if (pffd == -1) {
711974dfe30SBrian Feldman 		warn("altq support opening pf(4) control device");
712974dfe30SBrian Feldman 		return;
713974dfe30SBrian Feldman 	}
714974dfe30SBrian Feldman 	bzero(&pfioc, sizeof(pfioc));
715974dfe30SBrian Feldman 	if (ioctl(pffd, DIOCGETALTQS, &pfioc) != 0) {
716974dfe30SBrian Feldman 		warn("altq support getting queue list");
717974dfe30SBrian Feldman 		close(pffd);
718974dfe30SBrian Feldman 		return;
719974dfe30SBrian Feldman 	}
720974dfe30SBrian Feldman 	mnr = pfioc.nr;
721974dfe30SBrian Feldman 	for (pfioc.nr = 0; pfioc.nr < mnr; pfioc.nr++) {
722974dfe30SBrian Feldman 		if (ioctl(pffd, DIOCGETALTQ, &pfioc) != 0) {
723974dfe30SBrian Feldman 			if (errno == EBUSY)
724974dfe30SBrian Feldman 				break;
725974dfe30SBrian Feldman 			warn("altq support getting queue list");
726974dfe30SBrian Feldman 			close(pffd);
727974dfe30SBrian Feldman 			return;
728974dfe30SBrian Feldman 		}
729974dfe30SBrian Feldman 		if (pfioc.altq.qid == 0)
730974dfe30SBrian Feldman 			continue;
731974dfe30SBrian Feldman 		altq = malloc(sizeof(*altq));
732974dfe30SBrian Feldman 		if (altq == NULL)
733974dfe30SBrian Feldman 			err(EX_OSERR, "malloc");
734974dfe30SBrian Feldman 		*altq = pfioc.altq;
735974dfe30SBrian Feldman 		TAILQ_INSERT_TAIL(&altq_entries, altq, entries);
736974dfe30SBrian Feldman 	}
737974dfe30SBrian Feldman 	close(pffd);
738974dfe30SBrian Feldman }
739974dfe30SBrian Feldman 
740974dfe30SBrian Feldman static u_int32_t
741974dfe30SBrian Feldman altq_name_to_qid(const char *name)
742974dfe30SBrian Feldman {
743974dfe30SBrian Feldman 	struct pf_altq *altq;
744974dfe30SBrian Feldman 
745974dfe30SBrian Feldman 	altq_fetch();
746974dfe30SBrian Feldman 	TAILQ_FOREACH(altq, &altq_entries, entries)
747974dfe30SBrian Feldman 		if (strcmp(name, altq->qname) == 0)
748974dfe30SBrian Feldman 			break;
749974dfe30SBrian Feldman 	if (altq == NULL)
750974dfe30SBrian Feldman 		errx(EX_DATAERR, "altq has no queue named `%s'", name);
751974dfe30SBrian Feldman 	return altq->qid;
752974dfe30SBrian Feldman }
753974dfe30SBrian Feldman 
754974dfe30SBrian Feldman static const char *
755974dfe30SBrian Feldman altq_qid_to_name(u_int32_t qid)
756974dfe30SBrian Feldman {
757974dfe30SBrian Feldman 	struct pf_altq *altq;
758974dfe30SBrian Feldman 
759974dfe30SBrian Feldman 	altq_fetch();
760974dfe30SBrian Feldman 	TAILQ_FOREACH(altq, &altq_entries, entries)
761974dfe30SBrian Feldman 		if (qid == altq->qid)
762974dfe30SBrian Feldman 			break;
763974dfe30SBrian Feldman 	if (altq == NULL)
764974dfe30SBrian Feldman 		return NULL;
765974dfe30SBrian Feldman 	return altq->qname;
766974dfe30SBrian Feldman }
767974dfe30SBrian Feldman 
768974dfe30SBrian Feldman static void
769974dfe30SBrian Feldman fill_altq_qid(u_int32_t *qid, const char *av)
770974dfe30SBrian Feldman {
771974dfe30SBrian Feldman 	*qid = altq_name_to_qid(av);
772974dfe30SBrian Feldman }
773974dfe30SBrian Feldman 
774974dfe30SBrian Feldman /*
775571f8c1bSLuigi Rizzo  * Fill the body of the command with the list of port ranges.
7769758b77fSLuigi Rizzo  */
7779758b77fSLuigi Rizzo static int
7789758b77fSLuigi Rizzo fill_newports(ipfw_insn_u16 *cmd, char *av, int proto)
7799758b77fSLuigi Rizzo {
780571f8c1bSLuigi Rizzo 	uint16_t a, b, *p = cmd->ports;
7819758b77fSLuigi Rizzo 	int i = 0;
782e706181bSLuigi Rizzo 	char *s = av;
7839758b77fSLuigi Rizzo 
784e706181bSLuigi Rizzo 	while (*s) {
7859758b77fSLuigi Rizzo 		a = strtoport(av, &s, 0, proto);
7869758b77fSLuigi Rizzo 		if (s == av) /* no parameter */
7879758b77fSLuigi Rizzo 			break;
7889758b77fSLuigi Rizzo 		if (*s == '-') { /* a range */
7899758b77fSLuigi Rizzo 			av = s+1;
7909758b77fSLuigi Rizzo 			b = strtoport(av, &s, 0, proto);
7919758b77fSLuigi Rizzo 			if (s == av) /* no parameter */
7929758b77fSLuigi Rizzo 				break;
7939758b77fSLuigi Rizzo 			p[0] = a;
7949758b77fSLuigi Rizzo 			p[1] = b;
795571f8c1bSLuigi Rizzo 		} else if (*s == ',' || *s == '\0' )
7969758b77fSLuigi Rizzo 			p[0] = p[1] = a;
797571f8c1bSLuigi Rizzo 		else 	/* invalid separator */
79899e5e645SLuigi Rizzo 			errx(EX_DATAERR, "invalid separator <%c> in <%s>\n",
79999e5e645SLuigi Rizzo 				*s, av);
800e706181bSLuigi Rizzo 		i++;
801e706181bSLuigi Rizzo 		p += 2;
8029758b77fSLuigi Rizzo 		av = s+1;
8039758b77fSLuigi Rizzo 	}
8049758b77fSLuigi Rizzo 	if (i > 0) {
8059758b77fSLuigi Rizzo 		if (i+1 > F_LEN_MASK)
806e706181bSLuigi Rizzo 			errx(EX_DATAERR, "too many ports/ranges\n");
8079758b77fSLuigi Rizzo 		cmd->o.len |= i+1; /* leave F_NOT and F_OR untouched */
8089758b77fSLuigi Rizzo 	}
8099758b77fSLuigi Rizzo 	return i;
8109758b77fSLuigi Rizzo }
8119758b77fSLuigi Rizzo 
8129758b77fSLuigi Rizzo static struct _s_x icmpcodes[] = {
8139758b77fSLuigi Rizzo       { "net",			ICMP_UNREACH_NET },
8149758b77fSLuigi Rizzo       { "host",			ICMP_UNREACH_HOST },
8159758b77fSLuigi Rizzo       { "protocol",		ICMP_UNREACH_PROTOCOL },
8169758b77fSLuigi Rizzo       { "port",			ICMP_UNREACH_PORT },
8179758b77fSLuigi Rizzo       { "needfrag",		ICMP_UNREACH_NEEDFRAG },
8189758b77fSLuigi Rizzo       { "srcfail",		ICMP_UNREACH_SRCFAIL },
8199758b77fSLuigi Rizzo       { "net-unknown",		ICMP_UNREACH_NET_UNKNOWN },
8209758b77fSLuigi Rizzo       { "host-unknown",		ICMP_UNREACH_HOST_UNKNOWN },
8219758b77fSLuigi Rizzo       { "isolated",		ICMP_UNREACH_ISOLATED },
8229758b77fSLuigi Rizzo       { "net-prohib",		ICMP_UNREACH_NET_PROHIB },
8239758b77fSLuigi Rizzo       { "host-prohib",		ICMP_UNREACH_HOST_PROHIB },
8249758b77fSLuigi Rizzo       { "tosnet",		ICMP_UNREACH_TOSNET },
8259758b77fSLuigi Rizzo       { "toshost",		ICMP_UNREACH_TOSHOST },
8269758b77fSLuigi Rizzo       { "filter-prohib",	ICMP_UNREACH_FILTER_PROHIB },
8279758b77fSLuigi Rizzo       { "host-precedence",	ICMP_UNREACH_HOST_PRECEDENCE },
8289758b77fSLuigi Rizzo       { "precedence-cutoff",	ICMP_UNREACH_PRECEDENCE_CUTOFF },
8299758b77fSLuigi Rizzo       { NULL, 0 }
8309758b77fSLuigi Rizzo };
8319758b77fSLuigi Rizzo 
8329758b77fSLuigi Rizzo static void
8339758b77fSLuigi Rizzo fill_reject_code(u_short *codep, char *str)
8349758b77fSLuigi Rizzo {
8359758b77fSLuigi Rizzo 	int val;
8369758b77fSLuigi Rizzo 	char *s;
8379758b77fSLuigi Rizzo 
8389758b77fSLuigi Rizzo 	val = strtoul(str, &s, 0);
8399758b77fSLuigi Rizzo 	if (s == str || *s != '\0' || val >= 0x100)
8409758b77fSLuigi Rizzo 		val = match_token(icmpcodes, str);
841e706181bSLuigi Rizzo 	if (val < 0)
8429758b77fSLuigi Rizzo 		errx(EX_DATAERR, "unknown ICMP unreachable code ``%s''", str);
8439758b77fSLuigi Rizzo 	*codep = val;
8449758b77fSLuigi Rizzo 	return;
8459758b77fSLuigi Rizzo }
8469758b77fSLuigi Rizzo 
8479758b77fSLuigi Rizzo static void
848571f8c1bSLuigi Rizzo print_reject_code(uint16_t code)
8499758b77fSLuigi Rizzo {
85062ff38aeSLuigi Rizzo 	char const *s = match_value(icmpcodes, code);
8519758b77fSLuigi Rizzo 
8529758b77fSLuigi Rizzo 	if (s != NULL)
8535e43aef8SLuigi Rizzo 		printf("unreach %s", s);
8549758b77fSLuigi Rizzo 	else
8555e43aef8SLuigi Rizzo 		printf("unreach %u", code);
8569758b77fSLuigi Rizzo }
8579758b77fSLuigi Rizzo 
8589066356bSBjoern A. Zeeb static struct _s_x icmp6codes[] = {
8599066356bSBjoern A. Zeeb       { "no-route",		ICMP6_DST_UNREACH_NOROUTE },
8609066356bSBjoern A. Zeeb       { "admin-prohib",		ICMP6_DST_UNREACH_ADMIN },
8619066356bSBjoern A. Zeeb       { "address",		ICMP6_DST_UNREACH_ADDR },
8629066356bSBjoern A. Zeeb       { "port",			ICMP6_DST_UNREACH_NOPORT },
8639066356bSBjoern A. Zeeb       { NULL, 0 }
8649066356bSBjoern A. Zeeb };
8659066356bSBjoern A. Zeeb 
8669066356bSBjoern A. Zeeb static void
8679066356bSBjoern A. Zeeb fill_unreach6_code(u_short *codep, char *str)
8689066356bSBjoern A. Zeeb {
8699066356bSBjoern A. Zeeb 	int val;
8709066356bSBjoern A. Zeeb 	char *s;
8719066356bSBjoern A. Zeeb 
8729066356bSBjoern A. Zeeb 	val = strtoul(str, &s, 0);
8739066356bSBjoern A. Zeeb 	if (s == str || *s != '\0' || val >= 0x100)
8749066356bSBjoern A. Zeeb 		val = match_token(icmp6codes, str);
8759066356bSBjoern A. Zeeb 	if (val < 0)
8769066356bSBjoern A. Zeeb 		errx(EX_DATAERR, "unknown ICMPv6 unreachable code ``%s''", str);
8779066356bSBjoern A. Zeeb 	*codep = val;
8789066356bSBjoern A. Zeeb 	return;
8799066356bSBjoern A. Zeeb }
8809066356bSBjoern A. Zeeb 
8819066356bSBjoern A. Zeeb static void
8829066356bSBjoern A. Zeeb print_unreach6_code(uint16_t code)
8839066356bSBjoern A. Zeeb {
8849066356bSBjoern A. Zeeb 	char const *s = match_value(icmp6codes, code);
8859066356bSBjoern A. Zeeb 
8869066356bSBjoern A. Zeeb 	if (s != NULL)
8879066356bSBjoern A. Zeeb 		printf("unreach6 %s", s);
8889066356bSBjoern A. Zeeb 	else
8899066356bSBjoern A. Zeeb 		printf("unreach6 %u", code);
8909066356bSBjoern A. Zeeb }
8919066356bSBjoern A. Zeeb 
8929758b77fSLuigi Rizzo /*
8939758b77fSLuigi Rizzo  * Returns the number of bits set (from left) in a contiguous bitmask,
8949758b77fSLuigi Rizzo  * or -1 if the mask is not contiguous.
8959758b77fSLuigi Rizzo  * XXX this needs a proper fix.
8969758b77fSLuigi Rizzo  * This effectively works on masks in big-endian (network) format.
8979758b77fSLuigi Rizzo  * when compiled on little endian architectures.
8989758b77fSLuigi Rizzo  *
8999758b77fSLuigi Rizzo  * First bit is bit 7 of the first byte -- note, for MAC addresses,
9009758b77fSLuigi Rizzo  * the first bit on the wire is bit 0 of the first byte.
9019758b77fSLuigi Rizzo  * len is the max length in bits.
9029758b77fSLuigi Rizzo  */
9039758b77fSLuigi Rizzo static int
904f3a126d3SLuigi Rizzo contigmask(uint8_t *p, int len)
9059758b77fSLuigi Rizzo {
9069758b77fSLuigi Rizzo 	int i, n;
907f3a126d3SLuigi Rizzo 
9089758b77fSLuigi Rizzo 	for (i=0; i<len ; i++)
9099758b77fSLuigi Rizzo 		if ( (p[i/8] & (1 << (7 - (i%8)))) == 0) /* first bit unset */
9109758b77fSLuigi Rizzo 			break;
9119758b77fSLuigi Rizzo 	for (n=i+1; n < len; n++)
9129758b77fSLuigi Rizzo 		if ( (p[n/8] & (1 << (7 - (n%8)))) != 0)
9139758b77fSLuigi Rizzo 			return -1; /* mask not contiguous */
9149758b77fSLuigi Rizzo 	return i;
9159758b77fSLuigi Rizzo }
9169758b77fSLuigi Rizzo 
9179758b77fSLuigi Rizzo /*
9189758b77fSLuigi Rizzo  * print flags set/clear in the two bitmasks passed as parameters.
9199758b77fSLuigi Rizzo  * There is a specialized check for f_tcpflags.
9209758b77fSLuigi Rizzo  */
9219758b77fSLuigi Rizzo static void
92262ff38aeSLuigi Rizzo print_flags(char const *name, ipfw_insn *cmd, struct _s_x *list)
9239758b77fSLuigi Rizzo {
92462ff38aeSLuigi Rizzo 	char const *comma = "";
9259758b77fSLuigi Rizzo 	int i;
926f3a126d3SLuigi Rizzo 	uint8_t set = cmd->arg1 & 0xff;
927f3a126d3SLuigi Rizzo 	uint8_t clear = (cmd->arg1 >> 8) & 0xff;
9289758b77fSLuigi Rizzo 
9299758b77fSLuigi Rizzo 	if (list == f_tcpflags && set == TH_SYN && clear == TH_ACK) {
9309758b77fSLuigi Rizzo 		printf(" setup");
9319758b77fSLuigi Rizzo 		return;
9329758b77fSLuigi Rizzo 	}
9339758b77fSLuigi Rizzo 
9349758b77fSLuigi Rizzo 	printf(" %s ", name);
9359758b77fSLuigi Rizzo 	for (i=0; list[i].x != 0; i++) {
9369758b77fSLuigi Rizzo 		if (set & list[i].x) {
9379758b77fSLuigi Rizzo 			set &= ~list[i].x;
9389758b77fSLuigi Rizzo 			printf("%s%s", comma, list[i].s);
9399758b77fSLuigi Rizzo 			comma = ",";
9409758b77fSLuigi Rizzo 		}
9419758b77fSLuigi Rizzo 		if (clear & list[i].x) {
9429758b77fSLuigi Rizzo 			clear &= ~list[i].x;
9439758b77fSLuigi Rizzo 			printf("%s!%s", comma, list[i].s);
9449758b77fSLuigi Rizzo 			comma = ",";
9459758b77fSLuigi Rizzo 		}
9469758b77fSLuigi Rizzo 	}
9479758b77fSLuigi Rizzo }
9489758b77fSLuigi Rizzo 
9499758b77fSLuigi Rizzo /*
9509758b77fSLuigi Rizzo  * Print the ip address contained in a command.
9519758b77fSLuigi Rizzo  */
9529758b77fSLuigi Rizzo static void
95362ff38aeSLuigi Rizzo print_ip(ipfw_insn_ip *cmd, char const *s)
9549758b77fSLuigi Rizzo {
9559758b77fSLuigi Rizzo 	struct hostent *he = NULL;
956571f8c1bSLuigi Rizzo 	int len = F_LEN((ipfw_insn *)cmd);
957571f8c1bSLuigi Rizzo 	uint32_t *a = ((ipfw_insn_u32 *)cmd)->d;
9589758b77fSLuigi Rizzo 
959e706181bSLuigi Rizzo 	printf("%s%s ", cmd->o.len & F_NOT ? " not": "", s);
9609758b77fSLuigi Rizzo 
9619758b77fSLuigi Rizzo 	if (cmd->o.opcode == O_IP_SRC_ME || cmd->o.opcode == O_IP_DST_ME) {
9629758b77fSLuigi Rizzo 		printf("me");
9639758b77fSLuigi Rizzo 		return;
9649758b77fSLuigi Rizzo 	}
965cd8b5ae0SRuslan Ermilov 	if (cmd->o.opcode == O_IP_SRC_LOOKUP ||
966cd8b5ae0SRuslan Ermilov 	    cmd->o.opcode == O_IP_DST_LOOKUP) {
967cd8b5ae0SRuslan Ermilov 		printf("table(%u", ((ipfw_insn *)cmd)->arg1);
968cd8b5ae0SRuslan Ermilov 		if (len == F_INSN_SIZE(ipfw_insn_u32))
969cd8b5ae0SRuslan Ermilov 			printf(",%u", *a);
970cd8b5ae0SRuslan Ermilov 		printf(")");
971cd8b5ae0SRuslan Ermilov 		return;
972cd8b5ae0SRuslan Ermilov 	}
9739758b77fSLuigi Rizzo 	if (cmd->o.opcode == O_IP_SRC_SET || cmd->o.opcode == O_IP_DST_SET) {
974571f8c1bSLuigi Rizzo 		uint32_t x, *map = (uint32_t *)&(cmd->mask);
9759ef3f16dSLuigi Rizzo 		int i, j;
9769758b77fSLuigi Rizzo 		char comma = '{';
9779758b77fSLuigi Rizzo 
9789758b77fSLuigi Rizzo 		x = cmd->o.arg1 - 1;
9799758b77fSLuigi Rizzo 		x = htonl( ~x );
9809758b77fSLuigi Rizzo 		cmd->addr.s_addr = htonl(cmd->addr.s_addr);
9819758b77fSLuigi Rizzo 		printf("%s/%d", inet_ntoa(cmd->addr),
982f3a126d3SLuigi Rizzo 			contigmask((uint8_t *)&x, 32));
9839758b77fSLuigi Rizzo 		x = cmd->addr.s_addr = htonl(cmd->addr.s_addr);
9849758b77fSLuigi Rizzo 		x &= 0xff; /* base */
9859ef3f16dSLuigi Rizzo 		/*
9869ef3f16dSLuigi Rizzo 		 * Print bits and ranges.
9879ef3f16dSLuigi Rizzo 		 * Locate first bit set (i), then locate first bit unset (j).
9889ef3f16dSLuigi Rizzo 		 * If we have 3+ consecutive bits set, then print them as a
9899ef3f16dSLuigi Rizzo 		 * range, otherwise only print the initial bit and rescan.
9909ef3f16dSLuigi Rizzo 		 */
9919758b77fSLuigi Rizzo 		for (i=0; i < cmd->o.arg1; i++)
992571f8c1bSLuigi Rizzo 			if (map[i/32] & (1<<(i & 31))) {
9939ef3f16dSLuigi Rizzo 				for (j=i+1; j < cmd->o.arg1; j++)
994571f8c1bSLuigi Rizzo 					if (!(map[ j/32] & (1<<(j & 31))))
9959ef3f16dSLuigi Rizzo 						break;
9969758b77fSLuigi Rizzo 				printf("%c%d", comma, i+x);
9979ef3f16dSLuigi Rizzo 				if (j>i+2) { /* range has at least 3 elements */
9989ef3f16dSLuigi Rizzo 					printf("-%d", j-1+x);
9999ef3f16dSLuigi Rizzo 					i = j-1;
10009ef3f16dSLuigi Rizzo 				}
10019758b77fSLuigi Rizzo 				comma = ',';
10029758b77fSLuigi Rizzo 			}
10039758b77fSLuigi Rizzo 		printf("}");
10049758b77fSLuigi Rizzo 		return;
10059758b77fSLuigi Rizzo 	}
1006571f8c1bSLuigi Rizzo 	/*
1007571f8c1bSLuigi Rizzo 	 * len == 2 indicates a single IP, whereas lists of 1 or more
1008571f8c1bSLuigi Rizzo 	 * addr/mask pairs have len = (2n+1). We convert len to n so we
1009571f8c1bSLuigi Rizzo 	 * use that to count the number of entries.
1010571f8c1bSLuigi Rizzo 	 */
1011571f8c1bSLuigi Rizzo     for (len = len / 2; len > 0; len--, a += 2) {
1012571f8c1bSLuigi Rizzo 	int mb =	/* mask length */
1013571f8c1bSLuigi Rizzo 	    (cmd->o.opcode == O_IP_SRC || cmd->o.opcode == O_IP_DST) ?
1014f3a126d3SLuigi Rizzo 		32 : contigmask((uint8_t *)&(a[1]), 32);
10159758b77fSLuigi Rizzo 	if (mb == 32 && do_resolv)
1016571f8c1bSLuigi Rizzo 		he = gethostbyaddr((char *)&(a[0]), sizeof(u_long), AF_INET);
10179758b77fSLuigi Rizzo 	if (he != NULL)		/* resolved to name */
10189758b77fSLuigi Rizzo 		printf("%s", he->h_name);
10199758b77fSLuigi Rizzo 	else if (mb == 0)	/* any */
10209758b77fSLuigi Rizzo 		printf("any");
10219758b77fSLuigi Rizzo 	else {		/* numeric IP followed by some kind of mask */
1022571f8c1bSLuigi Rizzo 		printf("%s", inet_ntoa( *((struct in_addr *)&a[0]) ) );
10239758b77fSLuigi Rizzo 		if (mb < 0)
1024571f8c1bSLuigi Rizzo 			printf(":%s", inet_ntoa( *((struct in_addr *)&a[1]) ) );
10259758b77fSLuigi Rizzo 		else if (mb < 32)
10269758b77fSLuigi Rizzo 			printf("/%d", mb);
10279758b77fSLuigi Rizzo 	}
1028571f8c1bSLuigi Rizzo 	if (len > 1)
1029571f8c1bSLuigi Rizzo 		printf(",");
1030571f8c1bSLuigi Rizzo     }
10319758b77fSLuigi Rizzo }
10329758b77fSLuigi Rizzo 
10339758b77fSLuigi Rizzo /*
10349758b77fSLuigi Rizzo  * prints a MAC address/mask pair
10359758b77fSLuigi Rizzo  */
10369758b77fSLuigi Rizzo static void
1037f3a126d3SLuigi Rizzo print_mac(uint8_t *addr, uint8_t *mask)
10389758b77fSLuigi Rizzo {
10399758b77fSLuigi Rizzo 	int l = contigmask(mask, 48);
10409758b77fSLuigi Rizzo 
10419758b77fSLuigi Rizzo 	if (l == 0)
10429758b77fSLuigi Rizzo 		printf(" any");
10439758b77fSLuigi Rizzo 	else {
10449758b77fSLuigi Rizzo 		printf(" %02x:%02x:%02x:%02x:%02x:%02x",
10459758b77fSLuigi Rizzo 		    addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
10469758b77fSLuigi Rizzo 		if (l == -1)
10479758b77fSLuigi Rizzo 			printf("&%02x:%02x:%02x:%02x:%02x:%02x",
10489758b77fSLuigi Rizzo 			    mask[0], mask[1], mask[2],
10499758b77fSLuigi Rizzo 			    mask[3], mask[4], mask[5]);
10509758b77fSLuigi Rizzo 		else if (l < 48)
10519758b77fSLuigi Rizzo 			printf("/%d", l);
10529758b77fSLuigi Rizzo 	}
10539758b77fSLuigi Rizzo }
10549758b77fSLuigi Rizzo 
10555e43aef8SLuigi Rizzo static void
10565e43aef8SLuigi Rizzo fill_icmptypes(ipfw_insn_u32 *cmd, char *av)
10575e43aef8SLuigi Rizzo {
1058571f8c1bSLuigi Rizzo 	uint8_t type;
10595e43aef8SLuigi Rizzo 
10605e43aef8SLuigi Rizzo 	cmd->d[0] = 0;
10615e43aef8SLuigi Rizzo 	while (*av) {
10625e43aef8SLuigi Rizzo 		if (*av == ',')
10635e43aef8SLuigi Rizzo 			av++;
10645e43aef8SLuigi Rizzo 
10655e43aef8SLuigi Rizzo 		type = strtoul(av, &av, 0);
10665e43aef8SLuigi Rizzo 
10675e43aef8SLuigi Rizzo 		if (*av != ',' && *av != '\0')
10685e43aef8SLuigi Rizzo 			errx(EX_DATAERR, "invalid ICMP type");
10695e43aef8SLuigi Rizzo 
10705e43aef8SLuigi Rizzo 		if (type > 31)
10715e43aef8SLuigi Rizzo 			errx(EX_DATAERR, "ICMP type out of range");
10725e43aef8SLuigi Rizzo 
10735e43aef8SLuigi Rizzo 		cmd->d[0] |= 1 << type;
10745e43aef8SLuigi Rizzo 	}
10755e43aef8SLuigi Rizzo 	cmd->o.opcode = O_ICMPTYPE;
10765e43aef8SLuigi Rizzo 	cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32);
10775e43aef8SLuigi Rizzo }
10785e43aef8SLuigi Rizzo 
10795e43aef8SLuigi Rizzo static void
10805e43aef8SLuigi Rizzo print_icmptypes(ipfw_insn_u32 *cmd)
10815e43aef8SLuigi Rizzo {
10825e43aef8SLuigi Rizzo 	int i;
10835e43aef8SLuigi Rizzo 	char sep= ' ';
10845e43aef8SLuigi Rizzo 
10855e43aef8SLuigi Rizzo 	printf(" icmptypes");
10865e43aef8SLuigi Rizzo 	for (i = 0; i < 32; i++) {
10875e43aef8SLuigi Rizzo 		if ( (cmd->d[0] & (1 << (i))) == 0)
10885e43aef8SLuigi Rizzo 			continue;
10895e43aef8SLuigi Rizzo 		printf("%c%d", sep, i);
10905e43aef8SLuigi Rizzo 		sep = ',';
10915e43aef8SLuigi Rizzo 	}
10925e43aef8SLuigi Rizzo }
10939758b77fSLuigi Rizzo 
10949758b77fSLuigi Rizzo /*
10958195404bSBrooks Davis  * Print the ip address contained in a command.
10968195404bSBrooks Davis  */
10978195404bSBrooks Davis static void
10988195404bSBrooks Davis print_ip6(ipfw_insn_ip6 *cmd, char const *s)
10998195404bSBrooks Davis {
11008195404bSBrooks Davis        struct hostent *he = NULL;
11018195404bSBrooks Davis        int len = F_LEN((ipfw_insn *) cmd) - 1;
11028195404bSBrooks Davis        struct in6_addr *a = &(cmd->addr6);
11038195404bSBrooks Davis        char trad[255];
11048195404bSBrooks Davis 
11058195404bSBrooks Davis        printf("%s%s ", cmd->o.len & F_NOT ? " not": "", s);
11068195404bSBrooks Davis 
11078195404bSBrooks Davis        if (cmd->o.opcode == O_IP6_SRC_ME || cmd->o.opcode == O_IP6_DST_ME) {
11088195404bSBrooks Davis                printf("me6");
11098195404bSBrooks Davis                return;
11108195404bSBrooks Davis        }
11118195404bSBrooks Davis        if (cmd->o.opcode == O_IP6) {
111236c263ccSHajimu UMEMOTO                printf(" ip6");
11138195404bSBrooks Davis                return;
11148195404bSBrooks Davis        }
11158195404bSBrooks Davis 
11168195404bSBrooks Davis        /*
11178195404bSBrooks Davis         * len == 4 indicates a single IP, whereas lists of 1 or more
11188195404bSBrooks Davis         * addr/mask pairs have len = (2n+1). We convert len to n so we
11198195404bSBrooks Davis         * use that to count the number of entries.
11208195404bSBrooks Davis         */
11218195404bSBrooks Davis 
11228195404bSBrooks Davis        for (len = len / 4; len > 0; len -= 2, a += 2) {
11238195404bSBrooks Davis            int mb =        /* mask length */
11248195404bSBrooks Davis                (cmd->o.opcode == O_IP6_SRC || cmd->o.opcode == O_IP6_DST) ?
11258195404bSBrooks Davis                128 : contigmask((uint8_t *)&(a[1]), 128);
11268195404bSBrooks Davis 
11278195404bSBrooks Davis            if (mb == 128 && do_resolv)
11288195404bSBrooks Davis                he = gethostbyaddr((char *)a, sizeof(*a), AF_INET6);
11298195404bSBrooks Davis            if (he != NULL)             /* resolved to name */
11308195404bSBrooks Davis                printf("%s", he->h_name);
11318195404bSBrooks Davis            else if (mb == 0)           /* any */
11328195404bSBrooks Davis                printf("any");
11338195404bSBrooks Davis            else {          /* numeric IP followed by some kind of mask */
11348195404bSBrooks Davis                if (inet_ntop(AF_INET6,  a, trad, sizeof( trad ) ) == NULL)
11358195404bSBrooks Davis                    printf("Error ntop in print_ip6\n");
11368195404bSBrooks Davis                printf("%s",  trad );
11378195404bSBrooks Davis                if (mb < 0)     /* XXX not really legal... */
11388195404bSBrooks Davis                    printf(":%s",
11398195404bSBrooks Davis                        inet_ntop(AF_INET6, &a[1], trad, sizeof(trad)));
11408195404bSBrooks Davis                else if (mb < 128)
11418195404bSBrooks Davis                    printf("/%d", mb);
11428195404bSBrooks Davis            }
11438195404bSBrooks Davis            if (len > 2)
11448195404bSBrooks Davis                printf(",");
11458195404bSBrooks Davis        }
11468195404bSBrooks Davis }
11478195404bSBrooks Davis 
11488195404bSBrooks Davis static void
11498195404bSBrooks Davis fill_icmp6types(ipfw_insn_icmp6 *cmd, char *av)
11508195404bSBrooks Davis {
11518195404bSBrooks Davis        uint8_t type;
11528195404bSBrooks Davis 
11538195404bSBrooks Davis        cmd->d[0] = 0;
11548195404bSBrooks Davis        while (*av) {
11558195404bSBrooks Davis            if (*av == ',')
11568195404bSBrooks Davis                av++;
11578195404bSBrooks Davis            type = strtoul(av, &av, 0);
11588195404bSBrooks Davis            if (*av != ',' && *av != '\0')
11598195404bSBrooks Davis                errx(EX_DATAERR, "invalid ICMP6 type");
11608195404bSBrooks Davis 	   /*
11618195404bSBrooks Davis 	    * XXX: shouldn't this be 0xFF?  I can't see any reason why
11628195404bSBrooks Davis 	    * we shouldn't be able to filter all possiable values
11638195404bSBrooks Davis 	    * regardless of the ability of the rest of the kernel to do
11648195404bSBrooks Davis 	    * anything useful with them.
11658195404bSBrooks Davis 	    */
11668195404bSBrooks Davis            if (type > ICMP6_MAXTYPE)
11678195404bSBrooks Davis                errx(EX_DATAERR, "ICMP6 type out of range");
11688195404bSBrooks Davis            cmd->d[type / 32] |= ( 1 << (type % 32));
11698195404bSBrooks Davis        }
11708195404bSBrooks Davis        cmd->o.opcode = O_ICMP6TYPE;
11718195404bSBrooks Davis        cmd->o.len |= F_INSN_SIZE(ipfw_insn_icmp6);
11728195404bSBrooks Davis }
11738195404bSBrooks Davis 
11748195404bSBrooks Davis 
11758195404bSBrooks Davis static void
11768195404bSBrooks Davis print_icmp6types(ipfw_insn_u32 *cmd)
11778195404bSBrooks Davis {
11788195404bSBrooks Davis        int i, j;
11798195404bSBrooks Davis        char sep= ' ';
11808195404bSBrooks Davis 
118136c263ccSHajimu UMEMOTO        printf(" ip6 icmp6types");
11828195404bSBrooks Davis        for (i = 0; i < 7; i++)
11838195404bSBrooks Davis                for (j=0; j < 32; ++j) {
11848195404bSBrooks Davis                        if ( (cmd->d[i] & (1 << (j))) == 0)
11858195404bSBrooks Davis                                continue;
11868195404bSBrooks Davis                        printf("%c%d", sep, (i*32 + j));
11878195404bSBrooks Davis                        sep = ',';
11888195404bSBrooks Davis                }
11898195404bSBrooks Davis }
11908195404bSBrooks Davis 
11918195404bSBrooks Davis static void
11928195404bSBrooks Davis print_flow6id( ipfw_insn_u32 *cmd)
11938195404bSBrooks Davis {
11948195404bSBrooks Davis        uint16_t i, limit = cmd->o.arg1;
11958195404bSBrooks Davis        char sep = ',';
11968195404bSBrooks Davis 
11978195404bSBrooks Davis        printf(" flow-id ");
11988195404bSBrooks Davis        for( i=0; i < limit; ++i) {
11998195404bSBrooks Davis                if (i == limit - 1)
12008195404bSBrooks Davis                        sep = ' ';
12018195404bSBrooks Davis                printf("%d%c", cmd->d[i], sep);
12028195404bSBrooks Davis        }
12038195404bSBrooks Davis }
12048195404bSBrooks Davis 
12058195404bSBrooks Davis /* structure and define for the extension header in ipv6 */
12068195404bSBrooks Davis static struct _s_x ext6hdrcodes[] = {
12078195404bSBrooks Davis        { "frag",       EXT_FRAGMENT },
12088195404bSBrooks Davis        { "hopopt",     EXT_HOPOPTS },
12098195404bSBrooks Davis        { "route",      EXT_ROUTING },
12109066356bSBjoern A. Zeeb        { "dstopt",     EXT_DSTOPTS },
12118195404bSBrooks Davis        { "ah",         EXT_AH },
12128195404bSBrooks Davis        { "esp",        EXT_ESP },
12138195404bSBrooks Davis        { NULL,         0 }
12148195404bSBrooks Davis };
12158195404bSBrooks Davis 
12168195404bSBrooks Davis /* fills command for the extension header filtering */
12178195404bSBrooks Davis int
12188195404bSBrooks Davis fill_ext6hdr( ipfw_insn *cmd, char *av)
12198195404bSBrooks Davis {
12208195404bSBrooks Davis        int tok;
12218195404bSBrooks Davis        char *s = av;
12228195404bSBrooks Davis 
12238195404bSBrooks Davis        cmd->arg1 = 0;
12248195404bSBrooks Davis 
12258195404bSBrooks Davis        while(s) {
12268195404bSBrooks Davis            av = strsep( &s, ",") ;
12278195404bSBrooks Davis            tok = match_token(ext6hdrcodes, av);
12288195404bSBrooks Davis            switch (tok) {
12298195404bSBrooks Davis            case EXT_FRAGMENT:
12308195404bSBrooks Davis                cmd->arg1 |= EXT_FRAGMENT;
12318195404bSBrooks Davis                break;
12328195404bSBrooks Davis 
12338195404bSBrooks Davis            case EXT_HOPOPTS:
12348195404bSBrooks Davis                cmd->arg1 |= EXT_HOPOPTS;
12358195404bSBrooks Davis                break;
12368195404bSBrooks Davis 
12378195404bSBrooks Davis            case EXT_ROUTING:
12388195404bSBrooks Davis                cmd->arg1 |= EXT_ROUTING;
12398195404bSBrooks Davis                break;
12408195404bSBrooks Davis 
12419066356bSBjoern A. Zeeb            case EXT_DSTOPTS:
12429066356bSBjoern A. Zeeb                cmd->arg1 |= EXT_DSTOPTS;
12439066356bSBjoern A. Zeeb                break;
12449066356bSBjoern A. Zeeb 
12458195404bSBrooks Davis            case EXT_AH:
12468195404bSBrooks Davis                cmd->arg1 |= EXT_AH;
12478195404bSBrooks Davis                break;
12488195404bSBrooks Davis 
12498195404bSBrooks Davis            case EXT_ESP:
12508195404bSBrooks Davis                cmd->arg1 |= EXT_ESP;
12518195404bSBrooks Davis                break;
12528195404bSBrooks Davis 
12538195404bSBrooks Davis            default:
12548195404bSBrooks Davis                errx( EX_DATAERR, "invalid option for ipv6 exten header" );
12558195404bSBrooks Davis                break;
12568195404bSBrooks Davis            }
12578195404bSBrooks Davis        }
12588195404bSBrooks Davis        if (cmd->arg1 == 0 )
12598195404bSBrooks Davis            return 0;
12608195404bSBrooks Davis        cmd->opcode = O_EXT_HDR;
12618195404bSBrooks Davis        cmd->len |= F_INSN_SIZE( ipfw_insn );
12628195404bSBrooks Davis        return 1;
12638195404bSBrooks Davis }
12648195404bSBrooks Davis 
12658195404bSBrooks Davis void
12668195404bSBrooks Davis print_ext6hdr( ipfw_insn *cmd )
12678195404bSBrooks Davis {
12688195404bSBrooks Davis        char sep = ' ';
12698195404bSBrooks Davis 
12708195404bSBrooks Davis        printf(" extension header:");
12718195404bSBrooks Davis        if (cmd->arg1 & EXT_FRAGMENT ) {
12728195404bSBrooks Davis            printf("%cfragmentation", sep);
12738195404bSBrooks Davis            sep = ',';
12748195404bSBrooks Davis        }
12758195404bSBrooks Davis        if (cmd->arg1 & EXT_HOPOPTS ) {
12768195404bSBrooks Davis            printf("%chop options", sep);
12778195404bSBrooks Davis            sep = ',';
12788195404bSBrooks Davis        }
12798195404bSBrooks Davis        if (cmd->arg1 & EXT_ROUTING ) {
12808195404bSBrooks Davis            printf("%crouting options", sep);
12818195404bSBrooks Davis            sep = ',';
12828195404bSBrooks Davis        }
12839066356bSBjoern A. Zeeb        if (cmd->arg1 & EXT_DSTOPTS ) {
12849066356bSBjoern A. Zeeb            printf("%cdestination options", sep);
12859066356bSBjoern A. Zeeb            sep = ',';
12869066356bSBjoern A. Zeeb        }
12878195404bSBrooks Davis        if (cmd->arg1 & EXT_AH ) {
12888195404bSBrooks Davis            printf("%cauthentication header", sep);
12898195404bSBrooks Davis            sep = ',';
12908195404bSBrooks Davis        }
12918195404bSBrooks Davis        if (cmd->arg1 & EXT_ESP ) {
12928195404bSBrooks Davis            printf("%cencapsulated security payload", sep);
12938195404bSBrooks Davis        }
12948195404bSBrooks Davis }
12958195404bSBrooks Davis 
12968195404bSBrooks Davis /*
12979758b77fSLuigi Rizzo  * show_ipfw() prints the body of an ipfw rule.
12989758b77fSLuigi Rizzo  * Because the standard rule has at least proto src_ip dst_ip, we use
12999758b77fSLuigi Rizzo  * a helper function to produce these entries if not provided explicitly.
1300e706181bSLuigi Rizzo  * The first argument is the list of fields we have, the second is
1301e706181bSLuigi Rizzo  * the list of fields we want to be printed.
130299e5e645SLuigi Rizzo  *
1303e706181bSLuigi Rizzo  * Special cases if we have provided a MAC header:
1304e706181bSLuigi Rizzo  *   + if the rule does not contain IP addresses/ports, do not print them;
1305e706181bSLuigi Rizzo  *   + if the rule does not contain an IP proto, print "all" instead of "ip";
1306e706181bSLuigi Rizzo  *
1307e706181bSLuigi Rizzo  * Once we have 'have_options', IP header fields are printed as options.
13089758b77fSLuigi Rizzo  */
130999e5e645SLuigi Rizzo #define	HAVE_PROTO	0x0001
131099e5e645SLuigi Rizzo #define	HAVE_SRCIP	0x0002
131199e5e645SLuigi Rizzo #define	HAVE_DSTIP	0x0004
131299e5e645SLuigi Rizzo #define	HAVE_MAC	0x0008
131399e5e645SLuigi Rizzo #define	HAVE_MACTYPE	0x0010
131457cd6d26SMax Laier #define	HAVE_PROTO4	0x0040
13158195404bSBrooks Davis #define	HAVE_PROTO6	0x0080
1316e706181bSLuigi Rizzo #define	HAVE_OPTIONS	0x8000
13179758b77fSLuigi Rizzo 
131899e5e645SLuigi Rizzo #define	HAVE_IP		(HAVE_PROTO | HAVE_SRCIP | HAVE_DSTIP)
13199758b77fSLuigi Rizzo static void
1320e706181bSLuigi Rizzo show_prerequisites(int *flags, int want, int cmd)
13219758b77fSLuigi Rizzo {
1322ac6cec51SLuigi Rizzo 	if (comment_only)
1323ac6cec51SLuigi Rizzo 		return;
1324e706181bSLuigi Rizzo 	if ( (*flags & HAVE_IP) == HAVE_IP)
1325e706181bSLuigi Rizzo 		*flags |= HAVE_OPTIONS;
132699e5e645SLuigi Rizzo 
1327e706181bSLuigi Rizzo 	if ( (*flags & (HAVE_MAC|HAVE_MACTYPE|HAVE_OPTIONS)) == HAVE_MAC &&
1328e706181bSLuigi Rizzo 	     cmd != O_MAC_TYPE) {
1329e706181bSLuigi Rizzo 		/*
1330e706181bSLuigi Rizzo 		 * mac-type was optimized out by the compiler,
1331e706181bSLuigi Rizzo 		 * restore it
1332e706181bSLuigi Rizzo 		 */
1333e706181bSLuigi Rizzo 		printf(" any");
1334e706181bSLuigi Rizzo 		*flags |= HAVE_MACTYPE | HAVE_OPTIONS;
1335e706181bSLuigi Rizzo 		return;
1336e706181bSLuigi Rizzo 	}
1337e706181bSLuigi Rizzo 	if ( !(*flags & HAVE_OPTIONS)) {
13389758b77fSLuigi Rizzo 		if ( !(*flags & HAVE_PROTO) && (want & HAVE_PROTO))
133957cd6d26SMax Laier 			if ( (*flags & HAVE_PROTO4))
134057cd6d26SMax Laier 				printf(" ip4");
134157cd6d26SMax Laier 			else if ( (*flags & HAVE_PROTO6))
134257cd6d26SMax Laier 				printf(" ip6");
134357cd6d26SMax Laier 			else
1344e706181bSLuigi Rizzo 				printf(" ip");
134557cd6d26SMax Laier 
13469758b77fSLuigi Rizzo 		if ( !(*flags & HAVE_SRCIP) && (want & HAVE_SRCIP))
13479758b77fSLuigi Rizzo 			printf(" from any");
13489758b77fSLuigi Rizzo 		if ( !(*flags & HAVE_DSTIP) && (want & HAVE_DSTIP))
13499758b77fSLuigi Rizzo 			printf(" to any");
1350e706181bSLuigi Rizzo 	}
13519758b77fSLuigi Rizzo 	*flags |= want;
13529758b77fSLuigi Rizzo }
13539758b77fSLuigi Rizzo 
13549758b77fSLuigi Rizzo static void
135545f61351SMaxim Konovalov show_ipfw(struct ip_fw *rule, int pcwidth, int bcwidth)
13569758b77fSLuigi Rizzo {
13573d2209aeSGiorgos Keramidas 	static int twidth = 0;
13589758b77fSLuigi Rizzo 	int l;
13599758b77fSLuigi Rizzo 	ipfw_insn *cmd;
1360bbc39c83SLuigi Rizzo 	char *comment = NULL;	/* ptr to comment if we have one */
13619758b77fSLuigi Rizzo 	int proto = 0;		/* default */
13629758b77fSLuigi Rizzo 	int flags = 0;	/* prerequisites */
13639758b77fSLuigi Rizzo 	ipfw_insn_log *logptr = NULL; /* set if we find an O_LOG */
1364974dfe30SBrian Feldman 	ipfw_insn_altq *altqptr = NULL; /* set if we find an O_ALTQ */
13659758b77fSLuigi Rizzo 	int or_block = 0;	/* we are in an or block */
1366571f8c1bSLuigi Rizzo 	uint32_t set_disable;
13679758b77fSLuigi Rizzo 
1368330462a3SBernd Walter 	bcopy(&rule->next_rule, &set_disable, sizeof(set_disable));
136943405724SLuigi Rizzo 
137043405724SLuigi Rizzo 	if (set_disable & (1 << rule->set)) { /* disabled */
137143405724SLuigi Rizzo 		if (!show_sets)
137243405724SLuigi Rizzo 			return;
137343405724SLuigi Rizzo 		else
137443405724SLuigi Rizzo 			printf("# DISABLED ");
137543405724SLuigi Rizzo 	}
13769758b77fSLuigi Rizzo 	printf("%05u ", rule->rulenum);
13779758b77fSLuigi Rizzo 
137862ff38aeSLuigi Rizzo 	if (pcwidth>0 || bcwidth>0)
1379330462a3SBernd Walter 		printf("%*llu %*llu ", pcwidth, align_uint64(&rule->pcnt),
1380330462a3SBernd Walter 		    bcwidth, align_uint64(&rule->bcnt));
13819758b77fSLuigi Rizzo 
13821b43a426SLuigi Rizzo 	if (do_time == 2)
13831b43a426SLuigi Rizzo 		printf("%10u ", rule->timestamp);
13841b43a426SLuigi Rizzo 	else if (do_time == 1) {
13859758b77fSLuigi Rizzo 		char timestr[30];
13863d2209aeSGiorgos Keramidas 		time_t t = (time_t)0;
13873d2209aeSGiorgos Keramidas 
13883d2209aeSGiorgos Keramidas 		if (twidth == 0) {
13893d2209aeSGiorgos Keramidas 			strcpy(timestr, ctime(&t));
13903d2209aeSGiorgos Keramidas 			*strchr(timestr, '\n') = '\0';
13913d2209aeSGiorgos Keramidas 			twidth = strlen(timestr);
13923d2209aeSGiorgos Keramidas 		}
13933d2209aeSGiorgos Keramidas 		if (rule->timestamp) {
139462ff38aeSLuigi Rizzo #if _FreeBSD_version < 500000 /* XXX check */
139562ff38aeSLuigi Rizzo #define	_long_to_time(x)	(time_t)(x)
139662ff38aeSLuigi Rizzo #endif
13973d2209aeSGiorgos Keramidas 			t = _long_to_time(rule->timestamp);
13989758b77fSLuigi Rizzo 
13999758b77fSLuigi Rizzo 			strcpy(timestr, ctime(&t));
14009758b77fSLuigi Rizzo 			*strchr(timestr, '\n') = '\0';
14019758b77fSLuigi Rizzo 			printf("%s ", timestr);
14029758b77fSLuigi Rizzo 		} else {
14033d2209aeSGiorgos Keramidas 			printf("%*s", twidth, " ");
14049758b77fSLuigi Rizzo 		}
14059758b77fSLuigi Rizzo 	}
14069758b77fSLuigi Rizzo 
140743405724SLuigi Rizzo 	if (show_sets)
140843405724SLuigi Rizzo 		printf("set %d ", rule->set);
140943405724SLuigi Rizzo 
14109758b77fSLuigi Rizzo 	/*
141112b5dc6aSLuigi Rizzo 	 * print the optional "match probability"
141212b5dc6aSLuigi Rizzo 	 */
141312b5dc6aSLuigi Rizzo 	if (rule->cmd_len > 0) {
141412b5dc6aSLuigi Rizzo 		cmd = rule->cmd ;
141512b5dc6aSLuigi Rizzo 		if (cmd->opcode == O_PROB) {
141612b5dc6aSLuigi Rizzo 			ipfw_insn_u32 *p = (ipfw_insn_u32 *)cmd;
141712b5dc6aSLuigi Rizzo 			double d = 1.0 * p->d[0];
141812b5dc6aSLuigi Rizzo 
141912b5dc6aSLuigi Rizzo 			d = (d / 0x7fffffff);
142012b5dc6aSLuigi Rizzo 			printf("prob %f ", d);
142112b5dc6aSLuigi Rizzo 		}
142212b5dc6aSLuigi Rizzo 	}
142312b5dc6aSLuigi Rizzo 
142412b5dc6aSLuigi Rizzo 	/*
14259758b77fSLuigi Rizzo 	 * first print actions
14269758b77fSLuigi Rizzo 	 */
14279758b77fSLuigi Rizzo         for (l = rule->cmd_len - rule->act_ofs, cmd = ACTION_PTR(rule);
14289758b77fSLuigi Rizzo 			l > 0 ; l -= F_LEN(cmd), cmd += F_LEN(cmd)) {
14299758b77fSLuigi Rizzo 		switch(cmd->opcode) {
14309758b77fSLuigi Rizzo 		case O_CHECK_STATE:
14319758b77fSLuigi Rizzo 			printf("check-state");
1432e706181bSLuigi Rizzo 			flags = HAVE_IP; /* avoid printing anything else */
14339758b77fSLuigi Rizzo 			break;
14349758b77fSLuigi Rizzo 
14359758b77fSLuigi Rizzo 		case O_ACCEPT:
14369758b77fSLuigi Rizzo 			printf("allow");
14379758b77fSLuigi Rizzo 			break;
14389758b77fSLuigi Rizzo 
14399758b77fSLuigi Rizzo 		case O_COUNT:
14409758b77fSLuigi Rizzo 			printf("count");
14419758b77fSLuigi Rizzo 			break;
14429758b77fSLuigi Rizzo 
14439758b77fSLuigi Rizzo 		case O_DENY:
14449758b77fSLuigi Rizzo 			printf("deny");
14459758b77fSLuigi Rizzo 			break;
14469758b77fSLuigi Rizzo 
14475e43aef8SLuigi Rizzo 		case O_REJECT:
14485e43aef8SLuigi Rizzo 			if (cmd->arg1 == ICMP_REJECT_RST)
14495e43aef8SLuigi Rizzo 				printf("reset");
14505e43aef8SLuigi Rizzo 			else if (cmd->arg1 == ICMP_UNREACH_HOST)
14515e43aef8SLuigi Rizzo 				printf("reject");
14525e43aef8SLuigi Rizzo 			else
14535e43aef8SLuigi Rizzo 				print_reject_code(cmd->arg1);
14545e43aef8SLuigi Rizzo 			break;
14555e43aef8SLuigi Rizzo 
14569066356bSBjoern A. Zeeb 		case O_UNREACH6:
14579066356bSBjoern A. Zeeb 			if (cmd->arg1 == ICMP6_UNREACH_RST)
14589066356bSBjoern A. Zeeb 				printf("reset6");
14599066356bSBjoern A. Zeeb 			else
14609066356bSBjoern A. Zeeb 				print_unreach6_code(cmd->arg1);
14619066356bSBjoern A. Zeeb 			break;
14629066356bSBjoern A. Zeeb 
14639758b77fSLuigi Rizzo 		case O_SKIPTO:
14649758b77fSLuigi Rizzo 			printf("skipto %u", cmd->arg1);
14659758b77fSLuigi Rizzo 			break;
14669758b77fSLuigi Rizzo 
14679758b77fSLuigi Rizzo 		case O_PIPE:
14689758b77fSLuigi Rizzo 			printf("pipe %u", cmd->arg1);
14699758b77fSLuigi Rizzo 			break;
14709758b77fSLuigi Rizzo 
14719758b77fSLuigi Rizzo 		case O_QUEUE:
14729758b77fSLuigi Rizzo 			printf("queue %u", cmd->arg1);
14739758b77fSLuigi Rizzo 			break;
14749758b77fSLuigi Rizzo 
14759758b77fSLuigi Rizzo 		case O_DIVERT:
14769758b77fSLuigi Rizzo 			printf("divert %u", cmd->arg1);
14779758b77fSLuigi Rizzo 			break;
14789758b77fSLuigi Rizzo 
14799758b77fSLuigi Rizzo 		case O_TEE:
14809758b77fSLuigi Rizzo 			printf("tee %u", cmd->arg1);
14819758b77fSLuigi Rizzo 			break;
14829758b77fSLuigi Rizzo 
1483670742a1SGleb Smirnoff 		case O_NETGRAPH:
1484670742a1SGleb Smirnoff 			printf("netgraph %u", cmd->arg1);
1485670742a1SGleb Smirnoff 			break;
1486670742a1SGleb Smirnoff 
1487670742a1SGleb Smirnoff 		case O_NGTEE:
1488670742a1SGleb Smirnoff 			printf("ngtee %u", cmd->arg1);
1489670742a1SGleb Smirnoff 			break;
1490670742a1SGleb Smirnoff 
14919758b77fSLuigi Rizzo 		case O_FORWARD_IP:
14929758b77fSLuigi Rizzo 		    {
14939758b77fSLuigi Rizzo 			ipfw_insn_sa *s = (ipfw_insn_sa *)cmd;
14949758b77fSLuigi Rizzo 
14959758b77fSLuigi Rizzo 			printf("fwd %s", inet_ntoa(s->sa.sin_addr));
14969758b77fSLuigi Rizzo 			if (s->sa.sin_port)
14974f531a53SLuigi Rizzo 				printf(",%d", s->sa.sin_port);
14989758b77fSLuigi Rizzo 		    }
14999758b77fSLuigi Rizzo 			break;
15009758b77fSLuigi Rizzo 
15019758b77fSLuigi Rizzo 		case O_LOG: /* O_LOG is printed last */
15029758b77fSLuigi Rizzo 			logptr = (ipfw_insn_log *)cmd;
15039758b77fSLuigi Rizzo 			break;
15049758b77fSLuigi Rizzo 
1505974dfe30SBrian Feldman 		case O_ALTQ: /* O_ALTQ is printed after O_LOG */
1506974dfe30SBrian Feldman 			altqptr = (ipfw_insn_altq *)cmd;
1507974dfe30SBrian Feldman 			break;
1508974dfe30SBrian Feldman 
15099758b77fSLuigi Rizzo 		default:
15109758b77fSLuigi Rizzo 			printf("** unrecognized action %d len %d ",
15119758b77fSLuigi Rizzo 				cmd->opcode, cmd->len);
15129758b77fSLuigi Rizzo 		}
15139758b77fSLuigi Rizzo 	}
15149758b77fSLuigi Rizzo 	if (logptr) {
15159758b77fSLuigi Rizzo 		if (logptr->max_log > 0)
15169758b77fSLuigi Rizzo 			printf(" log logamount %d", logptr->max_log);
15179758b77fSLuigi Rizzo 		else
15189758b77fSLuigi Rizzo 			printf(" log");
15199758b77fSLuigi Rizzo 	}
1520974dfe30SBrian Feldman 	if (altqptr) {
1521974dfe30SBrian Feldman 		const char *qname;
1522974dfe30SBrian Feldman 
1523974dfe30SBrian Feldman 		qname = altq_qid_to_name(altqptr->qid);
1524974dfe30SBrian Feldman 		if (qname == NULL)
1525974dfe30SBrian Feldman 			printf(" altq ?<%u>", altqptr->qid);
1526974dfe30SBrian Feldman 		else
1527974dfe30SBrian Feldman 			printf(" altq %s", qname);
1528974dfe30SBrian Feldman 	}
1529e706181bSLuigi Rizzo 
15309758b77fSLuigi Rizzo 	/*
1531e706181bSLuigi Rizzo 	 * then print the body.
15329758b77fSLuigi Rizzo 	 */
153357cd6d26SMax Laier         for (l = rule->act_ofs, cmd = rule->cmd ;
153457cd6d26SMax Laier 			l > 0 ; l -= F_LEN(cmd) , cmd += F_LEN(cmd)) {
153557cd6d26SMax Laier 		if ((cmd->len & F_OR) || (cmd->len & F_NOT))
153657cd6d26SMax Laier 			continue;
153757cd6d26SMax Laier 		if (cmd->opcode == O_IP4) {
153857cd6d26SMax Laier 			flags |= HAVE_PROTO4;
153957cd6d26SMax Laier 			break;
154057cd6d26SMax Laier 		} else if (cmd->opcode == O_IP6) {
154157cd6d26SMax Laier 			flags |= HAVE_PROTO6;
154257cd6d26SMax Laier 			break;
154357cd6d26SMax Laier 		}
154457cd6d26SMax Laier 	}
1545e706181bSLuigi Rizzo 	if (rule->_pad & 1) {	/* empty rules before options */
154657cd6d26SMax Laier 		if (!do_compact) {
154757cd6d26SMax Laier 			show_prerequisites(&flags, HAVE_PROTO, 0);
154857cd6d26SMax Laier 			printf(" from any to any");
154957cd6d26SMax Laier 		}
1550e706181bSLuigi Rizzo 		flags |= HAVE_IP | HAVE_OPTIONS;
1551e706181bSLuigi Rizzo 	}
1552e706181bSLuigi Rizzo 
1553ac6cec51SLuigi Rizzo 	if (comment_only)
1554ac6cec51SLuigi Rizzo 		comment = "...";
1555ac6cec51SLuigi Rizzo 
15569758b77fSLuigi Rizzo         for (l = rule->act_ofs, cmd = rule->cmd ;
15579758b77fSLuigi Rizzo 			l > 0 ; l -= F_LEN(cmd) , cmd += F_LEN(cmd)) {
15585e43aef8SLuigi Rizzo 		/* useful alias */
15595e43aef8SLuigi Rizzo 		ipfw_insn_u32 *cmd32 = (ipfw_insn_u32 *)cmd;
15609758b77fSLuigi Rizzo 
1561ac6cec51SLuigi Rizzo 		if (comment_only) {
1562ac6cec51SLuigi Rizzo 			if (cmd->opcode != O_NOP)
1563ac6cec51SLuigi Rizzo 				continue;
1564ac6cec51SLuigi Rizzo 			printf(" // %s\n", (char *)(cmd + 1));
1565ac6cec51SLuigi Rizzo 			return;
1566ac6cec51SLuigi Rizzo 		}
1567ac6cec51SLuigi Rizzo 
1568e706181bSLuigi Rizzo 		show_prerequisites(&flags, 0, cmd->opcode);
1569e706181bSLuigi Rizzo 
15709758b77fSLuigi Rizzo 		switch(cmd->opcode) {
157112b5dc6aSLuigi Rizzo 		case O_PROB:
157212b5dc6aSLuigi Rizzo 			break;	/* done already */
157312b5dc6aSLuigi Rizzo 
15749758b77fSLuigi Rizzo 		case O_PROBE_STATE:
15759758b77fSLuigi Rizzo 			break; /* no need to print anything here */
15769758b77fSLuigi Rizzo 
15779758b77fSLuigi Rizzo 		case O_MACADDR2: {
15789758b77fSLuigi Rizzo 			ipfw_insn_mac *m = (ipfw_insn_mac *)cmd;
1579e706181bSLuigi Rizzo 
1580e706181bSLuigi Rizzo 			if ((cmd->len & F_OR) && !or_block)
1581e706181bSLuigi Rizzo 				printf(" {");
15829758b77fSLuigi Rizzo 			if (cmd->len & F_NOT)
15839758b77fSLuigi Rizzo 				printf(" not");
1584e706181bSLuigi Rizzo 			printf(" MAC");
1585e706181bSLuigi Rizzo 			flags |= HAVE_MAC;
15869758b77fSLuigi Rizzo 			print_mac(m->addr, m->mask);
15879758b77fSLuigi Rizzo 			print_mac(m->addr + 6, m->mask + 6);
15889758b77fSLuigi Rizzo 			}
15899758b77fSLuigi Rizzo 			break;
15909758b77fSLuigi Rizzo 
15919758b77fSLuigi Rizzo 		case O_MAC_TYPE:
1592e706181bSLuigi Rizzo 			if ((cmd->len & F_OR) && !or_block)
1593e706181bSLuigi Rizzo 				printf(" {");
1594e706181bSLuigi Rizzo 			print_newports((ipfw_insn_u16 *)cmd, IPPROTO_ETHERTYPE,
1595e706181bSLuigi Rizzo 				(flags & HAVE_OPTIONS) ? cmd->opcode : 0);
1596e706181bSLuigi Rizzo 			flags |= HAVE_MAC | HAVE_MACTYPE | HAVE_OPTIONS;
15979758b77fSLuigi Rizzo 			break;
15989758b77fSLuigi Rizzo 
15999758b77fSLuigi Rizzo 		case O_IP_SRC:
1600cd8b5ae0SRuslan Ermilov 		case O_IP_SRC_LOOKUP:
16019758b77fSLuigi Rizzo 		case O_IP_SRC_MASK:
16029758b77fSLuigi Rizzo 		case O_IP_SRC_ME:
16039758b77fSLuigi Rizzo 		case O_IP_SRC_SET:
1604e706181bSLuigi Rizzo 			show_prerequisites(&flags, HAVE_PROTO, 0);
16059758b77fSLuigi Rizzo 			if (!(flags & HAVE_SRCIP))
16069758b77fSLuigi Rizzo 				printf(" from");
16079758b77fSLuigi Rizzo 			if ((cmd->len & F_OR) && !or_block)
16089758b77fSLuigi Rizzo 				printf(" {");
1609e706181bSLuigi Rizzo 			print_ip((ipfw_insn_ip *)cmd,
1610e706181bSLuigi Rizzo 				(flags & HAVE_OPTIONS) ? " src-ip" : "");
16119758b77fSLuigi Rizzo 			flags |= HAVE_SRCIP;
16129758b77fSLuigi Rizzo 			break;
16139758b77fSLuigi Rizzo 
16149758b77fSLuigi Rizzo 		case O_IP_DST:
1615cd8b5ae0SRuslan Ermilov 		case O_IP_DST_LOOKUP:
16169758b77fSLuigi Rizzo 		case O_IP_DST_MASK:
16179758b77fSLuigi Rizzo 		case O_IP_DST_ME:
16189758b77fSLuigi Rizzo 		case O_IP_DST_SET:
1619e706181bSLuigi Rizzo 			show_prerequisites(&flags, HAVE_PROTO|HAVE_SRCIP, 0);
16209758b77fSLuigi Rizzo 			if (!(flags & HAVE_DSTIP))
16219758b77fSLuigi Rizzo 				printf(" to");
16229758b77fSLuigi Rizzo 			if ((cmd->len & F_OR) && !or_block)
16239758b77fSLuigi Rizzo 				printf(" {");
1624e706181bSLuigi Rizzo 			print_ip((ipfw_insn_ip *)cmd,
1625e706181bSLuigi Rizzo 				(flags & HAVE_OPTIONS) ? " dst-ip" : "");
16269758b77fSLuigi Rizzo 			flags |= HAVE_DSTIP;
16279758b77fSLuigi Rizzo 			break;
16289758b77fSLuigi Rizzo 
16298195404bSBrooks Davis 		case O_IP6_SRC:
16308195404bSBrooks Davis 		case O_IP6_SRC_MASK:
16318195404bSBrooks Davis 		case O_IP6_SRC_ME:
1632b730879fSMax Laier 			show_prerequisites(&flags, HAVE_PROTO, 0);
16338195404bSBrooks Davis 			if (!(flags & HAVE_SRCIP))
16348195404bSBrooks Davis 				printf(" from");
16358195404bSBrooks Davis 			if ((cmd->len & F_OR) && !or_block)
16368195404bSBrooks Davis 				printf(" {");
16378195404bSBrooks Davis 			print_ip6((ipfw_insn_ip6 *)cmd,
16388195404bSBrooks Davis 			    (flags & HAVE_OPTIONS) ? " src-ip6" : "");
16398195404bSBrooks Davis 			flags |= HAVE_SRCIP | HAVE_PROTO;
16408195404bSBrooks Davis 			break;
16418195404bSBrooks Davis 
16428195404bSBrooks Davis 		case O_IP6_DST:
16438195404bSBrooks Davis 		case O_IP6_DST_MASK:
16448195404bSBrooks Davis 		case O_IP6_DST_ME:
16458195404bSBrooks Davis 			show_prerequisites(&flags, HAVE_PROTO|HAVE_SRCIP, 0);
16468195404bSBrooks Davis 			if (!(flags & HAVE_DSTIP))
16478195404bSBrooks Davis 				printf(" to");
16488195404bSBrooks Davis 			if ((cmd->len & F_OR) && !or_block)
16498195404bSBrooks Davis 				printf(" {");
16508195404bSBrooks Davis 			print_ip6((ipfw_insn_ip6 *)cmd,
16518195404bSBrooks Davis 			    (flags & HAVE_OPTIONS) ? " dst-ip6" : "");
16528195404bSBrooks Davis 			flags |= HAVE_DSTIP;
16538195404bSBrooks Davis 			break;
16548195404bSBrooks Davis 
16558195404bSBrooks Davis 		case O_FLOW6ID:
16568195404bSBrooks Davis 		print_flow6id( (ipfw_insn_u32 *) cmd );
16578195404bSBrooks Davis 		flags |= HAVE_OPTIONS;
16588195404bSBrooks Davis 		break;
16598195404bSBrooks Davis 
16609758b77fSLuigi Rizzo 		case O_IP_DSTPORT:
1661e706181bSLuigi Rizzo 			show_prerequisites(&flags, HAVE_IP, 0);
16629758b77fSLuigi Rizzo 		case O_IP_SRCPORT:
1663e706181bSLuigi Rizzo 			show_prerequisites(&flags, HAVE_PROTO|HAVE_SRCIP, 0);
16648ed2d749SLuigi Rizzo 			if ((cmd->len & F_OR) && !or_block)
16658ed2d749SLuigi Rizzo 				printf(" {");
1666e706181bSLuigi Rizzo 			print_newports((ipfw_insn_u16 *)cmd, proto,
1667e706181bSLuigi Rizzo 				(flags & HAVE_OPTIONS) ? cmd->opcode : 0);
16689758b77fSLuigi Rizzo 			break;
16699758b77fSLuigi Rizzo 
16709758b77fSLuigi Rizzo 		case O_PROTO: {
16718195404bSBrooks Davis 			struct protoent *pe = NULL;
16729758b77fSLuigi Rizzo 
16739758b77fSLuigi Rizzo 			if ((cmd->len & F_OR) && !or_block)
16749758b77fSLuigi Rizzo 				printf(" {");
16759758b77fSLuigi Rizzo 			if (cmd->len & F_NOT)
16769758b77fSLuigi Rizzo 				printf(" not");
16779758b77fSLuigi Rizzo 			proto = cmd->arg1;
1678d360073bSBrooks Davis 			pe = getprotobynumber(cmd->arg1);
167957cd6d26SMax Laier 			if ((flags & (HAVE_PROTO4 | HAVE_PROTO6)) &&
168057cd6d26SMax Laier 			    !(flags & HAVE_PROTO))
168157cd6d26SMax Laier 				show_prerequisites(&flags,
168257cd6d26SMax Laier 				    HAVE_IP | HAVE_OPTIONS, 0);
1683e706181bSLuigi Rizzo 			if (flags & HAVE_OPTIONS)
1684e706181bSLuigi Rizzo 				printf(" proto");
16859758b77fSLuigi Rizzo 			if (pe)
16869758b77fSLuigi Rizzo 				printf(" %s", pe->p_name);
16879758b77fSLuigi Rizzo 			else
16889758b77fSLuigi Rizzo 				printf(" %u", cmd->arg1);
16899758b77fSLuigi Rizzo 			}
16909758b77fSLuigi Rizzo 			flags |= HAVE_PROTO;
16919758b77fSLuigi Rizzo 			break;
16929758b77fSLuigi Rizzo 
16939758b77fSLuigi Rizzo 		default: /*options ... */
169457cd6d26SMax Laier 			if (!(cmd->len & (F_OR|F_NOT)))
169557cd6d26SMax Laier 				if (((cmd->opcode == O_IP6) &&
169657cd6d26SMax Laier 				    (flags & HAVE_PROTO6)) ||
169757cd6d26SMax Laier 				    ((cmd->opcode == O_IP4) &&
169857cd6d26SMax Laier 				    (flags & HAVE_PROTO4)))
169957cd6d26SMax Laier 					break;
1700e706181bSLuigi Rizzo 			show_prerequisites(&flags, HAVE_IP | HAVE_OPTIONS, 0);
17019758b77fSLuigi Rizzo 			if ((cmd->len & F_OR) && !or_block)
17029758b77fSLuigi Rizzo 				printf(" {");
17039758b77fSLuigi Rizzo 			if (cmd->len & F_NOT && cmd->opcode != O_IN)
17049758b77fSLuigi Rizzo 				printf(" not");
17059758b77fSLuigi Rizzo 			switch(cmd->opcode) {
17069758b77fSLuigi Rizzo 			case O_FRAG:
17079758b77fSLuigi Rizzo 				printf(" frag");
17089758b77fSLuigi Rizzo 				break;
17099758b77fSLuigi Rizzo 
17109758b77fSLuigi Rizzo 			case O_IN:
17119758b77fSLuigi Rizzo 				printf(cmd->len & F_NOT ? " out" : " in");
17129758b77fSLuigi Rizzo 				break;
17139758b77fSLuigi Rizzo 
17146daf7ebdSBrian Feldman 			case O_DIVERTED:
17156daf7ebdSBrian Feldman 				switch (cmd->arg1) {
17166daf7ebdSBrian Feldman 				case 3:
17176daf7ebdSBrian Feldman 					printf(" diverted");
17186daf7ebdSBrian Feldman 					break;
17196daf7ebdSBrian Feldman 				case 1:
17206daf7ebdSBrian Feldman 					printf(" diverted-loopback");
17216daf7ebdSBrian Feldman 					break;
17226daf7ebdSBrian Feldman 				case 2:
17236daf7ebdSBrian Feldman 					printf(" diverted-output");
17246daf7ebdSBrian Feldman 					break;
17256daf7ebdSBrian Feldman 				default:
17266daf7ebdSBrian Feldman 					printf(" diverted-?<%u>", cmd->arg1);
17276daf7ebdSBrian Feldman 					break;
17286daf7ebdSBrian Feldman 				}
17296daf7ebdSBrian Feldman 				break;
17306daf7ebdSBrian Feldman 
17319758b77fSLuigi Rizzo 			case O_LAYER2:
17329758b77fSLuigi Rizzo 				printf(" layer2");
17339758b77fSLuigi Rizzo 				break;
17349758b77fSLuigi Rizzo 			case O_XMIT:
17359758b77fSLuigi Rizzo 			case O_RECV:
1736bd528823SGleb Smirnoff 			case O_VIA:
1737bd528823SGleb Smirnoff 			    {
173862ff38aeSLuigi Rizzo 				char const *s;
17399758b77fSLuigi Rizzo 				ipfw_insn_if *cmdif = (ipfw_insn_if *)cmd;
17409758b77fSLuigi Rizzo 
17419758b77fSLuigi Rizzo 				if (cmd->opcode == O_XMIT)
17429758b77fSLuigi Rizzo 					s = "xmit";
17439758b77fSLuigi Rizzo 				else if (cmd->opcode == O_RECV)
17449758b77fSLuigi Rizzo 					s = "recv";
174562ff38aeSLuigi Rizzo 				else /* if (cmd->opcode == O_VIA) */
17469758b77fSLuigi Rizzo 					s = "via";
17479758b77fSLuigi Rizzo 				if (cmdif->name[0] == '\0')
17485e43aef8SLuigi Rizzo 					printf(" %s %s", s,
17495e43aef8SLuigi Rizzo 					    inet_ntoa(cmdif->p.ip));
1750bd528823SGleb Smirnoff 				else
17519bf40edeSBrooks Davis 					printf(" %s %s", s, cmdif->name);
17529758b77fSLuigi Rizzo 
1753bd528823SGleb Smirnoff 				break;
1754bd528823SGleb Smirnoff 			    }
17559758b77fSLuigi Rizzo 			case O_IPID:
175644c884e1SLuigi Rizzo 				if (F_LEN(cmd) == 1)
17579758b77fSLuigi Rizzo 				    printf(" ipid %u", cmd->arg1 );
175844c884e1SLuigi Rizzo 				else
175944c884e1SLuigi Rizzo 				    print_newports((ipfw_insn_u16 *)cmd, 0,
176044c884e1SLuigi Rizzo 					O_IPID);
17619758b77fSLuigi Rizzo 				break;
17629758b77fSLuigi Rizzo 
17639758b77fSLuigi Rizzo 			case O_IPTTL:
176444c884e1SLuigi Rizzo 				if (F_LEN(cmd) == 1)
17659758b77fSLuigi Rizzo 				    printf(" ipttl %u", cmd->arg1 );
176644c884e1SLuigi Rizzo 				else
176744c884e1SLuigi Rizzo 				    print_newports((ipfw_insn_u16 *)cmd, 0,
176844c884e1SLuigi Rizzo 					O_IPTTL);
17699758b77fSLuigi Rizzo 				break;
17709758b77fSLuigi Rizzo 
17719758b77fSLuigi Rizzo 			case O_IPVER:
17729758b77fSLuigi Rizzo 				printf(" ipver %u", cmd->arg1 );
17739758b77fSLuigi Rizzo 				break;
17749758b77fSLuigi Rizzo 
17755e43aef8SLuigi Rizzo 			case O_IPPRECEDENCE:
17765e43aef8SLuigi Rizzo 				printf(" ipprecedence %u", (cmd->arg1) >> 5 );
17775e43aef8SLuigi Rizzo 				break;
17785e43aef8SLuigi Rizzo 
17799758b77fSLuigi Rizzo 			case O_IPLEN:
178044c884e1SLuigi Rizzo 				if (F_LEN(cmd) == 1)
17819758b77fSLuigi Rizzo 				    printf(" iplen %u", cmd->arg1 );
178244c884e1SLuigi Rizzo 				else
178344c884e1SLuigi Rizzo 				    print_newports((ipfw_insn_u16 *)cmd, 0,
178444c884e1SLuigi Rizzo 					O_IPLEN);
17859758b77fSLuigi Rizzo 				break;
17869758b77fSLuigi Rizzo 
178752bc23abSLuigi Rizzo 			case O_IPOPT:
17889758b77fSLuigi Rizzo 				print_flags("ipoptions", cmd, f_ipopts);
17899758b77fSLuigi Rizzo 				break;
17909758b77fSLuigi Rizzo 
17915e43aef8SLuigi Rizzo 			case O_IPTOS:
17925e43aef8SLuigi Rizzo 				print_flags("iptos", cmd, f_iptos);
17935e43aef8SLuigi Rizzo 				break;
17945e43aef8SLuigi Rizzo 
17955e43aef8SLuigi Rizzo 			case O_ICMPTYPE:
17965e43aef8SLuigi Rizzo 				print_icmptypes((ipfw_insn_u32 *)cmd);
17975e43aef8SLuigi Rizzo 				break;
17985e43aef8SLuigi Rizzo 
17999758b77fSLuigi Rizzo 			case O_ESTAB:
18009758b77fSLuigi Rizzo 				printf(" established");
18019758b77fSLuigi Rizzo 				break;
18029758b77fSLuigi Rizzo 
1803c99ee9e0SBrian Feldman 			case O_TCPDATALEN:
1804c99ee9e0SBrian Feldman 				if (F_LEN(cmd) == 1)
1805c99ee9e0SBrian Feldman 				    printf(" tcpdatalen %u", cmd->arg1 );
1806c99ee9e0SBrian Feldman 				else
1807c99ee9e0SBrian Feldman 				    print_newports((ipfw_insn_u16 *)cmd, 0,
1808c99ee9e0SBrian Feldman 					O_TCPDATALEN);
1809c99ee9e0SBrian Feldman 				break;
1810c99ee9e0SBrian Feldman 
18119758b77fSLuigi Rizzo 			case O_TCPFLAGS:
18129758b77fSLuigi Rizzo 				print_flags("tcpflags", cmd, f_tcpflags);
18139758b77fSLuigi Rizzo 				break;
18149758b77fSLuigi Rizzo 
18159758b77fSLuigi Rizzo 			case O_TCPOPTS:
18169758b77fSLuigi Rizzo 				print_flags("tcpoptions", cmd, f_tcpopts);
18179758b77fSLuigi Rizzo 				break;
18189758b77fSLuigi Rizzo 
18199758b77fSLuigi Rizzo 			case O_TCPWIN:
18209758b77fSLuigi Rizzo 				printf(" tcpwin %d", ntohs(cmd->arg1));
18219758b77fSLuigi Rizzo 				break;
18229758b77fSLuigi Rizzo 
18239758b77fSLuigi Rizzo 			case O_TCPACK:
18249758b77fSLuigi Rizzo 				printf(" tcpack %d", ntohl(cmd32->d[0]));
18259758b77fSLuigi Rizzo 				break;
18269758b77fSLuigi Rizzo 
18279758b77fSLuigi Rizzo 			case O_TCPSEQ:
18289758b77fSLuigi Rizzo 				printf(" tcpseq %d", ntohl(cmd32->d[0]));
18299758b77fSLuigi Rizzo 				break;
18309758b77fSLuigi Rizzo 
18319758b77fSLuigi Rizzo 			case O_UID:
18329758b77fSLuigi Rizzo 			    {
18339758b77fSLuigi Rizzo 				struct passwd *pwd = getpwuid(cmd32->d[0]);
18349758b77fSLuigi Rizzo 
18359758b77fSLuigi Rizzo 				if (pwd)
18369758b77fSLuigi Rizzo 					printf(" uid %s", pwd->pw_name);
18379758b77fSLuigi Rizzo 				else
18389758b77fSLuigi Rizzo 					printf(" uid %u", cmd32->d[0]);
18399758b77fSLuigi Rizzo 			    }
18409758b77fSLuigi Rizzo 				break;
18419758b77fSLuigi Rizzo 
18429758b77fSLuigi Rizzo 			case O_GID:
18439758b77fSLuigi Rizzo 			    {
18449758b77fSLuigi Rizzo 				struct group *grp = getgrgid(cmd32->d[0]);
18459758b77fSLuigi Rizzo 
18469758b77fSLuigi Rizzo 				if (grp)
18479758b77fSLuigi Rizzo 					printf(" gid %s", grp->gr_name);
18489758b77fSLuigi Rizzo 				else
18499758b77fSLuigi Rizzo 					printf(" gid %u", cmd32->d[0]);
18509758b77fSLuigi Rizzo 			    }
18519758b77fSLuigi Rizzo 				break;
18529758b77fSLuigi Rizzo 
185331c88a30SChristian S.J. Peron 			case O_JAIL:
185431c88a30SChristian S.J. Peron 				printf(" jail %d", cmd32->d[0]);
185531c88a30SChristian S.J. Peron 				break;
185631c88a30SChristian S.J. Peron 
1857010dabb0SCrist J. Clark 			case O_VERREVPATH:
1858010dabb0SCrist J. Clark 				printf(" verrevpath");
1859010dabb0SCrist J. Clark 				break;
1860010dabb0SCrist J. Clark 
186122b5770bSAndre Oppermann 			case O_VERSRCREACH:
186222b5770bSAndre Oppermann 				printf(" versrcreach");
186322b5770bSAndre Oppermann 				break;
186422b5770bSAndre Oppermann 
18655f9541ecSAndre Oppermann 			case O_ANTISPOOF:
18665f9541ecSAndre Oppermann 				printf(" antispoof");
18675f9541ecSAndre Oppermann 				break;
18685f9541ecSAndre Oppermann 
1869c3e5b9f1SLuigi Rizzo 			case O_IPSEC:
1870c3e5b9f1SLuigi Rizzo 				printf(" ipsec");
1871c3e5b9f1SLuigi Rizzo 				break;
1872c3e5b9f1SLuigi Rizzo 
187362ff38aeSLuigi Rizzo 			case O_NOP:
1874bbc39c83SLuigi Rizzo 				comment = (char *)(cmd + 1);
187562ff38aeSLuigi Rizzo 				break;
187662ff38aeSLuigi Rizzo 
18779758b77fSLuigi Rizzo 			case O_KEEP_STATE:
18789758b77fSLuigi Rizzo 				printf(" keep-state");
18799758b77fSLuigi Rizzo 				break;
18809758b77fSLuigi Rizzo 
18819758b77fSLuigi Rizzo 			case O_LIMIT:
18829758b77fSLuigi Rizzo 			    {
18839758b77fSLuigi Rizzo 				struct _s_x *p = limit_masks;
18849758b77fSLuigi Rizzo 				ipfw_insn_limit *c = (ipfw_insn_limit *)cmd;
1885571f8c1bSLuigi Rizzo 				uint8_t x = c->limit_mask;
188662ff38aeSLuigi Rizzo 				char const *comma = " ";
18879758b77fSLuigi Rizzo 
18889758b77fSLuigi Rizzo 				printf(" limit");
18899758b77fSLuigi Rizzo 				for (; p->x != 0 ; p++)
18900a7197a8SLuigi Rizzo 					if ((x & p->x) == p->x) {
18919758b77fSLuigi Rizzo 						x &= ~p->x;
18929758b77fSLuigi Rizzo 						printf("%s%s", comma, p->s);
18939758b77fSLuigi Rizzo 						comma = ",";
18949758b77fSLuigi Rizzo 					}
18959758b77fSLuigi Rizzo 				printf(" %d", c->conn_limit);
18969758b77fSLuigi Rizzo 			    }
18979758b77fSLuigi Rizzo 				break;
18989758b77fSLuigi Rizzo 
18998195404bSBrooks Davis 			case O_IP6:
190036c263ccSHajimu UMEMOTO 				printf(" ip6");
19018195404bSBrooks Davis 				break;
19028195404bSBrooks Davis 
190357cd6d26SMax Laier 			case O_IP4:
190436c263ccSHajimu UMEMOTO 				printf(" ip4");
190557cd6d26SMax Laier 				break;
190657cd6d26SMax Laier 
19078195404bSBrooks Davis 			case O_ICMP6TYPE:
19088195404bSBrooks Davis 				print_icmp6types((ipfw_insn_u32 *)cmd);
19098195404bSBrooks Davis 				break;
19108195404bSBrooks Davis 
19118195404bSBrooks Davis 			case O_EXT_HDR:
19128195404bSBrooks Davis 				print_ext6hdr( (ipfw_insn *) cmd );
19138195404bSBrooks Davis 				break;
19148195404bSBrooks Davis 
19159758b77fSLuigi Rizzo 			default:
19169758b77fSLuigi Rizzo 				printf(" [opcode %d len %d]",
19179758b77fSLuigi Rizzo 				    cmd->opcode, cmd->len);
19189758b77fSLuigi Rizzo 			}
19199758b77fSLuigi Rizzo 		}
19209758b77fSLuigi Rizzo 		if (cmd->len & F_OR) {
19219758b77fSLuigi Rizzo 			printf(" or");
19229758b77fSLuigi Rizzo 			or_block = 1;
19239758b77fSLuigi Rizzo 		} else if (or_block) {
19249758b77fSLuigi Rizzo 			printf(" }");
19259758b77fSLuigi Rizzo 			or_block = 0;
19269758b77fSLuigi Rizzo 		}
19279758b77fSLuigi Rizzo 	}
1928e706181bSLuigi Rizzo 	show_prerequisites(&flags, HAVE_IP, 0);
1929bbc39c83SLuigi Rizzo 	if (comment)
1930bbc39c83SLuigi Rizzo 		printf(" // %s", comment);
19319758b77fSLuigi Rizzo 	printf("\n");
19329758b77fSLuigi Rizzo }
19339758b77fSLuigi Rizzo 
19349758b77fSLuigi Rizzo static void
193545f61351SMaxim Konovalov show_dyn_ipfw(ipfw_dyn_rule *d, int pcwidth, int bcwidth)
19369758b77fSLuigi Rizzo {
19379758b77fSLuigi Rizzo 	struct protoent *pe;
19389758b77fSLuigi Rizzo 	struct in_addr a;
1939330462a3SBernd Walter 	uint16_t rulenum;
19409758b77fSLuigi Rizzo 
19419758b77fSLuigi Rizzo 	if (!do_expired) {
19429758b77fSLuigi Rizzo 		if (!d->expire && !(d->dyn_type == O_LIMIT_PARENT))
19439758b77fSLuigi Rizzo 			return;
19449758b77fSLuigi Rizzo 	}
1945330462a3SBernd Walter 	bcopy(&d->rule, &rulenum, sizeof(rulenum));
1946571f8c1bSLuigi Rizzo 	printf("%05d", rulenum);
194762ff38aeSLuigi Rizzo 	if (pcwidth>0 || bcwidth>0)
1948571f8c1bSLuigi Rizzo 	    printf(" %*llu %*llu (%ds)", pcwidth,
1949330462a3SBernd Walter 		align_uint64(&d->pcnt), bcwidth,
1950330462a3SBernd Walter 		align_uint64(&d->bcnt), d->expire);
19519758b77fSLuigi Rizzo 	switch (d->dyn_type) {
19529758b77fSLuigi Rizzo 	case O_LIMIT_PARENT:
19539758b77fSLuigi Rizzo 		printf(" PARENT %d", d->count);
19549758b77fSLuigi Rizzo 		break;
19559758b77fSLuigi Rizzo 	case O_LIMIT:
19569758b77fSLuigi Rizzo 		printf(" LIMIT");
19579758b77fSLuigi Rizzo 		break;
19589758b77fSLuigi Rizzo 	case O_KEEP_STATE: /* bidir, no mask */
19599758b77fSLuigi Rizzo 		printf(" STATE");
19609758b77fSLuigi Rizzo 		break;
19619758b77fSLuigi Rizzo 	}
19629758b77fSLuigi Rizzo 
19639758b77fSLuigi Rizzo 	if ((pe = getprotobynumber(d->id.proto)) != NULL)
19649758b77fSLuigi Rizzo 		printf(" %s", pe->p_name);
19659758b77fSLuigi Rizzo 	else
19669758b77fSLuigi Rizzo 		printf(" proto %u", d->id.proto);
19679758b77fSLuigi Rizzo 
19689758b77fSLuigi Rizzo 	a.s_addr = htonl(d->id.src_ip);
19699758b77fSLuigi Rizzo 	printf(" %s %d", inet_ntoa(a), d->id.src_port);
19709758b77fSLuigi Rizzo 
19719758b77fSLuigi Rizzo 	a.s_addr = htonl(d->id.dst_ip);
19729758b77fSLuigi Rizzo 	printf(" <-> %s %d", inet_ntoa(a), d->id.dst_port);
19739758b77fSLuigi Rizzo 	printf("\n");
19749758b77fSLuigi Rizzo }
19759758b77fSLuigi Rizzo 
197662ff38aeSLuigi Rizzo static int
19779758b77fSLuigi Rizzo sort_q(const void *pa, const void *pb)
19789758b77fSLuigi Rizzo {
19799758b77fSLuigi Rizzo 	int rev = (do_sort < 0);
19809758b77fSLuigi Rizzo 	int field = rev ? -do_sort : do_sort;
19819758b77fSLuigi Rizzo 	long long res = 0;
19829758b77fSLuigi Rizzo 	const struct dn_flow_queue *a = pa;
19839758b77fSLuigi Rizzo 	const struct dn_flow_queue *b = pb;
19849758b77fSLuigi Rizzo 
19859758b77fSLuigi Rizzo 	switch (field) {
19869758b77fSLuigi Rizzo 	case 1: /* pkts */
19879758b77fSLuigi Rizzo 		res = a->len - b->len;
19889758b77fSLuigi Rizzo 		break;
19899758b77fSLuigi Rizzo 	case 2: /* bytes */
19909758b77fSLuigi Rizzo 		res = a->len_bytes - b->len_bytes;
19919758b77fSLuigi Rizzo 		break;
19929758b77fSLuigi Rizzo 
19939758b77fSLuigi Rizzo 	case 3: /* tot pkts */
19949758b77fSLuigi Rizzo 		res = a->tot_pkts - b->tot_pkts;
19959758b77fSLuigi Rizzo 		break;
19969758b77fSLuigi Rizzo 
19979758b77fSLuigi Rizzo 	case 4: /* tot bytes */
19989758b77fSLuigi Rizzo 		res = a->tot_bytes - b->tot_bytes;
19999758b77fSLuigi Rizzo 		break;
20009758b77fSLuigi Rizzo 	}
20019758b77fSLuigi Rizzo 	if (res < 0)
20029758b77fSLuigi Rizzo 		res = -1;
20039758b77fSLuigi Rizzo 	if (res > 0)
20049758b77fSLuigi Rizzo 		res = 1;
20059758b77fSLuigi Rizzo 	return (int)(rev ? res : -res);
20069758b77fSLuigi Rizzo }
20079758b77fSLuigi Rizzo 
20089758b77fSLuigi Rizzo static void
20099758b77fSLuigi Rizzo list_queues(struct dn_flow_set *fs, struct dn_flow_queue *q)
20109758b77fSLuigi Rizzo {
20119758b77fSLuigi Rizzo 	int l;
20128195404bSBrooks Davis 	int index_printed, indexes = 0;
20138195404bSBrooks Davis 	char buff[255];
20148195404bSBrooks Davis 	struct protoent *pe;
20159758b77fSLuigi Rizzo 
20169758b77fSLuigi Rizzo 	if (fs->rq_elements == 0)
20179758b77fSLuigi Rizzo 		return;
20189758b77fSLuigi Rizzo 
20199758b77fSLuigi Rizzo 	if (do_sort != 0)
20209758b77fSLuigi Rizzo 		heapsort(q, fs->rq_elements, sizeof *q, sort_q);
20218195404bSBrooks Davis 
20228195404bSBrooks Davis 	/* Print IPv4 flows */
20238195404bSBrooks Davis 	index_printed = 0;
20249758b77fSLuigi Rizzo 	for (l = 0; l < fs->rq_elements; l++) {
20259758b77fSLuigi Rizzo 		struct in_addr ina;
20269758b77fSLuigi Rizzo 
20278195404bSBrooks Davis 		/* XXX: Should check for IPv4 flows */
20288195404bSBrooks Davis 		if (IS_IP6_FLOW_ID(&(q[l].id)))
20298195404bSBrooks Davis 			continue;
20308195404bSBrooks Davis 
20318195404bSBrooks Davis 		if (!index_printed) {
20328195404bSBrooks Davis 			index_printed = 1;
20338195404bSBrooks Davis 			if (indexes > 0)	/* currently a no-op */
20348195404bSBrooks Davis 				printf("\n");
20358195404bSBrooks Davis 			indexes++;
20368195404bSBrooks Davis 			printf("    "
20378195404bSBrooks Davis 			    "mask: 0x%02x 0x%08x/0x%04x -> 0x%08x/0x%04x\n",
20388195404bSBrooks Davis 			    fs->flow_mask.proto,
20398195404bSBrooks Davis 			    fs->flow_mask.src_ip, fs->flow_mask.src_port,
20408195404bSBrooks Davis 			    fs->flow_mask.dst_ip, fs->flow_mask.dst_port);
20418195404bSBrooks Davis 
20428195404bSBrooks Davis 			printf("BKT Prot ___Source IP/port____ "
20438195404bSBrooks Davis 			    "____Dest. IP/port____ "
20448195404bSBrooks Davis 			    "Tot_pkt/bytes Pkt/Byte Drp\n");
20458195404bSBrooks Davis 		}
20468195404bSBrooks Davis 
20479758b77fSLuigi Rizzo 		printf("%3d ", q[l].hash_slot);
20489758b77fSLuigi Rizzo 		pe = getprotobynumber(q[l].id.proto);
20499758b77fSLuigi Rizzo 		if (pe)
20509758b77fSLuigi Rizzo 			printf("%-4s ", pe->p_name);
20519758b77fSLuigi Rizzo 		else
20529758b77fSLuigi Rizzo 			printf("%4u ", q[l].id.proto);
20538195404bSBrooks Davis 		ina.s_addr = htonl(q[l].id.src_ip);
20549758b77fSLuigi Rizzo 		printf("%15s/%-5d ",
20559758b77fSLuigi Rizzo 		    inet_ntoa(ina), q[l].id.src_port);
20569758b77fSLuigi Rizzo 		ina.s_addr = htonl(q[l].id.dst_ip);
20579758b77fSLuigi Rizzo 		printf("%15s/%-5d ",
20589758b77fSLuigi Rizzo 		    inet_ntoa(ina), q[l].id.dst_port);
20599758b77fSLuigi Rizzo 		printf("%4qu %8qu %2u %4u %3u\n",
20609758b77fSLuigi Rizzo 		    q[l].tot_pkts, q[l].tot_bytes,
20619758b77fSLuigi Rizzo 		    q[l].len, q[l].len_bytes, q[l].drops);
20629758b77fSLuigi Rizzo 		if (verbose)
20639758b77fSLuigi Rizzo 			printf("   S %20qd  F %20qd\n",
20649758b77fSLuigi Rizzo 			    q[l].S, q[l].F);
20659758b77fSLuigi Rizzo 	}
20668195404bSBrooks Davis 
20678195404bSBrooks Davis 	/* Print IPv6 flows */
20688195404bSBrooks Davis 	index_printed = 0;
20698195404bSBrooks Davis 	for (l = 0; l < fs->rq_elements; l++) {
20708195404bSBrooks Davis 		if (!IS_IP6_FLOW_ID(&(q[l].id)))
20718195404bSBrooks Davis 			continue;
20728195404bSBrooks Davis 
20738195404bSBrooks Davis 		if (!index_printed) {
20748195404bSBrooks Davis 			index_printed = 1;
20758195404bSBrooks Davis 			if (indexes > 0)
20768195404bSBrooks Davis 				printf("\n");
20778195404bSBrooks Davis 			indexes++;
20788195404bSBrooks Davis 			printf("\n        mask: proto: 0x%02x, flow_id: 0x%08x,  ",
20798195404bSBrooks Davis 			    fs->flow_mask.proto, fs->flow_mask.flow_id6);
20808195404bSBrooks Davis 			inet_ntop(AF_INET6, &(fs->flow_mask.src_ip6),
20818195404bSBrooks Davis 			    buff, sizeof(buff));
20828195404bSBrooks Davis 			printf("%s/0x%04x -> ", buff, fs->flow_mask.src_port);
20838195404bSBrooks Davis 			inet_ntop( AF_INET6, &(fs->flow_mask.dst_ip6),
20848195404bSBrooks Davis 			    buff, sizeof(buff) );
20858195404bSBrooks Davis 			printf("%s/0x%04x\n", buff, fs->flow_mask.dst_port);
20868195404bSBrooks Davis 
20878195404bSBrooks Davis 			printf("BKT ___Prot___ _flow-id_ "
20888195404bSBrooks Davis 			    "______________Source IPv6/port_______________ "
20898195404bSBrooks Davis 			    "_______________Dest. IPv6/port_______________ "
20908195404bSBrooks Davis 			    "Tot_pkt/bytes Pkt/Byte Drp\n");
20918195404bSBrooks Davis 		}
20928195404bSBrooks Davis 		printf("%3d ", q[l].hash_slot);
20938195404bSBrooks Davis 		pe = getprotobynumber(q[l].id.proto);
20948195404bSBrooks Davis 		if (pe != NULL)
20958195404bSBrooks Davis 			printf("%9s ", pe->p_name);
20968195404bSBrooks Davis 		else
20978195404bSBrooks Davis 			printf("%9u ", q[l].id.proto);
20988195404bSBrooks Davis 		printf("%7d  %39s/%-5d ", q[l].id.flow_id6,
20998195404bSBrooks Davis 		    inet_ntop(AF_INET6, &(q[l].id.src_ip6), buff, sizeof(buff)),
21008195404bSBrooks Davis 		    q[l].id.src_port);
21018195404bSBrooks Davis 		printf(" %39s/%-5d ",
21028195404bSBrooks Davis 		    inet_ntop(AF_INET6, &(q[l].id.dst_ip6), buff, sizeof(buff)),
21038195404bSBrooks Davis 		    q[l].id.dst_port);
21048195404bSBrooks Davis 		printf(" %4qu %8qu %2u %4u %3u\n",
21058195404bSBrooks Davis 		    q[l].tot_pkts, q[l].tot_bytes,
21068195404bSBrooks Davis 		    q[l].len, q[l].len_bytes, q[l].drops);
21078195404bSBrooks Davis 		if (verbose)
21088195404bSBrooks Davis 			printf("   S %20qd  F %20qd\n", q[l].S, q[l].F);
21098195404bSBrooks Davis 	}
21109758b77fSLuigi Rizzo }
21119758b77fSLuigi Rizzo 
21129758b77fSLuigi Rizzo static void
21139758b77fSLuigi Rizzo print_flowset_parms(struct dn_flow_set *fs, char *prefix)
21149758b77fSLuigi Rizzo {
21159758b77fSLuigi Rizzo 	int l;
21169758b77fSLuigi Rizzo 	char qs[30];
21179758b77fSLuigi Rizzo 	char plr[30];
21189758b77fSLuigi Rizzo 	char red[90];	/* Display RED parameters */
21199758b77fSLuigi Rizzo 
21209758b77fSLuigi Rizzo 	l = fs->qsize;
21219758b77fSLuigi Rizzo 	if (fs->flags_fs & DN_QSIZE_IS_BYTES) {
21229758b77fSLuigi Rizzo 		if (l >= 8192)
21239758b77fSLuigi Rizzo 			sprintf(qs, "%d KB", l / 1024);
21249758b77fSLuigi Rizzo 		else
21259758b77fSLuigi Rizzo 			sprintf(qs, "%d B", l);
21269758b77fSLuigi Rizzo 	} else
21279758b77fSLuigi Rizzo 		sprintf(qs, "%3d sl.", l);
21289758b77fSLuigi Rizzo 	if (fs->plr)
21299758b77fSLuigi Rizzo 		sprintf(plr, "plr %f", 1.0 * fs->plr / (double)(0x7fffffff));
21309758b77fSLuigi Rizzo 	else
21319758b77fSLuigi Rizzo 		plr[0] = '\0';
21329758b77fSLuigi Rizzo 	if (fs->flags_fs & DN_IS_RED)	/* RED parameters */
21339758b77fSLuigi Rizzo 		sprintf(red,
21349758b77fSLuigi Rizzo 		    "\n\t  %cRED w_q %f min_th %d max_th %d max_p %f",
21359758b77fSLuigi Rizzo 		    (fs->flags_fs & DN_IS_GENTLE_RED) ? 'G' : ' ',
21369758b77fSLuigi Rizzo 		    1.0 * fs->w_q / (double)(1 << SCALE_RED),
21379758b77fSLuigi Rizzo 		    SCALE_VAL(fs->min_th),
21389758b77fSLuigi Rizzo 		    SCALE_VAL(fs->max_th),
21399758b77fSLuigi Rizzo 		    1.0 * fs->max_p / (double)(1 << SCALE_RED));
21409758b77fSLuigi Rizzo 	else
21419758b77fSLuigi Rizzo 		sprintf(red, "droptail");
21429758b77fSLuigi Rizzo 
21439758b77fSLuigi Rizzo 	printf("%s %s%s %d queues (%d buckets) %s\n",
21449758b77fSLuigi Rizzo 	    prefix, qs, plr, fs->rq_elements, fs->rq_size, red);
21459758b77fSLuigi Rizzo }
21469758b77fSLuigi Rizzo 
21479758b77fSLuigi Rizzo static void
214862ff38aeSLuigi Rizzo list_pipes(void *data, uint nbytes, int ac, char *av[])
21499758b77fSLuigi Rizzo {
215062ff38aeSLuigi Rizzo 	int rulenum;
21519758b77fSLuigi Rizzo 	void *next = data;
21529758b77fSLuigi Rizzo 	struct dn_pipe *p = (struct dn_pipe *) data;
21539758b77fSLuigi Rizzo 	struct dn_flow_set *fs;
21549758b77fSLuigi Rizzo 	struct dn_flow_queue *q;
21559758b77fSLuigi Rizzo 	int l;
21569758b77fSLuigi Rizzo 
21579758b77fSLuigi Rizzo 	if (ac > 0)
21589758b77fSLuigi Rizzo 		rulenum = strtoul(*av++, NULL, 10);
21599758b77fSLuigi Rizzo 	else
21609758b77fSLuigi Rizzo 		rulenum = 0;
21619758b77fSLuigi Rizzo 	for (; nbytes >= sizeof *p; p = (struct dn_pipe *)next) {
21629758b77fSLuigi Rizzo 		double b = p->bandwidth;
21639758b77fSLuigi Rizzo 		char buf[30];
21649758b77fSLuigi Rizzo 		char prefix[80];
21659758b77fSLuigi Rizzo 
2166e36ffd3bSGleb Smirnoff 		if (p->next.sle_next != (struct dn_pipe *)DN_IS_PIPE)
21679758b77fSLuigi Rizzo 			break;	/* done with pipes, now queues */
21689758b77fSLuigi Rizzo 
21699758b77fSLuigi Rizzo 		/*
21709758b77fSLuigi Rizzo 		 * compute length, as pipe have variable size
21719758b77fSLuigi Rizzo 		 */
21729758b77fSLuigi Rizzo 		l = sizeof(*p) + p->fs.rq_elements * sizeof(*q);
217362ff38aeSLuigi Rizzo 		next = (char *)p + l;
21749758b77fSLuigi Rizzo 		nbytes -= l;
21759758b77fSLuigi Rizzo 
2176c3d6fe74SPawel Jakub Dawidek 		if ((rulenum != 0 && rulenum != p->pipe_nr) || do_pipe == 2)
21779758b77fSLuigi Rizzo 			continue;
21789758b77fSLuigi Rizzo 
21799758b77fSLuigi Rizzo 		/*
21809758b77fSLuigi Rizzo 		 * Print rate (or clocking interface)
21819758b77fSLuigi Rizzo 		 */
21829758b77fSLuigi Rizzo 		if (p->if_name[0] != '\0')
21839758b77fSLuigi Rizzo 			sprintf(buf, "%s", p->if_name);
21849758b77fSLuigi Rizzo 		else if (b == 0)
21859758b77fSLuigi Rizzo 			sprintf(buf, "unlimited");
21869758b77fSLuigi Rizzo 		else if (b >= 1000000)
21879758b77fSLuigi Rizzo 			sprintf(buf, "%7.3f Mbit/s", b/1000000);
21889758b77fSLuigi Rizzo 		else if (b >= 1000)
21899758b77fSLuigi Rizzo 			sprintf(buf, "%7.3f Kbit/s", b/1000);
21909758b77fSLuigi Rizzo 		else
21919758b77fSLuigi Rizzo 			sprintf(buf, "%7.3f bit/s ", b);
21929758b77fSLuigi Rizzo 
21939758b77fSLuigi Rizzo 		sprintf(prefix, "%05d: %s %4d ms ",
21949758b77fSLuigi Rizzo 		    p->pipe_nr, buf, p->delay);
21959758b77fSLuigi Rizzo 		print_flowset_parms(&(p->fs), prefix);
21969758b77fSLuigi Rizzo 		if (verbose)
21979758b77fSLuigi Rizzo 			printf("   V %20qd\n", p->V >> MY_M);
21989758b77fSLuigi Rizzo 
21999758b77fSLuigi Rizzo 		q = (struct dn_flow_queue *)(p+1);
22009758b77fSLuigi Rizzo 		list_queues(&(p->fs), q);
22019758b77fSLuigi Rizzo 	}
22029758b77fSLuigi Rizzo 	for (fs = next; nbytes >= sizeof *fs; fs = next) {
22039758b77fSLuigi Rizzo 		char prefix[80];
22049758b77fSLuigi Rizzo 
2205e36ffd3bSGleb Smirnoff 		if (fs->next.sle_next != (struct dn_flow_set *)DN_IS_QUEUE)
22069758b77fSLuigi Rizzo 			break;
22079758b77fSLuigi Rizzo 		l = sizeof(*fs) + fs->rq_elements * sizeof(*q);
220862ff38aeSLuigi Rizzo 		next = (char *)fs + l;
22099758b77fSLuigi Rizzo 		nbytes -= l;
2210c3d6fe74SPawel Jakub Dawidek 
2211c3d6fe74SPawel Jakub Dawidek 		if (rulenum != 0 && ((rulenum != fs->fs_nr && do_pipe == 2) ||
2212c3d6fe74SPawel Jakub Dawidek 		    (rulenum != fs->parent_nr && do_pipe == 1))) {
2213c3d6fe74SPawel Jakub Dawidek 			continue;
2214c3d6fe74SPawel Jakub Dawidek 		}
2215c3d6fe74SPawel Jakub Dawidek 
22169758b77fSLuigi Rizzo 		q = (struct dn_flow_queue *)(fs+1);
22179758b77fSLuigi Rizzo 		sprintf(prefix, "q%05d: weight %d pipe %d ",
22189758b77fSLuigi Rizzo 		    fs->fs_nr, fs->weight, fs->parent_nr);
22199758b77fSLuigi Rizzo 		print_flowset_parms(fs, prefix);
22209758b77fSLuigi Rizzo 		list_queues(fs, q);
22219758b77fSLuigi Rizzo 	}
22229758b77fSLuigi Rizzo }
22239758b77fSLuigi Rizzo 
222499e5e645SLuigi Rizzo /*
222599e5e645SLuigi Rizzo  * This one handles all set-related commands
222699e5e645SLuigi Rizzo  * 	ipfw set { show | enable | disable }
222799e5e645SLuigi Rizzo  * 	ipfw set swap X Y
222899e5e645SLuigi Rizzo  * 	ipfw set move X to Y
222999e5e645SLuigi Rizzo  * 	ipfw set move rule X to Y
223099e5e645SLuigi Rizzo  */
223199e5e645SLuigi Rizzo static void
223299e5e645SLuigi Rizzo sets_handler(int ac, char *av[])
223399e5e645SLuigi Rizzo {
2234571f8c1bSLuigi Rizzo 	uint32_t set_disable, masks[2];
223599e5e645SLuigi Rizzo 	int i, nbytes;
2236571f8c1bSLuigi Rizzo 	uint16_t rulenum;
2237571f8c1bSLuigi Rizzo 	uint8_t cmd, new_set;
223899e5e645SLuigi Rizzo 
223999e5e645SLuigi Rizzo 	ac--;
224099e5e645SLuigi Rizzo 	av++;
224199e5e645SLuigi Rizzo 
224299e5e645SLuigi Rizzo 	if (!ac)
224399e5e645SLuigi Rizzo 		errx(EX_USAGE, "set needs command");
224401750186SBrooks Davis 	if (_substrcmp(*av, "show") == 0) {
224599e5e645SLuigi Rizzo 		void *data;
224662ff38aeSLuigi Rizzo 		char const *msg;
224799e5e645SLuigi Rizzo 
224899e5e645SLuigi Rizzo 		nbytes = sizeof(struct ip_fw);
2249571f8c1bSLuigi Rizzo 		if ((data = calloc(1, nbytes)) == NULL)
2250571f8c1bSLuigi Rizzo 			err(EX_OSERR, "calloc");
2251884be75cSThomas Moestl 		if (do_cmd(IP_FW_GET, data, (uintptr_t)&nbytes) < 0)
225299e5e645SLuigi Rizzo 			err(EX_OSERR, "getsockopt(IP_FW_GET)");
2253330462a3SBernd Walter 		bcopy(&((struct ip_fw *)data)->next_rule,
2254330462a3SBernd Walter 			&set_disable, sizeof(set_disable));
2255330462a3SBernd Walter 
22563004afcaSLuigi Rizzo 		for (i = 0, msg = "disable" ; i < RESVD_SET; i++)
225799e5e645SLuigi Rizzo 			if ((set_disable & (1<<i))) {
225899e5e645SLuigi Rizzo 				printf("%s %d", msg, i);
225999e5e645SLuigi Rizzo 				msg = "";
226099e5e645SLuigi Rizzo 			}
226199e5e645SLuigi Rizzo 		msg = (set_disable) ? " enable" : "enable";
22623004afcaSLuigi Rizzo 		for (i = 0; i < RESVD_SET; i++)
226399e5e645SLuigi Rizzo 			if (!(set_disable & (1<<i))) {
226499e5e645SLuigi Rizzo 				printf("%s %d", msg, i);
226599e5e645SLuigi Rizzo 				msg = "";
226699e5e645SLuigi Rizzo 			}
226799e5e645SLuigi Rizzo 		printf("\n");
226801750186SBrooks Davis 	} else if (_substrcmp(*av, "swap") == 0) {
226999e5e645SLuigi Rizzo 		ac--; av++;
227099e5e645SLuigi Rizzo 		if (ac != 2)
227199e5e645SLuigi Rizzo 			errx(EX_USAGE, "set swap needs 2 set numbers\n");
227299e5e645SLuigi Rizzo 		rulenum = atoi(av[0]);
227399e5e645SLuigi Rizzo 		new_set = atoi(av[1]);
22743004afcaSLuigi Rizzo 		if (!isdigit(*(av[0])) || rulenum > RESVD_SET)
227599e5e645SLuigi Rizzo 			errx(EX_DATAERR, "invalid set number %s\n", av[0]);
22763004afcaSLuigi Rizzo 		if (!isdigit(*(av[1])) || new_set > RESVD_SET)
227799e5e645SLuigi Rizzo 			errx(EX_DATAERR, "invalid set number %s\n", av[1]);
227899e5e645SLuigi Rizzo 		masks[0] = (4 << 24) | (new_set << 16) | (rulenum);
2279571f8c1bSLuigi Rizzo 		i = do_cmd(IP_FW_DEL, masks, sizeof(uint32_t));
228001750186SBrooks Davis 	} else if (_substrcmp(*av, "move") == 0) {
228199e5e645SLuigi Rizzo 		ac--; av++;
228201750186SBrooks Davis 		if (ac && _substrcmp(*av, "rule") == 0) {
228399e5e645SLuigi Rizzo 			cmd = 2;
228499e5e645SLuigi Rizzo 			ac--; av++;
228599e5e645SLuigi Rizzo 		} else
228699e5e645SLuigi Rizzo 			cmd = 3;
228701750186SBrooks Davis 		if (ac != 3 || _substrcmp(av[1], "to") != 0)
228899e5e645SLuigi Rizzo 			errx(EX_USAGE, "syntax: set move [rule] X to Y\n");
228999e5e645SLuigi Rizzo 		rulenum = atoi(av[0]);
229099e5e645SLuigi Rizzo 		new_set = atoi(av[2]);
22913004afcaSLuigi Rizzo 		if (!isdigit(*(av[0])) || (cmd == 3 && rulenum > RESVD_SET) ||
229299e5e645SLuigi Rizzo 			(cmd == 2 && rulenum == 65535) )
229399e5e645SLuigi Rizzo 			errx(EX_DATAERR, "invalid source number %s\n", av[0]);
22943004afcaSLuigi Rizzo 		if (!isdigit(*(av[2])) || new_set > RESVD_SET)
229599e5e645SLuigi Rizzo 			errx(EX_DATAERR, "invalid dest. set %s\n", av[1]);
229699e5e645SLuigi Rizzo 		masks[0] = (cmd << 24) | (new_set << 16) | (rulenum);
2297571f8c1bSLuigi Rizzo 		i = do_cmd(IP_FW_DEL, masks, sizeof(uint32_t));
229801750186SBrooks Davis 	} else if (_substrcmp(*av, "disable") == 0 ||
229901750186SBrooks Davis 		   _substrcmp(*av, "enable") == 0 ) {
230001750186SBrooks Davis 		int which = _substrcmp(*av, "enable") == 0 ? 1 : 0;
230199e5e645SLuigi Rizzo 
230299e5e645SLuigi Rizzo 		ac--; av++;
230399e5e645SLuigi Rizzo 		masks[0] = masks[1] = 0;
230499e5e645SLuigi Rizzo 
230599e5e645SLuigi Rizzo 		while (ac) {
230699e5e645SLuigi Rizzo 			if (isdigit(**av)) {
230799e5e645SLuigi Rizzo 				i = atoi(*av);
23083004afcaSLuigi Rizzo 				if (i < 0 || i > RESVD_SET)
230999e5e645SLuigi Rizzo 					errx(EX_DATAERR,
231099e5e645SLuigi Rizzo 					    "invalid set number %d\n", i);
231199e5e645SLuigi Rizzo 				masks[which] |= (1<<i);
231201750186SBrooks Davis 			} else if (_substrcmp(*av, "disable") == 0)
231399e5e645SLuigi Rizzo 				which = 0;
231401750186SBrooks Davis 			else if (_substrcmp(*av, "enable") == 0)
231599e5e645SLuigi Rizzo 				which = 1;
231699e5e645SLuigi Rizzo 			else
231799e5e645SLuigi Rizzo 				errx(EX_DATAERR,
231899e5e645SLuigi Rizzo 					"invalid set command %s\n", *av);
231999e5e645SLuigi Rizzo 			av++; ac--;
232099e5e645SLuigi Rizzo 		}
232199e5e645SLuigi Rizzo 		if ( (masks[0] & masks[1]) != 0 )
232299e5e645SLuigi Rizzo 			errx(EX_DATAERR,
232399e5e645SLuigi Rizzo 			    "cannot enable and disable the same set\n");
232499e5e645SLuigi Rizzo 
2325571f8c1bSLuigi Rizzo 		i = do_cmd(IP_FW_DEL, masks, sizeof(masks));
232699e5e645SLuigi Rizzo 		if (i)
232799e5e645SLuigi Rizzo 			warn("set enable/disable: setsockopt(IP_FW_DEL)");
232899e5e645SLuigi Rizzo 	} else
232999e5e645SLuigi Rizzo 		errx(EX_USAGE, "invalid set command %s\n", *av);
233099e5e645SLuigi Rizzo }
233199e5e645SLuigi Rizzo 
23329758b77fSLuigi Rizzo static void
23336690be9eSMatthew Dillon sysctl_handler(int ac, char *av[], int which)
23346690be9eSMatthew Dillon {
23356690be9eSMatthew Dillon 	ac--;
23366690be9eSMatthew Dillon 	av++;
23376690be9eSMatthew Dillon 
23381c56ad9bSMaxim Konovalov 	if (ac == 0) {
23396690be9eSMatthew Dillon 		warnx("missing keyword to enable/disable\n");
234001750186SBrooks Davis 	} else if (_substrcmp(*av, "firewall") == 0) {
234129c1402aSLuigi Rizzo 		sysctlbyname("net.inet.ip.fw.enable", NULL, 0,
234229c1402aSLuigi Rizzo 		    &which, sizeof(which));
234301750186SBrooks Davis 	} else if (_substrcmp(*av, "one_pass") == 0) {
234429c1402aSLuigi Rizzo 		sysctlbyname("net.inet.ip.fw.one_pass", NULL, 0,
234529c1402aSLuigi Rizzo 		    &which, sizeof(which));
234601750186SBrooks Davis 	} else if (_substrcmp(*av, "debug") == 0) {
234729c1402aSLuigi Rizzo 		sysctlbyname("net.inet.ip.fw.debug", NULL, 0,
234829c1402aSLuigi Rizzo 		    &which, sizeof(which));
234901750186SBrooks Davis 	} else if (_substrcmp(*av, "verbose") == 0) {
235029c1402aSLuigi Rizzo 		sysctlbyname("net.inet.ip.fw.verbose", NULL, 0,
235129c1402aSLuigi Rizzo 		    &which, sizeof(which));
235201750186SBrooks Davis 	} else if (_substrcmp(*av, "dyn_keepalive") == 0) {
235329c1402aSLuigi Rizzo 		sysctlbyname("net.inet.ip.fw.dyn_keepalive", NULL, 0,
235429c1402aSLuigi Rizzo 		    &which, sizeof(which));
235501750186SBrooks Davis 	} else if (_substrcmp(*av, "altq") == 0) {
2356974dfe30SBrian Feldman 		altq_set_enabled(which);
23576690be9eSMatthew Dillon 	} else {
23586690be9eSMatthew Dillon 		warnx("unrecognize enable/disable keyword: %s\n", *av);
23596690be9eSMatthew Dillon 	}
23606690be9eSMatthew Dillon }
23616690be9eSMatthew Dillon 
23626690be9eSMatthew Dillon static void
236362ff38aeSLuigi Rizzo list(int ac, char *av[], int show_counters)
23649758b77fSLuigi Rizzo {
23659758b77fSLuigi Rizzo 	struct ip_fw *r;
23669758b77fSLuigi Rizzo 	ipfw_dyn_rule *dynrules, *d;
23679758b77fSLuigi Rizzo 
236862ff38aeSLuigi Rizzo #define NEXT(r)	((struct ip_fw *)((char *)r + RULESIZE(r)))
236962ff38aeSLuigi Rizzo 	char *lim;
237062ff38aeSLuigi Rizzo 	void *data = NULL;
237145f61351SMaxim Konovalov 	int bcwidth, n, nbytes, nstat, ndyn, pcwidth, width;
23729758b77fSLuigi Rizzo 	int exitval = EX_OK;
23739758b77fSLuigi Rizzo 	int lac;
23749758b77fSLuigi Rizzo 	char **lav;
237562ff38aeSLuigi Rizzo 	u_long rnum, last;
23769758b77fSLuigi Rizzo 	char *endptr;
23779758b77fSLuigi Rizzo 	int seen = 0;
23789758b77fSLuigi Rizzo 
23799758b77fSLuigi Rizzo 	const int ocmd = do_pipe ? IP_DUMMYNET_GET : IP_FW_GET;
23809758b77fSLuigi Rizzo 	int nalloc = 1024;	/* start somewhere... */
23819758b77fSLuigi Rizzo 
238200ed6609SMaxim Konovalov 	last = 0;
238300ed6609SMaxim Konovalov 
2384571f8c1bSLuigi Rizzo 	if (test_only) {
2385571f8c1bSLuigi Rizzo 		fprintf(stderr, "Testing only, list disabled\n");
2386571f8c1bSLuigi Rizzo 		return;
2387571f8c1bSLuigi Rizzo 	}
2388571f8c1bSLuigi Rizzo 
23899758b77fSLuigi Rizzo 	ac--;
23909758b77fSLuigi Rizzo 	av++;
23919758b77fSLuigi Rizzo 
23929758b77fSLuigi Rizzo 	/* get rules or pipes from kernel, resizing array as necessary */
23939758b77fSLuigi Rizzo 	nbytes = nalloc;
23949758b77fSLuigi Rizzo 
23959758b77fSLuigi Rizzo 	while (nbytes >= nalloc) {
23969758b77fSLuigi Rizzo 		nalloc = nalloc * 2 + 200;
23979758b77fSLuigi Rizzo 		nbytes = nalloc;
23989758b77fSLuigi Rizzo 		if ((data = realloc(data, nbytes)) == NULL)
23999758b77fSLuigi Rizzo 			err(EX_OSERR, "realloc");
2400884be75cSThomas Moestl 		if (do_cmd(ocmd, data, (uintptr_t)&nbytes) < 0)
24019758b77fSLuigi Rizzo 			err(EX_OSERR, "getsockopt(IP_%s_GET)",
24029758b77fSLuigi Rizzo 				do_pipe ? "DUMMYNET" : "FW");
24039758b77fSLuigi Rizzo 	}
24049758b77fSLuigi Rizzo 
24059758b77fSLuigi Rizzo 	if (do_pipe) {
24069758b77fSLuigi Rizzo 		list_pipes(data, nbytes, ac, av);
24079758b77fSLuigi Rizzo 		goto done;
24089758b77fSLuigi Rizzo 	}
24099758b77fSLuigi Rizzo 
24109758b77fSLuigi Rizzo 	/*
24119758b77fSLuigi Rizzo 	 * Count static rules. They have variable size so we
24129758b77fSLuigi Rizzo 	 * need to scan the list to count them.
24139758b77fSLuigi Rizzo 	 */
241462ff38aeSLuigi Rizzo 	for (nstat = 1, r = data, lim = (char *)data + nbytes;
241562ff38aeSLuigi Rizzo 		    r->rulenum < 65535 && (char *)r < lim;
241662ff38aeSLuigi Rizzo 		    ++nstat, r = NEXT(r) )
24179758b77fSLuigi Rizzo 		; /* nothing */
24189758b77fSLuigi Rizzo 
24199758b77fSLuigi Rizzo 	/*
24209758b77fSLuigi Rizzo 	 * Count dynamic rules. This is easier as they have
24219758b77fSLuigi Rizzo 	 * fixed size.
24229758b77fSLuigi Rizzo 	 */
242362ff38aeSLuigi Rizzo 	r = NEXT(r);
24249758b77fSLuigi Rizzo 	dynrules = (ipfw_dyn_rule *)r ;
242562ff38aeSLuigi Rizzo 	n = (char *)r - (char *)data;
24269758b77fSLuigi Rizzo 	ndyn = (nbytes - n) / sizeof *dynrules;
24279758b77fSLuigi Rizzo 
242845f61351SMaxim Konovalov 	/* if showing stats, figure out column widths ahead of time */
242945f61351SMaxim Konovalov 	bcwidth = pcwidth = 0;
243062ff38aeSLuigi Rizzo 	if (show_counters) {
243162ff38aeSLuigi Rizzo 		for (n = 0, r = data; n < nstat; n++, r = NEXT(r)) {
243245f61351SMaxim Konovalov 			/* packet counter */
2433330462a3SBernd Walter 			width = snprintf(NULL, 0, "%llu",
2434330462a3SBernd Walter 			    align_uint64(&r->pcnt));
243545f61351SMaxim Konovalov 			if (width > pcwidth)
243645f61351SMaxim Konovalov 				pcwidth = width;
243745f61351SMaxim Konovalov 
243845f61351SMaxim Konovalov 			/* byte counter */
2439330462a3SBernd Walter 			width = snprintf(NULL, 0, "%llu",
2440330462a3SBernd Walter 			    align_uint64(&r->bcnt));
244145f61351SMaxim Konovalov 			if (width > bcwidth)
244245f61351SMaxim Konovalov 				bcwidth = width;
244345f61351SMaxim Konovalov 		}
244445f61351SMaxim Konovalov 	}
244545f61351SMaxim Konovalov 	if (do_dynamic && ndyn) {
244645f61351SMaxim Konovalov 		for (n = 0, d = dynrules; n < ndyn; n++, d++) {
2447330462a3SBernd Walter 			width = snprintf(NULL, 0, "%llu",
2448330462a3SBernd Walter 			    align_uint64(&d->pcnt));
244945f61351SMaxim Konovalov 			if (width > pcwidth)
245045f61351SMaxim Konovalov 				pcwidth = width;
245145f61351SMaxim Konovalov 
2452330462a3SBernd Walter 			width = snprintf(NULL, 0, "%llu",
2453330462a3SBernd Walter 			    align_uint64(&d->bcnt));
245445f61351SMaxim Konovalov 			if (width > bcwidth)
245545f61351SMaxim Konovalov 				bcwidth = width;
245645f61351SMaxim Konovalov 		}
245745f61351SMaxim Konovalov 	}
24589758b77fSLuigi Rizzo 	/* if no rule numbers were specified, list all rules */
24599758b77fSLuigi Rizzo 	if (ac == 0) {
246062ff38aeSLuigi Rizzo 		for (n = 0, r = data; n < nstat; n++, r = NEXT(r) )
246145f61351SMaxim Konovalov 			show_ipfw(r, pcwidth, bcwidth);
24629758b77fSLuigi Rizzo 
24639758b77fSLuigi Rizzo 		if (do_dynamic && ndyn) {
24649758b77fSLuigi Rizzo 			printf("## Dynamic rules (%d):\n", ndyn);
24659758b77fSLuigi Rizzo 			for (n = 0, d = dynrules; n < ndyn; n++, d++)
246645f61351SMaxim Konovalov 				show_dyn_ipfw(d, pcwidth, bcwidth);
24679758b77fSLuigi Rizzo 		}
24689758b77fSLuigi Rizzo 		goto done;
24699758b77fSLuigi Rizzo 	}
24709758b77fSLuigi Rizzo 
24719758b77fSLuigi Rizzo 	/* display specific rules requested on command line */
24729758b77fSLuigi Rizzo 
24739758b77fSLuigi Rizzo 	for (lac = ac, lav = av; lac != 0; lac--) {
24749758b77fSLuigi Rizzo 		/* convert command line rule # */
247562ff38aeSLuigi Rizzo 		last = rnum = strtoul(*lav++, &endptr, 10);
247662ff38aeSLuigi Rizzo 		if (*endptr == '-')
247762ff38aeSLuigi Rizzo 			last = strtoul(endptr+1, &endptr, 10);
24789758b77fSLuigi Rizzo 		if (*endptr) {
24799758b77fSLuigi Rizzo 			exitval = EX_USAGE;
24809758b77fSLuigi Rizzo 			warnx("invalid rule number: %s", *(lav - 1));
24819758b77fSLuigi Rizzo 			continue;
24829758b77fSLuigi Rizzo 		}
248362ff38aeSLuigi Rizzo 		for (n = seen = 0, r = data; n < nstat; n++, r = NEXT(r) ) {
248462ff38aeSLuigi Rizzo 			if (r->rulenum > last)
24859758b77fSLuigi Rizzo 				break;
248662ff38aeSLuigi Rizzo 			if (r->rulenum >= rnum && r->rulenum <= last) {
248745f61351SMaxim Konovalov 				show_ipfw(r, pcwidth, bcwidth);
24889758b77fSLuigi Rizzo 				seen = 1;
24899758b77fSLuigi Rizzo 			}
24909758b77fSLuigi Rizzo 		}
24919758b77fSLuigi Rizzo 		if (!seen) {
24929758b77fSLuigi Rizzo 			/* give precedence to other error(s) */
24939758b77fSLuigi Rizzo 			if (exitval == EX_OK)
24949758b77fSLuigi Rizzo 				exitval = EX_UNAVAILABLE;
24959758b77fSLuigi Rizzo 			warnx("rule %lu does not exist", rnum);
24969758b77fSLuigi Rizzo 		}
24979758b77fSLuigi Rizzo 	}
24989758b77fSLuigi Rizzo 
24999758b77fSLuigi Rizzo 	if (do_dynamic && ndyn) {
25009758b77fSLuigi Rizzo 		printf("## Dynamic rules:\n");
25019758b77fSLuigi Rizzo 		for (lac = ac, lav = av; lac != 0; lac--) {
25028195404bSBrooks Davis 			last = rnum = strtoul(*lav++, &endptr, 10);
250362ff38aeSLuigi Rizzo 			if (*endptr == '-')
250462ff38aeSLuigi Rizzo 				last = strtoul(endptr+1, &endptr, 10);
25059758b77fSLuigi Rizzo 			if (*endptr)
25069758b77fSLuigi Rizzo 				/* already warned */
25079758b77fSLuigi Rizzo 				continue;
25089758b77fSLuigi Rizzo 			for (n = 0, d = dynrules; n < ndyn; n++, d++) {
2509330462a3SBernd Walter 				uint16_t rulenum;
2510330462a3SBernd Walter 
2511330462a3SBernd Walter 				bcopy(&d->rule, &rulenum, sizeof(rulenum));
2512330462a3SBernd Walter 				if (rulenum > rnum)
25139758b77fSLuigi Rizzo 					break;
251462ff38aeSLuigi Rizzo 				if (r->rulenum >= rnum && r->rulenum <= last)
251545f61351SMaxim Konovalov 					show_dyn_ipfw(d, pcwidth, bcwidth);
25169758b77fSLuigi Rizzo 			}
25179758b77fSLuigi Rizzo 		}
25189758b77fSLuigi Rizzo 	}
25199758b77fSLuigi Rizzo 
25209758b77fSLuigi Rizzo 	ac = 0;
25219758b77fSLuigi Rizzo 
25229758b77fSLuigi Rizzo done:
25239758b77fSLuigi Rizzo 	free(data);
25249758b77fSLuigi Rizzo 
25259758b77fSLuigi Rizzo 	if (exitval != EX_OK)
25269758b77fSLuigi Rizzo 		exit(exitval);
252762ff38aeSLuigi Rizzo #undef NEXT
25289758b77fSLuigi Rizzo }
25299758b77fSLuigi Rizzo 
25309758b77fSLuigi Rizzo static void
25319758b77fSLuigi Rizzo show_usage(void)
25329758b77fSLuigi Rizzo {
25339758b77fSLuigi Rizzo 	fprintf(stderr, "usage: ipfw [options]\n"
25349758b77fSLuigi Rizzo "do \"ipfw -h\" or see ipfw manpage for details\n"
25359758b77fSLuigi Rizzo );
25369758b77fSLuigi Rizzo 	exit(EX_USAGE);
25379758b77fSLuigi Rizzo }
25389758b77fSLuigi Rizzo 
25399758b77fSLuigi Rizzo static void
25409758b77fSLuigi Rizzo help(void)
25419758b77fSLuigi Rizzo {
2542571f8c1bSLuigi Rizzo 	fprintf(stderr,
2543571f8c1bSLuigi Rizzo "ipfw syntax summary (but please do read the ipfw(8) manpage):\n"
2544ac6cec51SLuigi Rizzo "ipfw [-abcdefhnNqStTv] <command> where <command> is one of:\n"
2545571f8c1bSLuigi Rizzo "add [num] [set N] [prob x] RULE-BODY\n"
2546571f8c1bSLuigi Rizzo "{pipe|queue} N config PIPE-BODY\n"
2547571f8c1bSLuigi Rizzo "[pipe|queue] {zero|delete|show} [N{,N}]\n"
2548571f8c1bSLuigi Rizzo "set [disable N... enable N...] | move [rule] X to Y | swap X Y | show\n"
2549cd8b5ae0SRuslan Ermilov "table N {add ip[/bits] [value] | delete ip[/bits] | flush | list}\n"
25509758b77fSLuigi Rizzo "\n"
2551974dfe30SBrian Feldman "RULE-BODY:	check-state [PARAMS] | ACTION [PARAMS] ADDR [OPTION_LIST]\n"
25529066356bSBjoern A. Zeeb "ACTION:	check-state | allow | count | deny | unreach{,6} CODE |\n"
25539066356bSBjoern A. Zeeb "               skipto N | {divert|tee} PORT | forward ADDR |\n"
25549066356bSBjoern A. Zeeb "               pipe N | queue N\n"
2555974dfe30SBrian Feldman "PARAMS: 	[log [logamount LOGLIMIT]] [altq QUEUE_NAME]\n"
25569758b77fSLuigi Rizzo "ADDR:		[ MAC dst src ether_type ] \n"
25578195404bSBrooks Davis "		[ ip from IPADDR [ PORT ] to IPADDR [ PORTLIST ] ]\n"
25588195404bSBrooks Davis "		[ ipv6|ip6 from IP6ADDR [ PORT ] to IP6ADDR [ PORTLIST ] ]\n"
2559cd8b5ae0SRuslan Ermilov "IPADDR:	[not] { any | me | ip/bits{x,y,z} | table(t[,v]) | IPLIST }\n"
25608195404bSBrooks Davis "IP6ADDR:	[not] { any | me | me6 | ip6/bits | IP6LIST }\n"
25618195404bSBrooks Davis "IP6LIST:	{ ip6 | ip6/bits }[,IP6LIST]\n"
256226bf4d78SLuigi Rizzo "IPLIST:	{ ip | ip/bits | ip:mask }[,IPLIST]\n"
256326bf4d78SLuigi Rizzo "OPTION_LIST:	OPTION [OPTION_LIST]\n"
256417db1a04SBrian Feldman "OPTION:	bridged | diverted | diverted-loopback | diverted-output |\n"
25658195404bSBrooks Davis "	{dst-ip|src-ip} IPADDR | {dst-ip6|src-ip6|dst-ipv6|src-ipv6} IP6ADDR |\n"
25668195404bSBrooks Davis "	{dst-port|src-port} LIST |\n"
2567571f8c1bSLuigi Rizzo "	estab | frag | {gid|uid} N | icmptypes LIST | in | out | ipid LIST |\n"
2568571f8c1bSLuigi Rizzo "	iplen LIST | ipoptions SPEC | ipprecedence | ipsec | iptos SPEC |\n"
2569571f8c1bSLuigi Rizzo "	ipttl LIST | ipversion VER | keep-state | layer2 | limit ... |\n"
25708195404bSBrooks Davis "	icmp6types LIST | ext6hdr LIST | flow-id N[,N] |\n"
2571571f8c1bSLuigi Rizzo "	mac ... | mac-type LIST | proto LIST | {recv|xmit|via} {IF|IPADDR} |\n"
2572571f8c1bSLuigi Rizzo "	setup | {tcpack|tcpseq|tcpwin} NN | tcpflags SPEC | tcpoptions SPEC |\n"
2573c99ee9e0SBrian Feldman "	tcpdatalen LIST | verrevpath | versrcreach | antispoof\n"
25749758b77fSLuigi Rizzo );
25759758b77fSLuigi Rizzo exit(0);
25769758b77fSLuigi Rizzo }
25779758b77fSLuigi Rizzo 
25789758b77fSLuigi Rizzo 
25799758b77fSLuigi Rizzo static int
25809758b77fSLuigi Rizzo lookup_host (char *host, struct in_addr *ipaddr)
25819758b77fSLuigi Rizzo {
25829758b77fSLuigi Rizzo 	struct hostent *he;
25839758b77fSLuigi Rizzo 
25849758b77fSLuigi Rizzo 	if (!inet_aton(host, ipaddr)) {
25859758b77fSLuigi Rizzo 		if ((he = gethostbyname(host)) == NULL)
25869758b77fSLuigi Rizzo 			return(-1);
25879758b77fSLuigi Rizzo 		*ipaddr = *(struct in_addr *)he->h_addr_list[0];
25889758b77fSLuigi Rizzo 	}
25899758b77fSLuigi Rizzo 	return(0);
25909758b77fSLuigi Rizzo }
25919758b77fSLuigi Rizzo 
25929758b77fSLuigi Rizzo /*
25939758b77fSLuigi Rizzo  * fills the addr and mask fields in the instruction as appropriate from av.
25949758b77fSLuigi Rizzo  * Update length as appropriate.
25959758b77fSLuigi Rizzo  * The following formats are allowed:
25969758b77fSLuigi Rizzo  *	me	returns O_IP_*_ME
25979758b77fSLuigi Rizzo  *	1.2.3.4		single IP address
25989758b77fSLuigi Rizzo  *	1.2.3.4:5.6.7.8	address:mask
25999758b77fSLuigi Rizzo  *	1.2.3.4/24	address/mask
26009758b77fSLuigi Rizzo  *	1.2.3.4/26{1,6,5,4,23}	set of addresses in a subnet
2601571f8c1bSLuigi Rizzo  * We can have multiple comma-separated address/mask entries.
26029758b77fSLuigi Rizzo  */
26039758b77fSLuigi Rizzo static void
26049758b77fSLuigi Rizzo fill_ip(ipfw_insn_ip *cmd, char *av)
26059758b77fSLuigi Rizzo {
2606571f8c1bSLuigi Rizzo 	int len = 0;
2607571f8c1bSLuigi Rizzo 	uint32_t *d = ((ipfw_insn_u32 *)cmd)->d;
26089758b77fSLuigi Rizzo 
26099758b77fSLuigi Rizzo 	cmd->o.len &= ~F_LEN_MASK;	/* zero len */
26109758b77fSLuigi Rizzo 
261101750186SBrooks Davis 	if (_substrcmp(av, "any") == 0)
26129758b77fSLuigi Rizzo 		return;
26139758b77fSLuigi Rizzo 
261401750186SBrooks Davis 	if (_substrcmp(av, "me") == 0) {
26159758b77fSLuigi Rizzo 		cmd->o.len |= F_INSN_SIZE(ipfw_insn);
26169758b77fSLuigi Rizzo 		return;
26179758b77fSLuigi Rizzo 	}
26189758b77fSLuigi Rizzo 
261901750186SBrooks Davis 	if (strncmp(av, "table(", 6) == 0) {
2620cd8b5ae0SRuslan Ermilov 		char *p = strchr(av + 6, ',');
2621cd8b5ae0SRuslan Ermilov 
2622cd8b5ae0SRuslan Ermilov 		if (p)
2623cd8b5ae0SRuslan Ermilov 			*p++ = '\0';
2624cd8b5ae0SRuslan Ermilov 		cmd->o.opcode = O_IP_DST_LOOKUP;
2625cd8b5ae0SRuslan Ermilov 		cmd->o.arg1 = strtoul(av + 6, NULL, 0);
2626cd8b5ae0SRuslan Ermilov 		if (p) {
2627cd8b5ae0SRuslan Ermilov 			cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32);
2628cd8b5ae0SRuslan Ermilov 			d[0] = strtoul(p, NULL, 0);
2629cd8b5ae0SRuslan Ermilov 		} else
2630cd8b5ae0SRuslan Ermilov 			cmd->o.len |= F_INSN_SIZE(ipfw_insn);
2631cd8b5ae0SRuslan Ermilov 		return;
2632cd8b5ae0SRuslan Ermilov 	}
2633cd8b5ae0SRuslan Ermilov 
2634571f8c1bSLuigi Rizzo     while (av) {
2635571f8c1bSLuigi Rizzo 	/*
2636571f8c1bSLuigi Rizzo 	 * After the address we can have '/' or ':' indicating a mask,
2637571f8c1bSLuigi Rizzo 	 * ',' indicating another address follows, '{' indicating a
2638571f8c1bSLuigi Rizzo 	 * set of addresses of unspecified size.
2639571f8c1bSLuigi Rizzo 	 */
2640571f8c1bSLuigi Rizzo 	char *p = strpbrk(av, "/:,{");
2641571f8c1bSLuigi Rizzo 	int masklen;
2642571f8c1bSLuigi Rizzo 	char md;
2643571f8c1bSLuigi Rizzo 
26449758b77fSLuigi Rizzo 	if (p) {
26459758b77fSLuigi Rizzo 		md = *p;
26469758b77fSLuigi Rizzo 		*p++ = '\0';
2647571f8c1bSLuigi Rizzo 	} else
2648571f8c1bSLuigi Rizzo 		md = '\0';
26499758b77fSLuigi Rizzo 
2650571f8c1bSLuigi Rizzo 	if (lookup_host(av, (struct in_addr *)&d[0]) != 0)
26519758b77fSLuigi Rizzo 		errx(EX_NOHOST, "hostname ``%s'' unknown", av);
26529758b77fSLuigi Rizzo 	switch (md) {
26539758b77fSLuigi Rizzo 	case ':':
2654571f8c1bSLuigi Rizzo 		if (!inet_aton(p, (struct in_addr *)&d[1]))
26559758b77fSLuigi Rizzo 			errx(EX_DATAERR, "bad netmask ``%s''", p);
26569758b77fSLuigi Rizzo 		break;
26579758b77fSLuigi Rizzo 	case '/':
2658571f8c1bSLuigi Rizzo 		masklen = atoi(p);
2659571f8c1bSLuigi Rizzo 		if (masklen == 0)
2660571f8c1bSLuigi Rizzo 			d[1] = htonl(0);	/* mask */
2661571f8c1bSLuigi Rizzo 		else if (masklen > 32)
26629758b77fSLuigi Rizzo 			errx(EX_DATAERR, "bad width ``%s''", p);
26639758b77fSLuigi Rizzo 		else
2664571f8c1bSLuigi Rizzo 			d[1] = htonl(~0 << (32 - masklen));
26659758b77fSLuigi Rizzo 		break;
2666571f8c1bSLuigi Rizzo 	case '{':	/* no mask, assume /24 and put back the '{' */
2667571f8c1bSLuigi Rizzo 		d[1] = htonl(~0 << (32 - 24));
2668571f8c1bSLuigi Rizzo 		*(--p) = md;
2669571f8c1bSLuigi Rizzo 		break;
2670571f8c1bSLuigi Rizzo 
2671571f8c1bSLuigi Rizzo 	case ',':	/* single address plus continuation */
2672571f8c1bSLuigi Rizzo 		*(--p) = md;
2673571f8c1bSLuigi Rizzo 		/* FALLTHROUGH */
2674571f8c1bSLuigi Rizzo 	case 0:		/* initialization value */
26759758b77fSLuigi Rizzo 	default:
2676571f8c1bSLuigi Rizzo 		d[1] = htonl(~0);	/* force /32 */
26779758b77fSLuigi Rizzo 		break;
26789758b77fSLuigi Rizzo 	}
2679571f8c1bSLuigi Rizzo 	d[0] &= d[1];		/* mask base address with mask */
2680571f8c1bSLuigi Rizzo 	/* find next separator */
2681571f8c1bSLuigi Rizzo 	if (p)
2682571f8c1bSLuigi Rizzo 		p = strpbrk(p, ",{");
2683571f8c1bSLuigi Rizzo 	if (p && *p == '{') {
26849758b77fSLuigi Rizzo 		/*
2685571f8c1bSLuigi Rizzo 		 * We have a set of addresses. They are stored as follows:
26869758b77fSLuigi Rizzo 		 *   arg1	is the set size (powers of 2, 2..256)
26879758b77fSLuigi Rizzo 		 *   addr	is the base address IN HOST FORMAT
2688571f8c1bSLuigi Rizzo 		 *   mask..	is an array of arg1 bits (rounded up to
2689571f8c1bSLuigi Rizzo 		 *		the next multiple of 32) with bits set
2690571f8c1bSLuigi Rizzo 		 *		for each host in the map.
26919758b77fSLuigi Rizzo 		 */
2692571f8c1bSLuigi Rizzo 		uint32_t *map = (uint32_t *)&cmd->mask;
26939758b77fSLuigi Rizzo 		int low, high;
2694f3a126d3SLuigi Rizzo 		int i = contigmask((uint8_t *)&(d[1]), 32);
26959758b77fSLuigi Rizzo 
2696571f8c1bSLuigi Rizzo 		if (len > 0)
2697571f8c1bSLuigi Rizzo 			errx(EX_DATAERR, "address set cannot be in a list");
2698571f8c1bSLuigi Rizzo 		if (i < 24 || i > 31)
2699571f8c1bSLuigi Rizzo 			errx(EX_DATAERR, "invalid set with mask %d\n", i);
2700571f8c1bSLuigi Rizzo 		cmd->o.arg1 = 1<<(32-i);	/* map length		*/
2701571f8c1bSLuigi Rizzo 		d[0] = ntohl(d[0]);		/* base addr in host format */
27029758b77fSLuigi Rizzo 		cmd->o.opcode = O_IP_DST_SET;	/* default */
27039758b77fSLuigi Rizzo 		cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32) + (cmd->o.arg1+31)/32;
270461360012SLuigi Rizzo 		for (i = 0; i < (cmd->o.arg1+31)/32 ; i++)
2705571f8c1bSLuigi Rizzo 			map[i] = 0;	/* clear map */
27069758b77fSLuigi Rizzo 
27079758b77fSLuigi Rizzo 		av = p + 1;
2708571f8c1bSLuigi Rizzo 		low = d[0] & 0xff;
27099758b77fSLuigi Rizzo 		high = low + cmd->o.arg1 - 1;
2710571f8c1bSLuigi Rizzo 		/*
2711571f8c1bSLuigi Rizzo 		 * Here, i stores the previous value when we specify a range
2712571f8c1bSLuigi Rizzo 		 * of addresses within a mask, e.g. 45-63. i = -1 means we
2713571f8c1bSLuigi Rizzo 		 * have no previous value.
2714571f8c1bSLuigi Rizzo 		 */
27159ef3f16dSLuigi Rizzo 		i = -1;	/* previous value in a range */
27169758b77fSLuigi Rizzo 		while (isdigit(*av)) {
27179758b77fSLuigi Rizzo 			char *s;
2718571f8c1bSLuigi Rizzo 			int a = strtol(av, &s, 0);
27199758b77fSLuigi Rizzo 
2720571f8c1bSLuigi Rizzo 			if (s == av) { /* no parameter */
2721571f8c1bSLuigi Rizzo 			    if (*av != '}')
2722571f8c1bSLuigi Rizzo 				errx(EX_DATAERR, "set not closed\n");
2723571f8c1bSLuigi Rizzo 			    if (i != -1)
2724571f8c1bSLuigi Rizzo 				errx(EX_DATAERR, "incomplete range %d-", i);
27259758b77fSLuigi Rizzo 			    break;
27269758b77fSLuigi Rizzo 			}
2727571f8c1bSLuigi Rizzo 			if (a < low || a > high)
2728571f8c1bSLuigi Rizzo 			    errx(EX_DATAERR, "addr %d out of range [%d-%d]\n",
2729571f8c1bSLuigi Rizzo 				a, low, high);
27309758b77fSLuigi Rizzo 			a -= low;
27319ef3f16dSLuigi Rizzo 			if (i == -1)	/* no previous in range */
27329ef3f16dSLuigi Rizzo 			    i = a;
27339ef3f16dSLuigi Rizzo 			else {		/* check that range is valid */
27349ef3f16dSLuigi Rizzo 			    if (i > a)
27359ef3f16dSLuigi Rizzo 				errx(EX_DATAERR, "invalid range %d-%d",
27369ef3f16dSLuigi Rizzo 					i+low, a+low);
27379ef3f16dSLuigi Rizzo 			    if (*s == '-')
27389ef3f16dSLuigi Rizzo 				errx(EX_DATAERR, "double '-' in range");
27399ef3f16dSLuigi Rizzo 			}
27409ef3f16dSLuigi Rizzo 			for (; i <= a; i++)
2741571f8c1bSLuigi Rizzo 			    map[i/32] |= 1<<(i & 31);
27429ef3f16dSLuigi Rizzo 			i = -1;
27439ef3f16dSLuigi Rizzo 			if (*s == '-')
27449ef3f16dSLuigi Rizzo 			    i = a;
2745571f8c1bSLuigi Rizzo 			else if (*s == '}')
27469758b77fSLuigi Rizzo 			    break;
27479758b77fSLuigi Rizzo 			av = s+1;
27489758b77fSLuigi Rizzo 		}
27499758b77fSLuigi Rizzo 		return;
27509758b77fSLuigi Rizzo 	}
2751571f8c1bSLuigi Rizzo 	av = p;
2752571f8c1bSLuigi Rizzo 	if (av)			/* then *av must be a ',' */
2753571f8c1bSLuigi Rizzo 		av++;
27549758b77fSLuigi Rizzo 
2755571f8c1bSLuigi Rizzo 	/* Check this entry */
2756571f8c1bSLuigi Rizzo 	if (d[1] == 0) { /* "any", specified as x.x.x.x/0 */
2757571f8c1bSLuigi Rizzo 		/*
2758571f8c1bSLuigi Rizzo 		 * 'any' turns the entire list into a NOP.
2759571f8c1bSLuigi Rizzo 		 * 'not any' never matches, so it is removed from the
2760571f8c1bSLuigi Rizzo 		 * list unless it is the only item, in which case we
2761571f8c1bSLuigi Rizzo 		 * report an error.
2762571f8c1bSLuigi Rizzo 		 */
2763571f8c1bSLuigi Rizzo 		if (cmd->o.len & F_NOT) {	/* "not any" never matches */
2764571f8c1bSLuigi Rizzo 			if (av == NULL && len == 0) /* only this entry */
27659758b77fSLuigi Rizzo 				errx(EX_DATAERR, "not any never matches");
2766571f8c1bSLuigi Rizzo 		}
2767571f8c1bSLuigi Rizzo 		/* else do nothing and skip this entry */
276814533a98SMaxim Konovalov 		return;
2769571f8c1bSLuigi Rizzo 	}
2770571f8c1bSLuigi Rizzo 	/* A single IP can be stored in an optimized format */
2771571f8c1bSLuigi Rizzo 	if (d[1] == IP_MASK_ALL && av == NULL && len == 0) {
27729758b77fSLuigi Rizzo 		cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32);
2773571f8c1bSLuigi Rizzo 		return;
2774571f8c1bSLuigi Rizzo 	}
2775571f8c1bSLuigi Rizzo 	len += 2;	/* two words... */
2776571f8c1bSLuigi Rizzo 	d += 2;
2777571f8c1bSLuigi Rizzo     } /* end while */
2778571f8c1bSLuigi Rizzo     cmd->o.len |= len+1;
27799758b77fSLuigi Rizzo }
27809758b77fSLuigi Rizzo 
27819758b77fSLuigi Rizzo 
27828195404bSBrooks Davis /* Try to find ipv6 address by hostname */
27838195404bSBrooks Davis static int
27848195404bSBrooks Davis lookup_host6 (char *host, struct in6_addr *ip6addr)
27858195404bSBrooks Davis {
27868195404bSBrooks Davis 	struct hostent *he;
27878195404bSBrooks Davis 
27888195404bSBrooks Davis 	if (!inet_pton(AF_INET6, host, ip6addr)) {
27898195404bSBrooks Davis 		if ((he = gethostbyname2(host, AF_INET6)) == NULL)
27908195404bSBrooks Davis 			return(-1);
27918195404bSBrooks Davis 		memcpy(ip6addr, he->h_addr_list[0], sizeof( struct in6_addr));
27928195404bSBrooks Davis 	}
27938195404bSBrooks Davis 	return(0);
27948195404bSBrooks Davis }
27958195404bSBrooks Davis 
27968195404bSBrooks Davis 
27978195404bSBrooks Davis /* n2mask sets n bits of the mask */
27988195404bSBrooks Davis static void
27998195404bSBrooks Davis n2mask(struct in6_addr *mask, int n)
28008195404bSBrooks Davis {
28018195404bSBrooks Davis 	static int	minimask[9] =
28028195404bSBrooks Davis 	    { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
28038195404bSBrooks Davis 	u_char		*p;
28048195404bSBrooks Davis 
28058195404bSBrooks Davis 	memset(mask, 0, sizeof(struct in6_addr));
28068195404bSBrooks Davis 	p = (u_char *) mask;
28078195404bSBrooks Davis 	for (; n > 0; p++, n -= 8) {
28088195404bSBrooks Davis 		if (n >= 8)
28098195404bSBrooks Davis 			*p = 0xff;
28108195404bSBrooks Davis 		else
28118195404bSBrooks Davis 			*p = minimask[n];
28128195404bSBrooks Davis 	}
28138195404bSBrooks Davis 	return;
28148195404bSBrooks Davis }
28158195404bSBrooks Davis 
28168195404bSBrooks Davis 
28178195404bSBrooks Davis /*
28188195404bSBrooks Davis  * fill the addr and mask fields in the instruction as appropriate from av.
28198195404bSBrooks Davis  * Update length as appropriate.
28208195404bSBrooks Davis  * The following formats are allowed:
28218195404bSBrooks Davis  *     any     matches any IP6. Actually returns an empty instruction.
28228195404bSBrooks Davis  *     me      returns O_IP6_*_ME
28238195404bSBrooks Davis  *
28248195404bSBrooks Davis  *     03f1::234:123:0342                single IP6 addres
28258195404bSBrooks Davis  *     03f1::234:123:0342/24            address/mask
28268195404bSBrooks Davis  *     03f1::234:123:0342/24,03f1::234:123:0343/               List of address
28278195404bSBrooks Davis  *
28288195404bSBrooks Davis  * Set of address (as in ipv6) not supported because ipv6 address
28298195404bSBrooks Davis  * are typically random past the initial prefix.
28308195404bSBrooks Davis  * Return 1 on success, 0 on failure.
28318195404bSBrooks Davis  */
28328195404bSBrooks Davis static int
28338195404bSBrooks Davis fill_ip6(ipfw_insn_ip6 *cmd, char *av)
28348195404bSBrooks Davis {
28358195404bSBrooks Davis 	int len = 0;
28368195404bSBrooks Davis 	struct in6_addr *d = &(cmd->addr6);
28378195404bSBrooks Davis 	/*
28388195404bSBrooks Davis 	 * Needed for multiple address.
28398195404bSBrooks Davis 	 * Note d[1] points to struct in6_add r mask6 of cmd
28408195404bSBrooks Davis 	 */
28418195404bSBrooks Davis 
28428195404bSBrooks Davis        cmd->o.len &= ~F_LEN_MASK;	/* zero len */
28438195404bSBrooks Davis 
28448195404bSBrooks Davis        if (strcmp(av, "any") == 0)
28458195404bSBrooks Davis 	       return (1);
28468195404bSBrooks Davis 
28478195404bSBrooks Davis 
28488195404bSBrooks Davis        if (strcmp(av, "me") == 0) {	/* Set the data for "me" opt*/
28498195404bSBrooks Davis 	       cmd->o.len |= F_INSN_SIZE(ipfw_insn);
28508195404bSBrooks Davis 	       return (1);
28518195404bSBrooks Davis        }
28528195404bSBrooks Davis 
28538195404bSBrooks Davis        if (strcmp(av, "me6") == 0) {	/* Set the data for "me" opt*/
28548195404bSBrooks Davis 	       cmd->o.len |= F_INSN_SIZE(ipfw_insn);
28558195404bSBrooks Davis 	       return (1);
28568195404bSBrooks Davis        }
28578195404bSBrooks Davis 
28588195404bSBrooks Davis        av = strdup(av);
28598195404bSBrooks Davis        while (av) {
28608195404bSBrooks Davis 		/*
28618195404bSBrooks Davis 		 * After the address we can have '/' indicating a mask,
28628195404bSBrooks Davis 		 * or ',' indicating another address follows.
28638195404bSBrooks Davis 		 */
28648195404bSBrooks Davis 
28658195404bSBrooks Davis 		char *p;
28668195404bSBrooks Davis 		int masklen;
28678195404bSBrooks Davis 		char md = '\0';
28688195404bSBrooks Davis 
28698195404bSBrooks Davis 		if ((p = strpbrk(av, "/,")) ) {
28708195404bSBrooks Davis 			md = *p;	/* save the separator */
28718195404bSBrooks Davis 			*p = '\0';	/* terminate address string */
28728195404bSBrooks Davis 			p++;		/* and skip past it */
28738195404bSBrooks Davis 		}
28748195404bSBrooks Davis 		/* now p points to NULL, mask or next entry */
28758195404bSBrooks Davis 
28768195404bSBrooks Davis 		/* lookup stores address in *d as a side effect */
28778195404bSBrooks Davis 		if (lookup_host6(av, d) != 0) {
28788195404bSBrooks Davis 			/* XXX: failed. Free memory and go */
28798195404bSBrooks Davis 			errx(EX_DATAERR, "bad address \"%s\"", av);
28808195404bSBrooks Davis 		}
28818195404bSBrooks Davis 		/* next, look at the mask, if any */
28828195404bSBrooks Davis 		masklen = (md == '/') ? atoi(p) : 128;
28838195404bSBrooks Davis 		if (masklen > 128 || masklen < 0)
28848195404bSBrooks Davis 			errx(EX_DATAERR, "bad width \"%s\''", p);
28858195404bSBrooks Davis 		else
28868195404bSBrooks Davis 			n2mask(&d[1], masklen);
28878195404bSBrooks Davis 
28888195404bSBrooks Davis 		APPLY_MASK(d, &d[1])   /* mask base address with mask */
28898195404bSBrooks Davis 
28908195404bSBrooks Davis 		/* find next separator */
28918195404bSBrooks Davis 
28928195404bSBrooks Davis 		if (md == '/') {	/* find separator past the mask */
28938195404bSBrooks Davis 			p = strpbrk(p, ",");
28948195404bSBrooks Davis 			if (p != NULL)
28958195404bSBrooks Davis 				p++;
28968195404bSBrooks Davis 		}
28978195404bSBrooks Davis 		av = p;
28988195404bSBrooks Davis 
28998195404bSBrooks Davis 		/* Check this entry */
29008195404bSBrooks Davis 		if (masklen == 0) {
29018195404bSBrooks Davis 			/*
29028195404bSBrooks Davis 			 * 'any' turns the entire list into a NOP.
29038195404bSBrooks Davis 			 * 'not any' never matches, so it is removed from the
29048195404bSBrooks Davis 			 * list unless it is the only item, in which case we
29058195404bSBrooks Davis 			 * report an error.
29068195404bSBrooks Davis 			 */
29078195404bSBrooks Davis 			if (cmd->o.len & F_NOT && av == NULL && len == 0)
29088195404bSBrooks Davis 				errx(EX_DATAERR, "not any never matches");
29098195404bSBrooks Davis 			continue;
29108195404bSBrooks Davis 		}
29118195404bSBrooks Davis 
29128195404bSBrooks Davis 		/*
29138195404bSBrooks Davis 		 * A single IP can be stored alone
29148195404bSBrooks Davis 		 */
29158195404bSBrooks Davis 		if (masklen == 128 && av == NULL && len == 0) {
29168195404bSBrooks Davis 			len = F_INSN_SIZE(struct in6_addr);
29178195404bSBrooks Davis 			break;
29188195404bSBrooks Davis 		}
29198195404bSBrooks Davis 
29208195404bSBrooks Davis 		/* Update length and pointer to arguments */
29218195404bSBrooks Davis 		len += F_INSN_SIZE(struct in6_addr)*2;
29228195404bSBrooks Davis 		d += 2;
29238195404bSBrooks Davis 	} /* end while */
29248195404bSBrooks Davis 
29258195404bSBrooks Davis 	/*
29268195404bSBrooks Davis 	 * Total length of the command, remember that 1 is the size of
29278195404bSBrooks Davis 	 * the base command.
29288195404bSBrooks Davis 	 */
29298195404bSBrooks Davis 	cmd->o.len |= len+1;
29308195404bSBrooks Davis 	free(av);
29318195404bSBrooks Davis 	return (1);
29328195404bSBrooks Davis }
29338195404bSBrooks Davis 
29348195404bSBrooks Davis /*
29358195404bSBrooks Davis  * fills command for ipv6 flow-id filtering
29368195404bSBrooks Davis  * note that the 20 bit flow number is stored in a array of u_int32_t
29378195404bSBrooks Davis  * it's supported lists of flow-id, so in the o.arg1 we store how many
29388195404bSBrooks Davis  * additional flow-id we want to filter, the basic is 1
29398195404bSBrooks Davis  */
29408195404bSBrooks Davis void
29418195404bSBrooks Davis fill_flow6( ipfw_insn_u32 *cmd, char *av )
29428195404bSBrooks Davis {
29438195404bSBrooks Davis 	u_int32_t type;	 /* Current flow number */
29448195404bSBrooks Davis 	u_int16_t nflow = 0;    /* Current flow index */
29458195404bSBrooks Davis 	char *s = av;
29468195404bSBrooks Davis 	cmd->d[0] = 0;	  /* Initializing the base number*/
29478195404bSBrooks Davis 
29488195404bSBrooks Davis 	while (s) {
29498195404bSBrooks Davis 		av = strsep( &s, ",") ;
29508195404bSBrooks Davis 		type = strtoul(av, &av, 0);
29518195404bSBrooks Davis 		if (*av != ',' && *av != '\0')
29528195404bSBrooks Davis 			errx(EX_DATAERR, "invalid ipv6 flow number %s", av);
29538195404bSBrooks Davis 		if (type > 0xfffff)
29548195404bSBrooks Davis 			errx(EX_DATAERR, "flow number out of range %s", av);
29558195404bSBrooks Davis 		cmd->d[nflow] |= type;
29568195404bSBrooks Davis 		nflow++;
29578195404bSBrooks Davis 	}
29588195404bSBrooks Davis 	if( nflow > 0 ) {
29598195404bSBrooks Davis 		cmd->o.opcode = O_FLOW6ID;
29608195404bSBrooks Davis 		cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32) + nflow;
29618195404bSBrooks Davis 		cmd->o.arg1 = nflow;
29628195404bSBrooks Davis 	}
29638195404bSBrooks Davis 	else {
29648195404bSBrooks Davis 		errx(EX_DATAERR, "invalid ipv6 flow number %s", av);
29658195404bSBrooks Davis 	}
29668195404bSBrooks Davis }
29678195404bSBrooks Davis 
29688195404bSBrooks Davis static ipfw_insn *
29698195404bSBrooks Davis add_srcip6(ipfw_insn *cmd, char *av)
29708195404bSBrooks Davis {
29718195404bSBrooks Davis 
29728195404bSBrooks Davis 	fill_ip6((ipfw_insn_ip6 *)cmd, av);
29738195404bSBrooks Davis 	if (F_LEN(cmd) == 0)				/* any */
29748195404bSBrooks Davis 		;
29758195404bSBrooks Davis 	if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn)) {	/* "me" */
29768195404bSBrooks Davis 		cmd->opcode = O_IP6_SRC_ME;
29778195404bSBrooks Davis 	} else if (F_LEN(cmd) ==
29788195404bSBrooks Davis 	    (F_INSN_SIZE(struct in6_addr) + F_INSN_SIZE(ipfw_insn))) {
29798195404bSBrooks Davis 		/* single IP, no mask*/
29808195404bSBrooks Davis 		cmd->opcode = O_IP6_SRC;
29818195404bSBrooks Davis 	} else {					/* addr/mask opt */
29828195404bSBrooks Davis 		cmd->opcode = O_IP6_SRC_MASK;
29838195404bSBrooks Davis 	}
29848195404bSBrooks Davis 	return cmd;
29858195404bSBrooks Davis }
29868195404bSBrooks Davis 
29878195404bSBrooks Davis static ipfw_insn *
29888195404bSBrooks Davis add_dstip6(ipfw_insn *cmd, char *av)
29898195404bSBrooks Davis {
29908195404bSBrooks Davis 
29918195404bSBrooks Davis 	fill_ip6((ipfw_insn_ip6 *)cmd, av);
29928195404bSBrooks Davis 	if (F_LEN(cmd) == 0)				/* any */
29938195404bSBrooks Davis 		;
29948195404bSBrooks Davis 	if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn)) {	/* "me" */
29958195404bSBrooks Davis 		cmd->opcode = O_IP6_DST_ME;
29968195404bSBrooks Davis 	} else if (F_LEN(cmd) ==
29978195404bSBrooks Davis 	    (F_INSN_SIZE(struct in6_addr) + F_INSN_SIZE(ipfw_insn))) {
29988195404bSBrooks Davis 		/* single IP, no mask*/
29998195404bSBrooks Davis 		cmd->opcode = O_IP6_DST;
30008195404bSBrooks Davis 	} else {					/* addr/mask opt */
30018195404bSBrooks Davis 		cmd->opcode = O_IP6_DST_MASK;
30028195404bSBrooks Davis 	}
30038195404bSBrooks Davis 	return cmd;
30048195404bSBrooks Davis }
30058195404bSBrooks Davis 
30068195404bSBrooks Davis 
30079758b77fSLuigi Rizzo /*
30089758b77fSLuigi Rizzo  * helper function to process a set of flags and set bits in the
30099758b77fSLuigi Rizzo  * appropriate masks.
30109758b77fSLuigi Rizzo  */
30119758b77fSLuigi Rizzo static void
30129758b77fSLuigi Rizzo fill_flags(ipfw_insn *cmd, enum ipfw_opcodes opcode,
30139758b77fSLuigi Rizzo 	struct _s_x *flags, char *p)
30149758b77fSLuigi Rizzo {
3015571f8c1bSLuigi Rizzo 	uint8_t set=0, clear=0;
30169758b77fSLuigi Rizzo 
30179758b77fSLuigi Rizzo 	while (p && *p) {
30189758b77fSLuigi Rizzo 		char *q;	/* points to the separator */
30199758b77fSLuigi Rizzo 		int val;
3020571f8c1bSLuigi Rizzo 		uint8_t *which;	/* mask we are working on */
30219758b77fSLuigi Rizzo 
30229758b77fSLuigi Rizzo 		if (*p == '!') {
30239758b77fSLuigi Rizzo 			p++;
30249758b77fSLuigi Rizzo 			which = &clear;
30259758b77fSLuigi Rizzo 		} else
30269758b77fSLuigi Rizzo 			which = &set;
30279758b77fSLuigi Rizzo 		q = strchr(p, ',');
30289758b77fSLuigi Rizzo 		if (q)
30299758b77fSLuigi Rizzo 			*q++ = '\0';
30309758b77fSLuigi Rizzo 		val = match_token(flags, p);
30319758b77fSLuigi Rizzo 		if (val <= 0)
30329758b77fSLuigi Rizzo 			errx(EX_DATAERR, "invalid flag %s", p);
3033571f8c1bSLuigi Rizzo 		*which |= (uint8_t)val;
30349758b77fSLuigi Rizzo 		p = q;
30359758b77fSLuigi Rizzo 	}
30369758b77fSLuigi Rizzo         cmd->opcode = opcode;
30379758b77fSLuigi Rizzo         cmd->len =  (cmd->len & (F_NOT | F_OR)) | 1;
30389758b77fSLuigi Rizzo         cmd->arg1 = (set & 0xff) | ( (clear & 0xff) << 8);
30399758b77fSLuigi Rizzo }
30409758b77fSLuigi Rizzo 
30419758b77fSLuigi Rizzo 
30429758b77fSLuigi Rizzo static void
30439758b77fSLuigi Rizzo delete(int ac, char *av[])
30449758b77fSLuigi Rizzo {
3045571f8c1bSLuigi Rizzo 	uint32_t rulenum;
304662ff38aeSLuigi Rizzo 	struct dn_pipe p;
30479758b77fSLuigi Rizzo 	int i;
30489758b77fSLuigi Rizzo 	int exitval = EX_OK;
304943405724SLuigi Rizzo 	int do_set = 0;
30509758b77fSLuigi Rizzo 
305162ff38aeSLuigi Rizzo 	memset(&p, 0, sizeof p);
30529758b77fSLuigi Rizzo 
30539758b77fSLuigi Rizzo 	av++; ac--;
305404f70834SChristian S.J. Peron 	NEED1("missing rule specification");
305501750186SBrooks Davis 	if (ac > 0 && _substrcmp(*av, "set") == 0) {
305643405724SLuigi Rizzo 		do_set = 1;	/* delete set */
305743405724SLuigi Rizzo 		ac--; av++;
305899e5e645SLuigi Rizzo 	}
30599758b77fSLuigi Rizzo 
30609758b77fSLuigi Rizzo 	/* Rule number */
30619758b77fSLuigi Rizzo 	while (ac && isdigit(**av)) {
30629758b77fSLuigi Rizzo 		i = atoi(*av); av++; ac--;
30639758b77fSLuigi Rizzo 		if (do_pipe) {
30649758b77fSLuigi Rizzo 			if (do_pipe == 1)
306562ff38aeSLuigi Rizzo 				p.pipe_nr = i;
30669758b77fSLuigi Rizzo 			else
306762ff38aeSLuigi Rizzo 				p.fs.fs_nr = i;
306862ff38aeSLuigi Rizzo 			i = do_cmd(IP_DUMMYNET_DEL, &p, sizeof p);
30699758b77fSLuigi Rizzo 			if (i) {
30709758b77fSLuigi Rizzo 				exitval = 1;
30719758b77fSLuigi Rizzo 				warn("rule %u: setsockopt(IP_DUMMYNET_DEL)",
307262ff38aeSLuigi Rizzo 				    do_pipe == 1 ? p.pipe_nr : p.fs.fs_nr);
30739758b77fSLuigi Rizzo 			}
30749758b77fSLuigi Rizzo 		} else {
307599e5e645SLuigi Rizzo 			rulenum =  (i & 0xffff) | (do_set << 24);
3076571f8c1bSLuigi Rizzo 			i = do_cmd(IP_FW_DEL, &rulenum, sizeof rulenum);
30779758b77fSLuigi Rizzo 			if (i) {
30789758b77fSLuigi Rizzo 				exitval = EX_UNAVAILABLE;
30799758b77fSLuigi Rizzo 				warn("rule %u: setsockopt(IP_FW_DEL)",
30809758b77fSLuigi Rizzo 				    rulenum);
30819758b77fSLuigi Rizzo 			}
30829758b77fSLuigi Rizzo 		}
30839758b77fSLuigi Rizzo 	}
30849758b77fSLuigi Rizzo 	if (exitval != EX_OK)
30859758b77fSLuigi Rizzo 		exit(exitval);
30869758b77fSLuigi Rizzo }
30879758b77fSLuigi Rizzo 
30889758b77fSLuigi Rizzo 
30899758b77fSLuigi Rizzo /*
30909758b77fSLuigi Rizzo  * fill the interface structure. We do not check the name as we can
30919758b77fSLuigi Rizzo  * create interfaces dynamically, so checking them at insert time
30929758b77fSLuigi Rizzo  * makes relatively little sense.
30939bf40edeSBrooks Davis  * Interface names containing '*', '?', or '[' are assumed to be shell
30949bf40edeSBrooks Davis  * patterns which match interfaces.
30959758b77fSLuigi Rizzo  */
30969758b77fSLuigi Rizzo static void
30979758b77fSLuigi Rizzo fill_iface(ipfw_insn_if *cmd, char *arg)
30989758b77fSLuigi Rizzo {
30999758b77fSLuigi Rizzo 	cmd->name[0] = '\0';
31009758b77fSLuigi Rizzo 	cmd->o.len |= F_INSN_SIZE(ipfw_insn_if);
31019758b77fSLuigi Rizzo 
31029758b77fSLuigi Rizzo 	/* Parse the interface or address */
310301750186SBrooks Davis 	if (strcmp(arg, "any") == 0)
31049758b77fSLuigi Rizzo 		cmd->o.len = 0;		/* effectively ignore this command */
31059758b77fSLuigi Rizzo 	else if (!isdigit(*arg)) {
31069bf40edeSBrooks Davis 		strlcpy(cmd->name, arg, sizeof(cmd->name));
31079bf40edeSBrooks Davis 		cmd->p.glob = strpbrk(arg, "*?[") != NULL ? 1 : 0;
31089758b77fSLuigi Rizzo 	} else if (!inet_aton(arg, &cmd->p.ip))
31099758b77fSLuigi Rizzo 		errx(EX_DATAERR, "bad ip address ``%s''", arg);
31109758b77fSLuigi Rizzo }
31119758b77fSLuigi Rizzo 
31129758b77fSLuigi Rizzo static void
31139758b77fSLuigi Rizzo config_pipe(int ac, char **av)
31149758b77fSLuigi Rizzo {
311562ff38aeSLuigi Rizzo 	struct dn_pipe p;
31169758b77fSLuigi Rizzo 	int i;
31179758b77fSLuigi Rizzo 	char *end;
31189758b77fSLuigi Rizzo 	void *par = NULL;
31199758b77fSLuigi Rizzo 
312062ff38aeSLuigi Rizzo 	memset(&p, 0, sizeof p);
31219758b77fSLuigi Rizzo 
31229758b77fSLuigi Rizzo 	av++; ac--;
31239758b77fSLuigi Rizzo 	/* Pipe number */
31249758b77fSLuigi Rizzo 	if (ac && isdigit(**av)) {
31259758b77fSLuigi Rizzo 		i = atoi(*av); av++; ac--;
31269758b77fSLuigi Rizzo 		if (do_pipe == 1)
312762ff38aeSLuigi Rizzo 			p.pipe_nr = i;
31289758b77fSLuigi Rizzo 		else
312962ff38aeSLuigi Rizzo 			p.fs.fs_nr = i;
31309758b77fSLuigi Rizzo 	}
31315e43aef8SLuigi Rizzo 	while (ac > 0) {
31329758b77fSLuigi Rizzo 		double d;
31339758b77fSLuigi Rizzo 		int tok = match_token(dummynet_params, *av);
31349758b77fSLuigi Rizzo 		ac--; av++;
31359758b77fSLuigi Rizzo 
31369758b77fSLuigi Rizzo 		switch(tok) {
313799e5e645SLuigi Rizzo 		case TOK_NOERROR:
313862ff38aeSLuigi Rizzo 			p.fs.flags_fs |= DN_NOERROR;
313999e5e645SLuigi Rizzo 			break;
314099e5e645SLuigi Rizzo 
31419758b77fSLuigi Rizzo 		case TOK_PLR:
31429758b77fSLuigi Rizzo 			NEED1("plr needs argument 0..1\n");
31439758b77fSLuigi Rizzo 			d = strtod(av[0], NULL);
31449758b77fSLuigi Rizzo 			if (d > 1)
31459758b77fSLuigi Rizzo 				d = 1;
31469758b77fSLuigi Rizzo 			else if (d < 0)
31479758b77fSLuigi Rizzo 				d = 0;
314862ff38aeSLuigi Rizzo 			p.fs.plr = (int)(d*0x7fffffff);
31499758b77fSLuigi Rizzo 			ac--; av++;
31509758b77fSLuigi Rizzo 			break;
31519758b77fSLuigi Rizzo 
31529758b77fSLuigi Rizzo 		case TOK_QUEUE:
31539758b77fSLuigi Rizzo 			NEED1("queue needs queue size\n");
31549758b77fSLuigi Rizzo 			end = NULL;
315562ff38aeSLuigi Rizzo 			p.fs.qsize = strtoul(av[0], &end, 0);
31569758b77fSLuigi Rizzo 			if (*end == 'K' || *end == 'k') {
315762ff38aeSLuigi Rizzo 				p.fs.flags_fs |= DN_QSIZE_IS_BYTES;
315862ff38aeSLuigi Rizzo 				p.fs.qsize *= 1024;
315901750186SBrooks Davis 			} else if (*end == 'B' ||
316001750186SBrooks Davis 			    _substrcmp2(end, "by", "bytes") == 0) {
316162ff38aeSLuigi Rizzo 				p.fs.flags_fs |= DN_QSIZE_IS_BYTES;
31629758b77fSLuigi Rizzo 			}
31639758b77fSLuigi Rizzo 			ac--; av++;
31649758b77fSLuigi Rizzo 			break;
31659758b77fSLuigi Rizzo 
31669758b77fSLuigi Rizzo 		case TOK_BUCKETS:
31679758b77fSLuigi Rizzo 			NEED1("buckets needs argument\n");
316862ff38aeSLuigi Rizzo 			p.fs.rq_size = strtoul(av[0], NULL, 0);
31699758b77fSLuigi Rizzo 			ac--; av++;
31709758b77fSLuigi Rizzo 			break;
31719758b77fSLuigi Rizzo 
31729758b77fSLuigi Rizzo 		case TOK_MASK:
31739758b77fSLuigi Rizzo 			NEED1("mask needs mask specifier\n");
31749758b77fSLuigi Rizzo 			/*
31759758b77fSLuigi Rizzo 			 * per-flow queue, mask is dst_ip, dst_port,
31769758b77fSLuigi Rizzo 			 * src_ip, src_port, proto measured in bits
31779758b77fSLuigi Rizzo 			 */
31789758b77fSLuigi Rizzo 			par = NULL;
31799758b77fSLuigi Rizzo 
31808195404bSBrooks Davis 			bzero(&p.fs.flow_mask, sizeof(p.fs.flow_mask));
31819758b77fSLuigi Rizzo 			end = NULL;
31829758b77fSLuigi Rizzo 
31839758b77fSLuigi Rizzo 			while (ac >= 1) {
3184571f8c1bSLuigi Rizzo 			    uint32_t *p32 = NULL;
3185571f8c1bSLuigi Rizzo 			    uint16_t *p16 = NULL;
31868195404bSBrooks Davis 			    uint32_t *p20 = NULL;
31878195404bSBrooks Davis 			    struct in6_addr *pa6 = NULL;
31888195404bSBrooks Davis 			    uint32_t a;
31899758b77fSLuigi Rizzo 
31909758b77fSLuigi Rizzo 			    tok = match_token(dummynet_params, *av);
31919758b77fSLuigi Rizzo 			    ac--; av++;
31929758b77fSLuigi Rizzo 			    switch(tok) {
31939758b77fSLuigi Rizzo 			    case TOK_ALL:
31949758b77fSLuigi Rizzo 				    /*
31959758b77fSLuigi Rizzo 				     * special case, all bits significant
31969758b77fSLuigi Rizzo 				     */
319762ff38aeSLuigi Rizzo 				    p.fs.flow_mask.dst_ip = ~0;
319862ff38aeSLuigi Rizzo 				    p.fs.flow_mask.src_ip = ~0;
319962ff38aeSLuigi Rizzo 				    p.fs.flow_mask.dst_port = ~0;
320062ff38aeSLuigi Rizzo 				    p.fs.flow_mask.src_port = ~0;
320162ff38aeSLuigi Rizzo 				    p.fs.flow_mask.proto = ~0;
32028195404bSBrooks Davis 				    n2mask(&(p.fs.flow_mask.dst_ip6), 128);
32038195404bSBrooks Davis 				    n2mask(&(p.fs.flow_mask.src_ip6), 128);
32048195404bSBrooks Davis 				    p.fs.flow_mask.flow_id6 = ~0;
320562ff38aeSLuigi Rizzo 				    p.fs.flags_fs |= DN_HAVE_FLOW_MASK;
32069758b77fSLuigi Rizzo 				    goto end_mask;
32079758b77fSLuigi Rizzo 
32089758b77fSLuigi Rizzo 			    case TOK_DSTIP:
320962ff38aeSLuigi Rizzo 				    p32 = &p.fs.flow_mask.dst_ip;
32109758b77fSLuigi Rizzo 				    break;
32119758b77fSLuigi Rizzo 
32129758b77fSLuigi Rizzo 			    case TOK_SRCIP:
321362ff38aeSLuigi Rizzo 				    p32 = &p.fs.flow_mask.src_ip;
32149758b77fSLuigi Rizzo 				    break;
32159758b77fSLuigi Rizzo 
32168195404bSBrooks Davis 			    case TOK_DSTIP6:
32178195404bSBrooks Davis 				    pa6 = &(p.fs.flow_mask.dst_ip6);
32188195404bSBrooks Davis 				    break;
32198195404bSBrooks Davis 
32208195404bSBrooks Davis 			    case TOK_SRCIP6:
32218195404bSBrooks Davis 				    pa6 = &(p.fs.flow_mask.src_ip6);
32228195404bSBrooks Davis 				    break;
32238195404bSBrooks Davis 
32248195404bSBrooks Davis 			    case TOK_FLOWID:
32258195404bSBrooks Davis 				    p20 = &p.fs.flow_mask.flow_id6;
32268195404bSBrooks Davis 				    break;
32278195404bSBrooks Davis 
32289758b77fSLuigi Rizzo 			    case TOK_DSTPORT:
322962ff38aeSLuigi Rizzo 				    p16 = &p.fs.flow_mask.dst_port;
32309758b77fSLuigi Rizzo 				    break;
32319758b77fSLuigi Rizzo 
32329758b77fSLuigi Rizzo 			    case TOK_SRCPORT:
323362ff38aeSLuigi Rizzo 				    p16 = &p.fs.flow_mask.src_port;
32349758b77fSLuigi Rizzo 				    break;
32359758b77fSLuigi Rizzo 
32369758b77fSLuigi Rizzo 			    case TOK_PROTO:
32379758b77fSLuigi Rizzo 				    break;
32389758b77fSLuigi Rizzo 
32399758b77fSLuigi Rizzo 			    default:
32409758b77fSLuigi Rizzo 				    ac++; av--; /* backtrack */
32419758b77fSLuigi Rizzo 				    goto end_mask;
32429758b77fSLuigi Rizzo 			    }
32439758b77fSLuigi Rizzo 			    if (ac < 1)
32449758b77fSLuigi Rizzo 				    errx(EX_USAGE, "mask: value missing");
32459758b77fSLuigi Rizzo 			    if (*av[0] == '/') {
32469758b77fSLuigi Rizzo 				    a = strtoul(av[0]+1, &end, 0);
32478195404bSBrooks Davis 				    if (pa6 == NULL)
32489758b77fSLuigi Rizzo 					    a = (a == 32) ? ~0 : (1 << a) - 1;
32499758b77fSLuigi Rizzo 			    } else
32500a7197a8SLuigi Rizzo 				    a = strtoul(av[0], &end, 0);
32519758b77fSLuigi Rizzo 			    if (p32 != NULL)
32529758b77fSLuigi Rizzo 				    *p32 = a;
32539758b77fSLuigi Rizzo 			    else if (p16 != NULL) {
3254610055c9SBrooks Davis 				    if (a > 0xFFFF)
32559758b77fSLuigi Rizzo 					    errx(EX_DATAERR,
3256776c1005SBrooks Davis 						"port mask must be 16 bit");
3257571f8c1bSLuigi Rizzo 				    *p16 = (uint16_t)a;
32588195404bSBrooks Davis 			    } else if (p20 != NULL) {
32598195404bSBrooks Davis 				    if (a > 0xfffff)
32608195404bSBrooks Davis 					errx(EX_DATAERR,
32618195404bSBrooks Davis 					    "flow_id mask must be 20 bit");
32628195404bSBrooks Davis 				    *p20 = (uint32_t)a;
32638195404bSBrooks Davis 			    } else if (pa6 != NULL) {
32648195404bSBrooks Davis 				    if (a < 0 || a > 128)
32658195404bSBrooks Davis 					errx(EX_DATAERR,
32668195404bSBrooks Davis 					    "in6addr invalid mask len");
32678195404bSBrooks Davis 				    else
32688195404bSBrooks Davis 					n2mask(pa6, a);
32699758b77fSLuigi Rizzo 			    } else {
3270610055c9SBrooks Davis 				    if (a > 0xFF)
32719758b77fSLuigi Rizzo 					    errx(EX_DATAERR,
3272776c1005SBrooks Davis 						"proto mask must be 8 bit");
327362ff38aeSLuigi Rizzo 				    p.fs.flow_mask.proto = (uint8_t)a;
32749758b77fSLuigi Rizzo 			    }
32759758b77fSLuigi Rizzo 			    if (a != 0)
327662ff38aeSLuigi Rizzo 				    p.fs.flags_fs |= DN_HAVE_FLOW_MASK;
32779758b77fSLuigi Rizzo 			    ac--; av++;
32789758b77fSLuigi Rizzo 			} /* end while, config masks */
32799758b77fSLuigi Rizzo end_mask:
32809758b77fSLuigi Rizzo 			break;
32819758b77fSLuigi Rizzo 
32829758b77fSLuigi Rizzo 		case TOK_RED:
32839758b77fSLuigi Rizzo 		case TOK_GRED:
32849758b77fSLuigi Rizzo 			NEED1("red/gred needs w_q/min_th/max_th/max_p\n");
328562ff38aeSLuigi Rizzo 			p.fs.flags_fs |= DN_IS_RED;
32869758b77fSLuigi Rizzo 			if (tok == TOK_GRED)
328762ff38aeSLuigi Rizzo 				p.fs.flags_fs |= DN_IS_GENTLE_RED;
32889758b77fSLuigi Rizzo 			/*
32899758b77fSLuigi Rizzo 			 * the format for parameters is w_q/min_th/max_th/max_p
32909758b77fSLuigi Rizzo 			 */
32919758b77fSLuigi Rizzo 			if ((end = strsep(&av[0], "/"))) {
32929758b77fSLuigi Rizzo 			    double w_q = strtod(end, NULL);
32939758b77fSLuigi Rizzo 			    if (w_q > 1 || w_q <= 0)
32949758b77fSLuigi Rizzo 				errx(EX_DATAERR, "0 < w_q <= 1");
329562ff38aeSLuigi Rizzo 			    p.fs.w_q = (int) (w_q * (1 << SCALE_RED));
32969758b77fSLuigi Rizzo 			}
32979758b77fSLuigi Rizzo 			if ((end = strsep(&av[0], "/"))) {
329862ff38aeSLuigi Rizzo 			    p.fs.min_th = strtoul(end, &end, 0);
32999758b77fSLuigi Rizzo 			    if (*end == 'K' || *end == 'k')
330062ff38aeSLuigi Rizzo 				p.fs.min_th *= 1024;
33019758b77fSLuigi Rizzo 			}
33029758b77fSLuigi Rizzo 			if ((end = strsep(&av[0], "/"))) {
330362ff38aeSLuigi Rizzo 			    p.fs.max_th = strtoul(end, &end, 0);
33049758b77fSLuigi Rizzo 			    if (*end == 'K' || *end == 'k')
330562ff38aeSLuigi Rizzo 				p.fs.max_th *= 1024;
33069758b77fSLuigi Rizzo 			}
33079758b77fSLuigi Rizzo 			if ((end = strsep(&av[0], "/"))) {
33089758b77fSLuigi Rizzo 			    double max_p = strtod(end, NULL);
33099758b77fSLuigi Rizzo 			    if (max_p > 1 || max_p <= 0)
33109758b77fSLuigi Rizzo 				errx(EX_DATAERR, "0 < max_p <= 1");
331162ff38aeSLuigi Rizzo 			    p.fs.max_p = (int)(max_p * (1 << SCALE_RED));
33129758b77fSLuigi Rizzo 			}
33139758b77fSLuigi Rizzo 			ac--; av++;
33149758b77fSLuigi Rizzo 			break;
33159758b77fSLuigi Rizzo 
33169758b77fSLuigi Rizzo 		case TOK_DROPTAIL:
331762ff38aeSLuigi Rizzo 			p.fs.flags_fs &= ~(DN_IS_RED|DN_IS_GENTLE_RED);
33189758b77fSLuigi Rizzo 			break;
33199758b77fSLuigi Rizzo 
33209758b77fSLuigi Rizzo 		case TOK_BW:
33219758b77fSLuigi Rizzo 			NEED1("bw needs bandwidth or interface\n");
33229758b77fSLuigi Rizzo 			if (do_pipe != 1)
33239758b77fSLuigi Rizzo 			    errx(EX_DATAERR, "bandwidth only valid for pipes");
33249758b77fSLuigi Rizzo 			/*
33259758b77fSLuigi Rizzo 			 * set clocking interface or bandwidth value
33269758b77fSLuigi Rizzo 			 */
33279758b77fSLuigi Rizzo 			if (av[0][0] >= 'a' && av[0][0] <= 'z') {
332862ff38aeSLuigi Rizzo 			    int l = sizeof(p.if_name)-1;
33299758b77fSLuigi Rizzo 			    /* interface name */
333062ff38aeSLuigi Rizzo 			    strncpy(p.if_name, av[0], l);
333162ff38aeSLuigi Rizzo 			    p.if_name[l] = '\0';
333262ff38aeSLuigi Rizzo 			    p.bandwidth = 0;
33339758b77fSLuigi Rizzo 			} else {
333462ff38aeSLuigi Rizzo 			    p.if_name[0] = '\0';
333562ff38aeSLuigi Rizzo 			    p.bandwidth = strtoul(av[0], &end, 0);
33369758b77fSLuigi Rizzo 			    if (*end == 'K' || *end == 'k') {
33379758b77fSLuigi Rizzo 				end++;
333862ff38aeSLuigi Rizzo 				p.bandwidth *= 1000;
33399758b77fSLuigi Rizzo 			    } else if (*end == 'M') {
33409758b77fSLuigi Rizzo 				end++;
334162ff38aeSLuigi Rizzo 				p.bandwidth *= 1000000;
33429758b77fSLuigi Rizzo 			    }
334301750186SBrooks Davis 			    if (*end == 'B' ||
334401750186SBrooks Davis 			        _substrcmp2(end, "by", "bytes") == 0)
334562ff38aeSLuigi Rizzo 				p.bandwidth *= 8;
334662ff38aeSLuigi Rizzo 			    if (p.bandwidth < 0)
33479758b77fSLuigi Rizzo 				errx(EX_DATAERR, "bandwidth too large");
33489758b77fSLuigi Rizzo 			}
33499758b77fSLuigi Rizzo 			ac--; av++;
33509758b77fSLuigi Rizzo 			break;
33519758b77fSLuigi Rizzo 
33529758b77fSLuigi Rizzo 		case TOK_DELAY:
33539758b77fSLuigi Rizzo 			if (do_pipe != 1)
33549758b77fSLuigi Rizzo 				errx(EX_DATAERR, "delay only valid for pipes");
33559758b77fSLuigi Rizzo 			NEED1("delay needs argument 0..10000ms\n");
335662ff38aeSLuigi Rizzo 			p.delay = strtoul(av[0], NULL, 0);
33579758b77fSLuigi Rizzo 			ac--; av++;
33589758b77fSLuigi Rizzo 			break;
33599758b77fSLuigi Rizzo 
33609758b77fSLuigi Rizzo 		case TOK_WEIGHT:
33619758b77fSLuigi Rizzo 			if (do_pipe == 1)
33629758b77fSLuigi Rizzo 				errx(EX_DATAERR,"weight only valid for queues");
33639758b77fSLuigi Rizzo 			NEED1("weight needs argument 0..100\n");
336462ff38aeSLuigi Rizzo 			p.fs.weight = strtoul(av[0], &end, 0);
33659758b77fSLuigi Rizzo 			ac--; av++;
33669758b77fSLuigi Rizzo 			break;
33679758b77fSLuigi Rizzo 
33689758b77fSLuigi Rizzo 		case TOK_PIPE:
33699758b77fSLuigi Rizzo 			if (do_pipe == 1)
33709758b77fSLuigi Rizzo 				errx(EX_DATAERR,"pipe only valid for queues");
33719758b77fSLuigi Rizzo 			NEED1("pipe needs pipe_number\n");
337262ff38aeSLuigi Rizzo 			p.fs.parent_nr = strtoul(av[0], &end, 0);
33739758b77fSLuigi Rizzo 			ac--; av++;
33749758b77fSLuigi Rizzo 			break;
33759758b77fSLuigi Rizzo 
33769758b77fSLuigi Rizzo 		default:
337766d217f8SMaxim Konovalov 			errx(EX_DATAERR, "unrecognised option ``%s''", av[-1]);
33789758b77fSLuigi Rizzo 		}
33799758b77fSLuigi Rizzo 	}
33809758b77fSLuigi Rizzo 	if (do_pipe == 1) {
338162ff38aeSLuigi Rizzo 		if (p.pipe_nr == 0)
33829758b77fSLuigi Rizzo 			errx(EX_DATAERR, "pipe_nr must be > 0");
338362ff38aeSLuigi Rizzo 		if (p.delay > 10000)
33849758b77fSLuigi Rizzo 			errx(EX_DATAERR, "delay must be < 10000");
33859758b77fSLuigi Rizzo 	} else { /* do_pipe == 2, queue */
338662ff38aeSLuigi Rizzo 		if (p.fs.parent_nr == 0)
33879758b77fSLuigi Rizzo 			errx(EX_DATAERR, "pipe must be > 0");
338862ff38aeSLuigi Rizzo 		if (p.fs.weight >100)
33899758b77fSLuigi Rizzo 			errx(EX_DATAERR, "weight must be <= 100");
33909758b77fSLuigi Rizzo 	}
339162ff38aeSLuigi Rizzo 	if (p.fs.flags_fs & DN_QSIZE_IS_BYTES) {
339262ff38aeSLuigi Rizzo 		if (p.fs.qsize > 1024*1024)
33939758b77fSLuigi Rizzo 			errx(EX_DATAERR, "queue size must be < 1MB");
33949758b77fSLuigi Rizzo 	} else {
339562ff38aeSLuigi Rizzo 		if (p.fs.qsize > 100)
33969758b77fSLuigi Rizzo 			errx(EX_DATAERR, "2 <= queue size <= 100");
33979758b77fSLuigi Rizzo 	}
339862ff38aeSLuigi Rizzo 	if (p.fs.flags_fs & DN_IS_RED) {
33999758b77fSLuigi Rizzo 		size_t len;
34009758b77fSLuigi Rizzo 		int lookup_depth, avg_pkt_size;
34019758b77fSLuigi Rizzo 		double s, idle, weight, w_q;
340262ff38aeSLuigi Rizzo 		struct clockinfo ck;
34039758b77fSLuigi Rizzo 		int t;
34049758b77fSLuigi Rizzo 
340562ff38aeSLuigi Rizzo 		if (p.fs.min_th >= p.fs.max_th)
34069758b77fSLuigi Rizzo 		    errx(EX_DATAERR, "min_th %d must be < than max_th %d",
340762ff38aeSLuigi Rizzo 			p.fs.min_th, p.fs.max_th);
340862ff38aeSLuigi Rizzo 		if (p.fs.max_th == 0)
34099758b77fSLuigi Rizzo 		    errx(EX_DATAERR, "max_th must be > 0");
34109758b77fSLuigi Rizzo 
34119758b77fSLuigi Rizzo 		len = sizeof(int);
34129758b77fSLuigi Rizzo 		if (sysctlbyname("net.inet.ip.dummynet.red_lookup_depth",
34139758b77fSLuigi Rizzo 			&lookup_depth, &len, NULL, 0) == -1)
34149758b77fSLuigi Rizzo 
34159758b77fSLuigi Rizzo 		    errx(1, "sysctlbyname(\"%s\")",
34169758b77fSLuigi Rizzo 			"net.inet.ip.dummynet.red_lookup_depth");
34179758b77fSLuigi Rizzo 		if (lookup_depth == 0)
34189758b77fSLuigi Rizzo 		    errx(EX_DATAERR, "net.inet.ip.dummynet.red_lookup_depth"
34199758b77fSLuigi Rizzo 			" must be greater than zero");
34209758b77fSLuigi Rizzo 
34219758b77fSLuigi Rizzo 		len = sizeof(int);
34229758b77fSLuigi Rizzo 		if (sysctlbyname("net.inet.ip.dummynet.red_avg_pkt_size",
34239758b77fSLuigi Rizzo 			&avg_pkt_size, &len, NULL, 0) == -1)
34249758b77fSLuigi Rizzo 
34259758b77fSLuigi Rizzo 		    errx(1, "sysctlbyname(\"%s\")",
34269758b77fSLuigi Rizzo 			"net.inet.ip.dummynet.red_avg_pkt_size");
34279758b77fSLuigi Rizzo 		if (avg_pkt_size == 0)
34289758b77fSLuigi Rizzo 			errx(EX_DATAERR,
34299758b77fSLuigi Rizzo 			    "net.inet.ip.dummynet.red_avg_pkt_size must"
34309758b77fSLuigi Rizzo 			    " be greater than zero");
34319758b77fSLuigi Rizzo 
34329758b77fSLuigi Rizzo 		len = sizeof(struct clockinfo);
343362ff38aeSLuigi Rizzo 		if (sysctlbyname("kern.clockrate", &ck, &len, NULL, 0) == -1)
34349758b77fSLuigi Rizzo 			errx(1, "sysctlbyname(\"%s\")", "kern.clockrate");
34359758b77fSLuigi Rizzo 
34369758b77fSLuigi Rizzo 		/*
34379758b77fSLuigi Rizzo 		 * Ticks needed for sending a medium-sized packet.
34389758b77fSLuigi Rizzo 		 * Unfortunately, when we are configuring a WF2Q+ queue, we
34399758b77fSLuigi Rizzo 		 * do not have bandwidth information, because that is stored
34409758b77fSLuigi Rizzo 		 * in the parent pipe, and also we have multiple queues
34419758b77fSLuigi Rizzo 		 * competing for it. So we set s=0, which is not very
34429758b77fSLuigi Rizzo 		 * correct. But on the other hand, why do we want RED with
34439758b77fSLuigi Rizzo 		 * WF2Q+ ?
34449758b77fSLuigi Rizzo 		 */
344562ff38aeSLuigi Rizzo 		if (p.bandwidth==0) /* this is a WF2Q+ queue */
34469758b77fSLuigi Rizzo 			s = 0;
34479758b77fSLuigi Rizzo 		else
344862ff38aeSLuigi Rizzo 			s = ck.hz * avg_pkt_size * 8 / p.bandwidth;
34499758b77fSLuigi Rizzo 
34509758b77fSLuigi Rizzo 		/*
34519758b77fSLuigi Rizzo 		 * max idle time (in ticks) before avg queue size becomes 0.
34529758b77fSLuigi Rizzo 		 * NOTA:  (3/w_q) is approx the value x so that
34539758b77fSLuigi Rizzo 		 * (1-w_q)^x < 10^-3.
34549758b77fSLuigi Rizzo 		 */
345562ff38aeSLuigi Rizzo 		w_q = ((double)p.fs.w_q) / (1 << SCALE_RED);
34569758b77fSLuigi Rizzo 		idle = s * 3. / w_q;
345762ff38aeSLuigi Rizzo 		p.fs.lookup_step = (int)idle / lookup_depth;
345862ff38aeSLuigi Rizzo 		if (!p.fs.lookup_step)
345962ff38aeSLuigi Rizzo 			p.fs.lookup_step = 1;
34609758b77fSLuigi Rizzo 		weight = 1 - w_q;
346162ff38aeSLuigi Rizzo 		for (t = p.fs.lookup_step; t > 0; --t)
34629758b77fSLuigi Rizzo 			weight *= weight;
346362ff38aeSLuigi Rizzo 		p.fs.lookup_weight = (int)(weight * (1 << SCALE_RED));
34649758b77fSLuigi Rizzo 	}
346562ff38aeSLuigi Rizzo 	i = do_cmd(IP_DUMMYNET_CONFIGURE, &p, sizeof p);
34669758b77fSLuigi Rizzo 	if (i)
34679758b77fSLuigi Rizzo 		err(1, "setsockopt(%s)", "IP_DUMMYNET_CONFIGURE");
34689758b77fSLuigi Rizzo }
34699758b77fSLuigi Rizzo 
34709758b77fSLuigi Rizzo static void
3471f3a126d3SLuigi Rizzo get_mac_addr_mask(char *p, uint8_t *addr, uint8_t *mask)
34729758b77fSLuigi Rizzo {
34739758b77fSLuigi Rizzo 	int i, l;
34749758b77fSLuigi Rizzo 
34759758b77fSLuigi Rizzo 	for (i=0; i<6; i++)
34769758b77fSLuigi Rizzo 		addr[i] = mask[i] = 0;
347701750186SBrooks Davis 	if (strcmp(p, "any") == 0)
34789758b77fSLuigi Rizzo 		return;
34799758b77fSLuigi Rizzo 
34809758b77fSLuigi Rizzo 	for (i=0; *p && i<6;i++, p++) {
34819758b77fSLuigi Rizzo 		addr[i] = strtol(p, &p, 16);
34829758b77fSLuigi Rizzo 		if (*p != ':') /* we start with the mask */
34839758b77fSLuigi Rizzo 			break;
34849758b77fSLuigi Rizzo 	}
34859758b77fSLuigi Rizzo 	if (*p == '/') { /* mask len */
34869758b77fSLuigi Rizzo 		l = strtol(p+1, &p, 0);
34879758b77fSLuigi Rizzo 		for (i=0; l>0; l -=8, i++)
34889758b77fSLuigi Rizzo 			mask[i] = (l >=8) ? 0xff : (~0) << (8-l);
34899758b77fSLuigi Rizzo 	} else if (*p == '&') { /* mask */
34909758b77fSLuigi Rizzo 		for (i=0, p++; *p && i<6;i++, p++) {
34919758b77fSLuigi Rizzo 			mask[i] = strtol(p, &p, 16);
34929758b77fSLuigi Rizzo 			if (*p != ':')
34939758b77fSLuigi Rizzo 				break;
34949758b77fSLuigi Rizzo 		}
34959758b77fSLuigi Rizzo 	} else if (*p == '\0') {
34969758b77fSLuigi Rizzo 		for (i=0; i<6; i++)
34979758b77fSLuigi Rizzo 			mask[i] = 0xff;
34989758b77fSLuigi Rizzo 	}
34999758b77fSLuigi Rizzo 	for (i=0; i<6; i++)
35009758b77fSLuigi Rizzo 		addr[i] &= mask[i];
35019758b77fSLuigi Rizzo }
35029758b77fSLuigi Rizzo 
35039758b77fSLuigi Rizzo /*
35049758b77fSLuigi Rizzo  * helper function, updates the pointer to cmd with the length
35059758b77fSLuigi Rizzo  * of the current command, and also cleans up the first word of
35069758b77fSLuigi Rizzo  * the new command in case it has been clobbered before.
35079758b77fSLuigi Rizzo  */
35089758b77fSLuigi Rizzo static ipfw_insn *
35099758b77fSLuigi Rizzo next_cmd(ipfw_insn *cmd)
35109758b77fSLuigi Rizzo {
35119758b77fSLuigi Rizzo 	cmd += F_LEN(cmd);
35129758b77fSLuigi Rizzo 	bzero(cmd, sizeof(*cmd));
35139758b77fSLuigi Rizzo 	return cmd;
35149758b77fSLuigi Rizzo }
35159758b77fSLuigi Rizzo 
35169758b77fSLuigi Rizzo /*
351762ff38aeSLuigi Rizzo  * Takes arguments and copies them into a comment
351862ff38aeSLuigi Rizzo  */
351962ff38aeSLuigi Rizzo static void
352062ff38aeSLuigi Rizzo fill_comment(ipfw_insn *cmd, int ac, char **av)
352162ff38aeSLuigi Rizzo {
352262ff38aeSLuigi Rizzo 	int i, l;
352362ff38aeSLuigi Rizzo 	char *p = (char *)(cmd + 1);
352462ff38aeSLuigi Rizzo 
352562ff38aeSLuigi Rizzo 	cmd->opcode = O_NOP;
352662ff38aeSLuigi Rizzo 	cmd->len =  (cmd->len & (F_NOT | F_OR));
352762ff38aeSLuigi Rizzo 
352862ff38aeSLuigi Rizzo 	/* Compute length of comment string. */
352962ff38aeSLuigi Rizzo 	for (i = 0, l = 0; i < ac; i++)
353062ff38aeSLuigi Rizzo 		l += strlen(av[i]) + 1;
353162ff38aeSLuigi Rizzo 	if (l == 0)
353262ff38aeSLuigi Rizzo 		return;
353362ff38aeSLuigi Rizzo 	if (l > 84)
353462ff38aeSLuigi Rizzo 		errx(EX_DATAERR,
353562ff38aeSLuigi Rizzo 		    "comment too long (max 80 chars)");
353662ff38aeSLuigi Rizzo 	l = 1 + (l+3)/4;
353762ff38aeSLuigi Rizzo 	cmd->len =  (cmd->len & (F_NOT | F_OR)) | l;
353862ff38aeSLuigi Rizzo 	for (i = 0; i < ac; i++) {
353962ff38aeSLuigi Rizzo 		strcpy(p, av[i]);
354062ff38aeSLuigi Rizzo 		p += strlen(av[i]);
354162ff38aeSLuigi Rizzo 		*p++ = ' ';
354262ff38aeSLuigi Rizzo 	}
354362ff38aeSLuigi Rizzo 	*(--p) = '\0';
354462ff38aeSLuigi Rizzo }
354562ff38aeSLuigi Rizzo 
354662ff38aeSLuigi Rizzo /*
35479758b77fSLuigi Rizzo  * A function to fill simple commands of size 1.
35489758b77fSLuigi Rizzo  * Existing flags are preserved.
35499758b77fSLuigi Rizzo  */
35509758b77fSLuigi Rizzo static void
3551571f8c1bSLuigi Rizzo fill_cmd(ipfw_insn *cmd, enum ipfw_opcodes opcode, int flags, uint16_t arg)
35529758b77fSLuigi Rizzo {
35539758b77fSLuigi Rizzo 	cmd->opcode = opcode;
35549758b77fSLuigi Rizzo 	cmd->len =  ((cmd->len | flags) & (F_NOT | F_OR)) | 1;
35559758b77fSLuigi Rizzo 	cmd->arg1 = arg;
35569758b77fSLuigi Rizzo }
35579758b77fSLuigi Rizzo 
35589758b77fSLuigi Rizzo /*
35599758b77fSLuigi Rizzo  * Fetch and add the MAC address and type, with masks. This generates one or
35609758b77fSLuigi Rizzo  * two microinstructions, and returns the pointer to the last one.
35619758b77fSLuigi Rizzo  */
35629758b77fSLuigi Rizzo static ipfw_insn *
35639758b77fSLuigi Rizzo add_mac(ipfw_insn *cmd, int ac, char *av[])
35649758b77fSLuigi Rizzo {
3565e706181bSLuigi Rizzo 	ipfw_insn_mac *mac;
35669758b77fSLuigi Rizzo 
356799e5e645SLuigi Rizzo 	if (ac < 2)
35685a155b40SLuigi Rizzo 		errx(EX_DATAERR, "MAC dst src");
35699758b77fSLuigi Rizzo 
35709758b77fSLuigi Rizzo 	cmd->opcode = O_MACADDR2;
35719758b77fSLuigi Rizzo 	cmd->len = (cmd->len & (F_NOT | F_OR)) | F_INSN_SIZE(ipfw_insn_mac);
35729758b77fSLuigi Rizzo 
35739758b77fSLuigi Rizzo 	mac = (ipfw_insn_mac *)cmd;
35749758b77fSLuigi Rizzo 	get_mac_addr_mask(av[0], mac->addr, mac->mask);	/* dst */
35759758b77fSLuigi Rizzo 	get_mac_addr_mask(av[1], &(mac->addr[6]), &(mac->mask[6])); /* src */
3576e706181bSLuigi Rizzo 	return cmd;
35779758b77fSLuigi Rizzo }
35789758b77fSLuigi Rizzo 
3579e706181bSLuigi Rizzo static ipfw_insn *
3580e706181bSLuigi Rizzo add_mactype(ipfw_insn *cmd, int ac, char *av)
3581e706181bSLuigi Rizzo {
3582e706181bSLuigi Rizzo 	if (ac < 1)
3583e706181bSLuigi Rizzo 		errx(EX_DATAERR, "missing MAC type");
3584e706181bSLuigi Rizzo 	if (strcmp(av, "any") != 0) { /* we have a non-null type */
3585e706181bSLuigi Rizzo 		fill_newports((ipfw_insn_u16 *)cmd, av, IPPROTO_ETHERTYPE);
3586e706181bSLuigi Rizzo 		cmd->opcode = O_MAC_TYPE;
35879758b77fSLuigi Rizzo 		return cmd;
3588e706181bSLuigi Rizzo 	} else
3589e706181bSLuigi Rizzo 		return NULL;
3590e706181bSLuigi Rizzo }
3591e706181bSLuigi Rizzo 
3592e706181bSLuigi Rizzo static ipfw_insn *
359336c263ccSHajimu UMEMOTO add_proto0(ipfw_insn *cmd, char *av, u_char *protop)
3594e706181bSLuigi Rizzo {
3595e706181bSLuigi Rizzo 	struct protoent *pe;
359636c263ccSHajimu UMEMOTO 	char *ep;
359736c263ccSHajimu UMEMOTO 	int proto;
35988195404bSBrooks Davis 
359936c263ccSHajimu UMEMOTO 	proto = strtol(av, &ep, 0);
360036c263ccSHajimu UMEMOTO 	if (*ep != '\0' || proto < 0) {
360136c263ccSHajimu UMEMOTO 		if ((pe = getprotobyname(av)) == NULL)
360236c263ccSHajimu UMEMOTO 			return NULL;
360336c263ccSHajimu UMEMOTO 		proto = pe->p_proto;
360436c263ccSHajimu UMEMOTO 	}
360536c263ccSHajimu UMEMOTO 
360636c263ccSHajimu UMEMOTO 	fill_cmd(cmd, O_PROTO, 0, proto);
360736c263ccSHajimu UMEMOTO 	*protop = proto;
360836c263ccSHajimu UMEMOTO 	return cmd;
360936c263ccSHajimu UMEMOTO }
361036c263ccSHajimu UMEMOTO 
361136c263ccSHajimu UMEMOTO static ipfw_insn *
361236c263ccSHajimu UMEMOTO add_proto(ipfw_insn *cmd, char *av, u_char *protop)
361336c263ccSHajimu UMEMOTO {
361436c263ccSHajimu UMEMOTO 	u_char proto = IPPROTO_IP;
3615e706181bSLuigi Rizzo 
361601750186SBrooks Davis 	if (_substrcmp(av, "all") == 0)
361757cd6d26SMax Laier 		; /* do not set O_IP4 nor O_IP6 */
361836c263ccSHajimu UMEMOTO 	else if (strcmp(av, "ip4") == 0)
361936c263ccSHajimu UMEMOTO 		/* explicit "just IPv4" rule */
362036c263ccSHajimu UMEMOTO 		fill_cmd(cmd, O_IP4, 0, 0);
362136c263ccSHajimu UMEMOTO 	else if (strcmp(av, "ip6") == 0) {
362236c263ccSHajimu UMEMOTO 		/* explicit "just IPv6" rule */
362336c263ccSHajimu UMEMOTO 		proto = IPPROTO_IPV6;
362436c263ccSHajimu UMEMOTO 		fill_cmd(cmd, O_IP6, 0, 0);
362536c263ccSHajimu UMEMOTO 	} else
362636c263ccSHajimu UMEMOTO 		return add_proto0(cmd, av, protop);
362736c263ccSHajimu UMEMOTO 
362836c263ccSHajimu UMEMOTO 	*protop = proto;
362936c263ccSHajimu UMEMOTO 	return cmd;
363036c263ccSHajimu UMEMOTO }
363136c263ccSHajimu UMEMOTO 
363236c263ccSHajimu UMEMOTO static ipfw_insn *
363336c263ccSHajimu UMEMOTO add_proto_compat(ipfw_insn *cmd, char *av, u_char *protop)
363436c263ccSHajimu UMEMOTO {
363536c263ccSHajimu UMEMOTO 	u_char proto = IPPROTO_IP;
363636c263ccSHajimu UMEMOTO 
363736c263ccSHajimu UMEMOTO 	if (_substrcmp(av, "all") == 0 || strcmp(av, "ip") == 0)
363836c263ccSHajimu UMEMOTO 		; /* do not set O_IP4 nor O_IP6 */
363957cd6d26SMax Laier 	else if (strcmp(av, "ipv4") == 0 || strcmp(av, "ip4") == 0)
364057cd6d26SMax Laier 		/* explicit "just IPv4" rule */
364157cd6d26SMax Laier 		fill_cmd(cmd, O_IP4, 0, 0);
364257cd6d26SMax Laier 	else if (strcmp(av, "ipv6") == 0 || strcmp(av, "ip6") == 0) {
364357cd6d26SMax Laier 		/* explicit "just IPv6" rule */
364436c263ccSHajimu UMEMOTO 		proto = IPPROTO_IPV6;
364557cd6d26SMax Laier 		fill_cmd(cmd, O_IP6, 0, 0);
364636c263ccSHajimu UMEMOTO 	} else
364736c263ccSHajimu UMEMOTO 		return add_proto0(cmd, av, protop);
36488195404bSBrooks Davis 
364936c263ccSHajimu UMEMOTO 	*protop = proto;
3650e706181bSLuigi Rizzo 	return cmd;
3651e706181bSLuigi Rizzo }
3652e706181bSLuigi Rizzo 
3653e706181bSLuigi Rizzo static ipfw_insn *
3654e706181bSLuigi Rizzo add_srcip(ipfw_insn *cmd, char *av)
3655e706181bSLuigi Rizzo {
3656e706181bSLuigi Rizzo 	fill_ip((ipfw_insn_ip *)cmd, av);
3657e706181bSLuigi Rizzo 	if (cmd->opcode == O_IP_DST_SET)			/* set */
3658e706181bSLuigi Rizzo 		cmd->opcode = O_IP_SRC_SET;
3659cd8b5ae0SRuslan Ermilov 	else if (cmd->opcode == O_IP_DST_LOOKUP)		/* table */
3660cd8b5ae0SRuslan Ermilov 		cmd->opcode = O_IP_SRC_LOOKUP;
3661e706181bSLuigi Rizzo 	else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn))		/* me */
3662e706181bSLuigi Rizzo 		cmd->opcode = O_IP_SRC_ME;
3663e706181bSLuigi Rizzo 	else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn_u32))	/* one IP */
3664e706181bSLuigi Rizzo 		cmd->opcode = O_IP_SRC;
3665571f8c1bSLuigi Rizzo 	else							/* addr/mask */
3666e706181bSLuigi Rizzo 		cmd->opcode = O_IP_SRC_MASK;
3667e706181bSLuigi Rizzo 	return cmd;
3668e706181bSLuigi Rizzo }
3669e706181bSLuigi Rizzo 
3670e706181bSLuigi Rizzo static ipfw_insn *
3671e706181bSLuigi Rizzo add_dstip(ipfw_insn *cmd, char *av)
3672e706181bSLuigi Rizzo {
3673e706181bSLuigi Rizzo 	fill_ip((ipfw_insn_ip *)cmd, av);
3674e706181bSLuigi Rizzo 	if (cmd->opcode == O_IP_DST_SET)			/* set */
3675e706181bSLuigi Rizzo 		;
3676cd8b5ae0SRuslan Ermilov 	else if (cmd->opcode == O_IP_DST_LOOKUP)		/* table */
3677cd8b5ae0SRuslan Ermilov 		;
3678e706181bSLuigi Rizzo 	else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn))		/* me */
3679e706181bSLuigi Rizzo 		cmd->opcode = O_IP_DST_ME;
3680e706181bSLuigi Rizzo 	else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn_u32))	/* one IP */
3681e706181bSLuigi Rizzo 		cmd->opcode = O_IP_DST;
3682571f8c1bSLuigi Rizzo 	else							/* addr/mask */
3683e706181bSLuigi Rizzo 		cmd->opcode = O_IP_DST_MASK;
3684e706181bSLuigi Rizzo 	return cmd;
3685e706181bSLuigi Rizzo }
3686e706181bSLuigi Rizzo 
3687e706181bSLuigi Rizzo static ipfw_insn *
3688e706181bSLuigi Rizzo add_ports(ipfw_insn *cmd, char *av, u_char proto, int opcode)
3689e706181bSLuigi Rizzo {
369001750186SBrooks Davis 	if (_substrcmp(av, "any") == 0) {
3691e706181bSLuigi Rizzo 		return NULL;
3692e706181bSLuigi Rizzo 	} else if (fill_newports((ipfw_insn_u16 *)cmd, av, proto)) {
3693e706181bSLuigi Rizzo 		/* XXX todo: check that we have a protocol with ports */
3694e706181bSLuigi Rizzo 		cmd->opcode = opcode;
3695e706181bSLuigi Rizzo 		return cmd;
3696e706181bSLuigi Rizzo 	}
3697e706181bSLuigi Rizzo 	return NULL;
36989758b77fSLuigi Rizzo }
36999758b77fSLuigi Rizzo 
37008195404bSBrooks Davis static ipfw_insn *
37018195404bSBrooks Davis add_src(ipfw_insn *cmd, char *av, u_char proto)
37028195404bSBrooks Davis {
37038195404bSBrooks Davis 	struct in6_addr a;
37048195404bSBrooks Davis 
37058195404bSBrooks Davis 	if (proto == IPPROTO_IPV6  || strcmp(av, "me6") == 0 ||
37068195404bSBrooks Davis 	    inet_pton(AF_INET6, av, &a))
37078195404bSBrooks Davis 		return add_srcip6(cmd, av);
37088195404bSBrooks Davis 	/* XXX: should check for IPv4, not !IPv6 */
37098195404bSBrooks Davis 	if (proto == IPPROTO_IP || strcmp(av, "me") == 0 ||
37108195404bSBrooks Davis 	    !inet_pton(AF_INET6, av, &a))
37118195404bSBrooks Davis 		return add_srcip(cmd, av);
37128195404bSBrooks Davis 	if (strcmp(av, "any") != 0)
37138195404bSBrooks Davis 		return cmd;
37148195404bSBrooks Davis 
37158195404bSBrooks Davis 	return NULL;
37168195404bSBrooks Davis }
37178195404bSBrooks Davis 
37188195404bSBrooks Davis static ipfw_insn *
37198195404bSBrooks Davis add_dst(ipfw_insn *cmd, char *av, u_char proto)
37208195404bSBrooks Davis {
37218195404bSBrooks Davis 	struct in6_addr a;
37228195404bSBrooks Davis 
37238195404bSBrooks Davis 	if (proto == IPPROTO_IPV6  || strcmp(av, "me6") == 0 ||
37248195404bSBrooks Davis 	    inet_pton(AF_INET6, av, &a))
37258195404bSBrooks Davis 		return add_dstip6(cmd, av);
37268195404bSBrooks Davis 	/* XXX: should check for IPv4, not !IPv6 */
37278195404bSBrooks Davis 	if (proto == IPPROTO_IP || strcmp(av, "me") == 0 ||
37288195404bSBrooks Davis 	    !inet_pton(AF_INET6, av, &a))
37298195404bSBrooks Davis 		return add_dstip(cmd, av);
37308195404bSBrooks Davis 	if (strcmp(av, "any") != 0)
37318195404bSBrooks Davis 		return cmd;
37328195404bSBrooks Davis 
37338195404bSBrooks Davis 	return NULL;
37348195404bSBrooks Davis }
37358195404bSBrooks Davis 
37369758b77fSLuigi Rizzo /*
37379758b77fSLuigi Rizzo  * Parse arguments and assemble the microinstructions which make up a rule.
37389758b77fSLuigi Rizzo  * Rules are added into the 'rulebuf' and then copied in the correct order
37399758b77fSLuigi Rizzo  * into the actual rule.
37409758b77fSLuigi Rizzo  *
3741974dfe30SBrian Feldman  * The syntax for a rule starts with the action, followed by
3742974dfe30SBrian Feldman  * optional action parameters, and the various match patterns.
37439d5abbddSJens Schweikhardt  * In the assembled microcode, the first opcode must be an O_PROBE_STATE
37449758b77fSLuigi Rizzo  * (generated if the rule includes a keep-state option), then the
3745974dfe30SBrian Feldman  * various match patterns, log/altq actions, and the actual action.
37469758b77fSLuigi Rizzo  *
37479758b77fSLuigi Rizzo  */
37489758b77fSLuigi Rizzo static void
37499758b77fSLuigi Rizzo add(int ac, char *av[])
37509758b77fSLuigi Rizzo {
37519758b77fSLuigi Rizzo 	/*
37529758b77fSLuigi Rizzo 	 * rules are added into the 'rulebuf' and then copied in
37539758b77fSLuigi Rizzo 	 * the correct order into the actual rule.
37549758b77fSLuigi Rizzo 	 * Some things that need to go out of order (prob, action etc.)
37559758b77fSLuigi Rizzo 	 * go into actbuf[].
37569758b77fSLuigi Rizzo 	 */
3757571f8c1bSLuigi Rizzo 	static uint32_t rulebuf[255], actbuf[255], cmdbuf[255];
37589758b77fSLuigi Rizzo 
375962ff38aeSLuigi Rizzo 	ipfw_insn *src, *dst, *cmd, *action, *prev=NULL;
3760e706181bSLuigi Rizzo 	ipfw_insn *first_cmd;	/* first match pattern */
37619758b77fSLuigi Rizzo 
37629758b77fSLuigi Rizzo 	struct ip_fw *rule;
37639758b77fSLuigi Rizzo 
37649758b77fSLuigi Rizzo 	/*
37659758b77fSLuigi Rizzo 	 * various flags used to record that we entered some fields.
37669758b77fSLuigi Rizzo 	 */
376752bc23abSLuigi Rizzo 	ipfw_insn *have_state = NULL;	/* check-state or keep-state */
3768974dfe30SBrian Feldman 	ipfw_insn *have_log = NULL, *have_altq = NULL;
37699ec4f2e1SMaxim Konovalov 	size_t len;
37709758b77fSLuigi Rizzo 
37719758b77fSLuigi Rizzo 	int i;
37729758b77fSLuigi Rizzo 
37739758b77fSLuigi Rizzo 	int open_par = 0;	/* open parenthesis ( */
37749758b77fSLuigi Rizzo 
37759758b77fSLuigi Rizzo 	/* proto is here because it is used to fetch ports */
37769758b77fSLuigi Rizzo 	u_char proto = IPPROTO_IP;	/* default protocol */
37779758b77fSLuigi Rizzo 
377812b5dc6aSLuigi Rizzo 	double match_prob = 1; /* match probability, default is always match */
377912b5dc6aSLuigi Rizzo 
37809758b77fSLuigi Rizzo 	bzero(actbuf, sizeof(actbuf));		/* actions go here */
37819758b77fSLuigi Rizzo 	bzero(cmdbuf, sizeof(cmdbuf));
37829758b77fSLuigi Rizzo 	bzero(rulebuf, sizeof(rulebuf));
37839758b77fSLuigi Rizzo 
37849758b77fSLuigi Rizzo 	rule = (struct ip_fw *)rulebuf;
37859758b77fSLuigi Rizzo 	cmd = (ipfw_insn *)cmdbuf;
37869758b77fSLuigi Rizzo 	action = (ipfw_insn *)actbuf;
37879758b77fSLuigi Rizzo 
37889758b77fSLuigi Rizzo 	av++; ac--;
37899758b77fSLuigi Rizzo 
37909758b77fSLuigi Rizzo 	/* [rule N]	-- Rule number optional */
37919758b77fSLuigi Rizzo 	if (ac && isdigit(**av)) {
37929758b77fSLuigi Rizzo 		rule->rulenum = atoi(*av);
37939758b77fSLuigi Rizzo 		av++;
37949758b77fSLuigi Rizzo 		ac--;
37959758b77fSLuigi Rizzo 	}
37969758b77fSLuigi Rizzo 
37973004afcaSLuigi Rizzo 	/* [set N]	-- set number (0..RESVD_SET), optional */
379801750186SBrooks Davis 	if (ac > 1 && _substrcmp(*av, "set") == 0) {
379943405724SLuigi Rizzo 		int set = strtoul(av[1], NULL, 10);
38003004afcaSLuigi Rizzo 		if (set < 0 || set > RESVD_SET)
380143405724SLuigi Rizzo 			errx(EX_DATAERR, "illegal set %s", av[1]);
380243405724SLuigi Rizzo 		rule->set = set;
380343405724SLuigi Rizzo 		av += 2; ac -= 2;
380443405724SLuigi Rizzo 	}
380543405724SLuigi Rizzo 
38069758b77fSLuigi Rizzo 	/* [prob D]	-- match probability, optional */
380701750186SBrooks Davis 	if (ac > 1 && _substrcmp(*av, "prob") == 0) {
380812b5dc6aSLuigi Rizzo 		match_prob = strtod(av[1], NULL);
38099758b77fSLuigi Rizzo 
381012b5dc6aSLuigi Rizzo 		if (match_prob <= 0 || match_prob > 1)
38119758b77fSLuigi Rizzo 			errx(EX_DATAERR, "illegal match prob. %s", av[1]);
38129758b77fSLuigi Rizzo 		av += 2; ac -= 2;
38139758b77fSLuigi Rizzo 	}
38149758b77fSLuigi Rizzo 
38159758b77fSLuigi Rizzo 	/* action	-- mandatory */
38169758b77fSLuigi Rizzo 	NEED1("missing action");
38179758b77fSLuigi Rizzo 	i = match_token(rule_actions, *av);
38189758b77fSLuigi Rizzo 	ac--; av++;
38199758b77fSLuigi Rizzo 	action->len = 1;	/* default */
38209758b77fSLuigi Rizzo 	switch(i) {
38219758b77fSLuigi Rizzo 	case TOK_CHECKSTATE:
382252bc23abSLuigi Rizzo 		have_state = action;
38239758b77fSLuigi Rizzo 		action->opcode = O_CHECK_STATE;
38249758b77fSLuigi Rizzo 		break;
38259758b77fSLuigi Rizzo 
38269758b77fSLuigi Rizzo 	case TOK_ACCEPT:
38279758b77fSLuigi Rizzo 		action->opcode = O_ACCEPT;
38289758b77fSLuigi Rizzo 		break;
38299758b77fSLuigi Rizzo 
38309758b77fSLuigi Rizzo 	case TOK_DENY:
38319758b77fSLuigi Rizzo 		action->opcode = O_DENY;
38325e43aef8SLuigi Rizzo 		action->arg1 = 0;
38335e43aef8SLuigi Rizzo 		break;
38345e43aef8SLuigi Rizzo 
38355e43aef8SLuigi Rizzo 	case TOK_REJECT:
38365e43aef8SLuigi Rizzo 		action->opcode = O_REJECT;
38375e43aef8SLuigi Rizzo 		action->arg1 = ICMP_UNREACH_HOST;
38385e43aef8SLuigi Rizzo 		break;
38395e43aef8SLuigi Rizzo 
38405e43aef8SLuigi Rizzo 	case TOK_RESET:
38415e43aef8SLuigi Rizzo 		action->opcode = O_REJECT;
38425e43aef8SLuigi Rizzo 		action->arg1 = ICMP_REJECT_RST;
38435e43aef8SLuigi Rizzo 		break;
38445e43aef8SLuigi Rizzo 
38459066356bSBjoern A. Zeeb 	case TOK_RESET6:
38469066356bSBjoern A. Zeeb 		action->opcode = O_UNREACH6;
38479066356bSBjoern A. Zeeb 		action->arg1 = ICMP6_UNREACH_RST;
38489066356bSBjoern A. Zeeb 		break;
38499066356bSBjoern A. Zeeb 
38505e43aef8SLuigi Rizzo 	case TOK_UNREACH:
38515e43aef8SLuigi Rizzo 		action->opcode = O_REJECT;
38525e43aef8SLuigi Rizzo 		NEED1("missing reject code");
38535e43aef8SLuigi Rizzo 		fill_reject_code(&action->arg1, *av);
38545e43aef8SLuigi Rizzo 		ac--; av++;
38559758b77fSLuigi Rizzo 		break;
38569758b77fSLuigi Rizzo 
38579066356bSBjoern A. Zeeb 	case TOK_UNREACH6:
38589066356bSBjoern A. Zeeb 		action->opcode = O_UNREACH6;
38599066356bSBjoern A. Zeeb 		NEED1("missing unreach code");
38609066356bSBjoern A. Zeeb 		fill_unreach6_code(&action->arg1, *av);
38619066356bSBjoern A. Zeeb 		ac--; av++;
38629066356bSBjoern A. Zeeb 		break;
38639066356bSBjoern A. Zeeb 
38649758b77fSLuigi Rizzo 	case TOK_COUNT:
38659758b77fSLuigi Rizzo 		action->opcode = O_COUNT;
38669758b77fSLuigi Rizzo 		break;
38679758b77fSLuigi Rizzo 
38689758b77fSLuigi Rizzo 	case TOK_QUEUE:
38699758b77fSLuigi Rizzo 	case TOK_PIPE:
3870b090e4ceSGleb Smirnoff 		action->len = F_INSN_SIZE(ipfw_insn);
38719758b77fSLuigi Rizzo 	case TOK_SKIPTO:
38729758b77fSLuigi Rizzo 		if (i == TOK_QUEUE)
38739758b77fSLuigi Rizzo 			action->opcode = O_QUEUE;
38749758b77fSLuigi Rizzo 		else if (i == TOK_PIPE)
38759758b77fSLuigi Rizzo 			action->opcode = O_PIPE;
38769758b77fSLuigi Rizzo 		else if (i == TOK_SKIPTO)
38779758b77fSLuigi Rizzo 			action->opcode = O_SKIPTO;
38789758b77fSLuigi Rizzo 		NEED1("missing skipto/pipe/queue number");
38799758b77fSLuigi Rizzo 		action->arg1 = strtoul(*av, NULL, 10);
38809758b77fSLuigi Rizzo 		av++; ac--;
38819758b77fSLuigi Rizzo 		break;
38829758b77fSLuigi Rizzo 
38839758b77fSLuigi Rizzo 	case TOK_DIVERT:
38849758b77fSLuigi Rizzo 	case TOK_TEE:
38859758b77fSLuigi Rizzo 		action->opcode = (i == TOK_DIVERT) ? O_DIVERT : O_TEE;
38869758b77fSLuigi Rizzo 		NEED1("missing divert/tee port");
38879758b77fSLuigi Rizzo 		action->arg1 = strtoul(*av, NULL, 0);
38889758b77fSLuigi Rizzo 		if (action->arg1 == 0) {
38899758b77fSLuigi Rizzo 			struct servent *s;
38909758b77fSLuigi Rizzo 			setservent(1);
38919758b77fSLuigi Rizzo 			s = getservbyname(av[0], "divert");
38929758b77fSLuigi Rizzo 			if (s != NULL)
38939758b77fSLuigi Rizzo 				action->arg1 = ntohs(s->s_port);
38949758b77fSLuigi Rizzo 			else
38959758b77fSLuigi Rizzo 				errx(EX_DATAERR, "illegal divert/tee port");
38969758b77fSLuigi Rizzo 		}
38979758b77fSLuigi Rizzo 		ac--; av++;
38989758b77fSLuigi Rizzo 		break;
38999758b77fSLuigi Rizzo 
3900670742a1SGleb Smirnoff 	case TOK_NETGRAPH:
3901670742a1SGleb Smirnoff 	case TOK_NGTEE:
3902670742a1SGleb Smirnoff 		action->opcode = (i == TOK_NETGRAPH ) ? O_NETGRAPH : O_NGTEE;
3903670742a1SGleb Smirnoff 		NEED1("missing netgraph cookie");
3904670742a1SGleb Smirnoff 		action->arg1 = strtoul(*av, NULL, 0);
3905670742a1SGleb Smirnoff 		if (action->arg1 == 0)
3906670742a1SGleb Smirnoff 			errx(EX_DATAERR, "illegal netgraph cookie");
3907670742a1SGleb Smirnoff 		ac--; av++;
3908670742a1SGleb Smirnoff 		break;
3909670742a1SGleb Smirnoff 
39109758b77fSLuigi Rizzo 	case TOK_FORWARD: {
39119758b77fSLuigi Rizzo 		ipfw_insn_sa *p = (ipfw_insn_sa *)action;
39129758b77fSLuigi Rizzo 		char *s, *end;
39139758b77fSLuigi Rizzo 
39149758b77fSLuigi Rizzo 		NEED1("missing forward address[:port]");
39159758b77fSLuigi Rizzo 
39169758b77fSLuigi Rizzo 		action->opcode = O_FORWARD_IP;
39179758b77fSLuigi Rizzo 		action->len = F_INSN_SIZE(ipfw_insn_sa);
39189758b77fSLuigi Rizzo 
39199758b77fSLuigi Rizzo 		p->sa.sin_len = sizeof(struct sockaddr_in);
39209758b77fSLuigi Rizzo 		p->sa.sin_family = AF_INET;
39219758b77fSLuigi Rizzo 		p->sa.sin_port = 0;
39229758b77fSLuigi Rizzo 		/*
39239758b77fSLuigi Rizzo 		 * locate the address-port separator (':' or ',')
39249758b77fSLuigi Rizzo 		 */
39259758b77fSLuigi Rizzo 		s = strchr(*av, ':');
39269758b77fSLuigi Rizzo 		if (s == NULL)
39279758b77fSLuigi Rizzo 			s = strchr(*av, ',');
39289758b77fSLuigi Rizzo 		if (s != NULL) {
39299758b77fSLuigi Rizzo 			*(s++) = '\0';
39309758b77fSLuigi Rizzo 			i = strtoport(s, &end, 0 /* base */, 0 /* proto */);
39319758b77fSLuigi Rizzo 			if (s == end)
39329758b77fSLuigi Rizzo 				errx(EX_DATAERR,
39339758b77fSLuigi Rizzo 				    "illegal forwarding port ``%s''", s);
39344f531a53SLuigi Rizzo 			p->sa.sin_port = (u_short)i;
39359758b77fSLuigi Rizzo 		}
39369758b77fSLuigi Rizzo 		lookup_host(*av, &(p->sa.sin_addr));
39379758b77fSLuigi Rizzo 		}
39389758b77fSLuigi Rizzo 		ac--; av++;
39399758b77fSLuigi Rizzo 		break;
39409758b77fSLuigi Rizzo 
394162ff38aeSLuigi Rizzo 	case TOK_COMMENT:
394262ff38aeSLuigi Rizzo 		/* pretend it is a 'count' rule followed by the comment */
394362ff38aeSLuigi Rizzo 		action->opcode = O_COUNT;
394462ff38aeSLuigi Rizzo 		ac++; av--;	/* go back... */
394562ff38aeSLuigi Rizzo 		break;
394662ff38aeSLuigi Rizzo 
39479758b77fSLuigi Rizzo 	default:
3948e706181bSLuigi Rizzo 		errx(EX_DATAERR, "invalid action %s\n", av[-1]);
39499758b77fSLuigi Rizzo 	}
39509758b77fSLuigi Rizzo 	action = next_cmd(action);
39519758b77fSLuigi Rizzo 
39529758b77fSLuigi Rizzo 	/*
3953974dfe30SBrian Feldman 	 * [altq queuename] -- altq tag, optional
39549758b77fSLuigi Rizzo 	 * [log [logamount N]]	-- log, optional
39559758b77fSLuigi Rizzo 	 *
3956974dfe30SBrian Feldman 	 * If they exist, it go first in the cmdbuf, but then it is
39579758b77fSLuigi Rizzo 	 * skipped in the copy section to the end of the buffer.
39589758b77fSLuigi Rizzo 	 */
3959974dfe30SBrian Feldman 	while (ac != 0 && (i = match_token(rule_action_params, *av)) != -1) {
3960974dfe30SBrian Feldman 		ac--; av++;
3961974dfe30SBrian Feldman 		switch (i) {
3962974dfe30SBrian Feldman 		case TOK_LOG:
3963974dfe30SBrian Feldman 		    {
39649758b77fSLuigi Rizzo 			ipfw_insn_log *c = (ipfw_insn_log *)cmd;
396562ff38aeSLuigi Rizzo 			int l;
39669758b77fSLuigi Rizzo 
3967974dfe30SBrian Feldman 			if (have_log)
3968974dfe30SBrian Feldman 				errx(EX_DATAERR,
3969974dfe30SBrian Feldman 				    "log cannot be specified more than once");
3970974dfe30SBrian Feldman 			have_log = (ipfw_insn *)c;
39719758b77fSLuigi Rizzo 			cmd->len = F_INSN_SIZE(ipfw_insn_log);
39729758b77fSLuigi Rizzo 			cmd->opcode = O_LOG;
397301750186SBrooks Davis 			if (ac && _substrcmp(*av, "logamount") == 0) {
39749758b77fSLuigi Rizzo 				ac--; av++;
39759758b77fSLuigi Rizzo 				NEED1("logamount requires argument");
397662ff38aeSLuigi Rizzo 				l = atoi(*av);
397762ff38aeSLuigi Rizzo 				if (l < 0)
3978974dfe30SBrian Feldman 					errx(EX_DATAERR,
3979974dfe30SBrian Feldman 					    "logamount must be positive");
398062ff38aeSLuigi Rizzo 				c->max_log = l;
39819758b77fSLuigi Rizzo 				ac--; av++;
39829ec4f2e1SMaxim Konovalov 			} else {
39839ec4f2e1SMaxim Konovalov 				len = sizeof(c->max_log);
39849ec4f2e1SMaxim Konovalov 				if (sysctlbyname("net.inet.ip.fw.verbose_limit",
39859ec4f2e1SMaxim Konovalov 				    &c->max_log, &len, NULL, 0) == -1)
39869ec4f2e1SMaxim Konovalov 					errx(1, "sysctlbyname(\"%s\")",
39879ec4f2e1SMaxim Konovalov 					    "net.inet.ip.fw.verbose_limit");
39889758b77fSLuigi Rizzo 			}
3989974dfe30SBrian Feldman 		    }
3990974dfe30SBrian Feldman 			break;
3991974dfe30SBrian Feldman 
3992974dfe30SBrian Feldman 		case TOK_ALTQ:
3993974dfe30SBrian Feldman 		    {
3994974dfe30SBrian Feldman 			ipfw_insn_altq *a = (ipfw_insn_altq *)cmd;
3995974dfe30SBrian Feldman 
3996974dfe30SBrian Feldman 			NEED1("missing altq queue name");
3997974dfe30SBrian Feldman 			if (have_altq)
3998974dfe30SBrian Feldman 				errx(EX_DATAERR,
3999974dfe30SBrian Feldman 				    "altq cannot be specified more than once");
4000974dfe30SBrian Feldman 			have_altq = (ipfw_insn *)a;
4001974dfe30SBrian Feldman 			cmd->len = F_INSN_SIZE(ipfw_insn_altq);
4002974dfe30SBrian Feldman 			cmd->opcode = O_ALTQ;
4003974dfe30SBrian Feldman 			fill_altq_qid(&a->qid, *av);
4004974dfe30SBrian Feldman 			ac--; av++;
4005974dfe30SBrian Feldman 		    }
4006974dfe30SBrian Feldman 			break;
4007974dfe30SBrian Feldman 
4008974dfe30SBrian Feldman 		default:
4009974dfe30SBrian Feldman 			abort();
4010974dfe30SBrian Feldman 		}
40119758b77fSLuigi Rizzo 		cmd = next_cmd(cmd);
40129758b77fSLuigi Rizzo 	}
40139758b77fSLuigi Rizzo 
401452bc23abSLuigi Rizzo 	if (have_state)	/* must be a check-state, we are done */
40159758b77fSLuigi Rizzo 		goto done;
40169758b77fSLuigi Rizzo 
40179758b77fSLuigi Rizzo #define OR_START(target)					\
40189758b77fSLuigi Rizzo 	if (ac && (*av[0] == '(' || *av[0] == '{')) {		\
40199758b77fSLuigi Rizzo 		if (open_par)					\
40209758b77fSLuigi Rizzo 			errx(EX_USAGE, "nested \"(\" not allowed\n"); \
40218ed2d749SLuigi Rizzo 		prev = NULL;					\
40229758b77fSLuigi Rizzo 		open_par = 1;					\
40239758b77fSLuigi Rizzo 		if ( (av[0])[1] == '\0') {			\
40249758b77fSLuigi Rizzo 			ac--; av++;				\
40259758b77fSLuigi Rizzo 		} else						\
40269758b77fSLuigi Rizzo 			(*av)++;				\
40279758b77fSLuigi Rizzo 	}							\
40289758b77fSLuigi Rizzo 	target:							\
40299758b77fSLuigi Rizzo 
40309758b77fSLuigi Rizzo 
40319758b77fSLuigi Rizzo #define	CLOSE_PAR						\
40329758b77fSLuigi Rizzo 	if (open_par) {						\
40339758b77fSLuigi Rizzo 		if (ac && (					\
403401750186SBrooks Davis 		    strcmp(*av, ")") == 0 ||			\
403501750186SBrooks Davis 		    strcmp(*av, "}") == 0)) {			\
40368ed2d749SLuigi Rizzo 			prev = NULL;				\
40379758b77fSLuigi Rizzo 			open_par = 0;				\
40389758b77fSLuigi Rizzo 			ac--; av++;				\
40399758b77fSLuigi Rizzo 		} else						\
40409758b77fSLuigi Rizzo 			errx(EX_USAGE, "missing \")\"\n");	\
40419758b77fSLuigi Rizzo 	}
40429758b77fSLuigi Rizzo 
40439758b77fSLuigi Rizzo #define NOT_BLOCK						\
404401750186SBrooks Davis 	if (ac && _substrcmp(*av, "not") == 0) {		\
40459758b77fSLuigi Rizzo 		if (cmd->len & F_NOT)				\
40469758b77fSLuigi Rizzo 			errx(EX_USAGE, "double \"not\" not allowed\n"); \
40479758b77fSLuigi Rizzo 		cmd->len |= F_NOT;				\
40489758b77fSLuigi Rizzo 		ac--; av++;					\
40499758b77fSLuigi Rizzo 	}
40509758b77fSLuigi Rizzo 
40519758b77fSLuigi Rizzo #define OR_BLOCK(target)					\
405201750186SBrooks Davis 	if (ac && _substrcmp(*av, "or") == 0) {		\
40539758b77fSLuigi Rizzo 		if (prev == NULL || open_par == 0)		\
40549758b77fSLuigi Rizzo 			errx(EX_DATAERR, "invalid OR block");	\
40559758b77fSLuigi Rizzo 		prev->len |= F_OR;				\
40569758b77fSLuigi Rizzo 		ac--; av++;					\
40579758b77fSLuigi Rizzo 		goto target;					\
40589758b77fSLuigi Rizzo 	}							\
40599758b77fSLuigi Rizzo 	CLOSE_PAR;
40609758b77fSLuigi Rizzo 
4061e706181bSLuigi Rizzo 	first_cmd = cmd;
40625a155b40SLuigi Rizzo 
40635a155b40SLuigi Rizzo #if 0
4064e706181bSLuigi Rizzo 	/*
4065e706181bSLuigi Rizzo 	 * MAC addresses, optional.
4066e706181bSLuigi Rizzo 	 * If we have this, we skip the part "proto from src to dst"
4067e706181bSLuigi Rizzo 	 * and jump straight to the option parsing.
4068e706181bSLuigi Rizzo 	 */
4069e706181bSLuigi Rizzo 	NOT_BLOCK;
4070e706181bSLuigi Rizzo 	NEED1("missing protocol");
407101750186SBrooks Davis 	if (_substrcmp(*av, "MAC") == 0 ||
407201750186SBrooks Davis 	    _substrcmp(*av, "mac") == 0) {
4073e706181bSLuigi Rizzo 		ac--; av++;	/* the "MAC" keyword */
4074e706181bSLuigi Rizzo 		add_mac(cmd, ac, av); /* exits in case of errors */
4075e706181bSLuigi Rizzo 		cmd = next_cmd(cmd);
4076e706181bSLuigi Rizzo 		ac -= 2; av += 2;	/* dst-mac and src-mac */
4077e706181bSLuigi Rizzo 		NOT_BLOCK;
4078e706181bSLuigi Rizzo 		NEED1("missing mac type");
4079e706181bSLuigi Rizzo 		if (add_mactype(cmd, ac, av[0]))
4080e706181bSLuigi Rizzo 			cmd = next_cmd(cmd);
4081e706181bSLuigi Rizzo 		ac--; av++;	/* any or mac-type */
4082e706181bSLuigi Rizzo 		goto read_options;
4083e706181bSLuigi Rizzo 	}
40845a155b40SLuigi Rizzo #endif
4085e706181bSLuigi Rizzo 
40869758b77fSLuigi Rizzo 	/*
40879758b77fSLuigi Rizzo 	 * protocol, mandatory
40889758b77fSLuigi Rizzo 	 */
40899758b77fSLuigi Rizzo     OR_START(get_proto);
40909758b77fSLuigi Rizzo 	NOT_BLOCK;
40919758b77fSLuigi Rizzo 	NEED1("missing protocol");
409236c263ccSHajimu UMEMOTO 	if (add_proto_compat(cmd, *av, &proto)) {
40939758b77fSLuigi Rizzo 		av++; ac--;
4094b730879fSMax Laier 		if (F_LEN(cmd) != 0) {
4095e706181bSLuigi Rizzo 			prev = cmd;
40969758b77fSLuigi Rizzo 			cmd = next_cmd(cmd);
4097e706181bSLuigi Rizzo 		}
40985a155b40SLuigi Rizzo 	} else if (first_cmd != cmd) {
4099c82b8dceSMaxim Konovalov 		errx(EX_DATAERR, "invalid protocol ``%s''", *av);
41005a155b40SLuigi Rizzo 	} else
41015a155b40SLuigi Rizzo 		goto read_options;
41029758b77fSLuigi Rizzo     OR_BLOCK(get_proto);
41039758b77fSLuigi Rizzo 
41049758b77fSLuigi Rizzo 	/*
4105e706181bSLuigi Rizzo 	 * "from", mandatory
41069758b77fSLuigi Rizzo 	 */
410701750186SBrooks Davis 	if (!ac || _substrcmp(*av, "from") != 0)
41089758b77fSLuigi Rizzo 		errx(EX_USAGE, "missing ``from''");
41099758b77fSLuigi Rizzo 	ac--; av++;
41109758b77fSLuigi Rizzo 
41119758b77fSLuigi Rizzo 	/*
41129758b77fSLuigi Rizzo 	 * source IP, mandatory
41139758b77fSLuigi Rizzo 	 */
41149758b77fSLuigi Rizzo     OR_START(source_ip);
41159758b77fSLuigi Rizzo 	NOT_BLOCK;	/* optional "not" */
41169758b77fSLuigi Rizzo 	NEED1("missing source address");
41178195404bSBrooks Davis 	if (add_src(cmd, *av, proto)) {
41189758b77fSLuigi Rizzo 		ac--; av++;
4119e706181bSLuigi Rizzo 		if (F_LEN(cmd) != 0) {	/* ! any */
4120e706181bSLuigi Rizzo 			prev = cmd;
41219758b77fSLuigi Rizzo 			cmd = next_cmd(cmd);
4122e706181bSLuigi Rizzo 		}
41238195404bSBrooks Davis 	} else
41248195404bSBrooks Davis 		errx(EX_USAGE, "bad source address %s", *av);
41259758b77fSLuigi Rizzo     OR_BLOCK(source_ip);
41269758b77fSLuigi Rizzo 
41279758b77fSLuigi Rizzo 	/*
41289758b77fSLuigi Rizzo 	 * source ports, optional
41299758b77fSLuigi Rizzo 	 */
41309758b77fSLuigi Rizzo 	NOT_BLOCK;	/* optional "not" */
41318ed2d749SLuigi Rizzo 	if (ac) {
413201750186SBrooks Davis 		if (_substrcmp(*av, "any") == 0 ||
4133e706181bSLuigi Rizzo 		    add_ports(cmd, *av, proto, O_IP_SRCPORT)) {
4134e706181bSLuigi Rizzo 			ac--; av++;
4135e706181bSLuigi Rizzo 			if (F_LEN(cmd) != 0)
41369758b77fSLuigi Rizzo 				cmd = next_cmd(cmd);
41379758b77fSLuigi Rizzo 		}
41388ed2d749SLuigi Rizzo 	}
41399758b77fSLuigi Rizzo 
41409758b77fSLuigi Rizzo 	/*
4141e706181bSLuigi Rizzo 	 * "to", mandatory
41429758b77fSLuigi Rizzo 	 */
414301750186SBrooks Davis 	if (!ac || _substrcmp(*av, "to") != 0)
41449758b77fSLuigi Rizzo 		errx(EX_USAGE, "missing ``to''");
41459758b77fSLuigi Rizzo 	av++; ac--;
41469758b77fSLuigi Rizzo 
41479758b77fSLuigi Rizzo 	/*
41489758b77fSLuigi Rizzo 	 * destination, mandatory
41499758b77fSLuigi Rizzo 	 */
41509758b77fSLuigi Rizzo     OR_START(dest_ip);
41519758b77fSLuigi Rizzo 	NOT_BLOCK;	/* optional "not" */
41529758b77fSLuigi Rizzo 	NEED1("missing dst address");
41538195404bSBrooks Davis 	if (add_dst(cmd, *av, proto)) {
4154e706181bSLuigi Rizzo 		ac--; av++;
4155e706181bSLuigi Rizzo 		if (F_LEN(cmd) != 0) {	/* ! any */
41569758b77fSLuigi Rizzo 			prev = cmd;
41579758b77fSLuigi Rizzo 			cmd = next_cmd(cmd);
4158e706181bSLuigi Rizzo 		}
41598195404bSBrooks Davis 	} else
41608195404bSBrooks Davis 		errx( EX_USAGE, "bad destination address %s", *av);
41619758b77fSLuigi Rizzo     OR_BLOCK(dest_ip);
41629758b77fSLuigi Rizzo 
41639758b77fSLuigi Rizzo 	/*
41649758b77fSLuigi Rizzo 	 * dest. ports, optional
41659758b77fSLuigi Rizzo 	 */
41669758b77fSLuigi Rizzo 	NOT_BLOCK;	/* optional "not" */
41678ed2d749SLuigi Rizzo 	if (ac) {
416801750186SBrooks Davis 		if (_substrcmp(*av, "any") == 0 ||
4169e706181bSLuigi Rizzo 		    add_ports(cmd, *av, proto, O_IP_DSTPORT)) {
4170e706181bSLuigi Rizzo 			ac--; av++;
4171e706181bSLuigi Rizzo 			if (F_LEN(cmd) != 0)
4172e706181bSLuigi Rizzo 				cmd = next_cmd(cmd);
41739758b77fSLuigi Rizzo 		}
41748ed2d749SLuigi Rizzo 	}
41759758b77fSLuigi Rizzo 
41769758b77fSLuigi Rizzo read_options:
4177e706181bSLuigi Rizzo 	if (ac && first_cmd == cmd) {
4178e706181bSLuigi Rizzo 		/*
4179e706181bSLuigi Rizzo 		 * nothing specified so far, store in the rule to ease
4180e706181bSLuigi Rizzo 		 * printout later.
4181e706181bSLuigi Rizzo 		 */
4182e706181bSLuigi Rizzo 		 rule->_pad = 1;
4183e706181bSLuigi Rizzo 	}
41849758b77fSLuigi Rizzo 	prev = NULL;
41859758b77fSLuigi Rizzo 	while (ac) {
41868ed2d749SLuigi Rizzo 		char *s;
41878ed2d749SLuigi Rizzo 		ipfw_insn_u32 *cmd32;	/* alias for cmd */
41888ed2d749SLuigi Rizzo 
41898ed2d749SLuigi Rizzo 		s = *av;
41908ed2d749SLuigi Rizzo 		cmd32 = (ipfw_insn_u32 *)cmd;
41919758b77fSLuigi Rizzo 
41929758b77fSLuigi Rizzo 		if (*s == '!') {	/* alternate syntax for NOT */
41939758b77fSLuigi Rizzo 			if (cmd->len & F_NOT)
41949758b77fSLuigi Rizzo 				errx(EX_USAGE, "double \"not\" not allowed\n");
41959758b77fSLuigi Rizzo 			cmd->len = F_NOT;
41969758b77fSLuigi Rizzo 			s++;
41979758b77fSLuigi Rizzo 		}
41989758b77fSLuigi Rizzo 		i = match_token(rule_options, s);
41999758b77fSLuigi Rizzo 		ac--; av++;
42009758b77fSLuigi Rizzo 		switch(i) {
42019758b77fSLuigi Rizzo 		case TOK_NOT:
42029758b77fSLuigi Rizzo 			if (cmd->len & F_NOT)
42039758b77fSLuigi Rizzo 				errx(EX_USAGE, "double \"not\" not allowed\n");
42049758b77fSLuigi Rizzo 			cmd->len = F_NOT;
42059758b77fSLuigi Rizzo 			break;
42069758b77fSLuigi Rizzo 
42079758b77fSLuigi Rizzo 		case TOK_OR:
42088ed2d749SLuigi Rizzo 			if (open_par == 0 || prev == NULL)
42099758b77fSLuigi Rizzo 				errx(EX_USAGE, "invalid \"or\" block\n");
42109758b77fSLuigi Rizzo 			prev->len |= F_OR;
42119758b77fSLuigi Rizzo 			break;
42129758b77fSLuigi Rizzo 
42138ed2d749SLuigi Rizzo 		case TOK_STARTBRACE:
42148ed2d749SLuigi Rizzo 			if (open_par)
42158ed2d749SLuigi Rizzo 				errx(EX_USAGE, "+nested \"(\" not allowed\n");
42168ed2d749SLuigi Rizzo 			open_par = 1;
42178ed2d749SLuigi Rizzo 			break;
42188ed2d749SLuigi Rizzo 
42198ed2d749SLuigi Rizzo 		case TOK_ENDBRACE:
42208ed2d749SLuigi Rizzo 			if (!open_par)
42218ed2d749SLuigi Rizzo 				errx(EX_USAGE, "+missing \")\"\n");
42228ed2d749SLuigi Rizzo 			open_par = 0;
4223e706181bSLuigi Rizzo 			prev = NULL;
42248ed2d749SLuigi Rizzo         		break;
42258ed2d749SLuigi Rizzo 
42269758b77fSLuigi Rizzo 		case TOK_IN:
42279758b77fSLuigi Rizzo 			fill_cmd(cmd, O_IN, 0, 0);
42289758b77fSLuigi Rizzo 			break;
42299758b77fSLuigi Rizzo 
42309758b77fSLuigi Rizzo 		case TOK_OUT:
42319758b77fSLuigi Rizzo 			cmd->len ^= F_NOT; /* toggle F_NOT */
42329758b77fSLuigi Rizzo 			fill_cmd(cmd, O_IN, 0, 0);
42339758b77fSLuigi Rizzo 			break;
42349758b77fSLuigi Rizzo 
42356daf7ebdSBrian Feldman 		case TOK_DIVERTED:
42366daf7ebdSBrian Feldman 			fill_cmd(cmd, O_DIVERTED, 0, 3);
42376daf7ebdSBrian Feldman 			break;
42386daf7ebdSBrian Feldman 
42396daf7ebdSBrian Feldman 		case TOK_DIVERTEDLOOPBACK:
42406daf7ebdSBrian Feldman 			fill_cmd(cmd, O_DIVERTED, 0, 1);
42416daf7ebdSBrian Feldman 			break;
42426daf7ebdSBrian Feldman 
42436daf7ebdSBrian Feldman 		case TOK_DIVERTEDOUTPUT:
42446daf7ebdSBrian Feldman 			fill_cmd(cmd, O_DIVERTED, 0, 2);
42456daf7ebdSBrian Feldman 			break;
42466daf7ebdSBrian Feldman 
42479758b77fSLuigi Rizzo 		case TOK_FRAG:
42489758b77fSLuigi Rizzo 			fill_cmd(cmd, O_FRAG, 0, 0);
42499758b77fSLuigi Rizzo 			break;
42509758b77fSLuigi Rizzo 
42519758b77fSLuigi Rizzo 		case TOK_LAYER2:
42529758b77fSLuigi Rizzo 			fill_cmd(cmd, O_LAYER2, 0, 0);
42539758b77fSLuigi Rizzo 			break;
42549758b77fSLuigi Rizzo 
42559758b77fSLuigi Rizzo 		case TOK_XMIT:
42569758b77fSLuigi Rizzo 		case TOK_RECV:
42579758b77fSLuigi Rizzo 		case TOK_VIA:
42589758b77fSLuigi Rizzo 			NEED1("recv, xmit, via require interface name"
42599758b77fSLuigi Rizzo 				" or address");
42609758b77fSLuigi Rizzo 			fill_iface((ipfw_insn_if *)cmd, av[0]);
42619758b77fSLuigi Rizzo 			ac--; av++;
42629758b77fSLuigi Rizzo 			if (F_LEN(cmd) == 0)	/* not a valid address */
42639758b77fSLuigi Rizzo 				break;
42649758b77fSLuigi Rizzo 			if (i == TOK_XMIT)
42659758b77fSLuigi Rizzo 				cmd->opcode = O_XMIT;
42669758b77fSLuigi Rizzo 			else if (i == TOK_RECV)
42679758b77fSLuigi Rizzo 				cmd->opcode = O_RECV;
42689758b77fSLuigi Rizzo 			else if (i == TOK_VIA)
42699758b77fSLuigi Rizzo 				cmd->opcode = O_VIA;
42709758b77fSLuigi Rizzo 			break;
42719758b77fSLuigi Rizzo 
42725e43aef8SLuigi Rizzo 		case TOK_ICMPTYPES:
42735e43aef8SLuigi Rizzo 			NEED1("icmptypes requires list of types");
42745e43aef8SLuigi Rizzo 			fill_icmptypes((ipfw_insn_u32 *)cmd, *av);
42755e43aef8SLuigi Rizzo 			av++; ac--;
42765e43aef8SLuigi Rizzo 			break;
42775e43aef8SLuigi Rizzo 
42788195404bSBrooks Davis 		case TOK_ICMP6TYPES:
42798195404bSBrooks Davis 			NEED1("icmptypes requires list of types");
42808195404bSBrooks Davis 			fill_icmp6types((ipfw_insn_icmp6 *)cmd, *av);
42818195404bSBrooks Davis 			av++; ac--;
42828195404bSBrooks Davis 			break;
42838195404bSBrooks Davis 
42849758b77fSLuigi Rizzo 		case TOK_IPTTL:
42859758b77fSLuigi Rizzo 			NEED1("ipttl requires TTL");
428644c884e1SLuigi Rizzo 			if (strpbrk(*av, "-,")) {
428744c884e1SLuigi Rizzo 			    if (!add_ports(cmd, *av, 0, O_IPTTL))
428844c884e1SLuigi Rizzo 				errx(EX_DATAERR, "invalid ipttl %s", *av);
428944c884e1SLuigi Rizzo 			} else
42909758b77fSLuigi Rizzo 			    fill_cmd(cmd, O_IPTTL, 0, strtoul(*av, NULL, 0));
42919758b77fSLuigi Rizzo 			ac--; av++;
42929758b77fSLuigi Rizzo 			break;
42939758b77fSLuigi Rizzo 
42949758b77fSLuigi Rizzo 		case TOK_IPID:
429544c884e1SLuigi Rizzo 			NEED1("ipid requires id");
429644c884e1SLuigi Rizzo 			if (strpbrk(*av, "-,")) {
429744c884e1SLuigi Rizzo 			    if (!add_ports(cmd, *av, 0, O_IPID))
429844c884e1SLuigi Rizzo 				errx(EX_DATAERR, "invalid ipid %s", *av);
429944c884e1SLuigi Rizzo 			} else
43009758b77fSLuigi Rizzo 			    fill_cmd(cmd, O_IPID, 0, strtoul(*av, NULL, 0));
43019758b77fSLuigi Rizzo 			ac--; av++;
43029758b77fSLuigi Rizzo 			break;
43039758b77fSLuigi Rizzo 
43049758b77fSLuigi Rizzo 		case TOK_IPLEN:
43059758b77fSLuigi Rizzo 			NEED1("iplen requires length");
430644c884e1SLuigi Rizzo 			if (strpbrk(*av, "-,")) {
430744c884e1SLuigi Rizzo 			    if (!add_ports(cmd, *av, 0, O_IPLEN))
430844c884e1SLuigi Rizzo 				errx(EX_DATAERR, "invalid ip len %s", *av);
430944c884e1SLuigi Rizzo 			} else
43109758b77fSLuigi Rizzo 			    fill_cmd(cmd, O_IPLEN, 0, strtoul(*av, NULL, 0));
43119758b77fSLuigi Rizzo 			ac--; av++;
43129758b77fSLuigi Rizzo 			break;
43139758b77fSLuigi Rizzo 
43149758b77fSLuigi Rizzo 		case TOK_IPVER:
43159758b77fSLuigi Rizzo 			NEED1("ipver requires version");
43169758b77fSLuigi Rizzo 			fill_cmd(cmd, O_IPVER, 0, strtoul(*av, NULL, 0));
43179758b77fSLuigi Rizzo 			ac--; av++;
43189758b77fSLuigi Rizzo 			break;
43199758b77fSLuigi Rizzo 
43205e43aef8SLuigi Rizzo 		case TOK_IPPRECEDENCE:
43215e43aef8SLuigi Rizzo 			NEED1("ipprecedence requires value");
43225e43aef8SLuigi Rizzo 			fill_cmd(cmd, O_IPPRECEDENCE, 0,
43235e43aef8SLuigi Rizzo 			    (strtoul(*av, NULL, 0) & 7) << 5);
43245e43aef8SLuigi Rizzo 			ac--; av++;
43255e43aef8SLuigi Rizzo 			break;
43265e43aef8SLuigi Rizzo 
43279758b77fSLuigi Rizzo 		case TOK_IPOPTS:
43289758b77fSLuigi Rizzo 			NEED1("missing argument for ipoptions");
432952bc23abSLuigi Rizzo 			fill_flags(cmd, O_IPOPT, f_ipopts, *av);
43309758b77fSLuigi Rizzo 			ac--; av++;
43319758b77fSLuigi Rizzo 			break;
43329758b77fSLuigi Rizzo 
43335e43aef8SLuigi Rizzo 		case TOK_IPTOS:
43345e43aef8SLuigi Rizzo 			NEED1("missing argument for iptos");
433552bc23abSLuigi Rizzo 			fill_flags(cmd, O_IPTOS, f_iptos, *av);
43365e43aef8SLuigi Rizzo 			ac--; av++;
43375e43aef8SLuigi Rizzo 			break;
43385e43aef8SLuigi Rizzo 
43399758b77fSLuigi Rizzo 		case TOK_UID:
43409758b77fSLuigi Rizzo 			NEED1("uid requires argument");
43419758b77fSLuigi Rizzo 		    {
43429758b77fSLuigi Rizzo 			char *end;
43439758b77fSLuigi Rizzo 			uid_t uid;
43449758b77fSLuigi Rizzo 			struct passwd *pwd;
43459758b77fSLuigi Rizzo 
43469758b77fSLuigi Rizzo 			cmd->opcode = O_UID;
43479758b77fSLuigi Rizzo 			uid = strtoul(*av, &end, 0);
43489758b77fSLuigi Rizzo 			pwd = (*end == '\0') ? getpwuid(uid) : getpwnam(*av);
43499758b77fSLuigi Rizzo 			if (pwd == NULL)
43509758b77fSLuigi Rizzo 				errx(EX_DATAERR, "uid \"%s\" nonexistent", *av);
4351d6abaeebSMaxim Konovalov 			cmd32->d[0] = pwd->pw_uid;
43523a27af0dSChristian S.J. Peron 			cmd->len |= F_INSN_SIZE(ipfw_insn_u32);
43539758b77fSLuigi Rizzo 			ac--; av++;
43549758b77fSLuigi Rizzo 		    }
43559758b77fSLuigi Rizzo 			break;
43569758b77fSLuigi Rizzo 
43579758b77fSLuigi Rizzo 		case TOK_GID:
43589758b77fSLuigi Rizzo 			NEED1("gid requires argument");
43599758b77fSLuigi Rizzo 		    {
43609758b77fSLuigi Rizzo 			char *end;
43619758b77fSLuigi Rizzo 			gid_t gid;
43629758b77fSLuigi Rizzo 			struct group *grp;
43639758b77fSLuigi Rizzo 
43649758b77fSLuigi Rizzo 			cmd->opcode = O_GID;
43659758b77fSLuigi Rizzo 			gid = strtoul(*av, &end, 0);
43669758b77fSLuigi Rizzo 			grp = (*end == '\0') ? getgrgid(gid) : getgrnam(*av);
43679758b77fSLuigi Rizzo 			if (grp == NULL)
43689758b77fSLuigi Rizzo 				errx(EX_DATAERR, "gid \"%s\" nonexistent", *av);
4369d6abaeebSMaxim Konovalov 			cmd32->d[0] = grp->gr_gid;
43703a27af0dSChristian S.J. Peron 			cmd->len |= F_INSN_SIZE(ipfw_insn_u32);
43719758b77fSLuigi Rizzo 			ac--; av++;
43729758b77fSLuigi Rizzo 		    }
43739758b77fSLuigi Rizzo 			break;
43749758b77fSLuigi Rizzo 
437531c88a30SChristian S.J. Peron 		case TOK_JAIL:
437631c88a30SChristian S.J. Peron 			NEED1("jail requires argument");
437731c88a30SChristian S.J. Peron 		    {
437831c88a30SChristian S.J. Peron 			char *end;
437931c88a30SChristian S.J. Peron 			int jid;
438031c88a30SChristian S.J. Peron 
438131c88a30SChristian S.J. Peron 			cmd->opcode = O_JAIL;
438231c88a30SChristian S.J. Peron 			jid = (int)strtol(*av, &end, 0);
438331c88a30SChristian S.J. Peron 			if (jid < 0 || *end != '\0')
438431c88a30SChristian S.J. Peron 				errx(EX_DATAERR, "jail requires prison ID");
4385d413c2e4SChristian S.J. Peron 			cmd32->d[0] = (uint32_t)jid;
43863a27af0dSChristian S.J. Peron 			cmd->len |= F_INSN_SIZE(ipfw_insn_u32);
438731c88a30SChristian S.J. Peron 			ac--; av++;
438831c88a30SChristian S.J. Peron 		    }
438931c88a30SChristian S.J. Peron 			break;
439031c88a30SChristian S.J. Peron 
43919758b77fSLuigi Rizzo 		case TOK_ESTAB:
43929758b77fSLuigi Rizzo 			fill_cmd(cmd, O_ESTAB, 0, 0);
43939758b77fSLuigi Rizzo 			break;
43949758b77fSLuigi Rizzo 
43959758b77fSLuigi Rizzo 		case TOK_SETUP:
43969758b77fSLuigi Rizzo 			fill_cmd(cmd, O_TCPFLAGS, 0,
43979758b77fSLuigi Rizzo 				(TH_SYN) | ( (TH_ACK) & 0xff) <<8 );
43989758b77fSLuigi Rizzo 			break;
43999758b77fSLuigi Rizzo 
4400c99ee9e0SBrian Feldman 		case TOK_TCPDATALEN:
4401c99ee9e0SBrian Feldman 			NEED1("tcpdatalen requires length");
4402c99ee9e0SBrian Feldman 			if (strpbrk(*av, "-,")) {
4403c99ee9e0SBrian Feldman 			    if (!add_ports(cmd, *av, 0, O_TCPDATALEN))
4404c99ee9e0SBrian Feldman 				errx(EX_DATAERR, "invalid tcpdata len %s", *av);
4405c99ee9e0SBrian Feldman 			} else
4406c99ee9e0SBrian Feldman 			    fill_cmd(cmd, O_TCPDATALEN, 0,
4407c99ee9e0SBrian Feldman 				    strtoul(*av, NULL, 0));
4408c99ee9e0SBrian Feldman 			ac--; av++;
4409c99ee9e0SBrian Feldman 			break;
4410c99ee9e0SBrian Feldman 
44119758b77fSLuigi Rizzo 		case TOK_TCPOPTS:
44129758b77fSLuigi Rizzo 			NEED1("missing argument for tcpoptions");
44139758b77fSLuigi Rizzo 			fill_flags(cmd, O_TCPOPTS, f_tcpopts, *av);
44149758b77fSLuigi Rizzo 			ac--; av++;
44159758b77fSLuigi Rizzo 			break;
44169758b77fSLuigi Rizzo 
44179758b77fSLuigi Rizzo 		case TOK_TCPSEQ:
44189758b77fSLuigi Rizzo 		case TOK_TCPACK:
44199758b77fSLuigi Rizzo 			NEED1("tcpseq/tcpack requires argument");
44209758b77fSLuigi Rizzo 			cmd->len = F_INSN_SIZE(ipfw_insn_u32);
44219758b77fSLuigi Rizzo 			cmd->opcode = (i == TOK_TCPSEQ) ? O_TCPSEQ : O_TCPACK;
44229758b77fSLuigi Rizzo 			cmd32->d[0] = htonl(strtoul(*av, NULL, 0));
44239758b77fSLuigi Rizzo 			ac--; av++;
44249758b77fSLuigi Rizzo 			break;
44259758b77fSLuigi Rizzo 
44269758b77fSLuigi Rizzo 		case TOK_TCPWIN:
44279758b77fSLuigi Rizzo 			NEED1("tcpwin requires length");
44289758b77fSLuigi Rizzo 			fill_cmd(cmd, O_TCPWIN, 0,
44299758b77fSLuigi Rizzo 			    htons(strtoul(*av, NULL, 0)));
44309758b77fSLuigi Rizzo 			ac--; av++;
44319758b77fSLuigi Rizzo 			break;
44329758b77fSLuigi Rizzo 
44339758b77fSLuigi Rizzo 		case TOK_TCPFLAGS:
44349758b77fSLuigi Rizzo 			NEED1("missing argument for tcpflags");
44359758b77fSLuigi Rizzo 			cmd->opcode = O_TCPFLAGS;
44369758b77fSLuigi Rizzo 			fill_flags(cmd, O_TCPFLAGS, f_tcpflags, *av);
44379758b77fSLuigi Rizzo 			ac--; av++;
44389758b77fSLuigi Rizzo 			break;
44399758b77fSLuigi Rizzo 
44409758b77fSLuigi Rizzo 		case TOK_KEEPSTATE:
44418ed2d749SLuigi Rizzo 			if (open_par)
44428ed2d749SLuigi Rizzo 				errx(EX_USAGE, "keep-state cannot be part "
44438ed2d749SLuigi Rizzo 				    "of an or block");
44440a7197a8SLuigi Rizzo 			if (have_state)
444552bc23abSLuigi Rizzo 				errx(EX_USAGE, "only one of keep-state "
44460a7197a8SLuigi Rizzo 					"and limit is allowed");
444752bc23abSLuigi Rizzo 			have_state = cmd;
44489758b77fSLuigi Rizzo 			fill_cmd(cmd, O_KEEP_STATE, 0, 0);
44499758b77fSLuigi Rizzo 			break;
44509758b77fSLuigi Rizzo 
44519758b77fSLuigi Rizzo 		case TOK_LIMIT:
44528ed2d749SLuigi Rizzo 			if (open_par)
44538ed2d749SLuigi Rizzo 				errx(EX_USAGE, "limit cannot be part "
44548ed2d749SLuigi Rizzo 				    "of an or block");
44550a7197a8SLuigi Rizzo 			if (have_state)
445652bc23abSLuigi Rizzo 				errx(EX_USAGE, "only one of keep-state "
44570a7197a8SLuigi Rizzo 					"and limit is allowed");
44588ed2d749SLuigi Rizzo 			NEED1("limit needs mask and # of connections");
445952bc23abSLuigi Rizzo 			have_state = cmd;
44609758b77fSLuigi Rizzo 		    {
44619758b77fSLuigi Rizzo 			ipfw_insn_limit *c = (ipfw_insn_limit *)cmd;
44629758b77fSLuigi Rizzo 
44639758b77fSLuigi Rizzo 			cmd->len = F_INSN_SIZE(ipfw_insn_limit);
44649758b77fSLuigi Rizzo 			cmd->opcode = O_LIMIT;
44659758b77fSLuigi Rizzo 			c->limit_mask = 0;
44669758b77fSLuigi Rizzo 			c->conn_limit = 0;
44679758b77fSLuigi Rizzo 			for (; ac >1 ;) {
44689758b77fSLuigi Rizzo 				int val;
44699758b77fSLuigi Rizzo 
44709758b77fSLuigi Rizzo 				val = match_token(limit_masks, *av);
44719758b77fSLuigi Rizzo 				if (val <= 0)
44729758b77fSLuigi Rizzo 					break;
44739758b77fSLuigi Rizzo 				c->limit_mask |= val;
44749758b77fSLuigi Rizzo 				ac--; av++;
44759758b77fSLuigi Rizzo 			}
44769758b77fSLuigi Rizzo 			c->conn_limit = atoi(*av);
44779758b77fSLuigi Rizzo 			if (c->conn_limit == 0)
44789758b77fSLuigi Rizzo 				errx(EX_USAGE, "limit: limit must be >0");
44799758b77fSLuigi Rizzo 			if (c->limit_mask == 0)
44809758b77fSLuigi Rizzo 				errx(EX_USAGE, "missing limit mask");
44819758b77fSLuigi Rizzo 			ac--; av++;
44829758b77fSLuigi Rizzo 		    }
44839758b77fSLuigi Rizzo 			break;
44849758b77fSLuigi Rizzo 
4485e706181bSLuigi Rizzo 		case TOK_PROTO:
4486e706181bSLuigi Rizzo 			NEED1("missing protocol");
44878195404bSBrooks Davis 			if (add_proto(cmd, *av, &proto)) {
4488e706181bSLuigi Rizzo 				ac--; av++;
44895a155b40SLuigi Rizzo 			} else
4490c82b8dceSMaxim Konovalov 				errx(EX_DATAERR, "invalid protocol ``%s''",
4491c82b8dceSMaxim Konovalov 				    *av);
4492e706181bSLuigi Rizzo 			break;
4493e706181bSLuigi Rizzo 
4494e706181bSLuigi Rizzo 		case TOK_SRCIP:
4495e706181bSLuigi Rizzo 			NEED1("missing source IP");
4496e706181bSLuigi Rizzo 			if (add_srcip(cmd, *av)) {
4497e706181bSLuigi Rizzo 				ac--; av++;
4498e706181bSLuigi Rizzo 			}
4499e706181bSLuigi Rizzo 			break;
4500e706181bSLuigi Rizzo 
4501e706181bSLuigi Rizzo 		case TOK_DSTIP:
4502e706181bSLuigi Rizzo 			NEED1("missing destination IP");
4503e706181bSLuigi Rizzo 			if (add_dstip(cmd, *av)) {
4504e706181bSLuigi Rizzo 				ac--; av++;
4505e706181bSLuigi Rizzo 			}
4506e706181bSLuigi Rizzo 			break;
4507e706181bSLuigi Rizzo 
45088195404bSBrooks Davis 		case TOK_SRCIP6:
45098195404bSBrooks Davis 			NEED1("missing source IP6");
45108195404bSBrooks Davis 			if (add_srcip6(cmd, *av)) {
45118195404bSBrooks Davis 				ac--; av++;
45128195404bSBrooks Davis 			}
45138195404bSBrooks Davis 			break;
45148195404bSBrooks Davis 
45158195404bSBrooks Davis 		case TOK_DSTIP6:
45168195404bSBrooks Davis 			NEED1("missing destination IP6");
45178195404bSBrooks Davis 			if (add_dstip6(cmd, *av)) {
45188195404bSBrooks Davis 				ac--; av++;
45198195404bSBrooks Davis 			}
45208195404bSBrooks Davis 			break;
45218195404bSBrooks Davis 
4522e706181bSLuigi Rizzo 		case TOK_SRCPORT:
4523e706181bSLuigi Rizzo 			NEED1("missing source port");
452401750186SBrooks Davis 			if (_substrcmp(*av, "any") == 0 ||
4525e706181bSLuigi Rizzo 			    add_ports(cmd, *av, proto, O_IP_SRCPORT)) {
4526e706181bSLuigi Rizzo 				ac--; av++;
4527e706181bSLuigi Rizzo 			} else
4528e706181bSLuigi Rizzo 				errx(EX_DATAERR, "invalid source port %s", *av);
4529e706181bSLuigi Rizzo 			break;
4530e706181bSLuigi Rizzo 
4531e706181bSLuigi Rizzo 		case TOK_DSTPORT:
4532e706181bSLuigi Rizzo 			NEED1("missing destination port");
453301750186SBrooks Davis 			if (_substrcmp(*av, "any") == 0 ||
4534e706181bSLuigi Rizzo 			    add_ports(cmd, *av, proto, O_IP_DSTPORT)) {
4535e706181bSLuigi Rizzo 				ac--; av++;
4536e706181bSLuigi Rizzo 			} else
4537e706181bSLuigi Rizzo 				errx(EX_DATAERR, "invalid destination port %s",
4538e706181bSLuigi Rizzo 				    *av);
4539e706181bSLuigi Rizzo 			break;
4540e706181bSLuigi Rizzo 
4541e706181bSLuigi Rizzo 		case TOK_MAC:
4542e706181bSLuigi Rizzo 			if (add_mac(cmd, ac, av)) {
4543e706181bSLuigi Rizzo 				ac -= 2; av += 2;
4544e706181bSLuigi Rizzo 			}
4545e706181bSLuigi Rizzo 			break;
4546e706181bSLuigi Rizzo 
4547e706181bSLuigi Rizzo 		case TOK_MACTYPE:
4548e706181bSLuigi Rizzo 			NEED1("missing mac type");
4549e706181bSLuigi Rizzo 			if (!add_mactype(cmd, ac, *av))
4550c82b8dceSMaxim Konovalov 				errx(EX_DATAERR, "invalid mac type %s", *av);
4551e706181bSLuigi Rizzo 			ac--; av++;
4552e706181bSLuigi Rizzo 			break;
4553e706181bSLuigi Rizzo 
4554010dabb0SCrist J. Clark 		case TOK_VERREVPATH:
4555010dabb0SCrist J. Clark 			fill_cmd(cmd, O_VERREVPATH, 0, 0);
4556010dabb0SCrist J. Clark 			break;
4557010dabb0SCrist J. Clark 
455822b5770bSAndre Oppermann 		case TOK_VERSRCREACH:
455922b5770bSAndre Oppermann 			fill_cmd(cmd, O_VERSRCREACH, 0, 0);
456022b5770bSAndre Oppermann 			break;
456122b5770bSAndre Oppermann 
45625f9541ecSAndre Oppermann 		case TOK_ANTISPOOF:
45635f9541ecSAndre Oppermann 			fill_cmd(cmd, O_ANTISPOOF, 0, 0);
45645f9541ecSAndre Oppermann 			break;
45655f9541ecSAndre Oppermann 
4566c3e5b9f1SLuigi Rizzo 		case TOK_IPSEC:
4567c3e5b9f1SLuigi Rizzo 			fill_cmd(cmd, O_IPSEC, 0, 0);
4568c3e5b9f1SLuigi Rizzo 			break;
4569c3e5b9f1SLuigi Rizzo 
45708195404bSBrooks Davis 		case TOK_IPV6:
45718195404bSBrooks Davis 			fill_cmd(cmd, O_IP6, 0, 0);
45728195404bSBrooks Davis 			break;
45738195404bSBrooks Davis 
457457cd6d26SMax Laier 		case TOK_IPV4:
457557cd6d26SMax Laier 			fill_cmd(cmd, O_IP4, 0, 0);
457657cd6d26SMax Laier 			break;
457757cd6d26SMax Laier 
45788195404bSBrooks Davis 		case TOK_EXT6HDR:
45798195404bSBrooks Davis 			fill_ext6hdr( cmd, *av );
45808195404bSBrooks Davis 			ac--; av++;
45818195404bSBrooks Davis 			break;
45828195404bSBrooks Davis 
45838195404bSBrooks Davis 		case TOK_FLOWID:
45848195404bSBrooks Davis 			if (proto != IPPROTO_IPV6 )
45858195404bSBrooks Davis 				errx( EX_USAGE, "flow-id filter is active "
45868195404bSBrooks Davis 				    "only for ipv6 protocol\n");
45878195404bSBrooks Davis 			fill_flow6( (ipfw_insn_u32 *) cmd, *av );
45888195404bSBrooks Davis 			ac--; av++;
45898195404bSBrooks Davis 			break;
45908195404bSBrooks Davis 
459162ff38aeSLuigi Rizzo 		case TOK_COMMENT:
459262ff38aeSLuigi Rizzo 			fill_comment(cmd, ac, av);
459362ff38aeSLuigi Rizzo 			av += ac;
459462ff38aeSLuigi Rizzo 			ac = 0;
459562ff38aeSLuigi Rizzo 			break;
459662ff38aeSLuigi Rizzo 
45979758b77fSLuigi Rizzo 		default:
45989758b77fSLuigi Rizzo 			errx(EX_USAGE, "unrecognised option [%d] %s\n", i, s);
45999758b77fSLuigi Rizzo 		}
46009758b77fSLuigi Rizzo 		if (F_LEN(cmd) > 0) {	/* prepare to advance */
46019758b77fSLuigi Rizzo 			prev = cmd;
46029758b77fSLuigi Rizzo 			cmd = next_cmd(cmd);
46039758b77fSLuigi Rizzo 		}
46049758b77fSLuigi Rizzo 	}
46059758b77fSLuigi Rizzo 
46069758b77fSLuigi Rizzo done:
46079758b77fSLuigi Rizzo 	/*
46089758b77fSLuigi Rizzo 	 * Now copy stuff into the rule.
46099758b77fSLuigi Rizzo 	 * If we have a keep-state option, the first instruction
46109758b77fSLuigi Rizzo 	 * must be a PROBE_STATE (which is generated here).
46119758b77fSLuigi Rizzo 	 * If we have a LOG option, it was stored as the first command,
46129758b77fSLuigi Rizzo 	 * and now must be moved to the top of the action part.
46139758b77fSLuigi Rizzo 	 */
46149758b77fSLuigi Rizzo 	dst = (ipfw_insn *)rule->cmd;
46159758b77fSLuigi Rizzo 
46169758b77fSLuigi Rizzo 	/*
461712b5dc6aSLuigi Rizzo 	 * First thing to write into the command stream is the match probability.
461812b5dc6aSLuigi Rizzo 	 */
461912b5dc6aSLuigi Rizzo 	if (match_prob != 1) { /* 1 means always match */
462012b5dc6aSLuigi Rizzo 		dst->opcode = O_PROB;
462112b5dc6aSLuigi Rizzo 		dst->len = 2;
462212b5dc6aSLuigi Rizzo 		*((int32_t *)(dst+1)) = (int32_t)(match_prob * 0x7fffffff);
462312b5dc6aSLuigi Rizzo 		dst += dst->len;
462412b5dc6aSLuigi Rizzo 	}
462512b5dc6aSLuigi Rizzo 
462612b5dc6aSLuigi Rizzo 	/*
46279758b77fSLuigi Rizzo 	 * generate O_PROBE_STATE if necessary
46289758b77fSLuigi Rizzo 	 */
462952bc23abSLuigi Rizzo 	if (have_state && have_state->opcode != O_CHECK_STATE) {
46309758b77fSLuigi Rizzo 		fill_cmd(dst, O_PROBE_STATE, 0, 0);
46319758b77fSLuigi Rizzo 		dst = next_cmd(dst);
46329758b77fSLuigi Rizzo 	}
46339758b77fSLuigi Rizzo 	/*
4634974dfe30SBrian Feldman 	 * copy all commands but O_LOG, O_KEEP_STATE, O_LIMIT, O_ALTQ
46359758b77fSLuigi Rizzo 	 */
46369758b77fSLuigi Rizzo 	for (src = (ipfw_insn *)cmdbuf; src != cmd; src += i) {
46379758b77fSLuigi Rizzo 		i = F_LEN(src);
46389758b77fSLuigi Rizzo 
463952bc23abSLuigi Rizzo 		switch (src->opcode) {
464052bc23abSLuigi Rizzo 		case O_LOG:
464152bc23abSLuigi Rizzo 		case O_KEEP_STATE:
464252bc23abSLuigi Rizzo 		case O_LIMIT:
4643974dfe30SBrian Feldman 		case O_ALTQ:
464452bc23abSLuigi Rizzo 			break;
464552bc23abSLuigi Rizzo 		default:
4646571f8c1bSLuigi Rizzo 			bcopy(src, dst, i * sizeof(uint32_t));
46479758b77fSLuigi Rizzo 			dst += i;
46489758b77fSLuigi Rizzo 		}
46499758b77fSLuigi Rizzo 	}
46509758b77fSLuigi Rizzo 
46519758b77fSLuigi Rizzo 	/*
465252bc23abSLuigi Rizzo 	 * put back the have_state command as last opcode
465352bc23abSLuigi Rizzo 	 */
4654b985a624SLuigi Rizzo 	if (have_state && have_state->opcode != O_CHECK_STATE) {
465552bc23abSLuigi Rizzo 		i = F_LEN(have_state);
4656571f8c1bSLuigi Rizzo 		bcopy(have_state, dst, i * sizeof(uint32_t));
465752bc23abSLuigi Rizzo 		dst += i;
465852bc23abSLuigi Rizzo 	}
465952bc23abSLuigi Rizzo 	/*
46609758b77fSLuigi Rizzo 	 * start action section
46619758b77fSLuigi Rizzo 	 */
46629758b77fSLuigi Rizzo 	rule->act_ofs = dst - rule->cmd;
46639758b77fSLuigi Rizzo 
46649758b77fSLuigi Rizzo 	/*
4665974dfe30SBrian Feldman 	 * put back O_LOG, O_ALTQ if necessary
46669758b77fSLuigi Rizzo 	 */
4667974dfe30SBrian Feldman 	if (have_log) {
4668974dfe30SBrian Feldman 		i = F_LEN(have_log);
4669974dfe30SBrian Feldman 		bcopy(have_log, dst, i * sizeof(uint32_t));
4670974dfe30SBrian Feldman 		dst += i;
4671974dfe30SBrian Feldman 	}
4672974dfe30SBrian Feldman 	if (have_altq) {
4673974dfe30SBrian Feldman 		i = F_LEN(have_altq);
4674974dfe30SBrian Feldman 		bcopy(have_altq, dst, i * sizeof(uint32_t));
46759758b77fSLuigi Rizzo 		dst += i;
46769758b77fSLuigi Rizzo 	}
46779758b77fSLuigi Rizzo 	/*
46789758b77fSLuigi Rizzo 	 * copy all other actions
46799758b77fSLuigi Rizzo 	 */
46809758b77fSLuigi Rizzo 	for (src = (ipfw_insn *)actbuf; src != action; src += i) {
46819758b77fSLuigi Rizzo 		i = F_LEN(src);
4682571f8c1bSLuigi Rizzo 		bcopy(src, dst, i * sizeof(uint32_t));
46839758b77fSLuigi Rizzo 		dst += i;
46849758b77fSLuigi Rizzo 	}
46859758b77fSLuigi Rizzo 
4686571f8c1bSLuigi Rizzo 	rule->cmd_len = (uint32_t *)dst - (uint32_t *)(rule->cmd);
468762ff38aeSLuigi Rizzo 	i = (char *)dst - (char *)rule;
4688884be75cSThomas Moestl 	if (do_cmd(IP_FW_ADD, rule, (uintptr_t)&i) == -1)
46899758b77fSLuigi Rizzo 		err(EX_UNAVAILABLE, "getsockopt(%s)", "IP_FW_ADD");
46909758b77fSLuigi Rizzo 	if (!do_quiet)
469162ff38aeSLuigi Rizzo 		show_ipfw(rule, 0, 0);
46929758b77fSLuigi Rizzo }
46939758b77fSLuigi Rizzo 
46949758b77fSLuigi Rizzo static void
4695571f8c1bSLuigi Rizzo zero(int ac, char *av[], int optname /* IP_FW_ZERO or IP_FW_RESETLOG */)
46969758b77fSLuigi Rizzo {
46979758b77fSLuigi Rizzo 	int rulenum;
46989758b77fSLuigi Rizzo 	int failed = EX_OK;
469962ff38aeSLuigi Rizzo 	char const *name = optname == IP_FW_ZERO ?  "ZERO" : "RESETLOG";
47009758b77fSLuigi Rizzo 
47019758b77fSLuigi Rizzo 	av++; ac--;
47029758b77fSLuigi Rizzo 
47039758b77fSLuigi Rizzo 	if (!ac) {
47049758b77fSLuigi Rizzo 		/* clear all entries */
4705571f8c1bSLuigi Rizzo 		if (do_cmd(optname, NULL, 0) < 0)
4706571f8c1bSLuigi Rizzo 			err(EX_UNAVAILABLE, "setsockopt(IP_FW_%s)", name);
47079758b77fSLuigi Rizzo 		if (!do_quiet)
4708571f8c1bSLuigi Rizzo 			printf("%s.\n", optname == IP_FW_ZERO ?
4709571f8c1bSLuigi Rizzo 			    "Accounting cleared":"Logging counts reset");
47109758b77fSLuigi Rizzo 
47119758b77fSLuigi Rizzo 		return;
47129758b77fSLuigi Rizzo 	}
47139758b77fSLuigi Rizzo 
47149758b77fSLuigi Rizzo 	while (ac) {
47159758b77fSLuigi Rizzo 		/* Rule number */
47169758b77fSLuigi Rizzo 		if (isdigit(**av)) {
47179758b77fSLuigi Rizzo 			rulenum = atoi(*av);
47189758b77fSLuigi Rizzo 			av++;
47199758b77fSLuigi Rizzo 			ac--;
4720571f8c1bSLuigi Rizzo 			if (do_cmd(optname, &rulenum, sizeof rulenum)) {
4721571f8c1bSLuigi Rizzo 				warn("rule %u: setsockopt(IP_FW_%s)",
4722571f8c1bSLuigi Rizzo 				    rulenum, name);
47239758b77fSLuigi Rizzo 				failed = EX_UNAVAILABLE;
47249758b77fSLuigi Rizzo 			} else if (!do_quiet)
4725571f8c1bSLuigi Rizzo 				printf("Entry %d %s.\n", rulenum,
4726571f8c1bSLuigi Rizzo 				    optname == IP_FW_ZERO ?
4727571f8c1bSLuigi Rizzo 					"cleared" : "logging count reset");
47289758b77fSLuigi Rizzo 		} else {
47299758b77fSLuigi Rizzo 			errx(EX_USAGE, "invalid rule number ``%s''", *av);
47309758b77fSLuigi Rizzo 		}
47319758b77fSLuigi Rizzo 	}
47329758b77fSLuigi Rizzo 	if (failed != EX_OK)
47339758b77fSLuigi Rizzo 		exit(failed);
47349758b77fSLuigi Rizzo }
47359758b77fSLuigi Rizzo 
47369758b77fSLuigi Rizzo static void
473726bf4d78SLuigi Rizzo flush(int force)
47389758b77fSLuigi Rizzo {
47399758b77fSLuigi Rizzo 	int cmd = do_pipe ? IP_DUMMYNET_FLUSH : IP_FW_FLUSH;
47409758b77fSLuigi Rizzo 
474126bf4d78SLuigi Rizzo 	if (!force && !do_quiet) { /* need to ask user */
47429758b77fSLuigi Rizzo 		int c;
47439758b77fSLuigi Rizzo 
47449758b77fSLuigi Rizzo 		printf("Are you sure? [yn] ");
47459758b77fSLuigi Rizzo 		fflush(stdout);
47469758b77fSLuigi Rizzo 		do {
47479758b77fSLuigi Rizzo 			c = toupper(getc(stdin));
47489758b77fSLuigi Rizzo 			while (c != '\n' && getc(stdin) != '\n')
47499758b77fSLuigi Rizzo 				if (feof(stdin))
47509758b77fSLuigi Rizzo 					return; /* and do not flush */
47519758b77fSLuigi Rizzo 		} while (c != 'Y' && c != 'N');
47529758b77fSLuigi Rizzo 		printf("\n");
47539758b77fSLuigi Rizzo 		if (c == 'N')	/* user said no */
47549758b77fSLuigi Rizzo 			return;
47559758b77fSLuigi Rizzo 	}
4756571f8c1bSLuigi Rizzo 	if (do_cmd(cmd, NULL, 0) < 0)
47579758b77fSLuigi Rizzo 		err(EX_UNAVAILABLE, "setsockopt(IP_%s_FLUSH)",
47589758b77fSLuigi Rizzo 		    do_pipe ? "DUMMYNET" : "FW");
47599758b77fSLuigi Rizzo 	if (!do_quiet)
47609758b77fSLuigi Rizzo 		printf("Flushed all %s.\n", do_pipe ? "pipes" : "rules");
47619758b77fSLuigi Rizzo }
47629758b77fSLuigi Rizzo 
476362ff38aeSLuigi Rizzo /*
476426bf4d78SLuigi Rizzo  * Free a the (locally allocated) copy of command line arguments.
476526bf4d78SLuigi Rizzo  */
476626bf4d78SLuigi Rizzo static void
476726bf4d78SLuigi Rizzo free_args(int ac, char **av)
476826bf4d78SLuigi Rizzo {
476926bf4d78SLuigi Rizzo 	int i;
477026bf4d78SLuigi Rizzo 
477126bf4d78SLuigi Rizzo 	for (i=0; i < ac; i++)
477226bf4d78SLuigi Rizzo 		free(av[i]);
477326bf4d78SLuigi Rizzo 	free(av);
477426bf4d78SLuigi Rizzo }
477526bf4d78SLuigi Rizzo 
477626bf4d78SLuigi Rizzo /*
4777cd8b5ae0SRuslan Ermilov  * This one handles all table-related commands
4778cd8b5ae0SRuslan Ermilov  * 	ipfw table N add addr[/masklen] [value]
4779cd8b5ae0SRuslan Ermilov  * 	ipfw table N delete addr[/masklen]
4780cd8b5ae0SRuslan Ermilov  * 	ipfw table N flush
4781cd8b5ae0SRuslan Ermilov  * 	ipfw table N list
4782cd8b5ae0SRuslan Ermilov  */
4783cd8b5ae0SRuslan Ermilov static void
4784cd8b5ae0SRuslan Ermilov table_handler(int ac, char *av[])
4785cd8b5ae0SRuslan Ermilov {
4786cd8b5ae0SRuslan Ermilov 	ipfw_table_entry ent;
4787cd8b5ae0SRuslan Ermilov 	ipfw_table *tbl;
4788cd8b5ae0SRuslan Ermilov 	int do_add;
4789cd8b5ae0SRuslan Ermilov 	char *p;
4790cd8b5ae0SRuslan Ermilov 	socklen_t l;
4791cd8b5ae0SRuslan Ermilov 	uint32_t a;
4792cd8b5ae0SRuslan Ermilov 
4793cd8b5ae0SRuslan Ermilov 	ac--; av++;
4794cd8b5ae0SRuslan Ermilov 	if (ac && isdigit(**av)) {
4795cd8b5ae0SRuslan Ermilov 		ent.tbl = atoi(*av);
4796cd8b5ae0SRuslan Ermilov 		ac--; av++;
4797cd8b5ae0SRuslan Ermilov 	} else
4798cd8b5ae0SRuslan Ermilov 		errx(EX_USAGE, "table number required");
4799cd8b5ae0SRuslan Ermilov 	NEED1("table needs command");
480001750186SBrooks Davis 	if (_substrcmp(*av, "add") == 0 ||
480101750186SBrooks Davis 	    _substrcmp(*av, "delete") == 0) {
4802cd8b5ae0SRuslan Ermilov 		do_add = **av == 'a';
4803cd8b5ae0SRuslan Ermilov 		ac--; av++;
4804cd8b5ae0SRuslan Ermilov 		if (!ac)
4805cd8b5ae0SRuslan Ermilov 			errx(EX_USAGE, "IP address required");
4806cd8b5ae0SRuslan Ermilov 		p = strchr(*av, '/');
4807cd8b5ae0SRuslan Ermilov 		if (p) {
4808cd8b5ae0SRuslan Ermilov 			*p++ = '\0';
4809cd8b5ae0SRuslan Ermilov 			ent.masklen = atoi(p);
4810cd8b5ae0SRuslan Ermilov 			if (ent.masklen > 32)
4811cd8b5ae0SRuslan Ermilov 				errx(EX_DATAERR, "bad width ``%s''", p);
4812cd8b5ae0SRuslan Ermilov 		} else
4813cd8b5ae0SRuslan Ermilov 			ent.masklen = 32;
4814cd8b5ae0SRuslan Ermilov 		if (lookup_host(*av, (struct in_addr *)&ent.addr) != 0)
48151a41a8e4SRuslan Ermilov 			errx(EX_NOHOST, "hostname ``%s'' unknown", *av);
4816cd8b5ae0SRuslan Ermilov 		ac--; av++;
4817cd8b5ae0SRuslan Ermilov 		if (do_add && ac)
4818cd8b5ae0SRuslan Ermilov 			ent.value = strtoul(*av, NULL, 0);
4819cd8b5ae0SRuslan Ermilov 		else
4820cd8b5ae0SRuslan Ermilov 			ent.value = 0;
4821cd8b5ae0SRuslan Ermilov 		if (do_cmd(do_add ? IP_FW_TABLE_ADD : IP_FW_TABLE_DEL,
4822cd8b5ae0SRuslan Ermilov 		    &ent, sizeof(ent)) < 0)
4823cd8b5ae0SRuslan Ermilov 			err(EX_OSERR, "setsockopt(IP_FW_TABLE_%s)",
4824cd8b5ae0SRuslan Ermilov 			    do_add ? "ADD" : "DEL");
482501750186SBrooks Davis 	} else if (_substrcmp(*av, "flush") == 0) {
4826cd8b5ae0SRuslan Ermilov 		if (do_cmd(IP_FW_TABLE_FLUSH, &ent.tbl, sizeof(ent.tbl)) < 0)
4827cd8b5ae0SRuslan Ermilov 			err(EX_OSERR, "setsockopt(IP_FW_TABLE_FLUSH)");
482801750186SBrooks Davis 	} else if (_substrcmp(*av, "list") == 0) {
4829cd8b5ae0SRuslan Ermilov 		a = ent.tbl;
4830cd8b5ae0SRuslan Ermilov 		l = sizeof(a);
4831cd8b5ae0SRuslan Ermilov 		if (do_cmd(IP_FW_TABLE_GETSIZE, &a, (uintptr_t)&l) < 0)
4832cd8b5ae0SRuslan Ermilov 			err(EX_OSERR, "getsockopt(IP_FW_TABLE_GETSIZE)");
4833cd8b5ae0SRuslan Ermilov 		l = sizeof(*tbl) + a * sizeof(ipfw_table_entry);
4834cd8b5ae0SRuslan Ermilov 		tbl = malloc(l);
4835cd8b5ae0SRuslan Ermilov 		if (tbl == NULL)
4836cd8b5ae0SRuslan Ermilov 			err(EX_OSERR, "malloc");
4837cd8b5ae0SRuslan Ermilov 		tbl->tbl = ent.tbl;
4838cd8b5ae0SRuslan Ermilov 		if (do_cmd(IP_FW_TABLE_LIST, tbl, (uintptr_t)&l) < 0)
4839cd8b5ae0SRuslan Ermilov 			err(EX_OSERR, "getsockopt(IP_FW_TABLE_LIST)");
4840cd8b5ae0SRuslan Ermilov 		for (a = 0; a < tbl->cnt; a++) {
4841cd8b5ae0SRuslan Ermilov 			printf("%s/%u %u\n",
4842cd8b5ae0SRuslan Ermilov 			    inet_ntoa(*(struct in_addr *)&tbl->ent[a].addr),
4843cd8b5ae0SRuslan Ermilov 			    tbl->ent[a].masklen, tbl->ent[a].value);
4844cd8b5ae0SRuslan Ermilov 		}
4845cd8b5ae0SRuslan Ermilov 	} else
4846cd8b5ae0SRuslan Ermilov 		errx(EX_USAGE, "invalid table command %s", *av);
4847cd8b5ae0SRuslan Ermilov }
4848cd8b5ae0SRuslan Ermilov 
4849cd8b5ae0SRuslan Ermilov /*
485026bf4d78SLuigi Rizzo  * Called with the arguments (excluding program name).
485126bf4d78SLuigi Rizzo  * Returns 0 if successful, 1 if empty command, errx() in case of errors.
485262ff38aeSLuigi Rizzo  */
48539758b77fSLuigi Rizzo static int
4854571f8c1bSLuigi Rizzo ipfw_main(int oldac, char **oldav)
48559758b77fSLuigi Rizzo {
485662ff38aeSLuigi Rizzo 	int ch, ac, save_ac;
485762ff38aeSLuigi Rizzo 	char **av, **save_av;
485862ff38aeSLuigi Rizzo 	int do_acct = 0;		/* Show packet/byte count */
4859571f8c1bSLuigi Rizzo 
486062ff38aeSLuigi Rizzo #define WHITESP		" \t\f\v\n\r"
486126bf4d78SLuigi Rizzo 	if (oldac == 0)
486226bf4d78SLuigi Rizzo 		return 1;
486326bf4d78SLuigi Rizzo 	else if (oldac == 1) {
4864571f8c1bSLuigi Rizzo 		/*
4865571f8c1bSLuigi Rizzo 		 * If we are called with a single string, try to split it into
4866571f8c1bSLuigi Rizzo 		 * arguments for subsequent parsing.
4867571f8c1bSLuigi Rizzo 		 * But first, remove spaces after a ',', by copying the string
4868571f8c1bSLuigi Rizzo 		 * in-place.
4869571f8c1bSLuigi Rizzo 		 */
487062ff38aeSLuigi Rizzo 		char *arg = oldav[0];	/* The string... */
4871571f8c1bSLuigi Rizzo 		int l = strlen(arg);
4872571f8c1bSLuigi Rizzo 		int copy = 0;		/* 1 if we need to copy, 0 otherwise */
4873571f8c1bSLuigi Rizzo 		int i, j;
487462ff38aeSLuigi Rizzo 		for (i = j = 0; i < l; i++) {
487562ff38aeSLuigi Rizzo 			if (arg[i] == '#')	/* comment marker */
487662ff38aeSLuigi Rizzo 				break;
4877571f8c1bSLuigi Rizzo 			if (copy) {
4878571f8c1bSLuigi Rizzo 				arg[j++] = arg[i];
487962ff38aeSLuigi Rizzo 				copy = !index("," WHITESP, arg[i]);
4880571f8c1bSLuigi Rizzo 			} else {
488162ff38aeSLuigi Rizzo 				copy = !index(WHITESP, arg[i]);
4882571f8c1bSLuigi Rizzo 				if (copy)
4883571f8c1bSLuigi Rizzo 					arg[j++] = arg[i];
4884571f8c1bSLuigi Rizzo 			}
488562ff38aeSLuigi Rizzo 		}
4886571f8c1bSLuigi Rizzo 		if (!copy && j > 0)	/* last char was a 'blank', remove it */
4887571f8c1bSLuigi Rizzo 			j--;
4888571f8c1bSLuigi Rizzo 		l = j;			/* the new argument length */
4889571f8c1bSLuigi Rizzo 		arg[j++] = '\0';
489062ff38aeSLuigi Rizzo 		if (l == 0)		/* empty string! */
489126bf4d78SLuigi Rizzo 			return 1;
4892571f8c1bSLuigi Rizzo 
4893571f8c1bSLuigi Rizzo 		/*
4894571f8c1bSLuigi Rizzo 		 * First, count number of arguments. Because of the previous
489562ff38aeSLuigi Rizzo 		 * processing, this is just the number of blanks plus 1.
4896571f8c1bSLuigi Rizzo 		 */
4897571f8c1bSLuigi Rizzo 		for (i = 0, ac = 1; i < l; i++)
489862ff38aeSLuigi Rizzo 			if (index(WHITESP, arg[i]) != NULL)
4899571f8c1bSLuigi Rizzo 				ac++;
4900571f8c1bSLuigi Rizzo 
4901571f8c1bSLuigi Rizzo 		av = calloc(ac, sizeof(char *));
4902571f8c1bSLuigi Rizzo 
4903571f8c1bSLuigi Rizzo 		/*
4904571f8c1bSLuigi Rizzo 		 * Second, copy arguments from cmd[] to av[]. For each one,
4905571f8c1bSLuigi Rizzo 		 * j is the initial character, i is the one past the end.
4906571f8c1bSLuigi Rizzo 		 */
490762ff38aeSLuigi Rizzo 		for (ac = 0, i = j = 0; i < l; i++)
490862ff38aeSLuigi Rizzo 			if (index(WHITESP, arg[i]) != NULL || i == l-1) {
4909571f8c1bSLuigi Rizzo 				if (i == l-1)
4910571f8c1bSLuigi Rizzo 					i++;
4911571f8c1bSLuigi Rizzo 				av[ac] = calloc(i-j+1, 1);
4912571f8c1bSLuigi Rizzo 				bcopy(arg+j, av[ac], i-j);
4913571f8c1bSLuigi Rizzo 				ac++;
4914571f8c1bSLuigi Rizzo 				j = i + 1;
4915571f8c1bSLuigi Rizzo 			}
4916571f8c1bSLuigi Rizzo 	} else {
4917571f8c1bSLuigi Rizzo 		/*
4918571f8c1bSLuigi Rizzo 		 * If an argument ends with ',' join with the next one.
4919571f8c1bSLuigi Rizzo 		 */
4920571f8c1bSLuigi Rizzo 		int first, i, l;
4921571f8c1bSLuigi Rizzo 
4922571f8c1bSLuigi Rizzo 		av = calloc(oldac, sizeof(char *));
4923571f8c1bSLuigi Rizzo 		for (first = i = ac = 0, l = 0; i < oldac; i++) {
4924571f8c1bSLuigi Rizzo 			char *arg = oldav[i];
4925571f8c1bSLuigi Rizzo 			int k = strlen(arg);
4926571f8c1bSLuigi Rizzo 
4927571f8c1bSLuigi Rizzo 			l += k;
4928571f8c1bSLuigi Rizzo 			if (arg[k-1] != ',' || i == oldac-1) {
4929571f8c1bSLuigi Rizzo 				/* Time to copy. */
4930571f8c1bSLuigi Rizzo 				av[ac] = calloc(l+1, 1);
4931571f8c1bSLuigi Rizzo 				for (l=0; first <= i; first++) {
4932571f8c1bSLuigi Rizzo 					strcat(av[ac]+l, oldav[first]);
4933571f8c1bSLuigi Rizzo 					l += strlen(oldav[first]);
4934571f8c1bSLuigi Rizzo 				}
4935571f8c1bSLuigi Rizzo 				ac++;
4936571f8c1bSLuigi Rizzo 				l = 0;
4937571f8c1bSLuigi Rizzo 				first = i+1;
4938571f8c1bSLuigi Rizzo 			}
4939571f8c1bSLuigi Rizzo 		}
4940571f8c1bSLuigi Rizzo 	}
49419758b77fSLuigi Rizzo 
49429758b77fSLuigi Rizzo 	/* Set the force flag for non-interactive processes */
4943cec4ab6aSMaxim Konovalov 	if (!do_force)
49449758b77fSLuigi Rizzo 		do_force = !isatty(STDIN_FILENO);
49459758b77fSLuigi Rizzo 
494662ff38aeSLuigi Rizzo 	/* Save arguments for final freeing of memory. */
494762ff38aeSLuigi Rizzo 	save_ac = ac;
494862ff38aeSLuigi Rizzo 	save_av = av;
494962ff38aeSLuigi Rizzo 
495062ff38aeSLuigi Rizzo 	optind = optreset = 0;
4951ac6cec51SLuigi Rizzo 	while ((ch = getopt(ac, av, "abcdefhnNqs:STtv")) != -1)
49529758b77fSLuigi Rizzo 		switch (ch) {
4953571f8c1bSLuigi Rizzo 		case 'a':
4954571f8c1bSLuigi Rizzo 			do_acct = 1;
4955571f8c1bSLuigi Rizzo 			break;
4956571f8c1bSLuigi Rizzo 
4957ac6cec51SLuigi Rizzo 		case 'b':
4958ac6cec51SLuigi Rizzo 			comment_only = 1;
4959ac6cec51SLuigi Rizzo 			do_compact = 1;
4960ac6cec51SLuigi Rizzo 			break;
4961ac6cec51SLuigi Rizzo 
4962571f8c1bSLuigi Rizzo 		case 'c':
4963571f8c1bSLuigi Rizzo 			do_compact = 1;
4964571f8c1bSLuigi Rizzo 			break;
4965571f8c1bSLuigi Rizzo 
4966571f8c1bSLuigi Rizzo 		case 'd':
4967571f8c1bSLuigi Rizzo 			do_dynamic = 1;
4968571f8c1bSLuigi Rizzo 			break;
4969571f8c1bSLuigi Rizzo 
4970571f8c1bSLuigi Rizzo 		case 'e':
4971571f8c1bSLuigi Rizzo 			do_expired = 1;
4972571f8c1bSLuigi Rizzo 			break;
4973571f8c1bSLuigi Rizzo 
4974571f8c1bSLuigi Rizzo 		case 'f':
4975571f8c1bSLuigi Rizzo 			do_force = 1;
4976571f8c1bSLuigi Rizzo 			break;
4977571f8c1bSLuigi Rizzo 
49789758b77fSLuigi Rizzo 		case 'h': /* help */
497926bf4d78SLuigi Rizzo 			free_args(save_ac, save_av);
49809758b77fSLuigi Rizzo 			help();
49819758b77fSLuigi Rizzo 			break;	/* NOTREACHED */
49829758b77fSLuigi Rizzo 
4983571f8c1bSLuigi Rizzo 		case 'n':
4984571f8c1bSLuigi Rizzo 			test_only = 1;
49859758b77fSLuigi Rizzo 			break;
4986571f8c1bSLuigi Rizzo 
49879758b77fSLuigi Rizzo 		case 'N':
49889758b77fSLuigi Rizzo 			do_resolv = 1;
49899758b77fSLuigi Rizzo 			break;
4990571f8c1bSLuigi Rizzo 
49919758b77fSLuigi Rizzo 		case 'q':
49929758b77fSLuigi Rizzo 			do_quiet = 1;
49939758b77fSLuigi Rizzo 			break;
4994571f8c1bSLuigi Rizzo 
4995571f8c1bSLuigi Rizzo 		case 's': /* sort */
4996571f8c1bSLuigi Rizzo 			do_sort = atoi(optarg);
4997571f8c1bSLuigi Rizzo 			break;
4998571f8c1bSLuigi Rizzo 
499943405724SLuigi Rizzo 		case 'S':
500043405724SLuigi Rizzo 			show_sets = 1;
500143405724SLuigi Rizzo 			break;
5002571f8c1bSLuigi Rizzo 
50039758b77fSLuigi Rizzo 		case 't':
50049758b77fSLuigi Rizzo 			do_time = 1;
50059758b77fSLuigi Rizzo 			break;
5006571f8c1bSLuigi Rizzo 
50071b43a426SLuigi Rizzo 		case 'T':
50081b43a426SLuigi Rizzo 			do_time = 2;	/* numeric timestamp */
50091b43a426SLuigi Rizzo 			break;
50101b43a426SLuigi Rizzo 
50119758b77fSLuigi Rizzo 		case 'v': /* verbose */
5012571f8c1bSLuigi Rizzo 			verbose = 1;
50139758b77fSLuigi Rizzo 			break;
5014571f8c1bSLuigi Rizzo 
50159758b77fSLuigi Rizzo 		default:
501626bf4d78SLuigi Rizzo 			free_args(save_ac, save_av);
501726bf4d78SLuigi Rizzo 			return 1;
50189758b77fSLuigi Rizzo 		}
50199758b77fSLuigi Rizzo 
50209758b77fSLuigi Rizzo 	ac -= optind;
50219758b77fSLuigi Rizzo 	av += optind;
50229758b77fSLuigi Rizzo 	NEED1("bad arguments, for usage summary ``ipfw''");
50239758b77fSLuigi Rizzo 
50249758b77fSLuigi Rizzo 	/*
502526bf4d78SLuigi Rizzo 	 * An undocumented behaviour of ipfw1 was to allow rule numbers first,
502626bf4d78SLuigi Rizzo 	 * e.g. "100 add allow ..." instead of "add 100 allow ...".
502726bf4d78SLuigi Rizzo 	 * In case, swap first and second argument to get the normal form.
502826bf4d78SLuigi Rizzo 	 */
502926bf4d78SLuigi Rizzo 	if (ac > 1 && isdigit(*av[0])) {
503026bf4d78SLuigi Rizzo 		char *p = av[0];
503126bf4d78SLuigi Rizzo 
503226bf4d78SLuigi Rizzo 		av[0] = av[1];
503326bf4d78SLuigi Rizzo 		av[1] = p;
503426bf4d78SLuigi Rizzo 	}
503526bf4d78SLuigi Rizzo 
503626bf4d78SLuigi Rizzo 	/*
50379758b77fSLuigi Rizzo 	 * optional: pipe or queue
50389758b77fSLuigi Rizzo 	 */
50396fa74f7dSMaxim Konovalov 	do_pipe = 0;
504001750186SBrooks Davis 	if (_substrcmp(*av, "pipe") == 0)
50419758b77fSLuigi Rizzo 		do_pipe = 1;
504201750186SBrooks Davis 	else if (_substrcmp(*av, "queue") == 0)
50439758b77fSLuigi Rizzo 		do_pipe = 2;
504426bf4d78SLuigi Rizzo 	if (do_pipe) {
50459758b77fSLuigi Rizzo 		ac--;
50469758b77fSLuigi Rizzo 		av++;
50479758b77fSLuigi Rizzo 	}
50489758b77fSLuigi Rizzo 	NEED1("missing command");
50499758b77fSLuigi Rizzo 
50509758b77fSLuigi Rizzo 	/*
5051571f8c1bSLuigi Rizzo 	 * For pipes and queues we normally say 'pipe NN config'
50529758b77fSLuigi Rizzo 	 * but the code is easier to parse as 'pipe config NN'
50539758b77fSLuigi Rizzo 	 * so we swap the two arguments.
50549758b77fSLuigi Rizzo 	 */
505562ff38aeSLuigi Rizzo 	if (do_pipe > 0 && ac > 1 && isdigit(*av[0])) {
50569758b77fSLuigi Rizzo 		char *p = av[0];
505726bf4d78SLuigi Rizzo 
50589758b77fSLuigi Rizzo 		av[0] = av[1];
50599758b77fSLuigi Rizzo 		av[1] = p;
50609758b77fSLuigi Rizzo 	}
5061571f8c1bSLuigi Rizzo 
506201750186SBrooks Davis 	if (_substrcmp(*av, "add") == 0)
50639758b77fSLuigi Rizzo 		add(ac, av);
506401750186SBrooks Davis 	else if (do_pipe && _substrcmp(*av, "config") == 0)
50659758b77fSLuigi Rizzo 		config_pipe(ac, av);
506601750186SBrooks Davis 	else if (_substrcmp(*av, "delete") == 0)
50679758b77fSLuigi Rizzo 		delete(ac, av);
506801750186SBrooks Davis 	else if (_substrcmp(*av, "flush") == 0)
506926bf4d78SLuigi Rizzo 		flush(do_force);
507001750186SBrooks Davis 	else if (_substrcmp(*av, "zero") == 0)
5071571f8c1bSLuigi Rizzo 		zero(ac, av, IP_FW_ZERO);
507201750186SBrooks Davis 	else if (_substrcmp(*av, "resetlog") == 0)
5073571f8c1bSLuigi Rizzo 		zero(ac, av, IP_FW_RESETLOG);
507401750186SBrooks Davis 	else if (_substrcmp(*av, "print") == 0 ||
507501750186SBrooks Davis 	         _substrcmp(*av, "list") == 0)
507662ff38aeSLuigi Rizzo 		list(ac, av, do_acct);
507701750186SBrooks Davis 	else if (_substrcmp(*av, "set") == 0)
507899e5e645SLuigi Rizzo 		sets_handler(ac, av);
507901750186SBrooks Davis 	else if (_substrcmp(*av, "table") == 0)
5080cd8b5ae0SRuslan Ermilov 		table_handler(ac, av);
508101750186SBrooks Davis 	else if (_substrcmp(*av, "enable") == 0)
50826690be9eSMatthew Dillon 		sysctl_handler(ac, av, 1);
508301750186SBrooks Davis 	else if (_substrcmp(*av, "disable") == 0)
50846690be9eSMatthew Dillon 		sysctl_handler(ac, av, 0);
508501750186SBrooks Davis 	else if (_substrcmp(*av, "show") == 0)
508662ff38aeSLuigi Rizzo 		list(ac, av, 1 /* show counters */);
508762ff38aeSLuigi Rizzo 	else
50889758b77fSLuigi Rizzo 		errx(EX_USAGE, "bad command `%s'", *av);
508962ff38aeSLuigi Rizzo 
509062ff38aeSLuigi Rizzo 	/* Free memory allocated in the argument parsing. */
509126bf4d78SLuigi Rizzo 	free_args(save_ac, save_av);
50929758b77fSLuigi Rizzo 	return 0;
50939758b77fSLuigi Rizzo }
50949758b77fSLuigi Rizzo 
50959758b77fSLuigi Rizzo 
50969758b77fSLuigi Rizzo static void
50979758b77fSLuigi Rizzo ipfw_readfile(int ac, char *av[])
50989758b77fSLuigi Rizzo {
50999758b77fSLuigi Rizzo #define MAX_ARGS	32
51009758b77fSLuigi Rizzo 	char	buf[BUFSIZ];
510162ff38aeSLuigi Rizzo 	char	*cmd = NULL, *filename = av[ac-1];
510262ff38aeSLuigi Rizzo 	int	c, lineno=0;
51039758b77fSLuigi Rizzo 	FILE	*f = NULL;
51049758b77fSLuigi Rizzo 	pid_t	preproc = 0;
51059758b77fSLuigi Rizzo 
510662ff38aeSLuigi Rizzo 	filename = av[ac-1];
510762ff38aeSLuigi Rizzo 
5108cec4ab6aSMaxim Konovalov 	while ((c = getopt(ac, av, "cfNnp:qS")) != -1) {
51099758b77fSLuigi Rizzo 		switch(c) {
511062ff38aeSLuigi Rizzo 		case 'c':
511162ff38aeSLuigi Rizzo 			do_compact = 1;
511262ff38aeSLuigi Rizzo 			break;
511362ff38aeSLuigi Rizzo 
5114cec4ab6aSMaxim Konovalov 		case 'f':
5115cec4ab6aSMaxim Konovalov 			do_force = 1;
5116cec4ab6aSMaxim Konovalov 			break;
5117cec4ab6aSMaxim Konovalov 
511862ff38aeSLuigi Rizzo 		case 'N':
511962ff38aeSLuigi Rizzo 			do_resolv = 1;
512062ff38aeSLuigi Rizzo 			break;
512162ff38aeSLuigi Rizzo 
5122571f8c1bSLuigi Rizzo 		case 'n':
5123571f8c1bSLuigi Rizzo 			test_only = 1;
5124571f8c1bSLuigi Rizzo 			break;
5125571f8c1bSLuigi Rizzo 
51269758b77fSLuigi Rizzo 		case 'p':
51279758b77fSLuigi Rizzo 			cmd = optarg;
512862ff38aeSLuigi Rizzo 			/*
512962ff38aeSLuigi Rizzo 			 * Skip previous args and delete last one, so we
513062ff38aeSLuigi Rizzo 			 * pass all but the last argument to the preprocessor
513162ff38aeSLuigi Rizzo 			 * via av[optind-1]
513262ff38aeSLuigi Rizzo 			 */
513362ff38aeSLuigi Rizzo 			av += optind - 1;
513462ff38aeSLuigi Rizzo 			ac -= optind - 1;
513562ff38aeSLuigi Rizzo 			av[ac-1] = NULL;
513662ff38aeSLuigi Rizzo 			fprintf(stderr, "command is %s\n", av[0]);
51379758b77fSLuigi Rizzo 			break;
51389758b77fSLuigi Rizzo 
51399758b77fSLuigi Rizzo 		case 'q':
514062ff38aeSLuigi Rizzo 			do_quiet = 1;
514162ff38aeSLuigi Rizzo 			break;
514262ff38aeSLuigi Rizzo 
514362ff38aeSLuigi Rizzo 		case 'S':
514462ff38aeSLuigi Rizzo 			show_sets = 1;
51459758b77fSLuigi Rizzo 			break;
51469758b77fSLuigi Rizzo 
51479758b77fSLuigi Rizzo 		default:
51489758b77fSLuigi Rizzo 			errx(EX_USAGE, "bad arguments, for usage"
51499758b77fSLuigi Rizzo 			     " summary ``ipfw''");
51509758b77fSLuigi Rizzo 		}
51519758b77fSLuigi Rizzo 
515262ff38aeSLuigi Rizzo 		if (cmd != NULL)
5153ca6e3cb0SKelly Yancey 			break;
5154ca6e3cb0SKelly Yancey 	}
5155ca6e3cb0SKelly Yancey 
515662ff38aeSLuigi Rizzo 	if (cmd == NULL && ac != optind + 1) {
515762ff38aeSLuigi Rizzo 		fprintf(stderr, "ac %d, optind %d\n", ac, optind);
51589758b77fSLuigi Rizzo 		errx(EX_USAGE, "extraneous filename arguments");
515962ff38aeSLuigi Rizzo 	}
51609758b77fSLuigi Rizzo 
516162ff38aeSLuigi Rizzo 	if ((f = fopen(filename, "r")) == NULL)
516262ff38aeSLuigi Rizzo 		err(EX_UNAVAILABLE, "fopen: %s", filename);
51639758b77fSLuigi Rizzo 
516462ff38aeSLuigi Rizzo 	if (cmd != NULL) {			/* pipe through preprocessor */
51659758b77fSLuigi Rizzo 		int pipedes[2];
51669758b77fSLuigi Rizzo 
51679758b77fSLuigi Rizzo 		if (pipe(pipedes) == -1)
51689758b77fSLuigi Rizzo 			err(EX_OSERR, "cannot create pipe");
51699758b77fSLuigi Rizzo 
517062ff38aeSLuigi Rizzo 		preproc = fork();
517162ff38aeSLuigi Rizzo 		if (preproc == -1)
51729758b77fSLuigi Rizzo 			err(EX_OSERR, "cannot fork");
51739758b77fSLuigi Rizzo 
517462ff38aeSLuigi Rizzo 		if (preproc == 0) {
517562ff38aeSLuigi Rizzo 			/*
517662ff38aeSLuigi Rizzo 			 * Child, will run the preprocessor with the
517762ff38aeSLuigi Rizzo 			 * file on stdin and the pipe on stdout.
517862ff38aeSLuigi Rizzo 			 */
51799758b77fSLuigi Rizzo 			if (dup2(fileno(f), 0) == -1
51809758b77fSLuigi Rizzo 			    || dup2(pipedes[1], 1) == -1)
51819758b77fSLuigi Rizzo 				err(EX_OSERR, "dup2()");
51829758b77fSLuigi Rizzo 			fclose(f);
51839758b77fSLuigi Rizzo 			close(pipedes[1]);
51849758b77fSLuigi Rizzo 			close(pipedes[0]);
518562ff38aeSLuigi Rizzo 			execvp(cmd, av);
51869758b77fSLuigi Rizzo 			err(EX_OSERR, "execvp(%s) failed", cmd);
518762ff38aeSLuigi Rizzo 		} else { /* parent, will reopen f as the pipe */
51889758b77fSLuigi Rizzo 			fclose(f);
51899758b77fSLuigi Rizzo 			close(pipedes[1]);
51909758b77fSLuigi Rizzo 			if ((f = fdopen(pipedes[0], "r")) == NULL) {
51919758b77fSLuigi Rizzo 				int savederrno = errno;
51929758b77fSLuigi Rizzo 
51939758b77fSLuigi Rizzo 				(void)kill(preproc, SIGTERM);
51949758b77fSLuigi Rizzo 				errno = savederrno;
51959758b77fSLuigi Rizzo 				err(EX_OSERR, "fdopen()");
51969758b77fSLuigi Rizzo 			}
51979758b77fSLuigi Rizzo 		}
51989758b77fSLuigi Rizzo 	}
51999758b77fSLuigi Rizzo 
520062ff38aeSLuigi Rizzo 	while (fgets(buf, BUFSIZ, f)) {		/* read commands */
520162ff38aeSLuigi Rizzo 		char linename[10];
520262ff38aeSLuigi Rizzo 		char *args[1];
520362ff38aeSLuigi Rizzo 
52049758b77fSLuigi Rizzo 		lineno++;
5205571f8c1bSLuigi Rizzo 		sprintf(linename, "Line %d", lineno);
5206571f8c1bSLuigi Rizzo 		setprogname(linename); /* XXX */
520762ff38aeSLuigi Rizzo 		args[0] = buf;
520862ff38aeSLuigi Rizzo 		ipfw_main(1, args);
52099758b77fSLuigi Rizzo 	}
52109758b77fSLuigi Rizzo 	fclose(f);
521162ff38aeSLuigi Rizzo 	if (cmd != NULL) {
521262ff38aeSLuigi Rizzo 		int status;
521362ff38aeSLuigi Rizzo 
52149758b77fSLuigi Rizzo 		if (waitpid(preproc, &status, 0) == -1)
52159758b77fSLuigi Rizzo 			errx(EX_OSERR, "waitpid()");
52169758b77fSLuigi Rizzo 		if (WIFEXITED(status) && WEXITSTATUS(status) != EX_OK)
52179758b77fSLuigi Rizzo 			errx(EX_UNAVAILABLE,
52189758b77fSLuigi Rizzo 			    "preprocessor exited with status %d",
52199758b77fSLuigi Rizzo 			    WEXITSTATUS(status));
52209758b77fSLuigi Rizzo 		else if (WIFSIGNALED(status))
52219758b77fSLuigi Rizzo 			errx(EX_UNAVAILABLE,
52229758b77fSLuigi Rizzo 			    "preprocessor exited with signal %d",
52239758b77fSLuigi Rizzo 			    WTERMSIG(status));
52249758b77fSLuigi Rizzo 	}
52259758b77fSLuigi Rizzo }
52269758b77fSLuigi Rizzo 
52279758b77fSLuigi Rizzo int
52289758b77fSLuigi Rizzo main(int ac, char *av[])
52299758b77fSLuigi Rizzo {
52309758b77fSLuigi Rizzo 	/*
52319758b77fSLuigi Rizzo 	 * If the last argument is an absolute pathname, interpret it
52329758b77fSLuigi Rizzo 	 * as a file to be preprocessed.
52339758b77fSLuigi Rizzo 	 */
52349758b77fSLuigi Rizzo 
52359758b77fSLuigi Rizzo 	if (ac > 1 && av[ac - 1][0] == '/' && access(av[ac - 1], R_OK) == 0)
52369758b77fSLuigi Rizzo 		ipfw_readfile(ac, av);
523726bf4d78SLuigi Rizzo 	else {
523826bf4d78SLuigi Rizzo 		if (ipfw_main(ac-1, av+1))
523926bf4d78SLuigi Rizzo 			show_usage();
524026bf4d78SLuigi Rizzo 	}
52419758b77fSLuigi Rizzo 	return EX_OK;
52429758b77fSLuigi Rizzo }
5243