11 #include <netinet/in.h>
13 #include <linux/netfilter.h>
14 #include <linux/netfilter/nf_tables.h>
16 #include <libmnl/libmnl.h>
17 #include <libnftnl/object.h>
19 static int obj_cb(
const struct nlmsghdr *nlh,
void *data)
23 uint32_t *type = data;
25 t = nftnl_obj_alloc();
31 if (nftnl_obj_nlmsg_parse(nlh, t) < 0) {
32 perror(
"nftnl_obj_nlmsg_parse");
36 nftnl_obj_snprintf(buf,
sizeof(buf), t, *type, 0);
45 int main(
int argc,
char *argv[])
47 struct mnl_socket *nl;
48 char buf[MNL_SOCKET_BUFFER_SIZE];
50 uint32_t portid, seq, family;
51 struct nftnl_obj *t = NULL;
53 uint32_t type = NFTNL_OUTPUT_DEFAULT;
55 if (argc < 2 || argc > 5) {
56 fprintf(stderr,
"%s <family> <table> [<obj>]\n", argv[0]);
60 if (strcmp(argv[1],
"ip") == 0)
61 family = NFPROTO_IPV4;
62 else if (strcmp(argv[1],
"ip6") == 0)
63 family = NFPROTO_IPV6;
64 else if (strcmp(argv[1],
"inet") == 0)
65 family = NFPROTO_INET;
66 else if (strcmp(argv[1],
"bridge") == 0)
67 family = NFPROTO_BRIDGE;
68 else if (strcmp(argv[1],
"arp") == 0)
70 else if (strcmp(argv[1],
"unspec") == 0)
71 family = NFPROTO_UNSPEC;
73 fprintf(stderr,
"Unknown family: ip, ip6, inet, bridge, arp, unspec\n");
77 if (argc == 3 || argc == 4) {
78 t = nftnl_obj_alloc();
87 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETOBJ, family,
90 nftnl_obj_set_str(t, NFTNL_OBJ_TABLE, argv[2]);
91 nftnl_obj_nlmsg_build_payload(nlh, t);
95 nftnl_obj_set_str(t, NFTNL_OBJ_TABLE, argv[2]);
96 nftnl_obj_set_str(t, NFTNL_OBJ_NAME, argv[3]);
97 nftnl_obj_set_u32(t, NFTNL_OBJ_TYPE, NFT_OBJECT_COUNTER);
99 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETOBJ, family,
101 nftnl_obj_nlmsg_build_payload(nlh, t);
105 nl = mnl_socket_open(NETLINK_NETFILTER);
107 perror(
"mnl_socket_open");
111 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
112 perror(
"mnl_socket_bind");
115 portid = mnl_socket_get_portid(nl);
117 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
118 perror(
"mnl_socket_send");
122 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
124 ret = mnl_cb_run(buf, ret, seq, portid, obj_cb, &type);
127 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
133 mnl_socket_close(nl);