libnftnl  1.2.9
nft-rule-del.c
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.org>
4  *
5  * This software has been sponsored by Sophos Astaro <http://www.sophos.com>
6  */
7 
8 #include <stdlib.h>
9 #include <time.h>
10 #include <string.h>
11 #include <stddef.h> /* for offsetof */
12 #include <netinet/in.h>
13 
14 #include <linux/netfilter.h>
15 #include <linux/netfilter/nf_tables.h>
16 #include <linux/netfilter/nfnetlink.h>
17 
18 #include <libmnl/libmnl.h>
19 #include <libnftnl/rule.h>
20 
21 int main(int argc, char *argv[])
22 {
23  struct mnl_socket *nl;
24  char buf[MNL_SOCKET_BUFFER_SIZE];
25  struct nlmsghdr *nlh;
26  struct mnl_nlmsg_batch *batch;
27  uint32_t portid, seq;
28  struct nftnl_rule *r = NULL;
29  int ret, family;
30 
31  if (argc < 4 || argc > 5) {
32  fprintf(stderr, "Usage: %s <family> <table> <chain> [<handle>]\n",
33  argv[0]);
34  exit(EXIT_FAILURE);
35  }
36 
37  r = nftnl_rule_alloc();
38  if (r == NULL) {
39  perror("OOM");
40  exit(EXIT_FAILURE);
41  }
42 
43  if (strcmp(argv[1], "ip") == 0)
44  family = NFPROTO_IPV4;
45  else if (strcmp(argv[1], "ip6") == 0)
46  family = NFPROTO_IPV6;
47  else if (strcmp(argv[1], "inet") == 0)
48  family = NFPROTO_INET;
49  else if (strcmp(argv[1], "bridge") == 0)
50  family = NFPROTO_BRIDGE;
51  else if (strcmp(argv[1], "arp") == 0)
52  family = NFPROTO_ARP;
53  else {
54  fprintf(stderr, "Unknown family: ip, ip6, inet, bridge, arp\n");
55  exit(EXIT_FAILURE);
56  }
57 
58  seq = time(NULL);
59  nftnl_rule_set_str(r, NFTNL_RULE_TABLE, argv[2]);
60  nftnl_rule_set_str(r, NFTNL_RULE_CHAIN, argv[3]);
61 
62  /* If no handle is specified, delete all rules in the chain */
63  if (argc == 5)
64  nftnl_rule_set_u64(r, NFTNL_RULE_HANDLE, atoi(argv[4]));
65 
66  batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
67 
68  nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
69  mnl_nlmsg_batch_next(batch);
70 
71  nlh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
72  NFT_MSG_DELRULE, family, NLM_F_ACK, seq++);
73  nftnl_rule_nlmsg_build_payload(nlh, r);
74  nftnl_rule_free(r);
75  mnl_nlmsg_batch_next(batch);
76 
77  nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
78  mnl_nlmsg_batch_next(batch);
79 
80  nl = mnl_socket_open(NETLINK_NETFILTER);
81  if (nl == NULL) {
82  perror("mnl_socket_open");
83  exit(EXIT_FAILURE);
84  }
85 
86  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
87  perror("mnl_socket_bind");
88  exit(EXIT_FAILURE);
89  }
90  portid = mnl_socket_get_portid(nl);
91 
92  if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
93  mnl_nlmsg_batch_size(batch)) < 0) {
94  perror("mnl_socket_send");
95  exit(EXIT_FAILURE);
96  }
97 
98  mnl_nlmsg_batch_stop(batch);
99 
100  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
101  while (ret > 0) {
102  ret = mnl_cb_run(buf, ret, 0, portid, NULL, NULL);
103  if (ret <= 0)
104  break;
105  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
106  }
107  if (ret == -1) {
108  perror("error");
109  exit(EXIT_FAILURE);
110  }
111  mnl_socket_close(nl);
112 
113  return EXIT_SUCCESS;
114 }