Skip to content

Commit a331b78

Browse files
Phil Sutterummakynes
authored andcommitted
netfilter: nf_tables: Respect NETDEV_REGISTER events
Hook into new devices if their name matches the hook spec. Signed-off-by: Phil Sutter <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 104031a commit a331b78

File tree

2 files changed

+60
-9
lines changed

2 files changed

+60
-9
lines changed

net/netfilter/nf_tables_api.c

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9689,8 +9689,8 @@ struct nf_hook_ops *nft_hook_find_ops_rcu(const struct nft_hook *hook,
96899689
}
96909690
EXPORT_SYMBOL_GPL(nft_hook_find_ops_rcu);
96919691

9692-
static void nft_flowtable_event(unsigned long event, struct net_device *dev,
9693-
struct nft_flowtable *flowtable)
9692+
static int nft_flowtable_event(unsigned long event, struct net_device *dev,
9693+
struct nft_flowtable *flowtable)
96949694
{
96959695
struct nf_hook_ops *ops;
96969696
struct nft_hook *hook;
@@ -9708,9 +9708,32 @@ static void nft_flowtable_event(unsigned long event, struct net_device *dev,
97089708
list_del_rcu(&ops->list);
97099709
kfree_rcu(ops, rcu);
97109710
break;
9711+
case NETDEV_REGISTER:
9712+
if (strcmp(hook->ifname, dev->name))
9713+
continue;
9714+
9715+
ops = kzalloc(sizeof(struct nf_hook_ops),
9716+
GFP_KERNEL_ACCOUNT);
9717+
if (!ops)
9718+
return 1;
9719+
9720+
ops->pf = NFPROTO_NETDEV;
9721+
ops->hooknum = flowtable->hooknum;
9722+
ops->priority = flowtable->data.priority;
9723+
ops->priv = &flowtable->data;
9724+
ops->hook = flowtable->data.type->hook;
9725+
ops->dev = dev;
9726+
if (nft_register_flowtable_ops(dev_net(dev),
9727+
flowtable, ops)) {
9728+
kfree(ops);
9729+
return 1;
9730+
}
9731+
list_add_tail_rcu(&ops->list, &hook->ops_list);
9732+
break;
97119733
}
97129734
break;
97139735
}
9736+
return 0;
97149737
}
97159738

97169739
static int nf_tables_flowtable_event(struct notifier_block *this,
@@ -9722,15 +9745,19 @@ static int nf_tables_flowtable_event(struct notifier_block *this,
97229745
struct nft_table *table;
97239746
struct net *net;
97249747

9725-
if (event != NETDEV_UNREGISTER)
9726-
return 0;
9748+
if (event != NETDEV_REGISTER &&
9749+
event != NETDEV_UNREGISTER)
9750+
return NOTIFY_DONE;
97279751

97289752
net = dev_net(dev);
97299753
nft_net = nft_pernet(net);
97309754
mutex_lock(&nft_net->commit_mutex);
97319755
list_for_each_entry(table, &nft_net->tables, list) {
97329756
list_for_each_entry(flowtable, &table->flowtables, list) {
9733-
nft_flowtable_event(event, dev, flowtable);
9757+
if (nft_flowtable_event(event, dev, flowtable)) {
9758+
mutex_unlock(&nft_net->commit_mutex);
9759+
return NOTIFY_BAD;
9760+
}
97349761
}
97359762
}
97369763
mutex_unlock(&nft_net->commit_mutex);

net/netfilter/nft_chain_filter.c

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@ static const struct nft_chain_type nft_chain_filter_netdev = {
318318
},
319319
};
320320

321-
static void nft_netdev_event(unsigned long event, struct net_device *dev,
322-
struct nft_base_chain *basechain)
321+
static int nft_netdev_event(unsigned long event, struct net_device *dev,
322+
struct nft_base_chain *basechain)
323323
{
324324
struct nft_table *table = basechain->chain.table;
325325
struct nf_hook_ops *ops;
@@ -338,9 +338,29 @@ static void nft_netdev_event(unsigned long event, struct net_device *dev,
338338
list_del_rcu(&ops->list);
339339
kfree_rcu(ops, rcu);
340340
break;
341+
case NETDEV_REGISTER:
342+
if (strcmp(hook->ifname, dev->name))
343+
continue;
344+
345+
ops = kmemdup(&basechain->ops,
346+
sizeof(struct nf_hook_ops),
347+
GFP_KERNEL_ACCOUNT);
348+
if (!ops)
349+
return 1;
350+
351+
ops->dev = dev;
352+
353+
if (!(table->flags & NFT_TABLE_F_DORMANT) &&
354+
nf_register_net_hook(dev_net(dev), ops)) {
355+
kfree(ops);
356+
return 1;
357+
}
358+
list_add_tail_rcu(&ops->list, &hook->ops_list);
359+
break;
341360
}
342361
break;
343362
}
363+
return 0;
344364
}
345365

346366
static int nf_tables_netdev_event(struct notifier_block *this,
@@ -352,7 +372,8 @@ static int nf_tables_netdev_event(struct notifier_block *this,
352372
struct nft_chain *chain;
353373
struct nft_table *table;
354374

355-
if (event != NETDEV_UNREGISTER)
375+
if (event != NETDEV_REGISTER &&
376+
event != NETDEV_UNREGISTER)
356377
return NOTIFY_DONE;
357378

358379
nft_net = nft_pernet(dev_net(dev));
@@ -371,7 +392,10 @@ static int nf_tables_netdev_event(struct notifier_block *this,
371392
basechain->ops.hooknum != NF_INET_INGRESS)
372393
continue;
373394

374-
nft_netdev_event(event, dev, basechain);
395+
if (nft_netdev_event(event, dev, basechain)) {
396+
mutex_unlock(&nft_net->commit_mutex);
397+
return NOTIFY_BAD;
398+
}
375399
}
376400
}
377401
mutex_unlock(&nft_net->commit_mutex);

0 commit comments

Comments
 (0)