• Home
  • History
  • Annotate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/ap/gpl/iptables-1.4.12.1/extensions/

Lines Matching defs:*

0 /* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
2 * Patrick Schaaf <bof@bof.de>
3 * Martin Josefsson <gandalf@wlug.westbo.se>
4 * Copyright (C) 2003-2010 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 /* Shared library add-on to iptables to add IP set mangling target. */
12 #include <stdbool.h>
13 #include <stdio.h>
14 #include <netdb.h>
15 #include <string.h>
16 #include <stdlib.h>
17 #include <getopt.h>
18 #include <ctype.h>
20 #include <xtables.h>
21 #include <linux/netfilter/xt_set.h>
22 #include "libxt_set.h"
24 /* Revision 0 */
26 static void
27 set_target_help_v0(void)
29 printf("SET target options:\n"
30 " --add-set name flags\n"
31 " --del-set name flags\n"
32 " add/del src/dst IP/port from/to named sets,\n"
33 " where flags are the comma separated list of\n"
34 " 'src' and 'dst' specifications.\n");
37 static const struct option set_target_opts_v0[] = {
38 {.name = "add-set", .has_arg = true, .val = '1'},
39 {.name = "del-set", .has_arg = true, .val = '2'},
40 XT_GETOPT_TABLEEND,
43 static void
44 set_target_check_v0(unsigned int flags)
46 if (!flags)
47 xtables_error(PARAMETER_PROBLEM,
48 "You must specify either `--add-set' or `--del-set'");
51 static void
52 set_target_init_v0(struct xt_entry_target *target)
54 struct xt_set_info_target_v0 *info =
55 (struct xt_set_info_target_v0 *) target->data;
57 info->add_set.index =
58 info->del_set.index = IPSET_INVALID_ID;
62 static void
63 parse_target_v0(char **argv, int invert, unsigned int *flags,
64 struct xt_set_info_v0 *info, const char *what)
66 if (info->u.flags[0])
67 xtables_error(PARAMETER_PROBLEM,
68 "--%s can be specified only once", what);
70 if (!argv[optind]
71 || argv[optind][0] == '-' || argv[optind][0] == '!')
72 xtables_error(PARAMETER_PROBLEM,
73 "--%s requires two args.", what);
75 if (strlen(optarg) > IPSET_MAXNAMELEN - 1)
76 xtables_error(PARAMETER_PROBLEM,
77 "setname `%s' too long, max %d characters.",
78 optarg, IPSET_MAXNAMELEN - 1);
80 get_set_byname(optarg, (struct xt_set_info *)info);
81 parse_dirs_v0(argv[optind], info);
82 optind++;
84 *flags = 1;
87 static int
88 set_target_parse_v0(int c, char **argv, int invert, unsigned int *flags,
89 const void *entry, struct xt_entry_target **target)
91 struct xt_set_info_target_v0 *myinfo =
92 (struct xt_set_info_target_v0 *) (*target)->data;
94 switch (c) {
95 case '1': /* --add-set <set> <flags> */
96 parse_target_v0(argv, invert, flags,
97 &myinfo->add_set, "add-set");
98 break;
99 case '2': /* --del-set <set>[:<flags>] <flags> */
100 parse_target_v0(argv, invert, flags,
101 &myinfo->del_set, "del-set");
102 break;
104 return 1;
107 static void
108 print_target_v0(const char *prefix, const struct xt_set_info_v0 *info)
110 int i;
111 char setname[IPSET_MAXNAMELEN];
113 if (info->index == IPSET_INVALID_ID)
114 return;
115 get_set_byid(setname, info->index);
116 printf(" %s %s", prefix, setname);
117 for (i = 0; i < IPSET_DIM_MAX; i++) {
118 if (!info->u.flags[i])
119 break;
120 printf("%s%s",
121 i == 0 ? " " : ",",
122 info->u.flags[i] & IPSET_SRC ? "src" : "dst");
126 static void
127 set_target_print_v0(const void *ip, const struct xt_entry_target *target,
128 int numeric)
130 const struct xt_set_info_target_v0 *info = (const void *)target->data;
132 print_target_v0("add-set", &info->add_set);
133 print_target_v0("del-set", &info->del_set);
136 static void
137 set_target_save_v0(const void *ip, const struct xt_entry_target *target)
139 const struct xt_set_info_target_v0 *info = (const void *)target->data;
141 print_target_v0("--add-set", &info->add_set);
142 print_target_v0("--del-set", &info->del_set);
145 /* Revision 1 */
146 static void
147 set_target_init_v1(struct xt_entry_target *target)
149 struct xt_set_info_target_v1 *info =
150 (struct xt_set_info_target_v1 *) target->data;
152 info->add_set.index =
153 info->del_set.index = IPSET_INVALID_ID;
157 #define SET_TARGET_ADD 0x1
158 #define SET_TARGET_DEL 0x2
159 #define SET_TARGET_EXIST 0x4
160 #define SET_TARGET_TIMEOUT 0x8
162 static void
163 parse_target(char **argv, int invert, struct xt_set_info *info,
164 const char *what)
166 if (info->dim)
167 xtables_error(PARAMETER_PROBLEM,
168 "--%s can be specified only once", what);
169 if (!argv[optind]
170 || argv[optind][0] == '-' || argv[optind][0] == '!')
171 xtables_error(PARAMETER_PROBLEM,
172 "--%s requires two args.", what);
174 if (strlen(optarg) > IPSET_MAXNAMELEN - 1)
175 xtables_error(PARAMETER_PROBLEM,
176 "setname `%s' too long, max %d characters.",
177 optarg, IPSET_MAXNAMELEN - 1);
179 get_set_byname(optarg, info);
180 parse_dirs(argv[optind], info);
181 optind++;
184 static int
185 set_target_parse_v1(int c, char **argv, int invert, unsigned int *flags,
186 const void *entry, struct xt_entry_target **target)
188 struct xt_set_info_target_v1 *myinfo =
189 (struct xt_set_info_target_v1 *) (*target)->data;
191 switch (c) {
192 case '1': /* --add-set <set> <flags> */
193 parse_target(argv, invert, &myinfo->add_set, "add-set");
194 *flags |= SET_TARGET_ADD;
195 break;
196 case '2': /* --del-set <set>[:<flags>] <flags> */
197 parse_target(argv, invert, &myinfo->del_set, "del-set");
198 *flags |= SET_TARGET_DEL;
199 break;
201 return 1;
204 static void
205 print_target(const char *prefix, const struct xt_set_info *info)
207 int i;
208 char setname[IPSET_MAXNAMELEN];
210 if (info->index == IPSET_INVALID_ID)
211 return;
212 get_set_byid(setname, info->index);
213 printf(" %s %s", prefix, setname);
214 for (i = 1; i <= info->dim; i++) {
215 printf("%s%s",
216 i == 1 ? " " : ",",
217 info->flags & (1 << i) ? "src" : "dst");
221 static void
222 set_target_print_v1(const void *ip, const struct xt_entry_target *target,
223 int numeric)
225 const struct xt_set_info_target_v1 *info = (const void *)target->data;
227 print_target("add-set", &info->add_set);
228 print_target("del-set", &info->del_set);
231 static void
232 set_target_save_v1(const void *ip, const struct xt_entry_target *target)
234 const struct xt_set_info_target_v1 *info = (const void *)target->data;
236 print_target("--add-set", &info->add_set);
237 print_target("--del-set", &info->del_set);
240 /* Revision 2 */
242 static void
243 set_target_help_v2(void)
245 printf("SET target options:\n"
246 " --add-set name flags [--exist] [--timeout n]\n"
247 " --del-set name flags\n"
248 " add/del src/dst IP/port from/to named sets,\n"
249 " where flags are the comma separated list of\n"
250 " 'src' and 'dst' specifications.\n");
253 static const struct option set_target_opts_v2[] = {
254 {.name = "add-set", .has_arg = true, .val = '1'},
255 {.name = "del-set", .has_arg = true, .val = '2'},
256 {.name = "exist", .has_arg = false, .val = '3'},
257 {.name = "timeout", .has_arg = true, .val = '4'},
258 XT_GETOPT_TABLEEND,
261 static void
262 set_target_check_v2(unsigned int flags)
264 if (!(flags & (SET_TARGET_ADD|SET_TARGET_DEL)))
265 xtables_error(PARAMETER_PROBLEM,
266 "You must specify either `--add-set' or `--del-set'");
267 if (!(flags & SET_TARGET_ADD)) {
268 if (flags & SET_TARGET_EXIST)
269 xtables_error(PARAMETER_PROBLEM,
270 "Flag `--exist' can be used with `--add-set' only");
271 if (flags & SET_TARGET_TIMEOUT)
272 xtables_error(PARAMETER_PROBLEM,
273 "Option `--timeout' can be used with `--add-set' only");
278 static void
279 set_target_init_v2(struct xt_entry_target *target)
281 struct xt_set_info_target_v2 *info =
282 (struct xt_set_info_target_v2 *) target->data;
284 info->add_set.index =
285 info->del_set.index = IPSET_INVALID_ID;
286 info->timeout = UINT32_MAX;
289 static int
290 set_target_parse_v2(int c, char **argv, int invert, unsigned int *flags,
291 const void *entry, struct xt_entry_target **target)
293 struct xt_set_info_target_v2 *myinfo =
294 (struct xt_set_info_target_v2 *) (*target)->data;
295 unsigned int timeout;
297 switch (c) {
298 case '1': /* --add-set <set> <flags> */
299 parse_target(argv, invert, &myinfo->add_set, "add-set");
300 *flags |= SET_TARGET_ADD;
301 break;
302 case '2': /* --del-set <set>[:<flags>] <flags> */
303 parse_target(argv, invert, &myinfo->del_set, "del-set");
304 *flags |= SET_TARGET_DEL;
305 break;
306 case '3':
307 myinfo->flags |= IPSET_FLAG_EXIST;
308 *flags |= SET_TARGET_EXIST;
309 break;
310 case '4':
311 if (!xtables_strtoui(optarg, NULL, &timeout, 0, UINT32_MAX - 1))
312 xtables_error(PARAMETER_PROBLEM,
313 "Invalid value for option --timeout "
314 "or out of range 0-%u", UINT32_MAX - 1);
315 myinfo->timeout = timeout;
316 *flags |= SET_TARGET_TIMEOUT;
317 break;
319 return 1;
322 static void
323 set_target_print_v2(const void *ip, const struct xt_entry_target *target,
324 int numeric)
326 const struct xt_set_info_target_v2 *info = (const void *)target->data;
328 print_target("add-set", &info->add_set);
329 if (info->flags & IPSET_FLAG_EXIST)
330 printf(" exist");
331 if (info->timeout != UINT32_MAX)
332 printf(" timeout %u", info->timeout);
333 print_target("del-set", &info->del_set);
336 static void
337 set_target_save_v2(const void *ip, const struct xt_entry_target *target)
339 const struct xt_set_info_target_v2 *info = (const void *)target->data;
341 print_target("--add-set", &info->add_set);
342 if (info->flags & IPSET_FLAG_EXIST)
343 printf(" --exist");
344 if (info->timeout != UINT32_MAX)
345 printf(" --timeout %u", info->timeout);
346 print_target("--del-set", &info->del_set);
349 static struct xtables_target set_tg_reg[] = {
351 .name = "SET",
352 .revision = 0,
353 .version = XTABLES_VERSION,
354 .family = NFPROTO_IPV4,
355 .size = XT_ALIGN(sizeof(struct xt_set_info_target_v0)),
356 .userspacesize = XT_ALIGN(sizeof(struct xt_set_info_target_v0)),
357 .help = set_target_help_v0,
358 .init = set_target_init_v0,
359 .parse = set_target_parse_v0,
360 .final_check = set_target_check_v0,
361 .print = set_target_print_v0,
362 .save = set_target_save_v0,
363 .extra_opts = set_target_opts_v0,
366 .name = "SET",
367 .revision = 1,
368 .version = XTABLES_VERSION,
369 .family = NFPROTO_UNSPEC,
370 .size = XT_ALIGN(sizeof(struct xt_set_info_target_v1)),
371 .userspacesize = XT_ALIGN(sizeof(struct xt_set_info_target_v1)),
372 .help = set_target_help_v0,
373 .init = set_target_init_v1,
374 .parse = set_target_parse_v1,
375 .final_check = set_target_check_v0,
376 .print = set_target_print_v1,
377 .save = set_target_save_v1,
378 .extra_opts = set_target_opts_v0,
381 .name = "SET",
382 .revision = 2,
383 .version = XTABLES_VERSION,
384 .family = NFPROTO_UNSPEC,
385 .size = XT_ALIGN(sizeof(struct xt_set_info_target_v2)),
386 .userspacesize = XT_ALIGN(sizeof(struct xt_set_info_target_v2)),
387 .help = set_target_help_v2,
388 .init = set_target_init_v2,
389 .parse = set_target_parse_v2,
390 .final_check = set_target_check_v2,
391 .print = set_target_print_v2,
392 .save = set_target_save_v2,
393 .extra_opts = set_target_opts_v2,
397 void _init(void)
399 xtables_register_targets(set_tg_reg, ARRAY_SIZE(set_tg_reg));