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