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> |
Maciej Zenczykowski | b32b361 | 2011-04-19 09:14:04 +0200 | [diff] [blame^] | 30 | #include <sys/statfs.h> |
Yasuyuki KOZAKAI | 0b82e8e | 2007-07-24 05:47:40 +0000 | [diff] [blame] | 31 | #include <sys/types.h> |
| 32 | #include <sys/wait.h> |
Jan Engelhardt | 08b1616 | 2008-01-20 13:36:08 +0000 | [diff] [blame] | 33 | #include <arpa/inet.h> |
Maciej Zenczykowski | b32b361 | 2011-04-19 09:14:04 +0200 | [diff] [blame^] | 34 | #include <linux/magic.h> /* for PROC_SUPER_MAGIC */ |
Yasuyuki KOZAKAI | 3dfa448 | 2007-07-24 05:45:33 +0000 | [diff] [blame] | 35 | |
Yasuyuki KOZAKAI | 5208806 | 2007-07-24 05:44:11 +0000 | [diff] [blame] | 36 | #include <xtables.h> |
Jan Engelhardt | 4e41854 | 2009-02-21 03:46:37 +0100 | [diff] [blame] | 37 | #include <limits.h> /* INT_MAX in ip_tables.h/ip6_tables.h */ |
Jan Engelhardt | 77f48c2 | 2009-02-07 19:59:53 +0100 | [diff] [blame] | 38 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 39 | #include <linux/netfilter_ipv6/ip6_tables.h> |
Jan Engelhardt | ef18e81 | 2008-08-04 12:47:48 +0200 | [diff] [blame] | 40 | #include <libiptc/libxtc.h> |
Yasuyuki KOZAKAI | 3dfa448 | 2007-07-24 05:45:33 +0000 | [diff] [blame] | 41 | |
Mike Frysinger | 5a26b5f | 2007-12-19 14:51:17 +0000 | [diff] [blame] | 42 | #ifndef NO_SHARED_LIBS |
| 43 | #include <dlfcn.h> |
| 44 | #endif |
Jan Engelhardt | c31870f | 2009-02-10 10:48:28 +0100 | [diff] [blame] | 45 | #ifndef IPT_SO_GET_REVISION_MATCH /* Old kernel source. */ |
| 46 | # define IPT_SO_GET_REVISION_MATCH (IPT_BASE_CTL + 2) |
| 47 | # define IPT_SO_GET_REVISION_TARGET (IPT_BASE_CTL + 3) |
| 48 | #endif |
| 49 | #ifndef IP6T_SO_GET_REVISION_MATCH /* Old kernel source. */ |
| 50 | # define IP6T_SO_GET_REVISION_MATCH 68 |
| 51 | # define IP6T_SO_GET_REVISION_TARGET 69 |
| 52 | #endif |
Jamal Hadi Salim | 7058192 | 2009-02-13 08:36:44 -0500 | [diff] [blame] | 53 | #include <getopt.h> |
Jan Engelhardt | aa37acc | 2011-02-07 04:00:50 +0100 | [diff] [blame] | 54 | #include "iptables/internal.h" |
Jan Engelhardt | 1e128bd | 2011-01-08 02:25:28 +0100 | [diff] [blame] | 55 | #include "xshared.h" |
Mike Frysinger | 5a26b5f | 2007-12-19 14:51:17 +0000 | [diff] [blame] | 56 | |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 57 | #define NPROTO 255 |
| 58 | |
Yasuyuki KOZAKAI | 0b82e8e | 2007-07-24 05:47:40 +0000 | [diff] [blame] | 59 | #ifndef PROC_SYS_MODPROBE |
| 60 | #define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe" |
| 61 | #endif |
| 62 | |
Maciej Zenczykowski | 37911de | 2011-04-05 12:42:37 +0200 | [diff] [blame] | 63 | /* we need this for ip6?tables-restore. ip6?tables-restore.c sets line to the |
| 64 | * current line of the input file, in order to give a more precise error |
| 65 | * message. ip6?tables itself doesn't need this, so it is initialized to the |
| 66 | * magic number of -1 */ |
| 67 | int line = -1; |
| 68 | |
Jamal Hadi Salim | 8b7baeb | 2009-02-11 13:05:43 +0100 | [diff] [blame] | 69 | void basic_exit_err(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3))); |
| 70 | |
Jamal Hadi Salim | 40a8343 | 2009-02-11 13:02:21 +0100 | [diff] [blame] | 71 | struct xtables_globals *xt_params = NULL; |
| 72 | |
Jamal Hadi Salim | 8b7baeb | 2009-02-11 13:05:43 +0100 | [diff] [blame] | 73 | void basic_exit_err(enum xtables_exittype status, const char *msg, ...) |
Jamal Hadi Salim | 40a8343 | 2009-02-11 13:02:21 +0100 | [diff] [blame] | 74 | { |
| 75 | va_list args; |
| 76 | |
| 77 | va_start(args, msg); |
| 78 | fprintf(stderr, "%s v%s: ", xt_params->program_name, xt_params->program_version); |
| 79 | vfprintf(stderr, msg, args); |
| 80 | va_end(args); |
| 81 | fprintf(stderr, "\n"); |
| 82 | exit(status); |
| 83 | } |
| 84 | |
Jan Engelhardt | 600f38d | 2010-10-29 18:57:42 +0200 | [diff] [blame] | 85 | void xtables_free_opts(int unused) |
Jamal Hadi Salim | 84c3055 | 2009-02-11 13:00:02 +0100 | [diff] [blame] | 86 | { |
Jan Engelhardt | df28823 | 2011-01-31 02:33:43 +0100 | [diff] [blame] | 87 | if (xt_params->opts != xt_params->orig_opts) { |
Jan Engelhardt | 59e8114 | 2010-11-15 13:19:48 +0100 | [diff] [blame] | 88 | free(xt_params->opts); |
Jan Engelhardt | df28823 | 2011-01-31 02:33:43 +0100 | [diff] [blame] | 89 | xt_params->opts = NULL; |
| 90 | } |
Jamal Hadi Salim | 84c3055 | 2009-02-11 13:00:02 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Jan Engelhardt | 600f38d | 2010-10-29 18:57:42 +0200 | [diff] [blame] | 93 | struct option *xtables_merge_options(struct option *orig_opts, |
| 94 | struct option *oldopts, |
Jamal Hadi Salim | 7058192 | 2009-02-13 08:36:44 -0500 | [diff] [blame] | 95 | const struct option *newopts, |
| 96 | unsigned int *option_offset) |
| 97 | { |
Jan Engelhardt | 600f38d | 2010-10-29 18:57:42 +0200 | [diff] [blame] | 98 | unsigned int num_oold = 0, num_old = 0, num_new = 0, i; |
| 99 | struct option *merge, *mp; |
Jamal Hadi Salim | 7058192 | 2009-02-13 08:36:44 -0500 | [diff] [blame] | 100 | |
| 101 | if (newopts == NULL) |
| 102 | return oldopts; |
| 103 | |
Jan Engelhardt | 600f38d | 2010-10-29 18:57:42 +0200 | [diff] [blame] | 104 | for (num_oold = 0; orig_opts[num_oold].name; num_oold++) ; |
| 105 | if (oldopts != NULL) |
| 106 | for (num_old = 0; oldopts[num_old].name; num_old++) ; |
Jamal Hadi Salim | 7058192 | 2009-02-13 08:36:44 -0500 | [diff] [blame] | 107 | for (num_new = 0; newopts[num_new].name; num_new++) ; |
| 108 | |
Jan Engelhardt | 1dc2739 | 2011-01-08 02:10:52 +0100 | [diff] [blame] | 109 | /* |
| 110 | * Since @oldopts also has @orig_opts already (and does so at the |
| 111 | * start), skip these entries. |
| 112 | */ |
| 113 | oldopts += num_oold; |
| 114 | num_old -= num_oold; |
| 115 | |
Jan Engelhardt | 600f38d | 2010-10-29 18:57:42 +0200 | [diff] [blame] | 116 | merge = malloc(sizeof(*mp) * (num_oold + num_old + num_new + 1)); |
Jamal Hadi Salim | 7058192 | 2009-02-13 08:36:44 -0500 | [diff] [blame] | 117 | if (merge == NULL) |
| 118 | return NULL; |
Jamal Hadi Salim | 7058192 | 2009-02-13 08:36:44 -0500 | [diff] [blame] | 119 | |
Jan Engelhardt | 600f38d | 2010-10-29 18:57:42 +0200 | [diff] [blame] | 120 | /* Let the base options -[ADI...] have precedence over everything */ |
| 121 | memcpy(merge, orig_opts, sizeof(*mp) * num_oold); |
| 122 | mp = merge + num_oold; |
| 123 | |
| 124 | /* Second, the new options */ |
Jan Engelhardt | 1e128bd | 2011-01-08 02:25:28 +0100 | [diff] [blame] | 125 | xt_params->option_offset += XT_OPTION_OFFSET_SCALE; |
Jan Engelhardt | 600f38d | 2010-10-29 18:57:42 +0200 | [diff] [blame] | 126 | *option_offset = xt_params->option_offset; |
| 127 | memcpy(mp, newopts, sizeof(*mp) * num_new); |
| 128 | |
| 129 | for (i = 0; i < num_new; ++i, ++mp) |
| 130 | mp->val += *option_offset; |
| 131 | |
| 132 | /* Third, the old options */ |
| 133 | memcpy(mp, oldopts, sizeof(*mp) * num_old); |
| 134 | mp += num_old; |
| 135 | xtables_free_opts(0); |
| 136 | |
| 137 | /* Clear trailing entry */ |
| 138 | memset(mp, 0, sizeof(*mp)); |
Jamal Hadi Salim | 7058192 | 2009-02-13 08:36:44 -0500 | [diff] [blame] | 139 | return merge; |
| 140 | } |
| 141 | |
Jan Engelhardt | 77f48c2 | 2009-02-07 19:59:53 +0100 | [diff] [blame] | 142 | static const struct xtables_afinfo afinfo_ipv4 = { |
| 143 | .kmod = "ip_tables", |
Maciej Zenczykowski | b32b361 | 2011-04-19 09:14:04 +0200 | [diff] [blame^] | 144 | .proc_exists = "/proc/net/ip_tables_names", |
Jan Engelhardt | 77f48c2 | 2009-02-07 19:59:53 +0100 | [diff] [blame] | 145 | .libprefix = "libipt_", |
| 146 | .family = NFPROTO_IPV4, |
| 147 | .ipproto = IPPROTO_IP, |
| 148 | .so_rev_match = IPT_SO_GET_REVISION_MATCH, |
| 149 | .so_rev_target = IPT_SO_GET_REVISION_TARGET, |
| 150 | }; |
| 151 | |
| 152 | static const struct xtables_afinfo afinfo_ipv6 = { |
| 153 | .kmod = "ip6_tables", |
Maciej Zenczykowski | b32b361 | 2011-04-19 09:14:04 +0200 | [diff] [blame^] | 154 | .proc_exists = "/proc/net/ip6_tables_names", |
Jan Engelhardt | 77f48c2 | 2009-02-07 19:59:53 +0100 | [diff] [blame] | 155 | .libprefix = "libip6t_", |
| 156 | .family = NFPROTO_IPV6, |
| 157 | .ipproto = IPPROTO_IPV6, |
| 158 | .so_rev_match = IP6T_SO_GET_REVISION_MATCH, |
| 159 | .so_rev_target = IP6T_SO_GET_REVISION_TARGET, |
| 160 | }; |
| 161 | |
Jan Engelhardt | aa37acc | 2011-02-07 04:00:50 +0100 | [diff] [blame] | 162 | const struct xtables_afinfo *afinfo; |
Jan Engelhardt | 77f48c2 | 2009-02-07 19:59:53 +0100 | [diff] [blame] | 163 | |
Jan Engelhardt | 39bf9c8 | 2009-01-27 15:59:06 +0100 | [diff] [blame] | 164 | /* Search path for Xtables .so files */ |
| 165 | static const char *xtables_libdir; |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 166 | |
Yasuyuki KOZAKAI | 0b82e8e | 2007-07-24 05:47:40 +0000 | [diff] [blame] | 167 | /* the path to command to load kernel module */ |
Jan Engelhardt | c021c3c | 2009-01-27 15:10:05 +0100 | [diff] [blame] | 168 | const char *xtables_modprobe_program; |
Yasuyuki KOZAKAI | 0b82e8e | 2007-07-24 05:47:40 +0000 | [diff] [blame] | 169 | |
Maciej Zenczykowski | 2c6ac07 | 2011-04-04 15:31:43 +0200 | [diff] [blame] | 170 | /* Keep track of matches/targets pending full registration: linked lists. */ |
| 171 | struct xtables_match *xtables_pending_matches; |
| 172 | struct xtables_target *xtables_pending_targets; |
| 173 | |
| 174 | /* Keep track of fully registered external matches/targets: linked lists. */ |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 175 | struct xtables_match *xtables_matches; |
| 176 | struct xtables_target *xtables_targets; |
| 177 | |
Maciej Zenczykowski | 2c6ac07 | 2011-04-04 15:31:43 +0200 | [diff] [blame] | 178 | /* Fully register a match/target which was previously partially registered. */ |
| 179 | static void xtables_fully_register_pending_match(struct xtables_match *me); |
| 180 | static void xtables_fully_register_pending_target(struct xtables_target *me); |
| 181 | |
Jan Engelhardt | 39bf9c8 | 2009-01-27 15:59:06 +0100 | [diff] [blame] | 182 | void xtables_init(void) |
| 183 | { |
| 184 | xtables_libdir = getenv("XTABLES_LIBDIR"); |
| 185 | if (xtables_libdir != NULL) |
| 186 | return; |
| 187 | xtables_libdir = getenv("IPTABLES_LIB_DIR"); |
| 188 | if (xtables_libdir != NULL) { |
| 189 | fprintf(stderr, "IPTABLES_LIB_DIR is deprecated, " |
| 190 | "use XTABLES_LIBDIR.\n"); |
| 191 | return; |
| 192 | } |
Jan Engelhardt | ec93419 | 2009-02-10 09:54:04 +0100 | [diff] [blame] | 193 | /* |
| 194 | * Well yes, IP6TABLES_LIB_DIR is of lower priority over |
| 195 | * IPTABLES_LIB_DIR since this moved to libxtables; I think that is ok |
| 196 | * for these env vars are deprecated anyhow, and in light of the |
| 197 | * (shared) libxt_*.so files, makes less sense to have |
| 198 | * IPTABLES_LIB_DIR != IP6TABLES_LIB_DIR. |
| 199 | */ |
| 200 | xtables_libdir = getenv("IP6TABLES_LIB_DIR"); |
| 201 | if (xtables_libdir != NULL) { |
| 202 | fprintf(stderr, "IP6TABLES_LIB_DIR is deprecated, " |
| 203 | "use XTABLES_LIBDIR.\n"); |
| 204 | return; |
| 205 | } |
Jan Engelhardt | 39bf9c8 | 2009-01-27 15:59:06 +0100 | [diff] [blame] | 206 | xtables_libdir = XTABLES_LIBDIR; |
| 207 | } |
| 208 | |
Jan Engelhardt | 77f48c2 | 2009-02-07 19:59:53 +0100 | [diff] [blame] | 209 | void xtables_set_nfproto(uint8_t nfproto) |
| 210 | { |
| 211 | switch (nfproto) { |
| 212 | case NFPROTO_IPV4: |
| 213 | afinfo = &afinfo_ipv4; |
| 214 | break; |
| 215 | case NFPROTO_IPV6: |
| 216 | afinfo = &afinfo_ipv6; |
| 217 | break; |
| 218 | default: |
| 219 | fprintf(stderr, "libxtables: unhandled NFPROTO in %s\n", |
| 220 | __func__); |
| 221 | } |
| 222 | } |
| 223 | |
Jan Engelhardt | 630ef48 | 2009-01-27 14:58:41 +0100 | [diff] [blame] | 224 | /** |
Jamal Hadi Salim | 7e4db2f | 2009-02-13 09:14:17 -0500 | [diff] [blame] | 225 | * xtables_set_params - set the global parameters used by xtables |
| 226 | * @xtp: input xtables_globals structure |
| 227 | * |
| 228 | * The app is expected to pass a valid xtables_globals data-filled |
| 229 | * with proper values |
| 230 | * @xtp cannot be NULL |
| 231 | * |
| 232 | * Returns -1 on failure to set and 0 on success |
| 233 | */ |
| 234 | int xtables_set_params(struct xtables_globals *xtp) |
| 235 | { |
| 236 | if (!xtp) { |
| 237 | fprintf(stderr, "%s: Illegal global params\n",__func__); |
| 238 | return -1; |
| 239 | } |
| 240 | |
| 241 | xt_params = xtp; |
| 242 | |
| 243 | if (!xt_params->exit_err) |
| 244 | xt_params->exit_err = basic_exit_err; |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | int xtables_init_all(struct xtables_globals *xtp, uint8_t nfproto) |
| 250 | { |
| 251 | xtables_init(); |
| 252 | xtables_set_nfproto(nfproto); |
| 253 | return xtables_set_params(xtp); |
| 254 | } |
| 255 | |
| 256 | /** |
Jan Engelhardt | 630ef48 | 2009-01-27 14:58:41 +0100 | [diff] [blame] | 257 | * xtables_*alloc - wrappers that exit on failure |
| 258 | */ |
| 259 | void *xtables_calloc(size_t count, size_t size) |
Yasuyuki KOZAKAI | 3dfa448 | 2007-07-24 05:45:33 +0000 | [diff] [blame] | 260 | { |
| 261 | void *p; |
| 262 | |
| 263 | if ((p = calloc(count, size)) == NULL) { |
| 264 | perror("ip[6]tables: calloc failed"); |
| 265 | exit(1); |
| 266 | } |
| 267 | |
| 268 | return p; |
| 269 | } |
| 270 | |
Jan Engelhardt | 630ef48 | 2009-01-27 14:58:41 +0100 | [diff] [blame] | 271 | void *xtables_malloc(size_t size) |
Yasuyuki KOZAKAI | 3dfa448 | 2007-07-24 05:45:33 +0000 | [diff] [blame] | 272 | { |
| 273 | void *p; |
| 274 | |
| 275 | if ((p = malloc(size)) == NULL) { |
| 276 | perror("ip[6]tables: malloc failed"); |
| 277 | exit(1); |
| 278 | } |
| 279 | |
| 280 | return p; |
| 281 | } |
Yasuyuki KOZAKAI | 0b82e8e | 2007-07-24 05:47:40 +0000 | [diff] [blame] | 282 | |
Michael Granzow | 332e4ac | 2009-04-09 18:24:36 +0100 | [diff] [blame] | 283 | void *xtables_realloc(void *ptr, size_t size) |
| 284 | { |
| 285 | void *p; |
| 286 | |
| 287 | if ((p = realloc(ptr, size)) == NULL) { |
| 288 | perror("ip[6]tables: realloc failed"); |
| 289 | exit(1); |
| 290 | } |
| 291 | |
| 292 | return p; |
| 293 | } |
| 294 | |
Yasuyuki KOZAKAI | 0b82e8e | 2007-07-24 05:47:40 +0000 | [diff] [blame] | 295 | static char *get_modprobe(void) |
| 296 | { |
| 297 | int procfile; |
| 298 | char *ret; |
| 299 | |
| 300 | #define PROCFILE_BUFSIZ 1024 |
| 301 | procfile = open(PROC_SYS_MODPROBE, O_RDONLY); |
| 302 | if (procfile < 0) |
| 303 | return NULL; |
Maciej Zenczykowski | a239728 | 2011-04-04 15:30:32 +0200 | [diff] [blame] | 304 | if (fcntl(procfile, F_SETFD, FD_CLOEXEC) == -1) { |
| 305 | fprintf(stderr, "Could not set close on exec: %s\n", |
| 306 | strerror(errno)); |
| 307 | exit(1); |
| 308 | } |
Yasuyuki KOZAKAI | 0b82e8e | 2007-07-24 05:47:40 +0000 | [diff] [blame] | 309 | |
Jan Engelhardt | 371cea2 | 2010-07-25 23:36:17 +0200 | [diff] [blame] | 310 | ret = malloc(PROCFILE_BUFSIZ); |
Yasuyuki KOZAKAI | 0b82e8e | 2007-07-24 05:47:40 +0000 | [diff] [blame] | 311 | if (ret) { |
| 312 | memset(ret, 0, PROCFILE_BUFSIZ); |
| 313 | switch (read(procfile, ret, PROCFILE_BUFSIZ)) { |
| 314 | case -1: goto fail; |
| 315 | case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */ |
| 316 | } |
| 317 | if (ret[strlen(ret)-1]=='\n') |
| 318 | ret[strlen(ret)-1]=0; |
| 319 | close(procfile); |
| 320 | return ret; |
| 321 | } |
| 322 | fail: |
| 323 | free(ret); |
| 324 | close(procfile); |
| 325 | return NULL; |
| 326 | } |
| 327 | |
Jan Engelhardt | c021c3c | 2009-01-27 15:10:05 +0100 | [diff] [blame] | 328 | int xtables_insmod(const char *modname, const char *modprobe, bool quiet) |
Yasuyuki KOZAKAI | 0b82e8e | 2007-07-24 05:47:40 +0000 | [diff] [blame] | 329 | { |
| 330 | char *buf = NULL; |
| 331 | char *argv[4]; |
| 332 | int status; |
| 333 | |
| 334 | /* If they don't explicitly set it, read out of kernel */ |
| 335 | if (!modprobe) { |
| 336 | buf = get_modprobe(); |
| 337 | if (!buf) |
| 338 | return -1; |
| 339 | modprobe = buf; |
| 340 | } |
| 341 | |
Jan Engelhardt | c19f880 | 2009-02-12 01:28:35 +0100 | [diff] [blame] | 342 | /* |
| 343 | * Need to flush the buffer, or the child may output it again |
| 344 | * when switching the program thru execv. |
| 345 | */ |
| 346 | fflush(stdout); |
| 347 | |
Jan Engelhardt | 94aa2ea | 2009-10-11 03:56:18 -0400 | [diff] [blame] | 348 | switch (vfork()) { |
Yasuyuki KOZAKAI | 0b82e8e | 2007-07-24 05:47:40 +0000 | [diff] [blame] | 349 | case 0: |
| 350 | argv[0] = (char *)modprobe; |
| 351 | argv[1] = (char *)modname; |
| 352 | if (quiet) { |
| 353 | argv[2] = "-q"; |
| 354 | argv[3] = NULL; |
| 355 | } else { |
| 356 | argv[2] = NULL; |
| 357 | argv[3] = NULL; |
| 358 | } |
| 359 | execv(argv[0], argv); |
| 360 | |
| 361 | /* not usually reached */ |
| 362 | exit(1); |
| 363 | case -1: |
| 364 | return -1; |
| 365 | |
| 366 | default: /* parent */ |
| 367 | wait(&status); |
| 368 | } |
| 369 | |
| 370 | free(buf); |
| 371 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) |
| 372 | return 0; |
| 373 | return -1; |
| 374 | } |
| 375 | |
Maciej Zenczykowski | b32b361 | 2011-04-19 09:14:04 +0200 | [diff] [blame^] | 376 | /* return true if a given file exists within procfs */ |
| 377 | static bool proc_file_exists(const char *filename) |
| 378 | { |
| 379 | struct stat s; |
| 380 | struct statfs f; |
| 381 | |
| 382 | if (lstat(filename, &s)) |
| 383 | return false; |
| 384 | if (!S_ISREG(s.st_mode)) |
| 385 | return false; |
| 386 | if (statfs(filename, &f)) |
| 387 | return false; |
| 388 | if (f.f_type != PROC_SUPER_MAGIC) |
| 389 | return false; |
| 390 | return true; |
| 391 | } |
| 392 | |
Jan Engelhardt | c021c3c | 2009-01-27 15:10:05 +0100 | [diff] [blame] | 393 | int xtables_load_ko(const char *modprobe, bool quiet) |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 394 | { |
Jan Engelhardt | c021c3c | 2009-01-27 15:10:05 +0100 | [diff] [blame] | 395 | static bool loaded = false; |
Maciej Zenczykowski | b32b361 | 2011-04-19 09:14:04 +0200 | [diff] [blame^] | 396 | int ret; |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 397 | |
Maciej Zenczykowski | b32b361 | 2011-04-19 09:14:04 +0200 | [diff] [blame^] | 398 | if (loaded) |
| 399 | return 0; |
| 400 | |
| 401 | if (proc_file_exists(afinfo->proc_exists)) { |
| 402 | loaded = true; |
| 403 | return 0; |
| 404 | }; |
| 405 | |
| 406 | ret = xtables_insmod(afinfo->kmod, modprobe, quiet); |
| 407 | if (ret == 0) |
| 408 | loaded = true; |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 409 | |
| 410 | return ret; |
| 411 | } |
| 412 | |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 413 | /** |
| 414 | * xtables_strtou{i,l} - string to number conversion |
| 415 | * @s: input string |
| 416 | * @end: like strtoul's "end" pointer |
| 417 | * @value: pointer for result |
| 418 | * @min: minimum accepted value |
| 419 | * @max: maximum accepted value |
Jan Engelhardt | cd9e7aa | 2008-01-20 13:18:54 +0000 | [diff] [blame] | 420 | * |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 421 | * If @end is NULL, we assume the caller wants a "strict strtoul", and hence |
| 422 | * "15a" is rejected. |
| 423 | * In either case, the value obtained is compared for min-max compliance. |
| 424 | * Base is always 0, i.e. autodetect depending on @s. |
| 425 | * |
| 426 | * Returns true/false whether number was accepted. On failure, *value has |
| 427 | * undefined contents. |
Jan Engelhardt | cd9e7aa | 2008-01-20 13:18:54 +0000 | [diff] [blame] | 428 | */ |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 429 | bool xtables_strtoul(const char *s, char **end, unsigned long *value, |
| 430 | unsigned long min, unsigned long max) |
Jan Engelhardt | cd9e7aa | 2008-01-20 13:18:54 +0000 | [diff] [blame] | 431 | { |
| 432 | unsigned long v; |
| 433 | char *my_end; |
| 434 | |
| 435 | errno = 0; |
| 436 | v = strtoul(s, &my_end, 0); |
| 437 | |
| 438 | if (my_end == s) |
| 439 | return false; |
| 440 | if (end != NULL) |
| 441 | *end = my_end; |
| 442 | |
| 443 | if (errno != ERANGE && min <= v && (max == 0 || v <= max)) { |
| 444 | if (value != NULL) |
| 445 | *value = v; |
| 446 | if (end == NULL) |
| 447 | return *my_end == '\0'; |
| 448 | return true; |
| 449 | } |
| 450 | |
| 451 | return false; |
| 452 | } |
| 453 | |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 454 | bool xtables_strtoui(const char *s, char **end, unsigned int *value, |
| 455 | unsigned int min, unsigned int max) |
Jan Engelhardt | cd9e7aa | 2008-01-20 13:18:54 +0000 | [diff] [blame] | 456 | { |
| 457 | unsigned long v; |
| 458 | bool ret; |
| 459 | |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 460 | ret = xtables_strtoul(s, end, &v, min, max); |
Jan Engelhardt | cd9e7aa | 2008-01-20 13:18:54 +0000 | [diff] [blame] | 461 | if (value != NULL) |
| 462 | *value = v; |
| 463 | return ret; |
| 464 | } |
| 465 | |
Jan Engelhardt | aae6be9 | 2009-01-30 04:24:47 +0100 | [diff] [blame] | 466 | int xtables_service_to_port(const char *name, const char *proto) |
Yasuyuki KOZAKAI | 04f8c54 | 2007-07-24 05:53:48 +0000 | [diff] [blame] | 467 | { |
| 468 | struct servent *service; |
| 469 | |
| 470 | if ((service = getservbyname(name, proto)) != NULL) |
| 471 | return ntohs((unsigned short) service->s_port); |
| 472 | |
| 473 | return -1; |
| 474 | } |
| 475 | |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 476 | uint16_t xtables_parse_port(const char *port, const char *proto) |
Yasuyuki KOZAKAI | 04f8c54 | 2007-07-24 05:53:48 +0000 | [diff] [blame] | 477 | { |
Jan Engelhardt | 7a236f4 | 2008-03-03 12:30:41 +0100 | [diff] [blame] | 478 | unsigned int portnum; |
Yasuyuki KOZAKAI | 04f8c54 | 2007-07-24 05:53:48 +0000 | [diff] [blame] | 479 | |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 480 | if (xtables_strtoui(port, NULL, &portnum, 0, UINT16_MAX) || |
Jan Engelhardt | aae6be9 | 2009-01-30 04:24:47 +0100 | [diff] [blame] | 481 | (portnum = xtables_service_to_port(port, proto)) != (unsigned)-1) |
Jan Engelhardt | 213e185 | 2009-01-27 17:24:34 +0100 | [diff] [blame] | 482 | return portnum; |
Yasuyuki KOZAKAI | 04f8c54 | 2007-07-24 05:53:48 +0000 | [diff] [blame] | 483 | |
Jamal Hadi Salim | 8b7baeb | 2009-02-11 13:05:43 +0100 | [diff] [blame] | 484 | xt_params->exit_err(PARAMETER_PROBLEM, |
Yasuyuki KOZAKAI | 04f8c54 | 2007-07-24 05:53:48 +0000 | [diff] [blame] | 485 | "invalid port/service `%s' specified", port); |
| 486 | } |
| 487 | |
Jan Engelhardt | aae6be9 | 2009-01-30 04:24:47 +0100 | [diff] [blame] | 488 | void xtables_parse_interface(const char *arg, char *vianame, |
| 489 | unsigned char *mask) |
Yasuyuki KOZAKAI | 04f8c54 | 2007-07-24 05:53:48 +0000 | [diff] [blame] | 490 | { |
Jan Engelhardt | fcf5723 | 2010-02-09 15:59:13 +0100 | [diff] [blame] | 491 | unsigned int vialen = strlen(arg); |
Yasuyuki KOZAKAI | 04f8c54 | 2007-07-24 05:53:48 +0000 | [diff] [blame] | 492 | unsigned int i; |
| 493 | |
| 494 | memset(mask, 0, IFNAMSIZ); |
| 495 | memset(vianame, 0, IFNAMSIZ); |
| 496 | |
| 497 | if (vialen + 1 > IFNAMSIZ) |
Jamal Hadi Salim | 8b7baeb | 2009-02-11 13:05:43 +0100 | [diff] [blame] | 498 | xt_params->exit_err(PARAMETER_PROBLEM, |
Yasuyuki KOZAKAI | 04f8c54 | 2007-07-24 05:53:48 +0000 | [diff] [blame] | 499 | "interface name `%s' must be shorter than IFNAMSIZ" |
| 500 | " (%i)", arg, IFNAMSIZ-1); |
| 501 | |
| 502 | strcpy(vianame, arg); |
Jan Engelhardt | fcf5723 | 2010-02-09 15:59:13 +0100 | [diff] [blame] | 503 | if (vialen == 0) |
Yasuyuki KOZAKAI | 04f8c54 | 2007-07-24 05:53:48 +0000 | [diff] [blame] | 504 | memset(mask, 0, IFNAMSIZ); |
| 505 | else if (vianame[vialen - 1] == '+') { |
| 506 | memset(mask, 0xFF, vialen - 1); |
| 507 | memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1); |
| 508 | /* Don't remove `+' here! -HW */ |
| 509 | } else { |
| 510 | /* Include nul-terminator in match */ |
| 511 | memset(mask, 0xFF, vialen + 1); |
| 512 | memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1); |
| 513 | for (i = 0; vianame[i]; i++) { |
Jan Engelhardt | fcf5723 | 2010-02-09 15:59:13 +0100 | [diff] [blame] | 514 | if (vianame[i] == '/' || |
| 515 | vianame[i] == ' ') { |
Max Kellermann | aae4f82 | 2007-10-17 16:36:49 +0000 | [diff] [blame] | 516 | fprintf(stderr, |
| 517 | "Warning: weird character in interface" |
Jan Engelhardt | fcf5723 | 2010-02-09 15:59:13 +0100 | [diff] [blame] | 518 | " `%s' ('/' and ' ' are not allowed by the kernel).\n", |
Max Kellermann | aae4f82 | 2007-10-17 16:36:49 +0000 | [diff] [blame] | 519 | vianame); |
Yasuyuki KOZAKAI | 04f8c54 | 2007-07-24 05:53:48 +0000 | [diff] [blame] | 520 | break; |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | |
Jan Engelhardt | cb25af8 | 2008-04-14 18:37:57 +0200 | [diff] [blame] | 526 | #ifndef NO_SHARED_LIBS |
Jan Engelhardt | 9273850 | 2011-01-30 14:18:17 +0100 | [diff] [blame] | 527 | static void *load_extension(const char *search_path, const char *af_prefix, |
Jan Engelhardt | 21b41ee | 2008-02-11 01:02:00 +0100 | [diff] [blame] | 528 | const char *name, bool is_target) |
| 529 | { |
Jan Engelhardt | 9273850 | 2011-01-30 14:18:17 +0100 | [diff] [blame] | 530 | const char *all_prefixes[] = {"libxt_", af_prefix, NULL}; |
| 531 | const char **prefix; |
Jan Engelhardt | 21b41ee | 2008-02-11 01:02:00 +0100 | [diff] [blame] | 532 | const char *dir = search_path, *next; |
| 533 | void *ptr = NULL; |
| 534 | struct stat sb; |
| 535 | char path[256]; |
| 536 | |
| 537 | do { |
| 538 | next = strchr(dir, ':'); |
| 539 | if (next == NULL) |
| 540 | next = dir + strlen(dir); |
Jan Engelhardt | 21b41ee | 2008-02-11 01:02:00 +0100 | [diff] [blame] | 541 | |
Jan Engelhardt | 9273850 | 2011-01-30 14:18:17 +0100 | [diff] [blame] | 542 | for (prefix = all_prefixes; *prefix != NULL; ++prefix) { |
| 543 | snprintf(path, sizeof(path), "%.*s/%s%s.so", |
| 544 | (unsigned int)(next - dir), dir, |
| 545 | *prefix, name); |
| 546 | |
| 547 | if (stat(path, &sb) != 0) { |
| 548 | if (errno == ENOENT) |
| 549 | continue; |
| 550 | fprintf(stderr, "%s: %s\n", path, |
| 551 | strerror(errno)); |
| 552 | return NULL; |
| 553 | } |
| 554 | if (dlopen(path, RTLD_NOW) == NULL) { |
| 555 | fprintf(stderr, "%s: %s\n", path, dlerror()); |
| 556 | break; |
| 557 | } |
| 558 | |
Jan Engelhardt | 21b41ee | 2008-02-11 01:02:00 +0100 | [diff] [blame] | 559 | if (is_target) |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 560 | ptr = xtables_find_target(name, XTF_DONT_LOAD); |
Jan Engelhardt | 21b41ee | 2008-02-11 01:02:00 +0100 | [diff] [blame] | 561 | else |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 562 | ptr = xtables_find_match(name, |
| 563 | XTF_DONT_LOAD, NULL); |
Jan Engelhardt | 9273850 | 2011-01-30 14:18:17 +0100 | [diff] [blame] | 564 | |
| 565 | if (ptr != NULL) |
| 566 | return ptr; |
| 567 | |
| 568 | fprintf(stderr, "%s: no \"%s\" extension found for " |
| 569 | "this protocol\n", path, name); |
| 570 | errno = ENOENT; |
| 571 | return NULL; |
Jan Engelhardt | 21b41ee | 2008-02-11 01:02:00 +0100 | [diff] [blame] | 572 | } |
Jan Engelhardt | 21b41ee | 2008-02-11 01:02:00 +0100 | [diff] [blame] | 573 | dir = next + 1; |
| 574 | } while (*next != '\0'); |
| 575 | |
| 576 | return NULL; |
| 577 | } |
Jan Engelhardt | cb25af8 | 2008-04-14 18:37:57 +0200 | [diff] [blame] | 578 | #endif |
Jan Engelhardt | 21b41ee | 2008-02-11 01:02:00 +0100 | [diff] [blame] | 579 | |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 580 | struct xtables_match * |
| 581 | xtables_find_match(const char *name, enum xtables_tryload tryload, |
| 582 | struct xtables_rule_match **matches) |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 583 | { |
Maciej Zenczykowski | 2c6ac07 | 2011-04-04 15:31:43 +0200 | [diff] [blame] | 584 | struct xtables_match **dptr; |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 585 | struct xtables_match *ptr; |
| 586 | const char *icmp6 = "icmp6"; |
| 587 | |
Jan Engelhardt | 0cb675b | 2010-06-07 11:50:25 +0200 | [diff] [blame] | 588 | if (strlen(name) >= XT_EXTENSION_MAXNAMELEN) |
Jan Engelhardt | 21d1283 | 2010-03-16 16:49:21 +0100 | [diff] [blame] | 589 | xtables_error(PARAMETER_PROBLEM, |
| 590 | "Invalid match name \"%s\" (%u chars max)", |
Jan Engelhardt | 0cb675b | 2010-06-07 11:50:25 +0200 | [diff] [blame] | 591 | name, XT_EXTENSION_MAXNAMELEN - 1); |
Jan Engelhardt | 21d1283 | 2010-03-16 16:49:21 +0100 | [diff] [blame] | 592 | |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 593 | /* This is ugly as hell. Nonetheless, there is no way of changing |
| 594 | * this without hurting backwards compatibility */ |
| 595 | if ( (strcmp(name,"icmpv6") == 0) || |
| 596 | (strcmp(name,"ipv6-icmp") == 0) || |
| 597 | (strcmp(name,"icmp6") == 0) ) |
| 598 | name = icmp6; |
| 599 | |
Maciej Zenczykowski | 2c6ac07 | 2011-04-04 15:31:43 +0200 | [diff] [blame] | 600 | /* Trigger delayed initialization */ |
| 601 | for (dptr = &xtables_pending_matches; *dptr; ) { |
| 602 | if (strcmp(name, (*dptr)->name) == 0) { |
| 603 | ptr = *dptr; |
| 604 | *dptr = (*dptr)->next; |
| 605 | ptr->next = NULL; |
| 606 | xtables_fully_register_pending_match(ptr); |
| 607 | } else { |
| 608 | dptr = &((*dptr)->next); |
| 609 | } |
| 610 | } |
| 611 | |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 612 | for (ptr = xtables_matches; ptr; ptr = ptr->next) { |
| 613 | if (strcmp(name, ptr->name) == 0) { |
| 614 | struct xtables_match *clone; |
| 615 | |
| 616 | /* First match of this type: */ |
| 617 | if (ptr->m == NULL) |
| 618 | break; |
| 619 | |
| 620 | /* Second and subsequent clones */ |
Jan Engelhardt | 630ef48 | 2009-01-27 14:58:41 +0100 | [diff] [blame] | 621 | clone = xtables_malloc(sizeof(struct xtables_match)); |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 622 | memcpy(clone, ptr, sizeof(struct xtables_match)); |
| 623 | clone->mflags = 0; |
| 624 | /* This is a clone: */ |
| 625 | clone->next = clone; |
| 626 | |
| 627 | ptr = clone; |
| 628 | break; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | #ifndef NO_SHARED_LIBS |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 633 | if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) { |
Jan Engelhardt | 77f48c2 | 2009-02-07 19:59:53 +0100 | [diff] [blame] | 634 | ptr = load_extension(xtables_libdir, afinfo->libprefix, |
Jan Engelhardt | 39bf9c8 | 2009-01-27 15:59:06 +0100 | [diff] [blame] | 635 | name, false); |
Yasuyuki KOZAKAI | 170af8c | 2007-08-04 05:22:17 +0000 | [diff] [blame] | 636 | |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 637 | if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED) |
Jamal Hadi Salim | 8b7baeb | 2009-02-11 13:05:43 +0100 | [diff] [blame] | 638 | xt_params->exit_err(PARAMETER_PROBLEM, |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 639 | "Couldn't load match `%s':%s\n", |
Jan Engelhardt | 9273850 | 2011-01-30 14:18:17 +0100 | [diff] [blame] | 640 | name, strerror(errno)); |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 641 | } |
| 642 | #else |
| 643 | if (ptr && !ptr->loaded) { |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 644 | if (tryload != XTF_DONT_LOAD) |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 645 | ptr->loaded = 1; |
| 646 | else |
| 647 | ptr = NULL; |
| 648 | } |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 649 | if(!ptr && (tryload == XTF_LOAD_MUST_SUCCEED)) { |
Jamal Hadi Salim | 8b7baeb | 2009-02-11 13:05:43 +0100 | [diff] [blame] | 650 | xt_params->exit_err(PARAMETER_PROBLEM, |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 651 | "Couldn't find match `%s'\n", name); |
| 652 | } |
| 653 | #endif |
| 654 | |
| 655 | if (ptr && matches) { |
| 656 | struct xtables_rule_match **i; |
| 657 | struct xtables_rule_match *newentry; |
| 658 | |
Jan Engelhardt | 630ef48 | 2009-01-27 14:58:41 +0100 | [diff] [blame] | 659 | newentry = xtables_malloc(sizeof(struct xtables_rule_match)); |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 660 | |
| 661 | for (i = matches; *i; i = &(*i)->next) { |
| 662 | if (strcmp(name, (*i)->match->name) == 0) |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 663 | (*i)->completed = true; |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 664 | } |
| 665 | newentry->match = ptr; |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 666 | newentry->completed = false; |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 667 | newentry->next = NULL; |
| 668 | *i = newentry; |
| 669 | } |
| 670 | |
| 671 | return ptr; |
| 672 | } |
| 673 | |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 674 | struct xtables_target * |
| 675 | xtables_find_target(const char *name, enum xtables_tryload tryload) |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 676 | { |
Maciej Zenczykowski | 2c6ac07 | 2011-04-04 15:31:43 +0200 | [diff] [blame] | 677 | struct xtables_target **dptr; |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 678 | struct xtables_target *ptr; |
| 679 | |
| 680 | /* Standard target? */ |
| 681 | if (strcmp(name, "") == 0 |
| 682 | || strcmp(name, XTC_LABEL_ACCEPT) == 0 |
| 683 | || strcmp(name, XTC_LABEL_DROP) == 0 |
| 684 | || strcmp(name, XTC_LABEL_QUEUE) == 0 |
| 685 | || strcmp(name, XTC_LABEL_RETURN) == 0) |
| 686 | name = "standard"; |
| 687 | |
Maciej Zenczykowski | 2c6ac07 | 2011-04-04 15:31:43 +0200 | [diff] [blame] | 688 | /* Trigger delayed initialization */ |
| 689 | for (dptr = &xtables_pending_targets; *dptr; ) { |
| 690 | if (strcmp(name, (*dptr)->name) == 0) { |
| 691 | ptr = *dptr; |
| 692 | *dptr = (*dptr)->next; |
| 693 | ptr->next = NULL; |
| 694 | xtables_fully_register_pending_target(ptr); |
| 695 | } else { |
| 696 | dptr = &((*dptr)->next); |
| 697 | } |
| 698 | } |
| 699 | |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 700 | for (ptr = xtables_targets; ptr; ptr = ptr->next) { |
| 701 | if (strcmp(name, ptr->name) == 0) |
| 702 | break; |
| 703 | } |
| 704 | |
| 705 | #ifndef NO_SHARED_LIBS |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 706 | if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) { |
Jan Engelhardt | 77f48c2 | 2009-02-07 19:59:53 +0100 | [diff] [blame] | 707 | ptr = load_extension(xtables_libdir, afinfo->libprefix, |
Jan Engelhardt | 39bf9c8 | 2009-01-27 15:59:06 +0100 | [diff] [blame] | 708 | name, true); |
Yasuyuki KOZAKAI | 170af8c | 2007-08-04 05:22:17 +0000 | [diff] [blame] | 709 | |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 710 | if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED) |
Jamal Hadi Salim | 8b7baeb | 2009-02-11 13:05:43 +0100 | [diff] [blame] | 711 | xt_params->exit_err(PARAMETER_PROBLEM, |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 712 | "Couldn't load target `%s':%s\n", |
Jan Engelhardt | 9273850 | 2011-01-30 14:18:17 +0100 | [diff] [blame] | 713 | name, strerror(errno)); |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 714 | } |
| 715 | #else |
| 716 | if (ptr && !ptr->loaded) { |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 717 | if (tryload != XTF_DONT_LOAD) |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 718 | ptr->loaded = 1; |
| 719 | else |
| 720 | ptr = NULL; |
| 721 | } |
Peter Volkov | 854d2d9 | 2009-03-24 11:09:16 +0300 | [diff] [blame] | 722 | if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED) { |
Jamal Hadi Salim | 8b7baeb | 2009-02-11 13:05:43 +0100 | [diff] [blame] | 723 | xt_params->exit_err(PARAMETER_PROBLEM, |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 724 | "Couldn't find target `%s'\n", name); |
| 725 | } |
| 726 | #endif |
| 727 | |
| 728 | if (ptr) |
| 729 | ptr->used = 1; |
| 730 | |
| 731 | return ptr; |
| 732 | } |
| 733 | |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 734 | static int compatible_revision(const char *name, uint8_t revision, int opt) |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 735 | { |
| 736 | struct xt_get_revision rev; |
| 737 | socklen_t s = sizeof(rev); |
| 738 | int max_rev, sockfd; |
| 739 | |
Jan Engelhardt | 77f48c2 | 2009-02-07 19:59:53 +0100 | [diff] [blame] | 740 | sockfd = socket(afinfo->family, SOCK_RAW, IPPROTO_RAW); |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 741 | if (sockfd < 0) { |
Patrick McHardy | df1ef38 | 2007-12-03 15:32:28 +0000 | [diff] [blame] | 742 | if (errno == EPERM) { |
| 743 | /* revision 0 is always supported. */ |
| 744 | if (revision != 0) |
| 745 | fprintf(stderr, "Could not determine whether " |
| 746 | "revision %u is supported, " |
| 747 | "assuming it is.\n", |
| 748 | revision); |
| 749 | return 1; |
| 750 | } |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 751 | fprintf(stderr, "Could not open socket to kernel: %s\n", |
| 752 | strerror(errno)); |
| 753 | exit(1); |
| 754 | } |
| 755 | |
Maciej Zenczykowski | a239728 | 2011-04-04 15:30:32 +0200 | [diff] [blame] | 756 | if (fcntl(sockfd, F_SETFD, FD_CLOEXEC) == -1) { |
| 757 | fprintf(stderr, "Could not set close on exec: %s\n", |
| 758 | strerror(errno)); |
| 759 | exit(1); |
| 760 | } |
| 761 | |
Jan Engelhardt | c021c3c | 2009-01-27 15:10:05 +0100 | [diff] [blame] | 762 | xtables_load_ko(xtables_modprobe_program, true); |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 763 | |
| 764 | strcpy(rev.name, name); |
| 765 | rev.revision = revision; |
| 766 | |
Jan Engelhardt | 77f48c2 | 2009-02-07 19:59:53 +0100 | [diff] [blame] | 767 | max_rev = getsockopt(sockfd, afinfo->ipproto, opt, &rev, &s); |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 768 | if (max_rev < 0) { |
| 769 | /* Definitely don't support this? */ |
| 770 | if (errno == ENOENT || errno == EPROTONOSUPPORT) { |
| 771 | close(sockfd); |
| 772 | return 0; |
| 773 | } else if (errno == ENOPROTOOPT) { |
| 774 | close(sockfd); |
| 775 | /* Assume only revision 0 support (old kernel) */ |
| 776 | return (revision == 0); |
| 777 | } else { |
| 778 | fprintf(stderr, "getsockopt failed strangely: %s\n", |
| 779 | strerror(errno)); |
| 780 | exit(1); |
| 781 | } |
| 782 | } |
| 783 | close(sockfd); |
| 784 | return 1; |
| 785 | } |
| 786 | |
| 787 | |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 788 | static int compatible_match_revision(const char *name, uint8_t revision) |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 789 | { |
Jan Engelhardt | 77f48c2 | 2009-02-07 19:59:53 +0100 | [diff] [blame] | 790 | return compatible_revision(name, revision, afinfo->so_rev_match); |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 793 | static int compatible_target_revision(const char *name, uint8_t revision) |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 794 | { |
Jan Engelhardt | 77f48c2 | 2009-02-07 19:59:53 +0100 | [diff] [blame] | 795 | return compatible_revision(name, revision, afinfo->so_rev_target); |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 796 | } |
| 797 | |
Jan Engelhardt | dfbedfe | 2011-01-08 03:31:04 +0100 | [diff] [blame] | 798 | static void xtables_check_options(const char *name, const struct option *opt) |
| 799 | { |
| 800 | for (; opt->name != NULL; ++opt) |
| 801 | if (opt->val < 0 || opt->val >= XT_OPTION_OFFSET_SCALE) { |
| 802 | fprintf(stderr, "%s: Extension %s uses invalid " |
| 803 | "option value %d\n",xt_params->program_name, |
| 804 | name, opt->val); |
| 805 | exit(1); |
| 806 | } |
| 807 | } |
| 808 | |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 809 | void xtables_register_match(struct xtables_match *me) |
| 810 | { |
Jan Engelhardt | c284de5 | 2009-06-25 21:25:24 +0200 | [diff] [blame] | 811 | if (me->version == NULL) { |
| 812 | fprintf(stderr, "%s: match %s<%u> is missing a version\n", |
| 813 | xt_params->program_name, me->name, me->revision); |
| 814 | exit(1); |
| 815 | } |
Jan Engelhardt | dacafa5 | 2009-01-27 20:56:23 +0100 | [diff] [blame] | 816 | if (strcmp(me->version, XTABLES_VERSION) != 0) { |
| 817 | fprintf(stderr, "%s: match \"%s\" has version \"%s\", " |
| 818 | "but \"%s\" is required.\n", |
Jamal Hadi Salim | 5dd19de | 2009-02-13 10:42:24 -0500 | [diff] [blame] | 819 | xt_params->program_name, me->name, |
Jan Engelhardt | dacafa5 | 2009-01-27 20:56:23 +0100 | [diff] [blame] | 820 | me->version, XTABLES_VERSION); |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 821 | exit(1); |
| 822 | } |
| 823 | |
Jan Engelhardt | 0cb675b | 2010-06-07 11:50:25 +0200 | [diff] [blame] | 824 | if (strlen(me->name) >= XT_EXTENSION_MAXNAMELEN) { |
Li Yewang | 281439b | 2011-01-09 22:26:58 +0100 | [diff] [blame] | 825 | fprintf(stderr, "%s: match `%s' has invalid name\n", |
Jamal Hadi Salim | 5dd19de | 2009-02-13 10:42:24 -0500 | [diff] [blame] | 826 | xt_params->program_name, me->name); |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 827 | exit(1); |
| 828 | } |
| 829 | |
| 830 | if (me->family >= NPROTO) { |
| 831 | fprintf(stderr, |
| 832 | "%s: BUG: match %s has invalid protocol family\n", |
Jamal Hadi Salim | 5dd19de | 2009-02-13 10:42:24 -0500 | [diff] [blame] | 833 | xt_params->program_name, me->name); |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 834 | exit(1); |
| 835 | } |
| 836 | |
Jan Engelhardt | aa37acc | 2011-02-07 04:00:50 +0100 | [diff] [blame] | 837 | if (me->x6_options != NULL) |
| 838 | xtables_option_metavalidate(me->name, me->x6_options); |
Jan Engelhardt | dfbedfe | 2011-01-08 03:31:04 +0100 | [diff] [blame] | 839 | if (me->extra_opts != NULL) |
| 840 | xtables_check_options(me->name, me->extra_opts); |
| 841 | |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 842 | /* ignore not interested match */ |
Jan Engelhardt | 77f48c2 | 2009-02-07 19:59:53 +0100 | [diff] [blame] | 843 | if (me->family != afinfo->family && me->family != AF_UNSPEC) |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 844 | return; |
| 845 | |
Maciej Zenczykowski | 2c6ac07 | 2011-04-04 15:31:43 +0200 | [diff] [blame] | 846 | /* place on linked list of matches pending full registration */ |
| 847 | me->next = xtables_pending_matches; |
| 848 | xtables_pending_matches = me; |
| 849 | } |
| 850 | |
| 851 | static void xtables_fully_register_pending_match(struct xtables_match *me) |
| 852 | { |
| 853 | struct xtables_match **i, *old; |
| 854 | |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 855 | old = xtables_find_match(me->name, XTF_DURING_LOAD, NULL); |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 856 | if (old) { |
Jan Engelhardt | 23545c2 | 2008-02-14 04:23:04 +0100 | [diff] [blame] | 857 | if (old->revision == me->revision && |
| 858 | old->family == me->family) { |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 859 | fprintf(stderr, |
| 860 | "%s: match `%s' already registered.\n", |
Jamal Hadi Salim | 5dd19de | 2009-02-13 10:42:24 -0500 | [diff] [blame] | 861 | xt_params->program_name, me->name); |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 862 | exit(1); |
| 863 | } |
| 864 | |
| 865 | /* Now we have two (or more) options, check compatibility. */ |
| 866 | if (compatible_match_revision(old->name, old->revision) |
| 867 | && old->revision > me->revision) |
| 868 | return; |
| 869 | |
Jan Engelhardt | 23545c2 | 2008-02-14 04:23:04 +0100 | [diff] [blame] | 870 | /* See if new match can be used. */ |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 871 | if (!compatible_match_revision(me->name, me->revision)) |
| 872 | return; |
| 873 | |
Jan Engelhardt | 23545c2 | 2008-02-14 04:23:04 +0100 | [diff] [blame] | 874 | /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */ |
| 875 | if (old->revision == me->revision && me->family == AF_UNSPEC) |
| 876 | return; |
| 877 | |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 878 | /* Delete old one. */ |
| 879 | for (i = &xtables_matches; *i!=old; i = &(*i)->next); |
| 880 | *i = old->next; |
| 881 | } |
| 882 | |
| 883 | if (me->size != XT_ALIGN(me->size)) { |
| 884 | fprintf(stderr, "%s: match `%s' has invalid size %u.\n", |
Jamal Hadi Salim | 5dd19de | 2009-02-13 10:42:24 -0500 | [diff] [blame] | 885 | xt_params->program_name, me->name, |
| 886 | (unsigned int)me->size); |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 887 | exit(1); |
| 888 | } |
| 889 | |
| 890 | /* Append to list. */ |
| 891 | for (i = &xtables_matches; *i; i = &(*i)->next); |
| 892 | me->next = NULL; |
| 893 | *i = me; |
| 894 | |
| 895 | me->m = NULL; |
| 896 | me->mflags = 0; |
| 897 | } |
| 898 | |
Jan Engelhardt | 9a8fc4f | 2009-06-25 17:13:46 +0200 | [diff] [blame] | 899 | void xtables_register_matches(struct xtables_match *match, unsigned int n) |
| 900 | { |
| 901 | do { |
| 902 | xtables_register_match(&match[--n]); |
| 903 | } while (n > 0); |
| 904 | } |
| 905 | |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 906 | void xtables_register_target(struct xtables_target *me) |
| 907 | { |
Jan Engelhardt | c284de5 | 2009-06-25 21:25:24 +0200 | [diff] [blame] | 908 | if (me->version == NULL) { |
| 909 | fprintf(stderr, "%s: target %s<%u> is missing a version\n", |
| 910 | xt_params->program_name, me->name, me->revision); |
| 911 | exit(1); |
| 912 | } |
Jan Engelhardt | dacafa5 | 2009-01-27 20:56:23 +0100 | [diff] [blame] | 913 | if (strcmp(me->version, XTABLES_VERSION) != 0) { |
| 914 | fprintf(stderr, "%s: target \"%s\" has version \"%s\", " |
| 915 | "but \"%s\" is required.\n", |
Jamal Hadi Salim | 5dd19de | 2009-02-13 10:42:24 -0500 | [diff] [blame] | 916 | xt_params->program_name, me->name, |
Jan Engelhardt | dacafa5 | 2009-01-27 20:56:23 +0100 | [diff] [blame] | 917 | me->version, XTABLES_VERSION); |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 918 | exit(1); |
| 919 | } |
| 920 | |
Jan Engelhardt | 0cb675b | 2010-06-07 11:50:25 +0200 | [diff] [blame] | 921 | if (strlen(me->name) >= XT_EXTENSION_MAXNAMELEN) { |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 922 | fprintf(stderr, "%s: target `%s' has invalid name\n", |
Jamal Hadi Salim | 5dd19de | 2009-02-13 10:42:24 -0500 | [diff] [blame] | 923 | xt_params->program_name, me->name); |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 924 | exit(1); |
| 925 | } |
| 926 | |
| 927 | if (me->family >= NPROTO) { |
| 928 | fprintf(stderr, |
| 929 | "%s: BUG: target %s has invalid protocol family\n", |
Jamal Hadi Salim | 5dd19de | 2009-02-13 10:42:24 -0500 | [diff] [blame] | 930 | xt_params->program_name, me->name); |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 931 | exit(1); |
| 932 | } |
| 933 | |
Jan Engelhardt | aa37acc | 2011-02-07 04:00:50 +0100 | [diff] [blame] | 934 | if (me->x6_options != NULL) |
| 935 | xtables_option_metavalidate(me->name, me->x6_options); |
Jan Engelhardt | dfbedfe | 2011-01-08 03:31:04 +0100 | [diff] [blame] | 936 | if (me->extra_opts != NULL) |
| 937 | xtables_check_options(me->name, me->extra_opts); |
| 938 | |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 939 | /* ignore not interested target */ |
Jan Engelhardt | 77f48c2 | 2009-02-07 19:59:53 +0100 | [diff] [blame] | 940 | if (me->family != afinfo->family && me->family != AF_UNSPEC) |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 941 | return; |
| 942 | |
Maciej Zenczykowski | 2c6ac07 | 2011-04-04 15:31:43 +0200 | [diff] [blame] | 943 | /* place on linked list of targets pending full registration */ |
| 944 | me->next = xtables_pending_targets; |
| 945 | xtables_pending_targets = me; |
| 946 | } |
| 947 | |
| 948 | static void xtables_fully_register_pending_target(struct xtables_target *me) |
| 949 | { |
| 950 | struct xtables_target *old; |
| 951 | |
Jan Engelhardt | 2338efd | 2009-01-27 15:23:01 +0100 | [diff] [blame] | 952 | old = xtables_find_target(me->name, XTF_DURING_LOAD); |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 953 | if (old) { |
| 954 | struct xtables_target **i; |
| 955 | |
Jan Engelhardt | 23545c2 | 2008-02-14 04:23:04 +0100 | [diff] [blame] | 956 | if (old->revision == me->revision && |
| 957 | old->family == me->family) { |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 958 | fprintf(stderr, |
| 959 | "%s: target `%s' already registered.\n", |
Jamal Hadi Salim | 5dd19de | 2009-02-13 10:42:24 -0500 | [diff] [blame] | 960 | xt_params->program_name, me->name); |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 961 | exit(1); |
| 962 | } |
| 963 | |
| 964 | /* Now we have two (or more) options, check compatibility. */ |
| 965 | if (compatible_target_revision(old->name, old->revision) |
| 966 | && old->revision > me->revision) |
| 967 | return; |
| 968 | |
Jan Engelhardt | 23545c2 | 2008-02-14 04:23:04 +0100 | [diff] [blame] | 969 | /* See if new target can be used. */ |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 970 | if (!compatible_target_revision(me->name, me->revision)) |
| 971 | return; |
| 972 | |
Jan Engelhardt | 23545c2 | 2008-02-14 04:23:04 +0100 | [diff] [blame] | 973 | /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */ |
| 974 | if (old->revision == me->revision && me->family == AF_UNSPEC) |
| 975 | return; |
| 976 | |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 977 | /* Delete old one. */ |
| 978 | for (i = &xtables_targets; *i!=old; i = &(*i)->next); |
| 979 | *i = old->next; |
| 980 | } |
| 981 | |
| 982 | if (me->size != XT_ALIGN(me->size)) { |
| 983 | fprintf(stderr, "%s: target `%s' has invalid size %u.\n", |
Jamal Hadi Salim | 5dd19de | 2009-02-13 10:42:24 -0500 | [diff] [blame] | 984 | xt_params->program_name, me->name, |
| 985 | (unsigned int)me->size); |
Yasuyuki KOZAKAI | 0d502bc | 2007-07-24 05:52:07 +0000 | [diff] [blame] | 986 | exit(1); |
| 987 | } |
| 988 | |
| 989 | /* Prepend to list. */ |
| 990 | me->next = xtables_targets; |
| 991 | xtables_targets = me; |
| 992 | me->t = NULL; |
| 993 | me->tflags = 0; |
| 994 | } |
Jan Engelhardt | aafd269 | 2008-01-20 13:19:40 +0000 | [diff] [blame] | 995 | |
Jan Engelhardt | 9a8fc4f | 2009-06-25 17:13:46 +0200 | [diff] [blame] | 996 | void xtables_register_targets(struct xtables_target *target, unsigned int n) |
| 997 | { |
| 998 | do { |
| 999 | xtables_register_target(&target[--n]); |
| 1000 | } while (n > 0); |
| 1001 | } |
| 1002 | |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 1003 | /** |
| 1004 | * xtables_param_act - act on condition |
| 1005 | * @status: a constant from enum xtables_exittype |
| 1006 | * |
| 1007 | * %XTF_ONLY_ONCE: print error message that option may only be used once. |
| 1008 | * @p1: module name (e.g. "mark") |
| 1009 | * @p2(...): option in conflict (e.g. "--mark") |
| 1010 | * @p3(...): condition to match on (see extensions/ for examples) |
| 1011 | * |
| 1012 | * %XTF_NO_INVERT: option does not support inversion |
| 1013 | * @p1: module name |
| 1014 | * @p2: option in conflict |
| 1015 | * @p3: condition to match on |
| 1016 | * |
| 1017 | * %XTF_BAD_VALUE: bad value for option |
| 1018 | * @p1: module name |
| 1019 | * @p2: option with which the problem occured (e.g. "--mark") |
| 1020 | * @p3: string the user passed in (e.g. "99999999999999") |
| 1021 | * |
| 1022 | * %XTF_ONE_ACTION: two mutually exclusive actions have been specified |
| 1023 | * @p1: module name |
| 1024 | * |
| 1025 | * Displays an error message and exits the program. |
| 1026 | */ |
| 1027 | void xtables_param_act(unsigned int status, const char *p1, ...) |
Jan Engelhardt | aafd269 | 2008-01-20 13:19:40 +0000 | [diff] [blame] | 1028 | { |
| 1029 | const char *p2, *p3; |
| 1030 | va_list args; |
| 1031 | bool b; |
| 1032 | |
| 1033 | va_start(args, p1); |
| 1034 | |
| 1035 | switch (status) { |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 1036 | case XTF_ONLY_ONCE: |
Jan Engelhardt | aafd269 | 2008-01-20 13:19:40 +0000 | [diff] [blame] | 1037 | p2 = va_arg(args, const char *); |
| 1038 | b = va_arg(args, unsigned int); |
| 1039 | if (!b) |
| 1040 | return; |
Jamal Hadi Salim | 8b7baeb | 2009-02-11 13:05:43 +0100 | [diff] [blame] | 1041 | xt_params->exit_err(PARAMETER_PROBLEM, |
Jan Engelhardt | aafd269 | 2008-01-20 13:19:40 +0000 | [diff] [blame] | 1042 | "%s: \"%s\" option may only be specified once", |
| 1043 | p1, p2); |
| 1044 | break; |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 1045 | case XTF_NO_INVERT: |
Jan Engelhardt | aafd269 | 2008-01-20 13:19:40 +0000 | [diff] [blame] | 1046 | p2 = va_arg(args, const char *); |
| 1047 | b = va_arg(args, unsigned int); |
| 1048 | if (!b) |
| 1049 | return; |
Jamal Hadi Salim | 8b7baeb | 2009-02-11 13:05:43 +0100 | [diff] [blame] | 1050 | xt_params->exit_err(PARAMETER_PROBLEM, |
Jan Engelhardt | aafd269 | 2008-01-20 13:19:40 +0000 | [diff] [blame] | 1051 | "%s: \"%s\" option cannot be inverted", p1, p2); |
| 1052 | break; |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 1053 | case XTF_BAD_VALUE: |
Jan Engelhardt | aafd269 | 2008-01-20 13:19:40 +0000 | [diff] [blame] | 1054 | p2 = va_arg(args, const char *); |
| 1055 | p3 = va_arg(args, const char *); |
Jamal Hadi Salim | 8b7baeb | 2009-02-11 13:05:43 +0100 | [diff] [blame] | 1056 | xt_params->exit_err(PARAMETER_PROBLEM, |
Jan Engelhardt | aafd269 | 2008-01-20 13:19:40 +0000 | [diff] [blame] | 1057 | "%s: Bad value for \"%s\" option: \"%s\"", |
| 1058 | p1, p2, p3); |
| 1059 | break; |
Jan Engelhardt | a41545c | 2009-01-27 21:27:19 +0100 | [diff] [blame] | 1060 | case XTF_ONE_ACTION: |
Jan Engelhardt | aafd269 | 2008-01-20 13:19:40 +0000 | [diff] [blame] | 1061 | b = va_arg(args, unsigned int); |
| 1062 | if (!b) |
| 1063 | return; |
Jamal Hadi Salim | 8b7baeb | 2009-02-11 13:05:43 +0100 | [diff] [blame] | 1064 | xt_params->exit_err(PARAMETER_PROBLEM, |
Jan Engelhardt | aafd269 | 2008-01-20 13:19:40 +0000 | [diff] [blame] | 1065 | "%s: At most one action is possible", p1); |
| 1066 | break; |
| 1067 | default: |
Jamal Hadi Salim | 8b7baeb | 2009-02-11 13:05:43 +0100 | [diff] [blame] | 1068 | xt_params->exit_err(status, p1, args); |
Jan Engelhardt | aafd269 | 2008-01-20 13:19:40 +0000 | [diff] [blame] | 1069 | break; |
| 1070 | } |
| 1071 | |
| 1072 | va_end(args); |
| 1073 | } |
Jan Engelhardt | 08b1616 | 2008-01-20 13:36:08 +0000 | [diff] [blame] | 1074 | |
Jan Engelhardt | e44ea7f | 2009-01-30 03:55:09 +0100 | [diff] [blame] | 1075 | const char *xtables_ipaddr_to_numeric(const struct in_addr *addrp) |
Jan Engelhardt | 08b1616 | 2008-01-20 13:36:08 +0000 | [diff] [blame] | 1076 | { |
| 1077 | static char buf[20]; |
| 1078 | const unsigned char *bytep = (const void *)&addrp->s_addr; |
| 1079 | |
| 1080 | sprintf(buf, "%u.%u.%u.%u", bytep[0], bytep[1], bytep[2], bytep[3]); |
| 1081 | return buf; |
| 1082 | } |
| 1083 | |
| 1084 | static const char *ipaddr_to_host(const struct in_addr *addr) |
| 1085 | { |
| 1086 | struct hostent *host; |
| 1087 | |
| 1088 | host = gethostbyaddr(addr, sizeof(struct in_addr), AF_INET); |
| 1089 | if (host == NULL) |
| 1090 | return NULL; |
| 1091 | |
| 1092 | return host->h_name; |
| 1093 | } |
| 1094 | |
| 1095 | static const char *ipaddr_to_network(const struct in_addr *addr) |
| 1096 | { |
| 1097 | struct netent *net; |
| 1098 | |
| 1099 | if ((net = getnetbyaddr(ntohl(addr->s_addr), AF_INET)) != NULL) |
| 1100 | return net->n_name; |
| 1101 | |
| 1102 | return NULL; |
| 1103 | } |
| 1104 | |
Jan Engelhardt | e44ea7f | 2009-01-30 03:55:09 +0100 | [diff] [blame] | 1105 | const char *xtables_ipaddr_to_anyname(const struct in_addr *addr) |
Jan Engelhardt | 08b1616 | 2008-01-20 13:36:08 +0000 | [diff] [blame] | 1106 | { |
| 1107 | const char *name; |
| 1108 | |
| 1109 | if ((name = ipaddr_to_host(addr)) != NULL || |
| 1110 | (name = ipaddr_to_network(addr)) != NULL) |
| 1111 | return name; |
| 1112 | |
Jan Engelhardt | e44ea7f | 2009-01-30 03:55:09 +0100 | [diff] [blame] | 1113 | return xtables_ipaddr_to_numeric(addr); |
Jan Engelhardt | 08b1616 | 2008-01-20 13:36:08 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
Jan Engelhardt | e44ea7f | 2009-01-30 03:55:09 +0100 | [diff] [blame] | 1116 | const char *xtables_ipmask_to_numeric(const struct in_addr *mask) |
Jan Engelhardt | 08b1616 | 2008-01-20 13:36:08 +0000 | [diff] [blame] | 1117 | { |
| 1118 | static char buf[20]; |
| 1119 | uint32_t maskaddr, bits; |
| 1120 | int i; |
| 1121 | |
| 1122 | maskaddr = ntohl(mask->s_addr); |
| 1123 | |
| 1124 | if (maskaddr == 0xFFFFFFFFL) |
| 1125 | /* we don't want to see "/32" */ |
| 1126 | return ""; |
| 1127 | |
| 1128 | i = 32; |
| 1129 | bits = 0xFFFFFFFEL; |
| 1130 | while (--i >= 0 && maskaddr != bits) |
| 1131 | bits <<= 1; |
| 1132 | if (i >= 0) |
| 1133 | sprintf(buf, "/%d", i); |
| 1134 | else |
| 1135 | /* mask was not a decent combination of 1's and 0's */ |
Jan Engelhardt | e44ea7f | 2009-01-30 03:55:09 +0100 | [diff] [blame] | 1136 | sprintf(buf, "/%s", xtables_ipaddr_to_numeric(mask)); |
Jan Engelhardt | 08b1616 | 2008-01-20 13:36:08 +0000 | [diff] [blame] | 1137 | |
| 1138 | return buf; |
| 1139 | } |
| 1140 | |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1141 | static struct in_addr *__numeric_to_ipaddr(const char *dotted, bool is_mask) |
| 1142 | { |
| 1143 | static struct in_addr addr; |
| 1144 | unsigned char *addrp; |
| 1145 | unsigned int onebyte; |
| 1146 | char buf[20], *p, *q; |
| 1147 | int i; |
| 1148 | |
| 1149 | /* copy dotted string, because we need to modify it */ |
| 1150 | strncpy(buf, dotted, sizeof(buf) - 1); |
| 1151 | buf[sizeof(buf) - 1] = '\0'; |
| 1152 | addrp = (void *)&addr.s_addr; |
| 1153 | |
| 1154 | p = buf; |
| 1155 | for (i = 0; i < 3; ++i) { |
| 1156 | if ((q = strchr(p, '.')) == NULL) { |
| 1157 | if (is_mask) |
| 1158 | return NULL; |
| 1159 | |
| 1160 | /* autocomplete, this is a network address */ |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 1161 | if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX)) |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1162 | return NULL; |
| 1163 | |
| 1164 | addrp[i] = onebyte; |
| 1165 | while (i < 3) |
| 1166 | addrp[++i] = 0; |
| 1167 | |
| 1168 | return &addr; |
| 1169 | } |
| 1170 | |
| 1171 | *q = '\0'; |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 1172 | if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX)) |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1173 | return NULL; |
| 1174 | |
| 1175 | addrp[i] = onebyte; |
| 1176 | p = q + 1; |
| 1177 | } |
| 1178 | |
| 1179 | /* we have checked 3 bytes, now we check the last one */ |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 1180 | if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX)) |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1181 | return NULL; |
| 1182 | |
| 1183 | addrp[3] = onebyte; |
| 1184 | return &addr; |
| 1185 | } |
| 1186 | |
Jan Engelhardt | 1e01b0b | 2009-01-30 04:20:32 +0100 | [diff] [blame] | 1187 | struct in_addr *xtables_numeric_to_ipaddr(const char *dotted) |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1188 | { |
| 1189 | return __numeric_to_ipaddr(dotted, false); |
| 1190 | } |
| 1191 | |
Jan Engelhardt | 1e01b0b | 2009-01-30 04:20:32 +0100 | [diff] [blame] | 1192 | struct in_addr *xtables_numeric_to_ipmask(const char *dotted) |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1193 | { |
| 1194 | return __numeric_to_ipaddr(dotted, true); |
| 1195 | } |
| 1196 | |
| 1197 | static struct in_addr *network_to_ipaddr(const char *name) |
| 1198 | { |
| 1199 | static struct in_addr addr; |
| 1200 | struct netent *net; |
| 1201 | |
| 1202 | if ((net = getnetbyname(name)) != NULL) { |
| 1203 | if (net->n_addrtype != AF_INET) |
| 1204 | return NULL; |
| 1205 | addr.s_addr = htonl(net->n_net); |
| 1206 | return &addr; |
| 1207 | } |
| 1208 | |
| 1209 | return NULL; |
| 1210 | } |
| 1211 | |
| 1212 | static struct in_addr *host_to_ipaddr(const char *name, unsigned int *naddr) |
| 1213 | { |
| 1214 | struct hostent *host; |
| 1215 | struct in_addr *addr; |
| 1216 | unsigned int i; |
| 1217 | |
| 1218 | *naddr = 0; |
| 1219 | if ((host = gethostbyname(name)) != NULL) { |
| 1220 | if (host->h_addrtype != AF_INET || |
| 1221 | host->h_length != sizeof(struct in_addr)) |
| 1222 | return NULL; |
| 1223 | |
| 1224 | while (host->h_addr_list[*naddr] != NULL) |
| 1225 | ++*naddr; |
Wes Campaigne | 11e250b | 2011-02-21 19:10:11 -0500 | [diff] [blame] | 1226 | addr = xtables_calloc(*naddr, sizeof(struct in_addr)); |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1227 | for (i = 0; i < *naddr; i++) |
| 1228 | memcpy(&addr[i], host->h_addr_list[i], |
| 1229 | sizeof(struct in_addr)); |
| 1230 | return addr; |
| 1231 | } |
| 1232 | |
| 1233 | return NULL; |
| 1234 | } |
| 1235 | |
| 1236 | static struct in_addr * |
| 1237 | ipparse_hostnetwork(const char *name, unsigned int *naddrs) |
| 1238 | { |
| 1239 | struct in_addr *addrptmp, *addrp; |
| 1240 | |
Jan Engelhardt | 1e01b0b | 2009-01-30 04:20:32 +0100 | [diff] [blame] | 1241 | if ((addrptmp = xtables_numeric_to_ipaddr(name)) != NULL || |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1242 | (addrptmp = network_to_ipaddr(name)) != NULL) { |
Jan Engelhardt | 630ef48 | 2009-01-27 14:58:41 +0100 | [diff] [blame] | 1243 | addrp = xtables_malloc(sizeof(struct in_addr)); |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1244 | memcpy(addrp, addrptmp, sizeof(*addrp)); |
| 1245 | *naddrs = 1; |
| 1246 | return addrp; |
| 1247 | } |
| 1248 | if ((addrptmp = host_to_ipaddr(name, naddrs)) != NULL) |
| 1249 | return addrptmp; |
| 1250 | |
Jamal Hadi Salim | 8b7baeb | 2009-02-11 13:05:43 +0100 | [diff] [blame] | 1251 | xt_params->exit_err(PARAMETER_PROBLEM, "host/network `%s' not found", name); |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | static struct in_addr *parse_ipmask(const char *mask) |
| 1255 | { |
| 1256 | static struct in_addr maskaddr; |
| 1257 | struct in_addr *addrp; |
| 1258 | unsigned int bits; |
| 1259 | |
| 1260 | if (mask == NULL) { |
| 1261 | /* no mask at all defaults to 32 bits */ |
| 1262 | maskaddr.s_addr = 0xFFFFFFFF; |
| 1263 | return &maskaddr; |
| 1264 | } |
Jan Engelhardt | 1e01b0b | 2009-01-30 04:20:32 +0100 | [diff] [blame] | 1265 | if ((addrp = xtables_numeric_to_ipmask(mask)) != NULL) |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1266 | /* dotted_to_addr already returns a network byte order addr */ |
| 1267 | return addrp; |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 1268 | if (!xtables_strtoui(mask, NULL, &bits, 0, 32)) |
Jamal Hadi Salim | 8b7baeb | 2009-02-11 13:05:43 +0100 | [diff] [blame] | 1269 | xt_params->exit_err(PARAMETER_PROBLEM, |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1270 | "invalid mask `%s' specified", mask); |
| 1271 | if (bits != 0) { |
| 1272 | maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits)); |
| 1273 | return &maskaddr; |
| 1274 | } |
| 1275 | |
| 1276 | maskaddr.s_addr = 0U; |
| 1277 | return &maskaddr; |
| 1278 | } |
| 1279 | |
Michael Granzow | 332e4ac | 2009-04-09 18:24:36 +0100 | [diff] [blame] | 1280 | void xtables_ipparse_multiple(const char *name, struct in_addr **addrpp, |
| 1281 | struct in_addr **maskpp, unsigned int *naddrs) |
| 1282 | { |
| 1283 | struct in_addr *addrp; |
| 1284 | char buf[256], *p; |
| 1285 | unsigned int len, i, j, n, count = 1; |
| 1286 | const char *loop = name; |
| 1287 | |
| 1288 | while ((loop = strchr(loop, ',')) != NULL) { |
| 1289 | ++count; |
| 1290 | ++loop; /* skip ',' */ |
| 1291 | } |
| 1292 | |
| 1293 | *addrpp = xtables_malloc(sizeof(struct in_addr) * count); |
| 1294 | *maskpp = xtables_malloc(sizeof(struct in_addr) * count); |
| 1295 | |
| 1296 | loop = name; |
| 1297 | |
| 1298 | for (i = 0; i < count; ++i) { |
| 1299 | if (loop == NULL) |
| 1300 | break; |
| 1301 | if (*loop == ',') |
| 1302 | ++loop; |
| 1303 | if (*loop == '\0') |
| 1304 | break; |
| 1305 | p = strchr(loop, ','); |
| 1306 | if (p != NULL) |
| 1307 | len = p - loop; |
| 1308 | else |
| 1309 | len = strlen(loop); |
| 1310 | if (len == 0 || sizeof(buf) - 1 < len) |
| 1311 | break; |
| 1312 | |
| 1313 | strncpy(buf, loop, len); |
| 1314 | buf[len] = '\0'; |
| 1315 | loop += len; |
| 1316 | if ((p = strrchr(buf, '/')) != NULL) { |
| 1317 | *p = '\0'; |
| 1318 | addrp = parse_ipmask(p + 1); |
| 1319 | } else { |
| 1320 | addrp = parse_ipmask(NULL); |
| 1321 | } |
| 1322 | memcpy(*maskpp + i, addrp, sizeof(*addrp)); |
| 1323 | |
| 1324 | /* if a null mask is given, the name is ignored, like in "any/0" */ |
| 1325 | if ((*maskpp + i)->s_addr == 0) |
| 1326 | /* |
| 1327 | * A bit pointless to process multiple addresses |
| 1328 | * in this case... |
| 1329 | */ |
| 1330 | strcpy(buf, "0.0.0.0"); |
| 1331 | |
| 1332 | addrp = ipparse_hostnetwork(buf, &n); |
| 1333 | if (n > 1) { |
| 1334 | count += n - 1; |
| 1335 | *addrpp = xtables_realloc(*addrpp, |
| 1336 | sizeof(struct in_addr) * count); |
| 1337 | *maskpp = xtables_realloc(*maskpp, |
| 1338 | sizeof(struct in_addr) * count); |
| 1339 | for (j = 0; j < n; ++j) |
| 1340 | /* for each new addr */ |
| 1341 | memcpy(*addrpp + i + j, addrp + j, |
| 1342 | sizeof(*addrp)); |
| 1343 | for (j = 1; j < n; ++j) |
| 1344 | /* for each new mask */ |
| 1345 | memcpy(*maskpp + i + j, *maskpp + i, |
| 1346 | sizeof(*addrp)); |
| 1347 | i += n - 1; |
| 1348 | } else { |
| 1349 | memcpy(*addrpp + i, addrp, sizeof(*addrp)); |
| 1350 | } |
| 1351 | /* free what ipparse_hostnetwork had allocated: */ |
| 1352 | free(addrp); |
| 1353 | } |
| 1354 | *naddrs = count; |
Jan Engelhardt | 4b110b4 | 2011-02-21 03:21:18 +0100 | [diff] [blame] | 1355 | for (i = 0; i < count; ++i) |
Michael Granzow | 332e4ac | 2009-04-09 18:24:36 +0100 | [diff] [blame] | 1356 | (*addrpp+i)->s_addr &= (*maskpp+i)->s_addr; |
| 1357 | } |
| 1358 | |
| 1359 | |
Jan Engelhardt | a0baae8 | 2009-01-30 04:32:50 +0100 | [diff] [blame] | 1360 | /** |
| 1361 | * xtables_ipparse_any - transform arbitrary name to in_addr |
| 1362 | * |
| 1363 | * Possible inputs (pseudo regex): |
| 1364 | * m{^($hostname|$networkname|$ipaddr)(/$mask)?} |
| 1365 | * "1.2.3.4/5", "1.2.3.4", "hostname", "networkname" |
| 1366 | */ |
| 1367 | void xtables_ipparse_any(const char *name, struct in_addr **addrpp, |
| 1368 | struct in_addr *maskp, unsigned int *naddrs) |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1369 | { |
| 1370 | unsigned int i, j, k, n; |
| 1371 | struct in_addr *addrp; |
| 1372 | char buf[256], *p; |
| 1373 | |
| 1374 | strncpy(buf, name, sizeof(buf) - 1); |
| 1375 | buf[sizeof(buf) - 1] = '\0'; |
| 1376 | if ((p = strrchr(buf, '/')) != NULL) { |
| 1377 | *p = '\0'; |
| 1378 | addrp = parse_ipmask(p + 1); |
| 1379 | } else { |
| 1380 | addrp = parse_ipmask(NULL); |
| 1381 | } |
| 1382 | memcpy(maskp, addrp, sizeof(*maskp)); |
| 1383 | |
| 1384 | /* if a null mask is given, the name is ignored, like in "any/0" */ |
| 1385 | if (maskp->s_addr == 0U) |
| 1386 | strcpy(buf, "0.0.0.0"); |
| 1387 | |
| 1388 | addrp = *addrpp = ipparse_hostnetwork(buf, naddrs); |
| 1389 | n = *naddrs; |
| 1390 | for (i = 0, j = 0; i < n; ++i) { |
| 1391 | addrp[j++].s_addr &= maskp->s_addr; |
| 1392 | for (k = 0; k < j - 1; ++k) |
| 1393 | if (addrp[k].s_addr == addrp[j-1].s_addr) { |
Wes Campaigne | adcb281 | 2011-02-21 19:10:12 -0500 | [diff] [blame] | 1394 | /* |
| 1395 | * Nuke the dup by copying an address from the |
| 1396 | * tail here, and check the current position |
| 1397 | * again (--j). |
| 1398 | */ |
| 1399 | memcpy(&addrp[--j], &addrp[--*naddrs], |
| 1400 | sizeof(struct in_addr)); |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1401 | break; |
| 1402 | } |
| 1403 | } |
| 1404 | } |
| 1405 | |
Jan Engelhardt | e44ea7f | 2009-01-30 03:55:09 +0100 | [diff] [blame] | 1406 | const char *xtables_ip6addr_to_numeric(const struct in6_addr *addrp) |
Jan Engelhardt | 08b1616 | 2008-01-20 13:36:08 +0000 | [diff] [blame] | 1407 | { |
Maciej Zenczykowski | cf3e52d | 2011-04-04 15:31:09 +0200 | [diff] [blame] | 1408 | /* 0000:0000:0000:0000:0000:0000:000.000.000.000 |
Jan Engelhardt | 08b1616 | 2008-01-20 13:36:08 +0000 | [diff] [blame] | 1409 | * 0000:0000:0000:0000:0000:0000:0000:0000 */ |
| 1410 | static char buf[50+1]; |
| 1411 | return inet_ntop(AF_INET6, addrp, buf, sizeof(buf)); |
| 1412 | } |
| 1413 | |
| 1414 | static const char *ip6addr_to_host(const struct in6_addr *addr) |
| 1415 | { |
| 1416 | static char hostname[NI_MAXHOST]; |
| 1417 | struct sockaddr_in6 saddr; |
| 1418 | int err; |
| 1419 | |
| 1420 | memset(&saddr, 0, sizeof(struct sockaddr_in6)); |
| 1421 | memcpy(&saddr.sin6_addr, addr, sizeof(*addr)); |
| 1422 | saddr.sin6_family = AF_INET6; |
| 1423 | |
| 1424 | err = getnameinfo((const void *)&saddr, sizeof(struct sockaddr_in6), |
| 1425 | hostname, sizeof(hostname) - 1, NULL, 0, 0); |
| 1426 | if (err != 0) { |
| 1427 | #ifdef DEBUG |
| 1428 | fprintf(stderr,"IP2Name: %s\n",gai_strerror(err)); |
| 1429 | #endif |
| 1430 | return NULL; |
| 1431 | } |
| 1432 | |
| 1433 | #ifdef DEBUG |
| 1434 | fprintf (stderr, "\naddr2host: %s\n", hostname); |
| 1435 | #endif |
| 1436 | return hostname; |
| 1437 | } |
| 1438 | |
Jan Engelhardt | e44ea7f | 2009-01-30 03:55:09 +0100 | [diff] [blame] | 1439 | const char *xtables_ip6addr_to_anyname(const struct in6_addr *addr) |
Jan Engelhardt | 08b1616 | 2008-01-20 13:36:08 +0000 | [diff] [blame] | 1440 | { |
| 1441 | const char *name; |
| 1442 | |
| 1443 | if ((name = ip6addr_to_host(addr)) != NULL) |
| 1444 | return name; |
| 1445 | |
Jan Engelhardt | e44ea7f | 2009-01-30 03:55:09 +0100 | [diff] [blame] | 1446 | return xtables_ip6addr_to_numeric(addr); |
Jan Engelhardt | 08b1616 | 2008-01-20 13:36:08 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | static int ip6addr_prefix_length(const struct in6_addr *k) |
| 1450 | { |
| 1451 | unsigned int bits = 0; |
| 1452 | uint32_t a, b, c, d; |
| 1453 | |
Jan Engelhardt | 4860781 | 2008-06-10 15:17:53 +0200 | [diff] [blame] | 1454 | a = ntohl(k->s6_addr32[0]); |
| 1455 | b = ntohl(k->s6_addr32[1]); |
| 1456 | c = ntohl(k->s6_addr32[2]); |
| 1457 | d = ntohl(k->s6_addr32[3]); |
Jan Engelhardt | 08b1616 | 2008-01-20 13:36:08 +0000 | [diff] [blame] | 1458 | while (a & 0x80000000U) { |
| 1459 | ++bits; |
| 1460 | a <<= 1; |
| 1461 | a |= (b >> 31) & 1; |
| 1462 | b <<= 1; |
| 1463 | b |= (c >> 31) & 1; |
| 1464 | c <<= 1; |
| 1465 | c |= (d >> 31) & 1; |
| 1466 | d <<= 1; |
| 1467 | } |
| 1468 | if (a != 0 || b != 0 || c != 0 || d != 0) |
| 1469 | return -1; |
| 1470 | return bits; |
| 1471 | } |
| 1472 | |
Jan Engelhardt | e44ea7f | 2009-01-30 03:55:09 +0100 | [diff] [blame] | 1473 | const char *xtables_ip6mask_to_numeric(const struct in6_addr *addrp) |
Jan Engelhardt | 08b1616 | 2008-01-20 13:36:08 +0000 | [diff] [blame] | 1474 | { |
| 1475 | static char buf[50+2]; |
| 1476 | int l = ip6addr_prefix_length(addrp); |
| 1477 | |
| 1478 | if (l == -1) { |
| 1479 | strcpy(buf, "/"); |
Jan Engelhardt | e44ea7f | 2009-01-30 03:55:09 +0100 | [diff] [blame] | 1480 | strcat(buf, xtables_ip6addr_to_numeric(addrp)); |
Jan Engelhardt | 08b1616 | 2008-01-20 13:36:08 +0000 | [diff] [blame] | 1481 | return buf; |
| 1482 | } |
| 1483 | sprintf(buf, "/%d", l); |
| 1484 | return buf; |
| 1485 | } |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1486 | |
Jan Engelhardt | 1e01b0b | 2009-01-30 04:20:32 +0100 | [diff] [blame] | 1487 | struct in6_addr *xtables_numeric_to_ip6addr(const char *num) |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1488 | { |
| 1489 | static struct in6_addr ap; |
| 1490 | int err; |
| 1491 | |
| 1492 | if ((err = inet_pton(AF_INET6, num, &ap)) == 1) |
| 1493 | return ≈ |
| 1494 | #ifdef DEBUG |
| 1495 | fprintf(stderr, "\nnumeric2addr: %d\n", err); |
| 1496 | #endif |
| 1497 | return NULL; |
| 1498 | } |
| 1499 | |
| 1500 | static struct in6_addr * |
| 1501 | host_to_ip6addr(const char *name, unsigned int *naddr) |
| 1502 | { |
Wes Campaigne | 2ad8dc8 | 2011-02-21 19:10:10 -0500 | [diff] [blame] | 1503 | struct in6_addr *addr; |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1504 | struct addrinfo hints; |
Wes Campaigne | 2ad8dc8 | 2011-02-21 19:10:10 -0500 | [diff] [blame] | 1505 | struct addrinfo *res, *p; |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1506 | int err; |
Wes Campaigne | 2ad8dc8 | 2011-02-21 19:10:10 -0500 | [diff] [blame] | 1507 | unsigned int i; |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1508 | |
| 1509 | memset(&hints, 0, sizeof(hints)); |
| 1510 | hints.ai_flags = AI_CANONNAME; |
| 1511 | hints.ai_family = AF_INET6; |
| 1512 | hints.ai_socktype = SOCK_RAW; |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1513 | |
| 1514 | *naddr = 0; |
| 1515 | if ((err = getaddrinfo(name, NULL, &hints, &res)) != 0) { |
| 1516 | #ifdef DEBUG |
| 1517 | fprintf(stderr,"Name2IP: %s\n",gai_strerror(err)); |
| 1518 | #endif |
| 1519 | return NULL; |
| 1520 | } else { |
Wes Campaigne | 2ad8dc8 | 2011-02-21 19:10:10 -0500 | [diff] [blame] | 1521 | /* Find length of address chain */ |
| 1522 | for (p = res; p != NULL; p = p->ai_next) |
| 1523 | ++*naddr; |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1524 | #ifdef DEBUG |
| 1525 | fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen, |
Patrick McHardy | 30290ae | 2010-05-20 15:41:03 +0200 | [diff] [blame] | 1526 | xtables_ip6addr_to_numeric(&((struct sockaddr_in6 *)res->ai_addr)->sin6_addr)); |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1527 | #endif |
Wes Campaigne | 2ad8dc8 | 2011-02-21 19:10:10 -0500 | [diff] [blame] | 1528 | /* Copy each element of the address chain */ |
| 1529 | addr = xtables_calloc(*naddr, sizeof(struct in6_addr)); |
| 1530 | for (i = 0, p = res; p != NULL; p = p->ai_next) |
| 1531 | memcpy(&addr[i++], |
| 1532 | &((const struct sockaddr_in6 *)p->ai_addr)->sin6_addr, |
| 1533 | sizeof(struct in6_addr)); |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1534 | freeaddrinfo(res); |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1535 | return addr; |
| 1536 | } |
| 1537 | |
| 1538 | return NULL; |
| 1539 | } |
| 1540 | |
| 1541 | static struct in6_addr *network_to_ip6addr(const char *name) |
| 1542 | { |
| 1543 | /* abort();*/ |
| 1544 | /* TODO: not implemented yet, but the exception breaks the |
| 1545 | * name resolvation */ |
| 1546 | return NULL; |
| 1547 | } |
| 1548 | |
| 1549 | static struct in6_addr * |
| 1550 | ip6parse_hostnetwork(const char *name, unsigned int *naddrs) |
| 1551 | { |
| 1552 | struct in6_addr *addrp, *addrptmp; |
| 1553 | |
Jan Engelhardt | 1e01b0b | 2009-01-30 04:20:32 +0100 | [diff] [blame] | 1554 | if ((addrptmp = xtables_numeric_to_ip6addr(name)) != NULL || |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1555 | (addrptmp = network_to_ip6addr(name)) != NULL) { |
Jan Engelhardt | 630ef48 | 2009-01-27 14:58:41 +0100 | [diff] [blame] | 1556 | addrp = xtables_malloc(sizeof(struct in6_addr)); |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1557 | memcpy(addrp, addrptmp, sizeof(*addrp)); |
| 1558 | *naddrs = 1; |
| 1559 | return addrp; |
| 1560 | } |
| 1561 | if ((addrp = host_to_ip6addr(name, naddrs)) != NULL) |
| 1562 | return addrp; |
| 1563 | |
Jamal Hadi Salim | 8b7baeb | 2009-02-11 13:05:43 +0100 | [diff] [blame] | 1564 | xt_params->exit_err(PARAMETER_PROBLEM, "host/network `%s' not found", name); |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
| 1567 | static struct in6_addr *parse_ip6mask(char *mask) |
| 1568 | { |
| 1569 | static struct in6_addr maskaddr; |
| 1570 | struct in6_addr *addrp; |
| 1571 | unsigned int bits; |
| 1572 | |
| 1573 | if (mask == NULL) { |
| 1574 | /* no mask at all defaults to 128 bits */ |
| 1575 | memset(&maskaddr, 0xff, sizeof maskaddr); |
| 1576 | return &maskaddr; |
| 1577 | } |
Jan Engelhardt | 1e01b0b | 2009-01-30 04:20:32 +0100 | [diff] [blame] | 1578 | if ((addrp = xtables_numeric_to_ip6addr(mask)) != NULL) |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1579 | return addrp; |
Jan Engelhardt | 5f2922c | 2009-01-27 18:43:01 +0100 | [diff] [blame] | 1580 | if (!xtables_strtoui(mask, NULL, &bits, 0, 128)) |
Jamal Hadi Salim | 8b7baeb | 2009-02-11 13:05:43 +0100 | [diff] [blame] | 1581 | xt_params->exit_err(PARAMETER_PROBLEM, |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1582 | "invalid mask `%s' specified", mask); |
| 1583 | if (bits != 0) { |
| 1584 | char *p = (void *)&maskaddr; |
| 1585 | memset(p, 0xff, bits / 8); |
| 1586 | memset(p + (bits / 8) + 1, 0, (128 - bits) / 8); |
| 1587 | p[bits/8] = 0xff << (8 - (bits & 7)); |
| 1588 | return &maskaddr; |
| 1589 | } |
| 1590 | |
| 1591 | memset(&maskaddr, 0, sizeof(maskaddr)); |
| 1592 | return &maskaddr; |
| 1593 | } |
| 1594 | |
Michael Granzow | 332e4ac | 2009-04-09 18:24:36 +0100 | [diff] [blame] | 1595 | void |
| 1596 | xtables_ip6parse_multiple(const char *name, struct in6_addr **addrpp, |
| 1597 | struct in6_addr **maskpp, unsigned int *naddrs) |
| 1598 | { |
Olaf Rempel | 58df901 | 2009-09-20 13:24:11 +0200 | [diff] [blame] | 1599 | static const struct in6_addr zero_addr; |
Michael Granzow | 332e4ac | 2009-04-09 18:24:36 +0100 | [diff] [blame] | 1600 | struct in6_addr *addrp; |
| 1601 | char buf[256], *p; |
| 1602 | unsigned int len, i, j, n, count = 1; |
| 1603 | const char *loop = name; |
| 1604 | |
| 1605 | while ((loop = strchr(loop, ',')) != NULL) { |
| 1606 | ++count; |
| 1607 | ++loop; /* skip ',' */ |
| 1608 | } |
| 1609 | |
| 1610 | *addrpp = xtables_malloc(sizeof(struct in6_addr) * count); |
| 1611 | *maskpp = xtables_malloc(sizeof(struct in6_addr) * count); |
| 1612 | |
| 1613 | loop = name; |
| 1614 | |
| 1615 | for (i = 0; i < count /*NB: count can grow*/; ++i) { |
| 1616 | if (loop == NULL) |
| 1617 | break; |
| 1618 | if (*loop == ',') |
| 1619 | ++loop; |
| 1620 | if (*loop == '\0') |
| 1621 | break; |
| 1622 | p = strchr(loop, ','); |
| 1623 | if (p != NULL) |
| 1624 | len = p - loop; |
| 1625 | else |
| 1626 | len = strlen(loop); |
| 1627 | if (len == 0 || sizeof(buf) - 1 < len) |
| 1628 | break; |
| 1629 | |
| 1630 | strncpy(buf, loop, len); |
| 1631 | buf[len] = '\0'; |
| 1632 | loop += len; |
| 1633 | if ((p = strrchr(buf, '/')) != NULL) { |
| 1634 | *p = '\0'; |
| 1635 | addrp = parse_ip6mask(p + 1); |
| 1636 | } else { |
| 1637 | addrp = parse_ip6mask(NULL); |
| 1638 | } |
| 1639 | memcpy(*maskpp + i, addrp, sizeof(*addrp)); |
| 1640 | |
| 1641 | /* if a null mask is given, the name is ignored, like in "any/0" */ |
Olaf Rempel | 58df901 | 2009-09-20 13:24:11 +0200 | [diff] [blame] | 1642 | if (memcmp(*maskpp + i, &zero_addr, sizeof(zero_addr)) == 0) |
Michael Granzow | 332e4ac | 2009-04-09 18:24:36 +0100 | [diff] [blame] | 1643 | strcpy(buf, "::"); |
| 1644 | |
| 1645 | addrp = ip6parse_hostnetwork(buf, &n); |
Michael Granzow | 332e4ac | 2009-04-09 18:24:36 +0100 | [diff] [blame] | 1646 | if (n > 1) { |
| 1647 | count += n - 1; |
| 1648 | *addrpp = xtables_realloc(*addrpp, |
| 1649 | sizeof(struct in6_addr) * count); |
| 1650 | *maskpp = xtables_realloc(*maskpp, |
| 1651 | sizeof(struct in6_addr) * count); |
| 1652 | for (j = 0; j < n; ++j) |
| 1653 | /* for each new addr */ |
| 1654 | memcpy(*addrpp + i + j, addrp + j, |
| 1655 | sizeof(*addrp)); |
| 1656 | for (j = 1; j < n; ++j) |
| 1657 | /* for each new mask */ |
| 1658 | memcpy(*maskpp + i + j, *maskpp + i, |
| 1659 | sizeof(*addrp)); |
| 1660 | i += n - 1; |
| 1661 | } else { |
| 1662 | memcpy(*addrpp + i, addrp, sizeof(*addrp)); |
| 1663 | } |
| 1664 | /* free what ip6parse_hostnetwork had allocated: */ |
| 1665 | free(addrp); |
| 1666 | } |
| 1667 | *naddrs = count; |
Jan Engelhardt | 4b110b4 | 2011-02-21 03:21:18 +0100 | [diff] [blame] | 1668 | for (i = 0; i < count; ++i) |
Michael Granzow | 332e4ac | 2009-04-09 18:24:36 +0100 | [diff] [blame] | 1669 | for (j = 0; j < 4; ++j) |
| 1670 | (*addrpp+i)->s6_addr32[j] &= (*maskpp+i)->s6_addr32[j]; |
| 1671 | } |
| 1672 | |
Jan Engelhardt | a0baae8 | 2009-01-30 04:32:50 +0100 | [diff] [blame] | 1673 | void xtables_ip6parse_any(const char *name, struct in6_addr **addrpp, |
| 1674 | struct in6_addr *maskp, unsigned int *naddrs) |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1675 | { |
Jan Engelhardt | 9c0fa7d | 2009-04-03 22:40:35 +0200 | [diff] [blame] | 1676 | static const struct in6_addr zero_addr; |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1677 | struct in6_addr *addrp; |
| 1678 | unsigned int i, j, k, n; |
| 1679 | char buf[256], *p; |
| 1680 | |
| 1681 | strncpy(buf, name, sizeof(buf) - 1); |
| 1682 | buf[sizeof(buf)-1] = '\0'; |
| 1683 | if ((p = strrchr(buf, '/')) != NULL) { |
| 1684 | *p = '\0'; |
| 1685 | addrp = parse_ip6mask(p + 1); |
| 1686 | } else { |
| 1687 | addrp = parse_ip6mask(NULL); |
| 1688 | } |
| 1689 | memcpy(maskp, addrp, sizeof(*maskp)); |
| 1690 | |
| 1691 | /* if a null mask is given, the name is ignored, like in "any/0" */ |
Jan Engelhardt | 9c0fa7d | 2009-04-03 22:40:35 +0200 | [diff] [blame] | 1692 | if (memcmp(maskp, &zero_addr, sizeof(zero_addr)) == 0) |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1693 | strcpy(buf, "::"); |
| 1694 | |
| 1695 | addrp = *addrpp = ip6parse_hostnetwork(buf, naddrs); |
| 1696 | n = *naddrs; |
| 1697 | for (i = 0, j = 0; i < n; ++i) { |
| 1698 | for (k = 0; k < 4; ++k) |
Yasuyuki Kozakai | 5a2208c | 2008-06-04 15:16:03 +0200 | [diff] [blame] | 1699 | addrp[j].s6_addr32[k] &= maskp->s6_addr32[k]; |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1700 | ++j; |
| 1701 | for (k = 0; k < j - 1; ++k) |
| 1702 | if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) { |
Wes Campaigne | adcb281 | 2011-02-21 19:10:12 -0500 | [diff] [blame] | 1703 | /* |
| 1704 | * Nuke the dup by copying an address from the |
| 1705 | * tail here, and check the current position |
| 1706 | * again (--j). |
| 1707 | */ |
| 1708 | memcpy(&addrp[--j], &addrp[--*naddrs], |
| 1709 | sizeof(struct in_addr)); |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 1710 | break; |
| 1711 | } |
| 1712 | } |
| 1713 | } |
Max Kellermann | a5d0994 | 2008-01-29 13:44:34 +0000 | [diff] [blame] | 1714 | |
Jan Engelhardt | a0baae8 | 2009-01-30 04:32:50 +0100 | [diff] [blame] | 1715 | void xtables_save_string(const char *value) |
Max Kellermann | a5d0994 | 2008-01-29 13:44:34 +0000 | [diff] [blame] | 1716 | { |
| 1717 | static const char no_quote_chars[] = "_-0123456789" |
| 1718 | "abcdefghijklmnopqrstuvwxyz" |
| 1719 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 1720 | static const char escape_chars[] = "\"\\'"; |
| 1721 | size_t length; |
| 1722 | const char *p; |
| 1723 | |
Max Kellerman | 87dc7c4 | 2011-02-17 11:57:19 +0100 | [diff] [blame] | 1724 | length = strspn(value, no_quote_chars); |
Max Kellermann | a5d0994 | 2008-01-29 13:44:34 +0000 | [diff] [blame] | 1725 | if (length > 0 && value[length] == 0) { |
| 1726 | /* no quoting required */ |
Max Kellermann | a5d0994 | 2008-01-29 13:44:34 +0000 | [diff] [blame] | 1727 | putchar(' '); |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 1728 | fputs(value, stdout); |
Max Kellermann | a5d0994 | 2008-01-29 13:44:34 +0000 | [diff] [blame] | 1729 | } else { |
| 1730 | /* there is at least one dangerous character in the |
| 1731 | value, which we have to quote. Write double quotes |
| 1732 | around the value and escape special characters with |
| 1733 | a backslash */ |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 1734 | printf(" \""); |
Max Kellermann | a5d0994 | 2008-01-29 13:44:34 +0000 | [diff] [blame] | 1735 | |
| 1736 | for (p = strpbrk(value, escape_chars); p != NULL; |
| 1737 | p = strpbrk(value, escape_chars)) { |
| 1738 | if (p > value) |
| 1739 | fwrite(value, 1, p - value, stdout); |
| 1740 | putchar('\\'); |
| 1741 | putchar(*p); |
| 1742 | value = p + 1; |
| 1743 | } |
| 1744 | |
| 1745 | /* print the rest and finish the double quoted |
| 1746 | string */ |
| 1747 | fputs(value, stdout); |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 1748 | putchar('\"'); |
Max Kellermann | a5d0994 | 2008-01-29 13:44:34 +0000 | [diff] [blame] | 1749 | } |
| 1750 | } |
Jan Engelhardt | 0f16c72 | 2009-01-30 04:55:38 +0100 | [diff] [blame] | 1751 | |
| 1752 | /** |
| 1753 | * Check for option-intrapositional negation. |
| 1754 | * Do not use in new code. |
| 1755 | */ |
| 1756 | int xtables_check_inverse(const char option[], int *invert, |
Jan Engelhardt | bf97128 | 2009-11-03 19:55:11 +0100 | [diff] [blame] | 1757 | int *my_optind, int argc, char **argv) |
Jan Engelhardt | 0f16c72 | 2009-01-30 04:55:38 +0100 | [diff] [blame] | 1758 | { |
Jan Engelhardt | 2be22fb | 2009-10-24 00:08:09 +0200 | [diff] [blame] | 1759 | if (option == NULL || strcmp(option, "!") != 0) |
| 1760 | return false; |
Jan Engelhardt | 0f16c72 | 2009-01-30 04:55:38 +0100 | [diff] [blame] | 1761 | |
Jan Engelhardt | 2be22fb | 2009-10-24 00:08:09 +0200 | [diff] [blame] | 1762 | fprintf(stderr, "Using intrapositioned negation " |
| 1763 | "(`--option ! this`) is deprecated in favor of " |
| 1764 | "extrapositioned (`! --option this`).\n"); |
| 1765 | |
| 1766 | if (*invert) |
| 1767 | xt_params->exit_err(PARAMETER_PROBLEM, |
| 1768 | "Multiple `!' flags not allowed"); |
| 1769 | *invert = true; |
| 1770 | if (my_optind != NULL) { |
Jan Engelhardt | bf97128 | 2009-11-03 19:55:11 +0100 | [diff] [blame] | 1771 | optarg = argv[*my_optind]; |
Jan Engelhardt | 2be22fb | 2009-10-24 00:08:09 +0200 | [diff] [blame] | 1772 | ++*my_optind; |
| 1773 | if (argc && *my_optind > argc) |
Jamal Hadi Salim | 8b7baeb | 2009-02-11 13:05:43 +0100 | [diff] [blame] | 1774 | xt_params->exit_err(PARAMETER_PROBLEM, |
Jan Engelhardt | 2be22fb | 2009-10-24 00:08:09 +0200 | [diff] [blame] | 1775 | "no argument following `!'"); |
Jan Engelhardt | 0f16c72 | 2009-01-30 04:55:38 +0100 | [diff] [blame] | 1776 | } |
Jan Engelhardt | 2be22fb | 2009-10-24 00:08:09 +0200 | [diff] [blame] | 1777 | |
| 1778 | return true; |
Jan Engelhardt | 0f16c72 | 2009-01-30 04:55:38 +0100 | [diff] [blame] | 1779 | } |
Jan Engelhardt | 1de7edf | 2009-01-30 05:38:11 +0100 | [diff] [blame] | 1780 | |
| 1781 | const struct xtables_pprot xtables_chain_protos[] = { |
| 1782 | {"tcp", IPPROTO_TCP}, |
| 1783 | {"sctp", IPPROTO_SCTP}, |
| 1784 | {"udp", IPPROTO_UDP}, |
| 1785 | {"udplite", IPPROTO_UDPLITE}, |
| 1786 | {"icmp", IPPROTO_ICMP}, |
| 1787 | {"icmpv6", IPPROTO_ICMPV6}, |
| 1788 | {"ipv6-icmp", IPPROTO_ICMPV6}, |
| 1789 | {"esp", IPPROTO_ESP}, |
| 1790 | {"ah", IPPROTO_AH}, |
| 1791 | {"ipv6-mh", IPPROTO_MH}, |
| 1792 | {"mh", IPPROTO_MH}, |
| 1793 | {"all", 0}, |
| 1794 | {NULL}, |
| 1795 | }; |
| 1796 | |
Jan Engelhardt | 7ac4052 | 2011-01-07 12:34:04 +0100 | [diff] [blame] | 1797 | uint16_t |
Jan Engelhardt | 1de7edf | 2009-01-30 05:38:11 +0100 | [diff] [blame] | 1798 | xtables_parse_protocol(const char *s) |
| 1799 | { |
| 1800 | unsigned int proto; |
| 1801 | |
| 1802 | if (!xtables_strtoui(s, NULL, &proto, 0, UINT8_MAX)) { |
| 1803 | struct protoent *pent; |
| 1804 | |
| 1805 | /* first deal with the special case of 'all' to prevent |
| 1806 | * people from being able to redefine 'all' in nsswitch |
| 1807 | * and/or provoke expensive [not working] ldap/nis/... |
| 1808 | * lookups */ |
| 1809 | if (!strcmp(s, "all")) |
| 1810 | return 0; |
| 1811 | |
| 1812 | if ((pent = getprotobyname(s))) |
| 1813 | proto = pent->p_proto; |
| 1814 | else { |
| 1815 | unsigned int i; |
| 1816 | for (i = 0; i < ARRAY_SIZE(xtables_chain_protos); ++i) { |
Pablo Neira Ayuso | e55cc4a | 2009-05-12 09:51:26 +0200 | [diff] [blame] | 1817 | if (xtables_chain_protos[i].name == NULL) |
| 1818 | continue; |
| 1819 | |
Jan Engelhardt | 1de7edf | 2009-01-30 05:38:11 +0100 | [diff] [blame] | 1820 | if (strcmp(s, xtables_chain_protos[i].name) == 0) { |
| 1821 | proto = xtables_chain_protos[i].num; |
| 1822 | break; |
| 1823 | } |
| 1824 | } |
| 1825 | if (i == ARRAY_SIZE(xtables_chain_protos)) |
Jamal Hadi Salim | 8b7baeb | 2009-02-11 13:05:43 +0100 | [diff] [blame] | 1826 | xt_params->exit_err(PARAMETER_PROBLEM, |
Jan Engelhardt | 1de7edf | 2009-01-30 05:38:11 +0100 | [diff] [blame] | 1827 | "unknown protocol `%s' specified", |
| 1828 | s); |
| 1829 | } |
| 1830 | } |
| 1831 | |
| 1832 | return proto; |
| 1833 | } |