]> asedeno.scripts.mit.edu Git - linux.git/blob - net/netfilter/xt_nat.c
wil6210: rate limit wil_rx_refill error
[linux.git] / net / netfilter / xt_nat.c
1 /*
2  * (C) 1999-2001 Paul `Rusty' Russell
3  * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
4  * (C) 2011 Patrick McHardy <kaber@trash.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13 #include <linux/module.h>
14 #include <linux/skbuff.h>
15 #include <linux/netfilter.h>
16 #include <linux/netfilter/x_tables.h>
17 #include <net/netfilter/nf_nat_core.h>
18
19 static int xt_nat_checkentry_v0(const struct xt_tgchk_param *par)
20 {
21         const struct nf_nat_ipv4_multi_range_compat *mr = par->targinfo;
22
23         if (mr->rangesize != 1) {
24                 pr_info_ratelimited("multiple ranges no longer supported\n");
25                 return -EINVAL;
26         }
27         return nf_ct_netns_get(par->net, par->family);
28 }
29
30 static int xt_nat_checkentry(const struct xt_tgchk_param *par)
31 {
32         return nf_ct_netns_get(par->net, par->family);
33 }
34
35 static void xt_nat_destroy(const struct xt_tgdtor_param *par)
36 {
37         nf_ct_netns_put(par->net, par->family);
38 }
39
40 static void xt_nat_convert_range(struct nf_nat_range *dst,
41                                  const struct nf_nat_ipv4_range *src)
42 {
43         memset(&dst->min_addr, 0, sizeof(dst->min_addr));
44         memset(&dst->max_addr, 0, sizeof(dst->max_addr));
45
46         dst->flags       = src->flags;
47         dst->min_addr.ip = src->min_ip;
48         dst->max_addr.ip = src->max_ip;
49         dst->min_proto   = src->min;
50         dst->max_proto   = src->max;
51 }
52
53 static unsigned int
54 xt_snat_target_v0(struct sk_buff *skb, const struct xt_action_param *par)
55 {
56         const struct nf_nat_ipv4_multi_range_compat *mr = par->targinfo;
57         struct nf_nat_range range;
58         enum ip_conntrack_info ctinfo;
59         struct nf_conn *ct;
60
61         ct = nf_ct_get(skb, &ctinfo);
62         WARN_ON(!(ct != NULL &&
63                  (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED ||
64                   ctinfo == IP_CT_RELATED_REPLY)));
65
66         xt_nat_convert_range(&range, &mr->range[0]);
67         return nf_nat_setup_info(ct, &range, NF_NAT_MANIP_SRC);
68 }
69
70 static unsigned int
71 xt_dnat_target_v0(struct sk_buff *skb, const struct xt_action_param *par)
72 {
73         const struct nf_nat_ipv4_multi_range_compat *mr = par->targinfo;
74         struct nf_nat_range range;
75         enum ip_conntrack_info ctinfo;
76         struct nf_conn *ct;
77
78         ct = nf_ct_get(skb, &ctinfo);
79         WARN_ON(!(ct != NULL &&
80                  (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED)));
81
82         xt_nat_convert_range(&range, &mr->range[0]);
83         return nf_nat_setup_info(ct, &range, NF_NAT_MANIP_DST);
84 }
85
86 static unsigned int
87 xt_snat_target_v1(struct sk_buff *skb, const struct xt_action_param *par)
88 {
89         const struct nf_nat_range *range = par->targinfo;
90         enum ip_conntrack_info ctinfo;
91         struct nf_conn *ct;
92
93         ct = nf_ct_get(skb, &ctinfo);
94         WARN_ON(!(ct != NULL &&
95                  (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED ||
96                   ctinfo == IP_CT_RELATED_REPLY)));
97
98         return nf_nat_setup_info(ct, range, NF_NAT_MANIP_SRC);
99 }
100
101 static unsigned int
102 xt_dnat_target_v1(struct sk_buff *skb, const struct xt_action_param *par)
103 {
104         const struct nf_nat_range *range = par->targinfo;
105         enum ip_conntrack_info ctinfo;
106         struct nf_conn *ct;
107
108         ct = nf_ct_get(skb, &ctinfo);
109         WARN_ON(!(ct != NULL &&
110                  (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED)));
111
112         return nf_nat_setup_info(ct, range, NF_NAT_MANIP_DST);
113 }
114
115 static struct xt_target xt_nat_target_reg[] __read_mostly = {
116         {
117                 .name           = "SNAT",
118                 .revision       = 0,
119                 .checkentry     = xt_nat_checkentry_v0,
120                 .destroy        = xt_nat_destroy,
121                 .target         = xt_snat_target_v0,
122                 .targetsize     = sizeof(struct nf_nat_ipv4_multi_range_compat),
123                 .family         = NFPROTO_IPV4,
124                 .table          = "nat",
125                 .hooks          = (1 << NF_INET_POST_ROUTING) |
126                                   (1 << NF_INET_LOCAL_IN),
127                 .me             = THIS_MODULE,
128         },
129         {
130                 .name           = "DNAT",
131                 .revision       = 0,
132                 .checkentry     = xt_nat_checkentry_v0,
133                 .destroy        = xt_nat_destroy,
134                 .target         = xt_dnat_target_v0,
135                 .targetsize     = sizeof(struct nf_nat_ipv4_multi_range_compat),
136                 .family         = NFPROTO_IPV4,
137                 .table          = "nat",
138                 .hooks          = (1 << NF_INET_PRE_ROUTING) |
139                                   (1 << NF_INET_LOCAL_OUT),
140                 .me             = THIS_MODULE,
141         },
142         {
143                 .name           = "SNAT",
144                 .revision       = 1,
145                 .checkentry     = xt_nat_checkentry,
146                 .destroy        = xt_nat_destroy,
147                 .target         = xt_snat_target_v1,
148                 .targetsize     = sizeof(struct nf_nat_range),
149                 .table          = "nat",
150                 .hooks          = (1 << NF_INET_POST_ROUTING) |
151                                   (1 << NF_INET_LOCAL_IN),
152                 .me             = THIS_MODULE,
153         },
154         {
155                 .name           = "DNAT",
156                 .revision       = 1,
157                 .checkentry     = xt_nat_checkentry,
158                 .destroy        = xt_nat_destroy,
159                 .target         = xt_dnat_target_v1,
160                 .targetsize     = sizeof(struct nf_nat_range),
161                 .table          = "nat",
162                 .hooks          = (1 << NF_INET_PRE_ROUTING) |
163                                   (1 << NF_INET_LOCAL_OUT),
164                 .me             = THIS_MODULE,
165         },
166 };
167
168 static int __init xt_nat_init(void)
169 {
170         return xt_register_targets(xt_nat_target_reg,
171                                    ARRAY_SIZE(xt_nat_target_reg));
172 }
173
174 static void __exit xt_nat_exit(void)
175 {
176         xt_unregister_targets(xt_nat_target_reg, ARRAY_SIZE(xt_nat_target_reg));
177 }
178
179 module_init(xt_nat_init);
180 module_exit(xt_nat_exit);
181
182 MODULE_LICENSE("GPL");
183 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
184 MODULE_ALIAS("ipt_SNAT");
185 MODULE_ALIAS("ipt_DNAT");
186 MODULE_ALIAS("ip6t_SNAT");
187 MODULE_ALIAS("ip6t_DNAT");