]> git.ipfire.org Git - thirdparty/libnftnl.git/log
thirdparty/libnftnl.git
12 years agoset_elem: fix access after free in case of parsing errors
Pablo Neira Ayuso [Mon, 21 Oct 2013 12:42:33 +0000 (14:42 +0200)] 
set_elem: fix access after free in case of parsing errors

If this fails to parse a set element, stop the processing.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agosrc: json: remove spaces
Álvaro Neira Ayuso [Sun, 13 Oct 2013 19:52:34 +0000 (21:52 +0200)] 
src: json: remove spaces

Remove all the spaces from the JSON output to reduce the size
of the output string, this also provides a consistent output
in table, chain, rule and set.

As Stephen Hemminger suggested, better to squash the output to
consume as less bytes as possible.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agoruleset: fix compilation warning
Pablo Neira Ayuso [Wed, 2 Oct 2013 08:27:29 +0000 (10:27 +0200)] 
ruleset: fix compilation warning

Due to missing calloc definition in stdlib.h, spotted by gcc-4.8.

Reported-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agosrc: add low-level ruleset API
Arturo Borrero [Mon, 30 Sep 2013 22:33:18 +0000 (00:33 +0200)] 
src: add low-level ruleset API

This patch adds a low level ruleset API for libnftables.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agoset: json: fix returned string length
Pablo Neira Ayuso [Fri, 27 Sep 2013 14:03:37 +0000 (16:03 +0200)] 
set: json: fix returned string length

Similar to (e7c068d rule: json: fix returned string length).

Based on patch from Arturo.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agosrc: snprintf: fix buffer lengths
Arturo Borrero [Wed, 25 Sep 2013 22:13:08 +0000 (00:13 +0200)] 
src: snprintf: fix buffer lengths

Use 'len' instead of 'size' since we need the remaining unused bytes
in the buffer, not its total size.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agoset_elem: xml: avoid code duplication
Arturo Borrero [Wed, 25 Sep 2013 22:13:02 +0000 (00:13 +0200)] 
set_elem: xml: avoid code duplication

This patch removes 3x code duplication in the XML snprintf function.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agorule: json: fix returned string length
Alvaro Neira Ayuso [Wed, 25 Sep 2013 20:06:34 +0000 (22:06 +0200)] 
rule: json: fix returned string length

It was returning one byte more than the real string length.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agoexamples: nft-set-get: retrieve all sets per family
Pablo Neira Ayuso [Tue, 24 Sep 2013 16:29:33 +0000 (18:29 +0200)] 
examples: nft-set-get: retrieve all sets per family

Likewise other nftables objects, this patch allows you to dump
the sets per family.

This is possible since kernel changes (netfilter: nf_tables: allow
to dump all existing sets), we can get the full list of sets per
family.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agorule: json: fix wrong indentation in nft_rule_snprintf_json
Pablo Neira Ayuso [Tue, 24 Sep 2013 14:56:59 +0000 (16:56 +0200)] 
rule: json: fix wrong indentation in nft_rule_snprintf_json

Comestic cleanup.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agorule: get rid of NFTA_RULE_FLAGS
Pablo Neira Ayuso [Tue, 24 Sep 2013 10:30:39 +0000 (12:30 +0200)] 
rule: get rid of NFTA_RULE_FLAGS

It has been removed after (netfilter: nf_tables: all rule
updates are transactional).

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agosrc: replace size_t by uint32_t in set/get interfaces
Pablo Neira Ayuso [Thu, 19 Sep 2013 18:11:27 +0000 (20:11 +0200)] 
src: replace size_t by uint32_t in set/get interfaces

This patch breaks the ABI to shrink the size parameter from 8
to 4 bytes in x86_64. The maximum length of netlink attributes
is 2 bytes, so 4 bytes as attribute payload length should be
enough.

After this patch, size_t is only used in the nft_*_snprintf
interfaces.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agorule: don't put NFTA_RULE_EXPRESSION into messages
Pablo Neira Ayuso [Tue, 17 Sep 2013 16:24:30 +0000 (18:24 +0200)] 
rule: don't put NFTA_RULE_EXPRESSION into messages

If there are no expressions in this rule, skip the
NFTA_RULE_EXPRESSION attribute, otherwise it will be
empty.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agosrc: add nft_*_list_del
Pablo Neira Ayuso [Tue, 17 Sep 2013 14:16:22 +0000 (16:16 +0200)] 
src: add nft_*_list_del

