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 | |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 38 | for (i = 0; i < XT_NUM_SCTP_FLAGS; i++) { |
| 39 | einfo->flag_info[i].chunktype = -1; |
| 40 | } |
| 41 | } |
| 42 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 43 | static void sctp_help(void) |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 44 | { |
| 45 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 46 | "sctp match options\n" |
Jan Engelhardt | 9672792 | 2008-08-13 14:42:41 +0200 | [diff] [blame] | 47 | "[!] --source-port port[:port] match source port(s)\n" |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 48 | " --sport ...\n" |
Jan Engelhardt | 9672792 | 2008-08-13 14:42:41 +0200 | [diff] [blame] | 49 | "[!] --destination-port port[:port] match destination port(s)\n" |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 50 | " --dport ...\n" |
Jan Engelhardt | 9672792 | 2008-08-13 14:42:41 +0200 | [diff] [blame] | 51 | "[!] --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] | 52 | " chunktypes are present\n" |
Shan Wei | 4a49850 | 2010-06-08 14:16:57 +0200 | [diff] [blame] | 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"); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 56 | static const struct option sctp_opts[] = { |
Jan Engelhardt | 32b8e61 | 2010-07-23 21:16:14 +0200 | [diff] [blame] | 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, |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | static void |
| 66 | parse_sctp_ports(const char *portstring, |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 67 | uint16_t *ports) |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 68 | { |
| 69 | char *buffer; |
| 70 | char *cp; |
| 71 | |
| 72 | buffer = strdup(portstring); |
| 73 | DEBUGP("%s\n", portstring); |
| 74 | if ((cp = strchr(buffer, ':')) == NULL) { |
Jan Engelhardt | aae6be9 | 2009-01-30 04:24:47 +0100 | [diff] [blame] | 75 | ports[0] = ports[1] = xtables_parse_port(buffer, "sctp"); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 76 | } |
| 77 | else { |
| 78 | *cp = '\0'; |
| 79 | cp++; |
| 80 | |
Jan Engelhardt | aae6be9 | 2009-01-30 04:24:47 +0100 | [diff] [blame] | 81 | ports[0] = buffer[0] ? xtables_parse_port(buffer, "sctp") : 0; |
| 82 | ports[1] = cp[0] ? xtables_parse_port(cp, "sctp") : 0xFFFF; |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 83 | |
| 84 | if (ports[0] > ports[1]) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 85 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 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 | }; |
| 96 | |
| 97 | /*'ALL' and 'NONE' will be treated specially. */ |
Jan Engelhardt | 0e2abed | 2007-10-04 16:25:58 +0000 | [diff] [blame] | 98 | static const struct sctp_chunk_names sctp_chunk_names[] |
Shan Wei | b9f458f | 2010-06-08 14:15:39 +0200 | [diff] [blame] | 99 | = { { .name = "DATA", .chunk_type = 0, .valid_flags = "----IUBE"}, |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 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"}, |
Naohiro Ooiwa | ecd7f5e | 2008-02-29 12:45:27 +0000 | [diff] [blame] | 114 | { .name = "ASCONF", .chunk_type = 193, .valid_flags = "--------"}, |
| 115 | { .name = "ASCONF_ACK", .chunk_type = 128, .valid_flags = "--------"}, |
Shan Wei | 4a49850 | 2010-06-08 14:16:57 +0200 | [diff] [blame] | 116 | { .name = "FORWARD_TSN", .chunk_type = 192, .valid_flags = "--------"}, |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | static void |
| 120 | save_chunk_flag_info(struct xt_sctp_flag_info *flag_info, |
| 121 | int *flag_count, |
| 122 | int chunktype, |
| 123 | int bit, |
| 124 | int set) |
| 125 | { |
| 126 | int i; |
| 127 | |
| 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); |
| 133 | if (set) { |
| 134 | flag_info[i].flag |= (1 << bit); |
| 135 | } |
| 136 | |
| 137 | return; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | if (*flag_count == XT_NUM_SCTP_FLAGS) { |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 142 | xtables_error (PARAMETER_PROBLEM, |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 143 | "Number of chunk types with flags exceeds currently allowed limit." |
Yasuyuki KOZAKAI | 19f2950 | 2007-07-24 07:02:26 +0000 | [diff] [blame] | 144 | "Increasing this limit involves changing IPT_NUM_SCTP_FLAGS and" |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 145 | "recompiling both the kernel space and user space modules\n"); |
| 146 | } |
| 147 | |
| 148 | flag_info[*flag_count].chunktype = chunktype; |
| 149 | flag_info[*flag_count].flag_mask |= (1 << bit); |
| 150 | if (set) { |
| 151 | flag_info[*flag_count].flag |= (1 << bit); |
| 152 | } |
| 153 | (*flag_count)++; |
| 154 | } |
| 155 | |
| 156 | static void |
| 157 | parse_sctp_chunk(struct xt_sctp_info *einfo, |
| 158 | const char *chunks) |
| 159 | { |
| 160 | char *ptr; |
| 161 | char *buffer; |
| 162 | unsigned int i, j; |
| 163 | int found = 0; |
| 164 | char *chunk_flags; |
| 165 | |
| 166 | buffer = strdup(chunks); |
| 167 | DEBUGP("Buffer: %s\n", buffer); |
| 168 | |
| 169 | SCTP_CHUNKMAP_RESET(einfo->chunkmap); |
| 170 | |
| 171 | if (!strcasecmp(buffer, "ALL")) { |
| 172 | SCTP_CHUNKMAP_SET_ALL(einfo->chunkmap); |
| 173 | goto out; |
| 174 | } |
| 175 | |
| 176 | if (!strcasecmp(buffer, "NONE")) { |
| 177 | SCTP_CHUNKMAP_RESET(einfo->chunkmap); |
| 178 | goto out; |
| 179 | } |
| 180 | |
| 181 | for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) { |
| 182 | found = 0; |
| 183 | DEBUGP("Next Chunk type %s\n", ptr); |
| 184 | |
| 185 | if ((chunk_flags = strchr(ptr, ':')) != NULL) { |
| 186 | *chunk_flags++ = 0; |
| 187 | } |
| 188 | |
Jan Engelhardt | 2c69b55 | 2009-04-30 19:32:02 +0200 | [diff] [blame] | 189 | for (i = 0; i < ARRAY_SIZE(sctp_chunk_names); ++i) |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 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); |
| 194 | found = 1; |
| 195 | break; |
| 196 | } |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 197 | if (!found) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 198 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 199 | "Unknown sctp chunk `%s'", ptr); |
| 200 | |
| 201 | if (chunk_flags) { |
| 202 | DEBUGP("Chunk flags %s\n", chunk_flags); |
| 203 | for (j = 0; j < strlen(chunk_flags); j++) { |
| 204 | char *p; |
| 205 | int bit; |
| 206 | |
| 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; |
| 210 | bit = 7 - bit; |
| 211 | |
| 212 | save_chunk_flag_info(einfo->flag_info, |
| 213 | &(einfo->flag_count), i, bit, |
| 214 | isupper(chunk_flags[j])); |
| 215 | } else { |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 216 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 217 | "Invalid flags for chunk type %d\n", i); |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | out: |
| 223 | free(buffer); |
| 224 | } |
| 225 | |
| 226 | static void |
| 227 | parse_sctp_chunks(struct xt_sctp_info *einfo, |
| 228 | const char *match_type, |
| 229 | const char *chunks) |
| 230 | { |
| 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; |
| 238 | } else { |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 239 | xtables_error (PARAMETER_PROBLEM, |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 240 | "Match type has to be one of \"ALL\", \"ANY\" or \"ONLY\""); |
| 241 | } |
| 242 | |
| 243 | SCTP_CHUNKMAP_RESET(einfo->chunkmap); |
| 244 | parse_sctp_chunk(einfo, chunks); |
| 245 | } |
| 246 | |
| 247 | static int |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 248 | sctp_parse(int c, char **argv, int invert, unsigned int *flags, |
| 249 | const void *entry, struct xt_entry_match **match) |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 250 | { |
| 251 | struct xt_sctp_info *einfo |
| 252 | = (struct xt_sctp_info *)(*match)->data; |
| 253 | |
| 254 | switch (c) { |
| 255 | case '1': |
| 256 | if (*flags & XT_SCTP_SRC_PORTS) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 257 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 258 | "Only one `--source-port' allowed"); |
| 259 | einfo->flags |= XT_SCTP_SRC_PORTS; |
Jan Engelhardt | bf97128 | 2009-11-03 19:55:11 +0100 | [diff] [blame] | 260 | xtables_check_inverse(optarg, &invert, &optind, 0, argv); |
Jan Engelhardt | bbe8386 | 2009-10-24 00:45:33 +0200 | [diff] [blame] | 261 | parse_sctp_ports(optarg, einfo->spts); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 262 | if (invert) |
| 263 | einfo->invflags |= XT_SCTP_SRC_PORTS; |
| 264 | *flags |= XT_SCTP_SRC_PORTS; |
| 265 | break; |
| 266 | |
| 267 | case '2': |
| 268 | if (*flags & XT_SCTP_DEST_PORTS) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 269 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 270 | "Only one `--destination-port' allowed"); |
| 271 | einfo->flags |= XT_SCTP_DEST_PORTS; |
Jan Engelhardt | bf97128 | 2009-11-03 19:55:11 +0100 | [diff] [blame] | 272 | xtables_check_inverse(optarg, &invert, &optind, 0, argv); |
Jan Engelhardt | bbe8386 | 2009-10-24 00:45:33 +0200 | [diff] [blame] | 273 | parse_sctp_ports(optarg, einfo->dpts); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 274 | if (invert) |
| 275 | einfo->invflags |= XT_SCTP_DEST_PORTS; |
| 276 | *flags |= XT_SCTP_DEST_PORTS; |
| 277 | break; |
| 278 | |
| 279 | case '3': |
| 280 | if (*flags & XT_SCTP_CHUNK_TYPES) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 281 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 282 | "Only one `--chunk-types' allowed"); |
Jan Engelhardt | bf97128 | 2009-11-03 19:55:11 +0100 | [diff] [blame] | 283 | xtables_check_inverse(optarg, &invert, &optind, 0, argv); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 284 | |
| 285 | if (!argv[optind] |
| 286 | || argv[optind][0] == '-' || argv[optind][0] == '!') |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 287 | xtables_error(PARAMETER_PROBLEM, |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 288 | "--chunk-types requires two args"); |
| 289 | |
| 290 | einfo->flags |= XT_SCTP_CHUNK_TYPES; |
Jan Engelhardt | bbe8386 | 2009-10-24 00:45:33 +0200 | [diff] [blame] | 291 | parse_sctp_chunks(einfo, optarg, argv[optind]); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 292 | if (invert) |
| 293 | einfo->invflags |= XT_SCTP_CHUNK_TYPES; |
| 294 | optind++; |
| 295 | *flags |= XT_SCTP_CHUNK_TYPES; |
| 296 | break; |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 297 | } |
| 298 | return 1; |
| 299 | } |
| 300 | |
Jan Engelhardt | dd6e4b9 | 2011-05-07 00:05:24 +0200 | [diff] [blame] | 301 | static const char * |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 302 | port_to_service(int port) |
| 303 | { |
Jan Engelhardt | dd6e4b9 | 2011-05-07 00:05:24 +0200 | [diff] [blame] | 304 | const struct servent *service; |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 305 | |
| 306 | if ((service = getservbyport(htons(port), "sctp"))) |
| 307 | return service->s_name; |
| 308 | |
| 309 | return NULL; |
| 310 | } |
| 311 | |
| 312 | static void |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 313 | print_port(uint16_t port, int numeric) |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 314 | { |
Jan Engelhardt | dd6e4b9 | 2011-05-07 00:05:24 +0200 | [diff] [blame] | 315 | const char *service; |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 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 |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 324 | print_ports(const char *name, uint16_t min, uint16_t max, |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 325 | int invert, int numeric) |
| 326 | { |
| 327 | const char *inv = invert ? "!" : ""; |
| 328 | |
| 329 | if (min != 0 || max != 0xFFFF || invert) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 330 | printf(" %s", name); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 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 | } |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
| 343 | static void |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 344 | 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] | 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 |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 367 | print_chunk(uint32_t chunknum, int numeric) |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 368 | { |
| 369 | if (numeric) { |
| 370 | printf("0x%04X", chunknum); |
| 371 | } |
| 372 | else { |
| 373 | int i; |
| 374 | |
Jan Engelhardt | 2c69b55 | 2009-04-30 19:32:02 +0200 | [diff] [blame] | 375 | for (i = 0; i < ARRAY_SIZE(sctp_chunk_names); ++i) |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 376 | if (sctp_chunk_names[i].chunk_type == chunknum) |
| 377 | printf("%s", sctp_chunk_names[chunknum].name); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
| 381 | static void |
Li Zefan | 1f25b40 | 2007-10-18 09:12:49 +0000 | [diff] [blame] | 382 | print_chunks(const struct xt_sctp_info *einfo, int numeric) |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 383 | { |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 384 | uint32_t chunk_match_type = einfo->chunk_match_type; |
Li Zefan | 1f25b40 | 2007-10-18 09:12:49 +0000 | [diff] [blame] | 385 | const struct xt_sctp_flag_info *flag_info = einfo->flag_info; |
| 386 | int flag_count = einfo->flag_count; |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 387 | int i, j; |
| 388 | int flag; |
| 389 | |
| 390 | switch (chunk_match_type) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 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; |
Jan Engelhardt | da580fe | 2011-01-08 03:16:14 +0100 | [diff] [blame] | 394 | default: printf("Never reach here\n"); break; |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Li Zefan | 1f25b40 | 2007-10-18 09:12:49 +0000 | [diff] [blame] | 397 | if (SCTP_CHUNKMAP_IS_CLEAR(einfo->chunkmap)) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 398 | printf(" NONE"); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 399 | goto out; |
| 400 | } |
| 401 | |
Li Zefan | 1f25b40 | 2007-10-18 09:12:49 +0000 | [diff] [blame] | 402 | if (SCTP_CHUNKMAP_IS_ALL_SET(einfo->chunkmap)) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 403 | printf(" ALL"); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 404 | goto out; |
| 405 | } |
| 406 | |
| 407 | flag = 0; |
| 408 | for (i = 0; i < 256; i++) { |
Li Zefan | 1f25b40 | 2007-10-18 09:12:49 +0000 | [diff] [blame] | 409 | if (SCTP_CHUNKMAP_IS_SET(einfo->chunkmap, i)) { |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 410 | if (flag) |
| 411 | printf(","); |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 412 | else |
| 413 | putchar(' '); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 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 | } |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 424 | out: |
| 425 | return; |
| 426 | } |
| 427 | |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 428 | static void |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 429 | 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] | 430 | { |
| 431 | const struct xt_sctp_info *einfo = |
| 432 | (const struct xt_sctp_info *)match->data; |
| 433 | |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 434 | printf(" sctp"); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 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) { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 452 | printf(" !"); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 453 | } |
Li Zefan | 1f25b40 | 2007-10-18 09:12:49 +0000 | [diff] [blame] | 454 | print_chunks(einfo, numeric); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 458 | 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] | 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) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 465 | printf(" !"); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 466 | if (einfo->spts[0] != einfo->spts[1]) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 467 | printf(" --sport %u:%u", |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 468 | einfo->spts[0], einfo->spts[1]); |
| 469 | else |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 470 | printf(" --sport %u", einfo->spts[0]); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | if (einfo->flags & XT_SCTP_DEST_PORTS) { |
| 474 | if (einfo->invflags & XT_SCTP_DEST_PORTS) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 475 | printf(" !"); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 476 | if (einfo->dpts[0] != einfo->dpts[1]) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 477 | printf(" --dport %u:%u", |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 478 | einfo->dpts[0], einfo->dpts[1]); |
| 479 | else |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 480 | printf(" --dport %u", einfo->dpts[0]); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | if (einfo->flags & XT_SCTP_CHUNK_TYPES) { |
| 484 | if (einfo->invflags & XT_SCTP_CHUNK_TYPES) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 485 | printf(" !"); |
| 486 | printf(" --chunk-types"); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 487 | |
Li Zefan | 1f25b40 | 2007-10-18 09:12:49 +0000 | [diff] [blame] | 488 | print_chunks(einfo, 0); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 492 | static struct xtables_match sctp_match = { |
Yasuyuki KOZAKAI | 19f2950 | 2007-07-24 07:02:26 +0000 | [diff] [blame] | 493 | .name = "sctp", |
Jan Engelhardt | c5e8573 | 2009-06-12 20:55:44 +0200 | [diff] [blame] | 494 | .family = NFPROTO_UNSPEC, |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 495 | .version = XTABLES_VERSION, |
Yasuyuki KOZAKAI | 19f2950 | 2007-07-24 07:02:26 +0000 | [diff] [blame] | 496 | .size = XT_ALIGN(sizeof(struct xt_sctp_info)), |
| 497 | .userspacesize = XT_ALIGN(sizeof(struct xt_sctp_info)), |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 498 | .help = sctp_help, |
| 499 | .init = sctp_init, |
| 500 | .parse = sctp_parse, |
| 501 | .print = sctp_print, |
| 502 | .save = sctp_save, |
| 503 | .extra_opts = sctp_opts, |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 504 | }; |
| 505 | |
| 506 | void _init(void) |
| 507 | { |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 508 | xtables_register_match(&sctp_match); |
Patrick McHardy | 3810013 | 2006-11-13 19:38:44 +0000 | [diff] [blame] | 509 | } |