]> git.ipfire.org Git - thirdparty/libnftnl.git/log
thirdparty/libnftnl.git
9 years agorule: Implement internal iterator for expressions
Carlos Falgueras García [Wed, 10 Aug 2016 09:48:54 +0000 (11:48 +0200)] 
rule: Implement internal iterator for expressions

Introduce nftnl_expr_iter_init() to allow stack allocated iterators for
internal use.

Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
9 years agosrc: Constify iterators
Carlos Falgueras García [Tue, 9 Aug 2016 11:42:17 +0000 (13:42 +0200)] 
src: Constify iterators

Iterators do not modify objects which they iterate, so input pointer must
be const.

Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: don't set data_len to zero when returning pointers
Pablo Neira Ayuso [Sun, 31 Jul 2016 20:05:52 +0000 (22:05 +0200)] 
src: don't set data_len to zero when returning pointers

nft already assumes that passing NULL as data_len is valid, otherwise
it crashes. Fix this by leave data_len unset in this specific case.

Fixes: bda7102 ("src: Fix nftnl_*_get_data() to return the real attribute length")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoexamples: nft-rule-get: selective rule dumping
Josue Alvarez [Thu, 21 Jul 2016 14:59:41 +0000 (16:59 +0200)] 
examples: nft-rule-get: selective rule dumping

Improve nft-rule-get example to demonstrate selective rule dumping when
table and / or chain attributes are set in a rule dump request.

Usage is now as follows:

nft-rule-get <family> [<table> <chain>] [<xml|json>]

Signed-off-by: Josue Alvarez <jalvarez@toulouse.viveris.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoexpr: lookup: print flags only if they are available
Pablo Neira Ayuso [Thu, 21 Jul 2016 17:14:13 +0000 (19:14 +0200)] 
expr: lookup: print flags only if they are available

Follow same approach as with other objects, print what it is set only.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: Fix nftnl_*_get_data() to return the real attribute length
Carlos Falgueras García [Mon, 11 Jul 2016 16:07:40 +0000 (18:07 +0200)] 
src: Fix nftnl_*_get_data() to return the real attribute length

All getters must set the memory size of the attributes, ie. this
includes the nul-termination in strings.

For references to opaque objects hidden behind the curtain, report
a zero size.

Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: Fix missing nul-termination in nftnl_*_set_str()
Carlos Falgueras García [Tue, 5 Jul 2016 17:15:16 +0000 (19:15 +0200)] 
src: Fix missing nul-termination in nftnl_*_set_str()

The string length must be one character longer to include the
nul-termination.

Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests: Check set user data
Carlos Falgueras García [Mon, 27 Jun 2016 17:05:23 +0000 (19:05 +0200)] 
tests: Check set user data

Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoset: Add new attribute into 'set' to store user data
Carlos Falgueras García [Mon, 27 Jun 2016 17:05:22 +0000 (19:05 +0200)] 
set: Add new attribute into 'set' to store user data

The new structure 'user' holds a pointer to user data and its length. The
kernel must have the flag NFTA_SET_USERDATA to support this feature.

Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoexpr: lookup: give support for inverted matching
Arturo Borrero [Fri, 24 Jun 2016 07:07:02 +0000 (09:07 +0200)] 
expr: lookup: give support for inverted matching

Inverted matching support was included in the kernel, let's give support here
as well.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoset_elem: fix return in several error paths of nftnl_set_elems_parse2()
Pablo Neira Ayuso [Wed, 22 Jun 2016 17:30:21 +0000 (19:30 +0200)] 
set_elem: fix return in several error paths of nftnl_set_elems_parse2()

They don't set ret to anything, and ret is not initialized, so we return
garbage.

Fixes: 59cb13b ("src: fix missing error checking in parser functions")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: fix missing error checking in parser functions
Carlos Falgueras García [Mon, 20 Jun 2016 10:29:19 +0000 (12:29 +0200)] 
src: fix missing error checking in parser functions

Bail out on errors in several nftnl_*_nlmsg_parse() functions. We can
overwrite the previous error value, and may execute code which should
not.

Bad way:
int f() {
int ret;

ret = g();
ret = h();

return ret;
}

Good way:
int f() {
int ret;

ret = g();
if (ret < 0)
return ret;

ret = h();
if (ret < 0)
return ret;

return 0;
}

Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agochain: Check correct attribute
Carlos Falgueras García [Thu, 16 Jun 2016 10:20:10 +0000 (12:20 +0200)] 
chain: Check correct attribute

Fix nftnl_chain_set_data() with NFTNL_CHAIN_DEV.

Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests: stricter string attribute validation
Pablo Neira Ayuso [Wed, 15 Jun 2016 11:41:06 +0000 (13:41 +0200)] 
tests: stricter string attribute validation

