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