~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/netfilter/nf_nat_redirect.c

Version: ~ [ linux-5.4-rc3 ] ~ [ linux-5.3.6 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.79 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.149 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.196 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.196 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.75 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  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>
  5  *
  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.
  9  *
 10  * Based on Rusty Russell's IPv4 REDIRECT target. Development of IPv6
 11  * NAT funded by Astaro.
 12  */
 13 
 14 #include <linux/if.h>
 15 #include <linux/inetdevice.h>
 16 #include <linux/ip.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>
 30 
 31 unsigned int
 32 nf_nat_redirect_ipv4(struct sk_buff *skb,
 33                      const struct nf_nat_ipv4_multi_range_compat *mr,
 34                      unsigned int hooknum)
 35 {
 36         struct nf_conn *ct;
 37         enum ip_conntrack_info ctinfo;
 38         __be32 newdst;
 39         struct nf_nat_range newrange;
 40 
 41         NF_CT_ASSERT(hooknum == NF_INET_PRE_ROUTING ||
 42                      hooknum == NF_INET_LOCAL_OUT);
 43 
 44         ct = nf_ct_get(skb, &ctinfo);
 45         NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED));
 46 
 47         /* Local packets: make them go to loopback */
 48         if (hooknum == NF_INET_LOCAL_OUT) {
 49                 newdst = htonl(0x7F000001);
 50         } else {
 51                 struct in_device *indev;
 52                 struct in_ifaddr *ifa;
 53 
 54                 newdst = 0;
 55 
 56                 rcu_read_lock();
 57                 indev = __in_dev_get_rcu(skb->dev);
 58                 if (indev && indev->ifa_list) {
 59                         ifa = indev->ifa_list;
 60                         newdst = ifa->ifa_local;
 61                 }
 62                 rcu_read_unlock();
 63 
 64                 if (!newdst)
 65                         return NF_DROP;
 66         }
 67 
 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;
 76 
 77         /* Hand modified range to generic setup. */
 78         return nf_nat_setup_info(ct, &newrange, NF_NAT_MANIP_DST);
 79 }
 80 EXPORT_SYMBOL_GPL(nf_nat_redirect_ipv4);
 81 
 82 static const struct in6_addr loopback_addr = IN6ADDR_LOOPBACK_INIT;
 83 
 84 unsigned int
 85 nf_nat_redirect_ipv6(struct sk_buff *skb, const struct nf_nat_range *range,
 86                      unsigned int hooknum)
 87 {
 88         struct nf_nat_range newrange;
 89         struct in6_addr newdst;
 90         enum ip_conntrack_info ctinfo;
 91         struct nf_conn *ct;
 92 
 93         ct = nf_ct_get(skb, &ctinfo);
 94         if (hooknum == NF_INET_LOCAL_OUT) {
 95                 newdst = loopback_addr;
 96         } else {
 97                 struct inet6_dev *idev;
 98                 struct inet6_ifaddr *ifa;
 99                 bool addr = false;
100 
101                 rcu_read_lock();
102                 idev = __in6_dev_get(skb->dev);
103                 if (idev != NULL) {
104                         read_lock_bh(&idev->lock);
105                         list_for_each_entry(ifa, &idev->addr_list, if_list) {
106                                 newdst = ifa->addr;
107                                 addr = true;
108                                 break;
109                         }
110                         read_unlock_bh(&idev->lock);
111                 }
112                 rcu_read_unlock();
113 
114                 if (!addr)
115                         return NF_DROP;
116         }
117 
118         newrange.flags          = range->flags | NF_NAT_RANGE_MAP_IPS;
119         newrange.min_addr.in6   = newdst;
120         newrange.max_addr.in6   = newdst;
121         newrange.min_proto      = range->min_proto;
122         newrange.max_proto      = range->max_proto;
123 
124         return nf_nat_setup_info(ct, &newrange, NF_NAT_MANIP_DST);
125 }
126 EXPORT_SYMBOL_GPL(nf_nat_redirect_ipv6);
127 
128 MODULE_LICENSE("GPL");
129 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
130 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp