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