libnftnl  1.0.8
nft-table-parse-add.c
1 /*
2  * (C) 2013 by Pablo Neira Ayuso <pablo@netfilter.org>
3  * (C) 2013 by Arturo Borrero Gonzalez <arturo@debian.org>
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 {json} <file>\n", argv[0]);
84  exit(EXIT_FAILURE);
85  }
86 
87  if (strcmp(argv[1], "json") == 0) {
88  format = NFTNL_PARSE_JSON;
89  outformat = NFTNL_OUTPUT_JSON;
90  } else {
91  printf("Unknown format: only json is supported\n");
92  exit(EXIT_FAILURE);
93  }
94 
95  t = table_parse_file(argv[2], format);
96  if (t == NULL)
97  exit(EXIT_FAILURE);
98 
99  nftnl_table_fprintf(stdout, t, outformat, 0);
100  fprintf(stdout, "\n");
101 
102  seq = time(NULL);
103  batching = nftnl_batch_is_supported();
104  if (batching < 0) {
105  perror("cannot talk to nfnetlink");
106  exit(EXIT_FAILURE);
107  }
108 
109  batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
110 
111  if (batching) {
112  nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
113  mnl_nlmsg_batch_next(batch);
114  }
115 
116  family = nftnl_table_get_u32(t, NFTNL_TABLE_FAMILY);
117 
118  table_seq = seq;
119  nlh = nftnl_table_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
120  NFT_MSG_NEWTABLE, family,
121  NLM_F_CREATE|NLM_F_ACK, seq++);
122  nftnl_table_nlmsg_build_payload(nlh, t);
123  nftnl_table_free(t);
124  mnl_nlmsg_batch_next(batch);
125 
126  if (batching) {
127  nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
128  mnl_nlmsg_batch_next(batch);
129  }
130 
131  nl = mnl_socket_open(NETLINK_NETFILTER);
132  if (nl == NULL) {
133  perror("mnl_socket_open");
134  exit(EXIT_FAILURE);
135  }
136 
137  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
138  perror("mnl_socket_bind");
139  exit(EXIT_FAILURE);
140  }
141  portid = mnl_socket_get_portid(nl);
142 
143  if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
144  mnl_nlmsg_batch_size(batch)) < 0) {
145  perror("mnl_socket_send");
146  exit(EXIT_FAILURE);
147  }
148 
149  mnl_nlmsg_batch_stop(batch);
150 
151  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
152  while (ret > 0) {
153  ret = mnl_cb_run(buf, ret, table_seq, portid, NULL, NULL);
154  if (ret <= 0)
155  break;
156  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
157  }
158  if (ret == -1) {
159  perror("error");
160  exit(EXIT_FAILURE);
161  }
162 
163  mnl_socket_close(nl);
164 
165  return EXIT_SUCCESS;
166 }