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