2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
18 local uci = require "luci.model.uci".cursor()
19 local tools = require "luci.tools.ffwizard"
22 -------------------- View --------------------
23 f = SimpleForm("ffwizward", "Freifunkassistent",
24 "Dieser Assistent unterstüzt bei der Einrichtung des Routers für das Freifunknetz.")
27 dev = f:field(ListValue, "device", "WLAN-Gerät")
28 uci:foreach("wireless", "wifi-device",
30 dev:value(section[".name"])
34 main = f:field(Flag, "wifi", "Freifunkzugang einrichten")
36 net = f:field(Value, "net", "Freifunknetz")
38 net:depends("wifi", "1")
39 uci:foreach("freifunk", "community", function(s)
40 net:value(s[".name"], s.name)
43 function net.cfgvalue(self, section)
44 return uci:get("freifunk", "wizard", "net")
46 function net.write(self, section, value)
47 uci:set("freifunk", "wizard", "net", value)
52 subnet = f:field(Value, "subnet", "Subnetz (Projekt)")
54 subnet:depends("wifi", "1")
55 function subnet.cfgvalue(self, section)
56 return uci:get("freifunk", "wizard", "subnet")
58 function subnet.write(self, section, value)
59 uci:set("freifunk", "wizard", "subnet", value)
63 node = f:field(Value, "node", "Knoten")
65 node:depends("wifi", "1")
69 function node.cfgvalue(self, section)
70 return uci:get("freifunk", "wizard", "node")
72 function node.write(self, section, value)
73 uci:set("freifunk", "wizard", "node", value)
77 client = f:field(Flag, "client", "WLAN-DHCP anbieten")
78 client:depends("wifi", "1")
81 olsr = f:field(Flag, "olsr", "OLSR einrichten")
83 share = f:field(ListValue, "sharenet", "Eigenen Internetzugang freigeben")
84 share:value("maybe", "-- keine Aktion --")
85 share:value("yes", "einschalten")
86 share:value("no", "ausschalten")
90 -------------------- Control --------------------
91 function f.handle(self, state, data)
92 if state == FORM_VALID then
93 luci.http.redirect(luci.dispatcher.build_url("admin", "uci", "changes"))
95 elseif state == FORM_INVALID then
96 self.errmessage = "Ungültige Eingabe: Bitte die Formularfelder auf Fehler prüfen."
101 local function _strip_internals(tbl)
103 for k, v in pairs(tbl) do
104 if k:sub(1, 1) == "." then
111 -- Configure Freifunk checked
112 function main.write(self, section, value)
117 local device = dev:formvalue(section)
120 -- Collect IP-Address
121 local inet = net:formvalue(section)
122 local isubnet = subnet:formvalue(section)
123 local inode = node:formvalue(section)
127 net.tag_missing[section] = true
130 inet = uci:get("freifunk", community, "prefix") or inet
133 subnet.tag_missing[section] = true
136 node.tag_missing[section] = true
139 if not inet or not isubnet or not inode then
143 local ip = "%s.%s.%s" % {inet, isubnet, inode}
147 tools.wifi_delete_ifaces(device)
148 tools.network_remove_interface(device)
149 tools.firewall_zone_remove_interface("freifunk", device)
151 -- Tune community settings
153 uci:tset("freifunk", "community", uci:get_all("freifunk", community))
157 local devconfig = _strip_internals(uci:get_all("freifunk", "wifi_device"))
158 uci:tset("wireless", device, devconfig)
161 local ifconfig = _strip_internals(uci:get_all("freifunk", "wifi_iface"))
162 ifconfig.device = device
163 ifconfig.network = device
164 ifconfig.ssid = uci:get("freifunk", community, "ssid")
165 uci:section("wireless", "wifi-iface", nil, ifconfig)
170 -- Create firewall zone and add default rules (first time)
171 local newzone = tools.firewall_create_zone("freifunk", "DROP", "ACCEPT", "DROP", true)
173 uci:foreach("freifunk", "fw_forwarding", function(section)
174 uci:section("firewall", "forwarding", nil, _strip_internals(section))
177 uci:foreach("freifunk", "fw_rule", function(section)
178 uci:section("firewall", "rule", nil, _strip_internals(section))
185 -- Crate network interface
186 local netconfig = _strip_internals(uci:get_all("freifunk", "interface"))
187 netconfig.proto = "static"
188 netconfig.ipaddr = ip
189 uci:section("network", "interface", device, netconfig)
193 tools.firewall_zone_add_interface("freifunk", device)
197 function olsr.write(self, section, value)
202 local device = dev:formvalue(section)
204 -- Delete old interface
205 uci:delete_all("freifunk", "Interface", {Interface=device})
207 -- Write new interface
208 local olsrbase = _strip_internals(uci:get_all("freifunk", "olsr_interface"))
209 olsrbase.interface = device
210 olsrbase.ignore = "0"
211 uci:section("olsrd", "Interface", nil, olsrbase)
216 function share.write(self, section, value)
217 if value == "maybe" then
221 uci:delete_all("firewall", "forwarding", {src="freifunk", dest="wan"})
223 if value == "yes" then
224 uci:section("firewall", "forwarding", nil, {src="freifunk", dest="wan"})
230 function client.write(self, section, value)
235 local device = dev:formvalue(section)
237 -- Collect IP-Address
238 local inet = net:formvalue(section)
239 local isubnet = subnet:formvalue(section)
240 local inode = node:formvalue(section)
242 if not inet or not isubnet or not inode then
246 inet = uci:get("freifunk", inet, "prefix") or inet
248 local dhcpbeg = 48 + tonumber(inode) * 4
249 local dclient = "%s.%s.%s" % {inet:gsub("^[0-9]+", "10"), isubnet, dhcpbeg}
250 local limit = dhcpbeg < 252 and 3 or 2
253 uci:delete("network", device .. "dhcp")
256 local aliasbase = _strip_internals(uci:get_all("freifunk", "alias"))
257 aliasbase.interface = device
258 aliasbase.ipaddr = dclient
259 aliasbase.proto = "static"
260 uci:section("network", "alias", device .. "dhcp", aliasbase)
265 local dhcpbase = _strip_internals(uci:get_all("freifunk", "dhcp"))
266 dhcpbase.interface = device .. "dhcp"
267 dhcpbase.start = dhcpbeg
268 dhcpbase.limit = limit
270 uci:section("dhcp", "dhcp", device .. "dhcp", dhcpbase)
273 uci:delete_all("firewall", "rule", {
279 uci:section("firewall", "rule", nil, {
290 uci:delete_all("luci_splash", "iface", {net=device, zone="freifunk"})
293 uci:section("luci_splash", "iface", nil, {net=device, zone="freifunk"})
294 uci:save("luci_splash")