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