iptables.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * firewall3 - 3rd OpenWrt UCI firewall implementation
  3. *
  4. * Copyright (C) 2013 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_IPTABLES_H
  19. #define __FW3_IPTABLES_H
  20. #ifndef DISABLE_STATIC_EXTENSIONS
  21. /* libipt*ext.so interfaces */
  22. extern void init_extensions(void);
  23. extern void init_extensions4(void);
  24. extern void init_extensions6(void);
  25. #else
  26. static inline void init_extensions(void) { }
  27. static inline void init_extensions4(void) { }
  28. static inline void init_extensions6(void) { }
  29. #endif
  30. /* Required by certain extensions like SNAT and DNAT */
  31. extern int kernel_version;
  32. void get_kernel_version(void);
  33. struct fw3_ipt_handle {
  34. enum fw3_family family;
  35. enum fw3_table table;
  36. void *handle;
  37. };
  38. struct fw3_ipt_rule;
  39. struct fw3_ipt_handle *fw3_ipt_open(enum fw3_family family,
  40. enum fw3_table table);
  41. void fw3_ipt_set_policy(struct fw3_ipt_handle *h, const char *chain,
  42. enum fw3_flag policy);
  43. void fw3_ipt_flush_chain(struct fw3_ipt_handle *h, const char *chain);
  44. void fw3_ipt_delete_chain(struct fw3_ipt_handle *h, bool if_unused,
  45. const char *chain);
  46. void fw3_ipt_delete_id_rules(struct fw3_ipt_handle *h, const char *chain);
  47. void fw3_ipt_create_chain(struct fw3_ipt_handle *h, bool ignore_existing,
  48. const char *chain);
  49. void fw3_ipt_flush(struct fw3_ipt_handle *h);
  50. void fw3_ipt_gc(struct fw3_ipt_handle *h);
  51. void fw3_ipt_commit(struct fw3_ipt_handle *h);
  52. void fw3_ipt_close(struct fw3_ipt_handle *h);
  53. struct fw3_ipt_rule *fw3_ipt_rule_new(struct fw3_ipt_handle *h);
  54. void fw3_ipt_rule_proto(struct fw3_ipt_rule *r, struct fw3_protocol *proto);
  55. void fw3_ipt_rule_in_out(struct fw3_ipt_rule *r,
  56. struct fw3_device *in, struct fw3_device *out);
  57. void fw3_ipt_rule_src_dest(struct fw3_ipt_rule *r,
  58. struct fw3_address *src, struct fw3_address *dest);
  59. void fw3_ipt_rule_sport_dport(struct fw3_ipt_rule *r,
  60. struct fw3_port *sp, struct fw3_port *dp);
  61. void fw3_ipt_rule_device(struct fw3_ipt_rule *r, const char *device, bool out);
  62. void fw3_ipt_rule_mac(struct fw3_ipt_rule *r, struct fw3_mac *mac);
  63. void fw3_ipt_rule_icmptype(struct fw3_ipt_rule *r, struct fw3_icmptype *icmp);
  64. void fw3_ipt_rule_limit(struct fw3_ipt_rule *r, struct fw3_limit *limit);
  65. void fw3_ipt_rule_ipset(struct fw3_ipt_rule *r, struct fw3_setmatch *match);
  66. void fw3_ipt_rule_helper(struct fw3_ipt_rule *r, struct fw3_cthelpermatch *match);
  67. void fw3_ipt_rule_time(struct fw3_ipt_rule *r, struct fw3_time *time);
  68. void fw3_ipt_rule_mark(struct fw3_ipt_rule *r, struct fw3_mark *mark);
  69. void fw3_ipt_rule_dscp(struct fw3_ipt_rule *r, struct fw3_dscp *dscp);
  70. void fw3_ipt_rule_comment(struct fw3_ipt_rule *r, const char *fmt, ...);
  71. void fw3_ipt_rule_extra(struct fw3_ipt_rule *r, const char *extra);
  72. void fw3_ipt_rule_addarg(struct fw3_ipt_rule *r, bool inv,
  73. const char *k, const char *v);
  74. struct fw3_ipt_rule * fw3_ipt_rule_create(struct fw3_ipt_handle *handle,
  75. struct fw3_protocol *proto,
  76. struct fw3_device *in,
  77. struct fw3_device *out,
  78. struct fw3_address *src,
  79. struct fw3_address *dest);
  80. void __fw3_ipt_rule_append(struct fw3_ipt_rule *r, bool repl,
  81. const char *fmt, ...);
  82. #define fw3_ipt_rule_append(rule, ...) \
  83. __fw3_ipt_rule_append(rule, false, __VA_ARGS__)
  84. #define fw3_ipt_rule_replace(rule, ...) \
  85. __fw3_ipt_rule_append(rule, true, __VA_ARGS__)
  86. static inline void
  87. fw3_ipt_rule_target(struct fw3_ipt_rule *r, const char *fmt, ...)
  88. {
  89. va_list ap;
  90. char buf[32];
  91. va_start(ap, fmt);
  92. vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
  93. va_end(ap);
  94. fw3_ipt_rule_addarg(r, false, "-j", buf);
  95. }
  96. #endif