extensions: libxt_multiport: Add translation to nft
PATCH v2 1/1 extensions: libxt_multiport: Add translation to nft
Add translation for multiport to nftables, which it's supported natively.
Examples:
$ sudo iptables-translate -t filter -A INPUT -p tcp -m multiport --dports 80,81 -j ACCEPT
nft add rule ip filter INPUT ip protocol tcp tcp dport { 80,81} counter accept$ sudo iptables-translate -t filter -A INPUT -p tcp -m multiport --dports 80:88 -j ACCEPT
nft add rule ip filter INPUT ip protocol tcp tcp dport { 80-88} counter accept$ sudo iptables-translate -t filter -A INPUT -p tcp -m multiport ! --dports 80:88 -j ACCEPT nft add rule ip filter INPUT ip protocol tcp tcp dport != 80-88 counter accept
---
Changes in v2:
- Add curley brackets to lists and range of ports.
extensions/libxt_multiport.c | 116 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+)
extensions/libxt_multiport.c +116 -0
@@ -468,6 +468,118 @@ static void multiport_save6_v1(const void *ip_void,__multiport_save_v1 (match, ip->proto); } +static int __multiport_xlate (const void *ip,const struct xt_entry_match *match, +struct xt_xlate *xl,int numeric) +{ +const struct xt_multiport *multiinfo + = (const struct xt_multiport *)match->data; +unsigned int i; + +switch (multiinfo->flags) { +case XT_MULTIPORT_SOURCE : +xt_xlate_add (xl,"sport " ); +break ; +case XT_MULTIPORT_DESTINATION : +xt_xlate_add (xl,"dport " ); +break ; +case XT_MULTIPORT_EITHER : +return 0 ; + } + +if (multiinfo->count >1 ) +xt_xlate_add (xl,"{ " ); + +for (i =0 ; i < multiinfo->count; i++) +xt_xlate_add (xl,"%s%u" , i ?"," :"" , multiinfo->ports[i]); + +if (multiinfo->count >1 ) +xt_xlate_add (xl,"}" ); + +xt_xlate_add (xl," " ); + +return 1 ; +} + +static int multiport_xlate (const void *ip,const struct xt_entry_match *match, +struct xt_xlate *xl,int numeric) +{ +uint8_t proto = ((const struct ipt_ip *)ip)->proto; + +xt_xlate_add (xl,"%s " ,proto_to_name (proto)); +return __multiport_xlate (ip, match, xl, numeric); +} + +static int multiport_xlate6 (const void *ip,const struct xt_entry_match *match, +struct xt_xlate *xl,int numeric) +{ +uint8_t proto = ((const struct ip6t_ip6 *)ip)->proto; + +xt_xlate_add (xl,"%s " ,proto_to_name (proto)); +return __multiport_xlate (ip, match, xl, numeric); +} + +static int __multiport_xlate_v1 (const void *ip, +const struct xt_entry_match *match, +struct xt_xlate *xl,int numeric) +{ +const struct xt_multiport_v1 *multiinfo + = (const struct xt_multiport_v1 *)match->data; +unsigned int i; + +switch (multiinfo->flags) { +case XT_MULTIPORT_SOURCE : +xt_xlate_add (xl,"sport " ); +break ; +case XT_MULTIPORT_DESTINATION : +xt_xlate_add (xl,"dport " ); +break ; +case XT_MULTIPORT_EITHER : +return 0 ; + } + +if (multiinfo->invert) { +xt_xlate_add (xl,"!= " ); + }else { +if (multiinfo->count >1 ) +xt_xlate_add (xl,"{ " ); + } + +for (i =0 ; i < multiinfo->count; i++) { +xt_xlate_add (xl,"%s%u" , i ?"," :"" , multiinfo->ports[i]); +if (i && multiinfo->invert) +return 0 ; +if (multiinfo->pflags[i]) +xt_xlate_add (xl,"-%u" , multiinfo->ports[++i]); + } + +if (multiinfo->count >1 && !multiinfo->invert) +xt_xlate_add (xl,"}" ); + +xt_xlate_add (xl," " ); + +return 1 ; +} + +static int multiport_xlate_v1 (const void *ip, +const struct xt_entry_match *match, +struct xt_xlate *xl,int numeric) +{ +uint8_t proto = ((const struct ipt_ip *)ip)->proto; + +xt_xlate_add (xl,"%s " ,proto_to_name (proto)); +return __multiport_xlate_v1 (ip, match, xl, numeric); +} + +static int multiport_xlate6_v1 (const void *ip, +const struct xt_entry_match *match, +struct xt_xlate *xl,int numeric) +{ +uint8_t proto = ((const struct ip6t_ip6 *)ip)->proto; + +xt_xlate_add (xl,"%s " ,proto_to_name (proto)); +return __multiport_xlate_v1 (ip, match, xl, numeric); +} +static struct xtables_match multiport_mt_reg[] = { { .family =NFPROTO_IPV4 ,
@@ -482,6 +594,7 @@ static struct xtables_match multiport_mt_reg[] = { .print = multiport_print, .save = multiport_save, .x6_options = multiport_opts, + .xlate = multiport_xlate, }, { .family = NFPROTO_IPV6,
@@ -496,6 +609,7 @@ static struct xtables_match multiport_mt_reg[] = { .print = multiport_print6, .save = multiport_save6, .x6_options = multiport_opts, + .xlate = multiport_xlate6, }, { .family = NFPROTO_IPV4,
@@ -510,6 +624,7 @@ static struct xtables_match multiport_mt_reg[] = { .print = multiport_print_v1, .save = multiport_save_v1, .x6_options = multiport_opts, + .xlate = multiport_xlate_v1, }, { .family = NFPROTO_IPV6,
@@ -524,6 +639,7 @@ static struct xtables_match multiport_mt_reg[] = { .print = multiport_print6_v1, .save = multiport_save6_v1, .x6_options = multiport_opts, + .xlate = multiport_xlate6_v1, }, };
Signature · 1 line
2.7.0
Quoted 4 lines · Laura Garcia Liebana
Add translation for multiport to nftables, which it's supported natively.
Examples:
Quoted 9 lines
$ sudo iptables-translate -t filter -A INPUT -p tcp -m multiport --dports 80,81 -j ACCEPT nft add rule ip filter INPUT ip protocol tcp tcp dport { 80,81} counter accept$ sudo iptables-translate -t filter -A INPUT -p tcp -m multiport --dports 80:88 -j ACCEPT nft add rule ip filter INPUT ip protocol tcp tcp dport { 80-88} counter accept$ sudo iptables-translate -t filter -A INPUT -p tcp -m multiport ! --dports 80:88 -j ACCEPT nft add rule ip filter INPUT ip protocol tcp tcp dport != 80-88 counter accept
Lets clarify the syntax, this is valid:
tcp dport 8000-8100
tcp dport { 8000-8100}
tcp dport { 8000-8100, 9000-9100}
but they mean different things. It seems we should avoid the braces {} for the range case, otherwise we would be using a set with a single element.
However,
tcp dport {8000,8100} <-- valid
tcp dport 8000,8100 <-- invalid
So we should always use braces {} in the non-range case.
Same seems to apply in the case of inversion.
so we end with this combinations:
tcp dport {x,x}
tcp dport != {x,x}
tcp dport x-x
tcp dport != x-x
BTW, related to this, there seems to be a bug in nftables:
% nft add rule t c tcp dport != {80, 81}
BUG: invalid expression type set
nft: evaluate.c:1463: expr_evaluate_relational: Assertion `0' failed. Aborted
Quoted 23 lines
Signed-off-by Laura Garcia Liebana <[email protected]>---
Changes in v2:
- Add curley brackets to lists and range of ports.extensions/libxt_multiport.c | 116 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+)extensions/libxt_multiport.c +7
-0@@ -468,6 +468,118 @@ static void multiport_save6_v1(const void *ip_void,__multiport_save_v1 (match, ip->proto); } +static int __multiport_xlate (const void *ip,const struct xt_entry_match *match, +struct xt_xlate *xl,int numeric) +{ +const struct xt_multiport *multiinfo + = (const struct xt_multiport *)match->data; +unsigned int i; +
extensions/libxt_multiport.c static void multiport_save6_v1(const void *ip_void, · line 487
Quoted 10 lines
extensions/libxt_multiport.c +10
-0+switch (multiinfo->flags) { +case XT_MULTIPORT_SOURCE : +xt_xlate_add (xl,"sport " ); +break ; +case XT_MULTIPORT_DESTINATION : +xt_xlate_add (xl,"dport " ); +break ; +case XT_MULTIPORT_EITHER : +return 0 ; + }
this case XT_MULTIPORT_EITHER seems unsupported in nftables.
Is there anything established to do in case we find an impossible translation? print a warning or something? I don't know right now. I guess we should avoid printing an invalid nftables rule as if the translation was 100% ok (which is not true in this case).
Just wondering, I should check myself because I don't know this right now.
Quoted 41 lines
extensions/libxt_multiport.c +41
-0+ +if (multiinfo->count >1 ) +xt_xlate_add (xl,"{ " ); + +for (i =0 ; i < multiinfo->count; i++) +xt_xlate_add (xl,"%s%u" , i ?"," :"" , multiinfo->ports[i]); + +if (multiinfo->count >1 ) +xt_xlate_add (xl,"}" ); + +xt_xlate_add (xl," " ); + +return 1 ; +} + +static int multiport_xlate (const void *ip,const struct xt_entry_match *match, +struct xt_xlate *xl,int numeric) +{ +uint8_t proto = ((const struct ipt_ip *)ip)->proto; + +xt_xlate_add (xl,"%s " ,proto_to_name (proto)); +return __multiport_xlate (ip, match, xl, numeric); +} + +static int multiport_xlate6 (const void *ip,const struct xt_entry_match *match, +struct xt_xlate *xl,int numeric) +{ +uint8_t proto = ((const struct ip6t_ip6 *)ip)->proto; + +xt_xlate_add (xl,"%s " ,proto_to_name (proto)); +return __multiport_xlate (ip, match, xl, numeric); +} + +static int __multiport_xlate_v1 (const void *ip, +const struct xt_entry_match *match, +struct xt_xlate *xl,int numeric) +{ +const struct xt_multiport_v1 *multiinfo + = (const struct xt_multiport_v1 *)match->data; +unsigned int i; +
extensions/libxt_multiport.c static void multiport_save6_v1(const void *ip_void, · line 538
Quoted 10 lines
extensions/libxt_multiport.c +10
-0+switch (multiinfo->flags) { +case XT_MULTIPORT_SOURCE : +xt_xlate_add (xl,"sport " ); +break ; +case XT_MULTIPORT_DESTINATION : +xt_xlate_add (xl,"dport " ); +break ; +case XT_MULTIPORT_EITHER : +return 0 ; + }
same XT_MULTIPORT_EITHER here.
extensions/libxt_multiport.c static void multiport_save6_v1(const void *ip_void, · line 545
Quoted 7 lines
extensions/libxt_multiport.c +7
-0+ +if (multiinfo->invert) { +xt_xlate_add (xl,"!= " ); + }else { +if (multiinfo->count >1 ) +xt_xlate_add (xl,"{ " ); + }
This if/else seems bogus. We only allow port sets if not inverting?
extensions/libxt_multiport.c static void multiport_save6_v1(const void *ip_void, · line 550
Quoted 5 lines
extensions/libxt_multiport.c +5
-0+ +for (i =0 ; i < multiinfo->count; i++) { +xt_xlate_add (xl,"%s%u" , i ?"," :"" , multiinfo->ports[i]); +if (i && multiinfo->invert) +return 0 ;
This return here could mean that we build an incomplete nftables rule (ie, missing '}')
Quoted 11 lines
extensions/libxt_multiport.c +11
-0+if (multiinfo->pflags[i]) +xt_xlate_add (xl,"-%u" , multiinfo->ports[++i]); + } + +if (multiinfo->count >1 && !multiinfo->invert) +xt_xlate_add (xl,"}" ); + +xt_xlate_add (xl," " ); + +return 1 ; +}
Signature · 5 lines
Arturo Borrero González -- To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Pablo Neira Ayuso
maintainerQuoted 41 lines · Arturo Borrero Gonzalez
Quoted 4 lines · Laura Garcia Liebana
Add translation for multiport to nftables, which it's supported natively.
Examples:
Quoted 9 lines
$ sudo iptables-translate -t filter -A INPUT -p tcp -m multiport --dports 80,81 -j ACCEPT nft add rule ip filter INPUT ip protocol tcp tcp dport { 80,81} counter accept$ sudo iptables-translate -t filter -A INPUT -p tcp -m multiport --dports 80:88 -j ACCEPT nft add rule ip filter INPUT ip protocol tcp tcp dport { 80-88} counter accept$ sudo iptables-translate -t filter -A INPUT -p tcp -m multiport ! --dports 80:88 -j ACCEPT nft add rule ip filter INPUT ip protocol tcp tcp dport != 80-88 counter acceptLets clarify the syntax, this is valid:
tcp dport 8000-8100
tcp dport { 8000-8100}
tcp dport { 8000-8100, 9000-9100}but they mean different things. It seems we should avoid the braces {} for the range case, otherwise we would be using a set with a single element.
However,
tcp dport {8000,8100} <-- valid
tcp dport 8000,8100 <-- invalidSo we should always use braces {} in the non-range case.
Same seems to apply in the case of inversion.
so we end with this combinations:
tcp dport {x,x}
tcp dport != {x,x}
tcp dport x-x
tcp dport != x-x
Quoted 6 lines
BTW, related to this, there seems to be a bug in nftables:
% nft add rule t c tcp dport != {80, 81}
BUG: invalid expression type setnft: evaluate.c:1463:
expr_evaluate_relational: Assertion `0' failed. Aborted
This is not yet supported. This requires a small kernel patch to allow inversions in the nft_lookup.c. Then, the little extra code for libnftnl and nft.
All tests for this usecase are disabled at the moment.
Quoted 17 lines · Arturo Borrero Gonzalez
Quoted 4 lines · Laura Garcia Liebana
Add translation for multiport to nftables, which it's supported natively.
Examples:
Quoted 9 lines
$ sudo iptables-translate -t filter -A INPUT -p tcp -m multiport --dports 80,81 -j ACCEPT nft add rule ip filter INPUT ip protocol tcp tcp dport { 80,81} counter accept$ sudo iptables-translate -t filter -A INPUT -p tcp -m multiport --dports 80:88 -j ACCEPT nft add rule ip filter INPUT ip protocol tcp tcp dport { 80-88} counter accept$ sudo iptables-translate -t filter -A INPUT -p tcp -m multiport ! --dports 80:88 -j ACCEPT nft add rule ip filter INPUT ip protocol tcp tcp dport != 80-88 counter acceptLets clarify the syntax, this is valid:
Quoted 8 lines
tcp dport 8000-8100
tcp dport { 8000-8100}
tcp dport { 8000-8100, 9000-9100}but they mean different things. It seems we should avoid the braces {} for the range case, otherwise we would be using a set with a single element.
Yes, you're right. I'll change it in order to allow port ranges without {}.
Quoted 5 lines
However,
tcp dport {8000,8100} <-- valid
tcp dport 8000,8100 <-- invalid
Quoted 8 lines
So we should always use braces {} in the non-range case.
Same seems to apply in the case of inversion.
so we end with this combinations:
tcp dport {x,x}
tcp dport != {x,x}
This is not supported.
Quoted 47 lines
tcp dport x-x
tcp dport != x-xBTW, related to this, there seems to be a bug in nftables:
% nft add rule t c tcp dport != {80, 81}
BUG: invalid expression type setnft: evaluate.c:1463:
expr_evaluate_relational: Assertion `0' failed. AbortedQuoted 23 lines
Signed-off-by Laura Garcia Liebana <[email protected]>---
Changes in v2:
- Add curley brackets to lists and range of ports.extensions/libxt_multiport.c | 116 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+)extensions/libxt_multiport.c +7
-0@@ -468,6 +468,118 @@ static void multiport_save6_v1(const void *ip_void,__multiport_save_v1 (match, ip->proto); } +static int __multiport_xlate (const void *ip,const struct xt_entry_match *match, +struct xt_xlate *xl,int numeric) +{ +const struct xt_multiport *multiinfo + = (const struct xt_multiport *)match->data; +unsigned int i; +extensions/libxt_multiport.c static void multiport_save6_v1(const void *ip_void, · line 487
Quoted 10 lines
extensions/libxt_multiport.c +10
-0+switch (multiinfo->flags) { +case XT_MULTIPORT_SOURCE : +xt_xlate_add (xl,"sport " ); +break ; +case XT_MULTIPORT_DESTINATION : +xt_xlate_add (xl,"dport " ); +break ; +case XT_MULTIPORT_EITHER : +return 0 ; + }this case
XT_MULTIPORT_EITHERseems unsupported in nftables.
Quoted 5 lines
Is there anything established to do in case we find an impossible translation? print a warning or something? I don't know right now. I guess we should avoid printing an invalid nftables rule as if the translation was 100% ok (which is not true in this case).
It was agreed to return 0 if the translation is not supported for nftables. Currently, it does.
Quoted 48 lines
Just wondering, I should check myself because I don't know this right now.
Quoted 45 lines
extensions/libxt_multiport.c +45
-0+ +if (multiinfo->count >1 ) +xt_xlate_add (xl,"{ " ); + +for (i =0 ; i < multiinfo->count; i++) +xt_xlate_add (xl,"%s%u" , i ?"," :"" , multiinfo->ports[i]); + +if (multiinfo->count >1 ) +xt_xlate_add (xl,"}" ); + +xt_xlate_add (xl," " ); + +return 1 ; +} + +static int multiport_xlate (const void *ip,const struct xt_entry_match *match, +struct xt_xlate *xl,int numeric) +{ +uint8_t proto = ((const struct ipt_ip *)ip)->proto; + +xt_xlate_add (xl,"%s " ,proto_to_name (proto)); +return __multiport_xlate (ip, match, xl, numeric); +} + +static int multiport_xlate6 (const void *ip,const struct xt_entry_match *match, +struct xt_xlate *xl,int numeric) +{ +uint8_t proto = ((const struct ip6t_ip6 *)ip)->proto; + +xt_xlate_add (xl,"%s " ,proto_to_name (proto)); +return __multiport_xlate (ip, match, xl, numeric); +} + +static int __multiport_xlate_v1 (const void *ip, +const struct xt_entry_match *match, +struct xt_xlate *xl,int numeric) +{ +const struct xt_multiport_v1 *multiinfo + = (const struct xt_multiport_v1 *)match->data; +unsigned int i; + +switch (multiinfo->flags) { +case XT_MULTIPORT_SOURCE : +xt_xlate_add (xl,"sport " ); +break ;
Quoted 9 lines
extensions/libxt_multiport.c static void multiport_save6_v1(const void *ip_void, · line 487
Quoted 6 lines
extensions/libxt_multiport.c +6
-0+case XT_MULTIPORT_DESTINATION : +xt_xlate_add (xl,"dport " ); +break ; +case XT_MULTIPORT_EITHER : +return 0 ; + }same
XT_MULTIPORT_EITHERhere.
Same as before.
Quoted 10 lines
extensions/libxt_multiport.c static void multiport_save6_v1(const void *ip_void, · line 545
Quoted 7 lines
extensions/libxt_multiport.c +7
-0+ +if (multiinfo->invert) { +xt_xlate_add (xl,"!= " ); + }else { +if (multiinfo->count >1 ) +xt_xlate_add (xl,"{ " ); + }This if/else seems bogus. We only allow port sets if not inverting?
This should be now changed as we've to accept port ranges without {}.
Quoted 9 lines
extensions/libxt_multiport.c static void multiport_save6_v1(const void *ip_void, · line 550
Quoted 5 lines
extensions/libxt_multiport.c +5
-0+ +for (i =0 ; i < multiinfo->count; i++) { +xt_xlate_add (xl,"%s%u" , i ?"," :"" , multiinfo->ports[i]); +if (i && multiinfo->invert) +return 0 ;This return here could mean that we build an incomplete nftables rule (ie, missing '}')
Such condition cares that there is no translation in nftables for:
tcp dport != { 80,88 }
So the closed } doesn't even matter.
Quoted 4 lines
extensions/libxt_multiport.c static void multiport_save6_v1(const void *ip_void, · line 554
Quoted 4 lines
extensions/libxt_multiport.c +4
-0+if (multiinfo->pflags[i]) +xt_xlate_add (xl,"-%u" , multiinfo->ports[++i]); + } +
If the rule needs such closed } , then it's controlled here:
Quoted 11 lines
Quoted 7 lines
extensions/libxt_multiport.c +7
-0+if (multiinfo->count >1 && !multiinfo->invert) +xt_xlate_add (xl,"}" ); + +xt_xlate_add (xl," " ); + +return 1 ; +}Signature · 1 line
Arturo Borrero González
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 10 lines · Laura Garcia
Quoted 9 lines
extensions/libxt_multiport.c static void multiport_save6_v1(const void *ip_void, · line 550
Quoted 5 lines
extensions/libxt_multiport.c +5
-0+ +for (i =0 ; i < multiinfo->count; i++) { +xt_xlate_add (xl,"%s%u" , i ?"," :"" , multiinfo->ports[i]); +if (i && multiinfo->invert) +return 0 ;This return here could mean that we build an incomplete nftables rule (ie, missing '}')
Quoted 6 lines
Such condition cares that there is no translation in nftables for:
tcp dport != { 80,88 }
So the closed } doesn't even matter.
Then, at the start of the xlate function, I would suggest to do an early return if we are about to translate an unsupported rule.
No need to keep checking if we are in the inversion case
Quoted 3 lines
Quoted 3 lines
extensions/libxt_multiport.c static void multiport_save6_v1(const void *ip_void, · line 557
Quoted 3 lines
extensions/libxt_multiport.c +3
-0+if (multiinfo->count >1 && !multiinfo->invert) +xt_xlate_add (xl,"}" ); +
like here
Signature · 5 lines
Arturo Borrero González -- To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 5 lines · Pablo Neira Ayuso
This is not yet supported. This requires a small kernel patch to allow inversions in the
nft_lookup.c. Then, the little extra code for libnftnl and nft.All tests for this usecase are disabled at the moment.
What is your idea of the implementation?
Perhaps adding NFTA_LOOKUP_FLAGS and also:
enum nft_lookup_flags {
NFT_LOOKUP_F_INV = (1 << 0),
};
Signature · 5 lines
Arturo Borrero González -- To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Pablo Neira Ayuso
maintainerQuoted 5 lines · Arturo Borrero Gonzalez
Quoted 4 lines · Pablo Neira Ayuso
This is not yet supported. This requires a small kernel patch to allow inversions in the
nft_lookup.c. Then, the little extra code for libnftnl and nft.
Quoted 8 lines
Quoted 1 line
All tests for this usecase are disabled at the moment.
What is your idea of the implementation?
Perhaps adding
NFTA_LOOKUP_FLAGSand also:enum nft_lookup_flags { NFT_LOOKUP_F_INV = (1 << 0), };
Something like that, yes.