ipconfig: Split ipconfig_set_gateway()
[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 = CONNMAN_IPCONFIG_TYPE_IPV6;
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 = CONNMAN_IPCONFIG_TYPE_IPV4;
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                 GSList *list;
807                 GList *config_list;
808
809                 if (family == AF_INET6) {
810                         g_free(ipdevice->ipv6_gateway);
811                         ipdevice->ipv6_gateway = g_strdup(gateway);
812
813                         if (ipdevice->config_ipv6 != NULL &&
814                                 ipdevice->config_ipv6->system != NULL) {
815                                 g_free(ipdevice->config_ipv6->system->gateway);
816                                 ipdevice->config_ipv6->system->gateway =
817                                         g_strdup(gateway);
818                         }
819                 } else {
820                         g_free(ipdevice->ipv4_gateway);
821                         ipdevice->ipv4_gateway = g_strdup(gateway);
822
823                         if (ipdevice->config_ipv4 != NULL &&
824                                 ipdevice->config_ipv4->system != NULL) {
825                                 g_free(ipdevice->config_ipv4->system->gateway);
826                                 ipdevice->config_ipv4->system->gateway =
827                                         g_strdup(gateway);
828                         }
829                 }
830
831                 for (list = ipdevice->address_list; list; list = list->next) {
832                         struct connman_ipaddress *ipaddress = list->data;
833
834                         g_free(ipaddress->gateway);
835                         ipaddress->gateway = g_strdup(gateway);
836                 }
837
838                 for (config_list = g_list_first(ipconfig_list); config_list;
839                                         config_list = g_list_next(config_list)) {
840                         struct connman_ipconfig *ipconfig = config_list->data;
841
842                         if (index != ipconfig->index)
843                                 continue;
844
845                         if (ipconfig->ops == NULL)
846                                 continue;
847
848                         if (ipconfig->ops->ip_bound)
849                                 ipconfig->ops->ip_bound(ipconfig);
850                 }
851         }
852
853         connman_info("%s {add} route %s gw %s scope %u <%s>",
854                                         ipdevice->ifname, dst, gateway,
855                                                 scope, scope2str(scope));
856 }
857
858 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
859                                         const char *dst, const char *gateway)
860 {
861         struct connman_ipdevice *ipdevice;
862
863         DBG("index %d", index);
864
865         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
866         if (ipdevice == NULL)
867                 return;
868
869         if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
870                 GSList *list;
871                 GList *config_list;
872
873                 if (family == AF_INET6) {
874                         g_free(ipdevice->ipv6_gateway);
875                         ipdevice->ipv6_gateway = NULL;
876
877                         if (ipdevice->config_ipv6 != NULL &&
878                                 ipdevice->config_ipv6->system != NULL) {
879                                 g_free(ipdevice->config_ipv6->system->gateway);
880                                 ipdevice->config_ipv6->system->gateway = NULL;
881                         }
882                 } else {
883                         g_free(ipdevice->ipv4_gateway);
884                         ipdevice->ipv4_gateway = NULL;
885
886                         if (ipdevice->config_ipv4 != NULL &&
887                                 ipdevice->config_ipv4->system != NULL) {
888                                 g_free(ipdevice->config_ipv4->system->gateway);
889                                 ipdevice->config_ipv4->system->gateway = NULL;
890                         }
891                 }
892
893                 for (list = ipdevice->address_list; list; list = list->next) {
894                         struct connman_ipaddress *ipaddress = list->data;
895
896                         g_free(ipaddress->gateway);
897                         ipaddress->gateway = NULL;
898                 }
899
900                 for (config_list = g_list_first(ipconfig_list); config_list;
901                                         config_list = g_list_next(config_list)) {
902                         struct connman_ipconfig *ipconfig = config_list->data;
903
904                         if (index != ipconfig->index)
905                                 continue;
906
907                         if (ipconfig->ops == NULL)
908                                 continue;
909
910                         if (ipconfig->ops->ip_release)
911                                 ipconfig->ops->ip_release(ipconfig);
912                 }
913         }
914
915         connman_info("%s {del} route %s gw %s scope %u <%s>",
916                                         ipdevice->ifname, dst, gateway,
917                                                 scope, scope2str(scope));
918 }
919
920 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
921                                                         void *user_data)
922 {
923         GList *list, *keys;
924
925         keys = g_hash_table_get_keys(ipdevice_hash);
926         if (keys == NULL)
927                 return;
928
929         for (list = g_list_first(keys); list; list = g_list_next(list)) {
930                 int index = GPOINTER_TO_INT(list->data);
931
932                 function(index, user_data);
933         }
934
935         g_list_free(keys);
936 }
937
938 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
939                                         struct connman_ipconfig *ipconfig)
940 {
941         return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
942 }
943
944 unsigned short __connman_ipconfig_get_type_from_index(int index)
945 {
946         struct connman_ipdevice *ipdevice;
947
948         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
949         if (ipdevice == NULL)
950                 return ARPHRD_VOID;
951
952         return ipdevice->type;
953 }
954
955 unsigned int __connman_ipconfig_get_flags_from_index(int index)
956 {
957         struct connman_ipdevice *ipdevice;
958
959         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
960         if (ipdevice == NULL)
961                 return 0;
962
963         return ipdevice->flags;
964 }
965
966 const char *__connman_ipconfig_get_gateway_from_index(int index)
967 {
968         struct connman_ipdevice *ipdevice;
969
970         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
971         if (ipdevice == NULL)
972                 return NULL;
973
974         if (ipdevice->ipv4_gateway != NULL)
975                 return ipdevice->ipv4_gateway;
976
977         if (ipdevice->config_ipv4 != NULL &&
978                         ipdevice->config_ipv4->address != NULL)
979                 return ipdevice->config_ipv4->address->gateway;
980
981         if (ipdevice->ipv6_gateway != NULL)
982                 return ipdevice->ipv6_gateway;
983
984         if (ipdevice->config_ipv6 != NULL &&
985                         ipdevice->config_ipv6->address != NULL)
986                 return ipdevice->config_ipv6->address->gateway;
987
988         return NULL;
989 }
990
991 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
992 {
993         ipconfig->index = index;
994 }
995
996 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
997 {
998         if (ipconfig->address == NULL)
999                 return NULL;
1000
1001         return ipconfig->address->local;
1002 }
1003
1004 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig, const char *address)
1005 {
1006         if (ipconfig->address == NULL)
1007                 return;
1008
1009         g_free(ipconfig->address->local);
1010         ipconfig->address->local = g_strdup(address);
1011 }
1012
1013 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
1014 {
1015         if (ipconfig->address == NULL)
1016                 return NULL;
1017
1018         return ipconfig->address->peer;
1019 }
1020
1021 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig, const char *address)
1022 {
1023         if (ipconfig->address == NULL)
1024                 return;
1025
1026         g_free(ipconfig->address->peer);
1027         ipconfig->address->peer = g_strdup(address);
1028 }
1029
1030 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1031 {
1032         if (ipconfig->address == NULL)
1033                 return NULL;
1034
1035         return ipconfig->address->broadcast;
1036 }
1037
1038 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig, const char *broadcast)
1039 {
1040         if (ipconfig->address == NULL)
1041                 return;
1042
1043         g_free(ipconfig->address->broadcast);
1044         ipconfig->address->broadcast = g_strdup(broadcast);
1045 }
1046
1047 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1048 {
1049         if (ipconfig->address == NULL)
1050                 return NULL;
1051
1052         return ipconfig->address->gateway;
1053 }
1054
1055 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig, const char *gateway)
1056 {
1057         DBG("");
1058
1059         if (ipconfig->address == NULL)
1060                 return;
1061         g_free(ipconfig->address->gateway);
1062         ipconfig->address->gateway = g_strdup(gateway);
1063 }
1064
1065 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1066 {
1067         struct connman_service *service;
1068
1069         DBG("");
1070
1071         if (ipconfig->address == NULL || ipconfig->address->gateway == NULL)
1072                 return -EINVAL;
1073
1074         service = __connman_service_lookup_from_index(ipconfig->index);
1075         if (service == NULL)
1076                 return -EINVAL;
1077
1078         __connman_connection_gateway_remove(service);
1079
1080         if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1081                 return __connman_connection_gateway_add(service, NULL,
1082                                                 ipconfig->address->gateway,
1083                                                         ipconfig->address->peer);
1084         } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1085                 return __connman_connection_gateway_add(service,
1086                                                 ipconfig->address->gateway,
1087                                                         NULL, ipconfig->address->peer);
1088         }
1089
1090         return 0;
1091 }
1092
1093 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1094 {
1095         struct connman_service *service;
1096
1097         DBG("");
1098
1099         service = __connman_service_lookup_from_index(ipconfig->index);
1100         if (service != NULL)
1101                 __connman_connection_gateway_remove(service);
1102 }
1103
1104 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1105 {
1106         if (ipconfig->address == NULL)
1107                 return 0;
1108
1109         return ipconfig->address->prefixlen;
1110 }
1111
1112 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig, unsigned char prefixlen)
1113 {
1114         if (ipconfig->address == NULL)
1115                 return;
1116
1117         ipconfig->address->prefixlen = prefixlen;
1118 }
1119
1120 static struct connman_ipconfig *create_ipv6config(int index)
1121 {
1122         struct connman_ipconfig *ipv6config;
1123
1124         DBG("index %d", index);
1125
1126         ipv6config = g_try_new0(struct connman_ipconfig, 1);
1127         if (ipv6config == NULL)
1128                 return NULL;
1129
1130         ipv6config->refcount = 1;
1131
1132         ipv6config->index = index;
1133         ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1134         ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1135         ipv6config->ipv6_privacy_config = 0;
1136
1137         ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1138         if (ipv6config->address == NULL) {
1139                 g_free(ipv6config);
1140                 return NULL;
1141         }
1142
1143         ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1144
1145         DBG("ipconfig %p", ipv6config);
1146
1147         return ipv6config;
1148 }
1149
1150 /**
1151  * connman_ipconfig_create:
1152  *
1153  * Allocate a new ipconfig structure.
1154  *
1155  * Returns: a newly-allocated #connman_ipconfig structure
1156  */
1157 struct connman_ipconfig *connman_ipconfig_create(int index,
1158                                         enum connman_ipconfig_type type)
1159 {
1160         struct connman_ipconfig *ipconfig;
1161
1162         if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1163                 return create_ipv6config(index);
1164
1165         DBG("index %d", index);
1166
1167         ipconfig = g_try_new0(struct connman_ipconfig, 1);
1168         if (ipconfig == NULL)
1169                 return NULL;
1170
1171         ipconfig->refcount = 1;
1172
1173         ipconfig->index = index;
1174         ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1175
1176         ipconfig->address = connman_ipaddress_alloc(AF_INET);
1177         if (ipconfig->address == NULL) {
1178                 g_free(ipconfig);
1179                 return NULL;
1180         }
1181
1182         ipconfig->system = connman_ipaddress_alloc(AF_INET);
1183
1184         DBG("ipconfig %p", ipconfig);
1185
1186         return ipconfig;
1187 }
1188
1189
1190 /**
1191  * connman_ipconfig_ref:
1192  * @ipconfig: ipconfig structure
1193  *
1194  * Increase reference counter of ipconfig
1195  */
1196 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
1197 {
1198         DBG("ipconfig %p refcount %d", ipconfig,
1199                                 g_atomic_int_get(&ipconfig->refcount) + 1);
1200
1201         g_atomic_int_inc(&ipconfig->refcount);
1202
1203         return ipconfig;
1204 }
1205
1206 /**
1207  * connman_ipconfig_unref:
1208  * @ipconfig: ipconfig structure
1209  *
1210  * Decrease reference counter of ipconfig
1211  */
1212 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
1213 {
1214         if (ipconfig == NULL)
1215                 return;
1216
1217         DBG("ipconfig %p refcount %d", ipconfig,
1218                         g_atomic_int_get(&ipconfig->refcount) - 1);
1219
1220         if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
1221                 __connman_ipconfig_disable(ipconfig);
1222
1223                 connman_ipconfig_set_ops(ipconfig, NULL);
1224
1225                 if (ipconfig->origin != NULL) {
1226                         connman_ipconfig_unref(ipconfig->origin);
1227                         ipconfig->origin = NULL;
1228                 }
1229
1230                 connman_ipaddress_free(ipconfig->system);
1231                 connman_ipaddress_free(ipconfig->address);
1232                 g_free(ipconfig);
1233         }
1234 }
1235
1236 /**
1237  * connman_ipconfig_get_data:
1238  * @ipconfig: ipconfig structure
1239  *
1240  * Get private data pointer
1241  */
1242 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1243 {
1244         if (ipconfig == NULL)
1245                 return NULL;
1246
1247         return ipconfig->ops_data;
1248 }
1249
1250 /**
1251  * connman_ipconfig_set_data:
1252  * @ipconfig: ipconfig structure
1253  * @data: data pointer
1254  *
1255  * Set private data pointer
1256  */
1257 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1258 {
1259         ipconfig->ops_data = data;
1260 }
1261
1262 /**
1263  * connman_ipconfig_get_index:
1264  * @ipconfig: ipconfig structure
1265  *
1266  * Get interface index
1267  */
1268 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1269 {
1270         if (ipconfig == NULL)
1271                 return -1;
1272
1273         if (ipconfig->origin != NULL)
1274                 return ipconfig->origin->index;
1275
1276         return ipconfig->index;
1277 }
1278
1279 /**
1280  * connman_ipconfig_get_ifname:
1281  * @ipconfig: ipconfig structure
1282  *
1283  * Get interface name
1284  */
1285 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1286 {
1287         struct connman_ipdevice *ipdevice;
1288
1289         if (ipconfig == NULL)
1290                 return NULL;
1291
1292         if (ipconfig->index < 0)
1293                 return NULL;
1294
1295         ipdevice = g_hash_table_lookup(ipdevice_hash,
1296                                         GINT_TO_POINTER(ipconfig->index));
1297         if (ipdevice == NULL)
1298                 return NULL;
1299
1300         return ipdevice->ifname;
1301 }
1302
1303 /**
1304  * connman_ipconfig_set_ops:
1305  * @ipconfig: ipconfig structure
1306  * @ops: operation callbacks
1307  *
1308  * Set the operation callbacks
1309  */
1310 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1311                                 const struct connman_ipconfig_ops *ops)
1312 {
1313         ipconfig->ops = ops;
1314 }
1315
1316 /**
1317  * connman_ipconfig_set_method:
1318  * @ipconfig: ipconfig structure
1319  * @method: configuration method
1320  *
1321  * Set the configuration method
1322  */
1323 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1324                                         enum connman_ipconfig_method method)
1325 {
1326         ipconfig->method = method;
1327
1328         return 0;
1329 }
1330
1331 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1332 {
1333         if (ipconfig == NULL)
1334                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1335
1336         return ipconfig->method;
1337 }
1338
1339 /**
1340  * connman_ipconfig_bind:
1341  * @ipconfig: ipconfig structure
1342  * @ipaddress: ipaddress structure
1343  *
1344  * Bind IP address details to configuration
1345  */
1346 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
1347                                         struct connman_ipaddress *ipaddress)
1348 {
1349         struct connman_ipconfig *origin;
1350
1351         origin = ipconfig->origin ? ipconfig->origin : ipconfig;
1352
1353         connman_ipaddress_copy(origin->address, ipaddress);
1354
1355         connman_inet_set_address(origin->index, origin->address);
1356 }
1357
1358 void __connman_ipconfig_set_element_ipv6_gateway(
1359                         struct connman_ipconfig *ipconfig,
1360                                 struct connman_element *element)
1361 {
1362         if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1363                 element->ipv6.gateway = ipconfig->address->gateway;
1364 }
1365
1366 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1367 {
1368         DBG("");
1369
1370         switch (ipconfig->method) {
1371         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1372         case CONNMAN_IPCONFIG_METHOD_OFF:
1373         case CONNMAN_IPCONFIG_METHOD_AUTO:
1374                 break;
1375         case CONNMAN_IPCONFIG_METHOD_FIXED:
1376         case CONNMAN_IPCONFIG_METHOD_DHCP:
1377         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1378                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1379                         return connman_inet_set_address(ipconfig->index,
1380                                                         ipconfig->address);
1381                 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1382                         return connman_inet_set_ipv6_address(
1383                                         ipconfig->index, ipconfig->address);
1384         }
1385
1386         return 0;
1387 }
1388
1389 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1390 {
1391         DBG("");
1392
1393         if (ipconfig == NULL)
1394                 return 0;
1395
1396         DBG("method %d", ipconfig->method);
1397
1398         switch (ipconfig->method) {
1399         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1400         case CONNMAN_IPCONFIG_METHOD_OFF:
1401         case CONNMAN_IPCONFIG_METHOD_AUTO:
1402                 break;
1403         case CONNMAN_IPCONFIG_METHOD_FIXED:
1404         case CONNMAN_IPCONFIG_METHOD_DHCP:
1405         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1406                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1407                         return connman_inet_clear_address(ipconfig->index,
1408                                                         ipconfig->address);
1409                 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1410                         return connman_inet_clear_ipv6_address(
1411                                                 ipconfig->index,
1412                                                 ipconfig->address->local,
1413                                                 ipconfig->address->prefixlen);
1414         }
1415
1416         return 0;
1417 }
1418
1419 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1420                                                         const char *url)
1421 {
1422         struct connman_ipdevice *ipdevice;
1423
1424         DBG("ipconfig %p", ipconfig);
1425
1426         if (ipconfig == NULL || ipconfig->index < 0)
1427                 return -ENODEV;
1428
1429         ipdevice = g_hash_table_lookup(ipdevice_hash,
1430                                         GINT_TO_POINTER(ipconfig->index));
1431         if (ipdevice == NULL)
1432                 return -ENXIO;
1433
1434         g_free(ipdevice->pac);
1435         ipdevice->pac = g_strdup(url);
1436
1437         return 0;
1438 }
1439
1440 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1441 {
1442         struct connman_ipdevice *ipdevice;
1443
1444         DBG("ipconfig %p", ipconfig);
1445
1446         if (ipconfig == NULL || ipconfig->index < 0)
1447                 return NULL;
1448
1449         ipdevice = g_hash_table_lookup(ipdevice_hash,
1450                                         GINT_TO_POINTER(ipconfig->index));
1451         if (ipdevice == NULL)
1452                 return NULL;
1453
1454         return ipdevice->pac;
1455 }
1456
1457 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1458 {
1459         struct connman_ipdevice *ipdevice;
1460
1461         DBG("");
1462
1463         ipdevice = g_hash_table_lookup(ipdevice_hash,
1464                                         GINT_TO_POINTER(ipconfig->index));
1465         if (ipdevice == NULL)
1466                 return;
1467
1468         set_ipv6_state(ipdevice->ifname, FALSE);
1469 }
1470
1471 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1472 {
1473         struct connman_ipdevice *ipdevice;
1474
1475         DBG("");
1476
1477         ipdevice = g_hash_table_lookup(ipdevice_hash,
1478                                         GINT_TO_POINTER(ipconfig->index));
1479         if (ipdevice == NULL)
1480                 return;
1481
1482         if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1483                 set_ipv6_privacy(ipdevice->ifname,
1484                                 ipconfig->ipv6_privacy_config);
1485
1486         set_ipv6_state(ipdevice->ifname, TRUE);
1487 }
1488
1489 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1490 {
1491         if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1492                 return;
1493
1494         disable_ipv6(ipconfig);
1495 }
1496
1497 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1498 {
1499         struct connman_ipdevice *ipdevice;
1500         gboolean up = FALSE, down = FALSE;
1501         gboolean lower_up = FALSE, lower_down = FALSE;
1502         enum connman_ipconfig_type type;
1503
1504         DBG("ipconfig %p", ipconfig);
1505
1506         if (ipconfig == NULL || ipconfig->index < 0)
1507                 return -ENODEV;
1508
1509         ipdevice = g_hash_table_lookup(ipdevice_hash,
1510                                         GINT_TO_POINTER(ipconfig->index));
1511         if (ipdevice == NULL)
1512                 return -ENXIO;
1513
1514         if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1515                 if (ipdevice->config_ipv4 == ipconfig)
1516                         return -EALREADY;
1517                 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1518         } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1519                 if (ipdevice->config_ipv6 == ipconfig)
1520                         return -EALREADY;
1521                 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1522                 enable_ipv6(ipconfig);
1523         } else
1524                 return -EINVAL;
1525
1526         if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1527                                         ipdevice->config_ipv4 != NULL) {
1528                 ipconfig_list = g_list_remove(ipconfig_list,
1529                                                         ipdevice->config_ipv4);
1530
1531                 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1532
1533                 connman_ipconfig_unref(ipdevice->config_ipv4);
1534         }
1535
1536         if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1537                                         ipdevice->config_ipv6 != NULL) {
1538                 ipconfig_list = g_list_remove(ipconfig_list,
1539                                                         ipdevice->config_ipv6);
1540
1541                 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1542
1543                 connman_ipconfig_unref(ipdevice->config_ipv6);
1544         }
1545
1546         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1547                 ipdevice->config_ipv4 = connman_ipconfig_ref(ipconfig);
1548         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1549                 ipdevice->config_ipv6 = connman_ipconfig_ref(ipconfig);
1550
1551         ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1552
1553         if (ipdevice->flags & IFF_UP)
1554                 up = TRUE;
1555         else
1556                 down = TRUE;
1557
1558         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1559                         (IFF_RUNNING | IFF_LOWER_UP))
1560                 lower_up = TRUE;
1561         else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1562                 lower_down = TRUE;
1563
1564         if (up == TRUE && ipconfig->ops->up)
1565                 ipconfig->ops->up(ipconfig);
1566         if (lower_up == TRUE && ipconfig->ops->lower_up)
1567                 ipconfig->ops->lower_up(ipconfig);
1568
1569         if (lower_down == TRUE && ipconfig->ops->lower_down)
1570                 ipconfig->ops->lower_down(ipconfig);
1571         if (down == TRUE && ipconfig->ops->down)
1572                 ipconfig->ops->down(ipconfig);
1573
1574         return 0;
1575 }
1576
1577 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1578 {
1579         struct connman_ipdevice *ipdevice;
1580
1581         DBG("ipconfig %p", ipconfig);
1582
1583         if (ipconfig == NULL || ipconfig->index < 0)
1584                 return -ENODEV;
1585
1586         ipdevice = g_hash_table_lookup(ipdevice_hash,
1587                                         GINT_TO_POINTER(ipconfig->index));
1588         if (ipdevice == NULL)
1589                 return -ENXIO;
1590
1591         if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1592                 return -EINVAL;
1593
1594         if (ipdevice->config_ipv4 == ipconfig) {
1595                 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1596
1597                 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1598                 connman_ipconfig_unref(ipdevice->config_ipv4);
1599                 ipdevice->config_ipv4 = NULL;
1600                 return 0;
1601         }
1602
1603         if (ipdevice->config_ipv6 == ipconfig) {
1604                 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1605
1606                 if (ipdevice->config_ipv6->method ==
1607                                                 CONNMAN_IPCONFIG_METHOD_AUTO)
1608                         disable_ipv6(ipdevice->config_ipv6);
1609
1610                 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1611                 connman_ipconfig_unref(ipdevice->config_ipv6);
1612                 ipdevice->config_ipv6 = NULL;
1613                 return 0;
1614         }
1615
1616         return -EINVAL;
1617 }
1618
1619 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1620 {
1621         switch (method) {
1622         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1623                 break;
1624         case CONNMAN_IPCONFIG_METHOD_OFF:
1625                 return "off";
1626         case CONNMAN_IPCONFIG_METHOD_FIXED:
1627                 return "fixed";
1628         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1629                 return "manual";
1630         case CONNMAN_IPCONFIG_METHOD_DHCP:
1631                 return "dhcp";
1632         case CONNMAN_IPCONFIG_METHOD_AUTO:
1633                 return "auto";
1634         }
1635
1636         return NULL;
1637 }
1638
1639 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1640 {
1641         if (g_strcmp0(method, "off") == 0)
1642                 return CONNMAN_IPCONFIG_METHOD_OFF;
1643         else if (g_strcmp0(method, "fixed") == 0)
1644                 return CONNMAN_IPCONFIG_METHOD_FIXED;
1645         else if (g_strcmp0(method, "manual") == 0)
1646                 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1647         else if (g_strcmp0(method, "dhcp") == 0)
1648                 return CONNMAN_IPCONFIG_METHOD_DHCP;
1649         else if (g_strcmp0(method, "auto") == 0)
1650                 return CONNMAN_IPCONFIG_METHOD_AUTO;
1651         else
1652                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1653 }
1654
1655 static const char *privacy2string(int privacy)
1656 {
1657         if (privacy <= 0)
1658                 return "disabled";
1659         else if (privacy == 1)
1660                 return "enabled";
1661         else if (privacy > 1)
1662                 return "prefered";
1663
1664         return "disabled";
1665 }
1666
1667 static int string2privacy(const char *privacy)
1668 {
1669         if (g_strcmp0(privacy, "disabled") == 0)
1670                 return 0;
1671         else if (g_strcmp0(privacy, "enabled") == 0)
1672                 return 1;
1673         else if (g_strcmp0(privacy, "prefered") == 0)
1674                 return 2;
1675         else
1676                 return 0;
1677 }
1678
1679 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1680                                                         DBusMessageIter *iter)
1681 {
1682         const char *str;
1683
1684         DBG("");
1685
1686         if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1687                 return;
1688
1689         str = __connman_ipconfig_method2string(ipconfig->method);
1690         if (str == NULL)
1691                 return;
1692
1693         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1694
1695         if (ipconfig->system == NULL)
1696                 return;
1697
1698         if (ipconfig->system->local != NULL) {
1699                 in_addr_t addr;
1700                 struct in_addr netmask;
1701                 char *mask;
1702
1703                 connman_dbus_dict_append_basic(iter, "Address",
1704                                 DBUS_TYPE_STRING, &ipconfig->system->local);
1705
1706                 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1707                 netmask.s_addr = htonl(addr);
1708                 mask = inet_ntoa(netmask);
1709                 connman_dbus_dict_append_basic(iter, "Netmask",
1710                                                 DBUS_TYPE_STRING, &mask);
1711         }
1712
1713         if (ipconfig->system->gateway != NULL)
1714                 connman_dbus_dict_append_basic(iter, "Gateway",
1715                                 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1716 }
1717
1718 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1719                                                         DBusMessageIter *iter)
1720 {
1721         const char *str, *privacy;
1722
1723         DBG("");
1724
1725         if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1726                 return;
1727
1728         str = __connman_ipconfig_method2string(ipconfig->method);
1729         if (str == NULL)
1730                 return;
1731
1732         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1733
1734         if (ipconfig->system == NULL)
1735                 return;
1736
1737         if (ipconfig->system->local != NULL) {
1738                 connman_dbus_dict_append_basic(iter, "Address",
1739                                 DBUS_TYPE_STRING, &ipconfig->system->local);
1740                 connman_dbus_dict_append_basic(iter, "PrefixLength",
1741                                                 DBUS_TYPE_BYTE,
1742                                                 &ipconfig->system->prefixlen);
1743         }
1744
1745         if (ipconfig->system->gateway != NULL)
1746                 connman_dbus_dict_append_basic(iter, "Gateway",
1747                                 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1748
1749         privacy = privacy2string(ipconfig->ipv6_privacy_config);
1750         connman_dbus_dict_append_basic(iter, "Privacy",
1751                                 DBUS_TYPE_STRING, &privacy);
1752 }
1753
1754 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1755                                                         DBusMessageIter *iter)
1756 {
1757         const char *str, *privacy;
1758
1759         DBG("");
1760
1761         str = __connman_ipconfig_method2string(ipconfig->method);
1762         if (str == NULL)
1763                 return;
1764
1765         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1766
1767         switch (ipconfig->method) {
1768         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1769         case CONNMAN_IPCONFIG_METHOD_OFF:
1770         case CONNMAN_IPCONFIG_METHOD_DHCP:
1771                 return;
1772         case CONNMAN_IPCONFIG_METHOD_FIXED:
1773         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1774         case CONNMAN_IPCONFIG_METHOD_AUTO:
1775                 break;
1776         }
1777
1778         if (ipconfig->address == NULL)
1779                 return;
1780
1781         if (ipconfig->address->local != NULL) {
1782                 connman_dbus_dict_append_basic(iter, "Address",
1783                                 DBUS_TYPE_STRING, &ipconfig->address->local);
1784                 connman_dbus_dict_append_basic(iter, "PrefixLength",
1785                                                 DBUS_TYPE_BYTE,
1786                                                 &ipconfig->address->prefixlen);
1787         }
1788
1789         if (ipconfig->address->gateway != NULL)
1790                 connman_dbus_dict_append_basic(iter, "Gateway",
1791                                 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1792
1793         privacy = privacy2string(ipconfig->ipv6_privacy_config);
1794         connman_dbus_dict_append_basic(iter, "Privacy",
1795                                 DBUS_TYPE_STRING, &privacy);
1796 }
1797
1798 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1799                                                         DBusMessageIter *iter)
1800 {
1801         const char *str;
1802
1803         DBG("");
1804
1805         str = __connman_ipconfig_method2string(ipconfig->method);
1806         if (str == NULL)
1807                 return;
1808
1809         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1810
1811         switch (ipconfig->method) {
1812         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1813         case CONNMAN_IPCONFIG_METHOD_OFF:
1814         case CONNMAN_IPCONFIG_METHOD_FIXED:
1815         case CONNMAN_IPCONFIG_METHOD_DHCP:
1816         case CONNMAN_IPCONFIG_METHOD_AUTO:
1817                 return;
1818         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1819                 break;
1820         }
1821
1822         if (ipconfig->address == NULL)
1823                 return;
1824
1825         if (ipconfig->address->local != NULL) {
1826                 in_addr_t addr;
1827                 struct in_addr netmask;
1828                 char *mask;
1829
1830                 connman_dbus_dict_append_basic(iter, "Address",
1831                                 DBUS_TYPE_STRING, &ipconfig->address->local);
1832
1833                 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1834                 netmask.s_addr = htonl(addr);
1835                 mask = inet_ntoa(netmask);
1836                 connman_dbus_dict_append_basic(iter, "Netmask",
1837                                                 DBUS_TYPE_STRING, &mask);
1838         }
1839
1840         if (ipconfig->address->gateway != NULL)
1841                 connman_dbus_dict_append_basic(iter, "Gateway",
1842                                 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1843 }
1844
1845 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1846                                                         DBusMessageIter *array)
1847 {
1848         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1849         const char *address = NULL, *netmask = NULL, *gateway = NULL,
1850                 *prefix_length_string = NULL, *privacy_string = NULL;
1851         int prefix_length = 0, privacy = 0;
1852         DBusMessageIter dict;
1853
1854         DBG("ipconfig %p", ipconfig);
1855
1856         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1857                 return -EINVAL;
1858
1859         dbus_message_iter_recurse(array, &dict);
1860
1861         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1862                 DBusMessageIter entry;
1863                 const char *key;
1864                 int type;
1865
1866                 dbus_message_iter_recurse(&dict, &entry);
1867
1868                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1869                         return -EINVAL;
1870
1871                 dbus_message_iter_get_basic(&entry, &key);
1872                 dbus_message_iter_next(&entry);
1873
1874                 type = dbus_message_iter_get_arg_type(&entry);
1875
1876                 if (g_str_equal(key, "Method") == TRUE) {
1877                         const char *str;
1878
1879                         if (type != DBUS_TYPE_STRING)
1880                                 return -EINVAL;
1881
1882                         dbus_message_iter_get_basic(&entry, &str);
1883                         method = __connman_ipconfig_string2method(str);
1884                 } else if (g_str_equal(key, "Address") == TRUE) {
1885                         if (type != DBUS_TYPE_STRING)
1886                                 return -EINVAL;
1887
1888                         dbus_message_iter_get_basic(&entry, &address);
1889                 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1890                         if (type != DBUS_TYPE_STRING)
1891                                 return -EINVAL;
1892
1893                         dbus_message_iter_get_basic(&entry,
1894                                                         &prefix_length_string);
1895
1896                         prefix_length = atoi(prefix_length_string);
1897                         if (prefix_length < 0 || prefix_length > 128)
1898                                 return -EINVAL;
1899
1900                 } else if (g_str_equal(key, "Netmask") == TRUE) {
1901                         if (type != DBUS_TYPE_STRING)
1902                                 return -EINVAL;
1903
1904                         dbus_message_iter_get_basic(&entry, &netmask);
1905                 } else if (g_str_equal(key, "Gateway") == TRUE) {
1906                         if (type != DBUS_TYPE_STRING)
1907                                 return -EINVAL;
1908
1909                         dbus_message_iter_get_basic(&entry, &gateway);
1910                 } else if (g_str_equal(key, "Privacy") == TRUE) {
1911                         if (type != DBUS_TYPE_STRING)
1912                                 return -EINVAL;
1913
1914                         dbus_message_iter_get_basic(&entry, &privacy_string);
1915                         privacy = string2privacy(privacy_string);
1916                 }
1917                 dbus_message_iter_next(&dict);
1918         }
1919
1920         DBG("method %d address %s netmask %s gateway %s prefix_length %d "
1921                 "privacy %s",
1922                 method, address, netmask, gateway, prefix_length,
1923                 privacy_string);
1924
1925         switch (method) {
1926         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1927         case CONNMAN_IPCONFIG_METHOD_FIXED:
1928                 return -EINVAL;
1929
1930         case CONNMAN_IPCONFIG_METHOD_OFF:
1931                 ipconfig->method = method;
1932                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1933                         disable_ipv6(ipconfig);
1934                 break;
1935
1936         case CONNMAN_IPCONFIG_METHOD_AUTO:
1937                 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1938                         return -EINVAL;
1939
1940                 ipconfig->method = method;
1941                 if (privacy_string != NULL)
1942                         ipconfig->ipv6_privacy_config = privacy;
1943                 enable_ipv6(ipconfig);
1944                 break;
1945
1946         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1947                 if (address == NULL)
1948                         return -EINVAL;
1949
1950                 ipconfig->method = method;
1951
1952                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1953                         connman_ipaddress_set_ipv4(ipconfig->address,
1954                                                 address, netmask, gateway);
1955                 else
1956                         return connman_ipaddress_set_ipv6(
1957                                         ipconfig->address, address,
1958                                                 gateway, prefix_length);
1959                 break;
1960
1961         case CONNMAN_IPCONFIG_METHOD_DHCP:
1962                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1963                         return -EOPNOTSUPP;
1964
1965                 ipconfig->method = method;
1966                 break;
1967         }
1968
1969         return 0;
1970 }
1971
1972 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1973                                                         DBusMessageIter *iter)
1974 {
1975         struct connman_ipdevice *ipdevice;
1976         const char *method = "auto";
1977
1978         connman_dbus_dict_append_basic(iter, "Method",
1979                                                 DBUS_TYPE_STRING, &method);
1980
1981         ipdevice = g_hash_table_lookup(ipdevice_hash,
1982                                         GINT_TO_POINTER(ipconfig->index));
1983         if (ipdevice == NULL)
1984                 return;
1985
1986         if (ipdevice->ifname != NULL)
1987                 connman_dbus_dict_append_basic(iter, "Interface",
1988                                         DBUS_TYPE_STRING, &ipdevice->ifname);
1989
1990         if (ipdevice->address != NULL)
1991                 connman_dbus_dict_append_basic(iter, "Address",
1992                                         DBUS_TYPE_STRING, &ipdevice->address);
1993
1994         if (ipdevice->mtu > 0)
1995                 connman_dbus_dict_append_basic(iter, "MTU",
1996                                         DBUS_TYPE_UINT16, &ipdevice->mtu);
1997 }
1998
1999 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2000                 GKeyFile *keyfile, const char *identifier, const char *prefix)
2001 {
2002         char *method;
2003         char *key;
2004
2005         DBG("ipconfig %p identifier %s", ipconfig, identifier);
2006
2007         key = g_strdup_printf("%smethod", prefix);
2008         method = g_key_file_get_string(keyfile, identifier, key, NULL);
2009         if (method == NULL) {
2010                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2011                         ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2012                 else
2013                         ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2014         } else
2015                 ipconfig->method = __connman_ipconfig_string2method(method);
2016
2017         if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2018                 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2019
2020         if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2021                 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2022                         ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2023                         char *privacy;
2024                         char *pprefix = g_strdup_printf("%sprivacy", prefix);
2025                         privacy = g_key_file_get_string(keyfile, identifier,
2026                                                         pprefix, NULL);
2027                         ipconfig->ipv6_privacy_config = string2privacy(privacy);
2028                         g_free(pprefix);
2029                         g_free(privacy);
2030
2031                         __connman_ipconfig_enable(ipconfig);
2032                         enable_ipv6(ipconfig);
2033                 }
2034         }
2035
2036         g_free(method);
2037         g_free(key);
2038
2039         key = g_strdup_printf("%snetmask_prefixlen", prefix);
2040         ipconfig->address->prefixlen = g_key_file_get_integer(
2041                                 keyfile, identifier, key, NULL);
2042         g_free(key);
2043
2044         key = g_strdup_printf("%slocal_address", prefix);
2045         ipconfig->address->local = g_key_file_get_string(
2046                         keyfile, identifier, key, NULL);
2047         g_free(key);
2048
2049         key = g_strdup_printf("%speer_address", prefix);
2050         ipconfig->address->peer = g_key_file_get_string(
2051                                 keyfile, identifier, key, NULL);
2052         g_free(key);
2053
2054         key = g_strdup_printf("%sbroadcast_address", prefix);
2055         ipconfig->address->broadcast = g_key_file_get_string(
2056                                 keyfile, identifier, key, NULL);
2057         g_free(key);
2058
2059         key = g_strdup_printf("%sgateway", prefix);
2060         ipconfig->address->gateway = g_key_file_get_string(
2061                                 keyfile, identifier, key, NULL);
2062         g_free(key);
2063
2064         return 0;
2065 }
2066
2067 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2068                 GKeyFile *keyfile, const char *identifier, const char *prefix)
2069 {
2070         const char *method;
2071         char *key;
2072
2073         DBG("ipconfig %p identifier %s", ipconfig, identifier);
2074
2075         method = __connman_ipconfig_method2string(ipconfig->method);
2076
2077         key = g_strdup_printf("%smethod", prefix);
2078         g_key_file_set_string(keyfile, identifier, key, method);
2079         g_free(key);
2080
2081         if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2082                 const char *privacy;
2083                 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2084                 key = g_strdup_printf("%sprivacy", prefix);
2085                 g_key_file_set_string(keyfile, identifier, key, privacy);
2086                 g_free(key);
2087         }
2088
2089         switch (ipconfig->method) {
2090         case CONNMAN_IPCONFIG_METHOD_FIXED:
2091         case CONNMAN_IPCONFIG_METHOD_MANUAL:
2092                 break;
2093         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2094         case CONNMAN_IPCONFIG_METHOD_OFF:
2095         case CONNMAN_IPCONFIG_METHOD_DHCP:
2096         case CONNMAN_IPCONFIG_METHOD_AUTO:
2097                 return 0;
2098         }
2099
2100         key = g_strdup_printf("%snetmask_prefixlen", prefix);
2101         g_key_file_set_integer(keyfile, identifier,
2102                         key, ipconfig->address->prefixlen);
2103         g_free(key);
2104
2105         key = g_strdup_printf("%slocal_address", prefix);
2106         if (ipconfig->address->local != NULL)
2107                 g_key_file_set_string(keyfile, identifier,
2108                                 key, ipconfig->address->local);
2109         g_free(key);
2110
2111         key = g_strdup_printf("%speer_address", prefix);
2112         if (ipconfig->address->peer != NULL)
2113                 g_key_file_set_string(keyfile, identifier,
2114                                 key, ipconfig->address->peer);
2115         g_free(key);
2116
2117         key = g_strdup_printf("%sbroadcast_address", prefix);
2118         if (ipconfig->address->broadcast != NULL)
2119                 g_key_file_set_string(keyfile, identifier,
2120                         key, ipconfig->address->broadcast);
2121         g_free(key);
2122
2123         key = g_strdup_printf("%sgateway", prefix);
2124         if (ipconfig->address->gateway != NULL)
2125                 g_key_file_set_string(keyfile, identifier,
2126                         key, ipconfig->address->gateway);
2127         g_free(key);
2128
2129         return 0;
2130 }
2131
2132 int __connman_ipconfig_init(void)
2133 {
2134         DBG("");
2135
2136         ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2137                                                         NULL, free_ipdevice);
2138
2139         return 0;
2140 }
2141
2142 void __connman_ipconfig_cleanup(void)
2143 {
2144         DBG("");
2145
2146         g_hash_table_destroy(ipdevice_hash);
2147         ipdevice_hash = NULL;
2148 }