Skip to content

Commit 8c4d4e8

Browse files
committed
netfilter: nfnetlink: allow to check for generation ID
This patch allows userspace to specify the generation ID that has been used to build an incremental batch update. If userspace specifies the generation ID in the batch message as attribute, then nfnetlink compares it to the current generation ID so you make sure that you work against the right baseline. Otherwise, bail out with ERESTART so userspace knows that its changeset is stale and needs to respin. Userspace can do this transparently at the cost of taking slightly more time to refresh caches and rework the changeset. This check is optional, if there is no NFNL_BATCH_GENID attribute in the batch begin message, then no check is performed. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 4865683 commit 8c4d4e8

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

include/linux/netfilter/nfnetlink.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ struct nfnetlink_subsystem {
2828
const struct nfnl_callback *cb; /* callback for individual types */
2929
int (*commit)(struct net *net, struct sk_buff *skb);
3030
int (*abort)(struct net *net, struct sk_buff *skb);
31+
bool (*valid_genid)(struct net *net, u32 genid);
3132
};
3233

3334
int nfnetlink_subsys_register(const struct nfnetlink_subsystem *n);

include/uapi/linux/netfilter/nfnetlink.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,16 @@ struct nfgenmsg {
6565
#define NFNL_MSG_BATCH_BEGIN NLMSG_MIN_TYPE
6666
#define NFNL_MSG_BATCH_END NLMSG_MIN_TYPE+1
6767

68+
/**
69+
* enum nfnl_batch_attributes - nfnetlink batch netlink attributes
70+
*
71+
* @NFNL_BATCH_GENID: generation ID for this changeset (NLA_U32)
72+
*/
73+
enum nfnl_batch_attributes {
74+
NFNL_BATCH_UNSPEC,
75+
NFNL_BATCH_GENID,
76+
__NFNL_BATCH_MAX
77+
};
78+
#define NFNL_BATCH_MAX (__NFNL_BATCH_MAX - 1)
79+
6880
#endif /* _UAPI_NFNETLINK_H */

net/netfilter/nfnetlink.c

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* (C) 2001 by Jay Schulist <jschlst@samba.org>,
55
* (C) 2002-2005 by Harald Welte <laforge@gnumonks.org>
6-
* (C) 2005,2007 by Pablo Neira Ayuso <pablo@netfilter.org>
6+
* (C) 2005-2017 by Pablo Neira Ayuso <pablo@netfilter.org>
77
*
88
* Initial netfilter messages via netlink development funded and
99
* generally made possible by Network Robots, Inc. (www.networkrobots.com)
@@ -273,7 +273,7 @@ enum {
273273
};
274274

275275
static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,
276-
u16 subsys_id)
276+
u16 subsys_id, u32 genid)
277277
{
278278
struct sk_buff *oskb = skb;
279279
struct net *net = sock_net(skb->sk);
@@ -315,6 +315,12 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,
315315
return kfree_skb(skb);
316316
}
317317

318+
if (genid && ss->valid_genid && !ss->valid_genid(net, genid)) {
319+
nfnl_unlock(subsys_id);
320+
netlink_ack(oskb, nlh, -ERESTART);
321+
return kfree_skb(skb);
322+
}
323+
318324
while (skb->len >= nlmsg_total_size(0)) {
319325
int msglen, type;
320326

@@ -436,11 +442,20 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,
436442
kfree_skb(skb);
437443
}
438444

445+
static const struct nla_policy nfnl_batch_policy[NFNL_BATCH_MAX + 1] = {
446+
[NFNL_BATCH_GENID] = { .type = NLA_U32 },
447+
};
448+
439449
static void nfnetlink_rcv_skb_batch(struct sk_buff *skb, struct nlmsghdr *nlh)
440450
{
451+
int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
452+
struct nlattr *attr = (void *)nlh + min_len;
453+
struct nlattr *cda[NFNL_BATCH_MAX + 1];
454+
int attrlen = nlh->nlmsg_len - min_len;
441455
struct nfgenmsg *nfgenmsg;
456+
int msglen, err;
457+
u32 gen_id = 0;
442458
u16 res_id;
443-
int msglen;
444459

445460
msglen = NLMSG_ALIGN(nlh->nlmsg_len);
446461
if (msglen > skb->len)
@@ -450,6 +465,14 @@ static void nfnetlink_rcv_skb_batch(struct sk_buff *skb, struct nlmsghdr *nlh)
450465
skb->len < NLMSG_HDRLEN + sizeof(struct nfgenmsg))
451466
return;
452467

468+
err = nla_parse(cda, NFNL_BATCH_MAX, attr, attrlen, nfnl_batch_policy);
469+
if (err < 0) {
470+
netlink_ack(skb, nlh, err);
471+
return;
472+
}
473+
if (cda[NFNL_BATCH_GENID])
474+
gen_id = ntohl(nla_get_be32(cda[NFNL_BATCH_GENID]));
475+
453476
nfgenmsg = nlmsg_data(nlh);
454477
skb_pull(skb, msglen);
455478
/* Work around old nft using host byte order */
@@ -458,7 +481,7 @@ static void nfnetlink_rcv_skb_batch(struct sk_buff *skb, struct nlmsghdr *nlh)
458481
else
459482
res_id = ntohs(nfgenmsg->res_id);
460483

461-
nfnetlink_rcv_batch(skb, nlh, res_id);
484+
nfnetlink_rcv_batch(skb, nlh, res_id, gen_id);
462485
}
463486

464487
static void nfnetlink_rcv(struct sk_buff *skb)

0 commit comments

Comments
 (0)