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