libnftnl  1.1.7
nft-ct-timeout-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 <obj.h>
19 #include <libmnl/libmnl.h>
20 #include <libnftnl/object.h>
21 
22 static struct nftnl_obj *obj_add_parse(int argc, char *argv[])
23 {
24  size_t timeout_array_size;
25  struct nftnl_obj *t;
26  uint32_t *timeout;
27  uint16_t family;
28  uint8_t l4proto;
29 
30  if (strcmp(argv[1], "ip") == 0)
31  family = NFPROTO_IPV4;
32  else if (strcmp(argv[1], "ip6") == 0)
33  family = NFPROTO_IPV6;
34  else if (strcmp(argv[1], "bridge") == 0)
35  family = NFPROTO_BRIDGE;
36  else if (strcmp(argv[1], "arp") == 0)
37  family = NFPROTO_ARP;
38  else {
39  fprintf(stderr, "Unknown family: ip, ip6, bridge, arp\n");
40  return NULL;
41  }
42 
43  if (strcmp(argv[4], "udp") == 0)
44  l4proto = IPPROTO_UDP;
45  else if (strcmp(argv[4], "tcp") == 0)
46  l4proto = IPPROTO_TCP;
47  else {
48  fprintf(stderr, "Unknown layer 4 protocol\n");
49  return NULL;
50  }
51 
52  t = nftnl_obj_alloc();
53  if (t == NULL) {
54  perror("OOM");
55  return NULL;
56  }
57 
58  timeout_array_size = sizeof(uint32_t) * (NFTNL_CTTIMEOUT_TCP_MAX);
59  timeout = calloc(1, timeout_array_size);
60  if (timeout == NULL) {
61  perror("OOM");
62  return NULL;
63  }
64 
65  timeout[NFTNL_CTTIMEOUT_TCP_ESTABLISHED] = 111;
66  timeout[NFTNL_CTTIMEOUT_TCP_CLOSE] = 16;
67  timeout[NFTNL_CTTIMEOUT_TCP_CLOSE_WAIT] = 14;
68  nftnl_obj_set_u32(t, NFTNL_OBJ_FAMILY, family);
69  nftnl_obj_set_u32(t, NFTNL_OBJ_TYPE, NFT_OBJECT_CT_TIMEOUT);
70  nftnl_obj_set_str(t, NFTNL_OBJ_TABLE, argv[2]);
71  nftnl_obj_set_str(t, NFTNL_OBJ_NAME, argv[3]);
72  nftnl_obj_set_u8(t, NFTNL_OBJ_CT_TIMEOUT_L4PROTO, l4proto);
73  nftnl_obj_set_u16(t, NFTNL_OBJ_CT_TIMEOUT_L3PROTO, NFPROTO_IPV4);
74  nftnl_obj_set_data(t, NFTNL_OBJ_CT_TIMEOUT_ARRAY,
75  timeout, timeout_array_size);
76  return t;
77 
78 }
79 
80 int main(int argc, char *argv[])
81 {
82  struct mnl_socket *nl;
83  char buf[MNL_SOCKET_BUFFER_SIZE];
84  struct nlmsghdr *nlh;
85  uint32_t portid, seq, obj_seq, family;
86  struct nftnl_obj *t;
87  struct mnl_nlmsg_batch *batch;
88  int ret;
89 
90  if (argc != 5) {
91  fprintf(stderr, "%s <family> <table> <name> <protocol> \n", argv[0]);
92  exit(EXIT_FAILURE);
93  }
94 
95  t = obj_add_parse(argc, argv);
96  if (t == NULL) {
97  exit(EXIT_FAILURE);
98  }
99 
100  seq = time(NULL);
101  batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
102 
103  nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
104  mnl_nlmsg_batch_next(batch);
105 
106  obj_seq = seq;
107  family = nftnl_obj_get_u32(t, NFTNL_OBJ_FAMILY);
108  nlh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
109  NFT_MSG_NEWOBJ, family, NLM_F_ACK | NLM_F_CREATE, seq++);
110  nftnl_obj_nlmsg_build_payload(nlh, t);
111  nftnl_obj_free(t);
112  mnl_nlmsg_batch_next(batch);
113 
114  nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
115  mnl_nlmsg_batch_next(batch);
116 
117  nl = mnl_socket_open(NETLINK_NETFILTER);
118  if (nl == NULL) {
119  perror("mnl_socket_open");
120  exit(EXIT_FAILURE);
121  }
122 
123  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
124  perror("mnl_socket_bind");
125  exit(EXIT_FAILURE);
126  }
127  portid = mnl_socket_get_portid(nl);
128 
129  if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
130  mnl_nlmsg_batch_size(batch)) < 0) {
131  perror("mnl_socket_send");
132  exit(EXIT_FAILURE);
133  }
134 
135  mnl_nlmsg_batch_stop(batch);
136 
137  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
138  while (ret > 0) {
139  ret = mnl_cb_run(buf, ret, obj_seq, portid, NULL, NULL);
140  if (ret <= 0)
141  break;
142  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
143  }
144  if (ret == -1) {
145  perror("error");
146  exit(EXIT_FAILURE);
147  }
148  mnl_socket_close(nl);
149 
150  return EXIT_SUCCESS;
151 }