xref: /openbmc/linux/net/netfilter/xt_MASQUERADE.c (revision adf82acc)
1 /* Masquerade.  Simple mapping which alters range to a local IP address
2    (depending on route). */
3 
4 /* (C) 1999-2001 Paul `Rusty' Russell
5  * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/module.h>
13 #include <linux/netfilter/x_tables.h>
14 #include <net/netfilter/nf_nat.h>
15 #include <net/netfilter/nf_nat_masquerade.h>
16 
17 MODULE_LICENSE("GPL");
18 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
19 MODULE_DESCRIPTION("Xtables: automatic-address SNAT");
20 
21 /* FIXME: Multiple targets. --RR */
22 static int masquerade_tg_check(const struct xt_tgchk_param *par)
23 {
24 	const struct nf_nat_ipv4_multi_range_compat *mr = par->targinfo;
25 
26 	if (mr->range[0].flags & NF_NAT_RANGE_MAP_IPS) {
27 		pr_debug("bad MAP_IPS.\n");
28 		return -EINVAL;
29 	}
30 	if (mr->rangesize != 1) {
31 		pr_debug("bad rangesize %u\n", mr->rangesize);
32 		return -EINVAL;
33 	}
34 	return nf_ct_netns_get(par->net, par->family);
35 }
36 
37 static unsigned int
38 masquerade_tg(struct sk_buff *skb, const struct xt_action_param *par)
39 {
40 	struct nf_nat_range2 range;
41 	const struct nf_nat_ipv4_multi_range_compat *mr;
42 
43 	mr = par->targinfo;
44 	range.flags = mr->range[0].flags;
45 	range.min_proto = mr->range[0].min;
46 	range.max_proto = mr->range[0].max;
47 
48 	return nf_nat_masquerade_ipv4(skb, xt_hooknum(par), &range,
49 				      xt_out(par));
50 }
51 
52 static void masquerade_tg_destroy(const struct xt_tgdtor_param *par)
53 {
54 	nf_ct_netns_put(par->net, par->family);
55 }
56 
57 #if IS_ENABLED(CONFIG_IPV6)
58 static unsigned int
59 masquerade_tg6(struct sk_buff *skb, const struct xt_action_param *par)
60 {
61 	return nf_nat_masquerade_ipv6(skb, par->targinfo, xt_out(par));
62 }
63 
64 static int masquerade_tg6_checkentry(const struct xt_tgchk_param *par)
65 {
66 	const struct nf_nat_range2 *range = par->targinfo;
67 
68 	if (range->flags & NF_NAT_RANGE_MAP_IPS)
69 		return -EINVAL;
70 
71 	return nf_ct_netns_get(par->net, par->family);
72 }
73 #endif
74 
75 static struct xt_target masquerade_tg_reg[] __read_mostly = {
76 	{
77 #if IS_ENABLED(CONFIG_IPV6)
78 		.name		= "MASQUERADE",
79 		.family		= NFPROTO_IPV6,
80 		.target		= masquerade_tg6,
81 		.targetsize	= sizeof(struct nf_nat_range),
82 		.table		= "nat",
83 		.hooks		= 1 << NF_INET_POST_ROUTING,
84 		.checkentry	= masquerade_tg6_checkentry,
85 		.destroy	= masquerade_tg_destroy,
86 		.me		= THIS_MODULE,
87 	}, {
88 #endif
89 		.name		= "MASQUERADE",
90 		.family		= NFPROTO_IPV4,
91 		.target		= masquerade_tg,
92 		.targetsize	= sizeof(struct nf_nat_ipv4_multi_range_compat),
93 		.table		= "nat",
94 		.hooks		= 1 << NF_INET_POST_ROUTING,
95 		.checkentry	= masquerade_tg_check,
96 		.destroy	= masquerade_tg_destroy,
97 		.me		= THIS_MODULE,
98 	}
99 };
100 
101 static int __init masquerade_tg_init(void)
102 {
103 	int ret;
104 
105 	ret = xt_register_targets(masquerade_tg_reg,
106 				  ARRAY_SIZE(masquerade_tg_reg));
107 	if (ret)
108 		return ret;
109 
110 	ret = nf_nat_masquerade_ipv4_register_notifier();
111 	if (ret) {
112 		xt_unregister_targets(masquerade_tg_reg,
113 				      ARRAY_SIZE(masquerade_tg_reg));
114 		return ret;
115 	}
116 
117 #if IS_ENABLED(CONFIG_IPV6)
118 	ret = nf_nat_masquerade_ipv6_register_notifier();
119 	if (ret) {
120 		xt_unregister_targets(masquerade_tg_reg,
121 				      ARRAY_SIZE(masquerade_tg_reg));
122 		nf_nat_masquerade_ipv4_unregister_notifier();
123 		return ret;
124 	}
125 #endif
126 	return ret;
127 }
128 
129 static void __exit masquerade_tg_exit(void)
130 {
131 	xt_unregister_targets(masquerade_tg_reg, ARRAY_SIZE(masquerade_tg_reg));
132 	nf_nat_masquerade_ipv4_unregister_notifier();
133 #if IS_ENABLED(CONFIG_IPV6)
134 	nf_nat_masquerade_ipv6_unregister_notifier();
135 #endif
136 }
137 
138 module_init(masquerade_tg_init);
139 module_exit(masquerade_tg_exit);
140 #if IS_ENABLED(CONFIG_IPV6)
141 MODULE_ALIAS("ip6t_MASQUERADE");
142 #endif
143 MODULE_ALIAS("ipt_MASQUERADE");
144