This allows us to delete object from the list, note that
nft_chain_list_del already existed.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agobuild: fix build error
Arturo Borrero Gonzalez [Mon, 16 Sep 2013 18:20:34 +0000 (20:20 +0200)] 
build: fix build error

This declaration was missing and is needed in case you compile libnftables
without XML parsing support.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agosrc: xml: refactor XML parsing code
Arturo Borrero Gonzalez [Mon, 16 Sep 2013 18:24:51 +0000 (20:24 +0200)] 
src: xml: refactor XML parsing code

This patch refactors nft_*_xml_parse to provide a new intermediate function
nft_mxml_parse_* which will allow us to navigate an entire XML tree
containing a ruleset without xml2text2xml conversions.

While at it, I added a helper to build the XML tree and validate the top node
name.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
12 years agosrc: xml: avoid unecessary tree to text to tree conversions
Arturo Borrero Gonzalez [Fri, 13 Sep 2013 12:45:21 +0000 (14:45 +0200)] 
src: xml: avoid unecessary tree to text to tree conversions

This patch avoid several tree to text to tree conversions in the XML
parsing helpers.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agosrc: xml: add parsing optional/mandatory flag
Arturo Borrero Gonzalez [Fri, 13 Sep 2013 12:05:51 +0000 (14:05 +0200)] 
src: xml: add parsing optional/mandatory flag

Add an optional/mandatory flag to XML parsing.

In some elements (ie regs), no flag is used because is always mandatory.

DATA_NONE is created to indicate a non-parsed data_reg.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agoset: fix compilation warning
Álvaro Neira Ayuso [Thu, 12 Sep 2013 17:16:19 +0000 (19:16 +0200)] 
set: fix compilation warning

Signed-off-by: Álvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agoexpr: log: use real length when fetching attributes
Florian Westphal [Fri, 13 Sep 2013 14:44:47 +0000 (16:44 +0200)] 
expr: log: use real length when fetching attributes

NFTA_LOG_SNAPLEN is u32 and NFTA_LOG_QTHRESHOLD is u16.
Without this, netlink messages from kernel fail mnl_validate step when
QTHRESH or SNAPLEN was set.

Also, nft_rule_expr_log_get must update data_length, else 'nft list' doesn't
show log arguments (prefix, group ..) because the netlink message
decoding uses nft_rule_expr_get_u16/32 etc. which validate the length, too.

Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agosrc: json: refactor json parsing to allow tree based navigation
Álvaro Neira Ayuso [Thu, 12 Sep 2013 17:16:37 +0000 (19:16 +0200)] 
src: json: refactor json parsing to allow tree based navigation

This patch refactors nft_*_json_parse to provide a new
intermediate function nft_jansson_parse_chain which will
allows us to navigate the entire json tree containing the
ruleset.

Signed-off-by: Álvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agorule: use helper function nft_jansson_free_root
Álvaro Neira Ayuso [Thu, 12 Sep 2013 17:16:28 +0000 (19:16 +0200)] 
rule: use helper function nft_jansson_free_root

Signed-off-by: Álvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agochain: use human readable netfilter hook
Pablo Neira Ayuso [Thu, 5 Sep 2013 19:54:56 +0000 (21:54 +0200)] 
chain: use human readable netfilter hook

Since (108d9f6 src: Wrap netfilter hooks around human readable strings)
in nft, we have to use human readable netfilter hooks.

This patch also adapts the XML and JSON tests.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agoexamples: nft-{chain,rule,table}-get allows unspec family
Pablo Neira Ayuso [Thu, 5 Sep 2013 19:10:50 +0000 (21:10 +0200)] 
examples: nft-{chain,rule,table}-get allows unspec family

To obtain any table, chain and rule.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agobuild: display configuration after ./configure
Pablo Neira Ayuso [Thu, 5 Sep 2013 17:03:23 +0000 (19:03 +0200)] 
build: display configuration after ./configure

libnftables configuration:
  XML support:                          no
  JSON support:                         no

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agoset: xml: fix key_type parsing
Arturo Borrero [Wed, 4 Sep 2013 16:03:25 +0000 (18:03 +0200)] 
set: xml: fix key_type parsing

