summaryrefslogtreecommitdiffstats
path: root/extensions/libipt_sctp.c
blob: d512fa6356a68d2ba0f35beee10f38c429b2e4d4 (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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/* Shared library add-on to iptables for SCTP matching
 *
 * (C) 2003 by Harald Welte <laforge@gnumonks.org>
 *
 * This program is distributed under the terms of GNU GPL v2, 1991
 *
 * libipt_ecn.c borrowed heavily from libipt_dscp.c
 *
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#include <netdb.h>

#include <iptables.h>
#include <linux/netfilter_ipv4/ip_tables.h>
#include <linux/netfilter_ipv4/ipt_sctp.h>

/* Initialize the match. */
static void
init(struct ipt_entry_match *m, unsigned int *nfcache)
{
	struct ipt_sctp_info *einfo = (struct ipt_sctp_info *)m->data;

	einfo->spts[1] = einfo->dpts[1] = 0xFFFF;
}

static void help(void) 
{
	printf(
"SCTP match v%s options\n"
" --sctp-chunks [!] mask comp	match when SCTP chunks & mask == comp\n"
" --source-port [!] port[:port]\n"
" --sport ...\n"
"                               match source port(s)"
" --destination-port [!] port[:port]\n"
" --dport ...\n\n",
	IPTABLES_VERSION);
}

static struct option opts[] = {
	{ .name = "source-port", .has_arg = 1, .flag = 0, .val = '1' },
	{ .name = "sport", .has_arg = 1, .flag = 0, .val = '1' },
	{ .name = "destination-port", .has_arg = 1, .flag = 0, .val = '2' },
	{ .name = "dport", .has_arg = 1, .flag = 0, .val = '2' },
	{ .name = "sctp-chunks", .has_arg = 1, .flag = 0, .val = '3' },
	{ .name = 0 }
};

static int
service_to_port(const char *name)
{
	struct servent *service;

	if ((service = getservbyname(name, "sctp")) != NULL)
		return ntohs((unsigned short) service->s_port);

	return -1;
}

static u_int16_t
parse_sctp_port(const char *port)
{
	unsigned int portnum;

	if (string_to_number(port, 0, 65535, &portnum) != -1 ||
	    (portnum = service_to_port(port)) != -1)
		return (u_int16_t)portnum;

	exit_error(PARAMETER_PROBLEM,
		   "invalid TCP port/service `%s' specified", port);
}


static void
parse_sctp_ports(const char *portstring, u_int16_t *ports)
{
	char *buffer;
	char *cp;

	buffer = strdup(portstring);
	if ((cp = strchr(buffer, ':')) == NULL)
		ports[0] = ports[1] = parse_sctp_port(buffer);
	else {
		*cp = '\0';
		cp++;

		ports[0] = buffer[0] ? parse_sctp_port(buffer) : 0;
		ports[1] = cp[0] ? parse_sctp_port(cp) : 0xFFFF;

		if (ports[0] > ports[1])
			exit_error(PARAMETER_PROBLEM,
				   "invalid portrange (min > max)");
	}
	free(buffer);
}

struct sctp_chunk_names {
	const char *name;
	unsigned int flag;
};

/* FIXME: */
#define ALL_CHUNKS	0xabcdef
static struct sctp_chunk_names sctp_chunk_names[]
= { { .name = "DATA", 		.flag = (1 << 0) },
    { .name = "INIT", 		.flag = (1 << 1) },
    { .name = "INIT_ACK", 	.flag = (1 << 2) },
    { .name = "SACK",		.flag = (1 << 3) },
    { .name = "HEARTBEAT",	.flag = (1 << 4) },
    { .name = "HEARTBEAT_ACK",	.flag = (1 << 5) },
    { .name = "ABORT",		.flag = (1 << 6) },
    { .name = "SHUTDOWN",	.flag = (1 << 7) },
    { .name = "SHUTDOWN_ACK",	.flag = (1 << 8) },
    { .name = "ERROR",		.flag = (1 << 9) },
    { .name = "COOKIE_ECHO",	.flag = (1 << 10) },
    { .name = "COOKIE_ACK",	.flag = (1 << 11) },
    { .name = "ECN_ECNE",	.flag = (1 << 12) },
    { .name = "ECN_CWR",	.flag = (1 << 13) },
    { .name = "SHUTDOWN_COMPLETE", .flag = (1 << 14) },
    { .name = "ASCONF",		.flag = (1 << 31) },
    { .name = "ASCONF_ACK",	.flag = (1 << 30) },
    { .name = "ALL", 		.flag = ALL_CHUNKS },
    { .name = "NONE",		.flag = 0 },
};


static unsigned int
parse_sctp_chunk(const char *flags)
{
	unsigned int ret = 0;
	char *ptr;
	char *buffer;

	buffer = strdup(flags);

	for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) {
		unsigned int i;
		int found = 0;
		for (i = 0;
		     i < sizeof(sctp_chunk_names)/sizeof(struct sctp_chunk_names);
		     i++) {
			if (strcasecmp(sctp_chunk_names[i].name, ptr) == 0) {
				ret |= sctp_chunk_names[i].flag;
				found = 1;
				break;
			}
		}
		if (!found)
			exit_error(PARAMETER_PROBLEM,
				   "Unknown sctp chunk `%s'", ptr);
	}

	free(buffer);
	return ret;
}

