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