History

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.

Pablo Neira Ayuso committed 9 years ago

Commit 8c4d4e81 parent 48656833 files · +40 −4Browse files

Unified · Split

3 files
  1. include/linux/netfilter/nfnetlink.h +1 −0
  2. include/uapi/linux/netfilter/nfnetlink.h +12 −0
  3. net/netfilter/nfnetlink.c +27 −4
include/linux/netfilter/nfnetlink.h+1−0
@@ -28,6 +28,7 @@ struct nfnetlink_subsystem { · 27 unmodified lines
28 const struct nfnl_callback *cb; /* callback for individual types */28 const struct nfnl_callback *cb; /* callback for individual types */
29 int (*commit)(struct net *net, struct sk_buff *skb);29 int (*commit)(struct net *net, struct sk_buff *skb);
30 int (*abort)(struct net *net, struct sk_buff *skb);30 int (*abort)(struct net *net, struct sk_buff *skb);
31 bool (*valid_genid)(struct net *net, u32 genid);
31};32};
32 33
33int nfnetlink_subsys_register(const struct nfnetlink_subsystem *n);34int nfnetlink_subsys_register(const struct nfnetlink_subsystem *n);
include/uapi/linux/netfilter/nfnetlink.h+12−0
@@ -65,4 +65,16 @@ struct nfgenmsg { · 64 unmodified lines
65#define NFNL_MSG_BATCH_BEGIN NLMSG_MIN_TYPE65#define NFNL_MSG_BATCH_BEGIN NLMSG_MIN_TYPE
66#define NFNL_MSG_BATCH_END NLMSG_MIN_TYPE+166#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 */
73enum 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#endif /* _UAPI_NFNETLINK_H */80#endif /* _UAPI_NFNETLINK_H */
net/netfilter/nfnetlink.c+27−4
@@ -3,7 +3,7 @@ · 2 unmodified lines
3 *3 *
4 * (C) 2001 by Jay Schulist <jschlst@samba.org>,4 * (C) 2001 by Jay Schulist <jschlst@samba.org>,
5 * (C) 2002-2005 by Harald Welte <laforge@gnumonks.org>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 * Initial netfilter messages via netlink development funded and8 * Initial netfilter messages via netlink development funded and
9 * generally made possible by Network Robots, Inc. (www.networkrobots.com)9 * generally made possible by Network Robots, Inc. (www.networkrobots.com)
@@ -273,7 +273,7 @@ enum { · 263 unmodified lines
273};273};
274 274
275static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,275static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,
276 u16 subsys_id)276 u16 subsys_id, u32 genid)
277{277{
278 struct sk_buff *oskb = skb;278 struct sk_buff *oskb = skb;
279 struct net *net = sock_net(skb->sk);279 struct net *net = sock_net(skb->sk);
@@ -315,6 +315,12 @@ replay: · 35 unmodified lines
315 return kfree_skb(skb);315 return kfree_skb(skb);
316 }316 }
317 317
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
318 while (skb->len >= nlmsg_total_size(0)) {324 while (skb->len >= nlmsg_total_size(0)) {
319 int msglen, type;325 int msglen, type;
320 326
@@ -436,11 +442,20 @@ done: · 115 unmodified lines
436 kfree_skb(skb);442 kfree_skb(skb);
437}443}
438 444
445static const struct nla_policy nfnl_batch_policy[NFNL_BATCH_MAX + 1] = {
446 [NFNL_BATCH_GENID] = { .type = NLA_U32 },
447};
448
439static void nfnetlink_rcv_skb_batch(struct sk_buff *skb, struct nlmsghdr *nlh)449static void nfnetlink_rcv_skb_batch(struct sk_buff *skb, struct nlmsghdr *nlh)
440{450{
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;
441 struct nfgenmsg *nfgenmsg;455 struct nfgenmsg *nfgenmsg;
456 int msglen, err;
457 u32 gen_id = 0;
442 u16 res_id;458 u16 res_id;
443 int msglen;
444 459
445 msglen = NLMSG_ALIGN(nlh->nlmsg_len);460 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
446 if (msglen > skb->len)461 if (msglen > skb->len)
@@ -450,6 +465,14 @@ static void nfnetlink_rcv_skb_batch(struct sk_buff *skb, struct nlmsghdr *nlh) · 3 unmodified lines
450 skb->len < NLMSG_HDRLEN + sizeof(struct nfgenmsg))465 skb->len < NLMSG_HDRLEN + sizeof(struct nfgenmsg))
451 return;466 return;
452 467
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
453 nfgenmsg = nlmsg_data(nlh);476 nfgenmsg = nlmsg_data(nlh);
454 skb_pull(skb, msglen);477 skb_pull(skb, msglen);
455 /* Work around old nft using host byte order */478 /* 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) · 2 unmodified lines
458 else481 else
459 res_id = ntohs(nfgenmsg->res_id);482 res_id = ntohs(nfgenmsg->res_id);
460 483
461 nfnetlink_rcv_batch(skb, nlh, res_id);484 nfnetlink_rcv_batch(skb, nlh, res_id, gen_id);
462}485}
463 486
464static void nfnetlink_rcv(struct sk_buff *skb)487static void nfnetlink_rcv(struct sk_buff *skb)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t