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