blob: d8185796c0483497a9fbf0c63e973ccb790e843f [file] [log] [blame]
Yasuyuki KOZAKAI52088062007-07-24 05:44:11 +00001/*
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 */
Jan Engelhardt3c871012011-06-24 20:16:48 +020018#include "config.h"
Jan Engelhardtd61b02f2011-05-20 16:26:04 +020019#include <ctype.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000020#include <errno.h>
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000021#include <fcntl.h>
Jan Engelhardt0b7a1402011-05-24 02:30:23 +020022#include <inttypes.h>
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +000023#include <netdb.h>
Jan Engelhardtaafd2692008-01-20 13:19:40 +000024#include <stdarg.h>
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +000025#include <stdbool.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000026#include <stdio.h>
27#include <stdlib.h>
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000028#include <string.h>
29#include <unistd.h>
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000030#include <sys/socket.h>
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000031#include <sys/stat.h>
Maciej Zenczykowskib32b3612011-04-19 09:14:04 +020032#include <sys/statfs.h>
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000033#include <sys/types.h>
Jan Engelhardtf56b8a82011-09-03 14:27:55 +020034#include <sys/utsname.h>
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000035#include <sys/wait.h>
Jan Engelhardt08b16162008-01-20 13:36:08 +000036#include <arpa/inet.h>
Jan Engelhardt3c871012011-06-24 20:16:48 +020037#if defined(HAVE_LINUX_MAGIC_H)
38# include <linux/magic.h> /* for PROC_SUPER_MAGIC */
39#elif defined(HAVE_LINUX_PROC_FS_H)
40# include <linux/proc_fs.h> /* Linux 2.4 */
Jan Engelhardt41a9b482011-08-01 20:14:57 +020041#else
42# define PROC_SUPER_MAGIC 0x9fa0
Jan Engelhardt3c871012011-06-24 20:16:48 +020043#endif
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000044
Yasuyuki KOZAKAI52088062007-07-24 05:44:11 +000045#include <xtables.h>
Jan Engelhardt4e418542009-02-21 03:46:37 +010046#include <limits.h> /* INT_MAX in ip_tables.h/ip6_tables.h */
Jan Engelhardt77f48c22009-02-07 19:59:53 +010047#include <linux/netfilter_ipv4/ip_tables.h>
48#include <linux/netfilter_ipv6/ip6_tables.h>
Jan Engelhardtef18e812008-08-04 12:47:48 +020049#include <libiptc/libxtc.h>
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +000050
Mike Frysinger5a26b5f2007-12-19 14:51:17 +000051#ifndef NO_SHARED_LIBS
52#include <dlfcn.h>
53#endif
Jan Engelhardtc31870f2009-02-10 10:48:28 +010054#ifndef IPT_SO_GET_REVISION_MATCH /* Old kernel source. */
55# define IPT_SO_GET_REVISION_MATCH (IPT_BASE_CTL + 2)
56# define IPT_SO_GET_REVISION_TARGET (IPT_BASE_CTL + 3)
57#endif
58#ifndef IP6T_SO_GET_REVISION_MATCH /* Old kernel source. */
59# define IP6T_SO_GET_REVISION_MATCH 68
60# define IP6T_SO_GET_REVISION_TARGET 69
61#endif
Jamal Hadi Salim70581922009-02-13 08:36:44 -050062#include <getopt.h>
Jan Engelhardtaa37acc2011-02-07 04:00:50 +010063#include "iptables/internal.h"
Jan Engelhardt1e128bd2011-01-08 02:25:28 +010064#include "xshared.h"
Mike Frysinger5a26b5f2007-12-19 14:51:17 +000065
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000066#define NPROTO 255
67
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000068#ifndef PROC_SYS_MODPROBE
69#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe"
70#endif
71
Maciej Zenczykowski37911de2011-04-05 12:42:37 +020072/* we need this for ip6?tables-restore. ip6?tables-restore.c sets line to the
73 * current line of the input file, in order to give a more precise error
74 * message. ip6?tables itself doesn't need this, so it is initialized to the
75 * magic number of -1 */
76int line = -1;
77
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +010078void basic_exit_err(enum xtables_exittype status, const char *msg, ...) __attribute__((noreturn, format(printf,2,3)));
79
Jamal Hadi Salim40a83432009-02-11 13:02:21 +010080struct xtables_globals *xt_params = NULL;
81
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +010082void basic_exit_err(enum xtables_exittype status, const char *msg, ...)
Jamal Hadi Salim40a83432009-02-11 13:02:21 +010083{
84 va_list args;
85
86 va_start(args, msg);
87 fprintf(stderr, "%s v%s: ", xt_params->program_name, xt_params->program_version);
88 vfprintf(stderr, msg, args);
89 va_end(args);
90 fprintf(stderr, "\n");
91 exit(status);
92}
93
Jan Engelhardt600f38d2010-10-29 18:57:42 +020094void xtables_free_opts(int unused)
Jamal Hadi Salim84c30552009-02-11 13:00:02 +010095{
Jan Engelhardtdf288232011-01-31 02:33:43 +010096 if (xt_params->opts != xt_params->orig_opts) {
Jan Engelhardt59e81142010-11-15 13:19:48 +010097 free(xt_params->opts);
Jan Engelhardtdf288232011-01-31 02:33:43 +010098 xt_params->opts = NULL;
99 }
Jamal Hadi Salim84c30552009-02-11 13:00:02 +0100100}
101
Jan Engelhardt600f38d2010-10-29 18:57:42 +0200102struct option *xtables_merge_options(struct option *orig_opts,
103 struct option *oldopts,
Jamal Hadi Salim70581922009-02-13 08:36:44 -0500104 const struct option *newopts,
105 unsigned int *option_offset)
106{
Jan Engelhardt600f38d2010-10-29 18:57:42 +0200107 unsigned int num_oold = 0, num_old = 0, num_new = 0, i;
108 struct option *merge, *mp;
Jamal Hadi Salim70581922009-02-13 08:36:44 -0500109
110 if (newopts == NULL)
111 return oldopts;
112
Jan Engelhardt600f38d2010-10-29 18:57:42 +0200113 for (num_oold = 0; orig_opts[num_oold].name; num_oold++) ;
114 if (oldopts != NULL)
115 for (num_old = 0; oldopts[num_old].name; num_old++) ;
Jamal Hadi Salim70581922009-02-13 08:36:44 -0500116 for (num_new = 0; newopts[num_new].name; num_new++) ;
117
Jan Engelhardt1dc27392011-01-08 02:10:52 +0100118 /*
119 * Since @oldopts also has @orig_opts already (and does so at the
120 * start), skip these entries.
121 */
122 oldopts += num_oold;
123 num_old -= num_oold;
124
Jan Engelhardt600f38d2010-10-29 18:57:42 +0200125 merge = malloc(sizeof(*mp) * (num_oold + num_old + num_new + 1));
Jamal Hadi Salim70581922009-02-13 08:36:44 -0500126 if (merge == NULL)
127 return NULL;
Jamal Hadi Salim70581922009-02-13 08:36:44 -0500128
Jan Engelhardt600f38d2010-10-29 18:57:42 +0200129 /* Let the base options -[ADI...] have precedence over everything */
130 memcpy(merge, orig_opts, sizeof(*mp) * num_oold);
131 mp = merge + num_oold;
132
133 /* Second, the new options */
Jan Engelhardt1e128bd2011-01-08 02:25:28 +0100134 xt_params->option_offset += XT_OPTION_OFFSET_SCALE;
Jan Engelhardt600f38d2010-10-29 18:57:42 +0200135 *option_offset = xt_params->option_offset;
136 memcpy(mp, newopts, sizeof(*mp) * num_new);
137
138 for (i = 0; i < num_new; ++i, ++mp)
139 mp->val += *option_offset;
140
141 /* Third, the old options */
142 memcpy(mp, oldopts, sizeof(*mp) * num_old);
143 mp += num_old;
144 xtables_free_opts(0);
145
146 /* Clear trailing entry */
147 memset(mp, 0, sizeof(*mp));
Jamal Hadi Salim70581922009-02-13 08:36:44 -0500148 return merge;
149}
150
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100151static const struct xtables_afinfo afinfo_ipv4 = {
152 .kmod = "ip_tables",
Maciej Zenczykowskib32b3612011-04-19 09:14:04 +0200153 .proc_exists = "/proc/net/ip_tables_names",
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100154 .libprefix = "libipt_",
155 .family = NFPROTO_IPV4,
156 .ipproto = IPPROTO_IP,
157 .so_rev_match = IPT_SO_GET_REVISION_MATCH,
158 .so_rev_target = IPT_SO_GET_REVISION_TARGET,
159};
160
161static const struct xtables_afinfo afinfo_ipv6 = {
162 .kmod = "ip6_tables",
Maciej Zenczykowskib32b3612011-04-19 09:14:04 +0200163 .proc_exists = "/proc/net/ip6_tables_names",
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100164 .libprefix = "libip6t_",
165 .family = NFPROTO_IPV6,
166 .ipproto = IPPROTO_IPV6,
167 .so_rev_match = IP6T_SO_GET_REVISION_MATCH,
168 .so_rev_target = IP6T_SO_GET_REVISION_TARGET,
169};
170
Jan Engelhardtaa37acc2011-02-07 04:00:50 +0100171const struct xtables_afinfo *afinfo;
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100172
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100173/* Search path for Xtables .so files */
174static const char *xtables_libdir;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000175
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000176/* the path to command to load kernel module */
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100177const char *xtables_modprobe_program;
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000178
Maciej Zenczykowski2c6ac072011-04-04 15:31:43 +0200179/* Keep track of matches/targets pending full registration: linked lists. */
180struct xtables_match *xtables_pending_matches;
181struct xtables_target *xtables_pending_targets;
182
183/* Keep track of fully registered external matches/targets: linked lists. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000184struct xtables_match *xtables_matches;
185struct xtables_target *xtables_targets;
186
Maciej Zenczykowski2c6ac072011-04-04 15:31:43 +0200187/* Fully register a match/target which was previously partially registered. */
188static void xtables_fully_register_pending_match(struct xtables_match *me);
189static void xtables_fully_register_pending_target(struct xtables_target *me);
190
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100191void xtables_init(void)
192{
193 xtables_libdir = getenv("XTABLES_LIBDIR");
194 if (xtables_libdir != NULL)
195 return;
196 xtables_libdir = getenv("IPTABLES_LIB_DIR");
197 if (xtables_libdir != NULL) {
198 fprintf(stderr, "IPTABLES_LIB_DIR is deprecated, "
199 "use XTABLES_LIBDIR.\n");
200 return;
201 }
Jan Engelhardtec934192009-02-10 09:54:04 +0100202 /*
203 * Well yes, IP6TABLES_LIB_DIR is of lower priority over
204 * IPTABLES_LIB_DIR since this moved to libxtables; I think that is ok
205 * for these env vars are deprecated anyhow, and in light of the
206 * (shared) libxt_*.so files, makes less sense to have
207 * IPTABLES_LIB_DIR != IP6TABLES_LIB_DIR.
208 */
209 xtables_libdir = getenv("IP6TABLES_LIB_DIR");
210 if (xtables_libdir != NULL) {
211 fprintf(stderr, "IP6TABLES_LIB_DIR is deprecated, "
212 "use XTABLES_LIBDIR.\n");
213 return;
214 }
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100215 xtables_libdir = XTABLES_LIBDIR;
216}
217
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100218void xtables_set_nfproto(uint8_t nfproto)
219{
220 switch (nfproto) {
221 case NFPROTO_IPV4:
222 afinfo = &afinfo_ipv4;
223 break;
224 case NFPROTO_IPV6:
225 afinfo = &afinfo_ipv6;
226 break;
227 default:
228 fprintf(stderr, "libxtables: unhandled NFPROTO in %s\n",
229 __func__);
230 }
231}
232
Jan Engelhardt630ef482009-01-27 14:58:41 +0100233/**
Jamal Hadi Salim7e4db2f2009-02-13 09:14:17 -0500234 * xtables_set_params - set the global parameters used by xtables
235 * @xtp: input xtables_globals structure
236 *
237 * The app is expected to pass a valid xtables_globals data-filled
238 * with proper values
239 * @xtp cannot be NULL
240 *
241 * Returns -1 on failure to set and 0 on success
242 */
243int xtables_set_params(struct xtables_globals *xtp)
244{
245 if (!xtp) {
246 fprintf(stderr, "%s: Illegal global params\n",__func__);
247 return -1;
248 }
249
250 xt_params = xtp;
251
252 if (!xt_params->exit_err)
253 xt_params->exit_err = basic_exit_err;
254
255 return 0;
256}
257
258int xtables_init_all(struct xtables_globals *xtp, uint8_t nfproto)
259{
260 xtables_init();
261 xtables_set_nfproto(nfproto);
262 return xtables_set_params(xtp);
263}
264
265/**
Jan Engelhardt630ef482009-01-27 14:58:41 +0100266 * xtables_*alloc - wrappers that exit on failure
267 */
268void *xtables_calloc(size_t count, size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +0000269{
270 void *p;
271
272 if ((p = calloc(count, size)) == NULL) {
273 perror("ip[6]tables: calloc failed");
274 exit(1);
275 }
276
277 return p;
278}
279
Jan Engelhardt630ef482009-01-27 14:58:41 +0100280void *xtables_malloc(size_t size)
Yasuyuki KOZAKAI3dfa4482007-07-24 05:45:33 +0000281{
282 void *p;
283
284 if ((p = malloc(size)) == NULL) {
285 perror("ip[6]tables: malloc failed");
286 exit(1);
287 }
288
289 return p;
290}
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000291
Michael Granzow332e4ac2009-04-09 18:24:36 +0100292void *xtables_realloc(void *ptr, size_t size)
293{
294 void *p;
295
296 if ((p = realloc(ptr, size)) == NULL) {
297 perror("ip[6]tables: realloc failed");
298 exit(1);
299 }
300
301 return p;
302}
303
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000304static char *get_modprobe(void)
305{
306 int procfile;
307 char *ret;
308
309#define PROCFILE_BUFSIZ 1024
310 procfile = open(PROC_SYS_MODPROBE, O_RDONLY);
311 if (procfile < 0)
312 return NULL;
Maciej Zenczykowskia2397282011-04-04 15:30:32 +0200313 if (fcntl(procfile, F_SETFD, FD_CLOEXEC) == -1) {
314 fprintf(stderr, "Could not set close on exec: %s\n",
315 strerror(errno));
316 exit(1);
317 }
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000318
Jan Engelhardt371cea22010-07-25 23:36:17 +0200319 ret = malloc(PROCFILE_BUFSIZ);
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000320 if (ret) {
321 memset(ret, 0, PROCFILE_BUFSIZ);
322 switch (read(procfile, ret, PROCFILE_BUFSIZ)) {
323 case -1: goto fail;
324 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */
325 }
326 if (ret[strlen(ret)-1]=='\n')
327 ret[strlen(ret)-1]=0;
328 close(procfile);
329 return ret;
330 }
331 fail:
332 free(ret);
333 close(procfile);
334 return NULL;
335}
336
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100337int xtables_insmod(const char *modname, const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000338{
339 char *buf = NULL;
340 char *argv[4];
341 int status;
342
343 /* If they don't explicitly set it, read out of kernel */
344 if (!modprobe) {
345 buf = get_modprobe();
346 if (!buf)
347 return -1;
348 modprobe = buf;
349 }
350
Jan Engelhardtc19f8802009-02-12 01:28:35 +0100351 /*
352 * Need to flush the buffer, or the child may output it again
353 * when switching the program thru execv.
354 */
355 fflush(stdout);
356
Jan Engelhardt94aa2ea2009-10-11 03:56:18 -0400357 switch (vfork()) {
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000358 case 0:
359 argv[0] = (char *)modprobe;
360 argv[1] = (char *)modname;
361 if (quiet) {
362 argv[2] = "-q";
363 argv[3] = NULL;
364 } else {
365 argv[2] = NULL;
366 argv[3] = NULL;
367 }
368 execv(argv[0], argv);
369
370 /* not usually reached */
371 exit(1);
372 case -1:
Jiri Popelkaf53710b2011-06-10 15:26:02 +0200373 free(buf);
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +0000374 return -1;
375
376 default: /* parent */
377 wait(&status);
378 }
379
380 free(buf);
381 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
382 return 0;
383 return -1;
384}
385
Maciej Zenczykowskib32b3612011-04-19 09:14:04 +0200386/* return true if a given file exists within procfs */
387static bool proc_file_exists(const char *filename)
388{
389 struct stat s;
390 struct statfs f;
391
392 if (lstat(filename, &s))
393 return false;
394 if (!S_ISREG(s.st_mode))
395 return false;
396 if (statfs(filename, &f))
397 return false;
398 if (f.f_type != PROC_SUPER_MAGIC)
399 return false;
400 return true;
401}
402
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100403int xtables_load_ko(const char *modprobe, bool quiet)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000404{
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100405 static bool loaded = false;
Maciej Zenczykowskib32b3612011-04-19 09:14:04 +0200406 int ret;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000407
Maciej Zenczykowskib32b3612011-04-19 09:14:04 +0200408 if (loaded)
409 return 0;
410
411 if (proc_file_exists(afinfo->proc_exists)) {
412 loaded = true;
413 return 0;
414 };
415
416 ret = xtables_insmod(afinfo->kmod, modprobe, quiet);
417 if (ret == 0)
418 loaded = true;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000419
420 return ret;
421}
422
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100423/**
424 * xtables_strtou{i,l} - string to number conversion
425 * @s: input string
426 * @end: like strtoul's "end" pointer
427 * @value: pointer for result
428 * @min: minimum accepted value
429 * @max: maximum accepted value
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000430 *
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100431 * If @end is NULL, we assume the caller wants a "strict strtoul", and hence
432 * "15a" is rejected.
433 * In either case, the value obtained is compared for min-max compliance.
434 * Base is always 0, i.e. autodetect depending on @s.
435 *
436 * Returns true/false whether number was accepted. On failure, *value has
437 * undefined contents.
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000438 */
Jan Engelhardt0b7a1402011-05-24 02:30:23 +0200439bool xtables_strtoul(const char *s, char **end, uintmax_t *value,
440 uintmax_t min, uintmax_t max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000441{
Jan Engelhardt0b7a1402011-05-24 02:30:23 +0200442 uintmax_t v;
Jan Engelhardtd61b02f2011-05-20 16:26:04 +0200443 const char *p;
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000444 char *my_end;
445
446 errno = 0;
Jan Engelhardtd61b02f2011-05-20 16:26:04 +0200447 /* Since strtoul allows leading minus, we have to check for ourself. */
448 for (p = s; isspace(*p); ++p)
449 ;
450 if (*p == '-')
451 return false;
Jan Engelhardt0b7a1402011-05-24 02:30:23 +0200452 v = strtoumax(s, &my_end, 0);
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000453 if (my_end == s)
454 return false;
455 if (end != NULL)
456 *end = my_end;
457
458 if (errno != ERANGE && min <= v && (max == 0 || v <= max)) {
459 if (value != NULL)
460 *value = v;
461 if (end == NULL)
462 return *my_end == '\0';
463 return true;
464 }
465
466 return false;
467}
468
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100469bool xtables_strtoui(const char *s, char **end, unsigned int *value,
470 unsigned int min, unsigned int max)
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000471{
Jan Engelhardt0b7a1402011-05-24 02:30:23 +0200472 uintmax_t v;
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000473 bool ret;
474
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100475 ret = xtables_strtoul(s, end, &v, min, max);
Jan Engelhardtcd9e7aa2008-01-20 13:18:54 +0000476 if (value != NULL)
477 *value = v;
478 return ret;
479}
480
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100481int xtables_service_to_port(const char *name, const char *proto)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000482{
483 struct servent *service;
484
485 if ((service = getservbyname(name, proto)) != NULL)
486 return ntohs((unsigned short) service->s_port);
487
488 return -1;
489}
490
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100491uint16_t xtables_parse_port(const char *port, const char *proto)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000492{
Jan Engelhardt7a236f42008-03-03 12:30:41 +0100493 unsigned int portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000494
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100495 if (xtables_strtoui(port, NULL, &portnum, 0, UINT16_MAX) ||
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100496 (portnum = xtables_service_to_port(port, proto)) != (unsigned)-1)
Jan Engelhardt213e1852009-01-27 17:24:34 +0100497 return portnum;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000498
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100499 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000500 "invalid port/service `%s' specified", port);
501}
502
Jan Engelhardtaae6be92009-01-30 04:24:47 +0100503void xtables_parse_interface(const char *arg, char *vianame,
504 unsigned char *mask)
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000505{
Jan Engelhardtfcf57232010-02-09 15:59:13 +0100506 unsigned int vialen = strlen(arg);
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000507 unsigned int i;
508
509 memset(mask, 0, IFNAMSIZ);
510 memset(vianame, 0, IFNAMSIZ);
511
512 if (vialen + 1 > IFNAMSIZ)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100513 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000514 "interface name `%s' must be shorter than IFNAMSIZ"
515 " (%i)", arg, IFNAMSIZ-1);
516
517 strcpy(vianame, arg);
Jan Engelhardtfcf57232010-02-09 15:59:13 +0100518 if (vialen == 0)
Richard Weinberger2ca62732011-08-27 15:32:31 +0200519 return;
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000520 else if (vianame[vialen - 1] == '+') {
521 memset(mask, 0xFF, vialen - 1);
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000522 /* Don't remove `+' here! -HW */
523 } else {
524 /* Include nul-terminator in match */
525 memset(mask, 0xFF, vialen + 1);
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000526 for (i = 0; vianame[i]; i++) {
Jan Engelhardtfcf57232010-02-09 15:59:13 +0100527 if (vianame[i] == '/' ||
528 vianame[i] == ' ') {
Max Kellermannaae4f822007-10-17 16:36:49 +0000529 fprintf(stderr,
530 "Warning: weird character in interface"
Jan Engelhardtfcf57232010-02-09 15:59:13 +0100531 " `%s' ('/' and ' ' are not allowed by the kernel).\n",
Max Kellermannaae4f822007-10-17 16:36:49 +0000532 vianame);
Yasuyuki KOZAKAI04f8c542007-07-24 05:53:48 +0000533 break;
534 }
535 }
536 }
537}
538
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200539#ifndef NO_SHARED_LIBS
Jan Engelhardt92738502011-01-30 14:18:17 +0100540static void *load_extension(const char *search_path, const char *af_prefix,
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100541 const char *name, bool is_target)
542{
Jan Engelhardt92738502011-01-30 14:18:17 +0100543 const char *all_prefixes[] = {"libxt_", af_prefix, NULL};
544 const char **prefix;
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100545 const char *dir = search_path, *next;
546 void *ptr = NULL;
547 struct stat sb;
548 char path[256];
549
550 do {
551 next = strchr(dir, ':');
552 if (next == NULL)
553 next = dir + strlen(dir);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100554
Jan Engelhardt92738502011-01-30 14:18:17 +0100555 for (prefix = all_prefixes; *prefix != NULL; ++prefix) {
556 snprintf(path, sizeof(path), "%.*s/%s%s.so",
557 (unsigned int)(next - dir), dir,
558 *prefix, name);
559
560 if (stat(path, &sb) != 0) {
561 if (errno == ENOENT)
562 continue;
563 fprintf(stderr, "%s: %s\n", path,
564 strerror(errno));
565 return NULL;
566 }
567 if (dlopen(path, RTLD_NOW) == NULL) {
568 fprintf(stderr, "%s: %s\n", path, dlerror());
569 break;
570 }
571
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100572 if (is_target)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100573 ptr = xtables_find_target(name, XTF_DONT_LOAD);
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100574 else
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100575 ptr = xtables_find_match(name,
576 XTF_DONT_LOAD, NULL);
Jan Engelhardt92738502011-01-30 14:18:17 +0100577
578 if (ptr != NULL)
579 return ptr;
580
581 fprintf(stderr, "%s: no \"%s\" extension found for "
582 "this protocol\n", path, name);
583 errno = ENOENT;
584 return NULL;
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100585 }
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100586 dir = next + 1;
587 } while (*next != '\0');
588
589 return NULL;
590}
Jan Engelhardtcb25af82008-04-14 18:37:57 +0200591#endif
Jan Engelhardt21b41ee2008-02-11 01:02:00 +0100592
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100593struct xtables_match *
594xtables_find_match(const char *name, enum xtables_tryload tryload,
595 struct xtables_rule_match **matches)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000596{
Maciej Zenczykowski2c6ac072011-04-04 15:31:43 +0200597 struct xtables_match **dptr;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000598 struct xtables_match *ptr;
599 const char *icmp6 = "icmp6";
600
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200601 if (strlen(name) >= XT_EXTENSION_MAXNAMELEN)
Jan Engelhardt21d12832010-03-16 16:49:21 +0100602 xtables_error(PARAMETER_PROBLEM,
603 "Invalid match name \"%s\" (%u chars max)",
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200604 name, XT_EXTENSION_MAXNAMELEN - 1);
Jan Engelhardt21d12832010-03-16 16:49:21 +0100605
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000606 /* This is ugly as hell. Nonetheless, there is no way of changing
607 * this without hurting backwards compatibility */
608 if ( (strcmp(name,"icmpv6") == 0) ||
609 (strcmp(name,"ipv6-icmp") == 0) ||
610 (strcmp(name,"icmp6") == 0) )
611 name = icmp6;
612
Maciej Zenczykowski2c6ac072011-04-04 15:31:43 +0200613 /* Trigger delayed initialization */
614 for (dptr = &xtables_pending_matches; *dptr; ) {
615 if (strcmp(name, (*dptr)->name) == 0) {
616 ptr = *dptr;
617 *dptr = (*dptr)->next;
618 ptr->next = NULL;
619 xtables_fully_register_pending_match(ptr);
620 } else {
621 dptr = &((*dptr)->next);
622 }
623 }
624
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000625 for (ptr = xtables_matches; ptr; ptr = ptr->next) {
626 if (strcmp(name, ptr->name) == 0) {
627 struct xtables_match *clone;
628
629 /* First match of this type: */
630 if (ptr->m == NULL)
631 break;
632
633 /* Second and subsequent clones */
Jan Engelhardt630ef482009-01-27 14:58:41 +0100634 clone = xtables_malloc(sizeof(struct xtables_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000635 memcpy(clone, ptr, sizeof(struct xtables_match));
Jan Engelhardt3eab7862011-07-10 11:47:46 +0200636 clone->udata = NULL;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000637 clone->mflags = 0;
638 /* This is a clone: */
639 clone->next = clone;
640
641 ptr = clone;
642 break;
643 }
644 }
645
646#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100647 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100648 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100649 name, false);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000650
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100651 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100652 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000653 "Couldn't load match `%s':%s\n",
Jan Engelhardt92738502011-01-30 14:18:17 +0100654 name, strerror(errno));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000655 }
656#else
657 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100658 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000659 ptr->loaded = 1;
660 else
661 ptr = NULL;
662 }
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100663 if(!ptr && (tryload == XTF_LOAD_MUST_SUCCEED)) {
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100664 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000665 "Couldn't find match `%s'\n", name);
666 }
667#endif
668
669 if (ptr && matches) {
670 struct xtables_rule_match **i;
671 struct xtables_rule_match *newentry;
672
Jan Engelhardt630ef482009-01-27 14:58:41 +0100673 newentry = xtables_malloc(sizeof(struct xtables_rule_match));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000674
675 for (i = matches; *i; i = &(*i)->next) {
676 if (strcmp(name, (*i)->match->name) == 0)
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100677 (*i)->completed = true;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000678 }
679 newentry->match = ptr;
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100680 newentry->completed = false;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000681 newentry->next = NULL;
682 *i = newentry;
683 }
684
685 return ptr;
686}
687
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100688struct xtables_target *
689xtables_find_target(const char *name, enum xtables_tryload tryload)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000690{
Maciej Zenczykowski2c6ac072011-04-04 15:31:43 +0200691 struct xtables_target **dptr;
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000692 struct xtables_target *ptr;
693
694 /* Standard target? */
695 if (strcmp(name, "") == 0
696 || strcmp(name, XTC_LABEL_ACCEPT) == 0
697 || strcmp(name, XTC_LABEL_DROP) == 0
698 || strcmp(name, XTC_LABEL_QUEUE) == 0
699 || strcmp(name, XTC_LABEL_RETURN) == 0)
700 name = "standard";
701
Maciej Zenczykowski2c6ac072011-04-04 15:31:43 +0200702 /* Trigger delayed initialization */
703 for (dptr = &xtables_pending_targets; *dptr; ) {
704 if (strcmp(name, (*dptr)->name) == 0) {
705 ptr = *dptr;
706 *dptr = (*dptr)->next;
707 ptr->next = NULL;
708 xtables_fully_register_pending_target(ptr);
709 } else {
710 dptr = &((*dptr)->next);
711 }
712 }
713
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000714 for (ptr = xtables_targets; ptr; ptr = ptr->next) {
715 if (strcmp(name, ptr->name) == 0)
716 break;
717 }
718
719#ifndef NO_SHARED_LIBS
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100720 if (!ptr && tryload != XTF_DONT_LOAD && tryload != XTF_DURING_LOAD) {
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100721 ptr = load_extension(xtables_libdir, afinfo->libprefix,
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100722 name, true);
Yasuyuki KOZAKAI170af8c2007-08-04 05:22:17 +0000723
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100724 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED)
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100725 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000726 "Couldn't load target `%s':%s\n",
Jan Engelhardt92738502011-01-30 14:18:17 +0100727 name, strerror(errno));
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000728 }
729#else
730 if (ptr && !ptr->loaded) {
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100731 if (tryload != XTF_DONT_LOAD)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000732 ptr->loaded = 1;
733 else
734 ptr = NULL;
735 }
Peter Volkov854d2d92009-03-24 11:09:16 +0300736 if (ptr == NULL && tryload == XTF_LOAD_MUST_SUCCEED) {
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +0100737 xt_params->exit_err(PARAMETER_PROBLEM,
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000738 "Couldn't find target `%s'\n", name);
739 }
740#endif
741
742 if (ptr)
743 ptr->used = 1;
744
745 return ptr;
746}
747
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100748static int compatible_revision(const char *name, uint8_t revision, int opt)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000749{
750 struct xt_get_revision rev;
751 socklen_t s = sizeof(rev);
752 int max_rev, sockfd;
753
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100754 sockfd = socket(afinfo->family, SOCK_RAW, IPPROTO_RAW);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000755 if (sockfd < 0) {
Patrick McHardydf1ef382007-12-03 15:32:28 +0000756 if (errno == EPERM) {
757 /* revision 0 is always supported. */
758 if (revision != 0)
Jan Engelhardte1639b02011-05-05 12:54:52 +0200759 fprintf(stderr, "%s: Could not determine whether "
Patrick McHardydf1ef382007-12-03 15:32:28 +0000760 "revision %u is supported, "
761 "assuming it is.\n",
Jan Engelhardte1639b02011-05-05 12:54:52 +0200762 name, revision);
Patrick McHardydf1ef382007-12-03 15:32:28 +0000763 return 1;
764 }
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000765 fprintf(stderr, "Could not open socket to kernel: %s\n",
766 strerror(errno));
767 exit(1);
768 }
769
Maciej Zenczykowskia2397282011-04-04 15:30:32 +0200770 if (fcntl(sockfd, F_SETFD, FD_CLOEXEC) == -1) {
771 fprintf(stderr, "Could not set close on exec: %s\n",
772 strerror(errno));
773 exit(1);
774 }
775
Jan Engelhardtc021c3c2009-01-27 15:10:05 +0100776 xtables_load_ko(xtables_modprobe_program, true);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000777
778 strcpy(rev.name, name);
779 rev.revision = revision;
780
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100781 max_rev = getsockopt(sockfd, afinfo->ipproto, opt, &rev, &s);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000782 if (max_rev < 0) {
783 /* Definitely don't support this? */
784 if (errno == ENOENT || errno == EPROTONOSUPPORT) {
785 close(sockfd);
786 return 0;
787 } else if (errno == ENOPROTOOPT) {
788 close(sockfd);
789 /* Assume only revision 0 support (old kernel) */
790 return (revision == 0);
791 } else {
792 fprintf(stderr, "getsockopt failed strangely: %s\n",
793 strerror(errno));
794 exit(1);
795 }
796 }
797 close(sockfd);
798 return 1;
799}
800
801
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100802static int compatible_match_revision(const char *name, uint8_t revision)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000803{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100804 return compatible_revision(name, revision, afinfo->so_rev_match);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000805}
806
Jan Engelhardt7ac40522011-01-07 12:34:04 +0100807static int compatible_target_revision(const char *name, uint8_t revision)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000808{
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100809 return compatible_revision(name, revision, afinfo->so_rev_target);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000810}
811
Jan Engelhardtdfbedfe2011-01-08 03:31:04 +0100812static void xtables_check_options(const char *name, const struct option *opt)
813{
814 for (; opt->name != NULL; ++opt)
815 if (opt->val < 0 || opt->val >= XT_OPTION_OFFSET_SCALE) {
816 fprintf(stderr, "%s: Extension %s uses invalid "
817 "option value %d\n",xt_params->program_name,
818 name, opt->val);
819 exit(1);
820 }
821}
822
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000823void xtables_register_match(struct xtables_match *me)
824{
Jan Engelhardtc284de52009-06-25 21:25:24 +0200825 if (me->version == NULL) {
826 fprintf(stderr, "%s: match %s<%u> is missing a version\n",
827 xt_params->program_name, me->name, me->revision);
828 exit(1);
829 }
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100830 if (strcmp(me->version, XTABLES_VERSION) != 0) {
831 fprintf(stderr, "%s: match \"%s\" has version \"%s\", "
832 "but \"%s\" is required.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500833 xt_params->program_name, me->name,
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100834 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000835 exit(1);
836 }
837
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200838 if (strlen(me->name) >= XT_EXTENSION_MAXNAMELEN) {
Li Yewang281439b2011-01-09 22:26:58 +0100839 fprintf(stderr, "%s: match `%s' has invalid name\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500840 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000841 exit(1);
842 }
843
844 if (me->family >= NPROTO) {
845 fprintf(stderr,
846 "%s: BUG: match %s has invalid protocol family\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500847 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000848 exit(1);
849 }
850
Jan Engelhardtaa37acc2011-02-07 04:00:50 +0100851 if (me->x6_options != NULL)
852 xtables_option_metavalidate(me->name, me->x6_options);
Jan Engelhardtdfbedfe2011-01-08 03:31:04 +0100853 if (me->extra_opts != NULL)
854 xtables_check_options(me->name, me->extra_opts);
855
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000856 /* ignore not interested match */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100857 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000858 return;
859
Maciej Zenczykowski2c6ac072011-04-04 15:31:43 +0200860 /* place on linked list of matches pending full registration */
861 me->next = xtables_pending_matches;
862 xtables_pending_matches = me;
863}
864
865static void xtables_fully_register_pending_match(struct xtables_match *me)
866{
867 struct xtables_match **i, *old;
868
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100869 old = xtables_find_match(me->name, XTF_DURING_LOAD, NULL);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000870 if (old) {
Jan Engelhardt23545c22008-02-14 04:23:04 +0100871 if (old->revision == me->revision &&
872 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000873 fprintf(stderr,
874 "%s: match `%s' already registered.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500875 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000876 exit(1);
877 }
878
879 /* Now we have two (or more) options, check compatibility. */
880 if (compatible_match_revision(old->name, old->revision)
881 && old->revision > me->revision)
882 return;
883
Jan Engelhardt23545c22008-02-14 04:23:04 +0100884 /* See if new match can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000885 if (!compatible_match_revision(me->name, me->revision))
886 return;
887
Jan Engelhardt23545c22008-02-14 04:23:04 +0100888 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
889 if (old->revision == me->revision && me->family == AF_UNSPEC)
890 return;
891
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000892 /* Delete old one. */
893 for (i = &xtables_matches; *i!=old; i = &(*i)->next);
894 *i = old->next;
895 }
896
897 if (me->size != XT_ALIGN(me->size)) {
898 fprintf(stderr, "%s: match `%s' has invalid size %u.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500899 xt_params->program_name, me->name,
900 (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000901 exit(1);
902 }
903
904 /* Append to list. */
905 for (i = &xtables_matches; *i; i = &(*i)->next);
906 me->next = NULL;
907 *i = me;
908
909 me->m = NULL;
910 me->mflags = 0;
911}
912
Jan Engelhardt9a8fc4f2009-06-25 17:13:46 +0200913void xtables_register_matches(struct xtables_match *match, unsigned int n)
914{
915 do {
916 xtables_register_match(&match[--n]);
917 } while (n > 0);
918}
919
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000920void xtables_register_target(struct xtables_target *me)
921{
Jan Engelhardtc284de52009-06-25 21:25:24 +0200922 if (me->version == NULL) {
923 fprintf(stderr, "%s: target %s<%u> is missing a version\n",
924 xt_params->program_name, me->name, me->revision);
925 exit(1);
926 }
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100927 if (strcmp(me->version, XTABLES_VERSION) != 0) {
928 fprintf(stderr, "%s: target \"%s\" has version \"%s\", "
929 "but \"%s\" is required.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500930 xt_params->program_name, me->name,
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100931 me->version, XTABLES_VERSION);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000932 exit(1);
933 }
934
Jan Engelhardt0cb675b2010-06-07 11:50:25 +0200935 if (strlen(me->name) >= XT_EXTENSION_MAXNAMELEN) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000936 fprintf(stderr, "%s: target `%s' has invalid name\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500937 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000938 exit(1);
939 }
940
941 if (me->family >= NPROTO) {
942 fprintf(stderr,
943 "%s: BUG: target %s has invalid protocol family\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500944 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000945 exit(1);
946 }
947
Jan Engelhardtaa37acc2011-02-07 04:00:50 +0100948 if (me->x6_options != NULL)
949 xtables_option_metavalidate(me->name, me->x6_options);
Jan Engelhardtdfbedfe2011-01-08 03:31:04 +0100950 if (me->extra_opts != NULL)
951 xtables_check_options(me->name, me->extra_opts);
952
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000953 /* ignore not interested target */
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100954 if (me->family != afinfo->family && me->family != AF_UNSPEC)
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000955 return;
956
Maciej Zenczykowski2c6ac072011-04-04 15:31:43 +0200957 /* place on linked list of targets pending full registration */
958 me->next = xtables_pending_targets;
959 xtables_pending_targets = me;
960}
961
962static void xtables_fully_register_pending_target(struct xtables_target *me)
963{
964 struct xtables_target *old;
965
Jan Engelhardt2338efd2009-01-27 15:23:01 +0100966 old = xtables_find_target(me->name, XTF_DURING_LOAD);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000967 if (old) {
968 struct xtables_target **i;
969
Jan Engelhardt23545c22008-02-14 04:23:04 +0100970 if (old->revision == me->revision &&
971 old->family == me->family) {
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000972 fprintf(stderr,
973 "%s: target `%s' already registered.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500974 xt_params->program_name, me->name);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000975 exit(1);
976 }
977
978 /* Now we have two (or more) options, check compatibility. */
979 if (compatible_target_revision(old->name, old->revision)
980 && old->revision > me->revision)
981 return;
982
Jan Engelhardt23545c22008-02-14 04:23:04 +0100983 /* See if new target can be used. */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000984 if (!compatible_target_revision(me->name, me->revision))
985 return;
986
Jan Engelhardt23545c22008-02-14 04:23:04 +0100987 /* Prefer !AF_UNSPEC over AF_UNSPEC for same revision. */
988 if (old->revision == me->revision && me->family == AF_UNSPEC)
989 return;
990
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +0000991 /* Delete old one. */
992 for (i = &xtables_targets; *i!=old; i = &(*i)->next);
993 *i = old->next;
994 }
995
996 if (me->size != XT_ALIGN(me->size)) {
997 fprintf(stderr, "%s: target `%s' has invalid size %u.\n",
Jamal Hadi Salim5dd19de2009-02-13 10:42:24 -0500998 xt_params->program_name, me->name,
999 (unsigned int)me->size);
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +00001000 exit(1);
1001 }
1002
1003 /* Prepend to list. */
1004 me->next = xtables_targets;
1005 xtables_targets = me;
1006 me->t = NULL;
1007 me->tflags = 0;
1008}
Jan Engelhardtaafd2692008-01-20 13:19:40 +00001009
Jan Engelhardt9a8fc4f2009-06-25 17:13:46 +02001010void xtables_register_targets(struct xtables_target *target, unsigned int n)
1011{
1012 do {
1013 xtables_register_target(&target[--n]);
1014 } while (n > 0);
1015}
1016
Jan Engelhardta41545c2009-01-27 21:27:19 +01001017/**
1018 * xtables_param_act - act on condition
1019 * @status: a constant from enum xtables_exittype
1020 *
1021 * %XTF_ONLY_ONCE: print error message that option may only be used once.
1022 * @p1: module name (e.g. "mark")
1023 * @p2(...): option in conflict (e.g. "--mark")
1024 * @p3(...): condition to match on (see extensions/ for examples)
1025 *
1026 * %XTF_NO_INVERT: option does not support inversion
1027 * @p1: module name
1028 * @p2: option in conflict
1029 * @p3: condition to match on
1030 *
1031 * %XTF_BAD_VALUE: bad value for option
1032 * @p1: module name
1033 * @p2: option with which the problem occured (e.g. "--mark")
1034 * @p3: string the user passed in (e.g. "99999999999999")
1035 *
1036 * %XTF_ONE_ACTION: two mutually exclusive actions have been specified
1037 * @p1: module name
1038 *
1039 * Displays an error message and exits the program.
1040 */
1041void xtables_param_act(unsigned int status, const char *p1, ...)
Jan Engelhardtaafd2692008-01-20 13:19:40 +00001042{
1043 const char *p2, *p3;
1044 va_list args;
1045 bool b;
1046
1047 va_start(args, p1);
1048
1049 switch (status) {
Jan Engelhardta41545c2009-01-27 21:27:19 +01001050 case XTF_ONLY_ONCE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +00001051 p2 = va_arg(args, const char *);
1052 b = va_arg(args, unsigned int);
Jiri Popelkad0101692011-06-10 15:26:00 +02001053 if (!b) {
1054 va_end(args);
Jan Engelhardtaafd2692008-01-20 13:19:40 +00001055 return;
Jiri Popelkad0101692011-06-10 15:26:00 +02001056 }
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001057 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +00001058 "%s: \"%s\" option may only be specified once",
1059 p1, p2);
1060 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +01001061 case XTF_NO_INVERT:
Jan Engelhardtaafd2692008-01-20 13:19:40 +00001062 p2 = va_arg(args, const char *);
1063 b = va_arg(args, unsigned int);
Jiri Popelkad0101692011-06-10 15:26:00 +02001064 if (!b) {
1065 va_end(args);
Jan Engelhardtaafd2692008-01-20 13:19:40 +00001066 return;
Jiri Popelkad0101692011-06-10 15:26:00 +02001067 }
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001068 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +00001069 "%s: \"%s\" option cannot be inverted", p1, p2);
1070 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +01001071 case XTF_BAD_VALUE:
Jan Engelhardtaafd2692008-01-20 13:19:40 +00001072 p2 = va_arg(args, const char *);
1073 p3 = va_arg(args, const char *);
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001074 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +00001075 "%s: Bad value for \"%s\" option: \"%s\"",
1076 p1, p2, p3);
1077 break;
Jan Engelhardta41545c2009-01-27 21:27:19 +01001078 case XTF_ONE_ACTION:
Jan Engelhardtaafd2692008-01-20 13:19:40 +00001079 b = va_arg(args, unsigned int);
Jiri Popelkad0101692011-06-10 15:26:00 +02001080 if (!b) {
1081 va_end(args);
Jan Engelhardtaafd2692008-01-20 13:19:40 +00001082 return;
Jiri Popelkad0101692011-06-10 15:26:00 +02001083 }
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001084 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtaafd2692008-01-20 13:19:40 +00001085 "%s: At most one action is possible", p1);
1086 break;
1087 default:
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001088 xt_params->exit_err(status, p1, args);
Jan Engelhardtaafd2692008-01-20 13:19:40 +00001089 break;
1090 }
1091
1092 va_end(args);
1093}
Jan Engelhardt08b16162008-01-20 13:36:08 +00001094
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001095const char *xtables_ipaddr_to_numeric(const struct in_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001096{
1097 static char buf[20];
1098 const unsigned char *bytep = (const void *)&addrp->s_addr;
1099
1100 sprintf(buf, "%u.%u.%u.%u", bytep[0], bytep[1], bytep[2], bytep[3]);
1101 return buf;
1102}
1103
1104static const char *ipaddr_to_host(const struct in_addr *addr)
1105{
1106 struct hostent *host;
1107
1108 host = gethostbyaddr(addr, sizeof(struct in_addr), AF_INET);
1109 if (host == NULL)
1110 return NULL;
1111
1112 return host->h_name;
1113}
1114
1115static const char *ipaddr_to_network(const struct in_addr *addr)
1116{
1117 struct netent *net;
1118
1119 if ((net = getnetbyaddr(ntohl(addr->s_addr), AF_INET)) != NULL)
1120 return net->n_name;
1121
1122 return NULL;
1123}
1124
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001125const char *xtables_ipaddr_to_anyname(const struct in_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001126{
1127 const char *name;
1128
1129 if ((name = ipaddr_to_host(addr)) != NULL ||
1130 (name = ipaddr_to_network(addr)) != NULL)
1131 return name;
1132
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001133 return xtables_ipaddr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001134}
1135
Pablo Neira Ayusoa96166c2012-07-14 15:39:20 +02001136int xtables_ipmask_to_cidr(const struct in_addr *mask)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001137{
Jan Engelhardt08b16162008-01-20 13:36:08 +00001138 uint32_t maskaddr, bits;
1139 int i;
1140
1141 maskaddr = ntohl(mask->s_addr);
Pablo Neira Ayusoa96166c2012-07-14 15:39:20 +02001142 /* shortcut for /32 networks */
Jan Engelhardt08b16162008-01-20 13:36:08 +00001143 if (maskaddr == 0xFFFFFFFFL)
Pablo Neira Ayusoa96166c2012-07-14 15:39:20 +02001144 return 32;
Jan Engelhardt08b16162008-01-20 13:36:08 +00001145
1146 i = 32;
1147 bits = 0xFFFFFFFEL;
1148 while (--i >= 0 && maskaddr != bits)
1149 bits <<= 1;
1150 if (i >= 0)
Pablo Neira Ayusoa96166c2012-07-14 15:39:20 +02001151 return i;
1152
1153 /* this mask cannot be converted to CIDR notation */
1154 return -1;
1155}
1156
1157const char *xtables_ipmask_to_numeric(const struct in_addr *mask)
1158{
1159 static char buf[20];
1160 uint32_t cidr;
1161
1162 cidr = xtables_ipmask_to_cidr(mask);
1163 if (cidr < 0) {
Jan Engelhardt08b16162008-01-20 13:36:08 +00001164 /* mask was not a decent combination of 1's and 0's */
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001165 sprintf(buf, "/%s", xtables_ipaddr_to_numeric(mask));
Pablo Neira Ayusoa96166c2012-07-14 15:39:20 +02001166 return buf;
1167 } else if (cidr == 32) {
1168 /* we don't want to see "/32" */
1169 return "";
1170 }
Jan Engelhardt08b16162008-01-20 13:36:08 +00001171
Pablo Neira Ayusoa96166c2012-07-14 15:39:20 +02001172 sprintf(buf, "/%d", cidr);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001173 return buf;
1174}
1175
Jan Engelhardtbd943842008-01-20 13:38:08 +00001176static struct in_addr *__numeric_to_ipaddr(const char *dotted, bool is_mask)
1177{
1178 static struct in_addr addr;
1179 unsigned char *addrp;
1180 unsigned int onebyte;
1181 char buf[20], *p, *q;
1182 int i;
1183
1184 /* copy dotted string, because we need to modify it */
1185 strncpy(buf, dotted, sizeof(buf) - 1);
1186 buf[sizeof(buf) - 1] = '\0';
1187 addrp = (void *)&addr.s_addr;
1188
1189 p = buf;
1190 for (i = 0; i < 3; ++i) {
1191 if ((q = strchr(p, '.')) == NULL) {
1192 if (is_mask)
1193 return NULL;
1194
1195 /* autocomplete, this is a network address */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001196 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001197 return NULL;
1198
1199 addrp[i] = onebyte;
1200 while (i < 3)
1201 addrp[++i] = 0;
1202
1203 return &addr;
1204 }
1205
1206 *q = '\0';
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001207 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001208 return NULL;
1209
1210 addrp[i] = onebyte;
1211 p = q + 1;
1212 }
1213
1214 /* we have checked 3 bytes, now we check the last one */
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001215 if (!xtables_strtoui(p, NULL, &onebyte, 0, UINT8_MAX))
Jan Engelhardtbd943842008-01-20 13:38:08 +00001216 return NULL;
1217
1218 addrp[3] = onebyte;
1219 return &addr;
1220}
1221
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001222struct in_addr *xtables_numeric_to_ipaddr(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001223{
1224 return __numeric_to_ipaddr(dotted, false);
1225}
1226
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001227struct in_addr *xtables_numeric_to_ipmask(const char *dotted)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001228{
1229 return __numeric_to_ipaddr(dotted, true);
1230}
1231
1232static struct in_addr *network_to_ipaddr(const char *name)
1233{
1234 static struct in_addr addr;
1235 struct netent *net;
1236
1237 if ((net = getnetbyname(name)) != NULL) {
1238 if (net->n_addrtype != AF_INET)
1239 return NULL;
1240 addr.s_addr = htonl(net->n_net);
1241 return &addr;
1242 }
1243
1244 return NULL;
1245}
1246
1247static struct in_addr *host_to_ipaddr(const char *name, unsigned int *naddr)
1248{
1249 struct hostent *host;
1250 struct in_addr *addr;
1251 unsigned int i;
1252
1253 *naddr = 0;
1254 if ((host = gethostbyname(name)) != NULL) {
1255 if (host->h_addrtype != AF_INET ||
1256 host->h_length != sizeof(struct in_addr))
1257 return NULL;
1258
1259 while (host->h_addr_list[*naddr] != NULL)
1260 ++*naddr;
Wes Campaigne11e250b2011-02-21 19:10:11 -05001261 addr = xtables_calloc(*naddr, sizeof(struct in_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001262 for (i = 0; i < *naddr; i++)
1263 memcpy(&addr[i], host->h_addr_list[i],
1264 sizeof(struct in_addr));
1265 return addr;
1266 }
1267
1268 return NULL;
1269}
1270
1271static struct in_addr *
1272ipparse_hostnetwork(const char *name, unsigned int *naddrs)
1273{
1274 struct in_addr *addrptmp, *addrp;
1275
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001276 if ((addrptmp = xtables_numeric_to_ipaddr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +00001277 (addrptmp = network_to_ipaddr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001278 addrp = xtables_malloc(sizeof(struct in_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001279 memcpy(addrp, addrptmp, sizeof(*addrp));
1280 *naddrs = 1;
1281 return addrp;
1282 }
1283 if ((addrptmp = host_to_ipaddr(name, naddrs)) != NULL)
1284 return addrptmp;
1285
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001286 xt_params->exit_err(PARAMETER_PROBLEM, "host/network `%s' not found", name);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001287}
1288
1289static struct in_addr *parse_ipmask(const char *mask)
1290{
1291 static struct in_addr maskaddr;
1292 struct in_addr *addrp;
1293 unsigned int bits;
1294
1295 if (mask == NULL) {
1296 /* no mask at all defaults to 32 bits */
1297 maskaddr.s_addr = 0xFFFFFFFF;
1298 return &maskaddr;
1299 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001300 if ((addrp = xtables_numeric_to_ipmask(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001301 /* dotted_to_addr already returns a network byte order addr */
1302 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001303 if (!xtables_strtoui(mask, NULL, &bits, 0, 32))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001304 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtbd943842008-01-20 13:38:08 +00001305 "invalid mask `%s' specified", mask);
1306 if (bits != 0) {
1307 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits));
1308 return &maskaddr;
1309 }
1310
1311 maskaddr.s_addr = 0U;
1312 return &maskaddr;
1313}
1314
Michael Granzow332e4ac2009-04-09 18:24:36 +01001315void xtables_ipparse_multiple(const char *name, struct in_addr **addrpp,
1316 struct in_addr **maskpp, unsigned int *naddrs)
1317{
1318 struct in_addr *addrp;
Jan Engelhardtc0e69db2011-07-09 16:01:18 +02001319 char buf[256], *p, *next;
Michael Granzow332e4ac2009-04-09 18:24:36 +01001320 unsigned int len, i, j, n, count = 1;
1321 const char *loop = name;
1322
1323 while ((loop = strchr(loop, ',')) != NULL) {
1324 ++count;
1325 ++loop; /* skip ',' */
1326 }
1327
1328 *addrpp = xtables_malloc(sizeof(struct in_addr) * count);
1329 *maskpp = xtables_malloc(sizeof(struct in_addr) * count);
1330
1331 loop = name;
1332
1333 for (i = 0; i < count; ++i) {
Jan Engelhardt0c384442011-07-09 16:19:09 +02001334 while (isspace(*loop))
1335 ++loop;
Jan Engelhardtc0e69db2011-07-09 16:01:18 +02001336 next = strchr(loop, ',');
1337 if (next != NULL)
1338 len = next - loop;
Michael Granzow332e4ac2009-04-09 18:24:36 +01001339 else
1340 len = strlen(loop);
Jan Engelhardtc0e69db2011-07-09 16:01:18 +02001341 if (len > sizeof(buf) - 1)
1342 xt_params->exit_err(PARAMETER_PROBLEM,
1343 "Hostname too long");
Michael Granzow332e4ac2009-04-09 18:24:36 +01001344
1345 strncpy(buf, loop, len);
1346 buf[len] = '\0';
Michael Granzow332e4ac2009-04-09 18:24:36 +01001347 if ((p = strrchr(buf, '/')) != NULL) {
1348 *p = '\0';
1349 addrp = parse_ipmask(p + 1);
1350 } else {
1351 addrp = parse_ipmask(NULL);
1352 }
1353 memcpy(*maskpp + i, addrp, sizeof(*addrp));
1354
1355 /* if a null mask is given, the name is ignored, like in "any/0" */
1356 if ((*maskpp + i)->s_addr == 0)
1357 /*
1358 * A bit pointless to process multiple addresses
1359 * in this case...
1360 */
1361 strcpy(buf, "0.0.0.0");
1362
1363 addrp = ipparse_hostnetwork(buf, &n);
1364 if (n > 1) {
1365 count += n - 1;
1366 *addrpp = xtables_realloc(*addrpp,
1367 sizeof(struct in_addr) * count);
1368 *maskpp = xtables_realloc(*maskpp,
1369 sizeof(struct in_addr) * count);
1370 for (j = 0; j < n; ++j)
1371 /* for each new addr */
1372 memcpy(*addrpp + i + j, addrp + j,
1373 sizeof(*addrp));
1374 for (j = 1; j < n; ++j)
1375 /* for each new mask */
1376 memcpy(*maskpp + i + j, *maskpp + i,
1377 sizeof(*addrp));
1378 i += n - 1;
1379 } else {
1380 memcpy(*addrpp + i, addrp, sizeof(*addrp));
1381 }
1382 /* free what ipparse_hostnetwork had allocated: */
1383 free(addrp);
Jan Engelhardtc0e69db2011-07-09 16:01:18 +02001384 if (next == NULL)
1385 break;
1386 loop = next + 1;
Michael Granzow332e4ac2009-04-09 18:24:36 +01001387 }
1388 *naddrs = count;
Jan Engelhardt4b110b42011-02-21 03:21:18 +01001389 for (i = 0; i < count; ++i)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001390 (*addrpp+i)->s_addr &= (*maskpp+i)->s_addr;
1391}
1392
1393
Jan Engelhardta0baae82009-01-30 04:32:50 +01001394/**
1395 * xtables_ipparse_any - transform arbitrary name to in_addr
1396 *
1397 * Possible inputs (pseudo regex):
1398 * m{^($hostname|$networkname|$ipaddr)(/$mask)?}
1399 * "1.2.3.4/5", "1.2.3.4", "hostname", "networkname"
1400 */
1401void xtables_ipparse_any(const char *name, struct in_addr **addrpp,
1402 struct in_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001403{
1404 unsigned int i, j, k, n;
1405 struct in_addr *addrp;
1406 char buf[256], *p;
1407
1408 strncpy(buf, name, sizeof(buf) - 1);
1409 buf[sizeof(buf) - 1] = '\0';
1410 if ((p = strrchr(buf, '/')) != NULL) {
1411 *p = '\0';
1412 addrp = parse_ipmask(p + 1);
1413 } else {
1414 addrp = parse_ipmask(NULL);
1415 }
1416 memcpy(maskp, addrp, sizeof(*maskp));
1417
1418 /* if a null mask is given, the name is ignored, like in "any/0" */
1419 if (maskp->s_addr == 0U)
1420 strcpy(buf, "0.0.0.0");
1421
1422 addrp = *addrpp = ipparse_hostnetwork(buf, naddrs);
1423 n = *naddrs;
1424 for (i = 0, j = 0; i < n; ++i) {
1425 addrp[j++].s_addr &= maskp->s_addr;
1426 for (k = 0; k < j - 1; ++k)
1427 if (addrp[k].s_addr == addrp[j-1].s_addr) {
Wes Campaigneadcb2812011-02-21 19:10:12 -05001428 /*
1429 * Nuke the dup by copying an address from the
1430 * tail here, and check the current position
1431 * again (--j).
1432 */
1433 memcpy(&addrp[--j], &addrp[--*naddrs],
1434 sizeof(struct in_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001435 break;
1436 }
1437 }
1438}
1439
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001440const char *xtables_ip6addr_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001441{
Maciej Zenczykowskicf3e52d2011-04-04 15:31:09 +02001442 /* 0000:0000:0000:0000:0000:0000:000.000.000.000
Jan Engelhardt08b16162008-01-20 13:36:08 +00001443 * 0000:0000:0000:0000:0000:0000:0000:0000 */
1444 static char buf[50+1];
1445 return inet_ntop(AF_INET6, addrp, buf, sizeof(buf));
1446}
1447
1448static const char *ip6addr_to_host(const struct in6_addr *addr)
1449{
1450 static char hostname[NI_MAXHOST];
1451 struct sockaddr_in6 saddr;
1452 int err;
1453
1454 memset(&saddr, 0, sizeof(struct sockaddr_in6));
1455 memcpy(&saddr.sin6_addr, addr, sizeof(*addr));
1456 saddr.sin6_family = AF_INET6;
1457
1458 err = getnameinfo((const void *)&saddr, sizeof(struct sockaddr_in6),
1459 hostname, sizeof(hostname) - 1, NULL, 0, 0);
1460 if (err != 0) {
1461#ifdef DEBUG
1462 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err));
1463#endif
1464 return NULL;
1465 }
1466
1467#ifdef DEBUG
1468 fprintf (stderr, "\naddr2host: %s\n", hostname);
1469#endif
1470 return hostname;
1471}
1472
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001473const char *xtables_ip6addr_to_anyname(const struct in6_addr *addr)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001474{
1475 const char *name;
1476
1477 if ((name = ip6addr_to_host(addr)) != NULL)
1478 return name;
1479
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001480 return xtables_ip6addr_to_numeric(addr);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001481}
1482
Pablo Neira Ayusoa96166c2012-07-14 15:39:20 +02001483int xtables_ip6mask_to_cidr(const struct in6_addr *k)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001484{
1485 unsigned int bits = 0;
1486 uint32_t a, b, c, d;
1487
Jan Engelhardt48607812008-06-10 15:17:53 +02001488 a = ntohl(k->s6_addr32[0]);
1489 b = ntohl(k->s6_addr32[1]);
1490 c = ntohl(k->s6_addr32[2]);
1491 d = ntohl(k->s6_addr32[3]);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001492 while (a & 0x80000000U) {
1493 ++bits;
1494 a <<= 1;
1495 a |= (b >> 31) & 1;
1496 b <<= 1;
1497 b |= (c >> 31) & 1;
1498 c <<= 1;
1499 c |= (d >> 31) & 1;
1500 d <<= 1;
1501 }
1502 if (a != 0 || b != 0 || c != 0 || d != 0)
1503 return -1;
1504 return bits;
1505}
1506
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001507const char *xtables_ip6mask_to_numeric(const struct in6_addr *addrp)
Jan Engelhardt08b16162008-01-20 13:36:08 +00001508{
1509 static char buf[50+2];
Pablo Neira Ayusoa96166c2012-07-14 15:39:20 +02001510 int l = xtables_ip6mask_to_cidr(addrp);
Jan Engelhardt08b16162008-01-20 13:36:08 +00001511
1512 if (l == -1) {
1513 strcpy(buf, "/");
Jan Engelhardte44ea7f2009-01-30 03:55:09 +01001514 strcat(buf, xtables_ip6addr_to_numeric(addrp));
Jan Engelhardt08b16162008-01-20 13:36:08 +00001515 return buf;
1516 }
1517 sprintf(buf, "/%d", l);
1518 return buf;
1519}
Jan Engelhardtbd943842008-01-20 13:38:08 +00001520
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001521struct in6_addr *xtables_numeric_to_ip6addr(const char *num)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001522{
1523 static struct in6_addr ap;
1524 int err;
1525
1526 if ((err = inet_pton(AF_INET6, num, &ap)) == 1)
1527 return &ap;
1528#ifdef DEBUG
1529 fprintf(stderr, "\nnumeric2addr: %d\n", err);
1530#endif
1531 return NULL;
1532}
1533
1534static struct in6_addr *
1535host_to_ip6addr(const char *name, unsigned int *naddr)
1536{
Wes Campaigne2ad8dc82011-02-21 19:10:10 -05001537 struct in6_addr *addr;
Jan Engelhardtbd943842008-01-20 13:38:08 +00001538 struct addrinfo hints;
Wes Campaigne2ad8dc82011-02-21 19:10:10 -05001539 struct addrinfo *res, *p;
Jan Engelhardtbd943842008-01-20 13:38:08 +00001540 int err;
Wes Campaigne2ad8dc82011-02-21 19:10:10 -05001541 unsigned int i;
Jan Engelhardtbd943842008-01-20 13:38:08 +00001542
1543 memset(&hints, 0, sizeof(hints));
1544 hints.ai_flags = AI_CANONNAME;
1545 hints.ai_family = AF_INET6;
1546 hints.ai_socktype = SOCK_RAW;
Jan Engelhardtbd943842008-01-20 13:38:08 +00001547
1548 *naddr = 0;
1549 if ((err = getaddrinfo(name, NULL, &hints, &res)) != 0) {
1550#ifdef DEBUG
1551 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err));
1552#endif
1553 return NULL;
1554 } else {
Wes Campaigne2ad8dc82011-02-21 19:10:10 -05001555 /* Find length of address chain */
1556 for (p = res; p != NULL; p = p->ai_next)
1557 ++*naddr;
Jan Engelhardtbd943842008-01-20 13:38:08 +00001558#ifdef DEBUG
1559 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen,
Patrick McHardy30290ae2010-05-20 15:41:03 +02001560 xtables_ip6addr_to_numeric(&((struct sockaddr_in6 *)res->ai_addr)->sin6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001561#endif
Wes Campaigne2ad8dc82011-02-21 19:10:10 -05001562 /* Copy each element of the address chain */
1563 addr = xtables_calloc(*naddr, sizeof(struct in6_addr));
1564 for (i = 0, p = res; p != NULL; p = p->ai_next)
1565 memcpy(&addr[i++],
1566 &((const struct sockaddr_in6 *)p->ai_addr)->sin6_addr,
1567 sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001568 freeaddrinfo(res);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001569 return addr;
1570 }
1571
1572 return NULL;
1573}
1574
1575static struct in6_addr *network_to_ip6addr(const char *name)
1576{
1577 /* abort();*/
1578 /* TODO: not implemented yet, but the exception breaks the
1579 * name resolvation */
1580 return NULL;
1581}
1582
1583static struct in6_addr *
1584ip6parse_hostnetwork(const char *name, unsigned int *naddrs)
1585{
1586 struct in6_addr *addrp, *addrptmp;
1587
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001588 if ((addrptmp = xtables_numeric_to_ip6addr(name)) != NULL ||
Jan Engelhardtbd943842008-01-20 13:38:08 +00001589 (addrptmp = network_to_ip6addr(name)) != NULL) {
Jan Engelhardt630ef482009-01-27 14:58:41 +01001590 addrp = xtables_malloc(sizeof(struct in6_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001591 memcpy(addrp, addrptmp, sizeof(*addrp));
1592 *naddrs = 1;
1593 return addrp;
1594 }
1595 if ((addrp = host_to_ip6addr(name, naddrs)) != NULL)
1596 return addrp;
1597
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001598 xt_params->exit_err(PARAMETER_PROBLEM, "host/network `%s' not found", name);
Jan Engelhardtbd943842008-01-20 13:38:08 +00001599}
1600
1601static struct in6_addr *parse_ip6mask(char *mask)
1602{
1603 static struct in6_addr maskaddr;
1604 struct in6_addr *addrp;
1605 unsigned int bits;
1606
1607 if (mask == NULL) {
1608 /* no mask at all defaults to 128 bits */
1609 memset(&maskaddr, 0xff, sizeof maskaddr);
1610 return &maskaddr;
1611 }
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +01001612 if ((addrp = xtables_numeric_to_ip6addr(mask)) != NULL)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001613 return addrp;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +01001614 if (!xtables_strtoui(mask, NULL, &bits, 0, 128))
Jamal Hadi Salim8b7baeb2009-02-11 13:05:43 +01001615 xt_params->exit_err(PARAMETER_PROBLEM,
Jan Engelhardtbd943842008-01-20 13:38:08 +00001616 "invalid mask `%s' specified", mask);
1617 if (bits != 0) {
1618 char *p = (void *)&maskaddr;
1619 memset(p, 0xff, bits / 8);
1620 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8);
1621 p[bits/8] = 0xff << (8 - (bits & 7));
1622 return &maskaddr;
1623 }
1624
1625 memset(&maskaddr, 0, sizeof(maskaddr));
1626 return &maskaddr;
1627}
1628
Michael Granzow332e4ac2009-04-09 18:24:36 +01001629void
1630xtables_ip6parse_multiple(const char *name, struct in6_addr **addrpp,
1631 struct in6_addr **maskpp, unsigned int *naddrs)
1632{
Olaf Rempel58df9012009-09-20 13:24:11 +02001633 static const struct in6_addr zero_addr;
Michael Granzow332e4ac2009-04-09 18:24:36 +01001634 struct in6_addr *addrp;
Jan Engelhardtc0e69db2011-07-09 16:01:18 +02001635 char buf[256], *p, *next;
Michael Granzow332e4ac2009-04-09 18:24:36 +01001636 unsigned int len, i, j, n, count = 1;
1637 const char *loop = name;
1638
1639 while ((loop = strchr(loop, ',')) != NULL) {
1640 ++count;
1641 ++loop; /* skip ',' */
1642 }
1643
1644 *addrpp = xtables_malloc(sizeof(struct in6_addr) * count);
1645 *maskpp = xtables_malloc(sizeof(struct in6_addr) * count);
1646
1647 loop = name;
1648
1649 for (i = 0; i < count /*NB: count can grow*/; ++i) {
Jan Engelhardt0c384442011-07-09 16:19:09 +02001650 while (isspace(*loop))
1651 ++loop;
Jan Engelhardtc0e69db2011-07-09 16:01:18 +02001652 next = strchr(loop, ',');
1653 if (next != NULL)
1654 len = next - loop;
Michael Granzow332e4ac2009-04-09 18:24:36 +01001655 else
1656 len = strlen(loop);
Jan Engelhardtc0e69db2011-07-09 16:01:18 +02001657 if (len > sizeof(buf) - 1)
1658 xt_params->exit_err(PARAMETER_PROBLEM,
1659 "Hostname too long");
Michael Granzow332e4ac2009-04-09 18:24:36 +01001660
1661 strncpy(buf, loop, len);
1662 buf[len] = '\0';
Michael Granzow332e4ac2009-04-09 18:24:36 +01001663 if ((p = strrchr(buf, '/')) != NULL) {
1664 *p = '\0';
1665 addrp = parse_ip6mask(p + 1);
1666 } else {
1667 addrp = parse_ip6mask(NULL);
1668 }
1669 memcpy(*maskpp + i, addrp, sizeof(*addrp));
1670
1671 /* if a null mask is given, the name is ignored, like in "any/0" */
Olaf Rempel58df9012009-09-20 13:24:11 +02001672 if (memcmp(*maskpp + i, &zero_addr, sizeof(zero_addr)) == 0)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001673 strcpy(buf, "::");
1674
1675 addrp = ip6parse_hostnetwork(buf, &n);
Michael Granzow332e4ac2009-04-09 18:24:36 +01001676 if (n > 1) {
1677 count += n - 1;
1678 *addrpp = xtables_realloc(*addrpp,
1679 sizeof(struct in6_addr) * count);
1680 *maskpp = xtables_realloc(*maskpp,
1681 sizeof(struct in6_addr) * count);
1682 for (j = 0; j < n; ++j)
1683 /* for each new addr */
1684 memcpy(*addrpp + i + j, addrp + j,
1685 sizeof(*addrp));
1686 for (j = 1; j < n; ++j)
1687 /* for each new mask */
1688 memcpy(*maskpp + i + j, *maskpp + i,
1689 sizeof(*addrp));
1690 i += n - 1;
1691 } else {
1692 memcpy(*addrpp + i, addrp, sizeof(*addrp));
1693 }
1694 /* free what ip6parse_hostnetwork had allocated: */
1695 free(addrp);
Jan Engelhardtc0e69db2011-07-09 16:01:18 +02001696 if (next == NULL)
1697 break;
1698 loop = next + 1;
Michael Granzow332e4ac2009-04-09 18:24:36 +01001699 }
1700 *naddrs = count;
Jan Engelhardt4b110b42011-02-21 03:21:18 +01001701 for (i = 0; i < count; ++i)
Michael Granzow332e4ac2009-04-09 18:24:36 +01001702 for (j = 0; j < 4; ++j)
1703 (*addrpp+i)->s6_addr32[j] &= (*maskpp+i)->s6_addr32[j];
1704}
1705
Jan Engelhardta0baae82009-01-30 04:32:50 +01001706void xtables_ip6parse_any(const char *name, struct in6_addr **addrpp,
1707 struct in6_addr *maskp, unsigned int *naddrs)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001708{
Jan Engelhardt9c0fa7d2009-04-03 22:40:35 +02001709 static const struct in6_addr zero_addr;
Jan Engelhardtbd943842008-01-20 13:38:08 +00001710 struct in6_addr *addrp;
1711 unsigned int i, j, k, n;
1712 char buf[256], *p;
1713
1714 strncpy(buf, name, sizeof(buf) - 1);
1715 buf[sizeof(buf)-1] = '\0';
1716 if ((p = strrchr(buf, '/')) != NULL) {
1717 *p = '\0';
1718 addrp = parse_ip6mask(p + 1);
1719 } else {
1720 addrp = parse_ip6mask(NULL);
1721 }
1722 memcpy(maskp, addrp, sizeof(*maskp));
1723
1724 /* if a null mask is given, the name is ignored, like in "any/0" */
Jan Engelhardt9c0fa7d2009-04-03 22:40:35 +02001725 if (memcmp(maskp, &zero_addr, sizeof(zero_addr)) == 0)
Jan Engelhardtbd943842008-01-20 13:38:08 +00001726 strcpy(buf, "::");
1727
1728 addrp = *addrpp = ip6parse_hostnetwork(buf, naddrs);
1729 n = *naddrs;
1730 for (i = 0, j = 0; i < n; ++i) {
1731 for (k = 0; k < 4; ++k)
Yasuyuki Kozakai5a2208c2008-06-04 15:16:03 +02001732 addrp[j].s6_addr32[k] &= maskp->s6_addr32[k];
Jan Engelhardtbd943842008-01-20 13:38:08 +00001733 ++j;
1734 for (k = 0; k < j - 1; ++k)
1735 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) {
Wes Campaigneadcb2812011-02-21 19:10:12 -05001736 /*
1737 * Nuke the dup by copying an address from the
1738 * tail here, and check the current position
1739 * again (--j).
1740 */
1741 memcpy(&addrp[--j], &addrp[--*naddrs],
1742 sizeof(struct in_addr));
Jan Engelhardtbd943842008-01-20 13:38:08 +00001743 break;
1744 }
1745 }
1746}
Max Kellermanna5d09942008-01-29 13:44:34 +00001747
Jan Engelhardta0baae82009-01-30 04:32:50 +01001748void xtables_save_string(const char *value)
Max Kellermanna5d09942008-01-29 13:44:34 +00001749{
1750 static const char no_quote_chars[] = "_-0123456789"
1751 "abcdefghijklmnopqrstuvwxyz"
1752 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1753 static const char escape_chars[] = "\"\\'";
1754 size_t length;
1755 const char *p;
1756
Max Kellerman87dc7c42011-02-17 11:57:19 +01001757 length = strspn(value, no_quote_chars);
Max Kellermanna5d09942008-01-29 13:44:34 +00001758 if (length > 0 && value[length] == 0) {
1759 /* no quoting required */
Max Kellermanna5d09942008-01-29 13:44:34 +00001760 putchar(' ');
Jan Engelhardt73866352010-12-18 02:04:59 +01001761 fputs(value, stdout);
Max Kellermanna5d09942008-01-29 13:44:34 +00001762 } else {
1763 /* there is at least one dangerous character in the
1764 value, which we have to quote. Write double quotes
1765 around the value and escape special characters with
1766 a backslash */
Jan Engelhardt73866352010-12-18 02:04:59 +01001767 printf(" \"");
Max Kellermanna5d09942008-01-29 13:44:34 +00001768
1769 for (p = strpbrk(value, escape_chars); p != NULL;
1770 p = strpbrk(value, escape_chars)) {
1771 if (p > value)
1772 fwrite(value, 1, p - value, stdout);
1773 putchar('\\');
1774 putchar(*p);
1775 value = p + 1;
1776 }
1777
1778 /* print the rest and finish the double quoted
1779 string */
1780 fputs(value, stdout);
Jan Engelhardt73866352010-12-18 02:04:59 +01001781 putchar('\"');
Max Kellermanna5d09942008-01-29 13:44:34 +00001782 }
1783}
Jan Engelhardt0f16c722009-01-30 04:55:38 +01001784
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001785const struct xtables_pprot xtables_chain_protos[] = {
1786 {"tcp", IPPROTO_TCP},
1787 {"sctp", IPPROTO_SCTP},
1788 {"udp", IPPROTO_UDP},
1789 {"udplite", IPPROTO_UDPLITE},
1790 {"icmp", IPPROTO_ICMP},
1791 {"icmpv6", IPPROTO_ICMPV6},
1792 {"ipv6-icmp", IPPROTO_ICMPV6},
1793 {"esp", IPPROTO_ESP},
1794 {"ah", IPPROTO_AH},
1795 {"ipv6-mh", IPPROTO_MH},
1796 {"mh", IPPROTO_MH},
1797 {"all", 0},
1798 {NULL},
1799};
1800
Jan Engelhardt7ac40522011-01-07 12:34:04 +01001801uint16_t
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001802xtables_parse_protocol(const char *s)
1803{
Jan Engelhardt85f423a2011-05-12 14:03:36 +02001804 const struct protoent *pent;
1805 unsigned int proto, i;
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001806
Jan Engelhardt85f423a2011-05-12 14:03:36 +02001807 if (xtables_strtoui(s, NULL, &proto, 0, UINT8_MAX))
1808 return proto;
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001809
Jan Engelhardt85f423a2011-05-12 14:03:36 +02001810 /* first deal with the special case of 'all' to prevent
1811 * people from being able to redefine 'all' in nsswitch
1812 * and/or provoke expensive [not working] ldap/nis/...
1813 * lookups */
1814 if (strcmp(s, "all") == 0)
1815 return 0;
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001816
Jan Engelhardt85f423a2011-05-12 14:03:36 +02001817 pent = getprotobyname(s);
1818 if (pent != NULL)
1819 return pent->p_proto;
Pablo Neira Ayusoe55cc4a2009-05-12 09:51:26 +02001820
Jan Engelhardt85f423a2011-05-12 14:03:36 +02001821 for (i = 0; i < ARRAY_SIZE(xtables_chain_protos); ++i) {
1822 if (xtables_chain_protos[i].name == NULL)
1823 continue;
1824 if (strcmp(s, xtables_chain_protos[i].name) == 0)
1825 return xtables_chain_protos[i].num;
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001826 }
Jan Engelhardt85f423a2011-05-12 14:03:36 +02001827 xt_params->exit_err(PARAMETER_PROBLEM,
1828 "unknown protocol \"%s\" specified", s);
1829 return -1;
Jan Engelhardt1de7edf2009-01-30 05:38:11 +01001830}
Jan Engelhardtf56b8a82011-09-03 14:27:55 +02001831
1832int kernel_version;
1833
1834void get_kernel_version(void)
1835{
1836 static struct utsname uts;
1837 int x = 0, y = 0, z = 0;
1838
1839 if (uname(&uts) == -1) {
1840 fprintf(stderr, "Unable to retrieve kernel version.\n");
1841 xtables_free_opts(1);
1842 exit(1);
1843 }
1844
1845 sscanf(uts.release, "%d.%d.%d", &x, &y, &z);
1846 kernel_version = LINUX_VERSION(x, y, z);
1847}