4c26944b36a7c11d877e8bcb39c245236fe9be82
[platform/core/connectivity/net-config.git] / src / network-state.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #include <vconf.h>
21 #include <vconf-keys.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <net/if.h>
26 #include <arpa/inet.h>
27 #include <netinet/in.h>
28 #include <sys/ioctl.h>
29
30 #include "log.h"
31 #include "util.h"
32 #include "netdbus.h"
33 #include "neterror.h"
34 #include "emulator.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"
41
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"
65
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"
69
70 #define NET_TCP_BUFFERSIZE_WIFID_WMEM_MAX       "2097152"
71
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"
76
77 #define ROUTE_EXEC_PATH                                         "/sbin/route"
78
79 static Network *netconfigstate = NULL;
80
81 struct netconfig_default_connection {
82         char *profile;
83         char *ifname;
84         char *ipaddress;
85         char *ipaddress6;
86         char *proxy;
87         char *essid;
88         unsigned int freq;
89 };
90
91 static struct netconfig_default_connection
92                                 netconfig_default_connection_info = { NULL, };
93
94 gboolean netconfig_iface_network_state_ethernet_cable_state(gint32 *state);
95
96 static gboolean __netconfig_is_connected(GVariantIter *array)
97 {
98         gboolean is_connected = FALSE;
99         GVariant *variant = NULL;
100         gchar *key = NULL;
101         const gchar *value = NULL;
102
103         while (g_variant_iter_loop(array, "{sv}", &key, &variant)) {
104                 if (g_strcmp0(key, "State") != 0)
105                         continue;
106
107                 if (g_variant_is_of_type(variant, G_VARIANT_TYPE_STRING)) {
108                         value = g_variant_get_string(variant, NULL);
109                         if (g_strcmp0(value, "ready") == 0 || g_strcmp0(value, "online") == 0)
110                                 is_connected = TRUE;
111                 }
112
113                 g_free(key);
114                 g_variant_unref(variant);
115                 break;
116         }
117
118         return is_connected;
119 }
120
121 static char *__netconfig_get_default_profile(void)
122 {
123         GVariant *message = NULL;
124         GVariantIter *iter;
125         GVariantIter *next;
126         gchar *default_profile = NULL;
127         gchar *object_path;
128
129         message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
130                         CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
131                         "GetServices", NULL);
132         if (message == NULL) {
133                 ERR("Failed to get profiles");
134                 return NULL;
135         }
136
137         g_variant_get(message, "(a(oa{sv}))", &iter);
138         while (g_variant_iter_loop(iter, "(oa{sv})", &object_path, &next)) {
139                 if (object_path == NULL)
140                         continue;
141
142                 if (netconfig_is_cellular_profile(object_path) && !netconfig_is_cellular_internet_profile(object_path))
143                         continue;
144
145                 if (__netconfig_is_connected(next) == TRUE) {
146                         default_profile = g_strdup(object_path);
147                         g_free(object_path);
148                         g_variant_iter_free(next);
149                         break;
150                 }
151         }
152         g_variant_iter_free(iter);
153         g_variant_unref(message);
154
155         return default_profile;
156 }
157
158 static void __netconfig_get_default_connection_info(const char *profile)
159 {
160         GVariant *message = NULL, *variant = NULL, *variant2 = NULL;
161         GVariantIter *iter = NULL, *iter1 = NULL,  *service = NULL;
162         GVariant *next = NULL;
163         gchar *obj_path;
164         gchar *key = NULL;
165         gchar *key1 = NULL;
166         gchar *key2 = NULL;
167         gboolean found_profile = 0;
168
169         message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
170                         CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
171                         "GetServices", NULL);
172         if (message == NULL) {
173                 ERR("Failed to get services informations");
174                 goto done;
175         }
176
177         g_variant_get(message, "(a(oa{sv}))", &service);
178         if (service == NULL) {
179                 ERR("Failed to get services iter");
180                 goto done;
181         }
182
183         while (g_variant_iter_loop(service, "(oa{sv})", &obj_path, &iter)) {
184                 if (g_strcmp0(obj_path, profile) == 0) {
185                         g_free(obj_path);
186                         found_profile = 1;
187                         break;
188                 }
189         }
190
191         if (iter == NULL || found_profile == 0) {
192                 ERR("Profile %s doesn't exist", profile);
193                 goto done;
194         }
195
196         while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
197                 const gchar *value = NULL;
198                 guint16 freq = 0;
199                 if (g_strcmp0(key, "Name") == 0 &&
200                                 netconfig_is_wifi_profile(profile) == TRUE) {
201                         if (g_variant_is_of_type(next, G_VARIANT_TYPE_STRING)) {
202                                 value = g_variant_get_string(next, NULL);
203
204                                 netconfig_default_connection_info.essid = g_strdup(value);
205                         }
206                 } else if (g_strcmp0(key, "Ethernet") == 0) {
207                         g_variant_get(next, "a{sv}", &iter1);
208                         if (iter1 == NULL)
209                                 continue;
210                         while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
211                                 if (g_strcmp0(key1, "Interface") == 0) {
212                                         value = g_variant_get_string(variant, NULL);
213                                         netconfig_default_connection_info.ifname = g_strdup(value);
214                                 }
215                         }
216                         g_variant_iter_free(iter1);
217                 } else if (g_strcmp0(key, "IPv4") == 0) {
218                         g_variant_get(next, "a{sv}", &iter1);
219                         if (iter1 == NULL)
220                                 continue;
221                         while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
222                                 if (g_strcmp0(key1, "Address") == 0) {
223                                         value = g_variant_get_string(variant, NULL);
224                                         netconfig_default_connection_info.ipaddress = g_strdup(value);
225                                 }
226                         }
227                         g_variant_iter_free(iter1);
228                 } else if (g_strcmp0(key, "IPv6") == 0) {
229                         g_variant_get(next, "a{sv}", &iter1);
230                         if (iter1 == NULL)
231                                 continue;
232                         while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
233                                 if (g_strcmp0(key1, "Address") == 0) {
234                                         value = g_variant_get_string(variant, NULL);
235                                         netconfig_default_connection_info.ipaddress6 = g_strdup(value);
236                                 }
237                         }
238                         g_variant_iter_free(iter1);
239
240                 } else if (g_strcmp0(key, "Proxy") == 0) {
241                         g_variant_get(next, "a{sv}", &iter1);
242                         if (iter1 == NULL)
243                                 continue;
244                         while (g_variant_iter_loop(iter1, "{sv}", &key2, &variant2)) {
245                                 GVariantIter *iter_sub = NULL;
246
247                                 if (g_strcmp0(key2, "Servers") == 0) {
248                                         if (!g_variant_is_of_type(variant2, G_VARIANT_TYPE_STRING_ARRAY)) {
249                                                 g_free(key2);
250                                                 g_variant_unref(variant2);
251                                                 break;
252                                         }
253
254                                         g_variant_get(variant2, "as", &iter_sub);
255                                         g_variant_iter_loop(iter_sub, "s", &value);
256                                         g_variant_iter_free(iter_sub);
257                                         if (value != NULL && (strlen(value) > 0))
258                                                 netconfig_default_connection_info.proxy = g_strdup(value);
259                                 } else if (g_strcmp0(key2, "Method") == 0) {
260                                         if (g_variant_is_of_type(variant2, G_VARIANT_TYPE_STRING)) {
261                                                 g_free(key2);
262                                                 g_variant_unref(variant2);
263                                                 break;
264                                         }
265
266                                         value = g_variant_get_string(variant2, NULL);
267                                         if (g_strcmp0(value, "direct") == 0) {
268                                                 g_free(netconfig_default_connection_info.proxy);
269                                                 netconfig_default_connection_info.proxy = NULL;
270
271                                                 g_free(key2);
272                                                 g_variant_unref(variant2);
273                                                 break;
274                                         }
275                                 }
276                         }
277                         g_variant_iter_free(iter1);
278                 } else if (g_strcmp0(key, "Frequency") == 0) {
279                         if (g_variant_is_of_type(next, G_VARIANT_TYPE_UINT16)) {
280                                 freq = g_variant_get_uint16(next);
281                                 netconfig_default_connection_info.freq = freq;
282                         }
283                 }
284         }
285
286 done:
287         if (message)
288                 g_variant_unref(message);
289
290         if (iter)
291                 g_variant_iter_free(iter);
292
293         if (service)
294                 g_variant_iter_free(service);
295
296         return;
297 }
298
299 static void __netconfig_adjust_tcp_buffer_size(void)
300 {
301         int fdr = 0, fdw = 0;
302         int fdrmax = 0, fdwmax = 0;
303         const char *rbuf_size = NULL;
304         const char *wbuf_size = NULL;
305         const char *rmax_size = NULL;
306         const char *wmax_size = NULL;
307         const char *profile = netconfig_get_default_profile();
308
309         if (profile == NULL) {
310                 DBG("There is no default connection");
311
312                 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
313                 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
314         } else if (netconfig_is_wifi_profile(profile) == TRUE) {
315                 DBG("Default connection: Wi-Fi");
316
317                 rbuf_size = NET_TCP_BUFFERSIZE_WIFI_READ;
318                 wbuf_size = NET_TCP_BUFFERSIZE_WIFI_WRITE;
319                 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
320                 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
321         } else if (netconfig_is_cellular_profile(profile) == TRUE) {
322                 int telephony_svctype = 0, telephony_pstype = 0;
323
324                 netconfig_get_telephony_network_type(&telephony_svctype, &telephony_pstype);
325                 DBG("Default cellular %d, %d", telephony_svctype, telephony_pstype);
326
327                 switch (telephony_pstype) {
328                 case VCONFKEY_TELEPHONY_PSTYPE_HSPA:
329                         rbuf_size = NET_TCP_BUFFERSIZE_HSPA_READ;
330                         wbuf_size = NET_TCP_BUFFERSIZE_HSPA_WRITE;
331                         break;
332                 case VCONFKEY_TELEPHONY_PSTYPE_HSUPA:
333                         rbuf_size = NET_TCP_BUFFERSIZE_HSUPA_READ;
334                         wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
335                         break;
336                 case VCONFKEY_TELEPHONY_PSTYPE_HSDPA:
337                         rbuf_size = NET_TCP_BUFFERSIZE_HSDPA_READ;
338                         wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
339                         break;
340 #if !defined TIZEN_WEARABLE
341                 case VCONFKEY_TELEPHONY_PSTYPE_HSPAP:
342                         rbuf_size = NET_TCP_BUFFERSIZE_HSPAP_READ;
343                         wbuf_size = NET_TCP_BUFFERSIZE_HSPAP_WRITE;
344                         break;
345 #endif
346                 default:
347                         switch (telephony_svctype) {
348                         case VCONFKEY_TELEPHONY_SVCTYPE_LTE:
349                                 rbuf_size = NET_TCP_BUFFERSIZE_LTE_READ;
350                                 wbuf_size = NET_TCP_BUFFERSIZE_LTE_WRITE;
351                                 rmax_size = NET_TCP_BUFFERSIZE_LTE_RMEM_MAX;
352                                 break;
353                         case VCONFKEY_TELEPHONY_SVCTYPE_3G:
354                                 rbuf_size = NET_TCP_BUFFERSIZE_UMTS_READ;
355                                 wbuf_size = NET_TCP_BUFFERSIZE_UMTS_WRITE;
356                                 break;
357                         case VCONFKEY_TELEPHONY_SVCTYPE_2_5G_EDGE:
358                                 rbuf_size = NET_TCP_BUFFERSIZE_EDGE_READ;
359                                 wbuf_size = NET_TCP_BUFFERSIZE_EDGE_WRITE;
360                                 break;
361                         case VCONFKEY_TELEPHONY_SVCTYPE_2_5G:
362                                 rbuf_size = NET_TCP_BUFFERSIZE_GPRS_READ;
363                                 wbuf_size = NET_TCP_BUFFERSIZE_GPRS_WRITE;
364                                 break;
365                         default:
366                                 /* TODO: Check LTE support */
367                                 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
368                                 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
369                                 break;
370                         }
371                         break;
372                 }
373         } else {
374                 DBG("Default TCP buffer configured");
375
376                 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
377                 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
378         }
379
380         if (rbuf_size != NULL) {
381                 fdr = open(NET_PROC_SYS_NET_IPV4_TCP_RMEM, O_RDWR | O_CLOEXEC);
382
383                 if (fdr < 0 || write(fdr, rbuf_size, strlen(rbuf_size)) < 0)
384                         ERR("Failed to set TCP read buffer size");
385
386                 if (fdr >= 0)
387                         close(fdr);
388         }
389
390         if (wbuf_size != NULL) {
391                 fdw = open(NET_PROC_SYS_NET_IPv4_TCP_WMEM, O_RDWR | O_CLOEXEC);
392
393                 if (fdw < 0 || write(fdw, wbuf_size, strlen(wbuf_size)) < 0)
394                         ERR("Failed to set TCP write buffer size");
395
396                 if (fdw >= 0)
397                         close(fdw);
398         }
399
400         /* As default */
401         if (rmax_size == NULL)
402                 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
403         if (wmax_size == NULL)
404                 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
405
406         if (rmax_size != NULL) {
407                 fdrmax = open(NET_PROC_SYS_NET_CORE_RMEM_MAX, O_RDWR | O_CLOEXEC);
408
409                 if (fdrmax < 0 || write(fdrmax, rmax_size, strlen(rmax_size)) < 0)
410                         ERR("Failed to set TCP rmem_max size");
411
412                 if (fdrmax >= 0)
413                         close(fdrmax);
414         }
415
416         if (wmax_size != NULL) {
417                 fdwmax = open(NET_PROC_SYS_NET_CORE_WMEM_MAX, O_RDWR | O_CLOEXEC);
418
419                 if (fdwmax < 0 || write(fdwmax, wmax_size, strlen(wmax_size)) < 0)
420                         ERR("Failed to set TCP wmem_max size");
421
422                 if (fdwmax >= 0)
423                         close(fdwmax);
424         }
425 }
426
427 static void __netconfig_update_default_connection_info(void)
428 {
429         int old_network_status = 0;
430         const char *profile = netconfig_get_default_profile();
431         const char *ip_addr = netconfig_get_default_ipaddress();
432         const char *ip_addr6 = netconfig_get_default_ipaddress6();
433         const char *proxy_addr = netconfig_get_default_proxy();
434         unsigned int freq = netconfig_get_default_frequency();
435
436         if (emulator_is_emulated() == TRUE) {
437                 if (ip_addr != NULL)
438                         netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr);
439                 else
440                         netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
441
442                 if (ip_addr6 != NULL)
443                         netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, ip_addr6);
444                 else
445                         netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, "");
446
447                 return;
448         }
449
450         if (profile == NULL)
451                 DBG("Reset network state configuration");
452         else
453                 DBG("%s: ip(%s) ip6(%s) proxy(%s)", profile, ip_addr, ip_addr6, proxy_addr);
454
455         netconfig_vconf_get_int(VCONFKEY_NETWORK_STATUS, &old_network_status);
456
457         if (profile == NULL && old_network_status != VCONFKEY_NETWORK_OFF) {
458                 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
459
460                 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
461                 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, "");
462                 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
463
464                 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 0);
465                 netconfig_set_vconf_int("memory/private/wifi/frequency", 0);
466
467                 DBG("Successfully clear IP and PROXY up");
468
469         } else if (profile != NULL) {
470                 char *old_ip = vconf_get_str(VCONFKEY_NETWORK_IP);
471                 char *old_ip6 = vconf_get_str(VCONFKEY_NETWORK_IP6);
472                 char *old_proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY);
473
474                 if (netconfig_is_wifi_profile(profile) == TRUE) {
475                         netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_WIFI);
476                         netconfig_set_vconf_int("memory/private/wifi/frequency", freq);
477
478                         netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
479                                 EKEY_NETWORK_STATUS, EVAL_NETWORK_WIFI);
480                 } else if (netconfig_is_cellular_profile(profile)) {
481
482                         if (!netconfig_is_cellular_internet_profile(profile)) {
483                                 DBG("connection is not a internet profile - stop to update the cellular state");
484                                 if (old_ip)
485                                         free(old_ip);
486                                 if (old_ip6)
487                                         free(old_ip6);
488                                 if (old_proxy)
489                                         free(old_proxy);
490                                 return;
491                         }
492
493                         netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_CELLULAR);
494
495                         netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
496                                 EKEY_NETWORK_STATUS, EVAL_NETWORK_CELLULAR);
497                 } else if (netconfig_is_ethernet_profile(profile) == TRUE) {
498                         netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_ETHERNET);
499                         netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
500                                 EKEY_NETWORK_STATUS, EVAL_NETWORK_ETHERNET);
501                 } else if (netconfig_is_bluetooth_profile(profile) == TRUE) {
502                         netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_BLUETOOTH);
503                         netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
504                                 EKEY_NETWORK_STATUS, EVAL_NETWORK_BT);
505                 } else{
506                         netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
507                         netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
508                                 EKEY_NETWORK_STATUS, EVAL_NETWORK_DISCONNECTED);
509                 }
510
511                 if (g_strcmp0(old_ip, ip_addr) != 0 || old_ip == NULL) {
512                         if (ip_addr != NULL)
513                                 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr);
514                         else if (old_ip != NULL && strlen(old_ip) > 0)
515                                 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
516                 }
517                 if (old_ip)
518                         free(old_ip);
519
520                 if (g_strcmp0(old_ip6, ip_addr6) != 0 || old_ip6 == NULL) {
521                         if (ip_addr6 != NULL)
522                                 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, ip_addr6);
523                         else if (old_ip6 != NULL && strlen(old_ip6) > 0)
524                                 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, "");
525                 }
526                 if (old_ip6)
527                         free(old_ip6);
528
529                 if (g_strcmp0(old_proxy, proxy_addr) != 0) {
530                         if (proxy_addr == NULL)
531                                 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
532                         else
533                                 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, proxy_addr);
534                 }
535                 if (old_proxy)
536                         free(old_proxy);
537
538                 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 1);
539
540                 DBG("Successfully update default network configuration");
541         }
542
543         __netconfig_adjust_tcp_buffer_size();
544 }
545
546 static gboolean __netconfig_is_tech_state_connected(void)
547 {
548         gboolean ret = FALSE;
549         GVariant *message = NULL, *variant;
550         GVariantIter *iter, *next;
551         gchar *path;
552         gchar *key;
553
554         message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
555                         CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
556                         "GetTechnologies", NULL);
557
558         if (message == NULL) {
559                 DBG("Fail to get technology state");
560                 return FALSE;
561         }
562
563         g_variant_get(message, "(a(oa{sv}))", &iter);
564         while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
565                 if (path == NULL)
566                         continue;
567
568                 while (g_variant_iter_loop(next, "{sv}", &key, &variant)) {
569                         gboolean data;
570                         if (g_strcmp0(key, "Connected") == 0) {
571                                 data = g_variant_get_boolean(variant);
572                                 DBG("%s [%s: %s]", path, key, data ? "True" : "False");
573                                 if (TRUE == data) {
574                                         ret = TRUE;
575                                         g_free(path);
576                                         g_free(key);
577                                         g_variant_unref(variant);
578                                         g_variant_iter_free(next);
579                                         goto done;
580                                 }
581                         }
582                 }
583         }
584
585 done:
586         g_variant_iter_free(iter);
587         g_variant_unref(message);
588
589         return ret;
590 }
591
592 static void __netconfig_update_if_service_connected(void)
593 {
594         GVariant *message = NULL, *var;
595         GVariantIter *iter, *next;
596         gchar *path;
597         gchar *key;
598
599         message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
600                         CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
601                         "GetServices", NULL);
602
603         if (message == NULL) {
604                 ERR("Failed to get services");
605                 return;
606         }
607
608         g_variant_get(message, "(a(oa{sv}))", &iter);
609         while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
610                 if (path == NULL)
611                         continue;
612
613                 if (g_str_has_prefix(path,
614                                                 CONNMAN_WIFI_SERVICE_PROFILE_PREFIX) == TRUE) {
615                         if (g_strrstr(path + strlen(CONNMAN_WIFI_SERVICE_PROFILE_PREFIX),
616                                                         "hidden") != NULL) {
617                                 /* skip hidden profiles */
618                                 continue;
619                         }
620                         /* Process this */
621                 } else if (g_str_has_prefix(path,
622                                                 CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX) == TRUE) {
623                         /* Process this */
624                 } else {
625                         continue;
626                 }
627
628                 while (g_variant_iter_loop(next, "{sv}", &key, &var)) {
629                         if (g_strcmp0(key, "State") == 0) {
630                                 const gchar *sdata = NULL;
631                                 sdata = g_variant_get_string(var, NULL);
632                                 DBG("%s [%s: %s]", path, key, sdata);
633
634                                 if (g_strcmp0(sdata, "online") == 0 || g_strcmp0(sdata, "ready") == 0) {
635
636                                         /* Found a connected WiFi / 3G service.
637                                          * Lets update the default profile info.
638                                          */
639                                         netconfig_update_default_profile((const gchar*)path);
640                                         g_free(key);
641                                         g_free(path);
642                                         g_variant_unref(var);
643                                         g_variant_iter_free(next);
644                                         goto done;
645                                 }
646                         }
647                 }
648         }
649 done:
650         g_variant_iter_free(iter);
651         g_variant_unref(message);
652
653         return;
654 }
655
656 static void __netconfig_network_notify_result(const char *sig_name, const char *key)
657 {
658         GVariantBuilder *builder;
659         GVariant *params;
660
661         INFO("[Signal] %s %s", sig_name, key);
662
663         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
664         g_variant_builder_add(builder, "{sv}", "key", g_variant_new_string(key));
665
666         params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
667         g_variant_builder_unref(builder);
668
669         netconfig_dbus_emit_signal(NULL,
670                                 NETCONFIG_NETWORK_PATH,
671                                 NETCONFIG_NETWORK_INTERFACE,
672                                 sig_name,
673                                 params);
674
675         return;
676 }
677
678 const char *netconfig_get_default_profile(void)
679 {
680         return netconfig_default_connection_info.profile;
681 }
682
683 const char *netconfig_get_default_ifname(void)
684 {
685         return netconfig_default_connection_info.ifname;
686 }
687
688 const char *netconfig_get_default_ipaddress(void)
689 {
690         return netconfig_default_connection_info.ipaddress;
691 }
692
693 const char *netconfig_get_default_ipaddress6(void)
694 {
695         return netconfig_default_connection_info.ipaddress6;
696 }
697
698 const char *netconfig_get_default_proxy(void)
699 {
700         return netconfig_default_connection_info.proxy;
701 }
702
703 unsigned int netconfig_get_default_frequency(void)
704 {
705         return netconfig_default_connection_info.freq;
706 }
707
708 const char *netconfig_wifi_get_connected_essid(const char *default_profile)
709 {
710         if (default_profile == NULL)
711                 return NULL;
712
713         if (netconfig_is_wifi_profile(default_profile) != TRUE)
714                 return NULL;
715
716         if (g_strcmp0(default_profile, netconfig_default_connection_info.profile) != 0)
717                 return NULL;
718
719         return netconfig_default_connection_info.essid;
720 }
721
722 static int __netconfig_reset_ipv4_socket(void)
723 {
724         int ret;
725         int fd;
726         struct ifreq ifr;
727         struct sockaddr_in sai;
728         const char *ipaddr = netconfig_get_default_ipaddress();
729         DBG("ipaddr-[%s]", ipaddr);
730
731         if (!ipaddr)
732                 return -1;
733
734         fd = socket(AF_INET, SOCK_DGRAM, 0);
735         if (fd < 0)
736                 return -1;
737
738         memset(&sai, 0, sizeof(struct sockaddr_in));
739         sai.sin_family = AF_INET;
740         sai.sin_port = 0;
741         if (!inet_aton(ipaddr, &sai.sin_addr)) {
742                 DBG("fail to inet_aton()");
743                 close(fd);
744                 return -1;
745         }
746
747         memset(&ifr, 0, sizeof(struct ifreq));
748         memcpy(&ifr.ifr_addr, &sai, sizeof(sai));
749         g_strlcpy((char *)ifr.ifr_name, WIFI_IFNAME, IFNAMSIZ);
750
751 #ifndef SIOCKILLADDR
752 #define SIOCKILLADDR    0x8939
753 #endif
754
755         ret = ioctl(fd, SIOCKILLADDR, &ifr);
756         if (ret < 0) {
757                 DBG("fail to ioctl[SIOCKILLADDR]");
758                 close(fd);
759                 return -1;
760         }
761
762         close(fd);
763         return 0;
764 }
765
766 void netconfig_update_default_profile(const char *profile)
767 {
768         static char *old_profile = NULL;
769
770         /* It's automatically updated by signal-handler
771          * DO NOT update manually
772          *
773          * It is going to update default connection information
774          */
775
776         if (netconfig_default_connection_info.profile != NULL) {
777
778                 if (netconfig_is_wifi_profile(netconfig_default_connection_info.profile))
779                         __netconfig_reset_ipv4_socket();
780
781                 g_free(old_profile);
782                 old_profile = strdup(netconfig_default_connection_info.profile);
783
784                 g_free(netconfig_default_connection_info.profile);
785                 netconfig_default_connection_info.profile = NULL;
786
787                 g_free(netconfig_default_connection_info.ifname);
788                 netconfig_default_connection_info.ifname = NULL;
789
790                 g_free(netconfig_default_connection_info.ipaddress);
791                 netconfig_default_connection_info.ipaddress = NULL;
792
793                 g_free(netconfig_default_connection_info.ipaddress6);
794                 netconfig_default_connection_info.ipaddress6 = NULL;
795
796                 g_free(netconfig_default_connection_info.proxy);
797                 netconfig_default_connection_info.proxy = NULL;
798
799                 netconfig_default_connection_info.freq = 0;
800
801                 if (wifi_state_get_service_state() != NETCONFIG_WIFI_CONNECTED) {
802                         g_free(netconfig_default_connection_info.essid);
803                         netconfig_default_connection_info.essid = NULL;
804                 }
805         }
806
807         /* default profile is NULL and new connected profile is NULL */
808         if (!profile) {
809                 char *tmp_profile = __netconfig_get_default_profile();
810
811                 if (tmp_profile && netconfig_is_cellular_profile(tmp_profile) &&
812                         !netconfig_is_cellular_internet_profile(tmp_profile)) {
813                         DBG("not a default cellular profile");
814                         g_free(tmp_profile);
815                         tmp_profile = NULL;
816                 }
817
818                 if (!tmp_profile) {
819                         __netconfig_update_default_connection_info();
820                         return;
821                 }
822
823                 netconfig_default_connection_info.profile = g_strdup(tmp_profile);
824                 __netconfig_get_default_connection_info(tmp_profile);
825                 __netconfig_update_default_connection_info();
826                 g_free(tmp_profile);
827                 return;
828         }
829
830         netconfig_default_connection_info.profile = g_strdup(profile);
831         __netconfig_get_default_connection_info(profile);
832         __netconfig_update_default_connection_info();
833 }
834
835 void netconfig_update_default(void)
836 {
837         if (__netconfig_is_tech_state_connected() == TRUE)
838                 __netconfig_update_if_service_connected();
839         else
840                 __netconfig_adjust_tcp_buffer_size();
841 }
842
843 char *netconfig_get_ifname(const char *profile)
844 {
845         GVariant *message = NULL, *variant;
846         GVariantIter *iter, *next, *service;
847         gchar *obj_path;
848         gchar *key;
849         gchar *key1;
850         const gchar *value = NULL;
851         gchar *ifname = NULL;
852         gboolean found_profile = 0;
853
854         if (profile == NULL)
855                 return NULL;
856
857         message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
858                         CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
859                         "GetServices", NULL);
860         if (message == NULL) {
861                 ERR("Failed to get services informations");
862                 return NULL;
863         }
864
865         g_variant_get(message, "(a(oa{sv}))", &service);
866         while (g_variant_iter_loop(service, "(oa{sv})", &obj_path, &iter)) {
867                 if (g_strcmp0(obj_path, profile) == 0) {
868                         g_free(obj_path);
869                         found_profile = 1;
870                         break;
871                 }
872         }
873
874         if (found_profile == 0) {
875                 ERR("Profile %s doesn't exist", profile);
876                 g_variant_iter_free(service);
877                 g_variant_unref(message);
878                 return NULL;
879         }
880
881         while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
882                 if (g_strcmp0(key, "Ethernet") == 0) {
883                         while (g_variant_iter_loop(next, "{sv}", &key1, &variant)) {
884                                 if (g_strcmp0(key1, "Interface") == 0) {
885                                         value = g_variant_get_string(variant, NULL);
886                                         ifname = g_strdup(value);
887                                         g_free(key1);
888                                         g_variant_unref(variant);
889                                         break;
890                                 }
891                         }
892                 }
893         }
894
895         g_variant_unref(message);
896
897         g_variant_iter_free(service);
898         g_variant_iter_free(iter);
899
900         return ifname;
901 }
902
903 /* Check Ethernet Cable Plug-in /Plug-out Status */
904 void netconfig_network_notify_ethernet_cable_state(const char *key)
905 {
906         __netconfig_network_notify_result("EthernetCableState", key);
907 }
908
909 static gboolean handle_add_route(
910                 Network *object,
911                 GDBusMethodInvocation *context,
912                 gchar *ip_addr,
913                 gchar *netmask,
914                 gchar *interface,  gchar *gateway, gint address_family)
915 {
916         const gchar *path = ROUTE_EXEC_PATH;
917         gchar *const args[] = { "/sbin/route", "add", "-net", ip_addr,
918                 "netmask", netmask, "dev", interface, NULL };
919         gchar *const envs[] = { NULL };
920         const gchar* buf = NULL;
921         gchar* ch = NULL;
922         int prefix_len = 0;
923         int pos = 0;
924
925         DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
926
927         switch (address_family) {
928         case AF_INET:
929                 if (ip_addr == NULL || netmask == NULL || interface == NULL) {
930                         ERR("Invalid parameter");
931                         netconfig_error_invalid_parameter(context);
932                         return FALSE;
933                 }
934
935                 if (netconfig_execute_file(path, args, envs) < 0) {
936                         DBG("Failed to add a new route");
937                         netconfig_error_permission_denied(context);
938                         return FALSE;
939                 }
940
941                 break;
942         case AF_INET6:
943                 if (ip_addr == NULL || interface == NULL || gateway == NULL) {
944                         ERR("Invalid parameter");
945                         netconfig_error_invalid_parameter(context);
946                         return FALSE;
947                 }
948
949                 buf = ip_addr;
950                 ch = strchr(buf, '/');
951                 pos = ch - buf + 1;
952                 if (ch) {
953                         prefix_len = atoi(ch + 1);
954                         ip_addr[pos-1] = '\0';
955                 } else {
956                         prefix_len = 128;
957                 }
958
959                 if (netconfig_add_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
960                         DBG("Failed to add a new route");
961                         netconfig_error_permission_denied(context);
962                         return FALSE;
963                 }
964                 break;
965         default:
966                 DBG("Unknown Address Family");
967                 netconfig_error_invalid_parameter(context);
968                 return FALSE;
969         }
970
971         DBG("Successfully added a new route");
972         network_complete_add_route(object, context, TRUE);
973         return TRUE;
974 }
975
976 static gboolean handle_remove_route(
977                 Network *object,
978                 GDBusMethodInvocation *context,
979                 gchar *ip_addr,
980                 gchar *netmask,
981                 gchar *interface, gchar *gateway, gint address_family)
982 {
983         const char *path = ROUTE_EXEC_PATH;
984         gchar *const args[] = { "/sbin/route", "del", "-net", ip_addr,
985                 "netmask", netmask, "dev", interface, NULL };
986         char *const envs[] = { NULL };
987         const char* buf = NULL;
988         char* ch = NULL;
989         int prefix_len = 0;
990         int pos = 0;
991
992         DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
993
994         switch (address_family) {
995         case AF_INET:
996                 if (ip_addr == NULL || netmask == NULL || interface == NULL) {
997                         DBG("Invalid parameter!");
998                         netconfig_error_invalid_parameter(context);
999                         return FALSE;
1000                 }
1001                 if (netconfig_execute_file(path, args, envs) < 0) {
1002                         DBG("Failed to remove the route");
1003                         netconfig_error_permission_denied(context);
1004                         return FALSE;
1005                 }
1006                 break;
1007         case AF_INET6:
1008                 if (ip_addr == NULL || interface == NULL || gateway == NULL) {
1009                         DBG("Invalid parameter!");
1010                         netconfig_error_invalid_parameter(context);
1011                         return FALSE;
1012                 }
1013
1014                 buf = ip_addr;
1015                 ch = strchr(buf, '/');
1016                 pos = ch - buf + 1;
1017                 if (ch) {
1018                         prefix_len = atoi(ch + 1);
1019                         ip_addr[pos-1] = '\0';
1020                 } else {
1021                         prefix_len = 128;
1022                 }
1023
1024                 if (netconfig_del_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
1025                         DBG("Failed to remove the route");
1026                         netconfig_error_permission_denied(context);
1027                         return FALSE;
1028                 }
1029                 break;
1030         default:
1031                 DBG("Unknown Address Family");
1032                 netconfig_error_invalid_parameter(context);
1033                 return FALSE;
1034         }
1035
1036         DBG("Successfully removed the route");
1037         network_complete_remove_route(object, context, TRUE);
1038         return TRUE;
1039 }
1040
1041 static gboolean handle_check_get_privilege(Network *object,
1042                 GDBusMethodInvocation *context)
1043 {
1044         network_complete_check_get_privilege(object, context);
1045         return TRUE;
1046 }
1047
1048
1049 static gboolean handle_check_profile_privilege(Network *object,
1050                 GDBusMethodInvocation *context)
1051 {
1052         network_complete_check_profile_privilege(object, context);
1053         return TRUE;
1054 }
1055
1056 static gboolean handle_check_internet_privilege(Network *object,
1057                 GDBusMethodInvocation *context)
1058 {
1059         network_complete_check_internet_privilege(object, context);
1060         return TRUE;
1061 }
1062
1063 gboolean handle_ethernet_cable_state(Network *object,
1064         GDBusMethodInvocation *context)
1065 {
1066         int ret = 0;
1067         int state = 0;
1068
1069         ret = netconfig_get_ethernet_cable_state(&state);
1070         if (ret != 0) {
1071                 DBG("Failed to get ethernet cable state");
1072                 netconfig_error_fail_ethernet_cable_state(context);
1073                 return FALSE;
1074         }
1075
1076         DBG("Successfully get ethernet cable state[%d]", state);
1077         network_complete_ethernet_cable_state(object, context, state);
1078         return TRUE;
1079 }
1080
1081 void state_object_create_and_init(void)
1082 {
1083         DBG("Creating network state object");
1084         GDBusInterfaceSkeleton *interface_network = NULL;
1085         GDBusConnection *connection = NULL;
1086         GDBusObjectManagerServer *server = netdbus_get_state_manager();
1087         if (server == NULL)
1088                 return;
1089
1090         connection = netdbus_get_connection();
1091         g_dbus_object_manager_server_set_connection(server, connection);
1092
1093         /*Interface netconfig.network*/
1094         netconfigstate = network_skeleton_new();
1095
1096         interface_network = G_DBUS_INTERFACE_SKELETON(netconfigstate);
1097         g_signal_connect(netconfigstate, "handle-add-route",
1098                                 G_CALLBACK(handle_add_route), NULL);
1099         g_signal_connect(netconfigstate, "handle-check-get-privilege",
1100                                 G_CALLBACK(handle_check_get_privilege), NULL);
1101         g_signal_connect(netconfigstate, "handle-check-profile-privilege",
1102                                 G_CALLBACK(handle_check_profile_privilege), NULL);
1103         g_signal_connect(netconfigstate, "handle-check-internet-privilege",
1104                                 G_CALLBACK(handle_check_internet_privilege), NULL);
1105         g_signal_connect(netconfigstate, "handle-ethernet-cable-state",
1106                                 G_CALLBACK(handle_ethernet_cable_state), NULL);
1107         g_signal_connect(netconfigstate, "handle-remove-route",
1108                                 G_CALLBACK(handle_remove_route), NULL);
1109         g_signal_connect(netconfigstate, "handle-launch-mdns",
1110                                 G_CALLBACK(handle_launch_mdns), NULL);
1111         g_signal_connect(netconfigstate, "handle-device-policy-set-wifi",
1112                                 G_CALLBACK(handle_device_policy_set_wifi), NULL);
1113         g_signal_connect(netconfigstate, "handle-device-policy-get-wifi",
1114                                 G_CALLBACK(handle_device_policy_get_wifi), NULL);
1115         g_signal_connect(netconfigstate, "handle-device-policy-set-wifi-profile",
1116                                 G_CALLBACK(handle_device_policy_set_wifi_profile), NULL);
1117         g_signal_connect(netconfigstate, "handle-device-policy-get-wifi-profile",
1118                                 G_CALLBACK(handle_device_policy_get_wifi_profile), NULL);
1119
1120         if (!g_dbus_interface_skeleton_export(interface_network, connection,
1121                         NETCONFIG_NETWORK_STATE_PATH, NULL)) {
1122                 ERR("Export with path failed");
1123         }
1124 }
1125
1126 void state_object_deinit(void)
1127 {
1128         g_object_unref(netconfigstate);
1129 }