libnftnl  1.0.6
meta.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
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 <stdint.h>
14 #include <string.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 #ifndef NFT_META_MAX
25 #define NFT_META_MAX (NFT_META_PRANDOM + 1)
26 #endif
27 
29  enum nft_meta_keys key;
30  enum nft_registers dreg;
31  enum nft_registers sreg;
32 };
33 
34 static int
35 nftnl_expr_meta_set(struct nftnl_expr *e, uint16_t type,
36  const void *data, uint32_t data_len)
37 {
38  struct nftnl_expr_meta *meta = nftnl_expr_data(e);
39 
40  switch(type) {
41  case NFTNL_EXPR_META_KEY:
42  meta->key = *((uint32_t *)data);
43  break;
44  case NFTNL_EXPR_META_DREG:
45  meta->dreg = *((uint32_t *)data);
46  break;
47  case NFTNL_EXPR_META_SREG:
48  meta->sreg = *((uint32_t *)data);
49  break;
50  default:
51  return -1;
52  }
53  return 0;
54 }
55 
56 static const void *
57 nftnl_expr_meta_get(const struct nftnl_expr *e, uint16_t type,
58  uint32_t *data_len)
59 {
60  struct nftnl_expr_meta *meta = nftnl_expr_data(e);
61 
62  switch(type) {
63  case NFTNL_EXPR_META_KEY:
64  *data_len = sizeof(meta->key);
65  return &meta->key;
66  case NFTNL_EXPR_META_DREG:
67  *data_len = sizeof(meta->dreg);
68  return &meta->dreg;
69  case NFTNL_EXPR_META_SREG:
70  *data_len = sizeof(meta->sreg);
71  return &meta->sreg;
72  }
73  return NULL;
74 }
75 
76 static int nftnl_expr_meta_cb(const struct nlattr *attr, void *data)
77 {
78  const struct nlattr **tb = data;
79  int type = mnl_attr_get_type(attr);
80 
81  if (mnl_attr_type_valid(attr, NFTA_META_MAX) < 0)
82  return MNL_CB_OK;
83 
84  switch(type) {
85  case NFTA_META_KEY:
86  case NFTA_META_DREG:
87  case NFTA_META_SREG:
88  if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
89  abi_breakage();
90  break;
91  }
92 
93  tb[type] = attr;
94  return MNL_CB_OK;
95 }
96 
97 static void
98 nftnl_expr_meta_build(struct nlmsghdr *nlh, const struct nftnl_expr *e)
99 {
100  struct nftnl_expr_meta *meta = nftnl_expr_data(e);
101 
102  if (e->flags & (1 << NFTNL_EXPR_META_KEY))
103  mnl_attr_put_u32(nlh, NFTA_META_KEY, htonl(meta->key));
104  if (e->flags & (1 << NFTNL_EXPR_META_DREG))
105  mnl_attr_put_u32(nlh, NFTA_META_DREG, htonl(meta->dreg));
106  if (e->flags & (1 << NFTNL_EXPR_META_SREG))
107  mnl_attr_put_u32(nlh, NFTA_META_SREG, htonl(meta->sreg));
108 }
109 
110 static int
111 nftnl_expr_meta_parse(struct nftnl_expr *e, struct nlattr *attr)
112 {
113  struct nftnl_expr_meta *meta = nftnl_expr_data(e);
114  struct nlattr *tb[NFTA_META_MAX+1] = {};
115 
116  if (mnl_attr_parse_nested(attr, nftnl_expr_meta_cb, tb) < 0)
117  return -1;
118 
119  if (tb[NFTA_META_KEY]) {
120  meta->key = ntohl(mnl_attr_get_u32(tb[NFTA_META_KEY]));
121  e->flags |= (1 << NFTNL_EXPR_META_KEY);
122  }
123  if (tb[NFTA_META_DREG]) {
124  meta->dreg = ntohl(mnl_attr_get_u32(tb[NFTA_META_DREG]));
125  e->flags |= (1 << NFTNL_EXPR_META_DREG);
126  }
127  if (tb[NFTA_META_SREG]) {
128  meta->sreg = ntohl(mnl_attr_get_u32(tb[NFTA_META_SREG]));
129  e->flags |= (1 << NFTNL_EXPR_META_SREG);
130  }
131 
132  return 0;
133 }
134 
135 static const char *meta_key2str_array[NFT_META_MAX] = {
136  [NFT_META_LEN] = "len",
137  [NFT_META_PROTOCOL] = "protocol",
138  [NFT_META_NFPROTO] = "nfproto",
139  [NFT_META_L4PROTO] = "l4proto",
140  [NFT_META_PRIORITY] = "priority",
141  [NFT_META_MARK] = "mark",
142  [NFT_META_IIF] = "iif",
143  [NFT_META_OIF] = "oif",
144  [NFT_META_IIFNAME] = "iifname",
145  [NFT_META_OIFNAME] = "oifname",
146  [NFT_META_IIFTYPE] = "iiftype",
147  [NFT_META_OIFTYPE] = "oiftype",
148  [NFT_META_SKUID] = "skuid",
149  [NFT_META_SKGID] = "skgid",
150  [NFT_META_NFTRACE] = "nftrace",
151  [NFT_META_RTCLASSID] = "rtclassid",
152  [NFT_META_SECMARK] = "secmark",
153  [NFT_META_BRI_IIFNAME] = "bri_iifname",
154  [NFT_META_BRI_OIFNAME] = "bri_oifname",
155  [NFT_META_PKTTYPE] = "pkttype",
156  [NFT_META_CPU] = "cpu",
157  [NFT_META_IIFGROUP] = "iifgroup",
158  [NFT_META_OIFGROUP] = "oifgroup",
159  [NFT_META_CGROUP] = "cgroup",
160  [NFT_META_PRANDOM] = "prandom",
161 };
162 
163 static const char *meta_key2str(uint8_t key)
164 {
165  if (key < NFT_META_MAX)
166  return meta_key2str_array[key];
167 
168  return "unknown";
169 }
170 
171 static inline int str2meta_key(const char *str)
172 {
173  int i;
174 
175  for (i = 0; i < NFT_META_MAX; i++) {
176  if (strcmp(str, meta_key2str_array[i]) == 0)
177  return i;
178  }
179 
180  errno = EINVAL;
181  return -1;
182 }
183 
184 static int nftnl_expr_meta_json_parse(struct nftnl_expr *e, json_t *root,
185  struct nftnl_parse_err *err)
186 {
187 #ifdef JSON_PARSING
188  const char *key_str;
189  uint32_t reg;
190  int key;
191 
192  key_str = nftnl_jansson_parse_str(root, "key", err);
193  if (key_str != NULL) {
194  key = str2meta_key(key_str);
195  if (key >= 0)
196  nftnl_expr_set_u32(e, NFTNL_EXPR_META_KEY, key);
197  }
198 
199  if (nftnl_jansson_node_exist(root, "dreg")) {
200  if (nftnl_jansson_parse_reg(root, "dreg", NFTNL_TYPE_U32, &reg,
201  err) == 0)
202  nftnl_expr_set_u32(e, NFTNL_EXPR_META_DREG, reg);
203  }
204 
205  if (nftnl_jansson_node_exist(root, "sreg")) {
206  if (nftnl_jansson_parse_reg(root, "sreg", NFTNL_TYPE_U32, &reg,
207  err) == 0)
208  nftnl_expr_set_u32(e, NFTNL_EXPR_META_SREG, reg);
209  }
210 
211  return 0;
212 #else
213  errno = EOPNOTSUPP;
214  return -1;
215 #endif
216 }
217 
218 
219 static int nftnl_expr_meta_xml_parse(struct nftnl_expr *e, mxml_node_t *tree,
220  struct nftnl_parse_err *err)
221 {
222 #ifdef XML_PARSING
223  const char *key_str;
224  int key;
225  uint32_t dreg, sreg;
226 
227  key_str = nftnl_mxml_str_parse(tree, "key", MXML_DESCEND_FIRST,
228  NFTNL_XML_MAND, err);
229  if (key_str != NULL) {
230  key = str2meta_key(key_str);
231  if (key >= 0)
232  nftnl_expr_set_u32(e, NFTNL_EXPR_META_KEY, key);
233  }
234 
235  if (nftnl_mxml_reg_parse(tree, "dreg", &dreg, MXML_DESCEND_FIRST,
236  NFTNL_XML_OPT, err) == 0)
237  nftnl_expr_set_u32(e, NFTNL_EXPR_META_DREG, dreg);
238 
239  if (nftnl_mxml_reg_parse(tree, "sreg", &sreg, MXML_DESCEND_FIRST,
240  NFTNL_XML_OPT, err) == 0)
241  nftnl_expr_set_u32(e, NFTNL_EXPR_META_SREG, sreg);
242 
243  return 0;
244 #else
245  errno = EOPNOTSUPP;
246  return -1;
247 #endif
248 }
249 
250 static int
251 nftnl_expr_meta_snprintf_default(char *buf, size_t len,
252  const struct nftnl_expr *e)
253 {
254  struct nftnl_expr_meta *meta = nftnl_expr_data(e);
255 
256  if (e->flags & (1 << NFTNL_EXPR_META_SREG)) {
257  return snprintf(buf, len, "set %s with reg %u ",
258  meta_key2str(meta->key), meta->sreg);
259  }
260  if (e->flags & (1 << NFTNL_EXPR_META_DREG)) {
261  return snprintf(buf, len, "load %s => reg %u ",
262  meta_key2str(meta->key), meta->dreg);
263  }
264  return 0;
265 }
266 
267 static int nftnl_expr_meta_export(char *buf, size_t size,
268  const struct nftnl_expr *e, int type)
269 {
270  struct nftnl_expr_meta *meta = nftnl_expr_data(e);
271  NFTNL_BUF_INIT(b, buf, size);
272 
273  if (e->flags & (1 << NFTNL_EXPR_META_DREG))
274  nftnl_buf_u32(&b, type, meta->dreg, DREG);
275  if (e->flags & (1 << NFTNL_EXPR_META_KEY))
276  nftnl_buf_str(&b, type, meta_key2str(meta->key), KEY);
277  if (e->flags & (1 << NFTNL_EXPR_META_SREG))
278  nftnl_buf_u32(&b, type, meta->sreg, SREG);
279 
280  return nftnl_buf_done(&b);
281 }
282 
283 static int
284 nftnl_expr_meta_snprintf(char *buf, size_t len, uint32_t type,
285  uint32_t flags, const struct nftnl_expr *e)
286 {
287  switch (type) {
288  case NFTNL_OUTPUT_DEFAULT:
289  return nftnl_expr_meta_snprintf_default(buf, len, e);
290  case NFTNL_OUTPUT_XML:
291  case NFTNL_OUTPUT_JSON:
292  return nftnl_expr_meta_export(buf, len, e, type);
293  default:
294  break;
295  }
296  return -1;
297 }
298 
299 struct expr_ops expr_ops_meta = {
300  .name = "meta",
301  .alloc_len = sizeof(struct nftnl_expr_meta),
302  .max_attr = NFTA_META_MAX,
303  .set = nftnl_expr_meta_set,
304  .get = nftnl_expr_meta_get,
305  .parse = nftnl_expr_meta_parse,
306  .build = nftnl_expr_meta_build,
307  .snprintf = nftnl_expr_meta_snprintf,
308  .xml_parse = nftnl_expr_meta_xml_parse,
309  .json_parse = nftnl_expr_meta_json_parse,
310 };