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