libnftnl 1.2.9
nft-chain-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 <netinet/in.h>
12
13#include <linux/netfilter.h>
14#include <linux/netfilter/nf_tables.h>
15
16#include <libmnl/libmnl.h>
17#include <libnftnl/chain.h>
18
19static struct nftnl_chain *chain_del_parse(int argc, char *argv[])
20{
21 struct nftnl_chain *t;
22
23 t = nftnl_chain_alloc();
24 if (t == NULL) {
25 perror("OOM");
26 return NULL;
27 }
28
29 nftnl_chain_set_str(t, NFTNL_CHAIN_TABLE, argv[2]);
30 nftnl_chain_set_str(t, NFTNL_CHAIN_NAME, argv[3]);
31
32 return t;
33}
34
35int main(int argc, char *argv[])
36{
37 struct mnl_socket *nl;
38 struct mnl_nlmsg_batch *batch;
39 char buf[MNL_SOCKET_BUFFER_SIZE];
40 struct nlmsghdr *nlh;
41 uint32_t portid, seq, chain_seq;
42 struct nftnl_chain *t;
43 int ret, family;
44
45 if (argc != 4) {
46 fprintf(stderr, "Usage: %s <family> <table> <chain>\n",
47 argv[0]);
48 exit(EXIT_FAILURE);
49 }
50
51 if (strcmp(argv[1], "ip") == 0)
52 family = NFPROTO_IPV4;
53 else if (strcmp(argv[1], "ip6") == 0)
54 family = NFPROTO_IPV6;
55 else if (strcmp(argv[1], "inet") == 0)
56 family = NFPROTO_INET;
57 else if (strcmp(argv[1], "bridge") == 0)
58 family = NFPROTO_BRIDGE;
59 else if (strcmp(argv[1], "arp") == 0)
60 family = NFPROTO_ARP;
61 else {
62 fprintf(stderr, "Unknown family: ip, ip6, inet, bridge, arp\n");
63 exit(EXIT_FAILURE);
64 }
65
66 t = chain_del_parse(argc, argv);
67 if (t == NULL)
68 exit(EXIT_FAILURE);
69
70 seq = time(NULL);
71 batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
72
73 nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
74 mnl_nlmsg_batch_next(batch);
75
76 chain_seq = seq;
77 nlh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
78 NFT_MSG_DELCHAIN, family, NLM_F_ACK, seq++);
79 nftnl_chain_nlmsg_build_payload(nlh, t);
80 nftnl_chain_free(t);
81 mnl_nlmsg_batch_next(batch);
82
83 nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
84 mnl_nlmsg_batch_next(batch);
85
86 nl = mnl_socket_open(NETLINK_NETFILTER);
87 if (nl == NULL) {
88 perror("mnl_socket_open");
89 exit(EXIT_FAILURE);
90 }
91
92 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
93 perror("mnl_socket_bind");
94 exit(EXIT_FAILURE);
95 }
96 portid = mnl_socket_get_portid(nl);
97
98 if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
99 mnl_nlmsg_batch_size(batch)) < 0) {
100 perror("mnl_socket_send");
101 exit(EXIT_FAILURE);
102 }
103
104 mnl_nlmsg_batch_stop(batch);
105
106 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
107 while (ret > 0) {
108 ret = mnl_cb_run(buf, ret, chain_seq, portid, NULL, NULL);
109 if (ret <= 0)
110 break;
111 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
112 }
113 if (ret == -1) {
114 perror("error");
115 exit(EXIT_FAILURE);
116 }
117 mnl_socket_close(nl);
118
119 return EXIT_SUCCESS;
120}