options.c 23 KB

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