libnftnl  1.1.7
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 
12 static 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 
41 int 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  int batching;
51 
52  if (argc != 6) {
53  fprintf(stderr, "Usage: %s <family> <table> <name> <hook> <prio>\n",
54  argv[0]);
55  exit(EXIT_FAILURE);
56  }
57 
58  if (strcmp(argv[1], "ip") == 0)
59  family = NFPROTO_IPV4;
60  else if (strcmp(argv[1], "ip6") == 0)
61  family = NFPROTO_IPV6;
62  else if (strcmp(argv[1], "bridge") == 0)
63  family = NFPROTO_BRIDGE;
64  else if (strcmp(argv[1], "arp") == 0)
65  family = NFPROTO_ARP;
66  else {
67  fprintf(stderr, "Unknown family: ip, ip6, bridge, arp\n");
68  exit(EXIT_FAILURE);
69  }
70 
71  t = flowtable_add_parse(argc, argv);
72  if (t == NULL)
73  exit(EXIT_FAILURE);
74 
75  batching = nftnl_batch_is_supported();
76  if (batching < 0) {
77  perror("cannot talk to nfnetlink");
78  exit(EXIT_FAILURE);
79  }
80 
81  seq = time(NULL);
82  batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
83 
84  if (batching) {
85  nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
86  mnl_nlmsg_batch_next(batch);
87  }
88 
89  flowtable_seq = seq;
90  nlh = nftnl_flowtable_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
91  NFT_MSG_NEWFLOWTABLE, family,
92  NLM_F_CREATE|NLM_F_ACK, seq++);
93  nftnl_flowtable_nlmsg_build_payload(nlh, t);
94  nftnl_flowtable_free(t);
95  mnl_nlmsg_batch_next(batch);
96 
97  if (batching) {
98  nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
99  mnl_nlmsg_batch_next(batch);
100  }
101 
102  nl = mnl_socket_open(NETLINK_NETFILTER);
103  if (nl == NULL) {
104  perror("mnl_socket_open");
105  exit(EXIT_FAILURE);
106  }
107 
108  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
109  perror("mnl_socket_bind");
110  exit(EXIT_FAILURE);
111  }
112  portid = mnl_socket_get_portid(nl);
113 
114  if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
115  mnl_nlmsg_batch_size(batch)) < 0) {
116  perror("mnl_socket_send");
117  exit(EXIT_FAILURE);
118  }
119 
120  mnl_nlmsg_batch_stop(batch);
121 
122  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
123  while (ret > 0) {
124  ret = mnl_cb_run(buf, ret, flowtable_seq, portid, NULL, NULL);
125  if (ret <= 0)
126  break;
127  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
128  }
129  if (ret == -1) {
130  perror("error");
131  exit(EXIT_FAILURE);
132  }
133  mnl_socket_close(nl);
134 
135  return EXIT_SUCCESS;
136 }