2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
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
11 http://www.apache.org/licenses/LICENSE-2.0
15 local ds = require "luci.dispatcher"
16 local ft = require "luci.tools.firewall"
18 m = Map("firewall", translate("Firewall - Port Forwards"),
19 translate("Port forwarding allows remote computers on the Internet to \
20 connect to a specific computer or service within the \
27 s = m:section(TypedSection, "redirect", translate("Port Forwards"))
28 s.template = "cbi/tblsection"
32 s.extedit = ds.build_url("admin/network/firewall/forwards/%s")
33 s.template_addremove = "firewall/cbi_addforward"
35 function s.create(self, section)
36 local n = m:formvalue("_newfwd.name")
37 local p = m:formvalue("_newfwd.proto")
38 local e = m:formvalue("_newfwd.extport")
39 local a = m:formvalue("_newfwd.intaddr")
40 local i = m:formvalue("_newfwd.intport")
42 if p == "other" or (p and a) then
43 created = TypedSection.create(self, section)
45 self.map:set(created, "target", "DNAT")
46 self.map:set(created, "src", "wan")
47 self.map:set(created, "dest", "lan")
48 self.map:set(created, "proto", (p ~= "other") and p or "all")
49 self.map:set(created, "src_dport", e)
50 self.map:set(created, "dest_ip", a)
51 self.map:set(created, "dest_port", i)
52 self.map:set(created, "name", n)
60 function s.parse(self, ...)
61 TypedSection.parse(self, ...)
63 m.uci:save("firewall")
64 luci.http.redirect(ds.build_url(
65 "admin/network/firewall/redirect", created
70 function s.filter(self, sid)
71 return (self.map:get(sid, "target") ~= "SNAT")
75 ft.opt_name(s, DummyValue, translate("Name"))
78 local function forward_proto_txt(self, s)
81 ft.fmt_proto(self.map:get(s, "proto"),
82 self.map:get(s, "icmp_type")) or "TCP+UDP"
86 local function forward_src_txt(self, s)
87 local z = ft.fmt_zone(self.map:get(s, "src"), translate("any zone"))
88 local a = ft.fmt_ip(self.map:get(s, "src_ip"), translate("any host"))
89 local p = ft.fmt_port(self.map:get(s, "src_port"))
90 local m = ft.fmt_mac(self.map:get(s, "src_mac"))
93 return translatef("From %s in %s with source %s and %s", a, z, p, m)
95 return translatef("From %s in %s with source %s", a, z, p or m)
97 return translatef("From %s in %s", a, z)
101 local function forward_via_txt(self, s)
102 local a = ft.fmt_ip(self.map:get(s, "src_dip"), translate("any router IP"))
103 local p = ft.fmt_port(self.map:get(s, "src_dport"))
106 return translatef("Via %s at %s", a, p)
108 return translatef("Via %s", a)
112 match = s:option(DummyValue, "match", translate("Match"))
115 function match.cfgvalue(self, s)
116 return "<small>%s<br />%s<br />%s</small>" % {
117 forward_proto_txt(self, s),
118 forward_src_txt(self, s),
119 forward_via_txt(self, s)
124 dest = s:option(DummyValue, "dest", translate("Forward to"))
127 function dest.cfgvalue(self, s)
128 local z = ft.fmt_zone(self.map:get(s, "dest"), translate("any zone"))
129 local a = ft.fmt_ip(self.map:get(s, "dest_ip"), translate("any host"))
130 local p = ft.fmt_port(self.map:get(s, "dest_port")) or
131 ft.fmt_port(self.map:get(s, "src_dport"))
134 return translatef("%s, %s in %s", a, p, z)
136 return translatef("%s in %s", a, z)
140 ft.opt_enabled(s, Flag, translate("Enable")).width = "1%"