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