1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2010-2012 Jo-Philipp Wich <jow@openwrt.org>
3 -- Licensed to the public under the Apache License 2.0.
5 local ds = require "luci.dispatcher"
6 local ft = require "luci.tools.firewall"
8 m = Map("firewall", translate("Firewall - Port Forwards"),
9 translate("Port forwarding allows remote computers on the Internet to \
10 connect to a specific computer or service within the \
17 s = m:section(TypedSection, "redirect", translate("Port Forwards"))
18 s.template = "cbi/tblsection"
22 s.extedit = ds.build_url("admin/network/firewall/forwards/%s")
23 s.template_addremove = "firewall/cbi_addforward"
25 function s.create(self, section)
26 local n = m:formvalue("_newfwd.name")
27 local p = m:formvalue("_newfwd.proto")
28 local E = m:formvalue("_newfwd.extzone")
29 local e = m:formvalue("_newfwd.extport")
30 local I = m:formvalue("_newfwd.intzone")
31 local a = m:formvalue("_newfwd.intaddr")
32 local i = m:formvalue("_newfwd.intport")
34 if p == "other" or (p and a) then
35 created = TypedSection.create(self, section)
37 self.map:set(created, "target", "DNAT")
38 self.map:set(created, "src", E or "wan")
39 self.map:set(created, "dest", I or "lan")
40 self.map:set(created, "proto", (p ~= "other") and p or "all")
41 self.map:set(created, "src_dport", e)
42 self.map:set(created, "dest_ip", a)
43 self.map:set(created, "dest_port", i)
44 self.map:set(created, "name", n)
52 function s.parse(self, ...)
53 TypedSection.parse(self, ...)
55 m.uci:save("firewall")
56 luci.http.redirect(ds.build_url(
57 "admin/network/firewall/redirect", created
62 function s.filter(self, sid)
63 return (self.map:get(sid, "target") ~= "SNAT")
67 ft.opt_name(s, DummyValue, translate("Name"))
70 local function forward_proto_txt(self, s)
73 ft.fmt_proto(self.map:get(s, "proto"),
74 self.map:get(s, "icmp_type")) or "TCP+UDP"
78 local function forward_src_txt(self, s)
79 local z = ft.fmt_zone(self.map:get(s, "src"), translate("any zone"))
80 local a = ft.fmt_ip(self.map:get(s, "src_ip"), translate("any host"))
81 local p = ft.fmt_port(self.map:get(s, "src_port"))
82 local m = ft.fmt_mac(self.map:get(s, "src_mac"))
85 return translatef("From %s in %s with source %s and %s", a, z, p, m)
87 return translatef("From %s in %s with source %s", a, z, p or m)
89 return translatef("From %s in %s", a, z)
93 local function forward_via_txt(self, s)
94 local a = ft.fmt_ip(self.map:get(s, "src_dip"), translate("any router IP"))
95 local p = ft.fmt_port(self.map:get(s, "src_dport"))
98 return translatef("Via %s at %s", a, p)
100 return translatef("Via %s", a)
104 match = s:option(DummyValue, "match", translate("Match"))
107 function match.cfgvalue(self, s)
108 return "<small>%s<br />%s<br />%s</small>" % {
109 forward_proto_txt(self, s),
110 forward_src_txt(self, s),
111 forward_via_txt(self, s)
116 dest = s:option(DummyValue, "dest", translate("Forward to"))
119 function dest.cfgvalue(self, s)
120 local z = ft.fmt_zone(self.map:get(s, "dest"), translate("any zone"))
121 local a = ft.fmt_ip(self.map:get(s, "dest_ip"), translate("any host"))
122 local p = ft.fmt_port(self.map:get(s, "dest_port")) or
123 ft.fmt_port(self.map:get(s, "src_dport"))
126 return translatef("%s, %s in %s", a, p, z)
128 return translatef("%s in %s", a, z)
132 ft.opt_enabled(s, Flag, translate("Enable")).width = "1%"