blob: 932008bb054d7534b84446a82fdd0c6038020194 [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. */
Jan Engelhardt59d16402007-10-04 16:28:39 +000025static void set_help(void)
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000026{
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
Jan Engelhardt59d16402007-10-04 16:28:39 +000035static const struct option set_opts[] = {
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000036 {"set", 1, 0, '1'},
37 {0}
38};
39
40/* Initialize the match. */
Jan Engelhardt59d16402007-10-04 16:28:39 +000041static void set_init(struct xt_entry_match *match)
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000042{
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 */
Jan Engelhardt59d16402007-10-04 16:28:39 +000052static int set_parse(int c, char **argv, int invert, unsigned int *flags,
53 const void *entry, struct xt_entry_match **match)
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000054{
55 struct ipt_set_info_match *myinfo =
56 (struct ipt_set_info_match *) (*match)->data;
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +000057 struct ipt_set_info *info = &myinfo->match_set;
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000058
59 switch (c) {
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +000060 case '1': /* --set <set> <flag>[,<flag> */
61 if (info->flags[0])
62 exit_error(PARAMETER_PROBLEM,
63 "--set can be specified only once");
64
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000065 check_inverse(optarg, &invert, &optind, 0);
66 if (invert)
67 info->flags[0] |= IPSET_MATCH_INV;
68
69 if (!argv[optind]
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +000070 || argv[optind][0] == '-'
71 || argv[optind][0] == '!')
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000072 exit_error(PARAMETER_PROBLEM,
73 "--set requires two args.");
74
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +000075 if (strlen(argv[optind-1]) > IP_SET_MAXNAMELEN - 1)
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000076 exit_error(PARAMETER_PROBLEM,
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +000077 "setname `%s' too long, max %d characters.",
78 argv[optind-1], IP_SET_MAXNAMELEN - 1);
79
80 get_set_byname(argv[optind - 1], info);
81 parse_bindings(argv[optind], info);
82 DEBUGP("parse: set index %u\n", info->index);
83 optind++;
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000084
85 *flags = 1;
86 break;
87
88 default:
89 return 0;
90 }
91
92 return 1;
93}
94
95/* Final check; must have specified --set. */
Jan Engelhardt59d16402007-10-04 16:28:39 +000096static void set_check(unsigned int flags)
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000097{
98 if (!flags)
99 exit_error(PARAMETER_PROBLEM,
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +0000100 "You must specify `--set' with proper arguments");
101 DEBUGP("final check OK\n");
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +0000102}
103
104static void
105print_match(const char *prefix, const struct ipt_set_info *info)
106{
107 int i;
108 char setname[IP_SET_MAXNAMELEN];
109
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +0000110 get_set_byid(setname, info->index);
111 printf("%s%s %s",
Joszef Kadlecsika05720b2006-06-23 09:55:12 +0000112 (info->flags[0] & IPSET_MATCH_INV) ? "! " : "",
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +0000113 prefix,
114 setname);
115 for (i = 0; i < IP_SET_MAX_BINDINGS; i++) {
116 if (!info->flags[i])
117 break;
118 printf("%s%s",
119 i == 0 ? " " : ",",
120 info->flags[i] & IPSET_SRC ? "src" : "dst");
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +0000121 }
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +0000122 printf(" ");
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +0000123}
124
125/* Prints out the matchinfo. */
Jan Engelhardt59d16402007-10-04 16:28:39 +0000126static void set_print(const void *ip, const struct xt_entry_match *match,
127 int numeric)
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +0000128{
129 struct ipt_set_info_match *info =
130 (struct ipt_set_info_match *) match->data;
131
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +0000132 print_match("set", &info->match_set);
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +0000133}
134
135/* Saves the matchinfo in parsable form to stdout. */
Jan Engelhardt59d16402007-10-04 16:28:39 +0000136static void set_save(const void *ip, const struct xt_entry_match *match)
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +0000137{
138 struct ipt_set_info_match *info =
139 (struct ipt_set_info_match *) match->data;
140
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +0000141 print_match("--set", &info->match_set);
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +0000142}
143
Jan Engelhardt59d16402007-10-04 16:28:39 +0000144static struct iptables_match set_match = {
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +0000145 .name = "set",
146 .version = IPTABLES_VERSION,
147 .size = IPT_ALIGN(sizeof(struct ipt_set_info_match)),
148 .userspacesize = IPT_ALIGN(sizeof(struct ipt_set_info_match)),
Jan Engelhardt59d16402007-10-04 16:28:39 +0000149 .help = set_help,
150 .init = set_init,
151 .parse = set_parse,
152 .final_check = set_check,
153 .print = set_print,
154 .save = set_save,
155 .extra_opts = set_opts,
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +0000156};
157
158void _init(void)
159{
Jan Engelhardt59d16402007-10-04 16:28:39 +0000160 register_match(&set_match);
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +0000161}