zone-details.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. -- Copyright 2008 Steven Barth <steven@midlink.org>
  2. -- Copyright 2010-2011 Jo-Philipp Wich <jow@openwrt.org>
  3. -- Licensed to the public under the Apache License 2.0.
  4. local nw = require "luci.model.network"
  5. local fw = require "luci.model.firewall"
  6. local ds = require "luci.dispatcher"
  7. local ut = require "luci.util"
  8. local m, p, i, v
  9. local s, name, net, family, msrc, mdest, log, lim
  10. local s2, out, inp
  11. m = Map("firewall", translate("Firewall - Zone Settings"))
  12. m.redirect = luci.dispatcher.build_url("admin/network/firewall/zones")
  13. fw.init(m.uci)
  14. nw.init(m.uci)
  15. local zone = fw:get_zone(arg[1])
  16. if not zone then
  17. luci.http.redirect(dsp.build_url("admin/network/firewall/zones"))
  18. return
  19. else
  20. m.title = "%s - %s" %{
  21. translate("Firewall - Zone Settings"),
  22. translatef("Zone %q", zone:name() or "?")
  23. }
  24. end
  25. s = m:section(NamedSection, zone.sid, "zone",
  26. translatef("Zone %q", zone:name()),
  27. translatef("This section defines common properties of %q. \
  28. The <em>input</em> and <em>output</em> options set the default \
  29. policies for traffic entering and leaving this zone while the \
  30. <em>forward</em> option describes the policy for forwarded traffic \
  31. between different networks within the zone. \
  32. <em>Covered networks</em> specifies which available networks are \
  33. members of this zone.", zone:name()))
  34. s.anonymous = true
  35. s.addremove = false
  36. m.on_commit = function(map)
  37. local zone = fw:get_zone(arg[1])
  38. if zone then
  39. s.section = zone.sid
  40. s2.section = zone.sid
  41. end
  42. end
  43. s:tab("general", translate("General Settings"))
  44. s:tab("advanced", translate("Advanced Settings"))
  45. name = s:taboption("general", Value, "name", translate("Name"))
  46. name.optional = false
  47. name.forcewrite = true
  48. name.datatype = "and(uciname,maxlength(11))"
  49. function name.write(self, section, value)
  50. if zone:name() ~= value then
  51. fw:rename_zone(zone:name(), value)
  52. out.exclude = value
  53. inp.exclude = value
  54. end
  55. m.redirect = ds.build_url("admin/network/firewall/zones", value)
  56. m.title = "%s - %s" %{
  57. translate("Firewall - Zone Settings"),
  58. translatef("Zone %q", value or "?")
  59. }
  60. end
  61. p = {
  62. s:taboption("general", ListValue, "input", translate("Input")),
  63. s:taboption("general", ListValue, "output", translate("Output")),
  64. s:taboption("general", ListValue, "forward", translate("Forward"))
  65. }
  66. for i, v in ipairs(p) do
  67. v:value("REJECT", translate("reject"))
  68. v:value("DROP", translate("drop"))
  69. v:value("ACCEPT", translate("accept"))
  70. end
  71. s:taboption("general", Flag, "masq", translate("Masquerading"))
  72. s:taboption("general", Flag, "mtu_fix", translate("MSS clamping"))
  73. net = s:taboption("general", Value, "network", translate("Covered networks"))
  74. net.template = "cbi/network_netlist"
  75. net.widget = "checkbox"
  76. net.cast = "string"
  77. function net.formvalue(self, section)
  78. return Value.formvalue(self, section) or "-"
  79. end
  80. function net.cfgvalue(self, section)
  81. return Value.cfgvalue(self, section) or name:cfgvalue(section)
  82. end
  83. function net.write(self, section, value)
  84. zone:clear_networks()
  85. local n
  86. for n in ut.imatch(value) do
  87. zone:add_network(n)
  88. end
  89. end
  90. family = s:taboption("advanced", ListValue, "family",
  91. translate("Restrict to address family"))
  92. family.rmempty = true
  93. family:value("", translate("IPv4 and IPv6"))
  94. family:value("ipv4", translate("IPv4 only"))
  95. family:value("ipv6", translate("IPv6 only"))
  96. msrc = s:taboption("advanced", DynamicList, "masq_src",
  97. translate("Restrict Masquerading to given source subnets"))
  98. msrc.optional = true
  99. msrc.datatype = "list(neg(or(uciname,hostname,ip4addr)))"
  100. msrc.placeholder = "0.0.0.0/0"
  101. msrc:depends("family", "")
  102. msrc:depends("family", "ipv4")
  103. mdest = s:taboption("advanced", DynamicList, "masq_dest",
  104. translate("Restrict Masquerading to given destination subnets"))
  105. mdest.optional = true
  106. mdest.datatype = "list(neg(or(uciname,hostname,ip4addr)))"
  107. mdest.placeholder = "0.0.0.0/0"
  108. mdest:depends("family", "")
  109. mdest:depends("family", "ipv4")
  110. s:taboption("advanced", Flag, "conntrack",
  111. translate("Force connection tracking"))
  112. log = s:taboption("advanced", Flag, "log",
  113. translate("Enable logging on this zone"))
  114. log.rmempty = true
  115. log.enabled = "1"
  116. lim = s:taboption("advanced", Value, "log_limit",
  117. translate("Limit log messages"))
  118. lim.placeholder = "10/minute"
  119. lim:depends("log", "1")
  120. s2 = m:section(NamedSection, zone.sid, "fwd_out",
  121. translate("Inter-Zone Forwarding"),
  122. translatef("The options below control the forwarding policies between \
  123. this zone (%s) and other zones. <em>Destination zones</em> cover \
  124. forwarded traffic <strong>originating from %q</strong>. \
  125. <em>Source zones</em> match forwarded traffic from other zones \
  126. <strong>targeted at %q</strong>. The forwarding rule is \
  127. <em>unidirectional</em>, e.g. a forward from lan to wan does \
  128. <em>not</em> imply a permission to forward from wan to lan as well.",
  129. zone:name(), zone:name(), zone:name()
  130. ))
  131. out = s2:option(Value, "out",
  132. translate("Allow forward to <em>destination zones</em>:"))
  133. out.nocreate = true
  134. out.widget = "checkbox"
  135. out.exclude = zone:name()
  136. out.template = "cbi/firewall_zonelist"
  137. inp = s2:option(Value, "in",
  138. translate("Allow forward from <em>source zones</em>:"))
  139. inp.nocreate = true
  140. inp.widget = "checkbox"
  141. inp.exclude = zone:name()
  142. inp.template = "cbi/firewall_zonelist"
  143. function out.cfgvalue(self, section)
  144. local v = { }
  145. local f
  146. for _, f in ipairs(zone:get_forwardings_by("src")) do
  147. v[#v+1] = f:dest()
  148. end
  149. return table.concat(v, " ")
  150. end
  151. function inp.cfgvalue(self, section)
  152. local v = { }
  153. local f
  154. for _, f in ipairs(zone:get_forwardings_by("dest")) do
  155. v[#v+1] = f:src()
  156. end
  157. return v
  158. end
  159. function out.formvalue(self, section)
  160. return Value.formvalue(self, section) or "-"
  161. end
  162. function inp.formvalue(self, section)
  163. return Value.formvalue(self, section) or "-"
  164. end
  165. function out.write(self, section, value)
  166. zone:del_forwardings_by("src")
  167. local f
  168. for f in ut.imatch(value) do
  169. zone:add_forwarding_to(f)
  170. end
  171. end
  172. function inp.write(self, section, value)
  173. zone:del_forwardings_by("dest")
  174. local f
  175. for f in ut.imatch(value) do
  176. zone:add_forwarding_from(f)
  177. end
  178. end
  179. return m