]> git.ipfire.org Git - thirdparty/iptables.git/log
thirdparty/iptables.git
8 years agolibxtables: Display weird character warning for wildcards
Oliver Ford [Fri, 5 May 2017 11:11:50 +0000 (11:11 +0000)] 
libxtables: Display weird character warning for wildcards

Change the scope of the weird character check loop so that
it checks for invalid characters when the interface name
contains a wildcard.

Fixes Bugzilla #1085.

Signed-off-by: Oliver Ford <ojford@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@gnumonks.org>
8 years agotests: xlate: check if it is being run as root
Pablo M. Bermudo Garay [Tue, 18 Apr 2017 23:19:09 +0000 (01:19 +0200)] 
tests: xlate: check if it is being run as root

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: xlate: remove python 3.5 dependency
Pablo M. Bermudo Garay [Tue, 18 Apr 2017 23:19:08 +0000 (01:19 +0200)] 
tests: xlate: remove python 3.5 dependency

This commit replaces subprocess.run (introduced in python 3.5) with
subprocess.Popen (supported since the first version of python 3).

Furthermore, the output has been improved when ip[6]tables-translate
exits with non-zero return code.

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoiptables-restore/save: exit when given an unknown option
Vincent Bernat [Sat, 15 Apr 2017 10:16:47 +0000 (12:16 +0200)] 
iptables-restore/save: exit when given an unknown option

When an unknown option is given, iptables-restore should exit instead of
continue its operation. For example, if `--table` was misspelled, this
could lead to an unwanted change. Moreover, exit with a status code of
1. Make the same change for iptables-save.

OTOH, exit with a status code of 0 when requesting help.

Signed-off-by: Vincent Bernat <vincent@bernat.im>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoiptables-restore.8: document -w/-W options
Dan Williams [Mon, 10 Apr 2017 17:35:18 +0000 (12:35 -0500)] 
iptables-restore.8: document -w/-W options

Fixes: 999eaa241212 ("iptables-restore: support acquiring the lock.")
Signed-off-by: Dan Williams <dcbw@redhat.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoiptables-restore/ip6tables-restore: add --version/-V argument
Dan Williams [Mon, 10 Apr 2017 17:31:56 +0000 (12:31 -0500)] 
iptables-restore/ip6tables-restore: add --version/-V argument

Prints program version just like iptables/ip6tables.

Signed-off-by: Dan Williams <dcbw@redhat.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoextensions: libxt_hashlimit: fix 64-bit printf formats
James Cowgill [Fri, 7 Apr 2017 11:47:29 +0000 (12:47 +0100)] 
extensions: libxt_hashlimit: fix 64-bit printf formats

hashlimit was using "%lu" in a lot of printf format specifiers to print
64-bit integers. This is incorrect on 32-bit architectures because
"long int" is 32-bits there. On MIPS, it was causing iptables to
segfault when printing these integers.

Fix by using the correct format specifier.

Signed-off-by: James Cowgill <James.Cowgill@imgtec.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoiptables: extensions: Remove typedef in struct.
Arushi Singhal [Thu, 30 Mar 2017 09:04:05 +0000 (14:34 +0530)] 
iptables: extensions: Remove typedef in struct.

The Linux kernel coding style guidelines suggest not using typedefs for
structure. This patch gets rid of the typedefs for "_code".
The following Coccinelle semantic patch detects the cases for struct
type:

@tn@
identifier i;
type td;
@@

-typedef
 struct i { ... }
-td
 ;

@@
type tn.td;
identifier tn.i;
@@

-td
+ struct i

Signed-off-by: Arushi Singhal <arushisinghal19971997@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: add regression tests for xtables-translate
Pablo M. Bermudo Garay [Fri, 31 Mar 2017 12:34:38 +0000 (14:34 +0200)] 
tests: add regression tests for xtables-translate

This test suite is intended to detect regressions in the translation
infrastructure. The script checks if ip[6]tables-translate produces the
expected output, otherwise it prints the wrong translation and the
expected one.

** Arguments

  --all     # Show also passed tests
  [test]    # Run only the specified test file

** Test files structure

Test files are located under extensions directory. Every file contains
tests about specific extension translations. A test file name must end
with ".txlate".

Inside the files, every single test is defined by two consecutive lines:
ip[6]tables-translate command and expected result. One blank line is left
between tests by convention.

e.g.

  $ cat extensions/libxt_cpu.txlate
  iptables-translate -A INPUT -p tcp --dport 80 -m cpu --cpu 0 -j ACCEPT
  nft add rule ip filter INPUT tcp dport 80 cpu 0 counter accept

  iptables-translate -A INPUT -p tcp --dport 80 -m cpu ! --cpu 1 -j ACCEPT
  nft add rule ip filter INPUT tcp dport 80 cpu != 1 counter accept

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoextensions: libxt_TOS: Add translation to nft
Gargi Sharma [Tue, 28 Mar 2017 18:50:18 +0000 (00:20 +0530)] 
extensions: libxt_TOS: Add translation to nft

Add translation for TOS to nftables. TOS is deprecated
ans DSCP is ued in place of it. The first 6 bits of
TOS specify the DSCP value.

Examples:

$ iptables-translate -t mangle -A PREROUTING -p TCP --dport 22 -j TOS --set-tos 0x10
nft add rule ip mangle PREROUTING tcp dport 22 counter  ip6 dscp set 0x04

Signed-off-by: Gargi Sharma <gs051095@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoiptables: Remove unnecessary braces.
Varsha Rao [Wed, 29 Mar 2017 19:27:43 +0000 (00:57 +0530)] 
iptables: Remove unnecessary braces.

Remove braces which are not required, to fix the check patch issue.
The following coccinelle script was used to fix this issue.

@@
expression e;
expression e1;
@@
if(e)
-{
 e1;
-}

Signed-off-by: Varsha Rao <rvarsha016@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoiptables: Remove explicit static variables initalization.
Varsha Rao [Wed, 29 Mar 2017 19:27:02 +0000 (00:57 +0530)] 
iptables: Remove explicit static variables initalization.

Static variables are initialized to zero by default, so remove explicit
initalization. This patch fixes the checkpatch issue.

Signed-off-by: Varsha Rao <rvarsha016@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoiptables: Constify option struct
Gargi Sharma [Mon, 27 Mar 2017 19:10:15 +0000 (00:40 +0530)] 
iptables: Constify option struct

The struct  of the type option is only used to initialise a field
inside the xtables_match struct and is not modified anywhere.
Done using Coccinelle:

@r1 disable optional_qualifier@
identifier s,i;
position p;
@@

static struct option i@p[] ={...};

@ok1@
identifier r1.i;
expression e;
position p;
@@
e = i@p

@bad@
position p != {r1.p,ok1.p};
identifier r1.i;
@@
e@i@p

@depends on !bad disable optional_qualifier@
identifier r1.i;
@@
static
+const
struct option i[] = { ... };

Signed-off-by: Gargi Sharma <gs051095@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoiptables-restore: support acquiring the lock.
Lorenzo Colitti [Thu, 16 Mar 2017 07:55:02 +0000 (16:55 +0900)] 
iptables-restore: support acquiring the lock.

Currently, ip[6]tables-restore does not perform any locking, so it
is not safe to use concurrently with ip[6]tables.

This patch makes ip[6]tables-restore wait for the lock if -w
was specified. Arguments to -w and -W are supported in the same
was as they are in ip[6]tables.

The lock is not acquired on startup. Instead, it is acquired when
a new table handle is created (on encountering '*') and released
when the table is committed (COMMIT). This makes it possible to
keep long-running iptables-restore processes in the background
(for example, reading commands from a pipe opened by a system
management daemon) and simultaneously run iptables commands.

If -w is not specified, then the command proceeds without taking
the lock.

Tested as follows:

1. Run iptables-restore -w, and check that iptables commands work
   with or without -w.
2. Type "*filter" into the iptables-restore input. Verify that
   a) ip[6]tables commands without -w fail with "another app is
      currently holding the xtables lock...".
   b) ip[6]tables commands with "-w 2" fail after 2 seconds.
   c) ip[6]tables commands with "-w" hang until "COMMIT" is
      typed into the iptables-restore window.
3. With the lock held by an ip6tables-restore process:
     strace -e flock /tmp/iptables/sbin/iptables-restore -w 1 -W 100000
   shows 11 calls to flock and fails.
4. Run an iptables-restore with -w and one without -w, and check:
   a) Type "*filter" in the first and then the second, and the
      second exits with an error.
   b) Type "*filter" in the second and "*filter" "-S" "COMMIT"
      into the first. The rules are listed only when the first
      copy sees "COMMIT".

Signed-off-by: Narayan Kamath <narayan@google.com>
Signed-off-by: Lorenzo Colitti <lorenzo@google.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoiptables: remove duplicated argument parsing code
Lorenzo Colitti [Thu, 16 Mar 2017 07:55:01 +0000 (16:55 +0900)] 
iptables: remove duplicated argument parsing code

1. Factor out repeated code to a new xs_has_arg function.
2. Add a new parse_wait_time option to parse the value of -w.
3. Make parse_wait_interval take argc and argv so its callers
   can be simpler.

