26b15f
From 6465867eb48506687872b838b1ddfee61d1a0aeb Mon Sep 17 00:00:00 2001
26b15f
From: Daniel Borkmann <dborkman@redhat.com>
26b15f
Date: Mon, 23 Dec 2013 18:46:29 +0100
26b15f
Subject: iptables: add libxt_cgroup frontend
26b15f
26b15f
This patch adds the user space extension/frontend for process matching
26b15f
based on cgroups from the kernel patch entitled "netfilter: xtables:
26b15f
lightweight process control group matching".
26b15f
26b15f
Signed-off-by: Daniel Borkmann <dborkman@redhat.com>
26b15f
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
26b15f
26b15f
diff --git a/extensions/libxt_cgroup.c b/extensions/libxt_cgroup.c
26b15f
new file mode 100644
26b15f
index 0000000..e304e33
26b15f
--- /dev/null
26b15f
+++ b/extensions/libxt_cgroup.c
26b15f
@@ -0,0 +1,67 @@
26b15f
+#include <stdio.h>
26b15f
+#include <xtables.h>
26b15f
+#include <linux/netfilter/xt_cgroup.h>
26b15f
+
26b15f
+enum {
26b15f
+	O_CGROUP = 0,
26b15f
+};
26b15f
+
26b15f
+static void cgroup_help(void)
26b15f
+{
26b15f
+	printf(
26b15f
+"cgroup match options:\n"
26b15f
+"[!] --cgroup fwid  Match cgroup fwid\n");
26b15f
+}
26b15f
+
26b15f
+static const struct xt_option_entry cgroup_opts[] = {
26b15f
+	{
26b15f
+		.name = "cgroup",
26b15f
+		.id = O_CGROUP,
26b15f
+		.type = XTTYPE_UINT32,
26b15f
+		.flags = XTOPT_INVERT | XTOPT_MAND | XTOPT_PUT,
26b15f
+		XTOPT_POINTER(struct xt_cgroup_info, id)
26b15f
+	},
26b15f
+	XTOPT_TABLEEND,
26b15f
+};
26b15f
+
26b15f
+static void cgroup_parse(struct xt_option_call *cb)
26b15f
+{
26b15f
+	struct xt_cgroup_info *cgroupinfo = cb->data;
26b15f
+
26b15f
+	xtables_option_parse(cb);
26b15f
+	if (cb->invert)
26b15f
+		cgroupinfo->invert = true;
26b15f
+}
26b15f
+
26b15f
+static void
26b15f
+cgroup_print(const void *ip, const struct xt_entry_match *match, int numeric)
26b15f
+{
26b15f
+	const struct xt_cgroup_info *info = (void *) match->data;
26b15f
+
26b15f
+	printf(" cgroup %s%u", info->invert ? "! ":"", info->id);
26b15f
+}
26b15f
+
26b15f
+static void cgroup_save(const void *ip, const struct xt_entry_match *match)
26b15f
+{
26b15f
+	const struct xt_cgroup_info *info = (void *) match->data;
26b15f
+
26b15f
+	printf("%s --cgroup %u", info->invert ? " !" : "", info->id);
26b15f
+}
26b15f
+
26b15f
+static struct xtables_match cgroup_match = {
26b15f
+	.family		= NFPROTO_UNSPEC,
26b15f
+	.name		= "cgroup",
26b15f
+	.version	= XTABLES_VERSION,
26b15f
+	.size		= XT_ALIGN(sizeof(struct xt_cgroup_info)),
26b15f
+	.userspacesize	= XT_ALIGN(sizeof(struct xt_cgroup_info)),
26b15f
+	.help		= cgroup_help,
26b15f
+	.print		= cgroup_print,
26b15f
+	.save		= cgroup_save,
26b15f
+	.x6_parse	= cgroup_parse,
26b15f
+	.x6_options	= cgroup_opts,
26b15f
+};
26b15f
+
26b15f
+void _init(void)
26b15f
+{
26b15f
+	xtables_register_match(&cgroup_match);
26b15f
+}
26b15f
diff --git a/extensions/libxt_cgroup.man b/extensions/libxt_cgroup.man
26b15f
new file mode 100644
26b15f
index 0000000..456a031
26b15f
--- /dev/null
26b15f
+++ b/extensions/libxt_cgroup.man
26b15f
@@ -0,0 +1,15 @@
26b15f
+.TP
26b15f
+[\fB!\fP] \fB\-\-cgroup\fP \fIfwid\fP
26b15f
+Match corresponding cgroup for this packet.
26b15f
+
26b15f
+Can be used to assign particular firewall policies for aggregated
26b15f
+task/jobs on the system. This allows for more fine-grained firewall
26b15f
+policies that only match for a subset of the system's processes.
26b15f
+fwid is the maker set through the net_cls cgroup's id.
26b15f
+.PP
26b15f
+Example:
26b15f
+.PP
26b15f
+iptables \-A OUTPUT \-p tcp \-\-sport 80 \-m cgroup ! \-\-cgroup 1
26b15f
+\-j DROP
26b15f
+.PP
26b15f
+Available since Linux 3.14.
26b15f
diff --git a/include/linux/netfilter/xt_cgroup.h b/include/linux/netfilter/xt_cgroup.h
26b15f
new file mode 100644
26b15f
index 0000000..943d3a0
26b15f
--- /dev/null
26b15f
+++ b/include/linux/netfilter/xt_cgroup.h
26b15f
@@ -0,0 +1,11 @@
26b15f
+#ifndef _XT_CGROUP_H
26b15f
+#define _XT_CGROUP_H
26b15f
+
26b15f
+#include <linux/types.h>
26b15f
+
26b15f
+struct xt_cgroup_info {
26b15f
+	__u32 id;
26b15f
+	__u32 invert;
26b15f
+};
26b15f
+
26b15f
+#endif /* _XT_CGROUP_H */
26b15f
-- 
26b15f
cgit v0.10.2
26b15f