summaryrefslogtreecommitdiffstats
path: root/net/travelmate/src/daemon.uc
blob: e017db819a1a3a6b14bc5af5c83a67edcbcba5bc (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
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
// travelmate - connection manager: the run cycle and its system actions,
// the decisions come from the other modules

// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2016-2026 Dirk Brenken <dev@brenken.org>

import { readfile, writefile, rename, unlink, stat, access, mkdir, realpath, popen, open } from 'fs';
import { cursor } from 'uci';
import { connect as ubus_connect } from 'ubus';
import { RUNDIR, FETCH_TIMEOUT, global as cfg_global, typed, words, fetch_args, capture, have } from 'travelmate.common';
import { rank as scan_rank, report as scan_report } from 'travelmate.scan';
import { plan as select_plan, normbssid } from 'travelmate.select';
import { classify as net_classify, debug as net_debug, hostname, MARKER, BODY_MAX } from 'travelmate.net';
import { queries as probe_queries, ready as probe_ready } from 'travelmate.probe';
import { init as check_init, round as check_round } from 'travelmate.check';
import { sta_info, compose as status_compose } from 'travelmate.status';
import { load as uplink_load, ref as uplink_ref, option as uplink_option } from 'travelmate.uplink';
import { plan as vpn_plan, infolist as vpn_infolist, service as vpn_service } from 'travelmate.vpn';

export const FILES = {
	ntplock: RUNDIR + '/travelmate.ntp.lock',
	vpnfile: RUNDIR + '/travelmate.vpn',
	mailfile: RUNDIR + '/travelmate.mail',
	refreshfile: RUNDIR + '/travelmate.refresh',
	pidfile: RUNDIR + '/travelmate.pid',
	scanfile: RUNDIR + '/travelmate.scan',
	rtfile: RUNDIR + '/travelmate.runtime.json',
	revivefile: RUNDIR + '/travelmate.revive',
	rebindfile: RUNDIR + '/travelmate.rebind',
	actionfile: RUNDIR + '/travelmate.action'
};

// the system side, replaceable for tests
export function system_io(log_fn) {
	let conn = null;

	return {
		log: log_fn,
		// libubus calls uloop_init(), which catches SIGTERM/SIGINT; restore
		// the default action so procd can stop the daemon at once
		ubus: () => {
			if (conn == null) {
				conn = ubus_connect();
				signal('SIGTERM', 'default');
				signal('SIGINT', 'default');
			}

			return conn;
		},
		// run directly (no shell), return the exit code; with a timeout in
		// seconds the command is killed when it is over (negative code)
		exec: (argv, timeout) => (timeout > 0) ? system(argv, timeout * 1000) : system(argv),
		capture,
		sleep: (s) => { if (s > 0) sleep(s * 1000); },
		// monotonic seconds, for time budgets
		now: () => clock(true)[0],
		time: () => {
			let t = localtime();

			return sprintf('%04d-%02d-%02d %02d:%02d:%02d', t.year, t.mon, t.mday, t.hour, t.min, t.sec);
		},
		random: (n) => {
			let fh = open('/dev/urandom', 'r');
			let b = fh?.read(n) ?? '';

			fh?.close();

			return b;
		}
	};
};

// rebind allowlist entries per uplink connection, a portal that answers
// every probe with a new host name must not fill the dnsmasq config
const REBIND_MAX = 3;

// messages remembered as 'logged once', they carry ssids and bssids of the
// surroundings and would pile up on a router that travels
const NOTED_MAX = 64;

// seconds a captive portal login script may run, the daemon waits for it
const LOGIN_TIMEOUT = 90;

function dash(v) {
	return length('' + (v ?? '')) ? '' + v : '-';
}

// flags in debug lines
function b(v) {
	return v ? '1' : '0';
}

// interface status in debug lines: true, false, error or empty
function st_text(v) {
	return (v == null) ? '' : '' + v;
}

function is_file(p) {
	return stat(p)?.type == 'file';
}

function is_dir(p) {
	return stat(p)?.type == 'directory';
}

function touch_once(path, text) {
	if (!is_file(path))
		writefile(path, text);
}

// radio, essid and bssid of a 'radio/essid/bssid' station id: radio up to
// the first '/', bssid after the last one, the essid in between
function split_id(id) {
	let first = index(id, '/'), last = rindex(id, '/');
	let head = (last >= 0) ? substr(id, 0, last) : id;
	let i = index(head, '/');

	return {
		radio: (first >= 0) ? substr(id, 0, first) : id,
		essid: (i >= 0) ? substr(head, i + 1) : head,
		bssid: (last >= 0) ? substr(id, last + 1) : id
	};
}

// network address of 'a.b.c.d/len'
function network_of(cidr) {
	let m = match(cidr ?? '', /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)\/([0-9]+)$/);

	if (!m)
		return '';

	let len = +m[5], out = [];

	for (let i = 0; i < 4; i++) {
		let bits = len - i * 8;
		let mask = (bits >= 8) ? 255 : ((bits <= 0) ? 0 : (0xff << (8 - bits)) & 0xff);

		push(out, '' + (+m[i + 1] & mask));
	}

	return join('.', out);
}

