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)
72 if (data->vpn_phy_index >= 0)
73 connman_inet_del_host_route(data->vpn_phy_index,
75 address = data->vpn_ip;
77 connman_inet_del_host_route(data->index, data->gateway);
78 address = data->gateway;
80 return connman_inet_clear_gateway_address(data->index, address);
83 static void find_element(struct connman_element *element, gpointer user_data)
85 struct gateway_data *data = user_data;
87 DBG("element %p name %s", element, element->name);
89 if (data->element != NULL)
92 if (element->index != data->index)
95 data->element = element;
98 static struct gateway_data *add_gateway(int index, const char *gateway)
100 struct gateway_data *data;
101 struct connman_service *service;
103 data = g_try_new0(struct gateway_data, 1);
108 data->gateway = g_strdup(gateway);
109 data->active = FALSE;
110 data->element = NULL;
113 data->vpn_phy_index = -1;
115 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_CONNECTION,
118 service = __connman_element_get_service(data->element);
119 data->order = __connman_service_get_order(service);
121 gateway_list = g_slist_append(gateway_list, data);
126 static void connection_newgateway(int index, const char *gateway)
128 struct gateway_data *data;
130 DBG("index %d gateway %s", index, gateway);
132 data = find_gateway(index, gateway);
139 static void set_default_gateway(struct gateway_data *data)
141 struct connman_element *element = data->element;
142 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 ifflags = connman_inet_ifflags(element->index);
156 connman_error("Fail to get network interface flags");
160 if (ifflags & IFF_POINTOPOINT) {
161 if (connman_inet_set_gateway_interface(element->index) < 0)
166 connman_inet_add_host_route(element->index, data->gateway);
168 if (connman_inet_set_gateway_address(element->index, data->gateway) < 0)
172 service = __connman_element_get_service(element);
173 __connman_service_indicate_default(service);
176 static struct gateway_data *find_default_gateway(void)
178 struct gateway_data *found = NULL;
179 unsigned int order = 0;
182 for (list = gateway_list; list; list = list->next) {
183 struct gateway_data *data = list->data;
185 if (found == NULL || data->order > order) {
194 static int remove_gateway(struct gateway_data *data)
198 DBG("gateway %s", data->gateway);
200 gateway_list = g_slist_remove(gateway_list, data);
202 if (data->active == TRUE)
203 ret = del_routes(data);
205 g_free(data->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;
241 if (data->active == TRUE)
248 static int connection_probe(struct connman_element *element)
250 struct connman_service *service = NULL;
251 const char *gateway = NULL;
252 const char *vpn_ip = NULL;
253 struct gateway_data *active_gateway = NULL;
254 struct gateway_data *new_gateway = NULL;
256 DBG("element %p name %s", element, element->name);
258 if (element->parent == NULL)
261 /* FIXME: Remove temporarily for the static gateway support */
262 /* if (element->parent->type != CONNMAN_ELEMENT_TYPE_IPV4)
265 connman_element_get_value(element,
266 CONNMAN_PROPERTY_ID_IPV4_GATEWAY, &gateway);
268 connman_element_get_value(element,
269 CONNMAN_PROPERTY_ID_IPV4_ADDRESS, &vpn_ip);
271 DBG("gateway %s", gateway);
273 service = __connman_element_get_service(element);
274 __connman_service_indicate_state(service,
275 CONNMAN_SERVICE_STATE_READY);
277 connman_element_set_enabled(element, TRUE);
282 active_gateway = find_active_gateway();
283 new_gateway = add_gateway(element->index, gateway);
285 if (service == NULL) {
286 new_gateway->vpn = TRUE;
287 new_gateway->vpn_ip = g_strdup(vpn_ip);
288 /* make sure vpn gateway are at higher priority */
289 new_gateway->order = 10;
291 new_gateway->vpn_phy_index = active_gateway->index;
293 new_gateway->vpn = FALSE;
295 if (active_gateway == NULL) {
296 set_default_gateway(new_gateway);
300 if (new_gateway->vpn == TRUE) {
301 connman_inet_add_host_route_vpn(active_gateway->index,
302 active_gateway->gateway,
303 new_gateway->gateway);
306 if (new_gateway->order >= active_gateway->order) {
307 del_routes(active_gateway);
314 static void connection_remove(struct connman_element *element)
316 struct connman_service *service;
317 const char *gateway = NULL;
318 struct gateway_data *data = NULL;
319 gboolean set_default = FALSE;
322 DBG("element %p name %s", element, element->name);
324 service = __connman_element_get_service(element);
325 __connman_service_indicate_state(service,
326 CONNMAN_SERVICE_STATE_DISCONNECT);
328 connman_element_set_enabled(element, FALSE);
330 connman_element_get_value(element,
331 CONNMAN_PROPERTY_ID_IPV4_GATEWAY, &gateway);
333 DBG("gateway %s", gateway);
338 data = find_gateway(element->index, gateway);
342 set_default = data->vpn;
344 if (data->vpn == TRUE && data->vpn_phy_index >= 0)
345 connman_inet_del_host_route(data->vpn_phy_index, data->gateway);
347 ret = remove_gateway(data);
349 /* with vpn this will be called after the network was deleted,
350 * we need to call set_default here because we will not recieve any
351 * gateway delete notification.
352 * We hit the same issue if remove_gateway() fails.
354 if (set_default || ret) {
355 data = find_default_gateway();
357 set_default_gateway(data);
361 static struct connman_driver connection_driver = {
362 .name = "connection",
363 .type = CONNMAN_ELEMENT_TYPE_CONNECTION,
364 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
365 .probe = connection_probe,
366 .remove = connection_remove,
369 int __connman_connection_init(void)
373 if (connman_rtnl_register(&connection_rtnl) < 0)
374 connman_error("Failed to setup RTNL gateway driver");
376 return connman_driver_register(&connection_driver);
379 void __connman_connection_cleanup(void)
385 connman_driver_unregister(&connection_driver);
387 connman_rtnl_unregister(&connection_rtnl);
389 for (list = gateway_list; list; list = list->next) {
390 struct gateway_data *data = list->data;
392 DBG("index %d gateway %s", data->index, data->gateway);
394 g_free(data->gateway);
399 g_slist_free(gateway_list);
403 static void update_order(void)
407 for (list = gateway_list; list; list = list->next) {
408 struct gateway_data *data = list->data;
409 struct connman_service *service;
411 /* vpn gataway is not attached to a service. */
415 service = __connman_element_get_service(data->element);
416 data->order = __connman_service_get_order(service);
420 gboolean __connman_connection_update_gateway(void)
422 struct gateway_data *active_gateway, *default_gateway;
423 gboolean updated = FALSE;
427 active_gateway = find_active_gateway();
428 default_gateway = find_default_gateway();
430 if (active_gateway && active_gateway != default_gateway) {
431 del_routes(active_gateway);