1 /* Shared library add-on to iptables for SCTP matching
2 *
3 * (C) 2003 by Harald Welte <[email protected]>
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 I_DATA RE_CONFIG PAD ASCONF ASCONF_ACK FORWARD_TSN I_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 = xtables_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 const char *nftname;
96 };
97
98 /*'ALL' and 'NONE' will be treated specially. */
99 static const struct sctp_chunk_names sctp_chunk_names[]
100 = { { .name = "DATA", .chunk_type = 0, .valid_flags = "----IUBE", .nftname = "data" },
101 { .name = "INIT", .chunk_type = 1, .valid_flags = "--------", .nftname = "init" },
102 { .name = "INIT_ACK", .chunk_type = 2, .valid_flags = "--------", .nftname = "init-ack" },
103 { .name = "SACK", .chunk_type = 3, .valid_flags = "--------", .nftname = "sack" },
104 { .name = "HEARTBEAT", .chunk_type = 4, .valid_flags = "--------", .nftname = "heartbeat" },
105 { .name = "HEARTBEAT_ACK", .chunk_type = 5, .valid_flags = "--------", .nftname = "heartbeat-ack" },
106 { .name = "ABORT", .chunk_type = 6, .valid_flags = "-------T", .nftname = "abort" },
107 { .name = "SHUTDOWN", .chunk_type = 7, .valid_flags = "--------", .nftname = "shutdown" },
108 { .name = "SHUTDOWN_ACK", .chunk_type = 8, .valid_flags = "--------", .nftname = "shutdown-ack" },
109 { .name = "ERROR", .chunk_type = 9, .valid_flags = "--------", .nftname = "error" },
110 { .name = "COOKIE_ECHO", .chunk_type = 10, .valid_flags = "--------", .nftname = "cookie-echo" },
111 { .name = "COOKIE_ACK", .chunk_type = 11, .valid_flags = "--------", .nftname = "cookie-ack" },
112 { .name = "ECN_ECNE", .chunk_type = 12, .valid_flags = "--------", .nftname = "ecne" },
113 { .name = "ECN_CWR", .chunk_type = 13, .valid_flags = "--------", .nftname = "cwr" },
114 { .name = "SHUTDOWN_COMPLETE", .chunk_type = 14, .valid_flags = "-------T", .nftname = "shutdown-complete" },
115 { .name = "I_DATA", .chunk_type = 64, .valid_flags = "----IUBE", .nftname = "i-data"},
116 { .name = "RE_CONFIG", .chunk_type = 130, .valid_flags = "--------", .nftname = "re-config"},
117 { .name = "PAD", .chunk_type = 132, .valid_flags = "--------", .nftname = "pad"},
118 { .name = "ASCONF", .chunk_type = 193, .valid_flags = "--------", .nftname = "asconf" },
119 { .name = "ASCONF_ACK", .chunk_type = 128, .valid_flags = "--------", .nftname = "asconf-ack" },
120 { .name = "FORWARD_TSN", .chunk_type = 192, .valid_flags = "--------", .nftname = "forward-tsn" },
121 { .name = "I_FORWARD_TSN", .chunk_type = 194, .valid_flags = "--------", .nftname = "i-forward-tsn" },
122 };
123
124 static void
save_chunk_flag_info(struct xt_sctp_flag_info * flag_info,int * flag_count,int chunktype,int bit,int set)125 save_chunk_flag_info(struct xt_sctp_flag_info *flag_info,
126 int *flag_count,
127 int chunktype,
128 int bit,
129 int set)
130 {
131 int i;
132
133 for (i = 0; i < *flag_count; i++) {
134 if (flag_info[i].chunktype == chunktype) {
135 DEBUGP("Previous match found\n");
136 flag_info[i].chunktype = chunktype;
137 flag_info[i].flag_mask |= (1 << bit);
138 if (set) {
139 flag_info[i].flag |= (1 << bit);
140 }
141
142 return;
143 }
144 }
145
146 if (*flag_count == XT_NUM_SCTP_FLAGS) {
147 xtables_error(PARAMETER_PROBLEM,
148 "Number of chunk types with flags exceeds currently allowed limit. Increasing this limit involves changing IPT_NUM_SCTP_FLAGS and recompiling both the kernel space and user space modules");
149 }
150
151 flag_info[*flag_count].chunktype = chunktype;
152 flag_info[*flag_count].flag_mask |= (1 << bit);
153 if (set) {
154 flag_info[*flag_count].flag |= (1 << bit);
155 }
156 (*flag_count)++;
157 }
158
159 static void
parse_sctp_chunk(struct xt_sctp_info * einfo,const char * chunks)160 parse_sctp_chunk(struct xt_sctp_info *einfo,
161 const char *chunks)
162 {
163 char *ptr;
164 char *buffer;
165 unsigned int i, j;
166 int found = 0;
167 char *chunk_flags;
168
169 buffer = xtables_strdup(chunks);
170 DEBUGP("Buffer: %s\n", buffer);
171
172 SCTP_CHUNKMAP_RESET(einfo->chunkmap);
173
174 if (!strcasecmp(buffer, "ALL")) {
175 SCTP_CHUNKMAP_SET_ALL(einfo->chunkmap);
176 goto out;
177 }
178
179 if (!strcasecmp(buffer, "NONE")) {
180 SCTP_CHUNKMAP_RESET(einfo->chunkmap);
181 goto out;
182 }
183
184 for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) {
185 found = 0;
186 DEBUGP("Next Chunk type %s\n", ptr);
187
188 if ((chunk_flags = strchr(ptr, ':')) != NULL) {
189 *chunk_flags++ = 0;
190 }
191
192 for (i = 0; i < ARRAY_SIZE(sctp_chunk_names); ++i)
193 if (strcasecmp(sctp_chunk_names[i].name, ptr) == 0) {
194 DEBUGP("Chunk num %d\n", sctp_chunk_names[i].chunk_type);
195 SCTP_CHUNKMAP_SET(einfo->chunkmap,
196 sctp_chunk_names[i].chunk_type);
197 found = 1;
198 break;
199 }
200 if (!found)
201 xtables_error(PARAMETER_PROBLEM,
202 "Unknown sctp chunk `%s'", ptr);
203
204 if (chunk_flags) {
205 DEBUGP("Chunk flags %s\n", chunk_flags);
206 for (j = 0; j < strlen(chunk_flags); j++) {
207 char *p;
208 int bit;
209
210 if ((p = strchr(sctp_chunk_names[i].valid_flags,
211 toupper(chunk_flags[j]))) != NULL) {
212 bit = p - sctp_chunk_names[i].valid_flags;
213 bit = 7 - bit;
214
215 save_chunk_flag_info(einfo->flag_info,
216 &(einfo->flag_count), i, bit,
217 isupper(chunk_flags[j]));
218 } else {
219 xtables_error(PARAMETER_PROBLEM,
220 "Invalid flags for chunk type %d",
221 i);
222 }
223 }
224 }
225 }
226 out:
227 free(buffer);
228 }
229
230 static void
parse_sctp_chunks(struct xt_sctp_info * einfo,const char * match_type,const char * chunks)231 parse_sctp_chunks(struct xt_sctp_info *einfo,
232 const char *match_type,
233 const char *chunks)
234 {
235 DEBUGP("Match type: %s Chunks: %s\n", match_type, chunks);
236 if (!strcasecmp(match_type, "ANY")) {
237 einfo->chunk_match_type = SCTP_CHUNK_MATCH_ANY;
238 } else if (!strcasecmp(match_type, "ALL")) {
239 einfo->chunk_match_type = SCTP_CHUNK_MATCH_ALL;
240 } else if (!strcasecmp(match_type, "ONLY")) {
241 einfo->chunk_match_type = SCTP_CHUNK_MATCH_ONLY;
242 } else {
243 xtables_error (PARAMETER_PROBLEM,
244 "Match type has to be one of \"ALL\", \"ANY\" or \"ONLY\"");
245 }
246
247 SCTP_CHUNKMAP_RESET(einfo->chunkmap);
248 parse_sctp_chunk(einfo, chunks);
249 }
250
251 static int
sctp_parse(int c,char ** argv,int invert,unsigned int * flags,const void * entry,struct xt_entry_match ** match)252 sctp_parse(int c, char **argv, int invert, unsigned int *flags,
253 const void *entry, struct xt_entry_match **match)
254 {
255 struct xt_sctp_info *einfo
256 = (struct xt_sctp_info *)(*match)->data;
257
258 switch (c) {
259 case '1':
260 if (*flags & XT_SCTP_SRC_PORTS)
261 xtables_error(PARAMETER_PROBLEM,
262 "Only one `--source-port' allowed");
263 einfo->flags |= XT_SCTP_SRC_PORTS;
264 parse_sctp_ports(optarg, einfo->spts);
265 if (invert)
266 einfo->invflags |= XT_SCTP_SRC_PORTS;
267 *flags |= XT_SCTP_SRC_PORTS;
268 break;
269
270 case '2':
271 if (*flags & XT_SCTP_DEST_PORTS)
272 xtables_error(PARAMETER_PROBLEM,
273 "Only one `--destination-port' allowed");
274 einfo->flags |= XT_SCTP_DEST_PORTS;
275 parse_sctp_ports(optarg, einfo->dpts);
276 if (invert)
277 einfo->invflags |= XT_SCTP_DEST_PORTS;
278 *flags |= XT_SCTP_DEST_PORTS;
279 break;
280
281 case '3':
282 if (*flags & XT_SCTP_CHUNK_TYPES)
283 xtables_error(PARAMETER_PROBLEM,
284 "Only one `--chunk-types' allowed");
285 if (!argv[optind]
286 || argv[optind][0] == '-' || argv[optind][0] == '!')
287 xtables_error(PARAMETER_PROBLEM,
288 "--chunk-types requires two args");
289
290 einfo->flags |= XT_SCTP_CHUNK_TYPES;
291 parse_sctp_chunks(einfo, optarg, argv[optind]);
292 if (invert)
293 einfo->invflags |= XT_SCTP_CHUNK_TYPES;
294 optind++;
295 *flags |= XT_SCTP_CHUNK_TYPES;
296 break;
297 }
298 return 1;
299 }
300
301 static const char *
port_to_service(int port)302 port_to_service(int port)
303 {
304 const struct servent *service;
305
306 if ((service = getservbyport(htons(port), "sctp")))
307 return service->s_name;
308
309 return NULL;
310 }
311
312 static void
print_port(uint16_t port,int numeric)313 print_port(uint16_t port, int numeric)
314 {
315 const char *service;
316
317 if (numeric || (service = port_to_service(port)) == NULL)
318 printf("%u", port);
319 else
320 printf("%s", service);
321 }
322
323 static void
print_ports(const char * name,uint16_t min,uint16_t max,int invert,int numeric)324 print_ports(const char *name, uint16_t min, uint16_t max,
325 int invert, int numeric)
326 {
327 const char *inv = invert ? "!" : "";
328
329 if (min != 0 || max != 0xFFFF || invert) {
330 printf(" %s", name);
331 if (min == max) {
332 printf(":%s", inv);
333 print_port(min, numeric);
334 } else {
335 printf("s:%s", inv);
336 print_port(min, numeric);
337 printf(":");
338 print_port(max, numeric);
339 }
340 }
341 }
342
343 static void
print_chunk_flags(uint32_t chunknum,uint8_t chunk_flags,uint8_t chunk_flags_mask)344 print_chunk_flags(uint32_t chunknum, uint8_t chunk_flags, uint8_t chunk_flags_mask)
345 {
346 int i;
347
348 DEBUGP("type: %d\tflags: %x\tflag mask: %x\n", chunknum, chunk_flags,
349 chunk_flags_mask);
350
351 if (chunk_flags_mask) {
352 printf(":");
353 }
354
355 for (i = 7; i >= 0; i--) {
356 if (chunk_flags_mask & (1 << i)) {
357 if (chunk_flags & (1 << i)) {
358 printf("%c", sctp_chunk_names[chunknum].valid_flags[7-i]);
359 } else {
360 printf("%c", tolower(sctp_chunk_names[chunknum].valid_flags[7-i]));
361 }
362 }
363 }
364 }
365
366 static void
print_chunk(uint32_t chunknum,int numeric)367 print_chunk(uint32_t chunknum, int numeric)
368 {
369 if (numeric) {
370 printf("0x%04X", chunknum);
371 }
372 else {
373 int i;
374
375 for (i = 0; i < ARRAY_SIZE(sctp_chunk_names); ++i)
376 if (sctp_chunk_names[i].chunk_type == chunknum)
377 printf("%s", sctp_chunk_names[i].name);
378 }
379 }
380
381 static void
print_chunks(const struct xt_sctp_info * einfo,int numeric)382 print_chunks(const struct xt_sctp_info *einfo, int numeric)
383 {
384 uint32_t chunk_match_type = einfo->chunk_match_type;
385 const struct xt_sctp_flag_info *flag_info = einfo->flag_info;
386 int flag_count = einfo->flag_count;
387 int i, j;
388 int flag;
389
390 switch (chunk_match_type) {
391 case SCTP_CHUNK_MATCH_ANY: printf(" any"); break;
392 case SCTP_CHUNK_MATCH_ALL: printf(" all"); break;
393 case SCTP_CHUNK_MATCH_ONLY: printf(" only"); break;
394 default: printf("Never reach here\n"); break;
395 }
396
397 if (SCTP_CHUNKMAP_IS_CLEAR(einfo->chunkmap)) {
398 printf(" NONE");
399 goto out;
400 }
401
402 if (SCTP_CHUNKMAP_IS_ALL_SET(einfo->chunkmap)) {
403 printf(" ALL");
404 goto out;
405 }
406
407 flag = 0;
408 for (i = 0; i < 256; i++) {
409 if (SCTP_CHUNKMAP_IS_SET(einfo->chunkmap, i)) {
410 if (flag)
411 printf(",");
412 else
413 putchar(' ');
414 flag = 1;
415 print_chunk(i, numeric);
416 for (j = 0; j < flag_count; j++) {
417 if (flag_info[j].chunktype == i) {
418 print_chunk_flags(i, flag_info[j].flag,
419 flag_info[j].flag_mask);
420 }
421 }
422 }
423 }
424 out:
425 return;
426 }
427
428 static void
sctp_print(const void * ip,const struct xt_entry_match * match,int numeric)429 sctp_print(const void *ip, const struct xt_entry_match *match, int numeric)
430 {
431 const struct xt_sctp_info *einfo =
432 (const struct xt_sctp_info *)match->data;
433
434 printf(" sctp");
435
436 if (einfo->flags & XT_SCTP_SRC_PORTS) {
437 print_ports("spt", einfo->spts[0], einfo->spts[1],
438 einfo->invflags & XT_SCTP_SRC_PORTS,
439 numeric);
440 }
441
442 if (einfo->flags & XT_SCTP_DEST_PORTS) {
443 print_ports("dpt", einfo->dpts[0], einfo->dpts[1],
444 einfo->invflags & XT_SCTP_DEST_PORTS,
445 numeric);
446 }
447
448 if (einfo->flags & XT_SCTP_CHUNK_TYPES) {
449 /* FIXME: print_chunks() is used in save() where the printing of '!'
450 s taken care of, so we need to do that here as well */
451 if (einfo->invflags & XT_SCTP_CHUNK_TYPES) {
452 printf(" !");
453 }
454 print_chunks(einfo, numeric);
455 }
456 }
457
sctp_save(const void * ip,const struct xt_entry_match * match)458 static void sctp_save(const void *ip, const struct xt_entry_match *match)
459 {
460 const struct xt_sctp_info *einfo =
461 (const struct xt_sctp_info *)match->data;
462
463 if (einfo->flags & XT_SCTP_SRC_PORTS) {
464 if (einfo->invflags & XT_SCTP_SRC_PORTS)
465 printf(" !");
466 if (einfo->spts[0] != einfo->spts[1])
467 printf(" --sport %u:%u",
468 einfo->spts[0], einfo->spts[1]);
469 else
470 printf(" --sport %u", einfo->spts[0]);
471 }
472
473 if (einfo->flags & XT_SCTP_DEST_PORTS) {
474 if (einfo->invflags & XT_SCTP_DEST_PORTS)
475 printf(" !");
476 if (einfo->dpts[0] != einfo->dpts[1])
477 printf(" --dport %u:%u",
478 einfo->dpts[0], einfo->dpts[1]);
479 else
480 printf(" --dport %u", einfo->dpts[0]);
481 }
482
483 if (einfo->flags & XT_SCTP_CHUNK_TYPES) {
484 if (einfo->invflags & XT_SCTP_CHUNK_TYPES)
485 printf(" !");
486 printf(" --chunk-types");
487
488 print_chunks(einfo, 0);
489 }
490 }
491
sctp_xlate_chunk(struct xt_xlate * xl,const struct xt_sctp_info * einfo,const struct sctp_chunk_names * scn)492 static void sctp_xlate_chunk(struct xt_xlate *xl,
493 const struct xt_sctp_info *einfo,
494 const struct sctp_chunk_names *scn)
495 {
496 bool inv = einfo->invflags & XT_SCTP_CHUNK_TYPES;
497 const struct xt_sctp_flag_info *flag_info = NULL;
498 int i;
499
500 if (!scn->nftname)
501 return;
502
503 if (!SCTP_CHUNKMAP_IS_SET(einfo->chunkmap, scn->chunk_type)) {
504 if (einfo->chunk_match_type != SCTP_CHUNK_MATCH_ONLY)
505 return;
506
507 xt_xlate_add(xl, "sctp chunk %s %s",
508 scn->nftname, inv ? "exists" : "missing");
509 return;
510 }
511
512 for (i = 0; i < einfo->flag_count; i++) {
513 if (einfo->flag_info[i].chunktype == scn->chunk_type) {
514 flag_info = &einfo->flag_info[i];
515 break;
516 }
517 }
518
519 if (!flag_info) {
520 xt_xlate_add(xl, "sctp chunk %s %s",
521 scn->nftname, inv ? "missing" : "exists");
522 return;
523 }
524
525 xt_xlate_add(xl, "sctp chunk %s flags & 0x%x %s 0x%x",
526 scn->nftname, flag_info->flag_mask,
527 inv ? "!=" : "==", flag_info->flag);
528 }
529
sctp_xlate(struct xt_xlate * xl,const struct xt_xlate_mt_params * params)530 static int sctp_xlate(struct xt_xlate *xl,
531 const struct xt_xlate_mt_params *params)
532 {
533 const struct xt_sctp_info *einfo =
534 (const struct xt_sctp_info *)params->match->data;
535
536 if (!einfo->flags)
537 return 0;
538
539 if (einfo->flags & XT_SCTP_SRC_PORTS) {
540 if (einfo->spts[0] != einfo->spts[1])
541 xt_xlate_add(xl, "sctp sport%s %u-%u",
542 einfo->invflags & XT_SCTP_SRC_PORTS ? " !=" : "",
543 einfo->spts[0], einfo->spts[1]);
544 else
545 xt_xlate_add(xl, "sctp sport%s %u",
546 einfo->invflags & XT_SCTP_SRC_PORTS ? " !=" : "",
547 einfo->spts[0]);
548 }
549
550 if (einfo->flags & XT_SCTP_DEST_PORTS) {
551 if (einfo->dpts[0] != einfo->dpts[1])
552 xt_xlate_add(xl, "sctp dport%s %u-%u",
553 einfo->invflags & XT_SCTP_DEST_PORTS ? " !=" : "",
554 einfo->dpts[0], einfo->dpts[1]);
555 else
556 xt_xlate_add(xl, "sctp dport%s %u",
557 einfo->invflags & XT_SCTP_DEST_PORTS ? " !=" : "",
558 einfo->dpts[0]);
559 }
560
561 if (einfo->flags & XT_SCTP_CHUNK_TYPES) {
562 int i;
563
564 if (einfo->chunk_match_type == SCTP_CHUNK_MATCH_ANY)
565 return 0;
566
567 for (i = 0; i < ARRAY_SIZE(sctp_chunk_names); i++)
568 sctp_xlate_chunk(xl, einfo, &sctp_chunk_names[i]);
569 }
570
571 return 1;
572 }
573
574 static struct xtables_match sctp_match = {
575 .name = "sctp",
576 .family = NFPROTO_UNSPEC,
577 .version = XTABLES_VERSION,
578 .size = XT_ALIGN(sizeof(struct xt_sctp_info)),
579 .userspacesize = XT_ALIGN(sizeof(struct xt_sctp_info)),
580 .help = sctp_help,
581 .init = sctp_init,
582 .parse = sctp_parse,
583 .print = sctp_print,
584 .save = sctp_save,
585 .extra_opts = sctp_opts,
586 .xlate = sctp_xlate,
587 };
588
_init(void)589 void _init(void)
590 {
591 xtables_register_match(&sctp_match);
592 }
593