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