iptables.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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, const char *chain);
  45. void fw3_ipt_delete_id_rules(struct fw3_ipt_handle *h, const char *chain);
  46. void fw3_ipt_create_chain(struct fw3_ipt_handle *h, const char *fmt, ...);
  47. void fw3_ipt_flush(struct fw3_ipt_handle *h);
  48. void fw3_ipt_gc(struct fw3_ipt_handle *h);
  49. void fw3_ipt_commit(struct fw3_ipt_handle *h);
  50. void fw3_ipt_close(struct fw3_ipt_handle *h);
  51. struct fw3_ipt_rule *fw3_ipt_rule_new(struct fw3_ipt_handle *h);
  52. void fw3_ipt_rule_proto(struct fw3_ipt_rule *r, struct fw3_protocol *proto);
  53. void fw3_ipt_rule_in_out(struct fw3_ipt_rule *r,
  54. struct fw3_device *in, struct fw3_device *out);
  55. void fw3_ipt_rule_src_dest(struct fw3_ipt_rule *r,
  56. struct fw3_address *src, struct fw3_address *dest);
  57. void fw3_ipt_rule_sport_dport(struct fw3_ipt_rule *r,
  58. struct fw3_port *sp, struct fw3_port *dp);
  59. void fw3_ipt_rule_device(struct fw3_ipt_rule *r, const char *device, bool out);
  60. void fw3_ipt_rule_mac(struct fw3_ipt_rule *r, struct fw3_mac *mac);
  61. void fw3_ipt_rule_icmptype(struct fw3_ipt_rule *r, struct fw3_icmptype *icmp);
  62. void fw3_ipt_rule_limit(struct fw3_ipt_rule *r, struct fw3_limit *limit);
  63. void fw3_ipt_rule_ipset(struct fw3_ipt_rule *r, struct fw3_setmatch *match);
  64. void fw3_ipt_rule_helper(struct fw3_ipt_rule *r, struct fw3_cthelpermatch *match);
  65. void fw3_ipt_rule_time(struct fw3_ipt_rule *r, struct fw3_time *time);
  66. void fw3_ipt_rule_mark(struct fw3_ipt_rule *r, struct fw3_mark *mark);
  67. void fw3_ipt_rule_dscp(struct fw3_ipt_rule *r, struct fw3_dscp *dscp);
  68. void fw3_ipt_rule_comment(struct fw3_ipt_rule *r, const char *fmt, ...);
  69. void fw3_ipt_rule_extra(struct fw3_ipt_rule *r, const char *extra);
  70. void fw3_ipt_rule_addarg(struct fw3_ipt_rule *r, bool inv,
  71. const char *k, const char *v);
  72. struct fw3_ipt_rule * fw3_ipt_rule_create(struct fw3_ipt_handle *handle,
  73. struct fw3_protocol *proto,
  74. struct fw3_device *in,
  75. struct fw3_device *out,
  76. struct fw3_address *src,
  77. struct fw3_address *dest);
  78. void __fw3_ipt_rule_append(struct fw3_ipt_rule *r, bool repl,
  79. const char *fmt, ...);
  80. #define fw3_ipt_rule_append(rule, ...) \
  81. __fw3_ipt_rule_append(rule, false, __VA_ARGS__)
  82. #define fw3_ipt_rule_replace(rule, ...) \
  83. __fw3_ipt_rule_append(rule, true, __VA_ARGS__)
  84. static inline void
  85. fw3_ipt_rule_target(struct fw3_ipt_rule *r, const char *fmt, ...)
  86. {
  87. va_list ap;
  88. char buf[32];
  89. va_start(ap, fmt);
  90. vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
  91. va_end(ap);
  92. fw3_ipt_rule_addarg(r, false, "-j", buf);
  93. }
  94. #endif