static void
parse_sctp_chunks(struct ipt_sctp_info *einfo,
		const char *mask,
		const char *cmp,
		int invert)
{
	einfo->chunks = parse_sctp_chunk(mask);
	einfo->chunk_mask = parse_sctp_chunk(cmp);

	if (invert)
		einfo->invflags |= IPT_SCTP_INV_CHUNKS;
}

#define SCTP_SRC_PORTS	0x01
#define SCTP_DST_PORTS	0x02
#define SCTP_CHUNKS	0x03

static int
parse(int c, char **argv, int invert, unsigned int *flags,
      const struct ipt_entry *entry,
      unsigned int *nfcache,
      struct ipt_entry_match **match)
{
	struct ipt_sctp_info *einfo
		= (struct ipt_sctp_info *)(*match)->data;

	switch (c) {
	case '1':
		if (*flags & SCTP_SRC_PORTS)
			exit_error(PARAMETER_PROBLEM,
			           "Only one `--source-port' allowed");
		check_inverse(optarg, &invert, &optind, 0);
		parse_sctp_ports(argv[optind-1], einfo->spts);
		if (invert)
			einfo->invflags |= IPT_SCTP_INV_SRCPT;
		*flags |= SCTP_SRC_PORTS;
		*nfcache |= NFC_IP_SRC_PT;
		break;

	case '2':
		if (*flags & SCTP_DST_PORTS)
			exit_error(PARAMETER_PROBLEM,
				   "Only one `--destination-port' allowed");
		check_inverse(optarg, &invert, &optind, 0);
		parse_sctp_ports(argv[optind-1], einfo->dpts);
		if (invert)
			einfo->invflags |= IPT_SCTP_INV_DSTPT;
		*flags |= SCTP_DST_PORTS;
		*nfcache |= NFC_IP_DST_PT;
		break;

	case '3':
		if (*flags & SCTP_CHUNKS)
			exit_error(PARAMETER_PROBLEM,
				   "Only one `--sctp-chunks' allowed");
		check_inverse(optarg, &invert, &optind, 0);

		if (!argv[optind] 
		    || argv[optind][0] == '-' || argv[optind][0] == '!')
			exit_error(PARAMETER_PROBLEM,
				   "--sctp-chunks requires two args");

		parse_sctp_chunks(einfo, argv[optind-1], argv[optind], invert);
		optind++;
		*flags |= SCTP_CHUNKS;
		break;
	default:
		return 0;
	}

	return 1;
}

static void
final_check(unsigned int flags)
{
}

static char *
port_to_service(int port)
{
	struct servent *service;

	if ((service = getservbyport(htons(port), "sctp")))
		return service->s_name;

	return NULL;
}

static void
print_port(u_int16_t port, int numeric)
{
	char *service;

	if (numeric || (service = port_to_service(port)) == NULL)
		printf("%u", port);
	else
		printf("%s", service);
}

