]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - src/ipp2p/ipt_ipp2p.c
Load libata prior udev at installer because some SATA doesnt autoload it
[people/pmueller/ipfire-2.x.git] / src / ipp2p / ipt_ipp2p.c
CommitLineData
552b817b
MT
1#if defined(MODVERSIONS)
2#include <linux/modversions.h>
3#endif
4#include <linux/module.h>
552b817b 5#include <linux/version.h>
2d4a4c40
MT
6
7#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
8# include <linux/netfilter/x_tables.h>
9# define ipt_register_match xt_register_match
10# define ipt_unregister_match xt_unregister_match
11# define ipt_match xt_match
12#else
13# include <linux/netfilter_ipv4/ip_tables.h>
552b817b 14//#include <linux/netfilter_ipv4/ipt_ipp2p.h>
2d4a4c40
MT
15#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21) */
16
552b817b
MT
17#include "ipt_ipp2p.h"
18#include <net/tcp.h>
19#include <net/udp.h>
20
21#define get_u8(X,O) (*(__u8 *)(X + O))
22#define get_u16(X,O) (*(__u16 *)(X + O))
23#define get_u32(X,O) (*(__u32 *)(X + O))
24
25MODULE_AUTHOR("Eicke Friedrich/Klaus Degner <ipp2p@ipp2p.org>");
26MODULE_DESCRIPTION("An extension to iptables to identify P2P traffic.");
27MODULE_LICENSE("GPL");
28
29
30/*Search for UDP eDonkey/eMule/Kad commands*/
31int
32udp_search_edk (unsigned char *haystack, int packet_len)
33{
34 unsigned char *t = haystack;
35 t += 8;
36
37 switch (t[0]) {
38 case 0xe3:
39 { /*edonkey*/
40 switch (t[1])
41 {
42 /* client -> server status request */
43 case 0x96:
44 if (packet_len == 14) return ((IPP2P_EDK * 100) + 50);
45 break;
46 /* server -> client status request */
47 case 0x97: if (packet_len == 42) return ((IPP2P_EDK * 100) + 51);
48 break;
49 /* server description request */
50 /* e3 2a ff f0 .. | size == 6 */
51 case 0xa2: if ( (packet_len == 14) && ( get_u16(t,2) == __constant_htons(0xfff0) ) ) return ((IPP2P_EDK * 100) + 52);
52 break;
53 /* server description response */
54 /* e3 a3 ff f0 .. | size > 40 && size < 200 */
55 //case 0xa3: return ((IPP2P_EDK * 100) + 53);
56 // break;
57 case 0x9a: if (packet_len==26) return ((IPP2P_EDK * 100) + 54);
58 break;
59
60 case 0x92: if (packet_len==18) return ((IPP2P_EDK * 100) + 55);
61 break;
62 }
63 break;
64 }
65 case 0xe4:
66 {
67 switch (t[1])
68 {
69 /* e4 20 .. | size == 43 */
70 case 0x20: if ((packet_len == 43) && (t[2] != 0x00) && (t[34] != 0x00)) return ((IPP2P_EDK * 100) + 60);
71 break;
72 /* e4 00 .. 00 | size == 35 ? */
73 case 0x00: if ((packet_len == 35) && (t[26] == 0x00)) return ((IPP2P_EDK * 100) + 61);
74 break;
75 /* e4 10 .. 00 | size == 35 ? */
76 case 0x10: if ((packet_len == 35) && (t[26] == 0x00)) return ((IPP2P_EDK * 100) + 62);
77 break;
78 /* e4 18 .. 00 | size == 35 ? */
79 case 0x18: if ((packet_len == 35) && (t[26] == 0x00)) return ((IPP2P_EDK * 100) + 63);
80 break;
81 /* e4 52 .. | size = 44 */
82 case 0x52: if (packet_len == 44 ) return ((IPP2P_EDK * 100) + 64);
83 break;
84 /* e4 58 .. | size == 6 */
85 case 0x58: if (packet_len == 14 ) return ((IPP2P_EDK * 100) + 65);
86 break;
87 /* e4 59 .. | size == 2 */
88 case 0x59: if (packet_len == 10 )return ((IPP2P_EDK * 100) + 66);
89 break;
90 /* e4 28 .. | packet_len == 52,77,102,127... */
91 case 0x28: if (((packet_len-52) % 25) == 0) return ((IPP2P_EDK * 100) + 67);
92 break;
93 /* e4 50 xx xx | size == 4 */
94 case 0x50: if (packet_len == 12) return ((IPP2P_EDK * 100) + 68);
95 break;
96 /* e4 40 xx xx | size == 48 */
97 case 0x40: if (packet_len == 56) return ((IPP2P_EDK * 100) + 69);
98 break;
99 }
100 break;
101 }
102 } /* end of switch (t[0]) */
103 return 0;
104}/*udp_search_edk*/
105
106
107/*Search for UDP Gnutella commands*/
108int
109udp_search_gnu (unsigned char *haystack, int packet_len)
110{
111 unsigned char *t = haystack;
112 t += 8;
113
114 if (memcmp(t, "GND", 3) == 0) return ((IPP2P_GNU * 100) + 51);
115 if (memcmp(t, "GNUTELLA ", 9) == 0) return ((IPP2P_GNU * 100) + 52);
116 return 0;
117}/*udp_search_gnu*/
118
119
120/*Search for UDP KaZaA commands*/
121int
122udp_search_kazaa (unsigned char *haystack, int packet_len)
123{
124 unsigned char *t = haystack;
125
126 if (t[packet_len-1] == 0x00){
127 t += (packet_len - 6);
128 if (memcmp(t, "KaZaA", 5) == 0) return (IPP2P_KAZAA * 100 +50);
129 }
130
131 return 0;
132}/*udp_search_kazaa*/
133
134/*Search for UDP DirectConnect commands*/
135int
136udp_search_directconnect (unsigned char *haystack, int packet_len)
137{
138 unsigned char *t = haystack;
139 if ((*(t + 8) == 0x24) && (*(t + packet_len - 1) == 0x7c)) {
140 t+=8;
141 if (memcmp(t, "SR ", 3) == 0) return ((IPP2P_DC * 100) + 60);
142 if (memcmp(t, "Ping ", 5) == 0) return ((IPP2P_DC * 100) + 61);
143 }
144 return 0;
145}/*udp_search_directconnect*/
146
147
148
149/*Search for UDP BitTorrent commands*/
150int
151udp_search_bit (unsigned char *haystack, int packet_len)
152{
153 switch(packet_len)
154 {
155 case 24:
156 /* ^ 00 00 04 17 27 10 19 80 */
157 if ((ntohl(get_u32(haystack, 8)) == 0x00000417) && (ntohl(get_u32(haystack, 12)) == 0x27101980))
158 return (IPP2P_BIT * 100 + 50);
159 break;
160 case 44:
161 if (get_u32(haystack, 16) == __constant_htonl(0x00000400) && get_u32(haystack, 36) == __constant_htonl(0x00000104))
162 return (IPP2P_BIT * 100 + 51);
163 if (get_u32(haystack, 16) == __constant_htonl(0x00000400))
164 return (IPP2P_BIT * 100 + 61);
165 break;
166 case 65:
167 if (get_u32(haystack, 16) == __constant_htonl(0x00000404) && get_u32(haystack, 36) == __constant_htonl(0x00000104))
168 return (IPP2P_BIT * 100 + 52);
169 if (get_u32(haystack, 16) == __constant_htonl(0x00000404))
170 return (IPP2P_BIT * 100 + 62);
171 break;
172 case 67:
173 if (get_u32(haystack, 16) == __constant_htonl(0x00000406) && get_u32(haystack, 36) == __constant_htonl(0x00000104))
174 return (IPP2P_BIT * 100 + 53);
175 if (get_u32(haystack, 16) == __constant_htonl(0x00000406))
176 return (IPP2P_BIT * 100 + 63);
177 break;
178 case 211:
179 if (get_u32(haystack, 8) == __constant_htonl(0x00000405))
180 return (IPP2P_BIT * 100 + 54);
181 break;
182 case 29:
183 if ((get_u32(haystack, 8) == __constant_htonl(0x00000401)))
184 return (IPP2P_BIT * 100 + 55);
185 break;
186 case 52:
187 if (get_u32(haystack,8) == __constant_htonl(0x00000827) &&
188 get_u32(haystack,12) == __constant_htonl(0x37502950))
189 return (IPP2P_BIT * 100 + 80);
190 break;
191 default:
192 /* this packet does not have a constant size */
193 if (packet_len >= 40 && get_u32(haystack, 16) == __constant_htonl(0x00000402) && get_u32(haystack, 36) == __constant_htonl(0x00000104))
194 return (IPP2P_BIT * 100 + 56);
195 break;
196 }
197
198 /* some extra-bitcomet rules:
199 * "d1:" [a|r] "d2:id20:"
200 */
201 if (packet_len > 30 && get_u8(haystack, 8) == 'd' && get_u8(haystack, 9) == '1' && get_u8(haystack, 10) == ':' )
202 {
203 if (get_u8(haystack, 11) == 'a' || get_u8(haystack, 11) == 'r')
204 {
205 if (memcmp(haystack+12,"d2:id20:",8)==0)
206 return (IPP2P_BIT * 100 + 57);
207 }
208 }
209
210#if 0
211 /* bitlord rules */
212 /* packetlen must be bigger than 40 */
213 /* first 4 bytes are zero */
214 if (packet_len > 40 && get_u32(haystack, 8) == 0x00000000)
215 {
216 /* first rule: 00 00 00 00 01 00 00 xx xx xx xx 00 00 00 00*/
217 if (get_u32(haystack, 12) == 0x00000000 &&
218 get_u32(haystack, 16) == 0x00010000 &&
219 get_u32(haystack, 24) == 0x00000000 )
220 return (IPP2P_BIT * 100 + 71);
221
222 /* 00 01 00 00 0d 00 00 xx xx xx xx 00 00 00 00*/
223 if (get_u32(haystack, 12) == 0x00000001 &&
224 get_u32(haystack, 16) == 0x000d0000 &&
225 get_u32(haystack, 24) == 0x00000000 )
226 return (IPP2P_BIT * 100 + 71);
227
228
229 }
230#endif
231
232 return 0;
233}/*udp_search_bit*/
234
235
236
237/*Search for Ares commands*/
238//#define IPP2P_DEBUG_ARES
239int
240search_ares (const unsigned char *payload, const u16 plen)
241//int search_ares (unsigned char *haystack, int packet_len, int head_len)
242{
243// const unsigned char *t = haystack + head_len;
244
245 /* all ares packets start with */
246 if (payload[1] == 0 && (plen - payload[0]) == 3)
247 {
248 switch (payload[2])
249 {
250 case 0x5a:
251 /* ares connect */
252 if ( plen == 6 && payload[5] == 0x05 ) return ((IPP2P_ARES * 100) + 1);
253 break;
254 case 0x09:
255 /* ares search, min 3 chars --> 14 bytes
256 * lets define a search can be up to 30 chars --> max 34 bytes
257 */
258 if ( plen >= 14 && plen <= 34 ) return ((IPP2P_ARES * 100) + 1);
259 break;
260#ifdef IPP2P_DEBUG_ARES
261 default:
262 printk(KERN_DEBUG "Unknown Ares command %x recognized, len: %u \n", (unsigned int) payload[2],plen);
263#endif /* IPP2P_DEBUG_ARES */
264 }
265 }
266
267#if 0
268 /* found connect packet: 03 00 5a 04 03 05 */
269 /* new version ares 1.8: 03 00 5a xx xx 05 */
270 if ((plen) == 6){ /* possible connect command*/
271 if ((payload[0] == 0x03) && (payload[1] == 0x00) && (payload[2] == 0x5a) && (payload[5] == 0x05))
272 return ((IPP2P_ARES * 100) + 1);
273 }
274 if ((plen) == 60){ /* possible download command*/
275 if ((payload[59] == 0x0a) && (payload[58] == 0x0a)){
276 if (memcmp(t, "PUSH SHA1:", 10) == 0) /* found download command */
277 return ((IPP2P_ARES * 100) + 2);
278 }
279 }
280#endif
281
282 return 0;
283} /*search_ares*/
284
285/*Search for SoulSeek commands*/
286int
287search_soul (const unsigned char *payload, const u16 plen)
288{
289//#define IPP2P_DEBUG_SOUL
290 /* match: xx xx xx xx | xx = sizeof(payload) - 4 */
291 if (get_u32(payload, 0) == (plen - 4)){
292 const __u32 m=get_u32(payload, 4);
293 /* match 00 yy yy 00, yy can be everything */
294 if ( get_u8(payload, 4) == 0x00 && get_u8(payload, 7) == 0x00 )
295 {
296#ifdef IPP2P_DEBUG_SOUL
297 printk(KERN_DEBUG "0: Soulseek command 0x%x recognized\n",get_u32(payload, 4));
298#endif /* IPP2P_DEBUG_SOUL */
299 return ((IPP2P_SOUL * 100) + 1);
300 }
301
302 /* next match: 01 yy 00 00 | yy can be everything */
303 if ( get_u8(payload, 4) == 0x01 && get_u16(payload, 6) == 0x0000 )
304 {
305#ifdef IPP2P_DEBUG_SOUL
306 printk(KERN_DEBUG "1: Soulseek command 0x%x recognized\n",get_u16(payload, 4));
307#endif /* IPP2P_DEBUG_SOUL */
308 return ((IPP2P_SOUL * 100) + 2);
309 }
310
311 /* other soulseek commandos are: 1-5,7,9,13-18,22,23,26,28,35-37,40-46,50,51,60,62-69,91,92,1001 */
312 /* try to do this in an intelligent way */
313 /* get all small commandos */
314 switch(m)
315 {
316 case 7:
317 case 9:
318 case 22:
319 case 23:
320 case 26:
321 case 28:
322 case 50:
323 case 51:
324 case 60:
325 case 91:
326 case 92:
327 case 1001:
328#ifdef IPP2P_DEBUG_SOUL
329 printk(KERN_DEBUG "2: Soulseek command 0x%x recognized\n",get_u16(payload, 4));
330#endif /* IPP2P_DEBUG_SOUL */
331 return ((IPP2P_SOUL * 100) + 3);
332 }
333
334 if (m > 0 && m < 6 )
335 {
336#ifdef IPP2P_DEBUG_SOUL
337 printk(KERN_DEBUG "3: Soulseek command 0x%x recognized\n",get_u16(payload, 4));
338#endif /* IPP2P_DEBUG_SOUL */
339 return ((IPP2P_SOUL * 100) + 4);
340 }
341 if (m > 12 && m < 19 )
342 {
343#ifdef IPP2P_DEBUG_SOUL
344 printk(KERN_DEBUG "4: Soulseek command 0x%x recognized\n",get_u16(payload, 4));
345#endif /* IPP2P_DEBUG_SOUL */
346 return ((IPP2P_SOUL * 100) + 5);
347 }
348
349 if (m > 34 && m < 38 )
350 {
351#ifdef IPP2P_DEBUG_SOUL
352 printk(KERN_DEBUG "5: Soulseek command 0x%x recognized\n",get_u16(payload, 4));
353#endif /* IPP2P_DEBUG_SOUL */
354 return ((IPP2P_SOUL * 100) + 6);
355 }
356
357 if (m > 39 && m < 47 )
358 {
359#ifdef IPP2P_DEBUG_SOUL
360 printk(KERN_DEBUG "6: Soulseek command 0x%x recognized\n",get_u16(payload, 4));
361#endif /* IPP2P_DEBUG_SOUL */
362 return ((IPP2P_SOUL * 100) + 7);
363 }
364
365 if (m > 61 && m < 70 )
366 {
367#ifdef IPP2P_DEBUG_SOUL
368 printk(KERN_DEBUG "7: Soulseek command 0x%x recognized\n",get_u16(payload, 4));
369#endif /* IPP2P_DEBUG_SOUL */
370 return ((IPP2P_SOUL * 100) + 8);
371 }
372
373#ifdef IPP2P_DEBUG_SOUL
374 printk(KERN_DEBUG "unknown SOULSEEK command: 0x%x, first 16 bit: 0x%x, first 8 bit: 0x%x ,soulseek ???\n",get_u32(payload, 4),get_u16(payload, 4) >> 16,get_u8(payload, 4) >> 24);
375#endif /* IPP2P_DEBUG_SOUL */
376 }
377
378 /* match 14 00 00 00 01 yy 00 00 00 STRING(YY) 01 00 00 00 00 46|50 00 00 00 00 */
379 /* without size at the beginning !!! */
380 if ( get_u32(payload, 0) == 0x14 && get_u8(payload, 4) == 0x01 )
381 {
382 __u32 y=get_u32(payload, 5);
383 /* we need 19 chars + string */
384 if ( (y + 19) <= (plen) )
385 {
386 const unsigned char *w=payload+9+y;
387 if (get_u32(w, 0) == 0x01 && ( get_u16(w, 4) == 0x4600 || get_u16(w, 4) == 0x5000) && get_u32(w, 6) == 0x00);
388#ifdef IPP2P_DEBUG_SOUL
389 printk(KERN_DEBUG "Soulssek special client command recognized\n");
390#endif /* IPP2P_DEBUG_SOUL */
391 return ((IPP2P_SOUL * 100) + 9);
392 }
393 }
394 return 0;
395}
396
397
398/*Search for WinMX commands*/
399int
400search_winmx (const unsigned char *payload, const u16 plen)
401{
402//#define IPP2P_DEBUG_WINMX
403 if (((plen) == 4) && (memcmp(payload, "SEND", 4) == 0)) return ((IPP2P_WINMX * 100) + 1);
404 if (((plen) == 3) && (memcmp(payload, "GET", 3) == 0)) return ((IPP2P_WINMX * 100) + 2);
405 //if (packet_len < (head_len + 10)) return 0;
406 if (plen < 10) return 0;
407
408 if ((memcmp(payload, "SEND", 4) == 0) || (memcmp(payload, "GET", 3) == 0)){
409 u16 c=4;
410 const u16 end=plen-2;
411 u8 count=0;
412 while (c < end)
413 {
414 if (payload[c]== 0x20 && payload[c+1] == 0x22)
415 {
416 c++;
417 count++;
418 if (count>=2) return ((IPP2P_WINMX * 100) + 3);
419 }
420 c++;
421 }
422 }
423
424 if ( plen == 149 && payload[0] == '8' )
425 {
426#ifdef IPP2P_DEBUG_WINMX
427 printk(KERN_INFO "maybe WinMX\n");
428#endif
429 if (get_u32(payload,17) == 0 && get_u32(payload,21) == 0 && get_u32(payload,25) == 0 &&
430// get_u32(payload,33) == __constant_htonl(0x71182b1a) && get_u32(payload,37) == __constant_htonl(0x05050000) &&
431// get_u32(payload,133) == __constant_htonl(0x31097edf) && get_u32(payload,145) == __constant_htonl(0xdcb8f792))
432 get_u16(payload,39) == 0 && get_u16(payload,135) == __constant_htons(0x7edf) && get_u16(payload,147) == __constant_htons(0xf792))
433
434 {
435#ifdef IPP2P_DEBUG_WINMX
436 printk(KERN_INFO "got WinMX\n");
437#endif
438 return ((IPP2P_WINMX * 100) + 4);
439 }
440 }
441 return 0;
442} /*search_winmx*/
443
444
445/*Search for appleJuice commands*/
446int
447search_apple (const unsigned char *payload, const u16 plen)
448{
449 if ( (plen > 7) && (payload[6] == 0x0d) && (payload[7] == 0x0a) && (memcmp(payload, "ajprot", 6) == 0)) return (IPP2P_APPLE * 100);
450
451 return 0;
452}
453
454
455/*Search for BitTorrent commands*/
456int
457search_bittorrent (const unsigned char *payload, const u16 plen)
458{
459 if (plen > 20)
460 {
461 /* test for match 0x13+"BitTorrent protocol" */
462 if (payload[0] == 0x13)
463 {
464 if (memcmp(payload+1, "BitTorrent protocol", 19) == 0) return (IPP2P_BIT * 100);
465 }
466
467 /* get tracker commandos, all starts with GET /
468 * then it can follow: scrape| announce
469 * and then ?hash_info=
470 */
471 if (memcmp(payload,"GET /",5) == 0)
472 {
473 /* message scrape */
474 if ( memcmp(payload+5,"scrape?info_hash=",17)==0 ) return (IPP2P_BIT * 100 + 1);
475 /* message announce */
476 if ( memcmp(payload+5,"announce?info_hash=",19)==0 ) return (IPP2P_BIT * 100 + 2);
477 }
478 }
479 else
480 {
481 /* bitcomet encryptes the first packet, so we have to detect another
482 * one later in the flow */
483 /* first try failed, too many missdetections */
484 //if ( size == 5 && get_u32(t,0) == __constant_htonl(1) && t[4] < 3) return (IPP2P_BIT * 100 + 3);
485
486 /* second try: block request packets */
487 if ( plen == 17 && get_u32(payload,0) == __constant_htonl(0x0d) && payload[4] == 0x06 && get_u32(payload,13) == __constant_htonl(0x4000) ) return (IPP2P_BIT * 100 + 3);
488 }
489
490 return 0;
491}
492
493
494
495/*check for Kazaa get command*/
496int
497search_kazaa (const unsigned char *payload, const u16 plen)
498
499{
500 if ((payload[plen-2] == 0x0d) && (payload[plen-1] == 0x0a) && memcmp(payload, "GET /.hash=", 11) == 0)
501 return (IPP2P_DATA_KAZAA * 100);
502
503 return 0;
504}
505
506
507/*check for gnutella get command*/
508int
509search_gnu (const unsigned char *payload, const u16 plen)
510{
511 if ((payload[plen-2] == 0x0d) && (payload[plen-1] == 0x0a))
512 {
513 if (memcmp(payload, "GET /get/", 9) == 0) return ((IPP2P_DATA_GNU * 100) + 1);
514 if (memcmp(payload, "GET /uri-res/", 13) == 0) return ((IPP2P_DATA_GNU * 100) + 2);
515 }
516 return 0;
517}
518
519
520/*check for gnutella get commands and other typical data*/
521int
522search_all_gnu (const unsigned char *payload, const u16 plen)
523{
524
525 if ((payload[plen-2] == 0x0d) && (payload[plen-1] == 0x0a))
526 {
527
528 if (memcmp(payload, "GNUTELLA CONNECT/", 17) == 0) return ((IPP2P_GNU * 100) + 1);
529 if (memcmp(payload, "GNUTELLA/", 9) == 0) return ((IPP2P_GNU * 100) + 2);
530
531
532 if ((memcmp(payload, "GET /get/", 9) == 0) || (memcmp(payload, "GET /uri-res/", 13) == 0))
533 {
534 u16 c=8;
535 const u16 end=plen-22;
536 while (c < end) {
537 if ( payload[c] == 0x0a && payload[c+1] == 0x0d && ((memcmp(&payload[c+2], "X-Gnutella-", 11) == 0) || (memcmp(&payload[c+2], "X-Queue:", 8) == 0)))
538 return ((IPP2P_GNU * 100) + 3);
539 c++;
540 }
541 }
542 }
543 return 0;
544}
545
546
547/*check for KaZaA download commands and other typical data*/
548int
549search_all_kazaa (const unsigned char *payload, const u16 plen)
550{
551 if ((payload[plen-2] == 0x0d) && (payload[plen-1] == 0x0a))
552 {
553
554 if (memcmp(payload, "GIVE ", 5) == 0) return ((IPP2P_KAZAA * 100) + 1);
555
556 if (memcmp(payload, "GET /", 5) == 0) {
557 u16 c = 8;
558 const u16 end=plen-22;
559 while (c < end) {
560 if ( payload[c] == 0x0a && payload[c+1] == 0x0d && ((memcmp(&payload[c+2], "X-Kazaa-Username: ", 18) == 0) || (memcmp(&payload[c+2], "User-Agent: PeerEnabler/", 24) == 0)))
561 return ((IPP2P_KAZAA * 100) + 2);
562 c++;
563 }
564 }
565 }
566 return 0;
567}
568
569/*fast check for edonkey file segment transfer command*/
570int
571search_edk (const unsigned char *payload, const u16 plen)
572{
573 if (payload[0] != 0xe3)
574 return 0;
575 else {
576 if (payload[5] == 0x47)
577 return (IPP2P_DATA_EDK * 100);
578 else
579 return 0;
580 }
581}
582
583
584
585/*intensive but slower search for some edonkey packets including size-check*/
586int
587search_all_edk (const unsigned char *payload, const u16 plen)
588{
589 if (payload[0] != 0xe3)
590 return 0;
591 else {
592 //t += head_len;
593 const u16 cmd = get_u16(payload, 1);
594 if (cmd == (plen - 5)) {
595 switch (payload[5]) {
596 case 0x01: return ((IPP2P_EDK * 100) + 1); /*Client: hello or Server:hello*/
597 case 0x4c: return ((IPP2P_EDK * 100) + 9); /*Client: Hello-Answer*/
598 }
599 }
600 return 0;
601 }
602}
603
604
605/*fast check for Direct Connect send command*/
606int
607search_dc (const unsigned char *payload, const u16 plen)
608{
609
610 if (payload[0] != 0x24 )
611 return 0;
612 else {
613 if (memcmp(&payload[1], "Send|", 5) == 0)
614 return (IPP2P_DATA_DC * 100);
615 else
616 return 0;
617 }
618
619}
620
621
622/*intensive but slower check for all direct connect packets*/
623int
624search_all_dc (const unsigned char *payload, const u16 plen)
625{
626// unsigned char *t = haystack;
627
628 if (payload[0] == 0x24 && payload[plen-1] == 0x7c)
629 {
630 const unsigned char *t=&payload[1];
631 /* Client-Hub-Protocol */
632 if (memcmp(t, "Lock ", 5) == 0) return ((IPP2P_DC * 100) + 1);
633 /* Client-Client-Protocol, some are already recognized by client-hub (like lock) */
634 if (memcmp(t, "MyNick ", 7) == 0) return ((IPP2P_DC * 100) + 38);
635 }
636 return 0;
637}
638
639/*check for mute*/
640int
641search_mute (const unsigned char *payload, const u16 plen)
642{
643 if ( plen == 209 || plen == 345 || plen == 473 || plen == 609 || plen == 1121 )
644 {
645 //printk(KERN_DEBUG "size hit: %u",size);
646 if (memcmp(payload,"PublicKey: ",11) == 0 )
647 {
648 return ((IPP2P_MUTE * 100) + 0);
649
650/* if (memcmp(t+size-14,"\x0aEndPublicKey\x0a",14) == 0)
651 {
652 printk(KERN_DEBUG "end pubic key hit: %u",size);
653
654 }*/
655 }
656 }
657 return 0;
658}
659
660
661/* check for xdcc */
662int
663search_xdcc (const unsigned char *payload, const u16 plen)
664{
665 /* search in small packets only */
666 if (plen > 20 && plen < 200 && payload[plen-1] == 0x0a && payload[plen-2] == 0x0d && memcmp(payload,"PRIVMSG ",8) == 0)
667 {
668
669 u16 x=10;
670 const u16 end=plen - 13;
671
672 /* is seems to be a irc private massage, chedck for xdcc command */
673 while (x < end)
674 {
675 if (payload[x] == ':')
676 {
677 if ( memcmp(&payload[x+1],"xdcc send #",11) == 0 )
678 return ((IPP2P_XDCC * 100) + 0);
679 }
680 x++;
681 }
682 }
683 return 0;
684}
685
686/* search for waste */
687int search_waste(const unsigned char *payload, const u16 plen)
688{
689 if ( plen >= 8 && memcmp(payload,"GET.sha1:",9) == 0)
690 return ((IPP2P_WASTE * 100) + 0);
691
692 return 0;
693}
694
695
696static struct {
697 int command;
698 __u8 short_hand; /*for fucntions included in short hands*/
699 int packet_len;
700 int (*function_name) (const unsigned char *, const u16);
701} matchlist[] = {
702 {IPP2P_EDK,SHORT_HAND_IPP2P,20, &search_all_edk},
703// {IPP2P_DATA_KAZAA,SHORT_HAND_DATA,200, &search_kazaa},
704// {IPP2P_DATA_EDK,SHORT_HAND_DATA,60, &search_edk},
705// {IPP2P_DATA_DC,SHORT_HAND_DATA,26, &search_dc},
706 {IPP2P_DC,SHORT_HAND_IPP2P,5, search_all_dc},
707// {IPP2P_DATA_GNU,SHORT_HAND_DATA,40, &search_gnu},
708 {IPP2P_GNU,SHORT_HAND_IPP2P,5, &search_all_gnu},
709 {IPP2P_KAZAA,SHORT_HAND_IPP2P,5, &search_all_kazaa},
710 {IPP2P_BIT,SHORT_HAND_IPP2P,20, &search_bittorrent},
711 {IPP2P_APPLE,SHORT_HAND_IPP2P,5, &search_apple},
712 {IPP2P_SOUL,SHORT_HAND_IPP2P,5, &search_soul},
713 {IPP2P_WINMX,SHORT_HAND_IPP2P,2, &search_winmx},
714 {IPP2P_ARES,SHORT_HAND_IPP2P,5, &search_ares},
715 {IPP2P_MUTE,SHORT_HAND_NONE,200, &search_mute},
716 {IPP2P_WASTE,SHORT_HAND_NONE,5, &search_waste},
717 {IPP2P_XDCC,SHORT_HAND_NONE,5, &search_xdcc},
718 {0,0,0,NULL}
719};
720
721
722static struct {
723 int command;
724 __u8 short_hand; /*for fucntions included in short hands*/
725 int packet_len;
726 int (*function_name) (unsigned char *, int);
727} udp_list[] = {
728 {IPP2P_KAZAA,SHORT_HAND_IPP2P,14, &udp_search_kazaa},
729 {IPP2P_BIT,SHORT_HAND_IPP2P,23, &udp_search_bit},
730 {IPP2P_GNU,SHORT_HAND_IPP2P,11, &udp_search_gnu},
731 {IPP2P_EDK,SHORT_HAND_IPP2P,9, &udp_search_edk},
732 {IPP2P_DC,SHORT_HAND_IPP2P,12, &udp_search_directconnect},
733 {0,0,0,NULL}
734};
735
736
2d4a4c40
MT
737#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
738static bool
739#else
552b817b 740static int
2d4a4c40 741#endif
552b817b
MT
742match(const struct sk_buff *skb,
743 const struct net_device *in,
744 const struct net_device *out,
745#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
746 const struct xt_match *mymatch,
747 const void *matchinfo,
748 int offset,
749 unsigned int myprotoff,
750#else
751 const void *matchinfo,
752 int offset,
753#endif
754
755#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
756 const void *hdr,
757 u_int16_t datalen,
758#endif
759
2d4a4c40
MT
760#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
761 bool *hotdrop)
762#else
552b817b 763 int *hotdrop)
2d4a4c40 764#endif
552b817b
MT
765{
766 const struct ipt_p2p_info *info = matchinfo;
767 unsigned char *haystack;
2d4a4c40
MT
768#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
769 struct iphdr *ip = ip_hdr(skb);
770#else
552b817b 771 struct iphdr *ip = skb->nh.iph;
2d4a4c40 772#endif
552b817b
MT
773 int p2p_result = 0, i = 0;
774// int head_len;
775 int hlen = ntohs(ip->tot_len)-(ip->ihl*4); /*hlen = packet-data length*/
776
777 /*must not be a fragment*/
778 if (offset) {
779 if (info->debug) printk("IPP2P.match: offset found %i \n",offset);
780 return 0;
781 }
782
783 /*make sure that skb is linear*/
784 if(skb_is_nonlinear(skb)){
785 if (info->debug) printk("IPP2P.match: nonlinear skb found\n");
786 return 0;
787 }
788
789
790 haystack=(char *)ip+(ip->ihl*4); /*haystack = packet data*/
791
792 switch (ip->protocol){
793 case IPPROTO_TCP: /*what to do with a TCP packet*/
794 {
795 struct tcphdr *tcph = (void *) ip + ip->ihl * 4;
796
797 if (tcph->fin) return 0; /*if FIN bit is set bail out*/
798 if (tcph->syn) return 0; /*if SYN bit is set bail out*/
799 if (tcph->rst) return 0; /*if RST bit is set bail out*/
800
801 haystack += tcph->doff * 4; /*get TCP-Header-Size*/
802 hlen -= tcph->doff * 4;
803 while (matchlist[i].command) {
804 if ((((info->cmd & matchlist[i].command) == matchlist[i].command) ||
805 ((info->cmd & matchlist[i].short_hand) == matchlist[i].short_hand)) &&
806 (hlen > matchlist[i].packet_len)) {
807 p2p_result = matchlist[i].function_name(haystack, hlen);
808 if (p2p_result)
809 {
810 if (info->debug) printk("IPP2P.debug:TCP-match: %i from: %u.%u.%u.%u:%i to: %u.%u.%u.%u:%i Length: %i\n",
811 p2p_result, NIPQUAD(ip->saddr),ntohs(tcph->source), NIPQUAD(ip->daddr),ntohs(tcph->dest),hlen);
812 return p2p_result;
813 }
814 }
815 i++;
816 }
817 return p2p_result;
818 }
819
820 case IPPROTO_UDP: /*what to do with an UDP packet*/
821 {
822 struct udphdr *udph = (void *) ip + ip->ihl * 4;
823
824 while (udp_list[i].command){
825 if ((((info->cmd & udp_list[i].command) == udp_list[i].command) ||
826 ((info->cmd & udp_list[i].short_hand) == udp_list[i].short_hand)) &&
827 (hlen > udp_list[i].packet_len)) {
828 p2p_result = udp_list[i].function_name(haystack, hlen);
829 if (p2p_result){
830 if (info->debug) printk("IPP2P.debug:UDP-match: %i from: %u.%u.%u.%u:%i to: %u.%u.%u.%u:%i Length: %i\n",
831 p2p_result, NIPQUAD(ip->saddr),ntohs(udph->source), NIPQUAD(ip->daddr),ntohs(udph->dest),hlen);
832 return p2p_result;
833 }
834 }
835 i++;
836 }
837 return p2p_result;
838 }
839
840 default: return 0;
841 }
842}
843
844
845
2d4a4c40
MT
846#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
847static bool
848#else
552b817b 849static int
2d4a4c40 850#endif
552b817b
MT
851checkentry(const char *tablename,
852#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
853 const void *ip,
854 const struct xt_match *mymatch,
855#else
856 const struct ipt_ip *ip,
857#endif
858 void *matchinfo,
2d4a4c40 859#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
552b817b 860 unsigned int matchsize,
2d4a4c40 861#endif
552b817b
MT
862 unsigned int hook_mask)
863{
864 /* Must specify -p tcp */
865/* if (ip->proto != IPPROTO_TCP || (ip->invflags & IPT_INV_PROTO)) {
866 * printk("ipp2p: Only works on TCP packets, use -p tcp\n");
867 * return 0;
868 * }*/
869 return 1;
870}
871
872
552b817b
MT
873static struct ipt_match ipp2p_match = {
874#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
875 { NULL, NULL },
876 "ipp2p",
877 &match,
878 &checkentry,
879 NULL,
880 THIS_MODULE
2d4a4c40 881#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
552b817b
MT
882 .name = "ipp2p",
883 .match = &match,
884 .checkentry = &checkentry,
885 .me = THIS_MODULE,
2d4a4c40 886#else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17) */
552b817b
MT
887 .name = "ipp2p",
888 .match = &match,
889 .family = AF_INET,
2d4a4c40 890 .matchsize = XT_ALIGN(sizeof(struct ipt_p2p_info)),
552b817b
MT
891 .checkentry = &checkentry,
892 .me = THIS_MODULE,
893#endif
894};
895
896
897static int __init init(void)
898{
899 printk(KERN_INFO "IPP2P v%s loading\n", IPP2P_VERSION);
900 return ipt_register_match(&ipp2p_match);
901}
902
903static void __exit fini(void)
904{
905 ipt_unregister_match(&ipp2p_match);
906 printk(KERN_INFO "IPP2P v%s unloaded\n", IPP2P_VERSION);
907}
908
909module_init(init);
910module_exit(fini);
911
912