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