libnftnl  1.2.8
byteorder.c
1 /*
2  * (C) 2012-2013 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  enum nft_byteorder_ops op;
28  unsigned int len;
29  unsigned int size;
30 };
31 
32 static int
33 nftnl_expr_byteorder_set(struct nftnl_expr *e, uint16_t type,
34  const void *data, uint32_t data_len)
35 {
36  struct nftnl_expr_byteorder *byteorder = nftnl_expr_data(e);
37 
38  switch(type) {
39  case NFTNL_EXPR_BYTEORDER_SREG:
40  memcpy(&byteorder->sreg, data, data_len);
41  break;
42  case NFTNL_EXPR_BYTEORDER_DREG:
43  memcpy(&byteorder->dreg, data, data_len);
44  break;
45  case NFTNL_EXPR_BYTEORDER_OP:
46  memcpy(&byteorder->op, data, data_len);
47  break;
48  case NFTNL_EXPR_BYTEORDER_LEN:
49  memcpy(&byteorder->len, data, data_len);
50  break;
51  case NFTNL_EXPR_BYTEORDER_SIZE:
52  memcpy(&byteorder->size, data, data_len);
53  break;
54  }
55  return 0;
56 }
57 
58 static const void *
59 nftnl_expr_byteorder_get(const struct nftnl_expr *e, uint16_t type,
60  uint32_t *data_len)
61 {
62  struct nftnl_expr_byteorder *byteorder = nftnl_expr_data(e);
63 
64  switch(type) {
65  case NFTNL_EXPR_BYTEORDER_SREG:
66  *data_len = sizeof(byteorder->sreg);
67  return &byteorder->sreg;
68  case NFTNL_EXPR_BYTEORDER_DREG:
69  *data_len = sizeof(byteorder->dreg);
70  return &byteorder->dreg;
71  case NFTNL_EXPR_BYTEORDER_OP:
72  *data_len = sizeof(byteorder->op);
73  return &byteorder->op;
74  case NFTNL_EXPR_BYTEORDER_LEN:
75  *data_len = sizeof(byteorder->len);
76  return &byteorder->len;
77  case NFTNL_EXPR_BYTEORDER_SIZE:
78  *data_len = sizeof(byteorder->size);
79  return &byteorder->size;
80  }
81  return NULL;
82 }
83 
84 static int nftnl_expr_byteorder_cb(const struct nlattr *attr, void *data)
85 {
86  const struct nlattr **tb = data;
87  int type = mnl_attr_get_type(attr);
88 
89  if (mnl_attr_type_valid(attr, NFTA_BYTEORDER_MAX) < 0)
90  return MNL_CB_OK;
91 
92  switch(type) {
93  case NFTA_BYTEORDER_SREG:
94  case NFTA_BYTEORDER_DREG:
95  case NFTA_BYTEORDER_OP:
96  case NFTA_BYTEORDER_LEN:
97  case NFTA_BYTEORDER_SIZE:
98  if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
99  abi_breakage();
100  break;
101  }
102 
103  tb[type] = attr;
104  return MNL_CB_OK;
105 }
106 
107 static void
108 nftnl_expr_byteorder_build(struct nlmsghdr *nlh, const struct nftnl_expr *e)
109 {
110  struct nftnl_expr_byteorder *byteorder = nftnl_expr_data(e);
111 
112  if (e->flags & (1 << NFTNL_EXPR_BYTEORDER_SREG)) {
113  mnl_attr_put_u32(nlh, NFTA_BYTEORDER_SREG,
114  htonl(byteorder->sreg));
115  }
116  if (e->flags & (1 << NFTNL_EXPR_BYTEORDER_DREG)) {
117  mnl_attr_put_u32(nlh, NFTA_BYTEORDER_DREG,
118  htonl(byteorder->dreg));
119  }
120  if (e->flags & (1 << NFTNL_EXPR_BYTEORDER_OP)) {
121  mnl_attr_put_u32(nlh, NFTA_BYTEORDER_OP,
122  htonl(byteorder->op));
123  }
124  if (e->flags & (1 << NFTNL_EXPR_BYTEORDER_LEN)) {
125  mnl_attr_put_u32(nlh, NFTA_BYTEORDER_LEN,
126  htonl(byteorder->len));
127  }
128  if (e->flags & (1 << NFTNL_EXPR_BYTEORDER_SIZE)) {
129  mnl_attr_put_u32(nlh, NFTA_BYTEORDER_SIZE,
130  htonl(byteorder->size));
131  }
132 }
133 
134 static int
135 nftnl_expr_byteorder_parse(struct nftnl_expr *e, struct nlattr *attr)
136 {
137  struct nftnl_expr_byteorder *byteorder = nftnl_expr_data(e);
138  struct nlattr *tb[NFTA_BYTEORDER_MAX+1] = {};
139  int ret = 0;
140 
141  if (mnl_attr_parse_nested(attr, nftnl_expr_byteorder_cb, tb) < 0)
142  return -1;
143 
144  if (tb[NFTA_BYTEORDER_SREG]) {
145  byteorder->sreg =
146  ntohl(mnl_attr_get_u32(tb[NFTA_BYTEORDER_SREG]));
147  e->flags |= (1 << NFTNL_EXPR_BYTEORDER_SREG);
148  }
149  if (tb[NFTA_BYTEORDER_DREG]) {
150  byteorder->dreg =
151  ntohl(mnl_attr_get_u32(tb[NFTA_BYTEORDER_DREG]));
152  e->flags |= (1 << NFTNL_EXPR_BYTEORDER_DREG);
153  }
154  if (tb[NFTA_BYTEORDER_OP]) {
155  byteorder->op =
156  ntohl(mnl_attr_get_u32(tb[NFTA_BYTEORDER_OP]));
157  e->flags |= (1 << NFTNL_EXPR_BYTEORDER_OP);
158  }
159  if (tb[NFTA_BYTEORDER_LEN]) {
160  byteorder->len =
161  ntohl(mnl_attr_get_u32(tb[NFTA_BYTEORDER_LEN]));
162  e->flags |= (1 << NFTNL_EXPR_BYTEORDER_LEN);
163  }
164  if (tb[NFTA_BYTEORDER_SIZE]) {
165  byteorder->size =
166  ntohl(mnl_attr_get_u32(tb[NFTA_BYTEORDER_SIZE]));
167  e->flags |= (1 << NFTNL_EXPR_BYTEORDER_SIZE);
168  }
169 
170  return ret;
171 }
172 
173 static const char *expr_byteorder_str[] = {
174  [NFT_BYTEORDER_HTON] = "hton",
175  [NFT_BYTEORDER_NTOH] = "ntoh",
176 };
177 
178 static const char *bo2str(uint32_t type)
179 {
180  if (type > NFT_BYTEORDER_HTON)
181  return "unknown";
182 
183  return expr_byteorder_str[type];
184 }
185 
186 static inline int nftnl_str2ntoh(const char *op)
187 {
188  if (strcmp(op, "ntoh") == 0)
189  return NFT_BYTEORDER_NTOH;
190  else if (strcmp(op, "hton") == 0)
191  return NFT_BYTEORDER_HTON;
192  else {
193  errno = EINVAL;
194  return -1;
195  }
196 }
197 
198 static int
199 nftnl_expr_byteorder_snprintf(char *buf, size_t remain,
200  uint32_t flags, const struct nftnl_expr *e)
201 {
202  struct nftnl_expr_byteorder *byteorder = nftnl_expr_data(e);
203  int offset = 0, ret;
204 
205  ret = snprintf(buf, remain, "reg %u = %s(reg %u, %u, %u) ",
206  byteorder->dreg, bo2str(byteorder->op),
207  byteorder->sreg, byteorder->size, byteorder->len);
208  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
209 
210  return offset;
211 }
212 
213 static struct attr_policy byteorder_attr_policy[__NFTNL_EXPR_BYTEORDER_MAX] = {
214  [NFTNL_EXPR_BYTEORDER_DREG] = { .maxlen = sizeof(uint32_t) },
215  [NFTNL_EXPR_BYTEORDER_SREG] = { .maxlen = sizeof(uint32_t) },
216  [NFTNL_EXPR_BYTEORDER_OP] = { .maxlen = sizeof(uint32_t) },
217  [NFTNL_EXPR_BYTEORDER_LEN] = { .maxlen = sizeof(uint32_t) },
218  [NFTNL_EXPR_BYTEORDER_SIZE] = { .maxlen = sizeof(uint32_t) },
219 };
220 
221 struct expr_ops expr_ops_byteorder = {
222  .name = "byteorder",
223  .alloc_len = sizeof(struct nftnl_expr_byteorder),
224  .nftnl_max_attr = __NFTNL_EXPR_BYTEORDER_MAX - 1,
225  .attr_policy = byteorder_attr_policy,
226  .set = nftnl_expr_byteorder_set,
227  .get = nftnl_expr_byteorder_get,
228  .parse = nftnl_expr_byteorder_parse,
229  .build = nftnl_expr_byteorder_build,
230  .output = nftnl_expr_byteorder_snprintf,
231 };