iptables.htm 4.7 KB

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