1 /* Redirect. Simple mapping which alters dst to a local IP address. */
2 /* (C) 1999-2001 Paul `Rusty' Russell
3 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 #include <linux/types.h>
12 #include <linux/timer.h>
13 #include <linux/module.h>
14 #include <linux/netfilter.h>
15 #include <linux/netdevice.h>
17 #include <linux/inetdevice.h>
18 #include <net/protocol.h>
19 #include <net/checksum.h>
20 #include <linux/netfilter_ipv4.h>
21 #include <linux/netfilter/x_tables.h>
22 #include <net/netfilter/nf_nat_rule.h>
24 MODULE_LICENSE("GPL");
25 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
26 MODULE_DESCRIPTION("Xtables: Connection redirection to localhost");
28 /* FIXME: Take multiple ranges --RR */
29 static bool redirect_tg_check(const struct xt_tgchk_param
*par
)
31 const struct nf_nat_multi_range_compat
*mr
= par
->targinfo
;
33 if (mr
->range
[0].flags
& IP_NAT_RANGE_MAP_IPS
) {
34 pr_debug("redirect_check: bad MAP_IPS.\n");
37 if (mr
->rangesize
!= 1) {
38 pr_debug("redirect_check: bad rangesize %u.\n", mr
->rangesize
);
45 redirect_tg(struct sk_buff
*skb
, const struct xt_target_param
*par
)
48 enum ip_conntrack_info ctinfo
;
50 const struct nf_nat_multi_range_compat
*mr
= par
->targinfo
;
51 struct nf_nat_range newrange
;
53 NF_CT_ASSERT(par
->hooknum
== NF_INET_PRE_ROUTING
||
54 par
->hooknum
== NF_INET_LOCAL_OUT
);
56 ct
= nf_ct_get(skb
, &ctinfo
);
57 NF_CT_ASSERT(ct
&& (ctinfo
== IP_CT_NEW
|| ctinfo
== IP_CT_RELATED
));
59 /* Local packets: make them go to loopback */
60 if (par
->hooknum
== NF_INET_LOCAL_OUT
)
61 newdst
= htonl(0x7F000001);
63 struct in_device
*indev
;
64 struct in_ifaddr
*ifa
;
69 indev
= __in_dev_get_rcu(skb
->dev
);
70 if (indev
&& (ifa
= indev
->ifa_list
))
71 newdst
= ifa
->ifa_local
;
78 /* Transfer from original range. */
79 newrange
= ((struct nf_nat_range
)
80 { mr
->range
[0].flags
| IP_NAT_RANGE_MAP_IPS
,
82 mr
->range
[0].min
, mr
->range
[0].max
});
84 /* Hand modified range to generic setup. */
85 return nf_nat_setup_info(ct
, &newrange
, IP_NAT_MANIP_DST
);
88 static struct xt_target redirect_tg_reg __read_mostly
= {
90 .family
= NFPROTO_IPV4
,
91 .target
= redirect_tg
,
92 .targetsize
= sizeof(struct nf_nat_multi_range_compat
),
94 .hooks
= (1 << NF_INET_PRE_ROUTING
) | (1 << NF_INET_LOCAL_OUT
),
95 .checkentry
= redirect_tg_check
,
99 static int __init
redirect_tg_init(void)
101 return xt_register_target(&redirect_tg_reg
);
104 static void __exit
redirect_tg_exit(void)
106 xt_unregister_target(&redirect_tg_reg
);
109 module_init(redirect_tg_init
);
110 module_exit(redirect_tg_exit
);