Load and save ipconfig settings
[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 enum connman_ipconfig_method __connman_ipconfig_get_method(
892                                 struct connman_ipconfig *ipconfig)
893 {
894         if (ipconfig == NULL)
895                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
896
897         return ipconfig->method;
898 }
899
900 /**
901  * connman_ipconfig_bind:
902  * @ipconfig: ipconfig structure
903  * @ipaddress: ipaddress structure
904  *
905  * Bind IP address details to configuration
906  */
907 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
908                                         struct connman_ipaddress *ipaddress)
909 {
910         struct connman_ipconfig *origin;
911
912         origin = ipconfig->origin ? ipconfig->origin : ipconfig;
913
914         connman_ipaddress_copy(origin->address, ipaddress);
915
916         connman_inet_set_address(origin->index, origin->address);
917 }
918
919 int __connman_ipconfig_set_address(struct connman_ipconfig *ipconfig)
920 {
921         DBG("");
922
923         switch (ipconfig->method) {
924         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
925         case CONNMAN_IPCONFIG_METHOD_OFF:
926         case CONNMAN_IPCONFIG_METHOD_FIXED:
927         case CONNMAN_IPCONFIG_METHOD_DHCP:
928                 break;
929         case CONNMAN_IPCONFIG_METHOD_MANUAL:
930                 return connman_inet_set_address(ipconfig->index,
931                                                 ipconfig->address);
932         }
933
934         return 0;
935 }
936
937 int __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
938 {
939         DBG("");
940
941         switch (ipconfig->method) {
942         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
943         case CONNMAN_IPCONFIG_METHOD_OFF:
944         case CONNMAN_IPCONFIG_METHOD_FIXED:
945         case CONNMAN_IPCONFIG_METHOD_DHCP:
946                 break;
947         case CONNMAN_IPCONFIG_METHOD_MANUAL:
948                 return connman_inet_clear_address(ipconfig->index);
949         }
950
951         return 0;
952
953 }
954
955 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
956 {
957         struct connman_ipdevice *ipdevice;
958
959         DBG("ipconfig %p", ipconfig);
960
961         if (ipconfig == NULL || ipconfig->index < 0)
962                 return -ENODEV;
963
964         ipdevice = g_hash_table_lookup(ipdevice_hash,
965                                         GINT_TO_POINTER(ipconfig->index));
966         if (ipdevice == NULL)
967                 return -ENXIO;
968
969         if (ipdevice->config == ipconfig)
970                 return -EALREADY;
971
972         if (ipdevice->config != NULL) {
973                 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
974
975                 connman_ipaddress_clear(ipdevice->config->system);
976
977                 connman_ipconfig_unref(ipdevice->config);
978         }
979
980         ipdevice->config = connman_ipconfig_ref(ipconfig);
981
982         ipconfig_list = g_list_append(ipconfig_list, ipconfig);
983
984         return 0;
985 }
986
987 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
988 {
989         struct connman_ipdevice *ipdevice;
990
991         DBG("ipconfig %p", ipconfig);
992
993         if (ipconfig == NULL || ipconfig->index < 0)
994                 return -ENODEV;
995
996         ipdevice = g_hash_table_lookup(ipdevice_hash,
997                                         GINT_TO_POINTER(ipconfig->index));
998         if (ipdevice == NULL)
999                 return -ENXIO;
1000
1001         if (ipdevice->config == NULL || ipdevice->config != ipconfig)
1002                 return -EINVAL;
1003
1004         ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1005
1006         connman_ipaddress_clear(ipdevice->config->system);
1007
1008         connman_ipconfig_unref(ipdevice->config);
1009         ipdevice->config = NULL;
1010
1011         return 0;
1012 }
1013
1014 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1015 {
1016         switch (method) {
1017         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1018                 break;
1019         case CONNMAN_IPCONFIG_METHOD_OFF:
1020                 return "off";
1021         case CONNMAN_IPCONFIG_METHOD_FIXED:
1022                 return "fixed";
1023         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1024                 return "manual";
1025         case CONNMAN_IPCONFIG_METHOD_DHCP:
1026                 return "dhcp";
1027         }
1028
1029         return NULL;
1030 }
1031
1032 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1033 {
1034         if (g_strcmp0(method, "off") == 0)
1035                 return CONNMAN_IPCONFIG_METHOD_OFF;
1036         else if (g_strcmp0(method, "fixed") == 0)
1037                 return CONNMAN_IPCONFIG_METHOD_FIXED;
1038         else if (g_strcmp0(method, "manual") == 0)
1039                 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1040         else if (g_strcmp0(method, "dhcp") == 0)
1041                 return CONNMAN_IPCONFIG_METHOD_DHCP;
1042         else
1043                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1044 }
1045
1046 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1047                                                         DBusMessageIter *iter)
1048 {
1049         const char *str;
1050
1051         str = __connman_ipconfig_method2string(ipconfig->method);
1052         if (str == NULL)
1053                 return;
1054
1055         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1056
1057         if (ipconfig->system == NULL)
1058                 return;
1059
1060         if (ipconfig->system->local != NULL) {
1061                 in_addr_t addr;
1062                 struct in_addr netmask;
1063                 char *mask;
1064
1065                 connman_dbus_dict_append_basic(iter, "Address",
1066                                 DBUS_TYPE_STRING, &ipconfig->system->local);
1067
1068                 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1069                 netmask.s_addr = htonl(addr);
1070                 mask = inet_ntoa(netmask);
1071                 connman_dbus_dict_append_basic(iter, "Netmask",
1072                                                 DBUS_TYPE_STRING, &mask);
1073         }
1074 }
1075
1076 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1077                                                         DBusMessageIter *iter)
1078 {
1079         const char *str;
1080
1081         str = __connman_ipconfig_method2string(ipconfig->method);
1082         if (str == NULL)
1083                 return;
1084
1085         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1086
1087         switch (ipconfig->method) {
1088         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1089         case CONNMAN_IPCONFIG_METHOD_OFF:
1090         case CONNMAN_IPCONFIG_METHOD_FIXED:
1091         case CONNMAN_IPCONFIG_METHOD_DHCP:
1092                 return;
1093         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1094                 break;
1095         }
1096
1097         if (ipconfig->address == NULL)
1098                 return;
1099
1100         if (ipconfig->address->local != NULL) {
1101                 in_addr_t addr;
1102                 struct in_addr netmask;
1103                 char *mask;
1104
1105                 connman_dbus_dict_append_basic(iter, "Address",
1106                                 DBUS_TYPE_STRING, &ipconfig->address->local);
1107
1108                 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1109                 netmask.s_addr = htonl(addr);
1110                 mask = inet_ntoa(netmask);
1111                 connman_dbus_dict_append_basic(iter, "Netmask",
1112                                                 DBUS_TYPE_STRING, &mask);
1113         }
1114 }
1115
1116 int __connman_ipconfig_set_ipv4config(struct connman_ipconfig *ipconfig,
1117                                                         DBusMessageIter *array)
1118 {
1119         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1120         const char *address = NULL, *netmask = NULL;
1121         DBusMessageIter dict;
1122
1123         DBG("ipconfig %p", ipconfig);
1124
1125         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1126                 return -EINVAL;
1127
1128         dbus_message_iter_recurse(array, &dict);
1129
1130         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1131                 DBusMessageIter entry;
1132                 const char *key;
1133                 int type;
1134
1135                 dbus_message_iter_recurse(&dict, &entry);
1136
1137                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1138                         return -EINVAL;
1139
1140                 dbus_message_iter_get_basic(&entry, &key);
1141                 dbus_message_iter_next(&entry);
1142
1143                 type = dbus_message_iter_get_arg_type(&entry);
1144
1145                 if (g_str_equal(key, "Method") == TRUE) {
1146                         const char *str;
1147
1148                         if (type != DBUS_TYPE_STRING)
1149                                 return -EINVAL;
1150
1151                         dbus_message_iter_get_basic(&entry, &str);
1152                         method = __connman_ipconfig_string2method(str);
1153                 } else if (g_str_equal(key, "Address") == TRUE) {
1154                         if (type != DBUS_TYPE_STRING)
1155                                 return -EINVAL;
1156
1157                         dbus_message_iter_get_basic(&entry, &address);
1158                 } else if (g_str_equal(key, "Netmask") == TRUE) {
1159                         if (type != DBUS_TYPE_STRING)
1160                                 return -EINVAL;
1161
1162                         dbus_message_iter_get_basic(&entry, &netmask);
1163                 }
1164
1165                 dbus_message_iter_next(&dict);
1166         }
1167
1168         DBG("method %d address %s netmask %s", method, address, netmask);
1169
1170         switch (method) {
1171         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1172         case CONNMAN_IPCONFIG_METHOD_OFF:
1173         case CONNMAN_IPCONFIG_METHOD_FIXED:
1174                 return -EINVAL;
1175
1176         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1177                 if (address == NULL)
1178                         return -EINVAL;
1179
1180                 ipconfig->method = method;
1181                 connman_ipaddress_set(ipconfig->address, address, netmask);
1182                 break;
1183
1184         case CONNMAN_IPCONFIG_METHOD_DHCP:
1185                 if (ipconfig->method == method)
1186                         return 0;
1187
1188                 ipconfig->method = method;
1189                 break;
1190         }
1191
1192         return 0;
1193 }
1194
1195 void __connman_ipconfig_append_proxy(struct connman_ipconfig *ipconfig,
1196                                                         DBusMessageIter *iter)
1197 {
1198         const char *method = "direct";
1199
1200         connman_dbus_dict_append_basic(iter, "Method",
1201                                                 DBUS_TYPE_STRING, &method);
1202 }
1203
1204 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1205                                                         DBusMessageIter *iter)
1206 {
1207         struct connman_ipdevice *ipdevice;
1208         const char *method = "auto";
1209
1210         connman_dbus_dict_append_basic(iter, "Method",
1211                                                 DBUS_TYPE_STRING, &method);
1212
1213         ipdevice = g_hash_table_lookup(ipdevice_hash,
1214                                         GINT_TO_POINTER(ipconfig->index));
1215         if (ipdevice == NULL)
1216                 return;
1217
1218         if (ipdevice->ifname != NULL)
1219                 connman_dbus_dict_append_basic(iter, "Interface",
1220                                         DBUS_TYPE_STRING, &ipdevice->ifname);
1221
1222         if (ipdevice->address != NULL)
1223                 connman_dbus_dict_append_basic(iter, "Address",
1224                                         DBUS_TYPE_STRING, &ipdevice->address);
1225
1226         if (ipdevice->mtu > 0)
1227                 connman_dbus_dict_append_basic(iter, "MTU",
1228                                         DBUS_TYPE_UINT16, &ipdevice->mtu);
1229 }
1230
1231 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1232                 GKeyFile *keyfile, const char *identifier, const char *prefix)
1233 {
1234         const char *method;
1235         char *key;
1236
1237         DBG("ipconfig %p identifier %s", ipconfig, identifier);
1238
1239         key = g_strdup_printf("%smethod", prefix);
1240         method = g_key_file_get_string(keyfile, identifier, key, NULL);
1241         ipconfig->method = __connman_ipconfig_string2method(method);
1242         g_free(key);
1243
1244         key = g_strdup_printf("%snetmask_prefixlen", prefix);
1245         ipconfig->address->prefixlen = g_key_file_get_integer(
1246                                 keyfile, identifier, key, NULL);
1247         g_free(key);
1248
1249         key = g_strdup_printf("%slocal_address", prefix);
1250         ipconfig->address->local = g_key_file_get_string(
1251                         keyfile, identifier, key, NULL);
1252         g_free(key);
1253
1254         key = g_strdup_printf("%speer_address", prefix);
1255         ipconfig->address->peer = g_key_file_get_string(
1256                                 keyfile, identifier, key, NULL);
1257         g_free(key);
1258
1259         key = g_strdup_printf("%sbroadcast_address", prefix);
1260         ipconfig->address->broadcast = g_key_file_get_string(
1261                                 keyfile, identifier, key, NULL);
1262         g_free(key);
1263
1264         return 0;
1265 }
1266
1267 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
1268                 GKeyFile *keyfile, const char *identifier, const char *prefix)
1269 {
1270         const char *method;
1271         char *key;
1272
1273         DBG("ipconfig %p identifier %s", ipconfig, identifier);
1274
1275         method = __connman_ipconfig_method2string(ipconfig->method);
1276
1277         key = g_strdup_printf("%smethod", prefix);
1278         g_key_file_set_string(keyfile, identifier, key, method);
1279         g_free(key);
1280
1281         key = g_strdup_printf("%snetmask_prefixlen", prefix);
1282         g_key_file_set_integer(keyfile, identifier,
1283                         key, ipconfig->address->prefixlen);
1284         g_free(key);
1285
1286         key = g_strdup_printf("%slocal_address", prefix);
1287         if (ipconfig->address->local != NULL)
1288                 g_key_file_set_string(keyfile, identifier,
1289                                 key, ipconfig->address->local);
1290         g_free(key);
1291
1292         key = g_strdup_printf("%speer_address", prefix);
1293         if (ipconfig->address->peer != NULL)
1294                 g_key_file_set_string(keyfile, identifier,
1295                                 key, ipconfig->address->peer);
1296         g_free(key);
1297
1298         key = g_strdup_printf("%sbroadcast_address", prefix);
1299         if (ipconfig->address->broadcast != NULL)
1300                 g_key_file_set_string(keyfile, identifier,
1301                         "broadcast_address", ipconfig->address->broadcast);
1302         g_free(key);
1303
1304         return 0;
1305 }
1306
1307 int __connman_ipconfig_init(void)
1308 {
1309         DBG("");
1310
1311         ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1312                                                         NULL, free_ipdevice);
1313
1314         return 0;
1315 }
1316
1317 void __connman_ipconfig_cleanup(void)
1318 {
1319         DBG("");
1320
1321         g_hash_table_destroy(ipdevice_hash);
1322         ipdevice_hash = NULL;
1323 }