GNU Linux-libre 4.14.290-gnu1
[releases.git] / net / ipv4 / netfilter / nft_redir_ipv4.c
1 /*
2  * Copyright (c) 2014 Arturo Borrero Gonzalez <arturo@debian.org>
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/nf_nat_redirect.h>
18 #include <net/netfilter/nft_redir.h>
19
20 static void nft_redir_ipv4_eval(const struct nft_expr *expr,
21                                 struct nft_regs *regs,
22                                 const struct nft_pktinfo *pkt)
23 {
24         struct nft_redir *priv = nft_expr_priv(expr);
25         struct nf_nat_ipv4_multi_range_compat mr;
26
27         memset(&mr, 0, sizeof(mr));
28         if (priv->sreg_proto_min) {
29                 mr.range[0].min.all = (__force __be16)nft_reg_load16(
30                         &regs->data[priv->sreg_proto_min]);
31                 mr.range[0].max.all = (__force __be16)nft_reg_load16(
32                         &regs->data[priv->sreg_proto_max]);
33                 mr.range[0].flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
34         }
35
36         mr.range[0].flags |= priv->flags;
37
38         regs->verdict.code = nf_nat_redirect_ipv4(pkt->skb, &mr, nft_hook(pkt));
39 }
40
41 static void
42 nft_redir_ipv4_destroy(const struct nft_ctx *ctx, const struct nft_expr *expr)
43 {
44         nf_ct_netns_put(ctx->net, NFPROTO_IPV4);
45 }
46
47 static struct nft_expr_type nft_redir_ipv4_type;
48 static const struct nft_expr_ops nft_redir_ipv4_ops = {
49         .type           = &nft_redir_ipv4_type,
50         .size           = NFT_EXPR_SIZE(sizeof(struct nft_redir)),
51         .eval           = nft_redir_ipv4_eval,
52         .init           = nft_redir_init,
53         .destroy        = nft_redir_ipv4_destroy,
54         .dump           = nft_redir_dump,
55         .validate       = nft_redir_validate,
56 };
57
58 static struct nft_expr_type nft_redir_ipv4_type __read_mostly = {
59         .family         = NFPROTO_IPV4,
60         .name           = "redir",
61         .ops            = &nft_redir_ipv4_ops,
62         .policy         = nft_redir_policy,
63         .maxattr        = NFTA_REDIR_MAX,
64         .owner          = THIS_MODULE,
65 };
66
67 static int __init nft_redir_ipv4_module_init(void)
68 {
69         return nft_register_expr(&nft_redir_ipv4_type);
70 }
71
72 static void __exit nft_redir_ipv4_module_exit(void)
73 {
74         nft_unregister_expr(&nft_redir_ipv4_type);
75 }
76
77 module_init(nft_redir_ipv4_module_init);
78 module_exit(nft_redir_ipv4_module_exit);
79
80 MODULE_LICENSE("GPL");
81 MODULE_AUTHOR("Arturo Borrero Gonzalez <arturo@debian.org>");
82 MODULE_ALIAS_NFT_AF_EXPR(AF_INET, "redir");