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