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
37 struct connman_element *element;
46 static GSList *gateway_list = NULL;
48 static struct gateway_data *find_gateway(int index, const char *gateway)
55 for (list = gateway_list; list; list = list->next) {
56 struct gateway_data *data = list->data;
58 if (data->ipv4_gateway == NULL)
61 if (data->index == index &&
62 g_str_equal(data->ipv4_gateway, gateway)
70 static int del_routes(struct gateway_data *data)
73 if (data->vpn_phy_index >= 0)
74 connman_inet_del_host_route(data->vpn_phy_index,
76 return connman_inet_clear_gateway_address(data->index,
78 } else if (g_strcmp0(data->ipv4_gateway, "0.0.0.0") == 0) {
79 return connman_inet_clear_gateway_interface(data->index);
81 connman_inet_del_ipv6_host_route(data->index,
83 connman_inet_clear_ipv6_gateway_address(data->index,
85 connman_inet_del_host_route(data->index, data->ipv4_gateway);
86 return connman_inet_clear_gateway_address(data->index,
91 static void find_element(struct connman_element *element, gpointer user_data)
93 struct gateway_data *data = user_data;
95 DBG("element %p name %s", element, element->name);
97 if (data->element != NULL)
100 if (element->index != data->index)
103 data->element = element;
106 static struct gateway_data *add_gateway(int index, const char *gateway,
107 const char *ipv6_gateway)
109 struct gateway_data *data;
110 struct connman_service *service;
112 DBG("index %d gateway %s ipv6_gateway %s", index, gateway,
115 if (strlen(gateway) == 0)
118 data = g_try_new0(struct gateway_data, 1);
123 data->ipv4_gateway = g_strdup(gateway);
124 data->ipv6_gateway = g_strdup(ipv6_gateway);
125 data->active = FALSE;
126 data->element = NULL;
129 data->vpn_phy_index = -1;
131 __connman_element_foreach(NULL, CONNMAN_ELEMENT_TYPE_CONNECTION,
134 service = __connman_element_get_service(data->element);
135 data->order = __connman_service_get_order(service);
137 gateway_list = g_slist_append(gateway_list, data);
142 static void connection_newgateway(int index, const char *gateway)
144 struct gateway_data *data;
146 DBG("index %d gateway %s", index, gateway);
148 data = find_gateway(index, gateway);
155 static void set_default_gateway(struct gateway_data *data)
157 struct connman_element *element = data->element;
158 struct connman_service *service = NULL;
160 DBG("gateway %s", data->ipv4_gateway);
162 if (data->vpn == TRUE) {
163 connman_inet_set_gateway_address(data->index, data->vpn_ip);
165 /* vpn gateway going away no changes in services */
169 if (g_strcmp0(data->ipv4_gateway, "0.0.0.0") == 0) {
170 if (connman_inet_set_gateway_interface(element->index) < 0)
175 connman_inet_set_ipv6_gateway_address(element->index,
177 if (connman_inet_set_gateway_address(element->index,
178 data->ipv4_gateway) < 0)
182 service = __connman_element_get_service(element);
183 __connman_service_indicate_default(service);
186 static struct gateway_data *find_default_gateway(void)
188 struct gateway_data *found = NULL;
189 unsigned int order = 0;
192 for (list = gateway_list; list; list = list->next) {
193 struct gateway_data *data = list->data;
195 if (found == NULL || data->order > order) {
204 static int remove_gateway(struct gateway_data *data)
208 DBG("gateway %s", data->ipv4_gateway);
210 gateway_list = g_slist_remove(gateway_list, data);
212 if (data->active == TRUE)
213 err = del_routes(data);
217 g_free(data->ipv4_gateway);
218 g_free(data->vpn_ip);
224 static void connection_delgateway(int index, const char *gateway)
226 struct gateway_data *data;
228 DBG("index %d gateway %s", index, gateway);
230 data = find_gateway(index, gateway);
232 data->active = FALSE;
234 data = find_default_gateway();
236 set_default_gateway(data);
239 static struct connman_rtnl connection_rtnl = {
240 .name = "connection",
241 .newgateway = connection_newgateway,
242 .delgateway = connection_delgateway,
245 static struct gateway_data *find_active_gateway(void)
251 for (list = gateway_list; list; list = list->next) {
252 struct gateway_data *data = list->data;
254 if (data->active == TRUE)
261 static int connection_probe(struct connman_element *element)
263 struct connman_service *service = NULL;
264 const char *gateway = NULL, *ipv6_gateway = NULL;
265 const char *vpn_ip = NULL;
266 struct gateway_data *active_gateway = NULL;
267 struct gateway_data *new_gateway = NULL;
269 DBG("element %p name %s", element, element->name);
271 if (element->parent == NULL)
274 /* FIXME: Remove temporarily for the static gateway support */
275 /* if (element->parent->type != CONNMAN_ELEMENT_TYPE_IPV4)
278 connman_element_get_value(element,
279 CONNMAN_PROPERTY_ID_IPV4_GATEWAY, &gateway);
280 connman_element_get_value(element,
281 CONNMAN_PROPERTY_ID_IPV6_GATEWAY, &ipv6_gateway);
283 connman_element_get_value(element,
284 CONNMAN_PROPERTY_ID_IPV4_ADDRESS, &vpn_ip);
286 DBG("gateway %s, ipv6_gateway %s", gateway, ipv6_gateway);
289 * If gateway is NULL, it's a point to point link and the default
290 * gateway is 0.0.0.0, meaning the interface.
292 if (gateway == NULL) {
294 element->ipv4.gateway = g_strdup(gateway);
297 connman_element_set_enabled(element, TRUE);
299 active_gateway = find_active_gateway();
300 new_gateway = add_gateway(element->index, gateway, ipv6_gateway);
301 if (new_gateway == NULL)
304 service = __connman_element_get_service(element);
306 connman_inet_add_ipv6_host_route(element->index,
307 new_gateway->ipv6_gateway, NULL);
308 connman_inet_add_host_route(element->index,
309 new_gateway->ipv4_gateway, NULL);
310 __connman_service_nameserver_add_routes(service,
311 new_gateway->ipv4_gateway);
313 __connman_service_indicate_state(service, CONNMAN_SERVICE_STATE_READY);
315 if (service == NULL) {
316 new_gateway->vpn = TRUE;
317 new_gateway->vpn_ip = g_strdup(vpn_ip);
318 /* make sure vpn gateway are at higher priority */
319 new_gateway->order = 10;
321 new_gateway->vpn_phy_index = active_gateway->index;
323 new_gateway->vpn = FALSE;
325 if (active_gateway == NULL) {
326 set_default_gateway(new_gateway);
330 if (new_gateway->vpn == TRUE) {
331 connman_inet_add_host_route(active_gateway->index,
332 new_gateway->ipv4_gateway,
333 active_gateway->ipv4_gateway);
336 if (new_gateway->order >= active_gateway->order) {
337 del_routes(active_gateway);
344 static void connection_remove(struct connman_element *element)
346 struct connman_service *service;
347 const char *gateway = NULL;
348 struct gateway_data *data = NULL;
349 gboolean set_default = FALSE;
352 DBG("element %p name %s", element, element->name);
354 service = __connman_element_get_service(element);
355 __connman_service_nameserver_del_routes(service);
356 __connman_service_indicate_state(service,
357 CONNMAN_SERVICE_STATE_DISCONNECT);
359 connman_element_set_enabled(element, FALSE);
361 connman_element_get_value(element,
362 CONNMAN_PROPERTY_ID_IPV4_GATEWAY, &gateway);
364 DBG("gateway %s", gateway);
369 data = find_gateway(element->index, gateway);
373 set_default = data->vpn;
375 if (data->vpn == TRUE && data->vpn_phy_index >= 0)
376 connman_inet_del_host_route(data->vpn_phy_index,
378 err = remove_gateway(data);
380 /* with vpn this will be called after the network was deleted,
381 * we need to call set_default here because we will not recieve any
382 * gateway delete notification.
383 * We hit the same issue if remove_gateway() fails.
385 if (set_default || err < 0) {
386 data = find_default_gateway();
388 set_default_gateway(data);
392 static struct connman_driver connection_driver = {
393 .name = "connection",
394 .type = CONNMAN_ELEMENT_TYPE_CONNECTION,
395 .priority = CONNMAN_DRIVER_PRIORITY_LOW,
396 .probe = connection_probe,
397 .remove = connection_remove,
400 int __connman_connection_init(void)
404 if (connman_rtnl_register(&connection_rtnl) < 0)
405 connman_error("Failed to setup RTNL gateway driver");
407 return connman_driver_register(&connection_driver);
410 void __connman_connection_cleanup(void)
416 connman_driver_unregister(&connection_driver);
418 connman_rtnl_unregister(&connection_rtnl);
420 for (list = gateway_list; list; list = list->next) {
421 struct gateway_data *data = list->data;
423 DBG("index %d gateway %s", data->index, data->ipv4_gateway);
425 g_free(data->ipv4_gateway);
430 g_slist_free(gateway_list);
434 static void update_order(void)
438 for (list = gateway_list; list; list = list->next) {
439 struct gateway_data *data = list->data;
440 struct connman_service *service;
442 /* vpn gataway is not attached to a service. */
446 service = __connman_element_get_service(data->element);
447 data->order = __connman_service_get_order(service);
451 gboolean __connman_connection_update_gateway(void)
453 struct gateway_data *active_gateway, *default_gateway;
454 gboolean updated = FALSE;
458 active_gateway = find_active_gateway();
459 default_gateway = find_default_gateway();
461 if (active_gateway && active_gateway != default_gateway) {
462 del_routes(active_gateway);