options.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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. #ifndef __FW3_OPTIONS_H
  19. #define __FW3_OPTIONS_H
  20. #include <errno.h>
  21. #include <stdlib.h>
  22. #include <stdarg.h>
  23. #include <stdbool.h>
  24. #include <ctype.h>
  25. #include <string.h>
  26. #include <netdb.h>
  27. #include <arpa/inet.h>
  28. #include <sys/socket.h>
  29. #define _LINUX_IN_H
  30. #define _LINUX_IN6_H
  31. #include <netinet/in.h>
  32. #include <netinet/ether.h>
  33. #include <time.h>
  34. #include <uci.h>
  35. #include <libubox/list.h>
  36. #include <libubox/utils.h>
  37. #include <libubox/blobmsg.h>
  38. #include "icmp_codes.h"
  39. #include "utils.h"
  40. enum fw3_table
  41. {
  42. FW3_TABLE_FILTER = 0,
  43. FW3_TABLE_NAT = 1,
  44. FW3_TABLE_MANGLE = 2,
  45. FW3_TABLE_RAW = 3,
  46. };
  47. enum fw3_family
  48. {
  49. FW3_FAMILY_ANY = 0,
  50. FW3_FAMILY_V4 = 4,
  51. FW3_FAMILY_V6 = 5,
  52. };
  53. enum fw3_flag
  54. {
  55. FW3_FLAG_UNSPEC = 0,
  56. FW3_FLAG_ACCEPT = 6,
  57. FW3_FLAG_REJECT = 7,
  58. FW3_FLAG_DROP = 8,
  59. FW3_FLAG_NOTRACK = 9,
  60. FW3_FLAG_HELPER = 10,
  61. FW3_FLAG_MARK = 11,
  62. FW3_FLAG_DSCP = 12,
  63. FW3_FLAG_DNAT = 13,
  64. FW3_FLAG_SNAT = 14,
  65. FW3_FLAG_MASQUERADE = 15,
  66. FW3_FLAG_SRC_ACCEPT = 16,
  67. FW3_FLAG_SRC_REJECT = 17,
  68. FW3_FLAG_SRC_DROP = 18,
  69. FW3_FLAG_CUSTOM_CHAINS = 19,
  70. FW3_FLAG_SYN_FLOOD = 20,
  71. FW3_FLAG_MTU_FIX = 21,
  72. FW3_FLAG_DROP_INVALID = 22,
  73. FW3_FLAG_HOTPLUG = 23,
  74. __FW3_FLAG_MAX
  75. };
  76. enum fw3_reject_code
  77. {
  78. FW3_REJECT_CODE_TCP_RESET = 0,
  79. FW3_REJECT_CODE_PORT_UNREACH = 1,
  80. FW3_REJECT_CODE_ADM_PROHIBITED = 2,
  81. __FW3_REJECT_CODE_MAX
  82. };
  83. extern const char *fw3_flag_names[__FW3_FLAG_MAX];
  84. enum fw3_limit_unit
  85. {
  86. FW3_LIMIT_UNIT_SECOND = 0,
  87. FW3_LIMIT_UNIT_MINUTE = 1,
  88. FW3_LIMIT_UNIT_HOUR = 2,
  89. FW3_LIMIT_UNIT_DAY = 3,
  90. __FW3_LIMIT_UNIT_MAX
  91. };
  92. extern const char *fw3_limit_units[__FW3_LIMIT_UNIT_MAX];
  93. enum fw3_ipset_method
  94. {
  95. FW3_IPSET_METHOD_UNSPEC = 0,
  96. FW3_IPSET_METHOD_BITMAP = 1,
  97. FW3_IPSET_METHOD_HASH = 2,
  98. FW3_IPSET_METHOD_LIST = 3,
  99. __FW3_IPSET_METHOD_MAX
  100. };
  101. enum fw3_ipset_type
  102. {
  103. FW3_IPSET_TYPE_UNSPEC = 0,
  104. FW3_IPSET_TYPE_IP = 1,
  105. FW3_IPSET_TYPE_PORT = 2,
  106. FW3_IPSET_TYPE_MAC = 3,
  107. FW3_IPSET_TYPE_NET = 4,
  108. FW3_IPSET_TYPE_SET = 5,
  109. __FW3_IPSET_TYPE_MAX
  110. };
  111. extern const char *fw3_ipset_method_names[__FW3_IPSET_METHOD_MAX];
  112. extern const char *fw3_ipset_type_names[__FW3_IPSET_TYPE_MAX];
  113. enum fw3_include_type
  114. {
  115. FW3_INC_TYPE_SCRIPT = 0,
  116. FW3_INC_TYPE_RESTORE = 1,
  117. };
  118. enum fw3_reflection_source
  119. {
  120. FW3_REFLECTION_INTERNAL = 0,
  121. FW3_REFLECTION_EXTERNAL = 1,
  122. };
  123. struct fw3_ipset_datatype
  124. {
  125. struct list_head list;
  126. enum fw3_ipset_type type;
  127. const char *dir;
  128. };
  129. struct fw3_setmatch
  130. {
  131. bool set;
  132. bool invert;
  133. char name[32];
  134. const char *dir[3];
  135. struct fw3_ipset *ptr;
  136. };
  137. struct fw3_device
  138. {
  139. struct list_head list;
  140. bool set;
  141. bool any;
  142. bool invert;
  143. char name[32];
  144. char network[32];
  145. };
  146. struct fw3_address
  147. {
  148. struct list_head list;
  149. bool set;
  150. bool range;
  151. bool invert;
  152. bool resolved;
  153. enum fw3_family family;
  154. union {
  155. struct in_addr v4;
  156. struct in6_addr v6;
  157. struct ether_addr mac;
  158. } address;
  159. union {
  160. struct in_addr v4;
  161. struct in6_addr v6;
  162. struct ether_addr mac;
  163. } mask;
  164. };
  165. struct fw3_mac
  166. {
  167. struct list_head list;
  168. bool set;
  169. bool invert;
  170. struct ether_addr mac;
  171. };
  172. struct fw3_protocol
  173. {
  174. struct list_head list;
  175. bool any;
  176. bool invert;
  177. uint32_t protocol;
  178. };
  179. struct fw3_port
  180. {
  181. struct list_head list;
  182. bool set;
  183. bool invert;
  184. uint16_t port_min;
  185. uint16_t port_max;
  186. };
  187. struct fw3_icmptype
  188. {
  189. struct list_head list;
  190. bool invert;
  191. enum fw3_family family;
  192. uint8_t type;
  193. uint8_t code_min;
  194. uint8_t code_max;
  195. uint8_t type6;
  196. uint8_t code6_min;
  197. uint8_t code6_max;
  198. };
  199. struct fw3_limit
  200. {
  201. bool invert;
  202. int rate;
  203. int burst;
  204. enum fw3_limit_unit unit;
  205. };
  206. struct fw3_time
  207. {
  208. bool utc;
  209. struct tm datestart;
  210. struct tm datestop;
  211. uint32_t timestart;
  212. uint32_t timestop;
  213. uint32_t monthdays; /* bit 0 is invert + 1 .. 31 */
  214. uint8_t weekdays; /* bit 0 is invert + 1 .. 7 */
  215. };
  216. struct fw3_mark
  217. {
  218. bool set;
  219. bool invert;
  220. uint32_t mark;
  221. uint32_t mask;
  222. };
  223. struct fw3_dscp
  224. {
  225. bool set;
  226. bool invert;
  227. uint8_t dscp;
  228. };
  229. struct fw3_cthelpermatch
  230. {
  231. struct list_head list;
  232. bool set;
  233. bool invert;
  234. char name[32];
  235. struct fw3_cthelper *ptr;
  236. };
  237. struct fw3_defaults
  238. {
  239. enum fw3_flag policy_input;
  240. enum fw3_flag policy_output;
  241. enum fw3_flag policy_forward;
  242. bool drop_invalid;
  243. enum fw3_reject_code tcp_reject_code;
  244. enum fw3_reject_code any_reject_code;
  245. bool syn_flood;
  246. struct fw3_limit syn_flood_rate;
  247. bool tcp_syncookies;
  248. int tcp_ecn;
  249. bool tcp_window_scaling;
  250. bool accept_redirects;
  251. bool accept_source_route;
  252. bool custom_chains;
  253. bool auto_helper;
  254. bool flow_offloading;
  255. bool flow_offloading_hw;
  256. bool disable_ipv6;
  257. uint32_t flags[2];
  258. };
  259. struct fw3_zone
  260. {
  261. struct list_head list;
  262. bool enabled;
  263. const char *name;
  264. enum fw3_family family;
  265. enum fw3_flag policy_input;
  266. enum fw3_flag policy_output;
  267. enum fw3_flag policy_forward;
  268. struct list_head networks;
  269. struct list_head devices;
  270. struct list_head subnets;
  271. const char *extra_src;
  272. const char *extra_dest;
  273. bool masq;
  274. bool masq_allow_invalid;
  275. struct list_head masq_src;
  276. struct list_head masq_dest;
  277. bool mtu_fix;
  278. struct list_head cthelpers;
  279. int log;
  280. struct fw3_limit log_limit;
  281. bool custom_chains;
  282. bool auto_helper;
  283. uint32_t flags[2];
  284. struct list_head old_addrs;
  285. };
  286. struct fw3_rule
  287. {
  288. struct list_head list;
  289. bool enabled;
  290. const char *name;
  291. enum fw3_family family;
  292. struct fw3_zone *_src;
  293. struct fw3_zone *_dest;
  294. const char *device;
  295. bool direction_out;
  296. struct fw3_device src;
  297. struct fw3_device dest;
  298. struct fw3_setmatch ipset;
  299. struct fw3_cthelpermatch helper;
  300. struct list_head proto;
  301. struct list_head ip_src;
  302. struct list_head mac_src;
  303. struct list_head port_src;
  304. struct list_head ip_dest;
  305. struct list_head port_dest;
  306. struct list_head icmp_type;
  307. struct fw3_limit limit;
  308. struct fw3_time time;
  309. struct fw3_mark mark;
  310. struct fw3_dscp dscp;
  311. enum fw3_flag target;
  312. struct fw3_mark set_mark;
  313. struct fw3_mark set_xmark;
  314. struct fw3_dscp set_dscp;
  315. struct fw3_cthelpermatch set_helper;
  316. const char *extra;
  317. };
  318. struct fw3_redirect
  319. {
  320. struct list_head list;
  321. bool enabled;
  322. const char *name;
  323. enum fw3_family family;
  324. struct fw3_zone *_src;
  325. struct fw3_zone *_dest;
  326. struct fw3_device src;
  327. struct fw3_device dest;
  328. struct fw3_setmatch ipset;
  329. struct fw3_cthelpermatch helper;
  330. struct list_head proto;
  331. struct fw3_address ip_src;
  332. struct list_head mac_src;
  333. struct fw3_port port_src;
  334. struct fw3_address ip_dest;
  335. struct fw3_port port_dest;
  336. struct fw3_address ip_redir;
  337. struct fw3_port port_redir;
  338. struct fw3_limit limit;
  339. struct fw3_time time;
  340. struct fw3_mark mark;
  341. enum fw3_flag target;
  342. const char *extra;
  343. bool local;
  344. bool reflection;
  345. enum fw3_reflection_source reflection_src;
  346. };
  347. struct fw3_snat
  348. {
  349. struct list_head list;
  350. bool enabled;
  351. const char *name;
  352. enum fw3_family family;
  353. struct fw3_zone *_src;
  354. struct fw3_device src;
  355. struct fw3_setmatch ipset;
  356. struct fw3_cthelpermatch helper;
  357. const char *device;
  358. struct list_head proto;
  359. struct fw3_address ip_src;
  360. struct fw3_port port_src;
  361. struct fw3_address ip_dest;
  362. struct fw3_port port_dest;
  363. struct fw3_address ip_snat;
  364. struct fw3_port port_snat;
  365. struct fw3_limit limit;
  366. struct fw3_time time;
  367. struct fw3_mark mark;
  368. bool connlimit_ports;
  369. enum fw3_flag target;
  370. const char *extra;
  371. };
  372. struct fw3_forward
  373. {
  374. struct list_head list;
  375. bool enabled;
  376. const char *name;
  377. enum fw3_family family;
  378. struct fw3_zone *_src;
  379. struct fw3_zone *_dest;
  380. struct fw3_device src;
  381. struct fw3_device dest;
  382. };
  383. struct fw3_ipset
  384. {
  385. struct list_head list;
  386. bool enabled;
  387. const char *name;
  388. enum fw3_family family;
  389. enum fw3_ipset_method method;
  390. struct list_head datatypes;
  391. struct fw3_address iprange;
  392. struct fw3_port portrange;
  393. int netmask;
  394. int maxelem;
  395. int hashsize;
  396. int timeout;
  397. const char *external;
  398. struct list_head entries;
  399. const char *loadfile;
  400. uint32_t flags[2];
  401. };
  402. struct fw3_include
  403. {
  404. struct list_head list;
  405. bool enabled;
  406. const char *name;
  407. enum fw3_family family;
  408. const char *path;
  409. enum fw3_include_type type;
  410. bool reload;
  411. };
  412. struct fw3_cthelper
  413. {
  414. struct list_head list;
  415. bool enabled;
  416. const char *name;
  417. const char *module;
  418. const char *description;
  419. enum fw3_family family;
  420. struct list_head proto;
  421. struct fw3_port port;
  422. };
  423. struct fw3_setentry
  424. {
  425. struct list_head list;
  426. const char *value;
  427. };
  428. struct fw3_state
  429. {
  430. struct uci_context *uci;
  431. struct fw3_defaults defaults;
  432. struct list_head zones;
  433. struct list_head rules;
  434. struct list_head redirects;
  435. struct list_head snats;
  436. struct list_head forwards;
  437. struct list_head ipsets;
  438. struct list_head includes;
  439. struct list_head cthelpers;
  440. bool disable_ipsets;
  441. bool statefile;
  442. };
  443. struct fw3_chain_spec {
  444. int family;
  445. int table;
  446. int flag;
  447. const char *format;
  448. };
  449. struct fw3_option
  450. {
  451. const char *name;
  452. bool (*parse)(void *, const char *, bool);
  453. uintptr_t offset;
  454. size_t elem_size;
  455. };
  456. #define FW3_OPT(name, parse, structure, member) \
  457. { name, fw3_parse_##parse, offsetof(struct fw3_##structure, member) }
  458. #define FW3_LIST(name, parse, structure, member) \
  459. { name, fw3_parse_##parse, offsetof(struct fw3_##structure, member), \
  460. sizeof(struct fw3_##structure) }
  461. bool fw3_parse_bool(void *ptr, const char *val, bool is_list);
  462. bool fw3_parse_int(void *ptr, const char *val, bool is_list);
  463. bool fw3_parse_string(void *ptr, const char *val, bool is_list);
  464. bool fw3_parse_target(void *ptr, const char *val, bool is_list);
  465. bool fw3_parse_reject_code(void *ptr, const char *val, bool is_list);
  466. bool fw3_parse_limit(void *ptr, const char *val, bool is_list);
  467. bool fw3_parse_device(void *ptr, const char *val, bool is_list);
  468. bool fw3_parse_address(void *ptr, const char *val, bool is_list);
  469. bool fw3_parse_network(void *ptr, const char *val, bool is_list);
  470. bool fw3_parse_mac(void *ptr, const char *val, bool is_list);
  471. bool fw3_parse_port(void *ptr, const char *val, bool is_list);
  472. bool fw3_parse_family(void *ptr, const char *val, bool is_list);
  473. bool fw3_parse_icmptype(void *ptr, const char *val, bool is_list);
  474. bool fw3_parse_protocol(void *ptr, const char *val, bool is_list);
  475. bool fw3_parse_ipset_method(void *ptr, const char *val, bool is_list);
  476. bool fw3_parse_ipset_datatype(void *ptr, const char *val, bool is_list);
  477. bool fw3_parse_include_type(void *ptr, const char *val, bool is_list);
  478. bool fw3_parse_reflection_source(void *ptr, const char *val, bool is_list);
  479. bool fw3_parse_date(void *ptr, const char *val, bool is_list);
  480. bool fw3_parse_time(void *ptr, const char *val, bool is_list);
  481. bool fw3_parse_weekdays(void *ptr, const char *val, bool is_list);
  482. bool fw3_parse_monthdays(void *ptr, const char *val, bool is_list);
  483. bool fw3_parse_mark(void *ptr, const char *val, bool is_list);
  484. bool fw3_parse_dscp(void *ptr, const char *val, bool is_list);
  485. bool fw3_parse_setmatch(void *ptr, const char *val, bool is_list);
  486. bool fw3_parse_direction(void *ptr, const char *val, bool is_list);
  487. bool fw3_parse_cthelper(void *ptr, const char *val, bool is_list);
  488. bool fw3_parse_setentry(void *ptr, const char *val, bool is_list);
  489. bool fw3_parse_options(void *s, const struct fw3_option *opts,
  490. struct uci_section *section);
  491. bool fw3_parse_blob_options(void *s, const struct fw3_option *opts,
  492. struct blob_attr *a, const char *name);
  493. const char * fw3_address_to_string(struct fw3_address *address,
  494. bool allow_invert, bool as_cidr);
  495. #endif