]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
7 years agofiles: restore base table skeletons
Florian Westphal [Sun, 6 May 2018 19:44:56 +0000 (21:44 +0200)] 
files: restore base table skeletons

nftables releases until 0.8.2 included base skeleton hooks
that were installed into /etc/nftables (sysconfdir).

With 0.8.3 and newer these files were moved to the documentation
area but apparently some users expect them to be there.

Resurrect them.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoscanner: Support rfc4291 IPv4-compatible addresses
Phil Sutter [Sat, 5 May 2018 12:44:53 +0000 (14:44 +0200)] 
scanner: Support rfc4291 IPv4-compatible addresses

These are defined in section 2.5.5.1. Although it is stated that they
are deprecated and new implementations are not required to support them,
they occur in ruleset output if an address in the form '::feed:babe' was
given in input. In order to support reinsertion of that rule, we have to
support those deprecated addresses as well.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/py: Fix expected output in {bridge,inet}/icmpX.t
Phil Sutter [Sat, 5 May 2018 12:44:37 +0000 (14:44 +0200)] 
tests/py: Fix expected output in {bridge,inet}/icmpX.t

The first expression in that rule is not eliminated in evaluation phase,
so there is no reason why it should be while delinearizing.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoproto: Fix wrong token in proto_icmp6
Phil Sutter [Sat, 5 May 2018 12:44:26 +0000 (14:44 +0200)] 
proto: Fix wrong token in proto_icmp6

'token' value of ICMP6HDR_MTU field must be 'mtu', not 'packet-too-big'.
This went unnoticed because rule delinearization for icmp/icmpv6 payload
expressions is problematic anyway in that different fields point to the
same offset and therefore are indistinguishable. In this case, an
expression like e.g. 'icmpv6 mtu 1500' will be printed later as 'icmpv6
parameter-problem 1500'.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: use location to display error messages
Pablo Neira Ayuso [Thu, 3 May 2018 10:46:08 +0000 (12:46 +0200)] 
src: use location to display error messages

 # nft add chain foo bar
 Error: Could not process rule: No such file or directory
 add chain foo bar
           ^^^

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: add obj_spec
Pablo Neira Ayuso [Thu, 3 May 2018 10:49:39 +0000 (12:49 +0200)] 
src: add obj_spec

Store location object in handle to improve error reporting.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: add set_spec
Pablo Neira Ayuso [Thu, 3 May 2018 10:31:48 +0000 (12:31 +0200)] 
src: add set_spec

Store location object in handle to improve error reporting.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: add chain_spec
Pablo Neira Ayuso [Thu, 3 May 2018 10:11:32 +0000 (12:11 +0200)] 
src: add chain_spec

Store location object in handle to improve error reporting.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: add table_spec
Pablo Neira Ayuso [Thu, 3 May 2018 10:06:27 +0000 (12:06 +0200)] 
src: add table_spec

Store location object in handle to improve error reporting.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoparser: Removed LOOKUP token
Máté Eckl [Fri, 4 May 2018 21:50:47 +0000 (23:50 +0200)] 
parser: Removed LOOKUP token

It is never used.

Signed-off-by: Máté Eckl <ecklm94@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agodoc: update doc/ispell_nft to track recent nft.8 updates
Duncan Roe [Wed, 2 May 2018 23:02:20 +0000 (09:02 +1000)] 
doc: update doc/ispell_nft to track recent nft.8 updates

Track changes in commits 3baa28f24b3d70a7ee17d584c113a2c4e057a565 and
4787edad132c30ae0f6bb00135ae5d970b0ccb74 (rename ibriport and obriport:
s/iport/name).

Signed-off-by: Duncan Roe <duncan_roe@optusnet.com.au>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agometer: enforce presence of a max size
Florian Westphal [Wed, 2 May 2018 12:50:12 +0000 (14:50 +0200)] 
meter: enforce presence of a max size

meters are updated dynamically, so we don't know in advance
how large this structure can be.

Add a 'size' keyword to specifiy an upper limit and update
the old syntax to assume a default max value of 65535.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agolibnftables: fix header export
Arturo Borrero Gonzalez [Tue, 1 May 2018 17:33:45 +0000 (19:33 +0200)] 
libnftables: fix header export

Instruct Make to actually install the header to the system, otherwise
users won't see the header in their system after running 'make install'.

