Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (c) 2014 Arturo Borrero Gonzalez <arturo@debian.org>
0004  */
0005 
0006 #include <linux/kernel.h>
0007 #include <linux/init.h>
0008 #include <linux/module.h>
0009 #include <linux/netlink.h>
0010 #include <linux/netfilter.h>
0011 #include <linux/netfilter/nf_tables.h>
0012 #include <net/netfilter/nf_nat.h>
0013 #include <net/netfilter/nf_nat_redirect.h>
0014 #include <net/netfilter/nf_tables.h>
0015 
0016 struct nft_redir {
0017     u8          sreg_proto_min;
0018     u8          sreg_proto_max;
0019     u16         flags;
0020 };
0021 
0022 static const struct nla_policy nft_redir_policy[NFTA_REDIR_MAX + 1] = {
0023     [NFTA_REDIR_REG_PROTO_MIN]  = { .type = NLA_U32 },
0024     [NFTA_REDIR_REG_PROTO_MAX]  = { .type = NLA_U32 },
0025     [NFTA_REDIR_FLAGS]      = { .type = NLA_U32 },
0026 };
0027 
0028 static int nft_redir_validate(const struct nft_ctx *ctx,
0029                   const struct nft_expr *expr,
0030                   const struct nft_data **data)
0031 {
0032     int err;
0033 
0034     err = nft_chain_validate_dependency(ctx->chain, NFT_CHAIN_T_NAT);
0035     if (err < 0)
0036         return err;
0037 
0038     return nft_chain_validate_hooks(ctx->chain,
0039                     (1 << NF_INET_PRE_ROUTING) |
0040                     (1 << NF_INET_LOCAL_OUT));
0041 }
0042 
0043 static int nft_redir_init(const struct nft_ctx *ctx,
0044               const struct nft_expr *expr,
0045               const struct nlattr * const tb[])
0046 {
0047     struct nft_redir *priv = nft_expr_priv(expr);
0048     unsigned int plen;
0049     int err;
0050 
0051     plen = sizeof_field(struct nf_nat_range, min_addr.all);
0052     if (tb[NFTA_REDIR_REG_PROTO_MIN]) {
0053         err = nft_parse_register_load(tb[NFTA_REDIR_REG_PROTO_MIN],
0054                           &priv->sreg_proto_min, plen);
0055         if (err < 0)
0056             return err;
0057 
0058         if (tb[NFTA_REDIR_REG_PROTO_MAX]) {
0059             err = nft_parse_register_load(tb[NFTA_REDIR_REG_PROTO_MAX],
0060                               &priv->sreg_proto_max,
0061                               plen);
0062             if (err < 0)
0063                 return err;
0064         } else {
0065             priv->sreg_proto_max = priv->sreg_proto_min;
0066         }
0067     }
0068 
0069     if (tb[NFTA_REDIR_FLAGS]) {
0070         priv->flags = ntohl(nla_get_be32(tb[NFTA_REDIR_FLAGS]));
0071         if (priv->flags & ~NF_NAT_RANGE_MASK)
0072             return -EINVAL;
0073     }
0074 
0075     return nf_ct_netns_get(ctx->net, ctx->family);
0076 }
0077 
0078 static int nft_redir_dump(struct sk_buff *skb, const struct nft_expr *expr)
0079 {
0080     const struct nft_redir *priv = nft_expr_priv(expr);
0081 
0082     if (priv->sreg_proto_min) {
0083         if (nft_dump_register(skb, NFTA_REDIR_REG_PROTO_MIN,
0084                       priv->sreg_proto_min))
0085             goto nla_put_failure;
0086         if (nft_dump_register(skb, NFTA_REDIR_REG_PROTO_MAX,
0087                       priv->sreg_proto_max))
0088             goto nla_put_failure;
0089     }
0090 
0091     if (priv->flags != 0 &&
0092         nla_put_be32(skb, NFTA_REDIR_FLAGS, htonl(priv->flags)))
0093             goto nla_put_failure;
0094 
0095     return 0;
0096 
0097 nla_put_failure:
0098     return -1;
0099 }
0100 
0101 static void nft_redir_ipv4_eval(const struct nft_expr *expr,
0102                 struct nft_regs *regs,
0103                 const struct nft_pktinfo *pkt)
0104 {
0105     struct nft_redir *priv = nft_expr_priv(expr);
0106     struct nf_nat_ipv4_multi_range_compat mr;
0107 
0108     memset(&mr, 0, sizeof(mr));
0109     if (priv->sreg_proto_min) {
0110         mr.range[0].min.all = (__force __be16)nft_reg_load16(
0111             &regs->data[priv->sreg_proto_min]);
0112         mr.range[0].max.all = (__force __be16)nft_reg_load16(
0113             &regs->data[priv->sreg_proto_max]);
0114         mr.range[0].flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
0115     }
0116 
0117     mr.range[0].flags |= priv->flags;
0118 
0119     regs->verdict.code = nf_nat_redirect_ipv4(pkt->skb, &mr, nft_hook(pkt));
0120 }
0121 
0122 static void
0123 nft_redir_ipv4_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
0124 {
0125     nf_ct_netns_put(ctx->net, NFPROTO_IPV4);
0126 }
0127 
0128 static struct nft_expr_type nft_redir_ipv4_type;
0129 static const struct nft_expr_ops nft_redir_ipv4_ops = {
0130     .type       = &nft_redir_ipv4_type,
0131     .size       = NFT_EXPR_SIZE(sizeof(struct nft_redir)),
0132     .eval       = nft_redir_ipv4_eval,
0133     .init       = nft_redir_init,
0134     .destroy    = nft_redir_ipv4_destroy,
0135     .dump       = nft_redir_dump,
0136     .validate   = nft_redir_validate,
0137     .reduce     = NFT_REDUCE_READONLY,
0138 };
0139 
0140 static struct nft_expr_type nft_redir_ipv4_type __read_mostly = {
0141     .family     = NFPROTO_IPV4,
0142     .name       = "redir",
0143     .ops        = &nft_redir_ipv4_ops,
0144     .policy     = nft_redir_policy,
0145     .maxattr    = NFTA_REDIR_MAX,
0146     .owner      = THIS_MODULE,
0147 };
0148 
0149 #ifdef CONFIG_NF_TABLES_IPV6
0150 static void nft_redir_ipv6_eval(const struct nft_expr *expr,
0151                 struct nft_regs *regs,
0152                 const struct nft_pktinfo *pkt)
0153 {
0154     struct nft_redir *priv = nft_expr_priv(expr);
0155     struct nf_nat_range2 range;
0156 
0157     memset(&range, 0, sizeof(range));
0158     if (priv->sreg_proto_min) {
0159         range.min_proto.all = (__force __be16)nft_reg_load16(
0160             &regs->data[priv->sreg_proto_min]);
0161         range.max_proto.all = (__force __be16)nft_reg_load16(
0162             &regs->data[priv->sreg_proto_max]);
0163         range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
0164     }
0165 
0166     range.flags |= priv->flags;
0167 
0168     regs->verdict.code =
0169         nf_nat_redirect_ipv6(pkt->skb, &range, nft_hook(pkt));
0170 }
0171 
0172 static void
0173 nft_redir_ipv6_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
0174 {
0175     nf_ct_netns_put(ctx->net, NFPROTO_IPV6);
0176 }
0177 
0178 static struct nft_expr_type nft_redir_ipv6_type;
0179 static const struct nft_expr_ops nft_redir_ipv6_ops = {
0180     .type       = &nft_redir_ipv6_type,
0181     .size       = NFT_EXPR_SIZE(sizeof(struct nft_redir)),
0182     .eval       = nft_redir_ipv6_eval,
0183     .init       = nft_redir_init,
0184     .destroy    = nft_redir_ipv6_destroy,
0185     .dump       = nft_redir_dump,
0186     .validate   = nft_redir_validate,
0187     .reduce     = NFT_REDUCE_READONLY,
0188 };
0189 
0190 static struct nft_expr_type nft_redir_ipv6_type __read_mostly = {
0191     .family     = NFPROTO_IPV6,
0192     .name       = "redir",
0193     .ops        = &nft_redir_ipv6_ops,
0194     .policy     = nft_redir_policy,
0195     .maxattr    = NFTA_REDIR_MAX,
0196     .owner      = THIS_MODULE,
0197 };
0198 #endif
0199 
0200 #ifdef CONFIG_NF_TABLES_INET
0201 static void nft_redir_inet_eval(const struct nft_expr *expr,
0202                 struct nft_regs *regs,
0203                 const struct nft_pktinfo *pkt)
0204 {
0205     switch (nft_pf(pkt)) {
0206     case NFPROTO_IPV4:
0207         return nft_redir_ipv4_eval(expr, regs, pkt);
0208     case NFPROTO_IPV6:
0209         return nft_redir_ipv6_eval(expr, regs, pkt);
0210     }
0211 
0212     WARN_ON_ONCE(1);
0213 }
0214 
0215 static void
0216 nft_redir_inet_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
0217 {
0218     nf_ct_netns_put(ctx->net, NFPROTO_INET);
0219 }
0220 
0221 static struct nft_expr_type nft_redir_inet_type;
0222 static const struct nft_expr_ops nft_redir_inet_ops = {
0223     .type       = &nft_redir_inet_type,
0224     .size       = NFT_EXPR_SIZE(sizeof(struct nft_redir)),
0225     .eval       = nft_redir_inet_eval,
0226     .init       = nft_redir_init,
0227     .destroy    = nft_redir_inet_destroy,
0228     .dump       = nft_redir_dump,
0229     .validate   = nft_redir_validate,
0230     .reduce     = NFT_REDUCE_READONLY,
0231 };
0232 
0233 static struct nft_expr_type nft_redir_inet_type __read_mostly = {
0234     .family     = NFPROTO_INET,
0235     .name       = "redir",
0236     .ops        = &nft_redir_inet_ops,
0237     .policy     = nft_redir_policy,
0238     .maxattr    = NFTA_MASQ_MAX,
0239     .owner      = THIS_MODULE,
0240 };
0241 
0242 static int __init nft_redir_module_init_inet(void)
0243 {
0244     return nft_register_expr(&nft_redir_inet_type);
0245 }
0246 #else
0247 static inline int nft_redir_module_init_inet(void) { return 0; }
0248 #endif
0249 
0250 static int __init nft_redir_module_init(void)
0251 {
0252     int ret = nft_register_expr(&nft_redir_ipv4_type);
0253 
0254     if (ret)
0255         return ret;
0256 
0257 #ifdef CONFIG_NF_TABLES_IPV6
0258     ret = nft_register_expr(&nft_redir_ipv6_type);
0259     if (ret) {
0260         nft_unregister_expr(&nft_redir_ipv4_type);
0261         return ret;
0262     }
0263 #endif
0264 
0265     ret = nft_redir_module_init_inet();
0266     if (ret < 0) {
0267         nft_unregister_expr(&nft_redir_ipv4_type);
0268 #ifdef CONFIG_NF_TABLES_IPV6
0269         nft_unregister_expr(&nft_redir_ipv6_type);
0270 #endif
0271         return ret;
0272     }
0273 
0274     return ret;
0275 }
0276 
0277 static void __exit nft_redir_module_exit(void)
0278 {
0279     nft_unregister_expr(&nft_redir_ipv4_type);
0280 #ifdef CONFIG_NF_TABLES_IPV6
0281     nft_unregister_expr(&nft_redir_ipv6_type);
0282 #endif
0283 #ifdef CONFIG_NF_TABLES_INET
0284     nft_unregister_expr(&nft_redir_inet_type);
0285 #endif
0286 }
0287 
0288 module_init(nft_redir_module_init);
0289 module_exit(nft_redir_module_exit);
0290 
0291 MODULE_LICENSE("GPL");
0292 MODULE_AUTHOR("Arturo Borrero Gonzalez <arturo@debian.org>");
0293 MODULE_ALIAS_NFT_EXPR("redir");
0294 MODULE_DESCRIPTION("Netfilter nftables redirect support");