]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
10 years agosrc: interpret the event type from the evaluation step
Pablo Neira Ayuso [Mon, 6 Oct 2014 16:03:27 +0000 (18:03 +0200)] 
src: interpret the event type from the evaluation step

Postpone the event type interpretation to the evaluation step.
This patch also fixes the combination of event and object types,
which was broken. The export code needed to be adjusted too.

The new and destroy are not tokens that can be recognized by
the scanner anymore, so this also implicitly restores 'ct state'.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: add nat persistent and random options
Arturo Borrero [Fri, 3 Oct 2014 12:46:41 +0000 (14:46 +0200)] 
src: add nat persistent and random options

This patch adds more configuration options to the nat expression.

The syntax is as follow:
 % nft add rule nat postrouting <snat|dnat> <nat_arguments> [flags]

Flags are: random, persistent, random-fully.
Example:

 % nft add rule nat postrouting dnat 1.1.1.1 random,persistent

A requirement is to cache some [recent] copies of kernel headers.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agonft: complete reject support
Alvaro Neira [Tue, 30 Sep 2014 15:21:40 +0000 (17:21 +0200)] 
nft: complete reject support

This patch allows to use the reject action in rules. For example:

  nft add rule filter input udp dport 22 reject

In this rule, we assume that the reason is network unreachable. Also
we can specify the reason with the option "with" and the reason. For example:

  nft add rule filter input tcp dport 22 reject with icmp type host-unreachable

In the bridge tables and inet tables, we can use this action too. For example:

  nft add rule inet filter input reject with icmp type host-unreachable

In this rule above, this generates a meta nfproto dependency to match
ipv4 traffic because we use a icmpv4 reason to reject.

If the reason is not specified, we infer it from the context.

Moreover, we have the new icmpx datatype. You can use this datatype for
the bridge and the inet tables to simplify your ruleset. For example:

  nft add rule inet filter input reject with icmpx type host-unreachable

We have four icmpx reason and the mapping is:

ICMPX reason            |       ICMPv6          |      ICMPv4
                        |                       |
admin-prohibited        | admin-prohibited      | admin-prohibited
port-unreachable        | port-unreachable      | port-unreachable
no-route                | no-route              | net-unreachable
host-unreachable        | addr-unreachable      | host-unreachable

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agodatatype: Enhance symbolic_constant_parse()
Alvaro Neira [Tue, 30 Sep 2014 15:21:39 +0000 (17:21 +0200)] 
datatype: Enhance symbolic_constant_parse()

With this patch, this function finds the symbol inside the table. If the symbol
doesn't exist we use the basetype to parse it and create the constant
expression. Otherwise, return an error message.

This a refactorization to reuse this code in a follow up patch.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: Enhance payload_gen_dependency()
Alvaro Neira [Tue, 30 Sep 2014 15:21:38 +0000 (17:21 +0200)] 
src: Enhance payload_gen_dependency()

With this patch, this function returns a statement with the new dependency
that we want to add, instead of an expression.

This change is needed in a follow up patch.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agopayload: generate dependency in the appropriate byteorder
Alvaro Neira [Tue, 30 Sep 2014 15:21:37 +0000 (17:21 +0200)] 
payload: generate dependency in the appropriate byteorder

If we add a dependency, the constant expression on the right
hand side must be represented in the appropriate order.

Example without this patch:

  nft add rule bridge filter input reject with icmp-host-unreach --debug netlink

  [ payload load 2b @ link header + 12 => reg 1 ]
  [ cmp eq reg 1 0x00000800 ]
  [ reject type 0 code 1 ]

When we create the payload expression we have the right value in host endian but
this has to be in big endian.

With this patch, if we add the same rule:

  nft add rule bridge filter input reject with icmp-host-unreach --debug netlink

  [ payload load 2b @ link header + 12 => reg 1 ]
  [ cmp eq reg 1 0x00000008 ]
  [ reject type 0 code 1 ]

The new dependency is converted to big endian.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agomnl: use nft_batch_begin and nft_batch_end from libnftnl
Pablo Neira Ayuso [Tue, 30 Sep 2014 15:42:36 +0000 (17:42 +0200)] 
mnl: use nft_batch_begin and nft_batch_end from libnftnl

Use the existing functions in libnftnl to begin and end a batch.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: add list ruleset command
Arturo Borrero [Fri, 26 Sep 2014 16:30:08 +0000 (18:30 +0200)] 
src: add list ruleset command

This patch adds a new command to nft:
% nft list ruleset [family]

Which list the entire ruleset.
If no family is specified, all tables of all families are listed.

Users can now make several operations at ruleset level:

 % nft list ruleset > ruleset.nft
 % nft -f ruleset.nft
 % nft flush ruleset

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agorule: factorize chain and table listing code
Arturo Borrero [Wed, 24 Sep 2014 10:32:19 +0000 (12:32 +0200)] 
rule: factorize chain and table listing code

Let's factorize common code. This is also useful in follow-up patches.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agorule: rename do_command_list_cleanup() to table_cleanup()
Arturo Borrero [Wed, 24 Sep 2014 10:32:18 +0000 (12:32 +0200)] 
rule: rename do_command_list_cleanup() to table_cleanup()

Let's use a more generic name for this functions, since it has nothing to do
with commands.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: add set optimization options
Arturo Borrero [Tue, 23 Sep 2014 12:05:15 +0000 (14:05 +0200)] 
src: add set optimization options

This patch adds options to choose set optimization mechanisms.

Two new statements are added to the set syntax, and they can be mixed:

 nft add set filter set1 { type ipv4_addr ; size 1024 ; }
 nft add set filter set1 { type ipv4_addr ; policy memory ; }
 nft add set filter set1 { type ipv4_addr ; policy performance ; }
 nft add set filter set1 { type ipv4_addr ; policy memory ; size 1024 ; }
 nft add set filter set1 { type ipv4_addr ; size 1024 ; policy memory ; }
 nft add set filter set1 { type ipv4_addr ; policy performance ; size 1024 ; }
 nft add set filter set1 { type ipv4_addr ; size 1024 ; policy performance ; }

Also valid for maps:

 nft add map filter map1 { type ipv4_addr : verdict ; policy performace ; }
 [...]