In nft-expr_lookup-test.c, check for the strings instead of size.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agochain: dynamically allocate name
Pablo Neira Ayuso [Fri, 10 Jun 2016 12:34:10 +0000 (14:34 +0200)] 
chain: dynamically allocate name

Just in case we ever support chain with larger names in the future,
this will ensure the library doesn't break. Although I don't expect
allocating more bytes for this anytime soon, but let's be conservative
here.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests: shuffle values that are injected
Pablo Neira Ayuso [Fri, 10 Jun 2016 14:56:46 +0000 (16:56 +0200)] 
tests: shuffle values that are injected

Shuffle value that are used to set attributes, this variability should
help us catch more problems in the future.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: check for flags before releasing attributes
Pablo Neira Ayuso [Fri, 10 Jun 2016 14:45:48 +0000 (16:45 +0200)] 
src: check for flags before releasing attributes

Now that unsetters don't set pointers to NULL, check if the attribute is
set before trying to release it.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: simplify unsetters
Pablo Neira Ayuso [Fri, 10 Jun 2016 13:07:13 +0000 (15:07 +0200)] 
src: simplify unsetters

If the attribute is set as we already check at the beginning of this
function, then we can release the object.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoexpr: data_reg: get rid of leftover perror() calls
Pablo Neira Ayuso [Fri, 10 Jun 2016 12:49:01 +0000 (14:49 +0200)] 
expr: data_reg: get rid of leftover perror() calls

Let the client of this library decide when to display error messages.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: check for strdup() errors from setters and parsers
Pablo Neira Ayuso [Fri, 10 Jun 2016 12:47:53 +0000 (14:47 +0200)] 
src: check for strdup() errors from setters and parsers

And pass up an error to the caller.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: return value on setters that internally allocate memory
Pablo Neira Ayuso [Fri, 10 Jun 2016 12:30:56 +0000 (14:30 +0200)] 
src: return value on setters that internally allocate memory

So the client can bail out of memory allocation errors. Or in case of
daemon, make sure things are left in consistent state before bailing
out.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: assert when setting unknown attributes
Pablo Neira Ayuso [Fri, 10 Jun 2016 12:13:00 +0000 (14:13 +0200)] 
src: assert when setting unknown attributes

If this attribute is not supported by the library, we should rise an
assertion so the client knows something is wrong, instead of silently
going through.

The only case I can think may hit this problem is version mismatch
between library and tools. This should not ever really happen, so better
bail out from the library itself in this case.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: Fix leak in nftnl_*_unset()
Carlos Falgueras García [Fri, 10 Jun 2016 12:22:45 +0000 (14:22 +0200)] 
src: Fix leak in nftnl_*_unset()

Fix leak of NFTNL_*_USERDATA from unset() functions.

Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoexpr: payload: don't use deprecated definition NFT_EXPR_PAYLOAD_SREG
Pablo Neira Ayuso [Fri, 10 Jun 2016 12:08:27 +0000 (14:08 +0200)] 
expr: payload: don't use deprecated definition NFT_EXPR_PAYLOAD_SREG

Use NFTNL_EXPR_PAYLOAD_SREG instead.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoexamples: nft-table-upd: don't use deprecated aliases
Pablo Neira Ayuso [Tue, 7 Jun 2016 14:47:38 +0000 (16:47 +0200)] 
examples: nft-table-upd: don't use deprecated aliases

Convert this example not to use the deprecated aliases anymore.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agobuild: update LIBVERSION to prepare a new release libnftnl-1.0.6
Pablo Neira Ayuso [Thu, 26 May 2016 14:35:03 +0000 (16:35 +0200)] 
build: update LIBVERSION to prepare a new release

Bump Current and Age accordingly, given that we got new interfaces.
This git repository shows these changes in the map file since previous
release:

$ git diff libnftnl-1.0.5..HEAD src/libnftnl.map
--- a/src/libnftnl.map
+++ b/src/libnftnl.map
@@ -498,3 +498,33 @@ global:

 local: *;
 };
+
+LIBNFTNL_4.1 {
+       nftnl_trace_alloc;
+       nftnl_trace_free;
+
+       nftnl_trace_is_set;
+
+       nftnl_trace_get_u16;
+       nftnl_trace_get_u32;
+       nftnl_trace_get_u64;
+       nftnl_trace_get_str;
+       nftnl_trace_get_data;
+
+       nftnl_trace_nlmsg_parse;
+
+       nftnl_udata_buf_alloc;
+       nftnl_udata_buf_free;
+       nftnl_udata_buf_len;
+       nftnl_udata_buf_data;
+       nftnl_udata_buf_put;
+       nftnl_udata_start;
+       nftnl_udata_end;
+       nftnl_udata_put;
+       nftnl_udata_put_strz;
+       nftnl_udata_type;
+       nftnl_udata_len;
+       nftnl_udata_get;
+       nftnl_udata_next;
+       nftnl_udata_parse;
+} LIBNFTNL_4;

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoset_elem: Copy user data memory
Carlos Falgueras García [Fri, 27 May 2016 14:56:54 +0000 (16:56 +0200)] 
set_elem: Copy user data memory

