libnftnl  1.0.6
masq.c
1 /*
2  * (C) 2014 by Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
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 
10 #include <stdio.h>
11 #include <stdint.h>
12 #include <arpa/inet.h>
13 #include <errno.h>
14 #include <inttypes.h>
15 
16 #include <linux/netfilter/nf_tables.h>
17 
18 #include "internal.h"
19 #include <libmnl/libmnl.h>
20 #include <libnftnl/expr.h>
21 #include <libnftnl/rule.h>
22 
24  uint32_t flags;
25  enum nft_registers sreg_proto_min;
26  enum nft_registers sreg_proto_max;
27 };
28 
29 static int
30 nftnl_expr_masq_set(struct nftnl_expr *e, uint16_t type,
31  const void *data, uint32_t data_len)
32 {
33  struct nftnl_expr_masq *masq = nftnl_expr_data(e);
34 
35  switch (type) {
36  case NFTNL_EXPR_MASQ_FLAGS:
37  masq->flags = *((uint32_t *)data);
38  break;
39  case NFTNL_EXPR_MASQ_REG_PROTO_MIN:
40  masq->sreg_proto_min = *((uint32_t *)data);
41  break;
42  case NFTNL_EXPR_MASQ_REG_PROTO_MAX:
43  masq->sreg_proto_max = *((uint32_t *)data);
44  break;
45  default:
46  return -1;
47  }
48  return 0;
49 }
50 
51 static const void *
52 nftnl_expr_masq_get(const struct nftnl_expr *e, uint16_t type,
53  uint32_t *data_len)
54 {
55  struct nftnl_expr_masq *masq = nftnl_expr_data(e);
56 
57  switch (type) {
58  case NFTNL_EXPR_MASQ_FLAGS:
59  *data_len = sizeof(masq->flags);
60  return &masq->flags;
61  case NFTNL_EXPR_MASQ_REG_PROTO_MIN:
62  *data_len = sizeof(masq->sreg_proto_min);
63  return &masq->sreg_proto_min;
64  case NFTNL_EXPR_MASQ_REG_PROTO_MAX:
65  *data_len = sizeof(masq->sreg_proto_max);
66  return &masq->sreg_proto_max;
67  }
68  return NULL;
69 }
70 
71 static int nftnl_expr_masq_cb(const struct nlattr *attr, void *data)
72 {
73  const struct nlattr **tb = data;
74  int type = mnl_attr_get_type(attr);
75 
76  if (mnl_attr_type_valid(attr, NFTA_MASQ_MAX) < 0)
77  return MNL_CB_OK;
78 
79  switch (type) {
80  case NFTA_MASQ_REG_PROTO_MIN:
81  case NFTA_MASQ_REG_PROTO_MAX:
82  case NFTA_MASQ_FLAGS:
83  if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
84  abi_breakage();
85  break;
86  }
87 
88  tb[type] = attr;
89  return MNL_CB_OK;
90 }
91 
92 static void
93 nftnl_expr_masq_build(struct nlmsghdr *nlh, const struct nftnl_expr *e)
94 {
95  struct nftnl_expr_masq *masq = nftnl_expr_data(e);
96 
97  if (e->flags & (1 << NFTNL_EXPR_MASQ_FLAGS))
98  mnl_attr_put_u32(nlh, NFTA_MASQ_FLAGS, htobe32(masq->flags));
99  if (e->flags & (1 << NFTNL_EXPR_MASQ_REG_PROTO_MIN))
100  mnl_attr_put_u32(nlh, NFTA_MASQ_REG_PROTO_MIN,
101  htobe32(masq->sreg_proto_min));
102  if (e->flags & (1 << NFTNL_EXPR_MASQ_REG_PROTO_MAX))
103  mnl_attr_put_u32(nlh, NFTA_MASQ_REG_PROTO_MAX,
104  htobe32(masq->sreg_proto_max));
105 }
106 
107 static int
108 nftnl_expr_masq_parse(struct nftnl_expr *e, struct nlattr *attr)
109 {
110  struct nftnl_expr_masq *masq = nftnl_expr_data(e);
111  struct nlattr *tb[NFTA_MASQ_MAX+1] = {};
112 
113  if (mnl_attr_parse_nested(attr, nftnl_expr_masq_cb, tb) < 0)
114  return -1;
115 
116  if (tb[NFTA_MASQ_FLAGS]) {
117  masq->flags = be32toh(mnl_attr_get_u32(tb[NFTA_MASQ_FLAGS]));
118  e->flags |= (1 << NFTNL_EXPR_MASQ_FLAGS);
119  }
120  if (tb[NFTA_MASQ_REG_PROTO_MIN]) {
121  masq->sreg_proto_min =
122  be32toh(mnl_attr_get_u32(tb[NFTA_MASQ_REG_PROTO_MIN]));
123  e->flags |= (1 << NFTNL_EXPR_MASQ_REG_PROTO_MIN);
124  }
125  if (tb[NFTA_MASQ_REG_PROTO_MAX]) {
126  masq->sreg_proto_max =
127  be32toh(mnl_attr_get_u32(tb[NFTA_MASQ_REG_PROTO_MAX]));
128  e->flags |= (1 << NFTNL_EXPR_MASQ_REG_PROTO_MAX);
129  }
130 
131  return 0;
132 }
133 
134 static int
135 nftnl_expr_masq_json_parse(struct nftnl_expr *e, json_t *root,
136  struct nftnl_parse_err *err)
137 {
138 #ifdef JSON_PARSING
139  uint32_t reg, flags;
140 
141  if (nftnl_jansson_parse_val(root, "flags", NFTNL_TYPE_U32, &flags,
142  err) == 0)
143  nftnl_expr_set_u32(e, NFTNL_EXPR_MASQ_FLAGS, flags);
144  if (nftnl_jansson_parse_reg(root, "sreg_proto_min", NFTNL_TYPE_U32,
145  &reg, err) == 0)
146  nftnl_expr_set_u32(e, NFTNL_EXPR_MASQ_REG_PROTO_MIN, reg);
147  if (nftnl_jansson_parse_reg(root, "sreg_proto_max", NFTNL_TYPE_U32,
148  &reg, err) == 0)
149  nftnl_expr_set_u32(e, NFTNL_EXPR_MASQ_REG_PROTO_MAX, reg);
150 
151  return 0;
152 #else
153  errno = EOPNOTSUPP;
154  return -1;
155 #endif
156 }
157 
158 static int
159 nftnl_expr_masq_xml_parse(struct nftnl_expr *e, mxml_node_t *tree,
160  struct nftnl_parse_err *err)
161 {
162 #ifdef XML_PARSING
163  uint32_t flags;
164  uint32_t reg_proto_min, reg_proto_max;
165 
166  if (nftnl_mxml_num_parse(tree, "flags", MXML_DESCEND_FIRST, BASE_DEC,
167  &flags, NFTNL_TYPE_U32, NFTNL_XML_MAND, err) == 0)
168  nftnl_expr_set_u32(e, NFTNL_EXPR_MASQ_FLAGS, flags);
169  if (nftnl_mxml_reg_parse(tree, "sreg_proto_min", &reg_proto_min,
170  MXML_DESCEND, NFTNL_XML_MAND, err) == 0)
171  nftnl_expr_set_u32(e, NFTNL_EXPR_MASQ_REG_PROTO_MIN,
172  reg_proto_min);
173  if (nftnl_mxml_reg_parse(tree, "sreg_proto_max", &reg_proto_max,
174  MXML_DESCEND, NFTNL_XML_MAND, err) == 0)
175  nftnl_expr_set_u32(e, NFTNL_EXPR_MASQ_REG_PROTO_MAX,
176  reg_proto_max);
177 
178  return 0;
179 #else
180  errno = EOPNOTSUPP;
181  return -1;
182 #endif
183 }
184 static int nftnl_expr_masq_export(char *buf, size_t size,
185  const struct nftnl_expr *e, int type)
186 {
187  struct nftnl_expr_masq *masq = nftnl_expr_data(e);
188  NFTNL_BUF_INIT(b, buf, size);
189 
190  if (e->flags & (1 << NFTNL_EXPR_MASQ_FLAGS))
191  nftnl_buf_u32(&b, type, masq->flags, FLAGS);
192  if (e->flags & (1 << NFTNL_EXPR_MASQ_REG_PROTO_MIN))
193  nftnl_buf_u32(&b, type, masq->sreg_proto_min, SREG_PROTO_MIN);
194  if (e->flags & (1 << NFTNL_EXPR_MASQ_REG_PROTO_MAX))
195  nftnl_buf_u32(&b, type, masq->sreg_proto_max, SREG_PROTO_MAX);
196 
197  return nftnl_buf_done(&b);
198 }
199 
200 static int nftnl_expr_masq_snprintf_default(char *buf, size_t len,
201  const struct nftnl_expr *e)
202 {
203  struct nftnl_expr_masq *masq = nftnl_expr_data(e);
204 
205  if (e->flags & (1 << NFTNL_EXPR_MASQ_FLAGS))
206  return snprintf(buf, len, "flags 0x%x ", masq->flags);
207  if (e->flags & (1 << NFTNL_EXPR_MASQ_REG_PROTO_MIN)) {
208  return snprintf(buf, len,
209  "proto_min reg %u proto_max reg %u ",
210  masq->sreg_proto_min, masq->sreg_proto_max);
211  }
212 
213  return 0;
214 }
215 
216 static int nftnl_expr_masq_snprintf(char *buf, size_t len, uint32_t type,
217  uint32_t flags, const struct nftnl_expr *e)
218 {
219  switch (type) {
220  case NFTNL_OUTPUT_DEFAULT:
221  return nftnl_expr_masq_snprintf_default(buf, len, e);
222  case NFTNL_OUTPUT_XML:
223  case NFTNL_OUTPUT_JSON:
224  return nftnl_expr_masq_export(buf, len, e, type);
225  default:
226  break;
227  }
228  return -1;
229 }
230 
231 struct expr_ops expr_ops_masq = {
232  .name = "masq",
233  .alloc_len = sizeof(struct nftnl_expr_masq),
234  .max_attr = NFTA_MASQ_MAX,
235  .set = nftnl_expr_masq_set,
236  .get = nftnl_expr_masq_get,
237  .parse = nftnl_expr_masq_parse,
238  .build = nftnl_expr_masq_build,
239  .snprintf = nftnl_expr_masq_snprintf,
240  .xml_parse = nftnl_expr_masq_xml_parse,
241  .json_parse = nftnl_expr_masq_json_parse,
242 };