libnftnl  1.0.8
nft-set-elem-add.c
1 /*
2  * (C) 2013 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  * This software has been sponsored by Sophos Astaro <http://www.sophos.com>
10  */
11 
12 #include <stdlib.h>
13 #include <time.h>
14 #include <string.h>
15 #include <netinet/in.h>
16 
17 #include <linux/netfilter.h>
18 #include <linux/netfilter/nf_tables.h>
19 
20 #include <libmnl/libmnl.h>
21 #include <libnftnl/set.h>
22 
23 int main(int argc, char *argv[])
24 {
25  struct mnl_socket *nl;
26  char buf[MNL_SOCKET_BUFFER_SIZE];
27  struct mnl_nlmsg_batch *batch;
28  struct nlmsghdr *nlh;
29  uint32_t portid, seq, family;
30  struct nftnl_set *s;
31  struct nftnl_set_elem *e;
32  uint16_t data;
33  int ret;
34 
35  if (argc != 4) {
36  fprintf(stderr, "%s <family> <table> <set>\n", argv[0]);
37  exit(EXIT_FAILURE);
38  }
39 
40  s = nftnl_set_alloc();
41  if (s == NULL) {
42  perror("OOM");
43  exit(EXIT_FAILURE);
44  }
45 
46  seq = time(NULL);
47  if (strcmp(argv[1], "ip") == 0)
48  family = NFPROTO_IPV4;
49  else if (strcmp(argv[1], "ip6") == 0)
50  family = NFPROTO_IPV6;
51  else if (strcmp(argv[1], "bridge") == 0)
52  family = NFPROTO_BRIDGE;
53  else if (strcmp(argv[1], "arp") == 0)
54  family = NFPROTO_ARP;
55  else {
56  fprintf(stderr, "Unknown family: ip, ip6, bridge, arp\n");
57  exit(EXIT_FAILURE);
58  }
59 
60  nftnl_set_set(s, NFTNL_SET_TABLE, argv[2]);
61  nftnl_set_set(s, NFTNL_SET_NAME, argv[3]);
62 
63  /* Add to dummy elements to set */
64  e = nftnl_set_elem_alloc();
65  if (e == NULL) {
66  perror("OOM");
67  exit(EXIT_FAILURE);
68  }
69 
70  data = 0x1;
71  nftnl_set_elem_set(e, NFTNL_SET_ELEM_KEY, &data, sizeof(data));
72  nftnl_set_elem_add(s, e);
73 
74  e = nftnl_set_elem_alloc();
75  if (e == NULL) {
76  perror("OOM");
77  exit(EXIT_FAILURE);
78  }
79  data = 0x2;
80  nftnl_set_elem_set(e, NFTNL_SET_ELEM_KEY, &data, sizeof(data));
81  nftnl_set_elem_add(s, e);
82 
83  batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
84 
85  nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
86  mnl_nlmsg_batch_next(batch);
87 
88  nlh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
89  NFT_MSG_NEWSETELEM, family,
90  NLM_F_CREATE | NLM_F_EXCL | NLM_F_ACK,
91  seq++);
92  nftnl_set_elems_nlmsg_build_payload(nlh, s);
93  nftnl_set_free(s);
94  mnl_nlmsg_batch_next(batch);
95 
96  nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
97  mnl_nlmsg_batch_next(batch);
98 
99  nl = mnl_socket_open(NETLINK_NETFILTER);
100  if (nl == NULL) {
101  perror("mnl_socket_open");
102  exit(EXIT_FAILURE);
103  }
104 
105  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
106  perror("mnl_socket_bind");
107  exit(EXIT_FAILURE);
108  }
109  portid = mnl_socket_get_portid(nl);
110 
111  if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
112  mnl_nlmsg_batch_size(batch)) < 0) {
113  perror("mnl_socket_send");
114  exit(EXIT_FAILURE);
115  }
116 
117  mnl_nlmsg_batch_stop(batch);
118 
119  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
120  while (ret > 0) {
121  ret = mnl_cb_run(buf, ret, 0, portid, NULL, NULL);
122  if (ret <= 0)
123  break;
124  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
125  }
126  if (ret == -1) {
127  perror("error");
128  exit(EXIT_FAILURE);
129  }
130  mnl_socket_close(nl);
131 
132  return EXIT_SUCCESS;
133 }