Use common set of INET routing helpers
[platform/upstream/connman.git] / src / connection.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
28 #include <gdbus.h>
29
30 #include "connman.h"
31
32 struct gateway_data {
33         int index;
34         char *gateway;
35         struct connman_element *element;
36         unsigned int order;
37         gboolean active;
38         /* VPN extra data */
39         gboolean vpn;
40         char *vpn_ip;
41 };
42
43 static GSList *gateway_list = NULL;
44
45 static struct gateway_data *find_gateway(int index, const char *gateway)
46 {
47         GSList *list;
48
49         if (gateway == NULL)
50                 return NULL;
51
52         for (list = gateway_list; list; list = list->next) {
53                 struct gateway_data *data = list->data;
54
55                 if (data->gateway == NULL)
56                         continue;
57
58                 if (data->index == index &&
59                                 g_str_equal(data->gateway, gateway) == TRUE)
60                         return data;
61         }
62
63         return NULL;
64 }
65
66 static int del_routes(struct gateway_data *data)
67 {
68         const char *address;
69
70         connman_inet_del_host_route(data->index, data->gateway);
71
72         if (data->vpn)
73                 address = data->vpn_ip;
74         else
75                 address = data->gateway;
76
77         return connman_inet_clear_gateway_address(data->index, address);
78 }
79
80 static void find_element(struct connman_element *element, gpointer user_data)
81 {
82         struct gateway_data *data = user_data;
83
84         DBG("element %p name %s", element, element->name);
85
86         if (data->element != NULL)
87                 return;
88
89         if (element->index != data->index)
90                 return;
91
92         data->element = element;
93 }
94
95 static struct gateway_data *add_gateway(int index, const char *gateway)
96 {
97         struct gateway_data *data;
98         struct connman_service *service;
99
100         data = g_try_new0(struct gateway_data, 1);
101         if (data == NULL)
102                 return NULL;
103
104         data->index = index;
105         data->gateway = g_strdup(gateway);
106         data->active = FALSE;
107         data->element = NULL;
108         data->vpn_ip = NULL;
109         data->vpn = FALSE;
110
111         __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_CONNECTION,
112                                                         find_element, data);
113
114         service = __connman_element_get_service(data->element);
115         data->order = __connman_service_get_order(service);
116
117         gateway_list = g_slist_append(gateway_list, data);
118
119         return data;
120 }
121
122 static void connection_newgateway(int index, const char *gateway)
123 {
124         struct gateway_data *data;
125
126         DBG("index %d gateway %s", index, gateway);
127
128         data = find_gateway(index, gateway);
129         if (data == NULL)
130                 return;
131
132         data->active = TRUE;
133 }
134
135 static void set_default_gateway(struct gateway_data *data)
136 {
137         struct connman_element *element = data->element;
138         struct connman_service *service = NULL;
139         short int ifflags;
140
141         DBG("gateway %s", data->gateway);
142
143         if (data->vpn == TRUE) {
144                 connman_inet_set_gateway_address(data->index, data->vpn_ip);
145                 /* vpn gateway going away no changes in services */
146                 return;
147         }
148
149         ifflags = connman_inet_ifflags(element->index);
150         if (ifflags < 0) {
151                 connman_error("Fail to get network interface flags");
152                 return;
153         }
154
155         if (ifflags & IFF_POINTOPOINT) {
156                 if (connman_inet_set_gateway_interface(element->index) < 0)
157                         return;
158                 goto done;
159         }
160
161         connman_inet_add_host_route(element->index, data->gateway);
162
163         if (connman_inet_set_gateway_address(element->index, data->gateway) < 0)
164                 return;
165
166 done:
167         service = __connman_element_get_service(element);
168         __connman_service_indicate_default(service);
169 }
170
171 static struct gateway_data *find_default_gateway(void)
172 {
173         struct gateway_data *found = NULL;
174         unsigned int order = 0;
175         GSList *list;
176
177         for (list = gateway_list; list; list = list->next) {
178                 struct gateway_data *data = list->data;
179
180                 if (found == NULL || data->order > order) {
181                         found = data;
182                         order = data->order;
183                 }
184         }
185
186         return found;
187 }
188
189 static void remove_gateway(struct gateway_data *data)
190 {
191         DBG("gateway %s", data->gateway);
192
193         gateway_list = g_slist_remove(gateway_list, data);
194
195         if (data->active == TRUE)
196                 del_routes(data);
197
198         g_free(data->gateway);
199         g_free(data->vpn_ip);
200         g_free(data);
201 }
202
203 static void connection_delgateway(int index, const char *gateway)
204 {
205         struct gateway_data *data;
206
207         DBG("index %d gateway %s", index, gateway);
208
209         data = find_gateway(index, gateway);
210         if (data != NULL)
211                 data->active = FALSE;
212
213         data = find_default_gateway();
214         if (data != NULL)
215                 set_default_gateway(data);
216 }
217
218 static struct connman_rtnl connection_rtnl = {
219         .name           = "connection",
220         .newgateway     = connection_newgateway,
221         .delgateway     = connection_delgateway,
222 };
223
224 static struct gateway_data *find_active_gateway(void)
225 {
226         GSList *list;
227
228         DBG("");
229
230         for (list = gateway_list; list; list = list->next) {
231                 struct gateway_data *data = list->data;
232                 if (data->active == TRUE)
233                         return data;
234         }
235
236         return NULL;
237 }
238
239 static int connection_probe(struct connman_element *element)
240 {
241         struct connman_service *service = NULL;
242         const char *gateway = NULL;
243         const char *vpn_ip = NULL;
244         struct gateway_data *active_gateway = NULL;
245         struct gateway_data *new_gateway = NULL;
246
247         DBG("element %p name %s", element, element->name);
248
249         if (element->parent == NULL)
250                 return -ENODEV;
251
252         if (element->parent->type != CONNMAN_ELEMENT_TYPE_IPV4)
253                 return -ENODEV;
254
255         connman_element_get_value(element,
256                                 CONNMAN_PROPERTY_ID_IPV4_GATEWAY, &gateway);
257
258         connman_element_get_value(element,
259                                   CONNMAN_PROPERTY_ID_IPV4_ADDRESS, &vpn_ip);
260
261         DBG("gateway %s", gateway);
262
263         service = __connman_element_get_service(element);
264         __connman_service_indicate_state(service,
265                                         CONNMAN_SERVICE_STATE_READY);
266
267         connman_element_set_enabled(element, TRUE);
268
269         if (gateway == NULL)
270                 return 0;
271
272         active_gateway = find_active_gateway();
273         new_gateway = add_gateway(element->index, gateway);
274
275         if (service == NULL) {
276                 new_gateway->vpn = TRUE;
277                 new_gateway->vpn_ip = g_strdup(vpn_ip);
278                 /* make sure vpn gateway are at higher priority */
279                 new_gateway->order = 10;
280         } else
281                 new_gateway->vpn = FALSE;
282
283         if (active_gateway == NULL) {
284                 set_default_gateway(new_gateway);
285                 return 0;
286         }
287
288         if (new_gateway->vpn == TRUE) {
289                 connman_inet_add_host_route(active_gateway->index,
290                                                 active_gateway->gateway);
291
292                 connman_inet_set_gateway_address(new_gateway->index,
293                                                         new_gateway->gateway);
294         }
295
296         if (new_gateway->order >= active_gateway->order) {
297                 del_routes(active_gateway);
298                 return 0;
299         }
300
301         return 0;
302 }
303
304 static void connection_remove(struct connman_element *element)
305 {
306         struct connman_service *service;
307         const char *gateway = NULL;
308         struct gateway_data *data = NULL;
309         gboolean set_default = FALSE;
310
311         DBG("element %p name %s", element, element->name);
312
313         service = __connman_element_get_service(element);
314         __connman_service_indicate_state(service,
315                                         CONNMAN_SERVICE_STATE_DISCONNECT);
316
317         connman_element_set_enabled(element, FALSE);
318
319         connman_element_get_value(element,
320                                 CONNMAN_PROPERTY_ID_IPV4_GATEWAY, &gateway);
321
322         DBG("gateway %s", gateway);
323
324         if (gateway == NULL)
325                 return;
326
327         data = find_gateway(element->index, gateway);
328         if (data == NULL)
329                 return;
330
331         set_default = data->vpn;
332
333         if (data->vpn == TRUE)
334                 connman_inet_del_host_route(data->index, data->gateway);
335
336         remove_gateway(data);
337
338         /* with vpn this will be called after the network was deleted,
339          * we need to call set_default here because we will not recieve any
340          * gateway delete notification.
341          */
342         if (set_default) {
343                 data = find_default_gateway();
344                 if (data != NULL)
345                         set_default_gateway(data);
346         }
347 }
348
349 static struct connman_driver connection_driver = {
350         .name           = "connection",
351         .type           = CONNMAN_ELEMENT_TYPE_CONNECTION,
352         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
353         .probe          = connection_probe,
354         .remove         = connection_remove,
355 };
356
357 int __connman_connection_init(void)
358 {
359         DBG("");
360
361         if (connman_rtnl_register(&connection_rtnl) < 0)
362                 connman_error("Failed to setup RTNL gateway driver");
363
364         return connman_driver_register(&connection_driver);
365 }
366
367 void __connman_connection_cleanup(void)
368 {
369         GSList *list;
370
371         DBG("");
372
373         connman_driver_unregister(&connection_driver);
374
375         connman_rtnl_unregister(&connection_rtnl);
376
377         for (list = gateway_list; list; list = list->next) {
378                 struct gateway_data *data = list->data;
379
380                 DBG("index %d gateway %s", data->index, data->gateway);
381
382                 g_free(data->gateway);
383                 g_free(data);
384                 list->data = NULL;
385         }
386
387         g_slist_free(gateway_list);
388         gateway_list = NULL;
389 }
390
391 static void update_order(void)
392 {
393         GSList *list = NULL;
394
395         for (list = gateway_list; list; list = list->next) {
396                 struct gateway_data *data = list->data;
397                 struct connman_service *service;
398
399                 /* vpn gataway is not attached to a service. */
400                 if (data->vpn)
401                         continue;
402
403                 service = __connman_element_get_service(data->element);
404                 data->order = __connman_service_get_order(service);
405         }
406 }
407
408 gboolean __connman_connection_update_gateway(void)
409 {
410         struct gateway_data *active_gateway, *default_gateway;
411         gboolean updated = FALSE;
412
413         update_order();
414
415         active_gateway = find_active_gateway();
416         default_gateway = find_default_gateway();
417
418         if (active_gateway && active_gateway != default_gateway) {
419                 del_routes(active_gateway);
420                 updated = TRUE;
421         }
422
423         return updated;
424 }