13#include <netinet/in.h>
15#include <linux/netfilter.h>
16#include <linux/netfilter/nf_tables.h>
18#include <libmnl/libmnl.h>
19#include <libnftnl/object.h>
21static uint16_t parse_family(
char *str,
const char *option)
23 if (strcmp(str,
"ip") == 0)
25 else if (strcmp(str,
"ip6") == 0)
27 else if (strcmp(str,
"inet") == 0)
29 else if (strcmp(str,
"arp") == 0)
31 fprintf(stderr,
"Unknown %s: ip, ip6, inet, arp\n", option);
35static struct nftnl_obj *obj_parse(
int argc,
char *argv[])
40 t = nftnl_obj_alloc();
46 family = parse_family(argv[1],
"family");
47 nftnl_obj_set_u32(t, NFTNL_OBJ_FAMILY, family);
48 nftnl_obj_set_u32(t, NFTNL_OBJ_TYPE, NFT_OBJECT_CT_EXPECT);
49 nftnl_obj_set_str(t, NFTNL_OBJ_TABLE, argv[2]);
52 nftnl_obj_set_str(t, NFTNL_OBJ_NAME, argv[3]);
57static int obj_cb(
const struct nlmsghdr *nlh,
void *data)
59 uint32_t *type = data;
63 t = nftnl_obj_alloc();
69 if (nftnl_obj_nlmsg_parse(nlh, t) < 0) {
70 perror(
"nftnl_obj_nlmsg_parse");
74 nftnl_obj_snprintf(buf,
sizeof(buf), t, *type, 0);
83int main(
int argc,
char *argv[])
85 struct mnl_socket *nl;
86 char buf[MNL_SOCKET_BUFFER_SIZE];
88 uint32_t portid, seq, family;
91 uint32_t type = NFTNL_OUTPUT_DEFAULT;
92 uint16_t flags = NLM_F_ACK;
94 if (argc < 3 || argc > 4) {
95 fprintf(stderr,
"%s <family> <table> [<name>]\n", argv[0]);
99 t = obj_parse(argc, argv);
102 family = nftnl_obj_get_u32(t, NFTNL_OBJ_FAMILY);
107 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETOBJ, family, flags, seq);
108 nftnl_obj_nlmsg_build_payload(nlh, t);
111 nl = mnl_socket_open(NETLINK_NETFILTER);
113 perror(
"mnl_socket_open");
117 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
118 perror(
"mnl_socket_bind");
121 portid = mnl_socket_get_portid(nl);
123 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
124 perror(
"mnl_socket_send");
128 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
130 ret = mnl_cb_run(buf, ret, seq, portid, obj_cb, &type);
133 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
139 mnl_socket_close(nl);