All attributes are passed by copy, so user data should be copied too.

Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoset_elem: Fix memory leak
Carlos Falgueras García [Tue, 24 May 2016 15:17:07 +0000 (17:17 +0200)] 
set_elem: Fix memory leak

User data must be freed.

How to reproduce:
    > nft add table t
    > nft add set t s {type ipv4_addr\;}
    > valgrind nft add element t s {1.1.1.1}

Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agorule: Fix segfault due to invalid free of rule user data
Carlos Falgueras García [Tue, 17 May 2016 16:00:15 +0000 (18:00 +0200)] 
rule: Fix segfault due to invalid free of rule user data

If the user allocates a nftnl_udata_buf and then passes the TLV data to
nftnl_rule_set_data, the pointer stored in rule.user.data is not the
begining of the allocated block. In this situation, if it calls to
nftnl_rule_free, it tries to free this pointer and segfault is thrown.

Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests: Free nftnl_udata_buf before exit
Carlos Falgueras García [Tue, 17 May 2016 16:00:14 +0000 (18:00 +0200)] 
tests: Free nftnl_udata_buf before exit

Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agolibnftnl: gitignore: Fix mistake in gitignore regexp
Carlos Falgueras García [Tue, 17 May 2016 19:11:39 +0000 (21:11 +0200)] 
libnftnl: gitignore: Fix mistake in gitignore regexp

If a whole directory was ignored, files inside it will not be checked.

Fixes: f3d37ef ("libnftnl: Add to .gitignore all auto-generated files")
Reported-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoinclude: refresh nf_tables.h cache copy
Pablo Neira Ayuso [Tue, 17 May 2016 16:41:33 +0000 (18:41 +0200)] 
include: refresh nf_tables.h cache copy

Refresh the cached header file.

This includes a small fix to avoid this compilation warning after
refreshing the header:

trace.c: In function 'nftnl_trace_parse_attr_cb':
trace.c:87:2: warning: enumeration value 'NFTA_TRACE_PAD' not handled in switch [-Wswitch]

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agolibnftnl: Add to .gitignore all auto-generated files
Carlos Falgueras García [Mon, 16 May 2016 12:39:01 +0000 (14:39 +0200)] 
libnftnl: Add to .gitignore all auto-generated files

It ignores files inside test/ and examples/ except all c code (*.c)
and the Makefile.am.

Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agochain: missing constification of _get() functions
Pablo Neira Ayuso [Fri, 13 May 2016 10:51:22 +0000 (12:51 +0200)] 
chain: missing constification of _get() functions

These functions don't modify the chain object.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: missing static in several array definitions
Pablo Neira Ayuso [Mon, 9 May 2016 21:05:53 +0000 (23:05 +0200)] 
src: missing static in several array definitions

They are not used out of the scope of the C file where they are defined,
so we can statify them.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: remove unnecessary inline in _snprintf functions
Pablo Neira Ayuso [Mon, 9 May 2016 21:03:25 +0000 (23:03 +0200)] 
src: remove unnecessary inline in _snprintf functions

These functions are passed as parameter, so we basically get nothing
with this.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agolibnftnl: constify object arguments to various functions
Patrick McHardy [Tue, 26 Apr 2016 13:16:58 +0000 (14:16 +0100)] 
libnftnl: constify object arguments to various functions

flow table support needs constant object arguments to printing functions
to avoid ugly casts. While at it, also constify object arguments to message
construction, destructor and a few helper functions.

Signed-off-by: Patrick McHardy <kaber@trash.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agolibnftnl: allow any set name length
Pablo Neira Ayuso [Thu, 5 May 2016 12:05:56 +0000 (14:05 +0200)] 
libnftnl: allow any set name length

Unfortunately libnftnl restricts the set names in the lookup and dynset
expressions to 16 bytes. Remove this restriction so this can work with
the upcoming 4.7 Linux kernel.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoexamples: load modules when adding chains or tables
Daniel Wagner [Fri, 29 Apr 2016 10:07:34 +0000 (12:07 +0200)] 
examples: load modules when adding chains or tables

Tell the kernel to load the necessary modules by adding
the NLM_F_CREATE flag.

Signed-off-by: Daniel Wagner <daniel.wagner@bmw-carit.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoexpr: ct: fix typo unknow vs unknown
Arturo Borrero [Tue, 19 Apr 2016 12:54:46 +0000 (14:54 +0200)] 
expr: ct: fix typo unknow vs unknown

Reported by Debian's lintian tool.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
9 years agorule: fix leaks in NFTNL_RULE_USERDATA
Pablo Neira Ayuso [Fri, 15 Apr 2016 10:29:42 +0000 (12:29 +0200)] 
rule: fix leaks in NFTNL_RULE_USERDATA

