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

Subversion Repositories or1k

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/* This file contains all the functions required for the standalone
2
   ip_nat module.
3
 
4
   These are not required by the compatibility layer.
5
*/
6
 
7
/* (c) 1999 Paul `Rusty' Russell.  Licenced under the GNU General
8
 * Public Licence.
9
 *
10
 * 23 Apr 2001: Harald Welte <laforge@gnumonks.org>
11
 *      - new API and handling of conntrack/nat helpers
12
 *      - now capable of multiple expectations for one master
13
 * */
14
 
15
#include <linux/config.h>
16
#include <linux/types.h>
17
#include <linux/ip.h>
18
#include <linux/netfilter.h>
19
#include <linux/netfilter_ipv4.h>
20
#include <linux/module.h>
21
#include <linux/skbuff.h>
22
#include <linux/proc_fs.h>
23
#include <net/checksum.h>
24
#include <linux/spinlock.h>
25
#include <linux/version.h>
26
#include <linux/brlock.h>
27
 
28
#define ASSERT_READ_LOCK(x) MUST_BE_READ_LOCKED(&ip_nat_lock)
29
#define ASSERT_WRITE_LOCK(x) MUST_BE_WRITE_LOCKED(&ip_nat_lock)
30
 
31
#include <linux/netfilter_ipv4/ip_nat.h>
32
#include <linux/netfilter_ipv4/ip_nat_rule.h>
33
#include <linux/netfilter_ipv4/ip_nat_protocol.h>
34
#include <linux/netfilter_ipv4/ip_nat_core.h>
35
#include <linux/netfilter_ipv4/ip_nat_helper.h>
36
#include <linux/netfilter_ipv4/ip_tables.h>
37
#include <linux/netfilter_ipv4/ip_conntrack_core.h>
38
#include <linux/netfilter_ipv4/listhelp.h>
39
 
40
#if 0
41
#define DEBUGP printk
42
#else
43
#define DEBUGP(format, args...)
44
#endif
45
 
46
#define HOOKNAME(hooknum) ((hooknum) == NF_IP_POST_ROUTING ? "POST_ROUTING"  \
47
                           : ((hooknum) == NF_IP_PRE_ROUTING ? "PRE_ROUTING" \
48
                              : ((hooknum) == NF_IP_LOCAL_OUT ? "LOCAL_OUT"  \
49
                                 : ((hooknum) == NF_IP_LOCAL_IN ? "LOCAL_IN"  \
50
                                    : "*ERROR*")))
51
 
52
static inline int call_expect(struct ip_conntrack *master,
53
                              struct sk_buff **pskb,
54
                              unsigned int hooknum,
55
                              struct ip_conntrack *ct,
56
                              struct ip_nat_info *info)
57
{
58
        return master->nat.info.helper->expect(pskb, hooknum, ct, info);
59
}
60
 
61
static unsigned int
62
ip_nat_fn(unsigned int hooknum,
63
          struct sk_buff **pskb,
64
          const struct net_device *in,
65
          const struct net_device *out,
66
          int (*okfn)(struct sk_buff *))
