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.
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 #include <linux/skbuff.h>
16 #include <linux/module.h>
17 #include <linux/netfilter/x_tables.h>
18 #include <linux/netfilter/xt_cgroup.h>
21 MODULE_LICENSE("GPL");
23 MODULE_DESCRIPTION("Xtables: process control group matching");
24 MODULE_ALIAS("ipt_cgroup");
25 MODULE_ALIAS("ip6t_cgroup");
27 static int cgroup_mt_check_v0(const struct xt_mtchk_param *par)
29 struct xt_cgroup_info_v0 *info = par->matchinfo;
31 if (info->invert & ~1)
37 static int cgroup_mt_check_v1(const struct xt_mtchk_param *par)
39 struct xt_cgroup_info_v1 *info = par->matchinfo;
42 if ((info->invert_path & ~1) || (info->invert_classid & ~1))
45 if (!info->has_path && !info->has_classid) {
46 pr_info("xt_cgroup: no path or classid specified\n");
50 if (info->has_path && info->has_classid) {
51 pr_info("xt_cgroup: both path and classid specified\n");
56 cgrp = cgroup_get_from_path(info->path);
58 pr_info("xt_cgroup: invalid path, errno=%ld\n",
69 cgroup_mt_v0(const struct sk_buff *skb, struct xt_action_param *par)
71 const struct xt_cgroup_info_v0 *info = par->matchinfo;
73 if (skb->sk == NULL || !sk_fullsock(skb->sk))
76 return (info->id == sock_cgroup_classid(&skb->sk->sk_cgrp_data)) ^
80 static bool cgroup_mt_v1(const struct sk_buff *skb, struct xt_action_param *par)
82 const struct xt_cgroup_info_v1 *info = par->matchinfo;
83 struct sock_cgroup_data *skcd = &skb->sk->sk_cgrp_data;
84 struct cgroup *ancestor = info->priv;
86 if (!skb->sk || !sk_fullsock(skb->sk))
90 return cgroup_is_descendant(sock_cgroup_ptr(skcd), ancestor) ^
93 return (info->classid == sock_cgroup_classid(skcd)) ^
97 static void cgroup_mt_destroy_v1(const struct xt_mtdtor_param *par)
99 struct xt_cgroup_info_v1 *info = par->matchinfo;
102 cgroup_put(info->priv);
105 static struct xt_match cgroup_mt_reg[] __read_mostly = {
109 .family = NFPROTO_UNSPEC,
110 .checkentry = cgroup_mt_check_v0,
111 .match = cgroup_mt_v0,
112 .matchsize = sizeof(struct xt_cgroup_info_v0),
114 .hooks = (1 << NF_INET_LOCAL_OUT) |
115 (1 << NF_INET_POST_ROUTING) |
116 (1 << NF_INET_LOCAL_IN),
121 .family = NFPROTO_UNSPEC,
122 .checkentry = cgroup_mt_check_v1,
123 .match = cgroup_mt_v1,
124 .matchsize = sizeof(struct xt_cgroup_info_v1),
125 .destroy = cgroup_mt_destroy_v1,
127 .hooks = (1 << NF_INET_LOCAL_OUT) |
128 (1 << NF_INET_POST_ROUTING) |
129 (1 << NF_INET_LOCAL_IN),
133 static int __init cgroup_mt_init(void)
135 return xt_register_matches(cgroup_mt_reg, ARRAY_SIZE(cgroup_mt_reg));
138 static void __exit cgroup_mt_exit(void)
140 xt_unregister_matches(cgroup_mt_reg, ARRAY_SIZE(cgroup_mt_reg));
143 module_init(cgroup_mt_init);
144 module_exit(cgroup_mt_exit);