1 /* 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-2004 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. */
20 #include <linux/netfilter_ipv4/ip_tables.h>
21 #include <linux/netfilter_ipv4/ip_set.h>
22 #include <linux/netfilter_ipv4/ipt_set.h>
23 #include "libipt_set.h"
25 /* Function which prints out usage message. */
26 static void SET_help(void)
28 printf("SET target options:\n"
29 " --add-set name flags\n"
30 " --del-set name flags\n"
31 " add/del src/dst IP/port from/to named sets,\n"
32 " where flags are the comma separated list of\n"
33 " 'src' and 'dst'.\n");
36 static const struct option SET_opts[] = {
37 {"add-set", 1, NULL, '1'},
38 {"del-set", 1, NULL, '2'},
42 /* Initialize the target. */
43 static void SET_init(struct xt_entry_target *target)
45 struct ipt_set_info_target *info =
46 (struct ipt_set_info_target *) target->data;
48 memset(info, 0, sizeof(struct ipt_set_info_target));
50 info->del_set.index = IP_SET_INVALID_ID;
55 parse_target(char **argv, int invert, unsigned int *flags,
56 struct ipt_set_info *info, const char *what)
59 exit_error(PARAMETER_PROBLEM,
60 "--%s can be specified only once", what);
62 if (check_inverse(optarg, &invert, NULL, 0))
63 exit_error(PARAMETER_PROBLEM,
64 "Unexpected `!' after --%s", what);
67 || argv[optind][0] == '-' || argv[optind][0] == '!')
68 exit_error(PARAMETER_PROBLEM,
69 "--%s requires two args.", what);
71 if (strlen(argv[optind-1]) > IP_SET_MAXNAMELEN - 1)
72 exit_error(PARAMETER_PROBLEM,
73 "setname `%s' too long, max %d characters.",
74 argv[optind-1], IP_SET_MAXNAMELEN - 1);
76 get_set_byname(argv[optind - 1], info);
77 parse_bindings(argv[optind], info);
83 /* Function which parses command options; returns true if it
85 static int SET_parse(int c, char **argv, int invert, unsigned int *flags,
86 const void *entry, struct xt_entry_target **target)
88 struct ipt_set_info_target *myinfo =
89 (struct ipt_set_info_target *) (*target)->data;
92 case '1': /* --add-set <set> <flags> */
93 parse_target(argv, invert, flags,
94 &myinfo->add_set, "add-set");
96 case '2': /* --del-set <set>[:<flags>] <flags> */
97 parse_target(argv, invert, flags,
98 &myinfo->del_set, "del-set");
107 /* Final check; must specify at least one. */
108 static void SET_check(unsigned int flags)
111 exit_error(PARAMETER_PROBLEM,
112 "You must specify either `--add-set' or `--del-set'");
116 print_target(const char *prefix, const struct ipt_set_info *info)
119 char setname[IP_SET_MAXNAMELEN];
121 if (info->index == IP_SET_INVALID_ID)
123 get_set_byid(setname, info->index);
124 printf("%s %s", prefix, setname);
125 for (i = 0; i < IP_SET_MAX_BINDINGS; i++) {
130 info->flags[i] & IPSET_SRC ? "src" : "dst");
135 /* Prints out the targinfo. */
136 static void SET_print(const void *ip, const struct xt_entry_target *target,
139 struct ipt_set_info_target *info =
140 (struct ipt_set_info_target *) target->data;
142 print_target("add-set", &info->add_set);
143 print_target("del-set", &info->del_set);
146 /* Saves the union ipt_targinfo in parsable form to stdout. */
147 static void SET_save(const void *ip, const struct xt_entry_target *target)
149 struct ipt_set_info_target *info =
150 (struct ipt_set_info_target *) target->data;
152 print_target("--add-set", &info->add_set);
153 print_target("--del-set", &info->del_set);
156 static struct xtables_target set_tg_reg = {
158 .version = XTABLES_VERSION,
160 .size = XT_ALIGN(sizeof(struct ipt_set_info_target)),
161 .userspacesize = XT_ALIGN(sizeof(struct ipt_set_info_target)),
165 .final_check = SET_check,
168 .extra_opts = SET_opts,
173 xtables_register_target(&set_tg_reg);