Fix leaks in nftnl_rule_free() and nftnl_rule_set_data().

Reported-by: Carlos Falgueras García <carlosfg@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests: expr-nat: Use different values to test
Shivani Bhardwaj [Fri, 15 Apr 2016 08:12:57 +0000 (13:42 +0530)] 
tests: expr-nat: Use different values to test

Tests are more effective if different values are set so, use different
values for every expression.

Signed-off-by: Shivani Bhardwaj <shivanib134@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests: nft-rule-test: check for NFTNL_RULE_USERDATA
Carlos Falgueras García [Tue, 22 Mar 2016 19:46:26 +0000 (20:46 +0100)] 
tests: nft-rule-test: check for NFTNL_RULE_USERDATA

Modify nft-rule-test.c to check TLV attribute inclusion in nftnl_rule.

Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoudata: add TLV user data infrastructure
Carlos Falgueras García [Tue, 22 Mar 2016 19:46:24 +0000 (20:46 +0100)] 
udata: add TLV user data infrastructure

These functions allow to create a buffer (struct nftnl_udata_buf) of
user data attributes in TLV format (struct nftnl_udata). It is inspired
by libmnl/src/attr.c. It can be used to store several TLVs sequentially
into an object.

Example:

struct nftnl_udata_buf *buf;
struct nftnl_udata *attr;
const char *str = "Hello World!";

buf = nftnl_udata_buf_alloc(UDATA_SIZE);
if (!buf) {
perror("OOM");
exit(EXIT_FAILURE);
}

if (!nftnl_udata_put_strz(buf, MY_TYPE, str)) {
perror("Can't put attribute \"%s\"", str);
exit(EXIT_FAILURE);
}

nftnl_udata_for_each(buf, attr)
printf("%s\n", (char *)nftnl_udata_attr_value(attr));

nftnl_udata_buf_free(buf);

Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotrace: fix missing NFTNL_TRACE_JUMP_TARGET in nftnl_trace_get_str()
Patrick McHardy [Thu, 10 Mar 2016 12:16:05 +0000 (13:16 +0100)] 
trace: fix missing NFTNL_TRACE_JUMP_TARGET in nftnl_trace_get_str()

Signed-off-by: Patrick McHardy <kaber@trash.net>
9 years agotrace: fix multiple copy and paste errors
Patrick McHardy [Fri, 4 Mar 2016 11:26:30 +0000 (12:26 +0100)] 
trace: fix multiple copy and paste errors

Fix duplicated and incorrect assignments.

Signed-off-by: Patrick McHardy <kaber@trash.net>
9 years agoexpr: masq: Add support for port selection
Shivani Bhardwaj [Fri, 22 Jan 2016 19:36:06 +0000 (01:06 +0530)] 
expr: masq: Add support for port selection

Complete masquerading support by allowing port range selection.

Signed-off-by: Shivani Bhardwaj <shivanib134@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: meta: add prandom support
Florian Westphal [Mon, 1 Feb 2016 23:19:44 +0000 (00:19 +0100)] 
src: meta: add prandom support

Signed-off-by: Florian Westphal <fw@strlen.de>
9 years agoexpr: add forward expression
Pablo Neira Ayuso [Mon, 11 Jan 2016 18:43:01 +0000 (19:43 +0100)] 
expr: add forward expression

Add forward expression for the netdev family.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: ct: add packet and byte counter support
Florian Westphal [Thu, 14 Jan 2016 03:53:31 +0000 (04:53 +0100)] 
src: ct: add packet and byte counter support

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoexpr: limit: add support for flags
Pablo Neira Ayuso [Mon, 11 Jan 2016 18:38:24 +0000 (19:38 +0100)] 
expr: limit: add support for flags

This patch adds the limit flags, the first client of this is the
inversion flag that allows us to match overlimit.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: add trace infrastructure support
Florian Westphal [Wed, 11 Nov 2015 00:54:01 +0000 (01:54 +0100)] 
src: add trace infrastructure support

parses trace monitor netlink messages from the kernel and builds
nftnl_trace struct that contains the dissected information.

Provides getters to access these attributes.

Signed-off-by: Florian Westphal <fw@strlen.de>
10 years agopayload: add payload mangling support
Patrick McHardy [Wed, 25 Nov 2015 16:48:34 +0000 (16:48 +0000)] 
payload: add payload mangling support

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agosrc: rename EXPORT_SYMBOL to EXPORT_SYMBOL_ALIAS
Florian Westphal [Tue, 24 Nov 2015 10:02:09 +0000 (11:02 +0100)] 
src: rename EXPORT_SYMBOL to EXPORT_SYMBOL_ALIAS

