xt_TEE.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  28. # define WITH_CONNTRACK 1
  29. # include <net/netfilter/nf_conntrack.h>
  30. #endif
  31. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  32. # define WITH_IPV6 1
  33. #endif
  34. struct xt_tee_priv {
  35. struct notifier_block notifier;
  36. struct xt_tee_tginfo *tginfo;
  37. int oif;
  38. };
  39. static const union nf_inet_addr tee_zero_address;
  40. static DEFINE_PER_CPU(bool, tee_active);
  41. static struct net *pick_net(struct sk_buff *skb)
  42. {
  43. #ifdef CONFIG_NET_NS
  44. const struct dst_entry *dst;
  45. if (skb->dev != NULL)
  46. return dev_net(skb->dev);
  47. dst = skb_dst(skb);
  48. if (dst != NULL && dst->dev != NULL)
  49. return dev_net(dst->dev);
  50. #endif
  51. return &init_net;
  52. }
  53. static bool
  54. tee_tg_route4(struct sk_buff *skb, const struct xt_tee_tginfo *info)
  55. {
  56. const struct iphdr *iph = ip_hdr(skb);
  57. struct net *net = pick_net(skb);
  58. struct rtable *rt;
  59. struct flowi4 fl4;
  60. memset(&fl4, 0, sizeof(fl4));
  61. if (info->priv) {
  62. if (info->priv->oif == -1)
  63. return false;
  64. fl4.flowi4_oif = info->priv->oif;
  65. }
  66. fl4.daddr = info->gw.ip;
  67. fl4.flowi4_tos = RT_TOS(iph->tos);
  68. fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
  69. rt = ip_route_output_key(net, &fl4);
  70. if (IS_ERR(rt))
  71. return false;
  72. skb_dst_drop(skb);
  73. skb_dst_set(skb, &rt->dst);
  74. skb->dev = rt->dst.dev;
  75. skb->protocol = htons(ETH_P_IP);
  76. return true;
  77. }
  78. static unsigned int
  79. tee_tg4(struct sk_buff *skb, const struct xt_action_param *par)
  80. {
  81. const struct xt_tee_tginfo *info = par->targinfo;
  82. struct iphdr *iph;
  83. if (percpu_read(tee_active))
  84. return XT_CONTINUE;
  85. /*
  86. * Copy the skb, and route the copy. Will later return %XT_CONTINUE for
  87. * the original skb, which should continue on its way as if nothing has
  88. * happened. The copy should be independently delivered to the TEE
  89. * --gateway.
  90. */
  91. skb = pskb_copy(skb, GFP_ATOMIC);
  92. if (skb == NULL)
  93. return XT_CONTINUE;
  94. #ifdef WITH_CONNTRACK
  95. /* Avoid counting cloned packets towards the original connection. */
  96. nf_conntrack_put(skb->nfct);
  97. skb->nfct = &nf_ct_untracked_get()->ct_general;
  98. skb->nfctinfo = IP_CT_NEW;
  99. nf_conntrack_get(skb->nfct);
  100. #endif
  101. /*
  102. * If we are in PREROUTING/INPUT, the checksum must be recalculated
  103. * since the length could have changed as a result of defragmentation.
  104. *
  105. * We also decrease the TTL to mitigate potential TEE loops
  106. * between two hosts.
  107. *
  108. * Set %IP_DF so that the original source is notified of a potentially
  109. * decreased MTU on the clone route. IPv6 does this too.
  110. */
  111. iph = ip_hdr(skb);
  112. iph->frag_off |= htons(IP_DF);
  113. if (par->hooknum == NF_INET_PRE_ROUTING ||
  114. par->hooknum == NF_INET_LOCAL_IN)
  115. --iph->ttl;
  116. ip_send_check(iph);
  117. if (tee_tg_route4(skb, info)) {
  118. percpu_write(tee_active, true);
  119. ip_local_out(skb);
  120. percpu_write(tee_active, false);
  121. } else {
  122. kfree_skb(skb);
  123. }
  124. return XT_CONTINUE;
  125. }
  126. #ifdef WITH_IPV6
  127. static bool
  128. tee_tg_route6(struct sk_buff *skb, const struct xt_tee_tginfo *info)
  129. {
  130. const struct ipv6hdr *iph = ipv6_hdr(skb);
  131. struct net *net = pick_net(skb);
  132. struct dst_entry *dst;
  133. struct flowi6 fl6;
  134. memset(&fl6, 0, sizeof(fl6));
  135. if (info->priv) {
  136. if (info->priv->oif == -1)
  137. return false;
  138. fl6.flowi6_oif = info->priv->oif;
  139. }
  140. fl6.daddr = info->gw.in6;
  141. fl6.flowlabel = ((iph->flow_lbl[0] & 0xF) << 16) |
  142. (iph->flow_lbl[1] << 8) | iph->flow_lbl[2];
  143. dst = ip6_route_output(net, NULL, &fl6);
  144. if (dst == NULL)
  145. return false;
  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 (percpu_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. percpu_write(tee_active, true);
  174. ip6_local_out(skb);
  175. percpu_write(tee_active, false);
  176. } else {
  177. kfree_skb(skb);
  178. }
  179. return XT_CONTINUE;
  180. }
  181. #endif /* WITH_IPV6 */
  182. static int tee_netdev_event(struct notifier_block *this, unsigned long event,
  183. void *ptr)
  184. {
  185. struct net_device *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. #ifdef WITH_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");