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