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