summaryrefslogtreecommitdiffstats
path: root/ipset_portmap.c
blob: fd8746f75e718f7877523e651b96d53d1c54e10a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* Copyright 2004 Jozsef Kadlecsik (kadlec@blackhole.kfki.hu)
 *
 * This program is free software; you can redistribute it and/or modify   
 * it under the terms of the GNU General Public License as published by   
 * the Free Software Foundation; either version 2 of the License, or      
 * (at your option) any later version.                                    
 *                                                                         
 * This program is distributed in the hope that it will be useful,        
 * but WITHOUT ANY WARRANTY; without even the implied warranty of         
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          
 * GNU General Public License for more details.                           
 *                                                                         
 * You should have received a copy of the GNU General Public License      
 * along with this program; if not, write to the Free Software            
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */


#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <asm/bitops.h>

#include <linux/netfilter_ipv4/ip_set_portmap.h>
#include "ipset.h"


#define BUFLEN 30;

#define OPT_CREATE_FROM    0x01U
#define OPT_CREATE_TO      0x02U

#define OPT_ADDDEL_PORT      0x01U

/* Initialize the create. */
void create_init(void *data)
{
	DP("create INIT");
	/* Nothing */
}

/* Function which parses command options; returns true if it ate an option */
int create_parse(int c, char *argv[], void *data, unsigned *flags)
{
	struct ip_set_req_portmap_create *mydata =
	    (struct ip_set_req_portmap_create *) data;

	DP("create_parse");

	switch (c) {
	case '1':
		parse_port(optarg, &mydata->from);

		*flags |= OPT_CREATE_FROM;

		DP("--from %x (%s)", mydata->from,
		   port_tostring(mydata->from, 0));

		break;

	case '2':
		parse_port(optarg, &mydata->to);

		*flags |= OPT_CREATE_TO;

		DP("--to %x (%s)", mydata->to,
		   port_tostring(mydata->to, 0));

		break;

	default:
		return 0;
	}

	return 1;
}

/* Final check; exit if not ok. */
void create_final(void *data, unsigned int flags)
{
	struct ip_set_req_portmap_create *mydata =
	    (struct ip_set_req_portmap_create *) data;

	if (flags == 0) {
		exit_error(PARAMETER_PROBLEM,
			   "Need to specify --from and --to\n");
	} else {
		/* --from --to */
		if ((flags & OPT_CREATE_FROM) == 0
		    || (flags & OPT_CREATE_TO) == 0)
			exit_error(PARAMETER_PROBLEM,
				   "Need to specify both --from and --to\n");
	}

	DP("from : %x to: %x  diff: %d", mydata->from, mydata->to,
	   mydata->to - mydata->from);

	if (mydata->from > mydata->to)
		exit_error(PARAMETER_PROBLEM,
			   "From can't be lower than to.\n", MAX_RANGE);

	if (mydata->to - mydata->from > MAX_RANGE)
		exit_error(PARAMETER_PROBLEM,
			   "Range to large. Max is %d ports in range\n",
			   MAX_RANGE);
}

/* Create commandline options */
static struct option create_opts[] = {
	{"from", 1, 0, '1'},
	{"to", 1, 0, '2'},
	{0}
};

/* Add, del, test parser */
ip_set_ip_t adt_parser(int cmd, const char *optarg,
		       void *data, const void *setdata)
{
	struct ip_set_req_portmap *mydata =
	    (struct ip_set_req_portmap *) data;
	struct ip_set_portmap *mysetdata =
	    (struct ip_set_portmap *) setdata;

	DP("portmap: %p %p", data, setdata);

	parse_port(optarg, &mydata->port);

	DP("from %s", port_tostring(mysetdata->first_port, 0));
	DP("to   %s", port_tostring(mysetdata->last_port, 0));
	DP("port %s", port_tostring(mydata->port, 0));

	if (mydata->port < mysetdata->first_port ||
	    mydata->port > mysetdata->last_port)
		exit_error(PARAMETER_PROBLEM, "Port '%s' is out of range\n",
			   port_tostring(mydata->port, 0));

	return mydata->port;	
}

