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