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