netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3] extensions: libxt_statistic: Add translation to nft
@ 2016-03-01 20:40 Laura Garcia Liebana
  2016-03-02 11:46 ` Pablo Neira Ayuso
  0 siblings, 1 reply; 14+ messages in thread
From: Laura Garcia Liebana @ 2016-03-01 20:40 UTC (permalink / raw)
  To: netfilter-devel; +Cc: shivanib134, fw, pablo, outreachy-kernel

Add translation for random mode to nftables. The nth mode is not
supported yet.

Examples:

$ iptables-translate -A INPUT -m statistic --mode random --probability
0.1 -j ACCEPT
nft add rule ip filter INPUT meta random 0.10000000009 counter accept

$ iptables-translate -A INPUT -m statistic --mode random ! --probability
0.1 -j ACCEPT
nft add rule ip filter INPUT meta random != 0.10000000009 counter accept

The .xlate indirection returns 0 if the translation is not available.

Signed-off-by: Laura Garcia Liebana <nevola@gmail.com>
---
Changes in v2:
	- Return 0 if the translation is not supported, as Pablo suggested.
	- Include not supported modes in the commit message, as Shivani suggested.
Changes in v3:
	- Fix wrong email format.

 extensions/libxt_statistic.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/extensions/libxt_statistic.c b/extensions/libxt_statistic.c
index b6ae5f5..c771363 100644
--- a/extensions/libxt_statistic.c
+++ b/extensions/libxt_statistic.c
@@ -133,6 +133,22 @@ static void statistic_save(const void *ip, const struct xt_entry_match *match)
 	print_match(info, "--");
 }
 
+static int statistic_xlate(const struct xt_entry_match *match,
+			   struct xt_xlate *xl, int numeric)
+{
+	const struct xt_statistic_info *info = (void *)match->data;
+
+	if (info->mode == XT_STATISTIC_MODE_RANDOM) {
+		xt_xlate_add(xl, "meta random%s %.11f ",
+			     (info->flags & XT_STATISTIC_INVERT) ? " !=" : "",
+			     1.0 * info->u.random.probability / 0x80000000);
+	} else {
+		return 0;
+	}
+
+	return 1;
+}
+
 static struct xtables_match statistic_match = {
 	.family		= NFPROTO_UNSPEC,
 	.name		= "statistic",
@@ -145,6 +161,7 @@ static struct xtables_match statistic_match = {
 	.print		= statistic_print,
 	.save		= statistic_save,
 	.x6_options	= statistic_opts,
+	.xlate		= statistic_xlate,
 };
 
 void _init(void)
-- 
2.7.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2016-03-02 15:57 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-03-01 20:40 [PATCH v3] extensions: libxt_statistic: Add translation to nft Laura Garcia Liebana
2016-03-02 11:46 ` Pablo Neira Ayuso
2016-03-02 12:10   ` Florian Westphal
2016-03-02 12:33     ` Pablo Neira Ayuso
2016-03-02 12:37       ` Florian Westphal
2016-03-02 12:46         ` Pablo Neira Ayuso
2016-03-02 13:44           ` Florian Westphal
2016-03-02 13:52     ` Jan Engelhardt
2016-03-02 14:50       ` Florian Westphal
2016-03-02 14:54         ` Jan Engelhardt
2016-03-02 14:59           ` Florian Westphal
2016-03-02 15:17         ` Pablo Neira Ayuso
2016-03-02 15:29           ` Florian Westphal
2016-03-02 15:56             ` Pablo Neira Ayuso

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).