libnftnl  1.0.8
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_export(char *buf, size_t size,
120  const struct nftnl_expr *e, int type)
121 {
122  struct nftnl_expr_fwd *fwd = nftnl_expr_data(e);
123  NFTNL_BUF_INIT(b, buf, size);
124 
125  if (e->flags & (1 << NFTNL_EXPR_FWD_SREG_DEV))
126  nftnl_buf_u32(&b, type, fwd->sreg_dev, "sreg_dev");
127 
128  return nftnl_buf_done(&b);
129 }
130 
131 static int nftnl_expr_fwd_snprintf_default(char *buf, size_t len,
132  const struct nftnl_expr *e,
133  uint32_t flags)
134 {
135  int remain = len, offset = 0, ret;
136  struct nftnl_expr_fwd *fwd = nftnl_expr_data(e);
137 
138  if (e->flags & (1 << NFTNL_EXPR_FWD_SREG_DEV)) {
139  ret = snprintf(buf + offset, remain, "sreg_dev %u ",
140  fwd->sreg_dev);
141  SNPRINTF_BUFFER_SIZE(ret, remain, offset);
142  }
143 
144  return offset;
145 }
146 
147 static int nftnl_expr_fwd_snprintf(char *buf, size_t len, uint32_t type,
148  uint32_t flags, const struct nftnl_expr *e)
149 {
150  switch (type) {
151  case NFTNL_OUTPUT_DEFAULT:
152  return nftnl_expr_fwd_snprintf_default(buf, len, e, flags);
153  case NFTNL_OUTPUT_XML:
154  case NFTNL_OUTPUT_JSON:
155  return nftnl_expr_fwd_export(buf, len, e, type);
156  default:
157  break;
158  }
159  return -1;
160 }
161 
162 static bool nftnl_expr_fwd_cmp(const struct nftnl_expr *e1,
163  const struct nftnl_expr *e2)
164 {
165  struct nftnl_expr_fwd *f1 = nftnl_expr_data(e1);
166  struct nftnl_expr_fwd *f2 = nftnl_expr_data(e2);
167  bool eq = true;
168 
169  if (e1->flags & (1 << NFTNL_EXPR_FWD_SREG_DEV))
170  eq &= (f1->sreg_dev == f2->sreg_dev);
171 
172  return eq;
173 }
174 
175 struct expr_ops expr_ops_fwd = {
176  .name = "fwd",
177  .alloc_len = sizeof(struct nftnl_expr_fwd),
178  .max_attr = NFTA_FWD_MAX,
179  .cmp = nftnl_expr_fwd_cmp,
180  .set = nftnl_expr_fwd_set,
181  .get = nftnl_expr_fwd_get,
182  .parse = nftnl_expr_fwd_parse,
183  .build = nftnl_expr_fwd_build,
184  .snprintf = nftnl_expr_fwd_snprintf,
185  .json_parse = nftnl_expr_fwd_json_parse,
186 };