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