a5cc2c28498287ceacee90d9f32531b3a27c20af
[project/luci.git] / modules / admin-full / luasrc / controller / admin / network.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14 module("luci.controller.admin.network", package.seeall)
15
16 function index()
17         require("luci.i18n")
18         local uci = require("luci.model.uci").cursor()
19         local i18n = luci.i18n.translate
20
21         local page  = node("admin", "network")
22         page.target = alias("admin", "network", "network")
23         page.title  = i18n("Network")
24         page.order  = 50
25         page.index  = true
26
27         local page  = node("admin", "network", "vlan")
28         page.target = cbi("admin_network/vlan")
29         page.title  = i18n("Switch")
30         page.order  = 20
31
32         local page = entry({"admin", "network", "wireless"}, arcombine(template("admin_network/wifi_overview"), cbi("admin_network/wifi")), i18n("Wifi"), 15)
33         page.leaf = true
34         page.subindex = true
35
36         local page = entry({"admin", "network", "wireless_join"}, call("wifi_join"), nil, 16)
37         page.leaf = true
38
39         local page = entry({"admin", "network", "wireless_delete"}, call("wifi_delete"), nil, 16)
40         page.leaf = true
41
42         local page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), i18n("Interfaces"), 10)
43         page.leaf   = true
44         page.subindex = true
45
46         local page = entry({"admin", "network", "add"}, cbi("admin_network/iface_add"), nil)
47         page.leaf = true
48
49         uci:foreach("network", "interface",
50                 function (section)
51                         local ifc = section[".name"]
52                         if ifc ~= "loopback" then
53                                 entry({"admin", "network", "network", ifc},
54                                  true,
55                                  ifc:upper())
56                         end
57                 end
58         )
59
60         local page  = node("admin", "network", "dhcp")
61         page.target = cbi("admin_network/dhcp")
62         page.title  = "DHCP"
63         page.order  = 30
64         page.subindex = true
65
66         entry(
67          {"admin", "network", "dhcp", "leases"},
68          cbi("admin_network/dhcpleases"),
69          i18n("Leases")
70         )
71
72         local page  = node("admin", "network", "hosts")
73         page.target = cbi("admin_network/hosts")
74         page.title  = i18n("Hostnames")
75         page.order  = 40
76
77         local page  = node("admin", "network", "routes")
78         page.target = cbi("admin_network/routes")
79         page.title  = i18n("Static Routes")
80         page.order  = 50
81
82 end
83
84 function wifi_join()
85         local function param(x)
86                 return luci.http.formvalue(x)
87         end
88
89         local function ptable(x)
90                 x = param(x)
91                 return x and (type(x) ~= "table" and { x } or x) or {}
92         end
93
94         local dev  = param("device")
95         local ssid = param("join")
96
97         if dev and ssid then
98                 local wep     = (tonumber(param("wep")) == 1)
99                 local wpa     = tonumber(param("wpa_version")) or 0
100                 local channel = tonumber(param("channel"))
101                 local mode    = param("mode")
102                 local bssid   = param("bssid")
103
104                 local confirm = (param("confirm") == "1")
105                 local cancel  = param("cancel") and true or false
106
107                 if confirm and not cancel then
108                         local fixed_bssid = (param("fixed_bssid") == "1")
109                         local replace_net = (param("replace_net") == "1")
110                         local autoconnect = (param("autoconnect") == "1")
111                         local attach_intf = param("attach_intf")
112
113                         local uci = require "luci.model.uci".cursor()
114
115                         if replace_net then
116                                 uci:delete_all("wireless", "wifi-iface")
117                         end
118
119                         local wificonf = {
120                                 device  = dev,
121                                 mode    = (mode == "Ad-Hoc" and "adhoc" or "sta"),
122                                 ssid    = ssid
123                         }
124
125                         if attach_intf and uci:get("network", attach_intf) == "interface" then
126                                 -- target network already has a interface, make it a bridge
127                                 uci:set("network", attach_intf, "type", "bridge")
128                                 uci:save("network")
129                                 uci:commit("network")
130
131                                 wificonf.network = attach_intf
132
133                                 if autoconnect then
134                                         require "luci.sys".call("/sbin/ifup " .. attach_intf)
135                                 end
136                         end
137
138                         if fixed_bssid then
139                                 wificonf.bssid = bssid
140                         end
141
142                         if wep then
143                                 wificonf.encryption = "wep"
144                                 wificonf.key = param("key")
145                         elseif wpa > 0 then
146                                 wificonf.encryption = param("wpa_suite")
147                                 wificonf.key = param("key")
148                         end
149
150                         local s = uci:section("wireless", "wifi-iface", nil, wificonf)
151                         uci:delete("wireless", dev, "disabled")
152                         uci:set("wireless", dev, "channel", channel)
153
154                         uci:save("wireless")
155                         uci:commit("wireless")
156
157                         if autoconnect then
158                                 require "luci.sys".call("/sbin/wifi")
159                         end
160
161                         luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
162                 elseif cancel then
163                         luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
164                 else
165                         luci.template.render("admin_network/wifi_join_settings")
166                 end
167         else
168                 luci.template.render("admin_network/wifi_join")
169         end
170 end
171
172 function wifi_delete(network)
173         local uci = require "luci.model.uci".cursor()
174         local wlm = require "luci.model.wireless"
175
176         wlm.init(uci)
177         wlm:del_network(network)
178
179         uci:save("wireless")
180         luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
181 end