Future symbols don't need backwards-compat aliases.

Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoAdd support to print netdev family
Vijay Subramanian [Sun, 25 Oct 2015 02:50:33 +0000 (19:50 -0700)] 
Add support to print netdev family

When we lookup the family, return "netdev" for NFPROTO_NETDEV instead of
"unknown".

Signed-off-by: Vijay Subramanian <subramanian.vijay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agochain: fix segfault in 'device' XML parsing
Arturo Borrero [Tue, 13 Oct 2015 07:39:10 +0000 (09:39 +0200)] 
chain: fix segfault in 'device' XML parsing

Reported by valgrind:
[...]
==14065== Process terminating with default action of signal 11 (SIGSEGV)
==14065==  Access not within mapped region at address 0x0
==14065==    at 0x4C2C022: strlen (vg_replace_strmem.c:454)
==14065==    by 0x4E41A93: nftnl_chain_set_str (chain.c:259)
==14065==    by 0x4E427F7: nftnl_mxml_chain_parse (chain.c:770)
==14065==    by 0x4E48F96: nftnl_ruleset_parse_chains (ruleset.c:314)
==14065==    by 0x4E4959A: nftnl_ruleset_xml_parse_ruleset (ruleset.c:625)
==14065==    by 0x4E4959A: nftnl_ruleset_xml_parse_cmd (ruleset.c:668)
==14065==    by 0x4E4959A: nftnl_ruleset_xml_parse (ruleset.c:706)
==14065==    by 0x4E4959A: nftnl_ruleset_do_parse (ruleset.c:734)
==14065==    by 0x4013C9: test_xml (nft-parsing-test.c:166)
==14065==    by 0x4016F4: execute_test (nft-parsing-test.c:214)
==14065==    by 0x400EBA: main (nft-parsing-test.c:330)
[...]

While at it, fix a bit the coding style.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoexamples: Fix nft-table-upd example
Vijay Subramanian [Fri, 9 Oct 2015 21:24:18 +0000 (14:24 -0700)] 
examples: Fix nft-table-upd example

examples/nft-table-upd does not work currently since NFT_MSG_NEWTABLE
needs to use batching mode of netlink message delivery.

This patch adds batching to nft-table-upd example.

While here, also add support for netdev family.

Signed-off-by: Vijay Subramanian <subramanian.vijay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoexpr: Fix compilation with JSON and XML parsing enabled
Vijay Subramanian [Fri, 9 Oct 2015 19:21:07 +0000 (12:21 -0700)] 
expr: Fix compilation with JSON and XML parsing enabled

Fix missing/incorrect variables.
Also remove unsed variables to avoid warnings.

Signed-off-by: Vijay Subramanian <subramanian.vijay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoexpr: dup: fix missing space in text output
Pablo Neira Ayuso [Tue, 29 Sep 2015 18:07:03 +0000 (20:07 +0200)] 
expr: dup: fix missing space in text output

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoexpr: add dup expression support
Pablo Neira Ayuso [Wed, 3 Jun 2015 16:50:50 +0000 (18:50 +0200)] 
expr: add dup expression support

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoexpr: limit: add per-byte limiting support
Pablo Neira Ayuso [Sun, 2 Aug 2015 16:51:43 +0000 (18:51 +0200)] 
expr: limit: add per-byte limiting support

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoexpr: limit: add burst attribute
Pablo Neira Ayuso [Sun, 2 Aug 2015 16:44:13 +0000 (18:44 +0200)] 
expr: limit: add burst attribute

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agobump version to 1.0.5 libnftnl-1.0.5
Pablo Neira Ayuso [Wed, 16 Sep 2015 18:46:06 +0000 (20:46 +0200)] 
bump version to 1.0.5

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agobuild: bump library versioning
Jan Engelhardt [Wed, 16 Sep 2015 17:12:47 +0000 (19:12 +0200)] 
build: bump library versioning

Commit libnftnl-1.0.3-31-g5ea54b2 removed a symbol. Such requires a
bumped to n+1:0:0. The symbol groups can be merged again to save time
processing them as the groups are relative to a particular SONAME
(of which we have a new one).

Signed-off-by: Jan Engelhardt <jengelh@inai.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agobump version to 1.0.4 libnftnl-1.0.4
Pablo Neira Ayuso [Wed, 16 Sep 2015 10:29:50 +0000 (12:29 +0200)] 
bump version to 1.0.4

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: get rid of _attr_ infix in new nftnl_ definitions
Pablo Neira Ayuso [Mon, 7 Sep 2015 16:57:50 +0000 (18:57 +0200)] 
src: get rid of _attr_ infix in new nftnl_ definitions

The function names are already large, trim off the _ATTR_ infix in the
attribute definitions.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: get rid of _ATTR_ infix in new nfntl_ definitions
Pablo Neira Ayuso [Mon, 7 Sep 2015 16:02:50 +0000 (18:02 +0200)] 
src: get rid of _ATTR_ infix in new nfntl_ definitions

