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"
20 local util = require "luci.util"
23 -------------------- View --------------------
24 f = SimpleForm("ffwizward", "Freifunkassistent",
25 "Dieser Assistent unterstüzt bei der Einrichtung des Routers für das Freifunknetz.")
28 dev = f:field(ListValue, "device", "WLAN-Gerät")
29 uci:foreach("wireless", "wifi-device",
31 dev:value(section[".name"])
35 main = f:field(Flag, "wifi", "Freifunkzugang einrichten")
37 net = f:field(Value, "net", "Freifunknetz", "1. Teil der IP-Adresse")
39 net:depends("wifi", "1")
40 uci:foreach("freifunk", "community", function(s)
41 net:value(s[".name"], "%s (%s)" % {s.name, s.prefix})
44 function net.cfgvalue(self, section)
45 return uci:get("freifunk", "wizard", "net")
47 function net.write(self, section, value)
48 uci:set("freifunk", "wizard", "net", value)
53 subnet = f:field(Value, "subnet", "Subnetz (Projekt)", "2. Teil der IP-Adresse")
55 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", "3. Teil der IP-Adresse")
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")
81 function client.cfgvalue(self, section)
82 return uci:get("freifunk", "wizard", "client")
84 function client.write(self, section, value)
85 uci:set("freifunk", "wizard", "client", value)
90 olsr = f:field(Flag, "olsr", "OLSR einrichten")
92 share = f:field(Flag, "sharenet", "Eigenen Internetzugang freigeben")
94 function share.cfgvalue(self, section)
95 return uci:get("freifunk", "wizard", "sharenet")
97 function share.write(self, section, value)
98 uci:set("freifunk", "wizard", "sharenet", value)
104 -------------------- Control --------------------
105 function f.handle(self, state, data)
106 if state == FORM_VALID then
107 luci.http.redirect(luci.dispatcher.build_url("admin", "uci", "changes"))
109 elseif state == FORM_INVALID then
110 self.errmessage = "Ungültige Eingabe: Bitte die Formularfelder auf Fehler prüfen."
115 local function _strip_internals(tbl)
117 for k, v in pairs(tbl) do
118 if k:sub(1, 1) == "." then
125 -- Configure Freifunk checked
126 function main.write(self, section, value)
131 local device = dev:formvalue(section)
132 local community, external
134 -- Collect IP-Address
135 local inet = net:formvalue(section)
136 local isubnet = subnet:formvalue(section)
137 local inode = node:formvalue(section)
141 net.tag_missing[section] = true
144 external = uci:get("freifunk", community, "external") or ""
145 inet = uci:get("freifunk", community, "prefix") or inet
148 subnet.tag_missing[section] = true
151 node.tag_missing[section] = true
154 if not inet or not isubnet or not inode then
158 local ip = "%s.%s.%s" % {inet, isubnet, inode}
162 tools.wifi_delete_ifaces(device)
163 tools.network_remove_interface(device)
164 tools.firewall_zone_remove_interface("freifunk", device)
166 -- Tune community settings
168 uci:tset("freifunk", "community", uci:get_all("freifunk", community))
172 local devconfig = uci:get_all("freifunk", "wifi_device")
173 util.update(devconfig, uci:get_all(external, "wifi_device") or {})
174 uci:tset("wireless", device, devconfig)
177 local ifconfig = uci:get_all("freifunk", "wifi_iface")
178 util.update(ifconfig, uci:get_all(external, "wifi_iface") or {})
179 ifconfig.device = device
180 ifconfig.network = device
181 ifconfig.ssid = uci:get("freifunk", community, "ssid")
182 uci:section("wireless", "wifi-iface", nil, ifconfig)
187 -- Create firewall zone and add default rules (first time)
188 local newzone = tools.firewall_create_zone("freifunk", "DROP", "ACCEPT", "DROP", true)
190 uci:foreach("freifunk", "fw_forwarding", function(section)
191 uci:section("firewall", "forwarding", nil, section)
193 uci:foreach(external, "fw_forwarding", function(section)
194 uci:section("firewall", "forwarding", nil, section)
197 uci:foreach("freifunk", "fw_rule", function(section)
198 uci:section("firewall", "rule", nil, section)
200 uci:foreach(external, "fw_rule", function(section)
201 uci:section("firewall", "rule", nil, section)
208 -- Crate network interface
209 local netconfig = uci:get_all("freifunk", "interface")
210 util.update(netconfig, uci:get_all(external, "interface") or {})
211 netconfig.proto = "static"
212 netconfig.ipaddr = ip
213 uci:section("network", "interface", device, netconfig)
217 tools.firewall_zone_add_interface("freifunk", device)
221 function olsr.write(self, section, value)
227 local device = dev:formvalue(section)
229 local community = net:formvalue(section)
230 local external = community and uci:get("freifunk", community, "external") or ""
232 -- Delete old interface
233 uci:delete_all("olsrd", "Interface", {interface=device})
235 -- Write new interface
236 local olsrbase = uci:get_all("freifunk", "olsr_interface")
237 util.update(olsrbase, uci:get_all(external, "olsr_interface") or {})
238 olsrbase.interface = device
239 olsrbase.ignore = "0"
240 uci:section("olsrd", "Interface", nil, olsrbase)
245 function share.write(self, section, value)
246 uci:delete_all("firewall", "forwarding", {src="freifunk", dest="wan"})
249 uci:section("firewall", "forwarding", nil, {src="freifunk", dest="wan"})
255 function client.write(self, section, value)
260 local device = dev:formvalue(section)
262 -- Collect IP-Address
263 local inet = net:formvalue(section)
264 local isubnet = subnet:formvalue(section)
265 local inode = node:formvalue(section)
267 if not inet or not isubnet or not inode then
270 local community = inet
271 local external = community and uci:get("freifunk", community, "external") or ""
272 inet = uci:get("freifunk", community, "prefix") or inet
274 local dhcpbeg = 48 + tonumber(inode) * 4
275 local dclient = "%s.%s.%s" % {inet:gsub("^[0-9]+", "10"), isubnet, dhcpbeg}
276 local limit = dhcpbeg < 252 and 3 or 2
279 uci:delete("network", device .. "dhcp")
282 local aliasbase = uci:get_all("freifunk", "alias")
283 util.update(aliasbase, uci:get_all(external, "alias") or {})
284 aliasbase.interface = device
285 aliasbase.ipaddr = dclient
286 aliasbase.proto = "static"
287 uci:section("network", "alias", device .. "dhcp", aliasbase)
292 local dhcpbase = uci:get_all("freifunk", "dhcp")
293 util.update(dhcpbase, uci:get_all(external, "dhcp") or {})
294 dhcpbase.interface = device .. "dhcp"
295 dhcpbase.start = dhcpbeg
296 dhcpbase.limit = limit
298 uci:section("dhcp", "dhcp", device .. "dhcp", dhcpbase)
301 uci:delete_all("firewall", "rule", {
307 uci:section("firewall", "rule", nil, {
318 uci:delete_all("luci_splash", "iface", {net=device, zone="freifunk"})
321 uci:section("luci_splash", "iface", nil, {net=device, zone="freifunk"})
322 uci:save("luci_splash")