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>
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.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
19 static int xt_nat_checkentry_v0(const struct xt_tgchk_param
*par
)
21 const struct nf_nat_ipv4_multi_range_compat
*mr
= par
->targinfo
;
23 if (mr
->rangesize
!= 1) {
24 pr_info_ratelimited("multiple ranges no longer supported\n");
27 return nf_ct_netns_get(par
->net
, par
->family
);
30 static int xt_nat_checkentry(const struct xt_tgchk_param
*par
)
32 return nf_ct_netns_get(par
->net
, par
->family
);
35 static void xt_nat_destroy(const struct xt_tgdtor_param
*par
)
37 nf_ct_netns_put(par
->net
, par
->family
);
40 static void xt_nat_convert_range(struct nf_nat_range
*dst
,
41 const struct nf_nat_ipv4_range
*src
)
43 memset(&dst
->min_addr
, 0, sizeof(dst
->min_addr
));
44 memset(&dst
->max_addr
, 0, sizeof(dst
->max_addr
));
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
;
54 xt_snat_target_v0(struct sk_buff
*skb
, const struct xt_action_param
*par
)
56 const struct nf_nat_ipv4_multi_range_compat
*mr
= par
->targinfo
;
57 struct nf_nat_range range
;
58 enum ip_conntrack_info ctinfo
;
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
)));
66 xt_nat_convert_range(&range
, &mr
->range
[0]);
67 return nf_nat_setup_info(ct
, &range
, NF_NAT_MANIP_SRC
);
71 xt_dnat_target_v0(struct sk_buff
*skb
, const struct xt_action_param
*par
)
73 const struct nf_nat_ipv4_multi_range_compat
*mr
= par
->targinfo
;
74 struct nf_nat_range range
;
75 enum ip_conntrack_info ctinfo
;
78 ct
= nf_ct_get(skb
, &ctinfo
);
79 WARN_ON(!(ct
!= NULL
&&
80 (ctinfo
== IP_CT_NEW
|| ctinfo
== IP_CT_RELATED
)));
82 xt_nat_convert_range(&range
, &mr
->range
[0]);
83 return nf_nat_setup_info(ct
, &range
, NF_NAT_MANIP_DST
);
87 xt_snat_target_v1(struct sk_buff
*skb
, const struct xt_action_param
*par
)
89 const struct nf_nat_range
*range
= par
->targinfo
;
90 enum ip_conntrack_info ctinfo
;
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
)));
98 return nf_nat_setup_info(ct
, range
, NF_NAT_MANIP_SRC
);
102 xt_dnat_target_v1(struct sk_buff
*skb
, const struct xt_action_param
*par
)
104 const struct nf_nat_range
*range
= par
->targinfo
;
105 enum ip_conntrack_info ctinfo
;
108 ct
= nf_ct_get(skb
, &ctinfo
);
109 WARN_ON(!(ct
!= NULL
&&
110 (ctinfo
== IP_CT_NEW
|| ctinfo
== IP_CT_RELATED
)));
112 return nf_nat_setup_info(ct
, range
, NF_NAT_MANIP_DST
);
115 static struct xt_target xt_nat_target_reg
[] __read_mostly
= {
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
,
125 .hooks
= (1 << NF_INET_POST_ROUTING
) |
126 (1 << NF_INET_LOCAL_IN
),
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
,
138 .hooks
= (1 << NF_INET_PRE_ROUTING
) |
139 (1 << NF_INET_LOCAL_OUT
),
145 .checkentry
= xt_nat_checkentry
,
146 .destroy
= xt_nat_destroy
,
147 .target
= xt_snat_target_v1
,
148 .targetsize
= sizeof(struct nf_nat_range
),
150 .hooks
= (1 << NF_INET_POST_ROUTING
) |
151 (1 << NF_INET_LOCAL_IN
),
157 .checkentry
= xt_nat_checkentry
,
158 .destroy
= xt_nat_destroy
,
159 .target
= xt_dnat_target_v1
,
160 .targetsize
= sizeof(struct nf_nat_range
),
162 .hooks
= (1 << NF_INET_PRE_ROUTING
) |
163 (1 << NF_INET_LOCAL_OUT
),
168 static int __init
xt_nat_init(void)
170 return xt_register_targets(xt_nat_target_reg
,
171 ARRAY_SIZE(xt_nat_target_reg
));
174 static void __exit
xt_nat_exit(void)
176 xt_unregister_targets(xt_nat_target_reg
, ARRAY_SIZE(xt_nat_target_reg
));
179 module_init(xt_nat_init
);
180 module_exit(xt_nat_exit
);
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");