summaryrefslogtreecommitdiff
path: root/src/devices/bluetooth/nm-bluez5-dun.c
blob: 1338bcb8116d1bed8433e2377eedcaa4cfe851ee (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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// SPDX-License-Identifier: GPL-2.0+
/* NetworkManager -- Network link manager
 *
 * Copyright (C) 2014 Red Hat, Inc.
 */

#include "nm-default.h"

#include <sys/socket.h>
#include <bluetooth/sdp.h>
#include <bluetooth/sdp_lib.h>
#include <bluetooth/rfcomm.h>
#include <net/ethernet.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>

#include "nm-bluez5-dun.h"
#include "nm-bt-error.h"
#include "NetworkManagerUtils.h"

struct _NMBluez5DunContext {
	bdaddr_t src;
	bdaddr_t dst;
	char *src_str;
	char *dst_str;
	int rfcomm_channel;
	int rfcomm_fd;
	int rfcomm_tty_fd;
	int rfcomm_id;
	NMBluez5DunFunc callback;
	gpointer user_data;
	sdp_session_t *sdp_session;
	guint sdp_watch_id;
};

static void
dun_connect (NMBluez5DunContext *context)
{
	struct sockaddr_rc sa;
	int devid, try = 30;
	char tty[100];
	const int ttylen = sizeof (tty) - 1;
	GError *error = NULL;
	int errsv;

	struct rfcomm_dev_req req = {
		.flags = (1 << RFCOMM_REUSE_DLC) | (1 << RFCOMM_RELEASE_ONHUP),
		.dev_id = -1,
		.channel = context->rfcomm_channel
	};

	context->rfcomm_fd = socket (AF_BLUETOOTH, SOCK_STREAM | SOCK_CLOEXEC, BTPROTO_RFCOMM);
	if (context->rfcomm_fd < 0) {
		errsv = errno;
		error = g_error_new (NM_BT_ERROR, NM_BT_ERROR_DUN_CONNECT_FAILED,
		                     "Failed to create RFCOMM socket: (%d) %s",
		                     errsv, nm_strerror_native (errsv));
		goto done;
	}

	/* Connect to the remote device */
	sa.rc_family = AF_BLUETOOTH;
	sa.rc_channel = 0;
	memcpy (&sa.rc_bdaddr, &context->src, ETH_ALEN);
	if (bind (context->rfcomm_fd, (struct sockaddr *) &sa, sizeof(sa))) {
		errsv = errno;
		error = g_error_new (NM_BT_ERROR, NM_BT_ERROR_DUN_CONNECT_FAILED,
		                     "Failed to bind socket: (%d) %s",
		                     errsv, nm_strerror_native (errsv));
		goto done;
	}

	sa.rc_channel = context->rfcomm_channel;
	memcpy (&sa.rc_bdaddr, &context->dst, ETH_ALEN);
	if (connect (context->rfcomm_fd, (struct sockaddr *) &sa, sizeof (sa)) ) {
		errsv = errno;
		error = g_error_new (NM_BT_ERROR, NM_BT_ERROR_DUN_CONNECT_FAILED,
		                     "Failed to connect to remote device: (%d) %s",
		                     errsv, nm_strerror_native (errsv));
		goto done;
	}

	nm_log_dbg (LOGD_BT, "(%s): connected to %s on channel %d",
	            context->src_str, context->dst_str, context->rfcomm_channel);

	/* Create an RFCOMM kernel device for the DUN channel */
	memcpy (&req.src, &context->src, ETH_ALEN);
	memcpy (&req.dst, &context->dst, ETH_ALEN);
	devid = ioctl (context->rfcomm_fd, RFCOMMCREATEDEV, &req);
	if (devid < 0) {
		errsv = errno;
		error = g_error_new (NM_BT_ERROR, NM_BT_ERROR_DUN_CONNECT_FAILED,
		                     "Failed to create rfcomm device: (%d) %s",
		                     errsv, nm_strerror_native (errsv));
		goto done;
	}
	context->rfcomm_id = devid;

	snprintf (tty, ttylen, "/dev/rfcomm%d", devid);
	while ((context->rfcomm_tty_fd = open (tty, O_RDONLY | O_NOCTTY | O_CLOEXEC)) < 0 && try--) {
		if (try) {
			g_usleep (100 * 1000);
			continue;
		}

		error = g_error_new (NM_BT_ERROR, NM_BT_ERROR_DUN_CONNECT_FAILED,
		                     "Failed to find rfcomm device: %s",
		                     tty);
		break;
	}

done:
	context->callback (context, tty, error, context->user_data);
}

static void
sdp_search_cleanup (NMBluez5DunContext *context)
{
	if (context->sdp_session) {
		sdp_close (context->sdp_session);
		context->sdp_session = NULL;
	}

	nm_clear_g_source (&context->sdp_watch_id);
}

static void
sdp_search_completed_cb (uint8_t type, uint16_t status, uint8_t *rsp, size_t size, void *user_data)
{
	NMBluez5DunContext *context = user_data;
	int scanned, seqlen = 0, bytesleft = size;
	uint8_t dataType;
	int channel = -1;

	nm_log_dbg (LOGD_BT, "(%s -> %s): SDP search finished with type=%d status=%d",
	            context->src_str, context->dst_str, status, type);

	/* SDP response received */
	if (status || type != SDP_SVC_SEARCH_ATTR_RSP) {
		GError *error = g_error_new (NM_BT_ERROR,
		                             NM_BT_ERROR_DUN_CONNECT_FAILED,
		                             "Did not get a Service Discovery response");
		context->callback (context, NULL, error, context->user_data);
		goto done;
	}

	scanned = sdp_extract_seqtype (rsp, bytesleft, &dataType, &seqlen);

	nm_log_dbg (LOGD_BT, "(%s -> %s): SDP sequence type scanned=%d length=%d",
	            context->src_str, context->dst_str, scanned, seqlen);

	scanned = sdp_extract_seqtype (rsp, bytesleft, &dataType, &seqlen);
	if (!scanned || !seqlen) {
		/* Short read or unknown sequence type */
		GError *error = g_error_new (NM_BT_ERROR,
		                             NM_BT_ERROR_DUN_CONNECT_FAILED,
		                             "Improper Service Discovery response");
		context->callback (context, NULL, error, context->user_data);
		goto done;
	}

	rsp += scanned;
	bytesleft -= scanned;
	do {
		sdp_record_t *rec;
		int recsize = 0;
		sdp_list_t *protos;

		rec = sdp_extract_pdu (rsp, bytesleft, &recsize);
		if (!rec)
			break;

		if (!recsize) {
			sdp_record_free (rec);
			break;
		}

		if (sdp_get_access_protos (rec, &protos) == 0) {
			/* Extract the DUN channel number */
			channel = sdp_get_proto_port (protos, RFCOMM_UUID);
			sdp_list_free (protos, NULL);

			nm_log_dbg (LOGD_BT, "(%s -> %s): SDP channel=%d",
			            context->src_str, context->dst_str, channel);
		}
		sdp_record_free (rec);

		scanned += recsize;
		rsp += recsize;
		bytesleft -= recsize;
	} while ((scanned < (ssize_t) size) && (bytesleft > 0) && (channel < 0));

done:
	if (channel != -1) {
		context->rfcomm_channel = channel;
		dun_connect (context);
	}

	sdp_search_cleanup (context);
}

static gboolean
sdp_search_process_cb (GIOChannel *channel, GIOCondition condition, gpointer user_data)
{
	NMBluez5DunContext *context = user_data;

	nm_log_dbg (LOGD_BT, "(%s -> %s): SDP search progressed with condition=%d",
	            context->src_str, context->dst_str, condition);

	if (condition & (G_IO_ERR | G_IO_HUP | G_IO_NVAL)) {
		GError *error = g_error_new (NM_BT_ERROR,
		                             NM_BT_ERROR_DUN_CONNECT_FAILED,
		                             "Service Discovery interrupted");
		context->callback (context, NULL, error, context->user_data);
		sdp_search_cleanup (context);
		return FALSE;
	}

	if (sdp_process (context->sdp_session) < 0) {
		nm_log_dbg (LOGD_BT, "(%s -> %s): SDP search finished",
		            context->src_str, context->dst_str);

		/* Search finished successfully. */
		return FALSE;
	}

	/* Search progressed successfully. */
	return TRUE;
}

static gboolean
sdp_connect_watch (GIOChannel *channel, GIOCondition condition, gpointer user_data)
{
	NMBluez5DunContext *context = user_data;
	sdp_list_t *search, *attrs;
	uuid_t svclass;
	uint16_t attr;
	int fd, fd_err = 0;
	int err;
	socklen_t len = sizeof (fd_err);
	GError *error = NULL;

	context->sdp_watch_id = 0;

	fd = g_io_channel_unix_get_fd (channel);
	if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &fd_err, &len) < 0) {
		err = errno;
		nm_log_dbg (LOGD_BT, "(%s -> %s): getsockopt error=%d",
		            context->src_str, context->dst_str, err);
	} else {
		err = fd_err;
		nm_log_dbg (LOGD_BT, "(%s -> %s): SO_ERROR error=%d",
		            context->src_str, context->dst_str, fd_err);
	}

	if (err != 0) {
		error = g_error_new (NM_BT_ERROR, NM_BT_ERROR_DUN_CONNECT_FAILED,
		                     "Error on Service Discovery socket: (%d) %s",
		                     err, nm_strerror_native (err));
		goto done;
	}

	if (sdp_set_notify (context->sdp_session, sdp_search_completed_cb, context) < 0) {
		/* Should not be reached, only can fail if we passed bad sdp_session. */
		error = g_error_new (NM_BT_ERROR, NM_BT_ERROR_DUN_CONNECT_FAILED,
		                     "Could not request Service Discovery notification");
		goto done;
	}

	sdp_uuid16_create (&svclass, DIALUP_NET_SVCLASS_ID);
	search = sdp_list_append (NULL, &svclass);
	attr = SDP_ATTR_PROTO_DESC_LIST;
	attrs = sdp_list_append (NULL, &attr);

	if (!sdp_service_search_attr_async (context->sdp_session, search, SDP_ATTR_REQ_INDIVIDUAL, attrs)) {
		/* Set callback responsible for update the internal SDP transaction */
		context->sdp_watch_id = g_io_add_watch (channel,
		                                        G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
		                                        sdp_search_process_cb,
		                                        context);
	} else {
		err = sdp_get_error (context->sdp_session);
		error = g_error_new (NM_BT_ERROR,
		                     NM_BT_ERROR_DUN_CONNECT_FAILED,
		                     "Error starting Service Discovery: (%d) %s",
		                     err, nm_strerror_native (err));
	}

	sdp_list_free (attrs, NULL);
	sdp_list_free (search, NULL);

