blob: 4f1d13f564871c03e2eb60ff6bc031ae48036c74 [file] [log] [blame]
Patrick McHardy38100132006-11-13 19:38:44 +00001/* 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 <stdio.h>
11#include <string.h>
12#include <stdlib.h>
13#include <getopt.h>
14#include <netdb.h>
15#include <ctype.h>
16
Yasuyuki KOZAKAI19f29502007-07-24 07:02:26 +000017#include <xtables.h>
Patrick McHardy38100132006-11-13 19:38:44 +000018
19#ifndef ARRAY_SIZE
20#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
21#endif
22
23#include <linux/netfilter/xt_sctp.h>
24
25/* Some ZS!#@:$%*#$! has replaced the ELEMCOUNT macro in ipt_sctp.h with
Jan Engelhardtddac6c52008-09-01 14:22:19 +020026 * ARRAY_SIZE without noticing that this file is used from userspace,
Patrick McHardy38100132006-11-13 19:38:44 +000027 * and userspace doesn't have ARRAY_SIZE */
28
29#ifndef ELEMCOUNT
30#define ELEMCOUNT ARRAY_SIZE
31#endif
32
33#if 0
34#define DEBUGP(format, first...) printf(format, ##first)
35#define static
36#else
37#define DEBUGP(format, fist...)
38#endif
39
40static void
41print_chunk(u_int32_t chunknum, int numeric);
42
Jan Engelhardt181dead2007-10-04 16:27:07 +000043static void sctp_init(struct xt_entry_match *m)
Patrick McHardy38100132006-11-13 19:38:44 +000044{
45 int i;
46 struct xt_sctp_info *einfo = (struct xt_sctp_info *)m->data;
47
48 memset(einfo, 0, sizeof(struct xt_sctp_info));
49
50 for (i = 0; i < XT_NUM_SCTP_FLAGS; i++) {
51 einfo->flag_info[i].chunktype = -1;
52 }
53}
54
Jan Engelhardt181dead2007-10-04 16:27:07 +000055static void sctp_help(void)
Patrick McHardy38100132006-11-13 19:38:44 +000056{
57 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020058"sctp match options\n"
Jan Engelhardt96727922008-08-13 14:42:41 +020059"[!] --source-port port[:port] match source port(s)\n"
Patrick McHardy38100132006-11-13 19:38:44 +000060" --sport ...\n"
Jan Engelhardt96727922008-08-13 14:42:41 +020061"[!] --destination-port port[:port] match destination port(s)\n"
Patrick McHardy38100132006-11-13 19:38:44 +000062" --dport ...\n"
Jan Engelhardt96727922008-08-13 14:42:41 +020063"[!] --chunk-types (all|any|none) (chunktype[:flags])+ match if all, any or none of\n"
Patrick McHardy38100132006-11-13 19:38:44 +000064" chunktypes are present\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020065"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 ALL NONE\n");
Patrick McHardy38100132006-11-13 19:38:44 +000066}
67
Jan Engelhardt181dead2007-10-04 16:27:07 +000068static const struct option sctp_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000069 { .name = "source-port", .has_arg = 1, .val = '1' },
70 { .name = "sport", .has_arg = 1, .val = '1' },
71 { .name = "destination-port", .has_arg = 1, .val = '2' },
72 { .name = "dport", .has_arg = 1, .val = '2' },
73 { .name = "chunk-types", .has_arg = 1, .val = '3' },
Max Kellermann9ee386a2008-01-29 13:48:05 +000074 { .name = NULL }
Patrick McHardy38100132006-11-13 19:38:44 +000075};
76
77static void
78parse_sctp_ports(const char *portstring,
79 u_int16_t *ports)
80{
81 char *buffer;
82 char *cp;
83
84 buffer = strdup(portstring);
85 DEBUGP("%s\n", portstring);
86 if ((cp = strchr(buffer, ':')) == NULL) {
87 ports[0] = ports[1] = parse_port(buffer, "sctp");
88 }
89 else {
90 *cp = '\0';
91 cp++;
92
93 ports[0] = buffer[0] ? parse_port(buffer, "sctp") : 0;
94 ports[1] = cp[0] ? parse_port(cp, "sctp") : 0xFFFF;
95
96 if (ports[0] > ports[1])
97 exit_error(PARAMETER_PROBLEM,
98 "invalid portrange (min > max)");
99 }
100 free(buffer);
101}
102
103struct sctp_chunk_names {
104 const char *name;
105 unsigned int chunk_type;
106 const char *valid_flags;
107};
108
109/*'ALL' and 'NONE' will be treated specially. */
Jan Engelhardt0e2abed2007-10-04 16:25:58 +0000110static const struct sctp_chunk_names sctp_chunk_names[]
Patrick McHardy38100132006-11-13 19:38:44 +0000111= { { .name = "DATA", .chunk_type = 0, .valid_flags = "-----UBE"},
112 { .name = "INIT", .chunk_type = 1, .valid_flags = "--------"},
113 { .name = "INIT_ACK", .chunk_type = 2, .valid_flags = "--------"},
114 { .name = "SACK", .chunk_type = 3, .valid_flags = "--------"},
115 { .name = "HEARTBEAT", .chunk_type = 4, .valid_flags = "--------"},
116 { .name = "HEARTBEAT_ACK", .chunk_type = 5, .valid_flags = "--------"},
117 { .name = "ABORT", .chunk_type = 6, .valid_flags = "-------T"},
118 { .name = "SHUTDOWN", .chunk_type = 7, .valid_flags = "--------"},
119 { .name = "SHUTDOWN_ACK", .chunk_type = 8, .valid_flags = "--------"},
120 { .name = "ERROR", .chunk_type = 9, .valid_flags = "--------"},
121 { .name = "COOKIE_ECHO", .chunk_type = 10, .valid_flags = "--------"},
122 { .name = "COOKIE_ACK", .chunk_type = 11, .valid_flags = "--------"},
123 { .name = "ECN_ECNE", .chunk_type = 12, .valid_flags = "--------"},
124 { .name = "ECN_CWR", .chunk_type = 13, .valid_flags = "--------"},
125 { .name = "SHUTDOWN_COMPLETE", .chunk_type = 14, .valid_flags = "-------T"},
Naohiro Ooiwaecd7f5e2008-02-29 12:45:27 +0000126 { .name = "ASCONF", .chunk_type = 193, .valid_flags = "--------"},
127 { .name = "ASCONF_ACK", .chunk_type = 128, .valid_flags = "--------"},
Patrick McHardy38100132006-11-13 19:38:44 +0000128};
129
130static void
131save_chunk_flag_info(struct xt_sctp_flag_info *flag_info,
132 int *flag_count,
133 int chunktype,
134 int bit,
135 int set)
136{
137 int i;
138
139 for (i = 0; i < *flag_count; i++) {
140 if (flag_info[i].chunktype == chunktype) {
141 DEBUGP("Previous match found\n");
142 flag_info[i].chunktype = chunktype;
143 flag_info[i].flag_mask |= (1 << bit);
144 if (set) {
145 flag_info[i].flag |= (1 << bit);
146 }
147
148 return;
149 }
150 }
151
152 if (*flag_count == XT_NUM_SCTP_FLAGS) {
153 exit_error (PARAMETER_PROBLEM,
154 "Number of chunk types with flags exceeds currently allowed limit."
Yasuyuki KOZAKAI19f29502007-07-24 07:02:26 +0000155 "Increasing this limit involves changing IPT_NUM_SCTP_FLAGS and"
Patrick McHardy38100132006-11-13 19:38:44 +0000156 "recompiling both the kernel space and user space modules\n");
157 }
158
159 flag_info[*flag_count].chunktype = chunktype;
160 flag_info[*flag_count].flag_mask |= (1 << bit);
161 if (set) {
162 flag_info[*flag_count].flag |= (1 << bit);
163 }
164 (*flag_count)++;
165}
166
167static void
168parse_sctp_chunk(struct xt_sctp_info *einfo,
169 const char *chunks)
170{
171 char *ptr;
172 char *buffer;
173 unsigned int i, j;
174 int found = 0;
175 char *chunk_flags;
176
177 buffer = strdup(chunks);
178 DEBUGP("Buffer: %s\n", buffer);
179
180 SCTP_CHUNKMAP_RESET(einfo->chunkmap);
181
182 if (!strcasecmp(buffer, "ALL")) {
183 SCTP_CHUNKMAP_SET_ALL(einfo->chunkmap);
184 goto out;
185 }
186
187 if (!strcasecmp(buffer, "NONE")) {
188 SCTP_CHUNKMAP_RESET(einfo->chunkmap);
189 goto out;
190 }
191
192 for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) {
193 found = 0;
194 DEBUGP("Next Chunk type %s\n", ptr);
195
196 if ((chunk_flags = strchr(ptr, ':')) != NULL) {
197 *chunk_flags++ = 0;
198 }
199
200 for (i = 0; i < ELEMCOUNT(sctp_chunk_names); i++) {
201 if (strcasecmp(sctp_chunk_names[i].name, ptr) == 0) {
202 DEBUGP("Chunk num %d\n", sctp_chunk_names[i].chunk_type);
203 SCTP_CHUNKMAP_SET(einfo->chunkmap,
204 sctp_chunk_names[i].chunk_type);
205 found = 1;
206 break;
207 }
208 }
209 if (!found)
210 exit_error(PARAMETER_PROBLEM,
211 "Unknown sctp chunk `%s'", ptr);
212
213 if (chunk_flags) {
214 DEBUGP("Chunk flags %s\n", chunk_flags);
215 for (j = 0; j < strlen(chunk_flags); j++) {
216 char *p;
217 int bit;
218
219 if ((p = strchr(sctp_chunk_names[i].valid_flags,
220 toupper(chunk_flags[j]))) != NULL) {
221 bit = p - sctp_chunk_names[i].valid_flags;
222 bit = 7 - bit;
223
224 save_chunk_flag_info(einfo->flag_info,
225 &(einfo->flag_count), i, bit,
226 isupper(chunk_flags[j]));
227 } else {
228 exit_error(PARAMETER_PROBLEM,
229 "Invalid flags for chunk type %d\n", i);
230 }
231 }
232 }
233 }
234out:
235 free(buffer);
236}
237
238static void
239parse_sctp_chunks(struct xt_sctp_info *einfo,
240 const char *match_type,
241 const char *chunks)
242{
243 DEBUGP("Match type: %s Chunks: %s\n", match_type, chunks);
244 if (!strcasecmp(match_type, "ANY")) {
245 einfo->chunk_match_type = SCTP_CHUNK_MATCH_ANY;
246 } else if (!strcasecmp(match_type, "ALL")) {
247 einfo->chunk_match_type = SCTP_CHUNK_MATCH_ALL;
248 } else if (!strcasecmp(match_type, "ONLY")) {
249 einfo->chunk_match_type = SCTP_CHUNK_MATCH_ONLY;
250 } else {
251 exit_error (PARAMETER_PROBLEM,
252 "Match type has to be one of \"ALL\", \"ANY\" or \"ONLY\"");
253 }
254
255 SCTP_CHUNKMAP_RESET(einfo->chunkmap);
256 parse_sctp_chunk(einfo, chunks);
257}
258
259static int
Jan Engelhardt181dead2007-10-04 16:27:07 +0000260sctp_parse(int c, char **argv, int invert, unsigned int *flags,
261 const void *entry, struct xt_entry_match **match)
Patrick McHardy38100132006-11-13 19:38:44 +0000262{
263 struct xt_sctp_info *einfo
264 = (struct xt_sctp_info *)(*match)->data;
265
266 switch (c) {
267 case '1':
268 if (*flags & XT_SCTP_SRC_PORTS)
269 exit_error(PARAMETER_PROBLEM,
270 "Only one `--source-port' allowed");
271 einfo->flags |= XT_SCTP_SRC_PORTS;
272 check_inverse(optarg, &invert, &optind, 0);
273 parse_sctp_ports(argv[optind-1], einfo->spts);
274 if (invert)
275 einfo->invflags |= XT_SCTP_SRC_PORTS;
276 *flags |= XT_SCTP_SRC_PORTS;
277 break;
278
279 case '2':
280 if (*flags & XT_SCTP_DEST_PORTS)
281 exit_error(PARAMETER_PROBLEM,
282 "Only one `--destination-port' allowed");
283 einfo->flags |= XT_SCTP_DEST_PORTS;
284 check_inverse(optarg, &invert, &optind, 0);
285 parse_sctp_ports(argv[optind-1], einfo->dpts);
286 if (invert)
287 einfo->invflags |= XT_SCTP_DEST_PORTS;
288 *flags |= XT_SCTP_DEST_PORTS;
289 break;
290
291 case '3':
292 if (*flags & XT_SCTP_CHUNK_TYPES)
293 exit_error(PARAMETER_PROBLEM,
294 "Only one `--chunk-types' allowed");
295 check_inverse(optarg, &invert, &optind, 0);
296
297 if (!argv[optind]
298 || argv[optind][0] == '-' || argv[optind][0] == '!')
299 exit_error(PARAMETER_PROBLEM,
300 "--chunk-types requires two args");
301
302 einfo->flags |= XT_SCTP_CHUNK_TYPES;
303 parse_sctp_chunks(einfo, argv[optind-1], argv[optind]);
304 if (invert)
305 einfo->invflags |= XT_SCTP_CHUNK_TYPES;
306 optind++;
307 *flags |= XT_SCTP_CHUNK_TYPES;
308 break;
309
310 default:
311 return 0;
312 }
313 return 1;
314}
315
Patrick McHardy38100132006-11-13 19:38:44 +0000316static char *
317port_to_service(int port)
318{
319 struct servent *service;
320
321 if ((service = getservbyport(htons(port), "sctp")))
322 return service->s_name;
323
324 return NULL;
325}
326
327static void
328print_port(u_int16_t port, int numeric)
329{
330 char *service;
331
332 if (numeric || (service = port_to_service(port)) == NULL)
333 printf("%u", port);
334 else
335 printf("%s", service);
336}
337
338static void
339print_ports(const char *name, u_int16_t min, u_int16_t max,
340 int invert, int numeric)
341{
342 const char *inv = invert ? "!" : "";
343
344 if (min != 0 || max != 0xFFFF || invert) {
345 printf("%s", name);
346 if (min == max) {
347 printf(":%s", inv);
348 print_port(min, numeric);
349 } else {
350 printf("s:%s", inv);
351 print_port(min, numeric);
352 printf(":");
353 print_port(max, numeric);
354 }
355 printf(" ");
356 }
357}
358
359static void
360print_chunk_flags(u_int32_t chunknum, u_int8_t chunk_flags, u_int8_t chunk_flags_mask)
361{
362 int i;
363
364 DEBUGP("type: %d\tflags: %x\tflag mask: %x\n", chunknum, chunk_flags,
365 chunk_flags_mask);
366
367 if (chunk_flags_mask) {
368 printf(":");
369 }
370
371 for (i = 7; i >= 0; i--) {
372 if (chunk_flags_mask & (1 << i)) {
373 if (chunk_flags & (1 << i)) {
374 printf("%c", sctp_chunk_names[chunknum].valid_flags[7-i]);
375 } else {
376 printf("%c", tolower(sctp_chunk_names[chunknum].valid_flags[7-i]));
377 }
378 }
379 }
380}
381
382static void
383print_chunk(u_int32_t chunknum, int numeric)
384{
385 if (numeric) {
386 printf("0x%04X", chunknum);
387 }
388 else {
389 int i;
390
391 for (i = 0; i < ELEMCOUNT(sctp_chunk_names); i++) {
392 if (sctp_chunk_names[i].chunk_type == chunknum)
393 printf("%s", sctp_chunk_names[chunknum].name);
394 }
395 }
396}
397
398static void
Li Zefan1f25b402007-10-18 09:12:49 +0000399print_chunks(const struct xt_sctp_info *einfo, int numeric)
Patrick McHardy38100132006-11-13 19:38:44 +0000400{
Li Zefan1f25b402007-10-18 09:12:49 +0000401 u_int32_t chunk_match_type = einfo->chunk_match_type;
402 const struct xt_sctp_flag_info *flag_info = einfo->flag_info;
403 int flag_count = einfo->flag_count;
Patrick McHardy38100132006-11-13 19:38:44 +0000404 int i, j;
405 int flag;
406
407 switch (chunk_match_type) {
408 case SCTP_CHUNK_MATCH_ANY: printf("any "); break;
409 case SCTP_CHUNK_MATCH_ALL: printf("all "); break;
410 case SCTP_CHUNK_MATCH_ONLY: printf("only "); break;
411 default: printf("Never reach herer\n"); break;
412 }
413
Li Zefan1f25b402007-10-18 09:12:49 +0000414 if (SCTP_CHUNKMAP_IS_CLEAR(einfo->chunkmap)) {
Patrick McHardy38100132006-11-13 19:38:44 +0000415 printf("NONE ");
416 goto out;
417 }
418
Li Zefan1f25b402007-10-18 09:12:49 +0000419 if (SCTP_CHUNKMAP_IS_ALL_SET(einfo->chunkmap)) {
Patrick McHardy38100132006-11-13 19:38:44 +0000420 printf("ALL ");
421 goto out;
422 }
423
424 flag = 0;
425 for (i = 0; i < 256; i++) {
Li Zefan1f25b402007-10-18 09:12:49 +0000426 if (SCTP_CHUNKMAP_IS_SET(einfo->chunkmap, i)) {
Patrick McHardy38100132006-11-13 19:38:44 +0000427 if (flag)
428 printf(",");
429 flag = 1;
430 print_chunk(i, numeric);
431 for (j = 0; j < flag_count; j++) {
432 if (flag_info[j].chunktype == i) {
433 print_chunk_flags(i, flag_info[j].flag,
434 flag_info[j].flag_mask);
435 }
436 }
437 }
438 }
439
440 if (flag)
441 printf(" ");
442out:
443 return;
444}
445
Patrick McHardy38100132006-11-13 19:38:44 +0000446static void
Jan Engelhardt181dead2007-10-04 16:27:07 +0000447sctp_print(const void *ip, const struct xt_entry_match *match, int numeric)
Patrick McHardy38100132006-11-13 19:38:44 +0000448{
449 const struct xt_sctp_info *einfo =
450 (const struct xt_sctp_info *)match->data;
451
452 printf("sctp ");
453
454 if (einfo->flags & XT_SCTP_SRC_PORTS) {
455 print_ports("spt", einfo->spts[0], einfo->spts[1],
456 einfo->invflags & XT_SCTP_SRC_PORTS,
457 numeric);
458 }
459
460 if (einfo->flags & XT_SCTP_DEST_PORTS) {
461 print_ports("dpt", einfo->dpts[0], einfo->dpts[1],
462 einfo->invflags & XT_SCTP_DEST_PORTS,
463 numeric);
464 }
465
466 if (einfo->flags & XT_SCTP_CHUNK_TYPES) {
467 /* FIXME: print_chunks() is used in save() where the printing of '!'
468 s taken care of, so we need to do that here as well */
469 if (einfo->invflags & XT_SCTP_CHUNK_TYPES) {
470 printf("! ");
471 }
Li Zefan1f25b402007-10-18 09:12:49 +0000472 print_chunks(einfo, numeric);
Patrick McHardy38100132006-11-13 19:38:44 +0000473 }
474}
475
Jan Engelhardt181dead2007-10-04 16:27:07 +0000476static void sctp_save(const void *ip, const struct xt_entry_match *match)
Patrick McHardy38100132006-11-13 19:38:44 +0000477{
478 const struct xt_sctp_info *einfo =
479 (const struct xt_sctp_info *)match->data;
480
481 if (einfo->flags & XT_SCTP_SRC_PORTS) {
482 if (einfo->invflags & XT_SCTP_SRC_PORTS)
483 printf("! ");
484 if (einfo->spts[0] != einfo->spts[1])
485 printf("--sport %u:%u ",
486 einfo->spts[0], einfo->spts[1]);
487 else
488 printf("--sport %u ", einfo->spts[0]);
489 }
490
491 if (einfo->flags & XT_SCTP_DEST_PORTS) {
492 if (einfo->invflags & XT_SCTP_DEST_PORTS)
493 printf("! ");
494 if (einfo->dpts[0] != einfo->dpts[1])
495 printf("--dport %u:%u ",
496 einfo->dpts[0], einfo->dpts[1]);
497 else
498 printf("--dport %u ", einfo->dpts[0]);
499 }
500
501 if (einfo->flags & XT_SCTP_CHUNK_TYPES) {
502 if (einfo->invflags & XT_SCTP_CHUNK_TYPES)
503 printf("! ");
504 printf("--chunk-types ");
505
Li Zefan1f25b402007-10-18 09:12:49 +0000506 print_chunks(einfo, 0);
Patrick McHardy38100132006-11-13 19:38:44 +0000507 }
508}
509
Jan Engelhardt181dead2007-10-04 16:27:07 +0000510static struct xtables_match sctp_match = {
Yasuyuki KOZAKAI19f29502007-07-24 07:02:26 +0000511 .name = "sctp",
512 .family = AF_INET,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200513 .version = XTABLES_VERSION,
Yasuyuki KOZAKAI19f29502007-07-24 07:02:26 +0000514 .size = XT_ALIGN(sizeof(struct xt_sctp_info)),
515 .userspacesize = XT_ALIGN(sizeof(struct xt_sctp_info)),
Jan Engelhardt181dead2007-10-04 16:27:07 +0000516 .help = sctp_help,
517 .init = sctp_init,
518 .parse = sctp_parse,
519 .print = sctp_print,
520 .save = sctp_save,
521 .extra_opts = sctp_opts,
Yasuyuki KOZAKAI19f29502007-07-24 07:02:26 +0000522};
523
Jan Engelhardt181dead2007-10-04 16:27:07 +0000524static struct xtables_match sctp_match6 = {
Yasuyuki KOZAKAI19f29502007-07-24 07:02:26 +0000525 .name = "sctp",
526 .family = AF_INET6,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200527 .version = XTABLES_VERSION,
Yasuyuki KOZAKAI19f29502007-07-24 07:02:26 +0000528 .size = XT_ALIGN(sizeof(struct xt_sctp_info)),
529 .userspacesize = XT_ALIGN(sizeof(struct xt_sctp_info)),
Jan Engelhardt181dead2007-10-04 16:27:07 +0000530 .help = sctp_help,
531 .init = sctp_init,
532 .parse = sctp_parse,
533 .print = sctp_print,
534 .save = sctp_save,
535 .extra_opts = sctp_opts,
Patrick McHardy38100132006-11-13 19:38:44 +0000536};
537
538void _init(void)
539{
Jan Engelhardt181dead2007-10-04 16:27:07 +0000540 xtables_register_match(&sctp_match);
541 xtables_register_match(&sctp_match6);
Patrick McHardy38100132006-11-13 19:38:44 +0000542}