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