ipconfig: Add caller information to ref/unref debug prints.
[platform/upstream/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 gboolean __connman_ipconfig_ipv6_privacy_enabled(struct connman_ipconfig *ipconfig)
510 {
511         if (ipconfig == NULL)
512                 return FALSE;
513
514         return ipconfig->ipv6_privacy_config == 0 ? FALSE : TRUE;
515 }
516
517 static void free_ipdevice(gpointer data)
518 {
519         struct connman_ipdevice *ipdevice = data;
520
521         connman_info("%s {remove} index %d", ipdevice->ifname,
522                                                         ipdevice->index);
523
524         if (ipdevice->config_ipv4 != NULL) {
525                 __connman_ipconfig_unref(ipdevice->config_ipv4);
526                 ipdevice->config_ipv4 = NULL;
527         }
528
529         if (ipdevice->config_ipv6 != NULL) {
530                 __connman_ipconfig_unref(ipdevice->config_ipv6);
531                 ipdevice->config_ipv6 = NULL;
532         }
533
534         free_address_list(ipdevice);
535         g_free(ipdevice->ipv4_gateway);
536         g_free(ipdevice->ipv6_gateway);
537         g_free(ipdevice->pac);
538
539         g_free(ipdevice->address);
540
541         set_ipv6_state(ipdevice->ifname, ipdevice->ipv6_enabled);
542         set_ipv6_privacy(ipdevice->ifname, ipdevice->ipv6_privacy);
543
544         g_free(ipdevice->ifname);
545         g_free(ipdevice);
546 }
547
548 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
549 {
550         DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
551                                         ipdevice->config_ipv6);
552 }
553
554 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
555 {
556         DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
557                                         ipdevice->config_ipv6);
558
559         if (ipdevice->config_ipv4)
560                 connman_inet_clear_address(ipdevice->index,
561                                         ipdevice->config_ipv4->address);
562
563         if (ipdevice->config_ipv6)
564                 connman_inet_clear_ipv6_address(ipdevice->index,
565                                 ipdevice->config_ipv6->address->local,
566                                 ipdevice->config_ipv6->address->prefixlen);
567 }
568
569 static void update_stats(struct connman_ipdevice *ipdevice,
570                                                 struct rtnl_link_stats *stats)
571 {
572         struct connman_service *service;
573
574         if (stats->rx_packets == 0 && stats->tx_packets == 0)
575                 return;
576
577         connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
578                                         stats->rx_packets, stats->rx_bytes);
579         connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
580                                         stats->tx_packets, stats->tx_bytes);
581
582         if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
583                 return;
584
585         if (ipdevice->config_ipv4)
586                 service = __connman_ipconfig_get_data(ipdevice->config_ipv4);
587         else if (ipdevice->config_ipv6)
588                 service = __connman_ipconfig_get_data(ipdevice->config_ipv6);
589         else
590                 return;
591
592         if (service == NULL)
593                 return;
594
595         ipdevice->rx_packets = stats->rx_packets;
596         ipdevice->tx_packets = stats->tx_packets;
597         ipdevice->rx_bytes = stats->rx_bytes;
598         ipdevice->tx_bytes = stats->tx_bytes;
599         ipdevice->rx_errors = stats->rx_errors;
600         ipdevice->tx_errors = stats->tx_errors;
601         ipdevice->rx_dropped = stats->rx_dropped;
602         ipdevice->tx_dropped = stats->tx_dropped;
603
604         __connman_service_notify(service,
605                                 ipdevice->rx_packets, ipdevice->tx_packets,
606                                 ipdevice->rx_bytes, ipdevice->tx_bytes,
607                                 ipdevice->rx_errors, ipdevice->tx_errors,
608                                 ipdevice->rx_dropped, ipdevice->tx_dropped);
609 }
610
611 void __connman_ipconfig_newlink(int index, unsigned short type,
612                                 unsigned int flags, const char *address,
613                                                         unsigned short mtu,
614                                                 struct rtnl_link_stats *stats)
615 {
616         struct connman_ipdevice *ipdevice;
617         GList *list;
618         GString *str;
619         gboolean up = FALSE, down = FALSE;
620         gboolean lower_up = FALSE, lower_down = FALSE;
621
622         DBG("index %d", index);
623
624         if (type == ARPHRD_LOOPBACK)
625                 return;
626
627         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
628         if (ipdevice != NULL)
629                 goto update;
630
631         ipdevice = g_try_new0(struct connman_ipdevice, 1);
632         if (ipdevice == NULL)
633                 return;
634
635         ipdevice->index = index;
636         ipdevice->ifname = connman_inet_ifname(index);
637         ipdevice->type = type;
638
639         ipdevice->ipv6_enabled = get_ipv6_state(ipdevice->ifname);
640         ipdevice->ipv6_privacy = get_ipv6_privacy(ipdevice->ifname);
641
642         ipdevice->address = g_strdup(address);
643
644         g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
645
646         connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
647                                                 index, type, type2str(type));
648
649 update:
650         ipdevice->mtu = mtu;
651
652         update_stats(ipdevice, stats);
653
654         if (flags == ipdevice->flags)
655                 return;
656
657         if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
658                 if (flags & IFF_UP)
659                         up = TRUE;
660                 else
661                         down = TRUE;
662         }
663
664         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
665                                 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
666                 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
667                                         (IFF_RUNNING | IFF_LOWER_UP))
668                         lower_up = TRUE;
669                 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
670                         lower_down = TRUE;
671         }
672
673         ipdevice->flags = flags;
674
675         str = g_string_new(NULL);
676         if (str == NULL)
677                 return;
678
679         if (flags & IFF_UP)
680                 g_string_append(str, "UP");
681         else
682                 g_string_append(str, "DOWN");
683
684         if (flags & IFF_RUNNING)
685                 g_string_append(str, ",RUNNING");
686
687         if (flags & IFF_LOWER_UP)
688                 g_string_append(str, ",LOWER_UP");
689
690         connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
691                                                         flags, str->str);
692
693         g_string_free(str, TRUE);
694
695         for (list = g_list_first(ipconfig_list); list;
696                                                 list = g_list_next(list)) {
697                 struct connman_ipconfig *ipconfig = list->data;
698
699                 if (index != ipconfig->index)
700                         continue;
701
702                 if (ipconfig->ops == NULL)
703                         continue;
704
705                 if (up == TRUE && ipconfig->ops->up)
706                         ipconfig->ops->up(ipconfig);
707                 if (lower_up == TRUE && ipconfig->ops->lower_up)
708                         ipconfig->ops->lower_up(ipconfig);
709
710                 if (lower_down == TRUE && ipconfig->ops->lower_down)
711                         ipconfig->ops->lower_down(ipconfig);
712                 if (down == TRUE && ipconfig->ops->down)
713                         ipconfig->ops->down(ipconfig);
714         }
715
716         if (lower_up)
717                 __connman_ipconfig_lower_up(ipdevice);
718         if (lower_down)
719                 __connman_ipconfig_lower_down(ipdevice);
720 }
721
722 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
723 {
724         struct connman_ipdevice *ipdevice;
725         GList *list;
726
727         DBG("index %d", index);
728
729         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
730         if (ipdevice == NULL)
731                 return;
732
733         update_stats(ipdevice, stats);
734
735         for (list = g_list_first(ipconfig_list); list;
736                                                 list = g_list_next(list)) {
737                 struct connman_ipconfig *ipconfig = list->data;
738
739                 if (index != ipconfig->index)
740                         continue;
741
742                 ipconfig->index = -1;
743
744                 if (ipconfig->ops == NULL)
745                         continue;
746
747                 if (ipconfig->ops->lower_down)
748                         ipconfig->ops->lower_down(ipconfig);
749                 if (ipconfig->ops->down)
750                         ipconfig->ops->down(ipconfig);
751         }
752
753         __connman_ipconfig_lower_down(ipdevice);
754
755         g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
756 }
757
758 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
759 {
760         const struct connman_ipaddress *addr1 = a;
761         const struct connman_ipaddress *addr2 = b;
762
763         if (addr1->prefixlen != addr2->prefixlen)
764                 return addr2->prefixlen - addr1->prefixlen;
765
766         return g_strcmp0(addr1->local, addr2->local);
767 }
768
769 void __connman_ipconfig_newaddr(int index, int family, const char *label,
770                                 unsigned char prefixlen, const char *address)
771 {
772         struct connman_ipdevice *ipdevice;
773         struct connman_ipaddress *ipaddress;
774         enum connman_ipconfig_type type;
775         GList *list;
776
777         DBG("index %d", index);
778
779         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
780         if (ipdevice == NULL)
781                 return;
782
783         ipaddress = connman_ipaddress_alloc(family);
784         if (ipaddress == NULL)
785                 return;
786
787         ipaddress->prefixlen = prefixlen;
788         ipaddress->local = g_strdup(address);
789
790         if (g_slist_find_custom(ipdevice->address_list, ipaddress,
791                                         check_duplicate_address)) {
792                 connman_ipaddress_free(ipaddress);
793                 return;
794         }
795
796         if (family == AF_INET)
797                 type = CONNMAN_IPCONFIG_TYPE_IPV4;
798         else if (family == AF_INET6)
799                 type = CONNMAN_IPCONFIG_TYPE_IPV6;
800         else
801                 return;
802
803         ipdevice->address_list = g_slist_append(ipdevice->address_list,
804                                                                 ipaddress);
805
806         connman_info("%s {add} address %s/%u label %s family %d",
807                 ipdevice->ifname, address, prefixlen, label, family);
808
809         if (ipdevice->config_ipv4 != NULL && family == AF_INET)
810                 connman_ipaddress_copy(ipdevice->config_ipv4->system,
811                                         ipaddress);
812
813         else if (ipdevice->config_ipv6 != NULL && family == AF_INET6)
814                 connman_ipaddress_copy(ipdevice->config_ipv6->system,
815                                         ipaddress);
816         else
817                 return;
818
819         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
820                 return;
821
822         for (list = g_list_first(ipconfig_list); list;
823                                                 list = g_list_next(list)) {
824                 struct connman_ipconfig *ipconfig = list->data;
825
826                 if (index != ipconfig->index)
827                         continue;
828
829                 if (type != ipconfig->type)
830                         continue;
831
832                 if (ipconfig->ops == NULL)
833                         continue;
834
835                 if (ipconfig->ops->ip_bound)
836                         ipconfig->ops->ip_bound(ipconfig);
837         }
838 }
839
840 void __connman_ipconfig_deladdr(int index, int family, const char *label,
841                                 unsigned char prefixlen, const char *address)
842 {
843         struct connman_ipdevice *ipdevice;
844         struct connman_ipaddress *ipaddress;
845         enum connman_ipconfig_type type;
846         GList *list;
847
848         DBG("index %d", index);
849
850         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
851         if (ipdevice == NULL)
852                 return;
853
854         ipaddress = find_ipaddress(ipdevice, prefixlen, address);
855         if (ipaddress == NULL)
856                 return;
857
858         if (family == AF_INET)
859                 type = CONNMAN_IPCONFIG_TYPE_IPV4;
860         else if (family == AF_INET6)
861                 type = CONNMAN_IPCONFIG_TYPE_IPV6;
862         else
863                 return;
864
865         ipdevice->address_list = g_slist_remove(ipdevice->address_list,
866                                                                 ipaddress);
867
868         connman_ipaddress_clear(ipaddress);
869         g_free(ipaddress);
870
871         connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
872                                                 address, prefixlen, label);
873
874         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
875                 return;
876
877         if (g_slist_length(ipdevice->address_list) > 0)
878                 return;
879
880         for (list = g_list_first(ipconfig_list); list;
881                                                 list = g_list_next(list)) {
882                 struct connman_ipconfig *ipconfig = list->data;
883
884                 if (index != ipconfig->index)
885                         continue;
886
887                 if (type != ipconfig->type)
888                         continue;
889
890                 if (ipconfig->ops == NULL)
891                         continue;
892
893                 if (ipconfig->ops->ip_release)
894                         ipconfig->ops->ip_release(ipconfig);
895         }
896 }
897
898 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
899                                         const char *dst, const char *gateway)
900 {
901         struct connman_ipdevice *ipdevice;
902
903         DBG("index %d", index);
904
905         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
906         if (ipdevice == NULL)
907                 return;
908
909         if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
910                                                 g_strcmp0(dst, "::") == 0)) {
911                 GList *config_list;
912                 enum connman_ipconfig_type type;
913
914                 if (family == AF_INET6) {
915                         type = CONNMAN_IPCONFIG_TYPE_IPV6;
916                         g_free(ipdevice->ipv6_gateway);
917                         ipdevice->ipv6_gateway = g_strdup(gateway);
918
919                         if (ipdevice->config_ipv6 != NULL &&
920                                 ipdevice->config_ipv6->system != NULL) {
921                                 g_free(ipdevice->config_ipv6->system->gateway);
922                                 ipdevice->config_ipv6->system->gateway =
923                                         g_strdup(gateway);
924                         }
925                 } else if (family == AF_INET) {
926                         type = CONNMAN_IPCONFIG_TYPE_IPV4;
927                         g_free(ipdevice->ipv4_gateway);
928                         ipdevice->ipv4_gateway = g_strdup(gateway);
929
930                         if (ipdevice->config_ipv4 != NULL &&
931                                 ipdevice->config_ipv4->system != NULL) {
932                                 g_free(ipdevice->config_ipv4->system->gateway);
933                                 ipdevice->config_ipv4->system->gateway =
934                                         g_strdup(gateway);
935                         }
936                 } else
937                         return;
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->route_set)
953                                 ipconfig->ops->route_set(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                 GList *config_list;
976                 enum connman_ipconfig_type type;
977
978                 if (family == AF_INET6) {
979                         type = CONNMAN_IPCONFIG_TYPE_IPV6;
980                         g_free(ipdevice->ipv6_gateway);
981                         ipdevice->ipv6_gateway = NULL;
982
983                         if (ipdevice->config_ipv6 != NULL &&
984                                 ipdevice->config_ipv6->system != NULL) {
985                                 g_free(ipdevice->config_ipv6->system->gateway);
986                                 ipdevice->config_ipv6->system->gateway = NULL;
987                         }
988                 } else if (family == AF_INET) {
989                         type = CONNMAN_IPCONFIG_TYPE_IPV4;
990                         g_free(ipdevice->ipv4_gateway);
991                         ipdevice->ipv4_gateway = NULL;
992
993                         if (ipdevice->config_ipv4 != NULL &&
994                                 ipdevice->config_ipv4->system != NULL) {
995                                 g_free(ipdevice->config_ipv4->system->gateway);
996                                 ipdevice->config_ipv4->system->gateway = NULL;
997                         }
998                 } else
999                         return;
1000
1001                 for (config_list = g_list_first(ipconfig_list); config_list;
1002                                         config_list = g_list_next(config_list)) {
1003                         struct connman_ipconfig *ipconfig = config_list->data;
1004
1005                         if (index != ipconfig->index)
1006                                 continue;
1007
1008                         if (type != ipconfig->type)
1009                                 continue;
1010
1011                         if (ipconfig->ops == NULL)
1012                                 continue;
1013
1014                         if (ipconfig->ops->route_unset)
1015                                 ipconfig->ops->route_unset(ipconfig);
1016                 }
1017         }
1018
1019         connman_info("%s {del} route %s gw %s scope %u <%s>",
1020                                         ipdevice->ifname, dst, gateway,
1021                                                 scope, scope2str(scope));
1022 }
1023
1024 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
1025                                                         void *user_data)
1026 {
1027         GList *list, *keys;
1028
1029         keys = g_hash_table_get_keys(ipdevice_hash);
1030         if (keys == NULL)
1031                 return;
1032
1033         for (list = g_list_first(keys); list; list = g_list_next(list)) {
1034                 int index = GPOINTER_TO_INT(list->data);
1035
1036                 function(index, user_data);
1037         }
1038
1039         g_list_free(keys);
1040 }
1041
1042 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
1043                                         struct connman_ipconfig *ipconfig)
1044 {
1045         return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
1046 }
1047
1048 unsigned short __connman_ipconfig_get_type_from_index(int index)
1049 {
1050         struct connman_ipdevice *ipdevice;
1051
1052         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1053         if (ipdevice == NULL)
1054                 return ARPHRD_VOID;
1055
1056         return ipdevice->type;
1057 }
1058
1059 unsigned int __connman_ipconfig_get_flags_from_index(int index)
1060 {
1061         struct connman_ipdevice *ipdevice;
1062
1063         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1064         if (ipdevice == NULL)
1065                 return 0;
1066
1067         return ipdevice->flags;
1068 }
1069
1070 const char *__connman_ipconfig_get_gateway_from_index(int index)
1071 {
1072         struct connman_ipdevice *ipdevice;
1073
1074         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1075         if (ipdevice == NULL)
1076                 return NULL;
1077
1078         if (ipdevice->ipv4_gateway != NULL)
1079                 return ipdevice->ipv4_gateway;
1080
1081         if (ipdevice->config_ipv4 != NULL &&
1082                         ipdevice->config_ipv4->address != NULL)
1083                 return ipdevice->config_ipv4->address->gateway;
1084
1085         if (ipdevice->ipv6_gateway != NULL)
1086                 return ipdevice->ipv6_gateway;
1087
1088         if (ipdevice->config_ipv6 != NULL &&
1089                         ipdevice->config_ipv6->address != NULL)
1090                 return ipdevice->config_ipv6->address->gateway;
1091
1092         return NULL;
1093 }
1094
1095 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
1096 {
1097         ipconfig->index = index;
1098 }
1099
1100 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
1101 {
1102         if (ipconfig->address == NULL)
1103                 return NULL;
1104
1105         return ipconfig->address->local;
1106 }
1107
1108 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig, const char *address)
1109 {
1110         if (ipconfig->address == NULL)
1111                 return;
1112
1113         g_free(ipconfig->address->local);
1114         ipconfig->address->local = g_strdup(address);
1115 }
1116
1117 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
1118 {
1119         if (ipconfig->address == NULL)
1120                 return NULL;
1121
1122         return ipconfig->address->peer;
1123 }
1124
1125 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig, const char *address)
1126 {
1127         if (ipconfig->address == NULL)
1128                 return;
1129
1130         g_free(ipconfig->address->peer);
1131         ipconfig->address->peer = g_strdup(address);
1132 }
1133
1134 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1135 {
1136         if (ipconfig->address == NULL)
1137                 return NULL;
1138
1139         return ipconfig->address->broadcast;
1140 }
1141
1142 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig, const char *broadcast)
1143 {
1144         if (ipconfig->address == NULL)
1145                 return;
1146
1147         g_free(ipconfig->address->broadcast);
1148         ipconfig->address->broadcast = g_strdup(broadcast);
1149 }
1150
1151 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1152 {
1153         if (ipconfig->address == NULL)
1154                 return NULL;
1155
1156         return ipconfig->address->gateway;
1157 }
1158
1159 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig, const char *gateway)
1160 {
1161         DBG("");
1162
1163         if (ipconfig->address == NULL)
1164                 return;
1165         g_free(ipconfig->address->gateway);
1166         ipconfig->address->gateway = g_strdup(gateway);
1167 }
1168
1169 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1170 {
1171         struct connman_service *service;
1172
1173         DBG("");
1174
1175         if (ipconfig->address == NULL)
1176                 return -EINVAL;
1177
1178         service = __connman_service_lookup_from_index(ipconfig->index);
1179         if (service == NULL)
1180                 return -EINVAL;
1181
1182         __connman_connection_gateway_remove(service, ipconfig->type);
1183
1184         DBG("type %d gw %s peer %s", ipconfig->type,
1185                 ipconfig->address->gateway, ipconfig->address->peer);
1186
1187         if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6 ||
1188                                 ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1189                 return __connman_connection_gateway_add(service,
1190                                                 ipconfig->address->gateway,
1191                                                 ipconfig->type,
1192                                                 ipconfig->address->peer);
1193
1194         return 0;
1195 }
1196
1197 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1198 {
1199         struct connman_service *service;
1200
1201         DBG("");
1202
1203         service = __connman_service_lookup_from_index(ipconfig->index);
1204         if (service != NULL)
1205                 __connman_connection_gateway_remove(service, ipconfig->type);
1206 }
1207
1208 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1209 {
1210         if (ipconfig->address == NULL)
1211                 return 0;
1212
1213         return ipconfig->address->prefixlen;
1214 }
1215
1216 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig, unsigned char prefixlen)
1217 {
1218         if (ipconfig->address == NULL)
1219                 return;
1220
1221         ipconfig->address->prefixlen = prefixlen;
1222 }
1223
1224 static struct connman_ipconfig *create_ipv6config(int index)
1225 {
1226         struct connman_ipconfig *ipv6config;
1227
1228         DBG("index %d", index);
1229
1230         ipv6config = g_try_new0(struct connman_ipconfig, 1);
1231         if (ipv6config == NULL)
1232                 return NULL;
1233
1234         ipv6config->refcount = 1;
1235
1236         ipv6config->index = index;
1237         ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1238         ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1239         ipv6config->ipv6_privacy_config = 0;
1240
1241         ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1242         if (ipv6config->address == NULL) {
1243                 g_free(ipv6config);
1244                 return NULL;
1245         }
1246
1247         ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1248
1249         DBG("ipconfig %p", ipv6config);
1250
1251         return ipv6config;
1252 }
1253
1254 /**
1255  * connman_ipconfig_create:
1256  *
1257  * Allocate a new ipconfig structure.
1258  *
1259  * Returns: a newly-allocated #connman_ipconfig structure
1260  */
1261 struct connman_ipconfig *__connman_ipconfig_create(int index,
1262                                         enum connman_ipconfig_type type)
1263 {
1264         struct connman_ipconfig *ipconfig;
1265
1266         if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1267                 return create_ipv6config(index);
1268
1269         DBG("index %d", index);
1270
1271         ipconfig = g_try_new0(struct connman_ipconfig, 1);
1272         if (ipconfig == NULL)
1273                 return NULL;
1274
1275         ipconfig->refcount = 1;
1276
1277         ipconfig->index = index;
1278         ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1279
1280         ipconfig->address = connman_ipaddress_alloc(AF_INET);
1281         if (ipconfig->address == NULL) {
1282                 g_free(ipconfig);
1283                 return NULL;
1284         }
1285
1286         ipconfig->system = connman_ipaddress_alloc(AF_INET);
1287
1288         DBG("ipconfig %p", ipconfig);
1289
1290         return ipconfig;
1291 }
1292
1293
1294 /**
1295  * connman_ipconfig_ref:
1296  * @ipconfig: ipconfig structure
1297  *
1298  * Increase reference counter of ipconfig
1299  */
1300 struct connman_ipconfig *
1301 __connman_ipconfig_ref_debug(struct connman_ipconfig *ipconfig,
1302                                 const char *file, int line, const char *caller)
1303 {
1304         DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount + 1,
1305                 file, line, caller);
1306
1307         __sync_fetch_and_add(&ipconfig->refcount, 1);
1308
1309         return ipconfig;
1310 }
1311
1312 /**
1313  * connman_ipconfig_unref:
1314  * @ipconfig: ipconfig structure
1315  *
1316  * Decrease reference counter of ipconfig
1317  */
1318 void __connman_ipconfig_unref_debug(struct connman_ipconfig *ipconfig,
1319                                 const char *file, int line, const char *caller)
1320 {
1321         if (ipconfig == NULL)
1322                 return;
1323
1324         DBG("%p ref %d by %s:%d:%s()", ipconfig, ipconfig->refcount - 1,
1325                 file, line, caller);
1326
1327         if (__sync_fetch_and_sub(&ipconfig->refcount, 1) != 1)
1328                 return;
1329
1330         if (__connman_ipconfig_disable(ipconfig) < 0)
1331                 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1332
1333         __connman_ipconfig_set_ops(ipconfig, NULL);
1334
1335         if (ipconfig->origin != NULL && ipconfig->origin != ipconfig) {
1336                 __connman_ipconfig_unref(ipconfig->origin);
1337                 ipconfig->origin = NULL;
1338         }
1339
1340         connman_ipaddress_free(ipconfig->system);
1341         connman_ipaddress_free(ipconfig->address);
1342         g_free(ipconfig->last_dhcp_address);
1343         g_free(ipconfig);
1344 }
1345
1346 /**
1347  * connman_ipconfig_get_data:
1348  * @ipconfig: ipconfig structure
1349  *
1350  * Get private data pointer
1351  */
1352 void *__connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1353 {
1354         if (ipconfig == NULL)
1355                 return NULL;
1356
1357         return ipconfig->ops_data;
1358 }
1359
1360 /**
1361  * connman_ipconfig_set_data:
1362  * @ipconfig: ipconfig structure
1363  * @data: data pointer
1364  *
1365  * Set private data pointer
1366  */
1367 void __connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1368 {
1369         ipconfig->ops_data = data;
1370 }
1371
1372 /**
1373  * connman_ipconfig_get_index:
1374  * @ipconfig: ipconfig structure
1375  *
1376  * Get interface index
1377  */
1378 int __connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1379 {
1380         if (ipconfig == NULL)
1381                 return -1;
1382
1383         if (ipconfig->origin != NULL)
1384                 return ipconfig->origin->index;
1385
1386         return ipconfig->index;
1387 }
1388
1389 /**
1390  * connman_ipconfig_get_ifname:
1391  * @ipconfig: ipconfig structure
1392  *
1393  * Get interface name
1394  */
1395 const char *__connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1396 {
1397         struct connman_ipdevice *ipdevice;
1398
1399         if (ipconfig == NULL)
1400                 return NULL;
1401
1402         if (ipconfig->index < 0)
1403                 return NULL;
1404
1405         ipdevice = g_hash_table_lookup(ipdevice_hash,
1406                                         GINT_TO_POINTER(ipconfig->index));
1407         if (ipdevice == NULL)
1408                 return NULL;
1409
1410         return ipdevice->ifname;
1411 }
1412
1413 /**
1414  * connman_ipconfig_set_ops:
1415  * @ipconfig: ipconfig structure
1416  * @ops: operation callbacks
1417  *
1418  * Set the operation callbacks
1419  */
1420 void __connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1421                                 const struct connman_ipconfig_ops *ops)
1422 {
1423         ipconfig->ops = ops;
1424 }
1425
1426 /**
1427  * connman_ipconfig_set_method:
1428  * @ipconfig: ipconfig structure
1429  * @method: configuration method
1430  *
1431  * Set the configuration method
1432  */
1433 int __connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1434                                         enum connman_ipconfig_method method)
1435 {
1436         ipconfig->method = method;
1437
1438         return 0;
1439 }
1440
1441 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1442 {
1443         if (ipconfig == NULL)
1444                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1445
1446         return ipconfig->method;
1447 }
1448
1449 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1450 {
1451         DBG("");
1452
1453         switch (ipconfig->method) {
1454         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1455         case CONNMAN_IPCONFIG_METHOD_OFF:
1456                 break;
1457         case CONNMAN_IPCONFIG_METHOD_AUTO:
1458         case CONNMAN_IPCONFIG_METHOD_FIXED:
1459         case CONNMAN_IPCONFIG_METHOD_DHCP:
1460         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1461                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1462                         return connman_inet_set_address(ipconfig->index,
1463                                                         ipconfig->address);
1464                 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1465                         return connman_inet_set_ipv6_address(
1466                                         ipconfig->index, ipconfig->address);
1467         }
1468
1469         return 0;
1470 }
1471
1472 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1473 {
1474         int err;
1475
1476         DBG("");
1477
1478         if (ipconfig == NULL)
1479                 return 0;
1480
1481         DBG("method %d", ipconfig->method);
1482
1483         switch (ipconfig->method) {
1484         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1485         case CONNMAN_IPCONFIG_METHOD_OFF:
1486                 break;
1487         case CONNMAN_IPCONFIG_METHOD_AUTO:
1488         case CONNMAN_IPCONFIG_METHOD_FIXED:
1489         case CONNMAN_IPCONFIG_METHOD_DHCP:
1490         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1491                 err = __connman_ipconfig_address_unset(ipconfig);
1492                 connman_ipaddress_clear(ipconfig->address);
1493
1494                 return err;
1495         }
1496
1497         return 0;
1498 }
1499
1500 int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig)
1501 {
1502         int err;
1503
1504         DBG("");
1505
1506         if (ipconfig == NULL)
1507                 return 0;
1508
1509         DBG("method %d", ipconfig->method);
1510
1511         switch (ipconfig->method) {
1512         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1513         case CONNMAN_IPCONFIG_METHOD_OFF:
1514                 break;
1515         case CONNMAN_IPCONFIG_METHOD_AUTO:
1516         case CONNMAN_IPCONFIG_METHOD_FIXED:
1517         case CONNMAN_IPCONFIG_METHOD_DHCP:
1518         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1519                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1520                         err = connman_inet_clear_address(ipconfig->index,
1521                                                         ipconfig->address);
1522                 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1523                         err = connman_inet_clear_ipv6_address(
1524                                                 ipconfig->index,
1525                                                 ipconfig->address->local,
1526                                                 ipconfig->address->prefixlen);
1527                 else
1528                         err = -EINVAL;
1529
1530                 return err;
1531         }
1532
1533         return 0;
1534 }
1535
1536 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1537                                                         const char *url)
1538 {
1539         struct connman_ipdevice *ipdevice;
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         g_free(ipdevice->pac);
1552         ipdevice->pac = g_strdup(url);
1553
1554         return 0;
1555 }
1556
1557 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1558 {
1559         struct connman_ipdevice *ipdevice;
1560
1561         DBG("ipconfig %p", ipconfig);
1562
1563         if (ipconfig == NULL || ipconfig->index < 0)
1564                 return NULL;
1565
1566         ipdevice = g_hash_table_lookup(ipdevice_hash,
1567                                         GINT_TO_POINTER(ipconfig->index));
1568         if (ipdevice == NULL)
1569                 return NULL;
1570
1571         return ipdevice->pac;
1572 }
1573
1574 void __connman_ipconfig_set_dhcp_address(struct connman_ipconfig *ipconfig,
1575                                         const char *address)
1576 {
1577         if (ipconfig == NULL)
1578                 return;
1579
1580         g_free(ipconfig->last_dhcp_address);
1581         ipconfig->last_dhcp_address = g_strdup(address);
1582 }
1583
1584 char *__connman_ipconfig_get_dhcp_address(struct connman_ipconfig *ipconfig)
1585 {
1586         if (ipconfig == NULL)
1587                 return NULL;
1588
1589         return ipconfig->last_dhcp_address;
1590 }
1591
1592 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1593 {
1594         struct connman_ipdevice *ipdevice;
1595
1596         DBG("");
1597
1598         ipdevice = g_hash_table_lookup(ipdevice_hash,
1599                                         GINT_TO_POINTER(ipconfig->index));
1600         if (ipdevice == NULL)
1601                 return;
1602
1603         set_ipv6_state(ipdevice->ifname, FALSE);
1604 }
1605
1606 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1607 {
1608         struct connman_ipdevice *ipdevice;
1609
1610         DBG("");
1611
1612         ipdevice = g_hash_table_lookup(ipdevice_hash,
1613                                         GINT_TO_POINTER(ipconfig->index));
1614         if (ipdevice == NULL)
1615                 return;
1616
1617         if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1618                 set_ipv6_privacy(ipdevice->ifname,
1619                                 ipconfig->ipv6_privacy_config);
1620
1621         set_ipv6_state(ipdevice->ifname, TRUE);
1622 }
1623
1624 void __connman_ipconfig_enable_ipv6(struct connman_ipconfig *ipconfig)
1625 {
1626         if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1627                 return;
1628
1629         enable_ipv6(ipconfig);
1630 }
1631
1632 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1633 {
1634         if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1635                 return;
1636
1637         disable_ipv6(ipconfig);
1638 }
1639
1640 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1641 {
1642         struct connman_ipdevice *ipdevice;
1643         gboolean up = FALSE, down = FALSE;
1644         gboolean lower_up = FALSE, lower_down = FALSE;
1645         enum connman_ipconfig_type type;
1646
1647         DBG("ipconfig %p", ipconfig);
1648
1649         if (ipconfig == NULL || ipconfig->index < 0)
1650                 return -ENODEV;
1651
1652         ipdevice = g_hash_table_lookup(ipdevice_hash,
1653                                         GINT_TO_POINTER(ipconfig->index));
1654         if (ipdevice == NULL)
1655                 return -ENXIO;
1656
1657         if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1658                 if (ipdevice->config_ipv4 == ipconfig)
1659                         return -EALREADY;
1660                 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1661         } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1662                 if (ipdevice->config_ipv6 == ipconfig)
1663                         return -EALREADY;
1664                 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1665                 enable_ipv6(ipconfig);
1666         } else
1667                 return -EINVAL;
1668
1669         if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1670                                         ipdevice->config_ipv4 != NULL) {
1671                 ipconfig_list = g_list_remove(ipconfig_list,
1672                                                         ipdevice->config_ipv4);
1673
1674                 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1675
1676                 __connman_ipconfig_unref(ipdevice->config_ipv4);
1677         }
1678
1679         if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1680                                         ipdevice->config_ipv6 != NULL) {
1681                 ipconfig_list = g_list_remove(ipconfig_list,
1682                                                         ipdevice->config_ipv6);
1683
1684                 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1685
1686                 __connman_ipconfig_unref(ipdevice->config_ipv6);
1687         }
1688
1689         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1690                 ipdevice->config_ipv4 = __connman_ipconfig_ref(ipconfig);
1691         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1692                 ipdevice->config_ipv6 = __connman_ipconfig_ref(ipconfig);
1693
1694         ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1695
1696         if (ipdevice->flags & IFF_UP)
1697                 up = TRUE;
1698         else
1699                 down = TRUE;
1700
1701         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1702                         (IFF_RUNNING | IFF_LOWER_UP))
1703                 lower_up = TRUE;
1704         else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1705                 lower_down = TRUE;
1706
1707         if (up == TRUE && ipconfig->ops->up)
1708                 ipconfig->ops->up(ipconfig);
1709         if (lower_up == TRUE && ipconfig->ops->lower_up)
1710                 ipconfig->ops->lower_up(ipconfig);
1711
1712         if (lower_down == TRUE && ipconfig->ops->lower_down)
1713                 ipconfig->ops->lower_down(ipconfig);
1714         if (down == TRUE && ipconfig->ops->down)
1715                 ipconfig->ops->down(ipconfig);
1716
1717         return 0;
1718 }
1719
1720 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1721 {
1722         struct connman_ipdevice *ipdevice;
1723
1724         DBG("ipconfig %p", ipconfig);
1725
1726         if (ipconfig == NULL || ipconfig->index < 0)
1727                 return -ENODEV;
1728
1729         ipdevice = g_hash_table_lookup(ipdevice_hash,
1730                                         GINT_TO_POINTER(ipconfig->index));
1731         if (ipdevice == NULL)
1732                 return -ENXIO;
1733
1734         if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1735                 return -EINVAL;
1736
1737         if (ipdevice->config_ipv4 == ipconfig) {
1738                 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1739
1740                 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1741                 __connman_ipconfig_unref(ipdevice->config_ipv4);
1742                 ipdevice->config_ipv4 = NULL;
1743                 return 0;
1744         }
1745
1746         if (ipdevice->config_ipv6 == ipconfig) {
1747                 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1748
1749                 if (ipdevice->config_ipv6->method ==
1750                                                 CONNMAN_IPCONFIG_METHOD_AUTO)
1751                         disable_ipv6(ipdevice->config_ipv6);
1752
1753                 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1754                 __connman_ipconfig_unref(ipdevice->config_ipv6);
1755                 ipdevice->config_ipv6 = NULL;
1756                 return 0;
1757         }
1758
1759         return -EINVAL;
1760 }
1761
1762 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1763 {
1764         switch (method) {
1765         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1766                 break;
1767         case CONNMAN_IPCONFIG_METHOD_OFF:
1768                 return "off";
1769         case CONNMAN_IPCONFIG_METHOD_FIXED:
1770                 return "fixed";
1771         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1772                 return "manual";
1773         case CONNMAN_IPCONFIG_METHOD_DHCP:
1774                 return "dhcp";
1775         case CONNMAN_IPCONFIG_METHOD_AUTO:
1776                 return "auto";
1777         }
1778
1779         return NULL;
1780 }
1781
1782 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1783 {
1784         if (g_strcmp0(method, "off") == 0)
1785                 return CONNMAN_IPCONFIG_METHOD_OFF;
1786         else if (g_strcmp0(method, "fixed") == 0)
1787                 return CONNMAN_IPCONFIG_METHOD_FIXED;
1788         else if (g_strcmp0(method, "manual") == 0)
1789                 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1790         else if (g_strcmp0(method, "dhcp") == 0)
1791                 return CONNMAN_IPCONFIG_METHOD_DHCP;
1792         else if (g_strcmp0(method, "auto") == 0)
1793                 return CONNMAN_IPCONFIG_METHOD_AUTO;
1794         else
1795                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1796 }
1797
1798 static const char *privacy2string(int privacy)
1799 {
1800         if (privacy <= 0)
1801                 return "disabled";
1802         else if (privacy == 1)
1803                 return "enabled";
1804         else if (privacy > 1)
1805                 return "prefered";
1806
1807         return "disabled";
1808 }
1809
1810 static int string2privacy(const char *privacy)
1811 {
1812         if (g_strcmp0(privacy, "disabled") == 0)
1813                 return 0;
1814         else if (g_strcmp0(privacy, "enabled") == 0)
1815                 return 1;
1816         else if (g_strcmp0(privacy, "prefered") == 0)
1817                 return 2;
1818         else
1819                 return 0;
1820 }
1821
1822 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1823                                                         DBusMessageIter *iter)
1824 {
1825         const char *str;
1826
1827         DBG("");
1828
1829         if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1830                 return;
1831
1832         str = __connman_ipconfig_method2string(ipconfig->method);
1833         if (str == NULL)
1834                 return;
1835
1836         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1837
1838         if (ipconfig->system == NULL)
1839                 return;
1840
1841         if (ipconfig->system->local != NULL) {
1842                 in_addr_t addr;
1843                 struct in_addr netmask;
1844                 char *mask;
1845
1846                 connman_dbus_dict_append_basic(iter, "Address",
1847                                 DBUS_TYPE_STRING, &ipconfig->system->local);
1848
1849                 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1850                 netmask.s_addr = htonl(addr);
1851                 mask = inet_ntoa(netmask);
1852                 connman_dbus_dict_append_basic(iter, "Netmask",
1853                                                 DBUS_TYPE_STRING, &mask);
1854         }
1855
1856         if (ipconfig->system->gateway != NULL)
1857                 connman_dbus_dict_append_basic(iter, "Gateway",
1858                                 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1859 }
1860
1861 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1862                                         DBusMessageIter *iter,
1863                                         struct connman_ipconfig *ipconfig_ipv4)
1864 {
1865         const char *str, *privacy;
1866
1867         DBG("");
1868
1869         if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1870                 return;
1871
1872         str = __connman_ipconfig_method2string(ipconfig->method);
1873         if (str == NULL)
1874                 return;
1875
1876         if (ipconfig_ipv4 != NULL &&
1877                         ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1878                 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1879                         str = "6to4";
1880         }
1881
1882         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1883
1884         if (ipconfig->system == NULL)
1885                 return;
1886
1887         if (ipconfig->system->local != NULL) {
1888                 connman_dbus_dict_append_basic(iter, "Address",
1889                                 DBUS_TYPE_STRING, &ipconfig->system->local);
1890                 connman_dbus_dict_append_basic(iter, "PrefixLength",
1891                                                 DBUS_TYPE_BYTE,
1892                                                 &ipconfig->system->prefixlen);
1893         }
1894
1895         if (ipconfig->system->gateway != NULL)
1896                 connman_dbus_dict_append_basic(iter, "Gateway",
1897                                 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1898
1899         privacy = privacy2string(ipconfig->ipv6_privacy_config);
1900         connman_dbus_dict_append_basic(iter, "Privacy",
1901                                 DBUS_TYPE_STRING, &privacy);
1902 }
1903
1904 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1905                                                         DBusMessageIter *iter)
1906 {
1907         const char *str, *privacy;
1908
1909         DBG("");
1910
1911         str = __connman_ipconfig_method2string(ipconfig->method);
1912         if (str == NULL)
1913                 return;
1914
1915         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1916
1917         switch (ipconfig->method) {
1918         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1919         case CONNMAN_IPCONFIG_METHOD_OFF:
1920         case CONNMAN_IPCONFIG_METHOD_DHCP:
1921                 return;
1922         case CONNMAN_IPCONFIG_METHOD_FIXED:
1923         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1924         case CONNMAN_IPCONFIG_METHOD_AUTO:
1925                 break;
1926         }
1927
1928         if (ipconfig->address == NULL)
1929                 return;
1930
1931         if (ipconfig->address->local != NULL) {
1932                 connman_dbus_dict_append_basic(iter, "Address",
1933                                 DBUS_TYPE_STRING, &ipconfig->address->local);
1934                 connman_dbus_dict_append_basic(iter, "PrefixLength",
1935                                                 DBUS_TYPE_BYTE,
1936                                                 &ipconfig->address->prefixlen);
1937         }
1938
1939         if (ipconfig->address->gateway != NULL)
1940                 connman_dbus_dict_append_basic(iter, "Gateway",
1941                                 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1942
1943         privacy = privacy2string(ipconfig->ipv6_privacy_config);
1944         connman_dbus_dict_append_basic(iter, "Privacy",
1945                                 DBUS_TYPE_STRING, &privacy);
1946 }
1947
1948 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1949                                                         DBusMessageIter *iter)
1950 {
1951         const char *str;
1952
1953         DBG("");
1954
1955         str = __connman_ipconfig_method2string(ipconfig->method);
1956         if (str == NULL)
1957                 return;
1958
1959         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1960
1961         switch (ipconfig->method) {
1962         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1963         case CONNMAN_IPCONFIG_METHOD_OFF:
1964         case CONNMAN_IPCONFIG_METHOD_DHCP:
1965         case CONNMAN_IPCONFIG_METHOD_AUTO:
1966                 return;
1967         case CONNMAN_IPCONFIG_METHOD_FIXED:
1968         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1969                 break;
1970         }
1971
1972         if (ipconfig->address == NULL)
1973                 return;
1974
1975         if (ipconfig->address->local != NULL) {
1976                 in_addr_t addr;
1977                 struct in_addr netmask;
1978                 char *mask;
1979
1980                 connman_dbus_dict_append_basic(iter, "Address",
1981                                 DBUS_TYPE_STRING, &ipconfig->address->local);
1982
1983                 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1984                 netmask.s_addr = htonl(addr);
1985                 mask = inet_ntoa(netmask);
1986                 connman_dbus_dict_append_basic(iter, "Netmask",
1987                                                 DBUS_TYPE_STRING, &mask);
1988         }
1989
1990         if (ipconfig->address->gateway != NULL)
1991                 connman_dbus_dict_append_basic(iter, "Gateway",
1992                                 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1993 }
1994
1995 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1996                                                         DBusMessageIter *array)
1997 {
1998         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1999         const char *address = NULL, *netmask = NULL, *gateway = NULL,
2000                 *prefix_length_string = NULL, *privacy_string = NULL;
2001         int prefix_length = 0, privacy = 0;
2002         DBusMessageIter dict;
2003
2004         DBG("ipconfig %p", ipconfig);
2005
2006         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
2007                 return -EINVAL;
2008
2009         dbus_message_iter_recurse(array, &dict);
2010
2011         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2012                 DBusMessageIter entry;
2013                 const char *key;
2014                 int type;
2015
2016                 dbus_message_iter_recurse(&dict, &entry);
2017
2018                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2019                         return -EINVAL;
2020
2021                 dbus_message_iter_get_basic(&entry, &key);
2022                 dbus_message_iter_next(&entry);
2023
2024                 type = dbus_message_iter_get_arg_type(&entry);
2025
2026                 if (g_str_equal(key, "Method") == TRUE) {
2027                         const char *str;
2028
2029                         if (type != DBUS_TYPE_STRING)
2030                                 return -EINVAL;
2031
2032                         dbus_message_iter_get_basic(&entry, &str);
2033                         method = __connman_ipconfig_string2method(str);
2034                 } else if (g_str_equal(key, "Address") == TRUE) {
2035                         if (type != DBUS_TYPE_STRING)
2036                                 return -EINVAL;
2037
2038                         dbus_message_iter_get_basic(&entry, &address);
2039                 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
2040                         if (type != DBUS_TYPE_STRING)
2041                                 return -EINVAL;
2042
2043                         dbus_message_iter_get_basic(&entry,
2044                                                         &prefix_length_string);
2045
2046                         prefix_length = atoi(prefix_length_string);
2047                         if (prefix_length < 0 || prefix_length > 128)
2048                                 return -EINVAL;
2049
2050                 } else if (g_str_equal(key, "Netmask") == TRUE) {
2051                         if (type != DBUS_TYPE_STRING)
2052                                 return -EINVAL;
2053
2054                         dbus_message_iter_get_basic(&entry, &netmask);
2055                 } else if (g_str_equal(key, "Gateway") == TRUE) {
2056                         if (type != DBUS_TYPE_STRING)
2057                                 return -EINVAL;
2058
2059                         dbus_message_iter_get_basic(&entry, &gateway);
2060                 } else if (g_str_equal(key, "Privacy") == TRUE) {
2061                         if (type != DBUS_TYPE_STRING)
2062                                 return -EINVAL;
2063
2064                         dbus_message_iter_get_basic(&entry, &privacy_string);
2065                         privacy = string2privacy(privacy_string);
2066                 }
2067                 dbus_message_iter_next(&dict);
2068         }
2069
2070         DBG("method %d address %s netmask %s gateway %s prefix_length %d "
2071                 "privacy %s",
2072                 method, address, netmask, gateway, prefix_length,
2073                 privacy_string);
2074
2075         switch (method) {
2076         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2077         case CONNMAN_IPCONFIG_METHOD_FIXED:
2078                 return -EINVAL;
2079
2080         case CONNMAN_IPCONFIG_METHOD_OFF:
2081                 ipconfig->method = method;
2082                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2083                         disable_ipv6(ipconfig);
2084                 break;
2085
2086         case CONNMAN_IPCONFIG_METHOD_AUTO:
2087                 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2088                         return -EINVAL;
2089
2090                 ipconfig->method = method;
2091                 if (privacy_string != NULL)
2092                         ipconfig->ipv6_privacy_config = privacy;
2093                 enable_ipv6(ipconfig);
2094                 break;
2095
2096         case CONNMAN_IPCONFIG_METHOD_MANUAL:
2097                 if (address == NULL)
2098                         return -EINVAL;
2099
2100                 ipconfig->method = method;
2101
2102                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2103                         connman_ipaddress_set_ipv4(ipconfig->address,
2104                                                 address, netmask, gateway);
2105                 else
2106                         return connman_ipaddress_set_ipv6(
2107                                         ipconfig->address, address,
2108                                                 prefix_length, gateway);
2109                 break;
2110
2111         case CONNMAN_IPCONFIG_METHOD_DHCP:
2112                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2113                         return -EOPNOTSUPP;
2114
2115                 ipconfig->method = method;
2116                 break;
2117         }
2118
2119         return 0;
2120 }
2121
2122 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2123                                                         DBusMessageIter *iter)
2124 {
2125         struct connman_ipdevice *ipdevice;
2126         const char *method = "auto";
2127
2128         connman_dbus_dict_append_basic(iter, "Method",
2129                                                 DBUS_TYPE_STRING, &method);
2130
2131         ipdevice = g_hash_table_lookup(ipdevice_hash,
2132                                         GINT_TO_POINTER(ipconfig->index));
2133         if (ipdevice == NULL)
2134                 return;
2135
2136         if (ipdevice->ifname != NULL)
2137                 connman_dbus_dict_append_basic(iter, "Interface",
2138                                         DBUS_TYPE_STRING, &ipdevice->ifname);
2139
2140         if (ipdevice->address != NULL)
2141                 connman_dbus_dict_append_basic(iter, "Address",
2142                                         DBUS_TYPE_STRING, &ipdevice->address);
2143
2144         if (ipdevice->mtu > 0)
2145                 connman_dbus_dict_append_basic(iter, "MTU",
2146                                         DBUS_TYPE_UINT16, &ipdevice->mtu);
2147 }
2148
2149 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2150                 GKeyFile *keyfile, const char *identifier, const char *prefix)
2151 {
2152         char *method;
2153         char *key;
2154         char *str;
2155
2156         DBG("ipconfig %p identifier %s", ipconfig, identifier);
2157
2158         key = g_strdup_printf("%smethod", prefix);
2159         method = g_key_file_get_string(keyfile, identifier, key, NULL);
2160         if (method == NULL) {
2161                 switch (ipconfig->type) {
2162                 case CONNMAN_IPCONFIG_TYPE_IPV4:
2163                         ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2164                         break;
2165                 case CONNMAN_IPCONFIG_TYPE_IPV6:
2166                         ipconfig->method = CONNMAN_IPCONFIG_METHOD_AUTO;
2167                         break;
2168                 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2169                         ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2170                         break;
2171                 }
2172         } else
2173                 ipconfig->method = __connman_ipconfig_string2method(method);
2174
2175         if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2176                 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2177
2178         if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2179                 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2180                         ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2181                         char *privacy;
2182                         char *pprefix = g_strdup_printf("%sprivacy", prefix);
2183                         privacy = g_key_file_get_string(keyfile, identifier,
2184                                                         pprefix, NULL);
2185                         ipconfig->ipv6_privacy_config = string2privacy(privacy);
2186                         g_free(pprefix);
2187                         g_free(privacy);
2188
2189                         __connman_ipconfig_enable(ipconfig);
2190                         enable_ipv6(ipconfig);
2191                 }
2192         }
2193
2194         g_free(method);
2195         g_free(key);
2196
2197         key = g_strdup_printf("%snetmask_prefixlen", prefix);
2198         ipconfig->address->prefixlen = g_key_file_get_integer(
2199                                 keyfile, identifier, key, NULL);
2200         g_free(key);
2201
2202         key = g_strdup_printf("%slocal_address", prefix);
2203         ipconfig->address->local = g_key_file_get_string(
2204                         keyfile, identifier, key, NULL);
2205         g_free(key);
2206
2207         key = g_strdup_printf("%speer_address", prefix);
2208         ipconfig->address->peer = g_key_file_get_string(
2209                                 keyfile, identifier, key, NULL);
2210         g_free(key);
2211
2212         key = g_strdup_printf("%sbroadcast_address", prefix);
2213         ipconfig->address->broadcast = g_key_file_get_string(
2214                                 keyfile, identifier, key, NULL);
2215         g_free(key);
2216
2217         key = g_strdup_printf("%sgateway", prefix);
2218         ipconfig->address->gateway = g_key_file_get_string(
2219                                 keyfile, identifier, key, NULL);
2220         g_free(key);
2221
2222         key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2223         str = g_key_file_get_string(keyfile, identifier, key, NULL);
2224         if (str != NULL) {
2225                 g_free(ipconfig->last_dhcp_address);
2226                 ipconfig->last_dhcp_address = str;
2227         }
2228         g_free(key);
2229
2230         return 0;
2231 }
2232
2233 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2234                 GKeyFile *keyfile, const char *identifier, const char *prefix)
2235 {
2236         const char *method;
2237         char *key;
2238
2239         DBG("ipconfig %p identifier %s", ipconfig, identifier);
2240
2241         method = __connman_ipconfig_method2string(ipconfig->method);
2242
2243         key = g_strdup_printf("%smethod", prefix);
2244         g_key_file_set_string(keyfile, identifier, key, method);
2245         g_free(key);
2246
2247         if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2248                 const char *privacy;
2249                 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2250                 key = g_strdup_printf("%sprivacy", prefix);
2251                 g_key_file_set_string(keyfile, identifier, key, privacy);
2252                 g_free(key);
2253         }
2254
2255         switch (ipconfig->method) {
2256         case CONNMAN_IPCONFIG_METHOD_FIXED:
2257         case CONNMAN_IPCONFIG_METHOD_MANUAL:
2258                 break;
2259         case CONNMAN_IPCONFIG_METHOD_DHCP:
2260                 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2261                 if (ipconfig->last_dhcp_address != NULL &&
2262                                 strlen(ipconfig->last_dhcp_address) > 0)
2263                         g_key_file_set_string(keyfile, identifier, key,
2264                                         ipconfig->last_dhcp_address);
2265                 else
2266                         g_key_file_remove_key(keyfile, identifier, key, NULL);
2267                 g_free(key);
2268                 /* fall through */
2269         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2270         case CONNMAN_IPCONFIG_METHOD_OFF:
2271         case CONNMAN_IPCONFIG_METHOD_AUTO:
2272                 return 0;
2273         }
2274
2275         key = g_strdup_printf("%snetmask_prefixlen", prefix);
2276         g_key_file_set_integer(keyfile, identifier,
2277                         key, ipconfig->address->prefixlen);
2278         g_free(key);
2279
2280         key = g_strdup_printf("%slocal_address", prefix);
2281         if (ipconfig->address->local != NULL)
2282                 g_key_file_set_string(keyfile, identifier,
2283                                 key, ipconfig->address->local);
2284         g_free(key);
2285
2286         key = g_strdup_printf("%speer_address", prefix);
2287         if (ipconfig->address->peer != NULL)
2288                 g_key_file_set_string(keyfile, identifier,
2289                                 key, ipconfig->address->peer);
2290         g_free(key);
2291
2292         key = g_strdup_printf("%sbroadcast_address", prefix);
2293         if (ipconfig->address->broadcast != NULL)
2294                 g_key_file_set_string(keyfile, identifier,
2295                         key, ipconfig->address->broadcast);
2296         g_free(key);
2297
2298         key = g_strdup_printf("%sgateway", prefix);
2299         if (ipconfig->address->gateway != NULL)
2300                 g_key_file_set_string(keyfile, identifier,
2301                         key, ipconfig->address->gateway);
2302         g_free(key);
2303
2304         return 0;
2305 }
2306
2307 int __connman_ipconfig_init(void)
2308 {
2309         DBG("");
2310
2311         ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2312                                                         NULL, free_ipdevice);
2313
2314         return 0;
2315 }
2316
2317 void __connman_ipconfig_cleanup(void)
2318 {
2319         DBG("");
2320
2321         g_hash_table_destroy(ipdevice_hash);
2322         ipdevice_hash = NULL;
2323 }