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
35 struct connman_element *element;
44 static GSList *gateway_list = NULL;
46 static struct gateway_data *find_gateway(int index, const char *gateway)
53 for (list = gateway_list; list; list = list->next) {
54 struct gateway_data *data = list->data;
56 if (data->gateway == NULL)
59 if (data->index == index &&
60 g_str_equal(data->gateway, gateway) == TRUE)
67 static int del_routes(struct gateway_data *data)
70 if (data->vpn_phy_index >= 0)
71 connman_inet_del_host_route(data->vpn_phy_index,
73 return connman_inet_clear_gateway_address(data->index,
75 } else if (g_strcmp0(data->gateway, "0.0.0.0") == 0) {
76 return connman_inet_clear_gateway_interface(data->index);
78 connman_inet_del_host_route(data->index, data->gateway);
79 return connman_inet_clear_gateway_address(data->index,
84 static void find_element(struct connman_element *element, gpointer user_data)
86 struct gateway_data *data = user_data;
88 DBG("element %p name %s", element, element->name);
90 if (data->element != NULL)
93 if (element->index != data->index)
96 data->element = element;
99 static struct gateway_data *add_gateway(int index, const char *gateway)
101 struct gateway_data *data;
102 struct connman_service *service;
104 data = g_try_new0(struct gateway_data, 1);
109 data->gateway = g_strdup(gateway);
110 data->active = FALSE;
111 data->element = NULL;
114 data->vpn_phy_index = -1;
116 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_CONNECTION,
119 service = __connman_element_get_service(data->element);
120 data->order = __connman_service_get_order(service);
122 gateway_list = g_slist_append(gateway_list, data);
127 static void connection_newgateway(int index, const char *gateway)
129 struct gateway_data *data;
131 DBG("index %d gateway %s", index, gateway);
133 data = find_gateway(index, gateway);
140 static void set_default_gateway(struct gateway_data *data)
142 struct connman_element *element = data->element;
143 struct connman_service *service = NULL;
145 DBG("gateway %s", data->gateway);
147 if (data->vpn == TRUE) {
148 connman_inet_set_gateway_address(data->index, data->vpn_ip);
150 /* vpn gateway going away no changes in services */
154 if (g_strcmp0(data->gateway, "0.0.0.0") == 0) {
155 if (connman_inet_set_gateway_interface(element->index) < 0)
160 if (connman_inet_set_gateway_address(element->index, data->gateway) < 0)
164 service = __connman_element_get_service(element);
165 __connman_service_indicate_default(service);
168 static struct gateway_data *find_default_gateway(void)
170 struct gateway_data *found = NULL;
171 unsigned int order = 0;
174 for (list = gateway_list; list; list = list->next) {
175 struct gateway_data *data = list->data;
177 if (found == NULL || data->order > order) {
186 static int remove_gateway(struct gateway_data *data)
190 DBG("gateway %s", data->gateway);
192 gateway_list = g_slist_remove(gateway_list, data);
194 if (data->active == TRUE)
195 err = del_routes(data);
199 g_free(data->gateway);
200 g_free(data->vpn_ip);
206 static void connection_delgateway(int index, const char *gateway)
208 struct gateway_data *data;
210 DBG("index %d gateway %s", index, gateway);
212 data = find_gateway(index, gateway);
214 data->active = FALSE;
216 data = find_default_gateway();
218 set_default_gateway(data);
221 static struct connman_rtnl connection_rtnl = {
222 .name = "connection",
223 .newgateway = connection_newgateway,
224 .delgateway = connection_delgateway,
227 static struct gateway_data *find_active_gateway(void)
233 for (list = gateway_list; list; list = list->next) {
234 struct gateway_data *data = list->data;
236 if (data->active == TRUE)
243 static int connection_probe(struct connman_element *element)
245 struct connman_service *service = NULL;
246 const char *gateway = NULL;
247 const char *vpn_ip = NULL;
248 struct gateway_data *active_gateway = NULL;
249 struct gateway_data *new_gateway = NULL;
251 DBG("element %p name %s", element, element->name);
253 if (element->parent == NULL)
256 /* FIXME: Remove temporarily for the static gateway support */
257 /* if (element->parent->type != CONNMAN_ELEMENT_TYPE_IPV4)
260 connman_element_get_value(element,
261 CONNMAN_PROPERTY_ID_IPV4_GATEWAY, &gateway);
263 connman_element_get_value(element,
264 CONNMAN_PROPERTY_ID_IPV4_ADDRESS, &vpn_ip);
266 DBG("gateway %s", gateway);
269 * If gateway is NULL, it's a point to point link and the default
270 * gateway is 0.0.0.0, meaning the interface.
272 if (gateway == NULL) {
274 element->ipv4.gateway = g_strdup(gateway);
277 connman_element_set_enabled(element, TRUE);
279 active_gateway = find_active_gateway();
280 new_gateway = add_gateway(element->index, gateway);
281 service = __connman_element_get_service(element);
284 connman_inet_add_host_route(element->index,
285 new_gateway->gateway, NULL);
286 __connman_service_nameserver_add_routes(service,
287 new_gateway->gateway);
290 __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_READY);
292 if (service == NULL) {
293 new_gateway->vpn = TRUE;
294 new_gateway->vpn_ip = g_strdup(vpn_ip);
295 /* make sure vpn gateway are at higher priority */
296 new_gateway->order = 10;
298 new_gateway->vpn_phy_index = active_gateway->index;
300 new_gateway->vpn = FALSE;
302 if (active_gateway == NULL) {
303 set_default_gateway(new_gateway);
307 if (new_gateway->vpn == TRUE) {
308 connman_inet_add_host_route(active_gateway->index,
309 new_gateway->gateway,
310 active_gateway->gateway);
313 if (new_gateway->order >= active_gateway->order) {
314 del_routes(active_gateway);
321 static void connection_remove(struct connman_element *element)
323 struct connman_service *service;
324 const char *gateway = NULL;
325 struct gateway_data *data = NULL;
326 gboolean set_default = FALSE;
329 DBG("element %p name %s", element, element->name);
331 service = __connman_element_get_service(element);
332 __connman_service_nameserver_del_routes(service);
333 __connman_service_indicate_state(service,
334 CONNMAN_SERVICE_STATE_DISCONNECT);
336 connman_element_set_enabled(element, FALSE);
338 connman_element_get_value(element,
339 CONNMAN_PROPERTY_ID_IPV4_GATEWAY, &gateway);
341 DBG("gateway %s", gateway);
346 data = find_gateway(element->index, gateway);
350 set_default = data->vpn;
352 if (data->vpn == TRUE && data->vpn_phy_index >= 0)
353 connman_inet_del_host_route(data->vpn_phy_index, data->gateway);
355 err = remove_gateway(data);
357 /* with vpn this will be called after the network was deleted,
358 * we need to call set_default here because we will not recieve any
359 * gateway delete notification.
360 * We hit the same issue if remove_gateway() fails.
362 if (set_default || err < 0) {
363 data = find_default_gateway();
365 set_default_gateway(data);
369 static struct connman_driver connection_driver = {
370 .name = "connection",
371 .type = CONNMAN_ELEMENT_TYPE_CONNECTION,
372 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
373 .probe = connection_probe,
374 .remove = connection_remove,
377 int __connman_connection_init(void)
381 if (connman_rtnl_register(&connection_rtnl) < 0)
382 connman_error("Failed to setup RTNL gateway driver");
384 return connman_driver_register(&connection_driver);
387 void __connman_connection_cleanup(void)
393 connman_driver_unregister(&connection_driver);
395 connman_rtnl_unregister(&connection_rtnl);
397 for (list = gateway_list; list; list = list->next) {
398 struct gateway_data *data = list->data;
400 DBG("index %d gateway %s", data->index, data->gateway);
402 g_free(data->gateway);
407 g_slist_free(gateway_list);
411 static void update_order(void)
415 for (list = gateway_list; list; list = list->next) {
416 struct gateway_data *data = list->data;
417 struct connman_service *service;
419 /* vpn gataway is not attached to a service. */
423 service = __connman_element_get_service(data->element);
424 data->order = __connman_service_get_order(service);
428 gboolean __connman_connection_update_gateway(void)
430 struct gateway_data *active_gateway, *default_gateway;
431 gboolean updated = FALSE;
435 active_gateway = find_active_gateway();
436 default_gateway = find_default_gateway();
438 if (active_gateway && active_gateway != default_gateway) {
439 del_routes(active_gateway);