Release tizen_2.0_beta
[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 <errno.h>
27 #include <stdio.h>
28 #include <net/if.h>
29 #include <net/if_arp.h>
30 #include <linux/if_link.h>
31 #include <string.h>
32 #include <stdlib.h>
33
34 #ifndef IFF_LOWER_UP
35 #define IFF_LOWER_UP    0x10000
36 #endif
37
38 #include <gdbus.h>
39
40 #include "connman.h"
41
42 struct connman_ipconfig {
43         int refcount;
44         int index;
45         enum connman_ipconfig_type type;
46
47         struct connman_ipconfig *origin;
48
49         const struct connman_ipconfig_ops *ops;
50         void *ops_data;
51
52         enum connman_ipconfig_method method;
53         struct connman_ipaddress *address;
54         struct connman_ipaddress *system;
55
56         int ipv6_privacy_config;
57         char *last_dhcp_address;
58 };
59
60 struct connman_ipdevice {
61         int index;
62         char *ifname;
63         unsigned short type;
64         unsigned int flags;
65         char *address;
66         uint16_t mtu;
67         uint32_t rx_packets;
68         uint32_t tx_packets;
69         uint32_t rx_bytes;
70         uint32_t tx_bytes;
71         uint32_t rx_errors;
72         uint32_t tx_errors;
73         uint32_t rx_dropped;
74         uint32_t tx_dropped;
75
76         GSList *address_list;
77         char *ipv4_gateway;
78         char *ipv6_gateway;
79
80         char *pac;
81
82         struct connman_ipconfig *config_ipv4;
83         struct connman_ipconfig *config_ipv6;
84
85         gboolean ipv6_enabled;
86         int ipv6_privacy;
87 };
88
89 static GHashTable *ipdevice_hash = NULL;
90 static GList *ipconfig_list = NULL;
91
92 struct connman_ipaddress *connman_ipaddress_alloc(int family)
93 {
94         struct connman_ipaddress *ipaddress;
95
96         ipaddress = g_try_new0(struct connman_ipaddress, 1);
97         if (ipaddress == NULL)
98                 return NULL;
99
100         ipaddress->family = family;
101         ipaddress->prefixlen = 0;
102         ipaddress->local = NULL;
103         ipaddress->peer = NULL;
104         ipaddress->broadcast = NULL;
105         ipaddress->gateway = NULL;
106
107         return ipaddress;
108 }
109
110 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
111 {
112         if (ipaddress == NULL)
113                 return;
114
115         g_free(ipaddress->broadcast);
116         g_free(ipaddress->peer);
117         g_free(ipaddress->local);
118         g_free(ipaddress->gateway);
119         g_free(ipaddress);
120 }
121
122 unsigned char __connman_ipconfig_netmask_prefix_len(const char *netmask)
123 {
124         unsigned char bits;
125         in_addr_t mask;
126         in_addr_t host;
127
128         if (netmask == NULL)
129                 return 32;
130
131         mask = inet_network(netmask);
132         host = ~mask;
133
134         /* a valid netmask must be 2^n - 1 */
135         if ((host & (host + 1)) != 0)
136                 return -1;
137
138         bits = 0;
139         for (; mask; mask <<= 1)
140                 ++bits;
141
142         return bits;
143 }
144
145 static gboolean check_ipv6_address(const char *address)
146 {
147         unsigned char buf[sizeof(struct in6_addr)];
148         int err;
149
150         if (address == NULL)
151                 return FALSE;
152
153         err = inet_pton(AF_INET6, address, buf);
154         if (err > 0)
155                 return TRUE;
156
157         return FALSE;
158 }
159
160 int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress,
161                                 const char *address,
162                                 unsigned char prefix_length,
163                                 const char *gateway)
164 {
165         if (ipaddress == NULL)
166                 return -EINVAL;
167
168         if (check_ipv6_address(address) == FALSE)
169                 return -EINVAL;
170
171         if (check_ipv6_address(gateway) == FALSE)
172                 return -EINVAL;
173
174         DBG("prefix_len %d address %s gateway %s",
175                         prefix_length, address, gateway);
176
177         ipaddress->family = AF_INET6;
178
179         ipaddress->prefixlen = prefix_length;
180
181         g_free(ipaddress->local);
182         ipaddress->local = g_strdup(address);
183
184         g_free(ipaddress->gateway);
185         ipaddress->gateway = g_strdup(gateway);
186
187         return 0;
188 }
189
190 int connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress,
191                 const char *address, const char *netmask, const char *gateway)
192 {
193         if (ipaddress == NULL)
194                 return -EINVAL;
195
196         ipaddress->family = AF_INET;
197
198         ipaddress->prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
199
200         g_free(ipaddress->local);
201         ipaddress->local = g_strdup(address);
202
203         g_free(ipaddress->gateway);
204         ipaddress->gateway = g_strdup(gateway);
205
206         return 0;
207 }
208
209 void connman_ipaddress_set_peer(struct connman_ipaddress *ipaddress,
210                                 const char *peer)
211 {
212         if (ipaddress == NULL)
213                 return;
214
215         g_free(ipaddress->peer);
216         ipaddress->peer = g_strdup(peer);
217 }
218
219 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
220 {
221         if (ipaddress == NULL)
222                 return;
223
224         ipaddress->prefixlen = 0;
225
226         g_free(ipaddress->local);
227         ipaddress->local = NULL;
228
229         g_free(ipaddress->peer);
230         ipaddress->peer = NULL;
231
232         g_free(ipaddress->broadcast);
233         ipaddress->broadcast = NULL;
234
235         g_free(ipaddress->gateway);
236         ipaddress->gateway = NULL;
237 }
238
239 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
240                                         struct connman_ipaddress *source)
241 {
242         if (ipaddress == NULL || source == NULL)
243                 return;
244
245         ipaddress->family = source->family;
246         ipaddress->prefixlen = source->prefixlen;
247
248         g_free(ipaddress->local);
249         ipaddress->local = g_strdup(source->local);
250
251         g_free(ipaddress->peer);
252         ipaddress->peer = g_strdup(source->peer);
253
254         g_free(ipaddress->broadcast);
255         ipaddress->broadcast = g_strdup(source->broadcast);
256
257         g_free(ipaddress->gateway);
258         ipaddress->gateway = g_strdup(source->gateway);
259 }
260
261 static void free_address_list(struct connman_ipdevice *ipdevice)
262 {
263         GSList *list;
264
265         for (list = ipdevice->address_list; list; list = list->next) {
266                 struct connman_ipaddress *ipaddress = list->data;
267
268                 connman_ipaddress_free(ipaddress);
269                 list->data = NULL;
270         }
271
272         g_slist_free(ipdevice->address_list);
273         ipdevice->address_list = NULL;
274 }
275
276 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
277                                 unsigned char prefixlen, const char *local)
278 {
279         GSList *list;
280
281         for (list = ipdevice->address_list; list; list = list->next) {
282                 struct connman_ipaddress *ipaddress = list->data;
283
284                 if (g_strcmp0(ipaddress->local, local) == 0 &&
285                                         ipaddress->prefixlen == prefixlen)
286                         return ipaddress;
287         }
288
289         return NULL;
290 }
291
292 const char *__connman_ipconfig_type2string(enum connman_ipconfig_type type)
293 {
294         switch (type) {
295         case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
296                 return "unknown";
297         case CONNMAN_IPCONFIG_TYPE_IPV4:
298                 return "IPv4";
299         case CONNMAN_IPCONFIG_TYPE_IPV6:
300                 return "IPv6";
301         }
302
303         return NULL;
304 }
305
306 static const char *type2str(unsigned short type)
307 {
308         switch (type) {
309         case ARPHRD_ETHER:
310                 return "ETHER";
311         case ARPHRD_LOOPBACK:
312                 return "LOOPBACK";
313         case ARPHRD_PPP:
314                 return "PPP";
315         case ARPHRD_NONE:
316                 return "NONE";
317         case ARPHRD_VOID:
318                 return "VOID";
319         }
320
321         return "";
322 }
323
324 static const char *scope2str(unsigned char scope)
325 {
326         switch (scope) {
327         case 0:
328                 return "UNIVERSE";
329         case 253:
330                 return "LINK";
331         }
332
333         return "";
334 }
335
336 static gboolean get_ipv6_state(gchar *ifname)
337 {
338         int disabled;
339         gchar *path;
340         FILE *f;
341         gboolean enabled = FALSE;
342
343         if (ifname == NULL)
344                 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
345         else
346                 path = g_strdup_printf(
347                         "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
348
349         if (path == NULL)
350                 return enabled;
351
352         f = fopen(path, "r");
353
354         g_free(path);
355
356         if (f != NULL) {
357                 if (fscanf(f, "%d", &disabled) > 0)
358                         enabled = !disabled;
359                 fclose(f);
360         }
361
362         return enabled;
363 }
364
365 static void set_ipv6_state(gchar *ifname, gboolean enable)
366 {
367         gchar *path;
368         FILE *f;
369
370         if (ifname == NULL)
371                 path = g_strdup("/proc/sys/net/ipv6/conf/all/disable_ipv6");
372         else
373                 path = g_strdup_printf(
374                         "/proc/sys/net/ipv6/conf/%s/disable_ipv6", ifname);
375
376         if (path == NULL)
377                 return;
378
379         f = fopen(path, "r+");
380
381         g_free(path);
382
383         if (f == NULL)
384                 return;
385
386         if (enable == FALSE)
387                 fprintf(f, "1");
388         else
389                 fprintf(f, "0");
390
391         fclose(f);
392 }
393
394 static int get_ipv6_privacy(gchar *ifname)
395 {
396         gchar *path;
397         FILE *f;
398         int value;
399
400         if (ifname == NULL)
401                 return 0;
402
403         path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
404                                                                 ifname);
405
406         if (path == NULL)
407                 return 0;
408
409         f = fopen(path, "r");
410
411         g_free(path);
412
413         if (f == NULL)
414                 return 0;
415
416         if (fscanf(f, "%d", &value) <= 0)
417                 value = 0;
418
419         fclose(f);
420
421         return value;
422 }
423
424 /* Enable the IPv6 privacy extension for stateless address autoconfiguration.
425  * The privacy extension is described in RFC 3041 and RFC 4941
426  */
427 static void set_ipv6_privacy(gchar *ifname, int value)
428 {
429         gchar *path;
430         FILE *f;
431
432         if (ifname == NULL)
433                 return;
434
435         path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/use_tempaddr",
436                                                                 ifname);
437
438         if (path == NULL)
439                 return;
440
441         if (value < 0)
442                 value = 0;
443
444         f = fopen(path, "r+");
445
446         g_free(path);
447
448         if (f == NULL)
449                 return;
450
451         fprintf(f, "%d", value);
452         fclose(f);
453 }
454
455 static int get_rp_filter()
456 {
457         FILE *f;
458         int value = -EINVAL, tmp;
459
460         f = fopen("/proc/sys/net/ipv4/conf/all/rp_filter", "r");
461
462         if (f != NULL) {
463                 if (fscanf(f, "%d", &tmp) == 1)
464                         value = tmp;
465                 fclose(f);
466         }
467
468         return value;
469 }
470
471 static void set_rp_filter(int value)
472 {
473         FILE *f;
474
475         f = fopen("/proc/sys/net/ipv4/conf/all/rp_filter", "r+");
476
477         if (f == NULL)
478                 return;
479
480         fprintf(f, "%d", value);
481
482         fclose(f);
483 }
484
485 int __connman_ipconfig_set_rp_filter()
486 {
487         int value;
488
489         value = get_rp_filter();
490
491         if (value < 0)
492                 return value;
493
494         set_rp_filter(2);
495
496         connman_info("rp_filter set to 2 (loose mode routing), "
497                         "old value was %d", value);
498
499         return value;
500 }
501
502 void __connman_ipconfig_unset_rp_filter(int old_value)
503 {
504         set_rp_filter(old_value);
505
506         connman_info("rp_filter restored to %d", old_value);
507 }
508
509 static void free_ipdevice(gpointer data)
510 {
511         struct connman_ipdevice *ipdevice = data;
512
513         connman_info("%s {remove} index %d", ipdevice->ifname,
514                                                         ipdevice->index);
515
516         if (ipdevice->config_ipv4 != NULL) {
517                 connman_ipconfig_unref(ipdevice->config_ipv4);
518                 ipdevice->config_ipv4 = NULL;
519         }
520
521         if (ipdevice->config_ipv6 != NULL) {
522                 connman_ipconfig_unref(ipdevice->config_ipv6);
523                 ipdevice->config_ipv6 = NULL;
524         }
525
526         free_address_list(ipdevice);
527         g_free(ipdevice->ipv4_gateway);
528         g_free(ipdevice->ipv6_gateway);
529         g_free(ipdevice->pac);
530
531         g_free(ipdevice->address);
532
533         set_ipv6_state(ipdevice->ifname, ipdevice->ipv6_enabled);
534         set_ipv6_privacy(ipdevice->ifname, ipdevice->ipv6_privacy);
535
536         g_free(ipdevice->ifname);
537         g_free(ipdevice);
538 }
539
540 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
541 {
542         DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
543                                         ipdevice->config_ipv6);
544 }
545
546 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
547 {
548         DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
549                                         ipdevice->config_ipv6);
550
551         if (ipdevice->config_ipv4)
552                 connman_inet_clear_address(ipdevice->index,
553                                         ipdevice->config_ipv4->address);
554
555         if (ipdevice->config_ipv6)
556                 connman_inet_clear_ipv6_address(ipdevice->index,
557                                 ipdevice->config_ipv6->address->local,
558                                 ipdevice->config_ipv6->address->prefixlen);
559 }
560
561 static void update_stats(struct connman_ipdevice *ipdevice,
562                                                 struct rtnl_link_stats *stats)
563 {
564         struct connman_service *service;
565
566         if (stats->rx_packets == 0 && stats->tx_packets == 0)
567                 return;
568
569         connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
570                                         stats->rx_packets, stats->rx_bytes);
571         connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
572                                         stats->tx_packets, stats->tx_bytes);
573
574         if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
575                 return;
576
577         if (ipdevice->config_ipv4)
578                 service = connman_ipconfig_get_data(ipdevice->config_ipv4);
579         else if (ipdevice->config_ipv6)
580                 service = connman_ipconfig_get_data(ipdevice->config_ipv6);
581         else
582                 return;
583
584         if (service == NULL)
585                 return;
586
587         ipdevice->rx_packets = stats->rx_packets;
588         ipdevice->tx_packets = stats->tx_packets;
589         ipdevice->rx_bytes = stats->rx_bytes;
590         ipdevice->tx_bytes = stats->tx_bytes;
591         ipdevice->rx_errors = stats->rx_errors;
592         ipdevice->tx_errors = stats->tx_errors;
593         ipdevice->rx_dropped = stats->rx_dropped;
594         ipdevice->tx_dropped = stats->tx_dropped;
595
596         __connman_service_notify(service,
597                                 ipdevice->rx_packets, ipdevice->tx_packets,
598                                 ipdevice->rx_bytes, ipdevice->tx_bytes,
599                                 ipdevice->rx_errors, ipdevice->tx_errors,
600                                 ipdevice->rx_dropped, ipdevice->tx_dropped);
601 }
602
603 void __connman_ipconfig_newlink(int index, unsigned short type,
604                                 unsigned int flags, const char *address,
605                                                         unsigned short mtu,
606                                                 struct rtnl_link_stats *stats)
607 {
608         struct connman_ipdevice *ipdevice;
609         GList *list;
610         GString *str;
611         gboolean up = FALSE, down = FALSE;
612         gboolean lower_up = FALSE, lower_down = FALSE;
613
614         DBG("index %d", index);
615
616         if (type == ARPHRD_LOOPBACK)
617                 return;
618
619         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
620         if (ipdevice != NULL)
621                 goto update;
622
623         ipdevice = g_try_new0(struct connman_ipdevice, 1);
624         if (ipdevice == NULL)
625                 return;
626
627         ipdevice->index = index;
628         ipdevice->ifname = connman_inet_ifname(index);
629         ipdevice->type = type;
630
631         ipdevice->ipv6_enabled = get_ipv6_state(ipdevice->ifname);
632         ipdevice->ipv6_privacy = get_ipv6_privacy(ipdevice->ifname);
633
634         ipdevice->address = g_strdup(address);
635
636         g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
637
638         connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
639                                                 index, type, type2str(type));
640
641 update:
642         ipdevice->mtu = mtu;
643
644         update_stats(ipdevice, stats);
645
646         if (flags == ipdevice->flags)
647                 return;
648
649         if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
650                 if (flags & IFF_UP)
651                         up = TRUE;
652                 else
653                         down = TRUE;
654         }
655
656         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
657                                 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
658                 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
659                                         (IFF_RUNNING | IFF_LOWER_UP))
660                         lower_up = TRUE;
661                 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
662                         lower_down = TRUE;
663         }
664
665         ipdevice->flags = flags;
666
667         str = g_string_new(NULL);
668         if (str == NULL)
669                 return;
670
671         if (flags & IFF_UP)
672                 g_string_append(str, "UP");
673         else
674                 g_string_append(str, "DOWN");
675
676         if (flags & IFF_RUNNING)
677                 g_string_append(str, ",RUNNING");
678
679         if (flags & IFF_LOWER_UP)
680                 g_string_append(str, ",LOWER_UP");
681
682         connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
683                                                         flags, str->str);
684
685         g_string_free(str, TRUE);
686
687         for (list = g_list_first(ipconfig_list); list;
688                                                 list = g_list_next(list)) {
689                 struct connman_ipconfig *ipconfig = list->data;
690
691                 if (index != ipconfig->index)
692                         continue;
693
694                 if (ipconfig->ops == NULL)
695                         continue;
696
697                 if (up == TRUE && ipconfig->ops->up)
698                         ipconfig->ops->up(ipconfig);
699                 if (lower_up == TRUE && ipconfig->ops->lower_up)
700                         ipconfig->ops->lower_up(ipconfig);
701
702                 if (lower_down == TRUE && ipconfig->ops->lower_down)
703                         ipconfig->ops->lower_down(ipconfig);
704                 if (down == TRUE && ipconfig->ops->down)
705                         ipconfig->ops->down(ipconfig);
706         }
707
708 #if defined TIZEN_EXT
709         if (g_strcmp0(ipdevice->address, address) != 0) {
710                 g_free(ipdevice->address);
711                 ipdevice->address = g_strdup(address);
712         }
713 #endif
714
715         if (lower_up)
716                 __connman_ipconfig_lower_up(ipdevice);
717         if (lower_down)
718                 __connman_ipconfig_lower_down(ipdevice);
719 }
720
721 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
722 {
723         struct connman_ipdevice *ipdevice;
724         GList *list;
725
726         DBG("index %d", index);
727
728         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
729         if (ipdevice == NULL)
730                 return;
731
732         update_stats(ipdevice, stats);
733
734         for (list = g_list_first(ipconfig_list); list;
735                                                 list = g_list_next(list)) {
736                 struct connman_ipconfig *ipconfig = list->data;
737
738                 if (index != ipconfig->index)
739                         continue;
740
741                 ipconfig->index = -1;
742
743                 if (ipconfig->ops == NULL)
744                         continue;
745
746                 if (ipconfig->ops->lower_down)
747                         ipconfig->ops->lower_down(ipconfig);
748                 if (ipconfig->ops->down)
749                         ipconfig->ops->down(ipconfig);
750         }
751
752         __connman_ipconfig_lower_down(ipdevice);
753
754         g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
755 }
756
757 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
758 {
759         const struct connman_ipaddress *addr1 = a;
760         const struct connman_ipaddress *addr2 = b;
761
762         if (addr1->prefixlen != addr2->prefixlen)
763                 return addr2->prefixlen - addr1->prefixlen;
764
765         return g_strcmp0(addr1->local, addr2->local);
766 }
767
768 void __connman_ipconfig_newaddr(int index, int family, const char *label,
769                                 unsigned char prefixlen, const char *address)
770 {
771         struct connman_ipdevice *ipdevice;
772         struct connman_ipaddress *ipaddress;
773         enum connman_ipconfig_type type;
774         GList *list;
775
776         DBG("index %d", index);
777
778         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
779         if (ipdevice == NULL)
780                 return;
781
782         ipaddress = connman_ipaddress_alloc(family);
783         if (ipaddress == NULL)
784                 return;
785
786         ipaddress->prefixlen = prefixlen;
787         ipaddress->local = g_strdup(address);
788
789         if (g_slist_find_custom(ipdevice->address_list, ipaddress,
790                                         check_duplicate_address)) {
791                 connman_ipaddress_free(ipaddress);
792                 return;
793         }
794
795         if (family == AF_INET)
796                 type = CONNMAN_IPCONFIG_TYPE_IPV4;
797         else if (family == AF_INET6)
798                 type = CONNMAN_IPCONFIG_TYPE_IPV6;
799         else
800                 return;
801
802         ipdevice->address_list = g_slist_append(ipdevice->address_list,
803                                                                 ipaddress);
804
805         connman_info("%s {add} address %s/%u label %s family %d",
806                 ipdevice->ifname, address, prefixlen, label, family);
807
808         if (ipdevice->config_ipv4 != NULL && family == AF_INET)
809                 connman_ipaddress_copy(ipdevice->config_ipv4->system,
810                                         ipaddress);
811
812         else if (ipdevice->config_ipv6 != NULL && family == AF_INET6)
813                 connman_ipaddress_copy(ipdevice->config_ipv6->system,
814                                         ipaddress);
815         else
816                 return;
817
818         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
819                 return;
820
821         for (list = g_list_first(ipconfig_list); list;
822                                                 list = g_list_next(list)) {
823                 struct connman_ipconfig *ipconfig = list->data;
824
825                 if (index != ipconfig->index)
826                         continue;
827
828                 if (type != ipconfig->type)
829                         continue;
830
831                 if (ipconfig->ops == NULL)
832                         continue;
833
834                 if (ipconfig->ops->ip_bound)
835                         ipconfig->ops->ip_bound(ipconfig);
836         }
837 }
838
839 void __connman_ipconfig_deladdr(int index, int family, const char *label,
840                                 unsigned char prefixlen, const char *address)
841 {
842         struct connman_ipdevice *ipdevice;
843         struct connman_ipaddress *ipaddress;
844         enum connman_ipconfig_type type;
845         GList *list;
846
847         DBG("index %d", index);
848
849         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
850         if (ipdevice == NULL)
851                 return;
852
853         ipaddress = find_ipaddress(ipdevice, prefixlen, address);
854         if (ipaddress == NULL)
855                 return;
856
857         if (family == AF_INET)
858                 type = CONNMAN_IPCONFIG_TYPE_IPV4;
859         else if (family == AF_INET6)
860                 type = CONNMAN_IPCONFIG_TYPE_IPV6;
861         else
862                 return;
863
864         ipdevice->address_list = g_slist_remove(ipdevice->address_list,
865                                                                 ipaddress);
866
867         connman_ipaddress_clear(ipaddress);
868         g_free(ipaddress);
869
870         connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
871                                                 address, prefixlen, label);
872
873         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
874                 return;
875
876         if (g_slist_length(ipdevice->address_list) > 0)
877                 return;
878
879         for (list = g_list_first(ipconfig_list); list;
880                                                 list = g_list_next(list)) {
881                 struct connman_ipconfig *ipconfig = list->data;
882
883                 if (index != ipconfig->index)
884                         continue;
885
886                 if (type != ipconfig->type)
887                         continue;
888
889                 if (ipconfig->ops == NULL)
890                         continue;
891
892                 if (ipconfig->ops->ip_release)
893                         ipconfig->ops->ip_release(ipconfig);
894         }
895 }
896
897 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
898                                         const char *dst, const char *gateway)
899 {
900         struct connman_ipdevice *ipdevice;
901
902         DBG("index %d", index);
903
904         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
905         if (ipdevice == NULL)
906                 return;
907
908         if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
909                                                 g_strcmp0(dst, "::") == 0)) {
910                 GSList *list;
911                 GList *config_list;
912                 enum connman_ipconfig_type type;
913
914                 if (family == AF_INET6) {
915                         type = CONNMAN_IPCONFIG_TYPE_IPV6;
916                         g_free(ipdevice->ipv6_gateway);
917                         ipdevice->ipv6_gateway = g_strdup(gateway);
918
919                         if (ipdevice->config_ipv6 != NULL &&
920                                 ipdevice->config_ipv6->system != NULL) {
921                                 g_free(ipdevice->config_ipv6->system->gateway);
922                                 ipdevice->config_ipv6->system->gateway =
923                                         g_strdup(gateway);
924                         }
925                 } else if (family == AF_INET) {
926                         type = CONNMAN_IPCONFIG_TYPE_IPV4;
927                         g_free(ipdevice->ipv4_gateway);
928                         ipdevice->ipv4_gateway = g_strdup(gateway);
929
930                         if (ipdevice->config_ipv4 != NULL &&
931                                 ipdevice->config_ipv4->system != NULL) {
932                                 g_free(ipdevice->config_ipv4->system->gateway);
933                                 ipdevice->config_ipv4->system->gateway =
934                                         g_strdup(gateway);
935                         }
936                 } else
937                         return;
938
939                 for (list = ipdevice->address_list; list; list = list->next) {
940                         struct connman_ipaddress *ipaddress = list->data;
941
942                         g_free(ipaddress->gateway);
943                         ipaddress->gateway = g_strdup(gateway);
944                 }
945
946                 for (config_list = g_list_first(ipconfig_list); config_list;
947                                         config_list = g_list_next(config_list)) {
948                         struct connman_ipconfig *ipconfig = config_list->data;
949
950                         if (index != ipconfig->index)
951                                 continue;
952
953                         if (type != ipconfig->type)
954                                 continue;
955
956                         if (ipconfig->ops == NULL)
957                                 continue;
958
959                         if (ipconfig->ops->ip_bound)
960                                 ipconfig->ops->ip_bound(ipconfig);
961                 }
962         }
963
964         connman_info("%s {add} route %s gw %s scope %u <%s>",
965                                         ipdevice->ifname, dst, gateway,
966                                                 scope, scope2str(scope));
967 }
968
969 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
970                                         const char *dst, const char *gateway)
971 {
972         struct connman_ipdevice *ipdevice;
973
974         DBG("index %d", index);
975
976         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
977         if (ipdevice == NULL)
978                 return;
979
980         if (scope == 0 && (g_strcmp0(dst, "0.0.0.0") == 0 ||
981                                                 g_strcmp0(dst, "::") == 0)) {
982                 GSList *list;
983                 GList *config_list;
984                 enum connman_ipconfig_type type;
985
986                 if (family == AF_INET6) {
987                         type = CONNMAN_IPCONFIG_TYPE_IPV6;
988                         g_free(ipdevice->ipv6_gateway);
989                         ipdevice->ipv6_gateway = NULL;
990
991                         if (ipdevice->config_ipv6 != NULL &&
992                                 ipdevice->config_ipv6->system != NULL) {
993                                 g_free(ipdevice->config_ipv6->system->gateway);
994                                 ipdevice->config_ipv6->system->gateway = NULL;
995                         }
996                 } else if (family == AF_INET) {
997                         type = CONNMAN_IPCONFIG_TYPE_IPV4;
998                         g_free(ipdevice->ipv4_gateway);
999                         ipdevice->ipv4_gateway = NULL;
1000
1001                         if (ipdevice->config_ipv4 != NULL &&
1002                                 ipdevice->config_ipv4->system != NULL) {
1003                                 g_free(ipdevice->config_ipv4->system->gateway);
1004                                 ipdevice->config_ipv4->system->gateway = NULL;
1005                         }
1006                 } else
1007                         return;
1008
1009                 for (list = ipdevice->address_list; list; list = list->next) {
1010                         struct connman_ipaddress *ipaddress = list->data;
1011
1012                         g_free(ipaddress->gateway);
1013                         ipaddress->gateway = NULL;
1014                 }
1015
1016                 for (config_list = g_list_first(ipconfig_list); config_list;
1017                                         config_list = g_list_next(config_list)) {
1018                         struct connman_ipconfig *ipconfig = config_list->data;
1019
1020                         if (index != ipconfig->index)
1021                                 continue;
1022
1023                         if (type != ipconfig->type)
1024                                 continue;
1025
1026                         if (ipconfig->ops == NULL)
1027                                 continue;
1028
1029                         if (ipconfig->ops->ip_release)
1030                                 ipconfig->ops->ip_release(ipconfig);
1031                 }
1032         }
1033
1034         connman_info("%s {del} route %s gw %s scope %u <%s>",
1035                                         ipdevice->ifname, dst, gateway,
1036                                                 scope, scope2str(scope));
1037 }
1038
1039 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
1040                                                         void *user_data)
1041 {
1042         GList *list, *keys;
1043
1044         keys = g_hash_table_get_keys(ipdevice_hash);
1045         if (keys == NULL)
1046                 return;
1047
1048         for (list = g_list_first(keys); list; list = g_list_next(list)) {
1049                 int index = GPOINTER_TO_INT(list->data);
1050
1051                 function(index, user_data);
1052         }
1053
1054         g_list_free(keys);
1055 }
1056
1057 enum connman_ipconfig_type __connman_ipconfig_get_config_type(
1058                                         struct connman_ipconfig *ipconfig)
1059 {
1060         return ipconfig ? ipconfig->type : CONNMAN_IPCONFIG_TYPE_UNKNOWN;
1061 }
1062
1063 unsigned short __connman_ipconfig_get_type_from_index(int index)
1064 {
1065         struct connman_ipdevice *ipdevice;
1066
1067         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1068         if (ipdevice == NULL)
1069                 return ARPHRD_VOID;
1070
1071         return ipdevice->type;
1072 }
1073
1074 unsigned int __connman_ipconfig_get_flags_from_index(int index)
1075 {
1076         struct connman_ipdevice *ipdevice;
1077
1078         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1079         if (ipdevice == NULL)
1080                 return 0;
1081
1082         return ipdevice->flags;
1083 }
1084
1085 const char *__connman_ipconfig_get_gateway_from_index(int index)
1086 {
1087         struct connman_ipdevice *ipdevice;
1088
1089         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
1090         if (ipdevice == NULL)
1091                 return NULL;
1092
1093         if (ipdevice->ipv4_gateway != NULL)
1094                 return ipdevice->ipv4_gateway;
1095
1096         if (ipdevice->config_ipv4 != NULL &&
1097                         ipdevice->config_ipv4->address != NULL)
1098                 return ipdevice->config_ipv4->address->gateway;
1099
1100         if (ipdevice->ipv6_gateway != NULL)
1101                 return ipdevice->ipv6_gateway;
1102
1103         if (ipdevice->config_ipv6 != NULL &&
1104                         ipdevice->config_ipv6->address != NULL)
1105                 return ipdevice->config_ipv6->address->gateway;
1106
1107         return NULL;
1108 }
1109
1110 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
1111 {
1112         ipconfig->index = index;
1113 }
1114
1115 const char *__connman_ipconfig_get_local(struct connman_ipconfig *ipconfig)
1116 {
1117         if (ipconfig->address == NULL)
1118                 return NULL;
1119
1120         return ipconfig->address->local;
1121 }
1122
1123 void __connman_ipconfig_set_local(struct connman_ipconfig *ipconfig, const char *address)
1124 {
1125         if (ipconfig->address == NULL)
1126                 return;
1127
1128         g_free(ipconfig->address->local);
1129         ipconfig->address->local = g_strdup(address);
1130 }
1131
1132 const char *__connman_ipconfig_get_peer(struct connman_ipconfig *ipconfig)
1133 {
1134         if (ipconfig->address == NULL)
1135                 return NULL;
1136
1137         return ipconfig->address->peer;
1138 }
1139
1140 void __connman_ipconfig_set_peer(struct connman_ipconfig *ipconfig, const char *address)
1141 {
1142         if (ipconfig->address == NULL)
1143                 return;
1144
1145         g_free(ipconfig->address->peer);
1146         ipconfig->address->peer = g_strdup(address);
1147 }
1148
1149 const char *__connman_ipconfig_get_broadcast(struct connman_ipconfig *ipconfig)
1150 {
1151         if (ipconfig->address == NULL)
1152                 return NULL;
1153
1154         return ipconfig->address->broadcast;
1155 }
1156
1157 void __connman_ipconfig_set_broadcast(struct connman_ipconfig *ipconfig, const char *broadcast)
1158 {
1159         if (ipconfig->address == NULL)
1160                 return;
1161
1162         g_free(ipconfig->address->broadcast);
1163         ipconfig->address->broadcast = g_strdup(broadcast);
1164 }
1165
1166 const char *__connman_ipconfig_get_gateway(struct connman_ipconfig *ipconfig)
1167 {
1168         if (ipconfig->address == NULL)
1169                 return NULL;
1170
1171         return ipconfig->address->gateway;
1172 }
1173
1174 void __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig, const char *gateway)
1175 {
1176         DBG("");
1177
1178         if (ipconfig->address == NULL)
1179                 return;
1180         g_free(ipconfig->address->gateway);
1181         ipconfig->address->gateway = g_strdup(gateway);
1182 }
1183
1184 #if defined TIZEN_EXT
1185 /*
1186  * Description: __connman_service_lookup_from_index cannot find correct service
1187  */
1188 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig, struct connman_service *service)
1189 #else
1190 int __connman_ipconfig_gateway_add(struct connman_ipconfig *ipconfig)
1191 #endif
1192 {
1193 #if !defined TIZEN_EXT
1194         struct connman_service *service;
1195 #endif
1196
1197         DBG("");
1198
1199         if (ipconfig->address == NULL)
1200                 return -EINVAL;
1201
1202 #if !defined TIZEN_EXT
1203         service = __connman_service_lookup_from_index(ipconfig->index);
1204 #endif
1205         if (service == NULL)
1206                 return -EINVAL;
1207
1208         __connman_connection_gateway_remove(service, ipconfig->type);
1209
1210         DBG("type %d gw %s peer %s", ipconfig->type,
1211                 ipconfig->address->gateway, ipconfig->address->peer);
1212
1213         if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6 ||
1214                                 ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1215                 return __connman_connection_gateway_add(service,
1216                                                 ipconfig->address->gateway,
1217                                                 ipconfig->type,
1218                                                 ipconfig->address->peer);
1219
1220         return 0;
1221 }
1222
1223 void __connman_ipconfig_gateway_remove(struct connman_ipconfig *ipconfig)
1224 {
1225         struct connman_service *service;
1226
1227         DBG("");
1228
1229         service = __connman_service_lookup_from_index(ipconfig->index);
1230         if (service != NULL)
1231                 __connman_connection_gateway_remove(service, ipconfig->type);
1232 }
1233
1234 unsigned char __connman_ipconfig_get_prefixlen(struct connman_ipconfig *ipconfig)
1235 {
1236         if (ipconfig->address == NULL)
1237                 return 0;
1238
1239         return ipconfig->address->prefixlen;
1240 }
1241
1242 void __connman_ipconfig_set_prefixlen(struct connman_ipconfig *ipconfig, unsigned char prefixlen)
1243 {
1244         if (ipconfig->address == NULL)
1245                 return;
1246
1247         ipconfig->address->prefixlen = prefixlen;
1248 }
1249
1250 static struct connman_ipconfig *create_ipv6config(int index)
1251 {
1252         struct connman_ipconfig *ipv6config;
1253
1254         DBG("index %d", index);
1255
1256         ipv6config = g_try_new0(struct connman_ipconfig, 1);
1257         if (ipv6config == NULL)
1258                 return NULL;
1259
1260         ipv6config->refcount = 1;
1261
1262         ipv6config->index = index;
1263         ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
1264         ipv6config->method = CONNMAN_IPCONFIG_METHOD_AUTO;
1265         ipv6config->ipv6_privacy_config = 0;
1266
1267         ipv6config->address = connman_ipaddress_alloc(AF_INET6);
1268         if (ipv6config->address == NULL) {
1269                 g_free(ipv6config);
1270                 return NULL;
1271         }
1272
1273         ipv6config->system = connman_ipaddress_alloc(AF_INET6);
1274
1275         DBG("ipconfig %p", ipv6config);
1276
1277         return ipv6config;
1278 }
1279
1280 /**
1281  * connman_ipconfig_create:
1282  *
1283  * Allocate a new ipconfig structure.
1284  *
1285  * Returns: a newly-allocated #connman_ipconfig structure
1286  */
1287 struct connman_ipconfig *connman_ipconfig_create(int index,
1288                                         enum connman_ipconfig_type type)
1289 {
1290         struct connman_ipconfig *ipconfig;
1291
1292         if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1293                 return create_ipv6config(index);
1294
1295         DBG("index %d", index);
1296
1297         ipconfig = g_try_new0(struct connman_ipconfig, 1);
1298         if (ipconfig == NULL)
1299                 return NULL;
1300
1301         ipconfig->refcount = 1;
1302
1303         ipconfig->index = index;
1304         ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
1305
1306         ipconfig->address = connman_ipaddress_alloc(AF_INET);
1307         if (ipconfig->address == NULL) {
1308                 g_free(ipconfig);
1309                 return NULL;
1310         }
1311
1312         ipconfig->system = connman_ipaddress_alloc(AF_INET);
1313
1314         DBG("ipconfig %p", ipconfig);
1315
1316         return ipconfig;
1317 }
1318
1319
1320 /**
1321  * connman_ipconfig_ref:
1322  * @ipconfig: ipconfig structure
1323  *
1324  * Increase reference counter of ipconfig
1325  */
1326 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
1327 {
1328         DBG("ipconfig %p refcount %d", ipconfig, ipconfig->refcount + 1);
1329
1330         __sync_fetch_and_add(&ipconfig->refcount, 1);
1331
1332         return ipconfig;
1333 }
1334
1335 /**
1336  * connman_ipconfig_unref:
1337  * @ipconfig: ipconfig structure
1338  *
1339  * Decrease reference counter of ipconfig
1340  */
1341 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
1342 {
1343         if (ipconfig == NULL)
1344                 return;
1345
1346         DBG("ipconfig %p refcount %d", ipconfig, ipconfig->refcount - 1);
1347
1348         if (__sync_fetch_and_sub(&ipconfig->refcount, 1) != 1)
1349                 return;
1350
1351         if (__connman_ipconfig_disable(ipconfig) < 0)
1352                 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1353
1354         connman_ipconfig_set_ops(ipconfig, NULL);
1355
1356         if (ipconfig->origin != NULL) {
1357                 connman_ipconfig_unref(ipconfig->origin);
1358                 ipconfig->origin = NULL;
1359         }
1360
1361         connman_ipaddress_free(ipconfig->system);
1362         connman_ipaddress_free(ipconfig->address);
1363         g_free(ipconfig->last_dhcp_address);
1364         g_free(ipconfig);
1365 }
1366
1367 /**
1368  * connman_ipconfig_get_data:
1369  * @ipconfig: ipconfig structure
1370  *
1371  * Get private data pointer
1372  */
1373 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1374 {
1375         if (ipconfig == NULL)
1376                 return NULL;
1377
1378         return ipconfig->ops_data;
1379 }
1380
1381 /**
1382  * connman_ipconfig_set_data:
1383  * @ipconfig: ipconfig structure
1384  * @data: data pointer
1385  *
1386  * Set private data pointer
1387  */
1388 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1389 {
1390         ipconfig->ops_data = data;
1391 }
1392
1393 /**
1394  * connman_ipconfig_get_index:
1395  * @ipconfig: ipconfig structure
1396  *
1397  * Get interface index
1398  */
1399 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1400 {
1401         if (ipconfig == NULL)
1402                 return -1;
1403
1404         if (ipconfig->origin != NULL)
1405                 return ipconfig->origin->index;
1406
1407         return ipconfig->index;
1408 }
1409
1410 /**
1411  * connman_ipconfig_get_ifname:
1412  * @ipconfig: ipconfig structure
1413  *
1414  * Get interface name
1415  */
1416 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1417 {
1418         struct connman_ipdevice *ipdevice;
1419
1420         if (ipconfig == NULL)
1421                 return NULL;
1422
1423         if (ipconfig->index < 0)
1424                 return NULL;
1425
1426         ipdevice = g_hash_table_lookup(ipdevice_hash,
1427                                         GINT_TO_POINTER(ipconfig->index));
1428         if (ipdevice == NULL)
1429                 return NULL;
1430
1431         return ipdevice->ifname;
1432 }
1433
1434 /**
1435  * connman_ipconfig_set_ops:
1436  * @ipconfig: ipconfig structure
1437  * @ops: operation callbacks
1438  *
1439  * Set the operation callbacks
1440  */
1441 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1442                                 const struct connman_ipconfig_ops *ops)
1443 {
1444         ipconfig->ops = ops;
1445 }
1446
1447 /**
1448  * connman_ipconfig_set_method:
1449  * @ipconfig: ipconfig structure
1450  * @method: configuration method
1451  *
1452  * Set the configuration method
1453  */
1454 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1455                                         enum connman_ipconfig_method method)
1456 {
1457         ipconfig->method = method;
1458
1459         return 0;
1460 }
1461
1462 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1463 {
1464         if (ipconfig == NULL)
1465                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1466
1467         return ipconfig->method;
1468 }
1469
1470 int __connman_ipconfig_address_add(struct connman_ipconfig *ipconfig)
1471 {
1472         DBG("");
1473
1474         switch (ipconfig->method) {
1475         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1476         case CONNMAN_IPCONFIG_METHOD_OFF:
1477         case CONNMAN_IPCONFIG_METHOD_AUTO:
1478                 break;
1479         case CONNMAN_IPCONFIG_METHOD_FIXED:
1480         case CONNMAN_IPCONFIG_METHOD_DHCP:
1481         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1482                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1483                         return connman_inet_set_address(ipconfig->index,
1484                                                         ipconfig->address);
1485                 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1486                         return connman_inet_set_ipv6_address(
1487                                         ipconfig->index, ipconfig->address);
1488         }
1489
1490         return 0;
1491 }
1492
1493 int __connman_ipconfig_address_remove(struct connman_ipconfig *ipconfig)
1494 {
1495         int err;
1496
1497         DBG("");
1498
1499         if (ipconfig == NULL)
1500                 return 0;
1501
1502         DBG("method %d", ipconfig->method);
1503
1504         switch (ipconfig->method) {
1505         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1506         case CONNMAN_IPCONFIG_METHOD_OFF:
1507         case CONNMAN_IPCONFIG_METHOD_AUTO:
1508                 break;
1509         case CONNMAN_IPCONFIG_METHOD_FIXED:
1510         case CONNMAN_IPCONFIG_METHOD_DHCP:
1511         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1512                 err = __connman_ipconfig_address_unset(ipconfig);
1513                 connman_ipaddress_clear(ipconfig->address);
1514
1515                 return err;
1516         }
1517
1518         return 0;
1519 }
1520
1521 int __connman_ipconfig_address_unset(struct connman_ipconfig *ipconfig)
1522 {
1523         int err;
1524
1525         DBG("");
1526
1527         if (ipconfig == NULL)
1528                 return 0;
1529
1530         DBG("method %d", ipconfig->method);
1531
1532         switch (ipconfig->method) {
1533         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1534         case CONNMAN_IPCONFIG_METHOD_OFF:
1535         case CONNMAN_IPCONFIG_METHOD_AUTO:
1536                 break;
1537         case CONNMAN_IPCONFIG_METHOD_FIXED:
1538         case CONNMAN_IPCONFIG_METHOD_DHCP:
1539         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1540                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1541                         err = connman_inet_clear_address(ipconfig->index,
1542                                                         ipconfig->address);
1543                 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1544                         err = connman_inet_clear_ipv6_address(
1545                                                 ipconfig->index,
1546                                                 ipconfig->address->local,
1547                                                 ipconfig->address->prefixlen);
1548                 else
1549                         err = -EINVAL;
1550
1551                 return err;
1552         }
1553
1554         return 0;
1555 }
1556
1557 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1558                                                         const char *url)
1559 {
1560         struct connman_ipdevice *ipdevice;
1561
1562         DBG("ipconfig %p", ipconfig);
1563
1564         if (ipconfig == NULL || ipconfig->index < 0)
1565                 return -ENODEV;
1566
1567         ipdevice = g_hash_table_lookup(ipdevice_hash,
1568                                         GINT_TO_POINTER(ipconfig->index));
1569         if (ipdevice == NULL)
1570                 return -ENXIO;
1571
1572         g_free(ipdevice->pac);
1573         ipdevice->pac = g_strdup(url);
1574
1575         return 0;
1576 }
1577
1578 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1579 {
1580         struct connman_ipdevice *ipdevice;
1581
1582         DBG("ipconfig %p", ipconfig);
1583
1584         if (ipconfig == NULL || ipconfig->index < 0)
1585                 return NULL;
1586
1587         ipdevice = g_hash_table_lookup(ipdevice_hash,
1588                                         GINT_TO_POINTER(ipconfig->index));
1589         if (ipdevice == NULL)
1590                 return NULL;
1591
1592         return ipdevice->pac;
1593 }
1594
1595 void __connman_ipconfig_set_dhcp_address(struct connman_ipconfig *ipconfig,
1596                                         const char *address)
1597 {
1598         if (ipconfig == NULL)
1599                 return;
1600
1601         g_free(ipconfig->last_dhcp_address);
1602         ipconfig->last_dhcp_address = g_strdup(address);
1603 }
1604
1605 char *__connman_ipconfig_get_dhcp_address(struct connman_ipconfig *ipconfig)
1606 {
1607         if (ipconfig == NULL)
1608                 return NULL;
1609
1610         return ipconfig->last_dhcp_address;
1611 }
1612
1613 static void disable_ipv6(struct connman_ipconfig *ipconfig)
1614 {
1615         struct connman_ipdevice *ipdevice;
1616
1617         DBG("");
1618
1619         ipdevice = g_hash_table_lookup(ipdevice_hash,
1620                                         GINT_TO_POINTER(ipconfig->index));
1621         if (ipdevice == NULL)
1622                 return;
1623
1624         set_ipv6_state(ipdevice->ifname, FALSE);
1625 }
1626
1627 static void enable_ipv6(struct connman_ipconfig *ipconfig)
1628 {
1629         struct connman_ipdevice *ipdevice;
1630
1631         DBG("");
1632
1633         ipdevice = g_hash_table_lookup(ipdevice_hash,
1634                                         GINT_TO_POINTER(ipconfig->index));
1635         if (ipdevice == NULL)
1636                 return;
1637
1638         if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO)
1639                 set_ipv6_privacy(ipdevice->ifname,
1640                                 ipconfig->ipv6_privacy_config);
1641
1642         set_ipv6_state(ipdevice->ifname, TRUE);
1643 }
1644
1645 void __connman_ipconfig_enable_ipv6(struct connman_ipconfig *ipconfig)
1646 {
1647         if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1648                 return;
1649
1650         enable_ipv6(ipconfig);
1651 }
1652
1653 void __connman_ipconfig_disable_ipv6(struct connman_ipconfig *ipconfig)
1654 {
1655         if (ipconfig == NULL || ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1656                 return;
1657
1658         disable_ipv6(ipconfig);
1659 }
1660
1661 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1662 {
1663         struct connman_ipdevice *ipdevice;
1664         gboolean up = FALSE, down = FALSE;
1665         gboolean lower_up = FALSE, lower_down = FALSE;
1666         enum connman_ipconfig_type type;
1667
1668         DBG("ipconfig %p", ipconfig);
1669
1670         if (ipconfig == NULL || ipconfig->index < 0)
1671                 return -ENODEV;
1672
1673         ipdevice = g_hash_table_lookup(ipdevice_hash,
1674                                         GINT_TO_POINTER(ipconfig->index));
1675         if (ipdevice == NULL)
1676                 return -ENXIO;
1677
1678         if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1679                 if (ipdevice->config_ipv4 == ipconfig)
1680                         return -EALREADY;
1681                 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1682         } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1683                 if (ipdevice->config_ipv6 == ipconfig)
1684                         return -EALREADY;
1685                 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1686                 enable_ipv6(ipconfig);
1687         } else
1688                 return -EINVAL;
1689
1690         if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1691                                         ipdevice->config_ipv4 != NULL) {
1692                 ipconfig_list = g_list_remove(ipconfig_list,
1693                                                         ipdevice->config_ipv4);
1694
1695                 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1696
1697                 connman_ipconfig_unref(ipdevice->config_ipv4);
1698         }
1699
1700         if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1701                                         ipdevice->config_ipv6 != NULL) {
1702                 ipconfig_list = g_list_remove(ipconfig_list,
1703                                                         ipdevice->config_ipv6);
1704
1705                 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1706
1707                 connman_ipconfig_unref(ipdevice->config_ipv6);
1708         }
1709
1710         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1711                 ipdevice->config_ipv4 = connman_ipconfig_ref(ipconfig);
1712         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1713                 ipdevice->config_ipv6 = connman_ipconfig_ref(ipconfig);
1714
1715         ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1716
1717         if (ipdevice->flags & IFF_UP)
1718                 up = TRUE;
1719         else
1720                 down = TRUE;
1721
1722         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1723                         (IFF_RUNNING | IFF_LOWER_UP))
1724                 lower_up = TRUE;
1725         else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1726                 lower_down = TRUE;
1727
1728         if (up == TRUE && ipconfig->ops->up)
1729                 ipconfig->ops->up(ipconfig);
1730         if (lower_up == TRUE && ipconfig->ops->lower_up)
1731                 ipconfig->ops->lower_up(ipconfig);
1732
1733         if (lower_down == TRUE && ipconfig->ops->lower_down)
1734                 ipconfig->ops->lower_down(ipconfig);
1735         if (down == TRUE && ipconfig->ops->down)
1736                 ipconfig->ops->down(ipconfig);
1737
1738         return 0;
1739 }
1740
1741 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1742 {
1743         struct connman_ipdevice *ipdevice;
1744
1745         DBG("ipconfig %p", ipconfig);
1746
1747         if (ipconfig == NULL || ipconfig->index < 0)
1748                 return -ENODEV;
1749
1750         ipdevice = g_hash_table_lookup(ipdevice_hash,
1751                                         GINT_TO_POINTER(ipconfig->index));
1752         if (ipdevice == NULL)
1753                 return -ENXIO;
1754
1755         if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1756                 return -EINVAL;
1757
1758         if (ipdevice->config_ipv4 == ipconfig) {
1759                 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1760
1761                 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1762                 connman_ipconfig_unref(ipdevice->config_ipv4);
1763                 ipdevice->config_ipv4 = NULL;
1764                 return 0;
1765         }
1766
1767         if (ipdevice->config_ipv6 == ipconfig) {
1768                 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1769
1770                 if (ipdevice->config_ipv6->method ==
1771                                                 CONNMAN_IPCONFIG_METHOD_AUTO)
1772                         disable_ipv6(ipdevice->config_ipv6);
1773
1774                 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1775                 connman_ipconfig_unref(ipdevice->config_ipv6);
1776                 ipdevice->config_ipv6 = NULL;
1777                 return 0;
1778         }
1779
1780         return -EINVAL;
1781 }
1782
1783 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1784 {
1785         switch (method) {
1786         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1787                 break;
1788         case CONNMAN_IPCONFIG_METHOD_OFF:
1789                 return "off";
1790         case CONNMAN_IPCONFIG_METHOD_FIXED:
1791                 return "fixed";
1792         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1793                 return "manual";
1794         case CONNMAN_IPCONFIG_METHOD_DHCP:
1795                 return "dhcp";
1796         case CONNMAN_IPCONFIG_METHOD_AUTO:
1797                 return "auto";
1798         }
1799
1800         return NULL;
1801 }
1802
1803 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1804 {
1805         if (g_strcmp0(method, "off") == 0)
1806                 return CONNMAN_IPCONFIG_METHOD_OFF;
1807         else if (g_strcmp0(method, "fixed") == 0)
1808                 return CONNMAN_IPCONFIG_METHOD_FIXED;
1809         else if (g_strcmp0(method, "manual") == 0)
1810                 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1811         else if (g_strcmp0(method, "dhcp") == 0)
1812                 return CONNMAN_IPCONFIG_METHOD_DHCP;
1813         else if (g_strcmp0(method, "auto") == 0)
1814                 return CONNMAN_IPCONFIG_METHOD_AUTO;
1815         else
1816                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1817 }
1818
1819 static const char *privacy2string(int privacy)
1820 {
1821         if (privacy <= 0)
1822                 return "disabled";
1823         else if (privacy == 1)
1824                 return "enabled";
1825         else if (privacy > 1)
1826                 return "prefered";
1827
1828         return "disabled";
1829 }
1830
1831 static int string2privacy(const char *privacy)
1832 {
1833         if (g_strcmp0(privacy, "disabled") == 0)
1834                 return 0;
1835         else if (g_strcmp0(privacy, "enabled") == 0)
1836                 return 1;
1837         else if (g_strcmp0(privacy, "prefered") == 0)
1838                 return 2;
1839         else
1840                 return 0;
1841 }
1842
1843 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1844                                                         DBusMessageIter *iter)
1845 {
1846         const char *str;
1847
1848         DBG("");
1849
1850         if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1851                 return;
1852
1853         str = __connman_ipconfig_method2string(ipconfig->method);
1854         if (str == NULL)
1855                 return;
1856
1857         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1858
1859         if (ipconfig->system == NULL)
1860                 return;
1861
1862         if (ipconfig->system->local != NULL) {
1863                 in_addr_t addr;
1864                 struct in_addr netmask;
1865                 char *mask;
1866
1867                 connman_dbus_dict_append_basic(iter, "Address",
1868                                 DBUS_TYPE_STRING, &ipconfig->system->local);
1869
1870                 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1871                 netmask.s_addr = htonl(addr);
1872                 mask = inet_ntoa(netmask);
1873                 connman_dbus_dict_append_basic(iter, "Netmask",
1874                                                 DBUS_TYPE_STRING, &mask);
1875         }
1876
1877         if (ipconfig->system->gateway != NULL)
1878                 connman_dbus_dict_append_basic(iter, "Gateway",
1879                                 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1880 }
1881
1882 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1883                                         DBusMessageIter *iter,
1884                                         struct connman_ipconfig *ipconfig_ipv4)
1885 {
1886         const char *str, *privacy;
1887
1888         DBG("");
1889
1890         if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1891                 return;
1892
1893         str = __connman_ipconfig_method2string(ipconfig->method);
1894         if (str == NULL)
1895                 return;
1896
1897         if (ipconfig_ipv4 != NULL &&
1898                         ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO) {
1899                 if (__connman_6to4_check(ipconfig_ipv4) == 1)
1900                         str = "6to4";
1901         }
1902
1903         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1904
1905         if (ipconfig->system == NULL)
1906                 return;
1907
1908         if (ipconfig->system->local != NULL) {
1909                 connman_dbus_dict_append_basic(iter, "Address",
1910                                 DBUS_TYPE_STRING, &ipconfig->system->local);
1911                 connman_dbus_dict_append_basic(iter, "PrefixLength",
1912                                                 DBUS_TYPE_BYTE,
1913                                                 &ipconfig->system->prefixlen);
1914         }
1915
1916         if (ipconfig->system->gateway != NULL)
1917                 connman_dbus_dict_append_basic(iter, "Gateway",
1918                                 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1919
1920         privacy = privacy2string(ipconfig->ipv6_privacy_config);
1921         connman_dbus_dict_append_basic(iter, "Privacy",
1922                                 DBUS_TYPE_STRING, &privacy);
1923 }
1924
1925 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1926                                                         DBusMessageIter *iter)
1927 {
1928         const char *str, *privacy;
1929
1930         DBG("");
1931
1932         str = __connman_ipconfig_method2string(ipconfig->method);
1933         if (str == NULL)
1934                 return;
1935
1936         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1937
1938         switch (ipconfig->method) {
1939         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1940         case CONNMAN_IPCONFIG_METHOD_OFF:
1941         case CONNMAN_IPCONFIG_METHOD_DHCP:
1942                 return;
1943         case CONNMAN_IPCONFIG_METHOD_FIXED:
1944         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1945         case CONNMAN_IPCONFIG_METHOD_AUTO:
1946                 break;
1947         }
1948
1949         if (ipconfig->address == NULL)
1950                 return;
1951
1952         if (ipconfig->address->local != NULL) {
1953                 connman_dbus_dict_append_basic(iter, "Address",
1954                                 DBUS_TYPE_STRING, &ipconfig->address->local);
1955                 connman_dbus_dict_append_basic(iter, "PrefixLength",
1956                                                 DBUS_TYPE_BYTE,
1957                                                 &ipconfig->address->prefixlen);
1958         }
1959
1960         if (ipconfig->address->gateway != NULL)
1961                 connman_dbus_dict_append_basic(iter, "Gateway",
1962                                 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1963
1964         privacy = privacy2string(ipconfig->ipv6_privacy_config);
1965         connman_dbus_dict_append_basic(iter, "Privacy",
1966                                 DBUS_TYPE_STRING, &privacy);
1967 }
1968
1969 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1970                                                         DBusMessageIter *iter)
1971 {
1972         const char *str;
1973
1974         DBG("");
1975
1976         str = __connman_ipconfig_method2string(ipconfig->method);
1977         if (str == NULL)
1978                 return;
1979
1980         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1981
1982         switch (ipconfig->method) {
1983         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1984         case CONNMAN_IPCONFIG_METHOD_OFF:
1985         case CONNMAN_IPCONFIG_METHOD_DHCP:
1986         case CONNMAN_IPCONFIG_METHOD_AUTO:
1987                 return;
1988         case CONNMAN_IPCONFIG_METHOD_FIXED:
1989         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1990                 break;
1991         }
1992
1993         if (ipconfig->address == NULL)
1994                 return;
1995
1996         if (ipconfig->address->local != NULL) {
1997                 in_addr_t addr;
1998                 struct in_addr netmask;
1999                 char *mask;
2000
2001                 connman_dbus_dict_append_basic(iter, "Address",
2002                                 DBUS_TYPE_STRING, &ipconfig->address->local);
2003
2004                 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
2005                 netmask.s_addr = htonl(addr);
2006                 mask = inet_ntoa(netmask);
2007                 connman_dbus_dict_append_basic(iter, "Netmask",
2008                                                 DBUS_TYPE_STRING, &mask);
2009         }
2010
2011         if (ipconfig->address->gateway != NULL)
2012                 connman_dbus_dict_append_basic(iter, "Gateway",
2013                                 DBUS_TYPE_STRING, &ipconfig->address->gateway);
2014 }
2015
2016 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
2017                                                         DBusMessageIter *array)
2018 {
2019         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
2020         const char *address = NULL, *netmask = NULL, *gateway = NULL,
2021                 *prefix_length_string = NULL, *privacy_string = NULL;
2022         int prefix_length = 0, privacy = 0;
2023         DBusMessageIter dict;
2024
2025         DBG("ipconfig %p", ipconfig);
2026
2027         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
2028                 return -EINVAL;
2029
2030         dbus_message_iter_recurse(array, &dict);
2031
2032         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
2033                 DBusMessageIter entry;
2034                 const char *key;
2035                 int type;
2036
2037                 dbus_message_iter_recurse(&dict, &entry);
2038
2039                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
2040                         return -EINVAL;
2041
2042                 dbus_message_iter_get_basic(&entry, &key);
2043                 dbus_message_iter_next(&entry);
2044
2045                 type = dbus_message_iter_get_arg_type(&entry);
2046
2047                 if (g_str_equal(key, "Method") == TRUE) {
2048                         const char *str;
2049
2050                         if (type != DBUS_TYPE_STRING)
2051                                 return -EINVAL;
2052
2053                         dbus_message_iter_get_basic(&entry, &str);
2054                         method = __connman_ipconfig_string2method(str);
2055                 } else if (g_str_equal(key, "Address") == TRUE) {
2056                         if (type != DBUS_TYPE_STRING)
2057                                 return -EINVAL;
2058
2059                         dbus_message_iter_get_basic(&entry, &address);
2060                 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
2061                         if (type != DBUS_TYPE_STRING)
2062                                 return -EINVAL;
2063
2064                         dbus_message_iter_get_basic(&entry,
2065                                                         &prefix_length_string);
2066
2067                         prefix_length = atoi(prefix_length_string);
2068                         if (prefix_length < 0 || prefix_length > 128)
2069                                 return -EINVAL;
2070
2071                 } else if (g_str_equal(key, "Netmask") == TRUE) {
2072                         if (type != DBUS_TYPE_STRING)
2073                                 return -EINVAL;
2074
2075                         dbus_message_iter_get_basic(&entry, &netmask);
2076                 } else if (g_str_equal(key, "Gateway") == TRUE) {
2077                         if (type != DBUS_TYPE_STRING)
2078                                 return -EINVAL;
2079
2080                         dbus_message_iter_get_basic(&entry, &gateway);
2081                 } else if (g_str_equal(key, "Privacy") == TRUE) {
2082                         if (type != DBUS_TYPE_STRING)
2083                                 return -EINVAL;
2084
2085                         dbus_message_iter_get_basic(&entry, &privacy_string);
2086                         privacy = string2privacy(privacy_string);
2087                 }
2088                 dbus_message_iter_next(&dict);
2089         }
2090
2091         DBG("method %d address %s netmask %s gateway %s prefix_length %d "
2092                 "privacy %s",
2093                 method, address, netmask, gateway, prefix_length,
2094                 privacy_string);
2095
2096         switch (method) {
2097         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2098         case CONNMAN_IPCONFIG_METHOD_FIXED:
2099                 return -EINVAL;
2100
2101         case CONNMAN_IPCONFIG_METHOD_OFF:
2102                 ipconfig->method = method;
2103                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2104                         disable_ipv6(ipconfig);
2105                 break;
2106
2107         case CONNMAN_IPCONFIG_METHOD_AUTO:
2108                 if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
2109                         return -EINVAL;
2110
2111                 ipconfig->method = method;
2112                 if (privacy_string != NULL)
2113                         ipconfig->ipv6_privacy_config = privacy;
2114                 enable_ipv6(ipconfig);
2115                 break;
2116
2117         case CONNMAN_IPCONFIG_METHOD_MANUAL:
2118                 if (address == NULL)
2119                         return -EINVAL;
2120
2121                 ipconfig->method = method;
2122
2123                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2124                         connman_ipaddress_set_ipv4(ipconfig->address,
2125                                                 address, netmask, gateway);
2126                 else
2127                         return connman_ipaddress_set_ipv6(
2128                                         ipconfig->address, address,
2129                                                 prefix_length, gateway);
2130                 break;
2131
2132         case CONNMAN_IPCONFIG_METHOD_DHCP:
2133                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
2134                         return -EOPNOTSUPP;
2135
2136                 ipconfig->method = method;
2137                 break;
2138         }
2139
2140         return 0;
2141 }
2142
2143 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
2144                                                         DBusMessageIter *iter)
2145 {
2146         struct connman_ipdevice *ipdevice;
2147         const char *method = "auto";
2148
2149         connman_dbus_dict_append_basic(iter, "Method",
2150                                                 DBUS_TYPE_STRING, &method);
2151
2152         ipdevice = g_hash_table_lookup(ipdevice_hash,
2153                                         GINT_TO_POINTER(ipconfig->index));
2154         if (ipdevice == NULL)
2155                 return;
2156
2157         if (ipdevice->ifname != NULL)
2158                 connman_dbus_dict_append_basic(iter, "Interface",
2159                                         DBUS_TYPE_STRING, &ipdevice->ifname);
2160
2161         if (ipdevice->address != NULL)
2162                 connman_dbus_dict_append_basic(iter, "Address",
2163                                         DBUS_TYPE_STRING, &ipdevice->address);
2164
2165         if (ipdevice->mtu > 0)
2166                 connman_dbus_dict_append_basic(iter, "MTU",
2167                                         DBUS_TYPE_UINT16, &ipdevice->mtu);
2168 }
2169
2170 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
2171                 GKeyFile *keyfile, const char *identifier, const char *prefix)
2172 {
2173         char *method;
2174         char *key;
2175         char *str;
2176
2177         DBG("ipconfig %p identifier %s", ipconfig, identifier);
2178
2179         key = g_strdup_printf("%smethod", prefix);
2180         method = g_key_file_get_string(keyfile, identifier, key, NULL);
2181         if (method == NULL) {
2182                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
2183                         ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
2184                 else
2185                         ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2186         } else
2187                 ipconfig->method = __connman_ipconfig_string2method(method);
2188
2189         if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_UNKNOWN)
2190                 ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
2191
2192         if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2193                 if (ipconfig->method == CONNMAN_IPCONFIG_METHOD_AUTO ||
2194                         ipconfig->method == CONNMAN_IPCONFIG_METHOD_MANUAL) {
2195                         char *privacy;
2196                         char *pprefix = g_strdup_printf("%sprivacy", prefix);
2197                         privacy = g_key_file_get_string(keyfile, identifier,
2198                                                         pprefix, NULL);
2199                         ipconfig->ipv6_privacy_config = string2privacy(privacy);
2200                         g_free(pprefix);
2201                         g_free(privacy);
2202
2203                         __connman_ipconfig_enable(ipconfig);
2204                         enable_ipv6(ipconfig);
2205                 }
2206         }
2207
2208         g_free(method);
2209         g_free(key);
2210
2211         key = g_strdup_printf("%snetmask_prefixlen", prefix);
2212         ipconfig->address->prefixlen = g_key_file_get_integer(
2213                                 keyfile, identifier, key, NULL);
2214         g_free(key);
2215
2216         key = g_strdup_printf("%slocal_address", prefix);
2217         ipconfig->address->local = g_key_file_get_string(
2218                         keyfile, identifier, key, NULL);
2219         g_free(key);
2220
2221         key = g_strdup_printf("%speer_address", prefix);
2222         ipconfig->address->peer = g_key_file_get_string(
2223                                 keyfile, identifier, key, NULL);
2224         g_free(key);
2225
2226         key = g_strdup_printf("%sbroadcast_address", prefix);
2227         ipconfig->address->broadcast = g_key_file_get_string(
2228                                 keyfile, identifier, key, NULL);
2229         g_free(key);
2230
2231         key = g_strdup_printf("%sgateway", prefix);
2232         ipconfig->address->gateway = g_key_file_get_string(
2233                                 keyfile, identifier, key, NULL);
2234         g_free(key);
2235
2236         key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2237         str = g_key_file_get_string(keyfile, identifier, key, NULL);
2238         if (str != NULL) {
2239                 g_free(ipconfig->last_dhcp_address);
2240                 ipconfig->last_dhcp_address = str;
2241         }
2242         g_free(key);
2243
2244         return 0;
2245 }
2246
2247 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
2248                 GKeyFile *keyfile, const char *identifier, const char *prefix)
2249 {
2250         const char *method;
2251         char *key;
2252
2253         DBG("ipconfig %p identifier %s", ipconfig, identifier);
2254
2255         method = __connman_ipconfig_method2string(ipconfig->method);
2256
2257         key = g_strdup_printf("%smethod", prefix);
2258         g_key_file_set_string(keyfile, identifier, key, method);
2259         g_free(key);
2260
2261         if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
2262                 const char *privacy;
2263                 privacy = privacy2string(ipconfig->ipv6_privacy_config);
2264                 key = g_strdup_printf("%sprivacy", prefix);
2265                 g_key_file_set_string(keyfile, identifier, key, privacy);
2266                 g_free(key);
2267         }
2268
2269         switch (ipconfig->method) {
2270         case CONNMAN_IPCONFIG_METHOD_FIXED:
2271         case CONNMAN_IPCONFIG_METHOD_MANUAL:
2272                 break;
2273         case CONNMAN_IPCONFIG_METHOD_DHCP:
2274                 key = g_strdup_printf("%sDHCP.LastAddress", prefix);
2275                 if (ipconfig->last_dhcp_address != NULL &&
2276                                 strlen(ipconfig->last_dhcp_address) > 0)
2277                         g_key_file_set_string(keyfile, identifier, key,
2278                                         ipconfig->last_dhcp_address);
2279                 else
2280                         g_key_file_remove_key(keyfile, identifier, key, NULL);
2281                 g_free(key);
2282                 /* fall through */
2283         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
2284         case CONNMAN_IPCONFIG_METHOD_OFF:
2285         case CONNMAN_IPCONFIG_METHOD_AUTO:
2286                 return 0;
2287         }
2288
2289         key = g_strdup_printf("%snetmask_prefixlen", prefix);
2290         g_key_file_set_integer(keyfile, identifier,
2291                         key, ipconfig->address->prefixlen);
2292         g_free(key);
2293
2294         key = g_strdup_printf("%slocal_address", prefix);
2295         if (ipconfig->address->local != NULL)
2296                 g_key_file_set_string(keyfile, identifier,
2297                                 key, ipconfig->address->local);
2298         g_free(key);
2299
2300         key = g_strdup_printf("%speer_address", prefix);
2301         if (ipconfig->address->peer != NULL)
2302                 g_key_file_set_string(keyfile, identifier,
2303                                 key, ipconfig->address->peer);
2304         g_free(key);
2305
2306         key = g_strdup_printf("%sbroadcast_address", prefix);
2307         if (ipconfig->address->broadcast != NULL)
2308                 g_key_file_set_string(keyfile, identifier,
2309                         key, ipconfig->address->broadcast);
2310         g_free(key);
2311
2312         key = g_strdup_printf("%sgateway", prefix);
2313         if (ipconfig->address->gateway != NULL)
2314                 g_key_file_set_string(keyfile, identifier,
2315                         key, ipconfig->address->gateway);
2316         g_free(key);
2317
2318         return 0;
2319 }
2320
2321 int __connman_ipconfig_init(void)
2322 {
2323         DBG("");
2324
2325         ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
2326                                                         NULL, free_ipdevice);
2327
2328         return 0;
2329 }
2330
2331 void __connman_ipconfig_cleanup(void)
2332 {
2333         DBG("");
2334
2335         g_hash_table_destroy(ipdevice_hash);
2336         ipdevice_hash = NULL;
2337 }