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