static void
print_ports(const char *name, u_int16_t min, u_int16_t max,
	    int invert, int numeric)
{
	const char *inv = invert ? "!" : "";

	if (min != 0 || max != 0xFFFF || invert) {
		printf("%s", name);
		if (min == max) {
			printf(":%s", inv);
			print_port(min, numeric);
		} else {
			printf("s:%s", inv);
			print_port(min, numeric);
			printf(":");
			print_port(max, numeric);
		}
		printf(" ");
	}
}

static void
print_chunk(u_int32_t chunks)
{
	unsigned int have_flag = 0;

	while (chunks) {
		unsigned int i;

		for (i = 0; (chunks & sctp_chunk_names[i].flag) == 0; i++);

		if (have_flag)
			printf(",");
		printf("%s", sctp_chunk_names[i].name);
		have_flag = 1;

		chunks &= ~sctp_chunk_names[i].flag;
	}

	if (!have_flag)
		printf("NONE");
}

static void
print_chunks(u_int32_t mask, u_int32_t cmp, int invert, int numeric)
{
	if (mask || invert) {
		printf("flags:%s", invert ? "!" : "");
		if (numeric)
			printf("0x%04X/0x%04X ", mask, cmp);
		else {
			print_chunk(mask);
			printf("/");
			print_chunk(cmp);
			printf(" ");
		}
	}
}

/* Prints out the matchinfo. */
static void
print(const struct ipt_ip *ip,
      const struct ipt_entry_match *match,
      int numeric)
{
	const struct ipt_sctp_info *einfo =
		(const struct ipt_sctp_info *)match->data;

	printf("sctp ");

	print_ports("spt", einfo->spts[0], einfo->spts[1],
		    einfo->invflags & IPT_SCTP_INV_SRCPT,
		    numeric);
	print_ports("dpt", einfo->dpts[0], einfo->dpts[1],
		    einfo->invflags & IPT_SCTP_INV_DSTPT,
		    numeric);

	print_chunks(einfo->chunks, einfo->chunk_mask,
		     einfo->invflags & ~IPT_SCTP_INV_MASK,
		     numeric);
}

/* Saves the union ipt_matchinfo in parsable form to stdout. */
static void
save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
{
	const struct ipt_sctp_info *einfo =
		(const struct ipt_sctp_info *)match->data;

	if (einfo->spts[0] != 0
	    || einfo->spts[1] != 0xFFFF) {
		if (einfo->invflags & IPT_SCTP_INV_SRCPT)
			printf("! ");
		if (einfo->spts[0] != einfo->spts[1])
			printf("--sport %u:%u ", 
			       einfo->spts[0], einfo->spts[1]);
		else
			printf("--sport %u ", einfo->spts[0]);
	}

	if (einfo->dpts[0] != 0
	    || einfo->dpts[1] != 0xFFFF) {
		if (einfo->invflags & IPT_SCTP_INV_DSTPT)
			printf("! ");
		if (einfo->dpts[0] != einfo->dpts[1])
			printf("--dport %u:%u ",
			       einfo->dpts[0], einfo->dpts[1]);
		else
			printf("--dport %u ", einfo->dpts[0]);
	}

	if (einfo->chunks
	    || (einfo->invflags & IPT_SCTP_INV_CHUNKS)) {
		if (einfo->invflags & IPT_SCTP_INV_CHUNKS)
			printf("! ");
		printf("--sctp-chunks ");
		if (einfo->chunks != ALL_CHUNKS) {
			print_chunk(einfo->chunks);
		}
		printf(" ");
		print_chunk(einfo->chunk_mask);
		printf(" ");
	}
}

static
struct iptables_match sctp
= { .name          = "sctp",
    .version       = IPTABLES_VERSION,
    .size          = IPT_ALIGN(sizeof(struct ipt_sctp_info)),
    .userspacesize = IPT_ALIGN(sizeof(struct ipt_sctp_info)),
    .help          = &help,
    .init          = &init,
    .parse         = &parse,
    .final_check   = &final_check,
    .print         = &print,
    .save          = &save,
    .extra_opts    = opts
};

void _init(void)
{
	register_match(&sctp);
}