libnftnl  1.1.7
nft-rule-ct-timeout-add.c
1 /*
2  * (C) 2012 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  * This software has been sponsored by Sophos Astaro <http://www.sophos.com>
10  */
11 
12 #include <stdlib.h>
13 #include <time.h>
14 #include <string.h>
15 #include <stddef.h> /* for offsetof */
16 #include <netinet/in.h>
17 #include <netinet/ip.h>
18 #include <netinet/tcp.h>
19 #include <arpa/inet.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <errno.h>
23 
24 #include <linux/netfilter.h>
25 #include <linux/netfilter/nfnetlink.h>
26 #include <linux/netfilter/nf_tables.h>
27 
28 #include <libmnl/libmnl.h>
29 #include <libnftnl/rule.h>
30 #include <libnftnl/expr.h>
31 
32 static void add_ct_timeout(struct nftnl_rule *r, const char *obj_name)
33 {
34  struct nftnl_expr *e;
35 
36  e = nftnl_expr_alloc("objref");
37  if (e == NULL) {
38  perror("expr objref oom");
39  exit(EXIT_FAILURE);
40  }
41  nftnl_expr_set_str(e, NFTNL_EXPR_OBJREF_IMM_NAME, obj_name);
42  nftnl_expr_set_u32(e, NFTNL_EXPR_OBJREF_IMM_TYPE, NFT_OBJECT_CT_TIMEOUT);
43 
44  nftnl_rule_add_expr(r, e);
45 }
46 
47 static struct nftnl_rule *setup_rule(uint8_t family, const char *table,
48  const char *chain, const char *handle, const char *obj_name)
49 {
50  struct nftnl_rule *r = NULL;
51  uint64_t handle_num;
52 
53  r = nftnl_rule_alloc();
54  if (r == NULL) {
55  perror("OOM");
56  exit(EXIT_FAILURE);
57  }
58 
59  nftnl_rule_set_str(r, NFTNL_RULE_TABLE, table);
60  nftnl_rule_set_str(r, NFTNL_RULE_CHAIN, chain);
61  nftnl_rule_set_u32(r, NFTNL_RULE_FAMILY, family);
62 
63  if (handle != NULL) {
64  handle_num = atoll(handle);
65  nftnl_rule_set_u64(r, NFTNL_RULE_POSITION, handle_num);
66  }
67 
68  add_ct_timeout(r, obj_name);
69 
70  return r;
71 }
72 
73 int main(int argc, char *argv[])
74 {
75  struct mnl_socket *nl;
76  struct nftnl_rule *r;
77  struct nlmsghdr *nlh;
78  struct mnl_nlmsg_batch *batch;
79  uint8_t family;
80  char buf[MNL_SOCKET_BUFFER_SIZE];
81  uint32_t seq = time(NULL);
82  int ret;
83 
84  if (argc < 5 || argc > 6) {
85  fprintf(stderr, "Usage: %s <family> <table> <chain> <name>\n", argv[0]);
86  exit(EXIT_FAILURE);
87  }
88  if (strcmp(argv[1], "ip") == 0)
89  family = NFPROTO_IPV4;
90  else if (strcmp(argv[1], "ip6") == 0)
91  family = NFPROTO_IPV6;
92  else {
93  fprintf(stderr, "Unknown family: ip, ip6\n");
94  exit(EXIT_FAILURE);
95  }
96 
97  if (argc != 6)
98  r = setup_rule(family, argv[2], argv[3], NULL, argv[4]);
99  else
100  r = setup_rule(family, argv[2], argv[3], argv[4], argv[5]);
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 
113  batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
114 
115  nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
116  mnl_nlmsg_batch_next(batch);
117 
118  nlh = nftnl_rule_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
119  NFT_MSG_NEWRULE,
120  nftnl_rule_get_u32(r, NFTNL_RULE_FAMILY),
121  NLM_F_APPEND|NLM_F_CREATE|NLM_F_ACK, seq++);
122 
123  nftnl_rule_nlmsg_build_payload(nlh, r);
124  nftnl_rule_free(r);
125  mnl_nlmsg_batch_next(batch);
126 
127  nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
128  mnl_nlmsg_batch_next(batch);
129 
130  ret = mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
131  mnl_nlmsg_batch_size(batch));
132  if (ret == -1) {
133  perror("mnl_socket_sendto");
134  exit(EXIT_FAILURE);
135  }
136 
137  mnl_nlmsg_batch_stop(batch);
138 
139  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
140  if (ret == -1) {
141  perror("mnl_socket_recvfrom");
142  exit(EXIT_FAILURE);
143  }
144 
145  ret = mnl_cb_run(buf, ret, 0, mnl_socket_get_portid(nl), NULL, NULL);
146  if (ret < 0) {
147  perror("mnl_cb_run");
148  exit(EXIT_FAILURE);
149  }
150 
151  mnl_socket_close(nl);
152 
153  return EXIT_SUCCESS;
154 }