libnftnl  1.0.6
fwd.c
1 /*
2  * (C) 2015 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 
10 #include <stdio.h>
11 #include <stdint.h>
12 #include <string.h>
13 #include <arpa/inet.h>
14 #include <errno.h>
15 #include "internal.h"
16 #include <libmnl/libmnl.h>
17 #include <linux/netfilter/nf_tables.h>
18 #include <libnftnl/expr.h>
19 #include <libnftnl/rule.h>
20 #include "expr_ops.h"
21 #include "data_reg.h"
22 #include <buffer.h>
23 
25  enum nft_registers sreg_dev;
26 };
27 
28 static int nftnl_expr_fwd_set(struct nftnl_expr *e, uint16_t type,
29  const void *data, uint32_t data_len)
30 {
31  struct nftnl_expr_fwd *fwd = nftnl_expr_data(e);
32 
33  switch (type) {
34  case NFTNL_EXPR_FWD_SREG_DEV:
35  fwd->sreg_dev= *((uint32_t *)data);
36  break;
37  default:
38  return -1;
39  }
40  return 0;
41 }
42 
43 static const void *nftnl_expr_fwd_get(const struct nftnl_expr *e,
44  uint16_t type, uint32_t *data_len)
45 {
46  struct nftnl_expr_fwd *fwd = nftnl_expr_data(e);
47 
48  switch (type) {
49  case NFTNL_EXPR_FWD_SREG_DEV:
50  *data_len = sizeof(fwd->sreg_dev);
51  return &fwd->sreg_dev;
52  }
53  return NULL;
54 }
55 
56 static int nftnl_expr_fwd_cb(const struct nlattr *attr, void *data)
57 {
58  const struct nlattr **tb = data;
59  int type = mnl_attr_get_type(attr);
60 
61  if (mnl_attr_type_valid(attr, NFTA_FWD_MAX) < 0)
62  return MNL_CB_OK;
63 
64  switch (type) {
65  case NFTA_FWD_SREG_DEV:
66  if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
67  abi_breakage();
68  break;
69  }
70 
71  tb[type] = attr;
72  return MNL_CB_OK;
73 }
74 
75 static void nftnl_expr_fwd_build(struct nlmsghdr *nlh,
76  const struct nftnl_expr *e)
77 {
78  struct nftnl_expr_fwd *fwd = nftnl_expr_data(e);
79 
80  if (e->flags & (1 << NFTNL_EXPR_FWD_SREG_DEV))
81  mnl_attr_put_u32(nlh, NFTA_FWD_SREG_DEV, htonl(fwd->sreg_dev));
82 }
83 
84 static int nftnl_expr_fwd_parse(struct nftnl_expr *e, struct nlattr *attr)
85 {
86  struct nftnl_expr_fwd *fwd = nftnl_expr_data(e);
87  struct nlattr *tb[NFTA_FWD_MAX + 1] = {};
88  int ret = 0;
89 
90  if (mnl_attr_parse_nested(attr, nftnl_expr_fwd_cb, tb) < 0)
91  return -1;
92 
93  if (tb[NFTA_FWD_SREG_DEV]) {
94  fwd->sreg_dev = ntohl(mnl_attr_get_u32(tb[NFTA_FWD_SREG_DEV]));
95  e->flags |= (1 << NFTNL_EXPR_FWD_SREG_DEV);
96  }
97 
98  return ret;
99 }
100 
101 static int nftnl_expr_fwd_json_parse(struct nftnl_expr *e, json_t *root,
102  struct nftnl_parse_err *err)
103 {
104 #ifdef JSON_PARSING
105  uint32_t sreg_dev;
106  int ret;
107 
108  ret = nftnl_jansson_parse_val(root, "sreg_dev", NFTNL_TYPE_U32, &sreg_dev, err);
109  if (ret >= 0)
110  nftnl_expr_set_u32(e, NFTNL_EXPR_FWD_SREG_DEV, sreg_dev);
111 
112  return 0;
113 #else
114  errno = EOPNOTSUPP;
115  return -1;
116 #endif
117 }
118 
119 static int nftnl_expr_fwd_xml_parse(struct nftnl_expr *e, mxml_node_t *tree,
120  struct nftnl_parse_err *err)
121 {
122 #ifdef XML_PARSING
123  uint32_t sreg_dev;
124 
125  if (nftnl_mxml_reg_parse(tree, "sreg_dev", &sreg_dev, MXML_DESCEND_FIRST,
126  NFTNL_XML_OPT, err) == 0)
127  nftnl_expr_set_u32(e, NFTNL_EXPR_FWD_SREG_DEV, sreg_dev);
128 
129  return 0;
130 #else
131  errno = EOPNOTSUPP;
132  return -1;
133 #endif
134 }
135 
136 static int nftnl_expr_fwd_export(char *buf, size_t size,
137  const struct nftnl_expr *e, int type)
138 {
139  struct nftnl_expr_fwd *fwd = nftnl_expr_data(e);
140  NFTNL_BUF_INIT(b, buf, size);
141 
142  if (e->flags & (1 << NFTNL_EXPR_FWD_SREG_DEV))
143  nftnl_buf_u32(&b, type, fwd->sreg_dev, "sreg_dev");
144 
145  return nftnl_buf_done(&b);
146 }
147 
148 static int nftnl_expr_fwd_snprintf_default(char *buf, size_t len,
149  const struct nftnl_expr *e,
150  uint32_t flags)
151 {
152  int size = len, offset = 0, ret;
153  struct nftnl_expr_fwd *fwd = nftnl_expr_data(e);
154 
155  if (e->flags & (1 << NFTNL_EXPR_FWD_SREG_DEV)) {
156  ret = snprintf(buf + offset, len, "sreg_dev %u ", fwd->sreg_dev);
157  SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
158  }
159 
160  return offset;
161 }
162 
163 static int nftnl_expr_fwd_snprintf(char *buf, size_t len, uint32_t type,
164  uint32_t flags, const struct nftnl_expr *e)
165 {
166  switch (type) {
167  case NFTNL_OUTPUT_DEFAULT:
168  return nftnl_expr_fwd_snprintf_default(buf, len, e, flags);
169  case NFTNL_OUTPUT_XML:
170  case NFTNL_OUTPUT_JSON:
171  return nftnl_expr_fwd_export(buf, len, e, type);
172  default:
173  break;
174  }
175  return -1;
176 }
177 
178 struct expr_ops expr_ops_fwd = {
179  .name = "fwd",
180  .alloc_len = sizeof(struct nftnl_expr_fwd),
181  .max_attr = NFTA_FWD_MAX,
182  .set = nftnl_expr_fwd_set,
183  .get = nftnl_expr_fwd_get,
184  .parse = nftnl_expr_fwd_parse,
185  .build = nftnl_expr_fwd_build,
186  .snprintf = nftnl_expr_fwd_snprintf,
187  .xml_parse = nftnl_expr_fwd_xml_parse,
188  .json_parse = nftnl_expr_fwd_json_parse,
189 };