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 matching. */
21 #include <linux/netfilter_ipv4/ip_conntrack.h>
22 #include <linux/netfilter_ipv4/ipt_set.h>
23 #include "libipt_set.h"
25 /* Function which prints out usage message. */
26 static void help(void)
28 printf("set v%s options:\n"
29 " [!] --set name flags\n"
30 " 'name' is the set name from to match,\n"
31 " 'flags' are the comma separated list of\n"
33 "\n", IPTABLES_VERSION);
36 static struct option opts[] = {
41 /* Initialize the match. */
42 static void init(struct ipt_entry_match *match, unsigned int *nfcache)
44 struct ipt_set_info_match *info =
45 (struct ipt_set_info_match *) match->data;
48 memset(info, 0, sizeof(struct ipt_set_info_match));
52 /* Function which parses command options; returns true if it ate an option */
54 parse(int c, char **argv, int invert, unsigned int *flags,
55 const struct ipt_entry *entry,
56 unsigned int *nfcache, struct ipt_entry_match **match)
58 struct ipt_set_info_match *myinfo =
59 (struct ipt_set_info_match *) (*match)->data;
60 struct ipt_set_info *info = &myinfo->match_set;
63 case '1': /* --set <set> <flag>[,<flag> */
65 exit_error(PARAMETER_PROBLEM,
66 "--set can be specified only once");
68 check_inverse(optarg, &invert, &optind, 0);
70 info->flags[0] |= IPSET_MATCH_INV;
73 || argv[optind][0] == '-'
74 || argv[optind][0] == '!')
75 exit_error(PARAMETER_PROBLEM,
76 "--set requires two args.");
78 if (strlen(argv[optind-1]) > IP_SET_MAXNAMELEN - 1)
79 exit_error(PARAMETER_PROBLEM,
80 "setname `%s' too long, max %d characters.",
81 argv[optind-1], IP_SET_MAXNAMELEN - 1);
83 get_set_byname(argv[optind - 1], info);
84 parse_bindings(argv[optind], info);
85 DEBUGP("parse: set index %u\n", info->index);
98 /* Final check; must have specified --set. */
99 static void final_check(unsigned int flags)
102 exit_error(PARAMETER_PROBLEM,
103 "You must specify `--set' with proper arguments");
104 DEBUGP("final check OK\n");
108 print_match(const char *prefix, const struct ipt_set_info *info)
111 char setname[IP_SET_MAXNAMELEN];
113 get_set_byid(setname, info->index);
115 (info->flags[0] & IPSET_MATCH_INV) ? "!" : "",
118 for (i = 0; i < IP_SET_MAX_BINDINGS; i++) {
123 info->flags[i] & IPSET_SRC ? "src" : "dst");
128 /* Prints out the matchinfo. */
130 print(const struct ipt_ip *ip,
131 const struct ipt_entry_match *match, int numeric)
133 struct ipt_set_info_match *info =
134 (struct ipt_set_info_match *) match->data;
136 print_match("set", &info->match_set);
139 /* Saves the matchinfo in parsable form to stdout. */
140 static void save(const struct ipt_ip *ip,
141 const struct ipt_entry_match *match)
143 struct ipt_set_info_match *info =
144 (struct ipt_set_info_match *) match->data;
146 print_match("--set", &info->match_set);
150 struct iptables_match set = {
152 .version = IPTABLES_VERSION,
153 .size = IPT_ALIGN(sizeof(struct ipt_set_info_match)),
154 .userspacesize = IPT_ALIGN(sizeof(struct ipt_set_info_match)),
158 .final_check = &final_check,
166 register_match(&set);