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