]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
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>
7 years agosrc: fix routing header support
Florian Westphal [Fri, 16 Mar 2018 16:39:29 +0000 (17:39 +0100)] 
src: fix routing header support

We can't use nft_exthdr_op to encode routing header, it breaks
ipv6 extension header support.

When encountering RT header, userspace did now set a new ipv6 exthdr mode,
but old kernel doesn't know about this, so this failed with -EOPNOTSUPP.

Revert that part and use NFT_EXTHDR_OP_IPV6.
When decoding a routing extension header, try the various route
types until we find a match.

Note this patch isn't complete:

'srh tag 127' creates following expressions:
  [ exthdr load 2b @ 43 + 6 => reg 1 ]
  [ cmp eq reg 1 0x00007f00 ]

It should instead insert a dependency test ("rt type 4"):
  [ exthdr load 1b @ 43 + 2 => reg 1 ]
  [ cmp eq reg 1 0x00000004 ]
  [ exthdr load 2b @ 43 + 6 => reg 1 ]
  [ cmp eq reg 1 0x00007e00 ]

nft should then use this to infer the routing header type.

While add it, document the srh option.

Fixes: 1400288f6d39d ("src: handle rt0 and rt2 properly")
Reported-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Ahmed Abdelsalam <amsalam20@gmail.com>
7 years agotests: update to new syntax to add/update set from packet path
Pablo Neira Ayuso [Fri, 16 Mar 2018 16:09:12 +0000 (17:09 +0100)] 
tests: update to new syntax to add/update set from packet path

Reported-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: revisit syntax to update sets and maps from packet path
Pablo Neira Ayuso [Fri, 16 Mar 2018 09:14:47 +0000 (10:14 +0100)] 
src: revisit syntax to update sets and maps from packet path

For sets, we allow this:

nft add rule x y ip protocol tcp update @y { ip saddr}

For maps:

table ip nftlb {
        map persistencia {
            type ipv4_addr : mark
            timeout 1h
            elements = { 192.168.1.132 expires 59m55s : 0x00000064,
                         192.168.56.101 expires 59m24s : 0x00000065 }
        }

        chain pre {
            type nat hook prerouting priority 0; policy accept;
            update @persistencia \
                { @nh,96,32 : numgen inc mod 2 offset 100 }
        }
    }

nft --debug=netlink add rule ip nftlb pre add @persistencia \
        { ip saddr : numgen inc mod 2 offset 100 }

