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