applications/luci-firewall: fix rule table formatting, add mac & ip hints to various...
[project/luci.git] / applications / luci-firewall / luasrc / model / cbi / firewall / forward-details.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2011 Jo-Philipp Wich <xm@subsignal.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14
15 local sys = require "luci.sys"
16 local dsp = require "luci.dispatcher"
17 local ft  = require "luci.tools.firewall"
18
19 local m, s, o
20
21 arg[1] = arg[1] or ""
22
23 m = Map("firewall",
24         translate("Firewall - Port Forwards"),
25         translate("This page allows you to change advanced properties of the port \
26                    forwarding entry. In most cases there is no need to modify \
27                            those settings."))
28
29 m.redirect = dsp.build_url("admin/network/firewall/forwards")
30
31 if m.uci:get("firewall", arg[1]) ~= "redirect" then
32         luci.http.redirect(m.redirect)
33         return
34 else
35         local name = m:get(arg[1], "name") or m:get(arg[1], "_name")
36         if not name or #name == 0 then
37                 name = translate("(Unnamed Entry)")
38         end
39         m.title = "%s - %s" %{ translate("Firewall - Port Forwards"), name }
40 end
41
42 local wan_zone = nil
43
44 m.uci:foreach("firewall", "zone",
45         function(s)
46                 local n = s.network or s.name
47                 if n then
48                         local i
49                         for i in n:gmatch("%S+") do
50                                 if i == "wan" then
51                                         wan_zone = s.name
52                                         return false
53                                 end
54                         end
55                 end
56         end)
57
58 s = m:section(NamedSection, arg[1], "redirect", "")
59 s.anonymous = true
60 s.addremove = false
61
62 ft.opt_enabled(s, Button)
63 ft.opt_name(s, Value, translate("Name"))
64
65
66 o = s:option(Value, "proto", translate("Protocol"))
67 o:value("tcp udp", "TCP+UDP")
68 o:value("tcp", "TCP")
69 o:value("udp", "UDP")
70 o:value("icmp", "ICMP")
71
72 function o.cfgvalue(...)
73         local v = Value.cfgvalue(...)
74         if not v or v == "tcpudp" then
75                 return "tcp udp"
76         end
77         return v
78 end
79
80
81 o = s:option(Value, "src", translate("Source zone"))
82 o.nocreate = true
83 o.default = "wan"
84 o.template = "cbi/firewall_zonelist"
85
86
87 o = s:option(DynamicList, "src_mac",
88         translate("Source MAC address"),
89         translate("Only match incoming traffic from these MACs."))
90 o.rmempty = true
91 o.datatype = "macaddr"
92 o.placeholder = translate("any")
93
94 luci.sys.net.mac_hints(function(mac, name)
95         o:value(mac, "%s (%s)" %{ mac, name })
96 end)
97
98
99 o = s:option(Value, "src_ip",
100         translate("Source IP address"),
101         translate("Only match incoming traffic from this IP or range."))
102 o.rmempty = true
103 o.datatype = "neg(ip4addr)"
104 o.placeholder = translate("any")
105
106 luci.sys.net.ipv4_hints(function(ip, name)
107         o:value(ip, "%s (%s)" %{ ip, name })
108 end)
109
110
111 o = s:option(Value, "src_port",
112         translate("Source port"),
113         translate("Only match incoming traffic originating from the given source port or port range on the client host"))
114 o.rmempty = true
115 o.datatype = "portrange"
116 o.placeholder = translate("any")
117
118
119 o = s:option(Value, "src_dip",
120         translate("External IP address"),
121         translate("Only match incoming traffic directed at the given IP address."))
122
123 luci.sys.net.ipv4_hints(function(ip, name)
124         o:value(ip, "%s (%s)" %{ ip, name })
125 end)
126
127
128 o.rmempty = true
129 o.datatype = "ip4addr"
130 o.placeholder = translate("any")
131
132
133 o = s:option(Value, "src_dport", translate("External port"),
134         translate("Match incoming traffic directed at the given " ..
135                 "destination port or port range on this host"))
136 o.datatype = "portrange"
137
138
139
140 o = s:option(Value, "dest", translate("Internal zone"))
141 o.nocreate = true
142 o.default = "lan"
143 o.template = "cbi/firewall_zonelist"
144
145
146 o = s:option(Value, "dest_ip", translate("Internal IP address"),
147         translate("Redirect matched incoming traffic to the specified \
148                 internal host"))
149 o.datatype = "ip4addr"
150
151 luci.sys.net.ipv4_hints(function(ip, name)
152         o:value(ip, "%s (%s)" %{ ip, name })
153 end)
154
155
156 o = s:option(Value, "dest_port",
157         translate("Internal port"),
158         translate("Redirect matched incoming traffic to the given port on \
159                 the internal host"))
160 o.placeholder = translate("any")
161 o.datatype = "portrange"
162
163
164 o = s:option(Flag, "reflection", translate("Enable NAT Loopback"))
165 o.rmempty = true
166 o.default = o.enabled
167 o:depends("src", wan_zone)
168 o.cfgvalue = function(...)
169         return Flag.cfgvalue(...) or "1"
170 end
171
172
173 s:option(Value, "extra",
174         translate("Extra arguments"),
175         translate("Passes additional arguments to iptables. Use with care!"))
176
177
178 return m