blob: 17300256772ab8c49e7bd886b3926348df7b02c2 [file] [log] [blame]
Daniel Borkmann82a37132013-12-29 18:27:12 +01001/*
2 * Xtables module to match the process control group.
3 *
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.
7 *
8 * (C) 2013 Daniel Borkmann <dborkman@redhat.com>
9 *
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.
13 */
14
15#include <linux/skbuff.h>
16#include <linux/module.h>
17#include <linux/netfilter/x_tables.h>
18#include <linux/netfilter/xt_cgroup.h>
19#include <net/sock.h>
20
21MODULE_LICENSE("GPL");
22MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>");
23MODULE_DESCRIPTION("Xtables: process control group matching");
24MODULE_ALIAS("ipt_cgroup");
25MODULE_ALIAS("ip6t_cgroup");
26
Tejun Heo4ec8ff02015-12-07 17:38:54 -050027static int cgroup_mt_check_v0(const struct xt_mtchk_param *par)
Daniel Borkmann82a37132013-12-29 18:27:12 +010028{
Tejun Heo4ec8ff02015-12-07 17:38:54 -050029 struct xt_cgroup_info_v0 *info = par->matchinfo;
Daniel Borkmann82a37132013-12-29 18:27:12 +010030
31 if (info->invert & ~1)
32 return -EINVAL;
33
Daniel Borkmanncaa8ad92014-08-18 15:46:28 +020034 return 0;
Daniel Borkmann82a37132013-12-29 18:27:12 +010035}
36
37static bool
Tejun Heo4ec8ff02015-12-07 17:38:54 -050038cgroup_mt_v0(const struct sk_buff *skb, struct xt_action_param *par)
Daniel Borkmann82a37132013-12-29 18:27:12 +010039{
Tejun Heo4ec8ff02015-12-07 17:38:54 -050040 const struct xt_cgroup_info_v0 *info = par->matchinfo;
Daniel Borkmann82a37132013-12-29 18:27:12 +010041
Daniel Borkmannafb77182015-03-27 19:37:41 +010042 if (skb->sk == NULL || !sk_fullsock(skb->sk))
Daniel Borkmann82a37132013-12-29 18:27:12 +010043 return false;
44
Tejun Heo2a56a1f2015-12-07 17:38:52 -050045 return (info->id == sock_cgroup_classid(&skb->sk->sk_cgrp_data)) ^
46 info->invert;
Daniel Borkmann82a37132013-12-29 18:27:12 +010047}
48
Tejun Heo4ec8ff02015-12-07 17:38:54 -050049static struct xt_match cgroup_mt_reg[] __read_mostly = {
50 {
51 .name = "cgroup",
52 .revision = 0,
53 .family = NFPROTO_UNSPEC,
54 .checkentry = cgroup_mt_check_v0,
55 .match = cgroup_mt_v0,
56 .matchsize = sizeof(struct xt_cgroup_info_v0),
57 .me = THIS_MODULE,
58 .hooks = (1 << NF_INET_LOCAL_OUT) |
59 (1 << NF_INET_POST_ROUTING) |
60 (1 << NF_INET_LOCAL_IN),
61 },
Daniel Borkmann82a37132013-12-29 18:27:12 +010062};
63
64static int __init cgroup_mt_init(void)
65{
Tejun Heo4ec8ff02015-12-07 17:38:54 -050066 return xt_register_matches(cgroup_mt_reg, ARRAY_SIZE(cgroup_mt_reg));
Daniel Borkmann82a37132013-12-29 18:27:12 +010067}
68
69static void __exit cgroup_mt_exit(void)
70{
Tejun Heo4ec8ff02015-12-07 17:38:54 -050071 xt_unregister_matches(cgroup_mt_reg, ARRAY_SIZE(cgroup_mt_reg));
Daniel Borkmann82a37132013-12-29 18:27:12 +010072}
73
74module_init(cgroup_mt_init);
75module_exit(cgroup_mt_exit);