This is the output format, which can be imported later with `nft -f':

table filter {
set set1 {
type ipv4_addr
policy memory
size 1024
}
}

In this approach the parser accepts default options such as 'performance',
given they are a valid configurations, but aren't sent to the kernel.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoparser: rearrange monitor/export rules
Patrick McHardy [Thu, 18 Sep 2014 13:58:22 +0000 (15:58 +0200)] 
parser: rearrange monitor/export rules

Move the output format rules next to the monitor and export command rules,
format them similar to other simple value mappings and unify their naming.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoqueue: clean up queue statement
Patrick McHardy [Wed, 24 Sep 2014 11:49:05 +0000 (13:49 +0200)] 
queue: clean up queue statement

- Rename keyword tokens to their actual keyword
- Change the grammar to follow the standard schema for statements and arguments
- Use actual expression for the queue numbers to support using normal range
  expressions, symbolic expression and so on.
- restore comma seperation of flag keywords

The result is that its possible to use standard ranges, prefix expressions,
symbolic expressions etc for the queue number. We get checks for overflow,
negative ranges and so on automatically.

The comma seperation of flags is more similar to what we have for other
flag values. It is still possible to use spaces, however this could be
removed since we never had a release supporting that.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoexpr: make range_low()/range_high() usable outside of segtree
Patrick McHardy [Wed, 24 Sep 2014 11:49:04 +0000 (13:49 +0200)] 
expr: make range_low()/range_high() usable outside of segtree

Their functionality is also needed for set descriptions, move the functions
to expressions.c and give them a more suitable name for global functions.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agomnl: consistency checks across several netlink dumps
Pablo Neira Ayuso [Tue, 16 Sep 2014 16:58:11 +0000 (18:58 +0200)] 
mnl: consistency checks across several netlink dumps

Obtain the generation ID before dumping the object lists. Then,
check for generation ID updates when dumping the several lists that
this needs. In case of interference, nft has to remove the stale
objects and retry from scratch.

This is complementary to the NLM_F_DUMP_INTR flag which is local
to one single netlink dump.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: Add any folder with test files.
Ana Rey [Thu, 18 Sep 2014 10:39:21 +0000 (12:39 +0200)] 
tests: Add any folder with test files.

"any" folder contains the test files that are executed in ipv4, ipv6,
inet, arp, bridge family of tables.

These test files are executed with nft-tests.py

Signed-off-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: Add bridge folder with test files.
Ana Rey [Thu, 18 Sep 2014 10:39:20 +0000 (12:39 +0200)] 
tests: Add bridge folder with test files.

"bridge" folder contains the test files that are executed in bridge tables.

These test files are executed with nft-tests.py

Signed-off-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: Add arp folder with test files.
Ana Rey [Thu, 18 Sep 2014 10:39:19 +0000 (12:39 +0200)] 
tests: Add arp folder with test files.

"arp" folder contains the test files that are executed in arp tables.

These test files are executed with nft-tests.py

Signed-off-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: Add inet folder with test files.
Ana Rey [Thu, 18 Sep 2014 10:39:18 +0000 (12:39 +0200)] 
tests: Add inet folder with test files.

"inet" folder contains the test files that are executed in ipv4, ipv6
and inet family of tables.

These test files are executed with nft-tests.py

Signed-off-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: Add ip6 folder with test files.
Ana Rey [Thu, 18 Sep 2014 10:39:17 +0000 (12:39 +0200)] 
tests: Add ip6 folder with test files.

"ip6" folder contains the test files that are executed in ip6 and inet
family of tables.

These test files are executed with nft-tests.py

Signed-off-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: Add ip folder with test files
Ana Rey [Thu, 18 Sep 2014 10:39:16 +0000 (12:39 +0200)] 
tests: Add ip folder with test files

"ip" folder contains the test files that are executed in ip and inet
family of tables.

These test files are executed with nft-tests.py

Signed-off-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: Add automated regression testing
Ana Rey [Thu, 18 Sep 2014 10:39:15 +0000 (12:39 +0200)] 
tests: Add automated regression testing

Here, the automated regression testing for nftables and some test
files.

This script checks that the rule input and output of nft matches.
More details here below.

A) What is this testing?

This script tests two different paths:

* The rule input from the command-line. This checks the different steps
  from the command line to the kernel. This includes the parsing,
  evaluation and netlink generation steps.

* The output listing that is obtained from the kernel. This checks the
  different steps from the kernel to the command line: The netlink
  message parsing, postprocess and textify steps to display the rule
  listing.

As a final step, this script compares that the rule that is added can
be listed by nft.

B) What options are available?

The script offers the following options:

* Execute test files:

./nft-test.py                           # Run all test files
./nft-test.py path/file.t               # Run this test file

If there is a problem, it shows the differences between the rule that
is added and the rule that is listed by nft.

In case you hit an error, the script doesn't keep testing for more
families. Unless you specify the --force-family option.

* Execute broken tests:

./nft-test.sh -e

This runs tests for rules that need a fix: This mode runs the lines that
that start with a "-" symbol.

* Debugging:

./nft-test.sh -d

This shows all the commands that the script executes, so you can watch
its internal behaviour.

* Keep testing all families on error.

./nft-test.sh -f

Don't stop testing for more families in case of error.

C) What is the structure of the test file?

A test file contains a set of rules that are added in the system.

Here, an example of a test file:

   *ip;test-ipv4                               # line 1
   *ip6;test-ipv6                              # line 2
   *inet;test-inet                             # line 3

   :input;type filter hook input priority 0    # line 4

   ah hdrlength != 11-23;ok;ah hdrlength < 11 ah hdrlength > 23   # line 5
   - tcp dport != {22-25}                                         # line 6

   !set1 ipv4_addr;ok                          # line 7
   ?set1 192.168.3.8 192.168.3.9;ok            # line 8
   # This is a commented-line.                 # line 9

Line 1 defines a table. The name of the table is 'test-ip' and the
family is ip. Lines 2 and 3 defines more tables for different families
so the rules in this test file are also tested there.

Line 4 defines the chain. The name of this chain is "input". The type is
"filter", the hook is "input" and the priority is 0.

Line 5 defines the rule, the ";" character is used as separator of several
parts:

* Part 1: "ah hdrlength != 11-23" is the rule to check.
* Part 2: "ok" is the result expected with the execute of this rule.
* Part 3: "ah hdrlength < 11 ah hdrlength > 23". This is the expected
  output. You can leave this empty if the output is the same as the
  input.

Line 6 is a marked line. This means that this rule is tested if
'-e' is passed as argument to nft-test.py.

Line 7 adds a new set. The name of this set is "set1" and the type
of this set is "ipv4_add".

Line 8 adds two elements into the 'set1' set: "192.168.3.8" and
"192.168.3.9". A whitespace separates the elements of the set.

Line 9 uses the "#" symbol that means that this line is commented out.

D) The test folders

The test files are divided in several directories: ip, ip6, inet, arp,
bridge and any.

 * "ip" folder contains the test files that are executed in ip and inet
   table.

 * "ip" folder contains the test files that are executed in ip6 and inet
   table.

 * "inet" folder contains the test files that are executed in the ip, ip6
    and inet table.

 * "arp" folder contains the test files that are executed in the arp
   table.

 * "bridge" folder: Here are the test files are executed in bridge
   tables.

 * "any" folder: Here are the test files are executed in ip, ip6, inet,
   arp and bridge tables.

E) Meaning of messages:

* A warning message means the rule input and output of nft mismatches.
* An error message means the nft-tool shows an error when we add it or
  the listing is broken after the rule is added.

Signed-off-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoparser: compact log level grammar
Patrick McHardy [Wed, 17 Sep 2014 10:22:38 +0000 (12:22 +0200)] 
parser: compact log level grammar

Put rule and action on a single line as for other simple mappings.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agosrc: fix 'describe' command when passing wrong expressions
Pablo Neira Ayuso [Tue, 16 Sep 2014 09:03:57 +0000 (11:03 +0200)] 
src: fix 'describe' command when passing wrong expressions

Before this patch:

 # nft describe tcp foo
 value expression, datatype inet_proto (Internet protocol) (basetype integer), 8 bits
 Segmentation fault

After this patch:

 # nft describe tcp foo
 <cmdline>:1:14-16: Error: syntax error, unexpected string, expecting end of file or newline or semicolon
 describe tcp foo
              ^^^

Reported-by: Kevin Fenzi <kevin@scrye.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoparser: simplify monitor command parsing
Patrick McHardy [Wed, 17 Sep 2014 07:43:52 +0000 (09:43 +0200)] 
parser: simplify monitor command parsing

Add tokens for "new" and "destroy". Split up the monitor flags into an
event and an object to avoid lots of duplicated code.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agosrc: add specific byteorder to the struct proto_hdr_template
Alvaro Neira Ayuso [Wed, 17 Sep 2014 07:20:28 +0000 (09:20 +0200)] 
src: add specific byteorder to the struct proto_hdr_template

If we try to add a rule like:

nft add rule filter input udp length {55-9999}

nftable shows:

BUG: invalid byte order conversion 0 => 2
nft: src/evaluate.c:153: byteorder_conversion_op: Assertion `0' failed.

