xtables.c 47 KB

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