libnftnl  1.0.5
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 <stddef.h> /* for offsetof */
16 #include <netinet/in.h>
17 #include <netinet/ip.h>
18 #include <netinet/tcp.h>
19 #include <arpa/inet.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <errno.h>
23 
24 #include <linux/netfilter.h>
25 #include <linux/netfilter/nfnetlink.h>
26 #include <linux/netfilter/nf_tables.h>
27 
28 #include <libmnl/libmnl.h>
29 #include <libnftnl/set.h>
30 
31 static struct nftnl_set *setup_set(uint8_t family, const char *table,
32  const char *name)
33 {
34  struct nftnl_set *s = NULL;
35 
36  s = nftnl_set_alloc();
37  if (s == NULL) {
38  perror("OOM");
39  exit(EXIT_FAILURE);
40  }
41 
42  nftnl_set_set_str(s, NFTNL_SET_TABLE, table);
43  nftnl_set_set_str(s, NFTNL_SET_NAME, name);
44  nftnl_set_set_u32(s, NFTNL_SET_FAMILY, family);
45  nftnl_set_set_u32(s, NFTNL_SET_KEY_LEN, 2);
46  nftnl_set_set_u32(s, NFTNL_SET_ID, 1);
47  nftnl_set_set_u32(s, NFTNL_SET_FLAGS, NFT_SET_CONSTANT);
48 
49  return s;
50 }
51 
52 static void nftnl_mnl_batch_put(char *buf, uint16_t type, uint32_t seq)
53 {
54  struct nlmsghdr *nlh;
55  struct nfgenmsg *nfg;
56 
57  nlh = mnl_nlmsg_put_header(buf);
58  nlh->nlmsg_type = type;
59  nlh->nlmsg_flags = NLM_F_REQUEST;
60  nlh->nlmsg_seq = seq;
61 
62  nfg = mnl_nlmsg_put_extra_header(nlh, sizeof(*nfg));
63  nfg->nfgen_family = AF_INET;
64  nfg->version = NFNETLINK_V0;
65  nfg->res_id = NFNL_SUBSYS_NFTABLES;
66 }
67 
68 int main(int argc, char *argv[])
69 {
70  struct mnl_socket *nl;
71  struct nftnl_set *s;
72  struct nlmsghdr *nlh;
73  struct mnl_nlmsg_batch *batch;
74  uint8_t family;
75  char buf[MNL_SOCKET_BUFFER_SIZE];
76  uint32_t seq = time(NULL);
77  int ret;
78 
79  if (argc != 4) {
80  fprintf(stderr, "Usage: %s <family> <table> <setname>\n", argv[0]);
81  exit(EXIT_FAILURE);
82  }
83 
84  if (strcmp(argv[1], "ip") == 0)
85  family = NFPROTO_IPV4;
86  else if (strcmp(argv[1], "ip6") == 0)
87  family = NFPROTO_IPV6;
88  else if (strcmp(argv[1], "bridge") == 0)
89  family = NFPROTO_BRIDGE;
90  else if (strcmp(argv[1], "arp") == 0)
91  family = NFPROTO_ARP;
92  else {
93  fprintf(stderr, "Unknown family: ip, ip6, bridge, arp\n");
94  exit(EXIT_FAILURE);
95  }
96 
97  s = setup_set(family, argv[2], argv[3]);
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 
110  batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
111 
112  nftnl_mnl_batch_put(mnl_nlmsg_batch_current(batch),
113  NFNL_MSG_BATCH_BEGIN, seq++);
114  mnl_nlmsg_batch_next(batch);
115 
116  nlh = nftnl_set_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
117  NFT_MSG_NEWSET, family,
118  NLM_F_CREATE|NLM_F_ACK, seq++);
119 
120  nftnl_set_nlmsg_build_payload(nlh, s);
121  nftnl_set_free(s);
122  mnl_nlmsg_batch_next(batch);
123 
124  nftnl_mnl_batch_put(mnl_nlmsg_batch_current(batch), NFNL_MSG_BATCH_END,
125  seq++);
126  mnl_nlmsg_batch_next(batch);
127 
128  ret = mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
129  mnl_nlmsg_batch_size(batch));
130  if (ret == -1) {
131  perror("mnl_socket_sendto");
132  exit(EXIT_FAILURE);
133  }
134 
135  mnl_nlmsg_batch_stop(batch);
136 
137  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
138  if (ret == -1) {
139  perror("mnl_socket_recvfrom");
140  exit(EXIT_FAILURE);
141  }
142 
143  ret = mnl_cb_run(buf, ret, 0, mnl_socket_get_portid(nl), NULL, NULL);
144  if (ret < 0) {
145  perror("mnl_cb_run");
146  exit(EXIT_FAILURE);
147  }
148 
149  mnl_socket_close(nl);
150 
151  return EXIT_SUCCESS;
152 }