libnftnl  1.1.4
nft-map-add.c
1 /*
2  * (C) 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 <stddef.h> /* for offsetof */
14 #include <netinet/in.h>
15 #include <netinet/ip.h>
16 #include <netinet/tcp.h>
17 #include <arpa/inet.h>
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <errno.h>
21 
22 #include <linux/netfilter.h>
23 #include <linux/netfilter/nfnetlink.h>
24 #include <linux/netfilter/nf_tables.h>
25 
26 #include <libmnl/libmnl.h>
27 #include <libnftnl/set.h>
28 
29 static struct nftnl_set *setup_set(uint8_t family, const char *table,
30  const char *name)
31 {
32  struct nftnl_set *s = NULL;
33 
34  s = nftnl_set_alloc();
35  if (s == NULL) {
36  perror("OOM");
37  exit(EXIT_FAILURE);
38  }
39 
40  nftnl_set_set_str(s, NFTNL_SET_TABLE, table);
41  nftnl_set_set_str(s, NFTNL_SET_NAME, name);
42  nftnl_set_set_u32(s, NFTNL_SET_FAMILY, family);
43  nftnl_set_set_u32(s, NFTNL_SET_KEY_LEN, 2);
44  /* See nftables/include/datatype.h, where TYPE_INET_SERVICE is 13. We
45  * should place these datatypes in a public header so third party
46  * applications still work with nftables.
47  */
48  nftnl_set_set_u32(s, NFTNL_SET_KEY_TYPE, 13);
49  nftnl_set_set_u32(s, NFTNL_SET_DATA_LEN, 2);
50  nftnl_set_set_u32(s, NFTNL_SET_DATA_TYPE, 13);
51  nftnl_set_set_u32(s, NFTNL_SET_ID, 1);
52  nftnl_set_set_u32(s, NFTNL_SET_FLAGS, NFT_SET_CONSTANT | NFT_SET_MAP);
53 
54  return s;
55 }
56 
57 int main(int argc, char *argv[])
58 {
59  struct mnl_socket *nl;
60  struct nftnl_set *s;
61  struct nlmsghdr *nlh;
62  struct mnl_nlmsg_batch *batch;
63  uint8_t family;
64  char buf[MNL_SOCKET_BUFFER_SIZE];
65  uint32_t seq = time(NULL);
66  int ret;
67 
68  if (argc != 4) {
69  fprintf(stderr, "Usage: %s <family> <table> <setname>\n", argv[0]);
70  exit(EXIT_FAILURE);
71  }
72 
73  if (strcmp(argv[1], "ip") == 0)
74  family = NFPROTO_IPV4;
75  else if (strcmp(argv[1], "ip6") == 0)
76  family = NFPROTO_IPV6;
77  else if (strcmp(argv[1], "bridge") == 0)
78  family = NFPROTO_BRIDGE;
79  else if (strcmp(argv[1], "arp") == 0)
80  family = NFPROTO_ARP;
81  else {
82  fprintf(stderr, "Unknown family: ip, ip6, bridge, arp\n");
83  exit(EXIT_FAILURE);
84  }
85 
86  s = setup_set(family, argv[2], argv[3]);
87 
88  nl = mnl_socket_open(NETLINK_NETFILTER);
89  if (nl == NULL) {
90  perror("mnl_socket_open");
91  exit(EXIT_FAILURE);
92  }
93 
94  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
95  perror("mnl_socket_bind");
96  exit(EXIT_FAILURE);
97  }
98 
99  batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
100 
101  nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
102  mnl_nlmsg_batch_next(batch);
103 
104  nlh = nftnl_set_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
105  NFT_MSG_NEWSET, family,
106  NLM_F_CREATE|NLM_F_ACK, seq++);
107 
108  nftnl_set_nlmsg_build_payload(nlh, s);
109  nftnl_set_free(s);
110  mnl_nlmsg_batch_next(batch);
111 
112  nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
113  mnl_nlmsg_batch_next(batch);
114 
115  ret = mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
116  mnl_nlmsg_batch_size(batch));
117  if (ret == -1) {
118  perror("mnl_socket_sendto");
119  exit(EXIT_FAILURE);
120  }
121 
122  mnl_nlmsg_batch_stop(batch);
123 
124  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
125  if (ret == -1) {
126  perror("mnl_socket_recvfrom");
127  exit(EXIT_FAILURE);
128  }
129 
130  ret = mnl_cb_run(buf, ret, 0, mnl_socket_get_portid(nl), NULL, NULL);
131  if (ret < 0) {
132  perror("mnl_cb_run");
133  exit(EXIT_FAILURE);
134  }
135 
136  mnl_socket_close(nl);
137 
138  return EXIT_SUCCESS;
139 }