Bug introduced by myself at 44928cd5 [set: add XML parsing]

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agochain: xml: fix parsing of optional attributes
Arturo Borrero [Sun, 1 Sep 2013 23:32:31 +0000 (01:32 +0200)] 
chain: xml: fix parsing of optional attributes

This patch fixes the parsing of custom chains in XML.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agoexamples: Add nft-set-json-add
Álvaro Neira Ayuso [Tue, 27 Aug 2013 18:11:02 +0000 (20:11 +0200)] 
examples: Add nft-set-json-add

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agotests: set: add json parsing support
Álvaro Neira Ayuso [Tue, 27 Aug 2013 18:10:55 +0000 (20:10 +0200)] 
tests: set: add json parsing support

Test the functions for parsing set in JSON.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agoset: Add json parser support
Álvaro Neira Ayuso [Tue, 27 Aug 2013 18:10:47 +0000 (20:10 +0200)] 
set: Add json parser support

Add function for parsing set in format JSON.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agotests: remove unnecessary variable initialization
Álvaro Neira Ayuso [Tue, 27 Aug 2013 18:10:37 +0000 (20:10 +0200)] 
tests: remove unnecessary variable initialization

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agosrc: json: s/nft_jansson_value_parse_reg/nft_jansson_parse_reg/g
Pablo Neira Ayuso [Sat, 24 Aug 2013 16:27:58 +0000 (18:27 +0200)] 
src: json: s/nft_jansson_value_parse_reg/nft_jansson_parse_reg/g

This patch is a cleanup to save LOC.

I have also changed it to use < 0 to check for error conditions.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agosrc: json: s/nft_jansson_value_parse_val/nft_jansson_parse_val/g
Pablo Neira Ayuso [Sat, 24 Aug 2013 16:08:17 +0000 (18:08 +0200)] 
src: json: s/nft_jansson_value_parse_val/nft_jansson_parse_val/g

This patch is a cleanup to save LOC.

I have also changed it to use < 0 to check for error conditions.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agosrc: json: s/nft_jansson_value_parse_str/nft_jansson_parse_str/g
Pablo Neira Ayuso [Sat, 24 Aug 2013 15:57:16 +0000 (17:57 +0200)] 
src: json: s/nft_jansson_value_parse_str/nft_jansson_parse_str/g

Remove _value_ infix to make the function name smaller.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agorule: xml/json: support rule position attribute
Arturo Borrero [Fri, 23 Aug 2013 11:35:35 +0000 (13:35 +0200)] 
rule: xml/json: support rule position attribute

Note that the position attr is optional as stated in
net/netfilter/nf_tables_api.c

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agosrc: constify nft_*_parse input data
Arturo Borrero Gonzalez [Wed, 21 Aug 2013 19:21:29 +0000 (21:21 +0200)] 
src: constify nft_*_parse input data

Since the input string is not changing, let's make it constant.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agoexamples: Add nft-rule-json-add
Álvaro Neira Ayuso [Tue, 20 Aug 2013 15:59:03 +0000 (17:59 +0200)] 
examples: Add nft-rule-json-add

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
12 years agotests: rule: add json parsing support
Álvaro Neira Ayuso [Tue, 20 Aug 2013 15:58:56 +0000 (17:58 +0200)] 
tests: rule: add json parsing support

Test the functions for parsing rules in JSON.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
12 years agorule: Add json parser support
Álvaro Neira Ayuso [Tue, 20 Aug 2013 15:58:46 +0000 (17:58 +0200)] 
rule: Add json parser support

Add function for parsing rules in JSON format

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agoutils: fix error path for nft_strtoi
Arturo Borrero [Mon, 19 Aug 2013 12:37:30 +0000 (14:37 +0200)] 
utils: fix error path for nft_strtoi

If the return of nft_get_value() is not evaluated, we don't know if
the parsed value is between the limits of its type.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agotests: fix memory leaks in jansson test
Álvaro Neira Ayuso [Wed, 14 Aug 2013 10:19:31 +0000 (12:19 +0200)] 
tests: fix memory leaks in jansson test

Fixed a string leak and a jansson leak.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agojson: fixed some leaks in the json parsing function
Álvaro Neira Ayuso [Wed, 14 Aug 2013 10:19:19 +0000 (12:19 +0200)] 
json: fixed some leaks in the json parsing function

