Initial IPv6 support
[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, 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                 connman_ipaddress_copy(ipdevice->config->system, ipaddress);
614
615         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
616                 return;
617
618         if (g_slist_length(ipdevice->address_list) > 1)
619                 return;
620
621         for (list = g_list_first(ipconfig_list); list;
622                                                 list = g_list_next(list)) {
623                 struct connman_ipconfig *ipconfig = list->data;
624
625                 if (index != ipconfig->index)
626                         continue;
627
628                 if (ipconfig->ops == NULL)
629                         continue;
630
631                 if (ipconfig->ops->ip_bound)
632                         ipconfig->ops->ip_bound(ipconfig);
633         }
634 }
635
636 void __connman_ipconfig_deladdr(int index, const char *label,
637                                 unsigned char prefixlen, const char *address)
638 {
639         struct connman_ipdevice *ipdevice;
640         struct connman_ipaddress *ipaddress;
641         GList *list;
642
643         DBG("index %d", index);
644
645         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
646         if (ipdevice == NULL)
647                 return;
648
649         ipaddress = find_ipaddress(ipdevice, prefixlen, address);
650         if (ipaddress == NULL)
651                 return;
652
653         ipdevice->address_list = g_slist_remove(ipdevice->address_list,
654                                                                 ipaddress);
655
656         connman_ipaddress_free(ipaddress);
657
658         connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
659                                                 address, prefixlen, label);
660
661         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
662                 return;
663
664         if (g_slist_length(ipdevice->address_list) > 0)
665                 return;
666
667         for (list = g_list_first(ipconfig_list); list;
668                                                 list = g_list_next(list)) {
669                 struct connman_ipconfig *ipconfig = list->data;
670
671                 if (index != ipconfig->index)
672                         continue;
673
674                 if (ipconfig->ops == NULL)
675                         continue;
676
677                 if (ipconfig->ops->ip_release)
678                         ipconfig->ops->ip_release(ipconfig);
679         }
680 }
681
682 void __connman_ipconfig_newroute(int index, unsigned char scope,
683                                         const char *dst, const char *gateway)
684 {
685         struct connman_ipdevice *ipdevice;
686
687         DBG("index %d", index);
688
689         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
690         if (ipdevice == NULL)
691                 return;
692
693         if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
694                 GSList *list;
695
696                 g_free(ipdevice->gateway);
697                 ipdevice->gateway = g_strdup(gateway);
698
699                 if (ipdevice->config != NULL &&
700                                         ipdevice->config->system != NULL) {
701                         g_free(ipdevice->config->system->gateway);
702                         ipdevice->config->system->gateway = g_strdup(gateway);
703                 }
704
705                 for (list = ipdevice->address_list; list; list = list->next) {
706                         struct connman_ipaddress *ipaddress = list->data;
707
708                         g_free(ipaddress->gateway);
709                         ipaddress->gateway = g_strdup(gateway);
710                 }
711         }
712
713         connman_info("%s {add} route %s gw %s scope %u <%s>",
714                                         ipdevice->ifname, dst, gateway,
715                                                 scope, scope2str(scope));
716 }
717
718 void __connman_ipconfig_delroute(int index, unsigned char scope,
719                                         const char *dst, const char *gateway)
720 {
721         struct connman_ipdevice *ipdevice;
722
723         DBG("index %d", index);
724
725         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
726         if (ipdevice == NULL)
727                 return;
728
729         if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
730                 GSList *list;
731
732                 g_free(ipdevice->gateway);
733                 ipdevice->gateway = NULL;
734
735                 if (ipdevice->config != NULL &&
736                                         ipdevice->config->system != NULL) {
737                         g_free(ipdevice->config->system->gateway);
738                         ipdevice->config->system->gateway = NULL;
739                 }
740
741                 for (list = ipdevice->address_list; list; list = list->next) {
742                         struct connman_ipaddress *ipaddress = list->data;
743
744                         g_free(ipaddress->gateway);
745                         ipaddress->gateway = NULL;
746                 }
747         }
748
749         connman_info("%s {del} route %s gw %s scope %u <%s>",
750                                         ipdevice->ifname, dst, gateway,
751                                                 scope, scope2str(scope));
752 }
753
754 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
755                                                         void *user_data)
756 {
757         GList *list, *keys;
758
759         keys = g_hash_table_get_keys(ipdevice_hash);
760         if (keys == NULL)
761                 return;
762
763         for (list = g_list_first(keys); list; list = g_list_next(list)) {
764                 int index = GPOINTER_TO_INT(list->data);
765
766                 function(index, user_data);
767         }
768
769         g_list_free(keys);
770 }
771
772 unsigned short __connman_ipconfig_get_type(int index)
773 {
774         struct connman_ipdevice *ipdevice;
775
776         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
777         if (ipdevice == NULL)
778                 return ARPHRD_VOID;
779
780         return ipdevice->type;
781 }
782
783 unsigned int __connman_ipconfig_get_flags(int index)
784 {
785         struct connman_ipdevice *ipdevice;
786
787         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
788         if (ipdevice == NULL)
789                 return 0;
790
791         return ipdevice->flags;
792 }
793
794 const char *__connman_ipconfig_get_gateway(int index)
795 {
796         struct connman_ipdevice *ipdevice;
797
798         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
799         if (ipdevice == NULL)
800                 return NULL;
801
802         if (ipdevice->gateway != NULL)
803                 return ipdevice->gateway;
804
805         if (ipdevice->config != NULL &&
806                         ipdevice->config->address != NULL)
807                 return ipdevice->config->address->gateway;
808
809         return NULL;
810 }
811
812 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
813 {
814         ipconfig->index = index;
815
816         if (ipconfig->ipv6 != NULL)
817                 ipconfig->ipv6->index = index;
818 }
819
820 static struct connman_ipconfig *create_ipv6config(int index)
821 {
822         struct connman_ipconfig *ipv6config;
823
824         DBG("index %d", index);
825
826         ipv6config = g_try_new0(struct connman_ipconfig, 1);
827         if (ipv6config == NULL)
828                 return NULL;
829
830         ipv6config->index = index;
831         ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
832
833         ipv6config->address = connman_ipaddress_alloc();
834         if (ipv6config->address == NULL) {
835                 g_free(ipv6config);
836                 return NULL;
837         }
838
839         ipv6config->system = connman_ipaddress_alloc();
840
841         ipv6config->ipv6 = NULL;
842
843         DBG("ipconfig %p", ipv6config);
844
845         return ipv6config;
846 }
847
848 /**
849  * connman_ipconfig_create:
850  *
851  * Allocate a new ipconfig structure.
852  *
853  * Returns: a newly-allocated #connman_ipconfig structure
854  */
855 struct connman_ipconfig *connman_ipconfig_create(int index)
856 {
857         struct connman_ipconfig *ipconfig;
858
859         DBG("index %d", index);
860
861         ipconfig = g_try_new0(struct connman_ipconfig, 1);
862         if (ipconfig == NULL)
863                 return NULL;
864
865         ipconfig->refcount = 1;
866
867         ipconfig->index = index;
868         ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
869
870         ipconfig->address = connman_ipaddress_alloc();
871         if (ipconfig->address == NULL) {
872                 g_free(ipconfig);
873                 return NULL;
874         }
875
876         ipconfig->system = connman_ipaddress_alloc();
877
878         ipconfig->ipv6 = create_ipv6config(index);
879
880         DBG("ipconfig %p", ipconfig);
881
882         return ipconfig;
883 }
884
885 /**
886  * connman_ipconfig_clone:
887  *
888  * Clone an ipconfig structure and create new reference.
889  *
890  * Returns: a newly-allocated #connman_ipconfig structure
891  */
892 struct connman_ipconfig *connman_ipconfig_clone(struct connman_ipconfig *ipconfig)
893 {
894         struct connman_ipconfig *ipconfig_clone;
895
896         DBG("ipconfig %p", ipconfig);
897
898         ipconfig_clone = g_try_new0(struct connman_ipconfig, 1);
899         if (ipconfig_clone == NULL)
900                 return NULL;
901
902         ipconfig_clone->refcount = 1;
903
904         ipconfig_clone->origin = connman_ipconfig_ref(ipconfig);
905
906         ipconfig_clone->index = -1;
907
908         return ipconfig_clone;
909 }
910
911 /**
912  * connman_ipconfig_ref:
913  * @ipconfig: ipconfig structure
914  *
915  * Increase reference counter of ipconfig
916  */
917 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
918 {
919         g_atomic_int_inc(&ipconfig->refcount);
920
921         return ipconfig;
922 }
923
924 static void  free_ipv6config(struct connman_ipconfig *ipconfig)
925 {
926         if (ipconfig == NULL)
927                 return;
928
929         connman_ipconfig_set_ops(ipconfig, NULL);
930         connman_ipaddress_free(ipconfig->system);
931         connman_ipaddress_free(ipconfig->address);
932         g_free(ipconfig->ipv6);
933 }
934
935 /**
936  * connman_ipconfig_unref:
937  * @ipconfig: ipconfig structure
938  *
939  * Decrease reference counter of ipconfig
940  */
941 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
942 {
943         if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
944                 __connman_ipconfig_disable(ipconfig);
945
946                 connman_ipconfig_set_ops(ipconfig, NULL);
947
948                 if (ipconfig->origin != NULL) {
949                         connman_ipconfig_unref(ipconfig->origin);
950                         ipconfig->origin = NULL;
951                 }
952
953                 connman_ipaddress_free(ipconfig->system);
954                 connman_ipaddress_free(ipconfig->address);
955                 free_ipv6config(ipconfig->ipv6);
956                 g_free(ipconfig);
957         }
958 }
959
960 /**
961  * connman_ipconfig_get_data:
962  * @ipconfig: ipconfig structure
963  *
964  * Get private data pointer
965  */
966 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
967 {
968         return ipconfig->ops_data;
969 }
970
971 /**
972  * connman_ipconfig_set_data:
973  * @ipconfig: ipconfig structure
974  * @data: data pointer
975  *
976  * Set private data pointer
977  */
978 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
979 {
980         ipconfig->ops_data = data;
981 }
982
983 /**
984  * connman_ipconfig_get_index:
985  * @ipconfig: ipconfig structure
986  *
987  * Get interface index
988  */
989 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
990 {
991         if (ipconfig == NULL)
992                 return -1;
993
994         if (ipconfig->origin != NULL)
995                 return ipconfig->origin->index;
996
997         return ipconfig->index;
998 }
999
1000 /**
1001  * connman_ipconfig_get_ifname:
1002  * @ipconfig: ipconfig structure
1003  *
1004  * Get interface name
1005  */
1006 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1007 {
1008         struct connman_ipdevice *ipdevice;
1009
1010         if (ipconfig == NULL)
1011                 return NULL;
1012
1013         if (ipconfig->index < 0)
1014                 return NULL;
1015
1016         ipdevice = g_hash_table_lookup(ipdevice_hash,
1017                                         GINT_TO_POINTER(ipconfig->index));
1018         if (ipdevice == NULL)
1019                 return NULL;
1020
1021         return ipdevice->ifname;
1022 }
1023
1024 /**
1025  * connman_ipconfig_set_ops:
1026  * @ipconfig: ipconfig structure
1027  * @ops: operation callbacks
1028  *
1029  * Set the operation callbacks
1030  */
1031 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1032                                 const struct connman_ipconfig_ops *ops)
1033 {
1034         ipconfig->ops = ops;
1035 }
1036
1037 struct connman_ipconfig *connman_ipconfig_get_ipv6config(
1038                                 struct connman_ipconfig *ipconfig)
1039 {
1040         if (ipconfig == NULL)
1041                 return NULL;
1042
1043         return ipconfig->ipv6;
1044 }
1045
1046 /**
1047  * connman_ipconfig_set_method:
1048  * @ipconfig: ipconfig structure
1049  * @method: configuration method
1050  *
1051  * Set the configuration method
1052  */
1053 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1054                                         enum connman_ipconfig_method method)
1055 {
1056         ipconfig->method = method;
1057
1058         return 0;
1059 }
1060
1061 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1062 {
1063         if (ipconfig == NULL)
1064                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1065
1066         return ipconfig->method;
1067 }
1068
1069 /**
1070  * connman_ipconfig_bind:
1071  * @ipconfig: ipconfig structure
1072  * @ipaddress: ipaddress structure
1073  *
1074  * Bind IP address details to configuration
1075  */
1076 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
1077                                         struct connman_ipaddress *ipaddress)
1078 {
1079         struct connman_ipconfig *origin;
1080
1081         origin = ipconfig->origin ? ipconfig->origin : ipconfig;
1082
1083         connman_ipaddress_copy(origin->address, ipaddress);
1084
1085         connman_inet_set_address(origin->index, origin->address);
1086 }
1087
1088 void __connman_ipconfig_set_element_ipv6_gateway(
1089                         struct connman_ipconfig *ipconfig,
1090                                 struct connman_element *element)
1091 {
1092         element->ipv6.gateway = ipconfig->ipv6->address->gateway;
1093 }
1094
1095 /*
1096  * FIXME: The element soulution should be removed in the future
1097  * Set IPv4 and IPv6 gateway
1098  */
1099 int __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig,
1100                                                 struct connman_element *parent)
1101 {
1102         struct connman_element *connection;
1103
1104         connection = connman_element_create(NULL);
1105
1106         DBG("ipconfig %p", ipconfig);
1107
1108         connection->type  = CONNMAN_ELEMENT_TYPE_CONNECTION;
1109         connection->index = ipconfig->index;
1110         connection->ipv4.gateway = ipconfig->address->gateway;
1111         connection->ipv6.gateway = ipconfig->ipv6->address->gateway;
1112
1113         if (connman_element_register(connection, parent) < 0)
1114                 connman_element_unref(connection);
1115
1116         return 0;
1117 }
1118
1119 int __connman_ipconfig_set_address(struct connman_ipconfig *ipconfig)
1120 {
1121         DBG("");
1122
1123         switch (ipconfig->method) {
1124         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1125         case CONNMAN_IPCONFIG_METHOD_OFF:
1126         case CONNMAN_IPCONFIG_METHOD_FIXED:
1127         case CONNMAN_IPCONFIG_METHOD_DHCP:
1128                 break;
1129         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1130                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1131                         return connman_inet_set_address(ipconfig->index,
1132                                                         ipconfig->address);
1133                 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1134                         return connman_inet_set_ipv6_address(
1135                                         ipconfig->index, ipconfig->address);
1136         }
1137
1138         return 0;
1139 }
1140
1141 int __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
1142 {
1143         DBG("");
1144
1145         if (ipconfig == NULL)
1146                 return 0;
1147
1148         DBG("method %d", ipconfig->method);
1149
1150         switch (ipconfig->method) {
1151         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1152         case CONNMAN_IPCONFIG_METHOD_OFF:
1153         case CONNMAN_IPCONFIG_METHOD_FIXED:
1154         case CONNMAN_IPCONFIG_METHOD_DHCP:
1155                 break;
1156         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1157                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1158                         return connman_inet_clear_address(ipconfig->index);
1159                 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1160                         return connman_inet_clear_ipv6_address(
1161                                                 ipconfig->index,
1162                                                 ipconfig->address->local,
1163                                                 ipconfig->address->prefixlen);
1164         }
1165
1166         return 0;
1167 }
1168
1169 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1170 {
1171         struct connman_ipdevice *ipdevice;
1172         gboolean up = FALSE, down = FALSE;
1173         gboolean lower_up = FALSE, lower_down = FALSE;
1174
1175         DBG("ipconfig %p", ipconfig);
1176
1177         if (ipconfig == NULL || ipconfig->index < 0)
1178                 return -ENODEV;
1179
1180         ipdevice = g_hash_table_lookup(ipdevice_hash,
1181                                         GINT_TO_POINTER(ipconfig->index));
1182         if (ipdevice == NULL)
1183                 return -ENXIO;
1184
1185         if (ipdevice->config == ipconfig)
1186                 return -EALREADY;
1187
1188         if (ipdevice->config != NULL) {
1189                 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1190
1191                 connman_ipaddress_clear(ipdevice->config->system);
1192
1193                 connman_ipconfig_unref(ipdevice->config);
1194         }
1195
1196         ipdevice->config = connman_ipconfig_ref(ipconfig);
1197
1198         ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1199
1200         if (ipdevice->flags & IFF_UP)
1201                 up = TRUE;
1202         else
1203                 down = TRUE;
1204
1205         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1206                         (IFF_RUNNING | IFF_LOWER_UP))
1207                 lower_up = TRUE;
1208         else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1209                 lower_down = TRUE;
1210
1211         if (up == TRUE && ipconfig->ops->up)
1212                 ipconfig->ops->up(ipconfig);
1213         if (lower_up == TRUE && ipconfig->ops->lower_up)
1214                 ipconfig->ops->lower_up(ipconfig);
1215
1216         if (lower_down == TRUE && ipconfig->ops->lower_down)
1217                 ipconfig->ops->lower_down(ipconfig);
1218         if (down == TRUE && ipconfig->ops->down)
1219                 ipconfig->ops->down(ipconfig);
1220
1221         return 0;
1222 }
1223
1224 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1225 {
1226         struct connman_ipdevice *ipdevice;
1227
1228         DBG("ipconfig %p", ipconfig);
1229
1230         if (ipconfig == NULL || ipconfig->index < 0)
1231                 return -ENODEV;
1232
1233         ipdevice = g_hash_table_lookup(ipdevice_hash,
1234                                         GINT_TO_POINTER(ipconfig->index));
1235         if (ipdevice == NULL)
1236                 return -ENXIO;
1237
1238         if (ipdevice->config == NULL || ipdevice->config != ipconfig)
1239                 return -EINVAL;
1240
1241         ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1242
1243         connman_ipaddress_clear(ipdevice->config->system);
1244         connman_ipaddress_clear(ipdevice->config->ipv6->system);
1245
1246         connman_ipconfig_unref(ipdevice->config);
1247         ipdevice->config = NULL;
1248
1249         return 0;
1250 }
1251
1252 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1253 {
1254         switch (method) {
1255         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1256                 break;
1257         case CONNMAN_IPCONFIG_METHOD_OFF:
1258                 return "off";
1259         case CONNMAN_IPCONFIG_METHOD_FIXED:
1260                 return "fixed";
1261         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1262                 return "manual";
1263         case CONNMAN_IPCONFIG_METHOD_DHCP:
1264                 return "dhcp";
1265         }
1266
1267         return NULL;
1268 }
1269
1270 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1271 {
1272         if (g_strcmp0(method, "off") == 0)
1273                 return CONNMAN_IPCONFIG_METHOD_OFF;
1274         else if (g_strcmp0(method, "fixed") == 0)
1275                 return CONNMAN_IPCONFIG_METHOD_FIXED;
1276         else if (g_strcmp0(method, "manual") == 0)
1277                 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1278         else if (g_strcmp0(method, "dhcp") == 0)
1279                 return CONNMAN_IPCONFIG_METHOD_DHCP;
1280         else
1281                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1282 }
1283
1284 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1285                                                         DBusMessageIter *iter)
1286 {
1287         const char *str;
1288
1289         DBG("");
1290
1291         str = __connman_ipconfig_method2string(ipconfig->method);
1292         if (str == NULL)
1293                 return;
1294
1295         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1296
1297         if (ipconfig->system == NULL)
1298                 return;
1299
1300         if (ipconfig->system->local != NULL) {
1301                 in_addr_t addr;
1302                 struct in_addr netmask;
1303                 char *mask;
1304
1305                 connman_dbus_dict_append_basic(iter, "Address",
1306                                 DBUS_TYPE_STRING, &ipconfig->system->local);
1307
1308                 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1309                 netmask.s_addr = htonl(addr);
1310                 mask = inet_ntoa(netmask);
1311                 connman_dbus_dict_append_basic(iter, "Netmask",
1312                                                 DBUS_TYPE_STRING, &mask);
1313         }
1314
1315         if (ipconfig->system->gateway != NULL)
1316                 connman_dbus_dict_append_basic(iter, "Gateway",
1317                                 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1318 }
1319
1320 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1321                                                         DBusMessageIter *iter)
1322 {
1323         const char *str;
1324
1325         DBG("");
1326
1327         str = __connman_ipconfig_method2string(ipconfig->method);
1328         if (str == NULL)
1329                 return;
1330
1331         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1332
1333         if (ipconfig->system == NULL)
1334                 return;
1335
1336         if (ipconfig->system->local != NULL) {
1337                 connman_dbus_dict_append_basic(iter, "Address",
1338                                 DBUS_TYPE_STRING, &ipconfig->system->local);
1339                 connman_dbus_dict_append_basic(iter, "PrefixLength",
1340                                                 DBUS_TYPE_BYTE,
1341                                                 &ipconfig->system->prefixlen);
1342         }
1343
1344         if (ipconfig->system->gateway != NULL)
1345                 connman_dbus_dict_append_basic(iter, "Gateway",
1346                                 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1347 }
1348
1349 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1350                                                         DBusMessageIter *iter)
1351 {
1352         const char *str;
1353
1354         DBG("");
1355
1356         str = __connman_ipconfig_method2string(ipconfig->method);
1357         if (str == NULL)
1358                 return;
1359
1360         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1361
1362         switch (ipconfig->method) {
1363         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1364         case CONNMAN_IPCONFIG_METHOD_OFF:
1365         case CONNMAN_IPCONFIG_METHOD_DHCP:
1366                 return;
1367         case CONNMAN_IPCONFIG_METHOD_FIXED:
1368         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1369                 break;
1370         }
1371
1372         if (ipconfig->address == NULL)
1373                 return;
1374
1375         if (ipconfig->address->local != NULL) {
1376                 connman_dbus_dict_append_basic(iter, "Address",
1377                                 DBUS_TYPE_STRING, &ipconfig->address->local);
1378                 connman_dbus_dict_append_basic(iter, "PrefixLength",
1379                                                 DBUS_TYPE_BYTE,
1380                                                 &ipconfig->address->prefixlen);
1381         }
1382
1383         if (ipconfig->address->gateway != NULL)
1384                 connman_dbus_dict_append_basic(iter, "Gateway",
1385                                 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1386 }
1387
1388 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1389                                                         DBusMessageIter *iter)
1390 {
1391         const char *str;
1392
1393         DBG("");
1394
1395         str = __connman_ipconfig_method2string(ipconfig->method);
1396         if (str == NULL)
1397                 return;
1398
1399         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1400
1401         switch (ipconfig->method) {
1402         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1403         case CONNMAN_IPCONFIG_METHOD_OFF:
1404         case CONNMAN_IPCONFIG_METHOD_FIXED:
1405         case CONNMAN_IPCONFIG_METHOD_DHCP:
1406                 return;
1407         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1408                 break;
1409         }
1410
1411         if (ipconfig->address == NULL)
1412                 return;
1413
1414         if (ipconfig->address->local != NULL) {
1415                 in_addr_t addr;
1416                 struct in_addr netmask;
1417                 char *mask;
1418
1419                 connman_dbus_dict_append_basic(iter, "Address",
1420                                 DBUS_TYPE_STRING, &ipconfig->address->local);
1421
1422                 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1423                 netmask.s_addr = htonl(addr);
1424                 mask = inet_ntoa(netmask);
1425                 connman_dbus_dict_append_basic(iter, "Netmask",
1426                                                 DBUS_TYPE_STRING, &mask);
1427         }
1428
1429         if (ipconfig->address->gateway != NULL)
1430                 connman_dbus_dict_append_basic(iter, "Gateway",
1431                                 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1432 }
1433
1434 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1435                 enum connman_ipconfig_type type, DBusMessageIter *array)
1436 {
1437         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1438         const char *address = NULL, *netmask = NULL, *gateway = NULL,
1439                         *prefix_length_string = NULL;
1440         int prefix_length = 0;
1441         DBusMessageIter dict;
1442
1443         DBG("ipconfig %p type %d", ipconfig, type);
1444
1445         if (type != CONNMAN_IPCONFIG_TYPE_IPV4 &&
1446                         type != CONNMAN_IPCONFIG_TYPE_IPV6)
1447                 return -EINVAL;
1448
1449         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1450                 return -EINVAL;
1451
1452         dbus_message_iter_recurse(array, &dict);
1453
1454         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1455                 DBusMessageIter entry;
1456                 const char *key;
1457                 int type;
1458
1459                 dbus_message_iter_recurse(&dict, &entry);
1460
1461                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1462                         return -EINVAL;
1463
1464                 dbus_message_iter_get_basic(&entry, &key);
1465                 dbus_message_iter_next(&entry);
1466
1467                 type = dbus_message_iter_get_arg_type(&entry);
1468
1469                 if (g_str_equal(key, "Method") == TRUE) {
1470                         const char *str;
1471
1472                         if (type != DBUS_TYPE_STRING)
1473                                 return -EINVAL;
1474
1475                         dbus_message_iter_get_basic(&entry, &str);
1476                         method = __connman_ipconfig_string2method(str);
1477                 } else if (g_str_equal(key, "Address") == TRUE) {
1478                         if (type != DBUS_TYPE_STRING)
1479                                 return -EINVAL;
1480
1481                         dbus_message_iter_get_basic(&entry, &address);
1482                 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1483                         if (type != DBUS_TYPE_STRING)
1484                                 return -EINVAL;
1485
1486                         dbus_message_iter_get_basic(&entry,
1487                                                         &prefix_length_string);
1488
1489                         prefix_length = atoi(prefix_length_string);
1490                         if (prefix_length < 0 || prefix_length > 128)
1491                                 return -EINVAL;
1492
1493                 } else if (g_str_equal(key, "Netmask") == TRUE) {
1494                         if (type != DBUS_TYPE_STRING)
1495                                 return -EINVAL;
1496
1497                         dbus_message_iter_get_basic(&entry, &netmask);
1498                 } else if (g_str_equal(key, "Gateway") == TRUE) {
1499                         if (type != DBUS_TYPE_STRING)
1500                                 return -EINVAL;
1501
1502                         dbus_message_iter_get_basic(&entry, &gateway);
1503                 }
1504                 dbus_message_iter_next(&dict);
1505         }
1506
1507         DBG("method %d address %s netmask %s gateway %s prefix_length %d",
1508                         method, address, netmask, gateway, prefix_length);
1509
1510         switch (method) {
1511         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1512         case CONNMAN_IPCONFIG_METHOD_OFF:
1513         case CONNMAN_IPCONFIG_METHOD_FIXED:
1514                 return -EINVAL;
1515
1516         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1517                 if (address == NULL)
1518                         return -EINVAL;
1519
1520                 ipconfig->method = method;
1521
1522                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1523                         connman_ipaddress_set_ipv4(ipconfig->address,
1524                                                 address, netmask, gateway);
1525                 else
1526                         return connman_ipaddress_set_ipv6(
1527                                         ipconfig->address, address,
1528                                                 gateway, prefix_length);
1529                 break;
1530
1531         case CONNMAN_IPCONFIG_METHOD_DHCP:
1532                 if (ipconfig->method == method)
1533                         return 0;
1534
1535                 ipconfig->method = method;
1536                 break;
1537         }
1538
1539         return 0;
1540 }
1541
1542 void __connman_ipconfig_append_proxy(struct connman_ipconfig *ipconfig,
1543                                                         DBusMessageIter *iter)
1544 {
1545         const char *method = "direct";
1546
1547         connman_dbus_dict_append_basic(iter, "Method",
1548                                                 DBUS_TYPE_STRING, &method);
1549 }
1550
1551 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1552                                                         DBusMessageIter *iter)
1553 {
1554         struct connman_ipdevice *ipdevice;
1555         const char *method = "auto";
1556
1557         connman_dbus_dict_append_basic(iter, "Method",
1558                                                 DBUS_TYPE_STRING, &method);
1559
1560         ipdevice = g_hash_table_lookup(ipdevice_hash,
1561                                         GINT_TO_POINTER(ipconfig->index));
1562         if (ipdevice == NULL)
1563                 return;
1564
1565         if (ipdevice->ifname != NULL)
1566                 connman_dbus_dict_append_basic(iter, "Interface",
1567                                         DBUS_TYPE_STRING, &ipdevice->ifname);
1568
1569         if (ipdevice->address != NULL)
1570                 connman_dbus_dict_append_basic(iter, "Address",
1571                                         DBUS_TYPE_STRING, &ipdevice->address);
1572
1573         if (ipdevice->mtu > 0)
1574                 connman_dbus_dict_append_basic(iter, "MTU",
1575                                         DBUS_TYPE_UINT16, &ipdevice->mtu);
1576 }
1577
1578 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1579                 GKeyFile *keyfile, const char *identifier, const char *prefix)
1580 {
1581         const char *method;
1582         char *key;
1583
1584         DBG("ipconfig %p identifier %s", ipconfig, identifier);
1585
1586         key = g_strdup_printf("%smethod", prefix);
1587         method = g_key_file_get_string(keyfile, identifier, key, NULL);
1588         if (method == NULL)
1589                 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
1590         else
1591                 ipconfig->method = __connman_ipconfig_string2method(method);
1592         g_free(key);
1593
1594         key = g_strdup_printf("%snetmask_prefixlen", prefix);
1595         ipconfig->address->prefixlen = g_key_file_get_integer(
1596                                 keyfile, identifier, key, NULL);
1597         g_free(key);
1598
1599         key = g_strdup_printf("%slocal_address", prefix);
1600         ipconfig->address->local = g_key_file_get_string(
1601                         keyfile, identifier, key, NULL);
1602         g_free(key);
1603
1604         key = g_strdup_printf("%speer_address", prefix);
1605         ipconfig->address->peer = g_key_file_get_string(
1606                                 keyfile, identifier, key, NULL);
1607         g_free(key);
1608
1609         key = g_strdup_printf("%sbroadcast_address", prefix);
1610         ipconfig->address->broadcast = g_key_file_get_string(
1611                                 keyfile, identifier, key, NULL);
1612         g_free(key);
1613
1614         key = g_strdup_printf("%sgateway", prefix);
1615         ipconfig->address->gateway = g_key_file_get_string(
1616                                 keyfile, identifier, key, NULL);
1617         g_free(key);
1618
1619         return 0;
1620 }
1621
1622 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
1623                 GKeyFile *keyfile, const char *identifier, const char *prefix)
1624 {
1625         const char *method;
1626         char *key;
1627
1628         DBG("ipconfig %p identifier %s", ipconfig, identifier);
1629
1630         method = __connman_ipconfig_method2string(ipconfig->method);
1631
1632         key = g_strdup_printf("%smethod", prefix);
1633         g_key_file_set_string(keyfile, identifier, key, method);
1634         g_free(key);
1635
1636         key = g_strdup_printf("%snetmask_prefixlen", prefix);
1637         g_key_file_set_integer(keyfile, identifier,
1638                         key, ipconfig->address->prefixlen);
1639         g_free(key);
1640
1641         key = g_strdup_printf("%slocal_address", prefix);
1642         if (ipconfig->address->local != NULL)
1643                 g_key_file_set_string(keyfile, identifier,
1644                                 key, ipconfig->address->local);
1645         g_free(key);
1646
1647         key = g_strdup_printf("%speer_address", prefix);
1648         if (ipconfig->address->peer != NULL)
1649                 g_key_file_set_string(keyfile, identifier,
1650                                 key, ipconfig->address->peer);
1651         g_free(key);
1652
1653         key = g_strdup_printf("%sbroadcast_address", prefix);
1654         if (ipconfig->address->broadcast != NULL)
1655                 g_key_file_set_string(keyfile, identifier,
1656                         key, ipconfig->address->broadcast);
1657         g_free(key);
1658
1659         key = g_strdup_printf("%sgateway", prefix);
1660         if (ipconfig->address->gateway != NULL)
1661                 g_key_file_set_string(keyfile, identifier,
1662                         key, ipconfig->address->gateway);
1663         g_free(key);
1664
1665         return 0;
1666 }
1667
1668 int __connman_ipconfig_init(void)
1669 {
1670         DBG("");
1671
1672         ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1673                                                         NULL, free_ipdevice);
1674
1675         return 0;
1676 }
1677
1678 void __connman_ipconfig_cleanup(void)
1679 {
1680         DBG("");
1681
1682         g_hash_table_destroy(ipdevice_hash);
1683         ipdevice_hash = NULL;
1684 }