16 #include <netinet/in.h>
17 #include <netinet/ip.h>
18 #include <netinet/tcp.h>
19 #include <arpa/inet.h>
20 #include <sys/types.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>
31 static 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);
53 int 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],
"bridge") == 0)
74 family = NFPROTO_BRIDGE;
75 else if (strcmp(argv[1],
"arp") == 0)
78 fprintf(stderr,
"Unknown family: ip, ip6, bridge, arp\n");
82 s = setup_set(family, argv[2], argv[3]);
84 nl = mnl_socket_open(NETLINK_NETFILTER);
86 perror(
"mnl_socket_open");
90 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
91 perror(
"mnl_socket_bind");
95 batch = mnl_nlmsg_batch_start(buf,
sizeof(buf));
97 nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
98 mnl_nlmsg_batch_next(batch);
100 nlh = nftnl_set_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
101 NFT_MSG_NEWSET, family,
102 NLM_F_CREATE|NLM_F_ACK, seq++);
104 nftnl_set_nlmsg_build_payload(nlh, s);
106 mnl_nlmsg_batch_next(batch);
108 nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
109 mnl_nlmsg_batch_next(batch);
111 ret = mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
112 mnl_nlmsg_batch_size(batch));
114 perror(
"mnl_socket_sendto");
118 mnl_nlmsg_batch_stop(batch);
120 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
122 perror(
"mnl_socket_recvfrom");
126 ret = mnl_cb_run(buf, ret, 0, mnl_socket_get_portid(nl), NULL, NULL);
128 perror(
"mnl_cb_run");
132 mnl_socket_close(nl);