| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include <stdlib.h> |
| 9 |
#include <time.h> |
| 10 |
#include <string.h> |
| 11 |
#include <netinet/in.h> |
| 12 |
|
| 13 |
#include <linux/netfilter.h> |
| 14 |
#include <linux/netfilter/nf_tables.h> |
| 15 |
|
| 16 |
#include <libmnl/libmnl.h> |
| 17 |
#include <libnftnl/rule.h> |
| 18 |
|
| 19 |
static int table_cb(const struct nlmsghdr *nlh, void *data) |
| 20 |
{ |
| 21 |
struct nftnl_rule *t; |
| 22 |
char buf[4096]; |
| 23 |
uint32_t *type = data; |
| 24 |
|
| 25 |
t = nftnl_rule_alloc(); |
| 26 |
if (t == NULL) { |
| 27 |
perror("OOM"); |
| 28 |
goto err; |
| 29 |
} |
| 30 |
|
| 31 |
if (nftnl_rule_nlmsg_parse(nlh, t) < 0) { |
| 32 |
perror("nftnl_rule_nlmsg_parse"); |
| 33 |
goto err_free; |
| 34 |
} |
| 35 |
|
| 36 |
nftnl_rule_snprintf(buf, sizeof(buf), t, *type, 0); |
| 37 |
printf("%s\n", buf); |
| 38 |
|
| 39 |
err_free: |
| 40 |
nftnl_rule_free(t); |
| 41 |
err: |
| 42 |
return MNL_CB_OK; |
| 43 |
} |
| 44 |
|
| 45 |
static struct nftnl_rule *setup_rule(uint8_t family, const char *table, |
| 46 |
const char *chain, const char *handle) |
| 47 |
{ |
| 48 |
struct nftnl_rule *r; |
| 49 |
uint64_t handle_num; |
| 50 |
|
| 51 |
r = nftnl_rule_alloc(); |
| 52 |
if (r == NULL) |
| 53 |
return NULL; |
| 54 |
|
| 55 |
if (table != NULL) |
| 56 |
nftnl_rule_set_str(r, NFTNL_RULE_TABLE, table); |
| 57 |
if (chain != NULL) |
| 58 |
nftnl_rule_set_str(r, NFTNL_RULE_CHAIN, chain); |
| 59 |
|
| 60 |
nftnl_rule_set_u32(r, NFTNL_RULE_FAMILY, family); |
| 61 |
|
| 62 |
if (handle != NULL) { |
| 63 |
handle_num = atoll(handle); |
| 64 |
nftnl_rule_set_u64(r, NFTNL_RULE_POSITION, handle_num); |
| 65 |
} |
| 66 |
|
| 67 |
return r; |
| 68 |
} |
| 69 |
|
| 70 |
int main(int argc, char *argv[]) |
| 71 |
{ |
| 72 |
struct mnl_socket *nl; |
| 73 |
char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 74 |
struct nlmsghdr *nlh; |
| 75 |
uint32_t portid, seq, type = NFTNL_OUTPUT_DEFAULT; |
| 76 |
const char *table = NULL, *chain = NULL; |
| 77 |
struct nftnl_rule *r; |
| 78 |
int ret, family; |
| 79 |
|
| 80 |
if (argc < 2 || argc > 5) { |
| 81 |
fprintf(stderr, "Usage: %s <family> [<table> <chain>]\n", |
| 82 |
argv[0]); |
| 83 |
exit(EXIT_FAILURE); |
| 84 |
} |
| 85 |
|
| 86 |
if (strcmp(argv[1], "ip") == 0) |
| 87 |
family = NFPROTO_IPV4; |
| 88 |
else if (strcmp(argv[1], "ip6") == 0) |
| 89 |
family = NFPROTO_IPV6; |
| 90 |
else if (strcmp(argv[1], "inet") == 0) |
| 91 |
family = NFPROTO_INET; |
| 92 |
else if (strcmp(argv[1], "bridge") == 0) |
| 93 |
family = NFPROTO_BRIDGE; |
| 94 |
else if (strcmp(argv[1], "arp") == 0) |
| 95 |
family = NFPROTO_ARP; |
| 96 |
else if (strcmp(argv[1], "unspec") == 0) |
| 97 |
family = NFPROTO_UNSPEC; |
| 98 |
else { |
| 99 |
fprintf(stderr, "Unknown family: ip, ip6, inet, bridge, arp, unspec\n"); |
| 100 |
exit(EXIT_FAILURE); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
if (argc >= 4) { |
| 105 |
table = argv[2]; |
| 106 |
chain = argv[3]; |
| 107 |
} |
| 108 |
|
| 109 |
seq = time(NULL); |
| 110 |
nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETRULE, family, |
| 111 |
NLM_F_DUMP, seq); |
| 112 |
|
| 113 |
r = setup_rule(family, table, chain, NULL); |
| 114 |
if (!r) { |
| 115 |
perror("setup_rule"); |
| 116 |
exit(EXIT_FAILURE); |
| 117 |
} |
| 118 |
nftnl_rule_nlmsg_build_payload(nlh, r); |
| 119 |
|
| 120 |
nl = mnl_socket_open(NETLINK_NETFILTER); |
| 121 |
if (nl == NULL) { |
| 122 |
perror("mnl_socket_open"); |
| 123 |
exit(EXIT_FAILURE); |
| 124 |
} |
| 125 |
|
| 126 |
if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) { |
| 127 |
perror("mnl_socket_bind"); |
| 128 |
exit(EXIT_FAILURE); |
| 129 |
} |
| 130 |
portid = mnl_socket_get_portid(nl); |
| 131 |
|
| 132 |
if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { |
| 133 |
perror("mnl_socket_send"); |
| 134 |
exit(EXIT_FAILURE); |
| 135 |
} |
| 136 |
|
| 137 |
ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); |
| 138 |
while (ret > 0) { |
| 139 |
ret = mnl_cb_run(buf, ret, seq, portid, table_cb, &type); |
| 140 |
if (ret <= 0) |
| 141 |
break; |
| 142 |
ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); |
| 143 |
} |
| 144 |
if (ret == -1) { |
| 145 |
perror("error"); |
| 146 |
exit(EXIT_FAILURE); |
| 147 |
} |
| 148 |
mnl_socket_close(nl); |
| 149 |
|
| 150 |
return EXIT_SUCCESS; |
| 151 |
} |