1 /* Copyright 2004 Jozsef Kadlecsik (kadlec@blackhole.kfki.hu)
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24 #include <asm/bitops.h>
26 #include <linux/netfilter_ipv4/ip_set_portmap.h>
32 #define OPT_CREATE_FROM 0x01U
33 #define OPT_CREATE_TO 0x02U
35 #define OPT_ADDDEL_PORT 0x01U
37 /* Initialize the create. */
38 void create_init(void *data)
44 /* Function which parses command options; returns true if it ate an option */
45 int create_parse(int c, char *argv[], void *data, unsigned *flags)
47 struct ip_set_req_portmap_create *mydata =
48 (struct ip_set_req_portmap_create *) data;
54 parse_port(optarg, &mydata->from);
56 *flags |= OPT_CREATE_FROM;
58 DP("--from %x (%s)", mydata->from,
59 port_tostring(mydata->from, 0));
64 parse_port(optarg, &mydata->to);
66 *flags |= OPT_CREATE_TO;
68 DP("--to %x (%s)", mydata->to,
69 port_tostring(mydata->to, 0));
80 /* Final check; exit if not ok. */
81 void create_final(void *data, unsigned int flags)
83 struct ip_set_req_portmap_create *mydata =
84 (struct ip_set_req_portmap_create *) data;
87 exit_error(PARAMETER_PROBLEM,
88 "Need to specify --from and --to\n");
91 if ((flags & OPT_CREATE_FROM) == 0
92 || (flags & OPT_CREATE_TO) == 0)
93 exit_error(PARAMETER_PROBLEM,
94 "Need to specify both --from and --to\n");
97 DP("from : %x to: %x diff: %d", mydata->from, mydata->to,
98 mydata->to - mydata->from);
100 if (mydata->from > mydata->to)
101 exit_error(PARAMETER_PROBLEM,
102 "From can't be lower than to.\n");
104 if (mydata->to - mydata->from > MAX_RANGE)
105 exit_error(PARAMETER_PROBLEM,
106 "Range too large. Max is %d ports in range\n",
110 /* Create commandline options */
111 static struct option create_opts[] = {
117 /* Add, del, test parser */
118 ip_set_ip_t adt_parser(unsigned cmd, const char *optarg, void *data)
120 struct ip_set_req_portmap *mydata =
121 (struct ip_set_req_portmap *) data;
123 parse_port(optarg, &mydata->port);
124 DP("%s", port_tostring(mydata->port, 0));
133 void initheader(struct set *set, const void *data)
135 struct ip_set_req_portmap_create *header =
136 (struct ip_set_req_portmap_create *) data;
137 struct ip_set_portmap *map =
138 (struct ip_set_portmap *) set->settype->header;
140 memset(map, 0, sizeof(struct ip_set_portmap));
141 map->first_port = header->from;
142 map->last_port = header->to;
145 void printheader(struct set *set, unsigned options)
147 struct ip_set_portmap *mysetdata =
148 (struct ip_set_portmap *) set->settype->header;
150 printf(" from: %s", port_tostring(mysetdata->first_port, options));
151 printf(" to: %s\n", port_tostring(mysetdata->last_port, options));
154 void printports_sorted(struct set *set, void *data, size_t len, unsigned options)
156 struct ip_set_portmap *mysetdata =
157 (struct ip_set_portmap *) set->settype->header;
158 u_int32_t addr = mysetdata->first_port;
160 DP("%u -- %u", mysetdata->first_port, mysetdata->last_port);
161 while (addr <= mysetdata->last_port) {
162 if (test_bit(addr - mysetdata->first_port, data))
163 printf("%s\n", port_tostring(addr, options));
168 char * binding_port_tostring(struct set *set, ip_set_ip_t ip, unsigned options)
170 return port_tostring(ip, options);
173 void saveheader(struct set *set, unsigned options)
175 struct ip_set_portmap *mysetdata =
176 (struct ip_set_portmap *) set->settype->header;
178 printf("-N %s %s --from %s",
180 set->settype->typename,
181 port_tostring(mysetdata->first_port, options));
183 port_tostring(mysetdata->last_port, options));
186 void saveports(struct set *set, void *data, size_t len, unsigned options)
188 struct ip_set_portmap *mysetdata =
189 (struct ip_set_portmap *) set->settype->header;
190 u_int32_t addr = mysetdata->first_port;
192 while (addr <= mysetdata->last_port) {
193 if (test_bit(addr - mysetdata->first_port, data))
196 port_tostring(addr, options));
204 ("-N set portmap --from PORT --to PORT\n"
210 static struct settype settype_portmap = {
211 .typename = SETTYPE_NAME,
212 .protocol_version = IP_SET_PROTOCOL_VERSION,
215 .create_size = sizeof(struct ip_set_req_portmap_create),
216 .create_init = &create_init,
217 .create_parse = &create_parse,
218 .create_final = &create_final,
219 .create_opts = create_opts,
222 .adt_size = sizeof(struct ip_set_req_portmap),
223 .adt_parser = &adt_parser,
226 .header_size = sizeof(struct ip_set_portmap),
227 .initheader = &initheader,
228 .printheader = &printheader,
229 .printips = &printports_sorted, /* We only have sorted version */
230 .printips_sorted = &printports_sorted,
231 .saveheader = &saveheader,
232 .saveips = &saveports,
235 .bindip_tostring = &binding_port_tostring,
236 .bindip_parse = &parse_port,
243 settype_register(&settype_portmap);