1/*
2 * Copyright (c) 2015 Pablo Neira Ayuso <pablo@netfilter.org>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published by
6 * 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/ipv4/nf_dup_ipv4.h>
17
18struct nft_dup_ipv4 {
19 enum nft_registers sreg_addr:8;
20 enum nft_registers sreg_dev:8;
21};
22
23static void nft_dup_ipv4_eval(const struct nft_expr *expr,
24 struct nft_regs *regs,
25 const struct nft_pktinfo *pkt)
26{
27 struct nft_dup_ipv4 *priv = nft_expr_priv(expr);
28 struct in_addr gw = {
29 .s_addr = (__force __be32)regs->data[priv->sreg_addr],
30 };
31 int oif = priv->sreg_dev ? regs->data[priv->sreg_dev] : -1;
32
33 nf_dup_ipv4(nft_net(pkt), pkt->skb, nft_hook(pkt), &gw, oif);
34}
35
36static int nft_dup_ipv4_init(const struct nft_ctx *ctx,
37 const struct nft_expr *expr,
38 const struct nlattr * const tb[])
39{
40 struct nft_dup_ipv4 *priv = nft_expr_priv(expr);
41 int err;
42
43 if (tb[NFTA_DUP_SREG_ADDR] == NULL)
44 return -EINVAL;
45
46 priv->sreg_addr = nft_parse_register(tb[NFTA_DUP_SREG_ADDR]);
47 err = nft_validate_register_load(priv->sreg_addr, sizeof(struct in_addr));
48 if (err < 0)
49 return err;
50
51 if (tb[NFTA_DUP_SREG_DEV] != NULL) {
52 priv->sreg_dev = nft_parse_register(tb[NFTA_DUP_SREG_DEV]);
53 return nft_validate_register_load(priv->sreg_dev, sizeof(int));
54 }
55 return 0;
56}
57
58static int nft_dup_ipv4_dump(struct sk_buff *skb, const struct nft_expr *expr)
59{
60 struct nft_dup_ipv4 *priv = nft_expr_priv(expr);
61
62 if (nft_dump_register(skb, NFTA_DUP_SREG_ADDR, priv->sreg_addr))
63 goto nla_put_failure;
64 if (priv->sreg_dev &&
65 nft_dump_register(skb, NFTA_DUP_SREG_DEV, priv->sreg_dev))
66 goto nla_put_failure;
67
68 return 0;
69
70nla_put_failure:
71 return -1;
72}
73
74static struct nft_expr_type nft_dup_ipv4_type;
75static const struct nft_expr_ops nft_dup_ipv4_ops = {
76 .type = &nft_dup_ipv4_type,
77 .size = NFT_EXPR_SIZE(sizeof(struct nft_dup_ipv4)),
78 .eval = nft_dup_ipv4_eval,
79 .init = nft_dup_ipv4_init,
80 .dump = nft_dup_ipv4_dump,
81};
82
83static const struct nla_policy nft_dup_ipv4_policy[NFTA_DUP_MAX + 1] = {
84 [NFTA_DUP_SREG_ADDR] = { .type = NLA_U32 },
85 [NFTA_DUP_SREG_DEV] = { .type = NLA_U32 },
86};
87
88static struct nft_expr_type nft_dup_ipv4_type __read_mostly = {
89 .family = NFPROTO_IPV4,
90 .name = "dup",
91 .ops = &nft_dup_ipv4_ops,
92 .policy = nft_dup_ipv4_policy,
93 .maxattr = NFTA_DUP_MAX,
94 .owner = THIS_MODULE,
95};
96
97static int __init nft_dup_ipv4_module_init(void)
98{
99 return nft_register_expr(&nft_dup_ipv4_type);
100}
101
102static void __exit nft_dup_ipv4_module_exit(void)
103{
104 nft_unregister_expr(&nft_dup_ipv4_type);
105}
106
107module_init(nft_dup_ipv4_module_init);
108module_exit(nft_dup_ipv4_module_exit);
109
110MODULE_LICENSE("GPL");
111MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
112MODULE_ALIAS_NFT_AF_EXPR(AF_INET, "dup");
113