libnftnl  1.0.8
nft-chain-parse-add.c
1 /*
2  * (C) 2013 by Pablo Neira Ayuso <pablo@netfilter.org>
3  * (C) 2014 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.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 {json} <file>\n", argv[0]);
85  exit(EXIT_FAILURE);
86  }
87 
88  if (strcmp(argv[1], "json") == 0) {
89  format = NFTNL_PARSE_JSON;
90  outformat = NFTNL_OUTPUT_JSON;
91  } else {
92  printf("Unknown format: only json is supported\n");
93  exit(EXIT_FAILURE);
94  }
95 
96  c = chain_parse_file(argv[2], format);
97  if (c == NULL)
98  exit(EXIT_FAILURE);
99 
100  nftnl_chain_fprintf(stdout, c, outformat, 0);
101  fprintf(stdout, "\n");
102 
103  nftnl_chain_unset(c, NFTNL_CHAIN_HANDLE);
104  family = nftnl_chain_get_u32(c, NFTNL_CHAIN_FAMILY);
105 
106  seq = time(NULL);
107  batching = nftnl_batch_is_supported();
108  if (batching < 0) {
109  perror("cannot talk to nfnetlink");
110  exit(EXIT_FAILURE);
111  }
112 
113  batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
114 
115  if (batching) {
116  nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
117  mnl_nlmsg_batch_next(batch);
118  }
119 
120  chain_seq = seq;
121  nlh = nftnl_chain_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
122  NFT_MSG_NEWCHAIN, family,
123  NLM_F_ACK, seq++);
124  nftnl_chain_nlmsg_build_payload(nlh, c);
125  nftnl_chain_free(c);
126  mnl_nlmsg_batch_next(batch);
127 
128  if (batching) {
129  nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
130  mnl_nlmsg_batch_next(batch);
131  }
132 
133  nl = mnl_socket_open(NETLINK_NETFILTER);
134  if (nl == NULL) {
135  perror("mnl_socket_open");
136  exit(EXIT_FAILURE);
137  }
138 
139  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
140  perror("mnl_socket_bind");
141  exit(EXIT_FAILURE);
142  }
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, chain_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 
167  mnl_socket_close(nl);
168  return EXIT_SUCCESS;
169 }