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