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