service: __connman_service_notify() prototype change
[platform/upstream/connman.git] / src / ipconfig.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <net/if.h>
27 #include <net/if_arp.h>
28 #include <linux/if_link.h>
29 #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         struct connman_service *service;
427
428         if (stats->rx_packets == 0 && stats->tx_packets == 0)
429                 return;
430
431         connman_info("%s {RX} %u packets %u bytes", ipdevice->ifname,
432                                         stats->rx_packets, stats->rx_bytes);
433         connman_info("%s {TX} %u packets %u bytes", ipdevice->ifname,
434                                         stats->tx_packets, stats->tx_bytes);
435
436         if (ipdevice->config == NULL)
437                 return;
438
439         service = connman_ipconfig_get_data(ipdevice->config);
440         if (service == NULL)
441                 return;
442
443         ipdevice->rx_packets = stats->rx_packets;
444         ipdevice->tx_packets = stats->tx_packets;
445         ipdevice->rx_bytes = stats->rx_bytes;
446         ipdevice->tx_bytes = stats->tx_bytes;
447         ipdevice->rx_errors = stats->rx_errors;
448         ipdevice->tx_errors = stats->tx_errors;
449         ipdevice->rx_dropped = stats->rx_dropped;
450         ipdevice->tx_dropped = stats->tx_dropped;
451
452         __connman_service_notify(service,
453                                 ipdevice->rx_packets, ipdevice->tx_packets,
454                                 ipdevice->rx_bytes, ipdevice->tx_bytes,
455                                 ipdevice->rx_errors, ipdevice->tx_errors,
456                                 ipdevice->rx_dropped, ipdevice->tx_dropped);
457 }
458
459 void __connman_ipconfig_newlink(int index, unsigned short type,
460                                 unsigned int flags, const char *address,
461                                                         unsigned short mtu,
462                                                 struct rtnl_link_stats *stats)
463 {
464         struct connman_ipdevice *ipdevice;
465         GList *list;
466         GString *str;
467         gboolean up = FALSE, down = FALSE;
468         gboolean lower_up = FALSE, lower_down = FALSE;
469
470         DBG("index %d", index);
471
472         if (type == ARPHRD_LOOPBACK)
473                 return;
474
475         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
476         if (ipdevice != NULL)
477                 goto update;
478
479         ipdevice = g_try_new0(struct connman_ipdevice, 1);
480         if (ipdevice == NULL)
481                 return;
482
483         ipdevice->index = index;
484         ipdevice->ifname = connman_inet_ifname(index);
485         ipdevice->type = type;
486
487         ipdevice->address = g_strdup(address);
488
489         g_hash_table_insert(ipdevice_hash, GINT_TO_POINTER(index), ipdevice);
490
491         connman_info("%s {create} index %d type %d <%s>", ipdevice->ifname,
492                                                 index, type, type2str(type));
493
494 update:
495         ipdevice->mtu = mtu;
496
497         update_stats(ipdevice, stats);
498
499         if (flags == ipdevice->flags)
500                 return;
501
502         if ((ipdevice->flags & IFF_UP) != (flags & IFF_UP)) {
503                 if (flags & IFF_UP)
504                         up = TRUE;
505                 else
506                         down = TRUE;
507         }
508
509         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) !=
510                                 (flags & (IFF_RUNNING | IFF_LOWER_UP))) {
511                 if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
512                                         (IFF_RUNNING | IFF_LOWER_UP))
513                         lower_up = TRUE;
514                 else if ((flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
515                         lower_down = TRUE;
516         }
517
518         ipdevice->flags = flags;
519
520         str = g_string_new(NULL);
521         if (str == NULL)
522                 return;
523
524         if (flags & IFF_UP)
525                 g_string_append(str, "UP");
526         else
527                 g_string_append(str, "DOWN");
528
529         if (flags & IFF_RUNNING)
530                 g_string_append(str, ",RUNNING");
531
532         if (flags & IFF_LOWER_UP)
533                 g_string_append(str, ",LOWER_UP");
534
535         connman_info("%s {update} flags %u <%s>", ipdevice->ifname,
536                                                         flags, str->str);
537
538         g_string_free(str, TRUE);
539
540         for (list = g_list_first(ipconfig_list); list;
541                                                 list = g_list_next(list)) {
542                 struct connman_ipconfig *ipconfig = list->data;
543
544                 if (index != ipconfig->index)
545                         continue;
546
547                 if (ipconfig->ops == NULL)
548                         continue;
549
550                 if (up == TRUE && ipconfig->ops->up)
551                         ipconfig->ops->up(ipconfig);
552                 if (lower_up == TRUE && ipconfig->ops->lower_up)
553                         ipconfig->ops->lower_up(ipconfig);
554
555                 if (lower_down == TRUE && ipconfig->ops->lower_down)
556                         ipconfig->ops->lower_down(ipconfig);
557                 if (down == TRUE && ipconfig->ops->down)
558                         ipconfig->ops->down(ipconfig);
559         }
560
561         if (lower_up)
562                 __connman_ipconfig_lower_up(ipdevice);
563         if (lower_down)
564                 __connman_ipconfig_lower_down(ipdevice);
565 }
566
567 void __connman_ipconfig_dellink(int index, struct rtnl_link_stats *stats)
568 {
569         struct connman_ipdevice *ipdevice;
570         GList *list;
571
572         DBG("index %d", index);
573
574         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
575         if (ipdevice == NULL)
576                 return;
577
578         update_stats(ipdevice, stats);
579
580         for (list = g_list_first(ipconfig_list); list;
581                                                 list = g_list_next(list)) {
582                 struct connman_ipconfig *ipconfig = list->data;
583
584                 if (index != ipconfig->index)
585                         continue;
586
587                 ipconfig->index = -1;
588
589                 if (ipconfig->ops == NULL)
590                         continue;
591
592                 if (ipconfig->ops->lower_down)
593                         ipconfig->ops->lower_down(ipconfig);
594                 if (ipconfig->ops->down)
595                         ipconfig->ops->down(ipconfig);
596         }
597
598         __connman_ipconfig_lower_down(ipdevice);
599
600         g_hash_table_remove(ipdevice_hash, GINT_TO_POINTER(index));
601 }
602
603 static inline gint check_duplicate_address(gconstpointer a, gconstpointer b)
604 {
605         const struct connman_ipaddress *addr1 = a;
606         const struct connman_ipaddress *addr2 = b;
607
608         if (addr1->prefixlen != addr2->prefixlen)
609                 return addr2->prefixlen - addr1->prefixlen;
610
611         return g_strcmp0(addr1->local, addr2->local);
612 }
613
614 void __connman_ipconfig_newaddr(int index, int family, const char *label,
615                                 unsigned char prefixlen, const char *address)
616 {
617         struct connman_ipdevice *ipdevice;
618         struct connman_ipaddress *ipaddress;
619         GList *list;
620
621         DBG("index %d", index);
622
623         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
624         if (ipdevice == NULL)
625                 return;
626
627         ipaddress = connman_ipaddress_alloc(family);
628         if (ipaddress == NULL)
629                 return;
630
631         ipaddress->prefixlen = prefixlen;
632         ipaddress->local = g_strdup(address);
633
634         if (g_slist_find_custom(ipdevice->address_list, ipaddress,
635                                         check_duplicate_address)) {
636                 connman_ipaddress_free(ipaddress);
637                 return;
638         }
639
640         ipdevice->address_list = g_slist_append(ipdevice->address_list,
641                                                                 ipaddress);
642
643         connman_info("%s {add} address %s/%u label %s", ipdevice->ifname,
644                                                 address, prefixlen, label);
645
646         if (ipdevice->config != NULL) {
647                 if (family == AF_INET6 && ipdevice->config->ipv6 != NULL)
648                         connman_ipaddress_copy(ipdevice->config->ipv6->system,
649                                                         ipaddress);
650                 else
651                         connman_ipaddress_copy(ipdevice->config->system,
652                                                         ipaddress);
653         }
654
655         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
656                 return;
657
658         for (list = g_list_first(ipconfig_list); list;
659                                                 list = g_list_next(list)) {
660                 struct connman_ipconfig *ipconfig = list->data;
661
662                 if (index != ipconfig->index)
663                         continue;
664
665                 if (ipconfig->ops == NULL)
666                         continue;
667
668                 if (ipconfig->ops->ip_bound)
669                         ipconfig->ops->ip_bound(ipconfig);
670         }
671 }
672
673 void __connman_ipconfig_deladdr(int index, int family, const char *label,
674                                 unsigned char prefixlen, const char *address)
675 {
676         struct connman_ipdevice *ipdevice;
677         struct connman_ipaddress *ipaddress;
678         GList *list;
679
680         DBG("index %d", index);
681
682         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
683         if (ipdevice == NULL)
684                 return;
685
686         ipaddress = find_ipaddress(ipdevice, prefixlen, address);
687         if (ipaddress == NULL)
688                 return;
689
690         ipdevice->address_list = g_slist_remove(ipdevice->address_list,
691                                                                 ipaddress);
692
693         connman_ipaddress_free(ipaddress);
694
695         connman_info("%s {del} address %s/%u label %s", ipdevice->ifname,
696                                                 address, prefixlen, label);
697
698         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) != (IFF_RUNNING | IFF_LOWER_UP))
699                 return;
700
701         if (g_slist_length(ipdevice->address_list) > 0)
702                 return;
703
704         for (list = g_list_first(ipconfig_list); list;
705                                                 list = g_list_next(list)) {
706                 struct connman_ipconfig *ipconfig = list->data;
707
708                 if (index != ipconfig->index)
709                         continue;
710
711                 if (ipconfig->ops == NULL)
712                         continue;
713
714                 if (ipconfig->ops->ip_release)
715                         ipconfig->ops->ip_release(ipconfig);
716         }
717 }
718
719 void __connman_ipconfig_newroute(int index, int family, unsigned char scope,
720                                         const char *dst, const char *gateway)
721 {
722         struct connman_ipdevice *ipdevice;
723
724         DBG("index %d", index);
725
726         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
727         if (ipdevice == NULL)
728                 return;
729
730         if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
731                 GSList *list;
732                 GList *config_list;
733
734                 if (family == AF_INET6) {
735                         g_free(ipdevice->ipv6_gateway);
736                         ipdevice->ipv6_gateway = g_strdup(gateway);
737                 } else {
738                         g_free(ipdevice->ipv4_gateway);
739                         ipdevice->ipv4_gateway = g_strdup(gateway);
740                 }
741
742                 if (ipdevice->config != NULL &&
743                                         ipdevice->config->system != NULL) {
744                         g_free(ipdevice->config->system->gateway);
745                         ipdevice->config->system->gateway = g_strdup(gateway);
746                 }
747
748                 for (list = ipdevice->address_list; list; list = list->next) {
749                         struct connman_ipaddress *ipaddress = list->data;
750
751                         g_free(ipaddress->gateway);
752                         ipaddress->gateway = g_strdup(gateway);
753                 }
754
755                 for (config_list = g_list_first(ipconfig_list); config_list;
756                                         config_list = g_list_next(config_list)) {
757                         struct connman_ipconfig *ipconfig = config_list->data;
758
759                         if (index != ipconfig->index)
760                                 continue;
761
762                         if (ipconfig->ops == NULL)
763                                 continue;
764
765                         if (ipconfig->ops->ip_bound)
766                                 ipconfig->ops->ip_bound(ipconfig);
767                 }
768         }
769
770         connman_info("%s {add} route %s gw %s scope %u <%s>",
771                                         ipdevice->ifname, dst, gateway,
772                                                 scope, scope2str(scope));
773 }
774
775 void __connman_ipconfig_delroute(int index, int family, unsigned char scope,
776                                         const char *dst, const char *gateway)
777 {
778         struct connman_ipdevice *ipdevice;
779
780         DBG("index %d", index);
781
782         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
783         if (ipdevice == NULL)
784                 return;
785
786         if (scope == 0 && g_strcmp0(dst, "0.0.0.0") == 0) {
787                 GSList *list;
788                 GList *config_list;
789
790                 if (family == AF_INET6) {
791                         g_free(ipdevice->ipv6_gateway);
792                         ipdevice->ipv6_gateway = NULL;
793                 } else {
794                         g_free(ipdevice->ipv4_gateway);
795                         ipdevice->ipv4_gateway = NULL;
796                 }
797
798                 if (ipdevice->config != NULL &&
799                                         ipdevice->config->system != NULL) {
800                         g_free(ipdevice->config->system->gateway);
801                         ipdevice->config->system->gateway = NULL;
802                 }
803
804                 for (list = ipdevice->address_list; list; list = list->next) {
805                         struct connman_ipaddress *ipaddress = list->data;
806
807                         g_free(ipaddress->gateway);
808                         ipaddress->gateway = NULL;
809                 }
810
811                 for (config_list = g_list_first(ipconfig_list); config_list;
812                                         config_list = g_list_next(config_list)) {
813                         struct connman_ipconfig *ipconfig = config_list->data;
814
815                         if (index != ipconfig->index)
816                                 continue;
817
818                         if (ipconfig->ops == NULL)
819                                 continue;
820
821                         if (ipconfig->ops->ip_release)
822                                 ipconfig->ops->ip_release(ipconfig);
823                 }
824         }
825
826         connman_info("%s {del} route %s gw %s scope %u <%s>",
827                                         ipdevice->ifname, dst, gateway,
828                                                 scope, scope2str(scope));
829 }
830
831 void __connman_ipconfig_foreach(void (*function) (int index, void *user_data),
832                                                         void *user_data)
833 {
834         GList *list, *keys;
835
836         keys = g_hash_table_get_keys(ipdevice_hash);
837         if (keys == NULL)
838                 return;
839
840         for (list = g_list_first(keys); list; list = g_list_next(list)) {
841                 int index = GPOINTER_TO_INT(list->data);
842
843                 function(index, user_data);
844         }
845
846         g_list_free(keys);
847 }
848
849 unsigned short __connman_ipconfig_get_type(int index)
850 {
851         struct connman_ipdevice *ipdevice;
852
853         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
854         if (ipdevice == NULL)
855                 return ARPHRD_VOID;
856
857         return ipdevice->type;
858 }
859
860 unsigned int __connman_ipconfig_get_flags(int index)
861 {
862         struct connman_ipdevice *ipdevice;
863
864         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
865         if (ipdevice == NULL)
866                 return 0;
867
868         return ipdevice->flags;
869 }
870
871 const char *__connman_ipconfig_get_gateway(int index)
872 {
873         struct connman_ipdevice *ipdevice;
874
875         ipdevice = g_hash_table_lookup(ipdevice_hash, GINT_TO_POINTER(index));
876         if (ipdevice == NULL)
877                 return NULL;
878
879         if (ipdevice->ipv4_gateway != NULL)
880                 return ipdevice->ipv4_gateway;
881
882         if (ipdevice->config != NULL &&
883                         ipdevice->config->address != NULL)
884                 return ipdevice->config->address->gateway;
885
886         return NULL;
887 }
888
889 void __connman_ipconfig_set_index(struct connman_ipconfig *ipconfig, int index)
890 {
891         ipconfig->index = index;
892
893         if (ipconfig->ipv6 != NULL)
894                 ipconfig->ipv6->index = index;
895 }
896
897 static struct connman_ipconfig *create_ipv6config(int index)
898 {
899         struct connman_ipconfig *ipv6config;
900
901         DBG("index %d", index);
902
903         ipv6config = g_try_new0(struct connman_ipconfig, 1);
904         if (ipv6config == NULL)
905                 return NULL;
906
907         ipv6config->index = index;
908         ipv6config->type = CONNMAN_IPCONFIG_TYPE_IPV6;
909         ipv6config->method = CONNMAN_IPCONFIG_METHOD_OFF;
910
911         ipv6config->address = connman_ipaddress_alloc(AF_INET6);
912         if (ipv6config->address == NULL) {
913                 g_free(ipv6config);
914                 return NULL;
915         }
916
917         ipv6config->system = connman_ipaddress_alloc(AF_INET6);
918
919         ipv6config->ipv6 = NULL;
920
921         DBG("ipconfig %p", ipv6config);
922
923         return ipv6config;
924 }
925
926 /**
927  * connman_ipconfig_create:
928  *
929  * Allocate a new ipconfig structure.
930  *
931  * Returns: a newly-allocated #connman_ipconfig structure
932  */
933 struct connman_ipconfig *connman_ipconfig_create(int index)
934 {
935         struct connman_ipconfig *ipconfig;
936
937         DBG("index %d", index);
938
939         ipconfig = g_try_new0(struct connman_ipconfig, 1);
940         if (ipconfig == NULL)
941                 return NULL;
942
943         ipconfig->refcount = 1;
944
945         ipconfig->index = index;
946         ipconfig->type = CONNMAN_IPCONFIG_TYPE_IPV4;
947
948         ipconfig->address = connman_ipaddress_alloc(AF_INET);
949         if (ipconfig->address == NULL) {
950                 g_free(ipconfig);
951                 return NULL;
952         }
953
954         ipconfig->system = connman_ipaddress_alloc(AF_INET);
955
956         ipconfig->ipv6 = create_ipv6config(index);
957
958         DBG("ipconfig %p", ipconfig);
959
960         return ipconfig;
961 }
962
963 /**
964  * connman_ipconfig_clone:
965  *
966  * Clone an ipconfig structure and create new reference.
967  *
968  * Returns: a newly-allocated #connman_ipconfig structure
969  */
970 struct connman_ipconfig *connman_ipconfig_clone(struct connman_ipconfig *ipconfig)
971 {
972         struct connman_ipconfig *ipconfig_clone;
973
974         DBG("ipconfig %p", ipconfig);
975
976         ipconfig_clone = g_try_new0(struct connman_ipconfig, 1);
977         if (ipconfig_clone == NULL)
978                 return NULL;
979
980         ipconfig_clone->refcount = 1;
981
982         ipconfig_clone->origin = connman_ipconfig_ref(ipconfig);
983
984         ipconfig_clone->index = -1;
985
986         return ipconfig_clone;
987 }
988
989 /**
990  * connman_ipconfig_ref:
991  * @ipconfig: ipconfig structure
992  *
993  * Increase reference counter of ipconfig
994  */
995 struct connman_ipconfig *connman_ipconfig_ref(struct connman_ipconfig *ipconfig)
996 {
997         g_atomic_int_inc(&ipconfig->refcount);
998
999         return ipconfig;
1000 }
1001
1002 static void  free_ipv6config(struct connman_ipconfig *ipconfig)
1003 {
1004         if (ipconfig == NULL)
1005                 return;
1006
1007         connman_ipconfig_set_ops(ipconfig, NULL);
1008         connman_ipaddress_free(ipconfig->system);
1009         connman_ipaddress_free(ipconfig->address);
1010         g_free(ipconfig->ipv6);
1011 }
1012
1013 /**
1014  * connman_ipconfig_unref:
1015  * @ipconfig: ipconfig structure
1016  *
1017  * Decrease reference counter of ipconfig
1018  */
1019 void connman_ipconfig_unref(struct connman_ipconfig *ipconfig)
1020 {
1021         if (ipconfig &&
1022                 g_atomic_int_dec_and_test(&ipconfig->refcount) == TRUE) {
1023                 __connman_ipconfig_disable(ipconfig);
1024
1025                 connman_ipconfig_set_ops(ipconfig, NULL);
1026
1027                 if (ipconfig->origin != NULL) {
1028                         connman_ipconfig_unref(ipconfig->origin);
1029                         ipconfig->origin = NULL;
1030                 }
1031
1032                 connman_ipaddress_free(ipconfig->system);
1033                 connman_ipaddress_free(ipconfig->address);
1034                 free_ipv6config(ipconfig->ipv6);
1035                 g_free(ipconfig);
1036         }
1037 }
1038
1039 /**
1040  * connman_ipconfig_get_data:
1041  * @ipconfig: ipconfig structure
1042  *
1043  * Get private data pointer
1044  */
1045 void *connman_ipconfig_get_data(struct connman_ipconfig *ipconfig)
1046 {
1047         return ipconfig->ops_data;
1048 }
1049
1050 /**
1051  * connman_ipconfig_set_data:
1052  * @ipconfig: ipconfig structure
1053  * @data: data pointer
1054  *
1055  * Set private data pointer
1056  */
1057 void connman_ipconfig_set_data(struct connman_ipconfig *ipconfig, void *data)
1058 {
1059         ipconfig->ops_data = data;
1060 }
1061
1062 /**
1063  * connman_ipconfig_get_index:
1064  * @ipconfig: ipconfig structure
1065  *
1066  * Get interface index
1067  */
1068 int connman_ipconfig_get_index(struct connman_ipconfig *ipconfig)
1069 {
1070         if (ipconfig == NULL)
1071                 return -1;
1072
1073         if (ipconfig->origin != NULL)
1074                 return ipconfig->origin->index;
1075
1076         return ipconfig->index;
1077 }
1078
1079 /**
1080  * connman_ipconfig_get_ifname:
1081  * @ipconfig: ipconfig structure
1082  *
1083  * Get interface name
1084  */
1085 const char *connman_ipconfig_get_ifname(struct connman_ipconfig *ipconfig)
1086 {
1087         struct connman_ipdevice *ipdevice;
1088
1089         if (ipconfig == NULL)
1090                 return NULL;
1091
1092         if (ipconfig->index < 0)
1093                 return NULL;
1094
1095         ipdevice = g_hash_table_lookup(ipdevice_hash,
1096                                         GINT_TO_POINTER(ipconfig->index));
1097         if (ipdevice == NULL)
1098                 return NULL;
1099
1100         return ipdevice->ifname;
1101 }
1102
1103 /**
1104  * connman_ipconfig_set_ops:
1105  * @ipconfig: ipconfig structure
1106  * @ops: operation callbacks
1107  *
1108  * Set the operation callbacks
1109  */
1110 void connman_ipconfig_set_ops(struct connman_ipconfig *ipconfig,
1111                                 const struct connman_ipconfig_ops *ops)
1112 {
1113         ipconfig->ops = ops;
1114 }
1115
1116 struct connman_ipconfig *connman_ipconfig_get_ipv6config(
1117                                 struct connman_ipconfig *ipconfig)
1118 {
1119         if (ipconfig == NULL)
1120                 return NULL;
1121
1122         return ipconfig->ipv6;
1123 }
1124
1125 /**
1126  * connman_ipconfig_set_method:
1127  * @ipconfig: ipconfig structure
1128  * @method: configuration method
1129  *
1130  * Set the configuration method
1131  */
1132 int connman_ipconfig_set_method(struct connman_ipconfig *ipconfig,
1133                                         enum connman_ipconfig_method method)
1134 {
1135         ipconfig->method = method;
1136
1137         return 0;
1138 }
1139
1140 enum connman_ipconfig_method __connman_ipconfig_get_method(struct connman_ipconfig *ipconfig)
1141 {
1142         if (ipconfig == NULL)
1143                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1144
1145         return ipconfig->method;
1146 }
1147
1148 /**
1149  * connman_ipconfig_bind:
1150  * @ipconfig: ipconfig structure
1151  * @ipaddress: ipaddress structure
1152  *
1153  * Bind IP address details to configuration
1154  */
1155 void connman_ipconfig_bind(struct connman_ipconfig *ipconfig,
1156                                         struct connman_ipaddress *ipaddress)
1157 {
1158         struct connman_ipconfig *origin;
1159
1160         origin = ipconfig->origin ? ipconfig->origin : ipconfig;
1161
1162         connman_ipaddress_copy(origin->address, ipaddress);
1163
1164         connman_inet_set_address(origin->index, origin->address);
1165 }
1166
1167 void __connman_ipconfig_set_element_ipv6_gateway(
1168                         struct connman_ipconfig *ipconfig,
1169                                 struct connman_element *element)
1170 {
1171         element->ipv6.gateway = ipconfig->ipv6->address->gateway;
1172 }
1173
1174 /*
1175  * FIXME: The element soulution should be removed in the future
1176  * Set IPv4 and IPv6 gateway
1177  */
1178 int __connman_ipconfig_set_gateway(struct connman_ipconfig *ipconfig,
1179                                                 struct connman_element *parent)
1180 {
1181         struct connman_element *connection;
1182
1183         connection = connman_element_create(NULL);
1184
1185         DBG("ipconfig %p", ipconfig);
1186
1187         connection->type  = CONNMAN_ELEMENT_TYPE_CONNECTION;
1188         connection->index = ipconfig->index;
1189         connection->ipv4.gateway = ipconfig->address->gateway;
1190         connection->ipv6.gateway = ipconfig->ipv6->address->gateway;
1191
1192         if (connman_element_register(connection, parent) < 0)
1193                 connman_element_unref(connection);
1194
1195         return 0;
1196 }
1197
1198 int __connman_ipconfig_set_address(struct connman_ipconfig *ipconfig)
1199 {
1200         DBG("");
1201
1202         switch (ipconfig->method) {
1203         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1204         case CONNMAN_IPCONFIG_METHOD_OFF:
1205         case CONNMAN_IPCONFIG_METHOD_FIXED:
1206         case CONNMAN_IPCONFIG_METHOD_DHCP:
1207                 break;
1208         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1209                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1210                         return connman_inet_set_address(ipconfig->index,
1211                                                         ipconfig->address);
1212                 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1213                         return connman_inet_set_ipv6_address(
1214                                         ipconfig->index, ipconfig->address);
1215         }
1216
1217         return 0;
1218 }
1219
1220 int __connman_ipconfig_clear_address(struct connman_ipconfig *ipconfig)
1221 {
1222         DBG("");
1223
1224         if (ipconfig == NULL)
1225                 return 0;
1226
1227         DBG("method %d", ipconfig->method);
1228
1229         switch (ipconfig->method) {
1230         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1231         case CONNMAN_IPCONFIG_METHOD_OFF:
1232         case CONNMAN_IPCONFIG_METHOD_FIXED:
1233         case CONNMAN_IPCONFIG_METHOD_DHCP:
1234                 break;
1235         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1236                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1237                         return connman_inet_clear_address(ipconfig->index,
1238                                                         ipconfig->address);
1239                 else if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV6)
1240                         return connman_inet_clear_ipv6_address(
1241                                                 ipconfig->index,
1242                                                 ipconfig->address->local,
1243                                                 ipconfig->address->prefixlen);
1244         }
1245
1246         return 0;
1247 }
1248
1249 int __connman_ipconfig_set_proxy_autoconfig(struct connman_ipconfig *ipconfig,
1250                                                         const char *url)
1251 {
1252         struct connman_ipdevice *ipdevice;
1253
1254         DBG("ipconfig %p", ipconfig);
1255
1256         if (ipconfig == NULL || ipconfig->index < 0)
1257                 return -ENODEV;
1258
1259         ipdevice = g_hash_table_lookup(ipdevice_hash,
1260                                         GINT_TO_POINTER(ipconfig->index));
1261         if (ipdevice == NULL)
1262                 return -ENXIO;
1263
1264         g_free(ipdevice->pac);
1265         ipdevice->pac = g_strdup(url);
1266
1267         return 0;
1268 }
1269
1270 const char *__connman_ipconfig_get_proxy_autoconfig(struct connman_ipconfig *ipconfig)
1271 {
1272         struct connman_ipdevice *ipdevice;
1273
1274         DBG("ipconfig %p", ipconfig);
1275
1276         if (ipconfig == NULL || ipconfig->index < 0)
1277                 return NULL;
1278
1279         ipdevice = g_hash_table_lookup(ipdevice_hash,
1280                                         GINT_TO_POINTER(ipconfig->index));
1281         if (ipdevice == NULL)
1282                 return NULL;
1283
1284         return ipdevice->pac;
1285 }
1286
1287 int __connman_ipconfig_enable(struct connman_ipconfig *ipconfig)
1288 {
1289         struct connman_ipdevice *ipdevice;
1290         gboolean up = FALSE, down = FALSE;
1291         gboolean lower_up = FALSE, lower_down = FALSE;
1292
1293         DBG("ipconfig %p", ipconfig);
1294
1295         if (ipconfig == NULL || ipconfig->index < 0)
1296                 return -ENODEV;
1297
1298         ipdevice = g_hash_table_lookup(ipdevice_hash,
1299                                         GINT_TO_POINTER(ipconfig->index));
1300         if (ipdevice == NULL)
1301                 return -ENXIO;
1302
1303         if (ipdevice->config == ipconfig)
1304                 return -EALREADY;
1305
1306         if (ipdevice->config != NULL) {
1307                 ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1308
1309                 connman_ipaddress_clear(ipdevice->config->system);
1310
1311                 connman_ipconfig_unref(ipdevice->config);
1312         }
1313
1314         ipdevice->config = connman_ipconfig_ref(ipconfig);
1315
1316         ipconfig_list = g_list_append(ipconfig_list, ipconfig);
1317
1318         if (ipdevice->flags & IFF_UP)
1319                 up = TRUE;
1320         else
1321                 down = TRUE;
1322
1323         if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
1324                         (IFF_RUNNING | IFF_LOWER_UP))
1325                 lower_up = TRUE;
1326         else if ((ipdevice->flags & (IFF_RUNNING | IFF_LOWER_UP)) == 0)
1327                 lower_down = TRUE;
1328
1329         if (up == TRUE && ipconfig->ops->up)
1330                 ipconfig->ops->up(ipconfig);
1331         if (lower_up == TRUE && ipconfig->ops->lower_up)
1332                 ipconfig->ops->lower_up(ipconfig);
1333
1334         if (lower_down == TRUE && ipconfig->ops->lower_down)
1335                 ipconfig->ops->lower_down(ipconfig);
1336         if (down == TRUE && ipconfig->ops->down)
1337                 ipconfig->ops->down(ipconfig);
1338
1339         return 0;
1340 }
1341
1342 int __connman_ipconfig_disable(struct connman_ipconfig *ipconfig)
1343 {
1344         struct connman_ipdevice *ipdevice;
1345
1346         DBG("ipconfig %p", ipconfig);
1347
1348         if (ipconfig == NULL || ipconfig->index < 0)
1349                 return -ENODEV;
1350
1351         ipdevice = g_hash_table_lookup(ipdevice_hash,
1352                                         GINT_TO_POINTER(ipconfig->index));
1353         if (ipdevice == NULL)
1354                 return -ENXIO;
1355
1356         if (ipdevice->config == NULL || ipdevice->config != ipconfig)
1357                 return -EINVAL;
1358
1359         ipconfig_list = g_list_remove(ipconfig_list, ipconfig);
1360
1361         connman_ipaddress_clear(ipdevice->config->system);
1362         connman_ipaddress_clear(ipdevice->config->ipv6->system);
1363
1364         connman_ipconfig_unref(ipdevice->config);
1365         ipdevice->config = NULL;
1366
1367         return 0;
1368 }
1369
1370 const char *__connman_ipconfig_method2string(enum connman_ipconfig_method method)
1371 {
1372         switch (method) {
1373         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1374                 break;
1375         case CONNMAN_IPCONFIG_METHOD_OFF:
1376                 return "off";
1377         case CONNMAN_IPCONFIG_METHOD_FIXED:
1378                 return "fixed";
1379         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1380                 return "manual";
1381         case CONNMAN_IPCONFIG_METHOD_DHCP:
1382                 return "dhcp";
1383         }
1384
1385         return NULL;
1386 }
1387
1388 enum connman_ipconfig_method __connman_ipconfig_string2method(const char *method)
1389 {
1390         if (g_strcmp0(method, "off") == 0)
1391                 return CONNMAN_IPCONFIG_METHOD_OFF;
1392         else if (g_strcmp0(method, "fixed") == 0)
1393                 return CONNMAN_IPCONFIG_METHOD_FIXED;
1394         else if (g_strcmp0(method, "manual") == 0)
1395                 return CONNMAN_IPCONFIG_METHOD_MANUAL;
1396         else if (g_strcmp0(method, "dhcp") == 0)
1397                 return CONNMAN_IPCONFIG_METHOD_DHCP;
1398         else
1399                 return CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1400 }
1401
1402 void __connman_ipconfig_append_ipv4(struct connman_ipconfig *ipconfig,
1403                                                         DBusMessageIter *iter)
1404 {
1405         const char *str;
1406
1407         DBG("");
1408
1409         str = __connman_ipconfig_method2string(ipconfig->method);
1410         if (str == NULL)
1411                 return;
1412
1413         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1414
1415         if (ipconfig->system == NULL)
1416                 return;
1417
1418         if (ipconfig->system->local != NULL) {
1419                 in_addr_t addr;
1420                 struct in_addr netmask;
1421                 char *mask;
1422
1423                 connman_dbus_dict_append_basic(iter, "Address",
1424                                 DBUS_TYPE_STRING, &ipconfig->system->local);
1425
1426                 addr = 0xffffffff << (32 - ipconfig->system->prefixlen);
1427                 netmask.s_addr = htonl(addr);
1428                 mask = inet_ntoa(netmask);
1429                 connman_dbus_dict_append_basic(iter, "Netmask",
1430                                                 DBUS_TYPE_STRING, &mask);
1431         }
1432
1433         if (ipconfig->system->gateway != NULL)
1434                 connman_dbus_dict_append_basic(iter, "Gateway",
1435                                 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1436 }
1437
1438 void __connman_ipconfig_append_ipv6(struct connman_ipconfig *ipconfig,
1439                                                         DBusMessageIter *iter)
1440 {
1441         const char *str;
1442
1443         DBG("");
1444
1445         str = __connman_ipconfig_method2string(ipconfig->method);
1446         if (str == NULL)
1447                 return;
1448
1449         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1450
1451         if (ipconfig->system == NULL)
1452                 return;
1453
1454         if (ipconfig->system->local != NULL) {
1455                 connman_dbus_dict_append_basic(iter, "Address",
1456                                 DBUS_TYPE_STRING, &ipconfig->system->local);
1457                 connman_dbus_dict_append_basic(iter, "PrefixLength",
1458                                                 DBUS_TYPE_BYTE,
1459                                                 &ipconfig->system->prefixlen);
1460         }
1461
1462         if (ipconfig->system->gateway != NULL)
1463                 connman_dbus_dict_append_basic(iter, "Gateway",
1464                                 DBUS_TYPE_STRING, &ipconfig->system->gateway);
1465 }
1466
1467 void __connman_ipconfig_append_ipv6config(struct connman_ipconfig *ipconfig,
1468                                                         DBusMessageIter *iter)
1469 {
1470         const char *str;
1471
1472         DBG("");
1473
1474         str = __connman_ipconfig_method2string(ipconfig->method);
1475         if (str == NULL)
1476                 return;
1477
1478         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1479
1480         switch (ipconfig->method) {
1481         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1482         case CONNMAN_IPCONFIG_METHOD_OFF:
1483         case CONNMAN_IPCONFIG_METHOD_DHCP:
1484                 return;
1485         case CONNMAN_IPCONFIG_METHOD_FIXED:
1486         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1487                 break;
1488         }
1489
1490         if (ipconfig->address == NULL)
1491                 return;
1492
1493         if (ipconfig->address->local != NULL) {
1494                 connman_dbus_dict_append_basic(iter, "Address",
1495                                 DBUS_TYPE_STRING, &ipconfig->address->local);
1496                 connman_dbus_dict_append_basic(iter, "PrefixLength",
1497                                                 DBUS_TYPE_BYTE,
1498                                                 &ipconfig->address->prefixlen);
1499         }
1500
1501         if (ipconfig->address->gateway != NULL)
1502                 connman_dbus_dict_append_basic(iter, "Gateway",
1503                                 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1504 }
1505
1506 void __connman_ipconfig_append_ipv4config(struct connman_ipconfig *ipconfig,
1507                                                         DBusMessageIter *iter)
1508 {
1509         const char *str;
1510
1511         DBG("");
1512
1513         str = __connman_ipconfig_method2string(ipconfig->method);
1514         if (str == NULL)
1515                 return;
1516
1517         connman_dbus_dict_append_basic(iter, "Method", DBUS_TYPE_STRING, &str);
1518
1519         switch (ipconfig->method) {
1520         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1521         case CONNMAN_IPCONFIG_METHOD_OFF:
1522         case CONNMAN_IPCONFIG_METHOD_FIXED:
1523         case CONNMAN_IPCONFIG_METHOD_DHCP:
1524                 return;
1525         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1526                 break;
1527         }
1528
1529         if (ipconfig->address == NULL)
1530                 return;
1531
1532         if (ipconfig->address->local != NULL) {
1533                 in_addr_t addr;
1534                 struct in_addr netmask;
1535                 char *mask;
1536
1537                 connman_dbus_dict_append_basic(iter, "Address",
1538                                 DBUS_TYPE_STRING, &ipconfig->address->local);
1539
1540                 addr = 0xffffffff << (32 - ipconfig->address->prefixlen);
1541                 netmask.s_addr = htonl(addr);
1542                 mask = inet_ntoa(netmask);
1543                 connman_dbus_dict_append_basic(iter, "Netmask",
1544                                                 DBUS_TYPE_STRING, &mask);
1545         }
1546
1547         if (ipconfig->address->gateway != NULL)
1548                 connman_dbus_dict_append_basic(iter, "Gateway",
1549                                 DBUS_TYPE_STRING, &ipconfig->address->gateway);
1550 }
1551
1552 int __connman_ipconfig_set_config(struct connman_ipconfig *ipconfig,
1553                 enum connman_ipconfig_type type, DBusMessageIter *array)
1554 {
1555         enum connman_ipconfig_method method = CONNMAN_IPCONFIG_METHOD_UNKNOWN;
1556         const char *address = NULL, *netmask = NULL, *gateway = NULL,
1557                         *prefix_length_string = NULL;
1558         int prefix_length = 0;
1559         DBusMessageIter dict;
1560
1561         DBG("ipconfig %p type %d", ipconfig, type);
1562
1563         if (type != CONNMAN_IPCONFIG_TYPE_IPV4 &&
1564                         type != CONNMAN_IPCONFIG_TYPE_IPV6)
1565                 return -EINVAL;
1566
1567         if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY)
1568                 return -EINVAL;
1569
1570         dbus_message_iter_recurse(array, &dict);
1571
1572         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1573                 DBusMessageIter entry;
1574                 const char *key;
1575                 int type;
1576
1577                 dbus_message_iter_recurse(&dict, &entry);
1578
1579                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
1580                         return -EINVAL;
1581
1582                 dbus_message_iter_get_basic(&entry, &key);
1583                 dbus_message_iter_next(&entry);
1584
1585                 type = dbus_message_iter_get_arg_type(&entry);
1586
1587                 if (g_str_equal(key, "Method") == TRUE) {
1588                         const char *str;
1589
1590                         if (type != DBUS_TYPE_STRING)
1591                                 return -EINVAL;
1592
1593                         dbus_message_iter_get_basic(&entry, &str);
1594                         method = __connman_ipconfig_string2method(str);
1595                 } else if (g_str_equal(key, "Address") == TRUE) {
1596                         if (type != DBUS_TYPE_STRING)
1597                                 return -EINVAL;
1598
1599                         dbus_message_iter_get_basic(&entry, &address);
1600                 } else if (g_str_equal(key, "PrefixLength") == TRUE) {
1601                         if (type != DBUS_TYPE_STRING)
1602                                 return -EINVAL;
1603
1604                         dbus_message_iter_get_basic(&entry,
1605                                                         &prefix_length_string);
1606
1607                         prefix_length = atoi(prefix_length_string);
1608                         if (prefix_length < 0 || prefix_length > 128)
1609                                 return -EINVAL;
1610
1611                 } else if (g_str_equal(key, "Netmask") == TRUE) {
1612                         if (type != DBUS_TYPE_STRING)
1613                                 return -EINVAL;
1614
1615                         dbus_message_iter_get_basic(&entry, &netmask);
1616                 } else if (g_str_equal(key, "Gateway") == TRUE) {
1617                         if (type != DBUS_TYPE_STRING)
1618                                 return -EINVAL;
1619
1620                         dbus_message_iter_get_basic(&entry, &gateway);
1621                 }
1622                 dbus_message_iter_next(&dict);
1623         }
1624
1625         DBG("method %d address %s netmask %s gateway %s prefix_length %d",
1626                         method, address, netmask, gateway, prefix_length);
1627
1628         switch (method) {
1629         case CONNMAN_IPCONFIG_METHOD_UNKNOWN:
1630         case CONNMAN_IPCONFIG_METHOD_OFF:
1631         case CONNMAN_IPCONFIG_METHOD_FIXED:
1632                 return -EINVAL;
1633
1634         case CONNMAN_IPCONFIG_METHOD_MANUAL:
1635                 if (address == NULL)
1636                         return -EINVAL;
1637
1638                 ipconfig->method = method;
1639
1640                 if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
1641                         connman_ipaddress_set_ipv4(ipconfig->address,
1642                                                 address, netmask, gateway);
1643                 else
1644                         return connman_ipaddress_set_ipv6(
1645                                         ipconfig->address, address,
1646                                                 gateway, prefix_length);
1647                 break;
1648
1649         case CONNMAN_IPCONFIG_METHOD_DHCP:
1650                 if (ipconfig->method == method)
1651                         return 0;
1652
1653                 ipconfig->method = method;
1654                 break;
1655         }
1656
1657         return 0;
1658 }
1659
1660 void __connman_ipconfig_append_ethernet(struct connman_ipconfig *ipconfig,
1661                                                         DBusMessageIter *iter)
1662 {
1663         struct connman_ipdevice *ipdevice;
1664         const char *method = "auto";
1665
1666         connman_dbus_dict_append_basic(iter, "Method",
1667                                                 DBUS_TYPE_STRING, &method);
1668
1669         ipdevice = g_hash_table_lookup(ipdevice_hash,
1670                                         GINT_TO_POINTER(ipconfig->index));
1671         if (ipdevice == NULL)
1672                 return;
1673
1674         if (ipdevice->ifname != NULL)
1675                 connman_dbus_dict_append_basic(iter, "Interface",
1676                                         DBUS_TYPE_STRING, &ipdevice->ifname);
1677
1678         if (ipdevice->address != NULL)
1679                 connman_dbus_dict_append_basic(iter, "Address",
1680                                         DBUS_TYPE_STRING, &ipdevice->address);
1681
1682         if (ipdevice->mtu > 0)
1683                 connman_dbus_dict_append_basic(iter, "MTU",
1684                                         DBUS_TYPE_UINT16, &ipdevice->mtu);
1685 }
1686
1687 int __connman_ipconfig_load(struct connman_ipconfig *ipconfig,
1688                 GKeyFile *keyfile, const char *identifier, const char *prefix)
1689 {
1690         const char *method;
1691         char *key;
1692
1693         DBG("ipconfig %p identifier %s", ipconfig, identifier);
1694
1695         key = g_strdup_printf("%smethod", prefix);
1696         method = g_key_file_get_string(keyfile, identifier, key, NULL);
1697         if (method == NULL) {
1698                 if (ipconfig->type == CONNMAN_IPCONFIG_TYPE_IPV4)
1699                         ipconfig->method = CONNMAN_IPCONFIG_METHOD_DHCP;
1700                 else
1701                         ipconfig->method = CONNMAN_IPCONFIG_METHOD_OFF;
1702         } else
1703                 ipconfig->method = __connman_ipconfig_string2method(method);
1704         g_free(key);
1705
1706         key = g_strdup_printf("%snetmask_prefixlen", prefix);
1707         ipconfig->address->prefixlen = g_key_file_get_integer(
1708                                 keyfile, identifier, key, NULL);
1709         g_free(key);
1710
1711         key = g_strdup_printf("%slocal_address", prefix);
1712         ipconfig->address->local = g_key_file_get_string(
1713                         keyfile, identifier, key, NULL);
1714         g_free(key);
1715
1716         key = g_strdup_printf("%speer_address", prefix);
1717         ipconfig->address->peer = g_key_file_get_string(
1718                                 keyfile, identifier, key, NULL);
1719         g_free(key);
1720
1721         key = g_strdup_printf("%sbroadcast_address", prefix);
1722         ipconfig->address->broadcast = g_key_file_get_string(
1723                                 keyfile, identifier, key, NULL);
1724         g_free(key);
1725
1726         key = g_strdup_printf("%sgateway", prefix);
1727         ipconfig->address->gateway = g_key_file_get_string(
1728                                 keyfile, identifier, key, NULL);
1729         g_free(key);
1730
1731         return 0;
1732 }
1733
1734 int __connman_ipconfig_save(struct connman_ipconfig *ipconfig,
1735                 GKeyFile *keyfile, const char *identifier, const char *prefix)
1736 {
1737         const char *method;
1738         char *key;
1739
1740         DBG("ipconfig %p identifier %s", ipconfig, identifier);
1741
1742         method = __connman_ipconfig_method2string(ipconfig->method);
1743
1744         key = g_strdup_printf("%smethod", prefix);
1745         g_key_file_set_string(keyfile, identifier, key, method);
1746         g_free(key);
1747
1748         key = g_strdup_printf("%snetmask_prefixlen", prefix);
1749         g_key_file_set_integer(keyfile, identifier,
1750                         key, ipconfig->address->prefixlen);
1751         g_free(key);
1752
1753         key = g_strdup_printf("%slocal_address", prefix);
1754         if (ipconfig->address->local != NULL)
1755                 g_key_file_set_string(keyfile, identifier,
1756                                 key, ipconfig->address->local);
1757         g_free(key);
1758
1759         key = g_strdup_printf("%speer_address", prefix);
1760         if (ipconfig->address->peer != NULL)
1761                 g_key_file_set_string(keyfile, identifier,
1762                                 key, ipconfig->address->peer);
1763         g_free(key);
1764
1765         key = g_strdup_printf("%sbroadcast_address", prefix);
1766         if (ipconfig->address->broadcast != NULL)
1767                 g_key_file_set_string(keyfile, identifier,
1768                         key, ipconfig->address->broadcast);
1769         g_free(key);
1770
1771         key = g_strdup_printf("%sgateway", prefix);
1772         if (ipconfig->address->gateway != NULL)
1773                 g_key_file_set_string(keyfile, identifier,
1774                         key, ipconfig->address->gateway);
1775         g_free(key);
1776
1777         return 0;
1778 }
1779
1780 int __connman_ipconfig_init(void)
1781 {
1782         DBG("");
1783
1784         ipdevice_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
1785                                                         NULL, free_ipdevice);
1786
1787         return 0;
1788 }
1789
1790 void __connman_ipconfig_cleanup(void)
1791 {
1792         DBG("");
1793
1794         g_hash_table_destroy(ipdevice_hash);
1795         ipdevice_hash = NULL;
1796 }