blob: 809e80d5ec8e7ae666ecc2045fdbea29986b0d3d [file] [log] [blame]
András Kis-Szabó2ea56492002-04-29 13:51:37 +00001#include <stdio.h>
András Kis-Szabó2ea56492002-04-29 13:51:37 +00002#include <string.h>
3#include <stdlib.h>
András Kis-Szabó2ea56492002-04-29 13:51:37 +00004#include <errno.h>
Jan Engelhardt5d9678a2008-11-20 10:15:35 +01005#include <xtables.h>
András Kis-Szabó2ea56492002-04-29 13:51:37 +00006#include <linux/netfilter_ipv6/ip6t_opts.h>
Jan Engelhardtddac6c52008-09-01 14:22:19 +02007
András Kis-Szabó2ea56492002-04-29 13:51:37 +00008#define DEBUG 0
András Kis-Szabó2ea56492002-04-29 13:51:37 +00009
Jan Engelhardt7a969bb2011-03-03 00:40:43 +010010enum {
11 O_HBH_LEN = 0,
12 O_HBH_OPTS,
13};
14
Jan Engelhardt997045f2007-10-04 16:29:21 +000015static void hbh_help(void)
András Kis-Szabó2ea56492002-04-29 13:51:37 +000016{
17 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020018"hbh match options:\n"
Jan Engelhardt96727922008-08-13 14:42:41 +020019"[!] --hbh-len length total length of this header\n"
Jan Engelhardte2f588a2007-10-04 16:30:40 +000020" --hbh-opts TYPE[:LEN][,TYPE[:LEN]...] \n"
21" Options and its length (list, max: %d)\n",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020022IP6T_OPTS_OPTSNR);
András Kis-Szabó2ea56492002-04-29 13:51:37 +000023}
24
Jan Engelhardt7a969bb2011-03-03 00:40:43 +010025static const struct xt_option_entry hbh_opts[] = {
26 {.name = "hbh-len", .id = O_HBH_LEN, .type = XTTYPE_UINT32,
27 .flags = XTOPT_INVERT | XTOPT_PUT,
28 XTOPT_POINTER(struct ip6t_opts, hdrlen)},
29 {.name = "hbh-opts", .id = O_HBH_OPTS, .type = XTTYPE_STRING},
30 XTOPT_TABLEEND,
András Kis-Szabó2ea56492002-04-29 13:51:37 +000031};
András Kis-Szabó2ea56492002-04-29 13:51:37 +000032
Jan Engelhardt7ac40522011-01-07 12:34:04 +010033static uint32_t
András Kis-Szabó2ea56492002-04-29 13:51:37 +000034parse_opts_num(const char *idstr, const char *typestr)
35{
36 unsigned long int id;
37 char* ep;
38
39 id = strtoul(idstr,&ep,0) ;
40
41 if ( idstr == ep ) {
Jan Engelhardt1829ed42009-02-21 03:29:44 +010042 xtables_error(PARAMETER_PROBLEM,
Jan Engelhardte2f588a2007-10-04 16:30:40 +000043 "hbh: no valid digits in %s `%s'", typestr, idstr);
András Kis-Szabó2ea56492002-04-29 13:51:37 +000044 }
45 if ( id == ULONG_MAX && errno == ERANGE ) {
Jan Engelhardt1829ed42009-02-21 03:29:44 +010046 xtables_error(PARAMETER_PROBLEM,
András Kis-Szabó2ea56492002-04-29 13:51:37 +000047 "%s `%s' specified too big: would overflow",
48 typestr, idstr);
49 }
50 if ( *idstr != '\0' && *ep != '\0' ) {
Jan Engelhardt1829ed42009-02-21 03:29:44 +010051 xtables_error(PARAMETER_PROBLEM,
Jan Engelhardte2f588a2007-10-04 16:30:40 +000052 "hbh: error parsing %s `%s'", typestr, idstr);
András Kis-Szabó2ea56492002-04-29 13:51:37 +000053 }
Jan Engelhardt213e1852009-01-27 17:24:34 +010054 return id;
András Kis-Szabó2ea56492002-04-29 13:51:37 +000055}
56
57static int
Jan Engelhardt7ac40522011-01-07 12:34:04 +010058parse_options(const char *optsstr, uint16_t *opts)
András Kis-Szabó2ea56492002-04-29 13:51:37 +000059{
60 char *buffer, *cp, *next, *range;
61 unsigned int i;
62
63 buffer = strdup(optsstr);
Jan Engelhardt1829ed42009-02-21 03:29:44 +010064 if (!buffer) xtables_error(OTHER_PROBLEM, "strdup failed");
András Kis-Szabó2ea56492002-04-29 13:51:37 +000065
66 for (cp=buffer, i=0; cp && i<IP6T_OPTS_OPTSNR; cp=next,i++)
67 {
68 next=strchr(cp, ',');
69 if (next) *next++='\0';
70 range = strchr(cp, ':');
71 if (range) {
72 if (i == IP6T_OPTS_OPTSNR-1)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010073 xtables_error(PARAMETER_PROBLEM,
András Kis-Szabó2ea56492002-04-29 13:51:37 +000074 "too many ports specified");
75 *range++ = '\0';
76 }
Jan Engelhardt213e1852009-01-27 17:24:34 +010077 opts[i] = (parse_opts_num(cp, "opt") & 0xFF) << 8;
András Kis-Szabó2ea56492002-04-29 13:51:37 +000078 if (range) {
79 if (opts[i] == 0)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010080 xtables_error(PARAMETER_PROBLEM, "PAD0 has not got length");
Jan Engelhardt213e1852009-01-27 17:24:34 +010081 opts[i] |= parse_opts_num(range, "length") & 0xFF;
András Kis-Szabó2ea56492002-04-29 13:51:37 +000082 } else {
83 opts[i] |= (0x00FF);
84 }
85
86#if DEBUG
87 printf("opts str: %s %s\n", cp, range);
88 printf("opts opt: %04X\n", opts[i]);
89#endif
90 }
Jan Engelhardt1829ed42009-02-21 03:29:44 +010091 if (cp) xtables_error(PARAMETER_PROBLEM, "too many addresses specified");
András Kis-Szabó2ea56492002-04-29 13:51:37 +000092
93 free(buffer);
94
95#if DEBUG
96 printf("addr nr: %d\n", i);
97#endif
98
99 return i;
100}
101
Jan Engelhardt7a969bb2011-03-03 00:40:43 +0100102static void hbh_parse(struct xt_option_call *cb)
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000103{
Jan Engelhardt7a969bb2011-03-03 00:40:43 +0100104 struct ip6t_opts *optinfo = cb->data;
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000105
Jan Engelhardt7a969bb2011-03-03 00:40:43 +0100106 xtables_option_parse(cb);
107 switch (cb->entry->id) {
108 case O_HBH_LEN:
109 if (cb->invert)
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000110 optinfo->invflags |= IP6T_OPTS_INV_LEN;
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000111 break;
Jan Engelhardt7a969bb2011-03-03 00:40:43 +0100112 case O_HBH_OPTS:
113 optinfo->optsnr = parse_options(cb->arg, optinfo->opts);
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000114 optinfo->flags |= IP6T_OPTS_OPTS;
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000115 break;
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000116 }
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000117}
118
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000119static void
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100120print_options(unsigned int optsnr, uint16_t *optsp)
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000121{
122 unsigned int i;
123
124 for(i=0; i<optsnr; i++){
Jan Engelhardt73866352010-12-18 02:04:59 +0100125 printf("%c", (i==0)?' ':',');
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000126 printf("%d", (optsp[i] & 0xFF00)>>8);
127 if ((optsp[i] & 0x00FF) != 0x00FF){
128 printf(":%d", (optsp[i] & 0x00FF));
129 }
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000130 }
131}
132
Jan Engelhardt997045f2007-10-04 16:29:21 +0000133static void hbh_print(const void *ip, const struct xt_entry_match *match,
134 int numeric)
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000135{
136 const struct ip6t_opts *optinfo = (struct ip6t_opts *)match->data;
137
Jan Engelhardt73866352010-12-18 02:04:59 +0100138 printf(" hbh");
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000139 if (optinfo->flags & IP6T_OPTS_LEN) {
Jan Engelhardt73866352010-12-18 02:04:59 +0100140 printf(" length");
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000141 printf(":%s", optinfo->invflags & IP6T_OPTS_INV_LEN ? "!" : "");
142 printf("%u", optinfo->hdrlen);
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000143 }
Jan Engelhardt73866352010-12-18 02:04:59 +0100144 if (optinfo->flags & IP6T_OPTS_OPTS) printf(" opts");
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100145 print_options(optinfo->optsnr, (uint16_t *)optinfo->opts);
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000146 if (optinfo->invflags & ~IP6T_OPTS_INV_MASK)
Jan Engelhardt73866352010-12-18 02:04:59 +0100147 printf(" Unknown invflags: 0x%X",
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000148 optinfo->invflags & ~IP6T_OPTS_INV_MASK);
149}
150
Jan Engelhardt997045f2007-10-04 16:29:21 +0000151static void hbh_save(const void *ip, const struct xt_entry_match *match)
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000152{
153 const struct ip6t_opts *optinfo = (struct ip6t_opts *)match->data;
154
155 if (optinfo->flags & IP6T_OPTS_LEN) {
Jan Engelhardt73866352010-12-18 02:04:59 +0100156 printf("%s --hbh-len %u",
157 (optinfo->invflags & IP6T_OPTS_INV_LEN) ? " !" : "",
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000158 optinfo->hdrlen);
159 }
160
Jan Engelhardte2f588a2007-10-04 16:30:40 +0000161 if (optinfo->flags & IP6T_OPTS_OPTS)
Jan Engelhardt73866352010-12-18 02:04:59 +0100162 printf(" --hbh-opts");
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100163 print_options(optinfo->optsnr, (uint16_t *)optinfo->opts);
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000164}
165
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200166static struct xtables_match hbh_mt6_reg = {
Harald Welte02aa7332005-02-01 15:38:20 +0000167 .name = "hbh",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200168 .version = XTABLES_VERSION,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100169 .family = NFPROTO_IPV6,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200170 .size = XT_ALIGN(sizeof(struct ip6t_opts)),
171 .userspacesize = XT_ALIGN(sizeof(struct ip6t_opts)),
Jan Engelhardt997045f2007-10-04 16:29:21 +0000172 .help = hbh_help,
Jan Engelhardt997045f2007-10-04 16:29:21 +0000173 .print = hbh_print,
174 .save = hbh_save,
Jan Engelhardt7a969bb2011-03-03 00:40:43 +0100175 .x6_parse = hbh_parse,
176 .x6_options = hbh_opts,
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000177};
178
179void
180_init(void)
181{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200182 xtables_register_match(&hbh_mt6_reg);
András Kis-Szabó2ea56492002-04-29 13:51:37 +0000183}