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