This patch fixes some leaks in the json parsing function. After this patch,
we use nft_jansson_free_root. This function uses json_decref and it
decrements the reference count and it releases the node if needed.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agoexpr: add nft_rule_expr_get_u16 and nft_rule_expr_set_u16
Pablo Neira Ayuso [Sat, 17 Aug 2013 10:40:32 +0000 (12:40 +0200)] 
expr: add nft_rule_expr_get_u16 and nft_rule_expr_set_u16

Now required since log has two u16 fields.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agoexpr: log: group and qthreshold are 16 bits long
Pablo Neira Ayuso [Wed, 14 Aug 2013 22:08:58 +0000 (00:08 +0200)] 
expr: log: group and qthreshold are 16 bits long

Since kernel patch: netfilter: nft_log: group and qthreshold are 2^16

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agorule: xml: consolidate error path
Pablo Neira Ayuso [Wed, 14 Aug 2013 09:28:47 +0000 (11:28 +0200)] 
rule: xml: consolidate error path

Save a good bunch of lines of code with this.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agochain: xml: use nft_mxml_str_parse
Pablo Neira Ayuso [Wed, 14 Aug 2013 09:25:19 +0000 (11:25 +0200)] 
chain: xml: use nft_mxml_str_parse

Replace existing code to use nft_mxml_str_parse.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agosrc: xml: don't duplicate string in nft_table_xml_parse
Pablo Neira Ayuso [Wed, 14 Aug 2013 09:10:58 +0000 (11:10 +0200)] 
src: xml: don't duplicate string in nft_table_xml_parse

With this patch, nft_table_xml_parse does not duplicate the string
anymore, which is what most callers seem to need. This fixes memleaks
in several places in the code. Thus, this patch also adapts the code
to duplicate it when needed.

Based on patch from Arturo Borrero.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agorule: release expressions that are part of rules
Pablo Neira Ayuso [Tue, 13 Aug 2013 23:07:13 +0000 (01:07 +0200)] 
rule: release expressions that are part of rules

==11917== 96 bytes in 1 blocks are definitely lost in loss record 1 of 1
==11917==    at 0x4C272B8: calloc (vg_replace_malloc.c:566)
==11917==    by 0x4E3A46F: nft_rule_expr_alloc (expr.c:38)
==11917==    by 0x4E3A331: nft_jansson_expr_parse (jansson.c:165)
==11917==    by 0x4E36F66: nft_rule_json_parse (rule.c:542)
==11917==    by 0x400EA4: main (nft-rule-json-add.c:68)

Reported-by: Álvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agonat: xml: fix wrong node name in snprintf
Arturo Borrero [Mon, 12 Aug 2013 07:35:39 +0000 (09:35 +0200)] 
nat: xml: fix wrong node name in snprintf

This patch renames all <nat_type> nodes to <type> in nat expr.
A bug is fixed, since the default option in snprintf was already
<type>.

This follows the pattern of avoid prefixing XML nodes.

Note that this is mostly reverting what was done at:
31e34c3 (nat: xml: rename node type to nat_type).

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agochain: xml: delete <use> node
Arturo Borrero [Mon, 12 Aug 2013 07:35:34 +0000 (09:35 +0200)] 
chain: xml: delete <use> node

Since the 'use' attribute in a chain can't be set, ignore it in the
XML printing.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agotests: xml: reorder XML elements in sets
Arturo Borrero [Mon, 12 Aug 2013 07:35:29 +0000 (09:35 +0200)] 
tests: xml: reorder XML elements in sets

Some node reorganization in order to have an input file equal to
what set_snprintf will output.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agoxml: expr: limit: fix wrong assignation when parsing
Arturo Borrero [Mon, 12 Aug 2013 07:35:23 +0000 (09:35 +0200)] 
xml: expr: limit: fix wrong assignation when parsing

This assignation was wrong.

Introduced at commit e13819c
(src: xml: consolidate common XML code via nft_mxml_num_parse).

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agotests: remove superfluous if checking in test_json
Pablo Neira Ayuso [Sun, 11 Aug 2013 07:50:05 +0000 (09:50 +0200)] 
tests: remove superfluous if checking in test_json

