9 #include <netinet/in.h>
11 #include <linux/netfilter.h>
12 #include <linux/netfilter/nf_tables.h>
14 #include <libmnl/libmnl.h>
15 #include <libnftnl/object.h>
17 static uint16_t parse_family(
char *str,
const char *option)
19 if (strcmp(str,
"ip") == 0)
21 else if (strcmp(str,
"ip6") == 0)
23 else if (strcmp(str,
"inet") == 0)
25 else if (strcmp(str,
"arp") == 0)
27 fprintf(stderr,
"Unknown %s: ip, ip6, inet, arp\n", option);
31 static struct nftnl_obj *obj_parse(
int argc,
char *argv[])
36 t = nftnl_obj_alloc();
42 family = parse_family(argv[1],
"family");
43 nftnl_obj_set_u32(t, NFTNL_OBJ_FAMILY, family);
44 nftnl_obj_set_u32(t, NFTNL_OBJ_TYPE, NFT_OBJECT_CT_EXPECT);
45 nftnl_obj_set_str(t, NFTNL_OBJ_TABLE, argv[2]);
48 nftnl_obj_set_str(t, NFTNL_OBJ_NAME, argv[3]);
53 static int obj_cb(
const struct nlmsghdr *nlh,
void *data)
55 uint32_t *type = data;
59 t = nftnl_obj_alloc();
65 if (nftnl_obj_nlmsg_parse(nlh, t) < 0) {
66 perror(
"nftnl_obj_nlmsg_parse");
70 nftnl_obj_snprintf(buf,
sizeof(buf), t, *type, 0);
79 int main(
int argc,
char *argv[])
81 struct mnl_socket *nl;
82 char buf[MNL_SOCKET_BUFFER_SIZE];
84 uint32_t portid, seq, family;
87 uint32_t type = NFTNL_OUTPUT_DEFAULT;
88 uint16_t flags = NLM_F_ACK;
90 if (argc < 3 || argc > 4) {
91 fprintf(stderr,
"%s <family> <table> [<name>]\n", argv[0]);
95 t = obj_parse(argc, argv);
98 family = nftnl_obj_get_u32(t, NFTNL_OBJ_FAMILY);
103 nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETOBJ, family, flags, seq);
104 nftnl_obj_nlmsg_build_payload(nlh, t);
107 nl = mnl_socket_open(NETLINK_NETFILTER);
109 perror(
"mnl_socket_open");
113 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
114 perror(
"mnl_socket_bind");
117 portid = mnl_socket_get_portid(nl);
119 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
120 perror(
"mnl_socket_send");
124 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
126 ret = mnl_cb_run(buf, ret, seq, portid, obj_cb, &type);
129 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
135 mnl_socket_close(nl);