/
usb_moded-network.c
1247 lines (1107 loc) · 41 KB
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
* @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-2018 Jolla. All rights reserved.
*
* @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
*/
28
29
30
31
/*============================================================================= */
#include "usb_moded-network.h"
32
33
#include "usb_moded-config-private.h"
34
#include "usb_moded-control.h"
35
#include "usb_moded-log.h"
36
#include "usb_moded-modesetting.h"
37
#include "usb_moded-worker.h"
38
39
40
41
42
43
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
44
45
#if CONNMAN || OFONO
46
# include <dbus/dbus.h>
47
#endif
48
49
50
51
/* ========================================================================= *
* Constants
* ========================================================================= */
52
53
54
55
56
57
58
59
#define UDHCP_CONFIG_PATH "/run/usb-moded/udhcpd.conf"
#define UDHCP_CONFIG_DIR "/run/usb-moded"
#define UDHCP_CONFIG_LINK "/etc/udhcpd.conf"
/* ========================================================================= *
* Types
* ========================================================================= */
60
61
/** IP forwarding configuration block */
62
typedef struct ipforward_data_t
63
{
64
65
66
67
68
69
/** Address of primary DNS */
char *dns1;
/** Address of secondary DNS */
char *dns2;
/** Interface from which packets should be forwarded */
char *nat_interface;
70
}ipforward_data_t;
71
72
73
74
75
76
77
/* ========================================================================= *
* Prototypes
* ========================================================================= */
/* -- network -- */
78
static void network_free_ipforward_data (ipforward_data_t *ipforward);
79
static int network_check_interface (char *interface);
80
81
static char *network_get_interface (mode_list_elem_t *data);
static int network_set_usb_ip_forward (mode_list_elem_t *data, ipforward_data_t *ipforward);
82
83
static void network_clean_usb_ip_forward(void);
static int network_checklink (void);
84
85
86
87
static int network_write_udhcpd_conf (ipforward_data_t *ipforward, mode_list_elem_t *data);
int network_set_up_dhcpd (mode_list_elem_t *data);
int network_up (mode_list_elem_t *data);
int network_down (mode_list_elem_t *data);
88
89
90
91
int network_update (void);
/* -- connman -- */
92
#ifdef CONNMAN
93
94
95
static gboolean connman_try_set_tethering (DBusConnection *connection, const char *path, gboolean on);
gboolean connman_set_tethering (const char *path, gboolean on);
static char *connman_parse_manager_reply (DBusMessage *reply, const char *req_service);
96
static int connman_fill_connection_data(DBusMessage *reply, ipforward_data_t *ipforward);
97
98
static int connman_set_cellular_online (DBusConnection *dbus_conn_connman, const char *service, int retry);
static int connman_wifi_power_control (DBusConnection *dbus_conn_connman, int on);
99
static int connman_get_connection_data (ipforward_data_t *ipforward);
100
static int connman_reset_state (void);
101
102
#endif
103
104
105
106
107
108
109
110
111
112
/* ========================================================================= *
* Data
* ========================================================================= */
static const char default_interface[] = "usb0";
/* ========================================================================= *
* Functions
* ========================================================================= */
113
static void network_free_ipforward_data (ipforward_data_t *ipforward)
114
{
115
116
117
118
119
120
121
122
123
124
if(ipforward)
{
if(ipforward->dns1)
free(ipforward->dns1);
if(ipforward->dns2)
free(ipforward->dns2);
if(ipforward->nat_interface)
free(ipforward->nat_interface);
free(ipforward);
}
125
126
}
127
/* This function checks if the configured interface exists */
128
static int network_check_interface(char *interface)
129
{
130
int ret = -1;
131
132
133
134
135
136
137
if(interface)
{
char path[256];
snprintf(path, sizeof path, "/sys/class/net/%s", interface);
ret = access(path, F_OK);
}
138
139
return ret;
140
141
}
142
static char* network_get_interface(mode_list_elem_t *data)
143
{
144
145
146
147
148
149
(void)data; // FIXME: why is this passed in the 1st place?
char *interface = 0;
char *setting = config_get_network_setting(NETWORK_INTERFACE_KEY);
if(network_check_interface(setting) == 0)
150
{
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* Use the configured value */
interface = setting, setting = 0;
}
else
{
/* Fall back to default value */
interface = strdup(default_interface);
if(network_check_interface(interface) != 0)
{
log_warning("Neither configured %s nor fallback %s interface exists."
" Check your config!",
setting ?: "NULL",
interface ?: "NULL");
free(interface), interface = 0;
}
166
}
167
168
169
170
log_debug("interface = %s\n", interface ?: "NULL");
free(setting);
return interface;
171
}
172
173
174
/**
* Turn on ip forwarding on the usb interface
175
* @return: 0 on success, 1 on failure
176
*/
177
static int network_set_usb_ip_forward(mode_list_elem_t *data, ipforward_data_t *ipforward)
178
{
179
180
181
182
183
char *interface, *nat_interface;
char command[128];
interface = network_get_interface(data);
if(interface == NULL)
184
return 1;
185
186
187
188
189
190
nat_interface = config_get_network_setting(NETWORK_NAT_INTERFACE_KEY);
if((nat_interface == NULL) && (ipforward->nat_interface != NULL))
nat_interface = strdup(ipforward->nat_interface);
else
{
log_debug("No nat interface available!\n");
191
#ifdef CONNMAN
192
193
/* in case the cellular did not come up we want to make sure wifi gets restored */
connman_reset_state();
194
#endif
195
196
free(interface);
free(nat_interface);
197
return 1;
198
199
200
}
write_to_file("/proc/sys/net/ipv4/ip_forward", "1");
snprintf(command, 128, "/sbin/iptables -t nat -A POSTROUTING -o %s -j MASQUERADE", nat_interface);
201
common_system(command);
202
203
snprintf(command, 128, "/sbin/iptables -A FORWARD -i %s -o %s -m state --state RELATED,ESTABLISHED -j ACCEPT", nat_interface, interface);
204
common_system(command);
205
206
snprintf(command, 128, "/sbin/iptables -A FORWARD -i %s -o %s -j ACCEPT", interface, nat_interface);
207
common_system(command);
208
209
210
211
free(interface);
free(nat_interface);
log_debug("ipforwarding success!\n");
212
return 0;
213
214
}
215
/**
216
217
* Remove ip forward
*/
218
static void network_clean_usb_ip_forward(void)
219
{
220
#ifdef CONNMAN
221
connman_reset_state();
222
#endif
223
write_to_file("/proc/sys/net/ipv4/ip_forward", "0");
224
common_system("/sbin/iptables -F FORWARD");
225
226
}
227
228
229
#ifdef OFONO
/**
* Get roaming data from ofono
230
*
231
232
233
234
* @return : 1 if roaming, 0 when not (or when ofono is unavailable)
*/
static int get_roaming(void)
{
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
int ret = 0, type;
DBusError error;
DBusMessage *msg = NULL, *reply;
DBusConnection *dbus_conn_ofono = NULL;
char *modem = NULL;
DBusMessageIter iter, subiter;
dbus_error_init(&error);
if( (dbus_conn_ofono = dbus_bus_get(DBUS_BUS_SYSTEM, &error)) == 0 )
{
log_err("Could not connect to dbus for ofono\n");
}
/* find the modem object path so can find out if it is roaming or not (one modem only is assumed) */
if ((msg = dbus_message_new_method_call("org.ofono", "/", "org.ofono.Manager", "GetModems")) != NULL)
{
252
253
if ((reply = dbus_connection_send_with_reply_and_block(dbus_conn_ofono, msg, -1, NULL)) != NULL)
{
254
255
256
257
258
259
260
261
262
263
264
dbus_message_iter_init(reply, &iter);
dbus_message_iter_recurse(&iter, &subiter);
iter = subiter;
dbus_message_iter_recurse(&iter, &subiter);
type = dbus_message_iter_get_arg_type(&subiter);
if(type == DBUS_TYPE_OBJECT_PATH)
{
dbus_message_iter_get_basic(&subiter, &modem);
log_debug("modem = %s\n", modem);
}
dbus_message_unref(reply);
265
266
}
dbus_message_unref(msg);
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
}
/* if modem found then we check roaming state */
if(modem != NULL)
{
if ((msg = dbus_message_new_method_call("org.ofono", modem, "org.ofono.NetworkRegistration", "GetProperties")) != NULL)
{
if ((reply = dbus_connection_send_with_reply_and_block(dbus_conn_ofono, msg, -1, NULL)) != NULL)
{
dbus_message_iter_init(reply, &iter);
dbus_message_iter_recurse(&iter, &subiter);
iter = subiter;
dbus_message_iter_recurse(&iter, &subiter);
type = dbus_message_iter_get_arg_type(&subiter);
if(type == DBUS_TYPE_STRING)
{
dbus_message_iter_next (&subiter);
iter = subiter;
dbus_message_iter_recurse(&iter, &subiter);
dbus_message_iter_get_basic(&subiter, &modem);
log_debug("modem status = %s\n", modem);
}
}
dbus_message_unref(reply);
}
291
292
dbus_message_unref(msg);
293
294
295
296
if(!strcmp("roaming", modem))
ret = 1;
}
297
return ret;
298
299
300
}
#endif
301
#ifndef CONNMAN
302
303
304
/**
* Read dns settings from /etc/resolv.conf
*/
305
static int resolv_conf_dns(ipforward_data_t *ipforward)
306
{
307
308
309
310
311
312
313
314
FILE *resolv;
int i = 0, count = 0;
char *line = NULL, **tokens;
size_t len = 0;
ssize_t read;
resolv = fopen("/etc/resolv.conf", "r");
if (resolv == NULL)
315
return 1;
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/* we don't expect more than 10 lines in /etc/resolv.conf */
for (i=0; i < 10; i++)
{
read = getline(&line, &len, resolv);
if(read)
{
if(strstr(line, "nameserver") != NULL)
{
tokens = g_strsplit(line, " ", 2);
if(count == 0)
ipforward->dns1 = strdup(tokens[1]);
else
ipforward->dns2 = strdup(tokens[1]);
count++;
g_strfreev(tokens);
}
}
if(count == 2)
goto end;
}
337
end:
338
339
free(line);
fclose(resolv);
340
return 0;
341
}
342
#endif
343
344
static int network_checklink(void)
345
{
346
int ret = -1;
347
348
char dest[sizeof UDHCP_CONFIG_PATH + 1];
size_t len = readlink(UDHCP_CONFIG_LINK, dest, sizeof dest - 1);
349
350
351
352
353
354
if (len > 0)
{
dest[len] = 0;
ret = strcmp(dest, UDHCP_CONFIG_PATH);
}
355
return ret;
356
357
}
358
359
360
361
/**
* Write udhcpd.conf
* @ipforward : NULL if we want a simple config, otherwise include dns info etc...
*/
362
static int network_write_udhcpd_conf(ipforward_data_t *ipforward, mode_list_elem_t *data)
363
{
364
365
366
367
368
369
370
371
372
373
374
375
FILE *conffile;
char *ip, *interface, *netmask;
char *ipstart, *ipend;
int dot = 0, i = 0, test;
struct stat st;
/* /tmp and /run is often tmpfs, so we avoid writing to flash */
mkdir(UDHCP_CONFIG_DIR, 0664);
conffile = fopen(UDHCP_CONFIG_PATH, "w");
if(conffile == NULL)
{
log_debug("Error creating "UDHCP_CONFIG_PATH"!\n");
376
return 1;
377
378
379
380
381
382
}
interface = network_get_interface(data);
if(interface == NULL)
{
fclose(conffile);
383
return 1;
384
385
386
}
/* generate start and end ip based on the setting */
ip = config_get_network_setting(NETWORK_IP_KEY);
387
388
ipstart = malloc(15);
ipend = malloc(15);
389
390
while(i < 15)
{
391
392
if(dot < 3)
{
393
394
395
396
if(ip[i] == '.')
dot ++;
ipstart[i] = ip[i];
ipend[i] = ip[i];
397
398
399
}
else
{
400
401
402
ipstart[i] = '\0';
ipend[i] = '\0';
break;
403
404
}
i++;
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
}
strcat(ipstart,"1");
strcat(ipend, "15");
netmask = config_get_network_setting(NETWORK_NETMASK_KEY);
/* print all data in the file */
fprintf(conffile, "start\t%s\n", ipstart);
fprintf(conffile, "end\t%s\n", ipend);
fprintf(conffile, "interface\t%s\n", interface);
fprintf(conffile, "option\tsubnet\t%s\n", netmask);
fprintf(conffile, "option\tlease\t3600\n");
fprintf(conffile, "max_leases\t15\n");
if(ipforward != NULL)
{
if(!ipforward->dns1 || !ipforward->dns2)
{
log_debug("No dns info!");
}
else
fprintf(conffile, "opt\tdns\t%s %s\n", ipforward->dns1, ipforward->dns2);
fprintf(conffile, "opt\trouter\t%s\n", ip);
}
free(ipstart);
free(ipend);
free(ip);
free(interface);
free(netmask);
fclose(conffile);
/* check if it is a symlink, if not remove and link, create the link if missing */
test = lstat(UDHCP_CONFIG_LINK, &st);
/* if stat fails there is no file or link */
if(test == -1)
goto link;
/* if it is not a link, or points to the wrong place we remove it */
if(((st.st_mode & S_IFMT) != S_IFLNK) || network_checklink())
{
unlink(UDHCP_CONFIG_LINK);
}
else
goto end;
449
450
link:
451
452
453
454
if (symlink(UDHCP_CONFIG_PATH, UDHCP_CONFIG_LINK) == -1)
{
log_debug("Error creating link "UDHCP_CONFIG_LINK" -> "UDHCP_CONFIG_PATH": %m\n");
unlink(UDHCP_CONFIG_PATH);
455
return 1;
456
}
457
458
end:
459
log_debug(UDHCP_CONFIG_LINK" created\n");
460
return 0;
461
462
}
463
#ifdef CONNMAN
464
465
466
467
468
469
# define CONNMAN_SERVICE "net.connman"
# define CONNMAN_TECH_INTERFACE "net.connman.Technology"
# define CONNMAN_ERROR_INTERFACE CONNMAN_SERVICE ".Error"
# define CONNMAN_ERROR_ALREADY_ENABLED CONNMAN_ERROR_INTERFACE ".AlreadyEnabled"
# define CONNMAN_ERROR_ALREADY_DISABLED CONNMAN_ERROR_INTERFACE ".AlreadyDisabled"
470
471
472
473
474
475
/*
* Configures tethering for the specified connman technology.
*/
static gboolean connman_try_set_tethering(DBusConnection *connection, const char *path, gboolean on)
{
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
gboolean ok = FALSE;
DBusMessage *message = dbus_message_new_method_call(CONNMAN_SERVICE, path, CONNMAN_TECH_INTERFACE, "SetProperty");
if (message)
{
DBusMessage *reply;
DBusMessageIter iter;
DBusMessageIter iter2;
DBusError error;
const char* key = "Tethering";
dbus_bool_t value = (on != FALSE);
dbus_message_iter_init_append(message, &iter);
dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &key);
dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, DBUS_TYPE_BOOLEAN_AS_STRING, &iter2);
dbus_message_iter_append_basic(&iter2, DBUS_TYPE_BOOLEAN, &value);
dbus_message_iter_close_container(&iter, &iter2);
dbus_error_init(&error);
reply = dbus_connection_send_with_reply_and_block(connection, message, DBUS_TIMEOUT_USE_DEFAULT, &error);
dbus_message_unref(message);
if (reply)
{
log_debug("%s tethering %s", path, on ? "on" : "off");
dbus_message_unref(reply);
ok = TRUE;
}
else
{
if ((on && !g_strcmp0(error.name, CONNMAN_ERROR_ALREADY_ENABLED)) ||
(!on && (!g_strcmp0(error.name, CONNMAN_ERROR_ALREADY_DISABLED) ||
!g_strcmp0(error.name, DBUS_ERROR_UNKNOWN_OBJECT))))
{
ok = TRUE;
}
else
{
log_err("%s\n", error.message);
}
dbus_error_free(&error);
}
}
return ok;
519
520
521
522
}
gboolean connman_set_tethering(const char *path, gboolean on)
{
523
524
525
526
527
528
529
530
531
532
533
534
535
gboolean ok = FALSE;
DBusError error;
DBusConnection *connection;
dbus_error_init(&error);
connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
if (connection)
{
int i;
for (i=0; i<10; i++)
{
if (i>0)
{
536
537
if( !common_msleep(200) )
break;
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
}
if (connman_try_set_tethering(connection, path, on))
{
ok = TRUE;
break;
}
}
dbus_connection_unref(connection);
}
else
{
log_err("%s\n", error.message);
dbus_error_free (&error);
}
return ok;
554
555
}
556
557
558
/**
* Connman message handling
*/
559
static char * connman_parse_manager_reply(DBusMessage *reply, const char *req_service)
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
DBusMessageIter iter, subiter, origiter;
int type;
char *service;
dbus_message_iter_init(reply, &iter);
type = dbus_message_iter_get_arg_type(&iter);
dbus_message_iter_recurse(&iter, &subiter);
type = dbus_message_iter_get_arg_type(&subiter);
origiter = subiter;
iter = subiter;
while(type != DBUS_TYPE_INVALID)
{
if(type == DBUS_TYPE_STRUCT)
{
origiter = iter;
dbus_message_iter_recurse(&iter, &subiter);
type = dbus_message_iter_get_arg_type(&subiter);
if(type == DBUS_TYPE_OBJECT_PATH)
{
dbus_message_iter_get_basic(&subiter, &service);
log_debug("service = %s\n", service);
if(strstr(service, req_service))
{
log_debug("%s service found!\n", req_service);
586
return strdup(service);
587
588
589
590
591
592
593
594
}
}
}
dbus_message_iter_next(&origiter);
type = dbus_message_iter_get_arg_type(&origiter);
iter = origiter;
}
log_debug("end of list\n");
595
return 0;
596
597
}
598
static int connman_fill_connection_data(DBusMessage *reply, ipforward_data_t *ipforward)
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
DBusMessageIter array_iter, dict_iter, inside_dict_iter, variant_iter;
DBusMessageIter sub_array_iter, string_iter;
int type, next;
char *string;
log_debug("Filling in dns data\n");
dbus_message_iter_init(reply, &array_iter);
type = dbus_message_iter_get_arg_type(&array_iter);
dbus_message_iter_recurse(&array_iter, &dict_iter);
type = dbus_message_iter_get_arg_type(&dict_iter);
while(type != DBUS_TYPE_INVALID)
{
dbus_message_iter_recurse(&dict_iter, &inside_dict_iter);
type = dbus_message_iter_get_arg_type(&inside_dict_iter);
if(type == DBUS_TYPE_STRING)
{
dbus_message_iter_get_basic(&inside_dict_iter, &string);
//log_debug("string = %s\n", string);
if(!strcmp(string, "Nameservers"))
{
//log_debug("Trying to get Nameservers");
dbus_message_iter_next (&inside_dict_iter);
type = dbus_message_iter_get_arg_type(&inside_dict_iter);
dbus_message_iter_recurse(&inside_dict_iter, &variant_iter);
type = dbus_message_iter_get_arg_type(&variant_iter);
if(type == DBUS_TYPE_ARRAY)
{
dbus_message_iter_recurse(&variant_iter, &string_iter);
type = dbus_message_iter_get_arg_type(&string_iter);
if(type != DBUS_TYPE_STRING)
{
/* not online */
634
return 1;
635
636
637
638
639
640
641
642
643
644
}
dbus_message_iter_get_basic(&string_iter, &string);
log_debug("dns = %s\n", string);
ipforward->dns1 = strdup(string);
next = dbus_message_iter_next (&string_iter);
if(!next)
{
log_debug("No secundary dns\n");
/* FIXME: set the same dns for dns2 to avoid breakage */
ipforward->dns2 = strdup(string);
645
return 0;
646
647
648
649
}
dbus_message_iter_get_basic(&string_iter, &string);
log_debug("dns2 = %s\n", string);
ipforward->dns2 = strdup(string);
650
return 0;
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
}
}
else if(!strcmp(string, "State"))
{
//log_debug("Trying to get online state");
dbus_message_iter_next (&inside_dict_iter);
type = dbus_message_iter_get_arg_type(&inside_dict_iter);
dbus_message_iter_recurse(&inside_dict_iter, &variant_iter);
type = dbus_message_iter_get_arg_type(&variant_iter);
if(type == DBUS_TYPE_STRING)
{
dbus_message_iter_get_basic(&variant_iter, &string);
log_debug("Connection state = %s\n", string);
/* if cellular not online, connect it */
if(strcmp(string, "online"))
{
/* if in ready state connection might be up anyway */
if(strcmp(string, "ready"))
log_debug("Not online. Turning on cellular data connection.\n");
670
return 1;
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
}
}
}
else if(!strcmp(string, "Ethernet"))
{
dbus_message_iter_next (&inside_dict_iter);
type = dbus_message_iter_get_arg_type(&inside_dict_iter);
dbus_message_iter_recurse(&inside_dict_iter, &variant_iter);
type = dbus_message_iter_get_arg_type(&variant_iter);
if(type == DBUS_TYPE_ARRAY)
{
dbus_message_iter_recurse(&variant_iter, &sub_array_iter);
/* we want the second dict */
dbus_message_iter_next(&sub_array_iter);
/* we go into the dict and get the string */
dbus_message_iter_recurse(&sub_array_iter, &variant_iter);
type = dbus_message_iter_get_arg_type(&variant_iter);
if(type == DBUS_TYPE_STRING)
{
dbus_message_iter_get_basic(&variant_iter, &string);
if(!strcmp(string, "Interface"))
{
/* get variant and iter down in it */
dbus_message_iter_next(&variant_iter);
dbus_message_iter_recurse(&variant_iter, &string_iter);
dbus_message_iter_get_basic(&string_iter, &string);
log_debug("cellular interface = %s\n", string);
ipforward->nat_interface = strdup(string);
}
}
}
}
}
dbus_message_iter_next (&dict_iter);
type = dbus_message_iter_get_arg_type(&dict_iter);
}
710
return 0;
711
712
713
}
/**
714
* Turn on cellular connection if it is not on
715
*/
716
static int connman_set_cellular_online(DBusConnection *dbus_conn_connman, const char *service, int retry)
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
DBusMessage *msg = NULL, *reply;
DBusError error;
int ret = 0;
char *wifi = NULL;
dbus_error_init(&error);
if(!retry)
{
if ((msg = dbus_message_new_method_call("net.connman", "/", "net.connman.Manager", "GetServices")) != NULL)
{
if ((reply = dbus_connection_send_with_reply_and_block(dbus_conn_connman, msg, -1, NULL)) != NULL)
{
wifi = connman_parse_manager_reply(reply, "wifi");
dbus_message_unref(reply);
}
dbus_message_unref(msg);
}
if(wifi != NULL)
{
/* we must make sure that wifi is disconnected as sometimes cellular will not come up otherwise */
if ((msg = dbus_message_new_method_call("net.connman", wifi, "net.connman.Service", "Disconnect")) != NULL)
{
/* we don't care for the reply, which is empty anyway if all goes well */
ret = !dbus_connection_send(dbus_conn_connman, msg, NULL);
dbus_connection_flush(dbus_conn_connman);
dbus_message_unref(msg);
}
}
}
if ((msg = dbus_message_new_method_call("net.connman", service, "net.connman.Service", "Connect")) != NULL)
{
/* we don't care for the reply, which is empty anyway if all goes well */
752
ret = !dbus_connection_send(dbus_conn_connman, msg, NULL);
753
/* sleep for the connection to come up */
754
common_sleep(5);
755
756
/* make sure the message is sent before cleaning up and closing the connection */
dbus_connection_flush(dbus_conn_connman);
757
dbus_message_unref(msg);
758
}
759
760
761
if(wifi)
free(wifi);
762
763
return ret;
764
765
}
766
767
768
769
/*
* Turn on or off the wifi service
* wifistatus static variable tracks the state so we do not turn on the wifi if it was off
*
770
*/
771
772
static int connman_wifi_power_control(DBusConnection *dbus_conn_connman, int on)
{
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
static int wifistatus = 0;
int type = 0;
char *string;
DBusMessage *msg = NULL, *reply;
//DBusError error;
DBusMessageIter array_iter, dict_iter, inside_dict_iter, variant_iter;
if(!on)
{
/* check wifi status only before turning off */
if ((msg = dbus_message_new_method_call("net.connman", "/net/connman/technology/wifi", "net.connman.Technology", "GetProperties")) != NULL)
{
if ((reply = dbus_connection_send_with_reply_and_block(dbus_conn_connman, msg, -1, NULL)) != NULL)
{
dbus_message_iter_init(reply, &array_iter);
dbus_message_iter_recurse(&array_iter, &dict_iter);
type = dbus_message_iter_get_arg_type(&dict_iter);
while(type != DBUS_TYPE_INVALID)
{
dbus_message_iter_recurse(&dict_iter, &inside_dict_iter);
type = dbus_message_iter_get_arg_type(&inside_dict_iter);
if(type == DBUS_TYPE_STRING)
{
dbus_message_iter_get_basic(&inside_dict_iter, &string);
799
800
801
log_debug("string = %s\n", string);
if(!strcmp(string, "Powered"))
{
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
dbus_message_iter_next (&inside_dict_iter);
type = dbus_message_iter_get_arg_type(&inside_dict_iter);
dbus_message_iter_recurse(&inside_dict_iter, &variant_iter);
type = dbus_message_iter_get_arg_type(&variant_iter);
if(type == DBUS_TYPE_BOOLEAN)
{
dbus_message_iter_get_basic(&variant_iter, &wifistatus);
log_debug("Powered state = %d\n", wifistatus);
}
break;
}
}
dbus_message_iter_next (&dict_iter);
type = dbus_message_iter_get_arg_type(&dict_iter);
}
dbus_message_unref(reply);
}
dbus_message_unref(msg);
}
}
/* /net/connman/technology/wifi net.connman.Technology.SetProperty string:Powered variant:boolean:false */
if(wifistatus && !on)
825
common_system("/bin/dbus-send --print-reply --type=method_call --system --dest=net.connman /net/connman/technology/wifi net.connman.Technology.SetProperty string:Powered variant:boolean:false");
826
827
if(wifistatus && on)
/* turn on wifi after tethering is over and wifi was on before */
828
common_system("/bin/dbus-send --print-reply --type=method_call --system --dest=net.connman /net/connman/technology/wifi net.connman.Technology.SetProperty string:Powered variant:boolean:true");
829
830
return 0;
831
832
}
833
static int connman_get_connection_data(ipforward_data_t *ipforward)
834
{
835
836
837
838
839
DBusConnection *dbus_conn_connman = NULL;
DBusMessage *msg = NULL, *reply = NULL;
DBusError error;
char *service = NULL;
int online = 0, ret = 0;
840
841
dbus_error_init(&error);
842
843
844
845
846
if( (dbus_conn_connman = dbus_bus_get(DBUS_BUS_SYSTEM, &error)) == 0 )
{
log_err("Could not connect to dbus for connman\n");
}
847
848
849
/* turn off wifi in preparation for cellular connection if needed */
connman_wifi_power_control(dbus_conn_connman, 0);
850
851
852
853
/* get list of services so we can find out which one is the cellular */
if ((msg = dbus_message_new_method_call("net.connman", "/", "net.connman.Manager", "GetServices")) != NULL)
{
854
855
if ((reply = dbus_connection_send_with_reply_and_block(dbus_conn_connman, msg, -1, NULL)) != NULL)
{
856
service = connman_parse_manager_reply(reply, "cellular");
857
858
859
dbus_message_unref(reply);
}
dbus_message_unref(msg);
860
861
862
863
864
}
log_debug("service = %s\n", service);
if(service)
{
865
try_again:
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
if ((msg = dbus_message_new_method_call("net.connman", service, "net.connman.Service", "GetProperties")) != NULL)
{
if ((reply = dbus_connection_send_with_reply_and_block(dbus_conn_connman, msg, -1, NULL)) != NULL)
{
if(connman_fill_connection_data(reply, ipforward))
{
if(!connman_set_cellular_online(dbus_conn_connman, service, online) && !online)
{
online = 1;
goto try_again;
}
else
{
log_debug("Cannot connect to cellular data\n");
ret = 1;
}
}
dbus_message_unref(reply);
}
dbus_message_unref(msg);
}
}
dbus_connection_unref(dbus_conn_connman);
dbus_error_free(&error);
free(service);
891
return ret;
892
}
893
894
895
static int connman_reset_state(void)
{
896
897
DBusConnection *dbus_conn_connman = NULL;
DBusError error;
898
899
dbus_error_init(&error);
900
901
902
903
904
if( (dbus_conn_connman = dbus_bus_get(DBUS_BUS_SYSTEM, &error)) == 0 )
{
log_err("Could not connect to dbus for connman\n");
}
905
906
907
908
909
910
/* make sure connman turns wifi back on when we disconnect */
log_debug("Turning wifi back on\n");
connman_wifi_power_control(dbus_conn_connman, 1);
dbus_connection_unref(dbus_conn_connman);
dbus_error_free(&error);
911
912
return 0;
913
}
914
915
#endif /* CONNMAN */
916
/**
917
918
* Write out /etc/udhcpd.conf conf so the config is available when it gets started
*/
919
int network_set_up_dhcpd(mode_list_elem_t *data)
920
{
921
ipforward_data_t *ipforward = NULL;
922
int ret = 1;
923
924
925
926
/* Set up nat info only if it is required */
if(data->nat)
{
927
#ifdef OFONO
928
929
930
931
932
933
934
/* check if we are roaming or not */
if(get_roaming())
{
/* get permission to use roaming */
if(config_is_roaming_not_allowed())
goto end;
}
935
#endif /* OFONO */
936
ipforward = calloc(1, sizeof *ipforward);
937
#ifdef CONNMAN
938
939
940
941
942
943
if(connman_get_connection_data(ipforward))
{
log_debug("data connection not available!\n");
/* TODO: send a message to the UI */
goto end;
}
944
#else
945
946
if(resolv_conf_dns(ipforward))
goto end;
947
#endif /*CONNMAN */
948
949
950
}
/* ipforward can be NULL here, which is expected and handled in this function */
ret = network_write_udhcpd_conf(ipforward, data);
951
952
953
if(data->nat)
ret = network_set_usb_ip_forward(data, ipforward);
954
955
end:
956
957
/* the function checks if ipforward is NULL or not */
network_free_ipforward_data(ipforward);
958
return ret;
959
960
}
961
962
#if CONNMAN_WORKS_BETTER
static int append_variant(DBusMessageIter *iter, const char *property,
963
int type, const char *value)
964
{
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
DBusMessageIter variant;
const char *type_str;
switch(type) {
case DBUS_TYPE_BOOLEAN:
type_str = DBUS_TYPE_BOOLEAN_AS_STRING;
break;
case DBUS_TYPE_BYTE:
type_str = DBUS_TYPE_BYTE_AS_STRING;
break;
case DBUS_TYPE_STRING:
type_str = DBUS_TYPE_STRING_AS_STRING;
break;
case DBUS_TYPE_INT32:
type_str = DBUS_TYPE_INT32_AS_STRING;
break;
default:
return -EOPNOTSUPP;
}
984
985
986
987
988
989
990
991
dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &property);
dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, type_str,
&variant);
dbus_message_iter_append_basic(&variant, type, value);
dbus_message_iter_close_container(iter, &variant);
return 0;
992
993
994
}
#endif /* CONNMAN */
995
996
997
998
/**
* Activate the network interface
*
*/
999
int network_up(mode_list_elem_t *data)
1000
{