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