libnftnl  1.0.8
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 static int
219 nftnl_expr_meta_snprintf_default(char *buf, size_t len,
220  const struct nftnl_expr *e)
221 {
222  struct nftnl_expr_meta *meta = nftnl_expr_data(e);
223 
224  if (e->flags & (1 << NFTNL_EXPR_META_SREG)) {
225  return snprintf(buf, len, "set %s with reg %u ",
226  meta_key2str(meta->key), meta->sreg);
227  }
228  if (e->flags & (1 << NFTNL_EXPR_META_DREG)) {
229  return snprintf(buf, len, "load %s => reg %u ",
230  meta_key2str(meta->key), meta->dreg);
231  }
232  return 0;
233 }
234 
235 static int nftnl_expr_meta_export(char *buf, size_t size,
236  const struct nftnl_expr *e, int type)
237 {
238  struct nftnl_expr_meta *meta = nftnl_expr_data(e);
239  NFTNL_BUF_INIT(b, buf, size);
240 
241  if (e->flags & (1 << NFTNL_EXPR_META_DREG))
242  nftnl_buf_u32(&b, type, meta->dreg, DREG);
243  if (e->flags & (1 << NFTNL_EXPR_META_KEY))
244  nftnl_buf_str(&b, type, meta_key2str(meta->key), KEY);
245  if (e->flags & (1 << NFTNL_EXPR_META_SREG))
246  nftnl_buf_u32(&b, type, meta->sreg, SREG);
247 
248  return nftnl_buf_done(&b);
249 }
250 
251 static int
252 nftnl_expr_meta_snprintf(char *buf, size_t len, uint32_t type,
253  uint32_t flags, const struct nftnl_expr *e)
254 {
255  switch (type) {
256  case NFTNL_OUTPUT_DEFAULT:
257  return nftnl_expr_meta_snprintf_default(buf, len, e);
258  case NFTNL_OUTPUT_XML:
259  case NFTNL_OUTPUT_JSON:
260  return nftnl_expr_meta_export(buf, len, e, type);
261  default:
262  break;
263  }
264  return -1;
265 }
266 
267 static bool nftnl_expr_meta_cmp(const struct nftnl_expr *e1,
268  const struct nftnl_expr *e2)
269 {
270  struct nftnl_expr_meta *m1 = nftnl_expr_data(e1);
271  struct nftnl_expr_meta *m2 = nftnl_expr_data(e2);
272  bool eq = true;
273 
274  if (e1->flags & (1 << NFTNL_EXPR_META_KEY))
275  eq &= (m1->key == m2->key);
276  if (e1->flags & (1 << NFTNL_EXPR_META_DREG))
277  eq &= (m1->dreg == m2->dreg);
278  if (e1->flags & (1 << NFTNL_EXPR_META_SREG))
279  eq &= (m1->sreg == m2->sreg);
280 
281  return eq;
282 }
283 
284 struct expr_ops expr_ops_meta = {
285  .name = "meta",
286  .alloc_len = sizeof(struct nftnl_expr_meta),
287  .max_attr = NFTA_META_MAX,
288  .cmp = nftnl_expr_meta_cmp,
289  .set = nftnl_expr_meta_set,
290  .get = nftnl_expr_meta_get,
291  .parse = nftnl_expr_meta_parse,
292  .build = nftnl_expr_meta_build,
293  .snprintf = nftnl_expr_meta_snprintf,
294  .json_parse = nftnl_expr_meta_json_parse,
295 };