libnftnl  1.0.5
nft-chain-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/chain.h>
22 
23 static struct nftnl_chain *chain_del_parse(int argc, char *argv[])
24 {
25  struct nftnl_chain *t;
26 
27  t = nftnl_chain_alloc();
28  if (t == NULL) {
29  perror("OOM");
30  return NULL;
31  }
32 
33  nftnl_chain_set(t, NFTNL_CHAIN_TABLE, argv[2]);
34  nftnl_chain_set(t, NFTNL_CHAIN_NAME, argv[3]);
35 
36  return t;
37 }
38 
39 int main(int argc, char *argv[])
40 {
41  struct mnl_socket *nl;
42  struct mnl_nlmsg_batch *batch;
43  char buf[MNL_SOCKET_BUFFER_SIZE];
44  struct nlmsghdr *nlh;
45  uint32_t portid, seq, chain_seq;
46  struct nftnl_chain *t;
47  int ret, family, batching;
48 
49  if (argc != 4) {
50  fprintf(stderr, "Usage: %s <family> <table> <chain>\n",
51  argv[0]);
52  exit(EXIT_FAILURE);
53  }
54 
55  if (strcmp(argv[1], "ip") == 0)
56  family = NFPROTO_IPV4;
57  else if (strcmp(argv[1], "ip6") == 0)
58  family = NFPROTO_IPV6;
59  else if (strcmp(argv[1], "bridge") == 0)
60  family = NFPROTO_BRIDGE;
61  else if (strcmp(argv[1], "arp") == 0)
62  family = NFPROTO_ARP;
63  else {
64  fprintf(stderr, "Unknown family: ip, ip6, bridge, arp\n");
65  exit(EXIT_FAILURE);
66  }
67 
68  t = chain_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  chain_seq = seq;
87  nlh = nftnl_chain_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
88  NFT_MSG_DELCHAIN, family,
89  NLM_F_ACK, seq++);
90  nftnl_chain_nlmsg_build_payload(nlh, t);
91  nftnl_chain_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  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
118  while (ret > 0) {
119  ret = mnl_cb_run(buf, ret, chain_seq, portid, NULL, NULL);
120  if (ret <= 0)
121  break;
122  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
123  }
124  if (ret == -1) {
125  perror("error");
126  exit(EXIT_FAILURE);
127  }
128  mnl_socket_close(nl);
129 
130  return EXIT_SUCCESS;
131 }