It's already done a couple of lines above.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agotests: better parsing validation
Álvaro Neira Ayuso [Sat, 10 Aug 2013 19:40:51 +0000 (21:40 +0200)] 
tests: better parsing validation

Before this patch, the test only verified that we could parse
the file. After this, it also compares what it has parsed with
the original file.

Basically, the idea is that:

file -> buffer -> object -> buffer'
   read               snprintf

So we also compare that buffer = buffer' which should be true
since our tests should be based on the output that the library
generates.

This is an example output for the OK and FAILED cases:

parsing and validating xmlfiles/25-rule-exthdr.xml: OK
validating xmlfiles/27-rule-limit.xml: FAILED
from file:     it"><rate>123123</ra
from snprintf: it"><rate>321321</ra

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agosrc: expr: use the function base2str in payload
Álvaro Neira Ayuso [Sat, 10 Aug 2013 19:40:39 +0000 (21:40 +0200)] 
src: expr: use the function base2str in payload

Use base2str instead to consolidate code in the snprintf path.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agotests: xml: remove \t and \n from all the files
Pablo Neira Ayuso [Sat, 10 Aug 2013 10:52:12 +0000 (12:52 +0200)] 
tests: xml: remove \t and \n from all the files

These XML files are intented to be used for automated tests. Thus, it's
important to have exactly the same output that was generated by the library.

The beautified output does not match the real output that the library provides
and it just makes things harder for us to provide a more advanced automated
testing.

If you want to make any of those file human readable, please use:

    xmllint --format file.xml

or open the xml file and any browser, eg. firefox.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agoexpr: payload: fix missing base setting in XML parser
Pablo Neira Ayuso [Fri, 9 Aug 2013 17:05:22 +0000 (19:05 +0200)] 
expr: payload: fix missing base setting in XML parser

Reported-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agojansson: Add nft_jansson_family function
Álvaro Neira Ayuso [Fri, 9 Aug 2013 11:14:46 +0000 (13:14 +0200)] 
jansson: Add nft_jansson_family function

Refactor some existing code with the new function nft_jansson_family.

Signed-off-by: Alvaro Neira Ayuso Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agoexample: nft-rule-get: family parameter added
Álvaro Neira Ayuso [Fri, 9 Aug 2013 11:14:31 +0000 (13:14 +0200)] 
example: nft-rule-get: family parameter added

I have added the parameter family in the example nft-rule-get.

Signed-off-by: Alvaro Neira Ayuso Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agoexpr: payload: add nft_str2base function
Álvaro Neira Ayuso [Fri, 9 Aug 2013 11:14:16 +0000 (13:14 +0200)] 
expr: payload: add nft_str2base function

Add function that will be use in the JSON parser

Signed-off-by: Alvaro Neira Ayuso Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agoexpr: nat: add nft_str2nat function
Álvaro Neira Ayuso [Fri, 9 Aug 2013 11:13:59 +0000 (13:13 +0200)] 
expr: nat: add nft_str2nat function

Add function that will be use in the JSON parser.

Signed-off-by: Alvaro Neira Ayuso Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agoexpr: cmp: add nft_str2cmp function
Álvaro Neira Ayuso [Fri, 9 Aug 2013 11:13:45 +0000 (13:13 +0200)] 
expr: cmp: add nft_str2cmp function

Add function that will be use in the JSON parser

Signed-off-by: Alvaro Neira Ayuso Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agoexpr: bytecode: add nft_str2ntoh function
Álvaro Neira Ayuso [Fri, 9 Aug 2013 11:13:31 +0000 (13:13 +0200)] 
expr: bytecode: add nft_str2ntoh function

Add function that will be use in the JSON parser.

Signed-off-by: Alvaro Neira Ayuso Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agosrc: json: delete unneeded JSON prefixes
Álvaro Neira Ayuso [Fri, 9 Aug 2013 11:13:14 +0000 (13:13 +0200)] 
src: json: delete unneeded JSON prefixes

This patch adapts JSON nodes to mimic current XML node tags.

Signed-off-by: Alvaro Neira Ayuso Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agojson: bitwise: add missing node len
Álvaro Neira Ayuso [Fri, 9 Aug 2013 11:13:00 +0000 (13:13 +0200)] 
json: bitwise: add missing node len

I have added the len node in bitwise which was missing

