finally move buildroot-ng to trunk
[openwrt.git] / target / linux / generic-2.4 / patches / 601-netfilter_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 14:37:56.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/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
39 @@ -29,6 +29,7 @@
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
44   
45    dep_tristate '  DSCP match support' CONFIG_IP_NF_MATCH_DSCP $CONFIG_IP_NF_IPTABLES
46   
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
50 @@ -76,6 +76,7 @@
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
55  
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
61 @@ -0,0 +1,868 @@
62 +#if defined(MODVERSIONS)
63 +#include <linux/modversions.h>
64 +#endif
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>
69 +#include <net/tcp.h>
70 +#include <net/udp.h>
71 +
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))
75 +
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");
79 +
80 +
81 +/*Search for UDP eDonkey/eMule/Kad commands*/
82 +int
83 +udp_search_edk (unsigned char *haystack, int packet_len)
84 +{
85 +    unsigned char *t = haystack;
86 +    t += 8;
87 +
88 +       switch (t[0]) {
89 +               case 0xe3: 
90 +               {       /*edonkey*/
91 +                       switch (t[1]) 
92 +                       {
93 +                               /* client -> server status request */
94 +                               case 0x96: 
95 +                                       if (packet_len == 14) return ((IPP2P_EDK * 100) + 50);
96 +                                       break;
97 +                               /* server -> client status request */
98 +                               case 0x97: if (packet_len == 42) return ((IPP2P_EDK * 100) + 51);
99 +                                       break;
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);
103 +                                       break;
104 +                                               /* server description response */
105 +                                               /* e3 a3 ff f0 ..  | size > 40 && size < 200 */
106 +                               //case 0xa3: return ((IPP2P_EDK * 100) + 53);
107 +                               //      break;
108 +                               case 0x9a: if (packet_len==26) return ((IPP2P_EDK * 100) + 54);
109 +                                       break;
110 +
111 +                               case 0x92: if (packet_len==18) return ((IPP2P_EDK * 100) + 55);
112 +                                       break;
113 +                       }
114 +                       break;
115 +               }
116 +               case 0xe4: 
117 +               {
118 +                       switch (t[1]) 
119 +                       {
120 +                                               /* e4 20 .. | size == 43 */
121 +                               case 0x20: if ((packet_len == 43) && (t[2] != 0x00) && (t[34] != 0x00)) return ((IPP2P_EDK * 100) + 60);
122 +                                       break;
123 +                                               /* e4 00 .. 00 | size == 35 ? */
124 +                               case 0x00: if ((packet_len == 35) && (t[26] == 0x00)) return ((IPP2P_EDK * 100) + 61);
125 +                                       break;
126 +                                               /* e4 10 .. 00 | size == 35 ? */
127 +                               case 0x10: if ((packet_len == 35) && (t[26] == 0x00)) return ((IPP2P_EDK * 100) + 62);
128 +                                       break;
129 +                                               /* e4 18 .. 00 | size == 35 ? */
130 +                               case 0x18: if ((packet_len == 35) && (t[26] == 0x00)) return ((IPP2P_EDK * 100) + 63);
131 +                                       break;
132 +                                               /* e4 52 .. | size = 44 */
133 +                               case 0x52: if (packet_len == 44 ) return ((IPP2P_EDK * 100) + 64);
134 +                                       break;
135 +                                               /* e4 58 .. | size == 6 */
136 +                               case 0x58: if (packet_len == 14 ) return ((IPP2P_EDK * 100) + 65);
137 +                                       break;
138 +                                               /* e4 59 .. | size == 2 */
139 +                               case 0x59: if (packet_len == 10 )return ((IPP2P_EDK * 100) + 66);
140 +                                       break;
141 +                                       /* e4 28 .. | packet_len == 52,77,102,127... */
142 +                               case 0x28: if (((packet_len-52) % 25) == 0) return ((IPP2P_EDK * 100) + 67);
143 +                                       break;
144 +                                       /* e4 50 xx xx | size == 4 */
145 +                               case 0x50: if (packet_len == 12) return ((IPP2P_EDK * 100) + 68);
146 +                                       break;
147 +                                       /* e4 40 xx xx | size == 48 */
148 +                               case 0x40: if (packet_len == 56) return ((IPP2P_EDK * 100) + 69);
149 +                                       break;
150 +                       }
151 +                       break;
152 +               }
153 +       } /* end of switch (t[0]) */
154 +    return 0;
155 +}/*udp_search_edk*/
156 +
157 +
158 +/*Search for UDP Gnutella commands*/
159 +int
160 +udp_search_gnu (unsigned char *haystack, int packet_len)
161 +{
162 +    unsigned char *t = haystack;
163 +    t += 8;
164 +    
165 +    if (memcmp(t, "GND", 3) == 0) return ((IPP2P_GNU * 100) + 51);
166 +    if (memcmp(t, "GNUTELLA ", 9) == 0) return ((IPP2P_GNU * 100) + 52);
167 +    return 0;
168 +}/*udp_search_gnu*/
169 +
170 +
171 +/*Search for UDP KaZaA commands*/
172 +int
173 +udp_search_kazaa (unsigned char *haystack, int packet_len)
174 +{
175 +    unsigned char *t = haystack;
176 +    
177 +    if (t[packet_len-1] == 0x00){
178 +       t += (packet_len - 6);
179 +       if (memcmp(t, "KaZaA", 5) == 0) return (IPP2P_KAZAA * 100 +50);
180 +    }
181 +    
182 +    return 0;
183 +}/*udp_search_kazaa*/
184 +
185 +/*Search for UDP DirectConnect commands*/
186 +int
187 +udp_search_directconnect (unsigned char *haystack, int packet_len)
188 +{
189 +    unsigned char *t = haystack;
190 +    if ((*(t + 8) == 0x24) && (*(t + packet_len - 1) == 0x7c)) {
191 +       t+=8;
192 +       if (memcmp(t, "SR ", 3) == 0)                   return ((IPP2P_DC * 100) + 60);
193 +       if (memcmp(t, "Ping ", 5) == 0)                 return ((IPP2P_DC * 100) + 61);
194 +    }
195 +    return 0;
196 +}/*udp_search_directconnect*/
197 +
198 +
199 +
200 +/*Search for UDP BitTorrent commands*/
201 +int
202 +udp_search_bit (unsigned char *haystack, int packet_len)
203 +{
204 +       switch(packet_len)
205 +       {
206 +               case 24:
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);
210 +                       break;
211 +               case 44:
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);
216 +                       break;
217 +               case 65:
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);
222 +                       break;
223 +               case 67:
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);
228 +                       break;
229 +               case 211:
230 +                       if (get_u32(haystack, 8) == __constant_htonl(0x00000405)) 
231 +                               return (IPP2P_BIT * 100 + 54);
232 +                       break;
233 +               case 29:
234 +                       if ((get_u32(haystack, 8) == __constant_htonl(0x00000401))) 
235 +                               return (IPP2P_BIT * 100 + 55);
236 +                       break;
237 +               case 52:
238 +                       if (get_u32(haystack,8)  == __constant_htonl(0x00000827) &&
239 +                       get_u32(haystack,12) == __constant_htonl(0x37502950))
240 +                               return (IPP2P_BIT * 100 + 80);
241 +                       break;
242 +               default:
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);
246 +                       break;
247 +       }
248 +    
249 +       /* some extra-bitcomet rules:
250 +       * "d1:" [a|r] "d2:id20:"
251 +       */
252 +       if (packet_len > 30 && get_u8(haystack, 8) == 'd' && get_u8(haystack, 9) == '1' && get_u8(haystack, 10) == ':' )
253 +       {
254 +               if (get_u8(haystack, 11) == 'a' || get_u8(haystack, 11) == 'r')
255 +               {
256 +                       if (memcmp(haystack+12,"d2:id20:",8)==0)
257 +                               return (IPP2P_BIT * 100 + 57);
258 +               }
259 +       }
260 +    
261 +#if 0
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)
266 +       {
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);
272 +                       
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);
278 +               
279 +                   
280 +       }
281 +#endif
282 +
283 +    return 0;
284 +}/*udp_search_bit*/
285 +
286 +
287 +
288 +/*Search for Ares commands*/
289 +//#define IPP2P_DEBUG_ARES
290 +int
291 +search_ares (const unsigned char *payload, const u16 plen)
292 +//int search_ares (unsigned char *haystack, int packet_len, int head_len)
293 +{
294 +//     const unsigned char *t = haystack + head_len;
295 +       
296 +       /* all ares packets start with  */
297 +       if (payload[1] == 0 && (plen - payload[0]) == 3)
298 +       {
299 +               switch (payload[2])
300 +               {
301 +                       case 0x5a:
302 +                               /* ares connect */
303 +                               if ( plen == 6 && payload[5] == 0x05 ) return ((IPP2P_ARES * 100) + 1);
304 +                               break;
305 +                       case 0x09:
306 +                               /* ares search, min 3 chars --> 14 bytes
307 +                                * lets define a search can be up to 30 chars --> max 34 bytes
308 +                                */
309 +                               if ( plen >= 14 && plen <= 34 ) return ((IPP2P_ARES * 100) + 1);
310 +                               break;
311 +#ifdef IPP2P_DEBUG_ARES
312 +                       default:
313 +                       printk(KERN_DEBUG "Unknown Ares command %x recognized, len: %u \n", (unsigned int) payload[2],plen);
314 +#endif /* IPP2P_DEBUG_ARES */
315 +               }
316 +       }
317 +
318 +#if 0          
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);
324 +    }
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);
329 +       }
330 +    }
331 +#endif
332 +
333 +    return 0;
334 +} /*search_ares*/
335 +
336 +/*Search for SoulSeek commands*/
337 +int
338 +search_soul (const unsigned char *payload, const u16 plen)
339 +{
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 )
346 +       {
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);
351 +       }
352 +       
353 +        /* next match: 01 yy 00 00 | yy can be everything */
354 +        if ( get_u8(payload, 4) == 0x01 && get_u16(payload, 6) == 0x0000 )
355 +       {
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);
360 +       }
361 +       
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 */
365 +       switch(m)
366 +       {
367 +               case 7:
368 +               case 9:
369 +               case 22:
370 +               case 23:
371 +               case 26:
372 +               case 28:
373 +               case 50:
374 +               case 51:
375 +               case 60:
376 +               case 91:
377 +               case 92:
378 +               case 1001:
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);
383 +       }
384 +       
385 +       if (m > 0 && m < 6 ) 
386 +       {
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);
391 +       }
392 +       if (m > 12 && m < 19 )
393 +       {
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);
398 +       }
399 +
400 +       if (m > 34 && m < 38 )
401 +       {
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);
406 +       }
407 +
408 +       if (m > 39 && m < 47 )
409 +       {
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);
414 +       }
415 +
416 +       if (m > 61 && m < 70 ) 
417 +       {
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);
422 +       }
423 +
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 */
427 +    }
428 +       
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 )
432 +       {
433 +               __u32 y=get_u32(payload, 5);
434 +               /* we need 19 chars + string */
435 +               if ( (y + 19) <= (plen) )
436 +               {
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);
443 +               }
444 +       }
445 +    return 0;
446 +}
447 +
448 +
449 +/*Search for WinMX commands*/
450 +int
451 +search_winmx (const unsigned char *payload, const u16 plen)
452 +{
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;
458 +    
459 +    if ((memcmp(payload, "SEND", 4) == 0) || (memcmp(payload, "GET", 3) == 0)){
460 +        u16 c=4;
461 +        const u16 end=plen-2;
462 +        u8 count=0;
463 +        while (c < end)
464 +        {
465 +               if (payload[c]== 0x20 && payload[c+1] == 0x22)
466 +               {
467 +                       c++;
468 +                       count++;
469 +                       if (count>=2) return ((IPP2P_WINMX * 100) + 3);
470 +               }
471 +               c++;
472 +        }
473 +    }
474 +    
475 +    if ( plen == 149 && payload[0] == '8' )
476 +    {
477 +#ifdef IPP2P_DEBUG_WINMX
478 +       printk(KERN_INFO "maybe WinMX\n");
479 +#endif
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))
484 +           
485 +       {
486 +#ifdef IPP2P_DEBUG_WINMX
487 +               printk(KERN_INFO "got WinMX\n");
488 +#endif
489 +               return ((IPP2P_WINMX * 100) + 4);
490 +       }
491 +    }
492 +    return 0;
493 +} /*search_winmx*/
494 +
495 +
496 +/*Search for appleJuice commands*/
497 +int
498 +search_apple (const unsigned char *payload, const u16 plen)
499 +{
500 +    if ( (plen > 7) && (payload[6] == 0x0d) && (payload[7] == 0x0a) && (memcmp(payload, "ajprot", 6) == 0))  return (IPP2P_APPLE * 100);
501 +    
502 +    return 0;
503 +}
504 +
505 +
506 +/*Search for BitTorrent commands*/
507 +int
508 +search_bittorrent (const unsigned char *payload, const u16 plen)
509 +{
510 +    if (plen > 20)
511 +    {
512 +       /* test for match 0x13+"BitTorrent protocol" */
513 +       if (payload[0] == 0x13) 
514 +       {
515 +               if (memcmp(payload+1, "BitTorrent protocol", 19) == 0) return (IPP2P_BIT * 100);
516 +       }
517 +       
518 +       /* get tracker commandos, all starts with GET /
519 +       * then it can follow: scrape| announce
520 +       * and then ?hash_info=
521 +       */
522 +       if (memcmp(payload,"GET /",5) == 0)
523 +       {
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);
528 +       }
529 +    } 
530 +    else 
531 +    {
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);
536 +       
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);
539 +    }
540 +
541 +    return 0;
542 +}
543 +
544 +
545 +
546 +/*check for Kazaa get command*/
547 +int
548 +search_kazaa (const unsigned char *payload, const u16 plen)
549 +
550 +{
551 +    if ((payload[plen-2] == 0x0d) && (payload[plen-1] == 0x0a) && memcmp(payload, "GET /.hash=", 11) == 0)
552 +       return (IPP2P_DATA_KAZAA * 100);
553 +
554 +    return 0;
555 +}
556 +
557 +
558 +/*check for gnutella get command*/
559 +int
560 +search_gnu (const unsigned char *payload, const u16 plen)
561 +{
562 +    if ((payload[plen-2] == 0x0d) && (payload[plen-1] == 0x0a))
563 +    {
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); 
566 +    }
567 +    return 0;
568 +}
569 +
570 +
571 +/*check for gnutella get commands and other typical data*/
572 +int
573 +search_all_gnu (const unsigned char *payload, const u16 plen)
574 +{
575 +    
576 +    if ((payload[plen-2] == 0x0d) && (payload[plen-1] == 0x0a))
577 +    {
578 +       
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);    
581 +    
582 +    
583 +       if ((memcmp(payload, "GET /get/", 9) == 0) || (memcmp(payload, "GET /uri-res/", 13) == 0))
584 +       {        
585 +               u16 c=8;
586 +               const u16 end=plen-22;
587 +               while (c < end) {
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);
590 +                       c++;
591 +               }
592 +       }
593 +    }
594 +    return 0;
595 +}
596 +
597 +
598 +/*check for KaZaA download commands and other typical data*/
599 +int
600 +search_all_kazaa (const unsigned char *payload, const u16 plen)
601 +{
602 +    if ((payload[plen-2] == 0x0d) && (payload[plen-1] == 0x0a))
603 +    {
604 +
605 +       if (memcmp(payload, "GIVE ", 5) == 0) return ((IPP2P_KAZAA * 100) + 1);
606 +    
607 +       if (memcmp(payload, "GET /", 5) == 0) {
608 +               u16 c = 8;
609 +               const u16 end=plen-22;
610 +               while (c < end) {
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);
613 +                       c++;
614 +               }
615 +       }
616 +    }
617 +    return 0;
618 +}
619 +
620 +/*fast check for edonkey file segment transfer command*/
621 +int
622 +search_edk (const unsigned char *payload, const u16 plen)
623 +{
624 +    if (payload[0] != 0xe3) 
625 +       return 0;
626 +    else {
627 +       if (payload[5] == 0x47) 
628 +           return (IPP2P_DATA_EDK * 100);
629 +       else    
630 +           return 0;
631 +    }
632 +}
633 +
634 +
635 +
636 +/*intensive but slower search for some edonkey packets including size-check*/
637 +int
638 +search_all_edk (const unsigned char *payload, const u16 plen)
639 +{
640 +    if (payload[0] != 0xe3) 
641 +       return 0;
642 +    else {
643 +       //t += head_len;        
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*/
649 +           }
650 +       }
651 +       return 0;
652 +     }
653 +}
654 +
655 +
656 +/*fast check for Direct Connect send command*/
657 +int
658 +search_dc (const unsigned char *payload, const u16 plen)
659 +{
660 +
661 +    if (payload[0] != 0x24 ) 
662 +       return 0;
663 +    else {
664 +       if (memcmp(&payload[1], "Send|", 5) == 0)
665 +           return (IPP2P_DATA_DC * 100);
666 +       else
667 +           return 0;
668 +    }  
669 +
670 +}
671 +
672 +
673 +/*intensive but slower check for all direct connect packets*/
674 +int
675 +search_all_dc (const unsigned char *payload, const u16 plen)
676 +{
677 +//    unsigned char *t = haystack;
678 +
679 +    if (payload[0] == 0x24 && payload[plen-1] == 0x7c) 
680 +    {
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); 
686 +    }
687 +    return 0;
688 +}
689 +
690 +/*check for mute*/
691 +int
692 +search_mute (const unsigned char *payload, const u16 plen)
693 +{
694 +       if ( plen == 209 || plen == 345 || plen == 473 || plen == 609 || plen == 1121 )
695 +       {
696 +               //printk(KERN_DEBUG "size hit: %u",size);
697 +               if (memcmp(payload,"PublicKey: ",11) == 0 )
698 +               { 
699 +                       return ((IPP2P_MUTE * 100) + 0);
700 +                       
701 +/*                     if (memcmp(t+size-14,"\x0aEndPublicKey\x0a",14) == 0)
702 +                       {
703 +                               printk(KERN_DEBUG "end pubic key hit: %u",size);
704 +                               
705 +                       }*/
706 +               }
707 +       }
708 +       return 0;
709 +}
710 +
711 +
712 +/* check for xdcc */
713 +int
714 +search_xdcc (const unsigned char *payload, const u16 plen)
715 +{
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)
718 +       {
719 +               
720 +               u16 x=10;
721 +               const u16 end=plen - 13;
722 +               
723 +               /* is seems to be a irc private massage, chedck for xdcc command */
724 +               while (x < end)
725 +               {
726 +                       if (payload[x] == ':')
727 +                       {
728 +                               if ( memcmp(&payload[x+1],"xdcc send #",11) == 0 )
729 +                                       return ((IPP2P_XDCC * 100) + 0);
730 +                       }
731 +                       x++;
732 +               }
733 +       }
734 +       return 0;
735 +}
736 +
737 +/* search for waste */
738 +int search_waste(const unsigned char *payload, const u16 plen)
739 +{
740 +       if ( plen >= 8 && memcmp(payload,"GET.sha1:",9) == 0)
741 +               return ((IPP2P_WASTE * 100) + 0);
742 +
743 +       return 0;
744 +}
745 +
746 +
747 +static struct {
748 +    int command;
749 +    __u8 short_hand;                   /*for fucntions included in short hands*/
750 +    int packet_len;
751 +    int (*function_name) (const unsigned char *, const u16);
752 +} matchlist[] = {
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},
769 +    {0,0,0,NULL}
770 +};
771 +
772 +
773 +static struct {
774 +    int command;
775 +    __u8 short_hand;                   /*for fucntions included in short hands*/
776 +    int packet_len;
777 +    int (*function_name) (unsigned char *, int);
778 +} udp_list[] = {
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},    
784 +    {0,0,0,NULL}
785 +};
786 +
787 +
788 +static int
789 +match(const struct sk_buff *skb,
790 +      const struct net_device *in,
791 +      const struct net_device *out,
792 +      const void *matchinfo,
793 +      int offset,
794 +
795 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
796 +      const void *hdr,
797 +      u_int16_t datalen,
798 +#endif
799 +
800 +      int *hotdrop)
801 +{
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;
806 +//    int head_len;
807 +    int hlen = ntohs(ip->tot_len)-(ip->ihl*4); /*hlen = packet-data length*/
808 +
809 +    /*must not be a fragment*/
810 +    if (offset) {
811 +       if (info->debug) printk("IPP2P.match: offset found %i \n",offset);
812 +       return 0;
813 +    }
814 +    
815 +    /*make sure that skb is linear*/
816 +    if(skb_is_nonlinear(skb)){
817 +       if (info->debug) printk("IPP2P.match: nonlinear skb found\n");
818 +       return 0;
819 +    }
820 +
821 +
822 +    haystack=(char *)ip+(ip->ihl*4);           /*haystack = packet data*/
823 +
824 +    switch (ip->protocol){
825 +       case IPPROTO_TCP:               /*what to do with a TCP packet*/
826 +       {
827 +           struct tcphdr *tcph = (void *) ip + ip->ihl * 4;
828 +           
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*/
832 +           
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);
840 +                           if (p2p_result) 
841 +                           {
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);
844 +                               return p2p_result;
845 +                           }
846 +               }
847 +           i++;
848 +           }
849 +           return p2p_result;
850 +       }
851 +       
852 +       case IPPROTO_UDP:               /*what to do with an UDP packet*/
853 +       {
854 +           struct udphdr *udph = (void *) ip + ip->ihl * 4;
855 +           
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);
861 +                           if (p2p_result){
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);
864 +                               return p2p_result;
865 +                           }
866 +               }
867 +           i++;
868 +           }                   
869 +           return p2p_result;
870 +       }
871 +    
872 +       default: return 0;
873 +    }
874 +}
875 +
876 +
877 +
878 +static int
879 +checkentry(const char *tablename,
880 +            const struct ipt_ip *ip,
881 +           void *matchinfo,
882 +           unsigned int matchsize,
883 +           unsigned int hook_mask)
884 +{
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");
888 + *     return 0;
889 + *    }*/
890 +    return 1;
891 +}
892 +                                                                           
893 +
894 +
895 +
896 +static struct ipt_match ipp2p_match = { 
897 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
898 +       { NULL, NULL }, 
899 +       "ipp2p", 
900 +       &match, 
901 +       &checkentry, 
902 +       NULL, 
903 +       THIS_MODULE
904 +#endif
905 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
906 +       .name           = "ipp2p",
907 +       .match          = &match,
908 +       .checkentry     = &checkentry,
909 +       .me             = THIS_MODULE,
910 +#endif
911 +};
912 +
913 +
914 +static int __init init(void)
915 +{
916 +    printk(KERN_INFO "IPP2P v%s loading\n", IPP2P_VERSION);
917 +    return ipt_register_match(&ipp2p_match);
918 +}
919 +       
920 +static void __exit fini(void)
921 +{
922 +    ipt_unregister_match(&ipp2p_match);
923 +    printk(KERN_INFO "IPP2P v%s unloaded\n", IPP2P_VERSION);    
924 +}
925 +       
926 +module_init(init);
927 +module_exit(fini);
928 +
929 +