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