Signed-off-by: Alvaro Neira Ayuso Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agoexpr: ct: optional output in ct
Álvaro Neira Ayuso [Fri, 9 Aug 2013 11:12:46 +0000 (13:12 +0200)] 
expr: ct: optional output in ct

Display direction and key if available

Signed-off-by: Alvaro Neira Ayuso Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agosrc: fix display of compat_flag and compat_proto
Álvaro Neira Ayuso [Fri, 9 Aug 2013 11:12:32 +0000 (13:12 +0200)] 
src: fix display of compat_flag and compat_proto

Fixed display compat_proto value and compat_flag if available.

Signed-off-by: Alvaro Neira Ayuso Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agosrc: expr: fix wrong value in the chain field of data_reg
Álvaro Neira Ayuso [Fri, 9 Aug 2013 11:12:19 +0000 (13:12 +0200)] 
src: expr: fix wrong value in the chain field of data_reg

Fixed wrong value in data_reg_chain snprintf.

Signed-off-by: Alvaro Neira Ayuso Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agosrc: expr: missing curly braces in json output support
Álvaro Neira Ayuso [Fri, 9 Aug 2013 11:12:05 +0000 (13:12 +0200)] 
src: expr: missing curly braces in json output support

Added missing curly braces in json output support.

Signed-off-by: Alvaro Neira Ayuso Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agosrc: expr: missing commas in json output support
Álvaro Neira Ayuso [Fri, 9 Aug 2013 11:11:48 +0000 (13:11 +0200)] 
src: expr: missing commas in json output support

Added missing commas in json output support.

Signed-off-by: Alvaro Neira Ayuso Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agosrc: xml: cleanup set element parsing
Pablo Neira Ayuso [Wed, 7 Aug 2013 20:31:52 +0000 (22:31 +0200)] 
src: xml: cleanup set element parsing

Move nft_mxml_set_elem_parse to set_elem.c to improve readability, thus,
we don't need to jump from set_elem.c to mxml.c to see how the parsing
is done.

I have also refactored some common parsing code in the new helper
function nft_mxml_set_elem_parse, that avoids conversions from XML tree
to text and then again back to tree.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agosrc: xml: delete unneeded XML prefixes
Arturo Borrero Gonzalez [Tue, 6 Aug 2013 10:52:45 +0000 (12:52 +0200)] 
src: xml: delete unneeded XML prefixes

This patch changes some XML nodes with prefixes, as the example below
ilustrates.

Before:
<rule>
<rule_flags/>
<rule_family/>
<rule>

After:
<rule>
<flags>
<family>
</rule>

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agoset: xml: use nft_mxml_family_parse
Arturo Borrero Gonzalez [Tue, 6 Aug 2013 11:55:33 +0000 (13:55 +0200)] 
set: xml: use nft_mxml_family_parse

Use nft_mxml_family_parse() to parse the family.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agosrc: remove useless casting
Pablo Neira Ayuso [Tue, 6 Aug 2013 11:47:13 +0000 (13:47 +0200)] 
src: remove useless casting

Not needed anymore after constification.

Based on patch from Arturo Borrero.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agorule: constify char * fields
Pablo Neira Ayuso [Tue, 6 Aug 2013 11:44:07 +0000 (13:44 +0200)] 
rule: constify char * fields

Should have been done in (ec75831 src: fully constify nft_*_get
functions).

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agoset_elem: constify nft_set_elem_attr_get_str
Pablo Neira Ayuso [Tue, 6 Aug 2013 11:34:27 +0000 (13:34 +0200)] 
set_elem: constify nft_set_elem_attr_get_str

Should have been done in (ec75831 src: fully constify
nft_*_get functions)

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agosrc: xml: consolidate error path in table and chain objects
Pablo Neira Ayuso [Tue, 6 Aug 2013 09:16:02 +0000 (11:16 +0200)] 
src: xml: consolidate error path in table and chain objects

Remove a good bunch of LOC with this cleanup.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agosrc: xml: use nodes instead of attributes
Arturo Borrero Gonzalez [Tue, 6 Aug 2013 08:40:33 +0000 (10:40 +0200)] 
src: xml: use nodes instead of attributes

When working with XML, it's desirable to work with nodes better than
attributes.

Table/chain/rules had attributes in their XML representation, and
this patch transform those to nodes, ie:

Before:
<table name="filter">
<family>ip</family>
<table_flags>0</table_flags>
</table>

