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