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