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