libnftnl  1.0.6
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.borrero.glez@gmail.com>
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 {xml|json} <file>\n", argv[0]);
89  exit(EXIT_FAILURE);
90  }
91 
92  if (strcmp(argv[1], "xml") == 0) {
93  format = NFTNL_PARSE_XML;
94  outformat = NFTNL_OUTPUT_XML;
95  } else if (strcmp(argv[1], "json") == 0) {
96  format = NFTNL_PARSE_JSON;
97  outformat = NFTNL_OUTPUT_JSON;
98  } else {
99  printf("Unknown format: xml, json\n");
100  exit(EXIT_FAILURE);
101  }
102 
103  s = set_parse_file(argv[2], format);
104  if (s == NULL)
105  exit(EXIT_FAILURE);
106 
107  nftnl_set_fprintf(stdout, s, outformat, 0);
108  fprintf(stdout, "\n");
109 
110  seq = time(NULL);
111  batching = nftnl_batch_is_supported();
112  if (batching < 0) {
113  perror("cannot talk to nfnetlink");
114  exit(EXIT_FAILURE);
115  }
116 
117  batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
118 
119  if (batching) {
120  nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
121  mnl_nlmsg_batch_next(batch);
122  }
123 
124  family = nftnl_set_get_u32(s, NFTNL_SET_FAMILY);
125 
126  set_seq = seq;
127  nlh = nftnl_set_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
128  NFT_MSG_NEWSET, family,
129  NLM_F_CREATE|NLM_F_ACK, seq++);
130  nftnl_set_nlmsg_build_payload(nlh, s);
131  nftnl_set_free(s);
132  mnl_nlmsg_batch_next(batch);
133 
134  if (batching) {
135  nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
136  mnl_nlmsg_batch_next(batch);
137  }
138 
139  nl = mnl_socket_open(NETLINK_NETFILTER);
140  if (nl == NULL) {
141  perror("mnl_socket_open");
142  exit(EXIT_FAILURE);
143  }
144 
145  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
146  perror("mnl_socket_bind");
147  exit(EXIT_FAILURE);
148  }
149  portid = mnl_socket_get_portid(nl);
150 
151  if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
152  mnl_nlmsg_batch_size(batch)) < 0) {
153  perror("mnl_socket_send");
154  exit(EXIT_FAILURE);
155  }
156 
157  mnl_nlmsg_batch_stop(batch);
158 
159  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
160  while (ret > 0) {
161  ret = mnl_cb_run(buf, ret, set_seq, portid, NULL, NULL);
162  if (ret <= 0)
163  break;
164  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
165  }
166  if (ret == -1) {
167  perror("error");
168  exit(EXIT_FAILURE);
169  }
170 
171  mnl_socket_close(nl);
172 
173  return EXIT_SUCCESS;
174 }