blob: 9bdb007d24a4e5b9df50b69baea3dc6b0b69c963 [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
Jan Engelhardt5d9678a2008-11-20 10:15:35 +010020#include <xtables.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
Jan Engelhardt59d16402007-10-04 16:28:39 +000024static void set_help(void)
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000025{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020026 printf("set match options:\n"
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +000027 " [!] --set name flags\n"
28 " 'name' is the set name from to match,\n"
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000029 " 'flags' are the comma separated list of\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020030 " 'src' and 'dst'.\n");
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000031}
32
Jan Engelhardt59d16402007-10-04 16:28:39 +000033static const struct option set_opts[] = {
Patrick McHardy0ea82bc2008-06-07 15:15:29 +020034 {"set", 1, NULL, '1'},
35 { }
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000036};
37
Jan Engelhardt59d16402007-10-04 16:28:39 +000038static void set_init(struct xt_entry_match *match)
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000039{
40 struct ipt_set_info_match *info =
41 (struct ipt_set_info_match *) match->data;
42
43
44 memset(info, 0, sizeof(struct ipt_set_info_match));
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000045
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000046}
47
Jan Engelhardt59d16402007-10-04 16:28:39 +000048static int set_parse(int c, char **argv, int invert, unsigned int *flags,
49 const void *entry, struct xt_entry_match **match)
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000050{
51 struct ipt_set_info_match *myinfo =
52 (struct ipt_set_info_match *) (*match)->data;
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +000053 struct ipt_set_info *info = &myinfo->match_set;
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000054
55 switch (c) {
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +000056 case '1': /* --set <set> <flag>[,<flag> */
57 if (info->flags[0])
58 exit_error(PARAMETER_PROBLEM,
59 "--set can be specified only once");
60
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000061 check_inverse(optarg, &invert, &optind, 0);
62 if (invert)
63 info->flags[0] |= IPSET_MATCH_INV;
64
65 if (!argv[optind]
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +000066 || argv[optind][0] == '-'
67 || argv[optind][0] == '!')
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000068 exit_error(PARAMETER_PROBLEM,
69 "--set requires two args.");
70
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +000071 if (strlen(argv[optind-1]) > IP_SET_MAXNAMELEN - 1)
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000072 exit_error(PARAMETER_PROBLEM,
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +000073 "setname `%s' too long, max %d characters.",
74 argv[optind-1], IP_SET_MAXNAMELEN - 1);
75
76 get_set_byname(argv[optind - 1], info);
77 parse_bindings(argv[optind], info);
78 DEBUGP("parse: set index %u\n", info->index);
79 optind++;
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000080
81 *flags = 1;
82 break;
83
84 default:
85 return 0;
86 }
87
88 return 1;
89}
90
Jan Engelhardt59d16402007-10-04 16:28:39 +000091static void set_check(unsigned int flags)
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000092{
93 if (!flags)
94 exit_error(PARAMETER_PROBLEM,
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +000095 "You must specify `--set' with proper arguments");
96 DEBUGP("final check OK\n");
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +000097}
98
99static void
100print_match(const char *prefix, const struct ipt_set_info *info)
101{
102 int i;
103 char setname[IP_SET_MAXNAMELEN];
104
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +0000105 get_set_byid(setname, info->index);
106 printf("%s%s %s",
Joszef Kadlecsika05720b2006-06-23 09:55:12 +0000107 (info->flags[0] & IPSET_MATCH_INV) ? "! " : "",
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +0000108 prefix,
109 setname);
110 for (i = 0; i < IP_SET_MAX_BINDINGS; i++) {
111 if (!info->flags[i])
112 break;
113 printf("%s%s",
114 i == 0 ? " " : ",",
115 info->flags[i] & IPSET_SRC ? "src" : "dst");
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +0000116 }
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +0000117 printf(" ");
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +0000118}
119
120/* Prints out the matchinfo. */
Jan Engelhardt59d16402007-10-04 16:28:39 +0000121static void set_print(const void *ip, const struct xt_entry_match *match,
122 int numeric)
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +0000123{
124 struct ipt_set_info_match *info =
125 (struct ipt_set_info_match *) match->data;
126
Joszef Kadlecsikb9a49382004-12-01 09:11:33 +0000127 print_match("set", &info->match_set);
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +0000128}
129
Jan Engelhardt59d16402007-10-04 16:28:39 +0000130static void set_save(const void *ip, const struct xt_entry_match *match)
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +0000131{
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
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200138static struct xtables_match set_mt_reg = {
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +0000139 .name = "set",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200140 .version = XTABLES_VERSION,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100141 .family = NFPROTO_IPV4,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200142 .size = XT_ALIGN(sizeof(struct ipt_set_info_match)),
143 .userspacesize = XT_ALIGN(sizeof(struct ipt_set_info_match)),
Jan Engelhardt59d16402007-10-04 16:28:39 +0000144 .help = set_help,
145 .init = set_init,
146 .parse = set_parse,
147 .final_check = set_check,
148 .print = set_print,
149 .save = set_save,
150 .extra_opts = set_opts,
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +0000151};
152
153void _init(void)
154{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200155 xtables_register_match(&set_mt_reg);
Joszef Kadlecsik9c50ebe2004-02-09 13:47:01 +0000156}