65e3e5816dc0a897937073ac88e350eef6d9381b
[project/luci.git] / applications / luci-firewall / luasrc / model / cbi / luci_fw / rrule.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2010 Jo-Philipp Wich <xm@subsignal.org>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15
16 local sys = require "luci.sys"
17 local dsp = require "luci.dispatcher"
18
19 arg[1] = arg[1] or ""
20
21 m = Map("firewall", translate("Traffic Redirection"),
22         translate("Traffic redirection allows you to change the " ..
23                 "destination address of forwarded packets."))
24
25 m.redirect = dsp.build_url("admin", "network", "firewall")
26
27 if not m.uci:get(arg[1]) == "redirect" then
28         luci.http.redirect(m.redirect)
29         return
30 end
31
32 local has_v2 = nixio.fs.access("/lib/firewall/fw.sh")
33 local wan_zone = nil
34
35 m.uci:foreach("firewall", "zone",
36         function(s)
37                 local n = s.network or s.name
38                 if n then
39                         local i
40                         for i in n:gmatch("%S+") do
41                                 if i == "wan" then
42                                         wan_zone = s.name
43                                         return false
44                                 end
45                         end
46                 end
47         end)
48
49 s = m:section(NamedSection, arg[1], "redirect", "")
50 s.anonymous = true
51 s.addremove = false
52
53 s:tab("general", translate("General Settings"))
54 s:tab("advanced", translate("Advanced Settings"))
55
56 back = s:taboption("general", DummyValue, "_overview", translate("Overview"))
57 back.value = ""
58 back.titleref = luci.dispatcher.build_url("admin", "network", "firewall", "redirect")
59
60 name = s:taboption("general", Value, "_name", translate("Name"))
61 name.rmempty = true
62 name.size = 10
63
64 src = s:taboption("general", Value, "src", translate("Source zone"))
65 src.nocreate = true
66 src.default = "wan"
67 src.template = "cbi/firewall_zonelist"
68
69 proto = s:taboption("general", Value, "proto", translate("Protocol"))
70 proto.optional = true
71 proto:value("tcpudp", "TCP+UDP")
72 proto:value("tcp", "TCP")
73 proto:value("udp", "UDP")
74
75 dport = s:taboption("general", Value, "src_dport", translate("External port"),
76         translate("Match incoming traffic directed at the given " ..
77                 "destination port or port range on this host"))
78 dport.datatype = "portrange"
79 dport:depends("proto", "tcp")
80 dport:depends("proto", "udp")
81 dport:depends("proto", "tcpudp")
82
83 to = s:taboption("general", Value, "dest_ip", translate("Internal IP address"),
84         translate("Redirect matched incoming traffic to the specified " ..
85                 "internal host"))
86 to.datatype = "ip4addr"
87 for i, dataset in ipairs(luci.sys.net.arptable()) do
88         to:value(dataset["IP address"])
89 end
90
91 toport = s:taboption("general", Value, "dest_port", translate("Internal port (optional)"),
92         translate("Redirect matched incoming traffic to the given port on " ..
93                 "the internal host"))
94 toport.optional = true
95 toport.placeholder = "0-65535"
96 toport.datatype = "portrange"
97 toport:depends("proto", "tcp")
98 toport:depends("proto", "udp")
99 toport:depends("proto", "tcpudp")
100
101 target = s:taboption("advanced", ListValue, "target", translate("Redirection type"))
102 target:value("DNAT")
103 target:value("SNAT")
104
105 dest = s:taboption("advanced", Value, "dest", translate("Destination zone"))
106 dest.nocreate = true
107 dest.default = "lan"
108 dest.template = "cbi/firewall_zonelist"
109
110 src_dip = s:taboption("advanced", Value, "src_dip",
111         translate("Intended destination address"),
112         translate(
113                 "For DNAT, match incoming traffic directed at the given destination "..
114                 "ip address. For SNAT rewrite the source address to the given address."
115         ))
116
117 src_dip.optional = true
118 src_dip.datatype = "ip4addr"
119 src_dip.placeholder = translate("any")
120
121 src_mac = s:taboption("advanced", Value, "src_mac", translate("Source MAC address"))
122 src_mac.optional = true
123 src_mac.datatype = "macaddr"
124 src_mac.placeholder = translate("any")
125
126 src_ip = s:taboption("advanced", Value, "src_ip", translate("Source IP address"))
127 src_ip.optional = true
128 src_ip.datatype = "ip4addr"
129 src_ip.placeholder = translate("any")
130
131 sport = s:taboption("advanced", Value, "src_port", translate("Source port"),
132         translate("Match incoming traffic originating from the given " ..
133                 "source port or port range on the client host"))
134 sport.optional = true
135 sport.datatype = "portrange"
136 sport.placeholder = "0-65536"
137 sport:depends("proto", "tcp")
138 sport:depends("proto", "udp")
139 sport:depends("proto", "tcpudp")
140
141 reflection = s:taboption("advanced", Flag, "reflection", translate("Enable NAT Loopback"))
142 reflection.rmempty = true
143 reflection:depends({ target = "DNAT", src = wan_zone })
144 reflection.cfgvalue = function(...)
145         return Flag.cfgvalue(...) or "1"
146 end
147
148 return m