VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / net / ipv4 / netfilter / ip_conntrack_irc.c
1 /* IRC extension for IP connection tracking, Version 1.21
2  * (C) 2000-2002 by Harald Welte <laforge@gnumonks.org>
3  * based on RR's ip_conntrack_ftp.c     
4  *
5  * ip_conntrack_irc.c,v 1.21 2002/02/05 14:49:26 laforge Exp
6  *
7  *      This program is free software; you can redistribute it and/or
8  *      modify it under the terms of the GNU General Public License
9  *      as published by the Free Software Foundation; either version
10  *      2 of the License, or (at your option) any later version.
11  **
12  *      Module load syntax:
13  *      insmod ip_conntrack_irc.o ports=port1,port2,...port<MAX_PORTS>
14  *                          max_dcc_channels=n dcc_timeout=secs
15  *      
16  *      please give the ports of all IRC servers You wish to connect to.
17  *      If You don't specify ports, the default will be port 6667.
18  *      With max_dcc_channels you can define the maximum number of not
19  *      yet answered DCC channels per IRC session (default 8).
20  *      With dcc_timeout you can specify how long the system waits for 
21  *      an expected DCC channel (default 300 seconds).
22  *
23  */
24
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/netfilter.h>
28 #include <linux/ip.h>
29 #include <net/checksum.h>
30 #include <net/tcp.h>
31
32 #include <linux/netfilter_ipv4/lockhelp.h>
33 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
34 #include <linux/netfilter_ipv4/ip_conntrack_irc.h>
35
36 #define MAX_PORTS 8
37 static int ports[MAX_PORTS];
38 static int ports_c;
39 static int max_dcc_channels = 8;
40 static unsigned int dcc_timeout = 300;
41 /* This is slow, but it's simple. --RR */
42 static char irc_buffer[65536];
43
44 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
45 MODULE_DESCRIPTION("IRC (DCC) connection tracking helper");
46 MODULE_LICENSE("GPL");
47 MODULE_PARM(ports, "1-" __MODULE_STRING(MAX_PORTS) "i");
48 MODULE_PARM_DESC(ports, "port numbers of IRC servers");
49 MODULE_PARM(max_dcc_channels, "i");
50 MODULE_PARM_DESC(max_dcc_channels, "max number of expected DCC channels per IRC session");
51 MODULE_PARM(dcc_timeout, "i");
52 MODULE_PARM_DESC(dcc_timeout, "timeout on for unestablished DCC channels");
53
54 static char *dccprotos[] = { "SEND ", "CHAT ", "MOVE ", "TSEND ", "SCHAT " };
55 #define MINMATCHLEN     5
56
57 DECLARE_LOCK(ip_irc_lock);
58 struct module *ip_conntrack_irc = THIS_MODULE;
59
60 #if 0
61 #define DEBUGP(format, args...) printk(KERN_DEBUG "%s:%s:" format, \
62                                        __FILE__, __FUNCTION__ , ## args)
63 #else
64 #define DEBUGP(format, args...)
65 #endif
66
67 int parse_dcc(char *data, char *data_end, u_int32_t * ip, u_int16_t * port,
68               char **ad_beg_p, char **ad_end_p)
69 /* tries to get the ip_addr and port out of a dcc command
70    return value: -1 on failure, 0 on success 
71         data            pointer to first byte of DCC command data
72         data_end        pointer to last byte of dcc command data
73         ip              returns parsed ip of dcc command
74         port            returns parsed port of dcc command
75         ad_beg_p        returns pointer to first byte of addr data
76         ad_end_p        returns pointer to last byte of addr data */
77 {
78
79         /* at least 12: "AAAAAAAA P\1\n" */
80         while (*data++ != ' ')
81                 if (data > data_end - 12)
82                         return -1;
83
84         *ad_beg_p = data;
85         *ip = simple_strtoul(data, &data, 10);
86
87         /* skip blanks between ip and port */
88         while (*data == ' ') {
89                 if (data >= data_end) 
90                         return -1;
91                 data++;
92         }
93
94         *port = simple_strtoul(data, &data, 10);
95         *ad_end_p = data;
96
97         return 0;
98 }
99
100 static int help(struct sk_buff *skb,
101                 struct ip_conntrack *ct, enum ip_conntrack_info ctinfo)
102 {
103         unsigned int dataoff;
104         struct tcphdr tcph;
105         char *data, *data_limit;
106         int dir = CTINFO2DIR(ctinfo);
107         struct ip_conntrack_expect *exp;
108         struct ip_ct_irc_expect *exp_irc_info = NULL;
109
110         u_int32_t dcc_ip;
111         u_int16_t dcc_port;
112         int i;
113         char *addr_beg_p, *addr_end_p;
114
115         DEBUGP("entered\n");
116
117         /* If packet is coming from IRC server */
118         if (dir == IP_CT_DIR_REPLY)
119                 return NF_ACCEPT;
120
121         /* Until there's been traffic both ways, don't look in packets. */
122         if (ctinfo != IP_CT_ESTABLISHED
123             && ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY) {
124                 DEBUGP("Conntrackinfo = %u\n", ctinfo);
125                 return NF_ACCEPT;
126         }
127
128         /* Not a full tcp header? */
129         if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &tcph, sizeof(tcph)) != 0)
130                 return NF_ACCEPT;
131
132         /* No data? */
133         dataoff = skb->nh.iph->ihl*4 + tcph.doff*4;
134         if (dataoff >= skb->len)
135                 return NF_ACCEPT;
136
137         LOCK_BH(&ip_irc_lock);
138         skb_copy_bits(skb, dataoff, irc_buffer, skb->len - dataoff);
139
140         data = irc_buffer;
141         data_limit = irc_buffer + skb->len - dataoff;
142
143         /* strlen("\1DCC SENT t AAAAAAAA P\1\n")=24
144          * 5+MINMATCHLEN+strlen("t AAAAAAAA P\1\n")=14 */
145         while (data < (data_limit - (19 + MINMATCHLEN))) {
146                 if (memcmp(data, "\1DCC ", 5)) {
147                         data++;
148                         continue;
149                 }
150
151                 data += 5;
152                 /* we have at least (19+MINMATCHLEN)-5 bytes valid data left */
153
154                 DEBUGP("DCC found in master %u.%u.%u.%u:%u %u.%u.%u.%u:%u...\n",
155                         NIPQUAD(iph->saddr), ntohs(tcph.source),
156                         NIPQUAD(iph->daddr), ntohs(tcph.dest));
157
158                 for (i = 0; i < ARRAY_SIZE(dccprotos); i++) {
159                         if (memcmp(data, dccprotos[i], strlen(dccprotos[i]))) {
160                                 /* no match */
161                                 continue;
162                         }
163
164                         DEBUGP("DCC %s detected\n", dccprotos[i]);
165                         data += strlen(dccprotos[i]);
166                         /* we have at least 
167                          * (19+MINMATCHLEN)-5-dccprotos[i].matchlen bytes valid
168                          * data left (== 14/13 bytes) */
169                         if (parse_dcc((char *)data, data_limit, &dcc_ip,
170                                        &dcc_port, &addr_beg_p, &addr_end_p)) {
171                                 /* unable to parse */
172                                 DEBUGP("unable to parse dcc command\n");
173                                 continue;
174                         }
175                         DEBUGP("DCC bound ip/port: %u.%u.%u.%u:%u\n",
176                                 HIPQUAD(dcc_ip), dcc_port);
177
178                         /* dcc_ip can be the internal OR external (NAT'ed) IP
179                          * Tiago Sousa <mirage@kaotik.org> */
180                         if (ct->tuplehash[dir].tuple.src.ip != htonl(dcc_ip)
181                             && ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip != htonl(dcc_ip)) {
182                                 if (net_ratelimit())
183                                         printk(KERN_WARNING
184                                                 "Forged DCC command from "
185                                                 "%u.%u.%u.%u: %u.%u.%u.%u:%u\n",
186                                 NIPQUAD(ct->tuplehash[dir].tuple.src.ip),
187                                                 HIPQUAD(dcc_ip), dcc_port);
188
189                                 continue;
190                         }
191
192                         exp = ip_conntrack_expect_alloc();
193                         if (exp == NULL)
194                                 goto out;
195
196                         exp_irc_info = &exp->help.exp_irc_info;
197
198                         /* save position of address in dcc string,
199                          * necessary for NAT */
200                         DEBUGP("tcph->seq = %u\n", tcph.seq);
201                         exp->seq = ntohl(tcph.seq) + (addr_beg_p - irc_buffer);
202                         exp_irc_info->len = (addr_end_p - addr_beg_p);
203                         exp_irc_info->port = dcc_port;
204                         DEBUGP("wrote info seq=%u (ofs=%u), len=%d\n",
205                                 exp->seq, (addr_end_p - _data), exp_irc_info->len);
206
207                         exp->tuple = ((struct ip_conntrack_tuple)
208                                 { { 0, { 0 } },
209                                   { ct->tuplehash[dir].tuple.src.ip, { .tcp = { htons(dcc_port) } },
210                                     IPPROTO_TCP }});
211                         exp->mask = ((struct ip_conntrack_tuple)
212                                 { { 0, { 0 } },
213                                   { 0xFFFFFFFF, { .tcp = { 0xFFFF } }, 0xFFFF }});
214
215                         exp->expectfn = NULL;
216
217                         DEBUGP("expect_related %u.%u.%u.%u:%u-%u.%u.%u.%u:%u\n",
218                                 NIPQUAD(exp->tuple.src.ip),
219                                 ntohs(exp->tuple.src.u.tcp.port),
220                                 NIPQUAD(exp->tuple.dst.ip),
221                                 ntohs(exp->tuple.dst.u.tcp.port));
222
223                         ip_conntrack_expect_related(exp, ct);
224
225                         goto out;
226                 } /* for .. NUM_DCCPROTO */
227         } /* while data < ... */
228
229  out:
230         UNLOCK_BH(&ip_irc_lock);
231         return NF_ACCEPT;
232 }
233
234 static struct ip_conntrack_helper irc_helpers[MAX_PORTS];
235 static char irc_names[MAX_PORTS][10];
236
237 static void fini(void);
238
239 static int __init init(void)
240 {
241         int i, ret;
242         struct ip_conntrack_helper *hlpr;
243         char *tmpname;
244
245         if (max_dcc_channels < 1) {
246                 printk("ip_conntrack_irc: max_dcc_channels must be a positive integer\n");
247                 return -EBUSY;
248         }
249         if (dcc_timeout < 0) {
250                 printk("ip_conntrack_irc: dcc_timeout must be a positive integer\n");
251                 return -EBUSY;
252         }
253         
254         /* If no port given, default to standard irc port */
255         if (ports[0] == 0)
256                 ports[0] = IRC_PORT;
257
258         for (i = 0; (i < MAX_PORTS) && ports[i]; i++) {
259                 hlpr = &irc_helpers[i];
260                 hlpr->tuple.src.u.tcp.port = htons(ports[i]);
261                 hlpr->tuple.dst.protonum = IPPROTO_TCP;
262                 hlpr->mask.src.u.tcp.port = 0xFFFF;
263                 hlpr->mask.dst.protonum = 0xFFFF;
264                 hlpr->max_expected = max_dcc_channels;
265                 hlpr->timeout = dcc_timeout;
266                 hlpr->flags = IP_CT_HELPER_F_REUSE_EXPECT;
267                 hlpr->me = ip_conntrack_irc;
268                 hlpr->help = help;
269
270                 tmpname = &irc_names[i][0];
271                 if (ports[i] == IRC_PORT)
272                         sprintf(tmpname, "irc");
273                 else
274                         sprintf(tmpname, "irc-%d", i);
275                 hlpr->name = tmpname;
276
277                 DEBUGP("port #%d: %d\n", i, ports[i]);
278
279                 ret = ip_conntrack_helper_register(hlpr);
280
281                 if (ret) {
282                         printk("ip_conntrack_irc: ERROR registering port %d\n",
283                                 ports[i]);
284                         fini();
285                         return -EBUSY;
286                 }
287                 ports_c++;
288         }
289         return 0;
290 }
291
292 /* This function is intentionally _NOT_ defined as __exit, because 
293  * it is needed by the init function */
294 static void fini(void)
295 {
296         int i;
297         for (i = 0; i < ports_c; i++) {
298                 DEBUGP("unregistering port %d\n",
299                        ports[i]);
300                 ip_conntrack_helper_unregister(&irc_helpers[i]);
301         }
302 }
303
304 PROVIDES_CONNTRACK(irc);
305 EXPORT_SYMBOL(ip_irc_lock);
306
307 module_init(init);
308 module_exit(fini);