extensions: connlabel: Allow connlabel.conf override
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): [email protected]
Pablo Neira Ayuso
maintainerHi Phil,
Quoted 22 lines · Phil Sutter
Check for environment variable
XT_CONNLABEL_CFGand if set use its value as path to connlabel.conf.Signed-off-by Phil Sutter <[email protected]>--- extensions/libxt_connlabel.c | 6 ++++-- extensions/libxt_connlabel.man | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-)extensions/libxt_connlabel.c +1
-0@@ -3,6 +3,7 @@#include <string.h> #include <stdio.h> #include <stdint.h> +#include <stdlib.h> #include <xtables.h> #include <linux/netfilter/xt_connlabel.h> #include <libnetfilter_conntrack/libnetfilter_conntrack.h>
extensions/libxt_connlabel.c static void connlabel_open(void) · line 43
Quoted 5 lines
extensions/libxt_connlabel.c +1
-1@@ -39,11 +40,12 @@ static void connlabel_open(void)if (map)return ; - map =nfct_labelmap_new (NULL ); + fname =getenv ("XT_CONNLABEL_CFG" ) ?:nfct_labels_get_path ();
Only one question about this one: Would you need anything similar for nft? If so, probably it's better to place this code in libnetfilter_conntrack.
Phil Sutter
reviewerHi Pablo,
Quoted 28 lines · Pablo Neira Ayuso
Hi Phil,
Quoted 25 lines · Phil Sutter
Check for environment variable
XT_CONNLABEL_CFGand if set use its value as path to connlabel.conf.Signed-off-by Phil Sutter <[email protected]>--- extensions/libxt_connlabel.c | 6 ++++-- extensions/libxt_connlabel.man | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-)extensions/libxt_connlabel.c +1
-0@@ -3,6 +3,7 @@#include <string.h> #include <stdio.h> #include <stdint.h> +#include <stdlib.h> #include <xtables.h> #include <linux/netfilter/xt_connlabel.h> #include <libnetfilter_conntrack/libnetfilter_conntrack.h> @@ -39,11 +40,12 @@ static void connlabel_open(void)if (map)return ;
Quoted 6 lines
extensions/libxt_connlabel.c static void connlabel_open(void) · line 43
Quoted 2 lines
extensions/libxt_connlabel.c +1
-1- map =nfct_labelmap_new (NULL ); + fname =getenv ("XT_CONNLABEL_CFG" ) ?:nfct_labels_get_path ();Only one question about this one: Would you need anything similar for nft? If so, probably it's better to place this code in
libnetfilter_conntrack.
I guess not: In nftables, /etc/connlabel.conf is manually parsed via call to rt_symbol_table_init(). So while I could add the above to libnetfilter_conntrack, it wouldn't affect nftables.
Cheers, Phil
A message that is not in the archive (sent off-list or not yet received): [email protected]
Florian Westphal
maintainerRe: [iptables PATCH 5/5] iptables-test: Make use of sample connlabel.conf
Phil Sutter <[email protected]> wrote:
Sorry for being late.
Quoted 6 lines
+@cp -f extensions/libxt_connlabel.conf.test extensions/libxt_connlabel.conf.tmp
-m connlabel --label "bit40";=;OK
-m connlabel ! --label "bit40";=;OK
-m connlabel --label "bit41" --set;=;OK
-m connlabel ! --label "bit41" --set;=;OK
-m connlabel --label "bit128";;FAIL
Maybe we should forget about the label names and just tests -m connlabel --label 127
i.e., parse the numeric value instead of providing a fake one. I agree that temporary replace of hosts one is bad.
Phil Sutter
reviewerRe: [iptables PATCH 5/5] iptables-test: Make use of sample connlabel.conf
Hi,
Quoted 3 lines · Florian Westphal
Phil Sutter <[email protected]> wrote:
Sorry for being late.
No worries, it is not urgent.
Quoted 7 lines
Quoted 6 lines
+@cp -f extensions/libxt_connlabel.conf.test extensions/libxt_connlabel.conf.tmp
-m connlabel --label "bit40";=;OK
-m connlabel ! --label "bit40";=;OK
-m connlabel --label "bit41" --set;=;OK
-m connlabel ! --label "bit41" --set;=;OK
-m connlabel --label "bit128";;FAIL
Quoted 5 lines
Maybe we should forget about the label names and just tests -m connlabel --label 127
i.e., parse the numeric value instead of providing a fake one. I agree that temporary replace of hosts one is bad.
Fine with me as well. Obviously this would reduce code coverage of tests, although not much since libnetfilter_conntrack is used for label map lookup.
Cheers, Phil
Pablo Neira Ayuso
maintainerRe: [iptables PATCH 5/5] iptables-test: Make use of sample connlabel.conf
Quoted 19 lines · Phil Sutter
Hi,
Quoted 3 lines · Florian Westphal
Phil Sutter <[email protected]> wrote:
Sorry for being late.
No worries, it is not urgent.
Quoted 10 lines
Quoted 6 lines
+@cp -f extensions/libxt_connlabel.conf.test extensions/libxt_connlabel.conf.tmp
-m connlabel --label "bit40";=;OK
-m connlabel ! --label "bit40";=;OK
-m connlabel --label "bit41" --set;=;OK
-m connlabel ! --label "bit41" --set;=;OK
-m connlabel --label "bit128";;FAILMaybe we should forget about the label names and just tests -m connlabel --label 127
Quoted 6 lines
Quoted 2 lines
i.e., parse the numeric value instead of providing a fake one. I agree that temporary replace of hosts one is bad.
Fine with me as well. Obviously this would reduce code coverage of tests, although not much since
libnetfilter_conntrackis used for label map lookup.
We can probably place some mapping lookup tests for this in libnetfilter_conntrack.
Phil Sutter
reviewerRe: [iptables PATCH 5/5] iptables-test: Make use of sample connlabel.conf
Hi,
Quoted 20 lines · Pablo Neira Ayuso
Quoted 19 lines · Phil Sutter
Hi,
Quoted 3 lines · Florian Westphal
Phil Sutter <[email protected]> wrote:
Sorry for being late.
No worries, it is not urgent.
Quoted 10 lines
Quoted 6 lines
+@cp -f extensions/libxt_connlabel.conf.test extensions/libxt_connlabel.conf.tmp
-m connlabel --label "bit40";=;OK
-m connlabel ! --label "bit40";=;OK
-m connlabel --label "bit41" --set;=;OK
-m connlabel ! --label "bit41" --set;=;OK
-m connlabel --label "bit128";;FAILMaybe we should forget about the label names and just tests -m connlabel --label 127
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines
i.e., parse the numeric value instead of providing a fake one. I agree that temporary replace of hosts one is bad.
Fine with me as well. Obviously this would reduce code coverage of tests, although not much since
libnetfilter_conntrackis used for label map lookup.
Argh. So I started with simply dropping all the connlabel.conf mangling in libxt_connlabel.t along with replacing the names by values. Turns out the extension exits if file wasn't found, no big deal changing that. Doing so I discovered that parsing bit values is done by nfct_labelmap_get_bit() as well but only if library initialization has succeeded. Fine, manual parsing as a fallback it is. Checking libnetfilter_conntrack once again to be sure, I noticed that it doesn't accept bit values unless they appear in connlabel.conf. Now I start changing functional behaviour and dropping label name test becomes a larger change than supporting connlabel.conf in non-standard path. /o\
Quoted 2 lines
We can probably place some mapping lookup tests for this in
libnetfilter_conntrack.
I just found the ominous "qa" directory in there, so I guess we're already fine in that regard. :)
Cheers, Phil
Florian Westphal
maintainerRe: [iptables PATCH 5/5] iptables-test: Make use of sample connlabel.conf
Quoted 4 lines · Phil Sutter
libnetfilter_conntrackonce again to be sure, I noticed that it doesn't accept bit values unless they appear in connlabel.conf. Now I start changing functional behaviour and dropping label name test becomes a larger change than supporting connlabel.conf in non-standard path. /o\
I think it would make sense to accept raw numbers as well as a fallback. We accept it from nftables, and IIRC the extension will print the raw hex value if it can't map it back to a name on -save.
Phil Sutter
reviewerPATCH 1/1 extensions: connlabel: Fallback on missing connlabel.conf
If connlabel.conf was not found, fall back to manually parsing arguments as plain numbers.
If nfct_labelmap_new() has failed, nfct_labelmap_get_name() segfaults. Therefore make sure it is not called in connlabel_get_name() if that's the case.
---
This patch supersedes patches 3-5 of this series.
--- extensions/libxt_connlabel.c | 49 ++++++++++++++++++++++++------- extensions/libxt_connlabel.t | 23 ++++----------- extensions/libxt_connlabel.txlate | 8 ++--- 3 files changed, 48 insertions(+), 32 deletions(-)
extensions/libxt_connlabel.c +38 -11
@@ -1,8 +1,10 @@ +#define _GNU_SOURCE#include <errno.h> #include <stdbool.h> #include <string.h> #include <stdio.h> #include <stdint.h> +#include <stdlib.h> #include <xtables.h> #include <linux/netfilter/xt_connlabel.h> #include <libnetfilter_conntrack/libnetfilter_conntrack.h>
@@ -32,40 +34,59 @@ static const struct xt_option_entry connlabel_mt_opts[] = {/* cannot do this via _init, else static builds might spew error message * for every iptables invocation. */ -static void connlabel_open (void ) +static int connlabel_open (void ) {const char *fname;if (map) -return ; +return 0 ; map =nfct_labelmap_new (NULL );if (map !=NULL ) -return ; +return 0 ; fname =nfct_labels_get_path ();if (errno) { -xtables_error (RESOURCE_PROBLEM , -"cannot open %s: %s" , fname,strerror (errno)); +fprintf (stderr,"Warning: cannot open %s: %s\n" , + fname,strerror (errno)); }else {xtables_error (RESOURCE_PROBLEM ,"cannot parse %s: no labels found" , fname); } +return 1 ; +} + +static int connlabel_value_parse (const char *in) +{ +char *end; +unsigned long value =strtoul (in, &end,0 ); + +if (in[0 ] =='\0' || *end !='\0' ) +return -1 ; + +return value; }static void connlabel_mt_parse (struct xt_option_call *cb) {struct xt_connlabel_mtinfo *info = cb->data; +bool have_labelmap = !connlabel_open ();int tmp; -connlabel_open ();xtables_option_parse (cb);switch (cb->entry->id) {case O_LABEL : - tmp =nfct_labelmap_get_bit (map, cb->arg); +if (have_labelmap) + tmp =nfct_labelmap_get_bit (map, cb->arg); +else + tmp =connlabel_value_parse (cb->arg); +if (tmp <0 ) -xtables_error (PARAMETER_PROBLEM ,"label '%s' not found" , cb->arg); +xtables_error (PARAMETER_PROBLEM , +"label '%s' not found or invalid value" , + cb->arg); + info->bit = tmp;if (cb->invert) info->options |=XT_CONNLABEL_OP_INVERT ;
@@ -81,7 +102,8 @@ static const char *connlabel_get_name(int b) {const char *name; -connlabel_open (); +if (connlabel_open ()) +return NULL ; name =nfct_labelmap_get_name (map, b);if (name &&strcmp (name,"" ))
@@ -134,9 +156,13 @@ static int connlabel_mt_xlate(struct xt_xlate *xl,const struct xt_connlabel_mtinfo *info = (const void *)params->match->data;const char *name =connlabel_get_name (info->bit); +char *valbuf =NULL ; -if (name ==NULL ) -return 0 ; +if (name ==NULL ) { +if (asprintf (&valbuf,"%u" , info->bit) <0 ) +return 0 ; + name = valbuf; + }if (info->options &XT_CONNLABEL_OP_SET )xt_xlate_add (xl,"ct label set %s " , name);
@@ -146,6 +172,7 @@ static int connlabel_mt_xlate(struct xt_xlate *xl,xt_xlate_add (xl,"and %s != " , name);xt_xlate_add (xl,"%s" , name); +free (valbuf);return 1 ; }
extensions/libxt_connlabel.t +6 -17
@@ -1,18 +1,7 @@ :INPUT,FORWARD,OUTPUT -# Backup the connlabel.conf, then add some label maps for test -@[ -f /etc/xtables/connlabel.conf ] && mv /etc/xtables/connlabel.conf /tmp/connlabel.conf.bak -@mkdir -p /etc/xtables -@echo "40 bit40" > /etc/xtables/connlabel.conf -@echo "41 bit41" >> /etc/xtables/connlabel.conf -@echo "128 bit128" >> /etc/xtables/connlabel.conf --m connlabel --label "bit40";=;OK --m connlabel ! --label "bit40";=;OK --m connlabel --label "bit41" --set;=;OK --m connlabel ! --label "bit41" --set;=;OK --m connlabel --label "bit128";;FAIL -@echo > /etc/xtables/connlabel.conf --m connlabel --label "abc";;FAIL -@rm -f /etc/xtables/connlabel.conf --m connlabel --label "abc";;FAIL -# Restore the original connlabel.conf -@[ -f /tmp/connlabel.conf.bak ] && mv /tmp/connlabel.conf.bak /etc/xtables/connlabel.conf +-m connlabel --label "40";=;OK +-m connlabel ! --label "40";=;OK +-m connlabel --label "41" --set;=;OK +-m connlabel ! --label "41" --set;=;OK +-m connlabel --label "2048";;FAIL +-m connlabel --label "foobar_not_there";;FAIL
extensions/libxt_connlabel.txlate +4 -4
@@ -1,5 +1,5 @@ -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 40 +nft add rule ip filter INPUT ct label 40 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 +iptables-translate -A INPUT -m connlabel ! --label 40 --set +nft add rule ip filter INPUT ct label set 40 ct label and 40 != 40 counter
Signature · 1 line
2.20.1
Re: [iptables PATCH] extensions: connlabel: Fallback on missing connlabel.conf
Quoted 10 lines · Phil Sutter
If connlabel.conf was not found, fall back to manually parsing arguments as plain numbers.
If
nfct_labelmap_new()has failed,nfct_labelmap_get_name()segfaults. Therefore make sure it is not called inconnlabel_get_name()if that's the case.Signed-off-by Phil Sutter <[email protected]>---
This patch supersedes patches 3-5 of this series.
Applied, thanks for updating this.
Pablo Neira Ayuso
maintainerRe: [iptables PATCH 5/5] iptables-test: Make use of sample connlabel.conf
In reply to Phil SutterQuoted 2 lines · Phil Sutter
Quoted 1 line · Pablo Neira Ayuso
On Mon, Mar 04, 2019 at 01:43:11PM +0100, Phil Sutter wrote:
[...]
Quoted 5 lines
Quoted 2 lines
We can probably place some mapping lookup tests for this in
libnetfilter_conntrack.I just found the ominous "qa" directory in there, so I guess we're already fine in that regard. :)
We could rename this to a more orthodox tests/ directory :-)
Phil Sutter
reviewerPATCH 1/1 Rename 'qa' directory to 'tests'
When searching for library tests, 'qa' is easily overlooked. Use a more common name instead.
---
Makefile.am | 2 +-
{qa => tests}/.gitignore | 0
{qa => tests}/Makefile.am | 0
{qa => tests}/ct_echo_event.c | 0
{qa => tests}/ct_echo_event.sh | 0
{qa => tests}/ct_events_reliable.c | 0
{qa => tests}/ct_mark_filter.c | 0
{qa => tests}/ct_mark_filter.sh | 0
{qa => tests}/ct_stress.c | 0
{qa => tests}/inetd.conf | 0
{qa => tests}/nssocket.c | 0
{qa => tests}/nssocket.h | 0
{qa => tests}/nssocket_env.sh | 0
{qa => tests}/qa-connlabel.conf | 0
{qa => tests}/test_api.c | 0
{qa => tests}/test_connlabel.c | 2 +-
{qa => tests}/test_filter.c | 0
17 files changed, 2 insertions(+), 2 deletions(-)
rename {qa => tests}/.gitignore (100%)
rename {qa => tests}/Makefile.am (100%)
rename {qa => tests}/ct_echo_event.c (100%)
rename {qa => tests}/ct_echo_event.sh (100%)
rename {qa => tests}/ct_events_reliable.c (100%)
rename {qa => tests}/ct_mark_filter.c (100%)
rename {qa => tests}/ct_mark_filter.sh (100%)
rename {qa => tests}/ct_stress.c (100%)
rename {qa => tests}/inetd.conf (100%)
rename {qa => tests}/nssocket.c (100%)
rename {qa => tests}/nssocket.h (100%)
rename {qa => tests}/nssocket_env.sh (100%)
rename {qa => tests}/qa-connlabel.conf (100%)
rename {qa => tests}/test_api.c (100%)
rename {qa => tests}/test_connlabel.c (96%)
rename {qa => tests}/test_filter.c (100%)Makefile.am +1 -1
@@ -2,7 +2,7 @@ include $(top_srcdir)/Make_global.am ACLOCAL_AMFLAGS = -I m4 -SUBDIRS = include src utils examples qa +SUBDIRS = include src utils examples tests man_MANS = #nfnetlink_conntrack.3 nfnetlink_conntrack.7
tests/.gitignore renamed from qa/.gitignore +0 -0
tests/Makefile.am renamed from qa/Makefile.am +0 -0
tests/ct_echo_event.c renamed from qa/ct_echo_event.c +0 -0
tests/ct_echo_event.sh renamed from qa/ct_echo_event.sh +0 -0
tests/ct_events_reliable.c renamed from qa/ct_events_reliable.c +0 -0
tests/ct_mark_filter.c renamed from qa/ct_mark_filter.c +0 -0
tests/ct_mark_filter.sh renamed from qa/ct_mark_filter.sh +0 -0
tests/ct_stress.c renamed from qa/ct_stress.c +0 -0
tests/inetd.conf renamed from qa/inetd.conf +0 -0
tests/nssocket.c renamed from qa/nssocket.c +0 -0
tests/nssocket.h renamed from qa/nssocket.h +0 -0
tests/nssocket_env.sh renamed from qa/nssocket_env.sh +0 -0
tests/qa-connlabel.conf renamed from qa/qa-connlabel.conf +0 -0
tests/test_api.c renamed from qa/test_api.c +0 -0
tests/test_connlabel.c renamed from qa/test_connlabel.c +1 -1
@@ -59,7 +59,7 @@ int main(void) l =nfct_labelmap_new ("qa-connlabel.conf" );if (!l) - l =nfct_labelmap_new ("qa/qa-connlabel.conf" ); + l =nfct_labelmap_new ("tests/qa-connlabel.conf" );assert (l);puts ("qa-connlabel.conf:" );dump_map (l);
tests/test_filter.c renamed from qa/test_filter.c +0 -0
Signature · 1 line
2.21.0
Phil Sutter
reviewerPATCH v2 1/1 Rename 'qa' directory to 'tests'
When searching for library tests, 'qa' is easily overlooked. Use a more common name instead.
---
Changes since v1:
- Add needed changes to configure.ac missed in previous patch.
---
Makefile.am | 2 +-
configure.ac | 4 ++--
{qa => tests}/.gitignore | 0
{qa => tests}/Makefile.am | 0
{qa => tests}/ct_echo_event.c | 0
{qa => tests}/ct_echo_event.sh | 0
{qa => tests}/ct_events_reliable.c | 0
{qa => tests}/ct_mark_filter.c | 0
{qa => tests}/ct_mark_filter.sh | 0
{qa => tests}/ct_stress.c | 0
{qa => tests}/inetd.conf | 0
{qa => tests}/nssocket.c | 0
{qa => tests}/nssocket.h | 0
{qa => tests}/nssocket_env.sh | 0
{qa => tests}/qa-connlabel.conf | 0
{qa => tests}/test_api.c | 0
{qa => tests}/test_connlabel.c | 2 +-
{qa => tests}/test_filter.c | 0
18 files changed, 4 insertions(+), 4 deletions(-)
rename {qa => tests}/.gitignore (100%)
rename {qa => tests}/Makefile.am (100%)
rename {qa => tests}/ct_echo_event.c (100%)
rename {qa => tests}/ct_echo_event.sh (100%)
rename {qa => tests}/ct_events_reliable.c (100%)
rename {qa => tests}/ct_mark_filter.c (100%)
rename {qa => tests}/ct_mark_filter.sh (100%)
rename {qa => tests}/ct_stress.c (100%)
rename {qa => tests}/inetd.conf (100%)
rename {qa => tests}/nssocket.c (100%)
rename {qa => tests}/nssocket.h (100%)
rename {qa => tests}/nssocket_env.sh (100%)
rename {qa => tests}/qa-connlabel.conf (100%)
rename {qa => tests}/test_api.c (100%)
rename {qa => tests}/test_connlabel.c (96%)
rename {qa => tests}/test_filter.c (100%)Makefile.am +1 -1
@@ -2,7 +2,7 @@ include $(top_srcdir)/Make_global.am ACLOCAL_AMFLAGS = -I m4 -SUBDIRS = include src utils examples qa +SUBDIRS = include src utils examples tests man_MANS = #nfnetlink_conntrack.3 nfnetlink_conntrack.7
configure.ac +2 -2
@@ -31,7 +31,7 @@ PKG_CHECK_MODULES([LIBMNL], [libmnl >= 1.0.3]) AC_CHECK_HEADERS(arpa/inet.h) dnl Check for inet_ntop AC_CHECK_FUNCS(inet_ntop) -dnl Check for setns used in qa +dnl Check for setns used in tests AC_CHECK_FUNCS(setns) dnl Again, some systems have it, but not IPv6 if test "$ac_cv_func_inet_ntop" = "yes" ; then
@@ -66,7 +66,7 @@ fi dnl Output the makefile AC_CONFIG_FILES([Makefile src/Makefile include/Makefile utils/Makefile - examples/Makefile qa/Makefile include/libnetfilter_conntrack/Makefile + examples/Makefile tests/Makefile include/libnetfilter_conntrack/Makefile include/internal/Makefile src/conntrack/Makefile src/expect/Makefile libnetfilter_conntrack.pc doxygen.cfg]) AC_OUTPUT
tests/.gitignore renamed from qa/.gitignore +0 -0
tests/Makefile.am renamed from qa/Makefile.am +0 -0
tests/ct_echo_event.c renamed from qa/ct_echo_event.c +0 -0
tests/ct_echo_event.sh renamed from qa/ct_echo_event.sh +0 -0
tests/ct_events_reliable.c renamed from qa/ct_events_reliable.c +0 -0
tests/ct_mark_filter.c renamed from qa/ct_mark_filter.c +0 -0
tests/ct_mark_filter.sh renamed from qa/ct_mark_filter.sh +0 -0
tests/ct_stress.c renamed from qa/ct_stress.c +0 -0
tests/inetd.conf renamed from qa/inetd.conf +0 -0
tests/nssocket.c renamed from qa/nssocket.c +0 -0
tests/nssocket.h renamed from qa/nssocket.h +0 -0
tests/nssocket_env.sh renamed from qa/nssocket_env.sh +0 -0
tests/qa-connlabel.conf renamed from qa/qa-connlabel.conf +0 -0
tests/test_api.c renamed from qa/test_api.c +0 -0
tests/test_connlabel.c renamed from qa/test_connlabel.c +1 -1
@@ -59,7 +59,7 @@ int main(void) l =nfct_labelmap_new ("qa-connlabel.conf" );if (!l) - l =nfct_labelmap_new ("qa/qa-connlabel.conf" ); + l =nfct_labelmap_new ("tests/qa-connlabel.conf" );assert (l);puts ("qa-connlabel.conf:" );dump_map (l);
tests/test_filter.c renamed from qa/test_filter.c +0 -0
Signature · 1 line
2.21.0
Re: [libnetfilter_conntrack PATCH v2] Rename 'qa' directory to 'tests'
Quoted 2 lines · Phil Sutter
When searching for library tests, 'qa' is easily overlooked. Use a more common name instead.
Applied, thanks Phil!