1 /* Shared library add-on to iptables for SCTP matching
2  *
3  * (C) 2003 by Harald Welte <laforge@gnumonks.org>
4  *
5  * This program is distributed under the terms of GNU GPL v2, 1991
6  *
7  * libipt_ecn.c borrowed heavily from libipt_dscp.c
8  *
9  */
10 #include <stdbool.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <getopt.h>
15 #include <netdb.h>
16 #include <ctype.h>
17 
18 #include <netinet/in.h>
19 #include <xtables.h>
20 
21 #include <linux/netfilter/xt_sctp.h>
22 
23 #if 0
24 #define DEBUGP(format, first...) printf(format, ##first)
25 #define static
26 #else
27 #define DEBUGP(format, fist...)
28 #endif
29 
30 static void
31 print_chunk(uint32_t chunknum, int numeric);
32 
sctp_init(struct xt_entry_match * m)33 static void sctp_init(struct xt_entry_match *m)
34 {
35 	int i;
36 	struct xt_sctp_info *einfo = (struct xt_sctp_info *)m->data;
37 
38 	for (i = 0; i < XT_NUM_SCTP_FLAGS; i++) {
39 		einfo->flag_info[i].chunktype = -1;
40 	}
41 }
42 
sctp_help(void)43 static void sctp_help(void)
44 {
45 	printf(
46 "sctp match options\n"
47 "[!] --source-port port[:port]                          match source port(s)\n"
48 " --sport ...\n"
49 "[!] --destination-port port[:port]                     match destination port(s)\n"
50 " --dport ...\n"
51 "[!] --chunk-types (all|any|none) (chunktype[:flags])+	match if all, any or none of\n"
52 "						        chunktypes are present\n"
53 "chunktypes - DATA INIT INIT_ACK SACK HEARTBEAT HEARTBEAT_ACK ABORT SHUTDOWN SHUTDOWN_ACK ERROR COOKIE_ECHO COOKIE_ACK ECN_ECNE ECN_CWR SHUTDOWN_COMPLETE ASCONF ASCONF_ACK FORWARD_TSN ALL NONE\n");
54 }
55 
56 static const struct option sctp_opts[] = {
57 	{.name = "source-port",      .has_arg = true, .val = '1'},
58 	{.name = "sport",            .has_arg = true, .val = '1'},
59 	{.name = "destination-port", .has_arg = true, .val = '2'},
60 	{.name = "dport",            .has_arg = true, .val = '2'},
61 	{.name = "chunk-types",      .has_arg = true, .val = '3'},
62 	XT_GETOPT_TABLEEND,
63 };
64 
65 static void
parse_sctp_ports(const char * portstring,uint16_t * ports)66 parse_sctp_ports(const char *portstring,
67 		 uint16_t *ports)
68 {
69 	char *buffer;
70 	char *cp;
71 
72 	buffer = strdup(portstring);
73 	DEBUGP("%s\n", portstring);
74 	if ((cp = strchr(buffer, ':')) == NULL) {
75 		ports[0] = ports[1] = xtables_parse_port(buffer, "sctp");
76 	}
77 	else {
78 		*cp = '\0';
79 		cp++;
80 
81 		ports[0] = buffer[0] ? xtables_parse_port(buffer, "sctp") : 0;
82 		ports[1] = cp[0] ? xtables_parse_port(cp, "sctp") : 0xFFFF;
83 
84 		if (ports[0] > ports[1])
85 			xtables_error(PARAMETER_PROBLEM,
86 				   "invalid portrange (min > max)");
87 	}
88 	free(buffer);
89 }
90 
91 struct sctp_chunk_names {
92 	const char *name;
93 	unsigned int chunk_type;
94 	const char *valid_flags;
95 };
96 
97 /*'ALL' and 'NONE' will be treated specially. */
98 static const struct sctp_chunk_names sctp_chunk_names[]
99 = { { .name = "DATA", 		.chunk_type = 0,   .valid_flags = "----IUBE"},
100     { .name = "INIT", 		.chunk_type = 1,   .valid_flags = "--------"},
101     { .name = "INIT_ACK", 	.chunk_type = 2,   .valid_flags = "--------"},
102     { .name = "SACK",		.chunk_type = 3,   .valid_flags = "--------"},
103     { .name = "HEARTBEAT",	.chunk_type = 4,   .valid_flags = "--------"},
104     { .name = "HEARTBEAT_ACK",	.chunk_type = 5,   .valid_flags = "--------"},
105     { .name = "ABORT",		.chunk_type = 6,   .valid_flags = "-------T"},
106     { .name = "SHUTDOWN",	.chunk_type = 7,   .valid_flags = "--------"},
107     { .name = "SHUTDOWN_ACK",	.chunk_type = 8,   .valid_flags = "--------"},
108     { .name = "ERROR",		.chunk_type = 9,   .valid_flags = "--------"},
109     { .name = "COOKIE_ECHO",	.chunk_type = 10,  .valid_flags = "--------"},
110     { .name = "COOKIE_ACK",	.chunk_type = 11,  .valid_flags = "--------"},
111     { .name = "ECN_ECNE",	.chunk_type = 12,  .valid_flags = "--------"},
112     { .name = "ECN_CWR",	.chunk_type = 13,  .valid_flags = "--------"},
113     { .name = "SHUTDOWN_COMPLETE", .chunk_type = 14,  .valid_flags = "-------T"},
114     { .name = "ASCONF",		.chunk_type = 193,  .valid_flags = "--------"},
115     { .name = "ASCONF_ACK",	.chunk_type = 128,  .valid_flags = "--------"},
116     { .name = "FORWARD_TSN",	.chunk_type = 192,  .valid_flags = "--------"},
117 };
118 
119 static void
save_chunk_flag_info(struct xt_sctp_flag_info * flag_info,int * flag_count,int chunktype,int bit,int set)120 save_chunk_flag_info(struct xt_sctp_flag_info *flag_info,
121 		     int *flag_count,
122 		     int chunktype,
123 		     int bit,
124 		     int set)
125 {
126 	int i;
127 
128 	for (i = 0; i < *flag_count; i++) {
129 		if (flag_info[i].chunktype == chunktype) {
130 			DEBUGP("Previous match found\n");
131 			flag_info[i].chunktype = chunktype;
132 			flag_info[i].flag_mask |= (1 << bit);
133 			if (set) {
134 				flag_info[i].flag |= (1 << bit);
135 			}
136 
137 			return;
138 		}
139 	}
140 
141 	if (*flag_count == XT_NUM_SCTP_FLAGS) {
142 		xtables_error (PARAMETER_PROBLEM,
143 			"Number of chunk types with flags exceeds currently allowed limit."
144 			"Increasing this limit involves changing IPT_NUM_SCTP_FLAGS and"
145 			"recompiling both the kernel space and user space modules\n");
146 	}
147 
148 	flag_info[*flag_count].chunktype = chunktype;
149 	flag_info[*flag_count].flag_mask |= (1 << bit);
150 	if (set) {
151 		flag_info[*flag_count].flag |= (1 << bit);
152 	}
153 	(*flag_count)++;
154 }
155 
156 static void
parse_sctp_chunk(struct xt_sctp_info * einfo,const char * chunks)157 parse_sctp_chunk(struct xt_sctp_info *einfo,
158 		 const char *chunks)
159 {
160 	char *ptr;
161 	char *buffer;
162 	unsigned int i, j;
163 	int found = 0;
164 	char *chunk_flags;
165 
166 	buffer = strdup(chunks);
167 	DEBUGP("Buffer: %s\n", buffer);
168 
169 	SCTP_CHUNKMAP_RESET(einfo->chunkmap);
170 
171 	if (!strcasecmp(buffer, "ALL")) {
172 		SCTP_CHUNKMAP_SET_ALL(einfo->chunkmap);
173 		goto out;
174 	}
175 
176 	if (!strcasecmp(buffer, "NONE")) {
177 		SCTP_CHUNKMAP_RESET(einfo->chunkmap);
178 		goto out;
179 	}
180 
181 	for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) {
182 		found = 0;
183 		DEBUGP("Next Chunk type %s\n", ptr);
184 
185 		if ((chunk_flags = strchr(ptr, ':')) != NULL) {
186 			*chunk_flags++ = 0;
187 		}
188 
189 		for (i = 0; i < ARRAY_SIZE(sctp_chunk_names); ++i)
190 			if (strcasecmp(sctp_chunk_names[i].name, ptr) == 0) {
191 				DEBUGP("Chunk num %d\n", sctp_chunk_names[i].chunk_type);
192 				SCTP_CHUNKMAP_SET(einfo->chunkmap,
193 					sctp_chunk_names[i].chunk_type);
194 				found = 1;
195 				break;
196 			}
197 		if (!found)
198 			xtables_error(PARAMETER_PROBLEM,
199 				   "Unknown sctp chunk `%s'", ptr);
200 
201 		if (chunk_flags) {
202 			DEBUGP("Chunk flags %s\n", chunk_flags);
203 			for (j = 0; j < strlen(chunk_flags); j++) {
204 				char *p;
205 				int bit;
206 
207 				if ((p = strchr(sctp_chunk_names[i].valid_flags,
208 						toupper(chunk_flags[j]))) != NULL) {
209 					bit = p - sctp_chunk_names[i].valid_flags;
210 					bit = 7 - bit;
211 
212 					save_chunk_flag_info(einfo->flag_info,
213 						&(einfo->flag_count), i, bit,
214 						isupper(chunk_flags[j]));
215 				} else {
216 					xtables_error(PARAMETER_PROBLEM,
217 						"Invalid flags for chunk type %d\n", i);
218 				}
219 			}
220 		}
221 	}
222 out:
223 	free(buffer);
224 }
225 
226 static void
parse_sctp_chunks(struct xt_sctp_info * einfo,const char * match_type,const char * chunks)227 parse_sctp_chunks(struct xt_sctp_info *einfo,
228 		  const char *match_type,
229 		  const char *chunks)
230 {
231 	DEBUGP("Match type: %s Chunks: %s\n", match_type, chunks);
232 	if (!strcasecmp(match_type, "ANY")) {
233 		einfo->chunk_match_type = SCTP_CHUNK_MATCH_ANY;
234 	} else 	if (!strcasecmp(match_type, "ALL")) {
235 		einfo->chunk_match_type = SCTP_CHUNK_MATCH_ALL;
236 	} else 	if (!strcasecmp(match_type, "ONLY")) {
237 		einfo->chunk_match_type = SCTP_CHUNK_MATCH_ONLY;
238 	} else {
239 		xtables_error (PARAMETER_PROBLEM,
240 			"Match type has to be one of \"ALL\", \"ANY\" or \"ONLY\"");
241 	}
242 
243 	SCTP_CHUNKMAP_RESET(einfo->chunkmap);
244 	parse_sctp_chunk(einfo, chunks);
245 }
246 
247 static int
sctp_parse(int c,char ** argv,int invert,unsigned int * flags,const void * entry,struct xt_entry_match ** match)248 sctp_parse(int c, char **argv, int invert, unsigned int *flags,
249            const void *entry, struct xt_entry_match **match)
250 {
251 	struct xt_sctp_info *einfo
252 		= (struct xt_sctp_info *)(*match)->data;
253 
254 	switch (c) {
255 	case '1':
256 		if (*flags & XT_SCTP_SRC_PORTS)
257 			xtables_error(PARAMETER_PROBLEM,
258 			           "Only one `--source-port' allowed");
259 		einfo->flags |= XT_SCTP_SRC_PORTS;
260 		parse_sctp_ports(optarg, einfo->spts);
261 		if (invert)
262 			einfo->invflags |= XT_SCTP_SRC_PORTS;
263 		*flags |= XT_SCTP_SRC_PORTS;
264 		break;
265 
266 	case '2':
267 		if (*flags & XT_SCTP_DEST_PORTS)
268 			xtables_error(PARAMETER_PROBLEM,
269 				   "Only one `--destination-port' allowed");
270 		einfo->flags |= XT_SCTP_DEST_PORTS;
271 		parse_sctp_ports(optarg, einfo->dpts);
272 		if (invert)
273 			einfo->invflags |= XT_SCTP_DEST_PORTS;
274 		*flags |= XT_SCTP_DEST_PORTS;
275 		break;
276 
277 	case '3':
278 		if (*flags & XT_SCTP_CHUNK_TYPES)
279 			xtables_error(PARAMETER_PROBLEM,
280 				   "Only one `--chunk-types' allowed");
281 		if (!argv[optind]
282 		    || argv[optind][0] == '-' || argv[optind][0] == '!')
283 			xtables_error(PARAMETER_PROBLEM,
284 				   "--chunk-types requires two args");
285 
286 		einfo->flags |= XT_SCTP_CHUNK_TYPES;
287 		parse_sctp_chunks(einfo, optarg, argv[optind]);
288 		if (invert)
289 			einfo->invflags |= XT_SCTP_CHUNK_TYPES;
290 		optind++;
291 		*flags |= XT_SCTP_CHUNK_TYPES;
292 		break;
293 	}
294 	return 1;
295 }
296 
297 static const char *
port_to_service(int port)298 port_to_service(int port)
299 {
300 	const struct servent *service;
301 
302 	if ((service = getservbyport(htons(port), "sctp")))
303 		return service->s_name;
304 
305 	return NULL;
306 }
307 
308 static void
print_port(uint16_t port,int numeric)309 print_port(uint16_t port, int numeric)
310 {
311 	const char *service;
312 
313 	if (numeric || (service = port_to_service(port)) == NULL)
314 		printf("%u", port);
315 	else
316 		printf("%s", service);
317 }
318 
319 static void
print_ports(const char * name,uint16_t min,uint16_t max,int invert,int numeric)320 print_ports(const char *name, uint16_t min, uint16_t max,
321 	    int invert, int numeric)
322 {
323 	const char *inv = invert ? "!" : "";
324 
325 	if (min != 0 || max != 0xFFFF || invert) {
326 		printf(" %s", name);
327 		if (min == max) {
328 			printf(":%s", inv);
329 			print_port(min, numeric);
330 		} else {
331 			printf("s:%s", inv);
332 			print_port(min, numeric);
333 			printf(":");
334 			print_port(max, numeric);
335 		}
336 	}
337 }
338 
339 static void
print_chunk_flags(uint32_t chunknum,uint8_t chunk_flags,uint8_t chunk_flags_mask)340 print_chunk_flags(uint32_t chunknum, uint8_t chunk_flags, uint8_t chunk_flags_mask)
341 {
342 	int i;
343 
344 	DEBUGP("type: %d\tflags: %x\tflag mask: %x\n", chunknum, chunk_flags,
345 			chunk_flags_mask);
346 
347 	if (chunk_flags_mask) {
348 		printf(":");
349 	}
350 
351 	for (i = 7; i >= 0; i--) {
352 		if (chunk_flags_mask & (1 << i)) {
353 			if (chunk_flags & (1 << i)) {
354 				printf("%c", sctp_chunk_names[chunknum].valid_flags[7-i]);
355 			} else {
356 				printf("%c", tolower(sctp_chunk_names[chunknum].valid_flags[7-i]));
357 			}
358 		}
359 	}
360 }
361 
362 static void
print_chunk(uint32_t chunknum,int numeric)363 print_chunk(uint32_t chunknum, int numeric)
364 {
365 	if (numeric) {
366 		printf("0x%04X", chunknum);
367 	}
368 	else {
369 		int i;
370 
371 		for (i = 0; i < ARRAY_SIZE(sctp_chunk_names); ++i)
372 			if (sctp_chunk_names[i].chunk_type == chunknum)
373 				printf("%s", sctp_chunk_names[chunknum].name);
374 	}
375 }
376 
377 static void
print_chunks(const struct xt_sctp_info * einfo,int numeric)378 print_chunks(const struct xt_sctp_info *einfo, int numeric)
379 {
380 	uint32_t chunk_match_type = einfo->chunk_match_type;
381 	const struct xt_sctp_flag_info *flag_info = einfo->flag_info;
382 	int flag_count = einfo->flag_count;
383 	int i, j;
384 	int flag;
385 
386 	switch (chunk_match_type) {
387 		case SCTP_CHUNK_MATCH_ANY:	printf(" any"); break;
388 		case SCTP_CHUNK_MATCH_ALL:	printf(" all"); break;
389 		case SCTP_CHUNK_MATCH_ONLY:	printf(" only"); break;
390 		default:	printf("Never reach here\n"); break;
391 	}
392 
393 	if (SCTP_CHUNKMAP_IS_CLEAR(einfo->chunkmap)) {
394 		printf(" NONE");
395 		goto out;
396 	}
397 
398 	if (SCTP_CHUNKMAP_IS_ALL_SET(einfo->chunkmap)) {
399 		printf(" ALL");
400 		goto out;
401 	}
402 
403 	flag = 0;
404 	for (i = 0; i < 256; i++) {
405 		if (SCTP_CHUNKMAP_IS_SET(einfo->chunkmap, i)) {
406 			if (flag)
407 				printf(",");
408 			else
409 				putchar(' ');
410 			flag = 1;
411 			print_chunk(i, numeric);
412 			for (j = 0; j < flag_count; j++) {
413 				if (flag_info[j].chunktype == i) {
414 					print_chunk_flags(i, flag_info[j].flag,
415 						flag_info[j].flag_mask);
416 				}
417 			}
418 		}
419 	}
420 out:
421 	return;
422 }
423 
424 static void
sctp_print(const void * ip,const struct xt_entry_match * match,int numeric)425 sctp_print(const void *ip, const struct xt_entry_match *match, int numeric)
426 {
427 	const struct xt_sctp_info *einfo =
428 		(const struct xt_sctp_info *)match->data;
429 
430 	printf(" sctp");
431 
432 	if (einfo->flags & XT_SCTP_SRC_PORTS) {
433 		print_ports("spt", einfo->spts[0], einfo->spts[1],
434 			einfo->invflags & XT_SCTP_SRC_PORTS,
435 			numeric);
436 	}
437 
438 	if (einfo->flags & XT_SCTP_DEST_PORTS) {
439 		print_ports("dpt", einfo->dpts[0], einfo->dpts[1],
440 			einfo->invflags & XT_SCTP_DEST_PORTS,
441 			numeric);
442 	}
443 
444 	if (einfo->flags & XT_SCTP_CHUNK_TYPES) {
445 		/* FIXME: print_chunks() is used in save() where the printing of '!'
446 		s taken care of, so we need to do that here as well */
447 		if (einfo->invflags & XT_SCTP_CHUNK_TYPES) {
448 			printf(" !");
449 		}
450 		print_chunks(einfo, numeric);
451 	}
452 }
453 
sctp_save(const void * ip,const struct xt_entry_match * match)454 static void sctp_save(const void *ip, const struct xt_entry_match *match)
455 {
456 	const struct xt_sctp_info *einfo =
457 		(const struct xt_sctp_info *)match->data;
458 
459 	if (einfo->flags & XT_SCTP_SRC_PORTS) {
460 		if (einfo->invflags & XT_SCTP_SRC_PORTS)
461 			printf(" !");
462 		if (einfo->spts[0] != einfo->spts[1])
463 			printf(" --sport %u:%u",
464 			       einfo->spts[0], einfo->spts[1]);
465 		else
466 			printf(" --sport %u", einfo->spts[0]);
467 	}
468 
469 	if (einfo->flags & XT_SCTP_DEST_PORTS) {
470 		if (einfo->invflags & XT_SCTP_DEST_PORTS)
471 			printf(" !");
472 		if (einfo->dpts[0] != einfo->dpts[1])
473 			printf(" --dport %u:%u",
474 			       einfo->dpts[0], einfo->dpts[1]);
475 		else
476 			printf(" --dport %u", einfo->dpts[0]);
477 	}
478 
479 	if (einfo->flags & XT_SCTP_CHUNK_TYPES) {
480 		if (einfo->invflags & XT_SCTP_CHUNK_TYPES)
481 			printf(" !");
482 		printf(" --chunk-types");
483 
484 		print_chunks(einfo, 0);
485 	}
486 }
487 
sctp_xlate(struct xt_xlate * xl,const struct xt_xlate_mt_params * params)488 static int sctp_xlate(struct xt_xlate *xl,
489 		      const struct xt_xlate_mt_params *params)
490 {
491 	const struct xt_sctp_info *einfo =
492 		(const struct xt_sctp_info *)params->match->data;
493 	char *space = "";
494 
495 	if (!einfo->flags)
496 		return 0;
497 
498 	xt_xlate_add(xl, "sctp ");
499 
500 	if (einfo->flags & XT_SCTP_SRC_PORTS) {
501 		if (einfo->spts[0] != einfo->spts[1])
502 			xt_xlate_add(xl, "sport%s %u-%u",
503 				     einfo->invflags & XT_SCTP_SRC_PORTS ? " !=" : "",
504 				     einfo->spts[0], einfo->spts[1]);
505 		else
506 			xt_xlate_add(xl, "sport%s %u",
507 				     einfo->invflags & XT_SCTP_SRC_PORTS ? " !=" : "",
508 				     einfo->spts[0]);
509 		space = " ";
510 	}
511 
512 	if (einfo->flags & XT_SCTP_DEST_PORTS) {
513 		if (einfo->dpts[0] != einfo->dpts[1])
514 			xt_xlate_add(xl, "%sdport%s %u-%u", space,
515 				     einfo->invflags & XT_SCTP_DEST_PORTS ? " !=" : "",
516 				     einfo->dpts[0], einfo->dpts[1]);
517 		else
518 			xt_xlate_add(xl, "%sdport%s %u", space,
519 				     einfo->invflags & XT_SCTP_DEST_PORTS ? " !=" : "",
520 				     einfo->dpts[0]);
521 	}
522 
523 	return 1;
524 }
525 
526 static struct xtables_match sctp_match = {
527 	.name		= "sctp",
528 	.family		= NFPROTO_UNSPEC,
529 	.version	= XTABLES_VERSION,
530 	.size		= XT_ALIGN(sizeof(struct xt_sctp_info)),
531 	.userspacesize	= XT_ALIGN(sizeof(struct xt_sctp_info)),
532 	.help		= sctp_help,
533 	.init		= sctp_init,
534 	.parse		= sctp_parse,
535 	.print		= sctp_print,
536 	.save		= sctp_save,
537 	.extra_opts	= sctp_opts,
538 	.xlate		= sctp_xlate,
539 };
540 
_init(void)541 void _init(void)
542 {
543 	xtables_register_match(&sctp_match);
544 }
545