libnftnl  1.0.8
masq.c
1 /*
2  * (C) 2014 by Arturo Borrero Gonzalez <arturo@debian.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 
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 nftnl_expr_masq_export(char *buf, size_t size,
159  const struct nftnl_expr *e, int type)
160 {
161  struct nftnl_expr_masq *masq = nftnl_expr_data(e);
162  NFTNL_BUF_INIT(b, buf, size);
163 
164  if (e->flags & (1 << NFTNL_EXPR_MASQ_FLAGS))
165  nftnl_buf_u32(&b, type, masq->flags, FLAGS);
166  if (e->flags & (1 << NFTNL_EXPR_MASQ_REG_PROTO_MIN))
167  nftnl_buf_u32(&b, type, masq->sreg_proto_min, SREG_PROTO_MIN);
168  if (e->flags & (1 << NFTNL_EXPR_MASQ_REG_PROTO_MAX))
169  nftnl_buf_u32(&b, type, masq->sreg_proto_max, SREG_PROTO_MAX);
170 
171  return nftnl_buf_done(&b);
172 }
173 
174 static int nftnl_expr_masq_snprintf_default(char *buf, size_t len,
175  const struct nftnl_expr *e)
176 {
177  struct nftnl_expr_masq *masq = nftnl_expr_data(e);
178 
179  if (e->flags & (1 << NFTNL_EXPR_MASQ_FLAGS))
180  return snprintf(buf, len, "flags 0x%x ", masq->flags);
181  if (e->flags & (1 << NFTNL_EXPR_MASQ_REG_PROTO_MIN)) {
182  return snprintf(buf, len,
183  "proto_min reg %u proto_max reg %u ",
184  masq->sreg_proto_min, masq->sreg_proto_max);
185  }
186 
187  return 0;
188 }
189 
190 static int nftnl_expr_masq_snprintf(char *buf, size_t len, uint32_t type,
191  uint32_t flags, const struct nftnl_expr *e)
192 {
193  switch (type) {
194  case NFTNL_OUTPUT_DEFAULT:
195  return nftnl_expr_masq_snprintf_default(buf, len, e);
196  case NFTNL_OUTPUT_XML:
197  case NFTNL_OUTPUT_JSON:
198  return nftnl_expr_masq_export(buf, len, e, type);
199  default:
200  break;
201  }
202  return -1;
203 }
204 
205 static bool nftnl_expr_masq_cmp(const struct nftnl_expr *e1,
206  const struct nftnl_expr *e2)
207 {
208  struct nftnl_expr_masq *m1 = nftnl_expr_data(e1);
209  struct nftnl_expr_masq *m2 = nftnl_expr_data(e2);
210  bool eq = true;
211 
212  if (e1->flags & (1 << NFTNL_EXPR_MASQ_FLAGS))
213  eq &= (m1->flags == m2->flags);
214  if (e1->flags & (1 << NFTNL_EXPR_MASQ_REG_PROTO_MIN))
215  eq &= (m1->sreg_proto_min == m2->sreg_proto_min);
216  if (e1->flags & (1 << NFTNL_EXPR_MASQ_REG_PROTO_MAX))
217  eq &= (m1->sreg_proto_max == m2->sreg_proto_max);
218 
219  return eq;
220 }
221 
222 struct expr_ops expr_ops_masq = {
223  .name = "masq",
224  .alloc_len = sizeof(struct nftnl_expr_masq),
225  .max_attr = NFTA_MASQ_MAX,
226  .cmp = nftnl_expr_masq_cmp,
227  .set = nftnl_expr_masq_set,
228  .get = nftnl_expr_masq_get,
229  .parse = nftnl_expr_masq_parse,
230  .build = nftnl_expr_masq_build,
231  .snprintf = nftnl_expr_masq_snprintf,
232  .json_parse = nftnl_expr_masq_json_parse,
233 };