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_nat_rule.h>
22 #include <linux/netfilter_ipv4/ip_set.h>
23 #include <linux/netfilter_ipv4/ipt_set.h>
24 #include "libipt_set.h"
26 /* Function which prints out usage message. */
27 static void help(void)
29 printf("SET v%s 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"
35 "\n", IPTABLES_VERSION);
38 static struct option opts[] = {
39 {"add-set", 1, 0, '1'},
40 {"del-set", 1, 0, '2'},
44 /* Initialize the target. */
45 static void init(struct ipt_entry_target *target, unsigned int *nfcache)
47 struct ipt_set_info_target *info =
48 (struct ipt_set_info_target *) target->data;
50 memset(info, 0, sizeof(struct ipt_set_info_target));
52 info->del_set.index = IP_SET_INVALID_ID;
57 parse_target(char **argv, int invert, unsigned int *flags,
58 struct ipt_set_info *info, const char *what)
61 exit_error(PARAMETER_PROBLEM,
62 "--%s can be specified only once", what);
64 if (check_inverse(optarg, &invert, NULL, 0))
65 exit_error(PARAMETER_PROBLEM,
66 "Unexpected `!' after --%s", what);
69 || argv[optind][0] == '-' || argv[optind][0] == '!')
70 exit_error(PARAMETER_PROBLEM,
71 "--%s requires two args.", what);
73 if (strlen(argv[optind-1]) > IP_SET_MAXNAMELEN - 1)
74 exit_error(PARAMETER_PROBLEM,
75 "setname `%s' too long, max %d characters.",
76 argv[optind-1], IP_SET_MAXNAMELEN - 1);
78 get_set_byname(argv[optind - 1], info);
79 parse_bindings(argv[optind], info);
85 /* Function which parses command options; returns true if it
88 parse(int c, char **argv, int invert, unsigned int *flags,
89 const struct ipt_entry *entry, struct ipt_entry_target **target)
91 struct ipt_set_info_target *myinfo =
92 (struct ipt_set_info_target *) (*target)->data;
95 case '1': /* --add-set <set> <flags> */
96 parse_target(argv, invert, flags,
97 &myinfo->add_set, "add-set");
99 case '2': /* --del-set <set>[:<flags>] <flags> */
100 parse_target(argv, invert, flags,
101 &myinfo->del_set, "del-set");
110 /* Final check; must specify at least one. */
111 static void final_check(unsigned int flags)
114 exit_error(PARAMETER_PROBLEM,
115 "You must specify either `--add-set' or `--del-set'");
119 print_target(const char *prefix, const struct ipt_set_info *info)
122 char setname[IP_SET_MAXNAMELEN];
124 if (info->index == IP_SET_INVALID_ID)
126 get_set_byid(setname, info->index);
127 printf("%s %s", prefix, setname);
128 for (i = 0; i < IP_SET_MAX_BINDINGS; i++) {
133 info->flags[i] & IPSET_SRC ? "src" : "dst");
138 /* Prints out the targinfo. */
140 print(const struct ipt_ip *ip,
141 const struct ipt_entry_target *target, int numeric)
143 struct ipt_set_info_target *info =
144 (struct ipt_set_info_target *) target->data;
146 print_target("add-set", &info->add_set);
147 print_target("del-set", &info->del_set);
150 /* Saves the union ipt_targinfo in parsable form to stdout. */
152 save(const struct ipt_ip *ip, const struct ipt_entry_target *target)
154 struct ipt_set_info_target *info =
155 (struct ipt_set_info_target *) target->data;
157 print_target("--add-set", &info->add_set);
158 print_target("--del-set", &info->del_set);
162 struct iptables_target ipt_set_target
165 .version = IPTABLES_VERSION,
166 .size = IPT_ALIGN(sizeof(struct ipt_set_info_target)),
167 .userspacesize = IPT_ALIGN(sizeof(struct ipt_set_info_target)),
171 .final_check = &final_check,
179 register_target(&ipt_set_target);