2 * "TEE" target extension for Xtables
3 * Copyright © Sebastian Claßen, 2007
4 * Jan Engelhardt, 2007-2010
6 * based on ipt_ROUTE.c from Cédric de Launois
7 * <delaunois@info.ucl.be>
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.
13 #include <linux/module.h>
14 #include <linux/skbuff.h>
15 #include <linux/route.h>
16 #include <linux/netfilter/x_tables.h>
17 #include <net/net_namespace.h>
18 #include <net/netns/generic.h>
19 #include <net/route.h>
20 #include <net/netfilter/ipv4/nf_dup_ipv4.h>
21 #include <net/netfilter/ipv6/nf_dup_ipv6.h>
22 #include <linux/netfilter/xt_TEE.h>
25 struct list_head list
;
26 struct xt_tee_tginfo
*tginfo
;
30 static unsigned int tee_net_id __read_mostly
;
31 static const union nf_inet_addr tee_zero_address
;
34 struct list_head priv_list
;
35 /* lock protects the priv_list */
40 tee_tg4(struct sk_buff
*skb
, const struct xt_action_param
*par
)
42 const struct xt_tee_tginfo
*info
= par
->targinfo
;
43 int oif
= info
->priv
? info
->priv
->oif
: 0;
45 nf_dup_ipv4(xt_net(par
), skb
, xt_hooknum(par
), &info
->gw
.in
, oif
);
50 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
52 tee_tg6(struct sk_buff
*skb
, const struct xt_action_param
*par
)
54 const struct xt_tee_tginfo
*info
= par
->targinfo
;
55 int oif
= info
->priv
? info
->priv
->oif
: 0;
57 nf_dup_ipv6(xt_net(par
), skb
, xt_hooknum(par
), &info
->gw
.in6
, oif
);
63 static int tee_netdev_event(struct notifier_block
*this, unsigned long event
,
66 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
67 struct net
*net
= dev_net(dev
);
68 struct tee_net
*tn
= net_generic(net
, tee_net_id
);
69 struct xt_tee_priv
*priv
;
71 mutex_lock(&tn
->lock
);
72 list_for_each_entry(priv
, &tn
->priv_list
, list
) {
75 if (!strcmp(dev
->name
, priv
->tginfo
->oif
))
76 priv
->oif
= dev
->ifindex
;
78 case NETDEV_UNREGISTER
:
79 if (dev
->ifindex
== priv
->oif
)
82 case NETDEV_CHANGENAME
:
83 if (!strcmp(dev
->name
, priv
->tginfo
->oif
))
84 priv
->oif
= dev
->ifindex
;
85 else if (dev
->ifindex
== priv
->oif
)
90 mutex_unlock(&tn
->lock
);
95 static int tee_tg_check(const struct xt_tgchk_param
*par
)
97 struct tee_net
*tn
= net_generic(par
->net
, tee_net_id
);
98 struct xt_tee_tginfo
*info
= par
->targinfo
;
99 struct xt_tee_priv
*priv
;
101 /* 0.0.0.0 and :: not allowed */
102 if (memcmp(&info
->gw
, &tee_zero_address
,
103 sizeof(tee_zero_address
)) == 0)
107 struct net_device
*dev
;
109 if (info
->oif
[sizeof(info
->oif
)-1] != '\0')
112 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
120 dev
= dev_get_by_name(par
->net
, info
->oif
);
122 priv
->oif
= dev
->ifindex
;
125 mutex_lock(&tn
->lock
);
126 list_add(&priv
->list
, &tn
->priv_list
);
127 mutex_unlock(&tn
->lock
);
131 static_key_slow_inc(&xt_tee_enabled
);
135 static void tee_tg_destroy(const struct xt_tgdtor_param
*par
)
137 struct tee_net
*tn
= net_generic(par
->net
, tee_net_id
);
138 struct xt_tee_tginfo
*info
= par
->targinfo
;
141 mutex_lock(&tn
->lock
);
142 list_del(&info
->priv
->list
);
143 mutex_unlock(&tn
->lock
);
146 static_key_slow_dec(&xt_tee_enabled
);
149 static struct xt_target tee_tg_reg
[] __read_mostly
= {
153 .family
= NFPROTO_IPV4
,
155 .targetsize
= sizeof(struct xt_tee_tginfo
),
156 .usersize
= offsetof(struct xt_tee_tginfo
, priv
),
157 .checkentry
= tee_tg_check
,
158 .destroy
= tee_tg_destroy
,
161 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
165 .family
= NFPROTO_IPV6
,
167 .targetsize
= sizeof(struct xt_tee_tginfo
),
168 .usersize
= offsetof(struct xt_tee_tginfo
, priv
),
169 .checkentry
= tee_tg_check
,
170 .destroy
= tee_tg_destroy
,
176 static int __net_init
tee_net_init(struct net
*net
)
178 struct tee_net
*tn
= net_generic(net
, tee_net_id
);
180 INIT_LIST_HEAD(&tn
->priv_list
);
181 mutex_init(&tn
->lock
);
185 static struct pernet_operations tee_net_ops
= {
186 .init
= tee_net_init
,
188 .size
= sizeof(struct tee_net
),
191 static struct notifier_block tee_netdev_notifier
= {
192 .notifier_call
= tee_netdev_event
,
195 static int __init
tee_tg_init(void)
199 ret
= register_pernet_subsys(&tee_net_ops
);
203 ret
= xt_register_targets(tee_tg_reg
, ARRAY_SIZE(tee_tg_reg
));
207 ret
= register_netdevice_notifier(&tee_netdev_notifier
);
209 goto unregister_targets
;
214 xt_unregister_targets(tee_tg_reg
, ARRAY_SIZE(tee_tg_reg
));
216 unregister_pernet_subsys(&tee_net_ops
);
220 static void __exit
tee_tg_exit(void)
222 unregister_netdevice_notifier(&tee_netdev_notifier
);
223 xt_unregister_targets(tee_tg_reg
, ARRAY_SIZE(tee_tg_reg
));
224 unregister_pernet_subsys(&tee_net_ops
);
227 module_init(tee_tg_init
);
228 module_exit(tee_tg_exit
);
229 MODULE_AUTHOR("Sebastian Claßen <sebastian.classen@freenet.ag>");
230 MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
231 MODULE_DESCRIPTION("Xtables: Reroute packet copy");
232 MODULE_LICENSE("GPL");
233 MODULE_ALIAS("ipt_TEE");
234 MODULE_ALIAS("ip6t_TEE");