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