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 #ifndef __always_inline
25 #define __always_inline inline
27 #include <asm/bitops.h>
29 #include <linux/netfilter_ipv4/ip_set_portmap.h>
35 #define OPT_CREATE_FROM 0x01U
36 #define OPT_CREATE_TO 0x02U
38 #define OPT_ADDDEL_PORT 0x01U
40 /* Initialize the create. */
41 void create_init(void *data)
47 /* Function which parses command options; returns true if it ate an option */
48 int create_parse(int c, char *argv[], void *data, unsigned *flags)
50 struct ip_set_req_portmap_create *mydata =
51 (struct ip_set_req_portmap_create *) data;
57 parse_port(optarg, &mydata->from);
59 *flags |= OPT_CREATE_FROM;
61 DP("--from %x (%s)", mydata->from,
62 port_tostring(mydata->from, 0));
67 parse_port(optarg, &mydata->to);
69 *flags |= OPT_CREATE_TO;
71 DP("--to %x (%s)", mydata->to,
72 port_tostring(mydata->to, 0));
83 /* Final check; exit if not ok. */
84 void create_final(void *data, unsigned int flags)
86 struct ip_set_req_portmap_create *mydata =
87 (struct ip_set_req_portmap_create *) data;
90 exit_error(PARAMETER_PROBLEM,
91 "Need to specify --from and --to\n");
94 if ((flags & OPT_CREATE_FROM) == 0
95 || (flags & OPT_CREATE_TO) == 0)
96 exit_error(PARAMETER_PROBLEM,
97 "Need to specify both --from and --to\n");
100 DP("from : %x to: %x diff: %d", mydata->from, mydata->to,
101 mydata->to - mydata->from);
103 if (mydata->from > mydata->to)
104 exit_error(PARAMETER_PROBLEM,
105 "From can't be lower than to.\n");
107 if (mydata->to - mydata->from > MAX_RANGE)
108 exit_error(PARAMETER_PROBLEM,
109 "Range too large. Max is %d ports in range\n",
113 /* Create commandline options */
114 static struct option create_opts[] = {
120 /* Add, del, test parser */
121 ip_set_ip_t adt_parser(unsigned cmd, const char *optarg, void *data)
123 struct ip_set_req_portmap *mydata =
124 (struct ip_set_req_portmap *) data;
126 parse_port(optarg, &mydata->port);
127 DP("%s", port_tostring(mydata->port, 0));
136 void initheader(struct set *set, const void *data)
138 struct ip_set_req_portmap_create *header =
139 (struct ip_set_req_portmap_create *) data;
140 struct ip_set_portmap *map =
141 (struct ip_set_portmap *) set->settype->header;
143 memset(map, 0, sizeof(struct ip_set_portmap));
144 map->first_port = header->from;
145 map->last_port = header->to;
148 void printheader(struct set *set, unsigned options)
150 struct ip_set_portmap *mysetdata =
151 (struct ip_set_portmap *) set->settype->header;
153 printf(" from: %s", port_tostring(mysetdata->first_port, options));
154 printf(" to: %s\n", port_tostring(mysetdata->last_port, options));
157 void printports_sorted(struct set *set, void *data, size_t len, unsigned options)
159 struct ip_set_portmap *mysetdata =
160 (struct ip_set_portmap *) set->settype->header;
161 u_int32_t addr = mysetdata->first_port;
163 DP("%u -- %u", mysetdata->first_port, mysetdata->last_port);
164 while (addr <= mysetdata->last_port) {
165 if (test_bit(addr - mysetdata->first_port, data))
166 printf("%s\n", port_tostring(addr, options));
171 char * binding_port_tostring(struct set *set, ip_set_ip_t ip, unsigned options)
173 return port_tostring(ip, options);
176 void saveheader(struct set *set, unsigned options)
178 struct ip_set_portmap *mysetdata =
179 (struct ip_set_portmap *) set->settype->header;
181 printf("-N %s %s --from %s",
183 set->settype->typename,
184 port_tostring(mysetdata->first_port, options));
186 port_tostring(mysetdata->last_port, options));
189 void saveports(struct set *set, void *data, size_t len, unsigned options)
191 struct ip_set_portmap *mysetdata =
192 (struct ip_set_portmap *) set->settype->header;
193 u_int32_t addr = mysetdata->first_port;
195 while (addr <= mysetdata->last_port) {
196 if (test_bit(addr - mysetdata->first_port, data))
199 port_tostring(addr, options));
207 ("-N set portmap --from PORT --to PORT\n"
213 static struct settype settype_portmap = {
214 .typename = SETTYPE_NAME,
215 .protocol_version = IP_SET_PROTOCOL_VERSION,
218 .create_size = sizeof(struct ip_set_req_portmap_create),
219 .create_init = &create_init,
220 .create_parse = &create_parse,
221 .create_final = &create_final,
222 .create_opts = create_opts,
225 .adt_size = sizeof(struct ip_set_req_portmap),
226 .adt_parser = &adt_parser,
229 .header_size = sizeof(struct ip_set_portmap),
230 .initheader = &initheader,
231 .printheader = &printheader,
232 .printips = &printports_sorted, /* We only have sorted version */
233 .printips_sorted = &printports_sorted,
234 .saveheader = &saveheader,
235 .saveips = &saveports,
238 .bindip_tostring = &binding_port_tostring,
239 .bindip_parse = &parse_port,
246 settype_register(&settype_portmap);