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