libnftnl  1.0.8
nft-rule-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 <stddef.h> /* for offsetof */
16 #include <netinet/in.h>
17 
18 #include <linux/netfilter.h>
19 #include <linux/netfilter/nf_tables.h>
20 #include <linux/netfilter/nfnetlink.h>
21 
22 #include <libmnl/libmnl.h>
23 #include <libnftnl/rule.h>
24 
25 int main(int argc, char *argv[])
26 {
27  struct mnl_socket *nl;
28  char buf[MNL_SOCKET_BUFFER_SIZE];
29  struct nlmsghdr *nlh;
30  struct mnl_nlmsg_batch *batch;
31  uint32_t portid, seq;
32  struct nftnl_rule *r = NULL;
33  int ret, batching, family;
34 
35  if (argc < 4 || argc > 5) {
36  fprintf(stderr, "Usage: %s <family> <table> <chain> [<handle>]\n",
37  argv[0]);
38  exit(EXIT_FAILURE);
39  }
40 
41  r = nftnl_rule_alloc();
42  if (r == NULL) {
43  perror("OOM");
44  exit(EXIT_FAILURE);
45  }
46 
47  if (strcmp(argv[1], "ip") == 0)
48  family = NFPROTO_IPV4;
49  else if (strcmp(argv[1], "ip6") == 0)
50  family = NFPROTO_IPV6;
51  else if (strcmp(argv[1], "bridge") == 0)
52  family = NFPROTO_BRIDGE;
53  else if (strcmp(argv[1], "arp") == 0)
54  family = NFPROTO_ARP;
55  else {
56  fprintf(stderr, "Unknown family: ip, ip6, bridge, arp\n");
57  exit(EXIT_FAILURE);
58  }
59 
60  seq = time(NULL);
61  nftnl_rule_set(r, NFTNL_RULE_TABLE, argv[2]);
62  nftnl_rule_set(r, NFTNL_RULE_CHAIN, argv[3]);
63 
64  /* If no handle is specified, delete all rules in the chain */
65  if (argc == 5)
66  nftnl_rule_set_u64(r, NFTNL_RULE_HANDLE, atoi(argv[4]));
67 
68  batching = nftnl_batch_is_supported();
69  if (batching < 0) {
70  perror("cannot talk to nfnetlink");
71  exit(EXIT_FAILURE);
72  }
73 
74  batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
75 
76  if (batching) {
77  nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
78  mnl_nlmsg_batch_next(batch);
79  }
80 
81  nlh = nftnl_rule_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
82  NFT_MSG_DELRULE,
83  family,
84  NLM_F_ACK, seq++);
85 
86  nftnl_rule_nlmsg_build_payload(nlh, r);
87  nftnl_rule_free(r);
88  mnl_nlmsg_batch_next(batch);
89 
90  if (batching) {
91  nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
92  mnl_nlmsg_batch_next(batch);
93  }
94 
95  nl = mnl_socket_open(NETLINK_NETFILTER);
96  if (nl == NULL) {
97  perror("mnl_socket_open");
98  exit(EXIT_FAILURE);
99  }
100 
101  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
102  perror("mnl_socket_bind");
103  exit(EXIT_FAILURE);
104  }
105  portid = mnl_socket_get_portid(nl);
106 
107  if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
108  mnl_nlmsg_batch_size(batch)) < 0) {
109  perror("mnl_socket_send");
110  exit(EXIT_FAILURE);
111  }
112 
113  mnl_nlmsg_batch_stop(batch);
114 
115  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
116  while (ret > 0) {
117  ret = mnl_cb_run(buf, ret, 0, portid, NULL, NULL);
118  if (ret <= 0)
119  break;
120  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
121  }
122  if (ret == -1) {
123  perror("error");
124  exit(EXIT_FAILURE);
125  }
126  mnl_socket_close(nl);
127 
128  return EXIT_SUCCESS;
129 }