libnftnl  1.1.2
ct.c
1 /*
2  * (C) 2012-2013 by Pablo Neira Ayuso <pablo@netfilter.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published
6  * by the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This code has been sponsored by Sophos Astaro <http://www.sophos.com>
10  */
11 
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdint.h>
15 #include <arpa/inet.h>
16 #include <errno.h>
17 #include <linux/netfilter/nf_tables.h>
18 
19 #include "internal.h"
20 #include <libmnl/libmnl.h>
21 #include <libnftnl/expr.h>
22 #include <libnftnl/rule.h>
23 
24 struct nftnl_expr_ct {
25  enum nft_ct_keys key;
26  enum nft_registers dreg;
27  enum nft_registers sreg;
28  uint8_t dir;
29 };
30 
31 #define IP_CT_DIR_ORIGINAL 0
32 #define IP_CT_DIR_REPLY 1
33 
34 static int
35 nftnl_expr_ct_set(struct nftnl_expr *e, uint16_t type,
36  const void *data, uint32_t data_len)
37 {
38  struct nftnl_expr_ct *ct = nftnl_expr_data(e);
39 
40  switch(type) {
41  case NFTNL_EXPR_CT_KEY:
42  memcpy(&ct->key, data, sizeof(ct->key));
43  break;
44  case NFTNL_EXPR_CT_DIR:
45  memcpy(&ct->dir, data, sizeof(ct->dir));
46  break;
47  case NFTNL_EXPR_CT_DREG:
48  memcpy(&ct->dreg, data, sizeof(ct->dreg));
49  break;
50  case NFTNL_EXPR_CT_SREG:
51  memcpy(&ct->sreg, data, sizeof(ct->sreg));
52  break;
53  default:
54  return -1;
55  }
56  return 0;
57 }
58 
59 static const void *
60 nftnl_expr_ct_get(const struct nftnl_expr *e, uint16_t type,
61  uint32_t *data_len)
62 {
63  struct nftnl_expr_ct *ct = nftnl_expr_data(e);
64 
65  switch(type) {
66  case NFTNL_EXPR_CT_KEY:
67  *data_len = sizeof(ct->key);
68  return &ct->key;
69  case NFTNL_EXPR_CT_DIR:
70  *data_len = sizeof(ct->dir);
71  return &ct->dir;
72  case NFTNL_EXPR_CT_DREG:
73  *data_len = sizeof(ct->dreg);
74  return &ct->dreg;
75  case NFTNL_EXPR_CT_SREG:
76  *data_len = sizeof(ct->sreg);
77  return &ct->sreg;
78  }
79  return NULL;
80 }
81 
82 static int nftnl_expr_ct_cb(const struct nlattr *attr, void *data)
83 {
84  const struct nlattr **tb = data;
85  int type = mnl_attr_get_type(attr);
86 
87  if (mnl_attr_type_valid(attr, NFTA_CT_MAX) < 0)
88  return MNL_CB_OK;
89 
90  switch(type) {
91  case NFTA_CT_KEY:
92  case NFTA_CT_DREG:
93  case NFTA_CT_SREG:
94  if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
95  abi_breakage();
96  break;
97  case NFTA_CT_DIRECTION:
98  if (mnl_attr_validate(attr, MNL_TYPE_U8) < 0)
99  abi_breakage();
100  break;
101  }
102 
103  tb[type] = attr;
104  return MNL_CB_OK;
105 }
106 
107 static void
108 nftnl_expr_ct_build(struct nlmsghdr *nlh, const struct nftnl_expr *e)
109 {
110  struct nftnl_expr_ct *ct = nftnl_expr_data(e);
111 
112  if (e->flags & (1 << NFTNL_EXPR_CT_KEY))
113  mnl_attr_put_u32(nlh, NFTA_CT_KEY, htonl(ct->key));
114  if (e->flags & (1 << NFTNL_EXPR_CT_DREG))
115  mnl_attr_put_u32(nlh, NFTA_CT_DREG, htonl(ct->dreg));
116  if (e->flags & (1 << NFTNL_EXPR_CT_DIR))
117  mnl_attr_put_u8(nlh, NFTA_CT_DIRECTION, ct->dir);
118  if (e->flags & (1 << NFTNL_EXPR_CT_SREG))
119  mnl_attr_put_u32(nlh, NFTA_CT_SREG, htonl(ct->sreg));
120 }
121 
122 static int
123 nftnl_expr_ct_parse(struct nftnl_expr *e, struct nlattr *attr)
124 {
125  struct nftnl_expr_ct *ct = nftnl_expr_data(e);
126  struct nlattr *tb[NFTA_CT_MAX+1] = {};
127 
128  if (mnl_attr_parse_nested(attr, nftnl_expr_ct_cb, tb) < 0)
129  return -1;
130 
131  if (tb[NFTA_CT_KEY]) {
132  ct->key = ntohl(mnl_attr_get_u32(tb[NFTA_CT_KEY]));
133  e->flags |= (1 << NFTNL_EXPR_CT_KEY);
134  }
135  if (tb[NFTA_CT_DREG]) {
136  ct->dreg = ntohl(mnl_attr_get_u32(tb[NFTA_CT_DREG]));
137  e->flags |= (1 << NFTNL_EXPR_CT_DREG);
138  }
139  if (tb[NFTA_CT_SREG]) {
140  ct->sreg = ntohl(mnl_attr_get_u32(tb[NFTA_CT_SREG]));
141  e->flags |= (1 << NFTNL_EXPR_CT_SREG);
142  }
143  if (tb[NFTA_CT_DIRECTION]) {
144  ct->dir = mnl_attr_get_u8(tb[NFTA_CT_DIRECTION]);
145  e->flags |= (1 << NFTNL_EXPR_CT_DIR);
146  }
147 
148  return 0;
149 }
150 
151 static const char *ctkey2str_array[NFT_CT_MAX + 1] = {
152  [NFT_CT_STATE] = "state",
153  [NFT_CT_DIRECTION] = "direction",
154  [NFT_CT_STATUS] = "status",
155  [NFT_CT_MARK] = "mark",
156  [NFT_CT_SECMARK] = "secmark",
157  [NFT_CT_EXPIRATION] = "expiration",
158  [NFT_CT_HELPER] = "helper",
159  [NFT_CT_L3PROTOCOL] = "l3protocol",
160  [NFT_CT_PROTOCOL] = "protocol",
161  [NFT_CT_SRC] = "src",
162  [NFT_CT_DST] = "dst",
163  [NFT_CT_PROTO_SRC] = "proto_src",
164  [NFT_CT_PROTO_DST] = "proto_dst",
165  [NFT_CT_LABELS] = "label",
166  [NFT_CT_PKTS] = "packets",
167  [NFT_CT_BYTES] = "bytes",
168  [NFT_CT_AVGPKT] = "avgpkt",
169  [NFT_CT_ZONE] = "zone",
170  [NFT_CT_EVENTMASK] = "event",
171  [NFT_CT_SRC_IP] = "src_ip",
172  [NFT_CT_DST_IP] = "dst_ip",
173  [NFT_CT_SRC_IP6] = "src_ip6",
174  [NFT_CT_DST_IP6] = "dst_ip6",
175 };
176 
177 static const char *ctkey2str(uint32_t ctkey)
178 {
179  if (ctkey >= NFT_CT_MAX)
180  return "unknown";
181 
182  return ctkey2str_array[ctkey];
183 }
184 
185 static inline int str2ctkey(const char *ctkey)
186 {
187  int i;
188 
189  for (i = 0; i < NFT_CT_MAX; i++) {
190  if (strcmp(ctkey2str_array[i], ctkey) == 0)
191  return i;
192  }
193 
194  return -1;
195 }
196 
197 static const char *ctdir2str(uint8_t ctdir)
198 {
199  switch (ctdir) {
200  case IP_CT_DIR_ORIGINAL:
201  return "original";
202  case IP_CT_DIR_REPLY:
203  return "reply";
204  default:
205  return "unknown";
206  }
207 }
208 
209 static inline int str2ctdir(const char *str, uint8_t *ctdir)
210 {
211  if (strcmp(str, "original") == 0) {
212  *ctdir = IP_CT_DIR_ORIGINAL;
213  return 0;
214  }
215 
216  if (strcmp(str, "reply") == 0) {
217  *ctdir = IP_CT_DIR_REPLY;
218  return 0;
219  }
220 
221  return -1;
222 }
223 
224 static int
225 nftnl_expr_ct_snprintf_default(char *buf, size_t size,
226  const struct nftnl_expr *e)
227 {
228  int ret, remain = size, offset = 0;
229  struct nftnl_expr_ct *ct = nftnl_expr_data(e);
230 
231  if (e->flags & (1 << NFTNL_EXPR_CT_SREG)) {
232  ret = snprintf(buf, size, "set %s with reg %u ",
233  ctkey2str(ct->key), ct->sreg);
234  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
235  }
236 
237  if (e->flags & (1 << NFTNL_EXPR_CT_DREG)) {
238  ret = snprintf(buf, remain, "load %s => reg %u ",
239  ctkey2str(ct->key), ct->dreg);
240  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
241  }
242 
243  if (nftnl_expr_is_set(e, NFTNL_EXPR_CT_DIR)) {
244  ret = snprintf(buf + offset, remain, ", dir %s ",
245  ctdir2str(ct->dir));
246  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
247  }
248 
249  return offset;
250 }
251 
252 static int
253 nftnl_expr_ct_snprintf(char *buf, size_t len, uint32_t type,
254  uint32_t flags, const struct nftnl_expr *e)
255 {
256  switch (type) {
257  case NFTNL_OUTPUT_DEFAULT:
258  return nftnl_expr_ct_snprintf_default(buf, len, e);
259  case NFTNL_OUTPUT_XML:
260  case NFTNL_OUTPUT_JSON:
261  default:
262  break;
263  }
264  return -1;
265 }
266 
267 struct expr_ops expr_ops_ct = {
268  .name = "ct",
269  .alloc_len = sizeof(struct nftnl_expr_ct),
270  .max_attr = NFTA_CT_MAX,
271  .set = nftnl_expr_ct_set,
272  .get = nftnl_expr_ct_get,
273  .parse = nftnl_expr_ct_parse,
274  .build = nftnl_expr_ct_build,
275  .snprintf = nftnl_expr_ct_snprintf,
276 };