Some of the existing payload fields rely on BYTEORDER_INVALID. Therefore, if we
try to convert it in evaluation step, we hit this bug.

This patch allows to add a specific byteorder to the struct proto_hdr_template. If
we create a expression with a invalid byteorder, we will use the byteorder
added to the proto_hdr_template structure.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agonetlink: include file and line in netlink ABI errors
Arturo Borrero [Mon, 15 Sep 2014 12:10:45 +0000 (14:10 +0200)] 
netlink: include file and line in netlink ABI errors

Let's give some more information when netlink ABI errors happens.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agolog: netlink_linearize: don't set level if user didn't specify
Pablo Neira Ayuso [Thu, 11 Sep 2014 13:17:17 +0000 (15:17 +0200)] 
log: netlink_linearize: don't set level if user didn't specify

The kernel will default on LOG_WARNING, to mimic iptables behaviour.

Reported-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoinclude: refresh include/linux/nf_tables.h cached copy
Pablo Neira Ayuso [Thu, 11 Sep 2014 12:10:48 +0000 (14:10 +0200)] 
include: refresh include/linux/nf_tables.h cached copy

Keep in sync with nf_tables.h header file in net-next tree.

Rename NFT_CT_LABEL to NFT_CT_LABELS, this probably slipped through
when editing the header file by hand.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: add `flush ruleset'
Arturo Borrero [Tue, 2 Sep 2014 14:42:22 +0000 (16:42 +0200)] 
src: add `flush ruleset'

