evaluate: New internal helper __expr_evaluate_range
PATCH v8 1/2 evaluate: New internal helper __expr_evaluate_range
This is used by the followup patch to evaluate a range without emitting an error when the left value is larger than the right one.
This is done to handle time-matching such as 23:00-01:00 -- expr_evaluate_range() will reject this, but we want to be able to evaluate and then handle this as a request to match from 23:00 to 1am.
--- src/evaluate.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)
src/evaluate.c +16 -4
@@ -941,16 +941,28 @@ static int expr_evaluate_range_expr(struct eval_ctx *ctx,return 0 ; } -static int expr_evaluate_range (struct eval_ctx *ctx,struct expr **expr) +static int __expr_evaluate_range (struct eval_ctx *ctx,struct expr **expr) { -struct expr *range = *expr, *left, *right; +struct expr *range = *expr;if (expr_evaluate_range_expr (ctx, range, &range->left) <0 )return -1 ; - left = range->left; -if (expr_evaluate_range_expr (ctx, range, &range->right) <0 )return -1 ; + +return 0 ; +} + +static int expr_evaluate_range (struct eval_ctx *ctx,struct expr **expr) +{ +struct expr *range = *expr, *left, *right; +int rc; + + rc =__expr_evaluate_range (ctx, expr); +if (rc) +return rc; + + left = range->left; right = range->right;if (mpz_cmp (left->value, right->value) >=0 )
Signature · 1 line
2.17.1
PATCH v8 2/2 meta: Introduce new conditions 'time', 'day' and 'hour'
These keywords introduce new checks for a timestamp, an absolute date (which is converted to a timestamp), an hour in the day (which is converted to the number of seconds since midnight) and a day of week.
When converting an ISO date (eg. 2019-06-06 17:00) to a timestamp, we need to substract it the GMT difference in seconds, that is, the value of the 'tm_gmtoff' field in the tm structure. This is because the kernel doesn't know about time zones. And hence the kernel manages different timestamps than those that are advertised in userspace when running, for instance, date +%s.
The same conversion needs to be done when converting hours (e.g 17:00) to seconds since midnight as well.
The result needs to be computed modulo 86400 in case GMT offset (difference in seconds from UTC) is negative.
We also introduce a new command line option (-t, --seconds) to show the actual timestamps when printing the values, rather than the ISO dates, or the hour.
Some usage examples:
time < "2019-06-06 17:00" drop; time < "2019-06-06 17:20:20" drop; time < 12341234 drop; day "Sat" drop; day 6 drop; hour >= 17:00 drop; hour >= "17:00:01" drop; hour >= 63000 drop;
We need to convert an ISO date to a timestamp
without taking into account the time zone offset, since comparison will be done in kernel space and there is no time zone information there.
Overwriting TZ is portable, but will cause problems when parsing a ruleset that has 'time' and 'hour' rules. Parsing an 'hour' type must not do time zone conversion, but that will be automatically done if TZ has been overwritten to UTC.
Hence, we use timegm() to parse the 'time' type, even though it's not portable. Overwriting TZ seems to be a much worse solution.
Finally, be aware that timestamps are converted to nanoseconds when transferring to the kernel (as comparison is done with nanosecond precision), and back to seconds when retrieving them for printing.
We swap left and right values in a range to properly handle cross-day hour ranges (e.g. 23:15-03:22).
--- doc/nft.txt | 6 +- doc/primary-expression.txt | 27 ++- include/datatype.h | 3 + include/linux/netfilter/nf_tables.h | 6 + include/meta.h | 3 + include/nftables.h | 5 + include/nftables/libnftables.h | 1 + src/datatype.c | 3 + src/evaluate.c | 54 +++++ src/main.c | 11 +- src/meta.c | 331 ++++++++++++++++++++++++++++ src/parser_bison.y | 9 + src/scanner.l | 1 + tests/py/ip/meta.t | 15 ++ tests/py/ip/meta.t.json | 252 +++++++++++++++++++++ tests/py/ip/meta.t.json.output | 252 +++++++++++++++++++++ tests/py/ip/meta.t.payload | 84 +++++++ 17 files changed, 1059 insertions(+), 4 deletions(-) mode change 100755 => 100644 src/evaluate.c
doc/nft.txt +5 -1
@@ -9,7 +9,7 @@ nft - Administration tool of the nftables framework for packet filtering and cla SYNOPSIS -------- [verse] -*nft* [ *-nNscaeSupyj* ] [ *-I* 'directory' ] [ *-f* 'filename' | *-i* | 'cmd' ...] +*nft* [ *-nNscaeSupyjt* ] [ *-I* 'directory' ] [ *-f* 'filename' | *-i* | 'cmd' ...] *nft* *-h* *nft* *-v*
@@ -93,6 +93,10 @@ For a full summary of options, run *nft --help*. Read input from an interactive readline CLI. You can use quit to exit, or use the EOF marker, normally this is CTRL-D. +*-t*:: +*--seconds*:: + Show time, day and hour values in seconds. + INPUT FILE FORMATS ------------------ LEXICAL CONVENTIONS
doc/primary-expression.txt +25 -2
@@ -2,7 +2,7 @@ META EXPRESSIONS ~~~~~~~~~~~~~~~~ [verse] *meta* {*length* | *nfproto* | *l4proto* | *protocol* | *priority*} -[*meta*] {*mark* | *iif* | *iifname* | *iiftype* | *oif* | *oifname* | *oiftype* | *skuid* | *skgid* | *nftrace* | *rtclassid* | *ibrname* | *obrname* | *pkttype* | *cpu* | *iifgroup* | *oifgroup* | *cgroup* | *random* | *ipsec* | *iifkind* | *oifkind*} +[*meta*] {*mark* | *iif* | *iifname* | *iiftype* | *oif* | *oifname* | *oiftype* | *skuid* | *skgid* | *nftrace* | *rtclassid* | *ibrname* | *obrname* | *pkttype* | *cpu* | *iifgroup* | *oifgroup* | *cgroup* | *random* | *ipsec* | *iifkind* | *oifkind* | *time* | *hour* | *day* } A meta expression refers to meta data associated with a packet.
@@ -115,7 +115,16 @@ boolean (1 bit) |iifkind| Input interface kind | |oifkind| -Output interface kind +Output interface kind| +|time| +Absolute time of packet reception| +Integer (32 bit) or string +|day| +Day of week| +Integer (8 bit) or string +|hour| +Hour of day| +String |==================== .Meta expression specific types
@@ -141,6 +150,20 @@ Packet type: *host* (addressed to local host), *broadcast* (to all), *multicast* (to group), *other* (addressed to another host). |ifkind| Interface kind (16 byte string). Does not have to exist. +|time| +Either an integer or a date in ISO format. For example: "2019-06-06 17:00". +Hour and seconds are optional and can be omitted if desired. If omitted, +midnight will be assumed. +The following three would be equivalent: "2019-06-06", "2019-06-06 00:00" +and "2019-06-06 00:00:00". +When an integer is given, it is assumed to be a UNIX timestamp. +|day| +Either a day of week ("Monday", "Tuesday", etc.), or an integer between 0 and 6. +Strings are matched case-insensitively, and a full match is not expected (e.g. "Mon" would match "Monday"). +When an integer is given, 0 is Sunday and 6 is Saturday. +|hour| +A string representing an hour in 24-hour format. Seconds can optionally be specified. +For example, 17:00 and 17:00:00 would be equivalent. |============================= .Using meta expressions
include/datatype.h +3 -0
@@ -90,6 +90,9 @@ enum datatypes {TYPE_CT_EVENTBIT ,TYPE_IFNAME ,TYPE_IGMP_TYPE , +TYPE_TIME_DATE , +TYPE_TIME_HOUR , +TYPE_TIME_DAY , __TYPE_MAX };#define TYPE_MAX (__TYPE_MAX - 1)
include/linux/netfilter/nf_tables.h +6 -0
@@ -799,6 +799,9 @@ enum nft_exthdr_attributes {* @NFT_META_OIFKIND: packet output interface kind name (dev->rtnl_link_ops->kind) * @NFT_META_BRI_IIFPVID: packet input bridge port pvid * @NFT_META_BRI_IIFVPROTO: packet input bridge vlan proto +* @NFT_META_TIME_NS: time since epoch (in nanoseconds) +* @NFT_META_TIME_DAY: day of week (from 0 = Sunday to 6 = Saturday) +* @NFT_META_TIME_HOUR: hour of day (in seconds) */ enum nft_meta_keys {NFT_META_LEN ,
@@ -831,6 +834,9 @@ enum nft_meta_keys {NFT_META_OIFKIND ,NFT_META_BRI_IIFPVID ,NFT_META_BRI_IIFVPROTO , +NFT_META_TIME_NS , +NFT_META_TIME_DAY , + NFT_META_TIME_HOUR, };/**
include/meta.h +3 -0
@@ -41,5 +41,8 @@ extern const struct datatype uid_type;extern const struct datatype devgroup_type;extern const struct datatype pkttype_type;extern const struct datatype ifname_type; +extern const struct datatype date_type; +extern const struct datatype hour_type; +extern const struct datatype day_type;#endif /* NFTABLES_META_H */
include/nftables.h +5 -0
@@ -70,6 +70,11 @@ static inline bool nft_output_guid(const struct output_ctx *octx)return octx->flags &NFT_CTX_OUTPUT_GUID ; } +static inline bool nft_output_seconds (const struct output_ctx *octx) +{ +return octx->flags &NFT_CTX_OUTPUT_NUMERIC_TIME ; +} +static inline bool nft_output_numeric_proto (const struct output_ctx *octx) {return octx->flags &NFT_CTX_OUTPUT_NUMERIC_PROTO ;
include/nftables/libnftables.h +1 -0
@@ -52,6 +52,7 @@ enum {NFT_CTX_OUTPUT_NUMERIC_PROTO = (1 <<7 ),NFT_CTX_OUTPUT_NUMERIC_PRIO = (1 <<8 ),NFT_CTX_OUTPUT_NUMERIC_SYMBOL = (1 <<9 ), +NFT_CTX_OUTPUT_NUMERIC_TIME = (1 <<10 ),NFT_CTX_OUTPUT_NUMERIC_ALL = (NFT_CTX_OUTPUT_NUMERIC_PROTO |NFT_CTX_OUTPUT_NUMERIC_PRIO | NFT_CTX_OUTPUT_NUMERIC_SYMBOL),
src/datatype.c +3 -0
@@ -71,6 +71,9 @@ static const struct datatype *datatypes[TYPE_MAX + 1] = { [TYPE_BOOLEAN] = &boolean_type, [TYPE_IFNAME] = &ifname_type, [TYPE_IGMP_TYPE] = &igmp_type_type, + [TYPE_TIME_DATE] = &date_type, + [TYPE_TIME_HOUR] = &hour_type, + [TYPE_TIME_DAY] = &day_type, };const struct datatype *datatype_lookup (enum datatypes type)
src/evaluate.c +54 -0
68 lines not on this page · raw message
src/main.c +10 -1
@@ -43,8 +43,9 @@ enum opt_vals {OPT_NUMERIC_PRIO ='y' ,OPT_NUMERIC_PROTO ='p' ,OPT_INVALID ='?' , + OPT_SECONDS = 't', }; -#define OPTSTRING "hvcf:iI:jvnsNaeSupyp" +#define OPTSTRING "hvcf:iI:jvnsNaeSupypt"static const struct option options[] = { {
@@ -114,6 +115,10 @@ static const struct option options[] = { .name = "numeric-priority", .val = OPT_NUMERIC_PRIO, }, + { + .name = "seconds", + .val = OPT_SECONDS, + }, { .name = NULL }
@@ -143,6 +148,7 @@ static void show_help(const char *name)" -a, --handle Output rule handle.\n" " -e, --echo Echo what has been added, inserted or replaced.\n" " -I, --includepath <directory> Add <directory> to the paths searched for include files. Default is: %s\n" +" -t, --seconds Show time, day and hour values in seconds.\n" " --debug <level [,level...]> Specify debugging level (scanner, parser, eval, netlink, mnl, proto-ctx, segtree, all)\n" "\n" , name, DEFAULT_INCLUDE_PATH);
@@ -285,6 +291,9 @@ int main(int argc, char * const *argv)case OPT_GUID : output_flags |=NFT_CTX_OUTPUT_GUID ;break ; +case OPT_SECONDS : + output_flags |=NFT_CTX_OUTPUT_NUMERIC_TIME ; +break ;case OPT_NUMERIC_PRIO : output_flags |=NFT_CTX_OUTPUT_NUMERIC_PRIO ;break ;
src/meta.c +331 -0
349 lines not on this page · raw message
src/parser_bison.y +9 -0
@@ -431,6 +431,7 @@ int nft_lex(void *, void *, void *); %token IIFGROUP "iifgroup" %token OIFGROUP "oifgroup" %token CGROUP "cgroup" +%token TIME "time" %token CLASSID "classid" %token NEXTHOP "nexthop"
@@ -1827,6 +1828,11 @@ data_type_atom_expr : type_identifier dtype->size, NULL); xfree($1); } + | TIME + { + $$ = constant_expr_alloc(&@1, &time_type, time_type.byteorder, + time_type.size, NULL); + } ; data_type_expr : data_type_atom_expr
@@ -4049,6 +4055,9 @@ meta_key_unqualified : MARK { $$ = NFT_META_MARK; } | OIFGROUP { $$ = NFT_META_OIFGROUP; } | CGROUP { $$ = NFT_META_CGROUP; } | IPSEC { $$ = NFT_META_SECPATH; } + | TIME { $$ = NFT_META_TIME_NS; } + | DAY { $$ = NFT_META_TIME_DAY; } + | HOUR { $$ = NFT_META_TIME_HOUR; } ; meta_stmt : META meta_key SET stmt_expr
src/scanner.l +1 -0
@@ -426,6 +426,7 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "sack3" { return SACK3; } "sack-permitted" { return SACK_PERMITTED; } "timestamp" { return TIMESTAMP; } +"time" { return TIME; } "kind" { return KIND; } "count" { return COUNT; }
tests/py/ip/meta.t +15 -0
@@ -3,6 +3,21 @@ *ip;test-ip4;input icmp type echo-request;ok +meta time "1970-05-23 21:07:14" drop;ok +meta time 12341234 drop;ok;meta time "1970-05-23 21:07:14" drop +meta time "2019-06-21 17:00:00" drop;ok +meta time "2019-07-01 00:00:00" drop;ok +meta time "2019-07-01 00:01:00" drop;ok +meta time "2019-07-01 00:00:01" drop;ok +meta day "Sat" drop;ok +meta day "Saturday" drop;ok;meta day "Sat" drop +meta day 6 drop;ok;meta day "Sat" drop +meta day "Sa" drop;fail +meta hour "17:00" drop;ok +meta hour "17:00:00" drop;ok;meta hour "17:00" drop +meta hour "17:00:01" drop;ok +meta hour "00:00" drop;ok +meta hour "00:01" drop;ok meta l4proto icmp icmp type echo-request;ok;icmp type echo-request meta l4proto ipv6-icmp icmpv6 type nd-router-advert;ok;icmpv6 type nd-router-advert meta l4proto 58 icmpv6 type nd-router-advert;ok;icmpv6 type nd-router-advert
tests/py/ip/meta.t.json +252 -0
258 lines not on this page · raw message
tests/py/ip/meta.t.json.output +252 -0
258 lines not on this page · raw message
tests/py/ip/meta.t.payload +84 -0
87 lines not on this page · raw message
Signature · 1 line
2.17.1
Florian Westphal
maintainerRe: [PATCH nft v8 2/2] meta: Introduce new conditions 'time', 'day' and 'hour'
Quoted 9 lines · Ander Juaristi
These keywords introduce new checks for a timestamp, an absolute date (which is converted to a timestamp), an hour in the day (which is converted to the number of seconds since midnight) and a day of week.
When converting an ISO date (eg. 2019-06-06 17:00) to a timestamp, we need to substract it the GMT difference in seconds, that is, the value of the '
tm_gmtoff' field in the tm structure. This is because the kernel doesn't know about time zones. And hence the kernel manages different timestamps than those that are advertised in userspace when running, for instance, date +%s.
Quoted 8 lines
The same conversion needs to be done when converting hours (e.g 17:00) to seconds since midnight as well.
The result needs to be computed modulo 86400 in case GMT offset (difference in seconds from UTC) is negative.
We also introduce a new command line option (-t, --seconds) to show the actual timestamps when printing the values, rather than the ISO dates, or the hour.
Pablo, please see this "-t" option -- should be just re-use -n instead?
Other than this, this patch looks good and all tests pass for me.
Pablo Neira Ayuso
maintainerRe: [PATCH nft v8 2/2] meta: Introduce new conditions 'time', 'day' and 'hour'
Quoted 16 lines · Florian Westphal
Quoted 15 lines · Ander Juaristi
These keywords introduce new checks for a timestamp, an absolute date (which is converted to a timestamp), an hour in the day (which is converted to the number of seconds since midnight) and a day of week.
When converting an ISO date (eg. 2019-06-06 17:00) to a timestamp, we need to substract it the GMT difference in seconds, that is, the value of the '
tm_gmtoff' field in the tm structure. This is because the kernel doesn't know about time zones. And hence the kernel manages different timestamps than those that are advertised in userspace when running, for instance, date +%s.The same conversion needs to be done when converting hours (e.g 17:00) to seconds since midnight as well.
The result needs to be computed modulo 86400 in case GMT offset (difference in seconds from UTC) is negative.
Quoted 6 lines
Quoted 2 lines
We also introduce a new command line option (-t, --seconds) to show the actual timestamps when printing the values, rather than the ISO dates, or the hour.
Pablo, please see this "-t" option -- should be just re-use -n instead?
Other than this, this patch looks good and all tests pass for me.
this should be printed numerically with -n (global switch to disable literal printing).
Then, -t could be added for disabling literal in a more fine grain, as Phil suggest time ago with other existing options that are similar to this one.
Florian Westphal
maintainerRe: [PATCH nft v8 2/2] meta: Introduce new conditions 'time', 'day' and 'hour'
Quoted 22 lines · Pablo Neira Ayuso
Quoted 21 lines · Florian Westphal
Quoted 9 lines · Ander Juaristi
These keywords introduce new checks for a timestamp, an absolute date (which is converted to a timestamp), an hour in the day (which is converted to the number of seconds since midnight) and a day of week.
When converting an ISO date (eg. 2019-06-06 17:00) to a timestamp, we need to substract it the GMT difference in seconds, that is, the value of the '
tm_gmtoff' field in the tm structure. This is because the kernel doesn't know about time zones. And hence the kernel manages different timestamps than those that are advertised in userspace when running, for instance, date +%s.Quoted 8 lines
The same conversion needs to be done when converting hours (e.g 17:00) to seconds since midnight as well.
The result needs to be computed modulo 86400 in case GMT offset (difference in seconds from UTC) is negative.
We also introduce a new command line option (-t, --seconds) to show the actual timestamps when printing the values, rather than the ISO dates, or the hour.
Pablo, please see this "-t" option -- should be just re-use -n instead?
Quoted 8 lines
Quoted 1 line
Other than this, this patch looks good and all tests pass for me.
this should be printed numerically with -n (global switch to disable literal printing).
Then, -t could be added for disabling literal in a more fine grain, as Phil suggest time ago with other existing options that are similar to this one.
Ander, would you mind respinning this once more and excluding the -t option? You can reuse -n (OPT_NUMERIC) to print raw time values for the time being.
Re: [PATCH nft v8 2/2] meta: Introduce new conditions 'time', 'day' and 'hour'
Quoted 8 lines · Florian Westphal
Quoted 8 lines
Quoted 4 lines
Pablo, please see this "-t" option -- should be just re-use -n instead?
Other than this, this patch looks good and all tests pass for me.
this should be printed numerically with -n (global switch to disable literal printing).
Quoted 8 lines
Quoted 3 lines
Then, -t could be added for disabling literal in a more fine grain, as Phil suggest time ago with other existing options that are similar to this one.
Ander, would you mind respinning this once more and excluding the -t option? You can reuse -n (
OPT_NUMERIC) to print raw time values for the time being.
You mean removing the -t option altogether?
Okay, I'll send v9 today after lunch.
Pablo Neira Ayuso
maintainerRe: [PATCH nft v8 2/2] meta: Introduce new conditions 'time', 'day' and 'hour'
Quoted 13 lines · Ander Juaristi
Quoted 12 lines · Florian Westphal
Quoted 11 lines
Quoted 4 lines
Pablo, please see this "-t" option -- should be just re-use -n instead?
Other than this, this patch looks good and all tests pass for me.
this should be printed numerically with -n (global switch to disable literal printing).
Then, -t could be added for disabling literal in a more fine grain, as Phil suggest time ago with other existing options that are similar to this one.
Quoted 6 lines
Quoted 4 lines
Ander, would you mind respinning this once more and excluding the -t option? You can reuse -n (
OPT_NUMERIC) to print raw time values for the time being.You mean removing the -t option altogether?
Phil likes having fine grain knobs. I (as a user) particularly prefer the global -n switch, but I also see value in those fine grain knobs.
Anyway, I let you choose on this one.
Thanks.
Re: [PATCH nft v8 2/2] meta: Introduce new conditions 'time', 'day' and 'hour'
Quoted 5 lines · Pablo Neira Ayuso
Phil likes having fine grain knobs. I (as a user) particularly prefer the global -n switch, but I also see value in those fine grain knobs.
Anyway, I let you choose on this one.
I've decided to keep it, but I've changed it's long name to '--numeric-time'.
+" -t, --numeric-time Print time values numerically (time,
day, hour).\n"
I think it better captures the intent and it's in line with other --numeric-XXXX options.
In addition, -n also includes the behavior of -t.
I've just sent v9.