changing trunk/trunk to trunk
[iptables.git] / extensions / libipt_SET.c
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>
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 mangling target. */
12 #include <stdio.h>
13 #include <netdb.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include <getopt.h>
17 #include <ctype.h>
18
19 #include <iptables.h>
20 #include <linux/netfilter_ipv4/ip_tables.h>
21 #include <linux/netfilter_ipv4/ip_set.h>
22 #include <linux/netfilter_ipv4/ipt_set.h>
23 #include "libipt_set.h"
24
25 /* Function which prints out usage message. */
26 static void SET_help(void)
27 {
28         printf("SET target options:\n"
29                " --add-set name flags\n"
30                " --del-set name flags\n"
31                "                add/del src/dst IP/port from/to named sets,\n"
32                "                where flags are the comma separated list of\n"
33                "                'src' and 'dst'.\n");
34 }
35
36 static const struct option SET_opts[] = {
37         {"add-set",   1, NULL, '1'},
38         {"del-set",   1, NULL, '2'},
39         { }
40 };
41
42 /* Initialize the target. */
43 static void SET_init(struct xt_entry_target *target)
44 {
45         struct ipt_set_info_target *info =
46             (struct ipt_set_info_target *) target->data;
47
48         memset(info, 0, sizeof(struct ipt_set_info_target));
49         info->add_set.index =
50         info->del_set.index = IP_SET_INVALID_ID;
51
52 }
53
54 static void
55 parse_target(char **argv, int invert, unsigned int *flags,
56              struct ipt_set_info *info, const char *what)
57 {
58         if (info->flags[0])
59                 exit_error(PARAMETER_PROBLEM,
60                            "--%s can be specified only once", what);
61
62         if (check_inverse(optarg, &invert, NULL, 0))
63                 exit_error(PARAMETER_PROBLEM,
64                            "Unexpected `!' after --%s", what);
65
66         if (!argv[optind]
67             || argv[optind][0] == '-' || argv[optind][0] == '!')
68                 exit_error(PARAMETER_PROBLEM,
69                            "--%s requires two args.", what);
70
71         if (strlen(argv[optind-1]) > IP_SET_MAXNAMELEN - 1)
72                 exit_error(PARAMETER_PROBLEM,
73                            "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         optind++;
79         
80         *flags = 1;
81 }
82
83 /* Function which parses command options; returns true if it
84    ate an option */
85 static int SET_parse(int c, char **argv, int invert, unsigned int *flags,
86                      const void *entry, struct xt_entry_target **target)
87 {
88         struct ipt_set_info_target *myinfo =
89             (struct ipt_set_info_target *) (*target)->data;
90
91         switch (c) {
92         case '1':               /* --add-set <set> <flags> */
93                 parse_target(argv, invert, flags,
94                              &myinfo->add_set, "add-set");
95                 break;
96         case '2':               /* --del-set <set>[:<flags>] <flags> */
97                 parse_target(argv, invert, flags,
98                              &myinfo->del_set, "del-set");
99                 break;
100
101         default:
102                 return 0;
103         }
104         return 1;
105 }
106
107 /* Final check; must specify at least one. */
108 static void SET_check(unsigned int flags)
109 {
110         if (!flags)
111                 exit_error(PARAMETER_PROBLEM,
112                            "You must specify either `--add-set' or `--del-set'");
113 }
114
115 static void
116 print_target(const char *prefix, const struct ipt_set_info *info)
117 {
118         int i;
119         char setname[IP_SET_MAXNAMELEN];
120
121         if (info->index == IP_SET_INVALID_ID)
122                 return;
123         get_set_byid(setname, info->index);
124         printf("%s %s", prefix, setname);
125         for (i = 0; i < IP_SET_MAX_BINDINGS; i++) {
126                 if (!info->flags[i])
127                         break;          
128                 printf("%s%s",
129                        i == 0 ? " " : ",",
130                        info->flags[i] & IPSET_SRC ? "src" : "dst");
131         }
132         printf(" ");
133 }
134
135 /* Prints out the targinfo. */
136 static void SET_print(const void *ip, const struct xt_entry_target *target,
137                       int numeric)
138 {
139         struct ipt_set_info_target *info =
140             (struct ipt_set_info_target *) target->data;
141
142         print_target("add-set", &info->add_set);
143         print_target("del-set", &info->del_set);
144 }
145
146 /* Saves the union ipt_targinfo in parsable form to stdout. */
147 static void SET_save(const void *ip, const struct xt_entry_target *target)
148 {
149         struct ipt_set_info_target *info =
150             (struct ipt_set_info_target *) target->data;
151
152         print_target("--add-set", &info->add_set);
153         print_target("--del-set", &info->del_set);
154 }
155
156 static struct xtables_target set_tg_reg = {
157         .name           = "SET",
158         .version        = XTABLES_VERSION,
159         .family         = PF_INET,
160         .size           = XT_ALIGN(sizeof(struct ipt_set_info_target)),
161         .userspacesize  = XT_ALIGN(sizeof(struct ipt_set_info_target)),
162         .help           = SET_help,
163         .init           = SET_init,
164         .parse          = SET_parse,
165         .final_check    = SET_check,
166         .print          = SET_print,
167         .save           = SET_save,
168         .extra_opts     = SET_opts,
169 };
170
171 void _init(void)
172 {
173         xtables_register_target(&set_tg_reg);
174 }