67
{
68
        struct ip_conntrack *ct;
69
        enum ip_conntrack_info ctinfo;
70
        struct ip_nat_info *info;
71
        /* maniptype == SRC for postrouting. */
72
        enum ip_nat_manip_type maniptype = HOOK2MANIP(hooknum);
73
 
74
        /* We never see fragments: conntrack defrags on pre-routing
75
           and local-out, and ip_nat_out protects post-routing. */
76
        IP_NF_ASSERT(!((*pskb)->nh.iph->frag_off
77
                       & htons(IP_MF|IP_OFFSET)));
78
 
79
        (*pskb)->nfcache |= NFC_UNKNOWN;
80
 
81
        /* If we had a hardware checksum before, it's now invalid */
82
        if ((*pskb)->ip_summed == CHECKSUM_HW)
83
                (*pskb)->ip_summed = CHECKSUM_NONE;
84
 
85
        ct = ip_conntrack_get(*pskb, &ctinfo);
86
        /* Can't track?  It's not due to stress, or conntrack would
87
           have dropped it.  Hence it's the user's responsibilty to
88
           packet filter it out, or implement conntrack/NAT for that
89
           protocol. 8) --RR */
90
        if (!ct) {
91
                /* Exception: ICMP redirect to new connection (not in
92
                   hash table yet).  We must not let this through, in
93
                   case we're doing NAT to the same network. */
94
                struct iphdr *iph = (*pskb)->nh.iph;
95
                struct icmphdr *hdr = (struct icmphdr *)
96
                        ((u_int32_t *)iph + iph->ihl);
97
                if (iph->protocol == IPPROTO_ICMP
98
                    && hdr->type == ICMP_REDIRECT)
99
                        return NF_DROP;
100
                return NF_ACCEPT;
101
        }
102
 
103
        switch (ctinfo) {
104
        case IP_CT_RELATED:
105
        case IP_CT_RELATED+IP_CT_IS_REPLY:
106
                if ((*pskb)->nh.iph->protocol == IPPROTO_ICMP) {
107
                        return icmp_reply_translation(*pskb, ct, hooknum,
108
                                                      CTINFO2DIR(ctinfo));
109
                }
110
                /* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */
111
        case IP_CT_NEW:
112
                info = &ct->nat.info;
113
 
114
                WRITE_LOCK(&ip_nat_lock);
115
                /* Seen it before?  This can happen for loopback, retrans,
116
                   or local packets.. */
117
                if (!(info->initialized & (1 << maniptype))
118
#ifndef CONFIG_IP_NF_NAT_LOCAL
119
                    /* If this session has already been confirmed we must not
120
                     * touch it again even if there is no mapping set up.
121
                     * Can only happen on local->local traffic with
122
                     * CONFIG_IP_NF_NAT_LOCAL disabled.
123
                     */
124
                    && !(ct->status & IPS_CONFIRMED)
125
#endif
126
                    ) {
127
                        unsigned int ret;
128
 
129
                        if (ct->master
130
                            && master_ct(ct)->nat.info.helper
131
                            && master_ct(ct)->nat.info.helper->expect) {
132
                                ret = call_expect(master_ct(ct), pskb,
133
                                                  hooknum, ct, info);
134
                        } else {
135
#ifdef CONFIG_IP_NF_NAT_LOCAL
136
                                /* LOCAL_IN hook doesn't have a chain!  */
137
                                if (hooknum == NF_IP_LOCAL_IN)
138
                                        ret = alloc_null_binding(ct, info,
139
                                                                 hooknum);
140
                                else
141
#endif
142
                                ret = ip_nat_rule_find(pskb, hooknum, in, out,
143
                                                       ct, info);
144
                        }
145
 
146
                        if (ret != NF_ACCEPT) {
147
                                WRITE_UNLOCK(&ip_nat_lock);
148
                                return ret;
149
                        }
150
                } else
151
                        DEBUGP("Already setup manip %s for ct %p\n",
152
                               maniptype == IP_NAT_MANIP_SRC ? "SRC" : "DST",
153
                               ct);
154
                WRITE_UNLOCK(&ip_nat_lock);
155
                break;
156
 
157
        default:
158
                /* ESTABLISHED */
159
                IP_NF_ASSERT(ctinfo == IP_CT_ESTABLISHED
160
                             || ctinfo == (IP_CT_ESTABLISHED+IP_CT_IS_REPLY));
161
                info = &ct->nat.info;
162
        }
163
 
164
        IP_NF_ASSERT(info);
165
        return do_bindings(ct, ctinfo, info, hooknum, pskb);
166
}
167
 
168
static unsigned int
169
ip_nat_out(unsigned int hooknum,
170
           struct sk_buff **pskb,
171
           const struct net_device *in,
172
           const struct net_device *out,
173
           int (*okfn)(struct sk_buff *))
174
{
175
        /* root is playing with raw sockets. */
176
        if ((*pskb)->len < sizeof(struct iphdr)
177
            || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr))
