libnftnl  1.0.8
bitwise.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 "internal.h"
13 
14 #include <stdio.h>
15 #include <stdint.h>
16 #include <string.h> /* for memcpy */
17 #include <arpa/inet.h>
18 #include <errno.h>
19 #include <libmnl/libmnl.h>
20 #include <linux/netfilter/nf_tables.h>
21 #include <libnftnl/expr.h>
22 #include <libnftnl/rule.h>
23 
25  enum nft_registers sreg;
26  enum nft_registers dreg;
27  unsigned int len;
28  union nftnl_data_reg mask;
29  union nftnl_data_reg xor;
30 };
31 
32 static int
33 nftnl_expr_bitwise_set(struct nftnl_expr *e, uint16_t type,
34  const void *data, uint32_t data_len)
35 {
36  struct nftnl_expr_bitwise *bitwise = nftnl_expr_data(e);
37 
38  switch(type) {
39  case NFTNL_EXPR_BITWISE_SREG:
40  bitwise->sreg = *((uint32_t *)data);
41  break;
42  case NFTNL_EXPR_BITWISE_DREG:
43  bitwise->dreg = *((uint32_t *)data);
44  break;
45  case NFTNL_EXPR_BITWISE_LEN:
46  bitwise->len = *((unsigned int *)data);
47  break;
48  case NFTNL_EXPR_BITWISE_MASK:
49  memcpy(&bitwise->mask.val, data, data_len);
50  bitwise->mask.len = data_len;
51  break;
52  case NFTNL_EXPR_BITWISE_XOR:
53  memcpy(&bitwise->xor.val, data, data_len);
54  bitwise->xor.len = data_len;
55  break;
56  default:
57  return -1;
58  }
59  return 0;
60 }
61 
62 static const void *
63 nftnl_expr_bitwise_get(const struct nftnl_expr *e, uint16_t type,
64  uint32_t *data_len)
65 {
66  struct nftnl_expr_bitwise *bitwise = nftnl_expr_data(e);
67 
68  switch(type) {
69  case NFTNL_EXPR_BITWISE_SREG:
70  *data_len = sizeof(bitwise->sreg);
71  return &bitwise->sreg;
72  case NFTNL_EXPR_BITWISE_DREG:
73  *data_len = sizeof(bitwise->dreg);
74  return &bitwise->dreg;
75  case NFTNL_EXPR_BITWISE_LEN:
76  *data_len = sizeof(bitwise->len);
77  return &bitwise->len;
78  case NFTNL_EXPR_BITWISE_MASK:
79  *data_len = bitwise->mask.len;
80  return &bitwise->mask.val;
81  case NFTNL_EXPR_BITWISE_XOR:
82  *data_len = bitwise->xor.len;
83  return &bitwise->xor.val;
84  }
85  return NULL;
86 }
87 
88 static int nftnl_expr_bitwise_cb(const struct nlattr *attr, void *data)
89 {
90  const struct nlattr **tb = data;
91  int type = mnl_attr_get_type(attr);
92 
93  if (mnl_attr_type_valid(attr, NFTA_BITWISE_MAX) < 0)
94  return MNL_CB_OK;
95 
96  switch(type) {
97  case NFTA_BITWISE_SREG:
98  case NFTA_BITWISE_DREG:
99  case NFTA_BITWISE_LEN:
100  if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
101  abi_breakage();
102  break;
103  case NFTA_BITWISE_MASK:
104  case NFTA_BITWISE_XOR:
105  if (mnl_attr_validate(attr, MNL_TYPE_BINARY) < 0)
106  abi_breakage();
107  break;
108  }
109 
110  tb[type] = attr;
111  return MNL_CB_OK;
112 }
113 
114 static void
115 nftnl_expr_bitwise_build(struct nlmsghdr *nlh, const struct nftnl_expr *e)
116 {
117  struct nftnl_expr_bitwise *bitwise = nftnl_expr_data(e);
118 
119  if (e->flags & (1 << NFTNL_EXPR_BITWISE_SREG))
120  mnl_attr_put_u32(nlh, NFTA_BITWISE_SREG, htonl(bitwise->sreg));
121  if (e->flags & (1 << NFTNL_EXPR_BITWISE_DREG))
122  mnl_attr_put_u32(nlh, NFTA_BITWISE_DREG, htonl(bitwise->dreg));
123  if (e->flags & (1 << NFTNL_EXPR_BITWISE_LEN))
124  mnl_attr_put_u32(nlh, NFTA_BITWISE_LEN, htonl(bitwise->len));
125  if (e->flags & (1 << NFTNL_EXPR_BITWISE_MASK)) {
126  struct nlattr *nest;
127 
128  nest = mnl_attr_nest_start(nlh, NFTA_BITWISE_MASK);
129  mnl_attr_put(nlh, NFTA_DATA_VALUE, bitwise->mask.len,
130  bitwise->mask.val);
131  mnl_attr_nest_end(nlh, nest);
132  }
133  if (e->flags & (1 << NFTNL_EXPR_BITWISE_XOR)) {
134  struct nlattr *nest;
135 
136  nest = mnl_attr_nest_start(nlh, NFTA_BITWISE_XOR);
137  mnl_attr_put(nlh, NFTA_DATA_VALUE, bitwise->xor.len,
138  bitwise->xor.val);
139  mnl_attr_nest_end(nlh, nest);
140  }
141 }
142 
143 static int
144 nftnl_expr_bitwise_parse(struct nftnl_expr *e, struct nlattr *attr)
145 {
146  struct nftnl_expr_bitwise *bitwise = nftnl_expr_data(e);
147  struct nlattr *tb[NFTA_BITWISE_MAX+1] = {};
148  int ret = 0;
149 
150  if (mnl_attr_parse_nested(attr, nftnl_expr_bitwise_cb, tb) < 0)
151  return -1;
152 
153  if (tb[NFTA_BITWISE_SREG]) {
154  bitwise->sreg = ntohl(mnl_attr_get_u32(tb[NFTA_BITWISE_SREG]));
155  e->flags |= (1 << NFTNL_EXPR_BITWISE_SREG);
156  }
157  if (tb[NFTA_BITWISE_DREG]) {
158  bitwise->dreg = ntohl(mnl_attr_get_u32(tb[NFTA_BITWISE_DREG]));
159  e->flags |= (1 << NFTNL_EXPR_BITWISE_DREG);
160  }
161  if (tb[NFTA_BITWISE_LEN]) {
162  bitwise->len = ntohl(mnl_attr_get_u32(tb[NFTA_BITWISE_LEN]));
163  e->flags |= (1 << NFTNL_EXPR_BITWISE_LEN);
164  }
165  if (tb[NFTA_BITWISE_MASK]) {
166  ret = nftnl_parse_data(&bitwise->mask, tb[NFTA_BITWISE_MASK], NULL);
167  e->flags |= (1 << NFTA_BITWISE_MASK);
168  }
169  if (tb[NFTA_BITWISE_XOR]) {
170  ret = nftnl_parse_data(&bitwise->xor, tb[NFTA_BITWISE_XOR], NULL);
171  e->flags |= (1 << NFTA_BITWISE_XOR);
172  }
173 
174  return ret;
175 }
176 
177 static int
178 nftnl_expr_bitwise_json_parse(struct nftnl_expr *e, json_t *root,
179  struct nftnl_parse_err *err)
180 {
181 #ifdef JSON_PARSING
182  struct nftnl_expr_bitwise *bitwise = nftnl_expr_data(e);
183  uint32_t reg, len;
184 
185  if (nftnl_jansson_parse_reg(root, "sreg", NFTNL_TYPE_U32, &reg, err) == 0)
186  nftnl_expr_set_u32(e, NFTNL_EXPR_BITWISE_SREG, reg);
187 
188  if (nftnl_jansson_parse_reg(root, "dreg", NFTNL_TYPE_U32, &reg, err) == 0)
189  nftnl_expr_set_u32(e, NFTNL_EXPR_BITWISE_DREG, reg);
190 
191  if (nftnl_jansson_parse_val(root, "len", NFTNL_TYPE_U32, &len, err) == 0)
192  nftnl_expr_set_u32(e, NFTNL_EXPR_BITWISE_LEN, len);
193 
194  if (nftnl_jansson_data_reg_parse(root, "mask", &bitwise->mask,
195  err) == DATA_VALUE)
196  e->flags |= (1 << NFTNL_EXPR_BITWISE_MASK);
197 
198  if (nftnl_jansson_data_reg_parse(root, "xor", &bitwise->xor,
199  err) == DATA_VALUE)
200  e->flags |= (1 << NFTNL_EXPR_BITWISE_XOR);
201 
202  if (bitwise->mask.len != bitwise->xor.len)
203  return -1;
204 
205  return 0;
206 #else
207  errno = EOPNOTSUPP;
208  return -1;
209 #endif
210 }
211 
212 static int nftnl_expr_bitwise_export(char *buf, size_t size,
213  const struct nftnl_expr *e, int type)
214 {
215  struct nftnl_expr_bitwise *bitwise = nftnl_expr_data(e);
216  NFTNL_BUF_INIT(b, buf, size);
217 
218  if (e->flags & (1 << NFTNL_EXPR_BITWISE_SREG))
219  nftnl_buf_u32(&b, type, bitwise->sreg, SREG);
220  if (e->flags & (1 << NFTNL_EXPR_BITWISE_DREG))
221  nftnl_buf_u32(&b, type, bitwise->dreg, DREG);
222  if (e->flags & (1 << NFTNL_EXPR_BITWISE_LEN))
223  nftnl_buf_u32(&b, type, bitwise->len, LEN);
224  if (e->flags & (1 << NFTNL_EXPR_BITWISE_MASK))
225  nftnl_buf_reg(&b, type, &bitwise->mask, DATA_VALUE, MASK);
226  if (e->flags & (1 << NFTNL_EXPR_BITWISE_XOR))
227  nftnl_buf_reg(&b, type, &bitwise->xor, DATA_VALUE, XOR);
228 
229  return nftnl_buf_done(&b);
230 }
231 
232 static int nftnl_expr_bitwise_snprintf_default(char *buf, size_t size,
233  const struct nftnl_expr *e)
234 {
235  struct nftnl_expr_bitwise *bitwise = nftnl_expr_data(e);
236  int remain = size, offset = 0, ret;
237 
238  ret = snprintf(buf, remain, "reg %u = (reg=%u & ",
239  bitwise->dreg, bitwise->sreg);
240  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
241 
242  ret = nftnl_data_reg_snprintf(buf + offset, remain, &bitwise->mask,
243  NFTNL_OUTPUT_DEFAULT, 0, DATA_VALUE);
244  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
245 
246  ret = snprintf(buf + offset, remain, ") ^ ");
247  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
248 
249  ret = nftnl_data_reg_snprintf(buf + offset, remain, &bitwise->xor,
250  NFTNL_OUTPUT_DEFAULT, 0, DATA_VALUE);
251  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
252 
253  return offset;
254 }
255 
256 static int
257 nftnl_expr_bitwise_snprintf(char *buf, size_t size, uint32_t type,
258  uint32_t flags, const struct nftnl_expr *e)
259 {
260  switch (type) {
261  case NFTNL_OUTPUT_DEFAULT:
262  return nftnl_expr_bitwise_snprintf_default(buf, size, e);
263  case NFTNL_OUTPUT_XML:
264  case NFTNL_OUTPUT_JSON:
265  return nftnl_expr_bitwise_export(buf, size, e, type);
266  default:
267  break;
268  }
269  return -1;
270 }
271 
272 static bool nftnl_expr_bitwise_cmp(const struct nftnl_expr *e1,
273  const struct nftnl_expr *e2)
274 {
275  struct nftnl_expr_bitwise *b1 = nftnl_expr_data(e1);
276  struct nftnl_expr_bitwise *b2 = nftnl_expr_data(e2);
277  bool eq = true;
278 
279  if (e1->flags & (1 << NFTNL_EXPR_BITWISE_SREG))
280  eq &= (b1->sreg == b2->sreg);
281  if (e1->flags & (1 << NFTNL_EXPR_BITWISE_DREG))
282  eq &= (b1->dreg == b2->dreg);
283  if (e1->flags & (1 << NFTNL_EXPR_BITWISE_LEN))
284  eq &= (b1->len == b2->len);
285  if (e1->flags & (1 << NFTNL_EXPR_BITWISE_MASK))
286  eq &= nftnl_data_reg_cmp(&b1->mask, &b2->mask, DATA_VALUE);
287  if (e1->flags & (1 << NFTNL_EXPR_BITWISE_XOR))
288  eq &= nftnl_data_reg_cmp(&b1->xor, &b2->xor, DATA_VALUE);
289 
290  return eq;
291 }
292 
293 struct expr_ops expr_ops_bitwise = {
294  .name = "bitwise",
295  .alloc_len = sizeof(struct nftnl_expr_bitwise),
296  .max_attr = NFTA_BITWISE_MAX,
297  .cmp = nftnl_expr_bitwise_cmp,
298  .set = nftnl_expr_bitwise_set,
299  .get = nftnl_expr_bitwise_get,
300  .parse = nftnl_expr_bitwise_parse,
301  .build = nftnl_expr_bitwise_build,
302  .snprintf = nftnl_expr_bitwise_snprintf,
303  .json_parse = nftnl_expr_bitwise_json_parse,
304 };