libnftnl  1.0.6
nft-chain-parse-add.c
1 /*
2  * (C) 2013 by Pablo Neira Ayuso <pablo@netfilter.org>
3  * (C) 2014 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.h>
23 #include <linux/netfilter/nf_tables.h>
24 
25 #include <libmnl/libmnl.h>
26 #include <libnftnl/chain.h>
27 #include <libnftnl/rule.h>
28 
29 static struct nftnl_chain *chain_parse_file(const char *file, uint16_t format)
30 {
31  int fd;
32  struct nftnl_chain *c;
33  struct nftnl_parse_err *err;
34  char data[4096];
35 
36  c = nftnl_chain_alloc();
37  if (c == NULL) {
38  perror("OOM");
39  return NULL;
40  }
41 
42  fd = open(file, O_RDONLY);
43  if (fd < 0) {
44  perror("open");
45  return NULL;
46  }
47 
48  if (read(fd, data, sizeof(data)) < 0) {
49  perror("read");
50  close(fd);
51  return NULL;
52  }
53 
54  close(fd);
55 
56  err = nftnl_parse_err_alloc();
57  if (err == NULL) {
58  perror("OOM");
59  return NULL;
60  }
61 
62  if (nftnl_chain_parse(c, format, data, err) < 0) {
63  nftnl_parse_perror("Unable to parse file", err);
64  nftnl_parse_err_free(err);
65  return NULL;
66  }
67 
68  nftnl_parse_err_free(err);
69  return c;
70 }
71 
72 int main(int argc, char *argv[])
73 {
74  struct mnl_socket *nl;
75  char buf[MNL_SOCKET_BUFFER_SIZE];
76  struct nlmsghdr *nlh;
77  uint32_t portid, seq, chain_seq;
78  struct nftnl_chain *c;
79  uint16_t family, format, outformat;
80  int ret, batching;
81  struct mnl_nlmsg_batch *batch;
82 
83  if (argc < 3) {
84  printf("Usage: %s {xml|json} <file>\n", argv[0]);
85  exit(EXIT_FAILURE);
86  }
87 
88  if (strcmp(argv[1], "xml") == 0) {
89  format = NFTNL_PARSE_XML;
90  outformat = NFTNL_OUTPUT_XML;
91  } else if (strcmp(argv[1], "json") == 0) {
92  format = NFTNL_PARSE_JSON;
93  outformat = NFTNL_OUTPUT_JSON;
94  } else {
95  printf("Unknown format: xml, json\n");
96  exit(EXIT_FAILURE);
97  }
98 
99  c = chain_parse_file(argv[2], format);
100  if (c == NULL)
101  exit(EXIT_FAILURE);
102 
103  nftnl_chain_fprintf(stdout, c, outformat, 0);
104  fprintf(stdout, "\n");
105 
106  nftnl_chain_unset(c, NFTNL_CHAIN_HANDLE);
107  family = nftnl_chain_get_u32(c, NFTNL_CHAIN_FAMILY);
108 
109  seq = time(NULL);
110  batching = nftnl_batch_is_supported();
111  if (batching < 0) {
112  perror("cannot talk to nfnetlink");
113  exit(EXIT_FAILURE);
114  }
115 
116  batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
117 
118  if (batching) {
119  nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
120  mnl_nlmsg_batch_next(batch);
121  }
122 
123  chain_seq = seq;
124  nlh = nftnl_chain_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
125  NFT_MSG_NEWCHAIN, family,
126  NLM_F_ACK, seq++);
127  nftnl_chain_nlmsg_build_payload(nlh, c);
128  nftnl_chain_free(c);
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 
147  portid = mnl_socket_get_portid(nl);
148 
149  if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
150  mnl_nlmsg_batch_size(batch)) < 0) {
151  perror("mnl_socket_send");
152  exit(EXIT_FAILURE);
153  }
154 
155  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
156  while (ret > 0) {
157  ret = mnl_cb_run(buf, ret, chain_seq, portid, NULL, NULL);
158  if (ret <= 0)
159  break;
160  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
161  }
162  if (ret == -1) {
163  perror("error");
164  exit(EXIT_FAILURE);
165  }
166 
167 
168  mnl_socket_close(nl);
169  return EXIT_SUCCESS;
170 }