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