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