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