libnftnl  1.0.8
nft-set-parse-add.c
1 /*
2  * (C) 2013 by Álvaro Neira Ayuso <alvaroneay@gmail.com>
3  *
4  * Based on nft-set-xml-add from:
5  *
6  * (C) 2013 by Pablo Neira Ayuso <pablo@netfilter.org>
7  * (C) 2013 by Arturo Borrero Gonzalez <arturo@debian.org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  */
14 
15 #include <stdlib.h>
16 #include <time.h>
17 #include <string.h>
18 #include <netinet/in.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <errno.h>
23 
24 #include <linux/netfilter.h>
25 #include <linux/netfilter/nf_tables.h>
26 #include <linux/netfilter/nfnetlink.h>
27 
28 #include <libmnl/libmnl.h>
29 #include <libnftnl/set.h>
30 
31 static struct nftnl_set *set_parse_file(const char *file, uint16_t format)
32 {
33  int fd;
34  struct nftnl_set *s;
35  struct nftnl_parse_err *err;
36  char data[4096];
37 
38  s = nftnl_set_alloc();
39  if (s == NULL) {
40  perror("OOM");
41  return NULL;
42  }
43 
44  fd = open(file, O_RDONLY);
45  if (fd < 0) {
46  perror("open");
47  return NULL;
48  }
49 
50  if (read(fd, data, sizeof(data)) < 0) {
51  perror("read");
52  close(fd);
53  return NULL;
54  }
55  close(fd);
56 
57  err = nftnl_parse_err_alloc();
58  if (err == NULL) {
59  perror("error");
60  return NULL;
61  }
62 
63  if (nftnl_set_parse(s, format, data, err) < 0) {
64  nftnl_parse_perror("Unable to parse file", err);
65  nftnl_parse_err_free(err);
66  return NULL;
67  }
68 
69  nftnl_parse_err_free(err);
70 
71  nftnl_set_set_u32(s, NFTNL_SET_ID, 1);
72  return s;
73 
74 }
75 
76 int main(int argc, char *argv[])
77 {
78  struct mnl_socket *nl;
79  char buf[MNL_SOCKET_BUFFER_SIZE];
80  struct nlmsghdr *nlh;
81  uint32_t portid, seq, set_seq;
82  struct nftnl_set *s;
83  int ret, batching;
84  uint16_t family, format, outformat;
85  struct mnl_nlmsg_batch *batch;
86 
87  if (argc < 2) {
88  printf("Usage: %s {json} <file>\n", argv[0]);
89  exit(EXIT_FAILURE);
90  }
91 
92  if (strcmp(argv[1], "json") == 0) {
93  format = NFTNL_PARSE_JSON;
94  outformat = NFTNL_OUTPUT_JSON;
95  } else {
96  printf("Unknown format: only json is supported\n");
97  exit(EXIT_FAILURE);
98  }
99 
100  s = set_parse_file(argv[2], format);
101  if (s == NULL)
102  exit(EXIT_FAILURE);
103 
104  nftnl_set_fprintf(stdout, s, outformat, 0);
105  fprintf(stdout, "\n");
106 
107  seq = time(NULL);
108  batching = nftnl_batch_is_supported();
109  if (batching < 0) {
110  perror("cannot talk to nfnetlink");
111  exit(EXIT_FAILURE);
112  }
113 
114  batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
115 
116  if (batching) {
117  nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
118  mnl_nlmsg_batch_next(batch);
119  }
120 
121  family = nftnl_set_get_u32(s, NFTNL_SET_FAMILY);
122 
123  set_seq = seq;
124  nlh = nftnl_set_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
125  NFT_MSG_NEWSET, family,
126  NLM_F_CREATE|NLM_F_ACK, seq++);
127  nftnl_set_nlmsg_build_payload(nlh, s);
128  nftnl_set_free(s);
129  mnl_nlmsg_batch_next(batch);
130 
131  if (batching) {
132  nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
133  mnl_nlmsg_batch_next(batch);
134  }
135 
136  nl = mnl_socket_open(NETLINK_NETFILTER);
137  if (nl == NULL) {
138  perror("mnl_socket_open");
139  exit(EXIT_FAILURE);
140  }
141 
142  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
143  perror("mnl_socket_bind");
144  exit(EXIT_FAILURE);
145  }
146  portid = mnl_socket_get_portid(nl);
147 
148  if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
149  mnl_nlmsg_batch_size(batch)) < 0) {
150  perror("mnl_socket_send");
151  exit(EXIT_FAILURE);
152  }
153 
154  mnl_nlmsg_batch_stop(batch);
155 
156  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
157  while (ret > 0) {
158  ret = mnl_cb_run(buf, ret, set_seq, portid, NULL, NULL);
159  if (ret <= 0)
160  break;
161  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
162  }
163  if (ret == -1) {
164  perror("error");
165  exit(EXIT_FAILURE);
166  }
167 
168  mnl_socket_close(nl);
169 
170  return EXIT_SUCCESS;
171 }