/
usb_moded-network.c
1291 lines (1072 loc) · 36 KB
/
usb_moded-network.c
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
/**
* @file usb_moded-network.c
*
* (De)activates network depending on the network setting system.
*
* Copyright (c) 2011 Nokia Corporation. All rights reserved.
* Copyright (c) 2012 - 2021 Jolla Ltd.
* Copyright (c) 2020 Open Mobile Platform LLC.
*
* @author Philippe De Swert <philippe.de-swert@nokia.com>
* @author Philippe De Swert <philippedeswert@gmail.com>
* @author Philippe De Swert <philippe.deswert@jollamobile.com>
* @author Marko Saukko <marko.saukko@jollamobile.com>
* @author Slava Monich <slava.monich@jolla.com>
* @author Simo Piiroinen <simo.piiroinen@jollamobile.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Lesser GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the Lesser GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
/*============================================================================= */
#include "usb_moded-network.h"
#include "usb_moded-config-private.h"
#include "usb_moded-control.h"
#include "usb_moded-log.h"
#include "usb_moded-modesetting.h"
#include "usb_moded-worker.h"
#include "usb_moded-dbus-private.h"
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
/* ========================================================================= *
* Constants
* ========================================================================= */
#define UDHCP_CONFIG_PATH "/run/usb-moded/udhcpd.conf"
#define UDHCP_CONFIG_DIR "/run/usb-moded"
#define UDHCP_CONFIG_LINK "/etc/udhcpd.conf"
/* ========================================================================= *
* Types
* ========================================================================= */
/** IP forwarding configuration block */
typedef struct ipforward_data_t
{
/** Address of primary DNS */
char *dns1;
/** Address of secondary DNS */
char *dns2;
/** Interface from which packets should be forwarded */
char *nat_interface;
} ipforward_data_t;
/* ========================================================================= *
* Prototypes
* ========================================================================= */
/* ------------------------------------------------------------------------- *
* IPFORWARD_DATA
* ------------------------------------------------------------------------- */
static ipforward_data_t *ipforward_data_create (void);
static void ipforward_data_delete (ipforward_data_t *self);
static void ipforward_data_clear (ipforward_data_t *self);
static void ipforward_data_set_dns1 (ipforward_data_t *self, const char *dns);
static void ipforward_data_set_dns2 (ipforward_data_t *self, const char *dns);
static void ipforward_data_set_nat_interface(ipforward_data_t *self, const char *interface);
/* ------------------------------------------------------------------------- *
* OFONO
* ------------------------------------------------------------------------- */
#ifdef OFONO
static gchar *ofono_get_default_modem (void);
static gchar *ofono_get_modem_status (const char *modem);
static bool ofono_get_roaming_status(void);
#endif
/* ------------------------------------------------------------------------- *
* CONNMAN
* ------------------------------------------------------------------------- */
#ifdef CONNMAN
static bool connman_technology_set_tethering (DBusConnection *con, const char *technology, bool on, DBusError *err);
static gchar *connman_manager_get_service_path (DBusConnection *con, const char *type);
static bool connman_service_get_connection_data(DBusConnection *con, const char *service, ipforward_data_t *ipforward);
static bool connman_get_connection_data (ipforward_data_t *ipforward);
bool connman_set_tethering (const char *technology, bool on);
#endif
/* ------------------------------------------------------------------------- *
* LEGACY
* ------------------------------------------------------------------------- */
#ifndef CONNMAN
static bool legacy_get_connection_data(ipforward_data_t *ipforward);
#endif
/* ------------------------------------------------------------------------- *
* NETWORK
* ------------------------------------------------------------------------- */
static bool network_interface_exists (char *interface);
static char *network_get_interface (const modedata_t *data);
static char *network_get_nat_interface (const modedata_t *data);
static char *network_get_ip (const modedata_t *data);
static char *network_get_netmask (const modedata_t *data);
static int network_setup_ip_forwarding (const modedata_t *data, ipforward_data_t *ipforward);
static void network_cleanup_ip_forwarding(void);
static int network_check_udhcpd_symlink (void);
static int network_write_udhcpd_config (const modedata_t *data, ipforward_data_t *ipforward);
int network_update_udhcpd_config (const modedata_t *data);
int network_up (const modedata_t *data);
void network_down (const modedata_t *data);
void network_update (void);
/* ========================================================================= *
* IPFORWARD_DATA
* ========================================================================= */
static ipforward_data_t *
ipforward_data_create(void)
{
LOG_REGISTER_CONTEXT;
ipforward_data_t *self = g_malloc0(sizeof *self);
self->dns1 = 0;
self->dns2 = 0;
self->nat_interface = 0;
return self;
}
static void
ipforward_data_delete(ipforward_data_t *self)
{
LOG_REGISTER_CONTEXT;
if( self )
{
ipforward_data_clear(self);
g_free(self);
}
}
static void
ipforward_data_clear(ipforward_data_t *self)
{
LOG_REGISTER_CONTEXT;
ipforward_data_set_dns1(self, 0);
ipforward_data_set_dns2(self, 0);
ipforward_data_set_nat_interface(self, 0);
}
static void
ipforward_data_set_dns1(ipforward_data_t *self, const char *dns)
{
LOG_REGISTER_CONTEXT;
g_free(self->dns1),
self->dns1 = dns ? g_strdup(dns) : 0;
}
static void
ipforward_data_set_dns2(ipforward_data_t *self, const char *dns)
{
LOG_REGISTER_CONTEXT;
g_free(self->dns2),
self->dns2 = dns ? g_strdup(dns) : 0;
}
static void
ipforward_data_set_nat_interface(ipforward_data_t *self, const char *interface)
{
LOG_REGISTER_CONTEXT;
g_free(self->nat_interface),
self->nat_interface = interface ? g_strdup(interface) : 0;
}
/* ========================================================================= *
* OFONO
* ========================================================================= */
#ifdef OFONO
/** Get object path of the 1st modem known to ofono
*
* Caller must release the returned string with g_free().
*
* @return object path, or NULL
*/
static gchar *
ofono_get_default_modem(void)
{
gchar *modem = 0;
DBusConnection *con = 0;
DBusError err = DBUS_ERROR_INIT;
DBusMessage *rsp = 0;
if( !(con = umdbus_get_connection()) )
goto EXIT;
rsp = umdbus_blocking_call(con,
"org.ofono",
"/",
"org.ofono.Manager",
"GetModems",
&err,
DBUS_TYPE_INVALID);
if( !rsp )
goto EXIT;
// a(oa{sv}) -> get object path in the first struct in the array
DBusMessageIter body;
if( umdbus_parser_init(&body, rsp) ) {
DBusMessageIter iter_array;
if( umdbus_parser_get_array(&body, &iter_array) ) {
DBusMessageIter astruct;
if( umdbus_parser_get_struct(&iter_array, &astruct) ) {
const char *object = 0;
if( umdbus_parser_get_object(&astruct, &object) ) {
modem = g_strdup(object);
}
}
}
}
EXIT:
if( rsp )
dbus_message_unref(rsp);
if( con )
dbus_connection_unref(con);
dbus_error_free(&err);
log_warning("default modem = %s", modem ?: "n/a");
return modem;
}
/** Query modem status from ofono
*
* Caller must release the returned string with g_free().
*
* @param modem D-Bus object path
*
* @return modem status, or NULL
*/
static gchar *
ofono_get_modem_status(const char *modem)
{
gchar *status = 0;
DBusConnection *con = 0;
DBusError err = DBUS_ERROR_INIT;
DBusMessage *rsp = 0;
if( !(con = umdbus_get_connection()) )
goto EXIT;
rsp = umdbus_blocking_call(con,
"org.ofono",
modem,
"org.ofono.NetworkRegistration",
"GetProperties",
&err,
DBUS_TYPE_INVALID);
if( !rsp )
goto EXIT;
DBusMessageIter body;
if( umdbus_parser_init(&body, rsp) ) {
DBusMessageIter iter_array;
if( umdbus_parser_get_array(&body, &iter_array) ) {
DBusMessageIter entry;
while( umdbus_parser_get_entry(&iter_array, &entry) ) {
const char *key = 0;
if( !umdbus_parser_get_string(&entry, &key) )
break;
if( strcmp(key, "Status") )
continue;
DBusMessageIter var;
if( !umdbus_parser_get_variant(&entry, &var) )
break;
const char *val = 0;
if( !umdbus_parser_get_string(&var, &val) )
break;
status = g_strdup(val);
break;
}
}
}
EXIT:
if( rsp )
dbus_message_unref(rsp);
if( con )
dbus_connection_unref(con);
dbus_error_free(&err);
log_warning("modem status = %s", status ?: "n/a");
return status;
}
/** Get roaming data from ofono
*
* @return true if roaming, false when not (or when ofono is unavailable)
*/
static bool
ofono_get_roaming_status(void)
{
LOG_REGISTER_CONTEXT;
bool roaming = false;
gchar *modem = 0;
gchar *status = 0;
if( !(modem = ofono_get_default_modem()) )
goto EXIT;
if( !(status = ofono_get_modem_status(modem)) )
goto EXIT;
if( !strcmp(status, "roaming") )
roaming = true;
EXIT:
g_free(status);
g_free(modem);
log_warning("modem roaming = %d", roaming);
return roaming;
}
#endif /* OFONO */
/* ========================================================================= *
* CONNMAN
* ========================================================================= */
#ifdef CONNMAN
# define CONNMAN_SERVICE "net.connman"
# define CONNMAN_TECH_INTERFACE "net.connman.Technology"
# define CONNMAN_ERROR_ALREADY_ENABLED "net.connman.Error.AlreadyEnabled"
# define CONNMAN_ERROR_ALREADY_DISABLED "net.connman.Error.AlreadyDisabled"
/* ------------------------------------------------------------------------- *
* TECHNOLOGY interface
* ------------------------------------------------------------------------- */
/** Configures tethering for the specified connman technology.
*
* @param con D-Bus connection
* @param technology D-Bus object path
* @param on true to enable tethering, false to disable
* @param err Where to store D-Bus ipc error
*
* @return true on success, false otherwise
*/
static bool
connman_technology_set_tethering(DBusConnection *con, const char *technology, bool on,
DBusError *err)
{
LOG_REGISTER_CONTEXT;
bool res = FALSE;
DBusMessage *rsp = 0;
const char *key = "Tethering";
dbus_bool_t val = on;
rsp = umdbus_blocking_call(con,
CONNMAN_SERVICE,
technology,
CONNMAN_TECH_INTERFACE,
"SetProperty",
err,
DBUS_TYPE_STRING, &key,
DBUS_TYPE_VARIANT,
DBUS_TYPE_BOOLEAN, &val,
DBUS_TYPE_INVALID);
if( !rsp ) {
if( on ) {
if( !g_strcmp0(err->name, CONNMAN_ERROR_ALREADY_ENABLED) )
goto SUCCESS;
}
else {
if( !g_strcmp0(err->name, CONNMAN_ERROR_ALREADY_DISABLED) )
goto SUCCESS;
}
log_err("%s.%s method call failed: %s: %s",
CONNMAN_TECH_INTERFACE, "SetProperty",
err->name, err->message);
goto FAILURE;
}
SUCCESS:
log_debug("%s tethering %s", technology, on ? "on" : "off");
dbus_error_free(err);
res = TRUE;
FAILURE:
if( rsp )
dbus_message_unref(rsp);
return res;
}
/* ------------------------------------------------------------------------- *
* MANAGER interface
* ------------------------------------------------------------------------- */
/** Get object path for the 1st service matching given type
*
* Caller must release the returned string with g_free().
*
* @param type Connman service type string
*
* @return D-Bus object path, or NULL
*/
static gchar *
connman_manager_get_service_path(DBusConnection *con, const char *type)
{
LOG_REGISTER_CONTEXT;
gchar *service = 0;
DBusError err = DBUS_ERROR_INIT;
DBusMessage *rsp = 0;
rsp = umdbus_blocking_call(con,
"net.connman",
"/",
"net.connman.Manager",
"GetServices",
&err,
DBUS_TYPE_INVALID);
if( !rsp )
goto EXIT;
// a(oa{sv}) -> get object path in the first struct matching given type
DBusMessageIter body;
if( umdbus_parser_init(&body, rsp) ) {
// @ body
DBusMessageIter array_of_structs;
if( umdbus_parser_get_array(&body, &array_of_structs) ) {
// @ array of structs
DBusMessageIter astruct;
while( umdbus_parser_get_struct(&array_of_structs, &astruct) ) {
// @ struct
const char *object = 0;
if( !umdbus_parser_get_object(&astruct, &object) )
break;
DBusMessageIter array_of_entries;
if( !umdbus_parser_get_array(&astruct, &array_of_entries) )
break;
// @ array of dict entries
DBusMessageIter entry;
while( umdbus_parser_get_entry(&array_of_entries, &entry) ) {
// @ dict entry
const char *key = 0;
if( !umdbus_parser_get_string(&entry, &key) )
break;
if( strcmp(key, "Type") )
continue;
DBusMessageIter var;
if( !umdbus_parser_get_variant(&entry, &var) )
break;
const char *value = 0;
if( !umdbus_parser_get_string(&var, &value) )
break;
if( strcmp(value, type) )
continue;
service = g_strdup(object);
goto EXIT;
}
}
}
}
EXIT:
if( rsp )
dbus_message_unref(rsp);
dbus_error_free(&err);
log_warning("%s service = %s", type, service ?: "n/a");
return service;
}
/** Query ipforwarding parameters from connman
*
* @param con D-Bus connection
* @param service D-Bus object path
* @param ipforward ipforward object to fill in
*
* @return true on success, false otherwise
*/
static bool
connman_service_get_connection_data(DBusConnection *con,
const char *service,
ipforward_data_t *ipforward)
{
LOG_REGISTER_CONTEXT;
bool ack = false;
DBusMessage *rsp = NULL;
DBusError err = DBUS_ERROR_INIT;
log_debug("Filling in dns data");
rsp = umdbus_blocking_call(con,
"net.connman",
service,
"net.connman.Service",
"GetProperties",
&err,
DBUS_TYPE_INVALID);
if( !rsp )
goto EXIT;
const char *dns1 = 0;
const char *dns2 = 0;
const char *state = 0;
const char *interface = 0;
DBusMessageIter body;
if( umdbus_parser_init(&body, rsp) ) {
// @ body
DBusMessageIter array_of_entries;
if( umdbus_parser_get_array(&body, &array_of_entries) ) {
// @ array of entries
DBusMessageIter entry;
while( umdbus_parser_get_entry(&array_of_entries, &entry) ) {
// @ dict entry
const char *key = 0;
if( !umdbus_parser_get_string(&entry, &key) )
break;
DBusMessageIter var;
if( !umdbus_parser_get_variant(&entry, &var) )
break;
if( !strcmp(key, "Nameservers"))
{
DBusMessageIter array_of_strings;
if( umdbus_parser_get_array(&var, &array_of_strings) ) {
// expect 0, 1, or 2 entries
if( !umdbus_parser_at_end(&array_of_strings) )
umdbus_parser_get_string(&array_of_strings, &dns1);
if( !umdbus_parser_at_end(&array_of_strings) )
umdbus_parser_get_string(&array_of_strings, &dns2);
}
}
else if( !strcmp(key, "State"))
{
umdbus_parser_get_string(&var, &state);
}
else if( !strcmp(key, "Ethernet"))
{
DBusMessageIter array_of_en_entries;
if( umdbus_parser_get_array(&var, &array_of_en_entries) ) {
DBusMessageIter en_entry;
while( umdbus_parser_get_entry(&array_of_en_entries, &en_entry) ) {
const char *en_key = 0;
if( !umdbus_parser_get_string(&en_entry, &en_key) )
break;
if( strcmp(en_key, "Interface") )
continue;
DBusMessageIter en_var;
if( umdbus_parser_get_variant(&en_entry, &en_var) )
umdbus_parser_get_string(&en_var, &interface);
}
}
}
}
}
}
bool connected = (!g_strcmp0(state, "ready") ||
!g_strcmp0(state, "online"));
log_debug("state = %s", state ?: "n/a");
log_debug("connected = %s", connected ? "true" : "false");
log_debug("interface = %s", interface ?: "n/a");
log_debug("dns1 = %s", dns1 ?: "n/a");
log_debug("dns2 = %s", dns2 ?: "n/a");
if( !dns1 || !interface || !connected )
goto EXIT;
ipforward_data_set_dns1(ipforward, dns1);
ipforward_data_set_dns2(ipforward, dns2 ?: dns1);
ipforward_data_set_nat_interface(ipforward, interface);
ack = true;
EXIT:
if( rsp )
dbus_message_unref(rsp);
dbus_error_free(&err);
return ack;
}
/* ------------------------------------------------------------------------- *
* USB-MODED interface
* ------------------------------------------------------------------------- */
/** Query ipforwarding parameters from connman
*
* @param ipforward ipforward object to fill in
*
* @return true on success, false otherwise
*/
static bool
connman_get_connection_data(ipforward_data_t *ipforward)
{
LOG_REGISTER_CONTEXT;
bool ack = false;
DBusConnection *con = 0;
gchar *cellular = 0;
gchar *wifi = 0;
if( !(con = umdbus_get_connection()) )
goto FAILURE;
/* Try to get connection data from cellular service */
if( !(cellular = connman_manager_get_service_path(con, "cellular")) )
log_warning("no sellular service");
else if( connman_service_get_connection_data(con, cellular, ipforward) )
goto SUCCESS;
/* Try to get connection data from wifi service */
if( !(wifi = connman_manager_get_service_path(con, "wifi")) )
log_warning("no wifi service");
else if( connman_service_get_connection_data(con, wifi, ipforward) )
goto SUCCESS;
/* Abandon hope */
goto FAILURE;
SUCCESS:
ack = true;
FAILURE:
if( !ack )
log_warning("no connection data");
else
log_debug("got connection data");
g_free(wifi);
g_free(cellular);
if( con )
dbus_connection_unref(con);
return ack;
}
/** Configures tethering for the specified connman technology.
*
* @param technology D-Bus object path
* @param on true to enable tethering, false to disable
*
* @return true on success, false otherwise
*/
bool
connman_set_tethering(const char *technology, bool on)
{
LOG_REGISTER_CONTEXT;
bool res = false;
DBusError err = DBUS_ERROR_INIT;
DBusConnection *con = 0;
if( !(con = umdbus_get_connection()) )
goto EXIT;
res = connman_technology_set_tethering(con, technology, on, &err);
EXIT:
dbus_error_free(&err);
if( con )
dbus_connection_unref(con);
log_debug("set tethering(%s) = %s -> %s", technology,
on ? "on" : "off", res ? "ack" : "nak");
return res;
}
#endif /* CONNMAN */
/* ========================================================================= *
* LEGACY
* ========================================================================= */
#ifndef CONNMAN
/** Read dns settings from /etc/resolv.conf
*
* @return true on success, false on failure
*/
static bool
legacy_get_connection_data(ipforward_data_t *ipforward)
{
LOG_REGISTER_CONTEXT;
static const char path[] = "/etc/resolv.conf";
bool ack = false;
FILE *file = 0;
char *buff = 0;
size_t size = 0;
if( !(file = fopen(path, "r")) ) {
log_warning("%s: can't open for reading: %m", path);
goto EXIT;
}
int count = 0;
while( count < 2 && getline(&buff, &size, file) >= 0 ) {
/* skip comment and empty lines */
if( strchr("#\n", *buff) )
continue;
gchar **tokens = g_strsplit(buff, " ", 3);
if( !tokens || !tokens[0] || !tokens[1] ) {
// ignore
}
else if( !g_strcmp0(tokens[0], "nameserver") ) {
// TODO: can have '\n' at eol?
g_strstrip(tokens[1]);
if( ++count == 1 )
ipforward_data_set_dns1(ipforward, tokens[1]);
else
ipforward_data_set_dns2(ipforward, tokens[1]);
}
g_strfreev(tokens);
}
if( count < 1 ) {
log_warning("%s: no nameserver lines found", path);
goto EXIT;
}
/* FIXME: connman_service_get_connection_data() duplicates
* dns1 if dns2 is not set - is this necessary?
*/
if( count == 1 )
ipforward_data_set_dns2(ipforward, ipforward->dns1);
ack = true;
EXIT:
free(buff);
if( file )
fclose(file);
return ack;
}
#endif
/* ========================================================================= *
* NETWORK
* ========================================================================= */
/** This function checks if the configured interface exists
*
* @return true on success, false on failure
*/
static bool
network_interface_exists(char *interface)
{
LOG_REGISTER_CONTEXT;
bool ack = false;
if(interface)
{
char path[PATH_MAX];
snprintf(path, sizeof path, "/sys/class/net/%s", interface);
ack = (access(path, F_OK) == 0);
}
return ack;
}
/** Get network interface to use
*
* @param data Dynamic mode data
*
* @return interface name, or NULL
*/
static char *
network_get_interface(const modedata_t *data)
{
LOG_REGISTER_CONTEXT;
gchar *interface = g_strdup(data->cached_interface);
if( !interface ) {
log_warning("mode %s: network interface not specified",
data->mode_name);
}
else if( !network_interface_exists(interface) ) {
log_warning("mode %s: network interface %s does not exist",
data->mode_name, interface);
g_free(interface), interface = NULL;
}
return interface;
}
/** Get network nat interface to use
*
* @param data Dynamic mode data
*
* @return nat interface name, or NULL
*/
static char *
network_get_nat_interface(const modedata_t *data)
{
LOG_REGISTER_CONTEXT;
gchar *interface = g_strdup(data->cached_nat_interface);
if( !interface ) {
log_warning("mode %s: network nat interface not specified",
data->mode_name);
}
else if( !network_interface_exists(interface) ) {
log_warning("mode %s: network nat interface %s does not exist",
data->mode_name, interface);
g_free(interface), interface = NULL;
}
return interface;
}
/** Get network ip address to use
*
* @param data Dynamic mode data
*
* @return ip address, or NULL
*/
static char *
network_get_ip(const modedata_t *data)
{
LOG_REGISTER_CONTEXT;
gchar *ip = g_strdup(data->cached_ip);
if( !ip ) {
log_warning("mode %s: network ip address not specified",
data->mode_name);
}
return ip;
}
/** Get network netmask to use
*
* @param data Dynamic mode data
*
* @return netmask name, or NULL
*/
static char *
network_get_netmask(const modedata_t *data)
{
LOG_REGISTER_CONTEXT;
gchar *netmask = g_strdup(data->cached_netmask);
if( !netmask ) {
log_warning("mode %s: network netmask not specified",
data->mode_name);
}
return netmask;
}
/** Turn on ip forwarding on the usb interface
*
* To cleanup: #network_cleanup_ip_forwarding()
*
* @param data Dynamic mode data (not used)
*
* @return 0 on success, 1 on failure
*/
static int
network_setup_ip_forwarding(const modedata_t *data, ipforward_data_t *ipforward)
{
LOG_REGISTER_CONTEXT;
int failed = 1;
char *interface = 0;
char *nat_interface = 0;
char command[256];
if( !(interface = network_get_interface(data)) )
goto EXIT;
nat_interface = network_get_nat_interface(data);
if( !nat_interface ) {
if( !ipforward->nat_interface ) {
log_debug("No nat interface available!");
goto EXIT;
}
nat_interface = strdup(ipforward->nat_interface);
}
write_to_file("/proc/sys/net/ipv4/ip_forward", "1");
snprintf(command, sizeof command, "/sbin/iptables -t nat -A POSTROUTING -o %s -j MASQUERADE", nat_interface);
common_system(command);
snprintf(command, sizeof command, "/sbin/iptables -A FORWARD -i %s -o %s -m state --state RELATED,ESTABLISHED -j ACCEPT", nat_interface, interface);
common_system(command);
snprintf(command, sizeof command, "/sbin/iptables -A FORWARD -i %s -o %s -j ACCEPT", interface, nat_interface);
common_system(command);
log_debug("ipforwarding success!");
failed = 0;
EXIT:
free(interface);
free(nat_interface);
return failed;
}
/** Turn off ip forwarding on the usb interface
*/
static void
network_cleanup_ip_forwarding(void)
{
LOG_REGISTER_CONTEXT;
write_to_file("/proc/sys/net/ipv4/ip_forward", "0");
common_system("/sbin/iptables -F FORWARD");
}
/** Validate udhcpd.conf symlink
*
* @return zero if symlink is valid, non-zero otherwise
*/
static int
network_check_udhcpd_symlink(void)
{
LOG_REGISTER_CONTEXT;
int ret = -1;
char dest[sizeof UDHCP_CONFIG_PATH + 1];
ssize_t rc = readlink(UDHCP_CONFIG_LINK, dest, sizeof dest - 1);
if( rc < 0 ) {
if( errno != ENOENT )
log_err("%s: can't read symlink: %m", UDHCP_CONFIG_LINK);
}
else if( (size_t)rc < sizeof dest ) {
dest[rc] = 0;
if( strcmp(dest, UDHCP_CONFIG_PATH) )
log_warning("%s: symlink is invalid", UDHCP_CONFIG_LINK);
else
ret = 0;
}
return ret;
}
/** Write udhcpd.conf