16#include <netinet/in.h>
17#include <netinet/ip.h>
18#include <netinet/tcp.h>
21#include <sys/socket.h>
24#include <linux/netfilter.h>
25#include <linux/netfilter/nfnetlink.h>
26#include <linux/netfilter/nf_tables.h>
28#include <libmnl/libmnl.h>
29#include <libnftnl/set.h>
31static struct nftnl_set *setup_set(uint8_t family,
const char *table,
34 struct nftnl_set *s = NULL;
36 s = nftnl_set_alloc();
42 nftnl_set_set_str(s, NFTNL_SET_TABLE, table);
43 nftnl_set_set_str(s, NFTNL_SET_NAME, name);
44 nftnl_set_set_u32(s, NFTNL_SET_FAMILY, family);
45 nftnl_set_set_u32(s, NFTNL_SET_KEY_LEN,
sizeof(uint16_t));
47 nftnl_set_set_u32(s, NFTNL_SET_KEY_TYPE, 13);
48 nftnl_set_set_u32(s, NFTNL_SET_ID, 1);
53int main(
int argc,
char *argv[])
55 struct mnl_socket *nl;
58 struct mnl_nlmsg_batch *batch;
60 char buf[MNL_SOCKET_BUFFER_SIZE];
61 uint32_t seq = time(NULL);
65 fprintf(stderr,
"Usage: %s <family> <table> <setname>\n", argv[0]);
69 if (strcmp(argv[1],
"ip") == 0)
70 family = NFPROTO_IPV4;
71 else if (strcmp(argv[1],
"ip6") == 0)
72 family = NFPROTO_IPV6;
73 else if (strcmp(argv[1],
"inet") == 0)
74 family = NFPROTO_INET;
75 else if (strcmp(argv[1],
"bridge") == 0)
76 family = NFPROTO_BRIDGE;
77 else if (strcmp(argv[1],
"arp") == 0)
80 fprintf(stderr,
"Unknown family: ip, ip6, inet, bridge, arp\n");
84 s = setup_set(family, argv[2], argv[3]);
86 nl = mnl_socket_open(NETLINK_NETFILTER);
88 perror(
"mnl_socket_open");
92 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
93 perror(
"mnl_socket_bind");
97 batch = mnl_nlmsg_batch_start(buf,
sizeof(buf));
99 nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
100 mnl_nlmsg_batch_next(batch);
102 nlh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
103 NFT_MSG_NEWSET, family,
104 NLM_F_CREATE | NLM_F_ACK, seq++);
106 nftnl_set_nlmsg_build_payload(nlh, s);
108 mnl_nlmsg_batch_next(batch);
110 nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
111 mnl_nlmsg_batch_next(batch);
113 ret = mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
114 mnl_nlmsg_batch_size(batch));
116 perror(
"mnl_socket_sendto");
120 mnl_nlmsg_batch_stop(batch);
122 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
124 perror(
"mnl_socket_recvfrom");
128 ret = mnl_cb_run(buf, ret, 0, mnl_socket_get_portid(nl), NULL, NULL);
130 perror(
"mnl_cb_run");
134 mnl_socket_close(nl);