netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH conntrack-tools 1/2] conntrack: pass command object to nfct_mnl_request()
@ 2022-06-13 17:27 Pablo Neira Ayuso
  2022-06-13 17:27 ` [PATCH conntrack-tools 2/2] conntrack: update CT_GET to use libmnl Pablo Neira Ayuso
  0 siblings, 1 reply; 2+ messages in thread
From: Pablo Neira Ayuso @ 2022-06-13 17:27 UTC (permalink / raw)
  To: netfilter-devel

This patch comes in preparation for updating the CT_GET command to use
libmnl.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
 src/conntrack.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/src/conntrack.c b/src/conntrack.c
index 949ba1f22d0a..615aa01ed6be 100644
--- a/src/conntrack.c
+++ b/src/conntrack.c
@@ -2039,7 +2039,8 @@ done:
 
 static int nfct_mnl_request(struct nfct_mnl_socket *sock, uint16_t subsys,
 			    int family, uint16_t type, uint16_t flags,
-			    mnl_cb_t cb, const struct nf_conntrack *ct);
+			    mnl_cb_t cb, const struct nf_conntrack *ct,
+			    const struct ct_cmd *cmd);
 
 static int mnl_nfct_delete_cb(const struct nlmsghdr *nlh, void *data)
 {
@@ -2062,7 +2063,7 @@ static int mnl_nfct_delete_cb(const struct nlmsghdr *nlh, void *data)
 
 	res = nfct_mnl_request(modifier_sock, NFNL_SUBSYS_CTNETLINK,
 			       nfct_get_attr_u8(ct, ATTR_ORIG_L3PROTO),
-			       IPCTNL_MSG_CT_DELETE, NLM_F_ACK, NULL, ct);
+			       IPCTNL_MSG_CT_DELETE, NLM_F_ACK, NULL, ct, NULL);
 	if (res < 0)
 		exit_error(OTHER_PROBLEM,
 			   "Operation failed: %s",
@@ -2259,7 +2260,7 @@ static int mnl_nfct_update_cb(const struct nlmsghdr *nlh, void *data)
 		goto destroy_ok;
 
 	res = nfct_mnl_request(modifier_sock, NFNL_SUBSYS_CTNETLINK, cmd->family,
-			       IPCTNL_MSG_CT_NEW, NLM_F_ACK, NULL, tmp);
+			       IPCTNL_MSG_CT_NEW, NLM_F_ACK, NULL, tmp, NULL);
 	if (res < 0) {
 		fprintf(stderr, "Operation failed: %s\n",
 			err2str(errno, CT_UPDATE));
@@ -2267,7 +2268,7 @@ static int mnl_nfct_update_cb(const struct nlmsghdr *nlh, void *data)
 
 	res = nfct_mnl_request(modifier_sock, NFNL_SUBSYS_CTNETLINK, cmd->family,
 			       IPCTNL_MSG_CT_GET, NLM_F_ACK,
-			       mnl_nfct_print_cb, tmp);
+			       mnl_nfct_print_cb, tmp, NULL);
 	if (res < 0) {
 		/* the entry has vanish in middle of the update */
 		if (errno == ENOENT)
@@ -2529,7 +2530,8 @@ nfct_mnl_dump(struct nfct_mnl_socket *sock, uint16_t subsys, uint16_t type,
 }
 
 static int nfct_mnl_talk(struct nfct_mnl_socket *sock,
-			 const struct nlmsghdr *nlh, mnl_cb_t cb)
+			 const struct nlmsghdr *nlh, mnl_cb_t cb,
+			 const struct ct_cmd *cmd)
 {
 	char buf[MNL_SOCKET_BUFFER_SIZE];
 	int ret;
@@ -2542,12 +2544,13 @@ static int nfct_mnl_talk(struct nfct_mnl_socket *sock,
 	if (ret < 0)
 		return ret;
 
-	return mnl_cb_run(buf, ret, nlh->nlmsg_seq, sock->portid, cb, NULL);
+	return mnl_cb_run(buf, ret, nlh->nlmsg_seq, sock->portid, cb, (void *)cmd);
 }
 
 static int nfct_mnl_request(struct nfct_mnl_socket *sock, uint16_t subsys,
 			    int family, uint16_t type, uint16_t flags,
-			    mnl_cb_t cb, const struct nf_conntrack *ct)
+			    mnl_cb_t cb, const struct nf_conntrack *ct,
+			    const struct ct_cmd *cmd)
 {
 	char buf[MNL_SOCKET_BUFFER_SIZE];
 	struct nlmsghdr *nlh;
@@ -2561,7 +2564,7 @@ static int nfct_mnl_request(struct nfct_mnl_socket *sock, uint16_t subsys,
 			return err;
 	}
 
-	return nfct_mnl_talk(sock, nlh, cb);
+	return nfct_mnl_talk(sock, nlh, cb, cmd);
 }
 
 #define UNKNOWN_STATS_NUM 4
@@ -3381,7 +3384,7 @@ static int do_command_ct(const char *progname, struct ct_cmd *cmd,
 		res = nfct_mnl_request(sock, NFNL_SUBSYS_CTNETLINK, cmd->family,
 				       IPCTNL_MSG_CT_NEW,
 				       NLM_F_CREATE | NLM_F_ACK | NLM_F_EXCL,
-				       NULL, cmd->tmpl.ct);
+				       NULL, cmd->tmpl.ct, NULL);
 		if (res >= 0)
 			counter++;
 
@@ -3471,7 +3474,7 @@ static int do_command_ct(const char *progname, struct ct_cmd *cmd,
 
 	case CT_FLUSH:
 		res = nfct_mnl_request(sock, NFNL_SUBSYS_CTNETLINK, cmd->family,
-				       IPCTNL_MSG_CT_DELETE, NLM_F_ACK, NULL, NULL);
+				       IPCTNL_MSG_CT_DELETE, NLM_F_ACK, NULL, NULL, NULL);
 
 		fprintf(stderr, "%s v%s (conntrack-tools): ",PROGNAME,VERSION);
 		fprintf(stderr,"connection tracking table has been emptied.\n");
@@ -3594,7 +3597,7 @@ static int do_command_ct(const char *progname, struct ct_cmd *cmd,
 		 */
 		res = nfct_mnl_request(sock, NFNL_SUBSYS_CTNETLINK, AF_UNSPEC,
 				       IPCTNL_MSG_CT_GET_STATS, 0,
-				       nfct_global_stats_cb, NULL);
+				       nfct_global_stats_cb, NULL, NULL);
 
 		/* don't look at /proc, we got the information via ctnetlink */
 		if (res >= 0)
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH conntrack-tools 2/2] conntrack: update CT_GET to use libmnl
  2022-06-13 17:27 [PATCH conntrack-tools 1/2] conntrack: pass command object to nfct_mnl_request() Pablo Neira Ayuso
@ 2022-06-13 17:27 ` Pablo Neira Ayuso
  0 siblings, 0 replies; 2+ messages in thread
