Add static gateway support
[framework/connectivity/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         /* FIXME: Remove temporarily for the static gateway support */
253         /* if (element->parent->type != CONNMAN_ELEMENT_TYPE_IPV4)
254                 return -ENODEV; */
255
256         connman_element_get_value(element,
257                                 CONNMAN_PROPERTY_ID_IPV4_GATEWAY, &gateway);
258
259         connman_element_get_value(element,
260                                   CONNMAN_PROPERTY_ID_IPV4_ADDRESS, &vpn_ip);
261
262         DBG("gateway %s", gateway);
263
264         service = __connman_element_get_service(element);
265         __connman_service_indicate_state(service,
266                                         CONNMAN_SERVICE_STATE_READY);
267
268         connman_element_set_enabled(element, TRUE);
269
270         if (gateway == NULL)
271                 return 0;
272
273         active_gateway = find_active_gateway();
274         new_gateway = add_gateway(element->index, gateway);
275
276         if (service == NULL) {
277                 new_gateway->vpn = TRUE;
278                 new_gateway->vpn_ip = g_strdup(vpn_ip);
279                 /* make sure vpn gateway are at higher priority */
280                 new_gateway->order = 10;
281         } else
282                 new_gateway->vpn = FALSE;
283
284         if (active_gateway == NULL) {
285                 set_default_gateway(new_gateway);
286                 return 0;
287         }
288
289         if (new_gateway->vpn == TRUE) {
290                 connman_inet_add_host_route_vpn(active_gateway->index,
291                                                 active_gateway->gateway,
292                                                 new_gateway->gateway);
293
294                 connman_inet_set_gateway_address(new_gateway->index,
295                                                         new_gateway->gateway);
296         }
297
298         if (new_gateway->order >= active_gateway->order) {
299                 del_routes(active_gateway);
300                 return 0;
301         }
302
303         return 0;
304 }
305
306 static void connection_remove(struct connman_element *element)
307 {
308         struct connman_service *service;
309         const char *gateway = NULL;
310         struct gateway_data *data = NULL;
311         gboolean set_default = FALSE;
312
313         DBG("element %p name %s", element, element->name);
314
315         service = __connman_element_get_service(element);
316         __connman_service_indicate_state(service,
317                                         CONNMAN_SERVICE_STATE_DISCONNECT);
318
319         connman_element_set_enabled(element, FALSE);
320
321         connman_element_get_value(element,
322                                 CONNMAN_PROPERTY_ID_IPV4_GATEWAY, &gateway);
323
324         DBG("gateway %s", gateway);
325
326         if (gateway == NULL)
327                 return;
328
329         data = find_gateway(element->index, gateway);
330         if (data == NULL)
331                 return;
332
333         set_default = data->vpn;
334
335         if (data->vpn == TRUE)
336                 connman_inet_del_host_route(data->index, data->gateway);
337
338         remove_gateway(data);
339
340         /* with vpn this will be called after the network was deleted,
341          * we need to call set_default here because we will not recieve any
342          * gateway delete notification.
343          */
344         if (set_default) {
345                 data = find_default_gateway();
346                 if (data != NULL)
347                         set_default_gateway(data);
348         }
349 }
350
351 static struct connman_driver connection_driver = {
352         .name           = "connection",
353         .type           = CONNMAN_ELEMENT_TYPE_CONNECTION,
354         .priority       = CONNMAN_DRIVER_PRIORITY_LOW,
355         .probe          = connection_probe,
356         .remove         = connection_remove,
357 };
358
359 int __connman_connection_init(void)
360 {
361         DBG("");
362
363         if (connman_rtnl_register(&connection_rtnl) < 0)
364                 connman_error("Failed to setup RTNL gateway driver");
365
366         return connman_driver_register(&connection_driver);
367 }
368
369 void __connman_connection_cleanup(void)
370 {
371         GSList *list;
372
373         DBG("");
374
375         connman_driver_unregister(&connection_driver);
376
377         connman_rtnl_unregister(&connection_rtnl);
378
379         for (list = gateway_list; list; list = list->next) {
380                 struct gateway_data *data = list->data;
381
382                 DBG("index %d gateway %s", data->index, data->gateway);
383
384                 g_free(data->gateway);
385                 g_free(data);
386                 list->data = NULL;
387         }
388
389         g_slist_free(gateway_list);
390         gateway_list = NULL;
391 }
392
393 static void update_order(void)
394 {
395         GSList *list = NULL;
396
397         for (list = gateway_list; list; list = list->next) {
398                 struct gateway_data *data = list->data;
399                 struct connman_service *service;
400
401                 /* vpn gataway is not attached to a service. */
402                 if (data->vpn)
403                         continue;
404
405                 service = __connman_element_get_service(data->element);
406                 data->order = __connman_service_get_order(service);
407         }
408 }
409
410 gboolean __connman_connection_update_gateway(void)
411 {
412         struct gateway_data *active_gateway, *default_gateway;
413         gboolean updated = FALSE;
414
415         update_order();
416
417         active_gateway = find_active_gateway();
418         default_gateway = find_default_gateway();
419
420         if (active_gateway && active_gateway != default_gateway) {
421                 del_routes(active_gateway);
422                 updated = TRUE;
423         }
424
425         return updated;
426 }