xt_TEE.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * "TEE" target extension for Xtables
  3. * Copyright © Sebastian Claßen, 2007
  4. * Jan Engelhardt, 2007-2010
  5. *
  6. * based on ipt_ROUTE.c from Cédric de Launois
  7. * <delaunois@info.ucl.be>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * version 2 or later, as published by the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/route.h>
  16. #include <linux/netfilter/x_tables.h>
  17. #include <net/route.h>
  18. #include <net/netfilter/ipv4/nf_dup_ipv4.h>
  19. #include <net/netfilter/ipv6/nf_dup_ipv6.h>
  20. #include <linux/netfilter/xt_TEE.h>
  21. struct xt_tee_priv {
  22. struct notifier_block notifier;
  23. struct xt_tee_tginfo *tginfo;
  24. int oif;
  25. };
  26. static const union nf_inet_addr tee_zero_address;
  27. static unsigned int
  28. tee_tg4(struct sk_buff *skb, const struct xt_action_param *par)
  29. {
  30. const struct xt_tee_tginfo *info = par->targinfo;
  31. int oif = info->priv ? info->priv->oif : 0;
  32. nf_dup_ipv4(par->net, skb, par->hooknum, &info->gw.in, oif);
  33. return XT_CONTINUE;
  34. }
  35. #if IS_ENABLED(CONFIG_NF_DUP_IPV6)
  36. static unsigned int
  37. tee_tg6(struct sk_buff *skb, const struct xt_action_param *par)
  38. {
  39. const struct xt_tee_tginfo *info = par->targinfo;
  40. int oif = info->priv ? info->priv->oif : 0;
  41. nf_dup_ipv6(par->net, skb, par->hooknum, &info->gw.in6, oif);
  42. return XT_CONTINUE;
  43. }
  44. #endif
  45. static int tee_netdev_event(struct notifier_block *this, unsigned long event,
  46. void *ptr)
  47. {
  48. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  49. struct xt_tee_priv *priv;
  50. priv = container_of(this, struct xt_tee_priv, notifier);
  51. switch (event) {
  52. case NETDEV_REGISTER:
  53. if (!strcmp(dev->name, priv->tginfo->oif))
  54. priv->oif = dev->ifindex;
  55. break;
  56. case NETDEV_UNREGISTER:
  57. if (dev->ifindex == priv->oif)
  58. priv->oif = -1;
  59. break;
  60. case NETDEV_CHANGENAME:
  61. if (!strcmp(dev->name, priv->tginfo->oif))
  62. priv->oif = dev->ifindex;
  63. else if (dev->ifindex == priv->oif)
  64. priv->oif = -1;
  65. break;
  66. }
  67. return NOTIFY_DONE;
  68. }
  69. static int tee_tg_check(const struct xt_tgchk_param *par)
  70. {
  71. struct xt_tee_tginfo *info = par->targinfo;
  72. struct xt_tee_priv *priv;
  73. /* 0.0.0.0 and :: not allowed */
  74. if (memcmp(&info->gw, &tee_zero_address,
  75. sizeof(tee_zero_address)) == 0)
  76. return -EINVAL;
  77. if (info->oif[0]) {
  78. if (info->oif[sizeof(info->oif)-1] != '\0')
  79. return -EINVAL;
  80. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  81. if (priv == NULL)
  82. return -ENOMEM;
  83. priv->tginfo = info;
  84. priv->oif = -1;
  85. priv->notifier.notifier_call = tee_netdev_event;
  86. info->priv = priv;
  87. register_netdevice_notifier(&priv->notifier);
  88. } else
  89. info->priv = NULL;
  90. static_key_slow_inc(&xt_tee_enabled);
  91. return 0;
  92. }
  93. static void tee_tg_destroy(const struct xt_tgdtor_param *par)
  94. {
  95. struct xt_tee_tginfo *info = par->targinfo;
  96. if (info->priv) {
  97. unregister_netdevice_notifier(&info->priv->notifier);
  98. kfree(info->priv);
  99. }
  100. static_key_slow_dec(&xt_tee_enabled);
  101. }
  102. static struct xt_target tee_tg_reg[] __read_mostly = {
  103. {
  104. .name = "TEE",
  105. .revision = 1,
  106. .family = NFPROTO_IPV4,
  107. .target = tee_tg4,
  108. .targetsize = sizeof(struct xt_tee_tginfo),
  109. .checkentry = tee_tg_check,
  110. .destroy = tee_tg_destroy,
  111. .me = THIS_MODULE,
  112. },
  113. #if IS_ENABLED(CONFIG_NF_DUP_IPV6)
  114. {
  115. .name = "TEE",
  116. .revision = 1,
  117. .family = NFPROTO_IPV6,
  118. .target = tee_tg6,
  119. .targetsize = sizeof(struct xt_tee_tginfo),
  120. .checkentry = tee_tg_check,
  121. .destroy = tee_tg_destroy,
  122. .me = THIS_MODULE,
  123. },
  124. #endif
  125. };
  126. static int __init tee_tg_init(void)
  127. {
  128. return xt_register_targets(tee_tg_reg, ARRAY_SIZE(tee_tg_reg));
  129. }
  130. static void __exit tee_tg_exit(void)
  131. {
  132. xt_unregister_targets(tee_tg_reg, ARRAY_SIZE(tee_tg_reg));
  133. }
  134. module_init(tee_tg_init);
  135. module_exit(tee_tg_exit);
  136. MODULE_AUTHOR("Sebastian Claßen <sebastian.classen@freenet.ag>");
  137. MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
  138. MODULE_DESCRIPTION("Xtables: Reroute packet copy");
  139. MODULE_LICENSE("GPL");
  140. MODULE_ALIAS("ipt_TEE");
  141. MODULE_ALIAS("ip6t_TEE");