usb_moded-network.c 42.3 KB
Newer Older
1
/*
2 3 4
 * @file usb-moded_network.c : (De)activates network depending on the network setting system.
 *
 * Copyright (C) 2011 Nokia Corporation. All rights reserved.
5
 * Copyright (C) 2012-2019 Jolla. All rights reserved.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * @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
/* ========================================================================= *
 * Prototypes
 * ========================================================================= */

76 77 78
/* ------------------------------------------------------------------------- *
 * NETWORK
 * ------------------------------------------------------------------------- */
79

80
static void  network_free_ipforward_data (ipforward_data_t *ipforward);
81
static int   network_check_interface     (char *interface);
82 83
static char *network_get_interface       (const modedata_t *data);
static int   network_set_usb_ip_forward  (const modedata_t *data, ipforward_data_t *ipforward);
84 85
static void  network_clean_usb_ip_forward(void);
static int   network_checklink           (void);
86 87 88 89
static int   network_write_udhcpd_conf   (ipforward_data_t *ipforward, const modedata_t *data);
int          network_set_up_dhcpd        (const modedata_t *data);
int          network_up                  (const modedata_t *data);
int          network_down                (const modedata_t *data);
90 91
int          network_update              (void);

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
/* ------------------------------------------------------------------------- *
 * UTILITY
 * ------------------------------------------------------------------------- */

#ifdef OFONO
static int get_roaming   (void);
#endif // OFONO

#if CONNMAN_WORKS_BETTER
static int append_variant(DBusMessageIter *iter, const char *property, int type, const char *value);
#endif // CONNMAN_WORKS_BETTER

/* ------------------------------------------------------------------------- *
 * CONNMAN
 * ------------------------------------------------------------------------- */
107 108 109 110

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);
111
static int       connman_fill_connection_data(DBusMessage *reply, ipforward_data_t *ipforward);
112 113
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);
114
static int       connman_get_connection_data (ipforward_data_t *ipforward);
115
static int       connman_reset_state         (void);
116

117 118 119 120 121 122 123 124 125 126
/* ========================================================================= *
 * Data
 * ========================================================================= */

static const char default_interface[] = "usb0";

/* ========================================================================= *
 * Functions
 * ========================================================================= */

127
static void network_free_ipforward_data (ipforward_data_t *ipforward)
128
{
129 130
    LOG_REGISTER_CONTEXT;

131 132 133 134 135 136 137 138 139 140
    if(ipforward)
    {
        if(ipforward->dns1)
            free(ipforward->dns1);
        if(ipforward->dns2)
            free(ipforward->dns2);
        if(ipforward->nat_interface)
            free(ipforward->nat_interface);
        free(ipforward);
    }
141 142
}

143
/* This function checks if the configured interface exists */
144
static int network_check_interface(char *interface)
145
{
146 147
    LOG_REGISTER_CONTEXT;

148
    int ret = -1;
149

150 151 152 153 154 155
    if(interface)
    {
        char path[256];
        snprintf(path, sizeof path, "/sys/class/net/%s", interface);
        ret = access(path, F_OK);
    }
156

157
    return ret;
158 159
}

160
static char *network_get_interface(const modedata_t *data)
161
{
162 163
    LOG_REGISTER_CONTEXT;

164 165 166 167 168 169
    (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)
170
    {
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
        /* 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;
        }
186
    }
187

188 189 190
    log_debug("interface = %s\n", interface ?: "NULL");
    free(setting);
    return interface;
191
}
192

193 194
/**
 * Turn on ip forwarding on the usb interface
195
 * @return: 0 on success, 1 on failure
196
 */
197
static int network_set_usb_ip_forward(const modedata_t *data, ipforward_data_t *ipforward)
198
{
199 200
    LOG_REGISTER_CONTEXT;

201 202 203 204 205
    char *interface, *nat_interface;
    char command[128];

    interface = network_get_interface(data);
    if(interface == NULL)
206
        return 1;
207 208 209 210 211 212
    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");
213
#ifdef CONNMAN
214 215
        /* in case the cellular did not come up we want to make sure wifi gets restored */
        connman_reset_state();
216
#endif
217 218
        free(interface);
        free(nat_interface);
219
        return 1;
220 221 222
    }
    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);
223
    common_system(command);
224 225

    snprintf(command, 128, "/sbin/iptables -A FORWARD -i %s -o %s  -m state  --state RELATED,ESTABLISHED -j ACCEPT", nat_interface, interface);
226
    common_system(command);
227 228

    snprintf(command, 128, "/sbin/iptables -A FORWARD -i %s -o %s -j ACCEPT", interface, nat_interface);
229
    common_system(command);
230 231 232 233

    free(interface);
    free(nat_interface);
    log_debug("ipforwarding success!\n");
234
    return 0;
235 236
}