done:
	if (error) {
		context->callback (context, NULL, error, context->user_data);
		sdp_search_cleanup (context);
	}

	return G_SOURCE_REMOVE;
}

NMBluez5DunContext *
nm_bluez5_dun_new (const char *adapter,
                   const char *remote)

{
	NMBluez5DunContext *context;

	context = g_slice_new0 (NMBluez5DunContext);
	str2ba (adapter, &context->src);
	str2ba (remote, &context->dst);
	context->src_str = g_strdup (adapter);
	context->dst_str = g_strdup (remote);
	context->rfcomm_channel = -1;
	context->rfcomm_id = -1;
	context->rfcomm_fd = -1;
	return context;
}

void
nm_bluez5_dun_connect (NMBluez5DunContext *context,
                       NMBluez5DunFunc callback,
                       gpointer user_data)
{
	GIOChannel *channel;

	context->callback = callback;
	context->user_data = user_data;

	if (context->rfcomm_channel != -1) {
		nm_log_dbg (LOGD_BT, "(%s): channel number on device %s cached: %d",
		            context->src_str, context->dst_str, context->rfcomm_channel);
		/* FIXME: don't invoke the callback synchronously. */
		dun_connect (context);
		return;
	}

	nm_log_dbg (LOGD_BT, "(%s): starting channel number discovery for device %s",
	            context->src_str, context->dst_str);

	context->sdp_session = sdp_connect (&context->src, &context->dst, SDP_NON_BLOCKING);
	if (!context->sdp_session) {
		int err = errno;
		GError *error;

		error = g_error_new (NM_BT_ERROR, NM_BT_ERROR_DUN_CONNECT_FAILED,
		                     "Failed to connect to the SDP server: (%d) %s",
		                      err, nm_strerror_native (err));
		/* FIXME: don't invoke the callback synchronously. */
		context->callback (context, NULL, error, context->user_data);
		return;
	}

	/* FIXME(shutdown): make connect cancellable. */
	channel = g_io_channel_unix_new (sdp_get_socket (context->sdp_session));
	context->sdp_watch_id = g_io_add_watch (channel,
	                                        G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
	                                        sdp_connect_watch,
	                                        context);
	g_io_channel_unref (channel);
}

/* Only clean up connection-related stuff to allow reconnect */
void
nm_bluez5_dun_cleanup (NMBluez5DunContext *context)
{
	g_return_if_fail (context != NULL);

	sdp_search_cleanup (context);

	if (context->rfcomm_fd >= 0) {
		if (context->rfcomm_id >= 0) {
			struct rfcomm_dev_req req = { 0 };

			req.dev_id = context->rfcomm_id;
			(void) ioctl (context->rfcomm_fd, RFCOMMRELEASEDEV, &req);
			context->rfcomm_id = -1;
		}
		nm_close (context->rfcomm_fd);
		context->rfcomm_fd = -1;
	}

	nm_close (context->rfcomm_tty_fd);
	context->rfcomm_tty_fd = -1;
}

void
nm_bluez5_dun_free (NMBluez5DunContext *context)
{
	g_return_if_fail (context != NULL);

	nm_bluez5_dun_cleanup (context);
	g_clear_pointer (&context->src_str, g_free);
	g_clear_pointer (&context->dst_str, g_free);
	g_slice_free (NMBluez5DunContext, context);
}