xt_devgroup.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (c) 2011 Patrick McHardy <kaber@trash.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/netfilter/xt_devgroup.h>
  12. #include <linux/netfilter/x_tables.h>
  13. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  14. MODULE_LICENSE("GPL");
  15. MODULE_DESCRIPTION("Xtables: Device group match");
  16. MODULE_ALIAS("ipt_devgroup");
  17. MODULE_ALIAS("ip6t_devgroup");
  18. static bool devgroup_mt(const struct sk_buff *skb, struct xt_action_param *par)
  19. {
  20. const struct xt_devgroup_info *info = par->matchinfo;
  21. if (info->flags & XT_DEVGROUP_MATCH_SRC &&
  22. (((info->src_group ^ par->in->group) & info->src_mask ? 1 : 0) ^
  23. ((info->flags & XT_DEVGROUP_INVERT_SRC) ? 1 : 0)))
  24. return false;
  25. if (info->flags & XT_DEVGROUP_MATCH_DST &&
  26. (((info->dst_group ^ par->out->group) & info->dst_mask ? 1 : 0) ^
  27. ((info->flags & XT_DEVGROUP_INVERT_DST) ? 1 : 0)))
  28. return false;
  29. return true;
  30. }
  31. static int devgroup_mt_checkentry(const struct xt_mtchk_param *par)
  32. {
  33. const struct xt_devgroup_info *info = par->matchinfo;
  34. if (info->flags & ~(XT_DEVGROUP_MATCH_SRC | XT_DEVGROUP_INVERT_SRC |
  35. XT_DEVGROUP_MATCH_DST | XT_DEVGROUP_INVERT_DST))
  36. return -EINVAL;
  37. if (info->flags & XT_DEVGROUP_MATCH_SRC &&
  38. par->hook_mask & ~((1 << NF_INET_PRE_ROUTING) |
  39. (1 << NF_INET_LOCAL_IN) |
  40. (1 << NF_INET_FORWARD)))
  41. return -EINVAL;
  42. if (info->flags & XT_DEVGROUP_MATCH_DST &&
  43. par->hook_mask & ~((1 << NF_INET_FORWARD) |
  44. (1 << NF_INET_LOCAL_OUT) |
  45. (1 << NF_INET_POST_ROUTING)))
  46. return -EINVAL;
  47. return 0;
  48. }
  49. static struct xt_match devgroup_mt_reg __read_mostly = {
  50. .name = "devgroup",
  51. .match = devgroup_mt,
  52. .checkentry = devgroup_mt_checkentry,
  53. .matchsize = sizeof(struct xt_devgroup_info),
  54. .family = NFPROTO_UNSPEC,
  55. .me = THIS_MODULE
  56. };
  57. static int __init devgroup_mt_init(void)
  58. {
  59. return xt_register_match(&devgroup_mt_reg);
  60. }
  61. static void __exit devgroup_mt_exit(void)
  62. {
  63. xt_unregister_match(&devgroup_mt_reg);
  64. }
  65. module_init(devgroup_mt_init);
  66. module_exit(devgroup_mt_exit);