xtoptions.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  1. /*
  2. * Argument parser
  3. * Copyright © Jan Engelhardt, 2011
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or (at your option) any later version.
  9. */
  10. #include <ctype.h>
  11. #include <errno.h>
  12. #include <getopt.h>
  13. #include <limits.h>
  14. #include <netdb.h>
  15. #include <stdbool.h>
  16. #include <stdint.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <syslog.h>
  21. #include <arpa/inet.h>
  22. #include <netinet/ip.h>
  23. #include "xtables.h"
  24. #include "xshared.h"
  25. #ifndef IPTOS_NORMALSVC
  26. # define IPTOS_NORMALSVC 0
  27. #endif
  28. #define XTOPT_MKPTR(cb) \
  29. ((void *)((char *)(cb)->data + (cb)->entry->ptroff))
  30. /**
  31. * Simple key-value pairs for syslog levels
  32. */
  33. struct syslog_level {
  34. char name[8];
  35. uint8_t level;
  36. };
  37. struct tos_value_mask {
  38. uint8_t value, mask;
  39. };
  40. static const size_t xtopt_psize[] = {
  41. /*
  42. * All types not listed here, and thus essentially being initialized to
  43. * zero have zero on purpose.
  44. */
  45. [XTTYPE_UINT8] = sizeof(uint8_t),
  46. [XTTYPE_UINT16] = sizeof(uint16_t),
  47. [XTTYPE_UINT32] = sizeof(uint32_t),
  48. [XTTYPE_UINT64] = sizeof(uint64_t),
  49. [XTTYPE_UINT8RC] = sizeof(uint8_t[2]),
  50. [XTTYPE_UINT16RC] = sizeof(uint16_t[2]),
  51. [XTTYPE_UINT32RC] = sizeof(uint32_t[2]),
  52. [XTTYPE_UINT64RC] = sizeof(uint64_t[2]),
  53. [XTTYPE_DOUBLE] = sizeof(double),
  54. [XTTYPE_STRING] = -1,
  55. [XTTYPE_SYSLOGLEVEL] = sizeof(uint8_t),
  56. [XTTYPE_HOST] = sizeof(union nf_inet_addr),
  57. [XTTYPE_HOSTMASK] = sizeof(union nf_inet_addr),
  58. [XTTYPE_PROTOCOL] = sizeof(uint8_t),
  59. [XTTYPE_PORT] = sizeof(uint16_t),
  60. [XTTYPE_PORTRC] = sizeof(uint16_t[2]),
  61. [XTTYPE_PLENMASK] = sizeof(union nf_inet_addr),
  62. [XTTYPE_ETHERMAC] = sizeof(uint8_t[6]),
  63. };
  64. /**
  65. * Creates getopt options from the x6-style option map, and assigns each a
  66. * getopt id.
  67. */
  68. struct option *
  69. xtables_options_xfrm(struct option *orig_opts, struct option *oldopts,
  70. const struct xt_option_entry *entry, unsigned int *offset)
  71. {
  72. unsigned int num_orig, num_old = 0, num_new, i;
  73. struct option *merge, *mp;
  74. if (entry == NULL)
  75. return oldopts;
  76. for (num_orig = 0; orig_opts[num_orig].name != NULL; ++num_orig)
  77. ;
  78. if (oldopts != NULL)
  79. for (num_old = 0; oldopts[num_old].name != NULL; ++num_old)
  80. ;
  81. for (num_new = 0; entry[num_new].name != NULL; ++num_new)
  82. ;
  83. /*
  84. * Since @oldopts also has @orig_opts already (and does so at the
  85. * start), skip these entries.
  86. */
  87. oldopts += num_orig;
  88. num_old -= num_orig;
  89. merge = malloc(sizeof(*mp) * (num_orig + num_old + num_new + 1));
  90. if (merge == NULL)
  91. return NULL;
  92. /* Let the base options -[ADI...] have precedence over everything */
  93. memcpy(merge, orig_opts, sizeof(*mp) * num_orig);
  94. mp = merge + num_orig;
  95. /* Second, the new options */
  96. xt_params->option_offset += XT_OPTION_OFFSET_SCALE;
  97. *offset = xt_params->option_offset;
  98. for (i = 0; i < num_new; ++i, ++mp, ++entry) {
  99. mp->name = entry->name;
  100. mp->has_arg = entry->type != XTTYPE_NONE;
  101. mp->flag = NULL;
  102. mp->val = entry->id + *offset;
  103. }
  104. /* Third, the old options */
  105. memcpy(mp, oldopts, sizeof(*mp) * num_old);
  106. mp += num_old;
  107. xtables_free_opts(0);
  108. /* Clear trailing entry */
  109. memset(mp, 0, sizeof(*mp));
  110. return merge;
  111. }
  112. /**
  113. * Give the upper limit for a certain type.
  114. */
  115. static uintmax_t xtopt_max_by_type(enum xt_option_type type)
  116. {
  117. switch (type) {
  118. case XTTYPE_UINT8:
  119. case XTTYPE_UINT8RC:
  120. return UINT8_MAX;
  121. case XTTYPE_UINT16:
  122. case XTTYPE_UINT16RC:
  123. return UINT16_MAX;
  124. case XTTYPE_UINT32:
  125. case XTTYPE_UINT32RC:
  126. return UINT32_MAX;
  127. case XTTYPE_UINT64:
  128. case XTTYPE_UINT64RC:
  129. return UINT64_MAX;
  130. default:
  131. return 0;
  132. }
  133. }
  134. /**
  135. * Return the size of a single entity based upon a type - predominantly an
  136. * XTTYPE_UINT*RC type.
  137. */
  138. static size_t xtopt_esize_by_type(enum xt_option_type type)
  139. {
  140. switch (type) {
  141. case XTTYPE_UINT8RC:
  142. return xtopt_psize[XTTYPE_UINT8];
  143. case XTTYPE_UINT16RC:
  144. return xtopt_psize[XTTYPE_UINT16];
  145. case XTTYPE_UINT32RC:
  146. return xtopt_psize[XTTYPE_UINT32];
  147. case XTTYPE_UINT64RC:
  148. return xtopt_psize[XTTYPE_UINT64];
  149. default:
  150. return xtopt_psize[type];
  151. }
  152. }
  153. /**
  154. * Require a simple integer.
  155. */
  156. static void xtopt_parse_int(struct xt_option_call *cb)
  157. {
  158. const struct xt_option_entry *entry = cb->entry;
  159. uintmax_t lmin = 0, lmax = xtopt_max_by_type(entry->type);
  160. uintmax_t value;
  161. if (cb->entry->min != 0)
  162. lmin = cb->entry->min;
  163. if (cb->entry->max != 0)
  164. lmax = cb->entry->max;
  165. if (!xtables_strtoul(cb->arg, NULL, &value, lmin, lmax))
  166. xt_params->exit_err(PARAMETER_PROBLEM,
  167. "%s: bad value for option \"--%s\", "
  168. "or out of range (%ju-%ju).\n",
  169. cb->ext_name, entry->name, lmin, lmax);
  170. if (entry->type == XTTYPE_UINT8) {
  171. cb->val.u8 = value;
  172. if (entry->flags & XTOPT_PUT)
  173. *(uint8_t *)XTOPT_MKPTR(cb) = cb->val.u8;
  174. } else if (entry->type == XTTYPE_UINT16) {
  175. cb->val.u16 = value;
  176. if (entry->flags & XTOPT_PUT)
  177. *(uint16_t *)XTOPT_MKPTR(cb) = cb->val.u16;
  178. } else if (entry->type == XTTYPE_UINT32) {
  179. cb->val.u32 = value;
  180. if (entry->flags & XTOPT_PUT)
  181. *(uint32_t *)XTOPT_MKPTR(cb) = cb->val.u32;
  182. } else if (entry->type == XTTYPE_UINT64) {
  183. cb->val.u64 = value;
  184. if (entry->flags & XTOPT_PUT)
  185. *(uint64_t *)XTOPT_MKPTR(cb) = cb->val.u64;
  186. }
  187. }
  188. /**
  189. * Require a simple floating point number.
  190. */
  191. static void xtopt_parse_float(struct xt_option_call *cb)
  192. {
  193. const struct xt_option_entry *entry = cb->entry;
  194. double value;
  195. char *end;
  196. value = strtod(cb->arg, &end);
  197. if (end == cb->arg || *end != '\0' ||
  198. (entry->min != entry->max &&
  199. (value < entry->min || value > entry->max)))
  200. xt_params->exit_err(PARAMETER_PROBLEM,
  201. "%s: bad value for option \"--%s\", "
  202. "or out of range (%u-%u).\n",
  203. cb->ext_name, entry->name, entry->min, entry->max);
  204. cb->val.dbl = value;
  205. if (entry->flags & XTOPT_PUT)
  206. *(double *)XTOPT_MKPTR(cb) = cb->val.dbl;
  207. }
  208. /**
  209. * Copy the parsed value to the appropriate entry in cb->val.
  210. */
  211. static void xtopt_mint_value_to_cb(struct xt_option_call *cb, uintmax_t value)
  212. {
  213. const struct xt_option_entry *entry = cb->entry;
  214. if (cb->nvals >= ARRAY_SIZE(cb->val.u32_range))
  215. return;
  216. if (entry->type == XTTYPE_UINT8RC)
  217. cb->val.u8_range[cb->nvals] = value;
  218. else if (entry->type == XTTYPE_UINT16RC)
  219. cb->val.u16_range[cb->nvals] = value;
  220. else if (entry->type == XTTYPE_UINT32RC)
  221. cb->val.u32_range[cb->nvals] = value;
  222. else if (entry->type == XTTYPE_UINT64RC)
  223. cb->val.u64_range[cb->nvals] = value;
  224. }
  225. /**
  226. * Copy the parsed value to the data area, using appropriate type access.
  227. */
  228. static void xtopt_mint_value_to_ptr(struct xt_option_call *cb, void **datap,
  229. uintmax_t value)
  230. {
  231. const struct xt_option_entry *entry = cb->entry;
  232. void *data = *datap;
  233. if (!(entry->flags & XTOPT_PUT))
  234. return;
  235. if (entry->type == XTTYPE_UINT8RC)
  236. *(uint8_t *)data = value;
  237. else if (entry->type == XTTYPE_UINT16RC)
  238. *(uint16_t *)data = value;
  239. else if (entry->type == XTTYPE_UINT32RC)
  240. *(uint32_t *)data = value;
  241. else if (entry->type == XTTYPE_UINT64RC)
  242. *(uint64_t *)data = value;
  243. data += xtopt_esize_by_type(entry->type);
  244. *datap = data;
  245. }
  246. /**
  247. * Multiple integer parse routine.
  248. *
  249. * This function is capable of parsing any number of fields. Only the first
  250. * two values from the string will be put into @cb however (and as such,
  251. * @cb->val.uXX_range is just that large) to cater for the few extensions that
  252. * do not have a range[2] field, but {min, max}, and which cannot use
  253. * XTOPT_POINTER.
  254. */
  255. static void xtopt_parse_mint(struct xt_option_call *cb)
  256. {
  257. const struct xt_option_entry *entry = cb->entry;
  258. const char *arg = cb->arg;
  259. size_t esize = xtopt_esize_by_type(entry->type);
  260. const uintmax_t lmax = xtopt_max_by_type(entry->type);
  261. void *put = XTOPT_MKPTR(cb);
  262. unsigned int maxiter;
  263. uintmax_t value;
  264. char *end = "";
  265. char sep = ':';
  266. maxiter = entry->size / esize;
  267. if (maxiter == 0)
  268. maxiter = ARRAY_SIZE(cb->val.u32_range);
  269. if (entry->size % esize != 0)
  270. xt_params->exit_err(OTHER_PROBLEM, "%s: memory block does "
  271. "not have proper size\n", __func__);
  272. cb->nvals = 0;
  273. for (arg = cb->arg, end = (char *)arg; ; arg = end + 1) {
  274. if (cb->nvals == maxiter)
  275. xt_params->exit_err(PARAMETER_PROBLEM, "%s: Too many "
  276. "components for option \"--%s\" (max: %u)\n",
  277. cb->ext_name, entry->name, maxiter);
  278. if (*arg == '\0' || *arg == sep) {
  279. /* Default range components when field not spec'd. */
  280. end = (char *)arg;
  281. value = (cb->nvals == 1) ? lmax : 0;
  282. } else {
  283. if (!xtables_strtoul(arg, &end, &value, 0, lmax))
  284. xt_params->exit_err(PARAMETER_PROBLEM,
  285. "%s: bad value for option \"--%s\" near "
  286. "\"%s\", or out of range (0-%ju).\n",
  287. cb->ext_name, entry->name, arg, lmax);
  288. if (*end != '\0' && *end != sep)
  289. xt_params->exit_err(PARAMETER_PROBLEM,
  290. "%s: Argument to \"--%s\" has "
  291. "unexpected characters near \"%s\".\n",
  292. cb->ext_name, entry->name, end);
  293. }
  294. xtopt_mint_value_to_cb(cb, value);
  295. ++cb->nvals;
  296. xtopt_mint_value_to_ptr(cb, &put, value);
  297. if (*end == '\0')
  298. break;
  299. }
  300. }
  301. static void xtopt_parse_string(struct xt_option_call *cb)
  302. {
  303. const struct xt_option_entry *entry = cb->entry;
  304. size_t z = strlen(cb->arg);
  305. char *p;
  306. if (entry->min != 0 && z < entry->min)
  307. xt_params->exit_err(PARAMETER_PROBLEM,
  308. "Argument must have a minimum length of "
  309. "%u characters\n", entry->min);
  310. if (entry->max != 0 && z > entry->max)
  311. xt_params->exit_err(PARAMETER_PROBLEM,
  312. "Argument must have a maximum length of "
  313. "%u characters\n", entry->max);
  314. if (!(entry->flags & XTOPT_PUT))
  315. return;
  316. if (z >= entry->size)
  317. z = entry->size - 1;
  318. p = XTOPT_MKPTR(cb);
  319. strncpy(p, cb->arg, z);
  320. p[z] = '\0';
  321. }
  322. static const struct tos_symbol_info {
  323. unsigned char value;
  324. const char *name;
  325. } tos_symbol_names[] = {
  326. {IPTOS_LOWDELAY, "Minimize-Delay"},
  327. {IPTOS_THROUGHPUT, "Maximize-Throughput"},
  328. {IPTOS_RELIABILITY, "Maximize-Reliability"},
  329. {IPTOS_MINCOST, "Minimize-Cost"},
  330. {IPTOS_NORMALSVC, "Normal-Service"},
  331. {},
  332. };
  333. /*
  334. * tos_parse_numeric - parse a string like "15/255"
  335. *
  336. * @str: input string
  337. * @tvm: (value/mask) tuple
  338. * @max: maximum allowed value (must be pow(2,some_int)-1)
  339. */
  340. static bool tos_parse_numeric(const char *str, struct xt_option_call *cb,
  341. unsigned int max)
  342. {
  343. unsigned int value;
  344. char *end;
  345. xtables_strtoui(str, &end, &value, 0, max);
  346. cb->val.tos_value = value;
  347. cb->val.tos_mask = max;
  348. if (*end == '/') {
  349. const char *p = end + 1;
  350. if (!xtables_strtoui(p, &end, &value, 0, max))
  351. xtables_error(PARAMETER_PROBLEM, "Illegal value: \"%s\"",
  352. str);
  353. cb->val.tos_mask = value;
  354. }
  355. if (*end != '\0')
  356. xtables_error(PARAMETER_PROBLEM, "Illegal value: \"%s\"", str);
  357. return true;
  358. }
  359. /**
  360. * @str: input string
  361. * @tvm: (value/mask) tuple
  362. * @def_mask: mask to force when a symbolic name is used
  363. */
  364. static void xtopt_parse_tosmask(struct xt_option_call *cb)
  365. {
  366. const struct tos_symbol_info *symbol;
  367. char *tmp;
  368. if (xtables_strtoui(cb->arg, &tmp, NULL, 0, UINT8_MAX)) {
  369. tos_parse_numeric(cb->arg, cb, UINT8_MAX);
  370. return;
  371. }
  372. /*
  373. * This is our way we deal with different defaults
  374. * for different revisions.
  375. */
  376. cb->val.tos_mask = cb->entry->max;
  377. for (symbol = tos_symbol_names; symbol->name != NULL; ++symbol)
  378. if (strcasecmp(cb->arg, symbol->name) == 0) {
  379. cb->val.tos_value = symbol->value;
  380. return;
  381. }
  382. xtables_error(PARAMETER_PROBLEM, "Symbolic name \"%s\" is unknown",
  383. cb->arg);
  384. }
  385. /**
  386. * Validate the input for being conformant to "mark[/mask]".
  387. */
  388. static void xtopt_parse_markmask(struct xt_option_call *cb)
  389. {
  390. unsigned int mark = 0, mask = ~0U;
  391. char *end;
  392. if (!xtables_strtoui(cb->arg, &end, &mark, 0, UINT32_MAX))
  393. xt_params->exit_err(PARAMETER_PROBLEM,
  394. "%s: bad mark value for option \"--%s\", "
  395. "or out of range.\n",
  396. cb->ext_name, cb->entry->name);
  397. if (*end == '/' &&
  398. !xtables_strtoui(end + 1, &end, &mask, 0, UINT32_MAX))
  399. xt_params->exit_err(PARAMETER_PROBLEM,
  400. "%s: bad mask value for option \"--%s\", "
  401. "or out of range.\n",
  402. cb->ext_name, cb->entry->name);
  403. if (*end != '\0')
  404. xt_params->exit_err(PARAMETER_PROBLEM,
  405. "%s: trailing garbage after value "
  406. "for option \"--%s\".\n",
  407. cb->ext_name, cb->entry->name);
  408. cb->val.mark = mark;
  409. cb->val.mask = mask;
  410. }
  411. static int xtopt_sysloglvl_compare(const void *a, const void *b)
  412. {
  413. const char *name = a;
  414. const struct syslog_level *entry = b;
  415. return strcmp(name, entry->name);
  416. }
  417. static void xtopt_parse_sysloglevel(struct xt_option_call *cb)
  418. {
  419. static const struct syslog_level log_names[] = { /* must be sorted */
  420. {"alert", LOG_ALERT},
  421. {"crit", LOG_CRIT},
  422. {"debug", LOG_DEBUG},
  423. {"emerg", LOG_EMERG},
  424. {"error", LOG_ERR}, /* deprecated */
  425. {"info", LOG_INFO},
  426. {"notice", LOG_NOTICE},
  427. {"panic", LOG_EMERG}, /* deprecated */
  428. {"warning", LOG_WARNING},
  429. };
  430. const struct syslog_level *e;
  431. unsigned int num = 0;
  432. if (!xtables_strtoui(cb->arg, NULL, &num, 0, 7)) {
  433. e = bsearch(cb->arg, log_names, ARRAY_SIZE(log_names),
  434. sizeof(*log_names), xtopt_sysloglvl_compare);
  435. if (e == NULL)
  436. xt_params->exit_err(PARAMETER_PROBLEM,
  437. "log level \"%s\" unknown\n", cb->arg);
  438. num = e->level;
  439. }
  440. cb->val.syslog_level = num;
  441. if (cb->entry->flags & XTOPT_PUT)
  442. *(uint8_t *)XTOPT_MKPTR(cb) = num;
  443. }
  444. static void *xtables_sa_host(const void *sa, unsigned int afproto)
  445. {
  446. if (afproto == AF_INET6)
  447. return &((struct sockaddr_in6 *)sa)->sin6_addr;
  448. else if (afproto == AF_INET)
  449. return &((struct sockaddr_in *)sa)->sin_addr;
  450. return (void *)sa;
  451. }
  452. static socklen_t xtables_sa_hostlen(unsigned int afproto)
  453. {
  454. if (afproto == AF_INET6)
  455. return sizeof(struct in6_addr);
  456. else if (afproto == AF_INET)
  457. return sizeof(struct in_addr);
  458. return 0;
  459. }
  460. /**
  461. * Accepts: a hostname (DNS), or a single inetaddr - without any mask. The
  462. * result is stored in @cb->val.haddr. Additionally, @cb->val.hmask and
  463. * @cb->val.hlen are set for completeness to the appropriate values.
  464. */
  465. static void xtopt_parse_host(struct xt_option_call *cb)
  466. {
  467. struct addrinfo hints = {.ai_family = afinfo->family};
  468. unsigned int adcount = 0;
  469. struct addrinfo *res, *p;
  470. int ret;
  471. ret = getaddrinfo(cb->arg, NULL, &hints, &res);
  472. if (ret < 0)
  473. xt_params->exit_err(PARAMETER_PROBLEM,
  474. "getaddrinfo: %s\n", gai_strerror(ret));
  475. memset(&cb->val.hmask, 0xFF, sizeof(cb->val.hmask));
  476. cb->val.hlen = (afinfo->family == NFPROTO_IPV4) ? 32 : 128;
  477. for (p = res; p != NULL; p = p->ai_next) {
  478. if (adcount == 0) {
  479. memset(&cb->val.haddr, 0, sizeof(cb->val.haddr));
  480. memcpy(&cb->val.haddr,
  481. xtables_sa_host(p->ai_addr, p->ai_family),
  482. xtables_sa_hostlen(p->ai_family));
  483. ++adcount;
  484. continue;
  485. }
  486. if (memcmp(&cb->val.haddr,
  487. xtables_sa_host(p->ai_addr, p->ai_family),
  488. xtables_sa_hostlen(p->ai_family)) != 0)
  489. xt_params->exit_err(PARAMETER_PROBLEM,
  490. "%s resolves to more than one address\n",
  491. cb->arg);
  492. }
  493. freeaddrinfo(res);
  494. if (cb->entry->flags & XTOPT_PUT)
  495. /* Validation in xtables_option_metavalidate */
  496. memcpy(XTOPT_MKPTR(cb), &cb->val.haddr,
  497. sizeof(cb->val.haddr));
  498. }
  499. /**
  500. * @name: port name, or number as a string (e.g. "http" or "80")
  501. *
  502. * Resolve a port name to a number. Returns the port number in integral
  503. * form on success, or <0 on error. (errno will not be set.)
  504. */
  505. static int xtables_getportbyname(const char *name)
  506. {
  507. struct addrinfo *res = NULL, *p;
  508. int ret;
  509. ret = getaddrinfo(NULL, name, NULL, &res);
  510. if (ret < 0)
  511. return -1;
  512. ret = -1;
  513. for (p = res; p != NULL; p = p->ai_next) {
  514. if (p->ai_family == AF_INET6) {
  515. ret = ((struct sockaddr_in6 *)p->ai_addr)->sin6_port;
  516. break;
  517. } else if (p->ai_family == AF_INET) {
  518. ret = ((struct sockaddr_in *)p->ai_addr)->sin_port;
  519. break;
  520. }
  521. }
  522. freeaddrinfo(res);
  523. if (ret < 0)
  524. return ret;
  525. return ntohs(ret);
  526. }
  527. /**
  528. * Validate and parse a protocol specification (number or name) by use of
  529. * /etc/protocols and put the result into @cb->val.protocol.
  530. */
  531. static void xtopt_parse_protocol(struct xt_option_call *cb)
  532. {
  533. cb->val.protocol = xtables_parse_protocol(cb->arg);
  534. if (cb->entry->flags & XTOPT_PUT)
  535. *(uint8_t *)XTOPT_MKPTR(cb) = cb->val.protocol;
  536. }
  537. /**
  538. * Validate and parse a port specification and put the result into
  539. * @cb->val.port.
  540. */
  541. static void xtopt_parse_port(struct xt_option_call *cb)
  542. {
  543. const struct xt_option_entry *entry = cb->entry;
  544. int ret;
  545. ret = xtables_getportbyname(cb->arg);
  546. if (ret < 0)
  547. xt_params->exit_err(PARAMETER_PROBLEM,
  548. "Port \"%s\" does not resolve to anything.\n",
  549. cb->arg);
  550. if (entry->flags & XTOPT_NBO)
  551. ret = htons(ret);
  552. cb->val.port = ret;
  553. if (entry->flags & XTOPT_PUT)
  554. *(uint16_t *)XTOPT_MKPTR(cb) = cb->val.port;
  555. }
  556. static void xtopt_parse_mport(struct xt_option_call *cb)
  557. {
  558. static const size_t esize = sizeof(uint16_t);
  559. const struct xt_option_entry *entry = cb->entry;
  560. char *lo_arg, *wp_arg, *arg;
  561. unsigned int maxiter;
  562. int value;
  563. wp_arg = lo_arg = strdup(cb->arg);
  564. if (lo_arg == NULL)
  565. xt_params->exit_err(RESOURCE_PROBLEM, "strdup");
  566. maxiter = entry->size / esize;
  567. if (maxiter == 0)
  568. maxiter = 2; /* ARRAY_SIZE(cb->val.port_range) */
  569. if (entry->size % esize != 0)
  570. xt_params->exit_err(OTHER_PROBLEM, "%s: memory block does "
  571. "not have proper size\n", __func__);
  572. cb->val.port_range[0] = 0;
  573. cb->val.port_range[1] = UINT16_MAX;
  574. cb->nvals = 0;
  575. while ((arg = strsep(&wp_arg, ":")) != NULL) {
  576. if (cb->nvals == maxiter)
  577. xt_params->exit_err(PARAMETER_PROBLEM, "%s: Too many "
  578. "components for option \"--%s\" (max: %u)\n",
  579. cb->ext_name, entry->name, maxiter);
  580. if (*arg == '\0') {
  581. ++cb->nvals;
  582. continue;
  583. }
  584. value = xtables_getportbyname(arg);
  585. if (value < 0)
  586. xt_params->exit_err(PARAMETER_PROBLEM,
  587. "Port \"%s\" does not resolve to "
  588. "anything.\n", arg);
  589. if (entry->flags & XTOPT_NBO)
  590. value = htons(value);
  591. if (cb->nvals < ARRAY_SIZE(cb->val.port_range))
  592. cb->val.port_range[cb->nvals] = value;
  593. ++cb->nvals;
  594. }
  595. if (cb->nvals == 1) {
  596. cb->val.port_range[1] = cb->val.port_range[0];
  597. ++cb->nvals;
  598. }
  599. if (entry->flags & XTOPT_PUT)
  600. memcpy(XTOPT_MKPTR(cb), cb->val.port_range, sizeof(uint16_t) *
  601. (cb->nvals <= maxiter ? cb->nvals : maxiter));
  602. free(lo_arg);
  603. }
  604. /**
  605. * Parse an integer and ensure it is within the address family's prefix length
  606. * limits. The result is stored in @cb->val.hlen.
  607. */
  608. static void xtopt_parse_plen(struct xt_option_call *cb)
  609. {
  610. const struct xt_option_entry *entry = cb->entry;
  611. unsigned int prefix_len = 128; /* happiness is a warm gcc */
  612. cb->val.hlen = (afinfo->family == NFPROTO_IPV4) ? 32 : 128;
  613. if (!xtables_strtoui(cb->arg, NULL, &prefix_len, 0, cb->val.hlen))
  614. xt_params->exit_err(PARAMETER_PROBLEM,
  615. "%s: bad value for option \"--%s\", "
  616. "or out of range (%u-%u).\n",
  617. cb->ext_name, entry->name, 0, cb->val.hlen);
  618. cb->val.hlen = prefix_len;
  619. }
  620. /**
  621. * Reuse xtopt_parse_plen for testing the integer. Afterwards convert this to
  622. * a bitmask, and make it available through @cb->val.hmask (hlen remains
  623. * valid). If %XTOPT_PUT is used, hmask will be copied to the target area.
  624. */
  625. static void xtopt_parse_plenmask(struct xt_option_call *cb)
  626. {
  627. const struct xt_option_entry *entry = cb->entry;
  628. uint32_t *mask = cb->val.hmask.all;
  629. xtopt_parse_plen(cb);
  630. memset(mask, 0xFF, sizeof(union nf_inet_addr));
  631. /* This shifting is AF-independent. */
  632. if (cb->val.hlen == 0) {
  633. mask[0] = mask[1] = mask[2] = mask[3] = 0;
  634. } else if (cb->val.hlen <= 32) {
  635. mask[0] <<= 32 - cb->val.hlen;
  636. mask[1] = mask[2] = mask[3] = 0;
  637. } else if (cb->val.hlen <= 64) {
  638. mask[1] <<= 32 - (cb->val.hlen - 32);
  639. mask[2] = mask[3] = 0;
  640. } else if (cb->val.hlen <= 96) {
  641. mask[2] <<= 32 - (cb->val.hlen - 64);
  642. mask[3] = 0;
  643. } else if (cb->val.hlen <= 128) {
  644. mask[3] <<= 32 - (cb->val.hlen - 96);
  645. }
  646. mask[0] = htonl(mask[0]);
  647. mask[1] = htonl(mask[1]);
  648. mask[2] = htonl(mask[2]);
  649. mask[3] = htonl(mask[3]);
  650. if (entry->flags & XTOPT_PUT)
  651. memcpy(XTOPT_MKPTR(cb), mask, sizeof(union nf_inet_addr));
  652. }
  653. static void xtopt_parse_hostmask(struct xt_option_call *cb)
  654. {
  655. const char *orig_arg = cb->arg;
  656. char *work, *p;
  657. if (strchr(cb->arg, '/') == NULL) {
  658. xtopt_parse_host(cb);
  659. return;
  660. }
  661. work = strdup(orig_arg);
  662. if (work == NULL)
  663. xt_params->exit_err(PARAMETER_PROBLEM, "strdup");
  664. p = strchr(work, '/'); /* by def this can't be NULL now */
  665. *p++ = '\0';
  666. /*
  667. * Because xtopt_parse_host and xtopt_parse_plenmask would store
  668. * different things in the same target area, XTTYPE_HOSTMASK must
  669. * disallow XTOPT_PUT, which it does by forcing its absence,
  670. * cf. not being listed in xtopt_psize.
  671. */
  672. cb->arg = work;
  673. xtopt_parse_host(cb);
  674. cb->arg = p;
  675. xtopt_parse_plenmask(cb);
  676. cb->arg = orig_arg;
  677. }
  678. static void xtopt_parse_ethermac(struct xt_option_call *cb)
  679. {
  680. const char *arg = cb->arg;
  681. unsigned int i;
  682. char *end;
  683. for (i = 0; i < ARRAY_SIZE(cb->val.ethermac) - 1; ++i) {
  684. cb->val.ethermac[i] = strtoul(arg, &end, 16);
  685. if (*end != ':' || end - arg > 2)
  686. goto out;
  687. arg = end + 1;
  688. }
  689. i = ARRAY_SIZE(cb->val.ethermac) - 1;
  690. cb->val.ethermac[i] = strtoul(arg, &end, 16);
  691. if (*end != '\0' || end - arg > 2)
  692. goto out;
  693. if (cb->entry->flags & XTOPT_PUT)
  694. memcpy(XTOPT_MKPTR(cb), cb->val.ethermac,
  695. sizeof(cb->val.ethermac));
  696. return;
  697. out:
  698. xt_params->exit_err(PARAMETER_PROBLEM, "ether");
  699. }
  700. static void (*const xtopt_subparse[])(struct xt_option_call *) = {
  701. [XTTYPE_UINT8] = xtopt_parse_int,
  702. [XTTYPE_UINT16] = xtopt_parse_int,
  703. [XTTYPE_UINT32] = xtopt_parse_int,
  704. [XTTYPE_UINT64] = xtopt_parse_int,
  705. [XTTYPE_UINT8RC] = xtopt_parse_mint,
  706. [XTTYPE_UINT16RC] = xtopt_parse_mint,
  707. [XTTYPE_UINT32RC] = xtopt_parse_mint,
  708. [XTTYPE_UINT64RC] = xtopt_parse_mint,
  709. [XTTYPE_DOUBLE] = xtopt_parse_float,
  710. [XTTYPE_STRING] = xtopt_parse_string,
  711. [XTTYPE_TOSMASK] = xtopt_parse_tosmask,
  712. [XTTYPE_MARKMASK32] = xtopt_parse_markmask,
  713. [XTTYPE_SYSLOGLEVEL] = xtopt_parse_sysloglevel,
  714. [XTTYPE_HOST] = xtopt_parse_host,
  715. [XTTYPE_HOSTMASK] = xtopt_parse_hostmask,
  716. [XTTYPE_PROTOCOL] = xtopt_parse_protocol,
  717. [XTTYPE_PORT] = xtopt_parse_port,
  718. [XTTYPE_PORTRC] = xtopt_parse_mport,
  719. [XTTYPE_PLEN] = xtopt_parse_plen,
  720. [XTTYPE_PLENMASK] = xtopt_parse_plenmask,
  721. [XTTYPE_ETHERMAC] = xtopt_parse_ethermac,
  722. };
  723. /**
  724. * The master option parsing routine. May be used for the ".x6_parse"
  725. * function pointer in extensions if fully automatic parsing is desired.
  726. * It may be also called manually from a custom x6_parse function.
  727. */
  728. void xtables_option_parse(struct xt_option_call *cb)
  729. {
  730. const struct xt_option_entry *entry = cb->entry;
  731. unsigned int eflag = 1 << cb->entry->id;
  732. /*
  733. * With {.id = P_FOO, .excl = P_FOO} we can have simple double-use
  734. * prevention. Though it turned out that this is too much typing (most
  735. * of the options are one-time use only), so now we also have
  736. * %XTOPT_MULTI.
  737. */
  738. if ((!(entry->flags & XTOPT_MULTI) || (entry->excl & eflag)) &&
  739. cb->xflags & eflag)
  740. xt_params->exit_err(PARAMETER_PROBLEM,
  741. "%s: option \"--%s\" can only be used once.\n",
  742. cb->ext_name, cb->entry->name);
  743. if (cb->invert && !(entry->flags & XTOPT_INVERT))
  744. xt_params->exit_err(PARAMETER_PROBLEM,
  745. "%s: option \"--%s\" cannot be inverted.\n",
  746. cb->ext_name, entry->name);
  747. if (entry->type != XTTYPE_NONE && optarg == NULL)
  748. xt_params->exit_err(PARAMETER_PROBLEM,
  749. "%s: option \"--%s\" requires an argument.\n",
  750. cb->ext_name, entry->name);
  751. /*
  752. * Fill in fallback value for "nvals", in case an extension (as it
  753. * happened with libxt_conntrack.2) tries to read it, despite not using
  754. * a *RC option type.
  755. */
  756. cb->nvals = 1;
  757. if (entry->type <= ARRAY_SIZE(xtopt_subparse) &&
  758. xtopt_subparse[entry->type] != NULL)
  759. xtopt_subparse[entry->type](cb);
  760. /* Exclusion with other flags tested later in finalize. */
  761. cb->xflags |= 1 << entry->id;
  762. }
  763. /**
  764. * Verifies that an extension's option map descriptor is valid, and ought to
  765. * be called right after the extension has been loaded, and before option
  766. * merging/xfrm.
  767. */
  768. void xtables_option_metavalidate(const char *name,
  769. const struct xt_option_entry *entry)
  770. {
  771. for (; entry->name != NULL; ++entry) {
  772. if (entry->id >= CHAR_BIT * sizeof(unsigned int) ||
  773. entry->id >= XT_OPTION_OFFSET_SCALE)
  774. xt_params->exit_err(OTHER_PROBLEM,
  775. "Extension %s uses invalid ID %u\n",
  776. name, entry->id);
  777. if (!(entry->flags & XTOPT_PUT)) {
  778. if (entry->ptroff != 0)
  779. xt_params->exit_err(OTHER_PROBLEM,
  780. "%s: ptroff for \"--%s\" is non-"
  781. "zero but no XTOPT_PUT is specified. "
  782. "Oversight?", name, entry->name);
  783. continue;
  784. }
  785. if (entry->type >= ARRAY_SIZE(xtopt_psize) ||
  786. xtopt_psize[entry->type] == 0)
  787. xt_params->exit_err(OTHER_PROBLEM,
  788. "%s: entry type of option \"--%s\" cannot be "
  789. "combined with XTOPT_PUT\n",
  790. name, entry->name);
  791. if (xtopt_psize[entry->type] != -1 &&
  792. xtopt_psize[entry->type] != entry->size)
  793. xt_params->exit_err(OTHER_PROBLEM,
  794. "%s: option \"--%s\" points to a memory block "
  795. "of wrong size (expected %zu, got %zu)\n",
  796. name, entry->name,
  797. xtopt_psize[entry->type], entry->size);
  798. }
  799. }
  800. /**
  801. * Find an option entry by its id.
  802. */
  803. static const struct xt_option_entry *
  804. xtables_option_lookup(const struct xt_option_entry *entry, unsigned int id)
  805. {
  806. for (; entry->name != NULL; ++entry)
  807. if (entry->id == id)
  808. return entry;
  809. return NULL;
  810. }
  811. /**
  812. * @c: getopt id (i.e. with offset)
  813. * @fw: struct ipt_entry or ip6t_entry
  814. *
  815. * Dispatch arguments to the appropriate parse function, based upon the
  816. * extension's choice of API.
  817. */
  818. void xtables_option_tpcall(unsigned int c, char **argv, bool invert,
  819. struct xtables_target *t, void *fw)
  820. {
  821. struct xt_option_call cb;
  822. if (t->x6_parse == NULL) {
  823. if (t->parse != NULL)
  824. t->parse(c - t->option_offset, argv, invert,
  825. &t->tflags, fw, &t->t);
  826. return;
  827. }
  828. c -= t->option_offset;
  829. cb.entry = xtables_option_lookup(t->x6_options, c);
  830. if (cb.entry == NULL)
  831. xtables_error(OTHER_PROBLEM,
  832. "Extension does not know id %u\n", c);
  833. cb.arg = optarg;
  834. cb.invert = invert;
  835. cb.ext_name = t->name;
  836. cb.data = t->t->data;
  837. cb.xflags = t->tflags;
  838. cb.target = &t->t;
  839. cb.xt_entry = fw;
  840. cb.udata = t->udata;
  841. t->x6_parse(&cb);
  842. t->tflags = cb.xflags;
  843. }
  844. /**
  845. * @c: getopt id (i.e. with offset)
  846. * @fw: struct ipt_entry or ip6t_entry
  847. *
  848. * Dispatch arguments to the appropriate parse function, based upon the
  849. * extension's choice of API.
  850. */
  851. void xtables_option_mpcall(unsigned int c, char **argv, bool invert,
  852. struct xtables_match *m, void *fw)
  853. {
  854. struct xt_option_call cb;
  855. if (m->x6_parse == NULL) {
  856. if (m->parse != NULL)
  857. m->parse(c - m->option_offset, argv, invert,
  858. &m->mflags, fw, &m->m);
  859. return;
  860. }
  861. c -= m->option_offset;
  862. cb.entry = xtables_option_lookup(m->x6_options, c);
  863. if (cb.entry == NULL)
  864. xtables_error(OTHER_PROBLEM,
  865. "Extension does not know id %u\n", c);
  866. cb.arg = optarg;
  867. cb.invert = invert;
  868. cb.ext_name = m->name;
  869. cb.data = m->m->data;
  870. cb.xflags = m->mflags;
  871. cb.match = &m->m;
  872. cb.xt_entry = fw;
  873. cb.udata = m->udata;
  874. m->x6_parse(&cb);
  875. m->mflags = cb.xflags;
  876. }
  877. /**
  878. * @name: name of extension
  879. * @entry: current option (from all ext's entries) being validated
  880. * @xflags: flags the extension has collected
  881. * @i: conflicting option (id) to test for
  882. */
  883. static void
  884. xtables_option_fcheck2(const char *name, const struct xt_option_entry *entry,
  885. const struct xt_option_entry *other,
  886. unsigned int xflags)
  887. {
  888. unsigned int ef = 1 << entry->id, of = 1 << other->id;
  889. if (entry->also & of && !(xflags & of))
  890. xt_params->exit_err(PARAMETER_PROBLEM,
  891. "%s: option \"--%s\" also requires \"--%s\".\n",
  892. name, entry->name, other->name);
  893. if (!(entry->excl & of))
  894. /* Use of entry does not collide with other option, good. */
  895. return;
  896. if ((xflags & (ef | of)) != (ef | of))
  897. /* Conflicting options were not used. */
  898. return;
  899. xt_params->exit_err(PARAMETER_PROBLEM,
  900. "%s: option \"--%s\" cannot be used together with \"--%s\".\n",
  901. name, entry->name, other->name);
  902. }
  903. /**
  904. * @name: name of extension
  905. * @xflags: accumulated flags
  906. * @entry: extension's option table
  907. *
  908. * Check that all option constraints have been met. This effectively replaces
  909. * ->final_check of the older API.
  910. */
  911. void xtables_options_fcheck(const char *name, unsigned int xflags,
  912. const struct xt_option_entry *table)
  913. {
  914. const struct xt_option_entry *entry, *other;
  915. unsigned int i;
  916. for (entry = table; entry->name != NULL; ++entry) {
  917. if (entry->flags & XTOPT_MAND &&
  918. !(xflags & (1 << entry->id)))
  919. xt_params->exit_err(PARAMETER_PROBLEM,
  920. "%s: option \"--%s\" must be specified\n",
  921. name, entry->name);
  922. if (!(xflags & (1 << entry->id)))
  923. /* Not required, not specified, thus skip. */
  924. continue;
  925. for (i = 0; i < CHAR_BIT * sizeof(entry->id); ++i) {
  926. if (entry->id == i)
  927. /*
  928. * Avoid conflict with self. Multi-use check
  929. * was done earlier in xtables_option_parse.
  930. */
  931. continue;
  932. other = xtables_option_lookup(table, i);
  933. if (other == NULL)
  934. continue;
  935. xtables_option_fcheck2(name, entry, other, xflags);
  936. }
  937. }
  938. }
  939. /**
  940. * Dispatch arguments to the appropriate final_check function, based upon the
  941. * extension's choice of API.
  942. */
  943. void xtables_option_tfcall(struct xtables_target *t)
  944. {
  945. if (t->x6_fcheck != NULL) {
  946. struct xt_fcheck_call cb;
  947. cb.ext_name = t->name;
  948. cb.data = t->t->data;
  949. cb.xflags = t->tflags;
  950. cb.udata = t->udata;
  951. t->x6_fcheck(&cb);
  952. } else if (t->final_check != NULL) {
  953. t->final_check(t->tflags);
  954. }
  955. if (t->x6_options != NULL)
  956. xtables_options_fcheck(t->name, t->tflags, t->x6_options);
  957. }
  958. /**
  959. * Dispatch arguments to the appropriate final_check function, based upon the
  960. * extension's choice of API.
  961. */
  962. void xtables_option_mfcall(struct xtables_match *m)
  963. {
  964. if (m->x6_fcheck != NULL) {
  965. struct xt_fcheck_call cb;
  966. cb.ext_name = m->name;
  967. cb.data = m->m->data;
  968. cb.xflags = m->mflags;
  969. cb.udata = m->udata;
  970. m->x6_fcheck(&cb);
  971. } else if (m->final_check != NULL) {
  972. m->final_check(m->mflags);
  973. }
  974. if (m->x6_options != NULL)
  975. xtables_options_fcheck(m->name, m->mflags, m->x6_options);
  976. }
  977. struct xtables_lmap *xtables_lmap_init(const char *file)
  978. {
  979. struct xtables_lmap *lmap_head = NULL, *lmap_prev = NULL, *lmap_this;
  980. char buf[512];
  981. FILE *fp;
  982. char *cur, *nxt;
  983. int id;
  984. fp = fopen(file, "re");
  985. if (fp == NULL)
  986. return NULL;
  987. while (fgets(buf, sizeof(buf), fp) != NULL) {
  988. cur = buf;
  989. while (isspace(*cur))
  990. ++cur;
  991. if (*cur == '#' || *cur == '\n' || *cur == '\0')
  992. continue;
  993. /* iproute2 allows hex and dec format */
  994. errno = 0;
  995. id = strtoul(cur, &nxt, strncmp(cur, "0x", 2) == 0 ? 16 : 10);
  996. if (nxt == cur || errno != 0)
  997. continue;
  998. /* same boundaries as in iproute2 */
  999. if (id < 0 || id > 255)
  1000. continue;
  1001. cur = nxt;
  1002. if (!isspace(*cur))
  1003. continue;
  1004. while (isspace(*cur))
  1005. ++cur;
  1006. if (*cur == '#' || *cur == '\n' || *cur == '\0')
  1007. continue;
  1008. nxt = cur;
  1009. while (*nxt != '\0' && !isspace(*nxt))
  1010. ++nxt;
  1011. if (nxt == cur)
  1012. continue;
  1013. *nxt = '\0';
  1014. /* found valid data */
  1015. lmap_this = malloc(sizeof(*lmap_this));
  1016. if (lmap_this == NULL) {
  1017. perror("malloc");
  1018. goto out;
  1019. }
  1020. lmap_this->id = id;
  1021. lmap_this->name = strdup(cur);
  1022. if (lmap_this->name == NULL) {
  1023. free(lmap_this);
  1024. goto out;
  1025. }
  1026. lmap_this->next = NULL;
  1027. if (lmap_prev != NULL)
  1028. lmap_prev->next = lmap_this;
  1029. else
  1030. lmap_head = lmap_this;
  1031. lmap_prev = lmap_this;
  1032. }
  1033. fclose(fp);
  1034. return lmap_head;
  1035. out:
  1036. fclose(fp);
  1037. xtables_lmap_free(lmap_head);
  1038. return NULL;
  1039. }
  1040. void xtables_lmap_free(struct xtables_lmap *head)
  1041. {
  1042. struct xtables_lmap *next;
  1043. for (; head != NULL; head = next) {
  1044. next = head->next;
  1045. free(head->name);
  1046. free(head);
  1047. }
  1048. }
  1049. int xtables_lmap_name2id(const struct xtables_lmap *head, const char *name)
  1050. {
  1051. for (; head != NULL; head = head->next)
  1052. if (strcmp(head->name, name) == 0)
  1053. return head->id;
  1054. return -1;
  1055. }
  1056. const char *xtables_lmap_id2name(const struct xtables_lmap *head, int id)
  1057. {
  1058. for (; head != NULL; head = head->next)
  1059. if (head->id == id)
  1060. return head->name;
  1061. return NULL;
  1062. }