summaryrefslogtreecommitdiff
path: root/src/ipv6pd.c
blob: 8cd2a33e7fa40873b3915d0225a14940b6168e4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
 *
 *  Connection Manager
 *
 *  Copyright (C) 2013  Intel Corporation. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>

#include "connman.h"

#include <gdhcp/gdhcp.h>

#define DEFAULT_ROUTER_LIFETIME 180  /* secs */
#define DEFAULT_RA_INTERVAL 120  /* secs */

static int bridge_index = -1;
static guint timer_uplink;
static guint timer_ra;
static char *default_interface;
static GSList *prefixes;
static void *rs_context;

static int setup_prefix_delegation(struct connman_service *service);
static void dhcpv6_callback(struct connman_network *network,
			enum __connman_dhcpv6_status status, gpointer data);

static int enable_ipv6_forward(bool enable)
{
	FILE *f;

	f = fopen("/proc/sys/net/ipv6/ip_forward", "r+");
	if (!f)
		return -errno;

	if (enable)
		fprintf(f, "1");
	else
		fprintf(f, "0");

	fclose(f);

	return 0;
}

static gboolean send_ra(gpointer data)
{
	__connman_inet_ipv6_send_ra(bridge_index, NULL, prefixes,
					DEFAULT_ROUTER_LIFETIME);

	return TRUE;
}

static void start_ra(int ifindex, GSList *prefix)
{
	if (prefixes)
		g_slist_free_full(prefixes, g_free);

	prefixes = g_dhcpv6_copy_prefixes(prefix);

	enable_ipv6_forward(true);

	if (timer_ra > 0)
		g_source_remove(timer_ra);

	send_ra(NULL);

	timer_ra = g_timeout_add_seconds(DEFAULT_RA_INTERVAL, send_ra, NULL);
}

static void stop_ra(int ifindex)
{
	__connman_inet_ipv6_send_ra(ifindex, NULL, prefixes, 0);

	if (timer_ra > 0) {
		g_source_remove(timer_ra);
		timer_ra = 0;
	}

	enable_ipv6_forward(false);

	if (prefixes) {
		g_slist_free_full(prefixes, g_free);
		prefixes = NULL;
	}
}

static void rs_received(struct nd_router_solicit *reply,
			unsigned int length, void *user_data)
{
	GDHCPIAPrefix *prefix;
	GSList *list;

	if (!prefixes)
		return;

	DBG("");

	for (list = prefixes; list; list = list->next) {
		prefix = list->data;

		prefix->valid -= time(NULL) - prefix->expire;
		prefix->preferred -= time(NULL) - prefix->expire;
	}

	__connman_inet_ipv6_send_ra(bridge_index, NULL, prefixes,
					DEFAULT_ROUTER_LIFETIME);
}

static gboolean do_setup(gpointer data)
{
	int ret;

	timer_uplink = 0;

	if (!default_interface)
		DBG("No uplink connection, retrying prefix delegation");

	ret = setup_prefix_delegation(connman_service_get_default());
	if (ret < 0 && ret != -EINPROGRESS)
		return TRUE; /* delegation error, try again */

	return FALSE;
}

static void dhcpv6_renew_callback(struct connman_network *network,
				enum __connman_dhcpv6_status status,
				gpointer data)
{
	DBG("network %p status %d data %p", network, status, data);

	if (status == CONNMAN_DHCPV6_STATUS_SUCCEED)
		dhcpv6_callback(network, status, data);
	else
		setup_prefix_delegation(connman_service_get_default());
}

static void cleanup(void)
{
	if (timer_uplink != 0) {
		g_source_remove(timer_uplink);
		timer_uplink = 0;
	}

	if (prefixes) {
		g_slist_free_full(prefixes, g_free);
		prefixes = NULL;
	}
}

static void dhcpv6_callback(struct connman_network *network,
			enum __connman_dhcpv6_status status, gpointer data)
{
	GSList *prefix_list = data;

	DBG("network %p status %d data %p", network, status, data);

	if (status == CONNMAN_DHCPV6_STATUS_FAIL) {
		DBG("Prefix delegation request failed");
		cleanup();
		return;
	}

	if (!prefix_list) {
		DBG("No prefixes, retrying");
		if (timer_uplink == 0)
			timer_uplink = g_timeout_add_seconds(10, do_setup,
									NULL);
		return;
	}

	/*
	 * After we have got a list of prefixes, we can start to send router
	 * advertisements (RA) to tethering interface.
	 */
	start_ra(bridge_index, prefix_list);

