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