Signed-off-by: Lorenzo Colitti <lorenzo@google.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoiptables: move XT_LOCK_NAME from CFLAGS to config.h.
Lorenzo Colitti [Thu, 16 Mar 2017 03:54:20 +0000 (12:54 +0900)] 
iptables: move XT_LOCK_NAME from CFLAGS to config.h.

This slightly simplifies configure.ac and results in more
correct dependencies.

Tested by running ./configure with --with-xt-lock-name and
without, and using strace to verify that the right lock is used.

$ make distclean-recursive && ./autogen.sh &&
  ./configure --disable-nftables --prefix /tmp/iptables &&
  make -j64 &&
  make install &&
  sudo strace -e open,flock /tmp/iptables/sbin/iptables -L foo
...
open("/run/xtables.lock", O_RDONLY|O_CREAT, 0600) = 3
flock(3, LOCK_EX|LOCK_NB)               = 0

$ make distclean-recursive && ./autogen.sh && \
  ./configure --disable-nftables --prefix /tmp/iptables \
  --with-xt-lock-name=/tmp/iptables/run/xtables.lock &&
  make -j64 &&
  make install &&
  sudo strace -e open,flock /tmp/iptables/sbin/iptables -L foo
...
open("/tmp/iptables/run/xtables.lock", O_RDONLY|O_CREAT, 0600) = 3
flock(3, LOCK_EX|LOCK_NB)               = 0

Signed-off-by: Lorenzo Colitti <lorenzo@google.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoiptables: set the path of the lock file via a configure option.
Lorenzo Colitti [Tue, 14 Mar 2017 08:55:50 +0000 (17:55 +0900)] 
iptables: set the path of the lock file via a configure option.

Currently the iptables lock is hardcoded as "/run/xtables.lock".
Allow users to change this path using the --with-xt-lock-name
option to ./configure option. This is useful on systems like
Android which do not have /run.

Tested on Ubuntu, as follows:

1. By default, the lock is placed in /run/xtables.lock:

$ make distclean-recursive && ./autogen.sh &&
  ./configure --disable-nftables --prefix /tmp/iptables &&
  make -j64 &&
  make install &&
  sudo strace -e open,flock /tmp/iptables/sbin/iptables -L foo
...
open("/run/xtables.lock", O_RDONLY|O_CREAT, 0600) = 3
flock(3, LOCK_EX|LOCK_NB)               = 0
iptables: No chain/target/match by that name.

2. Specifying the lock results in the expected location being
   used:

$ make distclean-recursive && ./autogen.sh && \
  ./configure --disable-nftables --prefix /tmp/iptables \
  --with-xt-lock-name=/tmp/iptables/run/xtables.lock &&
  make -j64 &&
  make install &&
  sudo strace -e open,flock /tmp/iptables/sbin/iptables -L foo
...
open("/tmp/iptables/run/xtables.lock", O_RDONLY|O_CREAT, 0600) = 3
flock(3, LOCK_EX|LOCK_NB)               = 0
iptables: No chain/target/match by that name.

Signed-off-by: Lorenzo Colitti <lorenzo@google.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoiptables-translate: print nft iff there are more expanded rules to print
Pablo Neira Ayuso [Thu, 9 Mar 2017 10:56:39 +0000 (11:56 +0100)] 
iptables-translate: print nft iff there are more expanded rules to print

$ iptables-translate -I INPUT -s yahoo.com
nft insert rule ip filter INPUT ip saddr 98.139.183.24 counter
nft insert rule ip filter INPUT ip saddr 206.190.36.45 counter
nft insert rule ip filter INPUT ip saddr 98.138.253.109 counter
nft

This extra 'nft' print is incorrect, just print it if there are more
rules to be printed.

Reported-by: Alexander Alemayhu <alexander@alemayhu.com>
Tested-by: Alexander Alemayhu <alexander@alemayhu.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agolibxtables: abolish AI_CANONNAME
Jan Engelhardt [Wed, 8 Mar 2017 16:26:57 +0000 (17:26 +0100)] 
libxtables: abolish AI_CANONNAME

ares->ai_canonname is never used, so there is no point in requesting
that piece of information with AI_CANONNAME.

Signed-off-by: Jan Engelhardt <jengelh@inai.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agolibxtables: remove unnecessary nesting from host_to_ip(6)addr
Jan Engelhardt [Wed, 8 Mar 2017 16:26:56 +0000 (17:26 +0100)] 
libxtables: remove unnecessary nesting from host_to_ip(6)addr

The error path already terminally returns from the function, so there
is no point in having an explicit else block.

Signed-off-by: Jan Engelhardt <jengelh@inai.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoiptables-translate: print nft command for each expand rules via dns names
Pablo Neira Ayuso [Wed, 8 Mar 2017 13:16:09 +0000 (14:16 +0100)] 
iptables-translate: print nft command for each expand rules via dns names

We have to print nft at the very beginning for each rule that rules from
the expansion, otherwise the output is not correct:

 # iptables-translate -I INPUT -s yahoo.com
 nft insert rule ip filter INPUT ip saddr 206.190.36.45 counter
 insert rule ip filter INPUT ip saddr 98.138.253.109 counter
 insert rule ip filter INPUT ip saddr 98.139.183.24 counter

After this patch:

 # iptables-translate -I INPUT -s yahoo.com
 nft insert rule ip filter INPUT ip saddr 206.190.36.45 counter
 nft insert rule ip filter INPUT ip saddr 98.138.253.109 counter
 nft insert rule ip filter INPUT ip saddr 98.139.183.24 counter

Reported-by: Alexander Alemayhu <alexander@alemayhu.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoxtables-translate: Avoid querying the kernel
Phil Sutter [Wed, 8 Mar 2017 15:43:25 +0000 (16:43 +0100)] 
xtables-translate: Avoid querying the kernel

This originally came up when accidentally calling iptables-translate as
unprivileged user - nft_compatible_revision() then fails every time,
making the translator fall back to using revision 0 only which often
leads to failed translations (due to missing xlate callback).

The bottom line is there is no need to check what revision of a given
iptables match the kernel supports when it is only to be translated into
an nftables equivalent. So just assign a dummy callback returning good
for any revision being asked for.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoextensions: libxt_addrtype: Add translation to nft
Phil Sutter [Wed, 8 Mar 2017 01:06:14 +0000 (02:06 +0100)] 
extensions: libxt_addrtype: Add translation to nft

Translate addrtype match into fib expression:

$ iptables-translate -A INPUT -m addrtype --src-type LOCAL
nft add rule ip filter INPUT fib saddr type local counter

$ iptables-translate -A INPUT -m addrtype --dst-type LOCAL
nft add rule ip filter INPUT fib daddr type local counter

$ iptables-translate -A INPUT -m addrtype ! --dst-type ANYCAST,LOCAL
nft add rule ip filter INPUT fib daddr type != { local, anycast } counter

$ iptables-translate -A INPUT -m addrtype --limit-iface-in --dst-type ANYCAST,LOCAL
nft add rule ip filter INPUT fib daddr . iif type { local, anycast } counter

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoutils: nfsynproxy: fix build with musl libc
Baruch Siach [Thu, 2 Mar 2017 07:35:07 +0000 (09:35 +0200)] 
utils: nfsynproxy: fix build with musl libc

The musl libc exposes some struct tcphdr field only when _GNU_SOURCE is
defined. Fix the following build failure:

nfsynproxy.c: In function ‘parse_packet’:
nfsynproxy.c:34:9: error: ‘const struct tcphdr’ has no member named ‘syn’
  if (!th->syn || !th->ack)
         ^
nfsynproxy.c:34:21: error: ‘const struct tcphdr’ has no member named ‘ack’
  if (!th->syn || !th->ack)
                     ^
nfsynproxy.c:42:8: error: ‘const struct tcphdr’ has no member named ‘res2’
  if (th->res2 == 0x1)
        ^
nfsynproxy.c:45:13: error: ‘const struct tcphdr’ has no member named ‘doff’
  length = th->doff * 4 - sizeof(*th);
             ^

Signed-off-by: Baruch Siach <baruch@tkos.co.il>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agolibiptc: don't set_changed() when checking rules with module jumps
Dan Williams [Sun, 26 Feb 2017 04:02:03 +0000 (22:02 -0600)] 
libiptc: don't set_changed() when checking rules with module jumps

Checking a rule that includes a jump to a module-based target currently
sets the "changed" flag on the handle, which then causes TC_COMMIT() to
run through the whole SO_SET_REPLACE/SO_SET_ADD_COUNTERS path.  This
seems wrong for simply checking rules, an operation which is documented
as "...does not alter the existing iptables configuration..." but yet
it clearly could do so.

Fix that by ensuring that rule check operations for module targets
don't set the changed flag, and thus exit early from TC_COMMIT().

Signed-off-by: Dan Williams <dcbw@redhat.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoextensions: libxt_hashlimit: Add translation to nft
Elise Lennion [Mon, 27 Feb 2017 17:43:08 +0000 (14:43 -0300)] 
extensions: libxt_hashlimit: Add translation to nft

Hashlimit has similar functionality to flow tables in nftables. Some
usage examples are:

