libnftnl 1.2.6
obj/limit.c
1/*
2 * Copyright (c) 2017 Pablo M. Bermudo Garay <pablombg@gmail.com>
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/object.h>
21
22#include "obj.h"
23
24static int nftnl_obj_limit_set(struct nftnl_obj *e, uint16_t type,
25 const void *data, uint32_t data_len)
26{
27 struct nftnl_obj_limit *limit = nftnl_obj_data(e);
28
29 switch (type) {
30 case NFTNL_OBJ_LIMIT_RATE:
31 memcpy(&limit->rate, data, sizeof(limit->rate));
32 break;
33 case NFTNL_OBJ_LIMIT_UNIT:
34 memcpy(&limit->unit, data, sizeof(limit->unit));
35 break;
36 case NFTNL_OBJ_LIMIT_BURST:
37 memcpy(&limit->burst, data, sizeof(limit->burst));
38 break;
39 case NFTNL_OBJ_LIMIT_TYPE:
40 memcpy(&limit->type, data, sizeof(limit->type));
41 break;
42 case NFTNL_OBJ_LIMIT_FLAGS:
43 memcpy(&limit->flags, data, sizeof(limit->flags));
44 break;
45 default:
46 return -1;
47 }
48 return 0;
49}
50
51static const void *nftnl_obj_limit_get(const struct nftnl_obj *e,
52 uint16_t type, uint32_t *data_len)
53{
54 struct nftnl_obj_limit *limit = nftnl_obj_data(e);
55
56 switch (type) {
57 case NFTNL_OBJ_LIMIT_RATE:
58 *data_len = sizeof(limit->rate);
59 return &limit->rate;
60 case NFTNL_OBJ_LIMIT_UNIT:
61 *data_len = sizeof(limit->unit);
62 return &limit->unit;
63 case NFTNL_OBJ_LIMIT_BURST:
64 *data_len = sizeof(limit->burst);
65 return &limit->burst;
66 case NFTNL_OBJ_LIMIT_TYPE:
67 *data_len = sizeof(limit->type);
68 return &limit->type;
69 case NFTNL_OBJ_LIMIT_FLAGS:
70 *data_len = sizeof(limit->flags);
71 return &limit->flags;
72 }
73 return NULL;
74}
75
76static int nftnl_obj_limit_cb(const struct nlattr *attr, void *data)
77{
78 int type = mnl_attr_get_type(attr);
79 const struct nlattr **tb = data;
80
81 if (mnl_attr_type_valid(attr, NFTA_LIMIT_MAX) < 0)
82 return MNL_CB_OK;
83
84 switch (type) {
85 case NFTA_LIMIT_RATE:
86 case NFTA_LIMIT_UNIT:
87 if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0)
88 abi_breakage();
89 break;
90 case NFTA_LIMIT_BURST:
91 case NFTA_LIMIT_TYPE:
92 case NFTA_LIMIT_FLAGS:
93 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
94 abi_breakage();
95 break;
96 }
97
98 tb[type] = attr;
99 return MNL_CB_OK;
100}
101
102static void nftnl_obj_limit_build(struct nlmsghdr *nlh,
103 const struct nftnl_obj *e)
104{
105 struct nftnl_obj_limit *limit = nftnl_obj_data(e);
106
107 if (e->flags & (1 << NFTNL_OBJ_LIMIT_RATE))
108 mnl_attr_put_u64(nlh, NFTA_LIMIT_RATE, htobe64(limit->rate));
109 if (e->flags & (1 << NFTNL_OBJ_LIMIT_UNIT))
110 mnl_attr_put_u64(nlh, NFTA_LIMIT_UNIT, htobe64(limit->unit));
111 if (e->flags & (1 << NFTNL_OBJ_LIMIT_BURST))
112 mnl_attr_put_u32(nlh, NFTA_LIMIT_BURST, htonl(limit->burst));
113 if (e->flags & (1 << NFTNL_OBJ_LIMIT_TYPE))
114 mnl_attr_put_u32(nlh, NFTA_LIMIT_TYPE, htonl(limit->type));
115 if (e->flags & (1 << NFTNL_OBJ_LIMIT_FLAGS))
116 mnl_attr_put_u32(nlh, NFTA_LIMIT_FLAGS, htonl(limit->flags));
117}
118
119static int nftnl_obj_limit_parse(struct nftnl_obj *e, struct nlattr *attr)
120{
121 struct nftnl_obj_limit *limit = nftnl_obj_data(e);
122 struct nlattr *tb[NFTA_LIMIT_MAX + 1] = {};
123
124 if (mnl_attr_parse_nested(attr, nftnl_obj_limit_cb, tb) < 0)
125 return -1;
126
127 if (tb[NFTA_LIMIT_RATE]) {
128 limit->rate = be64toh(mnl_attr_get_u64(tb[NFTA_LIMIT_RATE]));
129 e->flags |= (1 << NFTNL_OBJ_LIMIT_RATE);
130 }
131 if (tb[NFTA_LIMIT_UNIT]) {
132 limit->unit = be64toh(mnl_attr_get_u64(tb[NFTA_LIMIT_UNIT]));
133 e->flags |= (1 << NFTNL_OBJ_LIMIT_UNIT);
134 }
135 if (tb[NFTA_LIMIT_BURST]) {
136 limit->burst = ntohl(mnl_attr_get_u32(tb[NFTA_LIMIT_BURST]));
137 e->flags |= (1 << NFTNL_OBJ_LIMIT_BURST);
138 }
139 if (tb[NFTA_LIMIT_TYPE]) {
140 limit->type = ntohl(mnl_attr_get_u32(tb[NFTA_LIMIT_TYPE]));
141 e->flags |= (1 << NFTNL_OBJ_LIMIT_TYPE);
142 }
143 if (tb[NFTA_LIMIT_FLAGS]) {
144 limit->flags = ntohl(mnl_attr_get_u32(tb[NFTA_LIMIT_FLAGS]));
145 e->flags |= (1 << NFTNL_OBJ_LIMIT_FLAGS);
146 }
147
148 return 0;
149}
150
151static int nftnl_obj_limit_snprintf(char *buf, size_t len,
152 uint32_t flags,
153 const struct nftnl_obj *e)
154{
155 struct nftnl_obj_limit *limit = nftnl_obj_data(e);
156
157 return snprintf(buf, len, "rate %"PRIu64" unit %"PRIu64" burst %u "
158 "type %u flags %u ", limit->rate, limit->unit,
159 limit->burst, limit->type, limit->flags);
160}
161
162struct obj_ops obj_ops_limit = {
163 .name = "limit",
164 .type = NFT_OBJECT_LIMIT,
165 .alloc_len = sizeof(struct nftnl_obj_limit),
166 .max_attr = NFTA_LIMIT_MAX,
167 .set = nftnl_obj_limit_set,
168 .get = nftnl_obj_limit_get,
169 .parse = nftnl_obj_limit_parse,
170 .build = nftnl_obj_limit_build,
171 .output = nftnl_obj_limit_snprintf,
172};