Separate v4 and v6 gateways in connman_ipdevice structure
[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
853         ipv6config->address = connman_ipaddress_alloc(AF_INET6);
854         if (ipv6config->address == NULL) {
855                 g_free(ipv6config);
856                 return NULL;
857         }
858
859         ipv6config->system = connman_ipaddress_alloc(AF_INET6);
860
861         ipv6config->ipv6 = NULL;
862
863         DBG("ipconfig %p", ipv6config);
864
865         return ipv6config;
866 }
867
868 /**
869  * connman_ipconfig_create:
870  *
871  * Allocate a new ipconfig structure.
872  *
873  * Returns: a newly-allocated #connman_ipconfig structure
874  */
875 struct connman_ipconfig *connman_ipconfig_create(int index)
876 {
877         struct connman_ipconfig *ipconfig;
878
879         DBG("index %d", index);
880
881         ipconfig = g_try_new0(struct connman_ipconfig, 1);
882         if (ipconfig == NULL)
883                 return NULL;
884
885         ipconfig->refcount = 1;
886
887         ipconfig->index = index;
888         ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
889
890         ipconfig->address = connman_ipaddress_alloc(AF_INET);
891         if (ipconfig->address == NULL) {
892                 g_free(ipconfig);
893                 return NULL;
894         }
895
896         ipconfig->system = connman_ipaddress_alloc(AF_INET);
897
898         ipconfig->ipv6 = create_ipv6config(index);
899
900         DBG("ipconfig %p", ipconfig);
901
902         return ipconfig;
903 }
904
905 /**
906  * connman_ipconfig_clone:
907  *
908  * Clone an ipconfig structure and create new reference.
909  *
910  * Returns: a newly-allocated #connman_ipconfig structure
911  */
912 struct connman_ipconfig *connman_ipconfig_clone(struct connman_ipconfig *ipconfig)
913 {
914         struct connman_ipconfig *ipconfig_clone;
915
916         DBG("ipconfig %p", ipconfig);
917
918         ipconfig_clone = g_try_new0(struct connman_ipconfig, 1);
919         if (ipconfig_clone == NULL)
920                 return NULL;
921
922         ipconfig_clone->refcount = 1;
923
924         ipconfig_clone->origin = connman_ipconfig_ref(ipconfig);
925
926         ipconfig_clone->index = -1;
927
928         return ipconfig_clone;
929 }
930
931 /**
932  * connman_ipconfig_ref:
933  * @ipconfig: ipconfig structure
934  *
935  * Increase reference counter of ipconfig
936  */
937 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
938 {
939         g_atomic_int_inc(&ipconfig->refcount);
940
941         return ipconfig;
942 }
943
944 static void  free_ipv6config(struct connman_ipconfig *ipconfig)
945 {
946         if (ipconfig == NULL)
947                 return;
948
949         connman_ipconfig_set_ops(ipconfig, NULL);
950         connman_ipaddress_free(ipconfig->system);
951         connman_ipaddress_free(ipconfig->address);
952         g_free(ipconfig->ipv6);
953 }
954
955 /**
956  * connman_ipconfig_unref:
957  * @ipconfig: ipconfig structure
958  *
959  * Decrease reference counter of ipconfig
960  */
961 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
962 {
963         if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
964                 __connman_ipconfig_disable(ipconfig);
965
966                 connman_ipconfig_set_ops(ipconfig, NULL);
967
968                 if (ipconfig->origin != NULL) {
969                         connman_ipconfig_unref(ipconfig->origin);
970                         ipconfig->origin = NULL;
971                 }
972
973                 connman_ipaddress_free(ipconfig->system);
974                 connman_ipaddress_free(ipconfig->address);
975                 free_ipv6config(ipconfig->ipv6);
976                 g_free(ipconfig);
977         }
978 }
979
980 /**
981  * connman_ipconfig_get_data:
982  * @ipconfig: ipconfig structure
983  *
984  * Get private data pointer
985  */
986 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
987 {
988         return ipconfig->ops_data;
989 }
990
991 /**
992  * connman_ipconfig_set_data:
993  * @ipconfig: ipconfig structure
994  * @data: data pointer
995  *
996  * Set private data pointer
997  */
998 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
999 {
1000         ipconfig->ops_data = data;
1001 }
1002
1003 /**
1004  * connman_ipconfig_get_index:
1005  * @ipconfig: ipconfig structure
1006  *
1007  * Get interface index
1008  */
1009 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1010 {
1011         if (ipconfig == NULL)
1012                 return -1;
1013
1014         if (ipconfig->origin != NULL)
1015                 return ipconfig->origin->index;
1016
1017         return ipconfig->index;
1018 }
1019
1020 /**
1021  * connman_ipconfig_get_ifname:
1022  * @ipconfig: ipconfig structure
1023  *
1024  * Get interface name
1025  */
1026 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1027 {
1028         struct connman_ipdevice *ipdevice;
1029
1030         if (ipconfig == NULL)
1031                 return NULL;
1032
1033         if (ipconfig->index < 0)
1034                 return NULL;
1035
1036         ipdevice = g_hash_table_lookup(ipdevice_hash,
1037                                         GINT_TO_POINTER(ipconfig->index));
1038         if (ipdevice == NULL)
1039                 return NULL;
1040
1041         return ipdevice->ifname;
1042 }
1043
1044 /**
1045  * connman_ipconfig_set_ops:
1046  * @ipconfig: ipconfig structure
1047  * @ops: operation callbacks
1048  *
1049  * Set the operation callbacks
1050  */
1051 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1052                                 const struct connman_ipconfig_ops *ops)
1053 {
1054         ipconfig->ops = ops;
1055 }
1056
1057 struct connman_ipconfig *connman_ipconfig_get_ipv6config(
1058                                 struct connman_ipconfig *ipconfig)
1059 {
1060         if (ipconfig == NULL)
1061                 return NULL;
1062
1063         return ipconfig->ipv6;
1064 }
1065
1066 /**
1067  * connman_ipconfig_set_method:
1068  * @ipconfig: ipconfig structure
1069  * @method: configuration method
1070  *
1071  * Set the configuration method
1072  */
1073 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1074                                         enum connman_ipconfig_method method)
1075 {
1076         ipconfig->method = method;
1077
1078         return 0;
1079 }
1080
1081 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1082 {
1083         if (ipconfig == NULL)
1084                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1085
1086         return ipconfig->method;
1087 }
1088
1089 /**
1090  * connman_ipconfig_bind:
1091  * @ipconfig: ipconfig structure
1092  * @ipaddress: ipaddress structure
1093  *
1094  * Bind IP address details to configuration
1095  */
1096 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
1097                                         struct connman_ipaddress *ipaddress)
1098 {
1099         struct connman_ipconfig *origin;
1100
1101         origin = ipconfig->origin ? ipconfig->origin : ipconfig;
1102
1103         connman_ipaddress_copy(origin->address, ipaddress);
1104
1105         connman_inet_set_address(origin->index, origin->address);
1106 }
1107
1108 void __connman_ipconfig_set_element_ipv6_gateway(
1109                         struct connman_ipconfig *ipconfig,
1110                                 struct connman_element *element)
1111 {
1112         element->ipv6.gateway = ipconfig->ipv6->address->gateway;
1113 }
1114
1115 /*
1116  * FIXME: The element soulution should be removed in the future
1117  * Set IPv4 and IPv6 gateway
1118  */
1119 int __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig,
1120                                                 struct connman_element *parent)
1121 {
1122         struct connman_element *connection;
1123
1124         connection = connman_element_create(NULL);
1125
1126         DBG("ipconfig %p", ipconfig);
1127
1128         connection->type  = CONNMAN_ELEMENT_TYPE_CONNECTION;
1129         connection->index = ipconfig->index;
1130         connection->ipv4.gateway = ipconfig->address->gateway;
1131         connection->ipv6.gateway = ipconfig->ipv6->address->gateway;
1132
1133         if (connman_element_register(connection, parent) < 0)
1134                 connman_element_unref(connection);
1135
1136         return 0;
1137 }
1138
1139 int __connman_ipconfig_set_address(struct connman_ipconfig *ipconfig)
1140 {
1141         DBG("");
1142
1143         switch (ipconfig->method) {
1144         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1145         case CONNMAN_IPCONFIG_METHOD_OFF:
1146         case CONNMAN_IPCONFIG_METHOD_FIXED:
1147         case CONNMAN_IPCONFIG_METHOD_DHCP:
1148                 break;
1149         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1150                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1151                         return connman_inet_set_address(ipconfig->index,
1152                                                         ipconfig->address);
1153                 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1154                         return connman_inet_set_ipv6_address(
1155                                         ipconfig->index, ipconfig->address);
1156         }
1157
1158         return 0;
1159 }
1160
1161 int __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
1162 {
1163         DBG("");
1164
1165         if (ipconfig == NULL)
1166                 return 0;
1167
1168         DBG("method %d", ipconfig->method);
1169
1170         switch (ipconfig->method) {
1171         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1172         case CONNMAN_IPCONFIG_METHOD_OFF:
1173         case CONNMAN_IPCONFIG_METHOD_FIXED:
1174         case CONNMAN_IPCONFIG_METHOD_DHCP:
1175                 break;
1176         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1177                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1178                         return connman_inet_clear_address(ipconfig->index);
1179                 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1180                         return connman_inet_clear_ipv6_address(
1181                                                 ipconfig->index,
1182                                                 ipconfig->address->local,
1183                                                 ipconfig->address->prefixlen);
1184         }
1185
1186         return 0;
1187 }
1188
1189 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1190 {
1191         struct connman_ipdevice *ipdevice;
1192         gboolean up = FALSE, down = FALSE;
1193         gboolean lower_up = FALSE, lower_down = FALSE;
1194
1195         DBG("ipconfig %p", ipconfig);
1196
1197         if (ipconfig == NULL || ipconfig->index < 0)
1198                 return -ENODEV;
1199
1200         ipdevice = g_hash_table_lookup(ipdevice_hash,
1201                                         GINT_TO_POINTER(ipconfig->index));
1202         if (ipdevice == NULL)
1203                 return -ENXIO;
1204
1205         if (ipdevice->config == ipconfig)
1206                 return -EALREADY;
1207
1208         if (ipdevice->config != NULL) {
1209                 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1210
1211                 connman_ipaddress_clear(ipdevice->config->system);
1212
1213                 connman_ipconfig_unref(ipdevice->config);
1214         }
1215
1216         ipdevice->config = connman_ipconfig_ref(ipconfig);
1217
1218         ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1219
1220         if (ipdevice->flags & IFF_UP)
1221                 up = TRUE;
1222         else
1223                 down = TRUE;
1224
1225         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1226                         (IFF_RUNNING | IFF_LOWER_UP))
1227                 lower_up = TRUE;
1228         else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1229                 lower_down = TRUE;
1230
1231         if (up == TRUE && ipconfig->ops->up)
1232                 ipconfig->ops->up(ipconfig);
1233         if (lower_up == TRUE && ipconfig->ops->lower_up)
1234                 ipconfig->ops->lower_up(ipconfig);
1235
1236         if (lower_down == TRUE && ipconfig->ops->lower_down)
1237                 ipconfig->ops->lower_down(ipconfig);
1238         if (down == TRUE && ipconfig->ops->down)
1239                 ipconfig->ops->down(ipconfig);
1240
1241         return 0;
1242 }
1243
1244 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1245 {
1246         struct connman_ipdevice *ipdevice;
1247
1248         DBG("ipconfig %p", ipconfig);
1249
1250         if (ipconfig == NULL || ipconfig->index < 0)
1251                 return -ENODEV;
1252
1253         ipdevice = g_hash_table_lookup(ipdevice_hash,
1254                                         GINT_TO_POINTER(ipconfig->index));
1255         if (ipdevice == NULL)
1256                 return -ENXIO;
1257
1258         if (ipdevice->config == NULL || ipdevice->config != ipconfig)
1259                 return -EINVAL;
1260
1261         ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1262
1263         connman_ipaddress_clear(ipdevice->config->system);
1264         connman_ipaddress_clear(ipdevice->config->ipv6->system);
1265
1266         connman_ipconfig_unref(ipdevice->config);
1267         ipdevice->config = NULL;
1268
1269         return 0;
1270 }
1271
1272 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1273 {
1274         switch (method) {
1275         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1276                 break;
1277         case CONNMAN_IPCONFIG_METHOD_OFF:
1278                 return "off";
1279         case CONNMAN_IPCONFIG_METHOD_FIXED:
1280                 return "fixed";
1281         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1282                 return "manual";
1283         case CONNMAN_IPCONFIG_METHOD_DHCP:
1284                 return "dhcp";
1285         }
1286
1287         return NULL;
1288 }
1289
1290 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1291 {
1292         if (g_strcmp0(method, "off") == 0)
1293                 return CONNMAN_IPCONFIG_METHOD_OFF;
1294         else if (g_strcmp0(method, "fixed") == 0)
1295                 return CONNMAN_IPCONFIG_METHOD_FIXED;
1296         else if (g_strcmp0(method, "manual") == 0)
1297                 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1298         else if (g_strcmp0(method, "dhcp") == 0)
1299                 return CONNMAN_IPCONFIG_METHOD_DHCP;
1300         else
1301                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1302 }
1303
1304 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1305                                                         DBusMessageIter *iter)
1306 {
1307         const char *str;
1308
1309         DBG("");
1310
1311         str = __connman_ipconfig_method2string(ipconfig->method);
1312         if (str == NULL)
1313                 return;
1314
1315         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1316
1317         if (ipconfig->system == NULL)
1318                 return;
1319
1320         if (ipconfig->system->local != NULL) {
1321                 in_addr_t addr;
1322                 struct in_addr netmask;
1323                 char *mask;
1324
1325                 connman_dbus_dict_append_basic(iter, "Address",
1326                                 DBUS_TYPE_STRING, &ipconfig->system->local);
1327
1328                 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1329                 netmask.s_addr = htonl(addr);
1330                 mask = inet_ntoa(netmask);
1331                 connman_dbus_dict_append_basic(iter, "Netmask",
1332                                                 DBUS_TYPE_STRING, &mask);
1333         }
1334
1335         if (ipconfig->system->gateway != NULL)
1336                 connman_dbus_dict_append_basic(iter, "Gateway",
1337                                 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1338 }
1339
1340 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1341                                                         DBusMessageIter *iter)
1342 {
1343         const char *str;
1344
1345         DBG("");
1346
1347         str = __connman_ipconfig_method2string(ipconfig->method);
1348         if (str == NULL)
1349                 return;
1350
1351         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1352
1353         if (ipconfig->system == NULL)
1354                 return;
1355
1356         if (ipconfig->system->local != NULL) {
1357                 connman_dbus_dict_append_basic(iter, "Address",
1358                                 DBUS_TYPE_STRING, &ipconfig->system->local);
1359                 connman_dbus_dict_append_basic(iter, "PrefixLength",
1360                                                 DBUS_TYPE_BYTE,
1361                                                 &ipconfig->system->prefixlen);
1362         }
1363
1364         if (ipconfig->system->gateway != NULL)
1365                 connman_dbus_dict_append_basic(iter, "Gateway",
1366                                 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1367 }
1368
1369 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1370                                                         DBusMessageIter *iter)
1371 {
1372         const char *str;
1373
1374         DBG("");
1375
1376         str = __connman_ipconfig_method2string(ipconfig->method);
1377         if (str == NULL)
1378                 return;
1379
1380         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1381
1382         switch (ipconfig->method) {
1383         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1384         case CONNMAN_IPCONFIG_METHOD_OFF:
1385         case CONNMAN_IPCONFIG_METHOD_DHCP:
1386                 return;
1387         case CONNMAN_IPCONFIG_METHOD_FIXED:
1388         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1389                 break;
1390         }
1391
1392         if (ipconfig->address == NULL)
1393                 return;
1394
1395         if (ipconfig->address->local != NULL) {
1396                 connman_dbus_dict_append_basic(iter, "Address",
1397                                 DBUS_TYPE_STRING, &ipconfig->address->local);
1398                 connman_dbus_dict_append_basic(iter, "PrefixLength",
1399                                                 DBUS_TYPE_BYTE,
1400                                                 &ipconfig->address->prefixlen);
1401         }
1402
1403         if (ipconfig->address->gateway != NULL)
1404                 connman_dbus_dict_append_basic(iter, "Gateway",
1405                                 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1406 }
1407
1408 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1409                                                         DBusMessageIter *iter)
1410 {
1411         const char *str;
1412
1413         DBG("");
1414
1415         str = __connman_ipconfig_method2string(ipconfig->method);
1416         if (str == NULL)
1417                 return;
1418
1419         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1420
1421         switch (ipconfig->method) {
1422         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1423         case CONNMAN_IPCONFIG_METHOD_OFF:
1424         case CONNMAN_IPCONFIG_METHOD_FIXED:
1425         case CONNMAN_IPCONFIG_METHOD_DHCP:
1426                 return;
1427         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1428                 break;
1429         }
1430
1431         if (ipconfig->address == NULL)
1432                 return;
1433
1434         if (ipconfig->address->local != NULL) {
1435                 in_addr_t addr;
1436                 struct in_addr netmask;
1437                 char *mask;
1438
1439                 connman_dbus_dict_append_basic(iter, "Address",
1440                                 DBUS_TYPE_STRING, &ipconfig->address->local);
1441
1442                 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1443                 netmask.s_addr = htonl(addr);
1444                 mask = inet_ntoa(netmask);
1445                 connman_dbus_dict_append_basic(iter, "Netmask",
1446                                                 DBUS_TYPE_STRING, &mask);
1447         }
1448
1449         if (ipconfig->address->gateway != NULL)
1450                 connman_dbus_dict_append_basic(iter, "Gateway",
1451                                 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1452 }
1453
1454 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1455                 enum connman_ipconfig_type type, DBusMessageIter *array)
1456 {
1457         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1458         const char *address = NULL, *netmask = NULL, *gateway = NULL,
1459                         *prefix_length_string = NULL;
1460         int prefix_length = 0;
1461         DBusMessageIter dict;
1462
1463         DBG("ipconfig %p type %d", ipconfig, type);
1464
1465         if (type != CONNMAN_IPCONFIG_TYPE_IPV4 &&
1466                         type != CONNMAN_IPCONFIG_TYPE_IPV6)
1467                 return -EINVAL;
1468
1469         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1470                 return -EINVAL;
1471
1472         dbus_message_iter_recurse(array, &dict);
1473
1474         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1475                 DBusMessageIter entry;
1476                 const char *key;
1477                 int type;
1478
1479                 dbus_message_iter_recurse(&dict, &entry);
1480
1481                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1482                         return -EINVAL;
1483
1484                 dbus_message_iter_get_basic(&entry, &key);
1485                 dbus_message_iter_next(&entry);
1486
1487                 type = dbus_message_iter_get_arg_type(&entry);
1488
1489                 if (g_str_equal(key, "Method") == TRUE) {
1490                         const char *str;
1491
1492                         if (type != DBUS_TYPE_STRING)
1493                                 return -EINVAL;
1494
1495                         dbus_message_iter_get_basic(&entry, &str);
1496                         method = __connman_ipconfig_string2method(str);
1497                 } else if (g_str_equal(key, "Address") == TRUE) {
1498                         if (type != DBUS_TYPE_STRING)
1499                                 return -EINVAL;
1500
1501                         dbus_message_iter_get_basic(&entry, &address);
1502                 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1503                         if (type != DBUS_TYPE_STRING)
1504                                 return -EINVAL;
1505
1506                         dbus_message_iter_get_basic(&entry,
1507                                                         &prefix_length_string);
1508
1509                         prefix_length = atoi(prefix_length_string);
1510                         if (prefix_length < 0 || prefix_length > 128)
1511                                 return -EINVAL;
1512
1513                 } else if (g_str_equal(key, "Netmask") == TRUE) {
1514                         if (type != DBUS_TYPE_STRING)
1515                                 return -EINVAL;
1516
1517                         dbus_message_iter_get_basic(&entry, &netmask);
1518                 } else if (g_str_equal(key, "Gateway") == TRUE) {
1519                         if (type != DBUS_TYPE_STRING)
1520                                 return -EINVAL;
1521
1522                         dbus_message_iter_get_basic(&entry, &gateway);
1523                 }
1524                 dbus_message_iter_next(&dict);
1525         }
1526
1527         DBG("method %d address %s netmask %s gateway %s prefix_length %d",
1528                         method, address, netmask, gateway, prefix_length);
1529
1530         switch (method) {
1531         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1532         case CONNMAN_IPCONFIG_METHOD_OFF:
1533         case CONNMAN_IPCONFIG_METHOD_FIXED:
1534                 return -EINVAL;
1535
1536         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1537                 if (address == NULL)
1538                         return -EINVAL;
1539
1540                 ipconfig->method = method;
1541
1542                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1543                         connman_ipaddress_set_ipv4(ipconfig->address,
1544                                                 address, netmask, gateway);
1545                 else
1546                         return connman_ipaddress_set_ipv6(
1547                                         ipconfig->address, address,
1548                                                 gateway, prefix_length);
1549                 break;
1550
1551         case CONNMAN_IPCONFIG_METHOD_DHCP:
1552                 if (ipconfig->method == method)
1553                         return 0;
1554
1555                 ipconfig->method = method;
1556                 break;
1557         }
1558
1559         return 0;
1560 }
1561
1562 void __connman_ipconfig_append_proxy(struct connman_ipconfig *ipconfig,
1563                                                         DBusMessageIter *iter)
1564 {
1565         const char *method = "direct";
1566
1567         connman_dbus_dict_append_basic(iter, "Method",
1568                                                 DBUS_TYPE_STRING, &method);
1569 }
1570
1571 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1572                                                         DBusMessageIter *iter)
1573 {
1574         struct connman_ipdevice *ipdevice;
1575         const char *method = "auto";
1576
1577         connman_dbus_dict_append_basic(iter, "Method",
1578                                                 DBUS_TYPE_STRING, &method);
1579
1580         ipdevice = g_hash_table_lookup(ipdevice_hash,
1581                                         GINT_TO_POINTER(ipconfig->index));
1582         if (ipdevice == NULL)
1583                 return;
1584
1585         if (ipdevice->ifname != NULL)
1586                 connman_dbus_dict_append_basic(iter, "Interface",
1587                                         DBUS_TYPE_STRING, &ipdevice->ifname);
1588
1589         if (ipdevice->address != NULL)
1590                 connman_dbus_dict_append_basic(iter, "Address",
1591                                         DBUS_TYPE_STRING, &ipdevice->address);
1592
1593         if (ipdevice->mtu > 0)
1594                 connman_dbus_dict_append_basic(iter, "MTU",
1595                                         DBUS_TYPE_UINT16, &ipdevice->mtu);
1596 }
1597
1598 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1599                 GKeyFile *keyfile, const char *identifier, const char *prefix)
1600 {
1601         const char *method;
1602         char *key;
1603
1604         DBG("ipconfig %p identifier %s", ipconfig, identifier);
1605
1606         key = g_strdup_printf("%smethod", prefix);
1607         method = g_key_file_get_string(keyfile, identifier, key, NULL);
1608         if (method == NULL)
1609                 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
1610         else
1611                 ipconfig->method = __connman_ipconfig_string2method(method);
1612         g_free(key);
1613
1614         key = g_strdup_printf("%snetmask_prefixlen", prefix);
1615         ipconfig->address->prefixlen = g_key_file_get_integer(
1616                                 keyfile, identifier, key, NULL);
1617         g_free(key);
1618
1619         key = g_strdup_printf("%slocal_address", prefix);
1620         ipconfig->address->local = g_key_file_get_string(
1621                         keyfile, identifier, key, NULL);
1622         g_free(key);
1623
1624         key = g_strdup_printf("%speer_address", prefix);
1625         ipconfig->address->peer = g_key_file_get_string(
1626                                 keyfile, identifier, key, NULL);
1627         g_free(key);
1628
1629         key = g_strdup_printf("%sbroadcast_address", prefix);
1630         ipconfig->address->broadcast = g_key_file_get_string(
1631                                 keyfile, identifier, key, NULL);
1632         g_free(key);
1633
1634         key = g_strdup_printf("%sgateway", prefix);
1635         ipconfig->address->gateway = g_key_file_get_string(
1636                                 keyfile, identifier, key, NULL);
1637         g_free(key);
1638
1639         return 0;
1640 }
1641
1642 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
1643                 GKeyFile *keyfile, const char *identifier, const char *prefix)
1644 {
1645         const char *method;
1646         char *key;
1647
1648         DBG("ipconfig %p identifier %s", ipconfig, identifier);
1649
1650         method = __connman_ipconfig_method2string(ipconfig->method);
1651
1652         key = g_strdup_printf("%smethod", prefix);
1653         g_key_file_set_string(keyfile, identifier, key, method);
1654         g_free(key);
1655
1656         key = g_strdup_printf("%snetmask_prefixlen", prefix);
1657         g_key_file_set_integer(keyfile, identifier,
1658                         key, ipconfig->address->prefixlen);
1659         g_free(key);
1660
1661         key = g_strdup_printf("%slocal_address", prefix);
1662         if (ipconfig->address->local != NULL)
1663                 g_key_file_set_string(keyfile, identifier,
1664                                 key, ipconfig->address->local);
1665         g_free(key);
1666
1667         key = g_strdup_printf("%speer_address", prefix);
1668         if (ipconfig->address->peer != NULL)
1669                 g_key_file_set_string(keyfile, identifier,
1670                                 key, ipconfig->address->peer);
1671         g_free(key);
1672
1673         key = g_strdup_printf("%sbroadcast_address", prefix);
1674         if (ipconfig->address->broadcast != NULL)
1675                 g_key_file_set_string(keyfile, identifier,
1676                         key, ipconfig->address->broadcast);
1677         g_free(key);
1678
1679         key = g_strdup_printf("%sgateway", prefix);
1680         if (ipconfig->address->gateway != NULL)
1681                 g_key_file_set_string(keyfile, identifier,
1682                         key, ipconfig->address->gateway);
1683         g_free(key);
1684
1685         return 0;
1686 }
1687
1688 int __connman_ipconfig_init(void)
1689 {
1690         DBG("");
1691
1692         ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1693                                                         NULL, free_ipdevice);
1694
1695         return 0;
1696 }
1697
1698 void __connman_ipconfig_cleanup(void)
1699 {
1700         DBG("");
1701
1702         g_hash_table_destroy(ipdevice_hash);
1703         ipdevice_hash = NULL;
1704 }