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