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