| 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 |
#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/table.h> |
| 18 |
|
| 19 |
static struct nftnl_table *table_add_parse(int argc, char *argv[]) |
| 20 |
{ |
| 21 |
struct nftnl_table *t; |
| 22 |
uint16_t family; |
| 23 |
|
| 24 |
if (strcmp(argv[1], "ip") == 0) |
| 25 |
family = NFPROTO_IPV4; |
| 26 |
else if (strcmp(argv[1], "ip6") == 0) |
| 27 |
family = NFPROTO_IPV6; |
| 28 |
else if (strcmp(argv[1], "inet") == 0) |
| 29 |
family = NFPROTO_INET; |
| 30 |
else if (strcmp(argv[1], "bridge") == 0) |
| 31 |
family = NFPROTO_BRIDGE; |
| 32 |
else if (strcmp(argv[1], "arp") == 0) |
| 33 |
family = NFPROTO_ARP; |
| 34 |
else { |
| 35 |
fprintf(stderr, "Unknown family: ip, ip6, inet, bridge, arp\n"); |
| 36 |
return NULL; |
| 37 |
} |
| 38 |
|
| 39 |
t = nftnl_table_alloc(); |
| 40 |
if (t == NULL) { |
| 41 |
perror("OOM"); |
| 42 |
return NULL; |
| 43 |
} |
| 44 |
|
| 45 |
nftnl_table_set_u32(t, NFTNL_TABLE_FAMILY, family); |
| 46 |
nftnl_table_set_str(t, NFTNL_TABLE_NAME, argv[2]); |
| 47 |
|
| 48 |
return t; |
| 49 |
} |
| 50 |
|
| 51 |
int main(int argc, char *argv[]) |
| 52 |
{ |
| 53 |
struct mnl_socket *nl; |
| 54 |
char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 55 |
struct nlmsghdr *nlh; |
| 56 |
uint32_t portid, seq, table_seq, family; |
| 57 |
struct nftnl_table *t; |
| 58 |
struct mnl_nlmsg_batch *batch; |
| 59 |
int ret; |
| 60 |
|
| 61 |
if (argc != 3) { |
| 62 |
fprintf(stderr, "%s <family> <name>\n", argv[0]); |
| 63 |
exit(EXIT_FAILURE); |
| 64 |
} |
| 65 |
|
| 66 |
t = table_add_parse(argc, argv); |
| 67 |
if (t == NULL) |
| 68 |
exit(EXIT_FAILURE); |
| 69 |
|
| 70 |
seq = time(NULL); |
| 71 |
batch = mnl_nlmsg_batch_start(buf, sizeof(buf)); |
| 72 |
|
| 73 |
nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++); |
| 74 |
mnl_nlmsg_batch_next(batch); |
| 75 |
|
| 76 |
table_seq = seq; |
| 77 |
family = nftnl_table_get_u32(t, NFTNL_TABLE_FAMILY); |
| 78 |
nlh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch), |
| 79 |
NFT_MSG_NEWTABLE, family, |
| 80 |
NLM_F_CREATE | NLM_F_ACK, seq++); |
| 81 |
nftnl_table_nlmsg_build_payload(nlh, t); |
| 82 |
nftnl_table_free(t); |
| 83 |
mnl_nlmsg_batch_next(batch); |
| 84 |
|
| 85 |
nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++); |
| 86 |
mnl_nlmsg_batch_next(batch); |
| 87 |
|
| 88 |
nl = mnl_socket_open(NETLINK_NETFILTER); |
| 89 |
if (nl == NULL) { |
| 90 |
perror("mnl_socket_open"); |
| 91 |
exit(EXIT_FAILURE); |
| 92 |
} |
| 93 |
|
| 94 |
if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) { |
| 95 |
perror("mnl_socket_bind"); |
| 96 |
exit(EXIT_FAILURE); |
| 97 |
} |
| 98 |
portid = mnl_socket_get_portid(nl); |
| 99 |
|
| 100 |
if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch), |
| 101 |
mnl_nlmsg_batch_size(batch)) < 0) { |
| 102 |
perror("mnl_socket_send"); |
| 103 |
exit(EXIT_FAILURE); |
| 104 |
} |
| 105 |
|
| 106 |
mnl_nlmsg_batch_stop(batch); |
| 107 |
|
| 108 |
ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); |
| 109 |
while (ret > 0) { |
| 110 |
ret = mnl_cb_run(buf, ret, table_seq, portid, NULL, NULL); |
| 111 |
if (ret <= 0) |
| 112 |
break; |
| 113 |
ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); |
| 114 |
} |
| 115 |
if (ret == -1) { |
| 116 |
perror("error"); |
| 117 |
exit(EXIT_FAILURE); |
| 118 |
} |
| 119 |
mnl_socket_close(nl); |
| 120 |
|
| 121 |
return EXIT_SUCCESS; |
| 122 |
} |