ip_set_ip_t getportbyid(const void *setdata, ip_set_ip_t id)
{
	struct ip_set_portmap *mysetdata =
	    (struct ip_set_portmap *) setdata;

	return (mysetdata->first_port + id);
}

ip_set_ip_t sizeid(const void *setdata)
{
	struct ip_set_portmap *mysetdata =
	    (struct ip_set_portmap *) setdata;

	return (mysetdata->last_port - mysetdata->first_port + 1);
}

void initheader(void **setdata, void *data, size_t len)
{
	struct ip_set_req_portmap_create *header =
	    (struct ip_set_req_portmap_create *) data;

	DP("portmap: initheader() 1");

	if (len != sizeof(struct ip_set_req_portmap_create))
		exit_error(OTHER_PROBLEM,
			   "Portmap: incorrect size of header. "
			   "Got %d, wanted %d.", len,
			   sizeof(struct ip_set_req_portmap_create));

	*setdata = ipset_malloc(sizeof(struct ip_set_portmap));

	DP("portmap: initheader() 2");

	((struct ip_set_portmap *) *setdata)->first_port =
	    header->from;
	((struct ip_set_portmap *) *setdata)->last_port = header->to;

}

void initmembers(void *setdata, void *data, size_t len)
{
	struct ip_set_portmap *mysetdata =
	    (struct ip_set_portmap *) setdata;
	size_t size;

	DP("portmap: initmembers()");

	/* Check so we get the right amount of memberdata */
	size = bitmap_bytes(mysetdata->first_port, mysetdata->last_port);

	if (len != size)
		exit_error(OTHER_PROBLEM,
			   "Portmap: incorrect size of members. "
			   "Got %d, wanted %d.", len, size);

	mysetdata->members = data;
}

void killmembers(void **setdata)
{
	struct ip_set_portmap *mysetdata =
	    (struct ip_set_portmap *) *setdata;

	DP("portmap: killmembers()");

	if (mysetdata->members != NULL)
		ipset_free(&mysetdata->members);
	
	ipset_free(setdata);
}


void printheader(const void *setdata, unsigned options)
{
	struct ip_set_portmap *mysetdata =
	    (struct ip_set_portmap *) setdata;

	printf(" from: %s", port_tostring(mysetdata->first_port, options));
	printf(" to: %s\n", port_tostring(mysetdata->last_port, options));
}

void printports_sorted(const void *setdata, unsigned options)
{
	struct ip_set_portmap *mysetdata =
	    (struct ip_set_portmap *) setdata;

	u_int32_t addr = mysetdata->first_port;

	while (addr <= mysetdata->last_port) {
		if (test_bit(addr - mysetdata->first_port, mysetdata->members))
			printf("%s\n", port_tostring(addr, options));
		addr++;
	}
}

void saveheader(const void *setdata)
{
	return;
}

/* Print save for an IP */
void saveports(const void *setdata)
{
	return;
}

void usage(void)
{
	printf
	    ("-N set portmap --from PORT --to PORT\n"
	     "-A set PORT\n"
	     "-D set PORT\n"
	     "-T set PORT\n");
}

static struct settype settype_portmap = {
	.typename = SETTYPE_NAME,
	.typecode = IPSET_TYPE_PORT,
	.protocol_version = IP_SET_PROTOCOL_VERSION,

	/* Create */
	.create_size = sizeof(struct ip_set_req_portmap_create),
	.create_init = &create_init,
	.create_parse = &create_parse,
	.create_final = &create_final,
	.create_opts = create_opts,

	/* Add/del/test */
	.req_size = sizeof(struct ip_set_req_portmap),
	.adt_parser = &adt_parser,

	/* Get an IP address by id */
	.getipbyid = &getportbyid,
	.sizeid = &sizeid,

	/* Printing */
	.initheader = &initheader,
	.initmembers = &initmembers,
	.killmembers = &killmembers,
	.printheader = &printheader,
	.printips = &printports_sorted,	/* We only have sorted version */
	.printips_sorted = &printports_sorted,
	.saveheader = &saveheader,
	.saveips = &saveports,
	.usage = &usage,
	.hint = NULL,
};

void _init(void)
{
	settype_register(&settype_portmap);

}