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