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/route.h>
18 #include <net/netfilter/ipv4/nf_dup_ipv4.h>
19 #include <net/netfilter/ipv6/nf_dup_ipv6.h>
20 #include <linux/netfilter/xt_TEE.h>
23 struct list_head list
;
24 struct xt_tee_tginfo
*tginfo
;
28 static const union nf_inet_addr tee_zero_address
;
31 tee_tg4(struct sk_buff
*skb
, const struct xt_action_param
*par
)
33 const struct xt_tee_tginfo
*info
= par
->targinfo
;
34 int oif
= info
->priv
? info
->priv
->oif
: 0;
36 nf_dup_ipv4(xt_net(par
), skb
, xt_hooknum(par
), &info
->gw
.in
, oif
);
41 #if IS_ENABLED(CONFIG_IPV6)
43 tee_tg6(struct sk_buff
*skb
, const struct xt_action_param
*par
)
45 const struct xt_tee_tginfo
*info
= par
->targinfo
;
46 int oif
= info
->priv
? info
->priv
->oif
: 0;
48 nf_dup_ipv6(xt_net(par
), skb
, xt_hooknum(par
), &info
->gw
.in6
, oif
);
54 static DEFINE_MUTEX(priv_list_mutex
);
55 static LIST_HEAD(priv_list
);
57 static int tee_netdev_event(struct notifier_block
*this, unsigned long event
,
60 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
61 struct xt_tee_priv
*priv
;
63 mutex_lock(&priv_list_mutex
);
64 list_for_each_entry(priv
, &priv_list
, list
) {
67 if (!strcmp(dev
->name
, priv
->tginfo
->oif
))
68 priv
->oif
= dev
->ifindex
;
70 case NETDEV_UNREGISTER
:
71 if (dev
->ifindex
== priv
->oif
)
74 case NETDEV_CHANGENAME
:
75 if (!strcmp(dev
->name
, priv
->tginfo
->oif
))
76 priv
->oif
= dev
->ifindex
;
77 else if (dev
->ifindex
== priv
->oif
)
82 mutex_unlock(&priv_list_mutex
);
87 static int tee_tg_check(const struct xt_tgchk_param
*par
)
89 struct xt_tee_tginfo
*info
= par
->targinfo
;
90 struct xt_tee_priv
*priv
;
92 /* 0.0.0.0 and :: not allowed */
93 if (memcmp(&info
->gw
, &tee_zero_address
,
94 sizeof(tee_zero_address
)) == 0)
98 if (info
->oif
[sizeof(info
->oif
)-1] != '\0')
101 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
109 mutex_lock(&priv_list_mutex
);
110 list_add(&priv
->list
, &priv_list
);
111 mutex_unlock(&priv_list_mutex
);
115 static_key_slow_inc(&xt_tee_enabled
);
119 static void tee_tg_destroy(const struct xt_tgdtor_param
*par
)
121 struct xt_tee_tginfo
*info
= par
->targinfo
;
124 mutex_lock(&priv_list_mutex
);
125 list_del(&info
->priv
->list
);
126 mutex_unlock(&priv_list_mutex
);
129 static_key_slow_dec(&xt_tee_enabled
);
132 static struct xt_target tee_tg_reg
[] __read_mostly
= {
136 .family
= NFPROTO_IPV4
,
138 .targetsize
= sizeof(struct xt_tee_tginfo
),
139 .usersize
= offsetof(struct xt_tee_tginfo
, priv
),
140 .checkentry
= tee_tg_check
,
141 .destroy
= tee_tg_destroy
,
144 #if IS_ENABLED(CONFIG_IPV6)
148 .family
= NFPROTO_IPV6
,
150 .targetsize
= sizeof(struct xt_tee_tginfo
),
151 .usersize
= offsetof(struct xt_tee_tginfo
, priv
),
152 .checkentry
= tee_tg_check
,
153 .destroy
= tee_tg_destroy
,
159 static struct notifier_block tee_netdev_notifier
= {
160 .notifier_call
= tee_netdev_event
,
163 static int __init
tee_tg_init(void)
167 ret
= xt_register_targets(tee_tg_reg
, ARRAY_SIZE(tee_tg_reg
));
170 ret
= register_netdevice_notifier(&tee_netdev_notifier
);
172 xt_unregister_targets(tee_tg_reg
, ARRAY_SIZE(tee_tg_reg
));
179 static void __exit
tee_tg_exit(void)
181 unregister_netdevice_notifier(&tee_netdev_notifier
);
182 xt_unregister_targets(tee_tg_reg
, ARRAY_SIZE(tee_tg_reg
));
185 module_init(tee_tg_init
);
186 module_exit(tee_tg_exit
);
187 MODULE_AUTHOR("Sebastian Claßen <sebastian.classen@freenet.ag>");
188 MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
189 MODULE_DESCRIPTION("Xtables: Reroute packet copy");
190 MODULE_LICENSE("GPL");
191 MODULE_ALIAS("ipt_TEE");
192 MODULE_ALIAS("ip6t_TEE");