1 /*
2 *
3 * BlueZ - Bluetooth protocol stack for Linux
4 *
5 * Copyright (C) 2009 Marcel Holtmann <marcel@holtmann.org>
6 * Copyright (C) 2009 Nokia Corporation
7 *
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 */
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <poll.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31
32 #include <bluetooth/bluetooth.h>
33 #include <bluetooth/l2cap.h>
34 #include <bluetooth/rfcomm.h>
35 #include <bluetooth/sco.h>
36 #include <bluetooth/hci.h>
37 #include <bluetooth/hci_lib.h>
38
39 #include <glib.h>
40
41 #include "logging.h"
42 #include "btio.h"
43
44 #define ERROR_FAILED(gerr, str, err) \
45 g_set_error(gerr, BT_IO_ERROR, BT_IO_ERROR_FAILED, \
46 str ": %s (%d)", strerror(err), err)
47
48 #define DEFAULT_DEFER_TIMEOUT 30
49
50 struct set_opts {
51 bdaddr_t src;
52 bdaddr_t dst;
53 int defer;
54 int sec_level;
55 uint8_t channel;
56 uint16_t psm;
57 uint16_t mtu;
58 uint16_t imtu;
59 uint16_t omtu;
60 int master;
61 };
62
63 struct connect {
64 BtIOConnect connect;
65 gpointer user_data;
66 GDestroyNotify destroy;
67 };
68
69 struct accept {
70 BtIOConnect connect;
71 gpointer user_data;
72 GDestroyNotify destroy;
73 };
74
75 struct server {
76 BtIOConnect connect;
77 BtIOConfirm confirm;
78 gpointer user_data;
79 GDestroyNotify destroy;
80 };
81
server_remove(struct server * server)82 static void server_remove(struct server *server)
83 {
84 if (server->destroy)
85 server->destroy(server->user_data);
86 g_free(server);
87 }
88
connect_remove(struct connect * conn)89 static void connect_remove(struct connect *conn)
90 {
91 if (conn->destroy)
92 conn->destroy(conn->user_data);
93 g_free(conn);
94 }
95
accept_remove(struct accept * accept)96 static void accept_remove(struct accept *accept)
97 {
98 if (accept->destroy)
99 accept->destroy(accept->user_data);
100 g_free(accept);
101 }
102
check_nval(GIOChannel * io)103 static gboolean check_nval(GIOChannel *io)
104 {
105 struct pollfd fds;
106
107 memset(&fds, 0, sizeof(fds));
108 fds.fd = g_io_channel_unix_get_fd(io);
109 fds.events = POLLNVAL;
110
111 if (poll(&fds, 1, 0) > 0 && (fds.revents & POLLNVAL))
112 return TRUE;
113
114 return FALSE;
115 }
116
accept_cb(GIOChannel * io,GIOCondition cond,gpointer user_data)117 static gboolean accept_cb(GIOChannel *io, GIOCondition cond,
118 gpointer user_data)
119 {
120 struct accept *accept = user_data;
121 GError *err = NULL;
122
123 /* If the user aborted this accept attempt */
124 if ((cond & G_IO_NVAL) || check_nval(io))
125 return FALSE;
126
127 if (cond & (G_IO_HUP | G_IO_ERR))
128 g_set_error(&err, BT_IO_ERROR, BT_IO_ERROR_DISCONNECTED,
129 "HUP or ERR on socket");
130
131 accept->connect(io, err, accept->user_data);
132
133 g_clear_error(&err);
134
135 return FALSE;
136 }
137
connect_cb(GIOChannel * io,GIOCondition cond,gpointer user_data)138 static gboolean connect_cb(GIOChannel *io, GIOCondition cond,
139 gpointer user_data)
140 {
141 struct connect *conn = user_data;
142 GError *gerr = NULL;
143
144 /* If the user aborted this connect attempt */
145 if ((cond & G_IO_NVAL) || check_nval(io))
146 return FALSE;
147
148 if (cond & G_IO_OUT) {
149 int err = 0, sock = g_io_channel_unix_get_fd(io);
150 socklen_t len = sizeof(err);
151
152 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &len) < 0)
153 err = errno;
154
155 if (err)
156 g_set_error(&gerr, BT_IO_ERROR,
157 BT_IO_ERROR_CONNECT_FAILED, "%s (%d)",
158 strerror(err), err);
159 } else if (cond & (G_IO_HUP | G_IO_ERR))
160 g_set_error(&gerr, BT_IO_ERROR, BT_IO_ERROR_CONNECT_FAILED,
161 "HUP or ERR on socket");
162
163 conn->connect(io, gerr, conn->user_data);
164
165 if (gerr)
166 g_error_free(gerr);
167
168 return FALSE;
169 }
170
server_cb(GIOChannel * io,GIOCondition cond,gpointer user_data)171 static gboolean server_cb(GIOChannel *io, GIOCondition cond,
172 gpointer user_data)
173 {
174 struct server *server = user_data;
175 int srv_sock, cli_sock;
176 GIOChannel *cli_io;
177
178 /* If the user closed the server */
179 if ((cond & G_IO_NVAL) || check_nval(io))
180 return FALSE;
181
182 srv_sock = g_io_channel_unix_get_fd(io);
183
184 cli_sock = accept(srv_sock, NULL, NULL);
185 if (cli_sock < 0) {
186 error("accept: %s (%d)", strerror(errno), errno);
187 return TRUE;
188 }
189
190 cli_io = g_io_channel_unix_new(cli_sock);
191
192 g_io_channel_set_close_on_unref(cli_io, TRUE);
193 g_io_channel_set_flags(cli_io, G_IO_FLAG_NONBLOCK, NULL);
194
195 if (server->confirm)
196 server->confirm(cli_io, server->user_data);
197 else
198 server->connect(cli_io, NULL, server->user_data);
199
200 g_io_channel_unref(cli_io);
201
202 return TRUE;
203 }
204
server_add(GIOChannel * io,BtIOConnect connect,BtIOConfirm confirm,gpointer user_data,GDestroyNotify destroy)205 static void server_add(GIOChannel *io, BtIOConnect connect,
206 BtIOConfirm confirm, gpointer user_data,
207 GDestroyNotify destroy)
208 {
209 struct server *server;
210 GIOCondition cond;
211
212 server = g_new0(struct server, 1);
213 server->connect = connect;
214 server->confirm = confirm;
215 server->user_data = user_data;
216 server->destroy = destroy;
217
218 cond = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
219 g_io_add_watch_full(io, G_PRIORITY_DEFAULT, cond, server_cb, server,
220 (GDestroyNotify) server_remove);
221 }
222
connect_add(GIOChannel * io,BtIOConnect connect,gpointer user_data,GDestroyNotify destroy)223 static void connect_add(GIOChannel *io, BtIOConnect connect,
224 gpointer user_data, GDestroyNotify destroy)
225 {
226 struct connect *conn;
227 GIOCondition cond;
228
229 conn = g_new0(struct connect, 1);
230 conn->connect = connect;
231 conn->user_data = user_data;
232 conn->destroy = destroy;
233
234 cond = G_IO_OUT | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
235 g_io_add_watch_full(io, G_PRIORITY_DEFAULT, cond, connect_cb, conn,
236 (GDestroyNotify) connect_remove);
237 }
238
accept_add(GIOChannel * io,BtIOConnect connect,gpointer user_data,GDestroyNotify destroy)239 static void accept_add(GIOChannel *io, BtIOConnect connect, gpointer user_data,
240 GDestroyNotify destroy)
241 {
242 struct accept *accept;
243 GIOCondition cond;
244
245 accept = g_new0(struct accept, 1);
246 accept->connect = connect;
247 accept->user_data = user_data;
248 accept->destroy = destroy;
249
250 cond = G_IO_OUT | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
251 g_io_add_watch_full(io, G_PRIORITY_DEFAULT, cond, accept_cb, accept,
252 (GDestroyNotify) accept_remove);
253 }
254
l2cap_bind(int sock,const bdaddr_t * src,uint16_t psm)255 static int l2cap_bind(int sock, const bdaddr_t *src, uint16_t psm)
256 {
257 struct sockaddr_l2 addr;
258
259 memset(&addr, 0, sizeof(addr));
260 addr.l2_family = AF_BLUETOOTH;
261 bacpy(&addr.l2_bdaddr, src);
262 addr.l2_psm = htobs(psm);
263
264 return bind(sock, (struct sockaddr *) &addr, sizeof(addr));
265 }
266
l2cap_connect(int sock,const bdaddr_t * dst,uint16_t psm)267 static int l2cap_connect(int sock, const bdaddr_t *dst, uint16_t psm)
268 {
269 int err;
270 struct sockaddr_l2 addr;
271
272 memset(&addr, 0, sizeof(addr));
273 addr.l2_family = AF_BLUETOOTH;
274 bacpy(&addr.l2_bdaddr, dst);
275 addr.l2_psm = htobs(psm);
276
277 err = connect(sock, (struct sockaddr *) &addr, sizeof(addr));
278 if (err < 0 && !(errno == EAGAIN || errno == EINPROGRESS))
279 return err;
280
281 return 0;
282 }
283
l2cap_set_master(int sock,int master)284 static int l2cap_set_master(int sock, int master)
285 {
286 int flags;
287 socklen_t len;
288
289 len = sizeof(flags);
290 if (getsockopt(sock, SOL_L2CAP, L2CAP_LM, &flags, &len) < 0)
291 return -errno;
292
293 if (master) {
294 if (flags & L2CAP_LM_MASTER)
295 return 0;
296 flags |= L2CAP_LM_MASTER;
297 } else {
298 if (!(flags & L2CAP_LM_MASTER))
299 return 0;
300 flags &= ~L2CAP_LM_MASTER;
301 }
302
303 if (setsockopt(sock, SOL_L2CAP, L2CAP_LM, &flags, sizeof(flags)) < 0)
304 return -errno;
305
306 return 0;
307 }
308
rfcomm_set_master(int sock,int master)309 static int rfcomm_set_master(int sock, int master)
310 {
311 int flags;
312 socklen_t len;
313
314 len = sizeof(flags);
315 if (getsockopt(sock, SOL_RFCOMM, RFCOMM_LM, &flags, &len) < 0)
316 return -errno;
317
318 if (master) {
319 if (flags & RFCOMM_LM_MASTER)
320 return 0;
321 flags |= RFCOMM_LM_MASTER;
322 } else {
323 if (!(flags & RFCOMM_LM_MASTER))
324 return 0;
325 flags &= ~RFCOMM_LM_MASTER;
326 }
327
328 if (setsockopt(sock, SOL_RFCOMM, RFCOMM_LM, &flags, sizeof(flags)) < 0)
329 return -errno;
330
331 return 0;
332 }
333
l2cap_set_lm(int sock,int level)334 static int l2cap_set_lm(int sock, int level)
335 {
336 int lm_map[] = {
337 0,
338 L2CAP_LM_AUTH,
339 L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT,
340 L2CAP_LM_AUTH | L2CAP_LM_ENCRYPT | L2CAP_LM_SECURE,
341 }, opt = lm_map[level];
342
343 if (setsockopt(sock, SOL_L2CAP, L2CAP_LM, &opt, sizeof(opt)) < 0)
344 return -errno;
345
346 return 0;
347 }
348
rfcomm_set_lm(int sock,int level)349 static int rfcomm_set_lm(int sock, int level)
350 {
351 int lm_map[] = {
352 0,
353 RFCOMM_LM_AUTH,
354 RFCOMM_LM_AUTH | RFCOMM_LM_ENCRYPT,
355 RFCOMM_LM_AUTH | RFCOMM_LM_ENCRYPT | RFCOMM_LM_SECURE,
356 }, opt = lm_map[level];
357
358 if (setsockopt(sock, SOL_RFCOMM, RFCOMM_LM, &opt, sizeof(opt)) < 0)
359 return -errno;
360
361 return 0;
362 }
363
set_sec_level(int sock,BtIOType type,int level,GError ** err)364 static gboolean set_sec_level(int sock, BtIOType type, int level, GError **err)
365 {
366 struct bt_security sec;
367 int ret;
368
369 if (level < BT_SECURITY_LOW || level > BT_SECURITY_HIGH) {
370 g_set_error(err, BT_IO_ERROR, BT_IO_ERROR_INVALID_ARGS,
371 "Valid security level range is %d-%d",
372 BT_SECURITY_LOW, BT_SECURITY_HIGH);
373 return FALSE;
374 }
375
376 memset(&sec, 0, sizeof(sec));
377 sec.level = level;
378
379 if (setsockopt(sock, SOL_BLUETOOTH, BT_SECURITY, &sec,
380 sizeof(sec)) == 0)
381 return TRUE;
382
383 if (errno != ENOPROTOOPT) {
384 ERROR_FAILED(err, "setsockopt(BT_SECURITY)", errno);
385 return FALSE;
386 }
387
388 if (type == BT_IO_L2CAP)
389 ret = l2cap_set_lm(sock, level);
390 else
391 ret = rfcomm_set_lm(sock, level);
392
393 if (ret < 0) {
394 ERROR_FAILED(err, "setsockopt(LM)", -ret);
395 return FALSE;
396 }
397
398 return TRUE;
399 }
400
l2cap_get_lm(int sock,int * sec_level)401 static int l2cap_get_lm(int sock, int *sec_level)
402 {
403 int opt;
404 socklen_t len;
405
406 len = sizeof(opt);
407 if (getsockopt(sock, SOL_L2CAP, L2CAP_LM, &opt, &len) < 0)
408 return -errno;
409
410 *sec_level = 0;
411
412 if (opt & L2CAP_LM_AUTH)
413 *sec_level = BT_SECURITY_LOW;
414 if (opt & L2CAP_LM_ENCRYPT)
415 *sec_level = BT_SECURITY_MEDIUM;
416 if (opt & L2CAP_LM_SECURE)
417 *sec_level = BT_SECURITY_HIGH;
418
419 return 0;
420 }
421
rfcomm_get_lm(int sock,int * sec_level)422 static int rfcomm_get_lm(int sock, int *sec_level)
423 {
424 int opt;
425 socklen_t len;
426
427 len = sizeof(opt);
428 if (getsockopt(sock, SOL_RFCOMM, RFCOMM_LM, &opt, &len) < 0)
429 return -errno;
430
431 *sec_level = 0;
432
433 if (opt & RFCOMM_LM_AUTH)
434 *sec_level = BT_SECURITY_LOW;
435 if (opt & RFCOMM_LM_ENCRYPT)
436 *sec_level = BT_SECURITY_MEDIUM;
437 if (opt & RFCOMM_LM_SECURE)
438 *sec_level = BT_SECURITY_HIGH;
439
440 return 0;
441 }
442
get_sec_level(int sock,BtIOType type,int * level,GError ** err)443 static gboolean get_sec_level(int sock, BtIOType type, int *level,
444 GError **err)
445 {
446 struct bt_security sec;
447 socklen_t len;
448 int ret;
449
450 memset(&sec, 0, sizeof(sec));
451 len = sizeof(sec);
452 if (getsockopt(sock, SOL_BLUETOOTH, BT_SECURITY, &sec, &len) == 0) {
453 *level = sec.level;
454 return TRUE;
455 }
456
457 if (errno != ENOPROTOOPT) {
458 ERROR_FAILED(err, "getsockopt(BT_SECURITY)", errno);
459 return FALSE;
460 }
461
462 if (type == BT_IO_L2CAP)
463 ret = l2cap_get_lm(sock, level);
464 else
465 ret = rfcomm_get_lm(sock, level);
466
467 if (ret < 0) {
468 ERROR_FAILED(err, "getsockopt(LM)", -ret);
469 return FALSE;
470 }
471
472 return TRUE;
473 }
474
l2cap_set(int sock,int sec_level,uint16_t imtu,uint16_t omtu,int master,GError ** err)475 static gboolean l2cap_set(int sock, int sec_level, uint16_t imtu,
476 uint16_t omtu, int master, GError **err)
477 {
478 if (imtu || omtu) {
479 struct l2cap_options l2o;
480 socklen_t len;
481
482 memset(&l2o, 0, sizeof(l2o));
483 len = sizeof(l2o);
484 if (getsockopt(sock, SOL_L2CAP, L2CAP_OPTIONS, &l2o,
485 &len) < 0) {
486 ERROR_FAILED(err, "getsockopt(L2CAP_OPTIONS)", errno);
487 return FALSE;
488 }
489
490 if (imtu)
491 l2o.imtu = imtu;
492 if (omtu)
493 l2o.omtu = omtu;
494
495 if (setsockopt(sock, SOL_L2CAP, L2CAP_OPTIONS, &l2o,
496 sizeof(l2o)) < 0) {
497 ERROR_FAILED(err, "setsockopt(L2CAP_OPTIONS)", errno);
498 return FALSE;
499 }
500 }
501
502 if (master >= 0 && l2cap_set_master(sock, master) < 0) {
503 ERROR_FAILED(err, "l2cap_set_master", errno);
504 return FALSE;
505 }
506
507 if (sec_level && !set_sec_level(sock, BT_IO_L2CAP, sec_level, err))
508 return FALSE;
509
510 return TRUE;
511 }
512
rfcomm_bind(int sock,const bdaddr_t * src,uint8_t channel)513 static int rfcomm_bind(int sock, const bdaddr_t *src, uint8_t channel)
514 {
515 struct sockaddr_rc addr;
516
517 memset(&addr, 0, sizeof(addr));
518 addr.rc_family = AF_BLUETOOTH;
519 bacpy(&addr.rc_bdaddr, src);
520 addr.rc_channel = channel;
521
522 return bind(sock, (struct sockaddr *) &addr, sizeof(addr));
523 }
524
rfcomm_connect(int sock,const bdaddr_t * dst,uint8_t channel)525 static int rfcomm_connect(int sock, const bdaddr_t *dst, uint8_t channel)
526 {
527 int err;
528 struct sockaddr_rc addr;
529
530 memset(&addr, 0, sizeof(addr));
531 addr.rc_family = AF_BLUETOOTH;
532 bacpy(&addr.rc_bdaddr, dst);
533 addr.rc_channel = channel;
534
535 err = connect(sock, (struct sockaddr *) &addr, sizeof(addr));
536 if (err < 0 && !(errno == EAGAIN || errno == EINPROGRESS))
537 return err;
538
539 return 0;
540 }
541
rfcomm_set(int sock,int sec_level,int master,GError ** err)542 static gboolean rfcomm_set(int sock, int sec_level, int master, GError **err)
543 {
544 if (sec_level && !set_sec_level(sock, BT_IO_RFCOMM, sec_level, err))
545 return FALSE;
546
547 if (master >= 0 && rfcomm_set_master(sock, master) < 0) {
548 ERROR_FAILED(err, "rfcomm_set_master", errno);
549 return FALSE;
550 }
551
552 return TRUE;
553 }
554
sco_bind(int sock,const bdaddr_t * src)555 static int sco_bind(int sock, const bdaddr_t *src)
556 {
557 struct sockaddr_sco addr;
558
559 memset(&addr, 0, sizeof(addr));
560 addr.sco_family = AF_BLUETOOTH;
561 bacpy(&addr.sco_bdaddr, src);
562
563 return bind(sock, (struct sockaddr *) &addr, sizeof(addr));
564 }
565
sco_connect(int sock,const bdaddr_t * dst)566 static int sco_connect(int sock, const bdaddr_t *dst)
567 {
568 struct sockaddr_sco addr;
569 int err;
570
571 memset(&addr, 0, sizeof(addr));
572 addr.sco_family = AF_BLUETOOTH;
573 bacpy(&addr.sco_bdaddr, dst);
574
575 err = connect(sock, (struct sockaddr *) &addr, sizeof(addr));
576 if (err < 0 && !(errno == EAGAIN || errno == EINPROGRESS))
577 return err;
578
579 return 0;
580 }
581
sco_set(int sock,uint16_t mtu,GError ** err)582 static gboolean sco_set(int sock, uint16_t mtu, GError **err)
583 {
584 struct sco_options sco_opt;
585 socklen_t len;
586
587 if (!mtu)
588 return TRUE;
589
590 len = sizeof(sco_opt);
591 memset(&sco_opt, 0, len);
592 if (getsockopt(sock, SOL_SCO, SCO_OPTIONS, &sco_opt, &len) < 0) {
593 ERROR_FAILED(err, "getsockopt(SCO_OPTIONS)", errno);
594 return FALSE;
595 }
596
597 sco_opt.mtu = mtu;
598 if (setsockopt(sock, SOL_SCO, SCO_OPTIONS, &sco_opt,
599 sizeof(sco_opt)) < 0) {
600 ERROR_FAILED(err, "setsockopt(SCO_OPTIONS)", errno);
601 return FALSE;
602 }
603
604 return TRUE;
605 }
606
parse_set_opts(struct set_opts * opts,GError ** err,BtIOOption opt1,va_list args)607 static gboolean parse_set_opts(struct set_opts *opts, GError **err,
608 BtIOOption opt1, va_list args)
609 {
610 BtIOOption opt = opt1;
611 const char *str;
612
613 memset(opts, 0, sizeof(*opts));
614
615 /* Set defaults */
616 opts->defer = DEFAULT_DEFER_TIMEOUT;
617 opts->master = -1;
618
619 while (opt != BT_IO_OPT_INVALID) {
620 switch (opt) {
621 case BT_IO_OPT_SOURCE:
622 str = va_arg(args, const char *);
623 if (strncasecmp(str, "hci", 3) == 0)
624 hci_devba(atoi(str + 3), &opts->src);
625 else
626 str2ba(str, &opts->src);
627 break;
628 case BT_IO_OPT_SOURCE_BDADDR:
629 bacpy(&opts->src, va_arg(args, const bdaddr_t *));
630 break;
631 case BT_IO_OPT_DEST:
632 str2ba(va_arg(args, const char *), &opts->dst);
633 break;
634 case BT_IO_OPT_DEST_BDADDR:
635 bacpy(&opts->dst, va_arg(args, const bdaddr_t *));
636 break;
637 case BT_IO_OPT_DEFER_TIMEOUT:
638 opts->defer = va_arg(args, int);
639 break;
640 case BT_IO_OPT_SEC_LEVEL:
641 opts->sec_level = va_arg(args, int);
642 break;
643 case BT_IO_OPT_CHANNEL:
644 opts->channel = va_arg(args, int);
645 break;
646 case BT_IO_OPT_PSM:
647 opts->psm = va_arg(args, int);
648 break;
649 case BT_IO_OPT_MTU:
650 opts->mtu = va_arg(args, int);
651 opts->imtu = opts->mtu;
652 opts->omtu = opts->mtu;
653 break;
654 case BT_IO_OPT_OMTU:
655 opts->omtu = va_arg(args, int);
656 if (!opts->mtu)
657 opts->mtu = opts->omtu;
658 break;
659 case BT_IO_OPT_IMTU:
660 opts->imtu = va_arg(args, int);
661 if (!opts->mtu)
662 opts->mtu = opts->imtu;
663 break;
664 case BT_IO_OPT_MASTER:
665 opts->master = va_arg(args, gboolean);
666 break;
667 default:
668 g_set_error(err, BT_IO_ERROR, BT_IO_ERROR_INVALID_ARGS,
669 "Unknown option %d", opt);
670 return FALSE;
671 }
672
673 opt = va_arg(args, int);
674 }
675
676 return TRUE;
677 }
678
get_peers(int sock,struct sockaddr * src,struct sockaddr * dst,socklen_t len,GError ** err)679 static gboolean get_peers(int sock, struct sockaddr *src, struct sockaddr *dst,
680 socklen_t len, GError **err)
681 {
682 socklen_t olen;
683
684 memset(src, 0, len);
685 olen = len;
686 if (getsockname(sock, src, &olen) < 0) {
687 ERROR_FAILED(err, "getsockname", errno);
688 return FALSE;
689 }
690
691 memset(dst, 0, len);
692 olen = len;
693 if (getpeername(sock, dst, &olen) < 0) {
694 ERROR_FAILED(err, "getpeername", errno);
695 return FALSE;
696 }
697
698 return TRUE;
699 }
700
l2cap_get_info(int sock,uint16_t * handle,uint8_t * dev_class)701 static int l2cap_get_info(int sock, uint16_t *handle, uint8_t *dev_class)
702 {
703 struct l2cap_conninfo info;
704 socklen_t len;
705
706 len = sizeof(info);
707 if (getsockopt(sock, SOL_L2CAP, L2CAP_CONNINFO, &info, &len) < 0)
708 return -errno;
709
710 if (handle)
711 *handle = info.hci_handle;
712
713 if (dev_class)
714 memcpy(dev_class, info.dev_class, 3);
715
716 return 0;
717 }
718
l2cap_get(int sock,GError ** err,BtIOOption opt1,va_list args)719 static gboolean l2cap_get(int sock, GError **err, BtIOOption opt1,
720 va_list args)
721 {
722 BtIOOption opt = opt1;
723 struct sockaddr_l2 src, dst;
724 struct l2cap_options l2o;
725 int flags;
726 uint8_t dev_class[3];
727 uint16_t handle;
728 socklen_t len;
729
730 len = sizeof(l2o);
731 memset(&l2o, 0, len);
732 if (getsockopt(sock, SOL_L2CAP, L2CAP_OPTIONS, &l2o, &len) < 0) {
733 ERROR_FAILED(err, "getsockopt(L2CAP_OPTIONS)", errno);
734 return FALSE;
735 }
736
737 if (!get_peers(sock, (struct sockaddr *) &src,
738 (struct sockaddr *) &dst, sizeof(src), err))
739 return FALSE;
740
741 while (opt != BT_IO_OPT_INVALID) {
742 switch (opt) {
743 case BT_IO_OPT_SOURCE:
744 ba2str(&src.l2_bdaddr, va_arg(args, char *));
745 break;
746 case BT_IO_OPT_SOURCE_BDADDR:
747 bacpy(va_arg(args, bdaddr_t *), &src.l2_bdaddr);
748 break;
749 case BT_IO_OPT_DEST:
750 ba2str(&dst.l2_bdaddr, va_arg(args, char *));
751 break;
752 case BT_IO_OPT_DEST_BDADDR:
753 bacpy(va_arg(args, bdaddr_t *), &dst.l2_bdaddr);
754 break;
755 case BT_IO_OPT_DEFER_TIMEOUT:
756 len = sizeof(int);
757 if (getsockopt(sock, SOL_BLUETOOTH, BT_DEFER_SETUP,
758 va_arg(args, int *), &len) < 0) {
759 ERROR_FAILED(err, "getsockopt(DEFER_SETUP)",
760 errno);
761 return FALSE;
762 }
763 break;
764 case BT_IO_OPT_SEC_LEVEL:
765 if (!get_sec_level(sock, BT_IO_L2CAP,
766 va_arg(args, int *), err))
767 return FALSE;
768 break;
769 case BT_IO_OPT_PSM:
770 *(va_arg(args, uint16_t *)) = src.l2_psm ?
771 src.l2_psm : dst.l2_psm;
772 break;
773 case BT_IO_OPT_OMTU:
774 *(va_arg(args, uint16_t *)) = l2o.omtu;
775 break;
776 case BT_IO_OPT_IMTU:
777 *(va_arg(args, uint16_t *)) = l2o.imtu;
778 break;
779 case BT_IO_OPT_MASTER:
780 len = sizeof(flags);
781 if (getsockopt(sock, SOL_L2CAP, L2CAP_LM, &flags,
782 &len) < 0) {
783 ERROR_FAILED(err, "getsockopt(L2CAP_LM)",
784 errno);
785 return FALSE;
786 }
787 *(va_arg(args, gboolean *)) =
788 (flags & L2CAP_LM_MASTER) ? TRUE : FALSE;
789 break;
790 case BT_IO_OPT_HANDLE:
791 if (l2cap_get_info(sock, &handle, dev_class) < 0) {
792 ERROR_FAILED(err, "L2CAP_CONNINFO", errno);
793 return FALSE;
794 }
795 *(va_arg(args, uint16_t *)) = handle;
796 break;
797 case BT_IO_OPT_CLASS:
798 if (l2cap_get_info(sock, &handle, dev_class) < 0) {
799 ERROR_FAILED(err, "L2CAP_CONNINFO", errno);
800 return FALSE;
801 }
802 memcpy(va_arg(args, uint8_t *), dev_class, 3);
803 break;
804 default:
805 g_set_error(err, BT_IO_ERROR, BT_IO_ERROR_INVALID_ARGS,
806 "Unknown option %d", opt);
807 return FALSE;
808 }
809
810 opt = va_arg(args, int);
811 }
812
813 return TRUE;
814 }
815
rfcomm_get_info(int sock,uint16_t * handle,uint8_t * dev_class)816 static int rfcomm_get_info(int sock, uint16_t *handle, uint8_t *dev_class)
817 {
818 struct rfcomm_conninfo info;
819 socklen_t len;
820
821 len = sizeof(info);
822 if (getsockopt(sock, SOL_RFCOMM, RFCOMM_CONNINFO, &info, &len) < 0)
823 return -errno;
824
825 if (handle)
826 *handle = info.hci_handle;
827
828 if (dev_class)
829 memcpy(dev_class, info.dev_class, 3);
830
831 return 0;
832 }
833
rfcomm_get(int sock,GError ** err,BtIOOption opt1,va_list args)834 static gboolean rfcomm_get(int sock, GError **err, BtIOOption opt1,
835 va_list args)
836 {
837 BtIOOption opt = opt1;
838 struct sockaddr_rc src, dst;
839 int flags;
840 socklen_t len;
841 uint8_t dev_class[3];
842 uint16_t handle;
843
844 if (!get_peers(sock, (struct sockaddr *) &src,
845 (struct sockaddr *) &dst, sizeof(src), err))
846 return FALSE;
847
848 while (opt != BT_IO_OPT_INVALID) {
849 switch (opt) {
850 case BT_IO_OPT_SOURCE:
851 ba2str(&src.rc_bdaddr, va_arg(args, char *));
852 break;
853 case BT_IO_OPT_SOURCE_BDADDR:
854 bacpy(va_arg(args, bdaddr_t *), &src.rc_bdaddr);
855 break;
856 case BT_IO_OPT_DEST:
857 ba2str(&dst.rc_bdaddr, va_arg(args, char *));
858 break;
859 case BT_IO_OPT_DEST_BDADDR:
860 bacpy(va_arg(args, bdaddr_t *), &dst.rc_bdaddr);
861 break;
862 case BT_IO_OPT_DEFER_TIMEOUT:
863 len = sizeof(int);
864 if (getsockopt(sock, SOL_BLUETOOTH, BT_DEFER_SETUP,
865 va_arg(args, int *), &len) < 0) {
866 ERROR_FAILED(err, "getsockopt(DEFER_SETUP)",
867 errno);
868 return FALSE;
869 }
870 break;
871 case BT_IO_OPT_SEC_LEVEL:
872 if (!get_sec_level(sock, BT_IO_RFCOMM,
873 va_arg(args, int *), err))
874 return FALSE;
875 break;
876 case BT_IO_OPT_CHANNEL:
877 *(va_arg(args, uint8_t *)) = src.rc_channel ?
878 src.rc_channel : dst.rc_channel;
879 break;
880 case BT_IO_OPT_MASTER:
881 len = sizeof(flags);
882 if (getsockopt(sock, SOL_RFCOMM, RFCOMM_LM, &flags,
883 &len) < 0) {
884 ERROR_FAILED(err, "getsockopt(RFCOMM_LM)",
885 errno);
886 return FALSE;
887 }
888 *(va_arg(args, gboolean *)) =
889 (flags & RFCOMM_LM_MASTER) ? TRUE : FALSE;
890 break;
891 case BT_IO_OPT_HANDLE:
892 if (rfcomm_get_info(sock, &handle, dev_class) < 0) {
893 ERROR_FAILED(err, "RFCOMM_CONNINFO", errno);
894 return FALSE;
895 }
896 *(va_arg(args, uint16_t *)) = handle;
897 break;
898 case BT_IO_OPT_CLASS:
899 if (rfcomm_get_info(sock, &handle, dev_class) < 0) {
900 ERROR_FAILED(err, "RFCOMM_CONNINFO", errno);
901 return FALSE;
902 }
903 memcpy(va_arg(args, uint8_t *), dev_class, 3);
904 break;
905 default:
906 g_set_error(err, BT_IO_ERROR, BT_IO_ERROR_INVALID_ARGS,
907 "Unknown option %d", opt);
908 return FALSE;
909 }
910
911 opt = va_arg(args, int);
912 }
913
914 return TRUE;
915 }
916
sco_get_info(int sock,uint16_t * handle,uint8_t * dev_class)917 static int sco_get_info(int sock, uint16_t *handle, uint8_t *dev_class)
918 {
919 struct sco_conninfo info;
920 socklen_t len;
921
922 len = sizeof(info);
923 if (getsockopt(sock, SOL_SCO, SCO_CONNINFO, &info, &len) < 0)
924 return -errno;
925
926 if (handle)
927 *handle = info.hci_handle;
928
929 if (dev_class)
930 memcpy(dev_class, info.dev_class, 3);
931
932 return 0;
933 }
934
sco_get(int sock,GError ** err,BtIOOption opt1,va_list args)935 static gboolean sco_get(int sock, GError **err, BtIOOption opt1, va_list args)
936 {
937 BtIOOption opt = opt1;
938 struct sockaddr_sco src, dst;
939 struct sco_options sco_opt;
940 socklen_t len;
941 uint8_t dev_class[3];
942 uint16_t handle;
943
944 len = sizeof(sco_opt);
945 memset(&sco_opt, 0, len);
946 if (getsockopt(sock, SOL_SCO, SCO_OPTIONS, &sco_opt, &len) < 0) {
947 ERROR_FAILED(err, "getsockopt(SCO_OPTIONS)", errno);
948 return FALSE;
949 }
950
951 if (!get_peers(sock, (struct sockaddr *) &src,
952 (struct sockaddr *) &dst, sizeof(src), err))
953 return FALSE;
954
955 while (opt != BT_IO_OPT_INVALID) {
956 switch (opt) {
957 case BT_IO_OPT_SOURCE:
958 ba2str(&src.sco_bdaddr, va_arg(args, char *));
959 break;
960 case BT_IO_OPT_SOURCE_BDADDR:
961 bacpy(va_arg(args, bdaddr_t *), &src.sco_bdaddr);
962 break;
963 case BT_IO_OPT_DEST:
964 ba2str(&dst.sco_bdaddr, va_arg(args, char *));
965 break;
966 case BT_IO_OPT_DEST_BDADDR:
967 bacpy(va_arg(args, bdaddr_t *), &dst.sco_bdaddr);
968 break;
969 case BT_IO_OPT_MTU:
970 case BT_IO_OPT_IMTU:
971 case BT_IO_OPT_OMTU:
972 *(va_arg(args, uint16_t *)) = sco_opt.mtu;
973 break;
974 case BT_IO_OPT_HANDLE:
975 if (sco_get_info(sock, &handle, dev_class) < 0) {
976 ERROR_FAILED(err, "RFCOMM_CONNINFO", errno);
977 return FALSE;
978 }
979 *(va_arg(args, uint16_t *)) = handle;
980 break;
981 case BT_IO_OPT_CLASS:
982 if (sco_get_info(sock, &handle, dev_class) < 0) {
983 ERROR_FAILED(err, "RFCOMM_CONNINFO", errno);
984 return FALSE;
985 }
986 memcpy(va_arg(args, uint8_t *), dev_class, 3);
987 break;
988 default:
989 g_set_error(err, BT_IO_ERROR, BT_IO_ERROR_INVALID_ARGS,
990 "Unknown option %d", opt);
991 return FALSE;
992 }
993
994 opt = va_arg(args, int);
995 }
996
997 return TRUE;
998 }
999
get_valist(GIOChannel * io,BtIOType type,GError ** err,BtIOOption opt1,va_list args)1000 static gboolean get_valist(GIOChannel *io, BtIOType type, GError **err,
1001 BtIOOption opt1, va_list args)
1002 {
1003 int sock;
1004
1005 sock = g_io_channel_unix_get_fd(io);
1006
1007 switch (type) {
1008 case BT_IO_L2RAW:
1009 case BT_IO_L2CAP:
1010 return l2cap_get(sock, err, opt1, args);
1011 case BT_IO_RFCOMM:
1012 return rfcomm_get(sock, err, opt1, args);
1013 case BT_IO_SCO:
1014 return sco_get(sock, err, opt1, args);
1015 }
1016
1017 g_set_error(err, BT_IO_ERROR, BT_IO_ERROR_INVALID_ARGS,
1018 "Unknown BtIO type %d", type);
1019 return FALSE;
1020 }
1021
bt_io_accept(GIOChannel * io,BtIOConnect connect,gpointer user_data,GDestroyNotify destroy,GError ** err)1022 gboolean bt_io_accept(GIOChannel *io, BtIOConnect connect, gpointer user_data,
1023 GDestroyNotify destroy, GError **err)
1024 {
1025 int sock;
1026 char c;
1027 struct pollfd pfd;
1028
1029 sock = g_io_channel_unix_get_fd(io);
1030
1031 memset(&pfd, 0, sizeof(pfd));
1032 pfd.fd = sock;
1033 pfd.events = POLLOUT;
1034
1035 if (poll(&pfd, 1, 0) < 0) {
1036 ERROR_FAILED(err, "poll", errno);
1037 return FALSE;
1038 }
1039
1040 if (!(pfd.revents & POLLOUT)) {
1041 int ret;
1042 ret = read(sock, &c, 1);
1043 }
1044
1045 accept_add(io, connect, user_data, destroy);
1046
1047 return TRUE;
1048 }
1049
bt_io_set(GIOChannel * io,BtIOType type,GError ** err,BtIOOption opt1,...)1050 gboolean bt_io_set(GIOChannel *io, BtIOType type, GError **err,
1051 BtIOOption opt1, ...)
1052 {
1053 va_list args;
1054 gboolean ret;
1055 struct set_opts opts;
1056 int sock;
1057
1058 va_start(args, opt1);
1059 ret = parse_set_opts(&opts, err, opt1, args);
1060 va_end(args);
1061
1062 if (!ret)
1063 return ret;
1064
1065 sock = g_io_channel_unix_get_fd(io);
1066
1067 switch (type) {
1068 case BT_IO_L2RAW:
1069 case BT_IO_L2CAP:
1070 return l2cap_set(sock, opts.sec_level, opts.imtu, opts.omtu,
1071 opts.master, err);
1072 case BT_IO_RFCOMM:
1073 return rfcomm_set(sock, opts.sec_level, opts.master, err);
1074 case BT_IO_SCO:
1075 return sco_set(sock, opts.mtu, err);
1076 }
1077
1078 g_set_error(err, BT_IO_ERROR, BT_IO_ERROR_INVALID_ARGS,
1079 "Unknown BtIO type %d", type);
1080 return FALSE;
1081 }
1082
bt_io_get(GIOChannel * io,BtIOType type,GError ** err,BtIOOption opt1,...)1083 gboolean bt_io_get(GIOChannel *io, BtIOType type, GError **err,
1084 BtIOOption opt1, ...)
1085 {
1086 va_list args;
1087 gboolean ret;
1088
1089 va_start(args, opt1);
1090 ret = get_valist(io, type, err, opt1, args);
1091 va_end(args);
1092
1093 return ret;
1094 }
1095
create_io(BtIOType type,gboolean server,struct set_opts * opts,GError ** err)1096 static GIOChannel *create_io(BtIOType type, gboolean server,
1097 struct set_opts *opts, GError **err)
1098 {
1099 int sock;
1100 GIOChannel *io;
1101
1102 switch (type) {
1103 case BT_IO_L2RAW:
1104 sock = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_L2CAP);
1105 if (sock < 0) {
1106 ERROR_FAILED(err, "socket(RAW, L2CAP)", errno);
1107 return NULL;
1108 }
1109 if (l2cap_bind(sock, &opts->src, server ? opts->psm : 0) < 0) {
1110 ERROR_FAILED(err, "l2cap_bind", errno);
1111 return NULL;
1112 }
1113 if (!l2cap_set(sock, opts->sec_level, 0, 0, -1, err))
1114 return NULL;
1115 break;
1116 case BT_IO_L2CAP:
1117 sock = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
1118 if (sock < 0) {
1119 ERROR_FAILED(err, "socket(SEQPACKET, L2CAP)", errno);
1120 return NULL;
1121 }
1122 if (l2cap_bind(sock, &opts->src, server ? opts->psm : 0) < 0) {
1123 ERROR_FAILED(err, "l2cap_bind", errno);
1124 return NULL;
1125 }
1126 if (!l2cap_set(sock, opts->sec_level, opts->imtu, opts->omtu,
1127 opts->master, err))
1128 return NULL;
1129 break;
1130 case BT_IO_RFCOMM:
1131 sock = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
1132 if (sock < 0) {
1133 ERROR_FAILED(err, "socket(STREAM, RFCOMM)", errno);
1134 return NULL;
1135 }
1136 if (rfcomm_bind(sock, &opts->src,
1137 server ? opts->channel : 0) < 0) {
1138 ERROR_FAILED(err, "rfcomm_bind", errno);
1139 return NULL;
1140 }
1141 if (!rfcomm_set(sock, opts->sec_level, opts->master, err))
1142 return NULL;
1143 break;
1144 case BT_IO_SCO:
1145 sock = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO);
1146 if (sock < 0) {
1147 ERROR_FAILED(err, "socket(SEQPACKET, SCO)", errno);
1148 return NULL;
1149 }
1150 if (sco_bind(sock, &opts->src) < 0) {
1151 ERROR_FAILED(err, "sco_bind", errno);
1152 return NULL;
1153 }
1154 if (!sco_set(sock, opts->mtu, err))
1155 return NULL;
1156 break;
1157 default:
1158 g_set_error(err, BT_IO_ERROR, BT_IO_ERROR_INVALID_ARGS,
1159 "Unknown BtIO type %d", type);
1160 return NULL;
1161 }
1162
1163 io = g_io_channel_unix_new(sock);
1164
1165 g_io_channel_set_close_on_unref(io, TRUE);
1166 g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
1167
1168 return io;
1169 }
1170
bt_io_connect(BtIOType type,BtIOConnect connect,gpointer user_data,GDestroyNotify destroy,GError ** gerr,BtIOOption opt1,...)1171 GIOChannel *bt_io_connect(BtIOType type, BtIOConnect connect,
1172 gpointer user_data, GDestroyNotify destroy,
1173 GError **gerr, BtIOOption opt1, ...)
1174 {
1175 GIOChannel *io;
1176 va_list args;
1177 struct set_opts opts;
1178 int err, sock;
1179 gboolean ret;
1180
1181 va_start(args, opt1);
1182 ret = parse_set_opts(&opts, gerr, opt1, args);
1183 va_end(args);
1184
1185 if (ret == FALSE)
1186 return NULL;
1187
1188 io = create_io(type, FALSE, &opts, gerr);
1189 if (io == NULL)
1190 return NULL;
1191
1192 sock = g_io_channel_unix_get_fd(io);
1193
1194 switch (type) {
1195 case BT_IO_L2RAW:
1196 err = l2cap_connect(sock, &opts.dst, 0);
1197 break;
1198 case BT_IO_L2CAP:
1199 err = l2cap_connect(sock, &opts.dst, opts.psm);
1200 break;
1201 case BT_IO_RFCOMM:
1202 err = rfcomm_connect(sock, &opts.dst, opts.channel);
1203 break;
1204 case BT_IO_SCO:
1205 err = sco_connect(sock, &opts.dst);
1206 break;
1207 default:
1208 g_set_error(gerr, BT_IO_ERROR, BT_IO_ERROR_INVALID_ARGS,
1209 "Unknown BtIO type %d", type);
1210 return NULL;
1211 }
1212
1213 if (err < 0) {
1214 g_set_error(gerr, BT_IO_ERROR, BT_IO_ERROR_CONNECT_FAILED,
1215 "connect: %s (%d)", strerror(-err), -err);
1216 g_io_channel_unref(io);
1217 return NULL;
1218 }
1219
1220 connect_add(io, connect, user_data, destroy);
1221
1222 return io;
1223 }
1224
bt_io_listen(BtIOType type,BtIOConnect connect,BtIOConfirm confirm,gpointer user_data,GDestroyNotify destroy,GError ** err,BtIOOption opt1,...)1225 GIOChannel *bt_io_listen(BtIOType type, BtIOConnect connect,
1226 BtIOConfirm confirm, gpointer user_data,
1227 GDestroyNotify destroy, GError **err,
1228 BtIOOption opt1, ...)
1229 {
1230 GIOChannel *io;
1231 va_list args;
1232 struct set_opts opts;
1233 int sock;
1234 gboolean ret;
1235
1236 if (type == BT_IO_L2RAW) {
1237 g_set_error(err, BT_IO_ERROR, BT_IO_ERROR_INVALID_ARGS,
1238 "Server L2CAP RAW sockets not supported");
1239 return NULL;
1240 }
1241
1242 va_start(args, opt1);
1243 ret = parse_set_opts(&opts, err, opt1, args);
1244 va_end(args);
1245
1246 if (ret == FALSE)
1247 return NULL;
1248
1249 io = create_io(type, TRUE, &opts, err);
1250 if (io == NULL)
1251 return NULL;
1252
1253 sock = g_io_channel_unix_get_fd(io);
1254
1255 if (confirm)
1256 setsockopt(sock, SOL_BLUETOOTH, BT_DEFER_SETUP, &opts.defer,
1257 sizeof(opts.defer));
1258
1259 if (listen(sock, 5) < 0) {
1260 ERROR_FAILED(err, "listen", errno);
1261 g_io_channel_unref(io);
1262 return NULL;
1263 }
1264
1265 server_add(io, connect, confirm, user_data, destroy);
1266
1267 return io;
1268 }
1269
bt_io_error_quark(void)1270 GQuark bt_io_error_quark(void)
1271 {
1272 return g_quark_from_static_string("bt-io-error-quark");
1273 }
1274
1275