94c0c2d29f57b019599e9fc8153555be41f281f7
[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 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 *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
1301 {
1302         DBG("ipconfig %p refcount %d", ipconfig, ipconfig->refcount + 1);
1303
1304         __sync_fetch_and_add(&ipconfig->refcount, 1);
1305
1306         return ipconfig;
1307 }
1308
1309 /**
1310  * connman_ipconfig_unref:
1311  * @ipconfig: ipconfig structure
1312  *
1313  * Decrease reference counter of ipconfig
1314  */
1315 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
1316 {
1317         if (ipconfig == NULL)
1318                 return;
1319
1320         DBG("ipconfig %p refcount %d", ipconfig, ipconfig->refcount - 1);
1321
1322         if (__sync_fetch_and_sub(&ipconfig->refcount, 1) != 1)
1323                 return;
1324
1325         if (__connman_ipconfig_disable(ipconfig) < 0)
1326                 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1327
1328         connman_ipconfig_set_ops(ipconfig, NULL);
1329
1330         if (ipconfig->origin != NULL) {
1331                 connman_ipconfig_unref(ipconfig->origin);
1332                 ipconfig->origin = NULL;
1333         }
1334
1335         connman_ipaddress_free(ipconfig->system);
1336         connman_ipaddress_free(ipconfig->address);
1337         g_free(ipconfig->last_dhcp_address);
1338         g_free(ipconfig);
1339 }
1340
1341 /**
1342  * connman_ipconfig_get_data:
1343  * @ipconfig: ipconfig structure
1344  *
1345  * Get private data pointer
1346  */
1347 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1348 {
1349         if (ipconfig == NULL)
1350                 return NULL;
1351
1352         return ipconfig->ops_data;
1353 }
1354
1355 /**
1356  * connman_ipconfig_set_data:
1357  * @ipconfig: ipconfig structure
1358  * @data: data pointer
1359  *
1360  * Set private data pointer
1361  */
1362 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1363 {
1364         ipconfig->ops_data = data;
1365 }
1366
1367 /**
1368  * connman_ipconfig_get_index:
1369  * @ipconfig: ipconfig structure
1370  *
1371  * Get interface index
1372  */
1373 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1374 {
1375         if (ipconfig == NULL)
1376                 return -1;
1377
1378         if (ipconfig->origin != NULL)
1379                 return ipconfig->origin->index;
1380
1381         return ipconfig->index;
1382 }
1383
1384 /**
1385  * connman_ipconfig_get_ifname:
1386  * @ipconfig: ipconfig structure
1387  *
1388  * Get interface name
1389  */
1390 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1391 {
1392         struct connman_ipdevice *ipdevice;
1393
1394         if (ipconfig == NULL)
1395                 return NULL;
1396
1397         if (ipconfig->index < 0)
1398                 return NULL;
1399
1400         ipdevice = g_hash_table_lookup(ipdevice_hash,
1401                                         GINT_TO_POINTER(ipconfig->index));
1402         if (ipdevice == NULL)
1403                 return NULL;
1404
1405         return ipdevice->ifname;
1406 }
1407
1408 /**
1409  * connman_ipconfig_set_ops:
1410  * @ipconfig: ipconfig structure
1411  * @ops: operation callbacks
1412  *
1413  * Set the operation callbacks
1414  */
1415 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1416                                 const struct connman_ipconfig_ops *ops)
1417 {
1418         ipconfig->ops = ops;
1419 }
1420
1421 /**
1422  * connman_ipconfig_set_method:
1423  * @ipconfig: ipconfig structure
1424  * @method: configuration method
1425  *
1426  * Set the configuration method
1427  */
1428 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1429                                         enum connman_ipconfig_method method)
1430 {
1431         ipconfig->method = method;
1432
1433         return 0;
1434 }
1435
1436 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1437 {
1438         if (ipconfig == NULL)
1439                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1440
1441         return ipconfig->method;
1442 }
1443
1444 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1445 {
1446         DBG("");
1447
1448         switch (ipconfig->method) {
1449         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1450         case CONNMAN_IPCONFIG_METHOD_OFF:
1451         case CONNMAN_IPCONFIG_METHOD_AUTO:
1452                 break;
1453         case CONNMAN_IPCONFIG_METHOD_FIXED:
1454         case CONNMAN_IPCONFIG_METHOD_DHCP:
1455         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1456                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1457                         return connman_inet_set_address(ipconfig->index,
1458                                                         ipconfig->address);
1459                 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1460                         return connman_inet_set_ipv6_address(
1461                                         ipconfig->index, ipconfig->address);
1462         }
1463
1464         return 0;
1465 }
1466
1467 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1468 {
1469         int err;
1470
1471         DBG("");
1472
1473         if (ipconfig == NULL)
1474                 return 0;
1475
1476         DBG("method %d", ipconfig->method);
1477
1478         switch (ipconfig->method) {
1479         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1480         case CONNMAN_IPCONFIG_METHOD_OFF:
1481         case CONNMAN_IPCONFIG_METHOD_AUTO:
1482                 break;
1483         case CONNMAN_IPCONFIG_METHOD_FIXED:
1484         case CONNMAN_IPCONFIG_METHOD_DHCP:
1485         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1486                 err = __connman_ipconfig_address_unset(ipconfig);
1487                 connman_ipaddress_clear(ipconfig->address);
1488
1489                 return err;
1490         }
1491
1492         return 0;
1493 }
1494
1495 int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig)
1496 {
1497         int err;
1498
1499         DBG("");
1500
1501         if (ipconfig == NULL)
1502                 return 0;
1503
1504         DBG("method %d", ipconfig->method);
1505
1506         switch (ipconfig->method) {
1507         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1508         case CONNMAN_IPCONFIG_METHOD_OFF:
1509         case CONNMAN_IPCONFIG_METHOD_AUTO:
1510                 break;
1511         case CONNMAN_IPCONFIG_METHOD_FIXED:
1512         case CONNMAN_IPCONFIG_METHOD_DHCP:
1513         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1514                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1515                         err = connman_inet_clear_address(ipconfig->index,
1516                                                         ipconfig->address);
1517                 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1518                         err = connman_inet_clear_ipv6_address(
1519                                                 ipconfig->index,
1520                                                 ipconfig->address->local,
1521                                                 ipconfig->address->prefixlen);
1522                 else
1523                         err = -EINVAL;
1524
1525                 return err;
1526         }
1527
1528         return 0;
1529 }
1530
1531 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1532                                                         const char *url)
1533 {
1534         struct connman_ipdevice *ipdevice;
1535
1536         DBG("ipconfig %p", ipconfig);
1537
1538         if (ipconfig == NULL || ipconfig->index < 0)
1539                 return -ENODEV;
1540
1541         ipdevice = g_hash_table_lookup(ipdevice_hash,
1542                                         GINT_TO_POINTER(ipconfig->index));
1543         if (ipdevice == NULL)
1544                 return -ENXIO;
1545
1546         g_free(ipdevice->pac);
1547         ipdevice->pac = g_strdup(url);
1548
1549         return 0;
1550 }
1551
1552 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1553 {
1554         struct connman_ipdevice *ipdevice;
1555
1556         DBG("ipconfig %p", ipconfig);
1557
1558         if (ipconfig == NULL || ipconfig->index < 0)
1559                 return NULL;
1560
1561         ipdevice = g_hash_table_lookup(ipdevice_hash,
1562                                         GINT_TO_POINTER(ipconfig->index));
1563         if (ipdevice == NULL)
1564                 return NULL;
1565
1566         return ipdevice->pac;
1567 }
1568
1569 void __connman_ipconfig_set_dhcp_address(struct connman_ipconfig *ipconfig,
1570                                         const char *address)
1571 {
1572         if (ipconfig == NULL)
1573                 return;
1574
1575         g_free(ipconfig->last_dhcp_address);
1576         ipconfig->last_dhcp_address = g_strdup(address);
1577 }
1578
1579 char *__connman_ipconfig_get_dhcp_address(struct connman_ipconfig *ipconfig)
1580 {
1581         if (ipconfig == NULL)
1582                 return NULL;
1583
1584         return ipconfig->last_dhcp_address;
1585 }
1586
1587 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1588 {
1589         struct connman_ipdevice *ipdevice;
1590
1591         DBG("");
1592
1593         ipdevice = g_hash_table_lookup(ipdevice_hash,
1594                                         GINT_TO_POINTER(ipconfig->index));
1595         if (ipdevice == NULL)
1596                 return;
1597
1598         set_ipv6_state(ipdevice->ifname, FALSE);
1599 }
1600
1601 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1602 {
1603         struct connman_ipdevice *ipdevice;
1604
1605         DBG("");
1606
1607         ipdevice = g_hash_table_lookup(ipdevice_hash,
1608                                         GINT_TO_POINTER(ipconfig->index));
1609         if (ipdevice == NULL)
1610                 return;
1611
1612         if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1613                 set_ipv6_privacy(ipdevice->ifname,
1614                                 ipconfig->ipv6_privacy_config);
1615
1616         set_ipv6_state(ipdevice->ifname, TRUE);
1617 }
1618
1619 void __connman_ipconfig_enable_ipv6(struct connman_ipconfig *ipconfig)
1620 {
1621         if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1622                 return;
1623
1624         enable_ipv6(ipconfig);
1625 }
1626
1627 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1628 {
1629         if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1630                 return;
1631
1632         disable_ipv6(ipconfig);
1633 }
1634
1635 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1636 {
1637         struct connman_ipdevice *ipdevice;
1638         gboolean up = FALSE, down = FALSE;
1639         gboolean lower_up = FALSE, lower_down = FALSE;
1640         enum connman_ipconfig_type type;
1641
1642         DBG("ipconfig %p", ipconfig);
1643
1644         if (ipconfig == NULL || ipconfig->index < 0)
1645                 return -ENODEV;
1646
1647         ipdevice = g_hash_table_lookup(ipdevice_hash,
1648                                         GINT_TO_POINTER(ipconfig->index));
1649         if (ipdevice == NULL)
1650                 return -ENXIO;
1651
1652         if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1653                 if (ipdevice->config_ipv4 == ipconfig)
1654                         return -EALREADY;
1655                 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1656         } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1657                 if (ipdevice->config_ipv6 == ipconfig)
1658                         return -EALREADY;
1659                 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1660                 enable_ipv6(ipconfig);
1661         } else
1662                 return -EINVAL;
1663
1664         if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1665                                         ipdevice->config_ipv4 != NULL) {
1666                 ipconfig_list = g_list_remove(ipconfig_list,
1667                                                         ipdevice->config_ipv4);
1668
1669                 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1670
1671                 connman_ipconfig_unref(ipdevice->config_ipv4);
1672         }
1673
1674         if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1675                                         ipdevice->config_ipv6 != NULL) {
1676                 ipconfig_list = g_list_remove(ipconfig_list,
1677                                                         ipdevice->config_ipv6);
1678
1679                 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1680
1681                 connman_ipconfig_unref(ipdevice->config_ipv6);
1682         }
1683
1684         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1685                 ipdevice->config_ipv4 = connman_ipconfig_ref(ipconfig);
1686         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1687                 ipdevice->config_ipv6 = connman_ipconfig_ref(ipconfig);
1688
1689         ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1690
1691         if (ipdevice->flags & IFF_UP)
1692                 up = TRUE;
1693         else
1694                 down = TRUE;
1695
1696         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1697                         (IFF_RUNNING | IFF_LOWER_UP))
1698                 lower_up = TRUE;
1699         else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1700                 lower_down = TRUE;
1701
1702         if (up == TRUE && ipconfig->ops->up)
1703                 ipconfig->ops->up(ipconfig);
1704         if (lower_up == TRUE && ipconfig->ops->lower_up)
1705                 ipconfig->ops->lower_up(ipconfig);
1706
1707         if (lower_down == TRUE && ipconfig->ops->lower_down)
1708                 ipconfig->ops->lower_down(ipconfig);
1709         if (down == TRUE && ipconfig->ops->down)
1710                 ipconfig->ops->down(ipconfig);
1711
1712         return 0;
1713 }
1714
1715 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1716 {
1717         struct connman_ipdevice *ipdevice;
1718
1719         DBG("ipconfig %p", ipconfig);
1720
1721         if (ipconfig == NULL || ipconfig->index < 0)
1722                 return -ENODEV;
1723
1724         ipdevice = g_hash_table_lookup(ipdevice_hash,
1725                                         GINT_TO_POINTER(ipconfig->index));
1726         if (ipdevice == NULL)
1727                 return -ENXIO;
1728
1729         if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1730                 return -EINVAL;
1731
1732         if (ipdevice->config_ipv4 == ipconfig) {
1733                 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1734
1735                 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1736                 connman_ipconfig_unref(ipdevice->config_ipv4);
1737                 ipdevice->config_ipv4 = NULL;
1738                 return 0;
1739         }
1740
1741         if (ipdevice->config_ipv6 == ipconfig) {
1742                 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1743
1744                 if (ipdevice->config_ipv6->method ==
1745                                                 CONNMAN_IPCONFIG_METHOD_AUTO)
1746                         disable_ipv6(ipdevice->config_ipv6);
1747
1748                 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1749                 connman_ipconfig_unref(ipdevice->config_ipv6);
1750                 ipdevice->config_ipv6 = NULL;
1751                 return 0;
1752         }
1753
1754         return -EINVAL;
1755 }
1756
1757 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1758 {
1759         switch (method) {
1760         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1761                 break;
1762         case CONNMAN_IPCONFIG_METHOD_OFF:
1763                 return "off";
1764         case CONNMAN_IPCONFIG_METHOD_FIXED:
1765                 return "fixed";
1766         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1767                 return "manual";
1768         case CONNMAN_IPCONFIG_METHOD_DHCP:
1769                 return "dhcp";
1770         case CONNMAN_IPCONFIG_METHOD_AUTO:
1771                 return "auto";
1772         }
1773
1774         return NULL;
1775 }
1776
1777 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1778 {
1779         if (g_strcmp0(method, "off") == 0)
1780                 return CONNMAN_IPCONFIG_METHOD_OFF;
1781         else if (g_strcmp0(method, "fixed") == 0)
1782                 return CONNMAN_IPCONFIG_METHOD_FIXED;
1783         else if (g_strcmp0(method, "manual") == 0)
1784                 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1785         else if (g_strcmp0(method, "dhcp") == 0)
1786                 return CONNMAN_IPCONFIG_METHOD_DHCP;
1787         else if (g_strcmp0(method, "auto") == 0)
1788                 return CONNMAN_IPCONFIG_METHOD_AUTO;
1789         else
1790                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1791 }
1792
1793 static const char *privacy2string(int privacy)
1794 {
1795         if (privacy <= 0)
1796                 return "disabled";
1797         else if (privacy == 1)
1798                 return "enabled";
1799         else if (privacy > 1)
1800                 return "prefered";
1801
1802         return "disabled";
1803 }
1804
1805 static int string2privacy(const char *privacy)
1806 {
1807         if (g_strcmp0(privacy, "disabled") == 0)
1808                 return 0;
1809         else if (g_strcmp0(privacy, "enabled") == 0)
1810                 return 1;
1811         else if (g_strcmp0(privacy, "prefered") == 0)
1812                 return 2;
1813         else
1814                 return 0;
1815 }
1816
1817 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1818                                                         DBusMessageIter *iter)
1819 {
1820         const char *str;
1821
1822         DBG("");
1823
1824         if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1825                 return;
1826
1827         str = __connman_ipconfig_method2string(ipconfig->method);
1828         if (str == NULL)
1829                 return;
1830
1831         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1832
1833         if (ipconfig->system == NULL)
1834                 return;
1835
1836         if (ipconfig->system->local != NULL) {
1837                 in_addr_t addr;
1838                 struct in_addr netmask;
1839                 char *mask;
1840
1841                 connman_dbus_dict_append_basic(iter, "Address",
1842                                 DBUS_TYPE_STRING, &ipconfig->system->local);
1843
1844                 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1845                 netmask.s_addr = htonl(addr);
1846                 mask = inet_ntoa(netmask);
1847                 connman_dbus_dict_append_basic(iter, "Netmask",
1848                                                 DBUS_TYPE_STRING, &mask);
1849         }
1850
1851         if (ipconfig->system->gateway != NULL)
1852                 connman_dbus_dict_append_basic(iter, "Gateway",
1853                                 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1854 }
1855
1856 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1857                                         DBusMessageIter *iter,
1858                                         struct connman_ipconfig *ipconfig_ipv4)
1859 {
1860         const char *str, *privacy;
1861
1862         DBG("");
1863
1864         if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1865                 return;
1866
1867         str = __connman_ipconfig_method2string(ipconfig->method);
1868         if (str == NULL)
1869                 return;
1870
1871         if (ipconfig_ipv4 != NULL &&
1872                         ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1873                 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1874                         str = "6to4";
1875         }
1876
1877         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1878
1879         if (ipconfig->system == NULL)
1880                 return;
1881
1882         if (ipconfig->system->local != NULL) {
1883                 connman_dbus_dict_append_basic(iter, "Address",
1884                                 DBUS_TYPE_STRING, &ipconfig->system->local);
1885                 connman_dbus_dict_append_basic(iter, "PrefixLength",
1886                                                 DBUS_TYPE_BYTE,
1887                                                 &ipconfig->system->prefixlen);
1888         }
1889
1890         if (ipconfig->system->gateway != NULL)
1891                 connman_dbus_dict_append_basic(iter, "Gateway",
1892                                 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1893
1894         privacy = privacy2string(ipconfig->ipv6_privacy_config);
1895         connman_dbus_dict_append_basic(iter, "Privacy",
1896                                 DBUS_TYPE_STRING, &privacy);
1897 }
1898
1899 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1900                                                         DBusMessageIter *iter)
1901 {
1902         const char *str, *privacy;
1903
1904         DBG("");
1905
1906         str = __connman_ipconfig_method2string(ipconfig->method);
1907         if (str == NULL)
1908                 return;
1909
1910         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1911
1912         switch (ipconfig->method) {
1913         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1914         case CONNMAN_IPCONFIG_METHOD_OFF:
1915         case CONNMAN_IPCONFIG_METHOD_DHCP:
1916                 return;
1917         case CONNMAN_IPCONFIG_METHOD_FIXED:
1918         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1919         case CONNMAN_IPCONFIG_METHOD_AUTO:
1920                 break;
1921         }
1922
1923         if (ipconfig->address == NULL)
1924                 return;
1925
1926         if (ipconfig->address->local != NULL) {
1927                 connman_dbus_dict_append_basic(iter, "Address",
1928                                 DBUS_TYPE_STRING, &ipconfig->address->local);
1929                 connman_dbus_dict_append_basic(iter, "PrefixLength",
1930                                                 DBUS_TYPE_BYTE,
1931                                                 &ipconfig->address->prefixlen);
1932         }
1933
1934         if (ipconfig->address->gateway != NULL)
1935                 connman_dbus_dict_append_basic(iter, "Gateway",
1936                                 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1937
1938         privacy = privacy2string(ipconfig->ipv6_privacy_config);
1939         connman_dbus_dict_append_basic(iter, "Privacy",
1940                                 DBUS_TYPE_STRING, &privacy);
1941 }
1942
1943 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1944                                                         DBusMessageIter *iter)
1945 {
1946         const char *str;
1947
1948         DBG("");
1949
1950         str = __connman_ipconfig_method2string(ipconfig->method);
1951         if (str == NULL)
1952                 return;
1953
1954         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1955
1956         switch (ipconfig->method) {
1957         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1958         case CONNMAN_IPCONFIG_METHOD_OFF:
1959         case CONNMAN_IPCONFIG_METHOD_DHCP:
1960         case CONNMAN_IPCONFIG_METHOD_AUTO:
1961                 return;
1962         case CONNMAN_IPCONFIG_METHOD_FIXED:
1963         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1964                 break;
1965         }
1966
1967         if (ipconfig->address == NULL)
1968                 return;
1969
1970         if (ipconfig->address->local != NULL) {
1971                 in_addr_t addr;
1972                 struct in_addr netmask;
1973                 char *mask;
1974
1975                 connman_dbus_dict_append_basic(iter, "Address",
1976                                 DBUS_TYPE_STRING, &ipconfig->address->local);
1977
1978                 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1979                 netmask.s_addr = htonl(addr);
1980                 mask = inet_ntoa(netmask);
1981                 connman_dbus_dict_append_basic(iter, "Netmask",
1982                                                 DBUS_TYPE_STRING, &mask);
1983         }
1984
1985         if (ipconfig->address->gateway != NULL)
1986                 connman_dbus_dict_append_basic(iter, "Gateway",
1987                                 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1988 }
1989
1990 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1991                                                         DBusMessageIter *array)
1992 {
1993         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1994         const char *address = NULL, *netmask = NULL, *gateway = NULL,
1995                 *prefix_length_string = NULL, *privacy_string = NULL;
1996         int prefix_length = 0, privacy = 0;
1997         DBusMessageIter dict;
1998
1999         DBG("ipconfig %p", ipconfig);
2000
2001         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
2002                 return -EINVAL;
2003
2004         dbus_message_iter_recurse(array, &dict);
2005
2006         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2007                 DBusMessageIter entry;
2008                 const char *key;
2009                 int type;
2010
2011                 dbus_message_iter_recurse(&dict, &entry);
2012
2013                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2014                         return -EINVAL;
2015
2016                 dbus_message_iter_get_basic(&entry, &key);
2017                 dbus_message_iter_next(&entry);
2018
2019                 type = dbus_message_iter_get_arg_type(&entry);
2020
2021                 if (g_str_equal(key, "Method") == TRUE) {
2022                         const char *str;
2023
2024                         if (type != DBUS_TYPE_STRING)
2025                                 return -EINVAL;
2026
2027                         dbus_message_iter_get_basic(&entry, &str);
2028                         method = __connman_ipconfig_string2method(str);
2029                 } else if (g_str_equal(key, "Address") == TRUE) {
2030                         if (type != DBUS_TYPE_STRING)
2031                                 return -EINVAL;
2032
2033                         dbus_message_iter_get_basic(&entry, &address);
2034                 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
2035                         if (type != DBUS_TYPE_STRING)
2036                                 return -EINVAL;
2037
2038                         dbus_message_iter_get_basic(&entry,
2039                                                         &prefix_length_string);
2040
2041                         prefix_length = atoi(prefix_length_string);
2042                         if (prefix_length < 0 || prefix_length > 128)
2043                                 return -EINVAL;
2044
2045                 } else if (g_str_equal(key, "Netmask") == TRUE) {
2046                         if (type != DBUS_TYPE_STRING)
2047                                 return -EINVAL;
2048
2049                         dbus_message_iter_get_basic(&entry, &netmask);
2050                 } else if (g_str_equal(key, "Gateway") == TRUE) {
2051                         if (type != DBUS_TYPE_STRING)
2052                                 return -EINVAL;
2053
2054                         dbus_message_iter_get_basic(&entry, &gateway);
2055                 } else if (g_str_equal(key, "Privacy") == TRUE) {
2056                         if (type != DBUS_TYPE_STRING)
2057                                 return -EINVAL;
2058
2059                         dbus_message_iter_get_basic(&entry, &privacy_string);
2060                         privacy = string2privacy(privacy_string);
2061                 }
2062                 dbus_message_iter_next(&dict);
2063         }
2064
2065         DBG("method %d address %s netmask %s gateway %s prefix_length %d "
2066                 "privacy %s",
2067                 method, address, netmask, gateway, prefix_length,
2068                 privacy_string);
2069
2070         switch (method) {
2071         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2072         case CONNMAN_IPCONFIG_METHOD_FIXED:
2073                 return -EINVAL;
2074
2075         case CONNMAN_IPCONFIG_METHOD_OFF:
2076                 ipconfig->method = method;
2077                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2078                         disable_ipv6(ipconfig);
2079                 break;
2080
2081         case CONNMAN_IPCONFIG_METHOD_AUTO:
2082                 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2083                         return -EINVAL;
2084
2085                 ipconfig->method = method;
2086                 if (privacy_string != NULL)
2087                         ipconfig->ipv6_privacy_config = privacy;
2088                 enable_ipv6(ipconfig);
2089                 break;
2090
2091         case CONNMAN_IPCONFIG_METHOD_MANUAL:
2092                 if (address == NULL)
2093                         return -EINVAL;
2094
2095                 ipconfig->method = method;
2096
2097                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2098                         connman_ipaddress_set_ipv4(ipconfig->address,
2099                                                 address, netmask, gateway);
2100                 else
2101                         return connman_ipaddress_set_ipv6(
2102                                         ipconfig->address, address,
2103                                                 prefix_length, gateway);
2104                 break;
2105
2106         case CONNMAN_IPCONFIG_METHOD_DHCP:
2107                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2108                         return -EOPNOTSUPP;
2109
2110                 ipconfig->method = method;
2111                 break;
2112         }
2113
2114         return 0;
2115 }
2116
2117 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2118                                                         DBusMessageIter *iter)
2119 {
2120         struct connman_ipdevice *ipdevice;
2121         const char *method = "auto";
2122
2123         connman_dbus_dict_append_basic(iter, "Method",
2124                                                 DBUS_TYPE_STRING, &method);
2125
2126         ipdevice = g_hash_table_lookup(ipdevice_hash,
2127                                         GINT_TO_POINTER(ipconfig->index));
2128         if (ipdevice == NULL)
2129                 return;
2130
2131         if (ipdevice->ifname != NULL)
2132                 connman_dbus_dict_append_basic(iter, "Interface",
2133                                         DBUS_TYPE_STRING, &ipdevice->ifname);
2134
2135         if (ipdevice->address != NULL)
2136                 connman_dbus_dict_append_basic(iter, "Address",
2137                                         DBUS_TYPE_STRING, &ipdevice->address);
2138
2139         if (ipdevice->mtu > 0)
2140                 connman_dbus_dict_append_basic(iter, "MTU",
2141                                         DBUS_TYPE_UINT16, &ipdevice->mtu);
2142 }
2143
2144 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2145                 GKeyFile *keyfile, const char *identifier, const char *prefix)
2146 {
2147         char *method;
2148         char *key;
2149         char *str;
2150
2151         DBG("ipconfig %p identifier %s", ipconfig, identifier);
2152
2153         key = g_strdup_printf("%smethod", prefix);
2154         method = g_key_file_get_string(keyfile, identifier, key, NULL);
2155         if (method == NULL) {
2156                 switch (ipconfig->type) {
2157                 case CONNMAN_IPCONFIG_TYPE_IPV4:
2158                         ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2159                         break;
2160                 case CONNMAN_IPCONFIG_TYPE_IPV6:
2161                         ipconfig->method = CONNMAN_IPCONFIG_METHOD_AUTO;
2162                         break;
2163                 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
2164                         ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2165                         break;
2166                 }
2167         } else
2168                 ipconfig->method = __connman_ipconfig_string2method(method);
2169
2170         if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2171                 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2172
2173         if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2174                 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2175                         ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2176                         char *privacy;
2177                         char *pprefix = g_strdup_printf("%sprivacy", prefix);
2178                         privacy = g_key_file_get_string(keyfile, identifier,
2179                                                         pprefix, NULL);
2180                         ipconfig->ipv6_privacy_config = string2privacy(privacy);
2181                         g_free(pprefix);
2182                         g_free(privacy);
2183
2184                         __connman_ipconfig_enable(ipconfig);
2185                         enable_ipv6(ipconfig);
2186                 }
2187         }
2188
2189         g_free(method);
2190         g_free(key);
2191
2192         key = g_strdup_printf("%snetmask_prefixlen", prefix);
2193         ipconfig->address->prefixlen = g_key_file_get_integer(
2194                                 keyfile, identifier, key, NULL);
2195         g_free(key);
2196
2197         key = g_strdup_printf("%slocal_address", prefix);
2198         ipconfig->address->local = g_key_file_get_string(
2199                         keyfile, identifier, key, NULL);
2200         g_free(key);
2201
2202         key = g_strdup_printf("%speer_address", prefix);
2203         ipconfig->address->peer = g_key_file_get_string(
2204                                 keyfile, identifier, key, NULL);
2205         g_free(key);
2206
2207         key = g_strdup_printf("%sbroadcast_address", prefix);
2208         ipconfig->address->broadcast = g_key_file_get_string(
2209                                 keyfile, identifier, key, NULL);
2210         g_free(key);
2211
2212         key = g_strdup_printf("%sgateway", prefix);
2213         ipconfig->address->gateway = g_key_file_get_string(
2214                                 keyfile, identifier, key, NULL);
2215         g_free(key);
2216
2217         key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2218         str = g_key_file_get_string(keyfile, identifier, key, NULL);
2219         if (str != NULL) {
2220                 g_free(ipconfig->last_dhcp_address);
2221                 ipconfig->last_dhcp_address = str;
2222         }
2223         g_free(key);
2224
2225         return 0;
2226 }
2227
2228 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2229                 GKeyFile *keyfile, const char *identifier, const char *prefix)
2230 {
2231         const char *method;
2232         char *key;
2233
2234         DBG("ipconfig %p identifier %s", ipconfig, identifier);
2235
2236         method = __connman_ipconfig_method2string(ipconfig->method);
2237
2238         key = g_strdup_printf("%smethod", prefix);
2239         g_key_file_set_string(keyfile, identifier, key, method);
2240         g_free(key);
2241
2242         if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2243                 const char *privacy;
2244                 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2245                 key = g_strdup_printf("%sprivacy", prefix);
2246                 g_key_file_set_string(keyfile, identifier, key, privacy);
2247                 g_free(key);
2248         }
2249
2250         switch (ipconfig->method) {
2251         case CONNMAN_IPCONFIG_METHOD_FIXED:
2252         case CONNMAN_IPCONFIG_METHOD_MANUAL:
2253                 break;
2254         case CONNMAN_IPCONFIG_METHOD_DHCP:
2255                 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2256                 if (ipconfig->last_dhcp_address != NULL &&
2257                                 strlen(ipconfig->last_dhcp_address) > 0)
2258                         g_key_file_set_string(keyfile, identifier, key,
2259                                         ipconfig->last_dhcp_address);
2260                 else
2261                         g_key_file_remove_key(keyfile, identifier, key, NULL);
2262                 g_free(key);
2263                 /* fall through */
2264         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2265         case CONNMAN_IPCONFIG_METHOD_OFF:
2266         case CONNMAN_IPCONFIG_METHOD_AUTO:
2267                 return 0;
2268         }
2269
2270         key = g_strdup_printf("%snetmask_prefixlen", prefix);
2271         g_key_file_set_integer(keyfile, identifier,
2272                         key, ipconfig->address->prefixlen);
2273         g_free(key);
2274
2275         key = g_strdup_printf("%slocal_address", prefix);
2276         if (ipconfig->address->local != NULL)
2277                 g_key_file_set_string(keyfile, identifier,
2278                                 key, ipconfig->address->local);
2279         g_free(key);
2280
2281         key = g_strdup_printf("%speer_address", prefix);
2282         if (ipconfig->address->peer != NULL)
2283                 g_key_file_set_string(keyfile, identifier,
2284                                 key, ipconfig->address->peer);
2285         g_free(key);
2286
2287         key = g_strdup_printf("%sbroadcast_address", prefix);
2288         if (ipconfig->address->broadcast != NULL)
2289                 g_key_file_set_string(keyfile, identifier,
2290                         key, ipconfig->address->broadcast);
2291         g_free(key);
2292
2293         key = g_strdup_printf("%sgateway", prefix);
2294         if (ipconfig->address->gateway != NULL)
2295                 g_key_file_set_string(keyfile, identifier,
2296                         key, ipconfig->address->gateway);
2297         g_free(key);
2298
2299         return 0;
2300 }
2301
2302 int __connman_ipconfig_init(void)
2303 {
2304         DBG("");
2305
2306         ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2307                                                         NULL, free_ipdevice);
2308
2309         return 0;
2310 }
2311
2312 void __connman_ipconfig_cleanup(void)
2313 {
2314         DBG("");
2315
2316         g_hash_table_destroy(ipdevice_hash);
2317         ipdevice_hash = NULL;
2318 }