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
20 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
23 #include <linux/netfilter/xt_sctp.h>
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 */
30 #define ELEMCOUNT ARRAY_SIZE
34 #define DEBUGP(format, first...) printf(format, ##first)
37 #define DEBUGP(format, fist...)
41 print_chunk(u_int32_t chunknum, int numeric);
43 /* Initialize the match. */
44 static void sctp_init(struct xt_entry_match *m)
47 struct xt_sctp_info *einfo = (struct xt_sctp_info *)m->data;
49 memset(einfo, 0, sizeof(struct xt_sctp_info));
51 for (i = 0; i < XT_NUM_SCTP_FLAGS; i++) {
52 einfo->flag_info[i].chunktype = -1;
56 static void sctp_help(void)
59 "sctp match options\n"
60 " --source-port [!] port[:port] match source port(s)\n"
62 " --destination-port [!] port[:port] match destination port(s)\n"
64 " --chunk-types [!] (all|any|none) (chunktype[:flags])+ match if all, any or none of\n"
65 " chunktypes are present\n"
66 "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 static const struct option sctp_opts[] = {
70 { .name = "source-port", .has_arg = 1, .val = '1' },
71 { .name = "sport", .has_arg = 1, .val = '1' },
72 { .name = "destination-port", .has_arg = 1, .val = '2' },
73 { .name = "dport", .has_arg = 1, .val = '2' },
74 { .name = "chunk-types", .has_arg = 1, .val = '3' },
79 parse_sctp_ports(const char *portstring,
85 buffer = strdup(portstring);
86 DEBUGP("%s\n", portstring);
87 if ((cp = strchr(buffer, ':')) == NULL) {
88 ports[0] = ports[1] = parse_port(buffer, "sctp");
94 ports[0] = buffer[0] ? parse_port(buffer, "sctp") : 0;
95 ports[1] = cp[0] ? parse_port(cp, "sctp") : 0xFFFF;
97 if (ports[0] > ports[1])
98 exit_error(PARAMETER_PROBLEM,
99 "invalid portrange (min > max)");
104 struct sctp_chunk_names {
106 unsigned int chunk_type;
107 const char *valid_flags;
110 /*'ALL' and 'NONE' will be treated specially. */
111 static const struct sctp_chunk_names sctp_chunk_names[]
112 = { { .name = "DATA", .chunk_type = 0, .valid_flags = "-----UBE"},
113 { .name = "INIT", .chunk_type = 1, .valid_flags = "--------"},
114 { .name = "INIT_ACK", .chunk_type = 2, .valid_flags = "--------"},
115 { .name = "SACK", .chunk_type = 3, .valid_flags = "--------"},
116 { .name = "HEARTBEAT", .chunk_type = 4, .valid_flags = "--------"},
117 { .name = "HEARTBEAT_ACK", .chunk_type = 5, .valid_flags = "--------"},
118 { .name = "ABORT", .chunk_type = 6, .valid_flags = "-------T"},
119 { .name = "SHUTDOWN", .chunk_type = 7, .valid_flags = "--------"},
120 { .name = "SHUTDOWN_ACK", .chunk_type = 8, .valid_flags = "--------"},
121 { .name = "ERROR", .chunk_type = 9, .valid_flags = "--------"},
122 { .name = "COOKIE_ECHO", .chunk_type = 10, .valid_flags = "--------"},
123 { .name = "COOKIE_ACK", .chunk_type = 11, .valid_flags = "--------"},
124 { .name = "ECN_ECNE", .chunk_type = 12, .valid_flags = "--------"},
125 { .name = "ECN_CWR", .chunk_type = 13, .valid_flags = "--------"},
126 { .name = "SHUTDOWN_COMPLETE", .chunk_type = 14, .valid_flags = "-------T"},
127 { .name = "ASCONF", .chunk_type = 193, .valid_flags = "--------"},
128 { .name = "ASCONF_ACK", .chunk_type = 128, .valid_flags = "--------"},
132 save_chunk_flag_info(struct xt_sctp_flag_info *flag_info,
140 for (i = 0; i < *flag_count; i++) {
141 if (flag_info[i].chunktype == chunktype) {
142 DEBUGP("Previous match found\n");
143 flag_info[i].chunktype = chunktype;
144 flag_info[i].flag_mask |= (1 << bit);
146 flag_info[i].flag |= (1 << bit);
153 if (*flag_count == XT_NUM_SCTP_FLAGS) {
154 exit_error (PARAMETER_PROBLEM,
155 "Number of chunk types with flags exceeds currently allowed limit."
156 "Increasing this limit involves changing IPT_NUM_SCTP_FLAGS and"
157 "recompiling both the kernel space and user space modules\n");
160 flag_info[*flag_count].chunktype = chunktype;
161 flag_info[*flag_count].flag_mask |= (1 << bit);
163 flag_info[*flag_count].flag |= (1 << bit);
169 parse_sctp_chunk(struct xt_sctp_info *einfo,
178 buffer = strdup(chunks);
179 DEBUGP("Buffer: %s\n", buffer);
181 SCTP_CHUNKMAP_RESET(einfo->chunkmap);
183 if (!strcasecmp(buffer, "ALL")) {
184 SCTP_CHUNKMAP_SET_ALL(einfo->chunkmap);
188 if (!strcasecmp(buffer, "NONE")) {
189 SCTP_CHUNKMAP_RESET(einfo->chunkmap);
193 for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) {
195 DEBUGP("Next Chunk type %s\n", ptr);
197 if ((chunk_flags = strchr(ptr, ':')) != NULL) {
201 for (i = 0; i < ELEMCOUNT(sctp_chunk_names); i++) {
202 if (strcasecmp(sctp_chunk_names[i].name, ptr) == 0) {
203 DEBUGP("Chunk num %d\n", sctp_chunk_names[i].chunk_type);
204 SCTP_CHUNKMAP_SET(einfo->chunkmap,
205 sctp_chunk_names[i].chunk_type);
211 exit_error(PARAMETER_PROBLEM,
212 "Unknown sctp chunk `%s'", ptr);
215 DEBUGP("Chunk flags %s\n", chunk_flags);
216 for (j = 0; j < strlen(chunk_flags); j++) {
220 if ((p = strchr(sctp_chunk_names[i].valid_flags,
221 toupper(chunk_flags[j]))) != NULL) {
222 bit = p - sctp_chunk_names[i].valid_flags;
225 save_chunk_flag_info(einfo->flag_info,
226 &(einfo->flag_count), i, bit,
227 isupper(chunk_flags[j]));
229 exit_error(PARAMETER_PROBLEM,
230 "Invalid flags for chunk type %d\n", i);
240 parse_sctp_chunks(struct xt_sctp_info *einfo,
241 const char *match_type,
244 DEBUGP("Match type: %s Chunks: %s\n", match_type, chunks);
245 if (!strcasecmp(match_type, "ANY")) {
246 einfo->chunk_match_type = SCTP_CHUNK_MATCH_ANY;
247 } else if (!strcasecmp(match_type, "ALL")) {
248 einfo->chunk_match_type = SCTP_CHUNK_MATCH_ALL;
249 } else if (!strcasecmp(match_type, "ONLY")) {
250 einfo->chunk_match_type = SCTP_CHUNK_MATCH_ONLY;
252 exit_error (PARAMETER_PROBLEM,
253 "Match type has to be one of \"ALL\", \"ANY\" or \"ONLY\"");
256 SCTP_CHUNKMAP_RESET(einfo->chunkmap);
257 parse_sctp_chunk(einfo, chunks);
261 sctp_parse(int c, char **argv, int invert, unsigned int *flags,
262 const void *entry, struct xt_entry_match **match)
264 struct xt_sctp_info *einfo
265 = (struct xt_sctp_info *)(*match)->data;
269 if (*flags & XT_SCTP_SRC_PORTS)
270 exit_error(PARAMETER_PROBLEM,
271 "Only one `--source-port' allowed");
272 einfo->flags |= XT_SCTP_SRC_PORTS;
273 check_inverse(optarg, &invert, &optind, 0);
274 parse_sctp_ports(argv[optind-1], einfo->spts);
276 einfo->invflags |= XT_SCTP_SRC_PORTS;
277 *flags |= XT_SCTP_SRC_PORTS;
281 if (*flags & XT_SCTP_DEST_PORTS)
282 exit_error(PARAMETER_PROBLEM,
283 "Only one `--destination-port' allowed");
284 einfo->flags |= XT_SCTP_DEST_PORTS;
285 check_inverse(optarg, &invert, &optind, 0);
286 parse_sctp_ports(argv[optind-1], einfo->dpts);
288 einfo->invflags |= XT_SCTP_DEST_PORTS;
289 *flags |= XT_SCTP_DEST_PORTS;
293 if (*flags & XT_SCTP_CHUNK_TYPES)
294 exit_error(PARAMETER_PROBLEM,
295 "Only one `--chunk-types' allowed");
296 check_inverse(optarg, &invert, &optind, 0);
299 || argv[optind][0] == '-' || argv[optind][0] == '!')
300 exit_error(PARAMETER_PROBLEM,
301 "--chunk-types requires two args");
303 einfo->flags |= XT_SCTP_CHUNK_TYPES;
304 parse_sctp_chunks(einfo, argv[optind-1], argv[optind]);
306 einfo->invflags |= XT_SCTP_CHUNK_TYPES;
308 *flags |= XT_SCTP_CHUNK_TYPES;
318 port_to_service(int port)
320 struct servent *service;
322 if ((service = getservbyport(htons(port), "sctp")))
323 return service->s_name;
329 print_port(u_int16_t port, int numeric)
333 if (numeric || (service = port_to_service(port)) == NULL)
336 printf("%s", service);
340 print_ports(const char *name, u_int16_t min, u_int16_t max,
341 int invert, int numeric)
343 const char *inv = invert ? "!" : "";
345 if (min != 0 || max != 0xFFFF || invert) {
349 print_port(min, numeric);
352 print_port(min, numeric);
354 print_port(max, numeric);
361 print_chunk_flags(u_int32_t chunknum, u_int8_t chunk_flags, u_int8_t chunk_flags_mask)
365 DEBUGP("type: %d\tflags: %x\tflag mask: %x\n", chunknum, chunk_flags,
368 if (chunk_flags_mask) {
372 for (i = 7; i >= 0; i--) {
373 if (chunk_flags_mask & (1 << i)) {
374 if (chunk_flags & (1 << i)) {
375 printf("%c", sctp_chunk_names[chunknum].valid_flags[7-i]);
377 printf("%c", tolower(sctp_chunk_names[chunknum].valid_flags[7-i]));
384 print_chunk(u_int32_t chunknum, int numeric)
387 printf("0x%04X", chunknum);
392 for (i = 0; i < ELEMCOUNT(sctp_chunk_names); i++) {
393 if (sctp_chunk_names[i].chunk_type == chunknum)
394 printf("%s", sctp_chunk_names[chunknum].name);
400 print_chunks(const struct xt_sctp_info *einfo, int numeric)
402 u_int32_t chunk_match_type = einfo->chunk_match_type;
403 const struct xt_sctp_flag_info *flag_info = einfo->flag_info;
404 int flag_count = einfo->flag_count;
408 switch (chunk_match_type) {
409 case SCTP_CHUNK_MATCH_ANY: printf("any "); break;
410 case SCTP_CHUNK_MATCH_ALL: printf("all "); break;
411 case SCTP_CHUNK_MATCH_ONLY: printf("only "); break;
412 default: printf("Never reach herer\n"); break;
415 if (SCTP_CHUNKMAP_IS_CLEAR(einfo->chunkmap)) {
420 if (SCTP_CHUNKMAP_IS_ALL_SET(einfo->chunkmap)) {
426 for (i = 0; i < 256; i++) {
427 if (SCTP_CHUNKMAP_IS_SET(einfo->chunkmap, i)) {
431 print_chunk(i, numeric);
432 for (j = 0; j < flag_count; j++) {
433 if (flag_info[j].chunktype == i) {
434 print_chunk_flags(i, flag_info[j].flag,
435 flag_info[j].flag_mask);
447 /* Prints out the matchinfo. */
449 sctp_print(const void *ip, const struct xt_entry_match *match, int numeric)
451 const struct xt_sctp_info *einfo =
452 (const struct xt_sctp_info *)match->data;
456 if (einfo->flags & XT_SCTP_SRC_PORTS) {
457 print_ports("spt", einfo->spts[0], einfo->spts[1],
458 einfo->invflags & XT_SCTP_SRC_PORTS,
462 if (einfo->flags & XT_SCTP_DEST_PORTS) {
463 print_ports("dpt", einfo->dpts[0], einfo->dpts[1],
464 einfo->invflags & XT_SCTP_DEST_PORTS,
468 if (einfo->flags & XT_SCTP_CHUNK_TYPES) {
469 /* FIXME: print_chunks() is used in save() where the printing of '!'
470 s taken care of, so we need to do that here as well */
471 if (einfo->invflags & XT_SCTP_CHUNK_TYPES) {
474 print_chunks(einfo, numeric);
478 /* Saves the union ipt_matchinfo in parsable form to stdout. */
479 static void sctp_save(const void *ip, const struct xt_entry_match *match)
481 const struct xt_sctp_info *einfo =
482 (const struct xt_sctp_info *)match->data;
484 if (einfo->flags & XT_SCTP_SRC_PORTS) {
485 if (einfo->invflags & XT_SCTP_SRC_PORTS)
487 if (einfo->spts[0] != einfo->spts[1])
488 printf("--sport %u:%u ",
489 einfo->spts[0], einfo->spts[1]);
491 printf("--sport %u ", einfo->spts[0]);
494 if (einfo->flags & XT_SCTP_DEST_PORTS) {
495 if (einfo->invflags & XT_SCTP_DEST_PORTS)
497 if (einfo->dpts[0] != einfo->dpts[1])
498 printf("--dport %u:%u ",
499 einfo->dpts[0], einfo->dpts[1]);
501 printf("--dport %u ", einfo->dpts[0]);
504 if (einfo->flags & XT_SCTP_CHUNK_TYPES) {
505 if (einfo->invflags & XT_SCTP_CHUNK_TYPES)
507 printf("--chunk-types ");
509 print_chunks(einfo, 0);
513 static struct xtables_match sctp_match = {
516 .version = XTABLES_VERSION,
517 .size = XT_ALIGN(sizeof(struct xt_sctp_info)),
518 .userspacesize = XT_ALIGN(sizeof(struct xt_sctp_info)),
524 .extra_opts = sctp_opts,
527 static struct xtables_match sctp_match6 = {
530 .version = XTABLES_VERSION,
531 .size = XT_ALIGN(sizeof(struct xt_sctp_info)),
532 .userspacesize = XT_ALIGN(sizeof(struct xt_sctp_info)),
538 .extra_opts = sctp_opts,
543 xtables_register_match(&sctp_match);
544 xtables_register_match(&sctp_match6);