xt_TEE.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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/ip.h>
  14. #include <linux/module.h>
  15. #include <linux/percpu.h>
  16. #include <linux/route.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/notifier.h>
  19. #include <net/checksum.h>
  20. #include <net/icmp.h>
  21. #include <net/ip.h>
  22. #include <net/ipv6.h>
  23. #include <net/ip6_route.h>
  24. #include <net/route.h>
  25. #include <linux/netfilter/x_tables.h>
  26. #include <linux/netfilter/xt_TEE.h>
  27. #if IS_ENABLED(CONFIG_NF_CONNTRACK)
  28. # define WITH_CONNTRACK 1
  29. # include <net/netfilter/nf_conntrack.h>
  30. #endif
  31. struct xt_tee_priv {
  32. struct notifier_block notifier;
  33. struct xt_tee_tginfo *tginfo;
  34. int oif;
  35. };
  36. static const union nf_inet_addr tee_zero_address;
  37. static DEFINE_PER_CPU(bool, tee_active);
  38. static struct net *pick_net(struct sk_buff *skb)
  39. {
  40. #ifdef CONFIG_NET_NS
  41. const struct dst_entry *dst;
  42. if (skb->dev != NULL)
  43. return dev_net(skb->dev);
  44. dst = skb_dst(skb);
  45. if (dst != NULL && dst->dev != NULL)
  46. return dev_net(dst->dev);
  47. #endif
  48. return &init_net;
  49. }
  50. static bool
  51. tee_tg_route4(struct sk_buff *skb, const struct xt_tee_tginfo *info)
  52. {
  53. const struct iphdr *iph = ip_hdr(skb);
  54. struct net *net = pick_net(skb);
  55. struct rtable *rt;
  56. struct flowi4 fl4;
  57. memset(&fl4, 0, sizeof(fl4));
  58. if (info->priv) {
  59. if (info->priv->oif == -1)
  60. return false;
  61. fl4.flowi4_oif = info->priv->oif;
  62. }
  63. fl4.daddr = info->gw.ip;
  64. fl4.flowi4_tos = RT_TOS(iph->tos);
  65. fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
  66. fl4.flowi4_flags = FLOWI_FLAG_KNOWN_NH;
  67. rt = ip_route_output_key(net, &fl4);
  68. if (IS_ERR(rt))
  69. return false;
  70. skb_dst_drop(skb);
  71. skb_dst_set(skb, &rt->dst);
  72. skb->dev = rt->dst.dev;
  73. skb->protocol = htons(ETH_P_IP);
  74. return true;
  75. }
  76. static unsigned int
  77. tee_tg4(struct sk_buff *skb, const struct xt_action_param *par)
  78. {
  79. const struct xt_tee_tginfo *info = par->targinfo;
  80. struct iphdr *iph;
  81. if (__this_cpu_read(tee_active))
  82. return XT_CONTINUE;
  83. /*
  84. * Copy the skb, and route the copy. Will later return %XT_CONTINUE for
  85. * the original skb, which should continue on its way as if nothing has
  86. * happened. The copy should be independently delivered to the TEE
  87. * --gateway.
  88. */
  89. skb = pskb_copy(skb, GFP_ATOMIC);
  90. if (skb == NULL)
  91. return XT_CONTINUE;
  92. #ifdef WITH_CONNTRACK
  93. /* Avoid counting cloned packets towards the original connection. */
  94. nf_conntrack_put(skb->nfct);
  95. skb->nfct = &nf_ct_untracked_get()->ct_general;
  96. skb->nfctinfo = IP_CT_NEW;
  97. nf_conntrack_get(skb->nfct);
  98. #endif
  99. /*
  100. * If we are in PREROUTING/INPUT, the checksum must be recalculated
  101. * since the length could have changed as a result of defragmentation.
  102. *
  103. * We also decrease the TTL to mitigate potential TEE loops
  104. * between two hosts.
  105. *
  106. * Set %IP_DF so that the original source is notified of a potentially
  107. * decreased MTU on the clone route. IPv6 does this too.
  108. */
  109. iph = ip_hdr(skb);
  110. iph->frag_off |= htons(IP_DF);
  111. if (par->hooknum == NF_INET_PRE_ROUTING ||
  112. par->hooknum == NF_INET_LOCAL_IN)
  113. --iph->ttl;
  114. ip_send_check(iph);
  115. if (tee_tg_route4(skb, info)) {
  116. __this_cpu_write(tee_active, true);
  117. ip_local_out(skb);
  118. __this_cpu_write(tee_active, false);
  119. } else {
  120. kfree_skb(skb);
  121. }
  122. return XT_CONTINUE;
  123. }
  124. #if IS_ENABLED(CONFIG_IPV6)
  125. static bool
  126. tee_tg_route6(struct sk_buff *skb, const struct xt_tee_tginfo *info)
  127. {
  128. const struct ipv6hdr *iph = ipv6_hdr(skb);
  129. struct net *net = pick_net(skb);
  130. struct dst_entry *dst;
  131. struct flowi6 fl6;
  132. memset(&fl6, 0, sizeof(fl6));
  133. if (info->priv) {
  134. if (info->priv->oif == -1)
  135. return false;
  136. fl6.flowi6_oif = info->priv->oif;
  137. }
  138. fl6.daddr = info->gw.in6;
  139. fl6.flowlabel = ((iph->flow_lbl[0] & 0xF) << 16) |
  140. (iph->flow_lbl[1] << 8) | iph->flow_lbl[2];
  141. dst = ip6_route_output(net, NULL, &fl6);
  142. if (dst->error) {
  143. dst_release(dst);
  144. return false;
  145. }
  146. skb_dst_drop(skb);
  147. skb_dst_set(skb, dst);
  148. skb->dev = dst->dev;
  149. skb->protocol = htons(ETH_P_IPV6);
  150. return true;
  151. }
  152. static unsigned int
  153. tee_tg6(struct sk_buff *skb, const struct xt_action_param *par)
  154. {
  155. const struct xt_tee_tginfo *info = par->targinfo;
  156. if (__this_cpu_read(tee_active))
  157. return XT_CONTINUE;
  158. skb = pskb_copy(skb, GFP_ATOMIC);
  159. if (skb == NULL)
  160. return XT_CONTINUE;
  161. #ifdef WITH_CONNTRACK
  162. nf_conntrack_put(skb->nfct);
  163. skb->nfct = &nf_ct_untracked_get()->ct_general;
  164. skb->nfctinfo = IP_CT_NEW;
  165. nf_conntrack_get(skb->nfct);
  166. #endif
  167. if (par->hooknum == NF_INET_PRE_ROUTING ||
  168. par->hooknum == NF_INET_LOCAL_IN) {
  169. struct ipv6hdr *iph = ipv6_hdr(skb);
  170. --iph->hop_limit;
  171. }
  172. if (tee_tg_route6(skb, info)) {
  173. __this_cpu_write(tee_active, true);
  174. ip6_local_out(skb);
  175. __this_cpu_write(tee_active, false);
  176. } else {
  177. kfree_skb(skb);
  178. }
  179. return XT_CONTINUE;
  180. }
  181. #endif
  182. static int tee_netdev_event(struct notifier_block *this, unsigned long event,
  183. void *ptr)
  184. {
  185. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  186. struct xt_tee_priv *priv;
  187. priv = container_of(this, struct xt_tee_priv, notifier);
  188. switch (event) {
  189. case NETDEV_REGISTER:
  190. if (!strcmp(dev->name, priv->tginfo->oif))
  191. priv->oif = dev->ifindex;
  192. break;
  193. case NETDEV_UNREGISTER:
  194. if (dev->ifindex == priv->oif)
  195. priv->oif = -1;
  196. break;
  197. case NETDEV_CHANGENAME:
  198. if (!strcmp(dev->name, priv->tginfo->oif))
  199. priv->oif = dev->ifindex;
  200. else if (dev->ifindex == priv->oif)
  201. priv->oif = -1;
  202. break;
  203. }
  204. return NOTIFY_DONE;
  205. }
  206. static int tee_tg_check(const struct xt_tgchk_param *par)
  207. {
  208. struct xt_tee_tginfo *info = par->targinfo;
  209. struct xt_tee_priv *priv;
  210. /* 0.0.0.0 and :: not allowed */
  211. if (memcmp(&info->gw, &tee_zero_address,
  212. sizeof(tee_zero_address)) == 0)
  213. return -EINVAL;
  214. if (info->oif[0]) {
  215. if (info->oif[sizeof(info->oif)-1] != '\0')
  216. return -EINVAL;
  217. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  218. if (priv == NULL)
  219. return -ENOMEM;
  220. priv->tginfo = info;
  221. priv->oif = -1;
  222. priv->notifier.notifier_call = tee_netdev_event;
  223. info->priv = priv;
  224. register_netdevice_notifier(&priv->notifier);
  225. } else
  226. info->priv = NULL;
  227. return 0;
  228. }
  229. static void tee_tg_destroy(const struct xt_tgdtor_param *par)
  230. {
  231. struct xt_tee_tginfo *info = par->targinfo;
  232. if (info->priv) {
  233. unregister_netdevice_notifier(&info->priv->notifier);
  234. kfree(info->priv);
  235. }
  236. }
  237. static struct xt_target tee_tg_reg[] __read_mostly = {
  238. {
  239. .name = "TEE",
  240. .revision = 1,
  241. .family = NFPROTO_IPV4,
  242. .target = tee_tg4,
  243. .targetsize = sizeof(struct xt_tee_tginfo),
  244. .checkentry = tee_tg_check,
  245. .destroy = tee_tg_destroy,
  246. .me = THIS_MODULE,
  247. },
  248. #if IS_ENABLED(CONFIG_IPV6)
  249. {
  250. .name = "TEE",
  251. .revision = 1,
  252. .family = NFPROTO_IPV6,
  253. .target = tee_tg6,
  254. .targetsize = sizeof(struct xt_tee_tginfo),
  255. .checkentry = tee_tg_check,
  256. .destroy = tee_tg_destroy,
  257. .me = THIS_MODULE,
  258. },
  259. #endif
  260. };
  261. static int __init tee_tg_init(void)
  262. {
  263. return xt_register_targets(tee_tg_reg, ARRAY_SIZE(tee_tg_reg));
  264. }
  265. static void __exit tee_tg_exit(void)
  266. {
  267. xt_unregister_targets(tee_tg_reg, ARRAY_SIZE(tee_tg_reg));
  268. }
  269. module_init(tee_tg_init);
  270. module_exit(tee_tg_exit);
  271. MODULE_AUTHOR("Sebastian Claßen <sebastian.classen@freenet.ag>");
  272. MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
  273. MODULE_DESCRIPTION("Xtables: Reroute packet copy");
  274. MODULE_LICENSE("GPL");
  275. MODULE_ALIAS("ipt_TEE");
  276. MODULE_ALIAS("ip6t_TEE");