| 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */cxx . . $ `<file>/src/expr/meta.c`/ |
| 2 | /* |
| 3 | * (C) 2012 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>cxx . . $ `<file>/include/linux/netfilter/nf_tables.h`/ |
| 14 | |
| 15 | #include "internal.h"cxx . . $ `<file>/include/internal.h`/ |
| 16 | #include <libmnl/libmnl.h> |
| 17 | #include <libnftnl/expr.h>cxx . . $ `<file>/include/libnftnl/expr.h`/ |
| 18 | #include <libnftnl/rule.h>cxx . . $ `<file>/include/libnftnl/rule.h`/ |
| 19 | |
| 20 | #ifndef NFT_META_MAX |
| 21 | #define NFT_META_MAXcxx . . $ `src/expr/meta.c:21:9`! (NFT_META_BRI_IIFHWADDR + 1) |
| 22 | #endif |
| 23 | |
| 24 | struct nftnl_expr_metacxx . . $ nftnl_expr_meta# { |
| 25 | enum nft_meta_keyscxx . . $ nft_meta_keys# keycxx . . $ nftnl_expr_meta#key.; |
| 26 | enum nft_registerscxx . . $ nft_registers# dregcxx . . $ nftnl_expr_meta#dreg.; |
| 27 | enum nft_registerscxx . . $ nft_registers# sregcxx . . $ nftnl_expr_meta#sreg.; |
| 28 | }; |
| 29 | |
| 30 | static int |
| 31 | nftnl_expr_meta_setcxx . . $ nftnl_expr_meta_set(211371a3e3842127).(struct nftnl_exprcxx . . $ nftnl_expr# *e, uint16_tcxx . . $ uint16_t# type, |
| 32 | const void *data, uint32_tcxx . . $ uint32_t# data_len, uint32_tcxx . . $ uint32_t# byteorder) |
| 33 | { |
| 34 | struct nftnl_expr_metacxx . . $ nftnl_expr_meta# *meta = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 35 | |
| 36 | switch(type) { |
| 37 | case NFTNL_EXPR_META_KEYcxx . . $ NFTNL_EXPR_META_KEY.: |
| 38 | memcpycxx . . $ memcpy(cc817cb175f282d).(&meta->keycxx . . $ nftnl_expr_meta#key., data, data_len); |
| 39 | break; |
| 40 | case NFTNL_EXPR_META_DREGcxx . . $ NFTNL_EXPR_META_DREG.: |
| 41 | memcpycxx . . $ memcpy(cc817cb175f282d).(&meta->dregcxx . . $ nftnl_expr_meta#dreg., data, data_len); |
| 42 | break; |
| 43 | case NFTNL_EXPR_META_SREGcxx . . $ NFTNL_EXPR_META_SREG.: |
| 44 | memcpycxx . . $ memcpy(cc817cb175f282d).(&meta->sregcxx . . $ nftnl_expr_meta#sreg., data, data_len); |
| 45 | break; |
| 46 | } |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | static const void * |
| 51 | nftnl_expr_meta_getcxx . . $ nftnl_expr_meta_get(9de263c4f9e71c4b).(const struct nftnl_exprcxx . . $ nftnl_expr# *e, uint16_tcxx . . $ uint16_t# type, |
| 52 | uint32_tcxx . . $ uint32_t# *data_len) |
| 53 | { |
| 54 | struct nftnl_expr_metacxx . . $ nftnl_expr_meta# *meta = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 55 | |
| 56 | switch(type) { |
| 57 | case NFTNL_EXPR_META_KEYcxx . . $ NFTNL_EXPR_META_KEY.: |
| 58 | *data_len = sizeof(meta->keycxx . . $ nftnl_expr_meta#key.); |
| 59 | return &meta->keycxx . . $ nftnl_expr_meta#key.; |
| 60 | case NFTNL_EXPR_META_DREGcxx . . $ NFTNL_EXPR_META_DREG.: |
| 61 | *data_len = sizeof(meta->dregcxx . . $ nftnl_expr_meta#dreg.); |
| 62 | return &meta->dregcxx . . $ nftnl_expr_meta#dreg.; |
| 63 | case NFTNL_EXPR_META_SREGcxx . . $ NFTNL_EXPR_META_SREG.: |
| 64 | *data_len = sizeof(meta->sregcxx . . $ nftnl_expr_meta#sreg.); |
| 65 | return &meta->sregcxx . . $ nftnl_expr_meta#sreg.; |
| 66 | } |
| 67 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 68 | } |
| 69 | |
| 70 | static int nftnl_expr_meta_cbcxx . . $ nftnl_expr_meta_cb(de2ef18c48fd675c).(const struct nlattrcxx . . $ nlattr# *attr, void *data) |
| 71 | { |
| 72 | const struct nlattrcxx . . $ nlattr# **tb = data; |
| 73 | int type = mnl_attr_get_typecxx . . $ mnl_attr_get_type(1da47bcc4c4b975).(attr); |
| 74 | |
| 75 | if (mnl_attr_type_validcxx . . $ mnl_attr_type_valid(47df1cce1c7be855).(attr, NFTA_META_MAXcxx . . $ `include/linux/netfilter/nf_tables.h:1078:9`!) < 0) |
| 76 | return MNL_CB_OKcxx . . $ `<external>/12386019527593959648/libmnl.h:174:9`!; |
| 77 | |
| 78 | switch(type) { |
| 79 | case NFTA_META_KEYcxx . . $ NFTA_META_KEY.: |
| 80 | case NFTA_META_DREGcxx . . $ NFTA_META_DREG.: |
| 81 | case NFTA_META_SREGcxx . . $ NFTA_META_SREG.: |
| 82 | if (mnl_attr_validatecxx . . $ mnl_attr_validate(e419697a5e1ae2b1).(attr, MNL_TYPE_U32cxx . . $ MNL_TYPE_U32.) < 0) |
| 83 | abi_breakagecxx . . $ `include/utils.h:28:9`!(); |
| 84 | break; |
| 85 | } |
| 86 | |
| 87 | tb[type] = attr; |
| 88 | return MNL_CB_OKcxx . . $ `<external>/12386019527593959648/libmnl.h:174:9`!; |
| 89 | } |
| 90 | |
| 91 | static void |
| 92 | nftnl_expr_meta_buildcxx . . $ nftnl_expr_meta_build(93c7528a9cf90b41).(struct nlmsghdrcxx . . $ nlmsghdr# *nlh, const struct nftnl_exprcxx . . $ nftnl_expr# *e) |
| 93 | { |
| 94 | struct nftnl_expr_metacxx . . $ nftnl_expr_meta# *meta = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 95 | |
| 96 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_META_KEYcxx . . $ NFTNL_EXPR_META_KEY.)) |
| 97 | mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_META_KEYcxx . . $ NFTA_META_KEY., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(meta->key)); |
| 98 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_META_DREGcxx . . $ NFTNL_EXPR_META_DREG.)) |
| 99 | mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_META_DREGcxx . . $ NFTA_META_DREG., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(meta->dreg)); |
| 100 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_META_SREGcxx . . $ NFTNL_EXPR_META_SREG.)) |
| 101 | mnl_attr_put_u32cxx . . $ mnl_attr_put_u32(b46fd0aeb0cfb96d).(nlh, NFTA_META_SREGcxx . . $ NFTA_META_SREG., htonlcxx . . $ `<external>/9581057695539925927/in.h:430:12`!(meta->sreg)); |
| 102 | } |
| 103 | |
| 104 | static int |
| 105 | nftnl_expr_meta_parsecxx . . $ nftnl_expr_meta_parse(ed61207049d8af9a).(struct nftnl_exprcxx . . $ nftnl_expr# *e, struct nlattrcxx . . $ nlattr# *attr) |
| 106 | { |
| 107 | struct nftnl_expr_metacxx . . $ nftnl_expr_meta# *meta = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 108 | struct nlattrcxx . . $ nlattr# *tb[NFTA_META_MAXcxx . . $ `include/linux/netfilter/nf_tables.h:1078:9`!+1] = {}; |
| 109 | |
| 110 | if (mnl_attr_parse_nestedcxx . . $ mnl_attr_parse_nested(f99c0e9fa94f99d9).(attr, nftnl_expr_meta_cbcxx . . $ nftnl_expr_meta_cb(de2ef18c48fd675c)., tb) < 0) |
| 111 | return -1; |
| 112 | |
| 113 | if (tb[NFTA_META_KEYcxx . . $ NFTA_META_KEY.]) { |
| 114 | meta->keycxx . . $ nftnl_expr_meta#key. = ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_META_KEY])); |
| 115 | e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_META_KEYcxx . . $ NFTNL_EXPR_META_KEY.); |
| 116 | } |
| 117 | if (tb[NFTA_META_DREGcxx . . $ NFTA_META_DREG.]) { |
| 118 | meta->dregcxx . . $ nftnl_expr_meta#dreg. = ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_META_DREG])); |
| 119 | e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_META_DREGcxx . . $ NFTNL_EXPR_META_DREG.); |
| 120 | } |
| 121 | if (tb[NFTA_META_SREGcxx . . $ NFTA_META_SREG.]) { |
| 122 | meta->sregcxx . . $ nftnl_expr_meta#sreg. = ntohlcxx . . $ `<external>/9581057695539925927/in.h:428:12`!(mnl_attr_get_u32(tb[NFTA_META_SREG])); |
| 123 | e->flagscxx . . $ nftnl_expr#flags. |= (1 << NFTNL_EXPR_META_SREGcxx . . $ NFTNL_EXPR_META_SREG.); |
| 124 | } |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static const char *meta_key2str_arraycxx . . $ meta_key2str_array.[NFT_META_MAXcxx . . $ `src/expr/meta.c:21:9`!] = { |
| 130 | [NFT_META_LENcxx . . $ NFT_META_LEN.] = "len", |
| 131 | [NFT_META_PROTOCOLcxx . . $ NFT_META_PROTOCOL.] = "protocol", |
| 132 | [NFT_META_NFPROTOcxx . . $ NFT_META_NFPROTO.] = "nfproto", |
| 133 | [NFT_META_L4PROTOcxx . . $ NFT_META_L4PROTO.] = "l4proto", |
| 134 | [NFT_META_PRIORITYcxx . . $ NFT_META_PRIORITY.] = "priority", |
| 135 | [NFT_META_MARKcxx . . $ NFT_META_MARK.] = "mark", |
| 136 | [NFT_META_IIFcxx . . $ NFT_META_IIF.] = "iif", |
| 137 | [NFT_META_OIFcxx . . $ NFT_META_OIF.] = "oif", |
| 138 | [NFT_META_IIFNAMEcxx . . $ NFT_META_IIFNAME.] = "iifname", |
| 139 | [NFT_META_OIFNAMEcxx . . $ NFT_META_OIFNAME.] = "oifname", |
| 140 | [NFT_META_IIFTYPEcxx . . $ `include/linux/netfilter/nf_tables.h:974:9`!] = "iiftype", |
| 141 | [NFT_META_OIFTYPEcxx . . $ NFT_META_OIFTYPE.] = "oiftype", |
| 142 | [NFT_META_SKUIDcxx . . $ NFT_META_SKUID.] = "skuid", |
| 143 | [NFT_META_SKGIDcxx . . $ NFT_META_SKGID.] = "skgid", |
| 144 | [NFT_META_NFTRACEcxx . . $ NFT_META_NFTRACE.] = "nftrace", |
| 145 | [NFT_META_RTCLASSIDcxx . . $ NFT_META_RTCLASSID.] = "rtclassid", |
| 146 | [NFT_META_SECMARKcxx . . $ NFT_META_SECMARK.] = "secmark", |
| 147 | [NFT_META_BRI_IIFNAMEcxx . . $ NFT_META_BRI_IIFNAME.] = "bri_iifname", |
| 148 | [NFT_META_BRI_OIFNAMEcxx . . $ NFT_META_BRI_OIFNAME.] = "bri_oifname", |
| 149 | [NFT_META_PKTTYPEcxx . . $ NFT_META_PKTTYPE.] = "pkttype", |
| 150 | [NFT_META_CPUcxx . . $ NFT_META_CPU.] = "cpu", |
| 151 | [NFT_META_IIFGROUPcxx . . $ NFT_META_IIFGROUP.] = "iifgroup", |
| 152 | [NFT_META_OIFGROUPcxx . . $ NFT_META_OIFGROUP.] = "oifgroup", |
| 153 | [NFT_META_CGROUPcxx . . $ NFT_META_CGROUP.] = "cgroup", |
| 154 | [NFT_META_PRANDOMcxx . . $ NFT_META_PRANDOM.] = "prandom", |
| 155 | [NFT_META_SECPATHcxx . . $ NFT_META_SECPATH.] = "secpath", |
| 156 | [NFT_META_IIFKINDcxx . . $ NFT_META_IIFKIND.] = "iifkind", |
| 157 | [NFT_META_OIFKINDcxx . . $ NFT_META_OIFKIND.] = "oifkind", |
| 158 | [NFT_META_BRI_IIFPVIDcxx . . $ NFT_META_BRI_IIFPVID.] = "bri_iifpvid", |
| 159 | [NFT_META_BRI_IIFVPROTOcxx . . $ NFT_META_BRI_IIFVPROTO.] = "bri_iifvproto", |
| 160 | [NFT_META_TIME_NScxx . . $ NFT_META_TIME_NS.] = "time", |
| 161 | [NFT_META_TIME_DAYcxx . . $ NFT_META_TIME_DAY.] = "day", |
| 162 | [NFT_META_TIME_HOURcxx . . $ NFT_META_TIME_HOUR.] = "hour", |
| 163 | [NFT_META_SDIFcxx . . $ NFT_META_SDIF.] = "sdif", |
| 164 | [NFT_META_SDIFNAMEcxx . . $ NFT_META_SDIFNAME.] = "sdifname", |
| 165 | [NFT_META_BRI_BROUTEcxx . . $ NFT_META_BRI_BROUTE.] = "broute", |
| 166 | [NFT_META_BRI_IIFHWADDRcxx . . $ NFT_META_BRI_IIFHWADDR.] = "ibrhwaddr", |
| 167 | }; |
| 168 | |
| 169 | static const char *meta_key2strcxx . . $ meta_key2str(42624c5f3be67010).(uint8_tcxx . . $ uint8_t# key) |
| 170 | { |
| 171 | if (key < NFT_META_MAXcxx . . $ `src/expr/meta.c:21:9`!) |
| 172 | return meta_key2str_arraycxx . . $ meta_key2str_array.[key]; |
| 173 | |
| 174 | return "unknown"; |
| 175 | } |
| 176 | |
| 177 | static int |
| 178 | nftnl_expr_meta_snprintfcxx . . $ nftnl_expr_meta_snprintf(2b4af69501aef780).(char *buf, size_t len, |
| 179 | uint32_tcxx . . $ uint32_t# flags, const struct nftnl_exprcxx . . $ nftnl_expr# *e) |
| 180 | { |
| 181 | struct nftnl_expr_metacxx . . $ nftnl_expr_meta# *meta = nftnl_expr_datacxx . . $ `include/expr_ops.h:31:9`!(e); |
| 182 | |
| 183 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_META_SREGcxx . . $ NFTNL_EXPR_META_SREG.)) { |
| 184 | return snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf, len, "set %s with reg %u ", |
| 185 | meta_key2strcxx . . $ meta_key2str(42624c5f3be67010).(meta->keycxx . . $ nftnl_expr_meta#key.), meta->sregcxx . . $ nftnl_expr_meta#sreg.); |
| 186 | } |
| 187 | if (e->flagscxx . . $ nftnl_expr#flags. & (1 << NFTNL_EXPR_META_DREGcxx . . $ NFTNL_EXPR_META_DREG.)) { |
| 188 | return snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf, len, "load %s => reg %u ", |
| 189 | meta_key2strcxx . . $ meta_key2str(42624c5f3be67010).(meta->keycxx . . $ nftnl_expr_meta#key.), meta->dregcxx . . $ nftnl_expr_meta#dreg.); |
| 190 | } |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | static struct attr_policycxx . . $ attr_policy# meta_attr_policycxx . . $ meta_attr_policy.[__NFTNL_EXPR_META_MAXcxx . . $ __NFTNL_EXPR_META_MAX.] = { |
| 195 | [NFTNL_EXPR_META_KEYcxx . . $ NFTNL_EXPR_META_KEY.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) }, |
| 196 | [NFTNL_EXPR_META_DREGcxx . . $ NFTNL_EXPR_META_DREG.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) }, |
| 197 | [NFTNL_EXPR_META_SREGcxx . . $ NFTNL_EXPR_META_SREG.] = { .maxlen = sizeof(uint32_tcxx . . $ uint32_t#) }, |
| 198 | }; |
| 199 | |
| 200 | struct expr_opscxx . . $ expr_ops# expr_ops_metacxx . . $ expr_ops_meta. = { |
| 201 | .name = "meta", |
| 202 | .alloc_len = sizeof(struct nftnl_expr_metacxx . . $ nftnl_expr_meta#), |
| 203 | .nftnl_max_attr = __NFTNL_EXPR_META_MAXcxx . . $ __NFTNL_EXPR_META_MAX. - 1, |
| 204 | .attr_policy = meta_attr_policycxx . . $ meta_attr_policy., |
| 205 | .set = nftnl_expr_meta_setcxx . . $ nftnl_expr_meta_set(211371a3e3842127)., |
| 206 | .get = nftnl_expr_meta_getcxx . . $ nftnl_expr_meta_get(9de263c4f9e71c4b)., |
| 207 | .parse = nftnl_expr_meta_parsecxx . . $ nftnl_expr_meta_parse(ed61207049d8af9a)., |
| 208 | .build = nftnl_expr_meta_buildcxx . . $ nftnl_expr_meta_build(93c7528a9cf90b41)., |
| 209 | .output = nftnl_expr_meta_snprintfcxx . . $ nftnl_expr_meta_snprintf(2b4af69501aef780)., |
| 210 | }; |