libnftnl 1.2.9
nft-obj-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/object.h>
18
19static struct nftnl_obj *obj_del_parse(int argc, char *argv[])
20{
21 struct nftnl_obj *t;
22 uint16_t family;
23
24 if (strcmp(argv[1], "ip") == 0)
25 family = NFPROTO_IPV4;
26 else if (strcmp(argv[1], "ip6") == 0)
27 family = NFPROTO_IPV6;
28 else if (strcmp(argv[1], "inet") == 0)
29 family = NFPROTO_INET;
30 else if (strcmp(argv[1], "bridge") == 0)
31 family = NFPROTO_BRIDGE;
32 else if (strcmp(argv[1], "arp") == 0)
33 family = NFPROTO_ARP;
34 else {
35 fprintf(stderr, "Unknown family: ip, ip6, inet, bridge, arp\n");
36 return NULL;
37 }
38
39 t = nftnl_obj_alloc();
40 if (t == NULL) {
41 perror("OOM");
42 return NULL;
43 }
44
45 nftnl_obj_set_str(t, NFTNL_OBJ_TABLE, argv[2]);
46 nftnl_obj_set_str(t, NFTNL_OBJ_NAME, argv[3]);
47 nftnl_obj_set_u32(t, NFTNL_OBJ_TYPE, NFT_OBJECT_COUNTER);
48 nftnl_obj_set_u32(t, NFTNL_OBJ_FAMILY, family);
49
50 return t;
51}
52
53int 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, obj_seq, family;
59 struct nftnl_obj *t;
60 struct mnl_nlmsg_batch *batch;
61 int ret;
62
63 if (argc != 4) {
64 fprintf(stderr, "%s <family> <table> <name>\n", argv[0]);
65 exit(EXIT_FAILURE);
66 }
67
68 t = obj_del_parse(argc, argv);
69 if (t == NULL)
70 exit(EXIT_FAILURE);
71
72 seq = time(NULL);
73 batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
74
75 nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
76 mnl_nlmsg_batch_next(batch);
77
78 obj_seq = seq;
79 family = nftnl_obj_get_u32(t, NFTNL_OBJ_FAMILY);
80 nlh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
81 NFT_MSG_DELOBJ, family, NLM_F_ACK,
82 seq++);
83 nftnl_obj_nlmsg_build_payload(nlh, t);
84 mnl_nlmsg_batch_next(batch);
85 nftnl_obj_free(t);
86
87 nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
88 mnl_nlmsg_batch_next(batch);
89
90 nl = mnl_socket_open(NETLINK_NETFILTER);
91 if (nl == NULL) {
92 perror("mnl_socket_open");
93 exit(EXIT_FAILURE);
94 }
95
96 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
97 perror("mnl_socket_bind");
98 exit(EXIT_FAILURE);
99 }
100 portid = mnl_socket_get_portid(nl);
101
102 if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
103 mnl_nlmsg_batch_size(batch)) < 0) {
104 perror("mnl_socket_send");
105 exit(EXIT_FAILURE);
106 }
107
108 mnl_nlmsg_batch_stop(batch);
109
110 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
111 while (ret > 0) {
112 ret = mnl_cb_run(buf, ret, obj_seq, portid, NULL, NULL);
113 if (ret <= 0)
114 break;
115 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
116 }
117 if (ret == -1) {
118 perror("error");
119 exit(EXIT_FAILURE);
120 }
121 mnl_socket_close(nl);
122
123 return EXIT_SUCCESS;
124}