libnftnl 1.2.6
nft-set-add.c
1/*
2 * (C) 2013 by Pablo Neira Ayuso <pablo@netfilter.org>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This software has been sponsored by Sophos Astaro <http://www.sophos.com>
10 */
11
12#include <stdlib.h>
13#include <time.h>
14#include <string.h>
15#include <stddef.h> /* for offsetof */
16#include <netinet/in.h>
17#include <netinet/ip.h>
18#include <netinet/tcp.h>
19#include <arpa/inet.h>
20#include <sys/types.h>
21#include <sys/socket.h>
22#include <errno.h>
23
24#include <linux/netfilter.h>
25#include <linux/netfilter/nfnetlink.h>
26#include <linux/netfilter/nf_tables.h>
27
28#include <libmnl/libmnl.h>
29#include <libnftnl/set.h>
30
31static struct nftnl_set *setup_set(uint8_t family, const char *table,
32 const char *name)
33{
34 struct nftnl_set *s = NULL;
35
36 s = nftnl_set_alloc();
37 if (s == NULL) {
38 perror("OOM");
39 exit(EXIT_FAILURE);
40 }
41
42 nftnl_set_set_str(s, NFTNL_SET_TABLE, table);
43 nftnl_set_set_str(s, NFTNL_SET_NAME, name);
44 nftnl_set_set_u32(s, NFTNL_SET_FAMILY, family);
45 nftnl_set_set_u32(s, NFTNL_SET_KEY_LEN, sizeof(uint16_t));
46 /* inet service type, see nftables/include/datatypes.h */
47 nftnl_set_set_u32(s, NFTNL_SET_KEY_TYPE, 13);
48 nftnl_set_set_u32(s, NFTNL_SET_ID, 1);
49
50 return s;
51}
52
53int 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, inet, 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}