/
usb_moded-network.c
1260 lines (1117 loc) · 41.4 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
32
/*============================================================================= */
#include <stdio.h>
#include <stdlib.h>
33
#include <string.h>
34
35
36
37
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
38
39
40
#include <glib.h>
41
#include "usb_moded.h"
42
#include "usb_moded-network.h"
43
#include "usb_moded-config-private.h"
44
#include "usb_moded-log.h"
45
#include "usb_moded-modesetting.h"
46
47
#if CONNMAN || OFONO
48
49
50
# include <dbus/dbus.h>
# include <dbus/dbus-glib.h>
# include <dbus/dbus-glib-lowlevel.h>
51
#endif
52
53
54
55
/* ========================================================================= *
* Constants
* ========================================================================= */
56
57
58
59
60
61
62
63
#define UDHCP_CONFIG_PATH "/run/usb-moded/udhcpd.conf"
#define UDHCP_CONFIG_DIR "/run/usb-moded"
#define UDHCP_CONFIG_LINK "/etc/udhcpd.conf"
/* ========================================================================= *
* Types
* ========================================================================= */
64
65
/** IP forwarding configuration block */
66
67
typedef struct ipforward_data
{
68
69
70
71
72
73
/** Address of primary DNS */
char *dns1;
/** Address of secondary DNS */
char *dns2;
/** Interface from which packets should be forwarded */
char *nat_interface;
74
}ipforward_data;
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* ========================================================================= *
* Prototypes
* ========================================================================= */
/* -- network -- */
static void network_free_ipforward_data (struct ipforward_data *ipforward);
static int network_check_interface (char *interface);
static char *network_get_interface (struct mode_list_elem *data);
static int network_set_usb_ip_forward (struct mode_list_elem *data, struct ipforward_data *ipforward);
static void network_clean_usb_ip_forward(void);
static int network_checklink (void);
static int network_write_udhcpd_conf (struct ipforward_data *ipforward, struct mode_list_elem *data);
int network_set_up_dhcpd (struct mode_list_elem *data);
int network_up (struct mode_list_elem *data);
int network_down (struct mode_list_elem *data);
int network_update (void);
/* -- connman -- */
96
#ifdef CONNMAN
97
98
99
100
101
102
103
104
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);
static int connman_fill_connection_data(DBusMessage *reply, struct ipforward_data *ipforward);
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);
static int connman_get_connection_data (struct ipforward_data *ipforward);
static int connman_reset_state (void);
105
106
#endif
107
108
109
110
111
112
113
114
115
116
117
/* ========================================================================= *
* Data
* ========================================================================= */
static const char default_interface[] = "usb0";
/* ========================================================================= *
* Functions
* ========================================================================= */
static void network_free_ipforward_data (struct ipforward_data *ipforward)
118
{
119
120
121
122
123
124
125
126
127
128
if(ipforward)
{
if(ipforward->dns1)
free(ipforward->dns1);
if(ipforward->dns2)
free(ipforward->dns2);
if(ipforward->nat_interface)
free(ipforward->nat_interface);
free(ipforward);
}
129
130
}
131
/* This function checks if the configured interface exists */
132
static int network_check_interface(char *interface)
133
{
134
int ret = -1;
135
136
137
138
139
140
141
if(interface)
{
char path[256];
snprintf(path, sizeof path, "/sys/class/net/%s", interface);
ret = access(path, F_OK);
}
142
143
return ret;
144
145
}
146
static char* network_get_interface(struct mode_list_elem *data)
147
{
148
149
150
151
152
153
(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)
154
{
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* 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;
}
170
}
171
172
173
174
log_debug("interface = %s\n", interface ?: "NULL");
free(setting);
return interface;
175
}
176
177
178
/**
* Turn on ip forwarding on the usb interface
179
* @return: 0 on success, 1 on failure
180
*/
181
static int network_set_usb_ip_forward(struct mode_list_elem *data, struct ipforward_data *ipforward)
182
{
183
184
185
186
187
188
189
190
191
192
193
194
char *interface, *nat_interface;
char command[128];
interface = network_get_interface(data);
if(interface == NULL)
return(1);
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");
195
#ifdef CONNMAN
196
197
/* in case the cellular did not come up we want to make sure wifi gets restored */
connman_reset_state();
198
#endif
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
free(interface);
free(nat_interface);
return(1);
}
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);
usbmoded_system(command);
snprintf(command, 128, "/sbin/iptables -A FORWARD -i %s -o %s -m state --state RELATED,ESTABLISHED -j ACCEPT", nat_interface, interface);
usbmoded_system(command);
snprintf(command, 128, "/sbin/iptables -A FORWARD -i %s -o %s -j ACCEPT", interface, nat_interface);
usbmoded_system(command);
free(interface);
free(nat_interface);
log_debug("ipforwarding success!\n");
return(0);
217
218
}
219
/**
220
221
* Remove ip forward
*/
222
static void network_clean_usb_ip_forward(void)
223
{
224
#ifdef CONNMAN
225
connman_reset_state();
226
#endif
227
228
write_to_file("/proc/sys/net/ipv4/ip_forward", "0");
usbmoded_system("/sbin/iptables -F FORWARD");
229
230
}
231
232
233
#ifdef OFONO
/**
* Get roaming data from ofono
234
*
235
236
237
238
* @return : 1 if roaming, 0 when not (or when ofono is unavailable)
*/
static int get_roaming(void)
{
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
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)
{
256
257
if ((reply = dbus_connection_send_with_reply_and_block(dbus_conn_ofono, msg, -1, NULL)) != NULL)
{
258
259
260
261
262
263
264
265
266
267
268
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);
269
270
}
dbus_message_unref(msg);
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
}
/* 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);
}
295
296
dbus_message_unref(msg);
297
298
299
300
301
if(!strcmp("roaming", modem))
ret = 1;
}
return(ret);
302
303
304
}
#endif
305
#ifndef CONNMAN
306
307
308
/**
* Read dns settings from /etc/resolv.conf
*/
309
static int resolv_conf_dns(struct ipforward_data *ipforward)
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
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)
return(1);
/* 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;
}
341
end:
342
343
344
free(line);
fclose(resolv);
return(0);
345
}
346
#endif
347
348
static int network_checklink(void)
349
{
350
351
352
353
354
355
356
357
358
359
int ret = -1;
char dest[sizeof(UDHCP_CONFIG_PATH)+1];
size_t len = readlink(UDHCP_CONFIG_LINK, dest, sizeof(dest)-1);
if (len > 0)
{
dest[len] = 0;
ret = strcmp(dest, UDHCP_CONFIG_PATH);
}
return(ret);
360
361
}
362
363
364
365
366
/**
* Write udhcpd.conf
* @ipforward : NULL if we want a simple config, otherwise include dns info etc...
*/
static int network_write_udhcpd_conf(struct ipforward_data *ipforward, struct mode_list_elem *data)
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
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");
return(1);
}
interface = network_get_interface(data);
if(interface == NULL)
{
fclose(conffile);
return(1);
}
/* generate start and end ip based on the setting */
ip = config_get_network_setting(NETWORK_IP_KEY);
ipstart = malloc(sizeof(char)*15);
ipend = malloc(sizeof(char)*15);
while(i < 15)
{
395
396
if(dot < 3)
{
397
398
399
400
if(ip[i] == '.')
dot ++;
ipstart[i] = ip[i];
ipend[i] = ip[i];
401
402
403
}
else
{
404
405
406
ipstart[i] = '\0';
ipend[i] = '\0';
break;
407
408
}
i++;
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
}
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;
453
454
link:
455
456
457
458
459
460
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);
return(1);
}
461
462
end:
463
464
log_debug(UDHCP_CONFIG_LINK" created\n");
return(0);
465
466
}
467
#ifdef CONNMAN
468
469
470
471
472
473
# 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"
474
475
476
477
478
479
/*
* Configures tethering for the specified connman technology.
*/
static gboolean connman_try_set_tethering(DBusConnection *connection, const char *path, gboolean on)
{
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
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;
523
524
525
526
}
gboolean connman_set_tethering(const char *path, gboolean on)
{
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
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)
{
usbmoded_msleep(200);
}
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;
557
558
}
559
560
561
/**
* Connman message handling
*/
562
static char * connman_parse_manager_reply(DBusMessage *reply, const char *req_service)
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
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);
return(strdup(service));
}
}
}
dbus_message_iter_next(&origiter);
type = dbus_message_iter_get_arg_type(&origiter);
iter = origiter;
}
log_debug("end of list\n");
return(0);
599
600
}
601
static int connman_fill_connection_data(DBusMessage *reply, struct ipforward_data *ipforward)
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
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 */
return(1);
}
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);
return(0);
}
dbus_message_iter_get_basic(&string_iter, &string);
log_debug("dns2 = %s\n", string);
ipforward->dns2 = strdup(string);
return(0);
}
}
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");
return(1);
}
}
}
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);
}
return(0);
714
715
716
}
/**
717
* Turn on cellular connection if it is not on
718
*/
719
static int connman_set_cellular_online(DBusConnection *dbus_conn_connman, const char *service, int retry)
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
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 */
755
ret = !dbus_connection_send(dbus_conn_connman, msg, NULL);
756
757
758
759
/* sleep for the connection to come up */
usbmoded_sleep(5);
/* make sure the message is sent before cleaning up and closing the connection */
dbus_connection_flush(dbus_conn_connman);
760
dbus_message_unref(msg);
761
}
762
763
764
if(wifi)
free(wifi);
765
766
return(ret);
767
768
}
769
770
771
772
/*
* 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
*
773
*/
774
775
static int connman_wifi_power_control(DBusConnection *dbus_conn_connman, int on)
{
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
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);
802
803
804
log_debug("string = %s\n", string);
if(!strcmp(string, "Powered"))
{
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
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)
usbmoded_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");
if(wifistatus && on)
/* turn on wifi after tethering is over and wifi was on before */
usbmoded_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");
return(0);
834
835
}
836
837
static int connman_get_connection_data(struct ipforward_data *ipforward)
{
838
839
840
841
842
DBusConnection *dbus_conn_connman = NULL;
DBusMessage *msg = NULL, *reply = NULL;
DBusError error;
char *service = NULL;
int online = 0, ret = 0;
843
844
dbus_error_init(&error);
845
846
847
848
849
if( (dbus_conn_connman = dbus_bus_get(DBUS_BUS_SYSTEM, &error)) == 0 )
{
log_err("Could not connect to dbus for connman\n");
}
850
851
852
/* turn off wifi in preparation for cellular connection if needed */
connman_wifi_power_control(dbus_conn_connman, 0);
853
854
855
856
/* 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)
{
857
858
if ((reply = dbus_connection_send_with_reply_and_block(dbus_conn_connman, msg, -1, NULL)) != NULL)
{
859
service = connman_parse_manager_reply(reply, "cellular");
860
861
862
dbus_message_unref(reply);
}
dbus_message_unref(msg);
863
864
865
866
867
}
log_debug("service = %s\n", service);
if(service)
{
868
try_again:
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
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);
return(ret);
895
}
896
897
898
static int connman_reset_state(void)
{
899
900
DBusConnection *dbus_conn_connman = NULL;
DBusError error;
901
902
dbus_error_init(&error);
903
904
905
906
907
if( (dbus_conn_connman = dbus_bus_get(DBUS_BUS_SYSTEM, &error)) == 0 )
{
log_err("Could not connect to dbus for connman\n");
}
908
909
910
911
912
913
/* 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);
914
915
return(0);
916
}
917
918
#endif /* CONNMAN */
919
/**
920
921
* Write out /etc/udhcpd.conf conf so the config is available when it gets started
*/
922
int network_set_up_dhcpd(struct mode_list_elem *data)
923
{
924
925
struct ipforward_data *ipforward = NULL;
int ret = 1;
926
927
928
929
/* Set up nat info only if it is required */
if(data->nat)
{
930
#ifdef OFONO
931
932
933
934
935
936
937
/* check if we are roaming or not */
if(get_roaming())
{
/* get permission to use roaming */
if(config_is_roaming_not_allowed())
goto end;
}
938
#endif /* OFONO */
939
940
ipforward = malloc(sizeof(struct ipforward_data));
memset(ipforward, 0, sizeof(struct ipforward_data));
941
#ifdef CONNMAN
942
943
944
945
946
947
if(connman_get_connection_data(ipforward))
{
log_debug("data connection not available!\n");
/* TODO: send a message to the UI */
goto end;
}
948
#else
949
950
if(resolv_conf_dns(ipforward))
goto end;
951
#endif /*CONNMAN */
952
953
954
}
/* ipforward can be NULL here, which is expected and handled in this function */
ret = network_write_udhcpd_conf(ipforward, data);
955
956
957
if(data->nat)
ret = network_set_usb_ip_forward(data, ipforward);
958
959
end:
960
961
962
/* the function checks if ipforward is NULL or not */
network_free_ipforward_data(ipforward);
return(ret);
963
964
}
965
966
#if CONNMAN_WORKS_BETTER
static int append_variant(DBusMessageIter *iter, const char *property,
967
int type, const char *value)
968
{
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
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;
}
988
989
990
991
992
993
994
995
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;
996
997
998
}
#endif /* CONNMAN */
999
1000
/**
* Activate the network interface