More compact and it doesn't gets it confused with a simple map update
command (interesting that bison didn't spew any conflict error).

Former syntax for sets is preserved.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agonetlink: Fold netlink_gen_cmp() into netlink_gen_relational()
Phil Sutter [Thu, 15 Mar 2018 23:03:20 +0000 (00:03 +0100)] 
netlink: Fold netlink_gen_cmp() into netlink_gen_relational()

Since netlink_gen_relational() didn't do much anymore after meta OP
treating had been removed, it makes sense to merge it with the only
function it dispached to.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agorelational: Eliminate meta OPs
Phil Sutter [Thu, 15 Mar 2018 23:03:19 +0000 (00:03 +0100)] 
relational: Eliminate meta OPs

With a bit of code reorganization, relational meta OPs OP_RANGE,
OP_FLAGCMP and OP_LOOKUP become unused and can be removed. The only meta
OP left is OP_IMPLICIT which is usually treated as alias to OP_EQ.
Though it needs to stay in place for one reason: When matching against a
bitmask (e.g. TCP flags or conntrack states), it has a different
meaning:

| nft --debug=netlink add rule ip t c tcp flags syn
| ip t c
|   [ meta load l4proto => reg 1 ]
|   [ cmp eq reg 1 0x00000006 ]
|   [ payload load 1b @ transport header + 13 => reg 1 ]
|   [ bitwise reg 1 = (reg=1 & 0x00000002 ) ^ 0x00000000 ]
|   [ cmp neq reg 1 0x00000000 ]

| nft --debug=netlink add rule ip t c tcp flags == syn
| ip t c
|   [ meta load l4proto => reg 1 ]
|   [ cmp eq reg 1 0x00000006 ]
|   [ payload load 1b @ transport header + 13 => reg 1 ]
|   [ cmp eq reg 1 0x00000002 ]

OP_IMPLICIT creates a match which just checks the given flag is present,
while OP_EQ creates a match which ensures the given flag and no other is
present.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/shell: Use custom nft binary for ruleset listing
Phil Sutter [Thu, 15 Mar 2018 22:58:38 +0000 (23:58 +0100)] 
tests/shell: Use custom nft binary for ruleset listing

Don't assume the system's nft binary is able to correctly list rulesets
generated in tests.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoRevert "src: update dynamic set updates from packet path syntax"
Pablo Neira Ayuso [Fri, 16 Mar 2018 08:55:57 +0000 (09:55 +0100)] 
Revert "src: update dynamic set updates from packet path syntax"

This reverts commit 9047cc7ae746b1c9abd4e11ed476e37d8716d400, this is breaking
tests.

7 years agosrc: update dynamic set updates from packet path syntax
Pablo Neira Ayuso [Thu, 15 Mar 2018 18:38:59 +0000 (19:38 +0100)] 
src: update dynamic set updates from packet path syntax

New prefered syntax is:

{add,update} set { key } @name

 # nft list ruleset
 table ip x {
        set y {
                type ipv4_addr
        }

        chain y {
                ip protocol tcp add set { ip saddr} @y
        }
 }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: support of dynamic map addition and update of elements
Laura Garcia Liebana [Thu, 15 Mar 2018 08:23:21 +0000 (09:23 +0100)] 
src: support of dynamic map addition and update of elements

The support of dynamic adds and updates are only available for sets
and meters. This patch gives such abilities to maps as well.

This patch is useful in cases where dynamic population of maps are
required, for example, to maintain a persistence during some period
of time.

Example:

table ip nftlb {
    map persistencia {
        type ipv4_addr : mark
        timeout 1h
        elements = { 192.168.1.132 expires 59m55s : 0x00000064,
                     192.168.56.101 expires 59m24s : 0x00000065 }
    }

    chain pre {
        type nat hook prerouting priority 0; policy accept;
        map update \
            { @nh,96,32 : numgen inc mod 2 offset 100 } @persistencia
    }
}

An example of the netlink generated sequence:

 nft --debug=netlink add rule ip nftlb pre map add \
    { ip saddr : numgen inc mod 2 offset 100 } @persistencia
ip nftlb pre
  [ payload load 4b @ network header + 12 => reg 1 ]
  [ numgen reg 2 = inc mod 2 offset 100 ]
  [ dynset add reg_key 1 set persistencia sreg_data 2 ]

Signed-off-by: Laura Garcia Liebana <nevola@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agonetlink: use nftnl_flowtable_get/set
Florian Westphal [Mon, 12 Mar 2018 11:58:59 +0000 (12:58 +0100)] 
netlink: use nftnl_flowtable_get/set

the '_array' variant is just a wrapper for get/set api; this
allows the array variant to be removed from libnftnl.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agosrc: Adding support for segment routing header 'srh'
Ahmed Abdelsalam [Tue, 27 Feb 2018 10:04:14 +0000 (11:04 +0100)] 
src: Adding support for segment routing header 'srh'

Segment Routing Header "SRH" is new type of IPv6 Routing extension
header (type 4).

SRH contains a list of segments (each is represented as an IPv6 address)
to be visited by packets during the journey from source to destination.

The SRH specification are defined in the below IETF SRH draft.
https://tools.ietf.org/html/draft-ietf-6man-segment-routing-header-07

Signed-off-by: Ahmed Abdelsalam <amsalam20@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: handle rt0 and rt2 properly
Ahmed Abdelsalam [Tue, 27 Feb 2018 06:25:14 +0000 (07:25 +0100)] 
src: handle rt0 and rt2 properly

Type 0 and 2 of the IPv6 Routing extension header are not handled
properly by exthdr_init_raw() in src/exthdr.c

In order to fix the bug, we extended the "enum nft_exthdr_op" to
differentiate between rt, rt0, and rt2.

This patch should fix the bug. We tested the patch against the
same configuration reported in the bug and the output is as
shown below.

table ip6 filter {
chain input {
type filter hook input priority 0; policy accept;
rt0 addr[1] a::2
}
}

Fixes: Bugzilla #1219
Signed-off-by: Ahmed Abdelsalam <amsalam20@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agodoc: add set information and example for run-time blackhole
Florian Westphal [Sun, 11 Mar 2018 16:47:05 +0000 (17:47 +0100)] 
doc: add set information and example for run-time blackhole

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agotests: shell: fix tests for deletion via handle attribute
Harsha Sharma [Fri, 9 Mar 2018 14:22:28 +0000 (19:52 +0530)] 
tests: shell: fix tests for deletion via handle attribute

Fetch object, chain and set handles and with '-a' option and then delete
them.

Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agorule: print handle attribute in more clearer manner
Harsha Sharma [Thu, 8 Mar 2018 16:46:13 +0000 (22:16 +0530)] 
rule: print handle attribute in more clearer manner

Print handles in this way:

table ip filter { # handle 2
}
Similarly, for chain, set and object handles

Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests: shell: autogenerate dump verification
Laura Garcia Liebana [Wed, 7 Mar 2018 21:51:10 +0000 (22:51 +0100)] 
tests: shell: autogenerate dump verification

Complete the automated shell tests with the verification of
the test file dump, only for positive tests and if the test
execution was successful.

It's able to generate the dump file with the -g option.
Example:

 # ./run-tests.sh -g testcases/chains/0001jumps_0

The dump files are generated in the same path in the folder named
dumps/ with .nft extension.

It has been avoided the dump verification code in every test
file.

Signed-off-by: Laura Garcia Liebana <nevola@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: don't not dump set content from netlink_get_setelems()
Pablo Neira Ayuso [Wed, 7 Mar 2018 14:51:35 +0000 (15:51 +0100)] 
src: don't not dump set content from netlink_get_setelems()

This causes python tests to report payload mismatching errors.

Fixes: a43cc8d53096 ("src: support for get element command")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: remove unused batch support checks
Pablo Neira Ayuso [Wed, 7 Mar 2018 14:23:33 +0000 (15:23 +0100)] 
src: remove unused batch support checks

Follow up after cc8c5fd02448 ("netlink: remove non-batching routine").

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: move monitor code to src/monitor.c
Pablo Neira Ayuso [Wed, 7 Mar 2018 12:08:30 +0000 (13:08 +0100)] 
src: move monitor code to src/monitor.c

netlink.c is rather large file, move the monitor code to its own file.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Acked-by: Phil Sutter <phil@nwl.cc>
Acked-by: Arturo Borrero Gonzalez <arturo@netfilter.org>
7 years agosrc: support for get element command
Pablo Neira Ayuso [Tue, 6 Mar 2018 17:58:29 +0000 (18:58 +0100)] 
src: support for get element command

You need a Linux kernel >= 4.15 to use this feature.

This patch allows us to dump the content of an existing set.

 # nft list ruleset
 table ip x {
        set x {
                type ipv4_addr
                flags interval
                elements = { 1.1.1.1-2.2.2.2, 3.3.3.3,
                             5.5.5.5-6.6.6.6 }
        }
 }

You check if a single element exists in the set:

 # nft get element x x { 1.1.1.5 }
 table ip x {
        set x {
                type ipv4_addr
                flags interval
                elements = { 1.1.1.1-2.2.2.2 }
        }
 }

Output means '1.1.1.5' belongs to the '1.1.1.1-2.2.2.2' interval.

You can also check for intervals:

 # nft get element x x { 1.1.1.1-2.2.2.2 }
 table ip x {
        set x {
                type ipv4_addr
                flags interval
                elements = { 1.1.1.1-2.2.2.2 }
        }
 }

If you try to check for an element that doesn't exist, an error is
displayed.

 # nft get element x x { 1.1.1.0 }
 Error: Could not receive set elements: No such file or directory
 get element x x { 1.1.1.0 }
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

You can also check for multiple elements in one go:

 # nft get element x x { 1.1.1.5, 5.5.5.10 }
 table ip x {
        set x {
                type ipv4_addr
                flags interval
                elements = { 1.1.1.1-2.2.2.2, 5.5.5.5-6.6.6.6 }
        }
 }

You can also use this to fetch the existing timeout for specific
elements, in case you have a set with timeouts in place:

 # nft get element w z { 2.2.2.2 }
 table ip w {
        set z {
                type ipv4_addr
                timeout 30s
                elements = { 2.2.2.2 expires 17s }
        }
 }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoconfigure: bump libnftnl dependency
Pablo Neira Ayuso [Tue, 6 Mar 2018 11:02:53 +0000 (12:02 +0100)] 
configure: bump libnftnl dependency

Check for 1.1.0, which includes flowtable symbols.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests: shell: missing redirection to file
Pablo Neira Ayuso [Mon, 5 Mar 2018 17:52:51 +0000 (18:52 +0100)] 
tests: shell: missing redirection to file

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agorule: broken handle listing of table and named objects
Pablo Neira Ayuso [Mon, 5 Mar 2018 17:22:00 +0000 (18:22 +0100)] 
rule: broken handle listing of table and named objects

Now listing looks good:

 table ip test-ip {
        counter https-traffic {
                packets 0 bytes 0
        } # handle 1
 } # handle 847

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/shell: add tests for deletion of objects via object handle
Harsha Sharma [Thu, 18 Jan 2018 20:45:14 +0000 (02:15 +0530)] 
tests/shell: add tests for deletion of objects via object handle

Delete objects with given object handle

Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agodoc/nft.xml: Add deletion for objects via handles
Harsha Sharma [Tue, 23 Jan 2018 22:03:10 +0000 (03:33 +0530)] 
doc/nft.xml: Add deletion for objects via handles

Add documentation for deletion of tables, chains, sets and objects
via unique handles.

Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: extend nft to list object handle and delete objects via handle
Harsha Sharma [Thu, 18 Jan 2018 18:52:56 +0000 (00:22 +0530)] 
src: extend nft to list object handle and delete objects via handle

Print handle attributes in objects when listing via '-a' option and
delete objects via their unique object handles.
For e.g.

nft delete [<object-type>] [<family>] <table-name> [handle <handle>]

Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agorule: print object handle with --echo --handle
Pablo Neira Ayuso [Mon, 5 Mar 2018 16:25:45 +0000 (17:25 +0100)] 
rule: print object handle with --echo --handle

 # nft --echo --handle add counter x y
 add counter ip x y { packets 0 bytes 0 }  # handle 0

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agonetlink: print table handle with --echo --handle
Pablo Neira Ayuso [Mon, 5 Mar 2018 16:06:51 +0000 (17:06 +0100)] 
netlink: print table handle with --echo --handle

 # nft --echo --handle add table x
 add table ip x # handle 80

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agonetlink: print chain handle with --echo --handle
Pablo Neira Ayuso [Mon, 5 Mar 2018 16:01:19 +0000 (17:01 +0100)] 
netlink: print chain handle with --echo --handle

 # nft add --echo --handle chain x y
 add chain ip x y # handle 1

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/shell: add tests for deletion of sets via set handle
Harsha Sharma [Mon, 15 Jan 2018 11:51:33 +0000 (17:21 +0530)] 
tests/shell: add tests for deletion of sets via set handle

Delete set with given unique set handle.

Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/shell: add tests for deletion of chains via chain handle
Harsha Sharma [Sun, 14 Jan 2018 19:33:07 +0000 (01:03 +0530)] 
tests/shell: add tests for deletion of chains via chain handle

Delete chain with given unique handle for a table.

Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: list set handle and delete set via set handle
Harsha Sharma [Sun, 14 Jan 2018 16:18:46 +0000 (21:48 +0530)] 
src: list set handle and delete set via set handle

Print 'handle' attribute in sets when listing via '-a' option and
delete sets via their unique set handles listed with '-a' option.
For e.g.

nft delete set [<family>] <table-name> [handle <handle>]

Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoparser_bison: delete chain via chain handle
Harsha Sharma [Tue, 9 Jan 2018 18:59:42 +0000 (00:29 +0530)] 
parser_bison: delete chain via chain handle

This patch allows deletion of chains via unique chain handles which
can be listed with '-a' option and table name and family.
For eg.

nft delete chain [<family>] <table-name> [handle <handle>]

Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: Print handle attribute in chains
Harsha Sharma [Tue, 9 Jan 2018 18:59:01 +0000 (00:29 +0530)] 
src: Print handle attribute in chains

Print handle attribute in chains when listing via '-a' option.

Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoparser_bison: delete table via table handle
Harsha Sharma [Mon, 8 Jan 2018 17:57:07 +0000 (23:27 +0530)] 
parser_bison: delete table via table handle

This patch allows deletion of table via unique table handles and table
family which can be listed with '-a' option.
For.eg.
nft delete table [<family>] [handle <handle>]

Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: print 'handle' attribute in tables
Harsha Sharma [Sat, 23 Dec 2017 19:45:25 +0000 (11:45 -0800)] 
src: print 'handle' attribute in tables

Print 'handle' attribute in tables, when listing via '-a' option

For eg.
nft list ruleset -a

table ip test-ip4 {
chain input {
ip saddr 8.8.8.8 counter packets 0 bytes 0 # handle 3
}
 # handle 1}
table ip filter {
chain output {
tcp dport ssh counter packets 0 bytes 0 # handle 4
}
 # handle 2}
table ip xyz {
 # handle 3}

Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agodoc: nft: document flowtable
Pablo Neira Ayuso [Tue, 23 Jan 2018 11:58:30 +0000 (12:58 +0100)] 
doc: nft: document flowtable

Document the new flowtable objects available since Linux kernel 4.16-rc.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests: shell: add flowtable tests
Pablo Neira Ayuso [Mon, 22 Jan 2018 18:54:36 +0000 (19:54 +0100)] 
tests: shell: add flowtable tests

Add basic flowtable tests.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: flow offload support
Pablo Neira Ayuso [Sun, 3 Dec 2017 20:27:03 +0000 (21:27 +0100)] 
src: flow offload support

This patch allows us to refer to existing flowtables:

 # nft add rule x x flow offload @m

Packets matching this rule create an entry in the flow table 'm', hence,
follow up packets that get to the flowtable at ingress bypass the
classic forwarding path.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: delete flowtable
Pablo Neira Ayuso [Fri, 19 Jan 2018 00:41:38 +0000 (01:41 +0100)] 
src: delete flowtable

This patch allows you to delete an existing flowtable:

 # nft delete flowtable x m

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: add support to add flowtables
Pablo Neira Ayuso [Thu, 18 Jan 2018 07:43:23 +0000 (08:43 +0100)] 
src: add support to add flowtables

This patch allows you to create flowtable:

 # nft add table x
 # nft add flowtable x m { hook ingress priority 10\; devices = { eth0, wlan0 }\; }

You have to specify hook and priority. So far, only the ingress hook is
supported. The priority represents where this flowtable is placed in the
ingress hook, which is registered to the devices that the user
specifies.

You can also use the 'create' command instead to bail out in case that
there is an existing flowtable with this name.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: support for flowtable listing
Pablo Neira Ayuso [Mon, 4 Dec 2017 12:28:25 +0000 (13:28 +0100)] 
src: support for flowtable listing

This patch allows you to dump existing flowtable.

 # nft list ruleset
 table ip x {
        flowtable x {
                hook ingress priority 10
                devices = { eth0, tap0 }
        }
 }

You can also list existing flowtables via:

 # nft list flowtables
 table ip x {
        flowtable x {
                hook ingress priority 10
                devices = { eth0, tap0 }
        }
 }

 You need a Linux kernel >= 4.16-rc to test this new feature.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests: shell: set timeout and size combination coverage
Pablo Neira Ayuso [Mon, 5 Mar 2018 15:19:02 +0000 (16:19 +0100)] 
tests: shell: set timeout and size combination coverage

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests: shell: redefine and undefine
Pablo Neira Ayuso [Sat, 3 Mar 2018 22:45:11 +0000 (23:45 +0100)] 
tests: shell: redefine and undefine

This tests cover the new redefine and undefine scripting feature.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: add variable expression and use it to allow redefinitions
Pablo Neira Ayuso [Sat, 3 Mar 2018 21:52:35 +0000 (22:52 +0100)] 
src: add variable expression and use it to allow redefinitions

Add new variable expression that we can use to attach symbols in
runtime, this allows us to redefine variables via new keyword, eg.

 table ip x {
        chain y {
                define address = { 1.1.1.1, 2.2.2.2 }
                ip saddr $address
                redefine address = { 3.3.3.3 }
                ip saddr $address
        }
 }

 # nft list ruleset
 table ip x {
        chain y {
                ip saddr { 1.1.1.1, 2.2.2.2 }
                ip saddr { 3.3.3.3 }
        }
 }

Note that redefinition just places a new symbol version before the
existing one, so symbol lookups always find the latest version. The
undefine keyword decrements the reference counter and removes the symbol
from the list, so it cannot be used anymore. Still, previous references
to this symbol via variable expression are still valid.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agobuild: Bump version to v0.8.3 v0.8.3
Florian Westphal [Sat, 3 Mar 2018 10:50:23 +0000 (11:50 +0100)] 
build: Bump version to v0.8.3

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agosrc: datatype: prefer sscanf, avoid strncpy
Florian Westphal [Sat, 3 Mar 2018 09:57:54 +0000 (10:57 +0100)] 
src: datatype: prefer sscanf, avoid strncpy

similar to previous patch, but replace strncpy+atoi by sscanf.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agosrc: Use snprintf() over strncpy()
Harsha Sharma [Sun, 8 Oct 2017 19:23:43 +0000 (00:53 +0530)] 
src: Use snprintf() over strncpy()

Use snprintf() over strncpy() functions as the buffer is not null
terminated in strncpy().

Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
7 years agonetlink: remove non-batching routines
Pablo Neira Ayuso [Sat, 3 Mar 2018 09:18:33 +0000 (10:18 +0100)] 
netlink: remove non-batching routines

This is only needed by 3.16, which was released 8 months after nftables
was merged upstream. That kernel version supports a reduced featureset.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoconfigure: misc updates
Pablo Neira Ayuso [Sat, 3 Mar 2018 09:02:31 +0000 (10:02 +0100)] 
configure: misc updates

This patch removes the following macros:

* AC_PREREQ checks for 2.61, which is not supported any contemporary
  distribution.

* AC_COPYRIGHT, autoconf documentation states "in addition to the Free
  Software Foundation's copyright on the Autoconf macros, parts of your
  configure are covered by the copyright-notice.".

  This only refers to the autoconf infrastructure: we are doing simple
  and standard usage of autoconf infrastructure, we also don't use this
  macro in other existing userspace software available at netfilter.org.
  The comment above at the beginning of this file shows text that is
  available in many configure.ac templates on the Internet.

* AC_CANONICAL_HOST, we don't need the canonical host-system type to
  build this software.

* AC_CONFIG_SRCDIR is not used in other userspace software in the tree.

* AC_DEFINE _GNU_SOURCE, define this where it's needed instead.

* AC_DEFINE _STDC_FORMAT_MACROS is not used in this codebase.

* AC_HEADER_STDC checks for ANSI C89 headers, however, we need more than
  just this C standard, so this doesn't guarantee anything at all.

* Remove "Checks for libraries" comment, it's obvious.

* AC_HEADER_ASSERT allows us to disable assertions, this is bad because
  this is helping us to diagnose bugs and incomplete features.

* AC_CHECK_HEADERS is checking for an arbitrary list of headers,
  this still doesn't even guarantee that we can actually do a successful
  compilation in a broken system.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoparser: use nf_key_proto
Florian Westphal [Sat, 18 Nov 2017 20:00:58 +0000 (21:00 +0100)] 
parser: use nf_key_proto

Consolidate into one so it can be reused by new users.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agolibnftables: don't crash when no commands are specified
Harsha Sharma [Sat, 3 Mar 2018 08:03:58 +0000 (13:33 +0530)] 
libnftables: don't crash when no commands are specified

For e.g.  nft -c "  "
Without this patch it segfaults.

Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoparser: support of maps with timeout
Laura Garcia Liebana [Fri, 2 Mar 2018 09:50:18 +0000 (10:50 +0100)] 
parser: support of maps with timeout

Support of key and value association with a certain timeout.

Example:

nft add map nftlb mapa { type inet_service: ipv4_addr\;
 timeout 5s\; }

Results in:

table ip nftlb {
map mapa {
type inet_service : ipv4_addr
timeout 5s
}
}

Signed-off-by: Laura Garcia Liebana <nevola@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agonetlink_delinearize: Fix resource leaks
Phil Sutter [Thu, 1 Mar 2018 14:00:32 +0000 (15:00 +0100)] 
netlink_delinearize: Fix resource leaks

Most of the cases are basically the same: Error path fails to free the
previously allocated statement or expression. A few cases received
special treatment though:

- In netlink_parse_payload_stmt(), the leak is easily avoided by code
  reordering.

- In netlink_parse_exthdr(), there's no point in introducing a goto
  label since there is but a single affected error check.

- In netlink_parse_hash() non-error path leaked as well if sreg
  contained a concatenated expression.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agonetlink: Complain if setting O_NONBLOCK fails
Phil Sutter [Thu, 1 Mar 2018 14:00:31 +0000 (15:00 +0100)] 
netlink: Complain if setting O_NONBLOCK fails

Assuming that code is not aware that reads from netlink socket may
block, treat inability to set O_NONBLOCK flag as fatal initialization
error aborting program execution.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agohash: Fix potential null-pointer dereference in hash_expr_cmp()
Phil Sutter [Thu, 1 Mar 2018 14:00:30 +0000 (15:00 +0100)] 
hash: Fix potential null-pointer dereference in hash_expr_cmp()

The first part of the conditional:

| (e1->hash.expr || expr_cmp(e1->hash.expr, e2->hash.expr))

will call expr_cmp() in case e1->hash.expr is NULL, causing null-pointer
dereference. This is probably a typo, the intention when introducing
this was to avoid the call to expr_cmp() for symmetric hash expressions
which don't use expr->hash.expr. Inverting the existence check should
fix this.

Fixes: 3a86406729782 ("src: hash: support of symmetric hash")
Cc: Laura Garcia Liebana <nevola@gmail.com>
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoevaluate: Fix memleak in stmt_reject_gen_dependency()
Phil Sutter [Thu, 1 Mar 2018 14:00:29 +0000 (15:00 +0100)] 
evaluate: Fix memleak in stmt_reject_gen_dependency()

The allocated payload expression is not used after returning from that
function, so it needs to be freed again.

Simple test case:

| nft add rule inet t c reject with tcp reset

Valgrind reports definitely lost 144 bytes.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoerec: Avoid passing negative offset to fseek()
Phil Sutter [Thu, 1 Mar 2018 14:00:28 +0000 (15:00 +0100)] 
erec: Avoid passing negative offset to fseek()

If the initial call to ftell() fails, variable orig_offset is set to -1.
Avoid passing this to fseek() later on.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agocli: Drop pointless check in cli_append_multiline()
Phil Sutter [Thu, 1 Mar 2018 14:00:27 +0000 (15:00 +0100)] 
cli: Drop pointless check in cli_append_multiline()

The function is called from cli_complete after it has checked for line
to be != NULL. The other part of the conditional, namely multiline being
NULL, is perfectly valid (if the last read line didn't end with
backslash. Hence drop the conditional completely.

Since variable eof is not used anywhere outside of the dropped
conditional, get rid of it completely.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests: shell: regression test for bugzilla 1228
Pablo Neira Ayuso [Tue, 27 Feb 2018 16:00:41 +0000 (17:00 +0100)] 
tests: shell: regression test for bugzilla 1228

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agomonitor: Make JSON/XML output respect output_fp
Phil Sutter [Wed, 28 Feb 2018 15:04:28 +0000 (16:04 +0100)] 
monitor: Make JSON/XML output respect output_fp

Make sure events callbacks print to output_ctx-defined stream for any
type of output format.

Since all of them use nft_print() as last call (if anything is printed
at all), the final call to fflush() in netlink_events_cb() can be
dropped.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agomonitor: Make trace events respect output_fp
Phil Sutter [Wed, 28 Feb 2018 15:04:27 +0000 (16:04 +0100)] 
monitor: Make trace events respect output_fp

Seems like this was incompletely converted, part of the output went to
output_fp already.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agodoc: add example for rule add/delete
Florian Westphal [Wed, 28 Feb 2018 14:32:11 +0000 (15:32 +0100)] 
doc: add example for rule add/delete

also mention that 'ip' is used when the family gets omitted.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agodoc: remove ipv6 address FIXME
Florian Westphal [Wed, 28 Feb 2018 08:34:07 +0000 (09:34 +0100)] 
doc: remove ipv6 address FIXME

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoReview switch statements for unmarked fall through cases
Phil Sutter [Wed, 28 Feb 2018 15:06:16 +0000 (16:06 +0100)] 
Review switch statements for unmarked fall through cases

While revisiting all of them, clear a few oddities as well:

- There's no point in marking empty fall through cases: They are easy to
  spot and a common concept when using switch().

- Fix indenting of break statement in one occasion.

- Drop needless braces around one case which doesn't declare variables.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agodoc: mention meta l4proto and ipv6 nexthdr issue wrt. extension headers
Florian Westphal [Tue, 27 Feb 2018 11:48:16 +0000 (12:48 +0100)] 
doc: mention meta l4proto and ipv6 nexthdr issue wrt. extension headers

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoAdded undefine/redefine keywords
David Fabian [Mon, 22 Jan 2018 13:02:11 +0000 (14:02 +0100)] 
Added undefine/redefine keywords

This is a small patch to nft which adds two new keywords - undefine and
redefine. undefine simply undefines a variable from the current scope.
redefine allows one to change a variable definition. We have a firewall
written in bash (using iptables) that is organized by customer VLANs.
Each VLAN has its own set of bash variables holding things like uplink
iface names, gateway IPs, etc. We want to rewrite the firewall to
nftables but are stuck on the fact that nft variables cannot be
overridden in the same scope. We have each VLAN configuration in a
separate file containing pre/post-routing, input, output and forward
rules,and we include those files to a master firewall configuration. One
solution is to rename all the variables with some VLAN specific
(pre/su)ffix. But that is cumbersome.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests: add raw payload test cases.
Florian Westphal [Sat, 24 Feb 2018 09:45:32 +0000 (10:45 +0100)] 
tests: add raw payload test cases.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agodoc: document raw protocol expression
Florian Westphal [Sat, 24 Feb 2018 13:43:16 +0000 (14:43 +0100)] 
doc: document raw protocol expression

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agosrc: make raw payloads work
Florian Westphal [Sun, 25 Feb 2018 18:46:04 +0000 (19:46 +0100)] 
src: make raw payloads work

make syntax consistent between print and parse.
No dependency handling -- once you use raw expression, you need
to make sure the raw expression only sees the packets that you'd
want it to see.

based on an earlier patch from Laurent Fasnacht <l@libres.ch>.
Laurents patch added a different syntax:
   @<protocol>,<base>,<data type>,<offset>,<length>

data_type is useful to make nftables not err when
asking for "@payload,32,32 192.168.0.1", this patch still requires
manual convsersion to an integer type (hex or decimal notation).

data_type should probably be added later by adding an explicit
cast expression, independent of the raw payload syntax.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agopayload: don't resolve expressions using the inet pseudoheader
Florian Westphal [Sat, 24 Feb 2018 10:10:27 +0000 (11:10 +0100)] 
payload: don't resolve expressions using the inet pseudoheader

Else, '@ll,0,8' will be mapped to 'inet nfproto', but thats
not correct (inet is a pseudo header).

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agopayload: use integer_type when initializing a raw expression
Florian Westphal [Sat, 24 Feb 2018 11:51:45 +0000 (12:51 +0100)] 
payload: use integer_type when initializing a raw expression

The invalid type prints prominent "[invalid]", so prefer integer type
in raw expressions.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agometa: introduce datatype ifname_type
Arturo Borrero Gonzalez [Sun, 25 Feb 2018 17:30:24 +0000 (18:30 +0100)] 
meta: introduce datatype ifname_type

This new datatype is a string subtype.
It will allow us to build named maps/sets using meta keys like 'iifname',
'oifname', 'ibriport' or 'obriport'.

Example:

table inet t {
set s {
type ifname
elements = { "eth0",
     "eth1" }
}

chain c {
iifname @s accept
oifname @s accept
}
}

Signed-off-by: Arturo Borrero Gonzalez <arturo@netfilter.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agofiles: add load balance example
Arturo Borrero Gonzalez [Sat, 24 Feb 2018 21:06:49 +0000 (22:06 +0100)] 
files: add load balance example

Include this example file in the tarball on how to do load balancing with
nftables, inspired from https://wiki.nftables.org

Signed-off-by: Arturo Borrero Gonzalez <arturo@netfilter.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoexamples: add ct helper examples
Arturo Borrero Gonzalez [Sun, 25 Feb 2018 17:36:16 +0000 (18:36 +0100)] 
examples: add ct helper examples

Include some examples in the nftables tarball on using the ct helper
infraestructure, inspired from wiki.nftables.org.

Signed-off-by: Arturo Borrero Gonzalez <arturo@netfilter.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agonftables: rearrange files and examples
Arturo Borrero Gonzalez [Sat, 24 Feb 2018 21:06:19 +0000 (22:06 +0100)] 
nftables: rearrange files and examples

Concatenate all family/hook examples into a single one by means of includes.

Put all example files under examples/. Use the '.nft' prefix and mark
them as executable files. Use a static shebang declaration, since these
are examples meant for final systems and users.

While at it, refresh also the sets_and_maps.nft example file and also
add the 'netdev-ingress.nft' example file.

Signed-off-by: Arturo Borrero Gonzalez <arturo@netfilter.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosegtree: check for overlapping elements at insertion
Pablo Neira Ayuso [Fri, 23 Feb 2018 09:36:27 +0000 (10:36 +0100)] 
segtree: check for overlapping elements at insertion

This speeds up element overlap checks quite a bit.

Fixes: https://bugzilla.netfilter.org/show_bug.cgi?id=1228
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests: meta.t: fix test case for anonymous set automerge
Florian Westphal [Sat, 24 Feb 2018 11:28:30 +0000 (12:28 +0100)] 
tests: meta.t: fix test case for anonymous set automerge

commit fb16c8b7f795e0d
("evaluate: Enable automerge feature for anonymous sets") re-enabled
merging of adjacent ranges, so 33-55, 56-88 turns into 33-88.

Update test case to reflect this.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agodoc/nft.xml: fix typo
Duncan Roe [Sun, 18 Feb 2018 22:54:11 +0000 (09:54 +1100)] 
doc/nft.xml: fix typo

Signed-off-by: Duncan Roe <duncan_roe@optusnet.com.au>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoinclude: fix build failure
Florian Westphal [Sat, 17 Feb 2018 22:21:14 +0000 (23:21 +0100)] 
include: fix build failure

on older machine of mine:
../include/nftables.h:130:30: error: 'UINT_MAX' undeclared (first use in this function)

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agosrc: bail out when exporting ruleset with unsupported output
Pablo Neira Ayuso [Thu, 15 Feb 2018 16:22:16 +0000 (17:22 +0100)] 
src: bail out when exporting ruleset with unsupported output

Display error message and propagate error to shell when running command
with unsupported output:

 # nft export ruleset json
 Error: this output type is not supported
 export ruleset json
 ^^^^^^^^^^^^^^^^^^^^
 # echo $?
 1

When displaying the output in json using the low-level VM
representation, it shows:

 # nft export ruleset vm json
 ... low-level VM json output
 # echo $?
 0

While at it, do the same with obsoleted XML output.

Fixes: https://bugzilla.netfilter.org/show_bug.cgi?id=1224
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agopayload: don't decode past last valid template
Florian Westphal [Thu, 15 Feb 2018 14:26:31 +0000 (15:26 +0100)] 
payload: don't decode past last valid template

When trying to decode payload header fields, be sure to bail out
when having exhausted all available templates.

Otherwise, we allocate invalid payload expressions (no dataype,
header length of 0) and then crash when trying to print them.

Fixes: https://bugzilla.netfilter.org/show_bug.cgi?id=1226
Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: Spelling fixes
Ville Skyttä [Wed, 7 Feb 2018 09:34:31 +0000 (11:34 +0200)] 
src: Spelling fixes

Signed-off-by: Ville Skyttä <ville.skytta@iki.fi>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agonetlink_delinearize: add meta_may_dependency_kill()
Pablo Neira Ayuso [Wed, 14 Feb 2018 15:27:10 +0000 (16:27 +0100)] 
netlink_delinearize: add meta_may_dependency_kill()

Do not exercise dependency removal for protocol key network payload
expressions in bridge, netdev and inet families from meta expressions,
more specifically:

* inet: nfproto and ether type.
* netdev and bridge: meta protocol and ether type.

need to be left in place.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agopayload: add payload_may_dependency_kill()
Pablo Neira Ayuso [Wed, 14 Feb 2018 15:27:01 +0000 (16:27 +0100)] 
payload: add payload_may_dependency_kill()

Payload protocol key expressions at network base are meaningful in the
netdev, bridge and inet families, do not exercise the redundant
dependency removal in those cases since it breaks rule semantics.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: get rid of __payload_dependency_kill()
Pablo Neira Ayuso [Wed, 14 Feb 2018 15:26:57 +0000 (16:26 +0100)] 
src: get rid of __payload_dependency_kill()

Use payload_dependency_release() instead.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>