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

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [rc203soc/] [sw/] [uClinux/] [net/] [ipv4/] [ip_masq.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1629 jcastillo
/*
2
 *
3
 *      Masquerading functionality
4
 *
5
 *      Copyright (c) 1994 Pauline Middelink
6
 *
7
 *      See ip_fw.c for original log
8
 *
9
 * Fixes:
10
 *      Juan Jose Ciarlante     :       Modularized application masquerading (see ip_masq_app.c)
11
 *      Juan Jose Ciarlante     :       New struct ip_masq_seq that holds output/input delta seq.
12
 *      Juan Jose Ciarlante     :       Added hashed lookup by proto,maddr,mport and proto,saddr,sport
13
 *      Juan Jose Ciarlante     :       Fixed deadlock if free ports get exhausted
14
 *      Juan Jose Ciarlante     :       Added NO_ADDR status flag.
15
 *      Richard Lynch           :       Added IP Autoforward
16
 *      Nigel Metheringham      :       Added ICMP handling for demasquerade
17
 *      Nigel Metheringham      :       Checksum checking of masqueraded data
18
 *      Nigel Metheringham      :       Better handling of timeouts of TCP conns
19
 *      Keith Owens             :       Keep control channels alive if any related data entries.
20
 *      Delian Delchev          :       Added support for ICMP requests and replys
21
 *      Nigel Metheringham      :       ICMP in ICMP handling, tidy ups, bug fixes, made ICMP optional
22
 *      Juan Jose Ciarlante     :       re-assign maddr if no packet received from outside
23
 *      John D. Hardin          :       Added PPTP and IPSEC protocols
24
 *
25
 */
26
 
27
 
28
#include <linux/config.h>
29
#include <linux/module.h>
30
#include <linux/types.h>
31
#include <linux/kernel.h>
32
#include <linux/errno.h>
33
#include <linux/skbuff.h>
34
#include <asm/system.h>
35
#include <linux/stat.h>
36
#include <linux/proc_fs.h>
37
#include <linux/in.h>
38
#include <linux/ip.h>
39
#include <linux/inet.h>
40
#include <net/protocol.h>
41
#include <net/icmp.h>
42
#include <net/tcp.h>
43
#include <net/udp.h>
44
#include <net/checksum.h>
45
#include <net/ip_masq.h>
46
#include <linux/ip_fw.h>
47
 
48
#define IP_MASQ_TAB_SIZE 256    /* must be power of 2 */
49
 
50
#ifdef CONFIG_IP_MASQUERADE_PPTP
51
/*
52
 * This is clumsier than it otherwise might be (i.e. the
53
 * PPTP control channel sniffer should be a module, and there
54
 * should be a separate table for GRE masq entries so that
55
 * we're not making all of the hacks to the TCP table code)
56
 # but I wanted to keep the code changes localized to one file
57
 # if possible.
58
 * This should all be modular, and the table routines need to
59
 * be somewhat more generic.
60
 *
61
 * Maybe for 2.0.38 - we'll see.
62
 *
63
 * John Hardin <jhardin@wolfenet.com> gets all blame...
64
 * See also http://www.wolfenet.com/~jhardin/ip_masq_vpn.html
65
 */
66
 
67
static const char *strGREProt = "GRE";
68
 
69
#ifdef CONFIG_IP_MASQUERADE_PPTP_MULTICLIENT
70
/*
71
 * MULTICLIENT watches the control channel and preloads the
72
 * call ID into the masq table entry, so we want the
73
 * masq table entry to persist until a Call Disconnect
74
 * occurs, otherwise the call IDs will be lost and the link broken.
75
 */
76
#define MASQUERADE_EXPIRE_PPTP 15*60*HZ
77
 
78
/*
79
 * To support multiple clients communicating with the same server,
80
 * we have to sniff the control channel and trap the client's
81
 * call ID, then substitute a unique-to-the-firewall call ID.
82
 * Then on inbound GRE packets we use the bogus call ID to figure
83
 * out which client to route the traffic to, then replace the
84
 * bogus call ID with the client's real call ID, which we've saved.
85
 * For simplicity we'll use masq port as the bogus call ID.
86
 * The actual call ID will be stored in the masq table as
87
 * the source port, and the destination port will always be zero.
88
 *
89
 * NB: PPTP servers can tell whether the client is masqueraded by
90
 * looking for call IDs above 61000.
91
 */
92
#define PPTP_CONTROL_PORT 1723
93
 
94
#else /* CONFIG_IP_MASQUERADE_PPTP_MULTICLIENT */
95
 
96
/* non-MULTICLIENT ignores call IDs, so masq table
97
 * entries may expire quickly without causing problems.
98
 */
99
#define MASQUERADE_EXPIRE_PPTP 5*60*HZ
100
 
101
#endif /* CONFIG_IP_MASQUERADE_PPTP_MULTICLIENT */
102
 
103
/*
104
 * Define this here rather than in /usr/src/linux/include/wherever/whatever.h
105
 * in order to localize my mistakes to one file...
106
 *
107
 * This struct may be architecture-specific because of the bitmaps.
108
 */
109
struct pptp_gre_header {
110
        __u8
111
                recur:3,
112
                is_strict:1,
113
                has_seq:1,
114
                has_key:1,
115
                has_routing:1,
116
                has_cksum:1;
117
        __u8
118
                version:3,
119
                flags:5;
120
        __u16
121
                protocol,
122
                payload_len,
123
                call_id;        /* peer's call_id for this session */
124
 
125
};
126
 
127
#endif /* CONFIG_IP_MASQUERADE_PPTP */
128
 
129
 
130
#ifdef CONFIG_IP_MASQUERADE_IPSEC
131
/*
132
 * The above comments about PPTP apply here, too. This should all be a module.
133
 *
134
 * The "port numbers" for masq table purposes will be part of the
135
 * SPI, just to gain a little benefit from the hashing.
136
 */
137
 
138
static const char *strESPProt = "ESP";
139
static const char *strAHProt = "AH";
140
 
141
/*
142
 * ISAKMP uses 500/udp, and the traffic must come from
143
 * 500/udp (i.e. 500/udp <-> 500/udp), so we need to
144
 * check for ISAKMP UDP traffic and avoid changing the
145
 * source port number. In order to associate the data streams
146
 * we may need to sniff the ISAKMP cookies as well.
147
 */
148
#define UDP_PORT_ISAKMP 500     /* ISAKMP default UDP port */
149
 
150
#if CONFIG_IP_MASQUERADE_IPSEC_EXPIRE > 15
151
#define MASQUERADE_EXPIRE_IPSEC CONFIG_IP_MASQUERADE_IPSEC_EXPIRE*60*HZ
152
#else
153
#define MASQUERADE_EXPIRE_IPSEC 15*60*HZ
154
#endif
155
 
156
/*
157
 * We can't know the inbound SPI until it comes in (the ISAKMP exchange
158
 * is encryptd so we can't sniff it out of that), so we associate inbound
159
 * and outbound traffic by inspection. If somebody sends a new packet to a
160
 * remote server, then block all other new traffic to that server until we
161
 * get a response from that server with a SPI we haven't seen yet. It is
162
 * assumed that this is the correct response - we have no way to verify it,
163
 * as everything else is encrypted.
164
 *
165
 * If there is a collision, the block will last for up to two minutes (or
166
 * whatever MASQUERADE_EXPIRE_IPSEC_INIT is set to), and if the client
167
 * retries during that time the timer will be reset. This could easily lead
168
 * to a Denial of Service, so we limit the number of retries that will
169
 * reset the timer. This means the maximum time the server could be blocked
170
 * is ((IPSEC_INIT_RETRIES + 1) * MASQUERADE_EXPIRE_IPSEC_INIT).
171
 *
172
 * Note: blocking will not affect already-established traffic (i.e. where
173
 * the inbound SPI has been associated with an outbound SPI).
174
 */
175
#define MASQUERADE_EXPIRE_IPSEC_INIT 2*60*HZ
176
#define IPSEC_INIT_RETRIES 5
177
 
178
/*
179
 * ...connections that don't get an answer are squelched
180
 * (recognized but ignored) for a short time to prevent DoS.
181
 * SPI values 1-255 are reserved by the IANA and are currently (2/99)
182
 * not assigned. If that should change, this number must also be changed
183
 * to an unused NONZERO value:
184
 */
185
#define IPSEC_INIT_SQUELCHED 1
186
 
187
struct ip_masq * ip_masq_out_get_ipsec(int protocol, __u32 s_addr, __u16 s_port, __u32 d_addr, __u16 d_port, __u32 o_spi);
188
struct ip_masq * ip_masq_in_get_ipsec(int protocol, __u32 s_addr, __u16 s_port, __u32 d_addr, __u16 d_port, __u32 i_spi);
189
struct ip_masq * ip_masq_out_get_isakmp(int protocol, __u32 s_addr, __u16 s_port, __u32 d_addr, __u16 d_port, __u32 cookie);
190
struct ip_masq * ip_masq_in_get_isakmp(int protocol, __u32 s_addr, __u16 s_port, __u32 d_addr, __u16 d_port, __u32 cookie);
191
 
192
#endif /* CONFIG_IP_MASQUERADE_IPSEC */
193
 
194
/*
195
 *      Implement IP packet masquerading
196
 */
197
 
198
static const char *strProt[] = {"UDP","TCP","ICMP"};
199
 
200
/*
201
 * masq_proto_num returns 0 for UDP, 1 for TCP, 2 for ICMP
202
 *
203
 * No, I am NOT going to add GRE/ESP/AH support to everything that relies on this...
204
 *
205
 */
206
 
207
static int masq_proto_num(unsigned proto)
208
{
209
   switch (proto)
210
   {
211
      case IPPROTO_UDP:  return (0); break;
212
#ifdef CONFIG_IP_MASQUERADE_PPTP
213
      case IPPROTO_GRE:
214
#endif /* CONFIG_IP_MASQUERADE_PPTP */
215
#ifdef CONFIG_IP_MASQUERADE_IPSEC
216
      case IPPROTO_ESP:
217
#endif /* CONFIG_IP_MASQUERADE_IPSEC */
218
      case IPPROTO_TCP:  return (1); break;
219
      case IPPROTO_ICMP: return (2); break;
220
      default:           return (-1); break;
221
   }
222
}
223
 
224
#ifdef CONFIG_IP_MASQUERADE_ICMP
225
/*
226
 * Converts an ICMP reply code into the equivalent request code
227
 */
228
static __inline__ const __u8 icmp_type_request(__u8 type)
229
{
230
   switch (type)
231
   {
232
      case ICMP_ECHOREPLY: return ICMP_ECHO; break;
233
      case ICMP_TIMESTAMPREPLY: return ICMP_TIMESTAMP; break;
234
      case ICMP_INFO_REPLY: return ICMP_INFO_REQUEST; break;
235
      case ICMP_ADDRESSREPLY: return ICMP_ADDRESS; break;
236
      default: return (255); break;
237
   }
238
}
239
 
240
/*
241
 * Helper macros - attempt to make code clearer!
242
 */
243
 
244
/* ID used in ICMP lookups */
245
#define icmp_id(icmph)          ((icmph->un).echo.id)
246
/* (port) hash value using in ICMP lookups for requests */
247
#define icmp_hv_req(icmph)      ((__u16)(icmph->code+(__u16)(icmph->type<<8)))
248
/* (port) hash value using in ICMP lookups for replies */
249
#define icmp_hv_rep(icmph)      ((__u16)(icmph->code+(__u16)(icmp_type_request(icmph->type)<<8)))
250
#endif
251
 
252
static __inline__ const char *masq_proto_name(unsigned proto)
253
{
254
 
255
        /*
256
         * I don't want to track down everything that
257
         * relies on masq_proto_num() and make it GRE/ESP/AH-tolerant.
258
         */
259
#ifdef CONFIG_IP_MASQUERADE_PPTP
260
        if (proto == IPPROTO_GRE) {
261
          return strGREProt;
262
        }
263
#endif /* CONFIG_IP_MASQUERADE_PPTP */
264
#ifdef CONFIG_IP_MASQUERADE_IPSEC
265
        if (proto == IPPROTO_ESP) {
266
          return strESPProt;
267
        } else if (proto == IPPROTO_AH) {
268
          return strAHProt;
269
        }
270
#endif /* CONFIG_IP_MASQUERADE_IPSEC */
271
 
272
        return strProt[masq_proto_num(proto)];
273
}
274
 
275
/*
276
 *      Last masq_port number in use.
277
 *      Will cycle in MASQ_PORT boundaries.
278
 */
279
static __u16 masq_port = PORT_MASQ_BEGIN;
280
 
281
/*
282
 *      free ports counters (UDP & TCP)
283
 *
284
 *      Their value is _less_ or _equal_ to actual free ports:
285
 *      same masq port, diff masq addr (firewall iface address) allocated
286
 *      entries are accounted but their actually don't eat a more than 1 port.
287
 *
288
 *      Greater values could lower MASQ_EXPIRATION setting as a way to
289
 *      manage 'masq_entries resource'.
290
 *
291
 */
292
 
293
int ip_masq_free_ports[3] = {
294
        PORT_MASQ_END - PORT_MASQ_BEGIN,        /* UDP */
295
        PORT_MASQ_END - PORT_MASQ_BEGIN,        /* TCP */
296
        PORT_MASQ_END - PORT_MASQ_BEGIN         /* ICMP */
297
};
298
 
299
static struct symbol_table ip_masq_syms = {
300
#include <linux/symtab_begin.h>
301
        X(ip_masq_new),
302
        X(ip_masq_set_expire),
303
        X(ip_masq_free_ports),
304
        X(ip_masq_expire),
305
        X(ip_masq_out_get_2),
306
#include <linux/symtab_end.h>
307
};
308
 
309
/*
310
 *      2 ip_masq hash tables: for input and output pkts lookups.
311
 */
312
 
313
struct ip_masq *ip_masq_m_tab[IP_MASQ_TAB_SIZE];
314
struct ip_masq *ip_masq_s_tab[IP_MASQ_TAB_SIZE];
315
 
316
#ifdef CONFIG_IP_MASQUERADE_IPSEC
317
        /*
318
         * Add a third hash table for input lookup by remote side
319
         */
320
struct ip_masq *ip_masq_d_tab[IP_MASQ_TAB_SIZE];
321
 
322
#endif /* CONFIG_IP_MASQUERADE_IPSEC */
323
 
324
/*
325
 * timeouts
326
 */
327
 
328
static struct ip_fw_masq ip_masq_dummy = {
329
        MASQUERADE_EXPIRE_TCP,
330
        MASQUERADE_EXPIRE_TCP_FIN,
331
        MASQUERADE_EXPIRE_UDP
332
};
333
 
334
struct ip_fw_masq *ip_masq_expire = &ip_masq_dummy;
335
 
336
#ifdef CONFIG_IP_MASQUERADE_IPAUTOFW
337
/*
338
 *      Auto-forwarding table
339
 */
340
 
341
struct ip_autofw * ip_autofw_hosts = NULL;
342
 
343
/*
344
 *      Check if a masq entry should be created for a packet
345
 */
346
 
347
struct ip_autofw * ip_autofw_check_range (__u32 where, __u16 port, __u16 protocol, int reqact)
348
{
349
        struct ip_autofw *af;
350
        af=ip_autofw_hosts;
351
        port=ntohs(port);
352
        while (af)
353
        {
354
                if (af->type==IP_FWD_RANGE &&
355
                     port>=af->low &&
356
                     port<=af->high &&
357
                     protocol==af->protocol &&
358
                     /* it's ok to create masq entries after the timeout if we're in insecure mode */
359
                     (af->flags & IP_AUTOFW_ACTIVE || !reqact || !(af->flags & IP_AUTOFW_SECURE)) &&
360
                     (!(af->flags & IP_AUTOFW_SECURE) || af->lastcontact==where || !reqact))
361
                        return(af);
362
                af=af->next;
363
        }
364
        return(NULL);
365
}
366
 
367
struct ip_autofw * ip_autofw_check_port (__u16 port, __u16 protocol)
368
{
369
        struct ip_autofw *af;
370
        af=ip_autofw_hosts;
371
        port=ntohs(port);
372
        while (af)
373
        {
374
                if (af->type==IP_FWD_PORT && port==af->visible && protocol==af->protocol)
375
                        return(af);
376
                af=af->next;
377
        }
378
        return(NULL);
379
}
380
 
381
struct ip_autofw * ip_autofw_check_direct (__u16 port, __u16 protocol)
382
{
383
        struct ip_autofw *af;
384
        af=ip_autofw_hosts;
385
        port=ntohs(port);
386
        while (af)
387
        {
388
                if (af->type==IP_FWD_DIRECT && af->low<=port && af->high>=port)
389
                        return(af);
390
                af=af->next;
391
        }
392
        return(NULL);
393
}
394
 
395
void ip_autofw_update_out (__u32 who, __u32 where, __u16 port, __u16 protocol)
396
{
397
        struct ip_autofw *af;
398
        af=ip_autofw_hosts;
399
        port=ntohs(port);
400
        while (af)
401
        {
402
                if (af->type==IP_FWD_RANGE && af->ctlport==port && af->ctlproto==protocol)
403
                {
404
                        if (af->flags & IP_AUTOFW_USETIME)
405
                        {
406
                                if (af->timer.expires)
407
                                        del_timer(&af->timer);
408
                                af->timer.expires=jiffies+IP_AUTOFW_EXPIRE;
409
                                add_timer(&af->timer);
410
                        }
411
                        af->flags|=IP_AUTOFW_ACTIVE;
412
                        af->lastcontact=where;
413
                        af->where=who;
414
                }
415
                af=af->next;
416
        }
417
}
418
 
419
void ip_autofw_update_in (__u32 where, __u16 port, __u16 protocol)
420
{
421
/*      struct ip_autofw *af;
422
        af=ip_autofw_check_range(where, port,protocol);
423
        if (af)
424
        {
425
                del_timer(&af->timer);
426
                af->timer.expires=jiffies+IP_AUTOFW_EXPIRE;
427
                add_timer(&af->timer);
428
        }*/
429
}
430
 
431
#endif /* CONFIG_IP_MASQUERADE_IPAUTOFW */
432
 
433
#ifdef CONFIG_IP_MASQUERADE_IPPORTFW
434
 
435
struct ip_portfw *ipportfw_lst[2];
436
 
437
struct ip_portfw *ip_portfw_lookup(__u16 protocol, __u16 lport, __u32 laddr, __u32 *daddr, __u16 *dport) {
438
       int prot = (protocol==IPPROTO_TCP);
439
       struct ip_portfw *n;
440
       unsigned long   flags;
441
 
442
       save_flags(flags); cli();
443
       for (n = ipportfw_lst[prot] ; n; n = n->next)
444
       {
445
               if (lport == n->lport && laddr == n->laddr) {
446
                       *daddr = n->raddr;
447
                       *dport = n->rport;
448
                       restore_flags(flags);
449
                       return n;
450
               }
451
       }
452
       restore_flags(flags);
453
       return NULL;
454
}
455
 
456
int ip_portfw_check(__u16 protocol, __u16 lport, __u32 laddr)
457
{
458
       __u16 rport;
459
       __u32 raddr;
460
       return (ip_portfw_lookup(protocol, lport, laddr, &raddr, &rport) != NULL);
461
}
462
#endif /* CONFIG_IP_MASQUERADE_IPPORTFW */
463
 
464
 
465
/*
466
 *      Returns hash value
467
 */
468
 
469
static __inline__ unsigned
470
ip_masq_hash_key(unsigned proto, __u32 addr, __u16 port)
471
{
472
        return (proto^ntohl(addr)^ntohs(port)) & (IP_MASQ_TAB_SIZE-1);
473
}
474
 
475
/*
476
 *      Hashes ip_masq by its proto,addrs,ports.
477
 *      should be called with masked interrupts.
478
 *      returns bool success.
479
 */
480
 
481
static __inline__ int
482
ip_masq_hash(struct ip_masq *ms)
483
{
484
        unsigned hash;
485
 
486
        if (ms->flags & IP_MASQ_F_HASHED) {
487
                printk("ip_masq_hash(): request for already hashed\n");
488
                return 0;
489
        }
490
        /*
491
         *      Hash by proto,m{addr,port}
492
         */
493
        hash = ip_masq_hash_key(ms->protocol, ms->maddr, ms->mport);
494
        ms->m_link = ip_masq_m_tab[hash];
495
        ip_masq_m_tab[hash] = ms;
496
 
497
        /*
498
         *      Hash by proto,s{addr,port}
499
         */
500
#ifdef CONFIG_IP_MASQUERADE_PPTP
501
        if (ms->protocol == IPPROTO_GRE) {
502
                /* Ignore the source port (Call ID) when hashing, as
503
                 * outbound packets will not be able to supply it...
504
                 */
505
                hash = ip_masq_hash_key(ms->protocol, ms->saddr, 0);
506
        } else
507
#endif /* CONFIG_IP_MASQUERADE_PPTP */
508
        hash = ip_masq_hash_key(ms->protocol, ms->saddr, ms->sport);
509
        ms->s_link = ip_masq_s_tab[hash];
510
        ip_masq_s_tab[hash] = ms;
511
 
512
#ifdef CONFIG_IP_MASQUERADE_IPSEC
513
        /*
514
          * Hash by proto,d{addr,port}
515
          */
516
        hash = ip_masq_hash_key(ms->protocol, ms->daddr, ms->dport);
517
        ms->d_link = ip_masq_d_tab[hash];
518
        ip_masq_d_tab[hash] = ms;
519
#endif /* CONFIG_IP_MASQUERADE_IPSEC */
520
 
521
        ms->flags |= IP_MASQ_F_HASHED;
522
        return 1;
523
}
524
 
525
/*
526
 *      UNhashes ip_masq from ip_masq_[ms]_tables.
527
 *      should be called with masked interrupts.
528
 *      returns bool success.
529
 */
530
 
531
static __inline__ int ip_masq_unhash(struct ip_masq *ms)
532
{
533
        unsigned hash;
534
        struct ip_masq ** ms_p;
535
        if (!(ms->flags & IP_MASQ_F_HASHED)) {
536
                printk("ip_masq_unhash(): request for unhash flagged\n");
537
                return 0;
538
        }
539
        /*
540
         *      UNhash by m{addr,port}
541
         */
542
        hash = ip_masq_hash_key(ms->protocol, ms->maddr, ms->mport);
543
        for (ms_p = &ip_masq_m_tab[hash]; *ms_p ; ms_p = &(*ms_p)->m_link)
544
                if (ms == (*ms_p))  {
545
                        *ms_p = ms->m_link;
546
                        break;
547
                }
548
        /*
549
         *      UNhash by s{addr,port}
550
         */
551
#ifdef CONFIG_IP_MASQUERADE_PPTP
552
        if (ms->protocol == IPPROTO_GRE) {
553
                hash = ip_masq_hash_key(ms->protocol, ms->saddr, 0);
554
        } else
555
#endif /* CONFIG_IP_MASQUERADE_PPTP */
556
        hash = ip_masq_hash_key(ms->protocol, ms->saddr, ms->sport);
557
        for (ms_p = &ip_masq_s_tab[hash]; *ms_p ; ms_p = &(*ms_p)->s_link)
558
                if (ms == (*ms_p))  {
559
                        *ms_p = ms->s_link;
560
                        break;
561
                }
562
 
563
#ifdef CONFIG_IP_MASQUERADE_IPSEC
564
        /*
565
         * UNhash by d{addr,port}
566
         */
567
        hash = ip_masq_hash_key(ms->protocol, ms->daddr, ms->dport);
568
        for (ms_p = &ip_masq_d_tab[hash]; *ms_p ; ms_p = &(*ms_p)->d_link)
569
                if (ms == (*ms_p))  {
570
                        *ms_p = ms->d_link;
571
                        break;
572
                }
573
#endif /* CONFIG_IP_MASQUERADE_IPSEC */
574
 
575
        ms->flags &= ~IP_MASQ_F_HASHED;
576
        return 1;
577
}
578
 
579
/*
580
 *      Returns ip_masq associated with addresses found in iph.
581
 *      called for pkts coming from outside-to-INside the firewall
582
 *
583
 *      NB. Cannot check destination address, just for the incoming port.
584
 *      reason: archie.doc.ac.uk has 6 interfaces, you send to
585
 *      phoenix and get a reply from any other interface(==dst)!
586
 *
587
 *      [Only for UDP] - AC
588
 */
589
 
590
struct ip_masq *
591
ip_masq_in_get(struct iphdr *iph)
592
{
593
        __u16 *portptr;
594
        int protocol;
595
        __u32 s_addr, d_addr;
596
        __u16 s_port, d_port;
597
#ifdef CONFIG_IP_MASQUERADE_IPSEC
598
        __u32 cookie;
599
#endif /* CONFIG_IP_MASQUERADE_IPSEC */
600
 
601
        portptr = (__u16 *)&(((char *)iph)[iph->ihl*4]);
602
        protocol = iph->protocol;
603
        s_addr = iph->saddr;
604
        s_port = portptr[0];
605
        d_addr = iph->daddr;
606
        d_port = portptr[1];
607
 
608
#ifdef CONFIG_IP_MASQUERADE_IPSEC
609
        if (protocol == IPPROTO_UDP && ntohs(s_port) == UDP_PORT_ISAKMP && ntohs(d_port) == UDP_PORT_ISAKMP) {
610
                cookie = *((__u32 *)&portptr[4]);
611
                return ip_masq_in_get_isakmp(protocol, s_addr, s_port, d_addr, d_port, cookie);
612
        } else
613
#endif /* CONFIG_IP_MASQUERADE_IPSEC */
614
 
615
        return ip_masq_in_get_2(protocol, s_addr, s_port, d_addr, d_port);
616
}
617
 
618
/*
619
 *      Returns ip_masq associated with supplied parameters, either
620
 *      broken out of the ip/tcp headers or directly supplied for those
621
 *      pathological protocols with address/port in the data stream
622
 *      (ftp, irc).  addresses and ports are in network order.
623
 *      called for pkts coming from outside-to-INside the firewall.
624
 *
625
 *      NB. Cannot check destination address, just for the incoming port.
626
 *      reason: archie.doc.ac.uk has 6 interfaces, you send to
627
 *      phoenix and get a reply from any other interface(==dst)!
628
 *
629
 *      [Only for UDP] - AC
630
 */
631
 
632
struct ip_masq *
633
ip_masq_in_get_2(int protocol, __u32 s_addr, __u16 s_port, __u32 d_addr, __u16 d_port)
634
{
635
        unsigned hash;
636
        struct ip_masq *ms;
637
 
638
        hash = ip_masq_hash_key(protocol, d_addr, d_port);
639
        for(ms = ip_masq_m_tab[hash]; ms ; ms = ms->m_link) {
640
                if (protocol==ms->protocol &&
641
                    ((s_addr==ms->daddr || ms->flags & IP_MASQ_F_NO_DADDR)
642
#ifdef CONFIG_IP_MASQUERADE_IPAUTOFW
643
                     || (ms->dport==htons(1558))
644
#endif /* CONFIG_IP_MASQUERADE_IPAUTOFW */
645
                     ) &&
646
                    (s_port==ms->dport || ms->flags & IP_MASQ_F_NO_DPORT) &&
647
                    (d_addr==ms->maddr && d_port==ms->mport)) {
648
#ifdef DEBUG_IP_MASQUERADE_VERBOSE
649
                        printk("MASQ: look/in %d %08X:%04hX->%08X:%04hX OK\n",
650
                               protocol,
651
                               s_addr,
652
                               s_port,
653
                               d_addr,
654
                               d_port);
655
#endif
656
                        return ms;
657
                }
658
        }
659
 
660
#ifdef CONFIG_IP_MASQUERADE_PPTP
661
        if (protocol == IPPROTO_GRE) {
662
                for(ms = ip_masq_m_tab[hash]; ms ; ms = ms->m_link) {
663
                        if (protocol==ms->protocol &&
664
#ifdef CONFIG_IP_MASQUERADE_PPTP_MULTICLIENT
665
                            ms->mport == d_port && /* ignore source port */
666
#else /* CONFIG_IP_MASQUERADE_PPTP_MULTICLIENT */
667
                            ms->mport == 0 && ms->sport == 0 &&
668
#endif /* CONFIG_IP_MASQUERADE_PPTP_MULTICLIENT */
669
                            s_addr==ms->daddr && d_addr==ms->maddr) {
670
#ifdef DEBUG_IP_MASQUERADE_PPTP_VERBOSE
671
                                printk(KERN_DEBUG "MASQ: look/in %d %08X:%04hX->%08X:%04hX OK\n",
672
                                       protocol,
673
                                       s_addr,
674
                                       s_port,
675
                                       d_addr,
676
                                       d_port);
677
#endif /* DEBUG_IP_MASQUERADE_PPTP_VERBOSE */
678
                                return ms;
679
                        }
680
                }
681
        }
682
#endif /* CONFIG_IP_MASQUERADE_PPTP */
683
 
684
 
685
#ifdef DEBUG_IP_MASQUERADE_VERBOSE
686
        printk("MASQ: look/in %d %08X:%04hX->%08X:%04hX fail\n",
687
               protocol,
688
               s_addr,
689
               s_port,
690
               d_addr,
691
               d_port);
692
#endif
693
        return NULL;
694
}
695
 
696
#ifdef CONFIG_IP_MASQUERADE_IPSEC
697
struct ip_masq *
698
ip_masq_in_get_ipsec(int protocol, __u32 s_addr, __u16 s_port, __u32 d_addr, __u16 d_port, __u32 i_spi)
699
{
700
        unsigned hash;
701
        struct ip_masq *ms;
702
 
703
        if (protocol != IPPROTO_ESP) {
704
                return ip_masq_in_get_2(protocol,s_addr,s_port,d_addr,d_port);
705
        }
706
 
707
        /* find an entry for a packet coming in from outside,
708
         * or find whether there's a setup pending
709
         */
710
 
711
        if (i_spi != 0) {
712
                /* there's a SPI - look for a completed entry */
713
                hash = ip_masq_hash_key(protocol, s_addr, s_port);
714
                for(ms = ip_masq_d_tab[hash]; ms ; ms = ms->d_link) {
715
                        if (protocol==ms->protocol &&
716
                            s_addr==ms->daddr &&
717
                            d_addr==ms->maddr &&
718
                            ms->ispi != 0 && i_spi==ms->ispi) {
719
#ifdef DEBUG_IP_MASQUERADE_IPSEC_VERBOSE
720
                                printk(KERN_DEBUG "MASQ: IPSEC look/in %08X->%08X:%08X OK\n",
721
                                       s_addr,
722
                                       d_addr,
723
                                       i_spi);
724
#endif
725
                                return ms;
726
                        }
727
                }
728
        }
729
 
730
        /* no joy. look for a pending connection - maybe somebody else's
731
         * if we're checking for a pending setup, the d_addr will be zero
732
         * to avoid having to know the masq IP.
733
         */
734
        hash = ip_masq_hash_key(protocol, s_addr, 0);
735
        for(ms = ip_masq_d_tab[hash]; ms ; ms = ms->d_link) {
736
                if (protocol==ms->protocol &&
737
                    s_addr==ms->daddr &&
738
                    (d_addr==0 || d_addr==ms->maddr) &&
739
                    ms->ispi==0) {
740
#ifdef DEBUG_IP_MASQUERADE_IPSEC_VERBOSE
741
                        printk(KERN_DEBUG "MASQ: IPSEC look/in %08X->%08X:0 OK\n",
742
                               s_addr,
743
                               d_addr
744
                               );
745
#endif
746
                        return ms;
747
                }
748
        }
749
 
750
#ifdef DEBUG_IP_MASQUERADE_IPSEC_VERBOSE
751
        printk(KERN_DEBUG "MASQ: IPSEC look/in %08X->%08X:%08X fail\n",
752
               s_addr,
753
               d_addr,
754
               i_spi);
755
#endif
756
        return NULL;
757
}
758
 
759
struct ip_masq *
760
ip_masq_in_get_isakmp(int protocol, __u32 s_addr, __u16 s_port, __u32 d_addr, __u16 d_port, __u32 cookie)
761
{
762
        unsigned hash;
763
        struct ip_masq *ms;
764
 
765
#ifdef DEBUG_IP_MASQUERADE_IPSEC
766
        printk(KERN_DEBUG "ip_masq_in_get_isakmp(): ");
767
        printk("%s -> ", in_ntoa(s_addr));
768
        printk("%s cookie %lX\n", in_ntoa(d_addr), ntohl(cookie));
769
#endif /* DEBUG_IP_MASQUERADE_IPSEC */
770
 
771
        if (cookie == 0) {
772
                printk(KERN_INFO "ip_masq_in_get_isakmp(): ");
773
                printk("zero cookie from %s\n", in_ntoa(s_addr));
774
        }
775
 
776
        hash = ip_masq_hash_key(protocol, d_addr, d_port);
777
        for(ms = ip_masq_m_tab[hash]; ms ; ms = ms->m_link) {
778
                if (protocol==ms->protocol &&
779
                    cookie==ms->ospi &&
780
                    ((s_addr==ms->daddr || ms->flags & IP_MASQ_F_NO_DADDR)
781
                     ) &&
782
                    (s_port==ms->dport || ms->flags & IP_MASQ_F_NO_DPORT) &&
783
                    (d_addr==ms->maddr && d_port==ms->mport)) {
784
#ifdef DEBUG_IP_MASQUERADE_IPSEC_VERBOSE
785
                        printk(KERN_DEBUG "MASQ: look/in %d %08X:%04hX->%08X:%04hX %08X OK\n",
786
                               protocol,
787
                               s_addr,
788
                               s_port,
789
                               d_addr,
790
                               d_port,
791
                               cookie);
792
#endif
793
                        return ms;
794
                }
795
        }
796
 
797
#ifdef DEBUG_IP_MASQUERADE_IPSEC_VERBOSE
798
        printk(KERN_DEBUG "MASQ: look/in %d %08X:%04hX->%08X:%04hX %08X fail\n",
799
               protocol,
800
               s_addr,
801
               s_port,
802
               d_addr,
803
               d_port,
804
               cookie);
805
#endif
806
        return NULL;
807
}
808
 
809
#endif /* CONFIG_IP_MASQUERADE_IPSEC */
810
 
811
/*
812
 *      Returns ip_masq associated with addresses found in iph.
813
 *      called for pkts coming from inside-to-OUTside the firewall.
814
 */
815
 
816
struct ip_masq *
817
ip_masq_out_get(struct iphdr *iph)
818
{
819
        __u16 *portptr;
820
        int protocol;
821
        __u32 s_addr, d_addr;
822
        __u16 s_port, d_port;
823
#ifdef CONFIG_IP_MASQUERADE_IPSEC
824
        __u32 cookie;
825
#endif /* CONFIG_IP_MASQUERADE_IPSEC */
826
 
827
 
828
        portptr = (__u16 *)&(((char *)iph)[iph->ihl*4]);
829
        protocol = iph->protocol;
830
        s_addr = iph->saddr;
831
        s_port = portptr[0];
832
        d_addr = iph->daddr;
833
        d_port = portptr[1];
834
 
835
#ifdef CONFIG_IP_MASQUERADE_IPSEC
836
        if (protocol == IPPROTO_UDP && ntohs(s_port) == UDP_PORT_ISAKMP && ntohs(d_port) == UDP_PORT_ISAKMP) {
837
                cookie = *((__u32 *)&portptr[4]);
838
                return ip_masq_out_get_isakmp(protocol, s_addr, s_port, d_addr, d_port, cookie);
839
        } else
840
#endif /* CONFIG_IP_MASQUERADE_IPSEC */
841
 
842
        return ip_masq_out_get_2(protocol, s_addr, s_port, d_addr, d_port);
843
}
844
 
845
/*
846
 *      Returns ip_masq associated with supplied parameters, either
847
 *      broken out of the ip/tcp headers or directly supplied for those
848
 *      pathological protocols with address/port in the data stream
849
 *      (ftp, irc).  addresses and ports are in network order.
850
 *      called for pkts coming from inside-to-OUTside the firewall.
851
 *
852
 *      Normally we know the source address and port but for some protocols
853
 *      (e.g. ftp PASV) we do not know the source port initially.  Alas the
854
 *      hash is keyed on source port so if the first lookup fails then try again
855
 *      with a zero port, this time only looking at entries marked "no source
856
 *      port".
857
 */
858
 
859
struct ip_masq *
860
ip_masq_out_get_2(int protocol, __u32 s_addr, __u16 s_port, __u32 d_addr, __u16 d_port)
861
{
862
        unsigned hash;
863
        struct ip_masq *ms;
864
 
865
 
866
#ifdef CONFIG_IP_MASQUERADE_PPTP
867
#ifdef CONFIG_IP_MASQUERADE_PPTP_MULTICLIENT
868
        if (protocol == IPPROTO_GRE) {
869
                /*
870
                 * Call ID is saved in source port number,
871
                 * but we have no way of knowing it on the outbound packet...
872
                 * we only know the *other side's* Call ID
873
                 */
874
 
875
                hash = ip_masq_hash_key(protocol, s_addr, 0);
876
                for(ms = ip_masq_s_tab[hash]; ms ; ms = ms->s_link) {
877
                        if (protocol == ms->protocol &&
878
                            s_addr == ms->saddr && (s_port == 0 || s_port == ms->sport) &&
879
                            d_addr == ms->daddr && d_port == ms->dport ) {
880
#ifdef DEBUG_IP_MASQUERADE_VERBOSE
881
                                printk(KERN_DEBUG "MASQ: lk/out2 %d %08X:%04hX->%08X:%04hX OK\n",
882
                                       protocol,
883
                                       s_addr,
884
                                       s_port,
885
                                       d_addr,
886
                                       d_port);
887
#endif /* DEBUG_IP_MASQUERADE_VERBOSE */
888
                                return ms;
889
                        }
890
                }
891
        }
892
#endif /* CONFIG_IP_MASQUERADE_PPTP_MULTICLIENT */
893
#endif /* CONFIG_IP_MASQUERADE_PPTP */
894
 
895
        hash = ip_masq_hash_key(protocol, s_addr, s_port);
896
        for(ms = ip_masq_s_tab[hash]; ms ; ms = ms->s_link) {
897
                if (protocol == ms->protocol &&
898
                    s_addr == ms->saddr && s_port == ms->sport &&
899
                    d_addr == ms->daddr && d_port == ms->dport ) {
900
#ifdef DEBUG_IP_MASQUERADE_VERBOSE
901
                        printk("MASQ: lk/out1 %d %08X:%04hX->%08X:%04hX OK\n",
902
                               protocol,
903
                               s_addr,
904
                               s_port,
905
                               d_addr,
906
                               d_port);
907
#endif
908
                        return ms;
909
                }
910
        }
911
        hash = ip_masq_hash_key(protocol, s_addr, 0);
912
        for(ms = ip_masq_s_tab[hash]; ms ; ms = ms->s_link) {
913
                if (ms->flags & IP_MASQ_F_NO_SPORT &&
914
                    protocol == ms->protocol &&
915
                    s_addr == ms->saddr &&
916
                    d_addr == ms->daddr && d_port == ms->dport ) {
917
#ifdef DEBUG_IP_MASQUERADE_VERBOSE
918
                        printk("MASQ: lk/out2 %d %08X:%04hX->%08X:%04hX OK\n",
919
                               protocol,
920
                               s_addr,
921
                               s_port,
922
                               d_addr,
923
                               d_port);
924
#endif
925
                        return ms;
926
                }
927
        }
928
#ifdef DEBUG_IP_MASQUERADE_VERBOSE
929
        printk("MASQ: lk/out1 %d %08X:%04hX->%08X:%04hX fail\n",
930
               protocol,
931
               s_addr,
932
               s_port,
933
               d_addr,
934
               d_port);
935
#endif
936
        return NULL;
937
}
938
 
939
#ifdef CONFIG_IP_MASQUERADE_IPSEC
940
struct ip_masq *
941
ip_masq_out_get_ipsec(int protocol, __u32 s_addr, __u16 s_port, __u32 d_addr, __u16 d_port, __u32 o_spi)
942
{
943
        unsigned hash;
944
        struct ip_masq *ms;
945
 
946
        if (protocol != IPPROTO_ESP) {
947
                return ip_masq_out_get_2(protocol,s_addr,s_port,d_addr,d_port);
948
        }
949
 
950
        hash = ip_masq_hash_key(protocol, s_addr, s_port);
951
        for(ms = ip_masq_s_tab[hash]; ms ; ms = ms->s_link) {
952
                if (protocol==ms->protocol &&
953
                    s_addr==ms->saddr &&
954
                    d_addr==ms->daddr &&
955
                    o_spi==ms->ospi) {
956
#ifdef DEBUG_IP_MASQUERADE_IPSEC_VERBOSE
957
                        printk(KERN_DEBUG "MASQ: IPSEC look/out %08X:%08X->%08X OK\n",
958
                               s_addr,
959
                               o_spi,
960
                               d_addr);
961
#endif
962
                        return ms;
963
                }
964
        }
965
 
966
#ifdef DEBUG_IP_MASQUERADE_IPSEC_VERBOSE
967
        printk(KERN_DEBUG "MASQ: IPSEC look/out %08X:%08X->%08X fail\n",
968
               s_addr,
969
               o_spi,
970
               d_addr);
971
#endif
972
        return NULL;
973
}
974
 
975
struct ip_masq *
976
ip_masq_out_get_isakmp(int protocol, __u32 s_addr, __u16 s_port, __u32 d_addr, __u16 d_port, __u32 cookie)
977
{
978
        unsigned hash;
979
        struct ip_masq *ms;
980
 
981
#ifdef DEBUG_IP_MASQUERADE_IPSEC
982
        printk(KERN_DEBUG "ip_masq_out_get_isakmp(): ");
983
        printk("%s -> ", in_ntoa(s_addr));
984
        printk("%s cookie %lX\n", in_ntoa(d_addr), ntohl(cookie));
985
#endif /* DEBUG_IP_MASQUERADE_IPSEC */
986
 
987
        if (cookie == 0) {
988
                printk(KERN_INFO "ip_masq_out_get_isakmp(): ");
989
                printk("zero cookie from %s\n", in_ntoa(s_addr));
990
        }
991
 
992
        hash = ip_masq_hash_key(protocol, s_addr, s_port);
993
        for(ms = ip_masq_s_tab[hash]; ms ; ms = ms->s_link) {
994
                if (protocol == ms->protocol &&
995
                    cookie == ms->ospi &&
996
                    s_addr == ms->saddr && s_port == ms->sport &&
997
                    d_addr == ms->daddr && d_port == ms->dport ) {
998
#ifdef DEBUG_IP_MASQUERADE_IPSEC_VERBOSE
999
                        printk(KERN_DEBUG "MASQ: lk/out1 %d %08X:%04hX->%08X:%04hX %08X OK\n",
1000
                               protocol,
1001
                               s_addr,
1002
                               s_port,
1003
                               d_addr,
1004
                               d_port,
1005
                               cookie);
1006
#endif
1007
                        return ms;
1008
                }
1009
        }
1010
 
1011
#ifdef DEBUG_IP_MASQUERADE_IPSEC_VERBOSE
1012
        printk(KERN_DEBUG "MASQ: lk/out1 %d %08X:%04hX->%08X:%04hX %08X fail\n",
1013
               protocol,
1014
               s_addr,
1015
               s_port,
1016
               d_addr,
1017
               d_port,
1018
               cookie);
1019
#endif
1020
        return NULL;
1021
}
1022
 
1023
#endif /* CONFIG_IP_MASQUERADE_IPSEC */
1024
 
1025
/*
1026
 *      Returns ip_masq for given proto,m_addr,m_port.
1027
 *      called by allocation routine to find an unused m_port.
1028
 */
1029
 
1030
struct ip_masq *
1031
ip_masq_getbym(int protocol, __u32 m_addr, __u16 m_port)
1032
{
1033
        unsigned hash;
1034
        struct ip_masq *ms;
1035
 
1036
        hash = ip_masq_hash_key(protocol, m_addr, m_port);
1037
        for(ms = ip_masq_m_tab[hash]; ms ; ms = ms->m_link) {
1038
                if ( protocol==ms->protocol &&
1039
                    (m_addr==ms->maddr && m_port==ms->mport))
1040
                        return ms;
1041
        }
1042
        return NULL;
1043
}
1044
 
1045
static void masq_expire(unsigned long data)
1046
{
1047
        struct ip_masq *ms = (struct ip_masq *)data, *ms_data;
1048
        unsigned long flags;
1049
 
1050
        if (ms->flags & IP_MASQ_F_CONTROL) {
1051
                /* a control channel is about to expire */
1052
                int idx = 0, reprieve = 0;
1053
#ifdef DEBUG_CONFIG_IP_MASQUERADE
1054
                printk("Masquerade control %s %lX:%X about to expire\n",
1055
                                masq_proto_name(ms->protocol),
1056
                                ntohl(ms->saddr),ntohs(ms->sport));
1057
#endif
1058
                save_flags(flags);
1059
                cli();
1060
 
1061
                /*
1062
                 * If any other masquerade entry claims that the expiring entry
1063
                 * is its control channel then keep the control entry alive.
1064
                 * Useful for long running data channels with inactive control
1065
                 * links which we don't want to lose, e.g. ftp.
1066
                 * Assumption: loops such as a->b->a or a->a will never occur.
1067
                 */
1068
                for (idx = 0; idx < IP_MASQ_TAB_SIZE && !reprieve; idx++) {
1069
                        for (ms_data = ip_masq_m_tab[idx]; ms_data ; ms_data = ms_data->m_link) {
1070
                                if (ms_data->control == ms) {
1071
                                        reprieve = 1;   /* this control connection can live a bit longer */
1072
                                        ip_masq_set_expire(ms, ip_masq_expire->tcp_timeout);
1073
#ifdef DEBUG_CONFIG_IP_MASQUERADE
1074
                                        printk("Masquerade control %s %lX:%X expiry reprieved\n",
1075
                                                        masq_proto_name(ms->protocol),
1076
                                                        ntohl(ms->saddr),ntohs(ms->sport));
1077
#endif
1078
                                        break;
1079
                                }
1080
                        }
1081
                }
1082
                restore_flags(flags);
1083
                if (reprieve)
1084
                        return;
1085
        }
1086
 
1087
#ifdef DEBUG_CONFIG_IP_MASQUERADE
1088
        printk("Masqueraded %s %lX:%X expired\n",masq_proto_name(ms->protocol),ntohl(ms->saddr),ntohs(ms->sport));
1089
#endif
1090
 
1091
        save_flags(flags);
1092
        cli();
1093
 
1094
        if (ip_masq_unhash(ms)) {
1095
                ip_masq_free_ports[masq_proto_num(ms->protocol)]++;
1096
                if (ms->protocol != IPPROTO_ICMP)
1097
                             ip_masq_unbind_app(ms);
1098
                kfree_s(ms,sizeof(*ms));
1099
        }
1100
 
1101
        restore_flags(flags);
1102
}
1103
 
1104
#ifdef CONFIG_IP_MASQUERADE_IPAUTOFW
1105
void ip_autofw_expire(unsigned long data)
1106
{
1107
        struct ip_autofw * af;
1108
        af=(struct ip_autofw *) data;
1109
        af->flags&=0xFFFF ^ IP_AUTOFW_ACTIVE;
1110
        af->timer.expires=0;
1111
        af->lastcontact=0;
1112
        if (af->flags & IP_AUTOFW_SECURE)
1113
                af->where=0;
1114
}
1115
#endif /* CONFIG_IP_MASQUERADE_IPAUTOFW */
1116
 
1117
/*
1118
 *      Create a new masquerade list entry, also allocate an
1119
 *      unused mport, keeping the portnumber between the
1120
 *      given boundaries MASQ_BEGIN and MASQ_END.
1121
 */
1122
 
1123
struct ip_masq * ip_masq_new_enh(struct device *dev, int proto, __u32 saddr, __u16 sport, __u32 daddr, __u16 dport, unsigned mflags, __u16 matchport)
1124
{
1125
        struct ip_masq *ms, *mst;
1126
        int ports_tried, *free_ports_p;
1127
        unsigned long flags;
1128
        static int n_fails = 0;
1129
 
1130
        free_ports_p = &ip_masq_free_ports[masq_proto_num(proto)];
1131
 
1132
        if (*free_ports_p == 0) {
1133
                if (++n_fails < 5)
1134
                        printk("ip_masq_new(proto=%s): no free ports.\n",
1135
                               masq_proto_name(proto));
1136
                return NULL;
1137
        }
1138
        ms = (struct ip_masq *) kmalloc(sizeof(struct ip_masq), GFP_ATOMIC);
1139
        if (ms == NULL) {
1140
                if (++n_fails < 5)
1141
                        printk("ip_masq_new(proto=%s): no memory available.\n",
1142
                               masq_proto_name(proto));
1143
                return NULL;
1144
        }
1145
        memset(ms, 0, sizeof(*ms));
1146
        init_timer(&ms->timer);
1147
        ms->timer.data     = (unsigned long)ms;
1148
        ms->timer.function = masq_expire;
1149
        ms->protocol       = proto;
1150
        ms->saddr          = saddr;
1151
        ms->sport          = sport;
1152
        ms->daddr          = daddr;
1153
        ms->dport          = dport;
1154
        ms->flags          = mflags;
1155
        ms->app_data       = NULL;
1156
        ms->control        = NULL;
1157
 
1158
        if (proto == IPPROTO_UDP && !matchport)
1159
                ms->flags |= IP_MASQ_F_NO_DADDR;
1160
 
1161
        /* get masq address from rif */
1162
        ms->maddr          = dev->pa_addr;
1163
        /*
1164
         *      Setup new entry as not replied yet.
1165
         *      This flag will allow masq. addr (ms->maddr)
1166
         *      to follow forwarding interface address.
1167
         */
1168
        ms->flags         |= IP_MASQ_F_NO_REPLY;
1169
 
1170
        for (ports_tried = 0;
1171
             (*free_ports_p && (ports_tried <= (PORT_MASQ_END - PORT_MASQ_BEGIN)));
1172
             ports_tried++){
1173
 
1174
#ifdef CONFIG_IP_MASQUERADE_PPTP
1175
#ifndef CONFIG_IP_MASQUERADE_PPTP_MULTICLIENT
1176
                /* Ignoring PPTP call IDs.
1177
                 * Don't needlessly increase the TCP port pointer.
1178
                 */
1179
                if (proto == IPPROTO_GRE) {
1180
                        ms->mport = 0;
1181
                        mst = NULL;
1182
                } else {
1183
#endif /* CONFIG_IP_MASQUERADE_PPTP_MULTICLIENT */
1184
#endif /* CONFIG_IP_MASQUERADE_PPTP */
1185
 
1186
#ifdef CONFIG_IP_MASQUERADE_IPSEC
1187
                /* ESP masq keys off the SPI, not the port number.
1188
                 * Don't needlessly increase the TCP port pointer.
1189
                 */
1190
                if (proto == IPPROTO_ESP) {
1191
                        ms->mport = 0;
1192
                        mst = NULL;
1193
                } else {
1194
                if (proto == IPPROTO_UDP && ntohs(sport) == UDP_PORT_ISAKMP && ntohs(dport) == UDP_PORT_ISAKMP) {
1195
                        /* the port number cannot be changed */
1196
                        ms->mport = htons(UDP_PORT_ISAKMP);
1197
                        mst = NULL;
1198
                } else {
1199
#endif /* CONFIG_IP_MASQUERADE_IPSEC */
1200
 
1201
                save_flags(flags);
1202
                cli();
1203
 
1204
                /*
1205
                 *      Try the next available port number
1206
                 */
1207
                if (!matchport || ports_tried)
1208
                        ms->mport = htons(masq_port++);
1209
                else
1210
                        ms->mport = matchport;
1211
 
1212
                if (masq_port==PORT_MASQ_END) masq_port = PORT_MASQ_BEGIN;
1213
 
1214
                restore_flags(flags);
1215
 
1216
                /*
1217
                 *      lookup to find out if this port is used.
1218
                 */
1219
 
1220
                mst = ip_masq_getbym(proto, ms->maddr, ms->mport);
1221
 
1222
#ifdef CONFIG_IP_MASQUERADE_PPTP
1223
#ifndef CONFIG_IP_MASQUERADE_PPTP_MULTICLIENT
1224
                }
1225
#endif /* CONFIG_IP_MASQUERADE_PPTP_MULTICLIENT */
1226
#endif /* CONFIG_IP_MASQUERADE_PPTP */
1227
 
1228
#ifdef CONFIG_IP_MASQUERADE_IPSEC
1229
                }
1230
                }
1231
#endif /* CONFIG_IP_MASQUERADE_IPSEC */
1232
 
1233
                if (mst == NULL || matchport) {
1234
                        save_flags(flags);
1235
                        cli();
1236
 
1237
                        if (*free_ports_p == 0) {
1238
                                restore_flags(flags);
1239
                                break;
1240
                        }
1241
                        (*free_ports_p)--;
1242
                        ip_masq_hash(ms);
1243
 
1244
                        restore_flags(flags);
1245
 
1246
                        if (proto != IPPROTO_ICMP)
1247
                              ip_masq_bind_app(ms);
1248
                        n_fails = 0;
1249
                        return ms;
1250
                }
1251
        }
1252
 
1253
        if (++n_fails < 5)
1254
                printk("ip_masq_new(proto=%s): could not get free masq entry (free=%d).\n",
1255
                       masq_proto_name(ms->protocol), *free_ports_p);
1256
        kfree_s(ms, sizeof(*ms));
1257
        return NULL;
1258
}
1259
 
1260
struct ip_masq * ip_masq_new(struct device *dev, int proto, __u32 saddr, __u16 sport, __u32 daddr, __u16 dport, unsigned mflags)
1261
{
1262
        return (ip_masq_new_enh(dev, proto, saddr, sport, daddr, dport, mflags, 0) );
1263
}
1264
 
1265
#ifdef CONFIG_IP_MASQUERADE_IPPORTFW
1266
 
1267
/*     New form of ip_masq creation which creates masqs for Port Forwarding
1268
 *     The routine is sufficiently different to ip_masq_new to require its own function
1269
 */
1270
 
1271
struct ip_masq * ip_masq_new_portfw(struct device *dev, int proto, __u32 raddr, __u16 rport, __u32 saddr, __u16 sport, __u32 laddr, __u16 lport)
1272
{
1273
       struct ip_masq *ms;
1274
       static int n_fails = 0;
1275
       unsigned long flags;
1276
 
1277
       ms = (struct ip_masq *) kmalloc(sizeof(struct ip_masq), GFP_ATOMIC);
1278
       if (ms == NULL) {
1279
               if (++n_fails < 5)
1280
                       printk("ip_masq_new_s(proto=%s): no memory available.\n",  masq_proto_name(proto));
1281
               return NULL;
1282
       }
1283
       memset(ms, 0, sizeof(*ms));
1284
       init_timer(&ms->timer);
1285
        ms->timer.data     = (unsigned long)ms;
1286
        ms->timer.function = masq_expire;
1287
        ms->protocol      = proto;
1288
        ms->saddr         = raddr;
1289
        ms->sport         = rport;
1290
        ms->daddr         = saddr;
1291
        ms->dport         = sport;
1292
        ms->maddr         = laddr;
1293
        ms->mport         = lport;
1294
        ms->flags         = 0;
1295
        ms->app_data      = NULL;
1296
        ms->control       = NULL;
1297
 
1298
        ip_masq_free_ports[masq_proto_num(proto)]--;
1299
 
1300
        save_flags(flags);
1301
        cli();
1302
        ip_masq_hash(ms);
1303
        restore_flags(flags);
1304
 
1305
        return ms;
1306
}
1307
#endif /* CONFIG_IP_MASQUERADE_IPPORTFW */
1308
 
1309
 
1310
/*
1311
 *      Set masq expiration (deletion) and adds timer,
1312
 *      if timeout==0 cancel expiration.
1313
 *      Warning: it does not check/delete previous timer!
1314
 */
1315
 
1316
void ip_masq_set_expire(struct ip_masq *ms, unsigned long tout)
1317
{
1318
        /* There Can Be Only One (timer on a masq table entry, that is) */
1319
        del_timer(&ms->timer);
1320
        if (tout) {
1321
                ms->timer.expires = jiffies+tout;
1322
                add_timer(&ms->timer);
1323
        }
1324
}
1325
 
1326
static void recalc_check(struct udphdr *uh, __u32 saddr,
1327
        __u32 daddr, int len)
1328
{
1329
        uh->check=0;
1330
        uh->check=csum_tcpudp_magic(saddr,daddr,len,
1331
                IPPROTO_UDP, csum_partial((char *)uh,len,0));
1332
        if(uh->check==0)
1333
                uh->check=0xFFFF;
1334
}
1335
 
1336
 
1337
#ifdef CONFIG_IP_MASQUERADE_PPTP
1338
/*
1339
 *      Masquerade of GRE connections
1340
 *      to support a PPTP VPN client or server.
1341
 */
1342
 
1343
/*
1344
 *      Handle outbound GRE packets.
1345
 *
1346
 *      This is largely a copy of ip_fw_masquerade()
1347
 */
1348
 
1349
int ip_fw_masq_gre(struct sk_buff **skb_p, struct device *dev)
1350
{
1351
        struct sk_buff  *skb   = *skb_p;
1352
        struct iphdr    *iph   = skb->h.iph;
1353
        struct pptp_gre_header  *greh;
1354
#ifdef DEBUG_IP_MASQUERADE_PPTP
1355
#ifdef DEBUG_IP_MASQUERADE_PPTP_VERBOSE
1356
        __u8            *greraw;
1357
#endif /* DEBUG_IP_MASQUERADE_PPTP_VERBOSE */
1358
#endif /* DEBUG_IP_MASQUERADE_PPTP */
1359
        struct ip_masq  *ms;
1360
        unsigned long    flags;
1361
 
1362
 
1363
        greh = (struct pptp_gre_header *)&(((char *)iph)[iph->ihl*4]);
1364
 
1365
#ifdef DEBUG_IP_MASQUERADE_PPTP
1366
 
1367
        printk(KERN_DEBUG "ip_fw_masq_gre(): ");
1368
        printk("Outbound GRE packet from %s", in_ntoa(iph->saddr));
1369
        printk(" to %s\n", in_ntoa(iph->daddr));
1370
 
1371
#ifdef DEBUG_IP_MASQUERADE_PPTP_VERBOSE
1372
        greraw = (__u8 *) greh;
1373
        printk(KERN_DEBUG "ip_fw_masq_gre(): ");
1374
        printk("GRE raw: %X %X %X %X %X %X %X %X %X %X %X %X.\n",
1375
                greraw[0],
1376
                greraw[1],
1377
                greraw[2],
1378
                greraw[3],
1379
                greraw[4],
1380
                greraw[5],
1381
                greraw[6],
1382
                greraw[7],
1383
                greraw[8],
1384
                greraw[9],
1385
                greraw[10],
1386
                greraw[11]);
1387
        printk(KERN_DEBUG "ip_fw_masq_gre(): ");
1388
        printk("GRE C: %d R: %d K: %d S: %d s: %d recur: %X.\n",
1389
                greh->has_cksum,
1390
                greh->has_routing,
1391
                greh->has_key,
1392
                greh->has_seq,
1393
                greh->is_strict,
1394
                greh->recur);
1395
        printk(KERN_DEBUG "ip_fw_masq_gre(): ");
1396
        printk("GRE flags: %X ver: %X.\n", greh->flags, greh->version);
1397
        printk(KERN_DEBUG "ip_fw_masq_gre(): ");
1398
        printk("GRE proto: %X.\n", ntohs(greh->protocol));
1399
#endif /* DEBUG_IP_MASQUERADE_PPTP_VERBOSE */
1400
#endif /* DEBUG_IP_MASQUERADE_PPTP */
1401
 
1402
        if (ntohs(greh->protocol) != 0x880B) {
1403
#ifdef DEBUG_IP_MASQUERADE_PPTP
1404
          printk(KERN_INFO "ip_fw_masq_gre(): ");
1405
          printk("GRE protocol %X not 0x880B (non-PPTP encap?) - discarding.\n", ntohs(greh->protocol));
1406
#endif /* DEBUG_IP_MASQUERADE_PPTP */
1407
          return -1;
1408
        }
1409
 
1410
        /*
1411
         *      Look for masq table entry
1412
         */
1413
 
1414
        ms = ip_masq_out_get_2(IPPROTO_GRE,
1415
                iph->saddr, 0,
1416
                iph->daddr, 0);
1417
 
1418
        if (ms!=NULL) {
1419
                /* delete the expiration timer */
1420
                ip_masq_set_expire(ms,0);
1421
 
1422
                /*
1423
                 *      Make sure that the masq IP address is correct
1424
                 *      for dynamic IP...
1425
                 */
1426
                if ( (ms->maddr != dev->pa_addr) && (sysctl_ip_dynaddr & 3) ) {
1427
                        printk(KERN_INFO "ip_fw_masq_gre(): ");
1428
                        printk("change maddr from %s", in_ntoa(ms->maddr));
1429
                        printk(" to %s\n", in_ntoa(dev->pa_addr));
1430
                        save_flags(flags);
1431
                        cli();
1432
                        ip_masq_unhash(ms);
1433
                        ms->maddr = dev->pa_addr;
1434
                        ip_masq_hash(ms);
1435
                        restore_flags(flags);
1436
                }
1437
        } else {
1438
                /*
1439
                 *      Nope, not found, create a new entry for it, maybe
1440
                 */
1441
 
1442
#ifdef CONFIG_IP_MASQUERADE_PPTP_MULTICLIENT
1443
                /* masq table entry has to come from control channel sniffing.
1444
                 * If we can't find one, it may have expired.
1445
                 * How can this happen with the control channel active?
1446
                 */
1447
                printk(KERN_INFO "ip_fw_masq_gre(): ");
1448
                printk("Outbound GRE to %s has no masq table entry.\n",
1449
                        in_ntoa(iph->daddr));
1450
                return -1;
1451
#else /* CONFIG_IP_MASQUERADE_PPTP_MULTICLIENT */
1452
                /* call IDs ignored, can create masq table entries on the fly. */
1453
                ms = ip_masq_new(dev, iph->protocol,
1454
                                 iph->saddr, 0,
1455
                                 iph->daddr, 0,
1456
                                 0);
1457
 
1458
                if (ms == NULL) {
1459
                        printk(KERN_NOTICE "ip_fw_masq_gre(): Couldn't create masq table entry.\n");
1460
                        return -1;
1461
                }
1462
#endif /* CONFIG_IP_MASQUERADE_PPTP_MULTICLIENT */
1463
        }
1464
 
1465
        /*
1466
         *      Set iph source addr from ip_masq obj.
1467
         */
1468
        iph->saddr = ms->maddr;
1469
 
1470
        /*
1471
         *      set timeout and check IP header
1472
         */
1473
 
1474
        ip_masq_set_expire(ms, MASQUERADE_EXPIRE_PPTP);
1475
        ip_send_check(iph);
1476
 
1477
#ifdef DEBUG_IP_MASQUERADE_PPTP
1478
        printk(KERN_DEBUG "MASQ: GRE O-routed from %s over %s\n",
1479
                in_ntoa(ms->maddr), dev->name);
1480
#endif /* DEBUG_IP_MASQUERADE_PPTP */
1481
 
1482
        return 0;
1483
}
1484
 
1485
/*
1486
 *      Handle inbound GRE packets.
1487
 *
1488
 */
1489
 
1490
int ip_fw_demasq_gre(struct sk_buff **skb_p, struct device *dev)
1491
{
1492
        struct sk_buff  *skb   = *skb_p;
1493
        struct iphdr    *iph   = skb->h.iph;
1494
        struct pptp_gre_header  *greh;
1495
#ifdef DEBUG_IP_MASQUERADE_PPTP
1496
#ifdef DEBUG_IP_MASQUERADE_PPTP_VERBOSE
1497
        __u8            *greraw;
1498
#endif /* DEBUG_IP_MASQUERADE_PPTP_VERBOSE */
1499
#endif /* DEBUG_IP_MASQUERADE_PPTP */
1500
        struct ip_masq  *ms;
1501
 
1502
 
1503
        greh = (struct pptp_gre_header *)&(((char *)iph)[iph->ihl*4]);
1504
 
1505
#ifdef DEBUG_IP_MASQUERADE_PPTP
1506
 
1507
#if 0
1508
        printk(KERN_DEBUG "ip_fw_demasq_gre(): ");
1509
        printk("Inbound GRE packet from %s", in_ntoa(iph->saddr));
1510
        printk(" to %s\n", in_ntoa(iph->daddr));
1511
#endif
1512
 
1513
#ifdef DEBUG_IP_MASQUERADE_PPTP_VERBOSE
1514
        greraw = (__u8 *) greh;
1515
        printk(KERN_DEBUG "ip_fw_demasq_gre(): ");
1516
        printk("GRE raw: %X %X %X %X %X %X %X %X %X %X %X %X.\n",
1517
                greraw[0],
1518
                greraw[1],
1519
                greraw[2],
1520
                greraw[3],
1521
                greraw[4],
1522
                greraw[5],
1523
                greraw[6],
1524
                greraw[7],
1525
                greraw[8],
1526
                greraw[9],
1527
                greraw[10],
1528
                greraw[11]);
1529
        printk(KERN_DEBUG "ip_fw_demasq_gre(): ");
1530
        printk("GRE C: %d R: %d K: %d S: %d s: %d recur: %X.\n",
1531
                greh->has_cksum,
1532
                greh->has_routing,
1533
                greh->has_key,
1534
                greh->has_seq,
1535
                greh->is_strict,
1536
                greh->recur);
1537
        printk(KERN_DEBUG "ip_fw_demasq_gre(): ");
1538
        printk("GRE flags: %X ver: %X.\n", greh->flags, greh->version);
1539
        printk(KERN_DEBUG "ip_fw_demasq_gre(): ");
1540
        printk("GRE proto: %X.\n", ntohs(greh->protocol));
1541
#endif /* DEBUG_IP_MASQUERADE_PPTP_VERBOSE */
1542
 
1543
#ifdef CONFIG_IP_MASQUERADE_PPTP_MULTICLIENT
1544
        printk(KERN_DEBUG "ip_fw_demasq_gre(): ");
1545
        printk("PPTP call ID: %X.\n", ntohs(greh->call_id));
1546
#endif /* CONFIG_IP_MASQUERADE_PPTP_MULTICLIENT */
1547
 
1548
#endif /* DEBUG_IP_MASQUERADE_PPTP */
1549
 
1550
        if (ntohs(greh->protocol) != 0x880B) {
1551
#ifdef DEBUG_IP_MASQUERADE_PPTP
1552
          printk(KERN_INFO "ip_fw_demasq_gre(): ");
1553
          printk("GRE protocol %X not 0x880B (non-PPTP encap?) - discarding.\n", ntohs(greh->protocol));
1554
#endif /* DEBUG_IP_MASQUERADE_PPTP */
1555
          return -1;
1556
        }
1557
 
1558
        /*
1559
         *      Look for a masq table entry and reroute if found
1560
         */
1561
 
1562
#ifdef CONFIG_IP_MASQUERADE_PPTP_MULTICLIENT
1563
        ms = ip_masq_getbym(IPPROTO_GRE,
1564
                iph->daddr, greh->call_id);
1565
#else /* CONFIG_IP_MASQUERADE_PPTP_MULTICLIENT */
1566
        ms = ip_masq_in_get_2(IPPROTO_GRE,
1567
                iph->saddr, 0,
1568
                iph->daddr, 0);
1569
#endif /* CONFIG_IP_MASQUERADE_PPTP_MULTICLIENT */
1570
 
1571
        if (ms != NULL)
1572
        {
1573
                /* delete the expiration timer */
1574
                ip_masq_set_expire(ms,0);
1575
 
1576
                iph->daddr = ms->saddr;
1577
 
1578
#ifdef CONFIG_IP_MASQUERADE_PPTP_MULTICLIENT
1579
                /*
1580
                 * change peer call ID to original value
1581
                 * (saved in masq table source port)
1582
                 */
1583
 
1584
                greh->call_id = ms->sport;
1585
 
1586
#ifdef DEBUG_IP_MASQUERADE_PPTP
1587
                printk(KERN_DEBUG "ip_fw_demasq_gre(): ");
1588
                printk("inbound PPTP from %s call ID now %X\n",
1589
                       in_ntoa(iph->saddr), ntohs(greh->call_id));
1590
#endif /* DEBUG_IP_MASQUERADE_PPTP */
1591
#endif /* CONFIG_IP_MASQUERADE_PPTP_MULTICLIENT */
1592
 
1593
                /*
1594
                 * resum checksums and set timeout
1595
                 */
1596
                ip_masq_set_expire(ms, MASQUERADE_EXPIRE_PPTP);
1597
                ip_send_check(iph);
1598
 
1599
#ifdef DEBUG_IP_MASQUERADE_PPTP
1600
                printk(KERN_DEBUG "MASQ: GRE I-routed to %s\n", in_ntoa(iph->daddr));
1601
#endif /* DEBUG_IP_MASQUERADE_PPTP */
1602
                return 1;
1603
        }
1604
 
1605
        /* sorry, all this trouble for a no-hit :) */
1606
#if 0
1607
        printk(KERN_INFO "ip_fw_demasq_gre(): ");
1608
        printk("Inbound from %s has no masq table entry.\n", in_ntoa(iph->saddr));
1609
#endif
1610
        return 0;
1611
}
1612
 
1613
#ifdef CONFIG_IP_MASQUERADE_PPTP_MULTICLIENT
1614
/*
1615
 *      Define all of the PPTP control channel message structures.
1616
 *      Sniff the control channel looking for start- and end-call
1617
 *      messages, and masquerade the Call ID as if it was a TCP
1618
 *      port.
1619
 */
1620
 
1621
#define PPTP_CONTROL_PACKET            1
1622
#define PPTP_MGMT_PACKET               2
1623
#define PPTP_MAGIC_COOKIE              0x1A2B3C4D
1624
 
1625
struct PptpPacketHeader {
1626
       __u16 packetLength;
1627
       __u16 packetType;
1628
       __u32 magicCookie;
1629
};
1630
 
1631
/* PptpControlMessageType values */
1632
#define PPTP_START_SESSION_REQUEST     1
1633
#define PPTP_START_SESSION_REPLY       2
1634
#define PPTP_STOP_SESSION_REQUEST      3
1635
#define PPTP_STOP_SESSION_REPLY        4
1636
#define PPTP_ECHO_REQUEST              5
1637
#define PPTP_ECHO_REPLY                6
1638
#define PPTP_OUT_CALL_REQUEST          7
1639
#define PPTP_OUT_CALL_REPLY            8
1640
#define PPTP_IN_CALL_REQUEST           9
1641
#define PPTP_IN_CALL_REPLY             10
1642
#define PPTP_CALL_CLEAR_REQUEST        11
1643
#define PPTP_CALL_DISCONNECT_NOTIFY    12
1644
#define PPTP_CALL_ERROR_NOTIFY         13
1645
#define PPTP_WAN_ERROR_NOTIFY          14
1646
#define PPTP_SET_LINK_INFO             15
1647
 
1648
struct PptpControlHeader {
1649
    __u16 messageType;
1650
    __u16 reserved;
1651
};
1652
 
1653
struct PptpOutCallRequest {
1654
    __u16 callID;
1655
    __u16 callSerialNumber;
1656
    __u32 minBPS;
1657
    __u32 maxBPS;
1658
    __u32 bearerType;
1659
    __u32 framingType;
1660
    __u16 packetWindow;
1661
    __u16 packetProcDelay;
1662
    __u16 reserved1;
1663
    __u16 phoneNumberLength;
1664
    __u16 reserved2;
1665
    __u8  phoneNumber[64];
1666
    __u8  subAddress[64];
1667
};
1668
 
1669
struct PptpOutCallReply {
1670
    __u16 callID;
1671
    __u16 peersCallID;
1672
    __u8  resultCode;
1673
    __u8  generalErrorCode;
1674
    __u16 causeCode;
1675
    __u32 connectSpeed;
1676
    __u16 packetWindow;
1677
    __u16 packetProcDelay;
1678
    __u32 physChannelID;
1679
};
1680
 
1681
struct PptpInCallRequest {
1682
    __u16 callID;
1683
    __u16 callSerialNumber;
1684
    __u32 callBearerType;
1685
    __u32 physChannelID;
1686
    __u16 dialedNumberLength;
1687
    __u16 dialingNumberLength;
1688
    __u8  dialedNumber[64];
1689
    __u8  dialingNumber[64];
1690
    __u8  subAddress[64];
1691
};
1692
 
1693
struct PptpInCallReply {
1694
    __u16 callID;
1695
    __u16 peersCallID;
1696
    __u8  resultCode;
1697
    __u8  generalErrorCode;
1698
    __u16 packetWindow;
1699
    __u16 packetProcDelay;
1700
    __u16 reserved;
1701
};
1702
 
1703
struct PptpCallDisconnectNotify {
1704
    __u16 callID;
1705
    __u8  resultCode;
1706
    __u8  generalErrorCode;
1707
    __u16 causeCode;
1708
    __u16 reserved;
1709
    __u8  callStatistics[128];
1710
};
1711
 
1712
struct PptpWanErrorNotify {
1713
    __u16 peersCallID;
1714
    __u16 reserved;
1715
    __u32 crcErrors;
1716
    __u32 framingErrors;
1717
    __u32 hardwareOverRuns;
1718
    __u32 bufferOverRuns;
1719
    __u32 timeoutErrors;
1720
    __u32 alignmentErrors;
1721
};
1722
 
1723
struct PptpSetLinkInfo {
1724
    __u16 peersCallID;
1725
    __u16 reserved;
1726
    __u32 sendAccm;
1727
    __u32 recvAccm;
1728
};
1729
 
1730
 
1731
/* Packet sent to or from PPTP control port. Process it. */
1732
/* Yes, all of this should be in a kernel module. Real Soon Now... */
1733
void ip_masq_pptp(struct sk_buff *skb, struct ip_masq *ms, struct device *dev)
1734
{
1735
        struct iphdr    *iph   = skb->h.iph;
1736
        struct PptpPacketHeader  *pptph = NULL;
1737
        struct PptpControlHeader *ctlh = NULL;
1738
        union {
1739
                char *req;
1740
                struct PptpOutCallRequest       *ocreq;
1741
                struct PptpOutCallReply         *ocack;
1742
                struct PptpInCallRequest        *icreq;
1743
                struct PptpInCallReply          *icack;
1744
                struct PptpCallDisconnectNotify *disc;
1745
                struct PptpWanErrorNotify       *wanerr;
1746
                struct PptpSetLinkInfo          *setlink;
1747
        } pptpReq;
1748
        struct ip_masq  *ms_gre = NULL;
1749
 
1750
        /*
1751
         * The GRE data channel will be treated as the "control channel"
1752
         * for the purposes of masq because there are keepalives happening
1753
         * on the control channel, whereas the data channel may be subject
1754
         * to relatively long periods of inactivity.
1755
         */
1756
 
1757
        pptph = (struct PptpPacketHeader *)&(((char *)iph)[sizeof(struct iphdr) + sizeof(struct tcphdr)]);
1758
#ifdef DEBUG_IP_MASQUERADE_PPTP_VERBOSE
1759
        printk(KERN_DEBUG "ip_masq_pptp(): ");
1760
        printk("LEN=%d TY=%d MC=%lX", ntohs(pptph->packetLength),
1761
                ntohs(pptph->packetType), ntohl(pptph->magicCookie));
1762
        printk(" from %s", in_ntoa(iph->saddr));
1763
        printk(" to %s\n", in_ntoa(iph->daddr));
1764
#endif /* DEBUG_IP_MASQUERADE_PPTP_VERBOSE */
1765
 
1766
        if (ntohs(pptph->packetType) == PPTP_CONTROL_PACKET &&
1767
            ntohl(pptph->magicCookie) == PPTP_MAGIC_COOKIE) {
1768
#ifdef DEBUG_IP_MASQUERADE_PPTP
1769
                printk(KERN_DEBUG "ip_masq_pptp(): ");
1770
                printk("PPTP control packet from %s", in_ntoa(iph->saddr));
1771
                printk(" to %s\n", in_ntoa(iph->daddr));
1772
#endif /* DEBUG_IP_MASQUERADE_PPTP */
1773
                ctlh = (struct PptpControlHeader *)&(((char*)pptph)[sizeof(struct PptpPacketHeader)]);
1774
                pptpReq.req = &(((char*)ctlh)[sizeof(struct PptpControlHeader)]);
1775
#ifdef DEBUG_IP_MASQUERADE_PPTP_VERBOSE
1776
                printk(KERN_DEBUG "ip_masq_pptp(): ");
1777
                printk("MTY=%X R0=%X\n",
1778
                        ntohs(ctlh->messageType), ctlh->reserved);
1779
#endif /* DEBUG_IP_MASQUERADE_PPTP_VERBOSE */
1780
 
1781
                switch (ntohs(ctlh->messageType))
1782
                {
1783
                        case PPTP_OUT_CALL_REQUEST:
1784
                                if (iph->daddr == ms->daddr)    /* outbound only */
1785
                                {
1786
#ifdef DEBUG_IP_MASQUERADE_PPTP
1787
                                        printk(KERN_DEBUG "ip_masq_pptp(): ");
1788
                                        printk("Call request, call ID %X\n",
1789
                                                ntohs(pptpReq.ocreq->callID));
1790
#endif /* DEBUG_IP_MASQUERADE_PPTP */
1791
                                        ms_gre = ip_masq_new(dev, IPPROTO_GRE,
1792
                                                ms->saddr, pptpReq.ocreq->callID,
1793
                                                ms->daddr, 0,
1794
                                                0);
1795
                                        if (ms_gre != NULL)
1796
                                        {
1797
                                                ms->control = ms_gre;
1798
                                                ms_gre->flags |= IP_MASQ_F_CONTROL;
1799
                                                ip_masq_set_expire(ms_gre, 0);
1800
                                                ip_masq_set_expire(ms_gre, 2*60*HZ);
1801
                                                pptpReq.ocreq->callID = ms_gre->mport;
1802
                                                printk(KERN_INFO "ip_masq_pptp(): ");
1803
                                                printk("Req outcall PPTP sess %s", in_ntoa(ms->saddr));
1804
                                                printk(" -> %s", in_ntoa(ms->daddr));
1805
                                                printk(" Call ID %X -> %X.\n", ntohs(ms_gre->sport), ntohs(ms_gre->mport));
1806
#ifdef DEBUG_IP_MASQUERADE_PPTP
1807
                                                printk(KERN_DEBUG "ip_masq_pptp(): ");
1808
                                                printk("masqed call ID %X\n",
1809
                                                        ntohs(pptpReq.ocreq->callID));
1810
#endif /* DEBUG_IP_MASQUERADE_PPTP */
1811
                                        } else {
1812
                                                printk(KERN_NOTICE "ip_masq_pptp(): ");
1813
                                                printk("Couldn't create GRE masq table entry (%s)\n", "OUT_CALL_REQ");
1814
                                        }
1815
                                }
1816
                        break;
1817
                        case PPTP_OUT_CALL_REPLY:
1818
                                if (iph->saddr == ms->daddr)    /* inbound (masqueraded client) */
1819
                                {
1820
#ifdef DEBUG_IP_MASQUERADE_PPTP
1821
                                        printk(KERN_DEBUG "ip_masq_pptp(): ");
1822
                                        printk("Call reply, peer call ID %X\n",
1823
                                                ntohs(pptpReq.ocack->peersCallID));
1824
#endif /* DEBUG_IP_MASQUERADE_PPTP */
1825
                                        ms_gre = ip_masq_getbym(IPPROTO_GRE,
1826
                                                ms->maddr, pptpReq.ocack->peersCallID);
1827
                                        if (ms_gre != NULL)
1828
                                        {
1829
                                                ip_masq_set_expire(ms_gre, 0);
1830
                                                ip_masq_set_expire(ms_gre, 2*60*HZ);
1831
                                                pptpReq.ocack->peersCallID = ms_gre->sport;
1832
                                                printk(KERN_INFO "ip_masq_pptp(): ");
1833
                                                printk("Estab outcall PPTP sess %s", in_ntoa(ms->saddr));
1834
                                                printk(" -> %s", in_ntoa(ms->daddr));
1835
                                                printk(" Call ID %X -> %X.\n", ntohs(ms_gre->sport), ntohs(ms_gre->mport));
1836
#ifdef DEBUG_IP_MASQUERADE_PPTP
1837
                                                printk(KERN_DEBUG "ip_masq_pptp(): ");
1838
                                                printk("unmasqed call ID %X\n",
1839
                                                        ntohs(pptpReq.ocack->callID));
1840
#endif /* DEBUG_IP_MASQUERADE_PPTP */
1841
                                        } else {
1842
                                                printk(KERN_INFO "ip_masq_pptp(): ");
1843
                                                printk("Lost GRE masq table entry (%s)\n", "OUT_CALL_REPLY");
1844
                                        }
1845
                                }
1846
                        break;
1847
                        case PPTP_IN_CALL_REQUEST:
1848
                                if (iph->daddr == ms->daddr)    /* outbound only */
1849
                                {
1850
#ifdef DEBUG_IP_MASQUERADE_PPTP
1851
                                        printk(KERN_DEBUG "ip_masq_pptp(): ");
1852
                                        printk("Call request, call ID %X\n",
1853
                                                ntohs(pptpReq.icreq->callID));
1854
#endif /* DEBUG_IP_MASQUERADE_PPTP */
1855
                                        ms_gre = ip_masq_new(dev, IPPROTO_GRE,
1856
                                                 ms->saddr, pptpReq.icreq->callID,
1857
                                                 ms->daddr, 0,
1858
                                                 0);
1859
                                        if (ms_gre != NULL)
1860
                                        {
1861
                                                ms->control = ms_gre;
1862
                                                ms_gre->flags |= IP_MASQ_F_CONTROL;
1863
                                                ip_masq_set_expire(ms_gre, 0);
1864
                                                ip_masq_set_expire(ms_gre, 2*60*HZ);
1865
                                                pptpReq.icreq->callID = ms_gre->mport;
1866
                                                printk(KERN_INFO "ip_masq_pptp(): ");
1867
                                                printk("Req incall PPTP sess %s", in_ntoa(ms->saddr));
1868
                                                printk(" -> %s", in_ntoa(ms->daddr));
1869
                                                printk(" Call ID %X -> %X.\n", ntohs(ms_gre->sport), ntohs(ms_gre->mport));
1870
#ifdef DEBUG_IP_MASQUERADE_PPTP
1871
                                                printk(KERN_DEBUG "ip_masq_pptp(): ");
1872
                                                printk("masqed call ID %X\n",
1873
                                                        ntohs(pptpReq.icreq->callID));
1874
#endif /* DEBUG_IP_MASQUERADE_PPTP */
1875
                                        } else {
1876
                                                printk(KERN_NOTICE "ip_masq_pptp(): ");
1877
                                                printk("Couldn't create GRE masq table entry (%s)\n", "IN_CALL_REQ");
1878
                                        }
1879
                                }
1880
                        break;
1881
                        case PPTP_IN_CALL_REPLY:
1882
                                if (iph->saddr == ms->daddr)    /* inbound (masqueraded client) */
1883
                                {
1884
#ifdef DEBUG_IP_MASQUERADE_PPTP
1885
                                        printk(KERN_DEBUG "ip_masq_pptp(): ");
1886
                                        printk("Call reply, peer call ID %X\n",
1887
                                                ntohs(pptpReq.icack->peersCallID));
1888
#endif /* DEBUG_IP_MASQUERADE_PPTP */
1889
                                        ms_gre = ip_masq_getbym(IPPROTO_GRE,
1890
                                                ms->maddr, pptpReq.icack->peersCallID);
1891
                                        if (ms_gre != NULL)
1892
                                        {
1893
                                                ip_masq_set_expire(ms_gre, 0);
1894
                                                ip_masq_set_expire(ms_gre, 2*60*HZ);
1895
                                                pptpReq.icack->peersCallID = ms_gre->sport;
1896
                                                printk(KERN_INFO "ip_masq_pptp(): ");
1897
                                                printk("Estab incall PPTP sess %s", in_ntoa(ms->saddr));
1898
                                                printk(" -> %s", in_ntoa(ms->daddr));
1899
                                                printk(" Call ID %X -> %X.\n", ntohs(ms_gre->sport), ntohs(ms_gre->mport));
1900
#ifdef DEBUG_IP_MASQUERADE_PPTP
1901
                                                printk(KERN_DEBUG "ip_masq_pptp(): ");
1902
                                                printk("unmasqed call ID %X\n",
1903
                                                        ntohs(pptpReq.icack->callID));
1904
#endif /* DEBUG_IP_MASQUERADE_PPTP */
1905
                                        } else {
1906
                                                printk(KERN_INFO "ip_masq_pptp(): ");
1907
                                                printk("Lost GRE masq table entry (%s)\n", "IN_CALL_REPLY");
1908
                                        }
1909
                                }
1910
                        break;
1911
                        case PPTP_CALL_DISCONNECT_NOTIFY:
1912
                                if (iph->daddr == ms->daddr)    /* outbound only */
1913
                                {
1914
#ifdef DEBUG_IP_MASQUERADE_PPTP
1915
                                        printk(KERN_DEBUG "ip_masq_pptp(): ");
1916
                                        printk("Disconnect notify, call ID %X\n",
1917
                                                ntohs(pptpReq.disc->callID));
1918
#endif /* DEBUG_IP_MASQUERADE_PPTP */
1919
                                        ms_gre = ip_masq_out_get_2(IPPROTO_GRE,
1920
                                                iph->saddr, pptpReq.disc->callID,
1921
                                                iph->daddr, 0);
1922
                                        if (ms_gre != NULL)
1923
                                        {
1924
                                                /*
1925
                                                 * expire the data channel
1926
                                                 * table entry quickly now.
1927
                                                 */
1928
                                                ip_masq_set_expire(ms_gre, 0);
1929
                                                ip_masq_set_expire(ms_gre, 30*HZ);
1930
                                                ms->control = NULL;
1931
                                                ms_gre->flags &= ~IP_MASQ_F_CONTROL;
1932
                                                pptpReq.disc->callID = ms_gre->mport;
1933
                                                printk(KERN_INFO "ip_masq_pptp(): ");
1934
                                                printk("Disconnect PPTP sess %s", in_ntoa(ms->saddr));
1935
                                                printk(" -> %s", in_ntoa(ms->daddr));
1936
                                                printk(" Call ID %X -> %X.\n", ntohs(ms_gre->sport), ntohs(ms_gre->mport));
1937
#ifdef DEBUG_IP_MASQUERADE_PPTP
1938
                                                printk(KERN_DEBUG "ip_masq_pptp(): ");
1939
                                                printk("masqed call ID %X\n",
1940
                                                        ntohs(pptpReq.disc->callID));
1941
#endif /* DEBUG_IP_MASQUERADE_PPTP */
1942
                                        }
1943
                                }
1944
                        break;
1945
                        case PPTP_WAN_ERROR_NOTIFY:
1946
                                if (iph->saddr == ms->daddr)    /* inbound only */
1947
                                {
1948
#ifdef DEBUG_IP_MASQUERADE_PPTP
1949
                                        printk(KERN_DEBUG "ip_masq_pptp(): ");
1950
                                        printk("Error notify, peer call ID %X\n",
1951
                                                ntohs(pptpReq.wanerr->peersCallID));
1952
#endif /* DEBUG_IP_MASQUERADE_PPTP */
1953
                                        ms_gre = ip_masq_getbym(IPPROTO_GRE,
1954
                                                ms->maddr, pptpReq.wanerr->peersCallID);
1955
                                        if (ms_gre != NULL)
1956
                                        {
1957
                                                pptpReq.wanerr->peersCallID = ms_gre->sport;
1958
#ifdef DEBUG_IP_MASQUERADE_PPTP
1959
                                                printk(KERN_DEBUG "ip_masq_pptp(): ");
1960
                                                printk("unmasqed call ID %X\n",
1961
                                                        ntohs(pptpReq.wanerr->peersCallID));
1962
#endif /* DEBUG_IP_MASQUERADE_PPTP */
1963
                                        } else {
1964
                                                printk(KERN_INFO "ip_masq_pptp(): ");
1965
                                                printk("Lost GRE masq table entry (%s)\n", "WAN_ERROR_NOTIFY");
1966
                                        }
1967
                                }
1968
                        break;
1969
                        case PPTP_SET_LINK_INFO:
1970
                                if (iph->saddr == ms->daddr)    /* inbound only */
1971
                                {
1972
#ifdef DEBUG_IP_MASQUERADE_PPTP
1973
                                        printk(KERN_DEBUG "ip_masq_pptp(): ");
1974
                                        printk("Set link info, peer call ID %X\n",
1975
                                                ntohs(pptpReq.setlink->peersCallID));
1976
#endif /* DEBUG_IP_MASQUERADE_PPTP */
1977
                                        ms_gre = ip_masq_getbym(IPPROTO_GRE,
1978
                                                ms->maddr, pptpReq.setlink->peersCallID);
1979
                                        if (ms_gre != NULL)
1980
                                        {
1981
                                                pptpReq.setlink->peersCallID = ms_gre->sport;
1982
#ifdef DEBUG_IP_MASQUERADE_PPTP
1983
                                                printk(KERN_DEBUG "ip_masq_pptp(): ");
1984
                                                printk("unmasqed call ID %X\n",
1985
                                                        ntohs(pptpReq.setlink->peersCallID));
1986
#endif /* DEBUG_IP_MASQUERADE_PPTP */
1987
                                        } else {
1988
                                                printk(KERN_INFO "ip_masq_pptp(): ");
1989
                                                printk("Lost GRE masq table entry (%s)\n", "SET_LINK_INFO");
1990
                                        }
1991
                                }
1992
                        break;
1993
                }
1994
        }
1995
}
1996
#endif /* CONFIG_IP_MASQUERADE_PPTP_MULTICLIENT */
1997
 
1998
static struct symbol_table pptp_masq_syms = {
1999
#include <linux/symtab_begin.h>
2000
        X(ip_fw_masq_gre),
2001
        X(ip_fw_demasq_gre),
2002
#ifdef CONFIG_IP_MASQUERADE_PPTP_MULTICLIENT
2003
        X(ip_masq_pptp),
2004
#endif /* CONFIG_IP_MASQUERADE_PPTP_MULTICLIENT */
2005
#include <linux/symtab_end.h>
2006
};
2007
 
2008
#endif /* CONFIG_IP_MASQUERADE_PPTP */
2009
 
2010
 
2011
#ifdef CONFIG_IP_MASQUERADE_IPSEC
2012
/*
2013
 *      Quick-and-dirty handling of ESP connections
2014
 *      John Hardin <jhardin@wolfenet.com> gets all blame...
2015
 */
2016
 
2017
/*
2018
 *      Handle outbound ESP packets.
2019
 *
2020
 *      This is largely a copy of ip_fw_masquerade()
2021
 *
2022
 * To associate inbound traffic with outbound traffic, we only
2023
 * allow one session per remote host to be negotiated at a time.
2024
 * If a packet comes in and there's no masq table entry for it,
2025
 * then check for other masq table entries for the same server
2026
 * with the inbound SPI set to zero (i.e. no response yet). If
2027
 * found, discard the packet.
2028
 * This will DoS the server for the duration of the connection
2029
 * attempt, so keep the masq entry's lifetime short until a
2030
 * response comes in.
2031
 * If multiple masqueraded hosts are in contention for the same
2032
 * remote host, enforce round-robin access. This may lead to
2033
 * misassociation of response traffic if the response is delayed
2034
 * a great deal, but the masqueraded hosts will clean that up
2035
 * if it happens.
2036
 */
2037
 
2038
int ip_fw_masq_esp(struct sk_buff **skb_p, struct device *dev)
2039
{
2040
        struct sk_buff  *skb   = *skb_p;
2041
        struct iphdr    *iph   = skb->h.iph;
2042
        struct ip_masq  *ms;
2043
        unsigned long    flags;
2044
        __u32 o_spi;
2045
        __u16 fake_sport;
2046
        unsigned long    timeout = MASQUERADE_EXPIRE_IPSEC;
2047
 
2048
        o_spi = *((__u32 *)&(((char *)iph)[iph->ihl*4]));
2049
        fake_sport = (__u16) ntohl(o_spi) & 0xffff;
2050
 
2051
#ifdef DEBUG_IP_MASQUERADE_IPSEC
2052
        printk(KERN_DEBUG "ip_fw_masq_esp(): ");
2053
        printk("pkt %s", in_ntoa(iph->saddr));
2054
        printk(" -> %s SPI %lX (fakeport %X)\n", in_ntoa(iph->daddr), ntohl(o_spi), fake_sport);
2055
#endif /* DEBUG_IP_MASQUERADE_IPSEC */
2056
 
2057
        if (o_spi == 0) {
2058
                /* illegal SPI - discard */
2059
                printk(KERN_INFO "ip_fw_masq_esp(): ");
2060
                printk("zero SPI from %s discarded\n", in_ntoa(iph->saddr));
2061
                return -1;
2062
        }
2063
 
2064
        /*
2065
         *      Look for masq table entry
2066
         */
2067
 
2068
        ms = ip_masq_out_get_ipsec(IPPROTO_ESP,
2069
                iph->saddr, fake_sport,
2070
                iph->daddr, 0,
2071
                o_spi);
2072
 
2073
        if (ms!=NULL) {
2074
                if (ms->ispi == IPSEC_INIT_SQUELCHED) {
2075
                        /* squelched: toss the packet without changing the timer */
2076
#ifdef DEBUG_IP_MASQUERADE_IPSEC
2077
                        printk(KERN_INFO "ip_fw_masq_esp(): ");
2078
                        printk("init %s ", in_ntoa(iph->saddr));
2079
                        printk("-> %s SPI %lX ", in_ntoa(iph->daddr), ntohl(o_spi));
2080
                        printk("squelched\n");
2081
#endif /* DEBUG_IP_MASQUERADE_IPSEC */
2082
                        return -1;
2083
                }
2084
 
2085
                /* delete the expiration timer */
2086
                ip_masq_set_expire(ms,0);
2087
 
2088
                /*
2089
                 *      Make sure that the masq IP address is correct
2090
                 *      for dynamic IP...
2091
                 */
2092
                if ( (ms->maddr != dev->pa_addr) && (sysctl_ip_dynaddr & 3) ) {
2093
                        printk(KERN_INFO "ip_fw_masq_esp(): ");
2094
                        printk("change maddr from %s", in_ntoa(ms->maddr));
2095
                        printk(" to %s\n", in_ntoa(dev->pa_addr));
2096
                        save_flags(flags);
2097
                        cli();
2098
                        ip_masq_unhash(ms);
2099
                        ms->maddr = dev->pa_addr;
2100
                        ip_masq_hash(ms);
2101
                        restore_flags(flags);
2102
                }
2103
 
2104
                if (ms->ispi == 0) {
2105
                        /* no response yet, keep timeout short */
2106
                        timeout = MASQUERADE_EXPIRE_IPSEC_INIT;
2107
                        if (ms->blocking) {
2108
                                /* prevent DoS: limit init packet timer resets */
2109
                                ms->ocnt++;
2110
        #ifdef DEBUG_IP_MASQUERADE_IPSEC
2111
                                printk(KERN_INFO "ip_fw_masq_esp(): ");
2112
                                printk("init %s ", in_ntoa(iph->saddr));
2113
                                printk("-> %s SPI %lX ", in_ntoa(iph->daddr), ntohl(o_spi));
2114
                                printk("retry %d\n", ms->ocnt);
2115
        #endif /* DEBUG_IP_MASQUERADE_IPSEC */
2116
                                if (ms->ocnt > IPSEC_INIT_RETRIES) {
2117
                                        /* more than IPSEC_INIT_RETRIES tries, give up */
2118
                                        printk(KERN_INFO "ip_fw_masq_esp(): ");
2119
                                        printk("init %s ", in_ntoa(iph->saddr));
2120
                                        printk("-> %s SPI %lX ", in_ntoa(iph->daddr), ntohl(o_spi));
2121
                                        printk("no response after %d tries, unblocking & squelching\n", ms->ocnt);
2122
                                        /* squelch that source+SPI for a bit */
2123
                                        timeout = 30*HZ;
2124
                                        save_flags(flags);
2125
                                        cli();
2126
                                        ip_masq_unhash(ms);
2127
                                        ms->ispi = IPSEC_INIT_SQUELCHED;
2128
                                        ms->dport = IPSEC_INIT_SQUELCHED;
2129
                                        ip_masq_hash(ms);
2130
                                        restore_flags(flags);
2131
                                        ip_masq_set_expire(ms, timeout);
2132
                                        /* toss the packet */
2133
                                        return -1;
2134
                                }
2135
                        }
2136
                }
2137
        } else {
2138
                /*
2139
                 *      Nope, not found, create a new entry for it, maybe
2140
                 */
2141
 
2142
                /* see if there are any pending inits with the same destination... */
2143
                ms = ip_masq_in_get_ipsec(IPPROTO_ESP,
2144
                        iph->daddr, 0,
2145
                        0, 0,
2146
                        0);
2147
 
2148
                if (ms != NULL) {
2149
                        /* found one with ispi == 0 */
2150
                        if (ms->saddr != iph->saddr) {
2151
                                /* it's not ours, don't step on their toes */
2152
                                printk(KERN_INFO "ip_fw_masq_esp(): ");
2153
                                printk("init %s ", in_ntoa(iph->saddr));
2154
                                printk("-> %s ", in_ntoa(iph->daddr));
2155
                                printk("temporarily blocked by pending ");
2156
                                printk("%s init\n", in_ntoa(ms->saddr));
2157
                                /* let it know it has competition */
2158
                                ms->blocking = 1;
2159
                                /* toss the packet */
2160
                                return -1;
2161
                        }
2162
                        if (ms->ospi != o_spi) {
2163
                                /* SPIs differ, still waiting for a previous attempt to expire */
2164
                                printk(KERN_INFO "ip_fw_masq_esp(): ");
2165
                                printk("init %s ", in_ntoa(iph->saddr));
2166
                                printk("-> %s SPI %lX ", in_ntoa(iph->daddr), ntohl(o_spi));
2167
                                printk("temporarily blocked by pending ");
2168
                                printk("init w/ SPI %lX\n", ntohl(ms->ospi));
2169
                                /* let it know it has competition */
2170
                                ms->blocking = 1;
2171
                                /* toss the packet */
2172
                                return -1;
2173
                        }
2174
                } else  /* nothing pending, make new entry, pending response */
2175
                        ms = ip_masq_new(dev, iph->protocol,
2176
                                 iph->saddr, fake_sport,
2177
                                 iph->daddr, 0,
2178
                                 0);
2179
 
2180
                if (ms == NULL) {
2181
                        printk(KERN_NOTICE "ip_fw_masq_esp(): Couldn't create masq table entry.\n");
2182
                        return -1;
2183
                }
2184
 
2185
                ms->blocking = ms->ocnt = 0;
2186
                ms->ospi = o_spi;
2187
                timeout = MASQUERADE_EXPIRE_IPSEC_INIT;      /* fairly brief timeout while waiting for a response */
2188
        }
2189
 
2190
        /*
2191
         *      Set iph source addr from ip_masq obj.
2192
         */
2193
        iph->saddr = ms->maddr;
2194
 
2195
        /*
2196
         *      set timeout and check IP header
2197
         */
2198
 
2199
        ip_masq_set_expire(ms, timeout);
2200
        ip_send_check(iph);
2201
 
2202
#ifdef DEBUG_IP_MASQUERADE_IPSEC_VERBOSE
2203
        printk(KERN_DEBUG "MASQ: ESP O-routed from %s over %s\n",
2204
                in_ntoa(ms->maddr), dev->name);
2205
#endif /* DEBUG_IP_MASQUERADE_IPSEC_VERBOSE */
2206
 
2207
        return 0;
2208
}
2209
 
2210
/*
2211
 *      Handle inbound ESP packets.
2212
 *
2213
 */
2214
 
2215
int ip_fw_demasq_esp(struct sk_buff **skb_p, struct device *dev)
2216
{
2217
        struct sk_buff  *skb   = *skb_p;
2218
        struct iphdr    *iph   = skb->h.iph;
2219
        struct ip_masq  *ms;
2220
        unsigned long   flags;
2221
        __u32 i_spi;
2222
        __u16 fake_sport;
2223
#ifndef CONFIG_IP_MASQUERADE_IPSEC_NOGUESS
2224
        #define ESP_GUESS_SZ 5                  /* minimum 3, please */
2225
        #define ESP_CAND_MIN_TM 5*60*HZ         /* max 10*60*HZ? */
2226
        unsigned        hash;
2227
        int             i, ii,
2228
                        ncand = 0, nguess = 0;
2229
        __u16           isakmp;
2230
        __u32           cand_ip,
2231
                        guess_ip[ESP_GUESS_SZ];
2232
        unsigned long   cand_tm,
2233
                        guess_tm[ESP_GUESS_SZ];
2234
        struct sk_buff  *skb_cl;
2235
        struct iphdr    *iph_cl;
2236
#endif /* CONFIG_IP_MASQUERADE_IPSEC_NOGUESS */
2237
 
2238
        i_spi = *((__u32 *)&(((char *)iph)[iph->ihl*4]));
2239
        fake_sport = (__u16) ntohl(i_spi) & 0xffff;
2240
 
2241
#ifdef DEBUG_IP_MASQUERADE_IPSEC
2242
        printk(KERN_DEBUG "ip_fw_demasq_esp(): ");
2243
        printk("pkt %s", in_ntoa(iph->saddr));
2244
        printk(" -> %s SPI %lX (fakeport %X)\n", in_ntoa(iph->daddr), ntohl(i_spi), fake_sport);
2245
#endif /* DEBUG_IP_MASQUERADE_IPSEC */
2246
 
2247
        if (i_spi == 0) {
2248
                /* illegal SPI - discard */
2249
                printk(KERN_INFO "ip_fw_demasq_esp(): ");
2250
                printk("zero SPI from %s discarded\n", in_ntoa(iph->saddr));
2251
                return -1;
2252
        }
2253
 
2254
        if (i_spi == IPSEC_INIT_SQUELCHED) {
2255
                /* Ack! This shouldn't happen! */
2256
                /* IPSEC_INIT_SQUELCHED is chosen to be a reserved value as of 4/99 */
2257
                printk(KERN_NOTICE "ip_fw_demasq_esp(): ");
2258
                printk("SPI from %s is IPSEC_INIT_SQUELCHED - modify ip_masq.c!\n", in_ntoa(iph->saddr));
2259
                return -1;
2260
        }
2261
 
2262
        /*
2263
         *      Look for a masq table entry and reroute if found
2264
         */
2265
 
2266
        ms = ip_masq_in_get_ipsec(IPPROTO_ESP,
2267
                iph->saddr, fake_sport,
2268
                iph->daddr, 0,
2269
                i_spi);
2270
 
2271
        if (ms != NULL)
2272
        {
2273
                /* delete the expiration timer */
2274
                ip_masq_set_expire(ms,0);
2275
 
2276
                iph->daddr = ms->saddr;
2277
 
2278
                if (ms->ispi == 0) {
2279
#ifdef DEBUG_IP_MASQUERADE_IPSEC
2280
                        printk(KERN_INFO "ip_fw_demasq_esp(): ");
2281
                        printk("resp from %s SPI %lX", in_ntoa(iph->saddr), ntohl(i_spi));
2282
                        printk(" routed to %s (SPI %lX)\n", in_ntoa(ms->saddr), ntohl(ms->ospi));
2283
#endif /* DEBUG_IP_MASQUERADE_IPSEC */
2284
                        save_flags(flags);
2285
                        cli();
2286
                        ip_masq_unhash(ms);
2287
                        ms->ispi = i_spi;
2288
                        ms->dport = fake_sport;
2289
                        ip_masq_hash(ms);
2290
                        restore_flags(flags);
2291
                }
2292
 
2293
                /*
2294
                 * resum checksums and set timeout
2295
                 */
2296
                ip_masq_set_expire(ms, MASQUERADE_EXPIRE_IPSEC);
2297
                ip_send_check(iph);
2298
 
2299
#ifdef DEBUG_IP_MASQUERADE_IPSEC_VERBOSE
2300
                printk(KERN_DEBUG "MASQ: ESP I-routed to %s\n", in_ntoa(iph->daddr));
2301
#endif /* DEBUG_IP_MASQUERADE_IPSEC_VERBOSE */
2302
                return 1;
2303
        }
2304
 
2305
#ifndef CONFIG_IP_MASQUERADE_IPSEC_NOGUESS
2306
        /* Guess who this packet is likely intended for:
2307
         * Scan the UDP masq table for local hosts that have communicated via
2308
         * ISAKMP with the host who sent this packet.
2309
         * Using an insertion sort with duplicate IP suppression, build a list
2310
         * of the ESP_GUESS_SZ most recent ISAKMP sessions (determined by
2311
         * sorting in decreasing order of timeout timer).
2312
         * Clone the original packet and send it to those hosts, but DON'T make
2313
         * a masq table entry, as we're only guessing. It is assumed that the correct
2314
         * host will respond to the traffic and that will create a masq table entry.
2315
         * To limit the list a bit, don't consider any ISAKMP masq entries with
2316
         * less than ESP_CAND_MIN_TM time to live. This should be some value less
2317
         * than the IPSEC table timeout or *all* entries will be ignored...
2318
         */
2319
 
2320
#ifdef DEBUG_IP_MASQUERADE_IPSEC_VERBOSE
2321
        printk(KERN_DEBUG "ip_fw_demasq_esp(): ");
2322
        printk("guessing from %s SPI %lX\n", in_ntoa(iph->saddr), ntohl(i_spi));
2323
#endif /* DEBUG_IP_MASQUERADE_IPSEC_VERBOSE */
2324
 
2325
        /* zero out the guess table */
2326
        for (i = 0;i < ESP_GUESS_SZ; i++) {
2327
                guess_ip[i] = 0;
2328
                guess_tm[i] = 0;
2329
        }
2330
 
2331
        /* scan ISAKMP sessions with the source host */
2332
        isakmp = htons(UDP_PORT_ISAKMP);
2333
        hash = ip_masq_hash_key(IPPROTO_UDP, iph->saddr, isakmp);
2334
        for(ms = ip_masq_d_tab[hash]; ms ; ms = ms->d_link) {
2335
                if (ms->protocol == IPPROTO_UDP &&
2336
                    ms->daddr == iph->saddr &&
2337
                    ms->sport == isakmp &&
2338
                    ms->dport == isakmp &&
2339
                    ms->mport == isakmp &&
2340
                    ms->ospi != 0) {
2341
                        /* a candidate... */
2342
                        ncand++;
2343
                        cand_ip = ms->saddr;
2344
                        cand_tm = ms->timer.expires - jiffies;
2345
#ifdef DEBUG_IP_MASQUERADE_IPSEC_VERBOSE
2346
                        printk(KERN_DEBUG "ip_fw_demasq_esp(): ");
2347
                        printk("cand %d: IP %s TM %ld\n", ncand, in_ntoa(cand_ip), cand_tm);
2348
#endif /* DEBUG_IP_MASQUERADE_IPSEC_VERBOSE */
2349
                        if (cand_tm > ESP_CAND_MIN_TM) {
2350
                                /* traffic is recent enough, add to list (maybe) */
2351
                                for (i = 0; i < ESP_GUESS_SZ; i++) {
2352
                                        if (cand_tm > guess_tm[i]) {
2353
                                                /* newer */
2354
                                                if (guess_ip[i] != 0 && cand_ip != guess_ip[i]) {
2355
                                                        /* newer and IP different - insert */
2356
                                                        if (i < (ESP_GUESS_SZ - 1)) {
2357
                                                                /* move entries down the list,
2358
                                                                 * find first entry after this slot
2359
                                                                 * where the IP is 0 (unused) or
2360
                                                                 * IP == candidate (older traffic, same host)
2361
                                                                 * rather than simply going to the end of the list,
2362
                                                                 * for efficiency (don't shift zeros) and
2363
                                                                 * duplicate IP suppression (don't keep older entries
2364
                                                                 * having the same IP)
2365
                                                                 */
2366
                                                                for (ii = i + 1; ii < (ESP_GUESS_SZ - 1); ii++) {
2367
                                                                        if (guess_ip[ii] == 0 || guess_ip[ii] == cand_ip)
2368
                                                                                break;
2369
                                                                }
2370
                                                                for (ii-- ; ii >= i; ii--) {
2371
                                                                        guess_ip[ii+1] = guess_ip[ii];
2372
                                                                        guess_tm[ii+1] = guess_tm[ii];
2373
                                                                }
2374
                                                        }
2375
                                                }
2376
                                                guess_ip[i] = cand_ip;
2377
                                                guess_tm[i] = cand_tm;
2378
                                                break;
2379
                                        }
2380
                                        if (cand_ip == guess_ip[i]) {
2381
                                                /* fresher entry already there */
2382
                                                break;
2383
                                        }
2384
                                }
2385
                        }
2386
                }
2387
        }
2388
 
2389
        if (guess_ip[0]) {
2390
                /* had guesses - send */
2391
                if (guess_ip[1]) {
2392
                        /* multiple guesses, send a copy to all */
2393
                        for (i = 0; guess_ip[i] != 0; i++) {
2394
                                nguess++;
2395
#ifdef DEBUG_IP_MASQUERADE_IPSEC_VERBOSE
2396
                                printk(KERN_DEBUG "ip_fw_demasq_esp(): ");
2397
                                printk("guess %d: IP %s TM %ld\n", nguess, in_ntoa(guess_ip[i]), guess_tm[i]);
2398
#endif /* DEBUG_IP_MASQUERADE_IPSEC_VERBOSE */
2399
                                /* duplicate and send the skb */
2400
                                if ((skb_cl = skb_copy(skb, GFP_ATOMIC)) == NULL) {
2401
                                        printk(KERN_INFO "ip_fw_demasq_esp(): ");
2402
                                        printk("guessing: cannot copy skb\n");
2403
                                } else {
2404
                                        iph_cl = skb_cl->h.iph;
2405
                                        iph_cl->daddr = guess_ip[i];
2406
                                        ip_send_check(iph_cl);
2407
                                        ip_forward(skb_cl, dev, IPFWD_MASQUERADED, iph_cl->daddr);
2408
                                        kfree_skb(skb_cl, FREE_WRITE);
2409
                                }
2410
                        }
2411
#ifdef DEBUG_IP_MASQUERADE_IPSEC
2412
                        printk(KERN_INFO "ip_fw_demasq_esp(): ");
2413
                        printk("guessing from %s SPI %lX sent to", in_ntoa(iph->saddr), ntohl(i_spi));
2414
                        printk(" %d hosts (%d cand)\n", nguess, ncand);
2415
#endif /* DEBUG_IP_MASQUERADE_IPSEC */
2416
                        return -1;      /* discard original packet */
2417
                } else {
2418
                        /* only one guess, send original packet to that host */
2419
                        iph->daddr = guess_ip[0];
2420
                        ip_send_check(iph);
2421
 
2422
#ifdef DEBUG_IP_MASQUERADE_IPSEC
2423
                        printk(KERN_INFO "ip_fw_demasq_esp(): ");
2424
                        printk("guessing from %s SPI %lX sent to", in_ntoa(iph->saddr), ntohl(i_spi));
2425
                        printk(" %s (%d cand)\n", in_ntoa(guess_ip[0]), ncand);
2426
#endif /* DEBUG_IP_MASQUERADE_IPSEC */
2427
                        return 1;
2428
                }
2429
        }
2430
#endif /* CONFIG_IP_MASQUERADE_IPSEC_NOGUESS */
2431
 
2432
        /* sorry, all this trouble for a no-hit :) */
2433
#if 0
2434
        printk(KERN_INFO "ip_fw_demasq_esp(): ");
2435
        printk("Inbound from %s SPI %lX has no masq table entry.\n", in_ntoa(iph->saddr), ntohl(i_spi));
2436
#endif
2437
        return 0;
2438
}
2439
 
2440
static struct symbol_table ipsec_masq_syms = {
2441
#include <linux/symtab_begin.h>
2442
        X(ip_masq_out_get_ipsec),
2443
        X(ip_masq_in_get_ipsec),
2444
        X(ip_masq_out_get_isakmp),
2445
        X(ip_masq_in_get_isakmp),
2446
        X(ip_fw_masq_esp),
2447
        X(ip_fw_demasq_esp),
2448
#include <linux/symtab_end.h>
2449
};
2450
 
2451
#endif /* CONFIG_IP_MASQUERADE_IPSEC */
2452
 
2453
 
2454
int ip_fw_masquerade(struct sk_buff **skb_ptr, struct device *dev)
2455
{
2456
        struct sk_buff  *skb=*skb_ptr;
2457
        struct iphdr    *iph = skb->h.iph;
2458
        __u16   *portptr;
2459
        struct ip_masq  *ms;
2460
        int             size;
2461
        unsigned long   timeout;
2462
 
2463
        /*
2464
         * We can only masquerade protocols with ports...
2465
         * [TODO]
2466
         * We may need to consider masq-ing some ICMP related to masq-ed protocols
2467
         */
2468
 
2469
        if (iph->protocol==IPPROTO_ICMP)
2470
            return (ip_fw_masq_icmp(skb_ptr,dev));
2471
#ifdef CONFIG_IP_MASQUERADE_PPTP
2472
        if (iph->protocol==IPPROTO_GRE)
2473
            return (ip_fw_masq_gre(skb_ptr,dev));
2474
#endif /* CONFIG_IP_MASQUERADE_PPTP */
2475
#ifdef CONFIG_IP_MASQUERADE_IPSEC
2476
        if (iph->protocol==IPPROTO_ESP)
2477
            return (ip_fw_masq_esp(skb_ptr,dev));
2478
#endif /* CONFIG_IP_MASQUERADE_IPSEC */
2479
        if (iph->protocol!=IPPROTO_UDP && iph->protocol!=IPPROTO_TCP)
2480
                return -1;
2481
 
2482
        /*
2483
         *      Now hunt the list to see if we have an old entry
2484
         */
2485
 
2486
        portptr = (__u16 *)&(((char *)iph)[iph->ihl*4]);
2487
#ifdef DEBUG_CONFIG_IP_MASQUERADE
2488
        printk("Outgoing %s %lX:%X -> %lX:%X\n",
2489
                masq_proto_name(iph->protocol),
2490
                ntohl(iph->saddr), ntohs(portptr[0]),
2491
                ntohl(iph->daddr), ntohs(portptr[1]));
2492
#endif
2493
 
2494
        ms = ip_masq_out_get(iph);
2495
 
2496
        if (ms!=NULL) {
2497
                ip_masq_set_expire(ms,0);
2498
 
2499
                /*
2500
                 *      If sysctl & 3 and either
2501
                 *        no pkt has been received yet
2502
                 *      or
2503
                 *        sysctl & 4
2504
                 *      in this tunnel ...
2505
                 *       "You are welcome, diald, ipppd, pppd-3.3...".
2506
                 */
2507
                if ( (sysctl_ip_dynaddr & 3) && (ms->flags & IP_MASQ_F_NO_REPLY || sysctl_ip_dynaddr & 4) && dev->pa_addr != ms->maddr) {
2508
                        unsigned long flags;
2509
                        if (sysctl_ip_dynaddr & 2) {
2510
                                printk(KERN_INFO "ip_fw_masquerade(): change maddr from %s",
2511
                                       in_ntoa(ms->maddr));
2512
                                printk(" to %s\n", in_ntoa(dev->pa_addr));
2513
                        }
2514
                        save_flags(flags);
2515
                        cli();
2516
                        ip_masq_unhash(ms);
2517
                        ms->maddr = dev->pa_addr;
2518
                        ip_masq_hash(ms);
2519
                        restore_flags(flags);
2520
                }
2521
 
2522
                /*
2523
                 *      Set sport if not defined yet (e.g. ftp PASV).  Because
2524
                 *      masq entries are hashed on sport, unhash with old value
2525
                 *      and hash with new.
2526
                 */
2527
 
2528
                if ( ms->flags & IP_MASQ_F_NO_SPORT && ms->protocol == IPPROTO_TCP ) {
2529
                        unsigned long flags;
2530
                        ms->flags &= ~IP_MASQ_F_NO_SPORT;
2531
                        save_flags(flags);
2532
                        cli();
2533
                        ip_masq_unhash(ms);
2534
                        ms->sport = portptr[0];
2535
                        ip_masq_hash(ms);       /* hash on new sport */
2536
                        restore_flags(flags);
2537
#ifdef DEBUG_CONFIG_IP_MASQUERADE
2538
                        printk("ip_fw_masquerade(): filled sport=%d\n",
2539
                               ntohs(ms->sport));
2540
#endif
2541
                }
2542
        }
2543
 
2544
#ifdef CONFIG_IP_MASQUERADE_IPAUTOFW
2545
        /* update any ipautofw entries .. */
2546
        ip_autofw_update_out(iph->saddr, iph->daddr, portptr[1],
2547
                             iph->protocol);
2548
#endif /* CONFIG_IP_MASQUERADE_IPAUTOFW */
2549
 
2550
        /*
2551
         *      Nope, not found, create a new entry for it
2552
         */
2553
        if (ms==NULL)
2554
        {
2555
#ifdef CONFIG_IP_MASQUERADE_IPAUTOFW
2556
                /* if the source port is supposed to match the masq port, then
2557
                   make it so */
2558
                if (ip_autofw_check_direct(portptr[1],iph->protocol))
2559
                        ms = ip_masq_new_enh(dev, iph->protocol,
2560
                                         iph->saddr, portptr[0],
2561
                                         iph->daddr, portptr[1],
2562
                                         0,
2563
                                         portptr[0]);
2564
                else
2565
#endif /* CONFIG_IP_MASQUERADE_IPAUTOFW */
2566
                        ms = ip_masq_new_enh(dev, iph->protocol,
2567
                                         iph->saddr, portptr[0],
2568
                                         iph->daddr, portptr[1],
2569
                                         0,
2570
                                         0);
2571
                if (ms == NULL)
2572
                        return -1;
2573
 
2574
#ifdef CONFIG_IP_MASQUERADE_IPSEC
2575
                if (iph->protocol == IPPROTO_UDP && ntohs(portptr[0]) == UDP_PORT_ISAKMP && ntohs(portptr[1]) == UDP_PORT_ISAKMP) {
2576
                        /* save the initiator cookie */
2577
                        ms->ospi = *((__u32 *)&portptr[4]);
2578
                }
2579
#endif /* CONFIG_IP_MASQUERADE_IPSEC */
2580
        }
2581
 
2582
#ifdef CONFIG_IP_MASQUERADE_PPTP
2583
#ifdef CONFIG_IP_MASQUERADE_PPTP_MULTICLIENT
2584
        if (iph->protocol == IPPROTO_TCP && ntohs(portptr[1]) == PPTP_CONTROL_PORT)
2585
        {
2586
                /*
2587
                 * Packet sent to PPTP control port. Process it.
2588
                 * May change call ID word in request, but
2589
                 * packet length will not change.
2590
                 */
2591
                ip_masq_pptp(skb, ms, dev);
2592
        }
2593
#endif /* CONFIG_IP_MASQUERADE_PPTP_MULTICLIENT */
2594
#endif /* CONFIG_IP_MASQUERADE_PPTP */
2595
 
2596
        /*
2597
         *      Change the fragments origin
2598
         */
2599
 
2600
        size = skb->len - ((unsigned char *)portptr - skb->h.raw);
2601
        /*
2602
         *      Set iph addr and port from ip_masq obj.
2603
         */
2604
        iph->saddr = ms->maddr;
2605
        portptr[0] = ms->mport;
2606
 
2607
        /*
2608
         *      Attempt ip_masq_app call.
2609
         *      will fix ip_masq and iph seq stuff
2610
         */
2611
        if (ip_masq_app_pkt_out(ms, skb_ptr, dev) != 0)
2612
        {
2613
                /*
2614
                 *      skb has possibly changed, update pointers.
2615
                 */
2616
                skb = *skb_ptr;
2617
                iph = skb->h.iph;
2618
                portptr = (__u16 *)&(((char *)iph)[iph->ihl*4]);
2619
                size = skb->len - ((unsigned char *)portptr-skb->h.raw);
2620
        }
2621
 
2622
        /*
2623
         *      Adjust packet accordingly to protocol
2624
         */
2625
 
2626
        if (masq_proto_num(iph->protocol)==0)
2627
        {
2628
#ifdef CONFIG_IP_MASQUERADE_IPSEC
2629
                if (iph->protocol == IPPROTO_UDP && ntohs(portptr[0]) == UDP_PORT_ISAKMP && ntohs(portptr[1]) == UDP_PORT_ISAKMP) {
2630
                        /* ISAKMP timeout should be same as ESP timeout to allow for rekeying */
2631
                        timeout = MASQUERADE_EXPIRE_IPSEC;
2632
                } else
2633
#endif /* CONFIG_IP_MASQUERADE_IPSEC */
2634
 
2635
                timeout = ip_masq_expire->udp_timeout;
2636
                recalc_check((struct udphdr *)portptr,iph->saddr,iph->daddr,size);
2637
        }
2638
        else
2639
        {
2640
                struct tcphdr *th;
2641
                th = (struct tcphdr *)portptr;
2642
 
2643
                /* Set the flags up correctly... */
2644
                if (th->fin)
2645
                {
2646
                        ms->flags |= IP_MASQ_F_SAW_FIN_OUT;
2647
                }
2648
 
2649
                if (th->rst)
2650
                {
2651
                        ms->flags |= IP_MASQ_F_SAW_RST;
2652
                }
2653
 
2654
                /*
2655
                 *      Timeout depends if FIN packet has been seen
2656
                 *      Very short timeout if RST packet seen.
2657
                 */
2658
                if (ms->flags & IP_MASQ_F_SAW_RST)
2659
                {
2660
                        timeout = 1;
2661
                }
2662
                else if ((ms->flags & IP_MASQ_F_SAW_FIN) == IP_MASQ_F_SAW_FIN)
2663
                {
2664
                        timeout = ip_masq_expire->tcp_fin_timeout;
2665
                }
2666
                else timeout = ip_masq_expire->tcp_timeout;
2667
 
2668
                skb->csum = csum_partial((void *)(th + 1), size - sizeof(*th), 0);
2669
                tcp_send_check(th,iph->saddr,iph->daddr,size,skb);
2670
        }
2671
        ip_masq_set_expire(ms, timeout);
2672
        ip_send_check(iph);
2673
 
2674
 #ifdef DEBUG_CONFIG_IP_MASQUERADE
2675
        printk("O-routed from %lX:%X over %s\n",ntohl(ms->maddr),ntohs(ms->mport),dev->name);
2676
 #endif
2677
 
2678
        return 0;
2679
 }
2680
 
2681
 
2682
/*
2683
 *      Handle ICMP messages in forward direction.
2684
 *      Find any that might be relevant, check against existing connections,
2685
 *      forward to masqueraded host if relevant.
2686
 *      Currently handles error types - unreachable, quench, ttl exceeded
2687
 */
2688
 
2689
int ip_fw_masq_icmp(struct sk_buff **skb_p, struct device *dev)
2690
{
2691
        struct sk_buff  *skb   = *skb_p;
2692
        struct iphdr    *iph   = skb->h.iph;
2693
        struct icmphdr  *icmph = (struct icmphdr *)((char *)iph + (iph->ihl<<2));
2694
        struct iphdr    *ciph;  /* The ip header contained within the ICMP */
2695
        __u16           *pptr;  /* port numbers from TCP/UDP contained header */
2696
        struct ip_masq  *ms;
2697
        unsigned short   len   = ntohs(iph->tot_len) - (iph->ihl * 4);
2698
 
2699
#ifdef DEBUG_CONFIG_IP_MASQUERADE_ICMP
2700
        printk("Incoming forward ICMP (%d,%d) %lX -> %lX\n",
2701
                icmph->type, ntohs(icmp_id(icmph)),
2702
                ntohl(iph->saddr), ntohl(iph->daddr));
2703
#endif
2704
 
2705
#ifdef CONFIG_IP_MASQUERADE_ICMP                
2706
        if ((icmph->type == ICMP_ECHO ) ||
2707
            (icmph->type == ICMP_TIMESTAMP ) ||
2708
            (icmph->type == ICMP_INFO_REQUEST ) ||
2709
            (icmph->type == ICMP_ADDRESS )) {
2710
#ifdef DEBUG_CONFIG_IP_MASQUERADE_ICMP
2711
                printk("MASQ: icmp request rcv %lX->%lX id %d type %d\n",
2712
                       ntohl(iph->saddr),
2713
                       ntohl(iph->daddr),
2714
                       ntohs(icmp_id(icmph)),
2715
                       icmph->type);
2716
#endif
2717
                ms = ip_masq_out_get_2(iph->protocol,
2718
                                       iph->saddr,
2719
                                       icmp_id(icmph),
2720
                                       iph->daddr,
2721
                                       icmp_hv_req(icmph));
2722
                if (ms == NULL) {
2723
                        ms = ip_masq_new(dev,
2724
                                         iph->protocol,
2725
                                         iph->saddr,
2726
                                         icmp_id(icmph),
2727
                                         iph->daddr,
2728
                                         icmp_hv_req(icmph),
2729
                                         0);
2730
                        if (ms == NULL)
2731
                                return (-1);
2732
#ifdef DEBUG_CONFIG_IP_MASQUERADE_ICMP
2733
                        printk("MASQ: Create new icmp entry\n");
2734
#endif                
2735
                }
2736
                ip_masq_set_expire(ms, 0);
2737
                /* Rewrite source address */
2738
 
2739
                /*
2740
                 *      If sysctl & 3 and either
2741
                 *        no pkt has been received yet
2742
                 *      or
2743
                 *        sysctl & 4
2744
                 *      in this tunnel ...
2745
                 *       "You are welcome, diald, ipppd, pppd-3.3...".
2746
                 */
2747
                if ( (sysctl_ip_dynaddr & 3) && (ms->flags & IP_MASQ_F_NO_REPLY || sysctl_ip_dynaddr & 4) && dev->pa_addr != ms->maddr) {
2748
                        unsigned long flags;
2749
#ifdef DEBUG_CONFIG_IP_MASQUERADE
2750
                        printk(KERN_INFO "ip_fw_masq_icmp(): change masq.addr %s",
2751
                               in_ntoa(ms->maddr));
2752
                        printk("-> %s\n", in_ntoa(dev->pa_addr));
2753
#endif
2754
                        save_flags(flags);
2755
                        cli();
2756
                        ip_masq_unhash(ms);
2757
                        ms->maddr = dev->pa_addr;
2758
                        ip_masq_hash(ms);
2759
                        restore_flags(flags);
2760
                }
2761
 
2762
                iph->saddr = ms->maddr;
2763
                ip_send_check(iph);
2764
                /* Rewrite port (id) */
2765
                (icmph->un).echo.id = ms->mport;
2766
                icmph->checksum = 0;
2767
                icmph->checksum = ip_compute_csum((unsigned char *)icmph, len);
2768
                ip_masq_set_expire(ms, MASQUERADE_EXPIRE_ICMP);
2769
#ifdef DEBUG_CONFIG_IP_MASQUERADE_ICMP
2770
                printk("MASQ: icmp request rwt %lX->%lX id %d type %d\n",
2771
                       ntohl(iph->saddr),
2772
                       ntohl(iph->daddr),
2773
                       ntohs(icmp_id(icmph)),
2774
                       icmph->type);
2775
#endif
2776
                return (1);
2777
        }
2778
#endif
2779
 
2780
        /*
2781
         * Work through seeing if this is for us.
2782
         * These checks are supposed to be in an order that
2783
         * means easy things are checked first to speed up
2784
         * processing.... however this means that some
2785
         * packets will manage to get a long way down this
2786
         * stack and then be rejected, but thats life
2787
         */
2788
        if ((icmph->type != ICMP_DEST_UNREACH) &&
2789
            (icmph->type != ICMP_SOURCE_QUENCH) &&
2790
            (icmph->type != ICMP_TIME_EXCEEDED))
2791
                return 0;
2792
 
2793
        /* Now find the contained IP header */
2794
        ciph = (struct iphdr *) (icmph + 1);
2795
 
2796
#ifdef CONFIG_IP_MASQUERADE_ICMP
2797
        if (ciph->protocol == IPPROTO_ICMP) {
2798
                /*
2799
                 * This section handles ICMP errors for ICMP packets
2800
                 */
2801
                struct icmphdr  *cicmph = (struct icmphdr *)((char *)ciph +
2802
                                                             (ciph->ihl<<2));
2803
 
2804
#ifdef DEBUG_CONFIG_IP_MASQUERADE_ICMP
2805
                printk("MASQ: fw icmp/icmp rcv %lX->%lX id %d type %d\n",
2806
                       ntohl(ciph->saddr),
2807
                       ntohl(ciph->daddr),
2808
                       ntohs(icmp_id(cicmph)),
2809
                       cicmph->type);
2810
#endif
2811
                ms = ip_masq_out_get_2(ciph->protocol,
2812
                                      ciph->daddr,
2813
                                      icmp_id(cicmph),
2814
                                      ciph->saddr,
2815
                                      icmp_hv_rep(cicmph));
2816
 
2817
                if (ms == NULL)
2818
                        return 0;
2819
 
2820
                /* Now we do real damage to this packet...! */
2821
                /* First change the source IP address, and recalc checksum */
2822
                iph->saddr = ms->maddr;
2823
                ip_send_check(iph);
2824
 
2825
                /* Now change the *dest* address in the contained IP */
2826
                ciph->daddr = ms->maddr;
2827
                ip_send_check(ciph);
2828
 
2829
                /* Change the ID to the masqed one! */
2830
                (cicmph->un).echo.id = ms->mport;
2831
 
2832
                /* And finally the ICMP checksum */
2833
                icmph->checksum = 0;
2834
                icmph->checksum = ip_compute_csum((unsigned char *) icmph, len);
2835
 
2836
#ifdef DEBUG_CONFIG_IP_MASQUERADE_ICMP
2837
                printk("MASQ: fw icmp/icmp rwt %lX->%lX id %d type %d\n",
2838
                       ntohl(ciph->saddr),
2839
                       ntohl(ciph->daddr),
2840
                       ntohs(icmp_id(cicmph)),
2841
                       cicmph->type);
2842
#endif
2843
                return 1;
2844
        }
2845
#endif /* CONFIG_IP_MASQUERADE_ICMP */
2846
 
2847
        /* We are only interested ICMPs generated from TCP or UDP packets */
2848
        if ((ciph->protocol != IPPROTO_UDP) && (ciph->protocol != IPPROTO_TCP))
2849
                return 0;
2850
 
2851
        /*
2852
         * Find the ports involved - this packet was
2853
         * incoming so the ports are right way round
2854
         * (but reversed relative to outer IP header!)
2855
         */
2856
        pptr = (__u16 *)&(((char *)ciph)[ciph->ihl*4]);
2857
 
2858
        /* Ensure the checksum is correct */
2859
        if (ip_compute_csum((unsigned char *) icmph, len))
2860
        {
2861
                /* Failed checksum! */
2862
                printk(KERN_DEBUG "MASQ: forward ICMP: failed checksum from %s!\n",
2863
                       in_ntoa(iph->saddr));
2864
                return(-1);
2865
        }
2866
 
2867
#ifdef DEBUG_CONFIG_IP_MASQUERADE
2868
        printk("Handling forward ICMP for %lX:%X -> %lX:%X\n",
2869
               ntohl(ciph->saddr), ntohs(pptr[0]),
2870
               ntohl(ciph->daddr), ntohs(pptr[1]));
2871
#endif
2872
 
2873
        /* This is pretty much what ip_masq_out_get() does */
2874
        ms = ip_masq_out_get_2(ciph->protocol,
2875
                               ciph->daddr,
2876
                               pptr[1],
2877
                               ciph->saddr,
2878
                               pptr[0]);
2879
 
2880
        if (ms == NULL)
2881
                return 0;
2882
 
2883
        /* Now we do real damage to this packet...! */
2884
        /* First change the source IP address, and recalc checksum */
2885
        iph->saddr = ms->maddr;
2886
        ip_send_check(iph);
2887
 
2888
        /* Now change the *dest* address in the contained IP */
2889
        ciph->daddr = ms->maddr;
2890
        ip_send_check(ciph);
2891
 
2892
        /* the TCP/UDP dest port - cannot redo check */
2893
        pptr[1] = ms->mport;
2894
 
2895
        /* And finally the ICMP checksum */
2896
        icmph->checksum = 0;
2897
        icmph->checksum = ip_compute_csum((unsigned char *) icmph, len);
2898
 
2899
#ifdef DEBUG_CONFIG_IP_MASQUERADE
2900
        printk("Rewrote forward ICMP to %lX:%X -> %lX:%X\n",
2901
               ntohl(ciph->saddr), ntohs(pptr[0]),
2902
               ntohl(ciph->daddr), ntohs(pptr[1]));
2903
#endif
2904
 
2905
        return 1;
2906
}
2907
 
2908
/*
2909
 *      Handle ICMP messages in reverse (demasquerade) direction.
2910
 *      Find any that might be relevant, check against existing connections,
2911
 *      forward to masqueraded host if relevant.
2912
 *      Currently handles error types - unreachable, quench, ttl exceeded
2913
 */
2914
 
2915
int ip_fw_demasq_icmp(struct sk_buff **skb_p, struct device *dev)
2916
{
2917
        struct sk_buff  *skb   = *skb_p;
2918
        struct iphdr    *iph   = skb->h.iph;
2919
        struct icmphdr  *icmph = (struct icmphdr *)((char *)iph + (iph->ihl<<2));
2920
        struct iphdr    *ciph;  /* The ip header contained within the ICMP */
2921
        __u16           *pptr;  /* port numbers from TCP/UDP contained header */
2922
        struct ip_masq  *ms;
2923
        unsigned short   len   = ntohs(iph->tot_len) - (iph->ihl * 4);
2924
 
2925
#ifdef DEBUG_CONFIG_IP_MASQUERADE_ICMP
2926
        printk("MASQ: icmp in/rev (%d,%d) %lX -> %lX\n",
2927
                icmph->type, ntohs(icmp_id(icmph)),
2928
                ntohl(iph->saddr), ntohl(iph->daddr));
2929
#endif
2930
 
2931
#ifdef CONFIG_IP_MASQUERADE_ICMP                
2932
        if ((icmph->type == ICMP_ECHOREPLY) ||
2933
            (icmph->type == ICMP_TIMESTAMPREPLY) ||
2934
            (icmph->type == ICMP_INFO_REPLY) ||
2935
            (icmph->type == ICMP_ADDRESSREPLY)) {
2936
#ifdef DEBUG_CONFIG_IP_MASQUERADE_ICMP
2937
                printk("MASQ: icmp reply rcv %lX->%lX id %d type %d, req %d\n",
2938
                       ntohl(iph->saddr),
2939
                       ntohl(iph->daddr),
2940
                       ntohs(icmp_id(icmph)),
2941
                       icmph->type,
2942
                       icmp_type_request(icmph->type));
2943
#endif
2944
                ms = ip_masq_in_get_2(iph->protocol,
2945
                                      iph->saddr,
2946
                                      icmp_hv_rep(icmph),
2947
                                      iph->daddr,
2948
                                      icmp_id(icmph));
2949
                if (ms == NULL)
2950
                        return 0;
2951
 
2952
                ip_masq_set_expire(ms,0);
2953
 
2954
                /*
2955
                 *      got reply, so clear flag
2956
                 */
2957
                ms->flags &= ~IP_MASQ_F_NO_REPLY;
2958
 
2959
                /* Reset source address */
2960
                iph->daddr = ms->saddr;
2961
                /* Redo IP header checksum */
2962
                ip_send_check(iph);
2963
                /* Set ID to fake port number */
2964
                (icmph->un).echo.id = ms->sport;
2965
                /* Reset ICMP checksum and set expiry */
2966
                icmph->checksum=0;
2967
                icmph->checksum=ip_compute_csum((unsigned char *)icmph,len);
2968
                ip_masq_set_expire(ms, MASQUERADE_EXPIRE_ICMP);
2969
#ifdef DEBUG_CONFIG_IP_MASQUERADE_ICMP
2970
                printk("MASQ: icmp reply rwt %lX->%lX id %d type %d\n",
2971
                       ntohl(iph->saddr),
2972
                       ntohl(iph->daddr),
2973
                       ntohs(icmp_id(icmph)),
2974
                       icmph->type);
2975
#endif
2976
                return 1;
2977
        } else {
2978
#endif
2979
                if ((icmph->type != ICMP_DEST_UNREACH) &&
2980
                    (icmph->type != ICMP_SOURCE_QUENCH) &&
2981
                    (icmph->type != ICMP_TIME_EXCEEDED))
2982
                        return 0;
2983
#ifdef CONFIG_IP_MASQUERADE_ICMP
2984
        }
2985
#endif
2986
        /*
2987
         * If we get here we have an ICMP error of one of the above 3 types
2988
         * Now find the contained IP header
2989
         */
2990
        ciph = (struct iphdr *) (icmph + 1);
2991
 
2992
#ifdef CONFIG_IP_MASQUERADE_ICMP
2993
        if (ciph->protocol == IPPROTO_ICMP) {
2994
                /*
2995
                 * This section handles ICMP errors for ICMP packets
2996
                 *
2997
                 * First get a new ICMP header structure out of the IP packet
2998
                 */
2999
                struct icmphdr  *cicmph = (struct icmphdr *)((char *)ciph +
3000
                                                             (ciph->ihl<<2));
3001
 
3002
#ifdef DEBUG_CONFIG_IP_MASQUERADE_ICMP
3003
                printk("MASQ: rv icmp/icmp rcv %lX->%lX id %d type %d\n",
3004
                       ntohl(ciph->saddr),
3005
                       ntohl(ciph->daddr),
3006
                       ntohs(icmp_id(cicmph)),
3007
                       cicmph->type);
3008
#endif
3009
                ms = ip_masq_in_get_2(ciph->protocol,
3010
                                      ciph->daddr,
3011
                                      icmp_hv_req(cicmph),
3012
                                      ciph->saddr,
3013
                                      icmp_id(cicmph));
3014
 
3015
                if (ms == NULL)
3016
                        return 0;
3017
 
3018
                /* Now we do real damage to this packet...! */
3019
                /* First change the dest IP address, and recalc checksum */
3020
                iph->daddr = ms->saddr;
3021
                ip_send_check(iph);
3022
 
3023
                /* Now change the *source* address in the contained IP */
3024
                ciph->saddr = ms->saddr;
3025
                ip_send_check(ciph);
3026
 
3027
                /* Change the ID to the original one! */
3028
                (cicmph->un).echo.id = ms->sport;
3029
 
3030
                /* And finally the ICMP checksum */
3031
                icmph->checksum = 0;
3032
                icmph->checksum = ip_compute_csum((unsigned char *) icmph, len);
3033
 
3034
#ifdef DEBUG_CONFIG_IP_MASQUERADE_ICMP
3035
                printk("MASQ: rv icmp/icmp rwt %lX->%lX id %d type %d\n",
3036
                       ntohl(ciph->saddr),
3037
                       ntohl(ciph->daddr),
3038
                       ntohs(icmp_id(cicmph)),
3039
                       cicmph->type);
3040
#endif
3041
                return 1;
3042
        }
3043
#endif /* CONFIG_IP_MASQUERADE_ICMP */
3044
 
3045
        /* We are only interested ICMPs generated from TCP or UDP packets */
3046
        if ((ciph->protocol != IPPROTO_UDP) &&
3047
            (ciph->protocol != IPPROTO_TCP))
3048
                return 0;
3049
 
3050
        /*
3051
         * Find the ports involved - remember this packet was
3052
         * *outgoing* so the ports are reversed (and addresses)
3053
         */
3054
        pptr = (__u16 *)&(((char *)ciph)[ciph->ihl*4]);
3055
        if (ntohs(pptr[0]) < PORT_MASQ_BEGIN ||
3056
            ntohs(pptr[0]) > PORT_MASQ_END)
3057
                return 0;
3058
 
3059
        /* Ensure the checksum is correct */
3060
        if (ip_compute_csum((unsigned char *) icmph, len))
3061
        {
3062
                /* Failed checksum! */
3063
                printk(KERN_DEBUG "MASQ: reverse ICMP: failed checksum from %s!\n",
3064
                       in_ntoa(iph->saddr));
3065
                return(-1);
3066
        }
3067
 
3068
#ifdef DEBUG_CONFIG_IP_MASQUERADE
3069
        printk("Handling reverse ICMP for %lX:%X -> %lX:%X\n",
3070
               ntohl(ciph->saddr), ntohs(pptr[0]),
3071
               ntohl(ciph->daddr), ntohs(pptr[1]));
3072
#endif
3073
 
3074
        /* This is pretty much what ip_masq_in_get() does, except params are wrong way round */
3075
        ms = ip_masq_in_get_2(ciph->protocol,
3076
                              ciph->daddr,
3077
                              pptr[1],
3078
                              ciph->saddr,
3079
                              pptr[0]);
3080
 
3081
        if (ms == NULL)
3082
                return 0;
3083
 
3084
        /* Now we do real damage to this packet...! */
3085
        /* First change the dest IP address, and recalc checksum */
3086
        iph->daddr = ms->saddr;
3087
        ip_send_check(iph);
3088
 
3089
        /* Now change the *source* address in the contained IP */
3090
        ciph->saddr = ms->saddr;
3091
        ip_send_check(ciph);
3092
 
3093
        /* the TCP/UDP source port - cannot redo check */
3094
        pptr[0] = ms->sport;
3095
 
3096
        /* And finally the ICMP checksum */
3097
        icmph->checksum = 0;
3098
        icmph->checksum = ip_compute_csum((unsigned char *) icmph, len);
3099
 
3100
#ifdef DEBUG_CONFIG_IP_MASQUERADE
3101
        printk("Rewrote reverse ICMP to %lX:%X -> %lX:%X\n",
3102
               ntohl(ciph->saddr), ntohs(pptr[0]),
3103
               ntohl(ciph->daddr), ntohs(pptr[1]));
3104
#endif
3105
 
3106
        return 1;
3107
}
3108
 
3109
 
3110
 /*
3111
  *     Check if it's an masqueraded port, look it up,
3112
  *     and send it on its way...
3113
  *
3114
  *     Better not have many hosts using the designated portrange
3115
  *     as 'normal' ports, or you'll be spending many time in
3116
  *     this function.
3117
  */
3118
 
3119
int ip_fw_demasquerade(struct sk_buff **skb_p, struct device *dev)
3120
{
3121
        struct sk_buff  *skb = *skb_p;
3122
        struct iphdr    *iph = skb->h.iph;
3123
        __u16   *portptr;
3124
        struct ip_masq  *ms;
3125
        unsigned short len;
3126
        unsigned long   timeout = MASQUERADE_EXPIRE_TCP;
3127
#ifdef CONFIG_IP_MASQUERADE_IPAUTOFW 
3128
        struct ip_autofw *af;
3129
#endif /* CONFIG_IP_MASQUERADE_IPAUTOFW */
3130
 
3131
 
3132
        switch (iph->protocol) {
3133
        case IPPROTO_ICMP:
3134
                return(ip_fw_demasq_icmp(skb_p, dev));
3135
#ifdef CONFIG_IP_MASQUERADE_PPTP
3136
        case IPPROTO_GRE:
3137
                return(ip_fw_demasq_gre(skb_p, dev));
3138
#endif /* CONFIG_IP_MASQUERADE_PPTP */
3139
#ifdef CONFIG_IP_MASQUERADE_IPSEC
3140
        case IPPROTO_ESP:
3141
                return(ip_fw_demasq_esp(skb_p, dev));
3142
#endif /* CONFIG_IP_MASQUERADE_IPSEC */
3143
        case IPPROTO_TCP:
3144
        case IPPROTO_UDP:
3145
                /* Make sure packet is in the masq range */
3146
                portptr = (__u16 *)&(((char *)iph)[iph->ihl*4]);
3147
                if ((ntohs(portptr[1]) < PORT_MASQ_BEGIN ||
3148
                     ntohs(portptr[1]) > PORT_MASQ_END)
3149
#ifdef CONFIG_IP_MASQUERADE_IPSEC
3150
                    && ((iph->protocol != IPPROTO_UDP) || (ntohs(portptr[0]) != UDP_PORT_ISAKMP) || (ntohs(portptr[1]) != UDP_PORT_ISAKMP))
3151
#endif /* CONFIG_IP_MASQUERADE_IPSEC */
3152
#ifdef CONFIG_IP_MASQUERADE_IPAUTOFW 
3153
                    && !ip_autofw_check_range(iph->saddr, portptr[1],
3154
                                              iph->protocol, 0)
3155
                    && !ip_autofw_check_direct(portptr[1], iph->protocol)
3156
                    && !ip_autofw_check_port(portptr[1], iph->protocol)
3157
#endif /* CONFIG_IP_MASQUERADE_IPAUTOFW */
3158
#ifdef CONFIG_IP_MASQUERADE_IPPORTFW
3159
                    && !ip_portfw_check(iph->protocol, portptr[1], iph->daddr)
3160
#endif /* CONFIG_IP_MASQUERADE_IPPORTFW */
3161
                        )
3162
                        return 0;
3163
 
3164
                /* Check that the checksum is OK */
3165
                len = ntohs(iph->tot_len) - (iph->ihl * 4);
3166
                if ((iph->protocol == IPPROTO_UDP) && (portptr[3] == 0))
3167
                        /* No UDP checksum */
3168
                        break;
3169
 
3170
                switch (skb->ip_summed)
3171
                {
3172
                        case CHECKSUM_NONE:
3173
                                skb->csum = csum_partial((char *)portptr, len, 0);
3174
                        case CHECKSUM_HW:
3175
                                if (csum_tcpudp_magic(iph->saddr, iph->daddr, len,
3176
                                                      iph->protocol, skb->csum))
3177
                                {
3178
                                        printk(KERN_DEBUG "MASQ: failed TCP/UDP checksum from %s!\n",
3179
                                               in_ntoa(iph->saddr));
3180
                                        return -1;
3181
                                }
3182
                        default:
3183
                                /* CHECKSUM_UNNECESSARY */
3184
                }
3185
                break;
3186
        default:
3187
                return 0;
3188
        }
3189
 
3190
 
3191
#ifdef DEBUG_CONFIG_IP_MASQUERADE
3192
        printk("Incoming %s %lX:%X -> %lX:%X\n",
3193
                masq_proto_name(iph->protocol),
3194
                ntohl(iph->saddr), ntohs(portptr[0]),
3195
                ntohl(iph->daddr), ntohs(portptr[1]));
3196
#endif
3197
        /*
3198
         * reroute to original host:port if found...
3199
         */
3200
 
3201
        ms = ip_masq_in_get(iph);
3202
 
3203
#ifdef CONFIG_IP_MASQUERADE_IPAUTOFW 
3204
 
3205
        if (ms == NULL && (af=ip_autofw_check_range(iph->saddr, portptr[1], iph->protocol, 0)))
3206
        {
3207
#ifdef DEBUG_CONFIG_IP_MASQUERADE
3208
                printk("ip_autofw_check_range\n");
3209
#endif
3210
                ms = ip_masq_new_enh(dev, iph->protocol,
3211
                                     af->where, portptr[1],
3212
                                     iph->saddr, portptr[0],
3213
                                     0,
3214
                                     portptr[1]);
3215
        }
3216
        if ( ms == NULL && (af=ip_autofw_check_port(portptr[1], iph->protocol)) )
3217
        {
3218
#ifdef DEBUG_CONFIG_IP_MASQUERADE
3219
                printk("ip_autofw_check_port\n");
3220
#endif
3221
                ms = ip_masq_new_enh(dev, iph->protocol,
3222
                                     af->where, htons(af->hidden),
3223
                                     iph->saddr, portptr[0],
3224
                                     IP_MASQ_F_AFW_PORT,
3225
                                     htons(af->visible));
3226
        }
3227
#endif /* CONFIG_IP_MASQUERADE_IPAUTOFW */
3228
 
3229
#ifdef CONFIG_IP_MASQUERADE_IPPORTFW
3230
        /* If no entry exists in the masquerading table, and the port is involved
3231
           in port forwarding, create a new entry */
3232
        {
3233
                __u32 raddr;
3234
                __u16 rport;
3235
                if ((ms == NULL) &&
3236
                      (iph->protocol==IPPROTO_TCP || iph->protocol==IPPROTO_UDP) &&
3237
                      ip_portfw_lookup(iph->protocol, portptr[1], iph->daddr, &raddr, &rport))
3238
                {
3239
                       ms = ip_masq_new_portfw(dev, iph->protocol, raddr, rport,
3240
                                iph->saddr, portptr[0], iph->daddr, portptr[1]);
3241
                }
3242
        }
3243
#endif /* CONFIG_IP_MASQUERADE_IPPORTFW */
3244
 
3245
 
3246
        if (ms != NULL)
3247
        {
3248
#ifdef CONFIG_IP_MASQUERADE_IPAUTOFW 
3249
                ip_autofw_update_in(iph->saddr, portptr[1], iph->protocol);
3250
#endif /* CONFIG_IP_MASQUERADE_IPAUTOFW */
3251
 
3252
                /* Stop the timer ticking.... */
3253
                ip_masq_set_expire(ms,0);
3254
 
3255
                /*
3256
                 *      got reply, so clear flag
3257
                 */
3258
                ms->flags &= ~IP_MASQ_F_NO_REPLY;
3259
 
3260
                /*
3261
                 *      Set dport if not defined yet.
3262
                 */
3263
 
3264
                if ( ms->flags & IP_MASQ_F_NO_DPORT && ms->protocol == IPPROTO_TCP ) {
3265
                        ms->flags &= ~IP_MASQ_F_NO_DPORT;
3266
                        ms->dport = portptr[0];
3267
#ifdef DEBUG_CONFIG_IP_MASQUERADE
3268
                        printk("ip_fw_demasquerade(): filled dport=%d\n",
3269
                               ntohs(ms->dport));
3270
#endif
3271
                }
3272
                if (ms->flags & IP_MASQ_F_NO_DADDR && ms->protocol == IPPROTO_TCP)  {
3273
                        ms->flags &= ~IP_MASQ_F_NO_DADDR;
3274
                        ms->daddr = iph->saddr;
3275
#ifdef DEBUG_CONFIG_IP_MASQUERADE
3276
                        printk("ip_fw_demasquerade(): filled daddr=%X\n",
3277
                               ntohs(ms->daddr));
3278
#endif
3279
                }
3280
                iph->daddr = ms->saddr;
3281
                portptr[1] = ms->sport;
3282
 
3283
                /*
3284
                 *      Attempt ip_masq_app call.
3285
                 *      will fix ip_masq and iph ack_seq stuff
3286
                 */
3287
 
3288
                if (ip_masq_app_pkt_in(ms, skb_p, dev) != 0)
3289
                {
3290
                        /*
3291
                         *      skb has changed, update pointers.
3292
                         */
3293
 
3294
                        skb = *skb_p;
3295
                        iph = skb->h.iph;
3296
                        portptr = (__u16 *)&(((char *)iph)[iph->ihl*4]);
3297
                        len = ntohs(iph->tot_len) - (iph->ihl * 4);
3298
                }
3299
 
3300
#ifdef CONFIG_IP_MASQUERADE_PPTP
3301
#ifdef CONFIG_IP_MASQUERADE_PPTP_MULTICLIENT
3302
                if (iph->protocol == IPPROTO_TCP && ntohs(portptr[0]) == PPTP_CONTROL_PORT)
3303
                {
3304
                        /*
3305
                         * Packet received from PPTP control port. Process it.
3306
                         * May change call ID word in request, but
3307
                         * packet length will not change.
3308
                         */
3309
                        ip_masq_pptp(skb, ms, dev);
3310
                }
3311
#endif /* CONFIG_IP_MASQUERADE_PPTP_MULTICLIENT */
3312
#endif /* CONFIG_IP_MASQUERADE_PPTP */
3313
 
3314
                /*
3315
                 * Yug! adjust UDP/TCP and IP checksums, also update
3316
                 * timeouts.
3317
                 * If a TCP RST is seen collapse the tunnel (by using short timeout)!
3318
                 */
3319
                if (masq_proto_num(iph->protocol)==0)
3320
                {
3321
                        recalc_check((struct udphdr *)portptr,iph->saddr,iph->daddr,len);
3322
 
3323
#ifdef CONFIG_IP_MASQUERADE_IPSEC
3324
                        if (iph->protocol == IPPROTO_UDP && ntohs(portptr[0]) == UDP_PORT_ISAKMP && ntohs(portptr[1]) == UDP_PORT_ISAKMP) {
3325
                                /* ISAKMP timeout should be same as ESP timeout to allow for rekeying */
3326
                                timeout = MASQUERADE_EXPIRE_IPSEC;
3327
                        } else
3328
#endif /* CONFIG_IP_MASQUERADE_IPSEC */
3329
 
3330
                        timeout = ip_masq_expire->udp_timeout;
3331
                }
3332
                else
3333
                {
3334
                        struct tcphdr *th;
3335
                        if(len>=sizeof(struct tcphdr))
3336
                        {
3337
                                skb->csum = csum_partial((void *)(((struct tcphdr *)portptr) + 1),
3338
                                                 len - sizeof(struct tcphdr), 0);
3339
                                tcp_send_check((struct tcphdr *)portptr,iph->saddr,iph->daddr,len,skb);
3340
 
3341
                                /* Check if TCP FIN or RST */
3342
                                th = (struct tcphdr *)portptr;
3343
                                if (th->fin)
3344
                                {
3345
                                        ms->flags |= IP_MASQ_F_SAW_FIN_IN;
3346
                                }
3347
                                if (th->rst)
3348
                                {
3349
                                        ms->flags |= IP_MASQ_F_SAW_RST;
3350
                                }
3351
 
3352
                                /* Now set the timeouts */
3353
                                if (ms->flags & IP_MASQ_F_SAW_RST)
3354
                                {
3355
                                        timeout = 1;
3356
                                }
3357
                                else if ((ms->flags & IP_MASQ_F_SAW_FIN) == IP_MASQ_F_SAW_FIN)
3358
                                {
3359
                                        timeout = ip_masq_expire->tcp_fin_timeout;
3360
                                }
3361
                                else timeout = ip_masq_expire->tcp_timeout;
3362
                        }
3363
                }
3364
                ip_masq_set_expire(ms, timeout);
3365
                ip_send_check(iph);
3366
#ifdef DEBUG_CONFIG_IP_MASQUERADE
3367
                printk("I-routed to %lX:%X\n",ntohl(iph->daddr),ntohs(portptr[1]));
3368
#endif
3369
                return 1;
3370
        }
3371
 
3372
        /* sorry, all this trouble for a no-hit :) */
3373
        return 0;
3374
}
3375
 
3376
/*
3377
 *      /proc/net entries
3378
 */
3379
 
3380
 
3381
#ifdef CONFIG_IP_MASQUERADE_IPAUTOFW
3382
 
3383
static int ip_autofw_procinfo(char *buffer, char **start, off_t offset,
3384
                              int length, int unused)
3385
{
3386
        off_t pos=0, begin=0;
3387
        struct ip_autofw * af;
3388
        int len=0;
3389
 
3390
        len=sprintf(buffer,"Type Prot Low  High Vis  Hid  Where    Last     CPto CPrt Timer Flags\n");
3391
 
3392
        for(af = ip_autofw_hosts; af ; af = af->next)
3393
        {
3394
                len+=sprintf(buffer+len,"%4X %4X %04X-%04X/%04X %04X %08lX %08lX %04X %04X %6lu %4X\n",
3395
                                        af->type,
3396
                                        af->protocol,
3397
                                        af->low,
3398
                                        af->high,
3399
                                        af->visible,
3400
                                        af->hidden,
3401
                                        ntohl(af->where),
3402
                                        ntohl(af->lastcontact),
3403
                                        af->ctlproto,
3404
                                        af->ctlport,
3405
                                        (af->timer.expires<jiffies ? 0 : af->timer.expires-jiffies),
3406
                                        af->flags);
3407
 
3408
                pos=begin+len;
3409
                if(pos<offset)
3410
                {
3411
                        len=0;
3412
                        begin=pos;
3413
                }
3414
                if(pos>offset+length)
3415
                        break;
3416
        }
3417
        *start=buffer+(offset-begin);
3418
        len-=(offset-begin);
3419
        if(len>length)
3420
                len=length;
3421
        return len;
3422
}
3423
#endif /* CONFIG_IP_MASQUERADE_IPAUTOFW */
3424
 
3425
#ifdef CONFIG_IP_MASQUERADE_IPPORTFW
3426
static int ip_portfw_procinfo(char *buffer, char **start, off_t offset,
3427
                            int length, int unused)
3428
{
3429
        off_t pos=0, begin;
3430
        struct ip_portfw *pf;
3431
        unsigned long flags;
3432
        char temp[65];
3433
        int ind, raddr, laddr;
3434
        int len=0;
3435
 
3436
        if (offset < 64)
3437
        {
3438
                sprintf(temp, "Prot Local Addr/Port > Remote Addr/Port");
3439
                len = sprintf(buffer, "%-63s\n", temp);
3440
        }
3441
        pos = 64;
3442
        save_flags(flags);
3443
        cli();
3444
 
3445
        for(ind = 0; ind < 2; ind++)
3446
        {
3447
                for(pf = ipportfw_lst[ind]; pf ; pf = pf->next)
3448
                {
3449
                        pos += 64;
3450
                        if (pos <= offset)
3451
                                continue;
3452
 
3453
                        raddr = ntohl(pf->raddr);
3454
                        laddr = ntohl(pf->laddr);
3455
                        sprintf(temp,"%s %d.%d.%d.%d/%d > %d.%d.%d.%d/%d",
3456
                                strProt[ind],
3457
                                (laddr >> 24) & 255, (laddr >> 16) & 255,
3458
                                (laddr >> 8) & 255, laddr & 255, ntohs(pf->lport),
3459
                                (raddr >> 24) & 255, (raddr >> 16) & 255,
3460
                                (raddr >> 8) & 255, raddr & 255, ntohs(pf->rport) );
3461
                        len += sprintf(buffer+len, "%-63s\n", temp);
3462
                        if (len >= length)
3463
                                goto done;
3464
               }
3465
        }
3466
done:
3467
        restore_flags(flags);
3468
        begin = len - (pos - offset);
3469
        *start = buffer + begin;
3470
        len -= begin;
3471
        if(len>length)
3472
                len = length;
3473
        return len;
3474
}
3475
#endif /* CONFIG_IP_MASQUERADE_IPPORTFW */
3476
 
3477
 
3478
static int ip_msqhst_procinfo(char *buffer, char **start, off_t offset,
3479
                              int length, int unused)
3480
{
3481
        off_t pos=0, begin;
3482
        struct ip_masq *ms;
3483
        unsigned long flags;
3484
        char temp[129];
3485
        int idx = 0;
3486
        int len=0;
3487
 
3488
        if (offset < 128)
3489
        {
3490
#ifdef CONFIG_IP_MASQUERADE_ICMP
3491
                sprintf(temp,
3492
                        "Prc FromIP   FPrt ToIP     TPrt Masq Init-seq  Delta PDelta Expires (free=%d,%d,%d)",
3493
                        ip_masq_free_ports[0], ip_masq_free_ports[1], ip_masq_free_ports[2]);
3494
#else /* !defined(CONFIG_IP_MASQUERADE_ICMP) */
3495
                sprintf(temp,
3496
                        "Prc FromIP   FPrt ToIP     TPrt Masq Init-seq  Delta PDelta Expires (free=%d,%d)",
3497
                        ip_masq_free_ports[0], ip_masq_free_ports[1]);
3498
#endif /* CONFIG_IP_MASQUERADE_ICMP */
3499
                len = sprintf(buffer, "%-127s\n", temp);
3500
        }
3501
        pos = 128;
3502
        save_flags(flags);
3503
        cli();
3504
 
3505
        for(idx = 0; idx < IP_MASQ_TAB_SIZE; idx++)
3506
        for(ms = ip_masq_m_tab[idx]; ms ; ms = ms->m_link)
3507
        {
3508
                int timer_active;
3509
                pos += 128;
3510
                if (pos <= offset)
3511
                        continue;
3512
 
3513
                timer_active = del_timer(&ms->timer);
3514
                if (!timer_active)
3515
                        ms->timer.expires = jiffies;
3516
                sprintf(temp,"%s %08lX:%04X %08lX:%04X %04X %08X %6d %6d %7lu",
3517
                        masq_proto_name(ms->protocol),
3518
                        ntohl(ms->saddr), ntohs(ms->sport),
3519
                        ntohl(ms->daddr), ntohs(ms->dport),
3520
                        ntohs(ms->mport),
3521
                        ms->out_seq.init_seq,
3522
                        ms->out_seq.delta,
3523
                        ms->out_seq.previous_delta,
3524
                        ms->timer.expires-jiffies);
3525
                if (timer_active)
3526
                        add_timer(&ms->timer);
3527
                len += sprintf(buffer+len, "%-127s\n", temp);
3528
 
3529
                if(len >= length)
3530
                        goto done;
3531
        }
3532
done:
3533
        restore_flags(flags);
3534
        begin = len - (pos - offset);
3535
        *start = buffer + begin;
3536
        len -= begin;
3537
        if(len>length)
3538
                len = length;
3539
        return len;
3540
}
3541
 
3542
#ifdef CONFIG_PROC_FS        
3543
static struct proc_dir_entry pde1 = {
3544
                PROC_NET_IPMSQHST, 13, "ip_masquerade",
3545
                S_IFREG | S_IRUGO, 1, 0, 0,
3546
                0, &proc_net_inode_operations,
3547
                ip_msqhst_procinfo
3548
        };
3549
#ifdef CONFIG_IP_MASQUERADE_IPAUTOFW
3550
static struct proc_dir_entry pde2 = {
3551
                PROC_NET_IPAUTOFW, 9, "ip_autofw",
3552
                S_IFREG | S_IRUGO, 1, 0, 0,
3553
                0, &proc_net_inode_operations,
3554
                ip_autofw_procinfo
3555
        };
3556
#endif /* CONFIG_IP_MASQUERADE_IPAUTOFW */
3557
#endif /* CONFIG_PROC_FS */
3558
/*
3559
 *      Initialize ip masquerading
3560
 */
3561
int ip_masq_init(void)
3562
{
3563
        register_symtab (&ip_masq_syms);
3564
#ifdef CONFIG_IP_MASQUERADE_PPTP
3565
        register_symtab (&pptp_masq_syms);
3566
#endif /* CONFIG_IP_MASQUERADE_PPTP */
3567
#ifdef CONFIG_IP_MASQUERADE_IPSEC
3568
        register_symtab (&ipsec_masq_syms);
3569
#endif /* CONFIG_IP_MASQUERADE_IPSEC */
3570
#ifdef CONFIG_PROC_FS        
3571
        proc_net_register(&pde1);
3572
#ifdef CONFIG_IP_MASQUERADE_IPAUTOFW
3573
        proc_net_register(&pde2);
3574
#endif /* CONFIG_IP_MASQUERADE_IPAUTOFW */
3575
#ifdef CONFIG_IP_MASQUERADE_IPPORTFW
3576
                proc_net_register(&(struct proc_dir_entry) {
3577
                PROC_NET_IPPORTFW, 9, "ip_portfw",
3578
                S_IFREG | S_IRUGO , 1, 0, 0,
3579
                0, &proc_net_inode_operations,
3580
                ip_portfw_procinfo
3581
        });
3582
#endif /* CONFIG_IP_MASQUERADE_IPPORTFW */
3583
#endif /* CONFIG_PROC_FS */
3584
        ip_masq_app_init();
3585
 
3586
#if !defined(MODULE)
3587
        {
3588
                extern int ip_masq_ftp_init(void), ip_masq_irc_init(void);
3589
                extern int ip_masq_raudio_init(void), ip_cuseeme_irc_init(void);
3590
                extern int ip_masq_vdolive_init(void), ip_quake_irc_init(void);
3591
 
3592
                ip_masq_ftp_init();
3593
                ip_masq_irc_init();
3594
                ip_masq_raudio_init();
3595
                ip_masq_cuseeme_init();
3596
                ip_masq_vdolive_init();
3597
                ip_masq_quake_init();
3598
        }
3599
#endif /* MODULE */
3600
        return 0;
3601
}

powered by: WebSVN 2.1.0

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