wifi_add.lua 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. -- Copyright 2009 Jo-Philipp Wich <jow@openwrt.org>
  2. -- Licensed to the public under the Apache License 2.0.
  3. local fs = require "nixio.fs"
  4. local nw = require "luci.model.network"
  5. local fw = require "luci.model.firewall"
  6. local uci = require "luci.model.uci".cursor()
  7. local http = require "luci.http"
  8. local iw = luci.sys.wifi.getiwinfo(http.formvalue("device"))
  9. local has_firewall = fs.access("/etc/config/firewall")
  10. if not iw then
  11. luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
  12. return
  13. end
  14. m = SimpleForm("network", translate("Joining Network: %q", http.formvalue("join")))
  15. m.cancel = translate("Back to scan results")
  16. m.reset = false
  17. function m.on_cancel()
  18. local dev = http.formvalue("device")
  19. http.redirect(luci.dispatcher.build_url(
  20. dev and "admin/network/wireless_join?device=" .. dev
  21. or "admin/network/wireless"
  22. ))
  23. end
  24. nw.init(uci)
  25. fw.init(uci)
  26. m.hidden = {
  27. device = http.formvalue("device"),
  28. join = http.formvalue("join"),
  29. channel = http.formvalue("channel"),
  30. mode = http.formvalue("mode"),
  31. bssid = http.formvalue("bssid"),
  32. wep = http.formvalue("wep"),
  33. wpa_suites = http.formvalue("wpa_suites"),
  34. wpa_version = http.formvalue("wpa_version")
  35. }
  36. if iw and iw.mbssid_support then
  37. replace = m:field(Flag, "replace", translate("Replace wireless configuration"),
  38. translate("Check this option to delete the existing networks from this radio."))
  39. function replace.cfgvalue() return "0" end
  40. else
  41. replace = m:field(DummyValue, "replace", translate("Replace wireless configuration"))
  42. replace.default = translate("The hardware is not multi-SSID capable and the existing " ..
  43. "configuration will be replaced if you proceed.")
  44. function replace.formvalue() return "1" end
  45. end
  46. if http.formvalue("wep") == "1" then
  47. key = m:field(Value, "key", translate("WEP passphrase"),
  48. translate("Specify the secret encryption key here."))
  49. key.password = true
  50. key.datatype = "wepkey"
  51. elseif (tonumber(m.hidden.wpa_version) or 0) > 0 and
  52. (m.hidden.wpa_suites == "PSK" or m.hidden.wpa_suites == "PSK2")
  53. then
  54. key = m:field(Value, "key", translate("WPA passphrase"),
  55. translate("Specify the secret encryption key here."))
  56. key.password = true
  57. key.datatype = "wpakey"
  58. --m.hidden.wpa_suite = (tonumber(http.formvalue("wpa_version")) or 0) >= 2 and "psk2" or "psk"
  59. end
  60. newnet = m:field(Value, "_netname_new", translate("Name of the new network"),
  61. translate("The allowed characters are: <code>A-Z</code>, <code>a-z</code>, " ..
  62. "<code>0-9</code> and <code>_</code>"
  63. ))
  64. newnet.default = m.hidden.mode == "Ad-Hoc" and "mesh" or "wwan"
  65. newnet.datatype = "uciname"
  66. if has_firewall then
  67. fwzone = m:field(Value, "_fwzone",
  68. translate("Create / Assign firewall-zone"),
  69. translate("Choose the firewall zone you want to assign to this interface. Select <em>unspecified</em> to remove the interface from the associated zone or fill out the <em>create</em> field to define a new zone and attach the interface to it."))
  70. fwzone.template = "cbi/firewall_zonelist"
  71. fwzone.default = m.hidden.mode == "Ad-Hoc" and "mesh" or "wan"
  72. end
  73. function newnet.parse(self, section)
  74. local net, zone
  75. if has_firewall then
  76. local zval = fwzone:formvalue(section)
  77. zone = fw:get_zone(zval)
  78. if not zone and zval == '-' then
  79. zval = m:formvalue(fwzone:cbid(section) .. ".newzone")
  80. if zval and #zval > 0 then
  81. zone = fw:add_zone(zval)
  82. end
  83. end
  84. end
  85. local wdev = nw:get_wifidev(m.hidden.device)
  86. wdev:set("disabled", false)
  87. wdev:set("channel", m.hidden.channel)
  88. if replace:formvalue(section) then
  89. local n
  90. for _, n in ipairs(wdev:get_wifinets()) do
  91. wdev:del_wifinet(n)
  92. end
  93. end
  94. local wconf = {
  95. device = m.hidden.device,
  96. ssid = m.hidden.join,
  97. mode = (m.hidden.mode == "Ad-Hoc" and "adhoc" or "sta")
  98. }
  99. if m.hidden.wep == "1" then
  100. wconf.encryption = "wep-open"
  101. wconf.key = "1"
  102. wconf.key1 = key and key:formvalue(section) or ""
  103. elseif (tonumber(m.hidden.wpa_version) or 0) > 0 then
  104. wconf.encryption = (tonumber(m.hidden.wpa_version) or 0) >= 2 and "psk2" or "psk"
  105. wconf.key = key and key:formvalue(section) or ""
  106. else
  107. wconf.encryption = "none"
  108. end
  109. if wconf.mode == "adhoc" or wconf.mode == "sta" then
  110. wconf.bssid = m.hidden.bssid
  111. end
  112. local value = self:formvalue(section)
  113. net = nw:add_network(value, { proto = "dhcp" })
  114. if not net then
  115. self.error = { [section] = "missing" }
  116. else
  117. wconf.network = net:name()
  118. local wnet = wdev:add_wifinet(wconf)
  119. if wnet then
  120. if zone then
  121. fw:del_network(net:name())
  122. zone:add_network(net:name())
  123. end
  124. uci:save("wireless")
  125. uci:save("network")
  126. uci:save("firewall")
  127. luci.http.redirect(wnet:adminlink())
  128. end
  129. end
  130. end
  131. if has_firewall then
  132. function fwzone.cfgvalue(self, section)
  133. self.iface = section
  134. local z = fw:get_zone_by_network(section)
  135. return z and z:name()
  136. end
  137. end
  138. return m