12 #include <netinet/in.h>
14 #include <linux/netfilter.h>
15 #include <linux/netfilter/nf_tables.h>
17 #include <libmnl/libmnl.h>
18 #include <libnftnl/object.h>
20 static int obj_cb(
const struct nlmsghdr *nlh,
void *data)
24 uint32_t *type = data;
26 t = nftnl_obj_alloc();
32 if (nftnl_obj_nlmsg_parse(nlh, t) < 0) {
33 perror(
"nftnl_obj_nlmsg_parse");
37 nftnl_obj_snprintf(buf,
sizeof(buf), t, *type, 0);
46 int main(
int argc,
char *argv[])
48 struct mnl_socket *nl;
49 char buf[MNL_SOCKET_BUFFER_SIZE];
51 uint32_t portid, seq, family;
52 struct nftnl_obj *t = NULL;
54 uint32_t type = NFTNL_OUTPUT_DEFAULT;
56 if (argc < 3 || argc > 5) {
57 fprintf(stderr,
"%s <family> <table> [<obj>]\n", argv[0]);
61 if (strcmp(argv[1],
"ip") == 0)
62 family = NFPROTO_IPV4;
63 else if (strcmp(argv[1],
"ip6") == 0)
64 family = NFPROTO_IPV6;
65 else if (strcmp(argv[1],
"inet") == 0)
66 family = NFPROTO_INET;
67 else if (strcmp(argv[1],
"unspec") == 0)
68 family = NFPROTO_UNSPEC;
70 fprintf(stderr,
"Unknown family: ip, ip6, inet, unspec");
74 if (argc == 3 || argc == 4) {
75 t = nftnl_obj_alloc();
83 nftnl_obj_set_u32(t, NFTNL_OBJ_TYPE, NFT_OBJECT_CT_TIMEOUT);
85 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETOBJ, family,
88 nftnl_obj_set_str(t, NFTNL_OBJ_TABLE, argv[2]);
89 nftnl_obj_nlmsg_build_payload(nlh, t);
93 nftnl_obj_set_str(t, NFTNL_OBJ_TABLE, argv[2]);
94 nftnl_obj_set_str(t, NFTNL_OBJ_NAME, argv[3]);
96 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETOBJ, family,
98 nftnl_obj_nlmsg_build_payload(nlh, t);
102 nl = mnl_socket_open(NETLINK_NETFILTER);
104 perror(
"mnl_socket_open");
108 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
109 perror(
"mnl_socket_bind");
112 portid = mnl_socket_get_portid(nl);
114 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
115 perror(
"mnl_socket_send");
119 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
121 ret = mnl_cb_run(buf, ret, seq, portid, obj_cb, &type);
124 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
130 mnl_socket_close(nl);