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