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