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