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