Arturo Borrero | 9ba1f726 | 2014-09-08 13:45:00 +0200 | [diff] [blame] | 1 | /* |
Arturo Borrero Gonzalez | cd72751 | 2016-10-18 14:02:29 +0200 | [diff] [blame] | 2 | * Copyright (c) 2014 Arturo Borrero Gonzalez <arturo@debian.org> |
Arturo Borrero | 9ba1f726 | 2014-09-08 13:45:00 +0200 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/netlink.h> |
| 13 | #include <linux/netfilter.h> |
| 14 | #include <linux/netfilter/nf_tables.h> |
| 15 | #include <net/netfilter/nf_tables.h> |
| 16 | #include <net/netfilter/nf_nat.h> |
| 17 | #include <net/netfilter/nft_masq.h> |
| 18 | |
| 19 | const struct nla_policy nft_masq_policy[NFTA_MASQ_MAX + 1] = { |
Pablo Neira Ayuso | 8a6bf5d | 2016-03-01 19:55:14 +0100 | [diff] [blame] | 20 | [NFTA_MASQ_FLAGS] = { .type = NLA_U32 }, |
| 21 | [NFTA_MASQ_REG_PROTO_MIN] = { .type = NLA_U32 }, |
| 22 | [NFTA_MASQ_REG_PROTO_MAX] = { .type = NLA_U32 }, |
Arturo Borrero | 9ba1f726 | 2014-09-08 13:45:00 +0200 | [diff] [blame] | 23 | }; |
| 24 | EXPORT_SYMBOL_GPL(nft_masq_policy); |
| 25 | |
Pablo Neira Ayuso | 75e8d06 | 2015-01-14 15:33:57 +0100 | [diff] [blame] | 26 | int nft_masq_validate(const struct nft_ctx *ctx, |
| 27 | const struct nft_expr *expr, |
| 28 | const struct nft_data **data) |
| 29 | { |
| 30 | int err; |
| 31 | |
| 32 | err = nft_chain_validate_dependency(ctx->chain, NFT_CHAIN_T_NAT); |
| 33 | if (err < 0) |
| 34 | return err; |
| 35 | |
| 36 | return nft_chain_validate_hooks(ctx->chain, |
| 37 | (1 << NF_INET_POST_ROUTING)); |
| 38 | } |
| 39 | EXPORT_SYMBOL_GPL(nft_masq_validate); |
| 40 | |
Arturo Borrero | 9ba1f726 | 2014-09-08 13:45:00 +0200 | [diff] [blame] | 41 | int nft_masq_init(const struct nft_ctx *ctx, |
| 42 | const struct nft_expr *expr, |
| 43 | const struct nlattr * const tb[]) |
| 44 | { |
Pablo Neira Ayuso | 8a6bf5d | 2016-03-01 19:55:14 +0100 | [diff] [blame] | 45 | u32 plen = FIELD_SIZEOF(struct nf_nat_range, min_addr.all); |
Arturo Borrero | 9ba1f726 | 2014-09-08 13:45:00 +0200 | [diff] [blame] | 46 | struct nft_masq *priv = nft_expr_priv(expr); |
Pablo Neira Ayuso | 7210e4e | 2014-10-13 19:50:22 +0200 | [diff] [blame] | 47 | int err; |
| 48 | |
Pablo Neira Ayuso | 8a6bf5d | 2016-03-01 19:55:14 +0100 | [diff] [blame] | 49 | if (tb[NFTA_MASQ_FLAGS]) { |
| 50 | priv->flags = ntohl(nla_get_be32(tb[NFTA_MASQ_FLAGS])); |
| 51 | if (priv->flags & ~NF_NAT_RANGE_MASK) |
| 52 | return -EINVAL; |
| 53 | } |
Arturo Borrero | 9ba1f726 | 2014-09-08 13:45:00 +0200 | [diff] [blame] | 54 | |
Pablo Neira Ayuso | 8a6bf5d | 2016-03-01 19:55:14 +0100 | [diff] [blame] | 55 | if (tb[NFTA_MASQ_REG_PROTO_MIN]) { |
| 56 | priv->sreg_proto_min = |
| 57 | nft_parse_register(tb[NFTA_MASQ_REG_PROTO_MIN]); |
| 58 | |
| 59 | err = nft_validate_register_load(priv->sreg_proto_min, plen); |
| 60 | if (err < 0) |
| 61 | return err; |
| 62 | |
| 63 | if (tb[NFTA_MASQ_REG_PROTO_MAX]) { |
| 64 | priv->sreg_proto_max = |
| 65 | nft_parse_register(tb[NFTA_MASQ_REG_PROTO_MAX]); |
| 66 | |
| 67 | err = nft_validate_register_load(priv->sreg_proto_max, |
| 68 | plen); |
| 69 | if (err < 0) |
| 70 | return err; |
| 71 | } else { |
| 72 | priv->sreg_proto_max = priv->sreg_proto_min; |
| 73 | } |
| 74 | } |
Arturo Borrero | 9ba1f726 | 2014-09-08 13:45:00 +0200 | [diff] [blame] | 75 | |
Pablo Neira Ayuso | 36596da | 2018-01-09 02:38:03 +0100 | [diff] [blame] | 76 | return nf_ct_netns_get(ctx->net, ctx->family); |
Arturo Borrero | 9ba1f726 | 2014-09-08 13:45:00 +0200 | [diff] [blame] | 77 | } |
| 78 | EXPORT_SYMBOL_GPL(nft_masq_init); |
| 79 | |
| 80 | int nft_masq_dump(struct sk_buff *skb, const struct nft_expr *expr) |
| 81 | { |
| 82 | const struct nft_masq *priv = nft_expr_priv(expr); |
| 83 | |
Pablo Neira Ayuso | 8a6bf5d | 2016-03-01 19:55:14 +0100 | [diff] [blame] | 84 | if (priv->flags != 0 && |
| 85 | nla_put_be32(skb, NFTA_MASQ_FLAGS, htonl(priv->flags))) |
Arturo Borrero | 9ba1f726 | 2014-09-08 13:45:00 +0200 | [diff] [blame] | 86 | goto nla_put_failure; |
| 87 | |
Pablo Neira Ayuso | 8a6bf5d | 2016-03-01 19:55:14 +0100 | [diff] [blame] | 88 | if (priv->sreg_proto_min) { |
| 89 | if (nft_dump_register(skb, NFTA_MASQ_REG_PROTO_MIN, |
| 90 | priv->sreg_proto_min) || |
| 91 | nft_dump_register(skb, NFTA_MASQ_REG_PROTO_MAX, |
| 92 | priv->sreg_proto_max)) |
| 93 | goto nla_put_failure; |
| 94 | } |
| 95 | |
Arturo Borrero | 9ba1f726 | 2014-09-08 13:45:00 +0200 | [diff] [blame] | 96 | return 0; |
| 97 | |
| 98 | nla_put_failure: |
| 99 | return -1; |
| 100 | } |
| 101 | EXPORT_SYMBOL_GPL(nft_masq_dump); |
| 102 | |
Arturo Borrero | 9ba1f726 | 2014-09-08 13:45:00 +0200 | [diff] [blame] | 103 | MODULE_LICENSE("GPL"); |
Arturo Borrero Gonzalez | cd72751 | 2016-10-18 14:02:29 +0200 | [diff] [blame] | 104 | MODULE_AUTHOR("Arturo Borrero Gonzalez <arturo@debian.org>"); |