libnftnl  1.1.4
nft-obj-add.c
1 /*
2  * (C) 2012-2016 by Pablo Neira Ayuso <pablo@netfilter.org>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by
6  * 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 struct nftnl_obj *obj_add_parse(int argc, char *argv[])
22 {
23  struct nftnl_obj *t;
24  uint16_t family;
25 
26  if (strcmp(argv[1], "ip") == 0)
27  family = NFPROTO_IPV4;
28  else if (strcmp(argv[1], "ip6") == 0)
29  family = NFPROTO_IPV6;
30  else if (strcmp(argv[1], "bridge") == 0)
31  family = NFPROTO_BRIDGE;
32  else if (strcmp(argv[1], "arp") == 0)
33  family = NFPROTO_ARP;
34  else {
35  fprintf(stderr, "Unknown family: ip, ip6, bridge, arp\n");
36  return NULL;
37  }
38 
39  t = nftnl_obj_alloc();
40  if (t == NULL) {
41  perror("OOM");
42  return NULL;
43  }
44 
45  nftnl_obj_set_u32(t, NFTNL_OBJ_FAMILY, family);
46  nftnl_obj_set_u32(t, NFTNL_OBJ_TYPE, NFT_OBJECT_COUNTER);
47  nftnl_obj_set_str(t, NFTNL_OBJ_TABLE, argv[2]);
48  nftnl_obj_set_str(t, NFTNL_OBJ_NAME, argv[3]);
49 
50  return t;
51 }
52 
53 int main(int argc, char *argv[])
54 {
55  struct mnl_socket *nl;
56  char buf[MNL_SOCKET_BUFFER_SIZE];
57  struct nlmsghdr *nlh;
58  uint32_t portid, seq, obj_seq, family;
59  struct nftnl_obj *t;
60  struct mnl_nlmsg_batch *batch;
61  int ret;
62 
63  if (argc != 4) {
64  fprintf(stderr, "%s <family> <table> <name>\n", argv[0]);
65  exit(EXIT_FAILURE);
66  }
67 
68  t = obj_add_parse(argc, argv);
69  if (t == NULL)
70  exit(EXIT_FAILURE);
71 
72  seq = time(NULL);
73  batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
74 
75  nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
76  mnl_nlmsg_batch_next(batch);
77 
78  obj_seq = seq;
79  family = nftnl_obj_get_u32(t, NFTNL_OBJ_FAMILY);
80  nlh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
81  NFT_MSG_NEWOBJ, family, NLM_F_ACK, seq++);
82  nftnl_obj_nlmsg_build_payload(nlh, t);
83  nftnl_obj_free(t);
84  mnl_nlmsg_batch_next(batch);
85 
86  nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
87  mnl_nlmsg_batch_next(batch);
88 
89  nl = mnl_socket_open(NETLINK_NETFILTER);
90  if (nl == NULL) {
91  perror("mnl_socket_open");
92  exit(EXIT_FAILURE);
93  }
94 
95  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
96  perror("mnl_socket_bind");
97  exit(EXIT_FAILURE);
98  }
99  portid = mnl_socket_get_portid(nl);
100 
101  if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
102  mnl_nlmsg_batch_size(batch)) < 0) {
103  perror("mnl_socket_send");
104  exit(EXIT_FAILURE);
105  }
106 
107  mnl_nlmsg_batch_stop(batch);
108 
109  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
110  while (ret > 0) {
111  ret = mnl_cb_run(buf, ret, obj_seq, portid, NULL, NULL);
112  if (ret <= 0)
113  break;
114  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
115  }
116  if (ret == -1) {
117  perror("error");
118  exit(EXIT_FAILURE);
119  }
120  mnl_socket_close(nl);
121 
122  return EXIT_SUCCESS;
123 }