libnftnl  1.0.2
nft-set-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/table.h>
22 #include <libnftnl/set.h>
23 
24 int main(int argc, char *argv[])
25 {
26  struct mnl_socket *nl;
27  char buf[MNL_SOCKET_BUFFER_SIZE];
28  struct nlmsghdr *nlh;
29  uint32_t portid, seq, family;
30  struct nft_set *t = NULL;
31  int ret;
32 
33  if (argc != 4) {
34  fprintf(stderr, "%s <family> <table> <set>\n", argv[0]);
35  exit(EXIT_FAILURE);
36  }
37 
38  t = nft_set_alloc();
39  if (t == NULL) {
40  perror("OOM");
41  exit(EXIT_FAILURE);
42  }
43 
44  seq = time(NULL);
45  if (strcmp(argv[1], "ip") == 0)
46  family = NFPROTO_IPV4;
47  else if (strcmp(argv[1], "ip6") == 0)
48  family = NFPROTO_IPV6;
49  else if (strcmp(argv[1], "bridge") == 0)
50  family = NFPROTO_BRIDGE;
51  else if (strcmp(argv[1], "arp") == 0)
52  family = NFPROTO_ARP;
53  else {
54  fprintf(stderr, "Unknown family: ip, ip6, bridge, arp\n");
55  exit(EXIT_FAILURE);
56  }
57 
58  nft_set_attr_set(t, NFT_SET_ATTR_TABLE, argv[2]);
59  nft_set_attr_set(t, NFT_SET_ATTR_NAME, argv[3]);
60  /* destroy set when the rule is destroyed, elements canno change */
61  nft_set_attr_set_u32(t, NFT_SET_ATTR_FLAGS, NFT_SET_ANONYMOUS |
62  NFT_SET_CONSTANT);
63  /* This key is only used by user-space to interpret key type */
64  nft_set_attr_set_u32(t, NFT_SET_ATTR_KEY_TYPE, 0);
65  /* key is 4 bytes long */
66  nft_set_attr_set_u32(t, NFT_SET_ATTR_KEY_LEN, 4);
67  /*
68  * data type and data length only useful for mapping (1:1):
69  * matching -> action.
70  *
71  * This data is only used by user-space to interpret data type.
72  */
73  // nft_set_attr_set_u32(t, NFT_SET_ATTR_DATA_TYPE, NFT_DATA_VERDICT);
74  // nft_set_attr_set_u32(t, NFT_SET_ATTR_DATA_LEN, 4);
75 
76  nlh = nft_set_nlmsg_build_hdr(buf, NFT_MSG_NEWSET, family,
77  NLM_F_CREATE|NLM_F_EXCL|NLM_F_ACK, seq);
78  nft_set_nlmsg_build_payload(nlh, t);
79  nft_set_free(t);
80 
81  nl = mnl_socket_open(NETLINK_NETFILTER);
82  if (nl == NULL) {
83  perror("mnl_socket_open");
84  exit(EXIT_FAILURE);
85  }
86 
87  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
88  perror("mnl_socket_bind");
89  exit(EXIT_FAILURE);
90  }
91  portid = mnl_socket_get_portid(nl);
92 
93  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
94  perror("mnl_socket_send");
95  exit(EXIT_FAILURE);
96  }
97 
98  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
99  while (ret > 0) {
100  ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);
101  if (ret <= 0)
102  break;
103  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
104  }
105  if (ret == -1) {
106  perror("error");
107  exit(EXIT_FAILURE);
108  }
109  mnl_socket_close(nl);
110 
111  return EXIT_SUCCESS;
112 }