Yasuyuki KOZAKAI | 5208806 | 2007-07-24 05:44:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2000-2006 by the netfilter coreteam <coreteam@netfilter.org>: |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 17 | */ |
| 18 | |
Yasuyuki KOZAKAI | 3dfa448 | 2007-07-24 05:45:33 +0000 | [diff] [blame] | 19 | #include <errno.h> |
Yasuyuki KOZAKAI | 0b82e8e | 2007-07-24 05:47:40 +0000 | [diff] [blame] | 20 | #include <fcntl.h> |
Yasuyuki KOZAKAI | 04f8c54 | 2007-07-24 05:53:48 +0000 | [diff] [blame] | 21 | #include <netdb.h> |
Jan Engelhardt | aafd269 | 2008-01-20 13:19:40 +0000 | [diff] [blame] | 22 | #include <stdarg.h> |
Jan Engelhardt | cd9e7aa | 2008-01-20 13:18:54 +0000 | [diff] [blame] | 23 | #include <stdbool.h> |
Yasuyuki KOZAKAI | 3dfa448 | 2007-07-24 05:45:33 +0000 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
Yasuyuki KOZAKAI | 0b82e8e | 2007-07-24 05:47:40 +0000 | [diff] [blame] | 26 | #include <string.h> |
| 27 | #include <unistd.h> |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 28 | #include <sys/socket.h> |
Yasuyuki KOZAKAI | 0b82e8e | 2007-07-24 05:47:40 +0000 | [diff] [blame] | 29 | #include <sys/stat.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/wait.h> |
Jan Engelhardt | 08b1616 | 2008-01-20 13:36:08 +0000 | [diff] [blame] | 32 | #include <arpa/inet.h> |
Yasuyuki KOZAKAI | 3dfa448 | 2007-07-24 05:45:33 +0000 | [diff] [blame] | 33 | |
Yasuyuki KOZAKAI | 5208806 | 2007-07-24 05:44:11 +0000 | [diff] [blame] | 34 | #include <xtables.h> |
Jan Engelhardt | ef18e81 | 2008-08-04 12:47:48 +0200 | [diff] [blame] | 35 | #include <libiptc/libxtc.h> |
Yasuyuki KOZAKAI | 3dfa448 | 2007-07-24 05:45:33 +0000 | [diff] [blame] | 36 | |
Mike Frysinger | 5a26b5f | 2007-12-19 14:51:17 +0000 | [diff] [blame] | 37 | #ifndef NO_SHARED_LIBS |
| 38 | #include <dlfcn.h> |
| 39 | #endif |
| 40 | |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 41 | #define NPROTO 255 |
| 42 | |
Yasuyuki KOZAKAI | 0b82e8e | 2007-07-24 05:47:40 +0000 | [diff] [blame] | 43 | #ifndef PROC_SYS_MODPROBE |
| 44 | #define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe" |
| 45 | #endif |
| 46 | |
Jan Engelhardt | 39bf9c8 | 2009-01-27 15:59:06 +0100 | [diff] [blame] | 47 | /* Search path for Xtables .so files */ |
| 48 | static const char *xtables_libdir; |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 49 | |
Yasuyuki KOZAKAI | 0b82e8e | 2007-07-24 05:47:40 +0000 | [diff] [blame] | 50 | /* the path to command to load kernel module */ |
Jan Engelhardt | c021c3c | 2009-01-27 15:10:05 +0100 | [diff] [blame] | 51 | const char *xtables_modprobe_program; |
Yasuyuki KOZAKAI | 0b82e8e | 2007-07-24 05:47:40 +0000 | [diff] [blame] | 52 | |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 53 | /* Keeping track of external matches and targets: linked lists. */ |
| 54 | struct xtables_match *xtables_matches; |
| 55 | struct xtables_target *xtables_targets; |
| 56 | |
Jan Engelhardt | 39bf9c8 | 2009-01-27 15:59:06 +0100 | [diff] [blame] | 57 | void xtables_init(void) |
| 58 | { |
| 59 | xtables_libdir = getenv("XTABLES_LIBDIR"); |
| 60 | if (xtables_libdir != NULL) |
| 61 | return; |
| 62 | xtables_libdir = getenv("IPTABLES_LIB_DIR"); |
| 63 | if (xtables_libdir != NULL) { |
| 64 | fprintf(stderr, "IPTABLES_LIB_DIR is deprecated, " |
| 65 | "use XTABLES_LIBDIR.\n"); |
| 66 | return; |
| 67 | } |
| 68 | xtables_libdir = XTABLES_LIBDIR; |
| 69 | } |
| 70 | |
Jan Engelhardt | 630ef48 | 2009-01-27 14:58:41 +0100 | [diff] [blame] | 71 | /** |
| 72 | * xtables_*alloc - wrappers that exit on failure |
| 73 | */ |
| 74 | void *xtables_calloc(size_t count, size_t size) |
Yasuyuki KOZAKAI | 3dfa448 | 2007-07-24 05:45:33 +0000 | [diff] [blame] | 75 | { |
| 76 | void *p; |
| 77 | |
| 78 | if ((p = calloc(count, size)) == NULL) { |
| 79 | perror("ip[6]tables: calloc failed"); |
| 80 | exit(1); |
| 81 | } |
| 82 | |
| 83 | return p; |
| 84 | } |
| 85 | |
Jan Engelhardt | 630ef48 | 2009-01-27 14:58:41 +0100 | [diff] [blame] | 86 | void *xtables_malloc(size_t size) |
Yasuyuki KOZAKAI | 3dfa448 | 2007-07-24 05:45:33 +0000 | [diff] [blame] | 87 | { |
| 88 | void *p; |
| 89 | |
| 90 | if ((p = malloc(size)) == NULL) { |
| 91 | perror("ip[6]tables: malloc failed"); |
| 92 | exit(1); |
| 93 | } |
| 94 | |
| 95 | return p; |
| 96 | } |
Yasuyuki KOZAKAI | 0b82e8e | 2007-07-24 05:47:40 +0000 | [diff] [blame] | 97 | |
| 98 | static char *get_modprobe(void) |
| 99 | { |
| 100 | int procfile; |
| 101 | char *ret; |
| 102 | |
| 103 | #define PROCFILE_BUFSIZ 1024 |
| 104 | procfile = open(PROC_SYS_MODPROBE, O_RDONLY); |
| 105 | if (procfile < 0) |
| 106 | return NULL; |
| 107 | |
| 108 | ret = (char *) malloc(PROCFILE_BUFSIZ); |
| 109 | if (ret) { |
| 110 | memset(ret, 0, PROCFILE_BUFSIZ); |
| 111 | switch (read(procfile, ret, PROCFILE_BUFSIZ)) { |
| 112 | case -1: goto fail; |
| 113 | case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */ |
| 114 | } |
| 115 | if (ret[strlen(ret)-1]=='\n') |
| 116 | ret[strlen(ret)-1]=0; |
| 117 | close(procfile); |
| 118 | return ret; |
| 119 | } |
| 120 | fail: |
| 121 | free(ret); |
| 122 | close(procfile); |
| 123 | return NULL; |
| 124 | } |
| 125 | |
Jan Engelhardt | c021c3c | 2009-01-27 15:10:05 +0100 | [diff] [blame] | 126 | int xtables_insmod(const char *modname, const char *modprobe, bool quiet) |
Yasuyuki KOZAKAI | 0b82e8e | 2007-07-24 05:47:40 +0000 | [diff] [blame] | 127 | { |
| 128 | char *buf = NULL; |
| 129 | char *argv[4]; |
| 130 | int status; |
| 131 | |
| 132 | /* If they don't explicitly set it, read out of kernel */ |
| 133 | if (!modprobe) { |
| 134 | buf = get_modprobe(); |
| 135 | if (!buf) |
| 136 | return -1; |
| 137 | modprobe = buf; |
| 138 | } |
| 139 | |
| 140 | switch (fork()) { |
| 141 | case 0: |
| 142 | argv[0] = (char *)modprobe; |
| 143 | argv[1] = (char *)modname; |
| 144 | if (quiet) { |
| 145 | argv[2] = "-q"; |
| 146 | argv[3] = NULL; |
| 147 | } else { |
| 148 | argv[2] = NULL; |
| 149 | argv[3] = NULL; |
| 150 | } |
| 151 | execv(argv[0], argv); |
| 152 | |
| 153 | /* not usually reached */ |
| 154 | exit(1); |
| 155 | case -1: |
| 156 | return -1; |
| 157 | |
| 158 | default: /* parent */ |
| 159 | wait(&status); |
| 160 | } |
| 161 | |
| 162 | free(buf); |
| 163 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) |
| 164 | return 0; |
| 165 | return -1; |
| 166 | } |
| 167 | |
Jan Engelhardt | c021c3c | 2009-01-27 15:10:05 +0100 | [diff] [blame] | 168 | int xtables_load_ko(const char *modprobe, bool quiet) |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 169 | { |
Jan Engelhardt | c021c3c | 2009-01-27 15:10:05 +0100 | [diff] [blame] | 170 | static bool loaded = false; |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 171 | static int ret = -1; |
| 172 | |
| 173 | if (!loaded) { |
| 174 | ret = xtables_insmod(afinfo.kmod, modprobe, quiet); |
| 175 | loaded = (ret == 0); |
| 176 | } |
| 177 | |
| 178 | return ret; |
| 179 | } |
| 180 | |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame^] | 181 | /** |
| 182 | * xtables_strtou{i,l} - string to number conversion |
| 183 | * @s: input string |
| 184 | * @end: like strtoul's "end" pointer |
| 185 | * @value: pointer for result |
| 186 | * @min: minimum accepted value |
| 187 | * @max: maximum accepted value |
Jan Engelhardt | cd9e7aa | 2008-01-20 13:18:54 +0000 | [diff] [blame] | 188 | * |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame^] | 189 | * If @end is NULL, we assume the caller wants a "strict strtoul", and hence |
| 190 | * "15a" is rejected. |
| 191 | * In either case, the value obtained is compared for min-max compliance. |
| 192 | * Base is always 0, i.e. autodetect depending on @s. |
| 193 | * |
| 194 | * Returns true/false whether number was accepted. On failure, *value has |
| 195 | * undefined contents. |
Jan Engelhardt | cd9e7aa | 2008-01-20 13:18:54 +0000 | [diff] [blame] | 196 | */ |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame^] | 197 | bool xtables_strtoul(const char *s, char **end, unsigned long *value, |
| 198 | unsigned long min, unsigned long max) |
Jan Engelhardt | cd9e7aa | 2008-01-20 13:18:54 +0000 | [diff] [blame] | 199 | { |
| 200 | unsigned long v; |
| 201 | char *my_end; |
| 202 | |
| 203 | errno = 0; |
| 204 | v = strtoul(s, &my_end, 0); |
| 205 | |
| 206 | if (my_end == s) |
| 207 | return false; |
| 208 | if (end != NULL) |
| 209 | *end = my_end; |
| 210 | |
| 211 | if (errno != ERANGE && min <= v && (max == 0 || v <= max)) { |
| 212 | if (value != NULL) |
| 213 | *value = v; |
| 214 | if (end == NULL) |
| 215 | return *my_end == '\0'; |
| 216 | return true; |
| 217 | } |
| 218 | |
| 219 | return false; |
| 220 | } |
| 221 | |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame^] | 222 | bool xtables_strtoui(const char *s, char **end, unsigned int *value, |
| 223 | unsigned int min, unsigned int max) |
Jan Engelhardt | cd9e7aa | 2008-01-20 13:18:54 +0000 | [diff] [blame] | 224 | { |
| 225 | unsigned long v; |
| 226 | bool ret; |
| 227 | |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame^] | 228 | ret = xtables_strtoul(s, end, &v, min, max); |
Jan Engelhardt | cd9e7aa | 2008-01-20 13:18:54 +0000 | [diff] [blame] | 229 | if (value != NULL) |
| 230 | *value = v; |
| 231 | return ret; |
| 232 | } |
| 233 | |
Yasuyuki KOZAKAI | 04f8c54 | 2007-07-24 05:53:48 +0000 | [diff] [blame] | 234 | int service_to_port(const char *name, const char *proto) |
| 235 | { |
| 236 | struct servent *service; |
| 237 | |
| 238 | if ((service = getservbyname(name, proto)) != NULL) |
| 239 | return ntohs((unsigned short) service->s_port); |
| 240 | |
| 241 | return -1; |
| 242 | } |
| 243 | |
| 244 | u_int16_t parse_port(const char *port, const char *proto) |
| 245 | { |
Jan Engelhardt | 7a236f4 | 2008-03-03 12:30:41 +0100 | [diff] [blame] | 246 | unsigned int portnum; |
Yasuyuki KOZAKAI | 04f8c54 | 2007-07-24 05:53:48 +0000 | [diff] [blame] | 247 | |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame^] | 248 | if (xtables_strtoui(port, NULL, &portnum, 0, UINT16_MAX) || |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame] | 249 | (portnum = service_to_port(port, proto)) != (unsigned)-1) |
Jan Engelhardt | 213e185 | 2009-01-27 17:24:34 +0100 | [diff] [blame] | 250 | return portnum; |
Yasuyuki KOZAKAI | 04f8c54 | 2007-07-24 05:53:48 +0000 | [diff] [blame] | 251 | |
| 252 | exit_error(PARAMETER_PROBLEM, |
| 253 | "invalid port/service `%s' specified", port); |
| 254 | } |
| 255 | |
| 256 | void parse_interface(const char *arg, char *vianame, unsigned char *mask) |
| 257 | { |
| 258 | int vialen = strlen(arg); |
| 259 | unsigned int i; |
| 260 | |
| 261 | memset(mask, 0, IFNAMSIZ); |
| 262 | memset(vianame, 0, IFNAMSIZ); |
| 263 | |
| 264 | if (vialen + 1 > IFNAMSIZ) |
| 265 | exit_error(PARAMETER_PROBLEM, |
| 266 | "interface name `%s' must be shorter than IFNAMSIZ" |
| 267 | " (%i)", arg, IFNAMSIZ-1); |
| 268 | |
| 269 | strcpy(vianame, arg); |
| 270 | if ((vialen == 0) || (vialen == 1 && vianame[0] == '+')) |
| 271 | memset(mask, 0, IFNAMSIZ); |
| 272 | else if (vianame[vialen - 1] == '+') { |
| 273 | memset(mask, 0xFF, vialen - 1); |
| 274 | memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1); |
| 275 | /* Don't remove `+' here! -HW */ |
| 276 | } else { |
| 277 | /* Include nul-terminator in match */ |
| 278 | memset(mask, 0xFF, vialen + 1); |
| 279 | memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1); |
| 280 | for (i = 0; vianame[i]; i++) { |
| 281 | if (vianame[i] == ':' || |
| 282 | vianame[i] == '!' || |
| 283 | vianame[i] == '*') { |
Max Kellermann | aae4f82 | 2007-10-17 16:36:49 +0000 | [diff] [blame] | 284 | fprintf(stderr, |
| 285 | "Warning: weird character in interface" |
| 286 | " `%s' (No aliases, :, ! or *).\n", |
| 287 | vianame); |
Yasuyuki KOZAKAI | 04f8c54 | 2007-07-24 05:53:48 +0000 | [diff] [blame] | 288 | break; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
Jan Engelhardt | cb25af8 | 2008-04-14 18:37:57 +0200 | [diff] [blame] | 294 | #ifndef NO_SHARED_LIBS |
Jan Engelhardt | 21b41ee | 2008-02-11 01:02:00 +0100 | [diff] [blame] | 295 | static void *load_extension(const char *search_path, const char *prefix, |
| 296 | const char *name, bool is_target) |
| 297 | { |
| 298 | const char *dir = search_path, *next; |
| 299 | void *ptr = NULL; |
| 300 | struct stat sb; |
| 301 | char path[256]; |
| 302 | |
| 303 | do { |
| 304 | next = strchr(dir, ':'); |
| 305 | if (next == NULL) |
| 306 | next = dir + strlen(dir); |
| 307 | snprintf(path, sizeof(path), "%.*s/libxt_%s.so", |
Jan Engelhardt | 2c0a0c9 | 2008-04-14 15:58:17 +0200 | [diff] [blame] | 308 | (unsigned int)(next - dir), dir, name); |
Jan Engelhardt | 21b41ee | 2008-02-11 01:02:00 +0100 | [diff] [blame] | 309 | |
| 310 | if (dlopen(path, RTLD_NOW) != NULL) { |
| 311 | /* Found library. If it didn't register itself, |
| 312 | maybe they specified target as match. */ |
| 313 | if (is_target) |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 314 | ptr = xtables_find_target(name, XTF_DONT_LOAD); |
Jan Engelhardt | 21b41ee | 2008-02-11 01:02:00 +0100 | [diff] [blame] | 315 | else |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 316 | ptr = xtables_find_match(name, |
| 317 | XTF_DONT_LOAD, NULL); |
Jan Engelhardt | 21b41ee | 2008-02-11 01:02:00 +0100 | [diff] [blame] | 318 | } else if (stat(path, &sb) == 0) { |
| 319 | fprintf(stderr, "%s: %s\n", path, dlerror()); |
| 320 | } |
| 321 | |
| 322 | if (ptr != NULL) |
| 323 | return ptr; |
| 324 | |
| 325 | snprintf(path, sizeof(path), "%.*s/%s%s.so", |
Jan Engelhardt | 2c0a0c9 | 2008-04-14 15:58:17 +0200 | [diff] [blame] | 326 | (unsigned int)(next - dir), dir, prefix, name); |
Jan Engelhardt | 21b41ee | 2008-02-11 01:02:00 +0100 | [diff] [blame] | 327 | if (dlopen(path, RTLD_NOW) != NULL) { |
| 328 | if (is_target) |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 329 | ptr = xtables_find_target(name, XTF_DONT_LOAD); |
Jan Engelhardt | 21b41ee | 2008-02-11 01:02:00 +0100 | [diff] [blame] | 330 | else |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 331 | ptr = xtables_find_match(name, |
| 332 | XTF_DONT_LOAD, NULL); |
Jan Engelhardt | 21b41ee | 2008-02-11 01:02:00 +0100 | [diff] [blame] | 333 | } else if (stat(path, &sb) == 0) { |
| 334 | fprintf(stderr, "%s: %s\n", path, dlerror()); |
| 335 | } |
| 336 | |
| 337 | if (ptr != NULL) |
| 338 | return ptr; |
| 339 | |
| 340 | dir = next + 1; |
| 341 | } while (*next != '\0'); |
| 342 | |
| 343 | return NULL; |
| 344 | } |
Jan Engelhardt | cb25af8 | 2008-04-14 18:37:57 +0200 | [diff] [blame] | 345 | #endif |
Jan Engelhardt | 21b41ee | 2008-02-11 01:02:00 +0100 | [diff] [blame] | 346 | |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 347 | struct xtables_match * |
| 348 | xtables_find_match(const char *name, enum xtables_tryload tryload, |
| 349 | struct xtables_rule_match **matches) |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 350 | { |
| 351 | struct xtables_match *ptr; |
| 352 | const char *icmp6 = "icmp6"; |
| 353 | |
| 354 | /* This is ugly as hell. Nonetheless, there is no way of changing |
| 355 | * this without hurting backwards compatibility */ |
| 356 | if ( (strcmp(name,"icmpv6") == 0) || |
| 357 | (strcmp(name,"ipv6-icmp") == 0) || |
| 358 | (strcmp(name,"icmp6") == 0) ) |
| 359 | name = icmp6; |
| 360 | |
| 361 | for (ptr = xtables_matches; ptr; ptr = ptr->next) { |
| 362 | if (strcmp(name, ptr->name) == 0) { |
| 363 | struct xtables_match *clone; |
| 364 | |
| 365 | /* First match of this type: */ |
| 366 | if (ptr->m == NULL) |
| 367 | break; |
| 368 | |
| 369 | /* Second and subsequent clones */ |
Jan Engelhardt | 630ef48 | 2009-01-27 14:58:41 +0100 | [diff] [blame] | 370 | clone = xtables_malloc(sizeof(struct xtables_match)); |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 371 | memcpy(clone, ptr, sizeof(struct xtables_match)); |
| 372 | clone->mflags = 0; |
| 373 | /* This is a clone: */ |
| 374 | clone->next = clone; |
| 375 | |
| 376 | ptr = clone; |
| 377 | break; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | #ifndef NO_SHARED_LIBS |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 382 | if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) { |
Jan Engelhardt | 39bf9c8 | 2009-01-27 15:59:06 +0100 | [diff] [blame] | 383 | ptr = load_extension(xtables_libdir, afinfo.libprefix, |
| 384 | name, false); |
Yasuyuki KOZAKAI | 170af8c | 2007-08-04 05:22:17 +0000 | [diff] [blame] | 385 | |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 386 | if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED) |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 387 | exit_error(PARAMETER_PROBLEM, |
| 388 | "Couldn't load match `%s':%s\n", |
| 389 | name, dlerror()); |
| 390 | } |
| 391 | #else |
| 392 | if (ptr && !ptr->loaded) { |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 393 | if (tryload != XTF_DONT_LOAD) |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 394 | ptr->loaded = 1; |
| 395 | else |
| 396 | ptr = NULL; |
| 397 | } |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 398 | if(!ptr && (tryload == XTF_LOAD_MUST_SUCCEED)) { |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 399 | exit_error(PARAMETER_PROBLEM, |
| 400 | "Couldn't find match `%s'\n", name); |
| 401 | } |
| 402 | #endif |
| 403 | |
| 404 | if (ptr && matches) { |
| 405 | struct xtables_rule_match **i; |
| 406 | struct xtables_rule_match *newentry; |
| 407 | |
Jan Engelhardt | 630ef48 | 2009-01-27 14:58:41 +0100 | [diff] [blame] | 408 | newentry = xtables_malloc(sizeof(struct xtables_rule_match)); |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 409 | |
| 410 | for (i = matches; *i; i = &(*i)->next) { |
| 411 | if (strcmp(name, (*i)->match->name) == 0) |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 412 | (*i)->completed = true; |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 413 | } |
| 414 | newentry->match = ptr; |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 415 | newentry->completed = false; |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 416 | newentry->next = NULL; |
| 417 | *i = newentry; |
| 418 | } |
| 419 | |
| 420 | return ptr; |
| 421 | } |
| 422 | |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 423 | struct xtables_target * |
| 424 | xtables_find_target(const char *name, enum xtables_tryload tryload) |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 425 | { |
| 426 | struct xtables_target *ptr; |
| 427 | |
| 428 | /* Standard target? */ |
| 429 | if (strcmp(name, "") == 0 |
| 430 | || strcmp(name, XTC_LABEL_ACCEPT) == 0 |
| 431 | || strcmp(name, XTC_LABEL_DROP) == 0 |
| 432 | || strcmp(name, XTC_LABEL_QUEUE) == 0 |
| 433 | || strcmp(name, XTC_LABEL_RETURN) == 0) |
| 434 | name = "standard"; |
| 435 | |
| 436 | for (ptr = xtables_targets; ptr; ptr = ptr->next) { |
| 437 | if (strcmp(name, ptr->name) == 0) |
| 438 | break; |
| 439 | } |
| 440 | |
| 441 | #ifndef NO_SHARED_LIBS |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 442 | if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) { |
Jan Engelhardt | 39bf9c8 | 2009-01-27 15:59:06 +0100 | [diff] [blame] | 443 | ptr = load_extension(xtables_libdir, afinfo.libprefix, |
| 444 | name, true); |
Yasuyuki KOZAKAI | 170af8c | 2007-08-04 05:22:17 +0000 | [diff] [blame] | 445 | |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 446 | if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED) |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 447 | exit_error(PARAMETER_PROBLEM, |
| 448 | "Couldn't load target `%s':%s\n", |
| 449 | name, dlerror()); |
| 450 | } |
| 451 | #else |
| 452 | if (ptr && !ptr->loaded) { |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 453 | if (tryload != XTF_DONT_LOAD) |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 454 | ptr->loaded = 1; |
| 455 | else |
| 456 | ptr = NULL; |
| 457 | } |
| 458 | if(!ptr && (tryload == LOAD_MUST_SUCCEED)) { |
| 459 | exit_error(PARAMETER_PROBLEM, |
| 460 | "Couldn't find target `%s'\n", name); |
| 461 | } |
| 462 | #endif |
| 463 | |
| 464 | if (ptr) |
| 465 | ptr->used = 1; |
| 466 | |
| 467 | return ptr; |
| 468 | } |
| 469 | |
| 470 | static int compatible_revision(const char *name, u_int8_t revision, int opt) |
| 471 | { |
| 472 | struct xt_get_revision rev; |
| 473 | socklen_t s = sizeof(rev); |
| 474 | int max_rev, sockfd; |
| 475 | |
| 476 | sockfd = socket(afinfo.family, SOCK_RAW, IPPROTO_RAW); |
| 477 | if (sockfd < 0) { |
Patrick McHardy | df1ef38 | 2007-12-03 15:32:28 +0000 | [diff] [blame] | 478 | if (errno == EPERM) { |
| 479 | /* revision 0 is always supported. */ |
| 480 | if (revision != 0) |
| 481 | fprintf(stderr, "Could not determine whether " |
| 482 | "revision %u is supported, " |
| 483 | "assuming it is.\n", |
| 484 | revision); |
| 485 | return 1; |
| 486 | } |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 487 | fprintf(stderr, "Could not open socket to kernel: %s\n", |
| 488 | strerror(errno)); |
| 489 | exit(1); |
| 490 | } |
| 491 | |
Jan Engelhardt | c021c3c | 2009-01-27 15:10:05 +0100 | [diff] [blame] | 492 | xtables_load_ko(xtables_modprobe_program, true); |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 493 | |
| 494 | strcpy(rev.name, name); |
| 495 | rev.revision = revision; |
| 496 | |
| 497 | max_rev = getsockopt(sockfd, afinfo.ipproto, opt, &rev, &s); |
| 498 | if (max_rev < 0) { |
| 499 | /* Definitely don't support this? */ |
| 500 | if (errno == ENOENT || errno == EPROTONOSUPPORT) { |
| 501 | close(sockfd); |
| 502 | return 0; |
| 503 | } else if (errno == ENOPROTOOPT) { |
| 504 | close(sockfd); |
| 505 | /* Assume only revision 0 support (old kernel) */ |
| 506 | return (revision == 0); |
| 507 | } else { |
| 508 | fprintf(stderr, "getsockopt failed strangely: %s\n", |
| 509 | strerror(errno)); |
| 510 | exit(1); |
| 511 | } |
| 512 | } |
| 513 | close(sockfd); |
| 514 | return 1; |
| 515 | } |
| 516 | |
| 517 | |
| 518 | static int compatible_match_revision(const char *name, u_int8_t revision) |
| 519 | { |
| 520 | return compatible_revision(name, revision, afinfo.so_rev_match); |
| 521 | } |
| 522 | |
| 523 | static int compatible_target_revision(const char *name, u_int8_t revision) |
| 524 | { |
| 525 | return compatible_revision(name, revision, afinfo.so_rev_target); |
| 526 | } |
| 527 | |
| 528 | void xtables_register_match(struct xtables_match *me) |
| 529 | { |
| 530 | struct xtables_match **i, *old; |
| 531 | |
| 532 | if (strcmp(me->version, program_version) != 0) { |
| 533 | fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n", |
| 534 | program_name, me->name, me->version, program_version); |
| 535 | exit(1); |
| 536 | } |
| 537 | |
| 538 | /* Revision field stole a char from name. */ |
| 539 | if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) { |
| 540 | fprintf(stderr, "%s: target `%s' has invalid name\n", |
| 541 | program_name, me->name); |
| 542 | exit(1); |
| 543 | } |
| 544 | |
| 545 | if (me->family >= NPROTO) { |
| 546 | fprintf(stderr, |
| 547 | "%s: BUG: match %s has invalid protocol family\n", |
| 548 | program_name, me->name); |
| 549 | exit(1); |
| 550 | } |
| 551 | |
| 552 | /* ignore not interested match */ |
Jan Engelhardt | 23545c2 | 2008-02-14 04:23:04 +0100 | [diff] [blame] | 553 | if (me->family != afinfo.family && me->family != AF_UNSPEC) |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 554 | return; |
| 555 | |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 556 | old = xtables_find_match(me->name, XTF_DURING_LOAD, NULL); |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 557 | if (old) { |
Jan Engelhardt | 23545c2 | 2008-02-14 04:23:04 +0100 | [diff] [blame] | 558 | if (old->revision == me->revision && |
| 559 | old->family == me->family) { |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 560 | fprintf(stderr, |
| 561 | "%s: match `%s' already registered.\n", |
| 562 | program_name, me->name); |
| 563 | exit(1); |
| 564 | } |
| 565 | |
| 566 | /* Now we have two (or more) options, check compatibility. */ |
| 567 | if (compatible_match_revision(old->name, old->revision) |
| 568 | && old->revision > me->revision) |
| 569 | return; |
| 570 | |
Jan Engelhardt | 23545c2 | 2008-02-14 04:23:04 +0100 | [diff] [blame] | 571 | /* See if new match can be used. */ |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 572 | if (!compatible_match_revision(me->name, me->revision)) |
| 573 | return; |
| 574 | |
Jan Engelhardt | 23545c2 | 2008-02-14 04:23:04 +0100 | [diff] [blame] | 575 | /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */ |
| 576 | if (old->revision == me->revision && me->family == AF_UNSPEC) |
| 577 | return; |
| 578 | |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 579 | /* Delete old one. */ |
| 580 | for (i = &xtables_matches; *i!=old; i = &(*i)->next); |
| 581 | *i = old->next; |
| 582 | } |
| 583 | |
| 584 | if (me->size != XT_ALIGN(me->size)) { |
| 585 | fprintf(stderr, "%s: match `%s' has invalid size %u.\n", |
| 586 | program_name, me->name, (unsigned int)me->size); |
| 587 | exit(1); |
| 588 | } |
| 589 | |
| 590 | /* Append to list. */ |
| 591 | for (i = &xtables_matches; *i; i = &(*i)->next); |
| 592 | me->next = NULL; |
| 593 | *i = me; |
| 594 | |
| 595 | me->m = NULL; |
| 596 | me->mflags = 0; |
| 597 | } |
| 598 | |
| 599 | void xtables_register_target(struct xtables_target *me) |
| 600 | { |
| 601 | struct xtables_target *old; |
| 602 | |
| 603 | if (strcmp(me->version, program_version) != 0) { |
| 604 | fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n", |
| 605 | program_name, me->name, me->version, program_version); |
| 606 | exit(1); |
| 607 | } |
| 608 | |
| 609 | /* Revision field stole a char from name. */ |
| 610 | if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) { |
| 611 | fprintf(stderr, "%s: target `%s' has invalid name\n", |
| 612 | program_name, me->name); |
| 613 | exit(1); |
| 614 | } |
| 615 | |
| 616 | if (me->family >= NPROTO) { |
| 617 | fprintf(stderr, |
| 618 | "%s: BUG: target %s has invalid protocol family\n", |
| 619 | program_name, me->name); |
| 620 | exit(1); |
| 621 | } |
| 622 | |
| 623 | /* ignore not interested target */ |
Jan Engelhardt | 23545c2 | 2008-02-14 04:23:04 +0100 | [diff] [blame] | 624 | if (me->family != afinfo.family && me->family != AF_UNSPEC) |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 625 | return; |
| 626 | |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 627 | old = xtables_find_target(me->name, XTF_DURING_LOAD); |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 628 | if (old) { |
| 629 | struct xtables_target **i; |
| 630 | |
Jan Engelhardt | 23545c2 | 2008-02-14 04:23:04 +0100 | [diff] [blame] | 631 | if (old->revision == me->revision && |
| 632 | old->family == me->family) { |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 633 | fprintf(stderr, |
| 634 | "%s: target `%s' already registered.\n", |
| 635 | program_name, me->name); |
| 636 | exit(1); |
| 637 | } |
| 638 | |
| 639 | /* Now we have two (or more) options, check compatibility. */ |
| 640 | if (compatible_target_revision(old->name, old->revision) |
| 641 | && old->revision > me->revision) |
| 642 | return; |
| 643 | |
Jan Engelhardt | 23545c2 | 2008-02-14 04:23:04 +0100 | [diff] [blame] | 644 | /* See if new target can be used. */ |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 645 | if (!compatible_target_revision(me->name, me->revision)) |
| 646 | return; |
| 647 | |
Jan Engelhardt | 23545c2 | 2008-02-14 04:23:04 +0100 | [diff] [blame] | 648 | /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */ |
| 649 | if (old->revision == me->revision && me->family == AF_UNSPEC) |
| 650 | return; |
| 651 | |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 652 | /* Delete old one. */ |
| 653 | for (i = &xtables_targets; *i!=old; i = &(*i)->next); |
| 654 | *i = old->next; |
| 655 | } |
| 656 | |
| 657 | if (me->size != XT_ALIGN(me->size)) { |
| 658 | fprintf(stderr, "%s: target `%s' has invalid size %u.\n", |
| 659 | program_name, me->name, (unsigned int)me->size); |
| 660 | exit(1); |
| 661 | } |
| 662 | |
| 663 | /* Prepend to list. */ |
| 664 | me->next = xtables_targets; |
| 665 | xtables_targets = me; |
| 666 | me->t = NULL; |
| 667 | me->tflags = 0; |
| 668 | } |
Jan Engelhardt | aafd269 | 2008-01-20 13:19:40 +0000 | [diff] [blame] | 669 | |
| 670 | void param_act(unsigned int status, const char *p1, ...) |
| 671 | { |
| 672 | const char *p2, *p3; |
| 673 | va_list args; |
| 674 | bool b; |
| 675 | |
| 676 | va_start(args, p1); |
| 677 | |
| 678 | switch (status) { |
| 679 | case P_ONLY_ONCE: |
| 680 | p2 = va_arg(args, const char *); |
| 681 | b = va_arg(args, unsigned int); |
| 682 | if (!b) |
| 683 | return; |
| 684 | exit_error(PARAMETER_PROBLEM, |
| 685 | "%s: \"%s\" option may only be specified once", |
| 686 | p1, p2); |
| 687 | break; |
| 688 | case P_NO_INVERT: |
| 689 | p2 = va_arg(args, const char *); |
| 690 | b = va_arg(args, unsigned int); |
| 691 | if (!b) |
| 692 | return; |
| 693 | exit_error(PARAMETER_PROBLEM, |
| 694 | "%s: \"%s\" option cannot be inverted", p1, p2); |
| 695 | break; |
| 696 | case P_BAD_VALUE: |
| 697 | p2 = va_arg(args, const char *); |
| 698 | p3 = va_arg(args, const char *); |
| 699 | exit_error(PARAMETER_PROBLEM, |
| 700 | "%s: Bad value for \"%s\" option: \"%s\"", |
| 701 | p1, p2, p3); |
| 702 | break; |
| 703 | case P_ONE_ACTION: |
| 704 | b = va_arg(args, unsigned int); |
| 705 | if (!b) |
| 706 | return; |
| 707 | exit_error(PARAMETER_PROBLEM, |
| 708 | "%s: At most one action is possible", p1); |
| 709 | break; |
| 710 | default: |
| 711 | exit_error(status, p1, args); |
| 712 | break; |
| 713 | } |
| 714 | |
| 715 | va_end(args); |
| 716 | } |
Jan Engelhardt | 08b1616 | 2008-01-20 13:36:08 +0000 | [diff] [blame] | 717 | |
| 718 | const char *ipaddr_to_numeric(const struct in_addr *addrp) |
| 719 | { |
| 720 | static char buf[20]; |
| 721 | const unsigned char *bytep = (const void *)&addrp->s_addr; |
| 722 | |
| 723 | sprintf(buf, "%u.%u.%u.%u", bytep[0], bytep[1], bytep[2], bytep[3]); |
| 724 | return buf; |
| 725 | } |
| 726 | |
| 727 | static const char *ipaddr_to_host(const struct in_addr *addr) |
| 728 | { |
| 729 | struct hostent *host; |
| 730 | |
| 731 | host = gethostbyaddr(addr, sizeof(struct in_addr), AF_INET); |
| 732 | if (host == NULL) |
| 733 | return NULL; |
| 734 | |
| 735 | return host->h_name; |
| 736 | } |
| 737 | |
| 738 | static const char *ipaddr_to_network(const struct in_addr *addr) |
| 739 | { |
| 740 | struct netent *net; |
| 741 | |
| 742 | if ((net = getnetbyaddr(ntohl(addr->s_addr), AF_INET)) != NULL) |
| 743 | return net->n_name; |
| 744 | |
| 745 | return NULL; |
| 746 | } |
| 747 | |
| 748 | const char *ipaddr_to_anyname(const struct in_addr *addr) |
| 749 | { |
| 750 | const char *name; |
| 751 | |
| 752 | if ((name = ipaddr_to_host(addr)) != NULL || |
| 753 | (name = ipaddr_to_network(addr)) != NULL) |
| 754 | return name; |
| 755 | |
| 756 | return ipaddr_to_numeric(addr); |
| 757 | } |
| 758 | |
| 759 | const char *ipmask_to_numeric(const struct in_addr *mask) |
| 760 | { |
| 761 | static char buf[20]; |
| 762 | uint32_t maskaddr, bits; |
| 763 | int i; |
| 764 | |
| 765 | maskaddr = ntohl(mask->s_addr); |
| 766 | |
| 767 | if (maskaddr == 0xFFFFFFFFL) |
| 768 | /* we don't want to see "/32" */ |
| 769 | return ""; |
| 770 | |
| 771 | i = 32; |
| 772 | bits = 0xFFFFFFFEL; |
| 773 | while (--i >= 0 && maskaddr != bits) |
| 774 | bits <<= 1; |
| 775 | if (i >= 0) |
| 776 | sprintf(buf, "/%d", i); |
| 777 | else |
| 778 | /* mask was not a decent combination of 1's and 0's */ |
| 779 | sprintf(buf, "/%s", ipaddr_to_numeric(mask)); |
| 780 | |
| 781 | return buf; |
| 782 | } |
| 783 | |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 784 | static struct in_addr *__numeric_to_ipaddr(const char *dotted, bool is_mask) |
| 785 | { |
| 786 | static struct in_addr addr; |
| 787 | unsigned char *addrp; |
| 788 | unsigned int onebyte; |
| 789 | char buf[20], *p, *q; |
| 790 | int i; |
| 791 | |
| 792 | /* copy dotted string, because we need to modify it */ |
| 793 | strncpy(buf, dotted, sizeof(buf) - 1); |
| 794 | buf[sizeof(buf) - 1] = '\0'; |
| 795 | addrp = (void *)&addr.s_addr; |
| 796 | |
| 797 | p = buf; |
| 798 | for (i = 0; i < 3; ++i) { |
| 799 | if ((q = strchr(p, '.')) == NULL) { |
| 800 | if (is_mask) |
| 801 | return NULL; |
| 802 | |
| 803 | /* autocomplete, this is a network address */ |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame^] | 804 | if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX)) |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 805 | return NULL; |
| 806 | |
| 807 | addrp[i] = onebyte; |
| 808 | while (i < 3) |
| 809 | addrp[++i] = 0; |
| 810 | |
| 811 | return &addr; |
| 812 | } |
| 813 | |
| 814 | *q = '\0'; |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame^] | 815 | if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX)) |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 816 | return NULL; |
| 817 | |
| 818 | addrp[i] = onebyte; |
| 819 | p = q + 1; |
| 820 | } |
| 821 | |
| 822 | /* we have checked 3 bytes, now we check the last one */ |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame^] | 823 | if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX)) |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 824 | return NULL; |
| 825 | |
| 826 | addrp[3] = onebyte; |
| 827 | return &addr; |
| 828 | } |
| 829 | |
| 830 | struct in_addr *numeric_to_ipaddr(const char *dotted) |
| 831 | { |
| 832 | return __numeric_to_ipaddr(dotted, false); |
| 833 | } |
| 834 | |
| 835 | struct in_addr *numeric_to_ipmask(const char *dotted) |
| 836 | { |
| 837 | return __numeric_to_ipaddr(dotted, true); |
| 838 | } |
| 839 | |
| 840 | static struct in_addr *network_to_ipaddr(const char *name) |
| 841 | { |
| 842 | static struct in_addr addr; |
| 843 | struct netent *net; |
| 844 | |
| 845 | if ((net = getnetbyname(name)) != NULL) { |
| 846 | if (net->n_addrtype != AF_INET) |
| 847 | return NULL; |
| 848 | addr.s_addr = htonl(net->n_net); |
| 849 | return &addr; |
| 850 | } |
| 851 | |
| 852 | return NULL; |
| 853 | } |
| 854 | |
| 855 | static struct in_addr *host_to_ipaddr(const char *name, unsigned int *naddr) |
| 856 | { |
| 857 | struct hostent *host; |
| 858 | struct in_addr *addr; |
| 859 | unsigned int i; |
| 860 | |
| 861 | *naddr = 0; |
| 862 | if ((host = gethostbyname(name)) != NULL) { |
| 863 | if (host->h_addrtype != AF_INET || |
| 864 | host->h_length != sizeof(struct in_addr)) |
| 865 | return NULL; |
| 866 | |
| 867 | while (host->h_addr_list[*naddr] != NULL) |
| 868 | ++*naddr; |
Jan Engelhardt | 630ef48 | 2009-01-27 14:58:41 +0100 | [diff] [blame] | 869 | addr = xtables_calloc(*naddr, sizeof(struct in_addr) * *naddr); |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 870 | for (i = 0; i < *naddr; i++) |
| 871 | memcpy(&addr[i], host->h_addr_list[i], |
| 872 | sizeof(struct in_addr)); |
| 873 | return addr; |
| 874 | } |
| 875 | |
| 876 | return NULL; |
| 877 | } |
| 878 | |
| 879 | static struct in_addr * |
| 880 | ipparse_hostnetwork(const char *name, unsigned int *naddrs) |
| 881 | { |
| 882 | struct in_addr *addrptmp, *addrp; |
| 883 | |
| 884 | if ((addrptmp = numeric_to_ipaddr(name)) != NULL || |
| 885 | (addrptmp = network_to_ipaddr(name)) != NULL) { |
Jan Engelhardt | 630ef48 | 2009-01-27 14:58:41 +0100 | [diff] [blame] | 886 | addrp = xtables_malloc(sizeof(struct in_addr)); |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 887 | memcpy(addrp, addrptmp, sizeof(*addrp)); |
| 888 | *naddrs = 1; |
| 889 | return addrp; |
| 890 | } |
| 891 | if ((addrptmp = host_to_ipaddr(name, naddrs)) != NULL) |
| 892 | return addrptmp; |
| 893 | |
| 894 | exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name); |
| 895 | } |
| 896 | |
| 897 | static struct in_addr *parse_ipmask(const char *mask) |
| 898 | { |
| 899 | static struct in_addr maskaddr; |
| 900 | struct in_addr *addrp; |
| 901 | unsigned int bits; |
| 902 | |
| 903 | if (mask == NULL) { |
| 904 | /* no mask at all defaults to 32 bits */ |
| 905 | maskaddr.s_addr = 0xFFFFFFFF; |
| 906 | return &maskaddr; |
| 907 | } |
| 908 | if ((addrp = numeric_to_ipmask(mask)) != NULL) |
| 909 | /* dotted_to_addr already returns a network byte order addr */ |
| 910 | return addrp; |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame^] | 911 | if (!xtables_strtoui(mask, NULL, &bits, 0, 32)) |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 912 | exit_error(PARAMETER_PROBLEM, |
| 913 | "invalid mask `%s' specified", mask); |
| 914 | if (bits != 0) { |
| 915 | maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits)); |
| 916 | return &maskaddr; |
| 917 | } |
| 918 | |
| 919 | maskaddr.s_addr = 0U; |
| 920 | return &maskaddr; |
| 921 | } |
| 922 | |
| 923 | void ipparse_hostnetworkmask(const char *name, struct in_addr **addrpp, |
| 924 | struct in_addr *maskp, unsigned int *naddrs) |
| 925 | { |
| 926 | unsigned int i, j, k, n; |
| 927 | struct in_addr *addrp; |
| 928 | char buf[256], *p; |
| 929 | |
| 930 | strncpy(buf, name, sizeof(buf) - 1); |
| 931 | buf[sizeof(buf) - 1] = '\0'; |
| 932 | if ((p = strrchr(buf, '/')) != NULL) { |
| 933 | *p = '\0'; |
| 934 | addrp = parse_ipmask(p + 1); |
| 935 | } else { |
| 936 | addrp = parse_ipmask(NULL); |
| 937 | } |
| 938 | memcpy(maskp, addrp, sizeof(*maskp)); |
| 939 | |
| 940 | /* if a null mask is given, the name is ignored, like in "any/0" */ |
| 941 | if (maskp->s_addr == 0U) |
| 942 | strcpy(buf, "0.0.0.0"); |
| 943 | |
| 944 | addrp = *addrpp = ipparse_hostnetwork(buf, naddrs); |
| 945 | n = *naddrs; |
| 946 | for (i = 0, j = 0; i < n; ++i) { |
| 947 | addrp[j++].s_addr &= maskp->s_addr; |
| 948 | for (k = 0; k < j - 1; ++k) |
| 949 | if (addrp[k].s_addr == addrp[j-1].s_addr) { |
| 950 | --*naddrs; |
| 951 | --j; |
| 952 | break; |
| 953 | } |
| 954 | } |
| 955 | } |
| 956 | |
Jan Engelhardt | 08b1616 | 2008-01-20 13:36:08 +0000 | [diff] [blame] | 957 | const char *ip6addr_to_numeric(const struct in6_addr *addrp) |
| 958 | { |
| 959 | /* 0000:0000:0000:0000:0000:000.000.000.000 |
| 960 | * 0000:0000:0000:0000:0000:0000:0000:0000 */ |
| 961 | static char buf[50+1]; |
| 962 | return inet_ntop(AF_INET6, addrp, buf, sizeof(buf)); |
| 963 | } |
| 964 | |
| 965 | static const char *ip6addr_to_host(const struct in6_addr *addr) |
| 966 | { |
| 967 | static char hostname[NI_MAXHOST]; |
| 968 | struct sockaddr_in6 saddr; |
| 969 | int err; |
| 970 | |
| 971 | memset(&saddr, 0, sizeof(struct sockaddr_in6)); |
| 972 | memcpy(&saddr.sin6_addr, addr, sizeof(*addr)); |
| 973 | saddr.sin6_family = AF_INET6; |
| 974 | |
| 975 | err = getnameinfo((const void *)&saddr, sizeof(struct sockaddr_in6), |
| 976 | hostname, sizeof(hostname) - 1, NULL, 0, 0); |
| 977 | if (err != 0) { |
| 978 | #ifdef DEBUG |
| 979 | fprintf(stderr,"IP2Name: %s\n",gai_strerror(err)); |
| 980 | #endif |
| 981 | return NULL; |
| 982 | } |
| 983 | |
| 984 | #ifdef DEBUG |
| 985 | fprintf (stderr, "\naddr2host: %s\n", hostname); |
| 986 | #endif |
| 987 | return hostname; |
| 988 | } |
| 989 | |
| 990 | const char *ip6addr_to_anyname(const struct in6_addr *addr) |
| 991 | { |
| 992 | const char *name; |
| 993 | |
| 994 | if ((name = ip6addr_to_host(addr)) != NULL) |
| 995 | return name; |
| 996 | |
| 997 | return ip6addr_to_numeric(addr); |
| 998 | } |
| 999 | |
| 1000 | static int ip6addr_prefix_length(const struct in6_addr *k) |
| 1001 | { |
| 1002 | unsigned int bits = 0; |
| 1003 | uint32_t a, b, c, d; |
| 1004 | |
Jan Engelhardt | 4860781 | 2008-06-10 15:17:53 +0200 | [diff] [blame] | 1005 | a = ntohl(k->s6_addr32[0]); |
| 1006 | b = ntohl(k->s6_addr32[1]); |
| 1007 | c = ntohl(k->s6_addr32[2]); |
| 1008 | d = ntohl(k->s6_addr32[3]); |
Jan Engelhardt | 08b1616 | 2008-01-20 13:36:08 +0000 | [diff] [blame] | 1009 | while (a & 0x80000000U) { |
| 1010 | ++bits; |
| 1011 | a <<= 1; |
| 1012 | a |= (b >> 31) & 1; |
| 1013 | b <<= 1; |
| 1014 | b |= (c >> 31) & 1; |
| 1015 | c <<= 1; |
| 1016 | c |= (d >> 31) & 1; |
| 1017 | d <<= 1; |
| 1018 | } |
| 1019 | if (a != 0 || b != 0 || c != 0 || d != 0) |
| 1020 | return -1; |
| 1021 | return bits; |
| 1022 | } |
| 1023 | |
| 1024 | const char *ip6mask_to_numeric(const struct in6_addr *addrp) |
| 1025 | { |
| 1026 | static char buf[50+2]; |
| 1027 | int l = ip6addr_prefix_length(addrp); |
| 1028 | |
| 1029 | if (l == -1) { |
| 1030 | strcpy(buf, "/"); |
| 1031 | strcat(buf, ip6addr_to_numeric(addrp)); |
| 1032 | return buf; |
| 1033 | } |
| 1034 | sprintf(buf, "/%d", l); |
| 1035 | return buf; |
| 1036 | } |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1037 | |
| 1038 | struct in6_addr *numeric_to_ip6addr(const char *num) |
| 1039 | { |
| 1040 | static struct in6_addr ap; |
| 1041 | int err; |
| 1042 | |
| 1043 | if ((err = inet_pton(AF_INET6, num, &ap)) == 1) |
| 1044 | return ≈ |
| 1045 | #ifdef DEBUG |
| 1046 | fprintf(stderr, "\nnumeric2addr: %d\n", err); |
| 1047 | #endif |
| 1048 | return NULL; |
| 1049 | } |
| 1050 | |
| 1051 | static struct in6_addr * |
| 1052 | host_to_ip6addr(const char *name, unsigned int *naddr) |
| 1053 | { |
| 1054 | static struct in6_addr *addr; |
| 1055 | struct addrinfo hints; |
| 1056 | struct addrinfo *res; |
| 1057 | int err; |
| 1058 | |
| 1059 | memset(&hints, 0, sizeof(hints)); |
| 1060 | hints.ai_flags = AI_CANONNAME; |
| 1061 | hints.ai_family = AF_INET6; |
| 1062 | hints.ai_socktype = SOCK_RAW; |
| 1063 | hints.ai_protocol = IPPROTO_IPV6; |
| 1064 | hints.ai_next = NULL; |
| 1065 | |
| 1066 | *naddr = 0; |
| 1067 | if ((err = getaddrinfo(name, NULL, &hints, &res)) != 0) { |
| 1068 | #ifdef DEBUG |
| 1069 | fprintf(stderr,"Name2IP: %s\n",gai_strerror(err)); |
| 1070 | #endif |
| 1071 | return NULL; |
| 1072 | } else { |
| 1073 | if (res->ai_family != AF_INET6 || |
| 1074 | res->ai_addrlen != sizeof(struct sockaddr_in6)) |
| 1075 | return NULL; |
| 1076 | |
| 1077 | #ifdef DEBUG |
| 1078 | fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen, |
| 1079 | ip6addr_to_numeric(&((struct sockaddr_in6 *)res->ai_addr)->sin6_addr)); |
| 1080 | #endif |
| 1081 | /* Get the first element of the address-chain */ |
Jan Engelhardt | 630ef48 | 2009-01-27 14:58:41 +0100 | [diff] [blame] | 1082 | addr = xtables_malloc(sizeof(struct in6_addr)); |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1083 | memcpy(addr, &((const struct sockaddr_in6 *)res->ai_addr)->sin6_addr, |
| 1084 | sizeof(struct in6_addr)); |
| 1085 | freeaddrinfo(res); |
| 1086 | *naddr = 1; |
| 1087 | return addr; |
| 1088 | } |
| 1089 | |
| 1090 | return NULL; |
| 1091 | } |
| 1092 | |
| 1093 | static struct in6_addr *network_to_ip6addr(const char *name) |
| 1094 | { |
| 1095 | /* abort();*/ |
| 1096 | /* TODO: not implemented yet, but the exception breaks the |
| 1097 | * name resolvation */ |
| 1098 | return NULL; |
| 1099 | } |
| 1100 | |
| 1101 | static struct in6_addr * |
| 1102 | ip6parse_hostnetwork(const char *name, unsigned int *naddrs) |
| 1103 | { |
| 1104 | struct in6_addr *addrp, *addrptmp; |
| 1105 | |
| 1106 | if ((addrptmp = numeric_to_ip6addr(name)) != NULL || |
| 1107 | (addrptmp = network_to_ip6addr(name)) != NULL) { |
Jan Engelhardt | 630ef48 | 2009-01-27 14:58:41 +0100 | [diff] [blame] | 1108 | addrp = xtables_malloc(sizeof(struct in6_addr)); |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1109 | memcpy(addrp, addrptmp, sizeof(*addrp)); |
| 1110 | *naddrs = 1; |
| 1111 | return addrp; |
| 1112 | } |
| 1113 | if ((addrp = host_to_ip6addr(name, naddrs)) != NULL) |
| 1114 | return addrp; |
| 1115 | |
| 1116 | exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name); |
| 1117 | } |
| 1118 | |
| 1119 | static struct in6_addr *parse_ip6mask(char *mask) |
| 1120 | { |
| 1121 | static struct in6_addr maskaddr; |
| 1122 | struct in6_addr *addrp; |
| 1123 | unsigned int bits; |
| 1124 | |
| 1125 | if (mask == NULL) { |
| 1126 | /* no mask at all defaults to 128 bits */ |
| 1127 | memset(&maskaddr, 0xff, sizeof maskaddr); |
| 1128 | return &maskaddr; |
| 1129 | } |
| 1130 | if ((addrp = numeric_to_ip6addr(mask)) != NULL) |
| 1131 | return addrp; |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame^] | 1132 | if (!xtables_strtoui(mask, NULL, &bits, 0, 128)) |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1133 | exit_error(PARAMETER_PROBLEM, |
| 1134 | "invalid mask `%s' specified", mask); |
| 1135 | if (bits != 0) { |
| 1136 | char *p = (void *)&maskaddr; |
| 1137 | memset(p, 0xff, bits / 8); |
| 1138 | memset(p + (bits / 8) + 1, 0, (128 - bits) / 8); |
| 1139 | p[bits/8] = 0xff << (8 - (bits & 7)); |
| 1140 | return &maskaddr; |
| 1141 | } |
| 1142 | |
| 1143 | memset(&maskaddr, 0, sizeof(maskaddr)); |
| 1144 | return &maskaddr; |
| 1145 | } |
| 1146 | |
| 1147 | void ip6parse_hostnetworkmask(const char *name, struct in6_addr **addrpp, |
| 1148 | struct in6_addr *maskp, unsigned int *naddrs) |
| 1149 | { |
| 1150 | struct in6_addr *addrp; |
| 1151 | unsigned int i, j, k, n; |
| 1152 | char buf[256], *p; |
| 1153 | |
| 1154 | strncpy(buf, name, sizeof(buf) - 1); |
| 1155 | buf[sizeof(buf)-1] = '\0'; |
| 1156 | if ((p = strrchr(buf, '/')) != NULL) { |
| 1157 | *p = '\0'; |
| 1158 | addrp = parse_ip6mask(p + 1); |
| 1159 | } else { |
| 1160 | addrp = parse_ip6mask(NULL); |
| 1161 | } |
| 1162 | memcpy(maskp, addrp, sizeof(*maskp)); |
| 1163 | |
| 1164 | /* if a null mask is given, the name is ignored, like in "any/0" */ |
| 1165 | if (memcmp(maskp, &in6addr_any, sizeof(in6addr_any)) == 0) |
| 1166 | strcpy(buf, "::"); |
| 1167 | |
| 1168 | addrp = *addrpp = ip6parse_hostnetwork(buf, naddrs); |
| 1169 | n = *naddrs; |
| 1170 | for (i = 0, j = 0; i < n; ++i) { |
| 1171 | for (k = 0; k < 4; ++k) |
Yasuyuki Kozakai | 5a2208c | 2008-06-04 15:16:03 +0200 | [diff] [blame] | 1172 | addrp[j].s6_addr32[k] &= maskp->s6_addr32[k]; |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1173 | ++j; |
| 1174 | for (k = 0; k < j - 1; ++k) |
| 1175 | if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) { |
| 1176 | --*naddrs; |
| 1177 | --j; |
| 1178 | break; |
| 1179 | } |
| 1180 | } |
| 1181 | } |
Max Kellermann | a5d0994 | 2008-01-29 13:44:34 +0000 | [diff] [blame] | 1182 | |
| 1183 | void save_string(const char *value) |
| 1184 | { |
| 1185 | static const char no_quote_chars[] = "_-0123456789" |
| 1186 | "abcdefghijklmnopqrstuvwxyz" |
| 1187 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 1188 | static const char escape_chars[] = "\"\\'"; |
| 1189 | size_t length; |
| 1190 | const char *p; |
| 1191 | |
| 1192 | length = strcspn(value, no_quote_chars); |
| 1193 | if (length > 0 && value[length] == 0) { |
| 1194 | /* no quoting required */ |
| 1195 | fputs(value, stdout); |
| 1196 | putchar(' '); |
| 1197 | } else { |
| 1198 | /* there is at least one dangerous character in the |
| 1199 | value, which we have to quote. Write double quotes |
| 1200 | around the value and escape special characters with |
| 1201 | a backslash */ |
| 1202 | putchar('"'); |
| 1203 | |
| 1204 | for (p = strpbrk(value, escape_chars); p != NULL; |
| 1205 | p = strpbrk(value, escape_chars)) { |
| 1206 | if (p > value) |
| 1207 | fwrite(value, 1, p - value, stdout); |
| 1208 | putchar('\\'); |
| 1209 | putchar(*p); |
| 1210 | value = p + 1; |
| 1211 | } |
| 1212 | |
| 1213 | /* print the rest and finish the double quoted |
| 1214 | string */ |
| 1215 | fputs(value, stdout); |
| 1216 | printf("\" "); |
| 1217 | } |
| 1218 | } |