| 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */cxx . . $ `<file>/src/expr/lookup.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 "internal.h"cxx . . $ `<file>/include/internal.h`/ |
| 9 | |
| 10 | #include <stdio.h> |
| 11 | #include <stdint.h> |
| 12 | #include <string.h> /* for memcpy */ |
| 13 | #include <arpa/inet.h> |
| 14 | #include <errno.h> |
| 15 | #include <libmnl/libmnl.h> |
| 16 | #include <linux/netfilter/nf_tables.h>cxx . . $ `<file>/include/linux/netfilter/nf_tables.h`/ |
| 17 | #include <libnftnl/rule.h>cxx . . $ `<file>/include/libnftnl/rule.h`/ |
| 18 | #include <libnftnl/expr.h>cxx . . $ `<file>/include/libnftnl/expr.h`/ |
| 19 | |
| 20 | struct nftnl_expr_lookupcxx . . $ nftnl_expr_lookup# { |
| 21 | enum nft_registerscxx . . $ nft_registers# sregcxx . . $ nftnl_expr_lookup#sreg.; |
| 22 | enum nft_registerscxx . . $ nft_registers# dregcxx . . $ nftnl_expr_lookup#dreg.; |
| 23 | char *set_namecxx . . $ nftnl_expr_lookup#set_name.; |
| 24 | uint32_tcxx . . $ uint32_t# set_idcxx . . $ nftnl_expr_lookup#set_id.; |
| 25 | uint32_tcxx . . $ uint32_t# flagscxx . . $ nftnl_expr_lookup#flags.; |
| 26 | }; |
| 27 | |
| 28 | static int |
| 29 | nftnl_expr_lookup_setcxx . . $ nftnl_expr_lookup_set(211371a3e3842127).(struct nftnl_exprcxx . . $ nftnl_expr# *e, uint16_tcxx . . $ uint16_t# type, |
| 30 | const void *data, uint32_tcxx . . $ uint32_t# data_len, uint32_tcxx . . $ uint32_t# byteorder) |
| 31 | { |
| 32 | struct nftnl_expr_lookupcxx . . $ nftnl_expr_lookup# *lookup = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 33 | |
| 34 | switch(type) { |
| 35 | case NFTNL_EXPR_LOOKUP_SREGcxx . . $ NFTNL_EXPR_LOOKUP_SREG.: |
| 36 | memcpycxx . . $ memcpy(cc817cb175f282d).(&lookup->sregcxx . . $ nftnl_expr_lookup#sreg., data, data_len); |
| 37 | break; |
| 38 | case NFTNL_EXPR_LOOKUP_DREGcxx . . $ NFTNL_EXPR_LOOKUP_DREG.: |
| 39 | memcpycxx . . $ memcpy(cc817cb175f282d).(&lookup->dregcxx . . $ nftnl_expr_lookup#dreg., data, data_len); |
| 40 | break; |
| 41 | case NFTNL_EXPR_LOOKUP_SETcxx . . $ NFTNL_EXPR_LOOKUP_SET.: |
| 42 | lookup->set_namecxx . . $ nftnl_expr_lookup#set_name. = strdupcxx . . $ strdup(a8b8e32347182212).((const char *)data); |
| 43 | if (!lookup->set_namecxx . . $ nftnl_expr_lookup#set_name.) |
| 44 | return -1; |
| 45 | break; |
| 46 | case NFTNL_EXPR_LOOKUP_SET_IDcxx . . $ NFTNL_EXPR_LOOKUP_SET_ID.: |
| 47 | memcpycxx . . $ memcpy(cc817cb175f282d).(&lookup->set_idcxx . . $ nftnl_expr_lookup#set_id., data, data_len); |
| 48 | break; |
| 49 | case NFTNL_EXPR_LOOKUP_FLAGScxx . . $ NFTNL_EXPR_LOOKUP_FLAGS.: |
| 50 | memcpycxx . . $ memcpy(cc817cb175f282d).(&lookup->flagscxx . . $ nftnl_expr_lookup#flags., data, data_len); |
| 51 | break; |
| 52 | } |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static const void * |
| 57 | nftnl_expr_lookup_getcxx . . $ nftnl_expr_lookup_get(9de263c4f9e71c4b).(const struct nftnl_exprcxx . . $ nftnl_expr# *e, uint16_tcxx . . $ uint16_t# type, |
| 58 | uint32_tcxx . . $ uint32_t# *data_len) |
| 59 | { |
| 60 | struct nftnl_expr_lookupcxx . . $ nftnl_expr_lookup# *lookup = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 61 | |
| 62 | switch(type) { |
| 63 | case NFTNL_EXPR_LOOKUP_SREGcxx . . $ NFTNL_EXPR_LOOKUP_SREG.: |
| 64 | *data_len = sizeof(lookup->sregcxx . . $ nftnl_expr_lookup#sreg.); |
| 65 | return &lookup->sregcxx . . $ nftnl_expr_lookup#sreg.; |
| 66 | case NFTNL_EXPR_LOOKUP_DREGcxx . . $ NFTNL_EXPR_LOOKUP_DREG.: |
| 67 | *data_len = sizeof(lookup->dregcxx . . $ nftnl_expr_lookup#dreg.); |
| 68 | return &lookup->dregcxx . . $ nftnl_expr_lookup#dreg.; |
| 69 | case NFTNL_EXPR_LOOKUP_SETcxx . . $ NFTNL_EXPR_LOOKUP_SET.: |
| 70 | *data_len = strlencxx . . $ strlen(751346f8406fd082).(lookup->set_namecxx . . $ nftnl_expr_lookup#set_name.) + 1; |
| 71 | return lookup->set_namecxx . . $ nftnl_expr_lookup#set_name.; |
| 72 | case NFTNL_EXPR_LOOKUP_SET_IDcxx . . $ NFTNL_EXPR_LOOKUP_SET_ID.: |
| 73 | *data_len = sizeof(lookup->set_idcxx . . $ nftnl_expr_lookup#set_id.); |
| 74 | return &lookup->set_idcxx . . $ nftnl_expr_lookup#set_id.; |
| 75 | case NFTNL_EXPR_LOOKUP_FLAGScxx . . $ NFTNL_EXPR_LOOKUP_FLAGS.: |
| 76 | *data_len = sizeof(lookup->flagscxx . . $ nftnl_expr_lookup#flags.); |
| 77 | return &lookup->flagscxx . . $ nftnl_expr_lookup#flags.; |
| 78 | } |
| 79 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 80 | } |
| 81 | |
| 82 | static int nftnl_expr_lookup_cbcxx . . $ nftnl_expr_lookup_cb(de2ef18c48fd675c).(const struct nlattrcxx . . $ nlattr# *attr, void *data) |
| 83 | { |
| 84 | const struct nlattrcxx . . $ nlattr# **tb = data; |
| 85 | int type = mnl_attr_get_typecxx . . $ mnl_attr_get_type(1da47bcc4c4b975).(attr); |
| 86 | |
| 87 | if (mnl_attr_type_validcxx . . $ mnl_attr_type_valid(47df1cce1c7be855).(attr, NFTA_LOOKUP_MAXcxx . . $ `include/linux/netfilter/nf_tables.h:749:9`!) < 0) |
| 88 | return MNL_CB_OKcxx . . $ `<external>/12386019527593959648/libmnl.h:174:9`!; |
| 89 | |
| 90 | switch(type) { |
| 91 | case NFTA_LOOKUP_SREGcxx . . $ NFTA_LOOKUP_SREG.: |
| 92 | case NFTA_LOOKUP_DREGcxx . . $ NFTA_LOOKUP_DREG.: |
| 93 | case NFTA_LOOKUP_SET_IDcxx . . $ NFTA_LOOKUP_SET_ID.: |
| 94 | case NFTA_LOOKUP_FLAGScxx . . $ NFTA_LOOKUP_FLAGS.: |
| 95 | if (mnl_attr_validatecxx . . $ mnl_attr_validate(e419697a5e1ae2b1).(attr, MNL_TYPE_U32cxx . . $ MNL_TYPE_U32.) < 0) |
| 96 | abi_breakagecxx . . $ `include/utils.h:28:9`!(); |
| 97 | break; |
| 98 | case NFTA_LOOKUP_SETcxx . . $ NFTA_LOOKUP_SET.: |
| 99 | if (mnl_attr_validatecxx . . $ mnl_attr_validate(e419697a5e1ae2b1).(attr, MNL_TYPE_STRINGcxx . . $ MNL_TYPE_STRING.) < 0) |
| 100 | abi_breakagecxx . . $ `include/utils.h:28:9`!(); |
| 101 | break; |
| 102 | } |
| 103 | |
| 104 | tb[type] = attr; |
| 105 | return MNL_CB_OKcxx . . $ `<external>/12386019527593959648/libmnl.h:174:9`!; |
| 106 | } |
| 107 | |
| 108 | static void |
| 109 | nftnl_expr_lookup_buildcxx . . $ nftnl_expr_lookup_build(93c7528a9cf90b41).(struct nlmsghdrcxx . . $ nlmsghdr# *nlh, const struct nftnl_exprcxx . . $ nftnl_expr# *e) |
| 110 | { |
| 111 | struct nftnl_expr_lookupcxx . . $ nftnl_expr_lookup# *lookup = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 112 | |
| 113 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_LOOKUP_SREGcxx . . $ NFTNL_EXPR_LOOKUP_SREG.)) |
| 114 | mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_LOOKUP_SREGcxx . . $ NFTA_LOOKUP_SREG., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(lookup->sreg)); |
| 115 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_LOOKUP_DREGcxx . . $ NFTNL_EXPR_LOOKUP_DREG.)) |
| 116 | mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_LOOKUP_DREGcxx . . $ NFTA_LOOKUP_DREG., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(lookup->dreg)); |
| 117 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_LOOKUP_SETcxx . . $ NFTNL_EXPR_LOOKUP_SET.)) |
| 118 | mnl_attr_put_strzcxx . . $ mnl_attr_put_strz(eb76286d4bfc1a8d).(nlh, NFTA_LOOKUP_SETcxx . . $ NFTA_LOOKUP_SET., lookup->set_namecxx . . $ nftnl_expr_lookup#set_name.); |
| 119 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_LOOKUP_SET_IDcxx . . $ NFTNL_EXPR_LOOKUP_SET_ID.)) |
| 120 | mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_LOOKUP_SET_IDcxx . . $ NFTA_LOOKUP_SET_ID., |
| 121 | htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(lookup->set_id)); |
| 122 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_LOOKUP_FLAGScxx . . $ NFTNL_EXPR_LOOKUP_FLAGS.)) |
| 123 | mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_LOOKUP_FLAGScxx . . $ NFTA_LOOKUP_FLAGS., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(lookup->flags)); |
| 124 | } |
| 125 | |
| 126 | static int |
| 127 | nftnl_expr_lookup_parsecxx . . $ nftnl_expr_lookup_parse(ed61207049d8af9a).(struct nftnl_exprcxx . . $ nftnl_expr# *e, struct nlattrcxx . . $ nlattr# *attr) |
| 128 | { |
| 129 | struct nftnl_expr_lookupcxx . . $ nftnl_expr_lookup# *lookup = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 130 | struct nlattrcxx . . $ nlattr# *tb[NFTA_LOOKUP_MAXcxx . . $ `include/linux/netfilter/nf_tables.h:749:9`!+1] = {}; |
| 131 | int ret = 0; |
| 132 | |
| 133 | if (mnl_attr_parse_nestedcxx . . $ mnl_attr_parse_nested(f99c0e9fa94f99d9).(attr, nftnl_expr_lookup_cbcxx . . $ nftnl_expr_lookup_cb(de2ef18c48fd675c)., tb) < 0) |
| 134 | return -1; |
| 135 | |
| 136 | if (tb[NFTA_LOOKUP_SREGcxx . . $ NFTA_LOOKUP_SREG.]) { |
| 137 | lookup->sregcxx . . $ nftnl_expr_lookup#sreg. = ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_LOOKUP_SREG])); |
| 138 | e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_LOOKUP_SREGcxx . . $ NFTNL_EXPR_LOOKUP_SREG.); |
| 139 | } |
| 140 | if (tb[NFTA_LOOKUP_DREGcxx . . $ NFTA_LOOKUP_DREG.]) { |
| 141 | lookup->dregcxx . . $ nftnl_expr_lookup#dreg. = ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_LOOKUP_DREG])); |
| 142 | e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_LOOKUP_DREGcxx . . $ NFTNL_EXPR_LOOKUP_DREG.); |
| 143 | } |
| 144 | if (nftnl_parse_str_attrcxx . . $ nftnl_parse_str_attr(2002c8408bf98f7f).(tb[NFTA_LOOKUP_SETcxx . . $ NFTA_LOOKUP_SET.], |
| 145 | NFTNL_EXPR_LOOKUP_SETcxx . . $ NFTNL_EXPR_LOOKUP_SET., |
| 146 | (const char **)&lookup->set_namecxx . . $ nftnl_expr_lookup#set_name., |
| 147 | &e->flagscxx . . $ nftnl_expr#flags.) < 0) |
| 148 | return -1; |
| 149 | if (tb[NFTA_LOOKUP_SET_IDcxx . . $ NFTA_LOOKUP_SET_ID.]) { |
| 150 | lookup->set_idcxx . . $ nftnl_expr_lookup#set_id. = |
| 151 | ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_LOOKUP_SET_ID])); |
| 152 | e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_LOOKUP_SET_IDcxx . . $ NFTNL_EXPR_LOOKUP_SET_ID.); |
| 153 | } |
| 154 | if (tb[NFTA_LOOKUP_FLAGScxx . . $ NFTA_LOOKUP_FLAGS.]) { |
| 155 | lookup->flagscxx . . $ nftnl_expr_lookup#flags. = ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_LOOKUP_FLAGS])); |
| 156 | e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_LOOKUP_FLAGScxx . . $ NFTNL_EXPR_LOOKUP_FLAGS.); |
| 157 | } |
| 158 | |
| 159 | return ret; |
| 160 | } |
| 161 | |
| 162 | static int |
| 163 | nftnl_expr_lookup_snprintfcxx . . $ nftnl_expr_lookup_snprintf(2b4af69501aef780).(char *buf, size_t remain, |
| 164 | uint32_tcxx . . $ uint32_t# flags, const struct nftnl_exprcxx . . $ nftnl_expr# *e) |
| 165 | { |
| 166 | struct nftnl_expr_lookupcxx . . $ nftnl_expr_lookup# *l = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 167 | int offset = 0, ret; |
| 168 | |
| 169 | ret = snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf, remain, "reg %u set %s ", l->sregcxx . . $ nftnl_expr_lookup#sreg., l->set_namecxx . . $ nftnl_expr_lookup#set_name.); |
| 170 | SNPRINTF_BUFFER_SIZEcxx . . $ `include/utils.h:55:9`!(ret, remain, offset); |
| 171 | |
| 172 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_LOOKUP_DREGcxx . . $ NFTNL_EXPR_LOOKUP_DREG.)) { |
| 173 | ret = snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf + offset, remain, "dreg %u ", l->dregcxx . . $ nftnl_expr_lookup#dreg.); |
| 174 | SNPRINTF_BUFFER_SIZEcxx . . $ `include/utils.h:55:9`!(ret, remain, offset); |
| 175 | } |
| 176 | |
| 177 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_LOOKUP_FLAGScxx . . $ NFTNL_EXPR_LOOKUP_FLAGS.)) { |
| 178 | ret = snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf + offset, remain, "0x%x ", l->flagscxx . . $ nftnl_expr_lookup#flags.); |
| 179 | SNPRINTF_BUFFER_SIZEcxx . . $ `include/utils.h:55:9`!(ret, remain, offset); |
| 180 | } |
| 181 | |
| 182 | return offset; |
| 183 | } |
| 184 | |
| 185 | static void nftnl_expr_lookup_freecxx . . $ nftnl_expr_lookup_free(cbe1a28527736226).(const struct nftnl_exprcxx . . $ nftnl_expr# *e) |
| 186 | { |
| 187 | struct nftnl_expr_lookupcxx . . $ nftnl_expr_lookup# *lookup = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 188 | |
| 189 | xfreecxx . . $ `include/utils.h:22:9`!(lookup->set_name); |
| 190 | } |
| 191 | |
| 192 | static struct attr_policycxx . . $ attr_policy# lookup_attr_policycxx . . $ lookup_attr_policy.[__NFTNL_EXPR_LOOKUP_MAXcxx . . $ __NFTNL_EXPR_LOOKUP_MAX.] = { |
| 193 | [NFTNL_EXPR_LOOKUP_SREGcxx . . $ NFTNL_EXPR_LOOKUP_SREG.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) }, |
| 194 | [NFTNL_EXPR_LOOKUP_DREGcxx . . $ NFTNL_EXPR_LOOKUP_DREG.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) }, |
| 195 | [NFTNL_EXPR_LOOKUP_SETcxx . . $ NFTNL_EXPR_LOOKUP_SET.] = { .maxlen = NFT_SET_MAXNAMELENcxx . . $ `include/linux/netfilter/nf_tables.h:8:9`! }, |
| 196 | [NFTNL_EXPR_LOOKUP_SET_IDcxx . . $ NFTNL_EXPR_LOOKUP_SET_ID.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) }, |
| 197 | [NFTNL_EXPR_LOOKUP_FLAGScxx . . $ NFTNL_EXPR_LOOKUP_FLAGS.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) }, |
| 198 | }; |
| 199 | |
| 200 | struct expr_opscxx . . $ expr_ops# expr_ops_lookupcxx . . $ expr_ops_lookup. = { |
| 201 | .name = "lookup", |
| 202 | .alloc_len = sizeof(struct nftnl_expr_lookupcxx . . $ nftnl_expr_lookup#), |
| 203 | .nftnl_max_attr = __NFTNL_EXPR_LOOKUP_MAXcxx . . $ __NFTNL_EXPR_LOOKUP_MAX. - 1, |
| 204 | .attr_policy = lookup_attr_policycxx . . $ lookup_attr_policy., |
| 205 | .free = nftnl_expr_lookup_freecxx . . $ nftnl_expr_lookup_free(cbe1a28527736226)., |
| 206 | .set = nftnl_expr_lookup_setcxx . . $ nftnl_expr_lookup_set(211371a3e3842127)., |
| 207 | .get = nftnl_expr_lookup_getcxx . . $ nftnl_expr_lookup_get(9de263c4f9e71c4b)., |
| 208 | .parse = nftnl_expr_lookup_parsecxx . . $ nftnl_expr_lookup_parse(ed61207049d8af9a)., |
| 209 | .build = nftnl_expr_lookup_buildcxx . . $ nftnl_expr_lookup_build(93c7528a9cf90b41)., |
| 210 | .output = nftnl_expr_lookup_snprintfcxx . . $ nftnl_expr_lookup_snprintf(2b4af69501aef780)., |
| 211 | }; |