1
0

iptables.htm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <%#
  2. Copyright 2008-2009 Steven Barth <steven@midlink.org>
  3. Copyright 2008-2015 Jo-Philipp Wich <jow@openwrt.org>
  4. Licensed to the public under the Apache License 2.0.
  5. -%>
  6. <%-
  7. require "luci.sys.iptparser"
  8. local wba = require "luci.tools.webadmin"
  9. local fs = require "nixio.fs"
  10. local io = require "io"
  11. local has_ip6tables = fs.access("/usr/sbin/ip6tables")
  12. local mode = 4
  13. if has_ip6tables then
  14. mode = luci.dispatcher.context.requestpath
  15. mode = tonumber(mode[#mode] ~= "iptables" and mode[#mode]) or 4
  16. end
  17. local ipt = luci.sys.iptparser.IptParser(mode)
  18. local rowcnt = 1
  19. function rowstyle()
  20. rowcnt = rowcnt + 1
  21. return (rowcnt % 2) + 1
  22. end
  23. function link_target(t,c)
  24. if ipt:is_custom_target(c) then
  25. return '<a href="#rule_%s_%s">%s</a>' %{ t:lower(), c, c }
  26. end
  27. return c
  28. end
  29. function link_iface(i)
  30. local net = wba.iface_get_network(i)
  31. if net and i ~= "lo" then
  32. return '<a href="%s">%s</a>' %{
  33. url("admin/network/network", net), i
  34. }
  35. end
  36. return i
  37. end
  38. local tables = { "Filter", "NAT", "Mangle", "Raw" }
  39. if mode == 6 then
  40. tables = { "Filter", "Mangle", "Raw" }
  41. local ok, lines = pcall(io.lines, "/proc/net/ip6_tables_names")
  42. if ok and lines then
  43. local line
  44. for line in lines do
  45. if line == "nat" then
  46. tables = { "Filter", "NAT", "Mangle", "Raw" }
  47. end
  48. end
  49. end
  50. end
  51. -%>
  52. <%+header%>
  53. <style type="text/css">
  54. span:target {
  55. color: blue;
  56. text-decoration: underline;
  57. }
  58. </style>
  59. <h2 name="content"><%:Firewall Status%></h2>
  60. <br />
  61. <% if has_ip6tables then %>
  62. <ul class="cbi-tabmenu">
  63. <li class="cbi-tab<%= mode ~= 4 and "-disabled" %>"><a href="<%=url("admin/status/iptables/4")%>"><%:IPv4 Firewall%></a></li>
  64. <li class="cbi-tab<%= mode ~= 6 and "-disabled" %>"><a href="<%=url("admin/status/iptables/6")%>"><%:IPv6 Firewall%></a></li>
  65. </ul>
  66. <% end %>
  67. <div class="cbi-map" style="position: relative">
  68. <form method="post" action="<%=url("admin/status/iptables_action")%>" style="position: absolute; right: 0">
  69. <input type="hidden" name="token" value="<%=token%>" />
  70. <input type="hidden" name="family" value="<%=mode%>" />
  71. <input type="submit" class="cbi-button" name="zero" value="<%:Reset Counters%>" />
  72. <input type="submit" class="cbi-button" name="restart" value="<%:Restart Firewall%>" />
  73. </form>
  74. <fieldset class="cbi-section">
  75. <% for _, tbl in ipairs(tables) do chaincnt = 0 %>
  76. <h3><%:Table%>: <%=tbl%></h3>
  77. <table class="cbi-section-table" style="font-size:90%">
  78. <% for _, chain in ipairs(ipt:chains(tbl)) do
  79. rowcnt = 0
  80. chaincnt = chaincnt + 1
  81. chaininfo = ipt:chain(tbl, chain)
  82. %>
  83. <tr class="cbi-section-table-titles cbi-rowstyle-<%=rowstyle()%>">
  84. <th class="cbi-section-table-cell" style="text-align:left" colspan="11">
  85. <br /><span id="rule_<%=tbl:lower()%>_<%=chain%>">
  86. <%:Chain%> <em><%=chain%></em>
  87. (<%- if chaininfo.policy then -%>
  88. <%:Policy%>: <em><%=chaininfo.policy%></em>, <%:Packets%>: <%=chaininfo.packets%>, <%:Traffic%>: <%=wba.byte_format(chaininfo.bytes)-%>
  89. <%- else -%>
  90. <%:References%>: <%=chaininfo.references-%>
  91. <%- end -%>)</span>
  92. </th>
  93. </tr>
  94. <tr class="cbi-section-table-descr">
  95. <th class="cbi-section-table-cell"><%:Pkts.%></th>
  96. <th class="cbi-section-table-cell"><%:Traffic%></th>
  97. <th class="cbi-section-table-cell"><%:Target%></th>
  98. <th class="cbi-section-table-cell"><%:Prot.%></th>
  99. <th class="cbi-section-table-cell"><%:In%></th>
  100. <th class="cbi-section-table-cell"><%:Out%></th>
  101. <th class="cbi-section-table-cell"><%:Source%></th>
  102. <th class="cbi-section-table-cell"><%:Destination%></th>
  103. <th class="cbi-section-table-cell" style="width:30%"><%:Options%></th>
  104. </tr>
  105. <% for _, rule in ipairs(ipt:find({table=tbl, chain=chain})) do %>
  106. <tr class="cbi-section-table-row cbi-rowstyle-<%=rowstyle()%>">
  107. <td><%=rule.packets%></td>
  108. <td style="white-space: nowrap"><%=wba.byte_format(rule.bytes)%></td>
  109. <td><%=rule.target and link_target(tbl, rule.target) or "-"%></td>
  110. <td><%=rule.protocol%></td>
  111. <td><%=link_iface(rule.inputif)%></td>
  112. <td><%=link_iface(rule.outputif)%></td>
  113. <td><%=rule.source%></td>
  114. <td><%=rule.destination%></td>
  115. <td style="width:30%"><small><%=#rule.options > 0 and luci.util.pcdata(table.concat(rule.options, " ")) or "-"%></small></td>
  116. </tr>
  117. <% end %>
  118. <% if rowcnt == 1 then %>
  119. <tr class="cbi-section-table-titles cbi-rowstyle-<%=rowstyle()%>">
  120. <td colspan="9"><em><%:No rules in this chain%></em></td>
  121. </tr>
  122. <% end %>
  123. <% end %>
  124. <% if chaincnt == 0 then %>
  125. <tr class="cbi-section-table-titles cbi-rowstyle-<%=rowstyle()%>">
  126. <td colspan="9"><em><%:No chains in this table%></em></td>
  127. </tr>
  128. <% end %>
  129. </table>
  130. <br /><br />
  131. <% end %>
  132. </fieldset>
  133. </div>
  134. <%+footer%>