| 1 | #include <stdlib.h> |
| 2 | #include <time.h> |
| 3 | #include <string.h> |
| 4 | #include <netinet/in.h> |
| 5 | |
| 6 | #include <linux/netfilter.h> |
| 7 | #include <linux/netfilter/nf_tables.h> |
| 8 | |
| 9 | #include <libmnl/libmnl.h> |
| 10 | #include <libnftnl/flowtable.h> |
| 11 | |
| 12 | static int table_cb(const struct nlmsghdr *nlh, void *data) |
| 13 | { |
| 14 | struct nftnl_flowtable *t; |
| 15 | char buf[4096]; |
| 16 | uint32_t *type = data; |
| 17 | |
| 18 | t = nftnl_flowtable_alloc(); |
| 19 | if (t == NULL) { |
| 20 | perror("OOM"); |
| 21 | goto err; |
| 22 | } |
| 23 | |
| 24 | if (nftnl_flowtable_nlmsg_parse(nlh, t) < 0) { |
| 25 | perror("nftnl_flowtable_nlmsg_parse"); |
| 26 | goto err_free; |
| 27 | } |
| 28 | |
| 29 | nftnl_flowtable_snprintf(buf, sizeof(buf), t, *type, 0); |
| 30 | printf("%s\n", buf); |
| 31 | |
| 32 | err_free: |
| 33 | nftnl_flowtable_free(t); |
| 34 | err: |
| 35 | return MNL_CB_OK; |
| 36 | } |
| 37 | |
| 38 | int main(int argc, char *argv[]) |
| 39 | { |
| 40 | struct mnl_socket *nl; |
| 41 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 42 | struct nlmsghdr *nlh; |
| 43 | uint32_t portid, seq, type = NFTNL_OUTPUT_DEFAULT; |
| 44 | struct nftnl_flowtable *t = NULL; |
| 45 | int ret, family; |
| 46 | |
| 47 | seq = time(NULL); |
| 48 | |
| 49 | if (argc < 2 || argc > 5) { |
| 50 | fprintf(stderr, "Usage: %s <family> [<table> <flowtable>]\n", |
| 51 | argv[0]); |
| 52 | exit(EXIT_FAILURE); |
| 53 | } |
| 54 | |
| 55 | if (strcmp(argv[1], "ip") == 0) |
| 56 | family = NFPROTO_IPV4; |
| 57 | else if (strcmp(argv[1], "ip6") == 0) |
| 58 | family = NFPROTO_IPV6; |
| 59 | else if (strcmp(argv[1], "inet") == 0) |
| 60 | family = NFPROTO_INET; |
| 61 | else if (strcmp(argv[1], "bridge") == 0) |
| 62 | family = NFPROTO_BRIDGE; |
| 63 | else if (strcmp(argv[1], "arp") == 0) |
| 64 | family = NFPROTO_ARP; |
| 65 | else if (strcmp(argv[1], "unspec") == 0) |
| 66 | family = NFPROTO_UNSPEC; |
| 67 | else { |
| 68 | fprintf(stderr, "Unknown family: ip, ip6, inet, bridge, arp, unspec\n"); |
| 69 | exit(EXIT_FAILURE); |
| 70 | } |
| 71 | |
| 72 | if (argc >= 4) { |
| 73 | t = nftnl_flowtable_alloc(); |
| 74 | if (t == NULL) { |
| 75 | perror("OOM"); |
| 76 | exit(EXIT_FAILURE); |
| 77 | } |
| 78 | nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETFLOWTABLE, family, |
| 79 | NLM_F_ACK, seq); |
| 80 | nftnl_flowtable_set_str(t, NFTNL_FLOWTABLE_TABLE, argv[2]); |
| 81 | nftnl_flowtable_set_str(t, NFTNL_FLOWTABLE_NAME, argv[3]); |
| 82 | nftnl_flowtable_nlmsg_build_payload(nlh, t); |
| 83 | nftnl_flowtable_free(t); |
| 84 | } else if (argc >= 2) { |
| 85 | nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETFLOWTABLE, family, |
| 86 | NLM_F_DUMP, seq); |
| 87 | } |
| 88 | |
| 89 | nl = mnl_socket_open(NETLINK_NETFILTER); |
| 90 | if (nl == NULL) { |
| 91 | perror("mnl_socket_open"); |
| 92 | exit(EXIT_FAILURE); |
| 93 | } |
| 94 | |
| 95 | if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) { |
| 96 | perror("mnl_socket_bind"); |
| 97 | exit(EXIT_FAILURE); |
| 98 | } |
| 99 | portid = mnl_socket_get_portid(nl); |
| 100 | |
| 101 | if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { |
| 102 | perror("mnl_socket_send"); |
| 103 | exit(EXIT_FAILURE); |
| 104 | } |
| 105 | |
| 106 | ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); |
| 107 | while (ret > 0) { |
| 108 | ret = mnl_cb_run(buf, ret, seq, portid, table_cb, &type); |
| 109 | if (ret <= 0) |
| 110 | break; |
| 111 | ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); |
| 112 | } |
| 113 | if (ret == -1) { |
| 114 | perror("error"); |
| 115 | exit(EXIT_FAILURE); |
| 116 | } |
| 117 | mnl_socket_close(nl); |
| 118 | |
| 119 | return EXIT_SUCCESS; |
| 120 | } |