After:
<table>
<name>filter</name>
<family>ip</family>
<table_flags>0</table_flags>
</table>

While at it:
 * There was a lot of redundant code that is now collapsed with the
new nft_mxml_family_parse() helper function.

 * I've added a small fix: additional validation for the name of
the current XML object, and also replace raw strtol calls to nft_strtoi.

 * Also, all XML testfiles are updated to keep passing the parsing tests and
mantain the repo in consisten state.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agosrc: fully constify nft_*_get functions
Pablo Neira Ayuso [Tue, 6 Aug 2013 09:36:54 +0000 (11:36 +0200)] 
src: fully constify nft_*_get functions

We have several char * field that were not constify to avoid
gcc compilation warnings when calling free. Since (99d2574 src:
add xfree and use it), we can fully constify these objects
fields without trouble.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agosrc: add xfree and use it
Pablo Neira Ayuso [Tue, 6 Aug 2013 09:33:45 +0000 (11:33 +0200)] 
src: add xfree and use it

This patch adds xfree, a replacement of free that accepts
const pointers. This helps to remove ugly castings that you usually
need to calm down gcc.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agochain: Use nft_str2hooknum() in the XML parsing code.
Arturo Borrero Gonzalez [Mon, 5 Aug 2013 12:01:39 +0000 (14:01 +0200)] 
chain: Use nft_str2hooknum() in the XML parsing code.

Note: I've used MXML_DESCEND_FIRST flag when calling nft_mxml_str_parse()
to ensure that the parsing travels from the top of the chain XML tree.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
12 years agochain: fix compilation warning due to unused nft_str2hooknum()
Arturo Borrero Gonzalez [Mon, 5 Aug 2013 12:01:39 +0000 (14:01 +0200)] 
chain: fix compilation warning due to unused nft_str2hooknum()

If neither XML_PARSING nor JSON_PARSING are defined (libnftables configured
without XML/JSON parsing support), a warning is produced due to unused
nft_str2hooknum() function.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
12 years agosrc: xml: table/chain delete properties
Arturo Borrero Gonzalez [Mon, 5 Aug 2013 12:01:28 +0000 (14:01 +0200)] 
src: xml: table/chain delete properties

This patch deletes the <properties> node in chain and table XML objects.

For this to work, the first tree search with MXML_DESCEND_FIRST flag is moved
to the next node.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agochain: fix nft_str2hooknum return value
Arturo Borrero Gonzalez [Mon, 5 Aug 2013 12:01:44 +0000 (14:01 +0200)] 
chain: fix nft_str2hooknum return value

nft_str2hooknum() should return -1 if no hooknum was found.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agoexamples: Add nft-chain-json-add
Álvaro Neira Ayuso [Wed, 31 Jul 2013 13:21:34 +0000 (15:21 +0200)] 
examples: Add nft-chain-json-add

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agotests: chain: add json parsing support
Álvaro Neira Ayuso [Wed, 31 Jul 2013 13:21:27 +0000 (15:21 +0200)] 
tests: chain: add json parsing support

Test the functions for parsing chains in JSON.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agochain: Add json parser support
Álvaro Neira Ayuso [Wed, 31 Jul 2013 13:21:20 +0000 (15:21 +0200)] 
chain: Add json parser support

Add function for parsing chains in format JSON.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agochain: remove the properties node in Json output
Álvaro Neira Ayuso [Wed, 31 Jul 2013 13:21:12 +0000 (15:21 +0200)] 
chain: remove the properties node in Json output

I have removed the properties node from chain because it's a node without relevant information

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agotable: remove the properties node in Json output
Álvaro Neira Ayuso [Wed, 31 Jul 2013 13:21:06 +0000 (15:21 +0200)] 
table: remove the properties node in Json output

I have removed the properties node from table because
it provides no relevant information.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agojansson: Add helper function for building the tree and use it
Álvaro Neira Ayuso [Wed, 31 Jul 2013 13:20:59 +0000 (15:20 +0200)] 
jansson: Add helper function for building the tree and use it

Add a helper function that parses and returns the jansson
tree, use it in the table parser.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agochain: remove duplicated policy2str
Álvaro Neira Ayuso [Wed, 31 Jul 2013 13:20:51 +0000 (15:20 +0200)] 
chain: remove duplicated policy2str

