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 | */ |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 13 | #include <linux/module.h> |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 14 | #include <linux/skbuff.h> |
Pablo Neira Ayuso | bbde9fc | 2015-05-31 17:54:44 +0200 | [diff] [blame] | 15 | #include <linux/route.h> |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 16 | #include <linux/netfilter/x_tables.h> |
Pablo Neira Ayuso | bbde9fc | 2015-05-31 17:54:44 +0200 | [diff] [blame] | 17 | #include <net/route.h> |
| 18 | #include <net/netfilter/ipv4/nf_dup_ipv4.h> |
| 19 | #include <net/netfilter/ipv6/nf_dup_ipv6.h> |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 20 | #include <linux/netfilter/xt_TEE.h> |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 21 | |
Patrick McHardy | 22265a5 | 2010-04-20 15:07:32 +0200 | [diff] [blame] | 22 | struct xt_tee_priv { |
| 23 | struct notifier_block notifier; |
| 24 | struct xt_tee_tginfo *tginfo; |
| 25 | int oif; |
| 26 | }; |
| 27 | |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 28 | static const union nf_inet_addr tee_zero_address; |
| 29 | |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 30 | static unsigned int |
Jan Engelhardt | 4b560b4 | 2009-07-05 19:43:26 +0200 | [diff] [blame] | 31 | tee_tg4(struct sk_buff *skb, const struct xt_action_param *par) |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 32 | { |
| 33 | const struct xt_tee_tginfo *info = par->targinfo; |
Eric Dumazet | 45efccd | 2015-10-19 18:02:01 -0700 | [diff] [blame] | 34 | int oif = info->priv ? info->priv->oif : 0; |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 35 | |
David S. Miller | d9c7dbc | 2015-11-04 20:47:50 -0500 | [diff] [blame] | 36 | nf_dup_ipv4(par->net, skb, par->hooknum, &info->gw.in, oif); |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 37 | |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 38 | return XT_CONTINUE; |
| 39 | } |
| 40 | |
Arnd Bergmann | 08a7f5d | 2016-02-05 10:20:21 +0100 | [diff] [blame^] | 41 | #if IS_ENABLED(CONFIG_IPV6) |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 42 | static unsigned int |
Jan Engelhardt | 4b560b4 | 2009-07-05 19:43:26 +0200 | [diff] [blame] | 43 | tee_tg6(struct sk_buff *skb, const struct xt_action_param *par) |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 44 | { |
| 45 | const struct xt_tee_tginfo *info = par->targinfo; |
Eric Dumazet | 45efccd | 2015-10-19 18:02:01 -0700 | [diff] [blame] | 46 | int oif = info->priv ? info->priv->oif : 0; |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 47 | |
David S. Miller | d9c7dbc | 2015-11-04 20:47:50 -0500 | [diff] [blame] | 48 | nf_dup_ipv6(par->net, skb, par->hooknum, &info->gw.in6, oif); |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 49 | |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 50 | return XT_CONTINUE; |
| 51 | } |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 52 | #endif |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 53 | |
Patrick McHardy | 22265a5 | 2010-04-20 15:07:32 +0200 | [diff] [blame] | 54 | static int tee_netdev_event(struct notifier_block *this, unsigned long event, |
| 55 | void *ptr) |
| 56 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 57 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
Patrick McHardy | 22265a5 | 2010-04-20 15:07:32 +0200 | [diff] [blame] | 58 | struct xt_tee_priv *priv; |
| 59 | |
| 60 | priv = container_of(this, struct xt_tee_priv, notifier); |
| 61 | switch (event) { |
| 62 | case NETDEV_REGISTER: |
| 63 | if (!strcmp(dev->name, priv->tginfo->oif)) |
| 64 | priv->oif = dev->ifindex; |
| 65 | break; |
| 66 | case NETDEV_UNREGISTER: |
| 67 | if (dev->ifindex == priv->oif) |
| 68 | priv->oif = -1; |
| 69 | break; |
| 70 | case NETDEV_CHANGENAME: |
| 71 | if (!strcmp(dev->name, priv->tginfo->oif)) |
| 72 | priv->oif = dev->ifindex; |
| 73 | else if (dev->ifindex == priv->oif) |
| 74 | priv->oif = -1; |
| 75 | break; |
| 76 | } |
| 77 | |
| 78 | return NOTIFY_DONE; |
| 79 | } |
| 80 | |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 81 | static int tee_tg_check(const struct xt_tgchk_param *par) |
| 82 | { |
Patrick McHardy | 22265a5 | 2010-04-20 15:07:32 +0200 | [diff] [blame] | 83 | struct xt_tee_tginfo *info = par->targinfo; |
| 84 | struct xt_tee_priv *priv; |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 85 | |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 86 | /* 0.0.0.0 and :: not allowed */ |
Patrick McHardy | 22265a5 | 2010-04-20 15:07:32 +0200 | [diff] [blame] | 87 | if (memcmp(&info->gw, &tee_zero_address, |
| 88 | sizeof(tee_zero_address)) == 0) |
| 89 | return -EINVAL; |
| 90 | |
| 91 | if (info->oif[0]) { |
| 92 | if (info->oif[sizeof(info->oif)-1] != '\0') |
| 93 | return -EINVAL; |
| 94 | |
| 95 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 96 | if (priv == NULL) |
| 97 | return -ENOMEM; |
| 98 | |
| 99 | priv->tginfo = info; |
| 100 | priv->oif = -1; |
| 101 | priv->notifier.notifier_call = tee_netdev_event; |
| 102 | info->priv = priv; |
| 103 | |
| 104 | register_netdevice_notifier(&priv->notifier); |
| 105 | } else |
| 106 | info->priv = NULL; |
| 107 | |
Florian Westphal | dcebd31 | 2015-07-14 17:51:09 +0200 | [diff] [blame] | 108 | static_key_slow_inc(&xt_tee_enabled); |
Patrick McHardy | 22265a5 | 2010-04-20 15:07:32 +0200 | [diff] [blame] | 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static void tee_tg_destroy(const struct xt_tgdtor_param *par) |
| 113 | { |
| 114 | struct xt_tee_tginfo *info = par->targinfo; |
| 115 | |
| 116 | if (info->priv) { |
| 117 | unregister_netdevice_notifier(&info->priv->notifier); |
| 118 | kfree(info->priv); |
| 119 | } |
Florian Westphal | dcebd31 | 2015-07-14 17:51:09 +0200 | [diff] [blame] | 120 | static_key_slow_dec(&xt_tee_enabled); |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static struct xt_target tee_tg_reg[] __read_mostly = { |
| 124 | { |
| 125 | .name = "TEE", |
| 126 | .revision = 1, |
| 127 | .family = NFPROTO_IPV4, |
| 128 | .target = tee_tg4, |
| 129 | .targetsize = sizeof(struct xt_tee_tginfo), |
| 130 | .checkentry = tee_tg_check, |
Patrick McHardy | 22265a5 | 2010-04-20 15:07:32 +0200 | [diff] [blame] | 131 | .destroy = tee_tg_destroy, |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 132 | .me = THIS_MODULE, |
| 133 | }, |
Arnd Bergmann | 08a7f5d | 2016-02-05 10:20:21 +0100 | [diff] [blame^] | 134 | #if IS_ENABLED(CONFIG_IPV6) |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 135 | { |
| 136 | .name = "TEE", |
| 137 | .revision = 1, |
| 138 | .family = NFPROTO_IPV6, |
| 139 | .target = tee_tg6, |
| 140 | .targetsize = sizeof(struct xt_tee_tginfo), |
| 141 | .checkentry = tee_tg_check, |
Patrick McHardy | 22265a5 | 2010-04-20 15:07:32 +0200 | [diff] [blame] | 142 | .destroy = tee_tg_destroy, |
Jan Engelhardt | e281b19 | 2010-04-19 14:17:47 +0200 | [diff] [blame] | 143 | .me = THIS_MODULE, |
| 144 | }, |
| 145 | #endif |
| 146 | }; |
| 147 | |
| 148 | static int __init tee_tg_init(void) |
| 149 | { |
| 150 | return xt_register_targets(tee_tg_reg, ARRAY_SIZE(tee_tg_reg)); |
| 151 | } |
| 152 | |
| 153 | static void __exit tee_tg_exit(void) |
| 154 | { |
| 155 | xt_unregister_targets(tee_tg_reg, ARRAY_SIZE(tee_tg_reg)); |
| 156 | } |
| 157 | |
| 158 | module_init(tee_tg_init); |
| 159 | module_exit(tee_tg_exit); |
| 160 | MODULE_AUTHOR("Sebastian Claßen <sebastian.classen@freenet.ag>"); |
| 161 | MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>"); |
| 162 | MODULE_DESCRIPTION("Xtables: Reroute packet copy"); |
| 163 | MODULE_LICENSE("GPL"); |
| 164 | MODULE_ALIAS("ipt_TEE"); |
| 165 | MODULE_ALIAS("ip6t_TEE"); |