libnftnl  1.0.5
limit.c
1 /*
2  * (C) 2012-2013 by 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  * This code has been sponsored by Sophos Astaro <http://www.sophos.com>
10  */
11 
12 #include <stdio.h>
13 #include <stdint.h>
14 #include <inttypes.h>
15 #include <string.h>
16 #include <arpa/inet.h>
17 #include <errno.h>
18 #include <linux/netfilter/nf_tables.h>
19 
20 #include "internal.h"
21 #include <libmnl/libmnl.h>
22 #include <libnftnl/expr.h>
23 #include <libnftnl/rule.h>
24 
26  uint64_t rate;
27  uint64_t unit;
28 };
29 
30 static int
31 nftnl_expr_limit_set(struct nftnl_expr *e, uint16_t type,
32  const void *data, uint32_t data_len)
33 {
34  struct nftnl_expr_limit *limit = nftnl_expr_data(e);
35 
36  switch(type) {
37  case NFTNL_EXPR_LIMIT_RATE:
38  limit->rate = *((uint64_t *)data);
39  break;
40  case NFTNL_EXPR_LIMIT_UNIT:
41  limit->unit = *((uint64_t *)data);
42  break;
43  default:
44  return -1;
45  }
46  return 0;
47 }
48 
49 static const void *
50 nftnl_expr_limit_get(const struct nftnl_expr *e, uint16_t type,
51  uint32_t *data_len)
52 {
53  struct nftnl_expr_limit *limit = nftnl_expr_data(e);
54 
55  switch(type) {
56  case NFTNL_EXPR_LIMIT_RATE:
57  *data_len = sizeof(uint64_t);
58  return &limit->rate;
59  case NFTNL_EXPR_LIMIT_UNIT:
60  *data_len = sizeof(uint64_t);
61  return &limit->unit;
62  }
63  return NULL;
64 }
65 
66 static int nftnl_expr_limit_cb(const struct nlattr *attr, void *data)
67 {
68  const struct nlattr **tb = data;
69  int type = mnl_attr_get_type(attr);
70 
71  if (mnl_attr_type_valid(attr, NFTA_LIMIT_MAX) < 0)
72  return MNL_CB_OK;
73 
74  switch(type) {
75  case NFTA_LIMIT_RATE:
76  case NFTA_LIMIT_UNIT:
77  if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0)
78  abi_breakage();
79  break;
80  }
81 
82  tb[type] = attr;
83  return MNL_CB_OK;
84 }
85 
86 static void
87 nftnl_expr_limit_build(struct nlmsghdr *nlh, struct nftnl_expr *e)
88 {
89  struct nftnl_expr_limit *limit = nftnl_expr_data(e);
90 
91  if (e->flags & (1 << NFTNL_EXPR_LIMIT_RATE))
92  mnl_attr_put_u64(nlh, NFTA_LIMIT_RATE, htobe64(limit->rate));
93  if (e->flags & (1 << NFTNL_EXPR_LIMIT_UNIT))
94  mnl_attr_put_u64(nlh, NFTA_LIMIT_UNIT, htobe64(limit->unit));
95 }
96 
97 static int
98 nftnl_expr_limit_parse(struct nftnl_expr *e, struct nlattr *attr)
99 {
100  struct nftnl_expr_limit *limit = nftnl_expr_data(e);
101  struct nlattr *tb[NFTA_LIMIT_MAX+1] = {};
102 
103  if (mnl_attr_parse_nested(attr, nftnl_expr_limit_cb, tb) < 0)
104  return -1;
105 
106  if (tb[NFTA_LIMIT_RATE]) {
107  limit->rate = be64toh(mnl_attr_get_u64(tb[NFTA_LIMIT_RATE]));
108  e->flags |= (1 << NFTNL_EXPR_LIMIT_RATE);
109  }
110  if (tb[NFTA_LIMIT_UNIT]) {
111  limit->unit = be64toh(mnl_attr_get_u64(tb[NFTA_LIMIT_UNIT]));
112  e->flags |= (1 << NFTNL_EXPR_LIMIT_UNIT);
113  }
114 
115  return 0;
116 }
117 
118 static int nftnl_expr_limit_json_parse(struct nftnl_expr *e, json_t *root,
119  struct nftnl_parse_err *err)
120 {
121 #ifdef JSON_PARSING
122  uint64_t uval64;
123 
124  if (nftnl_jansson_parse_val(root, "rate", NFTNL_TYPE_U64, &uval64, err) == 0)
125  nftnl_expr_set_u64(e, NFTNL_EXPR_LIMIT_RATE, uval64);
126 
127  if (nftnl_jansson_parse_val(root, "unit", NFTNL_TYPE_U64, &uval64, err) == 0)
128  nftnl_expr_set_u64(e, NFTNL_EXPR_LIMIT_UNIT, uval64);
129 
130  return 0;
131 #else
132  errno = EOPNOTSUPP;
133  return -1;
134 #endif
135 }
136 
137 static int nftnl_expr_limit_xml_parse(struct nftnl_expr *e,
138  mxml_node_t *tree,
139  struct nftnl_parse_err *err)
140 {
141 #ifdef XML_PARSING
142  uint64_t rate, unit;
143 
144  if (nftnl_mxml_num_parse(tree, "rate", MXML_DESCEND_FIRST, BASE_DEC,
145  &rate, NFTNL_TYPE_U64, NFTNL_XML_MAND, err) == 0)
146  nftnl_expr_set_u64(e, NFTNL_EXPR_LIMIT_RATE, rate);
147 
148  if (nftnl_mxml_num_parse(tree, "unit", MXML_DESCEND_FIRST, BASE_DEC,
149  &unit, NFTNL_TYPE_U64, NFTNL_XML_MAND, err) == 0)
150  nftnl_expr_set_u64(e, NFTNL_EXPR_LIMIT_UNIT, unit);
151 
152  return 0;
153 #else
154  errno = EOPNOTSUPP;
155  return -1;
156 #endif
157 }
158 
159 static const char *get_unit(uint64_t u)
160 {
161  switch (u) {
162  case 1: return "second";
163  case 60: return "minute";
164  case 60 * 60: return "hour";
165  case 60 * 60 * 24: return "day";
166  case 60 * 60 * 24 * 7: return "week";
167  }
168  return "error";
169 }
170 
171 static int nftnl_expr_limit_export(char *buf, size_t size,
172  struct nftnl_expr *e, int type)
173 {
174  struct nftnl_expr_limit *limit = nftnl_expr_data(e);
175  NFTNL_BUF_INIT(b, buf, size);
176 
177  if (e->flags & (1 << NFTNL_EXPR_LIMIT_RATE))
178  nftnl_buf_u64(&b, type, limit->rate, RATE);
179  if (e->flags & (1 << NFTNL_EXPR_LIMIT_UNIT))
180  nftnl_buf_u64(&b, type, limit->unit, UNIT);
181 
182  return nftnl_buf_done(&b);
183 }
184 
185 static int nftnl_expr_limit_snprintf_default(char *buf, size_t len,
186  struct nftnl_expr *e)
187 {
188  struct nftnl_expr_limit *limit = nftnl_expr_data(e);
189 
190  return snprintf(buf, len, "rate %"PRIu64"/%s ",
191  limit->rate, get_unit(limit->unit));
192 }
193 
194 static int
195 nftnl_expr_limit_snprintf(char *buf, size_t len, uint32_t type,
196  uint32_t flags, struct nftnl_expr *e)
197 {
198 
199  switch(type) {
200  case NFTNL_OUTPUT_DEFAULT:
201  return nftnl_expr_limit_snprintf_default(buf, len, e);
202  case NFTNL_OUTPUT_XML:
203  case NFTNL_OUTPUT_JSON:
204  return nftnl_expr_limit_export(buf, len, e, type);
205  default:
206  break;
207  }
208  return -1;
209 }
210 
211 struct expr_ops expr_ops_limit = {
212  .name = "limit",
213  .alloc_len = sizeof(struct nftnl_expr_limit),
214  .max_attr = NFTA_LIMIT_MAX,
215  .set = nftnl_expr_limit_set,
216  .get = nftnl_expr_limit_get,
217  .parse = nftnl_expr_limit_parse,
218  .build = nftnl_expr_limit_build,
219  .snprintf = nftnl_expr_limit_snprintf,
220  .xml_parse = nftnl_expr_limit_xml_parse,
221  .json_parse = nftnl_expr_limit_json_parse,
222 };