Skip to content

Commit 22265a5

Browse files
committed
netfilter: xt_TEE: resolve oif using netdevice notifiers
Replace the runtime oif name resolving by netdevice notifier based resolving. When an oif is given, a netdevice notifier is registered to resolve the name on NETDEV_REGISTER or NETDEV_CHANGE and unresolve it again on NETDEV_UNREGISTER or NETDEV_CHANGE to a different name. Signed-off-by: Patrick McHardy <[email protected]>
1 parent 5b775eb commit 22265a5

File tree

2 files changed

+83
-23
lines changed

2 files changed

+83
-23
lines changed

include/linux/netfilter/xt_TEE.h

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
struct xt_tee_tginfo {
55
union nf_inet_addr gw;
66
char oif[16];
7+
8+
/* used internally by the kernel */
9+
struct xt_tee_priv *priv __attribute__((aligned(8)));
710
};
811

912
#endif /* _XT_TEE_TARGET_H */

net/netfilter/xt_TEE.c

+80-23
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <linux/percpu.h>
1616
#include <linux/route.h>
1717
#include <linux/skbuff.h>
18+
#include <linux/notifier.h>
1819
#include <net/checksum.h>
1920
#include <net/icmp.h>
2021
#include <net/ip.h>
@@ -32,6 +33,12 @@
3233
# define WITH_IPV6 1
3334
#endif
3435

36+
struct xt_tee_priv {
37+
struct notifier_block notifier;
38+
struct xt_tee_tginfo *tginfo;
39+
int oif;
40+
};
41+
3542
static const union nf_inet_addr tee_zero_address;
3643
static DEFINE_PER_CPU(bool, tee_active);
3744

@@ -49,20 +56,6 @@ static struct net *pick_net(struct sk_buff *skb)
4956
return &init_net;
5057
}
5158

52-
static bool tee_tg_route_oif(struct flowi *f, struct net *net,
53-
const struct xt_tee_tginfo *info)
54-
{
55-
const struct net_device *dev;
56-
57-
if (*info->oif != '\0')
58-
return true;
59-
dev = dev_get_by_name(net, info->oif);
60-
if (dev == NULL)
61-
return false;
62-
f->oif = dev->ifindex;
63-
return true;
64-
}
65-
6659
static bool
6760
tee_tg_route4(struct sk_buff *skb, const struct xt_tee_tginfo *info)
6861
{
@@ -72,8 +65,11 @@ tee_tg_route4(struct sk_buff *skb, const struct xt_tee_tginfo *info)
7265
struct flowi fl;
7366

7467
memset(&fl, 0, sizeof(fl));
75-
if (!tee_tg_route_oif(&fl, net, info))
76-
return false;
68+
if (info->priv) {
69+
if (info->priv->oif == -1)
70+
return false;
71+
fl.oif = info->priv->oif;
72+
}
7773
fl.nl_u.ip4_u.daddr = info->gw.ip;
7874
fl.nl_u.ip4_u.tos = RT_TOS(iph->tos);
7975
fl.nl_u.ip4_u.scope = RT_SCOPE_UNIVERSE;
@@ -149,8 +145,11 @@ tee_tg_route6(struct sk_buff *skb, const struct xt_tee_tginfo *info)
149145
struct flowi fl;
150146

151147
memset(&fl, 0, sizeof(fl));
152-
if (!tee_tg_route_oif(&fl, net, info))
153-
return false;
148+
if (info->priv) {
149+
if (info->priv->oif == -1)
150+
return false;
151+
fl.oif = info->priv->oif;
152+
}
154153
fl.nl_u.ip6_u.daddr = info->gw.in6;
155154
fl.nl_u.ip6_u.flowlabel = ((iph->flow_lbl[0] & 0xF) << 16) |
156155
(iph->flow_lbl[1] << 8) | iph->flow_lbl[2];
@@ -198,15 +197,71 @@ tee_tg6(struct sk_buff *skb, const struct xt_target_param *par)
198197
}
199198
#endif /* WITH_IPV6 */
200199

200+
static int tee_netdev_event(struct notifier_block *this, unsigned long event,
201+
void *ptr)
202+
{
203+
struct net_device *dev = ptr;
204+
struct xt_tee_priv *priv;
205+
206+
priv = container_of(this, struct xt_tee_priv, notifier);
207+
switch (event) {
208+
case NETDEV_REGISTER:
209+
if (!strcmp(dev->name, priv->tginfo->oif))
210+
priv->oif = dev->ifindex;
211+
break;
212+
case NETDEV_UNREGISTER:
213+
if (dev->ifindex == priv->oif)
214+
priv->oif = -1;
215+
break;
216+
case NETDEV_CHANGENAME:
217+
if (!strcmp(dev->name, priv->tginfo->oif))
218+
priv->oif = dev->ifindex;
219+
else if (dev->ifindex == priv->oif)
220+
priv->oif = -1;
221+
break;
222+
}
223+
224+
return NOTIFY_DONE;
225+
}
226+
201227
static int tee_tg_check(const struct xt_tgchk_param *par)
202228
{
203-
const struct xt_tee_tginfo *info = par->targinfo;
229+
struct xt_tee_tginfo *info = par->targinfo;
230+
struct xt_tee_priv *priv;
204231

205-
if (info->oif[sizeof(info->oif)-1] != '\0')
206-
return -EINVAL;
207232
/* 0.0.0.0 and :: not allowed */
208-
return (memcmp(&info->gw, &tee_zero_address,
209-
sizeof(tee_zero_address)) == 0) ? -EINVAL : 0;
233+
if (memcmp(&info->gw, &tee_zero_address,
234+
sizeof(tee_zero_address)) == 0)
235+
return -EINVAL;
236+
237+
if (info->oif[0]) {
238+
if (info->oif[sizeof(info->oif)-1] != '\0')
239+
return -EINVAL;
240+
241+
priv = kzalloc(sizeof(*priv), GFP_KERNEL);
242+
if (priv == NULL)
243+
return -ENOMEM;
244+
245+
priv->tginfo = info;
246+
priv->oif = -1;
247+
priv->notifier.notifier_call = tee_netdev_event;
248+
info->priv = priv;
249+
250+
register_netdevice_notifier(&priv->notifier);
251+
} else
252+
info->priv = NULL;
253+
254+
return 0;
255+
}
256+
257+
static void tee_tg_destroy(const struct xt_tgdtor_param *par)
258+
{
259+
struct xt_tee_tginfo *info = par->targinfo;
260+
261+
if (info->priv) {
262+
unregister_netdevice_notifier(&info->priv->notifier);
263+
kfree(info->priv);
264+
}
210265
}
211266

212267
static struct xt_target tee_tg_reg[] __read_mostly = {
@@ -217,6 +272,7 @@ static struct xt_target tee_tg_reg[] __read_mostly = {
217272
.target = tee_tg4,
218273
.targetsize = sizeof(struct xt_tee_tginfo),
219274
.checkentry = tee_tg_check,
275+
.destroy = tee_tg_destroy,
220276
.me = THIS_MODULE,
221277
},
222278
#ifdef WITH_IPV6
@@ -227,6 +283,7 @@ static struct xt_target tee_tg_reg[] __read_mostly = {
227283
.target = tee_tg6,
228284
.targetsize = sizeof(struct xt_tee_tginfo),
229285
.checkentry = tee_tg_check,
286+
.destroy = tee_tg_destroy,
230287
.me = THIS_MODULE,
231288
},
232289
#endif

0 commit comments

Comments
 (0)