Merge branch 'menu'
[project/luci.git] / modules / freifunk / src / controller / freifunk / freifunk.lua
1 module("ffluci.controller.freifunk.freifunk", package.seeall)
2
3 function index()
4         local page  = node()
5         page.target = alias("freifunk")
6
7         local page    = node("freifunk")
8         page.title    = "Freifunk"
9         page.target   = alias("freifunk", "index")
10         page.order    = 5
11         page.setuser  = "nobody"
12         page.setgroup = "nogroup"
13         
14         local page  = node("freifunk", "index")
15         page.target = template("freifunk/index")
16         page.title  = "Übersicht"
17         page.order  = 10
18         
19         local page  = node("freifunk", "index", "contact")
20         page.target = template("freifunk/contact")
21         page.title  = "Kontakt"
22         
23         
24         local page  = node("freifunk", "status")
25         page.target = action_status
26         page.title  = "Status"
27         page.order  = 20
28         page.setuser  = false
29         page.setgroup = false
30
31         local page  = node("freifunk", "status", "routes")
32         page.target = template("public_status/routes")
33         page.title  = "Routingtabelle"
34         page.order  = 10
35         
36         local page  = node("freifunk", "status", "iwscan")
37         page.target = template("public_status/iwscan")
38         page.title  = "WLAN-Scan"
39         page.order  = 20        
40
41
42         local page  = node("admin", "index", "wizard")
43         page.target = action_wizard
44         page.title  = "Freifunkassistent"
45         page.order  = 20
46         
47         local page  = node("admin", "index", "freifunk")
48         page.target = cbi("freifunk/freifunk")
49         page.title  = "Freifunk"
50         page.order  = 30
51         
52         local page  = node("admin", "index", "contact")
53         page.target = cbi("freifunk/contact")
54         page.title  = "Kontakt"
55         page.order  = 40
56 end
57
58 function action_status()
59         local data = {}
60         
61         data.s, data.m, data.r = ffluci.sys.sysinfo()
62         
63         data.wifi = ffluci.sys.wifi.getiwconfig()
64         
65         data.routes = {}
66         for i, r in pairs(ffluci.sys.net.routes()) do
67                 if r.Destination == "00000000" then
68                         table.insert(data.routes, r)
69                 end
70         end
71
72         
73         ffluci.template.render("public_status/index", data)
74 end
75
76 function action_wizard()
77         if ffluci.http.formvalue("ip") then
78                 return configure_freifunk()
79         end
80         
81         local ifaces = {}
82         local wldevs = ffluci.model.uci.sections("wireless")
83         
84         if wldevs then
85                 for k, v in pairs(wldevs) do
86                         if v[".type"] == "wifi-device" then
87                                 table.insert(ifaces, k)
88                         end
89                 end
90         end
91         
92         ffluci.template.render("freifunk/wizard", {ifaces=ifaces})
93 end
94
95 function configure_freifunk()
96         local ip  = ffluci.http.formvalue("ip")
97         local uci = ffluci.model.uci.Session()
98         
99         -- Load UCI
100         uci:t_load("network")
101         uci:t_load("dhcp")
102         uci:t_load("freifunk")
103         uci:t_load("luci_splash")
104         uci:t_load("olsr")
105         uci:t_load("wireless")
106         uci:t_load("luci_fw")
107         
108         
109         -- Configure FF-Interface
110         uci:t_del("network", "ff")
111         uci:t_del("network", "ffdhcp")
112         
113         uci:t_set("network", "ff", nil, "interface")
114         uci:t_set("network", "ff", "type", "bridge")
115         uci:t_set("network", "ff", "proto", "static")
116         uci:t_set("network", "ff", "ipaddr", ip)
117         uci:t_set("network", "ff", "netmask", uci:t_get("freifunk", "community", "mask")) 
118         uci:t_set("network", "ff", "dns", uci:t_get("freifunk", "community", "dns")) 
119         
120         -- Reset Routing
121         local routing = uci:t_sections("luci_fw")
122         if routing then
123                 for k, v in pairs(routing) do
124                         if v[".type"] == "routing" and (v.iface == "ff" or v.oface == "ff") then
125                                 uci:t_del("luci_fw", k)
126                         end
127                 end
128         
129                 local int = uci:t_add("luci_fw", "routing")
130                 uci:t_set("luci_fw", int, "iface", "ff")
131                 uci:t_set("luci_fw", int, "oface", "ff")
132                 uci:t_set("luci_fw", int, "fwd", "1")
133         end
134         
135         -- Routing from Internal
136         local iface = ffluci.http.formvalue("frominternal")
137         if iface and iface ~= "" then
138                 local routing = uci:t_sections("luci_fw")
139                 if routing then
140                         for k, v in pairs(routing) do
141                                 if v[".type"] == "routing" and (v.iface == iface and v.oface == "ff") then
142                                         uci:t_del("luci_fw", k)
143                                 end
144                         end
145                 
146                         local int = uci:t_add("luci_fw", "routing")
147                         uci:t_set("luci_fw", int, "iface", iface)
148                         uci:t_set("luci_fw", int, "oface", "ff")
149                         uci:t_set("luci_fw", int, "fwd", "1")
150                         uci:t_set("luci_fw", int, "nat", "1")
151                 end             
152         end     
153         
154         -- Routing to External
155         local iface = ffluci.http.formvalue("toexternal")
156         if iface and iface ~= "" then
157                 local routing = uci:t_sections("luci_fw")
158                 if routing then
159                         for k, v in pairs(routing) do
160                                 if v[".type"] == "routing" and (v.oface == iface and v.iface == "ff") then
161                                         uci:t_del("luci_fw", k)
162                                 end
163                         end
164                 
165                         local int = uci:t_add("luci_fw", "routing")
166                         uci:t_set("luci_fw", int, "iface", "ff")
167                         uci:t_set("luci_fw", int, "oface", iface)
168                         uci:t_set("luci_fw", int, "fwd", "1")
169                         uci:t_set("luci_fw", int, "nat", "1")
170                 end             
171         end     
172         
173         -- Configure DHCP
174         if ffluci.http.formvalue("dhcp") then
175                 local dhcpnet = uci:t_get("freifunk", "community", "dhcp"):match("^([0-9]+)")
176                 local dhcpip  = ip:gsub("^[0-9]+", dhcpnet)
177         
178                 uci:t_set("network", "ffdhcp", nil, "interface")
179                 uci:t_set("network", "ffdhcp", "proto", "static")
180                 uci:t_set("network", "ffdhcp", "ifname", "br-ff:dhcp")
181                 uci:t_set("network", "ffdhcp", "ipaddr", dhcpip)
182                 uci:t_set("network", "ffdhcp", "netmask", uci:t_get("freifunk", "community", "dhcpmask"))
183                 
184                 local dhcp = uci:t_sections("dhcp")
185                 if dhcp then
186                         for k, v in pairs(dhcp) do
187                                 if v[".type"] == "dhcp" and v.interface == "ffdhcp" then
188                                         uci:t_del("dhcp", k)
189                                 end
190                         end             
191                         
192                         local dhcpbeg = 48 + tonumber(ip:match("[0-9]+$")) * 4
193                         
194                         local sk = uci:t_add("dhcp", "dhcp")
195                         uci:t_set("dhcp", sk, "interface", "ffdhcp")
196                         uci:t_set("dhcp", sk, "start", dhcpbeg)
197                         uci:t_set("dhcp", sk, "limit", (dhcpbeg < 252) and 3 or 2)
198                         uci:t_set("dhcp", sk, "leasetime", "30m")
199                 end 
200                 
201                 local splash = uci:t_sections("luci_splash")
202                 if splash then
203                         for k, v in pairs(splash) do
204                                 if v[".type"] == "iface" then
205                                         uci:t_del("luci_splash", k)
206                                 end
207                         end             
208                         
209                         local sk = uci:t_add("luci_splash", "iface")
210                         uci:t_set("luci_splash", sk, "network", "ffdhcp")
211                 end     
212                 
213                 local routing = uci:t_sections("luci_fw")
214                 if routing then
215                         for k, v in pairs(routing) do
216                                 if v[".type"] == "routing" and (v.iface == "ffdhcp" or v.oface == "ffdhcp") then
217                                         uci:t_del("luci_fw", k)
218                                 end
219                         end
220                         
221                         local int = uci:t_add("luci_fw", "routing")
222                         uci:t_set("luci_fw", int, "iface", "ffdhcp")
223                         uci:t_set("luci_fw", int, "oface", "ff")
224                         uci:t_set("luci_fw", int, "nat", "1")                   
225                         
226                         local iface = ffluci.http.formvalue("toexternal")
227                         if iface and iface ~= "" then
228                                 local int = uci:t_add("luci_fw", "routing")
229                                 uci:t_set("luci_fw", int, "iface", "ffdhcp")
230                                 uci:t_set("luci_fw", int, "oface", iface)
231                                 uci:t_set("luci_fw", int, "nat", "1")                           
232                         end
233                 end     
234         end
235         
236         -- Configure OLSR
237         if ffluci.http.formvalue("olsr") and uci:t_sections("olsr") then
238                 for k, v in pairs(uci:t_sections("olsr")) do
239                         if v[".type"] == "Interface" or v[".type"] == "LoadPlugin" then
240                                 uci:t_del("olsr", k)
241                         end
242                 end
243                 
244                 if ffluci.http.formvalue("shareinet") then
245                         uci:t_set("olsr", "dyn_gw", nil, "LoadPlugin")
246                         uci:t_set("olsr", "dyn_gw", "Library", "olsrd_dyn_gw.so.0.4")
247                 end
248                 
249                 uci:t_set("olsr", "nameservice", nil, "LoadPlugin")
250                 uci:t_set("olsr", "nameservice", "Library", "olsrd_nameservice.so.0.3")
251                 uci:t_set("olsr", "nameservice", "name", ip:gsub("%.", "-"))
252                 uci:t_set("olsr", "nameservice", "hosts_file", "/var/etc/hosts")
253                 uci:t_set("olsr", "nameservice", "suffix", ".olsr")
254                 uci:t_set("olsr", "nameservice", "latlon_infile", "/tmp/latlon.txt")
255                 
256                 uci:t_set("olsr", "txtinfo", nil, "LoadPlugin")
257                 uci:t_set("olsr", "txtinfo", "Library", "olsrd_txtinfo.so.0.1")
258                 uci:t_set("olsr", "txtinfo", "Accept", "127.0.0.1")
259                 
260                 local oif = uci:t_add("olsr", "Interface")
261                 uci:t_set("olsr", oif, "Interface", "ff")
262                 uci:t_set("olsr", oif, "HelloInterval", "6.0")
263                 uci:t_set("olsr", oif, "HelloValidityTime", "108.0")
264                 uci:t_set("olsr", oif, "TcInterval", "4.0")
265                 uci:t_set("olsr", oif, "TcValidityTime", "324.0")
266                 uci:t_set("olsr", oif, "MidInterval", "18.0")
267                 uci:t_set("olsr", oif, "MidValidityTime", "324.0")
268                 uci:t_set("olsr", oif, "HnaInterval", "18.0")
269                 uci:t_set("olsr", oif, "HnaValidityTime", "108.0")
270         end
271         
272         -- Configure Wifi
273         local wcfg = uci:t_sections("wireless")
274         if wcfg then
275                 for iface, v in pairs(wcfg) do
276                         if v[".type"] == "wifi-device" and ffluci.http.formvalue("wifi."..iface) then
277                                 -- Cleanup
278                                 for k, j in pairs(wcfg) do
279                                         if j[".type"] == "wifi-iface" and j.device == iface then
280                                                 uci:t_del("wireless", k)
281                                         end
282                                 end
283                                 
284                                 uci:t_set("wireless", iface, "disabled", "0")
285                                 uci:t_set("wireless", iface, "mode", "11g")
286                                 uci:t_set("wireless", iface, "txantenna", 1)
287                                 uci:t_set("wireless", iface, "rxantenna", 1)
288                                 uci:t_set("wireless", iface, "channel", uci:t_get("freifunk", "community", "channel")) 
289                                 
290                                 local wif = uci:t_add("wireless", "wifi-iface")
291                                 uci:t_set("wireless", wif, "device", iface)
292                                 uci:t_set("wireless", wif, "network", "ff")
293                                 uci:t_set("wireless", wif, "mode", "adhoc")
294                                 uci:t_set("wireless", wif, "ssid", uci:t_get("freifunk", "community", "essid"))
295                                 uci:t_set("wireless", wif, "bssid", uci:t_get("freifunk", "community", "bssid"))
296                                 uci:t_set("wireless", wif, "txpower", 13)
297                         end
298                 end
299         end
300         
301         -- Save UCI
302         uci:t_save("network")
303         uci:t_save("dhcp")
304         uci:t_save("freifunk")
305         uci:t_save("luci_splash")
306         uci:t_save("olsr")
307         uci:t_save("wireless")
308         uci:t_save("luci_fw")
309
310         ffluci.http.redirect(ffluci.dispatcher.build_url("admin", "uci", "changes"))
311 end