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