blob: 899b06115fc53c87ea34eb1bf975666e1992ca98 [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 */
Jan Engelhardte281b192010-04-19 14:17:47 +020013#include <linux/module.h>
Jan Engelhardte281b192010-04-19 14:17:47 +020014#include <linux/skbuff.h>
Pablo Neira Ayusobbde9fc2015-05-31 17:54:44 +020015#include <linux/route.h>
Jan Engelhardte281b192010-04-19 14:17:47 +020016#include <linux/netfilter/x_tables.h>
Pablo Neira Ayusobbde9fc2015-05-31 17:54:44 +020017#include <net/route.h>
18#include <net/netfilter/ipv4/nf_dup_ipv4.h>
19#include <net/netfilter/ipv6/nf_dup_ipv6.h>
Jan Engelhardte281b192010-04-19 14:17:47 +020020#include <linux/netfilter/xt_TEE.h>
Jan Engelhardte281b192010-04-19 14:17:47 +020021
Patrick McHardy22265a52010-04-20 15:07:32 +020022struct xt_tee_priv {
23 struct notifier_block notifier;
24 struct xt_tee_tginfo *tginfo;
25 int oif;
26};
27
Jan Engelhardte281b192010-04-19 14:17:47 +020028static const union nf_inet_addr tee_zero_address;
29
Jan Engelhardte281b192010-04-19 14:17:47 +020030static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +020031tee_tg4(struct sk_buff *skb, const struct xt_action_param *par)
Jan Engelhardte281b192010-04-19 14:17:47 +020032{
33 const struct xt_tee_tginfo *info = par->targinfo;
Jan Engelhardte281b192010-04-19 14:17:47 +020034
Eric W. Biederman206e8c02015-09-18 14:33:02 -050035 nf_dup_ipv4(par->net, skb, par->hooknum, &info->gw.in, info->priv->oif);
Jan Engelhardte281b192010-04-19 14:17:47 +020036
Jan Engelhardte281b192010-04-19 14:17:47 +020037 return XT_CONTINUE;
38}
39
Pablo Neira Ayuso116984a32015-08-21 21:34:08 +020040#if IS_ENABLED(CONFIG_NF_DUP_IPV6)
Jan Engelhardte281b192010-04-19 14:17:47 +020041static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +020042tee_tg6(struct sk_buff *skb, const struct xt_action_param *par)
Jan Engelhardte281b192010-04-19 14:17:47 +020043{
44 const struct xt_tee_tginfo *info = par->targinfo;
45
Eric W. Biederman206e8c02015-09-18 14:33:02 -050046 nf_dup_ipv6(par->net, skb, par->hooknum, &info->gw.in6, info->priv->oif);
Jan Engelhardte281b192010-04-19 14:17:47 +020047
Jan Engelhardte281b192010-04-19 14:17:47 +020048 return XT_CONTINUE;
49}
Eric Dumazetdfd56b82011-12-10 09:48:31 +000050#endif
Jan Engelhardte281b192010-04-19 14:17:47 +020051
Patrick McHardy22265a52010-04-20 15:07:32 +020052static int tee_netdev_event(struct notifier_block *this, unsigned long event,
53 void *ptr)
54{
Jiri Pirko351638e2013-05-28 01:30:21 +000055 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Patrick McHardy22265a52010-04-20 15:07:32 +020056 struct xt_tee_priv *priv;
57
58 priv = container_of(this, struct xt_tee_priv, notifier);
59 switch (event) {
60 case NETDEV_REGISTER:
61 if (!strcmp(dev->name, priv->tginfo->oif))
62 priv->oif = dev->ifindex;
63 break;
64 case NETDEV_UNREGISTER:
65 if (dev->ifindex == priv->oif)
66 priv->oif = -1;
67 break;
68 case NETDEV_CHANGENAME:
69 if (!strcmp(dev->name, priv->tginfo->oif))
70 priv->oif = dev->ifindex;
71 else if (dev->ifindex == priv->oif)
72 priv->oif = -1;
73 break;
74 }
75
76 return NOTIFY_DONE;
77}
78
Jan Engelhardte281b192010-04-19 14:17:47 +020079static int tee_tg_check(const struct xt_tgchk_param *par)
80{
Patrick McHardy22265a52010-04-20 15:07:32 +020081 struct xt_tee_tginfo *info = par->targinfo;
82 struct xt_tee_priv *priv;
Jan Engelhardte281b192010-04-19 14:17:47 +020083
Jan Engelhardte281b192010-04-19 14:17:47 +020084 /* 0.0.0.0 and :: not allowed */
Patrick McHardy22265a52010-04-20 15:07:32 +020085 if (memcmp(&info->gw, &tee_zero_address,
86 sizeof(tee_zero_address)) == 0)
87 return -EINVAL;
88
89 if (info->oif[0]) {
90 if (info->oif[sizeof(info->oif)-1] != '\0')
91 return -EINVAL;
92
93 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
94 if (priv == NULL)
95 return -ENOMEM;
96
97 priv->tginfo = info;
98 priv->oif = -1;
99 priv->notifier.notifier_call = tee_netdev_event;
100 info->priv = priv;
101
102 register_netdevice_notifier(&priv->notifier);
103 } else
104 info->priv = NULL;
105
Florian Westphaldcebd312015-07-14 17:51:09 +0200106 static_key_slow_inc(&xt_tee_enabled);
Patrick McHardy22265a52010-04-20 15:07:32 +0200107 return 0;
108}
109
110static void tee_tg_destroy(const struct xt_tgdtor_param *par)
111{
112 struct xt_tee_tginfo *info = par->targinfo;
113
114 if (info->priv) {
115 unregister_netdevice_notifier(&info->priv->notifier);
116 kfree(info->priv);
117 }
Florian Westphaldcebd312015-07-14 17:51:09 +0200118 static_key_slow_dec(&xt_tee_enabled);
Jan Engelhardte281b192010-04-19 14:17:47 +0200119}
120
121static struct xt_target tee_tg_reg[] __read_mostly = {
122 {
123 .name = "TEE",
124 .revision = 1,
125 .family = NFPROTO_IPV4,
126 .target = tee_tg4,
127 .targetsize = sizeof(struct xt_tee_tginfo),
128 .checkentry = tee_tg_check,
Patrick McHardy22265a52010-04-20 15:07:32 +0200129 .destroy = tee_tg_destroy,
Jan Engelhardte281b192010-04-19 14:17:47 +0200130 .me = THIS_MODULE,
131 },
Pablo Neira Ayuso116984a32015-08-21 21:34:08 +0200132#if IS_ENABLED(CONFIG_NF_DUP_IPV6)
Jan Engelhardte281b192010-04-19 14:17:47 +0200133 {
134 .name = "TEE",
135 .revision = 1,
136 .family = NFPROTO_IPV6,
137 .target = tee_tg6,
138 .targetsize = sizeof(struct xt_tee_tginfo),
139 .checkentry = tee_tg_check,
Patrick McHardy22265a52010-04-20 15:07:32 +0200140 .destroy = tee_tg_destroy,
Jan Engelhardte281b192010-04-19 14:17:47 +0200141 .me = THIS_MODULE,
142 },
143#endif
144};
145
146static int __init tee_tg_init(void)
147{
148 return xt_register_targets(tee_tg_reg, ARRAY_SIZE(tee_tg_reg));
149}
150
151static void __exit tee_tg_exit(void)
152{
153 xt_unregister_targets(tee_tg_reg, ARRAY_SIZE(tee_tg_reg));
154}
155
156module_init(tee_tg_init);
157module_exit(tee_tg_exit);
158MODULE_AUTHOR("Sebastian Claßen <sebastian.classen@freenet.ag>");
159MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
160MODULE_DESCRIPTION("Xtables: Reroute packet copy");
161MODULE_LICENSE("GPL");
162MODULE_ALIAS("ipt_TEE");
163MODULE_ALIAS("ip6t_TEE");