xt_connlabel.c 2.6 KB

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