Fix VPN issue when setting up host route
[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_vpn(active_gateway->index,
290                                                 active_gateway->gateway,
291                                                 new_gateway->gateway);
292
293                 connman_inet_set_gateway_address(new_gateway->index,
294                                                         new_gateway->gateway);
295         }
296
297         if (new_gateway->order >= active_gateway->order) {
298                 del_routes(active_gateway);
299                 return 0;
300         }
301
302         return 0;
303 }
304
305 static void connection_remove(struct connman_element *element)
306 {
307         struct connman_service *service;
308         const char *gateway = NULL;
309         struct gateway_data *data = NULL;
310         gboolean set_default = FALSE;
311
312         DBG("element %p name %s", element, element->name);
313
314         service = __connman_element_get_service(element);
315         __connman_service_indicate_state(service,
316                                         CONNMAN_SERVICE_STATE_DISCONNECT);
317
318         connman_element_set_enabled(element, FALSE);
319
320         connman_element_get_value(element,
321                                 CONNMAN_PROPERTY_ID_IPV4_GATEWAY, &gateway);
322
323         DBG("gateway %s", gateway);
324
325         if (gateway == NULL)
326                 return;
327
328         data = find_gateway(element->index, gateway);
329         if (data == NULL)
330                 return;
331
332         set_default = data->vpn;
333
334         if (data->vpn == TRUE)
335                 connman_inet_del_host_route(data->index, data->gateway);
336
337         remove_gateway(data);
338
339         /* with vpn this will be called after the network was deleted,
340          * we need to call set_default here because we will not recieve any
341          * gateway delete notification.
342          */
343         if (set_default) {
344                 data = find_default_gateway();
345                 if (data != NULL)
346                         set_default_gateway(data);
347         }
348 }
349
350 static struct connman_driver connection_driver = {
351         .name           = "connection",
352         .type           = CONNMAN_ELEMENT_TYPE_CONNECTION,
353         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
354         .probe          = connection_probe,
355         .remove         = connection_remove,
356 };
357
358 int __connman_connection_init(void)
359 {
360         DBG("");
361
362         if (connman_rtnl_register(&connection_rtnl) < 0)
363                 connman_error("Failed to setup RTNL gateway driver");
364
365         return connman_driver_register(&connection_driver);
366 }
367
368 void __connman_connection_cleanup(void)
369 {
370         GSList *list;
371
372         DBG("");
373
374         connman_driver_unregister(&connection_driver);
375
376         connman_rtnl_unregister(&connection_rtnl);
377
378         for (list = gateway_list; list; list = list->next) {
379                 struct gateway_data *data = list->data;
380
381                 DBG("index %d gateway %s", data->index, data->gateway);
382
383                 g_free(data->gateway);
384                 g_free(data);
385                 list->data = NULL;
386         }
387
388         g_slist_free(gateway_list);
389         gateway_list = NULL;
390 }
391
392 static void update_order(void)
393 {
394         GSList *list = NULL;
395
396         for (list = gateway_list; list; list = list->next) {
397                 struct gateway_data *data = list->data;
398                 struct connman_service *service;
399
400                 /* vpn gataway is not attached to a service. */
401                 if (data->vpn)
402                         continue;
403
404                 service = __connman_element_get_service(data->element);
405                 data->order = __connman_service_get_order(service);
406         }
407 }
408
409 gboolean __connman_connection_update_gateway(void)
410 {
411         struct gateway_data *active_gateway, *default_gateway;
412         gboolean updated = FALSE;
413
414         update_order();
415
416         active_gateway = find_active_gateway();
417         default_gateway = find_default_gateway();
418
419         if (active_gateway && active_gateway != default_gateway) {
420                 del_routes(active_gateway);
421                 updated = TRUE;
422         }
423
424         return updated;
425 }