xtables-10.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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_XTABLES_10_H
  19. #define __FW3_XTABLES_10_H
  20. extern struct xtables_match *xtables_pending_matches;
  21. extern struct xtables_target *xtables_pending_targets;
  22. static inline void
  23. fw3_xt_reset(void)
  24. {
  25. xtables_matches = NULL;
  26. xtables_targets = NULL;
  27. xtables_pending_matches = NULL;
  28. xtables_pending_targets = NULL;
  29. }
  30. static inline const char *
  31. fw3_xt_get_match_name(struct xtables_match *m)
  32. {
  33. if (m->alias)
  34. return m->alias(m->m);
  35. return m->m->u.user.name;
  36. }
  37. static inline void
  38. fw3_xt_set_match_name(struct xtables_match *m)
  39. {
  40. if (m->real_name)
  41. strcpy(m->m->u.user.name, m->real_name);
  42. else
  43. strcpy(m->m->u.user.name, m->name);
  44. }
  45. static inline bool
  46. fw3_xt_has_match_parse(struct xtables_match *m)
  47. {
  48. return (m->parse || m->x6_parse);
  49. }
  50. static inline void
  51. fw3_xt_free_match_udata(struct xtables_match *m)
  52. {
  53. if (m->udata_size)
  54. {
  55. free(m->udata);
  56. m->udata = fw3_alloc(m->udata_size);
  57. }
  58. }
  59. static inline void
  60. fw3_xt_merge_match_options(struct xtables_globals *g, struct xtables_match *m)
  61. {
  62. if (m->x6_options)
  63. g->opts = xtables_options_xfrm(g->orig_opts, g->opts,
  64. m->x6_options, &m->option_offset);
  65. if (m->extra_opts)
  66. g->opts = xtables_merge_options(g->orig_opts, g->opts,
  67. m->extra_opts, &m->option_offset);
  68. }
  69. static inline const char *
  70. fw3_xt_get_target_name(struct xtables_target *t)
  71. {
  72. if (t->alias)
  73. return t->alias(t->t);
  74. return t->t->u.user.name;
  75. }
  76. static inline void
  77. fw3_xt_set_target_name(struct xtables_target *t, const char *name)
  78. {
  79. if (t->real_name)
  80. strcpy(t->t->u.user.name, t->real_name);
  81. else
  82. strcpy(t->t->u.user.name, name);
  83. }
  84. static inline bool
  85. fw3_xt_has_target_parse(struct xtables_target *t)
  86. {
  87. return (t->parse || t->x6_parse);
  88. }
  89. static inline void
  90. fw3_xt_free_target_udata(struct xtables_target *t)
  91. {
  92. if (t->udata_size)
  93. {
  94. free(t->udata);
  95. t->udata = fw3_alloc(t->udata_size);
  96. }
  97. }
  98. static inline void
  99. fw3_xt_merge_target_options(struct xtables_globals *g, struct xtables_target *t)
  100. {
  101. if (t->x6_options)
  102. g->opts = xtables_options_xfrm(g->orig_opts, g->opts,
  103. t->x6_options, &t->option_offset);
  104. else
  105. g->opts = xtables_merge_options(g->orig_opts, g->opts,
  106. t->extra_opts, &t->option_offset);
  107. }
  108. static inline void
  109. fw3_xt_print_matches(void *ip, struct xtables_rule_match *matches)
  110. {
  111. struct xtables_rule_match *rm;
  112. struct xtables_match *m;
  113. for (rm = matches; rm; rm = rm->next)
  114. {
  115. m = rm->match;
  116. printf(" -m %s", fw3_xt_get_match_name(m));
  117. if (m->save)
  118. m->save(ip, m->m);
  119. }
  120. }
  121. static inline void
  122. fw3_xt_print_target(void *ip, struct xtables_target *target)
  123. {
  124. if (target)
  125. {
  126. printf(" -j %s", fw3_xt_get_target_name(target));
  127. if (target->save)
  128. target->save(ip, target->t);
  129. }
  130. }
  131. #endif