| 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 | /* |
| 3 | * (C) 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 <string.h> |
| 11 | #include <arpa/inet.h> |
| 12 | #include <errno.h> |
| 13 | #include <linux/netfilter/nf_tables.h> |
| 14 | |
| 15 | #include "internal.h" |
| 16 | #include <libmnl/libmnl.h> |
| 17 | #include <libnftnl/expr.h> |
| 18 | #include <libnftnl/rule.h> |
| 19 | |
| 20 | struct nftnl_expr_reject { |
| 21 | uint32_t type; |
| 22 | uint8_t icmp_code; |
| 23 | }; |
| 24 | |
| 25 | static int |
| 26 | nftnl_expr_reject_set(struct nftnl_expr *e, uint16_t type, |
| 27 | const void *data, uint32_t data_len, uint32_t byteorder) |
| 28 | { |
| 29 | struct nftnl_expr_reject *reject = nftnl_expr_data(e); |
| 30 | |
| 31 | switch(type) { |
| 32 | case NFTNL_EXPR_REJECT_TYPE: |
| 33 | memcpy(&reject->type, data, data_len); |
| 34 | break; |
| 35 | case NFTNL_EXPR_REJECT_CODE: |
| 36 | memcpy(&reject->icmp_code, data, data_len); |
| 37 | break; |
| 38 | } |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | static const void * |
| 43 | nftnl_expr_reject_get(const struct nftnl_expr *e, uint16_t type, |
| 44 | uint32_t *data_len) |
| 45 | { |
| 46 | struct nftnl_expr_reject *reject = nftnl_expr_data(e); |
| 47 | |
| 48 | switch(type) { |
| 49 | case NFTNL_EXPR_REJECT_TYPE: |
| 50 | *data_len = sizeof(reject->type); |
| 51 | return &reject->type; |
| 52 | case NFTNL_EXPR_REJECT_CODE: |
| 53 | *data_len = sizeof(reject->icmp_code); |
| 54 | return &reject->icmp_code; |
| 55 | } |
| 56 | return NULL; |
| 57 | } |
| 58 | |
| 59 | static int nftnl_expr_reject_cb(const struct nlattr *attr, void *data) |
| 60 | { |
| 61 | const struct nlattr **tb = data; |
| 62 | int type = mnl_attr_get_type(attr); |
| 63 | |
| 64 | if (mnl_attr_type_valid(attr, NFTA_REJECT_MAX) < 0) |
| 65 | return MNL_CB_OK; |
| 66 | |
| 67 | switch(type) { |
| 68 | case NFTA_REJECT_TYPE: |
| 69 | if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) |
| 70 | abi_breakage(); |
| 71 | break; |
| 72 | case NFTA_REJECT_ICMP_CODE: |
| 73 | if (mnl_attr_validate(attr, MNL_TYPE_U8) < 0) |
| 74 | abi_breakage(); |
| 75 | break; |
| 76 | } |
| 77 | |
| 78 | tb[type] = attr; |
| 79 | return MNL_CB_OK; |
| 80 | } |
| 81 | |
| 82 | static void |
| 83 | nftnl_expr_reject_build(struct nlmsghdr *nlh, const struct nftnl_expr *e) |
| 84 | { |
| 85 | struct nftnl_expr_reject *reject = nftnl_expr_data(e); |
| 86 | |
| 87 | if (e->flags & (1 << NFTNL_EXPR_REJECT_TYPE)) |
| 88 | mnl_attr_put_u32(nlh, NFTA_REJECT_TYPE, htonl(reject->type)); |
| 89 | if (e->flags & (1 << NFTNL_EXPR_REJECT_CODE)) |
| 90 | mnl_attr_put_u8(nlh, NFTA_REJECT_ICMP_CODE, reject->icmp_code); |
| 91 | } |
| 92 | |
| 93 | static int |
| 94 | nftnl_expr_reject_parse(struct nftnl_expr *e, struct nlattr *attr) |
| 95 | { |
| 96 | struct nftnl_expr_reject *reject = nftnl_expr_data(e); |
| 97 | struct nlattr *tb[NFTA_REJECT_MAX+1] = {}; |
| 98 | |
| 99 | if (mnl_attr_parse_nested(attr, nftnl_expr_reject_cb, tb) < 0) |
| 100 | return -1; |
| 101 | |
| 102 | if (tb[NFTA_REJECT_TYPE]) { |
| 103 | reject->type = ntohl(mnl_attr_get_u32(tb[NFTA_REJECT_TYPE])); |
| 104 | e->flags |= (1 << NFTNL_EXPR_REJECT_TYPE); |
| 105 | } |
| 106 | if (tb[NFTA_REJECT_ICMP_CODE]) { |
| 107 | reject->icmp_code = mnl_attr_get_u8(tb[NFTA_REJECT_ICMP_CODE]); |
| 108 | e->flags |= (1 << NFTNL_EXPR_REJECT_CODE); |
| 109 | } |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static int |
| 115 | nftnl_expr_reject_snprintf(char *buf, size_t len, |
| 116 | uint32_t flags, const struct nftnl_expr *e) |
| 117 | { |
| 118 | struct nftnl_expr_reject *reject = nftnl_expr_data(e); |
| 119 | |
| 120 | return snprintf(buf, len, "type %u code %u ", |
| 121 | reject->type, reject->icmp_code); |
| 122 | } |
| 123 | |
| 124 | static struct attr_policy reject_attr_policy[__NFTNL_EXPR_REJECT_MAX] = { |
| 125 | [NFTNL_EXPR_REJECT_TYPE] = { .maxlen = sizeof(uint32_t) }, |
| 126 | [NFTNL_EXPR_REJECT_CODE] = { .maxlen = sizeof(uint8_t) }, |
| 127 | }; |
| 128 | |
| 129 | struct expr_ops expr_ops_reject = { |
| 130 | .name = "reject", |
| 131 | .alloc_len = sizeof(struct nftnl_expr_reject), |
| 132 | .nftnl_max_attr = __NFTNL_EXPR_REJECT_MAX - 1, |
| 133 | .attr_policy = reject_attr_policy, |
| 134 | .set = nftnl_expr_reject_set, |
| 135 | .get = nftnl_expr_reject_get, |
| 136 | .parse = nftnl_expr_reject_parse, |
| 137 | .build = nftnl_expr_reject_build, |
| 138 | .output = nftnl_expr_reject_snprintf, |
| 139 | }; |