libnftnl  1.0.5
nft-table-del.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 static struct nftnl_table *table_del_parse(int argc, char *argv[])
24 {
25  struct nftnl_table *t;
26  uint16_t family;
27 
28  if (strcmp(argv[1], "ip") == 0)
29  family = NFPROTO_IPV4;
30  else if (strcmp(argv[1], "ip6") == 0)
31  family = NFPROTO_IPV6;
32  else if (strcmp(argv[1], "bridge") == 0)
33  family = NFPROTO_BRIDGE;
34  else if (strcmp(argv[1], "arp") == 0)
35  family = NFPROTO_ARP;
36  else {
37  fprintf(stderr, "Unknown family: ip, ip6, bridge, arp\n");
38  return NULL;
39  }
40 
41  t = nftnl_table_alloc();
42  if (t == NULL) {
43  perror("OOM");
44  return NULL;
45  }
46 
47  nftnl_table_set_str(t, NFTNL_TABLE_NAME, argv[2]);
48  nftnl_table_set_u32(t, NFTNL_TABLE_FAMILY, family);
49 
50  return t;
51 }
52 
53 int main(int argc, char *argv[])
54 {
55  struct mnl_socket *nl;
56  char buf[MNL_SOCKET_BUFFER_SIZE];
57  struct nlmsghdr *nlh;
58  uint32_t portid, seq, table_seq, family;
59  struct nftnl_table *t;
60  struct mnl_nlmsg_batch *batch;
61  int ret, batching;
62 
63  if (argc != 3) {
64  fprintf(stderr, "%s <family> <name>\n", argv[0]);
65  exit(EXIT_FAILURE);
66  }
67 
68  t = table_del_parse(argc, argv);
69  if (t == NULL)
70  exit(EXIT_FAILURE);
71 
72  batching = nftnl_batch_is_supported();
73  if (batching < 0) {
74  perror("cannot talk to nfnetlink");
75  exit(EXIT_FAILURE);
76  }
77 
78  seq = time(NULL);
79  batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
80 
81  if (batching) {
82  nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
83  mnl_nlmsg_batch_next(batch);
84  }
85 
86  table_seq = seq;
87  family = nftnl_table_get_u32(t, NFTNL_TABLE_FAMILY);
88  nlh = nftnl_table_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
89  NFT_MSG_DELTABLE, family,
90  NLM_F_ACK, seq++);
91  nftnl_table_nlmsg_build_payload(nlh, t);
92  mnl_nlmsg_batch_next(batch);
93  nftnl_table_free(t);
94 
95  if (batching) {
96  nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
97  mnl_nlmsg_batch_next(batch);
98  }
99 
100  nl = mnl_socket_open(NETLINK_NETFILTER);
101  if (nl == NULL) {
102  perror("mnl_socket_open");
103  exit(EXIT_FAILURE);
104  }
105 
106  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
107  perror("mnl_socket_bind");
108  exit(EXIT_FAILURE);
109  }
110  portid = mnl_socket_get_portid(nl);
111 
112  if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
113  mnl_nlmsg_batch_size(batch)) < 0) {
114  perror("mnl_socket_send");
115  exit(EXIT_FAILURE);
116  }
117 
118  mnl_nlmsg_batch_stop(batch);
119 
120  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
121  while (ret > 0) {
122  ret = mnl_cb_run(buf, ret, table_seq, portid, NULL, NULL);
123  if (ret <= 0)
124  break;
125  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
126  }
127  if (ret == -1) {
128  perror("error");
129  exit(EXIT_FAILURE);
130  }
131  mnl_socket_close(nl);
132 
133  return EXIT_SUCCESS;
134 }