Skip to contentSkip to conversation

src: netlink: Subscribe nft monitor and nft monitor trace to respective groups.

PATCH Superseded Varsha Rao 2 participants 2 messages Jul 28, 2017 netfilter

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Varsha Rao

author
#0 lore raw

PATCH v2 1/1 src: netlink: Subscribe nft monitor and nft monitor trace to respective groups.

Subscribe nft monitor to both NFNLGRP_NFTABLES and NFNLGRP_NFTRACE. nft monitor trace subscribes only to NFNLGRP_NFTRACE. Other event reporting options to only NFNLGRP_NFTABLES.

---
Changes in v1:
- Subscribe nft monitor to only _NFTABLES.
- Subsribe nft monitor trace to only _NFTRACE.

Changes in v2:
- Subsribe nft monitor to both _NFTABLES and _NFTRACE.
- Subscribe other events to _NFTABLES.
- Modified commit message.

 src/netlink.c | 35 ++++++++++++++++++++++++-----------
 1 file changed, 24 insertions(+), 11 deletions(-)
src/netlink.c +24 -11
index 9cef4c4..f7069fd 100644
@@ -3078,21 +3078,34 @@ static int netlink_events_cb(const struct nlmsghdr *nlh, void *data)
 int netlink_monitor(struct netlink_mon_handler *monhandler,
 		     struct mnl_socket *nf_sock)
 {
-	int group;
-
-	group = NFNLGRP_NFTABLES;
-	if (mnl_socket_setsockopt(nf_sock, NETLINK_ADD_MEMBERSHIP, &group,
-				  sizeof(int)) < 0)
-		return netlink_io_error(monhandler->ctx, monhandler->loc,
+	int group = NFNLGRP_NFTRACE, val;
+
+	val = monhandler->monitor_flags | NFNLGRP_NFTABLES;
+	if (val == 131439) {
+		if (mnl_socket_setsockopt(nf_sock, NETLINK_ADD_MEMBERSHIP,
+					&group, sizeof(int)) < 0)
+			return netlink_io_error(monhandler->ctx,
+					monhandler->loc,
 					"Could not bind to netlink socket %s",
 					strerror(errno));
-
-	group = NFNLGRP_NFTRACE;
-	if (mnl_socket_setsockopt(nf_sock, NETLINK_ADD_MEMBERSHIP, &group,
-				  sizeof(int)) < 0)
-		return netlink_io_error(monhandler->ctx, monhandler->loc,
+	} else {
+		if (val < 0) {
+			if (mnl_socket_setsockopt(nf_sock,
+						NETLINK_ADD_MEMBERSHIP, &group,
+						sizeof(int)) < 0)
+				return netlink_io_error(monhandler->ctx,
+						monhandler->loc,
+						"Could not bind to netlink socket %s",
+						strerror(errno));
+		}
+		group = NFNLGRP_NFTABLES;
+		if (mnl_socket_setsockopt(nf_sock, NETLINK_ADD_MEMBERSHIP,
+					&group, sizeof(int)) < 0)
+			return netlink_io_error(monhandler->ctx,
+					monhandler->loc,
 					"Could not bind to netlink socket %s",
 					strerror(errno));
+	}
 
 	return mnl_nft_event_listener(nf_sock, netlink_events_cb,
 				      monhandler);
Signature · 1 line
2.9.4
#1 lore raw
Quoted 26 lines · Varsha Rao

Subscribe nft monitor to both NFNLGRP_NFTABLES and NFNLGRP_NFTRACE. nft monitor trace subscribes only to NFNLGRP_NFTRACE. Other event reporting options to only NFNLGRP_NFTABLES.

---
Changes in v1:
- Subscribe nft monitor to only _NFTABLES.
- Subsribe nft monitor trace to only _NFTRACE.

Changes in v2:
- Subsribe nft monitor to both _NFTABLES and _NFTRACE.
- Subscribe other events to _NFTABLES.
- Modified commit message.

 src/netlink.c | 35 ++++++++++++++++++++++++-----------
 1 file changed, 24 insertions(+), 11 deletions(-)
src/netlink.c +0 -0
index 9cef4c4..f7069fd 100644
@@ -3078,21 +3078,34 @@ static int netlink_events_cb(const struct nlmsghdr *nlh, void *data)
 int netlink_monitor(struct netlink_mon_handler *monhandler,
 		     struct mnl_socket *nf_sock)
 {

src/netlink.c static int netlink_events_cb(const struct nlmsghdr *nlh, void *data) · line 3084

Quoted 10 lines
src/netlink.c +4 -6
-	int group;
-
-	group = NFNLGRP_NFTABLES;
-	if (mnl_socket_setsockopt(nf_sock, NETLINK_ADD_MEMBERSHIP, &group,
-				  sizeof(int)) < 0)
-		return netlink_io_error(monhandler->ctx, monhandler->loc,
+	int group = NFNLGRP_NFTRACE, val;
+
+	val = monhandler->monitor_flags | NFNLGRP_NFTABLES;
+	if (val == 131439) {

What is this magic number?

Please, express this via some definition, so we don't have to remember the meaning of magic number, it's good to make software more maintainable.

Thanks.