2 * (C) 1999-2001 Paul `Rusty' Russell
3 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
4 * Copyright (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.
10 * Based on Rusty Russell's IPv4 REDIRECT target. Development of IPv6
11 * NAT funded by Astaro.
15 #include <linux/inetdevice.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/netdevice.h>
20 #include <linux/netfilter.h>
21 #include <linux/types.h>
22 #include <linux/netfilter_ipv4.h>
23 #include <linux/netfilter_ipv6.h>
24 #include <linux/netfilter/x_tables.h>
25 #include <net/addrconf.h>
26 #include <net/checksum.h>
27 #include <net/protocol.h>
28 #include <net/netfilter/nf_nat.h>
29 #include <net/netfilter/nf_nat_redirect.h>
32 nf_nat_redirect_ipv4(struct sk_buff
*skb
,
33 const struct nf_nat_ipv4_multi_range_compat
*mr
,
37 enum ip_conntrack_info ctinfo
;
39 struct nf_nat_range newrange
;
41 NF_CT_ASSERT(hooknum
== NF_INET_PRE_ROUTING
||
42 hooknum
== NF_INET_LOCAL_OUT
);
44 ct
= nf_ct_get(skb
, &ctinfo
);
45 NF_CT_ASSERT(ct
&& (ctinfo
== IP_CT_NEW
|| ctinfo
== IP_CT_RELATED
));
47 /* Local packets: make them go to loopback */
48 if (hooknum
== NF_INET_LOCAL_OUT
) {
49 newdst
= htonl(0x7F000001);
51 struct in_device
*indev
;
52 struct in_ifaddr
*ifa
;
57 indev
= __in_dev_get_rcu(skb
->dev
);
58 if (indev
&& indev
->ifa_list
) {
59 ifa
= indev
->ifa_list
;
60 newdst
= ifa
->ifa_local
;
68 /* Transfer from original range. */
69 memset(&newrange
.min_addr
, 0, sizeof(newrange
.min_addr
));
70 memset(&newrange
.max_addr
, 0, sizeof(newrange
.max_addr
));
71 newrange
.flags
= mr
->range
[0].flags
| NF_NAT_RANGE_MAP_IPS
;
72 newrange
.min_addr
.ip
= newdst
;
73 newrange
.max_addr
.ip
= newdst
;
74 newrange
.min_proto
= mr
->range
[0].min
;
75 newrange
.max_proto
= mr
->range
[0].max
;
77 /* Hand modified range to generic setup. */
78 return nf_nat_setup_info(ct
, &newrange
, NF_NAT_MANIP_DST
);
80 EXPORT_SYMBOL_GPL(nf_nat_redirect_ipv4
);
82 static const struct in6_addr loopback_addr
= IN6ADDR_LOOPBACK_INIT
;
85 nf_nat_redirect_ipv6(struct sk_buff
*skb
, const struct nf_nat_range
*range
,
88 struct nf_nat_range newrange
;
89 struct in6_addr newdst
;
90 enum ip_conntrack_info ctinfo
;
93 ct
= nf_ct_get(skb
, &ctinfo
);
94 if (hooknum
== NF_INET_LOCAL_OUT
) {
95 newdst
= loopback_addr
;
97 struct inet6_dev
*idev
;
98 struct inet6_ifaddr
*ifa
;
102 idev
= __in6_dev_get(skb
->dev
);
104 list_for_each_entry(ifa
, &idev
->addr_list
, if_list
) {
116 newrange
.flags
= range
->flags
| NF_NAT_RANGE_MAP_IPS
;
117 newrange
.min_addr
.in6
= newdst
;
118 newrange
.max_addr
.in6
= newdst
;
119 newrange
.min_proto
= range
->min_proto
;
120 newrange
.max_proto
= range
->max_proto
;
122 return nf_nat_setup_info(ct
, &newrange
, NF_NAT_MANIP_DST
);
124 EXPORT_SYMBOL_GPL(nf_nat_redirect_ipv6
);
126 MODULE_LICENSE("GPL");
127 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");