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