Removed unnecessary logs
[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         if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1843                 return;
1844
1845         str = __connman_ipconfig_method2string(ipconfig->method);
1846         if (!str)
1847                 return;
1848
1849         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1850
1851         switch (ipconfig->method) {
1852         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1853         case CONNMAN_IPCONFIG_METHOD_OFF:
1854         case CONNMAN_IPCONFIG_METHOD_AUTO:
1855                 return;
1856
1857         case CONNMAN_IPCONFIG_METHOD_FIXED:
1858                 append_addr = ipconfig->address;
1859                 break;
1860
1861         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1862         case CONNMAN_IPCONFIG_METHOD_DHCP:
1863                 append_addr = ipconfig->system;
1864 #if defined TIZEN_EXT
1865                 /* TIZEN enables get_properties before __connman_ipconfig_newaddr */
1866                 if (append_addr && append_addr->local == NULL)
1867                         append_addr = ipconfig->address;
1868 #endif
1869                 break;
1870         }
1871
1872         if (!append_addr)
1873                 return;
1874
1875         if (append_addr->local) {
1876                 in_addr_t addr;
1877                 struct in_addr netmask;
1878                 char *mask;
1879
1880                 connman_dbus_dict_append_basic(iter, "Address",
1881                                 DBUS_TYPE_STRING, &append_addr->local);
1882
1883                 addr = 0xffffffff << (32 - append_addr->prefixlen);
1884                 netmask.s_addr = htonl(addr);
1885                 mask = inet_ntoa(netmask);
1886                 connman_dbus_dict_append_basic(iter, "Netmask",
1887                                                 DBUS_TYPE_STRING, &mask);
1888         }
1889
1890         if (append_addr->gateway)
1891                 connman_dbus_dict_append_basic(iter, "Gateway",
1892                                 DBUS_TYPE_STRING, &append_addr->gateway);
1893 }
1894
1895 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1896                                         DBusMessageIter *iter,
1897                                         struct connman_ipconfig *ipconfig_ipv4)
1898 {
1899         struct connman_ipaddress *append_addr = NULL;
1900         const char *str, *privacy;
1901
1902         if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1903                 return;
1904
1905         str = __connman_ipconfig_method2string(ipconfig->method);
1906         if (!str)
1907                 return;
1908
1909         if (ipconfig_ipv4 &&
1910                         ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1911                 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1912                         str = "6to4";
1913         }
1914
1915         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1916
1917         switch (ipconfig->method) {
1918         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1919         case CONNMAN_IPCONFIG_METHOD_OFF:
1920                 return;
1921
1922         case CONNMAN_IPCONFIG_METHOD_FIXED:
1923                 append_addr = ipconfig->address;
1924                 break;
1925
1926         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1927         case CONNMAN_IPCONFIG_METHOD_DHCP:
1928         case CONNMAN_IPCONFIG_METHOD_AUTO:
1929                 append_addr = ipconfig->system;
1930 #if defined TIZEN_EXT
1931                 /* TIZEN enables get_properties before __connman_ipconfig_newaddr */
1932                 if (append_addr && append_addr->local == NULL)
1933                         append_addr = ipconfig->address;
1934 #endif
1935                 break;
1936         }
1937
1938         if (!append_addr)
1939                 return;
1940
1941         if (append_addr->local) {
1942                 connman_dbus_dict_append_basic(iter, "Address",
1943                                 DBUS_TYPE_STRING, &append_addr->local);
1944                 connman_dbus_dict_append_basic(iter, "PrefixLength",
1945                                                 DBUS_TYPE_BYTE,
1946                                                 &append_addr->prefixlen);
1947         }
1948
1949         if (append_addr->gateway)
1950                 connman_dbus_dict_append_basic(iter, "Gateway",
1951                                 DBUS_TYPE_STRING, &append_addr->gateway);
1952
1953         privacy = privacy2string(ipconfig->ipv6_privacy_config);
1954         connman_dbus_dict_append_basic(iter, "Privacy",
1955                                 DBUS_TYPE_STRING, &privacy);
1956 }
1957
1958 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1959                                                         DBusMessageIter *iter)
1960 {
1961         const char *str, *privacy;
1962
1963         str = __connman_ipconfig_method2string(ipconfig->method);
1964         if (!str)
1965                 return;
1966
1967         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1968
1969         switch (ipconfig->method) {
1970         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1971         case CONNMAN_IPCONFIG_METHOD_OFF:
1972         case CONNMAN_IPCONFIG_METHOD_DHCP:
1973                 return;
1974         case CONNMAN_IPCONFIG_METHOD_FIXED:
1975         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1976         case CONNMAN_IPCONFIG_METHOD_AUTO:
1977                 break;
1978         }
1979
1980         if (!ipconfig->address)
1981                 return;
1982
1983         if (ipconfig->address->local) {
1984                 connman_dbus_dict_append_basic(iter, "Address",
1985                                 DBUS_TYPE_STRING, &ipconfig->address->local);
1986                 connman_dbus_dict_append_basic(iter, "PrefixLength",
1987                                                 DBUS_TYPE_BYTE,
1988                                                 &ipconfig->address->prefixlen);
1989         }
1990
1991         if (ipconfig->address->gateway)
1992                 connman_dbus_dict_append_basic(iter, "Gateway",
1993                                 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1994
1995         privacy = privacy2string(ipconfig->ipv6_privacy_config);
1996         connman_dbus_dict_append_basic(iter, "Privacy",
1997                                 DBUS_TYPE_STRING, &privacy);
1998 }
1999
2000 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
2001                                                         DBusMessageIter *iter)
2002 {
2003         const char *str;
2004
2005         str = __connman_ipconfig_method2string(ipconfig->method);
2006         if (!str)
2007                 return;
2008
2009         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
2010
2011         switch (ipconfig->method) {
2012         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2013         case CONNMAN_IPCONFIG_METHOD_OFF:
2014         case CONNMAN_IPCONFIG_METHOD_DHCP:
2015         case CONNMAN_IPCONFIG_METHOD_AUTO:
2016                 return;
2017         case CONNMAN_IPCONFIG_METHOD_FIXED:
2018         case CONNMAN_IPCONFIG_METHOD_MANUAL:
2019                 break;
2020         }
2021
2022         if (!ipconfig->address)
2023                 return;
2024
2025         if (ipconfig->address->local) {
2026                 in_addr_t addr;
2027                 struct in_addr netmask;
2028                 char *mask;
2029
2030                 connman_dbus_dict_append_basic(iter, "Address",
2031                                 DBUS_TYPE_STRING, &ipconfig->address->local);
2032
2033                 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
2034                 netmask.s_addr = htonl(addr);
2035                 mask = inet_ntoa(netmask);
2036                 connman_dbus_dict_append_basic(iter, "Netmask",
2037                                                 DBUS_TYPE_STRING, &mask);
2038         }
2039
2040         if (ipconfig->address->gateway)
2041                 connman_dbus_dict_append_basic(iter, "Gateway",
2042                                 DBUS_TYPE_STRING, &ipconfig->address->gateway);
2043 }
2044
2045 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
2046                                                         DBusMessageIter *array)
2047 {
2048         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2049         const char *address = NULL, *netmask = NULL, *gateway = NULL,
2050                 *privacy_string = NULL;
2051         int prefix_length = 0, privacy = 0;
2052         DBusMessageIter dict;
2053         int type = -1;
2054
2055         DBG("ipconfig %p", ipconfig);
2056
2057         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
2058                 return -EINVAL;
2059
2060         dbus_message_iter_recurse(array, &dict);
2061
2062         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2063                 DBusMessageIter entry, value;
2064                 const char *key;
2065                 int type;
2066
2067                 dbus_message_iter_recurse(&dict, &entry);
2068
2069                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2070                         return -EINVAL;
2071
2072                 dbus_message_iter_get_basic(&entry, &key);
2073                 dbus_message_iter_next(&entry);
2074
2075                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
2076                         return -EINVAL;
2077
2078                 dbus_message_iter_recurse(&entry, &value);
2079
2080                 type = dbus_message_iter_get_arg_type(&value);
2081
2082                 if (g_str_equal(key, "Method")) {
2083                         const char *str;
2084
2085                         if (type != DBUS_TYPE_STRING)
2086                                 return -EINVAL;
2087
2088                         dbus_message_iter_get_basic(&value, &str);
2089                         method = __connman_ipconfig_string2method(str);
2090                 } else if (g_str_equal(key, "Address")) {
2091                         if (type != DBUS_TYPE_STRING)
2092                                 return -EINVAL;
2093
2094                         dbus_message_iter_get_basic(&value, &address);
2095                 } else if (g_str_equal(key, "PrefixLength")) {
2096                         if (type != DBUS_TYPE_BYTE)
2097                                 return -EINVAL;
2098
2099                         dbus_message_iter_get_basic(&value, &prefix_length);
2100
2101                         if (prefix_length < 0 || prefix_length > 128)
2102                                 return -EINVAL;
2103                 } else if (g_str_equal(key, "Netmask")) {
2104                         if (type != DBUS_TYPE_STRING)
2105                                 return -EINVAL;
2106
2107                         dbus_message_iter_get_basic(&value, &netmask);
2108                 } else if (g_str_equal(key, "Gateway")) {
2109                         if (type != DBUS_TYPE_STRING)
2110                                 return -EINVAL;
2111
2112                         dbus_message_iter_get_basic(&value, &gateway);
2113                 } else if (g_str_equal(key, "Privacy")) {
2114                         if (type != DBUS_TYPE_STRING)
2115                                 return -EINVAL;
2116
2117                         dbus_message_iter_get_basic(&value, &privacy_string);
2118                         privacy = string2privacy(privacy_string);
2119                 }
2120
2121                 dbus_message_iter_next(&dict);
2122         }
2123
2124         DBG("method %d address %s netmask %s gateway %s prefix_length %d "
2125                 "privacy %s",
2126                 method, address, netmask, gateway, prefix_length,
2127                 privacy_string);
2128
2129         switch (method) {
2130         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2131         case CONNMAN_IPCONFIG_METHOD_FIXED:
2132                 return -EINVAL;
2133
2134         case CONNMAN_IPCONFIG_METHOD_OFF:
2135                 ipconfig->method = method;
2136 #if defined TIZEN_EXT
2137                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2138                         disable_ipv6(ipconfig);
2139 #endif
2140                 break;
2141
2142         case CONNMAN_IPCONFIG_METHOD_AUTO:
2143                 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2144                         return -EOPNOTSUPP;
2145
2146                 ipconfig->method = method;
2147                 if (privacy_string)
2148                         ipconfig->ipv6_privacy_config = privacy;
2149 #if defined TIZEN_EXT
2150                 enable_ipv6(ipconfig);
2151 #endif
2152                 break;
2153
2154         case CONNMAN_IPCONFIG_METHOD_MANUAL:
2155                 switch (ipconfig->type) {
2156                 case CONNMAN_IPCONFIG_TYPE_IPV4:
2157                         type = AF_INET;
2158                         break;
2159                 case CONNMAN_IPCONFIG_TYPE_IPV6:
2160                         type = AF_INET6;
2161                         break;
2162                 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2163                 case CONNMAN_IPCONFIG_TYPE_ALL:
2164                         type = -1;
2165                         break;
2166                 }
2167
2168                 if ((address && connman_inet_check_ipaddress(address)
2169                                                 != type) ||
2170                                 (netmask &&
2171                                 connman_inet_check_ipaddress(netmask)
2172                                                 != type) ||
2173                                 (gateway &&
2174                                 connman_inet_check_ipaddress(gateway)
2175                                                 != type))
2176                         return -EINVAL;
2177
2178                 ipconfig->method = method;
2179
2180                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2181                         connman_ipaddress_set_ipv4(ipconfig->address,
2182                                                 address, netmask, gateway);
2183                 else
2184                         return connman_ipaddress_set_ipv6(
2185                                         ipconfig->address, address,
2186                                                 prefix_length, gateway);
2187
2188                 break;
2189
2190         case CONNMAN_IPCONFIG_METHOD_DHCP:
2191                 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
2192                         return -EOPNOTSUPP;
2193
2194                 ipconfig->method = method;
2195                 break;
2196         }
2197
2198         return 0;
2199 }
2200
2201 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2202                                                         DBusMessageIter *iter)
2203 {
2204         struct connman_ipdevice *ipdevice;
2205         const char *method = "auto";
2206
2207         connman_dbus_dict_append_basic(iter, "Method",
2208                                                 DBUS_TYPE_STRING, &method);
2209
2210         ipdevice = g_hash_table_lookup(ipdevice_hash,
2211                                         GINT_TO_POINTER(ipconfig->index));
2212         if (!ipdevice)
2213                 return;
2214
2215         if (ipconfig->index >= 0) {
2216                 char *ifname = connman_inet_ifname(ipconfig->index);
2217                 if (ifname) {
2218                         connman_dbus_dict_append_basic(iter, "Interface",
2219                                                 DBUS_TYPE_STRING, &ifname);
2220                         g_free(ifname);
2221                 }
2222         }
2223
2224         if (ipdevice->address)
2225                 connman_dbus_dict_append_basic(iter, "Address",
2226                                         DBUS_TYPE_STRING, &ipdevice->address);
2227
2228         if (ipdevice->mtu > 0)
2229                 connman_dbus_dict_append_basic(iter, "MTU",
2230                                         DBUS_TYPE_UINT16, &ipdevice->mtu);
2231 }
2232
2233 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2234                 GKeyFile *keyfile, const char *identifier, const char *prefix)
2235 {
2236         char *method;
2237         char *key;
2238         char *str;
2239
2240         DBG("ipconfig %p identifier %s", ipconfig, identifier);
2241
2242         key = g_strdup_printf("%smethod", prefix);
2243         method = g_key_file_get_string(keyfile, identifier, key, NULL);
2244         if (!method) {
2245                 switch (ipconfig->type) {
2246                 case CONNMAN_IPCONFIG_TYPE_IPV4:
2247                         ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2248                         break;
2249                 case CONNMAN_IPCONFIG_TYPE_IPV6:
2250                         ipconfig->method = CONNMAN_IPCONFIG_METHOD_AUTO;
2251                         break;
2252                 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2253                 case CONNMAN_IPCONFIG_TYPE_ALL:
2254                         ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2255                         break;
2256                 }
2257         } else
2258                 ipconfig->method = __connman_ipconfig_string2method(method);
2259
2260         if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2261                 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2262
2263         if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2264                 gsize length;
2265                 char *pprefix;
2266
2267                 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2268                         ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2269                         char *privacy;
2270
2271                         pprefix = g_strdup_printf("%sprivacy", prefix);
2272                         privacy = g_key_file_get_string(keyfile, identifier,
2273                                                         pprefix, NULL);
2274                         ipconfig->ipv6_privacy_config = string2privacy(privacy);
2275                         g_free(pprefix);
2276                         g_free(privacy);
2277                 }
2278
2279                 pprefix = g_strdup_printf("%sDHCP.LastPrefixes", prefix);
2280                 ipconfig->last_dhcpv6_prefixes =
2281                         g_key_file_get_string_list(keyfile, identifier, pprefix,
2282                                                 &length, NULL);
2283                 if (ipconfig->last_dhcpv6_prefixes && length == 0) {
2284                         g_free(ipconfig->last_dhcpv6_prefixes);
2285                         ipconfig->last_dhcpv6_prefixes = NULL;
2286                 }
2287                 g_free(pprefix);
2288         }
2289
2290         g_free(method);
2291         g_free(key);
2292
2293         switch (ipconfig->method) {
2294         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2295         case CONNMAN_IPCONFIG_METHOD_OFF:
2296                 break;
2297
2298         case CONNMAN_IPCONFIG_METHOD_FIXED:
2299         case CONNMAN_IPCONFIG_METHOD_MANUAL:
2300
2301                 key = g_strdup_printf("%snetmask_prefixlen", prefix);
2302                 ipconfig->address->prefixlen = g_key_file_get_integer(
2303                                 keyfile, identifier, key, NULL);
2304                 g_free(key);
2305
2306                 key = g_strdup_printf("%slocal_address", prefix);
2307                 g_free(ipconfig->address->local);
2308                 ipconfig->address->local = g_key_file_get_string(
2309                         keyfile, identifier, key, NULL);
2310                 g_free(key);
2311
2312                 key = g_strdup_printf("%speer_address", prefix);
2313                 g_free(ipconfig->address->peer);
2314                 ipconfig->address->peer = g_key_file_get_string(
2315                                 keyfile, identifier, key, NULL);
2316                 g_free(key);
2317
2318                 key = g_strdup_printf("%sbroadcast_address", prefix);
2319                 g_free(ipconfig->address->broadcast);
2320                 ipconfig->address->broadcast = g_key_file_get_string(
2321                                 keyfile, identifier, key, NULL);
2322                 g_free(key);
2323
2324                 key = g_strdup_printf("%sgateway", prefix);
2325                 g_free(ipconfig->address->gateway);
2326                 ipconfig->address->gateway = g_key_file_get_string(
2327                                 keyfile, identifier, key, NULL);
2328                 g_free(key);
2329                 break;
2330
2331         case CONNMAN_IPCONFIG_METHOD_DHCP:
2332
2333                 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2334                 str = g_key_file_get_string(keyfile, identifier, key, NULL);
2335                 if (str) {
2336                         g_free(ipconfig->last_dhcp_address);
2337                         ipconfig->last_dhcp_address = str;
2338                 }
2339                 g_free(key);
2340
2341                 break;
2342
2343         case CONNMAN_IPCONFIG_METHOD_AUTO:
2344                 break;
2345         }
2346
2347         return 0;
2348 }
2349
2350 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2351                 GKeyFile *keyfile, const char *identifier, const char *prefix)
2352 {
2353         const char *method;
2354         char *key;
2355
2356         method = __connman_ipconfig_method2string(ipconfig->method);
2357
2358         DBG("ipconfig %p identifier %s method %s", ipconfig, identifier,
2359                                                                 method);
2360         if (method) {
2361                 key = g_strdup_printf("%smethod", prefix);
2362                 g_key_file_set_string(keyfile, identifier, key, method);
2363                 g_free(key);
2364         }
2365
2366         if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2367                 const char *privacy;
2368                 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2369                 key = g_strdup_printf("%sprivacy", prefix);
2370                 g_key_file_set_string(keyfile, identifier, key, privacy);
2371                 g_free(key);
2372
2373                 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2374                 if (ipconfig->last_dhcp_address &&
2375                                 strlen(ipconfig->last_dhcp_address) > 0)
2376                         g_key_file_set_string(keyfile, identifier, key,
2377                                         ipconfig->last_dhcp_address);
2378                 else
2379                         g_key_file_remove_key(keyfile, identifier, key, NULL);
2380                 g_free(key);
2381
2382                 key = g_strdup_printf("%sDHCP.LastPrefixes", prefix);
2383                 if (ipconfig->last_dhcpv6_prefixes &&
2384                                 ipconfig->last_dhcpv6_prefixes[0]) {
2385                         guint len =
2386                                 g_strv_length(ipconfig->last_dhcpv6_prefixes);
2387
2388                         g_key_file_set_string_list(keyfile, identifier, key,
2389                                 (const gchar **)ipconfig->last_dhcpv6_prefixes,
2390                                                 len);
2391                 } else
2392                         g_key_file_remove_key(keyfile, identifier, key, NULL);
2393                 g_free(key);
2394         }
2395
2396         switch (ipconfig->method) {
2397         case CONNMAN_IPCONFIG_METHOD_FIXED:
2398         case CONNMAN_IPCONFIG_METHOD_MANUAL:
2399                 break;
2400         case CONNMAN_IPCONFIG_METHOD_DHCP:
2401                 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2402                 if (ipconfig->last_dhcp_address &&
2403                                 strlen(ipconfig->last_dhcp_address) > 0)
2404                         g_key_file_set_string(keyfile, identifier, key,
2405                                         ipconfig->last_dhcp_address);
2406                 else
2407                         g_key_file_remove_key(keyfile, identifier, key, NULL);
2408                 g_free(key);
2409                 /* fall through */
2410         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2411         case CONNMAN_IPCONFIG_METHOD_OFF:
2412         case CONNMAN_IPCONFIG_METHOD_AUTO:
2413                 return 0;
2414         }
2415
2416         key = g_strdup_printf("%snetmask_prefixlen", prefix);
2417         if (ipconfig->address->prefixlen != 0)
2418                 g_key_file_set_integer(keyfile, identifier,
2419                                 key, ipconfig->address->prefixlen);
2420         g_free(key);
2421
2422         key = g_strdup_printf("%slocal_address", prefix);
2423         if (ipconfig->address->local)
2424                 g_key_file_set_string(keyfile, identifier,
2425                                 key, ipconfig->address->local);
2426         g_free(key);
2427
2428         key = g_strdup_printf("%speer_address", prefix);
2429         if (ipconfig->address->peer)
2430                 g_key_file_set_string(keyfile, identifier,
2431                                 key, ipconfig->address->peer);
2432         g_free(key);
2433
2434         key = g_strdup_printf("%sbroadcast_address", prefix);
2435         if (ipconfig->address->broadcast)
2436                 g_key_file_set_string(keyfile, identifier,
2437                         key, ipconfig->address->broadcast);
2438         g_free(key);
2439
2440         key = g_strdup_printf("%sgateway", prefix);
2441         if (ipconfig->address->gateway)
2442                 g_key_file_set_string(keyfile, identifier,
2443                         key, ipconfig->address->gateway);
2444         g_free(key);
2445
2446         return 0;
2447 }
2448
2449 int __connman_ipconfig_init(void)
2450 {
2451         DBG("");
2452
2453         ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2454                                                         NULL, free_ipdevice);
2455
2456         is_ipv6_supported = connman_inet_is_ipv6_supported();
2457
2458         return 0;
2459 }
2460
2461 void __connman_ipconfig_cleanup(void)
2462 {
2463         DBG("");
2464
2465         g_hash_table_destroy(ipdevice_hash);
2466         ipdevice_hash = NULL;
2467 }