summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-ffwizard/luasrc/model/cbi/ffwizard.lua
blob: bc8bfd61cb3bb6b4cc7748b1ffca25ac4730ce8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
--[[
LuCI - Lua Configuration Interface

Copyright 2008 Steven Barth <steven@midlink.org>
Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
Copyright 2011 Patrick Grimm <patrick@pberg.freifunk.net>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

$Id$

]]--


local uci = require "luci.model.uci".cursor()
local uci_state = require "luci.model.uci".cursor_state()
local tools = require "luci.tools.ffwizard"
local util = require "luci.util"
local sys = require "luci.sys"
local ip = require "luci.ip"
local fs  = require "nixio.fs"

local has_pptp  = fs.access("/usr/sbin/pptp")
local has_pppoe = fs.glob("/usr/lib/pppd/*/rp-pppoe.so")()
local has_l2gvpn  = fs.access("/usr/sbin/node")
local has_radvd  = fs.access("/etc/config/radvd")
local has_rom  = fs.access("/rom/etc")
local has_autoipv6  = fs.access("/usr/bin/auto-ipv6")
local has_qos  = fs.access("/etc/init.d/qos")
local has_ipv6 = fs.access("/proc/sys/net/ipv6")
local has_hb = fs.access("/sbin/heartbeat")

luci.i18n.loadc("freifunk")

function get_mac(ix)
	if string.find(ix, "radio") then
		ix = string.gsub(ix,"radio", 'wlan')
	end
	local mac = fs.readfile("/sys/class/net/" .. ix .. "/address")
	if not mac then
		mac = luci.util.exec("ifconfig " .. ix)
		mac = mac and mac:match(" ([A-F0-9:]+)%s*\n")
	else
		mac = mac:sub(1,17)
	end
	if mac and #mac > 0 then
		return mac:lower()
	end
	return "?"
end
function get_ula(imac)
	if string.len(imac) == 17 then
		local mac1 = string.sub(imac,4,8)
		local mac2 = string.sub(imac,10,14)
		local mac3 = string.sub(imac,16,17)
		return 'fdca:ffee:babe::02'..mac1..'ff:fe'..mac2..mac3..'/64'
	end
	return "?"
end


-------------------- View --------------------
f = SimpleForm("ffwizward", "Freifunkassistent",
 "Dieser Assistent unterstützt Sie bei der Einrichtung des Routers für das Freifunknetz.")
-- main netconfig
local newpsswd = has_rom and sys.exec("diff /rom/etc/passwd /etc/passwd")
if newpsswd ~= "" then
	pw = f:field(Flag, "pw", "Router Passwort", "Setzen Sie den Haken, um Ihr Passwort zu ändern.")
	function pw.cfgvalue(self, section)
		return 1
	end
end

pw1 = f:field(Value, "pw1", translate("password"))
pw1.password = true
pw1.rmempty = false

pw2 = f:field(Value, "pw2", translate("confirmation"))
pw2.password = true
pw2.rmempty = false

function pw2.validate(self, value, section)
	return pw1:formvalue(section) == value and value
end

if newpsswd ~= "" then
	pw1:depends("pw", "1")
	pw2:depends("pw", "1")
end

net = f:field(ListValue, "net", "Freifunk Community", "Nutzen Sie die Einstellungen der Freifunk Gemeinschaft in ihrer Nachbarschaft.")
net.rmempty = false
net.optional = false
uci:foreach("freifunk", "community", function(s)
	net:value(s[".name"], "%s (%s)" % {s.name, s.mesh_network or "?"})
end)
function net.cfgvalue(self, section)
	return uci:get("freifunk", "wizard", "net")
end
function net.write(self, section, value)
	uci:set("freifunk", "wizard", "net", value)
	uci:save("freifunk")
end
net_lat = f:field(ListValue, "net_lat", "", "")
net_lat:depends("net", "0")
net_lon = f:field(ListValue, "net_lon", "", "")
net_lon:depends("net", "0")

uci:foreach("freifunk", "community", function(s)
	if s.latitude then
		net_lat:value(s[".name"], "%s" % {s.latitude or "?"})
	end
	if s.longitude then
		net_lon:value(s[".name"], "%s" % {s.longitude or "?"})
	end
end)

-- hostname
hostname = f:field(Value, "hostname", "Knoten Name", "Geben Sie Ihrem Freifunk Router einen Namen. Wenn Sie dieses Feld leer lassen, wird der Name automatisch aus der Mesh IP generiert.")
hostname.rmempty = true
hostname.optional = false
function hostname.cfgvalue(self, section)
	return sys.hostname()
end
function hostname.write(self, section, value)
	uci:set("freifunk", "wizard", "hostname", value)
	uci:save("freifunk")
