OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [net/] [ipv4/] [netfilter/] [ipt_MASQUERADE.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/* Masquerade.  Simple mapping which alters range to a local IP address
2
   (depending on route). */
3
#include <linux/config.h>
4
#include <linux/types.h>
5
#include <linux/ip.h>
6
#include <linux/timer.h>
7
#include <linux/module.h>
8
#include <linux/netfilter.h>
9
#include <net/protocol.h>
10
#include <net/checksum.h>
11
#include <linux/netfilter_ipv4.h>
12
#include <linux/netfilter_ipv4/ip_nat_rule.h>
13
#include <linux/netfilter_ipv4/ip_tables.h>
14
 
15
#if 0
16
#define DEBUGP printk
17
#else
18
#define DEBUGP(format, args...)
19
#endif
20
 
21
/* Lock protects masq region inside conntrack */
22
static DECLARE_RWLOCK(masq_lock);
23
 
24
/* FIXME: Multiple targets. --RR */
25
static int
26
masquerade_check(const char *tablename,
27
                 const struct ipt_entry *e,
28
                 void *targinfo,
29
                 unsigned int targinfosize,
30
                 unsigned int hook_mask)
31
{
32
        const struct ip_nat_multi_range *mr = targinfo;
33
 
34
        if (strcmp(tablename, "nat") != 0) {
35
                DEBUGP("masquerade_check: bad table `%s'.\n", tablename);
36
                return 0;
37
        }
38
        if (targinfosize != IPT_ALIGN(sizeof(*mr))) {
39
                DEBUGP("masquerade_check: size %u != %u.\n",
40
                       targinfosize, sizeof(*mr));
41
                return 0;
42
        }
43
        if (hook_mask & ~(1 << NF_IP_POST_ROUTING)) {
44
                DEBUGP("masquerade_check: bad hooks %x.\n", hook_mask);
45
                return 0;
46
        }
47
        if (mr->range[0].flags & IP_NAT_RANGE_MAP_IPS) {
48
                DEBUGP("masquerade_check: bad MAP_IPS.\n");
49
                return 0;
50
        }
51
        if (mr->rangesize != 1) {
52
                DEBUGP("masquerade_check: bad rangesize %u.\n", mr->rangesize);
53
                return 0;
54
        }
55
        return 1;
56
}
57
 
58
static unsigned int
59
masquerade_target(struct sk_buff **pskb,
60
                  unsigned int hooknum,
61
                  const struct net_device *in,
62
                  const struct net_device *out,
63
                  const void *targinfo,
64
                  void *userinfo)
65
{
66
        struct ip_conntrack *ct;
67
        enum ip_conntrack_info ctinfo;
68
        const struct ip_nat_multi_range *mr;
69
        struct ip_nat_multi_range newrange;
70
        u_int32_t newsrc;
71
        struct rtable *rt;
72
        struct rt_key key;
73
 
74
        IP_NF_ASSERT(hooknum == NF_IP_POST_ROUTING);
75
 
76
        /* FIXME: For the moment, don't do local packets, breaks
77
           testsuite for 2.3.49 --RR */
78
        if ((*pskb)->sk)
79
                return NF_ACCEPT;
80
 
81
        ct = ip_conntrack_get(*pskb, &ctinfo);
82
        IP_NF_ASSERT(ct && (ctinfo == IP_CT_NEW
83
                                  || ctinfo == IP_CT_RELATED));
84
 
85
        mr = targinfo;
86
 
87
        key.dst = (*pskb)->nh.iph->daddr;
88
        key.src = 0; /* Unknown: that's what we're trying to establish */
89
        key.tos = RT_TOS((*pskb)->nh.iph->tos)|RTO_CONN;
90
        key.oif = 0;
91
#ifdef CONFIG_IP_ROUTE_FWMARK
92
        key.fwmark = (*pskb)->nfmark;
93
#endif
94
        if (ip_route_output_key(&rt, &key) != 0) {
95
                /* Funky routing can do this. */
96
                if (net_ratelimit())
97
                        printk("MASQUERADE:"
98
                               " No route: Rusty's brain broke!\n");
99
                return NF_DROP;
100
        }
101
        if (rt->u.dst.dev != out) {
102
                if (net_ratelimit())
103
                        printk("MASQUERADE:"
104
                               " Route sent us somewhere else.\n");
105
                return NF_DROP;
106
        }
107
 
108
        newsrc = rt->rt_src;
109
        DEBUGP("newsrc = %u.%u.%u.%u\n", NIPQUAD(newsrc));
110
        ip_rt_put(rt);
111
 
112
        WRITE_LOCK(&masq_lock);
113
        ct->nat.masq_index = out->ifindex;
114
        WRITE_UNLOCK(&masq_lock);
115
 
116
        /* Transfer from original range. */
117
        newrange = ((struct ip_nat_multi_range)
118
                { 1, { { mr->range[0].flags | IP_NAT_RANGE_MAP_IPS,
119
                         newsrc, newsrc,
120
                         mr->range[0].min, mr->range[0].max } } });
121
 
122
        /* Hand modified range to generic setup. */
123
        return ip_nat_setup_info(ct, &newrange, hooknum);
124
}
125
 
126
static inline int
127
device_cmp(const struct ip_conntrack *i, void *_ina)
128
{
129
        int ret = 0;
130
        struct in_ifaddr *ina = _ina;
131
 
132
        READ_LOCK(&masq_lock);
133
        /* If it's masquerading out this interface with a different address,
134
         * or we don't know the new address of this interface. */
135
        if (i->nat.masq_index == ina->ifa_dev->dev->ifindex
136
            && i->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip != ina->ifa_address)
137
                ret = 1;
138
        READ_UNLOCK(&masq_lock);
139
 
140
        return ret;
141
}
142
 
143
static int masq_inet_event(struct notifier_block *this,
144
                           unsigned long event,
145
                           void *ptr)
146
{
147
        /* For some configurations, interfaces often come back with
148
         * the same address.  If not, clean up old conntrack
149
         * entries. */
150
        if (event == NETDEV_UP)
151
                ip_ct_selective_cleanup(device_cmp, ptr);
152
 
153
        return NOTIFY_DONE;
154
}
155
 
156
static struct notifier_block masq_inet_notifier = {
157
        .notifier_call = masq_inet_event
158
};
159
 
160
static struct ipt_target masquerade
161
= { { NULL, NULL }, "MASQUERADE", masquerade_target, masquerade_check, NULL,
162
    THIS_MODULE };
163
 
164
static int __init init(void)
165
{
166
        int ret;
167
 
168
        ret = ipt_register_target(&masquerade);
169
 
170
        if (ret == 0)
171
                /* Register IP address change reports */
172
                register_inetaddr_notifier(&masq_inet_notifier);
173
 
174
        return ret;
175
}
176
 
177
static void __exit fini(void)
178
{
179
        ipt_unregister_target(&masquerade);
180
        unregister_inetaddr_notifier(&masq_inet_notifier);
181
}
182
 
183
module_init(init);
184
module_exit(fini);
185
MODULE_LICENSE("GPL");

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.