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