libnftnl 1.2.9
nft-ct-expectation-del.c
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * (C) 2019 by Stéphane Veyret <sveyret@gmail.com>
4 */
5
6#include <stdlib.h>
7#include <time.h>
8#include <string.h>
9#include <netinet/in.h>
10
11#include <linux/netfilter.h>
12#include <linux/netfilter/nf_tables.h>
13
14#include <libmnl/libmnl.h>
15#include <libnftnl/object.h>
16
17static uint16_t parse_family(char *str, const char *option)
18{
19 if (strcmp(str, "ip") == 0)
20 return NFPROTO_IPV4;
21 else if (strcmp(str, "ip6") == 0)
22 return NFPROTO_IPV6;
23 else if (strcmp(str, "inet") == 0)
24 return NFPROTO_INET;
25 else if (strcmp(str, "arp") == 0)
26 return NFPROTO_INET;
27 fprintf(stderr, "Unknown %s: ip, ip6, inet, arp\n", option);
28 exit(EXIT_FAILURE);
29}
30
31static struct nftnl_obj *obj_parse(int argc, char *argv[])
32{
33 struct nftnl_obj *t;
34 uint16_t family;
35
36 t = nftnl_obj_alloc();
37 if (t == NULL) {
38 perror("OOM");
39 return NULL;
40 }
41
42 family = parse_family(argv[1], "family");
43 nftnl_obj_set_u32(t, NFTNL_OBJ_FAMILY, family);
44 nftnl_obj_set_u32(t, NFTNL_OBJ_TYPE, NFT_OBJECT_CT_EXPECT);
45 nftnl_obj_set_str(t, NFTNL_OBJ_TABLE, argv[2]);
46 nftnl_obj_set_str(t, NFTNL_OBJ_NAME, argv[3]);
47
48 return t;
49}
50
51int main(int argc, char *argv[])
52{
53 struct mnl_socket *nl;
54 char buf[MNL_SOCKET_BUFFER_SIZE];
55 struct nlmsghdr *nlh;
56 uint32_t portid, seq, obj_seq, family;
57 struct nftnl_obj *t;
58 struct mnl_nlmsg_batch *batch;
59 int ret;
60
61 if (argc != 4) {
62 fprintf(stderr, "%s <family> <table> <name>\n", argv[0]);
63 exit(EXIT_FAILURE);
64 }
65
66 t = obj_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 obj_seq = seq;
77 family = nftnl_obj_get_u32(t, NFTNL_OBJ_FAMILY);
78 nlh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
79 NFT_MSG_DELOBJ, family, NLM_F_ACK,
80 seq++);
81 nftnl_obj_nlmsg_build_payload(nlh, t);
82 mnl_nlmsg_batch_next(batch);
83 nftnl_obj_free(t);
84
85 nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
86 mnl_nlmsg_batch_next(batch);
87
88 nl = mnl_socket_open(NETLINK_NETFILTER);
89 if (nl == NULL) {
90 perror("mnl_socket_open");
91 exit(EXIT_FAILURE);
92 }
93
94 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
95 perror("mnl_socket_bind");
96 exit(EXIT_FAILURE);
97 }
98 portid = mnl_socket_get_portid(nl);
99
100 if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
101 mnl_nlmsg_batch_size(batch)) < 0) {
102 perror("mnl_socket_send");
103 exit(EXIT_FAILURE);
104 }
105
106 mnl_nlmsg_batch_stop(batch);
107
108 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
109 while (ret > 0) {
110 ret = mnl_cb_run(buf, ret, obj_seq, portid, NULL, NULL);
111 if (ret <= 0)
112 break;
113 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
114 }
115 if (ret == -1) {
116 perror("error");
117 exit(EXIT_FAILURE);
118 }
119 mnl_socket_close(nl);
120
121 return EXIT_SUCCESS;
122}