Use nft_verdict2str function instead.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agosrc: remove version from XML and JSON representations
Pablo Neira Ayuso [Fri, 26 Jul 2013 13:56:37 +0000 (15:56 +0200)] 
src: remove version from XML and JSON representations

This patch removes the version XML node and the version JSON field
in all our existing objects. The current versioning approach
consists of adding a version field to every object representation
in XML and JSON. While listing my entire rule-set, one can notice
that this approach is too bloated.

Once the library enters stable stage, if we need to obsolete a XML
node and a JSON field, we can follow this procedure:

1) Remove the XML node and the JSON field from the output, so fresh
   outputs will not contain the old ones anymore.

2) Do not remove the parsing of the old XML node and the JSON field
   inmediately. We have to keep supporting the parsing for a while
   to avoid breaking the interpretion of old XML/JSON files. We can
   spot a warning to warn about it, so users generate a fresh
   output again.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agoset: add XML parsing
Arturo Borrero [Fri, 26 Jul 2013 12:22:03 +0000 (14:22 +0200)] 
set: add XML parsing

Sets are now parsed, following this previous snprintf pattern:

<set>
<set_name>string</set_name>
<set_table>table</set_table>
<set_xml_version>int</set_xml_version>
<set_flags>uint32_t</set_flags>
<key_type>uint32_t</key_type>
<key_len>size_t</key_len>
<data_type>uint32_t</data_type>
<data_len>size_t</data_len>
<set_elem>
<set_elem_flags>uint32_t</set_elem_flags>
<set_elem_key>
<data_reg type="value">
<len></len>
<dataN></dataN>
</data_reg>
</set_elem_key>
<set_elem_data>
<data_reg type="xx">
[...]
</data_reg>
</set_elem_data>
</set_elem>
</set>

Signed-off-by: Arturo Borrero González <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agoset: xml: change XML attributes to nodes.
Arturo Borrero [Fri, 26 Jul 2013 12:11:07 +0000 (14:11 +0200)] 
set: xml: change XML attributes to nodes.

This patch changes all XML attributes to XML nodes in nft_set.

In order to avoid issues regarding XML nodes with the same name but
different meanings, I've followed this pattern for the new elements:

<set>
<set_table>string</set_table>
<set_name>string</set_name>
[...]
</set>

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agotable: json: fix returned errno value while parsing
Pablo Neira Ayuso [Thu, 25 Jul 2013 21:22:26 +0000 (23:22 +0200)] 
table: json: fix returned errno value while parsing

Instead of returning ERANGE all the time, let functions set
errno accordingly and set EINVAL otherwise.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agoexamples: Add nft-table-json-add
Álvaro Neira Ayuso [Thu, 25 Jul 2013 20:52:32 +0000 (22:52 +0200)] 
examples: Add nft-table-json-add

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agotests: table: test json parsing support
Álvaro Neira Ayuso [Thu, 25 Jul 2013 20:52:24 +0000 (22:52 +0200)] 
tests: table: test json parsing support

Test the functions for parsing tables in JSON Support

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agotable: Add json parser support
Álvaro Neira Ayuso [Thu, 25 Jul 2013 20:52:15 +0000 (22:52 +0200)] 
table: Add json parser support

Add function for parsing tables in format JSON

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agotests: xml: add realistic XML tests files
Arturo Borrero [Thu, 25 Jul 2013 20:44:40 +0000 (22:44 +0200)] 
tests: xml: add realistic XML tests files

This patch refresh current XML testfiles with some realworld
expressions extracted from rules. The nft instruction itself is added
as a comment for future references.

All XMl files are now indented with tabs instead of spaces. Also, a
bunch of new realworld rules with mixed expressions are added.

I used this command to get the XML formatted with tabs:

$ export XMLLINT_INDENT=$'\t'
$ xmllint --format file.xml

The xmllint tool is included in the libxml2-utils package (at least on
debian systems).

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 years agoimmediate: xml: fix name inconsistency
Arturo Borrero [Thu, 25 Jul 2013 20:54:11 +0000 (22:54 +0200)] 
immediate: xml: fix name inconsistency

<immdata> should be <immediatedata> instead.

This bug was introduced at (1e8e5d4 src: xml: consolidate parsing of
data_reg via nft_mxml_data_reg_parse).

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>