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