| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* (C) 2016 by Pablo Neira Ayuso <[email protected]> |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <stdlib.h> |
| 7 |
#include <time.h> |
| 8 |
#include <string.h> |
| 9 |
#include <stddef.h> /* for offsetof */ |
| 10 |
#include <netinet/in.h> |
| 11 |
#include <netinet/ip.h> |
| 12 |
#include <netinet/tcp.h> |
| 13 |
#include <arpa/inet.h> |
| 14 |
#include <sys/types.h> |
| 15 |
#include <sys/socket.h> |
| 16 |
#include <errno.h> |
| 17 |
|
| 18 |
#include <linux/netfilter.h> |
| 19 |
#include <linux/netfilter/nfnetlink.h> |
| 20 |
#include <linux/netfilter/nf_tables.h> |
| 21 |
|
| 22 |
#include <libmnl/libmnl.h> |
| 23 |
#include <libnftnl/set.h> |
| 24 |
|
| 25 |
static struct nftnl_set *setup_set(uint8_t family, const char *table, |
| 26 |
const char *name) |
| 27 |
{ |
| 28 |
struct nftnl_set *s = NULL; |
| 29 |
|
| 30 |
s = nftnl_set_alloc(); |
| 31 |
if (s == NULL) { |
| 32 |
perror("OOM"); |
| 33 |
exit(EXIT_FAILURE); |
| 34 |
} |
| 35 |
|
| 36 |
nftnl_set_set_str(s, NFTNL_SET_TABLE, table); |
| 37 |
nftnl_set_set_str(s, NFTNL_SET_NAME, name); |
| 38 |
nftnl_set_set_u32(s, NFTNL_SET_FAMILY, family); |
| 39 |
nftnl_set_set_u32(s, NFTNL_SET_KEY_LEN, 2); |
| 40 |
/* See nftables/include/datatype.h, where TYPE_INET_SERVICE is 13. We |
| 41 |
* should place these datatypes in a public header so third party |
| 42 |
* applications still work with nftables. |
| 43 |
*/ |
| 44 |
nftnl_set_set_u32(s, NFTNL_SET_KEY_TYPE, 13); |
| 45 |
nftnl_set_set_u32(s, NFTNL_SET_DATA_LEN, 2); |
| 46 |
nftnl_set_set_u32(s, NFTNL_SET_DATA_TYPE, 13); |
| 47 |
nftnl_set_set_u32(s, NFTNL_SET_ID, 1); |
| 48 |
nftnl_set_set_u32(s, NFTNL_SET_FLAGS, NFT_SET_CONSTANT | NFT_SET_MAP); |
| 49 |
|
| 50 |
return s; |
| 51 |
} |
| 52 |
|
| 53 |
int main(int argc, char *argv[]) |
| 54 |
{ |
| 55 |
struct mnl_socket *nl; |
| 56 |
struct nftnl_set *s; |
| 57 |
struct nlmsghdr *nlh; |
| 58 |
struct mnl_nlmsg_batch *batch; |
| 59 |
uint8_t family; |
| 60 |
char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 61 |
uint32_t seq = time(NULL); |
| 62 |
int ret; |
| 63 |
|
| 64 |
if (argc != 4) { |
| 65 |
fprintf(stderr, "Usage: %s <family> <table> <setname>\n", argv[0]); |
| 66 |
exit(EXIT_FAILURE); |
| 67 |
} |
| 68 |
|
| 69 |
if (strcmp(argv[1], "ip") == 0) |
| 70 |
family = NFPROTO_IPV4; |
| 71 |
else if (strcmp(argv[1], "ip6") == 0) |
| 72 |
family = NFPROTO_IPV6; |
| 73 |
else if (strcmp(argv[1], "inet") == 0) |
| 74 |
family = NFPROTO_INET; |
| 75 |
else if (strcmp(argv[1], "bridge") == 0) |
| 76 |
family = NFPROTO_BRIDGE; |
| 77 |
else if (strcmp(argv[1], "arp") == 0) |
| 78 |
family = NFPROTO_ARP; |
| 79 |
else { |
| 80 |
fprintf(stderr, "Unknown family: ip, ip6, bridge, arp\n"); |
| 81 |
exit(EXIT_FAILURE); |
| 82 |
} |
| 83 |
|
| 84 |
s = setup_set(family, argv[2], argv[3]); |
| 85 |
|
| 86 |
nl = mnl_socket_open(NETLINK_NETFILTER); |
| 87 |
if (nl == NULL) { |
| 88 |
perror("mnl_socket_open"); |
| 89 |
exit(EXIT_FAILURE); |
| 90 |
} |
| 91 |
|
| 92 |
if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) { |
| 93 |
perror("mnl_socket_bind"); |
| 94 |
exit(EXIT_FAILURE); |
| 95 |
} |
| 96 |
|
| 97 |
batch = mnl_nlmsg_batch_start(buf, sizeof(buf)); |
| 98 |
|
| 99 |
nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++); |
| 100 |
mnl_nlmsg_batch_next(batch); |
| 101 |
|
| 102 |
nlh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch), |
| 103 |
NFT_MSG_NEWSET, family, |
| 104 |
NLM_F_CREATE | NLM_F_ACK, seq++); |
| 105 |
|
| 106 |
nftnl_set_nlmsg_build_payload(nlh, s); |
| 107 |
nftnl_set_free(s); |
| 108 |
mnl_nlmsg_batch_next(batch); |
| 109 |
|
| 110 |
nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++); |
| 111 |
mnl_nlmsg_batch_next(batch); |
| 112 |
|
| 113 |
ret = mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch), |
| 114 |
mnl_nlmsg_batch_size(batch)); |
| 115 |
if (ret == -1) { |
| 116 |
perror("mnl_socket_sendto"); |
| 117 |
exit(EXIT_FAILURE); |
| 118 |
} |
| 119 |
|
| 120 |
mnl_nlmsg_batch_stop(batch); |
| 121 |
|
| 122 |
ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); |
| 123 |
if (ret == -1) { |
| 124 |
perror("mnl_socket_recvfrom"); |
| 125 |
exit(EXIT_FAILURE); |
| 126 |
} |
| 127 |
|
| 128 |
ret = mnl_cb_run(buf, ret, 0, mnl_socket_get_portid(nl), NULL, NULL); |
| 129 |
if (ret < 0) { |
| 130 |
perror("mnl_cb_run"); |
| 131 |
exit(EXIT_FAILURE); |
| 132 |
} |
| 133 |
|
| 134 |
mnl_socket_close(nl); |
| 135 |
|
| 136 |
return EXIT_SUCCESS; |
| 137 |
} |