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