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