export function create(io, files) {
	files ??= FILES;

	// ifstatus: null (unknown), true, false or 'error'; conn: the net
	// state of an up uplink, { result, quality } or null
	let T = {
		action: '', runmode: '', ifstatus: null, conn: null, active: false, wifireload: true,
		radiolist: [], stalist: [], activesta: '', uplinkcfg: '', vpnlist: [],
		subnet: '', subnet_last: '', lannet: '',
		fver: '', bver: '', sysver: '', cores: 1, fetch: [], cfg: cfg_global(cursor()),
		c: null, uplinks: null, mailcmd: false, noted: {}
	};

	let c = () => T.c ??= cursor();
	let uplinks = () => T.uplinks ??= uplink_load(c());
	let cfg = (k) => T.cfg['trm_' + k];

	// connection text like 'net ok/90', the net state alone like 'net ok'
	let conn_text = () => T.conn ? T.conn.result + '/' + (T.conn.quality ?? '') : '';
	let conn_net = () => T.conn?.result ?? '';

	// every change is saved as a delta right away, like the uci cli did
	function uci_set(config, section, option, value) {
		c().set(config, section, option, value);
		c().save(config);
		T.uplinks = null;
	}

	function uci_delete(config, section, option) {
		c().delete(config, section, option);
		c().save(config);
		T.uplinks = null;
	}

	function uci_changes(config) {
		// only a fresh cursor sees the saved deltas
		let ch = cursor().changes(config);

		return length((type(ch) == 'array') ? ch : (ch?.[config] ?? [])) > 0;
	}

	function uci_commit(config) {
		c().commit(config);
		T.uplinks = null;
	}

	function uci_revert(config) {
		c().revert(config);
		T.c = null;
		T.uplinks = null;
	}

	// option of the current uplink, dflt if unset or there is none
	function getval(option, dflt) {
		return uplink_option(uplinks(), T.uplinkcfg, option, dflt ?? '');
	}

	function getcfg(radio, essid, bssid) {
		T.uplinkcfg = uplink_ref(uplinks(), radio ?? '', essid ?? '', bssid ?? '');
	}

	function wireless_status() {
		return io.ubus()?.call('network.wireless', 'status');
	}

	// genstatus is used before its definition
	let genstatus;

	function log(class, msg) {
		if (!length(msg ?? '') || (class == 'debug' && !cfg('debug')))
			return;

		io.log(class, 'trm-' + dash(T.bver), substr(msg, 0, 512));

		if (class == 'err' || class == 'emerg') {
			T.ifstatus = 'error';

			if (length(readfile(files.rtfile) ?? '') && length(T.bver))
				genstatus();

			writefile(files.pidfile, '');
			exit(1);
		}
	}

	// system information, once per daemon run
	function system_info() {
		let pkgs = io.ubus()?.call('rpc-sys', 'packagelist', { all: true })?.packages ?? {};
		let b = io.ubus()?.call('system', 'board') ?? {};
		let r = b.release ?? {};
		let v = (x) => (x == null) ? '' : '' + x;

		T.fver = v(pkgs['luci-app-travelmate']);
		T.bver = v(pkgs['travelmate']);
		T.sysver = sprintf('%s, %s, %s %s (%s)', v(b.model), v(r.target), v(r.distribution), v(r.version), v(r.revision));

		let n = 0;

		for (let l in split(readfile('/proc/cpuinfo') ?? '', '\n'))
			if (match(l, /^processor/) && ++n >= 16)
				break;

		T.cores = (n > 0) ? n : 1;
	}

	// wifi reload, then poll each radio until it is up and not pending
	function wifi() {
		let timeout = 0, radio_up = '', radio = '', maxwait = cfg('maxwait'), over = false;

		io.exec([ 'wifi', 'reload' ]);

		for (radio in T.radiolist) {
			while (true) {
				if (timeout >= maxwait) {
					over = true;
					break;
				}

				let s = wireless_status()?.[radio];

				if (s?.up !== true || s?.pending !== false) {
					if (radio != radio_up) {
						io.exec([ 'wifi', 'up', radio ]);
						radio_up = radio;
					}

					timeout++;
					io.sleep(1);
				}
				else {
					break;
				}
			}

			if (over)
				break;
		}

		if (timeout < maxwait) {
			io.sleep(int(maxwait / 6));
			timeout += int(maxwait / 6);
		}

		log('debug', sprintf('f_wifi      ::: radio_list: %s, ssid_filter: %s, radio: %s, timeout: %d',
			join(' ', T.radiolist), dash(join(' ', cfg('ssidfilter'))), radio, timeout));
	}

	// interfaces via netifd, like ifdown and ifup (which reloads the network
	// config first, so a removed interface comes back)
	function if_down(iface) {
		io.ubus()?.call('network.interface', 'down', { interface: iface });
	}

	function if_up(iface) {
		io.ubus()?.call('network', 'reload');
		io.ubus()?.call('network.interface', 'down', { interface: iface });
		io.ubus()?.call('network.interface', 'up', { interface: iface });
	}

	function if_remove(iface) {
		io.ubus()?.call('network.interface.' + iface, 'remove');
	}

	// true, false or null if the interface is unknown
	function iface_up(iface) {
		let v = io.ubus()?.call('network.interface.' + iface, 'status')?.up;

		return (type(v) == 'bool') ? v : null;
	}

	// objects already on ubus need no 'ubus wait_for' process
	function wait_for(timeout, ...objs) {
		let have_all = true;

		for (let o in objs) {
			let l = null;

			try { l = io.ubus()?.list(o); } catch (e) { }

			if (!length(l)) {
				have_all = false;
				break;
			}
		}

		return have_all || io.exec([ 'ubus', '-t', '' + timeout, 'wait_for', ...objs ]) == 0;
	}

	// layer 3 device of an interface, '' if unknown
	function iface_device(iface) {
		let s = io.ubus()?.call('network.interface.' + iface, 'status');

		return '' + (s?.l3_device ?? s?.device ?? '');
	}

	// the curl status probe of the vpn handling, bound to the vpn device
	// instead of the uplink device: the request has to pass the tunnel;
	// without curl retry where the caller probes in a loop anyway
	function vpn_net(iface, retry) {
		let device = iface_device(iface);

		// unbound the request would take the uplink and report a tunnel
		// that is not there
		if (!length(device))
			return 'no device';

		let rc = io.capture([ 'curl', ...fetch_args(T.cfg, device), '--user-agent', cfg('useragent'),
			'--header', 'Cache-Control: no-cache, no-store, must-revalidate, max-age=0',
			...(retry ? [] : [ '--retry', '0' ]),
			'--write-out', '%{response_code}', '--output', '/dev/null', '--url', cfg('captiveurl') ]).out;

		return (rc == '200' || rc == '204') ? 'net ok' : rc;
	}

	// start, keep or stop the vpn connection of an uplink
	function vpn_program(action, iface, service) {
		let maxwait = cfg('maxwait');

		if (action == 'disable') {
			if_down(iface);
			if_remove(iface);
			unlink(files.vpnfile);
			log('info', sprintf("%s client connection disabled '%s'", service, iface));

			return 0;
		}

		let status = (action == 'enable_keep') ? iface_up(iface) : null;

		log('debug', sprintf('f_vpnpgm    ::: action: %s, interface: %s, status: %s',
			action, iface, dash(st_text(status))));

		if (status === true)
			return 0;

		if_down(iface);
		if_remove(iface);
		io.sleep(1);
		if_up(iface);

		if (!wait_for(int(maxwait / 6), 'network.interface.' + iface))
			log('info', sprintf("travelmate vpn interface '%s' does not appear on ubus on ifup event", iface));

		// a time budget, not a number of rounds: a probe into a dead tunnel
		// takes its full timeout, an interface that is not up yet a second
		let net_status = '', start = io.now();

		while (true) {
			if (iface_up(iface) === true) {
				net_status = vpn_net(iface);

				if (net_status == 'net ok') {
					writefile(files.vpnfile, '');
					log('info', sprintf("%s client connection enabled '%s'", service, iface));

					return 0;
				}
			}

			if (io.now() - start >= maxwait) {
				if_down(iface);
				if_remove(iface);
				unlink(files.vpnfile);
				log('info', sprintf("%s client connection can't be established '%s' (rc: %s)", service, iface, dash(net_status)));

				return 1;
			}

			io.sleep(1);
		}
	}

	function vpn_handle(action) {
		let rc = null;
		let up = { vpn: getval('vpn') == '1', iface: getval('vpniface'), service: '' };

		if (length(up.iface))
			up.service = vpn_service(c().get_all('network', up.iface));

		let p = vpn_plan({
			vpn: cfg('vpn'), list: T.vpnlist, action, connection: conn_net() == 'net ok',
			connected: is_file(files.vpnfile), ntp: is_dir(files.ntplock)
		}, up, (iface) => iface_up(iface) === true);

		for (let s in p.steps) {
			switch (s.op) {
			case 'down':
				if_down(s.iface);

				if (s.why == 'initial')
					if_remove(s.iface);

				log('info', sprintf("take down vpn interface '%s' (%s)", s.iface, (s.why == 'initial') ? 'cleanup' : 'uplink switch'));
				break;

			case 'clear':
				unlink(files.vpnfile);
				io.sleep(1);
				break;

			case 'rc':
				rc = 1;
				break;

			case 'pgm':
				rc = vpn_program(s.action, s.iface, s.service);
				break;

			// the tunnel of an established vpn connection: set it up again if
			// the interface is gone or the probe (with retry) gets no reply
			case 'check':
				let state = (iface_up(s.iface) === true) ? vpn_net(s.iface, true) : 'interface down';

				if (state != 'net ok') {
					log('info', sprintf("%s client connection lost '%s' (rc: %s)", s.service, s.iface, dash(state)));
					rc = vpn_program('enable', s.iface, s.service);
				}
				break;
			}
		}

		if (p.pgm && rc != null)
			genstatus();

		log('debug', sprintf('f_vpn       ::: vpn: %s, enabled: %s, action: %s, vpn_service: %s, vpn_iface: %s, vpn_infolist: %s, connection: %s, rc: %s',
			b(cfg('vpn')), b(up.vpn), action, dash(up.service), dash(up.iface),
			dash(join(' ', T.vpnlist)), conn_net(), dash(rc)));
	}

	// a random unicast LAA mac, second nibble 2, 6, A or E
	function random_mac() {
		let r = io.random(7), out = [];

		for (let i = 0; i < 6; i++)
			push(out, sprintf('%02X', ord(r, i) ?? 0));

		out[0] = substr(out[0], 0, 1) + [ '2', '6', 'A', 'E' ][(ord(r, 6) ?? 0) % 4];

		return join(':', out);
	}

	function sta_macaddr() {
		for (let radio, r in (wireless_status() ?? {}))
			for (let i in (r?.interfaces ?? []))
				if (i?.config?.mode == 'sta' && i?.config?.macaddr != null)
					return '' + i.config.macaddr;

		return '';
	}

	function mac_set(section) {
		let macaddr = getval('macaddr'), result;

		if (length(macaddr)) {
			result = macaddr;
			uci_set('wireless', section, 'macaddr', result);
		}
		else if (cfg('randomize')) {
			result = random_mac();
			uci_set('wireless', section, 'macaddr', result);
		}
		else {
			if (c().get('wireless', section, 'macaddr') != null)
				uci_delete('wireless', section, 'macaddr');

			result = sta_macaddr();
		}

		log('debug', sprintf('f_mac       ::: action: set, section: %s, macaddr: %s, result: %s',
			dash(section), dash(macaddr), dash(result)));

		return result;
	}

	function network_dump() {
		return io.ubus()?.call('network.interface', 'dump')?.interface ?? [];
	}

	// default gateway of the first interface with a default route, per family
	function getgw() {
		let dump = network_dump(), gw = {};

		for (let fam in [ [ '0.0.0.0', 4 ], [ '::', 6 ] ]) {
			let wan = null;

			for (let i in dump)
				for (let r in (i.route ?? []))
					if (r.target == fam[0] && r.table == null && wan == null)
						wan = i;

			gw[fam[1]] = '';

			for (let r in (wan?.route ?? []))
				if (r.target == fam[0] && r.table == null && r.nexthop != null) {
					gw[fam[1]] = '' + r.nexthop;
					break;
				}
		}

		let result = length(gw[4]) > 0 || length(gw[6]) > 0;

		log('debug', sprintf('f_getgw     ::: wan4_gw: %s, wan6_gw: %s, result: %s', dash(gw[4]), dash(gw[6]), '' + result));

		return result;
	}

	function subnet_of(dump, iface) {
		for (let i in dump)
			if (i.interface == iface) {
				let a = i['ipv4-address']?.[0];

				return (a?.address != null) ? network_of(a.address + '/' + a.mask) : '';
			}

		return '';
	}

	function subnet() {
		let conn_state = conn_net();

		if (conn_state == T.subnet_last && length(T.subnet))
			return;

		let dump = network_dump();
		let wan = subnet_of(dump, length(cfg('iface')) ? cfg('iface') : 'trm_wwan');

		if (!length(T.lannet))
			T.lannet = subnet_of(dump, length(cfg('laniface')) ? cfg('laniface') : 'lan');

		if (length(T.lannet) && length(wan) && T.lannet == wan)
			log('info', sprintf("uplink network '%s' conflicts with router LAN network, please adjust your network settings", wan));

		T.subnet = sprintf('%s (lan: %s)', dash(wan), dash(T.lannet));
		T.subnet_last = conn_state;

		log('debug', sprintf('f_subnet    ::: lan: %s, wan: %s', dash(T.lannet), dash(wan)));
	}

	function runtime() {
		let rt = null;

		try { rt = json(readfile(files.rtfile) ?? ''); } catch (e) { }

		return (type(rt?.data) == 'object') ? rt.data : null;
	}

	function getstatus_text() {
		let data = runtime();

		if (!data)
			return '::: no travelmate runtime information available\n';

		let out = '::: travelmate runtime information\n';

		for (let k, v in data)
			out += sprintf('  + %-18s : %s\n', k, v);

		return out;
	}

	function mail(station_id) {
		let board = io.ubus()?.call('system', 'board') ?? {};
		let text = null, topic = cfg('mailtopic');

		if (!length(topic))
			topic = sprintf("travelmate connection to '%s'", station_id);

		// a mail header line, no control characters from an ssid
		topic = replace(topic, /[[:cntrl:]]/g, ' ');

		// the template is ucode that runs in the daemon, like the login
		// scripts it has to be located in /etc/travelmate
		let template = realpath(cfg('mailtemplate')) ?? '';

		if (is_file(template) && substr(template, 0, 16) != '/etc/travelmate/') {
			log('info', 'mail template rejected, it has to be located in /etc/travelmate');
		}
		else if (is_file(template)) {
			try {
				text = render(template, {
					banner: readfile('/etc/banner') ?? '', board,
					status: getstatus_text(), station_id
				});
			}
			catch (e) {
				log('info', sprintf('mail template error: %s', e.message));
			}
		}
		else {
			log('info', 'no mail template');
		}

		if (!length(text ?? ''))
			log('info', 'no mail content');

		let head = sprintf('From: %s\nTo: %s\nSubject: %s\nReply-to: %s\nMime-Version: 1.0\nContent-Type: text/html;charset=utf-8\nContent-Disposition: inline\n\n',
			cfg('mailsender'), cfg('mailreceiver'), topic, cfg('mailsender'));
		let argv = [ 'msmtp', '--timeout=10' ];

		if (cfg('debug'))
			push(argv, '--debug');

		push(argv, '-a', cfg('mailprofile'), cfg('mailreceiver'));

		let p = popen(argv, 'w');
		let rc = -1;

		if (p) {
			p.write(head + (text ?? ''));
			rc = p.close();
		}

		log('debug', sprintf('f_mail      ::: template: %s, profile: %s, receiver: %s, rc: %s',
			cfg('mailtemplate'), cfg('mailprofile'), cfg('mailreceiver'), '' + rc));
	}

	genstatus = function() {
		if (T.ifstatus !== true)
			T.conn = null;

		subnet();

		let st = {
			ifstatus: T.ifstatus, connection: conn_text(), active: T.active, runmode: T.runmode,
			subnet: T.subnet, fver: T.fver, bver: T.bver, sysver: T.sysver, cores: T.cores, fetch: 'curl',
			vpn: cfg('vpn'), vpnlist: T.vpnlist, mail: cfg('mail'),
			mailcmd: T.mailcmd, mailreceiver: cfg('mailreceiver'),
			uplinkcfg: T.uplinkcfg, captive: cfg('captive'), proactive: cfg('proactive'),
			netcheck: cfg('netcheck'), autoadd: cfg('autoadd'), randomize: cfg('randomize'), eviltwin: cfg('eviltwin')
		};
		let m = match(readfile('/proc/meminfo') ?? '', /MemAvailable:[ \t]*([0-9]+)/);
		let r = status_compose(st, (T.ifstatus === true) ? sta_info(wireless_status()) : null, uplinks(),
			(section) => c().get('wireless', section, 'device'),
			is_dir(files.ntplock), is_file(files.mailfile), is_file(files.vpnfile),
			m ? sprintf('%.2f', +m[1] / 1024) : '', io.time());
		let tmp = files.rtfile + '.tmp';

		if (writefile(tmp, sprintf('%J\n', { data: r.data })) == null || !rename(tmp, files.rtfile))
			unlink(tmp);

		if (r.uplinkcfg != null)
			T.uplinkcfg = r.uplinkcfg;

		if (r.mail) {
			writefile(files.mailfile, '');
			mail(r.data.station_id);
		}

		log('debug', 'f_genstatus ::: ' + r.debug);
	};

	// the internet and captive portal probe, net_classify() result
	function net() {
		let raw = io.capture([ 'curl', ...T.fetch, '--user-agent', cfg('useragent'),
			'--header', 'Cache-Control: no-cache, no-store, must-revalidate, max-age=0',
			'--max-filesize', '' + BODY_MAX, '--write-out', '\\n' + MARKER + '%{json}', '--url', cfg('captiveurl') ]).out;
		let r = net_classify(raw, cfg('captiveurl'), cfg('captive'));

		log('debug', sprintf('f_net       ::: timeout: %d, %s', FETCH_TIMEOUT, net_debug(r)));

		return r;
	}

	// the rebind allowlist entries travelmate added for the current uplink
	function rebind_read() {
		let l = null;

		try { l = json(readfile(files.rebindfile) ?? ''); } catch (e) { }

		return (type(l) == 'array') ? filter(l, (d) => type(d) == 'string') : [];
	}

	function rebind_section() {
		let sid = null;

		c().foreach('dhcp', 'dnsmasq', (s) => { sid ??= s['.name']; });

		return sid;
	}

	// add the captive portal domain to the dnsmasq rebind allowlist
	function rebind_allow(domain) {
		// only a dns name, an address or a malformed host never goes into
		// the dnsmasq config
		if (!hostname(domain))
			return false;

		let sid = rebind_section();

		if (sid == null)
			return false;

		let l = c().get('dhcp', sid, 'rebind_domain');

		l = (type(l) == 'array') ? l : words(l);

		if (domain in l)
			return false;

		let added = rebind_read();

		if (length(added) >= REBIND_MAX) {
			log('debug', sprintf("f_rebind    ::: limit of %d rebind allowlist entries reached, domain '%s' skipped", REBIND_MAX, domain));
			return false;
		}

		c().set('dhcp', sid, 'rebind_domain', [ ...l, domain ]);
		c().save('dhcp');

		if (uci_changes('dhcp'))
			uci_commit('dhcp');

		push(added, domain);
		writefile(files.rebindfile, sprintf('%J\n', added));
		io.exec([ '/etc/init.d/dnsmasq', 'reload' ]);
		log('info', sprintf("captive portal domain '%s' added to dhcp rebind allowlist", domain));

		return true;
	}

	// remove the entries of the previous uplink, an entry that was already
	// there before travelmate came along is not tracked and stays
	function rebind_drop() {
		let added = rebind_read();

		if (!length(added))
			return;

		let sid = is_file('/etc/config/dhcp') ? rebind_section() : null;

		if (sid != null) {
			let l = c().get('dhcp', sid, 'rebind_domain');

			l = (type(l) == 'array') ? l : words(l);

			let keep = filter(l, (d) => !(d in added));

			if (length(keep) != length(l)) {
				if (length(keep))
					c().set('dhcp', sid, 'rebind_domain', keep);
				else
					c().delete('dhcp', sid, 'rebind_domain');

				c().save('dhcp');

				if (uci_changes('dhcp'))
					uci_commit('dhcp');

				io.exec([ '/etc/init.d/dnsmasq', 'reload' ]);
				log('info', sprintf("captive portal domain(s) '%s' removed from dhcp rebind allowlist", join(' ', filter(l, (d) => d in added))));
			}
		}

		unlink(files.rebindfile);
	}

	// login script: '/etc/travelmate/*.uc', executable, credentials as args
	function login(domain) {
		let script = getval('script');

		if (!length(script))
			return null;

		script = realpath(script) ?? '';

		if (substr(script, 0, 16) != '/etc/travelmate/' || !match(script, /\.uc$/)) {
			log('info', 'captive portal login script rejected, it has to be a ucode script /etc/travelmate/*.uc');
			return null;
		}

		if (!is_file(script) || access(script, 'x') != true)
			return null;

		let rc = io.exec([ script, ...words(getval('script_args')) ], LOGIN_TIMEOUT);

		if (rc == -9)
			log('info', sprintf("captive portal login script for '%s' has been killed, it ran longer than %d seconds", domain, LOGIN_TIMEOUT));
		else
			log('info', sprintf("captive portal login script for '%s' has been finished with rc '%d'", domain, rc));

		return rc;
	}

	// the net and captive portal check of an up uplink, ends the poll
	function check_net(status, sta_id, quality) {
		let r = net();

		if (cfg('captive')) {
			let domain;

			while (true) {
				domain = r.domain;

				if (access('/etc/init.d/dnsmasq', 'x') != true || !is_file('/etc/config/dhcp') || !length(domain))
					break;

				if (!rebind_allow(domain))
					break;

				r = net();
			}

			if (length(domain)) {
				T.conn = { result: r.result, quality };
				genstatus();

				if (login(domain) === 0)
					r = net();
			}
		}

		if (r.net == 'nok' && cfg('netcheck'))
			r = net();

		if (r.net == 'nok') {
			vpn_handle('disable');

			if (cfg('netcheck')) {
				log('info', sprintf("uplink has no internet '%s'", sta_id));
				T.conn = null;
				T.ifstatus = status;
				genstatus();

				return;
			}
		}

		T.conn = { result: r.result, quality };
		genstatus();
	}

	function station_id() {
		return '' + (runtime()?.station_id ?? '');
	}

	// the current station from the runtime file, or from the wireless status
	// if a restart cleared the runtime file
	function station() {
		let p = split_id(station_id());

		p.bssid = replace(p.bssid, /-/g, '');

		if (length(p.radio) && p.radio != '-')
			return p;

		let sta = sta_info(wireless_status());
		let radio = sta ? '' + (c().get('wireless', sta.section, 'device') ?? '') : '';

		if (!length(radio) || !length(sta.essid))
			return p;

		return { radio, essid: sta.essid, bssid: normbssid(sta.bssid) };
	}

	// mode: initial, dev, sta or rev; status: the expected interface status
	function check(mode, status, sta_radio, sta_essid, sta_bssid) {
		let ifname = '', quality = null, wait = 0, maxwait = cfg('maxwait');

		if (mode == 'initial' || mode == 'dev') {
			let p = station();

			sta_radio = p.radio;
			sta_essid = p.essid;
			sta_bssid = p.bssid;
		}

		sta_radio ??= '';
		sta_essid ??= '';
		sta_bssid ??= '';

		let sta_id = sprintf('%s/%s/%s', dash(sta_radio), dash(sta_essid), dash(sta_bssid));
		let init = check_init({ mode, status, radio: sta_radio, essid: sta_essid, bssid: sta_bssid,
			ifstatus: T.ifstatus, wifireload: T.wifireload }, uplinks());
		let enabled = init.enabled;

		T.uplinkcfg = init.uplinkcfg;

		if (init.wifi)
			wifi();

		if (mode == 'sta') {
			io.ubus()?.call('network.interface.' + cfg('iface'), 'down');
			io.ubus()?.call('network.interface.' + cfg('iface'), 'up');

			if (!wait_for(int(maxwait / 6), 'network.interface.' + cfg('iface')))
				log('info', sprintf("travelmate interface '%s' does not appear on ubus on ifup event", cfg('iface')));

			io.sleep(1);
		}

		while (wait <= maxwait) {
			if (wait > 0)
				io.sleep(1);

			wait++;

			let next = null;
			let steps = check_round({
				mode, status, enabled, ifstatus: T.ifstatus, connection: T.conn != null,
				wait, maxwait, minquality: cfg('minquality'), quality, sta_id
			}, probe_queries(io.ubus(), cfg('iface')));

			for (let s in steps) {
				switch (s.op) {
				case 'probe': ifname = s.ifname; quality = s.quality; break;
				case 'ifstatus': T.ifstatus = s.value; break;
				case 'log': log(s.class, s.msg); break;
				case 'vpn': vpn_handle('disable'); break;
				case 'conn': T.conn = null; break;
				case 'genstatus': genstatus(); break;
				case 'sleep': io.sleep(s.s); break;
				default: next = s.op;
				}
			}

			if (next == 'break')
				break;

			if (next == 'continue')
				continue;

			if (next == 'net') {
				check_net(status, sta_id, quality);
				break;
			}
		}

		log('debug', sprintf('f_check     ::: mode: %s, sta_id: %s, name: %s, status: %s, enabled: %s, connection: %s, wait: %d, max_wait: %d, min_quality/quality: %d/%s, captive: %s, netcheck: %s',
			mode, sta_id, dash(ifname), st_text(T.ifstatus), b(enabled), dash(conn_text()), wait, maxwait,
			cfg('minquality'), dash(quality), b(cfg('captive')), b(cfg('netcheck'))));
	}

	// revive list: [ { cnt, rounds, radio, bssid, essid }, ... ]
	function revive_read() {
		let l = null;

		try { l = json(readfile(files.revivefile) ?? ''); } catch (e) { }

		return (type(l) == 'array') ? l : [];
	}

	function revive_write(list) {
		writefile(files.revivefile + '.tmp', sprintf('%J\n', list));
		rename(files.revivefile + '.tmp', files.revivefile);
	}

	function reviveload(radio, essid, bssid) {
		let revive = int(replace(getval('revive', '0'), /[^0-9]/g, '') || '0'), found = false;

		if (!(revive > 0))
			return;

		if (revive < 10)
			revive = 10;

		let list = revive_read();

		for (let e in list) {
			if (e.radio == radio && e.essid == essid && e.bssid == bssid) {
				found = true;
				e.rounds++;

				if (e.rounds > cfg('maxretry')) {
					e.cnt = 0;
					log('info', sprintf("uplink stays disabled '%s/%s/%s', revive limit reached", radio, essid, dash(bssid)));
				}
				else {
					e.cnt = revive;
				}
			}
		}

		if (!found)
			push(list, { cnt: revive, rounds: 1, radio, bssid, essid });

		revive_write(list);

		log('debug', sprintf('f_reviveload ::: radio: %s, essid: %s, bssid: %s, revive: %d, max_rounds: %d',
			radio, essid, dash(bssid), revive, cfg('maxretry')));
	}

	// a working uplink starts over, its revive rounds are dropped
	function revive_drop(radio, essid, bssid) {
		let list = revive_read();
		let out = filter(list, (e) => !(e.radio == radio && e.essid == essid && e.bssid == bssid));

		if (length(out) != length(list))
			revive_write(out);
	}

	// on service stop: the revive entries of uplinks that are still disabled
	// survive a restart, a manually re-enabled uplink starts over
	function revive_prune() {
		let list = revive_read();
		let out = filter(list, (e) => uplink_option(uplinks(),
			uplink_ref(uplinks(), e.radio, e.essid, e.bssid), 'enabled') == '0');

		if (!length(out))
			unlink(files.revivefile);
		else if (length(out) != length(list))
			revive_write(out);
	}

	function revive() {
		let list = revive_read(), out = [];

		if (!length(list))
			return;

		for (let e in list) {
			if (e.cnt > 0 && --e.cnt == 0) {
				getcfg(e.radio, e.essid, e.bssid);

				if (!length(T.uplinkcfg))
					continue;

				if (getval('enabled') == '0') {
					uci_set('travelmate', T.uplinkcfg, 'enabled', '1');
					uci_commit('travelmate');
					touch_once(files.refreshfile, 'cfg_reload');
					log('info', sprintf("uplink has been re-enabled '%s/%s/%s' (%d/%d)", e.radio, e.essid, dash(e.bssid), e.rounds, cfg('maxretry')));
				}
			}

			push(out, e);
		}

		revive_write(out);
	}

	function setdev(radio) {
		let match_ = length(cfg('radio')) ? (radio in cfg('radio')) : !(radio in T.radiolist);
		let disabled = '';

		if (match_) {
			if (cfg('revradio'))
				unshift(T.radiolist, radio);
			else
				push(T.radiolist, radio);

			disabled = '' + (c().get('wireless', radio, 'disabled') ?? '');

			if (disabled == '1')
				uci_set('wireless', radio, 'disabled', '0');
		}

		log('debug', sprintf('f_setdev    ::: device: %s, radio: %s, radio_list: %s, disabled: %s',
			dash(radio), dash(join(' ', cfg('radio'))), dash(join(' ', T.radiolist)), dash(disabled)));
	}

	function setif(section, proactive) {
		let get = (o) => '' + (c().get('wireless', section, o) ?? '');
		let radio = get('device');

		if (!(radio in T.radiolist))
			return;

		let mode = get('mode'), essid = get('ssid'), bssid = get('bssid'), disabled = get('disabled');

		getcfg(radio, essid, bssid);

		let enabled = getval('enabled', '0') == '1';

		if (mode == 'sta') {
			if (!enabled || ((disabled == '' || disabled == '0') && (!proactive || T.ifstatus !== true))) {
				uci_set('wireless', section, 'disabled', '1');
			}
			else if (disabled == '0' && T.ifstatus === true && proactive) {
				if (!length(T.activesta))
					T.activesta = section;
				else
					uci_set('wireless', section, 'disabled', '1');
			}

			if (enabled)
				push(T.stalist, { section, radio });
		}

		log('debug', sprintf('f_setif     ::: uplink_config: %s, section: %s, enabled: %s, active_sta: %s',
			dash(T.uplinkcfg), section, b(enabled), dash(T.activesta)));
	}

	// autoadd an open uplink, returns its wifi-iface section or null
	function addsta(radio, essid) {
		let new_uplink = true, offset = 1, cnt = null, out = null;

		c().foreach('wireless', 'wifi-iface', (s) => {
			if (s.ssid == essid && s.device == radio)
				new_uplink = false;
			else
				offset++;
		});

		if (new_uplink) {
			let k = offset + 1;

			while (c().get('wireless', 'trm_uplink' + k) != null)
				k++;

			let wifi_cfg = 'trm_uplink' + k;

			c().set('wireless', wifi_cfg, 'wifi-iface');

			for (let kv in [ [ 'mode', 'sta' ], [ 'network', cfg('iface') ], [ 'device', radio ],
					[ 'ssid', essid ], [ 'encryption', 'none' ], [ 'disabled', '1' ] ])
				c().set('wireless', wifi_cfg, kv[0], kv[1]);

			let trm_cfg = c().add('travelmate', 'uplink');

			c().set('travelmate', trm_cfg, 'device', radio);
			c().set('travelmate', trm_cfg, 'ssid', essid);
			c().set('travelmate', trm_cfg, 'opensta', '1');
			c().set('travelmate', trm_cfg, 'enabled', '1');

			if (length(cfg('stdvpniface'))) {
				c().set('travelmate', trm_cfg, 'vpniface', cfg('stdvpniface'));
				c().set('travelmate', trm_cfg, 'vpn', '1');
			}

			cnt = typed('int', c().get('travelmate', 'global', 'trm_autoaddcnt'), 0) + 1;
			c().set('travelmate', 'global', 'trm_autoaddcnt', '' + cnt);
			c().save('travelmate');
			c().save('wireless');

			if (uci_changes('travelmate'))
				uci_commit('travelmate');

			if (uci_changes('wireless'))
				uci_commit('wireless');

			T.uplinks = null;
			wifi();
			touch_once(files.refreshfile, 'ui_reload');
			log('info', sprintf("open uplink '%s/%s' added to wireless config", radio, essid));
			out = wifi_cfg;
		}

		log('debug', sprintf('f_addsta    ::: radio: %s, essid: %s, autoaddcnt/maxautoadd: %d/%d, new_uplink: %d, offset: %d',
			dash(radio), essid, cnt ?? cfg('autoaddcnt'), cfg('maxautoadd'), new_uplink ? 1 : 0, offset));

		return out;
	}

	// connect the station ctx { section, radio, essid, bssid }, after
	// terminating the current uplink cur { radio, essid, bssid }
	function connect_sta(ctx, cur) {
		if (cur && length(cur.radio)) {
			vpn_handle('disable');
			uci_set('wireless', T.activesta, 'disabled', '1');

			if (uci_changes('wireless'))
				uci_commit('wireless');

			check('rev', false);
			log('info', sprintf("uplink connection terminated '%s/%s/%s'", cur.radio, cur.essid, dash(cur.bssid)));
			cur.radio = cur.essid = cur.bssid = '';
		}

		rebind_drop();

		let maxretry = cfg('maxretry'), retry_display = (maxretry == 0) ? '-' : '' + maxretry;
		let sta = sprintf('%s/%s/%s', ctx.radio, ctx.essid, dash(ctx.bssid));

		getcfg(ctx.radio, ctx.essid, ctx.bssid);

		for (let retry = 1; maxretry == 0 || retry <= maxretry; retry++) {
			let mac = mac_set(ctx.section);

			uci_set('wireless', ctx.section, 'disabled', '0');
			check('sta', false, ctx.radio, ctx.essid, ctx.bssid);

			if (T.ifstatus === true) {
				unlink(files.mailfile);

				if (uci_changes('wireless'))
					uci_commit('wireless');

				log('info', sprintf("connected to uplink '%s' with mac '%s' (%d/%s)", sta, dash(mac), retry, retry_display));
				revive_drop(ctx.radio, ctx.essid, ctx.bssid);
				vpn_handle('enable');

				return true;
			}

			uci_revert('wireless');
			check('rev', false, ctx.radio, ctx.essid, ctx.bssid);

			if (retry == maxretry) {
				// without revive the uplink stays off, tell how to get it back
				let hint = (getval('revive', '0') == '0') ? ", re-enable it in LuCI or set 'revive'" : '';

				if (length(T.uplinkcfg)) {
					uci_set('travelmate', T.uplinkcfg, 'enabled', '0');
					uci_commit('travelmate');
					touch_once(files.refreshfile, 'cfg_reload');
					reviveload(ctx.radio, ctx.essid, ctx.bssid);
				}

				log('info', sprintf("uplink has been disabled '%s' (%d/%s)%s", sta, retry, retry_display, hint));

				return false;
			}

			genstatus();
			log('info', sprintf("can't connect to uplink '%s' (%d/%s)", sta, retry, retry_display));
			io.sleep(int(cfg('maxwait') / 6));
		}

		return false;
	}

	function select_cfg() {
		return {
			minquality: cfg('minquality'), autoadd: cfg('autoadd'), eviltwin: cfg('eviltwin'),
			maxautoadd: cfg('maxautoadd'), autoaddcnt: typed('int', c().get('travelmate', 'global', 'trm_autoaddcnt'), 0),
			ssidfilter: cfg('ssidfilter')
		};
	}

	function select_ifaces() {
		let out = {};

		c().foreach('wireless', 'wifi-iface', (s) => {
			out[s['.name']] = { device: s.device, ssid: s.ssid, bssid: s.bssid };
		});

		return out;
	}

	// one run cycle
	function main() {
		let cur = { radio: '', essid: '', bssid: '' };
		let proactive = cfg('proactive');

		T.active = true;
		revive();
		check('initial', false);

		if (!proactive)
			vpn_handle((conn_net() == 'net ok') ? 'enable_keep' : 'disable');

		log('debug', sprintf('f_main-1    ::: status: %s, connection: %s, proactive: %s',
			st_text(T.ifstatus), conn_net(), b(proactive)));

		if (T.ifstatus !== true || proactive) {
			c().foreach('wireless', 'wifi-iface', (s) => setif(s['.name'], proactive));

			if (T.ifstatus === true && length(T.activesta) && proactive) {
				let p = station();

				cur.radio = p.radio;
				cur.essid = p.essid;
				cur.bssid = normbssid(p.bssid);
				check('dev', true);
				log('debug', sprintf('f_main-2    ::: config_radio: %s, config_essid: "%s", config_bssid: %s',
					cur.radio, cur.essid, dash(cur.bssid)));
			}
			else {
				if (uci_changes('wireless')) {
					uci_commit('wireless');
					T.wifireload = true;
				}
				else if (!T.wifireload && !probe_ready(io.ubus(), T.radiolist)) {
					T.wifireload = true;
				}

				log('debug', 'f_main-2    ::: wifi_reload: ' + b(T.wifireload));
				check('dev', false);
				T.wifireload = false;
			}

			log('debug', sprintf('f_main-3    ::: radio_list: %s, sta_list: %s',
				dash(join(' ', T.radiolist)), dash(join(' ', map(T.stalist, (s) => s.section + '-' + s.radio)))));

			for (let radio in T.radiolist) {
				if (!length(filter(T.stalist, (s) => s.radio == radio)) && !cfg('autoadd'))
					continue;

				let steps = select_plan({
					radio, stalist: T.stalist,
					current: (T.conn && length(cur.radio)) ? cur : null,
					cfg: select_cfg(), ifaces: select_ifaces(),
					scan: () => scan_rank(io.ubus()?.call('iwinfo', 'scan', { device: radio })?.results)
				});
				let ctx = { section: '', radio: '', bssid: '', essid: '' }, skip = false;

				for (let s in steps) {
					switch (s.op) {
					case 'log':
						if (s.once && T.noted[s.msg])
							log('debug', s.msg);
						else {
							if (s.once) {
								if (length(T.noted) >= NOTED_MAX)
									T.noted = {};

								T.noted[s.msg] = true;
							}

							log(s.class, s.msg);
						}
						break;

					case 'noscan':
						log('info', sprintf("no scan results on '%s'", s.radio));
						T.wifireload = true;
						break;

					case 'sta':
						ctx = { section: s.section, radio: s.radio, bssid: s.bssid, essid: s.essid };
						skip = false;
						getcfg(ctx.radio, ctx.essid, ctx.bssid);
						break;

					case 'keep':
						vpn_handle('enable_keep');
						log('debug', sprintf('f_main-4    ::: config_radio: %s, config_essid: %s, config_bssid: %s',
							cur.radio, cur.essid, dash(cur.bssid)));
						return;

					case 'add':
						let section = addsta(s.radio, s.essid);

						if (section) {
							ctx = { section, radio: s.radio, bssid: '', essid: s.essid };
						}
						else {
							skip = true;
							log('info', sprintf("open uplink could not be added '%s/%s'", s.radio, s.essid));
						}
						break;

					case 'try':
						if (!skip && connect_sta(ctx, cur))
							return;

						skip = false;
						break;
					}
				}
			}
		}

		T.active = false;

		if (T.ifstatus !== true && T.ifstatus != 'error')
			genstatus();
	}

	// config and runtime checks before each cycle, false if travelmate has to stop
	function conf() {
		T.c = null;
		T.uplinks = null;
		T.cfg = cfg_global(c());
		T.radiolist = [];
		T.stalist = [];
		T.activesta = '';
		T.uplinkcfg = '';

		if (length(T.action))
			T.runmode = T.action;

		if (!length(cfg('iface'))) {
			log('info', "travelmate is currently not configured, please use the 'Interface Wizard' in LuCI");
			return false;
		}

		if (!wait_for(cfg('maxwait'), 'network.wireless', 'network.interface.' + cfg('iface'))) {
			log('info', sprintf("travelmate interface '%s' does not appear on ubus, please check your network setup", cfg('iface')));
			return false;
		}

		c().foreach('wireless', 'wifi-device', (s) => setdev(s['.name']));

		if (uci_changes('wireless')) {
			uci_commit('wireless');
			wifi();
		}

		if (cfg('vpn') && !length(T.vpnlist)) {
			let ifaces = [], refs = length(cfg('stdvpniface')) ? [ cfg('stdvpniface') ] : [];

			c().foreach('network', 'interface', (s) => push(ifaces, s));
			c().foreach('travelmate', 'uplink', (s) => {
				if (length(s.vpniface ?? ''))
					push(refs, '' + s.vpniface);
			});

			let r = vpn_infolist(ifaces, cfg('vpnifacelist'), refs);

			T.vpnlist = r.list;

			for (let l in r.logs)
				log('debug', l);
		}

		let device = io.ubus()?.call('network.interface.' + cfg('iface'), 'status')?.device;

		T.fetch = fetch_args(T.cfg, (device != null) ? '' + device : '');

		let url = c().get('travelmate', 'global', 'trm_captiveurl');
		let msg = sprintf("invalid 'trm_captiveurl', only http(s) urls are supported, using '%s'", cfg('captiveurl'));

		if (url != null && url != cfg('captiveurl') && !T.noted[msg]) {
			T.noted[msg] = true;
			log('info', msg);
		}

		log('debug', sprintf('f_conf      ::: frontend: %s, backend: %s, sys_ver: %s, fetch_parm: %s',
			T.fver, T.bver, T.sysver, join(' ', T.fetch)));

		return true;
	}

	// start-up checks
	function startup(action) {
		mkdir(RUNDIR);
		system_info();
		T.action = action ?? '';

		if (length(T.action) && T.action != 'stop') {
			if (!is_dir('/etc/travelmate'))
				log('err', 'no travelmate config directory');

			if (!is_file('/etc/config/travelmate'))
				log('err', 'no travelmate config');

			if (cursor().get('travelmate', 'global', 'trm_enabled') != '1')
				log('err', 'travelmate is disabled');
		}

		for (let cmd in [ 'curl', 'wifi', 'ubus' ])
			if (!have(cmd))
				log('emerg', sprintf("command '%s' not found", cmd));

		T.mailcmd = have('msmtp');
	}

	return {
		T, startup, conf, main, getgw, log, genstatus, rebind_drop, revive_prune,
		scan: (radio) => scan_report(io.ubus()?.call('iwinfo', 'scan', { device: radio })?.results),
		status_text: getstatus_text
	};
};