libnftnl  1.1.5
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  memcpy(&bitwise->sreg, data, sizeof(bitwise->sreg));
41  break;
42  case NFTNL_EXPR_BITWISE_DREG:
43  memcpy(&bitwise->dreg, data, sizeof(bitwise->dreg));
44  break;
45  case NFTNL_EXPR_BITWISE_LEN:
46  memcpy(&bitwise->len, data, sizeof(bitwise->len));
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 nftnl_expr_bitwise_snprintf_default(char *buf, size_t size,
178  const struct nftnl_expr *e)
179 {
180  struct nftnl_expr_bitwise *bitwise = nftnl_expr_data(e);
181  int remain = size, offset = 0, ret;
182 
183  ret = snprintf(buf, remain, "reg %u = (reg=%u & ",
184  bitwise->dreg, bitwise->sreg);
185  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
186 
187  ret = nftnl_data_reg_snprintf(buf + offset, remain, &bitwise->mask,
188  NFTNL_OUTPUT_DEFAULT, 0, DATA_VALUE);
189  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
190 
191  ret = snprintf(buf + offset, remain, ") ^ ");
192  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
193 
194  ret = nftnl_data_reg_snprintf(buf + offset, remain, &bitwise->xor,
195  NFTNL_OUTPUT_DEFAULT, 0, DATA_VALUE);
196  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
197 
198  return offset;
199 }
200 
201 static int
202 nftnl_expr_bitwise_snprintf(char *buf, size_t size, uint32_t type,
203  uint32_t flags, const struct nftnl_expr *e)
204 {
205  switch (type) {
206  case NFTNL_OUTPUT_DEFAULT:
207  return nftnl_expr_bitwise_snprintf_default(buf, size, e);
208  default:
209  break;
210  }
211  return -1;
212 }
213 
214 struct expr_ops expr_ops_bitwise = {
215  .name = "bitwise",
216  .alloc_len = sizeof(struct nftnl_expr_bitwise),
217  .max_attr = NFTA_BITWISE_MAX,
218  .set = nftnl_expr_bitwise_set,
219  .get = nftnl_expr_bitwise_get,
220  .parse = nftnl_expr_bitwise_parse,
221  .build = nftnl_expr_bitwise_build,
222  .snprintf = nftnl_expr_bitwise_snprintf,
223 };