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