| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* (C) 2012 by Pablo Neira Ayuso <[email protected]> |
| 4 |
* |
| 5 |
* This software has been sponsored by Sophos Astaro <http://www.sophos.com> |
| 6 |
*/ |
| 7 |
|
| 8 |
|
| 9 |
#include <stdlib.h> |
| 10 |
#include <time.h> |
| 11 |
#include <string.h> |
| 12 |
#include <netinet/in.h> |
| 13 |
|
| 14 |
#include <linux/netfilter.h> |
| 15 |
#include <linux/netfilter/nf_tables.h> |
| 16 |
|
| 17 |
#include <libmnl/libmnl.h> |
| 18 |
#include <libnftnl/object.h> |
| 19 |
|
| 20 |
static int obj_cb(const struct nlmsghdr *nlh, void *data) |
| 21 |
{ |
| 22 |
struct nftnl_obj *t; |
| 23 |
char buf[4096]; |
| 24 |
uint32_t *type = data; |
| 25 |
|
| 26 |
t = nftnl_obj_alloc(); |
| 27 |
if (t == NULL) { |
| 28 |
perror("OOM"); |
| 29 |
goto err; |
| 30 |
} |
| 31 |
|
| 32 |
if (nftnl_obj_nlmsg_parse(nlh, t) < 0) { |
| 33 |
perror("nftnl_obj_nlmsg_parse"); |
| 34 |
goto err_free; |
| 35 |
} |
| 36 |
|
| 37 |
nftnl_obj_snprintf(buf, sizeof(buf), t, *type, 0); |
| 38 |
printf("%s\n", buf); |
| 39 |
|
| 40 |
err_free: |
| 41 |
nftnl_obj_free(t); |
| 42 |
err: |
| 43 |
return MNL_CB_OK; |
| 44 |
} |
| 45 |
|
| 46 |
int main(int argc, char *argv[]) |
| 47 |
{ |
| 48 |
struct mnl_socket *nl; |
| 49 |
char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 50 |
struct nlmsghdr *nlh; |
| 51 |
uint32_t portid, seq, family; |
| 52 |
struct nftnl_obj *t = NULL; |
| 53 |
int ret; |
| 54 |
uint32_t type = NFTNL_OUTPUT_DEFAULT; |
| 55 |
|
| 56 |
if (argc < 3 || argc > 5) { |
| 57 |
fprintf(stderr, "%s <family> <table> [<obj>]\n", |
| 58 |
argv[0]); |
| 59 |
return EXIT_FAILURE; |
| 60 |
} |
| 61 |
|
| 62 |
if (strcmp(argv[1], "ip") == 0) |
| 63 |
family = NFPROTO_IPV4; |
| 64 |
else if (strcmp(argv[1], "ip6") == 0) |
| 65 |
family = NFPROTO_IPV6; |
| 66 |
else if (strcmp(argv[1], "inet") == 0) |
| 67 |
family = NFPROTO_INET; |
| 68 |
else if (strcmp(argv[1], "unspec") == 0) |
| 69 |
family = NFPROTO_UNSPEC; |
| 70 |
else { |
| 71 |
fprintf(stderr, "Unknown family: ip, ip6, inet, unspec"); |
| 72 |
exit(EXIT_FAILURE); |
| 73 |
} |
| 74 |
|
| 75 |
if (argc == 3 || argc == 4) { |
| 76 |
t = nftnl_obj_alloc(); |
| 77 |
if (t == NULL) { |
| 78 |
perror("OOM"); |
| 79 |
exit(EXIT_FAILURE); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
seq = time(NULL); |
| 84 |
nftnl_obj_set_u32(t, NFTNL_OBJ_TYPE, NFT_OBJECT_CT_HELPER); |
| 85 |
if (argc < 4) { |
| 86 |
nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETOBJ, family, |
| 87 |
NLM_F_DUMP, seq); |
| 88 |
if (argc == 3) { |
| 89 |
nftnl_obj_set_str(t, NFTNL_OBJ_TABLE, argv[2]); |
| 90 |
nftnl_obj_nlmsg_build_payload(nlh, t); |
| 91 |
nftnl_obj_free(t); |
| 92 |
} |
| 93 |
} else { |
| 94 |
nftnl_obj_set_str(t, NFTNL_OBJ_TABLE, argv[2]); |
| 95 |
nftnl_obj_set_str(t, NFTNL_OBJ_NAME, argv[3]); |
| 96 |
|
| 97 |
nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETOBJ, family, |
| 98 |
NLM_F_ACK, seq); |
| 99 |
nftnl_obj_nlmsg_build_payload(nlh, t); |
| 100 |
nftnl_obj_free(t); |
| 101 |
} |
| 102 |
|
| 103 |
nl = mnl_socket_open(NETLINK_NETFILTER); |
| 104 |
if (nl == NULL) { |
| 105 |
perror("mnl_socket_open"); |
| 106 |
exit(EXIT_FAILURE); |
| 107 |
} |
| 108 |
|
| 109 |
if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) { |
| 110 |
perror("mnl_socket_bind"); |
| 111 |
exit(EXIT_FAILURE); |
| 112 |
} |
| 113 |
portid = mnl_socket_get_portid(nl); |
| 114 |
|
| 115 |
if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { |
| 116 |
perror("mnl_socket_send"); |
| 117 |
exit(EXIT_FAILURE); |
| 118 |
} |
| 119 |
|
| 120 |
ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); |
| 121 |
while (ret > 0) { |
| 122 |
ret = mnl_cb_run(buf, ret, seq, portid, obj_cb, &type); |
| 123 |
if (ret <= 0) |
| 124 |
break; |
| 125 |
ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); |
| 126 |
} |
| 127 |
if (ret == -1) { |
| 128 |
perror("error"); |
| 129 |
exit(EXIT_FAILURE); |
| 130 |
} |
| 131 |
mnl_socket_close(nl); |
| 132 |
|
| 133 |
return EXIT_SUCCESS; |
| 134 |
} |