libnftnl  1.2.0
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], "inet") == 0)
78  family = NFPROTO_INET;
79  else if (strcmp(argv[1], "bridge") == 0)
80  family = NFPROTO_BRIDGE;
81  else if (strcmp(argv[1], "arp") == 0)
82  family = NFPROTO_ARP;
83  else {
84  fprintf(stderr, "Unknown family: ip, ip6, bridge, arp\n");
85  exit(EXIT_FAILURE);
86  }
87 
88  s = setup_set(family, argv[2], argv[3]);
89 
90  nl = mnl_socket_open(NETLINK_NETFILTER);
91  if (nl == NULL) {
92  perror("mnl_socket_open");
93  exit(EXIT_FAILURE);
94  }
95 
96  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
97  perror("mnl_socket_bind");
98  exit(EXIT_FAILURE);
99  }
100 
101  batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
102 
103  nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
104  mnl_nlmsg_batch_next(batch);
105 
106  nlh = nftnl_set_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
107  NFT_MSG_NEWSET, family,
108  NLM_F_CREATE|NLM_F_ACK, seq++);
109 
110  nftnl_set_nlmsg_build_payload(nlh, s);
111  nftnl_set_free(s);
112  mnl_nlmsg_batch_next(batch);
113 
114  nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
115  mnl_nlmsg_batch_next(batch);
116 
117  ret = mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
118  mnl_nlmsg_batch_size(batch));
119  if (ret == -1) {
120  perror("mnl_socket_sendto");
121  exit(EXIT_FAILURE);
122  }
123 
124  mnl_nlmsg_batch_stop(batch);
125 
126  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
127  if (ret == -1) {
128  perror("mnl_socket_recvfrom");
129  exit(EXIT_FAILURE);
130  }
131 
132  ret = mnl_cb_run(buf, ret, 0, mnl_socket_get_portid(nl), NULL, NULL);
133  if (ret < 0) {
134  perror("mnl_cb_run");
135  exit(EXIT_FAILURE);
136  }
137 
138  mnl_socket_close(nl);
139 
140  return EXIT_SUCCESS;
141 }