libnftnl  1.0.5
nft-rule-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 <stddef.h> /* for offsetof */
17 #include <netinet/in.h>
18 #include <arpa/inet.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/rule.h>
29 
30 static struct nftnl_rule *rule_parse_file(const char *file, uint16_t format)
31 {
32  int fd;
33  struct nftnl_rule *r;
34  struct nftnl_parse_err *err;
35  char data[4096];
36 
37  fd = open(file, O_RDONLY);
38  if (fd < 0) {
39  perror("open");
40  return NULL;
41  }
42 
43  if (read(fd, data, sizeof(data)) < 0) {
44  perror("read");
45  close(fd);
46  return NULL;
47  }
48  close(fd);
49 
50  r = nftnl_rule_alloc();
51  if (r == NULL) {
52  perror("OOM");
53  exit(EXIT_FAILURE);
54  }
55 
56  err = nftnl_parse_err_alloc();
57  if (err == NULL) {
58  perror("error");
59  exit(EXIT_FAILURE);
60  }
61 
62  if (nftnl_rule_parse(r, format, data, err) < 0) {
63  nftnl_parse_perror("Unable to parse file", err);
64  nftnl_parse_err_free(err);
65  nftnl_rule_free(r);
66  return NULL;
67  }
68 
69  nftnl_rule_unset(r, NFTNL_RULE_HANDLE);
70 
71  nftnl_parse_err_free(err);
72  return r;
73 }
74 
75 int main(int argc, char *argv[])
76 {
77  struct mnl_socket *nl;
78  struct mnl_nlmsg_batch *batch;
79  char buf[MNL_SOCKET_BUFFER_SIZE];
80  struct nlmsghdr *nlh;
81  uint32_t portid, seq, rule_seq;
82  struct nftnl_rule *r;
83  int ret, batching;
84  uint16_t family, format, outformat;
85 
86  if (argc < 3) {
87  printf("Usage: %s {xml|json} <file>\n", argv[0]);
88  exit(EXIT_FAILURE);
89  }
90 
91  if (strcmp(argv[1], "xml") == 0) {
92  format = NFTNL_PARSE_XML;
93  outformat = NFTNL_OUTPUT_XML;
94  } else if (strcmp(argv[1], "json") == 0) {
95  format = NFTNL_PARSE_JSON;
96  outformat = NFTNL_OUTPUT_JSON;
97  } else {
98  printf("Unknown format: xml, json\n");
99  exit(EXIT_FAILURE);
100  }
101 
102  r = rule_parse_file(argv[2], format);
103  if (r == NULL)
104  exit(EXIT_FAILURE);
105 
106  nftnl_rule_fprintf(stdout, r, outformat, 0);
107  fprintf(stdout, "\n");
108 
109  batching = nftnl_batch_is_supported();
110  if (batching < 0) {
111  perror("Cannot talk to nfnetlink");
112  exit(EXIT_FAILURE);
113  }
114 
115  seq = time(NULL);
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  rule_seq = seq;
124  family = nftnl_rule_get_u32(r, NFTNL_RULE_FAMILY);
125  nlh = nftnl_rule_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
126  NFT_MSG_NEWRULE, family,
127  NLM_F_CREATE|NLM_F_APPEND|NLM_F_ACK,
128  seq++);
129  nftnl_rule_nlmsg_build_payload(nlh, r);
130  nftnl_rule_free(r);
131  mnl_nlmsg_batch_next(batch);
132 
133  if (batching) {
134  nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
135  mnl_nlmsg_batch_next(batch);
136  }
137 
138  nl = mnl_socket_open(NETLINK_NETFILTER);
139  if (nl == NULL) {
140  perror("mnl_socket_open");
141  exit(EXIT_FAILURE);
142  }
143 
144  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
145  perror("mnl_socket_bind");
146  exit(EXIT_FAILURE);
147  }
148  portid = mnl_socket_get_portid(nl);
149 
150  if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
151  mnl_nlmsg_batch_size(batch)) < 0) {
152  perror("mnl_socket_send");
153  exit(EXIT_FAILURE);
154  }
155 
156  mnl_nlmsg_batch_stop(batch);
157 
158  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
159  while (ret > 0) {
160  ret = mnl_cb_run(buf, ret, rule_seq, portid, NULL, NULL);
161  if (ret <= 0)
162  break;
163  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
164  }
165  if (ret == -1) {
166  perror("error");
167  exit(EXIT_FAILURE);
168  }
169  mnl_socket_close(nl);
170 
171  return EXIT_SUCCESS;
172 }