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