Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 1 | /* 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 | */ |
Jan Engelhardt | 32b8e61 | 2010-07-23 21:16:14 +0200 | [diff] [blame] | 10 | #include <stdbool.h> |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 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 | |
Thomas Jarosch | ecae0c3 | 2008-10-23 15:41:27 +0200 | [diff] [blame] | 18 | #include <netinet/in.h> |
Yasuyuki KOZAKAI | 19f2950 | 2007-07-24 07:02:26 +0000 | [diff] [blame] | 19 | #include <xtables.h> |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 20 | |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 21 | #include <linux/netfilter/xt_sctp.h> |
| 22 | |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 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 |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 31 | print_chunk(uint32_t chunknum, int numeric); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 32 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 33 | static void sctp_init(struct xt_entry_match *m) |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 34 | { |
| 35 | int i; |
| 36 | struct xt_sctp_info *einfo = (struct xt_sctp_info *)m->data; |
| 37 | |
| 38 | memset(einfo, 0, sizeof(struct xt_sctp_info)); |
| 39 | |
| 40 | for (i = 0; i < XT_NUM_SCTP_FLAGS; i++) { |
| 41 | einfo->flag_info[i].chunktype = -1; |
| 42 | } |
| 43 | } |
| 44 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 45 | static void sctp_help(void) |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 46 | { |
| 47 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 48 | "sctp match options\n" |
Jan Engelhardt | 9672792 | 2008-08-13 14:42:41 +0200 | [diff] [blame] | 49 | "[!] --source-port port[:port] match source port(s)\n" |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 50 | " --sport ...\n" |
Jan Engelhardt | 9672792 | 2008-08-13 14:42:41 +0200 | [diff] [blame] | 51 | "[!] --destination-port port[:port] match destination port(s)\n" |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 52 | " --dport ...\n" |
Jan Engelhardt | 9672792 | 2008-08-13 14:42:41 +0200 | [diff] [blame] | 53 | "[!] --chunk-types (all|any|none) (chunktype[:flags])+ match if all, any or none of\n" |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 54 | " chunktypes are present\n" |
Shan Wei | 4a49850 | 2010-06-08 14:16:57 +0200 | [diff] [blame] | 55 | "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"); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 58 | static const struct option sctp_opts[] = { |
Jan Engelhardt | 32b8e61 | 2010-07-23 21:16:14 +0200 | [diff] [blame] | 59 | {.name = "source-port", .has_arg = true, .val = '1'}, |
| 60 | {.name = "sport", .has_arg = true, .val = '1'}, |
| 61 | {.name = "destination-port", .has_arg = true, .val = '2'}, |
| 62 | {.name = "dport", .has_arg = true, .val = '2'}, |
| 63 | {.name = "chunk-types", .has_arg = true, .val = '3'}, |
| 64 | XT_GETOPT_TABLEEND, |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | static void |
| 68 | parse_sctp_ports(const char *portstring, |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 69 | uint16_t *ports) |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 70 | { |
| 71 | char *buffer; |
| 72 | char *cp; |
| 73 | |
| 74 | buffer = strdup(portstring); |
| 75 | DEBUGP("%s\n", portstring); |
| 76 | if ((cp = strchr(buffer, ':')) == NULL) { |
Jan Engelhardt | aae6be9 | 2009-01-30 04:24:47 +0100 | [diff] [blame] | 77 | ports[0] = ports[1] = xtables_parse_port(buffer, "sctp"); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 78 | } |
| 79 | else { |
| 80 | *cp = '\0'; |
| 81 | cp++; |
| 82 | |
Jan Engelhardt | aae6be9 | 2009-01-30 04:24:47 +0100 | [diff] [blame] | 83 | ports[0] = buffer[0] ? xtables_parse_port(buffer, "sctp") : 0; |
| 84 | ports[1] = cp[0] ? xtables_parse_port(cp, "sctp") : 0xFFFF; |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 85 | |
| 86 | if (ports[0] > ports[1]) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 87 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 88 | "invalid portrange (min > max)"); |
| 89 | } |
| 90 | free(buffer); |
| 91 | } |
| 92 | |
| 93 | struct sctp_chunk_names { |
| 94 | const char *name; |
| 95 | unsigned int chunk_type; |
| 96 | const char *valid_flags; |
| 97 | }; |
| 98 | |
| 99 | /*'ALL' and 'NONE' will be treated specially. */ |
Jan Engelhardt | 0e2abed | 2007-10-04 16:25:58 +0000 | [diff] [blame] | 100 | static const struct sctp_chunk_names sctp_chunk_names[] |
Shan Wei | b9f458f | 2010-06-08 14:15:39 +0200 | [diff] [blame] | 101 | = { { .name = "DATA", .chunk_type = 0, .valid_flags = "----IUBE"}, |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 102 | { .name = "INIT", .chunk_type = 1, .valid_flags = "--------"}, |
| 103 | { .name = "INIT_ACK", .chunk_type = 2, .valid_flags = "--------"}, |
| 104 | { .name = "SACK", .chunk_type = 3, .valid_flags = "--------"}, |
| 105 | { .name = "HEARTBEAT", .chunk_type = 4, .valid_flags = "--------"}, |
| 106 | { .name = "HEARTBEAT_ACK", .chunk_type = 5, .valid_flags = "--------"}, |
| 107 | { .name = "ABORT", .chunk_type = 6, .valid_flags = "-------T"}, |
| 108 | { .name = "SHUTDOWN", .chunk_type = 7, .valid_flags = "--------"}, |
| 109 | { .name = "SHUTDOWN_ACK", .chunk_type = 8, .valid_flags = "--------"}, |
| 110 | { .name = "ERROR", .chunk_type = 9, .valid_flags = "--------"}, |
| 111 | { .name = "COOKIE_ECHO", .chunk_type = 10, .valid_flags = "--------"}, |
| 112 | { .name = "COOKIE_ACK", .chunk_type = 11, .valid_flags = "--------"}, |
| 113 | { .name = "ECN_ECNE", .chunk_type = 12, .valid_flags = "--------"}, |
| 114 | { .name = "ECN_CWR", .chunk_type = 13, .valid_flags = "--------"}, |
| 115 | { .name = "SHUTDOWN_COMPLETE", .chunk_type = 14, .valid_flags = "-------T"}, |
Naohiro Ooiwa | ecd7f5e | 2008-02-29 12:45:27 +0000 | [diff] [blame] | 116 | { .name = "ASCONF", .chunk_type = 193, .valid_flags = "--------"}, |
| 117 | { .name = "ASCONF_ACK", .chunk_type = 128, .valid_flags = "--------"}, |
Shan Wei | 4a49850 | 2010-06-08 14:16:57 +0200 | [diff] [blame] | 118 | { .name = "FORWARD_TSN", .chunk_type = 192, .valid_flags = "--------"}, |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | static void |
| 122 | save_chunk_flag_info(struct xt_sctp_flag_info *flag_info, |
| 123 | int *flag_count, |
| 124 | int chunktype, |
| 125 | int bit, |
| 126 | int set) |
| 127 | { |
| 128 | int i; |
| 129 | |
| 130 | for (i = 0; i < *flag_count; i++) { |
| 131 | if (flag_info[i].chunktype == chunktype) { |
| 132 | DEBUGP("Previous match found\n"); |
| 133 | flag_info[i].chunktype = chunktype; |
| 134 | flag_info[i].flag_mask |= (1 << bit); |
| 135 | if (set) { |
| 136 | flag_info[i].flag |= (1 << bit); |
| 137 | } |
| 138 | |
| 139 | return; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | if (*flag_count == XT_NUM_SCTP_FLAGS) { |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 144 | xtables_error (PARAMETER_PROBLEM, |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 145 | "Number of chunk types with flags exceeds currently allowed limit." |
Yasuyuki KOZAKAI | 19f2950 | 2007-07-24 07:02:26 +0000 | [diff] [blame] | 146 | "Increasing this limit involves changing IPT_NUM_SCTP_FLAGS and" |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 147 | "recompiling both the kernel space and user space modules\n"); |
| 148 | } |
| 149 | |
| 150 | flag_info[*flag_count].chunktype = chunktype; |
| 151 | flag_info[*flag_count].flag_mask |= (1 << bit); |
| 152 | if (set) { |
| 153 | flag_info[*flag_count].flag |= (1 << bit); |
| 154 | } |
| 155 | (*flag_count)++; |
| 156 | } |
| 157 | |
| 158 | static void |
| 159 | parse_sctp_chunk(struct xt_sctp_info *einfo, |
| 160 | const char *chunks) |
| 161 | { |
| 162 | char *ptr; |
| 163 | char *buffer; |
| 164 | unsigned int i, j; |
| 165 | int found = 0; |
| 166 | char *chunk_flags; |
| 167 | |
| 168 | buffer = strdup(chunks); |
| 169 | DEBUGP("Buffer: %s\n", buffer); |
| 170 | |
| 171 | SCTP_CHUNKMAP_RESET(einfo->chunkmap); |
| 172 | |
| 173 | if (!strcasecmp(buffer, "ALL")) { |
| 174 | SCTP_CHUNKMAP_SET_ALL(einfo->chunkmap); |
| 175 | goto out; |
| 176 | } |
| 177 | |
| 178 | if (!strcasecmp(buffer, "NONE")) { |
| 179 | SCTP_CHUNKMAP_RESET(einfo->chunkmap); |
| 180 | goto out; |
| 181 | } |
| 182 | |
| 183 | for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) { |
| 184 | found = 0; |
| 185 | DEBUGP("Next Chunk type %s\n", ptr); |
| 186 | |
| 187 | if ((chunk_flags = strchr(ptr, ':')) != NULL) { |
| 188 | *chunk_flags++ = 0; |
| 189 | } |
| 190 | |
Jan Engelhardt | 2c69b55 | 2009-04-30 19:32:02 +0200 | [diff] [blame] | 191 | for (i = 0; i < ARRAY_SIZE(sctp_chunk_names); ++i) |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 192 | if (strcasecmp(sctp_chunk_names[i].name, ptr) == 0) { |
| 193 | DEBUGP("Chunk num %d\n", sctp_chunk_names[i].chunk_type); |
| 194 | SCTP_CHUNKMAP_SET(einfo->chunkmap, |
| 195 | sctp_chunk_names[i].chunk_type); |
| 196 | found = 1; |
| 197 | break; |
| 198 | } |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 199 | if (!found) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 200 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 201 | "Unknown sctp chunk `%s'", ptr); |
| 202 | |
| 203 | if (chunk_flags) { |
| 204 | DEBUGP("Chunk flags %s\n", chunk_flags); |
| 205 | for (j = 0; j < strlen(chunk_flags); j++) { |
| 206 | char *p; |
| 207 | int bit; |
| 208 | |
| 209 | if ((p = strchr(sctp_chunk_names[i].valid_flags, |
| 210 | toupper(chunk_flags[j]))) != NULL) { |
| 211 | bit = p - sctp_chunk_names[i].valid_flags; |
| 212 | bit = 7 - bit; |
| 213 | |
| 214 | save_chunk_flag_info(einfo->flag_info, |
| 215 | &(einfo->flag_count), i, bit, |
| 216 | isupper(chunk_flags[j])); |
| 217 | } else { |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 218 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 219 | "Invalid flags for chunk type %d\n", i); |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | out: |
| 225 | free(buffer); |
| 226 | } |
| 227 | |
| 228 | static void |
| 229 | parse_sctp_chunks(struct xt_sctp_info *einfo, |
| 230 | const char *match_type, |
| 231 | const char *chunks) |
| 232 | { |
| 233 | DEBUGP("Match type: %s Chunks: %s\n", match_type, chunks); |
| 234 | if (!strcasecmp(match_type, "ANY")) { |
| 235 | einfo->chunk_match_type = SCTP_CHUNK_MATCH_ANY; |
| 236 | } else if (!strcasecmp(match_type, "ALL")) { |
| 237 | einfo->chunk_match_type = SCTP_CHUNK_MATCH_ALL; |
| 238 | } else if (!strcasecmp(match_type, "ONLY")) { |
| 239 | einfo->chunk_match_type = SCTP_CHUNK_MATCH_ONLY; |
| 240 | } else { |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 241 | xtables_error (PARAMETER_PROBLEM, |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 242 | "Match type has to be one of \"ALL\", \"ANY\" or \"ONLY\""); |
| 243 | } |
| 244 | |
| 245 | SCTP_CHUNKMAP_RESET(einfo->chunkmap); |
| 246 | parse_sctp_chunk(einfo, chunks); |
| 247 | } |
| 248 | |
| 249 | static int |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 250 | sctp_parse(int c, char **argv, int invert, unsigned int *flags, |
| 251 | const void *entry, struct xt_entry_match **match) |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 252 | { |
| 253 | struct xt_sctp_info *einfo |
| 254 | = (struct xt_sctp_info *)(*match)->data; |
| 255 | |
| 256 | switch (c) { |
| 257 | case '1': |
| 258 | if (*flags & XT_SCTP_SRC_PORTS) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 259 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 260 | "Only one `--source-port' allowed"); |
| 261 | einfo->flags |= XT_SCTP_SRC_PORTS; |
Jan Engelhardt | bf97128 | 2009-11-03 19:55:11 +0100 | [diff] [blame] | 262 | xtables_check_inverse(optarg, &invert, &optind, 0, argv); |
Jan Engelhardt | bbe8386 | 2009-10-24 00:45:33 +0200 | [diff] [blame] | 263 | parse_sctp_ports(optarg, einfo->spts); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 264 | if (invert) |
| 265 | einfo->invflags |= XT_SCTP_SRC_PORTS; |
| 266 | *flags |= XT_SCTP_SRC_PORTS; |
| 267 | break; |
| 268 | |
| 269 | case '2': |
| 270 | if (*flags & XT_SCTP_DEST_PORTS) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 271 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 272 | "Only one `--destination-port' allowed"); |
| 273 | einfo->flags |= XT_SCTP_DEST_PORTS; |
Jan Engelhardt | bf97128 | 2009-11-03 19:55:11 +0100 | [diff] [blame] | 274 | xtables_check_inverse(optarg, &invert, &optind, 0, argv); |
Jan Engelhardt | bbe8386 | 2009-10-24 00:45:33 +0200 | [diff] [blame] | 275 | parse_sctp_ports(optarg, einfo->dpts); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 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) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 283 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 284 | "Only one `--chunk-types' allowed"); |
Jan Engelhardt | bf97128 | 2009-11-03 19:55:11 +0100 | [diff] [blame] | 285 | xtables_check_inverse(optarg, &invert, &optind, 0, argv); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 286 | |
| 287 | if (!argv[optind] |
| 288 | || argv[optind][0] == '-' || argv[optind][0] == '!') |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 289 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 290 | "--chunk-types requires two args"); |
| 291 | |
| 292 | einfo->flags |= XT_SCTP_CHUNK_TYPES; |
Jan Engelhardt | bbe8386 | 2009-10-24 00:45:33 +0200 | [diff] [blame] | 293 | parse_sctp_chunks(einfo, optarg, argv[optind]); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 294 | if (invert) |
| 295 | einfo->invflags |= XT_SCTP_CHUNK_TYPES; |
| 296 | optind++; |
| 297 | *flags |= XT_SCTP_CHUNK_TYPES; |
| 298 | break; |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 299 | } |
| 300 | return 1; |
| 301 | } |
| 302 | |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 303 | static char * |
| 304 | port_to_service(int port) |
| 305 | { |
| 306 | struct servent *service; |
| 307 | |
| 308 | if ((service = getservbyport(htons(port), "sctp"))) |
| 309 | return service->s_name; |
| 310 | |
| 311 | return NULL; |
| 312 | } |
| 313 | |
| 314 | static void |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 315 | print_port(uint16_t port, int numeric) |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 316 | { |
| 317 | char *service; |
| 318 | |
| 319 | if (numeric || (service = port_to_service(port)) == NULL) |
| 320 | printf("%u", port); |
| 321 | else |
| 322 | printf("%s", service); |
| 323 | } |
| 324 | |
| 325 | static void |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 326 | print_ports(const char *name, uint16_t min, uint16_t max, |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 327 | int invert, int numeric) |
| 328 | { |
| 329 | const char *inv = invert ? "!" : ""; |
| 330 | |
| 331 | if (min != 0 || max != 0xFFFF || invert) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame^] | 332 | printf(" %s", name); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 333 | if (min == max) { |
| 334 | printf(":%s", inv); |
| 335 | print_port(min, numeric); |
| 336 | } else { |
| 337 | printf("s:%s", inv); |
| 338 | print_port(min, numeric); |
| 339 | printf(":"); |
| 340 | print_port(max, numeric); |
| 341 | } |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
| 345 | static void |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 346 | print_chunk_flags(uint32_t chunknum, uint8_t chunk_flags, uint8_t chunk_flags_mask) |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 347 | { |
| 348 | int i; |
| 349 | |
| 350 | DEBUGP("type: %d\tflags: %x\tflag mask: %x\n", chunknum, chunk_flags, |
| 351 | chunk_flags_mask); |
| 352 | |
| 353 | if (chunk_flags_mask) { |
| 354 | printf(":"); |
| 355 | } |
| 356 | |
| 357 | for (i = 7; i >= 0; i--) { |
| 358 | if (chunk_flags_mask & (1 << i)) { |
| 359 | if (chunk_flags & (1 << i)) { |
| 360 | printf("%c", sctp_chunk_names[chunknum].valid_flags[7-i]); |
| 361 | } else { |
| 362 | printf("%c", tolower(sctp_chunk_names[chunknum].valid_flags[7-i])); |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | static void |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 369 | print_chunk(uint32_t chunknum, int numeric) |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 370 | { |
| 371 | if (numeric) { |
| 372 | printf("0x%04X", chunknum); |
| 373 | } |
| 374 | else { |
| 375 | int i; |
| 376 | |
Jan Engelhardt | 2c69b55 | 2009-04-30 19:32:02 +0200 | [diff] [blame] | 377 | for (i = 0; i < ARRAY_SIZE(sctp_chunk_names); ++i) |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 378 | if (sctp_chunk_names[i].chunk_type == chunknum) |
| 379 | printf("%s", sctp_chunk_names[chunknum].name); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
| 383 | static void |
Li Zefan | 1f25b40 | 2007-10-18 09:12:49 +0000 | [diff] [blame] | 384 | print_chunks(const struct xt_sctp_info *einfo, int numeric) |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 385 | { |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 386 | uint32_t chunk_match_type = einfo->chunk_match_type; |
Li Zefan | 1f25b40 | 2007-10-18 09:12:49 +0000 | [diff] [blame] | 387 | const struct xt_sctp_flag_info *flag_info = einfo->flag_info; |
| 388 | int flag_count = einfo->flag_count; |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 389 | int i, j; |
| 390 | int flag; |
| 391 | |
| 392 | switch (chunk_match_type) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame^] | 393 | case SCTP_CHUNK_MATCH_ANY: printf(" any"); break; |
| 394 | case SCTP_CHUNK_MATCH_ALL: printf(" all"); break; |
| 395 | case SCTP_CHUNK_MATCH_ONLY: printf(" only"); break; |
Jan Engelhardt | da580fe | 2011-01-08 03:16:14 +0100 | [diff] [blame] | 396 | default: printf("Never reach here\n"); break; |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Li Zefan | 1f25b40 | 2007-10-18 09:12:49 +0000 | [diff] [blame] | 399 | if (SCTP_CHUNKMAP_IS_CLEAR(einfo->chunkmap)) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame^] | 400 | printf(" NONE"); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 401 | goto out; |
| 402 | } |
| 403 | |
Li Zefan | 1f25b40 | 2007-10-18 09:12:49 +0000 | [diff] [blame] | 404 | if (SCTP_CHUNKMAP_IS_ALL_SET(einfo->chunkmap)) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame^] | 405 | printf(" ALL"); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 406 | goto out; |
| 407 | } |
| 408 | |
| 409 | flag = 0; |
| 410 | for (i = 0; i < 256; i++) { |
Li Zefan | 1f25b40 | 2007-10-18 09:12:49 +0000 | [diff] [blame] | 411 | if (SCTP_CHUNKMAP_IS_SET(einfo->chunkmap, i)) { |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 412 | if (flag) |
| 413 | printf(","); |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame^] | 414 | else |
| 415 | putchar(' '); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 416 | flag = 1; |
| 417 | print_chunk(i, numeric); |
| 418 | for (j = 0; j < flag_count; j++) { |
| 419 | if (flag_info[j].chunktype == i) { |
| 420 | print_chunk_flags(i, flag_info[j].flag, |
| 421 | flag_info[j].flag_mask); |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | } |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 426 | out: |
| 427 | return; |
| 428 | } |
| 429 | |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 430 | static void |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 431 | sctp_print(const void *ip, const struct xt_entry_match *match, int numeric) |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 432 | { |
| 433 | const struct xt_sctp_info *einfo = |
| 434 | (const struct xt_sctp_info *)match->data; |
| 435 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame^] | 436 | printf(" sctp"); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 437 | |
| 438 | if (einfo->flags & XT_SCTP_SRC_PORTS) { |
| 439 | print_ports("spt", einfo->spts[0], einfo->spts[1], |
| 440 | einfo->invflags & XT_SCTP_SRC_PORTS, |
| 441 | numeric); |
| 442 | } |
| 443 | |
| 444 | if (einfo->flags & XT_SCTP_DEST_PORTS) { |
| 445 | print_ports("dpt", einfo->dpts[0], einfo->dpts[1], |
| 446 | einfo->invflags & XT_SCTP_DEST_PORTS, |
| 447 | numeric); |
| 448 | } |
| 449 | |
| 450 | if (einfo->flags & XT_SCTP_CHUNK_TYPES) { |
| 451 | /* FIXME: print_chunks() is used in save() where the printing of '!' |
| 452 | s taken care of, so we need to do that here as well */ |
| 453 | if (einfo->invflags & XT_SCTP_CHUNK_TYPES) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame^] | 454 | printf(" !"); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 455 | } |
Li Zefan | 1f25b40 | 2007-10-18 09:12:49 +0000 | [diff] [blame] | 456 | print_chunks(einfo, numeric); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 460 | static void sctp_save(const void *ip, const struct xt_entry_match *match) |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 461 | { |
| 462 | const struct xt_sctp_info *einfo = |
| 463 | (const struct xt_sctp_info *)match->data; |
| 464 | |
| 465 | if (einfo->flags & XT_SCTP_SRC_PORTS) { |
| 466 | if (einfo->invflags & XT_SCTP_SRC_PORTS) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame^] | 467 | printf(" !"); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 468 | if (einfo->spts[0] != einfo->spts[1]) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame^] | 469 | printf(" --sport %u:%u", |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 470 | einfo->spts[0], einfo->spts[1]); |
| 471 | else |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame^] | 472 | printf(" --sport %u", einfo->spts[0]); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | if (einfo->flags & XT_SCTP_DEST_PORTS) { |
| 476 | if (einfo->invflags & XT_SCTP_DEST_PORTS) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame^] | 477 | printf(" !"); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 478 | if (einfo->dpts[0] != einfo->dpts[1]) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame^] | 479 | printf(" --dport %u:%u", |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 480 | einfo->dpts[0], einfo->dpts[1]); |
| 481 | else |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame^] | 482 | printf(" --dport %u", einfo->dpts[0]); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | if (einfo->flags & XT_SCTP_CHUNK_TYPES) { |
| 486 | if (einfo->invflags & XT_SCTP_CHUNK_TYPES) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame^] | 487 | printf(" !"); |
| 488 | printf(" --chunk-types"); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 489 | |
Li Zefan | 1f25b40 | 2007-10-18 09:12:49 +0000 | [diff] [blame] | 490 | print_chunks(einfo, 0); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 494 | static struct xtables_match sctp_match = { |
Yasuyuki KOZAKAI | 19f2950 | 2007-07-24 07:02:26 +0000 | [diff] [blame] | 495 | .name = "sctp", |
Jan Engelhardt | c5e8573 | 2009-06-12 20:55:44 +0200 | [diff] [blame] | 496 | .family = NFPROTO_UNSPEC, |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 497 | .version = XTABLES_VERSION, |
Yasuyuki KOZAKAI | 19f2950 | 2007-07-24 07:02:26 +0000 | [diff] [blame] | 498 | .size = XT_ALIGN(sizeof(struct xt_sctp_info)), |
| 499 | .userspacesize = XT_ALIGN(sizeof(struct xt_sctp_info)), |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 500 | .help = sctp_help, |
| 501 | .init = sctp_init, |
| 502 | .parse = sctp_parse, |
| 503 | .print = sctp_print, |
| 504 | .save = sctp_save, |
| 505 | .extra_opts = sctp_opts, |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 506 | }; |
| 507 | |
| 508 | void _init(void) |
| 509 | { |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 510 | xtables_register_match(&sctp_match); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 511 | } |