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