237
/**
238 239
 * Remove ip forward
 */
240
static void network_clean_usb_ip_forward(void)
241
{
242 243
    LOG_REGISTER_CONTEXT;

244
#ifdef CONNMAN
245
    connman_reset_state();
246
#endif
247
    write_to_file("/proc/sys/net/ipv4/ip_forward", "0");
248
    common_system("/sbin/iptables -F FORWARD");
249 250
}

251 252 253
#ifdef OFONO
/**
 * Get roaming data from ofono
254
 *
255 256 257 258
 * @return : 1 if roaming, 0 when not (or when ofono is unavailable)
 */
static int get_roaming(void)
{
259 260
    LOG_REGISTER_CONTEXT;

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
    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)
    {
278 279
        if ((reply = dbus_connection_send_with_reply_and_block(dbus_conn_ofono, msg, -1, NULL)) != NULL)
        {
280 281 282 283 284 285 286 287 288 289 290
            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);
291 292
        }
        dbus_message_unref(msg);
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
    }
    /* 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);
        }
317 318
        dbus_message_unref(msg);

319 320 321 322
        if(!strcmp("roaming", modem))
            ret = 1;
    }

323
    return ret;
324 325 326
}
#endif

327
#ifndef CONNMAN
328 329 330
/**
 * Read dns settings from /etc/resolv.conf
 */
331
static int resolv_conf_dns(ipforward_data_t *ipforward)
332
{
333 334
    LOG_REGISTER_CONTEXT;

335 336 337 338 339 340 341 342
    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)
343
        return 1;
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364

    /* 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;
    }
365
end:
366 367
    free(line);
    fclose(resolv);
368
    return 0;
369
}
370
#endif
371

372
static int network_checklink(void)
373
{
374 375
    LOG_REGISTER_CONTEXT;

376
    int ret = -1;
377 378
    char dest[sizeof UDHCP_CONFIG_PATH + 1];
    size_t len = readlink(UDHCP_CONFIG_LINK, dest, sizeof dest - 1);
379 380 381 382 383 384

    if (len > 0)
    {
        dest[len] = 0;
        ret = strcmp(dest, UDHCP_CONFIG_PATH);
    }
385
    return ret;
386 387
}

388 389 390 391
/**
 * Write udhcpd.conf
 * @ipforward : NULL if we want a simple config, otherwise include dns info etc...
 */
392
static int network_write_udhcpd_conf(ipforward_data_t *ipforward, const modedata_t *data)
393
{
394 395
    LOG_REGISTER_CONTEXT;

396 397 398 399 400 401 402 403 404 405 406 407
    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");
408
        return 1;
409 410 411 412 413 414
    }

    interface = network_get_interface(data);
    if(interface == NULL)
    {
        fclose(conffile);
415
        return 1;
416 417 418
    }
    /* generate start and end ip based on the setting */
    ip = config_get_network_setting(NETWORK_IP_KEY);
419 420
    ipstart = malloc(15);
    ipend = malloc(15);
421 422
    while(i < 15)
    {
423 424
        if(dot < 3)
        {
425 426 427 428
            if(ip[i] == '.')
                dot ++;
            ipstart[i] = ip[i];
            ipend[i] = ip[i];
429 430 431
        }
        else
        {
432 433 434
            ipstart[i] = '\0';
            ipend[i] = '\0';
            break;
435 436
        }
        i++;
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
    }
    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;
481 482

link:
483 484 485 486
    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);
487
        return 1;
488
    }
489 490

end:
491
    log_debug(UDHCP_CONFIG_LINK" created\n");
492
    return 0;
493 494
}

