libnftnl  1.2.0
nft-table-add.c
1 /*
2  * (C) 2012 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 
23 static struct nftnl_table *table_add_parse(int argc, char *argv[])
24 {
25  struct nftnl_table *t;
26  uint16_t family;
27 
28  if (strcmp(argv[1], "ip") == 0)
29  family = NFPROTO_IPV4;
30  else if (strcmp(argv[1], "ip6") == 0)
31  family = NFPROTO_IPV6;
32  else if (strcmp(argv[1], "inet") == 0)
33  family = NFPROTO_INET;
34  else if (strcmp(argv[1], "bridge") == 0)
35  family = NFPROTO_BRIDGE;
36  else if (strcmp(argv[1], "arp") == 0)
37  family = NFPROTO_ARP;
38  else {
39  fprintf(stderr, "Unknown family: ip, ip6, inet, bridge, arp\n");
40  return NULL;
41  }
42 
43  t = nftnl_table_alloc();
44  if (t == NULL) {
45  perror("OOM");
46  return NULL;
47  }
48 
49  nftnl_table_set_u32(t, NFTNL_TABLE_FAMILY, family);
50  nftnl_table_set_str(t, NFTNL_TABLE_NAME, argv[2]);
51 
52  return t;
53 }
54 
55 int main(int argc, char *argv[])
56 {
57  struct mnl_socket *nl;
58  char buf[MNL_SOCKET_BUFFER_SIZE];
59  struct nlmsghdr *nlh;
60  uint32_t portid, seq, table_seq, family;
61  struct nftnl_table *t;
62  struct mnl_nlmsg_batch *batch;
63  int ret;
64 
65  if (argc != 3) {
66  fprintf(stderr, "%s <family> <name>\n", argv[0]);
67  exit(EXIT_FAILURE);
68  }
69 
70  t = table_add_parse(argc, argv);
71  if (t == NULL)
72  exit(EXIT_FAILURE);
73 
74  seq = time(NULL);
75  batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
76 
77  nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
78  mnl_nlmsg_batch_next(batch);
79 
80  table_seq = seq;
81  family = nftnl_table_get_u32(t, NFTNL_TABLE_FAMILY);
82  nlh = nftnl_table_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
83  NFT_MSG_NEWTABLE, family,
84  NLM_F_CREATE|NLM_F_ACK, seq++);
85  nftnl_table_nlmsg_build_payload(nlh, t);
86  nftnl_table_free(t);
87  mnl_nlmsg_batch_next(batch);
88 
89  nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
90  mnl_nlmsg_batch_next(batch);
91 
92  nl = mnl_socket_open(NETLINK_NETFILTER);
93  if (nl == NULL) {
94  perror("mnl_socket_open");
95  exit(EXIT_FAILURE);
96  }
97 
98  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
99  perror("mnl_socket_bind");
100  exit(EXIT_FAILURE);
101  }
102  portid = mnl_socket_get_portid(nl);
103 
104  if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
105  mnl_nlmsg_batch_size(batch)) < 0) {
106  perror("mnl_socket_send");
107  exit(EXIT_FAILURE);
108  }
109 
110  mnl_nlmsg_batch_stop(batch);
111 
112  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
113  while (ret > 0) {
114  ret = mnl_cb_run(buf, ret, table_seq, portid, NULL, NULL);
115  if (ret <= 0)
116  break;
117  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
118  }
119  if (ret == -1) {
120  perror("error");
121  exit(EXIT_FAILURE);
122  }
123  mnl_socket_close(nl);
124 
125  return EXIT_SUCCESS;
126 }