From: Easynet <devel@easynet.dev>
To: netfilter-devel@vger.kernel.org
Subject: libnftnl adding element to a set of type ipv4_addr or ipv6_addr
Date: Tue, 18 Jul 2023 21:02:02 +0200 [thread overview]
Message-ID: <ff54bc23-95f3-8300-c9d4-e5d74581a0e7@easynet.dev> (raw)
Hi,
I'm building a small firewall daemon that it receives if an user is
authenticated and then is adding his IP in a set to be allowed for 24h.
I'm new in nftnl library and I started to read the documentation and
also the examples.
Until now I was able to add in my daemon these tools based on libnftnl:
- create / delete / get tables
- create / delete chains
- create / delete sets.
Right now I'm facing an issue that I can't understand how to build the
nftnl packet for adding an element to my set, which has interval and
timeout flags.
The function I'm using it looks very similar to one in libnftnl, with
modifications:
/*
* Set add element
*/
int32_t nft_set_element_add(char *req_family, char *table_name, char
*set_name, char *element)
{
struct mnl_socket *nl;
char buf[MNL_SOCKET_BUFFER_SIZE];
struct mnl_nlmsg_batch *batch;
struct nlmsghdr *nlh;
uint32_t portid, seq, family;
struct nftnl_set *s;
struct nftnl_set_elem *e;
uint16_t data;
uint32_t mask;
uint32_t data32;
uint64_t data64;
uint64_t timeout;
char strAddr[32];
in_addr_t ipv4;
int ret;
s = nftnl_set_alloc();
if (s == NULL) {
perror("OOM");
exit(EXIT_FAILURE);
}
seq = time(NULL);
family = nft_check_table_family(req_family);
nftnl_set_set_str(s, NFTNL_SET_TABLE, table_name);
nftnl_set_set_str(s, NFTNL_SET_NAME, set_name);
/* Add to dummy elements to set */
e = nftnl_set_elem_alloc();
if (e == NULL) {
perror("OOM");
exit(EXIT_FAILURE);
}
strcpy(strAddr, "192.168.10.10");
ipv4 = inet_addr(strAddr);
printf("IPv4: %u\n", ipv4);
//timeout = 3600;
nftnl_set_elem_set(e, NFTNL_SET_ELEM_KEY, &ipv4, sizeof(ipv4));
//nftnl_set_elem_set(e, NFTNL_SET_ELEM_TIMEOUT, &timeout ,
sizeof(timeout));
nftnl_set_elem_add(s, e);
batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
mnl_nlmsg_batch_next(batch);
nlh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
NFT_MSG_NEWSETELEM, family,
NLM_F_CREATE | NLM_F_EXCL | NLM_F_ACK,
seq++);
nftnl_set_elems_nlmsg_build_payload(nlh, s);
nftnl_set_free(s);
mnl_nlmsg_batch_next(batch);
nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
mnl_nlmsg_batch_next(batch);
nl = mnl_socket_open(NETLINK_NETFILTER);
if (nl == NULL) {
perror("mnl_socket_open");
exit(EXIT_FAILURE);
}
if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
perror("mnl_socket_bind");
exit(EXIT_FAILURE);
}
portid = mnl_socket_get_portid(nl);
if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
mnl_nlmsg_batch_size(batch)) < 0) {
perror("mnl_socket_send");
return(EXIT_FAILURE);
}
mnl_nlmsg_batch_stop(batch);
ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
while (ret > 0) {
ret = mnl_cb_run(buf, ret, 0, portid, NULL, NULL);
if (ret <= 0)
break;
ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
}
if (ret == -1) {
perror("error");
return EXIT_FAILURE;
}
mnl_socket_close(nl);
return EXIT_SUCCESS;
}
I was able to add timeout flag for the IP, but the IP is added as a range:
set IPTVv4_ALLOW {
type ipv4_addr
flags interval,timeout
elements = { 192.168.10.10-255.255.255.255 }
}
I couldn't find too much information in the documentation or looking
into the nftables code or Linux Kernel. For me are too complicated to
understand the logic behind.
Can somebody advice me how can I implement the nflnl message correctly
to be able to add just the host for example? Or where I can find the
info about how nftnl message should be created?
Kind regards.
next reply other threads:[~2023-07-18 19:07 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-18 19:02 Easynet [this message]
2023-07-19 10:53 ` libnftnl adding element to a set of type ipv4_addr or ipv6_addr Phil Sutter
2023-07-19 11:31 ` Easynet
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ff54bc23-95f3-8300-c9d4-e5d74581a0e7@easynet.dev \
--to=devel@easynet.dev \
--cc=netfilter-devel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).