Changes required for DHCP leased seconds API
[platform/upstream/connman.git] / src / ipconfig.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2013  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <stdio.h>
28 #include <net/if.h>
29 #include <net/if_arp.h>
30 #include <linux/if_link.h>
31 #include <string.h>
32 #include <stdlib.h>
33
34 #ifndef IFF_LOWER_UP
35 #define IFF_LOWER_UP    0x10000
36 #endif
37
38 #include <gdbus.h>
39 #include <connman/ipaddress.h>
40
41 #include "connman.h"
42
43 struct connman_ipconfig {
44         int refcount;
45         int index;
46         enum connman_ipconfig_type type;
47
48         const struct connman_ipconfig_ops *ops;
49         void *ops_data;
50
51         bool enabled;
52         enum connman_ipconfig_method method;
53         struct connman_ipaddress *address;
54         struct connman_ipaddress *system;
55
56 #if defined TIZEN_EXT
57         int dhcp_lease_duration;
58 #endif
59
60         int ipv6_privacy_config;
61         char *last_dhcp_address;
62         char **last_dhcpv6_prefixes;
63 };
64
65 struct connman_ipdevice {
66         int index;
67         unsigned short type;
68         unsigned int flags;
69         char *address;
70         uint16_t mtu;
71         uint32_t rx_packets;
72         uint32_t tx_packets;
73         uint32_t rx_bytes;
74         uint32_t tx_bytes;
75         uint32_t rx_errors;
76         uint32_t tx_errors;
77         uint32_t rx_dropped;
78         uint32_t tx_dropped;
79
80         GSList *address_list;
81         char *ipv4_gateway;
82         char *ipv6_gateway;
83
84         char *pac;
85
86         struct connman_ipconfig *config_ipv4;
87         struct connman_ipconfig *config_ipv6;
88
89         bool ipv6_enabled;
90         int ipv6_privacy;
91 };
92
93 static GHashTable *ipdevice_hash = NULL;
94 static GList *ipconfig_list = NULL;
95 static bool is_ipv6_supported = false;
96
97 void __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
98 {
99         if (!ipconfig)
100                 return;
101
102         connman_ipaddress_clear(ipconfig->address);
103 }
104
105 static void free_address_list(struct connman_ipdevice *ipdevice)
106 {
107         GSList *list;
108
109         for (list = ipdevice->address_list; list; list = list->next) {
110                 struct connman_ipaddress *ipaddress = list->data;
111
112                 connman_ipaddress_free(ipaddress);
113                 list->data = NULL;
114         }
115
116         g_slist_free(ipdevice->address_list);
117         ipdevice->address_list = NULL;
118 }
119
120 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
121                                 unsigned char prefixlen, const char *local)
122 {
123         GSList *list;
124
125         for (list = ipdevice->address_list; list; list = list->next) {
126                 struct connman_ipaddress *ipaddress = list->data;
127
128                 if (g_strcmp0(ipaddress->local, local) == 0 &&
129                                         ipaddress->prefixlen == prefixlen)
130                         return ipaddress;
131         }
132
133         return NULL;
134 }
135
136 const char *__connman_ipconfig_type2string(enum connman_ipconfig_type type)
137 {
138         switch (type) {
139         case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
140         case CONNMAN_IPCONFIG_TYPE_ALL:
141                 return "unknown";
142         case CONNMAN_IPCONFIG_TYPE_IPV4:
143                 return "IPv4";
144         case CONNMAN_IPCONFIG_TYPE_IPV6:
145                 return "IPv6";
146         }
147
148         return NULL;
149 }
150
151 static const char *type2str(unsigned short type)
152 {
153         switch (type) {
154         case ARPHRD_ETHER:
155                 return "ETHER";
156         case ARPHRD_LOOPBACK:
157                 return "LOOPBACK";
158         case ARPHRD_PPP:
159                 return "PPP";
160         case ARPHRD_NONE:
161                 return "NONE";
162         case ARPHRD_VOID:
163                 return "VOID";
164         }
165
166         return "";
167 }
168
169 static const char *scope2str(unsigned char scope)
170 {
171         switch (scope) {
172         case 0:
173                 return "UNIVERSE";
174         case 253:
175                 return "LINK";
176         }
177
178         return "";
179 }
180
181 static bool get_ipv6_state(gchar *ifname)
182 {
183         int disabled;
184         gchar *path;
185         FILE *f;
186         bool enabled = false;
187
188         if (!ifname)
189                 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
190         else
191                 path = g_strdup_printf(
192                         "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
193
194         if (!path)
195                 return enabled;
196
197         f = fopen(path, "r");
198
199         g_free(path);
200
201         if (f) {
202                 if (fscanf(f, "%d", &disabled) > 0)
203                         enabled = !disabled;
204                 fclose(f);
205         }
206
207         return enabled;
208 }
209
210 static void set_ipv6_state(gchar *ifname, bool enable)
211 {
212         gchar *path;
213         FILE *f;
214
215         if (!ifname)
216                 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
217         else
218                 path = g_strdup_printf(
219                         "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
220
221         if (!path)
222                 return;
223
224         f = fopen(path, "r+");
225
226         g_free(path);
227
228         if (!f)
229                 return;
230
231         if (!enable)
232                 fprintf(f, "1");
233         else
234                 fprintf(f, "0");
235
236         fclose(f);
237 }
238
239 static int get_ipv6_privacy(gchar *ifname)
240 {
241         gchar *path;
242         FILE *f;
243         int value;
244
245         if (!ifname)
246                 return 0;
247
248         path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
249                                                                 ifname);
250
251         if (!path)
252                 return 0;
253
254         f = fopen(path, "r");
255
256         g_free(path);
257
258         if (!f)
259                 return 0;
260
261         if (fscanf(f, "%d", &value) <= 0)
262                 value = 0;
263
264         fclose(f);
265
266         return value;
267 }
268
269 /* Enable the IPv6 privacy extension for stateless address autoconfiguration.
270  * The privacy extension is described in RFC 3041 and RFC 4941
271  */
272 static void set_ipv6_privacy(gchar *ifname, int value)
273 {
274         gchar *path;
275         FILE *f;
276
277         if (!ifname)
278                 return;
279
280         path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
281                                                                 ifname);
282
283         if (!path)
284                 return;
285
286         if (value < 0)
287                 value = 0;
288
289         f = fopen(path, "r+");
290
291         g_free(path);
292
293         if (!f)
294                 return;
295
296         fprintf(f, "%d", value);
297         fclose(f);
298 }
299
300 static int get_rp_filter(void)
301 {
302         FILE *f;
303         int value = -EINVAL, tmp;
304
305         f = fopen("/proc/sys/net/ipv4/conf/all/rp_filter", "r");
306
307         if (f) {
308                 if (fscanf(f, "%d", &tmp) == 1)
309                         value = tmp;
310                 fclose(f);
311         }
312
313         return value;
314 }
315
316 static void set_rp_filter(int value)
317 {
318         FILE *f;
319
320         f = fopen("/proc/sys/net/ipv4/conf/all/rp_filter", "r+");
321
322         if (!f)
323                 return;
324
325         fprintf(f, "%d", value);
326
327         fclose(f);
328 }
329
330 int __connman_ipconfig_set_rp_filter()
331 {
332         int value;
333
334         value = get_rp_filter();
335
336         if (value < 0)
337                 return value;
338
339         set_rp_filter(2);
340
341         connman_info("rp_filter set to 2 (loose mode routing), "
342                         "old value was %d", value);
343
344         return value;
345 }
346
347 void __connman_ipconfig_unset_rp_filter(int old_value)
348 {
349         set_rp_filter(old_value);
350
351         connman_info("rp_filter restored to %d", old_value);
352 }
353
354 bool __connman_ipconfig_ipv6_privacy_enabled(struct connman_ipconfig *ipconfig)
355 {
356         if (!ipconfig)
357                 return false;
358
359         return ipconfig->ipv6_privacy_config == 0 ? FALSE : TRUE;
360 }
361
362 bool __connman_ipconfig_ipv6_is_enabled(struct connman_ipconfig *ipconfig)
363 {
364         struct connman_ipdevice *ipdevice;
365         char *ifname;
366         bool ret;
367
368         if (!ipconfig)
369                 return false;
370
371         ipdevice = g_hash_table_lookup(ipdevice_hash,
372                                         GINT_TO_POINTER(ipconfig->index));
373         if (!ipdevice)
374                 return false;
375
376         ifname = connman_inet_ifname(ipconfig->index);
377         ret = get_ipv6_state(ifname);
378         g_free(ifname);
379
380         return ret;
381 }
382
383 static void free_ipdevice(gpointer data)
384 {
385         struct connman_ipdevice *ipdevice = data;
386         char *ifname = connman_inet_ifname(ipdevice->index);
387
388         connman_info("%s {remove} index %d", ifname, ipdevice->index);
389
390         if (ipdevice->config_ipv4) {
391                 __connman_ipconfig_unref(ipdevice->config_ipv4);
392                 ipdevice->config_ipv4 = NULL;
393         }
394
395         if (ipdevice->config_ipv6) {
396                 __connman_ipconfig_unref(ipdevice->config_ipv6);
397                 ipdevice->config_ipv6 = NULL;
398         }
399
400         free_address_list(ipdevice);
401         g_free(ipdevice->ipv4_gateway);
402         g_free(ipdevice->ipv6_gateway);
403         g_free(ipdevice->pac);
404
405         g_free(ipdevice->address);
406
407         set_ipv6_state(ifname, ipdevice->ipv6_enabled);
408         set_ipv6_privacy(ifname, ipdevice->ipv6_privacy);
409
410         g_free(ifname);
411         g_free(ipdevice);
412 }
413
414 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
415 {
416         DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
417                         ipdevice->config_ipv6);
418 #if defined TIZEN_EXT
419         if (ipdevice->config_ipv6 != NULL &&
420                         ipdevice->config_ipv6->enabled == TRUE)
421                 return;
422
423         char *ifname = connman_inet_ifname(ipdevice->index);
424
425         if (__connman_device_isfiltered(ifname) == FALSE) {
426                 ipdevice->ipv6_enabled = get_ipv6_state(ifname);
427                 set_ipv6_state(ifname, FALSE);
428         }
429         g_free(ifname);
430 #endif
431 }
432
433 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
434 {
435         DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
436                                         ipdevice->config_ipv6);
437
438         if (ipdevice->config_ipv4)
439                 connman_inet_clear_address(ipdevice->index,
440                                         ipdevice->config_ipv4->address);
441
442         if (ipdevice->config_ipv6)
443                 connman_inet_clear_ipv6_address(ipdevice->index,
444                                 ipdevice->config_ipv6->address->local,
445                                 ipdevice->config_ipv6->address->prefixlen);
446 }
447
448 static void update_stats(struct connman_ipdevice *ipdevice,
449                         const char *ifname, struct rtnl_link_stats *stats)
450 {
451         struct connman_service *service;
452
453         if (stats->rx_packets == 0 && stats->tx_packets == 0)
454                 return;
455
456         connman_info("%s {RX} %u packets %u bytes", ifname,
457                                         stats->rx_packets, stats->rx_bytes);
458         connman_info("%s {TX} %u packets %u bytes", ifname,
459                                         stats->tx_packets, stats->tx_bytes);
460
461         if (!ipdevice->config_ipv4 && !ipdevice->config_ipv6)
462                 return;
463
464         service = __connman_service_lookup_from_index(ipdevice->index);
465
466         DBG("service %p", service);
467
468         if (!service)
469                 return;
470
471         ipdevice->rx_packets = stats->rx_packets;
472         ipdevice->tx_packets = stats->tx_packets;
473         ipdevice->rx_bytes = stats->rx_bytes;
474         ipdevice->tx_bytes = stats->tx_bytes;
475         ipdevice->rx_errors = stats->rx_errors;
476         ipdevice->tx_errors = stats->tx_errors;
477         ipdevice->rx_dropped = stats->rx_dropped;
478         ipdevice->tx_dropped = stats->tx_dropped;
479
480         __connman_service_notify(service,
481                                 ipdevice->rx_packets, ipdevice->tx_packets,
482                                 ipdevice->rx_bytes, ipdevice->tx_bytes,
483                                 ipdevice->rx_errors, ipdevice->tx_errors,
484                                 ipdevice->rx_dropped, ipdevice->tx_dropped);
485 }
486
487 void __connman_ipconfig_newlink(int index, unsigned short type,
488                                 unsigned int flags, const char *address,
489                                                         unsigned short mtu,
490                                                 struct rtnl_link_stats *stats)
491 {
492         struct connman_ipdevice *ipdevice;
493         GList *list, *ipconfig_copy;
494         GString *str;
495         bool up = false, down = false;
496         bool lower_up = false, lower_down = false;
497         char *ifname;
498
499         DBG("index %d", index);
500
501         if (type == ARPHRD_LOOPBACK)
502                 return;
503
504         ifname = connman_inet_ifname(index);
505
506         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
507         if (ipdevice)
508                 goto update;
509
510         ipdevice = g_try_new0(struct connman_ipdevice, 1);
511         if (!ipdevice)
512                 goto out;
513
514         ipdevice->index = index;
515         ipdevice->type = type;
516
517         ipdevice->ipv6_enabled = get_ipv6_state(ifname);
518         ipdevice->ipv6_privacy = get_ipv6_privacy(ifname);
519
520         ipdevice->address = g_strdup(address);
521
522         g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
523
524         connman_info("%s {create} index %d type %d <%s>", ifname,
525                                                 index, type, type2str(type));
526
527 update:
528 #if defined TIZEN_EXT
529         if (g_strcmp0(ipdevice->address, address) != 0) {
530                 /* If an original address is built-in physical device,
531                  * it's hardly get an address at a initial creation
532                  */
533                 g_free(ipdevice->address);
534                 ipdevice->address = g_strdup(address);
535         }
536 #endif
537
538         ipdevice->mtu = mtu;
539
540         update_stats(ipdevice, ifname, stats);
541
542         if (flags == ipdevice->flags)
543                 goto out;
544
545         if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
546                 if (flags & IFF_UP)
547                         up = true;
548                 else
549                         down = true;
550         }
551
552         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
553                                 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
554                 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
555                                         (IFF_RUNNING | IFF_LOWER_UP))
556                         lower_up = true;
557                 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
558                         lower_down = true;
559         }
560
561         ipdevice->flags = flags;
562
563         str = g_string_new(NULL);
564         if (!str)
565                 goto out;
566
567         if (flags & IFF_UP)
568                 g_string_append(str, "UP");
569         else
570                 g_string_append(str, "DOWN");
571
572         if (flags & IFF_RUNNING)
573                 g_string_append(str, ",RUNNING");
574
575         if (flags & IFF_LOWER_UP)
576                 g_string_append(str, ",LOWER_UP");
577
578         connman_info("%s {update} flags %u <%s>", ifname, flags, str->str);
579
580         g_string_free(str, TRUE);
581
582         ipconfig_copy = g_list_copy(ipconfig_list);
583
584         for (list = g_list_first(ipconfig_copy); list;
585                                                 list = g_list_next(list)) {
586                 struct connman_ipconfig *ipconfig = list->data;
587
588                 if (index != ipconfig->index)
589                         continue;
590
591                 if (!ipconfig->ops)
592                         continue;
593
594                 if (up && ipconfig->ops->up)
595                         ipconfig->ops->up(ipconfig, ifname);
596                 if (lower_up && ipconfig->ops->lower_up)
597                         ipconfig->ops->lower_up(ipconfig, ifname);
598
599                 if (lower_down && ipconfig->ops->lower_down)
600                         ipconfig->ops->lower_down(ipconfig, ifname);
601                 if (down && ipconfig->ops->down)
602                         ipconfig->ops->down(ipconfig, ifname);
603         }
604
605         g_list_free(ipconfig_copy);
606
607         if (lower_up)
608                 __connman_ipconfig_lower_up(ipdevice);
609         if (lower_down)
610                 __connman_ipconfig_lower_down(ipdevice);
611
612 out:
613         g_free(ifname);
614 }
615
616 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
617 {
618         struct connman_ipdevice *ipdevice;
619         GList *list;
620         char *ifname;
621
622         DBG("index %d", index);
623
624         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
625         if (!ipdevice)
626                 return;
627
628         ifname = connman_inet_ifname(index);
629
630         update_stats(ipdevice, ifname, stats);
631
632         for (list = g_list_first(ipconfig_list); list;
633                                                 list = g_list_next(list)) {
634                 struct connman_ipconfig *ipconfig = list->data;
635
636                 if (index != ipconfig->index)
637                         continue;
638
639                 ipconfig->index = -1;
640
641                 if (!ipconfig->ops)
642                         continue;
643
644                 if (ipconfig->ops->lower_down)
645                         ipconfig->ops->lower_down(ipconfig, ifname);
646                 if (ipconfig->ops->down)
647                         ipconfig->ops->down(ipconfig, ifname);
648         }
649
650         g_free(ifname);
651
652         __connman_ipconfig_lower_down(ipdevice);
653
654         g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
655 }
656
657 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
658 {
659         const struct connman_ipaddress *addr1 = a;
660         const struct connman_ipaddress *addr2 = b;
661
662         if (addr1->prefixlen != addr2->prefixlen)
663                 return addr2->prefixlen - addr1->prefixlen;
664
665         return g_strcmp0(addr1->local, addr2->local);
666 }
667
668 int __connman_ipconfig_newaddr(int index, int family, const char *label,
669                                 unsigned char prefixlen, const char *address)
670 {
671         struct connman_ipdevice *ipdevice;
672         struct connman_ipaddress *ipaddress;
673         enum connman_ipconfig_type type;
674         GList *list;
675         char *ifname;
676
677         DBG("index %d", index);
678
679         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
680         if (!ipdevice)
681                 return -ENXIO;
682
683         ipaddress = connman_ipaddress_alloc(family);
684         if (!ipaddress)
685                 return -ENOMEM;
686
687         ipaddress->prefixlen = prefixlen;
688         ipaddress->local = g_strdup(address);
689
690         if (g_slist_find_custom(ipdevice->address_list, ipaddress,
691                                         check_duplicate_address)) {
692                 connman_ipaddress_free(ipaddress);
693                 return -EALREADY;
694         }
695
696         if (family == AF_INET)
697                 type = CONNMAN_IPCONFIG_TYPE_IPV4;
698         else if (family == AF_INET6)
699                 type = CONNMAN_IPCONFIG_TYPE_IPV6;
700         else
701                 return -EINVAL;
702
703         ipdevice->address_list = g_slist_prepend(ipdevice->address_list,
704                                                                 ipaddress);
705
706         ifname = connman_inet_ifname(index);
707         connman_info("%s {add} address %s/%u label %s family %d",
708                 ifname, address, prefixlen, label, family);
709
710         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
711                 __connman_ippool_newaddr(index, address, prefixlen);
712
713         if (ipdevice->config_ipv4 && family == AF_INET)
714                 connman_ipaddress_copy_address(ipdevice->config_ipv4->system,
715                                         ipaddress);
716
717         else if (ipdevice->config_ipv6 && family == AF_INET6)
718                 connman_ipaddress_copy_address(ipdevice->config_ipv6->system,
719                                         ipaddress);
720         else
721                 goto out;
722
723         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
724                 goto out;
725
726         for (list = g_list_first(ipconfig_list); list;
727                                                 list = g_list_next(list)) {
728                 struct connman_ipconfig *ipconfig = list->data;
729
730                 if (index != ipconfig->index)
731                         continue;
732
733                 if (type != ipconfig->type)
734                         continue;
735
736                 if (!ipconfig->ops)
737                         continue;
738
739                 if (ipconfig->ops->ip_bound)
740                         ipconfig->ops->ip_bound(ipconfig, ifname);
741         }
742
743 out:
744         g_free(ifname);
745         return 0;
746 }
747
748 void __connman_ipconfig_deladdr(int index, int family, const char *label,
749                                 unsigned char prefixlen, const char *address)
750 {
751         struct connman_ipdevice *ipdevice;
752         struct connman_ipaddress *ipaddress;
753         enum connman_ipconfig_type type;
754         GList *list;
755         char *ifname;
756
757         DBG("index %d", index);
758
759         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
760         if (!ipdevice)
761                 return;
762
763         ipaddress = find_ipaddress(ipdevice, prefixlen, address);
764         if (!ipaddress)
765                 return;
766
767         if (family == AF_INET)
768                 type = CONNMAN_IPCONFIG_TYPE_IPV4;
769         else if (family == AF_INET6)
770                 type = CONNMAN_IPCONFIG_TYPE_IPV6;
771         else
772                 return;
773
774         ipdevice->address_list = g_slist_remove(ipdevice->address_list,
775                                                                 ipaddress);
776
777         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
778                 __connman_ippool_deladdr(index, address, prefixlen);
779
780         connman_ipaddress_clear(ipaddress);
781         g_free(ipaddress);
782
783         ifname = connman_inet_ifname(index);
784         connman_info("%s {del} address %s/%u label %s", ifname,
785                                                 address, prefixlen, label);
786
787         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
788                 goto out;
789
790         if (g_slist_length(ipdevice->address_list) > 0)
791                 goto out;
792
793         for (list = g_list_first(ipconfig_list); list;
794                                                 list = g_list_next(list)) {
795                 struct connman_ipconfig *ipconfig = list->data;
796
797                 if (index != ipconfig->index)
798                         continue;
799
800                 if (type != ipconfig->type)
801                         continue;
802
803                 if (!ipconfig->ops)
804                         continue;
805
806                 if (ipconfig->ops->ip_release)
807                         ipconfig->ops->ip_release(ipconfig, ifname);
808         }
809
810 out:
811         g_free(ifname);
812 }
813
814 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
815                                         const char *dst, const char *gateway)
816 {
817         struct connman_ipdevice *ipdevice;
818         char *ifname;
819
820         DBG("index %d", index);
821
822         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
823         if (!ipdevice)
824                 return;
825
826         ifname = connman_inet_ifname(index);
827
828         if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
829                                                 g_strcmp0(dst, "::") == 0)) {
830                 GList *config_list;
831                 enum connman_ipconfig_type type;
832
833                 if (family == AF_INET6) {
834                         type = CONNMAN_IPCONFIG_TYPE_IPV6;
835                         g_free(ipdevice->ipv6_gateway);
836                         ipdevice->ipv6_gateway = g_strdup(gateway);
837
838                         if (ipdevice->config_ipv6 &&
839                                 ipdevice->config_ipv6->system) {
840                                 g_free(ipdevice->config_ipv6->system->gateway);
841                                 ipdevice->config_ipv6->system->gateway =
842                                         g_strdup(gateway);
843                         }
844                 } else if (family == AF_INET) {
845                         type = CONNMAN_IPCONFIG_TYPE_IPV4;
846                         g_free(ipdevice->ipv4_gateway);
847                         ipdevice->ipv4_gateway = g_strdup(gateway);
848
849                         if (ipdevice->config_ipv4 &&
850                                 ipdevice->config_ipv4->system) {
851                                 g_free(ipdevice->config_ipv4->system->gateway);
852                                 ipdevice->config_ipv4->system->gateway =
853                                         g_strdup(gateway);
854                         }
855                 } else
856                         goto out;
857
858                 for (config_list = g_list_first(ipconfig_list); config_list;
859                                         config_list = g_list_next(config_list)) {
860                         struct connman_ipconfig *ipconfig = config_list->data;
861
862                         if (index != ipconfig->index)
863                                 continue;
864
865                         if (type != ipconfig->type)
866                                 continue;
867
868                         if (!ipconfig->ops)
869                                 continue;
870
871                         if (ipconfig->ops->route_set)
872                                 ipconfig->ops->route_set(ipconfig, ifname);
873                 }
874         }
875
876         connman_info("%s {add} route %s gw %s scope %u <%s>",
877                 ifname, dst, gateway, scope, scope2str(scope));
878
879 out:
880         g_free(ifname);
881 }
882
883 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
884                                         const char *dst, const char *gateway)
885 {
886         struct connman_ipdevice *ipdevice;
887         char *ifname;
888
889         DBG("index %d", index);
890
891         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
892         if (!ipdevice)
893                 return;
894
895         ifname = connman_inet_ifname(index);
896
897         if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
898                                                 g_strcmp0(dst, "::") == 0)) {
899                 GList *config_list;
900                 enum connman_ipconfig_type type;
901
902                 if (family == AF_INET6) {
903                         type = CONNMAN_IPCONFIG_TYPE_IPV6;
904                         g_free(ipdevice->ipv6_gateway);
905                         ipdevice->ipv6_gateway = NULL;
906
907                         if (ipdevice->config_ipv6 &&
908                                 ipdevice->config_ipv6->system) {
909                                 g_free(ipdevice->config_ipv6->system->gateway);
910                                 ipdevice->config_ipv6->system->gateway = NULL;
911                         }
912                 } else if (family == AF_INET) {
913                         type = CONNMAN_IPCONFIG_TYPE_IPV4;
914                         g_free(ipdevice->ipv4_gateway);
915                         ipdevice->ipv4_gateway = NULL;
916
917                         if (ipdevice->config_ipv4 &&
918                                 ipdevice->config_ipv4->system) {
919                                 g_free(ipdevice->config_ipv4->system->gateway);
920                                 ipdevice->config_ipv4->system->gateway = NULL;
921                         }
922                 } else
923                         goto out;
924
925                 for (config_list = g_list_first(ipconfig_list); config_list;
926                                         config_list = g_list_next(config_list)) {
927                         struct connman_ipconfig *ipconfig = config_list->data;
928
929                         if (index != ipconfig->index)
930                                 continue;
931
932                         if (type != ipconfig->type)
933                                 continue;
934
935                         if (!ipconfig->ops)
936                                 continue;
937
938                         if (ipconfig->ops->route_unset)
939                                 ipconfig->ops->route_unset(ipconfig, ifname);
940                 }
941         }
942
943         connman_info("%s {del} route %s gw %s scope %u <%s>",
944                 ifname, dst, gateway, scope, scope2str(scope));
945
946 out:
947         g_free(ifname);
948 }
949
950 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
951                                                         void *user_data)
952 {
953         GList *list, *keys;
954
955         keys = g_hash_table_get_keys(ipdevice_hash);
956         if (!keys)
957                 return;
958
959         for (list = g_list_first(keys); list; list = g_list_next(list)) {
960                 int index = GPOINTER_TO_INT(list->data);
961
962                 function(index, user_data);
963         }
964
965         g_list_free(keys);
966 }
967
968 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
969                                         struct connman_ipconfig *ipconfig)
970 {
971         return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
972 }
973
974 unsigned short __connman_ipconfig_get_type_from_index(int index)
975 {
976         struct connman_ipdevice *ipdevice;
977
978         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
979         if (!ipdevice)
980                 return ARPHRD_VOID;
981
982         return ipdevice->type;
983 }
984
985 unsigned int __connman_ipconfig_get_flags_from_index(int index)
986 {
987         struct connman_ipdevice *ipdevice;
988
989         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
990         if (!ipdevice)
991                 return 0;
992
993         return ipdevice->flags;
994 }
995
996 const char *__connman_ipconfig_get_gateway_from_index(int index,
997         enum connman_ipconfig_type type)
998 {
999         struct connman_ipdevice *ipdevice;
1000
1001         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1002         if (!ipdevice)
1003                 return NULL;
1004
1005         if (type != CONNMAN_IPCONFIG_TYPE_IPV6) {
1006                 if (ipdevice->ipv4_gateway)
1007                         return ipdevice->ipv4_gateway;
1008
1009                 if (ipdevice->config_ipv4 &&
1010                                 ipdevice->config_ipv4->address)
1011                         return ipdevice->config_ipv4->address->gateway;
1012         }
1013
1014         if (type != CONNMAN_IPCONFIG_TYPE_IPV4) {
1015                 if (ipdevice->ipv6_gateway)
1016                         return ipdevice->ipv6_gateway;
1017
1018                 if (ipdevice->config_ipv6 &&
1019                                 ipdevice->config_ipv6->address)
1020                         return ipdevice->config_ipv6->address->gateway;
1021         }
1022
1023         return NULL;
1024 }
1025
1026 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
1027 {
1028         ipconfig->index = index;
1029 }
1030
1031 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
1032 {
1033         if (!ipconfig->address)
1034                 return NULL;
1035
1036         return ipconfig->address->local;
1037 }
1038
1039 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig,
1040                                         const char *address)
1041 {
1042         if (!ipconfig->address)
1043                 return;
1044
1045         g_free(ipconfig->address->local);
1046         ipconfig->address->local = g_strdup(address);
1047 }
1048
1049 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
1050 {
1051         if (!ipconfig->address)
1052                 return NULL;
1053
1054         return ipconfig->address->peer;
1055 }
1056
1057 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig,
1058                                         const char *address)
1059 {
1060         if (!ipconfig->address)
1061                 return;
1062
1063         g_free(ipconfig->address->peer);
1064         ipconfig->address->peer = g_strdup(address);
1065 }
1066
1067 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1068 {
1069         if (!ipconfig->address)
1070                 return NULL;
1071
1072         return ipconfig->address->broadcast;
1073 }
1074
1075 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig,
1076                                         const char *broadcast)
1077 {
1078         if (!ipconfig->address)
1079                 return;
1080
1081         g_free(ipconfig->address->broadcast);
1082         ipconfig->address->broadcast = g_strdup(broadcast);
1083 }
1084
1085 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1086 {
1087         if (!ipconfig->address)
1088                 return NULL;
1089
1090         return ipconfig->address->gateway;
1091 }
1092
1093 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig,
1094                                         const char *gateway)
1095 {
1096         DBG("");
1097
1098         if (!ipconfig->address)
1099                 return;
1100         g_free(ipconfig->address->gateway);
1101         ipconfig->address->gateway = g_strdup(gateway);
1102 }
1103
1104 #if defined TIZEN_EXT
1105 void __connman_ipconfig_set_dhcp_lease_duration(struct connman_ipconfig *ipconfig,
1106                 int dhcp_lease_duration)
1107 {
1108         ipconfig->dhcp_lease_duration = dhcp_lease_duration;
1109 }
1110 #endif
1111
1112 #if defined TIZEN_EXT
1113 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig, struct connman_service *service)
1114 #else
1115 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1116 #endif
1117 {
1118 #if !defined TIZEN_EXT
1119         struct connman_service *service;
1120 #endif
1121
1122         DBG("");
1123
1124         if (!ipconfig->address)
1125                 return -EINVAL;
1126
1127 #if !defined TIZEN_EXT
1128         service = __connman_service_lookup_from_index(ipconfig->index);
1129 #endif
1130         if (!service)
1131                 return -EINVAL;
1132
1133         DBG("type %d gw %s peer %s", ipconfig->type,
1134                 ipconfig->address->gateway, ipconfig->address->peer);
1135
1136         if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6 ||
1137                                 ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1138                 return __connman_connection_gateway_add(service,
1139                                                 ipconfig->address->gateway,
1140                                                 ipconfig->type,
1141                                                 ipconfig->address->peer);
1142
1143         return 0;
1144 }
1145
1146 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1147 {
1148         struct connman_service *service;
1149
1150         DBG("");
1151
1152         service = __connman_service_lookup_from_index(ipconfig->index);
1153         if (service)
1154                 __connman_connection_gateway_remove(service, ipconfig->type);
1155 }
1156
1157 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1158 {
1159         if (!ipconfig->address)
1160                 return 0;
1161
1162         return ipconfig->address->prefixlen;
1163 }
1164
1165 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig,
1166                                         unsigned char prefixlen)
1167 {
1168         if (!ipconfig->address)
1169                 return;
1170
1171         ipconfig->address->prefixlen = prefixlen;
1172 }
1173
1174 static struct connman_ipconfig *create_ipv6config(int index)
1175 {
1176         struct connman_ipconfig *ipv6config;
1177         struct connman_ipdevice *ipdevice;
1178
1179         DBG("index %d", index);
1180
1181         ipv6config = g_try_new0(struct connman_ipconfig, 1);
1182         if (!ipv6config)
1183                 return NULL;
1184
1185         ipv6config->refcount = 1;
1186
1187         ipv6config->index = index;
1188         ipv6config->enabled = false;
1189         ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1190
1191         if (!is_ipv6_supported)
1192                 ipv6config->method = CONNMAN_IPCONFIG_METHOD_OFF;
1193         else
1194                 ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1195
1196         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1197         if (ipdevice)
1198                 ipv6config->ipv6_privacy_config = ipdevice->ipv6_privacy;
1199
1200         ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1201         if (!ipv6config->address) {
1202                 g_free(ipv6config);
1203                 return NULL;
1204         }
1205
1206         ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1207
1208         DBG("ipconfig %p method %s", ipv6config,
1209                 __connman_ipconfig_method2string(ipv6config->method));
1210
1211         return ipv6config;
1212 }
1213
1214 /**
1215  * connman_ipconfig_create:
1216  *
1217  * Allocate a new ipconfig structure.
1218  *
1219  * Returns: a newly-allocated #connman_ipconfig structure
1220  */
1221 struct connman_ipconfig *__connman_ipconfig_create(int index,
1222                                         enum connman_ipconfig_type type)
1223 {
1224         struct connman_ipconfig *ipconfig;
1225
1226         if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1227                 return create_ipv6config(index);
1228
1229         DBG("index %d", index);
1230
1231         ipconfig = g_try_new0(struct connman_ipconfig, 1);
1232         if (!ipconfig)
1233                 return NULL;
1234
1235         ipconfig->refcount = 1;
1236
1237         ipconfig->index = index;
1238         ipconfig->enabled = false;
1239         ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1240
1241         ipconfig->address = connman_ipaddress_alloc(AF_INET);
1242         if (!ipconfig->address) {
1243                 g_free(ipconfig);
1244                 return NULL;
1245         }
1246
1247         ipconfig->system = connman_ipaddress_alloc(AF_INET);
1248
1249         DBG("ipconfig %p", ipconfig);
1250
1251         return ipconfig;
1252 }
1253
1254
1255 /**
1256  * connman_ipconfig_ref:
1257  * @ipconfig: ipconfig structure
1258  *
1259  * Increase reference counter of ipconfig
1260  */
1261 struct connman_ipconfig *
1262 __connman_ipconfig_ref_debug(struct connman_ipconfig *ipconfig,
1263                                 const char *file, int line, const char *caller)
1264 {
1265         DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount + 1,
1266                 file, line, caller);
1267
1268         __sync_fetch_and_add(&ipconfig->refcount, 1);
1269
1270         return ipconfig;
1271 }
1272
1273 /**
1274  * connman_ipconfig_unref:
1275  * @ipconfig: ipconfig structure
1276  *
1277  * Decrease reference counter of ipconfig
1278  */
1279 void __connman_ipconfig_unref_debug(struct connman_ipconfig *ipconfig,
1280                                 const char *file, int line, const char *caller)
1281 {
1282         if (!ipconfig)
1283                 return;
1284
1285         DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount - 1,
1286                 file, line, caller);
1287
1288         if (__sync_fetch_and_sub(&ipconfig->refcount, 1) != 1)
1289                 return;
1290
1291         if (__connman_ipconfig_disable(ipconfig) < 0)
1292                 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1293
1294         __connman_ipconfig_set_ops(ipconfig, NULL);
1295
1296         connman_ipaddress_free(ipconfig->system);
1297         connman_ipaddress_free(ipconfig->address);
1298         g_free(ipconfig->last_dhcp_address);
1299         g_strfreev(ipconfig->last_dhcpv6_prefixes);
1300         g_free(ipconfig);
1301 }
1302
1303 /**
1304  * connman_ipconfig_get_data:
1305  * @ipconfig: ipconfig structure
1306  *
1307  * Get private data pointer
1308  */
1309 void *__connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1310 {
1311         if (!ipconfig)
1312                 return NULL;
1313
1314         return ipconfig->ops_data;
1315 }
1316
1317 /**
1318  * connman_ipconfig_set_data:
1319  * @ipconfig: ipconfig structure
1320  * @data: data pointer
1321  *
1322  * Set private data pointer
1323  */
1324 void __connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1325 {
1326         ipconfig->ops_data = data;
1327 }
1328
1329 /**
1330  * connman_ipconfig_get_index:
1331  * @ipconfig: ipconfig structure
1332  *
1333  * Get interface index
1334  */
1335 int __connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1336 {
1337         if (!ipconfig)
1338                 return -1;
1339
1340         return ipconfig->index;
1341 }
1342
1343 /**
1344  * connman_ipconfig_set_ops:
1345  * @ipconfig: ipconfig structure
1346  * @ops: operation callbacks
1347  *
1348  * Set the operation callbacks
1349  */
1350 void __connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1351                                 const struct connman_ipconfig_ops *ops)
1352 {
1353         ipconfig->ops = ops;
1354 }
1355
1356 /**
1357  * connman_ipconfig_set_method:
1358  * @ipconfig: ipconfig structure
1359  * @method: configuration method
1360  *
1361  * Set the configuration method
1362  */
1363 int __connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1364                                         enum connman_ipconfig_method method)
1365 {
1366         ipconfig->method = method;
1367
1368         return 0;
1369 }
1370
1371 enum connman_ipconfig_method __connman_ipconfig_get_method(
1372                                 struct connman_ipconfig *ipconfig)
1373 {
1374         if (!ipconfig)
1375                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1376
1377         return ipconfig->method;
1378 }
1379
1380 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1381 {
1382         DBG("");
1383
1384         switch (ipconfig->method) {
1385         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1386         case CONNMAN_IPCONFIG_METHOD_OFF:
1387                 break;
1388         case CONNMAN_IPCONFIG_METHOD_AUTO:
1389         case CONNMAN_IPCONFIG_METHOD_FIXED:
1390         case CONNMAN_IPCONFIG_METHOD_DHCP:
1391         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1392                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1393                         return connman_inet_set_address(ipconfig->index,
1394                                                         ipconfig->address);
1395                 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1396                         return connman_inet_set_ipv6_address(
1397                                         ipconfig->index, ipconfig->address);
1398         }
1399
1400         return 0;
1401 }
1402
1403 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1404 {
1405         int err;
1406
1407         DBG("");
1408
1409         if (!ipconfig)
1410                 return 0;
1411
1412         DBG("method %d", ipconfig->method);
1413
1414         switch (ipconfig->method) {
1415         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1416         case CONNMAN_IPCONFIG_METHOD_OFF:
1417                 break;
1418         case CONNMAN_IPCONFIG_METHOD_AUTO:
1419         case CONNMAN_IPCONFIG_METHOD_FIXED:
1420         case CONNMAN_IPCONFIG_METHOD_DHCP:
1421         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1422                 err = __connman_ipconfig_address_unset(ipconfig);
1423                 connman_ipaddress_clear(ipconfig->address);
1424
1425                 return err;
1426         }
1427
1428         return 0;
1429 }
1430
1431 int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig)
1432 {
1433         int err;
1434
1435         DBG("");
1436
1437         if (!ipconfig)
1438                 return 0;
1439
1440         DBG("method %d", ipconfig->method);
1441
1442         switch (ipconfig->method) {
1443         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1444         case CONNMAN_IPCONFIG_METHOD_OFF:
1445                 break;
1446         case CONNMAN_IPCONFIG_METHOD_AUTO:
1447         case CONNMAN_IPCONFIG_METHOD_FIXED:
1448         case CONNMAN_IPCONFIG_METHOD_DHCP:
1449         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1450                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1451                         err = connman_inet_clear_address(ipconfig->index,
1452                                                         ipconfig->address);
1453                 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1454                         err = connman_inet_clear_ipv6_address(
1455                                                 ipconfig->index,
1456                                                 ipconfig->address->local,
1457                                                 ipconfig->address->prefixlen);
1458                 else
1459                         err = -EINVAL;
1460
1461                 return err;
1462         }
1463
1464         return 0;
1465 }
1466
1467 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1468                                                         const char *url)
1469 {
1470         struct connman_ipdevice *ipdevice;
1471
1472         DBG("ipconfig %p", ipconfig);
1473
1474         if (!ipconfig || ipconfig->index < 0)
1475                 return -ENODEV;
1476
1477         ipdevice = g_hash_table_lookup(ipdevice_hash,
1478                                         GINT_TO_POINTER(ipconfig->index));
1479         if (!ipdevice)
1480                 return -ENXIO;
1481
1482         g_free(ipdevice->pac);
1483         ipdevice->pac = g_strdup(url);
1484
1485         return 0;
1486 }
1487
1488 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1489 {
1490         struct connman_ipdevice *ipdevice;
1491
1492         DBG("ipconfig %p", ipconfig);
1493
1494         if (!ipconfig || ipconfig->index < 0)
1495                 return NULL;
1496
1497         ipdevice = g_hash_table_lookup(ipdevice_hash,
1498                                         GINT_TO_POINTER(ipconfig->index));
1499         if (!ipdevice)
1500                 return NULL;
1501
1502         return ipdevice->pac;
1503 }
1504
1505 void __connman_ipconfig_set_dhcp_address(struct connman_ipconfig *ipconfig,
1506                                         const char *address)
1507 {
1508         if (!ipconfig)
1509                 return;
1510
1511         g_free(ipconfig->last_dhcp_address);
1512         ipconfig->last_dhcp_address = g_strdup(address);
1513 }
1514
1515 char *__connman_ipconfig_get_dhcp_address(struct connman_ipconfig *ipconfig)
1516 {
1517         if (!ipconfig)
1518                 return NULL;
1519
1520         return ipconfig->last_dhcp_address;
1521 }
1522
1523 void __connman_ipconfig_set_dhcpv6_prefixes(struct connman_ipconfig *ipconfig,
1524                                         char **prefixes)
1525 {
1526         if (!ipconfig)
1527                 return;
1528
1529         g_strfreev(ipconfig->last_dhcpv6_prefixes);
1530         ipconfig->last_dhcpv6_prefixes = prefixes;
1531 }
1532
1533 char **__connman_ipconfig_get_dhcpv6_prefixes(struct connman_ipconfig *ipconfig)
1534 {
1535         if (!ipconfig)
1536                 return NULL;
1537
1538         return ipconfig->last_dhcpv6_prefixes;
1539 }
1540
1541 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1542 {
1543         struct connman_ipdevice *ipdevice;
1544         char *ifname;
1545
1546         DBG("");
1547
1548         ipdevice = g_hash_table_lookup(ipdevice_hash,
1549                                         GINT_TO_POINTER(ipconfig->index));
1550         if (!ipdevice)
1551                 return;
1552
1553         ifname = connman_inet_ifname(ipconfig->index);
1554
1555         set_ipv6_state(ifname, false);
1556
1557         g_free(ifname);
1558 }
1559
1560 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1561 {
1562         struct connman_ipdevice *ipdevice;
1563         char *ifname;
1564
1565         DBG("");
1566
1567         ipdevice = g_hash_table_lookup(ipdevice_hash,
1568                                         GINT_TO_POINTER(ipconfig->index));
1569         if (!ipdevice)
1570                 return;
1571
1572         ifname = connman_inet_ifname(ipconfig->index);
1573
1574         if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1575                 set_ipv6_privacy(ifname, ipconfig->ipv6_privacy_config);
1576
1577         set_ipv6_state(ifname, true);
1578
1579         g_free(ifname);
1580 }
1581
1582 void __connman_ipconfig_enable_ipv6(struct connman_ipconfig *ipconfig)
1583 {
1584         if (!ipconfig || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1585                 return;
1586
1587         enable_ipv6(ipconfig);
1588 }
1589
1590 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1591 {
1592         if (!ipconfig || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1593                 return;
1594
1595         disable_ipv6(ipconfig);
1596 }
1597
1598 bool __connman_ipconfig_is_usable(struct connman_ipconfig *ipconfig)
1599 {
1600         if (!ipconfig)
1601                 return false;
1602
1603         switch (ipconfig->method) {
1604         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1605         case CONNMAN_IPCONFIG_METHOD_OFF:
1606                 return false;
1607         case CONNMAN_IPCONFIG_METHOD_AUTO:
1608         case CONNMAN_IPCONFIG_METHOD_FIXED:
1609         case CONNMAN_IPCONFIG_METHOD_DHCP:
1610         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1611                 break;
1612         }
1613
1614         return true;
1615 }
1616
1617 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1618 {
1619         struct connman_ipdevice *ipdevice;
1620         bool up = false, down = false;
1621         bool lower_up = false, lower_down = false;
1622         enum connman_ipconfig_type type;
1623         char *ifname;
1624
1625         DBG("ipconfig %p", ipconfig);
1626
1627         if (!ipconfig || ipconfig->index < 0)
1628                 return -ENODEV;
1629
1630         ipdevice = g_hash_table_lookup(ipdevice_hash,
1631                                         GINT_TO_POINTER(ipconfig->index));
1632         if (!ipdevice)
1633                 return -ENXIO;
1634
1635         if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1636                 if (ipdevice->config_ipv4 == ipconfig)
1637                         return -EALREADY;
1638                 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1639         } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1640                 if (ipdevice->config_ipv6 == ipconfig)
1641                         return -EALREADY;
1642                 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1643         } else
1644                 return -EINVAL;
1645
1646         ipconfig->enabled = true;
1647
1648         if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1649                                         ipdevice->config_ipv4) {
1650                 ipconfig_list = g_list_remove(ipconfig_list,
1651                                                         ipdevice->config_ipv4);
1652
1653                 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1654
1655                 __connman_ipconfig_unref(ipdevice->config_ipv4);
1656         }
1657
1658         if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1659                                         ipdevice->config_ipv6) {
1660                 ipconfig_list = g_list_remove(ipconfig_list,
1661                                                         ipdevice->config_ipv6);
1662
1663                 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1664
1665                 __connman_ipconfig_unref(ipdevice->config_ipv6);
1666         }
1667
1668         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1669                 ipdevice->config_ipv4 = __connman_ipconfig_ref(ipconfig);
1670         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1671                 ipdevice->config_ipv6 = __connman_ipconfig_ref(ipconfig);
1672
1673                 enable_ipv6(ipdevice->config_ipv6);
1674         }
1675         ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1676
1677         if (ipdevice->flags & IFF_UP)
1678                 up = true;
1679         else
1680                 down = true;
1681
1682         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1683                         (IFF_RUNNING | IFF_LOWER_UP))
1684                 lower_up = true;
1685         else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1686                 lower_down = true;
1687
1688         ifname = connman_inet_ifname(ipconfig->index);
1689
1690         if (up && ipconfig->ops->up)
1691                 ipconfig->ops->up(ipconfig, ifname);
1692         if (lower_up && ipconfig->ops->lower_up)
1693                 ipconfig->ops->lower_up(ipconfig, ifname);
1694
1695         if (lower_down && ipconfig->ops->lower_down)
1696                 ipconfig->ops->lower_down(ipconfig, ifname);
1697         if (down && ipconfig->ops->down)
1698                 ipconfig->ops->down(ipconfig, ifname);
1699
1700         g_free(ifname);
1701
1702         return 0;
1703 }
1704
1705 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1706 {
1707         struct connman_ipdevice *ipdevice;
1708
1709         DBG("ipconfig %p", ipconfig);
1710
1711         if (!ipconfig || ipconfig->index < 0)
1712                 return -ENODEV;
1713
1714         ipdevice = g_hash_table_lookup(ipdevice_hash,
1715                                         GINT_TO_POINTER(ipconfig->index));
1716         if (!ipdevice)
1717                 return -ENXIO;
1718
1719         if (!ipdevice->config_ipv4 && !ipdevice->config_ipv6)
1720                 return -EINVAL;
1721
1722         ipconfig->enabled = false;
1723
1724         if (ipdevice->config_ipv4 == ipconfig) {
1725                 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1726
1727                 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1728                 __connman_ipconfig_unref(ipdevice->config_ipv4);
1729                 ipdevice->config_ipv4 = NULL;
1730                 return 0;
1731         }
1732
1733         if (ipdevice->config_ipv6 == ipconfig) {
1734                 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1735
1736 #if defined TIZEN_EXT
1737                 if (ipdevice->config_ipv6->method ==
1738                                 CONNMAN_IPCONFIG_METHOD_AUTO)
1739                         disable_ipv6(ipdevice->config_ipv6);
1740 #endif
1741                 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1742                 __connman_ipconfig_unref(ipdevice->config_ipv6);
1743                 ipdevice->config_ipv6 = NULL;
1744                 return 0;
1745         }
1746
1747         return -EINVAL;
1748 }
1749
1750 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1751 {
1752         switch (method) {
1753         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1754                 break;
1755         case CONNMAN_IPCONFIG_METHOD_OFF:
1756                 return "off";
1757         case CONNMAN_IPCONFIG_METHOD_FIXED:
1758                 return "fixed";
1759         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1760                 return "manual";
1761         case CONNMAN_IPCONFIG_METHOD_DHCP:
1762                 return "dhcp";
1763         case CONNMAN_IPCONFIG_METHOD_AUTO:
1764                 return "auto";
1765         }
1766
1767         return NULL;
1768 }
1769
1770 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1771 {
1772         if (g_strcmp0(method, "off") == 0)
1773                 return CONNMAN_IPCONFIG_METHOD_OFF;
1774         else if (g_strcmp0(method, "fixed") == 0)
1775                 return CONNMAN_IPCONFIG_METHOD_FIXED;
1776         else if (g_strcmp0(method, "manual") == 0)
1777                 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1778         else if (g_strcmp0(method, "dhcp") == 0)
1779                 return CONNMAN_IPCONFIG_METHOD_DHCP;
1780         else if (g_strcmp0(method, "auto") == 0)
1781                 return CONNMAN_IPCONFIG_METHOD_AUTO;
1782         else
1783                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1784 }
1785
1786 static const char *privacy2string(int privacy)
1787 {
1788         if (privacy <= 0)
1789                 return "disabled";
1790         else if (privacy == 1)
1791                 return "enabled";
1792         else
1793                 return "prefered";
1794 }
1795
1796 static int string2privacy(const char *privacy)
1797 {
1798         if (g_strcmp0(privacy, "disabled") == 0)
1799                 return 0;
1800         else if (g_strcmp0(privacy, "enabled") == 0)
1801                 return 1;
1802         else if (g_strcmp0(privacy, "preferred") == 0)
1803                 return 2;
1804         else if (g_strcmp0(privacy, "prefered") == 0)
1805                 return 2;
1806         else
1807                 return 0;
1808 }
1809
1810 int __connman_ipconfig_ipv6_reset_privacy(struct connman_ipconfig *ipconfig)
1811 {
1812         struct connman_ipdevice *ipdevice;
1813         int err;
1814
1815         if (!ipconfig)
1816                 return -EINVAL;
1817
1818         ipdevice = g_hash_table_lookup(ipdevice_hash,
1819                                                 GINT_TO_POINTER(ipconfig->index));
1820         if (!ipdevice)
1821                 return -ENODEV;
1822
1823         err = __connman_ipconfig_ipv6_set_privacy(ipconfig, privacy2string(
1824                                                         ipdevice->ipv6_privacy));
1825
1826         return err;
1827 }
1828
1829 int __connman_ipconfig_ipv6_set_privacy(struct connman_ipconfig *ipconfig,
1830                                         const char *value)
1831 {
1832         int privacy;
1833
1834         if (!ipconfig)
1835                 return -EINVAL;
1836
1837         DBG("ipconfig %p privacy %s", ipconfig, value);
1838
1839         privacy = string2privacy(value);
1840
1841         ipconfig->ipv6_privacy_config = privacy;
1842
1843         enable_ipv6(ipconfig);
1844
1845         return 0;
1846 }
1847
1848 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1849                                                         DBusMessageIter *iter)
1850 {
1851         struct connman_ipaddress *append_addr = NULL;
1852         const char *str;
1853 #if defined TIZEN_EXT
1854         DBG("");
1855 #endif
1856
1857         if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1858                 return;
1859
1860         str = __connman_ipconfig_method2string(ipconfig->method);
1861         if (!str)
1862                 return;
1863
1864         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1865
1866         switch (ipconfig->method) {
1867         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1868         case CONNMAN_IPCONFIG_METHOD_OFF:
1869         case CONNMAN_IPCONFIG_METHOD_AUTO:
1870                 return;
1871
1872         case CONNMAN_IPCONFIG_METHOD_FIXED:
1873                 append_addr = ipconfig->address;
1874                 break;
1875
1876         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1877         case CONNMAN_IPCONFIG_METHOD_DHCP:
1878                 append_addr = ipconfig->system;
1879 #if defined TIZEN_EXT
1880                 /* TIZEN enables get_properties before __connman_ipconfig_newaddr */
1881                 if (append_addr && append_addr->local == NULL)
1882                         append_addr = ipconfig->address;
1883 #endif
1884                 break;
1885         }
1886
1887         if (!append_addr)
1888                 return;
1889
1890         if (append_addr->local) {
1891                 in_addr_t addr;
1892                 struct in_addr netmask;
1893                 char *mask;
1894
1895                 connman_dbus_dict_append_basic(iter, "Address",
1896                                 DBUS_TYPE_STRING, &append_addr->local);
1897
1898                 addr = 0xffffffff << (32 - append_addr->prefixlen);
1899                 netmask.s_addr = htonl(addr);
1900                 mask = inet_ntoa(netmask);
1901                 connman_dbus_dict_append_basic(iter, "Netmask",
1902                                                 DBUS_TYPE_STRING, &mask);
1903         }
1904
1905         if (append_addr->gateway)
1906                 connman_dbus_dict_append_basic(iter, "Gateway",
1907                                 DBUS_TYPE_STRING, &append_addr->gateway);
1908
1909 #if defined TIZEN_EXT
1910         if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_DHCP) {
1911                 char *server_ip;
1912                 server_ip = __connman_dhcp_get_server_address(ipconfig);
1913                 if (server_ip) {
1914                         connman_dbus_dict_append_basic(iter, "DHCPServerIP",
1915                                         DBUS_TYPE_STRING, &server_ip);
1916                         g_free(server_ip);
1917                 }
1918                 connman_dbus_dict_append_basic(iter, "DHCPLeaseDuration",
1919                                 DBUS_TYPE_INT32, &ipconfig->dhcp_lease_duration);
1920         }
1921 #endif
1922 }
1923
1924 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1925                                         DBusMessageIter *iter,
1926                                         struct connman_ipconfig *ipconfig_ipv4)
1927 {
1928         struct connman_ipaddress *append_addr = NULL;
1929         const char *str, *privacy;
1930 #if defined TIZEN_EXT
1931         DBG("");
1932 #endif
1933
1934         if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1935                 return;
1936
1937         str = __connman_ipconfig_method2string(ipconfig->method);
1938         if (!str)
1939                 return;
1940
1941         if (ipconfig_ipv4 &&
1942                         ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1943                 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1944                         str = "6to4";
1945         }
1946
1947         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1948
1949         switch (ipconfig->method) {
1950         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1951         case CONNMAN_IPCONFIG_METHOD_OFF:
1952                 return;
1953
1954         case CONNMAN_IPCONFIG_METHOD_FIXED:
1955                 append_addr = ipconfig->address;
1956                 break;
1957
1958         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1959         case CONNMAN_IPCONFIG_METHOD_DHCP:
1960         case CONNMAN_IPCONFIG_METHOD_AUTO:
1961                 append_addr = ipconfig->system;
1962 #if defined TIZEN_EXT
1963                 /* TIZEN enables get_properties before __connman_ipconfig_newaddr */
1964                 if (append_addr && append_addr->local == NULL)
1965                         append_addr = ipconfig->address;
1966 #endif
1967                 break;
1968         }
1969
1970         if (!append_addr)
1971                 return;
1972
1973         if (append_addr->local) {
1974                 connman_dbus_dict_append_basic(iter, "Address",
1975                                 DBUS_TYPE_STRING, &append_addr->local);
1976                 connman_dbus_dict_append_basic(iter, "PrefixLength",
1977                                                 DBUS_TYPE_BYTE,
1978                                                 &append_addr->prefixlen);
1979         }
1980
1981         if (append_addr->gateway)
1982                 connman_dbus_dict_append_basic(iter, "Gateway",
1983                                 DBUS_TYPE_STRING, &append_addr->gateway);
1984
1985         privacy = privacy2string(ipconfig->ipv6_privacy_config);
1986         connman_dbus_dict_append_basic(iter, "Privacy",
1987                                 DBUS_TYPE_STRING, &privacy);
1988 }
1989
1990 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1991                                                         DBusMessageIter *iter)
1992 {
1993         const char *str, *privacy;
1994 #if defined TIZEN_EXT
1995         DBG("");
1996 #endif
1997
1998         str = __connman_ipconfig_method2string(ipconfig->method);
1999         if (!str)
2000                 return;
2001
2002         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
2003
2004         switch (ipconfig->method) {
2005         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2006         case CONNMAN_IPCONFIG_METHOD_OFF:
2007         case CONNMAN_IPCONFIG_METHOD_DHCP:
2008                 return;
2009         case CONNMAN_IPCONFIG_METHOD_FIXED:
2010         case CONNMAN_IPCONFIG_METHOD_MANUAL:
2011         case CONNMAN_IPCONFIG_METHOD_AUTO:
2012                 break;
2013         }
2014
2015         if (!ipconfig->address)
2016                 return;
2017
2018         if (ipconfig->address->local) {
2019                 connman_dbus_dict_append_basic(iter, "Address",
2020                                 DBUS_TYPE_STRING, &ipconfig->address->local);
2021                 connman_dbus_dict_append_basic(iter, "PrefixLength",
2022                                                 DBUS_TYPE_BYTE,
2023                                                 &ipconfig->address->prefixlen);
2024         }
2025
2026         if (ipconfig->address->gateway)
2027                 connman_dbus_dict_append_basic(iter, "Gateway",
2028                                 DBUS_TYPE_STRING, &ipconfig->address->gateway);
2029
2030         privacy = privacy2string(ipconfig->ipv6_privacy_config);
2031         connman_dbus_dict_append_basic(iter, "Privacy",
2032                                 DBUS_TYPE_STRING, &privacy);
2033 }
2034
2035 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
2036                                                         DBusMessageIter *iter)
2037 {
2038         const char *str;
2039 #if defined TIZEN_EXT
2040         DBG("");
2041 #endif
2042
2043         str = __connman_ipconfig_method2string(ipconfig->method);
2044         if (!str)
2045                 return;
2046
2047         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
2048
2049         switch (ipconfig->method) {
2050         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2051         case CONNMAN_IPCONFIG_METHOD_OFF:
2052         case CONNMAN_IPCONFIG_METHOD_DHCP:
2053         case CONNMAN_IPCONFIG_METHOD_AUTO:
2054                 return;
2055         case CONNMAN_IPCONFIG_METHOD_FIXED:
2056         case CONNMAN_IPCONFIG_METHOD_MANUAL:
2057                 break;
2058         }
2059
2060         if (!ipconfig->address)
2061                 return;
2062
2063         if (ipconfig->address->local) {
2064                 in_addr_t addr;
2065                 struct in_addr netmask;
2066                 char *mask;
2067
2068                 connman_dbus_dict_append_basic(iter, "Address",
2069                                 DBUS_TYPE_STRING, &ipconfig->address->local);
2070
2071                 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
2072                 netmask.s_addr = htonl(addr);
2073                 mask = inet_ntoa(netmask);
2074                 connman_dbus_dict_append_basic(iter, "Netmask",
2075                                                 DBUS_TYPE_STRING, &mask);
2076         }
2077
2078         if (ipconfig->address->gateway)
2079                 connman_dbus_dict_append_basic(iter, "Gateway",
2080                                 DBUS_TYPE_STRING, &ipconfig->address->gateway);
2081 }
2082
2083 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
2084                                                         DBusMessageIter *array)
2085 {
2086         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2087         const char *address = NULL, *netmask = NULL, *gateway = NULL,
2088                 *privacy_string = NULL;
2089         int prefix_length = 0, privacy = 0;
2090         DBusMessageIter dict;
2091         int type = -1;
2092
2093         DBG("ipconfig %p", ipconfig);
2094
2095         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
2096                 return -EINVAL;
2097
2098         dbus_message_iter_recurse(array, &dict);
2099
2100         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2101                 DBusMessageIter entry, value;
2102                 const char *key;
2103                 int type;
2104
2105                 dbus_message_iter_recurse(&dict, &entry);
2106
2107                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2108                         return -EINVAL;
2109
2110                 dbus_message_iter_get_basic(&entry, &key);
2111                 dbus_message_iter_next(&entry);
2112
2113                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
2114                         return -EINVAL;
2115
2116                 dbus_message_iter_recurse(&entry, &value);
2117
2118                 type = dbus_message_iter_get_arg_type(&value);
2119
2120                 if (g_str_equal(key, "Method")) {
2121                         const char *str;
2122
2123                         if (type != DBUS_TYPE_STRING)
2124                                 return -EINVAL;
2125
2126                         dbus_message_iter_get_basic(&value, &str);
2127                         method = __connman_ipconfig_string2method(str);
2128                 } else if (g_str_equal(key, "Address")) {
2129                         if (type != DBUS_TYPE_STRING)
2130                                 return -EINVAL;
2131
2132                         dbus_message_iter_get_basic(&value, &address);
2133                 } else if (g_str_equal(key, "PrefixLength")) {
2134                         if (type != DBUS_TYPE_BYTE)
2135                                 return -EINVAL;
2136
2137                         dbus_message_iter_get_basic(&value, &prefix_length);
2138
2139                         if (prefix_length < 0 || prefix_length > 128)
2140                                 return -EINVAL;
2141                 } else if (g_str_equal(key, "Netmask")) {
2142                         if (type != DBUS_TYPE_STRING)
2143                                 return -EINVAL;
2144
2145                         dbus_message_iter_get_basic(&value, &netmask);
2146                 } else if (g_str_equal(key, "Gateway")) {
2147                         if (type != DBUS_TYPE_STRING)
2148                                 return -EINVAL;
2149
2150                         dbus_message_iter_get_basic(&value, &gateway);
2151                 } else if (g_str_equal(key, "Privacy")) {
2152                         if (type != DBUS_TYPE_STRING)
2153                                 return -EINVAL;
2154
2155                         dbus_message_iter_get_basic(&value, &privacy_string);
2156                         privacy = string2privacy(privacy_string);
2157                 }
2158
2159                 dbus_message_iter_next(&dict);
2160         }
2161
2162         DBG("method %d address %s netmask %s gateway %s prefix_length %d "
2163                 "privacy %s",
2164                 method, address, netmask, gateway, prefix_length,
2165                 privacy_string);
2166
2167         switch (method) {
2168         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2169         case CONNMAN_IPCONFIG_METHOD_FIXED:
2170                 return -EINVAL;
2171
2172         case CONNMAN_IPCONFIG_METHOD_OFF:
2173                 ipconfig->method = method;
2174 #if defined TIZEN_EXT
2175                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2176                         disable_ipv6(ipconfig);
2177 #endif
2178                 break;
2179
2180         case CONNMAN_IPCONFIG_METHOD_AUTO:
2181                 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2182                         return -EOPNOTSUPP;
2183
2184                 ipconfig->method = method;
2185                 if (privacy_string)
2186                         ipconfig->ipv6_privacy_config = privacy;
2187 #if defined TIZEN_EXT
2188                 enable_ipv6(ipconfig);
2189 #endif
2190                 break;
2191
2192         case CONNMAN_IPCONFIG_METHOD_MANUAL:
2193                 switch (ipconfig->type) {
2194                 case CONNMAN_IPCONFIG_TYPE_IPV4:
2195                         type = AF_INET;
2196                         break;
2197                 case CONNMAN_IPCONFIG_TYPE_IPV6:
2198                         type = AF_INET6;
2199                         break;
2200                 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2201                 case CONNMAN_IPCONFIG_TYPE_ALL:
2202                         type = -1;
2203                         break;
2204                 }
2205
2206                 if ((address && connman_inet_check_ipaddress(address)
2207                                                 != type) ||
2208                                 (netmask &&
2209                                 connman_inet_check_ipaddress(netmask)
2210                                                 != type) ||
2211                                 (gateway &&
2212                                 connman_inet_check_ipaddress(gateway)
2213                                                 != type))
2214                         return -EINVAL;
2215
2216                 ipconfig->method = method;
2217
2218                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2219                         connman_ipaddress_set_ipv4(ipconfig->address,
2220                                                 address, netmask, gateway);
2221                 else
2222                         return connman_ipaddress_set_ipv6(
2223                                         ipconfig->address, address,
2224                                                 prefix_length, gateway);
2225
2226                 break;
2227
2228         case CONNMAN_IPCONFIG_METHOD_DHCP:
2229                 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
2230                         return -EOPNOTSUPP;
2231
2232                 ipconfig->method = method;
2233                 break;
2234         }
2235
2236         return 0;
2237 }
2238
2239 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2240                                                         DBusMessageIter *iter)
2241 {
2242         struct connman_ipdevice *ipdevice;
2243         const char *method = "auto";
2244
2245         connman_dbus_dict_append_basic(iter, "Method",
2246                                                 DBUS_TYPE_STRING, &method);
2247
2248         ipdevice = g_hash_table_lookup(ipdevice_hash,
2249                                         GINT_TO_POINTER(ipconfig->index));
2250         if (!ipdevice)
2251                 return;
2252
2253         if (ipconfig->index >= 0) {
2254                 char *ifname = connman_inet_ifname(ipconfig->index);
2255                 if (ifname) {
2256                         connman_dbus_dict_append_basic(iter, "Interface",
2257                                                 DBUS_TYPE_STRING, &ifname);
2258                         g_free(ifname);
2259                 }
2260         }
2261
2262         if (ipdevice->address)
2263                 connman_dbus_dict_append_basic(iter, "Address",
2264                                         DBUS_TYPE_STRING, &ipdevice->address);
2265
2266         if (ipdevice->mtu > 0)
2267                 connman_dbus_dict_append_basic(iter, "MTU",
2268                                         DBUS_TYPE_UINT16, &ipdevice->mtu);
2269 }
2270
2271 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2272                 GKeyFile *keyfile, const char *identifier, const char *prefix)
2273 {
2274         char *method;
2275         char *key;
2276         char *str;
2277
2278         DBG("ipconfig %p identifier %s", ipconfig, identifier);
2279
2280         key = g_strdup_printf("%smethod", prefix);
2281         method = g_key_file_get_string(keyfile, identifier, key, NULL);
2282         if (!method) {
2283                 switch (ipconfig->type) {
2284                 case CONNMAN_IPCONFIG_TYPE_IPV4:
2285                         ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2286                         break;
2287                 case CONNMAN_IPCONFIG_TYPE_IPV6:
2288                         ipconfig->method = CONNMAN_IPCONFIG_METHOD_AUTO;
2289                         break;
2290                 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2291                 case CONNMAN_IPCONFIG_TYPE_ALL:
2292                         ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2293                         break;
2294                 }
2295         } else
2296                 ipconfig->method = __connman_ipconfig_string2method(method);
2297
2298         if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2299                 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2300
2301         if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2302                 gsize length;
2303                 char *pprefix;
2304
2305                 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2306                         ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2307                         char *privacy;
2308
2309                         pprefix = g_strdup_printf("%sprivacy", prefix);
2310                         privacy = g_key_file_get_string(keyfile, identifier,
2311                                                         pprefix, NULL);
2312                         ipconfig->ipv6_privacy_config = string2privacy(privacy);
2313                         g_free(pprefix);
2314                         g_free(privacy);
2315                 }
2316
2317                 pprefix = g_strdup_printf("%sDHCP.LastPrefixes", prefix);
2318                 ipconfig->last_dhcpv6_prefixes =
2319                         g_key_file_get_string_list(keyfile, identifier, pprefix,
2320                                                 &length, NULL);
2321                 if (ipconfig->last_dhcpv6_prefixes && length == 0) {
2322                         g_free(ipconfig->last_dhcpv6_prefixes);
2323                         ipconfig->last_dhcpv6_prefixes = NULL;
2324                 }
2325                 g_free(pprefix);
2326         }
2327
2328         g_free(method);
2329         g_free(key);
2330
2331         switch (ipconfig->method) {
2332         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2333         case CONNMAN_IPCONFIG_METHOD_OFF:
2334                 break;
2335
2336         case CONNMAN_IPCONFIG_METHOD_FIXED:
2337         case CONNMAN_IPCONFIG_METHOD_MANUAL:
2338
2339                 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2340                 ipconfig->address->prefixlen = g_key_file_get_integer(
2341                                 keyfile, identifier, key, NULL);
2342                 g_free(key);
2343
2344                 key = g_strdup_printf("%slocal_address", prefix);
2345                 g_free(ipconfig->address->local);
2346                 ipconfig->address->local = g_key_file_get_string(
2347                         keyfile, identifier, key, NULL);
2348                 g_free(key);
2349
2350                 key = g_strdup_printf("%speer_address", prefix);
2351                 g_free(ipconfig->address->peer);
2352                 ipconfig->address->peer = g_key_file_get_string(
2353                                 keyfile, identifier, key, NULL);
2354                 g_free(key);
2355
2356                 key = g_strdup_printf("%sbroadcast_address", prefix);
2357                 g_free(ipconfig->address->broadcast);
2358                 ipconfig->address->broadcast = g_key_file_get_string(
2359                                 keyfile, identifier, key, NULL);
2360                 g_free(key);
2361
2362                 key = g_strdup_printf("%sgateway", prefix);
2363                 g_free(ipconfig->address->gateway);
2364                 ipconfig->address->gateway = g_key_file_get_string(
2365                                 keyfile, identifier, key, NULL);
2366                 g_free(key);
2367                 break;
2368
2369         case CONNMAN_IPCONFIG_METHOD_DHCP:
2370
2371                 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2372                 str = g_key_file_get_string(keyfile, identifier, key, NULL);
2373                 if (str) {
2374                         g_free(ipconfig->last_dhcp_address);
2375                         ipconfig->last_dhcp_address = str;
2376                 }
2377                 g_free(key);
2378
2379                 break;
2380
2381         case CONNMAN_IPCONFIG_METHOD_AUTO:
2382                 break;
2383         }
2384
2385         return 0;
2386 }
2387
2388 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2389                 GKeyFile *keyfile, const char *identifier, const char *prefix)
2390 {
2391         const char *method;
2392         char *key;
2393
2394         method = __connman_ipconfig_method2string(ipconfig->method);
2395
2396         DBG("ipconfig %p identifier %s method %s", ipconfig, identifier,
2397                                                                 method);
2398         if (method) {
2399                 key = g_strdup_printf("%smethod", prefix);
2400                 g_key_file_set_string(keyfile, identifier, key, method);
2401                 g_free(key);
2402         }
2403
2404         if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2405                 const char *privacy;
2406                 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2407                 key = g_strdup_printf("%sprivacy", prefix);
2408                 g_key_file_set_string(keyfile, identifier, key, privacy);
2409                 g_free(key);
2410
2411                 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2412                 if (ipconfig->last_dhcp_address &&
2413                                 strlen(ipconfig->last_dhcp_address) > 0)
2414                         g_key_file_set_string(keyfile, identifier, key,
2415                                         ipconfig->last_dhcp_address);
2416                 else
2417                         g_key_file_remove_key(keyfile, identifier, key, NULL);
2418                 g_free(key);
2419
2420                 key = g_strdup_printf("%sDHCP.LastPrefixes", prefix);
2421                 if (ipconfig->last_dhcpv6_prefixes &&
2422                                 ipconfig->last_dhcpv6_prefixes[0]) {
2423                         guint len =
2424                                 g_strv_length(ipconfig->last_dhcpv6_prefixes);
2425
2426                         g_key_file_set_string_list(keyfile, identifier, key,
2427                                 (const gchar **)ipconfig->last_dhcpv6_prefixes,
2428                                                 len);
2429                 } else
2430                         g_key_file_remove_key(keyfile, identifier, key, NULL);
2431                 g_free(key);
2432         }
2433
2434         switch (ipconfig->method) {
2435         case CONNMAN_IPCONFIG_METHOD_FIXED:
2436         case CONNMAN_IPCONFIG_METHOD_MANUAL:
2437                 break;
2438         case CONNMAN_IPCONFIG_METHOD_DHCP:
2439                 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2440                 if (ipconfig->last_dhcp_address &&
2441                                 strlen(ipconfig->last_dhcp_address) > 0)
2442                         g_key_file_set_string(keyfile, identifier, key,
2443                                         ipconfig->last_dhcp_address);
2444                 else
2445                         g_key_file_remove_key(keyfile, identifier, key, NULL);
2446                 g_free(key);
2447                 /* fall through */
2448         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2449         case CONNMAN_IPCONFIG_METHOD_OFF:
2450         case CONNMAN_IPCONFIG_METHOD_AUTO:
2451                 return 0;
2452         }
2453
2454         key = g_strdup_printf("%snetmask_prefixlen", prefix);
2455         if (ipconfig->address->prefixlen != 0)
2456                 g_key_file_set_integer(keyfile, identifier,
2457                                 key, ipconfig->address->prefixlen);
2458         g_free(key);
2459
2460         key = g_strdup_printf("%slocal_address", prefix);
2461         if (ipconfig->address->local)
2462                 g_key_file_set_string(keyfile, identifier,
2463                                 key, ipconfig->address->local);
2464         g_free(key);
2465
2466         key = g_strdup_printf("%speer_address", prefix);
2467         if (ipconfig->address->peer)
2468                 g_key_file_set_string(keyfile, identifier,
2469                                 key, ipconfig->address->peer);
2470         g_free(key);
2471
2472         key = g_strdup_printf("%sbroadcast_address", prefix);
2473         if (ipconfig->address->broadcast)
2474                 g_key_file_set_string(keyfile, identifier,
2475                         key, ipconfig->address->broadcast);
2476         g_free(key);
2477
2478         key = g_strdup_printf("%sgateway", prefix);
2479         if (ipconfig->address->gateway)
2480                 g_key_file_set_string(keyfile, identifier,
2481                         key, ipconfig->address->gateway);
2482         g_free(key);
2483
2484         return 0;
2485 }
2486
2487 int __connman_ipconfig_init(void)
2488 {
2489         DBG("");
2490
2491         ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2492                                                         NULL, free_ipdevice);
2493
2494         is_ipv6_supported = connman_inet_is_ipv6_supported();
2495
2496         return 0;
2497 }
2498
2499 void __connman_ipconfig_cleanup(void)
2500 {
2501         DBG("");
2502
2503         g_hash_table_destroy(ipdevice_hash);
2504         ipdevice_hash = NULL;
2505 }