iface_add.lua 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. -- Copyright 2009-2010 Jo-Philipp Wich <jow@openwrt.org>
  2. -- Licensed to the public under the Apache License 2.0.
  3. local nw = require "luci.model.network".init()
  4. local fw = require "luci.model.firewall".init()
  5. local utl = require "luci.util"
  6. local uci = require "luci.model.uci".cursor()
  7. m = SimpleForm("network", translate("Create Interface"))
  8. m.redirect = luci.dispatcher.build_url("admin/network/network")
  9. m.reset = false
  10. newnet = m:field(Value, "_netname", translate("Name of the new interface"),
  11. translate("The allowed characters are: <code>A-Z</code>, <code>a-z</code>, " ..
  12. "<code>0-9</code> and <code>_</code>"
  13. ))
  14. newnet:depends("_attach", "")
  15. newnet.default = arg[1] and "net_" .. arg[1]:gsub("[^%w_]+", "_")
  16. newnet.datatype = "and(uciname,maxlength(15))"
  17. advice = m:field(DummyValue, "d1", translate("Note: interface name length"),
  18. translate("Maximum length of the name is 15 characters including " ..
  19. "the automatic protocol/bridge prefix (br-, 6in4-, pppoe- etc.)"
  20. ))
  21. newproto = m:field(ListValue, "_netproto", translate("Protocol of the new interface"))
  22. netbridge = m:field(Flag, "_bridge", translate("Create a bridge over multiple interfaces"))
  23. sifname = m:field(Value, "_ifname", translate("Cover the following interface"))
  24. sifname.widget = "radio"
  25. sifname.template = "cbi/network_ifacelist"
  26. sifname.nobridges = true
  27. mifname = m:field(Value, "_ifnames", translate("Cover the following interfaces"))
  28. mifname.widget = "checkbox"
  29. mifname.template = "cbi/network_ifacelist"
  30. mifname.nobridges = true
  31. local _, p
  32. for _, p in ipairs(nw:get_protocols()) do
  33. if p:is_installed() then
  34. newproto:value(p:proto(), p:get_i18n())
  35. if not p:is_virtual() then netbridge:depends("_netproto", p:proto()) end
  36. if not p:is_floating() then
  37. sifname:depends({ _bridge = "", _netproto = p:proto()})
  38. mifname:depends({ _bridge = "1", _netproto = p:proto()})
  39. end
  40. end
  41. end
  42. function newproto.validate(self, value, section)
  43. local name = newnet:formvalue(section)
  44. if not name or #name == 0 then
  45. newnet:add_error(section, translate("No network name specified"))
  46. elseif m:get(name) then
  47. newnet:add_error(section, translate("The given network name is not unique"))
  48. end
  49. local proto = nw:get_protocol(value)
  50. if proto and not proto:is_floating() then
  51. local br = (netbridge:formvalue(section) == "1")
  52. local ifn = br and mifname:formvalue(section) or sifname:formvalue(section)
  53. for ifn in utl.imatch(ifn) do
  54. return value
  55. end
  56. return nil, translate("The selected protocol needs a device assigned")
  57. end
  58. return value
  59. end
  60. function newproto.write(self, section, value)
  61. local name = newnet:formvalue(section)
  62. if name and #name > 0 then
  63. local br = (netbridge:formvalue(section) == "1") and "bridge" or nil
  64. local net = nw:add_network(name, { proto = value, type = br })
  65. if net then
  66. local ifn
  67. for ifn in utl.imatch(
  68. br and mifname:formvalue(section) or sifname:formvalue(section)
  69. ) do
  70. net:add_interface(ifn)
  71. end
  72. nw:save("network")
  73. nw:save("wireless")
  74. end
  75. luci.http.redirect(luci.dispatcher.build_url("admin/network/network", name))
  76. end
  77. end
  78. return m