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