$ iptables-translate -A OUTPUT -m tcp -p tcp --dport 443 -m hashlimit \
--hashlimit-above 20kb/s --hashlimit-burst 1mb --hashlimit-mode dstip \
--hashlimit-name https --hashlimit-dstmask 24 -m state --state NEW \
-j DROP

nft add rule ip filter OUTPUT tcp dport 443 flow table https { ip \
daddr and 255.255.255.0 timeout 60s limit rate over 20 kbytes/second \
burst 1 mbytes} ct state new  counter drop

$ iptables-translate -A OUTPUT -m tcp -p tcp --dport 443 -m hashlimit \
--hashlimit-upto 300 --hashlimit-burst 15 --hashlimit-mode \
srcip,dstip --hashlimit-name https --hashlimit-htable-expire 300000 \
-m state --state NEW -j DROP

nft add rule ip filter OUTPUT tcp dport 443 flow table https { ip \
daddr . ip saddr timeout 300s limit rate 300/second burst 15 packets} \
ct state new  counter drop

The translation isn't supported when --hashlimit-mode isn't specified.
Also, the following options don't apply to flow tables:

--hashlimit-htable-size
--hashlimit-htable-max
--hashlimit-htable-gcinterval

Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoxshared: using the blocking file lock request when we wait indefinitely
Liping Zhang [Mon, 6 Feb 2017 11:47:47 +0000 (19:47 +0800)] 
xshared: using the blocking file lock request when we wait indefinitely

When using "-w" to avoid concurrent instances, we try to do flock() every
one second until it success. But one second maybe too long in some
situations, and it's hard to select a suitable interval time. So when
using "iptables -w" to wait indefinitely, it's better to block until
it become success.

Now do some performance tests. First, flush all the iptables rules in
filter table, and run "iptables -w -S" endlessly:
  # iptables -F
  # iptables -X
  # while : ; do
  iptables -w -S >&- &
  done

Second, after adding and deleting the iptables rules 100 times, measure
the time cost:
  # time for i in $(seq 100); do
  iptables -w -A INPUT
  iptables -w -D INPUT
  done

Before this patch:
  real  1m15.962s
  user  0m0.224s
  sys   0m1.475s

Apply this patch:
  real  0m1.830s
  user  0m0.168s
  sys   0m1.130s

Signed-off-by: Liping Zhang <zlpnobody@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoxshared: do not lock again and again if "-w" option is not specified
Liping Zhang [Sun, 5 Feb 2017 13:57:34 +0000 (21:57 +0800)] 
xshared: do not lock again and again if "-w" option is not specified

After running the following commands, some confusing messages was printed
out:
  # while : ; do
  iptables -A INPUT &
  iptables -D INPUT &
  done
  [...]
  Another app is currently holding the xtables lock; still -9s 0us time
  ahead to have a chance to grab the lock...
  Another app is currently holding the xtables lock; still -29s 0us time
  ahead to have a chance to grab the lock...

If "-w" option is not specified, the "wait" will be zero, so we should
check whether the timer_left is less than wait_interval before we call
select to sleep.

