5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
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.
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.
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
36 struct connman_element *element;
45 static GSList *gateway_list = NULL;
47 static struct gateway_data *find_gateway(int index, const char *gateway)
54 for (list = gateway_list; list; list = list->next) {
55 struct gateway_data *data = list->data;
57 if (data->ipv4_gateway == NULL)
60 if (data->index == index &&
61 g_str_equal(data->ipv4_gateway, gateway)
69 static int del_routes(struct gateway_data *data)
72 if (data->vpn_phy_index >= 0)
73 connman_inet_del_host_route(data->vpn_phy_index,
75 return connman_inet_clear_gateway_address(data->index,
77 } else if (g_strcmp0(data->ipv4_gateway, "0.0.0.0") == 0) {
78 return connman_inet_clear_gateway_interface(data->index);
80 connman_inet_del_host_route(data->index, data->ipv4_gateway);
81 return connman_inet_clear_gateway_address(data->index,
86 static void find_element(struct connman_element *element, gpointer user_data)
88 struct gateway_data *data = user_data;
90 DBG("element %p name %s", element, element->name);
92 if (data->element != NULL)
95 if (element->index != data->index)
98 data->element = element;
101 static struct gateway_data *add_gateway(int index, const char *gateway)
103 struct gateway_data *data;
104 struct connman_service *service;
106 if (strlen(gateway) == 0)
109 data = g_try_new0(struct gateway_data, 1);
114 data->ipv4_gateway = g_strdup(gateway);
115 data->active = FALSE;
116 data->element = NULL;
119 data->vpn_phy_index = -1;
121 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_CONNECTION,
124 service = __connman_element_get_service(data->element);
125 data->order = __connman_service_get_order(service);
127 gateway_list = g_slist_append(gateway_list, data);
132 static void connection_newgateway(int index, const char *gateway)
134 struct gateway_data *data;
136 DBG("index %d gateway %s", index, gateway);
138 data = find_gateway(index, gateway);
145 static void set_default_gateway(struct gateway_data *data)
147 struct connman_element *element = data->element;
148 struct connman_service *service = NULL;
150 DBG("gateway %s", data->ipv4_gateway);
152 if (data->vpn == TRUE) {
153 connman_inet_set_gateway_address(data->index, data->vpn_ip);
155 /* vpn gateway going away no changes in services */
159 if (g_strcmp0(data->ipv4_gateway, "0.0.0.0") == 0) {
160 if (connman_inet_set_gateway_interface(element->index) < 0)
165 if (connman_inet_set_gateway_address(element->index,
166 data->ipv4_gateway) < 0)
170 service = __connman_element_get_service(element);
171 __connman_service_indicate_default(service);
174 static struct gateway_data *find_default_gateway(void)
176 struct gateway_data *found = NULL;
177 unsigned int order = 0;
180 for (list = gateway_list; list; list = list->next) {
181 struct gateway_data *data = list->data;
183 if (found == NULL || data->order > order) {
192 static int remove_gateway(struct gateway_data *data)
196 DBG("gateway %s", data->ipv4_gateway);
198 gateway_list = g_slist_remove(gateway_list, data);
200 if (data->active == TRUE)
201 err = del_routes(data);
205 g_free(data->ipv4_gateway);
206 g_free(data->vpn_ip);
212 static void connection_delgateway(int index, const char *gateway)
214 struct gateway_data *data;
216 DBG("index %d gateway %s", index, gateway);
218 data = find_gateway(index, gateway);
220 data->active = FALSE;
222 data = find_default_gateway();
224 set_default_gateway(data);
227 static struct connman_rtnl connection_rtnl = {
228 .name = "connection",
229 .newgateway = connection_newgateway,
230 .delgateway = connection_delgateway,
233 static struct gateway_data *find_active_gateway(void)
239 for (list = gateway_list; list; list = list->next) {
240 struct gateway_data *data = list->data;
242 if (data->active == TRUE)
249 static int connection_probe(struct connman_element *element)
251 struct connman_service *service = NULL;
252 const char *gateway = NULL;
253 const char *vpn_ip = NULL;
254 struct gateway_data *active_gateway = NULL;
255 struct gateway_data *new_gateway = NULL;
257 DBG("element %p name %s", element, element->name);
259 if (element->parent == NULL)
262 /* FIXME: Remove temporarily for the static gateway support */
263 /* if (element->parent->type != CONNMAN_ELEMENT_TYPE_IPV4)
266 connman_element_get_value(element,
267 CONNMAN_PROPERTY_ID_IPV4_GATEWAY, &gateway);
269 connman_element_get_value(element,
270 CONNMAN_PROPERTY_ID_IPV4_ADDRESS, &vpn_ip);
272 DBG("gateway %s", gateway);
275 * If gateway is NULL, it's a point to point link and the default
276 * gateway is 0.0.0.0, meaning the interface.
278 if (gateway == NULL) {
280 element->ipv4.gateway = g_strdup(gateway);
283 connman_element_set_enabled(element, TRUE);
285 active_gateway = find_active_gateway();
286 new_gateway = add_gateway(element->index, gateway);
287 if (new_gateway == NULL)
290 service = __connman_element_get_service(element);
292 connman_inet_add_host_route(element->index,
293 new_gateway->ipv4_gateway, NULL);
294 __connman_service_nameserver_add_routes(service,
295 new_gateway->ipv4_gateway);
297 __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_READY);
299 if (service == NULL) {
300 new_gateway->vpn = TRUE;
301 new_gateway->vpn_ip = g_strdup(vpn_ip);
302 /* make sure vpn gateway are at higher priority */
303 new_gateway->order = 10;
305 new_gateway->vpn_phy_index = active_gateway->index;
307 new_gateway->vpn = FALSE;
309 if (active_gateway == NULL) {
310 set_default_gateway(new_gateway);
314 if (new_gateway->vpn == TRUE) {
315 connman_inet_add_host_route(active_gateway->index,
316 new_gateway->ipv4_gateway,
317 active_gateway->ipv4_gateway);
320 if (new_gateway->order >= active_gateway->order) {
321 del_routes(active_gateway);
328 static void connection_remove(struct connman_element *element)
330 struct connman_service *service;
331 const char *gateway = NULL;
332 struct gateway_data *data = NULL;
333 gboolean set_default = FALSE;
336 DBG("element %p name %s", element, element->name);
338 service = __connman_element_get_service(element);
339 __connman_service_nameserver_del_routes(service);
340 __connman_service_indicate_state(service,
341 CONNMAN_SERVICE_STATE_DISCONNECT);
343 connman_element_set_enabled(element, FALSE);
345 connman_element_get_value(element,
346 CONNMAN_PROPERTY_ID_IPV4_GATEWAY, &gateway);
348 DBG("gateway %s", gateway);
353 data = find_gateway(element->index, gateway);
357 set_default = data->vpn;
359 if (data->vpn == TRUE && data->vpn_phy_index >= 0)
360 connman_inet_del_host_route(data->vpn_phy_index,
362 err = remove_gateway(data);
364 /* with vpn this will be called after the network was deleted,
365 * we need to call set_default here because we will not recieve any
366 * gateway delete notification.
367 * We hit the same issue if remove_gateway() fails.
369 if (set_default || err < 0) {
370 data = find_default_gateway();
372 set_default_gateway(data);
376 static struct connman_driver connection_driver = {
377 .name = "connection",
378 .type = CONNMAN_ELEMENT_TYPE_CONNECTION,
379 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
380 .probe = connection_probe,
381 .remove = connection_remove,
384 int __connman_connection_init(void)
388 if (connman_rtnl_register(&connection_rtnl) < 0)
389 connman_error("Failed to setup RTNL gateway driver");
391 return connman_driver_register(&connection_driver);
394 void __connman_connection_cleanup(void)
400 connman_driver_unregister(&connection_driver);
402 connman_rtnl_unregister(&connection_rtnl);
404 for (list = gateway_list; list; list = list->next) {
405 struct gateway_data *data = list->data;
407 DBG("index %d gateway %s", data->index, data->ipv4_gateway);
409 g_free(data->ipv4_gateway);
414 g_slist_free(gateway_list);
418 static void update_order(void)
422 for (list = gateway_list; list; list = list->next) {
423 struct gateway_data *data = list->data;
424 struct connman_service *service;
426 /* vpn gataway is not attached to a service. */
430 service = __connman_element_get_service(data->element);
431 data->order = __connman_service_get_order(service);
435 gboolean __connman_connection_update_gateway(void)
437 struct gateway_data *active_gateway, *default_gateway;
438 gboolean updated = FALSE;
442 active_gateway = find_active_gateway();
443 default_gateway = find_default_gateway();
445 if (active_gateway && active_gateway != default_gateway) {
446 del_routes(active_gateway);