The constant names are already large, trim off the _ATTR_ infix in the
attribute definitions.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: rename NFTNL_RULE_EXPR_ATTR to NFTNL_EXPR_
Pablo Neira Ayuso [Mon, 7 Sep 2015 15:59:49 +0000 (17:59 +0200)] 
src: rename NFTNL_RULE_EXPR_ATTR to NFTNL_EXPR_

So we get a shorter constant definition for expression attributes.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: rename nftnl_rule_expr to nftnl_expr
Pablo Neira Ayuso [Mon, 7 Sep 2015 15:56:47 +0000 (17:56 +0200)] 
src: rename nftnl_rule_expr to nftnl_expr

Use a shorter name for this, morever this can be used from sets so the _rule_
is misleading.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: add compat header file definitions
Pablo Neira Ayuso [Tue, 1 Sep 2015 17:34:18 +0000 (19:34 +0200)] 
src: add compat header file definitions

This patch restores the original nft_* definitions from the header files to
avoid sudden compilation breakage of the existing clients of this library.

Then, moving forward the idea is to deprecate the old nft_* symbols anytime
soon using __attribute__((deprecated)) from the header files to warn our users
that they need to update their code.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: rename existing functions to use the nftnl_ prefix
Pablo Neira Ayuso [Tue, 1 Sep 2015 18:19:56 +0000 (20:19 +0200)] 
src: rename existing functions to use the nftnl_ prefix

So we can use the nft_* prefix anytime soon for our upcoming higher level
library.

After this patch, the nft_* symbols become an alias of the nftnl_* symbols.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: introduce nftnl_* aliases for all existing functions
Pablo Neira Ayuso [Tue, 1 Sep 2015 11:59:54 +0000 (13:59 +0200)] 
src: introduce nftnl_* aliases for all existing functions

This patch introduces the nftnl_ symbols as aliases for the existing nft_
symbols through the EXPORT_SYMBOL(...) macro.

We would like to use the nft_* prefix from our upcoming higher level library,
meanwhile with this move we avoid that old binaries break because of missing
symbol dependencies.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoexpr: immediate: fix leak in expression destroy path
Pablo Neira Ayuso [Mon, 17 Aug 2015 02:41:32 +0000 (04:41 +0200)] 
expr: immediate: fix leak in expression destroy path

The verdict can be a chain string, make sure we release it when the expression
is destroyed. This patch adds a new nft_free_data() for this purpose and use it
from the immediate expression.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: fix memory leaks at nft_[object]_nlmsg_parse
Carlos Falgueras García [Wed, 5 Aug 2015 10:23:34 +0000 (12:23 +0200)] 
src: fix memory leaks at nft_[object]_nlmsg_parse

Free object attributes before overwrite it. Fix 'nlmsg_parse' methods of
following objects: 'table', 'chain', 'rule', 'set' and 'set_element'.

Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoexpr: redir: fix snprintf to return the number of bytes printed
Balazs Scheidler [Fri, 26 Jun 2015 09:57:53 +0000 (11:57 +0200)] 
expr: redir: fix snprintf to return the number of bytes printed

This fixes --debug netlink output when a redir target is included.

Signed-off-by: Balazs Scheidler <balazs.scheidler@balabit.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agochain: add netdev family support
Pablo Neira Ayuso [Thu, 11 Jun 2015 18:43:54 +0000 (20:43 +0200)] 
chain: add netdev family support

Add support for the new NFT_CHAIN_ATTR_DEV attribute that indicates that the
basechain is attached to a net_device.

This partially reworks 1dd9ba1ea23c ("table: add netdev family support").

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotable: add netdev family support
Pablo Neira Ayuso [Mon, 25 May 2015 10:51:54 +0000 (12:51 +0200)] 
table: add netdev family support

This adds support for the new 'netdev' family tables.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: add batch abstraction
Pablo Neira Ayuso [Tue, 24 Feb 2015 23:53:51 +0000 (00:53 +0100)] 
src: add batch abstraction

This patch adds a new batch class to libnftnl, it basically generalizes what we
already have.

