rules.lua 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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.
  4. local ds = require "luci.dispatcher"
  5. local ft = require "luci.tools.firewall"
  6. m = Map("firewall",
  7. translate("Firewall - Traffic Rules"),
  8. translate("Traffic rules define policies for packets traveling between \
  9. different zones, for example to reject traffic between certain hosts \
  10. or to open WAN ports on the router."))
  11. --
  12. -- Rules
  13. --
  14. s = m:section(TypedSection, "rule", translate("Traffic Rules"))
  15. s.addremove = true
  16. s.anonymous = true
  17. s.sortable = true
  18. s.template = "cbi/tblsection"
  19. s.extedit = ds.build_url("admin/network/firewall/rules/%s")
  20. s.defaults.target = "ACCEPT"
  21. s.template_addremove = "firewall/cbi_addrule"
  22. function s.create(self, section)
  23. created = TypedSection.create(self, section)
  24. end
  25. function s.parse(self, ...)
  26. TypedSection.parse(self, ...)
  27. local i_n = m:formvalue("_newopen.name")
  28. local i_p = m:formvalue("_newopen.proto")
  29. local i_e = m:formvalue("_newopen.extport")
  30. local i_x = m:formvalue("_newopen.submit")
  31. local f_n = m:formvalue("_newfwd.name")
  32. local f_s = m:formvalue("_newfwd.src")
  33. local f_d = m:formvalue("_newfwd.dest")
  34. local f_x = m:formvalue("_newfwd.submit")
  35. if i_x then
  36. created = TypedSection.create(self, section)
  37. self.map:set(created, "target", "ACCEPT")
  38. self.map:set(created, "src", "wan")
  39. self.map:set(created, "proto", (i_p ~= "other") and i_p or "all")
  40. self.map:set(created, "dest_port", i_e)
  41. self.map:set(created, "name", i_n)
  42. if i_p ~= "other" and i_e and #i_e > 0 then
  43. created = nil
  44. end
  45. elseif f_x then
  46. created = TypedSection.create(self, section)
  47. self.map:set(created, "target", "ACCEPT")
  48. self.map:set(created, "src", f_s)
  49. self.map:set(created, "dest", f_d)
  50. self.map:set(created, "name", f_n)
  51. end
  52. if created then
  53. m.uci:save("firewall")
  54. luci.http.redirect(ds.build_url(
  55. "admin/network/firewall/rules", created
  56. ))
  57. end
  58. end
  59. ft.opt_name(s, DummyValue, translate("Name"))
  60. local function rule_proto_txt(self, s)
  61. local f = self.map:get(s, "family")
  62. local p = ft.fmt_proto(self.map:get(s, "proto"),
  63. self.map:get(s, "icmp_type")) or translate("traffic")
  64. if f and f:match("4") then
  65. return "%s-%s" %{ translate("IPv4"), p }
  66. elseif f and f:match("6") then
  67. return "%s-%s" %{ translate("IPv6"), p }
  68. else
  69. return "%s %s" %{ translate("Any"), p }
  70. end
  71. end
  72. local function rule_src_txt(self, s)
  73. local z = ft.fmt_zone(self.map:get(s, "src"), translate("any zone"))
  74. local a = ft.fmt_ip(self.map:get(s, "src_ip"), translate("any host"))
  75. local p = ft.fmt_port(self.map:get(s, "src_port"))
  76. local m = ft.fmt_mac(self.map:get(s, "src_mac"))
  77. if p and m then
  78. return translatef("From %s in %s with source %s and %s", a, z, p, m)
  79. elseif p or m then
  80. return translatef("From %s in %s with source %s", a, z, p or m)
  81. else
  82. return translatef("From %s in %s", a, z)
  83. end
  84. end
  85. local function rule_dest_txt(self, s)
  86. local z = ft.fmt_zone(self.map:get(s, "dest"))
  87. local p = ft.fmt_port(self.map:get(s, "dest_port"))
  88. -- Forward
  89. if z then
  90. local a = ft.fmt_ip(self.map:get(s, "dest_ip"), translate("any host"))
  91. if p then
  92. return translatef("To %s, %s in %s", a, p, z)
  93. else
  94. return translatef("To %s in %s", a, z)
  95. end
  96. -- Input
  97. else
  98. local a = ft.fmt_ip(self.map:get(s, "dest_ip"),
  99. translate("any router IP"))
  100. if p then
  101. return translatef("To %s at %s on <var>this device</var>", a, p)
  102. else
  103. return translatef("To %s on <var>this device</var>", a)
  104. end
  105. end
  106. end
  107. local function snat_dest_txt(self, s)
  108. local z = ft.fmt_zone(self.map:get(s, "dest"), translate("any zone"))
  109. local a = ft.fmt_ip(self.map:get(s, "dest_ip"), translate("any host"))
  110. local p = ft.fmt_port(self.map:get(s, "dest_port")) or
  111. ft.fmt_port(self.map:get(s, "src_dport"))
  112. if p then
  113. return translatef("To %s, %s in %s", a, p, z)
  114. else
  115. return translatef("To %s in %s", a, z)
  116. end
  117. end
  118. match = s:option(DummyValue, "match", translate("Match"))
  119. match.rawhtml = true
  120. match.width = "70%"
  121. function match.cfgvalue(self, s)
  122. return "<small>%s<br />%s<br />%s</small>" % {
  123. rule_proto_txt(self, s),
  124. rule_src_txt(self, s),
  125. rule_dest_txt(self, s)
  126. }
  127. end
  128. target = s:option(DummyValue, "target", translate("Action"))
  129. target.rawhtml = true
  130. target.width = "20%"
  131. function target.cfgvalue(self, s)
  132. local t = ft.fmt_target(self.map:get(s, "target"), self.map:get(s, "dest"))
  133. local l = ft.fmt_limit(self.map:get(s, "limit"),
  134. self.map:get(s, "limit_burst"))
  135. if l then
  136. return translatef("<var>%s</var> and limit to %s", t, l)
  137. else
  138. return "<var>%s</var>" % t
  139. end
  140. end
  141. ft.opt_enabled(s, Flag, translate("Enable")).width = "1%"
  142. --
  143. -- SNAT
  144. --
  145. s = m:section(TypedSection, "redirect",
  146. translate("Source NAT"),
  147. translate("Source NAT is a specific form of masquerading which allows \
  148. fine grained control over the source IP used for outgoing traffic, \
  149. for example to map multiple WAN addresses to internal subnets."))
  150. s.template = "cbi/tblsection"
  151. s.addremove = true
  152. s.anonymous = true
  153. s.sortable = true
  154. s.extedit = ds.build_url("admin/network/firewall/rules/%s")
  155. s.template_addremove = "firewall/cbi_addsnat"
  156. function s.create(self, section)
  157. created = TypedSection.create(self, section)
  158. end
  159. function s.parse(self, ...)
  160. TypedSection.parse(self, ...)
  161. local n = m:formvalue("_newsnat.name")
  162. local s = m:formvalue("_newsnat.src")
  163. local d = m:formvalue("_newsnat.dest")
  164. local a = m:formvalue("_newsnat.dip")
  165. local p = m:formvalue("_newsnat.dport")
  166. local x = m:formvalue("_newsnat.submit")
  167. if x and a and #a > 0 then
  168. created = TypedSection.create(self, section)
  169. self.map:set(created, "target", "SNAT")
  170. self.map:set(created, "src", s)
  171. self.map:set(created, "dest", d)
  172. self.map:set(created, "proto", "all")
  173. self.map:set(created, "src_dip", a)
  174. self.map:set(created, "src_dport", p)
  175. self.map:set(created, "name", n)
  176. end
  177. if created then
  178. m.uci:save("firewall")
  179. luci.http.redirect(ds.build_url(
  180. "admin/network/firewall/rules", created
  181. ))
  182. end
  183. end
  184. function s.filter(self, sid)
  185. return (self.map:get(sid, "target") == "SNAT")
  186. end
  187. ft.opt_name(s, DummyValue, translate("Name"))
  188. match = s:option(DummyValue, "match", translate("Match"))
  189. match.rawhtml = true
  190. match.width = "70%"
  191. function match.cfgvalue(self, s)
  192. return "<small>%s<br />%s<br />%s</small>" % {
  193. rule_proto_txt(self, s),
  194. rule_src_txt(self, s),
  195. snat_dest_txt(self, s)
  196. }
  197. end
  198. snat = s:option(DummyValue, "via", translate("Action"))
  199. snat.rawhtml = true
  200. snat.width = "20%"
  201. function snat.cfgvalue(self, s)
  202. local a = ft.fmt_ip(self.map:get(s, "src_dip"))
  203. local p = ft.fmt_port(self.map:get(s, "src_dport"))
  204. if a and p then
  205. return translatef("Rewrite to source %s, %s", a, p)
  206. else
  207. return translatef("Rewrite to source %s", a or p)
  208. end
  209. end
  210. ft.opt_enabled(s, Flag, translate("Enable")).width = "1%"
  211. return m