libnftnl  1.2.9
nft-ct-helper-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 
19 static struct nftnl_obj *ct_helper_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 {
31  fprintf(stderr, "Unknown family: ip, ip6, inet\n");
32  return NULL;
33  }
34 
35  t = nftnl_obj_alloc();
36  if (t == NULL) {
37  perror("OOM");
38  return NULL;
39  }
40 
41  nftnl_obj_set_str(t, NFTNL_OBJ_TABLE, argv[2]);
42  nftnl_obj_set_str(t, NFTNL_OBJ_NAME, argv[3]);
43  nftnl_obj_set_u32(t, NFTNL_OBJ_TYPE, NFT_OBJECT_CT_HELPER);
44  nftnl_obj_set_u32(t, NFTNL_OBJ_FAMILY, family);
45 
46  return t;
47 }
48 
49 int main(int argc, char *argv[])
50 {
51  struct mnl_socket *nl;
52  char buf[MNL_SOCKET_BUFFER_SIZE];
53  struct nlmsghdr *nlh;
54  uint32_t portid, seq, obj_seq, family;
55  struct nftnl_obj *t;
56  struct mnl_nlmsg_batch *batch;
57  int ret;
58 
59  if (argc != 4) {
60  fprintf(stderr, "%s <family> <table> <name>\n", argv[0]);
61  exit(EXIT_FAILURE);
62  }
63 
64  t = ct_helper_del_parse(argc, argv);
65  if (t == NULL)
66  exit(EXIT_FAILURE);
67 
68  seq = time(NULL);
69  batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
70 
71  nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
72  mnl_nlmsg_batch_next(batch);
73 
74  obj_seq = seq;
75  family = nftnl_obj_get_u32(t, NFTNL_OBJ_FAMILY);
76  nlh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
77  NFT_MSG_DELOBJ, family, NLM_F_ACK,
78  seq++);
79  nftnl_obj_nlmsg_build_payload(nlh, t);
80  mnl_nlmsg_batch_next(batch);
81  nftnl_obj_free(t);
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, obj_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 }