ipconfig: Fix IPv6 config reference count initialization
[platform/upstream/connman.git] / src / ipconfig.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <net/if.h>
27 #include <net/if_arp.h>
28 #include <linux/if_link.h>
29 #include <string.h>
30 #include <stdlib.h>
31
32 #ifndef IFF_LOWER_UP
33 #define IFF_LOWER_UP    0x10000
34 #endif
35
36 #include <gdbus.h>
37
38 #include "connman.h"
39
40 struct connman_ipconfig {
41         gint refcount;
42         int index;
43         enum connman_ipconfig_type type;
44
45         struct connman_ipconfig *origin;
46
47         const struct connman_ipconfig_ops *ops;
48         void *ops_data;
49
50         enum connman_ipconfig_method method;
51         struct connman_ipaddress *address;
52         struct connman_ipaddress *system;
53 };
54
55 struct connman_ipdevice {
56         int index;
57         char *ifname;
58         unsigned short type;
59         unsigned int flags;
60         char *address;
61         uint16_t mtu;
62         uint32_t rx_packets;
63         uint32_t tx_packets;
64         uint32_t rx_bytes;
65         uint32_t tx_bytes;
66         uint32_t rx_errors;
67         uint32_t tx_errors;
68         uint32_t rx_dropped;
69         uint32_t tx_dropped;
70
71         GSList *address_list;
72         char *ipv4_gateway;
73         char *ipv6_gateway;
74
75         char *pac;
76
77         struct connman_ipconfig *config_ipv4;
78         struct connman_ipconfig *config_ipv6;
79 };
80
81 static GHashTable *ipdevice_hash = NULL;
82 static GList *ipconfig_list = NULL;
83
84 struct connman_ipaddress *connman_ipaddress_alloc(int family)
85 {
86         struct connman_ipaddress *ipaddress;
87
88         ipaddress = g_try_new0(struct connman_ipaddress, 1);
89         if (ipaddress == NULL)
90                 return NULL;
91
92         ipaddress->family = family;
93         ipaddress->prefixlen = 0;
94         ipaddress->local = NULL;
95         ipaddress->peer = NULL;
96         ipaddress->broadcast = NULL;
97         ipaddress->gateway = NULL;
98
99         return ipaddress;
100 }
101
102 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
103 {
104         if (ipaddress == NULL)
105                 return;
106
107         g_free(ipaddress->broadcast);
108         g_free(ipaddress->peer);
109         g_free(ipaddress->local);
110         g_free(ipaddress->gateway);
111         g_free(ipaddress);
112 }
113
114 unsigned char __connman_ipconfig_netmask_prefix_len(const char *netmask)
115 {
116         unsigned char bits;
117         in_addr_t mask;
118         in_addr_t host;
119
120         if (netmask == NULL)
121                 return 32;
122
123         mask = inet_network(netmask);
124         host = ~mask;
125
126         /* a valid netmask must be 2^n - 1 */
127         if ((host & (host + 1)) != 0)
128                 return -1;
129
130         bits = 0;
131         for (; mask; mask <<= 1)
132                 ++bits;
133
134         return bits;
135 }
136
137 static gboolean check_ipv6_address(const char *address)
138 {
139         unsigned char buf[sizeof(struct in6_addr)];
140         int err;
141
142         err = inet_pton(AF_INET6, address, buf);
143         if (err > 0)
144                 return TRUE;
145
146         return FALSE;
147 }
148
149 int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress,
150                                 const char *address, const char *gateway,
151                                                 unsigned char prefix_length)
152 {
153         if (ipaddress == NULL)
154                 return -EINVAL;
155
156         if (check_ipv6_address(address) == FALSE)
157                 return -EINVAL;
158
159         if (check_ipv6_address(gateway) == FALSE)
160                 return -EINVAL;
161
162         DBG("prefix_len %d address %s gateway %s",
163                         prefix_length, address, gateway);
164
165         ipaddress->prefixlen = prefix_length;
166
167         g_free(ipaddress->local);
168         ipaddress->local = g_strdup(address);
169
170         g_free(ipaddress->gateway);
171         ipaddress->gateway = g_strdup(gateway);
172
173         return 0;
174 }
175
176 void connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress,
177                 const char *address, const char *netmask, const char *gateway)
178 {
179         if (ipaddress == NULL)
180                 return;
181
182         ipaddress->prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
183
184         g_free(ipaddress->local);
185         ipaddress->local = g_strdup(address);
186
187         g_free(ipaddress->gateway);
188         ipaddress->gateway = g_strdup(gateway);
189 }
190
191 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
192 {
193         if (ipaddress == NULL)
194                 return;
195
196         ipaddress->prefixlen = 0;
197
198         g_free(ipaddress->local);
199         ipaddress->local = NULL;
200
201         g_free(ipaddress->peer);
202         ipaddress->peer = NULL;
203
204         g_free(ipaddress->broadcast);
205         ipaddress->broadcast = NULL;
206
207         g_free(ipaddress->gateway);
208         ipaddress->gateway = NULL;
209 }
210
211 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
212                                         struct connman_ipaddress *source)
213 {
214         if (ipaddress == NULL || source == NULL)
215                 return;
216
217         ipaddress->family = source->family;
218         ipaddress->prefixlen = source->prefixlen;
219
220         g_free(ipaddress->local);
221         ipaddress->local = g_strdup(source->local);
222
223         g_free(ipaddress->peer);
224         ipaddress->peer = g_strdup(source->peer);
225
226         g_free(ipaddress->broadcast);
227         ipaddress->broadcast = g_strdup(source->broadcast);
228
229         g_free(ipaddress->gateway);
230         ipaddress->gateway = g_strdup(source->gateway);
231 }
232
233 static void free_address_list(struct connman_ipdevice *ipdevice)
234 {
235         GSList *list;
236
237         for (list = ipdevice->address_list; list; list = list->next) {
238                 struct connman_ipaddress *ipaddress = list->data;
239
240                 connman_ipaddress_free(ipaddress);
241                 list->data = NULL;
242         }
243
244         g_slist_free(ipdevice->address_list);
245         ipdevice->address_list = NULL;
246 }
247
248 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
249                                 unsigned char prefixlen, const char *local)
250 {
251         GSList *list;
252
253         for (list = ipdevice->address_list; list; list = list->next) {
254                 struct connman_ipaddress *ipaddress = list->data;
255
256                 if (g_strcmp0(ipaddress->local, local) == 0 &&
257                                         ipaddress->prefixlen == prefixlen)
258                         return ipaddress;
259         }
260
261         return NULL;
262 }
263
264 static const char *type2str(unsigned short type)
265 {
266         switch (type) {
267         case ARPHRD_ETHER:
268                 return "ETHER";
269         case ARPHRD_LOOPBACK:
270                 return "LOOPBACK";
271         case ARPHRD_PPP:
272                 return "PPP";
273         case ARPHRD_NONE:
274                 return "NONE";
275         case ARPHRD_VOID:
276                 return "VOID";
277         }
278
279         return "";
280 }
281
282 static const char *scope2str(unsigned char scope)
283 {
284         switch (scope) {
285         case 0:
286                 return "UNIVERSE";
287         case 253:
288                 return "LINK";
289         }
290
291         return "";
292 }
293
294 static void free_ipdevice(gpointer data)
295 {
296         struct connman_ipdevice *ipdevice = data;
297
298         connman_info("%s {remove} index %d", ipdevice->ifname,
299                                                         ipdevice->index);
300
301         if (ipdevice->config_ipv4 != NULL) {
302                 connman_ipconfig_unref(ipdevice->config_ipv4);
303                 ipdevice->config_ipv4 = NULL;
304         }
305
306         if (ipdevice->config_ipv6 != NULL) {
307                 connman_ipconfig_unref(ipdevice->config_ipv6);
308                 ipdevice->config_ipv6 = NULL;
309         }
310
311         free_address_list(ipdevice);
312         g_free(ipdevice->ipv4_gateway);
313         g_free(ipdevice->ipv6_gateway);
314         g_free(ipdevice->pac);
315
316         g_free(ipdevice->address);
317         g_free(ipdevice->ifname);
318         g_free(ipdevice);
319 }
320
321 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
322 {
323         DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
324                                         ipdevice->config_ipv6);
325 }
326
327 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
328 {
329         DBG("ipconfig ipv4 %p ipv6 %p", ipdevice->config_ipv4,
330                                         ipdevice->config_ipv6);
331
332         if (ipdevice->config_ipv4)
333                 connman_inet_clear_address(ipdevice->index,
334                                         ipdevice->config_ipv4->address);
335
336         if (ipdevice->config_ipv6)
337                 connman_inet_clear_ipv6_address(ipdevice->index,
338                                 ipdevice->config_ipv6->address->local,
339                                 ipdevice->config_ipv6->address->prefixlen);
340 }
341
342 static void update_stats(struct connman_ipdevice *ipdevice,
343                                                 struct rtnl_link_stats *stats)
344 {
345         struct connman_service *service;
346
347         if (stats->rx_packets == 0 && stats->tx_packets == 0)
348                 return;
349
350         connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
351                                         stats->rx_packets, stats->rx_bytes);
352         connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
353                                         stats->tx_packets, stats->tx_bytes);
354
355         if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
356                 return;
357
358         if (ipdevice->config_ipv4)
359                 service = connman_ipconfig_get_data(ipdevice->config_ipv4);
360         else if (ipdevice->config_ipv6)
361                 service = connman_ipconfig_get_data(ipdevice->config_ipv6);
362         else
363                 return;
364
365         if (service == NULL)
366                 return;
367
368         ipdevice->rx_packets = stats->rx_packets;
369         ipdevice->tx_packets = stats->tx_packets;
370         ipdevice->rx_bytes = stats->rx_bytes;
371         ipdevice->tx_bytes = stats->tx_bytes;
372         ipdevice->rx_errors = stats->rx_errors;
373         ipdevice->tx_errors = stats->tx_errors;
374         ipdevice->rx_dropped = stats->rx_dropped;
375         ipdevice->tx_dropped = stats->tx_dropped;
376
377         __connman_service_notify(service,
378                                 ipdevice->rx_packets, ipdevice->tx_packets,
379                                 ipdevice->rx_bytes, ipdevice->tx_bytes,
380                                 ipdevice->rx_errors, ipdevice->tx_errors,
381                                 ipdevice->rx_dropped, ipdevice->tx_dropped);
382 }
383
384 void __connman_ipconfig_newlink(int index, unsigned short type,
385                                 unsigned int flags, const char *address,
386                                                         unsigned short mtu,
387                                                 struct rtnl_link_stats *stats)
388 {
389         struct connman_ipdevice *ipdevice;
390         GList *list;
391         GString *str;
392         gboolean up = FALSE, down = FALSE;
393         gboolean lower_up = FALSE, lower_down = FALSE;
394
395         DBG("index %d", index);
396
397         if (type == ARPHRD_LOOPBACK)
398                 return;
399
400         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
401         if (ipdevice != NULL)
402                 goto update;
403
404         ipdevice = g_try_new0(struct connman_ipdevice, 1);
405         if (ipdevice == NULL)
406                 return;
407
408         ipdevice->index = index;
409         ipdevice->ifname = connman_inet_ifname(index);
410         ipdevice->type = type;
411
412         ipdevice->address = g_strdup(address);
413
414         g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
415
416         connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
417                                                 index, type, type2str(type));
418
419 update:
420         ipdevice->mtu = mtu;
421
422         update_stats(ipdevice, stats);
423
424         if (flags == ipdevice->flags)
425                 return;
426
427         if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
428                 if (flags & IFF_UP)
429                         up = TRUE;
430                 else
431                         down = TRUE;
432         }
433
434         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
435                                 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
436                 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
437                                         (IFF_RUNNING | IFF_LOWER_UP))
438                         lower_up = TRUE;
439                 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
440                         lower_down = TRUE;
441         }
442
443         ipdevice->flags = flags;
444
445         str = g_string_new(NULL);
446         if (str == NULL)
447                 return;
448
449         if (flags & IFF_UP)
450                 g_string_append(str, "UP");
451         else
452                 g_string_append(str, "DOWN");
453
454         if (flags & IFF_RUNNING)
455                 g_string_append(str, ",RUNNING");
456
457         if (flags & IFF_LOWER_UP)
458                 g_string_append(str, ",LOWER_UP");
459
460         connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
461                                                         flags, str->str);
462
463         g_string_free(str, TRUE);
464
465         for (list = g_list_first(ipconfig_list); list;
466                                                 list = g_list_next(list)) {
467                 struct connman_ipconfig *ipconfig = list->data;
468
469                 if (index != ipconfig->index)
470                         continue;
471
472                 if (ipconfig->ops == NULL)
473                         continue;
474
475                 if (up == TRUE && ipconfig->ops->up)
476                         ipconfig->ops->up(ipconfig);
477                 if (lower_up == TRUE && ipconfig->ops->lower_up)
478                         ipconfig->ops->lower_up(ipconfig);
479
480                 if (lower_down == TRUE && ipconfig->ops->lower_down)
481                         ipconfig->ops->lower_down(ipconfig);
482                 if (down == TRUE && ipconfig->ops->down)
483                         ipconfig->ops->down(ipconfig);
484         }
485
486         if (lower_up)
487                 __connman_ipconfig_lower_up(ipdevice);
488         if (lower_down)
489                 __connman_ipconfig_lower_down(ipdevice);
490 }
491
492 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
493 {
494         struct connman_ipdevice *ipdevice;
495         GList *list;
496
497         DBG("index %d", index);
498
499         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
500         if (ipdevice == NULL)
501                 return;
502
503         update_stats(ipdevice, stats);
504
505         for (list = g_list_first(ipconfig_list); list;
506                                                 list = g_list_next(list)) {
507                 struct connman_ipconfig *ipconfig = list->data;
508
509                 if (index != ipconfig->index)
510                         continue;
511
512                 ipconfig->index = -1;
513
514                 if (ipconfig->ops == NULL)
515                         continue;
516
517                 if (ipconfig->ops->lower_down)
518                         ipconfig->ops->lower_down(ipconfig);
519                 if (ipconfig->ops->down)
520                         ipconfig->ops->down(ipconfig);
521         }
522
523         __connman_ipconfig_lower_down(ipdevice);
524
525         g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
526 }
527
528 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
529 {
530         const struct connman_ipaddress *addr1 = a;
531         const struct connman_ipaddress *addr2 = b;
532
533         if (addr1->prefixlen != addr2->prefixlen)
534                 return addr2->prefixlen - addr1->prefixlen;
535
536         return g_strcmp0(addr1->local, addr2->local);
537 }
538
539 void __connman_ipconfig_newaddr(int index, int family, const char *label,
540                                 unsigned char prefixlen, const char *address)
541 {
542         struct connman_ipdevice *ipdevice;
543         struct connman_ipaddress *ipaddress;
544         GList *list;
545
546         DBG("index %d", index);
547
548         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
549         if (ipdevice == NULL)
550                 return;
551
552         ipaddress = connman_ipaddress_alloc(family);
553         if (ipaddress == NULL)
554                 return;
555
556         ipaddress->prefixlen = prefixlen;
557         ipaddress->local = g_strdup(address);
558
559         if (g_slist_find_custom(ipdevice->address_list, ipaddress,
560                                         check_duplicate_address)) {
561                 connman_ipaddress_free(ipaddress);
562                 return;
563         }
564
565         ipdevice->address_list = g_slist_append(ipdevice->address_list,
566                                                                 ipaddress);
567
568         connman_info("%s {add} address %s/%u label %s family %d",
569                 ipdevice->ifname, address, prefixlen, label, family);
570
571         if (ipdevice->config_ipv4 != NULL && family == AF_INET)
572                 connman_ipaddress_copy(ipdevice->config_ipv4->system,
573                                         ipaddress);
574
575         else if (ipdevice->config_ipv6 != NULL && family == AF_INET6)
576                 connman_ipaddress_copy(ipdevice->config_ipv6->system,
577                                         ipaddress);
578         else
579                 return;
580
581         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
582                 return;
583
584         for (list = g_list_first(ipconfig_list); list;
585                                                 list = g_list_next(list)) {
586                 struct connman_ipconfig *ipconfig = list->data;
587
588                 if (index != ipconfig->index)
589                         continue;
590
591                 if (ipconfig->ops == NULL)
592                         continue;
593
594                 if (ipconfig->ops->ip_bound)
595                         ipconfig->ops->ip_bound(ipconfig);
596         }
597 }
598
599 void __connman_ipconfig_deladdr(int index, int family, const char *label,
600                                 unsigned char prefixlen, const char *address)
601 {
602         struct connman_ipdevice *ipdevice;
603         struct connman_ipaddress *ipaddress;
604         GList *list;
605
606         DBG("index %d", index);
607
608         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
609         if (ipdevice == NULL)
610                 return;
611
612         ipaddress = find_ipaddress(ipdevice, prefixlen, address);
613         if (ipaddress == NULL)
614                 return;
615
616         ipdevice->address_list = g_slist_remove(ipdevice->address_list,
617                                                                 ipaddress);
618
619         connman_ipaddress_free(ipaddress);
620
621         connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
622                                                 address, prefixlen, label);
623
624         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
625                 return;
626
627         if (g_slist_length(ipdevice->address_list) > 0)
628                 return;
629
630         for (list = g_list_first(ipconfig_list); list;
631                                                 list = g_list_next(list)) {
632                 struct connman_ipconfig *ipconfig = list->data;
633
634                 if (index != ipconfig->index)
635                         continue;
636
637                 if (ipconfig->ops == NULL)
638                         continue;
639
640                 if (ipconfig->ops->ip_release)
641                         ipconfig->ops->ip_release(ipconfig);
642         }
643 }
644
645 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
646                                         const char *dst, const char *gateway)
647 {
648         struct connman_ipdevice *ipdevice;
649
650         DBG("index %d", index);
651
652         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
653         if (ipdevice == NULL)
654                 return;
655
656         if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
657                 GSList *list;
658                 GList *config_list;
659
660                 if (family == AF_INET6) {
661                         g_free(ipdevice->ipv6_gateway);
662                         ipdevice->ipv6_gateway = g_strdup(gateway);
663
664                         if (ipdevice->config_ipv6 != NULL &&
665                                 ipdevice->config_ipv6->system != NULL) {
666                                 g_free(ipdevice->config_ipv6->system->gateway);
667                                 ipdevice->config_ipv6->system->gateway =
668                                         g_strdup(gateway);
669                         }
670                 } else {
671                         g_free(ipdevice->ipv4_gateway);
672                         ipdevice->ipv4_gateway = g_strdup(gateway);
673
674                         if (ipdevice->config_ipv4 != NULL &&
675                                 ipdevice->config_ipv4->system != NULL) {
676                                 g_free(ipdevice->config_ipv4->system->gateway);
677                                 ipdevice->config_ipv4->system->gateway =
678                                         g_strdup(gateway);
679                         }
680                 }
681
682                 for (list = ipdevice->address_list; list; list = list->next) {
683                         struct connman_ipaddress *ipaddress = list->data;
684
685                         g_free(ipaddress->gateway);
686                         ipaddress->gateway = g_strdup(gateway);
687                 }
688
689                 for (config_list = g_list_first(ipconfig_list); config_list;
690                                         config_list = g_list_next(config_list)) {
691                         struct connman_ipconfig *ipconfig = config_list->data;
692
693                         if (index != ipconfig->index)
694                                 continue;
695
696                         if (ipconfig->ops == NULL)
697                                 continue;
698
699                         if (ipconfig->ops->ip_bound)
700                                 ipconfig->ops->ip_bound(ipconfig);
701                 }
702         }
703
704         connman_info("%s {add} route %s gw %s scope %u <%s>",
705                                         ipdevice->ifname, dst, gateway,
706                                                 scope, scope2str(scope));
707 }
708
709 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
710                                         const char *dst, const char *gateway)
711 {
712         struct connman_ipdevice *ipdevice;
713
714         DBG("index %d", index);
715
716         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
717         if (ipdevice == NULL)
718                 return;
719
720         if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
721                 GSList *list;
722                 GList *config_list;
723
724                 if (family == AF_INET6) {
725                         g_free(ipdevice->ipv6_gateway);
726                         ipdevice->ipv6_gateway = NULL;
727
728                         if (ipdevice->config_ipv6 != NULL &&
729                                 ipdevice->config_ipv6->system != NULL) {
730                                 g_free(ipdevice->config_ipv6->system->gateway);
731                                 ipdevice->config_ipv6->system->gateway = NULL;
732                         }
733                 } else {
734                         g_free(ipdevice->ipv4_gateway);
735                         ipdevice->ipv4_gateway = NULL;
736
737                         if (ipdevice->config_ipv4 != NULL &&
738                                 ipdevice->config_ipv4->system != NULL) {
739                                 g_free(ipdevice->config_ipv4->system->gateway);
740                                 ipdevice->config_ipv4->system->gateway = NULL;
741                         }
742                 }
743
744                 for (list = ipdevice->address_list; list; list = list->next) {
745                         struct connman_ipaddress *ipaddress = list->data;
746
747                         g_free(ipaddress->gateway);
748                         ipaddress->gateway = NULL;
749                 }
750
751                 for (config_list = g_list_first(ipconfig_list); config_list;
752                                         config_list = g_list_next(config_list)) {
753                         struct connman_ipconfig *ipconfig = config_list->data;
754
755                         if (index != ipconfig->index)
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         connman_info("%s {del} route %s gw %s scope %u <%s>",
767                                         ipdevice->ifname, dst, gateway,
768                                                 scope, scope2str(scope));
769 }
770
771 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
772                                                         void *user_data)
773 {
774         GList *list, *keys;
775
776         keys = g_hash_table_get_keys(ipdevice_hash);
777         if (keys == NULL)
778                 return;
779
780         for (list = g_list_first(keys); list; list = g_list_next(list)) {
781                 int index = GPOINTER_TO_INT(list->data);
782
783                 function(index, user_data);
784         }
785
786         g_list_free(keys);
787 }
788
789 unsigned short __connman_ipconfig_get_type(int index)
790 {
791         struct connman_ipdevice *ipdevice;
792
793         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
794         if (ipdevice == NULL)
795                 return ARPHRD_VOID;
796
797         return ipdevice->type;
798 }
799
800 unsigned int __connman_ipconfig_get_flags(int index)
801 {
802         struct connman_ipdevice *ipdevice;
803
804         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
805         if (ipdevice == NULL)
806                 return 0;
807
808         return ipdevice->flags;
809 }
810
811 const char *__connman_ipconfig_get_gateway(int index)
812 {
813         struct connman_ipdevice *ipdevice;
814
815         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
816         if (ipdevice == NULL)
817                 return NULL;
818
819         if (ipdevice->ipv4_gateway != NULL)
820                 return ipdevice->ipv4_gateway;
821
822         if (ipdevice->config_ipv4 != NULL &&
823                         ipdevice->config_ipv4->address != NULL)
824                 return ipdevice->config_ipv4->address->gateway;
825
826         if (ipdevice->ipv6_gateway != NULL)
827                 return ipdevice->ipv6_gateway;
828
829         if (ipdevice->config_ipv6 != NULL &&
830                         ipdevice->config_ipv6->address != NULL)
831                 return ipdevice->config_ipv6->address->gateway;
832
833         return NULL;
834 }
835
836 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
837 {
838         ipconfig->index = index;
839 }
840
841 static struct connman_ipconfig *create_ipv6config(int index)
842 {
843         struct connman_ipconfig *ipv6config;
844
845         DBG("index %d", index);
846
847         ipv6config = g_try_new0(struct connman_ipconfig, 1);
848         if (ipv6config == NULL)
849                 return NULL;
850
851         ipv6config->refcount = 1;
852
853         ipv6config->index = index;
854         ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
855         ipv6config->method = CONNMAN_IPCONFIG_METHOD_OFF;
856
857         ipv6config->address = connman_ipaddress_alloc(AF_INET6);
858         if (ipv6config->address == NULL) {
859                 g_free(ipv6config);
860                 return NULL;
861         }
862
863         ipv6config->system = connman_ipaddress_alloc(AF_INET6);
864
865         DBG("ipconfig %p", ipv6config);
866
867         return ipv6config;
868 }
869
870 /**
871  * connman_ipconfig_create:
872  *
873  * Allocate a new ipconfig structure.
874  *
875  * Returns: a newly-allocated #connman_ipconfig structure
876  */
877 struct connman_ipconfig *connman_ipconfig_create(int index,
878                                         enum connman_ipconfig_type type)
879 {
880         struct connman_ipconfig *ipconfig;
881
882         if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
883                 return create_ipv6config(index);
884
885         DBG("index %d", index);
886
887         ipconfig = g_try_new0(struct connman_ipconfig, 1);
888         if (ipconfig == NULL)
889                 return NULL;
890
891         ipconfig->refcount = 1;
892
893         ipconfig->index = index;
894         ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
895
896         ipconfig->address = connman_ipaddress_alloc(AF_INET);
897         if (ipconfig->address == NULL) {
898                 g_free(ipconfig);
899                 return NULL;
900         }
901
902         ipconfig->system = connman_ipaddress_alloc(AF_INET);
903
904         DBG("ipconfig %p", ipconfig);
905
906         return ipconfig;
907 }
908
909 /**
910  * connman_ipconfig_clone:
911  *
912  * Clone an ipconfig structure and create new reference.
913  *
914  * Returns: a newly-allocated #connman_ipconfig structure
915  */
916 struct connman_ipconfig *connman_ipconfig_clone(struct connman_ipconfig *ipconfig)
917 {
918         struct connman_ipconfig *ipconfig_clone;
919
920         DBG("ipconfig %p", ipconfig);
921
922         ipconfig_clone = g_try_new0(struct connman_ipconfig, 1);
923         if (ipconfig_clone == NULL)
924                 return NULL;
925
926         ipconfig_clone->refcount = 1;
927
928         ipconfig_clone->origin = connman_ipconfig_ref(ipconfig);
929
930         ipconfig_clone->index = -1;
931
932         return ipconfig_clone;
933 }
934
935 /**
936  * connman_ipconfig_ref:
937  * @ipconfig: ipconfig structure
938  *
939  * Increase reference counter of ipconfig
940  */
941 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
942 {
943         g_atomic_int_inc(&ipconfig->refcount);
944
945         return ipconfig;
946 }
947
948 /**
949  * connman_ipconfig_unref:
950  * @ipconfig: ipconfig structure
951  *
952  * Decrease reference counter of ipconfig
953  */
954 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
955 {
956         if (ipconfig &&
957                 g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
958                 __connman_ipconfig_disable(ipconfig);
959
960                 connman_ipconfig_set_ops(ipconfig, NULL);
961
962                 if (ipconfig->origin != NULL) {
963                         connman_ipconfig_unref(ipconfig->origin);
964                         ipconfig->origin = NULL;
965                 }
966
967                 connman_ipaddress_free(ipconfig->system);
968                 connman_ipaddress_free(ipconfig->address);
969                 g_free(ipconfig);
970         }
971 }
972
973 /**
974  * connman_ipconfig_get_data:
975  * @ipconfig: ipconfig structure
976  *
977  * Get private data pointer
978  */
979 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
980 {
981         return ipconfig->ops_data;
982 }
983
984 /**
985  * connman_ipconfig_set_data:
986  * @ipconfig: ipconfig structure
987  * @data: data pointer
988  *
989  * Set private data pointer
990  */
991 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
992 {
993         ipconfig->ops_data = data;
994 }
995
996 /**
997  * connman_ipconfig_get_index:
998  * @ipconfig: ipconfig structure
999  *
1000  * Get interface index
1001  */
1002 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1003 {
1004         if (ipconfig == NULL)
1005                 return -1;
1006
1007         if (ipconfig->origin != NULL)
1008                 return ipconfig->origin->index;
1009
1010         return ipconfig->index;
1011 }
1012
1013 /**
1014  * connman_ipconfig_get_ifname:
1015  * @ipconfig: ipconfig structure
1016  *
1017  * Get interface name
1018  */
1019 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1020 {
1021         struct connman_ipdevice *ipdevice;
1022
1023         if (ipconfig == NULL)
1024                 return NULL;
1025
1026         if (ipconfig->index < 0)
1027                 return NULL;
1028
1029         ipdevice = g_hash_table_lookup(ipdevice_hash,
1030                                         GINT_TO_POINTER(ipconfig->index));
1031         if (ipdevice == NULL)
1032                 return NULL;
1033
1034         return ipdevice->ifname;
1035 }
1036
1037 /**
1038  * connman_ipconfig_set_ops:
1039  * @ipconfig: ipconfig structure
1040  * @ops: operation callbacks
1041  *
1042  * Set the operation callbacks
1043  */
1044 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1045                                 const struct connman_ipconfig_ops *ops)
1046 {
1047         ipconfig->ops = ops;
1048 }
1049
1050 /**
1051  * connman_ipconfig_set_method:
1052  * @ipconfig: ipconfig structure
1053  * @method: configuration method
1054  *
1055  * Set the configuration method
1056  */
1057 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1058                                         enum connman_ipconfig_method method)
1059 {
1060         ipconfig->method = method;
1061
1062         return 0;
1063 }
1064
1065 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1066 {
1067         if (ipconfig == NULL)
1068                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1069
1070         return ipconfig->method;
1071 }
1072
1073 /**
1074  * connman_ipconfig_bind:
1075  * @ipconfig: ipconfig structure
1076  * @ipaddress: ipaddress structure
1077  *
1078  * Bind IP address details to configuration
1079  */
1080 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
1081                                         struct connman_ipaddress *ipaddress)
1082 {
1083         struct connman_ipconfig *origin;
1084
1085         origin = ipconfig->origin ? ipconfig->origin : ipconfig;
1086
1087         connman_ipaddress_copy(origin->address, ipaddress);
1088
1089         connman_inet_set_address(origin->index, origin->address);
1090 }
1091
1092 void __connman_ipconfig_set_element_ipv6_gateway(
1093                         struct connman_ipconfig *ipconfig,
1094                                 struct connman_element *element)
1095 {
1096         if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1097                 element->ipv6.gateway = ipconfig->address->gateway;
1098 }
1099
1100 /*
1101  * FIXME: The element soulution should be removed in the future
1102  * Set IPv4 and IPv6 gateway
1103  */
1104 int __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig,
1105                                                 struct connman_element *parent)
1106 {
1107         struct connman_element *connection;
1108
1109         connection = connman_element_create(NULL);
1110
1111         DBG("ipconfig %p", ipconfig);
1112
1113         connection->type  = CONNMAN_ELEMENT_TYPE_CONNECTION;
1114         connection->index = ipconfig->index;
1115
1116         if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1117                 connection->ipv4.gateway = ipconfig->address->gateway;
1118         else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1119                 connection->ipv6.gateway = ipconfig->address->gateway;
1120
1121         if (connman_element_register(connection, parent) < 0)
1122                 connman_element_unref(connection);
1123
1124         return 0;
1125 }
1126
1127 int __connman_ipconfig_set_address(struct connman_ipconfig *ipconfig)
1128 {
1129         DBG("");
1130
1131         switch (ipconfig->method) {
1132         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1133         case CONNMAN_IPCONFIG_METHOD_OFF:
1134         case CONNMAN_IPCONFIG_METHOD_FIXED:
1135         case CONNMAN_IPCONFIG_METHOD_DHCP:
1136                 break;
1137         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1138                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1139                         return connman_inet_set_address(ipconfig->index,
1140                                                         ipconfig->address);
1141                 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1142                         return connman_inet_set_ipv6_address(
1143                                         ipconfig->index, ipconfig->address);
1144         }
1145
1146         return 0;
1147 }
1148
1149 int __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
1150 {
1151         DBG("");
1152
1153         if (ipconfig == NULL)
1154                 return 0;
1155
1156         DBG("method %d", ipconfig->method);
1157
1158         switch (ipconfig->method) {
1159         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1160         case CONNMAN_IPCONFIG_METHOD_OFF:
1161         case CONNMAN_IPCONFIG_METHOD_FIXED:
1162         case CONNMAN_IPCONFIG_METHOD_DHCP:
1163                 break;
1164         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1165                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1166                         return connman_inet_clear_address(ipconfig->index,
1167                                                         ipconfig->address);
1168                 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1169                         return connman_inet_clear_ipv6_address(
1170                                                 ipconfig->index,
1171                                                 ipconfig->address->local,
1172                                                 ipconfig->address->prefixlen);
1173         }
1174
1175         return 0;
1176 }
1177
1178 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1179                                                         const char *url)
1180 {
1181         struct connman_ipdevice *ipdevice;
1182
1183         DBG("ipconfig %p", ipconfig);
1184
1185         if (ipconfig == NULL || ipconfig->index < 0)
1186                 return -ENODEV;
1187
1188         ipdevice = g_hash_table_lookup(ipdevice_hash,
1189                                         GINT_TO_POINTER(ipconfig->index));
1190         if (ipdevice == NULL)
1191                 return -ENXIO;
1192
1193         g_free(ipdevice->pac);
1194         ipdevice->pac = g_strdup(url);
1195
1196         return 0;
1197 }
1198
1199 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1200 {
1201         struct connman_ipdevice *ipdevice;
1202
1203         DBG("ipconfig %p", ipconfig);
1204
1205         if (ipconfig == NULL || ipconfig->index < 0)
1206                 return NULL;
1207
1208         ipdevice = g_hash_table_lookup(ipdevice_hash,
1209                                         GINT_TO_POINTER(ipconfig->index));
1210         if (ipdevice == NULL)
1211                 return NULL;
1212
1213         return ipdevice->pac;
1214 }
1215
1216 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1217 {
1218         struct connman_ipdevice *ipdevice;
1219         gboolean up = FALSE, down = FALSE;
1220         gboolean lower_up = FALSE, lower_down = FALSE;
1221         enum connman_ipconfig_type type;
1222
1223         DBG("ipconfig %p", ipconfig);
1224
1225         if (ipconfig == NULL || ipconfig->index < 0)
1226                 return -ENODEV;
1227
1228         ipdevice = g_hash_table_lookup(ipdevice_hash,
1229                                         GINT_TO_POINTER(ipconfig->index));
1230         if (ipdevice == NULL)
1231                 return -ENXIO;
1232
1233         if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
1234                 if (ipdevice->config_ipv4 == ipconfig)
1235                         return -EALREADY;
1236                 type = CONNMAN_IPCONFIG_TYPE_IPV4;
1237         } else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6) {
1238                 if (ipdevice->config_ipv6 == ipconfig)
1239                         return -EALREADY;
1240                 type = CONNMAN_IPCONFIG_TYPE_IPV6;
1241         } else
1242                 return -EINVAL;
1243
1244         if (type == CONNMAN_IPCONFIG_TYPE_IPV4 &&
1245                                         ipdevice->config_ipv4 != NULL) {
1246                 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1247
1248                 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1249
1250                 connman_ipconfig_unref(ipdevice->config_ipv4);
1251         }
1252
1253         if (type == CONNMAN_IPCONFIG_TYPE_IPV6 &&
1254                                         ipdevice->config_ipv6 != NULL) {
1255                 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1256
1257                 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1258
1259                 connman_ipconfig_unref(ipdevice->config_ipv6);
1260         }
1261
1262         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1263                 ipdevice->config_ipv4 = connman_ipconfig_ref(ipconfig);
1264         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
1265                 ipdevice->config_ipv6 = connman_ipconfig_ref(ipconfig);
1266
1267         ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1268
1269         if (ipdevice->flags & IFF_UP)
1270                 up = TRUE;
1271         else
1272                 down = TRUE;
1273
1274         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1275                         (IFF_RUNNING | IFF_LOWER_UP))
1276                 lower_up = TRUE;
1277         else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1278                 lower_down = TRUE;
1279
1280         if (up == TRUE && ipconfig->ops->up)
1281                 ipconfig->ops->up(ipconfig);
1282         if (lower_up == TRUE && ipconfig->ops->lower_up)
1283                 ipconfig->ops->lower_up(ipconfig);
1284
1285         if (lower_down == TRUE && ipconfig->ops->lower_down)
1286                 ipconfig->ops->lower_down(ipconfig);
1287         if (down == TRUE && ipconfig->ops->down)
1288                 ipconfig->ops->down(ipconfig);
1289
1290         return 0;
1291 }
1292
1293 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1294 {
1295         struct connman_ipdevice *ipdevice;
1296
1297         DBG("ipconfig %p", ipconfig);
1298
1299         if (ipconfig == NULL || ipconfig->index < 0)
1300                 return -ENODEV;
1301
1302         ipdevice = g_hash_table_lookup(ipdevice_hash,
1303                                         GINT_TO_POINTER(ipconfig->index));
1304         if (ipdevice == NULL)
1305                 return -ENXIO;
1306
1307         if (ipdevice->config_ipv4 == NULL && ipdevice->config_ipv6 == NULL)
1308                 return -EINVAL;
1309
1310         if (ipdevice->config_ipv4 == ipconfig) {
1311                 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1312
1313                 connman_ipaddress_clear(ipdevice->config_ipv4->system);
1314                 connman_ipconfig_unref(ipdevice->config_ipv4);
1315                 ipdevice->config_ipv4 = NULL;
1316                 return 0;
1317         }
1318
1319         if (ipdevice->config_ipv6 == ipconfig) {
1320                 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1321
1322                 connman_ipaddress_clear(ipdevice->config_ipv6->system);
1323                 connman_ipconfig_unref(ipdevice->config_ipv6);
1324                 ipdevice->config_ipv6 = NULL;
1325                 return 0;
1326         }
1327
1328         return -EINVAL;
1329 }
1330
1331 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1332 {
1333         switch (method) {
1334         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1335                 break;
1336         case CONNMAN_IPCONFIG_METHOD_OFF:
1337                 return "off";
1338         case CONNMAN_IPCONFIG_METHOD_FIXED:
1339                 return "fixed";
1340         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1341                 return "manual";
1342         case CONNMAN_IPCONFIG_METHOD_DHCP:
1343                 return "dhcp";
1344         }
1345
1346         return NULL;
1347 }
1348
1349 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1350 {
1351         if (g_strcmp0(method, "off") == 0)
1352                 return CONNMAN_IPCONFIG_METHOD_OFF;
1353         else if (g_strcmp0(method, "fixed") == 0)
1354                 return CONNMAN_IPCONFIG_METHOD_FIXED;
1355         else if (g_strcmp0(method, "manual") == 0)
1356                 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1357         else if (g_strcmp0(method, "dhcp") == 0)
1358                 return CONNMAN_IPCONFIG_METHOD_DHCP;
1359         else
1360                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1361 }
1362
1363 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1364                                                         DBusMessageIter *iter)
1365 {
1366         const char *str;
1367
1368         DBG("");
1369
1370         if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV4)
1371                 return;
1372
1373         str = __connman_ipconfig_method2string(ipconfig->method);
1374         if (str == NULL)
1375                 return;
1376
1377         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1378
1379         if (ipconfig->system == NULL)
1380                 return;
1381
1382         if (ipconfig->system->local != NULL) {
1383                 in_addr_t addr;
1384                 struct in_addr netmask;
1385                 char *mask;
1386
1387                 connman_dbus_dict_append_basic(iter, "Address",
1388                                 DBUS_TYPE_STRING, &ipconfig->system->local);
1389
1390                 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1391                 netmask.s_addr = htonl(addr);
1392                 mask = inet_ntoa(netmask);
1393                 connman_dbus_dict_append_basic(iter, "Netmask",
1394                                                 DBUS_TYPE_STRING, &mask);
1395         }
1396
1397         if (ipconfig->system->gateway != NULL)
1398                 connman_dbus_dict_append_basic(iter, "Gateway",
1399                                 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1400 }
1401
1402 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1403                                                         DBusMessageIter *iter)
1404 {
1405         const char *str;
1406
1407         DBG("");
1408
1409         if (ipconfig->type != CONNMAN_IPCONFIG_TYPE_IPV6)
1410                 return;
1411
1412         str = __connman_ipconfig_method2string(ipconfig->method);
1413         if (str == NULL)
1414                 return;
1415
1416         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1417
1418         if (ipconfig->system == NULL)
1419                 return;
1420
1421         if (ipconfig->system->local != NULL) {
1422                 connman_dbus_dict_append_basic(iter, "Address",
1423                                 DBUS_TYPE_STRING, &ipconfig->system->local);
1424                 connman_dbus_dict_append_basic(iter, "PrefixLength",
1425                                                 DBUS_TYPE_BYTE,
1426                                                 &ipconfig->system->prefixlen);
1427         }
1428
1429         if (ipconfig->system->gateway != NULL)
1430                 connman_dbus_dict_append_basic(iter, "Gateway",
1431                                 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1432 }
1433
1434 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1435                                                         DBusMessageIter *iter)
1436 {
1437         const char *str;
1438
1439         DBG("");
1440
1441         str = __connman_ipconfig_method2string(ipconfig->method);
1442         if (str == NULL)
1443                 return;
1444
1445         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1446
1447         switch (ipconfig->method) {
1448         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1449         case CONNMAN_IPCONFIG_METHOD_OFF:
1450         case CONNMAN_IPCONFIG_METHOD_DHCP:
1451                 return;
1452         case CONNMAN_IPCONFIG_METHOD_FIXED:
1453         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1454                 break;
1455         }
1456
1457         if (ipconfig->address == NULL)
1458                 return;
1459
1460         if (ipconfig->address->local != NULL) {
1461                 connman_dbus_dict_append_basic(iter, "Address",
1462                                 DBUS_TYPE_STRING, &ipconfig->address->local);
1463                 connman_dbus_dict_append_basic(iter, "PrefixLength",
1464                                                 DBUS_TYPE_BYTE,
1465                                                 &ipconfig->address->prefixlen);
1466         }
1467
1468         if (ipconfig->address->gateway != NULL)
1469                 connman_dbus_dict_append_basic(iter, "Gateway",
1470                                 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1471 }
1472
1473 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1474                                                         DBusMessageIter *iter)
1475 {
1476         const char *str;
1477
1478         DBG("");
1479
1480         str = __connman_ipconfig_method2string(ipconfig->method);
1481         if (str == NULL)
1482                 return;
1483
1484         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1485
1486         switch (ipconfig->method) {
1487         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1488         case CONNMAN_IPCONFIG_METHOD_OFF:
1489         case CONNMAN_IPCONFIG_METHOD_FIXED:
1490         case CONNMAN_IPCONFIG_METHOD_DHCP:
1491                 return;
1492         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1493                 break;
1494         }
1495
1496         if (ipconfig->address == NULL)
1497                 return;
1498
1499         if (ipconfig->address->local != NULL) {
1500                 in_addr_t addr;
1501                 struct in_addr netmask;
1502                 char *mask;
1503
1504                 connman_dbus_dict_append_basic(iter, "Address",
1505                                 DBUS_TYPE_STRING, &ipconfig->address->local);
1506
1507                 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1508                 netmask.s_addr = htonl(addr);
1509                 mask = inet_ntoa(netmask);
1510                 connman_dbus_dict_append_basic(iter, "Netmask",
1511                                                 DBUS_TYPE_STRING, &mask);
1512         }
1513
1514         if (ipconfig->address->gateway != NULL)
1515                 connman_dbus_dict_append_basic(iter, "Gateway",
1516                                 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1517 }
1518
1519 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1520                 enum connman_ipconfig_type type, DBusMessageIter *array)
1521 {
1522         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1523         const char *address = NULL, *netmask = NULL, *gateway = NULL,
1524                         *prefix_length_string = NULL;
1525         int prefix_length = 0;
1526         DBusMessageIter dict;
1527
1528         DBG("ipconfig %p type %d", ipconfig, type);
1529
1530         if (type != CONNMAN_IPCONFIG_TYPE_IPV4 &&
1531                         type != CONNMAN_IPCONFIG_TYPE_IPV6)
1532                 return -EINVAL;
1533
1534         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1535                 return -EINVAL;
1536
1537         dbus_message_iter_recurse(array, &dict);
1538
1539         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1540                 DBusMessageIter entry;
1541                 const char *key;
1542                 int type;
1543
1544                 dbus_message_iter_recurse(&dict, &entry);
1545
1546                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1547                         return -EINVAL;
1548
1549                 dbus_message_iter_get_basic(&entry, &key);
1550                 dbus_message_iter_next(&entry);
1551
1552                 type = dbus_message_iter_get_arg_type(&entry);
1553
1554                 if (g_str_equal(key, "Method") == TRUE) {
1555                         const char *str;
1556
1557                         if (type != DBUS_TYPE_STRING)
1558                                 return -EINVAL;
1559
1560                         dbus_message_iter_get_basic(&entry, &str);
1561                         method = __connman_ipconfig_string2method(str);
1562                 } else if (g_str_equal(key, "Address") == TRUE) {
1563                         if (type != DBUS_TYPE_STRING)
1564                                 return -EINVAL;
1565
1566                         dbus_message_iter_get_basic(&entry, &address);
1567                 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1568                         if (type != DBUS_TYPE_STRING)
1569                                 return -EINVAL;
1570
1571                         dbus_message_iter_get_basic(&entry,
1572                                                         &prefix_length_string);
1573
1574                         prefix_length = atoi(prefix_length_string);
1575                         if (prefix_length < 0 || prefix_length > 128)
1576                                 return -EINVAL;
1577
1578                 } else if (g_str_equal(key, "Netmask") == TRUE) {
1579                         if (type != DBUS_TYPE_STRING)
1580                                 return -EINVAL;
1581
1582                         dbus_message_iter_get_basic(&entry, &netmask);
1583                 } else if (g_str_equal(key, "Gateway") == TRUE) {
1584                         if (type != DBUS_TYPE_STRING)
1585                                 return -EINVAL;
1586
1587                         dbus_message_iter_get_basic(&entry, &gateway);
1588                 }
1589                 dbus_message_iter_next(&dict);
1590         }
1591
1592         DBG("method %d address %s netmask %s gateway %s prefix_length %d",
1593                         method, address, netmask, gateway, prefix_length);
1594
1595         switch (method) {
1596         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1597         case CONNMAN_IPCONFIG_METHOD_OFF:
1598         case CONNMAN_IPCONFIG_METHOD_FIXED:
1599                 return -EINVAL;
1600
1601         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1602                 if (address == NULL)
1603                         return -EINVAL;
1604
1605                 ipconfig->method = method;
1606
1607                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1608                         connman_ipaddress_set_ipv4(ipconfig->address,
1609                                                 address, netmask, gateway);
1610                 else
1611                         return connman_ipaddress_set_ipv6(
1612                                         ipconfig->address, address,
1613                                                 gateway, prefix_length);
1614                 break;
1615
1616         case CONNMAN_IPCONFIG_METHOD_DHCP:
1617                 if (ipconfig->method == method)
1618                         return 0;
1619
1620                 ipconfig->method = method;
1621                 break;
1622         }
1623
1624         return 0;
1625 }
1626
1627 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1628                                                         DBusMessageIter *iter)
1629 {
1630         struct connman_ipdevice *ipdevice;
1631         const char *method = "auto";
1632
1633         connman_dbus_dict_append_basic(iter, "Method",
1634                                                 DBUS_TYPE_STRING, &method);
1635
1636         ipdevice = g_hash_table_lookup(ipdevice_hash,
1637                                         GINT_TO_POINTER(ipconfig->index));
1638         if (ipdevice == NULL)
1639                 return;
1640
1641         if (ipdevice->ifname != NULL)
1642                 connman_dbus_dict_append_basic(iter, "Interface",
1643                                         DBUS_TYPE_STRING, &ipdevice->ifname);
1644
1645         if (ipdevice->address != NULL)
1646                 connman_dbus_dict_append_basic(iter, "Address",
1647                                         DBUS_TYPE_STRING, &ipdevice->address);
1648
1649         if (ipdevice->mtu > 0)
1650                 connman_dbus_dict_append_basic(iter, "MTU",
1651                                         DBUS_TYPE_UINT16, &ipdevice->mtu);
1652 }
1653
1654 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1655                 GKeyFile *keyfile, const char *identifier, const char *prefix)
1656 {
1657         const char *method;
1658         char *key;
1659
1660         DBG("ipconfig %p identifier %s", ipconfig, identifier);
1661
1662         key = g_strdup_printf("%smethod", prefix);
1663         method = g_key_file_get_string(keyfile, identifier, key, NULL);
1664         if (method == NULL) {
1665                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1666                         ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
1667                 else
1668                         ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
1669         } else
1670                 ipconfig->method = __connman_ipconfig_string2method(method);
1671         g_free(key);
1672
1673         key = g_strdup_printf("%snetmask_prefixlen", prefix);
1674         ipconfig->address->prefixlen = g_key_file_get_integer(
1675                                 keyfile, identifier, key, NULL);
1676         g_free(key);
1677
1678         key = g_strdup_printf("%slocal_address", prefix);
1679         ipconfig->address->local = g_key_file_get_string(
1680                         keyfile, identifier, key, NULL);
1681         g_free(key);
1682
1683         key = g_strdup_printf("%speer_address", prefix);
1684         ipconfig->address->peer = g_key_file_get_string(
1685                                 keyfile, identifier, key, NULL);
1686         g_free(key);
1687
1688         key = g_strdup_printf("%sbroadcast_address", prefix);
1689         ipconfig->address->broadcast = g_key_file_get_string(
1690                                 keyfile, identifier, key, NULL);
1691         g_free(key);
1692
1693         key = g_strdup_printf("%sgateway", prefix);
1694         ipconfig->address->gateway = g_key_file_get_string(
1695                                 keyfile, identifier, key, NULL);
1696         g_free(key);
1697
1698         return 0;
1699 }
1700
1701 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
1702                 GKeyFile *keyfile, const char *identifier, const char *prefix)
1703 {
1704         const char *method;
1705         char *key;
1706
1707         DBG("ipconfig %p identifier %s", ipconfig, identifier);
1708
1709         method = __connman_ipconfig_method2string(ipconfig->method);
1710
1711         key = g_strdup_printf("%smethod", prefix);
1712         g_key_file_set_string(keyfile, identifier, key, method);
1713         g_free(key);
1714
1715         key = g_strdup_printf("%snetmask_prefixlen", prefix);
1716         g_key_file_set_integer(keyfile, identifier,
1717                         key, ipconfig->address->prefixlen);
1718         g_free(key);
1719
1720         key = g_strdup_printf("%slocal_address", prefix);
1721         if (ipconfig->address->local != NULL)
1722                 g_key_file_set_string(keyfile, identifier,
1723                                 key, ipconfig->address->local);
1724         g_free(key);
1725
1726         key = g_strdup_printf("%speer_address", prefix);
1727         if (ipconfig->address->peer != NULL)
1728                 g_key_file_set_string(keyfile, identifier,
1729                                 key, ipconfig->address->peer);
1730         g_free(key);
1731
1732         key = g_strdup_printf("%sbroadcast_address", prefix);
1733         if (ipconfig->address->broadcast != NULL)
1734                 g_key_file_set_string(keyfile, identifier,
1735                         key, ipconfig->address->broadcast);
1736         g_free(key);
1737
1738         key = g_strdup_printf("%sgateway", prefix);
1739         if (ipconfig->address->gateway != NULL)
1740                 g_key_file_set_string(keyfile, identifier,
1741                         key, ipconfig->address->gateway);
1742         g_free(key);
1743
1744         return 0;
1745 }
1746
1747 int __connman_ipconfig_init(void)
1748 {
1749         DBG("");
1750
1751         ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1752                                                         NULL, free_ipdevice);
1753
1754         return 0;
1755 }
1756
1757 void __connman_ipconfig_cleanup(void)
1758 {
1759         DBG("");
1760
1761         g_hash_table_destroy(ipdevice_hash);
1762         ipdevice_hash = NULL;
1763 }