Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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