495
#ifdef CONNMAN
496

497 498 499 500 501
# 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"
502 503 504 505 506 507

/*
 * Configures tethering for the specified connman technology.
 */
static gboolean connman_try_set_tethering(DBusConnection *connection, const char *path, gboolean on)
{
508 509
    LOG_REGISTER_CONTEXT;

510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
    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;
553 554 555 556
}

gboolean connman_set_tethering(const char *path, gboolean on)
{
557 558
    LOG_REGISTER_CONTEXT;

559 560 561 562 563 564 565 566 567 568 569 570 571
    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)
            {
572 573
                if( !common_msleep(200) )
                    break;
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
            }
            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;
590 591
}

592 593 594
/**
 * Connman message handling
 */
595
static char * connman_parse_manager_reply(DBusMessage *reply, const char *req_service)
596
{
597 598
    LOG_REGISTER_CONTEXT;

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
    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);
624
                    return strdup(service);
625 626 627 628 629 630 631 632
                }
            }
        }
        dbus_message_iter_next(&origiter);
        type = dbus_message_iter_get_arg_type(&origiter);
        iter = origiter;
    }
    log_debug("end of list\n");
633
    return 0;
634 635
}

636
static int connman_fill_connection_data(DBusMessage *reply, ipforward_data_t *ipforward)
637
{
638 639
    LOG_REGISTER_CONTEXT;

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
    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 */
674
                        return 1;
675 676 677 678 679 680 681 682 683 684
                    }
                    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);
685
                        return 0;
686 687 688 689
                    }
                    dbus_message_iter_get_basic(&string_iter, &string);
                    log_debug("dns2 = %s\n", string);
                    ipforward->dns2 = strdup(string);
690
                    return 0;
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
                }
            }
            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");
710
                        return 1;
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
                    }

                }

            }
            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);
    }
750
    return 0;
751 752 753
}

/**
754
 * Turn on cellular connection if it is not on
755
 */
756
static int connman_set_cellular_online(DBusConnection *dbus_conn_connman, const char *service, int retry)
757
{
758 759
    LOG_REGISTER_CONTEXT;

760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
    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 */
794
        ret = !dbus_connection_send(dbus_conn_connman, msg, NULL);
795
        /* sleep for the connection to come up */
796
        common_sleep(5);
797 798
        /* make sure the message is sent before cleaning up and closing the connection */
        dbus_connection_flush(dbus_conn_connman);
799
        dbus_message_unref(msg);
800
    }
801

802 803
    if(wifi)
        free(wifi);
804

805
    return ret;
806 807
}

808 809 810 811
/*
 * 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
 *
812
 */
813 814
static int connman_wifi_power_control(DBusConnection *dbus_conn_connman, int on)
{
815 816
    LOG_REGISTER_CONTEXT;

817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
    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);
843 844 845
                        log_debug("string = %s\n", string);
                        if(!strcmp(string, "Powered"))
                        {
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
                            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)
869
        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");
870 871
    if(wifistatus && on)
        /* turn on wifi after tethering is over and wifi was on before */
872
        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");
873

874
    return 0;
875 876
}

