libnftnl  1.2.4
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 if (strcmp(argv[1], "inet") == 0)
93  family = NFPROTO_INET;
94  else {
95  fprintf(stderr, "Unknown family: ip, ip6, inet\n");
96  exit(EXIT_FAILURE);
97  }
98 
99  if (argc != 6)
100  r = setup_rule(family, argv[2], argv[3], NULL, argv[4]);
101  else
102  r = setup_rule(family, argv[2], argv[3], argv[4], argv[5]);
103 
104  nl = mnl_socket_open(NETLINK_NETFILTER);
105  if (nl == NULL) {
106  perror("mnl_socket_open");
107  exit(EXIT_FAILURE);
108  }
109 
110  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
111  perror("mnl_socket_bind");
112  exit(EXIT_FAILURE);
113  }
114 
115  batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
116 
117  nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
118  mnl_nlmsg_batch_next(batch);
119 
120  nlh = nftnl_rule_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
121  NFT_MSG_NEWRULE,
122  nftnl_rule_get_u32(r, NFTNL_RULE_FAMILY),
123  NLM_F_APPEND|NLM_F_CREATE|NLM_F_ACK, seq++);
124 
125  nftnl_rule_nlmsg_build_payload(nlh, r);
126  nftnl_rule_free(r);
127  mnl_nlmsg_batch_next(batch);
128 
129  nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
130  mnl_nlmsg_batch_next(batch);
131 
132  ret = mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
133  mnl_nlmsg_batch_size(batch));
134  if (ret == -1) {
135  perror("mnl_socket_sendto");
136  exit(EXIT_FAILURE);
137  }
138 
139  mnl_nlmsg_batch_stop(batch);
140 
141  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
142  if (ret == -1) {
143  perror("mnl_socket_recvfrom");
144  exit(EXIT_FAILURE);
145  }
146 
147  ret = mnl_cb_run(buf, ret, 0, mnl_socket_get_portid(nl), NULL, NULL);
148  if (ret < 0) {
149  perror("mnl_cb_run");
150  exit(EXIT_FAILURE);
151  }
152 
153  mnl_socket_close(nl);
154 
155  return EXIT_SUCCESS;
156 }