12 #include <netinet/in.h>
13 #include <netinet/ip.h>
14 #include <netinet/tcp.h>
15 #include <arpa/inet.h>
16 #include <sys/types.h>
17 #include <sys/socket.h>
20 #include <linux/netfilter.h>
21 #include <linux/netfilter/nfnetlink.h>
22 #include <linux/netfilter/nf_tables.h>
24 #include <libmnl/libmnl.h>
25 #include <libnftnl/set.h>
27 static struct nftnl_set *setup_set(uint8_t family,
const char *table,
30 struct nftnl_set *s = NULL;
32 s = nftnl_set_alloc();
38 nftnl_set_set_str(s, NFTNL_SET_TABLE, table);
39 nftnl_set_set_str(s, NFTNL_SET_NAME, name);
40 nftnl_set_set_u32(s, NFTNL_SET_FAMILY, family);
41 nftnl_set_set_u32(s, NFTNL_SET_KEY_LEN,
sizeof(uint16_t));
43 nftnl_set_set_u32(s, NFTNL_SET_KEY_TYPE, 13);
44 nftnl_set_set_u32(s, NFTNL_SET_ID, 1);
49 int main(
int argc,
char *argv[])
51 struct mnl_socket *nl;
54 struct mnl_nlmsg_batch *batch;
56 char buf[MNL_SOCKET_BUFFER_SIZE];
57 uint32_t seq = time(NULL);
61 fprintf(stderr,
"Usage: %s <family> <table> <setname>\n", argv[0]);
65 if (strcmp(argv[1],
"ip") == 0)
66 family = NFPROTO_IPV4;
67 else if (strcmp(argv[1],
"ip6") == 0)
68 family = NFPROTO_IPV6;
69 else if (strcmp(argv[1],
"inet") == 0)
70 family = NFPROTO_INET;
71 else if (strcmp(argv[1],
"bridge") == 0)
72 family = NFPROTO_BRIDGE;
73 else if (strcmp(argv[1],
"arp") == 0)
76 fprintf(stderr,
"Unknown family: ip, ip6, inet, bridge, arp\n");
80 s = setup_set(family, argv[2], argv[3]);
82 nl = mnl_socket_open(NETLINK_NETFILTER);
84 perror(
"mnl_socket_open");
88 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
89 perror(
"mnl_socket_bind");
93 batch = mnl_nlmsg_batch_start(buf,
sizeof(buf));
95 nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
96 mnl_nlmsg_batch_next(batch);
98 nlh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
99 NFT_MSG_NEWSET, family,
100 NLM_F_CREATE | NLM_F_ACK, seq++);
102 nftnl_set_nlmsg_build_payload(nlh, s);
104 mnl_nlmsg_batch_next(batch);
106 nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
107 mnl_nlmsg_batch_next(batch);
109 ret = mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
110 mnl_nlmsg_batch_size(batch));
112 perror(
"mnl_socket_sendto");
116 mnl_nlmsg_batch_stop(batch);
118 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
120 perror(
"mnl_socket_recvfrom");
124 ret = mnl_cb_run(buf, ret, 0, mnl_socket_get_portid(nl), NULL, NULL);
126 perror(
"mnl_cb_run");
130 mnl_socket_close(nl);