libnftnl 1.2.6
nft-set-elem-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 <netinet/in.h>
16
17#include <linux/netfilter.h>
18#include <linux/netfilter/nf_tables.h>
19
20#include <libmnl/libmnl.h>
21#include <libnftnl/set.h>
22
23int main(int argc, char *argv[])
24{
25 struct mnl_socket *nl;
26 char buf[MNL_SOCKET_BUFFER_SIZE];
27 struct mnl_nlmsg_batch *batch;
28 struct nlmsghdr *nlh;
29 uint32_t portid, seq, family;
30 struct nftnl_set *s;
31 struct nftnl_set_elem *e;
32 uint16_t data;
33 int ret;
34
35 if (argc != 4) {
36 fprintf(stderr, "%s <family> <table> <set>\n", argv[0]);
37 exit(EXIT_FAILURE);
38 }
39
40 s = nftnl_set_alloc();
41 if (s == NULL) {
42 perror("OOM");
43 exit(EXIT_FAILURE);
44 }
45
46 seq = time(NULL);
47 if (strcmp(argv[1], "ip") == 0)
48 family = NFPROTO_IPV4;
49 else if (strcmp(argv[1], "ip6") == 0)
50 family = NFPROTO_IPV6;
51 else if (strcmp(argv[1], "inet") == 0)
52 family = NFPROTO_INET;
53 else if (strcmp(argv[1], "bridge") == 0)
54 family = NFPROTO_BRIDGE;
55 else if (strcmp(argv[1], "arp") == 0)
56 family = NFPROTO_ARP;
57 else {
58 fprintf(stderr, "Unknown family: ip, ip6, inet, bridge, arp\n");
59 exit(EXIT_FAILURE);
60 }
61
62 nftnl_set_set_str(s, NFTNL_SET_TABLE, argv[2]);
63 nftnl_set_set_str(s, NFTNL_SET_NAME, argv[3]);
64
65 /* Add to dummy elements to set */
66 e = nftnl_set_elem_alloc();
67 if (e == NULL) {
68 perror("OOM");
69 exit(EXIT_FAILURE);
70 }
71
72 data = 0x1;
73 nftnl_set_elem_set(e, NFTNL_SET_ELEM_KEY, &data, sizeof(data));
74 nftnl_set_elem_add(s, e);
75
76 e = nftnl_set_elem_alloc();
77 if (e == NULL) {
78 perror("OOM");
79 exit(EXIT_FAILURE);
80 }
81 data = 0x2;
82 nftnl_set_elem_set(e, NFTNL_SET_ELEM_KEY, &data, sizeof(data));
83 nftnl_set_elem_add(s, e);
84
85 batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
86
87 nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
88 mnl_nlmsg_batch_next(batch);
89
90 nlh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
91 NFT_MSG_NEWSETELEM, family,
92 NLM_F_CREATE | NLM_F_EXCL | NLM_F_ACK,
93 seq++);
94 nftnl_set_elems_nlmsg_build_payload(nlh, s);
95 nftnl_set_free(s);
96 mnl_nlmsg_batch_next(batch);
97
98 nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
99 mnl_nlmsg_batch_next(batch);
100
101 nl = mnl_socket_open(NETLINK_NETFILTER);
102 if (nl == NULL) {
103 perror("mnl_socket_open");
104 exit(EXIT_FAILURE);
105 }
106
107 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
108 perror("mnl_socket_bind");
109 exit(EXIT_FAILURE);
110 }
111 portid = mnl_socket_get_portid(nl);
112
113 if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
114 mnl_nlmsg_batch_size(batch)) < 0) {
115 perror("mnl_socket_send");
116 exit(EXIT_FAILURE);
117 }
118
119 mnl_nlmsg_batch_stop(batch);
120
121 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
122 while (ret > 0) {
123 ret = mnl_cb_run(buf, ret, 0, portid, NULL, NULL);
124 if (ret <= 0)
125 break;
126 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
127 }
128 if (ret == -1) {
129 perror("error");
130 exit(EXIT_FAILURE);
131 }
132 mnl_socket_close(nl);
133
134 return EXIT_SUCCESS;
135}