Updates to Tomato RAF including NGINX && PHP
[tomato.git] / release / src / router / iptables / extensions / libipt_ipp2p_K26.c
blobee28270afa9a2f0294004194b4f90c2757629c66
1 #include <stdio.h>
2 #include <netdb.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <getopt.h>
6 #include <ctype.h>
8 #include <iptables.h>
10 #include <linux/netfilter_ipv4/ipt_ipp2p.h>
12 static void
13 help(void)
15 printf(
16 "IPP2P v%s options:\n"
17 " --ipp2p\tGrab all known p2p packets\n"
18 " --edk\t\t[TCP&UDP]\tAll known eDonkey/eMule/Overnet packets\n"
19 " --dc\t\t[TCP]\t\tAll known Direct Connect packets\n"
20 " --kazaa\t[TCP&UDP]\tAll known KaZaA packets\n"
21 " --gnu\t\t[TCP&UDP]\tAll known Gnutella packets\n"
22 " --bit\t\t[TCP&UDP]\tAll known BitTorrent packets\n"
23 " --apple\t[TCP]\t\tAll known AppleJuice packets\n"
24 " --winmx\t[TCP]\t\tAll known WinMX\n"
25 " --soul\t\t[TCP]\t\tAll known SoulSeek\n"
26 " --ares\t\t[TCP]\t\tAll known Ares\n"
27 " --pp\t\t[TCP&UDP]\tAll known PPLive/PPStream/UUSee/QQLive packets (Please send feedback to: liulei@syiae.edu.cn)\n"
28 " --xunlei\t[TCP]\t\tAll known xunlei/QQCyclone packets\n"
29 " EXPERIMENTAL protocols (please send feedback to: ipp2p@ipp2p.org) :\n"
30 " --mute\t\t[TCP]\t\tAll known Mute packets\n"
31 " --waste\t[TCP]\t\tAll known Waste packets\n"
32 " --xdcc\t\t[TCP]\t\tAll known XDCC packets (only xdcc login)\n\n"
33 " DEBUG SUPPPORT, use only if you know why\n"
34 " --debug\t\tGenerate kernel debug output, THIS WILL SLOW DOWN THE FILTER\n"
35 "\nNote that the follwing options will have the same meaning:\n"
36 " '--ipp2p' is equal to '--edk --dc --kazaa --gnu --bit --apple --winmx --soul --ares --pp'\n"
37 "\nIPP2P was intended for TCP only. Due to increasing usage of UDP we needed to change this.\n"
38 "You can now use -p udp to search UDP packets only or without -p switch to search UDP and TCP packets.\n"
39 "\nSee README included with this package for more details or visit http://www.ipp2p.org\n"
40 "\nExamples:\n"
41 " iptables -A FORWARD -m ipp2p --ipp2p -j MARK --set-mark 0x01\n"
42 " iptables -A FORWARD -p udp -m ipp2p --kazaa --bit -j DROP\n"
43 " iptables -A FORWARD -p tcp -m ipp2p --edk --soul -j DROP\n\n"
44 , IPP2P_VERSION);
47 static struct option opts[] = {
48 { "ipp2p", 0, 0, '1' },
49 { "edk", 0, 0, '2' },
50 { "pp", 0, 0, '3' },
51 { "xunlei", 0, 0, '4'},
52 { "dc", 0, 0, '7' },
53 { "gnu", 0, 0, '9' },
54 { "kazaa", 0, 0, 'a' },
55 { "bit", 0, 0, 'b' },
56 { "apple", 0, 0, 'c' },
57 { "soul", 0, 0, 'd' },
58 { "winmx", 0, 0, 'e' },
59 { "ares", 0, 0, 'f' },
60 { "mute", 0, 0, 'g' },
61 { "waste", 0, 0, 'h' },
62 { "xdcc", 0, 0, 'i' },
63 { "debug", 0, 0, 'j' },
64 {0}
69 static void
70 init(struct ipt_entry_match *m, unsigned int *nfcache)
72 struct ipt_p2p_info *info = (struct ipt_p2p_info *)m->data;
74 *nfcache |= NFC_UNKNOWN;
76 /*init the module with default values*/
77 info->cmd = 0;
78 info->debug = 0;
83 static int
84 parse(int c, char **argv, int invert, unsigned int *flags,
85 const struct ipt_entry *entry,
86 unsigned int *nfcache,
87 struct ipt_entry_match **match)
89 struct ipt_p2p_info *info = (struct ipt_p2p_info *)(*match)->data;
91 switch (c) {
92 case '1': /*cmd: ipp2p*/
93 if ((*flags & SHORT_HAND_IPP2P) == SHORT_HAND_IPP2P)
94 exit_error(PARAMETER_PROBLEM,
95 "ipp2p: `--ipp2p' may only be "
96 "specified once!");
97 if ((*flags) != 0)
98 exit_error(PARAMETER_PROBLEM,
99 "ipp2p: `--ipp2p' may only be "
100 "specified alone!");
101 if (invert) exit_error(PARAMETER_PROBLEM, "ipp2p: invert [!] is not allowed!");
102 *flags += SHORT_HAND_IPP2P;
103 info->cmd = *flags;
104 break;
106 case '2': /*cmd: edk*/
107 if ((*flags & IPP2P_EDK) == IPP2P_EDK)
108 exit_error(PARAMETER_PROBLEM,
109 "ipp2p: `--edk' may only be "
110 "specified once");
111 if ((*flags & SHORT_HAND_IPP2P) == SHORT_HAND_IPP2P)
112 exit_error(PARAMETER_PROBLEM,
113 "ipp2p: `--ipp2p' may only be "
114 "specified alone!");
115 if (invert) exit_error(PARAMETER_PROBLEM, "ipp2p: invert [!] is not allowed!");
116 *flags += IPP2P_EDK;
117 info->cmd = *flags;
118 break;
120 case '3': /*cmd: pp*/
121 if ((*flags & IPP2P_PP) == IPP2P_PP)
122 exit_error(PARAMETER_PROBLEM,
123 "ipp2p: `--pp' may only be "
124 "specified once!");
125 if ((*flags & SHORT_HAND_IPP2P) == SHORT_HAND_IPP2P)
126 exit_error(PARAMETER_PROBLEM,
127 "ipp2p: `--ipp2p' may only be "
128 "specified alone!");
129 if (invert) exit_error(PARAMETER_PROBLEM, "ipp2p: invert [!] is not allowed!");
130 *flags += IPP2P_PP;
131 info->cmd = *flags;
132 break;
134 case '4':
135 if ((*flags & IPP2P_XUNLEI) == IPP2P_XUNLEI)
136 exit_error(PARAMETER_PROBLEM,
137 "ipp2p: `--xunlei' may only be "
138 "specified once!");
139 if (invert) exit_error(PARAMETER_PROBLEM, "ipp2p: invert [!] is not allowed!");
140 *flags += IPP2P_XUNLEI;
141 info->cmd = *flags;
142 break;
144 case '7': /*cmd: dc*/
145 if ((*flags & IPP2P_DC) == IPP2P_DC)
146 exit_error(PARAMETER_PROBLEM,
147 "ipp2p: `--dc' may only be "
148 "specified once!");
149 if ((*flags & SHORT_HAND_IPP2P) == SHORT_HAND_IPP2P)
150 exit_error(PARAMETER_PROBLEM,
151 "ipp2p: `--ipp2p' may only be "
152 "specified alone!");
153 if (invert) exit_error(PARAMETER_PROBLEM, "ipp2p: invert [!] is not allowed!");
154 *flags += IPP2P_DC;
155 info->cmd = *flags;
156 break;
159 case '9': /*cmd: gnu*/
160 if ((*flags & IPP2P_GNU) == IPP2P_GNU)
161 exit_error(PARAMETER_PROBLEM,
162 "ipp2p: `--gnu' may only be "
163 "specified once!");
164 if ((*flags & SHORT_HAND_IPP2P) == SHORT_HAND_IPP2P)
165 exit_error(PARAMETER_PROBLEM,
166 "ipp2p: `--ipp2p' may only be "
167 "specified alone!");
168 if (invert) exit_error(PARAMETER_PROBLEM, "ipp2p: invert [!] is not allowed!");
169 *flags += IPP2P_GNU;
170 info->cmd = *flags;
171 break;
173 case 'a': /*cmd: kazaa*/
174 if ((*flags & IPP2P_KAZAA) == IPP2P_KAZAA)
175 exit_error(PARAMETER_PROBLEM,
176 "ipp2p: `--kazaa' may only be "
177 "specified once!");
178 if ((*flags & SHORT_HAND_IPP2P) == SHORT_HAND_IPP2P)
179 exit_error(PARAMETER_PROBLEM,
180 "ipp2p: `--ipp2p' may only be "
181 "specified alone!");
182 if (invert) exit_error(PARAMETER_PROBLEM, "ipp2p: invert [!] is not allowed!");
183 *flags += IPP2P_KAZAA;
184 info->cmd = *flags;
185 break;
187 case 'b': /*cmd: bit*/
188 if ((*flags & IPP2P_BIT) == IPP2P_BIT)
189 exit_error(PARAMETER_PROBLEM,
190 "ipp2p: `--bit' may only be "
191 "specified once!");
192 if ((*flags & SHORT_HAND_IPP2P) == SHORT_HAND_IPP2P)
193 exit_error(PARAMETER_PROBLEM,
194 "ipp2p: `--ipp2p' may only be "
195 "specified alone!");
196 if (invert) exit_error(PARAMETER_PROBLEM, "ipp2p: invert [!] is not allowed!");
197 *flags += IPP2P_BIT;
198 info->cmd = *flags;
199 break;
201 case 'c': /*cmd: apple*/
202 if ((*flags & IPP2P_APPLE) == IPP2P_APPLE)
203 exit_error(PARAMETER_PROBLEM,
204 "ipp2p: `--apple' may only be "
205 "specified once!");
206 if ((*flags & SHORT_HAND_IPP2P) == SHORT_HAND_IPP2P)
207 exit_error(PARAMETER_PROBLEM,
208 "ipp2p: `--ipp2p' may only be "
209 "specified alone!");
210 if (invert) exit_error(PARAMETER_PROBLEM, "ipp2p: invert [!] is not allowed!");
211 *flags += IPP2P_APPLE;
212 info->cmd = *flags;
213 break;
216 case 'd': /*cmd: soul*/
217 if ((*flags & IPP2P_SOUL) == IPP2P_SOUL)
218 exit_error(PARAMETER_PROBLEM,
219 "ipp2p: `--soul' may only be "
220 "specified once!");
221 if ((*flags & SHORT_HAND_IPP2P) == SHORT_HAND_IPP2P)
222 exit_error(PARAMETER_PROBLEM,
223 "ipp2p: `--ipp2p' may only be "
224 "specified alone!");
225 if (invert) exit_error(PARAMETER_PROBLEM, "ipp2p: invert [!] is not allowed!");
226 *flags += IPP2P_SOUL;
227 info->cmd = *flags;
228 break;
231 case 'e': /*cmd: winmx*/
232 if ((*flags & IPP2P_WINMX) == IPP2P_WINMX)
233 exit_error(PARAMETER_PROBLEM,
234 "ipp2p: `--winmx' may only be "
235 "specified once!");
236 if ((*flags & SHORT_HAND_IPP2P) == SHORT_HAND_IPP2P)
237 exit_error(PARAMETER_PROBLEM,
238 "ipp2p: `--ipp2p' may only be "
239 "specified alone!");
240 if (invert) exit_error(PARAMETER_PROBLEM, "ipp2p: invert [!] is not allowed!");
241 *flags += IPP2P_WINMX;
242 info->cmd = *flags;
243 break;
245 case 'f': /*cmd: ares*/
246 if ((*flags & IPP2P_ARES) == IPP2P_ARES)
247 exit_error(PARAMETER_PROBLEM,
248 "ipp2p: `--ares' may only be "
249 "specified once!");
250 if ((*flags & SHORT_HAND_IPP2P) == SHORT_HAND_IPP2P)
251 exit_error(PARAMETER_PROBLEM,
252 "ipp2p: `--ipp2p' may only be "
253 "specified alone!");
254 if (invert) exit_error(PARAMETER_PROBLEM, "ipp2p: invert [!] is not allowed!");
255 *flags += IPP2P_ARES;
256 info->cmd = *flags;
257 break;
259 case 'g': /*cmd: mute*/
260 if ((*flags & IPP2P_MUTE) == IPP2P_MUTE)
261 exit_error(PARAMETER_PROBLEM,
262 "ipp2p: `--mute' may only be "
263 "specified once!");
264 if (invert) exit_error(PARAMETER_PROBLEM, "ipp2p: invert [!] is not allowed!");
265 *flags += IPP2P_MUTE;
266 info->cmd = *flags;
267 break;
268 case 'h': /*cmd: waste*/
269 if ((*flags & IPP2P_WASTE) == IPP2P_WASTE)
270 exit_error(PARAMETER_PROBLEM,
271 "ipp2p: `--waste' may only be "
272 "specified once!");
273 if (invert) exit_error(PARAMETER_PROBLEM, "ipp2p: invert [!] is not allowed!");
274 *flags += IPP2P_WASTE;
275 info->cmd = *flags;
276 break;
277 case 'i': /*cmd: xdcc*/
278 if ((*flags & IPP2P_XDCC) == IPP2P_XDCC)
279 exit_error(PARAMETER_PROBLEM,
280 "ipp2p: `--xdcc' may only be "
281 "specified once!");
282 if (invert) exit_error(PARAMETER_PROBLEM, "ipp2p: invert [!] is not allowed!");
283 *flags += IPP2P_XDCC;
284 info->cmd = *flags;
285 break;
287 case 'j': /*cmd: debug*/
288 if (invert) exit_error(PARAMETER_PROBLEM, "ipp2p: invert [!] is not allowed!");
289 info->debug = 1;
290 break;
292 default:
293 // exit_error(PARAMETER_PROBLEM,
294 // "\nipp2p-parameter problem: for ipp2p usage type: iptables -m ipp2p --help\n");
295 return 0;
297 return 1;
301 static void
302 final_check(unsigned int flags)
304 if (!flags)
305 exit_error(PARAMETER_PROBLEM,
306 "\nipp2p-parameter problem: for ipp2p usage type: iptables -m ipp2p --help\n");
311 static void
312 print(const struct ipt_ip *ip,
313 const struct ipt_entry_match *match,
314 int numeric)
316 struct ipt_p2p_info *info = (struct ipt_p2p_info *)match->data;
318 printf("ipp2p v%s", IPP2P_VERSION);
319 if ((info->cmd & SHORT_HAND_IPP2P) == SHORT_HAND_IPP2P) printf(" --ipp2p");
320 if ((info->cmd & IPP2P_KAZAA) == IPP2P_KAZAA) printf(" --kazaa");
321 if ((info->cmd & IPP2P_GNU) == IPP2P_GNU) printf(" --gnu");
322 if ((info->cmd & IPP2P_EDK) == IPP2P_EDK) printf(" --edk");
323 if ((info->cmd & IPP2P_DC) == IPP2P_DC) printf(" --dc");
324 if ((info->cmd & IPP2P_BIT) == IPP2P_BIT) printf(" --bit");
325 if ((info->cmd & IPP2P_PP) == IPP2P_PP) printf(" --pp");
326 if ((info->cmd & IPP2P_XUNLEI) == IPP2P_XUNLEI) printf(" --xunlei");
327 if ((info->cmd & IPP2P_APPLE) == IPP2P_APPLE) printf(" --apple");
328 if ((info->cmd & IPP2P_SOUL) == IPP2P_SOUL) printf(" --soul");
329 if ((info->cmd & IPP2P_WINMX) == IPP2P_WINMX) printf(" --winmx");
330 if ((info->cmd & IPP2P_ARES) == IPP2P_ARES) printf(" --ares");
331 if ((info->cmd & IPP2P_MUTE) == IPP2P_MUTE) printf(" --mute");
332 if ((info->cmd & IPP2P_WASTE) == IPP2P_WASTE) printf(" --waste");
333 if ((info->cmd & IPP2P_XDCC) == IPP2P_XDCC) printf(" --xdcc");
334 if (info->debug != 0) printf(" --debug");
335 printf(" ");
340 static void
341 save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
343 struct ipt_p2p_info *info = (struct ipt_p2p_info *)match->data;
345 if ((info->cmd & SHORT_HAND_IPP2P) == SHORT_HAND_IPP2P) printf("--ipp2p ");
346 if ((info->cmd & IPP2P_KAZAA) == IPP2P_KAZAA) printf("--kazaa ");
347 if ((info->cmd & IPP2P_GNU) == IPP2P_GNU) printf("--gnu ");
348 if ((info->cmd & IPP2P_EDK) == IPP2P_EDK) printf("--edk ");
349 if ((info->cmd & IPP2P_DC) == IPP2P_DC) printf("--dc ");
350 if ((info->cmd & IPP2P_BIT) == IPP2P_BIT) printf("--bit ");
351 if ((info->cmd & IPP2P_PP) == IPP2P_PP) printf("--pp ");
352 if ((info->cmd & IPP2P_XUNLEI) == IPP2P_XUNLEI) printf("--xunlei ");
353 if ((info->cmd & IPP2P_APPLE) == IPP2P_APPLE) printf("--apple ");
354 if ((info->cmd & IPP2P_SOUL) == IPP2P_SOUL) printf("--soul ");
355 if ((info->cmd & IPP2P_WINMX) == IPP2P_WINMX) printf("--winmx ");
356 if ((info->cmd & IPP2P_ARES) == IPP2P_ARES) printf("--ares ");
357 if ((info->cmd & IPP2P_MUTE) == IPP2P_MUTE) printf("--mute ");
358 if ((info->cmd & IPP2P_WASTE) == IPP2P_WASTE) printf("--waste ");
359 if ((info->cmd & IPP2P_XDCC) == IPP2P_XDCC) printf("--xdcc ");
360 if (info->debug != 0) printf("--debug ");
366 static
367 struct iptables_match ipp2p=
369 .next = NULL,
370 .name = "ipp2p",
371 .version = IPTABLES_VERSION,
372 .size = IPT_ALIGN(sizeof(struct ipt_p2p_info)),
373 .userspacesize = IPT_ALIGN(sizeof(struct ipt_p2p_info)),
374 .help = &help,
375 .init = &init,
376 .parse = &parse,
377 .final_check = &final_check,
378 .print = &print,
379 .save = &save,
380 .extra_opts = opts
385 void _init(void)
387 register_match(&ipp2p);