netfilter: Rework xt_TEE netdevice notifier
Register netdevice notifier for every iptable entry is not good, since this breaks modularity, and the hidden synchronization is based on rtnl_lock(). This patch reworks the synchronization via new lock, while the rest of logic remains as it was before. This is required for the next patch. Tested via: while :; do unshare -n iptables -t mangle -A OUTPUT -j TEE --gateway 1.1.1.2 --oif lo; done Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com> Acked-by: Pablo Neira Ayuso <pablo@netfilter.org> Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
parent
e9a441b6e7
commit
9e2f6c5d78
@ -20,7 +20,7 @@
|
|||||||
#include <linux/netfilter/xt_TEE.h>
|
#include <linux/netfilter/xt_TEE.h>
|
||||||
|
|
||||||
struct xt_tee_priv {
|
struct xt_tee_priv {
|
||||||
struct notifier_block notifier;
|
struct list_head list;
|
||||||
struct xt_tee_tginfo *tginfo;
|
struct xt_tee_tginfo *tginfo;
|
||||||
int oif;
|
int oif;
|
||||||
};
|
};
|
||||||
@ -51,29 +51,35 @@ tee_tg6(struct sk_buff *skb, const struct xt_action_param *par)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
static DEFINE_MUTEX(priv_list_mutex);
|
||||||
|
static LIST_HEAD(priv_list);
|
||||||
|
|
||||||
static int tee_netdev_event(struct notifier_block *this, unsigned long event,
|
static int tee_netdev_event(struct notifier_block *this, unsigned long event,
|
||||||
void *ptr)
|
void *ptr)
|
||||||
{
|
{
|
||||||
struct net_device *dev = netdev_notifier_info_to_dev(ptr);
|
struct net_device *dev = netdev_notifier_info_to_dev(ptr);
|
||||||
struct xt_tee_priv *priv;
|
struct xt_tee_priv *priv;
|
||||||
|
|
||||||
priv = container_of(this, struct xt_tee_priv, notifier);
|
mutex_lock(&priv_list_mutex);
|
||||||
switch (event) {
|
list_for_each_entry(priv, &priv_list, list) {
|
||||||
case NETDEV_REGISTER:
|
switch (event) {
|
||||||
if (!strcmp(dev->name, priv->tginfo->oif))
|
case NETDEV_REGISTER:
|
||||||
priv->oif = dev->ifindex;
|
if (!strcmp(dev->name, priv->tginfo->oif))
|
||||||
break;
|
priv->oif = dev->ifindex;
|
||||||
case NETDEV_UNREGISTER:
|
break;
|
||||||
if (dev->ifindex == priv->oif)
|
case NETDEV_UNREGISTER:
|
||||||
priv->oif = -1;
|
if (dev->ifindex == priv->oif)
|
||||||
break;
|
priv->oif = -1;
|
||||||
case NETDEV_CHANGENAME:
|
break;
|
||||||
if (!strcmp(dev->name, priv->tginfo->oif))
|
case NETDEV_CHANGENAME:
|
||||||
priv->oif = dev->ifindex;
|
if (!strcmp(dev->name, priv->tginfo->oif))
|
||||||
else if (dev->ifindex == priv->oif)
|
priv->oif = dev->ifindex;
|
||||||
priv->oif = -1;
|
else if (dev->ifindex == priv->oif)
|
||||||
break;
|
priv->oif = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
mutex_unlock(&priv_list_mutex);
|
||||||
|
|
||||||
return NOTIFY_DONE;
|
return NOTIFY_DONE;
|
||||||
}
|
}
|
||||||
@ -89,8 +95,6 @@ static int tee_tg_check(const struct xt_tgchk_param *par)
|
|||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
|
||||||
if (info->oif[0]) {
|
if (info->oif[0]) {
|
||||||
int ret;
|
|
||||||
|
|
||||||
if (info->oif[sizeof(info->oif)-1] != '\0')
|
if (info->oif[sizeof(info->oif)-1] != '\0')
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
|
||||||
@ -100,14 +104,11 @@ static int tee_tg_check(const struct xt_tgchk_param *par)
|
|||||||
|
|
||||||
priv->tginfo = info;
|
priv->tginfo = info;
|
||||||
priv->oif = -1;
|
priv->oif = -1;
|
||||||
priv->notifier.notifier_call = tee_netdev_event;
|
|
||||||
info->priv = priv;
|
info->priv = priv;
|
||||||
|
|
||||||
ret = register_netdevice_notifier(&priv->notifier);
|
mutex_lock(&priv_list_mutex);
|
||||||
if (ret) {
|
list_add(&priv->list, &priv_list);
|
||||||
kfree(priv);
|
mutex_unlock(&priv_list_mutex);
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
} else
|
} else
|
||||||
info->priv = NULL;
|
info->priv = NULL;
|
||||||
|
|
||||||
@ -120,7 +121,9 @@ static void tee_tg_destroy(const struct xt_tgdtor_param *par)
|
|||||||
struct xt_tee_tginfo *info = par->targinfo;
|
struct xt_tee_tginfo *info = par->targinfo;
|
||||||
|
|
||||||
if (info->priv) {
|
if (info->priv) {
|
||||||
unregister_netdevice_notifier(&info->priv->notifier);
|
mutex_lock(&priv_list_mutex);
|
||||||
|
list_del(&info->priv->list);
|
||||||
|
mutex_unlock(&priv_list_mutex);
|
||||||
kfree(info->priv);
|
kfree(info->priv);
|
||||||
}
|
}
|
||||||
static_key_slow_dec(&xt_tee_enabled);
|
static_key_slow_dec(&xt_tee_enabled);
|
||||||
@ -153,13 +156,29 @@ static struct xt_target tee_tg_reg[] __read_mostly = {
|
|||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static struct notifier_block tee_netdev_notifier = {
|
||||||
|
.notifier_call = tee_netdev_event,
|
||||||
|
};
|
||||||
|
|
||||||
static int __init tee_tg_init(void)
|
static int __init tee_tg_init(void)
|
||||||
{
|
{
|
||||||
return xt_register_targets(tee_tg_reg, ARRAY_SIZE(tee_tg_reg));
|
int ret;
|
||||||
|
|
||||||
|
ret = xt_register_targets(tee_tg_reg, ARRAY_SIZE(tee_tg_reg));
|
||||||
|
if (ret)
|
||||||
|
return ret;
|
||||||
|
ret = register_netdevice_notifier(&tee_netdev_notifier);
|
||||||
|
if (ret) {
|
||||||
|
xt_unregister_targets(tee_tg_reg, ARRAY_SIZE(tee_tg_reg));
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void __exit tee_tg_exit(void)
|
static void __exit tee_tg_exit(void)
|
||||||
{
|
{
|
||||||
|
unregister_netdevice_notifier(&tee_netdev_notifier);
|
||||||
xt_unregister_targets(tee_tg_reg, ARRAY_SIZE(tee_tg_reg));
|
xt_unregister_targets(tee_tg_reg, ARRAY_SIZE(tee_tg_reg));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user