This patch adds the `flush ruleset' operation to nft.

The syntax is:
 % nft flush ruleset [family]

To flush all the ruleset (all families):
 % nft flush ruleset

To flush the ruleset of a given family:
 % nft flush ruleset ip
 % nft flush ruleset inet

This flush is a shortcut operation which deletes all rules, sets, tables
and chains.
It's possible since the modifications in the kernel to the NFT_MSG_DELTABLE
API call.

Users can benefit of this operation when doing an atomic replacement of the
entire ruleset, loading a file like this:

 =========
 flush ruleset
 table ip filter {
  chain input {
counter accept
}
 }
 =========

Also, users who want to simply clean the ruleset for whatever reason can do it now
without having to iterate families/tables.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoFix typo in chain hook parsing
Yanchuan Nian [Thu, 4 Sep 2014 09:26:36 +0000 (17:26 +0800)] 
Fix typo in chain hook parsing

Just a typo in chain hook parsing

Signed-off-by: Yanchuan Nian <ycnian@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: Add devgroup support in meta expresion
Ana Rey [Tue, 2 Sep 2014 18:37:17 +0000 (20:37 +0200)] 
src: Add devgroup support in meta expresion

This adds device group support in meta expresion.

The new attributes of meta are "iffgroup" and "oifgroup"
- iffgroup: Match device group of incoming device.
- oifgroup: Match device group of outcoming device.

Example of use:
nft add rule ip test input meta iifgroup 2 counter
nft add rule ip test output meta oifgroup 2 counter

The kernel and libnftnl support were added in these commits:
netfilter: nf_tables: add devgroup support in meta expresion
src: meta: Add devgroup support to meta expresion

Signed-off-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: meta: Fix the size of cpu attribute
Ana Rey [Tue, 2 Sep 2014 18:13:25 +0000 (20:13 +0200)] 
src: meta: Fix the size of cpu attribute

Fix the size of cpu attribute in meta_template struct.

Signed-off-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agodoc: nft: Fix trivial error in man page where flush should be rename
Kevin Fenzi [Sat, 23 Aug 2014 15:59:52 +0000 (09:59 -0600)] 
doc: nft: Fix trivial error in man page where flush should be rename

Trivial fix, but someone filed a bug on it, and it should be fixed. ;)
https://bugzilla.redhat.com/show_bug.cgi?id=1132917

Signed-off-by: Kevin Fenzi <kevin@scrye.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: Add support for cpu in meta expresion
Ana Rey [Sun, 24 Aug 2014 12:21:00 +0000 (14:21 +0200)] 
src: Add support for cpu in meta expresion

This allows you to match cpu handling with a packet.

This is an example of the syntax for this new attribute:

nft add rule ip test input meta cpu 1 counter
nft add rule ip test input meta cpu 1-3 counter
nft add rule ip test input meta cpu { 1, 3} counter

Signed-off-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: Add support for pkttype in meta expresion
Ana Rey [Tue, 5 Aug 2014 18:33:39 +0000 (20:33 +0200)] 
src: Add support for pkttype in meta expresion

If you want to match the pkttype field of the skbuff, you have to
use the following syntax:

 nft add rule ip filter input meta pkttype PACKET_TYPE

where PACKET_TYPE can be: unicast, broadcast and multicast.

Joint work with Alvaro Neira Ayuso <alvaroneay@gmail.com>

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agopayload: use proto_unknown for raw protocol header
Yuxuan Shui [Sat, 9 Aug 2014 13:01:17 +0000 (21:01 +0800)] 
payload: use proto_unknown for raw protocol header

Otherwise payload.desc would be NULL, which causes the crash in bug 915.

Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: don't return error in netlink_linearize_rule()
Pablo Neira Ayuso [Mon, 18 Aug 2014 15:43:28 +0000 (17:43 +0200)] 
src: don't return error in netlink_linearize_rule()

This function converts the rule from the list of statements to the
netlink message format. The only two possible errors that can make
this function to fail are memory exhaustion and malformed statements
which inmediately stop the execution of nft.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoverdict type: handle verdict flags and encoded additional information
Patrick McHardy [Mon, 18 Aug 2014 23:35:36 +0000 (00:35 +0100)] 
verdict type: handle verdict flags and encoded additional information

The kernel can handle this, nftables should also.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoproto: fix byteorder of ETH_P_* values
Patrick McHardy [Mon, 18 Aug 2014 23:21:59 +0000 (00:21 +0100)] 
proto: fix byteorder of ETH_P_* values

The ethernet header type is in big endian byte order, the ETH_P_* values
are in host byte order however. Fix this using __constant_htons().

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agodatatype: take endianess into account in symbolic_constant_print()
Patrick McHardy [Mon, 18 Aug 2014 23:21:59 +0000 (00:21 +0100)] 
datatype: take endianess into account in symbolic_constant_print()

symbolic_constant_print() uses mpz_cmp_ui() to find the matching symbol.
Since GMP internally treats all values as being in host byte, this
doesn't work when the constant value is non-host byteorder, such as
the ethernet protocol type.

Export the expression's value in its original byteorder for comparison
to fix this.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agopayload: take endianess into account when updating the payload context
Patrick McHardy [Mon, 18 Aug 2014 23:21:59 +0000 (00:21 +0100)] 
payload: take endianess into account when updating the payload context

payload_expr_pctx_update() uses the numeric protocol value in host byte
order to find the upper layer protocol. This obviously doesn't work for
protocol expressions in other byte orders, such as the ethernet protocol
on little endian.

Export the protocol value in the correct byte order and use that value
to look up the upper layer protocol.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agolinealize: generate unary expression with the appropiate operation
Alvaro Neira [Mon, 18 Aug 2014 21:00:12 +0000 (23:00 +0200)] 
linealize: generate unary expression with the appropiate operation

If we add a unary expression which the operation is ntoh, we use hton.
This looks like a typo.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoFix memory leak in nft get operation
Yanchuan Nian [Mon, 11 Aug 2014 02:24:24 +0000 (10:24 +0800)] 
Fix memory leak in nft get operation

Some memories are forgotten to release on the error path in get operation.
Just release them. Also, in netlink_get_chain, it's better to return
immediately when a error is detected.

Signed-off-by: Yanchuan Nian <ycnian@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agodoc: update documentation with 'monitor' and 'export'
Arturo Borrero [Mon, 4 Aug 2014 08:29:45 +0000 (10:29 +0200)] 
doc: update documentation with 'monitor' and 'export'

Let's add info about 'monitor' and 'export'.

While at it, fix other minors things, like the no-netlink return code and
indentations of the document.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agosrc: add level option to the log statement
Pablo Neira Ayuso [Mon, 23 Jun 2014 00:49:38 +0000 (02:49 +0200)] 
src: add level option to the log statement

This patch is required if you use upcoming Linux kernels >= 3.17
which come with a complete logging support for nf_tables.

If you use 'log' without options, the kernel logging buffer is used:

nft> add rule filter input log

You can also specify the logging prefix string:

nft> add rule filter input log prefix "input: "

You may want to specify the log level:

nft> add rule filter input log prefix "input: " level notice

By default, if not specified, the default level is 'warn' (just like
in iptables).

If you specify the group, then nft uses the nfnetlink_log instead:

nft> add rule filter input log prefix "input: " group 10

You can also specify the snaplen and qthreshold for the nfnetlink_log.
But you cannot mix level and group at the same time, they are mutually
exclusive.

Default values for both snaplen and qthreshold are 0 (just like in
iptables).

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agomnl: fix crashes when using sets with many elements
Pablo Neira Ayuso [Wed, 23 Jul 2014 11:20:50 +0000 (13:20 +0200)] 
mnl: fix crashes when using sets with many elements

nft crashes when adding many elements into a set for two reasons:

1) The overflow of the nla_len field for the NFTA_SET_ELEM_LIST_ELEMENTS
   attribute.

2) Out-of-bound memory writes to the reserved area for the netlink
   message, which is solved by the patch entitled ("mnl: introduce
   NFT_NLMSG_MAXSIZE").

This patch adds the corresponding nla_len overflow check for
NFTA_SET_ELEM_LIST_ELEMENTS and it splits the elements in several
netlink messages. This should be enough when set updates are handled
by the transaction infrastructure.

With this patch, nft should be now capable of adding an unlimited
number of elements to a given set.

Fixes: https://bugzilla.netfilter.org/show_bug.cgi?id=898
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agomnl: introduce NFT_NLMSG_MAXSIZE
Pablo Neira Ayuso [Tue, 22 Jul 2014 20:16:59 +0000 (22:16 +0200)] 
mnl: introduce NFT_NLMSG_MAXSIZE

The NFT_NLMSG_MAXSIZE constant defines the maximum nf_tables netlink
message. Currently, the largest is the set element message, which
contains the NFTA_SET_ELEM_LIST_ELEMENTS attribute. This attribute is
a nest that describes the set elements. Given that the netlink attribute
length (nla_len) is 16 bits, the largest message is a bit larger than
64 KBytes. Thus, the proposed value of NFT_NLMSG_MAXSIZE is set to
(1 << 16) + getpagesize().

This new constant is used to calculate the length of:

1) the batch page length, when the batching mode is used.

2) the buffer that stores the netlink message in the send (when no
   batching is used) and receive paths.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agomain: propagate error to shell
Pablo Neira Ayuso [Tue, 22 Jul 2014 23:07:34 +0000 (01:07 +0200)] 
main: propagate error to shell

Before:

 # nft add rule ip test input ip hdrlength 3
 <cmdline>:1:1-37: Error: Could not process rule: Invalid argument
 add rule ip test input ip hdrlength 3
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 # echo $?
 0

After:

 # nft add rule ip test input ip hdrlength 3
 <cmdline>:1:1-37: Error: Could not process rule: Invalid argument
 add rule ip test input ip hdrlength 3
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 # echo $?
 1

Reported-by: Ana Rey Botello <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agosrc: rework batching logic to fix possible use of uninitialized pages
Pablo Neira Ayuso [Mon, 14 Jul 2014 17:01:25 +0000 (19:01 +0200)] 
src: rework batching logic to fix possible use of uninitialized pages

This patch reworks the batching logic in several aspects:

1) New batch pages are now always added into the batch page list in
   first place. Then, in the send path, if the last batch page is
   empty, it is removed from the batch list.

2) nft_batch_page_add() is only called if the current batch page is
   full. Therefore, it is guaranteed to find a valid netlink message
   in the batch page when moving the tail that didn't fit into a new
   batch page.

3) The batch paging is initialized and released from the nft_netlink()
   path.

4) No more global struct mnl_nlmsg_batch *batch that points to the
   current batch page. Instead, it is retrieved from the tail of the
   batch list, which indicates the current batch page.

This patch fixes a crash due to access of uninitialized memory area in
due to calling batch_page_add() with an empty batch in the send path,
and the memleak of the batch page contents. Reported in:

http://patchwork.ozlabs.org/patch/367085/
http://patchwork.ozlabs.org/patch/367774/

The patch is larger, but this saves the zeroing of the batch page area.

Reported-by: Yanchuan Nian <ycnian@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agomnl: add nft_nlmsg_batch_current() helper
Pablo Neira Ayuso [Mon, 14 Jul 2014 16:55:42 +0000 (18:55 +0200)] 
mnl: add nft_nlmsg_batch_current() helper

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agomnl: add nft_batch_continue() helper
Pablo Neira Ayuso [Mon, 14 Jul 2014 16:36:40 +0000 (18:36 +0200)] 
mnl: add nft_batch_continue() helper

Save some LOC with this function that wraps typical handling
after pushing the netlink message into the batch.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agonetlink: monitor: fix how rules with intervals are printed
Arturo Borrero [Mon, 14 Jul 2014 11:56:52 +0000 (13:56 +0200)] 
netlink: monitor: fix how rules with intervals are printed

Previous to this patch, if we add a rule like this:
 nft add rule filter test ip saddr { 1.1.1.1-2.2.2.2 }

The monitor operation output shows:
 add rule ip filter test ip saddr { 0.0.0.0, 1.1.1.1, 2.2.2.3}

The fix suggested by Pablo is to call interval_map_decompose().

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agonetlink: monitor: add a helper function to handle sets referenced by a rule
Arturo Borrero [Mon, 14 Jul 2014 11:56:46 +0000 (13:56 +0200)] 
netlink: monitor: add a helper function to handle sets referenced by a rule

This patch adds a helper function to handle lookup expressions with a callback,
so we can make an action for each set referenced by the rule.

Basically this is a refactorization, useful for follow-up patches.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
11 years agomnl: check for NLM_F_DUMP_INTR when dumping object lists
Pablo Neira Ayuso [Fri, 4 Jul 2014 10:24:46 +0000 (12:24 +0200)] 
mnl: check for NLM_F_DUMP_INTR when dumping object lists

This flag allows to detect that an update has ocurred while dumping
any of the object lists. In case of interference, nft cancels the
netlink socket to skip processing the remaining stale entries and
it retries to obtain fresh list of objects.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agomnl: immediately return on errors in mnl_nft_ruleset_dump()
Pablo Neira Ayuso [Sat, 12 Jul 2014 12:30:11 +0000 (14:30 +0200)] 
mnl: immediately return on errors in mnl_nft_ruleset_dump()

If this fails to fetch any of the objects, stop handling inmediately.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agoproto: initialize result expression in ethertype_parse()
Pablo Neira Ayuso [Tue, 1 Jul 2014 17:40:31 +0000 (19:40 +0200)] 
proto: initialize result expression in ethertype_parse()

Otherwise, you may crash in:

nft add rule bridge filter input ether type ip

Reported-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agoevaluate: fix concat expressions as map arguments
Patrick McHardy [Wed, 2 Jul 2014 07:42:30 +0000 (09:42 +0200)] 
evaluate: fix concat expressions as map arguments

The type in the evaluation context needs to be reset to avoid treating
the concatenation as a right hand side relational expression.

# nft filter output mark set ip daddr . tcp dport map { 192.168.0.1 . 22 : 1 }
<cmdline>:1:24-43: Error: datatype mismatch, expected packet mark, expression has type concatenation of (IPv4 address, internet network service)
filter output mark set ip daddr . tcp dport map { 192.168.0.1 . 22 : 1 }
                       ^^^^^^^^^^^^^^^^^^^^

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agonetlink: check and handle errors from netlink_delinearize_set()
Patrick McHardy [Thu, 10 Jul 2014 10:21:44 +0000 (12:21 +0200)] 
netlink: check and handle errors from netlink_delinearize_set()

Fix segfaults when delinearizing the set fails and abort on error when
listing sets.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agoBump version to v0.3 v0.3
Pablo Neira Ayuso [Mon, 30 Jun 2014 10:57:52 +0000 (12:57 +0200)] 
Bump version to v0.3

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agonetlink: Correct initial value of bytes counter in nftables rule
Yanchuan Nian [Thu, 19 Jun 2014 12:23:47 +0000 (20:23 +0800)] 
netlink: Correct initial value of bytes counter in nftables rule

Packages can be accounted by nftables through such command.
% nft add rule filter output ip daddr 8.8.8.8 counter

You can also give the initial values of packets and bytes.
% nft add rule filter output ip daddr 8.8.8.8 counter  packets 10 bytes 20

But packets and bytes are both initialized to 10 in above command for there is
a mistake in the program.

Signed-off-by: Yanchuan Nian <ycnian@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agosrc: revert broken reject icmp code support
Pablo Neira Ayuso [Fri, 20 Jun 2014 12:58:54 +0000 (14:58 +0200)] 
src: revert broken reject icmp code support

This patch reverts Alvaro's 34040b1 ("reject: add ICMP code parameter
for indicating the type of error") and 11b2bb2 ("reject: Use protocol
context for indicating the reject type").

These patches are flawed by two things:

1) IPv6 support is broken, only ICMP codes are considered.
2) If you don't specify any transport context, the utility exits without
   adding the rule, eg. nft add rule ip filter input reject.

The kernel is also flawed when it comes to the inet table. Let's revert
this until we can provide decent reject reason support.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agoreject: add ICMP code parameter for indicating the type of error
Álvaro Neira Ayuso [Wed, 11 Jun 2014 16:51:03 +0000 (18:51 +0200)] 
reject: add ICMP code parameter for indicating the type of error

This patch allows to indicate the ICMP code field in case that we
use to reject. Before, we have always sent network unreachable error
as ICMP code, now we can explicitly indicate the ICMP code that
we want to use. Examples:

nft add rule filter input tcp dport 22 reject with host-unreach
nft add rule filter input udp dport 22 reject with host-unreach

In this case, it will use the host unreachable code to reject traffic.

The default code field still is network unreachable and we can also
use the rules without the with like that:

nft add rule filter input udp dport 22 reject

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agoreject: Use protocol context for indicating the reject type
Álvaro Neira Ayuso [Wed, 11 Jun 2014 16:49:22 +0000 (18:49 +0200)] 
reject: Use protocol context for indicating the reject type

This patch uses the protocol context to initialize the reject type
considering if the transport protocol is tcp, udp, etc. Before this
patch, this was left unset.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agoqueue: More compact syntax
Álvaro Neira Ayuso [Tue, 10 Jun 2014 13:26:24 +0000 (15:26 +0200)] 
queue: More compact syntax

This patch allows to use a new syntax more compact and break
the current syntax. This new syntax is more similar than the nftables
syntax that we use usually. We can use range like we have used in
other case in nftables. Here, we have some examples:

Before, If we want to declare a queue, we have used a syntax like this:

 nft add rule test input queue num 1 total 3 options bypass,fanout

If we want to use the queue number 1 and the two next (total 3),
we use a range in the new syntax, for example:

 nft add rule test input queue num 1-3 bypass fanout

Also if we want to use only one queue, the new rules are like:

 nft add rule test input queue num 1 # queue 1

or

 nft add rule test input queue # queue 0

And if we want to add a specific flags we only need to put
what flags we want to use:

 nft add rule test input queue bypass

we don't need to use options and the comma for indicating the
flags.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agocli: fix nft -i command crashes when try to input multi line command
Guruswamy Basavaiah [Sat, 7 Jun 2014 19:14:16 +0000 (00:44 +0530)] 
cli: fix nft -i command crashes when try to input multi line command

When try to input multiline command in "nft -i", it crashes.

Issue is, function cli_append_multiline() return null in case of
multiline command. But in the calling function cli_complete(),
cli_exit is getting called, which in turn calls
rl_callback_handler_remove() and the handler is getting removed.

 [root@localhost ~]# nft -i
 nft> add table filter
 nft> list table \

 readline: readline_callback_read_char() called with no handler!
 Aborted (core dumped)
 [root@localhost ~]#

After this patch, it shows:

 nft> list table \
 .... filter
 table ip filter {
 }
 nft>

The ".... " prompt is used to indicate a multiline command, similar to
what Python does.

Signed-off-by: Guruswamy Basavaiah <guru2018@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agosrc: change type of chain.priority from unsigned int to int
Hendrik Schwartke [Mon, 9 Jun 2014 21:39:32 +0000 (23:39 +0200)] 
src: change type of chain.priority from unsigned int to int

This removes a bug that displays strange hook priorities
like "type route hook output priority 4294967146".

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agonetlink: Allow to invert the ranges
Álvaro Neira Ayuso [Wed, 28 May 2014 10:08:22 +0000 (12:08 +0200)] 
netlink: Allow to invert the ranges

This patch fix the bug:

http://bugzilla.netfilter.org/show_bug.cgi?id=924

Before, nftables doesn't permit invert ranges. This patch allows
add rules like this:

nft add rule ip test input ip daddr != 192.168.1.2-192.168.1.55
or
nft add rule ip test input ip daddr == 192.168.1.2-192.168.1.55

Also, we still have the option for adding rules like this:

sudo nft add rule ip test output frag id 33-45

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agopayload: Update the context only in equality relations
Álvaro Neira Ayuso [Mon, 2 Jun 2014 14:44:11 +0000 (16:44 +0200)] 
payload: Update the context only in equality relations

If we add this rule:

sudo nft add rule ip test input ip protocol != icmp

and we try to list the rules in the table test, nftables
show this error:

nft: src/payload.c:76: payload_expr_pctx_update: Assertion `expr->op == OP_EQ' failed.

