xt_connlabel.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * (C) 2013 Astaro GmbH & Co KG
  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 <net/netfilter/nf_conntrack.h>
  11. #include <net/netfilter/nf_conntrack_labels.h>
  12. #include <linux/netfilter/x_tables.h>
  13. MODULE_LICENSE("GPL");
  14. MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
  15. MODULE_DESCRIPTION("Xtables: add/match connection trackling labels");
  16. MODULE_ALIAS("ipt_connlabel");
  17. MODULE_ALIAS("ip6t_connlabel");
  18. static bool
  19. connlabel_mt(const struct sk_buff *skb, struct xt_action_param *par)
  20. {
  21. const struct xt_connlabel_mtinfo *info = par->matchinfo;
  22. enum ip_conntrack_info ctinfo;
  23. struct nf_conn *ct;
  24. bool invert = info->options & XT_CONNLABEL_OP_INVERT;
  25. ct = nf_ct_get(skb, &ctinfo);
  26. if (ct == NULL || nf_ct_is_untracked(ct))
  27. return invert;
  28. if (info->options & XT_CONNLABEL_OP_SET)
  29. return (nf_connlabel_set(ct, info->bit) == 0) ^ invert;
  30. return nf_connlabel_match(ct, info->bit) ^ invert;
  31. }
  32. static int connlabel_mt_check(const struct xt_mtchk_param *par)
  33. {
  34. const int options = XT_CONNLABEL_OP_INVERT |
  35. XT_CONNLABEL_OP_SET;
  36. struct xt_connlabel_mtinfo *info = par->matchinfo;
  37. int ret;
  38. if (info->options & ~options) {
  39. pr_err("Unknown options in mask %x\n", info->options);
  40. return -EINVAL;
  41. }
  42. ret = nf_ct_l3proto_try_module_get(par->family);
  43. if (ret < 0) {
  44. pr_info("cannot load conntrack support for proto=%u\n",
  45. par->family);
  46. return ret;
  47. }
  48. ret = nf_connlabels_get(par->net, info->bit + 1);
  49. if (ret < 0)
  50. nf_ct_l3proto_module_put(par->family);
  51. return ret;
  52. }
  53. static void connlabel_mt_destroy(const struct xt_mtdtor_param *par)
  54. {
  55. nf_connlabels_put(par->net);
  56. nf_ct_l3proto_module_put(par->family);
  57. }
  58. static struct xt_match connlabels_mt_reg __read_mostly = {
  59. .name = "connlabel",
  60. .family = NFPROTO_UNSPEC,
  61. .checkentry = connlabel_mt_check,
  62. .match = connlabel_mt,
  63. .matchsize = sizeof(struct xt_connlabel_mtinfo),
  64. .destroy = connlabel_mt_destroy,
  65. .me = THIS_MODULE,
  66. };
  67. static int __init connlabel_mt_init(void)
  68. {
  69. return xt_register_match(&connlabels_mt_reg);
  70. }
  71. static void __exit connlabel_mt_exit(void)
  72. {
  73. xt_unregister_match(&connlabels_mt_reg);
  74. }
  75. module_init(connlabel_mt_init);
  76. module_exit(connlabel_mt_exit);