Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 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> |
Jan Engelhardt | cd58bcd | 2010-04-19 16:06:52 +0200 | [diff] [blame] | 15 | #include <linux/percpu.h> |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 16 | #include <linux/route.h> |
| 17 | #include <linux/skbuff.h> |
Patrick McHardy | 22265a5 | 2010-04-20 15:07:32 +0200 | [diff] [blame] | 18 | #include <linux/notifier.h> |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 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 | |
Igor Maravić | c0cd115 | 2011-12-12 02:58:24 +0000 | [diff] [blame] | 28 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 29 | # define WITH_CONNTRACK 1 |
| 30 | # include <net/netfilter/nf_conntrack.h> |
| 31 | #endif |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 32 | |
Patrick McHardy | 22265a5 | 2010-04-20 15:07:32 +0200 | [diff] [blame] | 33 | struct xt_tee_priv { |
| 34 | struct notifier_block notifier; |
| 35 | struct xt_tee_tginfo *tginfo; |
| 36 | int oif; |
| 37 | }; |
| 38 | |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 39 | static const union nf_inet_addr tee_zero_address; |
Jan Engelhardt | cd58bcd | 2010-04-19 16:06:52 +0200 | [diff] [blame] | 40 | static DEFINE_PER_CPU(bool, tee_active); |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 41 | |
| 42 | static 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 Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 56 | static bool |
| 57 | tee_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. Miller | 9d6ec93 | 2011-03-12 01:12:47 -0500 | [diff] [blame] | 62 | struct flowi4 fl4; |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 63 | |
David S. Miller | 9d6ec93 | 2011-03-12 01:12:47 -0500 | [diff] [blame] | 64 | memset(&fl4, 0, sizeof(fl4)); |
Patrick McHardy | 22265a5 | 2010-04-20 15:07:32 +0200 | [diff] [blame] | 65 | if (info->priv) { |
| 66 | if (info->priv->oif == -1) |
| 67 | return false; |
David S. Miller | 9d6ec93 | 2011-03-12 01:12:47 -0500 | [diff] [blame] | 68 | fl4.flowi4_oif = info->priv->oif; |
Patrick McHardy | 22265a5 | 2010-04-20 15:07:32 +0200 | [diff] [blame] | 69 | } |
David S. Miller | 9d6ec93 | 2011-03-12 01:12:47 -0500 | [diff] [blame] | 70 | fl4.daddr = info->gw.ip; |
| 71 | fl4.flowi4_tos = RT_TOS(iph->tos); |
| 72 | fl4.flowi4_scope = RT_SCOPE_UNIVERSE; |
| 73 | rt = ip_route_output_key(net, &fl4); |
David S. Miller | b23dd4f | 2011-03-02 14:31:35 -0800 | [diff] [blame] | 74 | if (IS_ERR(rt)) |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 75 | return false; |
| 76 | |
Eric Dumazet | 50636af | 2010-05-28 03:41:17 -0700 | [diff] [blame] | 77 | skb_dst_drop(skb); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 78 | skb_dst_set(skb, &rt->dst); |
| 79 | skb->dev = rt->dst.dev; |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 80 | skb->protocol = htons(ETH_P_IP); |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | static unsigned int |
Jan Engelhardt | 4b560b4 | 2009-07-05 19:43:26 +0200 | [diff] [blame] | 85 | tee_tg4(struct sk_buff *skb, const struct xt_action_param *par) |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 86 | { |
| 87 | const struct xt_tee_tginfo *info = par->targinfo; |
| 88 | struct iphdr *iph; |
| 89 | |
Alex Shi | 19e8d69 | 2012-05-14 14:15:31 -0700 | [diff] [blame] | 90 | if (__this_cpu_read(tee_active)) |
Jan Engelhardt | cd58bcd | 2010-04-19 16:06:52 +0200 | [diff] [blame] | 91 | return XT_CONTINUE; |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 92 | /* |
| 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 Dumazet | 5bfddbd | 2010-06-08 16:09:52 +0200 | [diff] [blame] | 105 | skb->nfct = &nf_ct_untracked_get()->ct_general; |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 106 | 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 Engelhardt | cd58bcd | 2010-04-19 16:06:52 +0200 | [diff] [blame] | 126 | if (tee_tg_route4(skb, info)) { |
Alex Shi | 19e8d69 | 2012-05-14 14:15:31 -0700 | [diff] [blame] | 127 | __this_cpu_write(tee_active, true); |
Jan Engelhardt | cd58bcd | 2010-04-19 16:06:52 +0200 | [diff] [blame] | 128 | ip_local_out(skb); |
Alex Shi | 19e8d69 | 2012-05-14 14:15:31 -0700 | [diff] [blame] | 129 | __this_cpu_write(tee_active, false); |
Jan Engelhardt | cd58bcd | 2010-04-19 16:06:52 +0200 | [diff] [blame] | 130 | } else { |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 131 | kfree_skb(skb); |
Jan Engelhardt | cd58bcd | 2010-04-19 16:06:52 +0200 | [diff] [blame] | 132 | } |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 133 | return XT_CONTINUE; |
| 134 | } |
| 135 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 136 | #if IS_ENABLED(CONFIG_IPV6) |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 137 | static bool |
| 138 | tee_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. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 143 | struct flowi6 fl6; |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 144 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 145 | memset(&fl6, 0, sizeof(fl6)); |
Patrick McHardy | 22265a5 | 2010-04-20 15:07:32 +0200 | [diff] [blame] | 146 | if (info->priv) { |
| 147 | if (info->priv->oif == -1) |
| 148 | return false; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 149 | fl6.flowi6_oif = info->priv->oif; |
Patrick McHardy | 22265a5 | 2010-04-20 15:07:32 +0200 | [diff] [blame] | 150 | } |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 151 | fl6.daddr = info->gw.in6; |
| 152 | fl6.flowlabel = ((iph->flow_lbl[0] & 0xF) << 16) | |
Changli Gao | 5811662 | 2010-11-12 18:43:55 +0000 | [diff] [blame] | 153 | (iph->flow_lbl[1] << 8) | iph->flow_lbl[2]; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 154 | dst = ip6_route_output(net, NULL, &fl6); |
RongQing.Li | 5d38b1f | 2012-02-21 22:10:51 +0000 | [diff] [blame] | 155 | if (dst->error) { |
| 156 | dst_release(dst); |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 157 | return false; |
RongQing.Li | 5d38b1f | 2012-02-21 22:10:51 +0000 | [diff] [blame] | 158 | } |
Eric Dumazet | 50636af | 2010-05-28 03:41:17 -0700 | [diff] [blame] | 159 | skb_dst_drop(skb); |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 160 | skb_dst_set(skb, dst); |
| 161 | skb->dev = dst->dev; |
| 162 | skb->protocol = htons(ETH_P_IPV6); |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | static unsigned int |
Jan Engelhardt | 4b560b4 | 2009-07-05 19:43:26 +0200 | [diff] [blame] | 167 | tee_tg6(struct sk_buff *skb, const struct xt_action_param *par) |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 168 | { |
| 169 | const struct xt_tee_tginfo *info = par->targinfo; |
| 170 | |
Alex Shi | 19e8d69 | 2012-05-14 14:15:31 -0700 | [diff] [blame] | 171 | if (__this_cpu_read(tee_active)) |
Jan Engelhardt | cd58bcd | 2010-04-19 16:06:52 +0200 | [diff] [blame] | 172 | return XT_CONTINUE; |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 173 | skb = pskb_copy(skb, GFP_ATOMIC); |
| 174 | if (skb == NULL) |
| 175 | return XT_CONTINUE; |
| 176 | |
| 177 | #ifdef WITH_CONNTRACK |
| 178 | nf_conntrack_put(skb->nfct); |
Eric Dumazet | 5bfddbd | 2010-06-08 16:09:52 +0200 | [diff] [blame] | 179 | skb->nfct = &nf_ct_untracked_get()->ct_general; |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 180 | skb->nfctinfo = IP_CT_NEW; |
| 181 | nf_conntrack_get(skb->nfct); |
| 182 | #endif |
| 183 | if (par->hooknum == NF_INET_PRE_ROUTING || |
| 184 | par->hooknum == NF_INET_LOCAL_IN) { |
| 185 | struct ipv6hdr *iph = ipv6_hdr(skb); |
| 186 | --iph->hop_limit; |
| 187 | } |
Jan Engelhardt | cd58bcd | 2010-04-19 16:06:52 +0200 | [diff] [blame] | 188 | if (tee_tg_route6(skb, info)) { |
Alex Shi | 19e8d69 | 2012-05-14 14:15:31 -0700 | [diff] [blame] | 189 | __this_cpu_write(tee_active, true); |
Jan Engelhardt | cd58bcd | 2010-04-19 16:06:52 +0200 | [diff] [blame] | 190 | ip6_local_out(skb); |
Alex Shi | 19e8d69 | 2012-05-14 14:15:31 -0700 | [diff] [blame] | 191 | __this_cpu_write(tee_active, false); |
Jan Engelhardt | cd58bcd | 2010-04-19 16:06:52 +0200 | [diff] [blame] | 192 | } else { |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 193 | kfree_skb(skb); |
Jan Engelhardt | cd58bcd | 2010-04-19 16:06:52 +0200 | [diff] [blame] | 194 | } |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 195 | return XT_CONTINUE; |
| 196 | } |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 197 | #endif |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 198 | |
Patrick McHardy | 22265a5 | 2010-04-20 15:07:32 +0200 | [diff] [blame] | 199 | static int tee_netdev_event(struct notifier_block *this, unsigned long event, |
| 200 | void *ptr) |
| 201 | { |
| 202 | struct net_device *dev = ptr; |
| 203 | struct xt_tee_priv *priv; |
| 204 | |
| 205 | priv = container_of(this, struct xt_tee_priv, notifier); |
| 206 | switch (event) { |
| 207 | case NETDEV_REGISTER: |
| 208 | if (!strcmp(dev->name, priv->tginfo->oif)) |
| 209 | priv->oif = dev->ifindex; |
| 210 | break; |
| 211 | case NETDEV_UNREGISTER: |
| 212 | if (dev->ifindex == priv->oif) |
| 213 | priv->oif = -1; |
| 214 | break; |
| 215 | case NETDEV_CHANGENAME: |
| 216 | if (!strcmp(dev->name, priv->tginfo->oif)) |
| 217 | priv->oif = dev->ifindex; |
| 218 | else if (dev->ifindex == priv->oif) |
| 219 | priv->oif = -1; |
| 220 | break; |
| 221 | } |
| 222 | |
| 223 | return NOTIFY_DONE; |
| 224 | } |
| 225 | |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 226 | static int tee_tg_check(const struct xt_tgchk_param *par) |
| 227 | { |
Patrick McHardy | 22265a5 | 2010-04-20 15:07:32 +0200 | [diff] [blame] | 228 | struct xt_tee_tginfo *info = par->targinfo; |
| 229 | struct xt_tee_priv *priv; |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 230 | |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 231 | /* 0.0.0.0 and :: not allowed */ |
Patrick McHardy | 22265a5 | 2010-04-20 15:07:32 +0200 | [diff] [blame] | 232 | if (memcmp(&info->gw, &tee_zero_address, |
| 233 | sizeof(tee_zero_address)) == 0) |
| 234 | return -EINVAL; |
| 235 | |
| 236 | if (info->oif[0]) { |
| 237 | if (info->oif[sizeof(info->oif)-1] != '\0') |
| 238 | return -EINVAL; |
| 239 | |
| 240 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 241 | if (priv == NULL) |
| 242 | return -ENOMEM; |
| 243 | |
| 244 | priv->tginfo = info; |
| 245 | priv->oif = -1; |
| 246 | priv->notifier.notifier_call = tee_netdev_event; |
| 247 | info->priv = priv; |
| 248 | |
| 249 | register_netdevice_notifier(&priv->notifier); |
| 250 | } else |
| 251 | info->priv = NULL; |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static void tee_tg_destroy(const struct xt_tgdtor_param *par) |
| 257 | { |
| 258 | struct xt_tee_tginfo *info = par->targinfo; |
| 259 | |
| 260 | if (info->priv) { |
| 261 | unregister_netdevice_notifier(&info->priv->notifier); |
| 262 | kfree(info->priv); |
| 263 | } |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static struct xt_target tee_tg_reg[] __read_mostly = { |
| 267 | { |
| 268 | .name = "TEE", |
| 269 | .revision = 1, |
| 270 | .family = NFPROTO_IPV4, |
| 271 | .target = tee_tg4, |
| 272 | .targetsize = sizeof(struct xt_tee_tginfo), |
| 273 | .checkentry = tee_tg_check, |
Patrick McHardy | 22265a5 | 2010-04-20 15:07:32 +0200 | [diff] [blame] | 274 | .destroy = tee_tg_destroy, |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 275 | .me = THIS_MODULE, |
| 276 | }, |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 277 | #if IS_ENABLED(CONFIG_IPV6) |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 278 | { |
| 279 | .name = "TEE", |
| 280 | .revision = 1, |
| 281 | .family = NFPROTO_IPV6, |
| 282 | .target = tee_tg6, |
| 283 | .targetsize = sizeof(struct xt_tee_tginfo), |
| 284 | .checkentry = tee_tg_check, |
Patrick McHardy | 22265a5 | 2010-04-20 15:07:32 +0200 | [diff] [blame] | 285 | .destroy = tee_tg_destroy, |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 286 | .me = THIS_MODULE, |
| 287 | }, |
| 288 | #endif |
| 289 | }; |
| 290 | |
| 291 | static int __init tee_tg_init(void) |
| 292 | { |
| 293 | return xt_register_targets(tee_tg_reg, ARRAY_SIZE(tee_tg_reg)); |
| 294 | } |
| 295 | |
| 296 | static void __exit tee_tg_exit(void) |
| 297 | { |
| 298 | xt_unregister_targets(tee_tg_reg, ARRAY_SIZE(tee_tg_reg)); |
| 299 | } |
| 300 | |
| 301 | module_init(tee_tg_init); |
| 302 | module_exit(tee_tg_exit); |
| 303 | MODULE_AUTHOR("Sebastian Claßen <sebastian.classen@freenet.ag>"); |
| 304 | MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>"); |
| 305 | MODULE_DESCRIPTION("Xtables: Reroute packet copy"); |
| 306 | MODULE_LICENSE("GPL"); |
| 307 | MODULE_ALIAS("ipt_TEE"); |
| 308 | MODULE_ALIAS("ip6t_TEE"); |