Export IPv4 gateway properly information if available
[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 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         char *ifname;
385
386         DBG("index %d", index);
387
388         if (type == ARPHRD_LOOPBACK)
389                 return;
390
391         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
392         if (ipdevice != NULL)
393                 goto update;
394
395         ifname = connman_inet_ifname(index);
396
397         if (__connman_element_device_isfiltered(ifname) == TRUE) {
398                 connman_info("Ignoring interface %s (filtered)", ifname);
399                 g_free(ifname);
400                 return;
401         }
402
403         ipdevice = g_try_new0(struct connman_ipdevice, 1);
404         if (ipdevice == NULL) {
405                 g_free(ifname);
406                 return;
407         }
408
409         ipdevice->index = index;
410         ipdevice->ifname = ifname;
411         ipdevice->type = type;
412
413         ipdevice->address = g_strdup(address);
414
415         g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
416
417         connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
418                                                 index, type, type2str(type));
419
420 update:
421         ipdevice->mtu = mtu;
422
423         update_stats(ipdevice, stats);
424
425         if (flags == ipdevice->flags)
426                 return;
427
428         if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
429                 if (flags & IFF_UP)
430                         up = TRUE;
431                 else
432                         down = TRUE;
433         }
434
435         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
436                                 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
437                 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
438                                         (IFF_RUNNING | IFF_LOWER_UP))
439                         lower_up = TRUE;
440                 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
441                         lower_down = TRUE;
442         }
443
444         ipdevice->flags = flags;
445
446         str = g_string_new(NULL);
447         if (str == NULL)
448                 return;
449
450         if (flags & IFF_UP)
451                 g_string_append(str, "UP");
452         else
453                 g_string_append(str, "DOWN");
454
455         if (flags & IFF_RUNNING)
456                 g_string_append(str, ",RUNNING");
457
458         if (flags & IFF_LOWER_UP)
459                 g_string_append(str, ",LOWER_UP");
460
461         connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
462                                                         flags, str->str);
463
464         g_string_free(str, TRUE);
465
466         for (list = g_list_first(ipconfig_list); list;
467                                                 list = g_list_next(list)) {
468                 struct connman_ipconfig *ipconfig = list->data;
469
470                 if (index != ipconfig->index)
471                         continue;
472
473                 if (ipconfig->ops == NULL)
474                         continue;
475
476                 if (up == TRUE && ipconfig->ops->up)
477                         ipconfig->ops->up(ipconfig);
478                 if (lower_up == TRUE && ipconfig->ops->lower_up)
479                         ipconfig->ops->lower_up(ipconfig);
480
481                 if (lower_down == TRUE && ipconfig->ops->lower_down)
482                         ipconfig->ops->lower_down(ipconfig);
483                 if (down == TRUE && ipconfig->ops->down)
484                         ipconfig->ops->down(ipconfig);
485         }
486
487         if (lower_up)
488                 __connman_ipconfig_lower_up(ipdevice);
489         if (lower_down)
490                 __connman_ipconfig_lower_down(ipdevice);
491 }
492
493 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
494 {
495         struct connman_ipdevice *ipdevice;
496         GList *list;
497
498         DBG("index %d", index);
499
500         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
501         if (ipdevice == NULL)
502                 return;
503
504         update_stats(ipdevice, stats);
505
506         for (list = g_list_first(ipconfig_list); list;
507                                                 list = g_list_next(list)) {
508                 struct connman_ipconfig *ipconfig = list->data;
509
510                 if (index != ipconfig->index)
511                         continue;
512
513                 ipconfig->index = -1;
514
515                 if (ipconfig->ops == NULL)
516                         continue;
517
518                 if (ipconfig->ops->lower_down)
519                         ipconfig->ops->lower_down(ipconfig);
520                 if (ipconfig->ops->down)
521                         ipconfig->ops->down(ipconfig);
522         }
523
524         __connman_ipconfig_lower_down(ipdevice);
525
526         g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
527 }
528
529 void __connman_ipconfig_newaddr(int index, const char *label,
530                                 unsigned char prefixlen, const char *address)
531 {
532         struct connman_ipdevice *ipdevice;
533         struct connman_ipaddress *ipaddress;
534         GList *list;
535
536         DBG("index %d", index);
537
538         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
539         if (ipdevice == NULL)
540                 return;
541
542         ipaddress = connman_ipaddress_alloc();
543         if (ipaddress == NULL)
544                 return;
545
546         ipaddress->prefixlen = prefixlen;
547         ipaddress->local = g_strdup(address);
548
549         ipdevice->address_list = g_slist_append(ipdevice->address_list,
550                                                                 ipaddress);
551
552         connman_info("%s {add} address %s/%u label %s", ipdevice->ifname,
553                                                 address, prefixlen, label);
554
555         if (ipdevice->config != NULL)
556                 connman_ipaddress_copy(ipdevice->config->system, ipaddress);
557
558         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
559                 return;
560
561         if (g_slist_length(ipdevice->address_list) > 1)
562                 return;
563
564         for (list = g_list_first(ipconfig_list); list;
565                                                 list = g_list_next(list)) {
566                 struct connman_ipconfig *ipconfig = list->data;
567
568                 if (index != ipconfig->index)
569                         continue;
570
571                 if (ipconfig->ops == NULL)
572                         continue;
573
574                 if (ipconfig->ops->ip_bound)
575                         ipconfig->ops->ip_bound(ipconfig);
576         }
577 }
578
579 void __connman_ipconfig_deladdr(int index, const char *label,
580                                 unsigned char prefixlen, const char *address)
581 {
582         struct connman_ipdevice *ipdevice;
583         struct connman_ipaddress *ipaddress;
584         GList *list;
585
586         DBG("index %d", index);
587
588         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
589         if (ipdevice == NULL)
590                 return;
591
592         ipaddress = find_ipaddress(ipdevice, prefixlen, address);
593         if (ipaddress == NULL)
594                 return;
595
596         ipdevice->address_list = g_slist_remove(ipdevice->address_list,
597                                                                 ipaddress);
598
599         connman_ipaddress_free(ipaddress);
600
601         connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
602                                                 address, prefixlen, label);
603
604         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
605                 return;
606
607         if (g_slist_length(ipdevice->address_list) > 0)
608                 return;
609
610         for (list = g_list_first(ipconfig_list); list;
611                                                 list = g_list_next(list)) {
612                 struct connman_ipconfig *ipconfig = list->data;
613
614                 if (index != ipconfig->index)
615                         continue;
616
617                 if (ipconfig->ops == NULL)
618                         continue;
619
620                 if (ipconfig->ops->ip_release)
621                         ipconfig->ops->ip_release(ipconfig);
622         }
623 }
624
625 void __connman_ipconfig_newroute(int index, unsigned char scope,
626                                         const char *dst, const char *gateway)
627 {
628         struct connman_ipdevice *ipdevice;
629
630         DBG("index %d", index);
631
632         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
633         if (ipdevice == NULL)
634                 return;
635
636         if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
637                 g_free(ipdevice->gateway);
638                 ipdevice->gateway = g_strdup(gateway);
639         }
640
641         connman_info("%s {add} route %s gw %s scope %u <%s>",
642                                         ipdevice->ifname, dst, gateway,
643                                                 scope, scope2str(scope));
644 }
645
646 void __connman_ipconfig_delroute(int index, unsigned char scope,
647                                         const char *dst, const char *gateway)
648 {
649         struct connman_ipdevice *ipdevice;
650
651         DBG("index %d", index);
652
653         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
654         if (ipdevice == NULL)
655                 return;
656
657         if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
658                 g_free(ipdevice->gateway);
659                 ipdevice->gateway = NULL;
660         }
661
662         connman_info("%s {del} route %s gw %s scope %u <%s>",
663                                         ipdevice->ifname, dst, gateway,
664                                                 scope, scope2str(scope));
665 }
666
667 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
668                                                         void *user_data)
669 {
670         GList *list, *keys;
671
672         keys = g_hash_table_get_keys(ipdevice_hash);
673         if (keys == NULL)
674                 return;
675
676         for (list = g_list_first(keys); list; list = g_list_next(list)) {
677                 int index = GPOINTER_TO_INT(list->data);
678
679                 function(index, user_data);
680         }
681
682         g_list_free(keys);
683 }
684
685 unsigned short __connman_ipconfig_get_type(int index)
686 {
687         struct connman_ipdevice *ipdevice;
688
689         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
690         if (ipdevice == NULL)
691                 return ARPHRD_VOID;
692
693         return ipdevice->type;
694 }
695
696 unsigned int __connman_ipconfig_get_flags(int index)
697 {
698         struct connman_ipdevice *ipdevice;
699
700         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
701         if (ipdevice == NULL)
702                 return 0;
703
704         return ipdevice->flags;
705 }
706
707 const char *__connman_ipconfig_get_gateway(int index)
708 {
709         struct connman_ipdevice *ipdevice;
710
711         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
712         if (ipdevice == NULL)
713                 return NULL;
714
715         return ipdevice->gateway;
716 }
717
718 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
719 {
720         ipconfig->index = index;
721 }
722
723 /**
724  * connman_ipconfig_create:
725  *
726  * Allocate a new ipconfig structure.
727  *
728  * Returns: a newly-allocated #connman_ipconfig structure
729  */
730 struct connman_ipconfig *connman_ipconfig_create(int index)
731 {
732         struct connman_ipconfig *ipconfig;
733
734         DBG("index %d", index);
735
736         ipconfig = g_try_new0(struct connman_ipconfig, 1);
737         if (ipconfig == NULL)
738                 return NULL;
739
740         ipconfig->refcount = 1;
741
742         ipconfig->index = index;
743
744         ipconfig->address = connman_ipaddress_alloc();
745         if (ipconfig->address == NULL) {
746                 g_free(ipconfig);
747                 return NULL;
748         }
749
750         ipconfig->system = connman_ipaddress_alloc();
751
752         DBG("ipconfig %p", ipconfig);
753
754         return ipconfig;
755 }
756
757 /**
758  * connman_ipconfig_clone:
759  *
760  * Clone an ipconfig structure and create new reference.
761  *
762  * Returns: a newly-allocated #connman_ipconfig structure
763  */
764 struct connman_ipconfig *connman_ipconfig_clone(struct connman_ipconfig *ipconfig)
765 {
766         struct connman_ipconfig *ipconfig_clone;
767
768         DBG("ipconfig %p", ipconfig);
769
770         ipconfig_clone = g_try_new0(struct connman_ipconfig, 1);
771         if (ipconfig_clone == NULL)
772                 return NULL;
773
774         ipconfig_clone->refcount = 1;
775
776         ipconfig_clone->origin = connman_ipconfig_ref(ipconfig);
777
778         ipconfig_clone->index = -1;
779
780         return ipconfig_clone;
781 }
782
783 /**
784  * connman_ipconfig_ref:
785  * @ipconfig: ipconfig structure
786  *
787  * Increase reference counter of ipconfig
788  */
789 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
790 {
791         g_atomic_int_inc(&ipconfig->refcount);
792
793         return ipconfig;
794 }
795
796 /**
797  * connman_ipconfig_unref:
798  * @ipconfig: ipconfig structure
799  *
800  * Decrease reference counter of ipconfig
801  */
802 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
803 {
804         if (g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
805                 __connman_ipconfig_disable(ipconfig);
806
807                 connman_ipconfig_set_ops(ipconfig, NULL);
808
809                 if (ipconfig->origin != NULL) {
810                         connman_ipconfig_unref(ipconfig->origin);
811                         ipconfig->origin = NULL;
812                 }
813
814                 connman_ipaddress_free(ipconfig->system);
815                 connman_ipaddress_free(ipconfig->address);
816                 g_free(ipconfig);
817         }
818 }
819
820 /**
821  * connman_ipconfig_get_data:
822  * @ipconfig: ipconfig structure
823  *
824  * Get private data pointer
825  */
826 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
827 {
828         return ipconfig->ops_data;
829 }
830
831 /**
832  * connman_ipconfig_set_data:
833  * @ipconfig: ipconfig structure
834  * @data: data pointer
835  *
836  * Set private data pointer
837  */
838 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
839 {
840         ipconfig->ops_data = data;
841 }
842
843 /**
844  * connman_ipconfig_get_index:
845  * @ipconfig: ipconfig structure
846  *
847  * Get interface index
848  */
849 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
850 {
851         if (ipconfig == NULL)
852                 return -1;
853
854         if (ipconfig->origin != NULL)
855                 return ipconfig->origin->index;
856
857         return ipconfig->index;
858 }
859
860 /**
861  * connman_ipconfig_get_ifname:
862  * @ipconfig: ipconfig structure
863  *
864  * Get interface name
865  */
866 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
867 {
868         struct connman_ipdevice *ipdevice;
869
870         if (ipconfig == NULL)
871                 return NULL;
872
873         if (ipconfig->index < 0)
874                 return NULL;
875
876         ipdevice = g_hash_table_lookup(ipdevice_hash,
877                                         GINT_TO_POINTER(ipconfig->index));
878         if (ipdevice == NULL)
879                 return NULL;
880
881         return ipdevice->ifname;
882 }
883
884 /**
885  * connman_ipconfig_set_ops:
886  * @ipconfig: ipconfig structure
887  * @ops: operation callbacks
888  *
889  * Set the operation callbacks
890  */
891 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
892                                 const struct connman_ipconfig_ops *ops)
893 {
894         ipconfig->ops = ops;
895 }
896
897 /**
898  * connman_ipconfig_set_method:
899  * @ipconfig: ipconfig structure
900  * @method: configuration method
901  *
902  * Set the configuration method
903  */
904 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
905                                         enum connman_ipconfig_method method)
906 {
907         ipconfig->method = method;
908
909         return 0;
910 }
911
912 enum connman_ipconfig_method __connman_ipconfig_get_method(
913                                 struct connman_ipconfig *ipconfig)
914 {
915         if (ipconfig == NULL)
916                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
917
918         return ipconfig->method;
919 }
920
921 /**
922  * connman_ipconfig_bind:
923  * @ipconfig: ipconfig structure
924  * @ipaddress: ipaddress structure
925  *
926  * Bind IP address details to configuration
927  */
928 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
929                                         struct connman_ipaddress *ipaddress)
930 {
931         struct connman_ipconfig *origin;
932
933         origin = ipconfig->origin ? ipconfig->origin : ipconfig;
934
935         connman_ipaddress_copy(origin->address, ipaddress);
936
937         connman_inet_set_address(origin->index, origin->address);
938 }
939
940 /* FIXME: The element soulution should be removed in the future */
941 int __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig,
942                                                 struct connman_element *parent)
943 {
944         struct connman_element *connection;
945
946         connection = connman_element_create(NULL);
947
948         connection->type  = CONNMAN_ELEMENT_TYPE_CONNECTION;
949         connection->index = ipconfig->index;
950         connection->ipv4.gateway = ipconfig->address->gateway;
951
952         if (connman_element_register(connection, parent) < 0)
953                 connman_element_unref(connection);
954
955         return 0;
956 }
957
958 int __connman_ipconfig_set_address(struct connman_ipconfig *ipconfig)
959 {
960         DBG("");
961
962         switch (ipconfig->method) {
963         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
964         case CONNMAN_IPCONFIG_METHOD_OFF:
965         case CONNMAN_IPCONFIG_METHOD_FIXED:
966         case CONNMAN_IPCONFIG_METHOD_DHCP:
967                 break;
968         case CONNMAN_IPCONFIG_METHOD_MANUAL:
969                 return connman_inet_set_address(ipconfig->index,
970                                                 ipconfig->address);
971         }
972
973         return 0;
974 }
975
976 int __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
977 {
978         DBG("");
979
980         if (ipconfig == NULL)
981                 return 0;
982
983         DBG("method %d", ipconfig->method);
984
985         switch (ipconfig->method) {
986         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
987         case CONNMAN_IPCONFIG_METHOD_OFF:
988         case CONNMAN_IPCONFIG_METHOD_FIXED:
989         case CONNMAN_IPCONFIG_METHOD_DHCP:
990                 break;
991         case CONNMAN_IPCONFIG_METHOD_MANUAL:
992                 return connman_inet_clear_address(ipconfig->index);
993         }
994
995         return 0;
996
997 }
998
999 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1000 {
1001         struct connman_ipdevice *ipdevice;
1002
1003         DBG("ipconfig %p", ipconfig);
1004
1005         if (ipconfig == NULL || ipconfig->index < 0)
1006                 return -ENODEV;
1007
1008         ipdevice = g_hash_table_lookup(ipdevice_hash,
1009                                         GINT_TO_POINTER(ipconfig->index));
1010         if (ipdevice == NULL)
1011                 return -ENXIO;
1012
1013         if (ipdevice->config == ipconfig)
1014                 return -EALREADY;
1015
1016         if (ipdevice->config != NULL) {
1017                 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1018
1019                 connman_ipaddress_clear(ipdevice->config->system);
1020
1021                 connman_ipconfig_unref(ipdevice->config);
1022         }
1023
1024         ipdevice->config = connman_ipconfig_ref(ipconfig);
1025
1026         ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1027
1028         return 0;
1029 }
1030
1031 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1032 {
1033         struct connman_ipdevice *ipdevice;
1034
1035         DBG("ipconfig %p", ipconfig);
1036
1037         if (ipconfig == NULL || ipconfig->index < 0)
1038                 return -ENODEV;
1039
1040         ipdevice = g_hash_table_lookup(ipdevice_hash,
1041                                         GINT_TO_POINTER(ipconfig->index));
1042         if (ipdevice == NULL)
1043                 return -ENXIO;
1044
1045         if (ipdevice->config == NULL || ipdevice->config != ipconfig)
1046                 return -EINVAL;
1047
1048         ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1049
1050         connman_ipaddress_clear(ipdevice->config->system);
1051
1052         connman_ipconfig_unref(ipdevice->config);
1053         ipdevice->config = NULL;
1054
1055         return 0;
1056 }
1057
1058 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1059 {
1060         switch (method) {
1061         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1062                 break;
1063         case CONNMAN_IPCONFIG_METHOD_OFF:
1064                 return "off";
1065         case CONNMAN_IPCONFIG_METHOD_FIXED:
1066                 return "fixed";
1067         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1068                 return "manual";
1069         case CONNMAN_IPCONFIG_METHOD_DHCP:
1070                 return "dhcp";
1071         }
1072
1073         return NULL;
1074 }
1075
1076 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1077 {
1078         if (g_strcmp0(method, "off") == 0)
1079                 return CONNMAN_IPCONFIG_METHOD_OFF;
1080         else if (g_strcmp0(method, "fixed") == 0)
1081                 return CONNMAN_IPCONFIG_METHOD_FIXED;
1082         else if (g_strcmp0(method, "manual") == 0)
1083                 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1084         else if (g_strcmp0(method, "dhcp") == 0)
1085                 return CONNMAN_IPCONFIG_METHOD_DHCP;
1086         else
1087                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1088 }
1089
1090 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1091                                                         DBusMessageIter *iter)
1092 {
1093         const char *str;
1094
1095         str = __connman_ipconfig_method2string(ipconfig->method);
1096         if (str == NULL)
1097                 return;
1098
1099         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1100
1101         if (ipconfig->system == NULL)
1102                 return;
1103
1104         if (ipconfig->system->local != NULL) {
1105                 in_addr_t addr;
1106                 struct in_addr netmask;
1107                 char *mask;
1108
1109                 connman_dbus_dict_append_basic(iter, "Address",
1110                                 DBUS_TYPE_STRING, &ipconfig->system->local);
1111
1112                 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1113                 netmask.s_addr = htonl(addr);
1114                 mask = inet_ntoa(netmask);
1115                 connman_dbus_dict_append_basic(iter, "Netmask",
1116                                                 DBUS_TYPE_STRING, &mask);
1117         }
1118
1119         if (ipconfig->system->gateway != NULL)
1120                 connman_dbus_dict_append_basic(iter, "Gateway",
1121                                 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1122 }
1123
1124 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1125                                                         DBusMessageIter *iter)
1126 {
1127         const char *str;
1128
1129         str = __connman_ipconfig_method2string(ipconfig->method);
1130         if (str == NULL)
1131                 return;
1132
1133         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1134
1135         switch (ipconfig->method) {
1136         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1137         case CONNMAN_IPCONFIG_METHOD_OFF:
1138         case CONNMAN_IPCONFIG_METHOD_FIXED:
1139         case CONNMAN_IPCONFIG_METHOD_DHCP:
1140                 return;
1141         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1142                 break;
1143         }
1144
1145         if (ipconfig->address == NULL)
1146                 return;
1147
1148         if (ipconfig->address->local != NULL) {
1149                 in_addr_t addr;
1150                 struct in_addr netmask;
1151                 char *mask;
1152
1153                 connman_dbus_dict_append_basic(iter, "Address",
1154                                 DBUS_TYPE_STRING, &ipconfig->address->local);
1155
1156                 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1157                 netmask.s_addr = htonl(addr);
1158                 mask = inet_ntoa(netmask);
1159                 connman_dbus_dict_append_basic(iter, "Netmask",
1160                                                 DBUS_TYPE_STRING, &mask);
1161         }
1162
1163         if (ipconfig->address->gateway != NULL)
1164                 connman_dbus_dict_append_basic(iter, "Gateway",
1165                                 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1166 }
1167
1168 int __connman_ipconfig_set_ipv4config(struct connman_ipconfig *ipconfig,
1169                                                         DBusMessageIter *array)
1170 {
1171         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1172         const char *address = NULL, *netmask = NULL, *gateway = NULL;
1173         DBusMessageIter dict;
1174
1175         DBG("ipconfig %p", ipconfig);
1176
1177         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1178                 return -EINVAL;
1179
1180         dbus_message_iter_recurse(array, &dict);
1181
1182         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1183                 DBusMessageIter entry;
1184                 const char *key;
1185                 int type;
1186
1187                 dbus_message_iter_recurse(&dict, &entry);
1188
1189                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1190                         return -EINVAL;
1191
1192                 dbus_message_iter_get_basic(&entry, &key);
1193                 dbus_message_iter_next(&entry);
1194
1195                 type = dbus_message_iter_get_arg_type(&entry);
1196
1197                 if (g_str_equal(key, "Method") == TRUE) {
1198                         const char *str;
1199
1200                         if (type != DBUS_TYPE_STRING)
1201                                 return -EINVAL;
1202
1203                         dbus_message_iter_get_basic(&entry, &str);
1204                         method = __connman_ipconfig_string2method(str);
1205                 } else if (g_str_equal(key, "Address") == TRUE) {
1206                         if (type != DBUS_TYPE_STRING)
1207                                 return -EINVAL;
1208
1209                         dbus_message_iter_get_basic(&entry, &address);
1210                 } else if (g_str_equal(key, "Netmask") == TRUE) {
1211                         if (type != DBUS_TYPE_STRING)
1212                                 return -EINVAL;
1213
1214                         dbus_message_iter_get_basic(&entry, &netmask);
1215                 } else if (g_str_equal(key, "Gateway") == TRUE) {
1216                         if (type != DBUS_TYPE_STRING)
1217                                 return -EINVAL;
1218
1219                         dbus_message_iter_get_basic(&entry, &gateway);
1220                 }
1221                 dbus_message_iter_next(&dict);
1222         }
1223
1224         DBG("method %d address %s netmask %s gateway %s",
1225                                 method, address, netmask, gateway);
1226
1227         switch (method) {
1228         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1229         case CONNMAN_IPCONFIG_METHOD_OFF:
1230         case CONNMAN_IPCONFIG_METHOD_FIXED:
1231                 return -EINVAL;
1232
1233         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1234                 if (address == NULL)
1235                         return -EINVAL;
1236
1237                 ipconfig->method = method;
1238                 connman_ipaddress_set(ipconfig->address,
1239                                 address, netmask, gateway);
1240                 break;
1241
1242         case CONNMAN_IPCONFIG_METHOD_DHCP:
1243                 if (ipconfig->method == method)
1244                         return 0;
1245
1246                 ipconfig->method = method;
1247                 break;
1248         }
1249
1250         return 0;
1251 }
1252
1253 void __connman_ipconfig_append_proxy(struct connman_ipconfig *ipconfig,
1254                                                         DBusMessageIter *iter)
1255 {
1256         const char *method = "direct";
1257
1258         connman_dbus_dict_append_basic(iter, "Method",
1259                                                 DBUS_TYPE_STRING, &method);
1260 }
1261
1262 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1263                                                         DBusMessageIter *iter)
1264 {
1265         struct connman_ipdevice *ipdevice;
1266         const char *method = "auto";
1267
1268         connman_dbus_dict_append_basic(iter, "Method",
1269                                                 DBUS_TYPE_STRING, &method);
1270
1271         ipdevice = g_hash_table_lookup(ipdevice_hash,
1272                                         GINT_TO_POINTER(ipconfig->index));
1273         if (ipdevice == NULL)
1274                 return;
1275
1276         if (ipdevice->ifname != NULL)
1277                 connman_dbus_dict_append_basic(iter, "Interface",
1278                                         DBUS_TYPE_STRING, &ipdevice->ifname);
1279
1280         if (ipdevice->address != NULL)
1281                 connman_dbus_dict_append_basic(iter, "Address",
1282                                         DBUS_TYPE_STRING, &ipdevice->address);
1283
1284         if (ipdevice->mtu > 0)
1285                 connman_dbus_dict_append_basic(iter, "MTU",
1286                                         DBUS_TYPE_UINT16, &ipdevice->mtu);
1287 }
1288
1289 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1290                 GKeyFile *keyfile, const char *identifier, const char *prefix)
1291 {
1292         const char *method;
1293         char *key;
1294
1295         DBG("ipconfig %p identifier %s", ipconfig, identifier);
1296
1297         key = g_strdup_printf("%smethod", prefix);
1298         method = g_key_file_get_string(keyfile, identifier, key, NULL);
1299         if (method == NULL)
1300                 ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
1301         else
1302                 ipconfig->method = __connman_ipconfig_string2method(method);
1303         g_free(key);
1304
1305         key = g_strdup_printf("%snetmask_prefixlen", prefix);
1306         ipconfig->address->prefixlen = g_key_file_get_integer(
1307                                 keyfile, identifier, key, NULL);
1308         g_free(key);
1309
1310         key = g_strdup_printf("%slocal_address", prefix);
1311         ipconfig->address->local = g_key_file_get_string(
1312                         keyfile, identifier, key, NULL);
1313         g_free(key);
1314
1315         key = g_strdup_printf("%speer_address", prefix);
1316         ipconfig->address->peer = g_key_file_get_string(
1317                                 keyfile, identifier, key, NULL);
1318         g_free(key);
1319
1320         key = g_strdup_printf("%sbroadcast_address", prefix);
1321         ipconfig->address->broadcast = g_key_file_get_string(
1322                                 keyfile, identifier, key, NULL);
1323         g_free(key);
1324
1325         key = g_strdup_printf("%sgateway", prefix);
1326         ipconfig->address->gateway = g_key_file_get_string(
1327                                 keyfile, identifier, key, NULL);
1328         g_free(key);
1329
1330         return 0;
1331 }
1332
1333 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
1334                 GKeyFile *keyfile, const char *identifier, const char *prefix)
1335 {
1336         const char *method;
1337         char *key;
1338
1339         DBG("ipconfig %p identifier %s", ipconfig, identifier);
1340
1341         method = __connman_ipconfig_method2string(ipconfig->method);
1342
1343         key = g_strdup_printf("%smethod", prefix);
1344         g_key_file_set_string(keyfile, identifier, key, method);
1345         g_free(key);
1346
1347         key = g_strdup_printf("%snetmask_prefixlen", prefix);
1348         g_key_file_set_integer(keyfile, identifier,
1349                         key, ipconfig->address->prefixlen);
1350         g_free(key);
1351
1352         key = g_strdup_printf("%slocal_address", prefix);
1353         if (ipconfig->address->local != NULL)
1354                 g_key_file_set_string(keyfile, identifier,
1355                                 key, ipconfig->address->local);
1356         g_free(key);
1357
1358         key = g_strdup_printf("%speer_address", prefix);
1359         if (ipconfig->address->peer != NULL)
1360                 g_key_file_set_string(keyfile, identifier,
1361                                 key, ipconfig->address->peer);
1362         g_free(key);
1363
1364         key = g_strdup_printf("%sbroadcast_address", prefix);
1365         if (ipconfig->address->broadcast != NULL)
1366                 g_key_file_set_string(keyfile, identifier,
1367                         key, ipconfig->address->broadcast);
1368         g_free(key);
1369
1370         key = g_strdup_printf("%sgateway", prefix);
1371         if (ipconfig->address->gateway != NULL)
1372                 g_key_file_set_string(keyfile, identifier,
1373                         key, ipconfig->address->gateway);
1374         g_free(key);
1375
1376         return 0;
1377 }
1378
1379 int __connman_ipconfig_init(void)
1380 {
1381         DBG("");
1382
1383         ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1384                                                         NULL, free_ipdevice);
1385
1386         return 0;
1387 }
1388
1389 void __connman_ipconfig_cleanup(void)
1390 {
1391         DBG("");
1392
1393         g_hash_table_destroy(ipdevice_hash);
1394         ipdevice_hash = NULL;
1395 }