libnftnl  1.0.8
nft-table-upd.c
1 /*
2  * (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.org>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This software has been sponsored by Sophos Astaro <http://www.sophos.com>
10  */
11 
12 #include <stdlib.h>
13 #include <time.h>
14 #include <string.h>
15 #include <netinet/in.h>
16 
17 #include <linux/netfilter.h>
18 #include <linux/netfilter/nf_tables.h>
19 
20 #include <libmnl/libmnl.h>
21 #include <libnftnl/table.h>
22 
23 int main(int argc, char *argv[])
24 {
25  struct mnl_socket *nl;
26  char buf[MNL_SOCKET_BUFFER_SIZE];
27  struct nlmsghdr *nlh;
28  uint32_t portid, seq, table_seq, family, flags;
29  struct nftnl_table *t = NULL;
30  struct mnl_nlmsg_batch *batch;
31  int ret, batching;
32 
33  if (argc != 4) {
34  fprintf(stderr, "%s <family> <name> <state>\n", argv[0]);
35  exit(EXIT_FAILURE);
36  }
37 
38  t = nftnl_table_alloc();
39  if (t == NULL) {
40  perror("OOM");
41  exit(EXIT_FAILURE);
42  }
43 
44  batching = nftnl_batch_is_supported();
45  if (batching < 0) {
46  perror("cannot talk to nfnetlink");
47  exit(EXIT_FAILURE);
48  }
49 
50  seq = time(NULL);
51  batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
52 
53  if (batching) {
54  nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
55  mnl_nlmsg_batch_next(batch);
56  }
57 
58  if (strcmp(argv[1], "ip") == 0)
59  family = NFPROTO_IPV4;
60  else if (strcmp(argv[1], "ip6") == 0)
61  family = NFPROTO_IPV6;
62  else if (strcmp(argv[1], "bridge") == 0)
63  family = NFPROTO_BRIDGE;
64  else if (strcmp(argv[1], "arp") == 0)
65  family = NFPROTO_ARP;
66  else if (strcmp(argv[1], "netdev") == 0)
67  family = NFPROTO_NETDEV;
68  else {
69  fprintf(stderr,
70  "Unknown family: ip, ip6, bridge, arp, netdev\n");
71  exit(EXIT_FAILURE);
72  }
73 
74  if (strcmp(argv[3], "active") == 0)
75  flags = 0;
76  else if (strcmp(argv[3], "dormant") == 0)
77  flags = NFT_TABLE_F_DORMANT;
78  else {
79  fprintf(stderr, "Unknown state: active, dormant\n");
80  exit(EXIT_FAILURE);
81  }
82 
83  nftnl_table_set(t, NFTNL_TABLE_NAME, argv[2]);
84  nftnl_table_set_u32(t, NFTNL_TABLE_FLAGS, flags);
85 
86  table_seq = seq;
87  nlh = nftnl_table_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
88  NFT_MSG_NEWTABLE, family,
89  NLM_F_ACK, seq++);
90  nftnl_table_nlmsg_build_payload(nlh, t);
91  nftnl_table_free(t);
92  mnl_nlmsg_batch_next(batch);
93 
94  if (batching) {
95  nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
96  mnl_nlmsg_batch_next(batch);
97  }
98 
99  nl = mnl_socket_open(NETLINK_NETFILTER);
100  if (nl == NULL) {
101  perror("mnl_socket_open");
102  exit(EXIT_FAILURE);
103  }
104 
105  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
106  perror("mnl_socket_bind");
107  exit(EXIT_FAILURE);
108  }
109  portid = mnl_socket_get_portid(nl);
110 
111  if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
112  mnl_nlmsg_batch_size(batch)) < 0) {
113  perror("mnl_socket_send");
114  exit(EXIT_FAILURE);
115  }
116 
117  mnl_nlmsg_batch_stop(batch);
118 
119  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
120  while (ret > 0) {
121  ret = mnl_cb_run(buf, ret, table_seq, portid, NULL, NULL);
122  if (ret <= 0)
123  break;
124  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
125  }
126  if (ret == -1) {
127  perror("error");
128  exit(EXIT_FAILURE);
129  }
130  mnl_socket_close(nl);
131 
132  return EXIT_SUCCESS;
133 }