1 // SPDX-License-Identifier: GPL-2.0-only
3 * Xtables module to match the process control group.
5 * Might be used to implement individual "per-application" firewall
6 * policies in contrast to global policies based on control groups.
7 * Matching is based upon processes tagged to net_cls' classid marker.
9 * (C) 2013 Daniel Borkmann <dborkman@redhat.com>
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/skbuff.h>
15 #include <linux/module.h>
16 #include <linux/netfilter/x_tables.h>
17 #include <linux/netfilter/xt_cgroup.h>
20 MODULE_LICENSE("GPL");
21 MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>");
22 MODULE_DESCRIPTION("Xtables: process control group matching");
23 MODULE_ALIAS("ipt_cgroup");
24 MODULE_ALIAS("ip6t_cgroup");
26 static int cgroup_mt_check_v0(const struct xt_mtchk_param
*par
)
28 struct xt_cgroup_info_v0
*info
= par
->matchinfo
;
30 if (info
->invert
& ~1)
36 static int cgroup_mt_check_v1(const struct xt_mtchk_param
*par
)
38 struct xt_cgroup_info_v1
*info
= par
->matchinfo
;
41 if ((info
->invert_path
& ~1) || (info
->invert_classid
& ~1))
44 if (!info
->has_path
&& !info
->has_classid
) {
45 pr_info("xt_cgroup: no path or classid specified\n");
49 if (info
->has_path
&& info
->has_classid
) {
50 pr_info_ratelimited("path and classid specified\n");
56 cgrp
= cgroup_get_from_path(info
->path
);
58 pr_info_ratelimited("invalid path, errno=%ld\n",
68 static int cgroup_mt_check_v2(const struct xt_mtchk_param
*par
)
70 struct xt_cgroup_info_v2
*info
= par
->matchinfo
;
73 if ((info
->invert_path
& ~1) || (info
->invert_classid
& ~1))
76 if (!info
->has_path
&& !info
->has_classid
) {
77 pr_info("xt_cgroup: no path or classid specified\n");
81 if (info
->has_path
&& info
->has_classid
) {
82 pr_info_ratelimited("path and classid specified\n");
88 cgrp
= cgroup_get_from_path(info
->path
);
90 pr_info_ratelimited("invalid path, errno=%ld\n",
101 cgroup_mt_v0(const struct sk_buff
*skb
, struct xt_action_param
*par
)
103 const struct xt_cgroup_info_v0
*info
= par
->matchinfo
;
104 struct sock
*sk
= skb
->sk
;
106 if (!sk
|| !sk_fullsock(sk
) || !net_eq(xt_net(par
), sock_net(sk
)))
109 return (info
->id
== sock_cgroup_classid(&skb
->sk
->sk_cgrp_data
)) ^
113 static bool cgroup_mt_v1(const struct sk_buff
*skb
, struct xt_action_param
*par
)
115 const struct xt_cgroup_info_v1
*info
= par
->matchinfo
;
116 struct sock_cgroup_data
*skcd
= &skb
->sk
->sk_cgrp_data
;
117 struct cgroup
*ancestor
= info
->priv
;
118 struct sock
*sk
= skb
->sk
;
120 if (!sk
|| !sk_fullsock(sk
) || !net_eq(xt_net(par
), sock_net(sk
)))
124 return cgroup_is_descendant(sock_cgroup_ptr(skcd
), ancestor
) ^
127 return (info
->classid
== sock_cgroup_classid(skcd
)) ^
128 info
->invert_classid
;
131 static bool cgroup_mt_v2(const struct sk_buff
*skb
, struct xt_action_param
*par
)
133 const struct xt_cgroup_info_v2
*info
= par
->matchinfo
;
134 struct sock_cgroup_data
*skcd
= &skb
->sk
->sk_cgrp_data
;
135 struct cgroup
*ancestor
= info
->priv
;
136 struct sock
*sk
= skb
->sk
;
138 if (!sk
|| !sk_fullsock(sk
) || !net_eq(xt_net(par
), sock_net(sk
)))
142 return cgroup_is_descendant(sock_cgroup_ptr(skcd
), ancestor
) ^
145 return (info
->classid
== sock_cgroup_classid(skcd
)) ^
146 info
->invert_classid
;
149 static void cgroup_mt_destroy_v1(const struct xt_mtdtor_param
*par
)
151 struct xt_cgroup_info_v1
*info
= par
->matchinfo
;
154 cgroup_put(info
->priv
);
157 static void cgroup_mt_destroy_v2(const struct xt_mtdtor_param
*par
)
159 struct xt_cgroup_info_v2
*info
= par
->matchinfo
;
162 cgroup_put(info
->priv
);
165 static struct xt_match cgroup_mt_reg
[] __read_mostly
= {
169 .family
= NFPROTO_UNSPEC
,
170 .checkentry
= cgroup_mt_check_v0
,
171 .match
= cgroup_mt_v0
,
172 .matchsize
= sizeof(struct xt_cgroup_info_v0
),
174 .hooks
= (1 << NF_INET_LOCAL_OUT
) |
175 (1 << NF_INET_POST_ROUTING
) |
176 (1 << NF_INET_LOCAL_IN
),
181 .family
= NFPROTO_UNSPEC
,
182 .checkentry
= cgroup_mt_check_v1
,
183 .match
= cgroup_mt_v1
,
184 .matchsize
= sizeof(struct xt_cgroup_info_v1
),
185 .usersize
= offsetof(struct xt_cgroup_info_v1
, priv
),
186 .destroy
= cgroup_mt_destroy_v1
,
188 .hooks
= (1 << NF_INET_LOCAL_OUT
) |
189 (1 << NF_INET_POST_ROUTING
) |
190 (1 << NF_INET_LOCAL_IN
),
195 .family
= NFPROTO_UNSPEC
,
196 .checkentry
= cgroup_mt_check_v2
,
197 .match
= cgroup_mt_v2
,
198 .matchsize
= sizeof(struct xt_cgroup_info_v2
),
199 .usersize
= offsetof(struct xt_cgroup_info_v2
, priv
),
200 .destroy
= cgroup_mt_destroy_v2
,
202 .hooks
= (1 << NF_INET_LOCAL_OUT
) |
203 (1 << NF_INET_POST_ROUTING
) |
204 (1 << NF_INET_LOCAL_IN
),
208 static int __init
cgroup_mt_init(void)
210 return xt_register_matches(cgroup_mt_reg
, ARRAY_SIZE(cgroup_mt_reg
));
213 static void __exit
cgroup_mt_exit(void)
215 xt_unregister_matches(cgroup_mt_reg
, ARRAY_SIZE(cgroup_mt_reg
));
218 module_init(cgroup_mt_init
);
219 module_exit(cgroup_mt_exit
);