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