Skip to content

Commit 465b9ee

Browse files
Phil Sutterummakynes
authored andcommitted
netfilter: nf_tables: Add notifications for hook changes
Notify user space if netdev hooks are updated due to netdev add/remove events. Send minimal notification messages by introducing NFT_MSG_NEWDEV/DELDEV message types describing a single device only. Upon NETDEV_CHANGENAME, the callback has no information about the interface's old name. To provide a clear message to user space, include the hook's stored interface name in the notification. Signed-off-by: Phil Sutter <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 6d07a28 commit 465b9ee

File tree

6 files changed

+79
-0
lines changed

6 files changed

+79
-0
lines changed

include/net/netfilter/nf_tables.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,11 @@ int nft_set_catchall_validate(const struct nft_ctx *ctx, struct nft_set *set);
11421142
int nf_tables_bind_chain(const struct nft_ctx *ctx, struct nft_chain *chain);
11431143
void nf_tables_unbind_chain(const struct nft_ctx *ctx, struct nft_chain *chain);
11441144

1145+
struct nft_hook;
1146+
void nf_tables_chain_device_notify(const struct nft_chain *chain,
1147+
const struct nft_hook *hook,
1148+
const struct net_device *dev, int event);
1149+
11451150
enum nft_chain_types {
11461151
NFT_CHAIN_T_DEFAULT = 0,
11471152
NFT_CHAIN_T_ROUTE,

include/uapi/linux/netfilter/nf_tables.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ enum nf_tables_msg_types {
142142
NFT_MSG_DESTROYOBJ,
143143
NFT_MSG_DESTROYFLOWTABLE,
144144
NFT_MSG_GETSETELEM_RESET,
145+
NFT_MSG_NEWDEV,
146+
NFT_MSG_DELDEV,
145147
NFT_MSG_MAX,
146148
};
147149

@@ -1784,10 +1786,18 @@ enum nft_synproxy_attributes {
17841786
* enum nft_device_attributes - nf_tables device netlink attributes
17851787
*
17861788
* @NFTA_DEVICE_NAME: name of this device (NLA_STRING)
1789+
* @NFTA_DEVICE_TABLE: table containing the flowtable or chain hooking into the device (NLA_STRING)
1790+
* @NFTA_DEVICE_FLOWTABLE: flowtable hooking into the device (NLA_STRING)
1791+
* @NFTA_DEVICE_CHAIN: chain hooking into the device (NLA_STRING)
1792+
* @NFTA_DEVICE_SPEC: hook spec matching the device (NLA_STRING)
17871793
*/
17881794
enum nft_devices_attributes {
17891795
NFTA_DEVICE_UNSPEC,
17901796
NFTA_DEVICE_NAME,
1797+
NFTA_DEVICE_TABLE,
1798+
NFTA_DEVICE_FLOWTABLE,
1799+
NFTA_DEVICE_CHAIN,
1800+
NFTA_DEVICE_SPEC,
17911801
__NFTA_DEVICE_MAX
17921802
};
17931803
#define NFTA_DEVICE_MAX (__NFTA_DEVICE_MAX - 1)

include/uapi/linux/netfilter/nfnetlink.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ enum nfnetlink_groups {
2525
#define NFNLGRP_ACCT_QUOTA NFNLGRP_ACCT_QUOTA
2626
NFNLGRP_NFTRACE,
2727
#define NFNLGRP_NFTRACE NFNLGRP_NFTRACE
28+
NFNLGRP_NFT_DEV,
29+
#define NFNLGRP_NFT_DEV NFNLGRP_NFT_DEV
2830
__NFNLGRP_MAX,
2931
};
3032
#define NFNLGRP_MAX (__NFNLGRP_MAX - 1)

net/netfilter/nf_tables_api.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9686,6 +9686,64 @@ struct nf_hook_ops *nft_hook_find_ops_rcu(const struct nft_hook *hook,
96869686
}
96879687
EXPORT_SYMBOL_GPL(nft_hook_find_ops_rcu);
96889688

9689+
static void
9690+
nf_tables_device_notify(const struct nft_table *table, int attr,
9691+
const char *name, const struct nft_hook *hook,
9692+
const struct net_device *dev, int event)
9693+
{
9694+
struct net *net = dev_net(dev);
9695+
struct nlmsghdr *nlh;
9696+
struct sk_buff *skb;
9697+
u16 flags = 0;
9698+
9699+
if (!nfnetlink_has_listeners(net, NFNLGRP_NFT_DEV))
9700+
return;
9701+
9702+
skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9703+
if (!skb)
9704+
goto err;
9705+
9706+
event = event == NETDEV_REGISTER ? NFT_MSG_NEWDEV : NFT_MSG_DELDEV;
9707+
event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
9708+
nlh = nfnl_msg_put(skb, 0, 0, event, flags, table->family,
9709+
NFNETLINK_V0, nft_base_seq(net));
9710+
if (!nlh)
9711+
goto err;
9712+
9713+
if (nla_put_string(skb, NFTA_DEVICE_TABLE, table->name) ||
9714+
nla_put_string(skb, attr, name) ||
9715+
nla_put(skb, NFTA_DEVICE_SPEC, hook->ifnamelen, hook->ifname) ||
9716+
nla_put_string(skb, NFTA_DEVICE_NAME, dev->name))
9717+
goto err;
9718+
9719+
nlmsg_end(skb, nlh);
9720+
nfnetlink_send(skb, net, 0, NFNLGRP_NFT_DEV,
9721+
nlmsg_report(nlh), GFP_KERNEL);
9722+
return;
9723+
err:
9724+
if (skb)
9725+
kfree_skb(skb);
9726+
nfnetlink_set_err(net, 0, NFNLGRP_NFT_DEV, -ENOBUFS);
9727+
}
9728+
9729+
void
9730+
nf_tables_chain_device_notify(const struct nft_chain *chain,
9731+
const struct nft_hook *hook,
9732+
const struct net_device *dev, int event)
9733+
{
9734+
nf_tables_device_notify(chain->table, NFTA_DEVICE_CHAIN,
9735+
chain->name, hook, dev, event);
9736+
}
9737+
9738+
static void
9739+
nf_tables_flowtable_device_notify(const struct nft_flowtable *ft,
9740+
const struct nft_hook *hook,
9741+
const struct net_device *dev, int event)
9742+
{
9743+
nf_tables_device_notify(ft->table, NFTA_DEVICE_FLOWTABLE,
9744+
ft->name, hook, dev, event);
9745+
}
9746+
96899747
static int nft_flowtable_event(unsigned long event, struct net_device *dev,
96909748
struct nft_flowtable *flowtable, bool changename)
96919749
{
@@ -9733,6 +9791,7 @@ static int nft_flowtable_event(unsigned long event, struct net_device *dev,
97339791
list_add_tail_rcu(&ops->list, &hook->ops_list);
97349792
break;
97359793
}
9794+
nf_tables_flowtable_device_notify(flowtable, hook, dev, event);
97369795
break;
97379796
}
97389797
return 0;

net/netfilter/nfnetlink.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ static const int nfnl_group2type[NFNLGRP_MAX+1] = {
8686
[NFNLGRP_NFTABLES] = NFNL_SUBSYS_NFTABLES,
8787
[NFNLGRP_ACCT_QUOTA] = NFNL_SUBSYS_ACCT,
8888
[NFNLGRP_NFTRACE] = NFNL_SUBSYS_NFTABLES,
89+
[NFNLGRP_NFT_DEV] = NFNL_SUBSYS_NFTABLES,
8990
};
9091

9192
static struct nfnl_net *nfnl_pernet(struct net *net)

net/netfilter/nft_chain_filter.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ static int nft_netdev_event(unsigned long event, struct net_device *dev,
363363
list_add_tail_rcu(&ops->list, &hook->ops_list);
364364
break;
365365
}
366+
nf_tables_chain_device_notify(&basechain->chain,
367+
hook, dev, event);
366368
break;
367369
}
368370
return 0;

0 commit comments

Comments
 (0)