options.h 11 KB

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