libnftnl  1.2.5
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  [NFT_CT_ID] = "id",
176 };
177 
178 static const char *ctkey2str(uint32_t ctkey)
179 {
180  if (ctkey >= NFT_CT_MAX)
181  return "unknown";
182 
183  return ctkey2str_array[ctkey];
184 }
185 
186 static inline int str2ctkey(const char *ctkey)
187 {
188  int i;
189 
190  for (i = 0; i < NFT_CT_MAX; i++) {
191  if (strcmp(ctkey2str_array[i], ctkey) == 0)
192  return i;
193  }
194 
195  return -1;
196 }
197 
198 static const char *ctdir2str(uint8_t ctdir)
199 {
200  switch (ctdir) {
201  case IP_CT_DIR_ORIGINAL:
202  return "original";
203  case IP_CT_DIR_REPLY:
204  return "reply";
205  default:
206  return "unknown";
207  }
208 }
209 
210 static inline int str2ctdir(const char *str, uint8_t *ctdir)
211 {
212  if (strcmp(str, "original") == 0) {
213  *ctdir = IP_CT_DIR_ORIGINAL;
214  return 0;
215  }
216 
217  if (strcmp(str, "reply") == 0) {
218  *ctdir = IP_CT_DIR_REPLY;
219  return 0;
220  }
221 
222  return -1;
223 }
224 
225 static int
226 nftnl_expr_ct_snprintf(char *buf, size_t remain,
227  uint32_t flags, const struct nftnl_expr *e)
228 {
229  struct nftnl_expr_ct *ct = nftnl_expr_data(e);
230  int ret, offset = 0;
231 
232  if (e->flags & (1 << NFTNL_EXPR_CT_SREG)) {
233  ret = snprintf(buf, remain, "set %s with reg %u ",
234  ctkey2str(ct->key), ct->sreg);
235  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
236  }
237 
238  if (e->flags & (1 << NFTNL_EXPR_CT_DREG)) {
239  ret = snprintf(buf, remain, "load %s => reg %u ",
240  ctkey2str(ct->key), ct->dreg);
241  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
242  }
243 
244  if (nftnl_expr_is_set(e, NFTNL_EXPR_CT_DIR)) {
245  ret = snprintf(buf + offset, remain, ", dir %s ",
246  ctdir2str(ct->dir));
247  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
248  }
249 
250  return offset;
251 }
252 
253 struct expr_ops expr_ops_ct = {
254  .name = "ct",
255  .alloc_len = sizeof(struct nftnl_expr_ct),
256  .max_attr = NFTA_CT_MAX,
257  .set = nftnl_expr_ct_set,
258  .get = nftnl_expr_ct_get,
259  .parse = nftnl_expr_ct_parse,
260  .build = nftnl_expr_ct_build,
261  .output = nftnl_expr_ct_snprintf,
262 };