ipconfig: Add function to remove address from interface.
[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                 err = __connman_ipconfig_address_unset(ipconfig);
1405                 connman_ipaddress_clear(ipconfig->address);
1406
1407                 return err;
1408         }
1409
1410         return 0;
1411 }
1412
1413 int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig)
1414 {
1415         int err;
1416
1417         DBG("");
1418
1419         if (ipconfig == NULL)
1420                 return 0;
1421
1422         DBG("method %d", ipconfig->method);
1423
1424         switch (ipconfig->method) {
1425         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1426         case CONNMAN_IPCONFIG_METHOD_OFF:
1427         case CONNMAN_IPCONFIG_METHOD_AUTO:
1428                 break;
1429         case CONNMAN_IPCONFIG_METHOD_FIXED:
1430         case CONNMAN_IPCONFIG_METHOD_DHCP:
1431         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1432                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1433                         err = connman_inet_clear_address(ipconfig->index,
1434                                                         ipconfig->address);
1435                 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1436                         err = connman_inet_clear_ipv6_address(
1437                                                 ipconfig->index,
1438                                                 ipconfig->address->local,
1439                                                 ipconfig->address->prefixlen);
1440                 else
1441                         err = -EINVAL;
1442
1443                 return err;
1444         }
1445
1446         return 0;
1447 }
1448
1449 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1450                                                         const char *url)
1451 {
1452         struct connman_ipdevice *ipdevice;
1453
1454         DBG("ipconfig %p", ipconfig);
1455
1456         if (ipconfig == NULL || ipconfig->index < 0)
1457                 return -ENODEV;
1458
1459         ipdevice = g_hash_table_lookup(ipdevice_hash,
1460                                         GINT_TO_POINTER(ipconfig->index));
1461         if (ipdevice == NULL)
1462                 return -ENXIO;
1463
1464         g_free(ipdevice->pac);
1465         ipdevice->pac = g_strdup(url);
1466
1467         return 0;
1468 }
1469
1470 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1471 {
1472         struct connman_ipdevice *ipdevice;
1473
1474         DBG("ipconfig %p", ipconfig);
1475
1476         if (ipconfig == NULL || ipconfig->index < 0)
1477                 return NULL;
1478
1479         ipdevice = g_hash_table_lookup(ipdevice_hash,
1480                                         GINT_TO_POINTER(ipconfig->index));
1481         if (ipdevice == NULL)
1482                 return NULL;
1483
1484         return ipdevice->pac;
1485 }
1486
1487 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1488 {
1489         struct connman_ipdevice *ipdevice;
1490
1491         DBG("");
1492
1493         ipdevice = g_hash_table_lookup(ipdevice_hash,
1494                                         GINT_TO_POINTER(ipconfig->index));
1495         if (ipdevice == NULL)
1496                 return;
1497
1498         set_ipv6_state(ipdevice->ifname, FALSE);
1499 }
1500
1501 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1502 {
1503         struct connman_ipdevice *ipdevice;
1504
1505         DBG("");
1506
1507         ipdevice = g_hash_table_lookup(ipdevice_hash,
1508                                         GINT_TO_POINTER(ipconfig->index));
1509         if (ipdevice == NULL)
1510                 return;
1511
1512         if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1513                 set_ipv6_privacy(ipdevice->ifname,
1514                                 ipconfig->ipv6_privacy_config);
1515
1516         set_ipv6_state(ipdevice->ifname, TRUE);
1517 }
1518
1519 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1520 {
1521         if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1522                 return;
1523
1524         disable_ipv6(ipconfig);
1525 }
1526
1527 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1528 {
1529         struct connman_ipdevice *ipdevice;
1530         gboolean up = FALSE, down = FALSE;
1531         gboolean lower_up = FALSE, lower_down = FALSE;
1532         enum connman_ipconfig_type type;
1533
1534         DBG("ipconfig %p", ipconfig);
1535
1536         if (ipconfig == NULL || ipconfig->index < 0)
1537                 return -ENODEV;
1538
1539         ipdevice = g_hash_table_lookup(ipdevice_hash,
1540                                         GINT_TO_POINTER(ipconfig->index));
1541         if (ipdevice == NULL)
1542                 return -ENXIO;
1543
1544         if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1545                 if (ipdevice->config_ipv4 == ipconfig)
1546                         return -EALREADY;
1547                 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1548         } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1549                 if (ipdevice->config_ipv6 == ipconfig)
1550                         return -EALREADY;
1551                 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1552                 enable_ipv6(ipconfig);
1553         } else
1554                 return -EINVAL;
1555
1556         if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1557                                         ipdevice->config_ipv4 != NULL) {
1558                 ipconfig_list = g_list_remove(ipconfig_list,
1559                                                         ipdevice->config_ipv4);
1560
1561                 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1562
1563                 connman_ipconfig_unref(ipdevice->config_ipv4);
1564         }
1565
1566         if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1567                                         ipdevice->config_ipv6 != NULL) {
1568                 ipconfig_list = g_list_remove(ipconfig_list,
1569                                                         ipdevice->config_ipv6);
1570
1571                 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1572
1573                 connman_ipconfig_unref(ipdevice->config_ipv6);
1574         }
1575
1576         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1577                 ipdevice->config_ipv4 = connman_ipconfig_ref(ipconfig);
1578         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1579                 ipdevice->config_ipv6 = connman_ipconfig_ref(ipconfig);
1580
1581         ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1582
1583         if (ipdevice->flags & IFF_UP)
1584                 up = TRUE;
1585         else
1586                 down = TRUE;
1587
1588         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1589                         (IFF_RUNNING | IFF_LOWER_UP))
1590                 lower_up = TRUE;
1591         else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1592                 lower_down = TRUE;
1593
1594         if (up == TRUE && ipconfig->ops->up)
1595                 ipconfig->ops->up(ipconfig);
1596         if (lower_up == TRUE && ipconfig->ops->lower_up)
1597                 ipconfig->ops->lower_up(ipconfig);
1598
1599         if (lower_down == TRUE && ipconfig->ops->lower_down)
1600                 ipconfig->ops->lower_down(ipconfig);
1601         if (down == TRUE && ipconfig->ops->down)
1602                 ipconfig->ops->down(ipconfig);
1603
1604         return 0;
1605 }
1606
1607 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1608 {
1609         struct connman_ipdevice *ipdevice;
1610
1611         DBG("ipconfig %p", ipconfig);
1612
1613         if (ipconfig == NULL || ipconfig->index < 0)
1614                 return -ENODEV;
1615
1616         ipdevice = g_hash_table_lookup(ipdevice_hash,
1617                                         GINT_TO_POINTER(ipconfig->index));
1618         if (ipdevice == NULL)
1619                 return -ENXIO;
1620
1621         if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1622                 return -EINVAL;
1623
1624         if (ipdevice->config_ipv4 == ipconfig) {
1625                 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1626
1627                 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1628                 connman_ipconfig_unref(ipdevice->config_ipv4);
1629                 ipdevice->config_ipv4 = NULL;
1630                 return 0;
1631         }
1632
1633         if (ipdevice->config_ipv6 == ipconfig) {
1634                 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1635
1636                 if (ipdevice->config_ipv6->method ==
1637                                                 CONNMAN_IPCONFIG_METHOD_AUTO)
1638                         disable_ipv6(ipdevice->config_ipv6);
1639
1640                 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1641                 connman_ipconfig_unref(ipdevice->config_ipv6);
1642                 ipdevice->config_ipv6 = NULL;
1643                 return 0;
1644         }
1645
1646         return -EINVAL;
1647 }
1648
1649 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1650 {
1651         switch (method) {
1652         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1653                 break;
1654         case CONNMAN_IPCONFIG_METHOD_OFF:
1655                 return "off";
1656         case CONNMAN_IPCONFIG_METHOD_FIXED:
1657                 return "fixed";
1658         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1659                 return "manual";
1660         case CONNMAN_IPCONFIG_METHOD_DHCP:
1661                 return "dhcp";
1662         case CONNMAN_IPCONFIG_METHOD_AUTO:
1663                 return "auto";
1664         }
1665
1666         return NULL;
1667 }
1668
1669 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1670 {
1671         if (g_strcmp0(method, "off") == 0)
1672                 return CONNMAN_IPCONFIG_METHOD_OFF;
1673         else if (g_strcmp0(method, "fixed") == 0)
1674                 return CONNMAN_IPCONFIG_METHOD_FIXED;
1675         else if (g_strcmp0(method, "manual") == 0)
1676                 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1677         else if (g_strcmp0(method, "dhcp") == 0)
1678                 return CONNMAN_IPCONFIG_METHOD_DHCP;
1679         else if (g_strcmp0(method, "auto") == 0)
1680                 return CONNMAN_IPCONFIG_METHOD_AUTO;
1681         else
1682                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1683 }
1684
1685 static const char *privacy2string(int privacy)
1686 {
1687         if (privacy <= 0)
1688                 return "disabled";
1689         else if (privacy == 1)
1690                 return "enabled";
1691         else if (privacy > 1)
1692                 return "prefered";
1693
1694         return "disabled";
1695 }
1696
1697 static int string2privacy(const char *privacy)
1698 {
1699         if (g_strcmp0(privacy, "disabled") == 0)
1700                 return 0;
1701         else if (g_strcmp0(privacy, "enabled") == 0)
1702                 return 1;
1703         else if (g_strcmp0(privacy, "prefered") == 0)
1704                 return 2;
1705         else
1706                 return 0;
1707 }
1708
1709 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1710                                                         DBusMessageIter *iter)
1711 {
1712         const char *str;
1713
1714         DBG("");
1715
1716         if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1717                 return;
1718
1719         str = __connman_ipconfig_method2string(ipconfig->method);
1720         if (str == NULL)
1721                 return;
1722
1723         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1724
1725         if (ipconfig->system == NULL)
1726                 return;
1727
1728         if (ipconfig->system->local != NULL) {
1729                 in_addr_t addr;
1730                 struct in_addr netmask;
1731                 char *mask;
1732
1733                 connman_dbus_dict_append_basic(iter, "Address",
1734                                 DBUS_TYPE_STRING, &ipconfig->system->local);
1735
1736                 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1737                 netmask.s_addr = htonl(addr);
1738                 mask = inet_ntoa(netmask);
1739                 connman_dbus_dict_append_basic(iter, "Netmask",
1740                                                 DBUS_TYPE_STRING, &mask);
1741         }
1742
1743         if (ipconfig->system->gateway != NULL)
1744                 connman_dbus_dict_append_basic(iter, "Gateway",
1745                                 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1746 }
1747
1748 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1749                                         DBusMessageIter *iter,
1750                                         struct connman_ipconfig *ipconfig_ipv4)
1751 {
1752         const char *str, *privacy;
1753
1754         DBG("");
1755
1756         if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1757                 return;
1758
1759         str = __connman_ipconfig_method2string(ipconfig->method);
1760         if (str == NULL)
1761                 return;
1762
1763         if (ipconfig_ipv4 != NULL &&
1764                         ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1765                 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1766                         str = "6to4";
1767         }
1768
1769         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1770
1771         if (ipconfig->system == NULL)
1772                 return;
1773
1774         if (ipconfig->system->local != NULL) {
1775                 connman_dbus_dict_append_basic(iter, "Address",
1776                                 DBUS_TYPE_STRING, &ipconfig->system->local);
1777                 connman_dbus_dict_append_basic(iter, "PrefixLength",
1778                                                 DBUS_TYPE_BYTE,
1779                                                 &ipconfig->system->prefixlen);
1780         }
1781
1782         if (ipconfig->system->gateway != NULL)
1783                 connman_dbus_dict_append_basic(iter, "Gateway",
1784                                 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1785
1786         privacy = privacy2string(ipconfig->ipv6_privacy_config);
1787         connman_dbus_dict_append_basic(iter, "Privacy",
1788                                 DBUS_TYPE_STRING, &privacy);
1789 }
1790
1791 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1792                                                         DBusMessageIter *iter)
1793 {
1794         const char *str, *privacy;
1795
1796         DBG("");
1797
1798         str = __connman_ipconfig_method2string(ipconfig->method);
1799         if (str == NULL)
1800                 return;
1801
1802         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1803
1804         switch (ipconfig->method) {
1805         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1806         case CONNMAN_IPCONFIG_METHOD_OFF:
1807         case CONNMAN_IPCONFIG_METHOD_DHCP:
1808                 return;
1809         case CONNMAN_IPCONFIG_METHOD_FIXED:
1810         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1811         case CONNMAN_IPCONFIG_METHOD_AUTO:
1812                 break;
1813         }
1814
1815         if (ipconfig->address == NULL)
1816                 return;
1817
1818         if (ipconfig->address->local != NULL) {
1819                 connman_dbus_dict_append_basic(iter, "Address",
1820                                 DBUS_TYPE_STRING, &ipconfig->address->local);
1821                 connman_dbus_dict_append_basic(iter, "PrefixLength",
1822                                                 DBUS_TYPE_BYTE,
1823                                                 &ipconfig->address->prefixlen);
1824         }
1825
1826         if (ipconfig->address->gateway != NULL)
1827                 connman_dbus_dict_append_basic(iter, "Gateway",
1828                                 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1829
1830         privacy = privacy2string(ipconfig->ipv6_privacy_config);
1831         connman_dbus_dict_append_basic(iter, "Privacy",
1832                                 DBUS_TYPE_STRING, &privacy);
1833 }
1834
1835 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1836                                                         DBusMessageIter *iter)
1837 {
1838         const char *str;
1839
1840         DBG("");
1841
1842         str = __connman_ipconfig_method2string(ipconfig->method);
1843         if (str == NULL)
1844                 return;
1845
1846         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1847
1848         switch (ipconfig->method) {
1849         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1850         case CONNMAN_IPCONFIG_METHOD_OFF:
1851         case CONNMAN_IPCONFIG_METHOD_FIXED:
1852         case CONNMAN_IPCONFIG_METHOD_DHCP:
1853         case CONNMAN_IPCONFIG_METHOD_AUTO:
1854                 return;
1855         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1856                 break;
1857         }
1858
1859         if (ipconfig->address == NULL)
1860                 return;
1861
1862         if (ipconfig->address->local != NULL) {
1863                 in_addr_t addr;
1864                 struct in_addr netmask;
1865                 char *mask;
1866
1867                 connman_dbus_dict_append_basic(iter, "Address",
1868                                 DBUS_TYPE_STRING, &ipconfig->address->local);
1869
1870                 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1871                 netmask.s_addr = htonl(addr);
1872                 mask = inet_ntoa(netmask);
1873                 connman_dbus_dict_append_basic(iter, "Netmask",
1874                                                 DBUS_TYPE_STRING, &mask);
1875         }
1876
1877         if (ipconfig->address->gateway != NULL)
1878                 connman_dbus_dict_append_basic(iter, "Gateway",
1879                                 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1880 }
1881
1882 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1883                                                         DBusMessageIter *array)
1884 {
1885         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1886         const char *address = NULL, *netmask = NULL, *gateway = NULL,
1887                 *prefix_length_string = NULL, *privacy_string = NULL;
1888         int prefix_length = 0, privacy = 0;
1889         DBusMessageIter dict;
1890
1891         DBG("ipconfig %p", ipconfig);
1892
1893         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1894                 return -EINVAL;
1895
1896         dbus_message_iter_recurse(array, &dict);
1897
1898         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1899                 DBusMessageIter entry;
1900                 const char *key;
1901                 int type;
1902
1903                 dbus_message_iter_recurse(&dict, &entry);
1904
1905                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1906                         return -EINVAL;
1907
1908                 dbus_message_iter_get_basic(&entry, &key);
1909                 dbus_message_iter_next(&entry);
1910
1911                 type = dbus_message_iter_get_arg_type(&entry);
1912
1913                 if (g_str_equal(key, "Method") == TRUE) {
1914                         const char *str;
1915
1916                         if (type != DBUS_TYPE_STRING)
1917                                 return -EINVAL;
1918
1919                         dbus_message_iter_get_basic(&entry, &str);
1920                         method = __connman_ipconfig_string2method(str);
1921                 } else if (g_str_equal(key, "Address") == TRUE) {
1922                         if (type != DBUS_TYPE_STRING)
1923                                 return -EINVAL;
1924
1925                         dbus_message_iter_get_basic(&entry, &address);
1926                 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1927                         if (type != DBUS_TYPE_STRING)
1928                                 return -EINVAL;
1929
1930                         dbus_message_iter_get_basic(&entry,
1931                                                         &prefix_length_string);
1932
1933                         prefix_length = atoi(prefix_length_string);
1934                         if (prefix_length < 0 || prefix_length > 128)
1935                                 return -EINVAL;
1936
1937                 } else if (g_str_equal(key, "Netmask") == TRUE) {
1938                         if (type != DBUS_TYPE_STRING)
1939                                 return -EINVAL;
1940
1941                         dbus_message_iter_get_basic(&entry, &netmask);
1942                 } else if (g_str_equal(key, "Gateway") == TRUE) {
1943                         if (type != DBUS_TYPE_STRING)
1944                                 return -EINVAL;
1945
1946                         dbus_message_iter_get_basic(&entry, &gateway);
1947                 } else if (g_str_equal(key, "Privacy") == TRUE) {
1948                         if (type != DBUS_TYPE_STRING)
1949                                 return -EINVAL;
1950
1951                         dbus_message_iter_get_basic(&entry, &privacy_string);
1952                         privacy = string2privacy(privacy_string);
1953                 }
1954                 dbus_message_iter_next(&dict);
1955         }
1956
1957         DBG("method %d address %s netmask %s gateway %s prefix_length %d "
1958                 "privacy %s",
1959                 method, address, netmask, gateway, prefix_length,
1960                 privacy_string);
1961
1962         switch (method) {
1963         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1964         case CONNMAN_IPCONFIG_METHOD_FIXED:
1965                 return -EINVAL;
1966
1967         case CONNMAN_IPCONFIG_METHOD_OFF:
1968                 ipconfig->method = method;
1969                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1970                         disable_ipv6(ipconfig);
1971                 break;
1972
1973         case CONNMAN_IPCONFIG_METHOD_AUTO:
1974                 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1975                         return -EINVAL;
1976
1977                 ipconfig->method = method;
1978                 if (privacy_string != NULL)
1979                         ipconfig->ipv6_privacy_config = privacy;
1980                 enable_ipv6(ipconfig);
1981                 break;
1982
1983         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1984                 if (address == NULL)
1985                         return -EINVAL;
1986
1987                 ipconfig->method = method;
1988
1989                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1990                         connman_ipaddress_set_ipv4(ipconfig->address,
1991                                                 address, netmask, gateway);
1992                 else
1993                         return connman_ipaddress_set_ipv6(
1994                                         ipconfig->address, address,
1995                                                 prefix_length, gateway);
1996                 break;
1997
1998         case CONNMAN_IPCONFIG_METHOD_DHCP:
1999                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2000                         return -EOPNOTSUPP;
2001
2002                 ipconfig->method = method;
2003                 break;
2004         }
2005
2006         return 0;
2007 }
2008
2009 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2010                                                         DBusMessageIter *iter)
2011 {
2012         struct connman_ipdevice *ipdevice;
2013         const char *method = "auto";
2014
2015         connman_dbus_dict_append_basic(iter, "Method",
2016                                                 DBUS_TYPE_STRING, &method);
2017
2018         ipdevice = g_hash_table_lookup(ipdevice_hash,
2019                                         GINT_TO_POINTER(ipconfig->index));
2020         if (ipdevice == NULL)
2021                 return;
2022
2023         if (ipdevice->ifname != NULL)
2024                 connman_dbus_dict_append_basic(iter, "Interface",
2025                                         DBUS_TYPE_STRING, &ipdevice->ifname);
2026
2027         if (ipdevice->address != NULL)
2028                 connman_dbus_dict_append_basic(iter, "Address",
2029                                         DBUS_TYPE_STRING, &ipdevice->address);
2030
2031         if (ipdevice->mtu > 0)
2032                 connman_dbus_dict_append_basic(iter, "MTU",
2033                                         DBUS_TYPE_UINT16, &ipdevice->mtu);
2034 }
2035
2036 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2037                 GKeyFile *keyfile, const char *identifier, const char *prefix)
2038 {
2039         char *method;
2040         char *key;
2041
2042         DBG("ipconfig %p identifier %s", ipconfig, identifier);
2043
2044         key = g_strdup_printf("%smethod", prefix);
2045         method = g_key_file_get_string(keyfile, identifier, key, NULL);
2046         if (method == NULL) {
2047                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2048                         ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2049                 else
2050                         ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2051         } else
2052                 ipconfig->method = __connman_ipconfig_string2method(method);
2053
2054         if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2055                 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2056
2057         if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2058                 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2059                         ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2060                         char *privacy;
2061                         char *pprefix = g_strdup_printf("%sprivacy", prefix);
2062                         privacy = g_key_file_get_string(keyfile, identifier,
2063                                                         pprefix, NULL);
2064                         ipconfig->ipv6_privacy_config = string2privacy(privacy);
2065                         g_free(pprefix);
2066                         g_free(privacy);
2067
2068                         __connman_ipconfig_enable(ipconfig);
2069                         enable_ipv6(ipconfig);
2070                 }
2071         }
2072
2073         g_free(method);
2074         g_free(key);
2075
2076         key = g_strdup_printf("%snetmask_prefixlen", prefix);
2077         ipconfig->address->prefixlen = g_key_file_get_integer(
2078                                 keyfile, identifier, key, NULL);
2079         g_free(key);
2080
2081         key = g_strdup_printf("%slocal_address", prefix);
2082         ipconfig->address->local = g_key_file_get_string(
2083                         keyfile, identifier, key, NULL);
2084         g_free(key);
2085
2086         key = g_strdup_printf("%speer_address", prefix);
2087         ipconfig->address->peer = g_key_file_get_string(
2088                                 keyfile, identifier, key, NULL);
2089         g_free(key);
2090
2091         key = g_strdup_printf("%sbroadcast_address", prefix);
2092         ipconfig->address->broadcast = g_key_file_get_string(
2093                                 keyfile, identifier, key, NULL);
2094         g_free(key);
2095
2096         key = g_strdup_printf("%sgateway", prefix);
2097         ipconfig->address->gateway = g_key_file_get_string(
2098                                 keyfile, identifier, key, NULL);
2099         g_free(key);
2100
2101         return 0;
2102 }
2103
2104 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2105                 GKeyFile *keyfile, const char *identifier, const char *prefix)
2106 {
2107         const char *method;
2108         char *key;
2109
2110         DBG("ipconfig %p identifier %s", ipconfig, identifier);
2111
2112         method = __connman_ipconfig_method2string(ipconfig->method);
2113
2114         key = g_strdup_printf("%smethod", prefix);
2115         g_key_file_set_string(keyfile, identifier, key, method);
2116         g_free(key);
2117
2118         if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2119                 const char *privacy;
2120                 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2121                 key = g_strdup_printf("%sprivacy", prefix);
2122                 g_key_file_set_string(keyfile, identifier, key, privacy);
2123                 g_free(key);
2124         }
2125
2126         switch (ipconfig->method) {
2127         case CONNMAN_IPCONFIG_METHOD_FIXED:
2128         case CONNMAN_IPCONFIG_METHOD_MANUAL:
2129                 break;
2130         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2131         case CONNMAN_IPCONFIG_METHOD_OFF:
2132         case CONNMAN_IPCONFIG_METHOD_DHCP:
2133         case CONNMAN_IPCONFIG_METHOD_AUTO:
2134                 return 0;
2135         }
2136
2137         key = g_strdup_printf("%snetmask_prefixlen", prefix);
2138         g_key_file_set_integer(keyfile, identifier,
2139                         key, ipconfig->address->prefixlen);
2140         g_free(key);
2141
2142         key = g_strdup_printf("%slocal_address", prefix);
2143         if (ipconfig->address->local != NULL)
2144                 g_key_file_set_string(keyfile, identifier,
2145                                 key, ipconfig->address->local);
2146         g_free(key);
2147
2148         key = g_strdup_printf("%speer_address", prefix);
2149         if (ipconfig->address->peer != NULL)
2150                 g_key_file_set_string(keyfile, identifier,
2151                                 key, ipconfig->address->peer);
2152         g_free(key);
2153
2154         key = g_strdup_printf("%sbroadcast_address", prefix);
2155         if (ipconfig->address->broadcast != NULL)
2156                 g_key_file_set_string(keyfile, identifier,
2157                         key, ipconfig->address->broadcast);
2158         g_free(key);
2159
2160         key = g_strdup_printf("%sgateway", prefix);
2161         if (ipconfig->address->gateway != NULL)
2162                 g_key_file_set_string(keyfile, identifier,
2163                         key, ipconfig->address->gateway);
2164         g_free(key);
2165
2166         return 0;
2167 }
2168
2169 int __connman_ipconfig_init(void)
2170 {
2171         DBG("");
2172
2173         ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2174                                                         NULL, free_ipdevice);
2175
2176         return 0;
2177 }
2178
2179 void __connman_ipconfig_cleanup(void)
2180 {
2181         DBG("");
2182
2183         g_hash_table_destroy(ipdevice_hash);
2184         ipdevice_hash = NULL;
2185 }