877
static int connman_get_connection_data(ipforward_data_t *ipforward)
878
{
879 880
    LOG_REGISTER_CONTEXT;

881 882 883 884 885
    DBusConnection *dbus_conn_connman = NULL;
    DBusMessage *msg = NULL, *reply = NULL;
    DBusError error;
    char *service = NULL;
    int online = 0, ret = 0;
886

887
    dbus_error_init(&error);
888

889 890 891 892
    if( (dbus_conn_connman = dbus_bus_get(DBUS_BUS_SYSTEM, &error)) == 0 )
    {
        log_err("Could not connect to dbus for connman\n");
    }
893

894 895
    /* turn off wifi in preparation for cellular connection if needed */
    connman_wifi_power_control(dbus_conn_connman, 0);
896

897 898 899
    /* 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)
    {
900 901
        if ((reply = dbus_connection_send_with_reply_and_block(dbus_conn_connman, msg, -1, NULL)) != NULL)
        {
902
            service = connman_parse_manager_reply(reply, "cellular");
903 904 905
            dbus_message_unref(reply);
        }
        dbus_message_unref(msg);
906 907 908 909 910
    }

    log_debug("service = %s\n", service);
    if(service)
    {
911
try_again:
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
        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);
937
    return ret;
938
}
939 940 941

static int connman_reset_state(void)
{
942 943
    LOG_REGISTER_CONTEXT;

944 945
    DBusConnection *dbus_conn_connman = NULL;
    DBusError error;
946

947
    dbus_error_init(&error);
948

949 950 951 952
    if( (dbus_conn_connman = dbus_bus_get(DBUS_BUS_SYSTEM, &error)) == 0 )
    {
        log_err("Could not connect to dbus for connman\n");
    }
953

954 955 956 957 958
    /* 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);
959

960
    return 0;
961
}
962 963
#endif /* CONNMAN */

964
/**
965 966
 * Write out /etc/udhcpd.conf conf so the config is available when it gets started
 */
967
int network_set_up_dhcpd(const modedata_t *data)
968
{
969 970
    LOG_REGISTER_CONTEXT;

971
    ipforward_data_t *ipforward = NULL;
972
    int ret = 1;
973

974 975 976
    /* Set up nat info only if it is required */
    if(data->nat)
    {
977
#ifdef OFONO
978 979 980 981 982 983 984
        /* check if we are roaming or not */
        if(get_roaming())
        {
            /* get permission to use roaming */
            if(config_is_roaming_not_allowed())
                goto end;
        }
985
#endif /* OFONO */
986
        ipforward = calloc(1, sizeof *ipforward);
987
#ifdef CONNMAN
988 989 990 991 992 993
        if(connman_get_connection_data(ipforward))
        {
            log_debug("data connection not available!\n");
            /* TODO: send a message to the UI */
            goto end;
        }
994
#else
995 996
        if(resolv_conf_dns(ipforward))
            goto end;
997
#endif /*CONNMAN */
998 999 1000
    }
    /* ipforward can be NULL here, which is expected and handled in this function */
    ret = network_write_udhcpd_conf(ipforward, data);
1001

1002 1003
    if(data->nat)
        ret = network_set_usb_ip_forward(data, ipforward);
1004

1005
end:
1006 1007
    /* the function checks if ipforward is NULL or not */
    network_free_ipforward_data(ipforward);
1008
    return ret;
1009 1010
}

1011 1012
#if CONNMAN_WORKS_BETTER
static int append_variant(DBusMessageIter *iter, const char *property,
1013
                          int type, const char *value)
1014
{
1015 1016
    LOG_REGISTER_CONTEXT;

1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
    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;
    }
1036

1037 1038 1039 1040 1041 1042 1043
    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;
1044 1045 1046
}
#endif /* CONNMAN */

1047 1048 1049 1050
/**
 * Activate the network interface
 *
 */
