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