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