| 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 | /* |
| 3 | * (C) 2014 by Arturo Borrero Gonzalez <[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/expr.h> |
| 17 | #include <libnftnl/rule.h> |
| 18 | |
| 19 | struct nftnl_expr_masq { |
| 20 | uint32_t flags; |
| 21 | enum nft_registers sreg_proto_min; |
| 22 | enum nft_registers sreg_proto_max; |
| 23 | }; |
| 24 | |
| 25 | static int |
| 26 | nftnl_expr_masq_set(struct nftnl_expr *e, uint16_t type, |
| 27 | const void *data, uint32_t data_len, uint32_t byteorder) |
| 28 | { |
| 29 | struct nftnl_expr_masq *masq = nftnl_expr_data(e); |
| 30 | |
| 31 | switch (type) { |
| 32 | case NFTNL_EXPR_MASQ_FLAGS: |
| 33 | memcpy(&masq->flags, data, data_len); |
| 34 | break; |
| 35 | case NFTNL_EXPR_MASQ_REG_PROTO_MIN: |
| 36 | memcpy(&masq->sreg_proto_min, data, data_len); |
| 37 | break; |
| 38 | case NFTNL_EXPR_MASQ_REG_PROTO_MAX: |
| 39 | memcpy(&masq->sreg_proto_max, data, data_len); |
| 40 | break; |
| 41 | } |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | static const void * |
| 46 | nftnl_expr_masq_get(const struct nftnl_expr *e, uint16_t type, |
| 47 | uint32_t *data_len) |
| 48 | { |
| 49 | struct nftnl_expr_masq *masq = nftnl_expr_data(e); |
| 50 | |
| 51 | switch (type) { |
| 52 | case NFTNL_EXPR_MASQ_FLAGS: |
| 53 | *data_len = sizeof(masq->flags); |
| 54 | return &masq->flags; |
| 55 | case NFTNL_EXPR_MASQ_REG_PROTO_MIN: |
| 56 | *data_len = sizeof(masq->sreg_proto_min); |
| 57 | return &masq->sreg_proto_min; |
| 58 | case NFTNL_EXPR_MASQ_REG_PROTO_MAX: |
| 59 | *data_len = sizeof(masq->sreg_proto_max); |
| 60 | return &masq->sreg_proto_max; |
| 61 | } |
| 62 | return NULL; |
| 63 | } |
| 64 | |
| 65 | static int nftnl_expr_masq_cb(const struct nlattr *attr, void *data) |
| 66 | { |
| 67 | const struct nlattr **tb = data; |
| 68 | int type = mnl_attr_get_type(attr); |
| 69 | |
| 70 | if (mnl_attr_type_valid(attr, NFTA_MASQ_MAX) < 0) |
| 71 | return MNL_CB_OK; |
| 72 | |
| 73 | switch (type) { |
| 74 | case NFTA_MASQ_REG_PROTO_MIN: |
| 75 | case NFTA_MASQ_REG_PROTO_MAX: |
| 76 | case NFTA_MASQ_FLAGS: |
| 77 | if (mnl_attr_validate(attr, MNL_TYPE_U32) < 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_masq_build(struct nlmsghdr *nlh, const struct nftnl_expr *e) |
| 88 | { |
| 89 | struct nftnl_expr_masq *masq = nftnl_expr_data(e); |
| 90 | |
| 91 | if (e->flags & (1 << NFTNL_EXPR_MASQ_FLAGS)) |
| 92 | mnl_attr_put_u32(nlh, NFTA_MASQ_FLAGS, htobe32(masq->flags)); |
| 93 | if (e->flags & (1 << NFTNL_EXPR_MASQ_REG_PROTO_MIN)) |
| 94 | mnl_attr_put_u32(nlh, NFTA_MASQ_REG_PROTO_MIN, |
| 95 | htobe32(masq->sreg_proto_min)); |
| 96 | if (e->flags & (1 << NFTNL_EXPR_MASQ_REG_PROTO_MAX)) |
| 97 | mnl_attr_put_u32(nlh, NFTA_MASQ_REG_PROTO_MAX, |
| 98 | htobe32(masq->sreg_proto_max)); |
| 99 | } |
| 100 | |
| 101 | static int |
| 102 | nftnl_expr_masq_parse(struct nftnl_expr *e, struct nlattr *attr) |
| 103 | { |
| 104 | struct nftnl_expr_masq *masq = nftnl_expr_data(e); |
| 105 | struct nlattr *tb[NFTA_MASQ_MAX+1] = {}; |
| 106 | |
| 107 | if (mnl_attr_parse_nested(attr, nftnl_expr_masq_cb, tb) < 0) |
| 108 | return -1; |
| 109 | |
| 110 | if (tb[NFTA_MASQ_FLAGS]) { |
| 111 | masq->flags = be32toh(mnl_attr_get_u32(tb[NFTA_MASQ_FLAGS])); |
| 112 | e->flags |= (1 << NFTNL_EXPR_MASQ_FLAGS); |
| 113 | } |
| 114 | if (tb[NFTA_MASQ_REG_PROTO_MIN]) { |
| 115 | masq->sreg_proto_min = |
| 116 | be32toh(mnl_attr_get_u32(tb[NFTA_MASQ_REG_PROTO_MIN])); |
| 117 | e->flags |= (1 << NFTNL_EXPR_MASQ_REG_PROTO_MIN); |
| 118 | } |
| 119 | if (tb[NFTA_MASQ_REG_PROTO_MAX]) { |
| 120 | masq->sreg_proto_max = |
| 121 | be32toh(mnl_attr_get_u32(tb[NFTA_MASQ_REG_PROTO_MAX])); |
| 122 | e->flags |= (1 << NFTNL_EXPR_MASQ_REG_PROTO_MAX); |
| 123 | } |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static int nftnl_expr_masq_snprintf(char *buf, size_t remain, |
| 129 | uint32_t flags, const struct nftnl_expr *e) |
| 130 | { |
| 131 | struct nftnl_expr_masq *masq = nftnl_expr_data(e); |
| 132 | int offset = 0, ret = 0; |
| 133 | |
| 134 | if (e->flags & (1 << NFTNL_EXPR_MASQ_REG_PROTO_MIN)) { |
| 135 | ret = snprintf(buf + offset, remain, "proto_min reg %u ", |
| 136 | masq->sreg_proto_min); |
| 137 | SNPRINTF_BUFFER_SIZE(ret, remain, offset); |
| 138 | } |
| 139 | if (e->flags & (1 << NFTNL_EXPR_MASQ_REG_PROTO_MAX)) { |
| 140 | ret = snprintf(buf + offset, remain, "proto_max reg %u ", |
| 141 | masq->sreg_proto_max); |
| 142 | SNPRINTF_BUFFER_SIZE(ret, remain, offset); |
| 143 | } |
| 144 | if (e->flags & (1 << NFTNL_EXPR_MASQ_FLAGS)) { |
| 145 | ret = snprintf(buf + offset, remain, "flags 0x%x ", masq->flags); |
| 146 | SNPRINTF_BUFFER_SIZE(ret, remain, offset); |
| 147 | } |
| 148 | |
| 149 | return offset; |
| 150 | } |
| 151 | |
| 152 | static struct attr_policy masq_attr_policy[__NFTNL_EXPR_MASQ_MAX] = { |
| 153 | [NFTNL_EXPR_MASQ_FLAGS] = { .maxlen = sizeof(uint32_t) }, |
| 154 | [NFTNL_EXPR_MASQ_REG_PROTO_MIN] = { .maxlen = sizeof(uint32_t) }, |
| 155 | [NFTNL_EXPR_MASQ_REG_PROTO_MAX] = { .maxlen = sizeof(uint32_t) }, |
| 156 | }; |
| 157 | |
| 158 | struct expr_ops expr_ops_masq = { |
| 159 | .name = "masq", |
| 160 | .alloc_len = sizeof(struct nftnl_expr_masq), |
| 161 | .nftnl_max_attr = __NFTNL_EXPR_MASQ_MAX - 1, |
| 162 | .attr_policy = masq_attr_policy, |
| 163 | .set = nftnl_expr_masq_set, |
| 164 | .get = nftnl_expr_masq_get, |
| 165 | .parse = nftnl_expr_masq_parse, |
| 166 | .build = nftnl_expr_masq_build, |
| 167 | .output = nftnl_expr_masq_snprintf, |
| 168 | }; |