• Home
  • History
  • Annotate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/ap/gpl/iptables-1.4.12.1/extensions/

Lines Matching defs:*

0 /* 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
10 #include <stdbool.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <getopt.h>
15 #include <netdb.h>
16 #include <ctype.h>
18 #include <netinet/in.h>
19 #include <xtables.h>
21 #include <linux/netfilter/xt_sctp.h>
23 #if 0
24 #define DEBUGP(format, first...) printf(format, ##first)
25 #define static
26 #else
27 #define DEBUGP(format, fist...)
28 #endif
30 static void
31 print_chunk(uint32_t chunknum, int numeric);
33 static void sctp_init(struct xt_entry_match *m)
35 int i;
36 struct xt_sctp_info *einfo = (struct xt_sctp_info *)m->data;
38 for (i = 0; i < XT_NUM_SCTP_FLAGS; i++) {
39 einfo->flag_info[i].chunktype = -1;
43 static void sctp_help(void)
45 printf(
46 "sctp match options\n"
47 "[!] --source-port port[:port] match source port(s)\n"
48 " --sport ...\n"
49 "[!] --destination-port port[:port] match destination port(s)\n"
50 " --dport ...\n"
51 "[!] --chunk-types (all|any|none) (chunktype[:flags])+ match if all, any or none of\n"
52 " chunktypes are present\n"
53 "chunktypes - DATA INIT INIT_ACK SACK HEARTBEAT HEARTBEAT_ACK ABORT SHUTDOWN SHUTDOWN_ACK ERROR COOKIE_ECHO COOKIE_ACK ECN_ECNE ECN_CWR SHUTDOWN_COMPLETE ASCONF ASCONF_ACK FORWARD_TSN ALL NONE\n");
56 static const struct option sctp_opts[] = {
57 {.name = "source-port", .has_arg = true, .val = '1'},
58 {.name = "sport", .has_arg = true, .val = '1'},
59 {.name = "destination-port", .has_arg = true, .val = '2'},
60 {.name = "dport", .has_arg = true, .val = '2'},
61 {.name = "chunk-types", .has_arg = true, .val = '3'},
62 XT_GETOPT_TABLEEND,
65 static void
66 parse_sctp_ports(const char *portstring,
67 uint16_t *ports)
69 char *buffer;
70 char *cp;
72 buffer = strdup(portstring);
73 DEBUGP("%s\n", portstring);
74 if ((cp = strchr(buffer, ':')) == NULL) {
75 ports[0] = ports[1] = xtables_parse_port(buffer, "sctp");
77 else {
78 *cp = '\0';
79 cp++;
81 ports[0] = buffer[0] ? xtables_parse_port(buffer, "sctp") : 0;
82 ports[1] = cp[0] ? xtables_parse_port(cp, "sctp") : 0xFFFF;
84 if (ports[0] > ports[1])
85 xtables_error(PARAMETER_PROBLEM,
86 "invalid portrange (min > max)");
88 free(buffer);
91 struct sctp_chunk_names {
92 const char *name;
93 unsigned int chunk_type;
94 const char *valid_flags;
97 /*'ALL' and 'NONE' will be treated specially. */
98 static const struct sctp_chunk_names sctp_chunk_names[]
99 = { { .name = "DATA", .chunk_type = 0, .valid_flags = "----IUBE"},
100 { .name = "INIT", .chunk_type = 1, .valid_flags = "--------"},
101 { .name = "INIT_ACK", .chunk_type = 2, .valid_flags = "--------"},
102 { .name = "SACK", .chunk_type = 3, .valid_flags = "--------"},
103 { .name = "HEARTBEAT", .chunk_type = 4, .valid_flags = "--------"},
104 { .name = "HEARTBEAT_ACK", .chunk_type = 5, .valid_flags = "--------"},
105 { .name = "ABORT", .chunk_type = 6, .valid_flags = "-------T"},
106 { .name = "SHUTDOWN", .chunk_type = 7, .valid_flags = "--------"},
107 { .name = "SHUTDOWN_ACK", .chunk_type = 8, .valid_flags = "--------"},
108 { .name = "ERROR", .chunk_type = 9, .valid_flags = "--------"},
109 { .name = "COOKIE_ECHO", .chunk_type = 10, .valid_flags = "--------"},
110 { .name = "COOKIE_ACK", .chunk_type = 11, .valid_flags = "--------"},
111 { .name = "ECN_ECNE", .chunk_type = 12, .valid_flags = "--------"},
112 { .name = "ECN_CWR", .chunk_type = 13, .valid_flags = "--------"},
113 { .name = "SHUTDOWN_COMPLETE", .chunk_type = 14, .valid_flags = "-------T"},
114 { .name = "ASCONF", .chunk_type = 193, .valid_flags = "--------"},
115 { .name = "ASCONF_ACK", .chunk_type = 128, .valid_flags = "--------"},
116 { .name = "FORWARD_TSN", .chunk_type = 192, .valid_flags = "--------"},
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)
126 int i;
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);
137 return;
141 if (*flag_count == XT_NUM_SCTP_FLAGS) {
142 xtables_error (PARAMETER_PROBLEM,
143 "Number of chunk types with flags exceeds currently allowed limit."
144 "Increasing this limit involves changing IPT_NUM_SCTP_FLAGS and"
145 "recompiling both the kernel space and user space modules\n");
148 flag_info[*flag_count].chunktype = chunktype;
149 flag_info[*flag_count].flag_mask |= (1 << bit);
150 if (set) {
151 flag_info[*flag_count].flag |= (1 << bit);
153 (*flag_count)++;
156 static void
157 parse_sctp_chunk(struct xt_sctp_info *einfo,
158 const char *chunks)
160 char *ptr;
161 char *buffer;
162 unsigned int i, j;
163 int found = 0;
164 char *chunk_flags;
166 buffer = strdup(chunks);
167 DEBUGP("Buffer: %s\n", buffer);
169 SCTP_CHUNKMAP_RESET(einfo->chunkmap);
171 if (!strcasecmp(buffer, "ALL")) {
172 SCTP_CHUNKMAP_SET_ALL(einfo->chunkmap);
173 goto out;
176 if (!strcasecmp(buffer, "NONE")) {
177 SCTP_CHUNKMAP_RESET(einfo->chunkmap);
178 goto out;
181 for (ptr = strtok(buffer, ","); ptr; ptr = strtok(NULL, ",")) {
182 found = 0;
183 DEBUGP("Next Chunk type %s\n", ptr);
185 if ((chunk_flags = strchr(ptr, ':')) != NULL) {
186 *chunk_flags++ = 0;
189 for (i = 0; i < ARRAY_SIZE(sctp_chunk_names); ++i)
190 if (strcasecmp(sctp_chunk_names[i].name, ptr) == 0) {
191 DEBUGP("Chunk num %d\n", sctp_chunk_names[i].chunk_type);
192 SCTP_CHUNKMAP_SET(einfo->chunkmap,
193 sctp_chunk_names[i].chunk_type);
194 found = 1;
195 break;
197 if (!found)
198 xtables_error(PARAMETER_PROBLEM,
199 "Unknown sctp chunk `%s'", ptr);
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;
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;
212 save_chunk_flag_info(einfo->flag_info,
213 &(einfo->flag_count), i, bit,
214 isupper(chunk_flags[j]));
215 } else {
216 xtables_error(PARAMETER_PROBLEM,
217 "Invalid flags for chunk type %d\n", i);
222 out:
223 free(buffer);
226 static void
227 parse_sctp_chunks(struct xt_sctp_info *einfo,
228 const char *match_type,
229 const char *chunks)
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 {
239 xtables_error (PARAMETER_PROBLEM,
240 "Match type has to be one of \"ALL\", \"ANY\" or \"ONLY\"");
243 SCTP_CHUNKMAP_RESET(einfo->chunkmap);
244 parse_sctp_chunk(einfo, chunks);
247 static int
248 sctp_parse(int c, char **argv, int invert, unsigned int *flags,
249 const void *entry, struct xt_entry_match **match)
251 struct xt_sctp_info *einfo
252 = (struct xt_sctp_info *)(*match)->data;
254 switch (c) {
255 case '1':
256 if (*flags & XT_SCTP_SRC_PORTS)
257 xtables_error(PARAMETER_PROBLEM,
258 "Only one `--source-port' allowed");
259 einfo->flags |= XT_SCTP_SRC_PORTS;
260 parse_sctp_ports(optarg, einfo->spts);
261 if (invert)
262 einfo->invflags |= XT_SCTP_SRC_PORTS;
263 *flags |= XT_SCTP_SRC_PORTS;
264 break;
266 case '2':
267 if (*flags & XT_SCTP_DEST_PORTS)
268 xtables_error(PARAMETER_PROBLEM,
269 "Only one `--destination-port' allowed");
270 einfo->flags |= XT_SCTP_DEST_PORTS;
271 parse_sctp_ports(optarg, einfo->dpts);
272 if (invert)
273 einfo->invflags |= XT_SCTP_DEST_PORTS;
274 *flags |= XT_SCTP_DEST_PORTS;
275 break;
277 case '3':
278 if (*flags & XT_SCTP_CHUNK_TYPES)
279 xtables_error(PARAMETER_PROBLEM,
280 "Only one `--chunk-types' allowed");
281 if (!argv[optind]
282 || argv[optind][0] == '-' || argv[optind][0] == '!')
283 xtables_error(PARAMETER_PROBLEM,
284 "--chunk-types requires two args");
286 einfo->flags |= XT_SCTP_CHUNK_TYPES;
287 parse_sctp_chunks(einfo, optarg, argv[optind]);
288 if (invert)
289 einfo->invflags |= XT_SCTP_CHUNK_TYPES;
290 optind++;
291 *flags |= XT_SCTP_CHUNK_TYPES;
292 break;
294 return 1;
297 static const char *
298 port_to_service(int port)
300 const struct servent *service;
302 if ((service = getservbyport(htons(port), "sctp")))
303 return service->s_name;
305 return NULL;
308 static void
309 print_port(uint16_t port, int numeric)
311 const char *service;
313 if (numeric || (service = port_to_service(port)) == NULL)
314 printf("%u", port);
315 else
316 printf("%s", service);
319 static void
320 print_ports(const char *name, uint16_t min, uint16_t max,
321 int invert, int numeric)
323 const char *inv = invert ? "!" : "";
325 if (min != 0 || max != 0xFFFF || invert) {
326 printf(" %s", name);
327 if (min == max) {
328 printf(":%s", inv);
329 print_port(min, numeric);
330 } else {
331 printf("s:%s", inv);
332 print_port(min, numeric);
333 printf(":");
334 print_port(max, numeric);
339 static void
340 print_chunk_flags(uint32_t chunknum, uint8_t chunk_flags, uint8_t chunk_flags_mask)
342 int i;
344 DEBUGP("type: %d\tflags: %x\tflag mask: %x\n", chunknum, chunk_flags,
345 chunk_flags_mask);
347 if (chunk_flags_mask) {
348 printf(":");
351 for (i = 7; i >= 0; i--) {
352 if (chunk_flags_mask & (1 << i)) {
353 if (chunk_flags & (1 << i)) {
354 printf("%c", sctp_chunk_names[chunknum].valid_flags[7-i]);
355 } else {
356 printf("%c", tolower(sctp_chunk_names[chunknum].valid_flags[7-i]));
362 static void
363 print_chunk(uint32_t chunknum, int numeric)
365 if (numeric) {
366 printf("0x%04X", chunknum);
368 else {
369 int i;
371 for (i = 0; i < ARRAY_SIZE(sctp_chunk_names); ++i)
372 if (sctp_chunk_names[i].chunk_type == chunknum)
373 printf("%s", sctp_chunk_names[chunknum].name);
377 static void
378 print_chunks(const struct xt_sctp_info *einfo, int numeric)
380 uint32_t chunk_match_type = einfo->chunk_match_type;
381 const struct xt_sctp_flag_info *flag_info = einfo->flag_info;
382 int flag_count = einfo->flag_count;
383 int i, j;
384 int flag;
386 switch (chunk_match_type) {
387 case SCTP_CHUNK_MATCH_ANY: printf(" any"); break;
388 case SCTP_CHUNK_MATCH_ALL: printf(" all"); break;
389 case SCTP_CHUNK_MATCH_ONLY: printf(" only"); break;
390 default: printf("Never reach here\n"); break;
393 if (SCTP_CHUNKMAP_IS_CLEAR(einfo->chunkmap)) {
394 printf(" NONE");
395 goto out;
398 if (SCTP_CHUNKMAP_IS_ALL_SET(einfo->chunkmap)) {
399 printf(" ALL");
400 goto out;
403 flag = 0;
404 for (i = 0; i < 256; i++) {
405 if (SCTP_CHUNKMAP_IS_SET(einfo->chunkmap, i)) {
406 if (flag)
407 printf(",");
408 else
409 putchar(' ');
410 flag = 1;
411 print_chunk(i, numeric);
412 for (j = 0; j < flag_count; j++) {
413 if (flag_info[j].chunktype == i) {
414 print_chunk_flags(i, flag_info[j].flag,
415 flag_info[j].flag_mask);
420 out:
421 return;
424 static void
425 sctp_print(const void *ip, const struct xt_entry_match *match, int numeric)
427 const struct xt_sctp_info *einfo =
428 (const struct xt_sctp_info *)match->data;
430 printf(" sctp");
432 if (einfo->flags & XT_SCTP_SRC_PORTS) {
433 print_ports("spt", einfo->spts[0], einfo->spts[1],
434 einfo->invflags & XT_SCTP_SRC_PORTS,
435 numeric);
438 if (einfo->flags & XT_SCTP_DEST_PORTS) {
439 print_ports("dpt", einfo->dpts[0], einfo->dpts[1],
440 einfo->invflags & XT_SCTP_DEST_PORTS,
441 numeric);
444 if (einfo->flags & XT_SCTP_CHUNK_TYPES) {
445 /* FIXME: print_chunks() is used in save() where the printing of '!'
446 s taken care of, so we need to do that here as well */
447 if (einfo->invflags & XT_SCTP_CHUNK_TYPES) {
448 printf(" !");
450 print_chunks(einfo, numeric);
454 static void sctp_save(const void *ip, const struct xt_entry_match *match)
456 const struct xt_sctp_info *einfo =
457 (const struct xt_sctp_info *)match->data;
459 if (einfo->flags & XT_SCTP_SRC_PORTS) {
460 if (einfo->invflags & XT_SCTP_SRC_PORTS)
461 printf(" !");
462 if (einfo->spts[0] != einfo->spts[1])
463 printf(" --sport %u:%u",
464 einfo->spts[0], einfo->spts[1]);
465 else
466 printf(" --sport %u", einfo->spts[0]);
469 if (einfo->flags & XT_SCTP_DEST_PORTS) {
470 if (einfo->invflags & XT_SCTP_DEST_PORTS)
471 printf(" !");
472 if (einfo->dpts[0] != einfo->dpts[1])
473 printf(" --dport %u:%u",
474 einfo->dpts[0], einfo->dpts[1]);
475 else
476 printf(" --dport %u", einfo->dpts[0]);
479 if (einfo->flags & XT_SCTP_CHUNK_TYPES) {
480 if (einfo->invflags & XT_SCTP_CHUNK_TYPES)
481 printf(" !");
482 printf(" --chunk-types");
484 print_chunks(einfo, 0);
488 static struct xtables_match sctp_match = {
489 .name = "sctp",
490 .family = NFPROTO_UNSPEC,
491 .version = XTABLES_VERSION,
492 .size = XT_ALIGN(sizeof(struct xt_sctp_info)),
493 .userspacesize = XT_ALIGN(sizeof(struct xt_sctp_info)),
494 .help = sctp_help,
495 .init = sctp_init,
496 .parse = sctp_parse,
497 .print = sctp_print,
498 .save = sctp_save,
499 .extra_opts = sctp_opts,
502 void _init(void)
504 xtables_register_match(&sctp_match);