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