1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2014 Arturo Borrero Gonzalez <arturo@debian.org> 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> 14 #include <net/netfilter/nf_nat_masquerade.h> 15 16 struct nft_masq { 17 u32 flags; 18 u8 sreg_proto_min; 19 u8 sreg_proto_max; 20 }; 21 22 static const struct nla_policy nft_masq_policy[NFTA_MASQ_MAX + 1] = { 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 }, 26 }; 27 28 static int nft_masq_validate(const struct nft_ctx *ctx, 29 const struct nft_expr *expr, 30 const struct nft_data **data) 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 } 41 42 static int nft_masq_init(const struct nft_ctx *ctx, 43 const struct nft_expr *expr, 44 const struct nlattr * const tb[]) 45 { 46 u32 plen = sizeof_field(struct nf_nat_range, min_proto.all); 47 struct nft_masq *priv = nft_expr_priv(expr); 48 int err; 49 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 } 55 56 if (tb[NFTA_MASQ_REG_PROTO_MIN]) { 57 err = nft_parse_register_load(tb[NFTA_MASQ_REG_PROTO_MIN], 58 &priv->sreg_proto_min, plen); 59 if (err < 0) 60 return err; 61 62 if (tb[NFTA_MASQ_REG_PROTO_MAX]) { 63 err = nft_parse_register_load(tb[NFTA_MASQ_REG_PROTO_MAX], 64 &priv->sreg_proto_max, 65 plen); 66 if (err < 0) 67 return err; 68 } else { 69 priv->sreg_proto_max = priv->sreg_proto_min; 70 } 71 } 72 73 return nf_ct_netns_get(ctx->net, ctx->family); 74 } 75 76 static int nft_masq_dump(struct sk_buff *skb, 77 const struct nft_expr *expr, bool reset) 78 { 79 const struct nft_masq *priv = nft_expr_priv(expr); 80 81 if (priv->flags != 0 && 82 nla_put_be32(skb, NFTA_MASQ_FLAGS, htonl(priv->flags))) 83 goto nla_put_failure; 84 85 if (priv->sreg_proto_min) { 86 if (nft_dump_register(skb, NFTA_MASQ_REG_PROTO_MIN, 87 priv->sreg_proto_min) || 88 nft_dump_register(skb, NFTA_MASQ_REG_PROTO_MAX, 89 priv->sreg_proto_max)) 90 goto nla_put_failure; 91 } 92 93 return 0; 94 95 nla_put_failure: 96 return -1; 97 } 98 99 static void nft_masq_eval(const struct nft_expr *expr, 100 struct nft_regs *regs, 101 const struct nft_pktinfo *pkt) 102 { 103 const struct nft_masq *priv = nft_expr_priv(expr); 104 struct nf_nat_range2 range; 105 106 memset(&range, 0, sizeof(range)); 107 range.flags = priv->flags; 108 if (priv->sreg_proto_min) { 109 range.min_proto.all = (__force __be16) 110 nft_reg_load16(®s->data[priv->sreg_proto_min]); 111 range.max_proto.all = (__force __be16) 112 nft_reg_load16(®s->data[priv->sreg_proto_max]); 113 } 114 115 switch (nft_pf(pkt)) { 116 case NFPROTO_IPV4: 117 regs->verdict.code = nf_nat_masquerade_ipv4(pkt->skb, 118 nft_hook(pkt), 119 &range, 120 nft_out(pkt)); 121 break; 122 #ifdef CONFIG_NF_TABLES_IPV6 123 case NFPROTO_IPV6: 124 regs->verdict.code = nf_nat_masquerade_ipv6(pkt->skb, &range, 125 nft_out(pkt)); 126 break; 127 #endif 128 default: 129 WARN_ON_ONCE(1); 130 break; 131 } 132 } 133 134 static void 135 nft_masq_ipv4_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr) 136 { 137 nf_ct_netns_put(ctx->net, NFPROTO_IPV4); 138 } 139 140 static struct nft_expr_type nft_masq_ipv4_type; 141 static const struct nft_expr_ops nft_masq_ipv4_ops = { 142 .type = &nft_masq_ipv4_type, 143 .size = NFT_EXPR_SIZE(sizeof(struct nft_masq)), 144 .eval = nft_masq_eval, 145 .init = nft_masq_init, 146 .destroy = nft_masq_ipv4_destroy, 147 .dump = nft_masq_dump, 148 .validate = nft_masq_validate, 149 .reduce = NFT_REDUCE_READONLY, 150 }; 151 152 static struct nft_expr_type nft_masq_ipv4_type __read_mostly = { 153 .family = NFPROTO_IPV4, 154 .name = "masq", 155 .ops = &nft_masq_ipv4_ops, 156 .policy = nft_masq_policy, 157 .maxattr = NFTA_MASQ_MAX, 158 .owner = THIS_MODULE, 159 }; 160 161 #ifdef CONFIG_NF_TABLES_IPV6 162 static void 163 nft_masq_ipv6_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr) 164 { 165 nf_ct_netns_put(ctx->net, NFPROTO_IPV6); 166 } 167 168 static struct nft_expr_type nft_masq_ipv6_type; 169 static const struct nft_expr_ops nft_masq_ipv6_ops = { 170 .type = &nft_masq_ipv6_type, 171 .size = NFT_EXPR_SIZE(sizeof(struct nft_masq)), 172 .eval = nft_masq_eval, 173 .init = nft_masq_init, 174 .destroy = nft_masq_ipv6_destroy, 175 .dump = nft_masq_dump, 176 .validate = nft_masq_validate, 177 .reduce = NFT_REDUCE_READONLY, 178 }; 179 180 static struct nft_expr_type nft_masq_ipv6_type __read_mostly = { 181 .family = NFPROTO_IPV6, 182 .name = "masq", 183 .ops = &nft_masq_ipv6_ops, 184 .policy = nft_masq_policy, 185 .maxattr = NFTA_MASQ_MAX, 186 .owner = THIS_MODULE, 187 }; 188 189 static int __init nft_masq_module_init_ipv6(void) 190 { 191 return nft_register_expr(&nft_masq_ipv6_type); 192 } 193 194 static void nft_masq_module_exit_ipv6(void) 195 { 196 nft_unregister_expr(&nft_masq_ipv6_type); 197 } 198 #else 199 static inline int nft_masq_module_init_ipv6(void) { return 0; } 200 static inline void nft_masq_module_exit_ipv6(void) {} 201 #endif 202 203 #ifdef CONFIG_NF_TABLES_INET 204 static void 205 nft_masq_inet_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr) 206 { 207 nf_ct_netns_put(ctx->net, NFPROTO_INET); 208 } 209 210 static struct nft_expr_type nft_masq_inet_type; 211 static const struct nft_expr_ops nft_masq_inet_ops = { 212 .type = &nft_masq_inet_type, 213 .size = NFT_EXPR_SIZE(sizeof(struct nft_masq)), 214 .eval = nft_masq_eval, 215 .init = nft_masq_init, 216 .destroy = nft_masq_inet_destroy, 217 .dump = nft_masq_dump, 218 .validate = nft_masq_validate, 219 .reduce = NFT_REDUCE_READONLY, 220 }; 221 222 static struct nft_expr_type nft_masq_inet_type __read_mostly = { 223 .family = NFPROTO_INET, 224 .name = "masq", 225 .ops = &nft_masq_inet_ops, 226 .policy = nft_masq_policy, 227 .maxattr = NFTA_MASQ_MAX, 228 .owner = THIS_MODULE, 229 }; 230 231 static int __init nft_masq_module_init_inet(void) 232 { 233 return nft_register_expr(&nft_masq_inet_type); 234 } 235 236 static void nft_masq_module_exit_inet(void) 237 { 238 nft_unregister_expr(&nft_masq_inet_type); 239 } 240 #else 241 static inline int nft_masq_module_init_inet(void) { return 0; } 242 static inline void nft_masq_module_exit_inet(void) {} 243 #endif 244 245 static int __init nft_masq_module_init(void) 246 { 247 int ret; 248 249 ret = nft_masq_module_init_ipv6(); 250 if (ret < 0) 251 return ret; 252 253 ret = nft_masq_module_init_inet(); 254 if (ret < 0) { 255 nft_masq_module_exit_ipv6(); 256 return ret; 257 } 258 259 ret = nft_register_expr(&nft_masq_ipv4_type); 260 if (ret < 0) { 261 nft_masq_module_exit_inet(); 262 nft_masq_module_exit_ipv6(); 263 return ret; 264 } 265 266 ret = nf_nat_masquerade_inet_register_notifiers(); 267 if (ret < 0) { 268 nft_masq_module_exit_ipv6(); 269 nft_masq_module_exit_inet(); 270 nft_unregister_expr(&nft_masq_ipv4_type); 271 return ret; 272 } 273 274 return ret; 275 } 276 277 static void __exit nft_masq_module_exit(void) 278 { 279 nft_masq_module_exit_ipv6(); 280 nft_masq_module_exit_inet(); 281 nft_unregister_expr(&nft_masq_ipv4_type); 282 nf_nat_masquerade_inet_unregister_notifiers(); 283 } 284 285 module_init(nft_masq_module_init); 286 module_exit(nft_masq_module_exit); 287 288 MODULE_LICENSE("GPL"); 289 MODULE_AUTHOR("Arturo Borrero Gonzalez <arturo@debian.org>"); 290 MODULE_ALIAS_NFT_EXPR("masq"); 291 MODULE_DESCRIPTION("Netfilter nftables masquerade expression support"); 292