xt_CONNSECMARK.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * This module is used to copy security markings from packets
  4. * to connections, and restore security markings from connections
  5. * back to packets. This would normally be performed in conjunction
  6. * with the SECMARK target and state match.
  7. *
  8. * Based somewhat on CONNMARK:
  9. * Copyright (C) 2002,2004 MARA Systems AB <https://www.marasystems.com>
  10. * by Henrik Nordstrom <hno@marasystems.com>
  11. *
  12. * (C) 2006,2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
  13. */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/module.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/netfilter/x_tables.h>
  18. #include <linux/netfilter/xt_CONNSECMARK.h>
  19. #include <net/netfilter/nf_conntrack.h>
  20. #include <net/netfilter/nf_conntrack_ecache.h>
  21. MODULE_LICENSE("GPL");
  22. MODULE_AUTHOR("James Morris <jmorris@redhat.com>");
  23. MODULE_DESCRIPTION("Xtables: target for copying between connection and security mark");
  24. MODULE_ALIAS("ipt_CONNSECMARK");
  25. MODULE_ALIAS("ip6t_CONNSECMARK");
  26. /*
  27. * If the packet has a security mark and the connection does not, copy
  28. * the security mark from the packet to the connection.
  29. */
  30. static void secmark_save(const struct sk_buff *skb)
  31. {
  32. if (skb->secmark) {
  33. struct nf_conn *ct;
  34. enum ip_conntrack_info ctinfo;
  35. ct = nf_ct_get(skb, &ctinfo);
  36. if (ct && !ct->secmark) {
  37. ct->secmark = skb->secmark;
  38. nf_conntrack_event_cache(IPCT_SECMARK, ct);
  39. }
  40. }
  41. }
  42. /*
  43. * If packet has no security mark, and the connection does, restore the
  44. * security mark from the connection to the packet.
  45. */
  46. static void secmark_restore(struct sk_buff *skb)
  47. {
  48. if (!skb->secmark) {
  49. const struct nf_conn *ct;
  50. enum ip_conntrack_info ctinfo;
  51. ct = nf_ct_get(skb, &ctinfo);
  52. if (ct && ct->secmark)
  53. skb->secmark = ct->secmark;
  54. }
  55. }
  56. static unsigned int
  57. connsecmark_tg(struct sk_buff *skb, const struct xt_action_param *par)
  58. {
  59. const struct xt_connsecmark_target_info *info = par->targinfo;
  60. switch (info->mode) {
  61. case CONNSECMARK_SAVE:
  62. secmark_save(skb);
  63. break;
  64. case CONNSECMARK_RESTORE:
  65. secmark_restore(skb);
  66. break;
  67. default:
  68. BUG();
  69. }
  70. return XT_CONTINUE;
  71. }
  72. static int connsecmark_tg_check(const struct xt_tgchk_param *par)
  73. {
  74. const struct xt_connsecmark_target_info *info = par->targinfo;
  75. int ret;
  76. if (strcmp(par->table, "mangle") != 0 &&
  77. strcmp(par->table, "security") != 0) {
  78. pr_info_ratelimited("only valid in \'mangle\' or \'security\' table, not \'%s\'\n",
  79. par->table);
  80. return -EINVAL;
  81. }
  82. switch (info->mode) {
  83. case CONNSECMARK_SAVE:
  84. case CONNSECMARK_RESTORE:
  85. break;
  86. default:
  87. pr_info_ratelimited("invalid mode: %hu\n", info->mode);
  88. return -EINVAL;
  89. }
  90. ret = nf_ct_netns_get(par->net, par->family);
  91. if (ret < 0)
  92. pr_info_ratelimited("cannot load conntrack support for proto=%u\n",
  93. par->family);
  94. return ret;
  95. }
  96. static void connsecmark_tg_destroy(const struct xt_tgdtor_param *par)
  97. {
  98. nf_ct_netns_put(par->net, par->family);
  99. }
  100. static struct xt_target connsecmark_tg_reg __read_mostly = {
  101. .name = "CONNSECMARK",
  102. .revision = 0,
  103. .family = NFPROTO_UNSPEC,
  104. .checkentry = connsecmark_tg_check,
  105. .destroy = connsecmark_tg_destroy,
  106. .target = connsecmark_tg,
  107. .targetsize = sizeof(struct xt_connsecmark_target_info),
  108. .me = THIS_MODULE,
  109. };
  110. static int __init connsecmark_tg_init(void)
  111. {
  112. return xt_register_target(&connsecmark_tg_reg);
  113. }
  114. static void __exit connsecmark_tg_exit(void)
  115. {
  116. xt_unregister_target(&connsecmark_tg_reg);
  117. }
  118. module_init(connsecmark_tg_init);
  119. module_exit(connsecmark_tg_exit);