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
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+12−0
@@ -65,4 +65,16 @@ struct nfgenmsg { · 64 unmodified lines
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+27−4
@@ -3,7 +3,7 @@ · 2 unmodified lines
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 { · 263 unmodified lines
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 @@ replay: · 35 unmodified lines
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 @@ done: · 115 unmodified lines
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) · 3 unmodified lines
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) · 2 unmodified lines
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)

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