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