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