2 * Xtables module to match the process control group.
4 * Might be used to implement individual "per-application" firewall
5 * policies in contrast to global policies based on control groups.
6 * Matching is based upon processes tagged to net_cls' classid marker.
8 * (C) 2013 Daniel Borkmann <dborkman@redhat.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/skbuff.h>
18 #include <linux/module.h>
19 #include <linux/netfilter/x_tables.h>
20 #include <linux/netfilter/xt_cgroup.h>
23 MODULE_LICENSE("GPL");
24 MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>");
25 MODULE_DESCRIPTION("Xtables: process control group matching");
26 MODULE_ALIAS("ipt_cgroup");
27 MODULE_ALIAS("ip6t_cgroup");
29 static int cgroup_mt_check_v0(const struct xt_mtchk_param
*par
)
31 struct xt_cgroup_info_v0
*info
= par
->matchinfo
;
33 if (info
->invert
& ~1)
39 static int cgroup_mt_check_v1(const struct xt_mtchk_param
*par
)
41 struct xt_cgroup_info_v1
*info
= par
->matchinfo
;
44 if ((info
->invert_path
& ~1) || (info
->invert_classid
& ~1))
47 if (!info
->has_path
&& !info
->has_classid
) {
48 pr_info("xt_cgroup: no path or classid specified\n");
52 if (info
->has_path
&& info
->has_classid
) {
53 pr_info_ratelimited("path and classid specified\n");
59 cgrp
= cgroup_get_from_path(info
->path
);
61 pr_info_ratelimited("invalid path, errno=%ld\n",
72 cgroup_mt_v0(const struct sk_buff
*skb
, struct xt_action_param
*par
)
74 const struct xt_cgroup_info_v0
*info
= par
->matchinfo
;
75 struct sock
*sk
= skb
->sk
;
77 if (!sk
|| !sk_fullsock(sk
) || !net_eq(xt_net(par
), sock_net(sk
)))
80 return (info
->id
== sock_cgroup_classid(&skb
->sk
->sk_cgrp_data
)) ^
84 static bool cgroup_mt_v1(const struct sk_buff
*skb
, struct xt_action_param
*par
)
86 const struct xt_cgroup_info_v1
*info
= par
->matchinfo
;
87 struct sock_cgroup_data
*skcd
= &skb
->sk
->sk_cgrp_data
;
88 struct cgroup
*ancestor
= info
->priv
;
89 struct sock
*sk
= skb
->sk
;
91 if (!sk
|| !sk_fullsock(sk
) || !net_eq(xt_net(par
), sock_net(sk
)))
95 return cgroup_is_descendant(sock_cgroup_ptr(skcd
), ancestor
) ^
98 return (info
->classid
== sock_cgroup_classid(skcd
)) ^
102 static void cgroup_mt_destroy_v1(const struct xt_mtdtor_param
*par
)
104 struct xt_cgroup_info_v1
*info
= par
->matchinfo
;
107 cgroup_put(info
->priv
);
110 static struct xt_match cgroup_mt_reg
[] __read_mostly
= {
114 .family
= NFPROTO_UNSPEC
,
115 .checkentry
= cgroup_mt_check_v0
,
116 .match
= cgroup_mt_v0
,
117 .matchsize
= sizeof(struct xt_cgroup_info_v0
),
119 .hooks
= (1 << NF_INET_LOCAL_OUT
) |
120 (1 << NF_INET_POST_ROUTING
) |
121 (1 << NF_INET_LOCAL_IN
),
126 .family
= NFPROTO_UNSPEC
,
127 .checkentry
= cgroup_mt_check_v1
,
128 .match
= cgroup_mt_v1
,
129 .matchsize
= sizeof(struct xt_cgroup_info_v1
),
130 .usersize
= offsetof(struct xt_cgroup_info_v1
, priv
),
131 .destroy
= cgroup_mt_destroy_v1
,
133 .hooks
= (1 << NF_INET_LOCAL_OUT
) |
134 (1 << NF_INET_POST_ROUTING
) |
135 (1 << NF_INET_LOCAL_IN
),
139 static int __init
cgroup_mt_init(void)
141 return xt_register_matches(cgroup_mt_reg
, ARRAY_SIZE(cgroup_mt_reg
));
144 static void __exit
cgroup_mt_exit(void)
146 xt_unregister_matches(cgroup_mt_reg
, ARRAY_SIZE(cgroup_mt_reg
));
149 module_init(cgroup_mt_init
);
150 module_exit(cgroup_mt_exit
);