libnftnl  1.1.8
nft-ct-expectation-get.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 
21 static 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 
35 static 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 
51  if (argc > 3)
52  nftnl_obj_set_str(t, NFTNL_OBJ_NAME, argv[3]);
53 
54  return t;
55 }
56 
57 static int obj_cb(const struct nlmsghdr *nlh, void *data)
58 {
59  uint32_t *type = data;
60  struct nftnl_obj *t;
61  char buf[4096];
62 
63  t = nftnl_obj_alloc();
64  if (t == NULL) {
65  perror("OOM");
66  goto err;
67  }
68 
69  if (nftnl_obj_nlmsg_parse(nlh, t) < 0) {
70  perror("nftnl_obj_nlmsg_parse");
71  goto err_free;
72  }
73 
74  nftnl_obj_snprintf(buf, sizeof(buf), t, *type, 0);
75  printf("%s\n", buf);
76 
77 err_free:
78  nftnl_obj_free(t);
79 err:
80  return MNL_CB_OK;
81 }
82 
83 int main(int argc, char *argv[])
84 {
85  struct mnl_socket *nl;
86  char buf[MNL_SOCKET_BUFFER_SIZE];
87  struct nlmsghdr *nlh;
88  uint32_t portid, seq, family;
89  struct nftnl_obj *t;
90  int ret;
91  uint32_t type = NFTNL_OUTPUT_DEFAULT;
92  uint16_t flags = NLM_F_ACK;
93 
94  if (argc < 3 || argc > 4) {
95  fprintf(stderr, "%s <family> <table> [<name>]\n", argv[0]);
96  return EXIT_FAILURE;
97  }
98 
99  t = obj_parse(argc, argv);
100  if (t == NULL)
101  exit(EXIT_FAILURE);
102  family = nftnl_obj_get_u32(t, NFTNL_OBJ_FAMILY);
103 
104  seq = time(NULL);
105  if (argc < 4)
106  flags = NLM_F_DUMP;
107  nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_GETOBJ, family, flags, seq);
108  nftnl_obj_nlmsg_build_payload(nlh, t);
109  nftnl_obj_free(t);
110 
111  nl = mnl_socket_open(NETLINK_NETFILTER);
112  if (nl == NULL) {
113  perror("mnl_socket_open");
114  exit(EXIT_FAILURE);
115  }
116 
117  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
118  perror("mnl_socket_bind");
119  exit(EXIT_FAILURE);
120  }
121  portid = mnl_socket_get_portid(nl);
122 
123  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
124  perror("mnl_socket_send");
125  exit(EXIT_FAILURE);
126  }
127 
128  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
129  while (ret > 0) {
130  ret = mnl_cb_run(buf, ret, seq, portid, obj_cb, &type);
131  if (ret <= 0)
132  break;
133  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
134  }
135  if (ret == -1) {
136  perror("error");
137  exit(EXIT_FAILURE);
138  }
139  mnl_socket_close(nl);
140 
141  return EXIT_SUCCESS;
142 }