From 11219c63f4db1a803b5da4a2d308d3a5364d271f Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Fri, 24 Dec 2021 16:25:24 +0100 Subject: conntrack: use libmnl to create entry Use libmnl to create entries through the new nfct_mnl_create() helper function. Signed-off-by: Pablo Neira Ayuso --- src/conntrack.c | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/conntrack.c b/src/conntrack.c index 3f74fa1..fe604ff 100644 --- a/src/conntrack.c +++ b/src/conntrack.c @@ -2505,6 +2505,24 @@ nfct_mnl_get(uint16_t subsys, uint16_t type, mnl_cb_t cb, uint8_t family) return nfct_mnl_talk(nlh, cb); } +static int +nfct_mnl_create(uint16_t subsys, uint16_t type, const struct nf_conntrack *ct) +{ + char buf[MNL_SOCKET_BUFFER_SIZE]; + struct nlmsghdr *nlh; + int err; + + nlh = nfct_mnl_nlmsghdr_put(buf, subsys, type, + NLM_F_CREATE | NLM_F_ACK | NLM_F_EXCL, + nfct_get_attr_u8(ct, ATTR_ORIG_L3PROTO)); + + err = nfct_nlmsg_build(nlh, ct); + if (err < 0) + return err; + + return nfct_mnl_talk(nlh, NULL, NULL); +} + #define UNKNOWN_STATS_NUM 4 static int nfct_stats_attr_cb(const struct nlattr *attr, void *data) @@ -3322,14 +3340,16 @@ static int do_command_ct(const char *progname, struct ct_cmd *cmd) nfct_set_attr(cmd->tmpl.ct, ATTR_CONNLABELS, xnfct_bitmask_clone(cmd->tmpl.label_modify)); - cth = nfct_open(CONNTRACK, 0); - if (!cth) - exit_error(OTHER_PROBLEM, "Can't open handler"); + res = nfct_mnl_socket_open(0); + if (res < 0) + exit_error(OTHER_PROBLEM, "Can't open netlink socket"); - res = nfct_query(cth, NFCT_Q_CREATE, cmd->tmpl.ct); - if (res != -1) + res = nfct_mnl_create(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_NEW, + cmd->tmpl.ct); + if (res >= 0) counter++; - nfct_close(cth); + + nfct_mnl_socket_close(); break; case EXP_CREATE: -- cgit v1.2.3