Merge with master.kernel.org:/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / ipv4 / netfilter / ip_nat_irc.c
1 /* IRC extension for TCP NAT alteration.
2 * (C) 2000-2001 by Harald Welte <laforge@gnumonks.org>
3 * (C) 2004 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
4 * based on a copy of RR's ip_nat_ftp.c
5 *
6 * ip_nat_irc.c,v 1.16 2001/12/06 07:42:10 laforge Exp
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
14 #include <linux/module.h>
15 #include <linux/netfilter_ipv4.h>
16 #include <linux/ip.h>
17 #include <linux/tcp.h>
18 #include <linux/kernel.h>
19 #include <net/tcp.h>
20 #include <linux/netfilter_ipv4/ip_nat.h>
21 #include <linux/netfilter_ipv4/ip_nat_helper.h>
22 #include <linux/netfilter_ipv4/ip_nat_rule.h>
23 #include <linux/netfilter_ipv4/ip_conntrack_irc.h>
24 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
25 #include <linux/moduleparam.h>
26
27 #if 0
28 #define DEBUGP printk
29 #else
30 #define DEBUGP(format, args...)
31 #endif
32
33 MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
34 MODULE_DESCRIPTION("IRC (DCC) NAT helper");
35 MODULE_LICENSE("GPL");
36
37 static unsigned int help(struct sk_buff **pskb,
38 enum ip_conntrack_info ctinfo,
39 unsigned int matchoff,
40 unsigned int matchlen,
41 struct ip_conntrack_expect *exp)
42 {
43 u_int16_t port;
44 unsigned int ret;
45
46 /* "4294967296 65635 " */
47 char buffer[18];
48
49 DEBUGP("IRC_NAT: info (seq %u + %u) in %u\n",
50 expect->seq, exp_irc_info->len,
51 ntohl(tcph->seq));
52
53 /* Reply comes from server. */
54 exp->saved_proto.tcp.port = exp->tuple.dst.u.tcp.port;
55 exp->dir = IP_CT_DIR_REPLY;
56
57 /* When you see the packet, we need to NAT it the same as the
58 * this one. */
59 exp->expectfn = ip_nat_follow_master;
60
61 /* Try to get same port: if not, try to change it. */
62 for (port = ntohs(exp->saved_proto.tcp.port); port != 0; port++) {
63 exp->tuple.dst.u.tcp.port = htons(port);
64 if (ip_conntrack_expect_related(exp) == 0)
65 break;
66 }
67
68 if (port == 0)
69 return NF_DROP;
70
71 /* strlen("\1DCC CHAT chat AAAAAAAA P\1\n")=27
72 * strlen("\1DCC SCHAT chat AAAAAAAA P\1\n")=28
73 * strlen("\1DCC SEND F AAAAAAAA P S\1\n")=26
74 * strlen("\1DCC MOVE F AAAAAAAA P S\1\n")=26
75 * strlen("\1DCC TSEND F AAAAAAAA P S\1\n")=27
76 * AAAAAAAAA: bound addr (1.0.0.0==16777216, min 8 digits,
77 * 255.255.255.255==4294967296, 10 digits)
78 * P: bound port (min 1 d, max 5d (65635))
79 * F: filename (min 1 d )
80 * S: size (min 1 d )
81 * 0x01, \n: terminators
82 */
83
84 /* AAA = "us", ie. where server normally talks to. */
85 sprintf(buffer, "%u %u",
86 ntohl(exp->master->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip),
87 port);
88 DEBUGP("ip_nat_irc: Inserting '%s' == %u.%u.%u.%u, port %u\n",
89 buffer, NIPQUAD(exp->tuple.src.ip), port);
90
91 ret = ip_nat_mangle_tcp_packet(pskb, exp->master, ctinfo,
92 matchoff, matchlen, buffer,
93 strlen(buffer));
94 if (ret != NF_ACCEPT)
95 ip_conntrack_unexpect_related(exp);
96 return ret;
97 }
98
99 static void __exit fini(void)
100 {
101 ip_nat_irc_hook = NULL;
102 /* Make sure noone calls it, meanwhile. */
103 synchronize_net();
104 }
105
106 static int __init init(void)
107 {
108 BUG_ON(ip_nat_irc_hook);
109 ip_nat_irc_hook = help;
110 return 0;
111 }
112
113 /* Prior to 2.6.11, we had a ports param. No longer, but don't break users. */
114 static int warn_set(const char *val, struct kernel_param *kp)
115 {
116 printk(KERN_INFO __stringify(KBUILD_MODNAME)
117 ": kernel >= 2.6.10 only uses 'ports' for conntrack modules\n");
118 return 0;
119 }
120 module_param_call(ports, warn_set, NULL, NULL, 0);
121
122 module_init(init);
123 module_exit(fini);