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")
83 olsr = f:field(Flag, "olsr", "OLSR einrichten")
86 share = f:field(Flag, "sharenet", "Eigenen Internetzugang freigeben")
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)
119 local community, external
121 -- Collect IP-Address
122 local inet = net:formvalue(section)
123 local isubnet = subnet:formvalue(section)
124 local inode = node:formvalue(section)
128 net.tag_missing[section] = true
131 external = uci:get("freifunk", community, "external") or ""
132 inet = uci:get("freifunk", community, "prefix") or inet
135 subnet.tag_missing[section] = true
138 node.tag_missing[section] = true
141 if not inet or not isubnet or not inode then
145 local ip = "%s.%s.%s" % {inet, isubnet, inode}
149 tools.wifi_delete_ifaces(device)
150 tools.network_remove_interface(device)
151 tools.firewall_zone_remove_interface("freifunk", device)
153 -- Tune community settings
154 if community and uci:get("freifunk", community) then
155 uci:tset("freifunk", "community", uci:get_all("freifunk", community))
159 local devconfig = uci:get_all("freifunk", "wifi_device")
160 util.update(devconfig, uci:get_all(external, "wifi_device") or {})
161 uci:tset("wireless", device, devconfig)
164 local ifconfig = uci:get_all("freifunk", "wifi_iface")
165 util.update(ifconfig, uci:get_all(external, "wifi_iface") or {})
166 ifconfig.device = device
167 ifconfig.network = device
168 ifconfig.ssid = uci:get("freifunk", community, "ssid")
169 uci:section("wireless", "wifi-iface", nil, ifconfig)
174 -- Create firewall zone and add default rules (first time)
175 local newzone = tools.firewall_create_zone("freifunk", "REJECT", "ACCEPT", "REJECT", true)
177 uci:foreach("freifunk", "fw_forwarding", function(section)
178 uci:section("firewall", "forwarding", nil, section)
180 uci:foreach(external, "fw_forwarding", function(section)
181 uci:section("firewall", "forwarding", nil, section)
184 uci:foreach("freifunk", "fw_rule", function(section)
185 uci:section("firewall", "rule", nil, section)
187 uci:foreach(external, "fw_rule", function(section)
188 uci:section("firewall", "rule", nil, section)
192 -- Enforce firewall include
193 local has_include = false
194 uci:foreach("firewall", "include",
196 if section.path == "/etc/firewall.freifunk" then
201 if not has_include then
202 uci:section("firewall", "include", nil,
203 { path = "/etc/firewall.freifunk" })
206 -- Allow state: invalid packets
207 uci:foreach("firewall", "defaults",
209 uci:set("firewall", section[".name"], "drop_invalid", "0")
215 -- Crate network interface
216 local netconfig = uci:get_all("freifunk", "interface")
217 util.update(netconfig, uci:get_all(external, "interface") or {})
218 netconfig.proto = "static"
219 netconfig.ipaddr = ip
220 uci:section("network", "interface", device, netconfig)
224 tools.firewall_zone_add_interface("freifunk", device)
228 function olsr.write(self, section, value)
234 local device = dev:formvalue(section)
236 local community = net:formvalue(section)
237 local external = community and uci:get("freifunk", community, "external") or ""
239 -- Delete old interface
240 uci:delete_all("olsrd", "Interface", {interface=device})
242 -- Write new interface
243 local olsrbase = uci:get_all("freifunk", "olsr_interface")
244 util.update(olsrbase, uci:get_all(external, "olsr_interface") or {})
245 olsrbase.interface = device
246 olsrbase.ignore = "0"
247 uci:section("olsrd", "Interface", nil, olsrbase)
252 function share.write(self, section, value)
253 uci:delete_all("firewall", "forwarding", {src="freifunk", dest="wan"})
256 uci:section("firewall", "forwarding", nil, {src="freifunk", dest="wan"})
262 function client.write(self, section, value)
267 local device = dev:formvalue(section)
269 -- Collect IP-Address
270 local inet = net:formvalue(section)
271 local isubnet = subnet:formvalue(section)
272 local inode = node:formvalue(section)
274 if not inet or not isubnet or not inode then
277 local community = inet
278 local external = community and uci:get("freifunk", community, "external") or ""
279 inet = uci:get("freifunk", community, "prefix") or inet
281 local dhcpbeg = 48 + tonumber(inode) * 4
282 local dclient = "%s.%s.%s" % {inet:gsub("^[0-9]+", "10"), isubnet, dhcpbeg}
283 local limit = dhcpbeg < 252 and 3 or 2
286 uci:delete("network", device .. "dhcp")
289 local aliasbase = uci:get_all("freifunk", "alias")
290 util.update(aliasbase, uci:get_all(external, "alias") or {})
291 aliasbase.interface = device
292 aliasbase.ipaddr = dclient
293 aliasbase.proto = "static"
294 uci:section("network", "alias", device .. "dhcp", aliasbase)
299 local dhcpbase = uci:get_all("freifunk", "dhcp")
300 util.update(dhcpbase, uci:get_all(external, "dhcp") or {})
301 dhcpbase.interface = device .. "dhcp"
302 dhcpbase.start = dhcpbeg
303 dhcpbase.limit = limit
305 uci:section("dhcp", "dhcp", device .. "dhcp", dhcpbase)
308 uci:delete_all("firewall", "rule", {
314 uci:section("firewall", "rule", nil, {
321 uci:delete_all("firewall", "rule", {
326 uci:section("firewall", "rule", nil, {
336 uci:delete_all("luci_splash", "iface", {net=device, zone="freifunk"})
339 uci:section("luci_splash", "iface", nil, {net=device, zone="freifunk"})
340 uci:save("luci_splash")