ipconfig: Allow more than one IP address per device
[framework/connectivity/connman.git] / src / ipconfig.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <net/if.h>
27 #include <net/if_arp.h>
28 #include <linux/if_link.h>
29 #include <string.h>
30 #include <stdlib.h>
31
32 #ifndef IFF_LOWER_UP
33 #define IFF_LOWER_UP    0x10000
34 #endif
35
36 #include <gdbus.h>
37
38 #include "connman.h"
39
40 struct connman_ipconfig {
41         gint refcount;
42         int index;
43         enum connman_ipconfig_type type;
44
45         struct connman_ipconfig *origin;
46
47         const struct connman_ipconfig_ops *ops;
48         void *ops_data;
49
50         enum connman_ipconfig_method method;
51         struct connman_ipaddress *address;
52         struct connman_ipaddress *system;
53
54         struct connman_ipconfig *ipv6;
55 };
56
57 struct connman_ipdevice {
58         int index;
59         char *ifname;
60         unsigned short type;
61         unsigned int flags;
62         char *address;
63         uint16_t mtu;
64         uint32_t rx_packets;
65         uint32_t tx_packets;
66         uint32_t rx_bytes;
67         uint32_t tx_bytes;
68         uint32_t rx_errors;
69         uint32_t tx_errors;
70         uint32_t rx_dropped;
71         uint32_t tx_dropped;
72
73         GSList *address_list;
74         char *ipv4_gateway;
75         char *ipv6_gateway;
76
77         char *pac;
78
79         struct connman_ipconfig *config;
80
81         struct connman_ipconfig_driver *driver;
82         struct connman_ipconfig *driver_config;
83 };
84
85 static GHashTable *ipdevice_hash = NULL;
86 static GList *ipconfig_list = NULL;
87
88 struct connman_ipaddress *connman_ipaddress_alloc(int family)
89 {
90         struct connman_ipaddress *ipaddress;
91
92         ipaddress = g_try_new0(struct connman_ipaddress, 1);
93         if (ipaddress == NULL)
94                 return NULL;
95
96         ipaddress->family = family;
97         ipaddress->prefixlen = 0;
98         ipaddress->local = NULL;
99         ipaddress->peer = NULL;
100         ipaddress->broadcast = NULL;
101         ipaddress->gateway = NULL;
102
103         return ipaddress;
104 }
105
106 void connman_ipaddress_free(struct connman_ipaddress *ipaddress)
107 {
108         if (ipaddress == NULL)
109                 return;
110
111         g_free(ipaddress->broadcast);
112         g_free(ipaddress->peer);
113         g_free(ipaddress->local);
114         g_free(ipaddress->gateway);
115         g_free(ipaddress);
116 }
117
118 unsigned char __connman_ipconfig_netmask_prefix_len(const char *netmask)
119 {
120         unsigned char bits;
121         in_addr_t mask;
122         in_addr_t host;
123
124         if (netmask == NULL)
125                 return 32;
126
127         mask = inet_network(netmask);
128         host = ~mask;
129
130         /* a valid netmask must be 2^n - 1 */
131         if ((host & (host + 1)) != 0)
132                 return -1;
133
134         bits = 0;
135         for (; mask; mask <<= 1)
136                 ++bits;
137
138         return bits;
139 }
140
141 static gboolean check_ipv6_address(const char *address)
142 {
143         unsigned char buf[sizeof(struct in6_addr)];
144         int err;
145
146         err = inet_pton(AF_INET6, address, buf);
147         if (err > 0)
148                 return TRUE;
149
150         return FALSE;
151 }
152
153 int connman_ipaddress_set_ipv6(struct connman_ipaddress *ipaddress,
154                                 const char *address, const char *gateway,
155                                                 unsigned char prefix_length)
156 {
157         if (ipaddress == NULL)
158                 return -EINVAL;
159
160         if (check_ipv6_address(address) == FALSE)
161                 return -EINVAL;
162
163         if (check_ipv6_address(gateway) == FALSE)
164                 return -EINVAL;
165
166         DBG("prefix_len %d address %s gateway %s",
167                         prefix_length, address, gateway);
168
169         ipaddress->prefixlen = prefix_length;
170
171         g_free(ipaddress->local);
172         ipaddress->local = g_strdup(address);
173
174         g_free(ipaddress->gateway);
175         ipaddress->gateway = g_strdup(gateway);
176
177         return 0;
178 }
179
180 void connman_ipaddress_set_ipv4(struct connman_ipaddress *ipaddress,
181                 const char *address, const char *netmask, const char *gateway)
182 {
183         if (ipaddress == NULL)
184                 return;
185
186         ipaddress->prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);
187
188         g_free(ipaddress->local);
189         ipaddress->local = g_strdup(address);
190
191         g_free(ipaddress->gateway);
192         ipaddress->gateway = g_strdup(gateway);
193 }
194
195 void connman_ipaddress_clear(struct connman_ipaddress *ipaddress)
196 {
197         if (ipaddress == NULL)
198                 return;
199
200         ipaddress->prefixlen = 0;
201
202         g_free(ipaddress->local);
203         ipaddress->local = NULL;
204
205         g_free(ipaddress->peer);
206         ipaddress->peer = NULL;
207
208         g_free(ipaddress->broadcast);
209         ipaddress->broadcast = NULL;
210
211         g_free(ipaddress->gateway);
212         ipaddress->gateway = NULL;
213 }
214
215 void connman_ipaddress_copy(struct connman_ipaddress *ipaddress,
216                                         struct connman_ipaddress *source)
217 {
218         if (ipaddress == NULL || source == NULL)
219                 return;
220
221         ipaddress->family = source->family;
222         ipaddress->prefixlen = source->prefixlen;
223
224         g_free(ipaddress->local);
225         ipaddress->local = g_strdup(source->local);
226
227         g_free(ipaddress->peer);
228         ipaddress->peer = g_strdup(source->peer);
229
230         g_free(ipaddress->broadcast);
231         ipaddress->broadcast = g_strdup(source->broadcast);
232
233         g_free(ipaddress->gateway);
234         ipaddress->gateway = g_strdup(source->gateway);
235 }
236
237 static void free_address_list(struct connman_ipdevice *ipdevice)
238 {
239         GSList *list;
240
241         for (list = ipdevice->address_list; list; list = list->next) {
242                 struct connman_ipaddress *ipaddress = list->data;
243
244                 connman_ipaddress_free(ipaddress);
245                 list->data = NULL;
246         }
247
248         g_slist_free(ipdevice->address_list);
249         ipdevice->address_list = NULL;
250 }
251
252 static struct connman_ipaddress *find_ipaddress(struct connman_ipdevice *ipdevice,
253                                 unsigned char prefixlen, const char *local)
254 {
255         GSList *list;
256
257         for (list = ipdevice->address_list; list; list = list->next) {
258                 struct connman_ipaddress *ipaddress = list->data;
259
260                 if (g_strcmp0(ipaddress->local, local) == 0 &&
261                                         ipaddress->prefixlen == prefixlen)
262                         return ipaddress;
263         }
264
265         return NULL;
266 }
267
268 static const char *type2str(unsigned short type)
269 {
270         switch (type) {
271         case ARPHRD_ETHER:
272                 return "ETHER";
273         case ARPHRD_LOOPBACK:
274                 return "LOOPBACK";
275         case ARPHRD_PPP:
276                 return "PPP";
277         case ARPHRD_NONE:
278                 return "NONE";
279         case ARPHRD_VOID:
280                 return "VOID";
281         }
282
283         return "";
284 }
285
286 static const char *scope2str(unsigned char scope)
287 {
288         switch (scope) {
289         case 0:
290                 return "UNIVERSE";
291         case 253:
292                 return "LINK";
293         }
294
295         return "";
296 }
297
298 static void free_ipdevice(gpointer data)
299 {
300         struct connman_ipdevice *ipdevice = data;
301
302         connman_info("%s {remove} index %d", ipdevice->ifname,
303                                                         ipdevice->index);
304
305         if (ipdevice->config != NULL)
306                 connman_ipconfig_unref(ipdevice->config);
307
308         free_address_list(ipdevice);
309         g_free(ipdevice->ipv4_gateway);
310         g_free(ipdevice->ipv6_gateway);
311         g_free(ipdevice->pac);
312
313         g_free(ipdevice->address);
314         g_free(ipdevice->ifname);
315         g_free(ipdevice);
316 }
317
318 static GSList *driver_list = NULL;
319
320 static gint compare_priority(gconstpointer a, gconstpointer b)
321 {
322         const struct connman_ipconfig_driver *driver1 = a;
323         const struct connman_ipconfig_driver *driver2 = b;
324
325         return driver2->priority - driver1->priority;
326 }
327
328 /**
329  * connman_ipconfig_driver_register:
330  * @driver: IP configuration driver
331  *
332  * Register a new IP configuration driver
333  *
334  * Returns: %0 on success
335  */
336 int connman_ipconfig_driver_register(struct connman_ipconfig_driver *driver)
337 {
338         DBG("driver %p name %s", driver, driver->name);
339
340         driver_list = g_slist_insert_sorted(driver_list, driver,
341                                                         compare_priority);
342
343         return 0;
344 }
345
346 /**
347  * connman_ipconfig_driver_unregister:
348  * @driver: IP configuration driver
349  *
350  * Remove a previously registered IP configuration driver.
351  */
352 void connman_ipconfig_driver_unregister(struct connman_ipconfig_driver *driver)
353 {
354         DBG("driver %p name %s", driver, driver->name);
355
356         driver_list = g_slist_remove(driver_list, driver);
357 }
358
359 static void __connman_ipconfig_lower_up(struct connman_ipdevice *ipdevice)
360 {
361         GSList *list;
362
363         DBG("ipconfig %p", ipdevice->config);
364
365         if (ipdevice->config == NULL)
366                 return;
367
368         switch (ipdevice->config->method) {
369         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
370         case CONNMAN_IPCONFIG_METHOD_OFF:
371         case CONNMAN_IPCONFIG_METHOD_FIXED:
372         case CONNMAN_IPCONFIG_METHOD_MANUAL:
373                 return;
374         case CONNMAN_IPCONFIG_METHOD_DHCP:
375                 break;
376         }
377
378         if (ipdevice->driver != NULL)
379                 return;
380
381         ipdevice->driver_config = connman_ipconfig_clone(ipdevice->config);
382         if (ipdevice->driver_config == NULL)
383                 return;
384
385         for (list = driver_list; list; list = list->next) {
386                 struct connman_ipconfig_driver *driver = list->data;
387
388                 if (driver->request(ipdevice->driver_config) == 0) {
389                         ipdevice->driver = driver;
390                         break;
391                 }
392         }
393
394         if (ipdevice->driver == NULL) {
395                 connman_ipconfig_unref(ipdevice->driver_config);
396                 ipdevice->driver_config = NULL;
397         }
398 }
399
400 static void __connman_ipconfig_lower_down(struct connman_ipdevice *ipdevice)
401 {
402         DBG("ipconfig %p", ipdevice->config);
403
404         if (ipdevice->config == NULL)
405                 return;
406
407         if (ipdevice->driver == NULL)
408                 return;
409
410         ipdevice->driver->release(ipdevice->driver_config);
411
412         ipdevice->driver = NULL;
413
414         connman_ipconfig_unref(ipdevice->driver_config);
415         ipdevice->driver_config = NULL;
416
417         connman_inet_clear_address(ipdevice->index, ipdevice->config->address);
418         connman_inet_clear_ipv6_address(ipdevice->index,
419                         ipdevice->driver_config->address->local,
420                         ipdevice->driver_config->address->prefixlen);
421 }
422
423 static void update_stats(struct connman_ipdevice *ipdevice,
424                                                 struct rtnl_link_stats *stats)
425 {
426         if (stats->rx_packets == 0 && stats->tx_packets == 0)
427                 return;
428
429         connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
430                                         stats->rx_packets, stats->rx_bytes);
431         connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
432                                         stats->tx_packets, stats->tx_bytes);
433
434         if (ipdevice->config == NULL)
435                 return;
436
437         ipdevice->rx_packets = stats->rx_packets;
438         ipdevice->tx_packets = stats->tx_packets;
439         ipdevice->rx_bytes = stats->rx_bytes;
440         ipdevice->tx_bytes = stats->tx_bytes;
441         ipdevice->rx_errors = stats->rx_errors;
442         ipdevice->tx_errors = stats->tx_errors;
443         ipdevice->rx_dropped = stats->rx_dropped;
444         ipdevice->tx_dropped = stats->tx_dropped;
445
446         __connman_service_notify(ipdevice->config,
447                                 ipdevice->rx_packets, ipdevice->tx_packets,
448                                 ipdevice->rx_bytes, ipdevice->tx_bytes,
449                                 ipdevice->rx_errors, ipdevice->tx_errors,
450                                 ipdevice->rx_dropped, ipdevice->tx_dropped);
451 }
452
453 void __connman_ipconfig_newlink(int index, unsigned short type,
454                                 unsigned int flags, const char *address,
455                                                         unsigned short mtu,
456                                                 struct rtnl_link_stats *stats)
457 {
458         struct connman_ipdevice *ipdevice;
459         GList *list;
460         GString *str;
461         gboolean up = FALSE, down = FALSE;
462         gboolean lower_up = FALSE, lower_down = FALSE;
463
464         DBG("index %d", index);
465
466         if (type == ARPHRD_LOOPBACK)
467                 return;
468
469         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
470         if (ipdevice != NULL)
471                 goto update;
472
473         ipdevice = g_try_new0(struct connman_ipdevice, 1);
474         if (ipdevice == NULL)
475                 return;
476
477         ipdevice->index = index;
478         ipdevice->ifname = connman_inet_ifname(index);
479         ipdevice->type = type;
480
481         ipdevice->address = g_strdup(address);
482
483         g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
484
485         connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
486                                                 index, type, type2str(type));
487
488 update:
489         ipdevice->mtu = mtu;
490
491         update_stats(ipdevice, stats);
492
493         if (flags == ipdevice->flags)
494                 return;
495
496         if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
497                 if (flags & IFF_UP)
498                         up = TRUE;
499                 else
500                         down = TRUE;
501         }
502
503         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
504                                 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
505                 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
506                                         (IFF_RUNNING | IFF_LOWER_UP))
507                         lower_up = TRUE;
508                 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
509                         lower_down = TRUE;
510         }
511
512         ipdevice->flags = flags;
513
514         str = g_string_new(NULL);
515         if (str == NULL)
516                 return;
517
518         if (flags & IFF_UP)
519                 g_string_append(str, "UP");
520         else
521                 g_string_append(str, "DOWN");
522
523         if (flags & IFF_RUNNING)
524                 g_string_append(str, ",RUNNING");
525
526         if (flags & IFF_LOWER_UP)
527                 g_string_append(str, ",LOWER_UP");
528
529         connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
530                                                         flags, str->str);
531
532         g_string_free(str, TRUE);
533
534         for (list = g_list_first(ipconfig_list); list;
535                                                 list = g_list_next(list)) {
536                 struct connman_ipconfig *ipconfig = list->data;
537
538                 if (index != ipconfig->index)
539                         continue;
540
541                 if (ipconfig->ops == NULL)
542                         continue;
543
544                 if (up == TRUE && ipconfig->ops->up)
545                         ipconfig->ops->up(ipconfig);
546                 if (lower_up == TRUE && ipconfig->ops->lower_up)
547                         ipconfig->ops->lower_up(ipconfig);
548
549                 if (lower_down == TRUE && ipconfig->ops->lower_down)
550                         ipconfig->ops->lower_down(ipconfig);
551                 if (down == TRUE && ipconfig->ops->down)
552                         ipconfig->ops->down(ipconfig);
553         }
554
555         if (lower_up)
556                 __connman_ipconfig_lower_up(ipdevice);
557         if (lower_down)
558                 __connman_ipconfig_lower_down(ipdevice);
559 }
560
561 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
562 {
563         struct connman_ipdevice *ipdevice;
564         GList *list;
565
566         DBG("index %d", index);
567
568         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
569         if (ipdevice == NULL)
570                 return;
571
572         update_stats(ipdevice, stats);
573
574         for (list = g_list_first(ipconfig_list); list;
575                                                 list = g_list_next(list)) {
576                 struct connman_ipconfig *ipconfig = list->data;
577
578                 if (index != ipconfig->index)
579                         continue;
580
581                 ipconfig->index = -1;
582
583                 if (ipconfig->ops == NULL)
584                         continue;
585
586                 if (ipconfig->ops->lower_down)
587                         ipconfig->ops->lower_down(ipconfig);
588                 if (ipconfig->ops->down)
589                         ipconfig->ops->down(ipconfig);
590         }
591
592         __connman_ipconfig_lower_down(ipdevice);
593
594         g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
595 }
596
597 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
598 {
599         const struct connman_ipaddress *addr1 = a;
600         const struct connman_ipaddress *addr2 = b;
601
602         if (addr1->prefixlen != addr2->prefixlen)
603                 return addr2->prefixlen - addr1->prefixlen;
604
605         return g_strcmp0(addr1->local, addr2->local);
606 }
607
608 void __connman_ipconfig_newaddr(int index, int family, const char *label,
609                                 unsigned char prefixlen, const char *address)
610 {
611         struct connman_ipdevice *ipdevice;
612         struct connman_ipaddress *ipaddress;
613         GList *list;
614
615         DBG("index %d", index);
616
617         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
618         if (ipdevice == NULL)
619                 return;
620
621         ipaddress = connman_ipaddress_alloc(family);
622         if (ipaddress == NULL)
623                 return;
624
625         ipaddress->prefixlen = prefixlen;
626         ipaddress->local = g_strdup(address);
627
628         if (g_slist_find_custom(ipdevice->address_list, ipaddress,
629                                         check_duplicate_address)) {
630                 connman_ipaddress_free(ipaddress);
631                 return;
632         }
633
634         ipdevice->address_list = g_slist_append(ipdevice->address_list,
635                                                                 ipaddress);
636
637         connman_info("%s {add} address %s/%u label %s", ipdevice->ifname,
638                                                 address, prefixlen, label);
639
640         if (ipdevice->config != NULL) {
641                 if (family == AF_INET6 && ipdevice->config->ipv6 != NULL)
642                         connman_ipaddress_copy(ipdevice->config->ipv6->system,
643                                                         ipaddress);
644                 else
645                         connman_ipaddress_copy(ipdevice->config->system,
646                                                         ipaddress);
647         }
648
649         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
650                 return;
651
652         for (list = g_list_first(ipconfig_list); list;
653                                                 list = g_list_next(list)) {
654                 struct connman_ipconfig *ipconfig = list->data;
655
656                 if (index != ipconfig->index)
657                         continue;
658
659                 if (ipconfig->ops == NULL)
660                         continue;
661
662                 if (ipconfig->ops->ip_bound)
663                         ipconfig->ops->ip_bound(ipconfig);
664         }
665 }
666
667 void __connman_ipconfig_deladdr(int index, int family, const char *label,
668                                 unsigned char prefixlen, const char *address)
669 {
670         struct connman_ipdevice *ipdevice;
671         struct connman_ipaddress *ipaddress;
672         GList *list;
673
674         DBG("index %d", index);
675
676         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
677         if (ipdevice == NULL)
678                 return;
679
680         ipaddress = find_ipaddress(ipdevice, prefixlen, address);
681         if (ipaddress == NULL)
682                 return;
683
684         ipdevice->address_list = g_slist_remove(ipdevice->address_list,
685                                                                 ipaddress);
686
687         connman_ipaddress_free(ipaddress);
688
689         connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
690                                                 address, prefixlen, label);
691
692         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
693                 return;
694
695         if (g_slist_length(ipdevice->address_list) > 0)
696                 return;
697
698         for (list = g_list_first(ipconfig_list); list;
699                                                 list = g_list_next(list)) {
700                 struct connman_ipconfig *ipconfig = list->data;
701
702                 if (index != ipconfig->index)
703                         continue;
704
705                 if (ipconfig->ops == NULL)
706                         continue;
707
708                 if (ipconfig->ops->ip_release)
709                         ipconfig->ops->ip_release(ipconfig);
710         }
711 }
712
713 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
714                                         const char *dst, const char *gateway)
715 {
716         struct connman_ipdevice *ipdevice;
717
718         DBG("index %d", index);
719
720         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
721         if (ipdevice == NULL)
722                 return;
723
724         if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
725                 GSList *list;
726                 GList *config_list;
727
728                 if (family == AF_INET6) {
729                         g_free(ipdevice->ipv6_gateway);
730                         ipdevice->ipv6_gateway = g_strdup(gateway);
731                 } else {
732                         g_free(ipdevice->ipv4_gateway);
733                         ipdevice->ipv4_gateway = g_strdup(gateway);
734                 }
735
736                 if (ipdevice->config != NULL &&
737                                         ipdevice->config->system != NULL) {
738                         g_free(ipdevice->config->system->gateway);
739                         ipdevice->config->system->gateway = g_strdup(gateway);
740                 }
741
742                 for (list = ipdevice->address_list; list; list = list->next) {
743                         struct connman_ipaddress *ipaddress = list->data;
744
745                         g_free(ipaddress->gateway);
746                         ipaddress->gateway = g_strdup(gateway);
747                 }
748
749                 for (config_list = g_list_first(ipconfig_list); config_list;
750                                         config_list = g_list_next(config_list)) {
751                         struct connman_ipconfig *ipconfig = config_list->data;
752
753                         if (index != ipconfig->index)
754                                 continue;
755
756                         if (ipconfig->ops == NULL)
757                                 continue;
758
759                         if (ipconfig->ops->ip_bound)
760                                 ipconfig->ops->ip_bound(ipconfig);
761                 }
762         }
763
764         connman_info("%s {add} route %s gw %s scope %u <%s>",
765                                         ipdevice->ifname, dst, gateway,
766                                                 scope, scope2str(scope));
767 }
768
769 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
770                                         const char *dst, const char *gateway)
771 {
772         struct connman_ipdevice *ipdevice;
773
774         DBG("index %d", index);
775
776         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
777         if (ipdevice == NULL)
778                 return;
779
780         if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
781                 GSList *list;
782                 GList *config_list;
783
784                 if (family == AF_INET6) {
785                         g_free(ipdevice->ipv6_gateway);
786                         ipdevice->ipv6_gateway = NULL;
787                 } else {
788                         g_free(ipdevice->ipv4_gateway);
789                         ipdevice->ipv4_gateway = NULL;
790                 }
791
792                 if (ipdevice->config != NULL &&
793                                         ipdevice->config->system != NULL) {
794                         g_free(ipdevice->config->system->gateway);
795                         ipdevice->config->system->gateway = NULL;
796                 }
797
798                 for (list = ipdevice->address_list; list; list = list->next) {
799                         struct connman_ipaddress *ipaddress = list->data;
800
801                         g_free(ipaddress->gateway);
802                         ipaddress->gateway = NULL;
803                 }
804
805                 for (config_list = g_list_first(ipconfig_list); config_list;
806                                         config_list = g_list_next(config_list)) {
807                         struct connman_ipconfig *ipconfig = config_list->data;
808
809                         if (index != ipconfig->index)
810                                 continue;
811
812                         if (ipconfig->ops == NULL)
813                                 continue;
814
815                         if (ipconfig->ops->ip_release)
816                                 ipconfig->ops->ip_release(ipconfig);
817                 }
818         }
819
820         connman_info("%s {del} route %s gw %s scope %u <%s>",
821                                         ipdevice->ifname, dst, gateway,
822                                                 scope, scope2str(scope));
823 }
824
825 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
826                                                         void *user_data)
827 {
828         GList *list, *keys;
829
830         keys = g_hash_table_get_keys(ipdevice_hash);
831         if (keys == NULL)
832                 return;
833
834         for (list = g_list_first(keys); list; list = g_list_next(list)) {
835                 int index = GPOINTER_TO_INT(list->data);
836
837                 function(index, user_data);
838         }
839
840         g_list_free(keys);
841 }
842
843 unsigned short __connman_ipconfig_get_type(int index)
844 {
845         struct connman_ipdevice *ipdevice;
846
847         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
848         if (ipdevice == NULL)
849                 return ARPHRD_VOID;
850
851         return ipdevice->type;
852 }
853
854 unsigned int __connman_ipconfig_get_flags(int index)
855 {
856         struct connman_ipdevice *ipdevice;
857
858         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
859         if (ipdevice == NULL)
860                 return 0;
861
862         return ipdevice->flags;
863 }
864
865 const char *__connman_ipconfig_get_gateway(int index)
866 {
867         struct connman_ipdevice *ipdevice;
868
869         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
870         if (ipdevice == NULL)
871                 return NULL;
872
873         if (ipdevice->ipv4_gateway != NULL)
874                 return ipdevice->ipv4_gateway;
875
876         if (ipdevice->config != NULL &&
877                         ipdevice->config->address != NULL)
878                 return ipdevice->config->address->gateway;
879
880         return NULL;
881 }
882
883 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
884 {
885         ipconfig->index = index;
886
887         if (ipconfig->ipv6 != NULL)
888                 ipconfig->ipv6->index = index;
889 }
890
891 static struct connman_ipconfig *create_ipv6config(int index)
892 {
893         struct connman_ipconfig *ipv6config;
894
895         DBG("index %d", index);
896
897         ipv6config = g_try_new0(struct connman_ipconfig, 1);
898         if (ipv6config == NULL)
899                 return NULL;
900
901         ipv6config->index = index;
902         ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
903         ipv6config->method = CONNMAN_IPCONFIG_METHOD_OFF;
904
905         ipv6config->address = connman_ipaddress_alloc(AF_INET6);
906         if (ipv6config->address == NULL) {
907                 g_free(ipv6config);
908                 return NULL;
909         }
910
911         ipv6config->system = connman_ipaddress_alloc(AF_INET6);
912
913         ipv6config->ipv6 = NULL;
914
915         DBG("ipconfig %p", ipv6config);
916
917         return ipv6config;
918 }
919
920 /**
921  * connman_ipconfig_create:
922  *
923  * Allocate a new ipconfig structure.
924  *
925  * Returns: a newly-allocated #connman_ipconfig structure
926  */
927 struct connman_ipconfig *connman_ipconfig_create(int index)
928 {
929         struct connman_ipconfig *ipconfig;
930
931         DBG("index %d", index);
932
933         ipconfig = g_try_new0(struct connman_ipconfig, 1);
934         if (ipconfig == NULL)
935                 return NULL;
936
937         ipconfig->refcount = 1;
938
939         ipconfig->index = index;
940         ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
941
942         ipconfig->address = connman_ipaddress_alloc(AF_INET);
943         if (ipconfig->address == NULL) {
944                 g_free(ipconfig);
945                 return NULL;
946         }
947
948         ipconfig->system = connman_ipaddress_alloc(AF_INET);
949
950         ipconfig->ipv6 = create_ipv6config(index);
951
952         DBG("ipconfig %p", ipconfig);
953
954         return ipconfig;
955 }
956
957 /**
958  * connman_ipconfig_clone:
959  *
960  * Clone an ipconfig structure and create new reference.
961  *
962  * Returns: a newly-allocated #connman_ipconfig structure
963  */
964 struct connman_ipconfig *connman_ipconfig_clone(struct connman_ipconfig *ipconfig)
965 {
966         struct connman_ipconfig *ipconfig_clone;
967
968         DBG("ipconfig %p", ipconfig);
969
970         ipconfig_clone = g_try_new0(struct connman_ipconfig, 1);
971         if (ipconfig_clone == NULL)
972                 return NULL;
973
974         ipconfig_clone->refcount = 1;
975
976         ipconfig_clone->origin = connman_ipconfig_ref(ipconfig);
977
978         ipconfig_clone->index = -1;
979
980         return ipconfig_clone;
981 }
982
983 /**
984  * connman_ipconfig_ref:
985  * @ipconfig: ipconfig structure
986  *
987  * Increase reference counter of ipconfig
988  */
989 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
990 {
991         g_atomic_int_inc(&ipconfig->refcount);
992
993         return ipconfig;
994 }
995
996 static void  free_ipv6config(struct connman_ipconfig *ipconfig)
997 {
998         if (ipconfig == NULL)
999                 return;
1000
1001         connman_ipconfig_set_ops(ipconfig, NULL);
1002         connman_ipaddress_free(ipconfig->system);
1003         connman_ipaddress_free(ipconfig->address);
1004         g_free(ipconfig->ipv6);
1005 }
1006
1007 /**
1008  * connman_ipconfig_unref:
1009  * @ipconfig: ipconfig structure
1010  *
1011  * Decrease reference counter of ipconfig
1012  */
1013 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
1014 {
1015         if (ipconfig &&
1016                 g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
1017                 __connman_ipconfig_disable(ipconfig);
1018
1019                 connman_ipconfig_set_ops(ipconfig, NULL);
1020
1021                 if (ipconfig->origin != NULL) {
1022                         connman_ipconfig_unref(ipconfig->origin);
1023                         ipconfig->origin = NULL;
1024                 }
1025
1026                 connman_ipaddress_free(ipconfig->system);
1027                 connman_ipaddress_free(ipconfig->address);
1028                 free_ipv6config(ipconfig->ipv6);
1029                 g_free(ipconfig);
1030         }
1031 }
1032
1033 /**
1034  * connman_ipconfig_get_data:
1035  * @ipconfig: ipconfig structure
1036  *
1037  * Get private data pointer
1038  */
1039 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1040 {
1041         return ipconfig->ops_data;
1042 }
1043
1044 /**
1045  * connman_ipconfig_set_data:
1046  * @ipconfig: ipconfig structure
1047  * @data: data pointer
1048  *
1049  * Set private data pointer
1050  */
1051 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1052 {
1053         ipconfig->ops_data = data;
1054 }
1055
1056 /**
1057  * connman_ipconfig_get_index:
1058  * @ipconfig: ipconfig structure
1059  *
1060  * Get interface index
1061  */
1062 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1063 {
1064         if (ipconfig == NULL)
1065                 return -1;
1066
1067         if (ipconfig->origin != NULL)
1068                 return ipconfig->origin->index;
1069
1070         return ipconfig->index;
1071 }
1072
1073 /**
1074  * connman_ipconfig_get_ifname:
1075  * @ipconfig: ipconfig structure
1076  *
1077  * Get interface name
1078  */
1079 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1080 {
1081         struct connman_ipdevice *ipdevice;
1082
1083         if (ipconfig == NULL)
1084                 return NULL;
1085
1086         if (ipconfig->index < 0)
1087                 return NULL;
1088
1089         ipdevice = g_hash_table_lookup(ipdevice_hash,
1090                                         GINT_TO_POINTER(ipconfig->index));
1091         if (ipdevice == NULL)
1092                 return NULL;
1093
1094         return ipdevice->ifname;
1095 }
1096
1097 /**
1098  * connman_ipconfig_set_ops:
1099  * @ipconfig: ipconfig structure
1100  * @ops: operation callbacks
1101  *
1102  * Set the operation callbacks
1103  */
1104 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1105                                 const struct connman_ipconfig_ops *ops)
1106 {
1107         ipconfig->ops = ops;
1108 }
1109
1110 struct connman_ipconfig *connman_ipconfig_get_ipv6config(
1111                                 struct connman_ipconfig *ipconfig)
1112 {
1113         if (ipconfig == NULL)
1114                 return NULL;
1115
1116         return ipconfig->ipv6;
1117 }
1118
1119 /**
1120  * connman_ipconfig_set_method:
1121  * @ipconfig: ipconfig structure
1122  * @method: configuration method
1123  *
1124  * Set the configuration method
1125  */
1126 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1127                                         enum connman_ipconfig_method method)
1128 {
1129         ipconfig->method = method;
1130
1131         return 0;
1132 }
1133
1134 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1135 {
1136         if (ipconfig == NULL)
1137                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1138
1139         return ipconfig->method;
1140 }
1141
1142 /**
1143  * connman_ipconfig_bind:
1144  * @ipconfig: ipconfig structure
1145  * @ipaddress: ipaddress structure
1146  *
1147  * Bind IP address details to configuration
1148  */
1149 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
1150                                         struct connman_ipaddress *ipaddress)
1151 {
1152         struct connman_ipconfig *origin;
1153
1154         origin = ipconfig->origin ? ipconfig->origin : ipconfig;
1155
1156         connman_ipaddress_copy(origin->address, ipaddress);
1157
1158         connman_inet_set_address(origin->index, origin->address);
1159 }
1160
1161 void __connman_ipconfig_set_element_ipv6_gateway(
1162                         struct connman_ipconfig *ipconfig,
1163                                 struct connman_element *element)
1164 {
1165         element->ipv6.gateway = ipconfig->ipv6->address->gateway;
1166 }
1167
1168 /*
1169  * FIXME: The element soulution should be removed in the future
1170  * Set IPv4 and IPv6 gateway
1171  */
1172 int __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig,
1173                                                 struct connman_element *parent)
1174 {
1175         struct connman_element *connection;
1176
1177         connection = connman_element_create(NULL);
1178
1179         DBG("ipconfig %p", ipconfig);
1180
1181         connection->type  = CONNMAN_ELEMENT_TYPE_CONNECTION;
1182         connection->index = ipconfig->index;
1183         connection->ipv4.gateway = ipconfig->address->gateway;
1184         connection->ipv6.gateway = ipconfig->ipv6->address->gateway;
1185
1186         if (connman_element_register(connection, parent) < 0)
1187                 connman_element_unref(connection);
1188
1189         return 0;
1190 }
1191
1192 int __connman_ipconfig_set_address(struct connman_ipconfig *ipconfig)
1193 {
1194         DBG("");
1195
1196         switch (ipconfig->method) {
1197         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1198         case CONNMAN_IPCONFIG_METHOD_OFF:
1199         case CONNMAN_IPCONFIG_METHOD_FIXED:
1200         case CONNMAN_IPCONFIG_METHOD_DHCP:
1201                 break;
1202         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1203                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1204                         return connman_inet_set_address(ipconfig->index,
1205                                                         ipconfig->address);
1206                 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1207                         return connman_inet_set_ipv6_address(
1208                                         ipconfig->index, ipconfig->address);
1209         }
1210
1211         return 0;
1212 }
1213
1214 int __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
1215 {
1216         DBG("");
1217
1218         if (ipconfig == NULL)
1219                 return 0;
1220
1221         DBG("method %d", ipconfig->method);
1222
1223         switch (ipconfig->method) {
1224         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1225         case CONNMAN_IPCONFIG_METHOD_OFF:
1226         case CONNMAN_IPCONFIG_METHOD_FIXED:
1227         case CONNMAN_IPCONFIG_METHOD_DHCP:
1228                 break;
1229         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1230                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1231                         return connman_inet_clear_address(ipconfig->index,
1232                                                         ipconfig->address);
1233                 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1234                         return connman_inet_clear_ipv6_address(
1235                                                 ipconfig->index,
1236                                                 ipconfig->address->local,
1237                                                 ipconfig->address->prefixlen);
1238         }
1239
1240         return 0;
1241 }
1242
1243 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1244                                                         const char *url)
1245 {
1246         struct connman_ipdevice *ipdevice;
1247
1248         DBG("ipconfig %p", ipconfig);
1249
1250         if (ipconfig == NULL || ipconfig->index < 0)
1251                 return -ENODEV;
1252
1253         ipdevice = g_hash_table_lookup(ipdevice_hash,
1254                                         GINT_TO_POINTER(ipconfig->index));
1255         if (ipdevice == NULL)
1256                 return -ENXIO;
1257
1258         g_free(ipdevice->pac);
1259         ipdevice->pac = g_strdup(url);
1260
1261         return 0;
1262 }
1263
1264 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1265 {
1266         struct connman_ipdevice *ipdevice;
1267
1268         DBG("ipconfig %p", ipconfig);
1269
1270         if (ipconfig == NULL || ipconfig->index < 0)
1271                 return NULL;
1272
1273         ipdevice = g_hash_table_lookup(ipdevice_hash,
1274                                         GINT_TO_POINTER(ipconfig->index));
1275         if (ipdevice == NULL)
1276                 return NULL;
1277
1278         return ipdevice->pac;
1279 }
1280
1281 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1282 {
1283         struct connman_ipdevice *ipdevice;
1284         gboolean up = FALSE, down = FALSE;
1285         gboolean lower_up = FALSE, lower_down = FALSE;
1286
1287         DBG("ipconfig %p", ipconfig);
1288
1289         if (ipconfig == NULL || ipconfig->index < 0)
1290                 return -ENODEV;
1291
1292         ipdevice = g_hash_table_lookup(ipdevice_hash,
1293                                         GINT_TO_POINTER(ipconfig->index));
1294         if (ipdevice == NULL)
1295                 return -ENXIO;
1296
1297         if (ipdevice->config == ipconfig)
1298                 return -EALREADY;
1299
1300         if (ipdevice->config != NULL) {
1301                 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1302
1303                 connman_ipaddress_clear(ipdevice->config->system);
1304
1305                 connman_ipconfig_unref(ipdevice->config);
1306         }
1307
1308         ipdevice->config = connman_ipconfig_ref(ipconfig);
1309
1310         ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1311
1312         if (ipdevice->flags & IFF_UP)
1313                 up = TRUE;
1314         else
1315                 down = TRUE;
1316
1317         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1318                         (IFF_RUNNING | IFF_LOWER_UP))
1319                 lower_up = TRUE;
1320         else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1321                 lower_down = TRUE;
1322
1323         if (up == TRUE && ipconfig->ops->up)
1324                 ipconfig->ops->up(ipconfig);
1325         if (lower_up == TRUE && ipconfig->ops->lower_up)
1326                 ipconfig->ops->lower_up(ipconfig);
1327
1328         if (lower_down == TRUE && ipconfig->ops->lower_down)
1329                 ipconfig->ops->lower_down(ipconfig);
1330         if (down == TRUE && ipconfig->ops->down)
1331                 ipconfig->ops->down(ipconfig);
1332
1333         return 0;
1334 }
1335
1336 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1337 {
1338         struct connman_ipdevice *ipdevice;
1339
1340         DBG("ipconfig %p", ipconfig);
1341
1342         if (ipconfig == NULL || ipconfig->index < 0)
1343                 return -ENODEV;
1344
1345         ipdevice = g_hash_table_lookup(ipdevice_hash,
1346                                         GINT_TO_POINTER(ipconfig->index));
1347         if (ipdevice == NULL)
1348                 return -ENXIO;
1349
1350         if (ipdevice->config == NULL || ipdevice->config != ipconfig)
1351                 return -EINVAL;
1352
1353         ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1354
1355         connman_ipaddress_clear(ipdevice->config->system);
1356         connman_ipaddress_clear(ipdevice->config->ipv6->system);
1357
1358         connman_ipconfig_unref(ipdevice->config);
1359         ipdevice->config = NULL;
1360
1361         return 0;
1362 }
1363
1364 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1365 {
1366         switch (method) {
1367         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1368                 break;
1369         case CONNMAN_IPCONFIG_METHOD_OFF:
1370                 return "off";
1371         case CONNMAN_IPCONFIG_METHOD_FIXED:
1372                 return "fixed";
1373         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1374                 return "manual";
1375         case CONNMAN_IPCONFIG_METHOD_DHCP:
1376                 return "dhcp";
1377         }
1378
1379         return NULL;
1380 }
1381
1382 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1383 {
1384         if (g_strcmp0(method, "off") == 0)
1385                 return CONNMAN_IPCONFIG_METHOD_OFF;
1386         else if (g_strcmp0(method, "fixed") == 0)
1387                 return CONNMAN_IPCONFIG_METHOD_FIXED;
1388         else if (g_strcmp0(method, "manual") == 0)
1389                 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1390         else if (g_strcmp0(method, "dhcp") == 0)
1391                 return CONNMAN_IPCONFIG_METHOD_DHCP;
1392         else
1393                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1394 }
1395
1396 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1397                                                         DBusMessageIter *iter)
1398 {
1399         const char *str;
1400
1401         DBG("");
1402
1403         str = __connman_ipconfig_method2string(ipconfig->method);
1404         if (str == NULL)
1405                 return;
1406
1407         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1408
1409         if (ipconfig->system == NULL)
1410                 return;
1411
1412         if (ipconfig->system->local != NULL) {
1413                 in_addr_t addr;
1414                 struct in_addr netmask;
1415                 char *mask;
1416
1417                 connman_dbus_dict_append_basic(iter, "Address",
1418                                 DBUS_TYPE_STRING, &ipconfig->system->local);
1419
1420                 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1421                 netmask.s_addr = htonl(addr);
1422                 mask = inet_ntoa(netmask);
1423                 connman_dbus_dict_append_basic(iter, "Netmask",
1424                                                 DBUS_TYPE_STRING, &mask);
1425         }
1426
1427         if (ipconfig->system->gateway != NULL)
1428                 connman_dbus_dict_append_basic(iter, "Gateway",
1429                                 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1430 }
1431
1432 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1433                                                         DBusMessageIter *iter)
1434 {
1435         const char *str;
1436
1437         DBG("");
1438
1439         str = __connman_ipconfig_method2string(ipconfig->method);
1440         if (str == NULL)
1441                 return;
1442
1443         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1444
1445         if (ipconfig->system == NULL)
1446                 return;
1447
1448         if (ipconfig->system->local != NULL) {
1449                 connman_dbus_dict_append_basic(iter, "Address",
1450                                 DBUS_TYPE_STRING, &ipconfig->system->local);
1451                 connman_dbus_dict_append_basic(iter, "PrefixLength",
1452                                                 DBUS_TYPE_BYTE,
1453                                                 &ipconfig->system->prefixlen);
1454         }
1455
1456         if (ipconfig->system->gateway != NULL)
1457                 connman_dbus_dict_append_basic(iter, "Gateway",
1458                                 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1459 }
1460
1461 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1462                                                         DBusMessageIter *iter)
1463 {
1464         const char *str;
1465
1466         DBG("");
1467
1468         str = __connman_ipconfig_method2string(ipconfig->method);
1469         if (str == NULL)
1470                 return;
1471
1472         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1473
1474         switch (ipconfig->method) {
1475         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1476         case CONNMAN_IPCONFIG_METHOD_OFF:
1477         case CONNMAN_IPCONFIG_METHOD_DHCP:
1478                 return;
1479         case CONNMAN_IPCONFIG_METHOD_FIXED:
1480         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1481                 break;
1482         }
1483
1484         if (ipconfig->address == NULL)
1485                 return;
1486
1487         if (ipconfig->address->local != NULL) {
1488                 connman_dbus_dict_append_basic(iter, "Address",
1489                                 DBUS_TYPE_STRING, &ipconfig->address->local);
1490                 connman_dbus_dict_append_basic(iter, "PrefixLength",
1491                                                 DBUS_TYPE_BYTE,
1492                                                 &ipconfig->address->prefixlen);
1493         }
1494
1495         if (ipconfig->address->gateway != NULL)
1496                 connman_dbus_dict_append_basic(iter, "Gateway",
1497                                 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1498 }
1499
1500 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1501                                                         DBusMessageIter *iter)
1502 {
1503         const char *str;
1504
1505         DBG("");
1506
1507         str = __connman_ipconfig_method2string(ipconfig->method);
1508         if (str == NULL)
1509                 return;
1510
1511         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1512
1513         switch (ipconfig->method) {
1514         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1515         case CONNMAN_IPCONFIG_METHOD_OFF:
1516         case CONNMAN_IPCONFIG_METHOD_FIXED:
1517         case CONNMAN_IPCONFIG_METHOD_DHCP:
1518                 return;
1519         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1520                 break;
1521         }
1522
1523         if (ipconfig->address == NULL)
1524                 return;
1525
1526         if (ipconfig->address->local != NULL) {
1527                 in_addr_t addr;
1528                 struct in_addr netmask;
1529                 char *mask;
1530
1531                 connman_dbus_dict_append_basic(iter, "Address",
1532                                 DBUS_TYPE_STRING, &ipconfig->address->local);
1533
1534                 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1535                 netmask.s_addr = htonl(addr);
1536                 mask = inet_ntoa(netmask);
1537                 connman_dbus_dict_append_basic(iter, "Netmask",
1538                                                 DBUS_TYPE_STRING, &mask);
1539         }
1540
1541         if (ipconfig->address->gateway != NULL)
1542                 connman_dbus_dict_append_basic(iter, "Gateway",
1543                                 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1544 }
1545
1546 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1547                 enum connman_ipconfig_type type, DBusMessageIter *array)
1548 {
1549         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1550         const char *address = NULL, *netmask = NULL, *gateway = NULL,
1551                         *prefix_length_string = NULL;
1552         int prefix_length = 0;
1553         DBusMessageIter dict;
1554
1555         DBG("ipconfig %p type %d", ipconfig, type);
1556
1557         if (type != CONNMAN_IPCONFIG_TYPE_IPV4 &&
1558                         type != CONNMAN_IPCONFIG_TYPE_IPV6)
1559                 return -EINVAL;
1560
1561         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1562                 return -EINVAL;
1563
1564         dbus_message_iter_recurse(array, &dict);
1565
1566         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1567                 DBusMessageIter entry;
1568                 const char *key;
1569                 int type;
1570
1571                 dbus_message_iter_recurse(&dict, &entry);
1572
1573                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1574                         return -EINVAL;
1575
1576                 dbus_message_iter_get_basic(&entry, &key);
1577                 dbus_message_iter_next(&entry);
1578
1579                 type = dbus_message_iter_get_arg_type(&entry);
1580
1581                 if (g_str_equal(key, "Method") == TRUE) {
1582                         const char *str;
1583
1584                         if (type != DBUS_TYPE_STRING)
1585                                 return -EINVAL;
1586
1587                         dbus_message_iter_get_basic(&entry, &str);
1588                         method = __connman_ipconfig_string2method(str);
1589                 } else if (g_str_equal(key, "Address") == TRUE) {
1590                         if (type != DBUS_TYPE_STRING)
1591                                 return -EINVAL;
1592
1593                         dbus_message_iter_get_basic(&entry, &address);
1594                 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1595                         if (type != DBUS_TYPE_STRING)
1596                                 return -EINVAL;
1597
1598                         dbus_message_iter_get_basic(&entry,
1599                                                         &prefix_length_string);
1600
1601                         prefix_length = atoi(prefix_length_string);
1602                         if (prefix_length < 0 || prefix_length > 128)
1603                                 return -EINVAL;
1604
1605                 } else if (g_str_equal(key, "Netmask") == TRUE) {
1606                         if (type != DBUS_TYPE_STRING)
1607                                 return -EINVAL;
1608
1609                         dbus_message_iter_get_basic(&entry, &netmask);
1610                 } else if (g_str_equal(key, "Gateway") == TRUE) {
1611                         if (type != DBUS_TYPE_STRING)
1612                                 return -EINVAL;
1613
1614                         dbus_message_iter_get_basic(&entry, &gateway);
1615                 }
1616                 dbus_message_iter_next(&dict);
1617         }
1618
1619         DBG("method %d address %s netmask %s gateway %s prefix_length %d",
1620                         method, address, netmask, gateway, prefix_length);
1621
1622         switch (method) {
1623         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1624         case CONNMAN_IPCONFIG_METHOD_OFF:
1625         case CONNMAN_IPCONFIG_METHOD_FIXED:
1626                 return -EINVAL;
1627
1628         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1629                 if (address == NULL)
1630                         return -EINVAL;
1631
1632                 ipconfig->method = method;
1633
1634                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1635                         connman_ipaddress_set_ipv4(ipconfig->address,
1636                                                 address, netmask, gateway);
1637                 else
1638                         return connman_ipaddress_set_ipv6(
1639                                         ipconfig->address, address,
1640                                                 gateway, prefix_length);
1641                 break;
1642
1643         case CONNMAN_IPCONFIG_METHOD_DHCP:
1644                 if (ipconfig->method == method)
1645                         return 0;
1646
1647                 ipconfig->method = method;
1648                 break;
1649         }
1650
1651         return 0;
1652 }
1653
1654 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1655                                                         DBusMessageIter *iter)
1656 {
1657         struct connman_ipdevice *ipdevice;
1658         const char *method = "auto";
1659
1660         connman_dbus_dict_append_basic(iter, "Method",
1661                                                 DBUS_TYPE_STRING, &method);
1662
1663         ipdevice = g_hash_table_lookup(ipdevice_hash,
1664                                         GINT_TO_POINTER(ipconfig->index));
1665         if (ipdevice == NULL)
1666                 return;
1667
1668         if (ipdevice->ifname != NULL)
1669                 connman_dbus_dict_append_basic(iter, "Interface",
1670                                         DBUS_TYPE_STRING, &ipdevice->ifname);
1671
1672         if (ipdevice->address != NULL)
1673                 connman_dbus_dict_append_basic(iter, "Address",
1674                                         DBUS_TYPE_STRING, &ipdevice->address);
1675
1676         if (ipdevice->mtu > 0)
1677                 connman_dbus_dict_append_basic(iter, "MTU",
1678                                         DBUS_TYPE_UINT16, &ipdevice->mtu);
1679 }
1680
1681 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1682                 GKeyFile *keyfile, const char *identifier, const char *prefix)
1683 {
1684         const char *method;
1685         char *key;
1686
1687         DBG("ipconfig %p identifier %s", ipconfig, identifier);
1688
1689         key = g_strdup_printf("%smethod", prefix);
1690         method = g_key_file_get_string(keyfile, identifier, key, NULL);
1691         if (method == NULL) {
1692                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1693                         ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
1694                 else
1695                         ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
1696         } else
1697                 ipconfig->method = __connman_ipconfig_string2method(method);
1698         g_free(key);
1699
1700         key = g_strdup_printf("%snetmask_prefixlen", prefix);
1701         ipconfig->address->prefixlen = g_key_file_get_integer(
1702                                 keyfile, identifier, key, NULL);
1703         g_free(key);
1704
1705         key = g_strdup_printf("%slocal_address", prefix);
1706         ipconfig->address->local = g_key_file_get_string(
1707                         keyfile, identifier, key, NULL);
1708         g_free(key);
1709
1710         key = g_strdup_printf("%speer_address", prefix);
1711         ipconfig->address->peer = g_key_file_get_string(
1712                                 keyfile, identifier, key, NULL);
1713         g_free(key);
1714
1715         key = g_strdup_printf("%sbroadcast_address", prefix);
1716         ipconfig->address->broadcast = g_key_file_get_string(
1717                                 keyfile, identifier, key, NULL);
1718         g_free(key);
1719
1720         key = g_strdup_printf("%sgateway", prefix);
1721         ipconfig->address->gateway = g_key_file_get_string(
1722                                 keyfile, identifier, key, NULL);
1723         g_free(key);
1724
1725         return 0;
1726 }
1727
1728 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
1729                 GKeyFile *keyfile, const char *identifier, const char *prefix)
1730 {
1731         const char *method;
1732         char *key;
1733
1734         DBG("ipconfig %p identifier %s", ipconfig, identifier);
1735
1736         method = __connman_ipconfig_method2string(ipconfig->method);
1737
1738         key = g_strdup_printf("%smethod", prefix);
1739         g_key_file_set_string(keyfile, identifier, key, method);
1740         g_free(key);
1741
1742         key = g_strdup_printf("%snetmask_prefixlen", prefix);
1743         g_key_file_set_integer(keyfile, identifier,
1744                         key, ipconfig->address->prefixlen);
1745         g_free(key);
1746
1747         key = g_strdup_printf("%slocal_address", prefix);
1748         if (ipconfig->address->local != NULL)
1749                 g_key_file_set_string(keyfile, identifier,
1750                                 key, ipconfig->address->local);
1751         g_free(key);
1752
1753         key = g_strdup_printf("%speer_address", prefix);
1754         if (ipconfig->address->peer != NULL)
1755                 g_key_file_set_string(keyfile, identifier,
1756                                 key, ipconfig->address->peer);
1757         g_free(key);
1758
1759         key = g_strdup_printf("%sbroadcast_address", prefix);
1760         if (ipconfig->address->broadcast != NULL)
1761                 g_key_file_set_string(keyfile, identifier,
1762                         key, ipconfig->address->broadcast);
1763         g_free(key);
1764
1765         key = g_strdup_printf("%sgateway", prefix);
1766         if (ipconfig->address->gateway != NULL)
1767                 g_key_file_set_string(keyfile, identifier,
1768                         key, ipconfig->address->gateway);
1769         g_free(key);
1770
1771         return 0;
1772 }
1773
1774 int __connman_ipconfig_init(void)
1775 {
1776         DBG("");
1777
1778         ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1779                                                         NULL, free_ipdevice);
1780
1781         return 0;
1782 }
1783
1784 void __connman_ipconfig_cleanup(void)
1785 {
1786         DBG("");
1787
1788         g_hash_table_destroy(ipdevice_hash);
1789         ipdevice_hash = NULL;
1790 }