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 net:value("104.61", "Leipzig (104.61)")
40 net:value("104.62", "Halle (104.62)")
41 function net.cfgvalue(self, section)
42 return uci:get("freifunk", "wizard", "net")
44 function net.write(self, section, value)
45 uci:set("freifunk", "wizard", "net", value)
50 subnet = f:field(ListValue, "subnet", "Subnetz (Projekt)")
52 subnet:depends("wifi", "1")
56 function subnet.cfgvalue(self, section)
57 return uci:get("freifunk", "wizard", "subnet")
59 function subnet.write(self, section, value)
60 uci:set("freifunk", "wizard", "subnet", value)
64 node = f:field(Value, "node", "Knoten")
66 node:depends("wifi", "1")
70 function node.cfgvalue(self, section)
71 return uci:get("freifunk", "wizard", "node")
73 function node.write(self, section, value)
74 uci:set("freifunk", "wizard", "node", value)
78 client = f:field(Flag, "client", "WLAN-DHCP anbieten")
79 client:depends("wifi", "1")
82 olsr = f:field(Flag, "olsr", "OLSR einrichten")
84 share = f:field(ListValue, "sharenet", "Eigenen Internetzugang freigeben")
85 share:value("maybe", "-- keine Aktion --")
86 share:value("yes", "einschalten")
87 share:value("no", "ausschalten")
91 -------------------- Control --------------------
92 function f.handle(self, state, data)
93 if state == FORM_VALID then
94 luci.http.redirect(luci.dispatcher.build_url("admin", "uci", "changes"))
96 elseif state == FORM_INVALID then
97 self.errmessage = "Ungültige Eingabe: Bitte die Formularfelder auf Fehler prüfen."
102 local function _strip_internals(tbl)
104 for k, v in pairs(tbl) do
105 if k:sub(1, 1) == "." then
112 -- Configure Freifunk checked
113 function main.write(self, section, value)
118 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 subnet.tag_missing[section] = true
133 node.tag_missing[section] = true
136 if not inet or not isubnet or not inode then
140 local ip = "%s.%s.%s" % {inet, isubnet, inode}
144 luci.util.perror("1")
145 tools.wifi_delete_ifaces(device)
146 luci.util.perror("2")
147 tools.network_remove_interface(device)
148 luci.util.perror("3")
149 tools.firewall_zone_remove_interface("freifunk", device)
153 local devconfig = _strip_internals(uci:get_all("freifunk", "wifi_device"))
154 uci:tset("wireless", device, devconfig)
157 local ifconfig = _strip_internals(uci:get_all("freifunk", "wifi_iface"))
158 ifconfig.device = device
159 ifconfig.network = device
160 uci:section("wireless", "wifi-iface", nil, ifconfig)
165 -- Create firewall zone and add default rules (first time)
166 local newzone = tools.firewall_create_zone("freifunk", "DROP", "ACCEPT", "DROP", true)
168 uci:foreach("freifunk", "fw_forwarding", function(section)
169 uci:section("firewall", "forwarding", nil, _strip_internals(section))
172 uci:foreach("freifunk", "fw_rule", function(section)
173 uci:section("firewall", "rule", nil, _strip_internals(section))
180 -- Crate network interface
181 local netconfig = _strip_internals(uci:get_all("freifunk", "interface"))
182 netconfig.proto = "static"
183 netconfig.ipaddr = ip
184 uci:section("network", "interface", device, netconfig)
188 tools.firewall_zone_add_interface("freifunk", device)
192 function olsr.write(self, section, value)
197 local device = dev:formvalue(section)
199 -- Delete old interface
200 uci:delete_all("freifunk", "Interface", {Interface=device})
202 -- Write new interface
203 local olsrbase = _strip_internals(uci:get_all("freifunk", "olsr_interface"))
204 olsrbase.Interface = device
205 uci:section("olsr", "Interface", nil, olsrbase)
210 function share.write(self, section, value)
211 if value == "maybe" then
215 uci:delete_all("firewall", "forwarding", {src="freifunk", dest="wan"})
217 if value == "yes" then
218 uci:section("firewall", "forwarding", nil, {src="freifunk", dest="wan"})
224 function client.write(self, section, value)
229 local device = dev:formvalue(section)
231 -- Collect IP-Address
232 local inet = net:formvalue(section)
233 local isubnet = subnet:formvalue(section)
234 local inode = node:formvalue(section)
236 if not inet or not isubnet or not inode then
240 local dhcpbeg = 48 + tonumber(inode) * 4
241 local dclient = "%s.%s.%s" % {inet:gsub("^[0-9]+", "10"), isubnet, dhcpbeg}
242 local limit = dhcpbeg < 252 and 3 or 2
245 uci:delete("network", device .. "dhcp")
248 local aliasbase = _strip_internals(uci:get_all("freifunk", "alias"))
249 aliasbase.interface = device
250 aliasbase.ipaddr = dclient
251 aliasbase.proto = "static"
252 uci:section("network", "alias", device .. "dhcp", aliasbase)
257 local dhcpbase = _strip_internals(uci:get_all("freifunk", "dhcp"))
258 dhcpbase.interface = device .. "dhcp"
259 dhcpbase.start = dhcpbeg
260 dhcpbase.limit = limit
262 uci:section("dhcp", "dhcp", device .. "dhcp", dhcpbase)
267 uci:delete_all("luci_splash", "iface", {net=device, zone="freifunk"})
270 uci:section("luci_splash", "iface", nil, {net=device, zone="freifunk"})
271 uci:save("luci_splash")