178
                return NF_ACCEPT;
179
 
180
        /* We can hit fragment here; forwarded packets get
181
           defragmented by connection tracking coming in, then
182
           fragmented (grr) by the forward code.
183
 
184
           In future: If we have nfct != NULL, AND we have NAT
185
           initialized, AND there is no helper, then we can do full
186
           NAPT on the head, and IP-address-only NAT on the rest.
187
 
188
           I'm starting to have nightmares about fragments.  */
189
 
190
        if ((*pskb)->nh.iph->frag_off & htons(IP_MF|IP_OFFSET)) {
191
                *pskb = ip_ct_gather_frags(*pskb);
192
 
193
                if (!*pskb)
194
                        return NF_STOLEN;
195
        }
196
 
197
        return ip_nat_fn(hooknum, pskb, in, out, okfn);
198
}
199
 
200
#ifdef CONFIG_IP_NF_NAT_LOCAL
201
static unsigned int
202
ip_nat_local_fn(unsigned int hooknum,
203
                struct sk_buff **pskb,
204
                const struct net_device *in,
205
                const struct net_device *out,
206
                int (*okfn)(struct sk_buff *))
207
{
208
        u_int32_t saddr, daddr;
209
        unsigned int ret;
210
 
211
        /* root is playing with raw sockets. */
212
        if ((*pskb)->len < sizeof(struct iphdr)
213
            || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr))
214
                return NF_ACCEPT;
215
 
216
        saddr = (*pskb)->nh.iph->saddr;
217
        daddr = (*pskb)->nh.iph->daddr;
218
 
219
        ret = ip_nat_fn(hooknum, pskb, in, out, okfn);
220
        if (ret != NF_DROP && ret != NF_STOLEN
221
            && ((*pskb)->nh.iph->saddr != saddr
222
                || (*pskb)->nh.iph->daddr != daddr))
223
                return ip_route_me_harder(pskb) == 0 ? ret : NF_DROP;
224
        return ret;
225
}
226
#endif
227
 
228
/* We must be after connection tracking and before packet filtering. */
229
 
230
/* Before packet filtering, change destination */
231
static struct nf_hook_ops ip_nat_in_ops
232
= { { NULL, NULL }, ip_nat_fn, PF_INET, NF_IP_PRE_ROUTING, NF_IP_PRI_NAT_DST };
233
/* After packet filtering, change source */
234
static struct nf_hook_ops ip_nat_out_ops
235
= { { NULL, NULL }, ip_nat_out, PF_INET, NF_IP_POST_ROUTING, NF_IP_PRI_NAT_SRC};
236
 
237
#ifdef CONFIG_IP_NF_NAT_LOCAL
238
/* Before packet filtering, change destination */
239
static struct nf_hook_ops ip_nat_local_out_ops
240
= { { NULL, NULL }, ip_nat_local_fn, PF_INET, NF_IP_LOCAL_OUT, NF_IP_PRI_NAT_DST };
241
/* After packet filtering, change source for reply packets of LOCAL_OUT DNAT */
242
static struct nf_hook_ops ip_nat_local_in_ops
243
= { { NULL, NULL }, ip_nat_fn, PF_INET, NF_IP_LOCAL_IN, NF_IP_PRI_NAT_SRC };
244
#endif
245
 
246
/* Protocol registration. */
247
int ip_nat_protocol_register(struct ip_nat_protocol *proto)
248
{
249
        int ret = 0;
250
        struct list_head *i;
251
 
252
        WRITE_LOCK(&ip_nat_lock);
253
        for (i = protos.next; i != &protos; i = i->next) {
254
                if (((struct ip_nat_protocol *)i)->protonum
255
                    == proto->protonum) {
256
                        ret = -EBUSY;
257
                        goto out;
258
                }
259
        }
260
 
261
        list_prepend(&protos, proto);
262
        MOD_INC_USE_COUNT;
263
 
264
 out:
265
        WRITE_UNLOCK(&ip_nat_lock);
266
        return ret;
267
}
268
 
