libnftnl  1.0.2
nft-chain-json-add.c
1 /*
2  * (C) 2013 by Álvaro Neira Ayuso <alvaroneay@gmail.com>
3  *
4  * Based on nft-chain-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 
27 #include <libmnl/libmnl.h>
28 #include <libnftnl/chain.h>
29 #include <libnftnl/rule.h>
30 
31 int main(int argc, char *argv[])
32 {
33  struct mnl_socket *nl;
34  char buf[MNL_SOCKET_BUFFER_SIZE];
35  struct nlmsghdr *nlh;
36  uint32_t portid, seq;
37  struct nft_chain *c = NULL;
38  int ret, fd;
39  uint16_t family;
40  char json[4096];
41  char reprint[4096];
42  struct nft_parse_err *err;
43 
44  if (argc < 2) {
45  printf("Usage: %s <json-file>\n", argv[0]);
46  exit(EXIT_FAILURE);
47  }
48 
49  c = nft_chain_alloc();
50  if (c == NULL) {
51  perror("OOM");
52  exit(EXIT_FAILURE);
53  }
54 
55  fd = open(argv[1], O_RDONLY);
56  if (fd < 0) {
57  perror("open");
58  exit(EXIT_FAILURE);
59  }
60 
61  if (read(fd, json, sizeof(json)) < 0) {
62  perror("read");
63  close(fd);
64  exit(EXIT_FAILURE);
65  }
66 
67  err = nft_parse_err_alloc();
68  if (err == NULL) {
69  perror("error");
70  exit(EXIT_FAILURE);
71  }
72 
73  close(fd);
74 
75  if (nft_chain_parse(c, NFT_PARSE_JSON, json, err) < 0) {
76  nft_parse_perror("Unable to parse JSON file", err);
77  exit(EXIT_FAILURE);
78  }
79 
80  nft_chain_snprintf(reprint, sizeof(reprint), c, NFT_OUTPUT_JSON, 0);
81  printf("Parsed:\n%s\n", reprint);
82 
83  nft_chain_attr_unset(c, NFT_CHAIN_ATTR_HANDLE);
84  family = nft_chain_attr_get_u32(c, NFT_CHAIN_ATTR_FAMILY);
85 
86  seq = time(NULL);
87  nlh = nft_chain_nlmsg_build_hdr(buf, NFT_MSG_NEWCHAIN, family,
88  NLM_F_CREATE|NLM_F_ACK, seq);
89  nft_chain_nlmsg_build_payload(nlh, c);
90 
91  nft_chain_free(c);
92  nft_parse_err_free(err);
93 
94  nl = mnl_socket_open(NETLINK_NETFILTER);
95  if (nl == NULL) {
96  perror("mnl_socket_open");
97  exit(EXIT_FAILURE);
98  }
99 
100  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
101  perror("mnl_socket_bind");
102  exit(EXIT_FAILURE);
103  }
104 
105  portid = mnl_socket_get_portid(nl);
106 
107  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
108  perror("mnl_socket_send");
109  exit(EXIT_FAILURE);
110  }
111 
112  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
113  while (ret > 0) {
114  ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);
115  if (ret <= 0)
116  break;
117  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
118  }
119  if (ret == -1) {
120  perror("error");
121  exit(EXIT_FAILURE);
122  }
123 
124  mnl_socket_close(nl);
125  return EXIT_SUCCESS;
126 }