9d9ff4ad6fbfcb4c3de08ef13c65094aadeb4421
[framework/connectivity/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 <net/if.h>
27 #include <net/if_arp.h>
28 #include <linux/if_link.h>
29 #include <string.h>
30 #include <stdlib.h>
31
32 #ifndef IFF_LOWER_UP
33 #define IFF_LOWER_UP    0x10000
34 #endif
35
36 #include <gdbus.h>
37
38 #include "connman.h"
39
40 struct connman_ipconfig {
41         gint refcount;
42         int index;
43         enum connman_ipconfig_type type;
44
45         struct connman_ipconfig *origin;
46
47         const struct connman_ipconfig_ops *ops;
48         void *ops_data;
49
50         enum connman_ipconfig_method method;
51         struct connman_ipaddress *address;
52         struct connman_ipaddress *system;
53
54         struct connman_ipconfig *ipv6;
55 };
56
57 struct connman_ipdevice {
58         int index;
59         char *ifname;
60         unsigned short type;
61         unsigned int flags;
62         char *address;
63         uint16_t mtu;
64         uint32_t rx_packets;
65         uint32_t tx_packets;
66         uint32_t rx_bytes;
67         uint32_t tx_bytes;
68         uint32_t rx_errors;
69         uint32_t tx_errors;
70         uint32_t rx_dropped;
71         uint32_t tx_dropped;
72
73         GSList *address_list;
74         char *ipv4_gateway;
75         char *ipv6_gateway;
76
77         struct connman_ipconfig *config;
78
79         struct connman_ipconfig_driver *driver;
80         struct connman_ipconfig *driver_config;
81 };
82
83 static GHashTable *ipdevice_hash = NULL;
84 static GList *ipconfig_list = NULL;
85
86 struct connman_ipaddress *connman_ipaddress_alloc(int family)
87 {
88         struct connman_ipaddress *ipaddress;
89
90         ipaddress = g_try_new0(struct connman_ipaddress, 1);
91         if (ipaddress == NULL)
92                 return NULL;
93
94         ipaddress->family = family;
95         ipaddress->prefixlen = 0;
96         ipaddress->local = NULL;
97         ipaddress->peer = NULL;
98         ipaddress->broadcast = NULL;
99         ipaddress->gateway = NULL;
100
101         return ipaddress;
102 }
103
104 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
105 {
106         if (ipaddress == NULL)
107                 return;
108
109         g_free(ipaddress->broadcast);
110         g_free(ipaddress->peer);
111         g_free(ipaddress->local);
112         g_free(ipaddress->gateway);
113         g_free(ipaddress);
114 }
115
116 static unsigned char netmask2prefixlen(const char *netmask)
117 {
118         unsigned char bits = 0;
119         in_addr_t mask = inet_network(netmask);
120         in_addr_t host = ~mask;
121
122         /* a valid netmask must be 2^n - 1 */
123         if ((host & (host + 1)) != 0)
124                 return -1;
125
126         for (; mask; mask <<= 1)
127                 ++bits;
128
129         return bits;
130 }
131
132 static gboolean check_ipv6_address(const char *address)
133 {
134         unsigned char buf[sizeof(struct in6_addr)];
135         int err;
136
137         err = inet_pton(AF_INET6, address, buf);
138         if (err > 0)
139                 return TRUE;
140
141         return FALSE;
142 }
143
144 int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress,
145                                 const char *address, const char *gateway,
146                                                 unsigned char prefix_length)
147 {
148         if (ipaddress == NULL)
149                 return -EINVAL;
150
151         if (check_ipv6_address(address) == FALSE)
152                 return -EINVAL;
153
154         if (check_ipv6_address(gateway) == FALSE)
155                 return -EINVAL;
156
157         DBG("prefix_len %d address %s gateway %s",
158                         prefix_length, address, gateway);
159
160         ipaddress->prefixlen = prefix_length;
161
162         g_free(ipaddress->local);
163         ipaddress->local = g_strdup(address);
164
165         g_free(ipaddress->gateway);
166         ipaddress->gateway = g_strdup(gateway);
167
168         return 0;
169 }
170
171 void connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress,
172                 const char *address, const char *netmask, const char *gateway)
173 {
174         if (ipaddress == NULL)
175                 return;
176
177         if (netmask != NULL)
178                 ipaddress->prefixlen = netmask2prefixlen(netmask);
179         else
180                 ipaddress->prefixlen = 32;
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
189 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
190 {
191         if (ipaddress == NULL)
192                 return;
193
194         ipaddress->prefixlen = 0;
195
196         g_free(ipaddress->local);
197         ipaddress->local = NULL;
198
199         g_free(ipaddress->peer);
200         ipaddress->peer = NULL;
201
202         g_free(ipaddress->broadcast);
203         ipaddress->broadcast = NULL;
204
205         g_free(ipaddress->gateway);
206         ipaddress->gateway = NULL;
207 }
208
209 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
210                                         struct connman_ipaddress *source)
211 {
212         if (ipaddress == NULL || source == NULL)
213                 return;
214
215         ipaddress->family = source->family;
216         ipaddress->prefixlen = source->prefixlen;
217
218         g_free(ipaddress->local);
219         ipaddress->local = g_strdup(source->local);
220
221         g_free(ipaddress->peer);
222         ipaddress->peer = g_strdup(source->peer);
223
224         g_free(ipaddress->broadcast);
225         ipaddress->broadcast = g_strdup(source->broadcast);
226
227         g_free(ipaddress->gateway);
228         ipaddress->gateway = g_strdup(source->gateway);
229 }
230
231 static void free_address_list(struct connman_ipdevice *ipdevice)
232 {
233         GSList *list;
234
235         for (list = ipdevice->address_list; list; list = list->next) {
236                 struct connman_ipaddress *ipaddress = list->data;
237
238                 connman_ipaddress_free(ipaddress);
239                 list->data = NULL;
240         }
241
242         g_slist_free(ipdevice->address_list);
243         ipdevice->address_list = NULL;
244 }
245
246 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
247                                 unsigned char prefixlen, const char *local)
248 {
249         GSList *list;
250
251         for (list = ipdevice->address_list; list; list = list->next) {
252                 struct connman_ipaddress *ipaddress = list->data;
253
254                 if (g_strcmp0(ipaddress->local, local) == 0 &&
255                                         ipaddress->prefixlen == prefixlen)
256                         return ipaddress;
257         }
258
259         return NULL;
260 }
261
262 static const char *type2str(unsigned short type)
263 {
264         switch (type) {
265         case ARPHRD_ETHER:
266                 return "ETHER";
267         case ARPHRD_LOOPBACK:
268                 return "LOOPBACK";
269         case ARPHRD_PPP:
270                 return "PPP";
271         case ARPHRD_NONE:
272                 return "NONE";
273         case ARPHRD_VOID:
274                 return "VOID";
275         }
276
277         return "";
278 }
279
280 static const char *scope2str(unsigned char scope)
281 {
282         switch (scope) {
283         case 0:
284                 return "UNIVERSE";
285         case 253:
286                 return "LINK";
287         }
288
289         return "";
290 }
291
292 static void free_ipdevice(gpointer data)
293 {
294         struct connman_ipdevice *ipdevice = data;
295
296         connman_info("%s {remove} index %d", ipdevice->ifname,
297                                                         ipdevice->index);
298
299         if (ipdevice->config != NULL)
300                 connman_ipconfig_unref(ipdevice->config);
301
302         free_address_list(ipdevice);
303         g_free(ipdevice->ipv4_gateway);
304         g_free(ipdevice->ipv6_gateway);
305
306         g_free(ipdevice->address);
307         g_free(ipdevice->ifname);
308         g_free(ipdevice);
309 }
310
311 static GSList *driver_list = NULL;
312
313 static gint compare_priority(gconstpointer a, gconstpointer b)
314 {
315         const struct connman_ipconfig_driver *driver1 = a;
316         const struct connman_ipconfig_driver *driver2 = b;
317
318         return driver2->priority - driver1->priority;
319 }
320
321 /**
322  * connman_ipconfig_driver_register:
323  * @driver: IP configuration driver
324  *
325  * Register a new IP configuration driver
326  *
327  * Returns: %0 on success
328  */
329 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
330 {
331         DBG("driver %p name %s", driver, driver->name);
332
333         driver_list = g_slist_insert_sorted(driver_list, driver,
334                                                         compare_priority);
335
336         return 0;
337 }
338
339 /**
340  * connman_ipconfig_driver_unregister:
341  * @driver: IP configuration driver
342  *
343  * Remove a previously registered IP configuration driver.
344  */
345 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
346 {
347         DBG("driver %p name %s", driver, driver->name);
348
349         driver_list = g_slist_remove(driver_list, driver);
350 }
351
352 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
353 {
354         GSList *list;
355
356         DBG("ipconfig %p", ipdevice->config);
357
358         if (ipdevice->config == NULL)
359                 return;
360
361         switch (ipdevice->config->method) {
362         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
363         case CONNMAN_IPCONFIG_METHOD_OFF:
364         case CONNMAN_IPCONFIG_METHOD_FIXED:
365         case CONNMAN_IPCONFIG_METHOD_MANUAL:
366                 return;
367         case CONNMAN_IPCONFIG_METHOD_DHCP:
368                 break;
369         }
370
371         if (ipdevice->driver != NULL)
372                 return;
373
374         ipdevice->driver_config = connman_ipconfig_clone(ipdevice->config);
375         if (ipdevice->driver_config == NULL)
376                 return;
377
378         for (list = driver_list; list; list = list->next) {
379                 struct connman_ipconfig_driver *driver = list->data;
380
381                 if (driver->request(ipdevice->driver_config) == 0) {
382                         ipdevice->driver = driver;
383                         break;
384                 }
385         }
386
387         if (ipdevice->driver == NULL) {
388                 connman_ipconfig_unref(ipdevice->driver_config);
389                 ipdevice->driver_config = NULL;
390         }
391 }
392
393 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
394 {
395         DBG("ipconfig %p", ipdevice->config);
396
397         if (ipdevice->config == NULL)
398                 return;
399
400         if (ipdevice->driver == NULL)
401                 return;
402
403         ipdevice->driver->release(ipdevice->driver_config);
404
405         ipdevice->driver = NULL;
406
407         connman_ipconfig_unref(ipdevice->driver_config);
408         ipdevice->driver_config = NULL;
409
410         connman_inet_clear_address(ipdevice->index);
411         connman_inet_clear_ipv6_address(ipdevice->index,
412                         ipdevice->driver_config->address->local,
413                         ipdevice->driver_config->address->prefixlen);
414 }
415
416 static void update_stats(struct connman_ipdevice *ipdevice,
417                                                 struct rtnl_link_stats *stats)
418 {
419         if (stats->rx_packets == 0 && stats->tx_packets == 0)
420                 return;
421
422         connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
423                                         stats->rx_packets, stats->rx_bytes);
424         connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
425                                         stats->tx_packets, stats->tx_bytes);
426
427         if (ipdevice->config == NULL)
428                 return;
429
430         ipdevice->rx_packets = stats->rx_packets;
431         ipdevice->tx_packets = stats->tx_packets;
432         ipdevice->rx_bytes = stats->rx_bytes;
433         ipdevice->tx_bytes = stats->tx_bytes;
434         ipdevice->rx_errors = stats->rx_errors;
435         ipdevice->tx_errors = stats->tx_errors;
436         ipdevice->rx_dropped = stats->rx_dropped;
437         ipdevice->tx_dropped = stats->tx_dropped;
438
439         __connman_counter_notify(ipdevice->config,
440                                 ipdevice->rx_packets, ipdevice->tx_packets,
441                                 ipdevice->rx_bytes, ipdevice->tx_bytes,
442                                 ipdevice->rx_errors, ipdevice->tx_errors,
443                                 ipdevice->rx_dropped, ipdevice->tx_dropped);
444 }
445
446 void __connman_ipconfig_newlink(int index, unsigned short type,
447                                 unsigned int flags, const char *address,
448                                                         unsigned short mtu,
449                                                 struct rtnl_link_stats *stats)
450 {
451         struct connman_ipdevice *ipdevice;
452         GList *list;
453         GString *str;
454         gboolean up = FALSE, down = FALSE;
455         gboolean lower_up = FALSE, lower_down = FALSE;
456
457         DBG("index %d", index);
458
459         if (type == ARPHRD_LOOPBACK)
460                 return;
461
462         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
463         if (ipdevice != NULL)
464                 goto update;
465
466         ipdevice = g_try_new0(struct connman_ipdevice, 1);
467         if (ipdevice == NULL)
468                 return;
469
470         ipdevice->index = index;
471         ipdevice->ifname = connman_inet_ifname(index);
472         ipdevice->type = type;
473
474         ipdevice->address = g_strdup(address);
475
476         g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
477
478         connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
479                                                 index, type, type2str(type));
480
481 update:
482         ipdevice->mtu = mtu;
483
484         update_stats(ipdevice, stats);
485
486         if (flags == ipdevice->flags)
487                 return;
488
489         if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
490                 if (flags & IFF_UP)
491                         up = TRUE;
492                 else
493                         down = TRUE;
494         }
495
496         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
497                                 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
498                 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
499                                         (IFF_RUNNING | IFF_LOWER_UP))
500                         lower_up = TRUE;
501                 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
502                         lower_down = TRUE;
503         }
504
505         ipdevice->flags = flags;
506
507         str = g_string_new(NULL);
508         if (str == NULL)
509                 return;
510
511         if (flags & IFF_UP)
512                 g_string_append(str, "UP");
513         else
514                 g_string_append(str, "DOWN");
515
516         if (flags & IFF_RUNNING)
517                 g_string_append(str, ",RUNNING");
518
519         if (flags & IFF_LOWER_UP)
520                 g_string_append(str, ",LOWER_UP");
521
522         connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
523                                                         flags, str->str);
524
525         g_string_free(str, TRUE);
526
527         for (list = g_list_first(ipconfig_list); list;
528                                                 list = g_list_next(list)) {
529                 struct connman_ipconfig *ipconfig = list->data;
530
531                 if (index != ipconfig->index)
532                         continue;
533
534                 if (ipconfig->ops == NULL)
535                         continue;
536
537                 if (up == TRUE && ipconfig->ops->up)
538                         ipconfig->ops->up(ipconfig);
539                 if (lower_up == TRUE && ipconfig->ops->lower_up)
540                         ipconfig->ops->lower_up(ipconfig);
541
542                 if (lower_down == TRUE && ipconfig->ops->lower_down)
543                         ipconfig->ops->lower_down(ipconfig);
544                 if (down == TRUE && ipconfig->ops->down)
545                         ipconfig->ops->down(ipconfig);
546         }
547
548         if (lower_up)
549                 __connman_ipconfig_lower_up(ipdevice);
550         if (lower_down)
551                 __connman_ipconfig_lower_down(ipdevice);
552 }
553
554 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
555 {
556         struct connman_ipdevice *ipdevice;
557         GList *list;
558
559         DBG("index %d", index);
560
561         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
562         if (ipdevice == NULL)
563                 return;
564
565         update_stats(ipdevice, stats);
566
567         for (list = g_list_first(ipconfig_list); list;
568                                                 list = g_list_next(list)) {
569                 struct connman_ipconfig *ipconfig = list->data;
570
571                 if (index != ipconfig->index)
572                         continue;
573
574                 ipconfig->index = -1;
575
576                 if (ipconfig->ops == NULL)
577                         continue;
578
579                 if (ipconfig->ops->lower_down)
580                         ipconfig->ops->lower_down(ipconfig);
581                 if (ipconfig->ops->down)
582                         ipconfig->ops->down(ipconfig);
583         }
584
585         __connman_ipconfig_lower_down(ipdevice);
586
587         g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
588 }
589
590 void __connman_ipconfig_newaddr(int index, int family, const char *label,
591                                 unsigned char prefixlen, const char *address)
592 {
593         struct connman_ipdevice *ipdevice;
594         struct connman_ipaddress *ipaddress;
595         GList *list;
596
597         DBG("index %d", index);
598
599         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
600         if (ipdevice == NULL)
601                 return;
602
603         ipaddress = connman_ipaddress_alloc(family);
604         if (ipaddress == NULL)
605                 return;
606
607         ipaddress->prefixlen = prefixlen;
608         ipaddress->local = g_strdup(address);
609
610         ipdevice->address_list = g_slist_append(ipdevice->address_list,
611                                                                 ipaddress);
612
613         connman_info("%s {add} address %s/%u label %s", ipdevice->ifname,
614                                                 address, prefixlen, label);
615
616         if (ipdevice->config != NULL) {
617                 if (family == AF_INET6 && ipdevice->config->ipv6 != NULL)
618                         connman_ipaddress_copy(ipdevice->config->ipv6->system,
619                                                         ipaddress);
620                 else
621                         connman_ipaddress_copy(ipdevice->config->system,
622                                                         ipaddress);
623         }
624
625         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
626                 return;
627
628         if (g_slist_length(ipdevice->address_list) > 1)
629                 return;
630
631         for (list = g_list_first(ipconfig_list); list;
632                                                 list = g_list_next(list)) {
633                 struct connman_ipconfig *ipconfig = list->data;
634
635                 if (index != ipconfig->index)
636                         continue;
637
638                 if (ipconfig->ops == NULL)
639                         continue;
640
641                 if (ipconfig->ops->ip_bound)
642                         ipconfig->ops->ip_bound(ipconfig);
643         }
644 }
645
646 void __connman_ipconfig_deladdr(int index, int family, const char *label,
647                                 unsigned char prefixlen, const char *address)
648 {
649         struct connman_ipdevice *ipdevice;
650         struct connman_ipaddress *ipaddress;
651         GList *list;
652
653         DBG("index %d", index);
654
655         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
656         if (ipdevice == NULL)
657                 return;
658
659         ipaddress = find_ipaddress(ipdevice, prefixlen, address);
660         if (ipaddress == NULL)
661                 return;
662
663         ipdevice->address_list = g_slist_remove(ipdevice->address_list,
664                                                                 ipaddress);
665
666         connman_ipaddress_free(ipaddress);
667
668         connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
669                                                 address, prefixlen, label);
670
671         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
672                 return;
673
674         if (g_slist_length(ipdevice->address_list) > 0)
675                 return;
676
677         for (list = g_list_first(ipconfig_list); list;
678                                                 list = g_list_next(list)) {
679                 struct connman_ipconfig *ipconfig = list->data;
680
681                 if (index != ipconfig->index)
682                         continue;
683
684                 if (ipconfig->ops == NULL)
685                         continue;
686
687                 if (ipconfig->ops->ip_release)
688                         ipconfig->ops->ip_release(ipconfig);
689         }
690 }
691
692 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
693                                         const char *dst, const char *gateway)
694 {
695         struct connman_ipdevice *ipdevice;
696
697         DBG("index %d", index);
698
699         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
700         if (ipdevice == NULL)
701                 return;
702
703         if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
704                 GSList *list;
705
706                 if (family == AF_INET6) {
707                         g_free(ipdevice->ipv6_gateway);
708                         ipdevice->ipv6_gateway = g_strdup(gateway);
709                 } else {
710                         g_free(ipdevice->ipv4_gateway);
711                         ipdevice->ipv4_gateway = g_strdup(gateway);
712                 }
713
714                 if (ipdevice->config != NULL &&
715                                         ipdevice->config->system != NULL) {
716                         g_free(ipdevice->config->system->gateway);
717                         ipdevice->config->system->gateway = g_strdup(gateway);
718                 }
719
720                 for (list = ipdevice->address_list; list; list = list->next) {
721                         struct connman_ipaddress *ipaddress = list->data;
722
723                         g_free(ipaddress->gateway);
724                         ipaddress->gateway = g_strdup(gateway);
725                 }
726         }
727
728         connman_info("%s {add} route %s gw %s scope %u <%s>",
729                                         ipdevice->ifname, dst, gateway,
730                                                 scope, scope2str(scope));
731 }
732
733 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
734                                         const char *dst, const char *gateway)
735 {
736         struct connman_ipdevice *ipdevice;
737
738         DBG("index %d", index);
739
740         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
741         if (ipdevice == NULL)
742                 return;
743
744         if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
745                 GSList *list;
746
747                 if (family == AF_INET6) {
748                         g_free(ipdevice->ipv6_gateway);
749                         ipdevice->ipv6_gateway = NULL;
750                 } else {
751                         g_free(ipdevice->ipv4_gateway);
752                         ipdevice->ipv4_gateway = NULL;
753                 }
754
755                 if (ipdevice->config != NULL &&
756                                         ipdevice->config->system != NULL) {
757                         g_free(ipdevice->config->system->gateway);
758                         ipdevice->config->system->gateway = NULL;
759                 }
760
761                 for (list = ipdevice->address_list; list; list = list->next) {
762                         struct connman_ipaddress *ipaddress = list->data;
763
764                         g_free(ipaddress->gateway);
765                         ipaddress->gateway = NULL;
766                 }
767         }
768
769         connman_info("%s {del} route %s gw %s scope %u <%s>",
770                                         ipdevice->ifname, dst, gateway,
771                                                 scope, scope2str(scope));
772 }
773
774 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
775                                                         void *user_data)
776 {
777         GList *list, *keys;
778
779         keys = g_hash_table_get_keys(ipdevice_hash);
780         if (keys == NULL)
781                 return;
782
783         for (list = g_list_first(keys); list; list = g_list_next(list)) {
784                 int index = GPOINTER_TO_INT(list->data);
785
786                 function(index, user_data);
787         }
788
789         g_list_free(keys);
790 }
791
792 unsigned short __connman_ipconfig_get_type(int index)
793 {
794         struct connman_ipdevice *ipdevice;
795
796         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
797         if (ipdevice == NULL)
798                 return ARPHRD_VOID;
799
800         return ipdevice->type;
801 }
802
803 unsigned int __connman_ipconfig_get_flags(int index)
804 {
805         struct connman_ipdevice *ipdevice;
806
807         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
808         if (ipdevice == NULL)
809                 return 0;
810
811         return ipdevice->flags;
812 }
813
814 const char *__connman_ipconfig_get_gateway(int index)
815 {
816         struct connman_ipdevice *ipdevice;
817
818         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
819         if (ipdevice == NULL)
820                 return NULL;
821
822         if (ipdevice->ipv4_gateway != NULL)
823                 return ipdevice->ipv4_gateway;
824
825         if (ipdevice->config != NULL &&
826                         ipdevice->config->address != NULL)
827                 return ipdevice->config->address->gateway;
828
829         return NULL;
830 }
831
832 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
833 {
834         ipconfig->index = index;
835
836         if (ipconfig->ipv6 != NULL)
837                 ipconfig->ipv6->index = index;
838 }
839
840 static struct connman_ipconfig *create_ipv6config(int index)
841 {
842         struct connman_ipconfig *ipv6config;
843
844         DBG("index %d", index);
845
846         ipv6config = g_try_new0(struct connman_ipconfig, 1);
847         if (ipv6config == NULL)
848                 return NULL;
849
850         ipv6config->index = index;
851         ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
852         ipv6config->method = CONNMAN_IPCONFIG_METHOD_OFF;
853
854         ipv6config->address = connman_ipaddress_alloc(AF_INET6);
855         if (ipv6config->address == NULL) {
856                 g_free(ipv6config);
857                 return NULL;
858         }
859
860         ipv6config->system = connman_ipaddress_alloc(AF_INET6);
861
862         ipv6config->ipv6 = NULL;
863
864         DBG("ipconfig %p", ipv6config);
865
866         return ipv6config;
867 }
868
869 /**
870  * connman_ipconfig_create:
871  *
872  * Allocate a new ipconfig structure.
873  *
874  * Returns: a newly-allocated #connman_ipconfig structure
875  */
876 struct connman_ipconfig *connman_ipconfig_create(int index)
877 {
878         struct connman_ipconfig *ipconfig;
879
880         DBG("index %d", index);
881
882         ipconfig = g_try_new0(struct connman_ipconfig, 1);
883         if (ipconfig == NULL)
884                 return NULL;
885
886         ipconfig->refcount = 1;
887
888         ipconfig->index = index;
889         ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
890
891         ipconfig->address = connman_ipaddress_alloc(AF_INET);
892         if (ipconfig->address == NULL) {
893                 g_free(ipconfig);
894                 return NULL;
895         }
896
897         ipconfig->system = connman_ipaddress_alloc(AF_INET);
898
899         ipconfig->ipv6 = create_ipv6config(index);
900
901         DBG("ipconfig %p", ipconfig);
902
903         return ipconfig;
904 }
905
906 /**
907  * connman_ipconfig_clone:
908  *
909  * Clone an ipconfig structure and create new reference.
910  *
911  * Returns: a newly-allocated #connman_ipconfig structure
912  */
913 struct connman_ipconfig *connman_ipconfig_clone(struct connman_ipconfig *ipconfig)
914 {
915         struct connman_ipconfig *ipconfig_clone;
916
917         DBG("ipconfig %p", ipconfig);
918
919         ipconfig_clone = g_try_new0(struct connman_ipconfig, 1);
920         if (ipconfig_clone == NULL)
921                 return NULL;
922
923         ipconfig_clone->refcount = 1;
924
925         ipconfig_clone->origin = connman_ipconfig_ref(ipconfig);
926
927         ipconfig_clone->index = -1;
928
929         return ipconfig_clone;
930 }
931
932 /**
933  * connman_ipconfig_ref:
934  * @ipconfig: ipconfig structure
935  *
936  * Increase reference counter of ipconfig
937  */
938 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
939 {
940         g_atomic_int_inc(&ipconfig->refcount);
941
942         return ipconfig;
943 }
944
945 static void  free_ipv6config(struct connman_ipconfig *ipconfig)
946 {
947         if (ipconfig == NULL)
948                 return;
949
950         connman_ipconfig_set_ops(ipconfig, NULL);
951         connman_ipaddress_free(ipconfig->system);
952         connman_ipaddress_free(ipconfig->address);
953         g_free(ipconfig->ipv6);
954 }
955
956 /**
957  * connman_ipconfig_unref:
958  * @ipconfig: ipconfig structure
959  *
960  * Decrease reference counter of ipconfig
961  */
962 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
963 {
964         if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
965                 __connman_ipconfig_disable(ipconfig);
966
967                 connman_ipconfig_set_ops(ipconfig, NULL);
968
969                 if (ipconfig->origin != NULL) {
970                         connman_ipconfig_unref(ipconfig->origin);
971                         ipconfig->origin = NULL;
972                 }
973
974                 connman_ipaddress_free(ipconfig->system);
975                 connman_ipaddress_free(ipconfig->address);
976                 free_ipv6config(ipconfig->ipv6);
977                 g_free(ipconfig);
978         }
979 }
980
981 /**
982  * connman_ipconfig_get_data:
983  * @ipconfig: ipconfig structure
984  *
985  * Get private data pointer
986  */
987 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
988 {
989         return ipconfig->ops_data;
990 }
991
992 /**
993  * connman_ipconfig_set_data:
994  * @ipconfig: ipconfig structure
995  * @data: data pointer
996  *
997  * Set private data pointer
998  */
999 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1000 {
1001         ipconfig->ops_data = data;
1002 }
1003
1004 /**
1005  * connman_ipconfig_get_index:
1006  * @ipconfig: ipconfig structure
1007  *
1008  * Get interface index
1009  */
1010 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1011 {
1012         if (ipconfig == NULL)
1013                 return -1;
1014
1015         if (ipconfig->origin != NULL)
1016                 return ipconfig->origin->index;
1017
1018         return ipconfig->index;
1019 }
1020
1021 /**
1022  * connman_ipconfig_get_ifname:
1023  * @ipconfig: ipconfig structure
1024  *
1025  * Get interface name
1026  */
1027 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1028 {
1029         struct connman_ipdevice *ipdevice;
1030
1031         if (ipconfig == NULL)
1032                 return NULL;
1033
1034         if (ipconfig->index < 0)
1035                 return NULL;
1036
1037         ipdevice = g_hash_table_lookup(ipdevice_hash,
1038                                         GINT_TO_POINTER(ipconfig->index));
1039         if (ipdevice == NULL)
1040                 return NULL;
1041
1042         return ipdevice->ifname;
1043 }
1044
1045 /**
1046  * connman_ipconfig_set_ops:
1047  * @ipconfig: ipconfig structure
1048  * @ops: operation callbacks
1049  *
1050  * Set the operation callbacks
1051  */
1052 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1053                                 const struct connman_ipconfig_ops *ops)
1054 {
1055         ipconfig->ops = ops;
1056 }
1057
1058 struct connman_ipconfig *connman_ipconfig_get_ipv6config(
1059                                 struct connman_ipconfig *ipconfig)
1060 {
1061         if (ipconfig == NULL)
1062                 return NULL;
1063
1064         return ipconfig->ipv6;
1065 }
1066
1067 /**
1068  * connman_ipconfig_set_method:
1069  * @ipconfig: ipconfig structure
1070  * @method: configuration method
1071  *
1072  * Set the configuration method
1073  */
1074 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1075                                         enum connman_ipconfig_method method)
1076 {
1077         ipconfig->method = method;
1078
1079         return 0;
1080 }
1081
1082 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1083 {
1084         if (ipconfig == NULL)
1085                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1086
1087         return ipconfig->method;
1088 }
1089
1090 /**
1091  * connman_ipconfig_bind:
1092  * @ipconfig: ipconfig structure
1093  * @ipaddress: ipaddress structure
1094  *
1095  * Bind IP address details to configuration
1096  */
1097 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
1098                                         struct connman_ipaddress *ipaddress)
1099 {
1100         struct connman_ipconfig *origin;
1101
1102         origin = ipconfig->origin ? ipconfig->origin : ipconfig;
1103
1104         connman_ipaddress_copy(origin->address, ipaddress);
1105
1106         connman_inet_set_address(origin->index, origin->address);
1107 }
1108
1109 void __connman_ipconfig_set_element_ipv6_gateway(
1110                         struct connman_ipconfig *ipconfig,
1111                                 struct connman_element *element)
1112 {
1113         element->ipv6.gateway = ipconfig->ipv6->address->gateway;
1114 }
1115
1116 /*
1117  * FIXME: The element soulution should be removed in the future
1118  * Set IPv4 and IPv6 gateway
1119  */
1120 int __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig,
1121                                                 struct connman_element *parent)
1122 {
1123         struct connman_element *connection;
1124
1125         connection = connman_element_create(NULL);
1126
1127         DBG("ipconfig %p", ipconfig);
1128
1129         connection->type  = CONNMAN_ELEMENT_TYPE_CONNECTION;
1130         connection->index = ipconfig->index;
1131         connection->ipv4.gateway = ipconfig->address->gateway;
1132         connection->ipv6.gateway = ipconfig->ipv6->address->gateway;
1133
1134         if (connman_element_register(connection, parent) < 0)
1135                 connman_element_unref(connection);
1136
1137         return 0;
1138 }
1139
1140 int __connman_ipconfig_set_address(struct connman_ipconfig *ipconfig)
1141 {
1142         DBG("");
1143
1144         switch (ipconfig->method) {
1145         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1146         case CONNMAN_IPCONFIG_METHOD_OFF:
1147         case CONNMAN_IPCONFIG_METHOD_FIXED:
1148         case CONNMAN_IPCONFIG_METHOD_DHCP:
1149                 break;
1150         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1151                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1152                         return connman_inet_set_address(ipconfig->index,
1153                                                         ipconfig->address);
1154                 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1155                         return connman_inet_set_ipv6_address(
1156                                         ipconfig->index, ipconfig->address);
1157         }
1158
1159         return 0;
1160 }
1161
1162 int __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
1163 {
1164         DBG("");
1165
1166         if (ipconfig == NULL)
1167                 return 0;
1168
1169         DBG("method %d", ipconfig->method);
1170
1171         switch (ipconfig->method) {
1172         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1173         case CONNMAN_IPCONFIG_METHOD_OFF:
1174         case CONNMAN_IPCONFIG_METHOD_FIXED:
1175         case CONNMAN_IPCONFIG_METHOD_DHCP:
1176                 break;
1177         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1178                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1179                         return connman_inet_clear_address(ipconfig->index);
1180                 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1181                         return connman_inet_clear_ipv6_address(
1182                                                 ipconfig->index,
1183                                                 ipconfig->address->local,
1184                                                 ipconfig->address->prefixlen);
1185         }
1186
1187         return 0;
1188 }
1189
1190 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1191 {
1192         struct connman_ipdevice *ipdevice;
1193         gboolean up = FALSE, down = FALSE;
1194         gboolean lower_up = FALSE, lower_down = FALSE;
1195
1196         DBG("ipconfig %p", ipconfig);
1197
1198         if (ipconfig == NULL || ipconfig->index < 0)
1199                 return -ENODEV;
1200
1201         ipdevice = g_hash_table_lookup(ipdevice_hash,
1202                                         GINT_TO_POINTER(ipconfig->index));
1203         if (ipdevice == NULL)
1204                 return -ENXIO;
1205
1206         if (ipdevice->config == ipconfig)
1207                 return -EALREADY;
1208
1209         if (ipdevice->config != NULL) {
1210                 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1211
1212                 connman_ipaddress_clear(ipdevice->config->system);
1213
1214                 connman_ipconfig_unref(ipdevice->config);
1215         }
1216
1217         ipdevice->config = connman_ipconfig_ref(ipconfig);
1218
1219         ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1220
1221         if (ipdevice->flags & IFF_UP)
1222                 up = TRUE;
1223         else
1224                 down = TRUE;
1225
1226         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1227                         (IFF_RUNNING | IFF_LOWER_UP))
1228                 lower_up = TRUE;
1229         else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1230                 lower_down = TRUE;
1231
1232         if (up == TRUE && ipconfig->ops->up)
1233                 ipconfig->ops->up(ipconfig);
1234         if (lower_up == TRUE && ipconfig->ops->lower_up)
1235                 ipconfig->ops->lower_up(ipconfig);
1236
1237         if (lower_down == TRUE && ipconfig->ops->lower_down)
1238                 ipconfig->ops->lower_down(ipconfig);
1239         if (down == TRUE && ipconfig->ops->down)
1240                 ipconfig->ops->down(ipconfig);
1241
1242         return 0;
1243 }
1244
1245 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1246 {
1247         struct connman_ipdevice *ipdevice;
1248
1249         DBG("ipconfig %p", ipconfig);
1250
1251         if (ipconfig == NULL || ipconfig->index < 0)
1252                 return -ENODEV;
1253
1254         ipdevice = g_hash_table_lookup(ipdevice_hash,
1255                                         GINT_TO_POINTER(ipconfig->index));
1256         if (ipdevice == NULL)
1257                 return -ENXIO;
1258
1259         if (ipdevice->config == NULL || ipdevice->config != ipconfig)
1260                 return -EINVAL;
1261
1262         ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1263
1264         connman_ipaddress_clear(ipdevice->config->system);
1265         connman_ipaddress_clear(ipdevice->config->ipv6->system);
1266
1267         connman_ipconfig_unref(ipdevice->config);
1268         ipdevice->config = NULL;
1269
1270         return 0;
1271 }
1272
1273 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1274 {
1275         switch (method) {
1276         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1277                 break;
1278         case CONNMAN_IPCONFIG_METHOD_OFF:
1279                 return "off";
1280         case CONNMAN_IPCONFIG_METHOD_FIXED:
1281                 return "fixed";
1282         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1283                 return "manual";
1284         case CONNMAN_IPCONFIG_METHOD_DHCP:
1285                 return "dhcp";
1286         }
1287
1288         return NULL;
1289 }
1290
1291 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1292 {
1293         if (g_strcmp0(method, "off") == 0)
1294                 return CONNMAN_IPCONFIG_METHOD_OFF;
1295         else if (g_strcmp0(method, "fixed") == 0)
1296                 return CONNMAN_IPCONFIG_METHOD_FIXED;
1297         else if (g_strcmp0(method, "manual") == 0)
1298                 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1299         else if (g_strcmp0(method, "dhcp") == 0)
1300                 return CONNMAN_IPCONFIG_METHOD_DHCP;
1301         else
1302                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1303 }
1304
1305 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1306                                                         DBusMessageIter *iter)
1307 {
1308         const char *str;
1309
1310         DBG("");
1311
1312         str = __connman_ipconfig_method2string(ipconfig->method);
1313         if (str == NULL)
1314                 return;
1315
1316         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1317
1318         if (ipconfig->system == NULL)
1319                 return;
1320
1321         if (ipconfig->system->local != NULL) {
1322                 in_addr_t addr;
1323                 struct in_addr netmask;
1324                 char *mask;
1325
1326                 connman_dbus_dict_append_basic(iter, "Address",
1327                                 DBUS_TYPE_STRING, &ipconfig->system->local);
1328
1329                 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1330                 netmask.s_addr = htonl(addr);
1331                 mask = inet_ntoa(netmask);
1332                 connman_dbus_dict_append_basic(iter, "Netmask",
1333                                                 DBUS_TYPE_STRING, &mask);
1334         }
1335
1336         if (ipconfig->system->gateway != NULL)
1337                 connman_dbus_dict_append_basic(iter, "Gateway",
1338                                 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1339 }
1340
1341 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1342                                                         DBusMessageIter *iter)
1343 {
1344         const char *str;
1345
1346         DBG("");
1347
1348         str = __connman_ipconfig_method2string(ipconfig->method);
1349         if (str == NULL)
1350                 return;
1351
1352         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1353
1354         if (ipconfig->system == NULL)
1355                 return;
1356
1357         if (ipconfig->system->local != NULL) {
1358                 connman_dbus_dict_append_basic(iter, "Address",
1359                                 DBUS_TYPE_STRING, &ipconfig->system->local);
1360                 connman_dbus_dict_append_basic(iter, "PrefixLength",
1361                                                 DBUS_TYPE_BYTE,
1362                                                 &ipconfig->system->prefixlen);
1363         }
1364
1365         if (ipconfig->system->gateway != NULL)
1366                 connman_dbus_dict_append_basic(iter, "Gateway",
1367                                 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1368 }
1369
1370 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1371                                                         DBusMessageIter *iter)
1372 {
1373         const char *str;
1374
1375         DBG("");
1376
1377         str = __connman_ipconfig_method2string(ipconfig->method);
1378         if (str == NULL)
1379                 return;
1380
1381         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1382
1383         switch (ipconfig->method) {
1384         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1385         case CONNMAN_IPCONFIG_METHOD_OFF:
1386         case CONNMAN_IPCONFIG_METHOD_DHCP:
1387                 return;
1388         case CONNMAN_IPCONFIG_METHOD_FIXED:
1389         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1390                 break;
1391         }
1392
1393         if (ipconfig->address == NULL)
1394                 return;
1395
1396         if (ipconfig->address->local != NULL) {
1397                 connman_dbus_dict_append_basic(iter, "Address",
1398                                 DBUS_TYPE_STRING, &ipconfig->address->local);
1399                 connman_dbus_dict_append_basic(iter, "PrefixLength",
1400                                                 DBUS_TYPE_BYTE,
1401                                                 &ipconfig->address->prefixlen);
1402         }
1403
1404         if (ipconfig->address->gateway != NULL)
1405                 connman_dbus_dict_append_basic(iter, "Gateway",
1406                                 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1407 }
1408
1409 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1410                                                         DBusMessageIter *iter)
1411 {
1412         const char *str;
1413
1414         DBG("");
1415
1416         str = __connman_ipconfig_method2string(ipconfig->method);
1417         if (str == NULL)
1418                 return;
1419
1420         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1421
1422         switch (ipconfig->method) {
1423         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1424         case CONNMAN_IPCONFIG_METHOD_OFF:
1425         case CONNMAN_IPCONFIG_METHOD_FIXED:
1426         case CONNMAN_IPCONFIG_METHOD_DHCP:
1427                 return;
1428         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1429                 break;
1430         }
1431
1432         if (ipconfig->address == NULL)
1433                 return;
1434
1435         if (ipconfig->address->local != NULL) {
1436                 in_addr_t addr;
1437                 struct in_addr netmask;
1438                 char *mask;
1439
1440                 connman_dbus_dict_append_basic(iter, "Address",
1441                                 DBUS_TYPE_STRING, &ipconfig->address->local);
1442
1443                 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1444                 netmask.s_addr = htonl(addr);
1445                 mask = inet_ntoa(netmask);
1446                 connman_dbus_dict_append_basic(iter, "Netmask",
1447                                                 DBUS_TYPE_STRING, &mask);
1448         }
1449
1450         if (ipconfig->address->gateway != NULL)
1451                 connman_dbus_dict_append_basic(iter, "Gateway",
1452                                 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1453 }
1454
1455 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1456                 enum connman_ipconfig_type type, DBusMessageIter *array)
1457 {
1458         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1459         const char *address = NULL, *netmask = NULL, *gateway = NULL,
1460                         *prefix_length_string = NULL;
1461         int prefix_length = 0;
1462         DBusMessageIter dict;
1463
1464         DBG("ipconfig %p type %d", ipconfig, type);
1465
1466         if (type != CONNMAN_IPCONFIG_TYPE_IPV4 &&
1467                         type != CONNMAN_IPCONFIG_TYPE_IPV6)
1468                 return -EINVAL;
1469
1470         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1471                 return -EINVAL;
1472
1473         dbus_message_iter_recurse(array, &dict);
1474
1475         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1476                 DBusMessageIter entry;
1477                 const char *key;
1478                 int type;
1479
1480                 dbus_message_iter_recurse(&dict, &entry);
1481
1482                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1483                         return -EINVAL;
1484
1485                 dbus_message_iter_get_basic(&entry, &key);
1486                 dbus_message_iter_next(&entry);
1487
1488                 type = dbus_message_iter_get_arg_type(&entry);
1489
1490                 if (g_str_equal(key, "Method") == TRUE) {
1491                         const char *str;
1492
1493                         if (type != DBUS_TYPE_STRING)
1494                                 return -EINVAL;
1495
1496                         dbus_message_iter_get_basic(&entry, &str);
1497                         method = __connman_ipconfig_string2method(str);
1498                 } else if (g_str_equal(key, "Address") == TRUE) {
1499                         if (type != DBUS_TYPE_STRING)
1500                                 return -EINVAL;
1501
1502                         dbus_message_iter_get_basic(&entry, &address);
1503                 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1504                         if (type != DBUS_TYPE_STRING)
1505                                 return -EINVAL;
1506
1507                         dbus_message_iter_get_basic(&entry,
1508                                                         &prefix_length_string);
1509
1510                         prefix_length = atoi(prefix_length_string);
1511                         if (prefix_length < 0 || prefix_length > 128)
1512                                 return -EINVAL;
1513
1514                 } else if (g_str_equal(key, "Netmask") == TRUE) {
1515                         if (type != DBUS_TYPE_STRING)
1516                                 return -EINVAL;
1517
1518                         dbus_message_iter_get_basic(&entry, &netmask);
1519                 } else if (g_str_equal(key, "Gateway") == TRUE) {
1520                         if (type != DBUS_TYPE_STRING)
1521                                 return -EINVAL;
1522
1523                         dbus_message_iter_get_basic(&entry, &gateway);
1524                 }
1525                 dbus_message_iter_next(&dict);
1526         }
1527
1528         DBG("method %d address %s netmask %s gateway %s prefix_length %d",
1529                         method, address, netmask, gateway, prefix_length);
1530
1531         switch (method) {
1532         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1533         case CONNMAN_IPCONFIG_METHOD_OFF:
1534         case CONNMAN_IPCONFIG_METHOD_FIXED:
1535                 return -EINVAL;
1536
1537         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1538                 if (address == NULL)
1539                         return -EINVAL;
1540
1541                 ipconfig->method = method;
1542
1543                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1544                         connman_ipaddress_set_ipv4(ipconfig->address,
1545                                                 address, netmask, gateway);
1546                 else
1547                         return connman_ipaddress_set_ipv6(
1548                                         ipconfig->address, address,
1549                                                 gateway, prefix_length);
1550                 break;
1551
1552         case CONNMAN_IPCONFIG_METHOD_DHCP:
1553                 if (ipconfig->method == method)
1554                         return 0;
1555
1556                 ipconfig->method = method;
1557                 break;
1558         }
1559
1560         return 0;
1561 }
1562
1563 void __connman_ipconfig_append_proxy(struct connman_ipconfig *ipconfig,
1564                                                         DBusMessageIter *iter)
1565 {
1566         const char *method = "direct";
1567
1568         connman_dbus_dict_append_basic(iter, "Method",
1569                                                 DBUS_TYPE_STRING, &method);
1570 }
1571
1572 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1573                                                         DBusMessageIter *iter)
1574 {
1575         struct connman_ipdevice *ipdevice;
1576         const char *method = "auto";
1577
1578         connman_dbus_dict_append_basic(iter, "Method",
1579                                                 DBUS_TYPE_STRING, &method);
1580
1581         ipdevice = g_hash_table_lookup(ipdevice_hash,
1582                                         GINT_TO_POINTER(ipconfig->index));
1583         if (ipdevice == NULL)
1584                 return;
1585
1586         if (ipdevice->ifname != NULL)
1587                 connman_dbus_dict_append_basic(iter, "Interface",
1588                                         DBUS_TYPE_STRING, &ipdevice->ifname);
1589
1590         if (ipdevice->address != NULL)
1591                 connman_dbus_dict_append_basic(iter, "Address",
1592                                         DBUS_TYPE_STRING, &ipdevice->address);
1593
1594         if (ipdevice->mtu > 0)
1595                 connman_dbus_dict_append_basic(iter, "MTU",
1596                                         DBUS_TYPE_UINT16, &ipdevice->mtu);
1597 }
1598
1599 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1600                 GKeyFile *keyfile, const char *identifier, const char *prefix)
1601 {
1602         const char *method;
1603         char *key;
1604
1605         DBG("ipconfig %p identifier %s", ipconfig, identifier);
1606
1607         key = g_strdup_printf("%smethod", prefix);
1608         method = g_key_file_get_string(keyfile, identifier, key, NULL);
1609         if (method == NULL) {
1610                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1611                         ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
1612                 else
1613                         ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
1614         } else
1615                 ipconfig->method = __connman_ipconfig_string2method(method);
1616         g_free(key);
1617
1618         key = g_strdup_printf("%snetmask_prefixlen", prefix);
1619         ipconfig->address->prefixlen = g_key_file_get_integer(
1620                                 keyfile, identifier, key, NULL);
1621         g_free(key);
1622
1623         key = g_strdup_printf("%slocal_address", prefix);
1624         ipconfig->address->local = g_key_file_get_string(
1625                         keyfile, identifier, key, NULL);
1626         g_free(key);
1627
1628         key = g_strdup_printf("%speer_address", prefix);
1629         ipconfig->address->peer = g_key_file_get_string(
1630                                 keyfile, identifier, key, NULL);
1631         g_free(key);
1632
1633         key = g_strdup_printf("%sbroadcast_address", prefix);
1634         ipconfig->address->broadcast = g_key_file_get_string(
1635                                 keyfile, identifier, key, NULL);
1636         g_free(key);
1637
1638         key = g_strdup_printf("%sgateway", prefix);
1639         ipconfig->address->gateway = g_key_file_get_string(
1640                                 keyfile, identifier, key, NULL);
1641         g_free(key);
1642
1643         return 0;
1644 }
1645
1646 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
1647                 GKeyFile *keyfile, const char *identifier, const char *prefix)
1648 {
1649         const char *method;
1650         char *key;
1651
1652         DBG("ipconfig %p identifier %s", ipconfig, identifier);
1653
1654         method = __connman_ipconfig_method2string(ipconfig->method);
1655
1656         key = g_strdup_printf("%smethod", prefix);
1657         g_key_file_set_string(keyfile, identifier, key, method);
1658         g_free(key);
1659
1660         key = g_strdup_printf("%snetmask_prefixlen", prefix);
1661         g_key_file_set_integer(keyfile, identifier,
1662                         key, ipconfig->address->prefixlen);
1663         g_free(key);
1664
1665         key = g_strdup_printf("%slocal_address", prefix);
1666         if (ipconfig->address->local != NULL)
1667                 g_key_file_set_string(keyfile, identifier,
1668                                 key, ipconfig->address->local);
1669         g_free(key);
1670
1671         key = g_strdup_printf("%speer_address", prefix);
1672         if (ipconfig->address->peer != NULL)
1673                 g_key_file_set_string(keyfile, identifier,
1674                                 key, ipconfig->address->peer);
1675         g_free(key);
1676
1677         key = g_strdup_printf("%sbroadcast_address", prefix);
1678         if (ipconfig->address->broadcast != NULL)
1679                 g_key_file_set_string(keyfile, identifier,
1680                         key, ipconfig->address->broadcast);
1681         g_free(key);
1682
1683         key = g_strdup_printf("%sgateway", prefix);
1684         if (ipconfig->address->gateway != NULL)
1685                 g_key_file_set_string(keyfile, identifier,
1686                         key, ipconfig->address->gateway);
1687         g_free(key);
1688
1689         return 0;
1690 }
1691
1692 int __connman_ipconfig_init(void)
1693 {
1694         DBG("");
1695
1696         ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1697                                                         NULL, free_ipdevice);
1698
1699         return 0;
1700 }
1701
1702 void __connman_ipconfig_cleanup(void)
1703 {
1704         DBG("");
1705
1706         g_hash_table_destroy(ipdevice_hash);
1707         ipdevice_hash = NULL;
1708 }