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