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 #include <linux/module.h>
12 #include <linux/skbuff.h>
13 #include <linux/netfilter.h>
14 #include <linux/netfilter/x_tables.h>
15 #include <net/netfilter/nf_nat_core.h>
17 static int xt_nat_checkentry_v0(const struct xt_tgchk_param
*par
)
19 const struct nf_nat_ipv4_multi_range_compat
*mr
= par
->targinfo
;
21 if (mr
->rangesize
!= 1) {
22 pr_info("%s: multiple ranges no longer supported\n",
26 return nf_ct_netns_get(par
->net
, par
->family
);
29 static int xt_nat_checkentry(const struct xt_tgchk_param
*par
)
31 return nf_ct_netns_get(par
->net
, par
->family
);
34 static void xt_nat_destroy(const struct xt_tgdtor_param
*par
)
36 nf_ct_netns_put(par
->net
, par
->family
);
39 static void xt_nat_convert_range(struct nf_nat_range
*dst
,
40 const struct nf_nat_ipv4_range
*src
)
42 memset(&dst
->min_addr
, 0, sizeof(dst
->min_addr
));
43 memset(&dst
->max_addr
, 0, sizeof(dst
->max_addr
));
45 dst
->flags
= src
->flags
;
46 dst
->min_addr
.ip
= src
->min_ip
;
47 dst
->max_addr
.ip
= src
->max_ip
;
48 dst
->min_proto
= src
->min
;
49 dst
->max_proto
= src
->max
;
53 xt_snat_target_v0(struct sk_buff
*skb
, const struct xt_action_param
*par
)
55 const struct nf_nat_ipv4_multi_range_compat
*mr
= par
->targinfo
;
56 struct nf_nat_range range
;
57 enum ip_conntrack_info ctinfo
;
60 ct
= nf_ct_get(skb
, &ctinfo
);
61 WARN_ON(!(ct
!= NULL
&&
62 (ctinfo
== IP_CT_NEW
|| ctinfo
== IP_CT_RELATED
||
63 ctinfo
== IP_CT_RELATED_REPLY
)));
65 xt_nat_convert_range(&range
, &mr
->range
[0]);
66 return nf_nat_setup_info(ct
, &range
, NF_NAT_MANIP_SRC
);
70 xt_dnat_target_v0(struct sk_buff
*skb
, const struct xt_action_param
*par
)
72 const struct nf_nat_ipv4_multi_range_compat
*mr
= par
->targinfo
;
73 struct nf_nat_range range
;
74 enum ip_conntrack_info ctinfo
;
77 ct
= nf_ct_get(skb
, &ctinfo
);
78 WARN_ON(!(ct
!= NULL
&&
79 (ctinfo
== IP_CT_NEW
|| ctinfo
== IP_CT_RELATED
)));
81 xt_nat_convert_range(&range
, &mr
->range
[0]);
82 return nf_nat_setup_info(ct
, &range
, NF_NAT_MANIP_DST
);
86 xt_snat_target_v1(struct sk_buff
*skb
, const struct xt_action_param
*par
)
88 const struct nf_nat_range
*range
= par
->targinfo
;
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 return nf_nat_setup_info(ct
, range
, NF_NAT_MANIP_SRC
);
101 xt_dnat_target_v1(struct sk_buff
*skb
, const struct xt_action_param
*par
)
103 const struct nf_nat_range
*range
= par
->targinfo
;
104 enum ip_conntrack_info ctinfo
;
107 ct
= nf_ct_get(skb
, &ctinfo
);
108 WARN_ON(!(ct
!= NULL
&&
109 (ctinfo
== IP_CT_NEW
|| ctinfo
== IP_CT_RELATED
)));
111 return nf_nat_setup_info(ct
, range
, NF_NAT_MANIP_DST
);
114 static struct xt_target xt_nat_target_reg
[] __read_mostly
= {
118 .checkentry
= xt_nat_checkentry_v0
,
119 .destroy
= xt_nat_destroy
,
120 .target
= xt_snat_target_v0
,
121 .targetsize
= sizeof(struct nf_nat_ipv4_multi_range_compat
),
122 .family
= NFPROTO_IPV4
,
124 .hooks
= (1 << NF_INET_POST_ROUTING
) |
125 (1 << NF_INET_LOCAL_IN
),
131 .checkentry
= xt_nat_checkentry_v0
,
132 .destroy
= xt_nat_destroy
,
133 .target
= xt_dnat_target_v0
,
134 .targetsize
= sizeof(struct nf_nat_ipv4_multi_range_compat
),
135 .family
= NFPROTO_IPV4
,
137 .hooks
= (1 << NF_INET_PRE_ROUTING
) |
138 (1 << NF_INET_LOCAL_OUT
),
144 .checkentry
= xt_nat_checkentry
,
145 .destroy
= xt_nat_destroy
,
146 .target
= xt_snat_target_v1
,
147 .targetsize
= sizeof(struct nf_nat_range
),
149 .hooks
= (1 << NF_INET_POST_ROUTING
) |
150 (1 << NF_INET_LOCAL_IN
),
156 .checkentry
= xt_nat_checkentry
,
157 .destroy
= xt_nat_destroy
,
158 .target
= xt_dnat_target_v1
,
159 .targetsize
= sizeof(struct nf_nat_range
),
161 .hooks
= (1 << NF_INET_PRE_ROUTING
) |
162 (1 << NF_INET_LOCAL_OUT
),
167 static int __init
xt_nat_init(void)
169 return xt_register_targets(xt_nat_target_reg
,
170 ARRAY_SIZE(xt_nat_target_reg
));
173 static void __exit
xt_nat_exit(void)
175 xt_unregister_targets(xt_nat_target_reg
, ARRAY_SIZE(xt_nat_target_reg
));
178 module_init(xt_nat_init
);
179 module_exit(xt_nat_exit
);
181 MODULE_LICENSE("GPL");
182 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
183 MODULE_ALIAS("ipt_SNAT");
184 MODULE_ALIAS("ipt_DNAT");
185 MODULE_ALIAS("ip6t_SNAT");
186 MODULE_ALIAS("ip6t_DNAT");