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