eb2e6a244c1bafeb6020888c2b9ebb9cb0598d00
[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("a_n_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.i18n   = "wifi"
34         page.leaf = true
35         page.subindex = true
36
37         local page = entry({"admin", "network", "wireless_join"}, call("wifi_join"), nil, 16)
38         page.i18n = "wifi"
39         page.leaf = true
40
41         local page = entry({"admin", "network", "wireless_delete"}, call("wifi_delete"), nil, 16)
42         page.i18n = "wifi"
43         page.leaf = true
44
45         local page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), i18n("interfaces", "Schnittstellen"), 10)
46         page.leaf   = true
47         page.subindex = true
48
49         local page = entry({"admin", "network", "add"}, cbi("admin_network/iface_add"), nil)
50         page.leaf = true
51
52         uci:foreach("network", "interface",
53                 function (section)
54                         local ifc = section[".name"]
55                         if ifc ~= "loopback" then
56                                 entry({"admin", "network", "network", ifc},
57                                  true,
58                                  ifc:upper())
59                         end
60                 end
61         )
62
63         local page  = node("admin", "network", "dhcp")
64         page.target = cbi("admin_network/dhcp")
65         page.title  = "DHCP"
66         page.order  = 30
67         page.subindex = true
68
69         entry(
70          {"admin", "network", "dhcp", "leases"},
71          cbi("admin_network/dhcpleases"),
72          i18n("dhcp_leases")
73         )
74
75         local page  = node("admin", "network", "hosts")
76         page.target = cbi("admin_network/hosts")
77         page.title  = i18n("hostnames", "Hostnames")
78         page.order  = 40
79
80         local page  = node("admin", "network", "routes")
81         page.target = cbi("admin_network/routes")
82         page.title  = i18n("a_n_routes_static")
83         page.order  = 50
84
85 end
86
87 function wifi_join()
88         local function param(x)
89                 return luci.http.formvalue(x)
90         end
91
92         local function ptable(x)
93                 x = param(x)
94                 return x and (type(x) ~= "table" and { x } or x) or {}
95         end
96
97         local dev  = param("device")
98         local ssid = param("join")
99
100         if dev and ssid then
101                 local wep     = (tonumber(param("wep")) == 1)
102                 local wpa     = tonumber(param("wpa_version")) or 0
103                 local channel = tonumber(param("channel"))
104                 local mode    = param("mode")
105                 local bssid   = param("bssid")
106
107                 local confirm = (param("confirm") == "1")
108                 local cancel  = param("cancel") and true or false
109
110                 if confirm and not cancel then
111                         local fixed_bssid = (param("fixed_bssid") == "1")
112                         local replace_net = (param("replace_net") == "1")
113                         local autoconnect = (param("autoconnect") == "1")
114                         local attach_intf = param("attach_intf")
115
116                         local uci = require "luci.model.uci".cursor()
117
118                         if replace_net then
119                                 uci:delete_all("wireless", "wifi-iface")
120                         end
121
122                         local wificonf = {
123                                 device  = dev,
124                                 mode    = (mode == "Ad-Hoc" and "adhoc" or "sta"),
125                                 ssid    = ssid
126                         }
127
128                         if attach_intf and uci:get("network", attach_intf) == "interface" then
129                                 -- target network already has a interface, make it a bridge
130                                 uci:set("network", attach_intf, "type", "bridge")
131                                 uci:save("network")
132                                 uci:commit("network")
133
134                                 wificonf.network = attach_intf
135
136                                 if autoconnect then
137                                         require "luci.sys".call("/sbin/ifup " .. attach_intf)
138                                 end
139                         end
140
141                         if fixed_bssid then
142                                 wificonf.bssid = bssid
143                         end
144
145                         if wep then
146                                 wificonf.encryption = "wep"
147                                 wificonf.key = param("key")
148                         elseif wpa > 0 then
149                                 wificonf.encryption = param("wpa_suite")
150                                 wificonf.key = param("key")
151                         end
152
153                         local s = uci:section("wireless", "wifi-iface", nil, wificonf)
154                         uci:delete("wireless", dev, "disabled")
155                         uci:set("wireless", dev, "channel", channel)
156
157                         uci:save("wireless")
158                         uci:commit("wireless")
159
160                         if autoconnect then
161                                 require "luci.sys".call("/sbin/wifi")
162                         end
163
164                         luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
165                 elseif cancel then
166                         luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
167                 else
168                         luci.template.render("admin_network/wifi_join_settings")
169                 end
170         else
171                 luci.template.render("admin_network/wifi_join")
172         end
173 end
174
175 function wifi_delete(network)
176         local uci = require "luci.model.uci".cursor()
177         local wlm = require "luci.model.wireless"
178
179         wlm.init(uci)
180         wlm:del_network(network)
181
182         uci:save("wireless")
183         luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
184 end