libnftnl 1.2.6
nft-obj-add.c
1/*
2 * (C) 2012-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 <netinet/in.h>
14
15#include <linux/netfilter.h>
16#include <linux/netfilter/nf_tables.h>
17
18#include <libmnl/libmnl.h>
19#include <libnftnl/object.h>
20
21static struct nftnl_obj *obj_add_parse(int argc, char *argv[])
22{
23 struct nftnl_obj *t;
24 uint16_t family;
25
26 if (strcmp(argv[1], "ip") == 0)
27 family = NFPROTO_IPV4;
28 else if (strcmp(argv[1], "ip6") == 0)
29 family = NFPROTO_IPV6;
30 else if (strcmp(argv[1], "inet") == 0)
31 family = NFPROTO_INET;
32 else if (strcmp(argv[1], "bridge") == 0)
33 family = NFPROTO_BRIDGE;
34 else if (strcmp(argv[1], "arp") == 0)
35 family = NFPROTO_ARP;
36 else {
37 fprintf(stderr, "Unknown family: ip, ip6, inet, bridge, arp\n");
38 return NULL;
39 }
40
41 t = nftnl_obj_alloc();
42 if (t == NULL) {
43 perror("OOM");
44 return NULL;
45 }
46
47 nftnl_obj_set_u32(t, NFTNL_OBJ_FAMILY, family);
48 nftnl_obj_set_u32(t, NFTNL_OBJ_TYPE, NFT_OBJECT_COUNTER);
49 nftnl_obj_set_str(t, NFTNL_OBJ_TABLE, argv[2]);
50 nftnl_obj_set_str(t, NFTNL_OBJ_NAME, argv[3]);
51
52 return t;
53}
54
55int 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, obj_seq, family;
61 struct nftnl_obj *t;
62 struct mnl_nlmsg_batch *batch;
63 int ret;
64
65 if (argc != 4) {
66 fprintf(stderr, "%s <family> <table> <name>\n", argv[0]);
67 exit(EXIT_FAILURE);
68 }
69
70 t = obj_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 obj_seq = seq;
81 family = nftnl_obj_get_u32(t, NFTNL_OBJ_FAMILY);
82 nlh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
83 NFT_MSG_NEWOBJ, family, NLM_F_ACK, seq++);
84 nftnl_obj_nlmsg_build_payload(nlh, t);
85 nftnl_obj_free(t);
86 mnl_nlmsg_batch_next(batch);
87
88 nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
89 mnl_nlmsg_batch_next(batch);
90
91 nl = mnl_socket_open(NETLINK_NETFILTER);
92 if (nl == NULL) {
93 perror("mnl_socket_open");
94 exit(EXIT_FAILURE);
95 }
96
97 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
98 perror("mnl_socket_bind");
99 exit(EXIT_FAILURE);
100 }
101 portid = mnl_socket_get_portid(nl);
102
103 if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
104 mnl_nlmsg_batch_size(batch)) < 0) {
105 perror("mnl_socket_send");
106 exit(EXIT_FAILURE);
107 }
108
109 mnl_nlmsg_batch_stop(batch);
110
111 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
112 while (ret > 0) {
113 ret = mnl_cb_run(buf, ret, obj_seq, portid, NULL, NULL);
114 if (ret <= 0)
115 break;
116 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
117 }
118 if (ret == -1) {
119 perror("error");
120 exit(EXIT_FAILURE);
121 }
122 mnl_socket_close(nl);
123
124 return EXIT_SUCCESS;
125}