Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +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 | */ |
| 10 | #include <stdio.h> |
| 11 | #include <string.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <getopt.h> |
| 14 | #include <netdb.h> |
Kiran Kumar | 5492402 | 2004-03-02 18:38:27 +0000 | [diff] [blame] | 15 | #include <ctype.h> |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 16 | |
| 17 | #include <iptables.h> |
| 18 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 19 | #include <linux/netfilter_ipv4/ipt_sctp.h> |
| 20 | |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 21 | #if 0 |
| 22 | #define DEBUGP(format, first...) printf(format, ##first) |
| 23 | #define static |
| 24 | #else |
| 25 | #define DEBUGP(format, fist...) |
| 26 | #endif |
| 27 | |
Kiran Kumar | 5492402 | 2004-03-02 18:38:27 +0000 | [diff] [blame] | 28 | static void |
| 29 | print_chunk(u_int32_t chunknum, int numeric); |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 30 | |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 31 | /* Initialize the match. */ |
| 32 | static void |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 33 | init(struct ipt_entry_match *m, |
| 34 | unsigned int *nfcache) |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 35 | { |
Kiran Kumar | 5492402 | 2004-03-02 18:38:27 +0000 | [diff] [blame] | 36 | int i; |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 37 | struct ipt_sctp_info *einfo = (struct ipt_sctp_info *)m->data; |
| 38 | |
Kiran Kumar | 5492402 | 2004-03-02 18:38:27 +0000 | [diff] [blame] | 39 | memset(einfo, 0, sizeof(struct ipt_sctp_info)); |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 40 | |
Kiran Kumar | 5492402 | 2004-03-02 18:38:27 +0000 | [diff] [blame] | 41 | for (i = 0; i < IPT_NUM_SCTP_FLAGS; i++) { |
| 42 | einfo->flag_info[i].chunktype = -1; |
| 43 | } |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 46 | static void help(void) |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 47 | { |
| 48 | printf( |
| 49 | "SCTP match v%s options\n" |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 50 | " --source-port [!] port[:port] match source port(s)\n" |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 51 | " --sport ...\n" |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 52 | " --destination-port [!] port[:port] match destination port(s)\n" |
| 53 | " --dport ...\n" |
| 54 | " --chunk-types [!] (all|any|none) (chunktype[:flags])+ match if all, any or none of\n" |
| 55 | " chunktypes are present\n" |
| 56 | "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", |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 57 | IPTABLES_VERSION); |
| 58 | } |
| 59 | |
| 60 | static struct option opts[] = { |
| 61 | { .name = "source-port", .has_arg = 1, .flag = 0, .val = '1' }, |
| 62 | { .name = "sport", .has_arg = 1, .flag = 0, .val = '1' }, |
| 63 | { .name = "destination-port", .has_arg = 1, .flag = 0, .val = '2' }, |
| 64 | { .name = "dport", .has_arg = 1, .flag = 0, .val = '2' }, |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 65 | { .name = "chunk-types", .has_arg = 1, .flag = 0, .val = '3' }, |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 66 | { .name = 0 } |
| 67 | }; |
| 68 | |
| 69 | static int |
| 70 | service_to_port(const char *name) |
| 71 | { |
| 72 | struct servent *service; |
| 73 | |
| 74 | if ((service = getservbyname(name, "sctp")) != NULL) |
| 75 | return ntohs((unsigned short) service->s_port); |
| 76 | |
| 77 | return -1; |
| 78 | } |
| 79 | |
| 80 | static u_int16_t |
| 81 | parse_sctp_port(const char *port) |
| 82 | { |
| 83 | unsigned int portnum; |
| 84 | |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 85 | DEBUGP("%s\n", port); |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 86 | if (string_to_number(port, 0, 65535, &portnum) != -1 || |
| 87 | (portnum = service_to_port(port)) != -1) |
| 88 | return (u_int16_t)portnum; |
| 89 | |
| 90 | exit_error(PARAMETER_PROBLEM, |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 91 | "invalid SCTP port/service `%s' specified", port); |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 94 | static void |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 95 | parse_sctp_ports(const char *portstring, |
| 96 | u_int16_t *ports) |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 97 | { |
| 98 | char *buffer; |
| 99 | char *cp; |
| 100 | |
| 101 | buffer = strdup(portstring); |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 102 | DEBUGP("%s\n", portstring); |
| 103 | if ((cp = strchr(buffer, ':')) == NULL) { |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 104 | ports[0] = ports[1] = parse_sctp_port(buffer); |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 105 | } |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 106 | else { |
| 107 | *cp = '\0'; |
| 108 | cp++; |
| 109 | |
| 110 | ports[0] = buffer[0] ? parse_sctp_port(buffer) : 0; |
| 111 | ports[1] = cp[0] ? parse_sctp_port(cp) : 0xFFFF; |
| 112 | |
| 113 | if (ports[0] > ports[1]) |
| 114 | exit_error(PARAMETER_PROBLEM, |
| 115 | "invalid portrange (min > max)"); |
| 116 | } |
| 117 | free(buffer); |
| 118 | } |
| 119 | |
| 120 | struct sctp_chunk_names { |
| 121 | const char *name; |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 122 | unsigned int chunk_type; |
Kiran Kumar | 5492402 | 2004-03-02 18:38:27 +0000 | [diff] [blame] | 123 | const char *valid_flags; |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 126 | /*'ALL' and 'NONE' will be treated specially. */ |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 127 | static struct sctp_chunk_names sctp_chunk_names[] |
Kiran Kumar | 5492402 | 2004-03-02 18:38:27 +0000 | [diff] [blame] | 128 | = { { .name = "DATA", .chunk_type = 0, .valid_flags = "-----UBE"}, |
| 129 | { .name = "INIT", .chunk_type = 1, .valid_flags = "--------"}, |
| 130 | { .name = "INIT_ACK", .chunk_type = 2, .valid_flags = "--------"}, |
| 131 | { .name = "SACK", .chunk_type = 3, .valid_flags = "--------"}, |
| 132 | { .name = "HEARTBEAT", .chunk_type = 4, .valid_flags = "--------"}, |
| 133 | { .name = "HEARTBEAT_ACK", .chunk_type = 5, .valid_flags = "--------"}, |
| 134 | { .name = "ABORT", .chunk_type = 6, .valid_flags = "-------T"}, |
| 135 | { .name = "SHUTDOWN", .chunk_type = 7, .valid_flags = "--------"}, |
| 136 | { .name = "SHUTDOWN_ACK", .chunk_type = 8, .valid_flags = "--------"}, |
| 137 | { .name = "ERROR", .chunk_type = 9, .valid_flags = "--------"}, |
| 138 | { .name = "COOKIE_ECHO", .chunk_type = 10, .valid_flags = "--------"}, |
| 139 | { .name = "COOKIE_ACK", .chunk_type = 11, .valid_flags = "--------"}, |
| 140 | { .name = "ECN_ECNE", .chunk_type = 12, .valid_flags = "--------"}, |
| 141 | { .name = "ECN_CWR", .chunk_type = 13, .valid_flags = "--------"}, |
| 142 | { .name = "SHUTDOWN_COMPLETE", .chunk_type = 14, .valid_flags = "-------T"}, |
| 143 | { .name = "ASCONF", .chunk_type = 31, .valid_flags = "--------"}, |
| 144 | { .name = "ASCONF_ACK", .chunk_type = 30, .valid_flags = "--------"}, |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 147 | static void |
Kiran Kumar | 5492402 | 2004-03-02 18:38:27 +0000 | [diff] [blame] | 148 | save_chunk_flag_info(struct ipt_sctp_flag_info *flag_info, |
| 149 | int *flag_count, |
| 150 | int chunktype, |
| 151 | int bit, |
| 152 | int set) |
| 153 | { |
| 154 | int i; |
| 155 | |
| 156 | for (i = 0; i < *flag_count; i++) { |
| 157 | if (flag_info[i].chunktype == chunktype) { |
| 158 | DEBUGP("Previous match found\n"); |
| 159 | flag_info[i].chunktype = chunktype; |
| 160 | flag_info[i].flag_mask |= (1 << bit); |
| 161 | if (set) { |
| 162 | flag_info[i].flag |= (1 << bit); |
| 163 | } |
| 164 | |
| 165 | return; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | if (*flag_count == IPT_NUM_SCTP_FLAGS) { |
| 170 | exit_error (PARAMETER_PROBLEM, |
| 171 | "Number of chunk types with flags exceeds currently allowed limit." |
| 172 | "Increasing this limit involves changing IPT_NUM_SCTP_FLAGS and" |
| 173 | "recompiling both the kernel space and user space modules\n"); |
| 174 | } |
| 175 | |
| 176 | flag_info[*flag_count].chunktype = chunktype; |
| 177 | flag_info[*flag_count].flag_mask |= (1 << bit); |
| 178 | if (set) { |
| 179 | flag_info[*flag_count].flag |= (1 << bit); |
| 180 | } |
| 181 | (*flag_count)++; |
| 182 | } |
| 183 | |
| 184 | static void |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 185 | parse_sctp_chunk(struct ipt_sctp_info *einfo, |
| 186 | const char *chunks) |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 187 | { |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 188 | char *ptr; |
| 189 | char *buffer; |
Kiran Kumar | 5492402 | 2004-03-02 18:38:27 +0000 | [diff] [blame] | 190 | unsigned int i, j; |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 191 | int found = 0; |
Kiran Kumar | 5492402 | 2004-03-02 18:38:27 +0000 | [diff] [blame] | 192 | char *chunk_flags; |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 193 | |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 194 | buffer = strdup(chunks); |
| 195 | DEBUGP("Buffer: %s\n", buffer); |
| 196 | |
| 197 | SCTP_CHUNKMAP_RESET(einfo->chunkmap); |
| 198 | |
| 199 | if (!strcasecmp(buffer, "ALL")) { |
| 200 | SCTP_CHUNKMAP_SET_ALL(einfo->chunkmap); |
| 201 | goto out; |
| 202 | } |
| 203 | |
| 204 | if (!strcasecmp(buffer, "NONE")) { |
| 205 | SCTP_CHUNKMAP_RESET(einfo->chunkmap); |
| 206 | goto out; |
| 207 | } |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 208 | |
| 209 | for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) { |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 210 | found = 0; |
| 211 | DEBUGP("Next Chunk type %s\n", ptr); |
Kiran Kumar | 5492402 | 2004-03-02 18:38:27 +0000 | [diff] [blame] | 212 | |
| 213 | if ((chunk_flags = strchr(ptr, ':')) != NULL) { |
| 214 | *chunk_flags++ = 0; |
| 215 | } |
| 216 | |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 217 | for (i = 0; i < ELEMCOUNT(sctp_chunk_names); i++) { |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 218 | if (strcasecmp(sctp_chunk_names[i].name, ptr) == 0) { |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 219 | DEBUGP("Chunk num %d\n", sctp_chunk_names[i].chunk_type); |
| 220 | SCTP_CHUNKMAP_SET(einfo->chunkmap, |
| 221 | sctp_chunk_names[i].chunk_type); |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 222 | found = 1; |
| 223 | break; |
| 224 | } |
| 225 | } |
| 226 | if (!found) |
| 227 | exit_error(PARAMETER_PROBLEM, |
| 228 | "Unknown sctp chunk `%s'", ptr); |
Kiran Kumar | 5492402 | 2004-03-02 18:38:27 +0000 | [diff] [blame] | 229 | |
| 230 | if (chunk_flags) { |
| 231 | DEBUGP("Chunk flags %s\n", chunk_flags); |
| 232 | for (j = 0; j < strlen(chunk_flags); j++) { |
| 233 | char *p; |
| 234 | int bit; |
| 235 | |
| 236 | if ((p = strchr(sctp_chunk_names[i].valid_flags, |
| 237 | toupper(chunk_flags[j]))) != NULL) { |
| 238 | bit = p - sctp_chunk_names[i].valid_flags; |
| 239 | bit = 7 - bit; |
| 240 | |
| 241 | save_chunk_flag_info(einfo->flag_info, |
| 242 | &(einfo->flag_count), i, bit, |
| 243 | isupper(chunk_flags[j])); |
| 244 | } else { |
| 245 | exit_error(PARAMETER_PROBLEM, |
| 246 | "Invalid flags for chunk type %d\n", i); |
| 247 | } |
| 248 | } |
| 249 | } |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 250 | } |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 251 | out: |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 252 | free(buffer); |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | static void |
| 256 | parse_sctp_chunks(struct ipt_sctp_info *einfo, |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 257 | const char *match_type, |
| 258 | const char *chunks) |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 259 | { |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 260 | DEBUGP("Match type: %s Chunks: %s\n", match_type, chunks); |
| 261 | if (!strcasecmp(match_type, "ANY")) { |
| 262 | einfo->chunk_match_type = SCTP_CHUNK_MATCH_ANY; |
| 263 | } else if (!strcasecmp(match_type, "ALL")) { |
| 264 | einfo->chunk_match_type = SCTP_CHUNK_MATCH_ALL; |
| 265 | } else if (!strcasecmp(match_type, "ONLY")) { |
| 266 | einfo->chunk_match_type = SCTP_CHUNK_MATCH_ONLY; |
| 267 | } else { |
| 268 | exit_error (PARAMETER_PROBLEM, |
| 269 | "Match type has to be one of \"ALL\", \"ANY\" or \"ONLY\""); |
| 270 | } |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 271 | |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 272 | SCTP_CHUNKMAP_RESET(einfo->chunkmap); |
| 273 | parse_sctp_chunk(einfo, chunks); |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 276 | static int |
| 277 | parse(int c, char **argv, int invert, unsigned int *flags, |
| 278 | const struct ipt_entry *entry, |
| 279 | unsigned int *nfcache, |
| 280 | struct ipt_entry_match **match) |
| 281 | { |
| 282 | struct ipt_sctp_info *einfo |
| 283 | = (struct ipt_sctp_info *)(*match)->data; |
| 284 | |
| 285 | switch (c) { |
| 286 | case '1': |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 287 | if (*flags & IPT_SCTP_SRC_PORTS) |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 288 | exit_error(PARAMETER_PROBLEM, |
| 289 | "Only one `--source-port' allowed"); |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 290 | einfo->flags |= IPT_SCTP_SRC_PORTS; |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 291 | check_inverse(optarg, &invert, &optind, 0); |
| 292 | parse_sctp_ports(argv[optind-1], einfo->spts); |
| 293 | if (invert) |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 294 | einfo->invflags |= IPT_SCTP_SRC_PORTS; |
| 295 | *flags |= IPT_SCTP_SRC_PORTS; |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 296 | *nfcache |= NFC_IP_SRC_PT; |
| 297 | break; |
| 298 | |
| 299 | case '2': |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 300 | if (*flags & IPT_SCTP_DEST_PORTS) |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 301 | exit_error(PARAMETER_PROBLEM, |
| 302 | "Only one `--destination-port' allowed"); |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 303 | einfo->flags |= IPT_SCTP_DEST_PORTS; |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 304 | check_inverse(optarg, &invert, &optind, 0); |
| 305 | parse_sctp_ports(argv[optind-1], einfo->dpts); |
| 306 | if (invert) |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 307 | einfo->invflags |= IPT_SCTP_DEST_PORTS; |
| 308 | *flags |= IPT_SCTP_DEST_PORTS; |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 309 | *nfcache |= NFC_IP_DST_PT; |
| 310 | break; |
| 311 | |
| 312 | case '3': |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 313 | if (*flags & IPT_SCTP_CHUNK_TYPES) |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 314 | exit_error(PARAMETER_PROBLEM, |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 315 | "Only one `--chunk-types' allowed"); |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 316 | check_inverse(optarg, &invert, &optind, 0); |
| 317 | |
| 318 | if (!argv[optind] |
| 319 | || argv[optind][0] == '-' || argv[optind][0] == '!') |
| 320 | exit_error(PARAMETER_PROBLEM, |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 321 | "--chunk-types requires two args"); |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 322 | |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 323 | einfo->flags |= IPT_SCTP_CHUNK_TYPES; |
| 324 | parse_sctp_chunks(einfo, argv[optind-1], argv[optind]); |
| 325 | if (invert) |
| 326 | einfo->invflags |= IPT_SCTP_CHUNK_TYPES; |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 327 | optind++; |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 328 | *flags |= IPT_SCTP_CHUNK_TYPES; |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 329 | break; |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 330 | |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 331 | default: |
| 332 | return 0; |
| 333 | } |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 334 | return 1; |
| 335 | } |
| 336 | |
| 337 | static void |
| 338 | final_check(unsigned int flags) |
| 339 | { |
| 340 | } |
| 341 | |
| 342 | static char * |
| 343 | port_to_service(int port) |
| 344 | { |
| 345 | struct servent *service; |
| 346 | |
| 347 | if ((service = getservbyport(htons(port), "sctp"))) |
| 348 | return service->s_name; |
| 349 | |
| 350 | return NULL; |
| 351 | } |
| 352 | |
| 353 | static void |
| 354 | print_port(u_int16_t port, int numeric) |
| 355 | { |
| 356 | char *service; |
| 357 | |
| 358 | if (numeric || (service = port_to_service(port)) == NULL) |
| 359 | printf("%u", port); |
| 360 | else |
| 361 | printf("%s", service); |
| 362 | } |
| 363 | |
| 364 | static void |
| 365 | print_ports(const char *name, u_int16_t min, u_int16_t max, |
| 366 | int invert, int numeric) |
| 367 | { |
| 368 | const char *inv = invert ? "!" : ""; |
| 369 | |
| 370 | if (min != 0 || max != 0xFFFF || invert) { |
| 371 | printf("%s", name); |
| 372 | if (min == max) { |
| 373 | printf(":%s", inv); |
| 374 | print_port(min, numeric); |
| 375 | } else { |
| 376 | printf("s:%s", inv); |
| 377 | print_port(min, numeric); |
| 378 | printf(":"); |
| 379 | print_port(max, numeric); |
| 380 | } |
| 381 | printf(" "); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | static void |
Kiran Kumar | 5492402 | 2004-03-02 18:38:27 +0000 | [diff] [blame] | 386 | print_chunk_flags(u_int32_t chunknum, u_int8_t chunk_flags, u_int8_t chunk_flags_mask) |
| 387 | { |
| 388 | int i; |
| 389 | |
| 390 | DEBUGP("type: %d\tflags: %x\tflag mask: %x\n", chunknum, chunk_flags, |
| 391 | chunk_flags_mask); |
| 392 | |
| 393 | if (chunk_flags_mask) { |
| 394 | printf(":"); |
| 395 | } |
| 396 | |
| 397 | for (i = 7; i >= 0; i--) { |
| 398 | if (chunk_flags_mask & (1 << i)) { |
| 399 | if (chunk_flags & (1 << i)) { |
| 400 | printf("%c", sctp_chunk_names[chunknum].valid_flags[7-i]); |
| 401 | } else { |
| 402 | printf("%c", tolower(sctp_chunk_names[chunknum].valid_flags[7-i])); |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | static void |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 409 | print_chunk(u_int32_t chunknum, int numeric) |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 410 | { |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 411 | if (numeric) { |
| 412 | printf("0x%04X", chunknum); |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 413 | } |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 414 | else { |
Kiran Kumar | 5492402 | 2004-03-02 18:38:27 +0000 | [diff] [blame] | 415 | int i; |
| 416 | |
| 417 | for (i = 0; i < ELEMCOUNT(sctp_chunk_names); i++) { |
| 418 | if (sctp_chunk_names[i].chunk_type == chunknum) |
| 419 | printf("%s", sctp_chunk_names[chunknum].name); |
| 420 | } |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 421 | } |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | static void |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 425 | print_chunks(u_int32_t chunk_match_type, |
| 426 | const u_int32_t *chunkmap, |
Kiran Kumar | 5492402 | 2004-03-02 18:38:27 +0000 | [diff] [blame] | 427 | const struct ipt_sctp_flag_info *flag_info, |
| 428 | int flag_count, |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 429 | int numeric) |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 430 | { |
Kiran Kumar | 5492402 | 2004-03-02 18:38:27 +0000 | [diff] [blame] | 431 | int i, j; |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 432 | int flag; |
| 433 | |
| 434 | switch (chunk_match_type) { |
| 435 | case SCTP_CHUNK_MATCH_ANY: printf("any "); break; |
| 436 | case SCTP_CHUNK_MATCH_ALL: printf("all "); break; |
| 437 | case SCTP_CHUNK_MATCH_ONLY: printf("only "); break; |
| 438 | default: printf("Never reach herer\n"); break; |
| 439 | } |
| 440 | |
| 441 | if (SCTP_CHUNKMAP_IS_CLEAR(chunkmap)) { |
| 442 | printf("NONE "); |
| 443 | goto out; |
| 444 | } |
| 445 | |
| 446 | if (SCTP_CHUNKMAP_IS_ALL_SET(chunkmap)) { |
| 447 | printf("ALL "); |
| 448 | goto out; |
| 449 | } |
| 450 | |
| 451 | flag = 0; |
| 452 | for (i = 0; i < 256; i++) { |
| 453 | if (SCTP_CHUNKMAP_IS_SET(chunkmap, i)) { |
| 454 | flag && printf(","); |
| 455 | flag = 1; |
| 456 | print_chunk(i, numeric); |
Kiran Kumar | 5492402 | 2004-03-02 18:38:27 +0000 | [diff] [blame] | 457 | for (j = 0; j < flag_count; j++) { |
| 458 | if (flag_info[j].chunktype == i) { |
| 459 | print_chunk_flags(i, flag_info[j].flag, |
| 460 | flag_info[j].flag_mask); |
| 461 | } |
| 462 | } |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 463 | } |
| 464 | } |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 465 | |
| 466 | flag && printf(" "); |
| 467 | out: |
| 468 | return; |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | /* Prints out the matchinfo. */ |
| 472 | static void |
| 473 | print(const struct ipt_ip *ip, |
| 474 | const struct ipt_entry_match *match, |
| 475 | int numeric) |
| 476 | { |
| 477 | const struct ipt_sctp_info *einfo = |
| 478 | (const struct ipt_sctp_info *)match->data; |
| 479 | |
| 480 | printf("sctp "); |
| 481 | |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 482 | if (einfo->flags & IPT_SCTP_SRC_PORTS) { |
| 483 | print_ports("spt", einfo->spts[0], einfo->spts[1], |
| 484 | einfo->invflags & IPT_SCTP_SRC_PORTS, |
| 485 | numeric); |
| 486 | } |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 487 | |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 488 | if (einfo->flags & IPT_SCTP_DEST_PORTS) { |
| 489 | print_ports("dpt", einfo->dpts[0], einfo->dpts[1], |
| 490 | einfo->invflags & IPT_SCTP_DEST_PORTS, |
| 491 | numeric); |
| 492 | } |
| 493 | |
| 494 | if (einfo->flags & IPT_SCTP_CHUNK_TYPES) { |
| 495 | /* FIXME: print_chunks() is used in save() where the printing of '!' |
| 496 | s taken care of, so we need to do that here as well */ |
| 497 | if (einfo->invflags & IPT_SCTP_CHUNK_TYPES) { |
| 498 | printf("! "); |
| 499 | } |
Kiran Kumar | 5492402 | 2004-03-02 18:38:27 +0000 | [diff] [blame] | 500 | print_chunks(einfo->chunk_match_type, einfo->chunkmap, |
| 501 | einfo->flag_info, einfo->flag_count, numeric); |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 502 | } |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | /* Saves the union ipt_matchinfo in parsable form to stdout. */ |
| 506 | static void |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 507 | save(const struct ipt_ip *ip, |
| 508 | const struct ipt_entry_match *match) |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 509 | { |
| 510 | const struct ipt_sctp_info *einfo = |
| 511 | (const struct ipt_sctp_info *)match->data; |
| 512 | |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 513 | if (einfo->flags & IPT_SCTP_SRC_PORTS) { |
| 514 | if (einfo->invflags & IPT_SCTP_SRC_PORTS) |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 515 | printf("! "); |
| 516 | if (einfo->spts[0] != einfo->spts[1]) |
| 517 | printf("--sport %u:%u ", |
| 518 | einfo->spts[0], einfo->spts[1]); |
| 519 | else |
| 520 | printf("--sport %u ", einfo->spts[0]); |
| 521 | } |
| 522 | |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 523 | if (einfo->flags & IPT_SCTP_DEST_PORTS) { |
| 524 | if (einfo->invflags & IPT_SCTP_DEST_PORTS) |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 525 | printf("! "); |
| 526 | if (einfo->dpts[0] != einfo->dpts[1]) |
| 527 | printf("--dport %u:%u ", |
| 528 | einfo->dpts[0], einfo->dpts[1]); |
| 529 | else |
| 530 | printf("--dport %u ", einfo->dpts[0]); |
| 531 | } |
| 532 | |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 533 | if (einfo->flags & IPT_SCTP_CHUNK_TYPES) { |
| 534 | if (einfo->invflags & IPT_SCTP_CHUNK_TYPES) |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 535 | printf("! "); |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 536 | printf("--chunk-types "); |
| 537 | |
Kiran Kumar | 5492402 | 2004-03-02 18:38:27 +0000 | [diff] [blame] | 538 | print_chunks(einfo->chunk_match_type, einfo->chunkmap, |
| 539 | einfo->flag_info, einfo->flag_count, 0); |
Harald Welte | 8f578a0 | 2003-05-03 18:05:58 +0000 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | |
| 543 | static |
| 544 | struct iptables_match sctp |
| 545 | = { .name = "sctp", |
| 546 | .version = IPTABLES_VERSION, |
| 547 | .size = IPT_ALIGN(sizeof(struct ipt_sctp_info)), |
| 548 | .userspacesize = IPT_ALIGN(sizeof(struct ipt_sctp_info)), |
| 549 | .help = &help, |
| 550 | .init = &init, |
| 551 | .parse = &parse, |
| 552 | .final_check = &final_check, |
| 553 | .print = &print, |
| 554 | .save = &save, |
| 555 | .extra_opts = opts |
| 556 | }; |
| 557 | |
| 558 | void _init(void) |
| 559 | { |
| 560 | register_match(&sctp); |
| 561 | } |
Harald Welte | 1291523 | 2004-02-21 09:20:34 +0000 | [diff] [blame] | 562 | |