1051
int network_up(const modedata_t *data)
1052
{
1053 1054
    LOG_REGISTER_CONTEXT;

1055 1056
    char *ip = NULL, *gateway = NULL;
    int ret = -1;
1057

1058
#if CONNMAN_WORKS_BETTER
1059 1060 1061 1062 1063 1064 1065 1066 1067
    DBusConnection *dbus_conn_connman = NULL;
    DBusMessage *msg = NULL, *reply = NULL;
    DBusMessageIter iter, variant, dict;
    DBusMessageIter msg_iter;
    DBusMessageIter dict_entry;
    DBusError error;
    const char *service = NULL;

    /* make sure connman will recognize the gadget interface NEEDED? */
1068 1069
    //common_system("/bin/dbus-send --print-reply --type=method_call --system --dest=net.connman /net/connman/technology/gadget net.connman.Technology.SetProperty string:Powered variant:boolean:true");
    //common_system("/sbin/ifconfig rndis0 up");
1070 1071

    log_debug("waiting for connman to pick up interface\n");
1072
    common_sleep(1);
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
    dbus_error_init(&error);

    if( (dbus_conn_connman = dbus_bus_get(DBUS_BUS_SYSTEM, &error)) == 0 )
    {
        log_err("Could not connect to dbus for connman\n");
    }

    /* get list of services so we can find out which one is the usb gadget */
    if ((msg = dbus_message_new_method_call("net.connman", "/", "net.connman.Manager", "GetServices")) != NULL)
    {
1083 1084
        if ((reply = dbus_connection_send_with_reply_and_block(dbus_conn_connman, msg, -1, NULL)) != NULL)
        {
1085
            service = connman_parse_manager_reply(reply, "gadget");
1086 1087 1088
            dbus_message_unref(reply);
        }
        dbus_message_unref(msg);
1089 1090 1091
    }

    if(service == NULL)
1092
        return 1;
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
    log_debug("gadget = %s\n", service);

    /* now we need to configure the connection */
    if ((msg = dbus_message_new_method_call("net.connman", service, "net.connman.Service", "SetProperty")) != NULL)
    {
        log_debug("iter init\n");
        dbus_message_iter_init_append(msg, &msg_iter);
        log_debug("iter append\n");
        // TODO: crashes here, need to rework this whole bit, connman dbus is hell
        dbus_message_iter_append_basic(&msg_iter, DBUS_TYPE_STRING, "IPv4.Configuration");
        log_debug("iter open container\n");
        dbus_message_iter_open_container(&msg_iter, DBUS_TYPE_VARIANT,
                                         DBUS_TYPE_ARRAY_AS_STRING
                                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
                                         DBUS_TYPE_STRING_AS_STRING
                                         DBUS_TYPE_VARIANT_AS_STRING
                                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
                                         &variant);

        log_debug("iter open container 2\n");
        dbus_message_iter_open_container(&variant, DBUS_TYPE_ARRAY,
                                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
                                         DBUS_TYPE_STRING_AS_STRING
                                         DBUS_TYPE_VARIANT_AS_STRING
                                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
                                         &dict);

        log_debug("Set Method\n");
        dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY, NULL, &dict_entry);
        append_variant(&dict_entry, "Method", DBUS_TYPE_STRING, "manual");
        dbus_message_iter_close_container(&dict, &dict_entry);

        log_debug("Set ip\n");
        ip = get_network_settings(NETWORK_IP_KEY);
        dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY, NULL, &dict_entry);
        append_variant(&dict_entry, "Address", DBUS_TYPE_STRING, ip);
        dbus_message_iter_close_container(&dict, &dict_entry);

        log_debug("Set netmask\n");
        dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY, NULL, &dict_entry);
        append_variant(&dict_entry, "Netmask", DBUS_TYPE_STRING, "255.255.255.0");
        dbus_message_iter_close_container(&dict, &dict_entry);

        log_debug("set gateway\n");
        gateway = config_get_network_setting(NETWORK_GATEWAY_KEY);
        if(gateway)
        {
            dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY, NULL, &dict_entry);
            append_variant(&dict_entry, "Gateway", DBUS_TYPE_STRING, gateway);
            dbus_message_iter_close_container(&dict, &dict_entry);
        }
        dbus_message_iter_close_container(&variant, &dict);
        dbus_message_iter_close_container(&msg_iter, &variant);
    }

    log_debug("Connect gadget\n");
    /* Finally we can bring it up */
    if ((msg = dbus_message_new_method_call("net.connman", service, "net.connman.Service", "Connect")) != NULL)
    {
1152 1153 1154 1155 1156
        /* we don't care for the reply, which is empty anyway if all goes well */
        ret = !dbus_connection_send(dbus_conn_connman, msg, NULL);
        /* make sure the message is sent before cleaning up and closing the connection */
        dbus_connection_flush(dbus_conn_connman);
        dbus_message_unref(msg);
1157 1158 1159 1160 1161 1162 1163 1164
    }
    dbus_connection_unref(dbus_conn_connman);
    dbus_error_free(&error);
    free(service);
    if(ip)
        free(ip);
    if(gateway)
        free(gateway);
1165
    return ret;
1166 1167

#else
1168 1169 1170 1171 1172 1173 1174
    char command[128];
    char *interface;
    char *netmask;

    interface = network_get_interface(data);

    if(interface == NULL)
1175
        return 1;
