From: Tejun Heo <tj@kernel.org>
To: davem@davemloft.net, pablo@netfilter.org, kaber@trash.net,
kadlec@blackhole.kfki.hu, lizefan@huawei.com, hannes@cmpxchg.org
Cc: netdev@vger.kernel.org, netfilter-devel@vger.kernel.org,
coreteam@netfilter.org, cgroups@vger.kernel.org,
linux-kernel@vger.kernel.org, kernel-team@fb.com,
daniel@iogearbox.net, daniel.wagner@bmw-carit.de,
nhorman@tuxdriver.com
Subject: [PATCH v2 iptables] libxt_cgroup2: add support for cgroup2 path matching
Date: Thu, 19 Nov 2015 13:57:15 -0500 [thread overview]
Message-ID: <20151119185715.GB28608@mtj.duckdns.org> (raw)
In-Reply-To: <1447959171-20749-1-git-send-email-tj@kernel.org>
This patch adds the extension for the xt_cgroup2 which matches packets
based on the v2 cgroup path of the associated socket.
v2: cgroup2_match->userspacesize and ->save and man page updated as
per Jan.
Signed-off-by: Tejun Heo <tj@kernel.org>
Cc: Daniel Borkmann <dborkman@redhat.com>
Cc: Jan Engelhardt <jengelh@inai.de>
---
extensions/libxt_cgroup2.c | 71 +++++++++++++++++++++++++++++++++++
extensions/libxt_cgroup2.man | 24 +++++++++++
include/linux/netfilter/xt_cgroup2.h | 15 +++++++
3 files changed, 110 insertions(+)
--- /dev/null
+++ b/extensions/libxt_cgroup2.c
@@ -0,0 +1,71 @@
+#include <stdio.h>
+#include <mntent.h>
+#include <xtables.h>
+#include <linux/netfilter/xt_cgroup2.h>
+#include <linux/limits.h>
+
+enum {
+ O_CGROUP2_PATH,
+};
+
+static void cgroup2_help(void)
+{
+ printf(
+"cgroup2 match options:\n"
+"[!] --path path path relative to cgroup2 root, recursive match\n");
+}
+
+static const struct xt_option_entry cgroup2_opts[] = {
+ {
+ .name = "path",
+ .id = O_CGROUP2_PATH,
+ .type = XTTYPE_STRING,
+ .flags = XTOPT_INVERT | XTOPT_MAND | XTOPT_PUT,
+ XTOPT_POINTER(struct xt_cgroup2_info, path)
+ },
+ XTOPT_TABLEEND,
+};
+
+static void cgroup2_parse(struct xt_option_call *cb)
+{
+ struct xt_cgroup2_info *info = cb->data;
+
+ xtables_option_parse(cb);
+
+ if (cb->invert)
+ info->invert = true;
+}
+
+static void cgroup2_print(const void *ip, const struct xt_entry_match *match,
+ int numeric)
+{
+ const struct xt_cgroup2_info *info = (void *)match->data;
+
+ printf(" cgroup2 %s%s", info->invert ? "! ":"", info->path);
+}
+
+static void cgroup2_save(const void *ip, const struct xt_entry_match *match)
+{
+ const struct xt_cgroup2_info *info = (void *)match->data;
+
+ printf("%s --path", info->invert ? " !" : "");
+ xtables_save_string(info->path);
+}
+
+static struct xtables_match cgroup2_match = {
+ .family = NFPROTO_UNSPEC,
+ .name = "cgroup2",
+ .version = XTABLES_VERSION,
+ .size = XT_ALIGN(sizeof(struct xt_cgroup2_info)),
+ .userspacesize = offsetof(struct xt_cgroup2_info, priv),
+ .help = cgroup2_help,
+ .print = cgroup2_print,
+ .save = cgroup2_save,
+ .x6_parse = cgroup2_parse,
+ .x6_options = cgroup2_opts,
+};
+
+void _init(void)
+{
+ xtables_register_match(&cgroup2_match);
+}
--- /dev/null
+++ b/extensions/libxt_cgroup2.man
@@ -0,0 +1,24 @@
+.TP
+[\fB!\fP] \fB\-\-path\fP \fIpath\fP
+Match cgroup2 membership.
+
+Each socket is associated with the v2 cgroup of the creating process.
+This matches packets coming from or going to all sockets in the
+sub-hierarchy of the specified path. The path should be relative to
+the root of the cgroup2 hierarchy. Can be used in the OUTPUT and
+INPUT chains to assign particular firewall policies for aggregated
+processes on the system. This allows for more fine-grained firewall
+policies that only match for a subset of the system's processes.
+
+\fBIMPORTANT\fP: when being used in the INPUT chain, the cgroup2
+matcher is currently only of limited functionality, meaning it
+will only match on packets that are processed for local sockets
+through early socket demuxing. Therefore, general usage on the
+INPUT chain is not advised unless the implications are well
+understood.
+.PP
+Example:
+.IP
+iptables \-A OUTPUT \-p tcp \-\-sport 80 \-m cgroup2 ! \-\-path service/http-server \-j DROP
+.PP
+Available since Linux 4.5.
--- /dev/null
+++ b/include/linux/netfilter/xt_cgroup2.h
@@ -0,0 +1,15 @@
+#ifndef _XT_CGROUP2_H
+#define _XT_CGROUP2_H
+
+#include <linux/types.h>
+#include <linux/limits.h>
+
+struct xt_cgroup2_info {
+ char path[PATH_MAX];
+ __u8 invert;
+
+ /* kernel internal data */
+ void *priv __attribute__((aligned(8)));
+};
+
+#endif /* _XT_CGROUP2_H */
next prev parent reply other threads:[~2015-11-19 18:57 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-11-19 18:52 [PATCHSET v2] netfilter, cgroup: implement xt_cgroup2 match Tejun Heo
2015-11-19 18:52 ` [PATCH 1/7] cgroup: record ancestor IDs and reimplement cgroup_is_descendant() using it Tejun Heo
2015-11-19 18:52 ` [PATCH 2/7] kernfs: implement kernfs_walk_and_get() Tejun Heo
2015-11-20 4:41 ` Greg Kroah-Hartman
2015-11-20 21:12 ` Tejun Heo
2015-11-20 21:41 ` Greg Kroah-Hartman
2015-11-19 18:52 ` [PATCH 3/7] cgroup: implement cgroup_get_from_path() and expose cgroup_put() Tejun Heo
2015-11-19 18:52 ` [PATCH 4/7] netprio_cgroup: limit the maximum css->id to USHRT_MAX Tejun Heo
2015-11-20 9:18 ` Daniel Wagner
2015-11-19 18:52 ` [PATCH 5/7] net: wrap sock->sk_cgrp_prioidx and ->sk_classid inside a struct Tejun Heo
2015-11-19 18:52 ` [PATCH 6/7] sock, cgroup: add sock->sk_cgroup Tejun Heo
2015-11-20 11:04 ` Daniel Wagner
2015-11-20 19:12 ` Tejun Heo
2015-11-19 18:52 ` [PATCH 7/7] netfilter: implement xt_cgroup2 match Tejun Heo
2015-11-19 18:57 ` Tejun Heo [this message]
2015-11-19 18:58 ` [PATCH iptables] libxt_cgroup: improve wording in the man page Tejun Heo
2015-11-20 18:59 ` [PATCHSET v2] netfilter, cgroup: implement xt_cgroup2 match David Miller
2015-11-20 19:56 ` Pablo Neira Ayuso
2015-11-20 19:57 ` Pablo Neira Ayuso
2015-11-20 21:06 ` Tejun Heo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20151119185715.GB28608@mtj.duckdns.org \
--to=tj@kernel.org \
--cc=cgroups@vger.kernel.org \
--cc=coreteam@netfilter.org \
--cc=daniel.wagner@bmw-carit.de \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=hannes@cmpxchg.org \
--cc=kaber@trash.net \
--cc=kadlec@blackhole.kfki.hu \
--cc=kernel-team@fb.com \
--cc=linux-kernel@vger.kernel.org \
--cc=lizefan@huawei.com \
--cc=netdev@vger.kernel.org \
--cc=netfilter-devel@vger.kernel.org \
--cc=nhorman@tuxdriver.com \
--cc=pablo@netfilter.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®