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