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