1176 1177 1178 1179 1180 1181 1182 1183 1184

    /* interface found, so we can get all the rest */
    ip = config_get_network_setting(NETWORK_IP_KEY);
    gateway = config_get_network_setting(NETWORK_GATEWAY_KEY);
    netmask = config_get_network_setting(NETWORK_NETMASK_KEY);

    if(!strcmp(ip, "dhcp"))
    {
        sprintf(command, "dhclient -d %s\n", interface);
1185
        ret = common_system(command);
1186 1187 1188
        if(ret != 0)
        {
            sprintf(command, "udhcpc -i %s\n", interface);
1189
            common_system(command);
1190 1191 1192 1193 1194
        }

    }
    else
    {
1195
        sprintf(command, "ifconfig %s %s netmask %s\n", interface, ip, netmask);
1196
        common_system(command);
1197 1198 1199 1200 1201 1202
    }

    /* TODO: Check first if there is a gateway set */
    if(gateway)
    {
        sprintf(command, "route add default gw %s\n", gateway);
1203
        common_system(command);
1204 1205 1206 1207 1208 1209 1210
    }

    free(interface);
    free(gateway);
    free(ip);
    free(netmask);

1211
    return 0;
1212
#endif /* CONNMAN */
1213 1214 1215 1216 1217 1218
}

/**
 * Deactivate the network interface
 *
 */
1219
int network_down(const modedata_t *data)
1220
{
1221 1222
    LOG_REGISTER_CONTEXT;

1223
#if CONNMAN_WORKS_BETTER
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
    DBusConnection *dbus_conn_connman = NULL;
    DBusMessage *msg = NULL, *reply = NULL;
    DBusError error;
    char *service = NULL;
    int ret = -1;

    dbus_error_init(&error);

    if( (dbus_conn_connman = dbus_bus_get(DBUS_BUS_SYSTEM, &error)) == 0 )
    {
        log_err("Could not connect to dbus for connman\n");
    }

    /* get list of services so we can find out which one is the usb gadget */
    if ((msg = dbus_message_new_method_call("net.connman", "/", "net.connman.Manager", "GetServices")) != NULL)
    {
1240 1241 1242 1243 1244 1245
        if ((reply = dbus_connection_send_with_reply_and_block(dbus_conn_connman, msg, -1, NULL)) != NULL)
        {
            service = connman_parse_manager_reply(reply, "gadget");
            dbus_message_unref(reply);
        }
        dbus_message_unref(msg);
1246
    }
1247

1248
    if(service == NULL)
1249
        return 1;
1250

1251 1252 1253
    /* Finally we can shut it down */
    if ((msg = dbus_message_new_method_call("net.connman", service, "net.connman.Service", "Disconnect")) != NULL)
    {
1254 1255 1256 1257 1258
        /* we don't care for the reply, which is empty anyway if all goes well */
        ret = !dbus_connection_send(dbus_conn_connman, msg, NULL);
        /* make sure the message is sent before cleaning up and closing the connection */
        dbus_connection_flush(dbus_conn_connman);
        dbus_message_unref(msg);
1259 1260
    }
    free(service);
1261

1262 1263 1264 1265
    /* dhcp server shutdown happens on disconnect automatically */
    if(data->nat)
        network_clean_usb_ip_forward();
    dbus_error_free(&error);
1266

1267
    return ret;
1268
#else
1269 1270 1271 1272 1273
    char *interface;
    char command[128];

    interface = network_get_interface(data);
    if(interface == NULL)
1274
        return 0;
1275

1276
    sprintf(command, "ifconfig %s down\n", interface);
1277
    common_system(command);
1278

1279 1280 1281
    /* dhcp client shutdown happens on disconnect automatically */
    if(data->nat)
        network_clean_usb_ip_forward();
1282

1283
    free(interface);
1284

1285
    return 0;
1286
#endif /* CONNMAN_IS_EVER_FIXED_FOR_USB */
1287
}
1288 1289 1290

/**
 * Update the network interface with the new setting if connected.
1291 1292 1293
 *
 */
int network_update(void)
1294
{
1295 1296
    LOG_REGISTER_CONTEXT;

1297
    if( control_get_cable_state() == CABLE_STATE_PC_CONNECTED ) {
1298
        modedata_t *data = worker_dup_usb_mode_data();
1299 1300 1301 1302
        if( data && data->network ) {
            network_down(data);
            network_up(data);
        }
1303
        modedata_free(data);
1304
    }
1305
    return 0;
1306
}