libnftnl 1.2.6
nft-flowtable-add.c
1#include <stdlib.h>
2#include <time.h>
3#include <string.h>
4#include <netinet/in.h>
5
6#include <linux/netfilter.h>
7#include <linux/netfilter/nf_tables.h>
8
9#include <libmnl/libmnl.h>
10#include <libnftnl/flowtable.h>
11
12static struct nftnl_flowtable *flowtable_add_parse(int argc, char *argv[])
13{
14 const char *dev_array[] = { "eth0", "tap0", NULL };
15 struct nftnl_flowtable *t;
16 int hooknum = 0;
17
18 if (strcmp(argv[4], "ingress") == 0)
19 hooknum = NF_NETDEV_INGRESS;
20 else {
21 fprintf(stderr, "Unknown hook: %s\n", argv[4]);
22 return NULL;
23 }
24
25 t = nftnl_flowtable_alloc();
26 if (t == NULL) {
27 perror("OOM");
28 return NULL;
29 }
30 nftnl_flowtable_set_str(t, NFTNL_FLOWTABLE_TABLE, argv[2]);
31 nftnl_flowtable_set_str(t, NFTNL_FLOWTABLE_NAME, argv[3]);
32 if (argc == 6) {
33 nftnl_flowtable_set_u32(t, NFTNL_FLOWTABLE_HOOKNUM, hooknum);
34 nftnl_flowtable_set_u32(t, NFTNL_FLOWTABLE_PRIO, atoi(argv[5]));
35 }
36 nftnl_flowtable_set_data(t, NFTNL_FLOWTABLE_DEVICES, dev_array, 0);
37
38 return t;
39}
40
41int main(int argc, char *argv[])
42{
43 struct mnl_socket *nl;
44 char buf[MNL_SOCKET_BUFFER_SIZE];
45 struct nlmsghdr *nlh;
46 uint32_t portid, seq, flowtable_seq;
47 int ret, family;
48 struct nftnl_flowtable *t;
49 struct mnl_nlmsg_batch *batch;
50
51 if (argc != 6) {
52 fprintf(stderr, "Usage: %s <family> <table> <name> <hook> <prio>\n",
53 argv[0]);
54 exit(EXIT_FAILURE);
55 }
56
57 if (strcmp(argv[1], "ip") == 0)
58 family = NFPROTO_IPV4;
59 else if (strcmp(argv[1], "ip6") == 0)
60 family = NFPROTO_IPV6;
61 else if (strcmp(argv[1], "inet") == 0)
62 family = NFPROTO_INET;
63 else if (strcmp(argv[1], "bridge") == 0)
64 family = NFPROTO_BRIDGE;
65 else if (strcmp(argv[1], "arp") == 0)
66 family = NFPROTO_ARP;
67 else {
68 fprintf(stderr, "Unknown family: ip, ip6, inet, bridge, arp\n");
69 exit(EXIT_FAILURE);
70 }
71
72 t = flowtable_add_parse(argc, argv);
73 if (t == NULL)
74 exit(EXIT_FAILURE);
75
76 seq = time(NULL);
77 batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
78
79 nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
80 mnl_nlmsg_batch_next(batch);
81
82 flowtable_seq = seq;
83 nlh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
84 NFT_MSG_NEWFLOWTABLE, family,
85 NLM_F_CREATE | NLM_F_ACK, seq++);
86 nftnl_flowtable_nlmsg_build_payload(nlh, t);
87 nftnl_flowtable_free(t);
88 mnl_nlmsg_batch_next(batch);
89
90 nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
91 mnl_nlmsg_batch_next(batch);
92
93 nl = mnl_socket_open(NETLINK_NETFILTER);
94 if (nl == NULL) {
95 perror("mnl_socket_open");
96 exit(EXIT_FAILURE);
97 }
98
99 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
100 perror("mnl_socket_bind");
101 exit(EXIT_FAILURE);
102 }
103 portid = mnl_socket_get_portid(nl);
104
105 if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
106 mnl_nlmsg_batch_size(batch)) < 0) {
107 perror("mnl_socket_send");
108 exit(EXIT_FAILURE);
109 }
110
111 mnl_nlmsg_batch_stop(batch);
112
113 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
114 while (ret > 0) {
115 ret = mnl_cb_run(buf, ret, flowtable_seq, portid, NULL, NULL);
116 if (ret <= 0)
117 break;
118 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
119 }
120 if (ret == -1) {
121 perror("error");
122 exit(EXIT_FAILURE);
123 }
124 mnl_socket_close(nl);
125
126 return EXIT_SUCCESS;
127}