1 /*
2 *
3 * BlueZ - Bluetooth protocol stack for Linux
4 *
5 * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 *
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <sys/types.h>
32 #include <sys/ioctl.h>
33 #include <sys/wait.h>
34
35 #include <bluetooth/bluetooth.h>
36 #include <bluetooth/hci.h>
37 #include <bluetooth/hci_lib.h>
38
39 #include <glib.h>
40
41 #include "hcid.h"
42 #include "sdpd.h"
43 #include "adapter.h"
44 #include "plugin.h"
45 #include "log.h"
46 #include "manager.h"
47
48 static int child_pipe[2] = { -1, -1 };
49
50 static guint child_io_id = 0;
51 static guint ctl_io_id = 0;
52
child_exit(GIOChannel * io,GIOCondition cond,void * user_data)53 static gboolean child_exit(GIOChannel *io, GIOCondition cond, void *user_data)
54 {
55 int status, fd = g_io_channel_unix_get_fd(io);
56 pid_t child_pid;
57
58 if (read(fd, &child_pid, sizeof(child_pid)) != sizeof(child_pid)) {
59 error("child_exit: unable to read child pid from pipe");
60 return TRUE;
61 }
62
63 if (waitpid(child_pid, &status, 0) != child_pid)
64 error("waitpid(%d) failed", child_pid);
65 else
66 DBG("child %d exited", child_pid);
67
68 return TRUE;
69 }
70
at_child_exit(void)71 static void at_child_exit(void)
72 {
73 pid_t pid = getpid();
74
75 if (write(child_pipe[1], &pid, sizeof(pid)) != sizeof(pid))
76 error("unable to write to child pipe");
77 }
78
device_devup_setup(int index)79 static void device_devup_setup(int index)
80 {
81 struct hci_dev_info di;
82 uint16_t policy;
83 int dd, err;
84
85 if (hci_devinfo(index, &di) < 0)
86 return;
87
88 if (hci_test_bit(HCI_RAW, &di.flags))
89 return;
90
91 dd = hci_open_dev(index);
92 if (dd < 0) {
93 err = errno;
94 error("Can't open device hci%d: %s (%d)",
95 index, strerror(err), err);
96 return;
97 }
98
99 /* Set page timeout */
100 if ((main_opts.flags & (1 << HCID_SET_PAGETO))) {
101 write_page_timeout_cp cp;
102
103 cp.timeout = htobs(main_opts.pageto);
104 hci_send_cmd(dd, OGF_HOST_CTL, OCF_WRITE_PAGE_TIMEOUT,
105 WRITE_PAGE_TIMEOUT_CP_SIZE, &cp);
106 }
107
108 /* Set default link policy */
109 policy = htobs(main_opts.link_policy);
110 hci_send_cmd(dd, OGF_LINK_POLICY,
111 OCF_WRITE_DEFAULT_LINK_POLICY, 2, &policy);
112
113 hci_close_dev(dd);
114
115 start_security_manager(index);
116
117 /* Return value 1 means ioctl(DEVDOWN) was performed */
118 if (manager_start_adapter(index) == 1)
119 stop_security_manager(index);
120 }
121
init_device(int index)122 static void init_device(int index)
123 {
124 struct hci_dev_req dr;
125 struct hci_dev_info di;
126 pid_t pid;
127 int dd, err;
128
129 /* Do initialization in the separate process */
130 pid = fork();
131 switch (pid) {
132 case 0:
133 atexit(at_child_exit);
134 break;
135 case -1:
136 err = errno;
137 error("Fork failed. Can't init device hci%d: %s (%d)",
138 index, strerror(err), err);
139 default:
140 DBG("child %d forked", pid);
141 return;
142 }
143
144 dd = hci_open_dev(index);
145 if (dd < 0) {
146 err = errno;
147 error("Can't open device hci%d: %s (%d)",
148 index, strerror(err), err);
149 exit(1);
150 }
151
152 memset(&dr, 0, sizeof(dr));
153 dr.dev_id = index;
154
155 /* Set link mode */
156 dr.dev_opt = main_opts.link_mode;
157 if (ioctl(dd, HCISETLINKMODE, (unsigned long) &dr) < 0) {
158 err = errno;
159 error("Can't set link mode on hci%d: %s (%d)",
160 index, strerror(err), err);
161 }
162
163 /* Set link policy */
164 dr.dev_opt = main_opts.link_policy;
165 if (ioctl(dd, HCISETLINKPOL, (unsigned long) &dr) < 0 &&
166 errno != ENETDOWN) {
167 error("Can't set link policy on hci%d: %s (%d)",
168 index, strerror(errno), errno);
169 }
170
171 /* Start HCI device */
172 if (ioctl(dd, HCIDEVUP, index) < 0 && errno != EALREADY) {
173 error("Can't init device hci%d: %s (%d)",
174 index, strerror(errno), errno);
175 goto fail;
176 }
177
178 if (hci_devinfo(index, &di) < 0)
179 goto fail;
180
181 if (hci_test_bit(HCI_RAW, &di.flags))
182 goto done;
183
184 done:
185 hci_close_dev(dd);
186 exit(0);
187
188 fail:
189 hci_close_dev(dd);
190 exit(1);
191 }
192
device_devreg_setup(int index)193 static void device_devreg_setup(int index)
194 {
195 struct hci_dev_info di;
196 gboolean devup;
197
198 init_device(index);
199
200 memset(&di, 0, sizeof(di));
201
202 if (hci_devinfo(index, &di) < 0)
203 return;
204
205 devup = hci_test_bit(HCI_UP, &di.flags);
206
207 if (!hci_test_bit(HCI_RAW, &di.flags))
208 manager_register_adapter(index, devup);
209 }
210
device_event(int event,int index)211 static void device_event(int event, int index)
212 {
213 switch (event) {
214 case HCI_DEV_REG:
215 info("HCI dev %d registered", index);
216 device_devreg_setup(index);
217 break;
218
219 case HCI_DEV_UNREG:
220 info("HCI dev %d unregistered", index);
221 manager_unregister_adapter(index);
222 break;
223
224 case HCI_DEV_UP:
225 info("HCI dev %d up", index);
226 device_devup_setup(index);
227 break;
228
229 case HCI_DEV_DOWN:
230 info("HCI dev %d down", index);
231 manager_stop_adapter(index);
232 stop_security_manager(index);
233 break;
234 }
235 }
236
init_known_adapters(int ctl)237 static int init_known_adapters(int ctl)
238 {
239 struct hci_dev_list_req *dl;
240 struct hci_dev_req *dr;
241 int i, err;
242
243 dl = g_try_malloc0(HCI_MAX_DEV * sizeof(struct hci_dev_req) + sizeof(uint16_t));
244 if (!dl) {
245 err = errno;
246 error("Can't allocate devlist buffer: %s (%d)",
247 strerror(err), err);
248 return -err;
249 }
250
251 dl->dev_num = HCI_MAX_DEV;
252 dr = dl->dev_req;
253
254 if (ioctl(ctl, HCIGETDEVLIST, (void *) dl) < 0) {
255 err = errno;
256 error("Can't get device list: %s (%d)",
257 strerror(err), err);
258 g_free(dl);
259 return -err;
260 }
261
262 for (i = 0; i < dl->dev_num; i++, dr++) {
263 device_event(HCI_DEV_REG, dr->dev_id);
264
265 if (hci_test_bit(HCI_UP, &dr->dev_opt))
266 device_event(HCI_DEV_UP, dr->dev_id);
267 }
268
269 g_free(dl);
270 return 0;
271 }
272
io_stack_event(GIOChannel * chan,GIOCondition cond,gpointer data)273 static gboolean io_stack_event(GIOChannel *chan, GIOCondition cond,
274 gpointer data)
275 {
276 unsigned char buf[HCI_MAX_FRAME_SIZE], *ptr;
277 evt_stack_internal *si;
278 evt_si_device *sd;
279 hci_event_hdr *eh;
280 int type;
281 size_t len;
282 GIOError err;
283
284 ptr = buf;
285
286 err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf), &len);
287 if (err) {
288 if (err == G_IO_ERROR_AGAIN)
289 return TRUE;
290
291 error("Read from control socket failed: %s (%d)",
292 strerror(errno), errno);
293 return FALSE;
294 }
295
296 type = *ptr++;
297
298 if (type != HCI_EVENT_PKT)
299 return TRUE;
300
301 eh = (hci_event_hdr *) ptr;
302 if (eh->evt != EVT_STACK_INTERNAL)
303 return TRUE;
304
305 ptr += HCI_EVENT_HDR_SIZE;
306
307 si = (evt_stack_internal *) ptr;
308 switch (si->type) {
309 case EVT_SI_DEVICE:
310 sd = (void *) &si->data;
311 device_event(sd->event, sd->dev_id);
312 break;
313 }
314
315 return TRUE;
316 }
317
hciops_setup(void)318 static int hciops_setup(void)
319 {
320 struct sockaddr_hci addr;
321 struct hci_filter flt;
322 GIOChannel *ctl_io, *child_io;
323 int sock, err;
324
325 if (child_pipe[0] != -1)
326 return -EALREADY;
327
328 if (pipe(child_pipe) < 0) {
329 err = errno;
330 error("pipe(): %s (%d)", strerror(err), err);
331 return -err;
332 }
333
334 child_io = g_io_channel_unix_new(child_pipe[0]);
335 g_io_channel_set_close_on_unref(child_io, TRUE);
336 child_io_id = g_io_add_watch(child_io,
337 G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
338 child_exit, NULL);
339 g_io_channel_unref(child_io);
340
341 /* Create and bind HCI socket */
342 sock = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
343 if (sock < 0) {
344 err = errno;
345 error("Can't open HCI socket: %s (%d)", strerror(err),
346 err);
347 return -err;
348 }
349
350 /* Set filter */
351 hci_filter_clear(&flt);
352 hci_filter_set_ptype(HCI_EVENT_PKT, &flt);
353 hci_filter_set_event(EVT_STACK_INTERNAL, &flt);
354 if (setsockopt(sock, SOL_HCI, HCI_FILTER, &flt,
355 sizeof(flt)) < 0) {
356 err = errno;
357 error("Can't set filter: %s (%d)", strerror(err), err);
358 return -err;
359 }
360
361 memset(&addr, 0, sizeof(addr));
362 addr.hci_family = AF_BLUETOOTH;
363 addr.hci_dev = HCI_DEV_NONE;
364 if (bind(sock, (struct sockaddr *) &addr,
365 sizeof(addr)) < 0) {
366 err = errno;
367 error("Can't bind HCI socket: %s (%d)",
368 strerror(err), err);
369 return -err;
370 }
371
372 ctl_io = g_io_channel_unix_new(sock);
373 g_io_channel_set_close_on_unref(ctl_io, TRUE);
374
375 ctl_io_id = g_io_add_watch(ctl_io, G_IO_IN, io_stack_event, NULL);
376
377 g_io_channel_unref(ctl_io);
378
379 /* Initialize already connected devices */
380 return init_known_adapters(sock);
381 }
382
hciops_cleanup(void)383 static void hciops_cleanup(void)
384 {
385 if (child_io_id) {
386 g_source_remove(child_io_id);
387 child_io_id = 0;
388 }
389
390 if (ctl_io_id) {
391 g_source_remove(ctl_io_id);
392 ctl_io_id = 0;
393 }
394
395 if (child_pipe[0] >= 0) {
396 close(child_pipe[0]);
397 child_pipe[0] = -1;
398 }
399
400 if (child_pipe[1] >= 0) {
401 close(child_pipe[1]);
402 child_pipe[1] = -1;
403 }
404 }
405
hciops_start(int index)406 static int hciops_start(int index)
407 {
408 int dd;
409 int err = 0;
410
411 dd = hci_open_dev(index);
412 if (dd < 0)
413 return -EIO;
414
415 if (ioctl(dd, HCIDEVUP, index) == 0)
416 goto done; /* on success */
417
418 if (errno != EALREADY) {
419 err = errno;
420 error("Can't init device hci%d: %s (%d)",
421 index, strerror(err), err);
422 }
423
424 done:
425 hci_close_dev(dd);
426 return -err;
427 }
428
hciops_stop(int index)429 static int hciops_stop(int index)
430 {
431 int dd;
432 int err = 0;
433
434 dd = hci_open_dev(index);
435 if (dd < 0)
436 return -EIO;
437
438 if (ioctl(dd, HCIDEVDOWN, index) == 0)
439 goto done; /* on success */
440
441 if (errno != EALREADY) {
442 err = errno;
443 error("Can't stop device hci%d: %s (%d)",
444 index, strerror(err), err);
445 }
446
447 done:
448 hci_close_dev(dd);
449 return -err;
450 }
451
hciops_powered(int index,gboolean powered)452 static int hciops_powered(int index, gboolean powered)
453 {
454 int dd, err;
455 uint8_t mode = SCAN_DISABLED;
456
457 if (powered)
458 return hciops_start(index);
459
460 dd = hci_open_dev(index);
461 if (dd < 0)
462 return -EIO;
463
464 err = hci_send_cmd(dd, OGF_HOST_CTL, OCF_WRITE_SCAN_ENABLE,
465 1, &mode);
466 if (err < 0) {
467 err = -errno;
468 hci_close_dev(dd);
469 return err;
470 }
471
472 hci_close_dev(dd);
473
474 return hciops_stop(index);
475 }
476
hciops_connectable(int index)477 static int hciops_connectable(int index)
478 {
479 int dd, err;
480 uint8_t mode = SCAN_PAGE;
481
482 dd = hci_open_dev(index);
483 if (dd < 0)
484 return -EIO;
485
486 err = hci_send_cmd(dd, OGF_HOST_CTL, OCF_WRITE_SCAN_ENABLE,
487 1, &mode);
488 if (err < 0)
489 err = -errno;
490
491 hci_close_dev(dd);
492
493 return err;
494 }
495
hciops_discoverable(int index)496 static int hciops_discoverable(int index)
497 {
498 int dd, err;
499 uint8_t mode = (SCAN_PAGE | SCAN_INQUIRY);
500
501 dd = hci_open_dev(index);
502 if (dd < 0)
503 return -EIO;
504
505 err = hci_send_cmd(dd, OGF_HOST_CTL, OCF_WRITE_SCAN_ENABLE,
506 1, &mode);
507 if (err < 0)
508 err = -errno;
509
510 hci_close_dev(dd);
511
512 return err;
513 }
514
hciops_set_class(int index,uint32_t class)515 static int hciops_set_class(int index, uint32_t class)
516 {
517 int dd, err;
518 write_class_of_dev_cp cp;
519
520 dd = hci_open_dev(index);
521 if (dd < 0)
522 return -EIO;
523
524 memcpy(cp.dev_class, &class, 3);
525
526 err = hci_send_cmd(dd, OGF_HOST_CTL, OCF_WRITE_CLASS_OF_DEV,
527 WRITE_CLASS_OF_DEV_CP_SIZE, &cp);
528
529 if (err < 0)
530 err = -errno;
531
532 hci_close_dev(dd);
533
534 return err;
535 }
536
hciops_set_limited_discoverable(int index,uint32_t class,gboolean limited)537 static int hciops_set_limited_discoverable(int index, uint32_t class,
538 gboolean limited)
539 {
540 int dd, err;
541 int num = (limited ? 2 : 1);
542 uint8_t lap[] = { 0x33, 0x8b, 0x9e, 0x00, 0x8b, 0x9e };
543 write_current_iac_lap_cp cp;
544
545 /*
546 * 1: giac
547 * 2: giac + liac
548 */
549 dd = hci_open_dev(index);
550 if (dd < 0)
551 return -EIO;
552
553 memset(&cp, 0, sizeof(cp));
554 cp.num_current_iac = num;
555 memcpy(&cp.lap, lap, num * 3);
556
557 err = hci_send_cmd(dd, OGF_HOST_CTL, OCF_WRITE_CURRENT_IAC_LAP,
558 (num * 3 + 1), &cp);
559 if (err < 0) {
560 err = -errno;
561 hci_close_dev(dd);
562 return err;
563 }
564
565 hci_close_dev(dd);
566
567 return hciops_set_class(index, class);
568 }
569
hciops_start_discovery(int index,gboolean periodic)570 static int hciops_start_discovery(int index, gboolean periodic)
571 {
572 uint8_t lap[3] = { 0x33, 0x8b, 0x9e };
573 int dd, err;
574
575 dd = hci_open_dev(index);
576 if (dd < 0)
577 return -EIO;
578
579 if (periodic) {
580 periodic_inquiry_cp cp;
581
582 memset(&cp, 0, sizeof(cp));
583 memcpy(&cp.lap, lap, 3);
584 cp.max_period = htobs(24);
585 cp.min_period = htobs(16);
586 cp.length = 0x08;
587 cp.num_rsp = 0x00;
588
589 err = hci_send_cmd(dd, OGF_LINK_CTL, OCF_PERIODIC_INQUIRY,
590 PERIODIC_INQUIRY_CP_SIZE, &cp);
591 } else {
592 inquiry_cp inq_cp;
593
594 memset(&inq_cp, 0, sizeof(inq_cp));
595 memcpy(&inq_cp.lap, lap, 3);
596 inq_cp.length = 0x08;
597 inq_cp.num_rsp = 0x00;
598
599 err = hci_send_cmd(dd, OGF_LINK_CTL, OCF_INQUIRY,
600 INQUIRY_CP_SIZE, &inq_cp);
601 }
602
603 if (err < 0)
604 err = -errno;
605
606 hci_close_dev(dd);
607
608 return err;
609 }
610
hciops_stop_discovery(int index)611 static int hciops_stop_discovery(int index)
612 {
613 struct hci_dev_info di;
614 int dd, err;
615
616 if (hci_devinfo(index, &di) < 0)
617 return -errno;
618
619 dd = hci_open_dev(index);
620 if (dd < 0)
621 return -EIO;
622
623 if (hci_test_bit(HCI_INQUIRY, &di.flags))
624 err = hci_send_cmd(dd, OGF_LINK_CTL, OCF_INQUIRY_CANCEL,
625 0, 0);
626 else
627 err = hci_send_cmd(dd, OGF_LINK_CTL, OCF_EXIT_PERIODIC_INQUIRY,
628 0, 0);
629 if (err < 0)
630 err = -errno;
631
632 hci_close_dev(dd);
633
634 return err;
635 }
636
hciops_resolve_name(int index,bdaddr_t * bdaddr)637 static int hciops_resolve_name(int index, bdaddr_t *bdaddr)
638 {
639 remote_name_req_cp cp;
640 int dd, err;
641
642 dd = hci_open_dev(index);
643 if (dd < 0)
644 return -EIO;
645
646 memset(&cp, 0, sizeof(cp));
647 bacpy(&cp.bdaddr, bdaddr);
648 cp.pscan_rep_mode = 0x02;
649
650 err = hci_send_cmd(dd, OGF_LINK_CTL, OCF_REMOTE_NAME_REQ,
651 REMOTE_NAME_REQ_CP_SIZE, &cp);
652 if (err < 0)
653 err = -errno;
654
655 hci_close_dev(dd);
656
657 return err;
658 }
659
hciops_set_name(int index,const char * name)660 static int hciops_set_name(int index, const char *name)
661 {
662 change_local_name_cp cp;
663 int dd, err;
664
665 dd = hci_open_dev(index);
666 if (dd < 0)
667 return -EIO;
668
669 memset(&cp, 0, sizeof(cp));
670 strncpy((char *) cp.name, name, sizeof(cp.name));
671
672 err = hci_send_cmd(dd, OGF_HOST_CTL, OCF_CHANGE_LOCAL_NAME,
673 CHANGE_LOCAL_NAME_CP_SIZE, &cp);
674 if (err < 0)
675 err = -errno;
676
677 hci_close_dev(dd);
678
679 return err;
680 }
681
hciops_read_name(int index)682 static int hciops_read_name(int index)
683 {
684 int dd, err;
685
686 dd = hci_open_dev(index);
687 if (dd < 0)
688 return -EIO;
689
690 err = hci_send_cmd(dd, OGF_HOST_CTL, OCF_READ_LOCAL_NAME, 0, 0);
691 if (err < 0)
692 err = -errno;
693
694 hci_close_dev(dd);
695
696 return err;
697 }
698
hciops_cancel_resolve_name(int index,bdaddr_t * bdaddr)699 static int hciops_cancel_resolve_name(int index, bdaddr_t *bdaddr)
700 {
701 remote_name_req_cancel_cp cp;
702 int dd, err;
703
704 dd = hci_open_dev(index);
705 if (dd < 0)
706 return -EIO;
707
708 memset(&cp, 0, sizeof(cp));
709 bacpy(&cp.bdaddr, bdaddr);
710
711 err = hci_send_cmd(dd, OGF_LINK_CTL, OCF_REMOTE_NAME_REQ_CANCEL,
712 REMOTE_NAME_REQ_CANCEL_CP_SIZE, &cp);
713 if (err < 0)
714 err = -errno;
715
716 hci_close_dev(dd);
717
718 return err;
719 }
720
721 static struct btd_adapter_ops hci_ops = {
722 .setup = hciops_setup,
723 .cleanup = hciops_cleanup,
724 .start = hciops_start,
725 .stop = hciops_stop,
726 .set_powered = hciops_powered,
727 .set_connectable = hciops_connectable,
728 .set_discoverable = hciops_discoverable,
729 .set_limited_discoverable = hciops_set_limited_discoverable,
730 .start_discovery = hciops_start_discovery,
731 .stop_discovery = hciops_stop_discovery,
732 .resolve_name = hciops_resolve_name,
733 .cancel_resolve_name = hciops_cancel_resolve_name,
734 .set_name = hciops_set_name,
735 .read_name = hciops_read_name,
736 .set_class = hciops_set_class,
737 };
738
hciops_init(void)739 static int hciops_init(void)
740 {
741 return btd_register_adapter_ops(&hci_ops);
742 }
hciops_exit(void)743 static void hciops_exit(void)
744 {
745 btd_adapter_cleanup_ops(&hci_ops);
746 }
747
748 BLUETOOTH_PLUGIN_DEFINE(hciops, VERSION,
749 BLUETOOTH_PLUGIN_PRIORITY_LOW, hciops_init, hciops_exit)
750