Also, export main libnftables header with a proper name, since we have another
private header called 'nftables.h' (i.e, let's be concrete with the naming).

Signed-off-by: Arturo Borrero Gonzalez <arturo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agobuild: Bump version to v0.8.4 v0.8.4
Florian Westphal [Tue, 1 May 2018 09:14:58 +0000 (11:14 +0200)] 
build: Bump version to v0.8.4

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agorule: do not hardcode ingress when printing flowtable
Pablo Neira Ayuso [Wed, 25 Apr 2018 16:50:26 +0000 (18:50 +0200)] 
rule: do not hardcode ingress when printing flowtable

Call hook number to string function instead.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoevaluate: missing flowtable evaluation from nested notation
Pablo Neira Ayuso [Wed, 25 Apr 2018 16:51:08 +0000 (18:51 +0200)] 
evaluate: missing flowtable evaluation from nested notation

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: fix --debug mnl not producing output
Duncan Roe [Wed, 25 Apr 2018 23:23:49 +0000 (09:23 +1000)] 
src: fix --debug mnl not producing output

cache_update() needs to accept the full debug mask instead of a boolean of
NFT_DEBUG_NETLINK, because called functions may wish to check other bits
(NFT_DEBUG_MNL in particular).

Signed-off-by: Duncan Roe <duncan_roe@optusnet.com.au>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agodoc: reword insert position, this expects rule handle to insert, not a relative postition
Florian Westphal [Tue, 24 Apr 2018 14:54:52 +0000 (16:54 +0200)] 
doc: reword insert position, this expects rule handle to insert, not a relative postition

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agostatement: Fix get_rate() for zero byte_rate
Phil Sutter [Tue, 24 Apr 2018 09:46:01 +0000 (11:46 +0200)] 
statement: Fix get_rate() for zero byte_rate

The algorithm didn't detect whether given byte_rate was zero,
pointlessly iterating through data units. Make it exit early in this
case.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agorule: Free flowtable in handle_free()
Phil Sutter [Tue, 24 Apr 2018 09:44:19 +0000 (11:44 +0200)] 
rule: Free flowtable in handle_free()

Fixes: db0697ce7f602 ("src: support for flowtable listing")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agonetlink: no EINTR handling from netlink_get_setelem()
Pablo Neira Ayuso [Tue, 24 Apr 2018 09:53:54 +0000 (11:53 +0200)] 
netlink: no EINTR handling from netlink_get_setelem()

This cannot happen, this call does not set the NLM_F_DUMP flag.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: centralize netlink error reporting
Pablo Neira Ayuso [Tue, 24 Apr 2018 09:33:34 +0000 (11:33 +0200)] 
src: centralize netlink error reporting

Consolidate error reporting from do_command() call.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: simplify netlink_get_setelems() and rename it to netlink_list_setelems()
Pablo Neira Ayuso [Mon, 23 Apr 2018 23:21:32 +0000 (01:21 +0200)] 
src: simplify netlink_get_setelems() and rename it to netlink_list_setelems()

This is called from cache population path, remove netlink_io_error()
call since this is not needed. Rename it for consistency with similar
netlink_list_*() NLM_F_DUMP functions. Get rid of location parameter.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agonetlink: remove unused function declarations
Pablo Neira Ayuso [Mon, 23 Apr 2018 23:05:13 +0000 (01:05 +0200)] 
netlink: remove unused function declarations

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agonetlink: don't pass location to netlink_list_*() function
Pablo Neira Ayuso [Mon, 23 Apr 2018 23:05:12 +0000 (01:05 +0200)] 
netlink: don't pass location to netlink_list_*() function

Not needed anymore.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agonetlink: netlink_list_chains() callers always wants all existing chains
Pablo Neira Ayuso [Mon, 23 Apr 2018 23:05:11 +0000 (01:05 +0200)] 
netlink: netlink_list_chains() callers always wants all existing chains

Remove dead code, callers always need this to dump all of the existing
chains.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agonetlink: pass cmd object to netlink function calls
Pablo Neira Ayuso [Mon, 23 Apr 2018 23:05:10 +0000 (01:05 +0200)] 
netlink: pass cmd object to netlink function calls

Simplify function footprint.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agonetlink: remove dead netlink_io_error() calls
Pablo Neira Ayuso [Fri, 20 Apr 2018 10:30:01 +0000 (12:30 +0200)] 
netlink: remove dead netlink_io_error() calls

This error path is never entered since mnl_nft_*_batch_{add,del,replace}
calls never fail, and if they ever do fail it will be because we are
hitting OOM, in such case we can display a more generic non-netlink
error.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agomnl: remove non-batch netlink code
Pablo Neira Ayuso [Fri, 20 Apr 2018 10:06:26 +0000 (12:06 +0200)] 
mnl: remove non-batch netlink code

This functions have no clients anymore.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoevaluate: clear expression context before cmd evaluation
Florian Westphal [Thu, 19 Apr 2018 16:18:57 +0000 (18:18 +0200)] 
evaluate: clear expression context before cmd evaluation

We also need to clear expr ctx before we eval a command.
This is a followup fix to 'evaluate: reset eval context when evaluating
set definitions'.

The first patch only fixed set evaluation when dealing with
a complete table representation rather than individual commands.

Reported-by: David Fabian <david.fabian@bosson.cz>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agosrc: use ibrname and obrname
Pablo Neira Ayuso [Thu, 19 Apr 2018 12:32:25 +0000 (14:32 +0200)] 
src: use ibrname and obrname

Legacy tool name is 'brctl' and so the 'br' prefix is already known. If
we use ibrname and obrname it looks consistent with iifname and oifname.
So let's this instead of ibridgename and obridgename since Florian likes
this too.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoparser_bison: missing rules for IBRIDGENAME and OBRIDGENAME
Pablo Neira Ayuso [Thu, 19 Apr 2018 11:25:38 +0000 (13:25 +0200)] 
parser_bison: missing rules for IBRIDGENAME and OBRIDGENAME

Fixes: 3baa28f24b3d ("src: rename ibrportname, obrportname")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoexpression: fix constant expression allocation on big endian with partial bytes
Sabrina Dubroca [Thu, 19 Apr 2018 08:45:23 +0000 (10:45 +0200)] 
expression: fix constant expression allocation on big endian with partial bytes

Commit 5259feeb7cda ("expression: fix constant expression allocation on
big endian") improved constant handling on big endian, but didn't handle
the case of partial bytes correctly.

Currently, constant_data_ptr(val, 6) points to the item after val,
instead of the last byte of val.

Thanks to Stefano for providing the correct expression.

Fixes: 5259feeb7cda ("expression: fix constant expression allocation on big endian")
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoevaluate: reset eval context when evaluating set definitions
Florian Westphal [Wed, 18 Apr 2018 12:07:09 +0000 (14:07 +0200)] 
evaluate: reset eval context when evaluating set definitions

David reported nft chokes on this:
nft -f /tmp/A
/tmp/A:9:22-45: Error: datatype mismatch, expected concatenation of (IPv4 address, internet network service, IPv4 address), expression has type concatenation of (IPv4 address, internet network service)
cat /tmp/A
flush ruleset;
table ip filter {
set setA {
type ipv4_addr . inet_service . ipv4_addr
flags timeout
}
set setB {
type ipv4_addr . inet_service
flags timeout
}
}

Problem is we leak set definition details of setA to setB via eval
context, so reset this.

Also add test case for this.

Reported-by: David Fabian <david.fabian@bosson.cz>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agosrc: rename ibrportname, obrportname
Florian Westphal [Fri, 13 Apr 2018 12:36:32 +0000 (14:36 +0200)] 
src: rename ibrportname, obrportname

For bridge, iifname is the port name, whereas 'ibrport' is the
logical name of the bridge ("br0") the port ("iifname") is enslaved to.

So, 'ibrport' is a misnomer.

libnftl calls these 'bri_iifname' and 'bri_oifname', which is good
but using 'briiifname' in nft is rather ugly, so use 'ibridgename'
and 'obridgename' instead.

Old names are still recognized, listing shows the new names.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoscanner: add helpers token
Florian Westphal [Fri, 13 Apr 2018 09:40:50 +0000 (11:40 +0200)] 
scanner: add helpers token

without it, you get:
 nft list ct helpers table filter
 Error: syntax error, unexpected string, expecting helper or helpers

Fixes: 14fd3ad720f6e ("src: prepare for future ct timeout policy support")
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoparser_bison: Pass struct nft_ctx to parser_init()
Phil Sutter [Fri, 13 Apr 2018 14:52:35 +0000 (16:52 +0200)] 
parser_bison: Pass struct nft_ctx to parser_init()

Signature of parser_init() got quite huge, so simply pass the whole
context pointer to it - most of the parameters are just taken from there
anyway.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agolibnftables: Keep cmds list outside of parser_state
Phil Sutter [Fri, 13 Apr 2018 14:52:34 +0000 (16:52 +0200)] 
libnftables: Keep cmds list outside of parser_state

Parser basically turns input into a list of commands and error messages.
Having the commands list being part of struct parser_state does not make
sense from this point of view, also it will have to go away with
upcoming JSON support anyway.

While being at it, change nft_netlink() to take just the list of
commands instead of the whole parser state as parameter, also take care
of command freeing in nft_run_cmd_from_* functions (where the list
resides as auto-variable) instead of from inside nft_run().

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoct: Remove duplicate include of gmputil.h
Phil Sutter [Fri, 13 Apr 2018 14:52:33 +0000 (16:52 +0200)] 
ct: Remove duplicate include of gmputil.h

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoReview .gitignore files contents
Phil Sutter [Fri, 13 Apr 2018 14:52:32 +0000 (16:52 +0200)] 
Review .gitignore files contents

* Move entries belonging to src/ into src/.gitignore.
* Drop lines for files inside build-aux/ since that is ignored already.
* No need to ignore src/Makefile.in, the entry 'Makefile.in' catches
  that already.
* Remove entry for '.*.d', (recent?) autotools doesn't create any
  matching files.
* Drop entries for non-existent parser.c and parser.h files.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/py: Fix differences printing for rules with sets
Phil Sutter [Fri, 13 Apr 2018 14:52:31 +0000 (16:52 +0200)] 
tests/py: Fix differences printing for rules with sets

If a rule contained a set, it would always print the tested rule as
differing from the output instead of any given output (if defined).

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/py: rt.t: Drop needless rule output
Phil Sutter [Fri, 13 Apr 2018 14:52:30 +0000 (16:52 +0200)] 
tests/py: rt.t: Drop needless rule output

It is identical to the tested rule, so no point in keeping it.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosegtree: Fix for last elem at interval end
Phil Sutter [Fri, 13 Apr 2018 14:52:29 +0000 (16:52 +0200)] 
segtree: Fix for last elem at interval end

Unclosed interval check at end of interval_map_decompose() missed to
check whether interval start is the last possible element in given set
before creating a range expression. This led to the last element
incorrectly printed as range from itself to itself. Fix this by
comparing the upper boundary against the lower one.

In order to keep indenting level low, invert the entry check and jump to
the end if it matches.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoReview raw payload allocation points
Phil Sutter [Fri, 13 Apr 2018 14:52:28 +0000 (16:52 +0200)] 
Review raw payload allocation points

In parser_bison.y, call payload_init_raw() instead of assigning all
fields manually. Also drop manual initialization of flags field: it is
not touched in allocation path, so no need for that.

In stmt_evaluate_payload(), setting dtype field is redundant since
payload_init_raw() does that already.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosegtree: Fix memory leaks
Phil Sutter [Fri, 13 Apr 2018 14:52:27 +0000 (16:52 +0200)] 
segtree: Fix memory leaks

This fixes memory leaks in three places:

* set_overlap():

The allocated intervals have to be freed again before returning to
caller. While being at it, reduce indenting level in said function to
stay below 80 columns boundary.

* range_is_prefix():
* interval_map_decompose():

GMP documentation suggests to call mpz_clear() for all mpz_t type
variables once they are not used anymore to free the space they occupy.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agorule: Free table->objs in table_free()
Phil Sutter [Fri, 13 Apr 2018 14:52:26 +0000 (16:52 +0200)] 
rule: Free table->objs in table_free()

This fixes a memory leak identified by valgrind.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agolibnftables: Fix forgotten bit after introducing error_fp
Phil Sutter [Fri, 13 Apr 2018 14:52:25 +0000 (16:52 +0200)] 
libnftables: Fix forgotten bit after introducing error_fp

Like nft_run_cmd_from_buffer, nft_run_cmd_from_filename doesn't need to
change output_fp temporarily when printing error records anymore.

Fixes: 4176e24e14f07 ("libnftables: Introduce nft_ctx_set_error()")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoerec: Review erec_print()
Phil Sutter [Fri, 13 Apr 2018 14:52:24 +0000 (16:52 +0200)] 
erec: Review erec_print()

A new requirement to erec for the upcoming JSON support is printing
records with file input descriptors without open stream. The approach is
to treat 'name' field as file name, open it, extract the offending line
and close it again.

Further changes to libnftables input parsing routines though have shown
that the whole concept of file pointer reuse in erec is tedious and not
worth keeping:

* Closed files are to be supported as well, so there needs to be
  fallback code for opening the file anyway.

* When input descriptor is duplicated from parser state into an error
  record, the file pointer is copied as well. Therefore care has to be
  taken to not free the parser state before any error records have been
  printed. This is the only point where old and duplicated input
  descriptors are connected.

Therefore drop struct input_descriptor's 'fp' field and just always open
the file by name. This way also the old stream offset doesn't have to be
restored after reading.

While being at it, this patch fixes two other (potential) problems:

* If the offending line from input contains tabs, add them at the right
  position in the marker buffer as well to avoid misalignment.

* The input file may not be seekable (/dev/stdin for instance), so skip
  printing of offending line and markers if it couldn't be read
  properly.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agocli: Drop String termination workaround
Phil Sutter [Wed, 11 Apr 2018 08:21:35 +0000 (10:21 +0200)] 
cli: Drop String termination workaround

This spot was missed by commit 2b3f18e0cf7a7 ("libnftables: Fix for
input without trailing newline") - since line termination is now added
in nft_run_cmd_from_buffer(), cli is relieved from doing so.

Fixes: 2b3f18e0cf7a7 ("libnftables: Fix for input without trailing newline")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests: py: remove object ID from payload file
Pablo Neira Ayuso [Wed, 11 Apr 2018 07:56:25 +0000 (09:56 +0200)] 
tests: py: remove object ID from payload file

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/py: Allow passing multiple files to nft-test.py
Phil Sutter [Tue, 10 Apr 2018 17:00:26 +0000 (19:00 +0200)] 
tests/py: Allow passing multiple files to nft-test.py

This allows to run the testsuite over a subset of test cases, e.g. like
so:

| ./nft-test.py bridge/*.t

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/py: Review print statements in nft-test.py
Phil Sutter [Tue, 10 Apr 2018 17:00:25 +0000 (19:00 +0200)] 
tests/py: Review print statements in nft-test.py

Make use of format strings as they are easier to read than manual string
concatenation.

Also use class Table's __str__ method instead of printing the 'name'
attribute. This changes the output in that table names are prepended by
their family, but the extra information may come in handy when analyzing
issues.

Since class Chain's __str__ method returns just the 'name' attribute
content, it may be used as synonym.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/py: Use libnftables instead of calling nft binary
Phil Sutter [Tue, 10 Apr 2018 17:00:24 +0000 (19:00 +0200)] 
tests/py: Use libnftables instead of calling nft binary

This adds a simple nftables Python class in py/nftables.py which gives
access to libnftables API via ctypes module.

nft-test.py is extended to make use of the above class instead of
calling nft binary. Since command line formatting had to be touched
anyway, this patch also streamlines things a bit by introducing
__str__ methods to classes Table and Chain and making extensive use of
format strings instead of onerously adding all string parts together.

Since the called commands don't see a shell anymore, all shell meta
character escaping done in testcases is removed.

The visible effects of this change are:

* Four new warnings in ip/flowtable.t due to changing objref IDs (will
  be addressed later in a patch to libnftnl).

* Reported command line in warning and error messages changed slightly
  for obvious reasons.

* Reduction of a full test run's runtime by a factor of four. Status
  diff after running with 'time':

  < 83 test files, 77 files passed, 1724 unit tests, 0 error, 33 warning
  < 87.23user 696.13system 15:11.82elapsed 85%CPU (0avgtext+0avgdata 9604maxresident)k
  < 8inputs+36800outputs (0major+35171235minor)pagefaults 0swaps

  > 83 test files, 77 files passed, 1724 unit tests, 4 error, 33 warning
  > 6.80user 30.18system 3:45.86elapsed 16%CPU (0avgtext+0avgdata 14064maxresident)k
  > 0inputs+35808outputs (0major+2874minor)pagefaults 0swaps

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agolibnftables: Simplify cookie integration
Phil Sutter [Tue, 10 Apr 2018 17:00:23 +0000 (19:00 +0200)] 
libnftables: Simplify cookie integration

This increases the size of struct output_ctx quite a bit, but allows to
simplify internal functions dealing with the cookies mainly because
output_fp becomes accessible from struct cookie.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agolibnftables: Support buffering output and error
Phil Sutter [Tue, 10 Apr 2018 17:00:22 +0000 (19:00 +0200)] 
libnftables: Support buffering output and error

When integrating libnftables into Python code using ctypes module,
having to use a FILE pointer for output becomes a show-stopper.
Therefore make Python hackers' lives (a little) less painful by
providing convenience functions to setup buffering output and error
streams using fopencookie() and retrieving the buffers.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agolibnftables: Introduce nft_ctx_set_error()
Phil Sutter [Tue, 10 Apr 2018 17:00:21 +0000 (19:00 +0200)] 
libnftables: Introduce nft_ctx_set_error()

Analogous to nft_ctx_set_output(), this allows to set a custom file
pointer for writing error messages to.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agolibnftables: Fix for input without trailing newline
Phil Sutter [Tue, 10 Apr 2018 17:00:20 +0000 (19:00 +0200)] 
libnftables: Fix for input without trailing newline

Input parser implementation requires a newline at end of input,
otherwise the last pattern may not be recognized correctly.

If input comes from a file, the culprit was YY_INPUT macro not expecting
the last line not ending with a newline, so the last word wasn't
accepted. This is easily fixed by checking for feof(yyin) in there. A
simple test case for that is:

| echo -en "table ip t {\nchain c {\n}\n}" >/tmp/foo
| nft -f /tmp/foo

Input from a string buffer is a bit more tricky: The culprit here is
that detection of classid pattern is done by checking the character
following it which makes it impossible to sit right at end of input and
I haven't found an alternative to that. After dropping the manual
newline appending when combining argv into a single buffer in main(),
a rule like this won't be recognized anymore:

| nft add rule ip t c meta priority feed:babe

Since a direct call to run_cmd_from_buffer() via libnftables bypasses
the sanitizing done in main() entirely, it has to happen in libnftables
instead which means creating a newline-terminated duplicate of the input
buffer.

Note that main() created a buffer one byte longer than needed since it
accounts for whitespace at end of each argv but doesn't add it to the
buffer for the last one, so buffer length is reduced by two bytes
instead of just one although only one less character is printed into it.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoct: Fix output_fp bypass in ct_print()
Phil Sutter [Tue, 10 Apr 2018 17:00:19 +0000 (19:00 +0200)] 
ct: Fix output_fp bypass in ct_print()

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoconfigure: don't enable xtables when --without-xtables is passed
Florian Westphal [Wed, 4 Apr 2018 11:50:08 +0000 (13:50 +0200)] 
configure: don't enable xtables when --without-xtables is passed

AC_ARG_WITH runs this when EITHER --with-foo or --without-foo is given,
so use 'withval'.

After this patch:
./configure -> xtables off
./configure --with-xtables -> xtables on
./configure --without-xtables -> xtables off (was on).

Reported-by: Alexander Dahl <ada@thorsis.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoevaluate: use recursive call for SET_REF handling
Florian Westphal [Mon, 2 Apr 2018 19:26:07 +0000 (21:26 +0200)] 
evaluate: use recursive call for SET_REF handling

We can now call the helper again, with set->init as new RHS expression.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoevaluate: handle EXPR_MAPPING
Florian Westphal [Tue, 3 Apr 2018 11:16:31 +0000 (13:16 +0200)] 
evaluate: handle EXPR_MAPPING

Needed by followup patch.  EXPR_SET_REF handling is bonkers, it
"works" when using { key : value } because ->key and ->left are aliased
in struct expr to the same location.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoevaluate: split binop xfer to separate function
Florian Westphal [Mon, 2 Apr 2018 18:36:56 +0000 (20:36 +0200)] 
evaluate: split binop xfer to separate function

to reuse this in a followup patch.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoevaluate: move lhs fixup to a helper
Florian Westphal [Mon, 2 Apr 2018 18:34:23 +0000 (20:34 +0200)] 
evaluate: move lhs fixup to a helper

... to reuse this in a followup patch.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agonetlink: floating point exception with concatenations
Pablo Neira Ayuso [Tue, 3 Apr 2018 11:03:39 +0000 (13:03 +0200)] 
netlink: floating point exception with concatenations

If the length is less than 1 byte, this codepath hits a floating point
exception, use div_round_up() as everywhere else in the code.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoevaluate: propagate binop_transfer() adjustment to set key size
Pablo Neira Ayuso [Tue, 3 Apr 2018 11:00:31 +0000 (13:00 +0200)] 
evaluate: propagate binop_transfer() adjustment to set key size

The right shift transfer may be result in adjusting the set key size,
eg. ip6 dscp results in fetching 6 bits that are splitted between two
bytes, hence the set element ends up being 16 bytes long.

Reported-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoevaluate: do not inconditionally update cache from flush command
Pablo Neira Ayuso [Sun, 1 Apr 2018 18:10:08 +0000 (20:10 +0200)] 
evaluate: do not inconditionally update cache from flush command

This is only required by sets, maps and meters, skip cache.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: use mini-gmp 6.1.2 stable release
Pablo Neira Ayuso [Sat, 31 Mar 2018 19:52:35 +0000 (21:52 +0200)] 
src: use mini-gmp 6.1.2 stable release

Instead of a snapshot from the gmp hg repository.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests: ip6: fix 'missing payload' error
Florian Westphal [Sat, 31 Mar 2018 22:24:19 +0000 (00:24 +0200)] 
tests: ip6: fix 'missing payload' error

ERROR: line 2: did not find payload information for rule 'iifname "eth0" ct state established,new tcp dport vmap {22 : drop, 222 : drop } masquerade'

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agonft-test: check start of rule with sets too
Florian Westphal [Sat, 31 Mar 2018 09:08:33 +0000 (11:08 +0200)] 
nft-test: check start of rule with sets too

We special case rules with sets ({}) so set members are sorted properly.

But we failed to check start of rule:
input:  meta mark { 1, 2 } bar
expect: meta mark { 1, 2 } bar

We made sure 'bar' is equal in both input and expected output,
but we did not check start (before {).

This causes a gazillion of warnings, will fix in followup commit.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agotests: ct, meta: fixes for upcoming nft-test.py fix
Florian Westphal [Sat, 31 Mar 2018 13:34:35 +0000 (15:34 +0200)] 
tests: ct, meta: fixes for upcoming nft-test.py fix

nft-test.py currently fails to properly compare tests involving a set,
after that bug is fixed these lines would fail, so fix this up before.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agotests: mh: fix expected test output
Florian Westphal [Sat, 31 Mar 2018 10:10:53 +0000 (12:10 +0200)] 
tests: mh: fix expected test output

Forgot to include '!=', this doesn't trigger at the moment due to
a bug in nft-test.py, so fix this before fixing our test script.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agotests: redirect: fix tests for upcoming nft-test.py fix
Florian Westphal [Sat, 31 Mar 2018 14:18:55 +0000 (16:18 +0200)] 
tests: redirect: fix tests for upcoming nft-test.py fix

nft-test.py currently fails to properly compare tests involving a set,
after that bug is fixed these lines would fail, so fix this up before.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agonetlink_delinearize: kill dependency before eval of 'redirect' stmt
Florian Westphal [Sat, 31 Mar 2018 14:19:07 +0000 (16:19 +0200)] 
netlink_delinearize: kill dependency before eval of 'redirect' stmt

ip protocol 6 redirect to :tcp dport map { 22 : 8000, 80 : 8080}
   is printed as
redirect to :tcp dport map { 22 : 8000, 80 : 8080}

but that input yields:
Error: transport protocol mapping is only valid after transport protocol match

so kill dependencies beforehand so nft won't remove it.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agostatement: print space before "{" in set add
Florian Westphal [Sat, 31 Mar 2018 13:51:33 +0000 (15:51 +0200)] 
statement: print space before "{" in set add

old:
add @set5{ ip6 saddr . ip6 daddr}
new:
add @set5 { ip6 saddr . ip6 daddr}

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agosrc: Updates for mini-gmp.{c,h}
Harsha Sharma [Sat, 31 Mar 2018 14:49:41 +0000 (20:19 +0530)] 
src: Updates for mini-gmp.{c,h}

updates from latest stable release of libgmp to get in sync with them

Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoRevert "payload: don't remove icmp family dependency in special cases"
Florian Westphal [Wed, 28 Mar 2018 08:38:12 +0000 (10:38 +0200)] 
Revert "payload: don't remove icmp family dependency in special cases"

This reverts commit 126706c23c0458b07d54550dc27561b30f8a43f2.

As its now ok to use icmp-in-ip6 family we can remove the dependency.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoproto: permit icmp-in-ipv6 and icmpv6-in-ipv4
Florian Westphal [Wed, 28 Mar 2018 08:38:06 +0000 (10:38 +0200)] 
proto: permit icmp-in-ipv6 and icmpv6-in-ipv4

Jozsef points out that
 meta l4proto icmp icmp type destination-unreachable

is hard to read.  So, lets just add icmp/icmpv6 to
ip/ip6 protocol base so users can just go with

 icmp type destination-unreachable

and let nft fill in needed dependency.
After this patch, the recent patch to not remove the
dependency can be reverted again.

Suggested-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agopayload: don't remove icmp family dependency in special cases
Florian Westphal [Tue, 27 Mar 2018 08:18:18 +0000 (10:18 +0200)] 
payload: don't remove icmp family dependency in special cases

When using nftables to filter icmp-in-ipv6 or icmpv6-in-ipv4 we
erronously removed the dependency, i.e. "lis ruleset" shows

table ip6 filter { chain output {
type filter hook output priority 0; policy accept;
icmp type destination-unreachable
} }

but that won't restore because of ip vs ipv6 conflict.

After this patch, this lists as

 meta l4proto icmp icmp type destination-unreachable

instead.  We still remove the dependency in "ip" family.

Same applies to icmpv6-in-ip.

Reported-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agotests: meta: icmp, icmpv6: don't kill required dependencies
Florian Westphal [Tue, 27 Mar 2018 10:11:04 +0000 (12:11 +0200)] 
tests: meta: icmp, icmpv6: don't kill required dependencies

when explicitly filtering icmp-in-ipv6 and icmp6-in-ip don't remove the
required l3 protocol dependency, else "nft list ruleset" can't be read
via nft -f anymore.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agosrc: avoid errouneous assert with map+concat
Florian Westphal [Tue, 27 Mar 2018 07:29:54 +0000 (09:29 +0200)] 
src: avoid errouneous assert with map+concat

Phil reported following assert:

add rule ip6 f o mark set ip6 saddr . ip6 daddr . tcp dport \
                        map { dead::beef . f00::. 22 : 1 }
nft: netlink_linearize.c:655: netlink_gen_expr: Assertion `dreg < ctx->reg_low' failed.

This happens because "mark set" will allocate one register (the dreg),
but netlink_gen_concat_expr will populate a lot more register space if
the concat expression strings a lot of expressions together.

As the assert is useful pseudo-reserve the register space as per
concat->len and undo after generating the expressions.

Reported-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agodoc: nft.8 more spelling fixes
Duncan Roe [Tue, 27 Mar 2018 04:17:01 +0000 (15:17 +1100)] 
doc: nft.8 more spelling fixes

I ran the following command:

ispell -p ./ispell_nft -H nft.xml

to create the local dictionary ispell_nft.
ispell_nft contains almost every special word in nft.xml.
The idea is that anyone can run ispell the same way and only have to accept:

 - alpha strings in hexadecimal numbers
 - "FIXME" : that has to be fixed eventually
 - "differv" : I don't know what that is or whether it's correct

You need to use the English (i.e. American) dictionary, and you want the screen
to be about 100 chars wide (at least).

The patch enforces consistent capitalisation of words, e.g. IPv4 is always that
way but ipv4_addr stays as before. The existing dictionary suggested capital
Ethernet so that is in there too.

Signed-off-by: Duncan Roe <duncan_roe@optusnet.com.au>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoremove todo list
Florian Westphal [Mon, 26 Mar 2018 06:51:55 +0000 (08:51 +0200)] 
remove todo list

its outdated.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoExport libnftables (again)
Phil Sutter [Wed, 21 Mar 2018 13:16:40 +0000 (14:16 +0100)] 
Export libnftables (again)

This reverts commits d572d59788143945c2a638f12a5227d9b21ce489 and
9f5e49e147219cd161de8cd80cadf8a444969ef0.

Current libnftables API should be stable enough to release it into the
public, and after 4aba100e593f ("rule: reset cache iff there is an
existing cache") we have a simple way to batch commands through this
API.

Suggested-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/shell: Test flush and nat chain recreate in one go
Phil Sutter [Wed, 21 Mar 2018 11:59:11 +0000 (12:59 +0100)] 
tests/shell: Test flush and nat chain recreate in one go

This tests what kernel commit ae6153b50f9bf ("netfilter: nf_tables:
permit second nat hook if colliding hook is going away") fixed for.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agotests/shell: Allow to specify multiple testcases
Phil Sutter [Mon, 19 Mar 2018 17:02:07 +0000 (18:02 +0100)] 
tests/shell: Allow to specify multiple testcases

Extend run-tests.sh a bit so that all remaining arguments after option
parsing are treated as filenames to test and complain if one doesn't
seem like such. This allows for doing stuff like:

| ./run-tests.sh testcases/include/000*

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/shell: Fix sporadic fail of include/0007glob_double_0
Phil Sutter [Mon, 19 Mar 2018 17:02:06 +0000 (18:02 +0100)] 
tests/shell: Fix sporadic fail of include/0007glob_double_0

Since ruleset listing shows tables sorted by handle (which in turn
depends on table creation ordering), using random filenames here
guarantees to make the test fail randomly.

Since the include files reside in a temporary directory anyway, there is
no need to randomize their names so simplify the whole test a bit.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoflowtable: Make parsing a little more robust
Phil Sutter [Mon, 19 Mar 2018 17:02:05 +0000 (18:02 +0100)] 
flowtable: Make parsing a little more robust

It was surprisingly easy to crash nft with invalid syntax in 'add
flowtable' command. Catch at least three possible ways (illustrated in
provided test case) by making evaluation phase survive so that bison
gets a chance to complain.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/shell: Fix flowtable test cases
Phil Sutter [Mon, 19 Mar 2018 17:02:04 +0000 (18:02 +0100)] 
tests/shell: Fix flowtable test cases

The major problem here was that existence of network interfaces 'eth0'
and 'wlan0' was assumed. Overcome this by just using 'lo' instead, which
exists even in newly created netns by default.

Another minor issue was false naming of 0004delete_after_add0 - the
expected return code is supposed to be separated by '_' from the
remaining filename.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/shell: Fix dump of chains/0016delete_handle_0
Phil Sutter [Mon, 19 Mar 2018 17:02:03 +0000 (18:02 +0100)] 
tests/shell: Fix dump of chains/0016delete_handle_0

The purpose of this test is to delete some chains by their handle and
that is supposed to succeed. So the respective dump should not contain
them anymore.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoSupport 'nft -f -' to read from stdin
Phil Sutter [Mon, 19 Mar 2018 17:02:02 +0000 (18:02 +0100)] 
Support 'nft -f -' to read from stdin

In libnftables, detect if given filename is '-' and treat it as the
common way of requesting to read from stdin, then open /dev/stdin
instead. (Calling 'nft -f /dev/stdin' worked before as well, but this
makes it official.)

With this in place and bash's support for here strings, review all tests
in tests/shell for needless use of temp files. Note that two categories
of test cases were intentionally left unchanged:

- Tests creating potentially large rulesets to avoid running into shell
  parameter length limits.
- Tests for 'include' directive for obvious reasons.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agorule: reset cache iff there is an existing cache
Pablo Neira Ayuso [Sun, 18 Mar 2018 20:31:29 +0000 (21:31 +0100)] 
rule: reset cache iff there is an existing cache

If genid is unset, then do not reset existing cache. In the kernel,
generation ID is assumed to be always != zero.

This patch fixes:

nft 'add table x; add chain x y;'

that allow us to send several commands in one single batch from the
command line.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agodoc: nft.8 aim for consistent synopses throughout (again)
Duncan Roe [Mon, 19 Mar 2018 03:12:50 +0000 (14:12 +1100)] 
doc: nft.8 aim for consistent synopses throughout (again)

Fix a few more items as per commit f9cb9580b924f6320005f429f7d59e52a38aff82

Also insert a missing space I noticed along the way

Signed-off-by: Duncan Roe <duncan_roe@optusnet.com.au>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agodoc: describe table dormant flag
Florian Westphal [Sat, 17 Mar 2018 11:00:27 +0000 (12:00 +0100)] 
doc: describe table dormant flag

also mention how to quit interactive mode and provide
small table add example.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoCombine redir and masq statements into nat
Phil Sutter [Sat, 17 Mar 2018 09:39:27 +0000 (10:39 +0100)] 
Combine redir and masq statements into nat

All these statements are very similar, handling them with the same code
is obvious. The only thing required here is a custom extension of enum
nft_nat_types which is used in nat_stmt to distinguish between snat and
dnat already. Though since enum nft_nat_types is part of kernel uAPI,
create a local extended version containing the additional fields.

Note that nat statement printing got a bit more complicated to get the
number of spaces right for every possible combination of attributes.

Note also that there wasn't a case for STMT_MASQ in
rule_parse_postprocess(), which seems like a bug. Since STMT_MASQ became
just a variant of STMT_NAT, postprocessing will take place for it now
anyway.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: evaluate: add preliminary binop transfer support for vmaps
Florian Westphal [Thu, 11 Jan 2018 15:30:23 +0000 (16:30 +0100)] 
src: evaluate: add preliminary binop transfer support for vmaps

nftables doesn't support vmap with bit-sized headers, such as flow label or dscp:

nft add rule ip filter input ip dscp vmap \{ 4 : accept, 63 : continue \}
BUG: invalid binary operation 5

Unlike plain "ip dscp { 4, 63 }", we don't have a relational operation in
case of vmap. Binop fixups need to be done when evaluating map statements.

This patch is incomplete. 'ip dscp' works, but this won't:
  nft add rule --debug=netlink ip6 test-ip6 input ip6 dscp vmap { 0x04 : accept, 0x3f : continue }

The generated expressions look sane, however there is disagreement on
the sets key size vs. the sizes of the individual elements in the set.
This is because ip6 dscp spans a byte boundary.

Key set size is still set to one byte (dscp type is 6bits).
However, binop expansion requirements result in 2 byte loads, i.e.
set members will be 2 bytes in size as well.

This can hopefully get addressed in an incremental patch.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoevaluate: handle binop adjustment recursively
Florian Westphal [Thu, 11 Jan 2018 15:30:22 +0000 (16:30 +0100)] 
evaluate: handle binop adjustment recursively

currently this is fine, but a followup commit will add
EXPR_SET_ELEM handling.

And unlike RANGE we cannot assume the key is a value.
Therefore make binop_can_transfer and binop_transfer_one handle
right hand recursively if needed.  For RANGE, call it again with
from/to.

For future SET_ELEM, we can then just call the function recursively
again with right->key as new RHS.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agosrc: segtree: use value expression length
Florian Westphal [Thu, 11 Jan 2018 15:30:20 +0000 (16:30 +0100)] 
src: segtree: use value expression length

In case of EXPR_MAPPING, expr->len is 0, we need to use
the length of the key instead.

Without this we can get assertion failure later on:
nft: netlink_delinearize.c:1484: binop_adjust_one: Assertion `value->len >= binop->right->len' failed.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agosrc: netlink_delinearize: don't assume element contains a value
Florian Westphal [Thu, 11 Jan 2018 15:30:21 +0000 (16:30 +0100)] 
src: netlink_delinearize: don't assume element contains a value

We cannot assume i->key->key is EXPR_VALUE, we could look e.g.  at a range,
which will trigger an assertion failure in binop_adjust_one().

We should call __binop_adjust recursively again in the EXPR_SET_ELEM case,
using key as new input.

Fixes: b8b8e7b6ae10 ("evaluate: transfer right shifts to set reference side")
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agotests: add test cases for vmap binop transfer
Florian Westphal [Thu, 11 Jan 2018 15:30:24 +0000 (16:30 +0100)] 
tests: add test cases for vmap binop transfer

they fail with 'BUG: invalid binary operation 5'.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agotests: add srh test cases
Florian Westphal [Fri, 16 Mar 2018 19:15:50 +0000 (20:15 +0100)] 
tests: add srh test cases

Signed-off-by: Florian Westphal <fw@strlen.de>