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