libnftnl  1.1.7
expr/synproxy.c
1 #include <stdio.h>
2 #include <stdint.h>
3 #include <string.h>
4 #include <arpa/inet.h>
5 #include <errno.h>
6 #include <linux/netfilter/nf_tables.h>
7 
8 #include "internal.h"
9 #include <libmnl/libmnl.h>
10 #include <libnftnl/expr.h>
11 #include <libnftnl/rule.h>
12 
14  uint16_t mss;
15  uint8_t wscale;
16  uint32_t flags;
17 };
18 
19 static int nftnl_expr_synproxy_set(struct nftnl_expr *e, uint16_t type,
20  const void *data, uint32_t data_len)
21 {
22  struct nftnl_expr_synproxy *synproxy = nftnl_expr_data(e);
23 
24  switch(type) {
25  case NFTNL_EXPR_SYNPROXY_MSS:
26  memcpy(&synproxy->mss, data, sizeof(synproxy->mss));
27  break;
28  case NFTNL_EXPR_SYNPROXY_WSCALE:
29  memcpy(&synproxy->wscale, data, sizeof(synproxy->wscale));
30  break;
31  case NFTNL_EXPR_SYNPROXY_FLAGS:
32  memcpy(&synproxy->flags, data, sizeof(synproxy->flags));
33  break;
34  }
35  return 0;
36 }
37 
38 static const void *
39 nftnl_expr_synproxy_get(const struct nftnl_expr *e, uint16_t type,
40  uint32_t *data_len)
41 {
42  struct nftnl_expr_synproxy *synproxy = nftnl_expr_data(e);
43 
44  switch(type) {
45  case NFTNL_EXPR_SYNPROXY_MSS:
46  *data_len = sizeof(synproxy->mss);
47  return &synproxy->mss;
48  case NFTNL_EXPR_SYNPROXY_WSCALE:
49  *data_len = sizeof(synproxy->wscale);
50  return &synproxy->wscale;
51  case NFTNL_EXPR_SYNPROXY_FLAGS:
52  *data_len = sizeof(synproxy->flags);
53  return &synproxy->flags;
54  }
55  return NULL;
56 }
57 
58 static int nftnl_expr_synproxy_cb(const struct nlattr *attr, void *data)
59 {
60  const struct nlattr **tb = data;
61  int type = mnl_attr_get_type(attr);
62 
63  if (mnl_attr_type_valid(attr, NFTA_SYNPROXY_MAX) < 0)
64  return MNL_CB_OK;
65 
66  switch(type) {
67  case NFTNL_EXPR_SYNPROXY_MSS:
68  if (mnl_attr_validate(attr, MNL_TYPE_U16) < 0)
69  abi_breakage();
70  break;
71 
72  case NFTNL_EXPR_SYNPROXY_WSCALE:
73  if (mnl_attr_validate(attr, MNL_TYPE_U8) < 0)
74  abi_breakage();
75  break;
76 
77  case NFTNL_EXPR_SYNPROXY_FLAGS:
78  if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
79  abi_breakage();
80  break;
81  }
82 
83  tb[type] = attr;
84  return MNL_CB_OK;
85 }
86 
87 static void
88 nftnl_expr_synproxy_build(struct nlmsghdr *nlh, const struct nftnl_expr *e)
89 {
90  struct nftnl_expr_synproxy *synproxy = nftnl_expr_data(e);
91 
92  if (e->flags & (1 << NFTNL_EXPR_SYNPROXY_MSS))
93  mnl_attr_put_u16(nlh, NFTNL_EXPR_SYNPROXY_MSS,
94  htons(synproxy->mss));
95  if (e->flags & (1 << NFTNL_EXPR_SYNPROXY_WSCALE))
96  mnl_attr_put_u8(nlh, NFTNL_EXPR_SYNPROXY_WSCALE,
97  synproxy->wscale);
98  if (e->flags & (1 << NFTNL_EXPR_SYNPROXY_FLAGS))
99  mnl_attr_put_u32(nlh, NFTNL_EXPR_SYNPROXY_FLAGS,
100  htonl(synproxy->flags));
101 }
102 
103 static int
104 nftnl_expr_synproxy_parse(struct nftnl_expr *e, struct nlattr *attr)
105 {
106  struct nftnl_expr_synproxy *synproxy = nftnl_expr_data(e);
107  struct nlattr *tb[NFTA_SYNPROXY_MAX + 1] = {};
108 
109  if (mnl_attr_parse_nested(attr, nftnl_expr_synproxy_cb, tb) < 0)
110  return -1;
111 
112  if (tb[NFTA_SYNPROXY_MSS]) {
113  synproxy->mss = ntohs(mnl_attr_get_u16(tb[NFTA_SYNPROXY_MSS]));
114  e->flags |= (1 << NFTNL_EXPR_SYNPROXY_MSS);
115  }
116 
117  if (tb[NFTA_SYNPROXY_WSCALE]) {
118  synproxy->wscale = mnl_attr_get_u8(tb[NFTA_SYNPROXY_WSCALE]);
119  e->flags |= (1 << NFTNL_EXPR_SYNPROXY_WSCALE);
120  }
121 
122  if (tb[NFTA_SYNPROXY_FLAGS]) {
123  synproxy->flags = ntohl(mnl_attr_get_u32(tb[NFTA_SYNPROXY_FLAGS]));
124  e->flags |= (1 << NFTNL_EXPR_SYNPROXY_FLAGS);
125  }
126 
127  return 0;
128 }
129 
130 static int nftnl_expr_synproxy_snprintf_default(char *buf, size_t size,
131  const struct nftnl_expr *e)
132 {
133  struct nftnl_expr_synproxy *synproxy = nftnl_expr_data(e);
134  int ret, offset = 0, len = size;
135 
136  if (e->flags & (1 << NFTNL_EXPR_SYNPROXY_MSS) &&
137  e->flags & (1 << NFTNL_EXPR_SYNPROXY_WSCALE)) {
138  ret = snprintf(buf, len, "mss %u wscale %u ", synproxy->mss,
139  synproxy->wscale);
140  SNPRINTF_BUFFER_SIZE(ret, len, offset);
141  }
142 
143  return offset;
144 }
145 
146 static int
147 nftnl_expr_synproxy_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_synproxy_snprintf_default(buf, len, e);
153  case NFTNL_OUTPUT_XML:
154  case NFTNL_OUTPUT_JSON:
155  default:
156  break;
157  }
158  return -1;
159 }
160 
161 struct expr_ops expr_ops_synproxy = {
162  .name = "synproxy",
163  .alloc_len = sizeof(struct nftnl_expr_synproxy),
164  .max_attr = NFTA_SYNPROXY_MAX,
165  .set = nftnl_expr_synproxy_set,
166  .get = nftnl_expr_synproxy_get,
167  .parse = nftnl_expr_synproxy_parse,
168  .build = nftnl_expr_synproxy_build,
169  .snprintf = nftnl_expr_synproxy_snprintf,
170 };