Jan Engelhardt | ddac6c5 | 2008-09-01 14:22:19 +0200 | [diff] [blame] | 1 | /* Shared library add-on to ip6tables to add Dst header support. */ |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 2 | #include <stdio.h> |
| 3 | #include <netdb.h> |
| 4 | #include <string.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <getopt.h> |
| 7 | #include <errno.h> |
Jan Engelhardt | 5d9678a | 2008-11-20 10:15:35 +0100 | [diff] [blame] | 8 | #include <xtables.h> |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 9 | #include <linux/netfilter_ipv6/ip6t_opts.h> |
| 10 | #include <sys/types.h> |
| 11 | #include <sys/socket.h> |
| 12 | #include <arpa/inet.h> |
Harald Welte | ad8d1ab | 2003-09-04 21:55:10 +0000 | [diff] [blame] | 13 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 14 | static void dst_help(void) |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 15 | { |
| 16 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 17 | "dst match options:\n" |
Jan Engelhardt | 9672792 | 2008-08-13 14:42:41 +0200 | [diff] [blame] | 18 | "[!] --dst-len length total length of this header\n" |
Jan Engelhardt | e2f588a | 2007-10-04 16:30:40 +0000 | [diff] [blame] | 19 | " --dst-opts TYPE[:LEN][,TYPE[:LEN]...]\n" |
| 20 | " Options and its length (list, max: %d)\n", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 21 | IP6T_OPTS_OPTSNR); |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 22 | } |
| 23 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 24 | static const struct option dst_opts[] = { |
Jan Engelhardt | e2f588a | 2007-10-04 16:30:40 +0000 | [diff] [blame] | 25 | { .name = "dst-len", .has_arg = 1, .val = '1' }, |
| 26 | { .name = "dst-opts", .has_arg = 1, .val = '2' }, |
| 27 | { .name = "dst-not-strict", .has_arg = 1, .val = '3' }, |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame] | 28 | { .name = NULL } |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 29 | }; |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 30 | |
| 31 | static u_int32_t |
| 32 | parse_opts_num(const char *idstr, const char *typestr) |
| 33 | { |
| 34 | unsigned long int id; |
| 35 | char* ep; |
| 36 | |
Stephane Ouellette | 703575d | 2003-08-23 18:41:47 +0000 | [diff] [blame] | 37 | id = strtoul(idstr, &ep, 0); |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 38 | |
| 39 | if ( idstr == ep ) { |
| 40 | exit_error(PARAMETER_PROBLEM, |
Jan Engelhardt | e2f588a | 2007-10-04 16:30:40 +0000 | [diff] [blame] | 41 | "dst: no valid digits in %s `%s'", typestr, idstr); |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 42 | } |
| 43 | if ( id == ULONG_MAX && errno == ERANGE ) { |
| 44 | exit_error(PARAMETER_PROBLEM, |
| 45 | "%s `%s' specified too big: would overflow", |
| 46 | typestr, idstr); |
Harald Welte | ad8d1ab | 2003-09-04 21:55:10 +0000 | [diff] [blame] | 47 | } |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 48 | if ( *idstr != '\0' && *ep != '\0' ) { |
| 49 | exit_error(PARAMETER_PROBLEM, |
Jan Engelhardt | e2f588a | 2007-10-04 16:30:40 +0000 | [diff] [blame] | 50 | "dst: error parsing %s `%s'", typestr, idstr); |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 51 | } |
Jan Engelhardt | 213e185 | 2009-01-27 17:24:34 +0100 | [diff] [blame] | 52 | return id; |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static int |
| 56 | parse_options(const char *optsstr, u_int16_t *opts) |
| 57 | { |
| 58 | char *buffer, *cp, *next, *range; |
| 59 | unsigned int i; |
| 60 | |
| 61 | buffer = strdup(optsstr); |
Harald Welte | ad8d1ab | 2003-09-04 21:55:10 +0000 | [diff] [blame] | 62 | if (!buffer) |
| 63 | exit_error(OTHER_PROBLEM, "strdup failed"); |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 64 | |
Harald Welte | ad8d1ab | 2003-09-04 21:55:10 +0000 | [diff] [blame] | 65 | for (cp = buffer, i = 0; cp && i < IP6T_OPTS_OPTSNR; cp = next, i++) |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 66 | { |
Harald Welte | ad8d1ab | 2003-09-04 21:55:10 +0000 | [diff] [blame] | 67 | next = strchr(cp, ','); |
| 68 | |
| 69 | if (next) |
| 70 | *next++='\0'; |
| 71 | |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 72 | range = strchr(cp, ':'); |
Harald Welte | ad8d1ab | 2003-09-04 21:55:10 +0000 | [diff] [blame] | 73 | |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 74 | if (range) { |
| 75 | if (i == IP6T_OPTS_OPTSNR-1) |
| 76 | exit_error(PARAMETER_PROBLEM, |
| 77 | "too many ports specified"); |
| 78 | *range++ = '\0'; |
| 79 | } |
Harald Welte | ad8d1ab | 2003-09-04 21:55:10 +0000 | [diff] [blame] | 80 | |
Jan Engelhardt | 213e185 | 2009-01-27 17:24:34 +0100 | [diff] [blame] | 81 | opts[i] = (parse_opts_num(cp, "opt") & 0xFF) << 8; |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 82 | if (range) { |
| 83 | if (opts[i] == 0) |
Harald Welte | ad8d1ab | 2003-09-04 21:55:10 +0000 | [diff] [blame] | 84 | exit_error(PARAMETER_PROBLEM, |
| 85 | "PAD0 hasn't got length"); |
Jan Engelhardt | 213e185 | 2009-01-27 17:24:34 +0100 | [diff] [blame] | 86 | opts[i] |= parse_opts_num(range, "length") & 0xFF; |
Harald Welte | ad8d1ab | 2003-09-04 21:55:10 +0000 | [diff] [blame] | 87 | } else |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 88 | opts[i] |= (0x00FF); |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 89 | |
Harald Welte | ad8d1ab | 2003-09-04 21:55:10 +0000 | [diff] [blame] | 90 | #ifdef DEBUG |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 91 | printf("opts str: %s %s\n", cp, range); |
| 92 | printf("opts opt: %04X\n", opts[i]); |
| 93 | #endif |
| 94 | } |
Harald Welte | ad8d1ab | 2003-09-04 21:55:10 +0000 | [diff] [blame] | 95 | |
| 96 | if (cp) |
| 97 | exit_error(PARAMETER_PROBLEM, "too many addresses specified"); |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 98 | |
| 99 | free(buffer); |
| 100 | |
Harald Welte | ad8d1ab | 2003-09-04 21:55:10 +0000 | [diff] [blame] | 101 | #ifdef DEBUG |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 102 | printf("addr nr: %d\n", i); |
| 103 | #endif |
| 104 | |
| 105 | return i; |
| 106 | } |
| 107 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 108 | static void dst_init(struct xt_entry_match *m) |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 109 | { |
| 110 | struct ip6t_opts *optinfo = (struct ip6t_opts *)m->data; |
| 111 | |
| 112 | optinfo->hdrlen = 0; |
| 113 | optinfo->flags = 0; |
| 114 | optinfo->invflags = 0; |
| 115 | optinfo->optsnr = 0; |
| 116 | } |
| 117 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 118 | static int dst_parse(int c, char **argv, int invert, unsigned int *flags, |
| 119 | const void *entry, struct xt_entry_match **match) |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 120 | { |
| 121 | struct ip6t_opts *optinfo = (struct ip6t_opts *)(*match)->data; |
| 122 | |
| 123 | switch (c) { |
| 124 | case '1': |
| 125 | if (*flags & IP6T_OPTS_LEN) |
| 126 | exit_error(PARAMETER_PROBLEM, |
Jan Engelhardt | e2f588a | 2007-10-04 16:30:40 +0000 | [diff] [blame] | 127 | "Only one `--dst-len' allowed"); |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 128 | check_inverse(optarg, &invert, &optind, 0); |
| 129 | optinfo->hdrlen = parse_opts_num(argv[optind-1], "length"); |
| 130 | if (invert) |
| 131 | optinfo->invflags |= IP6T_OPTS_INV_LEN; |
| 132 | optinfo->flags |= IP6T_OPTS_LEN; |
| 133 | *flags |= IP6T_OPTS_LEN; |
| 134 | break; |
| 135 | case '2': |
| 136 | if (*flags & IP6T_OPTS_OPTS) |
| 137 | exit_error(PARAMETER_PROBLEM, |
Jan Engelhardt | e2f588a | 2007-10-04 16:30:40 +0000 | [diff] [blame] | 138 | "Only one `--dst-opts' allowed"); |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 139 | check_inverse(optarg, &invert, &optind, 0); |
| 140 | if (invert) |
| 141 | exit_error(PARAMETER_PROBLEM, |
Jan Engelhardt | e2f588a | 2007-10-04 16:30:40 +0000 | [diff] [blame] | 142 | " '!' not allowed with `--dst-opts'"); |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 143 | optinfo->optsnr = parse_options(argv[optind-1], optinfo->opts); |
| 144 | optinfo->flags |= IP6T_OPTS_OPTS; |
| 145 | *flags |= IP6T_OPTS_OPTS; |
| 146 | break; |
| 147 | case '3': |
| 148 | if (*flags & IP6T_OPTS_NSTRICT) |
| 149 | exit_error(PARAMETER_PROBLEM, |
Jan Engelhardt | e2f588a | 2007-10-04 16:30:40 +0000 | [diff] [blame] | 150 | "Only one `--dst-not-strict' allowed"); |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 151 | if ( !(*flags & IP6T_OPTS_OPTS) ) |
| 152 | exit_error(PARAMETER_PROBLEM, |
Jan Engelhardt | e2f588a | 2007-10-04 16:30:40 +0000 | [diff] [blame] | 153 | "`--dst-opts ...' required before " |
| 154 | "`--dst-not-strict'"); |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 155 | optinfo->flags |= IP6T_OPTS_NSTRICT; |
| 156 | *flags |= IP6T_OPTS_NSTRICT; |
| 157 | break; |
| 158 | default: |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | return 1; |
| 163 | } |
| 164 | |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 165 | static void |
Jan Engelhardt | 7a236f4 | 2008-03-03 12:30:41 +0100 | [diff] [blame] | 166 | print_options(unsigned int optsnr, u_int16_t *optsp) |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 167 | { |
| 168 | unsigned int i; |
| 169 | |
Harald Welte | ad8d1ab | 2003-09-04 21:55:10 +0000 | [diff] [blame] | 170 | for(i = 0; i < optsnr; i++) { |
| 171 | printf("%d", (optsp[i] & 0xFF00) >> 8); |
| 172 | |
| 173 | if ((optsp[i] & 0x00FF) != 0x00FF) |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 174 | printf(":%d", (optsp[i] & 0x00FF)); |
Harald Welte | ad8d1ab | 2003-09-04 21:55:10 +0000 | [diff] [blame] | 175 | |
| 176 | printf("%c", (i != optsnr - 1) ? ',' : ' '); |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 180 | static void dst_print(const void *ip, const struct xt_entry_match *match, |
| 181 | int numeric) |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 182 | { |
| 183 | const struct ip6t_opts *optinfo = (struct ip6t_opts *)match->data; |
| 184 | |
Jan Engelhardt | e2f588a | 2007-10-04 16:30:40 +0000 | [diff] [blame] | 185 | printf("dst "); |
Stephane Ouellette | 703575d | 2003-08-23 18:41:47 +0000 | [diff] [blame] | 186 | if (optinfo->flags & IP6T_OPTS_LEN) |
| 187 | printf("length:%s%u ", |
| 188 | optinfo->invflags & IP6T_OPTS_INV_LEN ? "!" : "", |
| 189 | optinfo->hdrlen); |
| 190 | |
| 191 | if (optinfo->flags & IP6T_OPTS_OPTS) |
| 192 | printf("opts "); |
| 193 | |
Fabrice MARIE | ae31bb6 | 2002-06-14 07:38:16 +0000 | [diff] [blame] | 194 | print_options(optinfo->optsnr, (u_int16_t *)optinfo->opts); |
Stephane Ouellette | 703575d | 2003-08-23 18:41:47 +0000 | [diff] [blame] | 195 | |
| 196 | if (optinfo->flags & IP6T_OPTS_NSTRICT) |
| 197 | printf("not-strict "); |
| 198 | |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 199 | if (optinfo->invflags & ~IP6T_OPTS_INV_MASK) |
| 200 | printf("Unknown invflags: 0x%X ", |
| 201 | optinfo->invflags & ~IP6T_OPTS_INV_MASK); |
| 202 | } |
| 203 | |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 204 | static void dst_save(const void *ip, const struct xt_entry_match *match) |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 205 | { |
| 206 | const struct ip6t_opts *optinfo = (struct ip6t_opts *)match->data; |
| 207 | |
| 208 | if (optinfo->flags & IP6T_OPTS_LEN) { |
Jan Engelhardt | cea9f71 | 2008-12-09 15:06:20 +0100 | [diff] [blame] | 209 | printf("%s--dst-len %u ", |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 210 | (optinfo->invflags & IP6T_OPTS_INV_LEN) ? "! " : "", |
| 211 | optinfo->hdrlen); |
| 212 | } |
| 213 | |
Stephane Ouellette | 703575d | 2003-08-23 18:41:47 +0000 | [diff] [blame] | 214 | if (optinfo->flags & IP6T_OPTS_OPTS) |
Jan Engelhardt | e2f588a | 2007-10-04 16:30:40 +0000 | [diff] [blame] | 215 | printf("--dst-opts "); |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 216 | |
Stephane Ouellette | 703575d | 2003-08-23 18:41:47 +0000 | [diff] [blame] | 217 | print_options(optinfo->optsnr, (u_int16_t *)optinfo->opts); |
| 218 | |
| 219 | if (optinfo->flags & IP6T_OPTS_NSTRICT) |
Jan Engelhardt | e2f588a | 2007-10-04 16:30:40 +0000 | [diff] [blame] | 220 | printf("--dst-not-strict "); |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 223 | static struct xtables_match dst_mt6_reg = { |
Jan Engelhardt | e2f588a | 2007-10-04 16:30:40 +0000 | [diff] [blame] | 224 | .name = "dst", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 225 | .version = XTABLES_VERSION, |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 226 | .family = NFPROTO_IPV6, |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 227 | .size = XT_ALIGN(sizeof(struct ip6t_opts)), |
| 228 | .userspacesize = XT_ALIGN(sizeof(struct ip6t_opts)), |
Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 229 | .help = dst_help, |
| 230 | .init = dst_init, |
| 231 | .parse = dst_parse, |
| 232 | .print = dst_print, |
| 233 | .save = dst_save, |
| 234 | .extra_opts = dst_opts, |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | void |
| 238 | _init(void) |
| 239 | { |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 240 | xtables_register_match(&dst_mt6_reg); |
András Kis-Szabó | 2ea5649 | 2002-04-29 13:51:37 +0000 | [diff] [blame] | 241 | } |