This patch change the function payload_match_postprocess for updating
only the context in equality relations case.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agoscanner: Add udplite token
Álvaro Neira Ayuso [Tue, 27 May 2014 14:30:11 +0000 (16:30 +0200)] 
scanner: Add udplite token

If we add a udplite rule, we can't because we have forgot
to add this token in the scanner.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agonetlink: fix crash if kernel doesn't support nfnetlink / nf_tables
Pablo Neira Ayuso [Wed, 28 May 2014 08:27:01 +0000 (10:27 +0200)] 
netlink: fix crash if kernel doesn't support nfnetlink / nf_tables

When trying to close a descriptor which failed to be opened.

==6231== Process terminating with default action of signal 11 (SIGSEGV)
==6231==  Access not within mapped region at address 0x0
==6231==    at 0x5503E21: mnl_socket_close (socket.c:248)
==6231==    by 0x40517F: netlink_close_sock (netlink.c:68)
==6231==    by 0x400EFEE: _dl_fini (dl-fini.c:253)
==6231==    by 0x5740AA0: __run_exit_handlers (exit.c:77)
==6231==    by 0x5740B24: exit (exit.c:99)
==6231==    by 0x40F16F: netlink_open_error (netlink.c:105)
==6231==    by 0x405642: netlink_open_sock (netlink.c:54)
==6231==    by 0x424E6C: __libc_csu_init (in /usr/sbin/nft)
==6231==    by 0x5728924: (below main) (libc-start.c:219)
==6231==  If you believe this happened as a result of a stack
==6231==  overflow in your program's main thread (unlikely but
==6231==  possible), you can try to increase the size of the
==6231==  main thread stack using the --main-stacksize= flag.
==6231==  The main thread stack size used in this run was 8388608.