end
function hostname.validate(self, value)
	if (#value > 16) then
		return
	elseif (string.find(value, "[^%w%_%-]")) then
		return
	else
		return value
	end
end

-- location
location = f:field(Value, "location", "Standort", "Geben Sie den Standort ihres Gerätes an")
location.rmempty = false
location.optional = false
function location.cfgvalue(self, section)
	return uci:get("freifunk", "contact", "location")
end
function location.write(self, section, value)
	uci:set("freifunk", "contact", "location", value)
	uci:save("freifunk")
end

-- mail
mail = f:field(Value, "mail", "E-Mail", "Bitte hinterlegen Sie eine Kontaktadresse.")
mail.rmempty = false
mail.optional = false
function mail.cfgvalue(self, section)
	return uci:get("freifunk", "contact", "mail")
end
function mail.write(self, section, value)
	uci:set("freifunk", "contact", "mail", value)
	uci:save("freifunk")
end
-- main netconfig
main = f:field(Flag, "netconfig", "Netzwerk einrichten", "Setzen Sie den Haken, wenn Sie Ihr Freifunk Netzwerk einrichten wollen.")
uci:foreach("wireless", "wifi-device",
	function(section)
		local device = section[".name"]
		local dev = f:field(Flag, "device_" .. device , "<b>Drahtloses Netzwerk \"" .. device:upper() .. "\"</b> ", "Konfigurieren Sie Ihre drahtlose " .. device:upper() .. "Schnittstelle (WLAN).")
			dev:depends("netconfig", "1")
			dev.rmempty = false
			function dev.cfgvalue(self, section)
				return uci:get("freifunk", "wizard", "device_" .. device)
			end
			function dev.write(self, sec, value)
				if value then
					uci:set("freifunk", "wizard", "device_" .. device, value)
					uci:save("freifunk")
				end
			end
		local chan = f:field(ListValue, "chan_" .. device, device:upper() .. "  Freifunk Kanal einrichten", "Ihr Gerät und benachbarte Freifunk Knoten müssen auf demselben Kanal senden. Je nach Gerätetyp können Sie zwischen verschiedenen 2,4Ghz und 5Ghz Kanälen auswählen.")
			chan:depends("device_" .. device, "1")
			chan.rmempty = true
			function chan.cfgvalue(self, section)
				return uci:get("freifunk", "wizard", "chan_" .. device)
			end

			chan:value('default')
			for _, f in ipairs(sys.wifi.channels(device)) do
				if not f.restricted then
					chan:value(f.channel)
				end
			end

			function chan.write(self, sec, value)
				if value then
					uci:set("freifunk", "wizard", "chan_" .. device, value)
					uci:save("freifunk")
				end
			end

			local meship = f:field(Value, "meship_" .. device, device:upper() .. "  Mesh IP Adresse einrichten", "Ihre Mesh IP Adresse erhalten Sie von der Freifunk Gemeinschaft in Ihrer Nachbarschaft. Es ist eine netzweit eindeutige Identifikation, z.B. 104.1.1.1.")
			meship:depends("device_" .. device, "1")
			meship.rmempty = true
			function meship.cfgvalue(self, section)
				return uci:get("freifunk", "wizard", "meship_" .. device)
			end
			function meship.validate(self, value)
				local x = ip.IPv4(value)
				return ( x and x:prefix() == 32 ) and x:string() or ""
			end
			function meship.write(self, sec, value)
				uci:set("freifunk", "wizard", "meship_" .. device, value)
				local new_ip = ip.IPv4(value)
				if new_ip then
					local new_hostname = new_ip:string():gsub("%.", "-")
					uci:set("freifunk", "wizard", "hostname", new_hostname)
					uci:save("freifunk")
				end
			end
		if has_ipv6 then
			local meship6 = f:field(Value, "meship6_" .. device, device:upper() .. "  Mesh IPv6 Adresse einrichten", "Ihre Mesh IP Adresse wird automatisch berechnet")
			meship6:depends("device_" .. device, "1")
			meship6.rmempty = true
			function meship6.cfgvalue(self, section)
				return get_ula(get_mac(device))
			end
		end
	
		local client = f:field(Flag, "client_" .. device, device:upper() .. "  DHCP anbieten", "DHCP weist verbundenen Benutzern automatisch eine Adresse zu. Diese Option sollten Sie unbedingt aktivieren, wenn Sie Nutzer an der drahtlosen Schnittstelle erwarten.")
			client:depends("device_" .. device, "1")
			client.rmempty = false
			function client.cfgvalue(self, section)
				return uci:get("freifunk", "wizard", "client_" .. device)
			end
			function client.write(self, sec, value)
				uci:set("freifunk", "wizard", "client_" .. device, value)
				uci:save("freifunk")
			end
		local dhcpmesh = f:field(Value, "dhcpmesh_" .. device, device:upper() .. "  Mesh DHCP anbieten", "Bestimmen Sie den Adressbereich aus dem Ihre Nutzer IP Adressen erhalten. Es wird empfohlen einen Adressbereich aus Ihrer lokalen Freifunk Gemeinschaft zu nutzen. Der Adressbereich ist ein netzweit eindeutiger Netzbereich. z.B. 104.1.2.1/28")
			dhcpmesh:depends("client_" .. device, "1")
			dhcpmesh.rmempty = true
			function dhcpmesh.cfgvalue(self, section)
				return uci:get("freifunk", "wizard", "dhcpmesh_" .. device)
			end
			function dhcpmesh.validate(self, value)
				local x = ip.IPv4(value)
				return ( x and x:minhost()) and x:string() or ""
			end
			function dhcpmesh.write(self, sec, value)
				uci:set("freifunk", "wizard", "dhcpmesh_" .. device, value)
				uci:save("freifunk")
			end
		local hwtype = section.type
		if hwtype == "atheros" then
			local vap = f:field(Flag, "vap_" .. device , "Virtueller Drahtloser Zugangspunkt", "Konfigurieren Sie Ihren Virtuellen AP")
			vap:depends("client_" .. device, "1")
			vap.rmempty = false
			function vap.cfgvalue(self, section)
				return uci:get("freifunk", "wizard", "vap_" .. device)
			end
			function vap.write(self, sec, value)
				uci:set("freifunk", "wizard", "vap_" .. device, value)
				uci:save("freifunk")
			end
		end
	end)

uci:foreach("network", "interface",
	function(section)
		local device = section[".name"]
		local ifname = uci_state:get("network",device,"ifname")
		if device ~= "loopback" and not string.find(device, "gvpn")  and not string.find(device, "wifi") and not string.find(device, "wl") and not string.find(device, "wlan") and not string.find(device, "wireless") and not string.find(device, "radio") then
			dev = f:field(Flag, "device_" .. device , "<b>Drahtgebundenes Netzwerk \"" .. device:upper() .. "\"</b>", "Konfigurieren Sie Ihre drahtgebunde " .. device:upper() .. " Schnittstelle (LAN).")
				dev:depends("netconfig", "1")
				dev.rmempty = false
				function dev.cfgvalue(self, section)
					return uci:get("freifunk", "wizard", "device_" .. device)
				end
				function dev.write(self, sec, value)
					uci:set("freifunk", "wizard", "device_" .. device, value)
					uci:save("freifunk")
				end
			meship = f:field(Value, "meship_" .. device, device:upper() .. "  Mesh IP Adresse einrichten", "Ihre Mesh IP Adresse erhalten Sie von der Freifunk Gemeinschaft in Ihrer Nachbarschaft. Es ist eine netzweit eindeutige Identifikation, z.B. 104.1.1.1.")
				meship:depends("device_" .. device, "1")
				meship.rmempty = true
				function meship.cfgvalue(self, section)
					return uci:get("freifunk", "wizard", "meship_" .. device)
				end
				function meship.validate(self, value)
					local x = ip.IPv4(value)
					return ( x and x:prefix() == 32 ) and x:string() or ""
				end
				function meship.write(self, sec, value)
					uci:set("freifunk", "wizard", "meship_" .. device, value)
				end
			if has_ipv6 then
				meship6 = f:field(Value, "meship6_" .. device, device:upper() .. "  Mesh IPv6 Adresse einrichten", "Ihre Mesh IP Adresse wird automatisch berechnet")
				meship6:depends("device_" .. device, "1")
				meship6.rmempty = true
				function meship6.cfgvalue(self, section)
					return get_ula(get_mac(ifname))
				end
			end

			client = f:field(Flag, "client_" .. device, device:upper() .. "  DHCP anbieten","DHCP weist verbundenen Benutzern automatisch eine Adresse zu. Diese Option sollten Sie unbedingt aktivieren, wenn Sie Nutzer an der drahtlosen Schnittstelle erwarten.")
				client:depends("device_" .. device, "1")
				client.rmempty = false
				function client.cfgvalue(self, section)
					return uci:get("freifunk", "wizard", "client_" .. device)
				end
				function client.write(self, sec, value)
					uci:set("freifunk", "wizard", "client_" .. device, value)
					uci:save("freifunk")
				end
			dhcpmesh = f:field(Value, "dhcpmesh_" .. device, device:upper() .. "  Mesh DHCP anbieten ", "Bestimmen Sie den Adressbereich aus dem Ihre Nutzer IP Adressen erhalten. Es wird empfohlen einen Adressbereich aus Ihrer lokalen Freifunk Gemeinschaft zu nutzen. Der Adressbereich ist ein netzweit eindeutiger Netzbereich. z.B. 104.1.2.1/28")
				dhcpmesh:depends("client_" .. device, "1")
				dhcpmesh.rmempty = true
				function dhcpmesh.cfgvalue(self, section)
					return uci:get("freifunk", "wizard", "dhcpmesh_" .. device)
				end
				function dhcpmesh.validate(self, value)
					local x = ip.IPv4(value)
					return ( x and x:prefix() <= 30 and x:minhost()) and x:string() or ""
				end
				function dhcpmesh.write(self, sec, value)
					uci:set("freifunk", "wizard", "dhcpmesh_" .. device, value)
					uci:save("freifunk")
				end
		end
	end)


local syslat = uci:get("freifunk", "wizard", "latitude") or 52
local syslon = uci:get("freifunk", "wizard", "longitude") or 10
uci:foreach("system", "system", function(s)
		if s.latitude then
			syslat = s.latitude
		end
		if s.longitude then
			syslon = s.longitude
		end
end)
uci:foreach("olsrd", "LoadPlugin", function(s)
	if s.library == "olsrd_nameservice.so.0.3" then
		if s.lat then
			syslat = s.lat
		end
		if s.lon then
			syslon = s.lon
		end
	end
end)

lat = f:field(Value, "lat", "geographischer Breitengrad", "Setzen Sie den Breitengrad (Latitude) Ihres Geräts.")
lat:depends("netconfig", "1")
function lat.cfgvalue(self, section)
	return syslat
end
function lat.write(self, section, value)
	uci:set("freifunk", "wizard", "latitude", value)
	uci:save("freifunk")
end

lon = f:field(Value, "lon", "geograpischer Längengrad", "Setzen Sie den Längengrad (Longitude) Ihres Geräts.")
lon:depends("netconfig", "1")
function lon.cfgvalue(self, section)
	return syslon
end
function lon.write(self, section, value)
	uci:set("freifunk", "wizard", "longitude", value)
	uci:save("freifunk")
end

--[[
*Opens an OpenStreetMap iframe or popup
*Makes use of resources/OSMLatLon.htm and htdocs/resources/osm.js
(is that the right place for files like these?)
]]--

local class = util.class

OpenStreetMapLonLat = class(AbstractValue)

function OpenStreetMapLonLat.__init__(self, ...)
	AbstractValue.__init__(self, ...)
	self.template = "cbi/osmll_value"
	self.latfield = nil
	self.lonfield = nil
	self.centerlat = ""
	self.centerlon = ""
	self.zoom = "0"
	self.width = "100%" --popups will ignore the %-symbol, "100%" is interpreted as "100"
	self.height = "600"
	self.popup = false
	self.displaytext="OpenStreetMap" --text on button, that loads and displays the OSMap
	self.hidetext="X" -- text on button, that hides OSMap
end

osm = f:field(OpenStreetMapLonLat, "latlon", "Geokoordinaten mit OpenStreetMap ermitteln:", "Klicken Sie auf Ihren Standort in der Karte. Diese Karte funktioniert nur, wenn das Gerät bereits eine Verbindung zum Internet hat.")
osm:depends("netconfig", "1")
osm.latfield = "lat"
osm.lonfield = "lon"
osm.centerlat = syslat
osm.centerlon = syslon
osm.width = "100%"
osm.height = "600"
osm.popup = false
syslatlengh = string.len(syslat)
if syslatlengh > 7 then
	osm.zoom = "15"
elseif syslatlengh > 5 then
	osm.zoom = "12"
else
	osm.zoom = "6"
end
osm.displaytext="OpenStreetMap anzeigen"
osm.hidetext="OpenStreetMap verbergen"

share = f:field(Flag, "sharenet", "Eigenen Internetzugang freigeben", "Geben Sie Ihren Internetzugang im Freifunknetz frei.")
share.rmempty = false
share:depends("netconfig", "1")
function share.cfgvalue(self, section)
	return uci:get("freifunk", "wizard", "share")
end
function share.write(self, section, value)
	uci:set("freifunk", "wizard", "share", value)
	uci:save("freifunk")
end

wanproto = f:field(ListValue, "wanproto", "Protokoll des Internetzugangs", "Geben Sie das Protokol an ueber das eine Internet verbindung hergestellt werden kann.")
wanproto:depends("sharenet", "1")
wanproto:value("static", translate("manual", "manual"))
wanproto:value("dhcp", translate("automatic", "automatic"))
if has_pppoe then wanproto:value("pppoe", "PPPoE") end
if has_pptp  then wanproto:value("pptp",  "PPTP")  end
function wanproto.cfgvalue(self, section)
	return uci:get("network", "wan", "proto") or "dhcp"
end
function wanproto.write(self, section, value)
	uci:set("network", "wan", "proto", value)
	uci:save("network")
end
wanip = f:field(Value, "wanipaddr", translate("ipaddress"))
wanip:depends("wanproto", "static")
function wanip.cfgvalue(self, section)
	return uci:get("network", "wan", "ipaddr")
end
function wanip.write(self, section, value)
	uci:set("network", "wan", "ipaddr", value)
	uci:save("network")
end
wannm = f:field(Value, "wannetmask", translate("netmask"))
wannm:depends("wanproto", "static")
function wannm.cfgvalue(self, section)
	return uci:get("network", "wan", "netmask")
end
function wannm.write(self, section, value)
	uci:set("network", "wan", "netmask", value)
	uci:save("network")
end
wangw = f:field(Value, "wangateway", translate("gateway"))
wangw:depends("wanproto", "static")
wangw.rmempty = true
function wangw.cfgvalue(self, section)
	return uci:get("network", "wan", "gateway")
end
function wangw.write(self, section, value)
	uci:set("network", "wan", "gateway", value)
	uci:save("network")
end
wandns = f:field(Value, "wandns", translate("dnsserver"))
wandns:depends("wanproto", "static")
wandns.rmempty = true
function wandns.cfgvalue(self, section)
	return uci:get("network", "wan", "dns")
end
function wandns.write(self, section, value)
	uci:set("network", "wan", "dns", value)
	uci:save("network")
end
wanusr = f:field(Value, "wanusername", translate("username"))
wanusr:depends("wanproto", "pppoe")
wanusr:depends("wanproto", "pptp")
function wanusr.cfgvalue(self, section)
	return uci:get("network", "wan", "username")
end
function wanusr.write(self, section, value)
	uci:set("network", "wan", "username", value)
	uci:save("network")
end
wanpwd = f:field(Value, "wanpassword", translate("password"))
wanpwd.password = true
wanpwd:depends("wanproto", "pppoe")
wanpwd:depends("wanproto", "pptp")
function wanpwd.cfgvalue(self, section)
	return uci:get("network", "wan", "password")
end
function wanpwd.write(self, section, value)
	uci:set("network", "wan", "password", value)
	uci:save("network")
end

wansec = f:field(Flag, "wansec", "WAN-Zugriff auf Gateway beschränken", "Verbieten Sie Zugriffe auf Ihr lokales Netzwerk aus dem Freifunknetz.")
wansec.rmempty = false
wansec:depends("wanproto", "static")
wansec:depends("wanproto", "dhcp")
function wansec.cfgvalue(self, section)
	return uci:get("freifunk", "wizard", "wan_security")
end
function wansec.write(self, section, value)
	uci:set("freifunk", "wizard", "wan_security", value)
	uci:save("freifunk")
end
if has_qos then
	wanqosdown = f:field(Value, "wanqosdown", "Download Bandbreite begrenzen", "kb/s")
	wanqosdown:depends("sharenet", "1")
	function wanqosdown.cfgvalue(self, section)
		return uci:get("qos", "wan", "download")
	end
	function wanqosdown.write(self, section, value)
		uci:set("qos", "wan", "download", value)
		uci:save("qos")
	end
	wanqosup = f:field(Value, "wanqosup", "Upload Bandbreite begrenzen", "kb/s")
	wanqosup:depends("sharenet", "1")
	function wanqosup.cfgvalue(self, section)
		return uci:get("qos", "wan", "upload")
	end
	function wanqosup.write(self, section, value)
		uci:set("qos", "wan", "upload", value)
		uci:save("qos")
	end
end

if has_l2gvpn then
	gvpn = f:field(Flag, "gvpn", "Freifunk Internet Tunnel", "Verbinden Sie ihren Router ueber das Internet mit anderen Freifunknetzen.")
	gvpn.rmempty = false
	gvpn:depends("sharenet", "1")
	function gvpn.cfgvalue(self, section)
		return uci:get("freifunk", "wizard", "gvpn")
	end
	function gvpn.write(self, section, value)
		uci:set("freifunk", "wizard", "gvpn", value)
		uci:save("freifunk")
	end
	gvpnip = f:field(Value, "gvpnipaddr", translate("ipaddress"))
	gvpnip:depends("gvpn", "1")
	function gvpnip.cfgvalue(self, section)
		return uci:get("l2gvpn", "bbb", "ip") or uci:get("network", "gvpn", "ipaddr")
	end
	function gvpnip.validate(self, value)
		local x = ip.IPv4(value)
		return ( x and x:prefix() == 32 ) and x:string() or ""
	end
end

if has_hb then
	hb = f:field(Flag, "hb", "Heartbeat aktivieren","Dem Gerät erlauben anonyme Statistiken zu übertragen. (empfohlen)")
	hb.rmempty = false
	hb:depends("netconfig", "1")
	function hb.cfgvalue(self, section)
		return uci:get("freifunk", "wizard", "hb")
	end
	function hb.write(self, section, value)
		uci:set("freifunk", "wizard", "hb", value)
		uci:save("freifunk")
	end
end

-------------------- Control --------------------
function f.handle(self, state, data)
	if state == FORM_VALID then
		local debug = uci:get("freifunk", "wizard", "debug")
		if debug == "1" then
			if data.pw1 then
				local stat = luci.sys.user.setpasswd("root", data.pw1) == 0
				if stat then
					f.message = translate("a_s_changepw_changed")
				else
					f.errmessage = translate("unknownerror")
				end
			end
			data.pw1 = nil
			data.pw2 = nil
			luci.http.redirect(luci.dispatcher.build_url(unpack(luci.dispatcher.context.requested.path), "system", "system"))
		else
			if data.pw1 then
				local stat = luci.sys.user.setpasswd("root", data.pw1) == 0
--				if stat then
--					f.message = translate("a_s_changepw_changed")
--			else
--				f.errmessage = translate("unknownerror")
				end
			data.pw1 = nil
			data.pw2 = nil
			uci:commit("freifunk")
			uci:commit("wireless")
			uci:commit("network")
			uci:commit("dhcp")
			uci:commit("luci_splash")
			uci:commit("firewall")
			uci:commit("system")
			uci:commit("uhttpd")
			uci:commit("olsrd")
			uci:commit("manager")
			if has_autoipv6 then
				uci:commit("autoipv6")
			end
			if has_qos then
				uci:commit("qos")
			end
			if has_l2gvpn then
				uci:commit("l2gvpn")
			end
			if has_radvd then
				uci:commit("radvd")
			end
-- the following line didn't work without admin-mini, for now i just replaced it with sys.exec... soma
--			luci.http.redirect(luci.dispatcher.build_url(unpack(luci.dispatcher.context.requested.path), "system", "reboot") .. "?reboot=1")
			sys.exec("reboot")
		end
		return false
	elseif state == FORM_INVALID then
		self.errmessage = "Ungültige Eingabe: Bitte die Formularfelder auf Fehler prüfen."
	end
	return true
end

local function _strip_internals(tbl)
	tbl = tbl or {}
	for k, v in pairs(tbl) do
		if k:sub(1, 1) == "." then
			tbl[k] = nil
		end
	end
	return tbl
end
-- Configure Freifunk checked
function main.write(self, section, value)
	if value == "0" then
		uci:set("freifunk", "wizard", "netconfig", "0")
		uci:save("freifunk")
		return
	end
	-- Collect IP-Address
	local community = net:formvalue(section)
	suffix = uci:get("freifunk", community, "suffix") or "olsr"

	-- Invalidate fields
	if not community then
		net.tag_missing[section] = true
		return
	end

	uci:set("freifunk", "wizard", "netconfig", "1")
	uci:save("freifunk")

	local external
	external = uci:get("freifunk", community, "external") or ""

	local netname = "wireless"
	local network
	network = ip.IPv4(uci:get("freifunk", community, "mesh_network") or "104.0.0.0/8")

	-- Tune community settings
	if community and uci:get("freifunk", community) then
		uci:tset("freifunk", "community", uci:get_all("freifunk", community))
	end

	-- Cleanup
	uci:delete_all("firewall","zone", {name="freifunk"})
	uci:delete_all("firewall","forwarding", {dest="freifunk"})
	uci:delete_all("firewall","forwarding", {src="freifunk"})
	uci:delete_all("firewall","rule", {dest="freifunk"})
	uci:delete_all("firewall","rule", {src="freifunk"})
	uci:save("firewall")
	-- Create firewall zone and add default rules (first time)
	--                    firewall_create_zone("name"    , "input" , "output", "forward ", Masqurade)
	local newzone = tools.firewall_create_zone("freifunk", "ACCEPT", "ACCEPT", "REJECT"  , true)
	if newzone then
		uci:foreach("freifunk", "fw_forwarding", function(section)
			uci:section("firewall", "forwarding", nil, section)
		end)
		uci:foreach(external, "fw_forwarding", function(section)
			uci:section("firewall", "forwarding", nil, section)
		end)

		uci:foreach("freifunk", "fw_rule", function(section)
			uci:section("firewall", "rule", nil, section)
		end)
		uci:foreach(external, "fw_rule", function(section)
			uci:section("firewall", "rule", nil, section)
		end)
	end
	uci:save("firewall")
	if has_hb then
		uci:delete("manager", "heartbeat", "interface")
		uci:save("manager")
	end
	-- Delete olsrdv4
	uci:delete_all("olsrd", "olsrd")
	local olsrbase
	olsrbase = uci:get_all("freifunk", "olsrd") or {}
	util.update(olsrbase, uci:get_all(external, "olsrd") or {})
	if has_ipv6 then
		olsrbase.IpVersion='6and4'
	else
		olsrbase.IpVersion='4'
	end
	uci:section("olsrd", "olsrd", nil, olsrbase)
	-- Delete olsrdv4 old p2pd settings
	uci:delete_all("olsrd", "LoadPlugin", {library="olsrd_mdns.so.1.0.0"})
	uci:delete_all("olsrd", "LoadPlugin", {library="olsrd_p2pd.so.0.1.0"})
	-- Write olsrdv4 new p2pd settings
	uci:section("olsrd", "LoadPlugin", nil, {
		library     = "olsrd_p2pd.so.0.1.0",
		P2pdTtl     = 10,
		UdpDestPort = "224.0.0.251 5353",
		ignore      = 1,
	})
	-- Delete http plugin
	uci:delete_all("olsrd", "LoadPlugin", {library="olsrd_httpinfo.so.0.1"})

	-- Delete olsrdv4 old interface
	uci:delete_all("olsrd", "Interface")
	uci:delete_all("olsrd", "Hna4")
	-- Create wireless ip4/ip6 and firewall config
	uci:foreach("wireless", "wifi-device",
	function(sec)
		local device = sec[".name"]
		if not luci.http.formvalue("cbid.ffwizward.1.device_" .. device) then
			return
		end
		node_ip = luci.http.formvalue("cbid.ffwizward.1.meship_" .. device) and ip.IPv4(luci.http.formvalue("cbid.ffwizward.1.meship_" .. device))
		if has_ipv6 then
			node_ip6 = luci.http.formvalue("cbid.ffwizward.1.meship6_" .. device) and ip.IPv6(luci.http.formvalue("cbid.ffwizward.1.meship6_" .. device))
		end
		if not node_ip or not network or not network:contains(node_ip) then
			meship.tag_missing[section] = true
			node_ip = nil
			return
		end
		-- rename the wireless interface s/wifi/wireless/
		local nif
		if string.find(device, "wifi") then
			nif = string.gsub(device,"wifi", netname)
		elseif string.find(device, "wl") then
			nif = string.gsub(device,"wl", netname)
		elseif string.find(device, "wlan") then
			nif = string.gsub(device,"wlan", netname)
		elseif string.find(device, "radio") then
			nif = string.gsub(device,"radio", netname)
		end
		-- Cleanup
		tools.wifi_delete_ifaces(device)
		-- tools.network_remove_interface(device)
		uci:delete("network", device .. "dhcp")
		uci:delete("network", device)
		tools.firewall_zone_remove_interface("freifunk", device)
		-- tools.network_remove_interface(nif)
		uci:delete("network", nif .. "dhcp")
		uci:delete("network", nif)
		tools.firewall_zone_remove_interface("freifunk", nif)
		-- Delete old dhcp
		uci:delete("dhcp", device)
		uci:delete("dhcp", device .. "dhcp")
		uci:delete("dhcp", nif)
		uci:delete("dhcp", nif .. "dhcp")
		-- Delete old splash
		uci:delete_all("luci_splash", "iface", {network=device.."dhcp", zone="freifunk"})
		uci:delete_all("luci_splash", "iface", {network=nif.."dhcp", zone="freifunk"})
		-- Delete old radvd
		if has_radvd then
			uci:delete_all("radvd", "interface", {interface=nif.."dhcp"})
			uci:delete_all("radvd", "interface", {interface=nif})
			uci:delete_all("radvd", "prefix", {interface=nif.."dhcp"})
			uci:delete_all("radvd", "prefix", {interface=nif})
		end
		-- New Config
		-- Tune wifi device
		local ssid = uci:get("freifunk", community, "ssid") or "olsr.freifunk.net"
		local devconfig = uci:get_all("freifunk", "wifi_device")
		util.update(devconfig, uci:get_all(external, "wifi_device") or {})
		local channel = luci.http.formvalue("cbid.ffwizward.1.chan_" .. device)
		local hwmode = "11bg"
		local bssid = uci:get_all(external, "wifi_iface", "bssid") or "02:CA:FF:EE:BA:BE"
		local mrate = 5500
		-- set bssid, see https://kifuse02.pberg.freifunk.net/moin/channel-bssid-essid for schema
		if channel and channel ~= "default" then
			if devconfig.channel ~= channel then
				devconfig.channel = channel
				local chan = tonumber(channel)
				if chan >= 0 and chan < 10 then
					bssid = channel .. "2:CA:FF:EE:BA:BE"
				elseif chan == 10 then 
					bssid = "02:CA:FF:EE:BA:BE" 
				elseif chan >= 11 and chan <= 14 then
					bssid = string.format("%X",channel) .. "2:CA:FF:EE:BA:BE"
				elseif chan >= 36 and chan <= 64 then
					hwmode = "11a"
					mrate = ""
					outdoor = 0
					bssid = "00:" .. channel ..":CA:FF:EE:EE"
				elseif chan >= 100 and chan <= 140 then
					hwmode = "11a"
					mrate = ""
					outdoor = 1
					bssid = "01:" .. string.sub(channel, 2) .. ":CA:FF:EE:EE"
				end
				devconfig.hwmode = hwmode
				devconfig.outdoor = outdoor
			end
			ssid = ssid .. " - ch" .. channel
		end
		uci:tset("wireless", device, devconfig)
		-- Create wifi iface
		local ifconfig = uci:get_all("freifunk", "wifi_iface")
		util.update(ifconfig, uci:get_all(external, "wifi_iface") or {})
		ifconfig.device = device
		ifconfig.network = nif
		ifconfig.ssid = ssid
		ifconfig.bssid = bssid
		ifconfig.encryption="none"
		-- Read Preset 
		local netconfig = uci:get_all("freifunk", "interface")
		util.update(netconfig, uci:get_all(external, "interface") or {})
		netconfig.proto = "static"
		netconfig.ipaddr = node_ip:string()
		if has_ipv6 then
			netconfig.ip6addr = node_ip6:string()
		end
		uci:section("network", "interface", nif, netconfig)
		if has_radvd then
			uci:section("radvd", "interface", nil, {
				interface          =nif,
				AdvSendAdvert      =1,
				AdvManagedFlag     =0,
				AdvOtherConfigFlag =0,
				ignore             =0
			})
			uci:section("radvd", "prefix", nil, {
				interface          =nif,
				AdvOnLink          =1,
				AdvAutonomous      =1,
				AdvRouterAddr      =0,
				ignore             =0,
			})
			uci:save("radvd")
		end
		local new_hostname = node_ip:string():gsub("%.", "-")
		uci:set("freifunk", "wizard", "hostname", new_hostname)
		uci:save("freifunk")
		tools.firewall_zone_add_interface("freifunk", nif)
		uci:save("firewall")
		-- Write new olsrv4 interface
		local olsrifbase = uci:get_all("freifunk", "olsr_interface")
		util.update(olsrifbase, uci:get_all(external, "olsr_interface") or {})
		olsrifbase.interface = nif
		olsrifbase.ignore    = "0"
		uci:section("olsrd", "Interface", nil, olsrifbase)
		-- Collect MESH DHCP IP NET
		local client = luci.http.formvalue("cbid.ffwizward.1.client_" .. device)
		if client then
			local dhcpmeshnet = luci.http.formvalue("cbid.ffwizward.1.dhcpmesh_" .. device) and ip.IPv4(luci.http.formvalue("cbid.ffwizward.1.dhcpmesh_" .. device))
			if has_hb then
				local ifacelist = uci:get_list("manager", "heartbeat", "interface") or {}
				table.insert(ifacelist,nif .. "dhcp")
				uci:set_list("manager", "heartbeat", "interface", ifacelist)
				uci:save("manager")
			end
			if dhcpmeshnet then
				if not dhcpmeshnet:minhost() or not dhcpmeshnet:mask() then
					dhcpmesh.tag_missing[section] = true
					dhcpmeshnet = nil
					return
				end
				dhcp_ip = dhcpmeshnet:minhost():string()
				dhcp_mask = dhcpmeshnet:mask():string()
				dhcp_network = dhcpmeshnet:network():string()
				uci:section("olsrd", "Hna4", nil, {
					netmask  = dhcp_mask,
					netaddr  = dhcp_network
				})
				uci:foreach("olsrd", "LoadPlugin",
					function(s)		
						if s.library == "olsrd_p2pd.so.0.1.0" then
							uci:set("olsrd", s['.name'], "ignore", "0")
							local nonolsr = uci:get("olsrd", s['.name'], "NonOlsrIf") or ""
							vap = luci.http.formvalue("cbid.ffwizward.1.vap_" .. device)
							if vap then
								nonolsr = nif.."dhcp "..nonolsr
							else
								nonolsr = nif.." "..nonolsr
							end
							uci:set("olsrd", s['.name'], "NonOlsrIf", nonolsr)
						end
					end)
			else
				local subnet_prefix = tonumber(uci:get("freifunk", community, "splash_prefix")) or 27
				local pool_network = uci:get("freifunk", community, "splash_network") or "10.104.0.0/16"
				local pool = luci.ip.IPv4(pool_network)
				local ip = tostring(node_ip)
				if pool and ip then
					local hosts_per_subnet = 2^(32 - subnet_prefix)
					local number_of_subnets = (2^pool:prefix())/hosts_per_subnet
					local seed1, seed2 = ip:match("(%d+)%.(%d+)$")
					if seed1 and seed2 then
						math.randomseed(seed1 * seed2)
					end
					local subnet = pool:add(hosts_per_subnet * math.random(number_of_subnets))
					dhcp_ip = subnet:network(subnet_prefix):add(1):string()
					dhcp_mask = subnet:mask(subnet_prefix):string()
				end
			end
			if dhcp_ip and dhcp_mask then
				-- Create alias
				local aliasbase = uci:get_all("freifunk", "alias")
				util.update(aliasbase, uci:get_all(external, "alias") or {})
				aliasbase.ipaddr = dhcp_ip
				aliasbase.netmask = dhcp_mask
				aliasbase.proto = "static"
				vap = luci.http.formvalue("cbid.ffwizward.1.vap_" .. device)
				if vap then
					uci:section("network", "interface", nif .. "dhcp", aliasbase)
					uci:section("wireless", "wifi-iface", nil, {
						device     =device,
						mode       ="ap",
						encryption ="none",
						network    =nif .. "dhcp",
						ssid       ="AP-" .. ssid
					})
					if has_radvd then
						uci:section("radvd", "interface", nil, {
							interface          =nif .. "dhcp",
							AdvSendAdvert      =1,
							AdvManagedFlag     =0,
							AdvOtherConfigFlag =0,
							ignore             =0
						})
						uci:section("radvd", "prefix", nil, {
							interface          =nif .. "dhcp",
							AdvOnLink          =1,
							AdvAutonomous      =1,
							AdvRouterAddr      =0,
							ignore             =0
						})
						uci:save("radvd")
					end
					tools.firewall_zone_add_interface("freifunk", nif .. "dhcp")
					uci:save("wireless")
					ifconfig.mcast_rate = nil
					ifconfig.encryption="none"
				else
					aliasbase.interface = nif
					uci:section("network", "alias", nif .. "dhcp", aliasbase)
				end
				-- Create dhcp
				local dhcpbase = uci:get_all("freifunk", "dhcp")
				util.update(dhcpbase, uci:get_all(external, "dhcp") or {})
				dhcpbase.interface = nif .. "dhcp"
				dhcpbase.force = 1
				uci:section("dhcp", "dhcp", nif .. "dhcp", dhcpbase)
				uci:set_list("dhcp", nif .. "dhcp", "dhcp_option", "119,olsr")
				-- Create firewall settings
				uci:delete_all("firewall", "rule", {
					src="freifunk",
					proto="udp",
					dest_port="53"
				})
				uci:section("firewall", "rule", nil, {
					src="freifunk",
					proto="udp",
					dest_port="53",
					target="ACCEPT"
				})
				uci:delete_all("firewall", "rule", {
					src="freifunk",
					proto="udp",
					src_port="68",
					dest_port="67"
				})
				uci:section("firewall", "rule", nil, {
					src="freifunk",
					proto="udp",
					src_port="68",
					dest_port="67",
					target="ACCEPT"
				})
				uci:delete_all("firewall", "rule", {
					src="freifunk",
					proto="tcp",
					dest_port="8082",
				})
				uci:section("firewall", "rule", nil, {
					src="freifunk",
					proto="tcp",
					dest_port="8082",
					target="ACCEPT"
				})
				-- Register splash
				uci:section("luci_splash", "iface", nil, {network=nif.."dhcp", zone="freifunk"})
				uci:save("luci_splash")
				-- Make sure that luci_splash is enabled
				sys.init.enable("luci_splash")
			end
		else
			-- Delete old splash
			uci:delete_all("luci_splash", "iface", {network=device.."dhcp", zone="freifunk"})
		end
		--Write Ad-Hoc wifi section after AP wifi section
		uci:section("wireless", "wifi-iface", nil, ifconfig)
		uci:save("network")
		uci:save("wireless")
		uci:save("network")
		uci:save("firewall")
		uci:save("dhcp")
	end)
	-- Create wired ip and firewall config
	uci:foreach("network", "interface",
		function(sec)
		local device = sec[".name"]
		if not luci.http.formvalue("cbid.ffwizward.1.device_" .. device) then
			return
		end
		if device ~= "loopback" and not string.find(device, "wifi") and not string.find(device, "wl") and not string.find(device, "wlan") and not string.find(device, "wireless") and not string.find(device, "radio") then
			local node_ip
			node_ip = luci.http.formvalue("cbid.ffwizward.1.meship_" .. device) and ip.IPv4(luci.http.formvalue("cbid.ffwizward.1.meship_" .. device))
			if has_ipv6 then
				node_ip6 = luci.http.formvalue("cbid.ffwizward.1.meship6_" .. device) and ip.IPv6(luci.http.formvalue("cbid.ffwizward.1.meship6_" .. device))
			end
			if not node_ip or not network or not network:contains(node_ip) then
				meship.tag_missing[section] = true
				node_ip = nil
				return
			end
			-- Cleanup
			tools.firewall_zone_remove_interface(device, device)
			uci:delete_all("firewall","zone", {name=device})
			uci:delete_all("firewall","forwarding", {src=device})
			uci:delete_all("firewall","forwarding", {dest=device})
			uci:delete("network", device .. "dhcp")
			-- Delete old dhcp
			uci:delete("dhcp", device)
			uci:delete("dhcp", device .. "dhcp")
			-- Delete old splash
			uci:delete_all("luci_splash", "iface", {network=device.."dhcp", zone="freifunk"})
			if has_radvd then
				uci:delete_all("radvd", "interface", {interface=device.."dhcp"})
				uci:delete_all("radvd", "interface", {interface=device})
				uci:delete_all("radvd", "prefix", {interface=device.."dhcp"})
				uci:delete_all("radvd", "prefix", {interface=device})
			end
			-- New Config
			local netconfig = uci:get_all("freifunk", "interface")
			util.update(netconfig, uci:get_all(external, "interface") or {})
			netconfig.proto = "static"
			netconfig.ipaddr = node_ip:string()
			if has_ipv6 then
				netconfig.ip6addr = node_ip6:string()
			end
			uci:section("network", "interface", device, netconfig)
			uci:save("network")
			if has_radvd then
				uci:section("radvd", "interface", nil, {
					interface          =device,
					AdvSendAdvert      =1,
					AdvManagedFlag     =0,
					AdvOtherConfigFlag =0,
					ignore             =0
				})
				uci:section("radvd", "prefix", nil, {
					interface          =device,
					AdvOnLink          =1,
					AdvAutonomous      =1,
					AdvRouterAddr      =0,
					ignore             =0,
				})
				uci:save("radvd")
			end
			local new_hostname = node_ip:string():gsub("%.", "-")
			uci:set("freifunk", "wizard", "hostname", new_hostname)
			uci:save("freifunk")
			tools.firewall_zone_add_interface("freifunk", device)
			uci:save("firewall")
			-- Write new olsrv4 interface
			local olsrifbase = uci:get_all("freifunk", "olsr_interface")
			util.update(olsrifbase, uci:get_all(external, "olsr_interface") or {})
			olsrifbase.interface = device
			olsrifbase.ignore    = "0"
			uci:section("olsrd", "Interface", nil, olsrifbase)
			olsrifbase.Mode = 'ether'
			-- Collect MESH DHCP IP NET
			local client = luci.http.formvalue("cbid.ffwizward.1.client_" .. device)
			if client then
				local dhcpmeshnet = luci.http.formvalue("cbid.ffwizward.1.dhcpmesh_" .. device) and ip.IPv4(luci.http.formvalue("cbid.ffwizward.1.dhcpmesh_" .. device))
				if has_hb then
					local ifacelist = uci:get_list("manager", "heartbeat", "interface") or {}
					table.insert(ifacelist,device .. "dhcp")
					uci:set_list("manager", "heartbeat", "interface", ifacelist)
					uci:save("manager")
				end
				if dhcpmeshnet then
					if not dhcpmeshnet:minhost() or not dhcpmeshnet:mask() then
						dhcpmesh.tag_missing[section] = true
						dhcpmeshnet = nil
						return
					end
					dhcp_ip = dhcpmeshnet:minhost():string()
					dhcp_mask = dhcpmeshnet:mask():string()
					dhcp_network = dhcpmeshnet:network():string()
					uci:section("olsrd", "Hna4", nil, {
						netmask  = dhcp_mask,
						netaddr  = dhcp_network
					})
					uci:foreach("olsrd", "LoadPlugin",
						function(s)		
							if s.library == "olsrd_p2pd.so.0.1.0" then
								uci:set("olsrd", s['.name'], "ignore", "0")
								local nonolsr = uci:get("olsrd", s['.name'], "NonOlsrIf") or ""
								uci:set("olsrd", s['.name'], "NonOlsrIf", device .." ".. nonolsr)
							end
						end)
				else
					local subnet_prefix = tonumber(uci:get("freifunk", community, "splash_prefix")) or 27
					local pool_network = uci:get("freifunk", community, "splash_network") or "10.104.0.0/16"
					local pool = luci.ip.IPv4(pool_network)
					local ip = tostring(node_ip)
					if pool and ip then
						local hosts_per_subnet = 2^(32 - subnet_prefix)
						local number_of_subnets = (2^pool:prefix())/hosts_per_subnet
						local seed1, seed2 = ip:match("(%d+)%.(%d+)$")
						if seed1 and seed2 then
							math.randomseed(seed1 * seed2)
						end
						local subnet = pool:add(hosts_per_subnet * math.random(number_of_subnets))
						dhcp_ip = subnet:network(subnet_prefix):add(1):string()
						dhcp_mask = subnet:mask(subnet_prefix):string()
					end
				end
				if dhcp_ip and dhcp_mask then
					-- Create alias
					local aliasbase = uci:get_all("freifunk", "alias")
					util.update(aliasbase, uci:get_all(external, "alias") or {})
					aliasbase.interface = device
					aliasbase.ipaddr = dhcp_ip
					aliasbase.netmask = dhcp_mask
					aliasbase.proto = "static"
					uci:section("network", "alias", device .. "dhcp", aliasbase)
					-- Create dhcp
					local dhcpbase = uci:get_all("freifunk", "dhcp")
					util.update(dhcpbase, uci:get_all(external, "dhcp") or {})
					dhcpbase.interface = device .. "dhcp"
					dhcpbase.force = 1
					uci:section("dhcp", "dhcp", device .. "dhcp", dhcpbase)
					uci:set_list("dhcp", device .. "dhcp", "dhcp_option", "119,olsr")
					-- Create firewall settings
					uci:delete_all("firewall", "rule", {
						src="freifunk",
						proto="udp",
						dest_port="53"
					})
					uci:section("firewall", "rule", nil, {
						src="freifunk",
						proto="udp",
						dest_port="53",
						target="ACCEPT"
					})
					uci:delete_all("firewall", "rule", {
						src="freifunk",
						proto="udp",
						src_port="68",
						dest_port="67"
					})
					uci:section("firewall", "rule", nil, {
						src="freifunk",
						proto="udp",
						src_port="68",
						dest_port="67",
						target="ACCEPT"
					})
					uci:delete_all("firewall", "rule", {
						src="freifunk",
						proto="tcp",
						dest_port="8082",
					})
					uci:section("firewall", "rule", nil, {
						src="freifunk",
						proto="tcp",
						dest_port="8082",
						target="ACCEPT"
					})
					-- Register splash
					uci:section("luci_splash", "iface", nil, {network=device.."dhcp", zone="freifunk"})
					uci:save("luci_splash")
					-- Make sure that luci_splash is enabled
					sys.init.enable("luci_splash")
				end
			end
			uci:save("wireless")
			uci:save("network")
			uci:save("firewall")
			uci:save("dhcp")
		end
	end)
	--enable radvd
	if has_radvd then
		sys.init.enable("radvd")
	end
	-- Enforce firewall include
	local has_include = false
	uci:foreach("firewall", "include",
		function(section)
			if section.path == "/etc/firewall.freifunk" then
				has_include = true
			end
		end)

	if not has_include then
		uci:section("firewall", "include", nil,
			{ path = "/etc/firewall.freifunk" })
	end
	-- Allow state: invalid packets
	uci:foreach("firewall", "defaults",
		function(section)
			uci:set("firewall", section[".name"], "drop_invalid", "0")
		end)

	-- Prepare advanced config
	local has_advanced = false
	uci:foreach("firewall", "advanced",
		function(section) has_advanced = true end)

	if not has_advanced then
		uci:section("firewall", "advanced", nil,
			{ tcp_ecn = "0", ip_conntrack_max = "8192", tcp_westwood = "1" })
	end
	uci:save("wireless")
	uci:save("network")
	uci:save("firewall")
	uci:save("dhcp")

	local new_hostname = uci:get("freifunk", "wizard", "hostname")
	local old_hostname = sys.hostname()

	if has_hb then
		local dhcphb = hb:formvalue(section)
		if dhcphb then
			uci:set("manager", "heartbeat", "enabled", "1")
			-- Make sure that heartbeat is enabled
			sys.init.enable("machash")
		else
			uci:set("manager", "heartbeat", "enabled", "0")
			-- Make sure that heartbeat is enabled
			sys.init.disable("machash")
		end
		uci:save("manager")
	end

	local custom_hostname = hostname:formvalue(section)
	uci:foreach("system", "system",
		function(s)
			-- Make crond silent
			uci:set("system", s['.name'], "cronloglevel", "10")
			-- Make set timzone and zonename
			uci:set("system", s['.name'], "zonename", "Europe/Berlin")
			uci:set("system", s['.name'], "timezone", 'CET-1CEST,M3.5.0,M10.5.0/3')
			-- Set hostname
			if custom_hostname then
				uci:set("system", s['.name'], "hostname", custom_hostname)
				sys.hostname(custom_hostname)
			else
				if new_hostname then
					if old_hostname == "OpenWrt" or old_hostname:match("^%d+-%d+-%d+-%d+$") then
						uci:set("system", s['.name'], "hostname", new_hostname)
						sys.hostname(new_hostname)
					end
				end
			end
		end)

	-- Create time rdate_servers
	local rdate = uci:get_all("freifunk", "time")
	uci:delete_all("system", "time")
	uci:section("system", "time", "rdate_servers", rdate)
	rdate.server = rdate.rdate_servers
	rdate.rdate_servers = ""
	uci:delete_all("system", "rdate", nil)
	uci:section("system", "rdate", nil, rdate)
	uci:save("system")

	-- Create http splash port 8082
	uci:set_list("uhttpd","main","listen_http",{"80"})
	uci:set_list("uhttpd","main","listen_https",{"443"})
	uci:save("uhttpd")

	-- Read geos
	local latval = tonumber(lat:formvalue(section))
	local lonval = tonumber(lon:formvalue(section))

	-- Save latlon to system too
	if latval and lonval then
		uci:foreach("system", "system", function(s)
			uci:set("system", s[".name"], "latlon",string.format("%.15f %.15f", latval, lonval))
			uci:set("system", s[".name"], "latitude",string.format("%.15f", latval))
			uci:set("system", s[".name"], "longitude",string.format("%.15f", lonval))
		end)
	else
		uci:foreach("system", "system", function(s)
			uci:delete("system", s[".name"], "latlon")
			uci:delete("system", s[".name"], "latitude")
			uci:delete("system", s[".name"], "longitude")
		end)
	end
	-- Delete old watchdog settings
	uci:delete_all("olsrd", "LoadPlugin", {library="olsrd_watchdog.so.0.1"})
	-- Write new watchdog settings
	uci:section("olsrd", "LoadPlugin", nil, {
		library  = "olsrd_watchdog.so.0.1",
		file     = "/var/run/olsrd.watchdog",
		interval = "30"
	})

	-- Delete old nameservice settings
	uci:delete_all("olsrd", "LoadPlugin", {library="olsrd_nameservice.so.0.3"})
	-- Write new nameservice settings
	uci:section("olsrd", "LoadPlugin", nil, {
		library     = "olsrd_nameservice.so.0.3",
		suffix      = "." .. suffix ,
		hosts_file  = "/var/etc/hosts.olsr",
		latlon_file = "/var/run/latlon.js",
		lat         = latval and string.format("%.15f", latval) or "",
		lon         = lonval and string.format("%.15f", lonval) or "",
		services_file = "/var/etc/services.olsr"
	})

	-- Import hosts and set domain
	uci:foreach("dhcp", "dnsmasq", function(s)
		uci:set_list("dhcp", s[".name"], "addnhosts", "/var/etc/hosts.olsr")
		uci:set("dhcp", s[".name"], "local", "/" .. suffix .. "/")
		uci:set("dhcp", s[".name"], "domain", suffix)
	end)

	-- Make sure that OLSR is enabled
	sys.init.enable("olsrd")

	uci:save("olsrd")
	uci:save("dhcp")
	-- Import hosts and set domain
	if has_ipv6 then
	        uci:foreach("dhcp", "dnsmasq", function(s)
	                uci:set_list("dhcp", s[".name"], "addnhosts", {"/var/etc/hosts.olsr","/var/etc/hosts.olsr.ipv6"})
	        end)
	else
	        uci:foreach("dhcp", "dnsmasq", function(s)
	                uci:set_list("dhcp", s[".name"], "addnhosts", "/var/etc/hosts.olsr")
        	end)
	end

	uci:save("dhcp")

	-- Internet sharing
	local share_value = share:formvalue(section)
	if share_value == "1" then
		uci:set("freifunk", "wizard", "netconfig", "1")
		uci:section("firewall", "forwarding", nil, {src="freifunk", dest="wan"})

		if has_autoipv6 then
			-- Set autoipv6 tunnel mode
			uci:set("autoipv6", "olsr_node", "enable", "0")
			uci:set("autoipv6", "tunnel", "enable", "1")
			uci:save("autoipv6")
		end

		-- Delete/Disable gateway plugin
		uci:delete_all("olsrd", "LoadPlugin", {library="olsrd_dyn_gw.so.0.5"})
		uci:delete_all("olsrd", "LoadPlugin", {library="olsrd_dyn_gw_plain.so.0.4"})
		-- Enable gateway_plain plugin
		uci:section("olsrd", "LoadPlugin", nil, {library="olsrd_dyn_gw_plain.so.0.4"})
		sys.exec("chmod +x /etc/init.d/freifunk-p2pblock")
		sys.init.enable("freifunk-p2pblock")
		sys.init.enable("qos")
		sys.exec('grep wan /etc/crontabs/root >/dev/null || echo "0 6 * * * 	ifup wan" >> /etc/crontabs/root')

		if wansec:formvalue(section) == "1" then
			uci:foreach("firewall", "zone",
				function(s)		
					if s.name == "wan" then
						uci:set("firewall", s['.name'], "local_restrict", "1")
						return false
					end
				end)
		end
	else
		uci:set("freifunk", "wizard", "netconfig", "0")
		uci:save("freifunk")
		if has_autoipv6 then
			-- Set autoipv6 olsrd mode
			uci:set("autoipv6", "olsr_node", "enable", "1")
			uci:set("autoipv6", "tunnel", "enable", "0")
			uci:save("autoipv6")
		end
		-- Delete gateway plugins
		uci:delete_all("olsrd", "LoadPlugin", {library="olsrd_dyn_gw.so.0.5"})
		uci:delete_all("olsrd", "LoadPlugin", {library="olsrd_dyn_gw_plain.so.0.4"})
		-- Disable gateway_plain plugin
		uci:section("olsrd", "LoadPlugin", nil, {
			library     = "olsrd_dyn_gw_plain.so.0.4",
			ignore      = 1,
		})
		sys.init.disable("freifunk-p2pblock")
		sys.init.disable("qos")
		sys.exec("chmod -x /etc/init.d/freifunk-p2pblock")
		uci:delete_all("firewall", "forwarding", {src="freifunk", dest="wan"})
		uci:foreach("firewall", "zone",
			function(s)		
				if s.name == "wan" then
					uci:delete("firewall", s['.name'], "local_restrict")
					return false
				end
			end)
	end
	-- Write gvpn dummy interface
	if has_l2gvpn then
		if gvpn then
			local vpn = gvpn:formvalue(section)
			if vpn then
				uci:delete_all("l2gvpn", "l2gvpn")
				uci:delete_all("l2gvpn", "node")
				uci:delete_all("l2gvpn", "supernode")
				-- Write olsr tunnel interface options
				local olsr_gvpnifbase = uci:get_all("freifunk", "olsr_gvpninterface")
				util.update(olsr_gvpnifbase, uci:get_all(external, "olsr_gvpninterface") or {})
				uci:section("olsrd", "Interface", nil, olsr_gvpnifbase)
				local vpnip = gvpnip:formvalue(section)
				local gvpnif = uci:get_all("freifunk", "gvpn_node")
				util.update(gvpnif, uci:get_all(external, "gvpn_node") or {})
				if gvpnif and gvpnif.tundev and vpnip then
					uci:section("network", "interface", gvpnif.tundev, {
						ifname  =gvpnif.tundev ,
						proto   ="static" ,
						ipaddr  =vpnip ,
						netmask =gvpnif.subnet or "255.255.255.192" ,
					})
					gvpnif.ip=""
					gvpnif.subnet=""
					gvpnif.up=""
					gvpnif.down=""
					gvpnif.mac="00:00:48:"..string.format("%X",string.gsub( vpnip, ".*%." , "" ))..":00:00"
					tools.firewall_zone_add_interface("freifunk", gvpnif.tundev)
					uci:section("l2gvpn", "node" , gvpnif.community , gvpnif)
					uci:save("network")
					uci:save("l2gvpn")
					uci:save("firewall")
					uci:save("olsrd")
					sys.init.enable("l2gvpn")
				end
			else
				-- Disable l2gvpn
				sys.exec("/etc/init.d/l2gvpn stop")
				sys.init.disable("l2gvpn")
			end
		end
	end

	uci:save("freifunk")
	uci:save("firewall")
	uci:save("olsrd")
	uci:save("system")
end

return f