1 // SPDX-License-Identifier: GPL-2.0-only
3 * (C) 1999-2001 Paul `Rusty' Russell
4 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
5 * (C) 2011 Patrick McHardy <kaber@trash.net>
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/skbuff.h>
12 #include <linux/netfilter.h>
13 #include <linux/netfilter/x_tables.h>
14 #include <net/netfilter/nf_nat.h>
16 static int xt_nat_checkentry_v0(const struct xt_tgchk_param
*par
)
18 const struct nf_nat_ipv4_multi_range_compat
*mr
= par
->targinfo
;
20 if (mr
->rangesize
!= 1) {
21 pr_info_ratelimited("multiple ranges no longer supported\n");
24 return nf_ct_netns_get(par
->net
, par
->family
);
27 static int xt_nat_checkentry(const struct xt_tgchk_param
*par
)
29 return nf_ct_netns_get(par
->net
, par
->family
);
32 static void xt_nat_destroy(const struct xt_tgdtor_param
*par
)
34 nf_ct_netns_put(par
->net
, par
->family
);
37 static void xt_nat_convert_range(struct nf_nat_range2
*dst
,
38 const struct nf_nat_ipv4_range
*src
)
40 memset(&dst
->min_addr
, 0, sizeof(dst
->min_addr
));
41 memset(&dst
->max_addr
, 0, sizeof(dst
->max_addr
));
42 memset(&dst
->base_proto
, 0, sizeof(dst
->base_proto
));
44 dst
->flags
= src
->flags
;
45 dst
->min_addr
.ip
= src
->min_ip
;
46 dst
->max_addr
.ip
= src
->max_ip
;
47 dst
->min_proto
= src
->min
;
48 dst
->max_proto
= src
->max
;
52 xt_snat_target_v0(struct sk_buff
*skb
, const struct xt_action_param
*par
)
54 const struct nf_nat_ipv4_multi_range_compat
*mr
= par
->targinfo
;
55 struct nf_nat_range2 range
;
56 enum ip_conntrack_info ctinfo
;
59 ct
= nf_ct_get(skb
, &ctinfo
);
60 WARN_ON(!(ct
!= NULL
&&
61 (ctinfo
== IP_CT_NEW
|| ctinfo
== IP_CT_RELATED
||
62 ctinfo
== IP_CT_RELATED_REPLY
)));
64 xt_nat_convert_range(&range
, &mr
->range
[0]);
65 return nf_nat_setup_info(ct
, &range
, NF_NAT_MANIP_SRC
);
69 xt_dnat_target_v0(struct sk_buff
*skb
, const struct xt_action_param
*par
)
71 const struct nf_nat_ipv4_multi_range_compat
*mr
= par
->targinfo
;
72 struct nf_nat_range2 range
;
73 enum ip_conntrack_info ctinfo
;
76 ct
= nf_ct_get(skb
, &ctinfo
);
77 WARN_ON(!(ct
!= NULL
&&
78 (ctinfo
== IP_CT_NEW
|| ctinfo
== IP_CT_RELATED
)));
80 xt_nat_convert_range(&range
, &mr
->range
[0]);
81 return nf_nat_setup_info(ct
, &range
, NF_NAT_MANIP_DST
);
85 xt_snat_target_v1(struct sk_buff
*skb
, const struct xt_action_param
*par
)
87 const struct nf_nat_range
*range_v1
= par
->targinfo
;
88 struct nf_nat_range2 range
;
89 enum ip_conntrack_info ctinfo
;
92 ct
= nf_ct_get(skb
, &ctinfo
);
93 WARN_ON(!(ct
!= NULL
&&
94 (ctinfo
== IP_CT_NEW
|| ctinfo
== IP_CT_RELATED
||
95 ctinfo
== IP_CT_RELATED_REPLY
)));
97 memcpy(&range
, range_v1
, sizeof(*range_v1
));
98 memset(&range
.base_proto
, 0, sizeof(range
.base_proto
));
100 return nf_nat_setup_info(ct
, &range
, NF_NAT_MANIP_SRC
);
104 xt_dnat_target_v1(struct sk_buff
*skb
, const struct xt_action_param
*par
)
106 const struct nf_nat_range
*range_v1
= par
->targinfo
;
107 struct nf_nat_range2 range
;
108 enum ip_conntrack_info ctinfo
;
111 ct
= nf_ct_get(skb
, &ctinfo
);
112 WARN_ON(!(ct
!= NULL
&&
113 (ctinfo
== IP_CT_NEW
|| ctinfo
== IP_CT_RELATED
)));
115 memcpy(&range
, range_v1
, sizeof(*range_v1
));
116 memset(&range
.base_proto
, 0, sizeof(range
.base_proto
));
118 return nf_nat_setup_info(ct
, &range
, NF_NAT_MANIP_DST
);
122 xt_snat_target_v2(struct sk_buff
*skb
, const struct xt_action_param
*par
)
124 const struct nf_nat_range2
*range
= par
->targinfo
;
125 enum ip_conntrack_info ctinfo
;
128 ct
= nf_ct_get(skb
, &ctinfo
);
129 WARN_ON(!(ct
!= NULL
&&
130 (ctinfo
== IP_CT_NEW
|| ctinfo
== IP_CT_RELATED
||
131 ctinfo
== IP_CT_RELATED_REPLY
)));
133 return nf_nat_setup_info(ct
, range
, NF_NAT_MANIP_SRC
);
137 xt_dnat_target_v2(struct sk_buff
*skb
, const struct xt_action_param
*par
)
139 const struct nf_nat_range2
*range
= par
->targinfo
;
140 enum ip_conntrack_info ctinfo
;
143 ct
= nf_ct_get(skb
, &ctinfo
);
144 WARN_ON(!(ct
!= NULL
&&
145 (ctinfo
== IP_CT_NEW
|| ctinfo
== IP_CT_RELATED
)));
147 return nf_nat_setup_info(ct
, range
, NF_NAT_MANIP_DST
);
150 static struct xt_target xt_nat_target_reg
[] __read_mostly
= {
154 .checkentry
= xt_nat_checkentry_v0
,
155 .destroy
= xt_nat_destroy
,
156 .target
= xt_snat_target_v0
,
157 .targetsize
= sizeof(struct nf_nat_ipv4_multi_range_compat
),
158 .family
= NFPROTO_IPV4
,
160 .hooks
= (1 << NF_INET_POST_ROUTING
) |
161 (1 << NF_INET_LOCAL_IN
),
167 .checkentry
= xt_nat_checkentry_v0
,
168 .destroy
= xt_nat_destroy
,
169 .target
= xt_dnat_target_v0
,
170 .targetsize
= sizeof(struct nf_nat_ipv4_multi_range_compat
),
171 .family
= NFPROTO_IPV4
,
173 .hooks
= (1 << NF_INET_PRE_ROUTING
) |
174 (1 << NF_INET_LOCAL_OUT
),
180 .checkentry
= xt_nat_checkentry
,
181 .destroy
= xt_nat_destroy
,
182 .target
= xt_snat_target_v1
,
183 .targetsize
= sizeof(struct nf_nat_range
),
185 .hooks
= (1 << NF_INET_POST_ROUTING
) |
186 (1 << NF_INET_LOCAL_IN
),
192 .checkentry
= xt_nat_checkentry
,
193 .destroy
= xt_nat_destroy
,
194 .target
= xt_dnat_target_v1
,
195 .targetsize
= sizeof(struct nf_nat_range
),
197 .hooks
= (1 << NF_INET_PRE_ROUTING
) |
198 (1 << NF_INET_LOCAL_OUT
),
204 .checkentry
= xt_nat_checkentry
,
205 .destroy
= xt_nat_destroy
,
206 .target
= xt_snat_target_v2
,
207 .targetsize
= sizeof(struct nf_nat_range2
),
209 .hooks
= (1 << NF_INET_POST_ROUTING
) |
210 (1 << NF_INET_LOCAL_IN
),
216 .checkentry
= xt_nat_checkentry
,
217 .destroy
= xt_nat_destroy
,
218 .target
= xt_dnat_target_v2
,
219 .targetsize
= sizeof(struct nf_nat_range2
),
221 .hooks
= (1 << NF_INET_PRE_ROUTING
) |
222 (1 << NF_INET_LOCAL_OUT
),
227 static int __init
xt_nat_init(void)
229 return xt_register_targets(xt_nat_target_reg
,
230 ARRAY_SIZE(xt_nat_target_reg
));
233 static void __exit
xt_nat_exit(void)
235 xt_unregister_targets(xt_nat_target_reg
, ARRAY_SIZE(xt_nat_target_reg
));
238 module_init(xt_nat_init
);
239 module_exit(xt_nat_exit
);
241 MODULE_LICENSE("GPL");
242 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
243 MODULE_ALIAS("ipt_SNAT");
244 MODULE_ALIAS("ipt_DNAT");
245 MODULE_ALIAS("ip6t_SNAT");
246 MODULE_ALIAS("ip6t_DNAT");
247 MODULE_DESCRIPTION("SNAT and DNAT targets support");