| András Kis-Szabó | f1f447b | 2002-03-26 12:45:19 +0000 | [diff] [blame] | 1 | /* Shared library add-on to ip6tables to add Fragmentation header support. */ |
| 2 | #include <stdio.h> |
| 3 | #include <netdb.h> |
| 4 | #include <string.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <getopt.h> |
| 7 | #include <errno.h> |
| 8 | #include <ip6tables.h> |
| 9 | #include <linux/netfilter_ipv6/ip6t_frag.h> |
| 10 | |
| 11 | /* Function which prints out usage message. */ |
| Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 12 | static void frag_help(void) |
| András Kis-Szabó | f1f447b | 2002-03-26 12:45:19 +0000 | [diff] [blame] | 13 | { |
| 14 | printf( |
| Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 15 | "frag match options:\n" |
| Jan Engelhardt | 9672792 | 2008-08-13 14:42:41 +0200 | [diff] [blame] | 16 | "[!] --fragid id[:id] match the id (range)\n" |
| 17 | "[!] --fraglen length total length of this header\n" |
| András Kis-Szabó | f1f447b | 2002-03-26 12:45:19 +0000 | [diff] [blame] | 18 | " --fragres check the reserved filed, too\n" |
| András Kis-Szabó | d8a12a8 | 2002-04-24 09:36:30 +0000 | [diff] [blame] | 19 | " --fragfirst matches on the first fragment\n" |
| András Kis-Szabó | f1f447b | 2002-03-26 12:45:19 +0000 | [diff] [blame] | 20 | " [--fragmore|--fraglast] there are more fragments or this\n" |
| Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 21 | " is the last one\n"); |
| András Kis-Szabó | f1f447b | 2002-03-26 12:45:19 +0000 | [diff] [blame] | 22 | } |
| 23 | |
| Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 24 | static const struct option frag_opts[] = { |
| Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 25 | { .name = "fragid", .has_arg = 1, .val = '1' }, |
| 26 | { .name = "fraglen", .has_arg = 1, .val = '2' }, |
| 27 | { .name = "fragres", .has_arg = 0, .val = '3' }, |
| 28 | { .name = "fragfirst", .has_arg = 0, .val = '4' }, |
| 29 | { .name = "fragmore", .has_arg = 0, .val = '5' }, |
| 30 | { .name = "fraglast", .has_arg = 0, .val = '6' }, |
| Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame] | 31 | { .name = NULL } |
| András Kis-Szabó | f1f447b | 2002-03-26 12:45:19 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | static u_int32_t |
| 35 | parse_frag_id(const char *idstr, const char *typestr) |
| 36 | { |
| 37 | unsigned long int id; |
| 38 | char* ep; |
| 39 | |
| Harald Welte | 46a73cf | 2003-09-05 12:53:44 +0000 | [diff] [blame] | 40 | id = strtoul(idstr, &ep, 0); |
| András Kis-Szabó | f1f447b | 2002-03-26 12:45:19 +0000 | [diff] [blame] | 41 | |
| 42 | if ( idstr == ep ) { |
| 43 | exit_error(PARAMETER_PROBLEM, |
| 44 | "FRAG no valid digits in %s `%s'", typestr, idstr); |
| 45 | } |
| 46 | if ( id == ULONG_MAX && errno == ERANGE ) { |
| 47 | exit_error(PARAMETER_PROBLEM, |
| 48 | "%s `%s' specified too big: would overflow", |
| 49 | typestr, idstr); |
| 50 | } |
| 51 | if ( *idstr != '\0' && *ep != '\0' ) { |
| 52 | exit_error(PARAMETER_PROBLEM, |
| 53 | "FRAG error parsing %s `%s'", typestr, idstr); |
| 54 | } |
| 55 | return (u_int32_t) id; |
| 56 | } |
| 57 | |
| 58 | static void |
| 59 | parse_frag_ids(const char *idstring, u_int32_t *ids) |
| 60 | { |
| 61 | char *buffer; |
| 62 | char *cp; |
| 63 | |
| 64 | buffer = strdup(idstring); |
| 65 | if ((cp = strchr(buffer, ':')) == NULL) |
| 66 | ids[0] = ids[1] = parse_frag_id(buffer,"id"); |
| 67 | else { |
| 68 | *cp = '\0'; |
| 69 | cp++; |
| 70 | |
| 71 | ids[0] = buffer[0] ? parse_frag_id(buffer,"id") : 0; |
| 72 | ids[1] = cp[0] ? parse_frag_id(cp,"id") : 0xFFFFFFFF; |
| 73 | } |
| 74 | free(buffer); |
| 75 | } |
| 76 | |
| 77 | /* Initialize the match. */ |
| Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 78 | static void frag_init(struct xt_entry_match *m) |
| András Kis-Szabó | f1f447b | 2002-03-26 12:45:19 +0000 | [diff] [blame] | 79 | { |
| 80 | struct ip6t_frag *fraginfo = (struct ip6t_frag *)m->data; |
| 81 | |
| 82 | fraginfo->ids[0] = 0x0L; |
| 83 | fraginfo->ids[1] = 0xFFFFFFFF; |
| 84 | fraginfo->hdrlen = 0; |
| 85 | fraginfo->flags = 0; |
| 86 | fraginfo->invflags = 0; |
| 87 | } |
| 88 | |
| 89 | /* Function which parses command options; returns true if it |
| 90 | ate an option */ |
| Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 91 | static int frag_parse(int c, char **argv, int invert, unsigned int *flags, |
| 92 | const void *entry, struct xt_entry_match **match) |
| András Kis-Szabó | f1f447b | 2002-03-26 12:45:19 +0000 | [diff] [blame] | 93 | { |
| 94 | struct ip6t_frag *fraginfo = (struct ip6t_frag *)(*match)->data; |
| 95 | |
| 96 | switch (c) { |
| 97 | case '1': |
| 98 | if (*flags & IP6T_FRAG_IDS) |
| 99 | exit_error(PARAMETER_PROBLEM, |
| 100 | "Only one `--fragid' allowed"); |
| 101 | check_inverse(optarg, &invert, &optind, 0); |
| 102 | parse_frag_ids(argv[optind-1], fraginfo->ids); |
| 103 | if (invert) |
| 104 | fraginfo->invflags |= IP6T_FRAG_INV_IDS; |
| 105 | fraginfo->flags |= IP6T_FRAG_IDS; |
| 106 | *flags |= IP6T_FRAG_IDS; |
| 107 | break; |
| 108 | case '2': |
| 109 | if (*flags & IP6T_FRAG_LEN) |
| 110 | exit_error(PARAMETER_PROBLEM, |
| 111 | "Only one `--fraglen' allowed"); |
| 112 | check_inverse(optarg, &invert, &optind, 0); |
| 113 | fraginfo->hdrlen = parse_frag_id(argv[optind-1], "length"); |
| 114 | if (invert) |
| 115 | fraginfo->invflags |= IP6T_FRAG_INV_LEN; |
| 116 | fraginfo->flags |= IP6T_FRAG_LEN; |
| 117 | *flags |= IP6T_FRAG_LEN; |
| 118 | break; |
| 119 | case '3': |
| 120 | if (*flags & IP6T_FRAG_RES) |
| 121 | exit_error(PARAMETER_PROBLEM, |
| 122 | "Only one `--fragres' allowed"); |
| 123 | fraginfo->flags |= IP6T_FRAG_RES; |
| 124 | *flags |= IP6T_FRAG_RES; |
| 125 | break; |
| 126 | case '4': |
| 127 | if (*flags & IP6T_FRAG_FST) |
| 128 | exit_error(PARAMETER_PROBLEM, |
| 129 | "Only one `--fragfirst' allowed"); |
| 130 | fraginfo->flags |= IP6T_FRAG_FST; |
| 131 | *flags |= IP6T_FRAG_FST; |
| 132 | break; |
| 133 | case '5': |
| 134 | if (*flags & (IP6T_FRAG_MF|IP6T_FRAG_NMF)) |
| 135 | exit_error(PARAMETER_PROBLEM, |
| 136 | "Only one `--fragmore' or `--fraglast' allowed"); |
| 137 | fraginfo->flags |= IP6T_FRAG_MF; |
| 138 | *flags |= IP6T_FRAG_MF; |
| 139 | break; |
| 140 | case '6': |
| 141 | if (*flags & (IP6T_FRAG_MF|IP6T_FRAG_NMF)) |
| 142 | exit_error(PARAMETER_PROBLEM, |
| 143 | "Only one `--fragmore' or `--fraglast' allowed"); |
| 144 | fraginfo->flags |= IP6T_FRAG_NMF; |
| 145 | *flags |= IP6T_FRAG_NMF; |
| 146 | break; |
| 147 | default: |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | return 1; |
| 152 | } |
| 153 | |
| András Kis-Szabó | f1f447b | 2002-03-26 12:45:19 +0000 | [diff] [blame] | 154 | static void |
| 155 | print_ids(const char *name, u_int32_t min, u_int32_t max, |
| 156 | int invert) |
| 157 | { |
| 158 | const char *inv = invert ? "!" : ""; |
| 159 | |
| 160 | if (min != 0 || max != 0xFFFFFFFF || invert) { |
| 161 | printf("%s", name); |
| Harald Welte | 46a73cf | 2003-09-05 12:53:44 +0000 | [diff] [blame] | 162 | if (min == max) |
| 163 | printf(":%s%u ", inv, min); |
| 164 | else |
| 165 | printf("s:%s%u:%u ", inv, min, max); |
| András Kis-Szabó | f1f447b | 2002-03-26 12:45:19 +0000 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| András Kis-Szabó | f1f447b | 2002-03-26 12:45:19 +0000 | [diff] [blame] | 169 | /* Prints out the union ip6t_matchinfo. */ |
| Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 170 | static void frag_print(const void *ip, const struct xt_entry_match *match, |
| 171 | int numeric) |
| András Kis-Szabó | f1f447b | 2002-03-26 12:45:19 +0000 | [diff] [blame] | 172 | { |
| 173 | const struct ip6t_frag *frag = (struct ip6t_frag *)match->data; |
| 174 | |
| 175 | printf("frag "); |
| 176 | print_ids("id", frag->ids[0], frag->ids[1], |
| 177 | frag->invflags & IP6T_FRAG_INV_IDS); |
| Harald Welte | 46a73cf | 2003-09-05 12:53:44 +0000 | [diff] [blame] | 178 | |
| András Kis-Szabó | d8a12a8 | 2002-04-24 09:36:30 +0000 | [diff] [blame] | 179 | if (frag->flags & IP6T_FRAG_LEN) { |
| Harald Welte | 46a73cf | 2003-09-05 12:53:44 +0000 | [diff] [blame] | 180 | printf("length:%s%u ", |
| 181 | frag->invflags & IP6T_FRAG_INV_LEN ? "!" : "", |
| 182 | frag->hdrlen); |
| András Kis-Szabó | d8a12a8 | 2002-04-24 09:36:30 +0000 | [diff] [blame] | 183 | } |
| Harald Welte | 46a73cf | 2003-09-05 12:53:44 +0000 | [diff] [blame] | 184 | |
| 185 | if (frag->flags & IP6T_FRAG_RES) |
| 186 | printf("reserved "); |
| 187 | |
| 188 | if (frag->flags & IP6T_FRAG_FST) |
| 189 | printf("first "); |
| 190 | |
| 191 | if (frag->flags & IP6T_FRAG_MF) |
| 192 | printf("more "); |
| 193 | |
| 194 | if (frag->flags & IP6T_FRAG_NMF) |
| 195 | printf("last "); |
| 196 | |
| András Kis-Szabó | f1f447b | 2002-03-26 12:45:19 +0000 | [diff] [blame] | 197 | if (frag->invflags & ~IP6T_FRAG_INV_MASK) |
| 198 | printf("Unknown invflags: 0x%X ", |
| 199 | frag->invflags & ~IP6T_FRAG_INV_MASK); |
| 200 | } |
| 201 | |
| 202 | /* Saves the union ip6t_matchinfo in parsable form to stdout. */ |
| Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 203 | static void frag_save(const void *ip, const struct xt_entry_match *match) |
| András Kis-Szabó | f1f447b | 2002-03-26 12:45:19 +0000 | [diff] [blame] | 204 | { |
| 205 | const struct ip6t_frag *fraginfo = (struct ip6t_frag *)match->data; |
| 206 | |
| 207 | if (!(fraginfo->ids[0] == 0 |
| 208 | && fraginfo->ids[1] == 0xFFFFFFFF)) { |
| 209 | printf("--fragid %s", |
| 210 | (fraginfo->invflags & IP6T_FRAG_INV_IDS) ? "! " : ""); |
| 211 | if (fraginfo->ids[0] |
| 212 | != fraginfo->ids[1]) |
| 213 | printf("%u:%u ", |
| 214 | fraginfo->ids[0], |
| 215 | fraginfo->ids[1]); |
| 216 | else |
| 217 | printf("%u ", |
| 218 | fraginfo->ids[0]); |
| 219 | } |
| 220 | |
| András Kis-Szabó | d8a12a8 | 2002-04-24 09:36:30 +0000 | [diff] [blame] | 221 | if (fraginfo->flags & IP6T_FRAG_LEN) { |
| András Kis-Szabó | f1f447b | 2002-03-26 12:45:19 +0000 | [diff] [blame] | 222 | printf("--fraglen %s%u ", |
| 223 | (fraginfo->invflags & IP6T_FRAG_INV_LEN) ? "! " : "", |
| 224 | fraginfo->hdrlen); |
| 225 | } |
| 226 | |
| Harald Welte | 46a73cf | 2003-09-05 12:53:44 +0000 | [diff] [blame] | 227 | if (fraginfo->flags & IP6T_FRAG_RES) |
| 228 | printf("--fragres "); |
| András Kis-Szabó | f1f447b | 2002-03-26 12:45:19 +0000 | [diff] [blame] | 229 | |
| Harald Welte | 46a73cf | 2003-09-05 12:53:44 +0000 | [diff] [blame] | 230 | if (fraginfo->flags & IP6T_FRAG_FST) |
| 231 | printf("--fragfirst "); |
| 232 | |
| 233 | if (fraginfo->flags & IP6T_FRAG_MF) |
| 234 | printf("--fragmore "); |
| 235 | |
| 236 | if (fraginfo->flags & IP6T_FRAG_NMF) |
| 237 | printf("--fraglast "); |
| András Kis-Szabó | f1f447b | 2002-03-26 12:45:19 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 240 | static struct xtables_match frag_mt6_reg = { |
| Harald Welte | 46a73cf | 2003-09-05 12:53:44 +0000 | [diff] [blame] | 241 | .name = "frag", |
| Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 242 | .version = XTABLES_VERSION, |
| 243 | .family = PF_INET6, |
| 244 | .size = XT_ALIGN(sizeof(struct ip6t_frag)), |
| 245 | .userspacesize = XT_ALIGN(sizeof(struct ip6t_frag)), |
| Jan Engelhardt | 997045f | 2007-10-04 16:29:21 +0000 | [diff] [blame] | 246 | .help = frag_help, |
| 247 | .init = frag_init, |
| 248 | .parse = frag_parse, |
| 249 | .print = frag_print, |
| 250 | .save = frag_save, |
| 251 | .extra_opts = frag_opts, |
| András Kis-Szabó | f1f447b | 2002-03-26 12:45:19 +0000 | [diff] [blame] | 252 | }; |
| 253 | |
| 254 | void |
| 255 | _init(void) |
| 256 | { |
| Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 257 | xtables_register_match(&frag_mt6_reg); |
| András Kis-Szabó | f1f447b | 2002-03-26 12:45:19 +0000 | [diff] [blame] | 258 | } |