5 * Copyright (C) 2013 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
27 #include <sys/types.h>
34 #include <gdhcp/gdhcp.h>
36 #define DEFAULT_ROUTER_LIFETIME 180 /* secs */
37 #define DEFAULT_RA_INTERVAL 120 /* secs */
39 static int bridge_index = -1;
40 static guint timer_uplink;
41 static guint timer_ra;
42 static char *default_interface;
43 static GSList *prefixes;
44 static GHashTable *timer_hash;
45 static void *rs_context;
47 static int setup_prefix_delegation(struct connman_service *service);
48 static void dhcpv6_callback(struct connman_network *network,
49 enum __connman_dhcpv6_status status, gpointer data);
51 static int enable_ipv6_forward(bool enable)
55 f = fopen("/proc/sys/net/ipv6/ip_forward", "r+");
69 static gboolean send_ra(gpointer data)
71 __connman_inet_ipv6_send_ra(bridge_index, NULL, prefixes,
72 DEFAULT_ROUTER_LIFETIME);
77 static void start_ra(int ifindex, GSList *prefix)
80 g_slist_free_full(prefixes, g_free);
82 prefixes = g_dhcpv6_copy_prefixes(prefix);
84 enable_ipv6_forward(true);
87 g_source_remove(timer_ra);
91 timer_ra = g_timeout_add_seconds(DEFAULT_RA_INTERVAL, send_ra, NULL);
94 static void stop_ra(int ifindex)
96 __connman_inet_ipv6_send_ra(ifindex, NULL, prefixes, 0);
99 g_source_remove(timer_ra);
103 enable_ipv6_forward(false);
106 g_slist_free_full(prefixes, g_free);
111 static void rs_received(struct nd_router_solicit *reply,
112 unsigned int length, void *user_data)
114 GDHCPIAPrefix *prefix;
122 for (list = prefixes; list; list = list->next) {
125 prefix->valid -= time(NULL) - prefix->expire;
126 prefix->preferred -= time(NULL) - prefix->expire;
129 __connman_inet_ipv6_send_ra(bridge_index, NULL, prefixes,
130 DEFAULT_ROUTER_LIFETIME);
133 static gboolean do_setup(gpointer data)
139 if (!default_interface)
140 DBG("No uplink connection, retrying prefix delegation");
142 ret = setup_prefix_delegation(__connman_service_get_default());
143 if (ret < 0 && ret != -EINPROGRESS)
144 return TRUE; /* delegation error, try again */
149 static void dhcpv6_renew_callback(struct connman_network *network,
150 enum __connman_dhcpv6_status status,
153 DBG("network %p status %d data %p", network, status, data);
155 if (status == CONNMAN_DHCPV6_STATUS_SUCCEED)
156 dhcpv6_callback(network, status, data);
158 setup_prefix_delegation(__connman_service_get_default());
161 static void cleanup(void)
163 if (timer_uplink != 0) {
164 g_source_remove(timer_uplink);
168 g_hash_table_destroy(timer_hash);
172 g_slist_free_full(prefixes, g_free);
177 static void dhcpv6_callback(struct connman_network *network,
178 enum __connman_dhcpv6_status status, gpointer data)
180 GSList *prefix_list = data;
182 DBG("network %p status %d data %p", network, status, data);
184 if (status == CONNMAN_DHCPV6_STATUS_FAIL) {
185 DBG("Prefix delegation request failed");
191 DBG("No prefixes, retrying");
192 if (timer_uplink == 0)
193 timer_uplink = g_timeout_add_seconds(10, do_setup,
199 * After we have got a list of prefixes, we can start to send router
200 * advertisements (RA) to tethering interface.
202 start_ra(bridge_index, prefix_list);
204 if (__connman_dhcpv6_start_pd_renew(network,
205 dhcpv6_renew_callback) == -ETIMEDOUT)
206 dhcpv6_renew_callback(network, CONNMAN_DHCPV6_STATUS_FAIL,
210 static int setup_prefix_delegation(struct connman_service *service)
212 struct connman_ipconfig *ipconfig;
214 int err = 0, ifindex;
218 * We do not have uplink connection. We just wait until
219 * default interface is updated.
224 interface = connman_service_get_interface(service);
226 DBG("interface %s bridge_index %d", interface, bridge_index);
228 if (default_interface) {
229 stop_ra(bridge_index);
231 ifindex = connman_inet_ifindex(default_interface);
232 __connman_dhcpv6_stop_pd(ifindex);
235 g_free(default_interface);
237 ipconfig = __connman_service_get_ip6config(service);
238 if (!__connman_ipconfig_ipv6_is_enabled(ipconfig)) {
240 default_interface = NULL;
241 return -EPFNOSUPPORT;
244 default_interface = interface;
246 if (default_interface) {
247 ifindex = connman_inet_ifindex(default_interface);
250 * Try to get a IPv6 prefix so we can start to advertise it.
252 err = __connman_dhcpv6_start_pd(ifindex, prefixes,
255 DBG("prefix delegation %d/%s", err, strerror(-err));
261 static void cleanup_timer(gpointer user_data)
263 guint timer = GPOINTER_TO_UINT(user_data);
265 g_source_remove(timer);
268 static void update_default_interface(struct connman_service *service)
270 setup_prefix_delegation(service);
273 static void update_ipconfig(struct connman_service *service,
274 struct connman_ipconfig *ipconfig)
276 if (!service || service != __connman_service_get_default())
279 if (ipconfig != __connman_service_get_ip6config(service))
282 if (!__connman_ipconfig_ipv6_is_enabled(ipconfig)) {
283 if (default_interface) {
286 ifindex = connman_inet_ifindex(default_interface);
287 __connman_dhcpv6_stop_pd(ifindex);
289 g_free(default_interface);
290 default_interface = NULL;
293 DBG("No IPv6 support for interface index %d",
294 __connman_ipconfig_get_index(ipconfig));
299 * Did we had PD activated already? If not, then start it.
301 if (!default_interface) {
302 DBG("IPv6 ipconfig %p changed for interface index %d", ipconfig,
303 __connman_ipconfig_get_index(ipconfig));
305 setup_prefix_delegation(service);
309 static struct connman_notifier pd_notifier = {
310 .name = "IPv6 prefix delegation",
311 .default_changed = update_default_interface,
312 .ipconfig_changed = update_ipconfig,
315 int __connman_ipv6pd_setup(const char *bridge)
317 struct connman_service *service;
320 if (!connman_inet_is_ipv6_supported())
321 return -EPFNOSUPPORT;
323 if (bridge_index >= 0) {
324 DBG("Prefix delegation already running");
328 err = connman_notifier_register(&pd_notifier);
332 bridge_index = connman_inet_ifindex(bridge);
334 timer_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
335 NULL, cleanup_timer);
337 err = __connman_inet_ipv6_start_recv_rs(bridge_index, rs_received,
340 DBG("Cannot receive router solicitation %d/%s",
341 err, strerror(-err));
343 service = __connman_service_get_default();
346 * We have an uplink connection already, try to use it.
348 return setup_prefix_delegation(service);
352 * The prefix delegation is started after have got the uplink
353 * connection up i.e., when the default service is setup in which
354 * case the update_default_interface() will be called.
359 void __connman_ipv6pd_cleanup(void)
363 if (!connman_inet_is_ipv6_supported())
366 connman_notifier_unregister(&pd_notifier);
368 __connman_inet_ipv6_stop_recv_rs(rs_context);
373 stop_ra(bridge_index);
375 if (default_interface) {
376 ifindex = connman_inet_ifindex(default_interface);
377 __connman_dhcpv6_stop_pd(ifindex);
378 g_free(default_interface);
379 default_interface = NULL;