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