In preparation for using libmnl for ct entry creation and other operations Signed-off-by: Mikhail Sennikovsky <mikhail.sennikovskii@xxxxxxxxx> --- src/conntrack.c | 55 ++++++++++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/src/conntrack.c b/src/conntrack.c index 5bd3cb5..d37f130 100644 --- a/src/conntrack.c +++ b/src/conntrack.c @@ -2417,6 +2417,7 @@ static int nfct_mnl_socket_open(unsigned int events) static struct nlmsghdr * nfct_mnl_nlmsghdr_put(char *buf, uint16_t subsys, uint16_t type, + uint16_t flags, uint8_t family) { struct nlmsghdr *nlh; @@ -2424,7 +2425,7 @@ nfct_mnl_nlmsghdr_put(char *buf, uint16_t subsys, uint16_t type, nlh = mnl_nlmsg_put_header(buf); nlh->nlmsg_type = (subsys << 8) | type; - nlh->nlmsg_flags = NLM_F_REQUEST|NLM_F_DUMP; + nlh->nlmsg_flags = flags; nlh->nlmsg_seq = time(NULL); nfh = mnl_nlmsg_put_extra_header(nlh, sizeof(struct nfgenmsg)); @@ -2441,15 +2442,28 @@ static void nfct_mnl_socket_close(void) } static int -nfct_mnl_dump(uint16_t subsys, uint16_t type, mnl_cb_t cb, - struct ct_cmd *cmd, const struct nfct_filter_dump *filter_dump) +nfct_mnl_call(uint16_t subsys, uint16_t type, uint16_t flags, + const struct nf_conntrack *ct, uint8_t family, + const struct nfct_filter_dump *filter_dump, + mnl_cb_t cb, void* context) { - uint8_t family = cmd ? cmd->family : AF_UNSPEC; char buf[MNL_SOCKET_BUFFER_SIZE]; struct nlmsghdr *nlh; int res; - nlh = nfct_mnl_nlmsghdr_put(buf, subsys, type, family); + if (ct) { + family = nfct_get_attr_u8(ct, ATTR_ORIG_L3PROTO); + if (!family) + return -1; + } + + nlh = nfct_mnl_nlmsghdr_put(buf, subsys, type, flags, family); + + if (ct) { + res = nfct_nlmsg_build(nlh, ct); + if (res < 0) + return res; + } if (filter_dump) nfct_nlmsg_build_filter(nlh, filter_dump); @@ -2461,7 +2475,7 @@ nfct_mnl_dump(uint16_t subsys, uint16_t type, mnl_cb_t cb, res = mnl_socket_recvfrom(sock.mnl, buf, sizeof(buf)); while (res > 0) { res = mnl_cb_run(buf, res, nlh->nlmsg_seq, sock.portid, - cb, cmd); + cb, context); if (res <= MNL_CB_STOP) break; @@ -2472,23 +2486,22 @@ nfct_mnl_dump(uint16_t subsys, uint16_t type, mnl_cb_t cb, } static int -nfct_mnl_get(uint16_t subsys, uint16_t type, mnl_cb_t cb, uint8_t family) +nfct_mnl_dump(uint16_t subsys, uint16_t type, mnl_cb_t cb, + struct ct_cmd *cmd, const struct nfct_filter_dump *filter_dump) { - char buf[MNL_SOCKET_BUFFER_SIZE]; - struct nlmsghdr *nlh; - int res; - - nlh = nfct_mnl_nlmsghdr_put(buf, subsys, type, family); - - res = mnl_socket_sendto(sock.mnl, nlh, nlh->nlmsg_len); - if (res < 0) - return res; - - res = mnl_socket_recvfrom(sock.mnl, buf, sizeof(buf)); - if (res < 0) - return res; + return nfct_mnl_call(subsys, type, NLM_F_REQUEST|NLM_F_DUMP, + NULL, cmd ? cmd->family : AF_UNSPEC, + filter_dump, + cb, cmd); +} - return mnl_cb_run(buf, res, nlh->nlmsg_seq, sock.portid, cb, NULL); +static int +nfct_mnl_get(uint16_t subsys, uint16_t type, mnl_cb_t cb, uint8_t family) +{ + return nfct_mnl_call(subsys, type, NLM_F_REQUEST|NLM_F_DUMP, + NULL, family, + NULL, + cb, NULL); } #define UNKNOWN_STATS_NUM 4 -- 2.25.1