From: Pablo Neira Ayuso @ 2022-06-13 17:27 UTC (permalink / raw)
  To: netfilter-devel

Use nfct_mnl_request() to build and send the netlink command. Remove
dump_cb() since this is a copy of the new libmnl's mnl_nfct_dump_cb()
callback function.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
 src/conntrack.c | 51 +++----------------------------------------------
 1 file changed, 3 insertions(+), 48 deletions(-)

diff --git a/src/conntrack.c b/src/conntrack.c
index 615aa01ed6be..82534ec9fb02 100644
--- a/src/conntrack.c
+++ b/src/conntrack.c
@@ -1996,47 +1996,6 @@ out:
 	return MNL_CB_OK;
 }
 
-static int dump_cb(enum nf_conntrack_msg_type type,
-		   struct nf_conntrack *ct,
-		   void *data)
-{
-	unsigned int op_type = NFCT_O_DEFAULT;
-	unsigned int op_flags = 0;
-	struct ct_cmd *cmd = data;
-	char buf[1024];
-
-	if (nfct_filter(cmd, ct, cur_tmpl))
-		return NFCT_CB_CONTINUE;
-
-	if (output_mask & _O_SAVE) {
-		ct_save_snprintf(buf, sizeof(buf), ct, labelmap, NFCT_T_NEW);
-		goto done;
-	}
-
-	if (output_mask & _O_XML) {
-		op_type = NFCT_O_XML;
-		if (dump_xml_header_done) {
-			dump_xml_header_done = 0;
-			printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
-			       "<conntrack>\n");
-		}
-	}
-	if (output_mask & _O_EXT)
-		op_flags = NFCT_OF_SHOW_LAYER3;
-	if (output_mask & _O_KTMS)
-		op_flags |= NFCT_OF_TIMESTAMP;
-	if (output_mask & _O_ID)
-		op_flags |= NFCT_OF_ID;
-
-	nfct_snprintf_labels(buf, sizeof(buf), ct, NFCT_T_UNKNOWN, op_type, op_flags, labelmap);
-done:
-	printf("%s\n", buf);
-
-	counter++;
-
-	return NFCT_CB_CONTINUE;
-}
-
 static int nfct_mnl_request(struct nfct_mnl_socket *sock, uint16_t subsys,
 			    int family, uint16_t type, uint16_t flags,
 			    mnl_cb_t cb, const struct nf_conntrack *ct,
@@ -3451,13 +3410,9 @@ static int do_command_ct(const char *progname, struct ct_cmd *cmd,
 		break;
 
 	case CT_GET:
-		cth = nfct_open(CONNTRACK, 0);
-		if (!cth)
-			exit_error(OTHER_PROBLEM, "Can't open handler");
-
-		nfct_callback_register(cth, NFCT_T_ALL, dump_cb, cmd);
-		res = nfct_query(cth, NFCT_Q_GET, cmd->tmpl.ct);
-		nfct_close(cth);
+		res = nfct_mnl_request(sock, NFNL_SUBSYS_CTNETLINK, cmd->family,
+				       IPCTNL_MSG_CT_GET, NLM_F_ACK,
+				       mnl_nfct_dump_cb, cmd->tmpl.ct, cmd);
 		break;
 
 	case EXP_GET:
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-06-13 19:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-06-13 17:27 [PATCH conntrack-tools 1/2] conntrack: pass command object to nfct_mnl_request() Pablo Neira Ayuso
2022-06-13 17:27 ` [PATCH conntrack-tools 2/2] conntrack: update CT_GET to use libmnl Pablo Neira Ayuso

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).