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