xt_cgroup.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  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. #include <linux/skbuff.h>
  15. #include <linux/module.h>
  16. #include <linux/netfilter/x_tables.h>
  17. #include <linux/netfilter/xt_cgroup.h>
  18. #include <net/sock.h>
  19. MODULE_LICENSE("GPL");
  20. MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>");
  21. MODULE_DESCRIPTION("Xtables: process control group matching");
  22. MODULE_ALIAS("ipt_cgroup");
  23. MODULE_ALIAS("ip6t_cgroup");
  24. static int cgroup_mt_check(const struct xt_mtchk_param *par)
  25. {
  26. struct xt_cgroup_info *info = par->matchinfo;
  27. if (info->invert & ~1)
  28. return -EINVAL;
  29. return 0;
  30. }
  31. static bool
  32. cgroup_mt(const struct sk_buff *skb, struct xt_action_param *par)
  33. {
  34. const struct xt_cgroup_info *info = par->matchinfo;
  35. if (skb->sk == NULL || !sk_fullsock(skb->sk))
  36. return false;
  37. return (info->id == skb->sk->sk_classid) ^ info->invert;
  38. }
  39. static struct xt_match cgroup_mt_reg __read_mostly = {
  40. .name = "cgroup",
  41. .revision = 0,
  42. .family = NFPROTO_UNSPEC,
  43. .checkentry = cgroup_mt_check,
  44. .match = cgroup_mt,
  45. .matchsize = sizeof(struct xt_cgroup_info),
  46. .me = THIS_MODULE,
  47. .hooks = (1 << NF_INET_LOCAL_OUT) |
  48. (1 << NF_INET_POST_ROUTING) |
  49. (1 << NF_INET_LOCAL_IN),
  50. };
  51. static int __init cgroup_mt_init(void)
  52. {
  53. return xt_register_match(&cgroup_mt_reg);
  54. }
  55. static void __exit cgroup_mt_exit(void)
  56. {
  57. xt_unregister_match(&cgroup_mt_reg);
  58. }
  59. module_init(cgroup_mt_init);
  60. module_exit(cgroup_mt_exit);