blob: cf5d19dd43a40cf71bd9d6497ef100c71282040f [file] [log] [blame]
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +00001/* 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>
5 *
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.
9 */
10
11/* Shared library add-on to iptables to add IP set matching. */
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000012#include <stdio.h>
13#include <netdb.h>
14#include <string.h>
15#include <stdlib.h>
16#include <getopt.h>
17#include <ctype.h>
18#include <errno.h>
19
20#include <iptables.h>
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000021#include <linux/netfilter_ipv4/ipt_set.h>
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +000022#include "libipt_set.h"
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000023
24/* Function which prints out usage message. */
25static void help(void)
26{
27 printf("set v%s options:\n"
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +000028 " [!] --set name flags\n"
29 " 'name' is the set name from to match,\n"
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000030 " 'flags' are the comma separated list of\n"
31 " 'src' and 'dst'.\n"
32 "\n", IPTABLES_VERSION);
33}
34
35static struct option opts[] = {
36 {"set", 1, 0, '1'},
37 {0}
38};
39
40/* Initialize the match. */
41static void init(struct ipt_entry_match *match, unsigned int *nfcache)
42{
43 struct ipt_set_info_match *info =
44 (struct ipt_set_info_match *) match->data;
45
46
47 memset(info, 0, sizeof(struct ipt_set_info_match));
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000048
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000049}
50
51/* Function which parses command options; returns true if it ate an option */
52static int
53parse(int c, char **argv, int invert, unsigned int *flags,
54 const struct ipt_entry *entry,
55 unsigned int *nfcache, struct ipt_entry_match **match)
56{
57 struct ipt_set_info_match *myinfo =
58 (struct ipt_set_info_match *) (*match)->data;
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +000059 struct ipt_set_info *info = &myinfo->match_set;
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000060
61 switch (c) {
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +000062 case '1': /* --set <set> <flag>[,<flag> */
63 if (info->flags[0])
64 exit_error(PARAMETER_PROBLEM,
65 "--set can be specified only once");
66
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000067 check_inverse(optarg, &invert, &optind, 0);
68 if (invert)
69 info->flags[0] |= IPSET_MATCH_INV;
70
71 if (!argv[optind]
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +000072 || argv[optind][0] == '-'
73 || argv[optind][0] == '!')
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000074 exit_error(PARAMETER_PROBLEM,
75 "--set requires two args.");
76
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +000077 if (strlen(argv[optind-1]) > IP_SET_MAXNAMELEN - 1)
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000078 exit_error(PARAMETER_PROBLEM,
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +000079 "setname `%s' too long, max %d characters.",
80 argv[optind-1], IP_SET_MAXNAMELEN - 1);
81
82 get_set_byname(argv[optind - 1], info);
83 parse_bindings(argv[optind], info);
84 DEBUGP("parse: set index %u\n", info->index);
85 optind++;
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000086
87 *flags = 1;
88 break;
89
90 default:
91 return 0;
92 }
93
94 return 1;
95}
96
97/* Final check; must have specified --set. */
98static void final_check(unsigned int flags)
99{
100 if (!flags)
101 exit_error(PARAMETER_PROBLEM,
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +0000102 "You must specify `--set' with proper arguments");
103 DEBUGP("final check OK\n");
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +0000104}
105
106static void
107print_match(const char *prefix, const struct ipt_set_info *info)
108{
109 int i;
110 char setname[IP_SET_MAXNAMELEN];
111
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +0000112 get_set_byid(setname, info->index);
113 printf("%s%s %s",
Joszef Kadlecsika05720b2006-06-23 09:55:12 +0000114 (info->flags[0] & IPSET_MATCH_INV) ? "! " : "",
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +0000115 prefix,
116 setname);
117 for (i = 0; i < IP_SET_MAX_BINDINGS; i++) {
118 if (!info->flags[i])
119 break;
120 printf("%s%s",
121 i == 0 ? " " : ",",
122 info->flags[i] & IPSET_SRC ? "src" : "dst");
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +0000123 }
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +0000124 printf(" ");
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +0000125}
126
127/* Prints out the matchinfo. */
128static void
129print(const struct ipt_ip *ip,
130 const struct ipt_entry_match *match, int numeric)
131{
132 struct ipt_set_info_match *info =
133 (struct ipt_set_info_match *) match->data;
134
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +0000135 print_match("set", &info->match_set);
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +0000136}
137
138/* Saves the matchinfo in parsable form to stdout. */
139static void save(const struct ipt_ip *ip,
140 const struct ipt_entry_match *match)
141{
142 struct ipt_set_info_match *info =
143 (struct ipt_set_info_match *) match->data;
144
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +0000145 print_match("--set", &info->match_set);
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +0000146}
147
148static
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +0000149struct iptables_match set = {
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +0000150 .name = "set",
151 .version = IPTABLES_VERSION,
152 .size = IPT_ALIGN(sizeof(struct ipt_set_info_match)),
153 .userspacesize = IPT_ALIGN(sizeof(struct ipt_set_info_match)),
154 .help = &help,
155 .init = &init,
156 .parse = &parse,
157 .final_check = &final_check,
158 .print = &print,
159 .save = &save,
160 .extra_opts = opts
161};
162
163void _init(void)
164{
165 register_match(&set);
166}