libnftnl  1.0.8
nft-rule-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 <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 {json} <file>\n", argv[0]);
88  exit(EXIT_FAILURE);
89  }
90 
91  if (strcmp(argv[1], "json") == 0) {
92  format = NFTNL_PARSE_JSON;
93  outformat = NFTNL_OUTPUT_JSON;
94  } else {
95  printf("Unknown format: json\n");
96  exit(EXIT_FAILURE);
97  }
98 
99  r = rule_parse_file(argv[2], format);
100  if (r == NULL)
101  exit(EXIT_FAILURE);
102 
103  nftnl_rule_fprintf(stdout, r, outformat, 0);
104  fprintf(stdout, "\n");
105 
106  batching = nftnl_batch_is_supported();
107  if (batching < 0) {
108  perror("Cannot talk to nfnetlink");
109  exit(EXIT_FAILURE);
110  }
111 
112  seq = time(NULL);
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  rule_seq = seq;
121  family = nftnl_rule_get_u32(r, NFTNL_RULE_FAMILY);
122  nlh = nftnl_rule_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
123  NFT_MSG_NEWRULE, family,
124  NLM_F_CREATE|NLM_F_APPEND|NLM_F_ACK,
125  seq++);
126  nftnl_rule_nlmsg_build_payload(nlh, r);
127  nftnl_rule_free(r);
128  mnl_nlmsg_batch_next(batch);
129 
130  if (batching) {
131  nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
132  mnl_nlmsg_batch_next(batch);
133  }
134 
135  nl = mnl_socket_open(NETLINK_NETFILTER);
136  if (nl == NULL) {
137  perror("mnl_socket_open");
138  exit(EXIT_FAILURE);
139  }
140 
141  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
142  perror("mnl_socket_bind");
143  exit(EXIT_FAILURE);
144  }
145  portid = mnl_socket_get_portid(nl);
146 
147  if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
148  mnl_nlmsg_batch_size(batch)) < 0) {
149  perror("mnl_socket_send");
150  exit(EXIT_FAILURE);
151  }
152 
153  mnl_nlmsg_batch_stop(batch);
154 
155  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
156  while (ret > 0) {
157  ret = mnl_cb_run(buf, ret, rule_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  mnl_socket_close(nl);
167 
168  return EXIT_SUCCESS;
169 }