	if (__connman_dhcpv6_start_pd_renew(network,
					dhcpv6_renew_callback) == -ETIMEDOUT)
		dhcpv6_renew_callback(network, CONNMAN_DHCPV6_STATUS_FAIL,
									NULL);
}

static int setup_prefix_delegation(struct connman_service *service)
{
	struct connman_ipconfig *ipconfig;
	char *interface;
	int err = 0, ifindex;

	if (!service) {
		/*
		 * We do not have uplink connection. We just wait until
		 * default interface is updated.
		 */
		return -EINPROGRESS;
	}

	interface = connman_service_get_interface(service);

	DBG("interface %s bridge_index %d", interface, bridge_index);

	if (default_interface) {
		stop_ra(bridge_index);

		ifindex = connman_inet_ifindex(default_interface);
		__connman_dhcpv6_stop_pd(ifindex);
	}

	g_free(default_interface);

	ipconfig = __connman_service_get_ip6config(service);
	if (!__connman_ipconfig_ipv6_is_enabled(ipconfig)) {
		g_free(interface);
		default_interface = NULL;
		return -EPFNOSUPPORT;
	}

	default_interface = interface;

	if (default_interface) {
		ifindex = connman_inet_ifindex(default_interface);

		/*
		 * Try to get a IPv6 prefix so we can start to advertise it.
		 */
		err = __connman_dhcpv6_start_pd(ifindex, prefixes,
						dhcpv6_callback);
		if (err < 0)
			DBG("prefix delegation %d/%s", err, strerror(-err));
	}

	return err;
}

static void update_default_interface(struct connman_service *service)
{
	setup_prefix_delegation(service);
}

static void update_ipconfig(struct connman_service *service,
				struct connman_ipconfig *ipconfig)
{
	if (!service || service != connman_service_get_default())
		return;

	if (ipconfig != __connman_service_get_ip6config(service))
		return;

	if (!__connman_ipconfig_ipv6_is_enabled(ipconfig)) {
		if (default_interface) {
			int ifindex;

			ifindex = connman_inet_ifindex(default_interface);
			__connman_dhcpv6_stop_pd(ifindex);

			g_free(default_interface);
			default_interface = NULL;
		}

		DBG("No IPv6 support for interface index %d",
			__connman_ipconfig_get_index(ipconfig));
		return;
	}

	/*
	 * Did we had PD activated already? If not, then start it.
	 */
	if (!default_interface) {
		DBG("IPv6 ipconfig %p changed for interface index %d", ipconfig,
			__connman_ipconfig_get_index(ipconfig));

		setup_prefix_delegation(service);
	}
}

static const struct connman_notifier pd_notifier = {
	.name			= "IPv6 prefix delegation",
	.default_changed	= update_default_interface,
	.ipconfig_changed	= update_ipconfig,
};

int __connman_ipv6pd_setup(const char *bridge)
{
	struct connman_service *service;
	int err;

	if (!connman_inet_is_ipv6_supported())
		return -EPFNOSUPPORT;

	if (bridge_index >= 0) {
		DBG("Prefix delegation already running");
		return -EALREADY;
	}

	err = connman_notifier_register(&pd_notifier);
	if (err < 0)
		return err;

	bridge_index = connman_inet_ifindex(bridge);

	err = __connman_inet_ipv6_start_recv_rs(bridge_index, rs_received,
						NULL, &rs_context);
	if (err < 0)
		DBG("Cannot receive router solicitation %d/%s",
			err, strerror(-err));

	service = connman_service_get_default();
	if (service) {
		/*
		 * We have an uplink connection already, try to use it.
		 */
		return setup_prefix_delegation(service);
	}

	/*
	 * The prefix delegation is started after have got the uplink
	 * connection up i.e., when the default service is setup in which
	 * case the update_default_interface() will be called.
	 */
	return -EINPROGRESS;
}

void __connman_ipv6pd_cleanup(void)
{
	int ifindex;

	if (!connman_inet_is_ipv6_supported())
		return;

	connman_notifier_unregister(&pd_notifier);

	__connman_inet_ipv6_stop_recv_rs(rs_context);
	rs_context = NULL;

	cleanup();

	stop_ra(bridge_index);

	if (default_interface) {
		ifindex = connman_inet_ifindex(default_interface);
		__connman_dhcpv6_stop_pd(ifindex);
		g_free(default_interface);
		default_interface = NULL;
	}

	bridge_index = -1;
}