269
/* Noone stores the protocol anywhere; simply delete it. */
270
void ip_nat_protocol_unregister(struct ip_nat_protocol *proto)
271
{
272
        WRITE_LOCK(&ip_nat_lock);
273
        LIST_DELETE(&protos, proto);
274
        WRITE_UNLOCK(&ip_nat_lock);
275
 
276
        /* Someone could be still looking at the proto in a bh. */
277
        br_write_lock_bh(BR_NETPROTO_LOCK);
278
        br_write_unlock_bh(BR_NETPROTO_LOCK);
279
 
280
        MOD_DEC_USE_COUNT;
281
}
282
 
283
static int init_or_cleanup(int init)
284
{
285
        int ret = 0;
286
 
287
        if (!init) goto cleanup;
288
 
289
        ret = ip_nat_rule_init();
290
        if (ret < 0) {
291
                printk("ip_nat_init: can't setup rules.\n");
292
                goto cleanup_nothing;
293
        }
294
        ret = ip_nat_init();
295
        if (ret < 0) {
296
                printk("ip_nat_init: can't setup rules.\n");
297
                goto cleanup_rule_init;
298
        }
299
        ret = nf_register_hook(&ip_nat_in_ops);
300
        if (ret < 0) {
301
                printk("ip_nat_init: can't register in hook.\n");
302
                goto cleanup_nat;
303
        }
304
        ret = nf_register_hook(&ip_nat_out_ops);
305
        if (ret < 0) {
306
                printk("ip_nat_init: can't register out hook.\n");
307
                goto cleanup_inops;
308
        }
309
#ifdef CONFIG_IP_NF_NAT_LOCAL
310
        ret = nf_register_hook(&ip_nat_local_out_ops);
311
        if (ret < 0) {
312
                printk("ip_nat_init: can't register local out hook.\n");
313
                goto cleanup_outops;
314
        }
315
        ret = nf_register_hook(&ip_nat_local_in_ops);
316
        if (ret < 0) {
317
                printk("ip_nat_init: can't register local in hook.\n");
318
                goto cleanup_localoutops;
319
        }
320
#endif
321
        return ret;
322
 
323
 cleanup:
324
#ifdef CONFIG_IP_NF_NAT_LOCAL
325
        nf_unregister_hook(&ip_nat_local_in_ops);
326
 cleanup_localoutops:
327
        nf_unregister_hook(&ip_nat_local_out_ops);
328
 cleanup_outops:
329
#endif
330
        nf_unregister_hook(&ip_nat_out_ops);
331
 cleanup_inops:
332
        nf_unregister_hook(&ip_nat_in_ops);
333
 cleanup_nat:
334
        ip_nat_cleanup();
335
 cleanup_rule_init:
336
        ip_nat_rule_cleanup();
337
 cleanup_nothing:
338
        MUST_BE_READ_WRITE_UNLOCKED(&ip_nat_lock);
339
        return ret;
340
}
341
 
342
static int __init init(void)
343
{
344
        return init_or_cleanup(1);
345
}
346
 
347
static void __exit fini(void)
348
{
349
        init_or_cleanup(0);
350
}
351
 
352
module_init(init);
353
module_exit(fini);
354
 
355
EXPORT_SYMBOL(ip_nat_setup_info);
356
EXPORT_SYMBOL(ip_nat_protocol_register);
357
EXPORT_SYMBOL(ip_nat_protocol_unregister);
358
EXPORT_SYMBOL(ip_nat_helper_register);
359
EXPORT_SYMBOL(ip_nat_helper_unregister);
360
EXPORT_SYMBOL(ip_nat_cheat_check);
361
EXPORT_SYMBOL(ip_nat_mangle_tcp_packet);
362
EXPORT_SYMBOL(ip_nat_mangle_udp_packet);
363
EXPORT_SYMBOL(ip_nat_used_tuple);
364
MODULE_LICENSE("GPL");

powered by: WebSVN 2.1.0

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