Commit | Line | Data |
---|---|---|
d877f071 PNA |
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 | ||
18 | struct nft_dup_ipv4 { | |
19 | enum nft_registers sreg_addr:8; | |
20 | enum nft_registers sreg_dev:8; | |
21 | }; | |
22 | ||
23 | static 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 = { | |
59e26423 | 29 | .s_addr = (__force __be32)regs->data[priv->sreg_addr], |
d877f071 PNA |
30 | }; |
31 | int oif = regs->data[priv->sreg_dev]; | |
32 | ||
206e8c00 | 33 | nf_dup_ipv4(pkt->net, pkt->skb, pkt->hook, &gw, oif); |
d877f071 PNA |
34 | } |
35 | ||
36 | static 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 | ||
58 | static 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 | nft_dump_register(skb, NFTA_DUP_SREG_DEV, priv->sreg_dev)) | |
64 | goto nla_put_failure; | |
65 | ||
66 | return 0; | |
67 | ||
68 | nla_put_failure: | |
69 | return -1; | |
70 | } | |
71 | ||
72 | static struct nft_expr_type nft_dup_ipv4_type; | |
73 | static const struct nft_expr_ops nft_dup_ipv4_ops = { | |
74 | .type = &nft_dup_ipv4_type, | |
75 | .size = NFT_EXPR_SIZE(sizeof(struct nft_dup_ipv4)), | |
76 | .eval = nft_dup_ipv4_eval, | |
77 | .init = nft_dup_ipv4_init, | |
78 | .dump = nft_dup_ipv4_dump, | |
79 | }; | |
80 | ||
81 | static const struct nla_policy nft_dup_ipv4_policy[NFTA_DUP_MAX + 1] = { | |
82 | [NFTA_DUP_SREG_ADDR] = { .type = NLA_U32 }, | |
83 | [NFTA_DUP_SREG_DEV] = { .type = NLA_U32 }, | |
84 | }; | |
85 | ||
86 | static struct nft_expr_type nft_dup_ipv4_type __read_mostly = { | |
87 | .family = NFPROTO_IPV4, | |
88 | .name = "dup", | |
89 | .ops = &nft_dup_ipv4_ops, | |
90 | .policy = nft_dup_ipv4_policy, | |
91 | .maxattr = NFTA_DUP_MAX, | |
92 | .owner = THIS_MODULE, | |
93 | }; | |
94 | ||
95 | static int __init nft_dup_ipv4_module_init(void) | |
96 | { | |
97 | return nft_register_expr(&nft_dup_ipv4_type); | |
98 | } | |
99 | ||
100 | static void __exit nft_dup_ipv4_module_exit(void) | |
101 | { | |
102 | nft_unregister_expr(&nft_dup_ipv4_type); | |
103 | } | |
104 | ||
105 | module_init(nft_dup_ipv4_module_init); | |
106 | module_exit(nft_dup_ipv4_module_exit); | |
107 | ||
108 | MODULE_LICENSE("GPL"); | |
109 | MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>"); | |
110 | MODULE_ALIAS_NFT_AF_EXPR(AF_INET, "dup"); |