Skip to content

Latest commit

 

History

History
1247 lines (1107 loc) · 41 KB

usb_moded-network.c

File metadata and controls

1247 lines (1107 loc) · 41 KB
 
Aug 24, 2018
Aug 24, 2018
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"
Sep 5, 2018
Sep 5, 2018
32
Aug 24, 2018
Aug 24, 2018
33
#include "usb_moded-config-private.h"
Sep 5, 2018
Sep 5, 2018
34
#include "usb_moded-control.h"
Sep 12, 2013
Sep 12, 2013
35
#include "usb_moded-log.h"
Nov 27, 2013
Nov 27, 2013
36
#include "usb_moded-modesetting.h"
Sep 5, 2018
Sep 5, 2018
37
#include "usb_moded-worker.h"
Sep 5, 2018
Sep 5, 2018
38
39
40
41
42
43
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
Feb 11, 2014
Feb 11, 2014
45
#if CONNMAN || OFONO
Aug 24, 2018
Aug 24, 2018
46
# include <dbus/dbus.h>
Jul 4, 2013
Jul 4, 2013
47
#endif
Aug 24, 2018
Aug 24, 2018
49
50
51
/* ========================================================================= *
* Constants
* ========================================================================= */
Mar 9, 2015
Mar 9, 2015
52
Aug 24, 2018
Aug 24, 2018
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
* ========================================================================= */
Mar 13, 2017
Mar 13, 2017
60
61
/** IP forwarding configuration block */
Sep 5, 2018
Sep 5, 2018
62
typedef struct ipforward_data_t
Dec 3, 2013
Dec 3, 2013
63
{
Aug 24, 2018
Aug 24, 2018
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;
Sep 5, 2018
Sep 5, 2018
70
}ipforward_data_t;
Aug 24, 2018
Aug 24, 2018
72
73
74
75
76
77
/* ========================================================================= *
* Prototypes
* ========================================================================= */
/* -- network -- */
Sep 5, 2018
Sep 5, 2018
78
static void network_free_ipforward_data (ipforward_data_t *ipforward);
Aug 24, 2018
Aug 24, 2018
79
static int network_check_interface (char *interface);
Sep 5, 2018
Sep 5, 2018
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);
Aug 24, 2018
Aug 24, 2018
82
83
static void network_clean_usb_ip_forward(void);
static int network_checklink (void);
Sep 5, 2018
Sep 5, 2018
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);
Aug 24, 2018
Aug 24, 2018
88
89
90
91
int network_update (void);
/* -- connman -- */
Jun 1, 2014
Jun 1, 2014
92
#ifdef CONNMAN
Aug 24, 2018
Aug 24, 2018
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);
Sep 5, 2018
Sep 5, 2018
96
static int connman_fill_connection_data(DBusMessage *reply, ipforward_data_t *ipforward);
Aug 24, 2018
Aug 24, 2018
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);
Sep 5, 2018
Sep 5, 2018
99
static int connman_get_connection_data (ipforward_data_t *ipforward);
Aug 24, 2018
Aug 24, 2018
100
static int connman_reset_state (void);
Jun 1, 2014
Jun 1, 2014
101
102
#endif
Aug 24, 2018
Aug 24, 2018
103
104
105
106
107
108
109
110
111
112
/* ========================================================================= *
* Data
* ========================================================================= */
static const char default_interface[] = "usb0";
/* ========================================================================= *
* Functions
* ========================================================================= */
Sep 5, 2018
Sep 5, 2018
113
static void network_free_ipforward_data (ipforward_data_t *ipforward)
Dec 15, 2013
Dec 15, 2013
114
{
Aug 24, 2018
Aug 24, 2018
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);
}
Dec 15, 2013
Dec 15, 2013
125
126
}
Jul 9, 2014
Jul 9, 2014
127
/* This function checks if the configured interface exists */
Aug 24, 2018
Aug 24, 2018
128
static int network_check_interface(char *interface)
Jul 9, 2014
Jul 9, 2014
129
{
Aug 24, 2018
Aug 24, 2018
130
int ret = -1;
Jul 9, 2014
Jul 9, 2014
131
Aug 24, 2018
Aug 24, 2018
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);
}
Jul 9, 2014
Jul 9, 2014
138
Aug 24, 2018
Aug 24, 2018
139
return ret;
Jul 9, 2014
Jul 9, 2014
140
141
}
Sep 5, 2018
Sep 5, 2018
142
static char* network_get_interface(mode_list_elem_t *data)
Jul 4, 2013
Jul 4, 2013
143
{
Aug 24, 2018
Aug 24, 2018
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)
Jul 7, 2016
Jul 7, 2016
150
{
Aug 24, 2018
Aug 24, 2018
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;
}
Jul 7, 2016
Jul 7, 2016
166
}
Jul 9, 2014
Jul 9, 2014
167
Aug 24, 2018
Aug 24, 2018
168
169
170
log_debug("interface = %s\n", interface ?: "NULL");
free(setting);
return interface;
Jul 4, 2013
Jul 4, 2013
171
}
Nov 27, 2013
Nov 27, 2013
173
174
/**
* Turn on ip forwarding on the usb interface
Mar 14, 2014
Mar 14, 2014
175
* @return: 0 on success, 1 on failure
Nov 27, 2013
Nov 27, 2013
176
*/
Sep 5, 2018
Sep 5, 2018
177
static int network_set_usb_ip_forward(mode_list_elem_t *data, ipforward_data_t *ipforward)
Nov 27, 2013
Nov 27, 2013
178
{
Aug 24, 2018
Aug 24, 2018
179
180
181
182
183
char *interface, *nat_interface;
char command[128];
interface = network_get_interface(data);
if(interface == NULL)
Sep 5, 2018
Sep 5, 2018
184
return 1;
Aug 24, 2018
Aug 24, 2018
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");
Jun 1, 2014
Jun 1, 2014
191
#ifdef CONNMAN
Aug 24, 2018
Aug 24, 2018
192
193
/* in case the cellular did not come up we want to make sure wifi gets restored */
connman_reset_state();
Jun 1, 2014
Jun 1, 2014
194
#endif
Aug 24, 2018
Aug 24, 2018
195
196
free(interface);
free(nat_interface);
Sep 5, 2018
Sep 5, 2018
197
return 1;
Aug 24, 2018
Aug 24, 2018
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);
Sep 5, 2018
Sep 5, 2018
201
common_system(command);
Aug 24, 2018
Aug 24, 2018
202
203
snprintf(command, 128, "/sbin/iptables -A FORWARD -i %s -o %s -m state --state RELATED,ESTABLISHED -j ACCEPT", nat_interface, interface);
Sep 5, 2018
Sep 5, 2018
204
common_system(command);
Aug 24, 2018
Aug 24, 2018
205
206
snprintf(command, 128, "/sbin/iptables -A FORWARD -i %s -o %s -j ACCEPT", interface, nat_interface);
Sep 5, 2018
Sep 5, 2018
207
common_system(command);
Aug 24, 2018
Aug 24, 2018
208
209
210
211
free(interface);
free(nat_interface);
log_debug("ipforwarding success!\n");
Sep 5, 2018
Sep 5, 2018
212
return 0;
Nov 27, 2013
Nov 27, 2013
213
214
}
Aug 24, 2018
Aug 24, 2018
215
/**
Dec 11, 2013
Dec 11, 2013
216
217
* Remove ip forward
*/
Aug 24, 2018
Aug 24, 2018
218
static void network_clean_usb_ip_forward(void)
Dec 11, 2013
Dec 11, 2013
219
{
Jun 1, 2014
Jun 1, 2014
220
#ifdef CONNMAN
Aug 24, 2018
Aug 24, 2018
221
connman_reset_state();
Jun 1, 2014
Jun 1, 2014
222
#endif
Aug 24, 2018
Aug 24, 2018
223
write_to_file("/proc/sys/net/ipv4/ip_forward", "0");
Sep 5, 2018
Sep 5, 2018
224
common_system("/sbin/iptables -F FORWARD");
Dec 11, 2013
Dec 11, 2013
225
226
}
Feb 11, 2014
Feb 11, 2014
227
228
229
#ifdef OFONO
/**
* Get roaming data from ofono
Aug 24, 2018
Aug 24, 2018
230
*
Feb 11, 2014
Feb 11, 2014
231
232
233
234
* @return : 1 if roaming, 0 when not (or when ofono is unavailable)
*/
static int get_roaming(void)
{
Aug 24, 2018
Aug 24, 2018
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)
{
Feb 11, 2014
Feb 11, 2014
252
253
if ((reply = dbus_connection_send_with_reply_and_block(dbus_conn_ofono, msg, -1, NULL)) != NULL)
{
Aug 24, 2018
Aug 24, 2018
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);
Feb 11, 2014
Feb 11, 2014
265
266
}
dbus_message_unref(msg);
Aug 24, 2018
Aug 24, 2018
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);
}
Feb 11, 2014
Feb 11, 2014
291
292
dbus_message_unref(msg);
Aug 24, 2018
Aug 24, 2018
293
294
295
296
if(!strcmp("roaming", modem))
ret = 1;
}
Sep 5, 2018
Sep 5, 2018
297
return ret;
Feb 11, 2014
Feb 11, 2014
298
299
300
}
#endif
Dec 13, 2013
Dec 13, 2013
301
#ifndef CONNMAN
Dec 3, 2013
Dec 3, 2013
302
303
304
/**
* Read dns settings from /etc/resolv.conf
*/
Sep 5, 2018
Sep 5, 2018
305
static int resolv_conf_dns(ipforward_data_t *ipforward)
Dec 3, 2013
Dec 3, 2013
306
{
Aug 24, 2018
Aug 24, 2018
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)
Sep 5, 2018
Sep 5, 2018
315
return 1;
Aug 24, 2018
Aug 24, 2018
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;
}
Feb 4, 2014
Feb 4, 2014
337
end:
Aug 24, 2018
Aug 24, 2018
338
339
free(line);
fclose(resolv);
Sep 5, 2018
Sep 5, 2018
340
return 0;
Dec 3, 2013
Dec 3, 2013
341
}
Dec 13, 2013
Dec 13, 2013
342
#endif
Dec 3, 2013
Dec 3, 2013
343
Aug 24, 2018
Aug 24, 2018
344
static int network_checklink(void)
Mar 9, 2015
Mar 9, 2015
345
{
Aug 24, 2018
Aug 24, 2018
346
int ret = -1;
Sep 5, 2018
Sep 5, 2018
347
348
char dest[sizeof UDHCP_CONFIG_PATH + 1];
size_t len = readlink(UDHCP_CONFIG_LINK, dest, sizeof dest - 1);
Aug 24, 2018
Aug 24, 2018
349
350
351
352
353
354
if (len > 0)
{
dest[len] = 0;
ret = strcmp(dest, UDHCP_CONFIG_PATH);
}
Sep 5, 2018
Sep 5, 2018
355
return ret;
Mar 9, 2015
Mar 9, 2015
356
357
}
Aug 24, 2018
Aug 24, 2018
358
359
360
361
/**
* Write udhcpd.conf
* @ipforward : NULL if we want a simple config, otherwise include dns info etc...
*/
Sep 5, 2018
Sep 5, 2018
362
static int network_write_udhcpd_conf(ipforward_data_t *ipforward, mode_list_elem_t *data)
Dec 10, 2013
Dec 10, 2013
363
{
Aug 24, 2018
Aug 24, 2018
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");
Sep 5, 2018
Sep 5, 2018
376
return 1;
Aug 24, 2018
Aug 24, 2018
377
378
379
380
381
382
}
interface = network_get_interface(data);
if(interface == NULL)
{
fclose(conffile);
Sep 5, 2018
Sep 5, 2018
383
return 1;
Aug 24, 2018
Aug 24, 2018
384
385
386
}
/* generate start and end ip based on the setting */
ip = config_get_network_setting(NETWORK_IP_KEY);
Sep 5, 2018
Sep 5, 2018
387
388
ipstart = malloc(15);
ipend = malloc(15);
Aug 24, 2018
Aug 24, 2018
389
390
while(i < 15)
{
Dec 10, 2013
Dec 10, 2013
391
392
if(dot < 3)
{
Aug 24, 2018
Aug 24, 2018
393
394
395
396
if(ip[i] == '.')
dot ++;
ipstart[i] = ip[i];
ipend[i] = ip[i];
Dec 10, 2013
Dec 10, 2013
397
398
399
}
else
{
Aug 24, 2018
Aug 24, 2018
400
401
402
ipstart[i] = '\0';
ipend[i] = '\0';
break;
Dec 10, 2013
Dec 10, 2013
403
404
}
i++;
Aug 24, 2018
Aug 24, 2018
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;
Mar 5, 2014
Mar 5, 2014
449
450
link:
Aug 24, 2018
Aug 24, 2018
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);
Sep 5, 2018
Sep 5, 2018
455
return 1;
Aug 24, 2018
Aug 24, 2018
456
}
Mar 5, 2014
Mar 5, 2014
457
458
end:
Aug 24, 2018
Aug 24, 2018
459
log_debug(UDHCP_CONFIG_LINK" created\n");
Sep 5, 2018
Sep 5, 2018
460
return 0;
Dec 10, 2013
Dec 10, 2013
461
462
}
Dec 3, 2013
Dec 3, 2013
463
#ifdef CONNMAN
Apr 15, 2015
Apr 15, 2015
464
Aug 24, 2018
Aug 24, 2018
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"
Apr 15, 2015
Apr 15, 2015
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)
{
Aug 24, 2018
Aug 24, 2018
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;
Apr 15, 2015
Apr 15, 2015
519
520
521
522
}
gboolean connman_set_tethering(const char *path, gboolean on)
{
Aug 24, 2018
Aug 24, 2018
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)
{
Sep 5, 2018
Sep 5, 2018
536
537
if( !common_msleep(200) )
break;
Aug 24, 2018
Aug 24, 2018
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;
Apr 15, 2015
Apr 15, 2015
554
555
}
Dec 3, 2013
Dec 3, 2013
556
557
558
/**
* Connman message handling
*/
Nov 4, 2014
Nov 4, 2014
559
static char * connman_parse_manager_reply(DBusMessage *reply, const char *req_service)
Dec 3, 2013
Dec 3, 2013
560
{
Aug 24, 2018
Aug 24, 2018
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);
Sep 5, 2018
Sep 5, 2018
586
return strdup(service);
Aug 24, 2018
Aug 24, 2018
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");
Sep 5, 2018
Sep 5, 2018
595
return 0;
Dec 3, 2013
Dec 3, 2013
596
597
}
Sep 5, 2018
Sep 5, 2018
598
static int connman_fill_connection_data(DBusMessage *reply, ipforward_data_t *ipforward)
Dec 3, 2013
Dec 3, 2013
599
{
Aug 24, 2018
Aug 24, 2018
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 */
Sep 5, 2018
Sep 5, 2018
634
return 1;
Aug 24, 2018
Aug 24, 2018
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);
Sep 5, 2018
Sep 5, 2018
645
return 0;
Aug 24, 2018
Aug 24, 2018
646
647
648
649
}
dbus_message_iter_get_basic(&string_iter, &string);
log_debug("dns2 = %s\n", string);
ipforward->dns2 = strdup(string);
Sep 5, 2018
Sep 5, 2018
650
return 0;
Aug 24, 2018
Aug 24, 2018
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");
Sep 5, 2018
Sep 5, 2018
670
return 1;
Aug 24, 2018
Aug 24, 2018
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);
}
Sep 5, 2018
Sep 5, 2018
710
return 0;
Dec 3, 2013
Dec 3, 2013
711
712
713
}
/**
Aug 24, 2018
Aug 24, 2018
714
* Turn on cellular connection if it is not on
Dec 3, 2013
Dec 3, 2013
715
*/
May 13, 2014
May 13, 2014
716
static int connman_set_cellular_online(DBusConnection *dbus_conn_connman, const char *service, int retry)
Dec 3, 2013
Dec 3, 2013
717
{
Aug 24, 2018
Aug 24, 2018
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 */
Dec 11, 2013
Dec 11, 2013
752
ret = !dbus_connection_send(dbus_conn_connman, msg, NULL);
Aug 24, 2018
Aug 24, 2018
753
/* sleep for the connection to come up */
Sep 5, 2018
Sep 5, 2018
754
common_sleep(5);
Aug 24, 2018
Aug 24, 2018
755
756
/* make sure the message is sent before cleaning up and closing the connection */
dbus_connection_flush(dbus_conn_connman);
Dec 3, 2013
Dec 3, 2013
757
dbus_message_unref(msg);
Aug 24, 2018
Aug 24, 2018
758
}
Dec 3, 2013
Dec 3, 2013
759
Aug 24, 2018
Aug 24, 2018
760
761
if(wifi)
free(wifi);
Nov 4, 2014
Nov 4, 2014
762
Sep 5, 2018
Sep 5, 2018
763
return ret;
Dec 3, 2013
Dec 3, 2013
764
765
}
Jun 1, 2014
Jun 1, 2014
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
*
Aug 24, 2018
Aug 24, 2018
770
*/
Jun 1, 2014
Jun 1, 2014
771
772
static int connman_wifi_power_control(DBusConnection *dbus_conn_connman, int on)
{
Aug 24, 2018
Aug 24, 2018
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);
Jun 1, 2014
Jun 1, 2014
799
800
801
log_debug("string = %s\n", string);
if(!strcmp(string, "Powered"))
{
Aug 24, 2018
Aug 24, 2018
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)
Sep 5, 2018
Sep 5, 2018
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");
Aug 24, 2018
Aug 24, 2018
826
827
if(wifistatus && on)
/* turn on wifi after tethering is over and wifi was on before */
Sep 5, 2018
Sep 5, 2018
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");
Aug 24, 2018
Aug 24, 2018
829
Sep 5, 2018
Sep 5, 2018
830
return 0;
Jun 1, 2014
Jun 1, 2014
831
832
}
Sep 5, 2018
Sep 5, 2018
833
static int connman_get_connection_data(ipforward_data_t *ipforward)
Dec 3, 2013
Dec 3, 2013
834
{
Aug 24, 2018
Aug 24, 2018
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;
Dec 3, 2013
Dec 3, 2013
840
Aug 24, 2018
Aug 24, 2018
841
dbus_error_init(&error);
Dec 3, 2013
Dec 3, 2013
842
Aug 24, 2018
Aug 24, 2018
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");
}
Dec 3, 2013
Dec 3, 2013
847
Aug 24, 2018
Aug 24, 2018
848
849
/* turn off wifi in preparation for cellular connection if needed */
connman_wifi_power_control(dbus_conn_connman, 0);
Jun 1, 2014
Jun 1, 2014
850
Aug 24, 2018
Aug 24, 2018
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)
{
Dec 3, 2013
Dec 3, 2013
854
855
if ((reply = dbus_connection_send_with_reply_and_block(dbus_conn_connman, msg, -1, NULL)) != NULL)
{
Aug 24, 2018
Aug 24, 2018
856
service = connman_parse_manager_reply(reply, "cellular");
Dec 3, 2013
Dec 3, 2013
857
858
859
dbus_message_unref(reply);
}
dbus_message_unref(msg);
Aug 24, 2018
Aug 24, 2018
860
861
862
863
864
}
log_debug("service = %s\n", service);
if(service)
{
Dec 10, 2013
Dec 10, 2013
865
try_again:
Aug 24, 2018
Aug 24, 2018
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);
Sep 5, 2018
Sep 5, 2018
891
return ret;
Dec 3, 2013
Dec 3, 2013
892
}
Jun 1, 2014
Jun 1, 2014
893
894
895
static int connman_reset_state(void)
{
Aug 24, 2018
Aug 24, 2018
896
897
DBusConnection *dbus_conn_connman = NULL;
DBusError error;
Jun 1, 2014
Jun 1, 2014
898
Aug 24, 2018
Aug 24, 2018
899
dbus_error_init(&error);
Jun 1, 2014
Jun 1, 2014
900
Aug 24, 2018
Aug 24, 2018
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");
}
Jun 1, 2014
Jun 1, 2014
905
Aug 24, 2018
Aug 24, 2018
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);
Jun 1, 2014
Jun 1, 2014
911
Sep 5, 2018
Sep 5, 2018
912
return 0;
Jun 1, 2014
Jun 1, 2014
913
}
Dec 3, 2013
Dec 3, 2013
914
915
#endif /* CONNMAN */
Aug 24, 2018
Aug 24, 2018
916
/**
Dec 3, 2013
Dec 3, 2013
917
918
* Write out /etc/udhcpd.conf conf so the config is available when it gets started
*/
Sep 5, 2018
Sep 5, 2018
919
int network_set_up_dhcpd(mode_list_elem_t *data)
Dec 3, 2013
Dec 3, 2013
920
{
Sep 5, 2018
Sep 5, 2018
921
ipforward_data_t *ipforward = NULL;
Aug 24, 2018
Aug 24, 2018
922
int ret = 1;
Dec 3, 2013
Dec 3, 2013
923
Aug 24, 2018
Aug 24, 2018
924
925
926
/* Set up nat info only if it is required */
if(data->nat)
{
Feb 11, 2014
Feb 11, 2014
927
#ifdef OFONO
Aug 24, 2018
Aug 24, 2018
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;
}
Feb 11, 2014
Feb 11, 2014
935
#endif /* OFONO */
Sep 5, 2018
Sep 5, 2018
936
ipforward = calloc(1, sizeof *ipforward);
Dec 3, 2013
Dec 3, 2013
937
#ifdef CONNMAN
Aug 24, 2018
Aug 24, 2018
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;
}
Dec 3, 2013
Dec 3, 2013
944
#else
Aug 24, 2018
Aug 24, 2018
945
946
if(resolv_conf_dns(ipforward))
goto end;
Dec 3, 2013
Dec 3, 2013
947
#endif /*CONNMAN */
Aug 24, 2018
Aug 24, 2018
948
949
950
}
/* ipforward can be NULL here, which is expected and handled in this function */
ret = network_write_udhcpd_conf(ipforward, data);
Dec 11, 2013
Dec 11, 2013
951
Aug 24, 2018
Aug 24, 2018
952
953
if(data->nat)
ret = network_set_usb_ip_forward(data, ipforward);
Dec 15, 2013
Dec 15, 2013
954
Dec 11, 2013
Dec 11, 2013
955
end:
Aug 24, 2018
Aug 24, 2018
956
957
/* the function checks if ipforward is NULL or not */
network_free_ipforward_data(ipforward);
Sep 5, 2018
Sep 5, 2018
958
return ret;
Dec 3, 2013
Dec 3, 2013
959
960
}
Nov 4, 2014
Nov 4, 2014
961
962
#if CONNMAN_WORKS_BETTER
static int append_variant(DBusMessageIter *iter, const char *property,
Aug 24, 2018
Aug 24, 2018
963
int type, const char *value)
Nov 4, 2014
Nov 4, 2014
964
{
Aug 24, 2018
Aug 24, 2018
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;
}
Nov 4, 2014
Nov 4, 2014
984
Aug 24, 2018
Aug 24, 2018
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;
Nov 4, 2014
Nov 4, 2014
992
993
994
}
#endif /* CONNMAN */
995
996
997
998
/**
* Activate the network interface
*
*/
Sep 5, 2018
Sep 5, 2018
999
int network_up(mode_list_elem_t *data)