1 /* Shared library add-on to iptables for SCTP matching
3 * (C) 2003 by Harald Welte <laforge@gnumonks.org>
5 * This program is distributed under the terms of GNU GPL v2, 1991
7 * libipt_ecn.c borrowed heavily from libipt_dscp.c
18 #include <netinet/in.h>
21 #include <linux/netfilter/xt_sctp.h>
24 #define DEBUGP(format, first...) printf(format, ##first)
27 #define DEBUGP(format, fist...)
31 print_chunk(uint32_t chunknum, int numeric);
33 static void sctp_init(struct xt_entry_match *m)
36 struct xt_sctp_info *einfo = (struct xt_sctp_info *)m->data;
38 for (i = 0; i < XT_NUM_SCTP_FLAGS; i++) {
39 einfo->flag_info[i].chunktype = -1;
43 static void sctp_help(void)
46 "sctp match options\n"
47 "[!] --source-port port[:port] match source port(s)\n"
49 "[!] --destination-port port[:port] match destination port(s)\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");
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'},
66 parse_sctp_ports(const char *portstring,
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");
81 ports[0] = buffer[0] ? xtables_parse_port(buffer, "sctp") : 0;
82 ports[1] = cp[0] ? xtables_parse_port(cp, "sctp") : 0xFFFF;
84 if (ports[0] > ports[1])
85 xtables_error(PARAMETER_PROBLEM,
86 "invalid portrange (min > max)");
91 struct sctp_chunk_names {
93 unsigned int chunk_type;
94 const char *valid_flags;
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 = "--------"},
120 save_chunk_flag_info(struct xt_sctp_flag_info *flag_info,
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);
134 flag_info[i].flag |= (1 << bit);
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");
148 flag_info[*flag_count].chunktype = chunktype;
149 flag_info[*flag_count].flag_mask |= (1 << bit);
151 flag_info[*flag_count].flag |= (1 << bit);
157 parse_sctp_chunk(struct xt_sctp_info *einfo,
166 buffer = strdup(chunks);
167 DEBUGP("Buffer: %s\n", buffer);
169 SCTP_CHUNKMAP_RESET(einfo->chunkmap);
171 if (!strcasecmp(buffer, "ALL")) {
172 SCTP_CHUNKMAP_SET_ALL(einfo->chunkmap);
176 if (!strcasecmp(buffer, "NONE")) {
177 SCTP_CHUNKMAP_RESET(einfo->chunkmap);
181 for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) {
183 DEBUGP("Next Chunk type %s\n", ptr);
185 if ((chunk_flags = strchr(ptr, ':')) != NULL) {
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);
198 xtables_error(PARAMETER_PROBLEM,
199 "Unknown sctp chunk `%s'", ptr);
202 DEBUGP("Chunk flags %s\n", chunk_flags);
203 for (j = 0; j < strlen(chunk_flags); j++) {
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;
212 save_chunk_flag_info(einfo->flag_info,
213 &(einfo->flag_count), i, bit,
214 isupper(chunk_flags[j]));
216 xtables_error(PARAMETER_PROBLEM,
217 "Invalid flags for chunk type %d\n", i);
227 parse_sctp_chunks(struct xt_sctp_info *einfo,
228 const char *match_type,
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;
239 xtables_error (PARAMETER_PROBLEM,
240 "Match type has to be one of \"ALL\", \"ANY\" or \"ONLY\"");
243 SCTP_CHUNKMAP_RESET(einfo->chunkmap);
244 parse_sctp_chunk(einfo, chunks);
248 sctp_parse(int c, char **argv, int invert, unsigned int *flags,
249 const void *entry, struct xt_entry_match **match)
251 struct xt_sctp_info *einfo
252 = (struct xt_sctp_info *)(*match)->data;
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);
262 einfo->invflags |= XT_SCTP_SRC_PORTS;
263 *flags |= XT_SCTP_SRC_PORTS;
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);
273 einfo->invflags |= XT_SCTP_DEST_PORTS;
274 *flags |= XT_SCTP_DEST_PORTS;
278 if (*flags & XT_SCTP_CHUNK_TYPES)
279 xtables_error(PARAMETER_PROBLEM,
280 "Only one `--chunk-types' allowed");
282 || argv[optind][0] == '-' || argv[optind][0] == '!')
283 xtables_error(PARAMETER_PROBLEM,
284 "--chunk-types requires two args");
286 einfo->flags |= XT_SCTP_CHUNK_TYPES;
287 parse_sctp_chunks(einfo, optarg, argv[optind]);
289 einfo->invflags |= XT_SCTP_CHUNK_TYPES;
291 *flags |= XT_SCTP_CHUNK_TYPES;
298 port_to_service(int port)
300 const struct servent *service;
302 if ((service = getservbyport(htons(port), "sctp")))
303 return service->s_name;
309 print_port(uint16_t port, int numeric)
313 if (numeric || (service = port_to_service(port)) == NULL)
316 printf("%s", service);
320 print_ports(const char *name, uint16_t min, uint16_t max,
321 int invert, int numeric)
323 const char *inv = invert ? "!" : "";
325 if (min != 0 || max != 0xFFFF || invert) {
329 print_port(min, numeric);
332 print_port(min, numeric);
334 print_port(max, numeric);
340 print_chunk_flags(uint32_t chunknum, uint8_t chunk_flags, uint8_t chunk_flags_mask)
344 DEBUGP("type: %d\tflags: %x\tflag mask: %x\n", chunknum, chunk_flags,
347 if (chunk_flags_mask) {
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]);
356 printf("%c", tolower(sctp_chunk_names[chunknum].valid_flags[7-i]));
363 print_chunk(uint32_t chunknum, int numeric)
366 printf("0x%04X", chunknum);
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);
378 print_chunks(const struct xt_sctp_info *einfo, int numeric)
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;
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;
393 if (SCTP_CHUNKMAP_IS_CLEAR(einfo->chunkmap)) {
398 if (SCTP_CHUNKMAP_IS_ALL_SET(einfo->chunkmap)) {
404 for (i = 0; i < 256; i++) {
405 if (SCTP_CHUNKMAP_IS_SET(einfo->chunkmap, i)) {
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);
425 sctp_print(const void *ip, const struct xt_entry_match *match, int numeric)
427 const struct xt_sctp_info *einfo =
428 (const struct xt_sctp_info *)match->data;
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,
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,
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) {
450 print_chunks(einfo, numeric);
454 static void sctp_save(const void *ip, const struct xt_entry_match *match)
456 const struct xt_sctp_info *einfo =
457 (const struct xt_sctp_info *)match->data;
459 if (einfo->flags & XT_SCTP_SRC_PORTS) {
460 if (einfo->invflags & XT_SCTP_SRC_PORTS)
462 if (einfo->spts[0] != einfo->spts[1])
463 printf(" --sport %u:%u",
464 einfo->spts[0], einfo->spts[1]);
466 printf(" --sport %u", einfo->spts[0]);
469 if (einfo->flags & XT_SCTP_DEST_PORTS) {
470 if (einfo->invflags & XT_SCTP_DEST_PORTS)
472 if (einfo->dpts[0] != einfo->dpts[1])
473 printf(" --dport %u:%u",
474 einfo->dpts[0], einfo->dpts[1]);
476 printf(" --dport %u", einfo->dpts[0]);
479 if (einfo->flags & XT_SCTP_CHUNK_TYPES) {
480 if (einfo->invflags & XT_SCTP_CHUNK_TYPES)
482 printf(" --chunk-types");
484 print_chunks(einfo, 0);
488 static struct xtables_match sctp_match = {
490 .family = NFPROTO_UNSPEC,
491 .version = XTABLES_VERSION,
492 .size = XT_ALIGN(sizeof(struct xt_sctp_info)),
493 .userspacesize = XT_ALIGN(sizeof(struct xt_sctp_info)),
499 .extra_opts = sctp_opts,
504 xtables_register_match(&sctp_match);