2 * Copyright (c) 2002-2003 Luigi Rizzo
3 * Copyright (c) 1996 Alex Nash, Paul Traina, Poul-Henning Kamp
4 * Copyright (c) 1994 Ugen J.S.Antsilevich
6 * Idea and grammar partially left from:
7 * Copyright (c) 1993 Daniel Boulet
9 * Redistribution and use in source forms, with and without modification,
10 * are permitted provided that this entire comment appears intact.
12 * Redistribution in binary form may occur without any restrictions.
13 * Obviously, it would be nice if you gave credit where credit is due
14 * but requiring it would be too onerous.
16 * This software is provided ``AS IS'' without any warranties of any kind.
18 * NEW command line interface for IP firewall facility
23 #include <sys/types.h>
24 #include <sys/param.h>
25 #include <sys/socket.h>
26 #include <sys/sockio.h>
27 #include <sys/sysctl.h>
44 #include <time.h> /* ctime */
45 #include <timeconv.h> /* _long_to_time */
48 #include <stddef.h> /* offsetof */
50 #include <net/ethernet.h>
51 #include <net/if.h> /* only IFNAMSIZ */
52 #include <netinet/in.h>
53 #include <netinet/in_systm.h> /* only n_short, n_long */
54 #include <netinet/ip.h>
55 #include <netinet/ip_icmp.h>
56 #include <netinet/ip_fw.h>
57 #include <netinet/tcp.h>
58 #include <arpa/inet.h>
60 struct cmdline_opts co; /* global options */
66 int show_time; /* show timestamp */
67 uint32_t set_mask; /* enabled sets mask */
68 uint32_t flags; /* request flags */
69 uint32_t first; /* first rule to request */
70 uint32_t last; /* last rule to request */
71 uint32_t dcnt; /* number of dynamic states */
72 ipfw_obj_ctlv *tstate; /* table state data */
75 int resvd_set_number = RESVD_SET;
79 #define CHECK_LENGTH(v, len) do { \
81 errx(EX_DATAERR, "Rule too long"); \
84 * Check if we have enough space in cmd buffer. Note that since
85 * first 8? u32 words are reserved by reserved header, full cmd
86 * buffer can't be used, so we need to protect from buffer overrun
87 * only. At the beginning, cblen is less than actual buffer size by
88 * size of ipfw_insn_u32 instruction + 1 u32 work. This eliminates need
89 * for checking small instructions fitting in given range.
90 * We also (ab)use the fact that ipfw_insn is always the first field
91 * for any custom instruction.
93 #define CHECK_CMDLEN CHECK_LENGTH(cblen, F_LEN((ipfw_insn *)cmd))
95 #define GET_UINT_ARG(arg, min, max, tok, s_x) do { \
97 errx(EX_USAGE, "%s: missing argument", match_value(s_x, tok)); \
98 if (_substrcmp(*av, "tablearg") == 0) { \
107 _xval = strtol(*av, &end, 10); \
109 if (!isdigit(**av) || *end != '\0' || (_xval == 0 && errno == EINVAL)) \
110 errx(EX_DATAERR, "%s: invalid argument: %s", \
111 match_value(s_x, tok), *av); \
113 if (errno == ERANGE || _xval < min || _xval > max) \
114 errx(EX_DATAERR, "%s: argument is out of range (%u..%u): %s", \
115 match_value(s_x, tok), min, max, *av); \
117 if (_xval == IP_FW_TARG) \
118 errx(EX_DATAERR, "%s: illegal argument value: %s", \
119 match_value(s_x, tok), *av); \
124 static struct _s_x f_tcpflags[] = {
135 static struct _s_x f_tcpopts[] = {
136 { "mss", IP_FW_TCPOPT_MSS },
137 { "maxseg", IP_FW_TCPOPT_MSS },
138 { "window", IP_FW_TCPOPT_WINDOW },
139 { "sack", IP_FW_TCPOPT_SACK },
140 { "ts", IP_FW_TCPOPT_TS },
141 { "timestamp", IP_FW_TCPOPT_TS },
142 { "cc", IP_FW_TCPOPT_CC },
148 * IP options span the range 0 to 255 so we need to remap them
149 * (though in fact only the low 5 bits are significant).
151 static struct _s_x f_ipopts[] = {
152 { "ssrr", IP_FW_IPOPT_SSRR},
153 { "lsrr", IP_FW_IPOPT_LSRR},
154 { "rr", IP_FW_IPOPT_RR},
155 { "ts", IP_FW_IPOPT_TS},
160 static struct _s_x f_iptos[] = {
161 { "lowdelay", IPTOS_LOWDELAY},
162 { "throughput", IPTOS_THROUGHPUT},
163 { "reliability", IPTOS_RELIABILITY},
164 { "mincost", IPTOS_MINCOST},
165 { "congestion", IPTOS_ECN_CE},
166 { "ecntransport", IPTOS_ECN_ECT0},
167 { "ip tos option", 0},
171 struct _s_x f_ipdscp[] = {
172 { "af11", IPTOS_DSCP_AF11 >> 2 }, /* 001010 */
173 { "af12", IPTOS_DSCP_AF12 >> 2 }, /* 001100 */
174 { "af13", IPTOS_DSCP_AF13 >> 2 }, /* 001110 */
175 { "af21", IPTOS_DSCP_AF21 >> 2 }, /* 010010 */
176 { "af22", IPTOS_DSCP_AF22 >> 2 }, /* 010100 */
177 { "af23", IPTOS_DSCP_AF23 >> 2 }, /* 010110 */
178 { "af31", IPTOS_DSCP_AF31 >> 2 }, /* 011010 */
179 { "af32", IPTOS_DSCP_AF32 >> 2 }, /* 011100 */
180 { "af33", IPTOS_DSCP_AF33 >> 2 }, /* 011110 */
181 { "af41", IPTOS_DSCP_AF41 >> 2 }, /* 100010 */
182 { "af42", IPTOS_DSCP_AF42 >> 2 }, /* 100100 */
183 { "af43", IPTOS_DSCP_AF43 >> 2 }, /* 100110 */
184 { "be", IPTOS_DSCP_CS0 >> 2 }, /* 000000 */
185 { "ef", IPTOS_DSCP_EF >> 2 }, /* 101110 */
186 { "cs0", IPTOS_DSCP_CS0 >> 2 }, /* 000000 */
187 { "cs1", IPTOS_DSCP_CS1 >> 2 }, /* 001000 */
188 { "cs2", IPTOS_DSCP_CS2 >> 2 }, /* 010000 */
189 { "cs3", IPTOS_DSCP_CS3 >> 2 }, /* 011000 */
190 { "cs4", IPTOS_DSCP_CS4 >> 2 }, /* 100000 */
191 { "cs5", IPTOS_DSCP_CS5 >> 2 }, /* 101000 */
192 { "cs6", IPTOS_DSCP_CS6 >> 2 }, /* 110000 */
193 { "cs7", IPTOS_DSCP_CS7 >> 2 }, /* 100000 */
197 static struct _s_x limit_masks[] = {
198 {"all", DYN_SRC_ADDR|DYN_SRC_PORT|DYN_DST_ADDR|DYN_DST_PORT},
199 {"src-addr", DYN_SRC_ADDR},
200 {"src-port", DYN_SRC_PORT},
201 {"dst-addr", DYN_DST_ADDR},
202 {"dst-port", DYN_DST_PORT},
207 * we use IPPROTO_ETHERTYPE as a fake protocol id to call the print routines
208 * This is only used in this code.
210 #define IPPROTO_ETHERTYPE 0x1000
211 static struct _s_x ether_types[] = {
213 * Note, we cannot use "-:&/" in the names because they are field
214 * separators in the type specifications. Also, we use s = NULL as
215 * end-delimiter, because a type of 0 can be legal.
228 { "pppoe_disc", 0x8863 },
229 { "pppoe_sess", 0x8864 },
230 { "ipx_8022", 0x00E0 },
231 { "ipx_8023", 0x0000 },
232 { "ipx_ii", 0x8137 },
233 { "ipx_snap", 0x8137 },
239 static struct _s_x rule_eactions[] = {
240 { "nat64lsn", TOK_NAT64LSN },
241 { "nat64stl", TOK_NAT64STL },
242 { "nptv6", TOK_NPTV6 },
243 { "tcp-setmss", TOK_TCPSETMSS },
244 { NULL, 0 } /* terminator */
247 static struct _s_x rule_actions[] = {
248 { "abort6", TOK_ABORT6 },
249 { "abort", TOK_ABORT },
250 { "accept", TOK_ACCEPT },
251 { "pass", TOK_ACCEPT },
252 { "allow", TOK_ACCEPT },
253 { "permit", TOK_ACCEPT },
254 { "count", TOK_COUNT },
255 { "pipe", TOK_PIPE },
256 { "queue", TOK_QUEUE },
257 { "divert", TOK_DIVERT },
259 { "netgraph", TOK_NETGRAPH },
260 { "ngtee", TOK_NGTEE },
261 { "fwd", TOK_FORWARD },
262 { "forward", TOK_FORWARD },
263 { "skipto", TOK_SKIPTO },
264 { "deny", TOK_DENY },
265 { "drop", TOK_DENY },
266 { "reject", TOK_REJECT },
267 { "reset6", TOK_RESET6 },
268 { "reset", TOK_RESET },
269 { "unreach6", TOK_UNREACH6 },
270 { "unreach", TOK_UNREACH },
271 { "check-state", TOK_CHECKSTATE },
272 { "//", TOK_COMMENT },
274 { "reass", TOK_REASS },
275 { "setfib", TOK_SETFIB },
276 { "setdscp", TOK_SETDSCP },
277 { "call", TOK_CALL },
278 { "return", TOK_RETURN },
279 { "eaction", TOK_EACTION },
280 { "tcp-setmss", TOK_TCPSETMSS },
281 { NULL, 0 } /* terminator */
284 static struct _s_x rule_action_params[] = {
285 { "altq", TOK_ALTQ },
288 { "untag", TOK_UNTAG },
289 { NULL, 0 } /* terminator */
293 * The 'lookup' instruction accepts one of the following arguments.
294 * -1 is a terminator for the list.
295 * Arguments are passed as v[1] in O_DST_LOOKUP options.
297 static int lookup_key[] = {
298 TOK_DSTIP, TOK_SRCIP, TOK_DSTPORT, TOK_SRCPORT,
299 TOK_UID, TOK_JAIL, TOK_DSCP, -1 };
301 static struct _s_x rule_options[] = {
302 { "tagged", TOK_TAGGED },
305 { "jail", TOK_JAIL },
307 { "limit", TOK_LIMIT },
308 { "set-limit", TOK_SETLIMIT },
309 { "keep-state", TOK_KEEPSTATE },
310 { "record-state", TOK_RECORDSTATE },
311 { "bridged", TOK_LAYER2 },
312 { "layer2", TOK_LAYER2 },
314 { "diverted", TOK_DIVERTED },
315 { "diverted-loopback", TOK_DIVERTEDLOOPBACK },
316 { "diverted-output", TOK_DIVERTEDOUTPUT },
317 { "xmit", TOK_XMIT },
318 { "recv", TOK_RECV },
320 { "fragment", TOK_FRAG },
321 { "frag", TOK_FRAG },
323 { "ipoptions", TOK_IPOPTS },
324 { "ipopts", TOK_IPOPTS },
325 { "iplen", TOK_IPLEN },
326 { "ipid", TOK_IPID },
327 { "ipprecedence", TOK_IPPRECEDENCE },
328 { "dscp", TOK_DSCP },
329 { "iptos", TOK_IPTOS },
330 { "ipttl", TOK_IPTTL },
331 { "ipversion", TOK_IPVER },
332 { "ipver", TOK_IPVER },
333 { "estab", TOK_ESTAB },
334 { "established", TOK_ESTAB },
335 { "setup", TOK_SETUP },
336 { "sockarg", TOK_SOCKARG },
337 { "tcpdatalen", TOK_TCPDATALEN },
338 { "tcpflags", TOK_TCPFLAGS },
339 { "tcpflgs", TOK_TCPFLAGS },
340 { "tcpoptions", TOK_TCPOPTS },
341 { "tcpopts", TOK_TCPOPTS },
342 { "tcpseq", TOK_TCPSEQ },
343 { "tcpack", TOK_TCPACK },
344 { "tcpwin", TOK_TCPWIN },
345 { "icmptype", TOK_ICMPTYPES },
346 { "icmptypes", TOK_ICMPTYPES },
347 { "dst-ip", TOK_DSTIP },
348 { "src-ip", TOK_SRCIP },
349 { "dst-port", TOK_DSTPORT },
350 { "src-port", TOK_SRCPORT },
351 { "proto", TOK_PROTO },
354 { "mac-type", TOK_MACTYPE },
355 { "verrevpath", TOK_VERREVPATH },
356 { "versrcreach", TOK_VERSRCREACH },
357 { "antispoof", TOK_ANTISPOOF },
358 { "ipsec", TOK_IPSEC },
359 { "icmp6type", TOK_ICMP6TYPES },
360 { "icmp6types", TOK_ICMP6TYPES },
361 { "ext6hdr", TOK_EXT6HDR},
362 { "flow-id", TOK_FLOWID},
367 { "dst-ipv6", TOK_DSTIP6},
368 { "dst-ip6", TOK_DSTIP6},
369 { "src-ipv6", TOK_SRCIP6},
370 { "src-ip6", TOK_SRCIP6},
371 { "lookup", TOK_LOOKUP},
373 { "defer-action", TOK_SKIPACTION },
374 { "defer-immediate-action", TOK_SKIPACTION },
375 { "//", TOK_COMMENT },
377 { "not", TOK_NOT }, /* pseudo option */
378 { "!", /* escape ? */ TOK_NOT }, /* pseudo option */
379 { "or", TOK_OR }, /* pseudo option */
380 { "|", /* escape */ TOK_OR }, /* pseudo option */
381 { "{", TOK_STARTBRACE }, /* pseudo option */
382 { "(", TOK_STARTBRACE }, /* pseudo option */
383 { "}", TOK_ENDBRACE }, /* pseudo option */
384 { ")", TOK_ENDBRACE }, /* pseudo option */
385 { NULL, 0 } /* terminator */
388 void bprint_uint_arg(struct buf_pr *bp, const char *str, uint32_t arg);
389 static int ipfw_get_config(struct cmdline_opts *co, struct format_opts *fo,
390 ipfw_cfg_lheader **pcfg, size_t *psize);
391 static int ipfw_show_config(struct cmdline_opts *co, struct format_opts *fo,
392 ipfw_cfg_lheader *cfg, size_t sz, int ac, char **av);
393 static void ipfw_list_tifaces(void);
396 static uint16_t pack_object(struct tidx *tstate, char *name, int otype);
397 static uint16_t pack_table(struct tidx *tstate, char *name);
399 static char *table_search_ctlv(ipfw_obj_ctlv *ctlv, uint16_t idx);
400 static void object_sort_ctlv(ipfw_obj_ctlv *ctlv);
401 static char *object_search_ctlv(ipfw_obj_ctlv *ctlv, uint16_t idx,
405 * Simple string buffer API.
406 * Used to simplify buffer passing between function and for
407 * transparent overrun handling.
411 * Allocates new buffer of given size @sz.
413 * Returns 0 on success.
416 bp_alloc(struct buf_pr *b, size_t size)
418 memset(b, 0, sizeof(struct buf_pr));
420 if ((b->buf = calloc(1, size)) == NULL)
431 bp_free(struct buf_pr *b)
438 * Flushes buffer so new writer start from beginning.
441 bp_flush(struct buf_pr *b)
450 * Print message specified by @format and args.
451 * Automatically manage buffer space and transparently handle
454 * Returns number of bytes that should have been printed.
457 bprintf(struct buf_pr *b, char *format, ...)
462 va_start(args, format);
464 i = vsnprintf(b->ptr, b->avail, format, args);
467 if (i > b->avail || i < 0) {
468 /* Overflow or print error */
481 * Special values printer for tablearg-aware opcodes.
484 bprint_uint_arg(struct buf_pr *bp, const char *str, uint32_t arg)
488 bprintf(bp, "%s", str);
489 if (arg == IP_FW_TARG)
490 bprintf(bp, "tablearg");
492 bprintf(bp, "%u", arg);
496 * Helper routine to print a possibly unaligned uint64_t on
497 * various platform. If width > 0, print the value with
498 * the desired width, followed by a space;
499 * otherwise, return the required width.
502 pr_u64(struct buf_pr *b, uint64_t *pd, int width)
505 #define U64_FMT "I64"
507 #define U64_FMT "llu"
510 unsigned long long d;
512 bcopy (pd, &u, sizeof(u));
515 bprintf(b, "%*" U64_FMT " ", width, d) :
516 snprintf(NULL, 0, "%" U64_FMT, d) ;
522 safe_calloc(size_t number, size_t size)
524 void *ret = calloc(number, size);
527 err(EX_OSERR, "calloc");
532 safe_realloc(void *ptr, size_t size)
534 void *ret = realloc(ptr, size);
537 err(EX_OSERR, "realloc");
542 * Compare things like interface or table names.
545 stringnum_cmp(const char *a, const char *b)
557 return (strcmp(a, b));
562 * conditionally runs the command.
563 * Selected options or negative -> getsockopt
566 do_cmd(int optname, void *optval, uintptr_t optlen)
573 if (ipfw_socket == -1)
574 ipfw_socket = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
576 err(EX_UNAVAILABLE, "socket");
578 if (optname == IP_FW_GET || optname == IP_DUMMYNET_GET ||
579 optname == IP_FW_ADD || optname == IP_FW3 ||
580 optname == IP_FW_NAT_GET_CONFIG ||
582 optname == IP_FW_NAT_GET_LOG) {
585 i = getsockopt(ipfw_socket, IPPROTO_IP, optname, optval,
586 (socklen_t *)optlen);
588 i = setsockopt(ipfw_socket, IPPROTO_IP, optname, optval, optlen);
594 * do_set3 - pass ipfw control cmd to kernel
595 * @optname: option name
596 * @optval: pointer to option data
597 * @optlen: option length
599 * Assumes op3 header is already embedded.
600 * Calls setsockopt() with IP_FW3 as kernel-visible opcode.
601 * Returns 0 on success or errno otherwise.
604 do_set3(int optname, ip_fw3_opheader *op3, size_t optlen)
610 if (ipfw_socket == -1)
611 ipfw_socket = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
613 err(EX_UNAVAILABLE, "socket");
615 op3->opcode = optname;
617 return (setsockopt(ipfw_socket, IPPROTO_IP, IP_FW3, op3, optlen));
621 * do_get3 - pass ipfw control cmd to kernel
622 * @optname: option name
623 * @optval: pointer to option data
624 * @optlen: pointer to option length
626 * Assumes op3 header is already embedded.
627 * Calls getsockopt() with IP_FW3 as kernel-visible opcode.
628 * Returns 0 on success or errno otherwise.
631 do_get3(int optname, ip_fw3_opheader *op3, size_t *optlen)
639 if (ipfw_socket == -1)
640 ipfw_socket = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
642 err(EX_UNAVAILABLE, "socket");
644 op3->opcode = optname;
647 error = getsockopt(ipfw_socket, IPPROTO_IP, IP_FW3, op3, &len);
654 * match_token takes a table and a string, returns the value associated
655 * with the string (-1 in case of failure).
658 match_token(struct _s_x *table, const char *string)
661 uint i = strlen(string);
663 for (pt = table ; i && pt->s != NULL ; pt++)
664 if (strlen(pt->s) == i && !bcmp(string, pt->s, i))
670 * match_token_relaxed takes a table and a string, returns the value associated
671 * with the string for the best match.
674 * value from @table for matched records
675 * -1 for non-matched records
676 * -2 if more than one records match @string.
679 match_token_relaxed(struct _s_x *table, const char *string)
687 for (pt = table ; i != 0 && pt->s != NULL ; pt++) {
688 if (strncmp(pt->s, string, i) != 0)
697 return (c > 0 ? -2: -1);
701 get_token(struct _s_x *table, const char *string, const char *errbase)
705 if ((tcmd = match_token_relaxed(table, string)) < 0)
706 errx(EX_USAGE, "%s %s %s",
707 (tcmd == 0) ? "invalid" : "ambiguous", errbase, string);
713 * match_value takes a table and a value, returns the string associated
714 * with the value (NULL in case of failure).
717 match_value(struct _s_x *p, int value)
719 for (; p->s != NULL; p++)
726 concat_tokens(char *buf, size_t bufsize, struct _s_x *table, char *delimiter)
732 for (sz = 0, pt = table ; pt->s != NULL; pt++) {
733 l = snprintf(buf + sz, bufsize - sz, "%s%s",
734 (sz == 0) ? "" : delimiter, pt->s);
745 * helper function to process a set of flags and set bits in the
749 fill_flags(struct _s_x *flags, char *p, char **e, uint32_t *set,
752 char *q; /* points to the separator */
754 uint32_t *which; /* mask we are working on */
765 val = match_token(flags, p);
771 *which |= (uint32_t)val;
778 print_flags_buffer(char *buf, size_t sz, struct _s_x *list, uint32_t set)
780 char const *comma = "";
783 for (i = 0; list[i].x != 0; i++) {
784 if ((set & list[i].x) == 0)
788 l = snprintf(buf, sz, "%s%s", comma, list[i].s);
798 * _substrcmp takes two strings and returns 1 if they do not match,
799 * and 0 if they match exactly or the first string is a sub-string
800 * of the second. A warning is printed to stderr in the case that the
801 * first string is a sub-string of the second.
803 * This function will be removed in the future through the usual
804 * deprecation process.
807 _substrcmp(const char *str1, const char* str2)
810 if (strncmp(str1, str2, strlen(str1)) != 0)
813 if (strlen(str1) != strlen(str2))
814 warnx("DEPRECATED: '%s' matched '%s' as a sub-string",
820 * _substrcmp2 takes three strings and returns 1 if the first two do not match,
821 * and 0 if they match exactly or the second string is a sub-string
822 * of the first. A warning is printed to stderr in the case that the
823 * first string does not match the third.
825 * This function exists to warn about the bizarre construction
826 * strncmp(str, "by", 2) which is used to allow people to use a shortcut
827 * for "bytes". The problem is that in addition to accepting "by",
828 * "byt", "byte", and "bytes", it also excepts "by_rabid_dogs" and any
829 * other string beginning with "by".
831 * This function will be removed in the future through the usual
832 * deprecation process.
835 _substrcmp2(const char *str1, const char* str2, const char* str3)
838 if (strncmp(str1, str2, strlen(str2)) != 0)
841 if (strcmp(str1, str3) != 0)
842 warnx("DEPRECATED: '%s' matched '%s'",
848 * prints one port, symbolic or numeric
851 print_port(struct buf_pr *bp, int proto, uint16_t port)
854 if (proto == IPPROTO_ETHERTYPE) {
857 if (co.do_resolv && (s = match_value(ether_types, port)) )
858 bprintf(bp, "%s", s);
860 bprintf(bp, "0x%04x", port);
862 struct servent *se = NULL;
864 struct protoent *pe = getprotobynumber(proto);
866 se = getservbyport(htons(port), pe ? pe->p_name : NULL);
869 bprintf(bp, "%s", se->s_name);
871 bprintf(bp, "%d", port);
875 static struct _s_x _port_name[] = {
876 {"dst-port", O_IP_DSTPORT},
877 {"src-port", O_IP_SRCPORT},
881 {"mac-type", O_MAC_TYPE},
882 {"tcpdatalen", O_TCPDATALEN},
883 {"tcpwin", O_TCPWIN},
884 {"tagged", O_TAGGED},
889 * Print the values in a list 16-bit items of the types above.
890 * XXX todo: add support for mask.
893 print_newports(struct buf_pr *bp, ipfw_insn_u16 *cmd, int proto, int opcode)
895 uint16_t *p = cmd->ports;
900 sep = match_value(_port_name, opcode);
903 bprintf(bp, " %s", sep);
906 for (i = F_LEN((ipfw_insn *)cmd) - 1; i > 0; i--, p += 2) {
907 bprintf(bp, "%s", sep);
908 print_port(bp, proto, p[0]);
911 print_port(bp, proto, p[1]);
918 * Like strtol, but also translates service names into port numbers
919 * for some protocols.
921 * proto == -1 disables the protocol check;
922 * proto == IPPROTO_ETHERTYPE looks up an internal table
923 * proto == <some value in /etc/protocols> matches the values there.
924 * Returns *end == s in case the parameter is not found.
927 strtoport(char *s, char **end, int base, int proto)
933 *end = s; /* default - not found */
935 return 0; /* not found */
938 return strtol(s, end, base);
941 * find separator. '\\' escapes the next char.
943 for (s1 = s; *s1 && (isalnum(*s1) || *s1 == '\\' ||
944 *s1 == '_' || *s1 == '.') ; s1++)
945 if (*s1 == '\\' && s1[1] != '\0')
948 buf = safe_calloc(s1 - s + 1, 1);
951 * copy into a buffer skipping backslashes
953 for (p = s, i = 0; p != s1 ; p++)
958 if (proto == IPPROTO_ETHERTYPE) {
959 i = match_token(ether_types, buf);
961 if (i != -1) { /* found */
966 struct protoent *pe = NULL;
970 pe = getprotobynumber(proto);
972 se = getservbyname(buf, pe ? pe->p_name : NULL);
976 return ntohs(se->s_port);
979 return 0; /* not found */
983 * Fill the body of the command with the list of port ranges.
986 fill_newports(ipfw_insn_u16 *cmd, char *av, int proto, int cblen)
988 uint16_t a, b, *p = cmd->ports;
993 a = strtoport(av, &s, 0, proto);
994 if (s == av) /* empty or invalid argument */
997 CHECK_LENGTH(cblen, i + 2);
1000 case '-': /* a range */
1002 b = strtoport(av, &s, 0, proto);
1003 /* Reject expressions like '1-abc' or '1-2-3'. */
1004 if (s == av || (*s != ',' && *s != '\0'))
1009 case ',': /* comma separated list */
1014 warnx("port list: invalid separator <%c> in <%s>",
1024 if (i + 1 > F_LEN_MASK)
1025 errx(EX_DATAERR, "too many ports/ranges\n");
1026 cmd->o.len |= i + 1; /* leave F_NOT and F_OR untouched */
1032 * Fill the body of the command with the list of DiffServ codepoints.
1035 fill_dscp(ipfw_insn *cmd, char *av, int cblen)
1037 uint32_t *low, *high;
1041 cmd->opcode = O_DSCP;
1042 cmd->len |= F_INSN_SIZE(ipfw_insn_u32) + 1;
1046 low = (uint32_t *)(cmd + 1);
1059 if ((code = match_token(f_ipdscp, s)) == -1)
1060 errx(EX_DATAERR, "Unknown DSCP code");
1062 code = strtoul(s, NULL, 10);
1063 if (code < 0 || code > 63)
1064 errx(EX_DATAERR, "Invalid DSCP value");
1068 *high |= 1 << (code - 32);
1076 static struct _s_x icmpcodes[] = {
1077 { "net", ICMP_UNREACH_NET },
1078 { "host", ICMP_UNREACH_HOST },
1079 { "protocol", ICMP_UNREACH_PROTOCOL },
1080 { "port", ICMP_UNREACH_PORT },
1081 { "needfrag", ICMP_UNREACH_NEEDFRAG },
1082 { "srcfail", ICMP_UNREACH_SRCFAIL },
1083 { "net-unknown", ICMP_UNREACH_NET_UNKNOWN },
1084 { "host-unknown", ICMP_UNREACH_HOST_UNKNOWN },
1085 { "isolated", ICMP_UNREACH_ISOLATED },
1086 { "net-prohib", ICMP_UNREACH_NET_PROHIB },
1087 { "host-prohib", ICMP_UNREACH_HOST_PROHIB },
1088 { "tosnet", ICMP_UNREACH_TOSNET },
1089 { "toshost", ICMP_UNREACH_TOSHOST },
1090 { "filter-prohib", ICMP_UNREACH_FILTER_PROHIB },
1091 { "host-precedence", ICMP_UNREACH_HOST_PRECEDENCE },
1092 { "precedence-cutoff", ICMP_UNREACH_PRECEDENCE_CUTOFF },
1097 fill_reject_code(u_short *codep, char *str)
1102 val = strtoul(str, &s, 0);
1103 if (s == str || *s != '\0' || val >= 0x100)
1104 val = match_token(icmpcodes, str);
1106 errx(EX_DATAERR, "unknown ICMP unreachable code ``%s''", str);
1112 print_reject_code(struct buf_pr *bp, uint16_t code)
1116 if ((s = match_value(icmpcodes, code)) != NULL)
1117 bprintf(bp, "unreach %s", s);
1119 bprintf(bp, "unreach %u", code);
1123 * Returns the number of bits set (from left) in a contiguous bitmask,
1124 * or -1 if the mask is not contiguous.
1125 * XXX this needs a proper fix.
1126 * This effectively works on masks in big-endian (network) format.
1127 * when compiled on little endian architectures.
1129 * First bit is bit 7 of the first byte -- note, for MAC addresses,
1130 * the first bit on the wire is bit 0 of the first byte.
1131 * len is the max length in bits.
1134 contigmask(uint8_t *p, int len)
1138 for (i=0; i<len ; i++)
1139 if ( (p[i/8] & (1 << (7 - (i%8)))) == 0) /* first bit unset */
1141 for (n=i+1; n < len; n++)
1142 if ( (p[n/8] & (1 << (7 - (n%8)))) != 0)
1143 return -1; /* mask not contiguous */
1148 * print flags set/clear in the two bitmasks passed as parameters.
1149 * There is a specialized check for f_tcpflags.
1152 print_flags(struct buf_pr *bp, char const *name, ipfw_insn *cmd,
1155 char const *comma = "";
1157 uint8_t set = cmd->arg1 & 0xff;
1158 uint8_t clear = (cmd->arg1 >> 8) & 0xff;
1160 if (list == f_tcpflags && set == TH_SYN && clear == TH_ACK) {
1161 bprintf(bp, " setup");
1165 bprintf(bp, " %s ", name);
1166 for (i=0; list[i].x != 0; i++) {
1167 if (set & list[i].x) {
1169 bprintf(bp, "%s%s", comma, list[i].s);
1172 if (clear & list[i].x) {
1173 clear &= ~list[i].x;
1174 bprintf(bp, "%s!%s", comma, list[i].s);
1182 * Print the ip address contained in a command.
1185 print_ip(struct buf_pr *bp, const struct format_opts *fo, ipfw_insn_ip *cmd)
1187 struct hostent *he = NULL;
1189 uint32_t len = F_LEN((ipfw_insn *)cmd);
1190 uint32_t *a = ((ipfw_insn_u32 *)cmd)->d;
1194 if (cmd->o.opcode == O_IP_DST_LOOKUP && len > F_INSN_SIZE(ipfw_insn_u32)) {
1196 const char *arg = "<invalid>";
1198 if (d < sizeof(lookup_key)/sizeof(lookup_key[0]))
1199 arg = match_value(rule_options, lookup_key[d]);
1200 t = table_search_ctlv(fo->tstate, ((ipfw_insn *)cmd)->arg1);
1201 bprintf(bp, "lookup %s %s", arg, t);
1204 if (cmd->o.opcode == O_IP_SRC_ME || cmd->o.opcode == O_IP_DST_ME) {
1208 if (cmd->o.opcode == O_IP_SRC_LOOKUP ||
1209 cmd->o.opcode == O_IP_DST_LOOKUP) {
1210 t = table_search_ctlv(fo->tstate, ((ipfw_insn *)cmd)->arg1);
1211 bprintf(bp, "table(%s", t);
1212 if (len == F_INSN_SIZE(ipfw_insn_u32))
1213 bprintf(bp, ",%u", *a);
1217 if (cmd->o.opcode == O_IP_SRC_SET || cmd->o.opcode == O_IP_DST_SET) {
1218 uint32_t x, *map = (uint32_t *)&(cmd->mask);
1222 x = cmd->o.arg1 - 1;
1224 cmd->addr.s_addr = htonl(cmd->addr.s_addr);
1225 bprintf(bp, "%s/%d", inet_ntoa(cmd->addr),
1226 contigmask((uint8_t *)&x, 32));
1227 x = cmd->addr.s_addr = htonl(cmd->addr.s_addr);
1228 x &= 0xff; /* base */
1230 * Print bits and ranges.
1231 * Locate first bit set (i), then locate first bit unset (j).
1232 * If we have 3+ consecutive bits set, then print them as a
1233 * range, otherwise only print the initial bit and rescan.
1235 for (i=0; i < cmd->o.arg1; i++)
1236 if (map[i/32] & (1<<(i & 31))) {
1237 for (j=i+1; j < cmd->o.arg1; j++)
1238 if (!(map[ j/32] & (1<<(j & 31))))
1240 bprintf(bp, "%c%d", comma, i+x);
1241 if (j>i+2) { /* range has at least 3 elements */
1242 bprintf(bp, "-%d", j-1+x);
1251 * len == 2 indicates a single IP, whereas lists of 1 or more
1252 * addr/mask pairs have len = (2n+1). We convert len to n so we
1253 * use that to count the number of entries.
1255 for (len = len / 2; len > 0; len--, a += 2) {
1256 int mb = /* mask length */
1257 (cmd->o.opcode == O_IP_SRC || cmd->o.opcode == O_IP_DST) ?
1258 32 : contigmask((uint8_t *)&(a[1]), 32);
1259 if (mb == 32 && co.do_resolv)
1260 he = gethostbyaddr((char *)&(a[0]), sizeof(in_addr_t),
1262 if (he != NULL) /* resolved to name */
1263 bprintf(bp, "%s", he->h_name);
1264 else if (mb == 0) /* any */
1266 else { /* numeric IP followed by some kind of mask */
1267 ia = (struct in_addr *)&a[0];
1268 bprintf(bp, "%s", inet_ntoa(*ia));
1270 ia = (struct in_addr *)&a[1];
1271 bprintf(bp, ":%s", inet_ntoa(*ia));
1273 bprintf(bp, "/%d", mb);
1281 * prints a MAC address/mask pair
1284 format_mac(struct buf_pr *bp, uint8_t *addr, uint8_t *mask)
1286 int l = contigmask(mask, 48);
1289 bprintf(bp, " any");
1291 bprintf(bp, " %02x:%02x:%02x:%02x:%02x:%02x",
1292 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
1294 bprintf(bp, "&%02x:%02x:%02x:%02x:%02x:%02x",
1295 mask[0], mask[1], mask[2],
1296 mask[3], mask[4], mask[5]);
1298 bprintf(bp, "/%d", l);
1303 print_mac(struct buf_pr *bp, ipfw_insn_mac *mac)
1306 bprintf(bp, " MAC");
1307 format_mac(bp, mac->addr, mac->mask);
1308 format_mac(bp, mac->addr + 6, mac->mask + 6);
1312 fill_icmptypes(ipfw_insn_u32 *cmd, char *av)
1321 type = strtoul(av, &av, 0);
1323 if (*av != ',' && *av != '\0')
1324 errx(EX_DATAERR, "invalid ICMP type");
1327 errx(EX_DATAERR, "ICMP type out of range");
1329 cmd->d[0] |= 1 << type;
1331 cmd->o.opcode = O_ICMPTYPE;
1332 cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32);
1336 print_icmptypes(struct buf_pr *bp, ipfw_insn_u32 *cmd)
1341 bprintf(bp, " icmptypes");
1342 for (i = 0; i < 32; i++) {
1343 if ( (cmd->d[0] & (1 << (i))) == 0)
1345 bprintf(bp, "%c%d", sep, i);
1351 print_dscp(struct buf_pr *bp, ipfw_insn_u32 *cmd)
1358 bprintf(bp, " dscp");
1361 if (*v & (1 << i)) {
1362 if ((code = match_value(f_ipdscp, i)) != NULL)
1363 bprintf(bp, "%c%s", sep, code);
1365 bprintf(bp, "%c%d", sep, i);
1369 if ((++i % 32) == 0)
1374 #define insntod(cmd, type) ((ipfw_insn_ ## type *)(cmd))
1376 struct ip_fw_rule *rule;
1377 const ipfw_insn *eaction;
1380 #define HAVE_PROTO 0x0001
1381 #define HAVE_SRCIP 0x0002
1382 #define HAVE_DSTIP 0x0004
1383 #define HAVE_PROBE_STATE 0x0008
1389 init_show_state(struct show_state *state, struct ip_fw_rule *rule)
1392 state->printed = calloc(rule->cmd_len, sizeof(uint8_t));
1393 if (state->printed == NULL)
1396 state->eaction = NULL;
1399 state->or_block = 0;
1404 free_show_state(struct show_state *state)
1407 free(state->printed);
1411 is_printed_opcode(struct show_state *state, const ipfw_insn *cmd)
1414 return (state->printed[cmd - state->rule->cmd]);
1418 mark_printed(struct show_state *state, const ipfw_insn *cmd)
1421 state->printed[cmd - state->rule->cmd] = 1;
1425 print_limit_mask(struct buf_pr *bp, const ipfw_insn_limit *limit)
1427 struct _s_x *p = limit_masks;
1428 char const *comma = " ";
1431 for (x = limit->limit_mask; p->x != 0; p++) {
1432 if ((x & p->x) == p->x) {
1434 bprintf(bp, "%s%s", comma, p->s);
1438 bprint_uint_arg(bp, " ", limit->conn_limit);
1442 print_instruction(struct buf_pr *bp, const struct format_opts *fo,
1443 struct show_state *state, ipfw_insn *cmd)
1445 struct protoent *pe;
1451 if (is_printed_opcode(state, cmd))
1453 if ((cmd->len & F_OR) != 0 && state->or_block == 0)
1455 if (cmd->opcode != O_IN && (cmd->len & F_NOT) != 0)
1456 bprintf(bp, " not");
1458 switch (cmd->opcode) {
1460 d = 1.0 * insntod(cmd, u32)->d[0] / 0x7fffffff;
1461 bprintf(bp, "prob %f ", d);
1463 case O_PROBE_STATE: /* no need to print anything here */
1464 state->flags |= HAVE_PROBE_STATE;
1467 case O_IP_SRC_LOOKUP:
1471 if (state->flags & HAVE_SRCIP)
1472 bprintf(bp, " src-ip");
1473 print_ip(bp, fo, insntod(cmd, ip));
1476 case O_IP_DST_LOOKUP:
1480 if (state->flags & HAVE_DSTIP)
1481 bprintf(bp, " dst-ip");
1482 print_ip(bp, fo, insntod(cmd, ip));
1485 case O_IP6_SRC_MASK:
1487 if (state->flags & HAVE_SRCIP)
1488 bprintf(bp, " src-ip6");
1489 print_ip6(bp, insntod(cmd, ip6));
1492 case O_IP6_DST_MASK:
1494 if (state->flags & HAVE_DSTIP)
1495 bprintf(bp, " dst-ip6");
1496 print_ip6(bp, insntod(cmd, ip6));
1499 print_flow6id(bp, insntod(cmd, u32));
1503 print_newports(bp, insntod(cmd, u16), state->proto,
1504 (state->flags & (HAVE_SRCIP | HAVE_DSTIP)) ==
1505 (HAVE_SRCIP | HAVE_DSTIP) ? cmd->opcode: 0);
1508 pe = getprotobynumber(cmd->arg1);
1509 if (state->flags & HAVE_PROTO)
1510 bprintf(bp, " proto");
1512 bprintf(bp, " %s", pe->p_name);
1514 bprintf(bp, " %u", cmd->arg1);
1515 state->proto = cmd->arg1;
1518 print_mac(bp, insntod(cmd, mac));
1521 print_newports(bp, insntod(cmd, u16),
1522 IPPROTO_ETHERTYPE, cmd->opcode);
1525 bprintf(bp, " frag");
1528 bprintf(bp, " fib %u", cmd->arg1);
1531 bprintf(bp, " sockarg");
1534 bprintf(bp, cmd->len & F_NOT ? " out" : " in");
1537 switch (cmd->arg1) {
1539 bprintf(bp, " diverted");
1542 bprintf(bp, " diverted-output");
1545 bprintf(bp, " diverted-loopback");
1548 bprintf(bp, " diverted-?<%u>", cmd->arg1);
1553 bprintf(bp, " layer2");
1558 if (cmd->opcode == O_XMIT)
1560 else if (cmd->opcode == O_RECV)
1562 else /* if (cmd->opcode == O_VIA) */
1564 switch (insntod(cmd, if)->name[0]) {
1566 bprintf(bp, " %s %s", s,
1567 inet_ntoa(insntod(cmd, if)->p.ip));
1570 bprintf(bp, " %s table(%s)", s,
1571 table_search_ctlv(fo->tstate,
1572 insntod(cmd, if)->p.kidx));
1575 bprintf(bp, " %s %s", s,
1576 insntod(cmd, if)->name);
1579 case O_IP_FLOW_LOOKUP:
1580 s = table_search_ctlv(fo->tstate, cmd->arg1);
1581 bprintf(bp, " flow table(%s", s);
1582 if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn_u32))
1583 bprintf(bp, ",%u", insntod(cmd, u32)->d[0]);
1591 if (F_LEN(cmd) == 1) {
1592 switch (cmd->opcode) {
1609 bprintf(bp, " %s %u", s, cmd->arg1);
1611 print_newports(bp, insntod(cmd, u16), 0,
1615 bprintf(bp, " ipver %u", cmd->arg1);
1617 case O_IPPRECEDENCE:
1618 bprintf(bp, " ipprecedence %u", cmd->arg1 >> 5);
1621 print_dscp(bp, insntod(cmd, u32));
1624 print_flags(bp, "ipoptions", cmd, f_ipopts);
1627 print_flags(bp, "iptos", cmd, f_iptos);
1630 print_icmptypes(bp, insntod(cmd, u32));
1633 bprintf(bp, " established");
1636 print_flags(bp, "tcpflags", cmd, f_tcpflags);
1639 print_flags(bp, "tcpoptions", cmd, f_tcpopts);
1642 bprintf(bp, " tcpack %d",
1643 ntohl(insntod(cmd, u32)->d[0]));
1646 bprintf(bp, " tcpseq %d",
1647 ntohl(insntod(cmd, u32)->d[0]));
1650 pwd = getpwuid(insntod(cmd, u32)->d[0]);
1652 bprintf(bp, " uid %s", pwd->pw_name);
1654 bprintf(bp, " uid %u",
1655 insntod(cmd, u32)->d[0]);
1658 grp = getgrgid(insntod(cmd, u32)->d[0]);
1660 bprintf(bp, " gid %s", grp->gr_name);
1662 bprintf(bp, " gid %u",
1663 insntod(cmd, u32)->d[0]);
1666 bprintf(bp, " jail %d", insntod(cmd, u32)->d[0]);
1669 bprintf(bp, " verrevpath");
1672 bprintf(bp, " versrcreach");
1675 bprintf(bp, " antispoof");
1678 bprintf(bp, " ipsec");
1681 bprintf(bp, " // %s", (char *)(cmd + 1));
1684 if (state->flags & HAVE_PROBE_STATE)
1685 bprintf(bp, " keep-state");
1687 bprintf(bp, " record-state");
1689 object_search_ctlv(fo->tstate, cmd->arg1,
1690 IPFW_TLV_STATE_NAME));
1693 if (state->flags & HAVE_PROBE_STATE)
1694 bprintf(bp, " limit");
1696 bprintf(bp, " set-limit");
1697 print_limit_mask(bp, insntod(cmd, limit));
1699 object_search_ctlv(fo->tstate, cmd->arg1,
1700 IPFW_TLV_STATE_NAME));
1703 bprintf(bp, " ip6");
1706 bprintf(bp, " ip4");
1709 print_icmp6types(bp, insntod(cmd, u32));
1712 print_ext6hdr(bp, cmd);
1715 if (F_LEN(cmd) == 1)
1716 bprint_uint_arg(bp, " tagged ", cmd->arg1);
1718 print_newports(bp, insntod(cmd, u16),
1722 bprintf(bp, " defer-immediate-action");
1725 bprintf(bp, " [opcode %d len %d]", cmd->opcode,
1728 if (cmd->len & F_OR) {
1730 state->or_block = 1;
1731 } else if (state->or_block != 0) {
1733 state->or_block = 0;
1735 mark_printed(state, cmd);
1741 print_opcode(struct buf_pr *bp, struct format_opts *fo,
1742 struct show_state *state, int opcode)
1747 for (l = state->rule->act_ofs, cmd = state->rule->cmd;
1748 l > 0; l -= F_LEN(cmd), cmd += F_LEN(cmd)) {
1749 /* We use zero opcode to print the rest of options */
1750 if (opcode >= 0 && cmd->opcode != opcode)
1753 * Skip O_NOP, when we printing the rest
1754 * of options, it will be handled separately.
1756 if (cmd->opcode == O_NOP && opcode != O_NOP)
1758 if (!print_instruction(bp, fo, state, cmd))
1766 print_fwd(struct buf_pr *bp, const ipfw_insn *cmd)
1768 char buf[INET6_ADDRSTRLEN + IF_NAMESIZE + 2];
1773 if (cmd->opcode == O_FORWARD_IP) {
1774 sa = insntod(cmd, sa);
1775 port = sa->sa.sin_port;
1776 if (sa->sa.sin_addr.s_addr == INADDR_ANY)
1777 bprintf(bp, "fwd tablearg");
1779 bprintf(bp, "fwd %s", inet_ntoa(sa->sa.sin_addr));
1781 sa6 = insntod(cmd, sa6);
1782 port = sa6->sa.sin6_port;
1783 bprintf(bp, "fwd ");
1784 if (getnameinfo((const struct sockaddr *)&sa6->sa,
1785 sizeof(struct sockaddr_in6), buf, sizeof(buf), NULL, 0,
1786 NI_NUMERICHOST) == 0)
1787 bprintf(bp, "%s", buf);
1790 bprintf(bp, ",%u", port);
1794 print_action_instruction(struct buf_pr *bp, const struct format_opts *fo,
1795 struct show_state *state, const ipfw_insn *cmd)
1799 if (is_printed_opcode(state, cmd))
1801 switch (cmd->opcode) {
1803 bprintf(bp, "check-state");
1805 s = object_search_ctlv(fo->tstate, cmd->arg1,
1806 IPFW_TLV_STATE_NAME);
1809 bprintf(bp, " :%s", s ? s: "any");
1812 bprintf(bp, "allow");
1815 bprintf(bp, "count");
1818 bprintf(bp, "deny");
1821 if (cmd->arg1 == ICMP_REJECT_RST)
1822 bprintf(bp, "reset");
1823 else if (cmd->arg1 == ICMP_REJECT_ABORT)
1824 bprintf(bp, "abort");
1825 else if (cmd->arg1 == ICMP_UNREACH_HOST)
1826 bprintf(bp, "reject");
1828 print_reject_code(bp, cmd->arg1);
1831 if (cmd->arg1 == ICMP6_UNREACH_RST)
1832 bprintf(bp, "reset6");
1833 else if (cmd->arg1 == ICMP6_UNREACH_ABORT)
1834 bprintf(bp, "abort6");
1836 print_unreach6_code(bp, cmd->arg1);
1839 bprint_uint_arg(bp, "skipto ", cmd->arg1);
1842 bprint_uint_arg(bp, "pipe ", cmd->arg1);
1845 bprint_uint_arg(bp, "queue ", cmd->arg1);
1848 bprint_uint_arg(bp, "divert ", cmd->arg1);
1851 bprint_uint_arg(bp, "tee ", cmd->arg1);
1854 bprint_uint_arg(bp, "netgraph ", cmd->arg1);
1857 bprint_uint_arg(bp, "ngtee ", cmd->arg1);
1864 if (insntod(cmd, log)->max_log > 0)
1865 bprintf(bp, " log logamount %d",
1866 insntod(cmd, log)->max_log);
1868 bprintf(bp, " log");
1872 print_altq_cmd(bp, insntod(cmd, altq));
1876 bprint_uint_arg(bp, cmd->len & F_NOT ? " untag ":
1877 " tag ", cmd->arg1);
1880 if (cmd->arg1 != IP_FW_NAT44_GLOBAL)
1881 bprint_uint_arg(bp, "nat ", cmd->arg1);
1883 bprintf(bp, "nat global");
1886 if (cmd->arg1 == IP_FW_TARG)
1887 bprint_uint_arg(bp, "setfib ", cmd->arg1);
1889 bprintf(bp, "setfib %u", cmd->arg1 & 0x7FFF);
1891 case O_EXTERNAL_ACTION:
1893 * The external action can consists of two following
1894 * each other opcodes - O_EXTERNAL_ACTION and
1895 * O_EXTERNAL_INSTANCE. The first contains the ID of
1896 * name of external action. The second contains the ID
1897 * of name of external action instance.
1898 * NOTE: in case when external action has no named
1899 * instances support, the second opcode isn't needed.
1901 state->eaction = cmd;
1902 s = object_search_ctlv(fo->tstate, cmd->arg1,
1904 if (match_token(rule_eactions, s) != -1)
1905 bprintf(bp, "%s", s);
1907 bprintf(bp, "eaction %s", s);
1909 case O_EXTERNAL_INSTANCE:
1910 if (state->eaction == NULL)
1913 * XXX: we need to teach ipfw(9) to rewrite opcodes
1914 * in the user buffer on rule addition. When we add
1915 * the rule, we specify zero TLV type for
1916 * O_EXTERNAL_INSTANCE object. To show correct
1917 * rule after `ipfw add` we need to search instance
1918 * name with zero type. But when we do `ipfw show`
1919 * we calculate TLV type using IPFW_TLV_EACTION_NAME()
1922 s = object_search_ctlv(fo->tstate, cmd->arg1, 0);
1924 s = object_search_ctlv(fo->tstate,
1925 cmd->arg1, IPFW_TLV_EACTION_NAME(
1926 state->eaction->arg1));
1927 bprintf(bp, " %s", s);
1929 case O_EXTERNAL_DATA:
1930 if (state->eaction == NULL)
1933 * Currently we support data formatting only for
1934 * external data with datalen u16. For unknown data
1935 * print its size in bytes.
1937 if (cmd->len == F_INSN_SIZE(ipfw_insn))
1938 bprintf(bp, " %u", cmd->arg1);
1940 bprintf(bp, " %ubytes",
1941 cmd->len * sizeof(uint32_t));
1944 if (cmd->arg1 == IP_FW_TARG) {
1945 bprintf(bp, "setdscp tablearg");
1948 s = match_value(f_ipdscp, cmd->arg1 & 0x3F);
1950 bprintf(bp, "setdscp %s", s);
1952 bprintf(bp, "setdscp %u", cmd->arg1 & 0x3F);
1955 bprintf(bp, "reass");
1958 if (cmd->len & F_NOT)
1959 bprintf(bp, "return");
1961 bprint_uint_arg(bp, "call ", cmd->arg1);
1964 bprintf(bp, "** unrecognized action %d len %d ",
1965 cmd->opcode, cmd->len);
1967 mark_printed(state, cmd);
1974 print_action(struct buf_pr *bp, struct format_opts *fo,
1975 struct show_state *state, uint8_t opcode)
1980 for (l = state->rule->cmd_len - state->rule->act_ofs,
1981 cmd = ACTION_PTR(state->rule); l > 0;
1982 l -= F_LEN(cmd), cmd += F_LEN(cmd)) {
1983 if (cmd->opcode != opcode)
1985 if (!print_action_instruction(bp, fo, state, cmd))
1993 print_proto(struct buf_pr *bp, struct format_opts *fo,
1994 struct show_state *state)
1997 int l, proto, ip4, ip6;
1999 /* Count all O_PROTO, O_IP4, O_IP6 instructions. */
2000 proto = ip4 = ip6 = 0;
2001 for (l = state->rule->act_ofs, cmd = state->rule->cmd;
2002 l > 0; l -= F_LEN(cmd), cmd += F_LEN(cmd)) {
2003 switch (cmd->opcode) {
2009 if (cmd->len & F_OR)
2014 if (cmd->len & F_OR)
2021 if (proto == 0 && ip4 == 0 && ip6 == 0) {
2022 state->proto = IPPROTO_IP;
2023 state->flags |= HAVE_PROTO;
2027 /* To handle the case { ip4 or ip6 }, print opcode with F_OR first */
2030 cmd = print_opcode(bp, fo, state, ip4 > ip6 ? O_IP4: O_IP6);
2031 if (cmd != NULL && (cmd->len & F_OR))
2032 cmd = print_opcode(bp, fo, state, ip4 > ip6 ? O_IP6: O_IP4);
2033 if (cmd == NULL || (cmd->len & F_OR))
2034 for (l = proto; l > 0; l--) {
2035 cmd = print_opcode(bp, fo, state, O_PROTO);
2036 if (cmd == NULL || (cmd->len & F_OR) == 0)
2039 /* Initialize proto, it is used by print_newports() */
2040 state->flags |= HAVE_PROTO;
2041 if (state->proto == 0 && ip6 != 0)
2042 state->proto = IPPROTO_IPV6;
2046 match_opcode(int opcode, const int opcodes[], size_t nops)
2050 for (i = 0; i < nops; i++)
2051 if (opcode == opcodes[i])
2057 print_address(struct buf_pr *bp, struct format_opts *fo,
2058 struct show_state *state, const int opcodes[], size_t nops, int portop,
2062 int count, l, portcnt, pf;
2064 count = portcnt = 0;
2065 for (l = state->rule->act_ofs, cmd = state->rule->cmd;
2066 l > 0; l -= F_LEN(cmd), cmd += F_LEN(cmd)) {
2067 if (match_opcode(cmd->opcode, opcodes, nops))
2069 else if (cmd->opcode == portop)
2073 bprintf(bp, " any");
2074 for (l = state->rule->act_ofs, cmd = state->rule->cmd;
2075 l > 0 && count > 0; l -= F_LEN(cmd), cmd += F_LEN(cmd)) {
2076 if (!match_opcode(cmd->opcode, opcodes, nops))
2078 print_instruction(bp, fo, state, cmd);
2079 if ((cmd->len & F_OR) == 0)
2084 * If several O_IP_?PORT opcodes specified, leave them to the
2088 for (l = state->rule->act_ofs, cmd = state->rule->cmd, pf = 0;
2089 l > 0; l -= F_LEN(cmd), cmd += F_LEN(cmd)) {
2090 if (cmd->opcode != portop) {
2091 pf = (cmd->len & F_OR);
2094 /* Print opcode iff it is not in OR block. */
2095 if (pf == 0 && (cmd->len & F_OR) == 0)
2096 print_instruction(bp, fo, state, cmd);
2100 state->flags |= flag;
2103 static const int action_opcodes[] = {
2104 O_CHECK_STATE, O_ACCEPT, O_COUNT, O_DENY, O_REJECT,
2105 O_UNREACH6, O_SKIPTO, O_PIPE, O_QUEUE, O_DIVERT, O_TEE,
2106 O_NETGRAPH, O_NGTEE, O_FORWARD_IP, O_FORWARD_IP6, O_NAT,
2107 O_SETFIB, O_SETDSCP, O_REASS, O_CALLRETURN,
2108 /* keep the following opcodes at the end of the list */
2109 O_EXTERNAL_ACTION, O_EXTERNAL_INSTANCE, O_EXTERNAL_DATA
2112 static const int modifier_opcodes[] = {
2113 O_LOG, O_ALTQ, O_TAG
2116 static const int src_opcodes[] = {
2117 O_IP_SRC, O_IP_SRC_LOOKUP, O_IP_SRC_MASK, O_IP_SRC_ME,
2118 O_IP_SRC_SET, O_IP6_SRC, O_IP6_SRC_MASK, O_IP6_SRC_ME
2121 static const int dst_opcodes[] = {
2122 O_IP_DST, O_IP_DST_LOOKUP, O_IP_DST_MASK, O_IP_DST_ME,
2123 O_IP_DST_SET, O_IP6_DST, O_IP6_DST_MASK, O_IP6_DST_ME
2127 show_static_rule(struct cmdline_opts *co, struct format_opts *fo,
2128 struct buf_pr *bp, struct ip_fw_rule *rule, struct ip_fw_bcounter *cntr)
2130 struct show_state state;
2132 static int twidth = 0;
2135 /* Print # DISABLED or skip the rule */
2136 if ((fo->set_mask & (1 << rule->set)) == 0) {
2141 bprintf(bp, "# DISABLED ");
2143 if (init_show_state(&state, rule) != 0) {
2144 warn("init_show_state() failed");
2147 bprintf(bp, "%05u ", rule->rulenum);
2149 /* Print counters if enabled */
2150 if (fo->pcwidth > 0 || fo->bcwidth > 0) {
2151 pr_u64(bp, &cntr->pcnt, fo->pcwidth);
2152 pr_u64(bp, &cntr->bcnt, fo->bcwidth);
2155 /* Print timestamp */
2156 if (co->do_time == TIMESTAMP_NUMERIC)
2157 bprintf(bp, "%10u ", cntr->timestamp);
2158 else if (co->do_time == TIMESTAMP_STRING) {
2160 time_t t = (time_t)0;
2163 strcpy(timestr, ctime(&t));
2164 *strchr(timestr, '\n') = '\0';
2165 twidth = strlen(timestr);
2167 if (cntr->timestamp > 0) {
2168 t = _long_to_time(cntr->timestamp);
2170 strcpy(timestr, ctime(&t));
2171 *strchr(timestr, '\n') = '\0';
2172 bprintf(bp, "%s ", timestr);
2174 bprintf(bp, "%*s", twidth, " ");
2178 /* Print set number */
2180 bprintf(bp, "set %d ", rule->set);
2182 /* Print the optional "match probability" */
2183 cmd = print_opcode(bp, fo, &state, O_PROB);
2184 /* Print rule action */
2185 for (i = 0; i < nitems(action_opcodes); i++) {
2186 cmd = print_action(bp, fo, &state, action_opcodes[i]);
2189 /* Handle special cases */
2190 switch (cmd->opcode) {
2193 case O_EXTERNAL_ACTION:
2194 case O_EXTERNAL_INSTANCE:
2195 /* External action can have several instructions */
2200 /* Print rule modifiers */
2201 for (i = 0; i < nitems(modifier_opcodes); i++)
2202 print_action(bp, fo, &state, modifier_opcodes[i]);
2206 if (co->comment_only != 0)
2209 if (rule->flags & IPFW_RULE_JUSTOPTS) {
2210 state.flags |= HAVE_PROTO | HAVE_SRCIP | HAVE_DSTIP;
2214 print_proto(bp, fo, &state);
2217 bprintf(bp, " from");
2218 print_address(bp, fo, &state, src_opcodes, nitems(src_opcodes),
2219 O_IP_SRCPORT, HAVE_SRCIP);
2221 /* Print destination */
2223 print_address(bp, fo, &state, dst_opcodes, nitems(dst_opcodes),
2224 O_IP_DSTPORT, HAVE_DSTIP);
2227 /* Print the rest of options */
2228 while (print_opcode(bp, fo, &state, -1))
2231 /* Print comment at the end */
2232 cmd = print_opcode(bp, fo, &state, O_NOP);
2233 if (co->comment_only != 0 && cmd == NULL)
2234 bprintf(bp, " // ...");
2236 free_show_state(&state);
2240 show_dyn_state(struct cmdline_opts *co, struct format_opts *fo,
2241 struct buf_pr *bp, ipfw_dyn_rule *d)
2243 struct protoent *pe;
2246 char buf[INET6_ADDRSTRLEN];
2248 if (!co->do_expired) {
2249 if (!d->expire && !(d->dyn_type == O_LIMIT_PARENT))
2252 bcopy(&d->rule, &rulenum, sizeof(rulenum));
2253 bprintf(bp, "%05d", rulenum);
2254 if (fo->pcwidth > 0 || fo->bcwidth > 0) {
2256 pr_u64(bp, &d->pcnt, fo->pcwidth);
2257 pr_u64(bp, &d->bcnt, fo->bcwidth);
2258 bprintf(bp, "(%ds)", d->expire);
2260 switch (d->dyn_type) {
2261 case O_LIMIT_PARENT:
2262 bprintf(bp, " PARENT %d", d->count);
2265 bprintf(bp, " LIMIT");
2267 case O_KEEP_STATE: /* bidir, no mask */
2268 bprintf(bp, " STATE");
2272 if ((pe = getprotobynumber(d->id.proto)) != NULL)
2273 bprintf(bp, " %s", pe->p_name);
2275 bprintf(bp, " proto %u", d->id.proto);
2277 if (d->id.addr_type == 4) {
2278 a.s_addr = htonl(d->id.src_ip);
2279 bprintf(bp, " %s %d", inet_ntoa(a), d->id.src_port);
2281 a.s_addr = htonl(d->id.dst_ip);
2282 bprintf(bp, " <-> %s %d", inet_ntoa(a), d->id.dst_port);
2283 } else if (d->id.addr_type == 6) {
2284 bprintf(bp, " %s %d", inet_ntop(AF_INET6, &d->id.src_ip6, buf,
2285 sizeof(buf)), d->id.src_port);
2286 bprintf(bp, " <-> %s %d", inet_ntop(AF_INET6, &d->id.dst_ip6,
2287 buf, sizeof(buf)), d->id.dst_port);
2289 bprintf(bp, " UNKNOWN <-> UNKNOWN");
2291 bprintf(bp, " :%s", object_search_ctlv(fo->tstate,
2292 d->kidx, IPFW_TLV_STATE_NAME));
2296 do_range_cmd(int cmd, ipfw_range_tlv *rt)
2298 ipfw_range_header rh;
2301 memset(&rh, 0, sizeof(rh));
2302 memcpy(&rh.range, rt, sizeof(*rt));
2303 rh.range.head.length = sizeof(*rt);
2304 rh.range.head.type = IPFW_TLV_RANGE;
2307 if (do_get3(cmd, &rh.opheader, &sz) != 0)
2309 /* Save number of matched objects */
2310 rt->new_set = rh.range.new_set;
2315 * This one handles all set-related commands
2316 * ipfw set { show | enable | disable }
2318 * ipfw set move X to Y
2319 * ipfw set move rule X to Y
2322 ipfw_sets_handler(char *av[])
2333 memset(&rt, 0, sizeof(rt));
2336 errx(EX_USAGE, "set needs command");
2337 if (_substrcmp(*av, "show") == 0) {
2338 struct format_opts fo;
2339 ipfw_cfg_lheader *cfg;
2341 memset(&fo, 0, sizeof(fo));
2342 if (ipfw_get_config(&co, &fo, &cfg, &size) != 0)
2343 err(EX_OSERR, "requesting config failed");
2345 for (i = 0, msg = "disable"; i < RESVD_SET; i++)
2346 if ((cfg->set_mask & (1<<i)) == 0) {
2347 printf("%s %d", msg, i);
2350 msg = (cfg->set_mask != (uint32_t)-1) ? " enable" : "enable";
2351 for (i = 0; i < RESVD_SET; i++)
2352 if ((cfg->set_mask & (1<<i)) != 0) {
2353 printf("%s %d", msg, i);
2358 } else if (_substrcmp(*av, "swap") == 0) {
2360 if ( av[0] == NULL || av[1] == NULL )
2361 errx(EX_USAGE, "set swap needs 2 set numbers\n");
2362 rt.set = atoi(av[0]);
2363 rt.new_set = atoi(av[1]);
2364 if (!isdigit(*(av[0])) || rt.set > RESVD_SET)
2365 errx(EX_DATAERR, "invalid set number %s\n", av[0]);
2366 if (!isdigit(*(av[1])) || rt.new_set > RESVD_SET)
2367 errx(EX_DATAERR, "invalid set number %s\n", av[1]);
2368 i = do_range_cmd(IP_FW_SET_SWAP, &rt);
2369 } else if (_substrcmp(*av, "move") == 0) {
2371 if (av[0] && _substrcmp(*av, "rule") == 0) {
2372 rt.flags = IPFW_RCFLAG_RANGE; /* move rules to new set */
2376 cmd = IP_FW_SET_MOVE; /* Move set to new one */
2377 if (av[0] == NULL || av[1] == NULL || av[2] == NULL ||
2378 av[3] != NULL || _substrcmp(av[1], "to") != 0)
2379 errx(EX_USAGE, "syntax: set move [rule] X to Y\n");
2380 rulenum = atoi(av[0]);
2381 rt.new_set = atoi(av[2]);
2382 if (cmd == IP_FW_XMOVE) {
2383 rt.start_rule = rulenum;
2384 rt.end_rule = rulenum;
2387 rt.new_set = atoi(av[2]);
2388 if (!isdigit(*(av[0])) || (cmd == 3 && rt.set > RESVD_SET) ||
2389 (cmd == 2 && rt.start_rule == IPFW_DEFAULT_RULE) )
2390 errx(EX_DATAERR, "invalid source number %s\n", av[0]);
2391 if (!isdigit(*(av[2])) || rt.new_set > RESVD_SET)
2392 errx(EX_DATAERR, "invalid dest. set %s\n", av[1]);
2393 i = do_range_cmd(cmd, &rt);
2395 err(EX_OSERR, "failed to move %s",
2396 cmd == IP_FW_SET_MOVE ? "set": "rule");
2397 } else if (_substrcmp(*av, "disable") == 0 ||
2398 _substrcmp(*av, "enable") == 0 ) {
2399 int which = _substrcmp(*av, "enable") == 0 ? 1 : 0;
2402 masks[0] = masks[1] = 0;
2405 if (isdigit(**av)) {
2407 if (i < 0 || i > RESVD_SET)
2409 "invalid set number %d\n", i);
2410 masks[which] |= (1<<i);
2411 } else if (_substrcmp(*av, "disable") == 0)
2413 else if (_substrcmp(*av, "enable") == 0)
2417 "invalid set command %s\n", *av);
2420 if ( (masks[0] & masks[1]) != 0 )
2422 "cannot enable and disable the same set\n");
2425 rt.new_set = masks[1];
2426 i = do_range_cmd(IP_FW_SET_ENABLE, &rt);
2428 warn("set enable/disable: setsockopt(IP_FW_SET_ENABLE)");
2430 errx(EX_USAGE, "invalid set command %s\n", *av);
2434 ipfw_sysctl_handler(char *av[], int which)
2438 if (av[0] == NULL) {
2439 warnx("missing keyword to enable/disable\n");
2440 } else if (_substrcmp(*av, "firewall") == 0) {
2441 sysctlbyname("net.inet.ip.fw.enable", NULL, 0,
2442 &which, sizeof(which));
2443 sysctlbyname("net.inet6.ip6.fw.enable", NULL, 0,
2444 &which, sizeof(which));
2445 } else if (_substrcmp(*av, "one_pass") == 0) {
2446 sysctlbyname("net.inet.ip.fw.one_pass", NULL, 0,
2447 &which, sizeof(which));
2448 } else if (_substrcmp(*av, "debug") == 0) {
2449 sysctlbyname("net.inet.ip.fw.debug", NULL, 0,
2450 &which, sizeof(which));
2451 } else if (_substrcmp(*av, "verbose") == 0) {
2452 sysctlbyname("net.inet.ip.fw.verbose", NULL, 0,
2453 &which, sizeof(which));
2454 } else if (_substrcmp(*av, "dyn_keepalive") == 0) {
2455 sysctlbyname("net.inet.ip.fw.dyn_keepalive", NULL, 0,
2456 &which, sizeof(which));
2458 } else if (_substrcmp(*av, "altq") == 0) {
2459 altq_set_enabled(which);
2462 warnx("unrecognize enable/disable keyword: %s\n", *av);
2466 typedef void state_cb(struct cmdline_opts *co, struct format_opts *fo,
2467 void *arg, void *state);
2470 prepare_format_dyn(struct cmdline_opts *co, struct format_opts *fo,
2471 void *arg, void *_state)
2477 d = (ipfw_dyn_rule *)_state;
2478 /* Count _ALL_ states */
2481 if (fo->show_counters == 0)
2485 /* skip states from another set */
2486 bcopy((char *)&d->rule + sizeof(uint16_t), &set,
2488 if (set != co->use_set - 1)
2492 width = pr_u64(NULL, &d->pcnt, 0);
2493 if (width > fo->pcwidth)
2494 fo->pcwidth = width;
2496 width = pr_u64(NULL, &d->bcnt, 0);
2497 if (width > fo->bcwidth)
2498 fo->bcwidth = width;
2502 foreach_state(struct cmdline_opts *co, struct format_opts *fo,
2503 caddr_t base, size_t sz, state_cb dyn_bc, void *dyn_arg)
2509 ipfw_obj_ctlv *ctlv;
2515 ctlv = (ipfw_obj_ctlv *)base;
2516 switch (ctlv->head.type) {
2517 case IPFW_TLV_DYNSTATE_LIST:
2518 base += sizeof(*ctlv);
2519 sz -= sizeof(*ctlv);
2520 ttype = IPFW_TLV_DYN_ENT;
2529 tlv = (ipfw_obj_tlv *)base;
2530 if (tlv->type != ttype)
2533 fptr(co, fo, farg, tlv + 1);
2535 base += tlv->length;
2543 prepare_format_opts(struct cmdline_opts *co, struct format_opts *fo,
2544 ipfw_obj_tlv *rtlv, int rcnt, caddr_t dynbase, size_t dynsz)
2546 int bcwidth, pcwidth, width;
2548 struct ip_fw_bcounter *cntr;
2549 struct ip_fw_rule *r;
2553 if (fo->show_counters != 0) {
2554 for (n = 0; n < rcnt; n++,
2555 rtlv = (ipfw_obj_tlv *)((caddr_t)rtlv + rtlv->length)) {
2556 cntr = (struct ip_fw_bcounter *)(rtlv + 1);
2557 r = (struct ip_fw_rule *)((caddr_t)cntr + cntr->size);
2558 /* skip rules from another set */
2559 if (co->use_set && r->set != co->use_set - 1)
2562 /* packet counter */
2563 width = pr_u64(NULL, &cntr->pcnt, 0);
2564 if (width > pcwidth)
2568 width = pr_u64(NULL, &cntr->bcnt, 0);
2569 if (width > bcwidth)
2573 fo->bcwidth = bcwidth;
2574 fo->pcwidth = pcwidth;
2577 if (co->do_dynamic && dynsz > 0)
2578 foreach_state(co, fo, dynbase, dynsz, prepare_format_dyn, NULL);
2582 list_static_range(struct cmdline_opts *co, struct format_opts *fo,
2583 struct buf_pr *bp, ipfw_obj_tlv *rtlv, int rcnt)
2586 struct ip_fw_rule *r;
2587 struct ip_fw_bcounter *cntr;
2590 for (n = seen = 0; n < rcnt; n++,
2591 rtlv = (ipfw_obj_tlv *)((caddr_t)rtlv + rtlv->length)) {
2593 if ((fo->show_counters | fo->show_time) != 0) {
2594 cntr = (struct ip_fw_bcounter *)(rtlv + 1);
2595 r = (struct ip_fw_rule *)((caddr_t)cntr + cntr->size);
2598 r = (struct ip_fw_rule *)(rtlv + 1);
2600 if (r->rulenum > fo->last)
2602 if (co->use_set && r->set != co->use_set - 1)
2604 if (r->rulenum >= fo->first && r->rulenum <= fo->last) {
2605 show_static_rule(co, fo, bp, r, cntr);
2606 printf("%s", bp->buf);
2617 list_dyn_state(struct cmdline_opts *co, struct format_opts *fo,
2618 void *_arg, void *_state)
2625 d = (ipfw_dyn_rule *)_state;
2626 bp = (struct buf_pr *)_arg;
2628 bcopy(&d->rule, &rulenum, sizeof(rulenum));
2629 if (rulenum > fo->last)
2632 bcopy((char *)&d->rule + sizeof(uint16_t),
2633 &set, sizeof(uint8_t));
2634 if (set != co->use_set - 1)
2637 if (rulenum >= fo->first) {
2638 show_dyn_state(co, fo, bp, d);
2639 printf("%s\n", bp->buf);
2645 list_dyn_range(struct cmdline_opts *co, struct format_opts *fo,
2646 struct buf_pr *bp, caddr_t base, size_t sz)
2649 sz = foreach_state(co, fo, base, sz, list_dyn_state, bp);
2654 ipfw_list(int ac, char *av[], int show_counters)
2656 ipfw_cfg_lheader *cfg;
2657 struct format_opts sfo;
2666 fprintf(stderr, "Testing only, list disabled\n");
2670 dummynet_list(ac, av, show_counters);
2676 memset(&sfo, 0, sizeof(sfo));
2678 /* Determine rule range to request */
2680 for (lac = ac, lav = av; lac != 0; lac--) {
2681 rnum = strtoul(*lav++, &endptr, 10);
2682 if (sfo.first == 0 || rnum < sfo.first)
2686 rnum = strtoul(endptr + 1, &endptr, 10);
2687 if (sfo.last == 0 || rnum > sfo.last)
2692 /* get configuraion from kernel */
2694 sfo.show_counters = show_counters;
2695 sfo.show_time = co.do_time;
2696 sfo.flags = IPFW_CFG_GET_STATIC;
2697 if (co.do_dynamic != 0)
2698 sfo.flags |= IPFW_CFG_GET_STATES;
2699 if ((sfo.show_counters | sfo.show_time) != 0)
2700 sfo.flags |= IPFW_CFG_GET_COUNTERS;
2701 if (ipfw_get_config(&co, &sfo, &cfg, &sz) != 0)
2702 err(EX_OSERR, "retrieving config failed");
2704 error = ipfw_show_config(&co, &sfo, cfg, sz, ac, av);
2713 ipfw_show_config(struct cmdline_opts *co, struct format_opts *fo,
2714 ipfw_cfg_lheader *cfg, size_t sz, int ac, char *av[])
2719 int exitval = EX_OK;
2725 ipfw_obj_ctlv *ctlv, *tstate;
2726 ipfw_obj_tlv *rbase;
2729 * Handle tablenames TLV first, if any
2735 readsz = sizeof(*cfg);
2738 fo->set_mask = cfg->set_mask;
2740 ctlv = (ipfw_obj_ctlv *)(cfg + 1);
2742 if (cfg->flags & IPFW_CFG_GET_STATIC) {
2743 /* We've requested static rules */
2744 if (ctlv->head.type == IPFW_TLV_TBLNAME_LIST) {
2745 object_sort_ctlv(ctlv);
2747 readsz += ctlv->head.length;
2748 ctlv = (ipfw_obj_ctlv *)((caddr_t)ctlv +
2752 if (ctlv->head.type == IPFW_TLV_RULE_LIST) {
2753 rbase = (ipfw_obj_tlv *)(ctlv + 1);
2755 readsz += ctlv->head.length;
2756 ctlv = (ipfw_obj_ctlv *)((caddr_t)ctlv +
2761 if ((cfg->flags & IPFW_CFG_GET_STATES) && (readsz != sz)) {
2762 /* We may have some dynamic states */
2763 dynsz = sz - readsz;
2764 /* Skip empty header */
2765 if (dynsz != sizeof(ipfw_obj_ctlv))
2766 dynbase = (caddr_t)ctlv;
2771 prepare_format_opts(co, fo, rbase, rcnt, dynbase, dynsz);
2772 bp_alloc(&bp, 4096);
2774 /* if no rule numbers were specified, list all rules */
2777 fo->last = IPFW_DEFAULT_RULE;
2778 list_static_range(co, fo, &bp, rbase, rcnt);
2780 if (co->do_dynamic && dynsz > 0) {
2781 printf("## Dynamic rules (%d %zu):\n", fo->dcnt, dynsz);
2782 list_dyn_range(co, fo, &bp, dynbase, dynsz);
2789 /* display specific rules requested on command line */
2790 for (lac = ac, lav = av; lac != 0; lac--) {
2791 /* convert command line rule # */
2792 fo->last = fo->first = strtoul(*lav++, &endptr, 10);
2794 fo->last = strtoul(endptr + 1, &endptr, 10);
2797 warnx("invalid rule number: %s", *(lav - 1));
2801 if (list_static_range(co, fo, &bp, rbase, rcnt) == 0) {
2802 /* give precedence to other error(s) */
2803 if (exitval == EX_OK)
2804 exitval = EX_UNAVAILABLE;
2805 if (fo->first == fo->last)
2806 warnx("rule %u does not exist", fo->first);
2808 warnx("no rules in range %u-%u",
2809 fo->first, fo->last);
2813 if (co->do_dynamic && dynsz > 0) {
2814 printf("## Dynamic rules:\n");
2815 for (lac = ac, lav = av; lac != 0; lac--) {
2816 fo->last = fo->first = strtoul(*lav++, &endptr, 10);
2818 fo->last = strtoul(endptr+1, &endptr, 10);
2820 /* already warned */
2822 list_dyn_range(co, fo, &bp, dynbase, dynsz);
2832 * Retrieves current ipfw configuration of given type
2833 * and stores its pointer to @pcfg.
2835 * Caller is responsible for freeing @pcfg.
2837 * Returns 0 on success.
2841 ipfw_get_config(struct cmdline_opts *co, struct format_opts *fo,
2842 ipfw_cfg_lheader **pcfg, size_t *psize)
2844 ipfw_cfg_lheader *cfg;
2849 if (co->test_only != 0) {
2850 fprintf(stderr, "Testing only, list disabled\n");
2854 /* Start with some data size */
2858 for (i = 0; i < 16; i++) {
2861 if ((cfg = calloc(1, sz)) == NULL)
2864 cfg->flags = fo->flags;
2865 cfg->start_rule = fo->first;
2866 cfg->end_rule = fo->last;
2868 if (do_get3(IP_FW_XGET, &cfg->opheader, &sz) != 0) {
2869 if (errno != ENOMEM) {
2874 /* Buffer size is not enough. Try to increase */
2891 lookup_host (char *host, struct in_addr *ipaddr)
2895 if (!inet_aton(host, ipaddr)) {
2896 if ((he = gethostbyname(host)) == NULL)
2898 *ipaddr = *(struct in_addr *)he->h_addr_list[0];
2912 ipfw_check_object_name(const char *name)
2917 * Check that name is null-terminated and contains
2918 * valid symbols only. Valid mask is:
2919 * [a-zA-Z0-9\-_\.]{1,63}
2922 if (l == 0 || l >= 64)
2924 for (i = 0; i < l; i++) {
2926 if (isalpha(c) || isdigit(c) || c == '_' ||
2927 c == '-' || c == '.')
2934 static char *default_state_name = "default";
2936 state_check_name(const char *name)
2939 if (ipfw_check_object_name(name) != 0)
2941 if (strcmp(name, "any") == 0)
2947 eaction_check_name(const char *name)
2950 if (ipfw_check_object_name(name) != 0)
2952 /* Restrict some 'special' names */
2953 if (match_token(rule_actions, name) != -1 &&
2954 match_token(rule_action_params, name) != -1)
2960 pack_object(struct tidx *tstate, char *name, int otype)
2963 ipfw_obj_ntlv *ntlv;
2965 for (i = 0; i < tstate->count; i++) {
2966 if (strcmp(tstate->idx[i].name, name) != 0)
2968 if (tstate->idx[i].set != tstate->set)
2970 if (tstate->idx[i].head.type != otype)
2973 return (tstate->idx[i].idx);
2976 if (tstate->count + 1 > tstate->size) {
2978 tstate->idx = realloc(tstate->idx, tstate->size *
2979 sizeof(ipfw_obj_ntlv));
2980 if (tstate->idx == NULL)
2984 ntlv = &tstate->idx[i];
2985 memset(ntlv, 0, sizeof(ipfw_obj_ntlv));
2986 strlcpy(ntlv->name, name, sizeof(ntlv->name));
2987 ntlv->head.type = otype;
2988 ntlv->head.length = sizeof(ipfw_obj_ntlv);
2989 ntlv->set = tstate->set;
2990 ntlv->idx = ++tstate->counter;
2997 pack_table(struct tidx *tstate, char *name)
3000 if (table_check_name(name) != 0)
3003 return (pack_object(tstate, name, IPFW_TLV_TBL_NAME));
3007 fill_table(struct _ipfw_insn *cmd, char *av, uint8_t opcode,
3008 struct tidx *tstate)
3010 uint32_t *d = ((ipfw_insn_u32 *)cmd)->d;
3014 if ((p = strchr(av + 6, ')')) == NULL)
3015 errx(EX_DATAERR, "forgotten parenthesis: '%s'", av);
3017 p = strchr(av + 6, ',');
3021 if ((uidx = pack_table(tstate, av + 6)) == 0)
3022 errx(EX_DATAERR, "Invalid table name: %s", av + 6);
3024 cmd->opcode = opcode;
3027 cmd->len |= F_INSN_SIZE(ipfw_insn_u32);
3028 d[0] = strtoul(p, NULL, 0);
3030 cmd->len |= F_INSN_SIZE(ipfw_insn);
3035 * fills the addr and mask fields in the instruction as appropriate from av.
3036 * Update length as appropriate.
3037 * The following formats are allowed:
3038 * me returns O_IP_*_ME
3039 * 1.2.3.4 single IP address
3040 * 1.2.3.4:5.6.7.8 address:mask
3041 * 1.2.3.4/24 address/mask
3042 * 1.2.3.4/26{1,6,5,4,23} set of addresses in a subnet
3043 * We can have multiple comma-separated address/mask entries.
3046 fill_ip(ipfw_insn_ip *cmd, char *av, int cblen, struct tidx *tstate)
3049 uint32_t *d = ((ipfw_insn_u32 *)cmd)->d;
3051 cmd->o.len &= ~F_LEN_MASK; /* zero len */
3053 if (_substrcmp(av, "any") == 0)
3056 if (_substrcmp(av, "me") == 0) {
3057 cmd->o.len |= F_INSN_SIZE(ipfw_insn);
3061 if (strncmp(av, "table(", 6) == 0) {
3062 fill_table(&cmd->o, av, O_IP_DST_LOOKUP, tstate);
3068 * After the address we can have '/' or ':' indicating a mask,
3069 * ',' indicating another address follows, '{' indicating a
3070 * set of addresses of unspecified size.
3072 char *t = NULL, *p = strpbrk(av, "/:,{");
3076 CHECK_LENGTH(cblen, F_INSN_SIZE(ipfw_insn) + 2 + len);
3081 if ((t = strpbrk(p, ",{")) != NULL) {
3088 if (lookup_host(av, (struct in_addr *)&d[0]) != 0)
3089 errx(EX_NOHOST, "hostname ``%s'' unknown", av);
3092 if (!inet_aton(p, (struct in_addr *)&d[1]))
3093 errx(EX_DATAERR, "bad netmask ``%s''", p);
3098 d[1] = htonl(0U); /* mask */
3099 else if (masklen > 32)
3100 errx(EX_DATAERR, "bad width ``%s''", p);
3102 d[1] = htonl(~0U << (32 - masklen));
3104 case '{': /* no mask, assume /24 and put back the '{' */
3105 d[1] = htonl(~0U << (32 - 24));
3109 case ',': /* single address plus continuation */
3112 case 0: /* initialization value */
3114 d[1] = htonl(~0U); /* force /32 */
3117 d[0] &= d[1]; /* mask base address with mask */
3120 /* find next separator */
3122 p = strpbrk(p, ",{");
3123 if (p && *p == '{') {
3125 * We have a set of addresses. They are stored as follows:
3126 * arg1 is the set size (powers of 2, 2..256)
3127 * addr is the base address IN HOST FORMAT
3128 * mask.. is an array of arg1 bits (rounded up to
3129 * the next multiple of 32) with bits set
3130 * for each host in the map.
3132 uint32_t *map = (uint32_t *)&cmd->mask;
3134 int i = contigmask((uint8_t *)&(d[1]), 32);
3137 errx(EX_DATAERR, "address set cannot be in a list");
3138 if (i < 24 || i > 31)
3139 errx(EX_DATAERR, "invalid set with mask %d\n", i);
3140 cmd->o.arg1 = 1<<(32-i); /* map length */
3141 d[0] = ntohl(d[0]); /* base addr in host format */
3142 cmd->o.opcode = O_IP_DST_SET; /* default */
3143 cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32) + (cmd->o.arg1+31)/32;
3144 for (i = 0; i < (cmd->o.arg1+31)/32 ; i++)
3145 map[i] = 0; /* clear map */
3149 high = low + cmd->o.arg1 - 1;
3151 * Here, i stores the previous value when we specify a range
3152 * of addresses within a mask, e.g. 45-63. i = -1 means we
3153 * have no previous value.
3155 i = -1; /* previous value in a range */
3156 while (isdigit(*av)) {
3158 int a = strtol(av, &s, 0);
3160 if (s == av) { /* no parameter */
3162 errx(EX_DATAERR, "set not closed\n");
3164 errx(EX_DATAERR, "incomplete range %d-", i);
3167 if (a < low || a > high)
3168 errx(EX_DATAERR, "addr %d out of range [%d-%d]\n",
3171 if (i == -1) /* no previous in range */
3173 else { /* check that range is valid */
3175 errx(EX_DATAERR, "invalid range %d-%d",
3178 errx(EX_DATAERR, "double '-' in range");
3181 map[i/32] |= 1<<(i & 31);
3192 if (av) /* then *av must be a ',' */
3195 /* Check this entry */
3196 if (d[1] == 0) { /* "any", specified as x.x.x.x/0 */
3198 * 'any' turns the entire list into a NOP.
3199 * 'not any' never matches, so it is removed from the
3200 * list unless it is the only item, in which case we
3203 if (cmd->o.len & F_NOT) { /* "not any" never matches */
3204 if (av == NULL && len == 0) /* only this entry */
3205 errx(EX_DATAERR, "not any never matches");
3207 /* else do nothing and skip this entry */
3210 /* A single IP can be stored in an optimized format */
3211 if (d[1] == (uint32_t)~0 && av == NULL && len == 0) {
3212 cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32);
3215 len += 2; /* two words... */
3218 if (len + 1 > F_LEN_MASK)
3219 errx(EX_DATAERR, "address list too long");
3220 cmd->o.len |= len+1;
3224 /* n2mask sets n bits of the mask */
3226 n2mask(struct in6_addr *mask, int n)
3228 static int minimask[9] =
3229 { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
3232 memset(mask, 0, sizeof(struct in6_addr));
3233 p = (u_char *) mask;
3234 for (; n > 0; p++, n -= 8) {
3244 fill_flags_cmd(ipfw_insn *cmd, enum ipfw_opcodes opcode,
3245 struct _s_x *flags, char *p)
3248 uint32_t set = 0, clear = 0;
3250 if (fill_flags(flags, p, &e, &set, &clear) != 0)
3251 errx(EX_DATAERR, "invalid flag %s", e);
3253 cmd->opcode = opcode;
3254 cmd->len = (cmd->len & (F_NOT | F_OR)) | 1;
3255 cmd->arg1 = (set & 0xff) | ( (clear & 0xff) << 8);
3260 ipfw_delete(char *av[])
3265 int exitval = EX_OK;
3269 NEED1("missing rule specification");
3270 if ( *av && _substrcmp(*av, "set") == 0) {
3271 /* Do not allow using the following syntax:
3272 * ipfw set N delete set M
3275 errx(EX_DATAERR, "invalid syntax");
3276 do_set = 1; /* delete set */
3281 while (*av && isdigit(**av)) {
3282 i = strtol(*av, &sep, 10);
3285 j = strtol(sep + 1, NULL, 10);
3288 exitval = do_cmd(IP_FW_NAT_DEL, &i, sizeof i);
3290 exitval = EX_UNAVAILABLE;
3293 warn("nat %u not available", i);
3295 } else if (co.do_pipe) {
3296 exitval = ipfw_delete_pipe(co.do_pipe, i);
3298 memset(&rt, 0, sizeof(rt));
3301 rt.flags = IPFW_RCFLAG_SET;
3303 rt.start_rule = i & 0xffff;
3304 rt.end_rule = j & 0xffff;
3305 if (rt.start_rule == 0 && rt.end_rule == 0)
3306 rt.flags |= IPFW_RCFLAG_ALL;
3308 rt.flags |= IPFW_RCFLAG_RANGE;
3309 if (co.use_set != 0) {
3310 rt.set = co.use_set - 1;
3311 rt.flags |= IPFW_RCFLAG_SET;
3314 i = do_range_cmd(IP_FW_XDEL, &rt);
3316 exitval = EX_UNAVAILABLE;
3319 warn("rule %u: setsockopt(IP_FW_XDEL)",
3321 } else if (rt.new_set == 0 && do_set == 0) {
3322 exitval = EX_UNAVAILABLE;
3325 if (rt.start_rule != rt.end_rule)
3326 warnx("no rules rules in %u-%u range",
3327 rt.start_rule, rt.end_rule);
3329 warnx("rule %u not found",
3334 if (exitval != EX_OK && co.do_force == 0)
3340 * fill the interface structure. We do not check the name as we can
3341 * create interfaces dynamically, so checking them at insert time
3342 * makes relatively little sense.
3343 * Interface names containing '*', '?', or '[' are assumed to be shell
3344 * patterns which match interfaces.
3347 fill_iface(ipfw_insn_if *cmd, char *arg, int cblen, struct tidx *tstate)
3352 cmd->name[0] = '\0';
3353 cmd->o.len |= F_INSN_SIZE(ipfw_insn_if);
3357 /* Parse the interface or address */
3358 if (strcmp(arg, "any") == 0)
3359 cmd->o.len = 0; /* effectively ignore this command */
3360 else if (strncmp(arg, "table(", 6) == 0) {
3361 if ((p = strchr(arg + 6, ')')) == NULL)
3362 errx(EX_DATAERR, "forgotten parenthesis: '%s'", arg);
3364 p = strchr(arg + 6, ',');
3367 if ((uidx = pack_table(tstate, arg + 6)) == 0)
3368 errx(EX_DATAERR, "Invalid table name: %s", arg + 6);
3370 cmd->name[0] = '\1'; /* Special value indicating table */
3372 } else if (!isdigit(*arg)) {
3373 strlcpy(cmd->name, arg, sizeof(cmd->name));
3374 cmd->p.glob = strpbrk(arg, "*?[") != NULL ? 1 : 0;
3375 } else if (!inet_aton(arg, &cmd->p.ip))
3376 errx(EX_DATAERR, "bad ip address ``%s''", arg);
3380 get_mac_addr_mask(const char *p, uint8_t *addr, uint8_t *mask)
3384 char *ap, *ptr, *optr;
3385 struct ether_addr *mac;
3386 const char *macset = "0123456789abcdefABCDEF:";
3388 if (strcmp(p, "any") == 0) {
3389 for (i = 0; i < ETHER_ADDR_LEN; i++)
3390 addr[i] = mask[i] = 0;
3394 optr = ptr = strdup(p);
3395 if ((ap = strsep(&ptr, "&/")) != NULL && *ap != 0) {
3397 if (strspn(ap, macset) != l || (mac = ether_aton(ap)) == NULL)
3398 errx(EX_DATAERR, "Incorrect MAC address");
3399 bcopy(mac, addr, ETHER_ADDR_LEN);
3401 errx(EX_DATAERR, "Incorrect MAC address");
3403 if (ptr != NULL) { /* we have mask? */
3404 if (p[ptr - optr - 1] == '/') { /* mask len */
3405 long ml = strtol(ptr, &ap, 10);
3406 if (*ap != 0 || ml > ETHER_ADDR_LEN * 8 || ml < 0)
3407 errx(EX_DATAERR, "Incorrect mask length");
3408 for (i = 0; ml > 0 && i < ETHER_ADDR_LEN; ml -= 8, i++)
3409 mask[i] = (ml >= 8) ? 0xff: (~0) << (8 - ml);
3412 if (strspn(ptr, macset) != l ||
3413 (mac = ether_aton(ptr)) == NULL)
3414 errx(EX_DATAERR, "Incorrect mask");
3415 bcopy(mac, mask, ETHER_ADDR_LEN);
3417 } else { /* default mask: ff:ff:ff:ff:ff:ff */
3418 for (i = 0; i < ETHER_ADDR_LEN; i++)
3421 for (i = 0; i < ETHER_ADDR_LEN; i++)
3428 * helper function, updates the pointer to cmd with the length
3429 * of the current command, and also cleans up the first word of
3430 * the new command in case it has been clobbered before.
3433 next_cmd(ipfw_insn *cmd, int *len)
3436 CHECK_LENGTH(*len, 0);
3438 bzero(cmd, sizeof(*cmd));
3443 * Takes arguments and copies them into a comment
3446 fill_comment(ipfw_insn *cmd, char **av, int cblen)
3449 char *p = (char *)(cmd + 1);
3451 cmd->opcode = O_NOP;
3452 cmd->len = (cmd->len & (F_NOT | F_OR));
3454 /* Compute length of comment string. */
3455 for (i = 0, l = 0; av[i] != NULL; i++)
3456 l += strlen(av[i]) + 1;
3461 "comment too long (max 80 chars)");
3463 cmd->len = (cmd->len & (F_NOT | F_OR)) | l;
3466 for (i = 0; av[i] != NULL; i++) {
3475 * A function to fill simple commands of size 1.
3476 * Existing flags are preserved.
3479 fill_cmd(ipfw_insn *cmd, enum ipfw_opcodes opcode, int flags, uint16_t arg)
3481 cmd->opcode = opcode;
3482 cmd->len = ((cmd->len | flags) & (F_NOT | F_OR)) | 1;
3487 * Fetch and add the MAC address and type, with masks. This generates one or
3488 * two microinstructions, and returns the pointer to the last one.
3491 add_mac(ipfw_insn *cmd, char *av[], int cblen)
3495 if ( ( av[0] == NULL ) || ( av[1] == NULL ) )
3496 errx(EX_DATAERR, "MAC dst src");
3498 cmd->opcode = O_MACADDR2;
3499 cmd->len = (cmd->len & (F_NOT | F_OR)) | F_INSN_SIZE(ipfw_insn_mac);
3502 mac = (ipfw_insn_mac *)cmd;
3503 get_mac_addr_mask(av[0], mac->addr, mac->mask); /* dst */
3504 get_mac_addr_mask(av[1], &(mac->addr[ETHER_ADDR_LEN]),
3505 &(mac->mask[ETHER_ADDR_LEN])); /* src */
3510 add_mactype(ipfw_insn *cmd, char *av, int cblen)
3513 errx(EX_DATAERR, "missing MAC type");
3514 if (strcmp(av, "any") != 0) { /* we have a non-null type */
3515 fill_newports((ipfw_insn_u16 *)cmd, av, IPPROTO_ETHERTYPE,
3517 cmd->opcode = O_MAC_TYPE;
3524 add_proto0(ipfw_insn *cmd, char *av, u_char *protop)
3526 struct protoent *pe;
3530 proto = strtol(av, &ep, 10);
3531 if (*ep != '\0' || proto <= 0) {
3532 if ((pe = getprotobyname(av)) == NULL)
3534 proto = pe->p_proto;
3537 fill_cmd(cmd, O_PROTO, 0, proto);
3543 add_proto(ipfw_insn *cmd, char *av, u_char *protop)
3545 u_char proto = IPPROTO_IP;
3547 if (_substrcmp(av, "all") == 0 || strcmp(av, "ip") == 0)
3548 ; /* do not set O_IP4 nor O_IP6 */
3549 else if (strcmp(av, "ip4") == 0)
3550 /* explicit "just IPv4" rule */
3551 fill_cmd(cmd, O_IP4, 0, 0);
3552 else if (strcmp(av, "ip6") == 0) {
3553 /* explicit "just IPv6" rule */
3554 proto = IPPROTO_IPV6;
3555 fill_cmd(cmd, O_IP6, 0, 0);
3557 return add_proto0(cmd, av, protop);
3564 add_proto_compat(ipfw_insn *cmd, char *av, u_char *protop)
3566 u_char proto = IPPROTO_IP;
3568 if (_substrcmp(av, "all") == 0 || strcmp(av, "ip") == 0)
3569 ; /* do not set O_IP4 nor O_IP6 */
3570 else if (strcmp(av, "ipv4") == 0 || strcmp(av, "ip4") == 0)
3571 /* explicit "just IPv4" rule */
3572 fill_cmd(cmd, O_IP4, 0, 0);
3573 else if (strcmp(av, "ipv6") == 0 || strcmp(av, "ip6") == 0) {
3574 /* explicit "just IPv6" rule */
3575 proto = IPPROTO_IPV6;
3576 fill_cmd(cmd, O_IP6, 0, 0);
3578 return add_proto0(cmd, av, protop);
3585 add_srcip(ipfw_insn *cmd, char *av, int cblen, struct tidx *tstate)
3587 fill_ip((ipfw_insn_ip *)cmd, av, cblen, tstate);
3588 if (cmd->opcode == O_IP_DST_SET) /* set */
3589 cmd->opcode = O_IP_SRC_SET;
3590 else if (cmd->opcode == O_IP_DST_LOOKUP) /* table */
3591 cmd->opcode = O_IP_SRC_LOOKUP;
3592 else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn)) /* me */
3593 cmd->opcode = O_IP_SRC_ME;
3594 else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn_u32)) /* one IP */
3595 cmd->opcode = O_IP_SRC;
3596 else /* addr/mask */
3597 cmd->opcode = O_IP_SRC_MASK;
3602 add_dstip(ipfw_insn *cmd, char *av, int cblen, struct tidx *tstate)
3604 fill_ip((ipfw_insn_ip *)cmd, av, cblen, tstate);
3605 if (cmd->opcode == O_IP_DST_SET) /* set */
3607 else if (cmd->opcode == O_IP_DST_LOOKUP) /* table */
3609 else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn)) /* me */
3610 cmd->opcode = O_IP_DST_ME;
3611 else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn_u32)) /* one IP */
3612 cmd->opcode = O_IP_DST;
3613 else /* addr/mask */
3614 cmd->opcode = O_IP_DST_MASK;
3618 static struct _s_x f_reserved_keywords[] = {
3621 { "diverted", TOK_OR },
3622 { "dst-port", TOK_OR },
3623 { "src-port", TOK_OR },
3624 { "established", TOK_OR },
3625 { "keep-state", TOK_OR },
3627 { "icmptypes", TOK_OR },
3635 { NULL, 0 } /* terminator */
3639 add_ports(ipfw_insn *cmd, char *av, u_char proto, int opcode, int cblen)
3642 if (match_token(f_reserved_keywords, av) != -1)
3645 if (fill_newports((ipfw_insn_u16 *)cmd, av, proto, cblen)) {
3646 /* XXX todo: check that we have a protocol with ports */
3647 cmd->opcode = opcode;
3654 add_src(ipfw_insn *cmd, char *av, u_char proto, int cblen, struct tidx *tstate)
3657 char *host, *ch, buf[INET6_ADDRSTRLEN];
3658 ipfw_insn *ret = NULL;
3661 /* Copy first address in set if needed */
3662 if ((ch = strpbrk(av, "/,")) != NULL) {
3664 strlcpy(buf, av, sizeof(buf));
3665 if (len < sizeof(buf))
3671 if (proto == IPPROTO_IPV6 || strcmp(av, "me6") == 0 ||
3672 inet_pton(AF_INET6, host, &a) == 1)
3673 ret = add_srcip6(cmd, av, cblen, tstate);
3674 /* XXX: should check for IPv4, not !IPv6 */
3675 if (ret == NULL && (proto == IPPROTO_IP || strcmp(av, "me") == 0 ||
3676 inet_pton(AF_INET6, host, &a) != 1))
3677 ret = add_srcip(cmd, av, cblen, tstate);
3678 if (ret == NULL && strcmp(av, "any") != 0)
3685 add_dst(ipfw_insn *cmd, char *av, u_char proto, int cblen, struct tidx *tstate)
3688 char *host, *ch, buf[INET6_ADDRSTRLEN];
3689 ipfw_insn *ret = NULL;
3692 /* Copy first address in set if needed */
3693 if ((ch = strpbrk(av, "/,")) != NULL) {
3695 strlcpy(buf, av, sizeof(buf));
3696 if (len < sizeof(buf))
3702 if (proto == IPPROTO_IPV6 || strcmp(av, "me6") == 0 ||
3703 inet_pton(AF_INET6, host, &a) == 1)
3704 ret = add_dstip6(cmd, av, cblen, tstate);
3705 /* XXX: should check for IPv4, not !IPv6 */
3706 if (ret == NULL && (proto == IPPROTO_IP || strcmp(av, "me") == 0 ||
3707 inet_pton(AF_INET6, host, &a) != 1))
3708 ret = add_dstip(cmd, av, cblen, tstate);
3709 if (ret == NULL && strcmp(av, "any") != 0)
3716 * Parse arguments and assemble the microinstructions which make up a rule.
3717 * Rules are added into the 'rulebuf' and then copied in the correct order
3718 * into the actual rule.
3720 * The syntax for a rule starts with the action, followed by
3721 * optional action parameters, and the various match patterns.
3722 * In the assembled microcode, the first opcode must be an O_PROBE_STATE
3723 * (generated if the rule includes a keep-state option), then the
3724 * various match patterns, log/altq actions, and the actual action.
3728 compile_rule(char *av[], uint32_t *rbuf, int *rbufsize, struct tidx *tstate)
3731 * rules are added into the 'rulebuf' and then copied in
3732 * the correct order into the actual rule.
3733 * Some things that need to go out of order (prob, action etc.)
3736 static uint32_t actbuf[255], cmdbuf[255];
3737 int rblen, ablen, cblen;
3739 ipfw_insn *src, *dst, *cmd, *action, *prev=NULL;
3740 ipfw_insn *first_cmd; /* first match pattern */
3742 struct ip_fw_rule *rule;
3745 * various flags used to record that we entered some fields.
3747 ipfw_insn *have_state = NULL; /* any state-related option */
3748 int have_rstate = 0;
3749 ipfw_insn *have_log = NULL, *have_altq = NULL, *have_tag = NULL;
3750 ipfw_insn *have_skipcmd = NULL;
3755 int open_par = 0; /* open parenthesis ( */
3757 /* proto is here because it is used to fetch ports */
3758 u_char proto = IPPROTO_IP; /* default protocol */
3760 double match_prob = 1; /* match probability, default is always match */
3762 bzero(actbuf, sizeof(actbuf)); /* actions go here */
3763 bzero(cmdbuf, sizeof(cmdbuf));
3764 bzero(rbuf, *rbufsize);
3766 rule = (struct ip_fw_rule *)rbuf;
3767 cmd = (ipfw_insn *)cmdbuf;
3768 action = (ipfw_insn *)actbuf;
3770 rblen = *rbufsize / sizeof(uint32_t);
3771 rblen -= sizeof(struct ip_fw_rule) / sizeof(uint32_t);
3772 ablen = sizeof(actbuf) / sizeof(actbuf[0]);
3773 cblen = sizeof(cmdbuf) / sizeof(cmdbuf[0]);
3774 cblen -= F_INSN_SIZE(ipfw_insn_u32) + 1;
3776 #define CHECK_RBUFLEN(len) { CHECK_LENGTH(rblen, len); rblen -= len; }
3777 #define CHECK_ACTLEN CHECK_LENGTH(ablen, action->len)
3781 /* [rule N] -- Rule number optional */
3782 if (av[0] && isdigit(**av)) {
3783 rule->rulenum = atoi(*av);
3787 /* [set N] -- set number (0..RESVD_SET), optional */
3788 if (av[0] && av[1] && _substrcmp(*av, "set") == 0) {
3789 int set = strtoul(av[1], NULL, 10);
3790 if (set < 0 || set > RESVD_SET)
3791 errx(EX_DATAERR, "illegal set %s", av[1]);
3797 /* [prob D] -- match probability, optional */
3798 if (av[0] && av[1] && _substrcmp(*av, "prob") == 0) {
3799 match_prob = strtod(av[1], NULL);
3801 if (match_prob <= 0 || match_prob > 1)
3802 errx(EX_DATAERR, "illegal match prob. %s", av[1]);
3806 /* action -- mandatory */
3807 NEED1("missing action");
3808 i = match_token(rule_actions, *av);
3810 action->len = 1; /* default */
3813 case TOK_CHECKSTATE:
3814 have_state = action;
3815 action->opcode = O_CHECK_STATE;
3817 match_token(rule_options, *av) == TOK_COMMENT) {
3818 action->arg1 = pack_object(tstate,
3819 default_state_name, IPFW_TLV_STATE_NAME);
3822 if (*av[0] == ':') {
3823 if (strcmp(*av + 1, "any") == 0)
3825 else if (state_check_name(*av + 1) == 0)
3826 action->arg1 = pack_object(tstate, *av + 1,
3827 IPFW_TLV_STATE_NAME);
3829 errx(EX_DATAERR, "Invalid state name %s",
3834 errx(EX_DATAERR, "Invalid state name %s", *av);
3838 action->opcode = O_REJECT;
3839 action->arg1 = ICMP_REJECT_ABORT;
3843 action->opcode = O_UNREACH6;
3844 action->arg1 = ICMP6_UNREACH_ABORT;
3848 action->opcode = O_ACCEPT;
3852 action->opcode = O_DENY;
3857 action->opcode = O_REJECT;
3858 action->arg1 = ICMP_UNREACH_HOST;
3862 action->opcode = O_REJECT;
3863 action->arg1 = ICMP_REJECT_RST;
3867 action->opcode = O_UNREACH6;
3868 action->arg1 = ICMP6_UNREACH_RST;
3872 action->opcode = O_REJECT;
3873 NEED1("missing reject code");
3874 fill_reject_code(&action->arg1, *av);
3879 action->opcode = O_UNREACH6;
3880 NEED1("missing unreach code");
3881 fill_unreach6_code(&action->arg1, *av);
3886 action->opcode = O_COUNT;
3890 action->opcode = O_NAT;
3891 action->len = F_INSN_SIZE(ipfw_insn_nat);
3893 if (*av != NULL && _substrcmp(*av, "global") == 0) {
3894 action->arg1 = IP_FW_NAT44_GLOBAL;
3900 action->opcode = O_QUEUE;
3903 action->opcode = O_PIPE;
3906 action->opcode = O_SKIPTO;
3909 action->opcode = O_NETGRAPH;
3912 action->opcode = O_NGTEE;
3915 action->opcode = O_DIVERT;
3918 action->opcode = O_TEE;
3921 action->opcode = O_CALLRETURN;
3924 errx(EX_USAGE, "missing argument for %s", *(av - 1));
3925 if (isdigit(**av)) {
3926 action->arg1 = strtoul(*av, NULL, 10);
3927 if (action->arg1 <= 0 || action->arg1 >= IP_FW_TABLEARG)
3928 errx(EX_DATAERR, "illegal argument for %s",
3930 } else if (_substrcmp(*av, "tablearg") == 0) {
3931 action->arg1 = IP_FW_TARG;
3932 } else if (i == TOK_DIVERT || i == TOK_TEE) {
3935 s = getservbyname(av[0], "divert");
3937 action->arg1 = ntohs(s->s_port);
3939 errx(EX_DATAERR, "illegal divert/tee port");
3941 errx(EX_DATAERR, "illegal argument for %s", *(av - 1));
3947 * Locate the address-port separator (':' or ',').
3948 * Could be one of the following:
3952 * IPv6 w:x:y::z,port
3953 * The ':' can only be used with hostname and IPv4 address.
3954 * XXX-BZ Should we also support [w:x:y::z]:port?
3956 struct sockaddr_storage result;
3957 struct addrinfo *res;
3960 u_short port_number;
3962 NEED1("missing forward address[:port]");
3965 * locate the address-port separator (':' or ',')
3967 s = strchr(*av, ',');
3969 /* Distinguish between IPv4:port and IPv6 cases. */
3970 s = strchr(*av, ':');
3971 if (s && strchr(s+1, ':'))
3972 s = NULL; /* no port */
3977 /* Terminate host portion and set s to start of port. */
3979 i = strtoport(s, &end, 0 /* base */, 0 /* proto */);
3982 "illegal forwarding port ``%s''", s);
3983 port_number = (u_short)i;
3986 if (_substrcmp(*av, "tablearg") == 0) {
3988 ((struct sockaddr_in*)&result)->sin_addr.s_addr =
3992 * Resolve the host name or address to a family and a
3993 * network representation of the address.
3995 if (getaddrinfo(*av, NULL, NULL, &res))
3996 errx(EX_DATAERR, NULL);
3997 /* Just use the first host in the answer. */
3998 family = res->ai_family;
3999 memcpy(&result, res->ai_addr, res->ai_addrlen);
4003 if (family == PF_INET) {
4004 ipfw_insn_sa *p = (ipfw_insn_sa *)action;
4006 action->opcode = O_FORWARD_IP;
4007 action->len = F_INSN_SIZE(ipfw_insn_sa);
4011 * In the kernel we assume AF_INET and use only
4012 * sin_port and sin_addr. Remember to set sin_len as
4013 * the routing code seems to use it too.
4015 p->sa.sin_len = sizeof(struct sockaddr_in);
4016 p->sa.sin_family = AF_INET;
4017 p->sa.sin_port = port_number;
4018 p->sa.sin_addr.s_addr =
4019 ((struct sockaddr_in *)&result)->sin_addr.s_addr;
4020 } else if (family == PF_INET6) {
4021 ipfw_insn_sa6 *p = (ipfw_insn_sa6 *)action;
4023 action->opcode = O_FORWARD_IP6;
4024 action->len = F_INSN_SIZE(ipfw_insn_sa6);
4027 p->sa.sin6_len = sizeof(struct sockaddr_in6);
4028 p->sa.sin6_family = AF_INET6;
4029 p->sa.sin6_port = port_number;
4030 p->sa.sin6_flowinfo = 0;
4031 p->sa.sin6_scope_id =
4032 ((struct sockaddr_in6 *)&result)->sin6_scope_id;
4033 bcopy(&((struct sockaddr_in6*)&result)->sin6_addr,
4034 &p->sa.sin6_addr, sizeof(p->sa.sin6_addr));
4036 errx(EX_DATAERR, "Invalid address family in forward action");
4042 /* pretend it is a 'count' rule followed by the comment */
4043 action->opcode = O_COUNT;
4044 av--; /* go back... */
4050 size_t intsize = sizeof(int);
4052 action->opcode = O_SETFIB;
4053 NEED1("missing fib number");
4054 if (_substrcmp(*av, "tablearg") == 0) {
4055 action->arg1 = IP_FW_TARG;
4057 action->arg1 = strtoul(*av, NULL, 10);
4058 if (sysctlbyname("net.fibs", &numfibs, &intsize,
4060 errx(EX_DATAERR, "fibs not suported.\n");
4061 if (action->arg1 >= numfibs) /* Temporary */
4062 errx(EX_DATAERR, "fib too large.\n");
4063 /* Add high-order bit to fib to make room for tablearg*/
4064 action->arg1 |= 0x8000;
4074 action->opcode = O_SETDSCP;
4075 NEED1("missing DSCP code");
4076 if (_substrcmp(*av, "tablearg") == 0) {
4077 action->arg1 = IP_FW_TARG;
4079 if (isalpha(*av[0])) {
4080 if ((code = match_token(f_ipdscp, *av)) == -1)
4081 errx(EX_DATAERR, "Unknown DSCP code");
4082 action->arg1 = code;
4084 action->arg1 = strtoul(*av, NULL, 10);
4086 * Add high-order bit to DSCP to make room
4089 action->arg1 |= 0x8000;
4096 action->opcode = O_REASS;
4100 fill_cmd(action, O_CALLRETURN, F_NOT, 0);
4103 case TOK_TCPSETMSS: {
4107 idx = pack_object(tstate, "tcp-setmss", IPFW_TLV_EACTION);
4109 errx(EX_DATAERR, "pack_object failed");
4110 fill_cmd(action, O_EXTERNAL_ACTION, 0, idx);
4111 NEED1("Missing MSS value");
4112 action = next_cmd(action, &ablen);
4115 mss = strtoul(*av, NULL, 10);
4116 if (mss == 0 || mss > UINT16_MAX)
4117 errx(EX_USAGE, "invalid MSS value %s", *av);
4118 fill_cmd(action, O_EXTERNAL_DATA, 0, (uint16_t)mss);
4125 if (match_token(rule_eactions, *av) == -1)
4126 errx(EX_DATAERR, "invalid action %s\n", *av);
4128 * External actions support.
4129 * XXX: we support only syntax with instance name.
4130 * For known external actions (from rule_eactions list)
4131 * we can handle syntax directly. But with `eaction'
4132 * keyword we can use only `eaction <name> <instance>'
4138 NEED1("Missing eaction name");
4139 if (eaction_check_name(*av) != 0)
4140 errx(EX_DATAERR, "Invalid eaction name %s", *av);
4141 idx = pack_object(tstate, *av, IPFW_TLV_EACTION);
4143 errx(EX_DATAERR, "pack_object failed");
4144 fill_cmd(action, O_EXTERNAL_ACTION, 0, idx);
4146 NEED1("Missing eaction instance name");
4147 action = next_cmd(action, &ablen);
4150 if (eaction_check_name(*av) != 0)
4151 errx(EX_DATAERR, "Invalid eaction instance name %s",
4154 * External action instance object has TLV type depended
4155 * from the external action name object index. Since we
4156 * currently don't know this index, use zero as TLV type.
4158 idx = pack_object(tstate, *av, 0);
4160 errx(EX_DATAERR, "pack_object failed");
4161 fill_cmd(action, O_EXTERNAL_INSTANCE, 0, idx);
4165 action = next_cmd(action, &ablen);
4168 * [altq queuename] -- altq tag, optional
4169 * [log [logamount N]] -- log, optional
4171 * If they exist, it go first in the cmdbuf, but then it is
4172 * skipped in the copy section to the end of the buffer.
4174 while (av[0] != NULL && (i = match_token(rule_action_params, *av)) != -1) {
4179 ipfw_insn_log *c = (ipfw_insn_log *)cmd;
4184 "log cannot be specified more than once");
4185 have_log = (ipfw_insn *)c;
4186 cmd->len = F_INSN_SIZE(ipfw_insn_log);
4188 cmd->opcode = O_LOG;
4189 if (av[0] && _substrcmp(*av, "logamount") == 0) {
4191 NEED1("logamount requires argument");
4195 "logamount must be positive");
4199 len = sizeof(c->max_log);
4200 if (sysctlbyname("net.inet.ip.fw.verbose_limit",
4201 &c->max_log, &len, NULL, 0) == -1) {
4206 errx(1, "sysctlbyname(\"%s\")",
4207 "net.inet.ip.fw.verbose_limit");
4216 ipfw_insn_altq *a = (ipfw_insn_altq *)cmd;
4218 NEED1("missing altq queue name");
4221 "altq cannot be specified more than once");
4222 have_altq = (ipfw_insn *)a;
4223 cmd->len = F_INSN_SIZE(ipfw_insn_altq);
4225 cmd->opcode = O_ALTQ;
4226 a->qid = altq_name_to_qid(*av);
4237 errx(EX_USAGE, "tag and untag cannot be "
4238 "specified more than once");
4239 GET_UINT_ARG(tag, IPFW_ARG_MIN, IPFW_ARG_MAX, i,
4240 rule_action_params);
4242 fill_cmd(cmd, O_TAG, (i == TOK_TAG) ? 0: F_NOT, tag);
4250 cmd = next_cmd(cmd, &cblen);
4253 if (have_state) { /* must be a check-state, we are done */
4255 match_token(rule_options, *av) == TOK_COMMENT) {
4256 /* check-state has a comment */
4258 fill_comment(cmd, av, cblen);
4259 cmd = next_cmd(cmd, &cblen);
4265 #define OR_START(target) \
4266 if (av[0] && (*av[0] == '(' || *av[0] == '{')) { \
4268 errx(EX_USAGE, "nested \"(\" not allowed\n"); \
4271 if ( (av[0])[1] == '\0') { \
4282 strcmp(*av, ")") == 0 || \
4283 strcmp(*av, "}") == 0)) { \
4288 errx(EX_USAGE, "missing \")\"\n"); \
4292 if (av[0] && _substrcmp(*av, "not") == 0) { \
4293 if (cmd->len & F_NOT) \
4294 errx(EX_USAGE, "double \"not\" not allowed\n"); \
4295 cmd->len |= F_NOT; \
4299 #define OR_BLOCK(target) \
4300 if (av[0] && _substrcmp(*av, "or") == 0) { \
4301 if (prev == NULL || open_par == 0) \
4302 errx(EX_DATAERR, "invalid OR block"); \
4303 prev->len |= F_OR; \
4313 * MAC addresses, optional.
4314 * If we have this, we skip the part "proto from src to dst"
4315 * and jump straight to the option parsing.
4318 NEED1("missing protocol");
4319 if (_substrcmp(*av, "MAC") == 0 ||
4320 _substrcmp(*av, "mac") == 0) {
4321 av++; /* the "MAC" keyword */
4322 add_mac(cmd, av); /* exits in case of errors */
4323 cmd = next_cmd(cmd);
4324 av += 2; /* dst-mac and src-mac */
4326 NEED1("missing mac type");
4327 if (add_mactype(cmd, av[0]))
4328 cmd = next_cmd(cmd);
4329 av++; /* any or mac-type */
4335 * protocol, mandatory
4337 OR_START(get_proto);
4339 NEED1("missing protocol");
4340 if (add_proto_compat(cmd, *av, &proto)) {
4342 if (F_LEN(cmd) != 0) {
4344 cmd = next_cmd(cmd, &cblen);
4346 } else if (first_cmd != cmd) {
4347 errx(EX_DATAERR, "invalid protocol ``%s''", *av);
4349 rule->flags |= IPFW_RULE_JUSTOPTS;
4352 OR_BLOCK(get_proto);
4357 if ((av[0] == NULL) || _substrcmp(*av, "from") != 0)
4358 errx(EX_USAGE, "missing ``from''");
4362 * source IP, mandatory
4364 OR_START(source_ip);
4365 NOT_BLOCK; /* optional "not" */
4366 NEED1("missing source address");
4367 if (add_src(cmd, *av, proto, cblen, tstate)) {
4369 if (F_LEN(cmd) != 0) { /* ! any */
4371 cmd = next_cmd(cmd, &cblen);
4374 errx(EX_USAGE, "bad source address %s", *av);
4375 OR_BLOCK(source_ip);
4378 * source ports, optional
4380 NOT_BLOCK; /* optional "not" */
4381 if ( av[0] != NULL ) {
4382 if (_substrcmp(*av, "any") == 0 ||
4383 add_ports(cmd, *av, proto, O_IP_SRCPORT, cblen)) {
4385 if (F_LEN(cmd) != 0)
4386 cmd = next_cmd(cmd, &cblen);
4393 if ( (av[0] == NULL) || _substrcmp(*av, "to") != 0 )
4394 errx(EX_USAGE, "missing ``to''");
4398 * destination, mandatory
4401 NOT_BLOCK; /* optional "not" */
4402 NEED1("missing dst address");
4403 if (add_dst(cmd, *av, proto, cblen, tstate)) {
4405 if (F_LEN(cmd) != 0) { /* ! any */
4407 cmd = next_cmd(cmd, &cblen);
4410 errx( EX_USAGE, "bad destination address %s", *av);
4414 * dest. ports, optional
4416 NOT_BLOCK; /* optional "not" */
4418 if (_substrcmp(*av, "any") == 0 ||
4419 add_ports(cmd, *av, proto, O_IP_DSTPORT, cblen)) {
4421 if (F_LEN(cmd) != 0)
4422 cmd = next_cmd(cmd, &cblen);
4428 while ( av[0] != NULL ) {
4430 ipfw_insn_u32 *cmd32; /* alias for cmd */
4433 cmd32 = (ipfw_insn_u32 *)cmd;
4435 if (*s == '!') { /* alternate syntax for NOT */
4436 if (cmd->len & F_NOT)
4437 errx(EX_USAGE, "double \"not\" not allowed\n");
4441 i = match_token(rule_options, s);
4445 if (cmd->len & F_NOT)
4446 errx(EX_USAGE, "double \"not\" not allowed\n");
4451 if (open_par == 0 || prev == NULL)
4452 errx(EX_USAGE, "invalid \"or\" block\n");
4456 case TOK_STARTBRACE:
4458 errx(EX_USAGE, "+nested \"(\" not allowed\n");
4464 errx(EX_USAGE, "+missing \")\"\n");
4470 fill_cmd(cmd, O_IN, 0, 0);
4474 cmd->len ^= F_NOT; /* toggle F_NOT */
4475 fill_cmd(cmd, O_IN, 0, 0);
4479 fill_cmd(cmd, O_DIVERTED, 0, 3);
4482 case TOK_DIVERTEDLOOPBACK:
4483 fill_cmd(cmd, O_DIVERTED, 0, 1);
4486 case TOK_DIVERTEDOUTPUT:
4487 fill_cmd(cmd, O_DIVERTED, 0, 2);
4491 fill_cmd(cmd, O_FRAG, 0, 0);
4495 fill_cmd(cmd, O_LAYER2, 0, 0);
4501 NEED1("recv, xmit, via require interface name"
4503 fill_iface((ipfw_insn_if *)cmd, av[0], cblen, tstate);
4505 if (F_LEN(cmd) == 0) /* not a valid address */
4508 cmd->opcode = O_XMIT;
4509 else if (i == TOK_RECV)
4510 cmd->opcode = O_RECV;
4511 else if (i == TOK_VIA)
4512 cmd->opcode = O_VIA;
4516 NEED1("icmptypes requires list of types");
4517 fill_icmptypes((ipfw_insn_u32 *)cmd, *av);
4521 case TOK_ICMP6TYPES:
4522 NEED1("icmptypes requires list of types");
4523 fill_icmp6types((ipfw_insn_icmp6 *)cmd, *av, cblen);
4528 NEED1("ipttl requires TTL");
4529 if (strpbrk(*av, "-,")) {
4530 if (!add_ports(cmd, *av, 0, O_IPTTL, cblen))
4531 errx(EX_DATAERR, "invalid ipttl %s", *av);
4533 fill_cmd(cmd, O_IPTTL, 0, strtoul(*av, NULL, 0));
4538 NEED1("ipid requires id");
4539 if (strpbrk(*av, "-,")) {
4540 if (!add_ports(cmd, *av, 0, O_IPID, cblen))
4541 errx(EX_DATAERR, "invalid ipid %s", *av);
4543 fill_cmd(cmd, O_IPID, 0, strtoul(*av, NULL, 0));
4548 NEED1("iplen requires length");
4549 if (strpbrk(*av, "-,")) {
4550 if (!add_ports(cmd, *av, 0, O_IPLEN, cblen))
4551 errx(EX_DATAERR, "invalid ip len %s", *av);
4553 fill_cmd(cmd, O_IPLEN, 0, strtoul(*av, NULL, 0));
4558 NEED1("ipver requires version");
4559 fill_cmd(cmd, O_IPVER, 0, strtoul(*av, NULL, 0));
4563 case TOK_IPPRECEDENCE:
4564 NEED1("ipprecedence requires value");
4565 fill_cmd(cmd, O_IPPRECEDENCE, 0,
4566 (strtoul(*av, NULL, 0) & 7) << 5);
4571 NEED1("missing DSCP code");
4572 fill_dscp(cmd, *av, cblen);
4577 NEED1("missing argument for ipoptions");
4578 fill_flags_cmd(cmd, O_IPOPT, f_ipopts, *av);
4583 NEED1("missing argument for iptos");
4584 fill_flags_cmd(cmd, O_IPTOS, f_iptos, *av);
4589 NEED1("uid requires argument");
4595 cmd->opcode = O_UID;
4596 uid = strtoul(*av, &end, 0);
4597 pwd = (*end == '\0') ? getpwuid(uid) : getpwnam(*av);
4599 errx(EX_DATAERR, "uid \"%s\" nonexistent", *av);
4600 cmd32->d[0] = pwd->pw_uid;
4601 cmd->len |= F_INSN_SIZE(ipfw_insn_u32);
4607 NEED1("gid requires argument");
4613 cmd->opcode = O_GID;
4614 gid = strtoul(*av, &end, 0);
4615 grp = (*end == '\0') ? getgrgid(gid) : getgrnam(*av);
4617 errx(EX_DATAERR, "gid \"%s\" nonexistent", *av);
4618 cmd32->d[0] = grp->gr_gid;
4619 cmd->len |= F_INSN_SIZE(ipfw_insn_u32);
4625 NEED1("jail requires argument");
4629 cmd->opcode = O_JAIL;
4630 jid = jail_getid(*av);
4632 errx(EX_DATAERR, "%s", jail_errmsg);
4633 cmd32->d[0] = (uint32_t)jid;
4634 cmd->len |= F_INSN_SIZE(ipfw_insn_u32);
4640 fill_cmd(cmd, O_ESTAB, 0, 0);
4644 fill_cmd(cmd, O_TCPFLAGS, 0,
4645 (TH_SYN) | ( (TH_ACK) & 0xff) <<8 );
4648 case TOK_TCPDATALEN:
4649 NEED1("tcpdatalen requires length");
4650 if (strpbrk(*av, "-,")) {
4651 if (!add_ports(cmd, *av, 0, O_TCPDATALEN, cblen))
4652 errx(EX_DATAERR, "invalid tcpdata len %s", *av);
4654 fill_cmd(cmd, O_TCPDATALEN, 0,
4655 strtoul(*av, NULL, 0));
4660 NEED1("missing argument for tcpoptions");
4661 fill_flags_cmd(cmd, O_TCPOPTS, f_tcpopts, *av);
4667 NEED1("tcpseq/tcpack requires argument");
4668 cmd->len = F_INSN_SIZE(ipfw_insn_u32);
4669 cmd->opcode = (i == TOK_TCPSEQ) ? O_TCPSEQ : O_TCPACK;
4670 cmd32->d[0] = htonl(strtoul(*av, NULL, 0));
4675 NEED1("tcpwin requires length");
4676 if (strpbrk(*av, "-,")) {
4677 if (!add_ports(cmd, *av, 0, O_TCPWIN, cblen))
4678 errx(EX_DATAERR, "invalid tcpwin len %s", *av);
4680 fill_cmd(cmd, O_TCPWIN, 0,
4681 strtoul(*av, NULL, 0));
4686 NEED1("missing argument for tcpflags");
4687 cmd->opcode = O_TCPFLAGS;
4688 fill_flags_cmd(cmd, O_TCPFLAGS, f_tcpflags, *av);
4693 case TOK_RECORDSTATE: {
4697 errx(EX_USAGE, "keep-state or record-state cannot be part "
4700 errx(EX_USAGE, "only one of keep-state, record-state, "
4701 " limit and set-limit is allowed");
4702 if (*av != NULL && *av[0] == ':') {
4703 if (state_check_name(*av + 1) != 0)
4705 "Invalid state name %s", *av);
4706 uidx = pack_object(tstate, *av + 1,
4707 IPFW_TLV_STATE_NAME);
4710 uidx = pack_object(tstate, default_state_name,
4711 IPFW_TLV_STATE_NAME);
4713 have_rstate = i == TOK_RECORDSTATE;
4714 fill_cmd(cmd, O_KEEP_STATE, 0, uidx);
4719 case TOK_SETLIMIT: {
4720 ipfw_insn_limit *c = (ipfw_insn_limit *)cmd;
4725 "limit or set-limit cannot be part of an or block");
4727 errx(EX_USAGE, "only one of keep-state, record-state, "
4728 " limit and set-limit is allowed");
4730 have_rstate = i == TOK_SETLIMIT;
4732 cmd->len = F_INSN_SIZE(ipfw_insn_limit);
4734 cmd->opcode = O_LIMIT;
4735 c->limit_mask = c->conn_limit = 0;
4737 while ( av[0] != NULL ) {
4738 if ((val = match_token(limit_masks, *av)) <= 0)
4740 c->limit_mask |= val;
4744 if (c->limit_mask == 0)
4745 errx(EX_USAGE, "limit: missing limit mask");
4747 GET_UINT_ARG(c->conn_limit, IPFW_ARG_MIN, IPFW_ARG_MAX,
4748 TOK_LIMIT, rule_options);
4751 if (*av != NULL && *av[0] == ':') {
4752 if (state_check_name(*av + 1) != 0)
4754 "Invalid state name %s", *av);
4755 cmd->arg1 = pack_object(tstate, *av + 1,
4756 IPFW_TLV_STATE_NAME);
4759 cmd->arg1 = pack_object(tstate,
4760 default_state_name, IPFW_TLV_STATE_NAME);
4765 NEED1("missing protocol");
4766 if (add_proto(cmd, *av, &proto)) {
4769 errx(EX_DATAERR, "invalid protocol ``%s''",
4774 NEED1("missing source IP");
4775 if (add_srcip(cmd, *av, cblen, tstate)) {
4781 NEED1("missing destination IP");
4782 if (add_dstip(cmd, *av, cblen, tstate)) {
4788 NEED1("missing source IP6");
4789 if (add_srcip6(cmd, *av, cblen, tstate)) {
4795 NEED1("missing destination IP6");
4796 if (add_dstip6(cmd, *av, cblen, tstate)) {
4802 NEED1("missing source port");
4803 if (_substrcmp(*av, "any") == 0 ||
4804 add_ports(cmd, *av, proto, O_IP_SRCPORT, cblen)) {
4807 errx(EX_DATAERR, "invalid source port %s", *av);
4811 NEED1("missing destination port");
4812 if (_substrcmp(*av, "any") == 0 ||
4813 add_ports(cmd, *av, proto, O_IP_DSTPORT, cblen)) {
4816 errx(EX_DATAERR, "invalid destination port %s",
4821 if (add_mac(cmd, av, cblen))
4826 NEED1("missing mac type");
4827 if (!add_mactype(cmd, *av, cblen))
4828 errx(EX_DATAERR, "invalid mac type %s", *av);
4832 case TOK_VERREVPATH:
4833 fill_cmd(cmd, O_VERREVPATH, 0, 0);
4836 case TOK_VERSRCREACH:
4837 fill_cmd(cmd, O_VERSRCREACH, 0, 0);
4841 fill_cmd(cmd, O_ANTISPOOF, 0, 0);
4845 fill_cmd(cmd, O_IPSEC, 0, 0);
4849 fill_cmd(cmd, O_IP6, 0, 0);
4853 fill_cmd(cmd, O_IP4, 0, 0);
4857 fill_ext6hdr( cmd, *av );
4862 if (proto != IPPROTO_IPV6 )
4863 errx( EX_USAGE, "flow-id filter is active "
4864 "only for ipv6 protocol\n");
4865 fill_flow6( (ipfw_insn_u32 *) cmd, *av, cblen);
4870 fill_comment(cmd, av, cblen);
4875 if (av[0] && strpbrk(*av, "-,")) {
4876 if (!add_ports(cmd, *av, 0, O_TAGGED, cblen))
4877 errx(EX_DATAERR, "tagged: invalid tag"
4883 GET_UINT_ARG(tag, IPFW_ARG_MIN, IPFW_ARG_MAX,
4884 TOK_TAGGED, rule_options);
4885 fill_cmd(cmd, O_TAGGED, 0, tag);
4891 NEED1("fib requires fib number");
4892 fill_cmd(cmd, O_FIB, 0, strtoul(*av, NULL, 0));
4896 fill_cmd(cmd, O_SOCKARG, 0, 0);
4900 ipfw_insn_u32 *c = (ipfw_insn_u32 *)cmd;
4903 if (!av[0] || !av[1])
4904 errx(EX_USAGE, "format: lookup argument tablenum");
4905 cmd->opcode = O_IP_DST_LOOKUP;
4906 cmd->len |= F_INSN_SIZE(ipfw_insn) + 2;
4907 i = match_token(rule_options, *av);
4908 for (j = 0; lookup_key[j] >= 0 ; j++) {
4909 if (i == lookup_key[j])
4912 if (lookup_key[j] <= 0)
4913 errx(EX_USAGE, "format: cannot lookup on %s", *av);
4914 __PAST_END(c->d, 1) = j; // i converted to option
4917 if ((j = pack_table(tstate, *av)) == 0)
4918 errx(EX_DATAERR, "Invalid table name: %s", *av);
4925 NEED1("missing table name");
4926 if (strncmp(*av, "table(", 6) != 0)
4928 "enclose table name into \"table()\"");
4929 fill_table(cmd, *av, O_IP_FLOW_LOOKUP, tstate);
4933 case TOK_SKIPACTION:
4935 errx(EX_USAGE, "only one defer-action "
4938 fill_cmd(cmd, O_SKIP_ACTION, 0, 0);
4942 errx(EX_USAGE, "unrecognised option [%d] %s\n", i, s);
4944 if (F_LEN(cmd) > 0) { /* prepare to advance */
4946 cmd = next_cmd(cmd, &cblen);
4952 if (!have_state && have_skipcmd)
4953 warnx("Rule contains \"defer-immediate-action\" "
4954 "and doesn't contain any state-related options.");
4957 * Now copy stuff into the rule.
4958 * If we have a keep-state option, the first instruction
4959 * must be a PROBE_STATE (which is generated here).
4960 * If we have a LOG option, it was stored as the first command,
4961 * and now must be moved to the top of the action part.
4963 dst = (ipfw_insn *)rule->cmd;
4966 * First thing to write into the command stream is the match probability.
4968 if (match_prob != 1) { /* 1 means always match */
4969 dst->opcode = O_PROB;
4971 *((int32_t *)(dst+1)) = (int32_t)(match_prob * 0x7fffffff);
4976 * generate O_PROBE_STATE if necessary
4978 if (have_state && have_state->opcode != O_CHECK_STATE && !have_rstate) {
4979 fill_cmd(dst, O_PROBE_STATE, 0, have_state->arg1);
4980 dst = next_cmd(dst, &rblen);
4984 * copy all commands but O_LOG, O_KEEP_STATE, O_LIMIT, O_ALTQ, O_TAG,
4987 for (src = (ipfw_insn *)cmdbuf; src != cmd; src += i) {
4991 switch (src->opcode) {
5000 bcopy(src, dst, i * sizeof(uint32_t));
5006 * put back the have_state command as last opcode
5008 if (have_state && have_state->opcode != O_CHECK_STATE) {
5009 i = F_LEN(have_state);
5011 bcopy(have_state, dst, i * sizeof(uint32_t));
5016 * put back the have_skipcmd command as very last opcode
5019 i = F_LEN(have_skipcmd);
5021 bcopy(have_skipcmd, dst, i * sizeof(uint32_t));
5026 * start action section
5028 rule->act_ofs = dst - rule->cmd;
5030 /* put back O_LOG, O_ALTQ, O_TAG if necessary */
5032 i = F_LEN(have_log);
5034 bcopy(have_log, dst, i * sizeof(uint32_t));
5038 i = F_LEN(have_altq);
5040 bcopy(have_altq, dst, i * sizeof(uint32_t));
5044 i = F_LEN(have_tag);
5046 bcopy(have_tag, dst, i * sizeof(uint32_t));
5051 * copy all other actions
5053 for (src = (ipfw_insn *)actbuf; src != action; src += i) {
5056 bcopy(src, dst, i * sizeof(uint32_t));
5060 rule->cmd_len = (uint32_t *)dst - (uint32_t *)(rule->cmd);
5061 *rbufsize = (char *)dst - (char *)rule;
5065 compare_ntlv(const void *_a, const void *_b)
5067 ipfw_obj_ntlv *a, *b;
5069 a = (ipfw_obj_ntlv *)_a;
5070 b = (ipfw_obj_ntlv *)_b;
5072 if (a->set < b->set)
5074 else if (a->set > b->set)
5077 if (a->idx < b->idx)
5079 else if (a->idx > b->idx)
5082 if (a->head.type < b->head.type)
5084 else if (a->head.type > b->head.type)
5091 * Provide kernel with sorted list of referenced objects
5094 object_sort_ctlv(ipfw_obj_ctlv *ctlv)
5097 qsort(ctlv + 1, ctlv->count, ctlv->objsize, compare_ntlv);
5105 compare_object_kntlv(const void *k, const void *v)
5107 ipfw_obj_ntlv *ntlv;
5108 struct object_kt key;
5110 key = *((struct object_kt *)k);
5111 ntlv = (ipfw_obj_ntlv *)v;
5113 if (key.uidx < ntlv->idx)
5115 else if (key.uidx > ntlv->idx)
5118 if (key.type < ntlv->head.type)
5120 else if (key.type > ntlv->head.type)
5127 * Finds object name in @ctlv by @idx and @type.
5128 * Uses the following facts:
5129 * 1) All TLVs are the same size
5130 * 2) Kernel implementation provides already sorted list.
5132 * Returns table name or NULL.
5135 object_search_ctlv(ipfw_obj_ctlv *ctlv, uint16_t idx, uint16_t type)
5137 ipfw_obj_ntlv *ntlv;
5138 struct object_kt key;
5143 ntlv = bsearch(&key, (ctlv + 1), ctlv->count, ctlv->objsize,
5144 compare_object_kntlv);
5147 return (ntlv->name);
5153 table_search_ctlv(ipfw_obj_ctlv *ctlv, uint16_t idx)
5156 return (object_search_ctlv(ctlv, idx, IPFW_TLV_TBL_NAME));
5160 * Adds one or more rules to ipfw chain.
5165 * [ ipfw_obj_ctlv(IPFW_TLV_TBL_LIST) ipfw_obj_ntlv x N ] (optional *1)
5166 * [ ipfw_obj_ctlv(IPFW_TLV_RULE_LIST) [ ip_fw_rule ip_fw_insn ] x N ] (*2) (*3)
5171 * [ ipfw_obj_ctlv(IPFW_TLV_TBL_LIST) ipfw_obj_ntlv x N ] (optional)
5172 * [ ipfw_obj_ctlv(IPFW_TLV_RULE_LIST) [ ip_fw_rule ip_fw_insn ] x N ]
5175 * Rules in reply are modified to store their actual ruleset number.
5177 * (*1) TLVs inside IPFW_TLV_TBL_LIST needs to be sorted ascending
5178 * according to their idx field and there has to be no duplicates.
5179 * (*2) Numbered rules inside IPFW_TLV_RULE_LIST needs to be sorted ascending.
5180 * (*3) Each ip_fw structure needs to be aligned to u64 boundary.
5183 ipfw_add(char *av[])
5185 uint32_t rulebuf[1024];
5186 int rbufsize, default_off, tlen, rlen;
5189 struct ip_fw_rule *rule;
5191 ip_fw3_opheader *op3;
5192 ipfw_obj_ctlv *ctlv, *tstate;
5194 rbufsize = sizeof(rulebuf);
5195 memset(rulebuf, 0, rbufsize);
5196 memset(&ts, 0, sizeof(ts));
5198 /* Optimize case with no tables */
5199 default_off = sizeof(ipfw_obj_ctlv) + sizeof(ip_fw3_opheader);
5200 op3 = (ip_fw3_opheader *)rulebuf;
5201 ctlv = (ipfw_obj_ctlv *)(op3 + 1);
5202 rule = (struct ip_fw_rule *)(ctlv + 1);
5203 rbufsize -= default_off;
5205 compile_rule(av, (uint32_t *)rule, &rbufsize, &ts);
5206 /* Align rule size to u64 boundary */
5207 rlen = roundup2(rbufsize, sizeof(uint64_t));
5212 if (ts.count != 0) {
5213 /* Some tables. We have to alloc more data */
5214 tlen = ts.count * sizeof(ipfw_obj_ntlv);
5215 sz = default_off + sizeof(ipfw_obj_ctlv) + tlen + rlen;
5217 if ((tbuf = calloc(1, sz)) == NULL)
5218 err(EX_UNAVAILABLE, "malloc() failed for IP_FW_ADD");
5219 op3 = (ip_fw3_opheader *)tbuf;
5221 ctlv = (ipfw_obj_ctlv *)(op3 + 1);
5222 ctlv->head.type = IPFW_TLV_TBLNAME_LIST;
5223 ctlv->head.length = sizeof(ipfw_obj_ctlv) + tlen;
5224 ctlv->count = ts.count;
5225 ctlv->objsize = sizeof(ipfw_obj_ntlv);
5226 memcpy(ctlv + 1, ts.idx, tlen);
5227 object_sort_ctlv(ctlv);
5230 ctlv = (ipfw_obj_ctlv *)((caddr_t)ctlv + ctlv->head.length);
5231 ctlv->head.type = IPFW_TLV_RULE_LIST;
5232 ctlv->head.length = sizeof(ipfw_obj_ctlv) + rlen;
5234 memcpy(ctlv + 1, rule, rbufsize);
5236 /* Simply add header */
5237 sz = rlen + default_off;
5238 memset(ctlv, 0, sizeof(*ctlv));
5239 ctlv->head.type = IPFW_TLV_RULE_LIST;
5240 ctlv->head.length = sizeof(ipfw_obj_ctlv) + rlen;
5244 if (do_get3(IP_FW_XADD, op3, &sz) != 0)
5245 err(EX_UNAVAILABLE, "getsockopt(%s)", "IP_FW_XADD");
5248 struct format_opts sfo;
5250 memset(&sfo, 0, sizeof(sfo));
5251 sfo.tstate = tstate;
5252 sfo.set_mask = (uint32_t)(-1);
5253 bp_alloc(&bp, 4096);
5254 show_static_rule(&co, &sfo, &bp, rule, NULL);
5255 printf("%s", bp.buf);
5267 * clear the counters or the log counters.
5268 * optname has the following values:
5269 * 0 (zero both counters and logging)
5270 * 1 (zero logging only)
5273 ipfw_zero(int ac, char *av[], int optname)
5277 char const *name = optname ? "RESETLOG" : "ZERO";
5281 optname = optname ? IP_FW_XRESETLOG : IP_FW_XZERO;
5285 /* clear all entries */
5286 memset(&rt, 0, sizeof(rt));
5287 rt.flags = IPFW_RCFLAG_ALL;
5288 if (do_range_cmd(optname, &rt) < 0)
5289 err(EX_UNAVAILABLE, "setsockopt(IP_FW_X%s)", name);
5291 printf("%s.\n", optname == IP_FW_XZERO ?
5292 "Accounting cleared":"Logging counts reset");
5299 if (isdigit(**av)) {
5300 arg = strtonum(*av, 0, 0xffff, &errstr);
5303 "invalid rule number %s\n", *av);
5304 memset(&rt, 0, sizeof(rt));
5305 rt.start_rule = arg;
5307 rt.flags |= IPFW_RCFLAG_RANGE;
5308 if (co.use_set != 0) {
5309 rt.set = co.use_set - 1;
5310 rt.flags |= IPFW_RCFLAG_SET;
5312 if (do_range_cmd(optname, &rt) != 0) {
5313 warn("rule %u: setsockopt(IP_FW_X%s)",
5315 failed = EX_UNAVAILABLE;
5316 } else if (rt.new_set == 0) {
5317 printf("Entry %d not found\n", arg);
5318 failed = EX_UNAVAILABLE;
5319 } else if (!co.do_quiet)
5320 printf("Entry %d %s.\n", arg,
5321 optname == IP_FW_XZERO ?
5322 "cleared" : "logging count reset");
5324 errx(EX_USAGE, "invalid rule number ``%s''", *av);
5328 if (failed != EX_OK)
5333 ipfw_flush(int force)
5337 if (!force && !co.do_quiet) { /* need to ask user */
5340 printf("Are you sure? [yn] ");
5343 c = toupper(getc(stdin));
5344 while (c != '\n' && getc(stdin) != '\n')
5346 return; /* and do not flush */
5347 } while (c != 'Y' && c != 'N');
5349 if (c == 'N') /* user said no */
5356 /* `ipfw set N flush` - is the same that `ipfw delete set N` */
5357 memset(&rt, 0, sizeof(rt));
5358 if (co.use_set != 0) {
5359 rt.set = co.use_set - 1;
5360 rt.flags = IPFW_RCFLAG_SET;
5362 rt.flags = IPFW_RCFLAG_ALL;
5363 if (do_range_cmd(IP_FW_XDEL, &rt) != 0)
5364 err(EX_UNAVAILABLE, "setsockopt(IP_FW_XDEL)");
5366 printf("Flushed all %s.\n", co.do_pipe ? "pipes" : "rules");
5369 static struct _s_x intcmds[] = {
5370 { "talist", TOK_TALIST },
5371 { "iflist", TOK_IFLIST },
5372 { "olist", TOK_OLIST },
5373 { "vlist", TOK_VLIST },
5377 static struct _s_x otypes[] = {
5378 { "EACTION", IPFW_TLV_EACTION },
5379 { "DYNSTATE", IPFW_TLV_STATE_NAME },
5384 lookup_eaction_name(ipfw_obj_ntlv *ntlv, int cnt, uint16_t type)
5390 for (i = 0; i < cnt; i++) {
5391 if (ntlv[i].head.type != IPFW_TLV_EACTION)
5393 if (IPFW_TLV_EACTION_NAME(ntlv[i].idx) != type)
5395 name = ntlv[i].name;
5402 ipfw_list_objects(int ac, char *av[])
5404 ipfw_obj_lheader req, *olh;
5405 ipfw_obj_ntlv *ntlv;
5410 memset(&req, 0, sizeof(req));
5412 if (do_get3(IP_FW_DUMP_SRVOBJECTS, &req.opheader, &sz) != 0)
5413 if (errno != ENOMEM)
5417 if ((olh = calloc(1, sz)) == NULL)
5421 if (do_get3(IP_FW_DUMP_SRVOBJECTS, &olh->opheader, &sz) != 0) {
5427 printf("Objects list:\n");
5429 printf("There are no objects\n");
5430 ntlv = (ipfw_obj_ntlv *)(olh + 1);
5431 for (i = 0; i < olh->count; i++) {
5432 name = match_value(otypes, ntlv->head.type);
5434 name = lookup_eaction_name(
5435 (ipfw_obj_ntlv *)(olh + 1), olh->count,
5438 printf(" kidx: %4d\ttype: %10d\tname: %s\n",
5439 ntlv->idx, ntlv->head.type, ntlv->name);
5441 printf(" kidx: %4d\ttype: %10s\tname: %s\n",
5442 ntlv->idx, name, ntlv->name);
5449 ipfw_internal_handler(int ac, char *av[])
5454 NEED1("internal cmd required");
5456 if ((tcmd = match_token(intcmds, *av)) == -1)
5457 errx(EX_USAGE, "invalid internal sub-cmd: %s", *av);
5461 ipfw_list_tifaces();
5464 ipfw_list_ta(ac, av);
5467 ipfw_list_objects(ac, av);
5470 ipfw_list_values(ac, av);
5476 ipfw_get_tracked_ifaces(ipfw_obj_lheader **polh)
5478 ipfw_obj_lheader req, *olh;
5481 memset(&req, 0, sizeof(req));
5484 if (do_get3(IP_FW_XIFLIST, &req.opheader, &sz) != 0) {
5485 if (errno != ENOMEM)
5490 if ((olh = calloc(1, sz)) == NULL)
5494 if (do_get3(IP_FW_XIFLIST, &olh->opheader, &sz) != 0) {
5504 ifinfo_cmp(const void *a, const void *b)
5506 ipfw_iface_info *ia, *ib;
5508 ia = (ipfw_iface_info *)a;
5509 ib = (ipfw_iface_info *)b;
5511 return (stringnum_cmp(ia->ifname, ib->ifname));
5515 * Retrieves table list from kernel,
5516 * optionally sorts it and calls requested function for each table.
5517 * Returns 0 on success.
5522 ipfw_obj_lheader *olh;
5523 ipfw_iface_info *info;
5526 if ((error = ipfw_get_tracked_ifaces(&olh)) != 0)
5527 err(EX_OSERR, "Unable to request ipfw tracked interface list");
5530 qsort(olh + 1, olh->count, olh->objsize, ifinfo_cmp);
5532 info = (ipfw_iface_info *)(olh + 1);
5533 for (i = 0; i < olh->count; i++) {
5534 if (info->flags & IPFW_IFFLAG_RESOLVED)
5535 printf("%s ifindex: %d refcount: %u changes: %u\n",
5536 info->ifname, info->ifindex, info->refcnt,
5539 printf("%s ifindex: unresolved refcount: %u changes: %u\n",
5540 info->ifname, info->refcnt, info->gencnt);
5541 info = (ipfw_iface_info *)((caddr_t)info + olh->objsize);