Don't filter out interfaces within IP configuration
[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
30 #ifndef IFF_LOWER_UP
31 #define IFF_LOWER_UP    0x10000
32 #endif
33
34 #include <gdbus.h>
35
36 #include "connman.h"
37
38 struct connman_ipconfig {
39         gint refcount;
40         int index;
41
42         struct connman_ipconfig *origin;
43
44         const struct connman_ipconfig_ops *ops;
45         void *ops_data;
46
47         enum connman_ipconfig_method method;
48         struct connman_ipaddress *address;
49         struct connman_ipaddress *system;
50 };
51
52 struct connman_ipdevice {
53         int index;
54         char *ifname;
55         unsigned short type;
56         unsigned int flags;
57         char *address;
58         uint16_t mtu;
59         uint32_t tx_bytes;
60         uint32_t rx_bytes;
61
62         GSList *address_list;
63         char *gateway;
64
65         struct connman_ipconfig *config;
66
67         struct connman_ipconfig_driver *driver;
68         struct connman_ipconfig *driver_config;
69 };
70
71 static GHashTable *ipdevice_hash = NULL;
72 static GList *ipconfig_list = NULL;
73
74 struct connman_ipaddress *connman_ipaddress_alloc(void)
75 {
76         struct connman_ipaddress *ipaddress;
77
78         ipaddress = g_try_new0(struct connman_ipaddress, 1);
79         if (ipaddress == NULL)
80                 return NULL;
81
82         return ipaddress;
83 }
84
85 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
86 {
87         if (ipaddress == NULL)
88                 return;
89
90         g_free(ipaddress->broadcast);
91         g_free(ipaddress->peer);
92         g_free(ipaddress->local);
93         g_free(ipaddress->gateway);
94         g_free(ipaddress);
95 }
96
97 static unsigned char netmask2prefixlen(const char *netmask)
98 {
99         unsigned char bits = 0;
100         in_addr_t mask = inet_network(netmask);
101         in_addr_t host = ~mask;
102
103         /* a valid netmask must be 2^n - 1 */
104         if ((host & (host + 1)) != 0)
105                 return -1;
106
107         for (; mask; mask <<= 1)
108                 ++bits;
109
110         return bits;
111 }
112
113 void connman_ipaddress_set(struct connman_ipaddress *ipaddress,
114                 const char *address, const char *netmask, const char *gateway)
115 {
116         if (ipaddress == NULL)
117                 return;
118
119         if (netmask != NULL)
120                 ipaddress->prefixlen = netmask2prefixlen(netmask);
121         else
122                 ipaddress->prefixlen = 32;
123
124         g_free(ipaddress->local);
125         ipaddress->local = g_strdup(address);
126
127         g_free(ipaddress->gateway);
128         ipaddress->gateway = g_strdup(gateway);
129 }
130
131 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
132 {
133         if (ipaddress == NULL)
134                 return;
135
136         ipaddress->prefixlen = 0;
137
138         g_free(ipaddress->local);
139         ipaddress->local = NULL;
140
141         g_free(ipaddress->peer);
142         ipaddress->peer = NULL;
143
144         g_free(ipaddress->broadcast);
145         ipaddress->broadcast = NULL;
146
147         g_free(ipaddress->gateway);
148         ipaddress->gateway = NULL;
149 }
150
151 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
152                                         struct connman_ipaddress *source)
153 {
154         if (ipaddress == NULL || source == NULL)
155                 return;
156
157         ipaddress->prefixlen = source->prefixlen;
158
159         g_free(ipaddress->local);
160         ipaddress->local = g_strdup(source->local);
161
162         g_free(ipaddress->peer);
163         ipaddress->peer = g_strdup(source->peer);
164
165         g_free(ipaddress->broadcast);
166         ipaddress->broadcast = g_strdup(source->broadcast);
167
168         g_free(ipaddress->gateway);
169         ipaddress->gateway = g_strdup(source->gateway);
170 }
171
172 static void free_address_list(struct connman_ipdevice *ipdevice)
173 {
174         GSList *list;
175
176         for (list = ipdevice->address_list; list; list = list->next) {
177                 struct connman_ipaddress *ipaddress = list->data;
178
179                 connman_ipaddress_free(ipaddress);
180                 list->data = NULL;
181         }
182
183         g_slist_free(ipdevice->address_list);
184         ipdevice->address_list = NULL;
185 }
186
187 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
188                                 unsigned char prefixlen, const char *local)
189 {
190         GSList *list;
191
192         for (list = ipdevice->address_list; list; list = list->next) {
193                 struct connman_ipaddress *ipaddress = list->data;
194
195                 if (g_strcmp0(ipaddress->local, local) == 0 &&
196                                         ipaddress->prefixlen == prefixlen)
197                         return ipaddress;
198         }
199
200         return NULL;
201 }
202
203 static const char *type2str(unsigned short type)
204 {
205         switch (type) {
206         case ARPHRD_ETHER:
207                 return "ETHER";
208         case ARPHRD_LOOPBACK:
209                 return "LOOPBACK";
210         case ARPHRD_PPP:
211                 return "PPP";
212         case ARPHRD_NONE:
213                 return "NONE";
214         case ARPHRD_VOID:
215                 return "VOID";
216         }
217
218         return "";
219 }
220
221 static const char *scope2str(unsigned char scope)
222 {
223         switch (scope) {
224         case 0:
225                 return "UNIVERSE";
226         case 253:
227                 return "LINK";
228         }
229
230         return "";
231 }
232
233 static void free_ipdevice(gpointer data)
234 {
235         struct connman_ipdevice *ipdevice = data;
236
237         connman_info("%s {remove} index %d", ipdevice->ifname,
238                                                         ipdevice->index);
239
240         if (ipdevice->config != NULL)
241                 connman_ipconfig_unref(ipdevice->config);
242
243         free_address_list(ipdevice);
244         g_free(ipdevice->gateway);
245
246         g_free(ipdevice->address);
247         g_free(ipdevice->ifname);
248         g_free(ipdevice);
249 }
250
251 static GSList *driver_list = NULL;
252
253 static gint compare_priority(gconstpointer a, gconstpointer b)
254 {
255         const struct connman_ipconfig_driver *driver1 = a;
256         const struct connman_ipconfig_driver *driver2 = b;
257
258         return driver2->priority - driver1->priority;
259 }
260
261 /**
262  * connman_ipconfig_driver_register:
263  * @driver: IP configuration driver
264  *
265  * Register a new IP configuration driver
266  *
267  * Returns: %0 on success
268  */
269 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
270 {
271         DBG("driver %p name %s", driver, driver->name);
272
273         driver_list = g_slist_insert_sorted(driver_list, driver,
274                                                         compare_priority);
275
276         return 0;
277 }
278
279 /**
280  * connman_ipconfig_driver_unregister:
281  * @driver: IP configuration driver
282  *
283  * Remove a previously registered IP configuration driver.
284  */
285 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
286 {
287         DBG("driver %p name %s", driver, driver->name);
288
289         driver_list = g_slist_remove(driver_list, driver);
290 }
291
292 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
293 {
294         GSList *list;
295
296         DBG("ipconfig %p", ipdevice->config);
297
298         if (ipdevice->config == NULL)
299                 return;
300
301         switch (ipdevice->config->method) {
302         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
303         case CONNMAN_IPCONFIG_METHOD_OFF:
304         case CONNMAN_IPCONFIG_METHOD_FIXED:
305         case CONNMAN_IPCONFIG_METHOD_MANUAL:
306                 return;
307         case CONNMAN_IPCONFIG_METHOD_DHCP:
308                 break;
309         }
310
311         if (ipdevice->driver != NULL)
312                 return;
313
314         ipdevice->driver_config = connman_ipconfig_clone(ipdevice->config);
315         if (ipdevice->driver_config == NULL)
316                 return;
317
318         for (list = driver_list; list; list = list->next) {
319                 struct connman_ipconfig_driver *driver = list->data;
320
321                 if (driver->request(ipdevice->driver_config) == 0) {
322                         ipdevice->driver = driver;
323                         break;
324                 }
325         }
326
327         if (ipdevice->driver == NULL) {
328                 connman_ipconfig_unref(ipdevice->driver_config);
329                 ipdevice->driver_config = NULL;
330         }
331 }
332
333 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
334 {
335         DBG("ipconfig %p", ipdevice->config);
336
337         if (ipdevice->config == NULL)
338                 return;
339
340         if (ipdevice->driver == NULL)
341                 return;
342
343         ipdevice->driver->release(ipdevice->driver_config);
344
345         ipdevice->driver = NULL;
346
347         connman_ipconfig_unref(ipdevice->driver_config);
348         ipdevice->driver_config = NULL;
349
350         connman_inet_clear_address(ipdevice->index);
351 }
352
353 static void update_stats(struct connman_ipdevice *ipdevice,
354                                                 struct rtnl_link_stats *stats)
355 {
356         if (stats->rx_packets == 0 && stats->tx_packets == 0)
357                 return;
358
359         connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
360                                         stats->rx_packets, stats->rx_bytes);
361         connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
362                                         stats->tx_packets, stats->tx_bytes);
363
364         if (ipdevice->config == NULL)
365                 return;
366
367         ipdevice->rx_bytes = stats->rx_bytes;
368         ipdevice->tx_bytes = stats->tx_bytes;
369
370         __connman_counter_notify(ipdevice->ifname,
371                                 ipdevice->rx_bytes, ipdevice->tx_bytes);
372 }
373
374 void __connman_ipconfig_newlink(int index, unsigned short type,
375                                 unsigned int flags, const char *address,
376                                                         unsigned short mtu,
377                                                 struct rtnl_link_stats *stats)
378 {
379         struct connman_ipdevice *ipdevice;
380         GList *list;
381         GString *str;
382         gboolean up = FALSE, down = FALSE;
383         gboolean lower_up = FALSE, lower_down = FALSE;
384
385         DBG("index %d", index);
386
387         if (type == ARPHRD_LOOPBACK)
388                 return;
389
390         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
391         if (ipdevice != NULL)
392                 goto update;
393
394         ipdevice = g_try_new0(struct connman_ipdevice, 1);
395         if (ipdevice == NULL)
396                 return;
397
398         ipdevice->index = index;
399         ipdevice->ifname = connman_inet_ifname(index);
400         ipdevice->type = type;
401
402         ipdevice->address = g_strdup(address);
403
404         g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
405
406         connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
407                                                 index, type, type2str(type));
408
409 update:
410         ipdevice->mtu = mtu;
411
412         update_stats(ipdevice, stats);
413
414         if (flags == ipdevice->flags)
415                 return;
416
417         if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
418                 if (flags & IFF_UP)
419                         up = TRUE;
420                 else
421                         down = TRUE;
422         }
423
424         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
425                                 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
426                 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
427                                         (IFF_RUNNING | IFF_LOWER_UP))
428                         lower_up = TRUE;
429                 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
430                         lower_down = TRUE;
431         }
432
433         ipdevice->flags = flags;
434
435         str = g_string_new(NULL);
436         if (str == NULL)
437                 return;
438
439         if (flags & IFF_UP)
440                 g_string_append(str, "UP");
441         else
442                 g_string_append(str, "DOWN");
443
444         if (flags & IFF_RUNNING)
445                 g_string_append(str, ",RUNNING");
446
447         if (flags & IFF_LOWER_UP)
448                 g_string_append(str, ",LOWER_UP");
449
450         connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
451                                                         flags, str->str);
452
453         g_string_free(str, TRUE);
454
455         for (list = g_list_first(ipconfig_list); list;
456                                                 list = g_list_next(list)) {
457                 struct connman_ipconfig *ipconfig = list->data;
458
459                 if (index != ipconfig->index)
460                         continue;
461
462                 if (ipconfig->ops == NULL)
463                         continue;
464
465                 if (up == TRUE && ipconfig->ops->up)
466                         ipconfig->ops->up(ipconfig);
467                 if (lower_up == TRUE && ipconfig->ops->lower_up)
468                         ipconfig->ops->lower_up(ipconfig);
469
470                 if (lower_down == TRUE && ipconfig->ops->lower_down)
471                         ipconfig->ops->lower_down(ipconfig);
472                 if (down == TRUE && ipconfig->ops->down)
473                         ipconfig->ops->down(ipconfig);
474         }
475
476         if (lower_up)
477                 __connman_ipconfig_lower_up(ipdevice);
478         if (lower_down)
479                 __connman_ipconfig_lower_down(ipdevice);
480 }
481
482 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
483 {
484         struct connman_ipdevice *ipdevice;
485         GList *list;
486
487         DBG("index %d", index);
488
489         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
490         if (ipdevice == NULL)
491                 return;
492
493         update_stats(ipdevice, stats);
494
495         for (list = g_list_first(ipconfig_list); list;
496                                                 list = g_list_next(list)) {
497                 struct connman_ipconfig *ipconfig = list->data;
498
499                 if (index != ipconfig->index)
500                         continue;
501
502                 ipconfig->index = -1;
503
504                 if (ipconfig->ops == NULL)
505                         continue;
506
507                 if (ipconfig->ops->lower_down)
508                         ipconfig->ops->lower_down(ipconfig);
509                 if (ipconfig->ops->down)
510                         ipconfig->ops->down(ipconfig);
511         }
512
513         __connman_ipconfig_lower_down(ipdevice);
514
515         g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
516 }
517
518 void __connman_ipconfig_newaddr(int index, const char *label,
519                                 unsigned char prefixlen, const char *address)
520 {
521         struct connman_ipdevice *ipdevice;
522         struct connman_ipaddress *ipaddress;
523         GList *list;
524
525         DBG("index %d", index);
526
527         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
528         if (ipdevice == NULL)
529                 return;
530
531         ipaddress = connman_ipaddress_alloc();
532         if (ipaddress == NULL)
533                 return;
534
535         ipaddress->prefixlen = prefixlen;
536         ipaddress->local = g_strdup(address);
537
538         ipdevice->address_list = g_slist_append(ipdevice->address_list,
539                                                                 ipaddress);
540
541         connman_info("%s {add} address %s/%u label %s", ipdevice->ifname,
542                                                 address, prefixlen, label);
543
544         if (ipdevice->config != NULL)
545                 connman_ipaddress_copy(ipdevice->config->system, ipaddress);
546
547         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
548                 return;
549
550         if (g_slist_length(ipdevice->address_list) > 1)
551                 return;
552
553         for (list = g_list_first(ipconfig_list); list;
554                                                 list = g_list_next(list)) {
555                 struct connman_ipconfig *ipconfig = list->data;
556
557                 if (index != ipconfig->index)
558                         continue;
559
560                 if (ipconfig->ops == NULL)
561                         continue;
562
563                 if (ipconfig->ops->ip_bound)
564                         ipconfig->ops->ip_bound(ipconfig);
565         }
566 }
567
568 void __connman_ipconfig_deladdr(int index, const char *label,
569                                 unsigned char prefixlen, const char *address)
570 {
571         struct connman_ipdevice *ipdevice;
572         struct connman_ipaddress *ipaddress;
573         GList *list;
574
575         DBG("index %d", index);
576
577         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
578         if (ipdevice == NULL)
579                 return;
580
581         ipaddress = find_ipaddress(ipdevice, prefixlen, address);
582         if (ipaddress == NULL)
583                 return;
584
585         ipdevice->address_list = g_slist_remove(ipdevice->address_list,
586                                                                 ipaddress);
587
588         connman_ipaddress_free(ipaddress);
589
590         connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
591                                                 address, prefixlen, label);
592
593         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
594                 return;
595
596         if (g_slist_length(ipdevice->address_list) > 0)
597                 return;
598
599         for (list = g_list_first(ipconfig_list); list;
600                                                 list = g_list_next(list)) {
601                 struct connman_ipconfig *ipconfig = list->data;
602
603                 if (index != ipconfig->index)
604                         continue;
605
606                 if (ipconfig->ops == NULL)
607                         continue;
608
609                 if (ipconfig->ops->ip_release)
610                         ipconfig->ops->ip_release(ipconfig);
611         }
612 }
613
614 void __connman_ipconfig_newroute(int index, unsigned char scope,
615                                         const char *dst, const char *gateway)
616 {
617         struct connman_ipdevice *ipdevice;
618
619         DBG("index %d", index);
620
621         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
622         if (ipdevice == NULL)
623                 return;
624
625         if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
626                 GSList *list;
627
628                 g_free(ipdevice->gateway);
629                 ipdevice->gateway = g_strdup(gateway);
630
631                 if (ipdevice->config != NULL &&
632                                         ipdevice->config->system != NULL) {
633                         g_free(ipdevice->config->system->gateway);
634                         ipdevice->config->system->gateway = g_strdup(gateway);
635                 }
636
637                 for (list = ipdevice->address_list; list; list = list->next) {
638                         struct connman_ipaddress *ipaddress = list->data;
639
640                         g_free(ipaddress->gateway);
641                         ipaddress->gateway = g_strdup(gateway);
642                 }
643         }
644
645         connman_info("%s {add} route %s gw %s scope %u <%s>",
646                                         ipdevice->ifname, dst, gateway,
647                                                 scope, scope2str(scope));
648 }
649
650 void __connman_ipconfig_delroute(int index, unsigned char scope,
651                                         const char *dst, const char *gateway)
652 {
653         struct connman_ipdevice *ipdevice;
654
655         DBG("index %d", index);
656
657         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
658         if (ipdevice == NULL)
659                 return;
660
661         if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
662                 GSList *list;
663
664                 g_free(ipdevice->gateway);
665                 ipdevice->gateway = NULL;
666
667                 if (ipdevice->config != NULL &&
668                                         ipdevice->config->system != NULL) {
669                         g_free(ipdevice->config->system->gateway);
670                         ipdevice->config->system->gateway = NULL;
671                 }
672
673                 for (list = ipdevice->address_list; list; list = list->next) {
674                         struct connman_ipaddress *ipaddress = list->data;
675
676                         g_free(ipaddress->gateway);
677                         ipaddress->gateway = NULL;
678                 }
679         }
680
681         connman_info("%s {del} route %s gw %s scope %u <%s>",
682                                         ipdevice->ifname, dst, gateway,
683                                                 scope, scope2str(scope));
684 }
685
686 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
687                                                         void *user_data)
688 {
689         GList *list, *keys;
690
691         keys = g_hash_table_get_keys(ipdevice_hash);
692         if (keys == NULL)
693                 return;
694
695         for (list = g_list_first(keys); list; list = g_list_next(list)) {
696                 int index = GPOINTER_TO_INT(list->data);
697
698                 function(index, user_data);
699         }
700
701         g_list_free(keys);
702 }
703
704 unsigned short __connman_ipconfig_get_type(int index)
705 {
706         struct connman_ipdevice *ipdevice;
707
708         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
709         if (ipdevice == NULL)
710                 return ARPHRD_VOID;
711
712         return ipdevice->type;
713 }
714
715 unsigned int __connman_ipconfig_get_flags(int index)
716 {
717         struct connman_ipdevice *ipdevice;
718
719         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
720         if (ipdevice == NULL)
721                 return 0;
722
723         return ipdevice->flags;
724 }
725
726 const char *__connman_ipconfig_get_gateway(int index)
727 {
728         struct connman_ipdevice *ipdevice;
729
730         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
731         if (ipdevice == NULL)
732                 return NULL;
733
734         return ipdevice->gateway;
735 }
736
737 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
738 {
739         ipconfig->index = index;
740 }
741
742 /**
743  * connman_ipconfig_create:
744  *
745  * Allocate a new ipconfig structure.
746  *
747  * Returns: a newly-allocated #connman_ipconfig structure
748  */
749 struct connman_ipconfig *connman_ipconfig_create(int index)
750 {
751         struct connman_ipconfig *ipconfig;
752
753         DBG("index %d", index);
754
755         ipconfig = g_try_new0(struct connman_ipconfig, 1);
756         if (ipconfig == NULL)
757                 return NULL;
758
759         ipconfig->refcount = 1;
760
761         ipconfig->index = index;
762
763         ipconfig->address = connman_ipaddress_alloc();
764         if (ipconfig->address == NULL) {
765                 g_free(ipconfig);
766                 return NULL;
767         }
768
769         ipconfig->system = connman_ipaddress_alloc();
770
771         DBG("ipconfig %p", ipconfig);
772
773         return ipconfig;
774 }
775
776 /**
777  * connman_ipconfig_clone:
778  *
779  * Clone an ipconfig structure and create new reference.
780  *
781  * Returns: a newly-allocated #connman_ipconfig structure
782  */
783 struct connman_ipconfig *connman_ipconfig_clone(struct connman_ipconfig *ipconfig)
784 {
785         struct connman_ipconfig *ipconfig_clone;
786
787         DBG("ipconfig %p", ipconfig);
788
789         ipconfig_clone = g_try_new0(struct connman_ipconfig, 1);
790         if (ipconfig_clone == NULL)
791                 return NULL;
792
793         ipconfig_clone->refcount = 1;
794
795         ipconfig_clone->origin = connman_ipconfig_ref(ipconfig);
796
797         ipconfig_clone->index = -1;
798
799         return ipconfig_clone;
800 }
801
802 /**
803  * connman_ipconfig_ref:
804  * @ipconfig: ipconfig structure
805  *
806  * Increase reference counter of ipconfig
807  */
808 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
809 {
810         g_atomic_int_inc(&ipconfig->refcount);
811
812         return ipconfig;
813 }
814
815 /**
816  * connman_ipconfig_unref:
817  * @ipconfig: ipconfig structure
818  *
819  * Decrease reference counter of ipconfig
820  */
821 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
822 {
823         if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
824                 __connman_ipconfig_disable(ipconfig);
825
826                 connman_ipconfig_set_ops(ipconfig, NULL);
827
828                 if (ipconfig->origin != NULL) {
829                         connman_ipconfig_unref(ipconfig->origin);
830                         ipconfig->origin = NULL;
831                 }
832
833                 connman_ipaddress_free(ipconfig->system);
834                 connman_ipaddress_free(ipconfig->address);
835                 g_free(ipconfig);
836         }
837 }
838
839 /**
840  * connman_ipconfig_get_data:
841  * @ipconfig: ipconfig structure
842  *
843  * Get private data pointer
844  */
845 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
846 {
847         return ipconfig->ops_data;
848 }
849
850 /**
851  * connman_ipconfig_set_data:
852  * @ipconfig: ipconfig structure
853  * @data: data pointer
854  *
855  * Set private data pointer
856  */
857 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
858 {
859         ipconfig->ops_data = data;
860 }
861
862 /**
863  * connman_ipconfig_get_index:
864  * @ipconfig: ipconfig structure
865  *
866  * Get interface index
867  */
868 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
869 {
870         if (ipconfig == NULL)
871                 return -1;
872
873         if (ipconfig->origin != NULL)
874                 return ipconfig->origin->index;
875
876         return ipconfig->index;
877 }
878
879 /**
880  * connman_ipconfig_get_ifname:
881  * @ipconfig: ipconfig structure
882  *
883  * Get interface name
884  */
885 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
886 {
887         struct connman_ipdevice *ipdevice;
888
889         if (ipconfig == NULL)
890                 return NULL;
891
892         if (ipconfig->index < 0)
893                 return NULL;
894
895         ipdevice = g_hash_table_lookup(ipdevice_hash,
896                                         GINT_TO_POINTER(ipconfig->index));
897         if (ipdevice == NULL)
898                 return NULL;
899
900         return ipdevice->ifname;
901 }
902
903 /**
904  * connman_ipconfig_set_ops:
905  * @ipconfig: ipconfig structure
906  * @ops: operation callbacks
907  *
908  * Set the operation callbacks
909  */
910 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
911                                 const struct connman_ipconfig_ops *ops)
912 {
913         ipconfig->ops = ops;
914 }
915
916 /**
917  * connman_ipconfig_set_method:
918  * @ipconfig: ipconfig structure
919  * @method: configuration method
920  *
921  * Set the configuration method
922  */
923 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
924                                         enum connman_ipconfig_method method)
925 {
926         ipconfig->method = method;
927
928         return 0;
929 }
930
931 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
932 {
933         if (ipconfig == NULL)
934                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
935
936         return ipconfig->method;
937 }
938
939 /**
940  * connman_ipconfig_bind:
941  * @ipconfig: ipconfig structure
942  * @ipaddress: ipaddress structure
943  *
944  * Bind IP address details to configuration
945  */
946 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
947                                         struct connman_ipaddress *ipaddress)
948 {
949         struct connman_ipconfig *origin;
950
951         origin = ipconfig->origin ? ipconfig->origin : ipconfig;
952
953         connman_ipaddress_copy(origin->address, ipaddress);
954
955         connman_inet_set_address(origin->index, origin->address);
956 }
957
958 /* FIXME: The element soulution should be removed in the future */
959 int __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig,
960                                                 struct connman_element *parent)
961 {
962         struct connman_element *connection;
963
964         connection = connman_element_create(NULL);
965
966         connection->type  = CONNMAN_ELEMENT_TYPE_CONNECTION;
967         connection->index = ipconfig->index;
968         connection->ipv4.gateway = ipconfig->address->gateway;
969
970         if (connman_element_register(connection, parent) < 0)
971                 connman_element_unref(connection);
972
973         return 0;
974 }
975
976 int __connman_ipconfig_set_address(struct connman_ipconfig *ipconfig)
977 {
978         DBG("");
979
980         switch (ipconfig->method) {
981         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
982         case CONNMAN_IPCONFIG_METHOD_OFF:
983         case CONNMAN_IPCONFIG_METHOD_FIXED:
984         case CONNMAN_IPCONFIG_METHOD_DHCP:
985                 break;
986         case CONNMAN_IPCONFIG_METHOD_MANUAL:
987                 return connman_inet_set_address(ipconfig->index,
988                                                 ipconfig->address);
989         }
990
991         return 0;
992 }
993
994 int __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
995 {
996         DBG("");
997
998         if (ipconfig == NULL)
999                 return 0;
1000
1001         DBG("method %d", ipconfig->method);
1002
1003         switch (ipconfig->method) {
1004         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1005         case CONNMAN_IPCONFIG_METHOD_OFF:
1006         case CONNMAN_IPCONFIG_METHOD_FIXED:
1007         case CONNMAN_IPCONFIG_METHOD_DHCP:
1008                 break;
1009         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1010                 return connman_inet_clear_address(ipconfig->index);
1011         }
1012
1013         return 0;
1014
1015 }
1016
1017 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1018 {
1019         struct connman_ipdevice *ipdevice;
1020
1021         DBG("ipconfig %p", ipconfig);
1022
1023         if (ipconfig == NULL || ipconfig->index < 0)
1024                 return -ENODEV;
1025
1026         ipdevice = g_hash_table_lookup(ipdevice_hash,
1027                                         GINT_TO_POINTER(ipconfig->index));
1028         if (ipdevice == NULL)
1029                 return -ENXIO;
1030
1031         if (ipdevice->config == ipconfig)
1032                 return -EALREADY;
1033
1034         if (ipdevice->config != NULL) {
1035                 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1036
1037                 connman_ipaddress_clear(ipdevice->config->system);
1038
1039                 connman_ipconfig_unref(ipdevice->config);
1040         }
1041
1042         ipdevice->config = connman_ipconfig_ref(ipconfig);
1043
1044         ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1045
1046         return 0;
1047 }
1048
1049 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1050 {
1051         struct connman_ipdevice *ipdevice;
1052
1053         DBG("ipconfig %p", ipconfig);
1054
1055         if (ipconfig == NULL || ipconfig->index < 0)
1056                 return -ENODEV;
1057
1058         ipdevice = g_hash_table_lookup(ipdevice_hash,
1059                                         GINT_TO_POINTER(ipconfig->index));
1060         if (ipdevice == NULL)
1061                 return -ENXIO;
1062
1063         if (ipdevice->config == NULL || ipdevice->config != ipconfig)
1064                 return -EINVAL;
1065
1066         ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1067
1068         connman_ipaddress_clear(ipdevice->config->system);
1069
1070         connman_ipconfig_unref(ipdevice->config);
1071         ipdevice->config = NULL;
1072
1073         return 0;
1074 }
1075
1076 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1077 {
1078         switch (method) {
1079         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1080                 break;
1081         case CONNMAN_IPCONFIG_METHOD_OFF:
1082                 return "off";
1083         case CONNMAN_IPCONFIG_METHOD_FIXED:
1084                 return "fixed";
1085         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1086                 return "manual";
1087         case CONNMAN_IPCONFIG_METHOD_DHCP:
1088                 return "dhcp";
1089         }
1090
1091         return NULL;
1092 }
1093
1094 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1095 {
1096         if (g_strcmp0(method, "off") == 0)
1097                 return CONNMAN_IPCONFIG_METHOD_OFF;
1098         else if (g_strcmp0(method, "fixed") == 0)
1099                 return CONNMAN_IPCONFIG_METHOD_FIXED;
1100         else if (g_strcmp0(method, "manual") == 0)
1101                 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1102         else if (g_strcmp0(method, "dhcp") == 0)
1103                 return CONNMAN_IPCONFIG_METHOD_DHCP;
1104         else
1105                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1106 }
1107
1108 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1109                                                         DBusMessageIter *iter)
1110 {
1111         const char *str;
1112
1113         str = __connman_ipconfig_method2string(ipconfig->method);
1114         if (str == NULL)
1115                 return;
1116
1117         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1118
1119         if (ipconfig->system == NULL)
1120                 return;
1121
1122         if (ipconfig->system->local != NULL) {
1123                 in_addr_t addr;
1124                 struct in_addr netmask;
1125                 char *mask;
1126
1127                 connman_dbus_dict_append_basic(iter, "Address",
1128                                 DBUS_TYPE_STRING, &ipconfig->system->local);
1129
1130                 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1131                 netmask.s_addr = htonl(addr);
1132                 mask = inet_ntoa(netmask);
1133                 connman_dbus_dict_append_basic(iter, "Netmask",
1134                                                 DBUS_TYPE_STRING, &mask);
1135         }
1136
1137         if (ipconfig->system->gateway != NULL)
1138                 connman_dbus_dict_append_basic(iter, "Gateway",
1139                                 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1140 }
1141
1142 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1143                                                         DBusMessageIter *iter)
1144 {
1145         const char *str;
1146
1147         str = __connman_ipconfig_method2string(ipconfig->method);
1148         if (str == NULL)
1149                 return;
1150
1151         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1152
1153         switch (ipconfig->method) {
1154         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1155         case CONNMAN_IPCONFIG_METHOD_OFF:
1156         case CONNMAN_IPCONFIG_METHOD_FIXED:
1157         case CONNMAN_IPCONFIG_METHOD_DHCP:
1158                 return;
1159         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1160                 break;
1161         }
1162
1163         if (ipconfig->address == NULL)
1164                 return;
1165
1166         if (ipconfig->address->local != NULL) {
1167                 in_addr_t addr;
1168                 struct in_addr netmask;
1169                 char *mask;
1170
1171                 connman_dbus_dict_append_basic(iter, "Address",
1172                                 DBUS_TYPE_STRING, &ipconfig->address->local);
1173
1174                 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1175                 netmask.s_addr = htonl(addr);
1176                 mask = inet_ntoa(netmask);
1177                 connman_dbus_dict_append_basic(iter, "Netmask",
1178                                                 DBUS_TYPE_STRING, &mask);
1179         }
1180
1181         if (ipconfig->address->gateway != NULL)
1182                 connman_dbus_dict_append_basic(iter, "Gateway",
1183                                 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1184 }
1185
1186 int __connman_ipconfig_set_ipv4config(struct connman_ipconfig *ipconfig,
1187                                                         DBusMessageIter *array)
1188 {
1189         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1190         const char *address = NULL, *netmask = NULL, *gateway = NULL;
1191         DBusMessageIter dict;
1192
1193         DBG("ipconfig %p", ipconfig);
1194
1195         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1196                 return -EINVAL;
1197
1198         dbus_message_iter_recurse(array, &dict);
1199
1200         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1201                 DBusMessageIter entry;
1202                 const char *key;
1203                 int type;
1204
1205                 dbus_message_iter_recurse(&dict, &entry);
1206
1207                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1208                         return -EINVAL;
1209
1210                 dbus_message_iter_get_basic(&entry, &key);
1211                 dbus_message_iter_next(&entry);
1212
1213                 type = dbus_message_iter_get_arg_type(&entry);
1214
1215                 if (g_str_equal(key, "Method") == TRUE) {
1216                         const char *str;
1217
1218                         if (type != DBUS_TYPE_STRING)
1219                                 return -EINVAL;
1220
1221                         dbus_message_iter_get_basic(&entry, &str);
1222                         method = __connman_ipconfig_string2method(str);
1223                 } else if (g_str_equal(key, "Address") == TRUE) {
1224                         if (type != DBUS_TYPE_STRING)
1225                                 return -EINVAL;
1226
1227                         dbus_message_iter_get_basic(&entry, &address);
1228                 } else if (g_str_equal(key, "Netmask") == TRUE) {
1229                         if (type != DBUS_TYPE_STRING)
1230                                 return -EINVAL;
1231
1232                         dbus_message_iter_get_basic(&entry, &netmask);
1233                 } else if (g_str_equal(key, "Gateway") == TRUE) {
1234                         if (type != DBUS_TYPE_STRING)
1235                                 return -EINVAL;
1236
1237                         dbus_message_iter_get_basic(&entry, &gateway);
1238                 }
1239                 dbus_message_iter_next(&dict);
1240         }
1241
1242         DBG("method %d address %s netmask %s gateway %s",
1243                                 method, address, netmask, gateway);
1244
1245         switch (method) {
1246         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1247         case CONNMAN_IPCONFIG_METHOD_OFF:
1248         case CONNMAN_IPCONFIG_METHOD_FIXED:
1249                 return -EINVAL;
1250
1251         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1252                 if (address == NULL)
1253                         return -EINVAL;
1254
1255                 ipconfig->method = method;
1256                 connman_ipaddress_set(ipconfig->address,
1257                                 address, netmask, gateway);
1258                 break;
1259
1260         case CONNMAN_IPCONFIG_METHOD_DHCP:
1261                 if (ipconfig->method == method)
1262                         return 0;
1263
1264                 ipconfig->method = method;
1265                 break;
1266         }
1267
1268         return 0;
1269 }
1270
1271 void __connman_ipconfig_append_proxy(struct connman_ipconfig *ipconfig,
1272                                                         DBusMessageIter *iter)
1273 {
1274         const char *method = "direct";
1275
1276         connman_dbus_dict_append_basic(iter, "Method",
1277                                                 DBUS_TYPE_STRING, &method);
1278 }
1279
1280 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1281                                                         DBusMessageIter *iter)
1282 {
1283         struct connman_ipdevice *ipdevice;
1284         const char *method = "auto";
1285
1286         connman_dbus_dict_append_basic(iter, "Method",
1287                                                 DBUS_TYPE_STRING, &method);
1288
1289         ipdevice = g_hash_table_lookup(ipdevice_hash,
1290                                         GINT_TO_POINTER(ipconfig->index));
1291         if (ipdevice == NULL)
1292                 return;
1293
1294         if (ipdevice->ifname != NULL)
1295                 connman_dbus_dict_append_basic(iter, "Interface",
1296                                         DBUS_TYPE_STRING, &ipdevice->ifname);
1297
1298         if (ipdevice->address != NULL)
1299                 connman_dbus_dict_append_basic(iter, "Address",
1300                                         DBUS_TYPE_STRING, &ipdevice->address);
1301
1302         if (ipdevice->mtu > 0)
1303                 connman_dbus_dict_append_basic(iter, "MTU",
1304                                         DBUS_TYPE_UINT16, &ipdevice->mtu);
1305 }
1306
1307 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1308                 GKeyFile *keyfile, const char *identifier, const char *prefix)
1309 {
1310         const char *method;
1311         char *key;
1312
1313         DBG("ipconfig %p identifier %s", ipconfig, identifier);
1314
1315         key = g_strdup_printf("%smethod", prefix);
1316         method = g_key_file_get_string(keyfile, identifier, key, NULL);
1317         if (method == NULL)
1318                 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
1319         else
1320                 ipconfig->method = __connman_ipconfig_string2method(method);
1321         g_free(key);
1322
1323         key = g_strdup_printf("%snetmask_prefixlen", prefix);
1324         ipconfig->address->prefixlen = g_key_file_get_integer(
1325                                 keyfile, identifier, key, NULL);
1326         g_free(key);
1327
1328         key = g_strdup_printf("%slocal_address", prefix);
1329         ipconfig->address->local = g_key_file_get_string(
1330                         keyfile, identifier, key, NULL);
1331         g_free(key);
1332
1333         key = g_strdup_printf("%speer_address", prefix);
1334         ipconfig->address->peer = g_key_file_get_string(
1335                                 keyfile, identifier, key, NULL);
1336         g_free(key);
1337
1338         key = g_strdup_printf("%sbroadcast_address", prefix);
1339         ipconfig->address->broadcast = g_key_file_get_string(
1340                                 keyfile, identifier, key, NULL);
1341         g_free(key);
1342
1343         key = g_strdup_printf("%sgateway", prefix);
1344         ipconfig->address->gateway = g_key_file_get_string(
1345                                 keyfile, identifier, key, NULL);
1346         g_free(key);
1347
1348         return 0;
1349 }
1350
1351 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
1352                 GKeyFile *keyfile, const char *identifier, const char *prefix)
1353 {
1354         const char *method;
1355         char *key;
1356
1357         DBG("ipconfig %p identifier %s", ipconfig, identifier);
1358
1359         method = __connman_ipconfig_method2string(ipconfig->method);
1360
1361         key = g_strdup_printf("%smethod", prefix);
1362         g_key_file_set_string(keyfile, identifier, key, method);
1363         g_free(key);
1364
1365         key = g_strdup_printf("%snetmask_prefixlen", prefix);
1366         g_key_file_set_integer(keyfile, identifier,
1367                         key, ipconfig->address->prefixlen);
1368         g_free(key);
1369
1370         key = g_strdup_printf("%slocal_address", prefix);
1371         if (ipconfig->address->local != NULL)
1372                 g_key_file_set_string(keyfile, identifier,
1373                                 key, ipconfig->address->local);
1374         g_free(key);
1375
1376         key = g_strdup_printf("%speer_address", prefix);
1377         if (ipconfig->address->peer != NULL)
1378                 g_key_file_set_string(keyfile, identifier,
1379                                 key, ipconfig->address->peer);
1380         g_free(key);
1381
1382         key = g_strdup_printf("%sbroadcast_address", prefix);
1383         if (ipconfig->address->broadcast != NULL)
1384                 g_key_file_set_string(keyfile, identifier,
1385                         key, ipconfig->address->broadcast);
1386         g_free(key);
1387
1388         key = g_strdup_printf("%sgateway", prefix);
1389         if (ipconfig->address->gateway != NULL)
1390                 g_key_file_set_string(keyfile, identifier,
1391                         key, ipconfig->address->gateway);
1392         g_free(key);
1393
1394         return 0;
1395 }
1396
1397 int __connman_ipconfig_init(void)
1398 {
1399         DBG("");
1400
1401         ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1402                                                         NULL, free_ipdevice);
1403
1404         return 0;
1405 }
1406
1407 void __connman_ipconfig_cleanup(void)
1408 {
1409         DBG("");
1410
1411         g_hash_table_destroy(ipdevice_hash);
1412         ipdevice_hash = NULL;
1413 }