xt_connlabel.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. size_t words;
  39. if (info->bit > XT_CONNLABEL_MAXBIT)
  40. return -ERANGE;
  41. if (info->options & ~options) {
  42. pr_err("Unknown options in mask %x\n", info->options);
  43. return -EINVAL;
  44. }
  45. ret = nf_ct_l3proto_try_module_get(par->family);
  46. if (ret < 0) {
  47. pr_info("cannot load conntrack support for proto=%u\n",
  48. par->family);
  49. return ret;
  50. }
  51. par->net->ct.labels_used++;
  52. words = BITS_TO_LONGS(info->bit+1);
  53. if (words > par->net->ct.label_words)
  54. par->net->ct.label_words = words;
  55. return ret;
  56. }
  57. static void connlabel_mt_destroy(const struct xt_mtdtor_param *par)
  58. {
  59. par->net->ct.labels_used--;
  60. if (par->net->ct.labels_used == 0)
  61. par->net->ct.label_words = 0;
  62. nf_ct_l3proto_module_put(par->family);
  63. }
  64. static struct xt_match connlabels_mt_reg __read_mostly = {
  65. .name = "connlabel",
  66. .family = NFPROTO_UNSPEC,
  67. .checkentry = connlabel_mt_check,
  68. .match = connlabel_mt,
  69. .matchsize = sizeof(struct xt_connlabel_mtinfo),
  70. .destroy = connlabel_mt_destroy,
  71. .me = THIS_MODULE,
  72. };
  73. static int __init connlabel_mt_init(void)
  74. {
  75. return xt_register_match(&connlabels_mt_reg);
  76. }
  77. static void __exit connlabel_mt_exit(void)
  78. {
  79. xt_unregister_match(&connlabels_mt_reg);
  80. }
  81. module_init(connlabel_mt_init);
  82. module_exit(connlabel_mt_exit);