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