| 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */cxx . . $ `<file>/src/expr/limit.c`/ |
| 2 | /* |
| 3 | * (C) 2012-2013 by Pablo Neira Ayuso <[email protected]> |
| 4 | * |
| 5 | * This code has been sponsored by Sophos Astaro <http://www.sophos.com> |
| 6 | */ |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <stdint.h> |
| 10 | #include <inttypes.h> |
| 11 | #include <string.h> |
| 12 | #include <arpa/inet.h> |
| 13 | #include <errno.h> |
| 14 | #include <linux/netfilter/nf_tables.h>cxx . . $ `<file>/include/linux/netfilter/nf_tables.h`/ |
| 15 | |
| 16 | #include "internal.h"cxx . . $ `<file>/include/internal.h`/ |
| 17 | #include <libmnl/libmnl.h> |
| 18 | #include <libnftnl/expr.h>cxx . . $ `<file>/include/libnftnl/expr.h`/ |
| 19 | #include <libnftnl/rule.h>cxx . . $ `<file>/include/libnftnl/rule.h`/ |
| 20 | |
| 21 | struct nftnl_expr_limitcxx . . $ nftnl_expr_limit# { |
| 22 | uint64_tcxx . . $ uint64_t# ratecxx . . $ nftnl_expr_limit#rate.; |
| 23 | uint64_tcxx . . $ uint64_t# unitcxx . . $ nftnl_expr_limit#unit.; |
| 24 | uint32_tcxx . . $ uint32_t# burstcxx . . $ nftnl_expr_limit#burst.; |
| 25 | enum nft_limit_typecxx . . $ nft_limit_type# typecxx . . $ nftnl_expr_limit#type.; |
| 26 | uint32_tcxx . . $ uint32_t# flagscxx . . $ nftnl_expr_limit#flags.; |
| 27 | }; |
| 28 | |
| 29 | static int |
| 30 | nftnl_expr_limit_setcxx . . $ nftnl_expr_limit_set(211371a3e3842127).(struct nftnl_exprcxx . . $ nftnl_expr# *e, uint16_tcxx . . $ uint16_t# type, |
| 31 | const void *data, uint32_tcxx . . $ uint32_t# data_len, uint32_tcxx . . $ uint32_t# byteorder) |
| 32 | { |
| 33 | struct nftnl_expr_limitcxx . . $ nftnl_expr_limit# *limit = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 34 | |
| 35 | switch(type) { |
| 36 | case NFTNL_EXPR_LIMIT_RATEcxx . . $ NFTNL_EXPR_LIMIT_RATE.: |
| 37 | memcpycxx . . $ memcpy(cc817cb175f282d).(&limit->ratecxx . . $ nftnl_expr_limit#rate., data, data_len); |
| 38 | break; |
| 39 | case NFTNL_EXPR_LIMIT_UNITcxx . . $ NFTNL_EXPR_LIMIT_UNIT.: |
| 40 | memcpycxx . . $ memcpy(cc817cb175f282d).(&limit->unitcxx . . $ nftnl_expr_limit#unit., data, data_len); |
| 41 | break; |
| 42 | case NFTNL_EXPR_LIMIT_BURSTcxx . . $ NFTNL_EXPR_LIMIT_BURST.: |
| 43 | memcpycxx . . $ memcpy(cc817cb175f282d).(&limit->burstcxx . . $ nftnl_expr_limit#burst., data, data_len); |
| 44 | break; |
| 45 | case NFTNL_EXPR_LIMIT_TYPEcxx . . $ NFTNL_EXPR_LIMIT_TYPE.: |
| 46 | memcpycxx . . $ memcpy(cc817cb175f282d).(&limit->typecxx . . $ nftnl_expr_limit#type., data, data_len); |
| 47 | break; |
| 48 | case NFTNL_EXPR_LIMIT_FLAGScxx . . $ NFTNL_EXPR_LIMIT_FLAGS.: |
| 49 | memcpycxx . . $ memcpy(cc817cb175f282d).(&limit->flagscxx . . $ nftnl_expr_limit#flags., data, data_len); |
| 50 | break; |
| 51 | } |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | static const void * |
| 56 | nftnl_expr_limit_getcxx . . $ nftnl_expr_limit_get(9de263c4f9e71c4b).(const struct nftnl_exprcxx . . $ nftnl_expr# *e, uint16_tcxx . . $ uint16_t# type, |
| 57 | uint32_tcxx . . $ uint32_t# *data_len) |
| 58 | { |
| 59 | struct nftnl_expr_limitcxx . . $ nftnl_expr_limit# *limit = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 60 | |
| 61 | switch(type) { |
| 62 | case NFTNL_EXPR_LIMIT_RATEcxx . . $ NFTNL_EXPR_LIMIT_RATE.: |
| 63 | *data_len = sizeof(uint64_tcxx . . $ uint64_t#); |
| 64 | return &limit->ratecxx . . $ nftnl_expr_limit#rate.; |
| 65 | case NFTNL_EXPR_LIMIT_UNITcxx . . $ NFTNL_EXPR_LIMIT_UNIT.: |
| 66 | *data_len = sizeof(uint64_tcxx . . $ uint64_t#); |
| 67 | return &limit->unitcxx . . $ nftnl_expr_limit#unit.; |
| 68 | case NFTNL_EXPR_LIMIT_BURSTcxx . . $ NFTNL_EXPR_LIMIT_BURST.: |
| 69 | *data_len = sizeof(uint32_tcxx . . $ uint32_t#); |
| 70 | return &limit->burstcxx . . $ nftnl_expr_limit#burst.; |
| 71 | case NFTNL_EXPR_LIMIT_TYPEcxx . . $ NFTNL_EXPR_LIMIT_TYPE.: |
| 72 | *data_len = sizeof(uint32_tcxx . . $ uint32_t#); |
| 73 | return &limit->typecxx . . $ nftnl_expr_limit#type.; |
| 74 | case NFTNL_EXPR_LIMIT_FLAGScxx . . $ NFTNL_EXPR_LIMIT_FLAGS.: |
| 75 | *data_len = sizeof(uint32_tcxx . . $ uint32_t#); |
| 76 | return &limit->flagscxx . . $ nftnl_expr_limit#flags.; |
| 77 | } |
| 78 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 79 | } |
| 80 | |
| 81 | static int nftnl_expr_limit_cbcxx . . $ nftnl_expr_limit_cb(de2ef18c48fd675c).(const struct nlattrcxx . . $ nlattr# *attr, void *data) |
| 82 | { |
| 83 | const struct nlattrcxx . . $ nlattr# **tb = data; |
| 84 | int type = mnl_attr_get_typecxx . . $ mnl_attr_get_type(1da47bcc4c4b975).(attr); |
| 85 | |
| 86 | if (mnl_attr_type_validcxx . . $ mnl_attr_type_valid(47df1cce1c7be855).(attr, NFTA_LIMIT_MAXcxx . . $ `include/linux/netfilter/nf_tables.h:1241:9`!) < 0) |
| 87 | return MNL_CB_OKcxx . . $ `<external>/12386019527593959648/libmnl.h:174:9`!; |
| 88 | |
| 89 | switch(type) { |
| 90 | case NFTA_LIMIT_RATEcxx . . $ NFTA_LIMIT_RATE.: |
| 91 | case NFTA_LIMIT_UNITcxx . . $ NFTA_LIMIT_UNIT.: |
| 92 | if (mnl_attr_validatecxx . . $ mnl_attr_validate(e419697a5e1ae2b1).(attr, MNL_TYPE_U64cxx . . $ MNL_TYPE_U64.) < 0) |
| 93 | abi_breakagecxx . . $ `include/utils.h:28:9`!(); |
| 94 | break; |
| 95 | case NFTA_LIMIT_BURSTcxx . . $ NFTA_LIMIT_BURST.: |
| 96 | case NFTA_LIMIT_TYPEcxx . . $ NFTA_LIMIT_TYPE.: |
| 97 | case NFTA_LIMIT_FLAGScxx . . $ NFTA_LIMIT_FLAGS.: |
| 98 | if (mnl_attr_validatecxx . . $ mnl_attr_validate(e419697a5e1ae2b1).(attr, MNL_TYPE_U32cxx . . $ MNL_TYPE_U32.) < 0) |
| 99 | abi_breakagecxx . . $ `include/utils.h:28:9`!(); |
| 100 | break; |
| 101 | } |
| 102 | |
| 103 | tb[type] = attr; |
| 104 | return MNL_CB_OKcxx . . $ `<external>/12386019527593959648/libmnl.h:174:9`!; |
| 105 | } |
| 106 | |
| 107 | static void |
| 108 | nftnl_expr_limit_buildcxx . . $ nftnl_expr_limit_build(93c7528a9cf90b41).(struct nlmsghdrcxx . . $ nlmsghdr# *nlh, const struct nftnl_exprcxx . . $ nftnl_expr# *e) |
| 109 | { |
| 110 | struct nftnl_expr_limitcxx . . $ nftnl_expr_limit# *limit = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 111 | |
| 112 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_LIMIT_RATEcxx . . $ NFTNL_EXPR_LIMIT_RATE.)) |
| 113 | mnl_attr_put_u64cxx . . $ mnl_attr_put_u64(6d753f4d486e3a2f).(nlh, NFTA_LIMIT_RATEcxx . . $ NFTA_LIMIT_RATE., htobe64cxx . . $ `<external>/15855202785537844739/endian.h:49:11`!(limit->rate)); |
| 114 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_LIMIT_UNITcxx . . $ NFTNL_EXPR_LIMIT_UNIT.)) |
| 115 | mnl_attr_put_u64cxx . . $ mnl_attr_put_u64(6d753f4d486e3a2f).(nlh, NFTA_LIMIT_UNITcxx . . $ NFTA_LIMIT_UNIT., htobe64cxx . . $ `<external>/15855202785537844739/endian.h:49:11`!(limit->unit)); |
| 116 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_LIMIT_BURSTcxx . . $ NFTNL_EXPR_LIMIT_BURST.)) |
| 117 | mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_LIMIT_BURSTcxx . . $ NFTA_LIMIT_BURST., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(limit->burst)); |
| 118 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_LIMIT_TYPEcxx . . $ NFTNL_EXPR_LIMIT_TYPE.)) |
| 119 | mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_LIMIT_TYPEcxx . . $ NFTA_LIMIT_TYPE., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(limit->type)); |
| 120 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_LIMIT_FLAGScxx . . $ NFTNL_EXPR_LIMIT_FLAGS.)) |
| 121 | mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_LIMIT_FLAGScxx . . $ NFTA_LIMIT_FLAGS., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(limit->flags)); |
| 122 | } |
| 123 | |
| 124 | static int |
| 125 | nftnl_expr_limit_parsecxx . . $ nftnl_expr_limit_parse(ed61207049d8af9a).(struct nftnl_exprcxx . . $ nftnl_expr# *e, struct nlattrcxx . . $ nlattr# *attr) |
| 126 | { |
| 127 | struct nftnl_expr_limitcxx . . $ nftnl_expr_limit# *limit = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 128 | struct nlattrcxx . . $ nlattr# *tb[NFTA_LIMIT_MAXcxx . . $ `include/linux/netfilter/nf_tables.h:1241:9`!+1] = {}; |
| 129 | |
| 130 | if (mnl_attr_parse_nestedcxx . . $ mnl_attr_parse_nested(f99c0e9fa94f99d9).(attr, nftnl_expr_limit_cbcxx . . $ nftnl_expr_limit_cb(de2ef18c48fd675c)., tb) < 0) |
| 131 | return -1; |
| 132 | |
| 133 | if (tb[NFTA_LIMIT_RATEcxx . . $ NFTA_LIMIT_RATE.]) { |
| 134 | limit->ratecxx . . $ nftnl_expr_limit#rate. = be64tohcxx . . $ `<external>/15855202785537844739/endian.h:51:11`!(mnl_attr_get_u64(tb[NFTA_LIMIT_RATE])); |
| 135 | e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_LIMIT_RATEcxx . . $ NFTNL_EXPR_LIMIT_RATE.); |
| 136 | } |
| 137 | if (tb[NFTA_LIMIT_UNITcxx . . $ NFTA_LIMIT_UNIT.]) { |
| 138 | limit->unitcxx . . $ nftnl_expr_limit#unit. = be64tohcxx . . $ `<external>/15855202785537844739/endian.h:51:11`!(mnl_attr_get_u64(tb[NFTA_LIMIT_UNIT])); |
| 139 | e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_LIMIT_UNITcxx . . $ NFTNL_EXPR_LIMIT_UNIT.); |
| 140 | } |
| 141 | if (tb[NFTA_LIMIT_BURSTcxx . . $ NFTA_LIMIT_BURST.]) { |
| 142 | limit->burstcxx . . $ nftnl_expr_limit#burst. = ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_LIMIT_BURST])); |
| 143 | e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_LIMIT_BURSTcxx . . $ NFTNL_EXPR_LIMIT_BURST.); |
| 144 | } |
| 145 | if (tb[NFTA_LIMIT_TYPEcxx . . $ NFTA_LIMIT_TYPE.]) { |
| 146 | limit->typecxx . . $ nftnl_expr_limit#type. = ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_LIMIT_TYPE])); |
| 147 | e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_LIMIT_TYPEcxx . . $ NFTNL_EXPR_LIMIT_TYPE.); |
| 148 | } |
| 149 | if (tb[NFTA_LIMIT_FLAGScxx . . $ NFTA_LIMIT_FLAGS.]) { |
| 150 | limit->flagscxx . . $ nftnl_expr_limit#flags. = ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_LIMIT_FLAGS])); |
| 151 | e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_LIMIT_FLAGScxx . . $ NFTNL_EXPR_LIMIT_FLAGS.); |
| 152 | } |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | static const char *get_timecxx . . $ get_time(b265e3ce8b0096c1).(uint64_tcxx . . $ uint64_t# seconds, uint64_tcxx . . $ uint64_t# *val) |
| 158 | { |
| 159 | static const structcxx . . $ get_time(b265e3ce8b0096c1).$anonymous_type_0# { |
| 160 | unsigned int sizecxx . . $ get_time(b265e3ce8b0096c1).$anonymous_type_0#size.; |
| 161 | const char *namecxx . . $ get_time(b265e3ce8b0096c1).$anonymous_type_0#name.; |
| 162 | } units[] = { |
| 163 | { 0, "second" }, |
| 164 | { 60, "minute" }, |
| 165 | { 60, "hour" }, |
| 166 | { 24, "day" }, |
| 167 | { 7, "week" } |
| 168 | }; |
| 169 | int i; |
| 170 | |
| 171 | for (i = 1; i < array_sizecxx . . $ `include/utils.h:69:9`!(units); i++) { |
| 172 | if (seconds % units[i].sizecxx . . $ get_time(b265e3ce8b0096c1).$anonymous_type_0#size.) |
| 173 | break; |
| 174 | seconds /= units[i].sizecxx . . $ get_time(b265e3ce8b0096c1).$anonymous_type_0#size.; |
| 175 | } |
| 176 | if (val) |
| 177 | *val = seconds; |
| 178 | return units[i - 1].namecxx . . $ get_time(b265e3ce8b0096c1).$anonymous_type_0#name.; |
| 179 | } |
| 180 | |
| 181 | static const char *limit_to_typecxx . . $ limit_to_type(91229ab62a76ecc6).(enum nft_limit_typecxx . . $ nft_limit_type# type) |
| 182 | { |
| 183 | switch (type) { |
| 184 | default: |
| 185 | case NFT_LIMIT_PKTScxx . . $ NFT_LIMIT_PKTS.: |
| 186 | return "packets"; |
| 187 | case NFT_LIMIT_PKT_BYTEScxx . . $ NFT_LIMIT_PKT_BYTES.: |
| 188 | return "bytes"; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | static int |
| 193 | nftnl_expr_limit_snprintfcxx . . $ nftnl_expr_limit_snprintf(2b4af69501aef780).(char *buf, size_t len, |
| 194 | uint32_tcxx . . $ uint32_t# flags, const struct nftnl_exprcxx . . $ nftnl_expr# *e) |
| 195 | { |
| 196 | struct nftnl_expr_limitcxx . . $ nftnl_expr_limit# *limit = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 197 | unsigned int offset = 0; |
| 198 | const char *time_unit; |
| 199 | uint64_tcxx . . $ uint64_t# time_val; |
| 200 | int ret; |
| 201 | |
| 202 | ret = snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf, len, "rate %"PRIu64cxx . . $ `<external>/15933738808807975373/inttypes.h:110:10`!"/", limit->ratecxx . . $ nftnl_expr_limit#rate.); |
| 203 | SNPRINTF_BUFFER_SIZEcxx . . $ `include/utils.h:55:9`!(ret, len, offset); |
| 204 | |
| 205 | time_unit = get_timecxx . . $ get_time(b265e3ce8b0096c1).(limit->unitcxx . . $ nftnl_expr_limit#unit., &time_val); |
| 206 | if (time_val > 1) { |
| 207 | ret = snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf + offset, len, "%"PRIu64cxx . . $ `<external>/15933738808807975373/inttypes.h:110:10`!" ", time_val); |
| 208 | SNPRINTF_BUFFER_SIZEcxx . . $ `include/utils.h:55:9`!(ret, len, offset); |
| 209 | } |
| 210 | |
| 211 | ret = snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf + offset, len, "%s burst %u type %s flags 0x%x ", |
| 212 | time_unit, limit->burstcxx . . $ nftnl_expr_limit#burst., limit_to_typecxx . . $ limit_to_type(91229ab62a76ecc6).(limit->typecxx . . $ nftnl_expr_limit#type.), |
| 213 | limit->flagscxx . . $ nftnl_expr_limit#flags.); |
| 214 | SNPRINTF_BUFFER_SIZEcxx . . $ `include/utils.h:55:9`!(ret, len, offset); |
| 215 | |
| 216 | return offset; |
| 217 | } |
| 218 | |
| 219 | static struct attr_policycxx . . $ attr_policy# limit_attr_policycxx . . $ limit_attr_policy.[__NFTNL_EXPR_LIMIT_MAXcxx . . $ __NFTNL_EXPR_LIMIT_MAX.] = { |
| 220 | [NFTNL_EXPR_LIMIT_RATEcxx . . $ NFTNL_EXPR_LIMIT_RATE.] = { .maxlen = sizeof(uint64_tcxx . . $ uint64_t#) }, |
| 221 | [NFTNL_EXPR_LIMIT_UNITcxx . . $ NFTNL_EXPR_LIMIT_UNIT.] = { .maxlen = sizeof(uint64_tcxx . . $ uint64_t#) }, |
| 222 | [NFTNL_EXPR_LIMIT_BURSTcxx . . $ NFTNL_EXPR_LIMIT_BURST.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) }, |
| 223 | [NFTNL_EXPR_LIMIT_TYPEcxx . . $ NFTNL_EXPR_LIMIT_TYPE.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) }, |
| 224 | [NFTNL_EXPR_LIMIT_FLAGScxx . . $ NFTNL_EXPR_LIMIT_FLAGS.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) }, |
| 225 | }; |
| 226 | |
| 227 | struct expr_opscxx . . $ expr_ops# expr_ops_limitcxx . . $ expr_ops_limit. = { |
| 228 | .name = "limit", |
| 229 | .alloc_len = sizeof(struct nftnl_expr_limitcxx . . $ nftnl_expr_limit#), |
| 230 | .nftnl_max_attr = __NFTNL_EXPR_LIMIT_MAXcxx . . $ __NFTNL_EXPR_LIMIT_MAX. - 1, |
| 231 | .attr_policy = limit_attr_policycxx . . $ limit_attr_policy., |
| 232 | .set = nftnl_expr_limit_setcxx . . $ nftnl_expr_limit_set(211371a3e3842127)., |
| 233 | .get = nftnl_expr_limit_getcxx . . $ nftnl_expr_limit_get(9de263c4f9e71c4b)., |
| 234 | .parse = nftnl_expr_limit_parsecxx . . $ nftnl_expr_limit_parse(ed61207049d8af9a)., |
| 235 | .build = nftnl_expr_limit_buildcxx . . $ nftnl_expr_limit_build(93c7528a9cf90b41)., |
| 236 | .output = nftnl_expr_limit_snprintfcxx . . $ nftnl_expr_limit_snprintf(2b4af69501aef780)., |
| 237 | }; |