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