Also remove unused "BASE_MICROSECONDS" and "struct timeval waited_time"
introduced by commit e8f857a5a151 ("xtables: Add an interval option for
xtables lock wait").

Fixes: e8f857a5a151 ("xtables: Add an interval option for xtables lock wait")
Signed-off-by: Liping Zhang <zlpnobody@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agolibxt_hashlimit: add new unit test to catch kernel bug
Pablo Neira Ayuso [Tue, 21 Feb 2017 12:58:33 +0000 (13:58 +0100)] 
libxt_hashlimit: add new unit test to catch kernel bug

commit ad5b55761956427f61ed9c96961bf9c5cd4f92dc
Author: Alban Browaeys <alban.browaeys@gmail.com>
Date:   Mon Feb 6 23:50:33 2017 +0100

    netfilter: xt_hashlimit: Fix integer divide round to zero.

http://patchwork.ozlabs.org/patch/724800/

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoiptables: update pf.os
Xose Vazquez Perez [Mon, 30 Jan 2017 10:30:16 +0000 (11:30 +0100)] 
iptables: update pf.os

Sync with latest OpenBSD release.
Changelog: http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/etc/pf.os

Cc: Pablo Neira Ayuso <pablo@netfilter.org>
Cc: netfilter-devel <netfilter-devel@vger.kernel.org>
Signed-off-by: Xose Vazquez Perez <xose.vazquez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoiptables: fix the wrong appending of jump verdict after the comment. v1.6.1
Shyam Saini [Thu, 26 Jan 2017 09:19:50 +0000 (14:49 +0530)] 
iptables: fix the wrong appending of jump verdict after the comment.

Fix wrong appending of jump verdict after the comment

For example:
$ iptables-translate -A INPUT -p tcp -m tcp --sport http -s  192.168.0.0/16 -d 192.168.0.0/16 -j LONGNACCEPT -m comment --comment "foobar"
nft add rule ip filter INPUT ip saddr 192.168.0.0/16 ip daddr 192.168.0.0/16 tcp sport 80 counter comment \"foobar\"jump LONGNACCEPT

Note that even without comment with double-quotes (i.e. --comment
"foobar"), it will add quotes:

$ iptables-translate -A FORWARD -p tcp -m tcp --sport http -s 192.168.0.0/16 -d 192.168.0.0/16 -j DROP -m comment --comment singlecomment
nft add rule ip filter FORWARD ip saddr 192.168.0.0/16 ip daddr 192.168.0.0/16 tcp sport 80 counter comment \"singlecomment\"drop

Attempting to apply the translated/generated rule will result to:

$ nft add rule ip filter INPUT ip saddr 192.168.0.0/16 ip daddr  192.168.0.0/16 tcp sport 80 counter comment \"foobar\"jump LONGNACCEPT
<cmdline>:1:111-114: Error: syntax error, unexpected jump, expecting endof file or newline or semicolon
add rule ip filter INPUT ip saddr 192.168.0.0/16 ip daddr 192.168.0.0/16 tcp sport 80 counter comment "foobar"jump LONGNACCEPT

After this patch
$ iptables-translate -A INPUT -p tcp -m tcp --sport http -s 192.168.0.0/16 -d 192.168.0.0/16 -j LONGNACCEPT -m comment --comment "foobar"
nft add rule ip filter INPUT ip saddr 192.168.0.0/16 ip daddr 192.168.0.0/16 tcp sport 80 counter jump LONGNACCEPT comment \"foobar\"
which is correct translation

Signed-off-by: Shyam Saini <mayhs11saini@gmail.com>
Reviewed-by: Shivani Bhardwaj <shivanib134@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoiptables-save: exit with error if unable to open proc file
Thomas Habets [Wed, 18 Jan 2017 13:58:13 +0000 (08:58 -0500)] 
iptables-save: exit with error if unable to open proc file

Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agoextensions: libxt_rpfilter: add translation to nft
Liping Zhang [Sat, 7 Jan 2017 14:26:46 +0000 (22:26 +0800)] 
extensions: libxt_rpfilter: add translation to nft

For example:
  # iptables-translate -t mangle -A PREROUTING -m rpfilter
  nft add rule ip mangle PREROUTING fib saddr . iif oif != 0 counter

  # iptables-translate -t mangle -A PREROUTING -m rpfilter --validmark \
  --loose
  nft add rule ip mangle PREROUTING fib saddr . mark oif != 0 counter

  # ip6tables-translate -t mangle -A PREROUTING -m rpfilter --validmark \
  --invert
  nft add rule ip6 mangle PREROUTING fib saddr . mark . iif oif 0 counter

Finally, when the "--accept-local" option is specified, we can combine
with "fib saddr type" to simulate it.

But when it is used like this: "-m rpfilter --accept-local", it means "||"
relationship, so we cannot translate it to one single nft rule,
translation is not supported yet:
  # iptables-translate -t mangle -A PREROUTING -m rpfilter --accept-local
  nft # -t mangle -A PREROUTING -m rpfilter --accept-local

When "--accpet-local" is combined with "--invert", it means "&&"
relationship, so translation can be:
  # iptables-translate -t mangle -A PREROUTING -m rpfilter \
  --accept-local --invert
  nft add rule ip mangle PREROUTING fib saddr type != local fib saddr \
  . iif oif 0 counter

Signed-off-by: Liping Zhang <zlpnobody@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoextensions: libxt_connbytes: Add translation to nft
Liping Zhang [Sun, 25 Dec 2016 12:27:51 +0000 (20:27 +0800)] 
extensions: libxt_connbytes: Add translation to nft

For example:
  # iptables-translate -A OUTPUT -m connbytes --connbytes 200 \
  --connbytes-dir original --connbytes-mode packets
  nft add rule ip filter OUTPUT ct original packets ge 200 counter

  # iptables-translate -A OUTPUT -m connbytes ! --connbytes 200 \
  --connbytes-dir reply --connbytes-mode packets
  nft add rule ip filter OUTPUT ct reply packets lt 200 counter

  # iptables-translate -A OUTPUT -m connbytes --connbytes 200:600 \
  --connbytes-dir both --connbytes-mode bytes
  nft add rule ip filter OUTPUT ct bytes 200-600 counter

  # iptables-translate -A OUTPUT -m connbytes ! --connbytes 200:600 \
  --connbytes-dir both --connbytes-mode bytes
  nft add rule ip filter OUTPUT ct bytes != 200-600 counter

  # iptables-translate -A OUTPUT -m connbytes --connbytes 200:200 \
  --connbytes-dir both --connbytes-mode avgpkt
  nft add rule ip filter OUTPUT ct avgpkt 200 counter

Signed-off-by: Liping Zhang <zlpnobody@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoiptables 1.6.1 release
Pablo Neira Ayuso [Mon, 16 Jan 2017 13:00:55 +0000 (14:00 +0100)] 
iptables 1.6.1 release

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agobuild: Fix two compile errors during out-of-tree build
Keno Fischer [Fri, 30 Dec 2016 05:43:37 +0000 (00:43 -0500)] 
build: Fix two compile errors during out-of-tree build

The first:
```
iptables/extensions/libebt_limit.c:21:26: fatal error: iptables/nft.h: No such file or directory
 #include "iptables/nft.h"
```

The second:
```
/data/keno/sandbox/iptables/iptables/xtables-config-parser.y:19:32: fatal error: libiptc/linux_list.h: No such file or directory
 #include <libiptc/linux_list.h>
                                ^
```

Simply fixed by adding the relevant `-I` directives.

Signed-off-by: Keno Fischer <keno@juliacomputing.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agolibxtables: xtables: Use getnameinfo()
Shyam Saini [Mon, 12 Dec 2016 14:53:57 +0000 (20:23 +0530)] 
libxtables: xtables: Use getnameinfo()

Replace gethostbyaddr() with getnameinfo() as getnameinfo()
deprecates the former and allows programs to
eliminate IPv4-versus-IPv6 dependencies

Signed-off-by: Shyam Saini <mayhs11saini@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agolibxtables: xtables: remove unnecessary debug code
Shyam Saini [Mon, 12 Dec 2016 14:53:56 +0000 (20:23 +0530)] 
libxtables: xtables: remove unnecessary debug code

Remove unnecessary debug code

Signed-off-by: Shyam Saini <mayhs11saini@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoiptables: on revision mismatch, do not call print/save
Willem de Bruijn [Thu, 8 Dec 2016 22:16:41 +0000 (17:16 -0500)] 
iptables: on revision mismatch, do not call print/save

Between revisions, the layout of xtables data may change completely.
Do not interpret the data in a revision M with a module of revision N.

Signed-off-by: Willem de Bruijn <willemb@google.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoextensions: libxt_bpf: support ebpf pinned objects
Willem de Bruijn [Thu, 8 Dec 2016 22:09:38 +0000 (17:09 -0500)] 
extensions: libxt_bpf: support ebpf pinned objects

Exercise the new kernel feature introduced in commit 2c16d6033264
("netfilter: xt_bpf: support ebpf") to load pinned eBPF programs.

The new interface allows instantiating a bpf match using

  -m bpf --object-pinned ${PATH}

where ${PATH} points to a node in a bpf virtual filesystem. See
also the revised man page.

Signed-off-by: Willem de Bruijn <willemb@google.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoextensions: LOG: add log flags translation to nft
Liping Zhang [Sun, 27 Nov 2016 12:08:29 +0000 (20:08 +0800)] 
extensions: LOG: add log flags translation to nft

For example:
 # iptables-translate -A OUTPUT -j LOG --log-uid
 nft add rule ip filter OUTPUT counter log flags skuid

 # iptables-translate -A OUTPUT -j LOG --log-tcp-sequence \
 --log-tcp-options
 nft add rule ip filter OUTPUT counter log flags tcp sequence,options

 # iptables-translate -A OUTPUT -j LOG --log-level debug --log-uid
 nft add rule ip filter OUTPUT counter log level debug flags skuid

 # ip6tables-translate -A OUTPUT -j LOG --log-ip-options --log-macdecode
 nft add rule ip6 filter OUTPUT counter log flags ip options flags ether

 # ip6tables-translate -A OUTPUT -j LOG --log-ip-options --log-uid \
 --log-tcp-sequence --log-tcp-options --log-macdecode
 nft add rule ip6 filter OUTPUT counter log flags all

Signed-off-by: Liping Zhang <zlpnobody@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotcp_xlate: Enclose LH flag values in parentheses
Phil Sutter [Tue, 29 Nov 2016 11:47:25 +0000 (12:47 +0100)] 
tcp_xlate: Enclose LH flag values in parentheses

This fixes TCP flags matches:

| $ iptables-translate -A invalid -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP
| nft add rule ip filter invalid tcp flags & fin|syn == fin|syn counter drop

Although the generated rule is syntactically correct and accepted by
nft, it will be interpreted in a different way than expected since
binary AND takes precedence over OR.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoxtables-translate: Fix chain type when translating nat table
Phil Sutter [Mon, 28 Nov 2016 12:14:16 +0000 (13:14 +0100)] 
xtables-translate: Fix chain type when translating nat table

This makes the type of translated chains in nat table to be of type
'nat' instead of 'filter' which is incorrect.

Verified like so:

| $ iptables-restore-translate -f /dev/stdin <<EOF
| *nat
| :POSTROUTING ACCEPT [0:0]
| [0:0] -A POSTROUTING -j MASQUERADE
| COMMIT
| EOF
| # Translated by ./install/sbin/iptables-restore-translate v1.6.0 on Mon Nov 28 12:11:30 2016
| add table ip nat
| add chain ip nat POSTROUTING { type nat hook postrouting priority 0; policy accept; }
| add rule ip nat POSTROUTING counter masquerade

Ditto for ip6tables-restore-translate.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agonft_ipv{4,6}_xlate: Respect prefix lengths
Phil Sutter [Fri, 25 Nov 2016 17:52:55 +0000 (18:52 +0100)] 
nft_ipv{4,6}_xlate: Respect prefix lengths

This was an annoying bug in the translator since it silently dropped
crucial information which is easily overlooked:

| $ iptables-translate -A INPUT -s 192.168.0.0/24 -j ACCEPT
| nft add rule ip filter INPUT ip saddr 192.168.0.0 counter accept
| $ ip6tables-translate -A INPUT -s feed:babe::/64 -j ACCEPT
| nft add rule ip6 filter INPUT ip6 saddr feed:babe:: counter accept

To my surprise, this fix works really well in all kinds of situations:

| $ iptables-translate -A INPUT -s 1.2.3.4/0 -j ACCEPT
| nft add rule ip filter INPUT counter accept
|
| $ iptables-translate -A INPUT -s 1.2.3.4/23 -j ACCEPT
| nft add rule ip filter INPUT ip saddr 1.2.2.0/23 counter accept
|
| $ iptables-translate -A INPUT -s 1.2.3.4/24 -j ACCEPT
| nft add rule ip filter INPUT ip saddr 1.2.3.0/24 counter accept
|
| $ iptables-translate -A INPUT -s 1.2.3.4/32 -j ACCEPT
| nft add rule ip filter INPUT ip saddr 1.2.3.4 counter accept
|
| $ iptables-translate -A INPUT -s 1.2.3.4/255.255.0.0 -j ACCEPT
| nft add rule ip filter INPUT ip saddr 1.2.0.0/16 counter accept

Ditto for IPv6.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoxtables-translate: Support setting standard chain policy
Phil Sutter [Fri, 25 Nov 2016 17:06:46 +0000 (18:06 +0100)] 
xtables-translate: Support setting standard chain policy

Looks like this bit was simply forgotten when implementing
xlate_chain_set() as everything needed was there to just print the
desired policy along with the chain definition.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoextensions: libip6t_ah: Fix translation of plain '-m ah'
Phil Sutter [Fri, 25 Nov 2016 16:32:39 +0000 (17:32 +0100)] 
extensions: libip6t_ah: Fix translation of plain '-m ah'

This is actually a limitation of ip6tables:

| # ip6tables -A INPUT -p ah -j ACCEPT
| Warning: never matched protocol: ah. use extension match instead.

The working alternative is like so:

| # ip6tables -A INPUT -m ah -j ACCEPT

But upon translating, this statement gets ignored:

| $ ip6tables-translate -A INPUT -m ah -j ACCEPT
| nft add rule ip6 filter INPUT  counter accept

This patch (ab)uses the 'space' variable to check if a parameter to the
'ah' match was present and if not translates the match into an extension
header check:

| $ ip6tables-translate -A INPUT -m ah -j ACCEPT
| add rule ip6 filter INPUT meta l4proto ah counter accept

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agolibxt_multiport: remove an unused variable
George Burgess IV [Wed, 23 Nov 2016 00:05:51 +0000 (16:05 -0800)] 
libxt_multiport: remove an unused variable

Clang's static analyzer flagged the shift this patch removes as
shifting a garbage value. Looks like `m` isn't used at all anyway, so
we can simply remove it.

Signed-off-by: George Burgess IV <gbiv@google.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agoconfigure: update libnetfilter_conntrack version dependency
Pablo Neira Ayuso [Thu, 10 Nov 2016 00:47:09 +0000 (01:47 +0100)] 
configure: update libnetfilter_conntrack version dependency

nfct_labels_get_path() requires libnetfilter_conntrack-1.0.6, update
this dependency.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoextensions: update Arturo Borrero email address
Arturo Borrero Gonzalez [Tue, 18 Oct 2016 12:12:57 +0000 (14:12 +0200)] 
extensions: update Arturo Borrero email address

The email address has changed, let's update it.

Signed-off-by: Arturo Borrero Gonzalez <arturo@debian.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoextensions: libxt_mangle: Use getaddrinfo()
Shivani Bhardwaj [Mon, 7 Nov 2016 12:45:17 +0000 (18:15 +0530)] 
extensions: libxt_mangle: Use getaddrinfo()

Replace gethostbyname() with getaddrinfo() as getaddrinfo()
deprecates the former and allows programs to eliminate
IPv4-versus-IPv6 dependencies.

Signed-off-by: Shivani Bhardwaj <shivanib134@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoiptables: xtables-arp: Use getaddrinfo()
Shivani Bhardwaj [Mon, 7 Nov 2016 12:28:46 +0000 (17:58 +0530)] 
iptables: xtables-arp: Use getaddrinfo()

Replace gethostbyname() with getaddrinfo() as getaddrinfo()
deprecates the former and allows programs to eliminate
IPv4-versus-IPv6 dependencies.

Signed-off-by: Shivani Bhardwaj <shivanib134@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoconnlabel: clarify default config path
Florian Westphal [Wed, 20 Jul 2016 09:53:20 +0000 (11:53 +0200)] 
connlabel: clarify default config path

Pablo suggested to print full config file path for connlabel.conf
parsing errors.

Suggested-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoextensions: libxt_statistic: add translation to nft
Liping Zhang [Fri, 7 Oct 2016 11:08:56 +0000 (19:08 +0800)] 
extensions: libxt_statistic: add translation to nft

For example:
  # iptables-translate -A OUTPUT -m statistic --mode nth --every 10 \
  --packet 1
  nft add rule ip filter OUTPUT numgen inc mod 10 1 counter

  # iptables-translate -A OUTPUT -m statistic --mode nth ! --every 10 \
  --packet 5
  nft add rule ip filter OUTPUT numgen inc mod 10 != 5 counter

Note, mode random is not completely supported in nft, so:
  # iptables-translate -A OUTPUT -m statistic --mode random \
  --probability 0.1
  nft # -A OUTPUT -m statistic --mode random --probability 0.1

Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoextensions: libxt_DSCP: add translation to nft
Liping Zhang [Fri, 7 Oct 2016 11:08:55 +0000 (19:08 +0800)] 
extensions: libxt_DSCP: add translation to nft

For example:
  # iptables-translate -A OUTPUT -j DSCP --set-dscp 1
  nft add rule ip filter OUTPUT counter ip dscp set 0x01

  # ip6tables-translate -A OUTPUT -j DSCP --set-dscp 6
  nft add rule ip6 filter OUTPUT counter ip6 dscp set 0x06

Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoextensions: libxt_quota: add translation to nft
Liping Zhang [Fri, 7 Oct 2016 11:08:54 +0000 (19:08 +0800)] 
extensions: libxt_quota: add translation to nft

For example:
  # iptables-translate -A OUTPUT -m quota --quota 111
  nft add rule ip filter OUTPUT quota 111 bytes counter

  # iptables-translate -A OUTPUT -m quota ! --quota 111
  nft add rule ip filter OUTPUT quota over 111 bytes counter

Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoextensions: libxt_ipcomp: add range support in translation
Liping Zhang [Fri, 7 Oct 2016 11:08:53 +0000 (19:08 +0800)] 
extensions: libxt_ipcomp: add range support in translation

When translate to nft rules, ipcompspi range is not supported, so:
  # iptables-translate -A OUTPUT -m ipcomp --ipcompspi 1:2
  nft add rule ip filter OUTPUT comp cpi 1 counter

  # iptables-translate -A OUTPUT -m ipcomp ! --ipcompspi 3:30
  nft add rule ip filter OUTPUT comp cpi != 3 counter

Apply this patch:
  # iptables-translate -A OUTPUT -m ipcomp --ipcompspi 1:2
  nft add rule ip filter OUTPUT comp cpi 1-2 counter

  # iptables-translate -A OUTPUT -m ipcomp ! --ipcompspi 3:30
  nft add rule ip filter OUTPUT comp cpi != 3-30 counter

Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoextensions: libxt_devgroup: handle the invert flag properly in translation
Liping Zhang [Fri, 7 Oct 2016 11:08:52 +0000 (19:08 +0800)] 
extensions: libxt_devgroup: handle the invert flag properly in translation

We forgot to put "!=" when devgroup can be mapped to name, so translation
is wrong:
  # iptables-translate -A OUTPUT -m devgroup ! --dst-group 0
  nft add rule ip filter OUTPUT oifgroup default counter

Apply this patch:
  # iptables-translate -A OUTPUT -m devgroup ! --dst-group 0
  nft add rule ip filter OUTPUT oifgroup != default counter

Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoextensions: libxt_iprange: handle the invert flag properly in translation
Liping Zhang [Fri, 7 Oct 2016 11:08:51 +0000 (19:08 +0800)] 
extensions: libxt_iprange: handle the invert flag properly in translation

If we specify the invert flag, we should put "!=" after "ip saddr/daddr",
so the current translation is wrong:
  # iptables-translate -A OUTPUT -m iprange ! --dst-range 1.1.1.1-1.1.1.2
  nft add rule ip filter OUTPUT != ip daddr 1.1.1.1-1.1.1.2 counter

  # ip6tables-translate -A OUTPUT -m iprange ! --src-range 2003::1-2003::3
  nft add rule ip6 filter OUTPUT != ip6 saddr 2003::1-2003::3 counter

Apply this patch:
  # iptables-translate -A OUTPUT -m iprange ! --dst-range 1.1.1.1-1.1.1.2
  nft add rule ip filter OUTPUT ip daddr != 1.1.1.1-1.1.1.2 counter

  # ip6tables-translate -A OUTPUT -m iprange ! --src-range 2003::1-2003::3
  nft add rule ip6 filter OUTPUT ip6 saddr != 2003::1-2003::3 counter

Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoextensions: libxt_iprange: rename "ip saddr" to "ip6 saddr" in ip6tables-xlate
Liping Zhang [Fri, 7 Oct 2016 11:08:50 +0000 (19:08 +0800)] 
extensions: libxt_iprange: rename "ip saddr" to "ip6 saddr" in ip6tables-xlate

nft will complain the syntax error if we use "ip saddr" or "ip daddr" in
ip6 family, so the current translation is wrong:
  # ip6tables-translate -A OUTPUT -m iprange --src-range 2003::1-2003::3
  nft add rule ip6 filter OUTPUT ip saddr 2003::1-2003::3 counter
                                 ^^

Apply this patch:
  # ip6tables-translate -A OUTPUT -m iprange --src-range 2003::1-2003::3
  nft add rule ip6 filter OUTPUT ip6 saddr 2003::1-2003::3 counter

Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoextensions: libipt_realm: add a missing space in translation
Liping Zhang [Fri, 7 Oct 2016 11:08:49 +0000 (19:08 +0800)] 
extensions: libipt_realm: add a missing space in translation

We missed a blank space when do translate to nft, so if rt_realm can be
mapped to name, the result looks ugly:
  # iptables-translate -A OUTPUT -m realm --realm 0
  nft add rule ip filter OUTPUT rtclassidcosmos counter
                                         ^

Apply this patch:
  # iptables-translate -A OUTPUT -m realm --realm 0
  nft add rule ip filter OUTPUT rtclassid cosmos counter

Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoextensions: libxt_hashlimit: Create revision 2 of xt_hashlimit to support higher...
Vishwanath Pai [Mon, 26 Sep 2016 19:08:52 +0000 (15:08 -0400)] 
extensions: libxt_hashlimit: Create revision 2 of xt_hashlimit to support higher pps rates

Create a new revision for the hashlimit iptables extension module. Rev 2
will support higher pps of upto 1 million, Version 1 supports only 10k.

To support this we have to increase the size of the variables avg and
burst in hashlimit_cfg to 64-bit. Create two new structs hashlimit_cfg2
and xt_hashlimit_mtinfo2 and also create newer versions of all the
functions for match, checkentry and destory.

Signed-off-by: Vishwanath Pai <vpai@akamai.com>
Signed-off-by: Joshua Hunt <johunt@akamai.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoextensions: libxt_hashlimit: Prepare libxt_hashlimit.c for revision 2
Vishwanath Pai [Mon, 26 Sep 2016 19:08:17 +0000 (15:08 -0400)] 
extensions: libxt_hashlimit: Prepare libxt_hashlimit.c for revision 2

I am planning to add a revision 2 for the hashlimit xtables module to
support higher packets per second rates. This patch renames all the
functions and variables related to revision 1 by adding _v1 at the
end of the names.

Signed-off-by: Vishwanath Pai <vpai@akamai.com>
Signed-off-by: Joshua Hunt <johunt@akamai.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoextensions: libip6t_SNAT/DNAT: add square bracket in xlat output when port is specified
Liping Zhang [Fri, 2 Sep 2016 12:47:05 +0000 (20:47 +0800)] 
extensions: libip6t_SNAT/DNAT: add square bracket in xlat output when port is specified

It is better to add square brackets to ip6 address in nft translation
output when the port is specified. This is keep consistent with the
nft syntax.

Before this patch:
  # ip6tables-translate -t nat -A OUTPUT -p tcp -j DNAT --to-destination \
  [123::4]:1
  nft add rule ip6 nat OUTPUT meta l4proto tcp counter dnat to 123::4 :1
  # ip6tables-translate -t nat -A POSTROUTING -p tcp -j SNAT --to-source \
  [123::4-123::8]:1
  nft add rule ip6 nat POSTROUTING meta l4proto tcp counter snat to 123::4-123::8 :1

Apply this patch:
  # ip6tables-translate -t nat -A OUTPUT -p tcp -j DNAT --to-destination \
  [123::4]:1
  nft add rule ip6 nat OUTPUT meta l4proto tcp counter dnat to [123::4]:1
  # ip6tables-translate -t nat -A POSTROUTING -p tcp -j SNAT --to-source \
  [123::4-123::8]:1
  nft add rule ip6 nat POSTROUTING meta l4proto tcp counter snat to [123::4]-[123::8]:1

Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoxtables-translate-restore: do not escape quotes
Pablo M. Bermudo Garay [Wed, 31 Aug 2016 07:59:16 +0000 (09:59 +0200)] 
xtables-translate-restore: do not escape quotes

If quotes are escaped, nft -f is unable to parse and load the translated
ruleset.

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoxtables-compat: add rule cache
Pablo M. Bermudo Garay [Fri, 26 Aug 2016 16:58:44 +0000 (18:58 +0200)] 
xtables-compat: add rule cache

This patch adds a cache of rules within the nft handle. This feature is
useful since the whole ruleset was brought from the kernel for every
chain during listing operations. In addition with the new checks of
ruleset compatibility, the rule list is loaded one more time.

Now all the operations causing changes in the ruleset must invalidate
the cache, a function called flush_rule_cache has been introduced for
this purpose.

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoextensions: libip[6]t_REDIRECT: use new nft syntax when do xlate
Liping Zhang [Sun, 28 Aug 2016 08:50:48 +0000 (16:50 +0800)] 
extensions: libip[6]t_REDIRECT: use new nft syntax when do xlate

After commit "parser_bison: redirect to :port for consistency with
nat/masq statement" in nftables tree, we should recommend the end
user to use the new syntax.

Before this patch:
  # iptables-translate -t nat -A PREROUTING -p tcp -j REDIRECT --to-ports 1
  nft add rule ip nat PREROUTING ip protocol tcp counter redirect to 1

Apply this patch:
  # iptables-translate -t nat -A PREROUTING -p tcp -j REDIRECT --to-ports 1
  nft add rule ip nat PREROUTING ip protocol tcp counter redirect to :1

Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoextensions: libip[6]t_SNAT/DNAT: use the new nft syntax when do xlate
Liping Zhang [Sun, 28 Aug 2016 08:50:47 +0000 (16:50 +0800)] 
extensions: libip[6]t_SNAT/DNAT: use the new nft syntax when do xlate

After commit "src: add 'to' for snat and dnat" in nftables tree,
we should recommend the end user to use the new syntax.

Before this patch:
  # iptables-translate -t nat -A POSTROUTING -j SNAT --to-source 1.1.1.1
  nft add rule ip nat POSTROUTING counter snat 1.1.1.1
  # ip6tables-translate -t nat -A PREROUTING -j DNAT --to-destination
  2001::1
  nft add rule ip6 nat PREROUTING counter dnat 2001::1

Apply this patch:
  # iptables-translate -t nat -A POSTROUTING -j SNAT --to-source 1.1.1.1
  nft add rule ip nat POSTROUTING counter snat to 1.1.1.1
  # ip6tables-translate -t nat -A PREROUTING -j DNAT --to-destination
  2001::1
  nft add rule ip6 nat PREROUTING counter dnat to 2001::1

Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoextensions: libipt_DNAT/SNAT: fix "OOM" when do translation to nft
Liping Zhang [Sun, 28 Aug 2016 08:50:46 +0000 (16:50 +0800)] 
extensions: libipt_DNAT/SNAT: fix "OOM" when do translation to nft

When I want to translate SNAT target to nft rule, an error message
was printed out:
  # iptables-translate -A POSTROUTING -j SNAT --to-source 1.1.1.1
  iptables-translate v1.6.0: OOM

Because ipt_natinfo{} started with a xt_entry_target{}, so when we
get the ipt_natinfo pointer, we should use the target itself,
not its data pointer. Yes, it is a little tricky and it's different
with other targets.

Fixes: 7a0992da44cf ("src: introduce struct xt_xlate_{mt,tg}_params")
Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoxtables-compat: check if nft ruleset is compatible
Pablo M. Bermudo Garay [Fri, 26 Aug 2016 16:58:43 +0000 (18:58 +0200)] 
xtables-compat: check if nft ruleset is compatible

This patch adds a verification of the compatibility between the nft
ruleset and iptables. Nft tables, chains and rules are checked to be
compatible with iptables. If something is not compatible, the execution
stops and an error message is displayed to the user.

This checking is triggered by xtables-compat -L and xtables-compat-save
commands.

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoiptables-restore: add missing arguments to usage message
Brian Haley [Tue, 23 Aug 2016 14:14:47 +0000 (10:14 -0400)] 
iptables-restore: add missing arguments to usage message

iptables-restore was missing -n, -T and -M from the
usage message, added them to match the man page.
Cleaned-up other *restore files as well.

Signed-off-by: Brian Haley <brian.haley@hpe.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoxtables-translate: add escape_quotes option to comment_xlate
Pablo M. Bermudo Garay [Mon, 22 Aug 2016 10:56:14 +0000 (12:56 +0200)] 
xtables-translate: add escape_quotes option to comment_xlate

The comment_xlate function was not supporting this option that is
necessary in some situations.

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoxtables-compat: remove useless functions
Pablo M. Bermudo Garay [Sun, 21 Aug 2016 18:10:25 +0000 (20:10 +0200)] 
xtables-compat: remove useless functions

The static function nft_rule_list_get was exposed outside nft.c through
the nft_rule_list_create function, but this was never used out there.

A similar situation occurs with nftnl_rule_list_free and
nft_rule_list_destroy.

This patch removes nft_rule_list_create and nft_rule_list_destroy for
the sake of simplicity.

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoextensions: libxt_CLASSIFY: Add translation to nft
Liping Zhang [Sun, 21 Aug 2016 14:34:55 +0000 (22:34 +0800)] 
extensions: libxt_CLASSIFY: Add translation to nft

For examples:
  # iptables-translate -A OUTPUT -j CLASSIFY --set-class 0:0
  nft add rule ip filter OUTPUT counter meta priority set none
  # iptables-translate -A OUTPUT -j CLASSIFY --set-class ffff:ffff
  nft add rule ip filter OUTPUT counter meta priority set root
  # iptables-translate -A OUTPUT -j CLASSIFY --set-class 1:234
  nft add rule ip filter OUTPUT counter meta priority set 1:234

Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoextensions/libxt_bpf.man: clarify BPF code generation with tcpdump
Willem de Bruijn [Wed, 10 Aug 2016 19:23:07 +0000 (15:23 -0400)] 
extensions/libxt_bpf.man: clarify BPF code generation with tcpdump

The xt_bpf module applies BPF bytecode to the packet. Depending on
where the module is invoked, the kernel may pass a packet with or
without link layer header. Iptables has no such header.

A common `tcpdump -ddd <string>` compilation command may revert to
a physical device that generates code for packets starting from the
mac layer up (e.g., E10MB data link type: Ethernet).

Clarify in the man page that when using this tool for code generation,
a suitable target device must be chosen.

Netfilter Bugzilla Bug #1048

Reported-by: Lorenzo Pistone <blaffablaffa@gmail.com>
Signed-off-by: Willem de Bruijn <willemb@google.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoxtables-compat: fix comments listing
Pablo M. Bermudo Garay [Tue, 2 Aug 2016 14:29:47 +0000 (16:29 +0200)] 
xtables-compat: fix comments listing

ip[6]tables-compat -L was not printing the comments since commit
d64ef34a9961 ("iptables-compat: use nft built-in comments support").

This patch solves the issue.

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoextensions: REJECT: do not adjust reject-with type footnote indentation
Sami Kerola [Tue, 26 Jul 2016 14:14:22 +0000 (15:14 +0100)] 
extensions: REJECT: do not adjust reject-with type footnote indentation

The footnote clarification to option argument documentation, so keep the
indentation level same as for the arguments.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoiptables-translate: add in/out ifname wildcard match translation to nft
Liping Zhang [Sat, 30 Jul 2016 05:20:59 +0000 (13:20 +0800)] 
iptables-translate: add in/out ifname wildcard match translation to nft

In iptables, "-i eth+" means match all in ifname with the prefix "eth".
But in nftables, this was changed to "iifname eth*". So we should handle
this subtle difference.

Apply this patch, translation will become:
  # iptables-translate -A INPUT -i eth+
  nft add rule ip filter INPUT iifname eth* counter
  # ip6tables-translate -A OUTPUT ! -o eth+
  nft add rule ip6 filter OUTPUT oifname != eth* counter

Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoxtables-translate: fix issue with quotes
Pablo M. Bermudo Garay [Tue, 26 Jul 2016 16:45:24 +0000 (18:45 +0200)] 
xtables-translate: fix issue with quotes

Some translations included escaped quotes when they were called from
nft:

$ sudo nft list ruleset
table ip mangle {
    chain FORWARD {
        type filter hook forward priority -150; policy accept;
        ct helper \"ftp\" counter packets 0 bytes 0
                  ^^   ^^
    }
}

This behavior is only correct when xlate functions are called from a
xtables-translate command. This patch solves that issue using a new
parameter (escape_quotes) in the xlate functions.

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoinclude: xtables: fix struct definitions grepability
Pablo M. Bermudo Garay [Tue, 26 Jul 2016 16:45:23 +0000 (18:45 +0200)] 
include: xtables: fix struct definitions grepability

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: introduce struct xt_xlate_{mt,tg}_params
Pablo Neira Ayuso [Sun, 24 Jul 2016 10:45:53 +0000 (12:45 +0200)] 
src: introduce struct xt_xlate_{mt,tg}_params

This structure is an extensible containers of parameters, so we don't
need to propagate interface updates in every extension file in case
we need to add new parameters in the future.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoextensions: libxt_connlabel: add unit test
Liping Zhang [Sat, 23 Jul 2016 07:11:39 +0000 (15:11 +0800)] 
extensions: libxt_connlabel: add unit test

Add some unit tests for connlabel match extension:
  # ./iptables-test.py extensions/libxt_connlabel.t
  extensions/libxt_connlabel.t: OK
  1 test files, 7 unit tests, 7 passed

Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoextensions: libxt_NFLOG: add unit test to cover nflog-size with zero
Liping Zhang [Wed, 20 Jul 2016 12:53:10 +0000 (20:53 +0800)] 
extensions: libxt_NFLOG: add unit test to cover nflog-size with zero

"--nflog-size 0" is valid and we must display it appropriately.

Suggested-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoextensions: libxt_NFLOG: translate to nft log snaplen if nflog-size is specified
Liping Zhang [Mon, 18 Jul 2016 14:14:28 +0000 (22:14 +0800)] 
extensions: libxt_NFLOG: translate to nft log snaplen if nflog-size is specified

The nflog-size was introduced by commit 7070b1f3c88a ("extensions:
libxt_NFLOG: nflog-range does not truncate packets"). Then make
the nflog-range become deprecated, because it has no effect from
the beginning.

So when we do translation, nft log snaplen is translated only if the
nflog-size is specified.

Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoextensions: libxt_NFLOG: display nflog-size even if it is zero
Liping Zhang [Mon, 18 Jul 2016 14:14:27 +0000 (22:14 +0800)] 
extensions: libxt_NFLOG: display nflog-size even if it is zero

The following iptables rules have the different semantics:
  # iptables -A INPUT -j NFLOG
  # iptables -A INPUT -j NFLOG --nflog-size 0

But they are all displayed as "-A INPUT -j NFLOG", so if
the user input the following commands, the original semantics
will be broken.
  # iptables-save | iptables-restore

Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoextensions: libxt_connlabel: Add translation to nft
Liping Zhang [Sat, 16 Jul 2016 10:42:24 +0000 (18:42 +0800)] 
extensions: libxt_connlabel: Add translation to nft

Add translation for connlabel to nftables.
For examples:

  # iptables-translate -A INPUT -m connlabel --label bit40
  nft add rule ip filter INPUT ct label bit40 counter

  # iptables-translate -A INPUT -m connlabel ! --label bit40 --set
  nft add rule ip filter INPUT ct label set bit40 ct label and bit40 != bit40 counter

Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
Acked-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoextensions: libxt_connlabel: fix crash when connlabel.conf is empty
Liping Zhang [Sat, 16 Jul 2016 11:39:53 +0000 (19:39 +0800)] 
extensions: libxt_connlabel: fix crash when connlabel.conf is empty

When connlabel.conf is empty, nfct_labelmap_new will return NULL and
set errno to 0. So we will miss to check this situation, and cause NULL
deference in nfct_labelmap_get_bit.

Input the following commands will reproduce this crash:
  # echo > /etc/xtables/connlabel.conf
  # iptables -A INPUT -m connlabel --label abc
  Segmentation fault (core dumped)

Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
9 years agoxtables-translate: fix multiple spaces issue
Pablo M. Bermudo Garay [Sat, 9 Jul 2016 10:27:51 +0000 (12:27 +0200)] 
xtables-translate: fix multiple spaces issue

This patch fixes a multiple spaces issue. The problem arises when a rule
set loaded through iptables-compat-restore is listed in nft.

Before this commit, two spaces were printed after every match
translation:

$ sudo iptables-save
*filter
:INPUT ACCEPT [0:0]
-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m multiport --dports 80:85 -m ttl --ttl-gt 5 -j ACCEPT
COMMIT

$ sudo iptables-compat-restore iptables-save

$ sudo nft list ruleset
table ip filter {
    chain INPUT {
        type filter hook input priority 0; policy accept;
        ct state related,established  counter packets 0 bytes 0 accept
                                    ^^
        ip protocol tcp tcp dport 80-85  ip ttl gt 5  counter packets 0 bytes 0 accept
                                       ^^           ^^
    }
}

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoxtables: Add an interval option for xtables lock wait
Subash Abhinov Kasiviswanathan [Fri, 24 Jun 2016 00:44:06 +0000 (18:44 -0600)] 
xtables: Add an interval option for xtables lock wait

ip[6]tables currently waits for 1 second for the xtables lock to be
freed if the -w option is used. We have seen that the lock is held
much less than that resulting in unnecessary delay when trying to
acquire the lock. This problem is even severe in case of latency
sensitive applications.

Introduce a new option 'W' to specify the wait interval in microseconds.
If this option is not specified, the command sleeps for 1 second by
default.

v1->v2: Change behavior to take millisecond sleep as an argument to
-w as suggested by Pablo. Also maintain current behavior for -w to
sleep for 1 second as mentioned by Liping.

v2->v3: Move the millisecond behavior to a new option as suggested
by Pablo.

v3->v4: Use select instead of usleep. Sleep every iteration for
the time specified in the "-W" argument. Update man page.

v4->v5: Fix compilation error when enabling nftables

v5->v6: Simplify -W so it only takes the interval wait in microseconds.
Bail out if -W is specific but -w is not.

Joint work with Pablo Neira.

Signed-off-by: Subash Abhinov Kasiviswanathan <subashab@codeaurora.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agolibxtables: Replace gethostbyname() with getaddrinfo()
Arpan Kapoor [Thu, 17 Mar 2016 12:57:19 +0000 (18:27 +0530)] 
libxtables: Replace gethostbyname() with getaddrinfo()

Make the function host_to_ipaddr() similar to host_to_ip6addr(),
using getaddrinfo() instead of the obsoleted gethostbyname().

Signed-off-by: Arpan Kapoor <rpnkpr@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoextensions: added AR substitution
Jordan Yelloz [Fri, 24 Jun 2016 19:18:45 +0000 (12:18 -0700)] 
extensions: added AR substitution

This is to ensure that the correct AR is run in cross-compile jobs.
Often a cross-compile build will succeed without this change but it
fails on my Gentoo Linux system when I have binutils installed with the
"multitarget" USE flag.
This change substitues AR with the autotools-supplied AR for the
extensions subdirectory.

Signed-off-by: Jordan Yelloz <jordan@yelloz.me>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoextensions: libxt_NFLOG: nflog-range does not truncate packets
Vishwanath Pai [Fri, 24 Jun 2016 20:42:31 +0000 (16:42 -0400)] 
extensions: libxt_NFLOG: nflog-range does not truncate packets

The option --nflog-range has never worked, but we cannot just fix this
because users might be using this feature option and their behavior would
change. Instead add a new option --nflog-size. This option works the same
way nflog-range should have, and both of them are mutually exclusive. When
someone uses --nflog-range we print a warning message informing them that
this feature has no effect.

To indicate the kernel that the user has set --nflog-size we have to pass a
new flag XT_NFLOG_F_COPY_LEN.

Also updated the man page to reflect the new option and added tests to
extensions/libxt_NFLOG.t

Reported-by: Joe Dollard <jdollard@akamai.com>
Reviewed-by: Josh Hunt <johunt@akamai.com>
Signed-off-by: Vishwanath Pai <vpai@akamai.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoextensions: libipt_realm: fix order of mask and id when do nft translation
Liping Zhang [Mon, 27 Jun 2016 13:57:25 +0000 (21:57 +0800)] 
extensions: libipt_realm: fix order of mask and id when do nft translation

Before:
  # iptables-translate -A INPUT -m realm --realm 1/0xf
  nft add rule ip filter INPUT rtclassid and 0x1 == 0xf counter

Apply this patch:
  # iptables-translate -A INPUT -m realm --realm 1/0xf
  nft add rule ip filter INPUT rtclassid and 0xf == 0x1 counter

Cc: Shivani Bhardwaj <shivanib134@gmail.com>
Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoiptables: extensions: libxt_ecn: Add translation to nft
Roberto García [Wed, 29 Jun 2016 18:48:09 +0000 (20:48 +0200)] 
iptables: extensions: libxt_ecn: Add translation to nft

Add translation of the ecn match to nftables.

Examples:
  # iptables-translate -A INPUT -m ecn --ecn-ip-ect 0
  nft add rule ip filter INPUT ip ecn not-ect counter

  # iptables-translate -A INPUT -m ecn --ecn-ip-ect 1
  nft add rule ip filter INPUT ip ecn ect1 counter

  # iptables-translate -A INPUT -m ecn --ecn-ip-ect 2
  nft add rule ip filter INPUT ip ecn ect0 counter

  # iptables-translate -A INPUT -m ecn --ecn-ip-ect 3
  nft add rule ip filter INPUT ip ecn ce counter

  # iptables-translate -A INPUT -m ecn ! --ecn-ip-ect 0
  nft add rule ip filter INPUT ip ecn != not-ect counter

  # iptables-translate -A INPUT -m ecn ! --ecn-ip-ect 1
  nft add rule ip filter INPUT ip ecn != ect1 counter

  # iptables-translate -A INPUT -m ecn ! --ecn-ip-ect 2
  nft add rule ip filter INPUT ip ecn != ect0 counter

  # iptables-translate -A INPUT -m ecn ! --ecn-ip-ect 3
  nft add rule ip filter INPUT ip ecn != ce counter

Signed-off-by: Roberto García <rodanber@gmail.com>
Reviewed-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoconfigure: Fix assignment statement
Shivani Bhardwaj [Wed, 22 Jun 2016 19:41:39 +0000 (01:11 +0530)] 
configure: Fix assignment statement

The assignment statement was interpreted as executing enable_connlabel
command with the argument "no". This was due to the whitespaces in the
assignment.

Fixes the trivial bug introduced in commit 3b7a227 (configure: Show
support for connlabel)

Signed-off-by: Shivani Bhardwaj <shivanib134@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
9 years agoiptables-compat: use nft built-in comments support
Pablo M. Bermudo Garay [Wed, 22 Jun 2016 17:07:01 +0000 (19:07 +0200)] 
iptables-compat: use nft built-in comments support

After this patch, iptables-compat uses nft built-in comments support
instead of comment match.

This change simplifies the treatment of comments in nft after load a
rule set through iptables-compat-restore.

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoextensions: libxt_conntrack: Add translation to nft
Laura Garcia Liebana [Mon, 20 Jun 2016 15:16:18 +0000 (17:16 +0200)] 
extensions: libxt_conntrack: Add translation to nft

Add translation of conntrack to nftables.

Examples:

$ sudo iptables-translate -t filter -A INPUT -m conntrack --ctstate NEW,RELATED -j ACCEPT
nft add rule ip filter INPUT ct state new,related counter accept

$ sudo ip6tables-translate -t filter -A INPUT -m conntrack ! --ctstate NEW,RELATED -j ACCEPT
nft add rule ip6 filter INPUT ct state != new,related counter accept

$ sudo iptables-translate -t filter -A INPUT -m conntrack --ctproto UDP -j ACCEPT
nft add rule ip filter INPUT ct proto 17 counter accept

$ sudo iptables-translate -t filter -A INPUT -m conntrack ! --ctproto UDP -j ACCEPT
nft add rule ip filter INPUT ct proto != 17 counter accept

$ sudo iptables-translate -t filter -A INPUT -m conntrack --ctorigsrc 10.100.2.131 -j ACCEPT
nft add rule ip filter INPUT ct original saddr 10.100.2.131 counter accept

$ sudo iptables-translate -t filter -A INPUT -m conntrack --ctorigsrc 10.100.0.0/255.255.0.0 -j ACCEPT
nft add rule ip filter INPUT ct original saddr 10.100.0.0/16 counter accept

$ sudo iptables-translate -t filter -A INPUT -m conntrack --ctorigdst 10.100.2.131 -j ACCEPT
nft add rule ip filter INPUT ct original daddr 10.100.2.131 counter accept

$ sudo iptables-translate -t filter -A INPUT -m conntrack --ctreplsrc 10.100.2.131 -j ACCEPT
nft add rule ip filter INPUT ct reply saddr 10.100.2.131 counter accept

$ sudo iptables-translate -t filter -A INPUT -m conntrack --ctrepldst 10.100.2.131 -j ACCEPT
nft add rule ip filter INPUT ct reply daddr 10.100.2.131 counter accept

$ sudo iptables-translate -t filter -A INPUT -m conntrack --ctproto tcp --ctorigsrcport 443:444 -j ACCEPT
nft add rule ip filter INPUT ct original protocol 6 ct original proto-src 443-444 counter accept

$ sudo iptables-translate -t filter -A INPUT -m conntrack ! --ctstatus CONFIRMED -j ACCEPT
nft add rule ip filter INPUT ct status != confirmed counter accept

$ sudo iptables-translate -t filter -A INPUT -m conntrack --ctexpire 3 -j ACCEPT
nft add rule ip filter INPUT ct expiration 3 counter accept

$ sudo iptables-translate -t filter -A INPUT -m conntrack --ctdir ORIGINAL -j ACCEPT
nft add rule ip filter INPUT ct direction original counter accept

Signed-off-by: Laura Garcia Liebana <nevola@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoextensions: iprange: remove extra space in translation
Pablo M. Bermudo Garay [Mon, 20 Jun 2016 10:24:56 +0000 (12:24 +0200)] 
extensions: iprange: remove extra space in translation

Extra space was printed by iprange_xlate:

  # iptables-translate -A INPUT -m iprange --src-range \
    192.168.25.149-192.168.25.151 -j ACCEPT

  nft add rule ip filter INPUT  ip saddr 192.168.25.149-192.168.25...
                              ^^

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoiptables: extensions: libxt_MARK: Fix translation of --set-xmark option
Roberto García [Wed, 22 Jun 2016 12:31:31 +0000 (14:31 +0200)] 
iptables: extensions: libxt_MARK: Fix translation of --set-xmark option

Fix translation of MARK target's --set-xmark option.

Before:
  #iptables-translate -t mangle -A PREROUTING -j MARK --set-xmark 0x64/0xaf
  nft add rule ip mangle PREROUTING counter meta mark set mark xor 0x64 and 0xaf

After:

  # iptables-translate -t mangle -A PREROUTING -j MARK --set-xmark 0x64/0xaf
  nft add rule ip mangle PREROUTING counter meta mark set mark and 0xffffff50 \
  xor 0x64

Signed-off-by: Roberto García <rodanber@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoextensions: libxt_CONNMARK: Add translation to nft
Roberto García [Fri, 17 Jun 2016 16:10:50 +0000 (18:10 +0200)] 
extensions: libxt_CONNMARK: Add translation to nft

Add translation for the CONNMARK target to nftables.

The following options have no available translation:

  --save-mark [--nfmask nfmask] [--ctmask ctmask]
  --restore-mark [--nfmask nfmask] [--ctmask ctmask]

Examples:

  # iptables-translate -t mangle -A PREROUTING -j CONNMARK --set-mark 0x16
  nft add rule ip mangle PREROUTING counter ct mark set 0x16

  # iptables-translate -t mangle -A PREROUTING -j CONNMARK --set-xmark 0x16/0x12
  nft add rule ip mangle PREROUTING counter ct mark set ct mark xor 0x16 and
  0xffffffed

  # iptables-translate -t mangle -A PREROUTING -j CONNMARK --and-mark 0x16
  nft add rule ip mangle PREROUTING counter ct mark set ct mark and 0x16

  # iptables-translate -t mangle -A PREROUTING -j CONNMARK --or-mark 0x16
  nft add rule ip mangle PREROUTING counter ct mark set ct mark or 0x16

  # iptables-translate -t mangle -A PREROUTING -j CONNMARK --save-mark
  nft add rule ip mangle PREROUTING counter ct mark set mark

  # iptables-translate -t mangle -A PREROUTING -j CONNMARK --save-mark \
    --mask 0x12
  nft add rule ip mangle PREROUTING counter ct mark set mark and 0x12

  # iptables-translate -t mangle -A PREROUTING -j CONNMARK --restore-mark
  nft add rule ip mangle PREROUTING counter meta mark set ct mark

  # iptables-translate -t mangle -A PREROUTING -j CONNMARK --restore-mark \
    --mask 0x12
  nft add rule ip mangle PREROUTING counter meta mark set ct mark and 0x12

Signed-off-by: Roberto García <rodanber@gmail.com>
Acked-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoextensions: libxt_MARK: Add translation for revision 1 to nft
Roberto García [Tue, 21 Jun 2016 20:08:24 +0000 (22:08 +0200)] 
extensions: libxt_MARK: Add translation for revision 1 to nft

Add translation for revision 1 of the MARK target to nft.

Examples:

  # iptables-translate -t mangle -A PREROUTING -j MARK --set-mark 0x64
  nft add rule ip mangle PREROUTING counter meta mark set 0x64

  # iptables-translate -t mangle -A PREROUTING -j MARK --and-mark 0x64
  nft add rule ip mangle PREROUTING counter meta mark set mark and 0x64

  # iptables-translate -t mangle -A PREROUTING -j MARK --or-mark 0x64
  nft add rule ip mangle PREROUTING counter meta mark set mark or 0x64

Signed-off-by: Roberto García <rodanber@gmail.com>
Acked-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>