Closes: http://bugzilla.netfilter.org/show_bug.cgi?id=881
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agoparser: use symbolic expression for ether too
Pablo Neira Ayuso [Tue, 27 May 2014 15:24:39 +0000 (17:24 +0200)] 
parser: use symbolic expression for ether too

Like in 0dbced3 ("parser: use symbolic expressions for parsing
keywords as protocol values"), convert `ether' to use a symbolic
expression.

This fixes:

 # nft add rule ip filter input meta iiftype ether
 # nft list table filter
 table ip filter {
...
iiftype 256

which was converted to network byte order.

Reported-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agonetlink: don't add table/chain/set to ctx->list in the event path
Pablo Neira Ayuso [Thu, 22 May 2014 13:17:20 +0000 (15:17 +0200)] 
netlink: don't add table/chain/set to ctx->list in the event path

The delinearize functions for tables, chains and sets add these objects
to the ctx->list. In the chain case, this is not required. Regarding
tables and sets, those are added to the cache.

This patch implicitly fixes an use chain object after free that result
in random crashes.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agonetlink_delinearize: fix double free in relational_binop_postprocess()
Pablo Neira Ayuso [Thu, 22 May 2014 12:29:23 +0000 (14:29 +0200)] 
netlink_delinearize: fix double free in relational_binop_postprocess()

free(expr->right) and free(value) point to the same object, so one
single free() is enough.

This manifests in valgrind with:

==4020== Invalid read of size 4
==4020==    at 0x40A429: expr_free (expression.c:65)
==4020==    by 0x414032: expr_postprocess (netlink_delinearize.c:747)
==4020==    by 0x414C33: netlink_delinearize_rule (netlink_delinearize.c:883)
==4020==    by 0x411305: netlink_events_cb (netlink.c:1692)
==4020==    by 0x55040AD: mnl_cb_run (callback.c:77)
==4020==    by 0x4171E4: nft_mnl_recv (mnl.c:45)
==4020==    by 0x407B44: do_command (rule.c:895)
==4020==    by 0x405C6C: nft_run (main.c:183)
==4020==    by 0x405849: main (main.c:334)
==4020==  Address 0x5d126f8 is 56 bytes inside a block of size 120 free'd
==4020==    at 0x4C2AF5C: free (vg_replace_malloc.c:446)
==4020==    by 0x41402A: expr_postprocess (netlink_delinearize.c:746)
==4020==    by 0x414C33: netlink_delinearize_rule (netlink_delinearize.c:883)
==4020==    by 0x411305: netlink_events_cb (netlink.c:1692)
==4020==    by 0x55040AD: mnl_cb_run (callback.c:77)
==4020==    by 0x4171E4: nft_mnl_recv (mnl.c:45)
==4020==    by 0x407B44: do_command (rule.c:895)
==4020==    by 0x405C6C: nft_run (main.c:183)
==4020==    by 0x405849: main (main.c:334)
==4020==

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agoparser: remove the "new" and "destroy" tokens from the scanner
Pablo Neira Ayuso [Mon, 19 May 2014 17:42:46 +0000 (19:42 +0200)] 
parser: remove the "new" and "destroy" tokens from the scanner

These new tokens were introduced in f9563c0 ("src: add events reporting")
to allow filtering based on the event type.

This confuses the parser when parsing the "new" token:

test:32:33-35: Error: syntax error, unexpected new
add rule filter output ct state new,established counter
                                ^^^

This patch fixes this by replacing these event type tokens by the
generic string token, which is then interpreted during the parsing.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agosrc: add table netlink messages to the batch
Pablo Neira Ayuso [Mon, 19 May 2014 10:21:46 +0000 (12:21 +0200)] 
src: add table netlink messages to the batch

This patch moves the table messages to the netlink batch that
is sent to kernel-space.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agosrc: add chain netlink messages to the batch
Pablo Neira Ayuso [Mon, 19 May 2014 10:21:23 +0000 (12:21 +0200)] 
src: add chain netlink messages to the batch

This patch moves the chain netlink messages to the big netlink
batch that is sent to kernel-space.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agosrc: add set netlink message to the batch
Pablo Neira Ayuso [Mon, 19 May 2014 10:18:04 +0000 (12:18 +0200)] 
src: add set netlink message to the batch

This patch moves the netlink set messages to the batch that contains
the rules. This helps to speed up rule-set restoration time by
changing the operational. To achieve this, an internal set ID which
is unique to the batch is allocated as suggested by Patrick.

To retain backward compatibility, nft initially guesses if the
kernel supports set in batches. Otherwise, it falls back to the
previous (slowier) operational.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agomnl: split talk() and recv() functions
Arturo Borrero [Mon, 28 Apr 2014 14:48:09 +0000 (16:48 +0200)] 
mnl: split talk() and recv() functions

Let's split talk() and recv() functions, so they can be used independently.

While at it, lets rename mnl_talk() to nft_mnl_talk() so we avoid potential
clashes with other functions in external libs.

Suggested-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agosrc: add events reporting
Arturo Borrero [Wed, 16 Apr 2014 16:43:17 +0000 (18:43 +0200)] 
src: add events reporting

This patch adds a basic events reporting option to nft.

The syntax is:
 % nft monitor [new|destroy] [tables|chains|rules|sets|elements] [xml|json]

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agonetlink: add socket error reporting helper function
Arturo Borrero [Mon, 14 Apr 2014 10:17:41 +0000 (12:17 +0200)] 
netlink: add socket error reporting helper function

This patch adds a simple helper function to report errors while
opening the Netlink socket.

To help users to diagnose problems, a new NFT_EXIT_NONL exit code is included,
which is 3.

Suggested-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agonetlink: refactorize set_elem conversion from netlink
Arturo Borrero [Mon, 14 Apr 2014 10:17:35 +0000 (12:17 +0200)] 
netlink: refactorize set_elem conversion from netlink

Lets refactorize set_elem handling.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agonetlink: add netlink_delinearize_table() func
Arturo Borrero [Mon, 14 Apr 2014 10:17:30 +0000 (12:17 +0200)] 
netlink: add netlink_delinearize_table() func

This code is suitable to be reusable.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agonetlink: add netlink_delinearize_chain() func
Arturo Borrero [Mon, 14 Apr 2014 10:17:24 +0000 (12:17 +0200)] 
netlink: add netlink_delinearize_chain() func

Let's make this code reusable.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agorule: generalize chain_print()
Arturo Borrero [Mon, 14 Apr 2014 10:17:19 +0000 (12:17 +0200)] 
rule: generalize chain_print()

Lest generalize the chain_print() function, so we can print a plain chain
as the user typed in the basic CLI.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agonetlink: add netlink_delinearize_set() func
Arturo Borrero [Mon, 14 Apr 2014 10:17:14 +0000 (12:17 +0200)] 
netlink: add netlink_delinearize_set() func

Let's factorize this code, so it can be reused.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agorule: allow to print sets in plain format
Arturo Borrero [Mon, 14 Apr 2014 10:17:08 +0000 (12:17 +0200)] 
rule: allow to print sets in plain format

Allow to print sets with or without format.

This is useful in situations where we want to print more or less the same
the user typed (IOW, in one single line, and with family/table info).

While at it, make family2str() function public, so it can be used in
other places.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agometa: Add support for input and output bridge interface name
Tomasz Bursztyka [Mon, 14 Apr 2014 12:41:30 +0000 (15:41 +0300)] 
meta: Add support for input and output bridge interface name

Add support to get an input or output bridge interface name through the
relevant meta keys.

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agoBump version to v0.2 v0.2
Patrick McHardy [Mon, 14 Apr 2014 10:56:03 +0000 (12:56 +0200)] 
Bump version to v0.2

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agodoc: fix make install problems
Patrick McHardy [Mon, 14 Apr 2014 10:04:31 +0000 (12:04 +0200)] 
doc: fix make install problems

-e   INSTALL doc
/usr/bin/install: cannot stat 'doc/nftables.8': No such file or directory
make[1]: *** [install] Error 1
make: *** [doc] Error 2

Rename everything to nft.* to fix this up.

Reported-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agodoc: change documentation license to CC BY-SA 4.0
Patrick McHardy [Mon, 14 Apr 2014 06:52:43 +0000 (08:52 +0200)] 
doc: change documentation license to CC BY-SA 4.0

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agodoc: fix programlisting indentation
Patrick McHardy [Mon, 14 Apr 2014 06:37:26 +0000 (08:37 +0200)] 
doc: fix programlisting indentation

Since programlistings are used literally, the should not be indented.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agodatatypes: rename some types for more consistency
Patrick McHardy [Sun, 13 Apr 2014 10:58:25 +0000 (12:58 +0200)] 
datatypes: rename some types for more consistency

Add some more consistency by using _addr for all address types, _proto
for all protocol types and iface_ for all interface types.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agodoc: documentation update
Patrick McHardy [Wed, 26 Mar 2014 12:28:59 +0000 (12:28 +0000)] 
doc: documentation update

Signed-off-by: Patrick McHardy
11 years agobuild: fix documentation build
Patrick McHardy [Mon, 14 Apr 2014 06:18:47 +0000 (08:18 +0200)] 
build: fix documentation build

Handle the docbook2x-man mess that is called differently on different distributions.
Also switch to dblatex since db2pdf is unable to handle XML on Fedora (and probably
other distributions).

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agonetlink: fix length value of concat data
Patrick McHardy [Mon, 14 Apr 2014 05:41:04 +0000 (07:41 +0200)] 
netlink: fix length value of concat data

The length is measured in bytes, not bits.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agogmputil: use MSF/LSF in import/export functions dependant on host byte order
Patrick McHardy [Sat, 12 Apr 2014 08:40:08 +0000 (10:40 +0200)] 
gmputil: use MSF/LSF in import/export functions dependant on host byte order

For data of byteorder BYTEORDER_HOST_ENDIAN we need to set the word order
dependant on the host byte order.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agoexpression: fix constant expression allocation on big endian
Arturo Borrero Gonzalez [Sat, 12 Apr 2014 08:40:08 +0000 (10:40 +0200)] 
expression: fix constant expression allocation on big endian

When allocating a constant expression, a pointer to the data is passed
to the allocation function. When the variable used to store the data
is larger than the size of the data type, this fails on big endian since
the most significant bytes (being zero) come first.

Add a helper function to calculate the proper address for the cases
where this is needed.

This currently affects symbolic tables for values < u64 and payload
dependency generation for protocol values < u32.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agoMerge branch 'master' of git.netfilter.org:nftables
Patrick McHardy [Fri, 11 Apr 2014 13:55:24 +0000 (15:55 +0200)] 
Merge branch 'master' of git.netfilter.org:nftables

11 years agoparser: fix ether keyword clash
Patrick McHardy [Fri, 11 Apr 2014 13:50:15 +0000 (15:50 +0200)] 
parser: fix ether keyword clash

Due to the renaming of the "eth" keyword to "ether", parsing of arphrd
type "ether" fails.

Fix similar to TCP, UDP etc by allocating a constant arphrd expression for
the ether keyword without a following key.

Signed-off-by: Patrick McHardy <kaber@trash.net>