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"
21 local sys = require "luci.sys"
24 -------------------- View --------------------
25 f = SimpleForm("ffwizward", "Freifunkassistent",
26 "Dieser Assistent unterstüzt bei der Einrichtung des Routers für das Freifunknetz.")
29 dev = f:field(ListValue, "device", "WLAN-Gerät")
30 uci:foreach("wireless", "wifi-device",
32 dev:value(section[".name"])
36 main = f:field(Flag, "wifi", "Freifunkzugang einrichten")
38 net = f:field(Value, "net", "Freifunknetz", "1. Teil der IP-Adresse")
40 net:depends("wifi", "1")
41 uci:foreach("freifunk", "community", function(s)
42 net:value(s[".name"], "%s (%s)" % {s.name, s.prefix})
45 function net.cfgvalue(self, section)
46 return uci:get("freifunk", "wizard", "net")
48 function net.write(self, section, value)
49 uci:set("freifunk", "wizard", "net", value)
54 subnet = f:field(Value, "subnet", "Subnetz (Projekt)", "2. Teil der IP-Adresse")
56 subnet:depends("wifi", "1")
57 function subnet.cfgvalue(self, section)
58 return uci:get("freifunk", "wizard", "subnet")
60 function subnet.write(self, section, value)
61 uci:set("freifunk", "wizard", "subnet", value)
65 node = f:field(Value, "node", "Knoten", "3. Teil der IP-Adresse")
67 node:depends("wifi", "1")
71 function node.cfgvalue(self, section)
72 return uci:get("freifunk", "wizard", "node")
74 function node.write(self, section, value)
75 uci:set("freifunk", "wizard", "node", value)
79 client = f:field(Flag, "client", "WLAN-DHCP anbieten")
80 client:depends("wifi", "1")
84 olsr = f:field(Flag, "olsr", "OLSR einrichten")
87 share = f:field(Flag, "sharenet", "Eigenen Internetzugang freigeben")
92 -------------------- Control --------------------
93 function f.handle(self, state, data)
94 if state == FORM_VALID then
95 luci.http.redirect(luci.dispatcher.build_url("admin", "uci", "changes"))
97 elseif state == FORM_INVALID then
98 self.errmessage = "Ungültige Eingabe: Bitte die Formularfelder auf Fehler prüfen."
103 local function _strip_internals(tbl)
105 for k, v in pairs(tbl) do
106 if k:sub(1, 1) == "." then
113 -- Configure Freifunk checked
114 function main.write(self, section, value)
119 local device = dev:formvalue(section)
120 local community, external
122 -- Collect IP-Address
123 local inet = net:formvalue(section)
124 local isubnet = subnet:formvalue(section)
125 local inode = node:formvalue(section)
129 net.tag_missing[section] = true
132 external = uci:get("freifunk", community, "external") or ""
133 inet = uci:get("freifunk", community, "prefix") or inet
136 subnet.tag_missing[section] = true
139 node.tag_missing[section] = true
142 if not inet or not isubnet or not inode then
146 local ip = "%s.%s.%s" % {inet, isubnet, inode}
150 tools.wifi_delete_ifaces(device)
151 tools.network_remove_interface(device)
152 tools.firewall_zone_remove_interface("freifunk", device)
154 -- Tune community settings
155 if community and uci:get("freifunk", community) then
156 uci:tset("freifunk", "community", uci:get_all("freifunk", community))
160 local devconfig = uci:get_all("freifunk", "wifi_device")
161 util.update(devconfig, uci:get_all(external, "wifi_device") or {})
162 uci:tset("wireless", device, devconfig)
165 local ifconfig = uci:get_all("freifunk", "wifi_iface")
166 util.update(ifconfig, uci:get_all(external, "wifi_iface") or {})
167 ifconfig.device = device
168 ifconfig.network = device
169 ifconfig.ssid = uci:get("freifunk", community, "ssid")
170 uci:section("wireless", "wifi-iface", nil, ifconfig)
175 -- Create firewall zone and add default rules (first time)
176 local newzone = tools.firewall_create_zone("freifunk", "REJECT", "ACCEPT", "REJECT", true)
178 uci:foreach("freifunk", "fw_forwarding", function(section)
179 uci:section("firewall", "forwarding", nil, section)
181 uci:foreach(external, "fw_forwarding", function(section)
182 uci:section("firewall", "forwarding", nil, section)
185 uci:foreach("freifunk", "fw_rule", function(section)
186 uci:section("firewall", "rule", nil, section)
188 uci:foreach(external, "fw_rule", function(section)
189 uci:section("firewall", "rule", nil, section)
193 -- Enforce firewall include
194 local has_include = false
195 uci:foreach("firewall", "include",
197 if section.path == "/etc/firewall.freifunk" then
202 if not has_include then
203 uci:section("firewall", "include", nil,
204 { path = "/etc/firewall.freifunk" })
207 -- Allow state: invalid packets
208 uci:foreach("firewall", "defaults",
210 uci:set("firewall", section[".name"], "drop_invalid", "0")
213 -- Prepare advanced config
214 local has_advanced = false
215 uci:foreach("firewall", "advanced",
216 function(section) has_advanced = true end)
218 if not has_advanced then
219 uci:section("firewall", "advanced", nil,
226 -- Create network interface
227 local netconfig = uci:get_all("freifunk", "interface")
228 util.update(netconfig, uci:get_all(external, "interface") or {})
229 netconfig.proto = "static"
230 netconfig.ipaddr = ip
231 uci:section("network", "interface", device, netconfig)
235 tools.firewall_zone_add_interface("freifunk", device)
239 local new_hostname = ip:gsub("%.", "-")
240 local old_hostname = sys.hostname()
242 if old_hostname == "OpenWrt" or old_hostname:match("^%d+-%d+-%d+-%d+$") then
243 uci:foreach("system", "system",
245 uci:set("system", s['.name'], "hostname", new_hostname)
248 sys.hostname(new_hostname)
254 function olsr.write(self, section, value)
260 local device = dev:formvalue(section)
262 local community = net:formvalue(section)
263 local external = community and uci:get("freifunk", community, "external") or ""
265 -- Configure nameservice
267 uci:foreach("system", "system", function(s) hostname = s.hostname end)
270 uci:foreach("olsrd", "LoadPlugin",
272 if s.library == "olsrd_nameservice.so.0.3" then
273 uci:set("olsrd", s['.name'], "name", hostname)
278 -- Delete old interface
279 uci:delete_all("olsrd", "Interface", {interface=device})
281 -- Write new interface
282 local olsrbase = uci:get_all("freifunk", "olsr_interface")
283 util.update(olsrbase, uci:get_all(external, "olsr_interface") or {})
284 olsrbase.interface = device
285 olsrbase.ignore = "0"
286 uci:section("olsrd", "Interface", nil, olsrbase)
290 uci:foreach("dhcp", "dnsmasq", function(s)
291 uci:set("dhcp", s[".name"], "addnhosts", "/var/etc/hosts.olsr")
297 function share.write(self, section, value)
298 uci:delete_all("firewall", "forwarding", {src="freifunk", dest="wan"})
299 uci:delete_all("olsrd", "LoadPlugin", {library="olsrd_dyn_gw_plain.so.0.4"})
302 uci:section("firewall", "forwarding", nil, {src="freifunk", dest="wan"})
303 uci:section("olsrd", "LoadPlugin", nil, {library="olsrd_dyn_gw_plain.so.0.4"})
310 function client.write(self, section, value)
315 local device = dev:formvalue(section)
317 -- Collect IP-Address
318 local inet = net:formvalue(section)
319 local isubnet = subnet:formvalue(section)
320 local inode = node:formvalue(section)
322 if not inet or not isubnet or not inode then
325 local community = inet
326 local external = community and uci:get("freifunk", community, "external") or ""
327 inet = uci:get("freifunk", community, "prefix") or inet
329 local dhcpbeg = 48 + tonumber(inode) * 4
330 local dclient = "%s.%s.%s" % {inet:gsub("^[0-9]+", "10"), isubnet, dhcpbeg}
331 local limit = dhcpbeg < 252 and 3 or 2
334 uci:delete("network", device .. "dhcp")
337 local aliasbase = uci:get_all("freifunk", "alias")
338 util.update(aliasbase, uci:get_all(external, "alias") or {})
339 aliasbase.interface = device
340 aliasbase.ipaddr = dclient
341 aliasbase.proto = "static"
342 uci:section("network", "alias", device .. "dhcp", aliasbase)
347 local dhcpbase = uci:get_all("freifunk", "dhcp")
348 util.update(dhcpbase, uci:get_all(external, "dhcp") or {})
349 dhcpbase.interface = device .. "dhcp"
350 dhcpbase.start = dhcpbeg
351 dhcpbase.limit = limit
353 uci:section("dhcp", "dhcp", device .. "dhcp", dhcpbase)
356 uci:delete_all("firewall", "rule", {
361 uci:section("firewall", "rule", nil, {
367 uci:delete_all("firewall", "rule", {
373 uci:section("firewall", "rule", nil, {
380 uci:delete_all("firewall", "rule", {
385 uci:section("firewall", "rule", nil, {
395 uci:delete_all("luci_splash", "iface", {net=device, zone="freifunk"})
398 uci:section("luci_splash", "iface", nil, {net=device, zone="freifunk"})
399 uci:save("luci_splash")