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