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