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