libnftnl  1.1.4
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(t, NFTNL_OBJ_CT_TIMEOUT_ARRAY, timeout);
75  return t;
76 
77 }
78 
79 int main(int argc, char *argv[])
80 {
81  struct mnl_socket *nl;
82  char buf[MNL_SOCKET_BUFFER_SIZE];
83  struct nlmsghdr *nlh;
84  uint32_t portid, seq, obj_seq, family;
85  struct nftnl_obj *t;
86  struct mnl_nlmsg_batch *batch;
87  int ret;
88 
89  if (argc != 5) {
90  fprintf(stderr, "%s <family> <table> <name> <protocol> \n", argv[0]);
91  exit(EXIT_FAILURE);
92  }
93 
94  t = obj_add_parse(argc, argv);
95  if (t == NULL) {
96  exit(EXIT_FAILURE);
97  }
98 
99  seq = time(NULL);
100  batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
101 
102  nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
103  mnl_nlmsg_batch_next(batch);
104 
105  obj_seq = seq;
106  family = nftnl_obj_get_u32(t, NFTNL_OBJ_FAMILY);
107  nlh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
108  NFT_MSG_NEWOBJ, family, NLM_F_ACK | NLM_F_CREATE, seq++);
109  nftnl_obj_nlmsg_build_payload(nlh, t);
110  nftnl_obj_free(t);
111  mnl_nlmsg_batch_next(batch);
112 
113  nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
114  mnl_nlmsg_batch_next(batch);
115 
116  nl = mnl_socket_open(NETLINK_NETFILTER);
117  if (nl == NULL) {
118  perror("mnl_socket_open");
119  exit(EXIT_FAILURE);
120  }
121 
122  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
123  perror("mnl_socket_bind");
124  exit(EXIT_FAILURE);
125  }
126  portid = mnl_socket_get_portid(nl);
127 
128  if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
129  mnl_nlmsg_batch_size(batch)) < 0) {
130  perror("mnl_socket_send");
131  exit(EXIT_FAILURE);
132  }
133 
134  mnl_nlmsg_batch_stop(batch);
135 
136  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
137  while (ret > 0) {
138  ret = mnl_cb_run(buf, ret, obj_seq, portid, NULL, NULL);
139  if (ret <= 0)
140  break;
141  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
142  }
143  if (ret == -1) {
144  perror("error");
145  exit(EXIT_FAILURE);
146  }
147  mnl_socket_close(nl);
148 
149  return EXIT_SUCCESS;
150 }