Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Arturo Borrero | 9ba1f726 | 2014-09-08 13:45:00 +0200 | [diff] [blame] | 2 | /* |
Arturo Borrero Gonzalez | cd72751 | 2016-10-18 14:02:29 +0200 | [diff] [blame] | 3 | * Copyright (c) 2014 Arturo Borrero Gonzalez <arturo@debian.org> |
Arturo Borrero | 9ba1f726 | 2014-09-08 13:45:00 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/kernel.h> |
| 7 | #include <linux/init.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/netlink.h> |
| 10 | #include <linux/netfilter.h> |
| 11 | #include <linux/netfilter/nf_tables.h> |
| 12 | #include <net/netfilter/nf_tables.h> |
| 13 | #include <net/netfilter/nf_nat.h> |
Florian Westphal | bf8981a | 2019-04-09 10:44:06 +0200 | [diff] [blame] | 14 | #include <net/netfilter/nf_nat_masquerade.h> |
Arturo Borrero | 9ba1f726 | 2014-09-08 13:45:00 +0200 | [diff] [blame] | 15 | |
Florian Westphal | a9ce849 | 2019-02-28 12:02:51 +0100 | [diff] [blame] | 16 | struct nft_masq { |
| 17 | u32 flags; |
Pablo Neira Ayuso | 4f16d25 | 2021-01-25 17:28:18 +0100 | [diff] [blame] | 18 | u8 sreg_proto_min; |
| 19 | u8 sreg_proto_max; |
Florian Westphal | a9ce849 | 2019-02-28 12:02:51 +0100 | [diff] [blame] | 20 | }; |
| 21 | |
| 22 | static const struct nla_policy nft_masq_policy[NFTA_MASQ_MAX + 1] = { |
Pablo Neira Ayuso | 8a6bf5d | 2016-03-01 19:55:14 +0100 | [diff] [blame] | 23 | [NFTA_MASQ_FLAGS] = { .type = NLA_U32 }, |
| 24 | [NFTA_MASQ_REG_PROTO_MIN] = { .type = NLA_U32 }, |
| 25 | [NFTA_MASQ_REG_PROTO_MAX] = { .type = NLA_U32 }, |
Arturo Borrero | 9ba1f726 | 2014-09-08 13:45:00 +0200 | [diff] [blame] | 26 | }; |
Arturo Borrero | 9ba1f726 | 2014-09-08 13:45:00 +0200 | [diff] [blame] | 27 | |
Florian Westphal | a9ce849 | 2019-02-28 12:02:51 +0100 | [diff] [blame] | 28 | static int nft_masq_validate(const struct nft_ctx *ctx, |
| 29 | const struct nft_expr *expr, |
| 30 | const struct nft_data **data) |
Pablo Neira Ayuso | 75e8d06 | 2015-01-14 15:33:57 +0100 | [diff] [blame] | 31 | { |
| 32 | int err; |
| 33 | |
| 34 | err = nft_chain_validate_dependency(ctx->chain, NFT_CHAIN_T_NAT); |
| 35 | if (err < 0) |
| 36 | return err; |
| 37 | |
| 38 | return nft_chain_validate_hooks(ctx->chain, |
| 39 | (1 << NF_INET_POST_ROUTING)); |
| 40 | } |
Pablo Neira Ayuso | 75e8d06 | 2015-01-14 15:33:57 +0100 | [diff] [blame] | 41 | |
Florian Westphal | a9ce849 | 2019-02-28 12:02:51 +0100 | [diff] [blame] | 42 | static int nft_masq_init(const struct nft_ctx *ctx, |
| 43 | const struct nft_expr *expr, |
| 44 | const struct nlattr * const tb[]) |
Arturo Borrero | 9ba1f726 | 2014-09-08 13:45:00 +0200 | [diff] [blame] | 45 | { |
Pankaj Bharadiya | c593642 | 2019-12-09 10:31:43 -0800 | [diff] [blame] | 46 | u32 plen = sizeof_field(struct nf_nat_range, min_addr.all); |
Arturo Borrero | 9ba1f726 | 2014-09-08 13:45:00 +0200 | [diff] [blame] | 47 | struct nft_masq *priv = nft_expr_priv(expr); |
Pablo Neira Ayuso | 7210e4e | 2014-10-13 19:50:22 +0200 | [diff] [blame] | 48 | int err; |
| 49 | |
Pablo Neira Ayuso | 8a6bf5d | 2016-03-01 19:55:14 +0100 | [diff] [blame] | 50 | if (tb[NFTA_MASQ_FLAGS]) { |
| 51 | priv->flags = ntohl(nla_get_be32(tb[NFTA_MASQ_FLAGS])); |
| 52 | if (priv->flags & ~NF_NAT_RANGE_MASK) |
| 53 | return -EINVAL; |
| 54 | } |
Arturo Borrero | 9ba1f726 | 2014-09-08 13:45:00 +0200 | [diff] [blame] | 55 | |
Pablo Neira Ayuso | 8a6bf5d | 2016-03-01 19:55:14 +0100 | [diff] [blame] | 56 | if (tb[NFTA_MASQ_REG_PROTO_MIN]) { |
Pablo Neira Ayuso | 4f16d25 | 2021-01-25 17:28:18 +0100 | [diff] [blame] | 57 | err = nft_parse_register_load(tb[NFTA_MASQ_REG_PROTO_MIN], |
| 58 | &priv->sreg_proto_min, plen); |
Pablo Neira Ayuso | 8a6bf5d | 2016-03-01 19:55:14 +0100 | [diff] [blame] | 59 | if (err < 0) |
| 60 | return err; |
| 61 | |
| 62 | if (tb[NFTA_MASQ_REG_PROTO_MAX]) { |
Pablo Neira Ayuso | 4f16d25 | 2021-01-25 17:28:18 +0100 | [diff] [blame] | 63 | err = nft_parse_register_load(tb[NFTA_MASQ_REG_PROTO_MAX], |
| 64 | &priv->sreg_proto_max, |
| 65 | plen); |
Pablo Neira Ayuso | 8a6bf5d | 2016-03-01 19:55:14 +0100 | [diff] [blame] | 66 | if (err < 0) |
| 67 | return err; |
| 68 | } else { |
| 69 | priv->sreg_proto_max = priv->sreg_proto_min; |
| 70 | } |
| 71 | } |
Arturo Borrero | 9ba1f726 | 2014-09-08 13:45:00 +0200 | [diff] [blame] | 72 | |
Pablo Neira Ayuso | 36596da | 2018-01-09 02:38:03 +0100 | [diff] [blame] | 73 | return nf_ct_netns_get(ctx->net, ctx->family); |
Arturo Borrero | 9ba1f726 | 2014-09-08 13:45:00 +0200 | [diff] [blame] | 74 | } |
Arturo Borrero | 9ba1f726 | 2014-09-08 13:45:00 +0200 | [diff] [blame] | 75 | |
Florian Westphal | a9ce849 | 2019-02-28 12:02:51 +0100 | [diff] [blame] | 76 | static int nft_masq_dump(struct sk_buff *skb, const struct nft_expr *expr) |
Arturo Borrero | 9ba1f726 | 2014-09-08 13:45:00 +0200 | [diff] [blame] | 77 | { |
| 78 | const struct nft_masq *priv = nft_expr_priv(expr); |
| 79 | |
Pablo Neira Ayuso | 8a6bf5d | 2016-03-01 19:55:14 +0100 | [diff] [blame] | 80 | if (priv->flags != 0 && |
| 81 | nla_put_be32(skb, NFTA_MASQ_FLAGS, htonl(priv->flags))) |
Arturo Borrero | 9ba1f726 | 2014-09-08 13:45:00 +0200 | [diff] [blame] | 82 | goto nla_put_failure; |
| 83 | |
Pablo Neira Ayuso | 8a6bf5d | 2016-03-01 19:55:14 +0100 | [diff] [blame] | 84 | if (priv->sreg_proto_min) { |
| 85 | if (nft_dump_register(skb, NFTA_MASQ_REG_PROTO_MIN, |
| 86 | priv->sreg_proto_min) || |
| 87 | nft_dump_register(skb, NFTA_MASQ_REG_PROTO_MAX, |
| 88 | priv->sreg_proto_max)) |
| 89 | goto nla_put_failure; |
| 90 | } |
| 91 | |
Arturo Borrero | 9ba1f726 | 2014-09-08 13:45:00 +0200 | [diff] [blame] | 92 | return 0; |
| 93 | |
| 94 | nla_put_failure: |
| 95 | return -1; |
| 96 | } |
Florian Westphal | a9ce849 | 2019-02-28 12:02:51 +0100 | [diff] [blame] | 97 | |
| 98 | static void nft_masq_ipv4_eval(const struct nft_expr *expr, |
| 99 | struct nft_regs *regs, |
| 100 | const struct nft_pktinfo *pkt) |
| 101 | { |
| 102 | struct nft_masq *priv = nft_expr_priv(expr); |
| 103 | struct nf_nat_range2 range; |
| 104 | |
| 105 | memset(&range, 0, sizeof(range)); |
| 106 | range.flags = priv->flags; |
| 107 | if (priv->sreg_proto_min) { |
| 108 | range.min_proto.all = (__force __be16)nft_reg_load16( |
| 109 | ®s->data[priv->sreg_proto_min]); |
| 110 | range.max_proto.all = (__force __be16)nft_reg_load16( |
| 111 | ®s->data[priv->sreg_proto_max]); |
| 112 | } |
| 113 | regs->verdict.code = nf_nat_masquerade_ipv4(pkt->skb, nft_hook(pkt), |
| 114 | &range, nft_out(pkt)); |
| 115 | } |
| 116 | |
| 117 | static void |
| 118 | nft_masq_ipv4_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr) |
| 119 | { |
| 120 | nf_ct_netns_put(ctx->net, NFPROTO_IPV4); |
| 121 | } |
| 122 | |
| 123 | static struct nft_expr_type nft_masq_ipv4_type; |
| 124 | static const struct nft_expr_ops nft_masq_ipv4_ops = { |
| 125 | .type = &nft_masq_ipv4_type, |
| 126 | .size = NFT_EXPR_SIZE(sizeof(struct nft_masq)), |
| 127 | .eval = nft_masq_ipv4_eval, |
| 128 | .init = nft_masq_init, |
| 129 | .destroy = nft_masq_ipv4_destroy, |
| 130 | .dump = nft_masq_dump, |
| 131 | .validate = nft_masq_validate, |
Pablo Neira Ayuso | b2d3065 | 2022-03-14 18:23:00 +0100 | [diff] [blame] | 132 | .reduce = NFT_REDUCE_READONLY, |
Florian Westphal | a9ce849 | 2019-02-28 12:02:51 +0100 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | static struct nft_expr_type nft_masq_ipv4_type __read_mostly = { |
| 136 | .family = NFPROTO_IPV4, |
| 137 | .name = "masq", |
| 138 | .ops = &nft_masq_ipv4_ops, |
| 139 | .policy = nft_masq_policy, |
| 140 | .maxattr = NFTA_MASQ_MAX, |
| 141 | .owner = THIS_MODULE, |
| 142 | }; |
| 143 | |
| 144 | #ifdef CONFIG_NF_TABLES_IPV6 |
| 145 | static void nft_masq_ipv6_eval(const struct nft_expr *expr, |
| 146 | struct nft_regs *regs, |
| 147 | const struct nft_pktinfo *pkt) |
| 148 | { |
| 149 | struct nft_masq *priv = nft_expr_priv(expr); |
| 150 | struct nf_nat_range2 range; |
| 151 | |
| 152 | memset(&range, 0, sizeof(range)); |
| 153 | range.flags = priv->flags; |
| 154 | if (priv->sreg_proto_min) { |
| 155 | range.min_proto.all = (__force __be16)nft_reg_load16( |
| 156 | ®s->data[priv->sreg_proto_min]); |
| 157 | range.max_proto.all = (__force __be16)nft_reg_load16( |
| 158 | ®s->data[priv->sreg_proto_max]); |
| 159 | } |
| 160 | regs->verdict.code = nf_nat_masquerade_ipv6(pkt->skb, &range, |
| 161 | nft_out(pkt)); |
| 162 | } |
| 163 | |
| 164 | static void |
| 165 | nft_masq_ipv6_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr) |
| 166 | { |
| 167 | nf_ct_netns_put(ctx->net, NFPROTO_IPV6); |
| 168 | } |
| 169 | |
| 170 | static struct nft_expr_type nft_masq_ipv6_type; |
| 171 | static const struct nft_expr_ops nft_masq_ipv6_ops = { |
| 172 | .type = &nft_masq_ipv6_type, |
| 173 | .size = NFT_EXPR_SIZE(sizeof(struct nft_masq)), |
| 174 | .eval = nft_masq_ipv6_eval, |
| 175 | .init = nft_masq_init, |
| 176 | .destroy = nft_masq_ipv6_destroy, |
| 177 | .dump = nft_masq_dump, |
| 178 | .validate = nft_masq_validate, |
Pablo Neira Ayuso | b2d3065 | 2022-03-14 18:23:00 +0100 | [diff] [blame] | 179 | .reduce = NFT_REDUCE_READONLY, |
Florian Westphal | a9ce849 | 2019-02-28 12:02:51 +0100 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | static struct nft_expr_type nft_masq_ipv6_type __read_mostly = { |
| 183 | .family = NFPROTO_IPV6, |
| 184 | .name = "masq", |
| 185 | .ops = &nft_masq_ipv6_ops, |
| 186 | .policy = nft_masq_policy, |
| 187 | .maxattr = NFTA_MASQ_MAX, |
| 188 | .owner = THIS_MODULE, |
| 189 | }; |
| 190 | |
| 191 | static int __init nft_masq_module_init_ipv6(void) |
| 192 | { |
Florian Westphal | 610a431 | 2019-04-09 10:44:08 +0200 | [diff] [blame] | 193 | return nft_register_expr(&nft_masq_ipv6_type); |
Florian Westphal | a9ce849 | 2019-02-28 12:02:51 +0100 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | static void nft_masq_module_exit_ipv6(void) |
| 197 | { |
| 198 | nft_unregister_expr(&nft_masq_ipv6_type); |
Florian Westphal | a9ce849 | 2019-02-28 12:02:51 +0100 | [diff] [blame] | 199 | } |
| 200 | #else |
| 201 | static inline int nft_masq_module_init_ipv6(void) { return 0; } |
| 202 | static inline void nft_masq_module_exit_ipv6(void) {} |
| 203 | #endif |
| 204 | |
Florian Westphal | 071657d | 2019-03-27 09:22:27 +0100 | [diff] [blame] | 205 | #ifdef CONFIG_NF_TABLES_INET |
| 206 | static void nft_masq_inet_eval(const struct nft_expr *expr, |
| 207 | struct nft_regs *regs, |
| 208 | const struct nft_pktinfo *pkt) |
| 209 | { |
| 210 | switch (nft_pf(pkt)) { |
| 211 | case NFPROTO_IPV4: |
| 212 | return nft_masq_ipv4_eval(expr, regs, pkt); |
| 213 | case NFPROTO_IPV6: |
| 214 | return nft_masq_ipv6_eval(expr, regs, pkt); |
| 215 | } |
| 216 | |
| 217 | WARN_ON_ONCE(1); |
| 218 | } |
| 219 | |
| 220 | static void |
| 221 | nft_masq_inet_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr) |
| 222 | { |
| 223 | nf_ct_netns_put(ctx->net, NFPROTO_INET); |
| 224 | } |
| 225 | |
| 226 | static struct nft_expr_type nft_masq_inet_type; |
| 227 | static const struct nft_expr_ops nft_masq_inet_ops = { |
| 228 | .type = &nft_masq_inet_type, |
| 229 | .size = NFT_EXPR_SIZE(sizeof(struct nft_masq)), |
| 230 | .eval = nft_masq_inet_eval, |
| 231 | .init = nft_masq_init, |
| 232 | .destroy = nft_masq_inet_destroy, |
| 233 | .dump = nft_masq_dump, |
| 234 | .validate = nft_masq_validate, |
Pablo Neira Ayuso | b2d3065 | 2022-03-14 18:23:00 +0100 | [diff] [blame] | 235 | .reduce = NFT_REDUCE_READONLY, |
Florian Westphal | 071657d | 2019-03-27 09:22:27 +0100 | [diff] [blame] | 236 | }; |
| 237 | |
| 238 | static struct nft_expr_type nft_masq_inet_type __read_mostly = { |
| 239 | .family = NFPROTO_INET, |
| 240 | .name = "masq", |
| 241 | .ops = &nft_masq_inet_ops, |
| 242 | .policy = nft_masq_policy, |
| 243 | .maxattr = NFTA_MASQ_MAX, |
| 244 | .owner = THIS_MODULE, |
| 245 | }; |
| 246 | |
| 247 | static int __init nft_masq_module_init_inet(void) |
| 248 | { |
| 249 | return nft_register_expr(&nft_masq_inet_type); |
| 250 | } |
| 251 | |
| 252 | static void nft_masq_module_exit_inet(void) |
| 253 | { |
| 254 | nft_unregister_expr(&nft_masq_inet_type); |
| 255 | } |
| 256 | #else |
| 257 | static inline int nft_masq_module_init_inet(void) { return 0; } |
| 258 | static inline void nft_masq_module_exit_inet(void) {} |
| 259 | #endif |
| 260 | |
Florian Westphal | a9ce849 | 2019-02-28 12:02:51 +0100 | [diff] [blame] | 261 | static int __init nft_masq_module_init(void) |
| 262 | { |
| 263 | int ret; |
| 264 | |
| 265 | ret = nft_masq_module_init_ipv6(); |
| 266 | if (ret < 0) |
| 267 | return ret; |
| 268 | |
Florian Westphal | 071657d | 2019-03-27 09:22:27 +0100 | [diff] [blame] | 269 | ret = nft_masq_module_init_inet(); |
| 270 | if (ret < 0) { |
| 271 | nft_masq_module_exit_ipv6(); |
| 272 | return ret; |
| 273 | } |
| 274 | |
Florian Westphal | a9ce849 | 2019-02-28 12:02:51 +0100 | [diff] [blame] | 275 | ret = nft_register_expr(&nft_masq_ipv4_type); |
| 276 | if (ret < 0) { |
Florian Westphal | 071657d | 2019-03-27 09:22:27 +0100 | [diff] [blame] | 277 | nft_masq_module_exit_inet(); |
Florian Westphal | a9ce849 | 2019-02-28 12:02:51 +0100 | [diff] [blame] | 278 | nft_masq_module_exit_ipv6(); |
| 279 | return ret; |
| 280 | } |
| 281 | |
Florian Westphal | 610a431 | 2019-04-09 10:44:08 +0200 | [diff] [blame] | 282 | ret = nf_nat_masquerade_inet_register_notifiers(); |
Florian Westphal | a9ce849 | 2019-02-28 12:02:51 +0100 | [diff] [blame] | 283 | if (ret < 0) { |
| 284 | nft_masq_module_exit_ipv6(); |
Florian Westphal | 071657d | 2019-03-27 09:22:27 +0100 | [diff] [blame] | 285 | nft_masq_module_exit_inet(); |
Florian Westphal | a9ce849 | 2019-02-28 12:02:51 +0100 | [diff] [blame] | 286 | nft_unregister_expr(&nft_masq_ipv4_type); |
| 287 | return ret; |
| 288 | } |
| 289 | |
| 290 | return ret; |
| 291 | } |
| 292 | |
| 293 | static void __exit nft_masq_module_exit(void) |
| 294 | { |
| 295 | nft_masq_module_exit_ipv6(); |
Florian Westphal | 071657d | 2019-03-27 09:22:27 +0100 | [diff] [blame] | 296 | nft_masq_module_exit_inet(); |
Florian Westphal | a9ce849 | 2019-02-28 12:02:51 +0100 | [diff] [blame] | 297 | nft_unregister_expr(&nft_masq_ipv4_type); |
Florian Westphal | 610a431 | 2019-04-09 10:44:08 +0200 | [diff] [blame] | 298 | nf_nat_masquerade_inet_unregister_notifiers(); |
Florian Westphal | a9ce849 | 2019-02-28 12:02:51 +0100 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | module_init(nft_masq_module_init); |
| 302 | module_exit(nft_masq_module_exit); |
Arturo Borrero | 9ba1f726 | 2014-09-08 13:45:00 +0200 | [diff] [blame] | 303 | |
Arturo Borrero | 9ba1f726 | 2014-09-08 13:45:00 +0200 | [diff] [blame] | 304 | MODULE_LICENSE("GPL"); |
Arturo Borrero Gonzalez | cd72751 | 2016-10-18 14:02:29 +0200 | [diff] [blame] | 305 | MODULE_AUTHOR("Arturo Borrero Gonzalez <arturo@debian.org>"); |
Pablo Neira Ayuso | 5142967 | 2019-05-24 22:26:34 +0200 | [diff] [blame] | 306 | MODULE_ALIAS_NFT_EXPR("masq"); |
Rob Gill | 4cacc39 | 2020-06-21 05:27:36 +0000 | [diff] [blame] | 307 | MODULE_DESCRIPTION("Netfilter nftables masquerade expression support"); |