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