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 void *rs_context;
46 static int setup_prefix_delegation(struct connman_service *service);
47 static void dhcpv6_callback(struct connman_network *network,
48 enum __connman_dhcpv6_status status, gpointer data);
50 static int enable_ipv6_forward(bool enable)
54 f = fopen("/proc/sys/net/ipv6/ip_forward", "r+");
68 static gboolean send_ra(gpointer data)
70 __connman_inet_ipv6_send_ra(bridge_index, NULL, prefixes,
71 DEFAULT_ROUTER_LIFETIME);
76 static void start_ra(int ifindex, GSList *prefix)
79 g_slist_free_full(prefixes, g_free);
81 prefixes = g_dhcpv6_copy_prefixes(prefix);
83 enable_ipv6_forward(true);
86 g_source_remove(timer_ra);
90 timer_ra = g_timeout_add_seconds(DEFAULT_RA_INTERVAL, send_ra, NULL);
93 static void stop_ra(int ifindex)
95 __connman_inet_ipv6_send_ra(ifindex, NULL, prefixes, 0);
98 g_source_remove(timer_ra);
102 enable_ipv6_forward(false);
105 g_slist_free_full(prefixes, g_free);
110 static void rs_received(struct nd_router_solicit *reply,
111 unsigned int length, void *user_data)
113 GDHCPIAPrefix *prefix;
121 for (list = prefixes; list; list = list->next) {
124 prefix->valid -= time(NULL) - prefix->expire;
125 prefix->preferred -= time(NULL) - prefix->expire;
128 __connman_inet_ipv6_send_ra(bridge_index, NULL, prefixes,
129 DEFAULT_ROUTER_LIFETIME);
132 static gboolean do_setup(gpointer data)
138 if (!default_interface)
139 DBG("No uplink connection, retrying prefix delegation");
141 ret = setup_prefix_delegation(connman_service_get_default());
142 if (ret < 0 && ret != -EINPROGRESS)
143 return TRUE; /* delegation error, try again */
148 static void dhcpv6_renew_callback(struct connman_network *network,
149 enum __connman_dhcpv6_status status,
152 DBG("network %p status %d data %p", network, status, data);
154 if (status == CONNMAN_DHCPV6_STATUS_SUCCEED)
155 dhcpv6_callback(network, status, data);
157 setup_prefix_delegation(connman_service_get_default());
160 static void cleanup(void)
162 if (timer_uplink != 0) {
163 g_source_remove(timer_uplink);
168 g_slist_free_full(prefixes, g_free);
173 static void dhcpv6_callback(struct connman_network *network,
174 enum __connman_dhcpv6_status status, gpointer data)
176 GSList *prefix_list = data;
178 DBG("network %p status %d data %p", network, status, data);
180 if (status == CONNMAN_DHCPV6_STATUS_FAIL) {
181 DBG("Prefix delegation request failed");
187 DBG("No prefixes, retrying");
188 if (timer_uplink == 0)
189 timer_uplink = g_timeout_add_seconds(10, do_setup,
195 * After we have got a list of prefixes, we can start to send router
196 * advertisements (RA) to tethering interface.
198 start_ra(bridge_index, prefix_list);
200 if (__connman_dhcpv6_start_pd_renew(network,
201 dhcpv6_renew_callback) == -ETIMEDOUT)
202 dhcpv6_renew_callback(network, CONNMAN_DHCPV6_STATUS_FAIL,
206 static int setup_prefix_delegation(struct connman_service *service)
208 struct connman_ipconfig *ipconfig;
210 int err = 0, ifindex;
214 * We do not have uplink connection. We just wait until
215 * default interface is updated.
220 interface = connman_service_get_interface(service);
222 DBG("interface %s bridge_index %d", interface, bridge_index);
224 if (default_interface) {
225 stop_ra(bridge_index);
227 ifindex = connman_inet_ifindex(default_interface);
228 __connman_dhcpv6_stop_pd(ifindex);
231 g_free(default_interface);
233 ipconfig = __connman_service_get_ip6config(service);
234 if (!__connman_ipconfig_ipv6_is_enabled(ipconfig)) {
236 default_interface = NULL;
237 return -EPFNOSUPPORT;
240 default_interface = interface;
242 if (default_interface) {
243 ifindex = connman_inet_ifindex(default_interface);
246 * Try to get a IPv6 prefix so we can start to advertise it.
248 err = __connman_dhcpv6_start_pd(ifindex, prefixes,
251 DBG("prefix delegation %d/%s", err, strerror(-err));
257 static void update_default_interface(struct connman_service *service)
259 setup_prefix_delegation(service);
262 static void update_ipconfig(struct connman_service *service,
263 struct connman_ipconfig *ipconfig)
265 if (!service || service != connman_service_get_default())
268 if (ipconfig != __connman_service_get_ip6config(service))
271 if (!__connman_ipconfig_ipv6_is_enabled(ipconfig)) {
272 if (default_interface) {
275 ifindex = connman_inet_ifindex(default_interface);
276 __connman_dhcpv6_stop_pd(ifindex);
278 g_free(default_interface);
279 default_interface = NULL;
282 DBG("No IPv6 support for interface index %d",
283 __connman_ipconfig_get_index(ipconfig));
288 * Did we had PD activated already? If not, then start it.
290 if (!default_interface) {
291 DBG("IPv6 ipconfig %p changed for interface index %d", ipconfig,
292 __connman_ipconfig_get_index(ipconfig));
294 setup_prefix_delegation(service);
298 static const struct connman_notifier pd_notifier = {
299 .name = "IPv6 prefix delegation",
300 .default_changed = update_default_interface,
301 .ipconfig_changed = update_ipconfig,
304 int __connman_ipv6pd_setup(const char *bridge)
306 struct connman_service *service;
309 if (!connman_inet_is_ipv6_supported())
310 return -EPFNOSUPPORT;
312 if (bridge_index >= 0) {
313 DBG("Prefix delegation already running");
317 err = connman_notifier_register(&pd_notifier);
321 bridge_index = connman_inet_ifindex(bridge);
323 err = __connman_inet_ipv6_start_recv_rs(bridge_index, rs_received,
326 DBG("Cannot receive router solicitation %d/%s",
327 err, strerror(-err));
329 service = connman_service_get_default();
332 * We have an uplink connection already, try to use it.
334 return setup_prefix_delegation(service);
338 * The prefix delegation is started after have got the uplink
339 * connection up i.e., when the default service is setup in which
340 * case the update_default_interface() will be called.
345 void __connman_ipv6pd_cleanup(void)
349 if (!connman_inet_is_ipv6_supported())
352 connman_notifier_unregister(&pd_notifier);
354 __connman_inet_ipv6_stop_recv_rs(rs_context);
359 stop_ra(bridge_index);
361 if (default_interface) {
362 ifindex = connman_inet_ifindex(default_interface);
363 __connman_dhcpv6_stop_pd(ifindex);
364 g_free(default_interface);
365 default_interface = NULL;