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