blob: c5d6556dbc5e407cffca198ac5fe66b97a0cb908 [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;
40
41static struct net *pick_net(struct sk_buff *skb)
42{
43#ifdef CONFIG_NET_NS
44 const struct dst_entry *dst;
45
46 if (skb->dev != NULL)
47 return dev_net(skb->dev);
48 dst = skb_dst(skb);
49 if (dst != NULL && dst->dev != NULL)
50 return dev_net(dst->dev);
51#endif
52 return &init_net;
53}
54
Jan Engelhardte281b192010-04-19 14:17:47 +020055static bool
56tee_tg_route4(struct sk_buff *skb, const struct xt_tee_tginfo *info)
57{
58 const struct iphdr *iph = ip_hdr(skb);
59 struct net *net = pick_net(skb);
60 struct rtable *rt;
David S. Miller9d6ec932011-03-12 01:12:47 -050061 struct flowi4 fl4;
Jan Engelhardte281b192010-04-19 14:17:47 +020062
David S. Miller9d6ec932011-03-12 01:12:47 -050063 memset(&fl4, 0, sizeof(fl4));
Patrick McHardy22265a52010-04-20 15:07:32 +020064 if (info->priv) {
65 if (info->priv->oif == -1)
66 return false;
David S. Miller9d6ec932011-03-12 01:12:47 -050067 fl4.flowi4_oif = info->priv->oif;
Patrick McHardy22265a52010-04-20 15:07:32 +020068 }
David S. Miller9d6ec932011-03-12 01:12:47 -050069 fl4.daddr = info->gw.ip;
70 fl4.flowi4_tos = RT_TOS(iph->tos);
71 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
Eric Dumazet2ad5b9e2012-10-16 22:33:29 +000072 fl4.flowi4_flags = FLOWI_FLAG_KNOWN_NH;
David S. Miller9d6ec932011-03-12 01:12:47 -050073 rt = ip_route_output_key(net, &fl4);
David S. Millerb23dd4f2011-03-02 14:31:35 -080074 if (IS_ERR(rt))
Jan Engelhardte281b192010-04-19 14:17:47 +020075 return false;
76
Eric Dumazet50636af2010-05-28 03:41:17 -070077 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -070078 skb_dst_set(skb, &rt->dst);
79 skb->dev = rt->dst.dev;
Jan Engelhardte281b192010-04-19 14:17:47 +020080 skb->protocol = htons(ETH_P_IP);
81 return true;
82}
83
84static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +020085tee_tg4(struct sk_buff *skb, const struct xt_action_param *par)
Jan Engelhardte281b192010-04-19 14:17:47 +020086{
87 const struct xt_tee_tginfo *info = par->targinfo;
88 struct iphdr *iph;
89
Florian Westphale7c88992015-07-14 17:51:07 +020090 if (__this_cpu_read(nf_skb_duplicated))
Jan Engelhardtcd58bcd2010-04-19 16:06:52 +020091 return XT_CONTINUE;
Jan Engelhardte281b192010-04-19 14:17:47 +020092 /*
93 * Copy the skb, and route the copy. Will later return %XT_CONTINUE for
94 * the original skb, which should continue on its way as if nothing has
95 * happened. The copy should be independently delivered to the TEE
96 * --gateway.
97 */
98 skb = pskb_copy(skb, GFP_ATOMIC);
99 if (skb == NULL)
100 return XT_CONTINUE;
101
102#ifdef WITH_CONNTRACK
103 /* Avoid counting cloned packets towards the original connection. */
104 nf_conntrack_put(skb->nfct);
Eric Dumazet5bfddbd2010-06-08 16:09:52 +0200105 skb->nfct = &nf_ct_untracked_get()->ct_general;
Jan Engelhardte281b192010-04-19 14:17:47 +0200106 skb->nfctinfo = IP_CT_NEW;
107 nf_conntrack_get(skb->nfct);
108#endif
109 /*
110 * If we are in PREROUTING/INPUT, the checksum must be recalculated
111 * since the length could have changed as a result of defragmentation.
112 *
113 * We also decrease the TTL to mitigate potential TEE loops
114 * between two hosts.
115 *
116 * Set %IP_DF so that the original source is notified of a potentially
117 * decreased MTU on the clone route. IPv6 does this too.
118 */
119 iph = ip_hdr(skb);
120 iph->frag_off |= htons(IP_DF);
121 if (par->hooknum == NF_INET_PRE_ROUTING ||
122 par->hooknum == NF_INET_LOCAL_IN)
123 --iph->ttl;
124 ip_send_check(iph);
125
Jan Engelhardtcd58bcd2010-04-19 16:06:52 +0200126 if (tee_tg_route4(skb, info)) {
Florian Westphale7c88992015-07-14 17:51:07 +0200127 __this_cpu_write(nf_skb_duplicated, true);
Jan Engelhardtcd58bcd2010-04-19 16:06:52 +0200128 ip_local_out(skb);
Florian Westphale7c88992015-07-14 17:51:07 +0200129 __this_cpu_write(nf_skb_duplicated, false);
Jan Engelhardtcd58bcd2010-04-19 16:06:52 +0200130 } else {
Jan Engelhardte281b192010-04-19 14:17:47 +0200131 kfree_skb(skb);
Jan Engelhardtcd58bcd2010-04-19 16:06:52 +0200132 }
Jan Engelhardte281b192010-04-19 14:17:47 +0200133 return XT_CONTINUE;
134}
135
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000136#if IS_ENABLED(CONFIG_IPV6)
Jan Engelhardte281b192010-04-19 14:17:47 +0200137static bool
138tee_tg_route6(struct sk_buff *skb, const struct xt_tee_tginfo *info)
139{
140 const struct ipv6hdr *iph = ipv6_hdr(skb);
141 struct net *net = pick_net(skb);
142 struct dst_entry *dst;
David S. Miller4c9483b2011-03-12 16:22:43 -0500143 struct flowi6 fl6;
Jan Engelhardte281b192010-04-19 14:17:47 +0200144
David S. Miller4c9483b2011-03-12 16:22:43 -0500145 memset(&fl6, 0, sizeof(fl6));
Patrick McHardy22265a52010-04-20 15:07:32 +0200146 if (info->priv) {
147 if (info->priv->oif == -1)
148 return false;
David S. Miller4c9483b2011-03-12 16:22:43 -0500149 fl6.flowi6_oif = info->priv->oif;
Patrick McHardy22265a52010-04-20 15:07:32 +0200150 }
David S. Miller4c9483b2011-03-12 16:22:43 -0500151 fl6.daddr = info->gw.in6;
152 fl6.flowlabel = ((iph->flow_lbl[0] & 0xF) << 16) |
Changli Gao58116622010-11-12 18:43:55 +0000153 (iph->flow_lbl[1] << 8) | iph->flow_lbl[2];
Martin KaFai Lau48e8aa62015-05-22 20:56:02 -0700154 fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
David S. Miller4c9483b2011-03-12 16:22:43 -0500155 dst = ip6_route_output(net, NULL, &fl6);
RongQing.Li5d38b1f2012-02-21 22:10:51 +0000156 if (dst->error) {
157 dst_release(dst);
Jan Engelhardte281b192010-04-19 14:17:47 +0200158 return false;
RongQing.Li5d38b1f2012-02-21 22:10:51 +0000159 }
Eric Dumazet50636af2010-05-28 03:41:17 -0700160 skb_dst_drop(skb);
Jan Engelhardte281b192010-04-19 14:17:47 +0200161 skb_dst_set(skb, dst);
162 skb->dev = dst->dev;
163 skb->protocol = htons(ETH_P_IPV6);
164 return true;
165}
166
167static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +0200168tee_tg6(struct sk_buff *skb, const struct xt_action_param *par)
Jan Engelhardte281b192010-04-19 14:17:47 +0200169{
170 const struct xt_tee_tginfo *info = par->targinfo;
171
Florian Westphale7c88992015-07-14 17:51:07 +0200172 if (__this_cpu_read(nf_skb_duplicated))
Jan Engelhardtcd58bcd2010-04-19 16:06:52 +0200173 return XT_CONTINUE;
Jan Engelhardte281b192010-04-19 14:17:47 +0200174 skb = pskb_copy(skb, GFP_ATOMIC);
175 if (skb == NULL)
176 return XT_CONTINUE;
177
178#ifdef WITH_CONNTRACK
179 nf_conntrack_put(skb->nfct);
Eric Dumazet5bfddbd2010-06-08 16:09:52 +0200180 skb->nfct = &nf_ct_untracked_get()->ct_general;
Jan Engelhardte281b192010-04-19 14:17:47 +0200181 skb->nfctinfo = IP_CT_NEW;
182 nf_conntrack_get(skb->nfct);
183#endif
184 if (par->hooknum == NF_INET_PRE_ROUTING ||
185 par->hooknum == NF_INET_LOCAL_IN) {
186 struct ipv6hdr *iph = ipv6_hdr(skb);
187 --iph->hop_limit;
188 }
Jan Engelhardtcd58bcd2010-04-19 16:06:52 +0200189 if (tee_tg_route6(skb, info)) {
Florian Westphale7c88992015-07-14 17:51:07 +0200190 __this_cpu_write(nf_skb_duplicated, true);
Jan Engelhardtcd58bcd2010-04-19 16:06:52 +0200191 ip6_local_out(skb);
Florian Westphale7c88992015-07-14 17:51:07 +0200192 __this_cpu_write(nf_skb_duplicated, false);
Jan Engelhardtcd58bcd2010-04-19 16:06:52 +0200193 } else {
Jan Engelhardte281b192010-04-19 14:17:47 +0200194 kfree_skb(skb);
Jan Engelhardtcd58bcd2010-04-19 16:06:52 +0200195 }
Jan Engelhardte281b192010-04-19 14:17:47 +0200196 return XT_CONTINUE;
197}
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000198#endif
Jan Engelhardte281b192010-04-19 14:17:47 +0200199
Patrick McHardy22265a52010-04-20 15:07:32 +0200200static int tee_netdev_event(struct notifier_block *this, unsigned long event,
201 void *ptr)
202{
Jiri Pirko351638e2013-05-28 01:30:21 +0000203 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Patrick McHardy22265a52010-04-20 15:07:32 +0200204 struct xt_tee_priv *priv;
205
206 priv = container_of(this, struct xt_tee_priv, notifier);
207 switch (event) {
208 case NETDEV_REGISTER:
209 if (!strcmp(dev->name, priv->tginfo->oif))
210 priv->oif = dev->ifindex;
211 break;
212 case NETDEV_UNREGISTER:
213 if (dev->ifindex == priv->oif)
214 priv->oif = -1;
215 break;
216 case NETDEV_CHANGENAME:
217 if (!strcmp(dev->name, priv->tginfo->oif))
218 priv->oif = dev->ifindex;
219 else if (dev->ifindex == priv->oif)
220 priv->oif = -1;
221 break;
222 }
223
224 return NOTIFY_DONE;
225}
226
Jan Engelhardte281b192010-04-19 14:17:47 +0200227static int tee_tg_check(const struct xt_tgchk_param *par)
228{
Patrick McHardy22265a52010-04-20 15:07:32 +0200229 struct xt_tee_tginfo *info = par->targinfo;
230 struct xt_tee_priv *priv;
Jan Engelhardte281b192010-04-19 14:17:47 +0200231
Jan Engelhardte281b192010-04-19 14:17:47 +0200232 /* 0.0.0.0 and :: not allowed */
Patrick McHardy22265a52010-04-20 15:07:32 +0200233 if (memcmp(&info->gw, &tee_zero_address,
234 sizeof(tee_zero_address)) == 0)
235 return -EINVAL;
236
237 if (info->oif[0]) {
238 if (info->oif[sizeof(info->oif)-1] != '\0')
239 return -EINVAL;
240
241 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
242 if (priv == NULL)
243 return -ENOMEM;
244
245 priv->tginfo = info;
246 priv->oif = -1;
247 priv->notifier.notifier_call = tee_netdev_event;
248 info->priv = priv;
249
250 register_netdevice_notifier(&priv->notifier);
251 } else
252 info->priv = NULL;
253
Florian Westphaldcebd312015-07-14 17:51:09 +0200254 static_key_slow_inc(&xt_tee_enabled);
Patrick McHardy22265a52010-04-20 15:07:32 +0200255 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 }
Florian Westphaldcebd312015-07-14 17:51:09 +0200266 static_key_slow_dec(&xt_tee_enabled);
Jan Engelhardte281b192010-04-19 14:17:47 +0200267}
268
269static struct xt_target tee_tg_reg[] __read_mostly = {
270 {
271 .name = "TEE",
272 .revision = 1,
273 .family = NFPROTO_IPV4,
274 .target = tee_tg4,
275 .targetsize = sizeof(struct xt_tee_tginfo),
276 .checkentry = tee_tg_check,
Patrick McHardy22265a52010-04-20 15:07:32 +0200277 .destroy = tee_tg_destroy,
Jan Engelhardte281b192010-04-19 14:17:47 +0200278 .me = THIS_MODULE,
279 },
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000280#if IS_ENABLED(CONFIG_IPV6)
Jan Engelhardte281b192010-04-19 14:17:47 +0200281 {
282 .name = "TEE",
283 .revision = 1,
284 .family = NFPROTO_IPV6,
285 .target = tee_tg6,
286 .targetsize = sizeof(struct xt_tee_tginfo),
287 .checkentry = tee_tg_check,
Patrick McHardy22265a52010-04-20 15:07:32 +0200288 .destroy = tee_tg_destroy,
Jan Engelhardte281b192010-04-19 14:17:47 +0200289 .me = THIS_MODULE,
290 },
291#endif
292};
293
294static int __init tee_tg_init(void)
295{
296 return xt_register_targets(tee_tg_reg, ARRAY_SIZE(tee_tg_reg));
297}
298
299static void __exit tee_tg_exit(void)
300{
301 xt_unregister_targets(tee_tg_reg, ARRAY_SIZE(tee_tg_reg));
302}
303
304module_init(tee_tg_init);
305module_exit(tee_tg_exit);
306MODULE_AUTHOR("Sebastian Claßen <sebastian.classen@freenet.ag>");
307MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
308MODULE_DESCRIPTION("Xtables: Reroute packet copy");
309MODULE_LICENSE("GPL");
310MODULE_ALIAS("ipt_TEE");
311MODULE_ALIAS("ip6t_TEE");