blob: a747eb475b68e174db6a0f3ebe41de8ec5ca7d61 [file] [log] [blame]
Jan Engelhardte281b192010-04-19 14:17:47 +02001/*
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>
Jan Engelhardtcd58bcd2010-04-19 16:06:52 +020015#include <linux/percpu.h>
Jan Engelhardte281b192010-04-19 14:17:47 +020016#include <linux/route.h>
17#include <linux/skbuff.h>
Patrick McHardy22265a52010-04-20 15:07:32 +020018#include <linux/notifier.h>
Jan Engelhardte281b192010-04-19 14:17:47 +020019#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
Igor Maravićc0cd1152011-12-12 02:58:24 +000028#if IS_ENABLED(CONFIG_NF_CONNTRACK)
Jan Engelhardte281b192010-04-19 14:17:47 +020029# define WITH_CONNTRACK 1
30# include <net/netfilter/nf_conntrack.h>
31#endif
Jan Engelhardte281b192010-04-19 14:17:47 +020032
Patrick McHardy22265a52010-04-20 15:07:32 +020033struct xt_tee_priv {
34 struct notifier_block notifier;
35 struct xt_tee_tginfo *tginfo;
36 int oif;
37};
38
Jan Engelhardte281b192010-04-19 14:17:47 +020039static const union nf_inet_addr tee_zero_address;
Jan Engelhardtcd58bcd2010-04-19 16:06:52 +020040static DEFINE_PER_CPU(bool, tee_active);
Jan Engelhardte281b192010-04-19 14:17:47 +020041
42static struct net *pick_net(struct sk_buff *skb)
43{
44#ifdef CONFIG_NET_NS
45 const struct dst_entry *dst;
46
47 if (skb->dev != NULL)
48 return dev_net(skb->dev);
49 dst = skb_dst(skb);
50 if (dst != NULL && dst->dev != NULL)
51 return dev_net(dst->dev);
52#endif
53 return &init_net;
54}
55
Jan Engelhardte281b192010-04-19 14:17:47 +020056static bool
57tee_tg_route4(struct sk_buff *skb, const struct xt_tee_tginfo *info)
58{
59 const struct iphdr *iph = ip_hdr(skb);
60 struct net *net = pick_net(skb);
61 struct rtable *rt;
David S. Miller9d6ec932011-03-12 01:12:47 -050062 struct flowi4 fl4;
Jan Engelhardte281b192010-04-19 14:17:47 +020063
David S. Miller9d6ec932011-03-12 01:12:47 -050064 memset(&fl4, 0, sizeof(fl4));
Patrick McHardy22265a52010-04-20 15:07:32 +020065 if (info->priv) {
66 if (info->priv->oif == -1)
67 return false;
David S. Miller9d6ec932011-03-12 01:12:47 -050068 fl4.flowi4_oif = info->priv->oif;
Patrick McHardy22265a52010-04-20 15:07:32 +020069 }
David S. Miller9d6ec932011-03-12 01:12:47 -050070 fl4.daddr = info->gw.ip;
71 fl4.flowi4_tos = RT_TOS(iph->tos);
72 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
Eric Dumazet2ad5b9e2012-10-16 22:33:29 +000073 fl4.flowi4_flags = FLOWI_FLAG_KNOWN_NH;
David S. Miller9d6ec932011-03-12 01:12:47 -050074 rt = ip_route_output_key(net, &fl4);
David S. Millerb23dd4f2011-03-02 14:31:35 -080075 if (IS_ERR(rt))
Jan Engelhardte281b192010-04-19 14:17:47 +020076 return false;
77
Eric Dumazet50636af2010-05-28 03:41:17 -070078 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -070079 skb_dst_set(skb, &rt->dst);
80 skb->dev = rt->dst.dev;
Jan Engelhardte281b192010-04-19 14:17:47 +020081 skb->protocol = htons(ETH_P_IP);
82 return true;
83}
84
85static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +020086tee_tg4(struct sk_buff *skb, const struct xt_action_param *par)
Jan Engelhardte281b192010-04-19 14:17:47 +020087{
88 const struct xt_tee_tginfo *info = par->targinfo;
89 struct iphdr *iph;
90
Alex Shi19e8d692012-05-14 14:15:31 -070091 if (__this_cpu_read(tee_active))
Jan Engelhardtcd58bcd2010-04-19 16:06:52 +020092 return XT_CONTINUE;
Jan Engelhardte281b192010-04-19 14:17:47 +020093 /*
94 * Copy the skb, and route the copy. Will later return %XT_CONTINUE for
95 * the original skb, which should continue on its way as if nothing has
96 * happened. The copy should be independently delivered to the TEE
97 * --gateway.
98 */
99 skb = pskb_copy(skb, GFP_ATOMIC);
100 if (skb == NULL)
101 return XT_CONTINUE;
102
103#ifdef WITH_CONNTRACK
104 /* Avoid counting cloned packets towards the original connection. */
105 nf_conntrack_put(skb->nfct);
Eric Dumazet5bfddbd2010-06-08 16:09:52 +0200106 skb->nfct = &nf_ct_untracked_get()->ct_general;
Jan Engelhardte281b192010-04-19 14:17:47 +0200107 skb->nfctinfo = IP_CT_NEW;
108 nf_conntrack_get(skb->nfct);
109#endif
110 /*
111 * If we are in PREROUTING/INPUT, the checksum must be recalculated
112 * since the length could have changed as a result of defragmentation.
113 *
114 * We also decrease the TTL to mitigate potential TEE loops
115 * between two hosts.
116 *
117 * Set %IP_DF so that the original source is notified of a potentially
118 * decreased MTU on the clone route. IPv6 does this too.
119 */
120 iph = ip_hdr(skb);
121 iph->frag_off |= htons(IP_DF);
122 if (par->hooknum == NF_INET_PRE_ROUTING ||
123 par->hooknum == NF_INET_LOCAL_IN)
124 --iph->ttl;
125 ip_send_check(iph);
126
Jan Engelhardtcd58bcd2010-04-19 16:06:52 +0200127 if (tee_tg_route4(skb, info)) {
Alex Shi19e8d692012-05-14 14:15:31 -0700128 __this_cpu_write(tee_active, true);
Jan Engelhardtcd58bcd2010-04-19 16:06:52 +0200129 ip_local_out(skb);
Alex Shi19e8d692012-05-14 14:15:31 -0700130 __this_cpu_write(tee_active, false);
Jan Engelhardtcd58bcd2010-04-19 16:06:52 +0200131 } else {
Jan Engelhardte281b192010-04-19 14:17:47 +0200132 kfree_skb(skb);
Jan Engelhardtcd58bcd2010-04-19 16:06:52 +0200133 }
Jan Engelhardte281b192010-04-19 14:17:47 +0200134 return XT_CONTINUE;
135}
136
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000137#if IS_ENABLED(CONFIG_IPV6)
Jan Engelhardte281b192010-04-19 14:17:47 +0200138static bool
139tee_tg_route6(struct sk_buff *skb, const struct xt_tee_tginfo *info)
140{
141 const struct ipv6hdr *iph = ipv6_hdr(skb);
142 struct net *net = pick_net(skb);
143 struct dst_entry *dst;
David S. Miller4c9483b2011-03-12 16:22:43 -0500144 struct flowi6 fl6;
Jan Engelhardte281b192010-04-19 14:17:47 +0200145
David S. Miller4c9483b2011-03-12 16:22:43 -0500146 memset(&fl6, 0, sizeof(fl6));
Patrick McHardy22265a52010-04-20 15:07:32 +0200147 if (info->priv) {
148 if (info->priv->oif == -1)
149 return false;
David S. Miller4c9483b2011-03-12 16:22:43 -0500150 fl6.flowi6_oif = info->priv->oif;
Patrick McHardy22265a52010-04-20 15:07:32 +0200151 }
David S. Miller4c9483b2011-03-12 16:22:43 -0500152 fl6.daddr = info->gw.in6;
153 fl6.flowlabel = ((iph->flow_lbl[0] & 0xF) << 16) |
Changli Gao58116622010-11-12 18:43:55 +0000154 (iph->flow_lbl[1] << 8) | iph->flow_lbl[2];
Martin KaFai Lau48e8aa62015-05-22 20:56:02 -0700155 fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
David S. Miller4c9483b2011-03-12 16:22:43 -0500156 dst = ip6_route_output(net, NULL, &fl6);
RongQing.Li5d38b1f2012-02-21 22:10:51 +0000157 if (dst->error) {
158 dst_release(dst);
Jan Engelhardte281b192010-04-19 14:17:47 +0200159 return false;
RongQing.Li5d38b1f2012-02-21 22:10:51 +0000160 }
Eric Dumazet50636af2010-05-28 03:41:17 -0700161 skb_dst_drop(skb);
Jan Engelhardte281b192010-04-19 14:17:47 +0200162 skb_dst_set(skb, dst);
163 skb->dev = dst->dev;
164 skb->protocol = htons(ETH_P_IPV6);
165 return true;
166}
167
168static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +0200169tee_tg6(struct sk_buff *skb, const struct xt_action_param *par)
Jan Engelhardte281b192010-04-19 14:17:47 +0200170{
171 const struct xt_tee_tginfo *info = par->targinfo;
172
Alex Shi19e8d692012-05-14 14:15:31 -0700173 if (__this_cpu_read(tee_active))
Jan Engelhardtcd58bcd2010-04-19 16:06:52 +0200174 return XT_CONTINUE;
Jan Engelhardte281b192010-04-19 14:17:47 +0200175 skb = pskb_copy(skb, GFP_ATOMIC);
176 if (skb == NULL)
177 return XT_CONTINUE;
178
179#ifdef WITH_CONNTRACK
180 nf_conntrack_put(skb->nfct);
Eric Dumazet5bfddbd2010-06-08 16:09:52 +0200181 skb->nfct = &nf_ct_untracked_get()->ct_general;
Jan Engelhardte281b192010-04-19 14:17:47 +0200182 skb->nfctinfo = IP_CT_NEW;
183 nf_conntrack_get(skb->nfct);
184#endif
185 if (par->hooknum == NF_INET_PRE_ROUTING ||
186 par->hooknum == NF_INET_LOCAL_IN) {
187 struct ipv6hdr *iph = ipv6_hdr(skb);
188 --iph->hop_limit;
189 }
Jan Engelhardtcd58bcd2010-04-19 16:06:52 +0200190 if (tee_tg_route6(skb, info)) {
Alex Shi19e8d692012-05-14 14:15:31 -0700191 __this_cpu_write(tee_active, true);
Jan Engelhardtcd58bcd2010-04-19 16:06:52 +0200192 ip6_local_out(skb);
Alex Shi19e8d692012-05-14 14:15:31 -0700193 __this_cpu_write(tee_active, false);
Jan Engelhardtcd58bcd2010-04-19 16:06:52 +0200194 } else {
Jan Engelhardte281b192010-04-19 14:17:47 +0200195 kfree_skb(skb);
Jan Engelhardtcd58bcd2010-04-19 16:06:52 +0200196 }
Jan Engelhardte281b192010-04-19 14:17:47 +0200197 return XT_CONTINUE;
198}
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000199#endif
Jan Engelhardte281b192010-04-19 14:17:47 +0200200
Patrick McHardy22265a52010-04-20 15:07:32 +0200201static int tee_netdev_event(struct notifier_block *this, unsigned long event,
202 void *ptr)
203{
Jiri Pirko351638e2013-05-28 01:30:21 +0000204 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Patrick McHardy22265a52010-04-20 15:07:32 +0200205 struct xt_tee_priv *priv;
206
207 priv = container_of(this, struct xt_tee_priv, notifier);
208 switch (event) {
209 case NETDEV_REGISTER:
210 if (!strcmp(dev->name, priv->tginfo->oif))
211 priv->oif = dev->ifindex;
212 break;
213 case NETDEV_UNREGISTER:
214 if (dev->ifindex == priv->oif)
215 priv->oif = -1;
216 break;
217 case NETDEV_CHANGENAME:
218 if (!strcmp(dev->name, priv->tginfo->oif))
219 priv->oif = dev->ifindex;
220 else if (dev->ifindex == priv->oif)
221 priv->oif = -1;
222 break;
223 }
224
225 return NOTIFY_DONE;
226}
227
Jan Engelhardte281b192010-04-19 14:17:47 +0200228static int tee_tg_check(const struct xt_tgchk_param *par)
229{
Patrick McHardy22265a52010-04-20 15:07:32 +0200230 struct xt_tee_tginfo *info = par->targinfo;
231 struct xt_tee_priv *priv;
Jan Engelhardte281b192010-04-19 14:17:47 +0200232
Jan Engelhardte281b192010-04-19 14:17:47 +0200233 /* 0.0.0.0 and :: not allowed */
Patrick McHardy22265a52010-04-20 15:07:32 +0200234 if (memcmp(&info->gw, &tee_zero_address,
235 sizeof(tee_zero_address)) == 0)
236 return -EINVAL;
237
238 if (info->oif[0]) {
239 if (info->oif[sizeof(info->oif)-1] != '\0')
240 return -EINVAL;
241
242 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
243 if (priv == NULL)
244 return -ENOMEM;
245
246 priv->tginfo = info;
247 priv->oif = -1;
248 priv->notifier.notifier_call = tee_netdev_event;
249 info->priv = priv;
250
251 register_netdevice_notifier(&priv->notifier);
252 } else
253 info->priv = NULL;
254
255 return 0;
256}
257
258static void tee_tg_destroy(const struct xt_tgdtor_param *par)
259{
260 struct xt_tee_tginfo *info = par->targinfo;
261
262 if (info->priv) {
263 unregister_netdevice_notifier(&info->priv->notifier);
264 kfree(info->priv);
265 }
Jan Engelhardte281b192010-04-19 14:17:47 +0200266}
267
268static struct xt_target tee_tg_reg[] __read_mostly = {
269 {
270 .name = "TEE",
271 .revision = 1,
272 .family = NFPROTO_IPV4,
273 .target = tee_tg4,
274 .targetsize = sizeof(struct xt_tee_tginfo),
275 .checkentry = tee_tg_check,
Patrick McHardy22265a52010-04-20 15:07:32 +0200276 .destroy = tee_tg_destroy,
Jan Engelhardte281b192010-04-19 14:17:47 +0200277 .me = THIS_MODULE,
278 },
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000279#if IS_ENABLED(CONFIG_IPV6)
Jan Engelhardte281b192010-04-19 14:17:47 +0200280 {
281 .name = "TEE",
282 .revision = 1,
283 .family = NFPROTO_IPV6,
284 .target = tee_tg6,
285 .targetsize = sizeof(struct xt_tee_tginfo),
286 .checkentry = tee_tg_check,
Patrick McHardy22265a52010-04-20 15:07:32 +0200287 .destroy = tee_tg_destroy,
Jan Engelhardte281b192010-04-19 14:17:47 +0200288 .me = THIS_MODULE,
289 },
290#endif
291};
292
293static int __init tee_tg_init(void)
294{
295 return xt_register_targets(tee_tg_reg, ARRAY_SIZE(tee_tg_reg));
296}
297
298static void __exit tee_tg_exit(void)
299{
300 xt_unregister_targets(tee_tg_reg, ARRAY_SIZE(tee_tg_reg));
301}
302
303module_init(tee_tg_init);
304module_exit(tee_tg_exit);
305MODULE_AUTHOR("Sebastian Claßen <sebastian.classen@freenet.ag>");
306MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
307MODULE_DESCRIPTION("Xtables: Reroute packet copy");
308MODULE_LICENSE("GPL");
309MODULE_ALIAS("ipt_TEE");
310MODULE_ALIAS("ip6t_TEE");