libnftnl  1.0.5
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_CGROUP + 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, 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 };
161 
162 static const char *meta_key2str(uint8_t key)
163 {
164  if (key < NFT_META_MAX)
165  return meta_key2str_array[key];
166 
167  return "unknown";
168 }
169 
170 static inline int str2meta_key(const char *str)
171 {
172  int i;
173 
174  for (i = 0; i < NFT_META_MAX; i++) {
175  if (strcmp(str, meta_key2str_array[i]) == 0)
176  return i;
177  }
178 
179  errno = EINVAL;
180  return -1;
181 }
182 
183 static int nftnl_expr_meta_json_parse(struct nftnl_expr *e, json_t *root,
184  struct nftnl_parse_err *err)
185 {
186 #ifdef JSON_PARSING
187  const char *key_str;
188  uint32_t reg;
189  int key;
190 
191  key_str = nftnl_jansson_parse_str(root, "key", err);
192  if (key_str != NULL) {
193  key = str2meta_key(key_str);
194  if (key >= 0)
195  nftnl_expr_set_u32(e, NFTNL_EXPR_META_KEY, key);
196  }
197 
198  if (nftnl_jansson_node_exist(root, "dreg")) {
199  if (nftnl_jansson_parse_reg(root, "dreg", NFTNL_TYPE_U32, &reg,
200  err) == 0)
201  nftnl_expr_set_u32(e, NFTNL_EXPR_META_DREG, reg);
202  }
203 
204  if (nftnl_jansson_node_exist(root, "sreg")) {
205  if (nftnl_jansson_parse_reg(root, "sreg", NFTNL_TYPE_U32, &reg,
206  err) == 0)
207  nftnl_expr_set_u32(e, NFTNL_EXPR_META_SREG, reg);
208  }
209 
210  return 0;
211 #else
212  errno = EOPNOTSUPP;
213  return -1;
214 #endif
215 }
216 
217 
218 static int nftnl_expr_meta_xml_parse(struct nftnl_expr *e, mxml_node_t *tree,
219  struct nftnl_parse_err *err)
220 {
221 #ifdef XML_PARSING
222  const char *key_str;
223  int key;
224  uint32_t dreg, sreg;
225 
226  key_str = nftnl_mxml_str_parse(tree, "key", MXML_DESCEND_FIRST,
227  NFTNL_XML_MAND, err);
228  if (key_str != NULL) {
229  key = str2meta_key(key_str);
230  if (key >= 0)
231  nftnl_expr_set_u32(e, NFTNL_EXPR_META_KEY, key);
232  }
233 
234  if (nftnl_mxml_reg_parse(tree, "dreg", &dreg, MXML_DESCEND_FIRST,
235  NFTNL_XML_OPT, err) == 0)
236  nftnl_expr_set_u32(e, NFTNL_EXPR_META_DREG, dreg);
237 
238  if (nftnl_mxml_reg_parse(tree, "sreg", &sreg, MXML_DESCEND_FIRST,
239  NFTNL_XML_OPT, err) == 0)
240  nftnl_expr_set_u32(e, NFTNL_EXPR_META_SREG, sreg);
241 
242  return 0;
243 #else
244  errno = EOPNOTSUPP;
245  return -1;
246 #endif
247 }
248 
249 static int
250 nftnl_expr_meta_snprintf_default(char *buf, size_t len,
251  struct nftnl_expr *e)
252 {
253  struct nftnl_expr_meta *meta = nftnl_expr_data(e);
254 
255  if (e->flags & (1 << NFTNL_EXPR_META_SREG)) {
256  return snprintf(buf, len, "set %s with reg %u ",
257  meta_key2str(meta->key), meta->sreg);
258  }
259  if (e->flags & (1 << NFTNL_EXPR_META_DREG)) {
260  return snprintf(buf, len, "load %s => reg %u ",
261  meta_key2str(meta->key), meta->dreg);
262  }
263  return 0;
264 }
265 
266 static int nftnl_expr_meta_export(char *buf, size_t size,
267  struct nftnl_expr *e, int type)
268 {
269  struct nftnl_expr_meta *meta = nftnl_expr_data(e);
270  NFTNL_BUF_INIT(b, buf, size);
271 
272  if (e->flags & (1 << NFTNL_EXPR_META_DREG))
273  nftnl_buf_u32(&b, type, meta->dreg, DREG);
274  if (e->flags & (1 << NFTNL_EXPR_META_KEY))
275  nftnl_buf_str(&b, type, meta_key2str(meta->key), KEY);
276  if (e->flags & (1 << NFTNL_EXPR_META_SREG))
277  nftnl_buf_u32(&b, type, meta->sreg, SREG);
278 
279  return nftnl_buf_done(&b);
280 }
281 
282 static int
283 nftnl_expr_meta_snprintf(char *buf, size_t len, uint32_t type,
284  uint32_t flags, struct nftnl_expr *e)
285 {
286  switch (type) {
287  case NFTNL_OUTPUT_DEFAULT:
288  return nftnl_expr_meta_snprintf_default(buf, len, e);
289  case NFTNL_OUTPUT_XML:
290  case NFTNL_OUTPUT_JSON:
291  return nftnl_expr_meta_export(buf, len, e, type);
292  default:
293  break;
294  }
295  return -1;
296 }
297 
298 struct expr_ops expr_ops_meta = {
299  .name = "meta",
300  .alloc_len = sizeof(struct nftnl_expr_meta),
301  .max_attr = NFTA_META_MAX,
302  .set = nftnl_expr_meta_set,
303  .get = nftnl_expr_meta_get,
304  .parse = nftnl_expr_meta_parse,
305  .build = nftnl_expr_meta_build,
306  .snprintf = nftnl_expr_meta_snprintf,
307  .xml_parse = nftnl_expr_meta_xml_parse,
308  .json_parse = nftnl_expr_meta_json_parse,
309 };