libnftnl  1.2.9
nft-compat-get.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/nfnetlink.h>
14 #include <linux/netfilter/nf_tables_compat.h>
15 
16 #include <libmnl/libmnl.h>
17 
18 static int data_attr_cb(const struct nlattr *attr, void *data)
19 {
20  const struct nlattr **tb = data;
21  int type = mnl_attr_get_type(attr);
22 
23  if (mnl_attr_type_valid(attr, NFTA_COMPAT_MAX) < 0)
24  return MNL_CB_OK;
25 
26  switch(type) {
27  case NFTA_COMPAT_NAME:
28  if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) {
29  perror("mnl_attr_validate");
30  return MNL_CB_ERROR;
31  }
32  break;
33  case NFTA_COMPAT_REV:
34  case NFTA_COMPAT_TYPE:
35  if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
36  perror("mnl_attr_validate");
37  return MNL_CB_ERROR;
38  }
39  break;
40  }
41  tb[type] = attr;
42  return MNL_CB_OK;
43 }
44 
45 static int cb(const struct nlmsghdr *nlh, void *data)
46 {
47  struct nlattr *tb[NFTA_COMPAT_MAX+1] = {};
48  struct nfgenmsg *nfg = mnl_nlmsg_get_payload(nlh);
49 
50  if (mnl_attr_parse(nlh, sizeof(*nfg), data_attr_cb, tb) < 0)
51  return MNL_CB_ERROR;
52 
53  if (tb[NFTA_COMPAT_NAME])
54  printf("name=%s ", mnl_attr_get_str(tb[NFTA_COMPAT_NAME]));
55  if (tb[NFTA_COMPAT_REV])
56  printf("rev=%d ", ntohl(mnl_attr_get_u32(tb[NFTA_COMPAT_REV])));
57  if (tb[NFTA_COMPAT_TYPE])
58  printf("type=%d ", ntohl(mnl_attr_get_u32(tb[NFTA_COMPAT_REV])));
59 
60  printf("\n");
61 
62  return MNL_CB_OK;
63 }
64 
65 int main(int argc, char *argv[])
66 {
67  struct mnl_socket *nl;
68  char buf[MNL_SOCKET_BUFFER_SIZE];
69  struct nlmsghdr *nlh;
70  uint32_t portid, seq, rev, type;
71  int ret;
72 
73  if (argc != 4) {
74  fprintf(stderr, "Usage: %s <extension_name> <type> <rev>\n",
75  argv[0]);
76  return EXIT_FAILURE;
77  }
78 
79  if (strcmp(argv[2], "target") == 0)
80  type = 1;
81  else if (strcmp(argv[2], "match") == 0)
82  type = 0;
83  else {
84  fprintf(stderr, "type should be `target' or `match'\n");
85  return EXIT_FAILURE;
86  }
87  rev = atoi(argv[3]);
88 
89  nlh = mnl_nlmsg_put_header(buf);
90  nlh->nlmsg_type = (NFNL_SUBSYS_NFT_COMPAT << 8) | NFNL_MSG_COMPAT_GET;
91  nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
92  nlh->nlmsg_seq = seq = time(NULL);
93 
94  struct nfgenmsg *nfg = mnl_nlmsg_put_extra_header(nlh, sizeof(*nfg));
95  nfg->nfgen_family = AF_INET;
96  nfg->version = NFNETLINK_V0;
97  nfg->res_id = 0;
98 
99  mnl_attr_put_strz(nlh, NFTA_COMPAT_NAME, argv[1]);
100  mnl_attr_put_u32(nlh, NFTA_COMPAT_REV, htonl(rev));
101  mnl_attr_put_u32(nlh, NFTA_COMPAT_TYPE, htonl(type));
102 
103  printf("requesting `%s' rev=%d type=%d\n", argv[1], rev, type);
104 
105  nl = mnl_socket_open(NETLINK_NETFILTER);
106  if (nl == NULL) {
107  perror("mnl_socket_open");
108  exit(EXIT_FAILURE);
109  }
110 
111  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
112  perror("mnl_socket_bind");
113  exit(EXIT_FAILURE);
114  }
115  portid = mnl_socket_get_portid(nl);
116 
117  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
118  perror("mnl_socket_send");
119  exit(EXIT_FAILURE);
120  }
121 
122  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
123  while (ret > 0) {
124  ret = mnl_cb_run(buf, ret, seq, portid, cb, NULL);
125  if (ret <= 0)
126  break;
127  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
128  }
129  if (ret == -1) {
130  perror("error");
131  exit(EXIT_FAILURE);
132  }
133  mnl_socket_close(nl);
134 
135  return EXIT_SUCCESS;
136 }