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