Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

+40 -4
+1
include/linux/netfilter/nfnetlink.h
··· 28 28 const struct nfnl_callback *cb; /* callback for individual types */ 29 29 int (*commit)(struct net *net, struct sk_buff *skb); 30 30 int (*abort)(struct net *net, struct sk_buff *skb); 31 + bool (*valid_genid)(struct net *net, u32 genid); 31 32 }; 32 33 33 34 int nfnetlink_subsys_register(const struct nfnetlink_subsystem *n);
+12
include/uapi/linux/netfilter/nfnetlink.h
··· 65 65 #define NFNL_MSG_BATCH_BEGIN NLMSG_MIN_TYPE 66 66 #define NFNL_MSG_BATCH_END NLMSG_MIN_TYPE+1 67 67 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 + 68 80 #endif /* _UAPI_NFNETLINK_H */
+27 -4
net/netfilter/nfnetlink.c
··· 3 3 * 4 4 * (C) 2001 by Jay Schulist <jschlst@samba.org>, 5 5 * (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> 7 7 * 8 8 * Initial netfilter messages via netlink development funded and 9 9 * generally made possible by Network Robots, Inc. (www.networkrobots.com) ··· 273 273 }; 274 274 275 275 static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh, 276 - u16 subsys_id) 276 + u16 subsys_id, u32 genid) 277 277 { 278 278 struct sk_buff *oskb = skb; 279 279 struct net *net = sock_net(skb->sk); ··· 312 312 if (!ss->commit || !ss->abort) { 313 313 nfnl_unlock(subsys_id); 314 314 netlink_ack(oskb, nlh, -EOPNOTSUPP); 315 + return kfree_skb(skb); 316 + } 317 + 318 + if (genid && ss->valid_genid && !ss->valid_genid(net, genid)) { 319 + nfnl_unlock(subsys_id); 320 + netlink_ack(oskb, nlh, -ERESTART); 315 321 return kfree_skb(skb); 316 322 } 317 323 ··· 442 436 kfree_skb(skb); 443 437 } 444 438 439 + static const struct nla_policy nfnl_batch_policy[NFNL_BATCH_MAX + 1] = { 440 + [NFNL_BATCH_GENID] = { .type = NLA_U32 }, 441 + }; 442 + 445 443 static void nfnetlink_rcv_skb_batch(struct sk_buff *skb, struct nlmsghdr *nlh) 446 444 { 445 + int min_len = nlmsg_total_size(sizeof(struct nfgenmsg)); 446 + struct nlattr *attr = (void *)nlh + min_len; 447 + struct nlattr *cda[NFNL_BATCH_MAX + 1]; 448 + int attrlen = nlh->nlmsg_len - min_len; 447 449 struct nfgenmsg *nfgenmsg; 450 + int msglen, err; 451 + u32 gen_id = 0; 448 452 u16 res_id; 449 - int msglen; 450 453 451 454 msglen = NLMSG_ALIGN(nlh->nlmsg_len); 452 455 if (msglen > skb->len) ··· 465 450 skb->len < NLMSG_HDRLEN + sizeof(struct nfgenmsg)) 466 451 return; 467 452 453 + err = nla_parse(cda, NFNL_BATCH_MAX, attr, attrlen, nfnl_batch_policy); 454 + if (err < 0) { 455 + netlink_ack(skb, nlh, err); 456 + return; 457 + } 458 + if (cda[NFNL_BATCH_GENID]) 459 + gen_id = ntohl(nla_get_be32(cda[NFNL_BATCH_GENID])); 460 + 468 461 nfgenmsg = nlmsg_data(nlh); 469 462 skb_pull(skb, msglen); 470 463 /* Work around old nft using host byte order */ ··· 481 458 else 482 459 res_id = ntohs(nfgenmsg->res_id); 483 460 484 - nfnetlink_rcv_batch(skb, nlh, res_id); 461 + nfnetlink_rcv_batch(skb, nlh, res_id, gen_id); 485 462 } 486 463 487 464 static void nfnetlink_rcv(struct sk_buff *skb)