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