12dd559bc77c840e8866f6b8e65854ce9705c6b2
[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         uci:foreach("wireless", "wifi-device",
38                 function (section)
39                         local ifc = section[".name"]
40                                 entry({"admin", "network", "wireless", ifc},
41                                  true,
42                                  ifc:upper()).i18n = "wifi"
43                 end
44         )
45
46         local page = entry({"admin", "network", "wireless_join"}, call("wifi_join"), nil, 16)
47         page.i18n = "wifi"
48         page.leaf = true
49
50         local page = entry({"admin", "network", "network"}, arcombine(cbi("admin_network/network"), cbi("admin_network/ifaces")), i18n("interfaces", "Schnittstellen"), 10)
51         page.leaf   = true
52         page.subindex = true
53
54         uci:foreach("network", "interface",
55                 function (section)
56                         local ifc = section[".name"]
57                         if ifc ~= "loopback" then
58                                 entry({"admin", "network", "network", ifc},
59                                  true,
60                                  ifc:upper())
61                         end
62                 end
63         )
64
65         local page  = node("admin", "network", "dhcp")
66         page.target = cbi("admin_network/dhcp")
67         page.title  = "DHCP"
68         page.order  = 30
69         page.subindex = true
70
71         entry(
72          {"admin", "network", "dhcp", "leases"},
73          cbi("admin_network/dhcpleases"),
74          i18n("dhcp_leases")
75         )
76
77         local page  = node("admin", "network", "hosts")
78         page.target = cbi("admin_network/hosts")
79         page.title  = i18n("hostnames", "Hostnames")
80         page.order  = 40
81
82         local page  = node("admin", "network", "routes")
83         page.target = cbi("admin_network/routes")
84         page.title  = i18n("a_n_routes_static")
85         page.order  = 50
86
87 end
88
89 function wifi_join()
90         local function param(x)
91                 return luci.http.formvalue(x)
92         end
93
94         local function ptable(x)
95                 x = param(x)
96                 return x and (type(x) ~= "table" and { x } or x) or {}
97         end
98
99         local dev  = param("device")
100         local ssid = param("join")
101
102         if dev and ssid then
103                 local wep     = (tonumber(param("wep")) == 1)
104                 local wpa     = tonumber(param("wpa_version")) or 0
105                 local channel = tonumber(param("channel"))
106                 local mode    = param("mode")
107                 local bssid   = param("bssid")
108
109                 local confirm = (param("confirm") == "1")
110                 local cancel  = param("cancel") and true or false
111
112                 if confirm and not cancel then
113                         local fixed_bssid = (param("fixed_bssid") == "1")
114                         local replace_net = (param("replace_net") == "1")
115                         local autoconnect = (param("autoconnect") == "1")
116                         local attach_intf = param("attach_intf")
117
118                         local uci = require "luci.model.uci".cursor()
119
120                         if replace_net then
121                                 uci:delete_all("wireless", "wifi-iface")
122                         end
123
124                         local wificonf = {
125                                 device  = dev,
126                                 mode    = (mode == "Ad-Hoc" and "adhoc" or "sta"),
127                                 ssid    = ssid
128                         }
129
130                         if attach_intf and uci:get("network", attach_intf, "ifname") then
131                                 -- target network already has a interface, make it a bridge
132                                 uci:set("network", attach_intf, "type", "bridge")
133                                 uci:save("network")
134                                 uci:commit("network")
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                         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", dev))
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