1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Masquerade. Simple mapping which alters range to a local IP address
3 (depending on route). */
5 /* (C) 1999-2001 Paul `Rusty' Russell
6 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <linux/module.h>
10 #include <linux/netfilter/x_tables.h>
11 #include <net/netfilter/nf_nat.h>
12 #include <net/netfilter/nf_nat_masquerade.h>
14 MODULE_LICENSE("GPL");
15 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
16 MODULE_DESCRIPTION("Xtables: automatic-address SNAT");
18 /* FIXME: Multiple targets. --RR */
19 static int masquerade_tg_check(const struct xt_tgchk_param
*par
)
21 const struct nf_nat_ipv4_multi_range_compat
*mr
= par
->targinfo
;
23 if (mr
->range
[0].flags
& NF_NAT_RANGE_MAP_IPS
) {
24 pr_debug("bad MAP_IPS.\n");
27 if (mr
->rangesize
!= 1) {
28 pr_debug("bad rangesize %u\n", mr
->rangesize
);
31 return nf_ct_netns_get(par
->net
, par
->family
);
35 masquerade_tg(struct sk_buff
*skb
, const struct xt_action_param
*par
)
37 struct nf_nat_range2 range
;
38 const struct nf_nat_ipv4_multi_range_compat
*mr
;
41 range
.flags
= mr
->range
[0].flags
;
42 range
.min_proto
= mr
->range
[0].min
;
43 range
.max_proto
= mr
->range
[0].max
;
45 return nf_nat_masquerade_ipv4(skb
, xt_hooknum(par
), &range
,
49 static void masquerade_tg_destroy(const struct xt_tgdtor_param
*par
)
51 nf_ct_netns_put(par
->net
, par
->family
);
54 #if IS_ENABLED(CONFIG_IPV6)
56 masquerade_tg6(struct sk_buff
*skb
, const struct xt_action_param
*par
)
58 return nf_nat_masquerade_ipv6(skb
, par
->targinfo
, xt_out(par
));
61 static int masquerade_tg6_checkentry(const struct xt_tgchk_param
*par
)
63 const struct nf_nat_range2
*range
= par
->targinfo
;
65 if (range
->flags
& NF_NAT_RANGE_MAP_IPS
)
68 return nf_ct_netns_get(par
->net
, par
->family
);
72 static struct xt_target masquerade_tg_reg
[] __read_mostly
= {
74 #if IS_ENABLED(CONFIG_IPV6)
76 .family
= NFPROTO_IPV6
,
77 .target
= masquerade_tg6
,
78 .targetsize
= sizeof(struct nf_nat_range
),
80 .hooks
= 1 << NF_INET_POST_ROUTING
,
81 .checkentry
= masquerade_tg6_checkentry
,
82 .destroy
= masquerade_tg_destroy
,
87 .family
= NFPROTO_IPV4
,
88 .target
= masquerade_tg
,
89 .targetsize
= sizeof(struct nf_nat_ipv4_multi_range_compat
),
91 .hooks
= 1 << NF_INET_POST_ROUTING
,
92 .checkentry
= masquerade_tg_check
,
93 .destroy
= masquerade_tg_destroy
,
98 static int __init
masquerade_tg_init(void)
102 ret
= xt_register_targets(masquerade_tg_reg
,
103 ARRAY_SIZE(masquerade_tg_reg
));
107 ret
= nf_nat_masquerade_inet_register_notifiers();
109 xt_unregister_targets(masquerade_tg_reg
,
110 ARRAY_SIZE(masquerade_tg_reg
));
117 static void __exit
masquerade_tg_exit(void)
119 xt_unregister_targets(masquerade_tg_reg
, ARRAY_SIZE(masquerade_tg_reg
));
120 nf_nat_masquerade_inet_unregister_notifiers();
123 module_init(masquerade_tg_init
);
124 module_exit(masquerade_tg_exit
);
125 #if IS_ENABLED(CONFIG_IPV6)
126 MODULE_ALIAS("ip6t_MASQUERADE");
128 MODULE_ALIAS("ipt_MASQUERADE");