2
0

options.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  1. /*
  2. * firewall3 - 3rd OpenWrt UCI firewall implementation
  3. *
  4. * Copyright (C) 2013-2014 Jo-Philipp Wich <jo@mein.io>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include "options.h"
  19. #include "ubus.h"
  20. static bool
  21. put_value(void *ptr, void *val, int elem_size, bool is_list)
  22. {
  23. void *copy;
  24. if (is_list)
  25. {
  26. copy = malloc(elem_size);
  27. if (!copy)
  28. return false;
  29. memcpy(copy, val, elem_size);
  30. list_add_tail((struct list_head *)copy, (struct list_head *)ptr);
  31. return true;
  32. }
  33. memcpy(ptr, val, elem_size);
  34. return false;
  35. }
  36. static bool
  37. parse_enum(void *ptr, const char *val, const char **values, int min, int max)
  38. {
  39. int i, l = strlen(val);
  40. if (l > 0)
  41. {
  42. for (i = 0; i <= (max - min); i++)
  43. {
  44. if (!strncasecmp(val, values[i], l))
  45. {
  46. *((int *)ptr) = min + i;
  47. return true;
  48. }
  49. }
  50. }
  51. return false;
  52. }
  53. const char *fw3_flag_names[__FW3_FLAG_MAX] = {
  54. "filter",
  55. "nat",
  56. "mangle",
  57. "raw",
  58. "IPv4",
  59. "IPv6",
  60. "ACCEPT",
  61. "REJECT",
  62. "DROP",
  63. "NOTRACK",
  64. "HELPER",
  65. "MARK",
  66. "DSCP",
  67. "DNAT",
  68. "SNAT",
  69. "MASQUERADE",
  70. "ACCEPT",
  71. "REJECT",
  72. "DROP",
  73. };
  74. const char *fw3_reject_code_names[__FW3_REJECT_CODE_MAX] = {
  75. "tcp-reset",
  76. "port-unreach",
  77. "adm-prohibited",
  78. };
  79. const char *fw3_limit_units[__FW3_LIMIT_UNIT_MAX] = {
  80. "second",
  81. "minute",
  82. "hour",
  83. "day",
  84. };
  85. const char *fw3_ipset_method_names[__FW3_IPSET_METHOD_MAX] = {
  86. "(bug)",
  87. "bitmap",
  88. "hash",
  89. "list",
  90. };
  91. const char *fw3_ipset_type_names[__FW3_IPSET_TYPE_MAX] = {
  92. "(bug)",
  93. "ip",
  94. "port",
  95. "mac",
  96. "net",
  97. "set",
  98. };
  99. static const char *weekdays[] = {
  100. "monday",
  101. "tuesday",
  102. "wednesday",
  103. "thursday",
  104. "friday",
  105. "saturday",
  106. "sunday",
  107. };
  108. static const char *include_types[] = {
  109. "script",
  110. "restore",
  111. };
  112. static const char *reflection_sources[] = {
  113. "internal",
  114. "external",
  115. };
  116. static const struct { const char *name; uint8_t dscp; } dscp_classes[] = {
  117. { "CS0", 0x00 },
  118. { "CS1", 0x08 },
  119. { "CS2", 0x10 },
  120. { "CS3", 0x18 },
  121. { "CS4", 0x20 },
  122. { "CS5", 0x28 },
  123. { "CS6", 0x30 },
  124. { "CS7", 0x38 },
  125. { "BE", 0x00 },
  126. { "AF11", 0x0a },
  127. { "AF12", 0x0c },
  128. { "AF13", 0x0e },
  129. { "AF21", 0x12 },
  130. { "AF22", 0x14 },
  131. { "AF23", 0x16 },
  132. { "AF31", 0x1a },
  133. { "AF32", 0x1c },
  134. { "AF33", 0x1e },
  135. { "AF41", 0x22 },
  136. { "AF42", 0x24 },
  137. { "AF43", 0x26 },
  138. { "EF", 0x2e }
  139. };
  140. bool
  141. fw3_parse_bool(void *ptr, const char *val, bool is_list)
  142. {
  143. if (!strcmp(val, "true") || !strcmp(val, "yes") || !strcmp(val, "1"))
  144. *((bool *)ptr) = true;
  145. else
  146. *((bool *)ptr) = false;
  147. return true;
  148. }
  149. bool
  150. fw3_parse_int(void *ptr, const char *val, bool is_list)
  151. {
  152. char *e;
  153. int n = strtol(val, &e, 0);
  154. if (e == val || *e)
  155. return false;
  156. *((int *)ptr) = n;
  157. return true;
  158. }
  159. bool
  160. fw3_parse_string(void *ptr, const char *val, bool is_list)
  161. {
  162. *((char **)ptr) = (char *)val;
  163. return true;
  164. }
  165. bool
  166. fw3_parse_target(void *ptr, const char *val, bool is_list)
  167. {
  168. return parse_enum(ptr, val, &fw3_flag_names[FW3_FLAG_ACCEPT],
  169. FW3_FLAG_ACCEPT, FW3_FLAG_MASQUERADE);
  170. }
  171. bool
  172. fw3_parse_reject_code(void *ptr, const char *val, bool is_list)
  173. {
  174. return parse_enum(ptr, val, &fw3_reject_code_names[FW3_REJECT_CODE_TCP_RESET],
  175. FW3_REJECT_CODE_TCP_RESET, FW3_REJECT_CODE_ADM_PROHIBITED);
  176. }
  177. bool
  178. fw3_parse_limit(void *ptr, const char *val, bool is_list)
  179. {
  180. struct fw3_limit *limit = ptr;
  181. enum fw3_limit_unit u = FW3_LIMIT_UNIT_SECOND;
  182. char *e;
  183. int n;
  184. if (*val == '!')
  185. {
  186. limit->invert = true;
  187. while (isspace(*++val));
  188. }
  189. n = strtol(val, &e, 10);
  190. if (errno == ERANGE || errno == EINVAL)
  191. return false;
  192. if (*e && *e++ != '/')
  193. return false;
  194. if (!strlen(e))
  195. return false;
  196. if (!parse_enum(&u, e, fw3_limit_units, 0, FW3_LIMIT_UNIT_DAY))
  197. return false;
  198. limit->rate = n;
  199. limit->unit = u;
  200. return true;
  201. }
  202. bool
  203. fw3_parse_device(void *ptr, const char *val, bool is_list)
  204. {
  205. char *p;
  206. struct fw3_device dev = { };
  207. if (*val == '*')
  208. {
  209. dev.set = true;
  210. dev.any = true;
  211. put_value(ptr, &dev, sizeof(dev), is_list);
  212. return true;
  213. }
  214. if (*val == '!')
  215. {
  216. dev.invert = true;
  217. while (isspace(*++val));
  218. }
  219. if ((p = strchr(val, '@')) != NULL)
  220. {
  221. *p++ = 0;
  222. snprintf(dev.network, sizeof(dev.network), "%s", p);
  223. }
  224. if (*val)
  225. snprintf(dev.name, sizeof(dev.name), "%s", val);
  226. else
  227. return false;
  228. dev.set = true;
  229. put_value(ptr, &dev, sizeof(dev), is_list);
  230. return true;
  231. }
  232. bool
  233. fw3_parse_address(void *ptr, const char *val, bool is_list)
  234. {
  235. struct fw3_address addr = { };
  236. struct in_addr v4;
  237. struct in6_addr v6;
  238. char *p = NULL, *m = NULL, *s, *e;
  239. int bits = -1;
  240. if (*val == '!')
  241. {
  242. addr.invert = true;
  243. while (isspace(*++val));
  244. }
  245. s = strdup(val);
  246. if (!s)
  247. return false;
  248. if ((m = strchr(s, '/')) != NULL)
  249. *m++ = 0;
  250. else if ((p = strchr(s, '-')) != NULL)
  251. *p++ = 0;
  252. if (inet_pton(AF_INET6, s, &v6))
  253. {
  254. addr.family = FW3_FAMILY_V6;
  255. addr.address.v6 = v6;
  256. if (m)
  257. {
  258. if (!inet_pton(AF_INET6, m, &v6))
  259. {
  260. bits = strtol(m, &e, 10);
  261. if ((*e != 0) || !fw3_bitlen2netmask(addr.family, bits, &v6))
  262. goto fail;
  263. }
  264. addr.mask.v6 = v6;
  265. }
  266. else if (p)
  267. {
  268. if (!inet_pton(AF_INET6, p, &addr.mask.v6))
  269. goto fail;
  270. addr.range = true;
  271. }
  272. else
  273. {
  274. memset(addr.mask.v6.s6_addr, 0xFF, 16);
  275. }
  276. }
  277. else if (inet_pton(AF_INET, s, &v4))
  278. {
  279. addr.family = FW3_FAMILY_V4;
  280. addr.address.v4 = v4;
  281. if (m)
  282. {
  283. if (!inet_pton(AF_INET, m, &v4))
  284. {
  285. bits = strtol(m, &e, 10);
  286. if ((*e != 0) || !fw3_bitlen2netmask(addr.family, bits, &v4))
  287. goto fail;
  288. }
  289. addr.mask.v4 = v4;
  290. }
  291. else if (p)
  292. {
  293. if (!inet_pton(AF_INET, p, &addr.mask.v4))
  294. goto fail;
  295. addr.range = true;
  296. }
  297. else
  298. {
  299. addr.mask.v4.s_addr = 0xFFFFFFFF;
  300. }
  301. }
  302. else
  303. {
  304. goto fail;
  305. }
  306. free(s);
  307. addr.set = true;
  308. put_value(ptr, &addr, sizeof(addr), is_list);
  309. return true;
  310. fail:
  311. free(s);
  312. return false;
  313. }
  314. bool
  315. fw3_parse_network(void *ptr, const char *val, bool is_list)
  316. {
  317. struct fw3_device dev = { };
  318. struct fw3_address *addr, *tmp;
  319. LIST_HEAD(addr_list);
  320. int n_addrs;
  321. if (!fw3_parse_address(ptr, val, is_list))
  322. {
  323. if (!fw3_parse_device(&dev, val, false))
  324. return false;
  325. n_addrs = fw3_ubus_address(&addr_list, dev.name);
  326. list_for_each_entry(addr, &addr_list, list)
  327. {
  328. addr->invert = dev.invert;
  329. addr->resolved = true;
  330. }
  331. /* add an empty address member with .set = false, .resolved = true
  332. * to signal resolving failure to callers */
  333. if (n_addrs == 0)
  334. {
  335. tmp = fw3_alloc(sizeof(*tmp));
  336. tmp->resolved = true;
  337. list_add_tail(&tmp->list, &addr_list);
  338. }
  339. if (is_list)
  340. {
  341. list_splice_tail(&addr_list, ptr);
  342. }
  343. else if (!list_empty(&addr_list))
  344. {
  345. memcpy(ptr, list_first_entry(&addr_list, typeof(*addr), list),
  346. sizeof(*addr));
  347. list_for_each_entry_safe(addr, tmp, &addr_list, list)
  348. free(addr);
  349. }
  350. }
  351. return true;
  352. }
  353. bool
  354. fw3_parse_mac(void *ptr, const char *val, bool is_list)
  355. {
  356. struct fw3_mac addr = { };
  357. struct ether_addr *mac;
  358. if (*val == '!')
  359. {
  360. addr.invert = true;
  361. while (isspace(*++val));
  362. }
  363. if ((mac = ether_aton(val)) != NULL)
  364. {
  365. addr.mac = *mac;
  366. addr.set = true;
  367. put_value(ptr, &addr, sizeof(addr), is_list);
  368. return true;
  369. }
  370. return false;
  371. }
  372. bool
  373. fw3_parse_port(void *ptr, const char *val, bool is_list)
  374. {
  375. struct fw3_port range = { };
  376. uint16_t n;
  377. uint16_t m;
  378. char *p;
  379. if (*val == '!')
  380. {
  381. range.invert = true;
  382. while (isspace(*++val));
  383. }
  384. n = strtoul(val, &p, 10);
  385. if (errno == ERANGE || errno == EINVAL)
  386. return false;
  387. if (*p && *p != '-' && *p != ':')
  388. return false;
  389. if (*p)
  390. {
  391. m = strtoul(++p, NULL, 10);
  392. if (errno == ERANGE || errno == EINVAL || m < n)
  393. return false;
  394. range.port_min = n;
  395. range.port_max = m;
  396. }
  397. else
  398. {
  399. range.port_min = n;
  400. range.port_max = n;
  401. }
  402. range.set = true;
  403. put_value(ptr, &range, sizeof(range), is_list);
  404. return true;
  405. }
  406. bool
  407. fw3_parse_family(void *ptr, const char *val, bool is_list)
  408. {
  409. if (!strcmp(val, "any") || !strcmp(val, "*"))
  410. *((enum fw3_family *)ptr) = FW3_FAMILY_ANY;
  411. else if (!strcmp(val, "inet") || strrchr(val, '4'))
  412. *((enum fw3_family *)ptr) = FW3_FAMILY_V4;
  413. else if (!strcmp(val, "inet6") || strrchr(val, '6'))
  414. *((enum fw3_family *)ptr) = FW3_FAMILY_V6;
  415. else
  416. return false;
  417. return true;
  418. }
  419. bool
  420. fw3_parse_icmptype(void *ptr, const char *val, bool is_list)
  421. {
  422. struct fw3_icmptype icmp = { };
  423. bool v4 = false;
  424. bool v6 = false;
  425. char *p;
  426. int i;
  427. for (i = 0; i < ARRAY_SIZE(fw3_icmptype_list_v4); i++)
  428. {
  429. if (!strcmp(val, fw3_icmptype_list_v4[i].name))
  430. {
  431. icmp.type = fw3_icmptype_list_v4[i].type;
  432. icmp.code_min = fw3_icmptype_list_v4[i].code_min;
  433. icmp.code_max = fw3_icmptype_list_v4[i].code_max;
  434. v4 = true;
  435. break;
  436. }
  437. }
  438. for (i = 0; i < ARRAY_SIZE(fw3_icmptype_list_v6); i++)
  439. {
  440. if (!strcmp(val, fw3_icmptype_list_v6[i].name))
  441. {
  442. icmp.type6 = fw3_icmptype_list_v6[i].type;
  443. icmp.code6_min = fw3_icmptype_list_v6[i].code_min;
  444. icmp.code6_max = fw3_icmptype_list_v6[i].code_max;
  445. v6 = true;
  446. break;
  447. }
  448. }
  449. if (!v4 && !v6)
  450. {
  451. i = strtoul(val, &p, 10);
  452. if ((p == val) || (*p != '/' && *p != 0) || (i > 0xFF))
  453. return false;
  454. icmp.type = i;
  455. if (*p == '/')
  456. {
  457. val = ++p;
  458. i = strtoul(val, &p, 10);
  459. if ((p == val) || (*p != 0) || (i > 0xFF))
  460. return false;
  461. icmp.code_min = i;
  462. icmp.code_max = i;
  463. }
  464. else
  465. {
  466. icmp.code_min = 0;
  467. icmp.code_max = 0xFF;
  468. }
  469. icmp.type6 = icmp.type;
  470. icmp.code6_min = icmp.code_max;
  471. icmp.code6_max = icmp.code_max;
  472. v4 = true;
  473. v6 = true;
  474. }
  475. icmp.family = (v4 && v6) ? FW3_FAMILY_ANY
  476. : (v6 ? FW3_FAMILY_V6 : FW3_FAMILY_V4);
  477. put_value(ptr, &icmp, sizeof(icmp), is_list);
  478. return true;
  479. }
  480. bool
  481. fw3_parse_protocol(void *ptr, const char *val, bool is_list)
  482. {
  483. struct fw3_protocol proto = { };
  484. struct protoent *ent;
  485. char *e;
  486. if (*val == '!')
  487. {
  488. proto.invert = true;
  489. while (isspace(*++val));
  490. }
  491. if (!strcmp(val, "all") || !strcmp(val, "any") || !strcmp(val, "*"))
  492. {
  493. proto.any = true;
  494. put_value(ptr, &proto, sizeof(proto), is_list);
  495. return true;
  496. }
  497. else if (!strcmp(val, "icmpv6"))
  498. {
  499. val = "ipv6-icmp";
  500. }
  501. else if (!strcmp(val, "tcpudp"))
  502. {
  503. proto.protocol = 6;
  504. if (put_value(ptr, &proto, sizeof(proto), is_list))
  505. {
  506. proto.protocol = 17;
  507. put_value(ptr, &proto, sizeof(proto), is_list);
  508. }
  509. return true;
  510. }
  511. ent = getprotobyname(val);
  512. if (ent)
  513. {
  514. proto.protocol = ent->p_proto;
  515. put_value(ptr, &proto, sizeof(proto), is_list);
  516. return true;
  517. }
  518. proto.protocol = strtoul(val, &e, 10);
  519. if ((e == val) || (*e != 0))
  520. return false;
  521. put_value(ptr, &proto, sizeof(proto), is_list);
  522. return true;
  523. }
  524. bool
  525. fw3_parse_ipset_method(void *ptr, const char *val, bool is_list)
  526. {
  527. return parse_enum(ptr, val, &fw3_ipset_method_names[FW3_IPSET_METHOD_BITMAP],
  528. FW3_IPSET_METHOD_BITMAP, FW3_IPSET_METHOD_LIST);
  529. }
  530. bool
  531. fw3_parse_ipset_datatype(void *ptr, const char *val, bool is_list)
  532. {
  533. struct fw3_ipset_datatype type = { };
  534. type.dir = "src";
  535. if (!strncmp(val, "dest_", 5))
  536. {
  537. val += 5;
  538. type.dir = "dst";
  539. }
  540. else if (!strncmp(val, "dst_", 4))
  541. {
  542. val += 4;
  543. type.dir = "dst";
  544. }
  545. else if (!strncmp(val, "src_", 4))
  546. {
  547. val += 4;
  548. type.dir = "src";
  549. }
  550. if (parse_enum(&type.type, val, &fw3_ipset_type_names[FW3_IPSET_TYPE_IP],
  551. FW3_IPSET_TYPE_IP, FW3_IPSET_TYPE_SET))
  552. {
  553. put_value(ptr, &type, sizeof(type), is_list);
  554. return true;
  555. }
  556. return false;
  557. }
  558. bool
  559. fw3_parse_date(void *ptr, const char *val, bool is_list)
  560. {
  561. unsigned int year = 1970, mon = 1, day = 1, hour = 0, min = 0, sec = 0;
  562. struct tm tm = { 0 };
  563. time_t ts;
  564. char *p;
  565. year = strtoul(val, &p, 10);
  566. if ((*p != '-' && *p) || year < 1970 || year > 2038)
  567. goto fail;
  568. else if (!*p)
  569. goto ret;
  570. mon = strtoul(++p, &p, 10);
  571. if ((*p != '-' && *p) || mon > 12)
  572. goto fail;
  573. else if (!*p)
  574. goto ret;
  575. day = strtoul(++p, &p, 10);
  576. if ((*p != 'T' && *p) || day > 31)
  577. goto fail;
  578. else if (!*p)
  579. goto ret;
  580. hour = strtoul(++p, &p, 10);
  581. if ((*p != ':' && *p) || hour > 23)
  582. goto fail;
  583. else if (!*p)
  584. goto ret;
  585. min = strtoul(++p, &p, 10);
  586. if ((*p != ':' && *p) || min > 59)
  587. goto fail;
  588. else if (!*p)
  589. goto ret;
  590. sec = strtoul(++p, &p, 10);
  591. if (*p || sec > 59)
  592. goto fail;
  593. ret:
  594. tm.tm_year = year - 1900;
  595. tm.tm_mon = mon - 1;
  596. tm.tm_mday = day;
  597. tm.tm_hour = hour;
  598. tm.tm_min = min;
  599. tm.tm_sec = sec;
  600. ts = mktime(&tm) - timezone;
  601. if (ts >= 0)
  602. {
  603. gmtime_r(&ts, (struct tm *)ptr);
  604. return true;
  605. }
  606. fail:
  607. return false;
  608. }
  609. bool
  610. fw3_parse_time(void *ptr, const char *val, bool is_list)
  611. {
  612. unsigned int hour = 0, min = 0, sec = 0;
  613. char *p;
  614. hour = strtoul(val, &p, 10);
  615. if (*p != ':' || hour > 23)
  616. goto fail;
  617. min = strtoul(++p, &p, 10);
  618. if ((*p != ':' && *p) || min > 59)
  619. goto fail;
  620. else if (!*p)
  621. goto ret;
  622. sec = strtoul(++p, &p, 10);
  623. if (*p || sec > 59)
  624. goto fail;
  625. ret:
  626. *((int *)ptr) = 60 * 60 * hour + 60 * min + sec;
  627. return true;
  628. fail:
  629. return false;
  630. }
  631. bool
  632. fw3_parse_weekdays(void *ptr, const char *val, bool is_list)
  633. {
  634. unsigned int w = 0;
  635. char *p, *s;
  636. if (*val == '!')
  637. {
  638. fw3_setbit(*(uint8_t *)ptr, 0);
  639. while (isspace(*++val));
  640. }
  641. if (!(s = strdup(val)))
  642. return false;
  643. for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
  644. {
  645. if (!parse_enum(&w, p, weekdays, 1, 7))
  646. {
  647. w = strtoul(p, &p, 10);
  648. if (*p || w < 1 || w > 7)
  649. {
  650. free(s);
  651. return false;
  652. }
  653. }
  654. fw3_setbit(*(uint8_t *)ptr, w);
  655. }
  656. free(s);
  657. return true;
  658. }
  659. bool
  660. fw3_parse_monthdays(void *ptr, const char *val, bool is_list)
  661. {
  662. unsigned int d;
  663. char *p, *s;
  664. if (*val == '!')
  665. {
  666. fw3_setbit(*(uint32_t *)ptr, 0);
  667. while (isspace(*++val));
  668. }
  669. if (!(s = strdup(val)))
  670. return false;
  671. for (p = strtok(s, " \t"); p; p = strtok(NULL, " \t"))
  672. {
  673. d = strtoul(p, &p, 10);
  674. if (*p || d < 1 || d > 31)
  675. {
  676. free(s);
  677. return false;
  678. }
  679. fw3_setbit(*(uint32_t *)ptr, d);
  680. }
  681. free(s);
  682. return true;
  683. }
  684. bool
  685. fw3_parse_include_type(void *ptr, const char *val, bool is_list)
  686. {
  687. return parse_enum(ptr, val, include_types,
  688. FW3_INC_TYPE_SCRIPT, FW3_INC_TYPE_RESTORE);
  689. }
  690. bool
  691. fw3_parse_reflection_source(void *ptr, const char *val, bool is_list)
  692. {
  693. return parse_enum(ptr, val, reflection_sources,
  694. FW3_REFLECTION_INTERNAL, FW3_REFLECTION_EXTERNAL);
  695. }
  696. bool
  697. fw3_parse_mark(void *ptr, const char *val, bool is_list)
  698. {
  699. uint32_t n;
  700. char *s, *e;
  701. struct fw3_mark *m = ptr;
  702. if (*val == '!')
  703. {
  704. m->invert = true;
  705. while (isspace(*++val));
  706. }
  707. if ((s = strchr(val, '/')) != NULL)
  708. *s++ = 0;
  709. n = strtoul(val, &e, 0);
  710. if (e == val || *e)
  711. return false;
  712. m->mark = n;
  713. m->mask = 0xFFFFFFFF;
  714. if (s)
  715. {
  716. n = strtoul(s, &e, 0);
  717. if (e == s || *e)
  718. return false;
  719. m->mask = n;
  720. }
  721. m->set = true;
  722. return true;
  723. }
  724. bool
  725. fw3_parse_dscp(void *ptr, const char *val, bool is_list)
  726. {
  727. uint32_t n;
  728. char *e;
  729. struct fw3_dscp *d = ptr;
  730. if (*val == '!')
  731. {
  732. d->invert = true;
  733. while (isspace(*++val));
  734. }
  735. for (n = 0; n < sizeof(dscp_classes) / sizeof(dscp_classes[0]); n++)
  736. {
  737. if (strcmp(dscp_classes[n].name, val))
  738. continue;
  739. d->set = true;
  740. d->dscp = dscp_classes[n].dscp;
  741. return true;
  742. }
  743. n = strtoul(val, &e, 0);
  744. if (e == val || *e || n > 0x3F)
  745. return false;
  746. d->set = true;
  747. d->dscp = n;
  748. return true;
  749. }
  750. bool
  751. fw3_parse_setmatch(void *ptr, const char *val, bool is_list)
  752. {
  753. struct fw3_setmatch *m = ptr;
  754. char *p, *s;
  755. int i;
  756. if (*val == '!')
  757. {
  758. m->invert = true;
  759. while (isspace(*++val));
  760. }
  761. if (!(s = strdup(val)))
  762. return false;
  763. if (!(p = strtok(s, " \t")))
  764. {
  765. free(s);
  766. return false;
  767. }
  768. strncpy(m->name, p, sizeof(m->name) - 1);
  769. for (i = 0, p = strtok(NULL, " \t,");
  770. i < 3 && p != NULL;
  771. i++, p = strtok(NULL, " \t,"))
  772. {
  773. if (!strncmp(p, "dest", 4) || !strncmp(p, "dst", 3))
  774. m->dir[i] = "dst";
  775. else if (!strncmp(p, "src", 3))
  776. m->dir[i] = "src";
  777. }
  778. free(s);
  779. m->set = true;
  780. return true;
  781. }
  782. bool
  783. fw3_parse_direction(void *ptr, const char *val, bool is_list)
  784. {
  785. bool *is_out = ptr;
  786. bool valid = true;
  787. if (!strcmp(val, "in") || !strcmp(val, "ingress"))
  788. *is_out = false;
  789. else if (!strcmp(val, "out") || !strcmp(val, "egress"))
  790. *is_out = true;
  791. else
  792. valid = false;
  793. return valid;
  794. }
  795. bool
  796. fw3_parse_cthelper(void *ptr, const char *val, bool is_list)
  797. {
  798. struct fw3_cthelpermatch m = { };
  799. if (*val == '!')
  800. {
  801. m.invert = true;
  802. while (isspace(*++val));
  803. }
  804. if (*val)
  805. {
  806. m.set = true;
  807. strncpy(m.name, val, sizeof(m.name) - 1);
  808. put_value(ptr, &m, sizeof(m), is_list);
  809. return true;
  810. }
  811. return false;
  812. }
  813. bool
  814. fw3_parse_setentry(void *ptr, const char *val, bool is_list)
  815. {
  816. struct fw3_setentry e = { };
  817. e.value = val;
  818. put_value(ptr, &e, sizeof(e), is_list);
  819. return true;
  820. }
  821. bool
  822. fw3_parse_options(void *s, const struct fw3_option *opts,
  823. struct uci_section *section)
  824. {
  825. char *p, *v;
  826. bool known, inv;
  827. struct uci_element *e, *l;
  828. struct uci_option *o;
  829. const struct fw3_option *opt;
  830. struct list_head *dest;
  831. bool valid = true;
  832. uci_foreach_element(&section->options, e)
  833. {
  834. o = uci_to_option(e);
  835. known = false;
  836. for (opt = opts; opt->name; opt++)
  837. {
  838. if (!opt->parse)
  839. continue;
  840. if (strcmp(opt->name, e->name))
  841. continue;
  842. if (o->type == UCI_TYPE_LIST)
  843. {
  844. if (!opt->elem_size)
  845. {
  846. warn_elem(e, "must not be a list");
  847. valid = false;
  848. }
  849. else
  850. {
  851. dest = (struct list_head *)((char *)s + opt->offset);
  852. uci_foreach_element(&o->v.list, l)
  853. {
  854. if (!l->name)
  855. continue;
  856. if (!opt->parse(dest, l->name, true))
  857. {
  858. warn_elem(e, "has invalid value '%s'", l->name);
  859. valid = false;
  860. continue;
  861. }
  862. }
  863. }
  864. }
  865. else
  866. {
  867. v = o->v.string;
  868. if (!v)
  869. continue;
  870. if (!opt->elem_size)
  871. {
  872. if (!opt->parse((char *)s + opt->offset, o->v.string, false))
  873. {
  874. warn_elem(e, "has invalid value '%s'", o->v.string);
  875. valid = false;
  876. }
  877. }
  878. else
  879. {
  880. inv = false;
  881. dest = (struct list_head *)((char *)s + opt->offset);
  882. for (p = strtok(v, " \t"); p != NULL; p = strtok(NULL, " \t"))
  883. {
  884. /* If we encounter a sole "!" token, assume that it
  885. * is meant to be part of the next token, so silently
  886. * skip it and remember the state... */
  887. if (!strcmp(p, "!"))
  888. {
  889. inv = true;
  890. continue;
  891. }
  892. /* The previous token was a sole "!", rewind pointer
  893. * back by one byte to precede the value with an
  894. * exclamation mark which effectively turns
  895. * ("!", "foo") into ("!foo") */
  896. if (inv)
  897. {
  898. *--p = '!';
  899. inv = false;
  900. }
  901. if (!opt->parse(dest, p, true))
  902. {
  903. warn_elem(e, "has invalid value '%s'", p);
  904. valid = false;
  905. continue;
  906. }
  907. }
  908. /* The last token was a sole "!" without any subsequent
  909. * text, so pass it to the option parser as-is. */
  910. if (inv && !opt->parse(dest, "!", true))
  911. {
  912. warn_elem(e, "has invalid value '%s'", p);
  913. valid = false;
  914. }
  915. }
  916. }
  917. known = true;
  918. break;
  919. }
  920. if (!known)
  921. warn_elem(e, "is unknown");
  922. }
  923. return valid;
  924. }
  925. bool
  926. fw3_parse_blob_options(void *s, const struct fw3_option *opts,
  927. struct blob_attr *a, const char *name)
  928. {
  929. char *p, *v, buf[16];
  930. bool known;
  931. unsigned rem, erem;
  932. struct blob_attr *o, *e;
  933. const struct fw3_option *opt;
  934. struct list_head *dest;
  935. bool valid = true;
  936. blobmsg_for_each_attr(o, a, rem)
  937. {
  938. known = false;
  939. for (opt = opts; opt->name; opt++)
  940. {
  941. if (!opt->parse)
  942. continue;
  943. if (strcmp(opt->name, blobmsg_name(o)))
  944. continue;
  945. if (blobmsg_type(o) == BLOBMSG_TYPE_ARRAY)
  946. {
  947. if (!opt->elem_size)
  948. {
  949. fprintf(stderr, "%s: '%s' must not be a list\n",
  950. name, opt->name);
  951. valid = false;
  952. }
  953. else
  954. {
  955. dest = (struct list_head *)((char *)s + opt->offset);
  956. blobmsg_for_each_attr(e, o, erem)
  957. {
  958. if (blobmsg_type(e) == BLOBMSG_TYPE_INT32) {
  959. snprintf(buf, sizeof(buf), "%d", blobmsg_get_u32(e));
  960. v = buf;
  961. } else {
  962. v = blobmsg_get_string(e);
  963. }
  964. if (!opt->parse(dest, v, true))
  965. {
  966. fprintf(stderr, "%s: '%s' has invalid value '%s'\n",
  967. name, opt->name, v);
  968. valid = false;
  969. continue;
  970. }
  971. }
  972. }
  973. }
  974. else
  975. {
  976. if (blobmsg_type(o) == BLOBMSG_TYPE_INT32) {
  977. snprintf(buf, sizeof(buf), "%d", blobmsg_get_u32(o));
  978. v = buf;
  979. } else {
  980. v = blobmsg_get_string(o);
  981. }
  982. if (!v)
  983. continue;
  984. if (!opt->elem_size)
  985. {
  986. if (!opt->parse((char *)s + opt->offset, v, false))
  987. {
  988. fprintf(stderr, "%s: '%s' has invalid value '%s'\n",
  989. name, opt->name, v);
  990. valid = false;
  991. }
  992. }
  993. else
  994. {
  995. dest = (struct list_head *)((char *)s + opt->offset);
  996. for (p = strtok(v, " \t"); p != NULL; p = strtok(NULL, " \t"))
  997. {
  998. if (!opt->parse(dest, p, true))
  999. {
  1000. fprintf(stderr, "%s: '%s' has invalid value '%s'\n",
  1001. name, opt->name, p);
  1002. valid = false;
  1003. continue;
  1004. }
  1005. }
  1006. }
  1007. }
  1008. known = true;
  1009. break;
  1010. }
  1011. if (!known && strcmp(blobmsg_name(o), "type"))
  1012. fprintf(stderr, "%s: '%s' is unknown\n", name, blobmsg_name(o));
  1013. }
  1014. return valid;
  1015. }
  1016. const char *
  1017. fw3_address_to_string(struct fw3_address *address, bool allow_invert, bool as_cidr)
  1018. {
  1019. char *p, ip[INET6_ADDRSTRLEN];
  1020. static char buf[INET6_ADDRSTRLEN * 2 + 2];
  1021. p = buf;
  1022. if (address->invert && allow_invert)
  1023. p += sprintf(p, "!");
  1024. inet_ntop(address->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
  1025. &address->address.v4, ip, sizeof(ip));
  1026. p += sprintf(p, "%s", ip);
  1027. if (address->range)
  1028. {
  1029. inet_ntop(address->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
  1030. &address->mask.v4, ip, sizeof(ip));
  1031. p += sprintf(p, "-%s", ip);
  1032. }
  1033. else if (!as_cidr)
  1034. {
  1035. inet_ntop(address->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
  1036. &address->mask.v4, ip, sizeof(ip));
  1037. p += sprintf(p, "/%s", ip);
  1038. }
  1039. else
  1040. {
  1041. p += sprintf(p, "/%u", fw3_netmask2bitlen(address->family,
  1042. &address->mask.v6));
  1043. }
  1044. return buf;
  1045. }