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 14:37:56.000000000 +0100
7 +#define IPP2P_VERSION "0.8.1_rc1"
14 +#endif //__IPT_IPP2P_H
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*/
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/Config.in linux.dev/net/ipv4/netfilter/Config.in
37 --- linux.old/net/ipv4/netfilter/Config.in 2006-03-23 14:35:20.000000000 +0100
38 +++ linux.dev/net/ipv4/netfilter/Config.in 2006-03-23 14:35:58.000000000 +0100
40 dep_tristate ' TOS match support' CONFIG_IP_NF_MATCH_TOS $CONFIG_IP_NF_IPTABLES
41 dep_tristate ' recent match support' CONFIG_IP_NF_MATCH_RECENT $CONFIG_IP_NF_IPTABLES
42 dep_tristate ' ECN match support' CONFIG_IP_NF_MATCH_ECN $CONFIG_IP_NF_IPTABLES
43 + dep_tristate ' peer to peer traffic match support' CONFIG_IP_NF_MATCH_IPP2P $CONFIG_IP_NF_IPTABLES
45 dep_tristate ' DSCP match support' CONFIG_IP_NF_MATCH_DSCP $CONFIG_IP_NF_IPTABLES
47 diff -urN linux.old/net/ipv4/netfilter/Makefile linux.dev/net/ipv4/netfilter/Makefile
48 --- linux.old/net/ipv4/netfilter/Makefile 2006-03-23 14:35:20.000000000 +0100
49 +++ linux.dev/net/ipv4/netfilter/Makefile 2006-03-23 14:35:58.000000000 +0100
51 obj-$(CONFIG_IP_NF_MATCH_LIMIT) += ipt_limit.o
52 obj-$(CONFIG_IP_NF_MATCH_MARK) += ipt_mark.o
53 obj-$(CONFIG_IP_NF_MATCH_MAC) += ipt_mac.o
54 +obj-$(CONFIG_IP_NF_MATCH_IPP2P) += ipt_ipp2p.o
56 obj-$(CONFIG_IP_NF_MATCH_PKTTYPE) += ipt_pkttype.o
57 obj-$(CONFIG_IP_NF_MATCH_MULTIPORT) += ipt_multiport.o
58 diff -urN linux.old/net/ipv4/netfilter/ipt_ipp2p.c linux.dev/net/ipv4/netfilter/ipt_ipp2p.c
59 --- linux.old/net/ipv4/netfilter/ipt_ipp2p.c 1970-01-01 01:00:00.000000000 +0100
60 +++ linux.dev/net/ipv4/netfilter/ipt_ipp2p.c 2006-03-23 14:37:18.000000000 +0100
62 +#if defined(MODVERSIONS)
63 +#include <linux/modversions.h>
65 +#include <linux/module.h>
66 +#include <linux/netfilter_ipv4/ip_tables.h>
67 +#include <linux/version.h>
68 +#include <linux/netfilter_ipv4/ipt_ipp2p.h>
72 +#define get_u8(X,O) (*(__u8 *)(X + O))
73 +#define get_u16(X,O) (*(__u16 *)(X + O))
74 +#define get_u32(X,O) (*(__u32 *)(X + O))
76 +MODULE_AUTHOR("Eicke Friedrich/Klaus Degner <ipp2p@ipp2p.org>");
77 +MODULE_DESCRIPTION("An extension to iptables to identify P2P traffic.");
78 +MODULE_LICENSE("GPL");
81 +/*Search for UDP eDonkey/eMule/Kad commands*/
83 +udp_search_edk (unsigned char *haystack, int packet_len)
85 + unsigned char *t = haystack;
93 + /* client -> server status request */
95 + if (packet_len == 14) return ((IPP2P_EDK * 100) + 50);
97 + /* server -> client status request */
98 + case 0x97: if (packet_len == 42) return ((IPP2P_EDK * 100) + 51);
100 + /* server description request */
101 + /* e3 2a ff f0 .. | size == 6 */
102 + case 0xa2: if ( (packet_len == 14) && ( get_u16(t,2) == __constant_htons(0xfff0) ) ) return ((IPP2P_EDK * 100) + 52);
104 + /* server description response */
105 + /* e3 a3 ff f0 .. | size > 40 && size < 200 */
106 + //case 0xa3: return ((IPP2P_EDK * 100) + 53);
108 + case 0x9a: if (packet_len==26) return ((IPP2P_EDK * 100) + 54);
111 + case 0x92: if (packet_len==18) return ((IPP2P_EDK * 100) + 55);
120 + /* e4 20 .. | size == 43 */
121 + case 0x20: if ((packet_len == 43) && (t[2] != 0x00) && (t[34] != 0x00)) return ((IPP2P_EDK * 100) + 60);
123 + /* e4 00 .. 00 | size == 35 ? */
124 + case 0x00: if ((packet_len == 35) && (t[26] == 0x00)) return ((IPP2P_EDK * 100) + 61);
126 + /* e4 10 .. 00 | size == 35 ? */
127 + case 0x10: if ((packet_len == 35) && (t[26] == 0x00)) return ((IPP2P_EDK * 100) + 62);
129 + /* e4 18 .. 00 | size == 35 ? */
130 + case 0x18: if ((packet_len == 35) && (t[26] == 0x00)) return ((IPP2P_EDK * 100) + 63);
132 + /* e4 52 .. | size = 44 */
133 + case 0x52: if (packet_len == 44 ) return ((IPP2P_EDK * 100) + 64);
135 + /* e4 58 .. | size == 6 */
136 + case 0x58: if (packet_len == 14 ) return ((IPP2P_EDK * 100) + 65);
138 + /* e4 59 .. | size == 2 */
139 + case 0x59: if (packet_len == 10 )return ((IPP2P_EDK * 100) + 66);
141 + /* e4 28 .. | packet_len == 52,77,102,127... */
142 + case 0x28: if (((packet_len-52) % 25) == 0) return ((IPP2P_EDK * 100) + 67);
144 + /* e4 50 xx xx | size == 4 */
145 + case 0x50: if (packet_len == 12) return ((IPP2P_EDK * 100) + 68);
147 + /* e4 40 xx xx | size == 48 */
148 + case 0x40: if (packet_len == 56) return ((IPP2P_EDK * 100) + 69);
153 + } /* end of switch (t[0]) */
158 +/*Search for UDP Gnutella commands*/
160 +udp_search_gnu (unsigned char *haystack, int packet_len)
162 + unsigned char *t = haystack;
165 + if (memcmp(t, "GND", 3) == 0) return ((IPP2P_GNU * 100) + 51);
166 + if (memcmp(t, "GNUTELLA ", 9) == 0) return ((IPP2P_GNU * 100) + 52);
171 +/*Search for UDP KaZaA commands*/
173 +udp_search_kazaa (unsigned char *haystack, int packet_len)
175 + unsigned char *t = haystack;
177 + if (t[packet_len-1] == 0x00){
178 + t += (packet_len - 6);
179 + if (memcmp(t, "KaZaA", 5) == 0) return (IPP2P_KAZAA * 100 +50);
183 +}/*udp_search_kazaa*/
185 +/*Search for UDP DirectConnect commands*/
187 +udp_search_directconnect (unsigned char *haystack, int packet_len)
189 + unsigned char *t = haystack;
190 + if ((*(t + 8) == 0x24) && (*(t + packet_len - 1) == 0x7c)) {
192 + if (memcmp(t, "SR ", 3) == 0) return ((IPP2P_DC * 100) + 60);
193 + if (memcmp(t, "Ping ", 5) == 0) return ((IPP2P_DC * 100) + 61);
196 +}/*udp_search_directconnect*/
200 +/*Search for UDP BitTorrent commands*/
202 +udp_search_bit (unsigned char *haystack, int packet_len)
207 + /* ^ 00 00 04 17 27 10 19 80 */
208 + if ((ntohl(get_u32(haystack, 8)) == 0x00000417) && (ntohl(get_u32(haystack, 12)) == 0x27101980))
209 + return (IPP2P_BIT * 100 + 50);
212 + if (get_u32(haystack, 16) == __constant_htonl(0x00000400) && get_u32(haystack, 36) == __constant_htonl(0x00000104))
213 + return (IPP2P_BIT * 100 + 51);
214 + if (get_u32(haystack, 16) == __constant_htonl(0x00000400))
215 + return (IPP2P_BIT * 100 + 61);
218 + if (get_u32(haystack, 16) == __constant_htonl(0x00000404) && get_u32(haystack, 36) == __constant_htonl(0x00000104))
219 + return (IPP2P_BIT * 100 + 52);
220 + if (get_u32(haystack, 16) == __constant_htonl(0x00000404))
221 + return (IPP2P_BIT * 100 + 62);
224 + if (get_u32(haystack, 16) == __constant_htonl(0x00000406) && get_u32(haystack, 36) == __constant_htonl(0x00000104))
225 + return (IPP2P_BIT * 100 + 53);
226 + if (get_u32(haystack, 16) == __constant_htonl(0x00000406))
227 + return (IPP2P_BIT * 100 + 63);
230 + if (get_u32(haystack, 8) == __constant_htonl(0x00000405))
231 + return (IPP2P_BIT * 100 + 54);
234 + if ((get_u32(haystack, 8) == __constant_htonl(0x00000401)))
235 + return (IPP2P_BIT * 100 + 55);
238 + if (get_u32(haystack,8) == __constant_htonl(0x00000827) &&
239 + get_u32(haystack,12) == __constant_htonl(0x37502950))
240 + return (IPP2P_BIT * 100 + 80);
243 + /* this packet does not have a constant size */
244 + if (packet_len >= 40 && get_u32(haystack, 16) == __constant_htonl(0x00000402) && get_u32(haystack, 36) == __constant_htonl(0x00000104))
245 + return (IPP2P_BIT * 100 + 56);
249 + /* some extra-bitcomet rules:
250 + * "d1:" [a|r] "d2:id20:"
252 + if (packet_len > 30 && get_u8(haystack, 8) == 'd' && get_u8(haystack, 9) == '1' && get_u8(haystack, 10) == ':' )
254 + if (get_u8(haystack, 11) == 'a' || get_u8(haystack, 11) == 'r')
256 + if (memcmp(haystack+12,"d2:id20:",8)==0)
257 + return (IPP2P_BIT * 100 + 57);
262 + /* bitlord rules */
263 + /* packetlen must be bigger than 40 */
264 + /* first 4 bytes are zero */
265 + if (packet_len > 40 && get_u32(haystack, 8) == 0x00000000)
267 + /* first rule: 00 00 00 00 01 00 00 xx xx xx xx 00 00 00 00*/
268 + if (get_u32(haystack, 12) == 0x00000000 &&
269 + get_u32(haystack, 16) == 0x00010000 &&
270 + get_u32(haystack, 24) == 0x00000000 )
271 + return (IPP2P_BIT * 100 + 71);
273 + /* 00 01 00 00 0d 00 00 xx xx xx xx 00 00 00 00*/
274 + if (get_u32(haystack, 12) == 0x00000001 &&
275 + get_u32(haystack, 16) == 0x000d0000 &&
276 + get_u32(haystack, 24) == 0x00000000 )
277 + return (IPP2P_BIT * 100 + 71);
288 +/*Search for Ares commands*/
289 +//#define IPP2P_DEBUG_ARES
291 +search_ares (const unsigned char *payload, const u16 plen)
292 +//int search_ares (unsigned char *haystack, int packet_len, int head_len)
294 +// const unsigned char *t = haystack + head_len;
296 + /* all ares packets start with */
297 + if (payload[1] == 0 && (plen - payload[0]) == 3)
299 + switch (payload[2])
303 + if ( plen == 6 && payload[5] == 0x05 ) return ((IPP2P_ARES * 100) + 1);
306 + /* ares search, min 3 chars --> 14 bytes
307 + * lets define a search can be up to 30 chars --> max 34 bytes
309 + if ( plen >= 14 && plen <= 34 ) return ((IPP2P_ARES * 100) + 1);
311 +#ifdef IPP2P_DEBUG_ARES
313 + printk(KERN_DEBUG "Unknown Ares command %x recognized, len: %u \n", (unsigned int) payload[2],plen);
314 +#endif /* IPP2P_DEBUG_ARES */
319 + /* found connect packet: 03 00 5a 04 03 05 */
320 + /* new version ares 1.8: 03 00 5a xx xx 05 */
321 + if ((plen) == 6){ /* possible connect command*/
322 + if ((payload[0] == 0x03) && (payload[1] == 0x00) && (payload[2] == 0x5a) && (payload[5] == 0x05))
323 + return ((IPP2P_ARES * 100) + 1);
325 + if ((plen) == 60){ /* possible download command*/
326 + if ((payload[59] == 0x0a) && (payload[58] == 0x0a)){
327 + if (memcmp(t, "PUSH SHA1:", 10) == 0) /* found download command */
328 + return ((IPP2P_ARES * 100) + 2);
336 +/*Search for SoulSeek commands*/
338 +search_soul (const unsigned char *payload, const u16 plen)
340 +//#define IPP2P_DEBUG_SOUL
341 + /* match: xx xx xx xx | xx = sizeof(payload) - 4 */
342 + if (get_u32(payload, 0) == (plen - 4)){
343 + const __u32 m=get_u32(payload, 4);
344 + /* match 00 yy yy 00, yy can be everything */
345 + if ( get_u8(payload, 4) == 0x00 && get_u8(payload, 7) == 0x00 )
347 +#ifdef IPP2P_DEBUG_SOUL
348 + printk(KERN_DEBUG "0: Soulseek command 0x%x recognized\n",get_u32(payload, 4));
349 +#endif /* IPP2P_DEBUG_SOUL */
350 + return ((IPP2P_SOUL * 100) + 1);
353 + /* next match: 01 yy 00 00 | yy can be everything */
354 + if ( get_u8(payload, 4) == 0x01 && get_u16(payload, 6) == 0x0000 )
356 +#ifdef IPP2P_DEBUG_SOUL
357 + printk(KERN_DEBUG "1: Soulseek command 0x%x recognized\n",get_u16(payload, 4));
358 +#endif /* IPP2P_DEBUG_SOUL */
359 + return ((IPP2P_SOUL * 100) + 2);
362 + /* 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 */
363 + /* try to do this in an intelligent way */
364 + /* get all small commandos */
379 +#ifdef IPP2P_DEBUG_SOUL
380 + printk(KERN_DEBUG "2: Soulseek command 0x%x recognized\n",get_u16(payload, 4));
381 +#endif /* IPP2P_DEBUG_SOUL */
382 + return ((IPP2P_SOUL * 100) + 3);
385 + if (m > 0 && m < 6 )
387 +#ifdef IPP2P_DEBUG_SOUL
388 + printk(KERN_DEBUG "3: Soulseek command 0x%x recognized\n",get_u16(payload, 4));
389 +#endif /* IPP2P_DEBUG_SOUL */
390 + return ((IPP2P_SOUL * 100) + 4);
392 + if (m > 12 && m < 19 )
394 +#ifdef IPP2P_DEBUG_SOUL
395 + printk(KERN_DEBUG "4: Soulseek command 0x%x recognized\n",get_u16(payload, 4));
396 +#endif /* IPP2P_DEBUG_SOUL */
397 + return ((IPP2P_SOUL * 100) + 5);
400 + if (m > 34 && m < 38 )
402 +#ifdef IPP2P_DEBUG_SOUL
403 + printk(KERN_DEBUG "5: Soulseek command 0x%x recognized\n",get_u16(payload, 4));
404 +#endif /* IPP2P_DEBUG_SOUL */
405 + return ((IPP2P_SOUL * 100) + 6);
408 + if (m > 39 && m < 47 )
410 +#ifdef IPP2P_DEBUG_SOUL
411 + printk(KERN_DEBUG "6: Soulseek command 0x%x recognized\n",get_u16(payload, 4));
412 +#endif /* IPP2P_DEBUG_SOUL */
413 + return ((IPP2P_SOUL * 100) + 7);
416 + if (m > 61 && m < 70 )
418 +#ifdef IPP2P_DEBUG_SOUL
419 + printk(KERN_DEBUG "7: Soulseek command 0x%x recognized\n",get_u16(payload, 4));
420 +#endif /* IPP2P_DEBUG_SOUL */
421 + return ((IPP2P_SOUL * 100) + 8);
424 +#ifdef IPP2P_DEBUG_SOUL
425 + 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);
426 +#endif /* IPP2P_DEBUG_SOUL */
429 + /* match 14 00 00 00 01 yy 00 00 00 STRING(YY) 01 00 00 00 00 46|50 00 00 00 00 */
430 + /* without size at the beginning !!! */
431 + if ( get_u32(payload, 0) == 0x14 && get_u8(payload, 4) == 0x01 )
433 + __u32 y=get_u32(payload, 5);
434 + /* we need 19 chars + string */
435 + if ( (y + 19) <= (plen) )
437 + const unsigned char *w=payload+9+y;
438 + if (get_u32(w, 0) == 0x01 && ( get_u16(w, 4) == 0x4600 || get_u16(w, 4) == 0x5000) && get_u32(w, 6) == 0x00);
439 +#ifdef IPP2P_DEBUG_SOUL
440 + printk(KERN_DEBUG "Soulssek special client command recognized\n");
441 +#endif /* IPP2P_DEBUG_SOUL */
442 + return ((IPP2P_SOUL * 100) + 9);
449 +/*Search for WinMX commands*/
451 +search_winmx (const unsigned char *payload, const u16 plen)
453 +//#define IPP2P_DEBUG_WINMX
454 + if (((plen) == 4) && (memcmp(payload, "SEND", 4) == 0)) return ((IPP2P_WINMX * 100) + 1);
455 + if (((plen) == 3) && (memcmp(payload, "GET", 3) == 0)) return ((IPP2P_WINMX * 100) + 2);
456 + //if (packet_len < (head_len + 10)) return 0;
457 + if (plen < 10) return 0;
459 + if ((memcmp(payload, "SEND", 4) == 0) || (memcmp(payload, "GET", 3) == 0)){
461 + const u16 end=plen-2;
465 + if (payload[c]== 0x20 && payload[c+1] == 0x22)
469 + if (count>=2) return ((IPP2P_WINMX * 100) + 3);
475 + if ( plen == 149 && payload[0] == '8' )
477 +#ifdef IPP2P_DEBUG_WINMX
478 + printk(KERN_INFO "maybe WinMX\n");
480 + if (get_u32(payload,17) == 0 && get_u32(payload,21) == 0 && get_u32(payload,25) == 0 &&
481 +// get_u32(payload,33) == __constant_htonl(0x71182b1a) && get_u32(payload,37) == __constant_htonl(0x05050000) &&
482 +// get_u32(payload,133) == __constant_htonl(0x31097edf) && get_u32(payload,145) == __constant_htonl(0xdcb8f792))
483 + get_u16(payload,39) == 0 && get_u16(payload,135) == __constant_htons(0x7edf) && get_u16(payload,147) == __constant_htons(0xf792))
486 +#ifdef IPP2P_DEBUG_WINMX
487 + printk(KERN_INFO "got WinMX\n");
489 + return ((IPP2P_WINMX * 100) + 4);
496 +/*Search for appleJuice commands*/
498 +search_apple (const unsigned char *payload, const u16 plen)
500 + if ( (plen > 7) && (payload[6] == 0x0d) && (payload[7] == 0x0a) && (memcmp(payload, "ajprot", 6) == 0)) return (IPP2P_APPLE * 100);
506 +/*Search for BitTorrent commands*/
508 +search_bittorrent (const unsigned char *payload, const u16 plen)
512 + /* test for match 0x13+"BitTorrent protocol" */
513 + if (payload[0] == 0x13)
515 + if (memcmp(payload+1, "BitTorrent protocol", 19) == 0) return (IPP2P_BIT * 100);
518 + /* get tracker commandos, all starts with GET /
519 + * then it can follow: scrape| announce
520 + * and then ?hash_info=
522 + if (memcmp(payload,"GET /",5) == 0)
524 + /* message scrape */
525 + if ( memcmp(payload+5,"scrape?info_hash=",17)==0 ) return (IPP2P_BIT * 100 + 1);
526 + /* message announce */
527 + if ( memcmp(payload+5,"announce?info_hash=",19)==0 ) return (IPP2P_BIT * 100 + 2);
532 + /* bitcomet encryptes the first packet, so we have to detect another
533 + * one later in the flow */
534 + /* first try failed, too many missdetections */
535 + //if ( size == 5 && get_u32(t,0) == __constant_htonl(1) && t[4] < 3) return (IPP2P_BIT * 100 + 3);
537 + /* second try: block request packets */
538 + 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);
546 +/*check for Kazaa get command*/
548 +search_kazaa (const unsigned char *payload, const u16 plen)
551 + if ((payload[plen-2] == 0x0d) && (payload[plen-1] == 0x0a) && memcmp(payload, "GET /.hash=", 11) == 0)
552 + return (IPP2P_DATA_KAZAA * 100);
558 +/*check for gnutella get command*/
560 +search_gnu (const unsigned char *payload, const u16 plen)
562 + if ((payload[plen-2] == 0x0d) && (payload[plen-1] == 0x0a))
564 + if (memcmp(payload, "GET /get/", 9) == 0) return ((IPP2P_DATA_GNU * 100) + 1);
565 + if (memcmp(payload, "GET /uri-res/", 13) == 0) return ((IPP2P_DATA_GNU * 100) + 2);
571 +/*check for gnutella get commands and other typical data*/
573 +search_all_gnu (const unsigned char *payload, const u16 plen)
576 + if ((payload[plen-2] == 0x0d) && (payload[plen-1] == 0x0a))
579 + if (memcmp(payload, "GNUTELLA CONNECT/", 17) == 0) return ((IPP2P_GNU * 100) + 1);
580 + if (memcmp(payload, "GNUTELLA/", 9) == 0) return ((IPP2P_GNU * 100) + 2);
583 + if ((memcmp(payload, "GET /get/", 9) == 0) || (memcmp(payload, "GET /uri-res/", 13) == 0))
586 + const u16 end=plen-22;
588 + if ( payload[c] == 0x0a && payload[c+1] == 0x0d && ((memcmp(&payload[c+2], "X-Gnutella-", 11) == 0) || (memcmp(&payload[c+2], "X-Queue:", 8) == 0)))
589 + return ((IPP2P_GNU * 100) + 3);
598 +/*check for KaZaA download commands and other typical data*/
600 +search_all_kazaa (const unsigned char *payload, const u16 plen)
602 + if ((payload[plen-2] == 0x0d) && (payload[plen-1] == 0x0a))
605 + if (memcmp(payload, "GIVE ", 5) == 0) return ((IPP2P_KAZAA * 100) + 1);
607 + if (memcmp(payload, "GET /", 5) == 0) {
609 + const u16 end=plen-22;
611 + 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)))
612 + return ((IPP2P_KAZAA * 100) + 2);
620 +/*fast check for edonkey file segment transfer command*/
622 +search_edk (const unsigned char *payload, const u16 plen)
624 + if (payload[0] != 0xe3)
627 + if (payload[5] == 0x47)
628 + return (IPP2P_DATA_EDK * 100);
636 +/*intensive but slower search for some edonkey packets including size-check*/
638 +search_all_edk (const unsigned char *payload, const u16 plen)
640 + if (payload[0] != 0xe3)
644 + const u16 cmd = get_u16(payload, 1);
645 + if (cmd == (plen - 5)) {
646 + switch (payload[5]) {
647 + case 0x01: return ((IPP2P_EDK * 100) + 1); /*Client: hello or Server:hello*/
648 + case 0x4c: return ((IPP2P_EDK * 100) + 9); /*Client: Hello-Answer*/
656 +/*fast check for Direct Connect send command*/
658 +search_dc (const unsigned char *payload, const u16 plen)
661 + if (payload[0] != 0x24 )
664 + if (memcmp(&payload[1], "Send|", 5) == 0)
665 + return (IPP2P_DATA_DC * 100);
673 +/*intensive but slower check for all direct connect packets*/
675 +search_all_dc (const unsigned char *payload, const u16 plen)
677 +// unsigned char *t = haystack;
679 + if (payload[0] == 0x24 && payload[plen-1] == 0x7c)
681 + const unsigned char *t=&payload[1];
682 + /* Client-Hub-Protocol */
683 + if (memcmp(t, "Lock ", 5) == 0) return ((IPP2P_DC * 100) + 1);
684 + /* Client-Client-Protocol, some are already recognized by client-hub (like lock) */
685 + if (memcmp(t, "MyNick ", 7) == 0) return ((IPP2P_DC * 100) + 38);
692 +search_mute (const unsigned char *payload, const u16 plen)
694 + if ( plen == 209 || plen == 345 || plen == 473 || plen == 609 || plen == 1121 )
696 + //printk(KERN_DEBUG "size hit: %u",size);
697 + if (memcmp(payload,"PublicKey: ",11) == 0 )
699 + return ((IPP2P_MUTE * 100) + 0);
701 +/* if (memcmp(t+size-14,"\x0aEndPublicKey\x0a",14) == 0)
703 + printk(KERN_DEBUG "end pubic key hit: %u",size);
712 +/* check for xdcc */
714 +search_xdcc (const unsigned char *payload, const u16 plen)
716 + /* search in small packets only */
717 + if (plen > 20 && plen < 200 && payload[plen-1] == 0x0a && payload[plen-2] == 0x0d && memcmp(payload,"PRIVMSG ",8) == 0)
721 + const u16 end=plen - 13;
723 + /* is seems to be a irc private massage, chedck for xdcc command */
726 + if (payload[x] == ':')
728 + if ( memcmp(&payload[x+1],"xdcc send #",11) == 0 )
729 + return ((IPP2P_XDCC * 100) + 0);
737 +/* search for waste */
738 +int search_waste(const unsigned char *payload, const u16 plen)
740 + if ( plen >= 8 && memcmp(payload,"GET.sha1:",9) == 0)
741 + return ((IPP2P_WASTE * 100) + 0);
749 + __u8 short_hand; /*for fucntions included in short hands*/
751 + int (*function_name) (const unsigned char *, const u16);
753 + {IPP2P_EDK,SHORT_HAND_IPP2P,20, &search_all_edk},
754 +// {IPP2P_DATA_KAZAA,SHORT_HAND_DATA,200, &search_kazaa},
755 +// {IPP2P_DATA_EDK,SHORT_HAND_DATA,60, &search_edk},
756 +// {IPP2P_DATA_DC,SHORT_HAND_DATA,26, &search_dc},
757 + {IPP2P_DC,SHORT_HAND_IPP2P,5, search_all_dc},
758 +// {IPP2P_DATA_GNU,SHORT_HAND_DATA,40, &search_gnu},
759 + {IPP2P_GNU,SHORT_HAND_IPP2P,5, &search_all_gnu},
760 + {IPP2P_KAZAA,SHORT_HAND_IPP2P,5, &search_all_kazaa},
761 + {IPP2P_BIT,SHORT_HAND_IPP2P,20, &search_bittorrent},
762 + {IPP2P_APPLE,SHORT_HAND_IPP2P,5, &search_apple},
763 + {IPP2P_SOUL,SHORT_HAND_IPP2P,5, &search_soul},
764 + {IPP2P_WINMX,SHORT_HAND_IPP2P,2, &search_winmx},
765 + {IPP2P_ARES,SHORT_HAND_IPP2P,5, &search_ares},
766 + {IPP2P_MUTE,SHORT_HAND_NONE,200, &search_mute},
767 + {IPP2P_WASTE,SHORT_HAND_NONE,5, &search_waste},
768 + {IPP2P_XDCC,SHORT_HAND_NONE,5, &search_xdcc},
775 + __u8 short_hand; /*for fucntions included in short hands*/
777 + int (*function_name) (unsigned char *, int);
779 + {IPP2P_KAZAA,SHORT_HAND_IPP2P,14, &udp_search_kazaa},
780 + {IPP2P_BIT,SHORT_HAND_IPP2P,23, &udp_search_bit},
781 + {IPP2P_GNU,SHORT_HAND_IPP2P,11, &udp_search_gnu},
782 + {IPP2P_EDK,SHORT_HAND_IPP2P,9, &udp_search_edk},
783 + {IPP2P_DC,SHORT_HAND_IPP2P,12, &udp_search_directconnect},
789 +match(const struct sk_buff *skb,
790 + const struct net_device *in,
791 + const struct net_device *out,
792 + const void *matchinfo,
795 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
802 + const struct ipt_p2p_info *info = matchinfo;
803 + unsigned char *haystack;
804 + struct iphdr *ip = skb->nh.iph;
805 + int p2p_result = 0, i = 0;
807 + int hlen = ntohs(ip->tot_len)-(ip->ihl*4); /*hlen = packet-data length*/
809 + /*must not be a fragment*/
811 + if (info->debug) printk("IPP2P.match: offset found %i \n",offset);
815 + /*make sure that skb is linear*/
816 + if(skb_is_nonlinear(skb)){
817 + if (info->debug) printk("IPP2P.match: nonlinear skb found\n");
822 + haystack=(char *)ip+(ip->ihl*4); /*haystack = packet data*/
824 + switch (ip->protocol){
825 + case IPPROTO_TCP: /*what to do with a TCP packet*/
827 + struct tcphdr *tcph = (void *) ip + ip->ihl * 4;
829 + if (tcph->fin) return 0; /*if FIN bit is set bail out*/
830 + if (tcph->syn) return 0; /*if SYN bit is set bail out*/
831 + if (tcph->rst) return 0; /*if RST bit is set bail out*/
833 + haystack += tcph->doff * 4; /*get TCP-Header-Size*/
834 + hlen -= tcph->doff * 4;
835 + while (matchlist[i].command) {
836 + if ((((info->cmd & matchlist[i].command) == matchlist[i].command) ||
837 + ((info->cmd & matchlist[i].short_hand) == matchlist[i].short_hand)) &&
838 + (hlen > matchlist[i].packet_len)) {
839 + p2p_result = matchlist[i].function_name(haystack, hlen);
842 + if (info->debug) printk("IPP2P.debug:TCP-match: %i from: %u.%u.%u.%u:%i to: %u.%u.%u.%u:%i Length: %i\n",
843 + p2p_result, NIPQUAD(ip->saddr),ntohs(tcph->source), NIPQUAD(ip->daddr),ntohs(tcph->dest),hlen);
852 + case IPPROTO_UDP: /*what to do with an UDP packet*/
854 + struct udphdr *udph = (void *) ip + ip->ihl * 4;
856 + while (udp_list[i].command){
857 + if ((((info->cmd & udp_list[i].command) == udp_list[i].command) ||
858 + ((info->cmd & udp_list[i].short_hand) == udp_list[i].short_hand)) &&
859 + (hlen > udp_list[i].packet_len)) {
860 + p2p_result = udp_list[i].function_name(haystack, hlen);
862 + if (info->debug) printk("IPP2P.debug:UDP-match: %i from: %u.%u.%u.%u:%i to: %u.%u.%u.%u:%i Length: %i\n",
863 + p2p_result, NIPQUAD(ip->saddr),ntohs(udph->source), NIPQUAD(ip->daddr),ntohs(udph->dest),hlen);
879 +checkentry(const char *tablename,
880 + const struct ipt_ip *ip,
882 + unsigned int matchsize,
883 + unsigned int hook_mask)
885 + /* Must specify -p tcp */
886 +/* if (ip->proto != IPPROTO_TCP || (ip->invflags & IPT_INV_PROTO)) {
887 + * printk("ipp2p: Only works on TCP packets, use -p tcp\n");
896 +static struct ipt_match ipp2p_match = {
897 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
905 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
908 + .checkentry = &checkentry,
914 +static int __init init(void)
916 + printk(KERN_INFO "IPP2P v%s loading\n", IPP2P_VERSION);
917 + return ipt_register_match(&ipp2p_match);
920 +static void __exit fini(void)
922 + ipt_unregister_match(&ipp2p_match);
923 + printk(KERN_INFO "IPP2P v%s unloaded\n", IPP2P_VERSION);