| 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 | /* |
| 3 | * (C) 2015 Pablo Neira Ayuso <[email protected]> |
| 4 | */ |
| 5 | |
| 6 | #include <stdio.h> |
| 7 | #include <stdint.h> |
| 8 | #include <string.h> |
| 9 | #include <arpa/inet.h> |
| 10 | #include <errno.h> |
| 11 | #include "internal.h" |
| 12 | #include <libmnl/libmnl.h> |
| 13 | #include <linux/netfilter/nf_tables.h> |
| 14 | #include <libnftnl/expr.h> |
| 15 | #include <libnftnl/rule.h> |
| 16 | #include "expr_ops.h" |
| 17 | #include "data_reg.h" |
| 18 | |
| 19 | struct nftnl_expr_fwd { |
| 20 | enum nft_registers sreg_dev; |
| 21 | enum nft_registers sreg_addr; |
| 22 | uint32_t nfproto; |
| 23 | }; |
| 24 | |
| 25 | static int |
| 26 | nftnl_expr_fwd_set(struct nftnl_expr *e, uint16_t type, |
| 27 | const void *data, uint32_t data_len, uint32_t byteorder) |
| 28 | { |
| 29 | struct nftnl_expr_fwd *fwd = nftnl_expr_data(e); |
| 30 | |
| 31 | switch (type) { |
| 32 | case NFTNL_EXPR_FWD_SREG_DEV: |
| 33 | memcpy(&fwd->sreg_dev, data, data_len); |
| 34 | break; |
| 35 | case NFTNL_EXPR_FWD_SREG_ADDR: |
| 36 | memcpy(&fwd->sreg_addr, data, data_len); |
| 37 | break; |
| 38 | case NFTNL_EXPR_FWD_NFPROTO: |
| 39 | memcpy(&fwd->nfproto, data, data_len); |
| 40 | break; |
| 41 | } |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | static const void *nftnl_expr_fwd_get(const struct nftnl_expr *e, |
| 46 | uint16_t type, uint32_t *data_len) |
| 47 | { |
| 48 | struct nftnl_expr_fwd *fwd = nftnl_expr_data(e); |
| 49 | |
| 50 | switch (type) { |
| 51 | case NFTNL_EXPR_FWD_SREG_DEV: |
| 52 | *data_len = sizeof(fwd->sreg_dev); |
| 53 | return &fwd->sreg_dev; |
| 54 | case NFTNL_EXPR_FWD_SREG_ADDR: |
| 55 | *data_len = sizeof(fwd->sreg_addr); |
| 56 | return &fwd->sreg_addr; |
| 57 | case NFTNL_EXPR_FWD_NFPROTO: |
| 58 | *data_len = sizeof(fwd->nfproto); |
| 59 | return &fwd->nfproto; |
| 60 | } |
| 61 | return NULL; |
| 62 | } |
| 63 | |
| 64 | static int nftnl_expr_fwd_cb(const struct nlattr *attr, void *data) |
| 65 | { |
| 66 | const struct nlattr **tb = data; |
| 67 | int type = mnl_attr_get_type(attr); |
| 68 | |
| 69 | if (mnl_attr_type_valid(attr, NFTA_FWD_MAX) < 0) |
| 70 | return MNL_CB_OK; |
| 71 | |
| 72 | switch (type) { |
| 73 | case NFTA_FWD_SREG_DEV: |
| 74 | case NFTA_FWD_SREG_ADDR: |
| 75 | case NFTA_FWD_NFPROTO: |
| 76 | if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) |
| 77 | abi_breakage(); |
| 78 | break; |
| 79 | } |
| 80 | |
| 81 | tb[type] = attr; |
| 82 | return MNL_CB_OK; |
| 83 | } |
| 84 | |
| 85 | static void nftnl_expr_fwd_build(struct nlmsghdr *nlh, |
| 86 | const struct nftnl_expr *e) |
| 87 | { |
| 88 | struct nftnl_expr_fwd *fwd = nftnl_expr_data(e); |
| 89 | |
| 90 | if (e->flags & (1 << NFTNL_EXPR_FWD_SREG_DEV)) |
| 91 | mnl_attr_put_u32(nlh, NFTA_FWD_SREG_DEV, htonl(fwd->sreg_dev)); |
| 92 | if (e->flags & (1 << NFTNL_EXPR_FWD_SREG_ADDR)) |
| 93 | mnl_attr_put_u32(nlh, NFTA_FWD_SREG_ADDR, htonl(fwd->sreg_addr)); |
| 94 | if (e->flags & (1 << NFTNL_EXPR_FWD_NFPROTO)) |
| 95 | mnl_attr_put_u32(nlh, NFTA_FWD_NFPROTO, htonl(fwd->nfproto)); |
| 96 | } |
| 97 | |
| 98 | static int nftnl_expr_fwd_parse(struct nftnl_expr *e, struct nlattr *attr) |
| 99 | { |
| 100 | struct nftnl_expr_fwd *fwd = nftnl_expr_data(e); |
| 101 | struct nlattr *tb[NFTA_FWD_MAX + 1] = {}; |
| 102 | int ret = 0; |
| 103 | |
| 104 | if (mnl_attr_parse_nested(attr, nftnl_expr_fwd_cb, tb) < 0) |
| 105 | return -1; |
| 106 | |
| 107 | if (tb[NFTA_FWD_SREG_DEV]) { |
| 108 | fwd->sreg_dev = ntohl(mnl_attr_get_u32(tb[NFTA_FWD_SREG_DEV])); |
| 109 | e->flags |= (1 << NFTNL_EXPR_FWD_SREG_DEV); |
| 110 | } |
| 111 | if (tb[NFTA_FWD_SREG_ADDR]) { |
| 112 | fwd->sreg_addr = ntohl(mnl_attr_get_u32(tb[NFTA_FWD_SREG_ADDR])); |
| 113 | e->flags |= (1 << NFTNL_EXPR_FWD_SREG_ADDR); |
| 114 | } |
| 115 | if (tb[NFTA_FWD_NFPROTO]) { |
| 116 | fwd->nfproto = ntohl(mnl_attr_get_u32(tb[NFTA_FWD_NFPROTO])); |
| 117 | e->flags |= (1 << NFTNL_EXPR_FWD_NFPROTO); |
| 118 | } |
| 119 | |
| 120 | return ret; |
| 121 | } |
| 122 | |
| 123 | static int nftnl_expr_fwd_snprintf(char *buf, size_t remain, |
| 124 | uint32_t flags, const struct nftnl_expr *e) |
| 125 | { |
| 126 | struct nftnl_expr_fwd *fwd = nftnl_expr_data(e); |
| 127 | int offset = 0, ret; |
| 128 | |
| 129 | if (e->flags & (1 << NFTNL_EXPR_FWD_SREG_DEV)) { |
| 130 | ret = snprintf(buf + offset, remain, "sreg_dev %u ", |
| 131 | fwd->sreg_dev); |
| 132 | SNPRINTF_BUFFER_SIZE(ret, remain, offset); |
| 133 | } |
| 134 | if (e->flags & (1 << NFTNL_EXPR_FWD_SREG_ADDR)) { |
| 135 | ret = snprintf(buf + offset, remain, "sreg_addr %u ", |
| 136 | fwd->sreg_addr); |
| 137 | SNPRINTF_BUFFER_SIZE(ret, remain, offset); |
| 138 | } |
| 139 | if (e->flags & (1 << NFTNL_EXPR_FWD_NFPROTO)) { |
| 140 | ret = snprintf(buf + offset, remain, "nfproto %u ", |
| 141 | fwd->nfproto); |
| 142 | SNPRINTF_BUFFER_SIZE(ret, remain, offset); |
| 143 | } |
| 144 | |
| 145 | return offset; |
| 146 | } |
| 147 | |
| 148 | static struct attr_policy fwd_attr_policy[__NFTNL_EXPR_FWD_MAX] = { |
| 149 | [NFTNL_EXPR_FWD_SREG_DEV] = { .maxlen = sizeof(uint32_t) }, |
| 150 | [NFTNL_EXPR_FWD_SREG_ADDR] = { .maxlen = sizeof(uint32_t) }, |
| 151 | [NFTNL_EXPR_FWD_NFPROTO] = { .maxlen = sizeof(uint32_t) }, |
| 152 | }; |
| 153 | |
| 154 | struct expr_ops expr_ops_fwd = { |
| 155 | .name = "fwd", |
| 156 | .alloc_len = sizeof(struct nftnl_expr_fwd), |
| 157 | .nftnl_max_attr = __NFTNL_EXPR_FWD_MAX - 1, |
| 158 | .attr_policy = fwd_attr_policy, |
| 159 | .set = nftnl_expr_fwd_set, |
| 160 | .get = nftnl_expr_fwd_get, |
| 161 | .parse = nftnl_expr_fwd_parse, |
| 162 | .build = nftnl_expr_fwd_build, |
| 163 | .output = nftnl_expr_fwd_snprintf, |
| 164 | }; |