2 * Network Configuration Module
4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 #include <vconf-keys.h>
26 #include <arpa/inet.h>
27 #include <netinet/in.h>
28 #include <sys/ioctl.h>
35 #include "wifi-state.h"
36 #include "wifi-power.h"
37 #include "network-state.h"
38 #include "network-dpm.h"
39 #include "network-monitor.h"
40 #include "netsupplicant.h"
42 #include "generated-code.h"
43 /* Define TCP buffer sizes for various networks */
44 /* ReadMin, ReadInitial, ReadMax */ /* WriteMin, WriteInitial, WriteMax */
45 #define NET_TCP_BUFFERSIZE_DEFAULT_READ "4096 87380 704512"
46 #define NET_TCP_BUFFERSIZE_DEFAULT_WRITE "4096 16384 110208"
47 #define NET_TCP_BUFFERSIZE_WIFI_READ "524288 1048576 2560000"
48 #define NET_TCP_BUFFERSIZE_WIFI_WRITE "524288 1048576 2560000"
49 #define NET_TCP_BUFFERSIZE_LTE_READ "524288 1048576 2560000"
50 #define NET_TCP_BUFFERSIZE_LTE_WRITE "524288 1048576 2560000"
51 #define NET_TCP_BUFFERSIZE_UMTS_READ "4094 87380 704512"
52 #define NET_TCP_BUFFERSIZE_UMTS_WRITE "4096 16384 110208"
53 #define NET_TCP_BUFFERSIZE_HSPA_READ "4092 87380 704512"
54 #define NET_TCP_BUFFERSIZE_HSPA_WRITE "4096 16384 262144"
55 #define NET_TCP_BUFFERSIZE_HSDPA_READ "4092 87380 704512"
56 #define NET_TCP_BUFFERSIZE_HSDPA_WRITE "4096 16384 262144"
57 #define NET_TCP_BUFFERSIZE_HSUPA_READ "4092 87380 704512"
58 #define NET_TCP_BUFFERSIZE_HSUPA_WRITE "4096 16384 262144"
59 #define NET_TCP_BUFFERSIZE_HSPAP_READ "4092 87380 1220608"
60 #define NET_TCP_BUFFERSIZE_HSPAP_WRITE "4096 16384 1220608"
61 #define NET_TCP_BUFFERSIZE_EDGE_READ "4093 26280 35040"
62 #define NET_TCP_BUFFERSIZE_EDGE_WRITE "4096 16384 35040"
63 #define NET_TCP_BUFFERSIZE_GPRS_READ "4096 30000 30000"
64 #define NET_TCP_BUFFERSIZE_GPRS_WRITE "4096 8760 11680"
66 #define NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX "1048576"
67 #define NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX "2097152"
68 #define NET_TCP_BUFFERSIZE_LTE_RMEM_MAX "5242880"
70 #define NET_TCP_BUFFERSIZE_WIFID_WMEM_MAX "2097152"
72 #define NET_PROC_SYS_NET_IPV4_TCP_RMEM "/proc/sys/net/ipv4/tcp_rmem"
73 #define NET_PROC_SYS_NET_IPv4_TCP_WMEM "/proc/sys/net/ipv4/tcp_wmem"
74 #define NET_PROC_SYS_NET_CORE_RMEM_MAX "/proc/sys/net/core/rmem_max"
75 #define NET_PROC_SYS_NET_CORE_WMEM_MAX "/proc/sys/net/core/wmem_max"
77 #define ROUTE_EXEC_PATH "/sbin/route"
79 #define TELEPHONY_SERVICE "com.tcore.ps"
80 #define TELEPHONY_MASTER_INTERFACE TELEPHONY_SERVICE ".master"
81 #define TELEPHONY_MODEM_INTERFACE TELEPHONY_SERVICE ".modem"
82 #define TELEPHONY_PROFILE_INTERFACE TELEPHONY_SERVICE ".context"
83 #define TELEPHONY_MASTER_PATH "/"
84 #define NET_PROFILE_NAME_LEN_MAX 512
87 char profile_name[NET_PROFILE_NAME_LEN_MAX];
90 static Network *netconfigstate = NULL;
92 struct netconfig_default_connection {
104 static struct netconfig_default_connection
105 netconfig_default_connection_info = { NULL, };
107 gboolean netconfig_iface_network_state_ethernet_cable_state(gint32 *state);
109 static gboolean __netconfig_is_connected(GVariantIter *array)
111 gboolean is_connected = FALSE;
112 GVariant *variant = NULL;
114 const gchar *value = NULL;
116 while (g_variant_iter_loop(array, "{sv}", &key, &variant)) {
117 if (g_strcmp0(key, "State") != 0)
120 if (g_variant_is_of_type(variant, G_VARIANT_TYPE_STRING)) {
121 value = g_variant_get_string(variant, NULL);
122 if (g_strcmp0(value, "ready") == 0 || g_strcmp0(value, "online") == 0)
127 g_variant_unref(variant);
134 static char *__netconfig_get_default_profile(void)
136 GVariant *message = NULL;
139 gchar *default_profile = NULL;
142 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
143 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
144 "GetServices", NULL);
145 if (message == NULL) {
146 ERR("Failed to get profiles");
150 g_variant_get(message, "(a(oa{sv}))", &iter);
151 while (g_variant_iter_loop(iter, "(oa{sv})", &object_path, &next)) {
152 if (object_path == NULL)
155 if (netconfig_is_cellular_profile(object_path) && !netconfig_is_cellular_internet_profile(object_path))
158 if (__netconfig_is_connected(next) == TRUE) {
159 default_profile = g_strdup(object_path);
161 g_variant_iter_free(next);
165 g_variant_iter_free(iter);
166 g_variant_unref(message);
168 return default_profile;
171 static int __netconfig_telephony_get_modem_object_path(GSList **modem_path_list)
174 GVariantIter *iter_modem = NULL;
175 GVariantIter *modem_properties = NULL;
176 const char *modem_path;
178 result = netconfig_invoke_dbus_method(TELEPHONY_SERVICE, TELEPHONY_MASTER_PATH,
179 TELEPHONY_MASTER_INTERFACE, "GetModems", NULL);
180 if (result == NULL) {
181 ERR("Failed to get modem path list");
185 g_variant_get(result, "(a{sa{ss}})", &iter_modem);
186 while (g_variant_iter_loop(iter_modem, "{sa{ss}}", &modem_path, &modem_properties)) {
187 *modem_path_list = g_slist_append(*modem_path_list, g_strdup(modem_path));
188 DBG("modem object path: %s", modem_path);
191 g_variant_iter_free(iter_modem);
192 g_variant_unref(result);
197 static int __netconfig_telephony_get_profile_list(net_profile_name_t **profile_list,
201 int count = 0, i = 0;
202 const char *str = NULL;
204 GVariantIter *iter = NULL;
205 GSList *profiles = NULL, *list = NULL;
206 net_profile_name_t *plist = NULL;
208 GSList *modem_path_list = NULL;
209 const char *path = NULL;
211 ret = __netconfig_telephony_get_modem_object_path(&modem_path_list);
213 ERR("Failed to get modems path list");
215 g_slist_free_full(modem_path_list, g_free);
219 for (list = modem_path_list; list != NULL; list = list->next) {
220 path = (const char *)list->data;
222 DBG("path: %s", path);
223 result = netconfig_invoke_dbus_method(TELEPHONY_SERVICE, path,
224 TELEPHONY_MODEM_INTERFACE, "GetProfileList", NULL);
225 if (result == NULL) {
226 DBG("Failed to get profiles: %s", path);
230 g_variant_get(result, "(as)", &iter);
231 while (g_variant_iter_loop(iter, "s", &str))
232 profiles = g_slist_append(profiles, g_strdup(str));
234 g_variant_iter_free(iter);
235 g_variant_unref(result);
238 g_slist_free_full(modem_path_list, g_free);
240 count = g_slist_length(profiles);
242 plist = (net_profile_name_t*)malloc(sizeof(net_profile_name_t) * count);
249 ERR("Failed to allocate memory");
255 for (list = profiles, i = 0; list != NULL; list = list->next, i++)
256 g_strlcpy(plist[i].profile_name,
257 (const char *)list->data, NET_PROFILE_NAME_LEN_MAX);
259 *profile_list = plist;
260 *profile_count = count;
263 g_slist_free_full(profiles, g_free);
268 static int __netconfig_telephony_search_pdp_profile(const char* profile_name, net_profile_name_t* pdp_name)
271 net_profile_name_t* profile_list = NULL;
272 char* prof_name = NULL;
273 char* tel_prof_name = NULL;
274 char* found_ptr = NULL;
275 int profile_count = 0;
278 /* Get pdp profile list from telephony service */
279 ret = __netconfig_telephony_get_profile_list(&profile_list, &profile_count);
281 ERR("Failed to get profile list from telephony service");
282 g_free(profile_list);
286 if (profile_list == NULL || profile_count <= 0) {
287 ERR("There is no PDP profiles");
288 g_free(profile_list);
292 /* Find matching profile */
293 prof_name = strrchr(profile_name, '/') + 1;
294 for (i = 0; i < profile_count; i++) {
295 tel_prof_name = strrchr(profile_list[i].profile_name, '/') + 1;
296 found_ptr = strstr(prof_name, tel_prof_name);
298 if (found_ptr != NULL && g_strcmp0(found_ptr, tel_prof_name) == 0) {
299 g_strlcpy(pdp_name->profile_name,
300 profile_list[i].profile_name, NET_PROFILE_NAME_LEN_MAX);
302 DBG("PDP profile name found in cellular profile: %s", pdp_name->profile_name);
307 if (i >= profile_count) {
308 ERR("There is no matching PDP profiles");
309 g_free(profile_list);
313 g_free(profile_list);
318 static gboolean __netconfig_telephony_get_metered_info(net_profile_name_t* pdp_name)
322 const gchar *key = NULL;
323 const gchar *value = NULL;
324 gboolean ret = FALSE;
326 if (pdp_name == NULL) {
327 ERR("Invalid parameter!");
331 result = netconfig_invoke_dbus_method(TELEPHONY_SERVICE, pdp_name->profile_name,
332 TELEPHONY_PROFILE_INTERFACE, "GetProfile", NULL);
333 if (result == NULL) {
334 ERR("_net_invoke_dbus_method failed");
338 g_variant_get(result, "(a{ss})", &iter);
339 while (g_variant_iter_next(iter, "{ss}", &key, &value)) {
340 if (g_strcmp0(key, "is_metered") == 0) {
344 if (g_strcmp0(value, "TRUE") == 0)
349 g_variant_iter_free(iter);
350 g_variant_unref(result);
352 DBG("is_metered = %s", ret ? "TRUE" : "FALSE");
357 static void __netconfig_get_default_connection_info(const char *profile)
359 GVariant *message = NULL, *variant = NULL, *variant2 = NULL;
360 GVariantIter *iter = NULL, *iter1 = NULL, *service = NULL;
361 GVariant *next = NULL;
366 gboolean found_profile = 0;
368 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
369 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
370 "GetServices", NULL);
371 if (message == NULL) {
372 ERR("Failed to get services informations");
376 g_variant_get(message, "(a(oa{sv}))", &service);
377 if (service == NULL) {
378 ERR("Failed to get services iter");
382 while (g_variant_iter_loop(service, "(oa{sv})", &obj_path, &iter)) {
383 if (g_strcmp0(obj_path, profile) == 0) {
390 if (iter == NULL || found_profile == 0) {
391 ERR("Profile %s doesn't exist", profile);
395 while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
396 const gchar *value = NULL;
398 if (g_strcmp0(key, "Name") == 0 &&
399 netconfig_is_wifi_profile(profile) == TRUE) {
400 if (g_variant_is_of_type(next, G_VARIANT_TYPE_STRING)) {
401 value = g_variant_get_string(next, NULL);
403 netconfig_default_connection_info.essid = g_strdup(value);
405 } else if (g_strcmp0(key, "Ethernet") == 0) {
406 g_variant_get(next, "a{sv}", &iter1);
409 while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
410 if (g_strcmp0(key1, "Interface") == 0) {
411 value = g_variant_get_string(variant, NULL);
412 netconfig_default_connection_info.ifname = g_strdup(value);
413 } else if (g_strcmp0(key1, "Address") == 0) {
414 value = g_variant_get_string(variant, NULL);
415 netconfig_default_connection_info.mac_address = g_strdup(value);
418 g_variant_iter_free(iter1);
419 } else if (g_strcmp0(key, "IPv4") == 0) {
420 g_variant_get(next, "a{sv}", &iter1);
423 while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
424 if (g_strcmp0(key1, "Address") == 0) {
425 value = g_variant_get_string(variant, NULL);
426 netconfig_default_connection_info.ipaddress = g_strdup(value);
429 g_variant_iter_free(iter1);
430 } else if (g_strcmp0(key, "IPv6") == 0) {
431 g_variant_get(next, "a{sv}", &iter1);
434 while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
435 if (g_strcmp0(key1, "Address") == 0) {
436 value = g_variant_get_string(variant, NULL);
437 netconfig_default_connection_info.ipaddress6 = g_strdup(value);
440 g_variant_iter_free(iter1);
442 } else if (g_strcmp0(key, "Proxy") == 0) {
443 g_variant_get(next, "a{sv}", &iter1);
446 while (g_variant_iter_loop(iter1, "{sv}", &key2, &variant2)) {
447 GVariantIter *iter_sub = NULL;
449 if (g_strcmp0(key2, "Servers") == 0) {
450 if (!g_variant_is_of_type(variant2, G_VARIANT_TYPE_STRING_ARRAY)) {
452 g_variant_unref(variant2);
456 g_variant_get(variant2, "as", &iter_sub);
457 g_variant_iter_loop(iter_sub, "s", &value);
458 g_variant_iter_free(iter_sub);
459 if (value != NULL && (strlen(value) > 0))
460 netconfig_default_connection_info.proxy = g_strdup(value);
461 } else if (g_strcmp0(key2, "Method") == 0) {
462 if (g_variant_is_of_type(variant2, G_VARIANT_TYPE_STRING)) {
464 g_variant_unref(variant2);
468 value = g_variant_get_string(variant2, NULL);
469 if (g_strcmp0(value, "direct") == 0) {
470 g_free(netconfig_default_connection_info.proxy);
471 netconfig_default_connection_info.proxy = NULL;
474 g_variant_unref(variant2);
479 g_variant_iter_free(iter1);
480 } else if (g_strcmp0(key, "Frequency") == 0) {
481 if (g_variant_is_of_type(next, G_VARIANT_TYPE_UINT16)) {
482 freq = g_variant_get_uint16(next);
483 netconfig_default_connection_info.freq = freq;
488 if (netconfig_is_cellular_profile(profile) == TRUE) {
489 net_profile_name_t pdp_name;
492 ret = __netconfig_telephony_search_pdp_profile(profile, &pdp_name);
493 if (ret >= 0 && strlen(pdp_name.profile_name) > 0)
494 if (__netconfig_telephony_get_metered_info(&pdp_name))
495 netconfig_default_connection_info.is_metered = TRUE;
500 g_variant_unref(message);
503 g_variant_iter_free(iter);
506 g_variant_iter_free(service);
511 static void __netconfig_adjust_tcp_buffer_size(void)
513 int fdr = 0, fdw = 0;
514 int fdrmax = 0, fdwmax = 0;
515 const char *rbuf_size = NULL;
516 const char *wbuf_size = NULL;
517 const char *rmax_size = NULL;
518 const char *wmax_size = NULL;
519 const char *profile = netconfig_get_default_profile();
521 if (profile == NULL) {
522 DBG("There is no default connection");
524 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
525 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
526 } else if (netconfig_is_wifi_profile(profile) == TRUE) {
527 DBG("Default connection: Wi-Fi");
529 rbuf_size = NET_TCP_BUFFERSIZE_WIFI_READ;
530 wbuf_size = NET_TCP_BUFFERSIZE_WIFI_WRITE;
531 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
532 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
533 } else if (netconfig_is_cellular_profile(profile) == TRUE) {
534 int telephony_svctype = 0, telephony_pstype = 0;
536 netconfig_get_telephony_network_type(&telephony_svctype, &telephony_pstype);
537 DBG("Default cellular %d, %d", telephony_svctype, telephony_pstype);
539 switch (telephony_pstype) {
540 case VCONFKEY_TELEPHONY_PSTYPE_HSPA:
541 rbuf_size = NET_TCP_BUFFERSIZE_HSPA_READ;
542 wbuf_size = NET_TCP_BUFFERSIZE_HSPA_WRITE;
544 case VCONFKEY_TELEPHONY_PSTYPE_HSUPA:
545 rbuf_size = NET_TCP_BUFFERSIZE_HSUPA_READ;
546 wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
548 case VCONFKEY_TELEPHONY_PSTYPE_HSDPA:
549 rbuf_size = NET_TCP_BUFFERSIZE_HSDPA_READ;
550 wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
552 #if !defined TIZEN_WEARABLE
553 case VCONFKEY_TELEPHONY_PSTYPE_HSPAP:
554 rbuf_size = NET_TCP_BUFFERSIZE_HSPAP_READ;
555 wbuf_size = NET_TCP_BUFFERSIZE_HSPAP_WRITE;
559 switch (telephony_svctype) {
560 case VCONFKEY_TELEPHONY_SVCTYPE_LTE:
561 rbuf_size = NET_TCP_BUFFERSIZE_LTE_READ;
562 wbuf_size = NET_TCP_BUFFERSIZE_LTE_WRITE;
563 rmax_size = NET_TCP_BUFFERSIZE_LTE_RMEM_MAX;
565 case VCONFKEY_TELEPHONY_SVCTYPE_3G:
566 rbuf_size = NET_TCP_BUFFERSIZE_UMTS_READ;
567 wbuf_size = NET_TCP_BUFFERSIZE_UMTS_WRITE;
569 case VCONFKEY_TELEPHONY_SVCTYPE_2_5G_EDGE:
570 rbuf_size = NET_TCP_BUFFERSIZE_EDGE_READ;
571 wbuf_size = NET_TCP_BUFFERSIZE_EDGE_WRITE;
573 case VCONFKEY_TELEPHONY_SVCTYPE_2_5G:
574 rbuf_size = NET_TCP_BUFFERSIZE_GPRS_READ;
575 wbuf_size = NET_TCP_BUFFERSIZE_GPRS_WRITE;
578 /* TODO: Check LTE support */
579 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
580 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
586 DBG("Default TCP buffer configured");
588 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
589 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
592 if (rbuf_size != NULL) {
593 fdr = open(NET_PROC_SYS_NET_IPV4_TCP_RMEM, O_RDWR | O_CLOEXEC);
595 if (fdr < 0 || write(fdr, rbuf_size, strlen(rbuf_size)) < 0)
596 ERR("Failed to set TCP read buffer size");
602 if (wbuf_size != NULL) {
603 fdw = open(NET_PROC_SYS_NET_IPv4_TCP_WMEM, O_RDWR | O_CLOEXEC);
605 if (fdw < 0 || write(fdw, wbuf_size, strlen(wbuf_size)) < 0)
606 ERR("Failed to set TCP write buffer size");
613 if (rmax_size == NULL)
614 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
615 if (wmax_size == NULL)
616 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
618 if (rmax_size != NULL) {
619 fdrmax = open(NET_PROC_SYS_NET_CORE_RMEM_MAX, O_RDWR | O_CLOEXEC);
621 if (fdrmax < 0 || write(fdrmax, rmax_size, strlen(rmax_size)) < 0)
622 ERR("Failed to set TCP rmem_max size");
628 if (wmax_size != NULL) {
629 fdwmax = open(NET_PROC_SYS_NET_CORE_WMEM_MAX, O_RDWR | O_CLOEXEC);
631 if (fdwmax < 0 || write(fdwmax, wmax_size, strlen(wmax_size)) < 0)
632 ERR("Failed to set TCP wmem_max size");
639 static void __netconfig_update_default_connection_info(void)
641 int old_network_status = 0;
642 const char *profile = netconfig_get_default_profile();
643 const char *ip_addr = netconfig_get_default_ipaddress();
644 const char *ip_addr6 = netconfig_get_default_ipaddress6();
645 const char *proxy_addr = netconfig_get_default_proxy();
646 unsigned int freq = netconfig_get_default_frequency();
648 if (emulator_is_emulated() == TRUE) {
650 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr);
652 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
654 if (ip_addr6 != NULL)
655 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, ip_addr6);
657 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, "");
663 DBG("Reset network state configuration");
665 DBG("profile[%s] ipv4(%s) ipv6(%s) proxy(%s)", profile, ip_addr, ip_addr6, proxy_addr);
667 netconfig_vconf_get_int(VCONFKEY_NETWORK_STATUS, &old_network_status);
669 if (profile == NULL && old_network_status != VCONFKEY_NETWORK_OFF) {
670 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
672 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
673 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, "");
674 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
676 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 0);
677 netconfig_set_vconf_int("memory/private/wifi/frequency", 0);
679 DBG("Successfully clear IP and PROXY up");
681 } else if (profile != NULL) {
682 char *old_ip = vconf_get_str(VCONFKEY_NETWORK_IP);
683 char *old_ip6 = vconf_get_str(VCONFKEY_NETWORK_IP6);
684 char *old_proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY);
686 if (netconfig_is_wifi_profile(profile) == TRUE) {
687 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_WIFI);
688 netconfig_set_vconf_int("memory/private/wifi/frequency", freq);
690 netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
691 EKEY_NETWORK_STATUS, EVAL_NETWORK_WIFI);
692 } else if (netconfig_is_cellular_profile(profile)) {
694 if (!netconfig_is_cellular_internet_profile(profile)) {
695 DBG("connection is not a internet profile - stop to update the cellular state");
705 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_CELLULAR);
707 netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
708 EKEY_NETWORK_STATUS, EVAL_NETWORK_CELLULAR);
709 } else if (netconfig_is_ethernet_profile(profile) == TRUE) {
710 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_ETHERNET);
711 netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
712 EKEY_NETWORK_STATUS, EVAL_NETWORK_ETHERNET);
713 } else if (netconfig_is_bluetooth_profile(profile) == TRUE) {
714 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_BLUETOOTH);
715 netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
716 EKEY_NETWORK_STATUS, EVAL_NETWORK_BT);
718 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
719 netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
720 EKEY_NETWORK_STATUS, EVAL_NETWORK_DISCONNECTED);
723 if (g_strcmp0(old_ip, ip_addr) != 0 || old_ip == NULL) {
725 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr);
726 else if (old_ip != NULL && strlen(old_ip) > 0)
727 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
732 if (g_strcmp0(old_ip6, ip_addr6) != 0 || old_ip6 == NULL) {
733 if (ip_addr6 != NULL)
734 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, ip_addr6);
735 else if (old_ip6 != NULL && strlen(old_ip6) > 0)
736 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, "");
741 if (g_strcmp0(old_proxy, proxy_addr) != 0) {
742 if (proxy_addr == NULL)
743 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
745 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, proxy_addr);
750 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 1);
752 DBG("Successfully update default network configuration");
755 __netconfig_adjust_tcp_buffer_size();
758 static gboolean __netconfig_is_tech_state_connected(void)
760 gboolean ret = FALSE;
761 GVariant *message = NULL, *variant;
762 GVariantIter *iter, *next;
766 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
767 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
768 "GetTechnologies", NULL);
770 if (message == NULL) {
771 DBG("Fail to get technology state");
775 g_variant_get(message, "(a(oa{sv}))", &iter);
776 while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
780 while (g_variant_iter_loop(next, "{sv}", &key, &variant)) {
782 if (g_strcmp0(key, "Connected") == 0) {
783 data = g_variant_get_boolean(variant);
784 DBG("%s [%s: %s]", path, key, data ? "True" : "False");
789 g_variant_unref(variant);
790 g_variant_iter_free(next);
798 g_variant_iter_free(iter);
799 g_variant_unref(message);
804 static void __netconfig_update_if_service_connected(void)
806 GVariant *message = NULL, *var;
807 GVariantIter *iter, *next;
811 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
812 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
813 "GetServices", NULL);
815 if (message == NULL) {
816 ERR("Failed to get services");
820 g_variant_get(message, "(a(oa{sv}))", &iter);
821 while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
825 if (g_str_has_prefix(path,
826 CONNMAN_WIFI_SERVICE_PROFILE_PREFIX) == TRUE) {
827 if (g_strrstr(path + strlen(CONNMAN_WIFI_SERVICE_PROFILE_PREFIX),
829 /* skip hidden profiles */
833 } else if (g_str_has_prefix(path,
834 CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX) == TRUE) {
840 while (g_variant_iter_loop(next, "{sv}", &key, &var)) {
841 if (g_strcmp0(key, "State") == 0) {
842 const gchar *sdata = NULL;
843 sdata = g_variant_get_string(var, NULL);
844 DBG("%s [%s: %s]", path, key, sdata);
846 if (g_strcmp0(sdata, "online") == 0 || g_strcmp0(sdata, "ready") == 0) {
848 /* Found a connected WiFi / 3G service.
849 * Lets update the default profile info.
851 netconfig_update_default_profile((const gchar*)path);
854 g_variant_unref(var);
855 g_variant_iter_free(next);
862 g_variant_iter_free(iter);
863 g_variant_unref(message);
868 static void __netconfig_network_notify_result(const char *sig_name, const char *key)
870 GVariantBuilder *builder;
873 INFO("[Signal] %s %s", sig_name, key);
875 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
876 g_variant_builder_add(builder, "{sv}", "key", g_variant_new_string(key));
878 params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
879 g_variant_builder_unref(builder);
881 netconfig_dbus_emit_signal(NULL,
882 NETCONFIG_NETWORK_PATH,
883 NETCONFIG_NETWORK_INTERFACE,
890 const char *netconfig_get_default_profile(void)
892 return netconfig_default_connection_info.profile;
895 const char *netconfig_get_default_ifname(void)
897 return netconfig_default_connection_info.ifname;
900 const char *netconfig_get_default_ipaddress(void)
902 return netconfig_default_connection_info.ipaddress;
905 const char *netconfig_get_default_ipaddress6(void)
907 return netconfig_default_connection_info.ipaddress6;
910 const char *netconfig_get_default_proxy(void)
912 return netconfig_default_connection_info.proxy;
915 unsigned int netconfig_get_default_frequency(void)
917 return netconfig_default_connection_info.freq;
920 const char *netconfig_get_default_mac_address(void)
922 return netconfig_default_connection_info.mac_address;
925 const char *netconfig_wifi_get_connected_essid(const char *default_profile)
927 if (default_profile == NULL)
930 if (netconfig_is_wifi_profile(default_profile) != TRUE)
933 if (g_strcmp0(default_profile, netconfig_default_connection_info.profile) != 0)
936 return netconfig_default_connection_info.essid;
939 gboolean netconfig_get_default_is_metered(void)
941 return netconfig_default_connection_info.is_metered;
944 static int __netconfig_reset_ipv4_socket(void)
949 struct sockaddr_in sai;
950 const char *ipaddr = netconfig_get_default_ipaddress();
951 DBG("ipaddr-[%s]", ipaddr);
956 fd = socket(AF_INET, SOCK_DGRAM, 0);
960 memset(&sai, 0, sizeof(struct sockaddr_in));
961 sai.sin_family = AF_INET;
963 if (!inet_aton(ipaddr, &sai.sin_addr)) {
964 DBG("fail to inet_aton()");
969 memset(&ifr, 0, sizeof(struct ifreq));
970 memcpy(&ifr.ifr_addr, &sai, sizeof(sai));
971 g_strlcpy((char *)ifr.ifr_name, WIFI_IFNAME, IFNAMSIZ);
974 #define SIOCKILLADDR 0x8939
977 ret = ioctl(fd, SIOCKILLADDR, &ifr);
979 DBG("fail to ioctl[SIOCKILLADDR]");
988 void netconfig_update_default_profile(const char *profile)
990 static char *old_profile = NULL;
992 /* It's automatically updated by signal-handler
993 * DO NOT update manually
995 * It is going to update default connection information
998 if (netconfig_default_connection_info.profile != NULL) {
1000 if (netconfig_is_wifi_profile(netconfig_default_connection_info.profile))
1001 __netconfig_reset_ipv4_socket();
1003 g_free(old_profile);
1004 old_profile = strdup(netconfig_default_connection_info.profile);
1006 g_free(netconfig_default_connection_info.profile);
1007 netconfig_default_connection_info.profile = NULL;
1009 g_free(netconfig_default_connection_info.ifname);
1010 netconfig_default_connection_info.ifname = NULL;
1012 g_free(netconfig_default_connection_info.ipaddress);
1013 netconfig_default_connection_info.ipaddress = NULL;
1015 g_free(netconfig_default_connection_info.ipaddress6);
1016 netconfig_default_connection_info.ipaddress6 = NULL;
1018 g_free(netconfig_default_connection_info.proxy);
1019 netconfig_default_connection_info.proxy = NULL;
1021 g_free(netconfig_default_connection_info.mac_address);
1022 netconfig_default_connection_info.mac_address = NULL;
1024 netconfig_default_connection_info.freq = 0;
1025 netconfig_default_connection_info.is_metered = FALSE;
1027 if (wifi_state_get_service_state() != NETCONFIG_WIFI_CONNECTED) {
1028 g_free(netconfig_default_connection_info.essid);
1029 netconfig_default_connection_info.essid = NULL;
1033 /* default profile is NULL and new connected profile is NULL */
1035 char *tmp_profile = __netconfig_get_default_profile();
1037 if (tmp_profile && netconfig_is_cellular_profile(tmp_profile) &&
1038 !netconfig_is_cellular_internet_profile(tmp_profile)) {
1039 DBG("not a default cellular profile");
1040 g_free(tmp_profile);
1045 __netconfig_update_default_connection_info();
1049 netconfig_default_connection_info.profile = g_strdup(tmp_profile);
1050 __netconfig_get_default_connection_info(tmp_profile);
1051 __netconfig_update_default_connection_info();
1052 g_free(tmp_profile);
1056 netconfig_default_connection_info.profile = g_strdup(profile);
1057 __netconfig_get_default_connection_info(profile);
1058 __netconfig_update_default_connection_info();
1061 void netconfig_update_default(void)
1063 if (__netconfig_is_tech_state_connected() == TRUE)
1064 __netconfig_update_if_service_connected();
1066 __netconfig_adjust_tcp_buffer_size();
1069 char *netconfig_get_ifname(const char *profile)
1071 GVariant *message = NULL, *variant;
1072 GVariantIter *iter, *next, *service;
1076 const gchar *value = NULL;
1077 gchar *ifname = NULL;
1078 gboolean found_profile = 0;
1080 if (profile == NULL)
1083 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
1084 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
1085 "GetServices", NULL);
1086 if (message == NULL) {
1087 ERR("Failed to get services informations");
1091 g_variant_get(message, "(a(oa{sv}))", &service);
1092 while (g_variant_iter_loop(service, "(oa{sv})", &obj_path, &iter)) {
1093 if (g_strcmp0(obj_path, profile) == 0) {
1100 if (found_profile == 0) {
1101 ERR("Profile %s doesn't exist", profile);
1102 g_variant_iter_free(service);
1103 g_variant_unref(message);
1107 while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
1108 if (g_strcmp0(key, "Ethernet") == 0) {
1109 while (g_variant_iter_loop(next, "{sv}", &key1, &variant)) {
1110 if (g_strcmp0(key1, "Interface") == 0) {
1111 value = g_variant_get_string(variant, NULL);
1112 ifname = g_strdup(value);
1114 g_variant_unref(variant);
1121 g_variant_unref(message);
1123 g_variant_iter_free(service);
1124 g_variant_iter_free(iter);
1129 /* Check Ethernet Cable Plug-in /Plug-out Status */
1130 void netconfig_network_notify_ethernet_cable_state(const char *key)
1132 __netconfig_network_notify_result("EthernetCableState", key);
1135 static gboolean handle_add_route(
1137 GDBusMethodInvocation *context,
1140 gchar *interface, gchar *gateway, gint address_family)
1142 const gchar *path = ROUTE_EXEC_PATH;
1143 gchar *const args[] = { "/sbin/route", "add", "-net", ip_addr,
1144 "netmask", netmask, "dev", interface, NULL };
1145 gchar *const envs[] = { NULL };
1146 const gchar* buf = NULL;
1151 DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
1153 switch (address_family) {
1155 if (ip_addr == NULL || netmask == NULL || interface == NULL) {
1156 ERR("Invalid parameter");
1157 netconfig_error_invalid_parameter(context);
1161 if (netconfig_execute_file(path, args, envs) < 0) {
1162 DBG("Failed to add a new route");
1163 netconfig_error_permission_denied(context);
1169 if (ip_addr == NULL || interface == NULL || gateway == NULL) {
1170 ERR("Invalid parameter");
1171 netconfig_error_invalid_parameter(context);
1176 ch = strchr(buf, '/');
1179 prefix_len = atoi(ch + 1);
1180 ip_addr[pos-1] = '\0';
1185 if (netconfig_add_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
1186 DBG("Failed to add a new route");
1187 netconfig_error_permission_denied(context);
1192 DBG("Unknown Address Family");
1193 netconfig_error_invalid_parameter(context);
1197 DBG("Successfully added a new route");
1198 network_complete_add_route(object, context, TRUE);
1202 static gboolean handle_remove_route(
1204 GDBusMethodInvocation *context,
1207 gchar *interface, gchar *gateway, gint address_family)
1209 const char *path = ROUTE_EXEC_PATH;
1210 gchar *const args[] = { "/sbin/route", "del", "-net", ip_addr,
1211 "netmask", netmask, "dev", interface, NULL };
1212 char *const envs[] = { NULL };
1213 const char* buf = NULL;
1218 DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
1220 switch (address_family) {
1222 if (ip_addr == NULL || netmask == NULL || interface == NULL) {
1223 DBG("Invalid parameter!");
1224 netconfig_error_invalid_parameter(context);
1227 if (netconfig_execute_file(path, args, envs) < 0) {
1228 DBG("Failed to remove the route");
1229 netconfig_error_permission_denied(context);
1234 if (ip_addr == NULL || interface == NULL || gateway == NULL) {
1235 DBG("Invalid parameter!");
1236 netconfig_error_invalid_parameter(context);
1241 ch = strchr(buf, '/');
1244 prefix_len = atoi(ch + 1);
1245 ip_addr[pos-1] = '\0';
1250 if (netconfig_del_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
1251 DBG("Failed to remove the route");
1252 netconfig_error_permission_denied(context);
1257 DBG("Unknown Address Family");
1258 netconfig_error_invalid_parameter(context);
1262 DBG("Successfully removed the route");
1263 network_complete_remove_route(object, context, TRUE);
1267 static gboolean handle_check_get_privilege(Network *object,
1268 GDBusMethodInvocation *context)
1270 network_complete_check_get_privilege(object, context);
1275 static gboolean handle_check_profile_privilege(Network *object,
1276 GDBusMethodInvocation *context)
1278 network_complete_check_profile_privilege(object, context);
1282 static gboolean handle_check_internet_privilege(Network *object,
1283 GDBusMethodInvocation *context)
1285 network_complete_check_internet_privilege(object, context);
1289 gboolean handle_ethernet_cable_state(Network *object,
1290 GDBusMethodInvocation *context)
1295 ret = netconfig_get_ethernet_cable_state(&state);
1297 DBG("Failed to get ethernet cable state");
1298 netconfig_error_fail_ethernet_cable_state(context);
1302 DBG("Successfully get ethernet cable state[%d]", state);
1303 network_complete_ethernet_cable_state(object, context, state);
1307 gboolean handle_get_metered_info(Network *object,
1308 GDBusMethodInvocation *context)
1312 state = netconfig_get_default_is_metered();
1314 DBG("Default metered state [%s]", state ? "TRUE" : "FALSE");
1315 network_complete_get_metered_info(object, context, state);
1320 void state_object_create_and_init(void)
1322 DBG("Creating network state object");
1323 GDBusInterfaceSkeleton *interface_network = NULL;
1324 GDBusConnection *connection = NULL;
1325 GDBusObjectManagerServer *server = netdbus_get_state_manager();
1329 connection = netdbus_get_connection();
1330 g_dbus_object_manager_server_set_connection(server, connection);
1332 /*Interface netconfig.network*/
1333 netconfigstate = network_skeleton_new();
1335 interface_network = G_DBUS_INTERFACE_SKELETON(netconfigstate);
1336 g_signal_connect(netconfigstate, "handle-add-route",
1337 G_CALLBACK(handle_add_route), NULL);
1338 g_signal_connect(netconfigstate, "handle-check-get-privilege",
1339 G_CALLBACK(handle_check_get_privilege), NULL);
1340 g_signal_connect(netconfigstate, "handle-check-profile-privilege",
1341 G_CALLBACK(handle_check_profile_privilege), NULL);
1342 g_signal_connect(netconfigstate, "handle-check-internet-privilege",
1343 G_CALLBACK(handle_check_internet_privilege), NULL);
1344 g_signal_connect(netconfigstate, "handle-ethernet-cable-state",
1345 G_CALLBACK(handle_ethernet_cable_state), NULL);
1346 g_signal_connect(netconfigstate, "handle-remove-route",
1347 G_CALLBACK(handle_remove_route), NULL);
1348 g_signal_connect(netconfigstate, "handle-launch-mdns",
1349 G_CALLBACK(handle_launch_mdns), NULL);
1350 g_signal_connect(netconfigstate, "handle-device-policy-set-wifi",
1351 G_CALLBACK(handle_device_policy_set_wifi), NULL);
1352 g_signal_connect(netconfigstate, "handle-device-policy-get-wifi",
1353 G_CALLBACK(handle_device_policy_get_wifi), NULL);
1354 g_signal_connect(netconfigstate, "handle-device-policy-set-wifi-profile",
1355 G_CALLBACK(handle_device_policy_set_wifi_profile), NULL);
1356 g_signal_connect(netconfigstate, "handle-device-policy-get-wifi-profile",
1357 G_CALLBACK(handle_device_policy_get_wifi_profile), NULL);
1358 g_signal_connect(netconfigstate, "handle-get-metered-info",
1359 G_CALLBACK(handle_get_metered_info), NULL);
1361 if (!g_dbus_interface_skeleton_export(interface_network, connection,
1362 NETCONFIG_NETWORK_STATE_PATH, NULL)) {
1363 ERR("Export with path failed");
1367 void state_object_deinit(void)
1369 g_object_unref(netconfigstate);