libnftnl 1.2.9
nft-table-upd.c
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.org>
4 *
5 * This software has been sponsored by Sophos Astaro <http://www.sophos.com>
6 */
7
8#include <stdlib.h>
9#include <time.h>
10#include <string.h>
11#include <netinet/in.h>
12
13#include <linux/netfilter.h>
14#include <linux/netfilter/nf_tables.h>
15
16#include <libmnl/libmnl.h>
17#include <libnftnl/table.h>
18
19int main(int argc, char *argv[])
20{
21 struct mnl_socket *nl;
22 char buf[MNL_SOCKET_BUFFER_SIZE];
23 struct nlmsghdr *nlh;
24 uint32_t portid, seq, table_seq, family, flags;
25 struct nftnl_table *t = NULL;
26 struct mnl_nlmsg_batch *batch;
27 int ret;
28
29 if (argc != 4) {
30 fprintf(stderr, "%s <family> <name> <state>\n", argv[0]);
31 exit(EXIT_FAILURE);
32 }
33
34 t = nftnl_table_alloc();
35 if (t == NULL) {
36 perror("OOM");
37 exit(EXIT_FAILURE);
38 }
39
40 seq = time(NULL);
41 batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
42
43 nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
44 mnl_nlmsg_batch_next(batch);
45
46 if (strcmp(argv[1], "ip") == 0)
47 family = NFPROTO_IPV4;
48 else if (strcmp(argv[1], "ip6") == 0)
49 family = NFPROTO_IPV6;
50 else if (strcmp(argv[1], "inet") == 0)
51 family = NFPROTO_INET;
52 else if (strcmp(argv[1], "bridge") == 0)
53 family = NFPROTO_BRIDGE;
54 else if (strcmp(argv[1], "arp") == 0)
55 family = NFPROTO_ARP;
56 else if (strcmp(argv[1], "netdev") == 0)
57 family = NFPROTO_NETDEV;
58 else {
59 fprintf(stderr,
60 "Unknown family: ip, ip6, inet, bridge, arp, netdev\n");
61 exit(EXIT_FAILURE);
62 }
63
64 if (strcmp(argv[3], "active") == 0)
65 flags = 0;
66 else if (strcmp(argv[3], "dormant") == 0)
67 flags = NFT_TABLE_F_DORMANT;
68 else {
69 fprintf(stderr, "Unknown state: active, dormant\n");
70 exit(EXIT_FAILURE);
71 }
72
73 nftnl_table_set_str(t, NFTNL_TABLE_NAME, argv[2]);
74 nftnl_table_set_u32(t, NFTNL_TABLE_FLAGS, flags);
75
76 table_seq = seq;
77 nlh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
78 NFT_MSG_NEWTABLE, family, NLM_F_ACK, seq++);
79 nftnl_table_nlmsg_build_payload(nlh, t);
80 nftnl_table_free(t);
81 mnl_nlmsg_batch_next(batch);
82
83 nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
84 mnl_nlmsg_batch_next(batch);
85
86 nl = mnl_socket_open(NETLINK_NETFILTER);
87 if (nl == NULL) {
88 perror("mnl_socket_open");
89 exit(EXIT_FAILURE);
90 }
91
92 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
93 perror("mnl_socket_bind");
94 exit(EXIT_FAILURE);
95 }
96 portid = mnl_socket_get_portid(nl);
97
98 if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
99 mnl_nlmsg_batch_size(batch)) < 0) {
100 perror("mnl_socket_send");
101 exit(EXIT_FAILURE);
102 }
103
104 mnl_nlmsg_batch_stop(batch);
105
106 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
107 while (ret > 0) {
108 ret = mnl_cb_run(buf, ret, table_seq, portid, NULL, NULL);
109 if (ret <= 0)
110 break;
111 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
112 }
113 if (ret == -1) {
114 perror("error");
115 exit(EXIT_FAILURE);
116 }
117 mnl_socket_close(nl);
118
119 return EXIT_SUCCESS;
120}