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