A batch is composed of one or more page objects. Every page may contain one or
more netlink messages.

 batch
   *      .------.   .------.         .------.
   |      |      |   |      |         |      |
   `----> | page |-->| page |-->...-->| page |
          |      |   |      |         |      |
          `------'   `------'         `------'

You can create a batch via:

batch = nft_batch_alloc(...);

This batch initially comes with one initial page.

You can fetch a pointer to the next spare area in the current page to add a new
netlink message to the batch.

void *nft_batch_buffer(struct nft_batch *batch);

Once you have added a netlink message, you have to call:

nft_batch_update(batch);

this internally updates the pointer to the next spare data area in the page.

Every page has a limit threshold after which you start using the overrun area.

  page  .------.
        |      |
        |      |
        .      . page area
        |      |
        |      |
        |------|<--- limit
        |      |
        |      | overrun area
        |      |
        '______'<--- real page size

If we write over the limit, then the next call to nft_batch_update() results in
a new empty page added to the batch. With careful page size and limit
selection, we ensure that a netlink message always fit into the page, so we
avoid the overhead of canceling the netlink message that doesn't fit in.

Once your batch is complete, if you want to send it out to kernel-space, you
can convert them to iovec via:

nft_batch_iovec(batch, iov, iov_len);

Then, after having sent the batch, you can release it via:

nft_batch_free(batch);

This class relies on the libmnl batching infrastructure.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agodynset: support expression templates
Patrick McHardy [Sun, 12 Apr 2015 19:17:52 +0000 (20:17 +0100)] 
dynset: support expression templates

Support expression templates for the dynset expression for dynamic
expression instantiation.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoset_elem: support expressions attached to set elements
Patrick McHardy [Sun, 12 Apr 2015 19:17:52 +0000 (20:17 +0100)] 
set_elem: support expressions attached to set elements

This patch supports attaching a struct nft_rule_expr to a set element
and adds netlink attribute encoding and decoding.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoexpr: seperate expression parsing and building functions
Patrick McHardy [Sun, 12 Apr 2015 19:17:52 +0000 (20:17 +0100)] 
expr: seperate expression parsing and building functions

The expression build function currently assumes to be only used from
rule context and actually builds rule attributes. Fix that and only
build the expression. Also it seems to have been exported by accident,
undo that.

Additionally, move the expression parsing function from rule parsing
and also remove any assumptions about being used in rule context.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agodata: increase maximum possible data size
Patrick McHardy [Sun, 1 Mar 2015 10:28:02 +0000 (10:28 +0000)] 
data: increase maximum possible data size

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoheaders: resync headers for new register definitions
Patrick McHardy [Thu, 26 Mar 2015 12:47:56 +0000 (12:47 +0000)] 
headers: resync headers for new register definitions

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoexpr: dynset: fix json/xml parsing
Arturo Borrero Gonzalez [Mon, 13 Apr 2015 19:23:45 +0000 (20:23 +0100)] 
expr: dynset: fix json/xml parsing

expr/dynset.c: In function 'nft_rule_expr_dynset_json_parse':
expr/dynset.c:194:3: warning: implicit declaration of function 'nft_rule_expr_dynset_str' [-Wimplicit-function-declaration]
   nft_rule_expr_dynset_str(e, NFT_EXPR_DYNSET_SET, set_name);
   ^
expr/dynset.c:194:31: error: 'NFT_EXPR_DYNSET_SET' undeclared (first use in this function)
   nft_rule_expr_dynset_str(e, NFT_EXPR_DYNSET_SET, set_name);
                               ^
expr/dynset.c:194:31: note: each undeclared identifier is reported only once for each function it appears in
expr/dynset.c:197:3: warning: implicit declaration of function 'nft_rule_expr_dynset_u32' [-Wimplicit-function-declaration]
   nft_rule_expr_dynset_u32(e, NFT_EXPR_DYNSET_SREG, sreg);
   ^
expr/dynset.c:197:31: error: 'NFT_EXPR_DYNSET_SREG' undeclared (first use in this function)
   nft_rule_expr_dynset_u32(e, NFT_EXPR_DYNSET_SREG, sreg);
                               ^
expr/dynset.c:200:31: error: 'NFT_EXPR_DYNSET_DREG' undeclared (first use in this function)
   nft_rule_expr_dynset_u32(e, NFT_EXPR_DYNSET_DREG, dreg);
                               ^
expr/dynset.c: In function 'nft_rule_expr_dynset_xml_parse':
expr/dynset.c:220:31: error: 'NFT_EXPR_DYNSET_SET' undeclared (first use in this function)
   nft_rule_expr_dynset_str(e, NFT_EXPR_DYNSET_SET, set_name);
                               ^
expr/dynset.c:224:31: error: 'NFT_EXPR_DYNSET_SREG' undeclared (first use in this function)
   nft_rule_expr_dynset_u32(e, NFT_EXPR_DYNSET_SREG, sreg);
                               ^
expr/dynset.c:228:31: error: 'NFT_EXPR_DYNSET_DREG' undeclared (first use in this function)
   nft_rule_expr_dynset_u32(e, NFT_EXPR_DYNSET_DREG, dreg);
                               ^
Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoexpr: add support for the dynset expr
Patrick McHardy [Sun, 12 Apr 2015 19:17:51 +0000 (20:17 +0100)] 
expr: add support for the dynset expr

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoset_elem: add support for userdata
Patrick McHardy [Sun, 12 Apr 2015 19:17:51 +0000 (20:17 +0100)] 
set_elem: add support for userdata

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoset: print set elem timeout information
Patrick McHardy [Sun, 12 Apr 2015 19:17:51 +0000 (20:17 +0100)] 
set: print set elem timeout information

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoset_elem: add timeout support
Patrick McHardy [Thu, 26 Mar 2015 13:10:20 +0000 (13:10 +0000)] 
set_elem: add timeout support

Signed-off-by: Patrick McHardy <kaber@trash.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoset: add support for set timeouts
Patrick McHardy [Thu, 26 Mar 2015 13:10:19 +0000 (13:10 +0000)] 
set: add support for set timeouts

Signed-off-by: Patrick McHardy <kaber@trash.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agolist: fix prefetch dummy
Patrick McHardy [Thu, 26 Mar 2015 12:48:36 +0000 (12:48 +0000)] 
list: fix prefetch dummy

../include/linux_list.h:385:59: warning: right-hand operand of comma expression has no effect [-Wunused-value]
  for (pos = list_entry((head)->next, typeof(*pos), member), \
                                                           ^
set.c:266:2: note: in expansion of macro 'list_for_each_entry'
  list_for_each_entry(elem, &set->element_list, head) {

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agosrc: restore static array with expression operations
Pablo Neira Ayuso [Sun, 22 Mar 2015 19:59:42 +0000 (20:59 +0100)] 
src: restore static array with expression operations

We cannot use __attribute__((constructor)) to register the supported
expressions in runtime when the library is statically linked. This lead
us to some explicit libnftnl_init() function that needs to be called
from the main() function of the client program.

This patch reverts 4dd0772 ("expr: use __attribute__((constructor)) to
register expression").

Reported-by: Laurent Bercot <ska-devel@skarnet.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoparser: Add operation not supported error message
Alvaro Neira [Mon, 16 Mar 2015 15:06:09 +0000 (16:06 +0100)] 
parser: Add operation not supported error message

If we try to import a ruleset in json or xml and the library was not
compile with support for those, this shows a misleading error.

To resolve this problem, this patch sets up EOPNOTSUPP by default when
we create the nft_parse_err structure.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoruleset: add nft_ruleset_ctx_free
Alvaro Neira [Thu, 12 Mar 2015 16:33:09 +0000 (17:33 +0100)] 
ruleset: add nft_ruleset_ctx_free

This function releases the ruleset objects attached in the parse context
structure, ie. struct nft_parse_ctx.

Moreover, this patch updates the nft_parse_ruleset_file to use it.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoexamples: add nft-ruleset-parse-file
Alvaro Neira [Mon, 2 Mar 2015 18:59:39 +0000 (19:59 +0100)] 
examples: add nft-ruleset-parse-file

With this example, we can parse the objects in the ruleset and create the
netlink message with the action associated. For example:

- Flush ruleset
- Add, delete or flush tables/chains
- Add, delete sets
- Add, delete set elements
- Add, delete, replace or prepend rules

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoxml: test if the root node name is initialized
Alvaro Neira [Mon, 2 Mar 2015 19:58:38 +0000 (20:58 +0100)] 
xml: test if the root node name is initialized

If the root node name is missing, we have a crash.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoruleset: crash from error path when we build the xml/json tree
Alvaro Neira [Mon, 2 Mar 2015 18:59:38 +0000 (19:59 +0100)] 
ruleset: crash from error path when we build the xml/json tree

Fix crash when we try to release a tree that is not initialized.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoruleset: fix crash if we free sets included in the set_list
Alvaro Neira [Tue, 24 Feb 2015 08:10:32 +0000 (09:10 +0100)] 
ruleset: fix crash if we free sets included in the set_list

When we parse a ruleset which has a rule using a set. First step is to
parse the set, set up an ID and add it to a set list. Later, we use this
set list to find the set associated to the rule and we set up the set ID
to the expression (lookup expression) of the rule.

The problem is that if we return this set to the callback function
nft_ruleset_parse_file_cb() and we free this set, we have a crash when
we try to iterate in the set list.

This patch solves it, cloning the set and adding the new set to the set
list.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoMakefile: internal.h now resides in include
Pablo Neira Ayuso [Thu, 19 Feb 2015 00:24:44 +0000 (01:24 +0100)] 
Makefile: internal.h now resides in include

Remove reference to the former internal.h under src/

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: split internal.h is smaller files
Pablo Neira Ayuso [Fri, 13 Feb 2015 17:01:02 +0000 (18:01 +0100)] 
src: split internal.h is smaller files

The internal.h file started being a small file with private definitions.
Its size has been increasing over time more and more, so let's split
this in small header files that map to the corresponding class where the
functions belong to.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoruleset: fix more leaks in error path
Pablo Neira Ayuso [Fri, 13 Feb 2015 15:48:35 +0000 (16:48 +0100)] 
ruleset: fix more leaks in error path

Shouldn't happen though, since you need to introduce an unsupported
markup language.

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