libnftnl  1.2.9
nft-obj-add.c
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * (C) 2012-2016 by Pablo Neira Ayuso <pablo@netfilter.org>
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 
17 static struct nftnl_obj *obj_add_parse(int argc, char *argv[])
18 {
19  struct nftnl_obj *t;
20  uint16_t family;
21 
22  if (strcmp(argv[1], "ip") == 0)
23  family = NFPROTO_IPV4;
24  else if (strcmp(argv[1], "ip6") == 0)
25  family = NFPROTO_IPV6;
26  else if (strcmp(argv[1], "inet") == 0)
27  family = NFPROTO_INET;
28  else if (strcmp(argv[1], "bridge") == 0)
29  family = NFPROTO_BRIDGE;
30  else if (strcmp(argv[1], "arp") == 0)
31  family = NFPROTO_ARP;
32  else {
33  fprintf(stderr, "Unknown family: ip, ip6, inet, bridge, arp\n");
34  return NULL;
35  }
36 
37  t = nftnl_obj_alloc();
38  if (t == NULL) {
39  perror("OOM");
40  return NULL;
41  }
42 
43  nftnl_obj_set_u32(t, NFTNL_OBJ_FAMILY, family);
44  nftnl_obj_set_u32(t, NFTNL_OBJ_TYPE, NFT_OBJECT_COUNTER);
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 
51 int 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_add_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_NEWOBJ, family, NLM_F_ACK, seq++);
80  nftnl_obj_nlmsg_build_payload(nlh, t);
81  nftnl_obj_free(t);
82  mnl_nlmsg_batch_next(batch);
83 
84  nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
85  mnl_nlmsg_batch_next(batch);
86 
87  nl = mnl_socket_open(NETLINK_NETFILTER);
88  if (nl == NULL) {
89  perror("mnl_socket_open");
90  exit(EXIT_FAILURE);
91  }
92 
93  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
94  perror("mnl_socket_bind");
95  exit(EXIT_FAILURE);
96  }
97  portid = mnl_socket_get_portid(nl);
98 
99  if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
100  mnl_nlmsg_batch_size(batch)) < 0) {
101  perror("mnl_socket_send");
102  exit(EXIT_FAILURE);
103  }
104 
105  mnl_nlmsg_batch_stop(batch);
106 
107  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
108  while (ret > 0) {
109  ret = mnl_cb_run(buf, ret, obj_seq, portid, NULL, NULL);
110  if (ret <= 0)
111  break;
112  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
113  }
114  if (ret == -1) {
115  perror("error");
116  exit(EXIT_FAILURE);
117  }
118  mnl_socket_close(nl);
119 
120  return EXIT_SUCCESS;
121 }