1/*
2 * Copyright (c) 2014 Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
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
19const struct nla_policy nft_masq_policy[NFTA_MASQ_MAX + 1] = {
20	[NFTA_MASQ_FLAGS]	= { .type = NLA_U32 },
21};
22EXPORT_SYMBOL_GPL(nft_masq_policy);
23
24int nft_masq_validate(const struct nft_ctx *ctx,
25		      const struct nft_expr *expr,
26		      const struct nft_data **data)
27{
28	int err;
29
30	err = nft_chain_validate_dependency(ctx->chain, NFT_CHAIN_T_NAT);
31	if (err < 0)
32		return err;
33
34	return nft_chain_validate_hooks(ctx->chain,
35				        (1 << NF_INET_POST_ROUTING));
36}
37EXPORT_SYMBOL_GPL(nft_masq_validate);
38
39int nft_masq_init(const struct nft_ctx *ctx,
40		  const struct nft_expr *expr,
41		  const struct nlattr * const tb[])
42{
43	struct nft_masq *priv = nft_expr_priv(expr);
44	int err;
45
46	err = nft_masq_validate(ctx, expr, NULL);
47	if (err)
48		return err;
49
50	if (tb[NFTA_MASQ_FLAGS] == NULL)
51		return 0;
52
53	priv->flags = ntohl(nla_get_be32(tb[NFTA_MASQ_FLAGS]));
54	if (priv->flags & ~NF_NAT_RANGE_MASK)
55		return -EINVAL;
56
57	return 0;
58}
59EXPORT_SYMBOL_GPL(nft_masq_init);
60
61int nft_masq_dump(struct sk_buff *skb, const struct nft_expr *expr)
62{
63	const struct nft_masq *priv = nft_expr_priv(expr);
64
65	if (priv->flags == 0)
66		return 0;
67
68	if (nla_put_be32(skb, NFTA_MASQ_FLAGS, htonl(priv->flags)))
69		goto nla_put_failure;
70
71	return 0;
72
73nla_put_failure:
74	return -1;
75}
76EXPORT_SYMBOL_GPL(nft_masq_dump);
77
78MODULE_LICENSE("GPL");
79MODULE_AUTHOR("Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>");
80